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 / kernel / hal / smmu_qemu_test.om

529 lines · 12331 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/kernel/hal/smmu_qemu_test.om: target a64efi QEMU proof of the mode-1 (memory-backed) SMMUv3
3note back end in smmuv3.om. Runs under qemu-system-aarch64 -M virt,iommu=smmuv3 with -device edu:
4note 1. IORT -> SMMUv3 base; smmu_init_instance (zeroed 2-level STRTAB, CR1/CR2, GBPA.ABORT, CMDQ/EVENTQ,
5note CFGI_ALL + TLBI_NSNH_ALL + CMD_SYNC completion, SMMUEN) -> SYNC must complete (CONS == PROD, ERR == 0).
6note 2. smmu_open_stream for the edu RID (CD + L1STD + L2 STE in memory, CFGI_STE/CFGI_CD_ALL/TLBI/SYNC).
7note 3. smmu_map_span one 8 KiB Normal-NC buffer (VA == PA stage-1 tables) and run the edu DMA engine
8note RAM -> device -> RAM through it: the bytes must round-trip (mapped DMA succeeds).
9note 4. edu DMA device -> an UNMAPPED page: the page must stay untouched and the EVENTQ must hold an
10note F_TRANSLATION (0x10) record whose StreamID and InputAddr match (unmapped DMA faults + is recorded).
11note 5. smmu_map_span_wb on frames_alloc (CPU Normal-WB) RAM: refused under a TEST-ONLY mock CCA=0, then
12note mapped with the real IORT CCA=1 and an edu device -> RAM DMA must land in it.
13note 6. CMDQ in-flight limit: a 15th non-SYNC command is refused, the SYNC fails closed, the queue recovers.
14note 7. Failed open (mode 1): CMDQ pre-filled so the open's CFGI is refused -> SMMU_ECMDERR; the STE written
15note to memory must be invalidated (word 0 == 0), the slot not live, and a retry of the same RID succeeds.
16note Prints SMMU_QEMU_PASS or SMMU_QEMU_FAIL step=N, then PSCI SYSTEM_RESET (QEMU -no-reboot exits).
17note QEMU-only: never run on Spark silicon (no edu device; refuses anything but vendor 0x1234/0x11e8).
18
19const SQ_EFI_MMAP_CAP ulong 65536
20const SQ_EDU_VID ulong 0x1234
21const SQ_EDU_DID ulong 0x11e8
22const SQ_EDU_BAR_SZ ulong 1048576
23const SQ_EDU_BUF ulong 0x40000
24const SQ_DMA_LEN ulong 256
25const SQ_SENT ulong 0x5a5a5a5a5a5a5a5a
26
27global efi_mmap uchar 65536
28global efi_mm_out uchar 64
29note sq_st: 0=edu dev, 1=edu bar0, 2=smmu base, 3=sid, 4=stream handle, 5=buf pa, 6=unmapped pa
30global sq_st ulong 8
31
32text sq_hdr "\n[SMMUQ] mode-1 SMMUv3 on QEMU virt iommu=smmuv3 + edu DMA\n"
33text sq_up "SMMUQ_UP base=0x"
34text sq_cmds " cmds="
35text sq_sid "SMMUQ_STREAM sid=0x"
36text sq_okdma "SMMUQ_MAPPED_DMA_OK bytes="
37text sq_evt "SMMUQ_FAULT type=0x"
38text sq_esid " sid=0x"
39text sq_eaddr " addr=0x"
40text sq_pass "SMMU_QEMU_PASS mapped_dma=ok unmapped_dma=F_TRANSLATION evtq=recorded cmd_sync=complete wb=ok cmdq_limit=ok failopen=teardown\n"
41text sq_wbmock "SMMUQ_WB_MOCK_CCA0_REFUSED (test-only: stream CCA forced to 0)\n"
42text sq_wbok "SMMUQ_WB_DMA_OK frames_alloc WB page, IORT CCA=1, bytes="
43text sq_qlim "SMMUQ_CMDQ_LIMIT_OK refused=15th recovered=yes\n"
44text sq_fo "SMMUQ_FAILOPEN_TEARDOWN_OK rid=0x10 ecmderr=yes ste_invalidated=yes retry=ok\n"
45text sq_fail "SMMU_QEMU_FAIL step="
46text sq_nl "\n"
47
48fn efi_find_acpi2_rsdp to ulong
49ret efcfg 0x11d3e4f18868e871 0x81883cc7800022bc
50end
51
52fn efi_exit_boot_services to bool
53bs = efbs
54ih = fwsys 0
55x = fwcall fwld add bs 256 8 0 0 0 0 0 0
56outp = fwadr efi_mm_out 0 64
57x = fwst add outp 40 8 efi_find_acpi2_rsdp
58x = fwst add outp 48 8 arm64_current_el
59gmm = fwld add bs 56 8
60ebs = fwld add bs 232 8
61mp = fwadr efi_mmap 0 SQ_EFI_MMAP_CAP
62att = ulong 0
63while lt att 8 bound 8
64x = fwst outp 8 SQ_EFI_MMAP_CAP
65s = fwcall gmm outp mp add outp 8 add outp 16 add outp 24 0
66if ne s 0
67ret false
68end
69s2 = fwcall ebs ih fwld add outp 8 8 0 0 0 0
70att = add att 1
71if eq s2 0
72ret true
73end
74end
75ret false
76end
77
78fn sq_halt to void
79arm64_psci_reset get acpi_info 7
80i = ulong 0
81while lt i 9223372036854775807 bound 9223372036854775807
82arm64_wfe
83end
84end
85
86fn sq_die step ulong v ulong to void
87uart_puts sq_fail
88uart_putdec step
89uart_puts sq_esid
90uart_puthex v
91uart_puts sq_nl
92sq_halt
93end
94
95note Same ECAM registration as spark_main.om spk_register_mcfg_segs (Bus 0..1, 2 MiB Device-nGnRnE window).
96fn sq_mcfg to ulong
97nm = get acpi_info 12
98if gt nm PCIE_MAX_SEGS
99nm = PCIE_MAX_SEGS
100end
101mapped = ulong 0
102i = ulong 0
103while lt i nm bound 16
104base = get acpi_mcfg_base i
105if ne base 0
106if mmio_map_device base SPARK_ECAM_WIN_SZ
107put pcie_seg_base mapped base
108put pcie_seg_id mapped uint get acpi_mcfg_seg i
109put pcie_seg_bus0 mapped uchar and get acpi_mcfg_sbus i 255
110put pcie_seg_bus1 mapped uchar 1
111mapped = add mapped 1
112end
113end
114i = add i 1
115end
116put pcie_nsegs 0 mapped
117j = ulong 0
118while lt j mapped bound 16
119h = pcie_map_seg_ecam get pcie_seg_id j
120j = add j 1
121end
122ret mapped
123end
124
125note Find edu on segment 0 bus 0; BAR0 is a 32-bit non-prefetchable memory BAR (type bits [2:1] = 0).
126fn sq_find_edu to bool
127d = ulong 0
128while lt d 32 bound 32
129if eq pcie_probe_dev 0 0 d 0 PCIE_OK
130if and eq get pcie_dev_info 0 SQ_EDU_VID eq get pcie_dev_info 1 SQ_EDU_DID
131b = get pcie_dev_info 5
132bar = and b 0xfffffff0
133if eq and b 6 4
134bar = or bar shl get pcie_dev_info 6 32
135end
136if eq bar 0
137ret false
138end
139put sq_st 0 d
140put sq_st 1 bar
141ret true
142end
143end
144d = add d 1
145end
146ret false
147end
148
149fn sq_iort to bool
150ib = new uchar 4096 0
151ipa = get acpi_info 10
152ilen = get acpi_info 11
153if or eq ipa 0 or lt ilen 48 gt ilen 4096
154ret false
155end
156k = ulong 0
157while lt k ilen bound 4096
158put ib k uchar and fwld add ipa k 1 255
159k = add k 1
160end
161ret eq iort_parse part ib 0 ilen 1
162end
163
164fn sq_smmu_up to long
165base = get iort_smmu_base 0
166put sq_st 2 base
167if not mmio_map_device base SPARK_SMMU_SZ
168ret -100
169end
170st = dma_alloc_nc 2 8192
171cq = dma_alloc_nc 1 4096
172eq0 = dma_alloc_nc 1 4096
173pool = dma_alloc_nc 64 4096
174if or or eq st 0 eq cq 0 or eq eq0 0 eq pool 0
175ret -101
176end
177if ne smmu_set_pt_pool pool 64 SMMU_OK
178ret -102
179end
180ret smmu_init_instance 0 base 16 st cq eq0
181end
182
183fn sq_edu_w64 off ulong v ulong to void
184arm64_mmio_w64 add get sq_st 1 off v
185end
186
187fn sq_edu_r32 off ulong to ulong
188ret arm64_mmio_r32 add get sq_st 1 off
189end
190
191note edu DMA (QEMU hw/misc/edu.c): 0x80 src, 0x88 dst, 0x90 count, 0x98 cmd (bit0 RUN, bit1 1 = device->RAM).
192fn sq_edu_dma src ulong dst ulong to_ram ulong to bool
193sq_edu_w64 0x80 src
194sq_edu_w64 0x88 dst
195sq_edu_w64 0x90 SQ_DMA_LEN
196cmd = ulong 1
197if ne to_ram 0
198cmd = ulong 3
199end
200arm64_mmio_w32 add get sq_st 1 0x98 cmd
201i = ulong 0
202while lt i 200000000 bound 200000000
203if eq and sq_edu_r32 0x98 1 0
204ret true
205end
206cpurelax
207i = add i 1
208end
209ret false
210end
211
212fn sq_fill pa ulong v ulong n ulong to void
213o = ulong 0
214while lt o n bound 1024
215arm64_mmio_w64 add pa o add v o
216o = add o 8
217end
218end
219
220fn sq_same pa ulong v ulong n ulong to bool
221o = ulong 0
222while lt o n bound 1024
223if ne arm64_mmio_r64 add pa o add v o
224ret false
225end
226o = add o 8
227end
228ret true
229end
230
231fn sq_untouched pa ulong n ulong to bool
232o = ulong 0
233while lt o n bound 1024
234if ne arm64_mmio_r64 add pa o SQ_SENT
235ret false
236end
237o = add o 8
238end
239ret true
240end
241
242fn sq_stream to bool
243d = get sq_st 0
244rid = uint shl d 3
245cd = dma_alloc_nc 1 4096
246tt = dma_alloc_nc 1 4096
247if or eq cd 0 eq tt 0
248ret false
249end
250sh = smmu_open_stream 0 rid cd tt
251if le sh 0
252ret false
253end
254put sq_st 3 get iort_qres 3
255put sq_st 4 ulong sh
256uart_puts sq_sid
257uart_puthex get sq_st 3
258uart_puts sq_nl
259ret true
260end
261
262fn sq_dma_tests to ulong
263buf = dma_alloc_nc 2 4096
264bad = dma_alloc_nc 1 4096
265if or eq buf 0 eq bad 0
266ret 30
267end
268put sq_st 5 buf
269put sq_st 6 bad
270if ne smmu_map_span long get sq_st 4 buf 8192 0 SMMU_OK
271ret 31
272end
273sq_fill buf 0x0123456789abcdef SQ_DMA_LEN
274for z ulong 32
275arm64_mmio_w64 add add buf 4096 mul z 8 SQ_SENT
276arm64_mmio_w64 add bad mul z 8 SQ_SENT
277end
278if ne pcie_enable_endpoint 0 0 get sq_st 0 0 PCIE_OK
279ret 32
280end
281if not mmio_map_device get sq_st 1 SQ_EDU_BAR_SZ
282ret 33
283end
284if not sq_edu_dma buf SQ_EDU_BUF 0
285ret 34
286end
287if not sq_edu_dma SQ_EDU_BUF add buf 4096 1
288ret 35
289end
290if not sq_same add buf 4096 0x0123456789abcdef SQ_DMA_LEN
291ret 36
292end
293uart_puts sq_okdma
294uart_putdec SQ_DMA_LEN
295uart_puts sq_nl
296if not sq_edu_dma SQ_EDU_BUF bad 1
297ret 37
298end
299if not sq_untouched bad SQ_DMA_LEN
300ret 38
301end
302ret 0
303end
304
305fn sq_check_fault to ulong
306n = smmu_drain_events 0
307if le n 0
308ret 40
309end
310w0 = get smmu_inst_last_evt 0
311uart_puts sq_evt
312uart_puthex and w0 255
313uart_puts sq_esid
314uart_puthex shr w0 32
315uart_puts sq_eaddr
316uart_puthex get smmu_inst_last_addr 0
317uart_puts sq_nl
318if ne and w0 255 0x10
319ret 41
320end
321if ne shr w0 32 get sq_st 3
322ret 42
323end
324if ne and get smmu_inst_last_addr 0 0xfffffffffffff000 get sq_st 6
325ret 43
326end
327ret 0
328end
329
330note WB attribute check on memory that really is CPU Normal-WB (frames_alloc RAM, mapped WB by mmu.om):
331note (a) TEST-ONLY mock: force this stream's recorded CCA to 0 -> smmu_map_span_wb must refuse (SMMU_EATTR);
332note (b) real IORT CCA (QEMU virt RC reports CCA=1) -> WB map, edu device->RAM DMA lands, CPU reads it back.
333fn sq_wb_tests to ulong
334wb = frames_alloc 1 4096 SMMU_PA_40_LIMIT
335if eq wb 0
336ret 50
337end
338sh = long get sq_st 4
339slot = sub get sq_st 4 1
340cca = get smmu_str_cca slot
341put smmu_str_cca slot 0
342r = smmu_map_span_wb sh wb 4096
343put smmu_str_cca slot cca
344if ne r SMMU_EATTR
345ret 51
346end
347uart_puts sq_wbmock
348if ne cca 1
349ret 52
350end
351for z ulong 32
352arm64_mmio_w64 add wb mul z 8 SQ_SENT
353end
354if ne smmu_map_span_wb sh wb 4096 SMMU_OK
355ret 53
356end
357if not sq_edu_dma SQ_EDU_BUF wb 1
358ret 54
359end
360if not sq_same wb 0x0123456789abcdef SQ_DMA_LEN
361ret 55
362end
363uart_puts sq_wbok
364uart_putdec SQ_DMA_LEN
365uart_puts sq_nl
366ret 0
367end
368
369note CMDQ in-flight limit (mode 1): 14 commands + SYNC is the maximum batch; a 15th non-SYNC command is
370note refused by smmu_cmd_push, the next SYNC fails closed (batch dropped), and the queue then recovers.
371fn sq_qlimit to ulong
372for k ulong 15
373smmu_cmd_push 0 SMMU_CMD_TLBI_NSNH_ALL 0
374end
375if ne smmu_cmd_sync 0 SMMU_ECMDERR
376ret 60
377end
378if ne smmu_cmd_sync 0 SMMU_OK
379ret 61
380end
381for j ulong 14
382smmu_cmd_push 0 SMMU_CMD_TLBI_NSNH_ALL 0
383end
384if ne smmu_cmd_sync 0 SMMU_OK
385ret 62
386end
387uart_puts sq_qlim
388ret 0
389end
390
391note Word 0 of the in-memory STE for sid on instance 0 (0 when no L2 table exists yet).
392fn sq_ste_w0 sid ulong to ulong
393d = arm64_mmio_r64 add get smmu_inst_strtab 0 mul shr sid SMMU_STRTAB_SPLIT 8
394if eq d 0
395ret 0
396end
397ret arm64_mmio_r64 add and d SMMU_L2PTR_MASK mul and sid 63 64
398end
399
400note Mode-1 failed open: 14 un-issued commands make the open's CFGI_STE over-limit, so its CMD_SYNC fails
401note (SMMU_ECMDERR) after the STE was already written to memory. The teardown must invalidate that STE,
402note leave the slot unused and the instance up, and a retry of the same RID must succeed.
403fn sq_failopen to ulong
404cd = dma_alloc_nc 1 4096
405tt = dma_alloc_nc 1 4096
406if or eq cd 0 eq tt 0
407ret 70
408end
409n0 = get smmu_str_n 0
410for k ulong 14
411smmu_cmd_push 0 SMMU_CMD_TLBI_NSNH_ALL 0
412end
413r = smmu_open_stream 0 0x0010 cd tt
414if ne r SMMU_ECMDERR
415sq_die 71 ulong sub 0 r
416end
417sid = get iort_qres 3
418if or ne get smmu_str_n 0 n0 ne get smmu_str_live n0 0
419ret 72
420end
421if ne sq_ste_w0 sid 0
422ret 73
423end
424if ne get smmu_inst_state 0 1
425ret 74
426end
427if le smmu_open_stream 0 0x0010 cd tt 0
428ret 75
429end
430if eq and sq_ste_w0 sid 1 0
431ret 76
432end
433uart_puts sq_fo
434ret 0
435end
436
437fn el1main el1_arg ulong to void
438uart_puts sq_hdr
439bmmio_reset
440pcie_reset
441smmu_reset
442put bmmio_mode 0 1
443if eq sq_mcfg 0
444sq_die 1 0
445end
446if not sq_find_edu
447sq_die 2 0
448end
449if lt pcie_quiesce_unused 0
450sq_die 3 0
451end
452if not sq_iort
453sq_die 4 get acpi_info 11
454end
455r = sq_smmu_up
456if ne r SMMU_OK
457sq_die 5 ulong r
458end
459uart_puts sq_up
460uart_puthex get sq_st 2
461uart_puts sq_cmds
462uart_putdec get smmu_cmd_count 0
463uart_puts sq_nl
464if not sq_stream
465sq_die 6 0
466end
467t = sq_dma_tests
468if ne t 0
469sq_die t 0
470end
471f = sq_check_fault
472if ne f 0
473sq_die f 0
474end
475w = sq_wb_tests
476if ne w 0
477sq_die w get iort_qres 8
478end
479q = sq_qlimit
480if ne q 0
481sq_die q 0
482end
483fo = sq_failopen
484if ne fo 0
485sq_die fo 0
486end
487uart_puts sq_pass
488sq_halt
489end
490
491fn main to int
492if not efi_exit_boot_services
493ret 1
494end
495arm64_daif_mask
496arm64_post_ebs_set UART_SPARK_16550_PA PSCI_CONDUIT_NONE
497outp = fwadr efi_mm_out 0 64
498rsdp = fwld add outp 40 8
499ok_acpi = acpi_parse_rsdp rsdp
500conduit = get acpi_info 7
501if ne get acpi_info 8 0
502uart_init get acpi_info 8 get acpi_info 9 conduit
503else
504uart_init UART_SPARK_16550_PA UART_KIND_16550_32 conduit
505end
506arm64_post_ebs_set uart_base conduit
507frames_init
508mp = fwadr efi_mmap 0 SQ_EFI_MMAP_CAP
509cnt = frames_ingest_efi_raw mp fwld outp 8 fwld add outp 16 8
510if eq cnt 0
511trap 8
512end
513ib = arm64_meta 0
514bss = arm64_meta 1
515ie = arm64_meta 2
516frames_reserve ib ie
517if ne rsdp 0
518frames_reserve rsdp add rsdp 4096
519end
520if not mmu_build_initial ib bss ie
521trap 8
522end
523e1 = arm64_meta 4
524if eq e1 0
525trap 8
526end
527mmu_enter_el1 e1 rsdp
528ret 0
529end