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 / crates / aienos-kernel / mm_iommu_test.om

2293 lines · 48573 bytes Raw Blame History
1note aienux/crates/aienos-kernel/mm_iommu_test.om
2note Comprehensive behavioral tests for mm_iommu.om covering all 9 subsystems
3note and all Rust unit tests with distinct non-zero exit codes on every failure.
4
5note ========================================================================
6note 1. Frame Allocator Tests (exit codes 1..15)
7note ========================================================================
8
9fn kmi_test_frame_allocator to int
10st = new ulong 16 0
11out_a = new ulong 1 0
12batch = new ulong 160 0
13
14note test_allocator_basic
15if ne kmi_fa_init st 0x10000000 256 4 0
16ret 1
17end
18if ne kmi_fa_total_frames st 256
19ret 2
20end
21if ne kmi_fa_free_count st 256
22ret 3
23end
24if not kmi_fa_alloc st out_a
25ret 4
26end
27if ne get out_a 0 0x10000000
28ret 4
29end
30if ne kmi_fa_allocated_count st 1
31ret 4
32end
33if not kmi_fa_alloc st out_a
34ret 5
35end
36if ne get out_a 0 0x10001000
37ret 5
38end
39if not kmi_fa_free st 0x10000000
40ret 6
41end
42if kmi_fa_free st 0x10000000
43ret 6
44end
45if ne kmi_fa_allocated_count st 1
46ret 6
47end
48if not kmi_fa_alloc st out_a
49ret 7
50end
51if ne get out_a 0 0x10000000
52ret 7
53end
54
55note test_alloc_contiguous
56if ne kmi_fa_init st 0x20000000 128 2 0
57ret 8
58end
59if not kmi_fa_alloc_contig st 4 out_a
60ret 8
61end
62if ne get out_a 0 0x20000000
63ret 8
64end
65if ne kmi_fa_allocated_count st 4
66ret 8
67end
68
69note allocator_rejects_unaligned_deallocation
70if ne kmi_fa_init st 0x20000000 64 1 0
71ret 9
72end
73if not kmi_fa_alloc st out_a
74ret 9
75end
76if kmi_fa_free st add get out_a 0 1
77ret 10
78end
79if ne kmi_fa_allocated_count st 1
80ret 10
81end
82if not kmi_fa_free st get out_a 0
83ret 10
84end
85
86note batch_reservation_is_bounded_and_released_as_a_unit
87if ne kmi_fa_init st 0x30000000 8 1 0
88ret 11
89end
90if ne kmi_fa_alloc_batch st 6 batch 6
91ret 11
92end
93if ne kmi_fa_free_count st 2
94ret 11
95end
96if not kmi_fa_release_batch st batch 6
97ret 12
98end
99if ne kmi_fa_free_count st 8
100ret 12
101end
102if ne kmi_fa_alloc_batch st 161 batch 0
103ret 12
104end
105
106note insufficient_batch_reservation_leaves_no_partial_allocations
107if ne kmi_fa_init st 0x40000000 8 1 0
108ret 13
109end
110if not kmi_fa_alloc st out_a
111ret 13
112end
113if ne kmi_fa_alloc_batch st 8 batch 0
114ret 13
115end
116if ne kmi_fa_allocated_count st 1
117ret 14
118end
119if ne kmi_fa_free_count st 7
120ret 14
121end
122
123note allocator_rejects_capacity_overflow and unaligned base
124if eq kmi_fa_init st 0x20000000 65 1 0
125ret 15
126end
127if eq kmi_fa_init st 0x20000001 8 1 0
128ret 15
129end
130ret 0
131end
132
133note ========================================================================
134note 2. Map Plan Tests (exit codes 16..35)
135note ========================================================================
136
137fn kmi_test_map_plan_regs to int
138m = kmi_mair_el1
139if ne and m 0xff 0xff
140ret 16
141end
142if ne and shr m 8 0xff 0x04
143ret 16
144end
145if ne m 0x04ff
146ret 16
147end
148tcr = kmi_tcr_el1 uchar 5
149if ne and tcr 0x3f 16
150ret 17
151end
152if ne and shr tcr 8 3 1
153ret 17
154end
155if ne and shr tcr 10 3 1
156ret 17
157end
158if ne and shr tcr 12 3 3
159ret 17
160end
161if ne and shr tcr 14 3 0
162ret 18
163end
164if ne and shr tcr 16 0x3f 16
165ret 18
166end
167if eq and tcr shl ulong 1 23 0
168ret 18
169end
170if ne and shr tcr 30 3 2
171ret 19
172end
173if ne and shr tcr 32 7 5
174ret 19
175end
176s = kmi_sctlr_el1
177if ne and s 0x30d00800 0x30d00800
178ret 20
179end
180mci = or ulong 1 or shl ulong 1 2 shl ulong 1 12
181if ne and s mci mci
182ret 20
183end
184bad_b = or shl ulong 1 1 or shl ulong 1 19 shl ulong 1 25
185if ne and s bad_b 0
186ret 21
187end
188ret 0
189end
190
191fn kmi_test_map_plan_rt_mmio to int
192meta = new ulong 4 0
193plan = new ulong 48 0
194rt_reg = new ulong 3 0
195rt_attrs = new ulong 8 0
196mmio = new ulong 4 0
197
198note missing_mat_maps_runtime_code_rx_without_wx
199kmi_plan_init meta 16
200put rt_reg 0 0x400000
201put rt_reg 1 4096
202put rt_reg 2 0
203if ne kmi_plan_add_runtime meta plan rt_reg rt_attrs KMI_PLAN_OK
204ret 28
205end
206if ne get meta 0 1
207ret 28
208end
209if ne get meta 1 KMI_RT_DECISION_RX_UNSPLIT
210ret 28
211end
212if eq and get plan 2 KMI_FLAG_EXECUTABLE 0
213ret 29
214end
215if ne and get plan 2 KMI_FLAG_WRITABLE 0
216ret 29
217end
218
219note runtime_code_needs_ro_xp_split
220kmi_plan_init meta 16
221put rt_reg 0 0x400000
222put rt_reg 1 8192
223put rt_reg 2 1
224put rt_attrs 0 0x400000
225put rt_attrs 1 4096
226put rt_attrs 2 1
227put rt_attrs 3 0
228put rt_attrs 4 0x401000
229put rt_attrs 5 4096
230put rt_attrs 6 0
231put rt_attrs 7 1
232if ne kmi_plan_add_runtime meta plan rt_reg rt_attrs KMI_PLAN_OK
233ret 30
234end
235if ne get meta 0 2
236ret 30
237end
238if eq and get plan 2 KMI_FLAG_EXECUTABLE 0
239ret 31
240end
241if ne and get plan 2 KMI_FLAG_WRITABLE 0
242ret 31
243end
244if eq and get plan 5 KMI_FLAG_WRITABLE 0
245ret 31
246end
247if ne and get plan 5 KMI_FLAG_EXECUTABLE 0
248ret 31
249end
250
251note writable_executable_runtime_code_is_mapped_rx
252kmi_plan_init meta 16
253put rt_reg 0 0x400000
254put rt_reg 1 4096
255put rt_reg 2 1
256put rt_attrs 0 0x400000
257put rt_attrs 1 4096
258put rt_attrs 2 0
259put rt_attrs 3 0
260if ne kmi_plan_add_runtime meta plan rt_reg rt_attrs KMI_PLAN_OK
261ret 32
262end
263if ne get meta 1 KMI_RT_DECISION_RX_UNSPLIT
264ret 32
265end
266if eq and get plan 2 KMI_FLAG_EXECUTABLE 0
267ret 33
268end
269if ne and get plan 2 KMI_FLAG_WRITABLE 0
270ret 33
271end
272
273note mmio_is_device_nx_and_overlap_rejected
274kmi_plan_init meta 16
275put mmio 0 0x90000000
276put mmio 1 4096
277if ne kmi_plan_add_mmio meta plan 0 part mmio 0 2 KMI_PLAN_OK
278ret 34
279end
280if eq and get plan 2 KMI_FLAG_DEVICE 0
281ret 34
282end
283if ne and get plan 2 KMI_FLAG_EXECUTABLE 0
284ret 34
285end
286kmi_plan_init meta 16
287put mmio 0 4096
288put mmio 1 8192
289put mmio 2 8192
290put mmio 3 4096
291if ne kmi_plan_add_mmio meta plan 0 mmio KMI_PLAN_OK
292ret 35
293end
294if ne kmi_plan_validate_overlaps meta plan KMI_PLAN_ERR_OVERLAP
295ret 35
296end
297ret 0
298end
299
300fn kmi_test_map_plan to int
301rc_r = kmi_test_map_plan_regs
302if ne rc_r 0
303ret rc_r
304end
305meta = new ulong 4 0
306plan = new ulong 48 0
307img_reg = new ulong 4 0
308secs = new ulong 6 0
309rx_ch = or ulong 0x40000000 KMI_SCN_MEM_EXECUTE
310rw_ch = or ulong 0x40000000 KMI_SCN_MEM_WRITE
311
312note section_permissions_are_wx_safe
313kmi_plan_init meta 16
314put img_reg 0 0x100000
315put img_reg 1 16384
316put img_reg 2 0x100000
317put img_reg 3 16384
318put secs 0 0x100000
319put secs 1 4096
320put secs 2 rx_ch
321put secs 3 0x101000
322put secs 4 4096
323put secs 5 rw_ch
324if ne kmi_plan_add_loader meta plan img_reg secs KMI_PLAN_OK
325ret 22
326end
327if ne kmi_plan_validate_overlaps meta plan KMI_PLAN_OK
328ret 22
329end
330if ne get meta 1 KMI_RT_DECISION_NONE
331ret 23
332end
333if ne get meta 0 2
334ret 23
335end
336if eq and get plan 2 KMI_FLAG_EXECUTABLE 0
337ret 24
338end
339if ne and get plan 2 KMI_FLAG_WRITABLE 0
340ret 24
341end
342if eq and get plan 5 KMI_FLAG_WRITABLE 0
343ret 25
344end
345if ne and get plan 5 KMI_FLAG_EXECUTABLE 0
346ret 25
347end
348
349note W+X section rejected as Overlap
350kmi_plan_init meta 16
351put secs 2 or KMI_SCN_MEM_EXECUTE KMI_SCN_MEM_WRITE
352if ne kmi_plan_add_loader meta plan img_reg secs KMI_PLAN_ERR_OVERLAP
353ret 26
354end
355
356note section outside allocation rejected
357kmi_plan_init meta 16
358put secs 0 0x500000
359put secs 2 rx_ch
360if ne kmi_plan_add_loader meta plan img_reg secs KMI_PLAN_ERR_SEC_OUTSIDE
361ret 27
362end
363ret kmi_test_map_plan_rt_mmio
364end
365
366note ========================================================================
367note 3. SMMUv3 Tests (exit codes 36..80)
368note ========================================================================
369
370fn kmi_test_smmu_desc_and_geom to int
371ste = new ulong 8 0
372cd = new ulong 8 0
373cur = new uint 2 0
374geom = new ulong 7 0
375
376note descriptor_layouts
377kmi_ste_abort ste
378if ne get ste 0 1
379ret 36
380end
381kmi_ste_stage1 0x123456780 ste
382if ne and get ste 0 1 1
383ret 37
384end
385if ne and shr get ste 0 1 7 5
386ret 37
387end
388if ne and shr get ste 0 4 3 0
389ret 37
390end
391if ne and get ste 0 0x000fffffffffffc0 0x123456780
392ret 38
393end
394for i ulong 7
395if ne get ste add i 1 0
396ret 38
397end
398end
399if ne kmi_cd_stage1 0x4000 ushort 0x1234 0xff00 uchar 16 cd KMI_SMMU_OK
400ret 39
401end
402exp0 = ulong 16
403exp0 = or exp0 shl ulong 1 8
404exp0 = or exp0 shl ulong 1 10
405exp0 = or exp0 shl ulong 3 12
406exp0 = or exp0 shl ulong 1 30
407exp0 = or exp0 shl ulong 1 31
408exp0 = or exp0 shl ulong 5 32
409exp0 = or exp0 shl ulong 1 41
410exp0 = or exp0 shl ulong 1 45
411exp0 = or exp0 shl ulong 1 46
412exp0 = or exp0 shl ulong 0x1234 48
413if ne get cd 0 exp0
414ret 40
415end
416if ne get cd 1 0x4000
417ret 40
418end
419if ne get cd 2 0
420ret 40
421end
422if ne get cd 3 0xff00
423ret 40
424end
425if ne kmi_cd_stage1 0x4001 ushort 1 0xff uchar 16 cd KMI_SMMU_ERR_INVALID_WINDOW
426ret 41
427end
428if ne kmi_cd_stage1 0x4000 ushort 1 0xff uchar 40 cd KMI_SMMU_ERR_INVALID_WINDOW
429ret 41
430end
431
432note command_words_and_queue_wrap
433if ne kmi_smmu_cmd_w0 KMI_CMD_CFGI_STE uint 7 or ulong 3 shl ulong 7 32
434ret 42
435end
436if ne kmi_smmu_cmd_w1 KMI_CMD_CFGI_STE true 1
437ret 42
438end
439if ne kmi_smmu_cmd_w0 KMI_CMD_CFGI_ALL uint 0 4
440ret 43
441end
442if ne kmi_smmu_cmd_w1 KMI_CMD_CFGI_ALL false 31
443ret 43
444end
445if ne kmi_smmu_cmd_w0 KMI_CMD_TLBI_NSNH_ALL uint 0 0x30
446ret 43
447end
448if ne kmi_smmu_cmd_w0 KMI_CMD_SYNC uint 0 0x46
449ret 43
450end
451put cur 0 uint 1
452put cur 1 uint 0
453kmi_qcursor_advance cur uint 2
454if ne get cur 0 0
455ret 44
456end
457if ne get cur 1 1
458ret 44
459end
460if ne kmi_qcursor_encode get cur 0 true uint 2 2
461ret 44
462end
463if ne kmi_qcursor_encode uint 5 true uint 16 0x15
464ret 45
465end
466if ne kmi_qcursor_decode_idx uint 0x15 uint 16 5
467ret 45
468end
469if not kmi_qcursor_decode_wrap uint 0x15 uint 16
470ret 45
471end
472if kmi_qcursor_decode_wrap uint 0x80000005 uint 16
473ret 45
474end
475
476note tables_reject_misaligned_or_bad_geometry
477put geom 0 0x100000
478put geom 1 16
479put geom 2 0x101000
480put geom 3 16
481put geom 4 0x102000
482put geom 5 16
483put geom 6 0x103000
484if not kmi_smmu_tables_valid geom
485ret 46
486end
487put geom 1 12
488if kmi_smmu_tables_valid geom
489ret 47
490end
491put geom 1 16
492put geom 3 1
493if kmi_smmu_tables_valid geom
494ret 47
495end
496put geom 3 16
497put geom 4 0x102100
498if kmi_smmu_tables_valid geom
499ret 48
500end
501put geom 4 0x102000
502put geom 6 0x103008
503if kmi_smmu_tables_valid geom
504ret 48
505end
506ret 0
507end
508
509fn kmi_test_smmu_bringup to int
510sm = new ulong 512 0
511cd = new ulong 8 0
512ste_exp = new ulong 8 0
513if ne kmi_cd_stage1 0x4000 ushort 1 0x00ff uchar 16 cd KMI_SMMU_OK
514ret 49
515end
516
517note bring_up_keeps_global_abort_through_smmuen
518kmi_smmu_init sm
519if ne kmi_smmu_configure_linear sm uint 3 cd 4 KMI_SMMU_OK
520ret 50
521end
522if ne get sm KMI_SM_FIRST_WR_REG ulong KMI_SMMU_GBPA
523ret 50
524end
525if ne get sm KMI_SM_ALL_ABORT 1
526ret 51
527end
528if ne get sm KMI_SM_LAST_REG ulong KMI_SMMU_CR0
529ret 51
530end
531if ne get sm KMI_SM_LAST_VAL 0x0d
532ret 51
533end
534if ne get sm KMI_SM_CR0ACK 0x0d
535ret 52
536end
537if not kmi_smmu_abort_in_effect sm
538ret 52
539end
540kmi_ste_stage1 get sm KMI_SM_CD_ADDR ste_exp
541for s ulong 16
542sb = add KMI_SM_STE_BASE mul s 8
543if eq s 3
544if ne get sm sb get ste_exp 0
545ret 53
546end
547else
548if ne get sm sb 1
549ret 53
550end
551end
552end
553if ne get sm KMI_SM_CONS_CNT 3
554ret 54
555end
556if ne get sm KMI_SM_STRTAB_CFG 4
557ret 54
558end
559
560note bring_up_cleans_tables_after_writing_before_programming
561gbpa_seq = get sm KMI_SM_FIRST_GBPA
562cr0_seq = get sm KMI_SM_FIRST_CR0
563str_seq = get sm KMI_SM_FIRST_STRTAB
564cs = get sm KMI_SM_CLEAN_STREAM
565cc = get sm KMI_SM_CLEAN_CD
566cm = get sm KMI_SM_CLEAN_CMD
567ie = get sm KMI_SM_INV_EV
568if le cs gbpa_seq
569ret 55
570end
571if ge cs cr0_seq
572ret 55
573end
574if ge cs str_seq
575ret 55
576end
577if ge cc cr0_seq
578ret 55
579end
580if ge cm cr0_seq
581ret 55
582end
583if ge ie cr0_seq
584ret 55
585end
586ret 0
587end
588
589fn kmi_test_smmu_failures to int
590sm = new ulong 512 0
591cd = new ulong 8 0
592if ne kmi_cd_stage1 0x4000 ushort 1 0x00ff uchar 16 cd KMI_SMMU_OK
593ret 56
594end
595
596note gbpa_update_timeout_fails_closed
597kmi_smmu_init sm
598put sm KMI_SM_GBPA_STUCK ulong 1
599if ne kmi_smmu_configure_linear sm uint 3 cd 4 KMI_SMMU_ERR_TIMEOUT
600ret 56
601end
602if eq and get sm KMI_SM_GBPA ulong KMI_GBPA_ABORT 0
603ret 57
604end
605if ne get sm KMI_SM_WR_CNT 1
606ret 57
607end
608
609note smmuen_ack_failure_fails_closed
610kmi_smmu_init sm
611put sm KMI_SM_ACK_NEVER ulong KMI_CR0_SMMUEN
612if ne kmi_smmu_configure_linear sm uint 3 cd 4 KMI_SMMU_ERR_TIMEOUT
613ret 58
614end
615if not kmi_smmu_abort_in_effect sm
616ret 58
617end
618if ne get sm KMI_SM_LAST_REG ulong KMI_SMMU_CR0
619ret 59
620end
621if ne get sm KMI_SM_LAST_VAL 0
622ret 59
623end
624if ne get sm KMI_SM_ALL_ABORT 1
625ret 59
626end
627
628note command_timeout_and_error_fail_closed
629kmi_smmu_init sm
630put sm KMI_SM_CMDQ_STUCK ulong 1
631if ne kmi_smmu_configure_linear sm uint 3 cd 4 KMI_SMMU_ERR_TIMEOUT
632ret 60
633end
634if ne get sm KMI_SM_SMMUEN_SEEN 0
635ret 60
636end
637if ne get sm KMI_SM_CR0 0
638ret 60
639end
640
641kmi_smmu_init sm
642put sm KMI_SM_CMDQ_ERROR ulong 1
643if ne kmi_smmu_configure_linear sm uint 3 cd 4 KMI_SMMU_ERR_CMDQ
644ret 61
645end
646if ne get sm KMI_SM_SMMUEN_SEEN 0
647ret 61
648end
649if ne get sm KMI_SM_CR0 0
650ret 61
651end
652
653note unsupported_hardware_or_bad_sid_fails_closed
654kmi_smmu_init sm
655kmi_smmu_reg_write sm KMI_SMMU_CR0 or KMI_CR0_SMMUEN KMI_CR0_CMDQEN
656kmi_smmu_reset_log sm
657put sm KMI_SM_IDR1 ulong 2
658if ne kmi_smmu_configure_linear sm uint 3 cd 4 KMI_SMMU_ERR_UNSUPPORTED
659ret 62
660end
661if ne get sm KMI_SM_CR0ACK 0
662ret 62
663end
664if ne get sm KMI_SM_STE_BASE 0xdead
665ret 62
666end
667
668kmi_smmu_init sm
669put sm KMI_SM_IDR0 ulong 0
670if ne kmi_smmu_configure_linear sm uint 3 cd 4 KMI_SMMU_ERR_UNSUPPORTED
671ret 63
672end
673
674kmi_smmu_init sm
675if ne kmi_smmu_configure_linear sm uint 16 cd 4 KMI_SMMU_ERR_INVALID_WINDOW
676ret 64
677end
678
679note gbpa_that_will_not_abort_is_an_error
680kmi_smmu_init sm
681put sm KMI_SM_GBPA_NO_ABORT ulong 1
682if ne kmi_smmu_configure_linear sm uint 3 cd 4 KMI_SMMU_ERR_ABORT_NOT_LATCHED
683ret 65
684end
685if ne get sm KMI_SM_WR_CNT 1
686ret 65
687end
688ret 0
689end
690
691fn kmi_test_smmu_ops_and_events to int
692sm = new ulong 512 0
693cd = new ulong 8 0
694ste = new ulong 8 0
695sync_cmds = new ulong 34 0
696ev_out = new ulong 6 0
697drain = new ulong 2 0
698wins = new ulong 3 0
699if ne kmi_cd_stage1 0x4000 ushort 1 0x00ff uchar 16 cd KMI_SMMU_OK
700ret 66
701end
702kmi_smmu_init sm
703if ne kmi_smmu_configure_linear sm uint 3 cd 4 KMI_SMMU_OK
704ret 66
705end
706
707note install_and_abort_ste_invalidate_that_stream
708kmi_smmu_reset_log sm
709kmi_ste_stage1 get sm KMI_SM_CD_ADDR ste
710if ne kmi_smmu_install_ste sm uint 7 ste 4 KMI_SMMU_OK
711ret 67
712end
713if ne get sm add KMI_SM_STE_BASE 56 get ste 0
714ret 67
715end
716if ne get sm KMI_SM_CONS_CNT 6
717ret 68
718end
719if ne kmi_smmu_abort_ste sm uint 7 4 KMI_SMMU_OK
720ret 69
721end
722if ne get sm add KMI_SM_STE_BASE 56 1
723ret 69
724end
725if ne kmi_smmu_install_ste sm uint 16 ste 4 KMI_SMMU_ERR_INVALID_WINDOW
726ret 70
727end
728if ne kmi_smmu_abort_ste sm uint 16 4 KMI_SMMU_ERR_INVALID_WINDOW
729ret 70
730end
731
732note command_queue_wraps_and_times_out
733kmi_smmu_init sm
734if ne kmi_smmu_configure_linear sm uint 3 cd 4 KMI_SMMU_OK
735ret 71
736end
737for i ulong 11
738if ne kmi_smmu_abort_ste sm uint 5 4 KMI_SMMU_OK
739ret 71
740end
741end
742if ne get sm KMI_SM_CONS_CNT 36
743ret 72
744end
745if ne get sm KMI_SM_CMDQ_PROD 4
746ret 72
747end
748for i ulong 4
749if ne kmi_smmu_abort_ste sm uint 5 4 KMI_SMMU_OK
750ret 73
751end
752end
753if ne get sm KMI_SM_CMDQ_PROD 0x10
754ret 73
755end
756put sm KMI_SM_CMDQ_STUCK ulong 1
757if ne kmi_smmu_abort_ste sm uint 5 4 KMI_SMMU_ERR_TIMEOUT
758ret 74
759end
760
761note submit_refuses_to_overwrite_a_full_queue
762kmi_smmu_init sm
763if ne kmi_smmu_configure_linear sm uint 3 cd 4 KMI_SMMU_OK
764ret 75
765end
766put sm KMI_SM_CMDQ_STUCK ulong 1
767for i ulong 17
768put sync_cmds mul i 2 kmi_smmu_cmd_w0 KMI_CMD_SYNC uint 0
769put sync_cmds add mul i 2 1 kmi_smmu_cmd_w1 KMI_CMD_SYNC false
770end
771if ne kmi_smmu_submit_cmds sm sync_cmds 17 4 KMI_SMMU_ERR_TIMEOUT
772ret 75
773end
774p_raw = uint and get sm KMI_SM_CMDQ_PROD 0xffffffff
775c_raw = uint and get sm KMI_SM_CMDQ_CONS 0xffffffff
776if ne kmi_qcursor_decode_idx p_raw uint 16 kmi_qcursor_decode_idx c_raw uint 16
777ret 76
778end
779if eq kmi_qcursor_decode_wrap p_raw uint 16 kmi_qcursor_decode_wrap c_raw uint 16
780ret 76
781end
782
783note event_reader_decodes_and_consumes_records
784kmi_smmu_init sm
785if ne kmi_smmu_configure_linear sm uint 3 cd 4 KMI_SMMU_OK
786ret 77
787end
788put sm add KMI_SM_EV_BASE 56 or ulong KMI_EVENT_F_TRANSLATION shl ulong 3 32
789put sm add KMI_SM_EV_BASE 58 ulong 0x12345000
790put sm add KMI_SM_EV_BASE 60 or ulong KMI_EVENT_C_BAD_STE shl ulong 9 32
791put sm add KMI_SM_EV_BASE 0 or ulong KMI_EVENT_F_PERMISSION shl ulong 3 32
792put sm add KMI_SM_EV_BASE 2 ulong 0xdeadb000
793put sm KMI_SM_EVENTQ_CONS ulong 14
794put sm KMI_SM_EVENTQ_PROD ulong 0x11
795kmi_smmu_read_events sm ev_out 2 drain
796if ne get drain 0 2
797ret 77
798end
799if ne get drain 1 0
800ret 77
801end
802if ne get ev_out 0 ulong KMI_EVENT_F_TRANSLATION
803ret 78
804end
805if ne get ev_out 1 3
806ret 78
807end
808if ne get ev_out 2 0x12345000
809ret 78
810end
811if ne get ev_out 3 ulong KMI_EVENT_C_BAD_STE
812ret 78
813end
814if ne get sm KMI_SM_EVENTQ_CONS 0x10
815ret 78
816end
817kmi_smmu_read_events sm ev_out 2 drain
818if ne get drain 0 1
819ret 79
820end
821if ne get ev_out 0 ulong KMI_EVENT_F_PERMISSION
822ret 79
823end
824if ne get ev_out 2 0xdeadb000
825ret 79
826end
827if ne get sm KMI_SM_EVENTQ_CONS 0x11
828ret 79
829end
830
831note event_reader_acknowledges_overflow
832kmi_smmu_init sm
833put sm KMI_SM_EVENTQ_PROD ulong KMI_EVENTQ_OVERFLOW
834kmi_smmu_read_events sm ev_out 1 drain
835if ne get drain 1 1
836ret 80
837end
838if ne get sm KMI_SM_EVENTQ_CONS ulong KMI_EVENTQ_OVERFLOW
839ret 80
840end
841kmi_smmu_read_events sm ev_out 1 drain
842if ne get drain 1 0
843ret 80
844end
845
846note policy_maps_only_allowed_windows
847put wins 0 0x1000
848put wins 1 0x1000
849put wins 2 4096
850if ne kmi_smmu_validate_policy_windows wins KMI_SMMU_OK
851ret 80
852end
853put wins 0 0x2000
854if ne kmi_smmu_validate_policy_windows wins KMI_SMMU_ERR_INVALID_WINDOW
855ret 80
856end
857ret 0
858end
859
860note ========================================================================
861note 4. DMA Gate Tests (exit codes 81..100)
862note ========================================================================
863
864fn kmi_test_dma_gate to int
865cfg = new uint 516 0
866sw = new ulong 40 0
867
868note command_bits
869if not kmi_bme_enabled ushort 0x0007
870ret 81
871end
872if kmi_bme_enabled ushort 0x0003
873ret 81
874end
875if ne kmi_without_bme ushort 0x0407 ushort 0x0403
876ret 82
877end
878if ne kmi_with_bme ushort 0x0400 ushort 0x0406
879ret 82
880end
881
882note grant_requires_smmu_or_explicit_bypass_without_smmu
883if ne kmi_dma_grant true true false KMI_DMA_GRANT_CONFINED
884ret 83
885end
886if ne kmi_dma_grant true true true KMI_DMA_GRANT_CONFINED
887ret 83
888end
889if ne kmi_dma_grant false false false KMI_DMA_DENIED_NO_SMMU
890ret 84
891end
892if ne kmi_dma_grant false false true KMI_DMA_GRANT_UNSAFE_BYPASS
893ret 84
894end
895
896note failed_smmu_never_falls_back_to_bypass
897if ne kmi_dma_grant false true false KMI_DMA_DENIED_SMMU_NOT_READY
898ret 85
899end
900if ne kmi_dma_grant false true true KMI_DMA_DENIED_SMMU_NOT_READY
901ret 85
902end
903
904note sweep_clears_endpoints_and_leaves_bridges
905kmi_pci_cfg_init cfg
906i0 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar 0 uchar 0 uint 0x00081b36 uchar 0
907kmi_pci_cfg_set_reg cfg ulong i0 1 uint 0x00100000
908i1 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar 1 uchar 0 uint 0x10421af4 uchar 0
909kmi_pci_cfg_set_reg cfg ulong i1 1 uint 0x00100007
910i2 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar 2 uchar 0 uint 0x000d1b36 uchar 0
911kmi_pci_cfg_set_reg cfg ulong i2 1 uint 0x00100002
912i3 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar 3 uchar 0 uint 0x000c1b36 uchar 1
913kmi_pci_cfg_set_reg cfg ulong i3 1 uint 0x00100007
914i4 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 1 uchar 0 uchar 0 uint 0x1234abcd uchar 0
915kmi_pci_cfg_set_reg cfg ulong i4 1 uint 0x00100006
916kmi_sweep_bus_master cfg uchar 0 uchar 1 sw
917if ne get sw 0 5
918ret 86
919end
920if ne get sw 1 1
921ret 86
922end
923if ne get sw 2 1
924ret 87
925end
926if ne get sw 3 2
927ret 87
928end
929if ne get sw 4 0
930ret 88
931end
932if ne get sw 8 ulong kmi_bdf_rid uchar 0 uchar 1 uchar 0
933ret 89
934end
935if ne get sw 9 0x0007
936ret 89
937end
938if ne get sw 10 0x0003
939ret 89
940end
941if ne get sw 12 ulong kmi_bdf_rid uchar 1 uchar 0 uchar 0
942ret 90
943end
944if ne and kmi_pci_cfg_get_reg cfg ulong i1 1 0xffff 0x0003
945ret 91
946end
947if ne and kmi_pci_cfg_get_reg cfg ulong i3 1 0xffff 0x0007
948ret 91
949end
950if ne and kmi_pci_cfg_get_reg cfg ulong i2 1 0xffff 0x0002
951ret 91
952end
953if ne get cfg 2 2
954ret 92
955end
956
957note sweep_reports_a_bus_master_bit_that_will_not_clear
958kmi_pci_cfg_init cfg
959i0 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar 4 uchar 0 uint 0x5678abcd uchar 0
960kmi_pci_cfg_set_reg cfg ulong i0 1 uint 0x00100006
961kmi_pci_cfg_set_stuck cfg ulong i0 true
962kmi_sweep_bus_master cfg uchar 0 uchar 0 sw
963if ne get sw 3 1
964ret 93
965end
966if ne get sw 4 1
967ret 93
968end
969if ne get sw 11 0
970ret 94
971end
972
973note sweep_walks_multifunction_devices_only_when_marked
974kmi_pci_cfg_init cfg
975i0 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar 5 uchar 0 uint 0x1111abcd uchar 0
976kmi_pci_cfg_set_reg cfg ulong i0 1 uint 0x0004
977i1 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar 5 uchar 1 uint 0x2222abcd uchar 0
978kmi_pci_cfg_set_reg cfg ulong i1 1 uint 0x0004
979i2 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar 6 uchar 0 uint 0x3333abcd uchar 0x80
980kmi_pci_cfg_set_reg cfg ulong i2 1 uint 0x0004
981i3 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar 6 uchar 1 uint 0x4444abcd uchar 0
982kmi_pci_cfg_set_reg cfg ulong i3 1 uint 0x0004
983kmi_sweep_bus_master cfg uchar 0 uchar 0 sw
984if ne get sw 0 3
985ret 95
986end
987if ne get sw 3 3
988ret 95
989end
990if ne and kmi_pci_cfg_get_reg cfg ulong i1 1 0xffff 0x0004
991ret 96
992end
993
994note sweep_keeps_counting_past_the_recorded_findings
995kmi_pci_cfg_init cfg
996for d ulong 12
997ix = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar and d 31 uchar 0 uint 0x9999abcd uchar 0
998kmi_pci_cfg_set_reg cfg ulong ix 1 uint 0x0004
999end
1000kmi_sweep_bus_master cfg uchar 0 uchar 0 sw
1001if ne get sw 3 12
1002ret 97
1003end
1004if ne get sw 4 0
1005ret 98
1006end
1007ret 0
1008end
1009
1010note ========================================================================
1011note 5. PCI Tests (exit codes 101..130)
1012note ========================================================================
1013
1014fn kmi_test_pci_bars to int
1015cfg = new uint 516 0
1016fi = new uint 6 0
1017bar = new ulong 4 0
1018sz = new ulong 2 0
1019
1020note decodes_32_and_64_bit_memory_bars_and_io_bars
1021if ne kmi_pci_decode_bar uint 0xfe000000 false uint 0 bar KMI_BAR_OK
1022ret 112
1023end
1024if ne get bar 0 KMI_BAR_MEM32
1025ret 112
1026end
1027if ne get bar 1 0xfe000000
1028ret 112
1029end
1030if ne get bar 2 0
1031ret 112
1032end
1033if ne kmi_pci_decode_bar uint 0x1000000c true uint 0x80 bar KMI_BAR_OK
1034ret 113
1035end
1036if ne get bar 0 KMI_BAR_MEM64
1037ret 113
1038end
1039if ne get bar 1 0x8010000000
1040ret 113
1041end
1042if ne get bar 2 1
1043ret 113
1044end
1045if ne kmi_pci_decode_bar uint 0xc001 false uint 0 bar KMI_BAR_OK
1046ret 114
1047end
1048if ne get bar 0 KMI_BAR_IO
1049ret 114
1050end
1051if ne get bar 1 0xc000
1052ret 114
1053end
1054if ne kmi_pci_decode_bar uint 0x4 false uint 0 bar KMI_BAR_ERR_MISSING_HIGH
1055ret 115
1056end
1057if ne kmi_pci_decode_bar uint 0x6 true uint 0 bar KMI_BAR_ERR_RESERVED
1058ret 115
1059end
1060if ne kmi_pci_decode_bar uint 0x2 false uint 0 bar KMI_BAR_ERR_RESERVED
1061ret 115
1062end
1063
1064note read_bar_respects_header_type_and_slots
1065kmi_pci_cfg_init cfg
1066rid2 = kmi_bdf_rid uchar 0 uchar 2 uchar 0
1067rid3 = kmi_bdf_rid uchar 0 uchar 3 uchar 0
1068i0 = kmi_pci_cfg_add cfg rid2 uint 0x11e81234 uchar 0
1069kmi_pci_cfg_set_reg cfg ulong i0 4 uint 0x10000000
1070kmi_pci_cfg_set_reg cfg ulong i0 5 uint 0x20000004
1071kmi_pci_cfg_set_reg cfg ulong i0 6 uint 0x00000001
1072kmi_pci_cfg_set_reg cfg ulong i0 9 uint 0x00000004
1073i1 = kmi_pci_cfg_add cfg rid3 uint 0x00011b36 uchar 1
1074kmi_pci_cfg_set_reg cfg ulong i1 4 uint 0x30000000
1075if not kmi_pci_read_fn cfg rid2 fi
1076ret 116
1077end
1078if ne kmi_pci_read_bar cfg fi uchar 0 bar KMI_BAR_OK
1079ret 116
1080end
1081if ne get bar 1 0x10000000
1082ret 116
1083end
1084if ne kmi_pci_read_bar cfg fi uchar 1 bar KMI_BAR_OK
1085ret 117
1086end
1087if ne get bar 3 1
1088ret 117
1089end
1090if ne get bar 1 0x120000000
1091ret 117
1092end
1093if ne kmi_pci_read_bar cfg fi uchar 5 bar KMI_BAR_ERR_MISSING_HIGH
1094ret 118
1095end
1096if ne kmi_pci_read_bar cfg fi uchar 6 bar KMI_BAR_ERR_NO_SUCH
1097ret 118
1098end
1099if not kmi_pci_read_fn cfg rid3 fi
1100ret 119
1101end
1102if ne kmi_pci_read_bar cfg fi uchar 0 bar KMI_BAR_OK
1103ret 119
1104end
1105if ne get bar 1 0x30000000
1106ret 119
1107end
1108if ne kmi_pci_read_bar cfg fi uchar 2 bar KMI_BAR_ERR_NO_SUCH
1109ret 119
1110end
1111
1112note probes_bar_sizes_and_restores_state
1113kmi_pci_cfg_init cfg
1114i0 = kmi_pci_cfg_add cfg rid2 uint 0x11e81234 uchar 0
1115kmi_pci_cfg_set_reg cfg ulong i0 1 uint or KMI_PCI_CMD_MEMORY KMI_PCI_CMD_BUS_MASTER
1116kmi_pci_cfg_set_reg cfg ulong i0 4 uint 0x10000000
1117kmi_pci_cfg_set_reg cfg ulong i0 5 uint 0x0000000c
1118kmi_pci_cfg_set_reg cfg ulong i0 6 uint 0x00000080
1119kmi_pci_cfg_set_bar_mask cfg ulong i0 0 uint 0xfff00000
1120kmi_pci_cfg_set_bar_mask cfg ulong i0 1 uint 0xffffc000
1121kmi_pci_cfg_set_bar_mask cfg ulong i0 2 uint 0xffffffff
1122kmi_pci_cfg_set_bar_mask cfg ulong i0 3 uint 0
1123if not kmi_pci_read_fn cfg rid2 fi
1124ret 120
1125end
1126if ne kmi_pci_probe_bar_size cfg fi uchar 0 sz KMI_BAR_OK
1127ret 120
1128end
1129if ne get sz 0 1
1130ret 120
1131end
1132if ne get sz 1 0x100000
1133ret 120
1134end
1135if ne kmi_pci_probe_bar_size cfg fi uchar 1 sz KMI_BAR_OK
1136ret 121
1137end
1138if ne get sz 0 1
1139ret 121
1140end
1141if ne get sz 1 16384
1142ret 121
1143end
1144if ne kmi_pci_probe_bar_size cfg fi uchar 3 sz KMI_BAR_OK
1145ret 122
1146end
1147if ne get sz 0 0
1148ret 122
1149end
1150if ne kmi_pci_cfg_get_reg cfg ulong i0 4 0x10000000
1151ret 123
1152end
1153if ne kmi_pci_cfg_get_reg cfg ulong i0 5 0x0000000c
1154ret 123
1155end
1156if ne kmi_pci_cfg_get_reg cfg ulong i0 6 0x00000080
1157ret 123
1158end
1159if ne and kmi_pci_cfg_get_reg cfg ulong i0 1 0xffff uint or KMI_PCI_CMD_MEMORY KMI_PCI_CMD_BUS_MASTER
1160ret 124
1161end
1162
1163note update_command_sets_and_clears_bits_without_touching_status
1164kmi_pci_cfg_init cfg
1165i0 = kmi_pci_cfg_add cfg rid2 uint 0x11e81234 uchar 0
1166st_bits = or shl uint 1 13 shl uint 1 4
1167kmi_pci_cfg_set_reg cfg ulong i0 1 or shl st_bits 16 shl uint 1 10
1168on_v = kmi_pci_update_cmd cfg rid2 or KMI_PCI_CMD_MEMORY KMI_PCI_CMD_BUS_MASTER ushort 0
1169if ne on_v or shl ushort 1 10 or KMI_PCI_CMD_MEMORY KMI_PCI_CMD_BUS_MASTER
1170ret 125
1171end
1172off_v = kmi_pci_update_cmd cfg rid2 ushort 0 KMI_PCI_CMD_BUS_MASTER
1173if ne off_v or shl ushort 1 10 KMI_PCI_CMD_MEMORY
1174ret 126
1175end
1176if ne shr kmi_pci_cfg_get_reg cfg ulong i0 1 16 st_bits
1177ret 127
1178end
1179ret 0
1180end
1181
1182fn kmi_test_pci to int
1183cfg = new uint 516 0
1184fi = new uint 6 0
1185found = new uint 60 0
1186
1187note requester_id_packs_bus_device_function
1188if ne kmi_bdf_rid uchar 0 uchar 0 uchar 0 0
1189ret 101
1190end
1191if ne kmi_bdf_rid uchar 0 uchar 2 uchar 0 0x10
1192ret 101
1193end
1194if ne kmi_bdf_rid uchar 1 uchar 3 uchar 5 0x11d
1195ret 102
1196end
1197if ne kmi_bdf_rid uchar 255 uchar 31 uchar 7 0xffff
1198ret 102
1199end
1200if kmi_bdf_valid uchar 32 uchar 0
1201ret 103
1202end
1203if kmi_bdf_valid uchar 0 uchar 8
1204ret 103
1205end
1206
1207note finds_device_by_vendor_and_device_id
1208kmi_pci_cfg_init cfg
1209i0 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar 0 uchar 0 uint 0x00081b36 uchar 0
1210i0 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar 1 uchar 0 uint 0x10001af4 uchar 0
1211i0 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar 3 uchar 0 uint 0x11e81234 uchar 0
1212kmi_pci_cfg_set_reg cfg ulong i0 2 uint 0x00ff0010
1213if not kmi_pci_find_device cfg uchar 0 uchar 0 uint 0x11e81234 fi
1214ret 104
1215end
1216if ne get fi 0 0x18
1217ret 105
1218end
1219if ne get fi 3 0
1220ret 105
1221end
1222if ne get fi 4 0
1223ret 105
1224end
1225if ne get fi 5 0x00ff0010
1226ret 105
1227end
1228if kmi_pci_find_device cfg uchar 0 uchar 0 uint 0x00011234 fi
1229ret 106
1230end
1231
1232note walk_skips_functions_1_to_7_unless_multi_function
1233kmi_pci_cfg_init cfg
1234i0 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar 1 uchar 0 uint 0x00011111 uchar 0
1235i0 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar 1 uchar 2 uint 0x00021111 uchar 0
1236i0 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar 4 uchar 0 uint 0x00012222 uchar 0x80
1237i0 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar 4 uchar 2 uint 0x00022222 uchar 0
1238i0 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar 4 uchar 7 uint 0x00032222 uchar 0
1239i0 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 0 uchar 6 uchar 1 uint 0x00013333 uchar 0
1240i0 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 2 uchar 0 uchar 0 uint 0x00014444 uchar 1
1241cnt = kmi_pci_walk cfg uchar 0 uchar 2 found
1242if ne cnt 5
1243ret 107
1244end
1245if ne get found 0 kmi_bdf_rid uchar 0 uchar 1 uchar 0
1246ret 108
1247end
1248if ne get found 6 kmi_bdf_rid uchar 0 uchar 4 uchar 0
1249ret 108
1250end
1251if ne get found 12 kmi_bdf_rid uchar 0 uchar 4 uchar 2
1252ret 108
1253end
1254if ne get found 18 kmi_bdf_rid uchar 0 uchar 4 uchar 7
1255ret 108
1256end
1257if ne get found 24 kmi_bdf_rid uchar 2 uchar 0 uchar 0
1258ret 108
1259end
1260if ne kmi_pci_walk cfg uchar 0 uchar 1 found 4
1261ret 109
1262end
1263
1264note walk_covers_last_bus_and_device_without_overflow
1265kmi_pci_cfg_init cfg
1266i0 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 255 uchar 31 uchar 0 uint 0x00015555 uchar 0x80
1267i0 = kmi_pci_cfg_add cfg kmi_bdf_rid uchar 255 uchar 31 uchar 7 uint 0x00025555 uchar 0
1268if ne kmi_pci_walk cfg uchar 255 uchar 255 found 2
1269ret 110
1270end
1271put cfg 1 uint 0
1272if ne kmi_pci_walk cfg uchar 5 uchar 4 found 0
1273ret 111
1274end
1275if ne get cfg 1 0
1276ret 111
1277end
1278ret kmi_test_pci_bars
1279end
1280
1281note ========================================================================
1282note 6. Device Registry and Authority Tests (exit codes 131..195)
1283note ========================================================================
1284
1285fn kmi_setup_std_device dtab edit ulong 128 to void
1286spec = new ulong 8 0
1287kmi_dtab_init dtab 4
1288put spec 0 0x100
1289put spec 1 3
1290put spec 2 0x10
1291put spec 3 0x10
1292put spec 4 KMI_DMA_QUIESCED
1293put spec 5 0
1294put spec 6 0
1295put spec 7 0
1296ign = kmi_dtab_register dtab spec
1297ign2 = kmi_dtab_set_window dtab 0x100 0x40000000 uint 4
1298end
1299
1300fn kmi_dev_state dtab view ulong 128 res ulong to ulong
1301idx = kmi_dtab_find dtab res
1302if lt idx 0
1303ret 99
1304end
1305off = add 4 mul ulong idx 12
1306ret get dtab add off 5
1307end
1308
1309fn kmi_check_teardown_effects hw view ulong 128 start_idx ulong to bool
1310if lt get hw 0 add start_idx 5
1311ret false
1312end
1313o0 = add 8 mul start_idx 3
1314if ne get hw o0 KMI_EFF_STOP_WORK
1315ret false
1316end
1317if ne get hw add o0 3 KMI_EFF_BME_OFF
1318ret false
1319end
1320if ne get hw add o0 6 KMI_EFF_QUIESCE
1321ret false
1322end
1323if ne get hw add o0 9 KMI_EFF_ABORT
1324ret false
1325end
1326if ne get hw add o0 12 KMI_EFF_INVALIDATE
1327ret false
1328end
1329ret true
1330end
1331
1332fn kmi_test_device_1 to int
1333c0 = new ulong 64 0
1334c1 = new ulong 64 0
1335dtab = new ulong 128 0
1336hw = new ulong 128 0
1337h0 = new ulong 1 0
1338h1 = new ulong 1 0
1339res_b = new ulong 1 0
1340req = new ulong 3 0
1341
1342note activate_installs_translation_before_bus_master
1343kmi_cap_init c0 1 4
1344ign = kmi_cap_insert c0 0x100 KMI_RIGHT_MAP h0
1345kmi_setup_std_device dtab
1346kmi_hw_init hw KMI_HW_NONE
1347if ne kmi_dev_activate c0 get h0 0 dtab hw KMI_DEV_OK
1348ret 131
1349end
1350if ne get hw 0 2
1351ret 132
1352end
1353if ne get hw 8 KMI_EFF_INSTALL
1354ret 132
1355end
1356if ne get hw 11 KMI_EFF_BME_ON
1357ret 132
1358end
1359if ne kmi_dev_state dtab 0x100 KMI_DMA_ACTIVE
1360ret 133
1361end
1362
1363note revoke_runs_exact_teardown_then_removes_capability_tree
1364kmi_cap_init c0 1 4
1365kmi_cap_init c1 2 4
1366ign = kmi_cap_insert c0 0x100 KMI_RIGHT_ALL h0
1367if ne kmi_cap_derive_into c0 get h0 0 c1 KMI_RIGHT_MAP h1 KMI_DEV_OK
1368ret 134
1369end
1370kmi_setup_std_device dtab
1371kmi_hw_init hw KMI_HW_NONE
1372if ne kmi_dev_activate c1 get h1 0 dtab hw KMI_DEV_OK
1373ret 134
1374end
1375kmi_hw_clear_effects hw
1376put req 0 1
1377put req 1 get h0 0
1378put req 2 1
1379if ne kmi_dev_revoke req c0 c1 dtab hw KMI_DEV_OK
1380ret 135
1381end
1382if not kmi_check_teardown_effects hw 0
1383ret 136
1384end
1385if ne kmi_dev_state dtab 0x100 KMI_DMA_QUIESCED
1386ret 137
1387end
1388if ne kmi_cap_lookup c0 get h0 0 KMI_RIGHT_NONE res_b KMI_DEV_ERR_CAP_INVALID
1389ret 138
1390end
1391if ne kmi_cap_lookup c1 get h1 0 KMI_RIGHT_NONE res_b KMI_DEV_ERR_CAP_INVALID
1392ret 138
1393end
1394
1395note activate_with_revoked_handle_fails_without_effects
1396kmi_hw_clear_effects hw
1397if ne kmi_dev_activate c0 get h0 0 dtab hw KMI_DEV_ERR_CAP_INVALID
1398ret 139
1399end
1400if ne get hw 0 0
1401ret 139
1402end
1403
1404note missing_capability_causes_no_effects
1405kmi_cap_init c0 1 4
1406kmi_setup_std_device dtab
1407kmi_hw_init hw KMI_HW_NONE
1408bogus = kmi_cap_pack_handle 0 1
1409if ne kmi_dev_activate c0 bogus dtab hw KMI_DEV_ERR_CAP_INVALID
1410ret 140
1411end
1412put req 0 1
1413put req 1 bogus
1414put req 2 0
1415if ne kmi_dev_revoke req c0 c1 dtab hw KMI_DEV_ERR_CAP_INVALID
1416ret 140
1417end
1418if ne get hw 0 0
1419ret 141
1420end
1421
1422note read_only_capability_causes_no_effects
1423kmi_cap_init c0 1 4
1424ign = kmi_cap_insert c0 0x100 KMI_RIGHT_READ h0
1425if ne kmi_dev_activate c0 get h0 0 dtab hw KMI_DEV_ERR_CAP_RIGHTS
1426ret 142
1427end
1428put req 1 get h0 0
1429if ne kmi_dev_revoke req c0 c1 dtab hw KMI_DEV_ERR_CAP_RIGHTS
1430ret 142
1431end
1432if ne get hw 0 0
1433ret 143
1434end
1435
1436note map_without_revoke_cannot_tear_down_an_active_device
1437kmi_cap_init c0 1 4
1438ign = kmi_cap_insert c0 0x100 KMI_RIGHT_MAP h0
1439kmi_setup_std_device dtab
1440kmi_hw_init hw KMI_HW_NONE
1441if ne kmi_dev_activate c0 get h0 0 dtab hw KMI_DEV_OK
1442ret 144
1443end
1444kmi_hw_clear_effects hw
1445put req 1 get h0 0
1446if ne kmi_dev_revoke req c0 c1 dtab hw KMI_DEV_ERR_CAP_RIGHTS
1447ret 144
1448end
1449if ne get hw 0 0
1450ret 145
1451end
1452if ne kmi_dev_state dtab 0x100 KMI_DMA_ACTIVE
1453ret 145
1454end
1455
1456note capability_for_other_resource_is_not_a_device
1457kmi_cap_init c0 1 4
1458ign = kmi_cap_insert c0 1 KMI_RIGHT_ALL h0
1459kmi_setup_std_device dtab
1460kmi_hw_init hw KMI_HW_NONE
1461if ne kmi_dev_activate c0 get h0 0 dtab hw KMI_DEV_ERR_NOT_DEVICE
1462ret 146
1463end
1464put req 1 get h0 0
1465if ne kmi_dev_revoke req c0 c1 dtab hw KMI_DEV_ERR_NOT_DEVICE
1466ret 146
1467end
1468if ne get hw 0 0
1469ret 147
1470end
1471
1472note activation_refused_without_window_or_when_already_active
1473kmi_cap_init c0 1 4
1474ign = kmi_cap_insert c0 0x100 KMI_RIGHT_MAP h0
1475spec = new ulong 8 0
1476kmi_dtab_init dtab 4
1477put spec 0 0x100
1478put spec 1 3
1479put spec 2 0x10
1480put spec 3 0x10
1481put spec 4 KMI_DMA_QUIESCED
1482ign2 = kmi_dtab_register dtab spec
1483kmi_hw_init hw KMI_HW_NONE
1484if ne kmi_dev_activate c0 get h0 0 dtab hw KMI_DEV_ERR_NO_WINDOW
1485ret 148
1486end
1487if ne get hw 0 0
1488ret 148
1489end
1490if ne kmi_dtab_set_window dtab 0x100 0x40000000 uint 4 KMI_DEV_OK
1491ret 149
1492end
1493if ne kmi_dev_activate c0 get h0 0 dtab hw KMI_DEV_OK
1494ret 149
1495end
1496kmi_hw_clear_effects hw
1497if ne kmi_dev_activate c0 get h0 0 dtab hw KMI_DEV_ERR_WRONG_STATE
1498ret 150
1499end
1500if ne kmi_dtab_set_window dtab 0x100 0x40000000 uint 4 KMI_DEV_ERR_WRONG_STATE
1501ret 150
1502end
1503if ne get hw 0 0
1504ret 150
1505end
1506ret 0
1507end
1508
1509fn kmi_test_device_reg_sweep to int
1510c0 = new ulong 64 0
1511c1 = new ulong 64 0
1512dtab = new ulong 128 0
1513hw = new ulong 128 0
1514h0 = new ulong 1 0
1515h1 = new ulong 1 0
1516spec = new ulong 8 0
1517
1518note register_rejects_clashes_and_live_devices and window_validation
1519kmi_dtab_init dtab 2
1520put spec 0 0x100
1521put spec 1 3
1522put spec 2 0x10
1523put spec 3 0x10
1524put spec 4 KMI_DMA_QUIESCED
1525put spec 5 0
1526if ne kmi_dtab_register dtab spec KMI_DEV_OK
1527ret 172
1528end
1529put spec 1 9
1530put spec 2 1
1531put spec 3 99
1532if ne kmi_dtab_register dtab spec KMI_DEV_ERR_DUPLICATE
1533ret 172
1534end
1535put spec 0 7
1536put spec 1 3
1537if ne kmi_dtab_register dtab spec KMI_DEV_ERR_DUPLICATE
1538ret 173
1539end
1540put spec 1 9
1541put spec 2 0x10
1542if ne kmi_dtab_register dtab spec KMI_DEV_ERR_DUPLICATE
1543ret 173
1544end
1545put spec 2 1
1546put spec 3 0x10
1547if ne kmi_dtab_register dtab spec KMI_DEV_ERR_DUPLICATE
1548ret 173
1549end
1550put spec 3 99
1551put spec 5 1
1552put spec 6 1
1553put spec 7 1
1554if ne kmi_dtab_register dtab spec KMI_DEV_ERR_BAD_WINDOW
1555ret 174
1556end
1557put spec 5 0
1558put spec 4 KMI_DMA_ACTIVE
1559if ne kmi_dtab_register dtab spec KMI_DEV_ERR_WRONG_STATE
1560ret 174
1561end
1562put spec 4 KMI_DMA_QUIESCED
1563if ne kmi_dtab_register dtab spec KMI_DEV_OK
1564ret 175
1565end
1566put spec 0 8
1567put spec 1 10
1568put spec 2 2
1569put spec 3 100
1570if ne kmi_dtab_register dtab spec KMI_DEV_ERR_FULL
1571ret 175
1572end
1573if ne kmi_dtab_set_window dtab 0x100 0x1000 uint 0 KMI_DEV_ERR_BAD_WINDOW
1574ret 176
1575end
1576if ne kmi_dtab_set_window dtab 0x100 0x1001 uint 1 KMI_DEV_ERR_BAD_WINDOW
1577ret 176
1578end
1579if ne kmi_dtab_set_window dtab 0x100 0xfffffffffffff000 uint 2 KMI_DEV_ERR_BAD_WINDOW
1580ret 176
1581end
1582if ne kmi_dtab_set_window dtab 0x999 0x40000000 uint 4 KMI_DEV_ERR_NOT_DEVICE
1583ret 177
1584end
1585
1586note sweep_leaves_live_alone, tears_down_ancestor_revoked, removed_reused, table_gone
1587kmi_cap_init c0 1 4
1588kmi_cap_init c1 2 4
1589ign = kmi_cap_insert c0 0x100 KMI_RIGHT_ALL h0
1590ign_i = kmi_cap_derive_into c0 get h0 0 c1 KMI_RIGHT_MAP h1
1591kmi_setup_std_device dtab
1592kmi_hw_init hw KMI_HW_NONE
1593ign_i = kmi_dev_activate c1 get h1 0 dtab hw
1594kmi_hw_clear_effects hw
1595if ne kmi_dtab_sweep dtab 2 c0 c1 hw KMI_DEV_OK
1596ret 178
1597end
1598if ne get hw 0 0
1599ret 178
1600end
1601if ne kmi_dev_state dtab 0x100 KMI_DMA_ACTIVE
1602ret 178
1603end
1604ign_i = kmi_cap_revoke2 1 get h0 0 c0 c1 true
1605if ne kmi_dtab_sweep dtab 2 c0 c1 hw KMI_DEV_OK
1606ret 179
1607end
1608if not kmi_check_teardown_effects hw 0
1609ret 179
1610end
1611if ne kmi_dev_state dtab 0x100 KMI_DMA_QUIESCED
1612ret 179
1613end
1614
1615note slot reuse generation check + table gone
1616kmi_cap_init c0 1 1
1617ign = kmi_cap_insert c0 0x100 KMI_RIGHT_MAP h0
1618kmi_setup_std_device dtab
1619kmi_hw_init hw KMI_HW_NONE
1620ign_i = kmi_dev_activate c0 get h0 0 dtab hw
1621ign = kmi_cap_remove c0 get h0 0
1622ign = kmi_cap_insert c0 0x100 KMI_RIGHT_MAP h1
1623kmi_hw_clear_effects hw
1624if ne kmi_dtab_sweep dtab 1 c0 c1 hw KMI_DEV_OK
1625ret 180
1626end
1627if not kmi_check_teardown_effects hw 0
1628ret 180
1629end
1630if ne kmi_dev_state dtab 0x100 KMI_DMA_QUIESCED
1631ret 180
1632end
1633ign_i = kmi_dtab_set_window dtab 0x100 0x40000000 uint 4
1634ign_i = kmi_dev_activate c0 get h1 0 dtab hw
1635kmi_hw_clear_effects hw
1636if ne kmi_dtab_sweep dtab 0 c0 c1 hw KMI_DEV_OK
1637ret 181
1638end
1639if ne kmi_dev_state dtab 0x100 KMI_DMA_QUIESCED
1640ret 181
1641end
1642
1643note state_discriminants_are_stable
1644if ne KMI_DMA_QUIESCED 0
1645ret 182
1646end
1647if ne KMI_DMA_TRANSLATION_INSTALLED 1
1648ret 182
1649end
1650if ne KMI_DMA_ACTIVE 2
1651ret 182
1652end
1653if ne KMI_DMA_REVOKING 3
1654ret 182
1655end
1656ret 0
1657end
1658
1659fn kmi_test_device_2 to int
1660c0 = new ulong 64 0
1661c1 = new ulong 64 0
1662dtab = new ulong 128 0
1663hw = new ulong 128 0
1664h0 = new ulong 1 0
1665h1 = new ulong 1 0
1666res_b = new ulong 1 0
1667req = new ulong 3 0
1668
1669note activation_failure_at_each_step
1670for stp ulong 2
1671fstep = add stp 1
1672kmi_cap_init c0 1 4
1673ign = kmi_cap_insert c0 0x100 KMI_RIGHT_MAP h0
1674kmi_setup_std_device dtab
1675kmi_hw_init hw fstep
1676if ne kmi_dev_activate c0 get h0 0 dtab hw KMI_DEV_ERR_HARDWARE
1677ret 161
1678end
1679if ne get hw 4 fstep
1680ret 161
1681end
1682if ne get hw 2 0
1683ret 162
1684end
1685if ne get hw 3 1
1686ret 162
1687end
1688if ne kmi_dev_state dtab 0x100 KMI_DMA_QUIESCED
1689ret 162
1690end
1691end
1692
1693note failed_teardown_blocks_activation_until_a_clean_retry
1694kmi_cap_init c0 1 4
1695ign = kmi_cap_insert c0 0x100 KMI_RIGHT_ALL h0
1696kmi_setup_std_device dtab
1697kmi_hw_init hw KMI_HW_QUIESCE
1698if ne kmi_dev_activate c0 get h0 0 dtab hw KMI_DEV_OK
1699ret 163
1700end
1701put req 0 1
1702put req 1 get h0 0
1703put req 2 0
1704if ne kmi_dev_revoke req c0 c1 dtab hw KMI_DEV_ERR_HARDWARE
1705ret 164
1706end
1707if ne kmi_dev_state dtab 0x100 KMI_DMA_REVOKING
1708ret 164
1709end
1710ign = kmi_cap_insert c0 0x100 KMI_RIGHT_MAP h1
1711kmi_hw_clear_effects hw
1712if ne kmi_dev_activate c0 get h1 0 dtab hw KMI_DEV_ERR_WRONG_STATE
1713ret 165
1714end
1715if ne get hw 0 0
1716ret 165
1717end
1718put hw 1 KMI_HW_NONE
1719if ne kmi_dtab_force_quiesce dtab 0x100 hw KMI_DEV_OK
1720ret 166
1721end
1722if not kmi_check_teardown_effects hw 0
1723ret 166
1724end
1725if ne kmi_dev_state dtab 0x100 KMI_DMA_QUIESCED
1726ret 166
1727end
1728
1729note revoke_failure_at_each_step_still_closes_gates_and_revokes
1730for s ulong 4
1731fstep = add s KMI_HW_DISABLE_BME
1732kmi_cap_init c0 1 4
1733kmi_cap_init c1 2 4
1734ign = kmi_cap_insert c0 0x100 KMI_RIGHT_ALL h0
1735ign_i = kmi_cap_derive_into c0 get h0 0 c1 KMI_RIGHT_MAP h1
1736kmi_setup_std_device dtab
1737kmi_hw_init hw KMI_HW_NONE
1738ign_i = kmi_dev_activate c1 get h1 0 dtab hw
1739kmi_hw_clear_effects hw
1740put hw 1 fstep
1741put req 0 1
1742put req 1 get h0 0
1743put req 2 1
1744if ne kmi_dev_revoke req c0 c1 dtab hw KMI_DEV_ERR_HARDWARE
1745ret 167
1746end
1747if ne get hw 4 fstep
1748ret 167
1749end
1750if not kmi_check_teardown_effects hw 0
1751ret 168
1752end
1753if ne kmi_dev_state dtab 0x100 KMI_DMA_REVOKING
1754ret 168
1755end
1756if ne kmi_cap_lookup c0 get h0 0 KMI_RIGHT_NONE res_b KMI_DEV_ERR_CAP_INVALID
1757ret 169
1758end
1759if ne kmi_cap_lookup c1 get h1 0 KMI_RIGHT_NONE res_b KMI_DEV_ERR_CAP_INVALID
1760ret 169
1761end
1762end
1763
1764note duplicate_or_missing_table_in_revoke_set
1765kmi_cap_init c0 1 4
1766kmi_cap_init c1 2 4
1767ign = kmi_cap_insert c0 0x100 KMI_RIGHT_ALL h0
1768kmi_setup_std_device dtab
1769kmi_hw_init hw KMI_HW_NONE
1770put req 0 1
1771put req 1 get h0 0
1772put req 2 0
1773if ne kmi_dev_revoke req c1 c0 dtab hw KMI_DEV_ERR_CAP_DUP_TABLE
1774ret 170
1775end
1776kmi_cap_init c1 1 4
1777put req 2 1
1778if ne kmi_dev_revoke req c0 c1 dtab hw KMI_DEV_ERR_CAP_DUP_TABLE
1779ret 170
1780end
1781if ne get hw 0 0
1782ret 171
1783end
1784ret kmi_test_device_reg_sweep
1785end
1786
1787note ========================================================================
1788note 7. EDU DMA Tests (exit codes 196..215)
1789note ========================================================================
1790
1791fn kmi_test_edu to int
1792st = new ulong 24 0
1793dev_buf = new uchar 4096 0
1794ram_buf = new uchar 16384 0
1795
1796note probe_reads_version_and_checks_liveness
1797kmi_edu_init st
1798if ne kmi_edu_probe st KMI_EDU_OK
1799ret 196
1800end
1801if ne get st 18 1
1802ret 196
1803end
1804if ne get st 19 0
1805ret 196
1806end
1807put st 0 ulong 0x1af41000
1808if ne kmi_edu_probe st KMI_EDU_ERR_NOT_EDU
1809ret 197
1810end
1811kmi_edu_init st
1812put st 16 ulong 0
1813if ne kmi_edu_probe st KMI_EDU_ERR_LIVENESS
1814ret 198
1815end
1816
1817note round_trips_bytes_through_the_device_buffer
1818kmi_edu_init st
1819for i ulong 200
1820put ram_buf add 0x100 i uchar and xor i 0xa5 255
1821end
1822src_a = add get st 9 0x100
1823dst_a = add get st 9 0x2000
1824if ne kmi_edu_transfer st dev_buf ram_buf src_a 200 false KMI_EDU_OK
1825ret 199
1826end
1827if ne kmi_edu_transfer st dev_buf ram_buf dst_a 200 true KMI_EDU_OK
1828ret 199
1829end
1830for i ulong 200
1831if ne get ram_buf add 0x2000 i get ram_buf add 0x100 i
1832ret 200
1833end
1834end
1835if ne get st 15 2
1836ret 201
1837end
1838if ne get st 3 KMI_EDU_DMA_BUF_ADDR
1839ret 201
1840end
1841if ne get st 4 dst_a
1842ret 201
1843end
1844if ne get st 6 KMI_EDU_DMA_TO_RAM
1845ret 201
1846end
1847if ne get st 2 0
1848ret 201
1849end
1850
1851note full_buffer_transfer_is_allowed
1852kmi_edu_init st
1853at_a = get st 9
1854if ne kmi_edu_transfer st dev_buf ram_buf at_a KMI_EDU_DMA_BUF_SIZE false KMI_EDU_OK
1855ret 202
1856end
1857if ne kmi_edu_transfer st dev_buf ram_buf add at_a KMI_EDU_DMA_BUF_SIZE KMI_EDU_DMA_BUF_SIZE true KMI_EDU_OK
1858ret 202
1859end
1860
1861note blocked_transfer_completes_but_leaves_ram_untouched
1862kmi_edu_init st
1863for i ulong 64
1864put dev_buf i uchar 0x77
1865put ram_buf add 0x3000 i uchar 0
1866end
1867forb = add get st 9 0x3000
1868put st 10 forb
1869put st 11 add forb 0x1000
1870if ne kmi_edu_transfer st dev_buf ram_buf forb 64 true KMI_EDU_OK
1871ret 203
1872end
1873for i ulong 64
1874if ne get ram_buf add 0x3000 i 0
1875ret 204
1876end
1877end
1878
1879note rejects_bad_lengths_and_addresses_before_touching_the_device
1880kmi_edu_init st
1881top = add KMI_EDU_DEFAULT_DMA_MASK 1
1882if ne kmi_edu_transfer st dev_buf ram_buf 0x1000 0 false KMI_EDU_ERR_EMPTY
1883ret 205
1884end
1885if ne kmi_edu_transfer st dev_buf ram_buf 0x1000 4097 false KMI_EDU_ERR_TOO_LONG
1886ret 205
1887end
1888if ne kmi_edu_transfer st dev_buf ram_buf top 16 true KMI_EDU_ERR_IOVA_RANGE
1889ret 206
1890end
1891if ne kmi_edu_transfer st dev_buf ram_buf sub top 8 16 true KMI_EDU_ERR_IOVA_RANGE
1892ret 206
1893end
1894if ne kmi_edu_transfer st dev_buf ram_buf sub ulong 0xffffffffffffffff 3 16 true KMI_EDU_ERR_IOVA_RANGE
1895ret 206
1896end
1897if ne get st 3 0
1898ret 207
1899end
1900if ne get st 6 0
1901ret 207
1902end
1903
1904note last_byte_below_the_mask_is_accepted
1905kmi_edu_init st
1906put st 9 sub top 0x2000
1907if ne kmi_edu_transfer st dev_buf ram_buf sub top 16 16 true KMI_EDU_OK
1908ret 208
1909end
1910
1911note wider_dma_mask_accepts_higher_addresses
1912kmi_edu_init st
1913put st 7 0xffffffffffffffff
1914put st 17 0xffffffffffffffff
1915put st 9 0x80000000
1916if ne kmi_edu_transfer st dev_buf ram_buf 0x80000000 16 true KMI_EDU_OK
1917ret 209
1918end
1919
1920note times_out_when_the_transfer_never_finishes
1921kmi_edu_init st
1922put st 12 ulong 0
1923put st 8 ulong 100
1924at_a = get st 9
1925if ne kmi_edu_transfer st dev_buf ram_buf at_a 16 false KMI_EDU_ERR_TIMEOUT
1926ret 210
1927end
1928if ne kmi_edu_transfer st dev_buf ram_buf at_a 16 true KMI_EDU_ERR_BUSY
1929ret 211
1930end
1931if ne get st 6 KMI_EDU_DMA_START
1932ret 211
1933end
1934if ne get st 15 0
1935ret 211
1936end
1937
1938note spin_budget_covers_a_slow_transfer
1939kmi_edu_init st
1940put st 13 ulong 1000
1941put st 8 ulong 1001
1942if ne kmi_edu_transfer st dev_buf ram_buf get st 9 16 false KMI_EDU_OK
1943ret 212
1944end
1945kmi_edu_init st
1946put st 13 ulong 1000
1947put st 8 ulong 1000
1948if ne kmi_edu_transfer st dev_buf ram_buf get st 9 16 false KMI_EDU_ERR_TIMEOUT
1949ret 213
1950end
1951
1952note interrupt_status_and_acknowledge
1953kmi_edu_init st
1954put st 2 ulong or KMI_EDU_IRQ_BIT 1
1955kmi_edu_irq_ack st KMI_EDU_IRQ_BIT
1956if ne get st 2 1
1957ret 214
1958end
1959ret 0
1960end
1961
1962note ========================================================================
1963note 8. Block RamDisk Tests (exit codes 216..230)
1964note ========================================================================
1965
1966fn kmi_test_block to int
1967st = new ulong 12 0
1968disk = new uchar 2048 0
1969wbuf = new uchar 512 0
1970rbuf = new uchar 512 0
1971bad_buf = new uchar 100 0
1972
1973note ram_disk_rejects_block_size_that_would_truncate
1974if ne kmi_ramdisk_init st 0x100000000 4 2048 KMI_BLK_INVALID_INPUT
1975ret 216
1976end
1977if ne kmi_ramdisk_init st 32 4 2048 KMI_BLK_INVALID_INPUT
1978ret 216
1979end
1980if ne kmi_ramdisk_init st 512 3 2048 KMI_BLK_INVALID_INPUT
1981ret 216
1982end
1983if ne kmi_ramdisk_init st 512 4 2048 KMI_BLK_OK
1984ret 217
1985end
1986
1987note read/write round-trip and validation
1988for i ulong 512
1989put wbuf i uchar and add i 17 255
1990end
1991if ne kmi_ramdisk_write st disk 1 wbuf KMI_BLK_OK
1992ret 218
1993end
1994if ne kmi_ramdisk_read st disk 1 rbuf KMI_BLK_OK
1995ret 218
1996end
1997for i ulong 512
1998if ne get rbuf i get wbuf i
1999ret 219
2000end
2001end
2002if ne kmi_ramdisk_read st disk 0 bad_buf KMI_BLK_INVALID_INPUT
2003ret 220
2004end
2005if ne kmi_ramdisk_read st disk 4 rbuf KMI_BLK_OUT_OF_RANGE
2006ret 220
2007end
2008
2009note fail_after_blocks torn write
2010kmi_ramdisk_fail_after st true 0
2011put wbuf 0 uchar 0xee
2012if ne kmi_ramdisk_write st disk 2 wbuf KMI_BLK_DEVICE_ERROR
2013ret 221
2014end
2015if ne get disk 1024 0
2016ret 221
2017end
2018
2019note tear_write persists prefix and marks failed
2020kmi_ramdisk_tear_write st 1 16
2021for i ulong 512
2022put wbuf i uchar 0xaa
2023end
2024if ne kmi_ramdisk_write st disk 2 wbuf KMI_BLK_DEVICE_ERROR
2025ret 222
2026end
2027if ne get disk 1024 0xaa
2028ret 223
2029end
2030if ne get disk 1039 0xaa
2031ret 223
2032end
2033if ne get disk 1040 0
2034ret 223
2035end
2036if ne kmi_ramdisk_write st disk 0 wbuf KMI_BLK_DEVICE_ERROR
2037ret 224
2038end
2039
2040note corrupt_byte
2041if ne kmi_ramdisk_corrupt st disk 5 uchar 0x55 KMI_BLK_OK
2042ret 225
2043end
2044if ne get disk 5 0x55
2045ret 225
2046end
2047if ne kmi_ramdisk_corrupt st disk 2048 uchar 0x55 KMI_BLK_OUT_OF_RANGE
2048ret 226
2049end
2050ret 0
2051end
2052
2053note ========================================================================
2054note 9. NVMe Atomicity Tests (exit codes 231..250)
2055note ========================================================================
2056
2057fn kmi_set_af af edit ulong 8 bs ulong has_aw ulong aw ulong has_naw ulong naw ulong to void
2058put af 0 bs
2059put af 1 has_aw
2060put af 2 aw
2061put af 3 has_naw
2062put af 4 naw
2063put af 5 ulong 0
2064put af 6 ulong 0
2065put af 7 ulong 0
2066end
2067
2068fn kmi_test_nvme_atomicity to int
2069af = new ulong 8 0
2070info = new ulong 4 0
2071lba = new ulong 2 0
2072raw = new uchar 3 0
2073u16_o = new ushort 1 0
2074
2075note insufficient_controller_awupf_rejects
2076kmi_set_af af 512 1 6 0 0
2077if ne kmi_nvme_write_atomic af 0 8 info KMI_ATOM_EXCEEDS
2078ret 231
2079end
2080if ne get info 1 8
2081ret 231
2082end
2083if ne get info 2 7
2084ret 231
2085end
2086
2087note sufficient_decoded_awupf_accepts
2088kmi_set_af af 512 1 7 0 0
2089if ne kmi_nvme_write_atomic af 0 8 info KMI_ATOM_ATOMIC
2090ret 232
2091end
2092if ne get info 0 8
2093ret 232
2094end
2095
2096note namespace_override_wins_over_controller
2097kmi_set_af af 512 1 15 1 3
2098if ne kmi_nvme_effective_pf af 4
2099ret 233
2100end
2101if ne kmi_nvme_write_atomic af 0 8 info KMI_ATOM_EXCEEDS
2102ret 233
2103end
2104if ne get info 2 4
2105ret 233
2106end
2107
2108note namespace_zero_falls_back_to_controller
2109kmi_set_af af 512 1 7 1 0
2110if ne kmi_nvme_effective_pf af 8
2111ret 234
2112end
2113if ne kmi_nvme_write_atomic af 0 8 info KMI_ATOM_ATOMIC
2114ret 234
2115end
2116
2117note sufficient_unit_but_crosses_boundary_rejects
2118kmi_set_af af 512 1 15 0 0
2119put af 5 ulong 1
2120put af 6 ulong 3
2121if ne kmi_nvme_write_atomic af 0 8 info KMI_ATOM_CROSSES
2122ret 235
2123end
2124if ne get info 0 4
2125ret 235
2126end
2127if ne kmi_nvme_write_atomic af 0 4 info KMI_ATOM_ATOMIC
2128ret 236
2129end
2130
2131note boundary_safe_write_accepts
2132kmi_set_af af 512 1 7 0 0
2133if ne kmi_nvme_write_atomic af 8 8 info KMI_ATOM_ATOMIC
2134ret 237
2135end
2136
2137note boundary_offset_is_respected
2138kmi_set_af af 512 1 15 0 0
2139put af 5 ulong 1
2140put af 6 ulong 7
2141put af 7 ulong 4
2142if ne kmi_nvme_write_atomic af 0 8 info KMI_ATOM_CROSSES
2143ret 238
2144end
2145if ne kmi_nvme_write_atomic af 4 8 info KMI_ATOM_ATOMIC
2146ret 239
2147end
2148
2149note store_root_512_geometry
2150kmi_set_af af 512 1 7 0 0
2151if not kmi_nvme_store_unit_lba af uint 0 lba
2152ret 240
2153end
2154if ne get lba 0 0
2155ret 240
2156end
2157if ne get lba 1 8
2158ret 240
2159end
2160if not kmi_nvme_store_unit_lba af uint 1 lba
2161ret 241
2162end
2163if ne get lba 0 8
2164ret 241
2165end
2166if ne get lba 1 8
2167ret 241
2168end
2169if ne kmi_nvme_store_root_decision af uint 0 info KMI_ATOM_ATOMIC
2170ret 242
2171end
2172if ne kmi_nvme_store_root_decision af uint 1 info KMI_ATOM_ATOMIC
2173ret 242
2174end
2175
2176note store_root_4096_geometry
2177kmi_set_af af 4096 1 0 0 0
2178if not kmi_nvme_store_unit_lba af uint 0 lba
2179ret 243
2180end
2181if ne get lba 1 1
2182ret 243
2183end
2184if ne kmi_nvme_store_root_decision af uint 1 info KMI_ATOM_ATOMIC
2185ret 243
2186end
2187
2188note encoding_edge_zero_and_max
2189kmi_set_af af 512 1 0 0 0
2190if ne kmi_nvme_effective_pf af 1
2191ret 244
2192end
2193kmi_set_af af 512 1 65535 0 0
2194if ne kmi_nvme_effective_pf af 65536
2195ret 244
2196end
2197
2198note unknown_state_fails_closed
2199kmi_set_af af 512 0 0 0 0
2200if ne kmi_nvme_write_atomic af 0 8 info KMI_ATOM_NO_GUARANTEE
2201ret 245
2202end
2203
2204note unsupported_geometry_fails_closed
2205kmi_set_af af 600 1 15 0 0
2206if ne kmi_nvme_store_root_decision af uint 0 info KMI_ATOM_BAD_GEOMETRY
2207ret 246
2208end
2209
2210note empty_write_rejects
2211kmi_set_af af 512 1 15 0 0
2212if ne kmi_nvme_write_atomic af 0 0 info KMI_ATOM_EMPTY
2213ret 247
2214end
2215
2216note read_u16_le_bounds
2217put raw 0 uchar 7
2218put raw 1 uchar 0
2219put raw 2 uchar 255
2220if not kmi_read_u16_le raw 0 u16_o
2221ret 248
2222end
2223if ne get u16_o 0 7
2224ret 248
2225end
2226if kmi_read_u16_le raw 2 u16_o
2227ret 249
2228end
2229if kmi_read_u16_le raw 99 u16_o
2230ret 249
2231end
2232ret 0
2233end
2234
2235note ========================================================================
2236note Main Test Runner
2237note ========================================================================
2238
2239fn main to int
2240rc = kmi_test_frame_allocator
2241if ne rc 0
2242ret rc
2243end
2244rc = kmi_test_map_plan
2245if ne rc 0
2246ret rc
2247end
2248rc = kmi_test_smmu_desc_and_geom
2249if ne rc 0
2250ret rc
2251end
2252rc = kmi_test_smmu_bringup
2253if ne rc 0
2254ret rc
2255end
2256rc = kmi_test_smmu_failures
2257if ne rc 0
2258ret rc
2259end
2260rc = kmi_test_smmu_ops_and_events
2261if ne rc 0
2262ret rc
2263end
2264rc = kmi_test_dma_gate
2265if ne rc 0
2266ret rc
2267end
2268rc = kmi_test_pci
2269if ne rc 0
2270ret rc
2271end
2272rc = kmi_test_device_1
2273if ne rc 0
2274ret rc
2275end
2276rc = kmi_test_device_2
2277if ne rc 0
2278ret rc
2279end
2280rc = kmi_test_edu
2281if ne rc 0
2282ret rc
2283end
2284rc = kmi_test_block
2285if ne rc 0
2286ret rc
2287end
2288rc = kmi_test_nvme_atomicity
2289if ne rc 0
2290ret rc
2291end
2292ret 0
2293end