foxygit / fake-08-xbox360 Log in
commit e8d1ec83c5a880bd1ab2685e4e4fabe58fad8d03
Author:     mrfox <jens.se@icloud.com>
AuthorDate: Wed Nov 25 16:54:16 2026 +0100
Commit:     mrfox <jens.se@icloud.com>
CommitDate: Wed Nov 25 16:54:16 2026 +0100

    xex: fault report in game:/crash.txt; log folder fallback

    A vectored exception handler (RtlSetVectoredExceptionHandler, found by
    xsdk) writes the exception, faulting address, registers and stack return
    addresses to game:/crash.txt, then ends the title. FAKE-08 keeps its data
    on hdd: or cache: when the XEX's folder is read-only, so the log path also
    runs in Xenia.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 platform/xex/Makefile                |   2 +-
 platform/xex/README.md               |   9 ++-
 platform/xex/runtime/crt.c           |   2 +
 platform/xex/runtime/extra_imports.S |   5 ++
 platform/xex/runtime/fault.c         | 139 +++++++++++++++++++++++++++++++++++
 platform/xex/source/XexHost.cpp      |  13 +++-
 6 files changed, 167 insertions(+), 3 deletions(-)

diff --git a/platform/xex/Makefile b/platform/xex/Makefile
index c69749f..f56985f 100644
--- a/platform/xex/Makefile
+++ b/platform/xex/Makefile
@@ -25,7 +25,7 @@ LDFLAGS  := $(MACH) -nostartfiles -nostdlib -T $(CURDIR)/runtime/link.ld -Wl,--g
 LIBS     := -L$(DEVKITXENON)/xenon/lib/32 -Wl,--start-group -lstdc++ -lm -lc -lgcc -Wl,--end-group

 RUNTIME_SRC := runtime/crt0.S runtime/crt.c runtime/sys.c runtime/fs.c runtime/posix.c \
-               runtime/video.c runtime/input.c \
+               runtime/video.c runtime/input.c runtime/fault.c \
                runtime/extra_imports.S sdk/thunks/ppc32.S
 RUNTIME_OBJ := $(patsubst %,$(BUILD)/%.o,$(RUNTIME_SRC))

diff --git a/platform/xex/README.md b/platform/xex/README.md
index 2178aa8..c5d773c 100644
--- a/platform/xex/README.md
+++ b/platform/xex/README.md
@@ -48,7 +48,8 @@ Copy `build/fake08/default.xex` into a folder of its own (for example
 first `p8carts` folder found: next to the XEX, then `usb0:`-`usb2:`, then
 the hard disk root (`Hdd1:\p8carts`); without one, `p8carts` is created
 next to the XEX. Settings, saves (`cdata`) and `pico.log` go to a `fake08`
-folder next to the XEX.
+folder next to the XEX (or `hdd:\fake08`, or `cache:` in Xenia, when that
+folder is read-only).

 | Controller | |
 |---|---|
@@ -80,6 +81,12 @@ The source is `source/XexHost.cpp`, the same logic as the libxenon host
 - **Heap:** a 96 MB `.bss` arena (`XEX_HEAP_SIZE`).
 - **Exit:** returning from `main`/`exit()` calls `XamLoaderTerminateTitle`,
   which goes back to the dashboard.
+- **Crashes:** a CPU fault (bad pointer, illegal instruction) writes
+  `game:/crash.txt` with the exception, the faulting address, the registers
+  and the return addresses on the stack, then goes back to the dashboard
+  (`RtlSetVectoredExceptionHandler`, as found by xsdk). Look addresses up
+  with `xenon-addr2line -f -C -e build/fake08/app.elf <address>`. A stack
+  overflow can't be reported.
 - `xex_runtime.h`: `xex_notify()` (guide notification), timing helpers.
 - **Video** (`xex_video.h`): takes the GPU engines like Direct3D, flips two
   tiled 32 bpp front buffers with `VdSwap` at vertical blank. On hardware a
diff --git a/platform/xex/runtime/crt.c b/platform/xex/runtime/crt.c
index 92ff060..cc7b830 100644
--- a/platform/xex/runtime/crt.c
+++ b/platform/xex/runtime/crt.c
@@ -14,12 +14,14 @@ extern char __eh_frame_start[];

 extern void __register_frame(void *);
 extern void xex_mount_devices(void);
+extern void xex_install_fault_handler(void);
 extern int main(int argc, char **argv);

 static char *argv_empty[] = { "game:\\default.xex", 0 };

 int __xex_crt_main(void)
 {
+    xex_install_fault_handler();   /* before constructors, so they're covered */
     __register_frame(__eh_frame_start);
     xex_mount_devices();

diff --git a/platform/xex/runtime/extra_imports.S b/platform/xex/runtime/extra_imports.S
index 4d30aa8..d597712 100644
--- a/platform/xex/runtime/extra_imports.S
+++ b/platform/xex/runtime/extra_imports.S
@@ -50,3 +50,8 @@ xbox360_stub VdQueryVideoMode, 0x010001ca
 /* VOID VdCallGraphicsNotificationRoutines(ULONG Event, PVOID Data) - xboxkrnl 0x1B1.
  * Event 1 with 64 bytes describes the title's display to XAM (as D3D does). */
 xbox360_stub VdCallGraphicsNotificationRoutines, 0x010001b1
+
+/* RtlSetVectoredExceptionHandler(handler, handler) - xboxkrnl 0x36D: the
+ * kernel calls the handler on the faulting thread with a pointer to the
+ * exception record and register context (found by xsdk on hardware). */
+xbox360_stub RtlSetVectoredExceptionHandler, 0x0100036d
diff --git a/platform/xex/runtime/fault.c b/platform/xex/runtime/fault.c
new file mode 100644
index 0000000..c4f12f9
--- /dev/null
+++ b/platform/xex/runtime/fault.c
@@ -0,0 +1,139 @@
+/*
+ * Fault report: a CPU exception in the title (bad pointer, illegal
+ * instruction, ...) writes game:/crash.txt (and DbgPrint) with the
+ * exception code, the faulting instruction, the registers and the return
+ * addresses along the stack, then ends the title back to the dashboard.
+ * Look the addresses up with `xenon-addr2line -f -C -e app.elf <addr>`.
+ *
+ * How the kernel hands a title its faults was found on hardware by xsdk
+ * (https://github.com/jessenic/xsdk, MIT; docs/HARDWARE.md, "How the kernel
+ * hands a title its faults"): RtlSetVectoredExceptionHandler (0x36D) with
+ * the handler as both arguments; the handler gets a pointer to the
+ * exception record and the register context. A stack overflow can't be
+ * caught (the context goes on the overflowed stack).
+ */
+#include <stdint.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "xex_runtime.h"
+
+extern unsigned long RtlSetVectoredExceptionHandler(void *first, void *handler);
+extern void XamLoaderTerminateTitle(void) __attribute__((noreturn));
+extern unsigned long DbgPrint(const char *format, ...);
+
+/* X_EXCEPTION_RECORD (xenia src/xenia/xbox.h) */
+typedef struct {
+    uint32_t code;
+    uint32_t flags;
+    uint32_t nested;
+    uint32_t address;
+    uint32_t parameter_count;
+    uint32_t parameters[15];
+} exception_record;
+
+typedef struct {
+    exception_record *record;
+    uint8_t *context;
+} exception_pointers;
+
+/* Register context offsets (xsdk's probe) */
+#define CTX_IAR 0x008
+#define CTX_LR  0x00c
+#define CTX_CTR 0x010
+#define CTX_GPR 0x018   /* r0-r31, 64 bits each */
+#define CTX_CR  0x118
+#define CTX_XER 0x11c
+
+#define CODE_START 0x82000000u
+#define CODE_END   0x84000000u
+
+static int report_fd = -1;
+static char line[256];
+
+static void out(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
+static void out(const char *fmt, ...)
+{
+    va_list ap;
+    va_start(ap, fmt);
+    int n = vsnprintf(line, sizeof(line), fmt, ap);
+    va_end(ap);
+    if (n < 0) return;
+    if (n >= (int)sizeof(line)) n = sizeof(line) - 1;
+    DbgPrint("%s", line);
+    if (report_fd >= 0) write(report_fd, line, n);
+}
+
+static uint32_t ctx32(const uint8_t *ctx, unsigned offset)
+{
+    uint32_t v;
+    memcpy(&v, ctx + offset, 4);
+    return v;
+}
+
+/* low word of a 64-bit register */
+static uint32_t gpr(const uint8_t *ctx, int n)
+{
+    return ctx32(ctx, CTX_GPR + n * 8 + 4);
+}
+
+static const char *code_name(uint32_t code)
+{
+    switch (code) {
+        case 0xc0000005: return "access violation";
+        case 0x80000002: return "misaligned data";
+        case 0xc000001d: return "illegal instruction";
+        case 0xc0000096: return "privileged instruction";
+        case 0xc00000fd: return "stack overflow";
+        case 0xc0000094: return "integer divide by zero";
+        default: return "exception";
+    }
+}
+
+static long fault_handler(exception_pointers *pointers)
+{
+    static int inside;
+    if (inside) XamLoaderTerminateTitle();   /* a fault while reporting */
+    inside = 1;
+
+    exception_record *rec = pointers->record;
+    const uint8_t *ctx = pointers->context;
+
+    report_fd = open("game:/crash.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
+    out("FAKE-08 XEX crash: %s (code %08x) at %08x\n", code_name(rec->code), (unsigned)rec->code,
+        (unsigned)rec->address);
+    if (rec->code == 0xc0000005 && rec->parameter_count >= 2)
+        out("  %s of address %08x\n", rec->parameters[0] ? "write" : "read", (unsigned)rec->parameters[1]);
+    else if (rec->parameter_count >= 1)
+        out("  parameter %08x\n", (unsigned)rec->parameters[0]);
+    out("IAR %08x LR %08x CTR %08x CR %08x XER %08x\n", (unsigned)ctx32(ctx, CTX_IAR),
+        (unsigned)ctx32(ctx, CTX_LR), (unsigned)ctx32(ctx, CTX_CTR + 4), (unsigned)ctx32(ctx, CTX_CR),
+        (unsigned)ctx32(ctx, CTX_XER));
+    for (int r = 0; r < 32; r += 4)
+        out("r%-2d %08x %08x %08x %08x\n", r, (unsigned)gpr(ctx, r), (unsigned)gpr(ctx, r + 1),
+            (unsigned)gpr(ctx, r + 2), (unsigned)gpr(ctx, r + 3));
+
+    /* Return addresses: SysV frames keep LR 4 bytes into the caller's frame. */
+    out("stack:\n");
+    uint32_t sp = gpr(ctx, 1);
+    for (int depth = 0; depth < 24 && sp && (sp & 7) == 0; depth++) {
+        uint32_t caller = *(volatile uint32_t *)sp;
+        if (caller <= sp || caller - sp > 0x100000) break;
+        uint32_t ret = *(volatile uint32_t *)(caller + 4);
+        if (ret >= CODE_START && ret < CODE_END) out("  %08x\n", (unsigned)ret);
+        sp = caller;
+    }
+    if (report_fd >= 0) close(report_fd);
+
+    xex_notify("FAKE-08 crashed: see crash.txt");
+    xex_sleep_ms(3000);
+    XamLoaderTerminateTitle();
+}
+
+void xex_install_fault_handler(void)
+{
+    RtlSetVectoredExceptionHandler((void *)fault_handler, (void *)fault_handler);
+}
diff --git a/platform/xex/source/XexHost.cpp b/platform/xex/source/XexHost.cpp
index 704143e..3367103 100644
--- a/platform/xex/source/XexHost.cpp
+++ b/platform/xex/source/XexHost.cpp
@@ -274,8 +274,19 @@ static void logTerminate() {

 Host::Host(int windowWidth, int windowHeight) {
     std::set_terminate(logTerminate);
+    // Settings, saves and the log next to the XEX; the hard disk when that
+    // folder can't be written (read-only media; Xenia: cache:).
     std::string base = "game:/fake08";
-    mkdir(base.c_str(), 0777);
+    for (const char *candidate : {"game:/fake08", "hdd:/fake08", "cache:/fake08"}) {
+        mkdir(candidate, 0777);
+        std::string probe = std::string(candidate) + "/.write-test";
+        if (FILE *f = fopen(probe.c_str(), "w")) {
+            fclose(f);
+            remove(probe.c_str());
+            base = candidate;
+            break;
+        }
+    }
     mkdir((base + "/cdata").c_str(), 0777);

     hasControllerSettings = true;