How a call reaches a hook

Every call to a hooked function goes through its slot in the dispatch table, g_disp (snx_lookup in cpu.h):

The recompiler routes only the symbols of the route list (src/nx/loader/hook_routes.c): each symbol that hooks.hook, gpu/nvn/nvn.hook and gpu/gl/gl.hook name in any version, and the route lines of the functions the runtime wraps with code of its own (nvnBootstrapLoader and nvnDeviceGetProcAddress, nvn_loader.c; a GL driver's eglGetProcAddress, gl_census.c; the Vulkan loader's entry points, gpu/gvk/gvk.hook). hookgen --routes generates the list. A direct call to any other function stays a direct call. SNX_ABI 15 and 16 first routed every function that a module exports, but some games export nearly all of their main executable: 88% to 98% of its functions in three games, so that 51% to 64% of all their direct calls became indirect calls (tools/hook_census.py). On one of them, in a gameplay scene, routing only the list's symbols took 9% of the cycles and 3.6% of the instructions that the main guest thread spends in the recompiled code each frame, and a quarter of the process's mispredicted indirect branches. The module became 7% smaller. Small accessors that went through their slots are inlined into their callers again. The frame time did not change there, because the GPU thread set it.

Each module carries the list it was recompiled with (snx_routes(), SnxRoutes in module_abi.h): the hash of the list and the hash of each symbol. hook_install logs the module's list. When it installs a hook whose symbol the module's list doesn't name, it warns that the hook sees only the calls through function pointers and the PLT stubs the module did not bind, and that a recompile gives it the direct calls too. The NVN loader (nvn_loader.c) does the same for nvnBootstrapLoader. A module made before the list has no snx_routes, and routes every function it exports, so it works with this runtime unchanged. The list doesn't change SNX_ABI: a hook's handler keeps the guest's data as its function does, so a direct call that runs the function's own code is still correct, only not accelerated.

hook_install (src/nx/nn/hook.c) reads the game's exported functions (kernel_load_symbols) and puts the hook's adapter into each matching function's slot, before the game's first thread and the JIT start. The slots never change while the game runs, except for hook_restore putting a hook back after the JIT drops the code of a range (jit_code_invalidate), and a hook the game invalidates (below). It holds HOOK_SITES hooked functions (hook.h), as many as a table has entries at most, and hook.c does not build with a table larger than that, so a game that exports each function once gets every hook. One whose modules export them more than once can run out: the game then stops, naming the function left without its hook, since that function would run the SDK's own code beside the native hooks of its family, which keep the family's state, and a service that only the native functions answer would be asked (tests/hook_sites.c).

What no hook sees is code the game's own compiler inlined from an SDK header: there is no call to take. A handler therefore keeps the guest's data as the function does, so inlined copies read what it left.


Made from docs/platforms/nx/hooks.md at 69237645142c. GPL-3.0-or-later.