Publish the host GPU name as the guest's PCI model so System Information can display the actual chipset name. Keep the x86-64 Metal name and vendor compatibility rules used by WindowServer and applications. Cover the PCI name in the guest tests and regenerate both prebuilt kext sets.
94 lines
3.9 KiB
C++
94 lines
3.9 KiB
C++
// The native test of the PCI model bytes the kext publishes on either architecture.
|
|
// The bytes come from the host name, bounded by its length, field size, and any NUL.
|
|
#include "RgfxDeviceName.hpp"
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
|
|
namespace {
|
|
int checks = 0;
|
|
|
|
void check(bool ok, const char *what)
|
|
{
|
|
checks++;
|
|
if (!ok) {
|
|
std::fprintf(stderr, "device_name_test: FAILED: %s\n", what);
|
|
std::exit(1);
|
|
}
|
|
}
|
|
|
|
// `model` as the kext reads it: out of a whole device info's bytes, the host's layout, every byte
|
|
// of it not the name's set to 0xa5.
|
|
size_t fromDeviceInfo(uint32_t nameLen, const void *name, size_t bytes, uint8_t (&out)[kRgfxDeviceModelMax])
|
|
{
|
|
static RgfxDeviceInfo info;
|
|
std::memset(&info, 0xa5, sizeof(info));
|
|
info.name_len = nameLen;
|
|
std::memset(info.name, 0, sizeof(info.name));
|
|
std::memcpy(info.name, name, bytes);
|
|
std::memset(out, 0x5a, kRgfxDeviceModelMax);
|
|
return rgfxDeviceModelFrom(reinterpret_cast<const uint8_t *>(&info) + kRgfxDeviceNameFirst, out);
|
|
}
|
|
|
|
// Whether `model`, `n` bytes, is `want` and its NUL.
|
|
bool is(const uint8_t *model, size_t n, const char *want)
|
|
{
|
|
const size_t len = std::strlen(want);
|
|
return n == len + 1 && !std::memcmp(model, want, len) && model[len] == 0;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
uint8_t model[kRgfxDeviceModelMax];
|
|
const char intel[] = "Intel UHD Graphics 630";
|
|
const char nvidia[] = "NVIDIA GeForce RTX 5090";
|
|
uint8_t full[RGFX_DEVICE_NAME_LEN];
|
|
for (size_t i = 0; i < sizeof(full); i++)
|
|
full[i] = static_cast<uint8_t>('A' + i % 26);
|
|
size_t n = 0;
|
|
|
|
// A host GPU's name: its bytes and a NUL, as the Intel GPU's 23 bytes of `model`.
|
|
n = fromDeviceInfo(22, intel, 22, model);
|
|
check(n == 23, "a 22-byte name is 23 bytes of model");
|
|
check(!std::memcmp(model, intel, 23), "the name's bytes, then its NUL");
|
|
n = fromDeviceInfo(sizeof(nvidia) - 1, nvidia, sizeof(nvidia) - 1, model);
|
|
check(n == sizeof(nvidia) && !std::memcmp(model, nvidia, sizeof(nvidia)),
|
|
"the host's GPU name, NUL-ended");
|
|
|
|
// No name: name_len 0 states none, whatever the field holds; so does a NUL first.
|
|
check(fromDeviceInfo(0, intel, 22, model) == 0, "name_len 0 is no name");
|
|
check(fromDeviceInfo(0, "", 0, model) == 0, "an empty field is no name");
|
|
check(fromDeviceInfo(5, "\0abcd", 5, model) == 0, "a NUL first is no name");
|
|
|
|
// A whole field, 256 bytes and no NUL in it: all of them, then a NUL past the field.
|
|
n = fromDeviceInfo(RGFX_DEVICE_NAME_LEN, full, sizeof(full), model);
|
|
check(n == RGFX_DEVICE_NAME_LEN + 1, "a full field is 256 bytes and a NUL");
|
|
check(!std::memcmp(model, full, sizeof(full)) && model[RGFX_DEVICE_NAME_LEN] == 0,
|
|
"every byte of a full field, NUL-ended");
|
|
// A length past the field reads the field alone, never the bytes after it (architecture_name).
|
|
n = fromDeviceInfo(UINT32_MAX, full, sizeof(full), model);
|
|
check(n == RGFX_DEVICE_NAME_LEN + 1 && model[RGFX_DEVICE_NAME_LEN] == 0,
|
|
"a length past the field is cut to it");
|
|
n = fromDeviceInfo(RGFX_DEVICE_NAME_LEN + 1, full, sizeof(full), model);
|
|
check(n == RGFX_DEVICE_NAME_LEN + 1, "257 is cut to the field too");
|
|
|
|
// A length short of the name: its first bytes, NUL-ended.
|
|
n = fromDeviceInfo(5, intel, 22, model);
|
|
check(is(model, n, "Intel"), "name_len bounds the name");
|
|
// A NUL inside the length: the name up to it.
|
|
n = fromDeviceInfo(10, "AMD\0Radeon", 10, model);
|
|
check(is(model, n, "AMD"), "a NUL inside the length ends the name");
|
|
|
|
// The span the kext reads holds name_len and the whole field, and lies inside the device info.
|
|
check(kRgfxDeviceNameFirst == offsetof(RgfxDeviceInfo, name_len), "the span starts at name_len");
|
|
check(kRgfxDeviceNameEnd == offsetof(RgfxDeviceInfo, name) + sizeof(RgfxDeviceInfo{}.name),
|
|
"the span ends at the name field's end");
|
|
check(kRgfxDeviceNameEnd <= sizeof(RgfxDeviceInfo), "the span ends inside the device info");
|
|
|
|
std::printf("device_name_test: OK (%d checks)\n", checks);
|
|
return 0;
|
|
}
|