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 / tests / test_gb10_gsp_boot.c
| 1 | /* test_gb10_gsp_boot -- host test for dev/gb10_gsp_boot.c (L192, aienos#286 cut C host side). |
| 2 | * Synthetic firmware bytes only; no device, no real firmware. Every decode below reads fixed byte |
| 3 | * offsets written out by hand from the NVIDIA headers (inventory sections 10-14), not the builder's |
| 4 | * mirror structs, so a wrong mirror offset fails here as well as in test_gb10_gsp_nvhdr. */ |
| 5 | #include <stdint.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include "ck_test.h" |
| 9 | #include "gb10_gsp_boot.h" |
| 10 | |
| 11 | static uint32_t r32(const uint8_t *p) { return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); } |
| 12 | static uint64_t r64(const uint8_t *p) { return (uint64_t)r32(p) | ((uint64_t)r32(p + 4) << 32); } |
| 13 | static uint16_t r16(const uint8_t *p) { return (uint16_t)(p[0] | (p[1] << 8)); } |
| 14 | static void w32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) { p[i] = (uint8_t)v; v >>= 8; } } |
| 15 | |
| 16 | #define ARENA_PA 0x80000000ULL |
| 17 | #define ARENA_CAP (8u << 20) |
| 18 | #define ELF_SIZE (3u * 1024 * 1024 + 123) /* 769 data pages: radix levels 1, 1, 2, 769 */ |
| 19 | #define SIG_SIZE 4097u |
| 20 | #define FMC_SIZE 70000u |
| 21 | |
| 22 | static uint8_t *arena_mem, *elf, *bl, *sig, *fmc, hash[48], key[384], fsig[384], sysinfo[GSP_SYSTEM_INFO_SIZE]; |
| 23 | static const uint8_t bin_val[5] = {9, 8, 7, 6, 5}; |
| 24 | static const gsp_reg_entry regs[3] = { |
| 25 | {"RMSecBusResetEnable", GSP_REG_DWORD, 1, 0, 0}, |
| 26 | {"RmTestString", GSP_REG_STRING, 0, (const uint8_t *)"abc", 4}, |
| 27 | {"RmTestBin", GSP_REG_BINARY, 0, bin_val, 5}, |
| 28 | }; |
| 29 | |
| 30 | static void fill(uint8_t *p, size_t n, uint32_t seed) { for (size_t i = 0; i < n; i++) p[i] = (uint8_t)((i * 131u + seed) >> 3); } |
| 31 | |
| 32 | static gsp_boot_in good_input(void) |
| 33 | { |
| 34 | gsp_boot_in in; |
| 35 | memset(&in, 0, sizeof in); |
| 36 | in.rm_elf = elf; in.rm_elf_size = ELF_SIZE; |
| 37 | in.rm_sig = sig; in.rm_sig_size = SIG_SIZE; |
| 38 | in.bootloader = bl; in.bootloader_size = GSP_GB10B_BOOTLOADER_SIZE; |
| 39 | in.bl_code_off = 0xb000; in.bl_data_off = 0xa00; in.bl_manifest_off = 0; |
| 40 | in.fmc = fmc; in.fmc_size = FMC_SIZE; in.fmc_hash = hash; in.fmc_pubkey = key; in.fmc_sig = fsig; |
| 41 | in.sysmem_heap_pa = 0x100000000ULL; in.sysmem_heap_size = 512u << 20; |
| 42 | in.system_info = sysinfo; |
| 43 | in.regs = regs; in.nregs = 3; |
| 44 | in.gpu_instance = 0; |
| 45 | in.dma_bits = 41; |
| 46 | return in; |
| 47 | } |
| 48 | |
| 49 | static uint8_t *at(uint64_t pa) { return arena_mem + (pa - ARENA_PA); } |
| 50 | |
| 51 | static int build(gsp_arena *a, const gsp_boot_in *in, gsp_boot_plan *plan, const char **why) |
| 52 | { |
| 53 | memset(arena_mem, 0xA5, ARENA_CAP); /* the builder must zero what it takes */ |
| 54 | gsp_arena_init(a, arena_mem, ARENA_PA, ARENA_CAP); |
| 55 | return gsp_boot_build(a, in, plan, why); |
| 56 | } |
| 57 | |
| 58 | /* Check one element at slot ptr: checksum, header fields. Returns rpc length. */ |
| 59 | static uint32_t check_elem(const uint8_t *e, uint32_t seq_num, uint32_t fn, uint32_t rpc_seq, uint32_t elems) |
| 60 | { |
| 61 | uint32_t len = r32(e + 48 + 8); |
| 62 | uint32_t msg = 48 + len, padded = (msg + 7) & ~7u; |
| 63 | uint8_t tmp[65536]; |
| 64 | memcpy(tmp, e, padded); |
| 65 | for (uint32_t i = msg; i < padded; i++) tmp[i] = 0; |
| 66 | CHECK(gsp_checksum32(tmp, padded) == 0); |
| 67 | CHECK(r32(e + 36) == seq_num); |
| 68 | CHECK(r32(e + 40) == elems); |
| 69 | CHECK(r32(e + 48) == 0x03000000u); |
| 70 | CHECK(r32(e + 52) == 0x43505256u); |
| 71 | CHECK(r32(e + 60) == fn); |
| 72 | CHECK(r32(e + 64) == 0xFFFFFFFFu && r32(e + 68) == 0xFFFFFFFFu); |
| 73 | CHECK(r32(e + 72) == rpc_seq); |
| 74 | return len; |
| 75 | } |
| 76 | |
| 77 | static void test_layout(void) |
| 78 | { |
| 79 | gsp_arena a; gsp_boot_plan p; const char *why = 0; |
| 80 | gsp_boot_in in = good_input(); |
| 81 | CHECK(build(&a, &in, &p, &why) == 0); |
| 82 | if (why) printf(" build: %s\n", why); |
| 83 | |
| 84 | /* every region 4 KiB aligned, inside the arena, in order, not overlapping */ |
| 85 | gsp_region rs[] = { p.wpr_meta, p.libos_args, p.log[0], p.log[1], p.log[2], p.log[3], p.log[4], p.log[5], p.log[6], |
| 86 | p.args_cached, p.rpc_shared, p.radix3, p.bootloader, p.signature, p.fmc, p.fmc_params }; |
| 87 | uint64_t end = ARENA_PA; |
| 88 | for (size_t i = 0; i < sizeof rs / sizeof rs[0]; i++) { |
| 89 | CHECK((rs[i].pa & 4095) == 0); |
| 90 | CHECK(rs[i].pa >= end); |
| 91 | end = rs[i].pa + rs[i].size; |
| 92 | } |
| 93 | CHECK(end <= ARENA_PA + a.used && a.used <= ARENA_CAP); |
| 94 | |
| 95 | /* GspFwWprMeta (gsp_fw_wpr_meta.h, natural layout) */ |
| 96 | const uint8_t *w = at(p.wpr_meta.pa); |
| 97 | CHECK(r64(w + 0) == 0xdc3aae21371a60b3ULL && r64(w + 8) == 1); |
| 98 | CHECK(r64(w + 16) == p.radix3_root_pa && r64(w + 24) == ELF_SIZE); |
| 99 | CHECK(r64(w + 32) == p.bootloader.pa && r64(w + 40) == 196608); |
| 100 | CHECK(r64(w + 48) == 0xb000 && r64(w + 56) == 0xa00 && r64(w + 64) == 0); |
| 101 | CHECK(r64(w + 72) == p.signature.pa && r64(w + 80) == 4352 && (p.signature.pa & 255) == 0); |
| 102 | CHECK(r64(w + 128) == (133ULL << 20)); |
| 103 | int fmc_owned_zero = 1; |
| 104 | for (int off = 88; off < 256; off += 8) |
| 105 | if (off != 128 && r64(w + off) != 0) fmc_owned_zero = 0; |
| 106 | CHECK(fmc_owned_zero); /* FB layout, bootCount, flags, verified: FMC's */ |
| 107 | |
| 108 | /* libos init args: seven logs then RMARGS, CONTIGUOUS / SYSMEM */ |
| 109 | static const char *ids[8] = {"LOGINIT", "LOGINTR", "LOGRM", "LOGMNOC", "LOGDBG", "LOGROOT", "LOGKRNL", "RMARGS"}; |
| 110 | static const uint32_t sizes[8] = {0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x1000, 0x10000, 0x1000}; |
| 111 | const uint8_t *l = at(p.libos_args.pa); |
| 112 | for (int i = 0; i < 8; i++) { |
| 113 | const uint8_t *e = l + 32 * i; |
| 114 | uint64_t want = 0; |
| 115 | for (const char *c = ids[i]; *c; c++) want = (want << 8) | (uint8_t)*c; |
| 116 | CHECK(r64(e) == want); |
| 117 | CHECK(r64(e + 8) == (i < 7 ? p.log[i].pa : p.args_cached.pa)); |
| 118 | CHECK(r64(e + 16) == sizes[i]); |
| 119 | CHECK(e[24] == 1 && e[25] == 1); |
| 120 | } |
| 121 | CHECK(r64(l) == 0x004C4F47494E4954ULL); /* "LOGINIT" big-endian packed */ |
| 122 | CHECK(r64(l + 32 * 8) == 0 && r64(l + 32 * 8 + 8) == 0); |
| 123 | |
| 124 | /* log buffers: put pointer 0, then one PA per page */ |
| 125 | for (int i = 0; i < 7; i++) { |
| 126 | const uint8_t *b = at(p.log[i].pa); |
| 127 | CHECK(r64(b) == 0); |
| 128 | CHECK(r64(b + 8) == p.log[i].pa); |
| 129 | CHECK(r64(b + 8 * (p.log[i].size / 4096)) == p.log[i].pa + p.log[i].size - 4096); |
| 130 | } |
| 131 | |
| 132 | /* GSP_ARGUMENTS_CACHED */ |
| 133 | const uint8_t *g = at(p.args_cached.pa); |
| 134 | CHECK(r64(g + 0) == p.rpc_shared.pa && r32(g + 8) == 129 && r64(g + 16) == 4096 && r64(g + 24) == 4096 + 0x40000); |
| 135 | CHECK(r32(g + 32) == 0 && r32(g + 36) == 0 && g[40] == 0); |
| 136 | CHECK(r32(g + 44) == 0 && g[48] == 1); |
| 137 | CHECK(r64(g + 56) == 0 && r64(g + 64) == 0); |
| 138 | CHECK(r64(g + 72) == 0x100000000ULL && r64(g + 80) == (512ULL << 20)); |
| 139 | |
| 140 | /* RPC shared buffer page table and command queue header */ |
| 141 | const uint8_t *s = at(p.rpc_shared.pa); |
| 142 | CHECK(p.rpc_shared.size == 129 * 4096); |
| 143 | CHECK(r64(s) == p.rpc_shared.pa && r64(s + 8 * 128) == p.rpc_shared.pa + 128 * 4096 && r64(s + 8 * 129) == 0); |
| 144 | CHECK(p.cmdq_pa == p.rpc_shared.pa + 4096 && p.statq_pa == p.rpc_shared.pa + 4096 + 0x40000); |
| 145 | const uint8_t *cq = at(p.cmdq_pa); |
| 146 | CHECK(r32(cq) == 0 && r32(cq + 4) == 0x40000 && r32(cq + 8) == 4096 && r32(cq + 12) == 63); |
| 147 | CHECK(r32(cq + 16) == 2 && r32(cq + 20) == 1 && r32(cq + 24) == 32 && r32(cq + 28) == 4096); |
| 148 | CHECK(p.cmdq_write_ptr == 2 && p.tx_seq == 2); |
| 149 | const uint8_t *sq = at(p.statq_pa); |
| 150 | int sq_zero = 1; |
| 151 | for (int i = 0; i < 4096; i++) if (sq[i]) sq_zero = 0; |
| 152 | CHECK(sq_zero); /* GSP creates the status queue */ |
| 153 | |
| 154 | /* element 0: GSP_SET_SYSTEM_INFO, async */ |
| 155 | const uint8_t *e0 = cq + 4096; |
| 156 | CHECK(check_elem(e0, 0, 72, 0, 1) == 32 + 936); |
| 157 | CHECK(memcmp(e0 + 80, sysinfo, 936) == 0); |
| 158 | /* element 1: SET_REGISTRY, async, PACKED_REGISTRY_TABLE */ |
| 159 | const uint8_t *e1 = cq + 2 * 4096; |
| 160 | uint32_t rl = check_elem(e1, 1, 73, 0, 1); |
| 161 | const uint8_t *t = e1 + 80; |
| 162 | uint32_t tsize = 8 + 16 * 3 + 20 + 13 + 4 + 10 + 5; |
| 163 | CHECK(rl == 32 + tsize && r32(t) == tsize && r32(t + 4) == 3); |
| 164 | CHECK(r32(t + 8) == 56 && t[12] == 1 && r32(t + 16) == 1 && r32(t + 20) == 4); |
| 165 | CHECK(strcmp((const char *)t + 56, "RMSecBusResetEnable") == 0); |
| 166 | CHECK(r32(t + 24) == 76 && t[28] == 3 && r32(t + 32) == 89 && r32(t + 36) == 4); |
| 167 | CHECK(strcmp((const char *)t + 76, "RmTestString") == 0 && strcmp((const char *)t + 89, "abc") == 0); |
| 168 | CHECK(r32(t + 40) == 93 && t[44] == 2 && r32(t + 48) == 103 && r32(t + 52) == 5); |
| 169 | CHECK(memcmp(t + 103, bin_val, 5) == 0); |
| 170 | CHECK(t[13] == 0 && t[14] == 0 && t[15] == 0); |
| 171 | |
| 172 | /* radix-3: level pages 1, 1, 2, 769; each level points at the next; data is the ELF */ |
| 173 | CHECK(p.radix3_level_pages[0] == 1 && p.radix3_level_pages[1] == 1 && p.radix3_level_pages[2] == 2 && p.radix3_level_pages[3] == 769); |
| 174 | CHECK(p.radix3.size == (4 + 769) * 4096ULL && p.elf_data_pa == p.radix3.pa + 4 * 4096); |
| 175 | const uint8_t *r0 = at(p.radix3.pa), *r1 = r0 + 4096, *r2 = r0 + 2 * 4096; |
| 176 | CHECK(r64(r0) == p.radix3.pa + 4096 && r64(r0 + 8) == 0); |
| 177 | CHECK(r64(r1) == p.radix3.pa + 2 * 4096 && r64(r1 + 8) == p.radix3.pa + 3 * 4096 && r64(r1 + 16) == 0); |
| 178 | CHECK(r64(r2) == p.elf_data_pa && r64(r2 + 8 * 511) == p.elf_data_pa + 511 * 4096ULL); |
| 179 | CHECK(r64(r2 + 4096) == p.elf_data_pa + 512 * 4096ULL && r64(r2 + 4096 + 8 * 256) == p.elf_data_pa + 768 * 4096ULL); |
| 180 | CHECK(r64(r2 + 4096 + 8 * 257) == 0); |
| 181 | CHECK(memcmp(at(p.elf_data_pa), elf, ELF_SIZE) == 0 && at(p.elf_data_pa)[ELF_SIZE] == 0); |
| 182 | |
| 183 | /* firmware copies */ |
| 184 | CHECK(memcmp(at(p.bootloader.pa), bl, 196608) == 0); |
| 185 | CHECK(memcmp(at(p.signature.pa), sig, SIG_SIZE) == 0 && at(p.signature.pa)[SIG_SIZE] == 0); |
| 186 | CHECK(memcmp(at(p.fmc.pa), fmc, FMC_SIZE) == 0); |
| 187 | |
| 188 | /* GSP_FMC_BOOT_PARAMS (gspifpub.h, natural layout) */ |
| 189 | const uint8_t *f = at(p.fmc_params.pa); |
| 190 | CHECK(r32(f + 0) == 0 && r32(f + 8) == 1 && r32(f + 12) == 256 && r64(f + 16) == p.wpr_meta.pa); |
| 191 | CHECK(r64(f + 24) == 0 && r32(f + 32) == 0 && f[36] == 1); |
| 192 | CHECK(r32(f + 40) == 2 && r64(f + 48) == p.libos_args.pa); |
| 193 | CHECK(r32(f + 56) == 0 && r64(f + 64) == 0 && r32(f + 72) == 0 && r32(f + 80) == 0 && r32(f + 84) == 0); |
| 194 | |
| 195 | /* SEC2 packet: MCTP dword 1 (GB10B HAL constant), NVDM 0x7e / 0x10de / COT 0x14, NVDM_PAYLOAD_COT packed */ |
| 196 | const uint8_t *k = p.cot_packet; |
| 197 | CHECK(r32(k) == 0x00000001u && r32(k + 4) == 0x1410de7eu); |
| 198 | CHECK(p.cot_packet_len == 868 && p.emem_queue_tail == 864); |
| 199 | const uint8_t *c = k + 8; |
| 200 | CHECK(r16(c) == 1 && r16(c + 2) == 860); |
| 201 | CHECK(r64(c + 4) == p.fmc.pa && r64(c + 12) == 0 && r32(c + 20) == FMC_SIZE && r64(c + 24) == 0 && r32(c + 32) == 0); |
| 202 | CHECK(memcmp(c + 36, hash, 48) == 0 && memcmp(c + 84, key, 384) == 0 && memcmp(c + 468, fsig, 384) == 0); |
| 203 | CHECK(r64(c + 852) == p.fmc_params.pa); |
| 204 | int tail_zero = 1; |
| 205 | for (int i = 868; i < 1024; i++) if (k[i]) tail_zero = 0; |
| 206 | CHECK(tail_zero); |
| 207 | |
| 208 | /* deterministic: same input, same bytes */ |
| 209 | uint8_t *copy = malloc(a.used); |
| 210 | memcpy(copy, arena_mem, a.used); |
| 211 | gsp_boot_plan p2; gsp_arena a2; |
| 212 | CHECK(build(&a2, &in, &p2, &why) == 0 && a2.used == a.used && memcmp(copy, arena_mem, a.used) == 0 && |
| 213 | memcmp(&p, &p2, sizeof p) == 0); |
| 214 | free(copy); |
| 215 | |
| 216 | /* no registry: only SET_SYSTEM_INFO queued */ |
| 217 | gsp_boot_in nr = in; nr.nregs = 0; nr.regs = 0; |
| 218 | CHECK(build(&a, &nr, &p, &why) == 0 && p.cmdq_write_ptr == 1 && p.tx_seq == 1); |
| 219 | } |
| 220 | |
| 221 | static void expect_refusal(gsp_boot_in in, const char *want) |
| 222 | { |
| 223 | gsp_arena a; gsp_boot_plan p; const char *why = 0; |
| 224 | int rc = build(&a, &in, &p, &why); |
| 225 | CHECK(rc == -1 && why && strcmp(why, want) == 0); |
| 226 | if (rc != -1 || !why || strcmp(why, want)) printf(" wanted \"%s\", got \"%s\"\n", want, why ? why : "(accepted)"); |
| 227 | } |
| 228 | |
| 229 | static void test_refusals(void) |
| 230 | { |
| 231 | gsp_boot_in in; |
| 232 | in = good_input(); in.rm_elf = 0; expect_refusal(in, "GSP-RM ELF missing"); |
| 233 | in = good_input(); in.rm_elf_size = 0; expect_refusal(in, "GSP-RM ELF missing"); |
| 234 | in = good_input(); in.rm_sig_size = 0; expect_refusal(in, "GSP-RM signature missing"); |
| 235 | in = good_input(); in.bootloader_size = 196608 + 4096; expect_refusal(in, "bootloader is not the GB10B GspRmBoot image size"); |
| 236 | in = good_input(); in.bl_code_off = 0xa000; expect_refusal(in, "bootloader descriptor offsets are not the GB10B values"); |
| 237 | in = good_input(); in.bl_manifest_off = 0x100; expect_refusal(in, "bootloader descriptor offsets are not the GB10B values"); |
| 238 | in = good_input(); in.fmc_size = 0; expect_refusal(in, "GSP-FMC image missing"); |
| 239 | in = good_input(); in.fmc_hash = 0; expect_refusal(in, "GSP-FMC hash, key or signature missing"); |
| 240 | in = good_input(); in.sysmem_heap_pa = 0x100000800ULL; expect_refusal(in, "sysmem heap zero or not 4 KiB aligned"); |
| 241 | in = good_input(); in.sysmem_heap_size = 0; expect_refusal(in, "sysmem heap zero or not 4 KiB aligned"); |
| 242 | in = good_input(); in.system_info = 0; expect_refusal(in, "GspSystemInfo missing"); |
| 243 | in = good_input(); in.dma_bits = 0; expect_refusal(in, "dma_bits out of range"); |
| 244 | in = good_input(); in.dma_bits = 31; expect_refusal(in, "address beyond the DMA window"); /* arena at 2 GiB */ |
| 245 | in = good_input(); in.dma_bits = 32; expect_refusal(in, "address beyond the DMA window"); /* heap at 4 GiB */ |
| 246 | in = good_input(); in.rm_elf_size = (1ULL << 39) + 1; expect_refusal(in, "ELF too large for a 3-level radix table"); |
| 247 | in = good_input(); in.rm_elf_size = 8u << 20; expect_refusal(in, "arena too small"); |
| 248 | gsp_reg_entry bad[2] = { {"", GSP_REG_DWORD, 1, 0, 0}, {"x", GSP_REG_STRING, 0, 0, 0} }; |
| 249 | in = good_input(); in.regs = bad; in.nregs = 1; expect_refusal(in, "registry entry invalid or too large"); |
| 250 | in = good_input(); in.regs = bad + 1; in.nregs = 1; expect_refusal(in, "registry entry invalid or too large"); |
| 251 | gsp_reg_entry many[GSP_REG_MAX_ENTRIES + 1]; |
| 252 | for (unsigned i = 0; i <= GSP_REG_MAX_ENTRIES; i++) many[i] = (gsp_reg_entry){"k", GSP_REG_DWORD, i, 0, 0}; |
| 253 | in = good_input(); in.regs = many; in.nregs = GSP_REG_MAX_ENTRIES + 1; expect_refusal(in, "registry entry invalid or too large"); |
| 254 | in = good_input(); in.regs = many; in.nregs = GSP_REG_MAX_ENTRIES; |
| 255 | { gsp_arena a; gsp_boot_plan p; const char *why = 0; CHECK(build(&a, &in, &p, &why) == 0); } |
| 256 | /* builder on a used arena */ |
| 257 | { gsp_arena a; gsp_boot_plan p; const char *why = 0; in = good_input(); |
| 258 | CHECK(build(&a, &in, &p, &why) == 0); CHECK(gsp_boot_build(&a, &in, &p, &why) == -1 && strcmp(why, "arena not empty") == 0); } |
| 259 | /* arena init */ |
| 260 | gsp_arena a; |
| 261 | CHECK(gsp_arena_init(&a, arena_mem, ARENA_PA + 8, ARENA_CAP) == -1); |
| 262 | CHECK(gsp_arena_init(&a, arena_mem + 8, ARENA_PA, ARENA_CAP) == -1); |
| 263 | CHECK(gsp_arena_init(&a, arena_mem, ~0ULL - 4095, ARENA_CAP) == -1); |
| 264 | CHECK(gsp_arena_init(&a, 0, ARENA_PA, ARENA_CAP) == -1); |
| 265 | } |
| 266 | |
| 267 | /* Simulated GSP side: create the status queue the way msgqTxCreate would and post one message. */ |
| 268 | static void gsp_post(uint8_t *sq, uint32_t slot, uint32_t seq_num, uint32_t fn, uint32_t result, uint32_t payload_len) |
| 269 | { |
| 270 | w32(sq + 0, 0); w32(sq + 4, 0x40000); w32(sq + 8, 4096); w32(sq + 12, 63); |
| 271 | w32(sq + 20, 1); w32(sq + 24, 32); w32(sq + 28, 4096); |
| 272 | uint8_t *e = sq + 4096 + slot * 4096; |
| 273 | memset(e, 0, 4096); |
| 274 | w32(e + 36, seq_num); w32(e + 40, 1); |
| 275 | w32(e + 48, 0x03000000u); w32(e + 52, 0x43505256u); w32(e + 56, 32 + payload_len); w32(e + 60, fn); |
| 276 | w32(e + 64, result); w32(e + 68, result); |
| 277 | for (uint32_t i = 0; i < payload_len; i++) e[80 + i] = (uint8_t)(i + 1); |
| 278 | uint32_t padded = (48 + 32 + payload_len + 7) & ~7u; |
| 279 | w32(e + 32, gsp_checksum32(e, padded)); |
| 280 | w32(sq + 16, (slot + 1) % 63); |
| 281 | } |
| 282 | |
| 283 | static void test_rpcs(void) |
| 284 | { |
| 285 | gsp_arena a; gsp_boot_plan p; const char *why = 0; |
| 286 | gsp_boot_in in = good_input(); |
| 287 | CHECK(build(&a, &in, &p, &why) == 0); |
| 288 | gsp_mq q; |
| 289 | CHECK(gsp_mq_attach(&q, &a, &p) == 0 && q.tx_seq == 2); |
| 290 | uint8_t *cq = at(p.cmdq_pa), *sq = at(p.statq_pa); |
| 291 | uint32_t seq = 99; |
| 292 | |
| 293 | /* GSP_RM_ALLOC: length covers the fixed part only, params follow in the same element */ |
| 294 | uint8_t params[300]; |
| 295 | fill(params, sizeof params, 7); |
| 296 | CHECK(gsp_rpc_rm_alloc(&q, 0xc1d00001, 0xc1d00001, 0xcaf00001, 0x0080, params, sizeof params, &seq, &why) == 0 && seq == 0); |
| 297 | const uint8_t *e = cq + 4096 + 2 * 4096; |
| 298 | CHECK(check_elem(e, 2, 103, 0, 1) == 64); |
| 299 | CHECK(r32(e + 80) == 0xc1d00001 && r32(e + 84) == 0xc1d00001 && r32(e + 88) == 0xcaf00001 && r32(e + 92) == 0x80); |
| 300 | CHECK(r32(e + 96) == 0 && r32(e + 100) == 300 && r32(e + 104) == 0 && r32(e + 108) == 0); |
| 301 | CHECK(memcmp(e + 112, params, 300) == 0 && e[412] == 0); |
| 302 | |
| 303 | /* GSP_RM_CONTROL spanning two elements */ |
| 304 | uint8_t *big = malloc(5000); |
| 305 | fill(big, 5000, 3); |
| 306 | CHECK(gsp_rpc_rm_control(&q, 0xc1d00001, 0xcaf00002, 0x20800110, big, 5000, &seq, &why) == 0 && seq == 1); |
| 307 | e = cq + 4096 + 3 * 4096; |
| 308 | CHECK(r32(e + 40) == 2 && r32(e + 56) == 32 + 40 + 5000); |
| 309 | uint8_t *flat = malloc(8192); |
| 310 | memcpy(flat, e, 8192); /* slots 3 and 4 are adjacent */ |
| 311 | CHECK(check_elem(flat, 3, 76, 1, 2) == 32 + 40 + 5000); |
| 312 | CHECK(r32(flat + 80) == 0xc1d00001 && r32(flat + 84) == 0xcaf00002 && r32(flat + 88) == 0x20800110 && r32(flat + 96) == 5000); |
| 313 | CHECK(r64(flat + 112) == 0 && memcmp(flat + 120, big, 5000) == 0); |
| 314 | |
| 315 | /* free and static-info (3 elements, zero payload) */ |
| 316 | CHECK(gsp_rpc_rm_free(&q, 0xc1d00001, 0xcaf00001, &seq, &why) == 0 && seq == 2); |
| 317 | e = cq + 4096 + 5 * 4096; |
| 318 | CHECK(check_elem(e, 4, 10, 2, 1) == 48); |
| 319 | CHECK(r32(e + 80) == 0xc1d00001 && r32(e + 84) == 0 && r32(e + 88) == 0xcaf00001 && r32(e + 92) == 0); |
| 320 | CHECK(gsp_rpc_get_static_info(&q, 9000, &seq, &why) == 0 && seq == 3); |
| 321 | e = cq + 4096 + 6 * 4096; |
| 322 | CHECK(r32(e + 40) == 3 && r32(e + 56) == 9032 && r32(e + 60) == 65); |
| 323 | CHECK(r32(cq + 16) == 9 && q.tx_seq == 6); |
| 324 | CHECK(gsp_rpc_get_static_info(&q, 0, &seq, &why) == -1); |
| 325 | CHECK(gsp_rpc_get_static_info(&q, 65457, &seq, &why) == -1); |
| 326 | CHECK(gsp_rpc_rm_control(&q, 1, 2, 3, big, 65488 - 32 - 40 + 1, &seq, &why) == -1); |
| 327 | |
| 328 | /* ring full: 62 usable slots, GSP has read none */ |
| 329 | int sent = 0; |
| 330 | while (gsp_rpc_rm_free(&q, 1, 2, &seq, &why) == 0) sent++; |
| 331 | CHECK(sent == 62 - 9 && strcmp(why, "command queue full") == 0); |
| 332 | /* GSP consumes everything (its readPtr is in the status queue at rxHdrOff), then a 2-element |
| 333 | * control wraps the ring end: slot 62 then slot 0 */ |
| 334 | w32(sq + 32, r32(cq + 16)); |
| 335 | CHECK(r32(cq + 16) == 62); |
| 336 | CHECK(gsp_rpc_rm_control(&q, 5, 6, 7, big, 5000, &seq, &why) == 0); |
| 337 | CHECK(r32(cq + 16) == 1); |
| 338 | memcpy(flat, cq + 4096 + 62 * 4096, 4096); |
| 339 | memcpy(flat + 4096, cq + 4096, 4096); |
| 340 | CHECK(check_elem(flat, q.tx_seq - 1, 76, seq, 2) == 32 + 40 + 5000 && memcmp(flat + 120, big, 5000) == 0); |
| 341 | /* corrupt ring pointer refused */ |
| 342 | w32(sq + 32, 63); |
| 343 | CHECK(gsp_rpc_rm_free(&q, 1, 2, &seq, &why) == -1 && strcmp(why, "ring pointer corrupt") == 0); |
| 344 | w32(sq + 32, 1); |
| 345 | |
| 346 | /* payload outside the covered elements refused */ |
| 347 | gsp_seg sg = { big, 4096 }; |
| 348 | CHECK(gsp_mq_send(&q, 1, &sg, 1, 16, 1, &seq, &why) == -1 && strcmp(why, "payload beyond the elements the length covers") == 0); |
| 349 | CHECK(gsp_mq_send(&q, 1, &sg, 1, 4096, 1, &seq, &why) == 0); |
| 350 | gsp_seg zs = { 0, 65457 }; |
| 351 | CHECK(gsp_mq_send(&q, 1, &zs, 1, 65457, 1, &seq, &why) == -1 && strcmp(why, "message length out of range") == 0); |
| 352 | CHECK(gsp_mq_send(&q, 1, &sg, 1, 5000, 1, &seq, &why) == -1 && strcmp(why, "payload shorter than the RPC length") == 0); |
| 353 | free(big); |
| 354 | |
| 355 | /* status queue: empty, then GSP_INIT_DONE, then checks */ |
| 356 | uint8_t out[65536]; |
| 357 | uint32_t fn = 0, res = 0; |
| 358 | CHECK(gsp_mq_recv(&q, out, sizeof out, &fn, &res, &why) == -1); /* not created yet */ |
| 359 | gsp_post(sq, 0, 0, GSP_EV_INIT_DONE, 0, 0); |
| 360 | w32(sq + 16, 0); |
| 361 | CHECK(gsp_mq_recv(&q, out, sizeof out, &fn, &res, &why) == 0); /* created, empty */ |
| 362 | w32(sq + 16, 1); |
| 363 | CHECK(gsp_mq_recv(&q, out, sizeof out, &fn, &res, &why) == 1 && fn == 0x1001 && res == 0); |
| 364 | CHECK(r32(cq + 32) == 1 && q.rx_seq == 1); /* our readPtr, SWAP_RX */ |
| 365 | CHECK(gsp_mq_recv(&q, out, sizeof out, &fn, &res, &why) == 0); |
| 366 | gsp_post(sq, 1, 1, 103, 0, 20); |
| 367 | sq[4096 + 4096 + 90] ^= 1; |
| 368 | CHECK(gsp_mq_recv(&q, out, sizeof out, &fn, &res, &why) == -1 && strcmp(why, "checksum not zero") == 0); |
| 369 | gsp_post(sq, 1, 5, 103, 0, 20); |
| 370 | CHECK(gsp_mq_recv(&q, out, sizeof out, &fn, &res, &why) == -1 && strcmp(why, "bad sequence number") == 0); |
| 371 | gsp_post(sq, 1, 1, 103, 0, 20); |
| 372 | CHECK(gsp_mq_recv(&q, out, 64, &fn, &res, &why) == -1 && strcmp(why, "output too small") == 0); |
| 373 | CHECK(gsp_mq_recv(&q, out, sizeof out, &fn, &res, &why) == 1 && fn == 103 && out[80] == 1 && out[99] == 20); |
| 374 | gsp_post(sq, 2, 2, 103, 0, 20); |
| 375 | w32(sq + 4096 + 2 * 4096 + 40, 5); |
| 376 | CHECK(gsp_mq_recv(&q, out, sizeof out, &fn, &res, &why) == -1 && strcmp(why, "element count bad") == 0); |
| 377 | free(flat); |
| 378 | } |
| 379 | |
| 380 | int main(void) |
| 381 | { |
| 382 | arena_mem = aligned_alloc(4096, ARENA_CAP); |
| 383 | elf = malloc(ELF_SIZE); bl = malloc(196608); sig = malloc(SIG_SIZE); fmc = malloc(FMC_SIZE); |
| 384 | fill(elf, ELF_SIZE, 1); fill(bl, 196608, 2); fill(sig, SIG_SIZE, 3); fill(fmc, FMC_SIZE, 4); |
| 385 | fill(hash, sizeof hash, 5); fill(key, sizeof key, 6); fill(fsig, sizeof fsig, 7); fill(sysinfo, sizeof sysinfo, 8); |
| 386 | test_layout(); |
| 387 | test_refusals(); |
| 388 | test_rpcs(); |
| 389 | free(arena_mem); free(elf); free(bl); free(sig); free(fmc); |
| 390 | return ck_t_verdict("test_gb10_gsp_boot"); |
| 391 | } |