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/host_cpu.cpp | |
| 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/host_cpu.cpp')
| -rw-r--r-- | runtime/src/host_cpu.cpp | 80 |
1 files changed, 80 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 | ||