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/disk/qemu/payload.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 / disk / qemu / payload.c` 220 lines ยท 8157 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 /* 18 * payload.c -- bare-metal AArch64 test of native/disk/disk_nvme.c against 19 * QEMU's emulated NVMe controller on the "virt" machine (highmem=off, so the 20 * PCIe ECAM sits at 0x3f000000 and the 32-bit MMIO window at 0x10000000). 21 * MMU off: every access is Device-nGnRnE and nothing is cached, so the DMA 22 * region needs no cache maintenance; the barrier op is DSB SY. 23 * 24 * QEMU is an emulator. A PASS here says the C driver works against QEMU's 25 * NVMe model; it says nothing about any physical controller. 26 */ 27 #include 28 #include "../disk.h" 29 #include "../disk_nvme.h" 30 31 #define UART 0x09000000ul 32 #define ECAM 0x3f000000ul 33 #define MMIO_BASE 0x10000000ul 34 35 static void putc_(char c) 36 { 37 volatile uint32_t *dr = (volatile uint32_t *)UART; 38 volatile uint32_t *fr = (volatile uint32_t *)(UART + 0x18); 39 while (*fr & (1u << 5)) 40 ; 41 *dr = (uint32_t)(uint8_t)c; 42 } 43 static void puts_(const char *s) { while (*s) { if (*s == '\n') putc_('\r'); putc_(*s++); } } 44 static void putdec(int64_t v) 45 { 46 char b[24]; 47 int n = 0; 48 uint64_t u = v < 0 ? (uint64_t)(-v) : (uint64_t)v; 49 if (v < 0) putc_('-'); 50 do { b[n++] = (char)('0' + u % 10); u /= 10; } while (u); 51 while (n) putc_(b[--n]); 52 } 53 static void puthex(uint64_t v) 54 { 55 puts_("0x"); 56 for (int i = 60; i >= 0; i -= 4) putc_("`0123456789abcdef`"[(v >> i) & 15]); 57 } 58 59 static int fails, checks; 60 static void check(int ok, const char *what, int64_t got) 61 { 62 checks++; 63 if (!ok) { 64 fails++; 65 puts_("FAIL: "); 66 puts_(what); 67 puts_(" (got "); 68 putdec(got); 69 puts_(")\n"); 70 } 71 } 72 73 /* ---- platform ops ---- */ 74 static volatile uint8_t *bar0; 75 static uint32_t r32(void *c, uint32_t o) { (void)c; return *(volatile uint32_t *)(bar0 + o); } 76 static void w32(void *c, uint32_t o, uint32_t v) { (void)c; *(volatile uint32_t *)(bar0 + o) = v; } 77 static void bar(void *c) { (void)c; __asm__ volatile("dsb sy" ::: "memory"); } 78 static uint64_t cntfrq(void) { uint64_t v; __asm__ volatile("mrs %0, cntfrq_el0" : "=r"(v)); return v; } 79 static uint64_t cntvct(void) { uint64_t v; __asm__ volatile("isb; mrs %0, cntvct_el0" : "=r"(v)); return v; } 80 static void delay(void *c, uint32_t us) 81 { 82 (void)c; 83 uint64_t f = cntfrq(), t0 = cntvct(), ticks = (f * us) / 1000000u + 1; 84 while (cntvct() - t0 < ticks) 85 ; 86 } 87 88 /* ---- PCIe: find the NVMe function, assign BAR0, enable MEM + bus master ---- */ 89 static volatile uint32_t *cfg(uint32_t bus, uint32_t dev, uint32_t fn, uint32_t off) 90 { 91 return (volatile uint32_t *)(ECAM + (bus << 20) + (dev << 15) + (fn << 12) + off); 92 } 93 static int pci_find_nvme(void) 94 { 95 for (uint32_t d = 0; d < 32; d++) { 96 uint32_t id = *cfg(0, d, 0, 0); 97 if ((id & 0xffff) == 0xffff) continue; 98 uint32_t cls = *cfg(0, d, 0, 8) >> 8; /* class/subclass/progif */ 99 if (cls != 0x010802) continue; 100 puts_("pci: NVMe at 00:"); 101 putdec(d); 102 puts_(".0 id "); 103 puthex(id); 104 puts_("\n"); 105 *cfg(0, d, 0, 4) = 0; /* disable decode while sizing */ 106 *cfg(0, d, 0, 0x10) = 0xffffffffu; 107 uint32_t lo = *cfg(0, d, 0, 0x10); 108 int is64 = ((lo >> 1) & 3) == 2; 109 uint32_t size = ~(lo & ~0xfu) + 1; 110 if (size == 0 || size > 0x1000000) { puts_("pci: bad BAR0 size\n"); return -1; } 111 *cfg(0, d, 0, 0x10) = (uint32_t)MMIO_BASE; 112 if (is64) *cfg(0, d, 0, 0x14) = 0; 113 *cfg(0, d, 0, 4) = (1u << 1) | (1u << 2); /* memory space + bus master */ 114 bar0 = (volatile uint8_t *)MMIO_BASE; 115 puts_("pci: BAR0 size "); 116 puthex(size); 117 puts_(is64 ? " 64-bit\n" : " 32-bit\n"); 118 return 0; 119 } 120 return -1; 121 } 122 123 /* ---- DMA region (identity mapped: phys == virt with the MMU off) ---- */ 124 #define DMA_BYTES (6u * 4096u + 128u * 1024u) 125 static uint8_t dma_mem[DMA_BYTES] __attribute__((aligned(4096))); 126 static uint8_t wbuf[300u * 1024u], rbuf[300u * 1024u]; 127 128 static void fill(uint8_t *b, uint32_t n, uint32_t seed) 129 { 130 for (uint32_t i = 0; i < n; i++) b[i] = (uint8_t)(seed * 37u + i * 13u + (i >> 8)); 131 } 132 static void zero(uint8_t *b, uint32_t n) { for (uint32_t i = 0; i < n; i++) ((volatile uint8_t *)b)[i] = 0; } 133 static int same(const uint8_t *a, const uint8_t *b, uint32_t n) 134 { 135 for (uint32_t i = 0; i < n; i++) if (a[i] != b[i]) return 0; 136 return 1; 137 } 138 139 static nvme_ctrl C; 140 static const nvme_ops OPS = {0, r32, w32, 0, 0, bar, delay}; 141 142 static void run_namespace(uint32_t nsid, uint32_t want_bs) 143 { 144 nvme_dma dma = {dma_mem, (uint64_t)(uintptr_t)dma_mem, DMA_BYTES}; 145 nvme_config cfg = {nsid, 0}; 146 int rc = nvme_init(&C, &OPS, &dma, &cfg); 147 check(rc == NVME_OK, "nvme_init", rc); 148 if (rc) return; 149 puts_("ns "); 150 putdec(C.nsid); 151 puts_(": block_size "); 152 putdec(C.block_size); 153 puts_(" blocks "); 154 putdec((int64_t)C.block_count); 155 puts_(" max_xfer "); 156 putdec(C.max_xfer_bytes); 157 puts_(" vwc "); 158 putdec(C.vwc); 159 puts_(" vs "); 160 puthex(C.vs); 161 puts_("\n"); 162 check(C.block_size == want_bs, "block size", C.block_size); 163 disk_dev d; 164 rc = nvme_disk(&C, &d); 165 check(rc == DISK_OK, "nvme_disk", rc); 166 if (rc) return; 167 uint32_t bs = d.block_size; 168 uint32_t sizes[] = {1, 8, (128u * 1024u) / bs, (300u * 1024u) / bs}; 169 uint64_t lba = 3; 170 for (uint32_t i = 0; i < 4; i++) { 171 uint32_t n = sizes[i], bytes = n * bs; 172 fill(wbuf, bytes, nsid * 10 + i); 173 rc = disk_write(&d, lba, n, wbuf); 174 check(rc == DISK_OK, "disk_write", rc); 175 zero(rbuf, bytes); 176 rc = disk_read(&d, lba, n, rbuf); 177 check(rc == DISK_OK, "disk_read", rc); 178 check(same(wbuf, rbuf, bytes), "read-back matches write", n); 179 lba += n + 1; 180 } 181 check(disk_flush(&d) == DISK_OK, "flush", 0); 182 /* out of range: refused by the driver, never sent */ 183 check(nvme_read(&C, d.block_count, 1, rbuf) == NVME_ERANGE, "range refusal", 0); 184 check(disk_read(&d, d.block_count - 1, 2, rbuf) == DISK_ERANGE, "disk range refusal", 0); 185 /* last block works */ 186 fill(wbuf, bs, 99); 187 check(disk_write(&d, d.block_count - 1, 1, wbuf) == DISK_OK, "write last block", 0); 188 check(disk_flush(&d) == DISK_OK, "flush 2", 0); 189 /* controller reset (disable + full re-init), data still there */ 190 check(nvme_disable(&C) == NVME_OK, "disable", 0); 191 check(nvme_read(&C, 0, 1, rbuf) == NVME_ESTATE, "I/O refused while disabled", 0); 192 rc = nvme_init(&C, &OPS, &dma, &cfg); 193 check(rc == NVME_OK, "re-init", rc); 194 zero(rbuf, bs); 195 check(nvme_read(&C, d.block_count - 1, 1, rbuf) == NVME_OK && same(wbuf, rbuf, bs), 196 "last block survives controller reset", 0); 197 uint32_t n = (300u * 1024u) / bs; 198 fill(wbuf, n * bs, nsid * 10 + 3); 199 zero(rbuf, n * bs); 200 check(disk_read(&d, lba - n - 1, n, rbuf) == DISK_OK && same(wbuf, rbuf, n * bs), 201 "300 KiB extent survives controller reset", 0); 202 check(nvme_disable(&C) == NVME_OK, "final disable", 0); 203 } 204 205 void payload_main(void) 206 { 207 puts_("AIENOS native NVMe payload (C driver, QEMU virt)\n"); 208 if (pci_find_nvme()) { 209 puts_("no NVMe function found\nNVME_QEMU_PAYLOAD: FAIL\n"); 210 return; 211 } 212 run_namespace(0, 512); /* first active namespace: 512 B LBAs */ 213 run_namespace(2, 4096); /* second namespace: 4 KiB LBAs */ 214 puts_("payload checks "); 215 putdec(checks); 216 puts_(", failures "); 217 putdec(fails); 218 puts_("\n"); 219 puts_(fails == 0 && checks > 0 ? "NVME_QEMU_PAYLOAD: PASS\n" : "NVME_QEMU_PAYLOAD: FAIL\n"); 220 }