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 / native / share / share.om

340 lines · 7602 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note Zero-copy buffer sharing between processes over the NVIDIA RM client, ABI 580.173.02.
3note Compose after native/nvrm/context.om. Reuses its descriptors, handles and mapping slots.
4note Export grants DUP_OBJECT by RM_SHARE with the OS security token policy (same user only).
5note Import duplicates the owner's memory with RM_DUP_OBJECT, proves its size, then maps it
6note for the CPU and the GPU at one address. Read-only imports set every access flag read-only.
7note The duplicate holds its own reference: the pages outlive the owner's free or exit.
8note Share record, 24 bytes little endian: 0 magic OMZC, 4 version 1, 8 owner client,
9note 12 owner memory handle, 16 byte size (whole 4 KiB pages).
10note Stages: 51 export, 52 duplicate, 53 size proof and CPU map, 54 UVM range, 55 DMA map,
11note 56 UVM external mapping, 57 imported.
12global nvshrec uchar 24
13note Slot provenance: the duplicate handle an import put in a slot. Handles come from nvseq
14note and are never reused, so a stale entry cannot match a later allocation's handle.
15global nvshobj uint 96
16fn nvshput off ulong v uint to void
17for i ulong 4
18put nvshrec add off i uchar and shr v mul i 8 255
19end
20end
21fn nvshget off ulong to uint
22v = uint 0
23for i ulong 4
24v = or v shl uint get nvshrec add off i mul i 8
25end
26ret v
27end
28fn nvshp64 off ulong v ulong to void
29for i ulong 8
30put nvshrec add off i uchar and shr v mul i 8 255
31end
32end
33fn nvshg64 off ulong to ulong
34v = ulong 0
35for i ulong 8
36v = or v shl ulong get nvshrec add off i mul i 8
37end
38ret v
39end
40
41note Publish one owned, fully mapped buffer. Fills nvshrec on success.
42note A buffer imported from another process is refused (EPERM): only the owner exports.
43fn nvexport m long to bool
44if or ne get nvfault 0 0 ne get nvstate 0 1
45put nverr 0 neg long 5
46ret false
47end
48slot = nvfind m
49if eq slot 96
50put nverr 0 neg long 22
51ret false
52end
53h = get nvmobj slot
54if or ne get nvmflag slot 15 eq h 0
55put nverr 0 neg long 22
56ret false
57end
58if eq get nvshobj slot h
59put nverr 0 neg long 1
60ret false
61end
62put nvstage 0 51
63npzero
64npput 0 get nvobj 0
65npput 4 h
66note RS_SHARE_POLICY: target 0, access mask bit RS_ACCESS_DUP_OBJECT, type OS security token, action 0.
67npput 8 0
68npput 12 1
69npput 16 2
70r = devctl get nvfd 0 uint 3222816309 nvpar 0 24
71if not nvos r npget 20
72ret false
73end
74for i ulong len nvshrec
75put nvshrec i 0
76end
77nvshput 0 1129991503
78nvshput 4 1
79nvshput 8 get nvobj 0
80nvshput 12 h
81nvshp64 16 get nvmlen slot
82ret true
83end
84
85note RM_MAP_MEMORY on the control device. Returns a negative errno or the RM status.
86fn nvshmap obj uint off ulong n ulong ro bool fd long to long
87npzero
88npput 0 get nvobj 0
89npput 4 get nvobj 1
90npput 8 obj
91npp64 16 off
92npp64 24 n
93if ro
94npput 44 1
95end
96npput 48 uint fd
97r = devctl get nvfd 0 uint 3224913486 nvpar 0 56
98if lt r 0
99ret r
100end
101if gt r 0
102ret neg long 5
103end
104ret long npget 40
105end
106
107note Size proof and CPU map. RM refuses a map that starts at or past the object's end
108note (INVALID_BASE 0x20) and one that runs past it (INVALID_LIMIT 0x2e). A probe one page
109note past the claimed end must be refused as INVALID_BASE, and the full claimed span must map.
110note Either mismatch returns -1 with nverr -34 (ERANGE). The caller frees the duplicate,
111note which also retires any RM CPU mapping record the probe or map created.
112fn nvshcpu obj uint size ulong ro bool to long
113fd = fopen nvctl 0 len nvctl 524290 0
114if lt fd 0
115put nverr 0 fd
116ret neg long 1
117end
118st = nvshmap obj size 4096 ro fd
119if eq st 32
120st = nvshmap obj 0 size ro fd
121if eq st 46
122st = neg long 34
123end
124else
125if eq st 0
126st = neg long 34
127end
128end
129if ne st 0
130r = fclose int fd
131if ne r 0
132put nvfault 0 1
133end
134put nverr 0 st
135ret neg long 1
136end
137if gt get nvnext 0 sub 1099511627776 size
138r = fclose int fd
139put nverr 0 neg long 75
140ret neg long 1
141end
142m = maphold get nvnext 0 size
143if gt m 0
144put nvnext 0 add get nvnext 0 size
145r = neg long 1
146if ro
147r = mapbind m int fd 0 1
148else
149r = mapbind m int fd 0 3
150end
151note On a returned mapbind error the seed has retired the reservation.
152note Cleanup failure traps inside mapbind and cannot reach this assignment.
153if ne r 0
154m = r
155end
156end
157r = fclose int fd
158if ne r 0
159put nverr 0 r
160put nvfault 0 1
161if gt m 0
162r = mapdrop m
163if ne r 0
164put nvunpub 0 m
165end
166end
167ret neg long 1
168end
169if lt m 0
170put nverr 0 m
171ret neg long 1
172end
173ret m
174end
175
176note Import the buffer described by nvshrec. want is the byte size the caller expects.
177note Returns the CPU mapping handle; its address is also the GPU virtual address.
178note Clean refusals leave no slot, RM object or mapping and do not fault the context:
179note malformed record -22, size different from want or from the object -34, slots full -24,
180note RM refusal of the duplicate (wrong handle, not shared) the positive RM status.
181fn nvimport want ulong ro bool to long
182if or eq get nvreg 1 0 ne get nvfault 0 0
183put nverr 0 neg long 5
184ret neg long 1
185end
186if not nvshchk want
187ret neg long 1
188end
189slot = ulong 96
190for i ulong 96
191if and eq slot 96 eq get nvmflag i 0
192slot = i
193end
194end
195if eq slot 96
196put nverr 0 neg long 24
197ret neg long 1
198end
199if eq get nvseq 0 4294967295
200put nverr 0 neg long 75
201ret neg long 1
202end
203h = get nvseq 0
204put nvseq 0 add h 1
205put nvstage 0 52
206npzero
207npput 0 get nvobj 0
208npput 4 get nvobj 1
209npput 8 h
210npput 12 nvshget 8
211npput 16 nvshget 12
212npput 20 0
213r = devctl get nvfd 0 uint 3223078452 nvpar 0 28
214if not nvos r npget 24
215ret neg long 1
216end
217d = npget 8
218if eq d 0
219put nverr 0 neg long 22
220put nvfault 0 1
221ret neg long 1
222end
223put nvmobj slot d
224put nvmlen slot want
225put nvmflag slot 1
226put nvshobj slot d
227put nvstage 0 53
228m = nvshcpu d want ro
229if le m 0
230e = get nverr 0
231if nvmrel slot
232put nverr 0 e
233end
234ret neg long 1
235end
236put nvmmap slot m
237put nvmaddr slot mapaddr m
238put nvmflag slot 3
239if not nvshgpu slot ro
240ret neg long 1
241end
242put nvstage 0 57
243ret m
244end
245
246note Record checks before any RM call. Malformed -22; size other than want -34.
247fn nvshchk want ulong to bool
248if or ne nvshget 0 1129991503 ne nvshget 4 1
249put nverr 0 neg long 22
250ret false
251end
252if or eq nvshget 8 0 eq nvshget 12 0
253put nverr 0 neg long 22
254ret false
255end
256size = nvshg64 16
257if or eq size 0 gt size 68719476736
258put nverr 0 neg long 22
259ret false
260end
261if ne and size 4095 0
262put nverr 0 neg long 22
263ret false
264end
265if ne size want
266put nverr 0 neg long 34
267ret false
268end
269ret true
270end
271
272note GPU side of an import, after the CPU map, as in nvalloc: UVM external range at the CPU
273note address, DMA map at that fixed address, UVM external mapping. A failure here leaves the
274note slot quarantined and the context faulted; nvmclose or nvmforce releases it.
275fn nvshgpu slot ulong ro bool to bool
276va = get nvmaddr slot
277size = get nvmlen slot
278d = get nvmobj slot
279if or ne and va 4095 0 gt va sub 562949869535232 size
280put nverr 0 neg long 75
281put nvfault 0 1
282ret false
283end
284put nvstage 0 54
285npzero
286npp64 0 va
287npp64 8 size
288r = uvmctl get nvfd 2 uint 73 nvpar 0 24
289if not nvos r npget 16
290put nvfault 0 1
291ret false
292end
293put nvmflag slot 7
294put nvstage 0 55
295npzero
296npput 0 get nvobj 0
297npput 4 get nvobj 1
298npput 8 get nvobj 3
299npput 12 d
300npp64 24 size
301fl = uint 33040
302if ro
303fl = 33041
304end
305npput 32 fl
306npp64 48 va
307r = devctl get nvfd 0 uint 3225437783 nvpar 0 64
308if not nvos r npget 56
309put nvfault 0 1
310ret false
311end
312put nvmflag slot 15
313if ne npg64 48 va
314put nverr 0 neg long 22
315put nvfault 0 1
316ret false
317end
318put nvstage 0 56
319npzero
320npp64 0 va
321npp64 8 size
322for i ulong 16
323put nvpar add i 24 get nvuuid i
324end
325kind = uint 1
326if ro
327kind = 3
328end
329npput 40 kind
330npp64 9240 1
331npput 9248 uint get nvfd 0
332npput 9252 get nvobj 0
333npput 9256 d
334r = uvmctl get nvfd 2 uint 33 nvpar 0 9264
335if not nvos r npget 9260
336put nvfault 0 1
337ret false
338end
339ret true
340end