interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / aienux / disk / tests / test_nvme_ro_bare.om

415 lines · 9305 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/disk/tests/test_nvme_ro_bare.om: Host simulator test for nvme_ro_bare.om.
3note Verifies:
4note 1. CAP.TO * 500 ms wall-clock timeout (succeeds when CSTS.RDY arrives at 300 ms, fails when stalled > 1000 ms).
5note 2. MDTS clamping (MDTS=0 -> 128 KiB, MDTS=3 -> 32 KiB, MDTS=6 -> clamped to 128 KiB).
6note 3. Multi-page PRP-list reads (1 page, 2 pages, 32 pages = 128 KiB) + 256 KiB chunked read.
7note 4. Pre-DMA WB flush (nvme_wb_clean_inv) called with exact (dst_pa, total_bytes) before DMA.
8note 5. <40-bit PA bound enforcement (NV_E_PA40).
9note 6. Hard read-only opcode allowlist: I/O Write (0x01), Flush (0x00), DSM (0x09), Format (0x80)
10note all refused with NV_E_OPCODE (13) with ZERO SQE or doorbell writes.
11note 7. CSTS.CFS fatal abort (NV_E_CFS = 11).
12
13const SIM_BAR0 ulong 1645215744
14const SIM_DMA ulong 268435456
15const SIM_RAM ulong 536870912
16
17note Simulated RAM: 64 KiB DMA arena at SIM_DMA, 512 KiB target RAM at SIM_RAM, 512 KiB disk (1024 sectors)
18global sim_dma uchar 65536
19global sim_ram uchar 524288
20global sim_disk uchar 524288
21note sim_st: 0=now_ms, 1=rdy_delay_ms, 2=rdy_start_ms, 3=cc, 4=csts, 5=stall_rdy,
22note 6=mdts_val, 7=wb_calls, 8=wb_last_pa, 9=wb_last_len, 10=sq_writes,
23note 11=db_writes, 12=asq_head, 13=isq_head, 14=acq_tail, 15=icq_tail,
24note 16=acq_phase, 17=icq_phase, 18=inject_cfs
25global sim_st ulong 20
26
27fn sim_u32_r mem ulong off ulong to ulong
28if eq mem 0
29b0 = ulong get sim_dma off
30b1 = ulong get sim_dma add off 1
31b2 = ulong get sim_dma add off 2
32b3 = ulong get sim_dma add off 3
33ret or or b0 shl b1 8 or shl b2 16 shl b3 24
34end
35b0 = ulong get sim_ram off
36b1 = ulong get sim_ram add off 1
37b2 = ulong get sim_ram add off 2
38b3 = ulong get sim_ram add off 3
39ret or or b0 shl b1 8 or shl b2 16 shl b3 24
40end
41
42fn sim_u32_w mem ulong off ulong v ulong to void
43if eq mem 0
44put sim_dma off uchar and v 255
45put sim_dma add off 1 uchar and shr v 8 255
46put sim_dma add off 2 uchar and shr v 16 255
47put sim_dma add off 3 uchar and shr v 24 255
48ret
49end
50put sim_ram off uchar and v 255
51put sim_ram add off 1 uchar and shr v 8 255
52put sim_ram add off 2 uchar and shr v 16 255
53put sim_ram add off 3 uchar and shr v 24 255
54end
55
56fn sim_u64_r mem ulong off ulong to ulong
57lo = sim_u32_r mem off
58hi = sim_u32_r mem add off 4
59ret or lo shl hi 32
60end
61
62fn nvme_dma_r32 addr ulong to ulong
63if and ge addr SIM_DMA lt addr add SIM_DMA 65536
64ret sim_u32_r 0 sub addr SIM_DMA
65end
66if and ge addr SIM_RAM lt addr add SIM_RAM 524288
67ret sim_u32_r 1 sub addr SIM_RAM
68end
69exit 91
70end
71
72fn nvme_dma_w32 addr ulong val ulong to void
73if and ge addr SIM_DMA lt addr add SIM_DMA 65536
74sim_u32_w 0 sub addr SIM_DMA val
75put sim_st 10 add get sim_st 10 1
76ret
77end
78if and ge addr SIM_RAM lt addr add SIM_RAM 524288
79sim_u32_w 1 sub addr SIM_RAM val
80ret
81end
82exit 92
83end
84
85fn nvme_dma_w64 addr ulong val ulong to void
86nvme_dma_w32 addr and val 4294967295
87nvme_dma_w32 add addr 4 shr val 32
88end
89
90fn nvme_dma_r8 addr ulong to ulong
91if and ge addr SIM_DMA lt addr add SIM_DMA 65536
92ret ulong get sim_dma sub addr SIM_DMA
93end
94if and ge addr SIM_RAM lt addr add SIM_RAM 524288
95ret ulong get sim_ram sub addr SIM_RAM
96end
97exit 93
98end
99
100fn nvme_dma_zero addr ulong size ulong to void
101if not and ge addr SIM_DMA le add addr size add SIM_DMA 65536
102exit 94
103end
104base = sub addr SIM_DMA
105i = ulong 0
106while lt i size bound 65536
107put sim_dma add base i 0
108i = add i 1
109end
110end
111
112fn nvme_now_ms to ulong
113t = get sim_st 0
114put sim_st 0 add t 10
115ret t
116end
117
118fn nvme_dsb_sy to void
119end
120
121fn nvme_wb_clean_inv pa ulong nbytes ulong to void
122put sim_st 7 add get sim_st 7 1
123put sim_st 8 pa
124put sim_st 9 nbytes
125end
126
127fn sim_post_cqe q ulong cid ulong status ulong to void
128cq_off = NV_OFF_ACQ
129tail_idx = ulong 14
130ph_idx = ulong 16
131sqh_idx = ulong 12
132if eq q 1
133cq_off = NV_OFF_ICQ
134tail_idx = ulong 15
135ph_idx = ulong 17
136sqh_idx = ulong 13
137end
138t = get sim_st tail_idx
139ph = get sim_st ph_idx
140slot = add cq_off mul t 16
141sim_u32_w 0 slot 0
142sim_u32_w 0 add slot 4 0
143sim_u32_w 0 add slot 8 or shl q 16 get sim_st sqh_idx
144sim_u32_w 0 add slot 12 or or cid shl ph 16 shl status 17
145t = add t 1
146if eq t NV_DEPTH
147t = ulong 0
148put sim_st ph_idx xor ph 1
149end
150put sim_st tail_idx t
151end
152
153fn sim_copy_to_pa pa ulong disk_off ulong nbytes ulong to void
154if not and ge pa SIM_RAM le add pa nbytes add SIM_RAM 524288
155exit 95
156end
157dst = sub pa SIM_RAM
158i = ulong 0
159while lt i nbytes bound 4096
160put sim_ram add dst i get sim_disk add disk_off i
161i = add i 1
162end
163end
164
165fn sim_exec_admin slot ulong to void
166dw0 = sim_u32_r 0 slot
167op = and dw0 255
168cid = shr dw0 16
169if eq op 6
170cns = sim_u32_r 0 add slot 40
171id_off = NV_OFF_ID
172i = ulong 0
173while lt i 4096 bound 4096
174put sim_dma add id_off i 0
175i = add i 1
176end
177if eq cns 1
178note Identify Controller: MDTS at +77, NN=1 at +516
179put sim_dma add id_off 77 uchar get sim_st 6
180sim_u32_w 0 add id_off 516 1
181else
182note Identify Namespace: NSZE=1024 at +0, LBAF0 lbads=9 (512B) at +128
183sim_u32_w 0 id_off 1024
184sim_u32_w 0 add id_off 128 shl ulong 9 16
185end
186sim_post_cqe 0 cid 0
187ret
188end
189if or eq op 5 eq op 1
190sim_post_cqe 0 cid 0
191ret
192end
193exit 96
194end
195
196fn sim_prp_pa prp1 ulong prp2 ulong pages ulong p ulong to ulong
197if eq p 0
198ret prp1
199end
200if and eq p 1 eq pages 2
201ret prp2
202end
203ret sim_u64_r 0 add sub prp2 SIM_DMA mul sub p 1 8
204end
205
206fn sim_exec_io slot ulong to void
207dw0 = sim_u32_r 0 slot
208if ne and dw0 255 2
209exit 97
210end
211prp1 = sim_u64_r 0 add slot 24
212prp2 = sim_u64_r 0 add slot 32
213doff = mul sim_u64_r 0 add slot 40 512
214nbytes = mul add and sim_u32_r 0 add slot 48 65535 1 512
215pages = div add nbytes 4095 4096
216p = ulong 0
217while lt p pages bound 32
218pa = sim_prp_pa prp1 prp2 pages p
219take = sub nbytes mul p 4096
220if gt take 4096
221take = ulong 4096
222end
223sim_copy_to_pa pa add doff mul p 4096 take
224p = add p 1
225end
226sim_post_cqe 1 shr dw0 16 0
227end
228
229fn nvme_mmio_r32 addr ulong to ulong
230off = sub addr SIM_BAR0
231if eq off 8
232ret shl ulong 1 16
233end
234if eq off 28
235if ne get sim_st 18 0
236ret 2
237end
238if ne get sim_st 5 0
239ret get sim_st 4
240end
241want = and get sim_st 3 1
242if ne want get sim_st 4
243if ge sub get sim_st 0 get sim_st 2 get sim_st 1
244put sim_st 4 want
245end
246end
247ret get sim_st 4
248end
249ret 0
250end
251
252fn nvme_mmio_r64 addr ulong to ulong
253off = sub addr SIM_BAR0
254if eq off 0
255note CAP: MQES=63, TO=2 (1000 ms), DSTRD=0, CSS=NVM(bit 37), MPSMIN=0
256ret or or ulong 63 shl ulong 2 24 shl ulong 1 37
257end
258ret 0
259end
260
261fn nvme_mmio_w32 addr ulong val ulong to void
262off = sub addr SIM_BAR0
263put sim_st 11 add get sim_st 11 1
264if eq off 20
265put sim_st 3 val
266put sim_st 2 get sim_st 0
267ret
268end
269if eq off 4096
270note Admin SQ doorbell
271h = get sim_st 12
272put sim_st 12 val
273sim_exec_admin add NV_OFF_ASQ mul h 64
274ret
275end
276if eq off 4104
277note I/O SQ doorbell
278h2 = get sim_st 13
279put sim_st 13 val
280sim_exec_io add NV_OFF_ISQ mul h2 64
281ret
282end
283end
284
285fn nvme_mmio_w64 addr ulong val ulong to void
286put sim_st 11 add get sim_st 11 1
287end
288
289fn sim_reset mdts ulong delay_ms ulong stall ulong to void
290for i ulong 20
291put sim_st i 0
292end
293put sim_st 1 delay_ms
294put sim_st 5 stall
295put sim_st 6 mdts
296put sim_st 16 1
297put sim_st 17 1
298end
299
300fn main to int
301note Populate synthetic 512 KiB disk with deterministic pattern
302d = ulong 0
303while lt d 524288 bound 524288
304put sim_disk d uchar and xor mul d 31 shr d 8 255
305d = add d 1
306end
307
308note Test 1: CAP.TO * 500 ms (1000 ms) succeeds when CSTS.RDY takes 300 ms (30 polls > old bug if stalled)
309sim_reset 0 300 0
310r = nvme_init SIM_BAR0 SIM_DMA 1
311if ne r NV_OK
312ret 10
313end
314if ne nvme_cap_to_ms 1000
315ret 11
316end
317if ne nvme_max_xfer 131072
318ret 12
319end
320
321note Test 2: 256 KiB (512 sectors) chunked PRP-list direct read into WB RAM -> checks pre-DMA WB flush + exact data
322r = nvme_read_direct 16 512 SIM_RAM 1
323if ne r NV_OK
324ret 13
325end
326if ne get sim_st 7 2
327ret 14
328end
329if ne get sim_st 8 SIM_RAM
330ret 14
331end
332if ne get sim_st 9 262144
333ret 14
334end
335k = ulong 0
336while lt k 262144 bound 262144
337if ne get sim_ram k get sim_disk add 8192 k
338ret 15
339end
340k = add k 1
341end
342
343note Test 3: Hard read-only opcode allowlist — hostile opcodes MUST return NV_E_OPCODE (13) with ZERO SQ/DB writes
344sq_before = get sim_st 10
345db_before = get sim_st 11
346nv_clear_sqe
347put nv_sqe 0 1
348if ne nv_submit 1 NV_E_OPCODE
349ret 16
350end
351nv_clear_sqe
352put nv_sqe 0 0
353if ne nv_submit 1 NV_E_OPCODE
354ret 17
355end
356nv_clear_sqe
357put nv_sqe 0 9
358if ne nv_submit 1 NV_E_OPCODE
359ret 18
360end
361nv_clear_sqe
362put nv_sqe 0 128
363if ne nv_submit 0 NV_E_OPCODE
364ret 19
365end
366if or ne get sim_st 10 sq_before ne get sim_st 11 db_before
367ret 20
368end
369
370note Test 4: PA=0, wrap, and <40-bit PA bound refusals
371if ne nvme_read_direct 0 8 0 0 NV_E_ARG
372ret 21
373end
374if ne nvme_read_direct 0 8 NV_PA40_LIMIT 0 NV_E_PA40
375ret 21
376end
377if ne nvme_init SIM_BAR0 0 1 NV_E_ARG
378ret 28
379end
380if ne nvme_init SIM_BAR0 18446744073709547520 1 NV_E_PA40
381ret 29
382end
383
384note Test 5: MDTS=3 -> capped at 32 KiB; MDTS=6 -> clamped at 128 KiB
385sim_reset 3 10 0
386if ne nvme_init SIM_BAR0 SIM_DMA 1 NV_OK
387ret 22
388end
389if ne nvme_max_xfer 32768
390ret 23
391end
392sim_reset 6 10 0
393if ne nvme_init SIM_BAR0 SIM_DMA 1 NV_OK
394ret 24
395end
396if ne nvme_max_xfer 131072
397ret 25
398end
399
400note Test 6: Stalled CSTS.RDY times out cleanly against CAP.TO * 500 ms (NV_E_TIMEOUT = 12)
401sim_reset 0 0 1
402put sim_st 4 1
403if ne nvme_init SIM_BAR0 SIM_DMA 1 NV_E_TIMEOUT
404ret 26
405end
406
407note Test 7: CSTS.CFS fatal abort (NV_E_CFS = 11)
408sim_reset 0 0 0
409put sim_st 18 1
410if ne nvme_init SIM_BAR0 SIM_DMA 1 NV_E_CFS
411ret 27
412end
413
414ret 0
415end