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/x86/bench_x86.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 / x86 / bench_x86.c` 410 lines ยท 14853 bytes Raw Blame History 1 /* bench_x86.c -- AIENOS GPU bench console (LAB BUILD ONLY: CK_X86_REPORT=1 with 2 * CK_X86_BENCH=1; the default image gets the stubs at the bottom). 3 * 4 * Authenticated GPU register RPC. The Spark sends bench commands (tools/ 5 * ck_gpu_bench.c) and the kernel executes them against the GPU's BAR5 (256 KiB 6 * register aperture) and BAR0 (VRAM aperture), found by pci_x86.c. HMAC-SHA256 7 * with a 32-byte key bound into the signed image at build time; a monotone 64-bit 8 * counter gives replay protection. Every response is HMAC'd, auth errors included. 9 * 10 * Safety invariants enforced here: 11 * - BAR5 writes restricted to the display and SMC-mailbox allowlist (bench_proto.h). 12 * - Any read returning 0xFFFFFFFF is "device gone" (status BENCH_ERR_ALLONES). 13 * The kernel answers that request and carries on with the next packet; the 14 * Spark tool is what stops the batch (ck_gpu_bench stops at ALLONES). 15 * - Every write logs the value before the write. 16 * - All waits are TSC-bounded. 17 * 18 * TRANSPORT: 19 * - Hardware: UDP port 47035 through tg3 (ck_x86_tg3_recv_bench, ck_x86_udp_send, 20 * tg3_x86.c). Compiled in the lab build; NOT tested here (QEMU has no BCM57766). 21 * - CK_X86_BENCH_TEST (QEMU test build only): the same 64-byte packets travel as 22 * text lines over COM1, which QEMU exposes as a socket. Request line: 23 * "ABCHREQ " + 128 hex digits 24 * reply line: 25 * "ABCHRESP " + 128 hex digits 26 * handle_bench_packet is the same code on both paths. 27 * 28 * DEVICE in the test build: an EMULATED BAR, not PCI emulation. The BARs are RAM 29 * stand-ins (test_bar5, test_bar0, test_cfg) with a known word at register 0x10 30 * and a word at 0x0FFF preset to 0xFFFFFFFF (a device that has gone). Reads and 31 * writes go through the same volatile accesses and allowlist as the real BARs. 32 * 33 * This file qualifies nothing physical. */ 34 #include "../core/ck_internal.h" 35 #include "../../native/argus/sha256.h" 36 #include "x86.h" 37 #include "bench_proto.h" 38 #include "report_x86.h" 39 40 #ifndef CK_X86_BENCH 41 #define CK_X86_BENCH 0 42 #endif 43 44 #if CK_X86_BENCH 45 46 /* HMAC-SHA256 key bound into the signed image: the build's own key from a 47 * header made in the build dir (make CK_X86_BENCH_KEY_FILE=..., never on a 48 * command line), else the test key. */ 49 #ifdef CK_X86_BENCH_KEY_H 50 #include CK_X86_BENCH_KEY_H 51 #define BENCH_KEY_KIND "this image's own key" 52 #endif 53 #ifndef CK_X86_BENCH_KEY 54 #define CK_X86_BENCH_KEY BENCH_TEST_KEY_INIT 55 #define BENCH_KEY_KIND "the TEST key" 56 #endif 57 #ifndef BENCH_KEY_KIND 58 #define BENCH_KEY_KIND "a key set at build time" 59 #endif 60 static const uint8_t bench_key[32] = { CK_X86_BENCH_KEY }; 61 62 /* GPU device state populated by ck_x86_bench_init. */ 63 static volatile uint32_t *bar5; /* 256 KiB register aperture */ 64 static volatile uint32_t *bar0; /* VRAM aperture (first 256 KiB) */ 65 static volatile uint8_t *pci_cfg; /* PCI config space (ECAM) */ 66 static int bench_dev_ready; 67 68 /* Monotone counter: reject any request with counter <= last_counter. */ 69 static uint64_t last_counter; 70 71 #ifdef CK_X86_BENCH_TEST 72 /* RAM stand-in for the BARs (QEMU test build only; see the header). */ 73 #define TEST_BAR5_WORDS 0x10000u 74 #define TEST_BAR0_WORDS 0x10000u 75 #define TEST_ALLONES_REG 0x0FFFu /* read as 0xFFFFFFFF: the device is gone */ 76 #define TEST_KNOWN_REG 0x0010u /* read as 0xC0FFEE00 */ 77 static volatile uint32_t test_bar5[TEST_BAR5_WORDS]; 78 static volatile uint32_t test_bar0[TEST_BAR0_WORDS]; 79 static volatile uint8_t test_cfg[256]; 80 static int test_inited; 81 static uint64_t test_tsc_hz; 82 #define COM1_BASE 0x3f8u 83 #endif 84 85 /* ---- Inline HMAC-SHA256 (uses argus/sha256.h, already in the build) ---- */ 86 static void bench_hmac(const uint8_t key[32], const uint8_t *msg, size_t len, uint8_t out[32]) 87 { 88 uint8_t ipad[64], opad[64], inner[32]; 89 for (unsigned i = 0; i < 32; i++) { 90 ipad[i] = key[i] ^ 0x36u; 91 opad[i] = key[i] ^ 0x5cu; 92 } 93 for (unsigned i = 32; i < 64; i++) { 94 ipad[i] = 0x36u; 95 opad[i] = 0x5cu; 96 } 97 sha256_ctx c; 98 sha256_init(&c); 99 sha256_update(&c, ipad, 64); 100 sha256_update(&c, msg, len); 101 sha256_final(&c, inner); 102 sha256_init(&c); 103 sha256_update(&c, opad, 64); 104 sha256_update(&c, inner, 32); 105 sha256_final(&c, out); 106 } 107 108 /* Constant-time 32-byte compare (avoid timing oracle on HMAC check). */ 109 static int ct_eq32(const uint8_t *a, const uint8_t *b) 110 { 111 uint8_t diff = 0; 112 for (unsigned i = 0; i < 32; i++) diff |= a[i] ^ b[i]; 113 return diff == 0; 114 } 115 116 /* ---- Register allowlist ---- */ 117 static int bar5_write_allowed(uint32_t reg_idx) 118 { 119 return (reg_idx >= BENCH_BAR5_WRITE_LO0 && reg_idx <= BENCH_BAR5_WRITE_HI0) || 120 (reg_idx >= BENCH_BAR5_WRITE_LO1 && reg_idx <= BENCH_BAR5_WRITE_HI1) || 121 (reg_idx >= BENCH_BAR5_WRITE_LO2 && reg_idx <= BENCH_BAR5_WRITE_HI2) || 122 (reg_idx >= BENCH_BAR5_WRITE_LO3 && reg_idx <= BENCH_BAR5_WRITE_HI3) || 123 (reg_idx >= BENCH_BAR5_WRITE_LO4 && reg_idx <= BENCH_BAR5_WRITE_HI4); 124 } 125 126 /* ---- Command execution ---- */ 127 static uint8_t exec_cmd(const uint8_t *req, uint32_t *result_out, uint32_t *before_out, 128 uint64_t tsc_hz) 129 { 130 uint8_t cmd = req[5]; 131 uint32_t arg0 = bench_r32(req + 16); 132 uint32_t arg1 = bench_r32(req + 20); 133 uint32_t arg2 = bench_r32(req + 24); 134 uint32_t arg3 = bench_r32(req + 28); 135 *result_out = 0; 136 *before_out = 0; 137 138 if (!bench_dev_ready && cmd != BENCH_CMD_PING) 139 return BENCH_ERR_NODEV; 140 141 switch (cmd) { 142 case BENCH_CMD_PING: 143 return BENCH_OK; 144 145 case BENCH_CMD_PCI_CFG: { 146 /* dword-aligned offsets inside the 256-byte config space (no wraparound) */ 147 if (arg0 > 252u || (arg0 & 3u)) return BENCH_ERR_RANGE; 148 uint32_t v = *(volatile uint32_t *)(pci_cfg + arg0); 149 if (v == BENCH_ALLONES) return BENCH_ERR_ALLONES; 150 *result_out = v; 151 return BENCH_OK; 152 } 153 case BENCH_CMD_BAR5_READ: { 154 if (arg0 > BENCH_BAR5_READ_MAX) return BENCH_ERR_RANGE; 155 uint32_t v = bar5[arg0]; 156 if (v == BENCH_ALLONES) return BENCH_ERR_ALLONES; 157 *result_out = v; 158 return BENCH_OK; 159 } 160 case BENCH_CMD_BAR5_WRITE: { 161 if (!bar5_write_allowed(arg0)) return BENCH_ERR_RANGE; 162 uint32_t before = bar5[arg0]; 163 if (before == BENCH_ALLONES) return BENCH_ERR_ALLONES; 164 bar5[arg0] = arg1; 165 uint32_t after = bar5[arg0]; 166 ck_printf("bench: bar5_write reg=0x%x 0x%08x -> 0x%08x\n", arg0, before, after); 167 *before_out = before; 168 *result_out = after; 169 return BENCH_OK; 170 } 171 case BENCH_CMD_BAR0_READ: { 172 if (arg0 >= BENCH_BAR0_MAX_WORDS) return BENCH_ERR_RANGE; 173 uint32_t v = bar0[arg0]; 174 if (v == BENCH_ALLONES) return BENCH_ERR_ALLONES; 175 *result_out = v; 176 return BENCH_OK; 177 } 178 case BENCH_CMD_BAR0_WRITE: { 179 if (arg0 >= BENCH_BAR0_MAX_WORDS) return BENCH_ERR_RANGE; 180 uint32_t before = bar0[arg0]; 181 if (before == BENCH_ALLONES) return BENCH_ERR_ALLONES; 182 bar0[arg0] = arg1; 183 ck_printf("bench: bar0_write off=0x%x 0x%08x -> 0x%08x\n", arg0, before, bar0[arg0]); 184 *before_out = before; 185 *result_out = bar0[arg0]; 186 return BENCH_OK; 187 } 188 case BENCH_CMD_BAR5_POLL: { 189 if (arg0 > BENCH_BAR5_READ_MAX) return BENCH_ERR_RANGE; 190 uint64_t t0 = ck_rdtsc(), limit = t0 + (uint64_t)tsc_hz / 1000 * arg3; 191 uint32_t v; 192 do { 193 v = bar5[arg0]; 194 if (v == BENCH_ALLONES) return BENCH_ERR_ALLONES; 195 if ((v & arg1) == (arg2 & arg1)) { *result_out = v; return BENCH_OK; } 196 ck_pause(); 197 } while (ck_rdtsc() < limit); 198 *result_out = v; 199 return BENCH_ERR_TIMEOUT; 200 } 201 case BENCH_CMD_BAR5_RMW: { 202 if (!bar5_write_allowed(arg0)) return BENCH_ERR_RANGE; 203 uint32_t before = bar5[arg0]; 204 if (before == BENCH_ALLONES) return BENCH_ERR_ALLONES; 205 uint32_t newval = (before & ~arg1) | (arg2 & arg1); 206 bar5[arg0] = newval; 207 uint32_t after = bar5[arg0]; 208 ck_printf("bench: bar5_rmw reg=0x%x 0x%08x -> 0x%08x\n", arg0, before, after); 209 *before_out = before; 210 *result_out = after; 211 (void)arg3; 212 return BENCH_OK; 213 } 214 default: 215 return BENCH_ERR_CMD; 216 } 217 } 218 219 /* ---- Protocol: build a response, HMAC'd, and send it ---- */ 220 /* Builds one response into out[BENCH_RESP_BYTES]. Returns byte count. */ 221 static size_t build_resp(uint8_t status, uint64_t counter, uint64_t tsc, 222 uint32_t result, uint32_t before, uint8_t out[BENCH_RESP_BYTES]) 223 { 224 memset(out, 0, BENCH_RESP_BYTES); 225 memcpy(out, BENCH_MAGIC_RESP, 4); 226 out[4] = BENCH_VERSION; 227 out[5] = status; 228 bench_w64(out + 8, counter); 229 bench_w64(out + 16, tsc); 230 bench_w32(out + 24, result); 231 bench_w32(out + 28, before); 232 bench_hmac(bench_key, out, BENCH_HMAC_OFF, out + BENCH_HMAC_OFF); 233 return BENCH_RESP_BYTES; 234 } 235 236 #ifdef CK_X86_BENCH_TEST 237 static void test_reply(const uint8_t body[BENCH_RESP_BYTES]) 238 { 239 static const char hex[] = "`0123456789abcdef`"; 240 char line[9 + 2 * BENCH_RESP_BYTES + 2]; 241 memcpy(line, "ABCHRESP ", 9); 242 for (unsigned i = 0; i < BENCH_RESP_BYTES; i++) { 243 line[9 + 2 * i] = hex[body[i] >> 4]; 244 line[9 + 2 * i + 1] = hex[body[i] & 15]; 245 } 246 line[9 + 2 * BENCH_RESP_BYTES] = '\n'; 247 line[9 + 2 * BENCH_RESP_BYTES + 1] = 0; 248 ck_puts(line); 249 } 250 #endif 251 252 static void reply(const uint8_t src_mac[6], const uint8_t body[BENCH_RESP_BYTES]) 253 { 254 #ifdef CK_X86_BENCH_TEST 255 (void)src_mac; 256 test_reply(body); 257 #else 258 ck_x86_udp_send(src_mac, body, BENCH_RESP_BYTES, CK_X86_BENCH_PORT, CK_X86_BENCH_PORT); 259 #endif 260 } 261 262 static void handle_bench_packet(const uint8_t *pkt, size_t pkt_len, 263 const uint8_t src_mac[6], uint64_t tsc_hz) 264 { 265 if (pkt_len < BENCH_REQ_BYTES) return; 266 if (memcmp(pkt, BENCH_MAGIC_REQ, 4) != 0) return; 267 if (pkt[4] != BENCH_VERSION) return; 268 269 /* Verify HMAC (covers bytes 0..31, hmac is at 32..63). An auth failure is 270 * answered with BENCH_ERR_AUTH and the counter field echoed as 0. */ 271 uint8_t expected[32]; 272 bench_hmac(bench_key, pkt, BENCH_HMAC_OFF, expected); 273 uint8_t body[BENCH_RESP_BYTES]; 274 if (!ct_eq32(expected, pkt + BENCH_HMAC_OFF)) { 275 build_resp(BENCH_ERR_AUTH, 0, ck_rdtsc(), 0, 0, body); 276 reply(src_mac, body); 277 return; 278 } 279 280 uint64_t counter = bench_r64(pkt + 8); 281 if (counter <= last_counter) { /* replay or stale counter */ 282 build_resp(BENCH_ERR_AUTH, counter, ck_rdtsc(), 0, 0, body); 283 reply(src_mac, body); 284 return; 285 } 286 last_counter = counter; 287 288 uint32_t result = 0, before = 0; 289 uint8_t status = exec_cmd(pkt, &result, &before, tsc_hz); 290 build_resp(status, counter, ck_rdtsc(), result, before, body); 291 reply(src_mac, body); 292 } 293 294 #ifdef CK_X86_BENCH_TEST 295 /* ---- Test transport: request lines on COM1 input ---- */ 296 static int hexnib(char c) 297 { 298 if (c >= '0' && c <= '9') return c - '0'; 299 if (c >= 'a' && c <= 'f') return c - 'a' + 10; 300 if (c >= 'A' && c <= 'F') return c - 'A' + 10; 301 return -1; 302 } 303 304 static char rxline[8 + 2 * BENCH_REQ_BYTES + 8]; 305 static unsigned rxn; 306 307 static void test_line(void) 308 { 309 if (rxn != 8 + 2 * BENCH_REQ_BYTES || memcmp(rxline, "ABCHREQ ", 8) != 0) 310 return; 311 uint8_t pkt[BENCH_REQ_BYTES]; 312 for (unsigned i = 0; i < BENCH_REQ_BYTES; i++) { 313 int hi = hexnib(rxline[8 + 2 * i]), lo = hexnib(rxline[9 + 2 * i]); 314 if (hi < 0 || lo < 0) return; 315 pkt[i] = (uint8_t)(hi << 4 | lo); 316 } 317 static const uint8_t no_mac[6]; 318 handle_bench_packet(pkt, sizeof pkt, no_mac, test_tsc_hz); 319 } 320 321 static void test_serial_poll(void) 322 { 323 if (ck_inb(COM1_BASE + 5) == 0xff) return; /* no 16550 here */ 324 while (ck_inb(COM1_BASE + 5) & 0x01) { 325 char c = (char)ck_inb(COM1_BASE); 326 if (c == '\r') continue; 327 if (c == '\n') { 328 if (rxn) { rxline[rxn] = 0; test_line(); } 329 rxn = 0; 330 continue; 331 } 332 if (rxn < sizeof rxline - 1) rxline[rxn++] = c; 333 else rxn = 0; /* over-long line: drop it */ 334 } 335 } 336 337 /* The emulated BARs (see the header). */ 338 static void test_bar_init(void) 339 { 340 if (test_inited) return; 341 test_inited = 1; 342 *(volatile uint32_t *)(volatile void *)test_cfg = 0x67ef1002u; /* 1002:67ef */ 343 test_bar5[TEST_KNOWN_REG] = 0xC0FFEE00u; 344 test_bar5[TEST_ALLONES_REG] = BENCH_ALLONES; 345 pci_cfg = test_cfg; 346 bar5 = test_bar5; 347 bar0 = test_bar0; 348 bench_dev_ready = 1; 349 ck_puts("bench: TEST BUILD: BARs are RAM stand-ins (emulated), not PCI hardware\n"); 350 } 351 #endif 352 353 /* ---- Public API ---- */ 354 355 /* "ready" means the main loop polls the console now: ck_gpu_bench sends its first request on this 356 * line, and a request sent while the kernel is still booting waits unanswered (the tool gives up). */ 357 static void announce_ready(void) 358 { 359 static int said; 360 if (said || !bench_dev_ready) 361 return; 362 said = 1; 363 /* one format string, so the line goes out whole (the test tool's requests share the wire) */ 364 ck_printf("bench: GPU bench console ready port=%u (HMAC-SHA256, " BENCH_KEY_KIND " bound in image)\n", 365 (unsigned)CK_X86_BENCH_PORT); 366 } 367 368 void ck_x86_bench_init(volatile uint8_t *gpu_cfg, volatile uint32_t *gpu_bar5, 369 volatile uint32_t *gpu_bar0) 370 { 371 #ifdef CK_X86_BENCH_TEST 372 (void)gpu_cfg; (void)gpu_bar5; (void)gpu_bar0; 373 test_bar_init(); 374 #else 375 if (!gpu_cfg || !gpu_bar5 || !gpu_bar0) { 376 ck_puts("bench: GPU not found (no BAR5/BAR0); bench console disabled\n"); 377 return; 378 } 379 pci_cfg = gpu_cfg; 380 bar5 = gpu_bar5; 381 bar0 = gpu_bar0; 382 bench_dev_ready = 1; 383 ck_puts("bench: GPU registers mapped (BAR5, BAR0); the console answers from the main loop\n"); 384 #endif 385 } 386 387 void ck_x86_bench_step(uint64_t tsc_hz) 388 { 389 announce_ready(); 390 #ifdef CK_X86_BENCH_TEST 391 test_tsc_hz = tsc_hz; 392 if (test_inited) 393 test_serial_poll(); 394 #else 395 /* Poll for a received bench packet from tg3 (LAB BUILD, untested on hardware). */ 396 static uint8_t pkt[2048]; 397 size_t pkt_len = 0; 398 uint8_t src_mac[6]; 399 /* ck_x86_tg3_recv_bench: fills pkt, pkt_len with the UDP payload (no IP/UDP 400 * headers) and src_mac when a bench packet arrives on port 47035. */ 401 if (ck_x86_tg3_recv_bench(pkt, sizeof pkt, &pkt_len, src_mac) == 0 && pkt_len) 402 handle_bench_packet(pkt, pkt_len, src_mac, tsc_hz); 403 #endif 404 } 405 406 #else /* !CK_X86_BENCH */ 407 void ck_x86_bench_init(volatile uint8_t *cfg, volatile uint32_t *b5, volatile uint32_t *b0) 408 { (void)cfg; (void)b5; (void)b0; } 409 void ck_x86_bench_step(uint64_t tsc_hz) { (void)tsc_hz; } 410 #endif /* CK_X86_BENCH */