Hooks are declared in src/nx/nn/hooks.hook, one per line, each under the family line before it:
family libc
hook memcpy(void* dst, const void* src, u64 size) -> void* = hook_memmove
_ZN2nn2os...). Names are never parsed, demangled or mangled.this as its first argument. The
types are void (a result only), bool, u8 to u64, i8 to i64, addr (a guest address) and f32/f64,
each of the size its name says in the guest (AArch64, LP64), usize and isize (the guest's size_t and
ptrdiff_t), and pointers. Integers take x0 to x7 and floats v0 to v7 under AAPCS64, counted apart. An argument
that would go on the stack is refused.hookgen --a32 (snx_hookgen(... A32)) also generates an adapter for a 32-bit program, a block of its code table
(SnxArmBlock). It reads the arguments where AAPCS32 with VFP arguments puts them: an integer, a pointer, a usize
or an isize in a word, a u64 or an i64 in an even pair of r0 to r3, floats in s0 to s15 and doubles in d0 to
d7, and the rest on the stack. It calls the same handler with the thread's CPU, and returns to lr in the state lr's
bit 0 gives. A pointer and a usize reach the handler zero-extended, an isize sign-extended. The NVN spec uses it:
tests/hook_adapters.c checks the mapping.a32 after a hook's handler (after pure) says the declaration holds for a 32-bit game too: its
arguments are the function's under AAPCS32 as written (a usize for a size_t, never a u64), and its handler
reads nothing whose layout differs in the 32-bit SDK. A hook that uses a struct can't be one: a struct's layout
is one SDK's. A @version after that a32 gives the versions the declaration holds for in a 32-bit game, in place
of the hook's own: hook SYMBOL() -> u32 = handler a32 @version(< 11.0.0), and several give one group each, any of
which holds, for ranges apart: a32 @version(< 1.0.0) @version(>= 4.0.0, < 6.0.0). The 32-bit SDKs a function was
read in are not the 64-bit ones: a range holds the SDKs read and the versions around them that the 32-bit SDKs read
agree on and that switchbrew shows the function's service unchanged in, stopping where the 64-bit declarations
change their handler or layout; each family's .hook says which SDKs were read and which versions hold by
inference (tests/nn_*.c check each family's ranges). A 32-bit game of a version inside a range but not read is
said once in the log (hooks: SDK 9.3.0 of a 32-bit game (module sdk) is inside the ranges read at ...: not read
itself), and one of a version outside them has no hook, and stops when its SDK opens a service the runtime answers
only natively ("Versions", below).a32 declares a hook for a 32-bit game alone: a32 hook SYMBOL(...) ..., with a
@version before it (or several, one group each) and uses after its handler as any hook. It is for a function whose declaration differs
there. A 32-bit SDK mangles a size_t as j where the 64-bit one has m (and an int64_t as x for l), so a
function with one in its signature has another symbol. AAPCS32 returns a class of more than 4 bytes through a
pointer the caller passes in r0, where AAPCS64 returns one of 8 bytes in x0, so nn::time's now() is
() -> i64 for a 64-bit game and a32 hook ...(addr out) for a 32-bit one, under one symbol. And a struct there
has its own layout, declared as a struct of another name. A symbol's declarations for a 32-bit game (those marked
a32 and those for it alone) are checked against each other, never against the ones that don't hold for it.hookgen --a32-marked (snx_hookgen(... A32_MARKED)) writes a 32-bit game's table after the other,
<prefix>32_table with <prefix>32_symbol, _fn (the adapters, SnxArmBlocks), _handler, _family, _key,
_lo, _hi and _layouts: the hooks marked a32 and the ones for a 32-bit game alone, which are in no other
table and name no route (a 32-bit game's every call goes through its code table). The runtime's own spec is
generated this way, and for a 32-bit game hook_install puts each adapter of that table in its function's slot
of the code table (arm_slot), a versioned one by the SDK label of the module that exports the function, as for
a 64-bit game; every other SDK function runs its own code there.*, each * optionally followed by const, after an optional const:
const NVNtexture* const* textures. It points to void, char, one of the types above, or a type of the C header
that the spec's include line names (include "nx/gpu/nvn/nvn.h", found in the folders that -I gives hookgen).
hookgen reads the names that the header's typedefs declare, and refuses a name it doesn't declare. A type of the
header is passed by value only when it is an enum (an int under AAPCS64, so an i32) or a pointer
(typedef void* NVNnativeWindow, or a function pointer type, so an addr). The language has no arrays: an array
argument is a pointer to its first element. A const without a * after it is refused, as it says nothing the
caller sees.u64, and the handler takes it as a
u64, as an addr: the type only says what it points to, for the function types below and for the tools that
read the spec (tools/hook_spec.py). No handler can take it for a host pointer.ret handler(CPU* c, args...), declared by the generated nn/hooks.hook.h. It reads and
writes guest memory at c->mem + addr, and calls the HLE kernel for anything that waits (a guest thread holds its
emulated core until a kernel wait releases it, so a handler never blocks on a host primitive).= HANDLER names the handler. Without it, the handler is the symbol with a _ before its first capital:
hook nvnDeviceFinalize(NVNdevice* d) runs nvn_DeviceFinalize. The symbol must have a part in lower case before
that capital (memcpy and _ZN... don't, so they name their handlers). An = HANDLER that names the handler the
symbol gives already is refused, so that each name is written once.With an include line, the generated header also includes that header, and gives each hooked function its type as
the guest calls it, PFN<SYMBOL in capitals>PROC:
hook nvnTextureWriteTexelsStrided(const NVNtexture* t, const NVNtextureView* v, const NVNcopyRegion* region, const void* src, i64 row, i64 image)
typedef void (*PFNNVNTEXTUREWRITETEXELSSTRIDEDPROC)(const NVNtexture* t, const NVNtextureView* v, const NVNcopyRegion* region, const void* src, int64_t row, int64_t image);
gpu/nvn/nvn.hook is thus the one prototype of each NVN function the runtime implements: nvn.h keeps only the
types of the callbacks and the resolvers, which no hook declares, and tools/nvn_hash.py reads the prototypes from
nvn.hook (tools/hook_spec.py). Each type has the guest's size, whatever the host:
<stdint.h> type of its size (int32_t, int64_t), and bool a uint8_t (NVN's
NVNboolean), never a host type that changes with the host: long is 32 bits on Windows, and a size_t or a
ptrdiff_t is as wide as the host's pointers. A ptrdiff_t of the guest is an i64 in the spec, and a size_t a
u64.nvn_pointer_size).int when its values fit in one, and each of NVN's has a
0x7FFFFFFF member to make it 32 bits.Nothing calls through these types: a guest function runs at a guest address, through its dispatch slot. All the
declarations of one symbol take the same types, as it has one function type. tests/nvn_header.c checks some of
them against prototypes in NVN's own types, with -pedantic-errors.
tools/hookgen generates the adapters (a GuestFn that unpacks the registers, calls the handler, puts the result in
x0 or v0 and returns to x30) and the table sorted by symbol. tests/hook_adapters.c checks the register mapping,
and the hookgen_refuses_* tests the declarations it refuses.
The Vita's system functions are declared in the same language, by NID ([0x99ba173e] hook sceIoLseek(...)), and
hookgen --vita generates their adapters and table: The system's functions by NID.
Made from docs/platforms/nx/hooks.md at 69237645142c. GPL-3.0-or-later.