Add system call intercepts to Lua

This commit is contained in:
wheremyfoodat 2025-07-03 13:02:10 +03:00
parent 620e3699ec
commit 228068901b
7 changed files with 147 additions and 34 deletions

View file

@ -3,9 +3,9 @@
#include "kernel_types.hpp"
#include "cpu.hpp"
Kernel::Kernel(CPU& cpu, Memory& mem, GPU& gpu, const EmulatorConfig& config)
: cpu(cpu), regs(cpu.regs()), mem(mem), handleCounter(0), serviceManager(regs, mem, gpu, currentProcess, *this, config) {
objects.reserve(512); // Make room for a few objects to avoid further memory allocs later
Kernel::Kernel(CPU& cpu, Memory& mem, GPU& gpu, const EmulatorConfig& config, LuaManager& lua)
: cpu(cpu), regs(cpu.regs()), mem(mem), handleCounter(0), serviceManager(regs, mem, gpu, currentProcess, *this, config, lua) {
objects.reserve(512); // Make room for a few objects to avoid further memory allocs later
mutexHandles.reserve(8);
portHandles.reserve(32);
threadIndices.reserve(appResourceLimits.maxThreads);

View file

@ -5,8 +5,10 @@
#include "ipc.hpp"
#include "kernel.hpp"
ServiceManager::ServiceManager(std::span<u32, 16> regs, Memory& mem, GPU& gpu, u32& currentPID, Kernel& kernel, const EmulatorConfig& config)
: regs(regs), mem(mem), kernel(kernel), ac(mem), am(mem), boss(mem), act(mem), apt(mem, kernel), cam(mem, kernel), cecd(mem, kernel),
ServiceManager::ServiceManager(
std::span<u32, 16> regs, Memory& mem, GPU& gpu, u32& currentPID, Kernel& kernel, const EmulatorConfig& config, LuaManager& lua
)
: regs(regs), mem(mem), kernel(kernel), lua(lua), ac(mem), am(mem), boss(mem), act(mem), apt(mem, kernel), cam(mem, kernel), cecd(mem, kernel),
cfg(mem, config), csnd(mem, kernel), dlp_srvr(mem), dsp(mem, kernel, config), hid(mem, kernel), http(mem), ir_user(mem, hid, config, kernel),
frd(mem), fs(mem, kernel, config), gsp_gpu(mem, gpu, kernel, currentPID), gsp_lcd(mem), ldr(mem, kernel), mcu_hwc(mem, config),
mic(mem, kernel), nfc(mem, kernel), nim(mem), ndm(mem), news_u(mem), ns(mem), nwm_uds(mem, kernel), ptm(mem, config), soc(mem), ssl(mem),
@ -214,6 +216,28 @@ void ServiceManager::publishToSubscriber(u32 messagePointer) {
}
void ServiceManager::sendCommandToService(u32 messagePointer, Handle handle) {
if (interceptedServices.size() != 0) [[unlikely]] {
// Check if there's a Lua handler for this function and call it
u32 function = mem.read32(messagePointer);
for (auto [serviceName, serviceHandle] : serviceMap) {
if (serviceHandle == handle) {
auto intercept = InterceptedService(std::string(serviceName), function);
if (interceptedServices.contains(intercept)) {
printf("Call to intercepted service\n");
// If the Lua handler returns true, it means the service is handled entirely
// From Lua, and we shouldn't do anything else here.
if (lua.signalInterceptedService(intercept.serviceName, function, messagePointer)) {
return;
}
}
break;
}
}
}
switch (handle) {
// Breaking alphabetical order a bit to place the ones I think are most common at the top
case KernelHandles::GPU: [[likely]] gsp_gpu.handleSyncRequest(messagePointer); break;