Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/tools/ck_gsp_rpc_encode.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / tools / ck_gsp_rpc_encode.c` 610 lines ยท 44427 bytes Raw Blame History 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_rpc_encode -- host tool: byte-exact encoder, decoder and refusal 18 * tests for the GSP-RM message queue (command queue, status queue, queue 19 * element, RPC header, continuation records and the packed registry table) 20 * exactly as the pinned Linux driver (nvidia-open 580.173.02) builds them 21 * (aienos#286 cut C host work: "parsers and refusal tests for firmware 22 * metadata. No loading"). docs/GB10_FIRMWARE_INVENTORY.md section 13 is the 23 * report this tool backs. Nothing here touches a device. 24 * 25 * Every line below is from open-gpu-kernel-modules tag 580.173.02: 26 * 27 * 1. Shared queue library (src/nvidia/src/libraries/.../msgq.c, msgq_priv.h): 28 * msgqTxHeader 32 bytes {version, size, msgSize, msgCount, writePtr, 29 * flags, rxHdrOff, entryOff} written by the producer; msgqRxHeader 30 * 4 bytes {readPtr} written by the consumer. MSGQ_VERSION 0. 31 * msgqTxCreate(size, msgSize, hdrAlign, entryAlign, flags): 32 * rxHdrOff = ALIGN_UP(32, 1< size, size < entryOff + msgSize 35 * msgCount = (size - entryOff) / msgSize, writePtr 0, txFree = msgCount-1 36 * msgqRxLink(size, msgSize) on the peer's header refuses (return codes): 37 * -2 msgSize < 16; -3 msgSize > size; -6 size < entryOff + msgSize; 38 * -7 size mismatch; -8 msgSize mismatch; -9 version != 0; 39 * -10 rxHdrOff < 32 or entryOff < our rxHdrOff + 4 or 40 * msgCount != (size - entryOff) / msgSize. 41 * SWAP_RX (flag 1) on both sides: each side's readPtr for incoming 42 * messages lives in its OWN backing store at its rxHdrOff. 43 * Ring arithmetic (msgqTxGetFreeSpace / msgqRxGetReadAvailable): 44 * free = (readPtr + msgCount - writePtr - 1) mod msgCount 45 * avail = (writePtr + msgCount - readPtr) mod msgCount 46 * a pointer >= msgCount is corrupt: 0 free / 0 available. 47 * 48 * 2. The driver's queues (message_queue_cpu.c GspMsgQueuesInit, kernel_gsp 49 * tu102 queue setup): command queue 0x40000 bytes, status queue 0x40000 50 * bytes, both msgSize 4096 (GSP_MSG_QUEUE_ELEMENT_SIZE_MIN), hdrAlign 4 51 * (16 bytes), entryAlign 12 (4096), flags MSGQ_FLAGS_SWAP_RX. Hence 52 * rxHdrOff 32, entryOff 4096, msgCount 63, 62 usable slots. 53 * 54 * 3. GSP_MSG_QUEUE_ELEMENT (message_queue_priv.h:36-51): authTagBuffer[16], 55 * aadBuffer[16], u32 checkSum, u32 seqNum, u32 elemCount, then the RPC 56 * header aligned 8 at offset 48 (GSP_MSG_QUEUE_ELEMENT_HDR_SIZE 48); 57 * sizeof = 80. ELEMENT_SIZE_MIN 4096, MAX 16*4096 = 65536, 58 * BYTES_TO_ELEMENTS = ceil(bytes / 4096). RPC_SIZE_MAX = 65536-48 = 59 * 65488 (pRpc->maxRpcSize, kernel_gsp.c). 60 * 61 * 4. rpc_message_header_v03_00 (g_rpc-message-header.h), 32 bytes: 62 * u32 header_version, signature, length, function, rpc_result, 63 * rpc_result_private, sequence, cpuRmGfid. rpcWriteCommonHeader 64 * (rpc_common.c): zero the buffer, header_version = MAJOR 3 << 24 | 65 * MINOR 0 << 16 (rpc_headers.h:56-59), signature 0x43505256 66 * ("VRPC" little-endian, rpc_headers.h:61), rpc_result and 67 * rpc_result_private = RPC_PENDING 0xFFFFFFFF, function, length = 32 + 68 * payload. sequence: synchronous RPCs get pRpc->sequence++, asynchronous 69 * (_issueRpcAsync, pSequence NULL) get 0 (kernel_gsp.c:395-406). 70 * 71 * 5. GspMsgQueueSendCommand (message_queue_cpu.c:440-600), no CC: 72 * msgLen = 48 + rpc.length; refuse msgLen < 80 or > 65536 73 * (NV_ERR_INVALID_PARAM_STRUCT); zero-pad to a multiple of 8; 74 * seqNum = txSeqNum (0 at init, ++ per accepted message); elemCount = 75 * ceil(msgLen/4096); checkSum = 0 then checkSum = _checkSum32(msgLen): 76 * XOR of every u64 word, then HI32 ^ LO32, so the finished element 77 * checksums to zero (message_queue_priv.h:112-124). Elements are copied 78 * 4096 bytes at a time into successive ring slots, then submitted 79 * (msgqTxSubmitBuffers) and the doorbell NV_PGSP_QUEUE_HEAD(0) = 80 * 0x110c00 is written 0 (dev_gsp_tu102.h:38, kernel_gsp_tu102.c 81 * kgspSetCmdQueueHead). 82 * 83 * 6. GspMsgQueueReceiveStatus (:600-720): read element 0 of the status 84 * ring, nElements = elemCount, copy all; checksum over 48 + rpc.length 85 * must be 0 (NV_ERR_INVALID_DATA); seqNum must equal rxSeqNum ("Bad 86 * sequence number": an older seqNum is consumed and the read retried, 87 * up to 3 times), then rxSeqNum++. kgspWaitForRmInitDone polls for 88 * NV_VGPU_MSG_EVENT_GSP_INIT_DONE (0x1001) with sequence 0 and requires 89 * rpc_result == NV_OK. 90 * 91 * 7. Large RPCs (_issueRpcLarge, rpc.c:2038-2130): first message carries 92 * the real function with length = min(total, 65488); each further chunk 93 * is a fresh header with function CONTINUATION_RECORD (71), length = 94 * 32 + chunk, chunk <= 65488 - 32 = 65456 payload bytes. 95 * 96 * 8. SET_REGISTRY (function 73, rpcSetRegistry_v17_00, rpc.c ~10700 and 97 * RmPackageRegistry, arch/nvalloc/unix/src/registry.c:628-711) payload 98 * PACKED_REGISTRY_TABLE (g_os_nvoc.h:226-248): u32 size, u32 99 * numEntries, entries[] of {u32 nameOffset; u8 type; u32 data; u32 100 * length} (natural layout 16 bytes, entries at offset 8), then the 101 * string area: for each entry the NUL-terminated name, then for STRING 102 * (3) / BINARY (2) the value bytes with data = their offset; DWORD (1) 103 * keeps the value in data with length 4 and no bytes in the area. size = 104 * 8 + 16*n + sum(namelen+1) + sum(value bytes); numEntries == n; 105 * a zero-length string/binary is skipped by the driver with a warning 106 * and this encoder refuses it. An empty registry is NV_WARN_NOTHING_TO_DO 107 * and the RPC is not sent. 108 * 109 * 9. GSP_SET_SYSTEM_INFO (function 72, rpcGspSetSystemInfo_v17_00, 110 * rpc.c:10493) payload GspSystemInfo (gsp_static_config.h:172-229): 111 * PCI/BAR physical addresses, PCI ids, chipset, ACPI method data, 112 * hypervisor/VF state, host page size. Its contents come from the real 113 * device and the native kernel's bus layer; this tool records the 114 * function number, the header rule and the size: 936 bytes under the 115 * natural LP64 layout (no pack pragma in gsp_static_config.h; nested 116 * BUSINFO 10 bytes (g_chipset_nvoc.h:220-227), ACPI_METHOD_DATA 676 117 * bytes = bValid + DOD 72 + JT 12 + MUX 580 + CAPS 8 with 118 * NV0073_CTRL_SYSTEM_ACPI_ID_MAP_MAX_DISPLAYS 16 (gpu_acpi_data.h:72-115, 119 * ctrl0073system.h:747), GSP_VF_INFO 40, GSP_PCIE_CONFIG_REG 4), so the 120 * RPC length is 32 + 936 = 968 and it fits one 4 KiB slot. The size was 121 * reproduced by compiling the struct text copied from those headers. 122 * 123 * Claim tags: every number above is DOCUMENTED (source lines). That the 124 * GSP-RM firmware on GB10B accepts exactly these bytes without Linux present 125 * is INFERRED (medium) until the first attended run. 126 */ 127 #include 128 #include 129 #include 130 #include 131 #include 132 133 #define MSGQ_VERSION 0u 134 #define MSGQ_FLAGS_SWAP_RX 1u 135 #define MSGQ_MSG_SIZE_MIN 16u 136 #define MSGQ_TX_HDR_SIZE 32u 137 #define MSGQ_RX_HDR_SIZE 4u 138 #define GSP_MQ_SIZE 0x40000u 139 #define GSP_MQ_MSG_SIZE 4096u 140 #define GSP_MQ_HDR_ALIGN 4u /* 2^4 */ 141 #define GSP_MQ_ENTRY_ALIGN 12u /* 2^12 */ 142 #define GSP_MQ_RXHDR_OFF 32u 143 #define GSP_MQ_ENTRY_OFF 4096u 144 #define GSP_MQ_MSG_COUNT 63u 145 #define ELEM_HDR_SIZE 48u 146 #define ELEM_STRUCT_SIZE 80u 147 #define ELEM_SIZE_MIN 4096u 148 #define ELEM_SIZE_MAX 65536u 149 #define RPC_HDR_SIZE 32u 150 #define RPC_SIZE_MAX (ELEM_SIZE_MAX - ELEM_HDR_SIZE) /* 65488 */ 151 #define RPC_CONT_PAYLOAD_MAX (RPC_SIZE_MAX - RPC_HDR_SIZE) /* 65456 */ 152 #define RPC_HEADER_VERSION 0x03000000u 153 #define RPC_SIGNATURE_VALID 0x43505256u 154 #define RPC_PENDING 0xFFFFFFFFu 155 #define RPC_RESULT_UNKNOWN_FN 0xFF100001u 156 #define RPC_RESULT_BAD_FORMAT 0xFF100002u 157 #define FN_NOP 0u 158 #define FN_CONTINUATION_RECORD 71u 159 #define FN_GSP_SET_SYSTEM_INFO 72u 160 #define FN_SET_REGISTRY 73u 161 #define EV_GSP_INIT_DONE 0x1001u 162 #define EV_GSP_RUN_CPU_SEQUENCER 0x1002u 163 #define EV_RC_TRIGGERED 0x1004u 164 #define EV_GSP_LOCKDOWN_NOTICE 0x101cu 165 #define EV_GSP_POST_NOCAT_RECORD 0x1020u 166 #define NV_PGSP_QUEUE_HEAD_0 0x110c00u 167 #define GSP_SYSTEM_INFO_SIZE 936u 168 #define GSP_SYSTEM_INFO_RPC_LEN (RPC_HDR_SIZE + GSP_SYSTEM_INFO_SIZE) /* 968 */ 169 #define REG_TYPE_UNKNOWN 0u 170 #define REG_TYPE_DWORD 1u 171 #define REG_TYPE_BINARY 2u 172 #define REG_TYPE_STRING 3u 173 #define REG_TABLE_HDR 8u 174 #define REG_ENTRY_SIZE 16u 175 176 /* Compiler-checked layouts (natural alignment, LP64; same on aarch64 and the 177 * x86-64 CI host). */ 178 typedef struct { uint32_t version, size, msgSize, msgCount, writePtr, flags, rxHdrOff, entryOff; } msgq_tx_header; 179 typedef struct { uint32_t readPtr; } msgq_rx_header; 180 typedef struct { uint32_t header_version, signature, length, function, rpc_result, rpc_result_private, sequence, cpuRmGfid; } rpc_header; 181 typedef struct { uint8_t authTag[16], aad[16]; uint32_t checkSum, seqNum, elemCount; _Alignas(8) rpc_header rpc; } mq_element; 182 typedef struct { uint32_t nameOffset; uint8_t type; uint32_t data; uint32_t length; } packed_reg_entry; 183 typedef struct { uint32_t size, numEntries; } packed_reg_table_hdr; 184 _Static_assert(sizeof(msgq_tx_header) == MSGQ_TX_HDR_SIZE, "msgqTxHeader is 32 bytes"); 185 _Static_assert(sizeof(msgq_rx_header) == MSGQ_RX_HDR_SIZE, "msgqRxHeader is 4 bytes"); 186 _Static_assert(sizeof(rpc_header) == RPC_HDR_SIZE, "rpc_message_header_v is 32 bytes"); 187 _Static_assert(offsetof(mq_element, rpc) == ELEM_HDR_SIZE, "rpc header at 48"); 188 _Static_assert(sizeof(mq_element) == ELEM_STRUCT_SIZE, "GSP_MSG_QUEUE_ELEMENT is 80 bytes"); 189 _Static_assert(sizeof(packed_reg_entry) == REG_ENTRY_SIZE, "PACKED_REGISTRY_ENTRY is 16 bytes"); 190 _Static_assert(offsetof(packed_reg_entry, type) == 4 && offsetof(packed_reg_entry, data) == 8 && offsetof(packed_reg_entry, length) == 12, "entry field offsets"); 191 _Static_assert(sizeof(packed_reg_table_hdr) == REG_TABLE_HDR, "PACKED_REGISTRY_TABLE entries at 8"); 192 193 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; } 194 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); } 195 static void wr32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) { p[i] = (uint8_t)v; v >>= 8; } } 196 static uint32_t align_up(uint32_t v, uint32_t a) { return (v + a - 1) & ~(a - 1); } 197 198 /* ---- 1. queue headers ---- */ 199 typedef struct { uint32_t size, msgSize, hdrAlign, entryAlign, flags; } mq_create_in; 200 201 /* msgqTxCreate rules; writes the 32-byte producer header. */ 202 static const char *mq_tx_create(const mq_create_in *in, uint8_t out[MSGQ_TX_HDR_SIZE]) { 203 if (in->msgSize < MSGQ_MSG_SIZE_MIN || in->msgSize > in->size) return "msgSize out of range"; 204 if (in->hdrAlign < 2 || in->hdrAlign > 31 || in->entryAlign < 2 || in->entryAlign > 31) return "alignment out of range"; 205 uint32_t rxHdrOff = align_up(MSGQ_TX_HDR_SIZE, 1u << in->hdrAlign); 206 uint32_t entryOff = align_up(rxHdrOff + MSGQ_RX_HDR_SIZE, 1u << in->entryAlign); 207 if (in->size < entryOff + in->msgSize) return "size too small for one entry"; 208 msgq_tx_header h = { MSGQ_VERSION, in->size, in->msgSize, (in->size - entryOff) / in->msgSize, 0, in->flags, rxHdrOff, entryOff }; 209 memcpy(out, &h, sizeof h); 210 return NULL; 211 } 212 213 /* msgqRxLink checks on a peer header we read from shared memory. Returns the 214 * driver's negative code as text so a log line can be matched. our_rxHdrOff 215 * is our own header's rxHdrOff (the -10 rule compares against it). */ 216 static const char *mq_rx_link_check(const uint8_t *b, size_t n, uint32_t size, uint32_t msgSize, uint32_t our_rxHdrOff, msgq_tx_header *out) { 217 if (n < MSGQ_TX_HDR_SIZE) return "short header"; 218 if (msgSize < MSGQ_MSG_SIZE_MIN) return "-2 msgSize below 16"; 219 if (msgSize > size) return "-3 msgSize above size"; 220 msgq_tx_header h; memcpy(&h, b, sizeof h); 221 if (size < h.entryOff + msgSize) return "-6 size below entryOff + msgSize"; 222 if (h.size != size) return "-7 size mismatch"; 223 if (h.msgSize != msgSize) return "-8 msgSize mismatch"; 224 if (h.version != MSGQ_VERSION) return "-9 version not 0"; 225 if (h.rxHdrOff < MSGQ_TX_HDR_SIZE || h.entryOff < our_rxHdrOff + MSGQ_RX_HDR_SIZE || h.msgCount != (size - h.entryOff) / msgSize) return "-10 offsets or msgCount inconsistent"; 226 if (h.writePtr >= h.msgCount) return "writePtr beyond msgCount"; 227 if (out) *out = h; 228 return NULL; 229 } 230 231 static uint32_t ring_free(uint32_t readPtr, uint32_t writePtr, uint32_t msgCount) { 232 if (readPtr >= msgCount || writePtr >= msgCount || msgCount == 0) return 0; 233 return (readPtr + msgCount - writePtr - 1) % msgCount; 234 } 235 static uint32_t ring_avail(uint32_t readPtr, uint32_t writePtr, uint32_t msgCount) { 236 if (readPtr >= msgCount || writePtr >= msgCount || msgCount == 0) return 0; 237 return (writePtr + msgCount - readPtr) % msgCount; 238 } 239 240 /* ---- 2. RPC header + queue element ---- */ 241 static uint32_t checksum32(const uint8_t *p, size_t len) { 242 uint64_t x = 0; 243 for (size_t i = 0; i < len; i += 8) x ^= rd64(p + i); 244 return (uint32_t)(x >> 32) ^ (uint32_t)x; 245 } 246 247 typedef struct { uint32_t function, sequence, payload_len; const uint8_t *payload; uint32_t seqNum; } elem_in; 248 249 /* Build one message as the driver would place it in the ring: element header + 250 * RPC header + payload, zero padded to elemCount*4096. out must hold 65536. 251 * *msg_len gets the padded-to-8 message length used for the checksum. */ 252 static const char *elem_encode(const elem_in *in, uint8_t *out, size_t cap, uint32_t *msg_len, uint32_t *elem_count) { 253 if (in->payload_len > RPC_SIZE_MAX - RPC_HDR_SIZE) return "payload exceeds RPC_SIZE_MAX"; 254 uint32_t rpc_len = RPC_HDR_SIZE + in->payload_len; 255 uint32_t msgLen = ELEM_HDR_SIZE + rpc_len; 256 if (msgLen < ELEM_STRUCT_SIZE || msgLen > ELEM_SIZE_MAX) return "message length outside [80, 65536]"; 257 uint32_t nelem = (msgLen + ELEM_SIZE_MIN - 1) / ELEM_SIZE_MIN; 258 if (cap < (size_t)nelem * ELEM_SIZE_MIN) return "output too small"; 259 memset(out, 0, (size_t)nelem * ELEM_SIZE_MIN); 260 rpc_header h = { RPC_HEADER_VERSION, RPC_SIGNATURE_VALID, rpc_len, in->function, RPC_PENDING, RPC_PENDING, in->sequence, 0 }; 261 memcpy(out + ELEM_HDR_SIZE, &h, sizeof h); 262 if (in->payload_len) memcpy(out + ELEM_HDR_SIZE + RPC_HDR_SIZE, in->payload, in->payload_len); 263 wr32(out + 36, in->seqNum); 264 wr32(out + 40, nelem); 265 wr32(out + 32, 0); 266 uint32_t padded = align_up(msgLen, 8); 267 wr32(out + 32, checksum32(out, padded)); 268 if (msg_len) *msg_len = padded; 269 if (elem_count) *elem_count = nelem; 270 return NULL; 271 } 272 273 typedef struct { uint32_t checkSum, seqNum, elemCount; rpc_header rpc; uint32_t msgLen; } elem_view; 274 275 /* Receive-side validation (GspMsgQueueReceiveStatus + rpcRecvPoll checks). */ 276 static const char *elem_check(const uint8_t *b, size_t n, uint32_t expect_seq, elem_view *out) { 277 if (n < ELEM_STRUCT_SIZE) return "short element"; 278 elem_view v; v.checkSum = rd32(b + 32); v.seqNum = rd32(b + 36); v.elemCount = rd32(b + 40); 279 memcpy(&v.rpc, b + ELEM_HDR_SIZE, sizeof v.rpc); 280 if (v.rpc.signature != RPC_SIGNATURE_VALID) return "bad RPC signature"; 281 if (v.rpc.header_version != RPC_HEADER_VERSION) return "bad RPC header version"; 282 if (v.rpc.length < RPC_HDR_SIZE || v.rpc.length > RPC_SIZE_MAX) return "RPC length outside [32, 65488]"; 283 v.msgLen = ELEM_HDR_SIZE + v.rpc.length; 284 if (v.elemCount == 0 || v.elemCount > ELEM_SIZE_MAX / ELEM_SIZE_MIN) return "elemCount outside [1, 16]"; 285 if (v.elemCount != (v.msgLen + ELEM_SIZE_MIN - 1) / ELEM_SIZE_MIN) return "elemCount does not match length"; 286 if (n < (size_t)align_up(v.msgLen, 8)) return "element shorter than its RPC length"; 287 if (checksum32(b, align_up(v.msgLen, 8)) != 0) return "checksum not zero"; 288 if (v.seqNum != expect_seq) return v.seqNum < expect_seq ? "old seqNum (driver consumes and retries)" : "seqNum ahead of expected"; 289 if (out) *out = v; 290 return NULL; 291 } 292 293 /* ---- 3. continuation plan for a large RPC ---- */ 294 typedef struct { uint32_t chunks; uint32_t first_len; uint32_t last_payload; } cont_plan; 295 static const char *cont_plan_for(uint32_t total_rpc_len, cont_plan *p) { 296 if (total_rpc_len < RPC_HDR_SIZE) return "total below header"; 297 p->first_len = total_rpc_len < RPC_SIZE_MAX ? total_rpc_len : RPC_SIZE_MAX; 298 uint32_t rem = total_rpc_len - p->first_len; p->chunks = 1; p->last_payload = 0; 299 while (rem) { uint32_t c = rem < RPC_CONT_PAYLOAD_MAX ? rem : RPC_CONT_PAYLOAD_MAX; rem -= c; p->chunks++; p->last_payload = c; } 300 return NULL; 301 } 302 303 /* ---- 4. packed registry table ---- */ 304 typedef struct { const char *name; uint8_t type; uint32_t dword; const uint8_t *bytes; uint32_t len; } reg_in; 305 306 static const char *reg_encode(const reg_in *e, uint32_t n, uint8_t *out, size_t cap, uint32_t *size_out) { 307 if (n == 0) return "empty registry (driver: NV_WARN_NOTHING_TO_DO, RPC not sent)"; 308 uint32_t size = REG_TABLE_HDR + REG_ENTRY_SIZE * n; 309 for (uint32_t i = 0; i < n; i++) { 310 if (!e[i].name || !e[i].name[0]) return "entry without a name"; 311 if (e[i].type == REG_TYPE_DWORD) { if (e[i].len) return "DWORD entry carries bytes"; } 312 else if (e[i].type == REG_TYPE_STRING || e[i].type == REG_TYPE_BINARY) { if (e[i].len == 0) return "zero-length string/binary (driver would drop it)"; } 313 else return "unknown entry type"; 314 size += (uint32_t)strlen(e[i].name) + 1 + e[i].len; 315 } 316 if (size > RPC_SIZE_MAX - RPC_HDR_SIZE) return "registry exceeds one RPC (driver would use continuation records)"; 317 if (cap < size) return "output too small"; 318 memset(out, 0, size); 319 wr32(out, size); wr32(out + 4, n); 320 uint32_t off = REG_TABLE_HDR + REG_ENTRY_SIZE * n; 321 for (uint32_t i = 0; i < n; i++) { 322 uint8_t *ent = out + REG_TABLE_HDR + REG_ENTRY_SIZE * i; 323 uint32_t slen = (uint32_t)strlen(e[i].name) + 1; 324 wr32(ent, off); memcpy(out + off, e[i].name, slen); off += slen; 325 ent[4] = e[i].type; 326 if (e[i].type == REG_TYPE_DWORD) { wr32(ent + 8, e[i].dword); wr32(ent + 12, 4); } 327 else { wr32(ent + 8, off); wr32(ent + 12, e[i].len); memcpy(out + off, e[i].bytes, e[i].len); off += e[i].len; } 328 } 329 if (off != size) return "first/second pass mismatch"; 330 if (size_out) *size_out = size; 331 return NULL; 332 } 333 334 static const char *reg_check(const uint8_t *b, size_t n, uint32_t *entries_out) { 335 if (n < REG_TABLE_HDR) return "short table"; 336 uint32_t size = rd32(b), cnt = rd32(b + 4); 337 if (size != n) return "size field does not match buffer"; 338 if (cnt == 0) return "no entries"; 339 if ((uint64_t)REG_TABLE_HDR + (uint64_t)REG_ENTRY_SIZE * cnt > size) return "entry table overruns size"; 340 uint32_t expect_off = REG_TABLE_HDR + REG_ENTRY_SIZE * cnt; 341 for (uint32_t i = 0; i < cnt; i++) { 342 const uint8_t *ent = b + REG_TABLE_HDR + REG_ENTRY_SIZE * i; 343 uint32_t nameOff = rd32(ent), type = ent[4], data = rd32(ent + 8), len = rd32(ent + 12); 344 if (ent[5] || ent[6] || ent[7]) return "entry padding not zero"; 345 if (nameOff != expect_off) return "name not packed in order"; 346 uint32_t k = nameOff; while (k < size && b[k]) k++; 347 if (k >= size || k == nameOff) return "name not terminated or empty"; 348 expect_off = k + 1; 349 if (type == REG_TYPE_DWORD) { if (len != 4) return "DWORD length not 4"; } 350 else if (type == REG_TYPE_STRING || type == REG_TYPE_BINARY) { 351 if (len == 0 || data != expect_off) return "string/binary data offset or length wrong"; 352 if ((uint64_t)data + len > size) return "value overruns size"; 353 expect_off += len; 354 } else return "unknown entry type"; 355 } 356 if (expect_off != size) return "trailing bytes or size short"; 357 if (entries_out) *entries_out = cnt; 358 return NULL; 359 } 360 361 /* ---- self-test ---- */ 362 static int checks, failures; 363 static void check(int ok, const char *what) { checks++; if (!ok) { failures++; printf("FAIL: %s\n", what); } } 364 static void expect_refusal(const char *got, const char *want, const char *what) { 365 checks++; if (!got || strncmp(got, want, strlen(want))) { failures++; printf("FAIL: %s: got '%s' want '%s'\n", what, got ? got : "(accepted)", want); } 366 } 367 368 static int selftest(void) { 369 /* 1. the driver's own queue geometry */ 370 mq_create_in q = { GSP_MQ_SIZE, GSP_MQ_MSG_SIZE, GSP_MQ_HDR_ALIGN, GSP_MQ_ENTRY_ALIGN, MSGQ_FLAGS_SWAP_RX }; 371 uint8_t th[MSGQ_TX_HDR_SIZE]; 372 check(mq_tx_create(&q, th) == NULL, "command queue header encodes"); 373 static const uint8_t golden_tx[32] = { 0,0,0,0, 0,0,4,0, 0,16,0,0, 63,0,0,0, 0,0,0,0, 1,0,0,0, 32,0,0,0, 0,16,0,0 }; 374 check(memcmp(th, golden_tx, 32) == 0, "golden msgqTxHeader for 256 KiB / 4096 / swap-rx"); 375 check(rd32(th + 12) == GSP_MQ_MSG_COUNT && rd32(th + 24) == GSP_MQ_RXHDR_OFF && rd32(th + 28) == GSP_MQ_ENTRY_OFF, "msgCount 63, rxHdrOff 32, entryOff 4096"); 376 mq_create_in bad = q; bad.msgSize = 8; expect_refusal(mq_tx_create(&bad, th), "msgSize out of range", "msgSize below 16 refused"); 377 bad = q; bad.msgSize = GSP_MQ_SIZE + 1; expect_refusal(mq_tx_create(&bad, th), "msgSize out of range", "msgSize above size refused"); 378 bad = q; bad.size = 4096; expect_refusal(mq_tx_create(&bad, th), "size too small", "size without room for an entry refused"); 379 bad = q; bad.hdrAlign = 1; expect_refusal(mq_tx_create(&bad, th), "alignment out of range", "hdrAlign below 2 refused"); 380 /* peer link checks against the same header (GSP builds its status queue identically) */ 381 msgq_tx_header peer; 382 check(mq_rx_link_check(golden_tx, 32, GSP_MQ_SIZE, GSP_MQ_MSG_SIZE, GSP_MQ_RXHDR_OFF, &peer) == NULL && peer.msgCount == 63, "rxLink accepts the driver's header"); 383 expect_refusal(mq_rx_link_check(golden_tx, 32, GSP_MQ_SIZE, 8, 32, NULL), "-2", "rxLink msgSize < 16"); 384 expect_refusal(mq_rx_link_check(golden_tx, 32, 4096, 8192, 32, NULL), "-3", "rxLink msgSize > size"); 385 expect_refusal(mq_rx_link_check(golden_tx, 32, 4096, 4096, 32, NULL), "-6", "rxLink size < entryOff + msgSize"); 386 expect_refusal(mq_rx_link_check(golden_tx, 32, 0x80000, 4096, 32, NULL), "-7", "rxLink size mismatch"); 387 expect_refusal(mq_rx_link_check(golden_tx, 32, GSP_MQ_SIZE, 8192, 32, NULL), "-8", "rxLink msgSize mismatch"); 388 uint8_t mut[32]; memcpy(mut, golden_tx, 32); wr32(mut, 1); expect_refusal(mq_rx_link_check(mut, 32, GSP_MQ_SIZE, 4096, 32, NULL), "-9", "rxLink version != 0"); 389 memcpy(mut, golden_tx, 32); wr32(mut + 24, 16); expect_refusal(mq_rx_link_check(mut, 32, GSP_MQ_SIZE, 4096, 32, NULL), "-10", "rxLink rxHdrOff < 32"); 390 memcpy(mut, golden_tx, 32); wr32(mut + 12, 62); expect_refusal(mq_rx_link_check(mut, 32, GSP_MQ_SIZE, 4096, 32, NULL), "-10", "rxLink msgCount wrong"); 391 memcpy(mut, golden_tx, 32); wr32(mut + 16, 63); expect_refusal(mq_rx_link_check(mut, 32, GSP_MQ_SIZE, 4096, 32, NULL), "writePtr beyond", "writePtr >= msgCount refused"); 392 expect_refusal(mq_rx_link_check(golden_tx, 16, GSP_MQ_SIZE, 4096, 32, NULL), "short header", "short header refused"); 393 /* ring arithmetic */ 394 check(ring_free(0, 0, 63) == 62 && ring_avail(0, 0, 63) == 0, "empty ring: 62 free, 0 available"); 395 check(ring_free(0, 62, 63) == 0 && ring_avail(0, 62, 63) == 62, "full ring: 0 free, 62 available"); 396 check(ring_free(5, 3, 63) == 1 && ring_avail(5, 3, 63) == 61, "wrapped write behind read"); 397 check(ring_free(10, 20, 63) == 52 && ring_avail(10, 20, 63) == 10, "read behind write"); 398 check(ring_free(63, 0, 63) == 0 && ring_avail(0, 63, 63) == 0, "pointer at msgCount reads as corrupt: 0"); 399 check(ring_free(0, 0, 0) == 0, "msgCount 0 never divides"); 400 /* 2. one small RPC (NOP, sync sequence 7, queue seqNum 3) */ 401 static uint8_t el[ELEM_SIZE_MAX]; uint32_t ml = 0, ec = 0; 402 elem_in e = { FN_NOP, 7, 0, NULL, 3 }; 403 check(elem_encode(&e, el, sizeof el, &ml, &ec) == NULL && ml == 80 && ec == 1, "NOP element: 80 bytes, 1 slot"); 404 check(rd32(el + 48) == RPC_HEADER_VERSION && rd32(el + 52) == RPC_SIGNATURE_VALID && rd32(el + 56) == 32 && rd32(el + 60) == FN_NOP, "RPC header words"); 405 check(rd32(el + 64) == RPC_PENDING && rd32(el + 68) == RPC_PENDING && rd32(el + 72) == 7 && rd32(el + 76) == 0, "pending results, sequence, gfid 0"); 406 check(rd32(el + 36) == 3 && rd32(el + 40) == 1, "element seqNum and elemCount"); 407 check(checksum32(el, 80) == 0, "finished element checksums to zero"); 408 for (int i = 0; i < 32; i++) check(el[i] == 0, "auth/aad bytes zero without CC"); 409 elem_view v; 410 check(elem_check(el, 4096, 3, &v) == NULL && v.rpc.function == FN_NOP && v.msgLen == 80, "receive-side validation accepts it"); 411 expect_refusal(elem_check(el, 4096, 4, NULL), "old seqNum", "older seqNum flagged for consume-and-retry"); 412 expect_refusal(elem_check(el, 4096, 2, NULL), "seqNum ahead", "future seqNum refused"); 413 memcpy(mut, el, 32); uint8_t tmp[4096]; memcpy(tmp, el, 4096); 414 tmp[70] ^= 1; expect_refusal(elem_check(tmp, 4096, 3, NULL), "checksum not zero", "flipped header bit breaks the checksum"); 415 memcpy(tmp, el, 4096); wr32(tmp + 52, 0x43505257); expect_refusal(elem_check(tmp, 4096, 3, NULL), "bad RPC signature", "signature mutation refused"); 416 memcpy(tmp, el, 4096); wr32(tmp + 48, 0x02000000); expect_refusal(elem_check(tmp, 4096, 3, NULL), "bad RPC header version", "version 2 refused"); 417 memcpy(tmp, el, 4096); wr32(tmp + 40, 2); expect_refusal(elem_check(tmp, 4096, 3, NULL), "elemCount does not match", "elemCount 2 for an 80-byte message refused"); 418 memcpy(tmp, el, 4096); wr32(tmp + 56, 16); expect_refusal(elem_check(tmp, 4096, 3, NULL), "RPC length outside", "length below header refused"); 419 expect_refusal(elem_check(el, 64, 3, NULL), "short element", "short buffer refused"); 420 /* 3. a payload that needs several ring slots, odd length to exercise the pad */ 421 static uint8_t pay[9000]; for (size_t i = 0; i < sizeof pay; i++) pay[i] = (uint8_t)(i * 7 + 1); 422 e.function = FN_SET_REGISTRY; e.sequence = 0; e.payload_len = 9001 - 32; e.payload = pay; e.seqNum = 0; 423 check(elem_encode(&e, el, sizeof el, &ml, &ec) == NULL && ec == 3 && ml == align_up(48 + 9001, 8), "9001-byte RPC: 3 slots, padded to 8"); 424 check(checksum32(el, ml) == 0 && el[48 + 9001] == 0 && el[48 + 9001 + 6] == 0, "zero padding after the message"); 425 check(elem_check(el, 3 * 4096, 0, &v) == NULL && v.elemCount == 3 && v.rpc.length == 9001, "multi-slot element validates"); 426 check(elem_check(el, 9056, 0, NULL) == NULL, "exactly the padded length suffices"); 427 expect_refusal(elem_check(el, 9048, 0, NULL), "element shorter", "buffer one word short refused"); 428 /* bounds */ 429 static uint8_t big[70000]; 430 e.payload = big; e.payload_len = RPC_SIZE_MAX - RPC_HDR_SIZE; check(elem_encode(&e, el, sizeof el, &ml, &ec) == NULL && ec == 16 && ml == ELEM_SIZE_MAX, "largest RPC: 65488 bytes in 16 slots"); 431 e.payload_len = RPC_SIZE_MAX - RPC_HDR_SIZE + 1; expect_refusal(elem_encode(&e, el, sizeof el, &ml, &ec), "payload exceeds", "65489 refused"); 432 e.payload_len = 0; expect_refusal(elem_encode(&e, el, 64, &ml, &ec), "output too small", "small output refused"); 433 /* 4. continuation plan */ 434 cont_plan cp; 435 check(cont_plan_for(32, &cp) == NULL && cp.chunks == 1 && cp.first_len == 32, "header-only RPC: one message"); 436 check(cont_plan_for(RPC_SIZE_MAX, &cp) == NULL && cp.chunks == 1, "65488 fits in one message"); 437 check(cont_plan_for(RPC_SIZE_MAX + 1, &cp) == NULL && cp.chunks == 2 && cp.last_payload == 1, "65489: one continuation record of 1 byte"); 438 check(cont_plan_for(RPC_SIZE_MAX + RPC_CONT_PAYLOAD_MAX, &cp) == NULL && cp.chunks == 2 && cp.last_payload == RPC_CONT_PAYLOAD_MAX, "exactly one full continuation"); 439 check(cont_plan_for(RPC_SIZE_MAX + RPC_CONT_PAYLOAD_MAX + 1, &cp) == NULL && cp.chunks == 3 && cp.last_payload == 1, "third chunk starts at 65489+65456+1"); 440 expect_refusal(cont_plan_for(8, &cp), "total below header", "sub-header total refused"); 441 /* 5. registry table */ 442 static const uint8_t bin[3] = { 0xaa, 0xbb, 0xcc }; 443 reg_in regs[3] = { { "RmMsg", REG_TYPE_STRING, 0, (const uint8_t *)"abc", 3 }, { "EnableGpuFirmwareLogs", REG_TYPE_DWORD, 1, NULL, 0 }, { "Blob", REG_TYPE_BINARY, 0, bin, 3 } }; 444 static uint8_t rt[4096]; uint32_t rs = 0; 445 check(reg_encode(regs, 3, rt, sizeof rt, &rs) == NULL, "three-entry registry encodes"); 446 /* 8 + 48 + ("RmMsg"6 + 3) + ("EnableGpuFirmwareLogs"22) + ("Blob"5 + 3) = 95 */ 447 check(rs == 95 && rd32(rt) == 95 && rd32(rt + 4) == 3, "size 95, numEntries 3"); 448 check(rd32(rt + 8) == 56 && rt[12] == REG_TYPE_STRING && rd32(rt + 16) == 62 && rd32(rt + 20) == 3 && memcmp(rt + 56, "RmMsg", 6) == 0 && memcmp(rt + 62, "abc", 3) == 0, "string entry: name at 56, value at 62"); 449 check(rd32(rt + 24) == 65 && rt[28] == REG_TYPE_DWORD && rd32(rt + 32) == 1 && rd32(rt + 36) == 4, "dword entry: name at 65, data 1, length 4"); 450 check(rd32(rt + 40) == 87 && rt[44] == REG_TYPE_BINARY && rd32(rt + 48) == 92 && rd32(rt + 52) == 3 && memcmp(rt + 92, bin, 3) == 0, "binary entry: name at 87, value at 92"); 451 check(rt[13] == 0 && rt[14] == 0 && rt[15] == 0, "entry padding bytes zero"); 452 uint32_t cnt = 0; check(reg_check(rt, rs, &cnt) == NULL && cnt == 3, "registry check accepts its own output"); 453 uint8_t rm[4096]; memcpy(rm, rt, rs); wr32(rm, 96); expect_refusal(reg_check(rm, rs, NULL), "size field", "size field mismatch refused"); 454 memcpy(rm, rt, rs); wr32(rm + 4, 0); expect_refusal(reg_check(rm, rs, NULL), "no entries", "zero entries refused"); 455 memcpy(rm, rt, rs); wr32(rm + 4, 7); expect_refusal(reg_check(rm, rs, NULL), "entry table overruns", "numEntries beyond size refused"); 456 memcpy(rm, rt, rs); rm[13] = 1; expect_refusal(reg_check(rm, rs, NULL), "entry padding", "dirty padding refused"); 457 memcpy(rm, rt, rs); wr32(rm + 8, 57); expect_refusal(reg_check(rm, rs, NULL), "name not packed", "out-of-order name refused"); 458 memcpy(rm, rt, rs); rm[61] = 'x'; expect_refusal(reg_check(rm, rs, NULL), "string/binary data offset", "unterminated name is caught by the value offset"); 459 memcpy(rm, rt, rs); wr32(rm + 36, 8); expect_refusal(reg_check(rm, rs, NULL), "DWORD length", "dword with length 8 refused"); 460 memcpy(rm, rt, rs); wr32(rm + 52, 4); expect_refusal(reg_check(rm, rs, NULL), "value overruns", "binary length past the end refused"); 461 memcpy(rm, rt, rs); rm[44] = 9; expect_refusal(reg_check(rm, rs, NULL), "unknown entry type", "type 9 refused"); 462 memcpy(rm, rt, rs); rm[rs] = 0; expect_refusal(reg_check(rm, rs + 1, NULL), "size field", "trailing byte refused"); 463 expect_refusal(reg_encode(regs, 0, rt, sizeof rt, &rs), "empty registry", "empty registry refused"); 464 reg_in z = { "Zero", REG_TYPE_STRING, 0, (const uint8_t *)"", 0 }; expect_refusal(reg_encode(&z, 1, rt, sizeof rt, &rs), "zero-length", "zero-length string refused"); 465 reg_in d = { "D", REG_TYPE_DWORD, 5, bin, 3 }; expect_refusal(reg_encode(&d, 1, rt, sizeof rt, &rs), "DWORD entry carries", "dword with bytes refused"); 466 reg_in u = { "U", 7, 0, NULL, 0 }; expect_refusal(reg_encode(&u, 1, rt, sizeof rt, &rs), "unknown entry type", "unknown type refused"); 467 reg_in nn = { "", REG_TYPE_DWORD, 1, NULL, 0 }; expect_refusal(reg_encode(&nn, 1, rt, sizeof rt, &rs), "entry without a name", "empty name refused"); 468 reg_in bg = { "Big", REG_TYPE_BINARY, 0, big, 66000 }; expect_refusal(reg_encode(&bg, 1, rt, sizeof rt, &rs), "registry exceeds one RPC", "oversize registry refused"); 469 expect_refusal(reg_encode(regs, 3, rt, 16, &rs), "output too small", "small registry output refused"); 470 /* 6. a complete SET_REGISTRY message, as the driver would ring it */ 471 e.function = FN_SET_REGISTRY; e.sequence = 0; e.payload_len = rs; e.payload = rt; e.seqNum = 1; 472 check(elem_encode(&e, el, sizeof el, &ml, &ec) == NULL && ec == 1 && rd32(el + 56) == 32 + 95 && rd32(el + 60) == FN_SET_REGISTRY, "SET_REGISTRY element: length 127, function 73"); 473 check(elem_check(el, 4096, 1, &v) == NULL && reg_check(el + 80, v.rpc.length - 32, &cnt) == NULL && cnt == 3, "registry survives the element round trip"); 474 /* 7. constants a native kernel must match */ 475 check(FN_CONTINUATION_RECORD == 71 && FN_GSP_SET_SYSTEM_INFO == 72 && FN_SET_REGISTRY == 73, "function numbers 71/72/73"); 476 check(EV_GSP_INIT_DONE == 0x1001 && EV_GSP_RUN_CPU_SEQUENCER == 0x1002 && EV_RC_TRIGGERED == 0x1004 && EV_GSP_LOCKDOWN_NOTICE == 0x101c && EV_GSP_POST_NOCAT_RECORD == 0x1020, "event numbers"); 477 check(RPC_RESULT_UNKNOWN_FN == 0xFF100001 && RPC_RESULT_BAD_FORMAT == 0xFF100002 && NV_PGSP_QUEUE_HEAD_0 == 0x110c00, "result codes and doorbell register"); 478 check(RPC_SIZE_MAX == 65488 && RPC_CONT_PAYLOAD_MAX == 65456, "RPC_SIZE_MAX 65488, continuation payload 65456"); 479 /* 8. the system-info message: 936-byte payload, one slot */ 480 e.function = FN_GSP_SET_SYSTEM_INFO; e.sequence = 0; e.payload_len = GSP_SYSTEM_INFO_SIZE; e.payload = big; e.seqNum = 0; 481 check(elem_encode(&e, el, sizeof el, &ml, &ec) == NULL && ec == 1 && rd32(el + 56) == GSP_SYSTEM_INFO_RPC_LEN && ml == 1016, "GSP_SET_SYSTEM_INFO: length 968, 1 slot, padded 1016"); 482 check(elem_check(el, 4096, 0, &v) == NULL && v.rpc.function == FN_GSP_SET_SYSTEM_INFO, "system-info element validates with seqNum 0"); 483 printf("CK_GSP_RPC_ENCODE_SELFTEST: %s (%d checks, %d failures)\n", failures ? "FAIL" : "PASS", checks, failures); 484 return failures ? 1 : 0; 485 } 486 487 /* ---- CLI ---- */ 488 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; } 489 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; } 490 static int read_file(const char *path, uint8_t **b, size_t *n) { 491 FILE *f = fopen(path, "rb"); if (!f) return -1; 492 fseek(f, 0, SEEK_END); long len = ftell(f); fseek(f, 0, SEEK_SET); 493 if (len < 0 || len > (1 << 20)) { fclose(f); return -1; } 494 *b = malloc((size_t)len + 1); if (!*b) { fclose(f); return -1; } 495 *n = fread(*b, 1, (size_t)len, f); fclose(f); return *n == (size_t)len ? 0 : -1; 496 } 497 static int usage(void) { 498 fprintf(stderr, "usage: ck_gsp_rpc_encode selftest | layout\n" 499 " txheader [--size N --msg-size N --hdr-align N --entry-align N --flags N] [--out F] (defaults: the driver's command queue)\n" 500 " check-txheader F [--size N --msg-size N --our-rxhdr N] (msgqRxLink rules on a peer header)\n" 501 " ring --read N --write N [--count N] (free / available slots)\n" 502 " element --function N [--sequence N] [--seqnum N] [--payload F] [--out F] (one ring message, padded to whole slots)\n" 503 " check-element F [--seqnum N] (receive-side validation)\n" 504 " contplan --rpc-len N (continuation records needed)\n" 505 " registry --dword NAME=N | --string NAME=TEXT ... [--out F] (PACKED_REGISTRY_TABLE)\n" 506 " check-registry F\n"); 507 return 2; 508 } 509 static int refused(const char *why) { printf("REFUSED: %s\n", why); return 2; } 510 static int emit(const char *out, const uint8_t *b, size_t n) { 511 if (!out) return 0; 512 if (write_file(out, b, n)) return refused("cannot write output"); 513 printf("wrote %s (%zu bytes)\n", out, n); 514 return 0; 515 } 516 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" : ""); } 517 518 int main(int argc, char **argv) { 519 if (argc < 2) return usage(); 520 const char *mode = argv[1]; 521 if (!strcmp(mode, "selftest")) return selftest(); 522 if (!strcmp(mode, "layout")) { 523 printf("msgqTxHeader 32 bytes: version 0 size 4 msgSize 8 msgCount 12 writePtr 16 flags 20 rxHdrOff 24 entryOff 28; msgqRxHeader: readPtr 0\n"); 524 printf("driver queues: size 0x40000 msgSize 4096 hdrAlign 16 entryAlign 4096 flags SWAP_RX -> rxHdrOff 32 entryOff 4096 msgCount 63 (62 usable)\n"); 525 printf("GSP_MSG_QUEUE_ELEMENT: authTag 0 aad 16 checkSum 32 seqNum 36 elemCount 40 rpc 48 (sizeof 80); slot 4096, max 16 slots, RPC_SIZE_MAX 65488\n"); 526 printf("rpc_message_header_v03_00 32 bytes: header_version 0 (0x03000000) signature 4 (0x43505256) length 8 function 12 rpc_result 16 rpc_result_private 20 sequence 24 cpuRmGfid 28\n"); 527 printf("functions: NOP 0 CONTINUATION_RECORD 71 GSP_SET_SYSTEM_INFO 72 SET_REGISTRY 73; events: GSP_INIT_DONE 0x1001 RUN_CPU_SEQUENCER 0x1002 RC_TRIGGERED 0x1004 LOCKDOWN_NOTICE 0x101c POST_NOCAT_RECORD 0x1020\n"); 528 printf("results: RPC_PENDING 0xffffffff UNKNOWN_FUNCTION 0xff100001 INVALID_MESSAGE_FORMAT 0xff100002; doorbell NV_PGSP_QUEUE_HEAD(0) 0x110c00 <- 0\n"); 529 printf("GspSystemInfo payload 936 bytes (gpuPhysAddr 0 ... FHBBusInfo 138 chipsetIDInfo 148 acpiMethodData 160 hypervisorType 836 gspVFInfo 856 hostPageSize 920 bWindowChannelAlwaysMapped 931); GSP_SET_SYSTEM_INFO RPC length 968\n"); 530 printf("PACKED_REGISTRY_TABLE: size 0 numEntries 4 entries 8 x {nameOffset 0 type 4 data 8 length 12} (16 each), then names and values; types DWORD 1 BINARY 2 STRING 3\n"); 531 return 0; 532 } 533 const char *out = NULL, *payload_path = NULL; int ok = 1; 534 uint64_t size = GSP_MQ_SIZE, msg_size = GSP_MQ_MSG_SIZE, hdr_align = GSP_MQ_HDR_ALIGN, entry_align = GSP_MQ_ENTRY_ALIGN, flags = MSGQ_FLAGS_SWAP_RX; 535 uint64_t our_rxhdr = GSP_MQ_RXHDR_OFF, rptr = 0, wptr = 0, count = GSP_MQ_MSG_COUNT, function = 0, sequence = 0, seqnum = 0, rpc_len = 0; 536 int have_function = 0; 537 reg_in regs[64]; uint32_t nregs = 0; static char regbuf[64][512]; 538 for (int i = 2; i < argc; i++) { 539 const char *a = argv[i]; 540 if (!strcmp(a, "--out") && i + 1 < argc) { out = argv[++i]; continue; } 541 if (!strcmp(a, "--payload") && i + 1 < argc) { payload_path = argv[++i]; continue; } 542 if ((!strcmp(a, "--dword") || !strcmp(a, "--string")) && i + 1 < argc) { 543 if (nregs >= 64) return refused("more than 64 registry entries"); 544 const char *kv = argv[++i]; const char *eq = strchr(kv, '='); 545 if (!eq || eq == kv || strlen(kv) >= sizeof regbuf[0]) return refused("registry entry must be NAME=VALUE"); 546 strcpy(regbuf[nregs], kv); regbuf[nregs][eq - kv] = 0; 547 reg_in *r = ®s[nregs]; r->name = regbuf[nregs]; r->bytes = NULL; r->len = 0; r->dword = 0; 548 if (!strcmp(a, "--dword")) { r->type = REG_TYPE_DWORD; r->dword = (uint32_t)parse_u64(eq + 1, &ok); if (!ok) return refused("bad dword value"); } 549 else { r->type = REG_TYPE_STRING; r->bytes = (const uint8_t *)regbuf[nregs] + (eq - kv) + 1; r->len = (uint32_t)strlen(eq + 1); } 550 nregs++; continue; 551 } 552 struct { const char *n; uint64_t *v; } opts[] = { {"--size", &size}, {"--msg-size", &msg_size}, {"--hdr-align", &hdr_align}, {"--entry-align", &entry_align}, {"--flags", &flags}, 553 {"--our-rxhdr", &our_rxhdr}, {"--read", &rptr}, {"--write", &wptr}, {"--count", &count}, {"--function", &function}, {"--sequence", &sequence}, {"--seqnum", &seqnum}, {"--rpc-len", &rpc_len} }; 554 int hit = 0; 555 for (size_t k = 0; k < sizeof opts / sizeof opts[0] && i + 1 < argc; k++) 556 if (!strcmp(a, opts[k].n)) { *opts[k].v = parse_u64(argv[++i], &ok); if (opts[k].v == &function) have_function = 1; hit = 1; break; } 557 if (!hit && a[0] == '-') return usage(); 558 if (!ok) return refused("bad number"); 559 } 560 if (size > 0xffffffffu || msg_size > 0xffffffffu || hdr_align > 31 || entry_align > 31 || flags > 0xffffffffu) return refused("value out of 32-bit range"); 561 if (!strcmp(mode, "txheader")) { 562 mq_create_in q = { (uint32_t)size, (uint32_t)msg_size, (uint32_t)hdr_align, (uint32_t)entry_align, (uint32_t)flags }; 563 uint8_t h[MSGQ_TX_HDR_SIZE]; const char *e = mq_tx_create(&q, h); if (e) return refused(e); 564 printf("txheader ok msgCount=%u rxHdrOff=%u entryOff=%u\n", rd32(h + 12), rd32(h + 24), rd32(h + 28)); dump(h, 32); 565 return emit(out, h, 32); 566 } 567 if (!strcmp(mode, "ring")) { 568 if (rptr > 0xffffffffu || wptr > 0xffffffffu || count > 0xffffffffu) return refused("value out of 32-bit range"); 569 printf("ring count=%llu read=%llu write=%llu free=%u available=%u\n", (unsigned long long)count, (unsigned long long)rptr, (unsigned long long)wptr, 570 ring_free((uint32_t)rptr, (uint32_t)wptr, (uint32_t)count), ring_avail((uint32_t)rptr, (uint32_t)wptr, (uint32_t)count)); 571 return 0; 572 } 573 if (!strcmp(mode, "element")) { 574 if (!have_function) return refused("need --function"); 575 if (function > 0xffffffffu || sequence > 0xffffffffu || seqnum > 0xffffffffu) return refused("value out of 32-bit range"); 576 uint8_t *p = NULL; size_t pn = 0; 577 if (payload_path && read_file(payload_path, &p, &pn)) return refused("cannot read payload"); 578 static uint8_t el[ELEM_SIZE_MAX]; uint32_t ml = 0, ec = 0; 579 elem_in in = { (uint32_t)function, (uint32_t)sequence, (uint32_t)pn, p, (uint32_t)seqnum }; 580 const char *e = elem_encode(&in, el, sizeof el, &ml, &ec); free(p); if (e) return refused(e); 581 printf("element ok function=%u sequence=%u seqNum=%u rpc_length=%u msg_len=%u slots=%u checksum=0x%08x\n", in.function, in.sequence, in.seqNum, rd32(el + 56), ml, ec, rd32(el + 32)); 582 dump(el, 80); 583 return emit(out, el, (size_t)ec * ELEM_SIZE_MIN); 584 } 585 if (!strcmp(mode, "contplan")) { 586 if (rpc_len > 0xffffffffu) return refused("value out of 32-bit range"); 587 cont_plan cp; const char *e = cont_plan_for((uint32_t)rpc_len, &cp); if (e) return refused(e); 588 printf("contplan rpc_len=%u messages=%u first_len=%u last_continuation_payload=%u\n", (uint32_t)rpc_len, cp.chunks, cp.first_len, cp.last_payload); 589 return 0; 590 } 591 if (!strcmp(mode, "registry")) { 592 static uint8_t rt[RPC_SIZE_MAX]; uint32_t rs = 0; 593 const char *e = reg_encode(regs, nregs, rt, sizeof rt, &rs); if (e) return refused(e); 594 printf("registry ok size=%u entries=%u\n", rs, nregs); dump(rt, rs > 96 ? 96 : rs); 595 return emit(out, rt, rs); 596 } 597 if (!strncmp(mode, "check-", 6)) { 598 if (argc < 3 || argv[2][0] == '-') return usage(); 599 uint8_t *b; size_t n; if (read_file(argv[2], &b, &n)) return refused("cannot read file"); 600 const char *e; char extra[96] = ""; 601 if (!strcmp(mode, "check-txheader")) { msgq_tx_header h; e = mq_rx_link_check(b, n, (uint32_t)size, (uint32_t)msg_size, (uint32_t)our_rxhdr, &h); if (!e) snprintf(extra, sizeof extra, " msgCount=%u writePtr=%u", h.msgCount, h.writePtr); } 602 else if (!strcmp(mode, "check-element")) { elem_view v; e = elem_check(b, n, (uint32_t)seqnum, &v); if (!e) snprintf(extra, sizeof extra, " function=0x%x sequence=%u result=0x%08x length=%u slots=%u", v.rpc.function, v.rpc.sequence, v.rpc.rpc_result, v.rpc.length, v.elemCount); } 603 else if (!strcmp(mode, "check-registry")) { uint32_t c = 0; e = reg_check(b, n, &c); if (!e) snprintf(extra, sizeof extra, " entries=%u", c); } 604 else e = "unknown check"; 605 free(b); 606 if (e) return refused(e); 607 printf("%s: OK%s\n", mode, extra); return 0; 608 } 609 return usage(); 610 }