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 / driver / w0.om

554 lines · 11507 bytes Raw Blame History
1note w0: bind-read-release identity probe for one vfio-pci device. Usage: w0 BDF GROUP [HEXOFF]
2note It never writes to the device: vfiomap is read-only and every other access is pread.
3note It never calls DEVICE_RESET (omc refuses it), but Linux vfio-pci resets the device itself:
4note FLR on open when a reset method is enabled, and pci_reset_bus (SBR) on release. See README.
5note Register offsets (open-gpu-kernel-modules 580.173.02, src/common/inc/swref/published/nv_ref.h):
6note NV_PMC_BOOT_0 0x0 :108, NV_PMC_BOOT_1 0x4 :138, NV_PMC_BOOT_42 0xA00 :149.
7note Config space NV_CONFIG_PCI_NV_0 vendor/device 0x0 :52, NV_CONFIG_PCI_NV_2 rev/class 0x8 :66,
8note NV_CONFIG_PCI_NV_11 subsystem 0x2C :81.
9note VFIO ABI: Linux include/uapi/linux/vfio.h; requests are _IO(';', 100+k) = 15204+k.
10text sysdev "/sys/bus/pci/devices/"
11text sgroup "/iommu_group"
12text sdriver "/driver"
13text sgroups "/sys/kernel/iommu_groups/"
14text sdevices "/devices"
15text vfiopci "vfio-pci"
16text vfioc "/dev/vfio/vfio"
17text vfiodir "/dev/vfio/"
18text usage "w0: usage: w0 BDF GROUP [HEXOFF] (BDF like 000f:01:00.0, GROUP decimal)\n"
19text egroup "w0: REFUSED: device iommu_group does not match GROUP\n"
20text edriver "w0: REFUSED: device is not bound to vfio-pci\n"
21text esingle "w0: REFUSED: iommu group must hold exactly this one device\n"
22text eapi "w0: REFUSED: VFIO API version or TYPE1v2 extension missing\n"
23text eviable "w0: REFUSED: group not viable\n"
24text econt "w0: REFUSED: could not attach group to container or set TYPE1v2\n"
25text edev "w0: REFUSED: could not get device fd\n"
26text einfo "w0: REFUSED: device info or region info unusable\n"
27text eones "w0: FAIL: BOOT_0 reads 0xffffffff (device not responding)\n"
28text emis "w0: FAIL: mapped and pread values differ\n"
29text emap "w0: FAIL: BAR0 vfiomap returned errno "
30text lbdf "w0: device "
31text lgroup " group "
32text ldev "w0: device flags "
33text lreg " regions "
34text lirq " irqs "
35text lbar "w0: BAR0 flags "
36text lsize " size "
37text loff " offset "
38text lcfg "w0: CONFIG offset "
39text lb0 "w0: BOOT_0 [0x000] "
40text lb1 "w0: BOOT_1 [0x004] "
41text lb42 "w0: BOOT_42 [0xa00] "
42text lvd "w0: config vendor/device [0x00] "
43text lcls "w0: config rev/class [0x08] "
44text lsub "w0: config subsystem [0x2c] "
45text lx "w0: extra [0x"
46text lxe "] "
47text lmap " mapget "
48text lpre " pread "
49text ldone "w0: released device, group and container\n"
50text nl "\n"
51global bdf uchar 32
52global grp uchar 8
53global xarg uchar 20
54global path uchar 256
55global plen ulong 1
56global lnk uchar 256
57global dents uchar 4096
58global out uchar 64
59global st uchar 8
60global val uchar 4
61global dinfo uchar 24
62global rbar uchar 32
63global rcfg uchar 32
64global word uchar 4
65global nums ulong 8
66global fds long 4
67fn fail code int msg int to void
68note msg selects the refusal line
69if eq msg 2
70r = write 2 usage 0 len usage
71end
72if eq msg 3
73r = write 2 egroup 0 len egroup
74end
75if eq msg 4
76r = write 2 edriver 0 len edriver
77end
78if eq msg 5
79r = write 2 esingle 0 len esingle
80end
81if eq msg 6
82r = write 2 eapi 0 len eapi
83end
84if eq msg 7
85r = write 2 eviable 0 len eviable
86end
87if eq msg 8
88r = write 2 econt 0 len econt
89end
90if eq msg 9
91r = write 2 edev 0 len edev
92end
93if eq msg 10
94r = write 2 einfo 0 len einfo
95end
96exit code
97end
98fn hexd v ulong to uchar
99if lt v 10
100ret uchar add v 48
101end
102ret uchar add v 87
103end
104note print 0x + 8 hex digits (or 16 when wide), then optionally a newline
105fn hex v ulong wide bool newline bool to void
106n = ulong 8
107if wide
108n = 16
109end
110put out 0 uchar 48
111put out 1 uchar 120
112for i ulong n
113put out add 2 i hexd and shr v mul sub sub n 1 i 4 15
114end
115k = add n 2
116if newline
117put out k uchar 10
118k = add k 1
119end
120r = write 1 out 0 k
121end
122fn le32 a ulong which int to ulong
123v = ulong 0
124if eq which 0
125v = ulong get dinfo add a 3
126v = or shl v 8 ulong get dinfo add a 2
127v = or shl v 8 ulong get dinfo add a 1
128ret or shl v 8 ulong get dinfo a
129end
130if eq which 1
131v = ulong get rbar add a 3
132v = or shl v 8 ulong get rbar add a 2
133v = or shl v 8 ulong get rbar add a 1
134ret or shl v 8 ulong get rbar a
135end
136if eq which 2
137v = ulong get rcfg add a 3
138v = or shl v 8 ulong get rcfg add a 2
139v = or shl v 8 ulong get rcfg add a 1
140ret or shl v 8 ulong get rcfg a
141end
142if eq which 3
143v = ulong get st add a 3
144v = or shl v 8 ulong get st add a 2
145v = or shl v 8 ulong get st add a 1
146ret or shl v 8 ulong get st a
147end
148v = ulong get word 3
149v = or shl v 8 ulong get word 2
150v = or shl v 8 ulong get word 1
151ret or shl v 8 ulong get word 0
152end
153fn le64 a ulong which int to ulong
154ret or le32 a which shl le32 add a 4 which 32
155end
156fn setval v ulong to void
157put val 0 uchar and v 255
158put val 1 uchar and shr v 8 255
159put val 2 uchar and shr v 16 255
160put val 3 uchar and shr v 24 255
161end
162note path building: a fresh path, then pieces; the terminator-free length is plen
163fn preset to void
164put plen 0 0
165end
166fn padd c uchar to void
167p = get plen 0
168put path p c
169put plen 0 add p 1
170end
171fn ptext k int to void
172if eq k 0
173for i ulong len sysdev
174padd get sysdev i
175end
176end
177if eq k 1
178for i ulong len sgroup
179padd get sgroup i
180end
181end
182if eq k 2
183for i ulong len sdriver
184padd get sdriver i
185end
186end
187if eq k 3
188for i ulong len sgroups
189padd get sgroups i
190end
191end
192if eq k 4
193for i ulong len sdevices
194padd get sdevices i
195end
196end
197if eq k 5
198for i ulong len vfiodir
199padd get vfiodir i
200end
201end
202end
203fn pbdf n ulong to void
204for i ulong n
205padd get bdf i
206end
207end
208fn pgrp n ulong to void
209for i ulong n
210padd get grp i
211end
212end
213note the final path component of lnk[0..n) must equal grp[0..g) (which 0) or "vfio-pci" (which 1)
214fn lastis n ulong g ulong which int to bool
215s = ulong 0
216for i ulong n
217if eq get lnk i 47
218s = add i 1
219end
220end
221m = sub n s
222if eq which 0
223if ne m g
224ret false
225end
226for i ulong g
227if ne get lnk add s i get grp i
228ret false
229end
230end
231ret true
232end
233if ne m len vfiopci
234ret false
235end
236for i ulong m
237if ne get lnk add s i get vfiopci i
238ret false
239end
240end
241ret true
242end
243note name at dents[o..] (zero-terminated) equals bdf[0..n)
244fn nameis o ulong n ulong to bool
245for i ulong n
246if ne get dents add o i get bdf i
247ret false
248end
249end
250ret eq get dents add o n 0
251end
252fn isdot o ulong to bool
253if ne get dents o 46
254ret false
255end
256if eq get dents add o 1 0
257ret true
258end
259ret and eq get dents add o 1 46 eq get dents add o 2 0
260end
261fn readcfg dev int base ulong reg ulong to ulong
262r = pread dev word 0 4 long add base reg
263if ne r 4
264fail 10 10
265end
266ret le32 0 4
267end
268note stage 0: argv. nums: 0 bdf length, 1 group length, 2 extra length, 3 extra offset, 4 extra given
269fn args to void
270ac = argcnt
271if or lt ac 3 gt ac 4
272fail 2 2
273end
274bn = argread 1 bdf 0 32
275gn = argread 2 grp 0 8
276if or ne bn 12 or le gn 0 gt gn 6
277fail 2 2
278end
279put nums 0 ulong bn
280put nums 1 ulong gn
281for i ulong 12
282c = get bdf i
283ok = or and ge c 48 le c 57 and ge c 97 le c 102
284if or eq i 4 eq i 7
285ok = eq c 58
286end
287if eq i 10
288ok = eq c 46
289end
290if not ok
291fail 2 2
292end
293end
294for i ulong ulong gn
295c = get grp i
296if or lt c 48 gt c 57
297fail 2 2
298end
299end
300if eq ac 4
301xn = argread 3 xarg 0 16
302if or le xn 0 gt xn 16
303fail 2 2
304end
305x = ulong 0
306for i ulong ulong xn
307c = get xarg i
308d = ulong 99
309if and ge c 48 le c 57
310d = ulong sub c 48
311end
312if and ge c 97 le c 102
313d = ulong sub c 87
314end
315if eq d 99
316fail 2 2
317end
318x = or shl x 4 d
319end
320put nums 2 ulong xn
321put nums 3 x
322put nums 4 1
323end
324end
325note stage 1: sysfs. iommu_group must be GROUP and the driver must be vfio-pci
326fn sysfs to void
327b = get nums 0
328g = get nums 1
329preset
330ptext 0
331pbdf b
332ptext 1
333n = readlnk path 0 get plen 0 lnk 0 256
334if or le n 0 ge n 256
335fail 3 3
336end
337if not lastis ulong n g 0
338fail 3 3
339end
340preset
341ptext 0
342pbdf b
343ptext 2
344n = readlnk path 0 get plen 0 lnk 0 256
345if or le n 0 ge n 256
346fail 4 4
347end
348if not lastis ulong n 0 1
349fail 4 4
350end
351end
352note stage 2: /sys/kernel/iommu_groups/GROUP/devices must list exactly this device
353fn single to void
354b = get nums 0
355preset
356ptext 3
357pgrp get nums 1
358ptext 4
359dfd = fopen path 0 get plen 0 540672 0
360if lt dfd 0
361fail 5 5
362end
363count = ulong 0
364other = ulong 0
365while true bound 4096
366r = getdnts int dfd dents 0 4096
367if lt r 0
368fail 5 5
369end
370if eq r 0
371break
372end
373o = ulong 0
374while lt o ulong r bound 4096
375rl = ulong get dents add o 17
376rl = or shl rl 8 ulong get dents add o 16
377if eq rl 0
378fail 5 5
379end
380if not isdot add o 19
381count = add count 1
382if not nameis add o 19 b
383other = add other 1
384end
385end
386o = add o rl
387end
388end
389r = fclose int dfd
390if or ne count 1 ne other 0
391fail 5 5
392end
393end
394note stage 3: container API 0 + TYPE1v2, group viable, attach, set TYPE1v2, device fd by name
395fn attach to void
396cfd = fopen vfioc 0 len vfioc 524290 0
397if lt cfd 0
398fail 6 6
399end
400ci = int cfd
401put fds 0 cfd
402if ne vfioctl ci uint 15204 val 0 0 0
403fail 6 6
404end
405setval 3
406if ne vfioctl ci uint 15205 val 0 4 1
407fail 6 6
408end
409preset
410ptext 5
411pgrp get nums 1
412gfd = fopen path 0 get plen 0 524290 0
413if lt gfd 0
414fail 7 7
415end
416gi = int gfd
417put fds 1 gfd
418if ne vfioctl gi uint 15207 st 0 8 0
419fail 7 7
420end
421if eq and le32 4 3 1 0
422fail 7 7
423end
424setval ulong cfd
425if ne vfioctl gi uint 15208 val 0 4 0
426fail 8 8
427end
428setval 3
429if ne vfioctl ci uint 15206 val 0 4 0
430fail 8 8
431end
432dv = vfioctl gi uint 15210 bdf 0 get nums 0
433if lt dv 0
434fail 9 9
435end
436put fds 2 dv
437r = write 1 lbdf 0 len lbdf
438r = write 1 bdf 0 get nums 0
439r = write 1 lgroup 0 len lgroup
440r = write 1 grp 0 get nums 1
441r = write 1 nl 0 len nl
442end
443note stage 4: device info (PCI flag 2, >= 8 regions), BAR0 (index 0) and CONFIG (index 7) region info
444fn info to void
445di = int get fds 2
446if ne vfioctl di uint 15211 dinfo 0 24 0
447fail 10 10
448end
449df = le32 4 0
450nr = le32 8 0
451r = write 1 ldev 0 len ldev
452hex df false false
453r = write 1 lreg 0 len lreg
454hex nr false false
455r = write 1 lirq 0 len lirq
456hex le32 12 0 false true
457if or eq and df 2 0 lt nr 8
458fail 10 10
459end
460put rbar 8 0
461if ne vfioctl di uint 15212 rbar 0 32 0
462fail 10 10
463end
464put rcfg 8 7
465if ne vfioctl di uint 15212 rcfg 0 32 0
466fail 10 10
467end
468bf = le32 4 1
469bs = le64 16 1
470r = write 1 lbar 0 len lbar
471hex bf false false
472r = write 1 lsize 0 len lsize
473hex bs true false
474r = write 1 loff 0 len loff
475hex le64 24 1 true true
476r = write 1 lcfg 0 len lcfg
477hex le64 24 2 true true
478if or eq and bf 1 0 lt bs 4096
479fail 10 10
480end
481end
482note stage 5: config identity through pread of the CONFIG region (reads only)
483fn config to void
484di = int get fds 2
485co = le64 24 2
486r = write 1 lvd 0 len lvd
487hex readcfg di co 0 false true
488r = write 1 lcls 0 len lcls
489hex readcfg di co 8 false true
490r = write 1 lsub 0 len lsub
491hex readcfg di co 44 false true
492end
493note stage 6: BOOT registers by pread of BAR0, then through the read-only map; release in reverse order
494fn boot to int
495di = int get fds 2
496bo = le64 24 1
497p0 = readcfg di bo 0
498p1 = readcfg di bo 4
499p42 = readcfg di bo 2560
500h = vfiomap di rbar 0 32
501if lt h 0
502r = write 2 emap 0 len emap
503hex ulong neg h false true
504exit 12
505end
506m0 = mapget h 0 4
507m1 = mapget h 4 4
508m42 = mapget h 2560 4
509r = write 1 lb0 0 len lb0
510r = write 1 lmap 0 len lmap
511hex m0 false false
512r = write 1 lpre 0 len lpre
513hex p0 false true
514r = write 1 lb1 0 len lb1
515r = write 1 lmap 0 len lmap
516hex m1 false false
517r = write 1 lpre 0 len lpre
518hex p1 false true
519r = write 1 lb42 0 len lb42
520r = write 1 lmap 0 len lmap
521hex m42 false false
522r = write 1 lpre 0 len lpre
523hex p42 false true
524if eq get nums 4 1
525r = write 1 lx 0 len lx
526r = write 1 xarg 0 get nums 2
527r = write 1 lxe 0 len lxe
528r = write 1 lmap 0 len lmap
529hex mapget h get nums 3 4 false true
530end
531r = mapdrop h
532r = fclose di
533r = fclose int get fds 1
534r = fclose int get fds 0
535r = write 1 ldone 0 len ldone
536if eq m0 4294967295
537r = write 2 eones 0 len eones
538ret 11
539end
540if or or ne m0 p0 ne m1 p1 ne m42 p42
541r = write 2 emis 0 len emis
542ret 13
543end
544ret 0
545end
546fn main to int
547args
548sysfs
549single
550attach
551info
552config
553ret boot
554end