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 / gpu / hw / main.om

927 lines · 16330 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note main.om: hwman unit 4, commands and the target lifecycle
3note discover -> claim -> prepare -> submit -> complete -> quiesce -> release
4note Usage (MAN is the manifest; every command checks it first and refuses a malformed one, exit 3):
5note hwman check MAN schema and rule check; prints CHECK rows families admitted
6note hwman doctor MAN one TARGET line per row
7note hwman admit MAN OP [TARGET] admission: TARGET (or the first admitted row offering
8note OP) must exist, offer OP, be admitted and have its
9note device node present
10note hwman run MAN TARGET OP ARG GEN STATE [EXPECT]
11note hwman fault MAN TARGET KIND GEN STATE send a malformed frame (tpm: badsize, badcc); passes only
12note on the one expected refusal: badsize kernel errno -22,
13note badcc rc 0x000b0143 (TPM2_RC_COMMAND_CODE from the
14note kernel resource manager layer, see tpm.om)
15note check binds every hwman- runner to the closed table in core.om (TRUN: family, route, operations),
16note so check, admit and run all use that one table.
17note STATE is the handle file "<generation> <target>\n" (created empty = generation 0); claim takes an
18note exclusive non-blocking flock on it; GEN must equal the stored generation. Before submission byte 0
19note becomes 'x' (synced); release then writes and syncs bytes 1.., then byte 0, then syncs again.
20note Any failed or short write, or failed sync, refuses with 74 instead of PASS; the handle is then
21note unreadable (exit 7), or after only the last sync failed holds the advanced record: never an
22note earlier generation. A replayed GEN is stale. timeout_ms is overrun detection, not cancellation:
23note CPU compares the elapsed time after the operation returns; TPM bounds the wait for the response
24note (waitio) and refuses, but the command already sent is not cancelled in the chip.
25note EXPECT is the independent oracle value (decimal).
26note Exit: 0 PASS, 2 usage, 3 manifest refused, 4 unknown target, 5 unsupported op, 6 not admitted,
27note 7 stale generation or handle, 8 wrong device, 9 busy, 10 offline, 11 timeout,
28note 12 oracle mismatch, 13 device error, 14 invalid argument, 74 I/O. Traps exit 100 + code.
29
30text MUSE "usage: hwman check MAN | doctor MAN | admit MAN OP [TARGET] | run MAN TARGET OP ARG GEN STATE [EXPECT] | fault MAN TARGET KIND GEN STATE\n"
31text MCHECK "check"
32text MDOCTOR "doctor"
33text MADMIT "admit"
34text MRUN "run"
35text MFAULT "fault"
36text MRCPU "hwman-cpu"
37text MRTPM "hwman-tpm"
38text ORC " rc="
39text OSTWR "state_write_failed state="
40text OFRES "unexpected_fault_result kind="
41text MYES "YES"
42text OADM " admit="
43text OCHECK "CHECK rows="
44text OFAMS " families=21 admitted="
45text OTARGET "TARGET "
46text OADMIT "ADMIT target="
47text ORUNNER " runner="
48text OROUTE " route="
49text OOP " op="
50text OSTATUS " status="
51text OBLOCK " blocker="
52text OFAM " family="
53text OUNKNOWN "unknown_target target="
54text OUNSUP "unsupported_op target="
55text ONOADM "not_admitted target="
56text ONOTGT "no_admitted_target op="
57text OSTWRONG "wrong_handle target="
58text OOFFLINE "offline target="
59text OERR " errno="
60text OINVAL "invalid_argument arg="
61text OBUSY "busy state="
62text OSTALE "stale_generation have="
63text OSTCORR "state_unreadable state="
64text OSTATEQ " state="
65text OWANT " want="
66text OUNPIN "wrong_device unpinned cpus_allowed_list"
67text OMOVED "wrong_device processor="
68text OPIN " pinned="
69text OMIDR "wrong_device midr="
70text OEXPECT " manifest="
71text OMANUF "wrong_device tpm_manufacturer="
72text OTIME "timeout elapsed_ns="
73text OBUDGET " budget_ns="
74text OMISM "oracle_mismatch result="
75text ODEV "device_error rc="
76text OSTEP "STEP "
77text SDISC "discover"
78text SCLAIM "claim"
79text SPREP "prepare"
80text SSUB "submit"
81text SCOMP "complete"
82text SQUI "quiesce"
83text SREL "release"
84text OCPU " cpu="
85text OMIDREQ " midr_el1="
86text OMANEQ " tpm_manufacturer="
87text OFW " firmware="
88text OREV " revision="
89text OGEN " generation="
90text ORES " result="
91text OELAP " elapsed_ns="
92text ORECEIPT "RECEIPT target="
93text OARROW "->"
94text OARG " arg="
95text OOUT " outcome="
96text OPASS "PASS"
97text OFAULT "FAULT target="
98text OKIND " kind="
99text OLAYK " layer=kernel errno="
100text OLAYT " layer=tpmrm rc="
101text OBADK "unsupported_fault kind="
102text ODASH "-"
103
104global gstate uchar 256
105global gtmp uchar 32
106
107fn refuse code int why view uchar * to int
108ot TREF
109ot why
110ret code
111end
112
113fn rend code int to int
114oc 10
115oflush
116ret code
117end
118
119note the STATE argument s could not be made durable: refuse with I/O, no PASS
120fn stfail s ulong to int
121c = refuse XIO OSTWR
122oarg s
123ret rend c
124end
125
126note the row's device node exists (faccess F_OK); 0 or -errno
127fn nodeok to long
128pclear
129pfield CNODE
130ret faccess gpath 0 get gst 5 0
131end
132
133note gate target argument t for op argument o (o = 0: no op check); leaves the row split
134fn gate t ulong o ulong to int
135r = findrow t
136if lt r 0
137c = refuse XUNKNOWN OUNKNOWN
138oarg t
139ret rend c
140end
141if gt o 0
142if not flisthas COPS o
143c = refuse XUNSUP OUNSUP
144ofield CID
145ot OOP
146oarg o
147ret rend c
148end
149end
150if not feq CADMIT MYES
151c = refuse XAUTH ONOADM
152ofield CID
153ot OSTATUS
154ofield CSTAT
155ot OBLOCK
156ofield CBLOCK
157ret rend c
158end
159note a runner outside the closed table is a manifest refusal in check (exit 3)
160e = nodeok
161if lt e 0
162c = refuse XOFFLINE OOFFLINE
163ofield CID
164ot OERR
165osdec e
166ret rend c
167end
168ret XOK
169end
170
171fn cmdcheck to int
172c = check
173if ne c XOK
174ret c
175end
176ot OCHECK
177odec get gst 3
178ot OFAMS
179odec get gst 8
180ret rend XOK
181end
182
183fn cmddoctor to int
184c = check
185if ne c XOK
186ret c
187end
188for r ulong get gst 3
189q = rowat get grow r
190ot OTARGET
191ofield CID
192ot OFAM
193ofield CFAM
194ot OROUTE
195ofield CROUTE
196ot OSTATUS
197ofield CSTAT
198ot OADM
199ofield CADMIT
200ot ORUNNER
201ofield CRUNNER
202ot OOP
203ofield COPS
204oc 10
205end
206ot OCHECK
207odec get gst 3
208ot OFAMS
209odec get gst 8
210ret rend XOK
211end
212
213fn oadmit to void
214ot OADMIT
215ofield CID
216ot OFAM
217ofield CFAM
218ot OROUTE
219ofield CROUTE
220ot ORUNNER
221ofield CRUNNER
222end
223
224fn cmdadmit to int
225c = check
226if ne c XOK
227ret c
228end
229if eq argc 5
230g = gate 4 3
231if ne g XOK
232ret g
233end
234oadmit
235ot OOP
236oarg 3
237ret rend XOK
238end
239for r ulong get gst 3
240q = rowat get grow r
241if and flisthas COPS 3 feq CADMIT MYES
242if ge nodeok 0
243oadmit
244ot OOP
245oarg 3
246ret rend XOK
247end
248end
249end
250c = refuse XUNSUP ONOTGT
251oarg 3
252ret rend c
253end
254
255note decimal text of v in gtmp; returns its length
256fn tmpdec v ulong to ulong
257d = new uchar 20 0
258k = ulong 0
259x = v
260while true bound 21
261put d k uchar add rem x 10 48
262k = add k 1
263x = div x 10
264if eq x 0
265break
266end
267end
268for i ulong k
269put gtmp i get d sub sub k i 1
270end
271ret k
272end
273
274note parse the n handle-file bytes in gstate, "<generation> <target>\n": the generation, -1 when
275note malformed, -2 when the stored target is not the split row's id
276fn stparse n ulong to long
277i = ulong 0
278have = ulong 0
279while true bound 20
280if ge i n
281break
282end
283ch = get gstate i
284if or lt ch 48 gt ch 57
285break
286end
287have = add mul have 10 ulong sub ch 48
288i = add i 1
289end
290if or eq i 0 ge i n
291ret -1
292end
293if ne get gstate i 32
294ret -1
295end
296o = add i 1
297idl = get gfl CID
298if ne add add o idl 1 n
299ret -2
300end
301if ne get gstate add o idl 10
302ret -2
303end
304fo = get gfo CID
305for j ulong idl
306if ne get gstate add o j get gman add fo j
307ret -2
308end
309end
310ret long have
311end
312
313fn unclaim fd int to void
314u = flock fd 8
315c = fclose fd
316end
317
318note claim: open and exclusively lock STATE (argument s); check generation argument g against it.
319note Returns the descriptor (>= 0) or minus an exit class.
320fn claim s ulong g ulong to long
321want = argdec g
322if lt want 0
323c = refuse XINVAL OINVAL
324oarg g
325ret long sub 0 rend c
326end
327pclear
328parg s
329fd = fopen gpath 0 get gst 5 66 384
330if lt fd 0
331c = refuse XIO OSTCORR
332oarg s
333ret long sub 0 rend c
334end
335k = flock int fd 6
336if lt k 0
337c = refuse XBUSY OBUSY
338oarg s
339cl = fclose int fd
340ret long sub 0 rend c
341end
342n = pread int fd gstate 0 len gstate 0
343have = long 0
344if gt n 0
345have = stparse ulong n
346end
347if lt n 0
348have = long -1
349end
350if eq have -2
351unclaim int fd
352c = refuse XSTALE OSTWRONG
353ofield CID
354ot OSTATEQ
355oarg s
356ret long sub 0 rend c
357end
358if lt have 0
359unclaim int fd
360c = refuse XSTALE OSTCORR
361oarg s
362ret long sub 0 rend c
363end
364if ne have want
365unclaim int fd
366c = refuse XSTALE OSTALE
367osdec have
368ot OWANT
369osdec want
370ret long sub 0 rend c
371end
372ot OSTEP
373ot SCLAIM
374ot OGEN
375osdec have
376oc 10
377put gst 15 ulong have
378ret fd
379end
380
381note inflight: before submission byte 0 of the handle becomes 'x' and is synced, so the handle no
382note longer parses (state_unreadable, exit 7) until release completes; true when both succeeded
383fn inflight fd int to bool
384put gtmp 0 120
385w = pwrite fd gtmp 0 1 0
386if ne w 1
387ret false
388end
389s = fsync fd
390ret eq s 0
391end
392
393note release: when bump, write "<generation+1> <id>\n" over the in-flight handle, bytes 1.. first and
394note synced, then byte 0 and synced, so a failed, short or unsynced write leaves byte 0 'x' and the
395note handle refused. The generation only grows, so the record never shrinks and needs no truncation.
396note Unlocks and closes; returns XOK, or XIO when a write was short or failed or a sync failed.
397fn release fd int bump bool to int
398code = XOK
399if bump
400k = tmpdec add get gst 15 1
401for i ulong k
402put gstate i get gtmp i
403end
404put gstate k 32
405o = get gfo CID
406l = get gfl CID
407for j ulong l
408put gstate add add k 1 j get gman add o j
409end
410put gstate add add k 1 l 10
411m = add add k 1 l
412w = pwrite fd gstate 1 m 1
413s = fsync fd
414ok = and eq w long m eq s 0
415if ok
416w = pwrite fd gstate 0 1 0
417s = fsync fd
418ok = and eq w 1 eq s 0
419end
420if not ok
421code = XIO
422end
423end
424u = flock fd 8
425c = fclose fd
426if ne code XOK
427ret code
428end
429ot OSTEP
430ot SREL
431ot OGEN
432if bump
433odec add get gst 15 1
434else
435odec get gst 15
436end
437oc 10
438ret XOK
439end
440
441fn oreceipt a ulong o ulong to void
442ot ORECEIPT
443ofield CID
444ot OFAM
445ofield CFAM
446ot OROUTE
447ofield CROUTE
448ot ORUNNER
449ofield CRUNNER
450ot OOP
451oarg o
452ot OARG
453oarg a
454ot OGEN
455odec get gst 15
456ot OARROW
457odec add get gst 15 1
458end
459
460note decimal text of v equals argument a
461fn expok a ulong v ulong to bool
462k = tmpdec v
463if ne k get gal a
464ret false
465end
466o = get gao a
467for i ulong k
468if ne get gtmp i get gav add o i
469ret false
470end
471end
472ret true
473end
474
475note CPU discovery: pinned processor, current processor and MIDR_EL1 against the manifest identity.
476note Returns the processor (MIDR in gst 10) or minus an exit class.
477fn cpudisc to long
478pin = cpupin
479if lt pin 0
480c = refuse XWRDEV OUNPIN
481ret long sub 0 rend c
482end
483cur = cpucur
484if ne cur pin
485c = refuse XWRDEV OMOVED
486osdec cur
487ot OPIN
488osdec pin
489ret long sub 0 rend c
490end
491m = cpumidr ulong pin
492if lt m 0
493c = refuse XOFFLINE OOFFLINE
494ofield CID
495ot OERR
496osdec m
497ret long sub 0 rend c
498end
499if ne m fidhex
500c = refuse XWRDEV OMIDR
501ohx ulong m 8
502ot OEXPECT
503ofield CIDENT
504ot OCPU
505osdec pin
506ret long sub 0 rend c
507end
508ot OSTEP
509ot SDISC
510ot OCPU
511osdec pin
512ot OMIDREQ
513ohx ulong m 8
514oc 10
515put gst 10 ulong m
516ret pin
517end
518
519note CPU lifecycle for argv: 2 MAN 3 TARGET 4 OP 5 ARG 6 GEN 7 STATE [8 EXPECT]
520fn runcpu to int
521pin = cpudisc
522if lt pin 0
523ret int sub 0 pin
524end
525m = get gst 10
526fd = claim 7 6
527if lt fd 0
528ret int sub 0 fd
529end
530n = argdec 5
531if or lt n 1 gt n long CPUMAXN
532x = release int fd false
533c = refuse XINVAL OINVAL
534oarg 5
535ret rend c
536end
537ot OSTEP
538ot SPREP
539ot OARG
540odec ulong n
541oc 10
542if not inflight int fd
543x = release int fd false
544ret stfail 7
545end
546tmo = mul ulong fdec CTMO 1000000
547t0 = monons
548v = cpuop 4 ulong n
549t1 = monons
550el = ulong sub t1 t0
551if eq get gst 9 1
552x = release int fd false
553c = refuse XUNSUP OUNSUP
554ofield CID
555ot OOP
556oarg 4
557ret rend c
558end
559ot OSTEP
560ot SSUB
561ot OELAP
562odec el
563oc 10
564code = XOK
565if eq argc 9
566if not expok 8 v
567code = XMISM
568end
569end
570ot OSTEP
571ot SCOMP
572ot ORES
573odec v
574oc 10
575cur2 = cpucur
576if ne cur2 pin
577code = XWRDEV
578end
579if gt el tmo
580code = XTIMEOUT
581end
582ot OSTEP
583ot SQUI
584ot OCPU
585osdec cur2
586ot OELAP
587odec el
588ot OBUDGET
589odec tmo
590oc 10
591rl = release int fd true
592if ne rl XOK
593ret stfail 7
594end
595oreceipt 5 4
596ot OCPU
597odec ulong pin
598ot OMIDREQ
599ohx ulong m 8
600ot ORES
601odec v
602ot OELAP
603odec el
604if eq code XOK
605ot OOUT
606ot OPASS
607ret rend XOK
608end
609oc 10
610if eq code XMISM
611c = refuse code OMISM
612odec v
613ot OWANT
614oarg 8
615ret rend c
616end
617if eq code XTIMEOUT
618c = refuse code OTIME
619odec el
620ot OBUDGET
621odec tmo
622ret rend c
623end
624c = refuse code OMOVED
625osdec cur2
626ot OPIN
627osdec pin
628ret rend c
629end
630
631note TPM discovery: open the node, read the fixed properties, compare the manufacturer.
632note Returns the descriptor or minus an exit class.
633fn tpmdisc to long
634fd = tpmopen
635if lt fd 0
636code = XOFFLINE
637if or eq fd -13 eq fd -1
638code = XAUTH
639end
640c = refuse code OOFFLINE
641ofield CID
642ot OERR
643osdec fd
644ret long sub 0 rend c
645end
646tmo = long mul ulong fdec CTMO 1000000
647r = tpmprops int fd tmo
648if ne r 0
649cl = fclose int fd
650code = XDEVERR
651if eq r TPMETIME
652code = XTIMEOUT
653end
654c = refuse code ODEV
655osdec r
656ret long sub 0 rend c
657end
658want = fidhex
659if ne long get gst 10 want
660cl = fclose int fd
661c = refuse XWRDEV OMANUF
662ohx get gst 10 8
663ot OEXPECT
664ofield CIDENT
665ret long sub 0 rend c
666end
667ot OSTEP
668ot SDISC
669ot OMANEQ
670ohx get gst 10 8
671ot OFW
672ohx get gst 11 8
673oc 46
674ohx get gst 12 8
675ot OREV
676odec get gst 13
677oc 10
678ret fd
679end
680note TPM result: the fixed properties, or the n random bytes in hex
681fn tpmout isprops bool n ulong to void
682if isprops
683ot OMANEQ
684ohx get gst 10 8
685ot OFW
686ohx get gst 11 8
687oc 46
688ohx get gst 12 8
689ret
690end
691ot ORES
692for i ulong n
693b = ulong get gresp add 12 i
694oc hexc shr b 4
695oc hexc and b 15
696end
697end
698
699note TPM prepare: the operand of argument 5 (props: 0; getrandom: 1..TPMMAXR), or -1
700fn tpmarg isprops bool to long
701n = argdec 5
702if lt n 0
703ret -1
704end
705if isprops
706if ne n 0
707ret -1
708end
709ret n
710end
711if or lt n 1 gt n long TPMMAXR
712ret -1
713end
714ret n
715end
716
717fn closefd fd int to void
718c = fclose fd
719end
720
721fn runtpm to int
722fd = tpmdisc
723if lt fd 0
724ret int sub 0 fd
725end
726sf = claim 7 6
727if lt sf 0
728closefd int fd
729ret int sub 0 sf
730end
731isprops = argeq 4 TPMPROPS
732n = tpmarg isprops
733if lt n 0
734x = release int sf false
735closefd int fd
736c = refuse XINVAL OINVAL
737oarg 5
738ret rend c
739end
740ot OSTEP
741ot SPREP
742ot OARG
743odec ulong n
744oc 10
745if not inflight int sf
746closefd int fd
747x = release int sf false
748ret stfail 7
749end
750tmo = long mul ulong fdec CTMO 1000000
751t0 = monons
752r = long 0
753if isprops
754r = tpmprops int fd tmo
755else
756r = tpmrand int fd ulong n tmo
757end
758el = ulong sub monons t0
759ot OSTEP
760ot SSUB
761ot OELAP
762odec el
763oc 10
764ot OSTEP
765ot SCOMP
766ot ORC
767osdec r
768oc 10
769closefd int fd
770ot OSTEP
771ot SQUI
772ot OELAP
773odec el
774ot OBUDGET
775odec ulong tmo
776oc 10
777rl = release int sf true
778if ne rl XOK
779ret stfail 7
780end
781oreceipt 5 4
782if and eq r 0 le el ulong tmo
783tpmout isprops ulong n
784ot OELAP
785odec el
786ot OOUT
787ot OPASS
788ret rend XOK
789end
790oc 10
791if or eq r TPMETIME gt el ulong tmo
792c = refuse XTIMEOUT OTIME
793odec el
794ot OBUDGET
795odec ulong tmo
796ret rend c
797end
798c = refuse XDEVERR ODEV
799osdec r
800ret rend c
801end
802
803fn cmdfault to int
804c = check
805if ne c XOK
806ret c
807end
808g = gate 3 0
809if ne g XOK
810ret g
811end
812okk = or argeq 4 TPMBSZ argeq 4 TPMBADCC
813if not and okk feq CRUNNER MRTPM
814c = refuse XUNSUP OBADK
815oarg 4
816ret rend c
817end
818fd = tpmdisc
819if lt fd 0
820ret int sub 0 fd
821end
822sf = claim 6 5
823if lt sf 0
824cl = fclose int fd
825ret int sub 0 sf
826end
827if not inflight int sf
828cl = fclose int fd
829x = release int sf false
830ret stfail 5
831end
832tmo = long mul ulong fdec CTMO 1000000
833t0 = monons
834r = tpmfault int fd 4 tmo
835el = ulong sub monons t0
836cl = fclose int fd
837rl = release int sf true
838if ne rl XOK
839ret stfail 5
840end
841if eq r TPMETIME
842c = refuse XTIMEOUT OTIME
843odec el
844ot OBUDGET
845odec ulong tmo
846ret rend c
847end
848want = long 0xb0143
849if argeq 4 TPMBSZ
850want = long -22
851end
852if ne r want
853c = refuse XDEVERR OFRES
854oarg 4
855ot ORES
856osdec r
857ot OWANT
858osdec want
859ret rend c
860end
861ot OFAULT
862ofield CID
863ot OKIND
864oarg 4
865if lt r 0
866ot OLAYK
867osdec r
868else
869ot OLAYT
870ohx ulong r 8
871end
872ot OOUT
873ot OPASS
874ret rend XOK
875end
876
877fn cmdrun to int
878c = check
879if ne c XOK
880ret c
881end
882g = gate 3 4
883if ne g XOK
884ret g
885end
886okop = argin 4 TOPTPM
887if feq CRUNNER MRCPU
888okop = argin 4 TOPCPU
889end
890if not okop
891c = refuse XUNSUP OUNSUP
892ofield CID
893ot OOP
894oarg 4
895ret rend c
896end
897if feq CRUNNER MRCPU
898ret runcpu
899end
900ret runtpm
901end
902
903fn main to int
904loadargs
905n = argc
906if lt n 3
907fail XUSAGE MUSE
908end
909readman 2
910if and argeq 1 MCHECK eq n 3
911ret cmdcheck
912end
913if and argeq 1 MDOCTOR eq n 3
914ret cmddoctor
915end
916if and argeq 1 MADMIT or eq n 4 eq n 5
917ret cmdadmit
918end
919if and argeq 1 MRUN or eq n 8 eq n 9
920ret cmdrun
921end
922if and argeq 1 MFAULT eq n 7
923ret cmdfault
924end
925fail XUSAGE MUSE
926ret XUSAGE
927end