interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / native / share / share.om

338 lines · 7455 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
151if ne r 0
152m = r
153end
154end
155r = fclose int fd
156if ne r 0
157put nverr 0 r
158put nvfault 0 1
159if gt m 0
160r = mapdrop m
161if ne r 0
162put nvunpub 0 m
163end
164end
165ret neg long 1
166end
167if lt m 0
168put nverr 0 m
169ret neg long 1
170end
171ret m
172end
173
174note Import the buffer described by nvshrec. want is the byte size the caller expects.
175note Returns the CPU mapping handle; its address is also the GPU virtual address.
176note Clean refusals leave no slot, RM object or mapping and do not fault the context:
177note malformed record -22, size different from want or from the object -34, slots full -24,
178note RM refusal of the duplicate (wrong handle, not shared) the positive RM status.
179fn nvimport want ulong ro bool to long
180if or eq get nvreg 1 0 ne get nvfault 0 0
181put nverr 0 neg long 5
182ret neg long 1
183end
184if not nvshchk want
185ret neg long 1
186end
187slot = ulong 96
188for i ulong 96
189if and eq slot 96 eq get nvmflag i 0
190slot = i
191end
192end
193if eq slot 96
194put nverr 0 neg long 24
195ret neg long 1
196end
197if eq get nvseq 0 4294967295
198put nverr 0 neg long 75
199ret neg long 1
200end
201h = get nvseq 0
202put nvseq 0 add h 1
203put nvstage 0 52
204npzero
205npput 0 get nvobj 0
206npput 4 get nvobj 1
207npput 8 h
208npput 12 nvshget 8
209npput 16 nvshget 12
210npput 20 0
211r = devctl get nvfd 0 uint 3223078452 nvpar 0 28
212if not nvos r npget 24
213ret neg long 1
214end
215d = npget 8
216if eq d 0
217put nverr 0 neg long 22
218put nvfault 0 1
219ret neg long 1
220end
221put nvmobj slot d
222put nvmlen slot want
223put nvmflag slot 1
224put nvshobj slot d
225put nvstage 0 53
226m = nvshcpu d want ro
227if le m 0
228e = get nverr 0
229if nvmrel slot
230put nverr 0 e
231end
232ret neg long 1
233end
234put nvmmap slot m
235put nvmaddr slot mapaddr m
236put nvmflag slot 3
237if not nvshgpu slot ro
238ret neg long 1
239end
240put nvstage 0 57
241ret m
242end
243
244note Record checks before any RM call. Malformed -22; size other than want -34.
245fn nvshchk want ulong to bool
246if or ne nvshget 0 1129991503 ne nvshget 4 1
247put nverr 0 neg long 22
248ret false
249end
250if or eq nvshget 8 0 eq nvshget 12 0
251put nverr 0 neg long 22
252ret false
253end
254size = nvshg64 16
255if or eq size 0 gt size 68719476736
256put nverr 0 neg long 22
257ret false
258end
259if ne and size 4095 0
260put nverr 0 neg long 22
261ret false
262end
263if ne size want
264put nverr 0 neg long 34
265ret false
266end
267ret true
268end
269
270note GPU side of an import, after the CPU map, as in nvalloc: UVM external range at the CPU
271note address, DMA map at that fixed address, UVM external mapping. A failure here leaves the
272note slot quarantined and the context faulted; nvmclose or nvmforce releases it.
273fn nvshgpu slot ulong ro bool to bool
274va = get nvmaddr slot
275size = get nvmlen slot
276d = get nvmobj slot
277if or ne and va 4095 0 gt va sub 562949869535232 size
278put nverr 0 neg long 75
279put nvfault 0 1
280ret false
281end
282put nvstage 0 54
283npzero
284npp64 0 va
285npp64 8 size
286r = uvmctl get nvfd 2 uint 73 nvpar 0 24
287if not nvos r npget 16
288put nvfault 0 1
289ret false
290end
291put nvmflag slot 7
292put nvstage 0 55
293npzero
294npput 0 get nvobj 0
295npput 4 get nvobj 1
296npput 8 get nvobj 3
297npput 12 d
298npp64 24 size
299fl = uint 33040
300if ro
301fl = 33041
302end
303npput 32 fl
304npp64 48 va
305r = devctl get nvfd 0 uint 3225437783 nvpar 0 64
306if not nvos r npget 56
307put nvfault 0 1
308ret false
309end
310put nvmflag slot 15
311if ne npg64 48 va
312put nverr 0 neg long 22
313put nvfault 0 1
314ret false
315end
316put nvstage 0 56
317npzero
318npp64 0 va
319npp64 8 size
320for i ulong 16
321put nvpar add i 24 get nvuuid i
322end
323kind = uint 1
324if ro
325kind = 3
326end
327npput 40 kind
328npp64 9240 1
329npput 9248 uint get nvfd 0
330npput 9252 get nvobj 0
331npput 9256 d
332r = uvmctl get nvfd 2 uint 33 nvpar 0 9264
333if not nvos r npget 9260
334put nvfault 0 1
335ret false
336end
337ret true
338end