XAPI GetExitCodeThread reads the object ObReferenceObjectByHandle hands back: SignalState at +4, ExitStatus at +0x120. The bridge returned NULL, so a title that waits for a loader thread this way read STILL_ACTIVE for ever. Each worker spawned by PsCreateSystemThread(Ex) now has a zeroed guest object (Type 6), reference counted host-side: one reference for the running thread, one per handle token, one per ObReference*. When the thread ends, by PsTerminateSystemThread or by returning, ExitStatus is written and then SignalState set. KeWaitForSingleObject and KeWaitForMultipleObjects wait on the object through the KE shadow table, and NtCurrentThread (-2) gets the caller's object. PsCreateSystemThreadEx's TlsDataSize is honoured. The kernel carves that many bytes off the top of the new stack, points KTHREAD.TlsData (+0x28) at them and makes NtTib.StackBase the address past them; XAPI's TLS index is -(TlsDataSize/4), so the startup code and the CRT must agree on one dword. Sizing TLS from the XBE header's copy of the directory, which a title may leave zeroed, put the CRT's per-thread pointer 0x90 bytes below the block. Workers take the block from their stack and the main thread from the heap. A TlsDataSize of 0 keeps the old layout. The handle-token table gets a lock. A worker starts running inside CreateThread, before its creator writes the thread's token, so a loader that opens a file at once can race it for the same free slot; one token then named both the thread and the file, and closing the file orphaned the thread object. NtDuplicateObject gives the duplicate a fresh token. On POSIX, DuplicateHandle returns its source pointer with one more reference, so the duplicate shared the source's token and NtClose on either cleared both. When the host returns the current-process pseudo-handle itself as the duplicate, as the POSIX shim does, the guest gets its own -1 back instead of a zero token. tests/kernel_bridge checks that ordinal 197 duplicates both pseudo-handles and pops its arguments. The new POSIX-only tests/kernel_memory_bridge_posix drives the thread bridges by ordinal against a mapped guest window; without these changes 7 of its checks fail. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Kernel bridge
The dispatch path a title's kernel calls actually take. No game files and no mapped guest window are needed -- a plain buffer stands in for guest memory.
cmake -S tests/kernel_bridge -B build/kernel-bridge
cmake --build build/kernel-bridge
ctest --test-dir build/kernel-bridge --output-on-failure
Runs on both hosts, unlike the _posix projects beside it. Nothing in it is
platform-specific: no fork, no signals, no POSIX headers -- just
VirtualAlloc, stdio, and the kernel's own API, which resolves to
<windows.h> on Windows and to the shim in platform/xbox_winnt.h
elsewhere. Add -A x64 when configuring alongside the Windows regressions.
What does differ per host is xbox_kernel_bridge_init()'s VirtualProtect
over the thunk region -- real on Windows, shimmed here -- and the first check
below fails if that rewrite does not happen, which is a reason to run this in
both places rather than only one.
What it guarantees
Checks on ordinal 173, MmGetPhysicalAddress, and ordinal 197,
NtDuplicateObject.
A thunk entry resolves to a bridge entry point. Given an import in the
form an XBE stores it (0x80000000 | ordinal), xbox_kernel_bridge_init()
rewrites the entry in place to a synthetic VA, and recomp_lookup_kernel
turns that VA back into a callable dispatcher.
Ordinal 173 translates through the contiguous window. Dispatched with a
guest stack, an address inside
[XBOX_CONTIG_BASE, XBOX_CONTIG_BASE + XBOX_CONTIG_SIZE) returns its physical
offset. DMA consumers are programmed with the result, so a virtual-window
address reaching one is not a value that faults -- it is one the NV2A reads as
a physical address with bit 31 set.
Ordinal 173 pops its stdcall argument. After the call esp has advanced
past the return address and the four bytes of argument. A wrong
stdcall_args_for_ordinal entry corrupts the caller's frame some distance
from the call, with nothing naming the ordinal.
Ordinal 197 duplicates the guest's pseudo-handles. NtCurrentThread()
and NtCurrentProcess() are the 32-bit values -2 and -1. A by-value handle
token that is not a table token must be sign-extended for these two: widened
with zero extension they are not the host's pseudo-handles, so Windows fails
the duplicate and the POSIX shim dereferences the value as an object and
faults. Each call must succeed, write a target token, and pop 12 bytes of
arguments.
Why this is a separate project
Two reasons, one practical and one about what the checks are worth.
xbox_kernel_bridge_init() has process-wide side effects: it rewrites a thunk
table in guest memory and installs slot state that later calls read. That does
not belong partway through a run of unrelated API checks.
More importantly, tests/kernel_memory_posix calls xbox_MmGetPhysicalAddress
directly, and that is not how a title reaches it. Ordinal 173 previously had
two implementations that disagreed -- the bridge translating, and
xbox_MmGetPhysicalAddress returning its argument unchanged as a placeholder.
The bridge now calls the latter, so the arithmetic exists once; but a bridge
that stopped delegating would leave every check in that project green. This
one fails instead, which was measured rather than assumed.
How the bridge is reachable at all
bridge_* functions are static, and kernel_thunk_dispatch reads guest CPU
state (g_kernel_dispatch_slot, the guest stack) rather than taking
arguments, so neither can be called directly. recomp_lookup_kernel is not
static: given a synthetic VA it selects the slot and returns the dispatcher.
Set g_xbox_mem_offset to a buffer, lay out a guest stack, set g_esp, call.
tests/kernel_directory uses the same seam for the directory ABI, and is the
older example of the pattern -- it is a Windows test, which is the precedent
for this one building there.
The stubs at the top of test_main.c
recomp_lookup and recomp_lookup_manual are provided by the generated
title. A regression harness has no title, and a static archive resolves only
what gets pulled in, so reaching the kernel bridge needs stand-ins. Nothing
here calls a guest function.