diff options
| author | Senne Cools <113857183+sennecools@users.noreply.github.com> | 2026-11-23 23:07:51 +0100 |
|---|---|---|
| committer | Senne Cools <113857183+sennecools@users.noreply.github.com> | 2026-11-23 23:07:51 +0100 |
| commit | ad6e5d6bbee5bd8c3c8a7e95216f12038fe3db22 (patch) | |
| tree | 4f0020d3c5053cf5ce189b7a26c5150c4861b5af /runtime/src | |
| parent | 5d74c272363f2703265d44e6aaab1a1df8ddfaf3 (diff) | |
gta6rt: --init/--run stop with a clear message on a host without AVX
The game runs AVX code from its TLS callbacks on, so a CPU or OS without
AVX (a QEMU qemu64 guest) died with a bare 0xC000001D. Check CPUID.1
AVX/OSXSAVE and XCR0 YMM state first, print one line and exit 4 (3 is
LEONIDA_UNIMPLEMENTED=stop). --dry-run is unchanged and
LEONIDA_SKIP_CPU_CHECK=1 skips the check.
AI-assisted.
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/host_cpu.cpp | 80 | ||||
| -rw-r--r-- | runtime/src/host_cpu.h | 38 | ||||
| -rw-r--r-- | runtime/src/main.cpp | 7 |
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 | |||
| 19 | namespace rt::host_cpu { | ||
| 20 | |||
| 21 | std::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__) | ||
| 32 | namespace { | ||
| 33 | |||
| 34 | uint32_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 | |||
| 45 | uint64_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 | |||
| 57 | std::vector<const char*> missing_host_features() { return missing_host_features(cpuid1_ecx(), &xcr0); } | ||
| 58 | #else | ||
| 59 | std::vector<const char*> missing_host_features() { return {}; } | ||
| 60 | #endif | ||
| 61 | |||
| 62 | std::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 | |||
| 70 | bool 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 | |||
| 12 | namespace rt::host_cpu { | ||
| 13 | |||
| 14 | constexpr uint32_t kCpuid1EcxOsxsave = 1u << 27; // CPUID.1:ECX.OSXSAVE: the OS enabled XSAVE, XGETBV is usable | ||
| 15 | constexpr uint32_t kCpuid1EcxAvx = 1u << 28; // CPUID.1:ECX.AVX | ||
| 16 | constexpr 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). | ||
| 19 | constexpr int kExitCode = 4; | ||
| 20 | |||
| 21 | constexpr const char* kMissingAvx = "AVX"; | ||
| 22 | constexpr const char* kMissingOsxsave = "OSXSAVE"; | ||
| 23 | constexpr 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. | ||
| 27 | std::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. | ||
| 30 | std::vector<const char*> missing_host_features(); | ||
| 31 | |||
| 32 | // The one stderr line gta6rt prints when the list is not empty. | ||
| 33 | std::string message(const std::vector<const char*>& missing); | ||
| 34 | |||
| 35 | // LEONIDA_SKIP_CPU_CHECK=1 skips the check (for the #UD emulation work). | ||
| 36 | bool check_skipped(); | ||
| 37 | |||
| 38 | } // namespace rt::host_cpu | ||
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index 71a5b44e1..c24fa0b1e 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" |
| @@ -101,6 +102,12 @@ int wmain(int argc, wchar_t** argv) { | |||
| 101 | if (image.empty()) return usage(); | 102 | if (image.empty()) return usage(); |
| 102 | // Exactly one mode: --dry-run --run would otherwise initialise the modules and call the entry point. | 103 | // Exactly one mode: --dry-run --run would otherwise initialise the modules and call the entry point. |
| 103 | if (dry_run + init + run != 1) return usage(); | 104 | if (dry_run + init + run != 1) return usage(); |
| 105 | // The game runs AVX code from its TLS callbacks on; without AVX that is a bare 0xC000001D (src/host_cpu.h). | ||
| 106 | if ((init || run) && !rt::host_cpu::check_skipped()) | ||
| 107 | if (const auto missing = rt::host_cpu::missing_host_features(); !missing.empty()) { | ||
| 108 | std::fprintf(stderr, "%s\n", rt::host_cpu::message(missing).c_str()); | ||
| 109 | return rt::host_cpu::kExitCode; | ||
| 110 | } | ||
| 104 | // The console folders are fixed once, as absolute paths. A bare image name (`gta6rt --run game.exe` from the | 111 | // The console folders are fixed once, as absolute paths. A bare image name (`gta6rt --run game.exe` from the |
| 105 | // game folder) has an empty parent path, which left GAME: with no root; and a relative --game-dir or --overlay | 112 | // game folder) has an empty parent path, which left GAME: with no root; and a relative --game-dir or --overlay |
| 106 | // would follow the title's later SetCurrentDirectory. If absolute() fails the path stays as given. | 113 | // would follow the title's later SetCurrentDirectory. If absolute() fails the path stays as given. |