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_native.om

457 lines · 11745 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note Zero-copy buffer sharing over the NVIDIA RM client, ABI 580.173.02: the OM-2 function-mode unit
3note for the one hosted native library (omega-bridge ABI v2, tools/native --host). It has the same
4note export and import semantics as the program-mode native/share/share.om, which composes with
5note native/nvrm/context.om globals. Function mode has no globals or texts, so all state lives in
6note caller buffers: one zcshare block per shared buffer, the 24-byte zcrec share record, one
7note 9264-byte parameter scratch (the largest request, UVM_MAP_EXTERNAL_ALLOCATION), and the
8note control-device path for the fresh descriptor RM_MAP_MEMORY binds. The CPU mapping token lives
9note in whichever native context the caller passes (omega2_ctx_nvshare_*); import and release
10note must use the same context, and each live import holds one of its slots.
11note Export grants DUP_OBJECT by RM_SHARE with the OS security token policy (same user only).
12note Import duplicates the owner's memory with RM_DUP_OBJECT, proves its size, then maps it for
13note the CPU and the GPU at the caller's address. Read-only imports set every access flag read-only.
14note The duplicate holds its own reference: the pages outlive the owner's free or exit.
15note Exports return 0, or the error that zcshare.err also records: a negative errno, or the
16note positive RM status of a refused request. -5 faulted block, -16 block already holds an import,
17note -22 malformed record or block, -34 size other than wanted or than the object, -75 address,
18note -1 re-export of an import (EPERM); a slot shortage is maphold's -24.
19om2
20note Per-buffer block. Inputs: ctlfd, uvmfd, client, device, vaspace, uuid (from the NVRM client);
21note export: obj (the owned memory handle) and size; import: newh (the handle the duplicate gets),
22note mode (0 read/write, 1 read-only), size (the byte size wanted) and addr (CPU and GPU address).
23note Outputs: obj (the live duplicate), map (the CPU mapping token), live (1 RM duplicate, 2 CPU
24note mapping, 4 UVM range, 8 DMA mapping, 16 completed import), stage (51 export, 52 duplicate,
25note 53 size proof and CPU map, 54 UVM range, 55 DMA map, 56 UVM external mapping, 57 imported),
26note err and fault. fault is sticky: the RM view may differ from the block, as for a faulted
27note context.om; the block is released with nvshare_release and then discarded.
28layout zcshare 96
29field ctlfd int at 0 little
30field uvmfd int at 4 little
31field client uint at 8 little
32field device uint at 12 little
33field vaspace uint at 16 little
34field obj uint at 20 little
35field newh uint at 24 little
36field mode uint at 28 little
37field size ulong at 32 little
38field addr ulong at 40 little
39field map long at 48 little
40field live uint at 56 little
41field stage uint at 60 little
42field err long at 64 little
43field uuid uchar 16 at 72 little
44field fault uint at 88 little
45end
46note Share record, little endian: magic OMZC, version 1, owner client, owner memory handle,
47note byte size (whole 4 KiB pages). Byte-identical to share.om's nvshrec.
48layout zcrec 24
49field magic uint at 0 little
50field version uint at 4 little
51field client uint at 8 little
52field handle uint at 12 little
53field size ulong at 16 little
54end
55
56fn zcz p edit uchar 9264 n ulong to void
57for i ulong n
58put p i 0
59end
60end
61fn zcp32 p edit uchar 9264 off ulong v uint to void
62for i ulong 4
63put p add off i uchar and shr v mul i 8 255
64end
65end
66fn zcg32 p edit uchar 9264 off ulong to uint
67v = uint 0
68for i ulong 4
69v = or v shl uint get p add off i mul i 8
70end
71ret v
72end
73fn zcp64 p edit uchar 9264 off ulong v ulong to void
74for i ulong 8
75put p add off i uchar and shr v mul i 8 255
76end
77end
78fn zcg64 p edit uchar 9264 off ulong to ulong
79v = ulong 0
80for i ulong 8
81v = or v shl ulong get p add off i mul i 8
82end
83ret v
84end
85fn zcfail s edit zcshare e long to long
86put s err e
87ret e
88end
89note A Linux result r and an RM status st: both zero, or record the first failure.
90fn zcok s edit zcshare r long st uint to bool
91if ne r 0
92put s err r
93ret false
94end
95if ne st 0
96put s err long st
97ret false
98end
99ret true
100end
101note Shareable sizes: whole 4 KiB pages, at most 64 GiB.
102fn zcsize size ulong to bool
103if or eq size 0 gt size 68719476736
104ret false
105end
106ret eq and size 4095 0
107end
108
109note Publish one owned buffer: obj and size in s. Fills rec on success only.
110note A block that holds an import is refused (EPERM): only the owner exports.
111export fn nvshare_export s edit zcshare rec edit zcrec p edit uchar 9264 to long
112if ne get s fault 0
113ret zcfail s neg long 5
114end
115if ne and get s live 16 0
116ret zcfail s neg long 1
117end
118h = get s obj
119size = get s size
120if or eq h 0 eq get s client 0
121ret zcfail s neg long 22
122end
123if not zcsize size
124ret zcfail s neg long 22
125end
126put s stage 51
127zcz p 24
128zcp32 p 0 get s client
129zcp32 p 4 h
130note RS_SHARE_POLICY: target 0, access mask bit RS_ACCESS_DUP_OBJECT, type OS security token, action 0.
131zcp32 p 8 0
132zcp32 p 12 1
133zcp32 p 16 2
134r = devctl get s ctlfd uint 3222816309 p 0 24
135if not zcok s r zcg32 p 20
136ret get s err
137end
138put rec magic 1129991503
139put rec version 1
140put rec client get s client
141put rec handle h
142put rec size size
143put s err 0
144ret long 0
145end
146
147note RM_FREE of the duplicate under the device. Clears live bit 1 on success; faults otherwise.
148fn zcfree s edit zcshare p edit uchar 9264 to bool
149zcz p 16
150zcp32 p 0 get s client
151zcp32 p 4 get s device
152zcp32 p 8 get s obj
153r = devctl get s ctlfd uint 3222292009 p 0 16
154if not zcok s r zcg32 p 12
155put s fault 1
156ret false
157end
158put s live and get s live 4294967294
159put s obj 0
160ret true
161end
162
163note RM_MAP_MEMORY on the control device. Returns a negative errno or the RM status.
164fn zcmap s edit zcshare p edit uchar 9264 obj uint off ulong n ulong fd long to long
165zcz p 56
166zcp32 p 0 get s client
167zcp32 p 4 get s device
168zcp32 p 8 obj
169zcp64 p 16 off
170zcp64 p 24 n
171if eq get s mode 1
172zcp32 p 44 1
173end
174zcp32 p 48 uint fd
175r = devctl get s ctlfd uint 3224913486 p 0 56
176if lt r 0
177ret r
178end
179if gt r 0
180ret neg long 5
181end
182ret long zcg32 p 40
183end
184
185note Size proof and CPU map. RM refuses a map that starts at or past the object's end
186note (INVALID_BASE 0x20) and one that runs past it (INVALID_LIMIT 0x2e). A probe one page past
187note the claimed end must be refused as INVALID_BASE, and the full claimed span must map.
188note Either mismatch is -34 (ERANGE). Returns the mapping token, or -1 with err set. A
189note mapping that cannot be dropped after a failed close stays in map with live bit 2.
190fn zccpu s edit zcshare p edit uchar 9264 path view uchar * obj uint size ulong to long
191fd = fopen path 0 len path 524290 0
192if lt fd 0
193put s err fd
194ret neg long 1
195end
196st = zcmap s p obj size 4096 fd
197if eq st 32
198st = zcmap s p obj 0 size fd
199if eq st 46
200st = neg long 34
201end
202else
203if eq st 0
204st = neg long 34
205end
206end
207if ne st 0
208r = fclose int fd
209if ne r 0
210put s fault 1
211end
212put s err st
213ret neg long 1
214end
215m = maphold get s addr size
216if gt m 0
217r = neg long 1
218if eq get s mode 1
219r = mapbind m int fd 0 1
220else
221r = mapbind m int fd 0 3
222end
223if ne r 0
224m = r
225end
226end
227r = fclose int fd
228if ne r 0
229put s err r
230put s fault 1
231if gt m 0
232r = mapdrop m
233if ne r 0
234put s map m
235put s live or get s live 2
236end
237end
238ret neg long 1
239end
240if lt m 0
241put s err m
242ret neg long 1
243end
244ret m
245end
246
247note GPU side of an import, after the CPU map, as in nvalloc: UVM external range at the CPU
248note address, DMA map at that fixed address, UVM external mapping. A failure faults the block
249note and leaves its live bits for nvshare_release.
250fn zcgpu s edit zcshare p edit uchar 9264 d uint size ulong va ulong to bool
251put s stage 54
252zcz p 24
253zcp64 p 0 va
254zcp64 p 8 size
255r = uvmctl get s uvmfd uint 73 p 0 24
256if not zcok s r zcg32 p 16
257put s fault 1
258ret false
259end
260put s live or get s live 4
261put s stage 55
262zcz p 64
263zcp32 p 0 get s client
264zcp32 p 4 get s device
265zcp32 p 8 get s vaspace
266zcp32 p 12 d
267zcp64 p 24 size
268fl = uint 33040
269if eq get s mode 1
270fl = 33041
271end
272zcp32 p 32 fl
273zcp64 p 48 va
274r = devctl get s ctlfd uint 3225437783 p 0 64
275if not zcok s r zcg32 p 56
276put s fault 1
277ret false
278end
279put s live or get s live 8
280if ne zcg64 p 48 va
281put s err neg long 22
282put s fault 1
283ret false
284end
285put s stage 56
286zcz p 9264
287zcp64 p 0 va
288zcp64 p 8 size
289for i ulong 16
290put p add i 24 get s uuid i
291end
292kind = uint 1
293if eq get s mode 1
294kind = 3
295end
296zcp32 p 40 kind
297zcp64 p 9240 1
298zcp32 p 9248 uint get s ctlfd
299zcp32 p 9252 get s client
300zcp32 p 9256 d
301r = uvmctl get s uvmfd uint 33 p 0 9264
302if not zcok s r zcg32 p 9260
303put s fault 1
304ret false
305end
306ret true
307end
308
309note Record and block checks before any RM call: 0, or the clean refusal to return.
310fn zcchk s edit zcshare rec view zcrec to long
311if ne get s fault 0
312ret neg long 5
313end
314if ne get s live 0
315ret neg long 16
316end
317if or ne get rec magic 1129991503 ne get rec version 1
318ret neg long 22
319end
320if or eq get rec client 0 eq get rec handle 0
321ret neg long 22
322end
323size = get rec size
324if not zcsize size
325ret neg long 22
326end
327if ne size get s size
328ret neg long 34
329end
330if gt get s mode 1
331ret neg long 22
332end
333if or eq get s newh 0 eq get s client 0
334ret neg long 22
335end
336note maphold's bounds (64 KiB up to 1 TiB) also keep the GPU address below the VA space vaSize.
337va = get s addr
338if ne and va 4095 0
339ret neg long 75
340end
341if or lt va 65536 gt va sub 1099511627776 size
342ret neg long 75
343end
344ret long 0
345end
346
347note RM_DUP_OBJECT of the record's object under the device as newh. Returns the duplicate
348note handle, or 0 with err set (fault too when RM accepted but returned handle 0).
349fn zcdup s edit zcshare rec view zcrec p edit uchar 9264 to uint
350zcz p 28
351zcp32 p 0 get s client
352zcp32 p 4 get s device
353zcp32 p 8 get s newh
354zcp32 p 12 get rec client
355zcp32 p 16 get rec handle
356zcp32 p 20 0
357r = devctl get s ctlfd uint 3223078452 p 0 28
358if not zcok s r zcg32 p 24
359ret 0
360end
361d = zcg32 p 8
362if eq d 0
363put s err neg long 22
364put s fault 1
365end
366ret d
367end
368
369note Import the buffer rec describes into s. Clean refusals leave no RM object or mapping and
370note do not fault the block: malformed record or block -22, size other than wanted or than the
371note object -34, address -75, slots full -24, RM refusal of the duplicate (wrong handle, not
372note shared) the positive RM status. A returned duplicate handle 0 is -22 and faults.
373export fn nvshare_import s edit zcshare rec view zcrec p edit uchar 9264 path view uchar * to long
374e = zcchk s rec
375if ne e 0
376ret zcfail s e
377end
378size = get rec size
379va = get s addr
380put s stage 52
381d = zcdup s rec p
382if eq d 0
383ret get s err
384end
385put s obj d
386put s live 1
387put s stage 53
388m = zccpu s p path d size
389if le m 0
390e = get s err
391if zcfree s p
392put s err e
393end
394ret get s err
395end
396put s map m
397put s live 3
398if not zcgpu s p d size va
399ret get s err
400end
401put s live 31
402put s stage 57
403put s err 0
404ret long 0
405end
406
407note Undo an import in nvmrel's order: UVM free, DMA unmap, RM free, CPU unmap. Each live bit
408note clears only after its step is confirmed; a refused step faults the block and keeps the rest
409note live for a retry. With nothing live it returns 0. fault stays set.
410export fn nvshare_release s edit zcshare p edit uchar 9264 to long
411va = get s addr
412size = get s size
413if ne and get s live 4 0
414zcz p 24
415zcp64 p 0 va
416zcp64 p 8 size
417r = uvmctl get s uvmfd uint 34 p 0 24
418if not zcok s r zcg32 p 16
419put s fault 1
420ret get s err
421end
422put s live and get s live 4294967291
423end
424if ne and get s live 8 0
425zcz p 48
426zcp32 p 0 get s client
427zcp32 p 4 get s device
428zcp32 p 8 get s vaspace
429zcp32 p 12 get s obj
430zcp64 p 24 va
431zcp64 p 32 size
432r = devctl get s ctlfd uint 3224389208 p 0 48
433if not zcok s r zcg32 p 40
434put s fault 1
435ret get s err
436end
437put s live and get s live 4294967287
438end
439if ne and get s live 1 0
440if not zcfree s p
441ret get s err
442end
443end
444if ne and get s live 2 0
445r = mapdrop get s map
446if ne r 0
447put s fault 1
448ret zcfail s r
449end
450end
451put s live 0
452put s map 0
453put s obj 0
454put s stage 0
455put s err 0
456ret long 0
457end