interplane.Get Omega
Read in your language

omega-gpu-driver

aien-dev/omega-gpu-driver

Brought by drakestapleton

branch main Branches Tags History

omega-gpu-driver / tests / rms_client.c

114 lines · 5694 bytes Raw Blame History
1/* TEST ONLY fake CUSE kernel. The included CUDApp client is not patched.
2 * Test transport replaces OS operations only; checked Omega client policy stays enabled.
3 * Anonymous CPU backing belongs to this fake kernel, never to a GPU/device mapping. */
4#define _GNU_SOURCE
5#include <assert.h>
6#include <errno.h>
7#include <fcntl.h>
8#include <stdarg.h>
9#include <stdint.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <stdatomic.h>
14#include <sys/ioctl.h>
15#include <sys/mman.h>
16#include <sys/socket.h>
17#include <sys/wait.h>
18#include <time.h>
19#include <unistd.h>
20#include <linux/fuse.h>
21static int kernel_fd;
22static uint64_t unique, tokens[32];
23static unsigned char response[16384];
24static int frame(unsigned op,const void *body,size_t size) {
25 unsigned char wire[16384]={0};
26 struct fuse_in_header h={.len=40+size,.opcode=op,.unique=++unique};
27 assert(h.len<=sizeof wire);memcpy(wire,&h,sizeof h);memcpy(wire+40,body,size);
28 assert(send(kernel_fd,wire,h.len,MSG_NOSIGNAL)==h.len);
29 ssize_t n=recv(kernel_fd,response,sizeof response,0);
30 assert(n>=16);struct fuse_out_header out;memcpy(&out,response,16);
31 assert(out.len==(unsigned)n && out.unique==unique);
32 if(out.error){errno=-out.error;return -1;}
33 return (int)n;
34}
35static uint32_t token(int fd) {assert(fd>=100&&fd<132&&tokens[fd-100]);return tokens[fd-100];}
36static int host_open(const char *path,int flags,...) {
37 (void)flags;assert(!strncmp(path,"/dev/nvidia",11));
38 struct fuse_open_in in={0};assert(frame(FUSE_OPEN,&in,sizeof in)>0);
39 struct fuse_open_out out;memcpy(&out,response+16,sizeof out);
40 for(int i=0;i<32;i++)if(!tokens[i]){tokens[i]=out.fh;return 100+i;}
41 abort();
42}
43static int host_close(int fd) {
44 struct fuse_release_in in={.fh=token(fd)};int r=frame(FUSE_RELEASE,&in,sizeof in);
45 tokens[fd-100]=0;return r<0?-1:0;
46}
47/* Faithful unrestricted FUSE ioctl retries: each output iov is copied back to
48 * precisely the address supplied by the client. No endpoint pointer dereference. */
49static int kernel_ioctl(int fd,unsigned long cmd,void *arg) {
50 unsigned char wire[16384]={0};
51 struct fuse_ioctl_in in={.fh=token(fd),.flags=FUSE_IOCTL_UNRESTRICTED,.cmd=cmd,.arg=(uintptr_t)arg};
52 struct fuse_ioctl_iovec outvec[3];unsigned outcount=0;
53 for(unsigned step=0;step<5;step++) {
54 memcpy(wire,&in,sizeof in);
55 int n=frame(FUSE_IOCTL,wire,sizeof in+in.in_size);if(n<0)return -1;
56 struct fuse_ioctl_out out;memcpy(&out,response+16,sizeof out);
57 if(!(out.flags&FUSE_IOCTL_RETRY)){
58 assert(n==32+(int)in.out_size);size_t at=32;
59 for(unsigned i=0;i<outcount;i++){memcpy((void*)(uintptr_t)outvec[i].base,response+at,outvec[i].len);at+=outvec[i].len;}
60 return out.result;
61 }
62 assert(out.in_iovs<=3&&out.out_iovs<=3&&out.in_iovs==out.out_iovs);
63 struct fuse_ioctl_iovec input[3];memcpy(input,response+32,out.in_iovs*sizeof *input);
64 memcpy(outvec,response+32+out.in_iovs*sizeof *input,out.out_iovs*sizeof *outvec);outcount=out.out_iovs;
65 in.in_size=in.out_size=0;
66 for(unsigned i=0;i<out.in_iovs;i++){
67 assert(input[i].len<=sizeof wire-sizeof in-in.in_size);
68 memcpy(wire+sizeof in+in.in_size,(void*)(uintptr_t)input[i].base,input[i].len);
69 in.in_size+=input[i].len;in.out_size+=outvec[i].len;
70 }
71 }
72 abort();
73}
74static int host_ioctl(int fd,unsigned long cmd,...) {
75 va_list ap;va_start(ap,cmd);void *arg=va_arg(ap,void*);va_end(ap);
76 uint32_t *translated=NULL,saved=0;
77 if((cmd&0xffff)==0x46c9)translated=arg;
78 else if((cmd&0xffff)==0x464e)translated=(uint32_t*)((char*)arg+48);
79 else if(cmd==25)translated=(uint32_t*)((char*)arg+16);
80 else if(cmd==33)translated=(uint32_t*)((char*)arg+9248);
81 if(translated){saved=*translated;*translated=token((int)saved);}
82 int r=kernel_ioctl(fd,cmd,arg);
83 if(translated)*translated=saved;
84 return r;
85}
86static void *host_mmap(void *addr,size_t n,int prot,int flags,int fd,off_t off) {
87 if(fd>=100){(void)token(fd);assert(off==0);return mmap(addr,n,prot,(flags&~MAP_SHARED)|MAP_PRIVATE|MAP_ANONYMOUS,-1,0);}
88 return mmap(addr,n,prot,flags,fd,off);
89}
90#define CUDAPP_NVRM_TEST_TRANSPORT 1
91#define open host_open
92#define close host_close
93#define ioctl host_ioctl
94#define mmap host_mmap
95#include "nvrm.c"
96#undef open
97#undef close
98#undef ioctl
99#undef mmap
100int main(int argc,char **argv) {
101 assert(argc==3);int pair[2];assert(!socketpair(AF_UNIX,SOCK_SEQPACKET,0,pair));
102 pid_t child=fork();assert(child>=0);
103 if(!child){close(pair[0]);assert(dup2(pair[1],0)==0&&dup2(pair[1],1)==1);close(pair[1]);execl(argv[1],argv[1],"--fake","--host-model",argv[2],NULL);_exit(127);}
104 close(pair[1]);kernel_fd=pair[0];
105 struct cuse_init_in init={.major=7,.minor=31,.flags=CUSE_UNRESTRICTED_IOCTL};assert(frame(CUSE_INIT,&init,sizeof init)>0);
106 Nvrm rm;int rc=nvrm_open(&rm);if(rc){fprintf(stderr,"nvrm_open: %s\n",rm.err);return 2;}
107 assert(rm.target_sm==121);assert(!memcmp(rm.gpu_uuid,"HOST_MODEL_UUID1",16));
108 NvrmMem m;rc=nvrm_alloc(&rm,8192,&m);if(rc){fprintf(stderr,"nvrm_alloc: %s\n",rm.err);return 3;}
109 const char sample[]="HOST_MODEL CPU bytes only";char copy[sizeof sample];
110 assert(!nvrm_copy_to(&rm,&m,0,sample,sizeof sample));assert(!nvrm_copy_from(&rm,&m,0,copy,sizeof copy));assert(!memcmp(sample,copy,sizeof copy));
111 assert(!nvrm_free(&rm,&m));assert(!nvrm_close(&rm));close(kernel_fd);
112 int status;assert(waitpid(child,&status,0)==child&&WIFEXITED(status)&&WEXITSTATUS(status)==0);
113 puts("UNMODIFIED CUDApp nvrm open/alloc/control/map/free HOST_MODEL PASS; GPU_BACKING/DOORBELLS NOT_RUN");return 0;
114}