mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-16 18:09:58 -04:00
Each player keeps their own game and save. One player makes a room, the others join it with its secret, and story flags and, by the room's rules, items, songs, quest items, small keys (a shared pool), rupees and ammunition reach every save. The layer is a host thread with one UDP socket to a relay the players run themselves (tools/coop_relay.py, Python's standard library only); the messages between the players are sealed with a key from the room's secret (XChaCha20-Poly1305, Monocypher 4.0.2 vendored with its licence and a row in THIRD_PARTY_NOTICES.md), every change is signed by the player who made it, a player accepts a room by its members' keys and the key of its rules, the save is copied to coop-backup before anything is applied, and values are set through the game's own routines between frames. The Co-op tab makes, joins, accepts, removes and leaves, and the menu takes typed text for the name, the relay and the secret. This is the contributor's pull request #518 on this line. Nothing reaches the network while co-op is off: the socket is opened, and the relay's name looked up, only after co-op is on, a relay is set and a room is made or joined. There is no default relay. Left out of the pull request: the other players drawn in the world (the "puppets") with the copied engine they need. They were unfinished, added two patch sites to the game's per-frame update that could not be put through the differential tests here, and their file helpers clashed with the ones this line already has. So no position is sent: the POS message's codec stays, nothing sends it. Changed against the pull request: - The menu's test hook drives the Co-op tab only when it comes from the environment. From nx2navi.ini it could turn co-op on, join a room and accept its offer. - The log's label for a room is hashed from the secret instead of being its first three characters. - A game that has not saved yet has no save to copy, and takes nothing from the room until it has saved once; that used to count as a copy made. - A PING's token no longer carries the PC's uptime. - On Windows the maker's key file and its folder get an access list with the player's account alone. - The tests that link the platform layer get the socket library, and the save test a stand-in for the title screen's hook. Checked on Windows, where the pull request had never been compiled: the runtime builds and links; --probe-coop passes (sockets on 127.0.0.1, the codec against its byte vectors, a fuzz of every parser, two sessions through a hostile mock relay, a relay restart, the sync engine); tests/coop_relay_test.py passes its 61 tests, with the probe through the real relay on 127.0.0.1 at 0 and 30 % loss; tests/coop_page_check.py passes and its deliberate fault fails 48 checks. Not checked: two games in one room (one game runs at a time here), any real network, the tab in the game. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
259 lines
11 KiB
C
259 lines
11 KiB
C
// Contains code Copyright (c) 2026 MacNativeGames contributors (MIT; see THIRD_PARTY_NOTICES.md).
|
|
// The SVC entry of the lifted code: preemption checkpoint, trace, then the thread and
|
|
// synchronization SVCs (kernel.c), IPC (ipc.c), memory (memory.c), Break, OutputDebugString, GetInfo and ExitProcess. The 21 SVCs
|
|
// NX2navi has but never reaches on its boot path stop with a named fatal, the caller and the guest backtrace.
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "coop.h"
|
|
#include "fs.h"
|
|
#include "host.h"
|
|
#include "kernel.h"
|
|
#include "vk.h"
|
|
#include "platform/platform.h"
|
|
|
|
#define R_OK 0u
|
|
#define ST_FREE 0u
|
|
#define ST_STACK 0xBu
|
|
#define PERM_RW 3u
|
|
#define PROGRAM_ID 0x0400486024dec000ull // the game's title id (GetInfo 18); fs_host.c NX2NAVI_APPLICATION_ID is the same
|
|
|
|
static uint64_t g_svcs;
|
|
void (*volatile svc_test_exit_hook)(unsigned code);
|
|
|
|
RtStats rt_stats(void) {
|
|
return (RtStats){__atomic_load_n(&g_svcs, __ATOMIC_RELAXED), kern_threads_created(), hostfn_calls()};
|
|
}
|
|
|
|
static int readable(uint64_t a, uint64_t n) { return mem_host_readable(a, n); }
|
|
|
|
void kern_backtrace(const Cpu *c) {
|
|
char d[160];
|
|
rt_logf(" lr %s\n", ldr_describe(c->x[30], d, sizeof d));
|
|
uint64_t fp = c->x[29];
|
|
for (int n = 0; n < 32 && fp && !(fp & 7) && readable(fp, 16); n++) {
|
|
uint64_t next = LD64(fp), lr = LD64(fp + 8);
|
|
rt_logf(" guest frame %2d: %s\n", n, ldr_describe(lr, d, sizeof d));
|
|
if (next <= fp || next - fp > (64u << 20)) break;
|
|
fp = next;
|
|
}
|
|
}
|
|
|
|
static const struct { uint32_t imm; const char *name; } unimplemented[] = {
|
|
{0x13, "MapSharedMemory"}, {0x14, "UnmapSharedMemory"},
|
|
{0x23, "SendAsyncRequestWithUserBuffer"}, {0x28, "ReturnFromException"}, {0x2A, "FlushEntireDataCache"},
|
|
{0x2F, "GetLastThreadInfo"}, {0x32, "SetThreadActivity"}, {0x33, "GetThreadContext3"},
|
|
{0x36, "SynchronizePreemptionState"}, {0x40, "CreateSession"}, {0x41, "AcceptSession"},
|
|
{0x43, "ReplyAndReceive"}, {0x44, "ReplyAndReceiveWithUserBuffer"}, {0x48, "MapPhysicalMemoryUnsafe"},
|
|
{0x49, "UnmapPhysicalMemoryUnsafe"}, {0x4B, "CreateCodeMemory"}, {0x50, "CreateSharedMemory"},
|
|
{0x51, "MapTransferMemory"}, {0x52, "UnmapTransferMemory"}, {0x70, "CreatePort"}, {0x72, "ConnectToPort"},
|
|
};
|
|
|
|
static __attribute__((noreturn)) void svc_fatal(Cpu *c, uint32_t imm, uint64_t pc, const char *why) {
|
|
const char *name = "unknown";
|
|
for (unsigned i = 0; i < sizeof unimplemented / sizeof *unimplemented; i++)
|
|
if (unimplemented[i].imm == imm) name = unimplemented[i].name;
|
|
char at[160];
|
|
fflush(stdout);
|
|
rt_logf("FATAL: svc #%#x %s at %s: %s\n x0 %#llx x1 %#llx x2 %#llx x3 %#llx\n", imm, name,
|
|
ldr_describe(pc, at, sizeof at), why, (unsigned long long)c->x[0], (unsigned long long)c->x[1],
|
|
(unsigned long long)c->x[2], (unsigned long long)c->x[3]);
|
|
kern_backtrace(c);
|
|
rt_fatal("svc #%#x %s is not implemented", imm, name);
|
|
}
|
|
|
|
// QueryMemory sees the guest main thread's stack, which memory.c does not know: Stack inside it, free ranges
|
|
// around it clipped (the way memory.c clips moved stacks).
|
|
static void main_stack_query(uint64_t a, RtMemoryInfo *r) {
|
|
uint64_t mb, ms;
|
|
kern_main_stack(&mb, &ms);
|
|
if (!ms) return;
|
|
uint64_t me = mb + ms;
|
|
if (a >= mb && a < me) {
|
|
*r = (RtMemoryInfo){.base = mb, .size = ms, .state = ST_STACK, .perm = PERM_RW};
|
|
return;
|
|
}
|
|
if (r->state != ST_FREE) return;
|
|
uint64_t lo = r->base, hi = r->base + r->size;
|
|
if (me <= a && me > lo) lo = me;
|
|
if (mb > a && mb < hi) hi = mb;
|
|
r->base = lo;
|
|
r->size = hi - lo;
|
|
}
|
|
|
|
static int on_main_stack(uint64_t a, uint64_t n) {
|
|
uint64_t mb, ms;
|
|
kern_main_stack(&mb, &ms);
|
|
return ms && a < mb + ms && mb < a + n;
|
|
}
|
|
|
|
static uint64_t user_exception_context(void) {
|
|
static void *page;
|
|
void *p = __atomic_load_n(&page, __ATOMIC_ACQUIRE);
|
|
if (!p) {
|
|
void *mine = mem_kernel_alloc(0x1000, 0x1000), *expected = NULL;
|
|
if (__atomic_compare_exchange_n(&page, &expected, mine, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) p = mine;
|
|
else {
|
|
mem_kernel_free(mine, 0x1000);
|
|
p = expected;
|
|
}
|
|
}
|
|
return (uint64_t)(uintptr_t)p;
|
|
}
|
|
|
|
// GetInfo(w1 id, w2 handle, x3 sub id -> x1)
|
|
static void get_info(Cpu *c, uint64_t pc) {
|
|
uint32_t id = (uint32_t)c->x[1];
|
|
uint64_t v = 0;
|
|
if (mem_info(id, &v)) {
|
|
c->x[0] = R_OK;
|
|
c->x[1] = v;
|
|
return;
|
|
}
|
|
switch (id) {
|
|
case 0: v = 0x3f; break; // core mask: cores 0-5 (Switch 2 application cores; see kernel.c NCORE)
|
|
case 1: v = 0x0ffffffff0000000ull; break; // priority mask
|
|
case 8: v = 0; break; // debugger attached
|
|
case 10: v = 0; break; // idle tick count
|
|
case 11: // random entropy
|
|
if (!plat_random(&v, sizeof v)) rt_fatal("GetInfo 11: no random bytes from the system");
|
|
break;
|
|
case 18: v = PROGRAM_ID; break;
|
|
case 20: v = user_exception_context(); break; // the SDK keeps e.g. the dying message at +0x1C0
|
|
case 23: v = 1; break;
|
|
case 29: // region the SDK records at nnosInitialize (address, then
|
|
case 30: v = 0; break;
|
|
case 33: v = 0x1000; break; // VAMM control region size: mapped at 0x300000000 and made into a unit heap
|
|
default: {
|
|
char why[64];
|
|
snprintf(why, sizeof why, "GetInfo id %u (handle %#x, sub id %#llx)", id, (uint32_t)c->x[2],
|
|
(unsigned long long)c->x[3]);
|
|
svc_fatal(c, 0x29, pc, why);
|
|
}
|
|
}
|
|
c->x[0] = R_OK;
|
|
c->x[1] = v;
|
|
}
|
|
|
|
static void svc_body(Cpu *c, uint32_t imm, uint64_t pc);
|
|
|
|
void rt_svc(Cpu *c, uint32_t imm, uint64_t pc) {
|
|
kern_checkpoint();
|
|
if (__builtin_expect(!kern_host_track || !c->thread, 1)) {
|
|
svc_body(c, imm, pc);
|
|
return;
|
|
}
|
|
void *t = c->thread; // kern_host_track: the SVC counted in the thread's host depth (hostfn.c hostfn_tracked)
|
|
int *d = kern_host_depth(t);
|
|
__atomic_store_n(d, *d + 1, __ATOMIC_RELAXED);
|
|
svc_body(c, imm, pc);
|
|
int n = *d - 1;
|
|
__atomic_store_n(d, n, __ATOMIC_RELAXED);
|
|
if (!n && kern_preempt_pending(t)) kern_host_left(t);
|
|
}
|
|
|
|
static void svc_body(Cpu *c, uint32_t imm, uint64_t pc) {
|
|
__atomic_fetch_add(&g_svcs, 1, __ATOMIC_RELAXED);
|
|
if (rt_trace & RT_TRACE_SVC) {
|
|
char at[160];
|
|
rt_log("svc %#x at %s x0=%#llx x1=%#llx x2=%#llx x3=%#llx", imm, ldr_describe(pc, at, sizeof at),
|
|
(unsigned long long)c->x[0], (unsigned long long)c->x[1], (unsigned long long)c->x[2],
|
|
(unsigned long long)c->x[3]);
|
|
}
|
|
if (kern_svc(c, imm)) return;
|
|
uint64_t *x = c->x;
|
|
switch (imm) {
|
|
case 0x15: case 0x1F: case 0x21: case 0x22:
|
|
if (ipc_svc(c, imm)) return;
|
|
svc_fatal(c, imm, pc, "not handled by ipc.c");
|
|
case 0x01: { // SetHeapSize(x1 size -> x1 address)
|
|
uint64_t a = 0;
|
|
uint32_t r = mem_set_heap_size(x[1], &a);
|
|
x[0] = r;
|
|
if (!r) x[1] = a;
|
|
return;
|
|
}
|
|
case 0x02: x[0] = mem_set_permission(x[0], x[1], (uint32_t)x[2]); return;
|
|
case 0x03: x[0] = mem_set_attribute(x[0], x[1], (uint32_t)x[2], (uint32_t)x[3]); return;
|
|
case 0x04: // MapMemory(x0 dst, x1 src, x2 size)
|
|
x[0] = on_main_stack(x[0], x[2]) ? KERN_R_INVALID_STATE_MEM : mem_map(x[0], x[1], x[2]);
|
|
return;
|
|
case 0x05: x[0] = mem_unmap(x[0], x[1], x[2]); return;
|
|
case 0x06: { // QueryMemory(x0 MemoryInfo*, x2 address -> w1 page info)
|
|
RtMemoryInfo mi;
|
|
uint32_t r = mem_query(x[2], &mi);
|
|
main_stack_query(x[2], &mi);
|
|
if (!r && x[0]) memcpy((void *)(uintptr_t)x[0], &mi, sizeof mi);
|
|
x[0] = r;
|
|
x[1] = 0;
|
|
return;
|
|
}
|
|
case 0x2C: x[0] = mem_map_physical(x[0], x[1]); return;
|
|
case 0x2D: x[0] = mem_unmap_physical(x[0], x[1]); return;
|
|
case 0x26: { // Break(x0 reason, x1 info, x2 info size); bit 31 of the reason: notification only
|
|
uint32_t result = 0;
|
|
if (x[1] && x[2] >= 4 && readable(x[1], 4)) memcpy(&result, (const void *)(uintptr_t)x[1], 4);
|
|
char at[160];
|
|
rt_log("guest Break reason %#llx result %#x at %s", (unsigned long long)x[0], result,
|
|
ldr_describe(pc, at, sizeof at));
|
|
if (x[0] & 0x80000000ull) {
|
|
x[0] = R_OK;
|
|
return;
|
|
}
|
|
{ // the SDK keeps the abort text in the user exception context: show any readable run
|
|
const char *ctx = (const char *)(uintptr_t)user_exception_context();
|
|
char run[161];
|
|
int n = 0;
|
|
for (int i = 0; i < 0x400; i++) {
|
|
unsigned char ch = (unsigned char)ctx[i];
|
|
if (ch >= 0x20 && ch < 0x7f) {
|
|
if (n < 160) run[n++] = (char)ch;
|
|
} else if (n >= 4) {
|
|
run[n] = 0;
|
|
rt_log("guest abort text at ctx+%#x: %s", i - n, run);
|
|
n = 0;
|
|
} else {
|
|
n = 0;
|
|
}
|
|
}
|
|
}
|
|
{ // the object the aborting code was working on: its first bytes carry the state machine's byte
|
|
uint64_t obj = c->x[19];
|
|
if (obj && readable(obj, 32)) {
|
|
const unsigned char *p = (const unsigned char *)(uintptr_t)obj;
|
|
rt_log("guest abort object %#llx: %02x %02x %02x %02x %02x %02x %02x %02x "
|
|
"%02x %02x %02x %02x %02x %02x %02x %02x", (unsigned long long)obj,
|
|
p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
|
|
p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
|
|
}
|
|
}
|
|
svc_fatal(c, imm, pc, "Break (the SDK aborted)");
|
|
}
|
|
case 0x27: { // OutputDebugString(x0 text, x1 size)
|
|
uint64_t n = x[1];
|
|
if (n > 65536) svc_fatal(c, imm, pc, "debug string longer than 64 KiB");
|
|
if (n) {
|
|
fflush(stdout);
|
|
rt_log_raw((const char *)(uintptr_t)x[0], n);
|
|
}
|
|
x[0] = R_OK;
|
|
return;
|
|
}
|
|
case 0x29: get_info(c, pc); return;
|
|
case 0x07: { // ExitProcess, as closing the window (gfx_window_closed): pipeline cache, shader records and
|
|
// profile saved, logs flushed, exit code 0
|
|
char at[160];
|
|
rt_log("guest ExitProcess at %s; exiting", ldr_describe(pc, at, sizeof at));
|
|
coop_shutdown(); // LEAVE and BYE to the room, before the process ends
|
|
fs_save_flush();
|
|
vk_pipeline_save();
|
|
prof_write();
|
|
rtss_limit_restore();
|
|
fflush(NULL);
|
|
if (svc_test_exit_hook) return svc_test_exit_hook(0);
|
|
plat_exit(0);
|
|
}
|
|
}
|
|
svc_fatal(c, imm, pc, "not implemented");
|
|
}
|