Skip to content

Commit

Permalink
Merge pull request #1721 from nicolasnoble/kernel-takeover
Browse files Browse the repository at this point in the history
Allowing psyqo to optionally take over the kernel.
  • Loading branch information
nicolasnoble authored Sep 2, 2024
2 parents 9653cea + 404b8af commit 85db6de
Show file tree
Hide file tree
Showing 18 changed files with 525 additions and 79 deletions.
3 changes: 3 additions & 0 deletions src/mips/common/hardware/flushcache.s
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ SOFTWARE.
.section .text.flushCache, "ax", @progbits
.align 2
.set noreorder
.global _ZN5psyqo6Kernel10flushCacheEv
.type _ZN5psyqo6Kernel10flushCacheEv, @function
.global flushCache
.type flushCache, @function

_ZN5psyqo6Kernel10flushCacheEv:
flushCache:
/* Saves $ra to $t6, and the current cop0 Status register to $t0, and ensure we
are running from uncached ram. */
Expand Down
18 changes: 12 additions & 6 deletions src/mips/common/syscalls/syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ static __attribute__((always_inline)) int changeThreadSubFunction(uint32_t addre
}

/* A0 table */
static __attribute__((always_inline)) size_t syscall_write(int fd, const void *buf, size_t size) {
register int n asm("t1") = 0x03;
__asm__ volatile("" : "=r"(n) : "r"(n));
return ((size_t (*)(int, const void *, size_t))0xa0)(fd, buf, size);
}

static __attribute__((always_inline)) int syscall_setjmp(struct JmpBuf *buf) {
register int n asm("t1") = 0x13;
__asm__ volatile("" : "=r"(n) : "r"(n));
Expand Down Expand Up @@ -179,6 +185,12 @@ static __attribute__((always_inline)) void syscall__exit(int code) {
((void (*)(int))0xa0)(code);
}

static __attribute__((always_inline)) void syscall_puts(const char *msg) {
register int n asm("t1") = 0x3e;
__asm__ volatile("" : "=r"(n) : "r"(n));
((void (*)(const char *))0xa0)(msg);
}

// doing this one in raw inline assembly would prove tricky,
// and there's already enough voodoo in this file.
// this is syscall a0:3f
Expand Down Expand Up @@ -440,12 +452,6 @@ static __attribute__((always_inline)) void syscall_putchar(int c) {
((void (*)(int))0xb0)(c);
}

static __attribute__((always_inline)) void syscall_puts(const char *msg) {
register int n asm("t1") = 0x3f;
__asm__ volatile("" : "=r"(n) : "r"(n));
((void (*)(const char *))0xb0)(msg);
}

static __attribute__((always_inline)) int syscall_addDevice(const struct Device *device) {
register int n asm("t1") = 0x47;
__asm__ volatile("" : "=r"(n) : "r"(n));
Expand Down
2 changes: 2 additions & 0 deletions src/mips/psyqo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ SRCS = \
../common/crt0/cxxglue.c \
../common/crt0/memory-c.c \
../common/crt0/memory-s.s \
../common/hardware/flushcache.s \
../common/syscalls/printf.s \
$(wildcard src/hardware/*.cpp) \
$(wildcard src/*.cpp) \
$(wildcard src/*.c) \
$(wildcard src/*.s) \

CPPFLAGS = -I../../../third_party/EASTL/include -I../../../third_party/EABase/include/Common
CXXFLAGS = -std=c++20
Expand Down
1 change: 1 addition & 0 deletions src/mips/psyqo/application.hh
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class Application {
eastl::fixed_vector<Scene*, 16> m_scenesStack;

friend class Scene;
friend void Kernel::takeOverKernel();
};

} // namespace psyqo
14 changes: 14 additions & 0 deletions src/mips/psyqo/examples/hello-nokernel/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
TARGET = hello
TYPE = ps-exe

SRCS = \
hello.cpp \

LDFLAGS += -Xlinker --defsym=TLOAD_ADDR=0x80001000

ifeq ($(TEST),true)
CPPFLAGS = -Werror
endif
CXXFLAGS = -std=c++20

include ../../psyqo.mk
94 changes: 94 additions & 0 deletions src/mips/psyqo/examples/hello-nokernel/hello.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
MIT License
Copyright (c) 2024 PCSX-Redux authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "psyqo/application.hh"
#include "psyqo/font.hh"
#include "psyqo/gpu.hh"
#include "psyqo/kernel.hh"
#include "psyqo/scene.hh"

// This variant of hello world demonstrates how to let psyqo take over the kernel.
// The only difference is the call to psyqo::Kernel::takeOverKernel() in main.
// Also, this binary is linked so to be loaded at 0x80001000, regaining 60kB of RAM.
namespace {

class Hello final : public psyqo::Application {
void prepare() override;
void createScene() override;

public:
psyqo::Font<> m_systemFont;
};

class HelloScene final : public psyqo::Scene {
void frame() override;

uint8_t m_anim = 0;
bool m_direction = true;
};

Hello hello;
HelloScene helloScene;

} // namespace

void Hello::prepare() {
psyqo::GPU::Configuration config;
config.set(psyqo::GPU::Resolution::W320)
.set(psyqo::GPU::VideoMode::AUTO)
.set(psyqo::GPU::ColorMode::C15BITS)
.set(psyqo::GPU::Interlace::PROGRESSIVE);
gpu().initialize(config);
}

void Hello::createScene() {
m_systemFont.uploadSystemFont(gpu());
pushScene(&helloScene);
}

void HelloScene::frame() {
if (m_anim == 0) {
m_direction = true;
} else if (m_anim == 255) {
m_direction = false;
}
psyqo::Color bg{{.r = 0, .g = 64, .b = 91}};
bg.r = m_anim;
hello.gpu().clear(bg);
if (m_direction) {
m_anim++;
} else {
m_anim--;
}

psyqo::Color c = {{.r = 255, .g = 255, .b = uint8_t(255 - m_anim)}};
hello.m_systemFont.print(hello.gpu(), "Hello World!", {{.x = 16, .y = 32}}, c);
}

int main() {
psyqo::Kernel::takeOverKernel();
return hello.run();
}
1 change: 0 additions & 1 deletion src/mips/psyqo/examples/hello/hello.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ SOFTWARE.
*/

#include "common/syscalls/syscalls.h"
#include "psyqo/application.hh"
#include "psyqo/font.hh"
#include "psyqo/gpu.hh"
Expand Down
3 changes: 2 additions & 1 deletion src/mips/psyqo/font.hh
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class FontBase {
* method, you should not call initialize() afterwards. The Kernel rom font is a 16x15 font created
* by Sony, and built into the PSX rom chip. Its appearance is variable, depending on the version
* of the PSX bios. It may not be available on all PSX models. The footprint for this font is 192
* bytes of read-only data, and a 256x90x4bpp texture.
* bytes of read-only data, and a 256x90x4bpp texture. This font isn't going to work if psyqo
* took over the kernel. See the `Kernel` namespace for more information.
*/
void uploadKromFont(GPU& gpu, Vertex location = {{.x = 960, .y = 422}});

Expand Down
2 changes: 2 additions & 0 deletions src/mips/psyqo/gpu.hh
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ class GPU {
void chain(uint32_t *first, uint32_t *last, size_t count);
void scheduleOTC(uint32_t *start, uint32_t count);
void checkOTCAndTriggerCallback();
void prepareForTakeover();

eastl::function<void(void)> m_dmaCallback = nullptr;
unsigned m_refreshRate = 0;
Expand Down Expand Up @@ -538,6 +539,7 @@ class GPU {

void flip();
friend class Application;
friend void psyqo::Kernel::takeOverKernel();
};

} // namespace psyqo
Expand Down
85 changes: 83 additions & 2 deletions src/mips/psyqo/kernel.hh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ SOFTWARE.

namespace psyqo {

class Application;

/**
* @brief The Kernel namespace for internal use.
*
Expand Down Expand Up @@ -80,18 +82,89 @@ enum class DMA : unsigned {
Max,
};

enum class IRQ : unsigned {
VBlank,
GPU,
CDRom,
DMA,
Timer0,
Timer1,
Timer2,
Controller,
SIO,
SPU,
PIO,
Max,
};

/**
* @brief Stops the execution of the application.
*/
[[noreturn]] void abort(const char* msg, std::source_location location = std::source_location::current());

/**
* @brief Takes over the kernel. Can only be called once inside the main function.
*
* @details This function will make psyqo take over the retail kernel.
* This means the application will no longer be able to call any of the
* kernel functions, and will have to rely on the psyqo kernel instead.
* Debugging features from third party addons which hook into the kernel
* will no longer work. Most calls to the kernel will either be no-ops or
* will crash the application. Most notably, only the `printf` call will
* be redirected to psyqo's printf, but will not be printing anywhere, so
* only emulators hooking into A0 calls will be able to see the output.
*
* Disabling the kernel is a one-way operation, and cannot be undone.
* The kernel will be taken over before the first call to `prepare`.
* The exception handler that psyqo installs will not be able to catch
* problems, but is much more lightweight and faster than the retail one.
* Also, 60kB of memory can be reclaimed, and linking the binary with
* -Xlinker --defsym=TLOAD_ADDR=0x80001000 will allow the application to
* do just that. This requires a loader able to write into the kernel
* while disabling interrupts. The ps1-packer tool can achieve that.
* The first 4kB of memory is reserved for the psyqo kernel.
*
* It is noteworthy that while the pros of taking over the kernel are
* significant, the cons are also significant. The loss of debugging,
* flexibility, and retail kernel features may not be worth it for most
* application cases, and should be considered carefully.
*
* Last but not least, like with most psyqo features, the added payload
* to the binary to support the feature will only occur if this function
* is called.
*/
void takeOverKernel();

/**
* @brief Returns whether the kernel has been taken over.
*/
bool isKernelTakenOver();

/**
* @brief Queues an IRQ handler to be called from the exception handler.
*
* @details This function is used to queue an IRQ handler to be called
* from the exception handler when the kernel has been taken over. The
* VBlank IRQ is excluded from this function, as it is handled by the
* GPU object instead. Also, note that the kernel has its own DMA IRQ
* handler, and that the `registerDmaEvent` function should be used
* instead of trying to queue a handler for the DMA IRQ. The specified
* handler will be called from the exception handler, with the same
* restrictions as for any other interrupt handler.
*
* @param irq The IRQ to handle.
* @param lambda The function to call when the IRQ is triggered.
*/
void queueIRQHandler(IRQ irq, eastl::function<void()>&& lambda);

/**
* @brief A C++ wrapper around the `openEvent` syscall.
*
* @details This enables the application to register a C++ lambda
* for the kernel's OpenEvent call. This will allocate an internal
* slot, with currently no mechanism to free it. This means that
* calling `closeEvent` on the resulting event will leak resources.
* If psyqo took over the kernel, this function will no longer work.
*/
uint32_t openEvent(uint32_t classId, uint32_t spec, uint32_t mode, eastl::function<void()>&& lambda);

Expand All @@ -110,6 +183,14 @@ uint32_t openEvent(uint32_t classId, uint32_t spec, uint32_t mode, eastl::functi
*/
unsigned registerDmaEvent(DMA channel, eastl::function<void()>&& lambda);

/**
* @brief Flushes the i-cache.
*
* @details This function is used to flush the i-cache. This is
* required when the application has written some code to memory.
*/
void flushCache();

/**
* @brief Enables the given DMA channel.
*
Expand Down Expand Up @@ -157,8 +238,8 @@ void queueCallbackFromISR(eastl::function<void()>&& lambda);

namespace Internal {
void pumpCallbacks();
void prepare();
void addInitializer(eastl::function<void()>&& lambda);
void prepare(Application&);
void addInitializer(eastl::function<void(Application&)>&& lambda);
void addOnFrame(eastl::function<void()>&& lambda);
void beginFrame();
} // namespace Internal
Expand Down
4 changes: 3 additions & 1 deletion src/mips/psyqo/simplepad.hh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ namespace psyqo {
* PAD interface, and has the same caveats, namely that it should be
* initialized prior using the BIOS' memory card functions, and that
* polling will alternate between the two pads at each frame when two
* pads are connected, which can introduce input lags.
* pads are connected, which can introduce input lags. This class
* will not be able to be used if the kernel is taken over. See the
* `Kernel` namespace for more information.
*/

class SimplePad {
Expand Down
2 changes: 1 addition & 1 deletion src/mips/psyqo/src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ SOFTWARE.

int psyqo::Application::run() {
Kernel::fastEnterCriticalSection();
Kernel::Internal::prepare(*this);
syscall_puts("*** PSYQo Application - starting ***\n");
psyqo_free(psyqo_malloc(1));
ramsyscall_printf("Current heap start: %p\n", psyqo_heap_start());
ramsyscall_printf("Current heap end: %p\n", psyqo_heap_end());
Kernel::Internal::prepare();
prepare();
Kernel::fastLeaveCriticalSection();
while (true) {
Expand Down
11 changes: 8 additions & 3 deletions src/mips/psyqo/src/cdrom-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ SOFTWARE.
void psyqo::CDRomDevice::prepare() {
Hardware::CPU::IMask.set(Hardware::CPU::IRQ::CDRom);
Kernel::enableDma(Kernel::DMA::CDRom);
m_event = Kernel::openEvent(EVENT_CDROM, 0x1000, EVENT_MODE_CALLBACK, [this]() {
eastl::function<void()> callback = [this]() {
Hardware::CPU::IReg.clear(Hardware::CPU::IRQ::CDRom);
irq();
});
syscall_enableEvent(m_event);
};
if (Kernel::isKernelTakenOver()) {
Kernel::queueIRQHandler(Kernel::IRQ::CDRom, eastl::move(callback));
} else {
m_event = Kernel::openEvent(EVENT_CDROM, 0x1000, EVENT_MODE_CALLBACK, eastl::move(callback));
syscall_enableEvent(m_event);
}
}

psyqo::CDRomDevice::~CDRomDevice() { Kernel::abort("CDRomDevice can't be destroyed (yet)"); }
Expand Down
Loading

0 comments on commit 85db6de

Please # to comment.