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