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_boot_args_encode.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* ck_gsp_boot_args_encode -- host tool: byte-exact encoder, decoder and |
| 18 | * refusal tests for the system-memory argument blocks the pinned Linux |
| 19 | * driver (nvidia-open 580.173.02) prepares before it boots GSP-RM on the |
| 20 | * GB10B (aienos#286 cut C host work: "parsers and refusal tests for firmware |
| 21 | * metadata. No loading"). docs/GB10_FIRMWARE_INVENTORY.md section 12 is the |
| 22 | * report this tool backs. Nothing here touches a device. |
| 23 | * |
| 24 | * Five blocks, every line from open-gpu-kernel-modules tag 580.173.02: |
| 25 | * |
| 26 | * 1. GspFwWprMeta, 256 bytes, natural layout (gsp_fw_wpr_meta.h:60-208, |
| 27 | * ct_assert(sizeof == 256) in kernel_gsp_gh100.c:305). Allocated as one |
| 28 | * 4 KiB page, 4 KiB aligned, cached, unprotected sysmem, zeroed |
| 29 | * (kernel_gsp_tu102.c:120-142). Filled by kgspPopulateWprMeta_GH100 |
| 30 | * (kernel_gsp_gh100.c:295-388) then overridden by |
| 31 | * kgspPopulateWprMeta_GB10B (kernel_gsp_gb10b.c:62-86): |
| 32 | * magic 0xdc3aae21371a60b3, revision 1 [gh100.c:375-376] |
| 33 | * sizeOfBootloader = GspRmBoot image size (196,608) [:321] |
| 34 | * sysmemAddrOfBootloader = its 4 KiB-aligned PA [:322-323] |
| 35 | * sizeOfRadix3Elf = GSP-RM .fwimage size [:326] |
| 36 | * sysmemAddrOfRadix3Elf = PA of the radix-3 root page [:327-328] |
| 37 | * bootloaderCodeOffset/DataOffset/ManifestOffset = |
| 38 | * desc monitorCodeOffset/monitorDataOffset/manifestOffset |
| 39 | * (GB10B: 0xb000 / 0xa00 / 0x0) [:331-333] |
| 40 | * sysmemAddrOfSignature (256-aligned PA), sizeOfSignature = |
| 41 | * NV_ALIGN_UP(signatureSize, 256) [:335-339; kernel_gsp.c:4697-4701] |
| 42 | * nonWprHeapSize = 0 (GB10B override) [gb10b.c:82] |
| 43 | * gspFwHeapSize = kgspGetFwHeapSize(.., 0, 0) [:355] |
| 44 | * frtsSize = 0 (kgspGetFrtsSize_4a4dee for GB10B) [:358; g_kernel_gsp_nvoc.c:1254-1256, .h:1781-1783] |
| 45 | * gspFwHeapVfPartitionCount = 0 (no vGPU offload) [:361-362] |
| 46 | * sysmemAddrOfCrashReportQueue/size: crash-cat queue if allocated [:364-372] |
| 47 | * vgaWorkspaceSize = 0, pmuReservedSize = 0 (GB10B) [gb10b.c:78-79] |
| 48 | * flags: PPCIE cleared; SCAN_RECOVERY_MARGIN only on a retry with |
| 49 | * ECC disabled [:378-385; kernel_gsp.c:3935-3943] |
| 50 | * everything else 0: the FMC/ACR patch the FB layout [gb10b.c:33-35] |
| 51 | * Heap size on GB10B (kernel_gsp.c:5538-5593, 5617-5668): ZERO_FB is |
| 52 | * true for "GB10B | GB20B | GB20C" (g_gpu_nvoc.c) so memSizeGB = 1; |
| 53 | * heap = OS carveout 22 MiB (kgspGetFwHeapParamOsCarveoutSize_4b5307, |
| 54 | * non-vGPU) + base 14 MiB (fwHeapParamBaseSize, Hopper+ list) + |
| 55 | * align1M(96 KiB * 1) + align1M(48 KiB * 2048) = 22+14+1+96 = 133 MiB, |
| 56 | * clamped to [88 MiB, U32_MAX] (min kgspGetMinWprHeapSizeMB_647ce6 = 88 |
| 57 | * non-vGPU; the override path is off by default, |
| 58 | * NV_REG_STR_GSP_FIRMWARE_HEAP_SIZE_MB_DEFAULT = 0). Registry overrides |
| 59 | * would be clamped to [88, 280] MiB (gsp_fw_heap.h:109-110). |
| 60 | * |
| 61 | * 2. Libos init arguments: an array of LibosMemoryRegionInitArgument |
| 62 | * (libos_init_args.h: id8 u64, pa u64, size u64, kind u8, loc u8; |
| 63 | * natural size 32) in one 4 KiB uncached, 4 KiB-aligned, zeroed page |
| 64 | * (LIBOS_MEMORY_REGION_INIT_ARGUMENTS_MAX = 4096; kernel_gsp_tu102.c: |
| 65 | * 147-171). kgspSetupLibosInitArgs_IMPL (kernel_gsp.c:5179-5222): one |
| 66 | * CONTIGUOUS/SYSMEM entry per log buffer in registration order, id8 = |
| 67 | * the memory id packed big-endian (_kgspGenerateInitArgId, :2657-2675), |
| 68 | * pa = the buffer's first page PA, size = buffer size; then "RMARGS" -> |
| 69 | * the GSP_ARGUMENTS_CACHED page (size 4096). Remaining entries stay |
| 70 | * zero (kind NONE). GB10B log set (libos 3: kgspGetLibosVersion_61db9f; |
| 71 | * release build; kernel logging true for GB10B, .fwlogging_kernel_gb10y |
| 72 | * exists) in order (kernel_gsp.c:3150-3240, nv-firmware-chip-family- |
| 73 | * select.h:105-115,151-152): LOGINIT 64 KiB, LOGINTR 64 KiB, LOGRM |
| 74 | * 64 KiB, LOGMNOC 64 KiB, LOGDBG 64 KiB, LOGROOT 4 KiB, LOGKRNL 64 KiB |
| 75 | * (= kgspGetLogCount LOG_IDX_LIBOS3_SIZE = 7), then RMARGS: 8 entries. |
| 76 | * |
| 77 | * 3. Each log buffer: contiguous cached sysmem, 4 KiB aligned, zeroed; the |
| 78 | * host writes its own PTE table into it after an 8-byte put pointer |
| 79 | * (kernel_gsp.c:3070-3104 "Pass the PTE table for the log buffer in the |
| 80 | * log buffer, after the put pointer"): u64 put = 0, then one u64 PA per |
| 81 | * 4 KiB page of the buffer. The init-argument pa is PTE[0] = the base. |
| 82 | * |
| 83 | * 4. GSP_ARGUMENTS_CACHED (gsp_init_args.h), natural layout 88 bytes, in |
| 84 | * one 4 KiB cached, 4 KiB-aligned, zeroed page (kernel_gsp_tu102.c: |
| 85 | * 174-198). kgspPopulateGspRmInitArgs_IMPL (kernel_gsp.c:4505-4573): |
| 86 | * messageQueue: sharedMemPhysAddr = PA of page 0 of the RPC shared |
| 87 | * buffer (the page-table page), pageTableEntryCount, cmdQueueOffset = |
| 88 | * pageTableSize, statQueueOffset = cmdQueueOffset + commandQueueSize; |
| 89 | * sr: all 0 on a normal boot; gpuInstance; bDmemStack = true (stack |
| 90 | * placement default 0); profilerArgs 0; sysmemHeapArgs = PA/size of the |
| 91 | * ZERO_FB sysmem heap (512 MiB default, contiguous uncached; |
| 92 | * kernel_gsp_tu102.c:200-223, nvrm_registry.h:2355-2356). |
| 93 | * RPC shared buffer (message_queue_cpu.c:60-125, 220-310): command |
| 94 | * queue 256 KiB on silicon, status queue 256 KiB; numPtes = 128 + |
| 95 | * ceil(128*8/4096) = 129; pageTableSize = align4K(129*8) = 4096; the |
| 96 | * table page holds the PAs of all 129 pages (itself first); command |
| 97 | * queue at 4096, status queue at 266240; sharedMemPA = PTE[0]. |
| 98 | * |
| 99 | * 5. Radix-3 page table for the GSP-RM ELF (kgspCreateRadix3, kernel_gsp.c: |
| 100 | * 4915-5050): 4 KiB pages, 512 entries per page; level 3 = data pages, |
| 101 | * level i-1 pages = ceil(level i pages / 512); level 0 must be 1 page; |
| 102 | * levels 0..2 are laid out back to back, then the data; each level |
| 103 | * page holds the PAs of the next level's pages. sizeOfRadix3Elf is the |
| 104 | * ELF size and sysmemAddrOfRadix3Elf the PA of the level-0 page. |
| 105 | * |
| 106 | * Refusals this tool enforces (reason strings are the test oracle): bad |
| 107 | * magic/revision, bootloader size/offsets not the GB10B pair, unaligned or |
| 108 | * zero addresses (4 KiB for pages, 256 for the signature), signature size |
| 109 | * not a 256 multiple, zero ELF size, heap size outside [88 MiB, 280 MiB], |
| 110 | * GB10B-must-be-zero fields non-zero (nonWprHeapSize, frtsSize, |
| 111 | * vgaWorkspaceSize, pmuReservedSize, VF count), FMC-owned fields non-zero |
| 112 | * on the host side (FB layout, bootCount, verified), unknown flag bits, |
| 113 | * libos entries out of order or of the wrong kind/loc/size, RMARGS not last, |
| 114 | * message-queue arguments inconsistent, log buffer size not a page multiple. |
| 115 | * |
| 116 | * INFERRED (medium), not a driver check: the GSP-FMC accepts the 133 MiB |
| 117 | * heap request on GB10B exactly as the driver computes it; a different value |
| 118 | * within the clamp is a change of behaviour that only the chip can judge. |
| 119 | * |
| 120 | * No device access, no firmware, no loading. Hosted build only. |
| 121 | */ |
| 122 | #include <stdint.h> |
| 123 | #include <stdio.h> |
| 124 | #include <stdlib.h> |
| 125 | #include <string.h> |
| 126 | #include <stddef.h> |
| 127 | |
| 128 | /* ---------- the driver's structures, natural layout (LP64, little endian) ---------- */ |
| 129 | |
| 130 | #define GSP_FW_WPR_META_MAGIC 0xdc3aae21371a60b3ULL |
| 131 | #define GSP_FW_WPR_META_REVISION 1ULL |
| 132 | #define GSP_FW_WPR_META_VERIFIED 0xa0a0a0a0a0a0a0a0ULL |
| 133 | #define GSP_FW_FLAGS_CLOCK_BOOST (1u << 0) |
| 134 | #define GSP_FW_FLAGS_RECOVERY_MARGIN_PRESENT (1u << 1) |
| 135 | #define GSP_FW_FLAGS_PPCIE_ENABLED (1u << 2) |
| 136 | #define GSP_FW_FLAGS_MULTI_GPU_NVLE_ENABLED (1u << 3) |
| 137 | #define GSP_FW_FLAGS_SCAN_RECOVERY_MARGIN (1u << 4) |
| 138 | |
| 139 | typedef struct { |
| 140 | uint64_t magic; |
| 141 | uint64_t revision; |
| 142 | uint64_t sysmemAddrOfRadix3Elf; |
| 143 | uint64_t sizeOfRadix3Elf; |
| 144 | uint64_t sysmemAddrOfBootloader; |
| 145 | uint64_t sizeOfBootloader; |
| 146 | uint64_t bootloaderCodeOffset; |
| 147 | uint64_t bootloaderDataOffset; |
| 148 | uint64_t bootloaderManifestOffset; |
| 149 | union { |
| 150 | struct { uint64_t sysmemAddrOfSignature; uint64_t sizeOfSignature; }; |
| 151 | struct { uint32_t gspFwHeapFreeListWprOffset; uint32_t unused0; uint64_t unused1; }; |
| 152 | }; |
| 153 | uint64_t gspFwRsvdStart; |
| 154 | uint64_t nonWprHeapOffset; |
| 155 | uint64_t nonWprHeapSize; |
| 156 | uint64_t gspFwWprStart; |
| 157 | uint64_t gspFwHeapOffset; |
| 158 | uint64_t gspFwHeapSize; |
| 159 | uint64_t gspFwOffset; |
| 160 | uint64_t bootBinOffset; |
| 161 | uint64_t frtsOffset; |
| 162 | uint64_t frtsSize; |
| 163 | uint64_t gspFwWprEnd; |
| 164 | uint64_t fbSize; |
| 165 | uint64_t vgaWorkspaceOffset; |
| 166 | uint64_t vgaWorkspaceSize; |
| 167 | uint64_t bootCount; |
| 168 | union { |
| 169 | struct { |
| 170 | uint64_t partitionRpcAddr; |
| 171 | uint16_t partitionRpcRequestOffset; |
| 172 | uint16_t partitionRpcReplyOffset; |
| 173 | uint32_t elfCodeOffset; |
| 174 | uint32_t elfDataOffset; |
| 175 | uint32_t elfCodeSize; |
| 176 | uint32_t elfDataSize; |
| 177 | uint32_t lsUcodeVersion; |
| 178 | }; |
| 179 | struct { |
| 180 | uint32_t partitionRpcPadding[4]; |
| 181 | uint64_t sysmemAddrOfCrashReportQueue; |
| 182 | uint32_t sizeOfCrashReportQueue; |
| 183 | uint32_t lsUcodeVersionPadding[1]; |
| 184 | }; |
| 185 | }; |
| 186 | uint8_t gspFwHeapVfPartitionCount; |
| 187 | uint8_t flags; |
| 188 | uint8_t padding[2]; |
| 189 | uint32_t pmuReservedSize; |
| 190 | uint64_t verified; |
| 191 | } GspFwWprMeta; |
| 192 | |
| 193 | #define LIBOS_MEMORY_REGION_INIT_ARGUMENTS_MAX 4096 |
| 194 | enum { LIBOS_MEMORY_REGION_NONE = 0, LIBOS_MEMORY_REGION_CONTIGUOUS = 1, LIBOS_MEMORY_REGION_RADIX3 = 2 }; |
| 195 | enum { LIBOS_MEMORY_REGION_LOC_NONE = 0, LIBOS_MEMORY_REGION_LOC_SYSMEM = 1, LIBOS_MEMORY_REGION_LOC_FB = 2 }; |
| 196 | typedef struct { |
| 197 | uint64_t id8; |
| 198 | uint64_t pa; |
| 199 | uint64_t size; |
| 200 | uint8_t kind; |
| 201 | uint8_t loc; |
| 202 | } LibosMemoryRegionInitArgument; |
| 203 | |
| 204 | typedef struct { |
| 205 | uint64_t sharedMemPhysAddr; |
| 206 | uint32_t pageTableEntryCount; |
| 207 | uint64_t cmdQueueOffset; /* NvLength = NvU64 on LP64 (nvtypes.h:309) */ |
| 208 | uint64_t statQueueOffset; |
| 209 | } MESSAGE_QUEUE_INIT_ARGUMENTS; |
| 210 | typedef struct { |
| 211 | uint32_t oldLevel; |
| 212 | uint32_t flags; |
| 213 | uint8_t bInPMTransition; /* NvBool */ |
| 214 | } GSP_SR_INIT_ARGUMENTS; |
| 215 | typedef struct { |
| 216 | MESSAGE_QUEUE_INIT_ARGUMENTS messageQueueInitArguments; |
| 217 | GSP_SR_INIT_ARGUMENTS srInitArguments; |
| 218 | uint32_t gpuInstance; |
| 219 | uint8_t bDmemStack; |
| 220 | struct { uint64_t pa; uint64_t size; } profilerArgs; |
| 221 | struct { uint64_t pa; uint64_t size; } sysmemHeapArgs; |
| 222 | } GSP_ARGUMENTS_CACHED; |
| 223 | |
| 224 | /* ---------- GB10B constants traced from the driver ---------- */ |
| 225 | |
| 226 | #define PAGE_SZ 4096ULL |
| 227 | #define GB10B_BOOTLOADER_SIZE 196608ULL /* GspRmBoot_GB10B UCODE_IMAGE_PROD */ |
| 228 | #define GB10B_BL_CODE_OFF 0xb000ULL /* desc monitorCodeOffset */ |
| 229 | #define GB10B_BL_DATA_OFF 0xa00ULL /* desc monitorDataOffset */ |
| 230 | #define GB10B_BL_MANIFEST_OFF 0x0ULL /* desc manifestOffset */ |
| 231 | #define GB10B_FW_HEAP_SIZE (133ULL << 20) |
| 232 | #define GB10B_FW_HEAP_MIN (88ULL << 20) |
| 233 | #define GB10B_FW_HEAP_MAX (280ULL << 20) |
| 234 | #define GB10B_SYSMEM_HEAP (512ULL << 20) |
| 235 | #define MQ_CMD_QUEUE_SIZE 0x40000ULL |
| 236 | #define MQ_STAT_QUEUE_SIZE 0x40000ULL |
| 237 | #define MQ_NUM_PTES 129u |
| 238 | #define MQ_PAGE_TABLE_SIZE 4096ULL |
| 239 | #define GSP_FW_FLAGS_KNOWN (GSP_FW_FLAGS_CLOCK_BOOST | GSP_FW_FLAGS_RECOVERY_MARGIN_PRESENT | \ |
| 240 | GSP_FW_FLAGS_PPCIE_ENABLED | GSP_FW_FLAGS_MULTI_GPU_NVLE_ENABLED | \ |
| 241 | GSP_FW_FLAGS_SCAN_RECOVERY_MARGIN) |
| 242 | |
| 243 | static const struct { const char *id; uint64_t size; } gb10b_logs[] = { |
| 244 | {"LOGINIT", 0x10000}, {"LOGINTR", 0x10000}, {"LOGRM", 0x10000}, {"LOGMNOC", 0x10000}, |
| 245 | {"LOGDBG", 0x10000}, {"LOGROOT", 0x1000}, {"LOGKRNL", 0x10000}, |
| 246 | }; |
| 247 | #define GB10B_LOG_COUNT 7u |
| 248 | #define GB10B_LIBOS_ENTRIES (GB10B_LOG_COUNT + 1u) |
| 249 | |
| 250 | /* _kgspGenerateInitArgId: at most 8 ASCII bytes, first byte most significant. */ |
| 251 | static uint64_t init_arg_id(const char *name) { |
| 252 | uint64_t id = 0; |
| 253 | for (int i = 0; i < 8; i++) { |
| 254 | unsigned char c = (unsigned char)*name++; |
| 255 | if (c == '\0') break; |
| 256 | id = (id << 8) | c; |
| 257 | } |
| 258 | return id; |
| 259 | } |
| 260 | |
| 261 | /* ---------- little-endian helpers over raw bytes ---------- */ |
| 262 | static uint64_t rd64(const uint8_t *p) { uint64_t v = 0; for (int i = 7; i >= 0; i--) v = (v << 8) | p[i]; return v; } |
| 263 | static uint32_t rd32(const uint8_t *p) { return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); } |
| 264 | static void wr64(uint8_t *p, uint64_t v) { for (int i = 0; i < 8; i++) { p[i] = (uint8_t)v; v >>= 8; } } |
| 265 | static void wr32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) { p[i] = (uint8_t)v; v >>= 8; } } |
| 266 | |
| 267 | /* ---------- 1. GspFwWprMeta ---------- */ |
| 268 | |
| 269 | typedef struct { |
| 270 | uint64_t bootloader_pa, radix3_root_pa, elf_size, signature_pa, signature_size; |
| 271 | uint64_t heap_size; /* 0 = GB10B default 133 MiB */ |
| 272 | uint64_t crash_queue_pa; uint32_t crash_queue_size; |
| 273 | int scan_recovery_margin; /* retry with ECC disabled only */ |
| 274 | } wpr_in; |
| 275 | |
| 276 | static const char *wpr_encode(const wpr_in *in, uint8_t out[256]) { |
| 277 | GspFwWprMeta m; |
| 278 | memset(&m, 0, sizeof m); |
| 279 | if (in->bootloader_pa == 0 || (in->bootloader_pa & (PAGE_SZ - 1))) return "bootloader address zero or not 4 KiB aligned"; |
| 280 | if (in->radix3_root_pa == 0 || (in->radix3_root_pa & (PAGE_SZ - 1))) return "radix3 root address zero or not 4 KiB aligned"; |
| 281 | if (in->elf_size == 0) return "ELF size zero"; |
| 282 | if (in->signature_pa == 0 || (in->signature_pa & 255)) return "signature address zero or not 256 aligned"; |
| 283 | if (in->signature_size == 0 || (in->signature_size & 255)) return "signature size zero or not a 256 multiple"; |
| 284 | uint64_t heap = in->heap_size ? in->heap_size : GB10B_FW_HEAP_SIZE; |
| 285 | if (heap < GB10B_FW_HEAP_MIN || heap > GB10B_FW_HEAP_MAX || (heap & ((1ULL << 20) - 1))) return "heap size outside [88 MiB, 280 MiB] or not a MiB multiple"; |
| 286 | if ((in->crash_queue_pa == 0) != (in->crash_queue_size == 0)) return "crash report queue address and size must both be zero or both set"; |
| 287 | if (in->crash_queue_pa & (PAGE_SZ - 1)) return "crash report queue address not 4 KiB aligned"; |
| 288 | m.magic = GSP_FW_WPR_META_MAGIC; |
| 289 | m.revision = GSP_FW_WPR_META_REVISION; |
| 290 | m.sizeOfBootloader = GB10B_BOOTLOADER_SIZE; |
| 291 | m.sysmemAddrOfBootloader = in->bootloader_pa; |
| 292 | m.sizeOfRadix3Elf = in->elf_size; |
| 293 | m.sysmemAddrOfRadix3Elf = in->radix3_root_pa; |
| 294 | m.bootloaderCodeOffset = GB10B_BL_CODE_OFF; |
| 295 | m.bootloaderDataOffset = GB10B_BL_DATA_OFF; |
| 296 | m.bootloaderManifestOffset = GB10B_BL_MANIFEST_OFF; |
| 297 | m.sysmemAddrOfSignature = in->signature_pa; |
| 298 | m.sizeOfSignature = in->signature_size; |
| 299 | m.nonWprHeapSize = 0; |
| 300 | m.gspFwHeapSize = heap; |
| 301 | m.frtsSize = 0; |
| 302 | m.gspFwHeapVfPartitionCount = 0; |
| 303 | m.sysmemAddrOfCrashReportQueue = in->crash_queue_pa; |
| 304 | m.sizeOfCrashReportQueue = in->crash_queue_size; |
| 305 | m.vgaWorkspaceSize = 0; |
| 306 | m.pmuReservedSize = 0; |
| 307 | m.flags = in->scan_recovery_margin ? GSP_FW_FLAGS_SCAN_RECOVERY_MARGIN : 0; |
| 308 | memcpy(out, &m, 256); |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | /* Decode + refuse: the host-side shape of a fresh-boot GB10B WprMeta. */ |
| 313 | static const char *wpr_check(const uint8_t *b, size_t n, wpr_in *out) { |
| 314 | if (n != 256) return "WprMeta size not 256"; |
| 315 | if (rd64(b + 0) != GSP_FW_WPR_META_MAGIC) return "magic wrong"; |
| 316 | if (rd64(b + 8) != GSP_FW_WPR_META_REVISION) return "revision not 1"; |
| 317 | uint64_t r3 = rd64(b + 16), elf = rd64(b + 24), bl = rd64(b + 32), bls = rd64(b + 40); |
| 318 | if (r3 == 0 || (r3 & (PAGE_SZ - 1))) return "radix3 root address zero or not 4 KiB aligned"; |
| 319 | if (elf == 0) return "ELF size zero"; |
| 320 | if (bl == 0 || (bl & (PAGE_SZ - 1))) return "bootloader address zero or not 4 KiB aligned"; |
| 321 | if (bls != GB10B_BOOTLOADER_SIZE) return "bootloader size not the GB10B GspRmBoot image size"; |
| 322 | if (rd64(b + 48) != GB10B_BL_CODE_OFF || rd64(b + 56) != GB10B_BL_DATA_OFF || rd64(b + 64) != GB10B_BL_MANIFEST_OFF) return "bootloader offsets not the GB10B descriptor values"; |
| 323 | uint64_t sig = rd64(b + 72), sigs = rd64(b + 80); |
| 324 | if (sig == 0 || (sig & 255)) return "signature address zero or not 256 aligned"; |
| 325 | if (sigs == 0 || (sigs & 255)) return "signature size zero or not a 256 multiple"; |
| 326 | /* FB layout fields are the FMC's: gspFwRsvdStart 88, nonWprHeapOffset 96, gspFwWprStart 112, |
| 327 | gspFwHeapOffset 120, gspFwOffset 136, bootBinOffset 144, frtsOffset 152, gspFwWprEnd 168, |
| 328 | fbSize 176, vgaWorkspaceOffset 184 */ |
| 329 | static const int fmc_owned[] = {88, 96, 112, 120, 136, 144, 152, 168, 176, 184}; |
| 330 | for (size_t i = 0; i < sizeof fmc_owned / sizeof fmc_owned[0]; i++) |
| 331 | if (rd64(b + fmc_owned[i]) != 0) return "FMC-owned FB layout field non-zero on the host side"; |
| 332 | if (rd64(b + 104) != 0) return "nonWprHeapSize must be 0 on GB10B"; |
| 333 | uint64_t heap = rd64(b + 128); |
| 334 | if (heap < GB10B_FW_HEAP_MIN || heap > GB10B_FW_HEAP_MAX || (heap & ((1ULL << 20) - 1))) return "heap size outside [88 MiB, 280 MiB] or not a MiB multiple"; |
| 335 | if (rd64(b + 160) != 0) return "frtsSize must be 0 on GB10B"; |
| 336 | if (rd64(b + 192) != 0) return "vgaWorkspaceSize must be 0 on GB10B"; |
| 337 | if (rd64(b + 200) != 0) return "bootCount must be 0 on the host side"; |
| 338 | /* union at 208: partitionRpc* zero (bootstrapper-owned) = padding[0..3] zero */ |
| 339 | for (int i = 0; i < 16; i++) if (b[208 + i] != 0) return "partition RPC fields must be 0"; |
| 340 | uint64_t cq = rd64(b + 224); uint32_t cqs = rd32(b + 232); |
| 341 | if ((cq == 0) != (cqs == 0)) return "crash report queue address and size must both be zero or both set"; |
| 342 | if (cq & (PAGE_SZ - 1)) return "crash report queue address not 4 KiB aligned"; |
| 343 | if (rd32(b + 236) != 0) return "lsUcodeVersion padding must be 0"; |
| 344 | if (b[240] != 0) return "gspFwHeapVfPartitionCount must be 0"; |
| 345 | if (b[241] & ~GSP_FW_FLAGS_KNOWN) return "unknown flag bits"; |
| 346 | if (b[241] & (GSP_FW_FLAGS_PPCIE_ENABLED | GSP_FW_FLAGS_MULTI_GPU_NVLE_ENABLED | GSP_FW_FLAGS_CLOCK_BOOST | GSP_FW_FLAGS_RECOVERY_MARGIN_PRESENT)) return "flag bit the host never sets on GB10B"; |
| 347 | if (b[242] != 0 || b[243] != 0) return "padding must be 0"; |
| 348 | if (rd32(b + 244) != 0) return "pmuReservedSize must be 0 on GB10B"; |
| 349 | if (rd64(b + 248) != 0) return "verified must be 0 on the host side"; |
| 350 | if (out) { |
| 351 | memset(out, 0, sizeof *out); |
| 352 | out->bootloader_pa = bl; out->radix3_root_pa = r3; out->elf_size = elf; |
| 353 | out->signature_pa = sig; out->signature_size = sigs; out->heap_size = heap; |
| 354 | out->crash_queue_pa = cq; out->crash_queue_size = cqs; |
| 355 | out->scan_recovery_margin = (b[241] & GSP_FW_FLAGS_SCAN_RECOVERY_MARGIN) != 0; |
| 356 | } |
| 357 | return NULL; |
| 358 | } |
| 359 | |
| 360 | /* ---------- 2. libos init arguments ---------- */ |
| 361 | |
| 362 | typedef struct { uint64_t log_pa[GB10B_LOG_COUNT]; uint64_t rmargs_pa; } libos_in; |
| 363 | |
| 364 | static const char *libos_encode(const libos_in *in, uint8_t out[LIBOS_MEMORY_REGION_INIT_ARGUMENTS_MAX]) { |
| 365 | memset(out, 0, LIBOS_MEMORY_REGION_INIT_ARGUMENTS_MAX); |
| 366 | for (unsigned i = 0; i < GB10B_LOG_COUNT; i++) |
| 367 | if (in->log_pa[i] == 0 || (in->log_pa[i] & (PAGE_SZ - 1))) return "log buffer address zero or not 4 KiB aligned"; |
| 368 | if (in->rmargs_pa == 0 || (in->rmargs_pa & (PAGE_SZ - 1))) return "RMARGS address zero or not 4 KiB aligned"; |
| 369 | LibosMemoryRegionInitArgument e; |
| 370 | for (unsigned i = 0; i < GB10B_LIBOS_ENTRIES; i++) { |
| 371 | memset(&e, 0, sizeof e); |
| 372 | e.kind = LIBOS_MEMORY_REGION_CONTIGUOUS; |
| 373 | e.loc = LIBOS_MEMORY_REGION_LOC_SYSMEM; |
| 374 | if (i < GB10B_LOG_COUNT) { e.id8 = init_arg_id(gb10b_logs[i].id); e.pa = in->log_pa[i]; e.size = gb10b_logs[i].size; } |
| 375 | else { e.id8 = init_arg_id("RMARGS"); e.pa = in->rmargs_pa; e.size = PAGE_SZ; } |
| 376 | memcpy(out + i * sizeof e, &e, sizeof e); |
| 377 | } |
| 378 | return NULL; |
| 379 | } |
| 380 | |
| 381 | static const char *libos_check(const uint8_t *b, size_t n, libos_in *out) { |
| 382 | if (n != LIBOS_MEMORY_REGION_INIT_ARGUMENTS_MAX) return "libos init arguments size not 4096"; |
| 383 | const size_t es = sizeof(LibosMemoryRegionInitArgument); |
| 384 | for (unsigned i = 0; i < GB10B_LIBOS_ENTRIES; i++) { |
| 385 | const uint8_t *p = b + i * es; |
| 386 | uint64_t id = rd64(p), pa = rd64(p + 8), sz = rd64(p + 16); |
| 387 | uint8_t kind = p[24], loc = p[25]; |
| 388 | for (int k = 26; k < 32; k++) if (p[k] != 0) return "entry padding must be 0"; |
| 389 | if (kind != LIBOS_MEMORY_REGION_CONTIGUOUS) return "entry kind not CONTIGUOUS"; |
| 390 | if (loc != LIBOS_MEMORY_REGION_LOC_SYSMEM) return "entry loc not SYSMEM"; |
| 391 | if (pa == 0 || (pa & (PAGE_SZ - 1))) return "entry address zero or not 4 KiB aligned"; |
| 392 | if (i < GB10B_LOG_COUNT) { |
| 393 | if (id != init_arg_id(gb10b_logs[i].id)) return "log entry id or order wrong for GB10B"; |
| 394 | if (sz != gb10b_logs[i].size) return "log entry size wrong for GB10B"; |
| 395 | } else { |
| 396 | if (id != init_arg_id("RMARGS")) return "entry after the logs is not RMARGS"; |
| 397 | if (sz != PAGE_SZ) return "RMARGS size not 4096"; |
| 398 | } |
| 399 | } |
| 400 | for (size_t k = GB10B_LIBOS_ENTRIES * es; k < n; k++) if (b[k] != 0) return "bytes after the last entry must be 0"; |
| 401 | if (out) { for (unsigned i = 0; i < GB10B_LOG_COUNT; i++) out->log_pa[i] = rd64(b + i * es + 8); out->rmargs_pa = rd64(b + GB10B_LOG_COUNT * es + 8); } |
| 402 | return NULL; |
| 403 | } |
| 404 | |
| 405 | /* ---------- 3. log buffer prefix: put pointer + own PTE table ---------- */ |
| 406 | |
| 407 | static const char *logbuf_prefix(uint64_t base_pa, uint64_t size, uint8_t *out, size_t cap, size_t *len) { |
| 408 | if (base_pa == 0 || (base_pa & (PAGE_SZ - 1))) return "log buffer address zero or not 4 KiB aligned"; |
| 409 | if (size == 0 || (size & (PAGE_SZ - 1))) return "log buffer size zero or not a page multiple"; |
| 410 | uint64_t pages = size / PAGE_SZ; |
| 411 | size_t need = 8 + (size_t)pages * 8; |
| 412 | if (need > size) return "PTE table does not fit in the buffer"; |
| 413 | if (need > cap) return "output buffer too small"; |
| 414 | wr64(out, 0); |
| 415 | for (uint64_t i = 0; i < pages; i++) wr64(out + 8 + i * 8, base_pa + i * PAGE_SZ); |
| 416 | *len = need; |
| 417 | return NULL; |
| 418 | } |
| 419 | |
| 420 | /* ---------- 4. GSP_ARGUMENTS_CACHED + RPC shared-buffer page table ---------- */ |
| 421 | |
| 422 | typedef struct { uint64_t mq_base_pa; uint32_t gpu_instance; uint64_t sysmem_heap_pa, sysmem_heap_size; } rmargs_in; |
| 423 | |
| 424 | static const char *rmargs_encode(const rmargs_in *in, uint8_t out[PAGE_SZ]) { |
| 425 | GSP_ARGUMENTS_CACHED a; |
| 426 | memset(&a, 0, sizeof a); |
| 427 | memset(out, 0, PAGE_SZ); |
| 428 | if (in->mq_base_pa == 0 || (in->mq_base_pa & (PAGE_SZ - 1))) return "message queue address zero or not 4 KiB aligned"; |
| 429 | if (in->sysmem_heap_pa == 0 || (in->sysmem_heap_pa & (PAGE_SZ - 1))) return "sysmem heap address zero or not 4 KiB aligned"; |
| 430 | if (in->sysmem_heap_size == 0 || (in->sysmem_heap_size & ((1ULL << 20) - 1))) return "sysmem heap size zero or not a MiB multiple"; |
| 431 | a.messageQueueInitArguments.sharedMemPhysAddr = in->mq_base_pa; |
| 432 | a.messageQueueInitArguments.pageTableEntryCount = MQ_NUM_PTES; |
| 433 | a.messageQueueInitArguments.cmdQueueOffset = MQ_PAGE_TABLE_SIZE; |
| 434 | a.messageQueueInitArguments.statQueueOffset = MQ_PAGE_TABLE_SIZE + MQ_CMD_QUEUE_SIZE; |
| 435 | a.gpuInstance = in->gpu_instance; |
| 436 | a.bDmemStack = 1; |
| 437 | a.sysmemHeapArgs.pa = in->sysmem_heap_pa; |
| 438 | a.sysmemHeapArgs.size = in->sysmem_heap_size; |
| 439 | memcpy(out, &a, sizeof a); |
| 440 | return NULL; |
| 441 | } |
| 442 | |
| 443 | static const char *rmargs_check(const uint8_t *b, size_t n, rmargs_in *out) { |
| 444 | if (n != PAGE_SZ) return "RMARGS page size not 4096"; |
| 445 | uint64_t mq = rd64(b + 0); uint32_t ptes = rd32(b + 8); |
| 446 | if (rd32(b + 12) != 0) return "padding after pageTableEntryCount must be 0"; |
| 447 | uint64_t cmd = rd64(b + 16), stat = rd64(b + 24); |
| 448 | if (mq == 0 || (mq & (PAGE_SZ - 1))) return "message queue address zero or not 4 KiB aligned"; |
| 449 | if (ptes != MQ_NUM_PTES) return "pageTableEntryCount not 129"; |
| 450 | if (cmd != MQ_PAGE_TABLE_SIZE) return "cmdQueueOffset not 4096"; |
| 451 | if (stat != MQ_PAGE_TABLE_SIZE + MQ_CMD_QUEUE_SIZE) return "statQueueOffset not 266240"; |
| 452 | if (rd32(b + 32) != 0 || rd32(b + 36) != 0 || b[40] != 0) return "suspend/resume arguments must be 0 on a normal boot"; |
| 453 | for (int i = 41; i < 44; i++) if (b[i] != 0) return "padding after srInitArguments must be 0"; |
| 454 | uint32_t gi = rd32(b + 44); |
| 455 | if (b[48] != 1) return "bDmemStack not 1"; |
| 456 | for (int i = 49; i < 56; i++) if (b[i] != 0) return "padding after bDmemStack must be 0"; |
| 457 | if (rd64(b + 56) != 0 || rd64(b + 64) != 0) return "profilerArgs must be 0"; |
| 458 | uint64_t hp = rd64(b + 72), hs = rd64(b + 80); |
| 459 | if (hp == 0 || (hp & (PAGE_SZ - 1))) return "sysmem heap address zero or not 4 KiB aligned"; |
| 460 | if (hs == 0 || (hs & ((1ULL << 20) - 1))) return "sysmem heap size zero or not a MiB multiple"; |
| 461 | for (size_t k = 88; k < n; k++) if (b[k] != 0) return "bytes after GSP_ARGUMENTS_CACHED must be 0"; |
| 462 | if (out) { out->mq_base_pa = mq; out->gpu_instance = gi; out->sysmem_heap_pa = hp; out->sysmem_heap_size = hs; } |
| 463 | return NULL; |
| 464 | } |
| 465 | |
| 466 | /* The RPC shared buffer's page-table page: PAs of all 129 pages, itself first (contiguous case). */ |
| 467 | static const char *mqtable_encode(uint64_t base_pa, uint8_t out[PAGE_SZ]) { |
| 468 | memset(out, 0, PAGE_SZ); |
| 469 | if (base_pa == 0 || (base_pa & (PAGE_SZ - 1))) return "message queue address zero or not 4 KiB aligned"; |
| 470 | for (unsigned i = 0; i < MQ_NUM_PTES; i++) wr64(out + i * 8, base_pa + (uint64_t)i * PAGE_SZ); |
| 471 | return NULL; |
| 472 | } |
| 473 | |
| 474 | /* ---------- 5. radix-3 layout ---------- */ |
| 475 | |
| 476 | typedef struct { uint64_t npages[4]; uint64_t offset[4]; uint64_t pt_size, alloc_size; } radix3_layout; |
| 477 | |
| 478 | static const char *radix3_plan(uint64_t size, radix3_layout *l) { |
| 479 | if (size == 0) return "ELF size zero"; |
| 480 | memset(l, 0, sizeof *l); |
| 481 | l->npages[3] = (size + PAGE_SZ - 1) / PAGE_SZ; |
| 482 | for (int i = 3; i > 0; i--) l->npages[i - 1] = ((l->npages[i] - 1) >> 9) + 1; |
| 483 | uint64_t n = 0; |
| 484 | for (int i = 1; i < 4; i++) { n += l->npages[i - 1]; l->offset[i] = n * PAGE_SZ; } |
| 485 | if (l->npages[0] != 1) return "ELF too large for a 3-level radix table"; |
| 486 | l->pt_size = n * PAGE_SZ; |
| 487 | l->alloc_size = l->pt_size + l->npages[3] * PAGE_SZ; |
| 488 | return NULL; |
| 489 | } |
| 490 | |
| 491 | /* Fill the three table levels for a contiguous allocation at base_pa (data follows the tables). */ |
| 492 | static const char *radix3_tables(uint64_t size, uint64_t base_pa, uint8_t *out, size_t cap, size_t *len) { |
| 493 | radix3_layout l; const char *e = radix3_plan(size, &l); |
| 494 | if (e) return e; |
| 495 | if (base_pa == 0 || (base_pa & (PAGE_SZ - 1))) return "radix3 base address zero or not 4 KiB aligned"; |
| 496 | if (l.pt_size > cap) return "output buffer too small"; |
| 497 | memset(out, 0, l.pt_size); |
| 498 | for (int i = 0; i < 3; i++) |
| 499 | for (uint64_t k = 0; k < l.npages[i + 1]; k++) |
| 500 | wr64(out + l.offset[i] + k * 8, base_pa + l.offset[i + 1] + k * PAGE_SZ); |
| 501 | *len = l.pt_size; |
| 502 | return NULL; |
| 503 | } |
| 504 | |
| 505 | /* ---------- self-test ---------- */ |
| 506 | |
| 507 | static int checks, failures; |
| 508 | static void check(int ok, const char *what) { checks++; if (!ok) { failures++; printf("FAIL: %s\n", what); } } |
| 509 | static void expect_refusal(const char *got, const char *want, const char *what) { |
| 510 | char buf[256]; snprintf(buf, sizeof buf, "%s: expected \"%s\", got \"%s\"", what, want, got ? got : "(accepted)"); |
| 511 | check(got != NULL && strcmp(got, want) == 0, buf); |
| 512 | } |
| 513 | |
| 514 | static void sample_wpr(wpr_in *w) { |
| 515 | memset(w, 0, sizeof *w); |
| 516 | w->bootloader_pa = 0x100000000ULL; w->radix3_root_pa = 0x100100000ULL; w->elf_size = 0x4000000ULL; |
| 517 | w->signature_pa = 0x100200100ULL; w->signature_size = 0x400; |
| 518 | } |
| 519 | |
| 520 | static int selftest(void) { |
| 521 | /* layouts against the compiler */ |
| 522 | check(sizeof(GspFwWprMeta) == 256, "sizeof GspFwWprMeta == 256"); |
| 523 | check(offsetof(GspFwWprMeta, sysmemAddrOfSignature) == 72, "sysmemAddrOfSignature at 72"); |
| 524 | check(offsetof(GspFwWprMeta, gspFwHeapSize) == 128, "gspFwHeapSize at 128"); |
| 525 | check(offsetof(GspFwWprMeta, bootCount) == 200, "bootCount at 200"); |
| 526 | check(offsetof(GspFwWprMeta, sysmemAddrOfCrashReportQueue) == 224, "sysmemAddrOfCrashReportQueue at 224"); |
| 527 | check(offsetof(GspFwWprMeta, gspFwHeapVfPartitionCount) == 240, "gspFwHeapVfPartitionCount at 240"); |
| 528 | check(offsetof(GspFwWprMeta, flags) == 241, "flags at 241"); |
| 529 | check(offsetof(GspFwWprMeta, pmuReservedSize) == 244, "pmuReservedSize at 244"); |
| 530 | check(offsetof(GspFwWprMeta, verified) == 248, "verified at 248"); |
| 531 | check(sizeof(LibosMemoryRegionInitArgument) == 32, "sizeof LibosMemoryRegionInitArgument == 32"); |
| 532 | check(offsetof(LibosMemoryRegionInitArgument, kind) == 24 && offsetof(LibosMemoryRegionInitArgument, loc) == 25, "kind/loc at 24/25"); |
| 533 | check(sizeof(GSP_ARGUMENTS_CACHED) == 88, "sizeof GSP_ARGUMENTS_CACHED == 88"); |
| 534 | check(offsetof(GSP_ARGUMENTS_CACHED, srInitArguments) == 32, "srInitArguments at 32"); |
| 535 | check(offsetof(GSP_ARGUMENTS_CACHED, gpuInstance) == 44, "gpuInstance at 44"); |
| 536 | check(offsetof(GSP_ARGUMENTS_CACHED, bDmemStack) == 48, "bDmemStack at 48"); |
| 537 | check(offsetof(GSP_ARGUMENTS_CACHED, profilerArgs) == 56, "profilerArgs at 56"); |
| 538 | check(offsetof(GSP_ARGUMENTS_CACHED, sysmemHeapArgs) == 72, "sysmemHeapArgs at 72"); |
| 539 | check(sizeof(MESSAGE_QUEUE_INIT_ARGUMENTS) == 32, "sizeof MESSAGE_QUEUE_INIT_ARGUMENTS == 32"); |
| 540 | check(sizeof(GSP_SR_INIT_ARGUMENTS) == 12, "sizeof GSP_SR_INIT_ARGUMENTS == 12"); |
| 541 | |
| 542 | /* golden constants */ |
| 543 | check(init_arg_id("RMARGS") == 0x524D41524753ULL, "RMARGS id8"); |
| 544 | check(init_arg_id("LOGINIT") == 0x4C4F47494E4954ULL, "LOGINIT id8"); |
| 545 | check(init_arg_id("LOGKRNL") == 0x4C4F474B524E4CULL, "LOGKRNL id8"); |
| 546 | check(init_arg_id("ABCDEFGHIJ") == 0x4142434445464748ULL, "id8 truncates to 8 bytes"); |
| 547 | check(GB10B_FW_HEAP_SIZE == 0x8500000ULL, "GB10B heap 133 MiB"); |
| 548 | { /* recompute the heap exactly as _kgspCalculateFwHeapSize does with ZERO_FB */ |
| 549 | uint64_t memSizeGB = 1, a1m = (1ULL << 20) - 1; |
| 550 | uint64_t heap = (22ULL << 20) + (14ULL << 20) + (((96ULL << 10) * memSizeGB + a1m) & ~a1m) + (((48ULL << 10) * 2048 + a1m) & ~a1m); |
| 551 | if (heap < (88ULL << 20)) heap = 88ULL << 20; |
| 552 | check(heap == GB10B_FW_HEAP_SIZE, "heap formula reproduces 133 MiB"); |
| 553 | } |
| 554 | check(MQ_NUM_PTES == (MQ_CMD_QUEUE_SIZE + MQ_STAT_QUEUE_SIZE) / PAGE_SZ + ((MQ_CMD_QUEUE_SIZE + MQ_STAT_QUEUE_SIZE) / PAGE_SZ * 8 + PAGE_SZ - 1) / PAGE_SZ, "numPtes formula gives 129"); |
| 555 | check(((MQ_NUM_PTES * 8 + PAGE_SZ - 1) & ~(PAGE_SZ - 1)) == MQ_PAGE_TABLE_SIZE, "pageTableSize 4096"); |
| 556 | |
| 557 | /* 1. WprMeta round trip + golden bytes */ |
| 558 | wpr_in w, w2; uint8_t m[256]; |
| 559 | sample_wpr(&w); |
| 560 | check(wpr_encode(&w, m) == NULL, "WprMeta encode accepted"); |
| 561 | check(rd64(m) == GSP_FW_WPR_META_MAGIC && rd64(m + 8) == 1, "magic + revision bytes"); |
| 562 | check(rd64(m + 40) == GB10B_BOOTLOADER_SIZE && rd64(m + 48) == 0xb000 && rd64(m + 56) == 0xa00 && rd64(m + 64) == 0, "bootloader size/offsets bytes"); |
| 563 | check(rd64(m + 128) == GB10B_FW_HEAP_SIZE && rd64(m + 104) == 0 && rd64(m + 160) == 0 && rd64(m + 192) == 0 && rd32(m + 244) == 0, "GB10B zeroed fields + heap"); |
| 564 | check(wpr_check(m, 256, &w2) == NULL, "WprMeta decode accepted"); |
| 565 | check(memcmp(&w, &w2, sizeof w) == 0 || (w2.heap_size == GB10B_FW_HEAP_SIZE && w2.bootloader_pa == w.bootloader_pa && w2.radix3_root_pa == w.radix3_root_pa && w2.elf_size == w.elf_size && w2.signature_pa == w.signature_pa && w2.signature_size == w.signature_size), "WprMeta round trip"); |
| 566 | w.heap_size = 200ULL << 20; w.scan_recovery_margin = 1; w.crash_queue_pa = 0x100300000ULL; w.crash_queue_size = 0x2000; |
| 567 | check(wpr_encode(&w, m) == NULL && wpr_check(m, 256, &w2) == NULL && w2.heap_size == (200ULL << 20) && w2.scan_recovery_margin == 1 && w2.crash_queue_pa == w.crash_queue_pa && w2.crash_queue_size == 0x2000, "WprMeta optional fields round trip"); |
| 568 | check(m[241] == GSP_FW_FLAGS_SCAN_RECOVERY_MARGIN, "flags byte"); |
| 569 | /* encode refusals */ |
| 570 | { wpr_in b = w; b.bootloader_pa = 0x100000800ULL; expect_refusal(wpr_encode(&b, m), "bootloader address zero or not 4 KiB aligned", "unaligned bootloader"); } |
| 571 | { wpr_in b = w; b.bootloader_pa = 0; expect_refusal(wpr_encode(&b, m), "bootloader address zero or not 4 KiB aligned", "zero bootloader"); } |
| 572 | { wpr_in b = w; b.radix3_root_pa = 0x1000ULL + 8; expect_refusal(wpr_encode(&b, m), "radix3 root address zero or not 4 KiB aligned", "unaligned radix3"); } |
| 573 | { wpr_in b = w; b.elf_size = 0; expect_refusal(wpr_encode(&b, m), "ELF size zero", "zero elf"); } |
| 574 | { wpr_in b = w; b.signature_pa = 0x100200080ULL; expect_refusal(wpr_encode(&b, m), "signature address zero or not 256 aligned", "unaligned signature"); } |
| 575 | { wpr_in b = w; b.signature_size = 0x3f0; expect_refusal(wpr_encode(&b, m), "signature size zero or not a 256 multiple", "odd signature size"); } |
| 576 | { wpr_in b = w; b.heap_size = 87ULL << 20; expect_refusal(wpr_encode(&b, m), "heap size outside [88 MiB, 280 MiB] or not a MiB multiple", "heap too small"); } |
| 577 | { wpr_in b = w; b.heap_size = 281ULL << 20; expect_refusal(wpr_encode(&b, m), "heap size outside [88 MiB, 280 MiB] or not a MiB multiple", "heap too large"); } |
| 578 | { wpr_in b = w; b.heap_size = (133ULL << 20) + 1; expect_refusal(wpr_encode(&b, m), "heap size outside [88 MiB, 280 MiB] or not a MiB multiple", "heap not MiB"); } |
| 579 | { wpr_in b = w; b.crash_queue_size = 0; expect_refusal(wpr_encode(&b, m), "crash report queue address and size must both be zero or both set", "crash queue half set"); } |
| 580 | { wpr_in b = w; b.crash_queue_pa = 0x100300010ULL; expect_refusal(wpr_encode(&b, m), "crash report queue address not 4 KiB aligned", "crash queue unaligned"); } |
| 581 | /* decode refusals, one field at a time from a good block */ |
| 582 | sample_wpr(&w); check(wpr_encode(&w, m) == NULL, "good block"); |
| 583 | expect_refusal(wpr_check(m, 255, NULL), "WprMeta size not 256", "short block"); |
| 584 | { |
| 585 | struct { int off; int width; uint64_t val; const char *want; } muts[] = { |
| 586 | {0, 8, 0xdc3aae21371a60b2ULL, "magic wrong"}, |
| 587 | {8, 8, 2, "revision not 1"}, |
| 588 | {16, 8, 0x100100800ULL, "radix3 root address zero or not 4 KiB aligned"}, |
| 589 | {24, 8, 0, "ELF size zero"}, |
| 590 | {32, 8, 0, "bootloader address zero or not 4 KiB aligned"}, |
| 591 | {40, 8, 196607, "bootloader size not the GB10B GspRmBoot image size"}, |
| 592 | {48, 8, 0xb100, "bootloader offsets not the GB10B descriptor values"}, |
| 593 | {56, 8, 0xa00 + 0x100, "bootloader offsets not the GB10B descriptor values"}, |
| 594 | {64, 8, 0x100, "bootloader offsets not the GB10B descriptor values"}, |
| 595 | {72, 8, 0x100200180ULL, "signature address zero or not 256 aligned"}, |
| 596 | {80, 8, 0x3ff, "signature size zero or not a 256 multiple"}, |
| 597 | {88, 8, 1, "FMC-owned FB layout field non-zero on the host side"}, |
| 598 | {96, 8, 1, "FMC-owned FB layout field non-zero on the host side"}, |
| 599 | {104, 8, 1, "nonWprHeapSize must be 0 on GB10B"}, |
| 600 | {112, 8, 1, "FMC-owned FB layout field non-zero on the host side"}, |
| 601 | {120, 8, 1, "FMC-owned FB layout field non-zero on the host side"}, |
| 602 | {128, 8, 1ULL << 20, "heap size outside [88 MiB, 280 MiB] or not a MiB multiple"}, |
| 603 | {136, 8, 1, "FMC-owned FB layout field non-zero on the host side"}, |
| 604 | {144, 8, 1, "FMC-owned FB layout field non-zero on the host side"}, |
| 605 | {152, 8, 1, "FMC-owned FB layout field non-zero on the host side"}, |
| 606 | {160, 8, 1, "frtsSize must be 0 on GB10B"}, |
| 607 | {168, 8, 1, "FMC-owned FB layout field non-zero on the host side"}, |
| 608 | {176, 8, 1, "FMC-owned FB layout field non-zero on the host side"}, |
| 609 | {184, 8, 1, "FMC-owned FB layout field non-zero on the host side"}, |
| 610 | {192, 8, 1, "vgaWorkspaceSize must be 0 on GB10B"}, |
| 611 | {200, 8, 1, "bootCount must be 0 on the host side"}, |
| 612 | {208, 8, 1, "partition RPC fields must be 0"}, |
| 613 | {220, 4, 1, "partition RPC fields must be 0"}, |
| 614 | {224, 8, 0x100300000ULL, "crash report queue address and size must both be zero or both set"}, |
| 615 | {232, 4, 0x2000, "crash report queue address and size must both be zero or both set"}, |
| 616 | {236, 4, 1, "lsUcodeVersion padding must be 0"}, |
| 617 | {240, 1, 1, "gspFwHeapVfPartitionCount must be 0"}, |
| 618 | {241, 1, 0x20, "unknown flag bits"}, |
| 619 | {241, 1, GSP_FW_FLAGS_PPCIE_ENABLED, "flag bit the host never sets on GB10B"}, |
| 620 | {241, 1, GSP_FW_FLAGS_CLOCK_BOOST, "flag bit the host never sets on GB10B"}, |
| 621 | {242, 1, 1, "padding must be 0"}, |
| 622 | {244, 4, 1, "pmuReservedSize must be 0 on GB10B"}, |
| 623 | {248, 8, GSP_FW_WPR_META_VERIFIED, "verified must be 0 on the host side"}, |
| 624 | }; |
| 625 | for (size_t i = 0; i < sizeof muts / sizeof muts[0]; i++) { |
| 626 | uint8_t t[256]; memcpy(t, m, 256); |
| 627 | if (muts[i].width == 8) wr64(t + muts[i].off, muts[i].val); |
| 628 | else if (muts[i].width == 4) wr32(t + muts[i].off, (uint32_t)muts[i].val); |
| 629 | else t[muts[i].off] = (uint8_t)muts[i].val; |
| 630 | char what[64]; snprintf(what, sizeof what, "WprMeta mutation at %d", muts[i].off); |
| 631 | expect_refusal(wpr_check(t, 256, NULL), muts[i].want, what); |
| 632 | } |
| 633 | { uint8_t t[256]; memcpy(t, m, 256); wr64(t + 224, 0x100300010ULL); wr32(t + 232, 0x1000); |
| 634 | expect_refusal(wpr_check(t, 256, NULL), "crash report queue address not 4 KiB aligned", "crash queue unaligned (decode)"); } |
| 635 | } |
| 636 | |
| 637 | /* 2. libos init arguments */ |
| 638 | libos_in li, li2; uint8_t la[LIBOS_MEMORY_REGION_INIT_ARGUMENTS_MAX]; |
| 639 | for (unsigned i = 0; i < GB10B_LOG_COUNT; i++) li.log_pa[i] = 0x200000000ULL + (uint64_t)i * 0x10000; |
| 640 | li.rmargs_pa = 0x200100000ULL; |
| 641 | check(libos_encode(&li, la) == NULL, "libos encode accepted"); |
| 642 | check(rd64(la) == init_arg_id("LOGINIT") && rd64(la + 16) == 0x10000 && la[24] == 1 && la[25] == 1, "entry 0 bytes"); |
| 643 | check(rd64(la + 5 * 32) == init_arg_id("LOGROOT") && rd64(la + 5 * 32 + 16) == 0x1000, "LOGROOT entry 4 KiB"); |
| 644 | check(rd64(la + 6 * 32) == init_arg_id("LOGKRNL"), "LOGKRNL entry seventh"); |
| 645 | check(rd64(la + 7 * 32) == init_arg_id("RMARGS") && rd64(la + 7 * 32 + 16) == 4096 && rd64(la + 7 * 32 + 8) == li.rmargs_pa, "RMARGS entry eighth"); |
| 646 | check(la[8 * 32] == 0 && la[8 * 32 + 24] == LIBOS_MEMORY_REGION_NONE, "ninth entry is NONE"); |
| 647 | check(libos_check(la, sizeof la, &li2) == NULL && memcmp(&li, &li2, sizeof li) == 0, "libos round trip"); |
| 648 | { libos_in b = li; b.log_pa[2] = 0x200020800ULL; expect_refusal(libos_encode(&b, la), "log buffer address zero or not 4 KiB aligned", "libos unaligned log"); } |
| 649 | { libos_in b = li; b.rmargs_pa = 0; expect_refusal(libos_encode(&b, la), "RMARGS address zero or not 4 KiB aligned", "libos zero rmargs"); } |
| 650 | check(libos_encode(&li, la) == NULL, "libos good block"); |
| 651 | expect_refusal(libos_check(la, 4095, NULL), "libos init arguments size not 4096", "libos short"); |
| 652 | { uint8_t t[4096]; memcpy(t, la, 4096); t[24] = LIBOS_MEMORY_REGION_RADIX3; expect_refusal(libos_check(t, 4096, NULL), "entry kind not CONTIGUOUS", "libos kind"); } |
| 653 | { uint8_t t[4096]; memcpy(t, la, 4096); t[25] = LIBOS_MEMORY_REGION_LOC_FB; expect_refusal(libos_check(t, 4096, NULL), "entry loc not SYSMEM", "libos loc"); } |
| 654 | { uint8_t t[4096]; memcpy(t, la, 4096); t[30] = 1; expect_refusal(libos_check(t, 4096, NULL), "entry padding must be 0", "libos pad"); } |
| 655 | { uint8_t t[4096]; memcpy(t, la, 4096); wr64(t + 32, init_arg_id("LOGRM")); expect_refusal(libos_check(t, 4096, NULL), "log entry id or order wrong for GB10B", "libos order"); } |
| 656 | { uint8_t t[4096]; memcpy(t, la, 4096); wr64(t + 5 * 32 + 16, 0x10000); expect_refusal(libos_check(t, 4096, NULL), "log entry size wrong for GB10B", "libos size"); } |
| 657 | { uint8_t t[4096]; memcpy(t, la, 4096); wr64(t + 7 * 32, init_arg_id("RMCOV")); expect_refusal(libos_check(t, 4096, NULL), "entry after the logs is not RMARGS", "libos rmargs id"); } |
| 658 | { uint8_t t[4096]; memcpy(t, la, 4096); wr64(t + 7 * 32 + 16, 88); expect_refusal(libos_check(t, 4096, NULL), "RMARGS size not 4096", "libos rmargs size"); } |
| 659 | { uint8_t t[4096]; memcpy(t, la, 4096); wr64(t + 3 * 32 + 8, 0); expect_refusal(libos_check(t, 4096, NULL), "entry address zero or not 4 KiB aligned", "libos zero pa"); } |
| 660 | { uint8_t t[4096]; memcpy(t, la, 4096); t[9 * 32 + 24] = 1; expect_refusal(libos_check(t, 4096, NULL), "bytes after the last entry must be 0", "libos trailing"); } |
| 661 | |
| 662 | /* 3. log buffer prefix */ |
| 663 | uint8_t lb[8 + 16 * 8]; size_t ln = 0; |
| 664 | check(logbuf_prefix(0x200000000ULL, 0x10000, lb, sizeof lb, &ln) == NULL && ln == 8 + 16 * 8, "logbuf 64 KiB prefix length"); |
| 665 | check(rd64(lb) == 0 && rd64(lb + 8) == 0x200000000ULL && rd64(lb + 8 + 15 * 8) == 0x20000F000ULL, "logbuf put + PTEs"); |
| 666 | check(logbuf_prefix(0x200000000ULL, 0x1000, lb, sizeof lb, &ln) == NULL && ln == 16, "logbuf 4 KiB prefix"); |
| 667 | expect_refusal(logbuf_prefix(0x200000000ULL, 0x1800, lb, sizeof lb, &ln), "log buffer size zero or not a page multiple", "logbuf odd size"); |
| 668 | expect_refusal(logbuf_prefix(0x200000100ULL, 0x1000, lb, sizeof lb, &ln), "log buffer address zero or not 4 KiB aligned", "logbuf unaligned"); |
| 669 | expect_refusal(logbuf_prefix(0x200000000ULL, 0x10000, lb, 16, &ln), "output buffer too small", "logbuf small out"); |
| 670 | |
| 671 | /* 4. RMARGS + message queue table */ |
| 672 | rmargs_in ri = { 0x300000000ULL, 0, 0x400000000ULL, GB10B_SYSMEM_HEAP }, ri2; uint8_t ra[PAGE_SZ]; |
| 673 | check(rmargs_encode(&ri, ra) == NULL, "rmargs encode accepted"); |
| 674 | check(rd64(ra) == 0x300000000ULL && rd32(ra + 8) == 129 && rd64(ra + 16) == 4096 && rd64(ra + 24) == 266240 && ra[48] == 1 && rd64(ra + 72) == 0x400000000ULL && rd64(ra + 80) == (512ULL << 20), "rmargs bytes"); |
| 675 | check(rmargs_check(ra, PAGE_SZ, &ri2) == NULL && memcmp(&ri, &ri2, sizeof ri) == 0, "rmargs round trip"); |
| 676 | { rmargs_in b = ri; b.mq_base_pa = 0x300000010ULL; expect_refusal(rmargs_encode(&b, ra), "message queue address zero or not 4 KiB aligned", "rmargs mq unaligned"); } |
| 677 | { rmargs_in b = ri; b.sysmem_heap_size = 0x80000; expect_refusal(rmargs_encode(&b, ra), "sysmem heap size zero or not a MiB multiple", "rmargs heap size"); } |
| 678 | { rmargs_in b = ri; b.sysmem_heap_pa = 0; expect_refusal(rmargs_encode(&b, ra), "sysmem heap address zero or not 4 KiB aligned", "rmargs heap pa"); } |
| 679 | check(rmargs_encode(&ri, ra) == NULL, "rmargs good block"); |
| 680 | expect_refusal(rmargs_check(ra, 88, NULL), "RMARGS page size not 4096", "rmargs short"); |
| 681 | { uint8_t t[PAGE_SZ]; memcpy(t, ra, PAGE_SZ); wr32(t + 8, 128); expect_refusal(rmargs_check(t, PAGE_SZ, NULL), "pageTableEntryCount not 129", "rmargs ptes"); } |
| 682 | { uint8_t t[PAGE_SZ]; memcpy(t, ra, PAGE_SZ); wr32(t + 12, 1); expect_refusal(rmargs_check(t, PAGE_SZ, NULL), "padding after pageTableEntryCount must be 0", "rmargs pad"); } |
| 683 | { uint8_t t[PAGE_SZ]; memcpy(t, ra, PAGE_SZ); wr64(t + 16, 8192); expect_refusal(rmargs_check(t, PAGE_SZ, NULL), "cmdQueueOffset not 4096", "rmargs cmd"); } |
| 684 | { uint8_t t[PAGE_SZ]; memcpy(t, ra, PAGE_SZ); wr64(t + 24, 4096); expect_refusal(rmargs_check(t, PAGE_SZ, NULL), "statQueueOffset not 266240", "rmargs stat"); } |
| 685 | { uint8_t t[PAGE_SZ]; memcpy(t, ra, PAGE_SZ); t[40] = 1; expect_refusal(rmargs_check(t, PAGE_SZ, NULL), "suspend/resume arguments must be 0 on a normal boot", "rmargs sr"); } |
| 686 | { uint8_t t[PAGE_SZ]; memcpy(t, ra, PAGE_SZ); t[48] = 0; expect_refusal(rmargs_check(t, PAGE_SZ, NULL), "bDmemStack not 1", "rmargs dmem"); } |
| 687 | { uint8_t t[PAGE_SZ]; memcpy(t, ra, PAGE_SZ); wr64(t + 56, 1); expect_refusal(rmargs_check(t, PAGE_SZ, NULL), "profilerArgs must be 0", "rmargs profiler"); } |
| 688 | { uint8_t t[PAGE_SZ]; memcpy(t, ra, PAGE_SZ); t[100] = 1; expect_refusal(rmargs_check(t, PAGE_SZ, NULL), "bytes after GSP_ARGUMENTS_CACHED must be 0", "rmargs trailing"); } |
| 689 | uint8_t mt[PAGE_SZ]; |
| 690 | check(mqtable_encode(0x300000000ULL, mt) == NULL && rd64(mt) == 0x300000000ULL && rd64(mt + 128 * 8) == 0x300080000ULL && rd64(mt + 129 * 8) == 0, "mq table 129 PTEs, itself first"); |
| 691 | expect_refusal(mqtable_encode(0x300000008ULL, mt), "message queue address zero or not 4 KiB aligned", "mq table unaligned"); |
| 692 | |
| 693 | /* 5. radix3 */ |
| 694 | radix3_layout rl; |
| 695 | check(radix3_plan(1, &rl) == NULL && rl.npages[3] == 1 && rl.npages[2] == 1 && rl.npages[1] == 1 && rl.npages[0] == 1 && rl.pt_size == 3 * PAGE_SZ && rl.alloc_size == 4 * PAGE_SZ, "radix3 one byte"); |
| 696 | check(radix3_plan(512 * PAGE_SZ, &rl) == NULL && rl.npages[3] == 512 && rl.npages[2] == 1 && rl.pt_size == 3 * PAGE_SZ, "radix3 exactly 512 pages"); |
| 697 | check(radix3_plan(512 * PAGE_SZ + 1, &rl) == NULL && rl.npages[3] == 513 && rl.npages[2] == 2 && rl.npages[1] == 1 && rl.pt_size == 4 * PAGE_SZ && rl.offset[1] == PAGE_SZ && rl.offset[2] == 2 * PAGE_SZ && rl.offset[3] == 4 * PAGE_SZ, "radix3 513 pages"); |
| 698 | check(radix3_plan(75012080ULL, &rl) == NULL && rl.npages[3] == 18314 && rl.npages[2] == 36 && rl.npages[1] == 1 && rl.pt_size == 38 * PAGE_SZ, "radix3 for the pinned gsp_ga10x.bin size"); |
| 699 | expect_refusal(radix3_plan(0, &rl), "ELF size zero", "radix3 zero"); |
| 700 | expect_refusal(radix3_plan(((uint64_t)512 * 512 * 512 + 1) * PAGE_SZ, &rl), "ELF too large for a 3-level radix table", "radix3 too large"); |
| 701 | static uint8_t rt[4 * 4096]; size_t rn = 0; |
| 702 | check(radix3_tables(512 * PAGE_SZ + 1, 0x500000000ULL, rt, sizeof rt, &rn) == NULL && rn == 4 * PAGE_SZ, "radix3 tables length"); |
| 703 | check(rd64(rt) == 0x500001000ULL && rd64(rt + 8) == 0, "level 0 -> level 1 page"); |
| 704 | check(rd64(rt + PAGE_SZ) == 0x500002000ULL && rd64(rt + PAGE_SZ + 8) == 0x500003000ULL && rd64(rt + PAGE_SZ + 16) == 0, "level 1 -> two level 2 pages"); |
| 705 | check(rd64(rt + 2 * PAGE_SZ) == 0x500004000ULL && rd64(rt + 2 * PAGE_SZ + 511 * 8) == 0x500004000ULL + 511 * PAGE_SZ && rd64(rt + 3 * PAGE_SZ) == 0x500004000ULL + 512 * PAGE_SZ && rd64(rt + 3 * PAGE_SZ + 8) == 0, "level 2 -> 513 data pages"); |
| 706 | expect_refusal(radix3_tables(PAGE_SZ, 0x500000100ULL, rt, sizeof rt, &rn), "radix3 base address zero or not 4 KiB aligned", "radix3 base unaligned"); |
| 707 | |
| 708 | printf("CK_GSP_BOOT_ARGS_ENCODE_SELFTEST: %s (%d checks, %d failures)\n", failures ? "FAIL" : "PASS", checks, failures); |
| 709 | return failures ? 1 : 0; |
| 710 | } |
| 711 | |
| 712 | /* ---------- CLI ---------- */ |
| 713 | |
| 714 | static uint64_t parse_u64(const char *s, int *ok) { char *e; unsigned long long v = strtoull(s, &e, 0); if (*s == 0 || *e != 0) { *ok = 0; return 0; } return v; } |
| 715 | static int write_file(const char *path, const uint8_t *b, size_t n) { FILE *f = fopen(path, "wb"); if (!f) return -1; size_t w = fwrite(b, 1, n, f); fclose(f); return w == n ? 0 : -1; } |
| 716 | static int read_file(const char *path, uint8_t **b, size_t *n) { |
| 717 | FILE *f = fopen(path, "rb"); if (!f) return -1; |
| 718 | fseek(f, 0, SEEK_END); long len = ftell(f); fseek(f, 0, SEEK_SET); |
| 719 | if (len < 0 || len > (1 << 20)) { fclose(f); return -1; } |
| 720 | *b = malloc((size_t)len + 1); if (!*b) { fclose(f); return -1; } |
| 721 | *n = fread(*b, 1, (size_t)len, f); fclose(f); return *n == (size_t)len ? 0 : -1; |
| 722 | } |
| 723 | static int usage(void) { |
| 724 | fprintf(stderr, "usage: ck_gsp_boot_args_encode selftest | layout\n" |
| 725 | " wprmeta --bootloader HEX --radix3 HEX --elf-size N --sig HEX --sig-size N [--heap N] [--crashq HEX --crashq-size N] [--scan-margin] [--out F]\n" |
| 726 | " check-wprmeta F | check-libos F | check-rmargs F\n" |
| 727 | " libos --log HEX x7 (LOGINIT LOGINTR LOGRM LOGMNOC LOGDBG LOGROOT LOGKRNL) --rmargs HEX [--out F]\n" |
| 728 | " logbuf --pa HEX --size N [--out F]\n" |
| 729 | " rmargs --mq HEX [--gpu N] --heap HEX --heap-size N [--out F]\n" |
| 730 | " mqtable --mq HEX [--out F]\n" |
| 731 | " radix3 --elf-size N [--base HEX --out F]\n"); |
| 732 | return 2; |
| 733 | } |
| 734 | static int refused(const char *why) { printf("REFUSED: %s\n", why); return 2; } |
| 735 | /* Write the block to --out when given; 0 on success, 2 (refused) on a write failure. */ |
| 736 | static int emit(const char *out, const uint8_t *b, size_t n) { |
| 737 | if (!out) return 0; |
| 738 | if (write_file(out, b, n)) return refused("cannot write output"); |
| 739 | printf("wrote %s (%zu bytes)\n", out, n); |
| 740 | return 0; |
| 741 | } |
| 742 | static void dump(const uint8_t *b, size_t n) { for (size_t i = 0; i < n; i++) printf("%02x%s", b[i], (i % 32 == 31 || i + 1 == n) ? "\n" : ""); } |
| 743 | |
| 744 | int main(int argc, char **argv) { |
| 745 | if (argc < 2) return usage(); |
| 746 | const char *mode = argv[1]; |
| 747 | if (!strcmp(mode, "selftest")) return selftest(); |
| 748 | if (!strcmp(mode, "layout")) { |
| 749 | printf("GspFwWprMeta 256 bytes: magic 0 revision 8 sysmemAddrOfRadix3Elf 16 sizeOfRadix3Elf 24 sysmemAddrOfBootloader 32 sizeOfBootloader 40 bootloaderCodeOffset 48 bootloaderDataOffset 56 bootloaderManifestOffset 64 sysmemAddrOfSignature 72 sizeOfSignature 80 gspFwRsvdStart 88 nonWprHeapOffset 96 nonWprHeapSize 104 gspFwWprStart 112 gspFwHeapOffset 120 gspFwHeapSize 128 gspFwOffset 136 bootBinOffset 144 frtsOffset 152 frtsSize 160 gspFwWprEnd 168 fbSize 176 vgaWorkspaceOffset 184 vgaWorkspaceSize 192 bootCount 200 partitionRpc/crashQueue 208..239 gspFwHeapVfPartitionCount 240 flags 241 padding 242 pmuReservedSize 244 verified 248\n"); |
| 750 | printf("LibosMemoryRegionInitArgument 32 bytes: id8 0 pa 8 size 16 kind 24 loc 25; page holds 128 entries, GB10B uses 8\n"); |
| 751 | printf("GSP_ARGUMENTS_CACHED 88 bytes: sharedMemPhysAddr 0 pageTableEntryCount 8 cmdQueueOffset 16 statQueueOffset 24 sr.oldLevel 32 sr.flags 36 sr.bInPMTransition 40 gpuInstance 44 bDmemStack 48 profilerArgs 56 sysmemHeapArgs 72\n"); |
| 752 | printf("GB10B constants: bootloader 196608 code 0xb000 data 0xa00 manifest 0x0; heap 0x%llx (133 MiB) in [88,280] MiB; frts 0; nonWprHeap 0; sysmem heap default 512 MiB; mq cmd 0x40000 stat 0x40000 ptes 129 table 4096; logs LOGINIT LOGINTR LOGRM LOGMNOC LOGDBG(64K each) LOGROOT(4K) LOGKRNL(64K) then RMARGS(4K)\n", (unsigned long long)GB10B_FW_HEAP_SIZE); |
| 753 | return 0; |
| 754 | } |
| 755 | const char *out = NULL; int ok = 1; |
| 756 | uint64_t v[16] = {0}; int nlog = 0; uint64_t logs[GB10B_LOG_COUNT] = {0}; int scan = 0; unsigned gpu = 0; |
| 757 | enum { A_BL, A_R3, A_ELF, A_SIG, A_SIGS, A_HEAP, A_CQ, A_CQS, A_RM, A_PA, A_SZ, A_MQ, A_HEAPPA, A_HEAPSZ, A_BASE }; |
| 758 | static const char *names[] = {"--bootloader", "--radix3", "--elf-size", "--sig", "--sig-size", "--heap", "--crashq", "--crashq-size", "--rmargs", "--pa", "--size", "--mq", "--heap", "--heap-size", "--base"}; |
| 759 | int is_rmargs = !strcmp(mode, "rmargs"); |
| 760 | for (int i = 2; i < argc; i++) { |
| 761 | if (!strcmp(argv[i], "--out") && i + 1 < argc) { out = argv[++i]; continue; } |
| 762 | if (!strcmp(argv[i], "--scan-margin")) { scan = 1; continue; } |
| 763 | if (!strcmp(argv[i], "--gpu") && i + 1 < argc) { gpu = (unsigned)parse_u64(argv[++i], &ok); continue; } |
| 764 | if (!strcmp(argv[i], "--log") && i + 1 < argc) { if (nlog < (int)GB10B_LOG_COUNT) logs[nlog++] = parse_u64(argv[++i], &ok); else return refused("more than 7 --log values"); continue; } |
| 765 | int hit = 0; |
| 766 | for (int k = 0; k < 15 && i + 1 < argc; k++) { |
| 767 | if (!strcmp(argv[i], names[k])) { |
| 768 | if (k == A_HEAP && is_rmargs) k = A_HEAPPA; /* rmargs: --heap is the sysmem heap address */ |
| 769 | v[k] = parse_u64(argv[++i], &ok); hit = 1; break; |
| 770 | } |
| 771 | } |
| 772 | if (!hit) { if (argv[i][0] == '-') return usage(); } |
| 773 | if (!ok) return refused("bad number"); |
| 774 | } |
| 775 | if (!strcmp(mode, "wprmeta")) { |
| 776 | wpr_in w = { v[A_BL], v[A_R3], v[A_ELF], v[A_SIG], v[A_SIGS], v[A_HEAP], v[A_CQ], (uint32_t)v[A_CQS], scan }; |
| 777 | uint8_t m[256]; const char *e = wpr_encode(&w, m); if (e) return refused(e); |
| 778 | printf("wprmeta ok heap=0x%llx\n", (unsigned long long)rd64(m + 128)); dump(m, 256); |
| 779 | return emit(out, m, 256); |
| 780 | } |
| 781 | if (!strcmp(mode, "libos")) { |
| 782 | if (nlog != (int)GB10B_LOG_COUNT) return refused("need exactly 7 --log values"); |
| 783 | libos_in li; memcpy(li.log_pa, logs, sizeof logs); li.rmargs_pa = v[A_RM]; |
| 784 | uint8_t la[4096]; const char *e = libos_encode(&li, la); if (e) return refused(e); |
| 785 | printf("libos ok entries=%u\n", GB10B_LIBOS_ENTRIES); dump(la, GB10B_LIBOS_ENTRIES * 32); |
| 786 | return emit(out, la, 4096); |
| 787 | } |
| 788 | if (!strcmp(mode, "logbuf")) { |
| 789 | static uint8_t lb[8 + 4096]; size_t ln = 0; const char *e = logbuf_prefix(v[A_PA], v[A_SZ], lb, sizeof lb, &ln); if (e) return refused(e); |
| 790 | printf("logbuf ok prefix=%zu bytes pages=%llu\n", ln, (unsigned long long)(v[A_SZ] / PAGE_SZ)); dump(lb, ln > 64 ? 64 : ln); |
| 791 | return emit(out, lb, ln); |
| 792 | } |
| 793 | if (is_rmargs) { |
| 794 | rmargs_in ri = { v[A_MQ], gpu, v[A_HEAPPA], v[A_HEAPSZ] ? v[A_HEAPSZ] : GB10B_SYSMEM_HEAP }; |
| 795 | uint8_t ra[PAGE_SZ]; const char *e = rmargs_encode(&ri, ra); if (e) return refused(e); |
| 796 | printf("rmargs ok\n"); dump(ra, 88); |
| 797 | return emit(out, ra, PAGE_SZ); |
| 798 | } |
| 799 | if (!strcmp(mode, "mqtable")) { |
| 800 | uint8_t mt[PAGE_SZ]; const char *e = mqtable_encode(v[A_MQ], mt); if (e) return refused(e); |
| 801 | printf("mqtable ok ptes=%u cmd=0x%llx stat=0x%llx\n", MQ_NUM_PTES, (unsigned long long)(v[A_MQ] + MQ_PAGE_TABLE_SIZE), (unsigned long long)(v[A_MQ] + MQ_PAGE_TABLE_SIZE + MQ_CMD_QUEUE_SIZE)); dump(mt, 64); |
| 802 | return emit(out, mt, PAGE_SZ); |
| 803 | } |
| 804 | if (!strcmp(mode, "radix3")) { |
| 805 | radix3_layout l; const char *e = radix3_plan(v[A_ELF], &l); if (e) return refused(e); |
| 806 | printf("radix3 ok data_pages=%llu l2_pages=%llu l1_pages=%llu l0_pages=1 table_bytes=%llu alloc_bytes=%llu data_offset=0x%llx\n", |
| 807 | (unsigned long long)l.npages[3], (unsigned long long)l.npages[2], (unsigned long long)l.npages[1], (unsigned long long)l.pt_size, (unsigned long long)l.alloc_size, (unsigned long long)l.offset[3]); |
| 808 | if (v[A_BASE]) { |
| 809 | uint8_t *rt = malloc(l.pt_size); size_t rn = 0; if (!rt) return refused("out of memory"); |
| 810 | e = radix3_tables(v[A_ELF], v[A_BASE], rt, l.pt_size, &rn); if (e) { free(rt); return refused(e); } |
| 811 | int rc = emit(out, rt, rn); |
| 812 | free(rt); |
| 813 | if (rc) return rc; |
| 814 | } |
| 815 | return 0; |
| 816 | } |
| 817 | if (!strncmp(mode, "check-", 6)) { |
| 818 | if (argc < 3) return usage(); |
| 819 | uint8_t *b; size_t n; if (read_file(argv[2], &b, &n)) return refused("cannot read file"); |
| 820 | const char *e = !strcmp(mode, "check-wprmeta") ? wpr_check(b, n, NULL) : !strcmp(mode, "check-libos") ? libos_check(b, n, NULL) : !strcmp(mode, "check-rmargs") ? rmargs_check(b, n, NULL) : "unknown check"; |
| 821 | free(b); |
| 822 | if (e) return refused(e); |
| 823 | printf("%s: OK\n", mode); return 0; |
| 824 | } |
| 825 | return usage(); |
| 826 | } |