src/nx/ directory | Responsibility |
|---|---|
runtime/ | Module loading, INI, clock, RtAPI, crashes; settings.txt, every setting (tools/settingsgen generates snx_settings.h, g_settings and the tables the runtime, the launcher and the recompiler read them with) |
hle/kernel/, hle/svc/, memory/ | Objects, threads, sync, SVC, Switch memory, IPC |
hle/services/, hle/filesystem/ | Services, nvdrv, guest filesystem services |
gpu/ and gpu/nvn/ | Guest memory as the GPU sees it, the GPU thread, NVN (loader, objects, command buffers, replay), shaders, Vulkan renderer |
gpu/maxwell/ | The Maxwell front end for programs that write their own command buffers: the 3D, compute, 2D, DMA and inline-to-memory engines, the macro interpreter, the registers as the draw state, the last GPU write to each page |
gpu/video/ | host1x, NVDEC H.264, VP8 and VP9 decoding through FFmpeg, VIC |
runtime/platform/ | Host APIs, SDL3/headless, input, screenshots, timed exit, the in-game menu, the shader loading screen |
The host layer that every driver shares is src/common/platform/: threads (thread.h, thread_spawn.c) and the
Windows resources now, with the rest of the plat_* functions (time, files, memory, libraries, the log) moving there
from runtime/platform/.
Guest thread = host thread + CPU + TLS. hle/kernel/thread.c: one owner/core, four cores; release on kernel block,
sleep/yield, IPC, WFE. Waiters: priority then FIFO; a waiter of higher priority takes the core at the holder's next SVC
or backward branch (CPU.poll, RtAPI.poll), or where NCE's kick stops its code. Game code stopped with its TLS
disable count raised (the SDK's spinning locks) keeps the core, with its interrupt flag set, until its
svcSynchronizePreemptionState. A ready thread that would wait
behind its core's holder runs on an idle core of its mask, and an idle core takes such a waiter (only
svcSleepThread(0) keeps a yielding thread on its core). A mutex owner runs at its best waiter's priority
(hle/kernel/sync.c).
memory/mem.c: one guest VA reservation; GP(addr) = g_mem + addr, accessed directly by generated code.
NCE (runtime/nce/, the setting nce, AArch64 Linux and Android 10 and later, NCE): the game's code
runs on the host's CPU. The recompile makes no code ahead of time. It finds the words to patch with the full analysis
(nce_patch.c): SVC, the TLS registers, the counter registers, and the exclusives, which become their ordered forms.
Each module moves up to leave a pool for its stubs after it, in the reach of a B (128 MiB); a module whose first
words that pool would not reach gets one before it too, as Eden splits its patch section, and a word that neither
reaches (a module past about 256 MiB) ends the recompile, naming the word, the module and its size, with NCE to set
off for the game (nce_layout).
The pools and the sites go in <game>.nce. The game sees a pool as code it has no permission on (QueryMemory), as
Eden reprotects its patch sections, while the host's pages run it: the game's rtld takes each readable and executable
region of code for a module and reads its MOD0 header from the offset at +4. With that file, the runtime reserves the guest's address space at the guest's own addresses (g_mem is
0, and all of it is executable; on Android the whole layout first moves above 64 GiB), and patches the code after
the load (nce.c). A stub calls the outcall of nce_host.c. The outcall
puts the guest's state in its CPU, and calls the runtime on the host's stack through RtAPI (svcs, poll). A read
of the counter registers is made in its stub, from the host's counter, which guest time then follows.
A run of native code starts from the whole CPU through a signal that the thread sends to itself, and sigreturn
loads it. The same signal resumes the guest after a kick (another signal) stopped it for CPU.poll, or after
SIGILL or SIGTRAP. A hook or NVN takes a function as in the recompiled code, by its dispatch slot. Under NCE,
the function's first word then branches to an outcall that runs what the slot holds. An empty slot means the
function's own code, from its first word, which the stub moved. ldr:ro's NROs are patched as they are mapped.
SVC -> its wrapper in hle/svc/svc.c's tables, generated by tools/svcgen from hle/svc/hos.svc (each SVC's
registers in the 64-bit and 32-bit ABIs), which calls the SVC's handler; svcSendSyncRequest -> hle/kernel/ipc.c -> session's HLE service object, whose dispatcher
is generated from the interface's .swipc file and calls the service's typed handler (IPC interfaces). A
service the runtime doesn't have gets sm's stand-in object, which answers every command with success.
Filesystem: startup scan; indexed lookup, metadata, enumeration, mutations.
Made from docs/dev/architecture.md at 69237645142c. GPL-3.0-or-later.