From b488d9e41dee50bfe63cff59e1fe9839da2d6a75 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Tue, 17 Nov 2026 08:05:19 +0300 Subject: [PATCH] totk.exe --plugin-init runs a mod plugin's init on our kernel and records what it did (#845) The plugin service run loads the modules as boot does, runs sdk's DT_INIT and then each plugin's from nn::init::Start's module-init callback (rtld initialises the last loaded module first), writes the record and exits before any game constructor, graphics or audio. The record holds every word the init wrote into a game module, the plugin's image before and after its init, the process-memory maps, and the svcs and imports it called. exlaunch's init needs a Switch-like address space: QueryMemory shows the game's own rtld image below main and the host's ranges as Inaccessible (it takes Code RX runs for modules), and a 64 GiB free pool for its random alias picks. New svcs: GetSystemTick, GetInfo 65001 (current process handle), and Map/UnmapProcessMemory, which only the service run serves: an alias is a copy whose changed words go back into the source when it is unmapped. Any other svc, such as the CreateSession fallback, ends the run with a reason. nn::oe::GetDisplayVersion is answered with the build's game version. The game calls none of these svcs; the SVC trace test is still one mask test per SVC. After the design in #845 by itaskaev2, with the init run natively instead of under Unicorn. Co-Authored-By: Claude Opus 5.5 --- docs/settings.md | 1 + runtime/boot.c | 22 ++- runtime/cfg.c | 2 +- runtime/dispatch.h | 6 +- runtime/host.h | 23 ++- runtime/kernel.h | 6 + runtime/loader.c | 59 +++++- runtime/main.c | 7 +- runtime/memory.c | 4 + runtime/plugin_init.c | 429 ++++++++++++++++++++++++++++++++++++++++++ runtime/svc.c | 22 ++- 11 files changed, 559 insertions(+), 22 deletions(-) create mode 100644 runtime/plugin_init.c diff --git a/docs/settings.md b/docs/settings.md index b887a1af..6f23cdda 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -24,6 +24,7 @@ Many rows below are developer tools that only act together with another setting | `core_diag` | `1` records why ready guest threads wait for a virtual core and for how long, with a histogram every 60 presents. | Turns on host-call and SVC depth tracking and writes many log lines (one per present over 25 ms); frames get slower. | Caution | | `barrier_merge` (default `0`) | Experimental. `1` keeps a render pass open through the game's Barrier calls that ask only for fragment ordering and texture invalidation, when no draw of the pass can write a storage buffer or image; the next draw that samples a written target gets a by-region barrier in the pass (the feedback layout). In scenes with about a thousand such barriers a frame this cuts the passes from about 1200 to 130 and the GPU time from 24.5 to 7.8 ms. | The game's barrier orders every pixel, the in-pass by-region barrier only the pixel a draw writes: a driver that runs it per screen tile can show flicker or squares at the edges of water, decals or particles. | Caution | | `menu_cursor_log` | `1` logs what the menu cursor code reads at every screen change. | Only adds log lines. | Safe | +| `plugin_tick_offset` (default `0`) | Added to every GetSystemTick in the plugin service run (`totk.exe --plugin-init DIR`), so `tools/exefs_plugins.py` can run a plugin's init twice with other ticks and compare the records. | Has no effect outside `--plugin-init`; the game never asks for GetSystemTick. | Safe | | `perf_slow_ms` (default `50`) | Frames at least this many milliseconds long get a `perf: slow frame` log line with the time blocked per cause. | Changes how many frames are logged; a small value fills the log. | Safe | | `replay_check` | `1` reads every texture read-back a second time after a full drain of the replay queue and compares the two. | Stalls the replay queue at every read-back, so frames get much slower; mismatches are only counted and logged. | Caution | | `resolve_check` | `1` compares every cached pool lookup with the locked lookup and stops on a difference. | Slows every pool lookup; the game stops if the two ever differ. | Caution | diff --git a/runtime/boot.c b/runtime/boot.c index bd099b8c..14028825 100644 --- a/runtime/boot.c +++ b/runtime/boot.c @@ -77,6 +77,7 @@ static void cb_loader_ready(Cpu *c) { // afterwards (MacNativeGames loader.c:458-462). static void cb_module_inits(Cpu *c) { if (plat_cas32(&inits_done, 0, 0)) rt_fatal("boot: module constructors requested twice"); + if (rt_config()->plugin_init) plugin_init_run(c, module_named("sdk")); // the plugin service run ends there for (unsigned k = 0; k < sizeof inits_121 / sizeof *inits_121; k++) { const RtLoaded *m = module_named(expected_inits[k].name); uint64_t entries = m->init_array_size / 8; @@ -118,8 +119,8 @@ void rt_boot_event(const char *what, Cpu *c, const char *detail) { rt_fatal("boot: checkpoint %s reached from %s, expected main+%#llx", detail, at, (unsigned long long)CHECKPOINT_RET); if (!plat_cas32(&inits_done, 0, 0)) rt_fatal("boot: checkpoint %s before DT_INIT", detail); - for (int i = 0; i < ldr_count(); i++) - if (ctor_state(ldr_module(i)) != CTOR_DONE) + for (int i = 0; i < ldr_count(); i++) // a plugin's init does not run at play time (plugin_init.c) + if (!ldr_module(i)->plugin && ctor_state(ldr_module(i)) != CTOR_DONE) rt_fatal("boot: checkpoint %s: %s constructor flag reads %u", detail, ldr_module(i)->name, ctor_state(ldr_module(i))); plat_xchg32(&checkpoint_seen, 1); @@ -341,6 +342,23 @@ static void prewarm_main(void *arg) { return; } +// --plugin-init DIR: the modules load and nn::init::Start runs as at boot, and its module-init callback runs sdk's +// DT_INIT and each plugin's instead of the game's constructors, writes the record and exits (plugin_init.c). No game +// constructor, nnMain, shader prewarm, graphics or audio runs. +void rt_boot_plugin_init(void) { + check_npdm(); + plugin_init_setup(); + ldr_load(rt_config()->exefs); + rt_patches_init(); // the generated code reads the patch page + cb_addr[0] = hostfn_register("", cb_loader_ready); + cb_addr[1] = hostfn_register("", cb_module_inits); + cb_addr[2] = hostfn_register("", cb_module_finis); + hostfn_seal(); + rt_guest_start_ns = kern_now_ns(); + kern_run_on_guest_main(guest_main); + rt_fatal("boot: the guest main thread ended"); +} + void rt_boot(void) { check_npdm(); if (!rt_config()->shot) vk_pipeline_async_enable(); // a --shot image must not depend on build timing diff --git a/runtime/cfg.c b/runtime/cfg.c index 38d58207..5dd06739 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -56,7 +56,7 @@ static const char *const known[] = { "MEM_RESERVE_MIB", "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_NO_CODE", "MODS_ORDER", "MODS_REBUILD", "MODS_WITHOUT_CODE", "MOTION", "MOUSE_AIM_SCALE", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_ASPECT", "MOVIE_HOST", "MOVIE_PACE", "MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK", "MV_SURVEY", - "ONE_PER_CORE", "OPERATION_MODE", "OPTREF_GUARD", "OUTPUT_RES", "PAD_LAYOUT", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_SCENE", "PCORE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POINTER_IMAGE", + "ONE_PER_CORE", "OPERATION_MODE", "OPTREF_GUARD", "OUTPUT_RES", "PAD_LAYOUT", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_SCENE", "PCORE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "PLUGIN_TICK_OFFSET", "POINTER_IMAGE", "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_BENCH", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", "PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_ASYNC", "REC_END", "REC_SPIN_US", "REEXEC", "REFLEX", "RENDER_SCALE", "REPLAY_ASYNC", "REPLAY_CHUNKS", "REPLAY_CHUNK_DRAWS", "REPLAY_SKEW_US", "REPLAY_STATE_CHECK", "REPLAY_WORKERS", diff --git a/runtime/dispatch.h b/runtime/dispatch.h index a46e66e3..81165b17 100644 --- a/runtime/dispatch.h +++ b/runtime/dispatch.h @@ -19,8 +19,10 @@ typedef struct RtModule { } RtModule; // The module glue that tools/build_game.py writes for each build (build//modules.c), from layout.json and -// layout.h: every module of the layout, and the modules linked into this executable. -typedef struct RtLayoutEntry { const char *name; uint64_t base, size; } RtLayoutEntry; +// layout.h: every module of the layout, and the modules linked into this executable. A mod's code module +// (recomp/layout.py plugin, after sdk) also names its NSO file and that file's SHA-256; a game module has NULL there +// and is read from the ExeFS directory. +typedef struct RtLayoutEntry { const char *name; uint64_t base, size; const char *file, *sha256; } RtLayoutEntry; extern const RtLayoutEntry rt_layout[]; extern const int rt_layout_count; int rt_linked_modules(RtModule *out, int max); diff --git a/runtime/host.h b/runtime/host.h index 152e1b82..3d84371c 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -25,7 +25,9 @@ void rt_log_raw(const char *p, size_t n); const char *rt_log_path(void); // the log file, NULL when this run writes none enum { RT_TRACE_SVC = 1, RT_TRACE_HOSTCALL = 2, RT_TRACE_IPC = 4, RT_TRACE_FS = 8, RT_TRACE_GFX = 16, RT_TRACE_PERF = 32, RT_TRACE_SCHED = 64, RT_TRACE_AUDIO = 128, RT_TRACE_PRESENT = 256, - RT_TRACE_HOSTCOUNT = 512 }; // no TOTK_TRACE name of its own: hostfn_count switches it (host.h) + RT_TRACE_HOSTCOUNT = 512, // no TOTK_TRACE name of its own: hostfn_count switches it (host.h) + RT_TRACE_PLUGIN = 1024 }; // no name either: the plugin service run (plugin_init.c) sees every SVC through + // rt_plugin_svc while it runs a plugin's init extern unsigned rt_trace; // from TOTK_TRACE=svc,host,ipc,fs,gfx,perf,sched,audio,present (or all) // prof_win.c: TOTK_PROFILE= sampling profiler; TOTK_TRACE=perf frame timing (waits of the render thread) // PERF_TEX: texture uploads on the render thread (deswizzle, decode, staging), PERF_ASTC included @@ -150,6 +152,7 @@ uint32_t mem_set_attribute(uint64_t a, uint64_t n, uint32_t mask, uint32_t at); uint32_t mem_map(uint64_t dst, uint64_t src, uint64_t n); // 0x04 uint32_t mem_unmap(uint64_t dst, uint64_t src, uint64_t n); // 0x05 uint32_t mem_query(uint64_t a, RtMemoryInfo *out); // 0x06 +extern void (*mem_query_hook)(uint64_t a, RtMemoryInfo *r); // plugin service run only: adjusts mem_query's answer uint32_t mem_map_physical(uint64_t a, uint64_t n); // 0x2C uint32_t mem_unmap_physical(uint64_t a, uint64_t n); // 0x2D int mem_info(uint32_t id, uint64_t *out); // GetInfo memory ids 2-7, 12-17, 21, 22, 28; 0 if not a memory id @@ -199,13 +202,20 @@ int rt_oom_forced_left(int site); // forced failures st typedef struct { const char *name; uint64_t base, size, text_end, data_off; // text_end, data_off: offsets from base uint64_t init, init_array, init_array_size, ctor_flag; // absolute guest addresses; init_array_size in bytes + int plugin; // a mod's code module (layout plugin), not a game module } RtLoaded; void ldr_load(const char *exefs_dir); // load, relocate (overrides applied), seal; once +extern GuestFn ldr_plugin_import; // set before ldr_load (plugin_init.c): every JUMP_SLOT + // import of a plugin binds to this observer int ldr_count(void); const RtLoaded *ldr_module(int i); // order: main, subsdk0, sdk uint64_t ldr_symbol(const char *name); // exported definition, 0 if none uint64_t ldr_plt_stub(const char *module, const char *name); // PLT stub whose GOT slot holds name, 0 if none const char *ldr_describe(uint64_t a, char *buf, int n); // "main+0x1234 (symbol+0x10)", "host ", or the address +uint64_t ldr_nso_size(const char *dir, const char *file); // image size of an ExeFS NSO (SHA-256 checked) +void ldr_map_image(const char *dir, const char *file, uint64_t base, uint64_t *text_end, uint64_t *data_off); + // its segments at base (reserved by the caller), not + // linked: the plugin service run's rtld (plugin_init.c) uint8_t *ldr_read_verified(const char *dir, const char *file, size_t *len); // whole file, SHA-256 checked against // modules.c (fatal on mismatch); free() it @@ -358,13 +368,22 @@ int rt_knob(const char *name, const char *def); void frame_step_present(uint64_t display_ns, int interval); // nvnQueuePresentTexture: the frame's display time and // present interval (60 Hz ticks) // ---- boot (boot.c, main.c) -typedef struct { const char *exefs, *romfs, *profile; int boot; double seconds; const char *shot, *gpu; } RtConfig; +typedef struct { + const char *exefs, *romfs, *profile; + int boot; + double seconds; + const char *shot, *gpu; + const char *plugin_init; // --plugin-init DIR: the plugin service run writes its record there (plugin_init.c) +} RtConfig; // romfs: directory with romfs.bin and index.json; // frames: --frames N (0 = no limit); shot: --shot // PATH; gpu: TOTK_GPU const RtConfig *rt_config(void); void rt_boot_event(const char *what, Cpu *c, const char *detail); // checkpoint log; "nvn" ends --boot void rt_boot(void); // boot.c +void rt_boot_plugin_init(void); // boot.c: --plugin-init, the plugin service run +void plugin_init_setup(void); // plugin_init.c: before ldr_load +void plugin_init_run(Cpu *c, const RtLoaded *sdk); // plugin_init.c: from the module-init callback; exits // Modes of totk.exe. main.c has run mem_init and parsed the paths; each mode calls ldr_load itself if it needs the // modules, registers its host functions, then seals (hostfn_seal, or implicitly at the first rt_call). Return the // process exit code. diff --git a/runtime/kernel.h b/runtime/kernel.h index d719b301..d232f68d 100644 --- a/runtime/kernel.h +++ b/runtime/kernel.h @@ -38,3 +38,9 @@ extern volatile int kern_test_cv_old_order; // WaitProcessWideKeyAtomi // the key (MNG's pre-fix order, the negative control) extern void (*volatile kern_test_cv_hook)(void); // called right after the lock release, holding the kernel lock extern void (*volatile svc_test_exit_hook)(unsigned code); // svc 0x07 calls it instead of ExitProcess (probes) + +// ---- mod plugin service run (plugin_init.c sets these; the game calls none of the SVCs they serve) +extern uint64_t rt_svc_tick_offset; // added to GetSystemTick (0x1E): the service run's second run picks other ticks +extern void (*rt_plugin_svc)(Cpu *c, uint32_t imm, uint64_t pc); // with RT_TRACE_PLUGIN: sees each SVC first +extern uint32_t (*rt_process_memory)(int unmap, uint64_t dst, uint32_t process, uint64_t src, uint64_t size); + // Map/UnmapProcessMemory (0x74/0x75); NULL outside the service run: fatal diff --git a/runtime/loader.c b/runtime/loader.c index 970c5fb7..4e7f833b 100644 --- a/runtime/loader.c +++ b/runtime/loader.c @@ -52,6 +52,7 @@ typedef struct { } Mod; static Mod mods[MOD_COUNT]; +GuestFn ldr_plugin_import; static int nmods; static uint64_t identity_refs, stubs; static int *ovr_used, *obs_used; // per linked table entry, plus probe overrides counted separately @@ -74,6 +75,17 @@ static void hex32(const uint8_t d[32], char out[65]) { for (int i = 0; i < 32; i++) snprintf(out + 2 * i, 3, "%02x", d[i]); } +static uint8_t *read_path_verified(const char *path, const char *want, size_t *len, const char *hint) { + uint8_t *p = read_file(path, len); + if (!p) rt_fatal("loader: cannot read %s (%s)", path, hint); + uint8_t d[32]; + char hex[65]; + rt_sha256(p, *len, d); + hex32(d, hex); + if (strcmp(hex, want)) rt_fatal("loader: %s has SHA-256 %s, expected %s", path, hex, want); + return p; +} + uint8_t *ldr_read_verified(const char *dir, const char *file, size_t *len) { const char *want = NULL; for (int i = 0; i < rt_exefs_sha256_count; i++) @@ -81,14 +93,7 @@ uint8_t *ldr_read_verified(const char *dir, const char *file, size_t *len) { if (!want) rt_fatal("loader: no expected SHA-256 for %s in modules.c", file); char path[1024]; snprintf(path, sizeof path, "%s/%s", dir, file); - uint8_t *p = read_file(path, len); - if (!p) rt_fatal("loader: cannot read %s (extract the game first: tools/extract.py)", path); - uint8_t d[32]; - char hex[65]; - rt_sha256(p, *len, d); - hex32(d, hex); - if (strcmp(hex, want)) rt_fatal("loader: %s has SHA-256 %s, expected %s", path, hex, want); - return p; + return read_path_verified(path, want, len, "extract the game first: tools/extract.py"); } // ---- NSO @@ -141,6 +146,32 @@ static void nso_segment(const uint8_t *raw, const Seg *s, uint8_t *dst, const ch } } +// The plugin service run's stand-in for rtld (plugin_init.c): an ExeFS NSO's image, read and checked like a module but +// neither relocated, linked nor sealed. Nothing runs it; exlaunch only reads it while it walks the address space. +uint64_t ldr_nso_size(const char *dir, const char *file) { + size_t len; + uint8_t *raw = ldr_read_verified(dir, file, &len); + Seg seg[3]; + uint64_t size = nso_segments(raw, len, file, seg); + free(raw); + return size; +} + +void ldr_map_image(const char *dir, const char *file, uint64_t base, uint64_t *text_end, uint64_t *data_off) { + size_t len; + uint8_t *raw = ldr_read_verified(dir, file, &len); + Seg seg[3]; + uint64_t size = nso_segments(raw, len, file, seg); + if (!plat_mem_commit(base, size)) rt_fatal("loader: cannot commit %s at %#llx", file, (unsigned long long)base); + for (int i = 0; i < 3; i++) { + if ((uint64_t)seg[i].mem_off + seg[i].size > size) rt_fatal("loader: %s segment %d exceeds the image", file, i); + nso_segment(raw, &seg[i], (uint8_t *)(uintptr_t)(base + seg[i].mem_off), file, i); + } + free(raw); + *text_end = seg[0].mem_off + seg[0].size; + *data_off = seg[2].mem_off; +} + // ---- symbols static const char *sym_name(const Mod *m, uint32_t i) { return m->strtab + m->syms[i].name; } static int sym_defined(const Mod *m, uint32_t i) { return m->syms[i].shndx != 0; } @@ -273,9 +304,12 @@ static uint64_t decode_ctor_flag(const Mod *m) { return 0; } +// A plugin (lay->file set) comes from its mod folder, checked against the hash the build took when it lifted it. static void load_module(Mod *m, const char *dir, const RtLayoutEntry *lay) { size_t len; - uint8_t *raw = ldr_read_verified(dir, lay->name, &len); + uint8_t *raw = lay->file ? read_path_verified(lay->file, lay->sha256, &len, "the mod's code module the build " + "lifted; rebuild after changing mods") + : ldr_read_verified(dir, lay->name, &len); Seg seg[3]; uint64_t size = nso_segments(raw, len, lay->name, seg); if (size != lay->size) @@ -291,6 +325,7 @@ static void load_module(Mod *m, const char *dir, const RtLayoutEntry *lay) { } free(raw); m->pub.name = lay->name; + m->pub.plugin = lay->file != NULL; m->pub.base = lay->base; m->pub.size = size; m->pub.text_end = seg[0].mem_off + seg[0].size; @@ -328,7 +363,11 @@ static Res resolve(Mod *m, uint32_t si, int jump) { uint64_t guest = sym_defined(m, si) ? m->pub.base + m->syms[si].value : find_symbol(name, NULL); GuestFn f; Res r; - if (jump && (f = hostfn_observer(name))) { + if (jump && m->pub.plugin && ldr_plugin_import) { // the plugin service run counts the plugin's calls by name + uint64_t to = (f = hostfn_override(name)) ? hostfn_register(name, f) : guest; + if (!to) to = hostfn_register(name, unresolved_stub); + r = (Res){hostfn_register_observer(name, ldr_plugin_import, to), RES_OBSERVER}; + } else if (jump && (f = hostfn_observer(name))) { r = (Res){hostfn_register_observer(name, f, guest), RES_OBSERVER}; mark_used(1, name); } else if ((f = hostfn_override(name))) { diff --git a/runtime/main.c b/runtime/main.c index 9ce8517a..d84f2306 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -611,7 +611,7 @@ static int usage(const char *argv0) { "--probe-cfg | --probe-dpi | --probe-mods | --probe-playtime | --probe-kernel | --probe-ipc | --probe-services | --probe-fs | --probe-gfx-abi | " "--probe-nvn | --probe-nvn-tex | --probe-window | --probe-shader | --probe-vkmem | " "--probe-vkexec | --probe-astc | --probe-movie | --probe-crash-call | --probe-crash-null | --probe-discord | " - "--prepare-shaders | --warm-shaders | --boot |--seconds S [--shot PNG]]\n", argv0); + "--prepare-shaders | --warm-shaders | --boot | --plugin-init DIR | --seconds S [--shot PNG]]\n", argv0); return 2; } @@ -700,6 +700,10 @@ int main(int argc, char **argv) { } else if (!strcmp(argv[i], "--shot")) { if (++i == argc) return usage(argv[0]); cfg.shot = argv[i]; + } else if (!mode && !strcmp(argv[i], "--plugin-init")) { // the plugin service run (plugin_init.c) + if (++i == argc) return usage(argv[0]); + mode = argv[i - 1]; + cfg.plugin_init = argv[i]; } else if (!mode && !strncmp(argv[i], "--", 2)) { mode = argv[i]; } else { @@ -744,5 +748,6 @@ int main(int argc, char **argv) { if (!strcmp(mode, "--prepare-shaders")) return vk_pipeline_prepare_shaders(); if (!strcmp(mode, "--warm-shaders")) return vk_pipeline_warm_shaders(); if (!strcmp(mode, "--boot")) { cfg.boot = 1; rt_boot(); return 0; } + if (!strcmp(mode, "--plugin-init")) { rt_boot_plugin_init(); return 0; } return usage(argv[0]); } diff --git a/runtime/memory.c b/runtime/memory.c index 078f119a..c452c398 100644 --- a/runtime/memory.c +++ b/runtime/memory.c @@ -562,6 +562,9 @@ static uint64_t run_start(const uint8_t *p, uint64_t i, uint8_t v) { // first return i; } +// The plugin service run (plugin_init.c) shows rtld, its process-memory aliases and its free pool here. +void (*mem_query_hook)(uint64_t a, RtMemoryInfo *r); + uint32_t mem_query(uint64_t a, RtMemoryInfo *out) { RtMemoryInfo r = {.base = a & ~(PAGE - 1), .size = PAGE}; uint64_t low = L.aslr.base; @@ -632,6 +635,7 @@ uint32_t mem_query(uint64_t a, RtMemoryInfo *out) { r.size = hi - lo; } plat_mutex_unlock_shared(&lock); + if (mem_query_hook) mem_query_hook(a, &r); if (out) *out = r; return R_OK; } diff --git a/runtime/plugin_init.c b/runtime/plugin_init.c new file mode 100644 index 00000000..96486584 --- /dev/null +++ b/runtime/plugin_init.c @@ -0,0 +1,429 @@ +// The plugin service run: totk.exe --plugin-init DIR runs the DT_INIT of every mod code module (recomp/layout.py +// plugin, an exlaunch plugin such as a mod's exefs/subsdk9) on this runtime's own kernel and sdk, records what the +// init did and exits before any game module's constructor, nnMain, graphics or audio. The record is the input of +// tools/exefs_plugins.py (checks) and, later, of the generator's hook sites; at play time a plugin's init does not run. +// The design follows #845 by itaskaev2, with the init run natively instead of under Unicorn. +// +// What exlaunch's init needs from the process, and how this run gives it: +// - Its module walk (exl::util::FindModules) reads QueryMemory from 0 up and takes every Code RX, Code R, CodeData +// RW run as a module, the first being rtld and the second main. Our layout has no rtld and reports the host's +// ranges as Code RX (memory.c taken_gap), which would make it take a gap for a module. mem_query_hook shows the +// game's own rtld NSO (exefs/rtld, read, never run) right below main, and the host's ranges as Inaccessible. +// - It picks a random free range in the ASLR region (GetSystemTick seeds it) for its process-memory aliases. Our +// ASLR region is almost all host ranges, so a free pool of POOL_SIZE is reserved after the alias region. +// - GetInfo 65001 (svc.c) gives it the current process handle; MapProcessMemory maps main's .text and its own pages +// at the picked address. Guest addresses are host addresses and the modules are not file-backed, so an alias is a +// copy: its pages hold the source's bytes when mapped, and the words written through it are written into the +// source when it is unmapped and when the init returns. That is how the stubs reach the plugin's image and the +// hook branches reach main's .text. +// - nn::oe::GetDisplayVersion is answered with this build's game version, so the plugin picks its table for the +// installed game without the applet services. +// Only svcs the init needs are allowed (QueryMemory, GetSystemTick, OutputDebugString, GetInfo 2-5, 12-15 and 65001, +// Map/UnmapProcessMemory); any other one, such as CreateSession and ReplyAndReceive of exlaunch's process-handle +// fallback, ends the run with the reason in the record. +// +// The record, in DIR: record.json (modules, the writes into every game module as words, the alias calls, svc and +// import counts, the tick offset, a refusal), plugin.img (the plugin's image after its init, all segments) and +// plugin.pre.img (the same after loading and relocation, before the init). TOTK_PLUGIN_TICK_OFFSET adds to every +// GetSystemTick, so two runs pick different aliases. +#include +#include +#include + +#include "cfg.h" +#include "host.h" +#include "kernel.h" +#include "platform/platform.h" + +#define PAGE 0x1000ull +#define GiB (1ull << 30) +#define POOL_SIZE (64 * GiB) +#define POOL_ALIGN (4 * GiB) +#define RTLD_ALIGN 0x200000ull +#define MAX_ALIASES 256 +#define MAX_IMPORTS 64 +#define CUR_PROCESS 0xFFFF8001u +// Horizon memory states and permissions of QueryMemory, and results. +enum { ST_FREE = 0, ST_CODE = 3, ST_CODEDATA = 4, ST_SHARED_CODE = 0xF, ST_INACCESSIBLE = 0x10 }; +enum { PERM_R = 1, PERM_RW = 3, PERM_RX = 5 }; +#define R_OK 0u +#define R_INVALID_ADDRESS 0xcc01u +#define R_INVALID_STATE 0xd401u +#define R_INVALID_HANDLE 0xe401u +#define R_LIMIT_REACHED 0x10801u +#define SYM_DISPLAY_VERSION "_ZN2nn2oe17GetDisplayVersionEPNS0_14DisplayVersionE" + +typedef struct { + uint64_t dst, src, size; + uint8_t *snap; // the source's bytes when mapped: the words that differ from it were written through the alias + int live, unmapped; +} Alias; +static Alias aliases[MAX_ALIASES]; +static int naliases; +static uint64_t rtld_base, rtld_size, rtld_text_end, rtld_data_off; +static uint64_t pool_base; +static int running; // a plugin's init runs: svcs and imports are counted +static uint64_t svc_count[0x80], info_count[16], info_65001; +static struct { const char *name; uint64_t calls; } imports[MAX_IMPORTS]; +static int nimports; +static uint8_t **pre; // per loaded module: its image before the plugins' inits + +static uint64_t align_up(uint64_t v, uint64_t a) { return (v + a - 1) & ~(a - 1); } + +static void clip(uint64_t a, uint64_t *lo, uint64_t *hi, uint64_t b, uint64_t n) { + if (!n) return; + uint64_t e = b + n; + if (e <= a && e > *lo) *lo = e; + if (b > a && b < *hi) *hi = b; +} + +static void clip_aliases(uint64_t a, uint64_t *lo, uint64_t *hi) { + for (int i = 0; i < naliases; i++) + if (aliases[i].live) clip(a, lo, hi, aliases[i].dst, aliases[i].size); +} + +static int in_module(uint64_t a) { + for (int i = 0; i < ldr_count(); i++) + if (a - ldr_module(i)->base < ldr_module(i)->size) return 1; + return 0; +} + +// ---- QueryMemory as exlaunch expects it (memory.c mem_query_hook) +static void query_hook(uint64_t a, RtMemoryInfo *r) { + for (int i = 0; i < naliases; i++) { + const Alias *al = &aliases[i]; + if (al->live && a - al->dst < al->size) { + *r = (RtMemoryInfo){.base = al->dst, .size = al->size, .state = ST_SHARED_CODE, .perm = PERM_RW}; + return; + } + } + if (a - rtld_base < rtld_size) { + uint64_t off = a - rtld_base, text = align_up(rtld_text_end, PAGE); + if (off < text) *r = (RtMemoryInfo){.base = rtld_base, .size = text, .state = ST_CODE, .perm = PERM_RX}; + else if (off < rtld_data_off) + *r = (RtMemoryInfo){.base = rtld_base + text, .size = rtld_data_off - text, .state = ST_CODE, .perm = PERM_R}; + else + *r = (RtMemoryInfo){.base = rtld_base + rtld_data_off, .size = rtld_size - rtld_data_off, + .state = ST_CODEDATA, .perm = PERM_RW}; + return; + } + uint64_t lo = r->base, hi = r->base + r->size; + if (a - pool_base < POOL_SIZE) { + lo = pool_base, hi = pool_base + POOL_SIZE; + *r = (RtMemoryInfo){.state = ST_FREE}; + } else if (r->state == ST_CODE && !in_module(a)) { // a host range (memory.c taken_gap) + clip(a, &lo, &hi, rtld_base, rtld_size); + clip(a, &lo, &hi, pool_base, POOL_SIZE); + *r = (RtMemoryInfo){.state = ST_INACCESSIBLE}; + } else if (r->state != ST_FREE) { + return; + } + clip_aliases(a, &lo, &hi); + r->base = lo; + r->size = hi - lo; +} + +// ---- Map/UnmapProcessMemory (svc.c rt_process_memory) +static const RtLoaded *module_holding(uint64_t a, uint64_t n) { + for (int i = 0; i < ldr_count(); i++) { + const RtLoaded *m = ldr_module(i); + if (a >= m->base && n <= m->size && a - m->base <= m->size - n) return m; + } + return NULL; +} + +// Writes the words changed through an alias into its source. The source's read-only pages (sealed .text and .rodata) +// are made writable for the copy and sealed again. +static void flush(Alias *al) { + const RtLoaded *m = module_holding(al->src, al->size); + const uint8_t *now = (const uint8_t *)(uintptr_t)al->dst; + for (uint64_t off = 0; off < al->size; off += PAGE) { + if (!memcmp(now + off, al->snap + off, PAGE)) continue; + uint64_t page = al->src + off; + int sealed = page - m->base < m->data_off; + if (sealed && !plat_mem_protect(page, PAGE, PLAT_PROT_RW)) rt_fatal("plugin-init: cannot unseal %#llx", (unsigned long long)page); + for (uint64_t w = 0; w < PAGE; w += 4) + if (memcmp(now + off + w, al->snap + off + w, 4)) memcpy((void *)(uintptr_t)(page + w), now + off + w, 4); + if (sealed && !plat_mem_protect(page, PAGE, PLAT_PROT_READ)) rt_fatal("plugin-init: cannot seal %#llx", (unsigned long long)page); + } + memcpy(al->snap, now, al->size); // flushed: a later flush writes only what changes after this one +} + +static uint32_t process_memory(int unmap, uint64_t dst, uint32_t process, uint64_t src, uint64_t size) { + if (process != CUR_PROCESS) return R_INVALID_HANDLE; + if (!size || ((dst | src | size) & (PAGE - 1))) return R_INVALID_ADDRESS; + if (unmap) { + for (int i = 0; i < naliases; i++) { + Alias *al = &aliases[i]; + if (!al->live || al->dst != dst || al->src != src || al->size != size) continue; + flush(al); + if (!plat_mem_decommit(dst, size)) rt_fatal("plugin-init: cannot unmap the alias at %#llx", (unsigned long long)dst); + free(al->snap); + al->snap = NULL; + al->live = 0; + al->unmapped = 1; + return R_OK; + } + return R_INVALID_STATE; + } + if (!module_holding(src, size)) return R_INVALID_STATE; // only the modules' own memory + RtMemoryInfo q; + mem_query(dst, &q); + if (q.state != ST_FREE || dst + size > q.base + q.size) return R_INVALID_STATE; + if (naliases == MAX_ALIASES) return R_LIMIT_REACHED; + if (!plat_mem_commit(dst, size)) return R_INVALID_STATE; // not inside one of our reservations + Alias *al = &aliases[naliases]; + al->snap = malloc(size); + if (!al->snap) rt_fatal("plugin-init: out of memory for a %#llx byte alias", (unsigned long long)size); + memcpy((void *)(uintptr_t)dst, (const void *)(uintptr_t)src, size); + memcpy(al->snap, (const void *)(uintptr_t)src, size); + *al = (Alias){dst, src, size, al->snap, 1, 0}; + naliases++; + return R_OK; +} + +// ---- svcs and imports +static void write_record(const char *refused); + +static void refuse(const char *why) { + rt_log("plugin-init: refused: %s", why); + write_record(why); + fflush(NULL); + plat_exit(3); +} + +static void plugin_svc(Cpu *c, uint32_t imm, uint64_t pc) { + if (!running) return; + svc_count[imm & 0x7F]++; + char why[256], at[128]; + switch (imm) { + case 0x06: case 0x1E: case 0x27: case 0x74: case 0x75: return; + case 0x29: { + uint32_t id = (uint32_t)c->x[1]; + if ((id >= 2 && id <= 5) || (id >= 12 && id <= 15)) { + info_count[id]++; + return; + } + if (id == 65001) { + info_65001++; + return; + } + snprintf(why, sizeof why, "GetInfo id %u at %s", id, ldr_describe(pc, at, sizeof at)); + refuse(why); + } + } + snprintf(why, sizeof why, "svc %#x at %s (only QueryMemory, GetSystemTick, OutputDebugString, GetInfo 2-5, 12-15 " + "and 65001, Map/UnmapProcessMemory are served)", imm, ldr_describe(pc, at, sizeof at)); + refuse(why); +} + +// Every JUMP_SLOT import of a plugin binds here (loader.c resolve): counted by name, then the guest definition runs, +// except nn::oe::GetDisplayVersion, which gets this build's game version. +static void plugin_import(Cpu *c) { + const char *name = hostfn_name(c->pc); + int i = 0; + while (i < nimports && strcmp(imports[i].name, name)) i++; + if (i == nimports && nimports < MAX_IMPORTS) imports[nimports++].name = name; + if (i < MAX_IMPORTS && running) imports[i].calls++; + if (!strcmp(name, SYM_DISPLAY_VERSION)) { // DisplayVersion { char value[16]; } + char v[16] = {0}; + snprintf(v, sizeof v, "%d.%d.%d", rt_game_version / 100, rt_game_version / 10 % 10, rt_game_version % 10); + memcpy((void *)(uintptr_t)c->x[0], v, sizeof v); + return; + } + rt_original(c); +} + +// ---- the record +static FILE *open_out(const char *file, const char *mode) { + char path[PLAT_PATH_MAX]; + snprintf(path, sizeof path, "%s/%s", rt_config()->plugin_init, file); + FILE *f = fopen(path, mode); + if (!f) rt_fatal("plugin-init: cannot write %s", path); + return f; +} + +static void write_image(const char *file, const void *p, uint64_t n) { + FILE *f = open_out(file, "wb"); + if (fwrite(p, 1, n, f) != n || fclose(f)) rt_fatal("plugin-init: cannot write %s", file); +} + +static void sha256_hex(const void *p, uint64_t n, char out[65]) { + uint8_t d[32]; + rt_sha256(p, n, d); + for (int i = 0; i < 32; i++) snprintf(out + 2 * i, 3, "%02x", d[i]); +} + +static void json_str(FILE *f, const char *s) { + fputc('"', f); + for (; *s; s++) { + if (*s == '"' || *s == '\\') fputc('\\', f); + if ((unsigned char)*s < 0x20) fprintf(f, "\\u%04x", *s); + else fputc(*s, f); + } + fputc('"', f); +} + +// The words of each game module that differ from its image before the plugins' inits: (module, offset, old, new). +static void write_writes(FILE *f) { + int first = 1; + for (int i = 0; i < ldr_count(); i++) { + const RtLoaded *m = ldr_module(i); + if (m->plugin || !pre || !pre[i]) continue; + const uint8_t *now = (const uint8_t *)(uintptr_t)m->base; + for (uint64_t off = 0; off < m->size; off += PAGE) { + if (!memcmp(now + off, pre[i] + off, PAGE)) continue; + for (uint64_t w = off; w < off + PAGE; w += 4) { + uint32_t a, b; + memcpy(&a, pre[i] + w, 4); + memcpy(&b, now + w, 4); + if (a == b) continue; + // fn_start: a lifted function starts at the word (a b there replaces the function) + fprintf(f, "%s\n {\"module\": \"%s\", \"offset\": \"%#llx\", \"old\": \"%#010x\", \"new\": \"%#010x\", " + "\"fn_start\": %s}", first ? "" : ",", m->name, (unsigned long long)w, a, b, + rt_dispatch_lookup(m->base + w) ? "true" : "false"); + first = 0; + } + } + } +} + +static void write_record(const char *refused) { + for (int i = 0; i < naliases; i++) + if (aliases[i].live) flush(&aliases[i]); + FILE *f = open_out("record.json", "w"); + // The marker of generated game-derived files (tools/check_content.py): the record never goes into the repository. + fprintf(f, "{\n\"marker\": \"%s%s\",\n\"game_version\": %d,\n\"tick_offset\": \"%#llx\",\n\"refused\": ", + "TOTK_RECOMP_", "GENERATED_FROM_GAME_CODE", rt_game_version, (unsigned long long)rt_svc_tick_offset); + if (refused) json_str(f, refused); + else fputs("null", f); + fprintf(f, ",\n\"rtld\": {\"base\": \"%#llx\", \"size\": \"%#llx\"},\n\"pool\": {\"base\": \"%#llx\", \"size\": \"%#llx\"},", + (unsigned long long)rtld_base, (unsigned long long)rtld_size, (unsigned long long)pool_base, + (unsigned long long)POOL_SIZE); + fputs("\n\"modules\": [", f); + for (int i = 0; i < ldr_count(); i++) { + const RtLoaded *m = ldr_module(i); + fprintf(f, "%s\n {\"name\": \"%s\", \"base\": \"%#llx\", \"size\": \"%#llx\", \"text_end\": \"%#llx\", " + "\"data_off\": \"%#llx\", \"init\": \"%#llx\", \"plugin\": %s", + i ? "," : "", m->name, (unsigned long long)m->base, (unsigned long long)m->size, + (unsigned long long)m->text_end, (unsigned long long)m->data_off, (unsigned long long)m->init, + m->plugin ? "true" : "false"); + if (m->plugin && !refused) { + char img[64], pre_img[64], hex[65]; + snprintf(img, sizeof img, "%s.img", m->name); + snprintf(pre_img, sizeof pre_img, "%s.pre.img", m->name); + write_image(img, (const void *)(uintptr_t)m->base, m->size); + if (pre && pre[i]) write_image(pre_img, pre[i], m->size); + sha256_hex((const void *)(uintptr_t)m->base, m->size, hex); + fprintf(f, ", \"image\": \"%s\", \"image_sha256\": \"%s\", \"pre_image\": \"%s\"", img, hex, pre_img); + } + if (m->plugin) { // the lifted function starts: a main write may branch straight to one (tools/exefs_plugins.py) + RtModule lm[16]; + int n = rt_linked_modules(lm, 16); + for (int k = 0; k < n; k++) { + if (strcmp(lm[k].name, m->name)) continue; + fputs(", \"functions\": [", f); + for (uint64_t j = 0; j < lm[k].count; j++) + fprintf(f, "%s\"%#llx\"", j ? ", " : "", (unsigned long long)(lm[k].addrs[j] - m->base)); + fputc(']', f); + } + } + for (int k = 0; k < rt_layout_count; k++) + if (rt_layout[k].file && !strcmp(rt_layout[k].name, m->name)) { + fputs(", \"file\": ", f); + json_str(f, rt_layout[k].file); + fprintf(f, ", \"nso_sha256\": \"%s\"", rt_layout[k].sha256); + } + fputc('}', f); + } + fputs("\n],\n\"writes\": [", f); + write_writes(f); + fputs("\n],\n\"aliases\": [", f); + for (int i = 0; i < naliases; i++) + fprintf(f, "%s\n {\"dst\": \"%#llx\", \"src\": \"%#llx\", \"size\": \"%#llx\", \"unmapped\": %s}", i ? "," : "", + (unsigned long long)aliases[i].dst, (unsigned long long)aliases[i].src, + (unsigned long long)aliases[i].size, aliases[i].unmapped ? "true" : "false"); + fputs("\n],\n\"svcs\": {", f); + int first = 1; + for (int i = 0; i < 0x80; i++) + if (svc_count[i]) fprintf(f, "%s\"%#x\": %llu", first ? "" : ", ", i, (unsigned long long)svc_count[i]), first = 0; + fputs("},\n\"get_info\": {", f); + first = 1; + for (int i = 0; i < 16; i++) + if (info_count[i]) fprintf(f, "%s\"%d\": %llu", first ? "" : ", ", i, (unsigned long long)info_count[i]), first = 0; + if (info_65001) fprintf(f, "%s\"65001\": %llu", first ? "" : ", ", (unsigned long long)info_65001); + fputs("},\n\"imports\": {", f); + for (int i = 0; i < nimports; i++) { + fputs(i ? ", " : "", f); + json_str(f, imports[i].name); + fprintf(f, ": %llu", (unsigned long long)imports[i].calls); + } + fputs("}\n}\n", f); + if (fclose(f)) rt_fatal("plugin-init: cannot write record.json"); +} + +// ---- setup and run +// Before ldr_load: the import observer, the svc hooks, the pool and the stand-in for rtld. +void plugin_init_setup(void) { + int plugins = 0; + for (int i = 0; i < rt_layout_count; i++) plugins += rt_layout[i].file != NULL; + if (!plugins) rt_fatal("plugin-init: this build's layout has no plugin module (recomp/gen.py --plugins)"); + plat_file_mkdir_p(rt_config()->plugin_init); + const char *off = cfg_get("TOTK_PLUGIN_TICK_OFFSET"); + rt_svc_tick_offset = off ? strtoull(off, NULL, 0) : 0; + ldr_plugin_import = plugin_import; + rt_process_memory = process_memory; + rt_plugin_svc = plugin_svc; + + const RtGuestLayout *g = mem_layout(); + uint64_t main_base = rt_layout[0].base; + rtld_size = ldr_nso_size(rt_config()->exefs, "rtld"); + rtld_base = main_base - align_up(rtld_size, RTLD_ALIGN); + for (uint64_t at = align_up(g->alias.base + g->alias.size, POOL_ALIGN); at + POOL_SIZE <= rtld_base && !pool_base; + at += POOL_ALIGN) + if (plat_mem_reserve(at, POOL_SIZE, 0)) pool_base = at; + if (!pool_base) rt_fatal("plugin-init: no free %llu GiB below main for the alias pool", POOL_SIZE / GiB); + if (!plat_mem_reserve(rtld_base, align_up(rtld_size, PAGE), 0)) + rt_fatal("plugin-init: %#llx below main is taken; rtld goes there", (unsigned long long)rtld_base); + ldr_map_image(rt_config()->exefs, "rtld", rtld_base, &rtld_text_end, &rtld_data_off); + mem_query_hook = query_hook; + rt_log("plugin-init: rtld at %#llx+%#llx, alias pool %#llx+%llu GiB, tick offset %#llx", (unsigned long long)rtld_base, + (unsigned long long)rtld_size, (unsigned long long)pool_base, POOL_SIZE / GiB, + (unsigned long long)rt_svc_tick_offset); +} + +// From nn::init::Start's module-init callback (boot.c), in place of the game's constructors. rtld initialises the +// modules from the last loaded to the first, so on the Switch sdk's DT_INIT runs before a subsdk plugin's; then each +// plugin's, the last first. Exits with the record written. +void plugin_init_run(Cpu *c, const RtLoaded *sdk) { + rt_log("plugin-init: ENTER %s DT_INIT", sdk->name); + rt_call(c, sdk->init, 0); + pre = calloc((size_t)ldr_count(), sizeof *pre); + if (!pre) rt_fatal("plugin-init: out of memory"); + for (int i = 0; i < ldr_count(); i++) { // the images before the plugins run + const RtLoaded *m = ldr_module(i); + pre[i] = malloc(m->size); + if (!pre[i]) rt_fatal("plugin-init: out of memory for a copy of %s", m->name); + memcpy(pre[i], (const void *)(uintptr_t)m->base, m->size); + } + for (int i = ldr_count() - 1; i >= 0; i--) { + const RtLoaded *m = ldr_module(i); + if (!m->plugin) continue; + if (!m->init) refuse("a plugin without DT_INIT"); + rt_log("plugin-init: ENTER %s DT_INIT at %#llx (%llu init_array entries)", m->name, (unsigned long long)m->init, + (unsigned long long)(m->init_array_size / 8)); + running = 1; + rt_trace |= RT_TRACE_PLUGIN; + rt_call(c, m->init, 0); + rt_trace &= ~(unsigned)RT_TRACE_PLUGIN; + running = 0; + rt_log("plugin-init: %s DT_INIT returned", m->name); + } + write_record(NULL); + int live = 0; + for (int i = 0; i < naliases; i++) live += aliases[i].live; + rt_log("plugin-init: record written to %s (%d process-memory maps, %d still mapped)", rt_config()->plugin_init, + naliases, live); + fflush(NULL); + plat_exit(0); +} diff --git a/runtime/svc.c b/runtime/svc.c index 27cdad19..1f62f62d 100644 --- a/runtime/svc.c +++ b/runtime/svc.c @@ -3,6 +3,8 @@ // The SVC entry of the lifted code: preemption checkpoint, trace, then the thread and // synchronization SVCs (kernel.c), IPC (ipc.c), memory (memory.c), Break, OutputDebugString, GetInfo and ExitProcess. The 21 SVCs // TotK has but never reaches on MNG's boot path stop with a named fatal, the caller and the guest backtrace. +// GetSystemTick, GetInfo 65001 and Map/UnmapProcessMemory are for mod plugins (exlaunch): the game calls none of them, +// and process memory is mapped only in the plugin service run (plugin_init.c). #include #include @@ -17,9 +19,13 @@ #define ST_STACK 0xBu #define PERM_RW 3u #define PROGRAM_ID 0x0100f2c0115b6000ull +#define CUR_PROCESS 0xFFFF8001u // Horizon's pseudo handle of the current process static uint64_t g_svcs; void (*volatile svc_test_exit_hook)(unsigned code); +uint64_t rt_svc_tick_offset; +void (*rt_plugin_svc)(Cpu *c, uint32_t imm, uint64_t pc); +uint32_t (*rt_process_memory)(int unmap, uint64_t dst, uint32_t process, uint64_t src, uint64_t size); RtStats rt_stats(void) { return (RtStats){__atomic_load_n(&g_svcs, __ATOMIC_RELAXED), kern_threads_created(), hostfn_calls()}; @@ -121,6 +127,7 @@ static void get_info(Cpu *c, uint64_t pc) { case 18: v = PROGRAM_ID; break; case 20: v = user_exception_context(); break; // the SDK keeps e.g. the dying message at +0x1C0 case 23: v = 1; break; + case 65001: v = CUR_PROCESS; break; // Mesosphere's current process handle (exlaunch's way to its own memory) default: { char why[64]; snprintf(why, sizeof why, "GetInfo id %u (handle %#x, sub id %#llx)", id, (uint32_t)c->x[2], @@ -151,11 +158,13 @@ void rt_svc(Cpu *c, uint32_t imm, uint64_t pc) { static void svc_body(Cpu *c, uint32_t imm, uint64_t pc) { __atomic_fetch_add(&g_svcs, 1, __ATOMIC_RELAXED); - if (rt_trace & RT_TRACE_SVC) { + if (rt_trace & (RT_TRACE_SVC | RT_TRACE_PLUGIN)) { + if ((rt_trace & RT_TRACE_PLUGIN) && rt_plugin_svc) rt_plugin_svc(c, imm, pc); char at[160]; - rt_log("svc %#x at %s x0=%#llx x1=%#llx x2=%#llx x3=%#llx", imm, ldr_describe(pc, at, sizeof at), - (unsigned long long)c->x[0], (unsigned long long)c->x[1], (unsigned long long)c->x[2], - (unsigned long long)c->x[3]); + if (rt_trace & RT_TRACE_SVC) + rt_log("svc %#x at %s x0=%#llx x1=%#llx x2=%#llx x3=%#llx", imm, ldr_describe(pc, at, sizeof at), + (unsigned long long)c->x[0], (unsigned long long)c->x[1], (unsigned long long)c->x[2], + (unsigned long long)c->x[3]); } if (kern_svc(c, imm)) return; uint64_t *x = c->x; @@ -210,6 +219,11 @@ static void svc_body(Cpu *c, uint32_t imm, uint64_t pc) { return; } case 0x29: get_info(c, pc); return; + case 0x1E: x[0] = rt_ticks() + rt_svc_tick_offset; return; // GetSystemTick (19.2 MHz) + case 0x74: case 0x75: // MapProcessMemory / UnmapProcessMemory(x0 dst, w1 process, x2 src, x3 size) + if (!rt_process_memory) svc_fatal(c, imm, pc, "process memory is mapped only in the plugin service run"); + x[0] = rt_process_memory(imm == 0x75, x[0], (uint32_t)x[1], x[2], x[3]); + return; case 0x07: { // ExitProcess, as closing the window (gfx_window_closed): pipeline cache, shader records and // profile saved, logs flushed, exit code 0 char at[160];