Merge dev into task/pr1263

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-11-17 19:59:52 +03:00
commit e031ac596a
50 files changed
+3918 -315

No files matched your search

+39 -5
View File
@@ -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) {
@@ -77,15 +79,28 @@ 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
// 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);
@@ -118,8 +133,9 @@ 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 &&
!(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);
@@ -341,11 +357,29 @@ 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("<rtld loader_ready>", cb_loader_ready);
cb_addr[1] = hostfn_register("<rtld module_inits>", cb_module_inits);
cb_addr[2] = hostfn_register("<rtld module_finis>", 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
// 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
+6 -5
View File
@@ -32,7 +32,7 @@ static int save_ok = 1; // the last cfg_save_to wrote (o
// logged as unknown. A new variable goes here too, or its ini key logs a harmless "unknown key" line, and gets a
// description in totk.ini.example or docs/settings.md (tests/settings_doc_check.py checks both).
static const char *const known[] = {
"AB", "ABILITY_GUARD", "ADDR_REPORT", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_OFF", "AUDIO_PREFILL_MS",
"AB", "ABILITY_GUARD", "ADDR_REPORT", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_DRAIN_FIX", "AUDIO_OFF", "AUDIO_PREFILL_MS",
"BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_LSTICK_DOWN", "BIND_LSTICK_LEFT",
"BIND_LSTICK_RIGHT", "BIND_LSTICK_UP", "BIND_MINUS",
"BIND_PLUS", "BIND_R", "BIND_RCLICK", "BIND_RIGHT", "BIND_RSTICK_DOWN", "BIND_RSTICK_LEFT", "BIND_RSTICK_RIGHT", "BIND_RSTICK_UP",
@@ -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", "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",
@@ -75,7 +75,7 @@ static const char *const known[] = {
static const struct { const char *key, *section; } sections[] = {
{"FPS_LIMIT", "frame rate"}, {"FPS60", "frame rate"}, {"FRAME_QUEUE", "frame rate"},
{"REALTIME", "frame rate"}, {"CLOTH_REALTIME", "frame rate"}, {"HIGHFPS_FIX", "frame rate"},
{"CLIMB_FIX", "frame rate"},
{"CLIMB_FIX", "frame rate"}, {"AUDIO_DRAIN_FIX", "frame rate"},
{"FULLSCREEN", "display"}, {"WINDOW_SIZE", "display"}, {"PERF_OVERLAY", "display"}, {"VK_PRESENT", "display"},
{"GPU_INDEX", "display"}, {"OUTPUT_RES", "display"}, {"RENDER_SCALE", "display"}, {"UPSCALER", "display"},
{"SHARPNESS", "display"}, {"DYNAMIC_RES", "display"}, {"ASPECT", "display"}, {"MOVIE_ASPECT", "display"}, {"FOV", "display"}, {"ANISO", "display"},
@@ -115,7 +115,7 @@ static const struct { const char *key, *section; } sections[] = {
{"OPTREF_GUARD", "performance"}, {"ABILITY_GUARD", "performance"}, {"REPORT_GUARD", "performance"},
{"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"PASS_CRUMBS", "diagnostics"}, {"ADDR_REPORT", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"},
{"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"},
{"MV_SURVEY", "diagnostics"},
{"MV_SURVEY", "diagnostics"}, {"PLUGIN_TRACE", "diagnostics"},
{"GAMEPLAY_TWEAKS_ENABLED", "gameplay tweaks"}, {"GAMEPLAY_ULTRAHAND_ROTATE", "gameplay tweaks"},
{"GAMEPLAY_ULTRAHAND_C_AXIS", "gameplay tweaks"}, {"GAMEPLAY_ULTRAHAND_DISTANCE", "gameplay tweaks"},
{"GAMEPLAY_ITEM_WHEEL", "gameplay tweaks"}, {"GAMEPLAY_ITEM_AD", "gameplay tweaks"},
@@ -161,7 +161,7 @@ static const CfgMeta metas[] = {
{"REALTIME", "1", "0|1", RESTART}, {"CLOTH_REALTIME", "1", "0|1", RESTART},
{"MODS_REBUILD", "auto", "auto|off", RESTART},
{"MODS_WITHOUT_CODE", "0", "0|1", RESTART}, // 1: a mod is used without the code module it cannot run (mods.c)
{"HIGHFPS_FIX", "1", "0|1", RESTART}, {"CLIMB_FIX", "1", "0|1", RESTART},
{"HIGHFPS_FIX", "1", "0|1", RESTART}, {"CLIMB_FIX", "1", "0|1", RESTART}, {"AUDIO_DRAIN_FIX", "1", "0|1", RESTART},
{"LANG", "", "ja|en-US|fr|de|it|es|zh-CN|ko|nl|pt|ru|zh-TW|en-GB|fr-CA|es-419|zh-Hans|zh-Hant|pt-BR", RESTART},
{"OPERATION_MODE", "docked", "docked|handheld", RESTART},
{"ASSUME_FOCUS", "1", "0|1", LIVE}, // read on every focus change (ov_system.c)
@@ -234,6 +234,7 @@ static const CfgMeta metas[] = {
{"WB_FENCE_LIMIT", "65536", "0..1073741824", RESTART}, {"WB_MERGE", "1", "0|1", RESTART}, {"WB_EXACT", "1", "0|1", RESTART},
{"PASS_CRUMBS", "", "0|1", RESTART}, // vk_crumbs.c pass marks; unset: VK_AMD_buffer_marker only
{"ADDR_REPORT", "0", "0|1", RESTART}, // vk_device.c GPU address map for device faults
{"PLUGIN_TRACE", "0", "0|1", RESTART}, // plugin_init.c: the first call of each mod plugin hook entry
{"INPUT_RECORD", "", NULL, RESTART},
};
+16 -7
View File
@@ -516,9 +516,13 @@ static void plain_name(char *s) {
// ---- rebuild: code cheats and ExeFS patches the build lacks
// The setup writes build-cache\rebuild.txt (installer/steps.py write_rebuild_command), the command that starts its
// window in rebuild mode on this install folder, one argument per line. When an enabled mod has code cheats this build
// lacks (CHEAT_REBUILD) or ExeFS patch files it lacks (mods_exefs_pending), the game runs it and exits; the setup lifts
// and compiles them in and starts the game again.
// window in rebuild mode on this install folder, one argument per line. The build kit's command (installer/layout.py)
// is relative to the install folder: its program is taken from this exe's folder, and the command runs with that
// folder as its working folder, against which the setup resolves its other arguments. When an enabled mod has code
// cheats this build
// lacks (CHEAT_REBUILD) or ExeFS patch files it lacks (mods_exefs_pending), or the build has a mod plugin whose file is
// gone (ldr_plugins_gone: its mod was removed), the game runs it and exits; the setup lifts and compiles them in (or the
// plugin out) and starts the game again.
// build-cache\rebuild.skip holds the key of the cheats a rebuild was started for: a rebuild that failed or was
// cancelled leaves them unbuilt, and the same set does not start it again. TOTK_MODS_REBUILD=off never starts it.
// Scripted runs (--seconds, --boot) never start it.
@@ -532,7 +536,7 @@ static void rebuild_if_needed(const char *dir) {
key = (key ^ fnv(c->info.key, strlen(c->info.key))) * 0x01000193u;
key = (key ^ c->content) * 0x01000193u;
}
int patches = mods_exefs_pending(&key);
int patches = mods_exefs_pending(&key) + ldr_plugins_gone(&key);
if (!need && !patches) return;
char cmd_path[PLAT_PATH_MAX + 64], skip_path[PLAT_PATH_MAX + 64], skip[16] = "", want[16];
snprintf(cmd_path, sizeof cmd_path, "%sbuild-cache" SEP "rebuild.txt", dir);
@@ -548,12 +552,13 @@ static void rebuild_if_needed(const char *dir) {
char *cmd = why ? NULL : read_text(cmd_path);
if (!why && !cmd) why = "no build-cache/rebuild.txt (a build without the setup)";
if (why) {
rt_log("cheats: %d code cheats and %d ExeFS patch files are not built into the game; no rebuild: %s", need,
rt_log("cheats: %d code cheats and %d ExeFS patch files or plugins differ from the build; no rebuild: %s", need,
patches, why);
free(cmd);
return;
}
const char *argv[32];
char program[PLAT_PATH_MAX + 64];
int argc = 0;
for (char *line = cmd; line && *line && argc < 31;) {
char *end = strchr(line, '\n'), *next = end ? end + 1 : NULL;
@@ -564,13 +569,17 @@ static void rebuild_if_needed(const char *dir) {
line = next;
}
argv[argc] = NULL;
// CreateProcess resolves a relative program against this process's working folder, which a shortcut may set
if (argc && !plat_path_absolute(argv[0]) &&
snprintf(program, sizeof program, "%s%s", dir, argv[0]) < (int)sizeof program)
argv[0] = program;
if (argc < 2 || !plat_file_write(skip_path, want, strlen(want))) {
rt_log("cheats: %d code cheats and %d ExeFS patch files are not built into the game; cannot start the "
rt_log("cheats: %d code cheats and %d ExeFS patch files or plugins differ from the build; cannot start the "
"rebuild (%s)", need, patches, cmd_path);
free(cmd);
return;
}
rt_log("cheats: %d code cheats and %d ExeFS patch files are not built into the game; starting the rebuild: %s",
rt_log("cheats: %d code cheats and %d ExeFS patch files or plugins differ from the build; starting the rebuild: %s",
need, patches, argv[0]);
int ok = plat_spawn(argv, dir);
free(cmd);
+15
View File
@@ -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};
}
+5 -2
View File
@@ -19,8 +19,10 @@ typedef struct RtModule {
} RtModule;
// The module glue that tools/build_game.py writes for each build (build/<version>/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<N>, 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);
@@ -36,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);
+30 -2
View File
@@ -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=<path> 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,24 @@ 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<N>), 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
// The module tables of the runtime library: it is built once per release (tools/build_game.py --make-runtime-lib),
// before an install's layout (recomp/layout.py) adds its mod plugins after sdk, so they do not use that MOD_COUNT.
#define RT_MAX_MODULES 32
int ldr_count(void);
int ldr_plugins_gone(uint32_t *key); // layout plugins whose file is gone; mixes their paths into key
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 <name>", 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 +372,27 @@ 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
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.
+7
View File
@@ -38,3 +38,10 @@ 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 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
+95 -16
View File
@@ -47,11 +47,12 @@ typedef struct {
uint64_t *plt_slot, *plt_stub, nplt; // PLT stubs sorted by GOT slot (ldr_plt_stub), built on first use
// statistics
uint64_t kinds[K_COUNT], sym_import, sym_own;
uint32_t imports, import_to[MOD_COUNT], weak_unresolved, strong_unresolved;
uint32_t imports, import_to[RT_MAX_MODULES], weak_unresolved, strong_unresolved;
uint32_t init_off;
} Mod;
static Mod mods[MOD_COUNT];
static Mod mods[RT_MAX_MODULES];
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,51 @@ 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;
}
// Mod plugins of the layout whose file is gone (their mod was removed): a rebuild without them is due (cheats.c
// rebuild_if_needed), keyed by their paths.
static int plugins_gone;
static uint32_t plugins_gone_key = 0x811C9DC5u;
int ldr_plugins_gone(uint32_t *key) {
if (plugins_gone) *key = (*key ^ plugins_gone_key) * 0x01000193u;
return plugins_gone;
}
// 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);
if (!plat_file_exists(lay->file)) {
plugins_gone++;
for (const char *c = lay->file; *c; c++) plugins_gone_key = (plugins_gone_key ^ (uint8_t)*c) * 0x01000193u;
}
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++)
@@ -81,14 +127,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 +180,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 +338,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 +359,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 +397,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))) {
@@ -444,9 +517,15 @@ static void seal(Mod *m) {
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;
if (rt_layout_count > RT_MAX_MODULES) rt_fatal("loader: %d modules in the layout", 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++) {
@@ -557,7 +636,7 @@ uint64_t ldr_plt_stub(const char *module, const char *name) {
typedef struct {
const char *name;
uint64_t kinds[K_COUNT], total, sym_import, sym_own;
uint32_t imports, to[MOD_COUNT], weak, init_off, init_array;
uint32_t imports, to[RT_MAX_MODULES], weak, init_off, init_array;
} Expect;
static const Expect expect_121[] = {
{"main", {508816, 1415, 32, 641}, 510904, 2088, 0, 675, {0, 16, 647}, 12, 0x30, 10673},
+6 -1
View File
@@ -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]);
}
+6 -2
View File
@@ -57,7 +57,7 @@ static uint8_t *alias_pages; // 1 per mapped 4 KiB page of the alias regi
typedef struct { uint64_t dst, src, size; } StackMap;
static StackMap stack_maps[MAX_STACK_MAPS];
static unsigned nstack_maps;
static uint8_t *module_perm[MOD_COUNT]; // per data page of each module: perm | ATTR_PERMISSION_LOCKED
static uint8_t *module_perm[RT_MAX_MODULES]; // per data page of each module: perm | ATTR_PERMISSION_LOCKED
static uint64_t align_up(uint64_t v, uint64_t a) { return (v + a - 1) & ~(a - 1); }
@@ -530,7 +530,7 @@ uint32_t mem_unmap_physical(uint64_t a, uint64_t n) { return alias_map(1, a, n);
// as taken (Code, RX), so the SDK never picks it.
static void taken_gap(uint64_t a, uint64_t *base, uint64_t *size) {
uint64_t lo = 0, hi = RT_ADDRESS_LIMIT;
RtRange known[3 + MOD_COUNT + 2];
RtRange known[3 + RT_MAX_MODULES + 2];
int n = 0;
known[n++] = (RtRange){L.aslr.base, low_size};
known[n++] = L.alias;
@@ -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;
}
+142 -9
View File
@@ -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_<hash> 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 <ctype.h>
#include <stdio.h>
#include <stdlib.h>
@@ -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/<name> 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 <dir>/<mod>/<exefs place>
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
@@ -369,11 +446,12 @@ static int scan_cb(const char *name, int kind, uint64_t size, void *ctx) {
int unsupported = exefs_check(m, path);
// A mod whose code part the port cannot use is off as a whole: its RomFS files may expect that code. Used without
// its code (its switch, or mods_without_code = 1 for every mod), the code modules (m->code) and the ExeFS patch
// files the port cannot apply are left out and the rest of the mod is used, when it has a rest.
// files the port cannot apply are left out and the rest of the mod is used, when it has a rest. Each plugin of a
// mod is built or refused on its own (tools/exefs_plugins.py), so a built plugin is such a rest too.
m->no_code = listed(cfg_get("TOTK_MODS_NO_CODE"), m->name);
m->without_code = m->no_code || cfg_bool("TOTK_MODS_WITHOUT_CODE");
int usable = m->nsets || m->ndata || m->kind & (MOD_ROMFS | MOD_CHEATS);
int code_off = m->code[0] && (!m->without_code || !(m->kind & (MOD_ROMFS | MOD_EXEFS | MOD_CHEATS)));
int code_off = m->code[0] && (!m->without_code || !(m->kind & (MOD_ROMFS | MOD_EXEFS | MOD_CHEATS) || m->nplugins));
m->problem = strchr(m->name, '|') ? MOD_BAD_NAME
: code_off ? MOD_PLUGIN
: unsupported && (!m->without_code || !usable) ? MOD_EXEFS_UNSUPPORTED
@@ -592,6 +670,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 +856,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;
+13
View File
@@ -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 <bytes>")
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);
+108
View File
@@ -197,6 +197,111 @@ 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
// T a built plugin + a refused one each judged alone: off for the refused one, its reason kept; used
// without its code, the built one's set 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", "T/exefs/subsdk8", "T/exefs/subsdk9"};
for (size_t i = 0; i < sizeof files / sizeof *files; i++) put(root, files[i]);
put(root, q);
static char names[4][16];
static const char *const table[] = {"fps60", names[1], names[2], names[3]};
set_name(names[1], sizeof names[1], "P/exefs/subsdk9");
set_name(names[2], sizeof names[2], q);
set_name(names[3], sizeof names[3], "T/exefs/subsdk9");
char status[256], plugins[512];
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"
"%08X\t%08X\tplugin\t-\tplugin1, 2 hooks\n"
"%08X\t%08X\tunsupported\t-\tthe run was refused: svc 0x1 at plugin2+0x50a0\n", fnv("P/exefs/subsdk9", 15),
fnv("probe", 5), fnv("S/exefs/subsdk9", 15), fnv("probe", 5), fnv("T/exefs/subsdk9", 15), fnv("probe", 5),
fnv("T/exefs/subsdk8", 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, 4, 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"),
*tm = by_name("T");
CHECK(pm && qm && rm && sm && tm, "plugin scan: five mods");
if (!pm || !qm || !rm || !sm || !tm) 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");
CHECK(tm->problem == MOD_PLUGIN && tm->nplugins == 1 && tm->plugin_hooks == 2 && EQ(tm->code, "subsdk8") &&
strstr(tm->plugin_note, "svc 0x1") && !rt_patch_enabled(3),
"a built plugin beside a refused one: the mod off, the refused one's reason on its row");
}
// 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, 4, 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|T");
mods_rescan();
mods_scan();
rt_patch_probe_table(table, 4, picks, 1);
mods_exefs_apply(0, 0, 0, NULL);
pm = by_name("P");
const ModInfo *sm = by_name("S"), *tm = by_name("T");
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");
CHECK(tm && tm->problem == MOD_OK && tm->plugin_note[0] && rt_patch_enabled(3),
"a built plugin beside a refused one, used without its code: the built one's set 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 +397,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 +468,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;
+11
View File
@@ -379,6 +379,17 @@ static inline char *plat_last_sep(const char *path) {
if (*p == '/' || *p == '\\') last = (char *)p;
return last;
}
// 1 when path does not depend on the working directory: it starts with a separator (a root or UNC path) or, on
// Windows, with a drive letter and a separator.
static inline int plat_path_absolute(const char *path) {
if (path[0] == '/' || path[0] == '\\') return 1;
#ifdef _WIN32
char c = path[0];
return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) && path[1] == ':' && (path[2] == '/' || path[2] == '\\');
#else
return 0;
#endif
}
// Environment variable as UTF-8 into out; its length, or 0 when unset, empty or too long for cap.
size_t plat_getenv(const char *name, char *out, size_t cap);
// The user's UI language as a BCP-47 tag ("ru-RU", "en-US"); "" when the system gives none. Valid for the process.
+703
View File
@@ -0,0 +1,703 @@
// Mod plugins' inits (recomp/layout.py plugin<N>, 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 the plugins' DT_INITs, records what the inits did and exits before
// any game module's constructor, nnMain, graphics or audio. TOTK_PLUGIN_ONLY names the one plugin module whose init
// runs ("-": none); the others stay loaded, as they are at play. tools/exefs_plugins.py runs each plugin on its own,
// so a plugin that is refused, crashes or hangs costs only itself; it checks each record 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). Each plugin is compared
// right after its own init, and the branches it wrote into main's .text are taken back before the next init: the next
// plugin sees the game's own bytes, as in its own service run (two plugins hooking one site each relocate the game's
// instruction; the generated site runs the one the Mods tab order picks). 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
// 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, which plugins' inits ran, the writes into every game module as words, the
// alias calls, svc and import counts, the tick offset, a refusal), plugin<N>.img (the plugin's image after its init,
// all segments) and plugin<N>.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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#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)
#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 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;
static uint8_t **pre; // per loaded module: its image before the plugins' inits
static uint8_t *ran; // service run, per loaded module: its init ran
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);
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);
}
#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 0;
case 0x29: {
uint32_t id = (uint32_t)c->x[1];
if ((id >= 2 && id <= 5) || (id >= 12 && id <= 15)) {
info_count[id]++;
return 0;
}
if (id == 65001) {
info_65001++;
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,
// 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) fprintf(f, ", \"ran\": %s", ran && ran[i] ? "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
static int layout_plugins(void) {
int plugins = 0;
for (int i = 0; i < rt_layout_count; i++) plugins += rt_layout[i].file != NULL;
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;
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);
}
// 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.
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);
}
const char *only = cfg_get("TOTK_PLUGIN_ONLY");
ran = calloc((size_t)ldr_count(), 1);
if (!ran) rt_fatal("plugin-init: out of memory");
for (int i = ldr_count() - 1; i >= 0; i--) {
const RtLoaded *m = ldr_module(i);
if (!m->plugin) continue;
if (only && *only && strcmp(only, m->name)) {
rt_log("plugin-init: %s stays loaded, its init does not run (TOTK_PLUGIN_ONLY=%s)", m->name, only);
continue;
}
ran[i] = 1;
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);
}
// ---- 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 a page an init 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_page(const RtLoaded *m, uint64_t off, const uint8_t *was) {
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, PAGE);
plat_mem_protect(m->base + off, PAGE, PLAT_PROT_READ);
}
// After plugin i's init: what it wrote into the game modules, word by word, against the record of the build (its
// entries of RT_PLUGIN_WRITES). The first difference goes into why (unless a reason is there already). Then the game
// modules are made what they were before the init: the sealed part (.text, .rodata) gets the game's bytes back, a
// write elsewhere stays and goes into pre, so the next plugin's init is compared only with what it writes itself.
// The aliases this init mapped (from first_alias on) are flushed first; an earlier init's were flushed after it.
static void check_writes(int plugin, int first_alias, char *why, size_t cap) {
for (int i = first_alias; i < naliases; i++)
if (aliases[i].live) flush(&aliases[i]);
uint8_t seen[NWRITES > 0 ? NWRITES : 1];
memset(seen, 0, sizeof seen);
int unexpected = 0, missing = 0;
char first[160] = "";
for (int i = 0; i < ldr_count(); i++) {
const RtLoaded *m = ldr_module(i);
if (!pre[i]) continue;
uint8_t *now = (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].plugin == plugin && 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);
}
}
if (off < m->data_off) restore_page(m, off, pre[i] + off);
else memcpy(pre[i] + off, now + off, PAGE);
}
}
for (int k = 0; k < NWRITES; k++) missing += write_tab[k].plugin == plugin && !seen[k];
if (why[0]) return;
if (unexpected)
snprintf(why, cap, "its init wrote %d words the build did not record (first %s)", unexpected, first);
else if (missing)
snprintf(why, cap, "%d of the words the build recorded were not written", missing);
}
#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) { // every plugin's mod off or gone: the game sees its address space as without plugins
mem_query_hook = NULL;
rt_process_memory = NULL;
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;
int first_alias = naliases;
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);
check_writes(i, first_alias, why[i], sizeof why[i]);
char hex[65];
sha256_hex((const void *)(uintptr_t)m->base, m->text_end, hex);
if (!why[i][0] && strcmp(hex, plugin_tab[i].sha256))
snprintf(why[i], sizeof why[i], "its code after the init is not the one the build lifted");
}
for (int i = 0; i < ldr_count(); i++) free(pre[i]);
free(pre);
pre = NULL;
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
}
+4 -4
View File
@@ -625,10 +625,10 @@ int res_wants_wide_overlay(const unsigned out[2]) {
return !res_is_16x9(out[0], out[1]) && rt_patch_set("aspect") >= 0;
}
// Field of view (TOTK_FOV, percent of the game's own, 70..140, live): the camera's vertical view angle times the
// factor on page +60 (recomp/patches-v143.txt, set fov). The horizontal angle follows through the aspect ratio. 100
// switches the set off, so the camera code is the game's own. Runs on a settings change (gfx_present.c
// window_settings_live), the first time at the first present.
// Field of view (TOTK_FOV, percent of the game's own, 70..140, live): the camera's vertical view angle times the factor
// on page +60 (recomp/patches-v110.txt, patches-v121.txt and patches-v143.txt, set fov). The horizontal angle follows
// through the aspect ratio. 100 switches the set off, so the camera code is the game's own. Runs on a settings change
// (gfx_present.c window_settings_live), the first time at the first present.
#define FOV_MIN 70
#define FOV_MAX 140
void res_fov_live(void) {
+8 -3
View File
@@ -1146,7 +1146,8 @@ int romfs_shaped_prompts_ready(const char *folder, unsigned w, unsigned h) { ret
// The setup writes the shaped folders for the displays connected when it ran (installer/steps.py Shapes). After a
// monitor change the menu can start the same generator for the shape of the game resolution: the setup's Python and
// tools/make_romfs_patches.py, which build-cache/rebuild.txt names (installer/steps.py write_rebuild_command: line 1 the
// Python, pythonw on Windows, so no console opens; line 4 the setup's folder), on the install folder's romfs/. Child
// Python, pythonw on Windows, so no console opens; line 4 the setup's folder; both relative to the install folder for
// the build kit, installer/layout.py), on the install folder's romfs/. Child
// processes the game does not wait for: the generator writes manifest.tsv last, so a folder it has not finished is not
// taken, and the next start loads the finished one.
static int prepare_tools(char *python, size_t pcap, char *script, size_t scap, char *dir, size_t dcap) {
@@ -1166,8 +1167,12 @@ static int prepare_tools(char *python, size_t pcap, char *script, size_t scap, c
lines[k++] = p;
p = e + 1;
}
int ok = k >= 4 && !strcmp(lines[1], "-c") && snprintf(python, pcap, "%s", lines[0]) < (int)pcap &&
snprintf(script, scap, "%s/tools/make_romfs_patches.py", lines[3]) < (int)scap;
int ok = k >= 4 && !strcmp(lines[1], "-c");
if (ok) { // the build kit's lines are relative to the install folder
const char *at0 = plat_path_absolute(lines[0]) ? "" : dir, *at3 = plat_path_absolute(lines[3]) ? "" : dir;
ok = snprintf(python, pcap, "%s%s%s", at0, *at0 ? "/" : "", lines[0]) < (int)pcap &&
snprintf(script, scap, "%s%s%s/tools/make_romfs_patches.py", at3, *at3 ? "/" : "", lines[3]) < (int)scap;
}
free(text);
snprintf(path, sizeof path, "%s/romfs/romfs.bin", dir);
return ok && plat_file_exists(python) && plat_file_exists(script) && plat_file_exists(path);
+18 -4
View File
@@ -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 <stdio.h>
#include <string.h>
@@ -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;
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) {
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)) return; // refused, x0 set
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];
+19 -3
View File
@@ -1271,7 +1271,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)
@@ -1282,7 +1288,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);
@@ -1292,7 +1307,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;
+27 -5
View File
@@ -86,7 +86,7 @@ static const char *const text_en[UI_TEXT_COUNT] = {
"bars, or stretch them over the whole picture. Applies from the next movie.",
[UI_MENU_MOVIE_ASPECT_KEEP] = "Black bars", [UI_MENU_MOVIE_ASPECT_STRETCH] = "Stretch",
[UI_MENU_FOV] = "Field of view",
[UI_MENU_FOV_DESC] = "How much of the world the camera shows, in percent of the game's own. Game versions 1.1.0 and 1.4.3.",
[UI_MENU_FOV_DESC] = "How much of the world the camera shows, in percent of the game's own. Game versions 1.1.0, 1.2.1 and 1.4.3.",
[UI_MENU_SHADOW_RES] = "Shadow map size",
[UI_MENU_SHADOW_RES_DESC] = "Size of the sun's shadow map. The game's own size changes with the view, down to about 400 "
"near the ground, so leaf shadows look blocky and jump; a fixed size keeps them still and "
@@ -592,6 +592,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: "
@@ -663,7 +669,7 @@ static const char *const text_ru[UI_TEXT_COUNT] = {
"растянуть их на всю картинку. Действует со следующего ролика.",
[UI_MENU_MOVIE_ASPECT_KEEP] = "Чёрные полосы", [UI_MENU_MOVIE_ASPECT_STRETCH] = "Растянуть",
[UI_MENU_FOV] = "Поле зрения",
[UI_MENU_FOV_DESC] = "Сколько мира показывает камера, в процентах от игрового. Для версий игры 1.1.0 и 1.4.3.",
[UI_MENU_FOV_DESC] = "Сколько мира показывает камера, в процентах от игрового. Для версий игры 1.1.0, 1.2.1 и 1.4.3.",
[UI_MENU_SHADOW_RES] = "Размер карты теней",
[UI_MENU_SHADOW_RES_DESC] = "Размер карты теней от солнца. Собственный размер игры меняется вместе с видом, у земли до "
"примерно 400, поэтому тени листвы выглядят квадратными и дёргаются; постоянный размер делает "
@@ -1173,6 +1179,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), которые порт не может "
@@ -1570,8 +1582,8 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = {
[UI_MENU_OV_FMT_FSR] = "AMD FSR 1, nitidez %d%%", [UI_MENU_OV_SCALE_NONE] = "nenhuma (1:1)",
[UI_MENU_OV_SCALE_BILINEAR] = "bilinear",
[UI_MENU_FOV] = "Campo de visão",
[UI_MENU_FOV_DESC] = "Quanto do mundo a câmera mostra, em porcentagem do original do jogo. Nas versões 1.1.0 e 1.4.3 do "
"jogo.",
[UI_MENU_FOV_DESC] = "Quanto do mundo a câmera mostra, em porcentagem do original do jogo. Nas versões 1.1.0, 1.2.1 e 1.4.3 "
"do jogo.",
[UI_MENU_SHADOW_RES] = "Tamanho do mapa de sombras",
[UI_MENU_SHADOW_RES_DESC] = "Tamanho do mapa de sombras do sol. O tamanho do jogo muda com a vista, até cerca de "
"400 perto do chão, então as sombras das folhas ficam quadriculadas e pulam; um "
@@ -1788,6 +1800,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 "
@@ -1914,7 +1932,7 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = {
[UI_MENU_MOVIE_ASPECT_STRETCH] = "拉伸",
[UI_MENU_FOV] = "视野",
[UI_MENU_FOV_DESC] = "镜头能看到多大范围,以游戏自身视野的百分比表示。"
"支持游戏版本 1.1.0 和 1.4.3。",
"支持游戏版本 1.1.0、1.2.1 和 1.4.3。",
[UI_MENU_SHARPNESS] = "FSR 锐度",
[UI_MENU_SHARPNESS_DESC] = "FSR 缩放后的锐化强度;0 为关闭。",
[UI_MENU_COLOR] = "色彩",
@@ -2480,6 +2498,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。",
+4
View File
@@ -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