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