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 / iort.om

732 lines · 15414 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/kernel/hal/iort.om: Pure-Omega ACPI SDT checksum and IORT SMMUv3 route parser.
3note Converted from aienux native/kernel/core/acpi.{h,c} (ck_acpi_sum_ok, ck_iort_parse,
4note ck_iort_stream_id, ck_iort_other_stream, ck_iort_route_rid, ck_iort_named, RMR ranges).
5note Compose with: si/directives/om2.om aienux/kernel/hal/iort.om ...
6
7const IORT_SDT_HDR ulong 36
8const IORT_TABLE_HDR ulong 48
9const IORT_NODE_HDR ulong 16
10const IORT_MAP_BYTES ulong 20
11const IORT_RC_SEGMENT ulong 28
12const IORT_RMR_HDR ulong 28
13const IORT_RMR_DESC_BYTES ulong 20
14const IORT_NAMED_NAME_OFF ulong 29
15
16const IORT_NODE_NAMED uchar 1
17const IORT_NODE_RC uchar 2
18const IORT_NODE_SMMUV3 uchar 4
19const IORT_NODE_RMR uchar 6
20const IORT_MAP_SINGLE uint 1
21
22const IORT_MAX_SMMUS ulong 8
23const IORT_MAX_MAPS ulong 16
24const IORT_MAX_OTHER ulong 8
25const IORT_MAX_RMR ulong 4
26const IORT_MAX_RMR_RANGES ulong 4
27
28note Metadata slots: 0=nsmmus, 1=nmaps, 2=nother, 3=nrmr, 4=primary_base, 5=primary_off,
29note 6=mutant_route_first (0=off, 1=route all to smmu 0), 7=mutant_rmr_excl (0=inclusive, 1=exclusive)
30global iort_meta ulong 8
31global iort_smmu_base ulong 8
32global iort_smmu_off uint 8
33
34global iort_map_seg uint 16
35global iort_map_in uint 16
36global iort_map_cnt uint 16
37global iort_map_out uint 16
38
39global iort_oth_seg uint 8
40global iort_oth_in uint 8
41global iort_oth_cnt uint 8
42global iort_oth_out uint 8
43global iort_oth_base ulong 8
44global iort_oth_off uint 8
45
46global iort_rmr_off uint 4
47global iort_rmr_sidx uint 4
48global iort_rmr_slo uint 4
49global iort_rmr_shi uint 4
50global iort_rmr_flags uint 4
51global iort_rmr_nrng uint 4
52global iort_rmr_rbase ulong 16
53global iort_rmr_rlen ulong 16
54global iort_tmp_rb ulong 4
55global iort_tmp_rl ulong 4
56
57note Route query result slots: 0=smmu_index, 1=smmu_base, 2=smmu_off, 3=sid, 4=rmr_hits,
58note 5=rmr_first_flags, 6=named_node_off, 7=named_sid, 8=route CCA (iort_route_rid)
59global iort_qres ulong 9
60note Root-complex Memory Access Properties CCA (IORT RC node +16, u32; 1 = fully IO-coherent),
61note recorded per primary/other map; iort_route_rid reports it in iort_qres[8].
62global iort_map_cca uint 16
63global iort_oth_cca uint 8
64global iort_cur_cca uint 1
65
66fn iort_rd16 buf view uchar * off ulong to uint
67b0 = uint get buf off
68b1 = uint get buf add off 1
69ret or b0 shl b1 8
70end
71
72fn iort_rd32 buf view uchar * off ulong to uint
73b0 = uint get buf off
74b1 = uint get buf add off 1
75b2 = uint get buf add off 2
76b3 = uint get buf add off 3
77lo = or b0 shl b1 8
78hi = or shl b2 16 shl b3 24
79ret or lo hi
80end
81
82fn iort_rd64 buf view uchar * off ulong to ulong
83lo = ulong iort_rd32 buf off
84hi = ulong iort_rd32 buf add off 4
85ret or lo shl hi 32
86end
87
88fn acpi_sum_ok buf view uchar * n ulong to bool
89if or lt n IORT_SDT_HDR gt n len buf
90ret false
91end
92acc = uint 0
93i = ulong 0
94note ACPI tables are bounded in size; 65536 covers all IORT/MADT/SDT tables.
95while lt i n bound 65536
96acc = and add acc uint get buf i 0xff
97i = add i 1
98end
99ret eq acc 0
100end
101
102fn iort_reset to void
103for i ulong 8
104put iort_meta i 0
105put iort_smmu_base i 0
106put iort_smmu_off i 0
107put iort_oth_seg i 0
108put iort_oth_in i 0
109put iort_oth_cnt i 0
110put iort_oth_out i 0
111put iort_oth_base i 0
112put iort_oth_off i 0
113put iort_oth_cca i 0
114put iort_qres i 0
115end
116put iort_qres 8 0
117for j ulong 16
118put iort_map_seg j 0
119put iort_map_in j 0
120put iort_map_cnt j 0
121put iort_map_out j 0
122put iort_map_cca j 0
123put iort_rmr_rbase j 0
124put iort_rmr_rlen j 0
125end
126for k ulong 4
127put iort_rmr_off k 0
128put iort_rmr_sidx k 0
129put iort_rmr_slo k 0
130put iort_rmr_shi k 0
131put iort_rmr_flags k 0
132put iort_rmr_nrng k 0
133end
134end
135
136fn iort_set_mutants route_first ulong rmr_excl ulong to void
137put iort_meta 6 route_first
138put iort_meta 7 rmr_excl
139end
140
141fn iort_find_smmu_idx off uint to ulong
142ns = get iort_meta 0
143i = ulong 0
144while lt i ns bound 8
145if eq get iort_smmu_off i off
146ret i
147end
148i = add i 1
149end
150ret IORT_MAX_SMMUS
151end
152
153fn iort_u32_add_overflow a uint b uint to bool
154s = add a b
155ret lt s a
156end
157
158fn iort_u64_add_overflow a ulong b ulong to bool
159s = add a b
160ret lt s a
161end
162
163fn iort_ranges_overlap a_lo uint a_cnt uint b_lo uint b_cnt uint to bool
164a_hi = add a_lo a_cnt
165b_hi = add b_lo b_cnt
166if lt a_hi b_lo
167ret false
168end
169if lt b_hi a_lo
170ret false
171end
172ret true
173end
174
175fn iort_check_hdr buf view uchar * to long
176blen = len buf
177if lt blen IORT_TABLE_HDR
178ret -1
179end
180note Signature "IORT" = 0x49 0x4f 0x52 0x54
181if or ne get buf 0 73 ne get buf 1 79
182ret -1
183end
184if or ne get buf 2 82 ne get buf 3 84
185ret -1
186end
187tlen = ulong iort_rd32 buf 4
188if or lt tlen IORT_TABLE_HDR gt tlen blen
189ret -1
190end
191if not acpi_sum_ok buf tlen
192ret -1
193end
194ncnt = ulong iort_rd32 buf 36
195noff = ulong iort_rd32 buf 40
196if eq ncnt 0
197ret -1
198end
199if or lt noff IORT_TABLE_HDR gt noff tlen
200ret -1
201end
202ret long tlen
203end
204
205fn iort_load_rmr_descs buf view uchar * start ulong rcount ulong to int
206d = ulong 0
207while lt d rcount bound 4
208doff = add start mul d IORT_RMR_DESC_BYTES
209b = iort_rd64 buf doff
210l = iort_rd64 buf add doff 8
211if eq l 0
212ret -1
213end
214if or ne and b 0xfff 0 ne and l 0xfff 0
215ret -1
216end
217if iort_u64_add_overflow b l
218ret -1
219end
220put iort_tmp_rb d b
221put iort_tmp_rl d l
222d = add d 1
223end
224ret 0
225end
226
227fn iort_bind_rmr_one off ulong sidx ulong ob uint cnt uint flags uint rcount ulong to int
228nr = get iort_meta 3
229if ge nr IORT_MAX_RMR
230ret -1
231end
232if iort_u32_add_overflow ob cnt
233ret -1
234end
235shi = add ob cnt
236if ne get iort_meta 7 0
237note Mutant: treat id_count as exclusive count instead of inclusive (number of IDs - 1)
238if gt cnt 0
239shi = sub add ob cnt 1
240else
241shi = ob
242end
243end
244put iort_rmr_off nr uint off
245put iort_rmr_sidx nr uint sidx
246put iort_rmr_slo nr ob
247put iort_rmr_shi nr shi
248put iort_rmr_flags nr flags
249put iort_rmr_nrng nr uint rcount
250k = ulong 0
251while lt k rcount bound 4
252rslot = add mul nr 4 k
253put iort_rmr_rbase rslot get iort_tmp_rb k
254put iort_rmr_rlen rslot get iort_tmp_rl k
255k = add k 1
256end
257put iort_meta 3 add nr 1
258ret 0
259end
260
261fn iort_bind_rmr_maps buf view uchar * off ulong mcount ulong moff ulong to int
262flags = iort_rd32 buf add off 16
263rcount = ulong iort_rd32 buf add off 20
264m = ulong 0
265while lt m mcount bound 64
266eoff = add add off moff mul m IORT_MAP_BYTES
267ref = iort_rd32 buf add eoff 12
268sidx = iort_find_smmu_idx ref
269if lt sidx IORT_MAX_SMMUS
270cnt = iort_rd32 buf add eoff 4
271ob = iort_rd32 buf add eoff 8
272if ne iort_bind_rmr_one off sidx ob cnt flags rcount 0
273ret -1
274end
275end
276m = add m 1
277end
278ret 0
279end
280
281fn iort_parse_rmr buf view uchar * off ulong nlen ulong mcount ulong moff ulong to int
282if lt nlen IORT_RMR_HDR
283ret -1
284end
285rcount = ulong iort_rd32 buf add off 20
286roff = ulong iort_rd32 buf add off 24
287if or eq rcount 0 gt rcount IORT_MAX_RMR_RANGES
288ret -1
289end
290if or lt roff IORT_RMR_HDR gt roff nlen
291ret -1
292end
293if gt mul rcount IORT_RMR_DESC_BYTES sub nlen roff
294ret -1
295end
296if ne iort_load_rmr_descs buf add off roff rcount 0
297ret -1
298end
299ret iort_bind_rmr_maps buf off mcount moff
300end
301
302fn iort_add_primary_map seg uint inb uint cnt uint ob uint to int
303if or iort_u32_add_overflow inb cnt iort_u32_add_overflow ob cnt
304ret -1
305end
306nm = get iort_meta 1
307if ge nm IORT_MAX_MAPS
308ret -1
309end
310i = ulong 0
311while lt i nm bound 16
312if eq get iort_map_seg i seg
313if iort_ranges_overlap get iort_map_in i get iort_map_cnt i inb cnt
314ret -1
315end
316end
317i = add i 1
318end
319put iort_map_seg nm seg
320put iort_map_in nm inb
321put iort_map_cnt nm cnt
322put iort_map_out nm ob
323put iort_map_cca nm get iort_cur_cca 0
324put iort_meta 1 add nm 1
325ret 0
326end
327
328fn iort_add_other_map seg uint inb uint cnt uint ob uint sidx ulong to int
329if or iort_u32_add_overflow inb cnt iort_u32_add_overflow ob cnt
330ret -1
331end
332no = get iort_meta 2
333if ge no IORT_MAX_OTHER
334ret -1
335end
336i = ulong 0
337while lt i no bound 8
338if eq get iort_oth_seg i seg
339if iort_ranges_overlap get iort_oth_in i get iort_oth_cnt i inb cnt
340ret -1
341end
342end
343i = add i 1
344end
345put iort_oth_seg no seg
346put iort_oth_in no inb
347put iort_oth_cnt no cnt
348put iort_oth_out no ob
349put iort_oth_base no get iort_smmu_base sidx
350put iort_oth_off no get iort_smmu_off sidx
351put iort_oth_cca no get iort_cur_cca 0
352put iort_meta 2 add no 1
353ret 0
354end
355
356fn iort_parse_rc_map buf view uchar * eoff ulong seg uint to int
357inb = iort_rd32 buf eoff
358cnt = iort_rd32 buf add eoff 4
359ob = iort_rd32 buf add eoff 8
360ref = iort_rd32 buf add eoff 12
361prim_off = uint get iort_meta 5
362if eq ref prim_off
363ret iort_add_primary_map seg inb cnt ob
364end
365sidx = iort_find_smmu_idx ref
366if lt sidx IORT_MAX_SMMUS
367ret iort_add_other_map seg inb cnt ob sidx
368end
369ret 0
370end
371
372fn iort_parse_rc buf view uchar * off ulong nlen ulong mcount ulong moff ulong to int
373if lt nlen add IORT_RC_SEGMENT 4
374ret -1
375end
376seg = iort_rd32 buf add off IORT_RC_SEGMENT
377put iort_cur_cca 0 iort_rd32 buf add off 16
378m = ulong 0
379while lt m mcount bound 64
380eoff = add add off moff mul m IORT_MAP_BYTES
381if ne iort_parse_rc_map buf eoff seg 0
382ret -1
383end
384m = add m 1
385end
386ret 0
387end
388
389fn iort_parse_pass0 buf view uchar * tlen ulong ncnt ulong noff ulong to int
390off = noff
391i = ulong 0
392while lt i ncnt bound 128
393if or lt off IORT_TABLE_HDR gt off tlen
394ret -1
395end
396if lt sub tlen off IORT_NODE_HDR
397ret -1
398end
399ntype = get buf off
400nlen = ulong iort_rd16 buf add off 1
401if or lt nlen IORT_NODE_HDR gt nlen sub tlen off
402ret -1
403end
404if eq ntype IORT_NODE_SMMUV3
405if lt nlen 24
406ret -1
407end
408base = iort_rd64 buf add off 16
409if eq base 0
410ret -1
411end
412ns = get iort_meta 0
413if ge ns IORT_MAX_SMMUS
414ret -1
415end
416put iort_smmu_base ns base
417put iort_smmu_off ns uint off
418if eq ns 0
419put iort_meta 4 base
420put iort_meta 5 off
421end
422put iort_meta 0 add ns 1
423end
424off = add off nlen
425i = add i 1
426end
427if eq get iort_meta 0 0
428ret 0
429end
430ret 1
431end
432
433fn iort_parse_pass1 buf view uchar * tlen ulong ncnt ulong noff ulong to int
434off = noff
435i = ulong 0
436while lt i ncnt bound 128
437ntype = get buf off
438nlen = ulong iort_rd16 buf add off 1
439mcount = ulong iort_rd32 buf add off 8
440moff = ulong iort_rd32 buf add off 12
441if gt mcount 0
442if or lt moff IORT_NODE_HDR gt moff nlen
443ret -1
444end
445if gt mul mcount IORT_MAP_BYTES sub nlen moff
446ret -1
447end
448end
449if eq ntype IORT_NODE_RC
450if ne iort_parse_rc buf off nlen mcount moff 0
451ret -1
452end
453end
454if eq ntype IORT_NODE_RMR
455if ne iort_parse_rmr buf off nlen mcount moff 0
456ret -1
457end
458end
459off = add off nlen
460i = add i 1
461end
462ret 1
463end
464
465note Parse an ACPI IORT table buffer. Preserves mutant flags across reset.
466note Returns 1 if at least one SMMUv3 node was found, 0 if none, -1 if malformed.
467fn iort_parse buf view uchar * to int
468m0 = get iort_meta 6
469m1 = get iort_meta 7
470iort_reset
471put iort_meta 6 m0
472put iort_meta 7 m1
473tl = iort_check_hdr buf
474if lt tl 0
475ret -1
476end
477tlen = ulong tl
478ncnt = ulong iort_rd32 buf 36
479noff = ulong iort_rd32 buf 40
480p0 = iort_parse_pass0 buf tlen ncnt noff
481if le p0 0
482ret p0
483end
484ret iort_parse_pass1 buf tlen ncnt noff
485end
486
487fn iort_count_rmr_for sidx ulong sid uint to ulong
488nr = get iort_meta 3
489hits = ulong 0
490i = ulong 0
491while lt i nr bound 4
492if eq ulong get iort_rmr_sidx i sidx
493lo = get iort_rmr_slo i
494hi = get iort_rmr_shi i
495if and ge sid lo le sid hi
496if eq hits 0
497put iort_qres 5 ulong get iort_rmr_flags i
498end
499hits = add hits 1
500end
501end
502i = add i 1
503end
504ret hits
505end
506
507note Route (segment, rid) across primary and other SMMUv3 nodes.
508note Returns 0 on match (results in iort_qres[0..5], RC CCA in [8]), -1 if uncovered.
509fn iort_route_rid seg uint rid uint to int
510for z ulong 6
511put iort_qres z 0
512end
513put iort_qres 8 0
514nm = get iort_meta 1
515i = ulong 0
516while lt i nm bound 16
517if eq get iort_map_seg i seg
518inb = get iort_map_in i
519if ge rid inb
520rel = sub rid inb
521if le rel get iort_map_cnt i
522sid = add get iort_map_out i rel
523put iort_qres 0 0
524put iort_qres 1 get iort_meta 4
525put iort_qres 2 get iort_meta 5
526put iort_qres 3 ulong sid
527put iort_qres 4 iort_count_rmr_for 0 sid
528put iort_qres 8 ulong get iort_map_cca i
529ret 0
530end
531end
532end
533i = add i 1
534end
535no = get iort_meta 2
536j = ulong 0
537while lt j no bound 8
538if eq get iort_oth_seg j seg
539inb = get iort_oth_in j
540if ge rid inb
541rel = sub rid inb
542if le rel get iort_oth_cnt j
543sid = add get iort_oth_out j rel
544sidx = iort_find_smmu_idx get iort_oth_off j
545if ge sidx IORT_MAX_SMMUS
546ret -1
547end
548if ne get iort_meta 6 0
549note Mutant: route every other-SMMU requester to SMMU 0
550sidx = ulong 0
551end
552put iort_qres 0 sidx
553put iort_qres 1 get iort_smmu_base sidx
554put iort_qres 2 ulong get iort_smmu_off sidx
555put iort_qres 3 ulong sid
556put iort_qres 4 iort_count_rmr_for sidx sid
557put iort_qres 8 ulong get iort_oth_cca j
558ret 0
559end
560end
561end
562j = add j 1
563end
564ret -1
565end
566
567fn iort_last_seg4 buf view uchar * start ulong lim ulong dest edit uchar 4 to int
568seg_start = start
569q = start
570while lt q lim bound 64
571c = get buf q
572if eq c 0
573break
574end
575if or eq c 46 or eq c 92 eq c 94
576seg_start = add q 1
577end
578q = add q 1
579end
580i = ulong 0
581s = seg_start
582while and lt s lim lt i 4 bound 5
583c = get buf s
584if eq c 0
585break
586end
587put dest i c
588i = add i 1
589s = add s 1
590end
591if eq i 0
592ret -1
593end
594if lt s lim
595if ne get buf s 0
596ret -1
597end
598end
599while lt i 4 bound 4
600put dest i 95
601i = add i 1
602end
603ret 0
604end
605
606fn iort_named_bind_smmu buf view uchar * map_start ulong mcount ulong to int
607tlen = ulong iort_rd32 buf 4
608m = ulong 0
609while lt m mcount bound 64
610eoff = add map_start mul m IORT_MAP_BYTES
611ref = ulong iort_rd32 buf add eoff 12
612if or lt ref IORT_TABLE_HDR gt ref tlen
613ret -1
614end
615if lt sub tlen ref IORT_NODE_HDR
616ret -1
617end
618if eq get buf ref IORT_NODE_SMMUV3
619is_single = ne and iort_rd32 buf add eoff 16 IORT_MAP_SINGLE 0
620is_one = eq iort_rd32 buf add eoff 4 0
621if or is_single is_one
622put iort_qres 7 ulong iort_rd32 buf add eoff 8
623t_nlen = ulong iort_rd16 buf add ref 1
624if and ge t_nlen 24 le t_nlen sub tlen ref
625put iort_qres 1 iort_rd64 buf add ref 16
626end
627ret 0
628end
629end
630m = add m 1
631end
632ret 0
633end
634
635fn iort_named_match buf view uchar * off ulong nlen ulong mcount ulong moff ulong to int
636if le nlen IORT_NAMED_NAME_OFF
637ret -1
638end
639nend = add off nlen
640if gt mcount 0
641nend = add off moff
642end
643nstart = add off IORT_NAMED_NAME_OFF
644if le nend nstart
645ret -1
646end
647s4 = new uchar 4 0
648if ne iort_last_seg4 buf nstart nend s4 0
649ret 0
650end
651for k ulong 4
652if ne ulong get s4 k get iort_tmp_rb k
653ret 0
654end
655end
656ret 1
657end
658
659fn iort_named_scan buf view uchar * tlen ulong ncnt ulong noff ulong to int
660found = ulong 0
661off = noff
662i = ulong 0
663while lt i ncnt bound 128
664if or lt off IORT_TABLE_HDR gt off tlen
665ret -1
666end
667if lt sub tlen off IORT_NODE_HDR
668ret -1
669end
670ntype = get buf off
671nlen = ulong iort_rd16 buf add off 1
672if or lt nlen IORT_NODE_HDR gt nlen sub tlen off
673ret -1
674end
675mcount = ulong iort_rd32 buf add off 8
676moff = ulong iort_rd32 buf add off 12
677if gt mcount 0
678if or lt moff IORT_NODE_HDR gt moff nlen
679ret -1
680end
681if gt mul mcount IORT_MAP_BYTES sub nlen moff
682ret -1
683end
684end
685if eq ntype IORT_NODE_NAMED
686mr = iort_named_match buf off nlen mcount moff
687if lt mr 0
688ret -1
689end
690if eq mr 1
691if ne found 0
692ret -2
693end
694found = ulong 1
695put iort_qres 6 off
696if ne iort_named_bind_smmu buf add off moff mcount 0
697ret -1
698end
699end
700end
701off = add off nlen
702i = add i 1
703end
704if eq found 0
705ret 0
706end
707ret 1
708end
709
710note Lookup a Named Component node by its final 4-char ACPI segment (padded with '_').
711note Returns 1 if found (iort_qres[6]=node_off, [7]=stream_id, [1]=target_base),
712note 0 if not found, -1 if malformed, -2 if duplicate named component matches.
713fn iort_named_route buf view uchar * want view uchar * to int
714put iort_qres 6 0
715put iort_qres 7 0
716put iort_qres 1 0
717tl = iort_check_hdr buf
718if lt tl 0
719ret -1
720end
721w4 = new uchar 4 0
722if ne iort_last_seg4 want 0 len want w4 0
723ret -1
724end
725for k ulong 4
726put iort_tmp_rb k ulong get w4 k
727end
728tlen = ulong tl
729ncnt = ulong iort_rd32 buf 36
730noff = ulong iort_rd32 buf 40
731ret iort_named_scan buf tlen ncnt noff
732end