upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/host_cpu.cpp
blob: a49525a3ea74e0479e59bfd3dfd4ae80e6f63209 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "host_cpu.h"

#include <cstring>

#if defined(_WIN32)
#include <windows.h>
#else
#include <cstdlib>
#endif

#if defined(_M_X64) || defined(__x86_64__)
#if defined(_MSC_VER)
#include <intrin.h>
#else
#include <cpuid.h>
#endif
#endif

namespace rt::host_cpu {

std::vector<const char*> missing_host_features(uint32_t cpuid1_ecx, uint64_t (*read_xcr0)()) {
    std::vector<const char*> missing;
    if (!(cpuid1_ecx & kCpuid1EcxAvx)) missing.push_back(kMissingAvx);
    if (!(cpuid1_ecx & kCpuid1EcxOsxsave))
        missing.push_back(kMissingOsxsave);  // XGETBV is #UD without OSXSAVE: XCR0 is not read
    else if ((read_xcr0() & kXcr0SseYmm) != kXcr0SseYmm)
        missing.push_back(kMissingYmmState);
    return missing;
}

#if defined(_M_X64) || defined(__x86_64__)
namespace {

uint32_t cpuid1_ecx() {
#if defined(_MSC_VER)
    int r[4] = {};
    __cpuid(r, 1);
    return static_cast<uint32_t>(r[2]);
#else
    unsigned a = 0, b = 0, c = 0, d = 0;
    return __get_cpuid(1, &a, &b, &c, &d) ? c : 0;
#endif
}

uint64_t xcr0() {
#if defined(_MSC_VER)
    return _xgetbv(0);
#else
    uint32_t lo = 0, hi = 0;
    __asm__ volatile("xgetbv" : "=a"(lo), "=d"(hi) : "c"(0));
    return (static_cast<uint64_t>(hi) << 32) | lo;
#endif
}

}  // namespace

std::vector<const char*> missing_host_features() { return missing_host_features(cpuid1_ecx(), &xcr0); }
#else
std::vector<const char*> missing_host_features() { return {}; }
#endif

std::string message(const std::vector<const char*>& missing) {
    std::string names;
    for (const char* m : missing) names += (names.empty() ? "" : ", ") + std::string(m);
    return "error: the game's code needs AVX, and this CPU or OS lacks " + names +
           "; in a virtual machine, pass the host CPU through (for example QEMU -cpu host); under Rosetta on "
           "macOS, set ROSETTA_ADVERTISE_AVX=1. LEONIDA_SKIP_CPU_CHECK=1 skips this check";
}

bool check_skipped() {
#if defined(_WIN32)
    char v[4] = {};
    return GetEnvironmentVariableA("LEONIDA_SKIP_CPU_CHECK", v, sizeof v) == 1 && v[0] == '1';
#else
    const char* v = std::getenv("LEONIDA_SKIP_CPU_CHECK");
    return v && std::strcmp(v, "1") == 0;
#endif
}

}  // namespace rt::host_cpu