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

1184 lines · 21122 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note gpu/nvidia/driver/client.om: the setup and teardown sequences of CUDApp nvrm/nvrm.c and m16/m16_native.c
3note (84b524ca) that were still C: nvrm_open, quarantine_allocation, alloc_with_cacheability, nvrm_free,
4note chan_extra, chan_extra_destroy, nvrm_channel, nvrm_channel_n, nvrm_channel_destroy, nvrm_enqueue,
5note nvrm_chan_submit, nvrm_close and m16_native_free_memory, and the wrappers around them that were marked
6note device shell: nvrm_ring, nvrm_submit, m16_native_open, m16_native_create_channel, m16_native_ring_doorbell
7note and m16_native_close. Each decides which driver command comes next, with which request fields, and how a
8note refusal is unwound; the command itself is a dx_ effect.
9note
10note The dx_ effects are declared by the part composed with this one. fx.om answers them from a script and
11note records each one (the host gate, seqgate.om). No device part exists yet: DEVICE.md lists, per effect, what
12note it needs from the hardware and which existing Omega unit already does it. Request fields go to dx_fk (struct
13note kind, as nvrm_struct.om numbers them) and dx_fa (one field) before the effect that sends them.
14note
15note One client per process: its state is the globals below (the C keeps it in one Nvrm). Memory descriptors
16note (NvrmMem: handle, va, size, cpu) are rows of cm: 0 the main ring, 1 its notifier, 2 + 2i and 3 + 2i extra
17note channel i's ring and notifier, 8 a scratch row for the descriptor the C builds on its stack, 9 m16's
18note pushbuffer, 10 to 25 the caller's, 26 to 89 m16's legacy table. The live table lv has the C's NVRM_MAX_LIVE (4096) rows of 9:
19note handle, va, size, cpu, uvm_live, dma_live, rm_live, cpu_live, quarantined. A refusal at 4096 rows is the
20note C's defined answer ("live allocation table full"), reproduced here; the table is not grown silently.
21
22const CS_FAULT ulong 0
23const CS_ROOT ulong 1
24const CS_DEV ulong 2
25const CS_SUB ulong 3
26const CS_VMEM ulong 4
27const CS_UMODE ulong 5
28const CS_VAS ulong 6
29const CS_CHG ulong 7
30const CS_CTX ulong 8
31const CS_GPF ulong 9
32const CS_COMP ulong 10
33const CS_FDCTL ulong 11
34const CS_FDDEV ulong 12
35const CS_FDUVM ulong 13
36const CS_FDUVM2 ulong 14
37const CS_GREG ulong 15
38const CS_VREG ulong 16
39const CS_CREG ulong 17
40const CS_NEXTRA ulong 18
41const CS_LIVEN ulong 19
42const CS_PUT ulong 20
43const CS_RET ulong 21
44const CS_ENT ulong 22
45const CS_USERD ulong 23
46const CS_TOKEN ulong 24
47const CS_CHVA ulong 25
48const CS_CHVASZ ulong 26
49const CS_SLOT ulong 27
50const CS_VANEXT ulong 28
51const CS_TSM ulong 29
52const CS_DISC ulong 30
53const CS_CCLS ulong 31
54const CS_GCLS ulong 32
55const CS_UCLS ulong 33
56const CS_SMV ulong 34
57const CS_GPUID ulong 35
58const CS_MINOR ulong 36
59const CS_UCPU ulong 37
60const CS_LRS ulong 38
61const CS_RETRIES ulong 39
62const CS_RETMS ulong 40
63const CS_NEXTH ulong 41
64const CS_UUID0 ulong 42
65const CS_UUID1 ulong 43
66const CS_DOOR ulong 44
67const CN_MAPFAILED ulong 18446744073709551615
68const CN_U32 ulong 4294967295
69
70global cs ulong 64
71global cm ulong 360
72global lv ulong 36864
73global cx ulong 48
74
75fn cg i ulong to ulong
76ret get cs i
77end
78
79fn cp i ulong v ulong to void
80put cs i v
81end
82
83fn cn_fault to void
84put cs CS_FAULT 1
85end
86
87fn cn_faulted to bool
88ret ne get cs CS_FAULT 0
89end
90
91note memory descriptor rows
92fn mh s ulong to ulong
93ret get cm mul s 4
94end
95
96fn mva s ulong to ulong
97ret get cm add mul s 4 1
98end
99
100fn msz s ulong to ulong
101ret get cm add mul s 4 2
102end
103
104fn mcpu s ulong to ulong
105ret get cm add mul s 4 3
106end
107
108fn mset s ulong h ulong va ulong sz ulong cpu ulong to void
109put cm mul s 4 h
110put cm add mul s 4 1 va
111put cm add mul s 4 2 sz
112put cm add mul s 4 3 cpu
113end
114
115fn mzero s ulong to void
116mset s 0 0 0 0
117end
118
119note live rows
120fn lg r ulong f ulong to ulong
121ret get lv add mul r 9 f
122end
123
124fn lp r ulong f ulong v ulong to void
125put lv add mul r 9 f v
126end
127
128note extra channel i (0 to 2) fields: 0 gpfifo, 1 compute_obj, 2 ctxshare, 3 entries, 4 userd_off, 5 token,
129note 6 put, 7 retired, 8 channel_va, 9 channel_va_size, 10 registered
130fn xg i ulong f ulong to ulong
131ret get cx add mul i 16 f
132end
133
134fn xp i ulong f ulong v ulong to void
135put cx add mul i 16 f v
136end
137
138note fail(): message code (the oracle's table, seqoracle.c g_msgs), its arguments, -1
139fn cn_e0 code ulong to long
140dx_er code
141dx_ee
142ret -1
143end
144
145fn cn_e1 code ulong a ulong to long
146dx_er code
147dx_eu a
148dx_ee
149ret -1
150end
151
152fn cn_e2 code ulong a ulong b ulong to long
153dx_er code
154dx_eu a
155dx_eu b
156dx_ee
157ret -1
158end
159
160fn cn_ed code ulong d long to long
161dx_er code
162dx_ed d
163dx_ee
164ret -1
165end
166
167fn cn_bool b bool to ulong
168if b
169ret 1
170end
171ret 0
172end
173
174note the shell's va_release faults the client on every refused release
175fn cn_varel va ulong sz ulong to bool
176ok = dx_varel va sz
177if not ok
178cn_fault
179end
180ret ok
181end
182
183note client reset: the C's memset of the whole Nvrm (live table, extra channels, ring and notifier included)
184fn cn_zero to void
185for i ulong 64
186put cs i 0
187end
188for i ulong 36864
189put lv i 0
190end
191for i ulong 48
192put cx i 0
193end
194for s ulong 9
195mzero s
196end
197end
198
199note uuid byte i (0 to 15) of the GPU, from the two little-endian words
200fn cn_uuidb i ulong to ulong
201w = get cs add CS_UUID0 div i 8
202ret and shr w mul rem i 8 8 255
203end
204
205note RM_ALLOC under parent of class cls into client field i; false when refused (the field keeps its value)
206fn cn_obj i ulong parent ulong cls ulong to bool
207h = dx_alloc parent cls
208if eq h 0
209ret false
210end
211cp i h
212ret true
213end
214
215note --- nvrm_open: admission, VA slot, the control nodes, the RM client, UVM, the card table ---
216fn cn_open to long
217cn_zero
218if not dx_admit_open
219ret -1
220end
221cp CS_NEXTH 3472883713
222va = dx_slot
223cp CS_VANEXT va
224if eq va 0
225ret cn_e0 0
226end
227cp CS_SLOT va
228cp CS_FDCTL bitcast ulong dx_fopen 1
229cp CS_FDUVM bitcast ulong dx_fopen 2
230cp CS_FDUVM2 bitcast ulong dx_fopen 2
231if or or lt bitcast long cg CS_FDCTL 0 lt bitcast long cg CS_FDUVM 0 lt bitcast long cg CS_FDUVM2 0
232ret cn_ed 1 dx_errno
233end
234if not cn_obj CS_ROOT 0 65
235ret -1
236end
237dx_fk 18
238dx_fa 0
239if not dx_uvm bitcast long cg CS_FDUVM 805306369
240ret -1
241end
242dx_fk 19
243dx_fa and cg CS_FDUVM CN_U32
244dx_uvmraw bitcast long cg CS_FDUVM2 75
245dx_fk 16
246dx_fa 0
247if not dx_ioctl bitcast long cg CS_FDCTL 200
248ret cn_ed 2 dx_errno
249end
250found = dx_cardidx
251if lt found 0
252dx_er 3
253dx_es
254dx_ee
255ret -1
256end
257if ne cn_open_card ulong found 0
258ret -1
259end
260ret cn_open_vas
261end
262
263note nvrm_open on card index found: admission, the device node, device, subdevice, virtual memory, SM version,
264note class list and the class selection, which must select the SM the admission admitted
265fn cn_open_card found ulong to long
266asm = dx_admit_card found
267if lt asm 0
268ret -1
269end
270cp CS_GPUID and dx_cardget found 5 CN_U32
271cp CS_MINOR and dx_cardget found 6 CN_U32
272fd = dx_opendev cg CS_MINOR
273cp CS_FDDEV bitcast ulong fd
274if lt fd 0
275ret -1
276end
277dx_fk 1
278dx_fa cg CS_GPUID
279if not dx_ctrl cg CS_ROOT 517
280ret -1
281end
282dx_fk 2
283dx_fa dx_nsget 1 1
284dx_fa cg CS_ROOT
285dx_fa 0
286if not cn_obj CS_DEV cg CS_ROOT 128
287ret -1
288end
289dx_fk 3
290dx_fa 0
291if not cn_obj CS_SUB cg CS_DEV 8320
292ret -1
293end
294dx_fk 4
295dx_fa 0
296dx_fa 562949953421311
297if not cn_obj CS_VMEM cg CS_DEV 112
298ret -1
299end
300dx_fk 17
301dx_fa 12
302dx_fk 6
303dx_fa 1
304dx_fa 0
305if not dx_ctrl cg CS_SUB 545264129
306ret -1
307end
308cp CS_SMV and dx_nsget 17 1 CN_U32
309dx_fk 7
310dx_fa 0
311if not dx_ctrl cg CS_DEV 8389121
312ret -1
313end
314ncls = and dx_nsget 7 0 CN_U32
315if gt ncls 512
316ret cn_e0 4
317end
318dx_fk 7
319dx_fa ncls
320dx_fa 0
321if not dx_ctrl cg CS_DEV 8389121
322ret -1
323end
324ncls = and dx_nsget 7 0 CN_U32
325if gt ncls 512
326ret -1
327end
328if not dx_select ncls and dx_cardget found 7 65535 and dx_cardget found 8 65535
329ret -1
330end
331if ne cg CS_TSM ulong asm
332ret cn_e2 5 ulong asm cg CS_TSM
333end
334ret 0
335end
336
337note nvrm_open last: usermode doorbell page, VA space, the GPU uuid, UVM registration of the GPU and its VA space
338fn cn_open_vas to long
339if not cn_obj CS_UMODE cg CS_SUB cg CS_UCLS
340ret -1
341end
342mmio = dx_map cg CS_UMODE 65536 0 0 0
343if eq mmio CN_MAPFAILED
344ret -1
345end
346cp CS_UCPU mmio
347cp CS_DOOR add mmio 144
348dx_fk 8
349dx_fa 0
350dx_fa 72
351dx_fa 562949869535232
352dx_fa 4096
353if not cn_obj CS_VAS cg CS_DEV 37105
354ret -1
355end
356dx_fk 9
357dx_fa 0
358dx_fa 2
359dx_fa 16
360if not dx_ctrl cg CS_SUB 545259850
361ret -1
362end
363for i ulong 16
364w = add CS_UUID0 div i 8
365cp w or cg w shl and dx_nsget 9 add 3 i 255 mul rem i 8 8
366end
367dx_fk 21
368dx_fa cg CS_UUID0
369dx_fa cg CS_UUID1
370dx_fa CN_U32
371if not dx_uvm bitcast long cg CS_FDUVM 37
372ret -1
373end
374cp CS_GREG 1
375dx_fk 23
376dx_fa cg CS_UUID0
377dx_fa cg CS_UUID1
378dx_fa and cg CS_FDCTL CN_U32
379dx_fa cg CS_ROOT
380dx_fa cg CS_VAS
381if not dx_uvm bitcast long cg CS_FDUVM 25
382ret -1
383end
384cp CS_VREG 1
385ret 0
386end
387
388note --- quarantine_allocation: a live row that is kept, quarantined, and the client faulted ---
389fn cn_quar h ulong va ulong sz ulong cpu ulong uvml ulong dmal ulong to void
390r = cg CS_LIVEN
391cp CS_LIVEN add r 1
392lp r 0 h
393lp r 1 va
394lp r 2 sz
395lp r 3 cpu
396lp r 4 uvml
397lp r 5 dmal
398lp r 6 1
399lp r 7 1
400lp r 8 1
401dx_transfer h
402cn_fault
403end
404
405note --- alloc_with_cacheability (cach: NVOS32_ATTR2_GPU_CACHEABLE 1 yes, 2 no; local: framebuffer) into row s ---
406fn cn_alloc size ulong s ulong cach ulong local bool to long
407sz = drv_asize cn_faulted size
408if eq sz 0
409ret cn_e0 7
410end
411if drv_livefull cg CS_LIVEN 4096
412ret cn_ed 8 4096
413end
414va = dx_vatake sz 4096
415if eq va 0
416ret cn_e0 9
417end
418res = dx_reserve va sz
419if ne res va
420if ne res CN_MAPFAILED
421q = dx_drop res sz
422end
423cn_fault
424ret cn_e1 10 va
425end
426dx_fk 10
427dx_fa cg CS_ROOT
428dx_fa 0
429dx_fa 49409
430attr = ulong 436207616
431cls = ulong 62
432system = long 1
433if local
434attr = ulong 402653184
435cls = ulong 64
436system = long 0
437end
438dx_fa attr
439dx_fa or shl cach 2 1
440dx_fa 6
441dx_fa sz
442dx_fa 4096
443dx_fa sub sz 1
444h = dx_alloc cg CS_DEV cls
445if eq h 0
446if not dx_drop va sz
447cn_fault
448end
449if local
450if not cn_faulted
451if ne cg CS_LRS 0
452q = cn_varel va sz
453ret -1
454end
455end
456end
457cn_fault
458dx_ea sz
459ret -1
460end
461cpu = dx_map h sz va 0 system
462if ne cpu va
463cn_quar h va sz va 0 0
464if local
465mset 8 h va sz va
466if eq cn_free 8 0
467cp CS_FAULT 0
468end
469end
470dx_er 11
471dx_eu va
472dx_es
473dx_ee
474ret -1
475end
476um = dx_uvmmap va sz h
477if ne um 0
478cn_quar h va sz cpu cn_bool ge um 2 cn_bool ge um 3
479if local
480mset 8 h va sz cpu
481if eq cn_free 8 0
482cp CS_FAULT 0
483end
484end
485ret -1
486end
487mset s h va sz cpu
488dx_zero cpu sz
489r = cg CS_LIVEN
490lp r 0 h
491lp r 1 va
492lp r 2 sz
493lp r 3 cpu
494lp r 4 1
495lp r 5 1
496lp r 6 1
497lp r 7 1
498note FIX-1: the C leaves quarantined as the row's last tenant had it. After a local allocation whose CPU or UVM
499note mapping failed and was released at once (its row quarantined, then removed, the client unfaulted), the next
500note allocation took that row with quarantined 1 and copy_live refused it. A new row is not quarantined.
501lp r 8 0
502dx_transfer h
503cp CS_LIVEN add r 1
504ret 0
505end
506
507note nvrm_alloc (cacheable), nvrm_alloc_gpu_uncached and nvrm_alloc_vram
508fn cn_alloc_mode size ulong s ulong mode ulong to long
509if eq mode 0
510ret cn_alloc size s 1 false
511end
512if eq mode 1
513ret cn_alloc size s 2 false
514end
515if not drv_vramok ne cg CS_DISC 0 cg CS_TSM
516ret cn_e0 6
517end
518ret cn_alloc size s 2 true
519end
520
521note the live row of descriptor s (same va, handle and size): its index; 2^64 - 1 none; 2^64 - 2 two rows
522fn cn_find s ulong to ulong
523idx = ulong 18446744073709551615
524for i ulong cg CS_LIVEN bound 4096
525if and and eq lg i 1 mva s eq lg i 0 mh s eq lg i 2 msz s
526if ne idx 18446744073709551615
527ret 18446744073709551614
528end
529idx = i
530end
531end
532ret idx
533end
534
535note --- nvrm_free of descriptor row s ---
536fn cn_free s ulong to long
537if and and and eq mh s 0 eq mva s 0 eq msz s 0 eq mcpu s 0
538ret 0
539end
540idx = cn_find s
541if eq idx 18446744073709551614
542cn_fault
543ret cn_e0 12
544end
545if eq idx 18446744073709551615
546dx_er 13
547dx_eu mva s
548dx_eu mh s
549dx_eu msz s
550dx_ee
551ret -1
552end
553h = mh s
554va = mva s
555sz = msz s
556cpu = mcpu s
557bad = ne lg idx 3 cpu
558bad = or bad eq cpu 0
559bad = or bad eq sz 0
560bad = or bad ne and va 4095 0
561bad = or bad ne and sz 4095 0
562if bad
563cn_fault
564ret cn_e0 14
565end
566if ne lg idx 4 0
567if not dx_uvmfree va sz
568lp idx 8 1
569cn_fault
570ret -1
571end
572lp idx 4 0
573end
574if ne lg idx 5 0
575if not dx_dmaunmap h va sz
576lp idx 8 1
577cn_fault
578ret -1
579end
580lp idx 5 0
581end
582if ne lg idx 6 0
583if not dx_free cg CS_DEV h
584lp idx 8 1
585cn_fault
586ret -1
587end
588lp idx 6 0
589end
590if ne lg idx 7 0
591if not dx_drop cpu sz
592lp idx 8 1
593cn_fault
594dx_er 15
595dx_eu va
596dx_ed dx_errno
597dx_ee
598ret -1
599end
600lp idx 7 0
601end
602last = sub cg CS_LIVEN 1
603for f ulong 9
604lp idx f lg last f
605end
606cp CS_LIVEN last
607if not cn_varel va sz
608ret -1
609end
610mzero s
611ret 0
612end
613
614note --- chan_extra: extra channel i (n_extra already counts it) ---
615fn cn_chx i ulong to long
616fifo = add 2 mul i 2
617notif = add fifo 1
618dx_fk 13
619dx_fa cg CS_VAS
620dx_fa 1
621ctx = dx_alloc cg CS_CHG 36967
622if eq ctx 0
623ret -1
624end
625xp i 2 ctx
626xp i 3 1024
627if ne cn_alloc 65536 fifo 2 false 0
628ret -1
629end
630if ne cn_alloc 1048576 notif 1 false 0
631ret -1
632end
633xp i 4 and mul xg i 3 8 CN_U32
634dx_fk 11
635dx_fa mh notif
636dx_fa mh fifo
637dx_fa mva fifo
638dx_fa xg i 3
639dx_fa ctx
640dx_fa 0
641dx_fa mh fifo
642dx_fa xg i 4
643dx_fa 0
644g = dx_alloc cg CS_CHG cg CS_GCLS
645if eq g 0
646ret -1
647end
648xp i 0 g
649co = dx_alloc g cg CS_CCLS
650if eq co 0
651ret -1
652end
653xp i 1 co
654dx_fk 14
655dx_fa CN_U32
656if not dx_ctrl g 3278831880
657ret -1
658end
659xp i 5 and dx_nsget 14 0 CN_U32
660cva = dx_vatake 67108864 4096
661xp i 8 cva
662if eq cva 0
663cn_fault
664ret cn_e0 16
665end
666xp i 9 67108864
667dx_fk 24
668dx_fa cg CS_UUID0
669dx_fa cg CS_UUID1
670dx_fa and cg CS_FDCTL CN_U32
671dx_fa cg CS_ROOT
672dx_fa g
673dx_fa cva
674dx_fa 67108864
675if not dx_uvm bitcast long cg CS_FDUVM 27
676cn_fault
677ret -1
678end
679xp i 10 1
680xp i 6 0
681xp i 7 0
682ret 0
683end
684
685note --- chan_extra_destroy: extra channel i ---
686fn cn_chxd i ulong to long
687fifo = add 2 mul i 2
688notif = add fifo 1
689if ne xg i 10 0
690dx_fk 25
691dx_fa cg CS_UUID0
692dx_fa cg CS_UUID1
693dx_fa cg CS_ROOT
694dx_fa xg i 0
695if not dx_uvm bitcast long cg CS_FDUVM 28
696ret -1
697end
698xp i 10 0
699end
700if ne xg i 1 0
701if not dx_free xg i 0 xg i 1
702ret -1
703end
704end
705xp i 1 0
706if ne xg i 0 0
707if not dx_free cg CS_CHG xg i 0
708ret -1
709end
710end
711xp i 0 0
712if ne xg i 2 0
713if not dx_free cg CS_CHG xg i 2
714ret -1
715end
716end
717xp i 2 0
718if ne mh fifo 0
719if ne cn_free fifo 0
720ret -1
721end
722end
723if ne mh notif 0
724if ne cn_free notif 0
725ret -1
726end
727end
728if ne xg i 9 0
729if not cn_varel xg i 8 xg i 9
730ret -1
731end
732xp i 8 0
733xp i 9 0
734end
735xp i 6 0
736xp i 7 0
737ret 0
738end
739
740note --- nvrm_channel_n (nvrm_channel is extra 0) ---
741fn cn_channel extra ulong to long
742adm = nvrm_chadm cg CS_FAULT cg CS_CHG cg CS_CREG cg CS_NEXTRA extra 3
743if eq adm 1
744ret cn_e0 17
745end
746if eq adm 2
747dx_er 18
748dx_eu extra
749dx_ed 3
750dx_ee
751ret -1
752end
753dx_fk 12
754dx_fa 0
755dx_fa 1
756chg = dx_alloc cg CS_DEV 41068
757if eq chg 0
758ret -1
759end
760cp CS_CHG chg
761dx_fk 13
762dx_fa cg CS_VAS
763dx_fa 1
764ctx = dx_alloc chg 36967
765if eq ctx 0
766ret -1
767end
768cp CS_CTX ctx
769cp CS_ENT 1024
770if ne cn_alloc 65536 0 2 false 0
771ret -1
772end
773if ne cn_alloc 1048576 1 1 false 0
774ret -1
775end
776cp CS_USERD and mul cg CS_ENT 8 CN_U32
777dx_fk 11
778dx_fa mh 1
779dx_fa mh 0
780dx_fa mva 0
781dx_fa cg CS_ENT
782dx_fa cg CS_CTX
783dx_fa 0
784dx_fa mh 0
785dx_fa cg CS_USERD
786dx_fa 0
787g = dx_alloc cg CS_CHG cg CS_GCLS
788if eq g 0
789ret -1
790end
791cp CS_GPF g
792delay = cg CS_RETMS
793if eq delay 0
794delay = ulong 100
795end
796cp CS_RETRIES 0
797while true bound 1000
798co = dx_alloc g cg CS_CCLS
799arc = ulong 1
800if ne co 0
801cp CS_COMP co
802arc = ulong 0
803end
804rd = nvrm_retry arc cg CS_RETRIES 50 cn_bool eq cg CS_LRS 81
805if eq rd 0
806break
807end
808if eq rd 3
809ret -1
810end
811if eq rd 2
812dx_en and add cg CS_RETRIES 1 CN_U32 delay
813ret -1
814end
815cp CS_RETRIES and add cg CS_RETRIES 1 CN_U32
816dx_sleep and mul delay 1000 CN_U32
817end
818dx_fk 14
819dx_fa CN_U32
820if not dx_ctrl g 3278831880
821ret -1
822end
823cp CS_TOKEN and dx_nsget 14 0 CN_U32
824cva = dx_vatake 67108864 4096
825cp CS_CHVA cva
826if eq cva 0
827cn_fault
828ret cn_e0 19
829end
830cp CS_CHVASZ 67108864
831dx_fk 24
832dx_fa cg CS_UUID0
833dx_fa cg CS_UUID1
834dx_fa and cg CS_FDCTL CN_U32
835dx_fa cg CS_ROOT
836dx_fa g
837dx_fa cva
838dx_fa 67108864
839if not dx_uvm bitcast long cg CS_FDUVM 27
840cn_fault
841ret -1
842end
843cp CS_CREG 1
844for i ulong extra bound 3
845cp CS_NEXTRA add i 1
846if ne cn_chx i 0
847ret -1
848end
849end
850dx_fk 15
851dx_fa 1
852if not dx_ctrl cg CS_CHG 2691432705
853ret -1
854end
855cp CS_PUT 0
856cp CS_RET 0
857ret 0
858end
859
860note --- nvrm_channel_destroy: extras last first, then the main channel; any refusal faults the client ---
861fn cn_chdestroy to long
862while ne cg CS_NEXTRA 0 bound 3
863if ne cn_chxd sub cg CS_NEXTRA 1 0
864cn_fault
865ret -1
866end
867cp CS_NEXTRA sub cg CS_NEXTRA 1
868end
869if ne cg CS_CREG 0
870dx_fk 25
871dx_fa cg CS_UUID0
872dx_fa cg CS_UUID1
873dx_fa cg CS_ROOT
874dx_fa cg CS_GPF
875if not dx_uvm bitcast long cg CS_FDUVM 28
876cn_fault
877ret -1
878end
879cp CS_CREG 0
880end
881if ne cg CS_COMP 0
882if not dx_free cg CS_GPF cg CS_COMP
883cn_fault
884ret -1
885end
886end
887cp CS_COMP 0
888if ne cg CS_GPF 0
889if not dx_free cg CS_CHG cg CS_GPF
890cn_fault
891ret -1
892end
893end
894cp CS_GPF 0
895if ne cg CS_CTX 0
896if not dx_free cg CS_CHG cg CS_CTX
897cn_fault
898ret -1
899end
900end
901cp CS_CTX 0
902if ne cg CS_CHG 0
903if not dx_free cg CS_DEV cg CS_CHG
904cn_fault
905ret -1
906end
907end
908cp CS_CHG 0
909if ne mh 0 0
910if ne cn_free 0 0
911cn_fault
912ret -1
913end
914end
915if ne mh 1 0
916if ne cn_free 1 0
917cn_fault
918ret -1
919end
920end
921if ne cg CS_CHVASZ 0
922if not cn_varel cg CS_CHVA cg CS_CHVASZ
923cn_fault
924ret -1
925end
926cp CS_CHVA 0
927cp CS_CHVASZ 0
928end
929cp CS_PUT 0
930cp CS_RET 0
931ret 0
932end
933
934note --- nvrm_enqueue: pushbuffer row s, words [off, off + 4 nw) on the main channel ---
935fn cn_enqueue s ulong off ulong nw ulong to long
936if or or or ne cg CS_FAULT 0 eq cg CS_GPF 0 eq mcpu 0 0 lt cg CS_ENT 2
937ret cn_e0 20
938end
939sp = nvrm_pspan off nw msz s mva s
940if eq sp 1
941ret cn_e0 21
942end
943if ne cg CS_DISC 0
944if not dx_validate off nw
945ret -1
946end
947end
948if eq sp 2
949ret cn_e0 22
950end
951if ne nvrm_pfull cg CS_PUT cg CS_RET cg CS_ENT 0
952ret cn_e1 23 and sub cg CS_PUT cg CS_RET CN_U32
953end
954if not dx_publish mh 0 cg CS_ENT cg CS_PUT cg CS_USERD add mva s off nw
955ret cn_e0 24
956end
957cp CS_PUT and add cg CS_PUT 1 CN_U32
958ret 0
959end
960
961note --- nvrm_chan_submit: the same on extra channel ch (1 to n_extra), then its doorbell ---
962fn cn_submit ch ulong s ulong off ulong nw ulong to long
963if or or ne cg CS_FAULT 0 eq ch 0 gt ch cg CS_NEXTRA
964ret cn_e1 25 ch
965end
966i = sub ch 1
967if or or eq xg i 0 0 eq mcpu add 2 mul i 2 0 lt xg i 3 2
968ret cn_e1 25 ch
969end
970sp = nvrm_pspan off nw msz s mva s
971if eq sp 1
972ret cn_e0 21
973end
974if eq sp 2
975ret cn_e0 22
976end
977if ne cg CS_DISC 0
978if not dx_validate off nw
979ret -1
980end
981end
982if ne nvrm_pfull xg i 6 xg i 7 xg i 3 0
983ret cn_e2 26 ch and sub xg i 6 xg i 7 CN_U32
984end
985if not dx_publish mh add 2 mul i 2 xg i 3 xg i 6 xg i 4 add mva s off nw
986ret cn_e1 27 ch
987end
988xp i 6 and add xg i 6 1 CN_U32
989if not dx_doorbell xg i 5
990ret cn_e1 28 ch
991end
992ret 0
993end
994
995note close a descriptor that is above 0 (the C closes fd > 0 and then forgets it)
996fn cn_fdclose i ulong to void
997fd = bitcast long cg i
998if gt fd 0
999q = dx_fclose fd
1000end
1001cp i 0
1002end
1003
1004note --- nvrm_close ---
1005fn cn_close to long
1006if ne cg CS_ROOT 0
1007if ne cn_chdestroy 0
1008cn_fault
1009end
1010end
1011i = cg CS_LIVEN
1012while gt i 0 bound 1000000
1013i = sub i 1
1014eh = lg i 0
1015eva = lg i 1
1016esz = lg i 2
1017ecpu = lg i 3
1018ecl = lg i 7
1019mset 8 eh eva esz ecpu
1020rc = cn_free 8
1021if ne rc 0
1022cn_fault
1023if and ne ecl 0 ne ecpu 0
1024q = dx_drop ecpu esz
1025end
1026end
1027if and eq rc 0 lt i cg CS_LIVEN
1028i = add i 1
1029end
1030end
1031if ne cg CS_UCPU 0
1032if dx_drop cg CS_UCPU 65536
1033cp CS_UCPU 0
1034cp CS_DOOR 0
1035else
1036cn_fault
1037end
1038end
1039if ne cg CS_VREG 0
1040dx_fk 22
1041dx_fa cg CS_UUID0
1042dx_fa cg CS_UUID1
1043if not dx_uvm bitcast long cg CS_FDUVM 26
1044cn_fault
1045end
1046cp CS_VREG 0
1047end
1048if ne cg CS_GREG 0
1049dx_fk 22
1050dx_fa cg CS_UUID0
1051dx_fa cg CS_UUID1
1052if not dx_uvm bitcast long cg CS_FDUVM 38
1053cn_fault
1054end
1055cp CS_GREG 0
1056end
1057if ne cg CS_UMODE 0
1058if not dx_free cg CS_SUB cg CS_UMODE
1059cn_fault
1060end
1061end
1062cp CS_UMODE 0
1063if ne cg CS_VAS 0
1064if not dx_free cg CS_DEV cg CS_VAS
1065cn_fault
1066end
1067end
1068cp CS_VAS 0
1069if ne cg CS_VMEM 0
1070if not dx_free cg CS_DEV cg CS_VMEM
1071cn_fault
1072end
1073end
1074cp CS_VMEM 0
1075if ne cg CS_SUB 0
1076if not dx_free cg CS_DEV cg CS_SUB
1077cn_fault
1078end
1079end
1080cp CS_SUB 0
1081if ne cg CS_DEV 0
1082if not dx_free cg CS_ROOT cg CS_DEV
1083cn_fault
1084end
1085end
1086cp CS_DEV 0
1087if ne cg CS_ROOT 0
1088if not dx_free cg CS_ROOT cg CS_ROOT
1089cn_fault
1090end
1091end
1092cp CS_ROOT 0
1093cn_fdclose CS_FDDEV
1094cn_fdclose CS_FDUVM2
1095cn_fdclose CS_FDUVM
1096cn_fdclose CS_FDCTL
1097if not dx_finish
1098cn_fault
1099end
1100base = cg CS_SLOT
1101vn = cg CS_VANEXT
1102if and and and eq cg CS_FAULT 0 eq cg CS_LIVEN 0 eq cg CS_CREG 0 ne base 0
1103if ge vn base
1104if le sub vn base 4294967296
1105dx_recycle base
1106end
1107end
1108end
1109cp CS_SLOT 0
1110if ne cg CS_FAULT 0
1111ret -1
1112end
1113ret 0
1114end
1115
1116note --- m16_native_alloc_memory and m16_native_free_memory over the 64 legacy rows (26 to 89) ---
1117fn cn_m16alloc size ulong to long
1118for i ulong 64
1119s = add 26 i
1120if eq mh s 0
1121if ne cn_alloc size s 1 false 0
1122ret -1
1123end
1124ret 0
1125end
1126end
1127ret -1
1128end
1129
1130fn cn_m16free cpu ulong to long
1131if eq cpu 0
1132ret -1
1133end
1134for i ulong 64
1135s = add 26 i
1136if and eq mcpu s cpu ne mh s 0
1137ret cn_free s
1138end
1139end
1140ret -1
1141end
1142
1143note --- m16_native_open, m16_native_create_channel and m16_native_close: the m16 context is the client, its
1144note legacy rows (26 to 89) and its pushbuffer descriptor (row 9) ---
1145fn cn_m16open to long
1146for s ulong 81
1147mzero add 9 s
1148end
1149if eq cn_open 0
1150ret 0
1151end
1152r = cn_close
1153ret -1
1154end
1155
1156note nvrm_channel, then the pushbuffer the CPU rewrites between submissions, uncached on the GPU
1157fn cn_m16chan to long
1158if ne cn_channel 0 0
1159ret -1
1160end
1161if ne cn_alloc 4096 9 2 false 0
1162ret -1
1163end
1164ret 0
1165end
1166
1167fn cn_m16close to long
1168ret cn_close
1169end
1170
1171note --- nvrm_ring (also m16_native_ring_doorbell) and nvrm_submit: a refused doorbell faults the client ---
1172fn cn_ring to void
1173if not dx_doorbell cg CS_TOKEN
1174cp CS_FAULT 1
1175end
1176end
1177
1178fn cn_nsubmit s ulong off ulong nw ulong to long
1179if ne cn_enqueue s off nw 0
1180ret -1
1181end
1182cn_ring
1183ret 0
1184end