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
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* HOST_ONLY. Ordinary files/anonymous reservations only; never nvrm_open(). */ |
| 3 | #include "nvrm.c" |
| 4 | #include <assert.h> |
| 5 | #include "native_ioctl_fixture.h" |
| 6 | |
| 7 | static int64_t signed_result(uint64_t n) { return (int64_t)n; } |
| 8 | int main(void) { |
| 9 | fixture_run(); |
| 10 | fixture_cleanup_run(); |
| 11 | int fd = nvrm_file_open("/dev/null"); assert(fd >= 0); |
| 12 | assert(nvrm_file_close(fd) == 0); |
| 13 | errno = 0; assert(nvrm_file_close(-1) == -1 && errno == EBADF); |
| 14 | assert(nvrm_file_open("/nonexistent-cudapp-nvrm-native-fixture") == -1 && errno == ENOENT); |
| 15 | NVOS00_PARAMETERS release = {.hRoot=1,.hObjectParent=2,.hObjectOld=3,.status=0x12345678}; |
| 16 | assert(nvrm_rm_ioctl(-1,NV_ESC_RM_FREE,&release,sizeof release) == -1 && errno == EBADF); |
| 17 | assert(release.status == 0x12345678); |
| 18 | NVOS21_PARAMETERS alloc = {.hRoot=1,.hObjectParent=2,.hObjectNew=3,.hClass=4,.status=0x87654321}; |
| 19 | assert(nvrm_rm_ioctl(-1,NV_ESC_RM_ALLOC,&alloc,sizeof alloc) == -1 && errno == EBADF); |
| 20 | assert(alloc.status == 0x87654321); |
| 21 | UVM_FREE_PARAMS uf = {.base=0x7000000000,.length=4096,.rmStatus=0x12345678}; |
| 22 | assert(nvrm_uvm_ioctl(-1,UVM_FREE,&uf) == -1 && errno == EBADF); |
| 23 | assert(uf.rmStatus == 0x12345678); |
| 24 | uint64_t value=0; |
| 25 | assert(omega2_nvrm_ioctl(&value,UINT64_MAX,0,(unsigned char *)&release,sizeof release,NULL,0)==0); |
| 26 | assert(signed_result(value)==-EINVAL); |
| 27 | assert(omega2_nvrm_uvm(&value,UINT64_MAX,UVM_FREE,(unsigned char *)&uf,sizeof uf-1)==0); |
| 28 | assert(signed_result(value)==-EINVAL); |
| 29 | |
| 30 | fd=memfd_create("cudapp-nvrm-host-fixture",MFD_CLOEXEC);assert(fd>=0); |
| 31 | assert(ftruncate(fd,0x10000)==0); |
| 32 | Nvrm rm={0}; |
| 33 | void *address=(void *)(uintptr_t)UINT64_C(0x7000000000); |
| 34 | assert(nvrm_cpu_reserve(&rm,(uintptr_t)address,4096)==address); |
| 35 | uint64_t retired=rm.omega_pending_mapping; |
| 36 | assert(nvrm_cpu_map(&rm,-1,4096,address)==MAP_FAILED && errno==EBADF); |
| 37 | assert(rm.omega_pending_mapping==0); |
| 38 | assert(omega2_ctx_nvrm_drop(rm.omega_context,&value,retired)!=0); |
| 39 | assert(nvrm_cpu_reserve(&rm,UINT64_C(0x10000000000),4096)==MAP_FAILED && errno==EFBIG); |
| 40 | assert(nvrm_cpu_reserve(&rm,UINT64_C(0x7000000000),UINT64_C(0x1000000001))==MAP_FAILED && errno==EFBIG); |
| 41 | assert(nvrm_cpu_reserve(&rm,UINT64_C(0xfffffff000),8192)==MAP_FAILED && errno==EFBIG); |
| 42 | assert(nvrm_cpu_map(&rm,fd,UINT64_C(0x1000000001),NULL)==MAP_FAILED && errno==EFBIG); |
| 43 | assert(!rm.omega_pending_mapping && !rm.omega_usermode_mapping); |
| 44 | /* WolframLanguageEvaluator: {2^36,2^40-4096,2^36+2^36<2^40} |
| 45 | -> {68719476736,1099511623680,True}. PROT_NONE only, never backed memory. */ |
| 46 | void *extent_start=(void *)(uintptr_t)UINT64_C(68719476736); |
| 47 | assert(nvrm_cpu_reserve(&rm,(uintptr_t)extent_start,UINT64_C(68719476736))==extent_start); |
| 48 | assert(nvrm_cpu_drop(&rm,extent_start,UINT64_C(68719476736))==0); |
| 49 | void *span_end=(void *)(uintptr_t)UINT64_C(1099511623680); |
| 50 | assert(nvrm_cpu_reserve(&rm,(uintptr_t)span_end,4096)==span_end); |
| 51 | assert(nvrm_cpu_drop(&rm,span_end,4096)==0); |
| 52 | assert(nvrm_cpu_reserve(&rm,(uintptr_t)address,4096)==address); |
| 53 | assert(nvrm_cpu_map(&rm,fd,4096,address)==address); |
| 54 | *(volatile unsigned char *)address=0x5a; |
| 55 | unsigned char byte=0;assert(pread(fd,&byte,1,0)==1 && byte==0x5a); |
| 56 | rm.live[0]=(NvrmLiveAlloc){.va=(uintptr_t)address,.size=4096,.cpu=address,.cpu_live=1}; |
| 57 | nvrm_mapping_transfer(&rm,&rm.live[0]);rm.live_count=1; |
| 58 | assert(rm.live[0].omega_mapping && !rm.omega_pending_mapping); |
| 59 | assert(omega_native_context_destroy(rm.omega_context)==36); |
| 60 | assert(nvrm_cpu_drop(&rm,(unsigned char *)address+4096,4096)==-1 && errno==EINVAL); |
| 61 | assert(*(volatile unsigned char *)address==0x5a); |
| 62 | assert(nvrm_cpu_drop(&rm,address,4096)==0 && !rm.live[0].cpu_live && !rm.live[0].omega_mapping); |
| 63 | rm.live_count=0; |
| 64 | rm.usermode_cpu=nvrm_cpu_map(&rm,fd,0x10000,NULL);assert(rm.usermode_cpu!=MAP_FAILED); |
| 65 | assert(nvrm_cpu_drop(&rm,rm.usermode_cpu,0x10000)==0);rm.usermode_cpu=NULL; |
| 66 | assert(nvrm_mapping_finish(&rm)==0 && !rm.omega_context); |
| 67 | |
| 68 | omega_native_context *a=NULL,*b=NULL; |
| 69 | assert(omega_native_context_create(4098,&a)==0); |
| 70 | assert(omega_native_context_create(4098,&b)==0); |
| 71 | uint64_t handles[4098],foreign=0; |
| 72 | for(unsigned i=0;i<4098;i++) { |
| 73 | assert(omega2_ctx_nvrm_map_file(a,&handles[i],fd,4096)==0 && signed_result(handles[i])>0); |
| 74 | } |
| 75 | assert(omega2_ctx_nvrm_map_file(a,&value,fd,4096)==0 && signed_result(value)==-EMFILE); |
| 76 | assert(omega2_ctx_nvrm_map_file(b,&foreign,fd,4096)==0 && signed_result(foreign)>0); |
| 77 | assert(foreign!=handles[0]); |
| 78 | assert(omega2_ctx_nvrm_drop(b,&value,handles[0])!=0); |
| 79 | assert(omega2_ctx_nvrm_drop(a,&value,foreign)!=0); |
| 80 | assert(omega2_ctx_nvrm_bind(a,&value,handles[0],fd)!=0); /* data mapping is not a reservation */ |
| 81 | for(unsigned i=0;i<4098;i++) assert(omega2_ctx_nvrm_drop(a,&value,handles[i])==0 && value==0); |
| 82 | assert(omega2_ctx_nvrm_drop(b,&value,foreign)==0 && value==0); |
| 83 | assert(omega_native_context_destroy(a)==0 && omega_native_context_destroy(b)==0); |
| 84 | assert(close(fd)==0); |
| 85 | puts("NVRM NATIVE TRANSPORT PASS (file/error/fake reply, reserve/bind/free, 4098 slots, independent contexts, stale/foreign/invalid controls; HOST_ONLY)"); |
| 86 | return 0; |
| 87 | } |