aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / tools / ck_gsp_sysinfo_encode.c
| 1 | /* ck_gsp_sysinfo_encode -- host tool: the 936-byte GspSystemInfo payload of GSP_SET_SYSTEM_INFO (function 72), the |
| 2 | * first message GSP-RM reads from the command queue (L332 rung 2 window 1; L192 gb10_gsp_boot.h takes these bytes as |
| 3 | * gsp_boot_in.system_info). |
| 4 | * |
| 5 | * The layout is NVIDIA's (open-gpu-kernel-modules 580.173.02, MIT): GspSystemInfo |
| 6 | * src/nvidia/inc/kernel/gpu/gsp/gsp_static_config.h:172-227 with GSP_VF_INFO :45-55 and GSP_PCIE_CONFIG_REG :58-62, |
| 7 | * BUSINFO src/nvidia/generated/g_chipset_nvoc.h:220-227, ACPI_METHOD_DATA src/nvidia/inc/kernel/gpu/gpu_acpi_data.h:72-115 |
| 8 | * (NV0073_CTRL_SYSTEM_ACPI_ID_MAP_MAX_DISPLAYS 16, ctrl0073system.h:747). Natural LP64 layout, no pack pragma. |
| 9 | * The mirror below is our own declaration of the same fields; `make gsp-sysinfo-nvhdr-test` compiles it against those |
| 10 | * NVIDIA lines (cut out of the pinned tree at build time, never vendored) and compares every offset and size. |
| 11 | * |
| 12 | * The values are data, not code: rpcGspSetSystemInfo_v17_00 (src/nvidia/src/kernel/vgpu/rpc.c:10493) fills them from |
| 13 | * the bus, the chipset object and the OS. They come in as a text file, one "field value" per line ("#" comments; field |
| 14 | * names are the C names, nested ones with a dot, e.g. FHBBusInfo.deviceID; values decimal or 0x hex). Every field not |
| 15 | * named is 0. The window-1 values and their sources are in evidence/own-gpu-driver/rung2/system_info.txt. |
| 16 | * |
| 17 | * ck_gsp_sysinfo_encode encode IN.txt OUT.bin writes 936 bytes, prints each field set; exit 2 + REFUSED on an |
| 18 | * unknown field, a repeated field, a value that does not fit, bad syntax |
| 19 | * ck_gsp_sysinfo_encode fields prints every field with its offset and size |
| 20 | * ck_gsp_sysinfo_encode selftest layout anchors, a known encoding, every refusal |
| 21 | * Hosted C, no outside libraries. Never part of an image. */ |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <stddef.h> |
| 25 | #include <stdint.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | #define SI_ACPI_MAX_DISPLAYS 16 |
| 32 | |
| 33 | typedef struct { uint16_t deviceID, vendorID, subdeviceID, subvendorID; uint8_t revisionID; } si_businfo; |
| 34 | typedef struct { uint32_t status, acpiIdListLen, acpiIdList[SI_ACPI_MAX_DISPLAYS]; } si_dod; |
| 35 | typedef struct { uint32_t status, jtCaps; uint16_t jtRevId; uint8_t bSBIOSCaps; } si_jt; |
| 36 | typedef struct { uint32_t acpiId, mode, status; } si_mux_elem; |
| 37 | typedef struct { |
| 38 | uint32_t tableLen; |
| 39 | si_mux_elem acpiIdMuxModeTable[SI_ACPI_MAX_DISPLAYS], acpiIdMuxPartTable[SI_ACPI_MAX_DISPLAYS], |
| 40 | acpiIdMuxStateTable[SI_ACPI_MAX_DISPLAYS]; |
| 41 | } si_mux; |
| 42 | typedef struct { uint32_t status, optimusCaps; } si_caps; |
| 43 | typedef struct { uint8_t bValid; si_dod dodMethodData; si_jt jtMethodData; si_mux muxMethodData; si_caps capsMethodData; } si_acpi; |
| 44 | typedef struct { |
| 45 | uint32_t totalVFs, firstVFOffset; |
| 46 | uint64_t FirstVFBar0Address, FirstVFBar1Address, FirstVFBar2Address; |
| 47 | uint8_t b64bitBar0, b64bitBar1, b64bitBar2; |
| 48 | } si_vf; |
| 49 | typedef struct { uint32_t linkCap; } si_pcie; |
| 50 | |
| 51 | typedef struct { |
| 52 | uint64_t gpuPhysAddr, gpuPhysFbAddr, gpuPhysInstAddr, gpuPhysIoAddr, nvDomainBusDeviceFunc, simAccessBufPhysAddr, |
| 53 | notifyOpSharedSurfacePhysAddr, pcieAtomicsOpMask, consoleMemSize, maxUserVa; |
| 54 | uint32_t pciConfigMirrorBase, pciConfigMirrorSize, PCIDeviceID, PCISubDeviceID, PCIRevisionID, |
| 55 | pcieAtomicsCplDeviceCapMask; |
| 56 | uint8_t oorArch; |
| 57 | uint64_t clPdbProperties; |
| 58 | uint32_t Chipset; |
| 59 | uint8_t bGpuBehindBridge, bFlrSupported, b64bBar0Supported, bMnocAvailable; |
| 60 | uint32_t chipsetL1ssEnable; |
| 61 | uint8_t bUpstreamL0sUnsupported, bUpstreamL1Unsupported, bUpstreamL1PorSupported, bUpstreamL1PorMobileOnly, |
| 62 | bSystemHasMux, upstreamAddressValid; |
| 63 | si_businfo FHBBusInfo, chipsetIDInfo; |
| 64 | si_acpi acpiMethodData; |
| 65 | uint32_t hypervisorType; |
| 66 | uint16_t virtualConfigBits; |
| 67 | uint8_t bIsPassthru; |
| 68 | uint64_t sysTimerOffsetNs; |
| 69 | si_vf gspVFInfo; |
| 70 | uint8_t bIsPrimary, bIsUnixHdmiFrlComplianceEnabled, isGridBuild; |
| 71 | si_pcie pcieConfigReg; |
| 72 | uint32_t gridBuildCsp; |
| 73 | uint8_t bPreserveVideoMemoryAllocations, bTdrEventSupported, bFeatureStretchVblankCapable, |
| 74 | bEnableDynamicGranularityPageArrays, bClockBoostSupported; |
| 75 | uint64_t hostPageSize; |
| 76 | uint8_t bIsCmcBasedHws, bGspNocatEnabled, bS0ixSupport, bWindowChannelAlwaysMapped; |
| 77 | } si_mirror; |
| 78 | |
| 79 | /* Anchors also recorded by ck_gsp_rpc_encode (inventory section 13) from compiling the NVIDIA struct text. */ |
| 80 | _Static_assert(sizeof(si_mirror) == 936, "GspSystemInfo is 936 bytes"); |
| 81 | _Static_assert(sizeof(si_businfo) == 10 && sizeof(si_acpi) == 676 && sizeof(si_vf) == 40, "nested sizes"); |
| 82 | _Static_assert(offsetof(si_mirror, FHBBusInfo) == 138 && offsetof(si_mirror, chipsetIDInfo) == 148 && |
| 83 | offsetof(si_mirror, acpiMethodData) == 160 && offsetof(si_mirror, hypervisorType) == 836 && |
| 84 | offsetof(si_mirror, gspVFInfo) == 856 && offsetof(si_mirror, hostPageSize) == 920 && |
| 85 | offsetof(si_mirror, bWindowChannelAlwaysMapped) == 931, "GspSystemInfo anchors"); |
| 86 | |
| 87 | #define F(f) {#f, offsetof(si_mirror, f), sizeof(((si_mirror *)0)->f)} |
| 88 | #define FN(s, f) {#s "." #f, offsetof(si_mirror, s) + offsetof(__typeof__(((si_mirror *)0)->s), f), \ |
| 89 | sizeof(((si_mirror *)0)->s.f)} |
| 90 | static const struct field { const char *name; uint32_t off, size; } fields[] = { |
| 91 | F(gpuPhysAddr), F(gpuPhysFbAddr), F(gpuPhysInstAddr), F(gpuPhysIoAddr), F(nvDomainBusDeviceFunc), |
| 92 | F(simAccessBufPhysAddr), F(notifyOpSharedSurfacePhysAddr), F(pcieAtomicsOpMask), F(consoleMemSize), F(maxUserVa), |
| 93 | F(pciConfigMirrorBase), F(pciConfigMirrorSize), F(PCIDeviceID), F(PCISubDeviceID), F(PCIRevisionID), |
| 94 | F(pcieAtomicsCplDeviceCapMask), F(oorArch), F(clPdbProperties), F(Chipset), F(bGpuBehindBridge), F(bFlrSupported), |
| 95 | F(b64bBar0Supported), F(bMnocAvailable), F(chipsetL1ssEnable), F(bUpstreamL0sUnsupported), |
| 96 | F(bUpstreamL1Unsupported), F(bUpstreamL1PorSupported), F(bUpstreamL1PorMobileOnly), F(bSystemHasMux), |
| 97 | F(upstreamAddressValid), |
| 98 | FN(FHBBusInfo, deviceID), FN(FHBBusInfo, vendorID), FN(FHBBusInfo, subdeviceID), FN(FHBBusInfo, subvendorID), |
| 99 | FN(FHBBusInfo, revisionID), |
| 100 | FN(chipsetIDInfo, deviceID), FN(chipsetIDInfo, vendorID), FN(chipsetIDInfo, subdeviceID), |
| 101 | FN(chipsetIDInfo, subvendorID), FN(chipsetIDInfo, revisionID), |
| 102 | FN(acpiMethodData, bValid), FN(acpiMethodData, dodMethodData.status), FN(acpiMethodData, dodMethodData.acpiIdListLen), |
| 103 | FN(acpiMethodData, jtMethodData.status), FN(acpiMethodData, capsMethodData.status), |
| 104 | F(hypervisorType), F(virtualConfigBits), F(bIsPassthru), F(sysTimerOffsetNs), |
| 105 | FN(gspVFInfo, totalVFs), FN(gspVFInfo, firstVFOffset), FN(gspVFInfo, FirstVFBar0Address), |
| 106 | FN(gspVFInfo, FirstVFBar1Address), FN(gspVFInfo, FirstVFBar2Address), FN(gspVFInfo, b64bitBar0), |
| 107 | FN(gspVFInfo, b64bitBar1), FN(gspVFInfo, b64bitBar2), |
| 108 | F(bIsPrimary), F(bIsUnixHdmiFrlComplianceEnabled), F(isGridBuild), FN(pcieConfigReg, linkCap), F(gridBuildCsp), |
| 109 | F(bPreserveVideoMemoryAllocations), F(bTdrEventSupported), F(bFeatureStretchVblankCapable), |
| 110 | F(bEnableDynamicGranularityPageArrays), F(bClockBoostSupported), F(hostPageSize), F(bIsCmcBasedHws), |
| 111 | F(bGspNocatEnabled), F(bS0ixSupport), F(bWindowChannelAlwaysMapped), |
| 112 | }; |
| 113 | #define NFIELDS (sizeof(fields) / sizeof(fields[0])) |
| 114 | |
| 115 | /* Encode the text in src into out[936]. Returns 0, or -1 with a reason in why. */ |
| 116 | static int encode(const char *src, uint8_t out[936], char *why, size_t whylen, FILE *echo) |
| 117 | { |
| 118 | uint8_t seen[NFIELDS] = {0}; |
| 119 | memset(out, 0, 936); |
| 120 | unsigned line = 0; |
| 121 | for (const char *p = src; *p;) { |
| 122 | const char *eol = strchr(p, '\n'); |
| 123 | size_t n = eol ? (size_t)(eol - p) : strlen(p); |
| 124 | char buf[512]; |
| 125 | line++; |
| 126 | if (n >= sizeof buf) { snprintf(why, whylen, "line %u too long", line); return -1; } |
| 127 | memcpy(buf, p, n); |
| 128 | buf[n] = 0; |
| 129 | p += n + (eol ? 1 : 0); |
| 130 | char *h = strchr(buf, '#'); |
| 131 | if (h) *h = 0; |
| 132 | char name[128], val[128], extra[8]; |
| 133 | int k = sscanf(buf, "%127s %127s %7s", name, val, extra); |
| 134 | if (k <= 0) continue; |
| 135 | if (k != 2) { snprintf(why, whylen, "line %u: want \"field value\"", line); return -1; } |
| 136 | size_t i = 0; |
| 137 | while (i < NFIELDS && strcmp(fields[i].name, name)) i++; |
| 138 | if (i == NFIELDS) { snprintf(why, whylen, "line %u: unknown field %s", line, name); return -1; } |
| 139 | if (seen[i]) { snprintf(why, whylen, "line %u: field %s repeated", line, name); return -1; } |
| 140 | seen[i] = 1; |
| 141 | char *end; |
| 142 | errno = 0; |
| 143 | unsigned long long v = strtoull(val, &end, 0); |
| 144 | if (errno || *end || val[0] == '-') { snprintf(why, whylen, "line %u: %s is not a number", line, val); return -1; } |
| 145 | if (fields[i].size < 8 && v >> (8 * fields[i].size)) { |
| 146 | snprintf(why, whylen, "line %u: %.40s does not fit %.60s (%u bytes)", line, val, name, fields[i].size); |
| 147 | return -1; |
| 148 | } |
| 149 | for (uint32_t b = 0; b < fields[i].size; b++) out[fields[i].off + b] = (uint8_t)(v >> (8 * b)); /* little endian */ |
| 150 | if (echo) fprintf(echo, "set %-36s off=%3u size=%u value=%#llx\n", name, fields[i].off, fields[i].size, v); |
| 151 | } |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static char *slurp(const char *path) |
| 156 | { |
| 157 | FILE *f = fopen(path, "rb"); |
| 158 | if (!f) return NULL; |
| 159 | char *b = NULL; |
| 160 | size_t n = 0, cap = 0, r; |
| 161 | char tmp[4096]; |
| 162 | while ((r = fread(tmp, 1, sizeof tmp, f)) > 0) { |
| 163 | if (n + r + 1 > cap) { cap = (n + r + 1) * 2; b = realloc(b, cap); if (!b) { fclose(f); return NULL; } } |
| 164 | memcpy(b + n, tmp, r); |
| 165 | n += r; |
| 166 | } |
| 167 | fclose(f); |
| 168 | if (!b) b = calloc(1, 1); |
| 169 | else b[n] = 0; |
| 170 | return b; |
| 171 | } |
| 172 | |
| 173 | static int selftest(void) |
| 174 | { |
| 175 | int fail = 0, n = 0; |
| 176 | uint8_t out[936]; |
| 177 | char why[256]; |
| 178 | #define CHECK(c, msg) do { n++; if (!(c)) { printf("FAIL %s\n", msg); fail = 1; } else printf("PASS %s\n", msg); } while (0) |
| 179 | CHECK(encode("gpuPhysAddr 0x24000000\nPCIDeviceID 0x2e1210de # GB10\n\nFHBBusInfo.deviceID 0x22d1\n" |
| 180 | "hostPageSize 4096\nbWindowChannelAlwaysMapped 1\n", out, why, sizeof why, NULL) == 0, |
| 181 | "valid text accepted"); |
| 182 | uint8_t want[936] = {0}; |
| 183 | want[3] = 0x24; |
| 184 | want[88] = 0xde; want[89] = 0x10; want[90] = 0x12; want[91] = 0x2e; |
| 185 | want[138] = 0xd1; want[139] = 0x22; |
| 186 | want[921] = 0x10; |
| 187 | want[931] = 1; |
| 188 | CHECK(memcmp(out, want, 936) == 0, "bytes land at the NVIDIA offsets, little endian"); |
| 189 | static const struct { const char *text, *want; } bad[] = { |
| 190 | {"gpuPhysAdr 1\n", "unknown field"}, |
| 191 | {"Chipset 1\nChipset 2\n", "repeated"}, |
| 192 | {"oorArch 256\n", "does not fit"}, |
| 193 | {"FHBBusInfo.deviceID 0x10000\n", "does not fit"}, |
| 194 | {"PCIRevisionID 0x100000000\n", "does not fit"}, |
| 195 | {"maxUserVa 0x10000000000000000\n", "not a number"}, |
| 196 | {"bIsPrimary -1\n", "not a number"}, |
| 197 | {"bIsPrimary 1x\n", "not a number"}, |
| 198 | {"bIsPrimary\n", "want"}, |
| 199 | {"bIsPrimary 1 2\n", "want"}, |
| 200 | {"acpiMethodData.dodMethodData 1\n", "unknown field"}, |
| 201 | }; |
| 202 | for (size_t i = 0; i < sizeof bad / sizeof bad[0]; i++) { |
| 203 | char msg[300]; |
| 204 | int rc = encode(bad[i].text, out, why, sizeof why, NULL); |
| 205 | snprintf(msg, sizeof msg, "refused \"%.*s\": %s", (int)strcspn(bad[i].text, "\n"), bad[i].text, rc ? why : "ACCEPTED"); |
| 206 | CHECK(rc == -1 && strstr(why, bad[i].want), msg); |
| 207 | } |
| 208 | /* every field distinct and inside the struct */ |
| 209 | int overlap = 0; |
| 210 | for (size_t i = 0; i < NFIELDS; i++) |
| 211 | for (size_t j = i + 1; j < NFIELDS; j++) |
| 212 | if (fields[i].off < fields[j].off + fields[j].size && fields[j].off < fields[i].off + fields[i].size) overlap = 1; |
| 213 | CHECK(!overlap, "no two fields overlap"); |
| 214 | /* ACPI statuses: dod at 160+4, jt after dod's 16-entry list, caps right before hypervisorType (836) */ |
| 215 | CHECK(encode("acpiMethodData.dodMethodData.status 0xffff\nacpiMethodData.jtMethodData.status 0x56\n" |
| 216 | "acpiMethodData.capsMethodData.status 0x56\n", out, why, sizeof why, NULL) == 0 && |
| 217 | out[164] == 0xff && out[165] == 0xff && out[236] == 0x56 && out[828] == 0x56, |
| 218 | "ACPI status fields land at 164, 236 and 828"); |
| 219 | printf("CK_GSP_SYSINFO_ENCODE_SELFTEST: %s (%d checks, %zu fields)\n", fail ? "FAIL" : "PASS", n, NFIELDS); |
| 220 | return fail; |
| 221 | } |
| 222 | |
| 223 | #ifdef CK_SYSINFO_NVHDR |
| 224 | /* make gsp-sysinfo-nvhdr-test: the NVIDIA struct text, cut out of the pinned tree by the Makefile. */ |
| 225 | #include "nvtypes.h" |
| 226 | #include "nvstatus.h" |
| 227 | #include CK_SYSINFO_NVHDR |
| 228 | #define SAME(f) (offsetof(si_mirror, f) == offsetof(GspSystemInfo, f) && \ |
| 229 | sizeof(((si_mirror *)0)->f) == sizeof(((GspSystemInfo *)0)->f)) |
| 230 | static int nvhdr(void) |
| 231 | { |
| 232 | int fail = sizeof(si_mirror) != sizeof(GspSystemInfo); |
| 233 | int n = 1; |
| 234 | #define S(f) do { n++; if (!SAME(f)) { printf("FAIL %s\n", #f); fail = 1; } } while (0) |
| 235 | S(gpuPhysAddr); S(gpuPhysFbAddr); S(gpuPhysInstAddr); S(gpuPhysIoAddr); S(nvDomainBusDeviceFunc); |
| 236 | S(simAccessBufPhysAddr); S(notifyOpSharedSurfacePhysAddr); S(pcieAtomicsOpMask); S(consoleMemSize); S(maxUserVa); |
| 237 | S(pciConfigMirrorBase); S(pciConfigMirrorSize); S(PCIDeviceID); S(PCISubDeviceID); S(PCIRevisionID); |
| 238 | S(pcieAtomicsCplDeviceCapMask); S(oorArch); S(clPdbProperties); S(Chipset); S(bGpuBehindBridge); S(bFlrSupported); |
| 239 | S(b64bBar0Supported); S(bMnocAvailable); S(chipsetL1ssEnable); S(bUpstreamL0sUnsupported); |
| 240 | S(bUpstreamL1Unsupported); S(bUpstreamL1PorSupported); S(bUpstreamL1PorMobileOnly); S(bSystemHasMux); |
| 241 | S(upstreamAddressValid); S(FHBBusInfo); S(FHBBusInfo.deviceID); S(FHBBusInfo.vendorID); S(FHBBusInfo.subdeviceID); |
| 242 | S(FHBBusInfo.subvendorID); S(FHBBusInfo.revisionID); S(chipsetIDInfo); S(chipsetIDInfo.revisionID); |
| 243 | S(acpiMethodData); S(acpiMethodData.bValid); S(acpiMethodData.muxMethodData); S(acpiMethodData.capsMethodData); |
| 244 | S(acpiMethodData.dodMethodData.status); S(acpiMethodData.dodMethodData.acpiIdListLen); |
| 245 | S(acpiMethodData.jtMethodData.status); S(acpiMethodData.capsMethodData.status); |
| 246 | S(hypervisorType); S(virtualConfigBits); S(bIsPassthru); S(sysTimerOffsetNs); S(gspVFInfo); |
| 247 | S(gspVFInfo.FirstVFBar2Address); S(gspVFInfo.b64bitBar2); S(bIsPrimary); S(bIsUnixHdmiFrlComplianceEnabled); |
| 248 | S(isGridBuild); S(pcieConfigReg); S(pcieConfigReg.linkCap); S(gridBuildCsp); S(bPreserveVideoMemoryAllocations); |
| 249 | S(bTdrEventSupported); S(bFeatureStretchVblankCapable); S(bEnableDynamicGranularityPageArrays); |
| 250 | S(bClockBoostSupported); S(hostPageSize); S(bIsCmcBasedHws); S(bGspNocatEnabled); S(bS0ixSupport); |
| 251 | S(bWindowChannelAlwaysMapped); |
| 252 | printf("GSP_SYSINFO_NVHDR: %s (%d comparisons, sizeof GspSystemInfo %zu)\n", fail ? "FAIL" : "PASS", n, |
| 253 | sizeof(GspSystemInfo)); |
| 254 | return fail; |
| 255 | } |
| 256 | #endif |
| 257 | |
| 258 | int main(int argc, char **argv) |
| 259 | { |
| 260 | if (argc == 2 && !strcmp(argv[1], "selftest")) return selftest(); |
| 261 | #ifdef CK_SYSINFO_NVHDR |
| 262 | if (argc == 2 && !strcmp(argv[1], "nvhdr")) return nvhdr(); |
| 263 | #endif |
| 264 | if (argc == 2 && !strcmp(argv[1], "fields")) { |
| 265 | for (size_t i = 0; i < NFIELDS; i++) printf("%-36s off=%3u size=%u\n", fields[i].name, fields[i].off, fields[i].size); |
| 266 | return 0; |
| 267 | } |
| 268 | if (argc == 4 && !strcmp(argv[1], "encode")) { |
| 269 | char *src = slurp(argv[2]), why[256]; |
| 270 | uint8_t out[936]; |
| 271 | if (!src) { printf("REFUSED: cannot read %s\n", argv[2]); return 2; } |
| 272 | if (encode(src, out, why, sizeof why, stdout)) { printf("REFUSED: %s\n", why); return 2; } |
| 273 | /* 0644, not fopen's 0666 & ~umask: the payload goes into the window-1 package and must not be world-writable. */ |
| 274 | int fd = open(argv[3], O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); |
| 275 | FILE *f = fd < 0 ? NULL : fdopen(fd, "wb"); |
| 276 | if (fd >= 0 && !f) close(fd); |
| 277 | if (!f || fwrite(out, 1, sizeof out, f) != sizeof out || fclose(f)) { printf("REFUSED: cannot write %s\n", argv[3]); return 2; } |
| 278 | printf("RESULT OK %s 936 bytes\n", argv[3]); |
| 279 | return 0; |
| 280 | } |
| 281 | fprintf(stderr, "usage: ck_gsp_sysinfo_encode encode IN.txt OUT.bin | fields | selftest\n"); |
| 282 | return 1; |
| 283 | } |