upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src/main.cpp')
-rw-r--r--runtime/src/main.cpp49
1 files changed, 35 insertions, 14 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp
index 8e3767740..83f5bd70d 100644
--- a/runtime/src/main.cpp
+++ b/runtime/src/main.cpp
@@ -32,24 +32,45 @@
32 32
33namespace { 33namespace {
34 34
35// The retail executable (1.0.2.1) runs a hang watchdog thread (function at RVA 0x2215a60): when the game's main loop 35// The retail title runs a hang watchdog thread: when the game's main loop has not set its "kick" byte for 30
36// has not set its "kick" byte (RVA 0x71b6331) for 30 one-second ticks, it calls a crash callback and then writes to 36// one-second ticks, it calls a crash callback and then writes to address 0 (`movl $0, 0`). Under the runtime the
37// address 0 (`movl $0, 0` at RVA 0x2215c35). Under the runtime the game thread can legitimately stall for longer than 37// game thread can legitimately stall for longer than that, e.g. while the driver compiles pipelines against a cold
38// that, e.g. while the driver compiles pipelines against a cold cache (first boot after a driver update), and the 38// cache (first boot after a driver update), and the title then kills itself. By default the watchdog stays armed, as
39// title then kills itself. By default the watchdog stays armed, as on the console. With LEONIDA_TITLE_WATCHDOG=0, and 39// on the console. The two places are measured locally and not kept in the repository:
40// only when that exact trap instruction sits where it does in this build, a runtime thread sets the kick byte twice a 40// LEONIDA_TITLE_WATCHDOG_KICK="<trap RVA>,<kick RVA>" (hex) names them, and only when that exact trap instruction
41// second so the title's watchdog never fires. 41// sits at the first one does a runtime thread set the byte at the second twice a second, so the title's watchdog
42constexpr std::uint32_t kWatchdogTrapRva = 0x2215c35; 42// never fires. Unset, or with LEONIDA_TITLE_WATCHDOG=1, the watchdog stays armed.
43constexpr std::uint32_t kWatchdogKickRva = 0x71b6331;
44constexpr unsigned char kWatchdogTrap[] = {0xC7, 0x04, 0x25, 0, 0, 0, 0, 0, 0, 0, 0}; // movl $0, 0x0 43constexpr unsigned char kWatchdogTrap[] = {0xC7, 0x04, 0x25, 0, 0, 0, 0, 0, 0, 0, 0}; // movl $0, 0x0
45 44
45bool watchdog_rvas(std::uint32_t& trap_rva, std::uint32_t& kick_rva) {
46 char value[64]{};
47 const DWORD n = GetEnvironmentVariableA("LEONIDA_TITLE_WATCHDOG_KICK", value, sizeof(value));
48 if (!n || n >= sizeof(value)) return false;
49 char* rest = nullptr;
50 const unsigned long long trap = std::strtoull(value, &rest, 16);
51 if (!rest || *rest != ',') return false;
52 const unsigned long long kick = std::strtoull(rest + 1, &rest, 16);
53 if (!rest || *rest || !trap || !kick || trap > 0xFFFFFFFFull || kick > 0xFFFFFFFFull) return false;
54 trap_rva = static_cast<std::uint32_t>(trap);
55 kick_rva = static_cast<std::uint32_t>(kick);
56 return true;
57}
58
46void keep_title_watchdog_kicked(std::uint8_t* base) { 59void keep_title_watchdog_kicked(std::uint8_t* base) {
47 char value[4]{}; 60 char keep[4]{};
48 if (GetEnvironmentVariableA("LEONIDA_TITLE_WATCHDOG", value, sizeof(value)) != 1 || value[0] != '0') return; 61 const DWORD keep_length = GetEnvironmentVariableA("LEONIDA_TITLE_WATCHDOG", keep, sizeof(keep));
62 if (keep_length == 1 && keep[0] == '1') return;
63 std::uint32_t kWatchdogTrapRva = 0, kWatchdogKickRva = 0;
64 if (!watchdog_rvas(kWatchdogTrapRva, kWatchdogKickRva)) {
65 if (keep_length == 1 && keep[0] == '0')
66 std::fprintf(stderr, "[rt] loader: LEONIDA_TITLE_WATCHDOG=0 needs LEONIDA_TITLE_WATCHDOG_KICK=<trap RVA>,"
67 "<kick RVA> (hex); the title's hang watchdog stays armed\n");
68 return;
69 }
49 MEMORY_BASIC_INFORMATION trap{}, kick{}; 70 MEMORY_BASIC_INFORMATION trap{}, kick{};
50 if (!VirtualQuery(base + kWatchdogTrapRva, &trap, sizeof(trap)) || trap.State != MEM_COMMIT || 71 if (!VirtualQuery(base + kWatchdogTrapRva, &trap, sizeof(trap)) || trap.State != MEM_COMMIT ||
51 std::memcmp(base + kWatchdogTrapRva, kWatchdogTrap, sizeof(kWatchdogTrap)) != 0) 72 std::memcmp(base + kWatchdogTrapRva, kWatchdogTrap, sizeof(kWatchdogTrap)) != 0)
52 return; // not the build this was measured on: leave the title's watchdog alone 73 return; // not the trap the RVAs name: leave the title's watchdog alone
53 const DWORD writable = PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY; 74 const DWORD writable = PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY;
54 if (!VirtualQuery(base + kWatchdogKickRva, &kick, sizeof(kick)) || kick.State != MEM_COMMIT || 75 if (!VirtualQuery(base + kWatchdogKickRva, &kick, sizeof(kick)) || kick.State != MEM_COMMIT ||
55 !(kick.Protect & writable)) 76 !(kick.Protect & writable))
@@ -67,11 +88,11 @@ void keep_title_watchdog_kicked(std::uint8_t* base) {
67 const_cast<std::uint8_t*>(byte), 0, nullptr); 88 const_cast<std::uint8_t*>(byte), 0, nullptr);
68 if (thread) { 89 if (thread) {
69 CloseHandle(thread); 90 CloseHandle(thread);
70 std::fprintf(stderr, "[rt] loader: keeping the title's hang watchdog kicked (LEONIDA_TITLE_WATCHDOG=0; unset or 1 keeps it armed)\n"); 91 std::fprintf(stderr, "[rt] loader: keeping the title's hang watchdog kicked (LEONIDA_TITLE_WATCHDOG_KICK)\n");
71 } 92 }
72} 93}
73 94
74// LEONIDA_PEEK="0x9e1dac0:3,0x7446510,0x98f828c" (diagnostic): a thread prints the 8-byte values at these RVAs of the 95// LEONIDA_PEEK="<rva>:3,<rva>,<rva>" (diagnostic): a thread prints the 8-byte values at these RVAs of the
75// title's image (":n" = n consecutive values) every LEONIDA_PEEK_SECONDS (default 10). For answering "what does this 96// title's image (":n" = n consecutive values) every LEONIDA_PEEK_SECONDS (default 10). For answering "what does this
76// global hold at run time" without a debugger: the reverse-engineering notes (docs/re/requests.md) ask for exactly 97// global hold at run time" without a debugger: the reverse-engineering notes (docs/re/requests.md) ask for exactly
77// that. Reads are checked; nothing is written. 98// that. Reads are checked; nothing is written.