diff --git a/runtime/boot.c b/runtime/boot.c index 14028825..d14cc301 100644 --- a/runtime/boot.c +++ b/runtime/boot.c @@ -30,6 +30,7 @@ #define CHECKPOINT_RET (RT_V143 ? 0xbdb48cull : RT_V110 ? 0xeb8c14ull : 0xe7a954ull) #define NVN_RET (RT_V143 ? 0xbe0360ull : RT_V110 ? 0xebcdecull : 0xe7eb2cull) #define CTOR_DONE 2 // a module's constructor flag after its DT_INIT ran the init_array +#define CTOR_WAITING 1 // after a DT_INIT that musl deferred (sdk's, called before main's) // The init_array sizes (1.1.0 and 1.2.1: counted in the modules; 1.4.3: DT_INIT_ARRAYSZ); MacNativeGames' boot check // expects the same. The three tables carry the same rows, so cb_module_inits walks them by the size of one. @@ -40,6 +41,7 @@ static const InitCount inits_143[] = {{"main", 10717}, {"subsdk0", 45}, {"sdk", #define expected_inits (RT_V143 ? inits_143 : RT_V110 ? inits_110 : inits_121) static volatile int32_t inits_done, checkpoint_seen; +static int sdk_first; // a mod plugin ran: sdk's DT_INIT came first (cb_module_inits) uint64_t rt_guest_start_ns; static const RtLoaded *module_named(const char *name) { @@ -78,15 +80,27 @@ static void cb_loader_ready(Cpu *c) { 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 + // A mod plugin's init runs after sdk's and before the game's (plugin_init.c plugins_start): sdk's DT_INIT then + // comes first, as on the Switch; without a plugin the order stays main, subsdk0, sdk. Called first, sdk's DT_INIT + // only registers sdk with its own musl (__nnmusl_init_dso returns 1: deferred), and main's DT_INIT then finishes + // sdk from musl's list of waiting modules (the log shows sdk's RELRO protected and 44 pages of sdk's data written + // during it on 1.4.3): sdk's flag stays 1 and its DT_INIT is not called again (a second call would protect its + // locked RELRO again, and the SDK aborts). + sdk_first = plugins_start(c, module_named("sdk")); 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; if (!m->init) rt_fatal("boot: %s has no DT_INIT", m->name); - rt_log("boot: ENTER %s DT_INIT (%llu init_array entries)", m->name, (unsigned long long)entries); - rt_call(c, m->init, 0); + int deferred = sdk_first && m == module_named("sdk"); + if (!deferred) { + rt_log("boot: ENTER %s DT_INIT (%llu init_array entries)", m->name, (unsigned long long)entries); + rt_call(c, m->init, 0); + } uint32_t state = ctor_state(m); rt_logf("boot: DT_INIT %s state=%u (%llu)\n", m->name, state, (unsigned long long)entries); - if (state != CTOR_DONE) rt_fatal("boot: %s DT_INIT left its constructor flag at %u, not 2", m->name, state); + if (state != (deferred ? CTOR_WAITING : CTOR_DONE)) + rt_fatal("boot: %s DT_INIT left its constructor flag at %u, not %d", m->name, state, + deferred ? CTOR_WAITING : CTOR_DONE); if (entries != expected_inits[k].entries) rt_fatal("boot: %s has %llu init_array entries, this game version has %llu", m->name, (unsigned long long)entries, (unsigned long long)expected_inits[k].entries); @@ -120,7 +134,8 @@ void rt_boot_event(const char *what, Cpu *c, const char *detail) { (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++) // 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) + if (!ldr_module(i)->plugin && ctor_state(ldr_module(i)) != CTOR_DONE && + !(sdk_first && !strcmp(ldr_module(i)->name, "sdk") && ctor_state(ldr_module(i)) == CTOR_WAITING)) 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); @@ -364,6 +379,7 @@ void rt_boot(void) { if (!rt_config()->shot) vk_pipeline_async_enable(); // a --shot image must not depend on build timing // Shaders and pipelines recorded in earlier runs are made ready while the modules load; the guest waits for them. PlatThread *warm = plat_thread_new(prewarm_main, NULL, "prewarm"); + plugins_setup(); // a mod plugin's environment (plugin_init.c), before its imports bind ldr_load(rt_config()->exefs); rt_patches_init(); mods_exefs_switch(); // after the patch sets' settings: an enabled mod's ExeFS patches on diff --git a/runtime/cfg.c b/runtime/cfg.c index 5dd06739..921af94d 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", "PLUGIN_TICK_OFFSET", "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", "PLUGIN_TRACE", "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.c b/runtime/dispatch.c index b284cf6c..931ea9ac 100644 --- a/runtime/dispatch.c +++ b/runtime/dispatch.c @@ -214,6 +214,21 @@ void rt_dispatch(Cpu *c) { __attribute__((musttail)) return f(c); } +// The plugin trace (plugin_init.c, TOTK_PLUGIN_TRACE): the slot of the function start a runs fn from now on. Once per +// traced function, before the game's code runs; the hot path stays as it is. 0 when a is no function start of the +// table or fn lies out of the reach of a slot's 32-bit offset. +int rt_dispatch_reroute(uint64_t a, GuestFn fn) { + uint64_t i = (a - D.lo) >> 2; + if (!g_ready || (a & 3) || a < D.lo || i >= D.slots || !D.slot[i]) return 0; + uintptr_t f = (uintptr_t)fn; + if (f <= D.anchor || f - D.anchor > UINT32_MAX) return 0; + uint64_t page = (uint64_t)(uintptr_t)&D.slot[i] & ~0xFFFull; + if (!plat_mem_protect(page, 0x1000, PLAT_PROT_RW)) return 0; + ((uint32_t *)D.slot)[i] = (uint32_t)(f - D.anchor); // the table is sealed read-only, unsealed above + plat_mem_protect(page, 0x1000, PLAT_PROT_READ); + return 1; +} + RtDispatchStats rt_dispatch_stats(void) { return (RtDispatchStats){D.lo, D.lo + D.slots * 4, D.slots, g_table_bytes, g_functions, g_nhost}; } diff --git a/runtime/dispatch.h b/runtime/dispatch.h index 81165b17..1f224f2e 100644 --- a/runtime/dispatch.h +++ b/runtime/dispatch.h @@ -38,6 +38,7 @@ void rt_dispatch_init(const RtModule *mods, int count); // The host function for a guest address, or NULL when nothing is there. Does not run guest code. GuestFn rt_dispatch_lookup(uint64_t pc); +int rt_dispatch_reroute(uint64_t a, GuestFn fn); // plugin trace only (dispatch.c); 0 when it cannot // "0x7000001234 (main+0x1234)" for a guest address inside a module, else the bare address. Returns buf. const char *rt_guest_name(uint64_t pc, char *buf, int size); diff --git a/runtime/host.h b/runtime/host.h index 3d84371c..d4f8c060 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -384,6 +384,11 @@ 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 +void plugins_setup(void); // plugin_init.c: rt_boot, before ldr_load; nothing + // without a plugin in the build +int plugins_start(Cpu *c, const RtLoaded *sdk); // plugin_init.c: from the module-init callback, before + // the game's constructors; 1 when it ran sdk's DT_INIT + // (a plugin's init ran after it) // 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 d232f68d..1114758b 100644 --- a/runtime/kernel.h +++ b/runtime/kernel.h @@ -41,6 +41,7 @@ extern void (*volatile svc_test_exit_hook)(unsigned code); // svc 0x07 calls i // ---- 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 int (*rt_plugin_svc)(Cpu *c, uint32_t imm, uint64_t pc); // with RT_TRACE_PLUGIN: sees each SVC + // first; 1: refused, the result is in x0 and the SVC does not run 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 4e7f833b..6c698ffb 100644 --- a/runtime/loader.c +++ b/runtime/loader.c @@ -86,6 +86,26 @@ static uint8_t *read_path_verified(const char *path, const char *want, size_t *l return p; } +// 1 when the plugin file of lay is the one the build lifted; logged when not. +static int plugin_file_ok(const RtLayoutEntry *lay) { + size_t len; + uint8_t *p = read_file(lay->file, &len); + if (!p) { + rt_log("loader: %s: cannot read %s; the plugin is left out", lay->name, lay->file); + return 0; + } + uint8_t d[32]; + char hex[65]; + rt_sha256(p, len, d); + free(p); + hex32(d, hex); + if (strcmp(hex, lay->sha256)) { + rt_log("loader: %s: %s changed since the build lifted it; the plugin is left out", lay->name, lay->file); + return 0; + } + return 1; +} + 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++) @@ -484,8 +504,14 @@ void ldr_load(const char *exefs_dir) { if (nmods) rt_fatal("ldr_load called twice"); if (!mem_layout()->pool) rt_fatal("ldr_load before mem_init"); if (rt_layout_count > MOD_COUNT) rt_fatal("loader: %d modules in the layout", rt_layout_count); - for (int i = 0; i < rt_layout_count; i++) load_module(&mods[i], exefs_dir, &rt_layout[i]); - nmods = rt_layout_count; + int n = 0; + for (int i = 0; i < rt_layout_count; i++) { + // A mod plugin's file that is gone or changed since the build is left out at play time: its patch set cannot + // run (plugin_init.c plugins_start keeps it off); the plugin service run needs it and stops. + if (rt_layout[i].file && !rt_config()->plugin_init && !plugin_file_ok(&rt_layout[i])) continue; + load_module(&mods[n++], exefs_dir, &rt_layout[i]); + } + nmods = n; ovr_used = calloc(hostfn_table_size(0) + 1, sizeof(int)); obs_used = calloc(hostfn_table_size(1) + 1, sizeof(int)); for (int i = 0; i < nmods; i++) { diff --git a/runtime/mods.c b/runtime/mods.c index 3f99e8dd..5d7667da 100644 --- a/runtime/mods.c +++ b/runtime/mods.c @@ -26,9 +26,14 @@ // .bss) are lines of build-cache/exefs.data, stored into the loaded main before the game runs (mods_exefs_data), as // emulators apply a patch file to the module before its dynamic linker runs. // -// Code modules: an exefs/subsdk* file is a plugin that patches main from its own start-up on the Switch, and a replaced -// exefs/main is another game code; the port cannot load either (ModInfo.code names them): their mod stays off unless -// it is used without its code, which also leaves out the ExeFS patch files the port cannot apply. +// Code modules: an exefs/subsdk* file is a plugin that patches main from its own start-up on the Switch (exlaunch). +// The setup builds it in (tools/exefs_plugins.py, after the design in #845 by itaskaev2) and writes its line into +// build-cache/plugins.status (the format of exefs.status): `plugin`, its patch set mod_ holds its hook sites and +// its init runs at every start (plugin_init.c); `unsupported`, the note says why the build refused it. A plugin without +// a line waits for the rebuild like an ExeFS patch file; one whose start differed from the build is listed in +// build-cache/plugins.stale (mods_plugin_off) and waits for it too. A replaced exefs/main is another game code the port +// cannot load. A code module the port cannot run (ModInfo.code names them) keeps its mod off unless it is used without +// its code, which also leaves out the ExeFS patch files the port cannot apply. #include #include #include @@ -92,6 +97,8 @@ static void code_unrun(ModInfo *m, const char *name) { snprintf(m->code + k, sizeof m->code - k, "%s%s", k ? ", " : "", name); } +static void plugin_file(ModInfo *m, const char *leaf); + // What the ExeFS folder holds: MOD_EXEFS for patches (.ips, .pchtxt), MOD_SUBSDK for code modules (subsdk* plugins, a // replaced main). main.npdm alone is the process's metadata, not code: it counts as neither. static int exefs_cb(const char *name, int kind, uint64_t size, void *ctx) { @@ -100,7 +107,10 @@ static int exefs_cb(const char *name, int kind, uint64_t size, void *ctx) { if (kind != PLAT_FS_FILE) return 1; if (ends_with_ci(name, ".ips") || ends_with_ci(name, ".pchtxt")) { m->kind |= MOD_EXEFS; - } else if (!strncasecmp(name, "subsdk", 6) || !strcasecmp(name, "main")) { + } else if (!strncasecmp(name, "subsdk", 6)) { + m->kind |= MOD_SUBSDK; + plugin_file(m, name); + } else if (!strcasecmp(name, "main")) { m->kind |= MOD_SUBSDK; code_unrun(m, name); } @@ -202,6 +212,7 @@ static int same_build(const char *hex, size_t n) { // build-cache/ next to totk.exe, read once into *text; "" when there is none (mods_probe_cache sets the text). static char *status_text, *data_text; +static int no_stale_file; // --probe-mods: plugins.stale stays in memory static const char *build_cache(const char *name, char **text) { if (*text) return *text; char path[2100]; @@ -217,14 +228,17 @@ static const char *build_cache(const char *name, char **text) { return *text; } +static char *plugin_status_text, *plugin_stale_text; static const char *exefs_status(void) { return build_cache("exefs.status", &status_text); } static const char *exefs_data(void) { return build_cache("exefs.data", &data_text); } +static const char *plugin_status(void) { return build_cache("plugins.status", &plugin_status_text); } +static const char *plugin_stale(void) { return build_cache("plugins.stale", &plugin_stale_text); } -// The status line of key: its kind, built-in sets and note, NUL-terminated copies; 0 when none. -static int status_line(const char *key, const char *content, char *kind, char *over, char *note) { +// The line of key in a status text: its kind, built-in sets and note, NUL-terminated copies; 0 when none. +static int status_in(const char *text, const char *key, const char *content, char *kind, char *over, char *note) { char want[20]; snprintf(want, sizeof want, "%s\t%s\t", key, content); - for (const char *line = exefs_status(); *line;) { + for (const char *line = text; *line;) { const char *end = strchr(line, '\n'); size_t len = end ? (size_t)(end - line) : strlen(line); if (len > 18 && !memcmp(line, want, 18)) { @@ -246,6 +260,69 @@ static int status_line(const char *key, const char *content, char *kind, char *o return 0; } +static int status_line(const char *key, const char *content, char *kind, char *over, char *note) { + return status_in(exefs_status(), key, content, kind, over, note); +} + +// The reason plugins.stale gives for key and content, or NULL. +static const char *stale_reason(const char *key, const char *content, char *out, size_t cap) { + char want[20]; + snprintf(want, sizeof want, "%s\t%s\t", key, content); + for (const char *line = plugin_stale(); *line;) { + const char *end = strchr(line, '\n'); + size_t len = end ? (size_t)(end - line) : strlen(line); + if (len > 18 && !memcmp(line, want, 18)) { + size_t n = len - 18; + if (n && line[len - 1] == '\r') n--; + snprintf(out, cap, "%.*s", (int)(n < cap ? n : cap - 1), line + 18); + return out; + } + line = end ? end + 1 : line + len; + } + return NULL; +} + +// One exefs/subsdk* file of m: a built plugin joins its patch sets, a refused one is code it cannot run, one the build +// lacks (or whose start differed from the build) waits for the rebuild. +static void plugin_file(ModInfo *m, const char *leaf) { + char path[4300], rel[600]; + snprintf(path, sizeof path, "%s/%s", m->exefs, leaf); + size_t root = strlen(dir) + 1 + strlen(m->name) + 1; // m->exefs is // + snprintf(rel, sizeof rel, "%s/%s/%s", m->name, strlen(m->exefs) > root ? m->exefs + root : "exefs", leaf); + uint64_t n = 0; + char *data = (char *)plat_file_read(path, &n); + if (!data) { + rt_log("mods: %s: cannot read %s", m->name, path); + code_unrun(m, leaf); + return; + } + char key[9], content[9], kind[16], over[256], note[256], stale[256]; + snprintf(key, sizeof key, "%08X", fnv(rel, strlen(rel))); + snprintf(content, sizeof content, "%08X", fnv(data, (size_t)n)); + free(data); + if (!status_in(plugin_status(), key, content, kind, over, note) || stale_reason(key, content, stale, sizeof stale)) { + m->pending++; + m->pending_key = (m->pending_key ^ fnv(key, 8)) * 0x01000193u; + m->pending_key = (m->pending_key ^ fnv(content, 8)) * 0x01000193u; + return; + } + if (!strcmp(kind, "plugin") && m->nsets < (int)(sizeof m->sets / sizeof *m->sets) && + m->nplugins < (int)(sizeof m->plugin_keys / sizeof *m->plugin_keys)) { + uint32_t k = (uint32_t)strtoul(key, NULL, 16); + m->sets[m->nsets++] = k; + m->plugin_keys[m->nplugins] = k; + m->plugin_contents[m->nplugins++] = (uint32_t)strtoul(content, NULL, 16); + const char *hooks = strstr(note, ", "); + m->plugin_hooks += hooks ? atoi(hooks + 2) : 0; + size_t at = strlen(m->overrides); + if (*over) snprintf(m->overrides + at, sizeof m->overrides - at, "%s%s", at ? "," : "", over); + } else { + code_unrun(m, leaf); + if (!m->plugin_note[0]) snprintf(m->plugin_note, sizeof m->plugin_note, "%s", note); + } + if (!m->plugin_file[0]) snprintf(m->plugin_file, sizeof m->plugin_file, "%s", leaf); +} + typedef struct { ModInfo *m; const char *folder, *rel; // the folder listed and its path in the mod @@ -592,6 +669,14 @@ void mods_probe_cache(const char *status, const char *data) { data_text = dup_text(data); } +void mods_probe_plugins(const char *status, const char *stale) { + free(plugin_status_text); + free(plugin_stale_text); + plugin_status_text = dup_text(status); + plugin_stale_text = dup_text(stale); + no_stale_file = status != NULL; +} + void mods_rescan(void) { free(mods); mods = NULL; @@ -770,6 +855,53 @@ void mods_exefs_apply(uint64_t base, uint64_t size, uint64_t code_end, code_conflicts(); } +// build-cache/plugins.stale next to totk.exe: one more line (--probe-mods keeps it in memory). +static void stale_append(const char *line) { + size_t had = plugin_stale_text ? strlen(plugin_stale_text) : 0, n = strlen(line); + char *grown = realloc(plugin_stale_text, had + n + 1); + if (grown) { + if (!plugin_stale_text) grown[0] = 0; + memcpy(grown + had, line, n + 1); + plugin_stale_text = grown; + } + char path[2100]; + if (no_stale_file || !plat_exe_path(path, sizeof path)) return; + char *cut = strrchr(path, '/'), *back = strrchr(path, '\\'); + if (back > cut) cut = back; + if (!cut || snprintf(cut + 1, sizeof path - (size_t)(cut + 1 - path), "build-cache/plugins.stale") >= + (int)(sizeof path - (size_t)(cut + 1 - path))) + return; + FILE *f = fopen(path, "ab"); + if (!f) { + rt_log("mods: cannot write %s", path); + return; + } + fputs(line, f); + fclose(f); +} + +void mods_plugin_off(int set, const char *why) { + const ModInfo *owner = set_owner(set); + rt_patch_switch(set, 0); + if (!owner) return; + ModInfo *m = (ModInfo *)owner; + snprintf(m->plugin_off, sizeof m->plugin_off, "%s", why); + const char *name = rt_patch_name(set); + uint32_t key = name ? (uint32_t)strtoul(name + 4, NULL, 16) : 0; + for (int k = 0; k < m->nplugins; k++) { + if (m->plugin_keys[k] != key) continue; + char line[400]; + snprintf(line, sizeof line, "%08X\t%08X\t%s\n", key, m->plugin_contents[k], why); + stale_append(line); + } + rt_log("mods: %s: its plugin stays off this run (%s); it waits for the rebuild at the next start", m->name, why); + int kept = 0; // the code conflicts counted anew without the set + for (int i = 0; i < nconflicts; i++) + if (conflicts[i].part != MOD_PART_CODE) conflicts[kept++] = conflicts[i]; + nconflicts = kept; + code_conflicts(); +} + void mods_exefs_switch(void) { mods_scan(); const RtLoaded *main_mod = NULL; diff --git a/runtime/mods.h b/runtime/mods.h index 466fde80..8fb4f7d6 100644 --- a/runtime/mods.h +++ b/runtime/mods.h @@ -53,6 +53,13 @@ typedef struct { uint32_t data_bytes; // the bytes its usable ExeFS patch files write into main's data, as their // status lines count them ("data ") char overrides[256]; // the port's own patch sets its patches replace, comma separated + // Its exefs/subsdk* plugins (build-cache/plugins.status, tools/exefs_plugins.py): the built ones are patch sets in + // sets[] whose init runs at start (plugin_init.c); a refused one is a code module it cannot run (code). + int nplugins, plugin_hooks; // built plugins and their hooks + uint32_t plugin_keys[4], plugin_contents[4]; // their file and content hashes, for plugins.stale + char plugin_file[64]; // the first plugin's file name + char plugin_note[256]; // why the build refused a plugin ("" none) + char plugin_off[256]; // why its plugin stays off this run: its start differed from the build } ModInfo; // What two mods both change, the higher one winning: files (romfs.c), bytes of main's data and code sites @@ -105,5 +112,11 @@ void mods_rescan(void); // The ExeFS patch files of the enabled mods that the build lacks, their FNV-1a mixed into *key (cheats.c starts the // rebuild for them). int mods_exefs_pending(uint32_t *key); +// From plugin_init.c plugins_start: the plugin patch set `set` stays off this run (why), its mod's row says so, +// build-cache/plugins.stale marks its file for the rebuild at the next start, and the code conflicts are counted anew. +void mods_plugin_off(int set, const char *why); +// --probe-mods: the texts of build-cache/plugins.status and plugins.stale instead of the files; the stale lines +// mods_plugin_off adds stay in memory. +void mods_probe_plugins(const char *status, const char *stale); // The hex of the game main's build id (64 digits), "" when exefs/main cannot be read. const char *mods_main_build_id(void); diff --git a/runtime/mods_probe.c b/runtime/mods_probe.c index cc223bfc..473ea1e5 100644 --- a/runtime/mods_probe.c +++ b/runtime/mods_probe.c @@ -197,6 +197,97 @@ static void probe_code(const char *root) { remove_tree(root); } +// build-cache/plugins.status lines (tools/exefs_plugins.py) that refuse the probe's subsdk9 files of E and H: they stay +// code the port cannot run. +static const char *refused_plugins(void) { + static char text[256]; + snprintf(text, sizeof text, "%08X\t%08X\tunsupported\t-\tthe init called svc 0x40\n" + "%08X\t%08X\tunsupported\t-\tthe init called svc 0x40\n", fnv("E/exefs/subsdk9", 15), fnv("probe", 5), + fnv("H/exefs/subsdk9", 15), fnv("probe", 5)); + return text; +} + +// Mod plugins (build-cache/plugins.status, plugins.stale; mods.c plugin_file, mods_plugin_off) in a folder of their own: +// P RomFS + a built plugin (7 hooks) its set on, ranked by the order; a contested site with Q's patch file +// Q an ExeFS patch file on P's site +// R a plugin the build has no line for waits for the rebuild +// S RomFS + a refused plugin off, the reason kept; used without its code, the rest runs +// Then P's start differs from the build (mods_plugin_off): its set goes off, Q's code runs at the site, the conflict +// goes, a stale line marks P's file, and the next scan has P wait for the rebuild. +static void probe_plugins(const char *root) { + remove_tree(root); + plat_file_mkdir_p(root); + const char *bid = *mods_main_build_id() ? mods_main_build_id() : "0"; + char q[160]; + snprintf(q, sizeof q, "Q/exefs/%s.ips", bid); + static const char *const files[] = {"P/romfs/p.bin", "P/exefs/subsdk9", "R/exefs/subsdk9", "S/romfs/s.bin", + "S/exefs/subsdk9"}; + for (size_t i = 0; i < sizeof files / sizeof *files; i++) put(root, files[i]); + put(root, q); + static char names[3][16]; + static const char *const table[] = {"fps60", names[1], names[2]}; + set_name(names[1], sizeof names[1], "P/exefs/subsdk9"); + set_name(names[2], sizeof names[2], q); + char status[256], plugins[256]; + snprintf(status, sizeof status, "%08X\t%08X\tcode\t-\t\n", fnv(q, strlen(q)), fnv("probe", 5)); + snprintf(plugins, sizeof plugins, "%08X\t%08X\tplugin\tfps60\tplugin0, 7 hooks\n" + "%08X\t%08X\tunsupported\t-\tthe code its hooks reach has svc 0x40\n", fnv("P/exefs/subsdk9", 15), + fnv("probe", 5), fnv("S/exefs/subsdk9", 15), fnv("probe", 5)); + static const int16_t s0[] = {1, 2}; + static const RtPatchPick picks[] = {{"main", 0x100, s0, 2}}; + mods_probe_cache(status, ""); + mods_probe_plugins(plugins, ""); + cfg_set("TOTK_MODS_DIR", root); + cfg_set("TOTK_MODS_DISABLED", ""); + cfg_set("TOTK_MODS_NO_CODE", ""); + for (int round = 0; round < 2; round++) { + cfg_set("TOTK_MODS_ORDER", round ? "Q|P" : "P|Q"); + mods_rescan(); + mods_scan(); + rt_patch_probe_table(table, 3, picks, 1); + rt_patch_switch(0, 1); + mods_exefs_apply(0, 0, 0, NULL); + const ModInfo *pm = by_name("P"), *qm = by_name("Q"), *rm = by_name("R"), *sm = by_name("S"); + CHECK(pm && qm && rm && sm, "plugin scan: four mods"); + if (!pm || !qm || !rm || !sm) break; + CHECK(pm->problem == MOD_OK && pm->nsets == 1 && pm->nplugins == 1 && pm->plugin_hooks == 7 && !pm->code[0] && + EQ(pm->overrides, "fps60"), "a built plugin: its set, its hooks, the built-in set it replaces"); + CHECK(rt_patch_enabled(1) && !rt_patch_enabled(0), "the plugin's set on, the built-in set it replaces off"); + CHECK(rt_patch_pick_winner(0) == (round ? 1 : 0), round ? "Q above P: Q's patch runs at their site" + : "P above Q: P's plugin hook runs at their site"); + CHECK(mods_conflict_count() == 1 && mods_conflict_at(0)->part == MOD_PART_CODE && + mods_conflict_at(0)->winner == (round ? qm : pm)->id, + "the conflict table: a plugin site against a patch file goes by the order"); + CHECK(rm->problem == MOD_EXEFS_LATER && rm->pending == 1, "a plugin the build lacks waits for the rebuild"); + CHECK(sm->problem == MOD_PLUGIN && EQ(sm->code, "subsdk9") && strstr(sm->plugin_note, "svc 0x40"), + "a refused plugin keeps its mod off and says why"); + } + // P above Q, then P's start differs from the build + cfg_set("TOTK_MODS_ORDER", "P|Q"); + mods_rescan(); + mods_scan(); + rt_patch_probe_table(table, 3, picks, 1); + mods_exefs_apply(0, 0, 0, NULL); + mods_plugin_off(1, "probe: 1 of the words the build recorded were not written"); + const ModInfo *pm = by_name("P"); + CHECK(!rt_patch_enabled(1) && rt_patch_pick_winner(0) == 1, "a plugin off at start: the other mod's patch runs"); + CHECK(pm && strstr(pm->plugin_off, "were not written"), "its row says why"); + CHECK(mods_conflict_count() == 0, "its conflict goes with it"); + cfg_set("TOTK_MODS_NO_CODE", "S"); + mods_rescan(); + mods_scan(); + pm = by_name("P"); + const ModInfo *sm = by_name("S"); + CHECK(pm && pm->problem == MOD_EXEFS_LATER && pm->pending == 1, "the next start: its file waits for the rebuild"); + CHECK(sm && sm->problem == MOD_OK && sm->plugin_note[0], "a refused plugin used without its code: the rest runs"); + rt_patch_probe_table(NULL, 0, NULL, 0); + cfg_set("TOTK_MODS_NO_CODE", ""); + cfg_set("TOTK_MODS_ORDER", ""); + mods_probe_cache(NULL, NULL); + mods_probe_plugins(refused_plugins(), ""); + remove_tree(root); +} + // Mods with code the port cannot run (a code module, an ExeFS patch file it cannot apply): used without that code // by their own switch (mods_no_code) or by mods_without_code for every mod. static void probe_without_code(const char *root) { @@ -292,6 +383,7 @@ int probe_mods(void) { if (back > cut) cut = back; if (!cut) return 1; strcpy(cut + 1, "mods-probe"); + mods_probe_plugins(refused_plugins(), ""); // not the build's own plugins.status remove_tree(root); plat_file_mkdir_p(root); static const char *const files[] = { @@ -362,6 +454,8 @@ int probe_mods(void) { probe_data(root); probe_without_code(root); probe_code(root); + probe_plugins(root); + mods_probe_plugins(NULL, NULL); printf("mods: %s\n", fails ? "FAIL" : "ok"); int table = restbl_probe(); return fails || table ? 1 : 0; diff --git a/runtime/plugin_init.c b/runtime/plugin_init.c index 96486584..227f6621 100644 --- a/runtime/plugin_init.c +++ b/runtime/plugin_init.c @@ -1,8 +1,23 @@ -// 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. +// Mod plugins' inits (recomp/layout.py plugin, an exlaunch plugin such as a mod's exefs/subsdk9), run natively on +// this runtime's own kernel and sdk. The design follows #845 by itaskaev2, with the init run natively instead of under +// Unicorn, and run again at every start instead of an image taken after one run at build time. +// +// The service run: totk.exe --plugin-init DIR runs every plugin's DT_INIT, 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, which +// checks it and writes the plugin's patch set (its hook sites, recomp/patches.py `plugin` lines). +// +// At every start (plugins_start, from the module-init callback in boot.c): when the build has a plugin whose patch set +// its mod switched on, sdk's DT_INIT runs first, then the plugin's, in the same environment as in the service run, and +// then the game's constructors. That is the order of the Switch's rtld, which runs the modules' DT_INITs from the last +// loaded to the first (oss-rtld, librtld/source/ld.cpp __rtld_modules_init walks g_pAutoLoadList backwards; the list is +// filled in address order by __rtld_relocate_modules in librtld/source/relocation.cpp: main, subsdk*, sdk), so sdk +// runs before a subsdk plugin and the plugin before the game's main. The plugin keeps what its init made: heap blocks, +// its GOT, its alias addresses. What the init wrote into the game modules (its hook branches) and the plugin's .text +// after it (its stubs and trampolines) are compared with the record the build used (patches.h RT_PLUGIN_WRITES and +// RT_PLUGINS): equal, the plugin's set stays on and the generated hook sites call into it; different, the set goes off +// for this run, the reason is logged and the mod waits for a rebuild (mods.c mods_plugin_off). The branches written +// into main's .text are taken back afterwards: the lifted code is the code that runs, and main keeps the game's bytes. +// Without a plugin in the build none of this runs (RT_PLUGIN_COUNT 0). // // 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 @@ -31,10 +46,19 @@ #include #include "cfg.h" +#include "dispatch.h" #include "host.h" #include "kernel.h" +#include "mods.h" #include "platform/platform.h" +#if __has_include("patches.h") +#include "patches.h" +#endif +#ifndef RT_PLUGIN_COUNT +#define RT_PLUGIN_COUNT 0 +#endif + #define PAGE 0x1000ull #define GiB (1ull << 30) #define POOL_SIZE (64 * GiB) @@ -63,6 +87,8 @@ 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 int play; // plugins_start: a refused SVC fails the plugin instead of ending the process +static char failed[256]; // play: why the running init was refused 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; @@ -187,34 +213,44 @@ static void write_record(const char *refused); static void refuse(const char *why) { rt_log("plugin-init: refused: %s", why); + if (play) { // the init goes on with an error result; the plugin stays off (plugins_start) + if (!failed[0]) snprintf(failed, sizeof failed, "%s", why); + return; + } write_record(why); fflush(NULL); plat_exit(3); } -static void plugin_svc(Cpu *c, uint32_t imm, uint64_t pc) { - if (!running) return; +#define R_NOT_IMPLEMENTED 0xF601u + +static int plugin_svc(Cpu *c, uint32_t imm, uint64_t pc) { + if (!running) return 0; svc_count[imm & 0x7F]++; char why[256], at[128]; switch (imm) { - case 0x06: case 0x1E: case 0x27: case 0x74: case 0x75: return; + case 0x06: case 0x1E: case 0x27: case 0x74: case 0x75: return 0; case 0x29: { uint32_t id = (uint32_t)c->x[1]; if ((id >= 2 && id <= 5) || (id >= 12 && id <= 15)) { info_count[id]++; - return; + return 0; } if (id == 65001) { info_65001++; - return; + return 0; } snprintf(why, sizeof why, "GetInfo id %u at %s", id, ldr_describe(pc, at, sizeof at)); refuse(why); + c->x[0] = R_NOT_IMPLEMENTED; + return 1; } } 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); + c->x[0] = R_NOT_IMPLEMENTED; + return 1; } // Every JUMP_SLOT import of a plugin binds here (loader.c resolve): counted by name, then the guest definition runs, @@ -363,14 +399,25 @@ static void write_record(const char *refused) { } // ---- 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) { +static int layout_plugins(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)"); + return plugins; +} + +static void environment(void); + +// Before ldr_load: the import observer, the svc hooks, the pool and the stand-in for rtld. +void plugin_init_setup(void) { + if (!layout_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; + environment(); +} + +// The environment of a plugin's init, the same in the service run and at every start. +static void environment(void) { ldr_plugin_import = plugin_import; rt_process_memory = process_memory; rt_plugin_svc = plugin_svc; @@ -392,6 +439,13 @@ void plugin_init_setup(void) { (unsigned long long)rt_svc_tick_offset); } +// rt_boot, before ldr_load: the plugins' environment when the build has one (nothing otherwise). +void plugins_setup(void) { + if (!RT_PLUGIN_COUNT || !layout_plugins()) return; + play = 1; + environment(); +} + // 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. @@ -427,3 +481,202 @@ void plugin_init_run(Cpu *c, const RtLoaded *sdk) { fflush(NULL); plat_exit(0); } + +// ---- at every start +#if RT_PLUGIN_COUNT +static const struct { const char *module; int set; const char *sha256; } plugin_tab[] = { +#define X(i, mod, set, sha) {mod, set, sha}, + RT_PLUGINS(X) +#undef X +}; +static const struct { int plugin; const char *module; uint64_t off; uint32_t old, now; } write_tab[] = { +#define X(p, mod, off, old, now) {p, mod, off, old, now}, + RT_PLUGIN_WRITES(X) +#undef X +}; +#define NWRITES ((int)(sizeof write_tab / sizeof *write_tab)) +#ifdef RT_PLUGIN_ROOTS +static const struct { const char *module; uint64_t off; } root_tab[] = { +#define X(mod, off) {mod, off}, + RT_PLUGIN_ROOTS(X) +#undef X +}; +#define NROOTS ((int)(sizeof root_tab / sizeof *root_tab)) +#else +#define NROOTS 0 +#endif + +static const RtLoaded *loaded(const char *name) { + for (int i = 0; i < ldr_count(); i++) + if (!strcmp(ldr_module(i)->name, name)) return ldr_module(i); + return NULL; +} + +// TOTK_PLUGIN_TRACE = 1: the first call of each hook entry (patches.h RT_PLUGIN_ROOTS: callbacks, replace and call +// targets) is logged with its count so far; the entries run through count_entry, their dispatch slots rerouted once +// at start (dispatch.c rt_dispatch_reroute). The calls inside the plugin are direct and not counted. +static struct { uint64_t addr; GuestFn fn; uint64_t calls; } traced[64]; +static int ntraced; + +static void count_entry(Cpu *c) { + for (int i = 0; i < ntraced; i++) { + if (traced[i].addr != c->pc) continue; + if (__atomic_add_fetch(&traced[i].calls, 1, __ATOMIC_RELAXED) == 1) { + char at[128]; + rt_log("plugins: hook entry %s reached (first call)", ldr_describe(c->pc, at, sizeof at)); + } + __attribute__((musttail)) return traced[i].fn(c); + } + rt_fatal("plugins: traced entry %#llx without its function", (unsigned long long)c->pc); +} + +static void trace_entries(void) { +#ifdef RT_PLUGIN_ROOTS + for (int i = 0; i < NROOTS && ntraced < 64; i++) { + const RtLoaded *m = loaded(root_tab[i].module); + if (!m) continue; + uint64_t a = m->base + root_tab[i].off; + GuestFn f = rt_dispatch_lookup(a); + if (!f) continue; + traced[ntraced] = (typeof(traced[0])){a, f, 0}; + if (rt_dispatch_reroute(a, count_entry)) ntraced++; + else rt_log("plugins: trace: cannot reroute %#llx", (unsigned long long)a); + } +#endif + rt_log("plugins: trace: %d hook entries counted", ntraced); +} + +// Takes back the words the inits changed in the sealed part (.text, .rodata) of a game module: main keeps the game's +// own bytes, the lifted code being what runs. +static void restore_sealed(const RtLoaded *m, const uint8_t *was) { + for (uint64_t off = 0; off < m->data_off; off += PAGE) { + const uint8_t *now = (const uint8_t *)(uintptr_t)(m->base + off); + if (!memcmp(now, was + off, PAGE)) continue; + if (!plat_mem_protect(m->base + off, PAGE, PLAT_PROT_RW)) rt_fatal("plugins: cannot unseal %s+%#llx", m->name, + (unsigned long long)off); + memcpy((void *)(uintptr_t)(m->base + off), was + off, PAGE); + plat_mem_protect(m->base + off, PAGE, PLAT_PROT_READ); + } +} +#endif + +int plugins_start(Cpu *c, const RtLoaded *sdk) { +#if RT_PLUGIN_COUNT + if (!play) return 0; + int run[RT_PLUGIN_COUNT] = {0}, nrun = 0; + char why[RT_PLUGIN_COUNT][256]; + for (int i = 0; i < RT_PLUGIN_COUNT; i++) { + why[i][0] = 0; + const RtLoaded *m = loaded(plugin_tab[i].module); + if (!rt_patch_enabled(plugin_tab[i].set)) continue; + if (!m) { // loader.c left it out (its file is gone or changed): its set cannot run + mods_plugin_off(plugin_tab[i].set, "its file is not the one the build lifted"); + continue; + } + if (!m->init) { + mods_plugin_off(plugin_tab[i].set, "it has no DT_INIT"); + continue; + } + run[i] = 1, nrun++; + } + if (!nrun) return 0; + uint64_t t0 = plat_now_ms(); + rt_log("boot: ENTER %s DT_INIT first: a mod plugin's init runs before the game's constructors", sdk->name); + rt_call(c, sdk->init, 0); + pre = calloc((size_t)ldr_count(), sizeof *pre); + if (!pre) rt_fatal("plugins: out of memory"); + for (int i = 0; i < ldr_count(); i++) { // the game modules before the plugins run + const RtLoaded *m = ldr_module(i); + if (m->plugin) continue; + pre[i] = malloc(m->size); + if (!pre[i]) rt_fatal("plugins: out of memory for a copy of %s", m->name); + memcpy(pre[i], (const void *)(uintptr_t)m->base, m->size); + } + for (int i = RT_PLUGIN_COUNT - 1; i >= 0; i--) { + if (!run[i]) continue; + const RtLoaded *m = loaded(plugin_tab[i].module); + rt_log("plugins: ENTER %s DT_INIT at %#llx", m->name, (unsigned long long)m->init); + failed[0] = 0; + running = 1; + rt_trace |= RT_TRACE_PLUGIN; + rt_call(c, m->init, 0); + rt_trace &= ~(unsigned)RT_TRACE_PLUGIN; + running = 0; + if (failed[0]) snprintf(why[i], sizeof why[i], "its init asked for what the port does not serve: %s", failed); + } + for (int i = 0; i < naliases; i++) + if (aliases[i].live) flush(&aliases[i]); + // What the inits wrote into the game modules, word by word, against the record of the build. + uint8_t seen[NWRITES > 0 ? NWRITES : 1]; + memset(seen, 0, sizeof seen); + int unexpected = 0; + char first[160] = ""; + for (int i = 0; i < ldr_count(); i++) { + const RtLoaded *m = ldr_module(i); + if (!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; + int k = 0; + while (k < NWRITES && !(write_tab[k].off == w && !strcmp(write_tab[k].module, m->name) && + write_tab[k].old == a && write_tab[k].now == b)) + k++; + if (k < NWRITES) { + seen[k] = 1; + } else if (!unexpected++) { + snprintf(first, sizeof first, "%s+%#llx %08x -> %08x", m->name, (unsigned long long)w, a, b); + } + } + } + restore_sealed(m, pre[i]); + free(pre[i]); + } + free(pre); + pre = NULL; + for (int i = 0; i < RT_PLUGIN_COUNT; i++) { + if (!run[i] || why[i][0]) continue; + const RtLoaded *m = loaded(plugin_tab[i].module); + int missing = 0; + for (int k = 0; k < NWRITES; k++) missing += write_tab[k].plugin == i && !seen[k]; + char hex[65]; + sha256_hex((const void *)(uintptr_t)m->base, m->text_end, hex); + if (unexpected) + snprintf(why[i], sizeof why[i], "the inits wrote %d words the build did not record (first %s)", unexpected, + first); + else if (missing) + snprintf(why[i], sizeof why[i], "%d of the words the build recorded were not written", missing); + else if (strcmp(hex, plugin_tab[i].sha256)) + snprintf(why[i], sizeof why[i], "its code after the init is not the one the build lifted"); + } + mem_query_hook = NULL; // the game's own QueryMemory sees its address space as without plugins + rt_process_memory = NULL; // a callback that maps process memory stops with a named fatal (svc.c) + int on = 0; + for (int i = 0; i < RT_PLUGIN_COUNT; i++) { + if (!run[i]) continue; + if (why[i][0]) { + rt_log("plugins: %s stays off: %s", plugin_tab[i].module, why[i]); + mods_plugin_off(plugin_tab[i].set, why[i]); + continue; + } + int hooks = 0; + for (int k = 0; k < NWRITES; k++) hooks += write_tab[k].plugin == i; + rt_log("plugins: %s: init ran, %d hooks on (set %s)", plugin_tab[i].module, hooks, + rt_patch_name(plugin_tab[i].set)); + on++; + } + int live = 0; + for (int i = 0; i < naliases; i++) live += aliases[i].live; + rt_log("plugins: %d of %d plugins on in %llu ms (%d process-memory maps, %d kept)", on, nrun, + (unsigned long long)(plat_now_ms() - t0), naliases, live); + if (on && cfg_bool("TOTK_PLUGIN_TRACE")) trace_entries(); + return 1; +#else + (void)c, (void)sdk; + return 0; +#endif +} diff --git a/runtime/svc.c b/runtime/svc.c index 1f62f62d..91c92880 100644 --- a/runtime/svc.c +++ b/runtime/svc.c @@ -24,7 +24,7 @@ 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); +int (*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) { @@ -159,7 +159,7 @@ 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 | RT_TRACE_PLUGIN)) { - if ((rt_trace & RT_TRACE_PLUGIN) && rt_plugin_svc) rt_plugin_svc(c, imm, pc); + if ((rt_trace & RT_TRACE_PLUGIN) && rt_plugin_svc && rt_plugin_svc(c, imm, pc)) return; // refused, x0 set char at[160]; 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), diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 921386c4..c7c8e73f 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -1270,7 +1270,13 @@ static void tab_mods(float scale) { if (count) snprintf(cheat, sizeof cheat, menu_text(UI_MENU_MODS_CHEATS), count); else snprintf(cheat, sizeof cheat, menu_text(UI_MENU_MODS_CHEATS_FILE), cheats_file()); } - char code[512] = "", problem[512] = "", skipped[512] = "", left[512] = ""; + char code[512] = "", problem[768] = "", skipped[512] = "", left[512] = "", plugin[512] = ""; + if (m->plugin_off[0]) // its plugin's start differed from the build (plugin_init.c) + snprintf(plugin, sizeof plugin, menu_text(UI_MENU_MODS_PLUGIN_OFF), m->plugin_off); + else if (m->plugin_hooks && m->problem == MOD_OK) + snprintf(plugin, sizeof plugin, menu_text(UI_MENU_MODS_PLUGIN_ON), m->plugin_hooks); + else if (m->plugin_note[0] && m->problem != MOD_PLUGIN) // used without its code: the refusal stays visible + snprintf(plugin, sizeof plugin, menu_text(UI_MENU_MODS_PLUGIN_REFUSED), m->plugin_file, m->plugin_note); if (m->code[0] && m->problem != MOD_PLUGIN) // used without its code snprintf(skipped, sizeof skipped, menu_text(UI_MENU_MODS_CODE_SKIPPED), m->code); if (m->unsupported && m->problem != MOD_EXEFS_UNSUPPORTED) @@ -1281,7 +1287,16 @@ static void tab_mods(float scale) { snprintf(code, sizeof code, menu_text(UI_MENU_MODS_EXEFS_ON), m->nsets + m->ndata); switch (m->problem) { case MOD_EMPTY: snprintf(problem, sizeof problem, "%s", menu_text(UI_MENU_MODS_EMPTY)); break; - case MOD_PLUGIN: snprintf(problem, sizeof problem, "%s", menu_text(UI_MENU_MODS_SUBSDK)); break; + case MOD_PLUGIN: + if (m->plugin_note[0]) { // the build refused its plugin, and says why + int n = snprintf(problem, sizeof problem, menu_text(UI_MENU_MODS_PLUGIN_REFUSED), m->plugin_file, + m->plugin_note); + if (n >= 0 && n < (int)sizeof problem) + snprintf(problem + n, sizeof problem - n, " %s", menu_text(UI_MENU_MODS_PLUGIN_STAYS_OFF)); + } else { + snprintf(problem, sizeof problem, "%s", menu_text(UI_MENU_MODS_SUBSDK)); + } + break; case MOD_EXEFS_LATER: snprintf(problem, sizeof problem, "%s", menu_text(UI_MENU_MODS_EXEFS_LATER)); break; case MOD_EXEFS_UNSUPPORTED: snprintf(problem, sizeof problem, menu_text(UI_MENU_MODS_EXEFS_UNSUPPORTED), m->note); @@ -1291,7 +1306,8 @@ static void tab_mods(float scale) { default: break; } int k = snprintf(desc, sizeof desc, menu_text(UI_MENU_MODS_ROW_DESC), m->replaced, m->added); - const char *parts[] = {over, other, cheat, code, skipped, left, problem, menu_text(UI_MENU_MODS_MOVE_HINT)}; + const char *parts[] = {over, other, cheat, code, plugin, skipped, left, problem, + menu_text(UI_MENU_MODS_MOVE_HINT)}; for (const char *part : parts) if (*part && k >= 0 && k < (int)sizeof desc) k += snprintf(desc + k, sizeof desc - k, "\n\n%s", part); const char *note = m->enabled && m->problem != MOD_OK ? menu_text(UI_MENU_MODS_UNUSED) : after; diff --git a/runtime/ui_text.c b/runtime/ui_text.c index 17f33965..1c9a5461 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -586,6 +586,12 @@ static const char *const text_en[UI_TEXT_COUNT] = { "stays off while this mod is on.", [UI_MENU_MODS_CODE_SKIPPED] = "Its own game code (%s) does not load: the rest of the mod is used. Switch the mod " "off if the game misbehaves with it.", + [UI_MENU_MODS_PLUGIN_ON] = "Its plugin runs: %d hooks into the game code.", + [UI_MENU_MODS_PLUGIN_REFUSED] = "Its plugin %s cannot run in the port: %s.", + [UI_MENU_MODS_PLUGIN_STAYS_OFF] = "The whole mod stays off. Its switch \"Use ... without its code\" below uses the " + "rest of it.", + [UI_MENU_MODS_PLUGIN_OFF] = "Its plugin stays off this time: it started differently from the build (%s). The game " + "rebuilds it at the next start.", [UI_MENU_MODS_WITHOUT_CODE] = "Use %s without its code", [UI_MENU_MODS_WITHOUT_CODE_DESC] = "The mod above brings code the port cannot run: its own game code (a subsdk " "plugin or a replaced main) or code patches (exefs) the port cannot apply. Off: " @@ -1161,6 +1167,12 @@ static const char *const text_ru[UI_TEXT_COUNT] = { "который выключен, пока этот мод включён.", [UI_MENU_MODS_CODE_SKIPPED] = "Его собственный код игры (%s) не загружается: используется остальная часть мода. " "Выключите мод, если игра с ним ведёт себя странно.", + [UI_MENU_MODS_PLUGIN_ON] = "Его плагин работает: перехватов в коде игры: %d.", + [UI_MENU_MODS_PLUGIN_REFUSED] = "Его плагин %s не может работать в порте: %s.", + [UI_MENU_MODS_PLUGIN_STAYS_OFF] = "Мод выключен целиком. Его переключатель «... без его кода» ниже включает " + "остальную часть мода.", + [UI_MENU_MODS_PLUGIN_OFF] = "Его плагин в этот раз выключен: он запустился не так, как при сборке (%s). Игра " + "пересоберёт его при следующем запуске.", [UI_MENU_MODS_WITHOUT_CODE] = "%s без его кода", [UI_MENU_MODS_WITHOUT_CODE_DESC] = "Мод выше приносит код, который порт не может запустить: свой код игры (плагин " "subsdk или заменённый main) или патчи кода (exefs), которые порт не может " @@ -1770,6 +1782,12 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = { "%s, que fica desligado enquanto este mod está ligado.", [UI_MENU_MODS_CODE_SKIPPED] = "O código próprio do jogo dele (%s) não é carregado: o resto do mod é usado. " "Desligue o mod se o jogo se comportar mal com ele.", + [UI_MENU_MODS_PLUGIN_ON] = "O plugin dele está ativo: %d ganchos no código do jogo.", + [UI_MENU_MODS_PLUGIN_REFUSED] = "O plugin %s dele não pode rodar no port: %s.", + [UI_MENU_MODS_PLUGIN_STAYS_OFF] = "O mod inteiro fica desligado. A chave \"Usar ... sem o código dele\", abaixo, " + "usa o resto dele.", + [UI_MENU_MODS_PLUGIN_OFF] = "O plugin dele fica desligado desta vez: ele iniciou de forma diferente da compilação " + "(%s). O jogo o recompila na próxima inicialização.", [UI_MENU_MODS_WITHOUT_CODE] = "Usar %s sem o código dele", [UI_MENU_MODS_WITHOUT_CODE_DESC] = "O mod acima traz código que o port não consegue rodar: código próprio do jogo " "(um plugin subsdk ou um main substituído) ou patches de código (exefs) que o " @@ -2458,6 +2476,10 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = { [UI_MENU_MODS_EXEFS_ON] = "正在使用的代码补丁:%d 个文件。", [UI_MENU_MODS_REPLACES_BUILTIN] = "正在使用的代码补丁:%d 个文件。它们取代本移植自带的补丁 %s,此模组开启时该补丁保持关闭。", [UI_MENU_MODS_CODE_SKIPPED] = "它自带的游戏代码(%s)不会加载:模组的其余部分照常使用。如果游戏因此表现异常,请关闭此模组。", + [UI_MENU_MODS_PLUGIN_ON] = "它的插件正在运行:游戏代码中有 %d 处挂钩。", + [UI_MENU_MODS_PLUGIN_REFUSED] = "它的插件 %s 无法在移植版中运行:%s。", + [UI_MENU_MODS_PLUGIN_STAYS_OFF] = "整个模组保持关闭。打开下方它的“不带代码使用 ...”开关即可使用其余部分。", + [UI_MENU_MODS_PLUGIN_OFF] = "它的插件本次保持关闭:启动情况与构建时不同(%s)。游戏会在下次启动时重新构建它。", [UI_MENU_MODS_WITHOUT_CODE] = "不带代码使用 %s", [UI_MENU_MODS_WITHOUT_CODE_DESC] = "上方的模组带有移植版无法运行的代码:它自己的游戏代码(subsdk 插件或替换的 main),或移植版无法应用的代码补丁(exefs)。关闭:整个模组保持关闭。开启:跳过这些代码,使用模组的其余部分:其 RomFS 文件、金手指和可用的代码补丁。它的文件可能依赖缺失的代码;如果游戏表现异常,请再次关闭。下次启动时生效。", [UI_MENU_MODS_WITHOUT_CODE_ALL] = "已对所有模组开启:totk.ini 中 mods_without_code = 1。", diff --git a/runtime/ui_text.h b/runtime/ui_text.h index 2423b4dd..96f7114f 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -92,6 +92,10 @@ typedef enum { UI_MENU_MODS_EXEFS_ON, // printf: its ExeFS patch files in use UI_MENU_MODS_REPLACES_BUILTIN, // printf: its ExeFS patch files in use, the port's own patch sets they turn off UI_MENU_MODS_CODE_SKIPPED, // printf: its code modules in exefs, which do not load (used without its code) + UI_MENU_MODS_PLUGIN_ON, // printf: the hooks of its plugin (runtime/plugin_init.c) that run + UI_MENU_MODS_PLUGIN_REFUSED, // printf: its plugin file, why the build refused it (English, from the tool) + UI_MENU_MODS_PLUGIN_STAYS_OFF, // after UI_MENU_MODS_PLUGIN_REFUSED when the whole mod stays off + UI_MENU_MODS_PLUGIN_OFF, // printf: why its plugin's start differed from the build (English) UI_MENU_MODS_WITHOUT_CODE, // printf: the mod's name; its switch "use without its code" (TOTK_MODS_NO_CODE) UI_MENU_MODS_WITHOUT_CODE_DESC, UI_MENU_MODS_WITHOUT_CODE_ALL, // why that switch cannot be changed: TOTK_MODS_WITHOUT_CODE=1