A stub reaches the thread's CPU from TPIDR_EL0 at a fixed offset (NceEnv.tls). On Linux that is static TLS of
the executable. Android's runtime is libscarletnx.so, a library the app loads: built for API 21 its TLS is emulated
(__emutls_get_address, no offset at all), and from API 29 bionic refuses an initial-exec access in a loaded
library. The word the stubs read is bionic's TLS_SLOT_APP (TPIDR_EL0 + 16), which the app owns from Android 10
(API 29). It holds a pointer to the thread's block, so the stubs load twice (NceEnv.indirect) in the same three
words, and the outcall's asm does the same (nce_host.c).
TPIDR_EL0 stays the host's while guest code runs. ART's signal handlers, reached through libsigchain, and the
runtime's own (the write tracking's SIGSEGV, NCE's kicks and traps) run C code as they do without NCE, with bionic's
TLS where they expect it.
Limits:
TLS_SLOT_APP breaks the stubs. The runtime checks the
word is 0 before it reserves anything (nce_host_check): if not, NCE is off, the log names the value found, and the
JIT compiles the game's code. Each thread checks again as it starts guest code (nce_host_attach), and a word
taken there ends the game with the value. A library that writes the word while the runtime serves a guest thread
(an outcall) is not seen.errno in that word: no NCE, with the API level in the recompile's line.What would serve them is Eden's way (src/core/arm/nce/arm_nce.s): TPIDR_EL0 set to the thread's NCE block while
guest code runs, and the host's put back on each outcall. It needs no slot, so it works from API 21. The cost:
TPIDR_EL0 in asm before any C, the write tracking's SIGSEGV
among them, which is a hot path.sigaction
(common/signal_chain.cpp) to stay ahead of it. Every handler of the runtime would need that.MRS TPIDR_EL0 already goes through a stub, so the stubs stay as they are; the outcall and the four
handlers change.Made from docs/platforms/nx/nce.md at 69237645142c. GPL-3.0-or-later.