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/tests/omega_nvme_runner.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 / tests / omega_nvme_runner.c` 273 lines ยท 13534 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Verification-only byte transport and existing C model/oracle, not a driver. 3 * Never accepts a block device. QEMU runs stopped with a disposable regular image. 4 */ 5 #define _POSIX_C_SOURCE 200809L 6 #define main original_reference_tests_main 7 #include "nvme_test.c" 8 #undef main 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 #include 17 #include 18 #include 19 #include 20 21 static int qfd = -1, outfd, infd, errors, published, reported, retained; 22 static pid_t qp = -1, child = -1; 23 static char tempdir[128], image[180], socketpath[180], qlog[180]; 24 static uint8_t expected[8192]; 25 static size_t wanted; 26 static const char *scenario; 27 static int result_code = -1; 28 static void die(const char *s) { perror(s); exit(2); } 29 static void check(int b, const char *s) { if (!b) { fprintf(stderr,"FAIL %s: %s\n",scenario,s); errors++; } } 30 static int exact(int fd, void *p, size_t n, int writing) 31 { 32 size_t done = 0; 33 while (done < n) { 34 struct pollfd f = {fd, writing ? POLLOUT : POLLIN, 0}; 35 if (poll(&f,1,3000) != 1) return -1; 36 ssize_t r = writing ? write(fd,(char *)p+done,n-done) : read(fd,(char *)p+done,n-done); 37 if (r <= 0) return -1; 38 done += (size_t)r; 39 } 40 return 0; 41 } 42 static void cleanup(void) 43 { 44 if (child > 0) { kill(child,SIGKILL); waitpid(child,NULL,0); } 45 if (qp > 0) { kill(qp,SIGTERM); waitpid(qp,NULL,0); } 46 if (qfd >= 0) close(qfd); 47 if (*image) unlink(image); 48 if (*socketpath) unlink(socketpath); 49 /* Keep QEMU stderr as an artifact when requested by the caller. */ 50 if (*qlog) unlink(qlog); 51 if (*tempdir) rmdir(tempdir); 52 } 53 static uint64_t qt(const char *fmt,...) 54 { 55 char line[512], response[256]; 56 va_list a; va_start(a,fmt); vsnprintf(line,sizeof line,fmt,a); va_end(a); 57 size_t n = strlen(line); line[n++]='\n'; 58 if (exact(qfd,line,n,1)) die("qtest send"); 59 n=0; 60 do { if (n+1 >= sizeof response || exact(qfd,response+n,1,0)) die("qtest response"); } while (response[n++]!='\n'); 61 response[n]=0; 62 if (strncmp(response,"OK",2)) { fprintf(stderr,"qtest: %s",response); exit(2); } 63 return response[2]==' ' ? strtoull(response+3,NULL,0) : 0; 64 } 65 static void start_qemu(void) 66 { 67 strcpy(tempdir,"/tmp/omega-nvme-XXXXXX"); 68 if (!mkdtemp(tempdir)) die("mkdtemp"); 69 snprintf(image,sizeof image,"%s/namespace.raw",tempdir); 70 snprintf(socketpath,sizeof socketpath,"%s/qtest",tempdir); 71 snprintf(qlog,sizeof qlog,"%s/qemu.log",tempdir); 72 int fd=open(image,O_CREAT|O_EXCL|O_RDWR,0600); if (fd<0) die("image"); 73 struct stat st; if (fstat(fd,&st) || !S_ISREG(st.st_mode)) die("regular image"); 74 uint8_t block[4096]; 75 for (size_t pos=0;pos<4096u*512u;pos+=sizeof block) { 76 for (size_t j=0;j>8)); 77 if (write(fd,block,sizeof block)!=(ssize_t)sizeof block) die("image fixture"); 78 } 79 close(fd); 80 char drive[256], qtest[256]; 81 snprintf(drive,sizeof drive,"if=none,id=ns,file=%s,format=raw",image); 82 snprintf(qtest,sizeof qtest,"unix:%s,server=on,wait=off",socketpath); 83 qp=fork(); if (qp<0) die("fork qemu"); 84 if (!qp) { 85 int log=open(qlog,O_CREAT|O_WRONLY,0600); dup2(log,2); close(log); 86 execlp("qemu-system-aarch64","qemu-system-aarch64","-machine","virt","-accel","tcg","-S","-m","64M","-nodefaults","-display","none","-monitor","none","-serial","none","-qtest",qtest,"-drive",drive,"-device","nvme,drive=ns,serial=OMEGA-FIXTURE-000001,addr=1.0",NULL); 87 _exit(127); 88 } 89 struct sockaddr_un sa={.sun_family=AF_UNIX}; strcpy(sa.sun_path,socketpath); 90 struct timespec nap={0,`10000000`}; 91 for (int i=0;i<300;i++) { 92 qfd=socket(AF_UNIX,SOCK_STREAM,0); 93 if (connect(qfd,(struct sockaddr *)&sa,sizeof sa)==0) break; 94 close(qfd); qfd=-1; nanosleep(&nap,NULL); 95 } 96 if (qfd<0) die("QEMU qtest socket"); 97 /* virt ECAM, bus0/dev1/function0. PCI binding is verifier setup, not .om NVMe logic. */ 98 uint64_t ecam=0x4010008000ull; 99 check((qt("readl 0x%llx",(unsigned long long)ecam)&0xffff)==0x1b36,"QEMU NVMe vendor"); 100 qt("writel 0x%llx 0x10000004",(unsigned long long)(ecam+0x10)); 101 qt("writel 0x%llx 0",(unsigned long long)(ecam+0x14)); 102 qt("writew 0x%llx 6",(unsigned long long)(ecam+4)); 103 } 104 static int iscase(const char *s) { return !strcmp(scenario,s); } 105 static void inject_io(void) 106 { 107 if (iscase("cid")) M.bad_cid=1; 108 if (iscase("sqid")) M.bad_sqid=1; 109 if (iscase("timeout") || iscase("cfs")) M.stuck=1; 110 if (iscase("cfs")) M.cfs_on_stuck=1; 111 if (iscase("status")) M.inject_sc=0x80; 112 } 113 static uint64_t access_rpc(uint64_t op,uint64_t addr,uint64_t val) 114 { 115 if (qfd<0 && M.nlog>0 && (iscase("timeout") || iscase("cfs") || iscase("cid") || iscase("sqid") || iscase("phase") || iscase("sqhead"))) { 116 check(!(op==5 || (op==3 && addr==0x10000014)), "uncertain DMA is not zeroed/reset/reopened"); 117 } 118 if (qfd>=0) { 119 if (op==1) return qt("readl 0x%llx",(unsigned long long)addr); 120 if (op==2) return qt("readq 0x%llx",(unsigned long long)addr); 121 if (op==3) return qt("writel 0x%llx 0x%llx",(unsigned long long)addr,(unsigned long long)val); 122 if (op==4) return qt("writeq 0x%llx 0x%llx",(unsigned long long)addr,(unsigned long long)val); 123 if (op==5) return qt("memset 0x%llx %llu 0",(unsigned long long)addr,(unsigned long long)val); 124 return 0; 125 } 126 if (addr>=0x10000000 && addr<0x10010000) { 127 uint32_t off=(uint32_t)(addr-0x10000000); 128 if (op==1) return m_read32(NULL,off); 129 if (op==2) return m_read32(NULL,off)|((uint64_t)m_read32(NULL,off+4)<<32); 130 if (op==3 || op==4) { 131 if (off==0x1008) inject_io(); 132 m_write32(NULL,off,(uint32_t)val); 133 if (off==0x1008 && (iscase("phase") || iscase("sqhead"))) { 134 uint64_t slot=(M.iocq_tail+M.iocq_size-1)%M.iocq_size; 135 uint8_t *c=ptr(M.iocq+slot*16,16); 136 if (iscase("phase")) p32(c+12,g32(c+12)^(1u<<16)); 137 else p32(c+8,(g32(c+8)&0xffff0000u)|15); 138 } 139 if (op==4) m_write32(NULL,off+4,(uint32_t)(val>>32)); 140 return 0; 141 } 142 } else if (addr>=PHYS_BASE && addr-PHYS_BASE8192) die("payload bound"); 156 uint8_t b[8192]; 157 if (qfd>=0) { 158 for (uint64_t i=0;i>(j*8)); 161 } 162 } else { 163 uint8_t *p=ptr(addr,n); if (!p) die("payload map"); memcpy(b,p,(size_t)n); 164 if (M.nadmin==1) { 165 memcpy(b+4,"OMEGA-FIXTURE-000001 ",20); 166 memcpy(b+64,"8.2.2 ",8); 167 } 168 } 169 if (exact(infd,b,(size_t)n,1)) die("payload send"); 170 } 171 int main(int argc,char **argv) 172 { 173 if (argc!=3 && argc!=4) { fprintf(stderr,"usage: runner binary case [qemu]\n"); return 2; } 174 scenario=argv[2]; atexit(cleanup); signal(SIGPIPE,SIG_IGN); 175 if (posix_memalign((void **)&dma_mem,4096,DMA_BYTES)) die("oracle memory"); 176 model_reset(); 177 if (iscase("lba4k")) M.lbads=12; 178 model_alloc_media(); fill(M.media,(size_t)M.nsze*bsz(),7); 179 check(init_default()==NVME_OK,"C oracle initialization"); 180 check(nvme_read(&C,8,iscase("lba4k")?2:16,expected)==NVME_OK,"C oracle read"); 181 model_reset(); 182 if (iscase("lba4k")) M.lbads=12; 183 model_alloc_media(); fill(M.media,(size_t)M.nsze*bsz(),7); 184 uint8_t manifest[80]={0}; 185 memcpy(manifest,"OMEGA-FIXTURE-000001 ",20); memcpy(manifest+20,"8.2.2 ",8); 186 p32(manifest+28,1); p64(manifest+32,8); p64(manifest+40,64); 187 p64(manifest+48,8); p32(manifest+56,16); p32(manifest+60,40); p32(manifest+64,1); 188 p64(manifest+72,argc==4?0x40000000:PHYS_BASE); 189 int expected_code=0, expected_copies=40, expected_retained=0; 190 wanted=8192; 191 if (iscase("lba4k")) p32(manifest+56,2); 192 if (iscase("singlepage")) { p32(manifest+56,8); wanted=4096; } 193 if (iscase("serial")) { manifest[0]^=1; expected_code=21; } 194 if (iscase("firmware")) { manifest[20]^=1; expected_code=22; } 195 if (iscase("approval")) { p32(manifest+64,0); expected_code=20; } 196 if (iscase("nsid")) { p32(manifest+28,2); expected_code=argc==4?25:24; } 197 if (iscase("extent")) { p64(manifest+40,4096); expected_code=27; } 198 if (iscase("range")) { p64(manifest+48,72); expected_code=31; } 199 if (iscase("before")) { p64(manifest+48,7); expected_code=31; } 200 if (iscase("prp")) { p32(manifest+56,17); expected_code=30; } 201 if (iscase("zero")) { p32(manifest+56,0); expected_code=30; } 202 if (iscase("metadata")) { M.ms=8; expected_code=26; } 203 if (iscase("format")) { M.lbads=10; expected_code=26; } 204 if (iscase("nsze")) { M.nsze=0; expected_code=25; } 205 if (iscase("sqes")) { M.sqes=0x55; expected_code=23; } 206 if (iscase("cqes")) { M.cqes=0x33; expected_code=23; } 207 if (iscase("cap")) { M.cap&=~(1ull<<37); expected_code=28; } 208 if (iscase("dstrd")) M.cap|=1ull<<32; 209 if (iscase("stride")) { M.cap|=13ull<<32; expected_code=28; } 210 if (iscase("readytimeout")) { M.never_ready=1; expected_code=12; expected_retained=1; } 211 if (iscase("readycfs")) { M.cfs_on_enable=1; expected_code=11; expected_retained=1; } 212 if (iscase("cid")) { expected_code=14; expected_retained=1; } 213 if (iscase("sqid")) { expected_code=15; expected_retained=1; } 214 if (iscase("timeout")) { expected_code=12; expected_retained=1; } 215 if (iscase("cfs")) { expected_code=11; expected_retained=1; } 216 if (iscase("phase")) { expected_code=12; expected_retained=1; } 217 if (iscase("sqhead")) { expected_code=16; expected_retained=1; } 218 if (iscase("status")) expected_code=17; 219 if (!strncmp(scenario,"opcode",6)) { 220 expected_code=13; 221 p32(manifest+60,1); 222 p32(manifest+68,(uint32_t)strtoul(scenario+6,NULL,0)); 223 } 224 if (iscase("adminformat")) { 225 expected_code=13; p32(manifest+60,0); p32(manifest+68,0x80); 226 } 227 if (expected_code) expected_copies=0; 228 if (argc==4) { 229 start_qemu(); 230 /* QEMU 8.2.2 reports its model firmware as 8.2.2 (space padded). */ 231 } 232 int input[2], output[2]; if (pipe(input)||pipe(output)) die("pipe"); 233 child=fork(); if (child<0) die("fork candidate"); 234 if (!child) { 235 dup2(input[0],0); dup2(output[1],1); 236 close(input[0]); close(input[1]); close(output[0]); close(output[1]); 237 execl(argv[1],argv[1],NULL); _exit(127); 238 } 239 close(input[0]); close(output[1]); infd=input[1]; outfd=output[0]; 240 if (exact(infd,manifest,sizeof manifest,1)) die("manifest"); 241 uint8_t request[24], response[8]; unsigned requests=0; 242 while (exact(outfd,request,sizeof request,0)==0) { 243 if (++requests>50000) die("RPC budget"); 244 uint64_t op=g64(request),addr=g64(request+8),val=g64(request+16),r=0; 245 if (op>=1 && op<=5) r=access_rpc(op,addr,val); 246 else if (op!=6 && op!=7 && op!=8) die("RPC opcode"); 247 p64(response,r); if (exact(infd,response,sizeof response,1)) die("RPC response"); 248 if (op==6) payload(addr,val); 249 if (op==7) { 250 uint8_t data[8192]; check(val==wanted,"copy-out length"); 251 if (val>sizeof data || exact(outfd,data,(size_t)val,0)) die("copy-out"); 252 check(!memcmp(data,expected,(size_t)val),"copy-out equals existing C oracle"); published++; 253 } 254 if (op==8) { result_code=(int)val; retained=(int)addr; reported++; } 255 } 256 close(infd); close(outfd); 257 int status=0; waitpid(child,&status,0); child=-1; 258 check(WIFEXITED(status) && WEXITSTATUS(status)==expected_code,"candidate exit code"); 259 check(reported==1 && result_code==expected_code,"reported result"); 260 check(retained==expected_retained,"timeout/correlation DMA retention"); 261 check(published==expected_copies,"copy-out only after success"); 262 if (qfd<0) { 263 check(M.protocol_errors==0,"existing C model protocol checks"); 264 for (int i=0;i