#include <scarletnx/snx_recompiler.h>
static void my_svc(CPU* cpu, uint32_t imm) { /* cpu->x[] holds the arguments */ }
static void my_undef(CPU* cpu, uint64_t pc, uint32_t word) { cpu->pc = 0; }
static void my_brk(CPU* cpu, uint64_t pc, uint32_t imm) { cpu->pc = 0; }
struct snx_recompiler* r = snx_recompiler_new(SNX_RECOMPILER_FLAG_NATIVE);
struct snx_recompiled* m = snx_recompile(r, 0x8000000, text, text_size);
if (!m) fprintf(stderr, "%s\n", snx_recompiler_error(r));
RtAPI rt;
snx_runtime_defaults(&rt);
rt.svc = my_svc;
rt.undef = my_undef;
rt.brk = my_brk;
CPU cpu = {0};
cpu.mem = guest_memory; /* flat: guest address a is guest_memory[a], the code included */
cpu.rt = &rt;
cpu.thread = my_context; /* yours, for the hooks */
cpu.x[31] = stack_top;
cpu.x[30] = 0; /* the entry's return address: reaching it ends the run */
snx_recompiled_bind(m, &cpu);
snx_recompiled_run(m, &cpu, 0x8000000, 0);
snx_recompiled_free(m);
snx_recompiler_free(r);
snx_recompiler_new takes the backend: SNX_RECOMPILER_FLAG_NATIVE, the only one
(SNX_RECOMPILER_FLAG_LLVM, its name in earlier versions, is still taken for it and said once), which
names the host's native back end (x86-64's, arm64's or another machine's,
src/common/cpu/recompiler/mach/native.h): it makes all of the A64 code.
snx_recompiler_has tells whether this host has one. The recompiler also takes the -O
level (snx_recompiler_set_optimization, 2 by default: the native back end's code is
the same at every one, and the caches keep it), how many code units compile at once
(snx_recompiler_set_jobs, on threads),
progress lines (snx_recompiler_set_verbose) and a callback with how far a recompile is,
for a progress bar (snx_recompiler_set_progress). snx_recompiler_set_keep_ir asked for
LLVM's IR, and is refused with the folder named. snx_recompiler_set_backend(r, 0), LLVM
alone in earlier versions, is refused at the recompile. snx_recompiler_set_hook_direct_calls
routes calls between discovered functions through the CPU's dispatch table. Enable it
when the runtime can invalidate guest code, so callers do not retain direct pointers to
retired functions.
snx_recompiler_set_a64_target chooses the A64 features of the guest's CPU, which
decide what the decoder (tools/decodetree, from QEMU's a64.decode) accepts: an
encoding needing a feature the target lacks is an undefined instruction, as on that
CPU. SNX_RECOMPILER_A64_SWITCH, the default, is the Switch's Cortex-A57 (Armv8.0
with CRC32 and the Cryptographic Extension). SNX_RECOMPILER_A64_MAX takes every
feature the decoder knows; native_qemu_differential checks the host's native code against QEMU for it, and the
native back
ends' tests check theirs (FEAT_FP8's as FPMR zero has them, which is all code can do
here: an MSR or MRS of FPMR is undefined). An instruction a native back end has no
code for is said at the recompile, once a function: its address, the instruction, its
word and address, the feature and the target, and how many functions there were; the
function is made, the instruction undefined where it runs.
SNX_RECOMPILER_A64_ANDROID is an Android phone's CPU, as the Android
driver's native bridge runs it: Armv8.2 with the atomics, RDM, half precision, the
dot product and LRCPC, without pointer authentication, so that the PACIASP and
AUTIASP of the HINT space are NOPs, as on a phone without the feature.
snx_recompile recompiles the bytes of one buffer as they will be found at a guest
address (both multiples of 4), and loads the result: on Linux from an anonymous memory
file, elsewhere through a temporary file, removed once loaded on Unix and when the module
is freed on Windows, where a loaded DLL keeps its file. snx_recompile_ranges takes
several ranges (the .text of several modules) and hints: known function addresses,
such as the entry point or a symbol table. Functions are found by exploring calls and branches from the
start of each range and from the hints, as the game workflow does. Code the
exploration does not reach is not lost: the module keeps a copy of its code ranges, and
its JIT (jit_service.c, started by the first miss) compiles any address of them the
dispatch table has no function for when a CPU reaches it, which waits for the compile.
An address it can't compile (outside the ranges, or on a host without the JIT) is an
undefined instruction. With snx_recompiler_set_live_code the module keeps no copy: the
JIT reads the caller's ranges where they are, so code the guest patches after the recompile
(a game's own hooks, reported with IC IVAU) is compiled as it then is, and the ranges must
outlive the module. snx_recompiled_functions reports what was recompiled, and
snx_recompiled_fallbacks is always 0: an instruction without an emitter fails the build.
The contract between the generated code and the caller is cpu.h (the CPU state,
versioned by SNX_ABI, which snx_recompiler_abi reports) and
runtime/interface.h (RtAPI). snx_runtime_defaults fills every hook but svc,
undef and brk, which the caller writes, and snx_recompiled_bind refuses a CPU
without them. Its miss runs the code the module a CPU is bound to compiles for a pc
it has no function for. snx_svc_call64 is the type of the SVC hook: the generated code calls
it with cpu->pc past the SVC instruction, and the handler reads and writes the
registers. snx_recompiled_bind points a CPU at the module's dispatch table and code
range. Any number of CPUs, run by any number of threads, may be bound to one module.
Every recompiled function is a host function F(CPU*, entry) that returns when the
guest function returns, with cpu->pc its return address, or right after the brk
hook ran, or the undef hook for a word that decodes to nothing, with cpu->pc at that
instruction. snx_recompiled_call makes one such call. snx_recompiled_run repeats it
from cpu->pc until cpu->pc reaches the stop address: the caller puts that address in
x30 before the entry, and has undef and brk set cpu->pc to it when they end the
run. One case is different: an instruction that decodes but whose encoding the helpers
reject (a reserved SIMD or system register form) calls undef from inside its body and
goes on with the next instruction, without touching cpu->pc. A handler that must stop
there records the fault for the caller to act on, or longjmps out of the generated code,
as the Switch runtime does with a fatal error. snx_recompiled_lookup gives the host
function for an address, for callers with a run loop of their own.
A just-in-time compiler is built from three more calls. snx_recompile_from compiles
only the functions at its hints and the functions they call, breadth first, until about
a given number of instructions, skipping call targets a callback says are compiled
already, and splits a function longer than 4096 instructions into pieces. Its module's
table covers just what it found. snx_recompiled_function gives the function a module
has for an address (NULL for any other), for the caller's own dispatch table. A caller with
its own table also gives RtAPI.miss its own handler, which compiles the code at that
address and runs it, as the Linux runtime (src/gnuabi/runtime/jit.c) does. A caller that
keeps snx_runtime_defaults' miss can instead name that handler with
snx_recompiled_set_fallback, which the module's misses then run instead of its own JIT. snx_recompiler_set_cache names a
folder that keeps linked modules by a hash of everything they are made from (the native
back end, the host's features it uses and its code's version among them): a module found there is loaded instead of compiled. The
units its lazy JIT compiles on a miss are kept in library-jit.cache in the same folder,
and a later miss loads a unit instead of compiling it when its code is the same, byte for
byte, and the file was made with the same settings, native back end and host features. The modules of one
program and other programs may share the file. A damaged file is made again, and a folder
that can't be written to leaves the JIT working without the file.
snx_recompiled_invalidate clears dispatch entries for compiled functions overlapping
[addr, addr+size) and purges matching lazy JIT units. Host code stays loaded for running
threads. An optional callback receives each retired [start, end) span.
snx_recompiled_update_code(m, addr, code, size) copies the guest's current bytes for a
word-aligned range into the module's own copy of the code, the copy its JIT compiles from,
and returns the bytes inside the module's ranges. Call it with the changed bytes before
snx_recompiled_invalidate. Without it, a module made for an embedder compiles a patched
function again from the bytes it was made from. A module made with
snx_recompiler_set_live_code has no copy, so the call returns 0 and writes nothing. JIT workers read copied words
atomically while they compile. Concurrent updates to one module are serialized, and readers reject copied-image
reads when the image generation changes; callers still invalidate changed functions after updating the bytes.
For A32 and Thumb, snx_recompile32 prepares an address range whose blocks are compiled
on first use by the AArch32 block compiler, the host's native back end's (src/common/cpu/arm/), and
snx_recompiled32_run runs an SnxArmCPU (cpu/arm/arm.h) from r[15] until an exit.
The SVC hook is an snx_svc_call32: it sees r7 as the number and r0 to r5
zero-extended, the console's 64-bit SVC surface, and returns non-zero to end the run.
The bytes are read from cpu->mem, so the code must be there. Ranges are multiples of 2
bytes (Thumb) and end at or below 4 GiB. cpu->exit is cleared when a run starts, so a
run after an exit starts over from r[15]. cpu->rt (an SnxArmRt, or NULL) decides
what the blocks leave to the system: instructions they can't run, and the coprocessor
registers (cpu/arm/README.md). With NULL, they stop with an exit, and the thread ID
registers come from the CPU's fields.
Made from docs/dev/library.md at 69237645142c. GPL-3.0-or-later.