interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / tools / hwgates / hwgates.om

707 lines · 12521 bytes Raw Blame History
1note hwgates.om: hardware-gate register check (DRA-19). Seeded Omega, built with the compiler named in
2note PIN at the repository root (the newest omega-mv build when this tool last moved).
3note usage: hwgates REGISTER ROOT. REGISTER is a tab-separated file with 7 fields per row:
4note id component source tier status receipt owner
5note tier is HOST, QEMU or PHYSICAL. status is PASS, FAIL, NOT_RUN, BLOCKED_HARDWARE,
6note BLOCKED_OPERATOR or MISSING_IMPLEMENTATION. Blank lines and lines starting with # are skipped.
7note Rules, each failure prints one line HWGATES FAIL line=N rule=RK:
8note R1 exactly 7 non-empty fields
9note R2 tier and status are in the vocabulary
10note R3 PASS or FAIL needs a receipt file under ROOT that holds the exact line
11note HWGATE <id> <tier> <status> (no claim without a receipt)
12note R4 a status that is not an observation (NOT_RUN, BLOCKED_*, MISSING_IMPLEMENTATION) has receipt -
13note R5 every gate id has exactly one PHYSICAL row (physical rows stay explicit)
14note R6 no id appears twice with the same tier
15note R7 a receipt path starts with evidence/ and holds no .. and no leading /
16note R8 the register has at least one row
17note R9 every gate id named by a HWGATE line of a cited receipt has rows in the register, so a whole
18note gate cannot leave the register while its receipt still names it
19note Exit 0 and HWGATES PASS when no rule fails; exit 1 and HWGATES FAIL otherwise; exit 2 on usage,
20note an unreadable register, or a register larger than the checker's tables (CAP bytes, MAXR rows):
21note that refusal names its table, and tools/hwgates/gate.sh alarms before the register gets there.
22note Every byte loop is bounded: each pass consumes one byte of a buffer of at most CAP bytes.
23note CAP and MAXR exist only because omc has no runtime-sized allocation: every global is a
24note fixed-size table (no heap by design, omega-mv docs/ROADMAP-1.0.md item 10, Decision 24).
25note If omc gains input-sized buffers, size these from the register and receipt lengths and drop
26note both constants; until then gate.sh's alarm at three quarters of each is the guard.
27
28text vocab "HOST\nQEMU\nPHYSICAL\n#PASS\nFAIL\nNOT_RUN\nBLOCKED_HARDWARE\nBLOCKED_OPERATOR\nMISSING_IMPLEMENTATION\n#"
29text hwtag "HWGATE "
30text evpre "evidence/"
31text mfail "HWGATES FAIL line="
32text mrule " rule=R"
33text mpass "HWGATES PASS rows="
34text mgates " gates="
35text mphys " physical="
36text mppass " physical_pass="
37text mnrun " physical_not_pass="
38text mfails "HWGATES FAIL failures="
39text mrows " rows="
40text musage "usage: hwgates REGISTER ROOT\n"
41text mread "HWGATES ERROR cannot read register or it is larger than CAP bytes\n"
42text msize "HWGATES ERROR register has more rows than the row table holds (MAXR)\n"
43text nl "\n"
44
45const CAP ulong 524288
46const MAXR ulong 1024
47
48global rb uchar 524288
49global eb uchar 524288
50global pb uchar 4096
51global ab uchar 4096
52global nb uchar 1024
53global fs ulong 8
54global fl ulong 8
55global rid ulong 1024
56global ril ulong 1024
57global rti long 1024
58global rst long 1024
59global rln ulong 1024
60global rrs ulong 1024
61global rrl ulong 1024
62global cnt ulong 8
63
64note cnt: 0 rows, 1 failures, 2 register length, 3 root length, 4 physical rows, 5 physical PASS
65
66fn pnum v ulong to void
67b = new uchar 24 0
68i = ulong 24
69x = v
70c = true
71while c bound 24
72i = sub i 1
73put b i uchar add 48 rem x 10
74x = div x 10
75if eq x 0
76c = false
77end
78end
79k = write 1 b i sub 24 i
80end
81
82fn fail ln ulong rule ulong to void
83k = write 1 mfail 0 len mfail
84pnum ln
85k = write 1 mrule 0 len mrule
86pnum rule
87k = write 1 nl 0 len nl
88put cnt 1 add get cnt 1 1
89end
90
91note read the file named by pb[0..n) into rb; returns its length, or -1
92fn rdreg n ulong to long
93fd = fopen pb 0 n 0 0
94if lt fd 0
95ret -1
96end
97tot = ulong 0
98c = true
99while c bound 524289
100k = read int fd rb tot sub CAP tot
101if lt k 0
102c = false
103tot = CAP
104else
105if eq k 0
106c = false
107else
108tot = add tot ulong k
109if ge tot CAP
110c = false
111end
112end
113end
114end
115r = fclose int fd
116if ge tot CAP
117ret -1
118end
119ret long tot
120end
121
122note same for eb (a receipt)
123fn rdrec n ulong to long
124fd = fopen pb 0 n 0 0
125if lt fd 0
126ret -1
127end
128tot = ulong 0
129c = true
130while c bound 524289
131k = read int fd eb tot sub CAP tot
132if lt k 0
133c = false
134tot = CAP
135else
136if eq k 0
137c = false
138else
139tot = add tot ulong k
140if ge tot CAP
141c = false
142end
143end
144end
145end
146r = fclose int fd
147if ge tot CAP
148ret -1
149end
150ret long tot
151end
152
153note rb[a..a+l) equals rb[b..b+l)
154fn eqrb a ulong b ulong l ulong to bool
155i = ulong 0
156while lt i l bound 524289
157if ne get rb add a i get rb add b i
158ret false
159end
160i = add i 1
161end
162ret true
163end
164
165note index of rb[s..s+l) inside vocabulary group g, or -1
166fn vidx s ulong l ulong g ulong to long
167grp = ulong 0
168e = long 0
169es = ulong 0
170i = ulong 0
171n = len vocab
172while lt i n bound 4096
173ch = get vocab i
174if eq ch uchar 35
175grp = add grp 1
176e = long 0
177es = add i 1
178else
179if eq ch uchar 10
180if eq grp g
181if eq sub i es l
182hit = vmatch s es l
183if hit
184ret e
185end
186end
187end
188e = add e 1
189es = add i 1
190end
191end
192i = add i 1
193end
194ret -1
195end
196
197fn vmatch s ulong es ulong l ulong to bool
198j = ulong 0
199while lt j l bound 4096
200if ne get rb add s j get vocab add es j
201ret false
202end
203j = add j 1
204end
205ret true
206end
207
208note field k is exactly "-"
209fn isdash k ulong to bool
210if ne get fl k 1
211ret false
212end
213ret eq get rb get fs k uchar 45
214end
215
216note R7: receipt field 5 starts with evidence/, has no .., no leading /
217fn rpath to bool
218s = get fs 5
219l = get fl 5
220p = len evpre
221if lt l add p 1
222ret false
223end
224i = ulong 0
225while lt i p bound 16
226if ne get rb add s i get evpre i
227ret false
228end
229i = add i 1
230end
231i = ulong 0
232while lt add i 1 l bound 4096
233if eq get rb add s i uchar 46
234if eq get rb add s add i 1 uchar 46
235ret false
236end
237end
238i = add i 1
239end
240ret true
241end
242
243note build ROOT/receipt into pb; returns its length (0 when too long)
244fn mkpath to ulong
245al = get cnt 3
246s = get fs 5
247l = get fl 5
248if ge add add al l 1 4096
249ret 0
250end
251i = ulong 0
252while lt i al bound 4096
253put pb i get ab i
254i = add i 1
255end
256put pb al uchar 47
257j = ulong 0
258while lt j l bound 4096
259put pb add add al 1 j get rb add s j
260j = add j 1
261end
262ret add add al 1 l
263end
264
265note copy rb[s..s+l) into nb at o; returns new offset
266fn ncopy o ulong s ulong l ulong to ulong
267i = ulong 0
268while lt i l bound 1024
269put nb add o i get rb add s i
270i = add i 1
271end
272ret add o l
273end
274
275note build HWGATE <id> <tier> <status> into nb; returns its length (0 when too long)
276fn mkneed to ulong
277t = add add add get fl 0 get fl 3 get fl 4 10
278if ge t 1024
279ret 0
280end
281i = ulong 0
282p = len hwtag
283while lt i p bound 16
284put nb i get hwtag i
285i = add i 1
286end
287o = ncopy p get fs 0 get fl 0
288put nb o uchar 32
289o = ncopy add o 1 get fs 3 get fl 3
290put nb o uchar 32
291o = ncopy add o 1 get fs 4 get fl 4
292ret o
293end
294
295note eb[ls..ls+n) equals nb[0..n)
296fn eqneed ls ulong n ulong to bool
297i = ulong 0
298while lt i n bound 1024
299if ne get eb add ls i get nb i
300ret false
301end
302i = add i 1
303end
304ret true
305end
306
307note the receipt eb[0..el) holds nb[0..n) as a whole line
308fn hasline el ulong n ulong to bool
309ls = ulong 0
310i = ulong 0
311while le i el bound 524290
312eol = true
313if lt i el
314eol = eq get eb i uchar 10
315end
316if eol
317if eq sub i ls n
318ok = eqneed ls n
319if ok
320ret true
321end
322end
323ls = add i 1
324end
325i = add i 1
326end
327ret false
328end
329
330note R3 for the current row; returns true when the receipt supports the claim
331fn receipt ln ulong to bool
332okp = rpath
333if eq okp false
334fail ln 7
335ret false
336end
337pl = mkpath
338if eq pl 0
339fail ln 7
340ret false
341end
342el = rdrec pl
343if lt el 0
344fail ln 3
345ret false
346end
347n = mkneed
348if eq n 0
349fail ln 3
350ret false
351end
352ok = hasline ulong el n
353if eq ok false
354fail ln 3
355ret false
356end
357ret true
358end
359
360note split rb[s..e) on tabs into fs/fl; returns the field count (8 means more than 7)
361fn split s ulong e ulong to ulong
362k = ulong 0
363fst = s
364i = s
365while le i e bound 524290
366endf = true
367if lt i e
368endf = eq get rb i uchar 9
369end
370if endf
371if lt k 7
372put fs k fst
373put fl k sub i fst
374end
375k = add k 1
376fst = add i 1
377end
378i = add i 1
379end
380if gt k 7
381ret 8
382end
383ret k
384end
385
386note check one non-comment line; ln is its 1-based line number
387fn row s ulong e ulong ln ulong to void
388k = split s e
389if ne k 7
390fail ln 1
391ret
392end
393f = ulong 0
394while lt f 7 bound 8
395if eq get fl f 0
396fail ln 1
397ret
398end
399f = add f 1
400end
401ti = vidx get fs 3 get fl 3 0
402si = vidx get fs 4 get fl 4 1
403if lt ti 0
404fail ln 2
405ret
406end
407if lt si 0
408fail ln 2
409ret
410end
411r = get cnt 0
412if ge r MAXR
413w = write 2 msize 0 len msize
414exit 2
415end
416dup = isdup r ti
417if dup
418fail ln 6
419end
420put rid r get fs 0
421put ril r get fl 0
422put rti r ti
423put rst r si
424put rln r ln
425put rrs r get fs 5
426put rrl r get fl 5
427put cnt 0 add r 1
428dash = isdash 5
429if le si 1
430if dash
431fail ln 3
432ret
433end
434ok = receipt ln
435else
436if eq dash false
437fail ln 4
438end
439end
440end
441
442note an earlier row j < r has tier ti and the id in field 0
443fn isdup r ulong ti long to bool
444j = ulong 0
445s = get fs 0
446l = get fl 0
447while lt j r bound 1025
448if eq get rti j ti
449if eq get ril j l
450same = eqrb get rid j s l
451if same
452ret true
453end
454end
455end
456j = add j 1
457end
458ret false
459end
460
461note R5: every distinct id has exactly one PHYSICAL row; returns the number of distinct ids
462fn pairs to ulong
463r = get cnt 0
464g = ulong 0
465i = ulong 0
466while lt i r bound 1025
467first = true
468j = ulong 0
469while lt j i bound 1025
470if eq get ril j get ril i
471same = eqrb get rid j get rid i get ril i
472if same
473first = false
474end
475end
476j = add j 1
477end
478if first
479g = add g 1
480np = ulong 0
481j = ulong 0
482while lt j r bound 1025
483if eq get rti j 2
484if eq get ril j get ril i
485same = eqrb get rid j get rid i get ril i
486if same
487np = add np 1
488end
489end
490end
491j = add j 1
492end
493if ne np 1
494fail get rln i 5
495end
496end
497i = add i 1
498end
499ret g
500end
501
502note an earlier PASS/FAIL row i < j cites the same receipt path as row j
503fn samerec j ulong to bool
504i = ulong 0
505while lt i j bound 1025
506if le get rst i 1
507if eq get rrl i get rrl j
508same = eqrb get rrs i get rrs j get rrl j
509if same
510ret true
511end
512end
513end
514i = add i 1
515end
516ret false
517end
518
519note rb[a..a+l) equals eb[b..b+l)
520fn eqid a ulong b ulong l ulong to bool
521i = ulong 0
522while lt i l bound 524289
523if ne get rb add a i get eb add b i
524ret false
525end
526i = add i 1
527end
528ret true
529end
530
531note the gate id eb[s..s+l) has a row in the register
532fn idknown s ulong l ulong to bool
533r = get cnt 0
534j = ulong 0
535while lt j r bound 1025
536if eq get ril j l
537same = eqid get rid j s l
538if same
539ret true
540end
541end
542j = add j 1
543end
544ret false
545end
546
547note eb[ls..) starts with the HWGATE tag
548fn eqtag ls ulong to bool
549p = len hwtag
550k = ulong 0
551while lt k p bound 16
552if ne get eb add ls k get hwtag k
553ret false
554end
555k = add k 1
556end
557ret true
558end
559
560note first space in eb[s..e), or e
561fn idend s ulong e ulong to ulong
562i = s
563while lt i e bound 524290
564if eq get eb i uchar 32
565ret i
566end
567i = add i 1
568end
569ret e
570end
571
572note R9 for one receipt eb[0..el): every HWGATE line names a register id; ln is the citing row
573fn scanids el ulong ln ulong to void
574p = len hwtag
575ls = ulong 0
576i = ulong 0
577while le i el bound 524290
578eol = true
579if lt i el
580eol = eq get eb i uchar 10
581end
582if eol
583if gt sub i ls p
584hit = eqtag ls
585if hit
586s = add ls p
587e = idend s i
588known = idknown s sub e s
589if eq known false
590fail ln 9
591end
592end
593end
594ls = add i 1
595end
596i = add i 1
597end
598end
599
600note R9: read each distinct receipt cited by a PASS/FAIL row once and check its HWGATE ids
601fn receiptids to void
602r = get cnt 0
603j = ulong 0
604while lt j r bound 1025
605if le get rst j 1
606seen = samerec j
607if eq seen false
608put fs 5 get rrs j
609put fl 5 get rrl j
610okp = rpath
611pl = ulong 0
612if okp
613pl = mkpath
614end
615if gt pl 0
616el = rdrec pl
617if ge el 0
618scanids ulong el get rln j
619end
620end
621end
622end
623j = add j 1
624end
625end
626
627fn main to int
628if ne argcnt 3
629k = write 2 musage 0 len musage
630ret 2
631end
632n = argread 1 pb 0 4096
633if le n 0
634k = write 2 musage 0 len musage
635ret 2
636end
637rl = rdreg ulong n
638if lt rl 0
639k = write 2 mread 0 len mread
640ret 2
641end
642put cnt 2 ulong rl
643m = argread 2 ab 0 4096
644if le m 0
645k = write 2 musage 0 len musage
646ret 2
647end
648put cnt 3 ulong m
649total = ulong rl
650pos = ulong 0
651ln = ulong 1
652i = ulong 0
653while le i total bound 524290
654eol = true
655if lt i total
656eol = eq get rb i uchar 10
657end
658if eol
659if gt i pos
660if ne get rb pos uchar 35
661row pos i ln
662end
663end
664pos = add i 1
665ln = add ln 1
666end
667i = add i 1
668end
669if eq get cnt 0 0
670fail 0 8
671end
672g = pairs
673receiptids
674r = get cnt 0
675np = ulong 0
676pp = ulong 0
677j = ulong 0
678while lt j r bound 1025
679if eq get rti j 2
680np = add np 1
681if eq get rst j 0
682pp = add pp 1
683end
684end
685j = add j 1
686end
687if gt get cnt 1 0
688k = write 1 mfails 0 len mfails
689pnum get cnt 1
690k = write 1 mrows 0 len mrows
691pnum r
692k = write 1 nl 0 len nl
693ret 1
694end
695k = write 1 mpass 0 len mpass
696pnum r
697k = write 1 mgates 0 len mgates
698pnum g
699k = write 1 mphys 0 len mphys
700pnum np
701k = write 1 mppass 0 len mppass
702pnum pp
703k = write 1 mnrun 0 len mnrun
704pnum sub np pp
705k = write 1 nl 0 len nl
706ret 0
707end