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 / core / test.om

677 lines · 16287 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/kernel/core/test.om: Pure-Omega test suite for Aienux Kernel Core
3note (Scheduler, Capability-Gated IPC, and AArch64 Stage-1 Page Tables).
4note Compose with: si/directives/om2.om aienux/kernel/core/scheduler.om aienux/kernel/core/ipc.om aienux/kernel/core/pagetable.om aienux/kernel/core/test.om
5
6text s_pass "PASS "
7text s_fail "FAIL "
8text s_nl "\n"
9text s_t01 "sched_madt_class_placement"
10text s_t02 "sched_fair_equal_priority_and_priority_order"
11text s_t03 "sched_background_anti_starvation"
12text s_t04 "sched_work_stealing_leaves_high_prio"
13text s_t05 "sched_remove_and_reuse_slot"
14text s_t06 "sched_negative_controls"
15text s_t07 "ipc_channel_fifo_and_bounds"
16text s_t08 "ipc_capability_rights_and_wrong_resource"
17text s_t09 "ipc_cross_principal_isolation"
18text s_t10 "ipc_object_read_write_and_oob"
19text s_t11 "ipc_attenuated_delegation_and_no_escalation"
20text s_t12 "ipc_tree_revocation_cascades"
21text s_t13 "pt_map_4k_2m_1g_and_translate"
22text s_t14 "pt_high_half_canonical_kernel_va"
23text s_t15 "pt_wx_and_device_exec_refused"
24text s_t16 "pt_pxn_uxn_and_flag_roundtrip"
25text s_t17 "pt_overlap_and_partial_unmap_refused"
26text s_t18 "pt_unmap_and_frame_exhaustion"
27text s_sum "KERNEL_CORE_TEST_PASS total=18 failed=0\n"
28text s_fsum "KERNEL_CORE_TEST_FAIL\n"
29
30fn kwrite_all fd int buf view uchar * to bool
31total = len buf
32off = ulong 0
33note Each write advances off by at least 1 byte on progress; 4096 covers test output lines.
34while lt off total bound 4096
35n = write fd buf off sub total off
36if le n 0
37ret false
38end
39off = add off ulong n
40end
41ret true
42end
43
44fn report name view uchar * ok bool to uint
45if ok
46w1 = kwrite_all 1 s_pass
47w2 = kwrite_all 1 name
48w3 = kwrite_all 1 s_nl
49if and w1 and w2 w3
50ret 0
51end
52ret 1
53end
54w1 = kwrite_all 1 s_fail
55w2 = kwrite_all 1 name
56w3 = kwrite_all 1 s_nl
57if and w1 and w2 w3
58ret 1
59end
60ret 1
61end
62
63note ---- Scheduler Tests (01..06) ----
64
65fn test_01_sched_placement to bool
66cls = new uchar 4 0
67put cls 0 1
68put cls 1 3
69put cls 2 1
70put cls 3 3
71if ne sched_init 4 8 cls SCHED_OK
72ret false
73end
74cpu = new ulong 1 0
75note Latency must land on class 3 (CPU 1 or 3)
76if ne sched_enqueue 10 SCHED_PRIO_LATENCY cpu SCHED_OK
77ret false
78end
79if ne get cls get cpu 0 3
80ret false
81end
82note Background must land on class 1 (CPU 0 or 2)
83if ne sched_enqueue 20 SCHED_PRIO_BG cpu SCHED_OK
84ret false
85end
86if ne get cls get cpu 0 1
87ret false
88end
89note Normal lands on shortest queue (length 0)
90if ne sched_enqueue 30 SCHED_PRIO_NORMAL cpu SCHED_OK
91ret false
92end
93if ne sched_queue_len get cpu 0 1
94ret false
95end
96ret true
97end
98
99fn test_02_sched_fairness to bool
100cls = new uchar 1 2
101if ne sched_init 1 8 cls SCHED_OK
102ret false
103end
104cpu = new ulong 1 0
105if ne sched_enqueue 1 SCHED_PRIO_NORMAL cpu SCHED_OK
106ret false
107end
108if ne sched_enqueue 2 SCHED_PRIO_NORMAL cpu SCHED_OK
109ret false
110end
111if ne sched_enqueue 3 SCHED_PRIO_LATENCY cpu SCHED_OK
112ret false
113end
114ran = new uchar 1 0
115tid = new uint 1 0
116note Latency task 3 always beats Normal tasks 1 & 2
117if ne sched_tick 0 ran tid SCHED_OK
118ret false
119end
120if or eq get ran 0 0 ne get tid 0 3
121ret false
122end
123note Remove latency task 3 -> tasks 1 and 2 share 6 ticks evenly (3 each)
124if ne sched_remove 3 SCHED_OK
125ret false
126end
127i = uint 0
128while lt i 6 bound 8
129if ne sched_tick 0 ran tid SCHED_OK
130ret false
131end
132i = add i 1
133end
134if ne sched_task_ticks 1 3
135ret false
136end
137if ne sched_task_ticks 2 3
138ret false
139end
140ret true
141end
142
143fn test_03_sched_bg_anti_starvation to bool
144cls = new uchar 1 2
145if ne sched_init 1 4 cls SCHED_OK
146ret false
147end
148cpu = new ulong 1 0
149if ne sched_enqueue 100 SCHED_PRIO_LATENCY cpu SCHED_OK
150ret false
151end
152if ne sched_enqueue 200 SCHED_PRIO_BG cpu SCHED_OK
153ret false
154end
155ran = new uchar 1 0
156tid = new uint 1 0
157note 8 contested ticks with interval 4 -> BG must get exactly 2 ticks, Latency 6 ticks
158i = uint 0
159while lt i 8 bound 16
160if ne sched_tick 0 ran tid SCHED_OK
161ret false
162end
163i = add i 1
164end
165if ne sched_task_ticks 200 2
166ret false
167end
168if ne sched_task_ticks 100 6
169ret false
170end
171ret true
172end
173
174fn test_04_sched_work_stealing to bool
175cls = new uchar 2 0
176put cls 0 3
177put cls 1 1
178if ne sched_init 2 8 cls SCHED_OK
179ret false
180end
181cpu = new ulong 1 0
182note Put two Latency tasks on CPU 0 (class 3), leaving CPU 1 idle
183if ne sched_enqueue 10 SCHED_PRIO_LATENCY cpu SCHED_OK
184ret false
185end
186if ne sched_enqueue 11 SCHED_PRIO_LATENCY cpu SCHED_OK
187ret false
188end
189ran = new uchar 1 0
190tid = new uint 1 0
191note Tick CPU 0 once so task 10 has 1 tick, task 11 has 0 ticks
192if ne sched_tick 0 ran tid SCHED_OK
193ret false
194end
195note Tick idle CPU 1 -> must steal most-served task (10) from CPU 0
196if ne sched_tick 1 ran tid SCHED_OK
197ret false
198end
199if or eq get ran 0 0 ne get tid 0 10
200ret false
201end
202if or ne sched_queue_len 0 1 ne sched_queue_len 1 1
203ret false
204end
205ret true
206end
207
208fn test_05_sched_remove_reuse to bool
209cls = new uchar 2 2
210if ne sched_init 2 8 cls SCHED_OK
211ret false
212end
213cpu = new ulong 1 0
214if ne sched_enqueue 77 SCHED_PRIO_NORMAL cpu SCHED_OK
215ret false
216end
217if ne sched_remove 77 SCHED_OK
218ret false
219end
220note Re-enqueueing same ID after removal succeeds
221if ne sched_enqueue 77 SCHED_PRIO_NORMAL cpu SCHED_OK
222ret false
223end
224ret true
225end
226
227fn test_06_sched_negatives to bool
228cls = new uchar 2 1
229if ne sched_init 0 8 cls SCHED_ERR_NO_CPUS
230ret false
231end
232if ne sched_init 2 8 cls SCHED_OK
233ret false
234end
235cpu = new ulong 1 0
236if ne sched_enqueue 5 SCHED_PRIO_NORMAL cpu SCHED_OK
237ret false
238end
239note Duplicate task ID refused
240if ne sched_enqueue 5 SCHED_PRIO_NORMAL cpu SCHED_ERR_DUP_TASK
241ret false
242end
243note Invalid CPU refused
244ran = new uchar 1 0
245tid = new uint 1 0
246if ne sched_tick 2 ran tid SCHED_ERR_BAD_CPU
247ret false
248end
249note Unknown task remove refused
250if ne sched_remove 999 SCHED_ERR_UNKNOWN
251ret false
252end
253note Fill remaining 63 slots -> 65th enqueue returns SCHED_ERR_FULL
254i = uint 1
255while lt i 64 bound 64
256if ne sched_enqueue add 100 i SCHED_PRIO_NORMAL cpu SCHED_OK
257ret false
258end
259i = add i 1
260end
261if ne sched_enqueue 999 SCHED_PRIO_NORMAL cpu SCHED_ERR_FULL
262ret false
263end
264ret true
265end
266
267note ---- IPC Tests (07..12) ----
268
269fn test_07_ipc_fifo_bounds to bool
270hs = new ulong 1 0
271ho = new ulong 1 0
272hr = new ulong 1 0
273ipc_reset hs ho hr
274msg = new IpcMsg 0 0 0 0
275note Empty recv refused
276if ne ipc_channel_recv IPC_PRINCIPAL_B get hr 0 msg IPC_ERR_CH_EMPTY
277ret false
278end
279note Fill 8 messages, 9th returns IPC_ERR_CH_FULL
280i = ulong 0
281while lt i 8 bound 16
282put msg tag uint add 10 i
283put msg sender IPC_PRINCIPAL_A
284put msg arg0 mul i 100
285put msg arg1 add i 1
286if ne ipc_channel_send IPC_PRINCIPAL_A get hs 0 msg IPC_OK
287ret false
288end
289i = add i 1
290end
291if ne ipc_channel_send IPC_PRINCIPAL_A get hs 0 msg IPC_ERR_CH_FULL
292ret false
293end
294note Receive in exact FIFO order
295i = ulong 0
296while lt i 8 bound 16
297if ne ipc_channel_recv IPC_PRINCIPAL_B get hr 0 msg IPC_OK
298ret false
299end
300if ne get msg tag uint add 10 i
301ret false
302end
303if ne get msg arg0 mul i 100
304ret false
305end
306i = add i 1
307end
308ret true
309end
310
311fn test_08_ipc_rights_and_resource to bool
312hs = new ulong 1 0
313ho = new ulong 1 0
314hr = new ulong 1 0
315ipc_reset hs ho hr
316msg = new IpcMsg 1 0 11 22
317note Principal A has WRITE-only channel handle -> recv refused
318if ne ipc_channel_recv IPC_PRINCIPAL_A get hs 0 msg IPC_ERR_MISSING_RIGHTS
319ret false
320end
321note Principal B has READ-only channel handle -> send refused
322if ne ipc_channel_send IPC_PRINCIPAL_B get hr 0 msg IPC_ERR_MISSING_RIGHTS
323ret false
324end
325note Object handle has READ|WRITE but wrong resource for channel ops
326if ne ipc_channel_send IPC_PRINCIPAL_A get ho 0 msg IPC_ERR_WRONG_RESOURCE
327ret false
328end
329if ne ipc_channel_recv IPC_PRINCIPAL_A get ho 0 msg IPC_ERR_WRONG_RESOURCE
330ret false
331end
332ret true
333end
334
335fn test_09_ipc_principal_isolation to bool
336hs = new ulong 1 0
337ho = new ulong 1 0
338hr = new ulong 1 0
339ipc_reset hs ho hr
340val = new ulong 1 0
341note Principal B cannot use Principal A's object handle
342if ne ipc_object_read IPC_PRINCIPAL_B get ho 0 0 val IPC_ERR_INVALID_HANDLE
343ret false
344end
345note Forged generation handle refused
346bad_h = xor get ho 0 0x100
347if ne ipc_object_read IPC_PRINCIPAL_A bad_h 0 val IPC_ERR_INVALID_HANDLE
348ret false
349end
350ret true
351end
352
353fn test_10_ipc_object_rw_oob to bool
354hs = new ulong 1 0
355ho = new ulong 1 0
356hr = new ulong 1 0
357ipc_reset hs ho hr
358val = new ulong 1 0
359if ne ipc_object_read IPC_PRINCIPAL_A get ho 0 0 val IPC_OK
360ret false
361end
362if ne get val 0 0xc0ffee01
363ret false
364end
365if ne ipc_object_write IPC_PRINCIPAL_A get ho 0 2 0xdeadbeef12345678 IPC_OK
366ret false
367end
368if ne ipc_object_read IPC_PRINCIPAL_A get ho 0 2 val IPC_OK
369ret false
370end
371if ne get val 0 0xdeadbeef12345678
372ret false
373end
374note Out-of-bounds object offset refused
375if ne ipc_object_read IPC_PRINCIPAL_A get ho 0 4 val IPC_ERR_WRONG_RESOURCE
376ret false
377end
378if ne ipc_object_write IPC_PRINCIPAL_A get ho 0 4 0 IPC_ERR_WRONG_RESOURCE
379ret false
380end
381ret true
382end
383
384fn test_11_ipc_delegation_attenuation to bool
385hs = new ulong 1 0
386ho = new ulong 1 0
387hr = new ulong 1 0
388ipc_reset hs ho hr
389hb = new ulong 1 0
390note Attempt to delegate DERIVE right refused
391if ne ipc_delegate_object IPC_PRINCIPAL_A get ho 0 IPC_PRINCIPAL_B or IPC_RIGHT_READ IPC_RIGHT_DERIVE hb IPC_ERR_MISSING_RIGHTS
392ret false
393end
394note Attenuated READ-only delegation succeeds
395if ne ipc_delegate_object IPC_PRINCIPAL_A get ho 0 IPC_PRINCIPAL_B IPC_RIGHT_READ hb IPC_OK
396ret false
397end
398val = new ulong 1 0
399if ne ipc_object_read IPC_PRINCIPAL_B get hb 0 1 val IPC_OK
400ret false
401end
402if ne get val 0 0x0badc0de
403ret false
404end
405note B cannot write with READ-only delegated handle
406if ne ipc_object_write IPC_PRINCIPAL_B get hb 0 1 99 IPC_ERR_MISSING_RIGHTS
407ret false
408end
409ret true
410end
411
412fn test_12_ipc_tree_revocation to bool
413hs = new ulong 1 0
414ho = new ulong 1 0
415hr = new ulong 1 0
416ipc_reset hs ho hr
417hb = new ulong 1 0
418if ne ipc_delegate_object IPC_PRINCIPAL_A get ho 0 IPC_PRINCIPAL_B IPC_RIGHT_READ hb IPC_OK
419ret false
420end
421val = new ulong 1 0
422if ne ipc_object_read IPC_PRINCIPAL_B get hb 0 0 val IPC_OK
423ret false
424end
425note Revoking Principal A's parent handle cascades to Principal B's derived handle
426if ne ipc_revoke IPC_PRINCIPAL_A get ho 0 IPC_OK
427ret false
428end
429if ne ipc_object_read IPC_PRINCIPAL_A get ho 0 0 val IPC_ERR_INVALID_HANDLE
430ret false
431end
432if ne ipc_object_read IPC_PRINCIPAL_B get hb 0 0 val IPC_ERR_INVALID_HANDLE
433ret false
434end
435ret true
436end
437
438note ---- AArch64 Stage-1 PageTable Tests (13..18) ----
439
440fn test_13_pt_4k_2m_1g_translate to bool
441if ne pt_init 0x40000000 32 PT_OK
442ret false
443end
444f = new PtFlags PT_MAIR_NORMAL_WB 1 0 0 1 1
445note 1 GiB block at 0x40000000 -> 0x80000000
446if ne pt_map 0x40000000 0x80000000 PT_BLOCK_1G f PT_OK
447ret false
448end
449note 2 MiB block at 0x00200000 -> 0x10200000
450if ne pt_map 0x00200000 0x10200000 PT_BLOCK_2M f PT_OK
451ret false
452end
453note 4 KiB page at 0x00001000 -> 0x20001000
454if ne pt_map 0x00001000 0x20001000 PT_PAGE_SIZE f PT_OK
455ret false
456end
457tr = new PtTrans 0 0 0 0 0 0 0 0
458if ne pt_translate 0x40000123 tr PT_OK
459ret false
460end
461if or ne get tr pa 0x80000123 ne get tr page_size PT_BLOCK_1G
462ret false
463end
464if ne pt_translate 0x00200456 tr PT_OK
465ret false
466end
467if or ne get tr pa 0x10200456 ne get tr page_size PT_BLOCK_2M
468ret false
469end
470if ne pt_translate 0x00001789 tr PT_OK
471ret false
472end
473if or ne get tr pa 0x20001789 ne get tr page_size PT_PAGE_SIZE
474ret false
475end
476ret true
477end
478
479fn test_14_pt_high_half_canonical to bool
480if ne pt_init 0x40000000 32 PT_OK
481ret false
482end
483f = new PtFlags PT_MAIR_NORMAL_WB 0 0 1 1 1
484note 48-bit canonical high-half kernel VA has bit 47 == 1 and bits 63..48 == 0xffff
485hi_va = ulong 0xffff800040000000
486if ne pt_map hi_va 0x50000000 PT_PAGE_SIZE f PT_OK
487ret false
488end
489tr = new PtTrans 0 0 0 0 0 0 0 0
490if ne pt_translate add hi_va 0x44 tr PT_OK
491ret false
492end
493if ne get tr pa 0x50000044
494ret false
495end
496note Non-canonical VAs (both low-hole and high-hole where bit 47 != bits 63..48) refused
497non_can1 = ulong 0x0001000000000000
498non_can2 = ulong 0xffff000040000000
499if ne pt_map non_can1 0x50001000 PT_PAGE_SIZE f PT_ERR_ADDR_OOB
500ret false
501end
502if ne pt_map non_can2 0x50001000 PT_PAGE_SIZE f PT_ERR_ADDR_OOB
503ret false
504end
505if ne pt_translate non_can1 tr PT_ERR_ADDR_OOB
506ret false
507end
508ret true
509end
510
511fn test_15_pt_wx_and_device_exec to bool
512if ne pt_init 0x40000000 32 PT_OK
513ret false
514end
515note W^X: writable + executable MUST be refused
516wx = new PtFlags PT_MAIR_NORMAL_WB 1 0 1 1 1
517if ne pt_map 0x1000 0x1000 PT_PAGE_SIZE wx PT_ERR_WX
518ret false
519end
520note Device-nGnRE + executable MUST be refused
521devx = new PtFlags PT_MAIR_DEVICE_NGNRE 0 0 1 0 1
522if ne pt_map 0x2000 0x09000000 PT_PAGE_SIZE devx PT_ERR_DEVICE_EXEC
523ret false
524end
525note Device-nGnRE non-executable writable succeeds
526devrw = new PtFlags PT_MAIR_DEVICE_NGNRE 1 0 0 0 1
527if ne pt_map 0x2000 0x09000000 PT_PAGE_SIZE devrw PT_OK
528ret false
529end
530ret true
531end
532
533fn test_16_pt_pxn_uxn_flags to bool
534if ne pt_init 0x40000000 32 PT_OK
535ret false
536end
537kcode = new PtFlags PT_MAIR_NORMAL_WB 0 0 1 1 1
538ucode = new PtFlags PT_MAIR_NORMAL_WB 0 1 1 1 0
539kdata = new PtFlags PT_MAIR_NORMAL_WB 1 0 0 1 1
540if ne pt_map 0x1000 0x10000 PT_PAGE_SIZE kcode PT_OK
541ret false
542end
543if ne pt_map 0x2000 0x20000 PT_PAGE_SIZE ucode PT_OK
544ret false
545end
546if ne pt_map 0x3000 0x30000 PT_PAGE_SIZE kdata PT_OK
547ret false
548end
549tr = new PtTrans 0 0 0 0 0 0 0 0
550if ne pt_translate 0x1000 tr PT_OK
551ret false
552end
553if ne get tr executable 1
554ret false
555end
556if ne get tr user 0
557ret false
558end
559if ne get tr writable 0
560ret false
561end
562if ne pt_translate 0x2000 tr PT_OK
563ret false
564end
565if ne get tr executable 1
566ret false
567end
568if ne get tr user 1
569ret false
570end
571if ne get tr global_flag 0
572ret false
573end
574if ne pt_translate 0x3000 tr PT_OK
575ret false
576end
577if ne get tr executable 0
578ret false
579end
580if ne get tr writable 1
581ret false
582end
583if ne get tr user 0
584ret false
585end
586ret true
587end
588
589fn test_17_pt_overlap_and_partial_unmap to bool
590if ne pt_init 0x40000000 32 PT_OK
591ret false
592end
593f = new PtFlags PT_MAIR_NORMAL_WB 1 0 0 1 1
594if ne pt_map 0x00200000 0x10200000 PT_BLOCK_2M f PT_OK
595ret false
596end
597note Mapping 4 KiB inside existing 2 MiB block refused with PT_ERR_OVERLAP
598if ne pt_map 0x00201000 0x20000000 PT_PAGE_SIZE f PT_ERR_OVERLAP
599ret false
600end
601note Unmapping 4 KiB of a 2 MiB block refused with PT_ERR_MISALIGNED
602if ne pt_unmap 0x00200000 PT_PAGE_SIZE PT_ERR_MISALIGNED
603ret false
604end
605note Remapping same 4 KiB refused with PT_ERR_OVERLAP
606if ne pt_map 0x1000 0x10000 PT_PAGE_SIZE f PT_OK
607ret false
608end
609if ne pt_map 0x1000 0x20000 PT_PAGE_SIZE f PT_ERR_OVERLAP
610ret false
611end
612ret true
613end
614
615fn test_18_pt_unmap_and_exhaustion to bool
616if ne pt_init 0x40000000 4 PT_OK
617ret false
618end
619f = new PtFlags PT_MAIR_NORMAL_WB 1 0 0 1 1
620note Mapping a 4 KiB page consumes 4 frames (L0 root + L1 + L2 + L3) -> exact fit
621if ne pt_map 0x1000 0x10000 PT_PAGE_SIZE f PT_OK
622ret false
623end
624if ne pt_frames_used 4
625ret false
626end
627note Mapping in a different L1 subtree requires a 5th frame -> PT_ERR_NO_FRAMES
628if ne pt_map 0x8000000000 0x20000 PT_PAGE_SIZE f PT_ERR_NO_FRAMES
629ret false
630end
631note Unmap 0x1000 -> translate returns PT_ERR_NOT_MAPPED
632if ne pt_unmap 0x1000 PT_PAGE_SIZE PT_OK
633ret false
634end
635tr = new PtTrans 0 0 0 0 0 0 0 0
636if ne pt_translate 0x1000 tr PT_ERR_NOT_MAPPED
637ret false
638end
639if ne pt_unmap 0x1000 PT_PAGE_SIZE PT_ERR_NOT_MAPPED
640ret false
641end
642ret true
643end
644
645fn main to int
646failed = uint 0
647failed = add failed report s_t01 test_01_sched_placement
648failed = add failed report s_t02 test_02_sched_fairness
649failed = add failed report s_t03 test_03_sched_bg_anti_starvation
650failed = add failed report s_t04 test_04_sched_work_stealing
651failed = add failed report s_t05 test_05_sched_remove_reuse
652failed = add failed report s_t06 test_06_sched_negatives
653failed = add failed report s_t07 test_07_ipc_fifo_bounds
654failed = add failed report s_t08 test_08_ipc_rights_and_resource
655failed = add failed report s_t09 test_09_ipc_principal_isolation
656failed = add failed report s_t10 test_10_ipc_object_rw_oob
657failed = add failed report s_t11 test_11_ipc_delegation_attenuation
658failed = add failed report s_t12 test_12_ipc_tree_revocation
659failed = add failed report s_t13 test_13_pt_4k_2m_1g_translate
660failed = add failed report s_t14 test_14_pt_high_half_canonical
661failed = add failed report s_t15 test_15_pt_wx_and_device_exec
662failed = add failed report s_t16 test_16_pt_pxn_uxn_flags
663failed = add failed report s_t17 test_17_pt_overlap_and_partial_unmap
664failed = add failed report s_t18 test_18_pt_unmap_and_exhaustion
665if eq failed 0
666w = kwrite_all 1 s_sum
667if w
668ret 0
669end
670ret 1
671end
672w = kwrite_all 1 s_fsum
673if w
674ret 1
675end
676ret 1
677end