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 / dev / gb10_gsp_boot.c
| 1 | /* gb10_gsp_boot.c -- GB10 GSP boot builder (see gb10_gsp_boot.h). Freestanding: no libc, no |
| 2 | * MMIO, no device access; writes little-endian bytes into a caller-owned arena only. */ |
| 3 | #include "gb10_gsp_boot.h" |
| 4 | |
| 5 | /* TEST-ONLY mutants (make gb10-gsp-mutants): each breaks one rule the host tests must catch. */ |
| 6 | #if (defined(GSP_MUTANT_SWAPRX) || defined(GSP_MUTANT_CKSUM) || defined(GSP_MUTANT_RADIX) || \ |
| 7 | defined(GSP_MUTANT_ALLOCLEN)) && !defined(GSP_MUTANT_HOST) |
| 8 | #error "GSP mutants are host-test builds only" |
| 9 | #endif |
| 10 | |
| 11 | /* ---- layout asserts: every offset the builder writes is taken from these mirrors, and the |
| 12 | * host test compares the mirrors with the NVIDIA headers themselves (test_gb10_gsp_nvhdr). ---- */ |
| 13 | _Static_assert(sizeof(gsp_mirror_wpr_meta) == GSP_WPR_META_SIZE, "GspFwWprMeta is 256 bytes"); |
| 14 | _Static_assert(offsetof(gsp_mirror_wpr_meta, sysmemAddrOfSignature) == 72, "WprMeta signature at 72"); |
| 15 | _Static_assert(offsetof(gsp_mirror_wpr_meta, gspFwHeapSize) == 128, "WprMeta heap size at 128"); |
| 16 | _Static_assert(offsetof(gsp_mirror_wpr_meta, gspFwHeapVfPartitionCount) == 240, "WprMeta VF count at 240"); |
| 17 | _Static_assert(offsetof(gsp_mirror_wpr_meta, verified) == 248, "WprMeta verified at 248"); |
| 18 | _Static_assert(sizeof(gsp_mirror_libos_arg) == GSP_LIBOS_ENTRY_SIZE, "LibosMemoryRegionInitArgument is 32"); |
| 19 | _Static_assert(offsetof(gsp_mirror_libos_arg, kind) == 24 && offsetof(gsp_mirror_libos_arg, loc) == 25, "libos kind/loc"); |
| 20 | _Static_assert(sizeof(gsp_mirror_args_cached) == GSP_ARGS_CACHED_SIZE, "GSP_ARGUMENTS_CACHED is 88"); |
| 21 | _Static_assert(offsetof(gsp_mirror_args_cached, sr) == 32 && offsetof(gsp_mirror_args_cached, gpuInstance) == 44 && |
| 22 | offsetof(gsp_mirror_args_cached, bDmemStack) == 48 && offsetof(gsp_mirror_args_cached, profilerArgs) == 56 && |
| 23 | offsetof(gsp_mirror_args_cached, sysmemHeapArgs) == 72, "GSP_ARGUMENTS_CACHED offsets"); |
| 24 | _Static_assert(sizeof(gsp_mirror_msgq_tx) == 32, "msgqTxHeader is 32"); |
| 25 | _Static_assert(sizeof(gsp_mirror_rpc_hdr) == GSP_RPC_HDR_SIZE, "rpc_message_header_v03_00 is 32"); |
| 26 | _Static_assert(offsetof(gsp_mirror_elem, rpc) == GSP_ELEM_HDR_SIZE && sizeof(gsp_mirror_elem) == GSP_ELEM_MIN_LEN, "GSP_MSG_QUEUE_ELEMENT"); |
| 27 | _Static_assert(sizeof(gsp_mirror_rm_alloc) == GSP_RM_ALLOC_FIXED, "rpc_gsp_rm_alloc_v03_00 fixed part"); |
| 28 | _Static_assert(sizeof(gsp_mirror_rm_control) == GSP_RM_CONTROL_FIXED, "rpc_gsp_rm_control_v03_00 fixed part"); |
| 29 | _Static_assert(sizeof(gsp_mirror_rm_free) == GSP_RM_FREE_SIZE, "rpc_free_v03_00"); |
| 30 | _Static_assert(sizeof(gsp_mirror_reg_entry) == 16, "PACKED_REGISTRY_ENTRY is 16"); |
| 31 | _Static_assert(sizeof(gsp_mirror_fmc_params) == GSP_FMC_PARAMS_SIZE, "GSP_FMC_BOOT_PARAMS is 88"); |
| 32 | _Static_assert(offsetof(gsp_mirror_fmc_params, boot.bIsGspRmBoot) == 36 && offsetof(gsp_mirror_fmc_params, rm) == 40 && |
| 33 | offsetof(gsp_mirror_fmc_params, spdm) == 56 && offsetof(gsp_mirror_fmc_params, mem) == 80, "FMC params offsets"); |
| 34 | _Static_assert(sizeof(gsp_mirror_cot) == GSP_COT_PAYLOAD_SIZE, "NVDM_PAYLOAD_COT is 860 packed"); |
| 35 | _Static_assert(offsetof(gsp_mirror_cot, gspBootArgsSysmemOffset) == 852, "COT boot args at 852"); |
| 36 | |
| 37 | #define OFF(T, f) ((uint32_t)offsetof(T, f)) |
| 38 | #define WM(f) OFF(gsp_mirror_wpr_meta, f) |
| 39 | |
| 40 | /* GB10B log set, registration order (kernel_gsp.c:3150-3240): seven buffers then RMARGS. */ |
| 41 | static const struct { const char *id; uint32_t size; } gb10b_logs[GSP_GB10B_LOG_COUNT] = { |
| 42 | {"LOGINIT", 0x10000}, {"LOGINTR", 0x10000}, {"LOGRM", 0x10000}, {"LOGMNOC", 0x10000}, |
| 43 | {"LOGDBG", 0x10000}, {"LOGROOT", 0x1000}, {"LOGKRNL", 0x10000}, |
| 44 | }; |
| 45 | |
| 46 | /* ---- byte helpers ---- */ |
| 47 | static void wr16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); } |
| 48 | static void wr32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) { p[i] = (uint8_t)v; v >>= 8; } } |
| 49 | static void wr64(uint8_t *p, uint64_t v) { for (int i = 0; i < 8; i++) { p[i] = (uint8_t)v; v >>= 8; } } |
| 50 | 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); } |
| 51 | static uint64_t rd64(const uint8_t *p) { return (uint64_t)rd32(p) | ((uint64_t)rd32(p + 4) << 32); } |
| 52 | static void bcopy_(uint8_t *d, const uint8_t *s, uint64_t n) { for (uint64_t i = 0; i < n; i++) d[i] = s[i]; } |
| 53 | static void bzero_(uint8_t *d, uint64_t n) { for (uint64_t i = 0; i < n; i++) d[i] = 0; } |
| 54 | static uint64_t up(uint64_t v, uint64_t a) { return (v + a - 1) & ~(a - 1); } |
| 55 | static uint32_t slen(const char *s) { uint32_t n = 0; while (s[n]) n++; return n; } |
| 56 | |
| 57 | uint32_t gsp_checksum32(const uint8_t *p, uint32_t len) |
| 58 | { |
| 59 | uint64_t x = 0; |
| 60 | for (uint32_t i = 0; i < len; i += 8) |
| 61 | x ^= rd64(p + i); |
| 62 | return (uint32_t)(x >> 32) ^ (uint32_t)x; |
| 63 | } |
| 64 | |
| 65 | /* _kgspGenerateInitArgId: up to 8 ASCII bytes, first byte most significant. */ |
| 66 | uint64_t gsp_init_arg_id(const char *name) |
| 67 | { |
| 68 | uint64_t id = 0; |
| 69 | for (int i = 0; i < 8 && name[i]; i++) |
| 70 | id = (id << 8) | (uint8_t)name[i]; |
| 71 | return id; |
| 72 | } |
| 73 | |
| 74 | /* ---- arena ---- */ |
| 75 | int gsp_arena_init(gsp_arena *a, void *base, uint64_t pa, uint64_t cap) |
| 76 | { |
| 77 | if (!a || !base || ((uintptr_t)base & (GSP_PAGE - 1)) || (pa & (GSP_PAGE - 1)) || pa + cap < pa) |
| 78 | return -1; |
| 79 | a->base = base; |
| 80 | a->pa = pa; |
| 81 | a->cap = cap; |
| 82 | a->used = 0; |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | /* Reserve size bytes, 4 KiB aligned, zeroed. Returns the host pointer and the PA. */ |
| 87 | static uint8_t *take(gsp_arena *a, uint64_t size, uint64_t *pa) |
| 88 | { |
| 89 | uint64_t off = up(a->used, GSP_PAGE); |
| 90 | uint64_t sz = up(size, GSP_PAGE); |
| 91 | if (off < a->used || sz < size || off + sz < off || off + sz > a->cap) |
| 92 | return NULL; |
| 93 | a->used = off + sz; |
| 94 | bzero_(a->base + off, sz); |
| 95 | *pa = a->pa + off; |
| 96 | return a->base + off; |
| 97 | } |
| 98 | |
| 99 | static int fits_dma(uint64_t pa, uint64_t size, unsigned bits) |
| 100 | { |
| 101 | if (bits >= 64) return 1; |
| 102 | uint64_t lim = 1ULL << bits; |
| 103 | return pa < lim && size <= lim - pa; |
| 104 | } |
| 105 | |
| 106 | /* ---- registry table (PACKED_REGISTRY_TABLE, RmPackageRegistry) ---- */ |
| 107 | uint32_t gsp_reg_table_size(const gsp_reg_entry *e, uint32_t n) |
| 108 | { |
| 109 | if (n == 0 || n > GSP_REG_MAX_ENTRIES) return 0; |
| 110 | uint64_t size = 8 + 16ULL * n; |
| 111 | for (uint32_t i = 0; i < n; i++) { |
| 112 | if (!e[i].name || !e[i].name[0]) return 0; |
| 113 | if (e[i].type == GSP_REG_DWORD) { if (e[i].len) return 0; } |
| 114 | else if (e[i].type == GSP_REG_BINARY || e[i].type == GSP_REG_STRING) { if (!e[i].len || !e[i].bytes) return 0; } |
| 115 | else return 0; |
| 116 | size += slen(e[i].name) + 1ULL + e[i].len; |
| 117 | } |
| 118 | return size > GSP_RPC_SIZE_MAX - GSP_RPC_HDR_SIZE ? 0 : (uint32_t)size; |
| 119 | } |
| 120 | |
| 121 | /* Writes the table header and entry array into head (8 + 16 n bytes) and describes the whole |
| 122 | * table as segments: head, then per entry its NUL-terminated name and, for BINARY / STRING, the |
| 123 | * value bytes (the driver's string area order). Returns the segment count. */ |
| 124 | static uint32_t reg_write(const gsp_reg_entry *e, uint32_t n, uint32_t size, uint8_t *head, gsp_seg *segs) |
| 125 | { |
| 126 | uint32_t ns = 0; |
| 127 | bzero_(head, 8 + 16 * n); |
| 128 | wr32(head, size); |
| 129 | wr32(head + 4, n); |
| 130 | segs[ns++] = (gsp_seg){ head, 8 + 16 * n }; |
| 131 | uint32_t off = 8 + 16 * n; |
| 132 | for (uint32_t i = 0; i < n; i++) { |
| 133 | uint8_t *ent = head + 8 + 16 * i; |
| 134 | uint32_t nl = slen(e[i].name) + 1; |
| 135 | wr32(ent + OFF(gsp_mirror_reg_entry, nameOffset), off); |
| 136 | segs[ns++] = (gsp_seg){ e[i].name, nl }; |
| 137 | off += nl; |
| 138 | ent[OFF(gsp_mirror_reg_entry, type)] = e[i].type; |
| 139 | if (e[i].type == GSP_REG_DWORD) { |
| 140 | wr32(ent + OFF(gsp_mirror_reg_entry, data), e[i].dword); |
| 141 | wr32(ent + OFF(gsp_mirror_reg_entry, length), 4); |
| 142 | } else { |
| 143 | wr32(ent + OFF(gsp_mirror_reg_entry, data), off); |
| 144 | wr32(ent + OFF(gsp_mirror_reg_entry, length), e[i].len); |
| 145 | segs[ns++] = (gsp_seg){ e[i].bytes, e[i].len }; |
| 146 | off += e[i].len; |
| 147 | } |
| 148 | } |
| 149 | return ns; |
| 150 | } |
| 151 | |
| 152 | /* ---- message queue ---- */ |
| 153 | #define TXH(f) OFF(gsp_mirror_msgq_tx, f) |
| 154 | #define RH(f) (GSP_ELEM_HDR_SIZE + OFF(gsp_mirror_rpc_hdr, f)) |
| 155 | |
| 156 | static uint32_t ring_free(uint32_t rd, uint32_t wr) { return (rd + GSP_MQ_MSG_COUNT - wr - 1) % GSP_MQ_MSG_COUNT; } |
| 157 | |
| 158 | int gsp_mq_attach(gsp_mq *q, const gsp_arena *a, const gsp_boot_plan *plan) |
| 159 | { |
| 160 | if (!q || !a || !plan || plan->cmdq_pa < a->pa || plan->statq_pa < a->pa || |
| 161 | plan->statq_pa - a->pa + GSP_MQ_QUEUE_SIZE > a->used || plan->cmdq_pa - a->pa + GSP_MQ_QUEUE_SIZE > a->used) |
| 162 | return -1; |
| 163 | q->cmdq = a->base + (plan->cmdq_pa - a->pa); |
| 164 | q->statq = a->base + (plan->statq_pa - a->pa); |
| 165 | q->tx_seq = plan->tx_seq; |
| 166 | q->rx_seq = 0; |
| 167 | q->rpc_seq = 0; |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /* Sequential reader over the 80-byte element+RPC header followed by the payload segments. */ |
| 172 | typedef struct { |
| 173 | const uint8_t *hdr; |
| 174 | const gsp_seg *segs; |
| 175 | uint32_t nsegs, seg, off, pos; |
| 176 | } cursor; |
| 177 | |
| 178 | static uint8_t next_byte(cursor *c) |
| 179 | { |
| 180 | if (c->pos < GSP_ELEM_HDR_SIZE + GSP_RPC_HDR_SIZE) |
| 181 | return c->hdr[c->pos++]; |
| 182 | c->pos++; |
| 183 | while (c->seg < c->nsegs && c->off >= c->segs[c->seg].len) { c->seg++; c->off = 0; } |
| 184 | if (c->seg >= c->nsegs) return 0; |
| 185 | const uint8_t *p = c->segs[c->seg].p; |
| 186 | uint8_t v = p ? p[c->off] : 0; |
| 187 | c->off++; |
| 188 | return v; |
| 189 | } |
| 190 | |
| 191 | int gsp_mq_send(gsp_mq *q, uint32_t function, const gsp_seg *segs, uint32_t nsegs, uint32_t rpc_len, |
| 192 | int sync, uint32_t *sequence_out, const char **why) |
| 193 | { |
| 194 | *why = 0; |
| 195 | if (!q || (nsegs && !segs)) { *why = "null argument"; return -1; } |
| 196 | uint64_t copy_len = 0; |
| 197 | for (uint32_t i = 0; i < nsegs; i++) copy_len += segs[i].len; |
| 198 | if (copy_len < rpc_len) { *why = "payload shorter than the RPC length"; return -1; } |
| 199 | uint64_t msg_len = (uint64_t)GSP_ELEM_HDR_SIZE + GSP_RPC_HDR_SIZE + rpc_len; |
| 200 | if (msg_len < GSP_ELEM_MIN_LEN || msg_len > GSP_ELEM_SIZE_MAX) { *why = "message length out of range"; return -1; } |
| 201 | uint32_t elems = (uint32_t)((msg_len + GSP_MQ_MSG_SIZE - 1) / GSP_MQ_MSG_SIZE); |
| 202 | uint64_t placed = (uint64_t)GSP_ELEM_HDR_SIZE + GSP_RPC_HDR_SIZE + copy_len; |
| 203 | if (placed > (uint64_t)elems * GSP_MQ_MSG_SIZE) { *why = "payload beyond the elements the length covers"; return -1; } |
| 204 | uint32_t wr = rd32(q->cmdq + TXH(writePtr)); |
| 205 | #ifdef GSP_MUTANT_SWAPRX |
| 206 | uint32_t rd = rd32(q->cmdq + GSP_MQ_RXHDR_OFF); |
| 207 | #else |
| 208 | uint32_t rd = rd32(q->statq + GSP_MQ_RXHDR_OFF); /* GSP's readPtr lives in GSP's own queue (SWAP_RX) */ |
| 209 | #endif |
| 210 | if (wr >= GSP_MQ_MSG_COUNT || rd >= GSP_MQ_MSG_COUNT) { *why = "ring pointer corrupt"; return -1; } |
| 211 | if (ring_free(rd, wr) < elems) { *why = "command queue full"; return -1; } |
| 212 | |
| 213 | uint32_t seq = sync ? q->rpc_seq : 0; |
| 214 | uint8_t hdr[GSP_ELEM_HDR_SIZE + GSP_RPC_HDR_SIZE]; |
| 215 | bzero_(hdr, sizeof hdr); |
| 216 | wr32(hdr + OFF(gsp_mirror_elem, seqNum), q->tx_seq); |
| 217 | wr32(hdr + OFF(gsp_mirror_elem, elemCount), elems); |
| 218 | wr32(hdr + RH(header_version), GSP_RPC_HEADER_VERSION); |
| 219 | wr32(hdr + RH(signature), GSP_RPC_SIGNATURE); |
| 220 | wr32(hdr + RH(length), GSP_RPC_HDR_SIZE + rpc_len); |
| 221 | wr32(hdr + RH(function), function); |
| 222 | wr32(hdr + RH(rpc_result), GSP_RPC_PENDING); |
| 223 | wr32(hdr + RH(rpc_result_private), GSP_RPC_PENDING); |
| 224 | wr32(hdr + RH(sequence), seq); |
| 225 | |
| 226 | /* _checkSum32 over msg_len, zero-padded to 8 (message_queue_cpu.c), checkSum field 0. */ |
| 227 | cursor c = { hdr, segs, nsegs, 0, 0, 0 }; |
| 228 | uint64_t x = 0; |
| 229 | for (uint64_t i = 0; i < up(msg_len, 8); i += 8) { |
| 230 | uint8_t w[8]; |
| 231 | for (uint32_t k = 0; k < 8; k++) { |
| 232 | uint8_t v = next_byte(&c); |
| 233 | w[k] = i + k < msg_len ? v : 0; |
| 234 | } |
| 235 | x ^= rd64(w); |
| 236 | } |
| 237 | #ifndef GSP_MUTANT_CKSUM |
| 238 | wr32(hdr + OFF(gsp_mirror_elem, checkSum), (uint32_t)(x >> 32) ^ (uint32_t)x); |
| 239 | #endif |
| 240 | |
| 241 | /* Slot e of the message gets bytes [e*4096, e*4096 + 4096): header, payload, zeros. */ |
| 242 | c = (cursor){ hdr, segs, nsegs, 0, 0, 0 }; |
| 243 | for (uint32_t e = 0; e < elems; e++) { |
| 244 | uint8_t *slot = q->cmdq + GSP_MQ_ENTRY_OFF + ((wr + e) % GSP_MQ_MSG_COUNT) * GSP_MQ_MSG_SIZE; |
| 245 | for (uint32_t k = 0; k < GSP_MQ_MSG_SIZE; k++) { |
| 246 | uint64_t b = (uint64_t)e * GSP_MQ_MSG_SIZE + k; |
| 247 | slot[k] = b < placed ? next_byte(&c) : 0; |
| 248 | } |
| 249 | } |
| 250 | /* message_queue_cpu.c:574-576: store fence before the writePtr publish (the doorbell after it is |
| 251 | * the caller's; this file writes no register). */ |
| 252 | __atomic_thread_fence(__ATOMIC_SEQ_CST); |
| 253 | wr32(q->cmdq + TXH(writePtr), (wr + elems) % GSP_MQ_MSG_COUNT); |
| 254 | q->tx_seq++; |
| 255 | if (sync) q->rpc_seq++; |
| 256 | if (sequence_out) *sequence_out = seq; |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | int gsp_rpc_rm_alloc(gsp_mq *q, uint32_t hClient, uint32_t hParent, uint32_t hObject, uint32_t hClass, |
| 261 | const void *params, uint32_t params_size, uint32_t *seq, const char **why) |
| 262 | { |
| 263 | uint8_t fixed[GSP_RM_ALLOC_FIXED]; |
| 264 | if (params_size && !params) { *why = "null argument"; return -1; } |
| 265 | bzero_(fixed, sizeof fixed); |
| 266 | wr32(fixed + OFF(gsp_mirror_rm_alloc, hClient), hClient); |
| 267 | wr32(fixed + OFF(gsp_mirror_rm_alloc, hParent), hParent); |
| 268 | wr32(fixed + OFF(gsp_mirror_rm_alloc, hObject), hObject); |
| 269 | wr32(fixed + OFF(gsp_mirror_rm_alloc, hClass), hClass); |
| 270 | wr32(fixed + OFF(gsp_mirror_rm_alloc, paramsSize), params_size); |
| 271 | gsp_seg s[2] = { { fixed, sizeof fixed }, { params, params_size } }; |
| 272 | /* rpcRmApiAlloc_GSP: rpcWriteCommonHeader(.., sizeof(rpc_gsp_rm_alloc_v03_00)); the params |
| 273 | * follow outside the length. Kept as the driver does it (inventory section 14). */ |
| 274 | #ifdef GSP_MUTANT_ALLOCLEN |
| 275 | return gsp_mq_send(q, GSP_FN_GSP_RM_ALLOC, s, 2, GSP_RM_ALLOC_FIXED + params_size, 1, seq, why); |
| 276 | #else |
| 277 | return gsp_mq_send(q, GSP_FN_GSP_RM_ALLOC, s, 2, GSP_RM_ALLOC_FIXED, 1, seq, why); |
| 278 | #endif |
| 279 | } |
| 280 | |
| 281 | int gsp_rpc_rm_control(gsp_mq *q, uint32_t hClient, uint32_t hObject, uint32_t cmd, |
| 282 | const void *params, uint32_t params_size, uint32_t *seq, const char **why) |
| 283 | { |
| 284 | uint8_t fixed[GSP_RM_CONTROL_FIXED]; |
| 285 | if (params_size && !params) { *why = "null argument"; return -1; } |
| 286 | if (params_size > GSP_RPC_SIZE_MAX - GSP_RPC_HDR_SIZE - GSP_RM_CONTROL_FIXED) { |
| 287 | *why = "control params need continuation records (not built)"; |
| 288 | return -1; |
| 289 | } |
| 290 | bzero_(fixed, sizeof fixed); |
| 291 | wr32(fixed + OFF(gsp_mirror_rm_control, hClient), hClient); |
| 292 | wr32(fixed + OFF(gsp_mirror_rm_control, hObject), hObject); |
| 293 | wr32(fixed + OFF(gsp_mirror_rm_control, cmd), cmd); |
| 294 | wr32(fixed + OFF(gsp_mirror_rm_control, paramsSize), params_size); |
| 295 | gsp_seg s[2] = { { fixed, sizeof fixed }, { params, params_size } }; |
| 296 | return gsp_mq_send(q, GSP_FN_GSP_RM_CONTROL, s, 2, GSP_RM_CONTROL_FIXED + params_size, 1, seq, why); |
| 297 | } |
| 298 | |
| 299 | int gsp_rpc_rm_free(gsp_mq *q, uint32_t hClient, uint32_t hObject, uint32_t *seq, const char **why) |
| 300 | { |
| 301 | uint8_t buf[GSP_RM_FREE_SIZE]; |
| 302 | bzero_(buf, sizeof buf); |
| 303 | wr32(buf + OFF(gsp_mirror_rm_free, hRoot), hClient); |
| 304 | wr32(buf + OFF(gsp_mirror_rm_free, hObjectParent), 0); /* NV01_NULL_OBJECT */ |
| 305 | wr32(buf + OFF(gsp_mirror_rm_free, hObjectOld), hObject); |
| 306 | gsp_seg s = { buf, sizeof buf }; |
| 307 | return gsp_mq_send(q, GSP_FN_FREE, &s, 1, sizeof buf, 1, seq, why); |
| 308 | } |
| 309 | |
| 310 | int gsp_rpc_get_static_info(gsp_mq *q, uint32_t static_info_size, uint32_t *seq, const char **why) |
| 311 | { |
| 312 | /* rpcGetGspStaticInfo_v14_00: zeroed request, length = 32 + sizeof(GspStaticConfigInfo), |
| 313 | * refused by the driver above maxRpcSize. The size is a parameter: the struct pulls in |
| 314 | * RM-internal headers that do not compile standalone (inventory section 14). */ |
| 315 | if (static_info_size == 0 || static_info_size > GSP_RPC_SIZE_MAX - GSP_RPC_HDR_SIZE) { |
| 316 | *why = "static info size out of range"; |
| 317 | return -1; |
| 318 | } |
| 319 | gsp_seg s = { 0, static_info_size }; |
| 320 | return gsp_mq_send(q, GSP_FN_GET_GSP_STATIC_INFO, &s, 1, static_info_size, 1, seq, why); |
| 321 | } |
| 322 | |
| 323 | int gsp_mq_recv(gsp_mq *q, uint8_t *out, uint32_t cap, uint32_t *function, uint32_t *rpc_result, |
| 324 | const char **why) |
| 325 | { |
| 326 | *why = 0; |
| 327 | if (!q || !out) { *why = "null argument"; return -1; } |
| 328 | const uint8_t *tx = q->statq; |
| 329 | if (rd32(tx + TXH(version)) != 0 || rd32(tx + TXH(size)) != GSP_MQ_QUEUE_SIZE || |
| 330 | rd32(tx + TXH(msgSize)) != GSP_MQ_MSG_SIZE || rd32(tx + TXH(msgCount)) != GSP_MQ_MSG_COUNT || |
| 331 | rd32(tx + TXH(rxHdrOff)) < 32 || rd32(tx + TXH(entryOff)) != GSP_MQ_ENTRY_OFF) { |
| 332 | *why = "status queue header not the driver's shape"; |
| 333 | return -1; |
| 334 | } |
| 335 | uint32_t wr = rd32(tx + TXH(writePtr)); |
| 336 | __atomic_thread_fence(__ATOMIC_SEQ_CST); /* read the slots only after the writePtr that covers them */ |
| 337 | uint8_t *rdp = q->cmdq + GSP_MQ_RXHDR_OFF; /* our readPtr lives in our own queue (SWAP_RX) */ |
| 338 | uint32_t rd = rd32(rdp); |
| 339 | if (wr >= GSP_MQ_MSG_COUNT || rd >= GSP_MQ_MSG_COUNT) { *why = "ring pointer corrupt"; return -1; } |
| 340 | uint32_t avail = (wr + GSP_MQ_MSG_COUNT - rd) % GSP_MQ_MSG_COUNT; |
| 341 | if (avail == 0) return 0; |
| 342 | const uint8_t *first = tx + GSP_MQ_ENTRY_OFF + rd * GSP_MQ_MSG_SIZE; |
| 343 | uint32_t elems = rd32(first + OFF(gsp_mirror_elem, elemCount)); |
| 344 | uint32_t len = rd32(first + RH(length)); |
| 345 | if (elems == 0 || elems > avail || elems > GSP_ELEM_SIZE_MAX / GSP_MQ_MSG_SIZE) { *why = "element count bad"; return -1; } |
| 346 | if (len < GSP_RPC_HDR_SIZE || GSP_ELEM_HDR_SIZE + len > elems * GSP_MQ_MSG_SIZE) { *why = "rpc length bad"; return -1; } |
| 347 | uint32_t msg_len = GSP_ELEM_HDR_SIZE + len; |
| 348 | if (cap < up(msg_len, 8)) { *why = "output too small"; return -1; } |
| 349 | for (uint32_t b = 0; b < (uint32_t)up(msg_len, 8); b++) { |
| 350 | uint32_t e = b / GSP_MQ_MSG_SIZE; |
| 351 | out[b] = b < msg_len ? tx[GSP_MQ_ENTRY_OFF + ((rd + e) % GSP_MQ_MSG_COUNT) * GSP_MQ_MSG_SIZE + b % GSP_MQ_MSG_SIZE] : 0; |
| 352 | } |
| 353 | if (gsp_checksum32(out, (uint32_t)up(msg_len, 8)) != 0) { *why = "checksum not zero"; return -1; } |
| 354 | if (rd32(out + OFF(gsp_mirror_elem, seqNum)) != q->rx_seq) { *why = "bad sequence number"; return -1; } |
| 355 | if (rd32(out + RH(signature)) != GSP_RPC_SIGNATURE) { *why = "rpc signature bad"; return -1; } |
| 356 | __atomic_thread_fence(__ATOMIC_SEQ_CST); /* finish reading before handing the slots back */ |
| 357 | wr32(rdp, (rd + elems) % GSP_MQ_MSG_COUNT); |
| 358 | q->rx_seq++; |
| 359 | if (function) *function = rd32(out + RH(function)); |
| 360 | if (rpc_result) *rpc_result = rd32(out + RH(rpc_result)); |
| 361 | return 1; |
| 362 | } |
| 363 | |
| 364 | /* ---- the boot build ---- */ |
| 365 | #define FAIL(s) do { *why = (s); return -1; } while (0) |
| 366 | |
| 367 | int gsp_boot_build(gsp_arena *a, const gsp_boot_in *in, gsp_boot_plan *out, const char **why) |
| 368 | { |
| 369 | *why = 0; |
| 370 | if (!a || !in || !out) FAIL("null argument"); |
| 371 | if (!in->rm_elf || !in->rm_elf_size) FAIL("GSP-RM ELF missing"); |
| 372 | if (!in->rm_sig || !in->rm_sig_size) FAIL("GSP-RM signature missing"); |
| 373 | if (!in->bootloader || in->bootloader_size != GSP_GB10B_BOOTLOADER_SIZE) FAIL("bootloader is not the GB10B GspRmBoot image size"); |
| 374 | if (in->bl_code_off != GSP_GB10B_BL_CODE_OFF || in->bl_data_off != GSP_GB10B_BL_DATA_OFF || |
| 375 | in->bl_manifest_off != GSP_GB10B_BL_MANIFEST_OFF) FAIL("bootloader descriptor offsets are not the GB10B values"); |
| 376 | if (!in->fmc || !in->fmc_size) FAIL("GSP-FMC image missing"); |
| 377 | if (!in->fmc_hash || !in->fmc_pubkey || !in->fmc_sig) FAIL("GSP-FMC hash, key or signature missing"); |
| 378 | if (!in->sysmem_heap_pa || (in->sysmem_heap_pa & (GSP_PAGE - 1)) || !in->sysmem_heap_size || |
| 379 | (in->sysmem_heap_size & (GSP_PAGE - 1))) FAIL("sysmem heap zero or not 4 KiB aligned"); |
| 380 | if (!in->system_info) FAIL("GspSystemInfo missing"); |
| 381 | uint32_t reg_size = 0; |
| 382 | if (in->nregs) { |
| 383 | if (!in->regs || !(reg_size = gsp_reg_table_size(in->regs, in->nregs))) FAIL("registry entry invalid or too large"); |
| 384 | } |
| 385 | if (in->dma_bits == 0 || in->dma_bits > 64) FAIL("dma_bits out of range"); |
| 386 | if (a->used) FAIL("arena not empty"); |
| 387 | |
| 388 | bzero_((uint8_t *)out, sizeof *out); |
| 389 | uint64_t pa; |
| 390 | uint8_t *p; |
| 391 | |
| 392 | /* 1. WprMeta page (filled last, needs the other addresses). */ |
| 393 | uint8_t *wpr = take(a, GSP_PAGE, &pa); |
| 394 | if (!wpr) FAIL("arena too small"); |
| 395 | out->wpr_meta = (gsp_region){pa, GSP_WPR_META_SIZE}; |
| 396 | |
| 397 | /* 2. libos init-argument page. */ |
| 398 | uint8_t *libos = take(a, GSP_PAGE, &pa); |
| 399 | if (!libos) FAIL("arena too small"); |
| 400 | out->libos_args = (gsp_region){pa, GSP_PAGE}; |
| 401 | |
| 402 | /* 3. log buffers: u64 put pointer 0, then one u64 PA per 4 KiB page. */ |
| 403 | for (uint32_t i = 0; i < GSP_GB10B_LOG_COUNT; i++) { |
| 404 | uint32_t sz = gb10b_logs[i].size; |
| 405 | if (!(p = take(a, sz, &pa))) FAIL("arena too small"); |
| 406 | for (uint32_t k = 0; k < sz / GSP_PAGE; k++) |
| 407 | wr64(p + 8 + 8 * k, pa + (uint64_t)k * GSP_PAGE); |
| 408 | out->log[i] = (gsp_region){pa, sz}; |
| 409 | } |
| 410 | |
| 411 | /* 4. GSP_ARGUMENTS_CACHED page. */ |
| 412 | uint8_t *args = take(a, GSP_PAGE, &pa); |
| 413 | if (!args) FAIL("arena too small"); |
| 414 | out->args_cached = (gsp_region){pa, GSP_PAGE}; |
| 415 | |
| 416 | /* 5. RPC shared buffer: page table, command queue, status queue. */ |
| 417 | uint8_t *shared = take(a, GSP_MQ_SHARED_SIZE, &pa); |
| 418 | if (!shared) FAIL("arena too small"); |
| 419 | out->rpc_shared = (gsp_region){pa, GSP_MQ_SHARED_SIZE}; |
| 420 | for (uint32_t k = 0; k < GSP_MQ_NUM_PTES; k++) |
| 421 | wr64(shared + 8 * k, pa + (uint64_t)k * GSP_PAGE); |
| 422 | out->cmdq_pa = pa + GSP_MQ_CMDQ_OFF; |
| 423 | out->statq_pa = pa + GSP_MQ_STATQ_OFF; |
| 424 | uint8_t *cmdq = shared + GSP_MQ_CMDQ_OFF; |
| 425 | wr32(cmdq + TXH(version), 0); |
| 426 | wr32(cmdq + TXH(size), GSP_MQ_QUEUE_SIZE); |
| 427 | wr32(cmdq + TXH(msgSize), GSP_MQ_MSG_SIZE); |
| 428 | wr32(cmdq + TXH(msgCount), GSP_MQ_MSG_COUNT); |
| 429 | wr32(cmdq + TXH(writePtr), 0); |
| 430 | wr32(cmdq + TXH(flags), GSP_MQ_FLAGS_SWAP_RX); |
| 431 | wr32(cmdq + TXH(rxHdrOff), GSP_MQ_RXHDR_OFF); |
| 432 | wr32(cmdq + TXH(entryOff), GSP_MQ_ENTRY_OFF); |
| 433 | |
| 434 | /* libos entries: logs in order, then RMARGS -> GSP_ARGUMENTS_CACHED. */ |
| 435 | for (uint32_t i = 0; i <= GSP_GB10B_LOG_COUNT; i++) { |
| 436 | uint8_t *e = libos + i * GSP_LIBOS_ENTRY_SIZE; |
| 437 | int rm = i == GSP_GB10B_LOG_COUNT; |
| 438 | wr64(e + OFF(gsp_mirror_libos_arg, id8), gsp_init_arg_id(rm ? "RMARGS" : gb10b_logs[i].id)); |
| 439 | wr64(e + OFF(gsp_mirror_libos_arg, pa), rm ? out->args_cached.pa : out->log[i].pa); |
| 440 | wr64(e + OFF(gsp_mirror_libos_arg, size), rm ? GSP_PAGE : out->log[i].size); |
| 441 | e[OFF(gsp_mirror_libos_arg, kind)] = GSP_LIBOS_KIND_CONTIGUOUS; |
| 442 | e[OFF(gsp_mirror_libos_arg, loc)] = GSP_LIBOS_LOC_SYSMEM; |
| 443 | } |
| 444 | |
| 445 | /* GSP_ARGUMENTS_CACHED fill. */ |
| 446 | wr64(args + OFF(gsp_mirror_args_cached, mq.sharedMemPhysAddr), out->rpc_shared.pa); |
| 447 | wr32(args + OFF(gsp_mirror_args_cached, mq.pageTableEntryCount), GSP_MQ_NUM_PTES); |
| 448 | wr64(args + OFF(gsp_mirror_args_cached, mq.cmdQueueOffset), GSP_MQ_CMDQ_OFF); |
| 449 | wr64(args + OFF(gsp_mirror_args_cached, mq.statQueueOffset), GSP_MQ_STATQ_OFF); |
| 450 | wr32(args + OFF(gsp_mirror_args_cached, gpuInstance), in->gpu_instance); |
| 451 | args[OFF(gsp_mirror_args_cached, bDmemStack)] = 1; |
| 452 | wr64(args + OFF(gsp_mirror_args_cached, sysmemHeapArgs.pa), in->sysmem_heap_pa); |
| 453 | wr64(args + OFF(gsp_mirror_args_cached, sysmemHeapArgs.size), in->sysmem_heap_size); |
| 454 | |
| 455 | /* 6. radix-3: levels 0..2 back to back, then the ELF data pages. */ |
| 456 | uint64_t np[4], lvl_off[4] = {0, 0, 0, 0}, n = 0; |
| 457 | np[3] = (in->rm_elf_size + GSP_PAGE - 1) / GSP_PAGE; |
| 458 | for (int i = 3; i > 0; i--) np[i - 1] = ((np[i] - 1) >> 9) + 1; |
| 459 | if (np[0] != 1) FAIL("ELF too large for a 3-level radix table"); |
| 460 | for (int i = 1; i < 4; i++) { n += np[i - 1]; lvl_off[i] = n * GSP_PAGE; } |
| 461 | uint64_t r3_size = n * GSP_PAGE + np[3] * GSP_PAGE; |
| 462 | if (!(p = take(a, r3_size, &pa))) FAIL("arena too small"); |
| 463 | for (int i = 0; i < 3; i++) |
| 464 | for (uint64_t k = 0; k < np[i + 1]; k++) |
| 465 | #ifdef GSP_MUTANT_RADIX |
| 466 | wr64(p + lvl_off[i] + k * 8, pa + lvl_off[i + 1] + (k + 1) * GSP_PAGE); |
| 467 | #else |
| 468 | wr64(p + lvl_off[i] + k * 8, pa + lvl_off[i + 1] + k * GSP_PAGE); |
| 469 | #endif |
| 470 | bcopy_(p + lvl_off[3], in->rm_elf, in->rm_elf_size); |
| 471 | out->radix3 = (gsp_region){pa, r3_size}; |
| 472 | out->radix3_root_pa = pa; |
| 473 | out->elf_data_pa = pa + lvl_off[3]; |
| 474 | for (int i = 0; i < 4; i++) out->radix3_level_pages[i] = (uint32_t)np[i]; |
| 475 | |
| 476 | /* 7. bootloader, 8. signature (size rounded up to 256), 9. FMC image. */ |
| 477 | if (!(p = take(a, in->bootloader_size, &pa))) FAIL("arena too small"); |
| 478 | bcopy_(p, in->bootloader, in->bootloader_size); |
| 479 | out->bootloader = (gsp_region){pa, in->bootloader_size}; |
| 480 | uint64_t sig_sz = up(in->rm_sig_size, 256); |
| 481 | if (!(p = take(a, sig_sz, &pa))) FAIL("arena too small"); |
| 482 | bcopy_(p, in->rm_sig, in->rm_sig_size); |
| 483 | out->signature = (gsp_region){pa, sig_sz}; |
| 484 | if (!(p = take(a, in->fmc_size, &pa))) FAIL("arena too small"); |
| 485 | bcopy_(p, in->fmc, in->fmc_size); |
| 486 | out->fmc = (gsp_region){pa, in->fmc_size}; |
| 487 | |
| 488 | /* 10. GSP_FMC_BOOT_PARAMS. */ |
| 489 | uint8_t *fp = take(a, GSP_PAGE, &pa); |
| 490 | if (!fp) FAIL("arena too small"); |
| 491 | out->fmc_params = (gsp_region){pa, GSP_FMC_PARAMS_SIZE}; |
| 492 | #define FP(f) OFF(gsp_mirror_fmc_params, f) |
| 493 | wr32(fp + FP(regkeys), in->fmc_regkeys); |
| 494 | wr32(fp + FP(boot.target), GSP_DMA_TARGET_COHERENT); /* WprMeta page is cached sysmem */ |
| 495 | wr32(fp + FP(boot.gspRmDescSize), GSP_WPR_META_SIZE); |
| 496 | wr64(fp + FP(boot.gspRmDescOffset), out->wpr_meta.pa); |
| 497 | fp[FP(boot.bIsGspRmBoot)] = 1; |
| 498 | wr32(fp + FP(rm.target), GSP_DMA_TARGET_NONCOHERENT); /* libos args page is uncached */ |
| 499 | wr64(fp + FP(rm.bootArgsOffset), out->libos_args.pa); |
| 500 | |
| 501 | /* WprMeta (gh100 populate + GB10B overrides; everything FMC-owned stays 0). */ |
| 502 | wr64(wpr + WM(magic), GSP_WPR_META_MAGIC); |
| 503 | wr64(wpr + WM(revision), GSP_WPR_META_REVISION); |
| 504 | wr64(wpr + WM(sysmemAddrOfRadix3Elf), out->radix3_root_pa); |
| 505 | wr64(wpr + WM(sizeOfRadix3Elf), in->rm_elf_size); |
| 506 | wr64(wpr + WM(sysmemAddrOfBootloader), out->bootloader.pa); |
| 507 | wr64(wpr + WM(sizeOfBootloader), in->bootloader_size); |
| 508 | wr64(wpr + WM(bootloaderCodeOffset), in->bl_code_off); |
| 509 | wr64(wpr + WM(bootloaderDataOffset), in->bl_data_off); |
| 510 | wr64(wpr + WM(bootloaderManifestOffset), in->bl_manifest_off); |
| 511 | wr64(wpr + WM(sysmemAddrOfSignature), out->signature.pa); |
| 512 | wr64(wpr + WM(sizeOfSignature), sig_sz); |
| 513 | wr64(wpr + WM(gspFwHeapSize), GSP_GB10B_FW_HEAP_SIZE); |
| 514 | |
| 515 | /* Every chip-visible address must fit the DMA window. */ |
| 516 | if (!fits_dma(a->pa, a->used, in->dma_bits) || !fits_dma(in->sysmem_heap_pa, in->sysmem_heap_size, in->dma_bits)) |
| 517 | FAIL("address beyond the DMA window"); |
| 518 | |
| 519 | /* Pre-boot RPCs, async (sequence 0), in kgspInitRm order. */ |
| 520 | gsp_mq q = { .cmdq = cmdq, .statq = shared + GSP_MQ_STATQ_OFF, .tx_seq = 0, .rx_seq = 0, .rpc_seq = 0 }; |
| 521 | /* The status queue is GSP's to create; until then its rx header (GSP's readPtr) is 0. */ |
| 522 | gsp_seg si = { in->system_info, GSP_SYSTEM_INFO_SIZE }; |
| 523 | if (gsp_mq_send(&q, GSP_FN_SET_SYSTEM_INFO, &si, 1, GSP_SYSTEM_INFO_SIZE, 0, 0, why)) |
| 524 | return -1; |
| 525 | if (in->nregs) { |
| 526 | uint8_t head[8 + 16 * GSP_REG_MAX_ENTRIES]; |
| 527 | gsp_seg segs[1 + 2 * GSP_REG_MAX_ENTRIES]; |
| 528 | uint32_t ns = reg_write(in->regs, in->nregs, reg_size, head, segs); |
| 529 | if (gsp_mq_send(&q, GSP_FN_SET_REGISTRY, segs, ns, reg_size, 0, 0, why)) |
| 530 | return -1; |
| 531 | } |
| 532 | out->tx_seq = q.tx_seq; |
| 533 | out->cmdq_write_ptr = rd32(cmdq + TXH(writePtr)); |
| 534 | |
| 535 | /* SEC2 packet: MCTP dword, NVDM dword (type 0x7e, vendor 0x10de, NVDM type COT 0x14), then the |
| 536 | * 860-byte payload; tail = padded length - 4. The MCTP dword on GB10B is the constant 1, not SOM|EOM: |
| 537 | * kernel_sec2.c:320 via ksec2CreateMctpHeader_HAL; on GB10B the HAL is ksec2CreateMctpHeader_539ab4 = `return 1;` (g_kernel_sec2_nvoc.c:786-788, g_kernel_sec2_nvoc.h:853-855); only GB20B/GB20C build SOM|EOM (kernel_sec2_gb20b.c:206-219). */ |
| 538 | uint8_t *pk = out->cot_packet; |
| 539 | wr32(pk, 1u); |
| 540 | wr32(pk + 4, 0x7eu | (0x10deu << 8) | (0x14u << 24)); |
| 541 | uint8_t *cot = pk + 8; |
| 542 | #define CO(f) OFF(gsp_mirror_cot, f) |
| 543 | wr16(cot + CO(version), GSP_COT_VERSION); |
| 544 | wr16(cot + CO(size), GSP_COT_PAYLOAD_SIZE); |
| 545 | wr64(cot + CO(gspFmcSysmemOffset), out->fmc.pa); |
| 546 | wr32(cot + CO(frtsSysmemSize), in->fmc_size); /* sic: the driver stores the FMC size here */ |
| 547 | bcopy_(cot + CO(hash384), in->fmc_hash, GSP_COT_HASH_SIZE); |
| 548 | bcopy_(cot + CO(publicKey), in->fmc_pubkey, GSP_COT_KEY_SIZE); |
| 549 | bcopy_(cot + CO(signature), in->fmc_sig, GSP_COT_SIG_SIZE); |
| 550 | wr64(cot + CO(gspBootArgsSysmemOffset), out->fmc_params.pa); |
| 551 | out->cot_packet_len = 8 + GSP_COT_PAYLOAD_SIZE; |
| 552 | out->emem_queue_tail = (uint32_t)up(out->cot_packet_len, 4) - 4; |
| 553 | return 0; |
| 554 | } |