A game module (<id>_code.so, .dll on Windows) is code the recompiler made for one contract with the runtime:
CPU layout and the SNX_FP_* numbers in src/common/cpu/aarch64/cpu.h;cpu_helpers.c);RtAPI and SNX_SVC_COUNT in src/common/runtime/interface.h;src/nx/loader/module_abi.h.SNX_ABI in cpu.h versions it. The recompiler writes it into each module (SnxModule.abi) and into its game's
<id>.abi stamp; the runtime refuses a module of any other, its message naming both ("ABI 12, where this one runs
13"), and the driver recompiles a game stamped with another when it is played.
Bump SNX_ABI for any change to the contract: a field of CPU, RtAPI, SnxModule or SnxModInfo added, removed,
moved or resized; a helper's results; an SNX_FP_* number; SNX_SVC_COUNT; an RtAPI function's arguments. Bump it
too for an addition the games should get though older modules would run without it (13's snx_variants() table),
and for a change of the addresses the guest's code lies at (18's modules one after another): a save state holds the
guest's memory at the addresses it was saved with, and is refused by SNX_ABI. A change to the recompiler's own
output for the same contract leaves it alone, and every game keeps its old code until it is recompiled (-r, or R in
Games).
The module's snx_routes() table (SnxRoutes, with its own SNX_ROUTES_VERSION) is read as optional and left
SNX_ABI alone: it names the symbols whose calls the module sends through their dispatch slots
(Hooks). A module without it sends the calls to every function it exports through
their slots, which every hook also works with.
A bump costs a recompile, which the x86-64 back end makes in seconds where it is built. SNX_IR_OBJECT_VERSION
(ir.h) rises when snx_ir_module_object makes another object from the same IR.
ctest -R module_contract (tests/module_contract.c) holds the contract's layout as it was at the last bump: each
field's offset and size, the structs' sizes and the numbers above. A change to any of them fails it, naming the field,
until SNX_ABI rises and the table the failure prints is recorded there. It cannot see a helper's results, an RtAPI
function's arguments, or a field added into a struct's tail padding: for those the list above is the rule.
SNX_ABI | What changed (cpu.h) |
|---|---|
| 7 | CPU.poll and RtAPI.poll |
| 8 | the FP/SIMD emitters and discovery |
| 9 | RtAPI.svcs |
| 10 | RtAPI.miss, and CPU.disp read with relaxed atomic loads |
| 11 | the reworked LLVM backend's modules |
| 12 | RtAPI.cas128, and snx_cas128 takes the CPU |
| 13 | the AOT module's snx_variants() table, read as optional |
| 14 | RtAPI.code_changed for IC IVAU, and SnxModule's function table with the byte each AOT function checks first |
| 15 | calls to an exported function go through its dispatch slot, which a hook of the runtime's may hold |
| 16 | stx's result for a store-exclusive whose range the armed monitor covers |
| 17 | the FP helpers take FPCR by value and return their result with the FPSR flags they raised, and generated functions call snx_fp_env on entry |
| 18 | a program's modules lie one after another, as the console's loader lays them (src/nx/loader/loader.h); the contract is as it was |
| 19 | the route list names the SDK functions the runtime answers natively (nn::fs, time, pl, ngc, nsd, prepo, codec): a module made before calls the SDK's own code, which opens services no longer served; the contract is as it was |
Mods for Atmosphère (Skyline, exlaunch) hook a game by writing branches into its code through a writable view of it
(svcMapProcessMemory), then make the new instructions visible the way Arm requires, with IC IVAU on each line
they wrote. The AOT code was compiled from the bytes before, so since SNX_ABI 14 IC IVAU reaches the runtime
(snx_ic_ivau, RtAPI.code_changed), and jit_code_changed (runtime/guest_jit.c) finds the AOT functions compiled
from the line by the descriptor's fn_lo and fn_len. Each one loses its dispatch slots and its entries in
entry_fn (which the JIT calls AOT functions through), then its byte in SnxModule.stale is set. Every AOT function
reads its byte first (stale_check in ir_codegen.c) and, once it is set, runs its entry through dispatch, where the
lookup finds no code and the JIT compiles the instructions as they are now, the patch included. A direct call from
other AOT code arrives at the same check, so a hook in the middle of a function takes effect for every caller. A call
returning into an AOT function reads the byte too: a loader may patch a function from inside a call it makes, and once
the byte is set the function leaves for the thread's dispatch loop at the return address, which runs the new code from
there. The
JIT's code goes as any other code change drops it (jit_code_invalidate): snx_jit_invalidate drops every unit
whose span of guest code the change touches, all its leaders, since a unit's functions call each other directly. That
covers hooks in code the game maps itself (NROs), which only the JIT runs.
Made from docs/dev/architecture.md at 69237645142c. GPL-3.0-or-later.