physics-mv
FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.
Brought by drakestapleton
physics-mv / forge / forge_descriptor.c
| 1 | #include "forge_descriptor.h" |
| 2 | #include "nvrm.h" |
| 3 | #include "nvos.h" |
| 4 | #include "nv-ioctl.h" |
| 5 | #include "nv-ioctl-numbers.h" |
| 6 | #include "nv_escape.h" |
| 7 | #include "ctrl/ctrl2080/ctrl2080gr.h" |
| 8 | |
| 9 | #include <ctype.h> |
| 10 | #include <dirent.h> |
| 11 | #include <errno.h> |
| 12 | #include <fcntl.h> |
| 13 | #include <inttypes.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | #include <sys/ioctl.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #define NV_IOWR(nr, sz) _IOC(_IOC_READ | _IOC_WRITE, NV_IOCTL_MAGIC, (nr), (sz)) |
| 21 | |
| 22 | /* Forward declaration of clean SHA-256 implementation */ |
| 23 | void sha256_compute(const uint8_t *data, uint64_t len, uint8_t scratch_buf[128], uint32_t out_digest[8]); |
| 24 | |
| 25 | static void sha256_bytes(const uint8_t *data, size_t len, uint8_t out[32]) { |
| 26 | uint8_t scratch[128]; |
| 27 | uint32_t words[8]; |
| 28 | sha256_compute(data, len, scratch, words); |
| 29 | for (int i = 0; i < 8; i++) { |
| 30 | out[i * 4 + 0] = (uint8_t)((words[i] >> 24) & 0xff); |
| 31 | out[i * 4 + 1] = (uint8_t)((words[i] >> 16) & 0xff); |
| 32 | out[i * 4 + 2] = (uint8_t)((words[i] >> 8) & 0xff); |
| 33 | out[i * 4 + 3] = (uint8_t)(words[i] & 0xff); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | static inline void put_u16_le(uint8_t *p, uint16_t v) { |
| 38 | p[0] = (uint8_t)(v & 0xff); |
| 39 | p[1] = (uint8_t)((v >> 8) & 0xff); |
| 40 | } |
| 41 | |
| 42 | static inline void put_u32_le(uint8_t *p, uint32_t v) { |
| 43 | p[0] = (uint8_t)(v & 0xff); |
| 44 | p[1] = (uint8_t)((v >> 8) & 0xff); |
| 45 | p[2] = (uint8_t)((v >> 16) & 0xff); |
| 46 | p[3] = (uint8_t)((v >> 24) & 0xff); |
| 47 | } |
| 48 | |
| 49 | static inline void put_u64_le(uint8_t *p, uint64_t v) { |
| 50 | put_u32_le(p, (uint32_t)(v & 0xffffffffULL)); |
| 51 | put_u32_le(p + 4, (uint32_t)((v >> 32) & 0xffffffffULL)); |
| 52 | } |
| 53 | |
| 54 | static int read_sysfs_u32(const char *path, uint32_t *out) { |
| 55 | FILE *f = fopen(path, "r"); |
| 56 | if (!f) return -1; |
| 57 | uint32_t val = 0; |
| 58 | int rc = fscanf(f, "0x%x", &val); |
| 59 | if (rc != 1) { |
| 60 | rewind(f); |
| 61 | rc = fscanf(f, "%u", &val); |
| 62 | } |
| 63 | fclose(f); |
| 64 | if (rc != 1) return -1; |
| 65 | *out = val; |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static int read_proc_driver_version(char *out_ver, size_t max_len) { |
| 70 | FILE *f = fopen("/proc/driver/nvidia/version", "r"); |
| 71 | if (!f) return -1; |
| 72 | char line[512]; |
| 73 | int found = 0; |
| 74 | if (fgets(line, sizeof(line), f)) { |
| 75 | char *p = strstr(line, "NVRM version:"); |
| 76 | if (p) { |
| 77 | char *v = strstr(p, "580."); |
| 78 | if (!v) v = strstr(p, "5"); |
| 79 | if (v) { |
| 80 | size_t i = 0; |
| 81 | while (v[i] && !isspace((unsigned char)v[i]) && i < max_len - 1) { |
| 82 | out_ver[i] = v[i]; |
| 83 | i++; |
| 84 | } |
| 85 | out_ver[i] = '\0'; |
| 86 | found = 1; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | fclose(f); |
| 91 | return found ? 0 : -1; |
| 92 | } |
| 93 | |
| 94 | static int read_proc_gpu_info(const char *bdf, uint32_t expected_minor, const uint8_t expected_uuid[16], |
| 95 | char *out_vbios, size_t max_vbios, |
| 96 | char *out_fw, size_t max_fw, |
| 97 | uint64_t *out_dma_mask) { |
| 98 | char path[256]; |
| 99 | snprintf(path, sizeof(path), "/proc/driver/nvidia/gpus/%s/information", bdf); |
| 100 | FILE *f = fopen(path, "r"); |
| 101 | if (!f) return -1; |
| 102 | |
| 103 | char line[256]; |
| 104 | int verified_minor = 0; |
| 105 | int verified_uuid = 0; |
| 106 | int found_vbios = 0; |
| 107 | int found_fw = 0; |
| 108 | int found_dma = 0; |
| 109 | |
| 110 | char expected_uuid_str[64]; |
| 111 | snprintf(expected_uuid_str, sizeof(expected_uuid_str), |
| 112 | "GPU-%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
| 113 | expected_uuid[0], expected_uuid[1], expected_uuid[2], expected_uuid[3], |
| 114 | expected_uuid[4], expected_uuid[5], expected_uuid[6], expected_uuid[7], |
| 115 | expected_uuid[8], expected_uuid[9], expected_uuid[10], expected_uuid[11], |
| 116 | expected_uuid[12], expected_uuid[13], expected_uuid[14], expected_uuid[15]); |
| 117 | |
| 118 | while (fgets(line, sizeof(line), f)) { |
| 119 | if (strncmp(line, "Device Minor:", 13) == 0) { |
| 120 | uint32_t m = 0; |
| 121 | if (sscanf(line + 13, "%u", &m) == 1 && m == expected_minor) { |
| 122 | verified_minor = 1; |
| 123 | } |
| 124 | } else if (strncmp(line, "GPU UUID:", 9) == 0) { |
| 125 | char *p = line + 9; |
| 126 | while (*p && isspace((unsigned char)*p)) p++; |
| 127 | if (strncasecmp(p, expected_uuid_str, strlen(expected_uuid_str)) == 0) { |
| 128 | verified_uuid = 1; |
| 129 | } |
| 130 | } else if (strncmp(line, "Video BIOS:", 11) == 0) { |
| 131 | char *p = line + 11; |
| 132 | while (*p && isspace((unsigned char)*p)) p++; |
| 133 | size_t i = 0; |
| 134 | while (p[i] && !isspace((unsigned char)p[i]) && i < max_vbios - 1) { |
| 135 | out_vbios[i] = p[i]; |
| 136 | i++; |
| 137 | } |
| 138 | out_vbios[i] = '\0'; |
| 139 | found_vbios = 1; |
| 140 | } else if (strncmp(line, "GPU Firmware:", 13) == 0) { |
| 141 | char *p = line + 13; |
| 142 | while (*p && isspace((unsigned char)*p)) p++; |
| 143 | size_t i = 0; |
| 144 | while (p[i] && !isspace((unsigned char)p[i]) && i < max_fw - 1) { |
| 145 | out_fw[i] = p[i]; |
| 146 | i++; |
| 147 | } |
| 148 | out_fw[i] = '\0'; |
| 149 | found_fw = 1; |
| 150 | } else if (strncmp(line, "DMA Mask:", 9) == 0) { |
| 151 | char *p = line + 9; |
| 152 | while (*p && isspace((unsigned char)*p)) p++; |
| 153 | uint64_t mask = 0; |
| 154 | if (sscanf(p, "0x%" SCNx64, &mask) == 1 || sscanf(p, "%" SCNu64, &mask) == 1) { |
| 155 | *out_dma_mask = mask; |
| 156 | found_dma = 1; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | fclose(f); |
| 161 | |
| 162 | if (!verified_minor || !verified_uuid) { |
| 163 | return FORGE_PROBE_ERR_DEVICE_MISMATCH; |
| 164 | } |
| 165 | if (!found_vbios || !found_fw) { |
| 166 | return FORGE_PROBE_ERR_PROCFS; |
| 167 | } |
| 168 | if (!found_dma) { |
| 169 | *out_dma_mask = 0; /* explicitly record unobserved rather than guessing */ |
| 170 | } |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static int read_total_memory(uint64_t *out_bytes) { |
| 175 | FILE *f = fopen("/proc/meminfo", "r"); |
| 176 | if (!f) return -1; |
| 177 | char line[256]; |
| 178 | int found = 0; |
| 179 | while (fgets(line, sizeof(line), f)) { |
| 180 | if (strncmp(line, "MemTotal:", 9) == 0) { |
| 181 | uint64_t kb = 0; |
| 182 | if (sscanf(line + 9, "%" SCNu64, &kb) == 1) { |
| 183 | *out_bytes = kb * 1024ULL; |
| 184 | found = 1; |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | fclose(f); |
| 190 | return found ? 0 : -1; |
| 191 | } |
| 192 | |
| 193 | int forge_probe_hardware(ForgeMachineDescriptor *out) { |
| 194 | if (!out) return FORGE_PROBE_ERR_INVALID_ARG; |
| 195 | memset(out, 0, sizeof(*out)); |
| 196 | |
| 197 | /* 1. Open NVRM client to bind to the active physical GPU */ |
| 198 | Nvrm rm; |
| 199 | if (nvrm_open(&rm) != 0) { |
| 200 | return FORGE_PROBE_ERR_NVRM; |
| 201 | } |
| 202 | |
| 203 | /* 2. Query NV_ESC_CARD_INFO from NVRM to get exact BDF of the opened GPU */ |
| 204 | nv_ioctl_card_info_t cards[64]; |
| 205 | memset(cards, 0, sizeof(cards)); |
| 206 | if (ioctl(rm.fd_ctl, NV_IOWR(NV_ESC_CARD_INFO, sizeof(cards)), cards) != 0) { |
| 207 | nvrm_close(&rm); |
| 208 | return FORGE_PROBE_ERR_NVRM; |
| 209 | } |
| 210 | |
| 211 | int found = -1; |
| 212 | for (int i = 0; i < 64; i++) { |
| 213 | if (cards[i].valid && cards[i].gpu_id == rm.gpu_id) { |
| 214 | found = i; |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | if (found < 0) { |
| 219 | nvrm_close(&rm); |
| 220 | return FORGE_PROBE_ERR_DEVICE_MISMATCH; |
| 221 | } |
| 222 | |
| 223 | /* Record exact PCI BDF observed from RM card info */ |
| 224 | snprintf(out->pci_bdf, sizeof(out->pci_bdf), "%04x:%02x:%02x.%d", |
| 225 | cards[found].pci_info.domain, cards[found].pci_info.bus, |
| 226 | cards[found].pci_info.slot, cards[found].pci_info.function); |
| 227 | out->pci_vendor_id = cards[found].pci_info.vendor_id; |
| 228 | out->pci_device_id = cards[found].pci_info.device_id; |
| 229 | out->rm_compute_class = rm.compute_class; |
| 230 | out->rm_sm_version = rm.sm_version; |
| 231 | memcpy(out->gpu_uuid, rm.gpu_uuid, 16); |
| 232 | |
| 233 | out->observation_flags |= FORGE_OBS_PCI_IDENTITY | |
| 234 | FORGE_OBS_GPU_UUID | |
| 235 | FORGE_OBS_RM_COMPUTE_CLASS | |
| 236 | FORGE_OBS_RM_SM_VERSION; |
| 237 | |
| 238 | /* 3. Query RM GR Info properties (warp size, warps per SM, core count, SM per TPC) */ |
| 239 | NV2080_CTRL_GR_INFO list[8]; |
| 240 | memset(list, 0, sizeof(list)); |
| 241 | list[0].index = NV2080_CTRL_GR_INFO_INDEX_MAX_THREADS_PER_WARP; |
| 242 | list[1].index = NV2080_CTRL_GR_INFO_INDEX_MAX_WARPS_PER_SM; |
| 243 | list[2].index = NV2080_CTRL_GR_INFO_INDEX_GPU_CORE_COUNT; |
| 244 | list[3].index = NV2080_CTRL_GR_INFO_INDEX_LITTER_NUM_SM_PER_TPC; |
| 245 | |
| 246 | NV2080_CTRL_GR_GET_INFO_PARAMS gp; |
| 247 | memset(&gp, 0, sizeof(gp)); |
| 248 | gp.grInfoListSize = 4; |
| 249 | gp.grInfoList = (NvP64)(uintptr_t)list; |
| 250 | |
| 251 | NVOS54_PARAMETERS p; |
| 252 | memset(&p, 0, sizeof(p)); |
| 253 | p.hClient = rm.root; |
| 254 | p.hObject = rm.subdevice; |
| 255 | p.cmd = NV2080_CTRL_CMD_GR_GET_INFO; |
| 256 | p.params = (NvP64)(uintptr_t)&gp; |
| 257 | p.paramsSize = sizeof(gp); |
| 258 | |
| 259 | if (ioctl(rm.fd_ctl, NV_IOWR(NV_ESC_RM_CONTROL, sizeof(p)), &p) == 0 && p.status == 0) { |
| 260 | out->warp_size = list[0].data; |
| 261 | out->max_warps_per_sm = list[1].data; |
| 262 | out->gpu_core_count = list[2].data; |
| 263 | out->sm_per_tpc = list[3].data; |
| 264 | out->observation_flags |= FORGE_OBS_WARP_SIZE | |
| 265 | FORGE_OBS_WARPS_PER_SM | |
| 266 | FORGE_OBS_CORE_COUNT | |
| 267 | FORGE_OBS_SM_PER_TPC; |
| 268 | } else { |
| 269 | out->warp_size = 0; /* unobserved */ |
| 270 | } |
| 271 | |
| 272 | nvrm_close(&rm); |
| 273 | |
| 274 | /* 4. Validate RM compute class: must be Blackwell Compute B (0xcec0) */ |
| 275 | if (out->rm_compute_class != 0xcec0) { |
| 276 | return FORGE_PROBE_ERR_UNSUPPORTED_CC; |
| 277 | } |
| 278 | |
| 279 | /* 5. Cross-validate with sysfs for the SAME observed BDF */ |
| 280 | char path[512]; |
| 281 | uint32_t val = 0; |
| 282 | |
| 283 | snprintf(path, sizeof(path), "/sys/bus/pci/devices/%s/vendor", out->pci_bdf); |
| 284 | if (read_sysfs_u32(path, &val) != 0 || val != out->pci_vendor_id) return FORGE_PROBE_ERR_MALFORMED_PCI; |
| 285 | |
| 286 | snprintf(path, sizeof(path), "/sys/bus/pci/devices/%s/device", out->pci_bdf); |
| 287 | if (read_sysfs_u32(path, &val) != 0 || val != out->pci_device_id) return FORGE_PROBE_ERR_DEVICE_MISMATCH; |
| 288 | |
| 289 | snprintf(path, sizeof(path), "/sys/bus/pci/devices/%s/subsystem_vendor", out->pci_bdf); |
| 290 | if (read_sysfs_u32(path, &val) == 0) out->pci_subsystem_vendor_id = (uint16_t)val; |
| 291 | |
| 292 | snprintf(path, sizeof(path), "/sys/bus/pci/devices/%s/subsystem_device", out->pci_bdf); |
| 293 | if (read_sysfs_u32(path, &val) == 0) out->pci_subsystem_device_id = (uint16_t)val; |
| 294 | |
| 295 | /* 6. Read and cross-validate with procfs for the SAME observed BDF */ |
| 296 | if (read_proc_driver_version(out->driver_version, sizeof(out->driver_version)) != 0) { |
| 297 | return FORGE_PROBE_ERR_PROCFS; |
| 298 | } |
| 299 | out->observation_flags |= FORGE_OBS_DRIVER_VERSION; |
| 300 | |
| 301 | if (read_proc_gpu_info(out->pci_bdf, 0, out->gpu_uuid, |
| 302 | out->vbios_version, sizeof(out->vbios_version), |
| 303 | out->firmware_version, sizeof(out->firmware_version), |
| 304 | &out->dma_mask) != 0) { |
| 305 | return FORGE_PROBE_ERR_PROCFS; |
| 306 | } |
| 307 | out->observation_flags |= FORGE_OBS_VBIOS_VERSION | FORGE_OBS_FIRMWARE_VERSION; |
| 308 | if (out->dma_mask != 0) out->observation_flags |= FORGE_OBS_DMA_MASK; |
| 309 | |
| 310 | /* 7. Read host memory topology */ |
| 311 | if (read_total_memory(&out->total_system_memory_bytes) != 0) { |
| 312 | return FORGE_PROBE_ERR_MISSING_FIELD; |
| 313 | } |
| 314 | out->observation_flags |= FORGE_OBS_TOTAL_MEMORY; |
| 315 | |
| 316 | out->page_size = (uint32_t)sysconf(_SC_PAGESIZE); |
| 317 | if (out->page_size > 0) out->observation_flags |= FORGE_OBS_PAGE_SIZE; |
| 318 | |
| 319 | /* 8. Classified Derived Architectural Invariants (derived from observed SM version) */ |
| 320 | if (out->rm_sm_version == 0x0a04) { |
| 321 | out->max_threads_per_block = 1024; |
| 322 | out->shared_mem_per_block_bytes = 49152; |
| 323 | out->observation_flags |= FORGE_DERIVED_THREADS_PER_BLK | FORGE_DERIVED_SHARED_MEM; |
| 324 | } else { |
| 325 | out->max_threads_per_block = 0; |
| 326 | out->shared_mem_per_block_bytes = 0; |
| 327 | } |
| 328 | |
| 329 | out->features = FORGE_FEATURE_UNIFIED_MEMORY | |
| 330 | FORGE_FEATURE_COMPUTE_B | |
| 331 | FORGE_FEATURE_GPFIFO_A | |
| 332 | FORGE_FEATURE_USERMODE_A | |
| 333 | FORGE_FEATURE_PAGE_FAULTING; |
| 334 | out->observation_flags |= FORGE_DERIVED_FEATURES; |
| 335 | |
| 336 | return forge_descriptor_normalize(out); |
| 337 | } |
| 338 | |
| 339 | int forge_descriptor_normalize(ForgeMachineDescriptor *desc) { |
| 340 | if (!desc) return FORGE_PROBE_ERR_INVALID_ARG; |
| 341 | |
| 342 | /* Check required identities */ |
| 343 | if (desc->pci_vendor_id != 0x10de) return FORGE_PROBE_ERR_MALFORMED_PCI; |
| 344 | if (desc->pci_device_id == 0) return FORGE_PROBE_ERR_MALFORMED_PCI; |
| 345 | if (desc->pci_bdf[0] == '\0') return FORGE_PROBE_ERR_MISSING_FIELD; |
| 346 | if (desc->rm_compute_class == 0) return FORGE_PROBE_ERR_MISSING_FIELD; |
| 347 | if (desc->rm_sm_version == 0) return FORGE_PROBE_ERR_MISSING_FIELD; |
| 348 | if (desc->driver_version[0] == '\0') return FORGE_PROBE_ERR_MISSING_FIELD; |
| 349 | if (desc->firmware_version[0] == '\0') return FORGE_PROBE_ERR_MISSING_FIELD; |
| 350 | |
| 351 | /* Ensure strict null termination and zero unused trailing buffer space */ |
| 352 | desc->pci_bdf[FORGE_DESC_BDF_LEN - 1] = '\0'; |
| 353 | for (size_t i = strlen(desc->pci_bdf); i < FORGE_DESC_BDF_LEN; i++) { |
| 354 | desc->pci_bdf[i] = '\0'; |
| 355 | } |
| 356 | |
| 357 | desc->driver_version[FORGE_DESC_VER_LEN - 1] = '\0'; |
| 358 | for (size_t i = strlen(desc->driver_version); i < FORGE_DESC_VER_LEN; i++) { |
| 359 | desc->driver_version[i] = '\0'; |
| 360 | } |
| 361 | |
| 362 | desc->vbios_version[FORGE_DESC_VER_LEN - 1] = '\0'; |
| 363 | for (size_t i = strlen(desc->vbios_version); i < FORGE_DESC_VER_LEN; i++) { |
| 364 | desc->vbios_version[i] = '\0'; |
| 365 | } |
| 366 | |
| 367 | desc->firmware_version[FORGE_DESC_VER_LEN - 1] = '\0'; |
| 368 | for (size_t i = strlen(desc->firmware_version); i < FORGE_DESC_VER_LEN; i++) { |
| 369 | desc->firmware_version[i] = '\0'; |
| 370 | } |
| 371 | |
| 372 | return FORGE_PROBE_OK; |
| 373 | } |
| 374 | |
| 375 | size_t forge_descriptor_serialize(const ForgeMachineDescriptor *desc, uint8_t *out_buf, size_t max_len) { |
| 376 | if (!desc || !out_buf || max_len < FORGE_DESC_SERIALIZED_LEN) return 0; |
| 377 | memset(out_buf, 0, FORGE_DESC_SERIALIZED_LEN); |
| 378 | |
| 379 | /* |
| 380 | * Explicit little-endian canonical serialization (208 bytes): |
| 381 | * 0x00: pci_vendor_id (2) |
| 382 | * 0x02: pci_device_id (2) |
| 383 | * 0x04: pci_subsystem_vendor_id (2) |
| 384 | * 0x06: pci_subsystem_device_id (2) |
| 385 | * 0x08: pci_bdf (16) |
| 386 | * 0x18: gpu_uuid (16) |
| 387 | * 0x28: rm_compute_class (4) |
| 388 | * 0x2c: rm_sm_version (4) |
| 389 | * 0x30: warp_size (4) |
| 390 | * 0x34: max_warps_per_sm (4) |
| 391 | * 0x38: gpu_core_count (4) |
| 392 | * 0x3c: sm_per_tpc (4) |
| 393 | * 0x40: driver_version (32) |
| 394 | * 0x60: vbios_version (32) |
| 395 | * 0x80: firmware_version (32) |
| 396 | * 0xa0: total_system_memory_bytes (8) |
| 397 | * 0xa8: dma_mask (8) |
| 398 | * 0xb0: page_size (4) |
| 399 | * 0xb4: max_threads_per_block (4) |
| 400 | * 0xb8: shared_mem_per_block_bytes (4) |
| 401 | * 0xbc: features (4) |
| 402 | * 0xc0: observation_flags (4) |
| 403 | * 0xc4..0xcf: zero reserved (12) |
| 404 | */ |
| 405 | put_u16_le(out_buf + 0x00, desc->pci_vendor_id); |
| 406 | put_u16_le(out_buf + 0x02, desc->pci_device_id); |
| 407 | put_u16_le(out_buf + 0x04, desc->pci_subsystem_vendor_id); |
| 408 | put_u16_le(out_buf + 0x06, desc->pci_subsystem_device_id); |
| 409 | memcpy(out_buf + 0x08, desc->pci_bdf, FORGE_DESC_BDF_LEN); |
| 410 | memcpy(out_buf + 0x18, desc->gpu_uuid, FORGE_DESC_UUID_LEN); |
| 411 | put_u32_le(out_buf + 0x28, desc->rm_compute_class); |
| 412 | put_u32_le(out_buf + 0x2c, desc->rm_sm_version); |
| 413 | put_u32_le(out_buf + 0x30, desc->warp_size); |
| 414 | put_u32_le(out_buf + 0x34, desc->max_warps_per_sm); |
| 415 | put_u32_le(out_buf + 0x38, desc->gpu_core_count); |
| 416 | put_u32_le(out_buf + 0x3c, desc->sm_per_tpc); |
| 417 | memcpy(out_buf + 0x40, desc->driver_version, FORGE_DESC_VER_LEN); |
| 418 | memcpy(out_buf + 0x60, desc->vbios_version, FORGE_DESC_VER_LEN); |
| 419 | memcpy(out_buf + 0x80, desc->firmware_version, FORGE_DESC_VER_LEN); |
| 420 | put_u64_le(out_buf + 0xa0, desc->total_system_memory_bytes); |
| 421 | put_u64_le(out_buf + 0xa8, desc->dma_mask); |
| 422 | put_u32_le(out_buf + 0xb0, desc->page_size); |
| 423 | put_u32_le(out_buf + 0xb4, desc->max_threads_per_block); |
| 424 | put_u32_le(out_buf + 0xb8, desc->shared_mem_per_block_bytes); |
| 425 | put_u32_le(out_buf + 0xbc, desc->features); |
| 426 | put_u32_le(out_buf + 0xc0, desc->observation_flags); |
| 427 | |
| 428 | return FORGE_DESC_SERIALIZED_LEN; |
| 429 | } |
| 430 | |
| 431 | int forge_descriptor_compute_digest(const ForgeMachineDescriptor *desc, uint8_t out_digest[FORGE_DESC_DIGEST_LEN]) { |
| 432 | if (!desc || !out_digest) return FORGE_PROBE_ERR_INVALID_ARG; |
| 433 | |
| 434 | ForgeMachineDescriptor norm = *desc; |
| 435 | int rc = forge_descriptor_normalize(&norm); |
| 436 | if (rc != FORGE_PROBE_OK) return rc; |
| 437 | |
| 438 | uint8_t serialized[FORGE_DESC_SERIALIZED_LEN]; |
| 439 | if (forge_descriptor_serialize(&norm, serialized, sizeof(serialized)) != FORGE_DESC_SERIALIZED_LEN) { |
| 440 | return FORGE_PROBE_ERR_INVALID_ARG; |
| 441 | } |
| 442 | |
| 443 | sha256_bytes(serialized, sizeof(serialized), out_digest); |
| 444 | return FORGE_PROBE_OK; |
| 445 | } |
| 446 | |
| 447 | int forge_descriptor_run_kat(void) { |
| 448 | /* Frozen Known Answer Test (KAT) descriptor */ |
| 449 | ForgeMachineDescriptor kat_desc; |
| 450 | memset(&kat_desc, 0, sizeof(kat_desc)); |
| 451 | |
| 452 | kat_desc.pci_vendor_id = 0x10de; |
| 453 | kat_desc.pci_device_id = 0x2e12; |
| 454 | kat_desc.pci_subsystem_vendor_id = 0x10de; |
| 455 | kat_desc.pci_subsystem_device_id = 0x0000; |
| 456 | snprintf(kat_desc.pci_bdf, sizeof(kat_desc.pci_bdf), "000f:01:00.0"); |
| 457 | for (int i = 0; i < 16; i++) kat_desc.gpu_uuid[i] = (uint8_t)(i + 1); |
| 458 | kat_desc.rm_compute_class = 0x0000cec0; |
| 459 | kat_desc.rm_sm_version = 0x00000a04; |
| 460 | kat_desc.warp_size = 32; |
| 461 | kat_desc.max_warps_per_sm = 48; |
| 462 | kat_desc.gpu_core_count = 6144; |
| 463 | kat_desc.sm_per_tpc = 2; |
| 464 | snprintf(kat_desc.driver_version, sizeof(kat_desc.driver_version), "580.173.02"); |
| 465 | snprintf(kat_desc.vbios_version, sizeof(kat_desc.vbios_version), "9a.0b.2d.00.00"); |
| 466 | snprintf(kat_desc.firmware_version, sizeof(kat_desc.firmware_version), "580.173.02"); |
| 467 | kat_desc.total_system_memory_bytes = 130660888576ULL; |
| 468 | kat_desc.dma_mask = 0x1ffffffffffULL; |
| 469 | kat_desc.page_size = 4096; |
| 470 | kat_desc.max_threads_per_block = 1024; |
| 471 | kat_desc.shared_mem_per_block_bytes = 49152; |
| 472 | kat_desc.features = 0x1f; |
| 473 | kat_desc.observation_flags = 0x1ffff; |
| 474 | |
| 475 | uint8_t serialized[FORGE_DESC_SERIALIZED_LEN]; |
| 476 | if (forge_descriptor_serialize(&kat_desc, serialized, sizeof(serialized)) != FORGE_DESC_SERIALIZED_LEN) { |
| 477 | return FORGE_PROBE_ERR_KAT_FAILURE; |
| 478 | } |
| 479 | |
| 480 | uint8_t digest[32]; |
| 481 | sha256_bytes(serialized, sizeof(serialized), digest); |
| 482 | |
| 483 | char hex[65]; |
| 484 | forge_descriptor_digest_hex(digest, hex); |
| 485 | |
| 486 | const char *expected_kat_digest = "10d63d05f888eaba4fe473c5f17febe22f462f543ae0c2b005a9a8e5417e09fd"; |
| 487 | if (strcmp(hex, expected_kat_digest) != 0) { |
| 488 | fprintf(stderr, "ERROR: FORGE descriptor KAT mismatch: expected %s, got %s\n", |
| 489 | expected_kat_digest, hex); |
| 490 | return FORGE_PROBE_ERR_KAT_FAILURE; |
| 491 | } |
| 492 | return FORGE_PROBE_OK; |
| 493 | } |
| 494 | |
| 495 | const char *forge_descriptor_derived_alias(const ForgeMachineDescriptor *desc) { |
| 496 | if (!desc) return NULL; |
| 497 | if (desc->rm_compute_class == 0xcec0 && desc->rm_sm_version == 0x0a04) { |
| 498 | return "sm_121"; |
| 499 | } |
| 500 | return NULL; |
| 501 | } |
| 502 | |
| 503 | int forge_descriptor_verify_alias(const ForgeMachineDescriptor *desc, const char *alias) { |
| 504 | if (!desc || !alias) return FORGE_PROBE_ERR_INVALID_ARG; |
| 505 | const char *derived = forge_descriptor_derived_alias(desc); |
| 506 | if (!derived) return FORGE_PROBE_ERR_UNBOUND_ALIAS; |
| 507 | if (strcmp(derived, alias) != 0) return FORGE_PROBE_ERR_UNBOUND_ALIAS; |
| 508 | return FORGE_PROBE_OK; |
| 509 | } |
| 510 | |
| 511 | void forge_descriptor_digest_hex(const uint8_t digest[FORGE_DESC_DIGEST_LEN], char out_hex[65]) { |
| 512 | if (!digest || !out_hex) return; |
| 513 | for (int i = 0; i < FORGE_DESC_DIGEST_LEN; i++) { |
| 514 | snprintf(&out_hex[i * 2], 3, "%02x", digest[i]); |
| 515 | } |
| 516 | out_hex[64] = '\0'; |
| 517 | } |