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.cpp141
1 files changed, 124 insertions, 17 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp
index 094aaa321..21551e948 100644
--- a/runtime/src/main.cpp
+++ b/runtime/src/main.cpp
@@ -9,6 +9,13 @@
9// gta6rt --init <image> [--game-dir DIR] [--overlay DIR] 9// gta6rt --init <image> [--game-dir DIR] [--overlay DIR]
10// The same mapping, then each loaded DLL's TLS callbacks and DllMain(DLL_PROCESS_ATTACH) are run. 10// The same mapping, then each loaded DLL's TLS callbacks and DllMain(DLL_PROCESS_ATTACH) are run.
11// 11//
12// gta6rt --precompile-pipelines [--threads N] [--big-threads N] [--big-bytes N] [--all] [--smallest-first]
13// [--limit N] [--min-bytes N] [--max-bytes N] [--filter PART]... [--list]
14// No game: creates the Vulkan device as --run does and builds every compute pipeline of the shader library
15// (LEONIDA_SHADERS) into the pipeline journal and the driver's cache, so a title's first dispatch of a large one
16// does not stall it. Resumable: entries already journaled are skipped (--all: built anyway). See
17// shims/d3d12_precompile.h.
18//
12// gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--trace-d3d12] [--trace FILE] [-- game arguments...] 19// gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--trace-d3d12] [--trace FILE] [-- game arguments...]
13// The same, then the EXE's own TLS callbacks and its entry point, on a game thread. The game's argv and 20// The same, then the EXE's own TLS callbacks and its entry point, on a game thread. The game's argv and
14// command line are <exe name> plus the arguments after "--"; exit/ExitProcess from the game end the run 21// command line are <exe name> plus the arguments after "--"; exit/ExitProcess from the game end the run
@@ -33,24 +40,51 @@
33 40
34namespace { 41namespace {
35 42
36// The retail executable (1.0.2.1) runs a hang watchdog thread (function at RVA 0x2215a60): when the game's main loop 43// The retail title runs a hang watchdog thread: when the game's main loop has not set its "kick" byte for 30
37// has not set its "kick" byte (RVA 0x71b6331) for 30 one-second ticks, it calls a crash callback and then writes to 44// one-second ticks, it calls a crash callback and then writes to address 0 (`movl $0, 0`). Under the runtime the
38// address 0 (`movl $0, 0` at RVA 0x2215c35). Under the runtime the game thread can legitimately stall for longer than 45// game thread can legitimately stall for longer than that, e.g. while the driver compiles pipelines against a cold
39// that, e.g. while the driver compiles pipelines against a cold cache (first boot after a driver update), and the 46// cache (first boot after a driver update), and the title then kills itself. By default the watchdog stays armed, as
40// title then kills itself. By default the watchdog stays armed, as on the console. With LEONIDA_TITLE_WATCHDOG=0, and 47// on the console. LEONIDA_TITLE_WATCHDOG=0 asks to keep it from firing, as before. The two places that takes are
41// only when that exact trap instruction sits where it does in this build, a runtime thread sets the kick byte twice a 48// measured locally and not kept in the repository: LEONIDA_TITLE_WATCHDOG_KICK="<trap RVA>,<kick RVA>" (hex) names
42// second so the title's watchdog never fires. 49// them, and only with both variables, and only when that exact trap instruction sits at the first RVA, does a
43constexpr std::uint32_t kWatchdogTrapRva = 0x2215c35; 50// runtime thread set the byte at the second twice a second, so the title's watchdog never fires. Either variable
44constexpr std::uint32_t kWatchdogKickRva = 0x71b6331; 51// alone leaves the watchdog armed and says so once.
45constexpr unsigned char kWatchdogTrap[] = {0xC7, 0x04, 0x25, 0, 0, 0, 0, 0, 0, 0, 0}; // movl $0, 0x0 52constexpr unsigned char kWatchdogTrap[] = {0xC7, 0x04, 0x25, 0, 0, 0, 0, 0, 0, 0, 0}; // movl $0, 0x0
46 53
54bool watchdog_rvas(std::uint32_t& trap_rva, std::uint32_t& kick_rva) {
55 char value[64]{};
56 const DWORD n = GetEnvironmentVariableA("LEONIDA_TITLE_WATCHDOG_KICK", value, sizeof(value));
57 if (!n || n >= sizeof(value)) return false;
58 char* rest = nullptr;
59 const unsigned long long trap = std::strtoull(value, &rest, 16);
60 if (!rest || *rest != ',') return false;
61 const unsigned long long kick = std::strtoull(rest + 1, &rest, 16);
62 if (!rest || *rest || !trap || !kick || trap > 0xFFFFFFFFull || kick > 0xFFFFFFFFull) return false;
63 trap_rva = static_cast<std::uint32_t>(trap);
64 kick_rva = static_cast<std::uint32_t>(kick);
65 return true;
66}
67
47void keep_title_watchdog_kicked(std::uint8_t* base) { 68void keep_title_watchdog_kicked(std::uint8_t* base) {
48 char value[4]{}; 69 char keep[4]{};
49 if (GetEnvironmentVariableA("LEONIDA_TITLE_WATCHDOG", value, sizeof(value)) != 1 || value[0] != '0') return; 70 const bool disarm = GetEnvironmentVariableA("LEONIDA_TITLE_WATCHDOG", keep, sizeof(keep)) == 1 && keep[0] == '0';
71 std::uint32_t kWatchdogTrapRva = 0, kWatchdogKickRva = 0;
72 const bool named = watchdog_rvas(kWatchdogTrapRva, kWatchdogKickRva);
73 if (!disarm) {
74 if (named)
75 std::fprintf(stderr, "[rt] loader: LEONIDA_TITLE_WATCHDOG_KICK is set but LEONIDA_TITLE_WATCHDOG is not 0;"
76 " the title's hang watchdog stays armed\n");
77 return;
78 }
79 if (!named) {
80 std::fprintf(stderr, "[rt] loader: LEONIDA_TITLE_WATCHDOG=0 needs LEONIDA_TITLE_WATCHDOG_KICK=<trap RVA>,"
81 "<kick RVA> (hex); the title's hang watchdog stays armed\n");
82 return;
83 }
50 MEMORY_BASIC_INFORMATION trap{}, kick{}; 84 MEMORY_BASIC_INFORMATION trap{}, kick{};
51 if (!VirtualQuery(base + kWatchdogTrapRva, &trap, sizeof(trap)) || trap.State != MEM_COMMIT || 85 if (!VirtualQuery(base + kWatchdogTrapRva, &trap, sizeof(trap)) || trap.State != MEM_COMMIT ||
52 std::memcmp(base + kWatchdogTrapRva, kWatchdogTrap, sizeof(kWatchdogTrap)) != 0) 86 std::memcmp(base + kWatchdogTrapRva, kWatchdogTrap, sizeof(kWatchdogTrap)) != 0)
53 return; // not the build this was measured on: leave the title's watchdog alone 87 return; // not the trap the RVAs name: leave the title's watchdog alone
54 const DWORD writable = PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY; 88 const DWORD writable = PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY;
55 if (!VirtualQuery(base + kWatchdogKickRva, &kick, sizeof(kick)) || kick.State != MEM_COMMIT || 89 if (!VirtualQuery(base + kWatchdogKickRva, &kick, sizeof(kick)) || kick.State != MEM_COMMIT ||
56 !(kick.Protect & writable)) 90 !(kick.Protect & writable))
@@ -72,7 +106,7 @@ void keep_title_watchdog_kicked(std::uint8_t* base) {
72 } 106 }
73} 107}
74 108
75// LEONIDA_PEEK="0x9e1dac0:3,0x7446510,0x98f828c" (diagnostic): a thread prints the 8-byte values at these RVAs of the 109// LEONIDA_PEEK="<rva>:3,<rva>,<rva>" (diagnostic): a thread prints the 8-byte values at these RVAs of the
76// title's image (":n" = n consecutive values, at most 32; "RVA>OFF" reads through the pointer stored at the RVA, at 110// title's image (":n" = n consecutive values, at most 32; "RVA>OFF" reads through the pointer stored at the RVA, at
77// pointer + OFF, both hex) every LEONIDA_PEEK_SECONDS (default 10). For answering "what does this 111// pointer + OFF, both hex) every LEONIDA_PEEK_SECONDS (default 10). For answering "what does this
78// global hold at run time" without a debugger: the reverse-engineering notes (docs/re/requests.md) ask for exactly 112// global hold at run time" without a debugger: the reverse-engineering notes (docs/re/requests.md) ask for exactly
@@ -149,6 +183,7 @@ void start_title_peek(std::uint8_t* base) {
149#include "mods.h" 183#include "mods.h"
150#include "pe_loader.h" 184#include "pe_loader.h"
151#include "thread_stacks.h" 185#include "thread_stacks.h"
186#include "dev_overlay.h"
152#include "thread_sampler.h" 187#include "thread_sampler.h"
153#include "title_page_watch.h" 188#include "title_page_watch.h"
154#include "title_geometry_watch.h" 189#include "title_geometry_watch.h"
@@ -156,7 +191,9 @@ void start_title_peek(std::uint8_t* base) {
156#include "../shims/d3d12_xs_device.h" 191#include "../shims/d3d12_xs_device.h"
157#include "../shims/file_system.h" 192#include "../shims/file_system.h"
158#ifdef LEONIDA_VULKAN_DEVICE 193#ifdef LEONIDA_VULKAN_DEVICE
194#include "vulkan_device_fault.h"
159#include "../shims/d3d12_compile_exit.h" 195#include "../shims/d3d12_compile_exit.h"
196#include "../shims/d3d12_precompile.h"
160#include "../shims/package_splash.h" 197#include "../shims/package_splash.h"
161#endif 198#endif
162#include "../shims/process_shim.h" 199#include "../shims/process_shim.h"
@@ -224,10 +261,66 @@ int usage() {
224 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace FILE]\n" 261 "usage: gta6rt --dry-run <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace FILE]\n"
225 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE]\n" 262 " gta6rt --init <image> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE]\n"
226 " gta6rt --first-run-ui <exe> [--game-dir DIR] [--overlay DIR] [--trace FILE]\n" 263 " gta6rt --first-run-ui <exe> [--game-dir DIR] [--overlay DIR] [--trace FILE]\n"
227 " gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n"); 264 " gta6rt --run <exe> [--game-dir DIR] [--overlay DIR] [--mods DIR] [--archetype-lookup RVA[,ARG]] [--view-mode-getter RVA] [--trace-d3d12] [--trace FILE] [-- game arguments...]\n"
265 " gta6rt --precompile-pipelines [--threads N] [--big-threads N] [--big-bytes N] [--all] [--smallest-first] [--limit N] [--min-bytes N] [--max-bytes N] [--filter PART]... [--list] [--via-helper]\n"
266 " gta6rt --compile-server [--parent PID] [--threads N] [--idle-threads N] [--idle-exit SECONDS]\n");
228 return 2; 267 return 2;
229} 268}
230 269
270#ifdef LEONIDA_VULKAN_DEVICE
271// gta6rt --precompile-pipelines ...: argv[1] is the mode.
272int precompile_main(int argc, wchar_t** argv) {
273 rt::d3d12::PrecompileOptions options;
274 const auto number = [&](int& i, auto& out) {
275 if (i + 1 >= argc) return false;
276 wchar_t* end = nullptr;
277 const unsigned long long v = std::wcstoull(argv[++i], &end, 10);
278 if (!end || *end) return false;
279 out = static_cast<std::remove_reference_t<decltype(out)>>(v);
280 return true;
281 };
282 for (int i = 2; i < argc; ++i) {
283 const std::wstring a = argv[i];
284 bool ok = true;
285 if (a == L"--threads") ok = number(i, options.threads);
286 else if (a == L"--big-threads") ok = number(i, options.big_threads);
287 else if (a == L"--big-bytes") ok = number(i, options.big_bytes);
288 else if (a == L"--limit") ok = number(i, options.limit);
289 else if (a == L"--min-bytes") ok = number(i, options.min_bytes);
290 else if (a == L"--max-bytes") ok = number(i, options.max_bytes);
291 else if (a == L"--all") options.rebuild = true;
292 else if (a == L"--smallest-first") options.smallest_first = true;
293 else if (a == L"--list") options.dry_run = true;
294 else if (a == L"--via-helper") options.via_helper = true;
295 else if (a == L"--filter" && i + 1 < argc) {
296 const std::wstring part = argv[++i];
297 options.filters.emplace_back(part.begin(), part.end()); // library names are ASCII
298 } else ok = false;
299 if (!ok) return usage();
300 }
301 return rt::d3d12::precompile_compute_pipelines(options);
302}
303
304// gta6rt --compile-server [--parent PID] [--threads N] [--idle-threads N] [--idle-exit SECONDS]
305// (shims/d3d12_compile_helper.h).
306int compile_server_main(int argc, wchar_t** argv) {
307 rt::d3d12::CompileServerOptions options;
308 for (int i = 2; i < argc; ++i) {
309 const std::wstring a = argv[i];
310 if (i + 1 >= argc) return usage();
311 wchar_t* end = nullptr;
312 const unsigned long long v = std::wcstoull(argv[++i], &end, 10);
313 if (!end || *end) return usage();
314 if (a == L"--parent") options.parent_pid = static_cast<unsigned long>(v);
315 else if (a == L"--threads") options.threads = static_cast<unsigned>(v);
316 else if (a == L"--idle-threads") options.idle_threads = static_cast<unsigned>(v);
317 else if (a == L"--idle-exit") options.idle_exit_s = static_cast<unsigned>(v);
318 else return usage();
319 }
320 return rt::d3d12::compile_server(options);
321}
322#endif
323
231} // namespace 324} // namespace
232 325
233#ifdef LEONIDA_VULKAN_DEVICE 326#ifdef LEONIDA_VULKAN_DEVICE
@@ -292,6 +385,10 @@ static BOOL WINAPI show_package_window(HWND window, int command) {
292#endif 385#endif
293 386
294int wmain(int argc, wchar_t** argv) { 387int wmain(int argc, wchar_t** argv) {
388#ifdef LEONIDA_VULKAN_DEVICE
389 if (argc > 1 && std::wstring(argv[1]) == L"--precompile-pipelines") return precompile_main(argc, argv);
390 if (argc > 1 && std::wstring(argv[1]) == L"--compile-server") return compile_server_main(argc, argv);
391#endif
295 std::filesystem::path image, game_dir, overlay_dir, mods_dir; 392 std::filesystem::path image, game_dir, overlay_dir, mods_dir;
296 uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash 393 uint32_t archetype_lookup_rva = 0; // --archetype-lookup RVA[,ARG]: the game's archetype lookup by hash
297 int archetype_lookup_arg = 0; 394 int archetype_lookup_arg = 0;
@@ -436,6 +533,8 @@ int wmain(int argc, wchar_t** argv) {
436 rt::d3d12_trace::set_enabled(trace_d3d12); 533 rt::d3d12_trace::set_enabled(trace_d3d12);
437 // Slot 133 imports the title's pages; xmem commits them on first touch, so they are committed first. 534 // Slot 133 imports the title's pages; xmem commits them on first touch, so they are committed first.
438 rt::d3d12_xs::set_placed_at_va_pages_hook(&rt::xmem::commit_physical_range); 535 rt::d3d12_xs::set_placed_at_va_pages_hook(&rt::xmem::commit_physical_range);
536 if (rt::xmem::slot31_runs() && rt::xmem::queue_page_mappings_enabled())
537 rt::d3d12_xs::set_copy_page_mappings_hook(&rt::xmem::copy_page_mappings);
439 // So do the file shims' reads, whose kernel writes do not fault them in: on ERROR_NOACCESS (title_page_watch.h). 538 // So do the file shims' reads, whose kernel writes do not fault them in: on ERROR_NOACCESS (title_page_watch.h).
440 leonida::geometry_writes::aliases.store(&rt::xmem::geometry_aliases); 539 leonida::geometry_writes::aliases.store(&rt::xmem::geometry_aliases);
441 leonida::kernel_writes::set_committer(&rt::xmem::commit_physical_range); 540 leonida::kernel_writes::set_committer(&rt::xmem::commit_physical_range);
@@ -541,7 +640,9 @@ int wmain(int argc, wchar_t** argv) {
541 std::fprintf(stderr, "error: %s has no entry point\n", main_module.name.c_str()); 640 std::fprintf(stderr, "error: %s has no entry point\n", main_module.name.c_str());
542 return 1; 641 return 1;
543 } 642 }
544 const std::wstring paths = L"console paths: GAME: -> " + game_dir.native() + L", writes -> " + overlay_dir.native(); 643 const std::filesystem::path scratch_dir = rt::fs::scratch_dir();
644 const std::wstring paths = L"console paths: GAME: -> " + game_dir.native() + L", writes -> " +
645 overlay_dir.native() + L", T: -> " + scratch_dir.native();
545 const HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); 646 const HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
546 DWORD mode; 647 DWORD mode;
547 bool printed = false; 648 bool printed = false;
@@ -554,19 +655,25 @@ int wmain(int argc, wchar_t** argv) {
554 if (!printed) { 655 if (!printed) {
555 const auto game = game_dir.u8string(); 656 const auto game = game_dir.u8string();
556 const auto overlay = overlay_dir.u8string(); 657 const auto overlay = overlay_dir.u8string();
557 std::printf("console paths: GAME: -> %s, writes -> %s\n", 658 const auto scratch = scratch_dir.u8string();
558 reinterpret_cast<const char*>(game.c_str()), reinterpret_cast<const char*>(overlay.c_str())); 659 std::printf("console paths: GAME: -> %s, writes -> %s, T: -> %s\n",
660 reinterpret_cast<const char*>(game.c_str()), reinterpret_cast<const char*>(overlay.c_str()),
661 reinterpret_cast<const char*>(scratch.c_str()));
559 } 662 }
560 std::printf("running %s\n", main_module.name.c_str()); 663 std::printf("running %s\n", main_module.name.c_str());
561 std::fflush(stdout); 664 std::fflush(stdout);
562 keep_title_watchdog_kicked(main_module.base); 665 keep_title_watchdog_kicked(main_module.base);
563 start_title_peek(main_module.base); 666 start_title_peek(main_module.base);
667 leonida::dev_overlay::start(); // LEONIDA_DEV_OVERLAY=1
564 leonida::thread_sampler::start(main_module.base, main_module.size); // LEONIDA_SAMPLE_MS (diagnostic) 668 leonida::thread_sampler::start(main_module.base, main_module.size); // LEONIDA_SAMPLE_MS (diagnostic)
565#ifdef LEONIDA_VULKAN_DEVICE 669#ifdef LEONIDA_VULKAN_DEVICE
566 SetConsoleCtrlHandler(&stop_compiles_on_console_exit, TRUE); 670 SetConsoleCtrlHandler(&stop_compiles_on_console_exit, TRUE);
567 previous_fatal_filter = SetUnhandledExceptionFilter(&save_caches_on_fatal_exception); 671 previous_fatal_filter = SetUnhandledExceptionFilter(&save_caches_on_fatal_exception);
568#endif 672#endif
569 int code; 673 int code;
674#ifdef LEONIDA_VULKAN_DEVICE
675 leonida::thread_stack_hook.store(&rt::print_thread_stacks_now, std::memory_order_release);
676#endif
570 { 677 {
571 auto stacks = rt::start_thread_stacks(); 678 auto stacks = rt::start_thread_stacks();
572 code = rt::process::run_entry(main_module.base + main_module.entry_rva); 679 code = rt::process::run_entry(main_module.base + main_module.entry_rva);