upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-23 17:29:12 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-23 17:29:12 -0500
commitf7b3afcda95b5689a57e17e30acbd77148b104d1 (patch)
treea99c4d12b8a4645ff1183c02d0e9b9b36816c3d9 /runtime/src
parentdb1d454ce7d7051102417c1a5ce8b19d0599fcb1 (diff)
parentad6e5d6bbee5bd8c3c8a7e95216f12038fe3db22 (diff)
Merge remote-tracking branch 'origin/pr/9754'
# Conflicts: # scripts/build_runtime.ps1
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/host_cpu.cpp80
-rw-r--r--runtime/src/host_cpu.h38
-rw-r--r--runtime/src/main.cpp7
3 files changed, 125 insertions, 0 deletions
diff --git a/runtime/src/host_cpu.cpp b/runtime/src/host_cpu.cpp
new file mode 100644
index 000000000..a49525a3e
--- /dev/null
+++ b/runtime/src/host_cpu.cpp
@@ -0,0 +1,80 @@
1#include "host_cpu.h"
2
3#include <cstring>
4
5#if defined(_WIN32)
6#include <windows.h>
7#else
8#include <cstdlib>
9#endif
10
11#if defined(_M_X64) || defined(__x86_64__)
12#if defined(_MSC_VER)
13#include <intrin.h>
14#else
15#include <cpuid.h>
16#endif
17#endif
18
19namespace rt::host_cpu {
20
21std::vector<const char*> missing_host_features(uint32_t cpuid1_ecx, uint64_t (*read_xcr0)()) {
22 std::vector<const char*> missing;
23 if (!(cpuid1_ecx & kCpuid1EcxAvx)) missing.push_back(kMissingAvx);
24 if (!(cpuid1_ecx & kCpuid1EcxOsxsave))
25 missing.push_back(kMissingOsxsave); // XGETBV is #UD without OSXSAVE: XCR0 is not read
26 else if ((read_xcr0() & kXcr0SseYmm) != kXcr0SseYmm)
27 missing.push_back(kMissingYmmState);
28 return missing;
29}
30
31#if defined(_M_X64) || defined(__x86_64__)
32namespace {
33
34uint32_t cpuid1_ecx() {
35#if defined(_MSC_VER)
36 int r[4] = {};
37 __cpuid(r, 1);
38 return static_cast<uint32_t>(r[2]);
39#else
40 unsigned a = 0, b = 0, c = 0, d = 0;
41 return __get_cpuid(1, &a, &b, &c, &d) ? c : 0;
42#endif
43}
44
45uint64_t xcr0() {
46#if defined(_MSC_VER)
47 return _xgetbv(0);
48#else
49 uint32_t lo = 0, hi = 0;
50 __asm__ volatile("xgetbv" : "=a"(lo), "=d"(hi) : "c"(0));
51 return (static_cast<uint64_t>(hi) << 32) | lo;
52#endif
53}
54
55} // namespace
56
57std::vector<const char*> missing_host_features() { return missing_host_features(cpuid1_ecx(), &xcr0); }
58#else
59std::vector<const char*> missing_host_features() { return {}; }
60#endif
61
62std::string message(const std::vector<const char*>& missing) {
63 std::string names;
64 for (const char* m : missing) names += (names.empty() ? "" : ", ") + std::string(m);
65 return "error: the game's code needs AVX, and this CPU or OS lacks " + names +
66 "; in a virtual machine, pass the host CPU through (for example QEMU -cpu host); under Rosetta on "
67 "macOS, set ROSETTA_ADVERTISE_AVX=1. LEONIDA_SKIP_CPU_CHECK=1 skips this check";
68}
69
70bool check_skipped() {
71#if defined(_WIN32)
72 char v[4] = {};
73 return GetEnvironmentVariableA("LEONIDA_SKIP_CPU_CHECK", v, sizeof v) == 1 && v[0] == '1';
74#else
75 const char* v = std::getenv("LEONIDA_SKIP_CPU_CHECK");
76 return v && std::strcmp(v, "1") == 0;
77#endif
78}
79
80} // namespace rt::host_cpu
diff --git a/runtime/src/host_cpu.h b/runtime/src/host_cpu.h
new file mode 100644
index 000000000..b9dd143db
--- /dev/null
+++ b/runtime/src/host_cpu.h
@@ -0,0 +1,38 @@
1// Host CPU preflight for --init and --run: the game executes AVX instructions from its TLS callbacks on, so a host
2// whose CPU or OS lacks AVX (a QEMU qemu64 guest, for one) dies there with a bare EXCEPTION_ILLEGAL_INSTRUCTION
3// (0xC000001D) that reads like a loader bug. This checks AVX only, which is what was measured; it claims nothing
4// about AVX2, FMA or BMI2. No emulation here: that is RT-500's scope. Rosetta on macOS runs AVX code but leaves
5// AVX out of CPUID unless ROSETTA_ADVERTISE_AVX=1 is set, so the check fails there until that variable is set.
6#pragma once
7
8#include <cstdint>
9#include <string>
10#include <vector>
11
12namespace rt::host_cpu {
13
14constexpr uint32_t kCpuid1EcxOsxsave = 1u << 27; // CPUID.1:ECX.OSXSAVE: the OS enabled XSAVE, XGETBV is usable
15constexpr uint32_t kCpuid1EcxAvx = 1u << 28; // CPUID.1:ECX.AVX
16constexpr uint64_t kXcr0SseYmm = 0x6; // XCR0 bits 1 (SSE state) and 2 (AVX/YMM state)
17
18// gta6rt's exit code when the check fails (2 is a usage error, 3 is LEONIDA_UNIMPLEMENTED=stop).
19constexpr int kExitCode = 4;
20
21constexpr const char* kMissingAvx = "AVX";
22constexpr const char* kMissingOsxsave = "OSXSAVE";
23constexpr const char* kMissingYmmState = "OS support for YMM state (XCR0 bits 1-2)";
24
25// The features missing for the game's AVX code, from CPUID.1:ECX. read_xcr0 is called only when OSXSAVE is set,
26// because XGETBV faults without it. An empty list means the host can run AVX code.
27std::vector<const char*> missing_host_features(uint32_t cpuid1_ecx, uint64_t (*read_xcr0)());
28
29// The same for the running host: CPUID leaf 1 and XGETBV(0). Empty on a host that is not x86-64.
30std::vector<const char*> missing_host_features();
31
32// The one stderr line gta6rt prints when the list is not empty.
33std::string message(const std::vector<const char*>& missing);
34
35// LEONIDA_SKIP_CPU_CHECK=1 skips the check (for the #UD emulation work).
36bool check_skipped();
37
38} // namespace rt::host_cpu
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp
index 39b362a01..491873fd1 100644
--- a/runtime/src/main.cpp
+++ b/runtime/src/main.cpp
@@ -29,6 +29,7 @@
29 29
30#include "base_reservation.h" 30#include "base_reservation.h"
31#include "code_hooks.h" 31#include "code_hooks.h"
32#include "host_cpu.h"
32#include "mods.h" 33#include "mods.h"
33#include "pe_loader.h" 34#include "pe_loader.h"
34#include "../shims/d3d12_trace.h" 35#include "../shims/d3d12_trace.h"
@@ -103,6 +104,12 @@ int wmain(int argc, wchar_t** argv) {
103 if (image.empty()) return usage(); 104 if (image.empty()) return usage();
104 // Exactly one mode: --dry-run --run would otherwise initialise the modules and call the entry point. 105 // Exactly one mode: --dry-run --run would otherwise initialise the modules and call the entry point.
105 if (dry_run + init + run != 1) return usage(); 106 if (dry_run + init + run != 1) return usage();
107 // The game runs AVX code from its TLS callbacks on; without AVX that is a bare 0xC000001D (src/host_cpu.h).
108 if ((init || run) && !rt::host_cpu::check_skipped())
109 if (const auto missing = rt::host_cpu::missing_host_features(); !missing.empty()) {
110 std::fprintf(stderr, "%s\n", rt::host_cpu::message(missing).c_str());
111 return rt::host_cpu::kExitCode;
112 }
106 // The console folders are fixed once, as absolute paths. A bare image name (`gta6rt --run game.exe` from the 113 // The console folders are fixed once, as absolute paths. A bare image name (`gta6rt --run game.exe` from the
107 // game folder) has an empty parent path, which left GAME: with no root; and a relative --game-dir or --overlay 114 // game folder) has an empty parent path, which left GAME: with no root; and a relative --game-dir or --overlay
108 // would follow the title's later SetCurrentDirectory. If absolute() fails the path stays as given. 115 // would follow the title's later SetCurrentDirectory. If absolute() fails the path stays as given.