PS2 games in the browser: static recompilation to WebAssembly with a native runtime
C++
modload LoadStartModule, StartModule and LoadModuleBuffer
reikop5bcd5fe3aa65
diff --git a/runtime/src/iop/iop.hpp b/runtime/src/iop/iop.hppindex bbcc121..275374b 100644--- a/runtime/src/iop/iop.hpp+++ b/runtime/src/iop/iop.hpp@@ -73,6 +73,9 @@ void dump();std::int32_t load_module(const std::string &path, const std::string &args, std::function<void(int, int)> done = {});std::int32_t load_module_buffer(const std::vector<std::uint8_t> &data, const std::string &path, const std::string &args,std::function<void(int, int)> done = {});+// modload's LoadModuleBuffer and StartModule: link a module without running it, then run its start routine+std::int32_t link_module(const std::vector<std::uint8_t> &data, const std::string &path);+bool start_module(int id, const std::string &args, std::function<void(int, int)> done);void set_trace(bool modules, bool hle);}diff --git a/runtime/src/iop/kernel.cpp b/runtime/src/iop/kernel.cppindex 25a3d79..9349970 100644--- a/runtime/src/iop/kernel.cpp+++ b/runtime/src/iop/kernel.cpp@@ -60,6 +60,7 @@ std::map<std::string, std::uint32_t> g_hle_versions;std::map<std::string, std::uint32_t> g_libraries;std::map<std::string, HleModuleStart> g_hle_modules;std::map<int, Module> g_modules;+std::map<int, std::string> g_module_paths;int g_next_module = 1;std::map<int, Thread> g_threads;@@ -524,6 +525,7 @@ void boot() {g_libraries.clear();g_hle_modules.clear();g_modules.clear();+ g_module_paths.clear();g_threads.clear();g_used.clear();while (!g_events.empty()) g_events.pop();@@ -656,6 +658,14 @@ std::int32_t load_module_buffer(const std::vector<std::uint8_t> &data, const stdif (done) done(id, 0);return id;}+ const std::int32_t id = link_module(data, path);+ if (id < 0) return id;+ start_module(id, args, std::move(done));+ return id;+}++std::int32_t link_module(const std::vector<std::uint8_t> &data, const std::string &path) {+ using namespace kernel;if (data.size() < 52 || std::memcmp(data.data(), "\x7f" "ELF", 4) != 0) {std::fprintf(stderr, "[iop] %s is not an ELF module\n", path.c_str());return -1;@@ -688,11 +698,26 @@ std::int32_t load_module_buffer(const std::vector<std::uint8_t> &data, const stdrelocate(elf, base, total, type == 0xFF81u);link_imports(base, total);+ // a module handed over in memory has no file name, the one in its .iopmod header stands in+ std::string name = module_name_of(path);+ if (path == "buffer") {+ std::string own;+ for (std::size_t i = iopmod + 26; i < data.size() && data[i] && own.size() < 56; ++i) own += static_cast<char>(data[i]);+ if (!own.empty()) name = own;+ }const int id = g_next_module++;g_modules[id] = Module{id, name, base, total, base + entry, base + gp};+ g_module_paths[id] = path;if (g_trace_modules) std::fprintf(stderr, "[iop] module %d %s at 0x%08X size 0x%X entry 0x%08X\n", id, name.c_str(), base, total, base + entry);+ return id;+}- std::vector<std::string> argv{path};+bool start_module(int id, const std::string &args, std::function<void(int, int)> done) {+ using namespace kernel;+ auto found = g_modules.find(id);+ if (found == g_modules.end() || !found->second.entry) return false;+ const std::uint32_t entry = found->second.entry, gp = found->second.gp;+ std::vector<std::string> argv{g_module_paths[id]};std::size_t start = 0;while (start < args.size()) {const auto end = args.find('\0', start);@@ -711,7 +736,7 @@ std::int32_t load_module_buffer(const std::vector<std::uint8_t> &data, const std}set_ram32(argv_at + 4u * static_cast<std::uint32_t>(argv.size()), 0);- const int tid = create_thread(base + entry, 0x4000u, kDefaultModulePriority, base + gp, 0x02000000u, 0);+ const int tid = create_thread(entry, 0x4000u, kDefaultModulePriority, gp, 0x02000000u, 0);Thread *t = thread(tid);t->on_exit = [id, tid, argv_at, done]() {const std::uint32_t result = thread(tid)->context.r[2];@@ -724,7 +749,7 @@ std::int32_t load_module_buffer(const std::vector<std::uint8_t> &data, const std}};start_thread(tid, static_cast<std::uint32_t>(argv.size()), argv_at);- return id;+ return true;}void set_trace(bool modules_on, bool hle_on) {diff --git a/runtime/src/iop/libs.cpp b/runtime/src/iop/libs.cppindex a1d7569..849e0d2 100644--- a/runtime/src/iop/libs.cpp+++ b/runtime/src/iop/libs.cpp@@ -26,6 +26,7 @@ constexpr int kErrorSemaZero = -419;constexpr int kErrorEventCondition = -421;constexpr int kErrorMbxNone = -424;constexpr int kErrorNoMemory = -400;+constexpr int kErrorIllegalObject = -201;constexpr int kErrorUnknownModule = -202;std::uint32_t arg(const Cpu &c, int i) { return i < 4 ? c.r[4 + i] : read32(c.r[29] + 16u + static_cast<std::uint32_t>(i - 4) * 4u); }@@ -983,6 +984,44 @@ void install_stdio() {void install_modload() {register_hle_library("modload", 0x0102);+ // the calling thread waits until the module's start routine returns, *result gets what it returned+ const auto start_and_wait = [](Cpu &c, int id, std::uint32_t arglen, std::uint32_t args, std::uint32_t result_at) {+ Thread *caller = current();+ if (!caller) { c.r[2] = static_cast<std::uint32_t>(kErrorUnknownModule); return; }+ std::string text;+ if (args && arglen && phys(args) + arglen <= kRamSize) text.assign(reinterpret_cast<const char *>(ram_ptr(args)), arglen);+ const int waiter = caller->id;+ const bool started = start_module(id, text, [waiter, result_at](int module, int result) {+ if (result_at) write32(result_at, static_cast<std::uint32_t>(result));+ if (Thread *t = thread(waiter)) wake(*t, static_cast<std::uint32_t>(module));+ });+ if (!started) { c.r[2] = static_cast<std::uint32_t>(kErrorUnknownModule); return; }+ block(c, kWaitHle, 0);+ };+ def("modload", 7, "LoadStartModule", [](Cpu &c) {+ Thread *caller = current();+ if (!caller) { c.r[2] = static_cast<std::uint32_t>(kErrorUnknownModule); return; }+ std::string text;+ const std::uint32_t arglen = arg(c, 1), args = arg(c, 2), result_at = arg(c, 3);+ if (args && arglen && phys(args) + arglen <= kRamSize) text.assign(reinterpret_cast<const char *>(ram_ptr(args)), arglen);+ const int waiter = caller->id;+ const std::int32_t id = load_module(ram_string(arg(c, 0), 256), text, [waiter, result_at](int module, int result) {+ if (result_at) write32(result_at, static_cast<std::uint32_t>(result));+ if (Thread *t = thread(waiter)) wake(*t, static_cast<std::uint32_t>(module));+ });+ if (id < 0) { c.r[2] = static_cast<std::uint32_t>(kErrorIllegalObject); return; }+ block(c, kWaitHle, 0);+ });+ def("modload", 8, "StartModule", [start_and_wait](Cpu &c) {+ start_and_wait(c, static_cast<int>(arg(c, 0)), arg(c, 2), arg(c, 3), arg(c, 4));+ });+ def("modload", 10, "LoadModuleBuffer", [](Cpu &c) {+ const std::uint32_t at = phys(arg(c, 0));+ if (at >= kRamSize) { c.r[2] = static_cast<std::uint32_t>(kErrorIllegalObject); return; }+ const std::uint8_t *p = ram_ptr(at);+ const std::int32_t id = link_module(std::vector<std::uint8_t>(p, p + (kRamSize - at)), "buffer");+ c.r[2] = static_cast<std::uint32_t>(id < 0 ? kErrorIllegalObject : id);+ });def("modload", 13, "SetCheckKelfPathCallback", [](Cpu &c) { c.r[2] = 0; });def("modload", 16, "GetModuleIdList", [](Cpu &c) {std::uint32_t n = 0;