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