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/CUDApp/blob/main/nvrm/native_client.h # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / nvrm / native_client.h` 217 lines · 9489 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* C ABI glue only. File/ioctl/mapping operations are compiled Omega. */ 3 #ifndef CUDAPP_NVRM_NATIVE_CLIENT_H 4 #define CUDAPP_NVRM_NATIVE_CLIENT_H 5 /* Only the explicitly selected test boundary may use injected libc macros. 6 * Merely defining ioctl/mmap must never select the migration-pending C path. */ 7 #if CUDAPP_MIGRATION_PENDING_C_NVRM || (defined(CUDAPP_NVRM_TEST_TRANSPORT) && CUDAPP_NVRM_TEST_TRANSPORT) 8 #define NVRM_C_TRANSPORT 1 9 #else 10 #define NVRM_C_TRANSPORT 0 11 #include "cudapp_nvrm_native.h" 12 static int nvrm_native_ok(uint32_t status, uint64_t value) { 13 if (status) { errno = EPROTO; return -1; } 14 if ((int64_t)value < 0) { errno = (int)-(int64_t)value; return -1; } 15 return 0; 16 } 17 static int nvrm_mapping_context(Nvrm *rm) { 18 if (!rm->omega_context && omega_native_context_create(NVRM_MAX_LIVE + 2, &rm->omega_context)) { 19 errno = ENOMEM; return -1; 20 } 21 return 0; 22 } 23 /* Owned mapping handle and byte offset for [p, p+n) inside exactly one live, 24 * unquarantined CPU mapping of this client. 0 on one match; -1 with EINVAL 25 * (bad args/no context) or EFAULT (no or ambiguous owner). Shared by the 26 * submit stores and the platform marker poll; never a second owner. */ 27 static inline int nvrm_live_token(const Nvrm *rm, const void *p, size_t n, uint64_t *token, uint64_t *offset) { 28 if (!rm || !p || !n || !token || !offset || !rm->omega_context) { errno = EINVAL; return -1; } 29 uintptr_t a = (uintptr_t)p; 30 if (a > UINTPTR_MAX - n) { errno = EINVAL; return -1; } 31 uint32_t hits = 0; 32 for (uint32_t i = 0; i < rm->live_count; i++) { 33 const NvrmLiveAlloc *l = &rm->live[i]; 34 uintptr_t b = (uintptr_t)l->cpu; 35 if (!l->cpu_live || l->quarantined || !l->omega_mapping || !b || a < b || l->size < n || 36 a - b > l->size - n) continue; 37 *token = l->omega_mapping; *offset = a - b; hits++; 38 } 39 if (hits != 1) { errno = EFAULT; return -1; } 40 return 0; 41 } 42 #endif 43 44 static int nvrm_file_open(const char *path) { 45 #if NVRM_C_TRANSPORT 46 return open(path, O_RDWR | O_CLOEXEC); 47 #else 48 uint64_t result = 0; 49 uint32_t status = omega2_nvrm_open_file(&result, (const unsigned char *)path, strlen(path)); 50 return nvrm_native_ok(status, result) ? -1 : (int)result; 51 #endif 52 } 53 static int nvrm_file_close(int fd) { 54 #if NVRM_C_TRANSPORT 55 return close(fd); 56 #else 57 uint64_t result = 0; 58 uint32_t status = omega2_nvrm_close_file(&result, (uint64_t)(int64_t)fd); 59 return nvrm_native_ok(status, result); 60 #endif 61 } 62 static int nvrm_rm_ioctl(int fd, unsigned nr, void *arg, size_t size) { 63 #if NVRM_C_TRANSPORT 64 return ioctl(fd, NV_IOWR(nr, size), arg); 65 #else 66 void *params = NULL; size_t length = 0; 67 if (nr == NV_ESC_RM_ALLOC) { 68 NVOS21_PARAMETERS *h = arg; params = (void *)(uintptr_t)h->pAllocParms; length = h->paramsSize; 69 } else if (nr == NV_ESC_RM_CONTROL) { 70 NVOS54_PARAMETERS *h = arg; params = (void *)(uintptr_t)h->params; length = h->paramsSize; 71 } 72 uint64_t result = 0; 73 uint32_t status = omega2_nvrm_ioctl(&result, (uint64_t)(int64_t)fd, nr, 74 arg, size, params, length); 75 return nvrm_native_ok(status, result); 76 #endif 77 } 78 static int nvrm_uvm_ioctl(int fd, unsigned long cmd, void *arg) { 79 #if NVRM_C_TRANSPORT 80 return ioctl(fd, cmd, arg); 81 #else 82 size_t size; 83 switch (cmd) { 84 case UVM_INITIALIZE: size = sizeof(UVM_INITIALIZE_PARAMS); break; 85 case UVM_MM_INITIALIZE: size = sizeof(UVM_MM_INITIALIZE_PARAMS); break; 86 case UVM_REGISTER_GPU: size = sizeof(UVM_REGISTER_GPU_PARAMS); break; 87 case UVM_UNREGISTER_GPU: size = sizeof(UVM_UNREGISTER_GPU_PARAMS); break; 88 case UVM_REGISTER_GPU_VASPACE: size = sizeof(UVM_REGISTER_GPU_VASPACE_PARAMS); break; 89 case UVM_UNREGISTER_GPU_VASPACE: size = sizeof(UVM_UNREGISTER_GPU_VASPACE_PARAMS); break; 90 case UVM_REGISTER_CHANNEL: size = sizeof(UVM_REGISTER_CHANNEL_PARAMS); break; 91 case UVM_UNREGISTER_CHANNEL: size = sizeof(UVM_UNREGISTER_CHANNEL_PARAMS); break; 92 case UVM_MAP_EXTERNAL_ALLOCATION: size = sizeof(UVM_MAP_EXTERNAL_ALLOCATION_PARAMS); break; 93 case UVM_FREE: size = sizeof(UVM_FREE_PARAMS); break; 94 case UVM_CREATE_EXTERNAL_RANGE: size = sizeof(UVM_CREATE_EXTERNAL_RANGE_PARAMS); break; 95 default: errno = EINVAL; return -1; 96 } 97 uint64_t result = 0; 98 uint32_t status = omega2_nvrm_uvm(&result, (uint64_t)(int64_t)fd, cmd, arg, size); 99 return nvrm_native_ok(status, result); 100 #endif 101 } 102 static void *nvrm_cpu_reserve(Nvrm *rm, uint64_t address, uint64_t size) { 103 #if NVRM_C_TRANSPORT 104 (void)rm; 105 return mmap((void *)(uintptr_t)address, size, PROT_NONE, 106 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0); 107 #else 108 if (nvrm_mapping_context(rm)) return MAP_FAILED; 109 if (rm->omega_pending_mapping) { errno = EBUSY; return MAP_FAILED; } 110 uint64_t token = 0; 111 uint32_t status = omega2_ctx_nvrm_reserve(rm->omega_context, &token, address, size); 112 if (nvrm_native_ok(status, token)) return MAP_FAILED; 113 rm->omega_pending_mapping = token; 114 rm->omega_pending_address = address; rm->omega_pending_size = size; 115 return (void *)(uintptr_t)address; 116 #endif 117 } 118 static void *nvrm_cpu_map(Nvrm *rm, int fd, uint64_t size, void *target) { 119 #if NVRM_C_TRANSPORT 120 (void)rm; 121 return mmap(target, size, PROT_READ | PROT_WRITE, MAP_SHARED | (target ? MAP_FIXED : 0), fd, 0); 122 #else 123 if (nvrm_mapping_context(rm)) return MAP_FAILED; 124 uint64_t result = 0, token; 125 uint32_t status; 126 if (target) { 127 if (!rm->omega_pending_mapping || rm->omega_pending_address != (uintptr_t)target || 128 rm->omega_pending_size != size) { errno = EINVAL; return MAP_FAILED; } 129 status = omega2_ctx_nvrm_bind(rm->omega_context, &result, 130 rm->omega_pending_mapping, (uint64_t)(int64_t)fd); 131 if (nvrm_native_ok(status, result)) { 132 /* The checked mapbind contract unmaps and retires on a Linux 133 * failure. A trap does not establish that ownership was retired. */ 134 if (!status) { 135 rm->omega_pending_mapping = 0; 136 rm->omega_pending_address = rm->omega_pending_size = 0; 137 } 138 return MAP_FAILED; 139 } 140 return target; 141 } 142 if (rm->omega_usermode_mapping) { errno = EBUSY; return MAP_FAILED; } 143 status = omega2_ctx_nvrm_map_file(rm->omega_context, &result, (uint64_t)(int64_t)fd, size); 144 if (nvrm_native_ok(status, result)) return MAP_FAILED; 145 token = result; 146 rm->omega_usermode_mapping = token; 147 status = omega2_ctx_nvrm_map_address(rm->omega_context, &result, token); 148 if (nvrm_native_ok(status, result)) { 149 int saved_errno = errno; 150 uint64_t ignored = 0; 151 uint32_t cleanup = omega2_ctx_nvrm_drop(rm->omega_context, &ignored, token); 152 if (!cleanup && ignored == 0) rm->omega_usermode_mapping = 0; 153 else rm->faulted = 1; /* retain ownership for close's retry */ 154 errno = saved_errno; 155 return MAP_FAILED; 156 } 157 return (void *)(uintptr_t)result; 158 #endif 159 } 160 static int nvrm_cpu_drop(Nvrm *rm, void *address, size_t size) { 161 #if NVRM_C_TRANSPORT 162 (void)rm; 163 return munmap(address, size); 164 #else 165 uint64_t *owned = NULL; 166 NvrmLiveAlloc *live = NULL; 167 if (rm->omega_pending_mapping && rm->omega_pending_address == (uintptr_t)address && 168 rm->omega_pending_size == size) owned = &rm->omega_pending_mapping; 169 else if (rm->omega_usermode_mapping && rm->usermode_cpu == address && size == 0x10000) 170 owned = &rm->omega_usermode_mapping; 171 else for (uint32_t i = 0; i < rm->live_count; i++) { 172 if (rm->live[i].cpu == address && rm->live[i].size == size && rm->live[i].omega_mapping) { 173 live = &rm->live[i]; owned = &live->omega_mapping; break; 174 } 175 } 176 if (!owned || !rm->omega_context) { errno = EINVAL; return -1; } 177 uint64_t result = 0; 178 uint32_t status = omega2_ctx_nvrm_drop(rm->omega_context, &result, *owned); 179 if (nvrm_native_ok(status, result)) return -1; 180 *owned = 0; 181 if (live) live->cpu_live = 0; 182 if (owned == &rm->omega_pending_mapping) 183 rm->omega_pending_address = rm->omega_pending_size = 0; 184 return 0; 185 #endif 186 } 187 static void nvrm_mapping_transfer(Nvrm *rm, NvrmLiveAlloc *live) { 188 #if NVRM_C_TRANSPORT 189 (void)rm; (void)live; 190 #else 191 live->omega_mapping = rm->omega_pending_mapping; 192 live->cpu_live = live->omega_mapping != 0; 193 if (live->omega_mapping && (rm->omega_pending_address != live->va || rm->omega_pending_size != live->size)) 194 rm->faulted = 1; 195 rm->omega_pending_mapping = rm->omega_pending_address = rm->omega_pending_size = 0; 196 #endif 197 } 198 static int nvrm_mapping_finish(Nvrm *rm) { 199 #if NVRM_C_TRANSPORT 200 (void)rm; 201 #else 202 if (rm->omega_pending_mapping && nvrm_cpu_drop(rm, (void *)(uintptr_t)rm->omega_pending_address, 203 rm->omega_pending_size)) return -1; 204 if (rm->omega_usermode_mapping && !rm->usermode_cpu) { 205 uint64_t result = 0; 206 uint32_t status = omega2_ctx_nvrm_drop(rm->omega_context, &result, rm->omega_usermode_mapping); 207 if (nvrm_native_ok(status, result)) return -1; 208 rm->omega_usermode_mapping = 0; 209 } 210 if (rm->omega_context) { 211 if (omega_native_context_destroy(rm->omega_context)) { errno = EBUSY; return -1; } 212 rm->omega_context = NULL; 213 } 214 #endif 215 return 0; 216 } 217 #endif