interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / tests / amd-kfd-launch / trace.c

330 lines · 17918 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 * Native AArch64 TEST instrument. No GPU pathname is ever opened: device opens
3 * become memfd_create before entry, ioctls become getpid, and only this oracle
4 * supplies KFD replies. Production contains none of this simulation. */
5#define _GNU_SOURCE
6#include <asm/ptrace.h>
7#include <elf.h>
8#include <errno.h>
9#include <fcntl.h>
10#include <linux/kfd_ioctl.h>
11#include <signal.h>
12#include <stdint.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <sys/mman.h>
17#include <sys/ptrace.h>
18#include <sys/stat.h>
19#include <sys/syscall.h>
20#include <sys/sysmacros.h>
21#include <sys/uio.h>
22#include <sys/wait.h>
23#include <unistd.h>
24#ifndef NT_ARM_SYSTEM_CALL
25#define NT_ARM_SYSTEM_CALL 0x404
26#endif
27static pid_t child;
28static void fail(const char *s) { fprintf(stderr,"TEST_SIM FAIL %s errno=%d\n",s,errno); exit(1); }
29#define CHECK(x) do { if (!(x)) fail(#x); } while(0)
30static void rd(uint64_t a, void *dst, size_t n) {
31 unsigned char *d=dst;
32 for (size_t i=0;i<n;i+=sizeof(long)) {
33 errno=0; long v=ptrace(PTRACE_PEEKDATA,child,(void *)(uintptr_t)(a+i),0);
34 if (errno) fail("peek");
35 memcpy(d+i,&v,n-i<sizeof v?n-i:sizeof v);
36 }
37}
38static void wr(uint64_t a, const void *src, size_t n) {
39 const unsigned char *s=src;
40 for (size_t i=0;i<n;i+=sizeof(long)) {
41 long v;
42 rd(a+i,&v,sizeof v);
43 memcpy(&v,s+i,n-i<sizeof v?n-i:sizeof v);
44 CHECK(!ptrace(PTRACE_POKEDATA,child,(void *)(uintptr_t)(a+i),(void *)v));
45 }
46}
47static uint64_t word(uint64_t a) { uint64_t v; rd(a,&v,8); return v; }
48static void setword(uint64_t a,uint64_t v) { wr(a,&v,8); }
49static void string(uint64_t a,char *s,size_t n) {
50 for(size_t i=0;i<n;i++) { rd(a+i,s+i,1); if(!s[i]) return; }
51 fail("unterminated pathname");
52}
53static void setregs(struct user_pt_regs *r) {
54 struct iovec v={r,sizeof *r};
55 CHECK(!ptrace(PTRACE_SETREGSET,child,(void *)NT_PRSTATUS,&v));
56}
57static void replace_call(struct user_pt_regs *r,int nr) {
58 r->regs[8]=(uint64_t)nr; setregs(r);
59 struct iovec v={&nr,sizeof nr};
60 CHECK(!ptrace(PTRACE_SETREGSET,child,(void *)NT_ARM_SYSTEM_CALL,&v));
61}
62struct alloc { uint64_t va,size,off,handle; uint32_t flags; int mapped,freed; };
63static struct alloc *allocs;
64static size_t count;
65static uint64_t endoff;
66static int kfd=-1,drm=-1,kback=-1,dback=-1;
67static struct kfd_ioctl_create_queue_args queue;
68static int queued,packets;
69static int planmode;
70static size_t ring_index=5,allocation_count=10;
71static unsigned clocks;
72static time_t elapsed_seconds;
73static unsigned expected_gpu=1,expected_minor=128;
74static int denied;
75static int topology_fd=-1;
76static int entry_order(const void *a,const void *b) {
77 return strcmp(*(const char *const *)a+19,*(const char *const *)b+19);
78}
79static void ordered_entries(uint64_t address,size_t bytes) {
80 unsigned char *input=malloc(bytes),*output=malloc(bytes);
81 char **entries=malloc((bytes/20+1)*sizeof *entries);
82 CHECK(input && output && entries); rd(address,input,bytes);
83 size_t count_entries=0;
84 for(size_t p=0;p<bytes;) {
85 uint16_t size; CHECK(bytes-p>=20); memcpy(&size,input+p+16,2);
86 CHECK(size>=20 && size<=bytes-p && memchr(input+p+19,0,size-19));
87 entries[count_entries++]=(char *)input+p; p+=size;
88 }
89 qsort(entries,count_entries,sizeof *entries,entry_order);
90 size_t offset=0;
91 for(size_t i=0;i<count_entries;i++) {
92 uint16_t size; memcpy(&size,entries[i]+16,2);
93 memcpy(output+offset,entries[i],size); offset+=size;
94 }
95 wr(address,output,bytes); free(entries); free(output); free(input);
96}
97static int backing(int fd) {
98 char p[128]; snprintf(p,sizeof p,"/proc/%d/fd/%d",child,fd);
99 int x=open(p,O_RDWR); CHECK(x>=0); return x;
100}
101static struct alloc *findalloc(uint64_t h) {
102 for(size_t i=0;i<count;i++) if(allocs[i].handle==h && !allocs[i].freed) return allocs+i;
103 fail("unknown allocation handle"); return 0;
104}
105static void ioctl_reply(uint64_t request,uint64_t p) {
106 fprintf(stderr,"TEST_SIM ioctl request=0x%llx bytes=%lu\n",(unsigned long long)request,(unsigned long)_IOC_SIZE(request));
107 switch(request) {
108 case AMDKFD_IOC_GET_VERSION: {
109 struct kfd_ioctl_get_version_args a={1,14}; wr(p,&a,sizeof a); break;
110 }
111 case AMDKFD_IOC_ACQUIRE_VM: {
112 struct kfd_ioctl_acquire_vm_args a; rd(p,&a,sizeof a);
113 CHECK(a.drm_fd==(unsigned)drm && a.gpu_id==expected_gpu); break;
114 }
115 case AMDKFD_IOC_GET_PROCESS_APERTURES_NEW: {
116 struct kfd_ioctl_get_process_apertures_new_args a; rd(p,&a,sizeof a);
117 CHECK(a.kfd_process_device_apertures_ptr && !a.pad);
118 const char *mode=getenv("KFD_TEST_APERTURE");
119 int large=mode && (!strcmp(mode,"large") || !strcmp(mode,"large-duplicate") || !strcmp(mode,"grow"));
120 uint32_t cap=a.num_of_nodes,total=large?80:2,written=cap<total?cap:total;
121 if(!cap) {
122 a.num_of_nodes=(mode && !strcmp(mode,"grow"))?1:total;
123 if(mode && !strcmp(mode,"empty"))a.num_of_nodes=0;
124 wr(p,&a,sizeof a);break;
125 }
126 struct kfd_process_device_apertures other={
127 UINT64_C(0x3000000000000),UINT64_C(0x30000ffffffff),UINT64_C(0x4000000000000),UINT64_C(0x40000ffffffff),0x10000,UINT64_C(0x7fffffffffff),0,0};
128 struct kfd_process_device_apertures selected={
129 UINT64_C(0x1000000000000),UINT64_C(0x10000ffffffff),UINT64_C(0x2000000000000),UINT64_C(0x20000ffffffff),0x10000,UINT64_C(0x7fffffffffff),expected_gpu,0};
130 struct kfd_process_device_apertures *ap=calloc(written,sizeof *ap);CHECK(ap);
131 for(uint32_t i=0;i<written;i++) {
132 ap[i]=other;ap[i].gpu_id=expected_gpu+100+i;
133 if(i==total-1)ap[i]=selected;
134 if(mode && !strcmp(mode,"missing") && i==total-1)ap[i].gpu_id=expected_gpu+2;
135 if(mode && !strcmp(mode,"duplicate") && i==0)ap[i]=selected;
136 if(mode && !strcmp(mode,"large-duplicate") && i==1)ap[i]=selected;
137 if(mode && !strcmp(mode,"invalid") && i==total-1)ap[i].lds_limit=0;
138 }
139 a.num_of_nodes=written;
140 if(mode && !strcmp(mode,"oversized"))a.num_of_nodes=cap+1;
141 if(mode && !strcmp(mode,"empty"))a.num_of_nodes=0;
142 wr(a.kfd_process_device_apertures_ptr,ap,written*sizeof *ap);free(ap);wr(p,&a,sizeof a);break;
143 }
144 case AMDKFD_IOC_ALLOC_MEMORY_OF_GPU: {
145 struct kfd_ioctl_alloc_memory_of_gpu_args a; rd(p,&a,sizeof a);
146 CHECK(a.gpu_id==expected_gpu && a.va_addr && !(a.va_addr%2097152) && a.size && !(a.size%4096));
147 CHECK(a.flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE);
148 CHECK(a.flags & KFD_IOC_ALLOC_MEM_FLAGS_PUBLIC);
149 allocs=realloc(allocs,(count+1)*sizeof *allocs); CHECK(allocs);
150 a.handle=count+1; a.mmap_offset=endoff;
151 allocs[count++]=(struct alloc){a.va_addr,a.size,endoff,a.handle,a.flags,0,0};
152 CHECK(a.size<=INT64_MAX-endoff); endoff+=a.size;
153 CHECK(!ftruncate(dback,(off_t)endoff)); wr(p,&a,sizeof a); break;
154 }
155 case AMDKFD_IOC_MAP_MEMORY_TO_GPU:
156 case AMDKFD_IOC_UNMAP_MEMORY_FROM_GPU: {
157 struct kfd_ioctl_map_memory_to_gpu_args a; rd(p,&a,sizeof a);
158 uint32_t gpu; rd(a.device_ids_array_ptr,&gpu,sizeof gpu);
159 CHECK(a.n_devices==1 && gpu==expected_gpu);
160 struct alloc *b=findalloc(a.handle);
161 CHECK(b->mapped==(request==AMDKFD_IOC_UNMAP_MEMORY_FROM_GPU));
162 b->mapped=request==AMDKFD_IOC_MAP_MEMORY_TO_GPU;
163 a.n_success=1; wr(p,&a,sizeof a); break;
164 }
165 case AMDKFD_IOC_FREE_MEMORY_OF_GPU: {
166 struct kfd_ioctl_free_memory_of_gpu_args a; rd(p,&a,sizeof a);
167 struct alloc *b=findalloc(a.handle); CHECK(!b->mapped); b->freed=1; break;
168 }
169 case AMDKFD_IOC_CREATE_QUEUE:
170 rd(p,&queue,sizeof queue);
171 for(size_t i=0;i<sizeof queue/8;i++) {
172 uint64_t value; memcpy(&value,(char *)&queue+i*8,8);
173 fprintf(stderr,"TEST_SIM queue_input_qword_%zu=%llu\n",i,(unsigned long long)value);
174 }
175 CHECK(count==allocation_count && !queued && queue.gpu_id==expected_gpu && queue.queue_type==KFD_IOC_QUEUE_TYPE_COMPUTE_AQL);
176 CHECK(queue.ring_base_address==allocs[ring_index].va && queue.ring_size==(planmode?2048:1024));
177 CHECK(allocs[ring_index].flags & KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE);
178 CHECK(queue.write_pointer_address==allocs[ring_index+2].va+56 && queue.read_pointer_address==allocs[ring_index+2].va+128);
179 /* Independent queue image, including all unused bytes and the inactive signal. */
180 uint64_t queue_image[512]={0};
181 queue_image[0]=UINT64_C(1)<<32;
182 queue_image[1]=allocs[ring_index].va;
183 queue_image[3]=planmode?32:16;
184 queue_image[4]=UINT64_MAX;
185 queue_image[8]=UINT64_C(0x2000000010000);
186 queue_image[9]=(UINT64_C(31)<<32)|(getenv("KFD_TEST_CTX_BOUND")?303:7);
187 queue_image[17]=128;
188 queue_image[22]=UINT64_C(2)<<32;
189 queue_image[24]=allocs[ring_index+1].va;
190 for (size_t off=0;off<4096;off+=8)
191 CHECK(word(allocs[ring_index+2].va+off)==queue_image[off/8]);
192 for (size_t off=0;off<4096;off+=8)
193 CHECK(word(allocs[ring_index+1].va+off)==(off==0 ? 1 : 0));
194 CHECK(!queue.eop_buffer_address && !queue.eop_buffer_size);
195 CHECK(queue.ctx_save_restore_address==allocs[ring_index+3].va && queue.ctx_save_restore_size<=allocs[ring_index+3].size);
196 CHECK(queue.ctl_stack_size<=queue.ctx_save_restore_size);
197 if(getenv("KFD_TEST_CTX_BOUND")) {
198 /* ROCT topology: NumShaderBanks=4/2=2; queues: min(304*40,2/2*512)=512. */
199 uint64_t ctl=8192,wg=UINT64_C(304)*(0x80000+0x4000+0x10000+0x1000);
200 CHECK(queue.ctl_stack_size==ctl && queue.ctx_save_restore_size==ctl+wg);
201 CHECK(word(queue.ctx_save_restore_address+16)==((UINT64_C(16384)<<32) | (ctl+wg)));
202 }
203 queue.queue_id=7; queue.doorbell_offset=8; wr(p,&queue,sizeof queue); queued=1; break;
204 case AMDKFD_IOC_DESTROY_QUEUE: {
205 struct kfd_ioctl_destroy_queue_args a; rd(p,&a,sizeof a);
206 CHECK(queued && a.queue_id==7 && packets); queued=0; break;
207 }
208 default: fail("unexpected KFD request");
209 }
210}
211static void complete_packets(void) {
212 uint64_t nw=word(queue.write_pointer_address);
213 while ((uint64_t)packets<nw) {
214 uint64_t p=queue.ring_base_address+(uint64_t)(packets%(planmode?32:16))*64;
215 uint32_t h; rd(p,&h,4); CHECK(h==0x21502);
216 uint16_t wg[3]; rd(p+4,wg,sizeof wg); CHECK(wg[0]==64 && wg[1]==1 && wg[2]==1);
217 uint64_t kd=word(p+32),args=word(p+40),sig=word(p+56);
218 /* HSA packet private/group ordering is independent of the AMD descriptor order. */
219 uint64_t segments=word(kd);
220 CHECK(word(p+24)==((segments&UINT64_C(0xffffffff))<<32 | segments>>32));
221 if(planmode) {
222 size_t ci=1+(size_t)(packets%2);
223 CHECK(kd>=allocs[ci].va && kd+64<=allocs[ci].va+allocs[ci].size);
224 CHECK(args==allocs[0].va+(uint64_t)(packets%20)*32);
225 CHECK(sig==((packets%20)==19 ? allocs[ring_index+4].va : 0));
226 } else {
227 CHECK(kd>=allocs[0].va && kd+64<=allocs[0].va+allocs[0].size);
228 CHECK(args==allocs[1].va && sig==allocs[ring_index+4].va);
229 }
230 uint64_t a=word(args),b=word(args+8),out=word(args+16);
231 uint32_t n; rd(args+24,&n,4); CHECK(n==256);
232 if(planmode) CHECK(a==allocs[3].va && b==allocs[3].va+4096 && out==allocs[4].va);
233 else CHECK(a==allocs[3].va && b==allocs[4].va && out==allocs[2].va);
234 if(!getenv("KFD_TEST_NO_OUTPUT"))
235 for(uint32_t i=0;i<n;i++) { float x,y,z; rd(a+4*i,&x,4); rd(b+4*i,&y,4); z=x+y; wr(out+4*i,&z,4); }
236 if(sig) {
237 CHECK(word(sig)==1 && word(sig+8));
238 if(!getenv("KFD_TEST_NO_SIGNAL")) setword(sig+8,word(sig+8)-1);
239 }
240 packets++; setword(queue.read_pointer_address,(uint64_t)packets);
241 }
242}
243int main(int argc,char **argv) {
244 CHECK(argc>=4);
245 planmode=getenv("KFD_TEST_PLAN")!=NULL;
246 if(planmode) { ring_index=5; allocation_count=10; }
247 if(getenv("KFD_TEST_GPU")) expected_gpu=(unsigned)strtoul(getenv("KFD_TEST_GPU"),0,10);
248 if(getenv("KFD_TEST_MINOR")) expected_minor=(unsigned)strtoul(getenv("KFD_TEST_MINOR"),0,10);
249 child=fork(); CHECK(child>=0);
250 if(!child) { CHECK(!ptrace(PTRACE_TRACEME,0,0,0)); raise(SIGSTOP); execv(argv[2],argv+2); _exit(92); }
251 int st; CHECK(waitpid(child,&st,0)==child && WIFSTOPPED(st));
252 CHECK(!ptrace(PTRACE_SETOPTIONS,child,0,PTRACE_O_TRACESYSGOOD|PTRACE_O_EXITKILL));
253 int enter=1,kind=0,changed=0; uint64_t call=0,a[6]={0}; char old[4096];
254 const char *prefix="/sys/class/kfd/kfd/topology/nodes/";
255 for(;;) {
256 CHECK(!ptrace(PTRACE_SYSCALL,child,0,0)); CHECK(waitpid(child,&st,0)==child);
257 if(WIFEXITED(st)) {
258 fprintf(stderr,"TEST_SIM child_exit=%d allocations=%zu packets=%d queued=%d\n",WEXITSTATUS(st),count,packets,queued);
259 CHECK(WEXITSTATUS(st)==0 && count==allocation_count && packets && !queued);
260 for(size_t i=0;i<count;i++) CHECK(allocs[i].freed);
261 fprintf(stderr,"TEST_SIM PASS allocations=%zu packets=%d freed=all device=NOT_RUN\n",count,packets);
262 free(allocs); close(kback); close(dback); return 0;
263 }
264 CHECK(WIFSTOPPED(st)); if(WSTOPSIG(st)==SIGTRAP) continue;
265 CHECK(WSTOPSIG(st)==(SIGTRAP|0x80));
266 struct user_pt_regs r; struct iovec v={&r,sizeof r};
267 CHECK(!ptrace(PTRACE_GETREGSET,child,(void *)NT_PRSTATUS,&v));
268 if(enter) {
269 call=r.regs[8]; memcpy(a,r.regs,sizeof a); kind=0; changed=0; denied=0;
270 if(call==SYS_openat) {
271 string(a[1],old,sizeof old);
272 if(!strcmp(old,"/dev/kfd") || !strncmp(old,"/dev/dri/renderD",sizeof("/dev/dri/renderD")-1)) {
273 kind=!strcmp(old,"/dev/kfd")?1:2;
274 if(kind==2) {
275 CHECK(strtoul(old+strlen("/dev/dri/renderD"),0,10)==expected_minor);
276 if(getenv("KFD_TEST_RENDER_ABSENT")) {
277 denied=ENOENT; replace_call(&r,SYS_getpid); kind=0;
278 }
279 }
280 if(!denied) { r.regs[0]=a[1]; r.regs[1]=0; replace_call(&r,SYS_memfd_create); }
281 } else if(!strncmp(old,prefix,strlen(prefix))) {
282 const char *deny=getenv("KFD_TEST_DENY");
283 if(deny && !strcmp(old+strlen(prefix),deny)) {
284 denied=EPERM; replace_call(&r,SYS_getpid);
285 } else {
286 char path[4096]; int n=snprintf(path,sizeof path,"%s/%s",argv[1],old+strlen(prefix));
287 CHECK(n>0 && (size_t)n<=strlen(old)); wr(a[1],path,(size_t)n+1); changed=1;
288 }
289 } else if(!strncmp(old,"/dev/",5)) fail("unexpected device open");
290 } else if(call==SYS_ioctl) { CHECK((int)a[0]==kfd); replace_call(&r,SYS_getpid); }
291 else if(call==SYS_newfstatat) { string(a[1],old,sizeof old); CHECK(!strcmp(old,"/dev/kfd")); replace_call(&r,SYS_getpid); }
292 else if(call==SYS_mmap && ((drm>=0 && (int)a[4]==drm) || (kfd>=0 && (int)a[4]==kfd))) {
293 if((int)a[4]==drm) {
294 size_t i; for(i=0;i<count;i++) if(a[5]==allocs[i].off) break;
295 CHECK(i<count && a[0]==allocs[i].va && a[1]==allocs[i].size && a[3]==(MAP_SHARED|MAP_FIXED));
296 } else CHECK(a[1]==8192 && a[5]==0 && a[3]==MAP_SHARED);
297 fprintf(stderr,"TEST_SIM mmap fd_kind=%s fd=%llu offset=%llu bytes=%llu va=%llx\n",(int)a[4]==drm?"render":"kfd",(unsigned long long)a[4],(unsigned long long)a[5],(unsigned long long)a[1],(unsigned long long)a[0]);
298 }
299 } else {
300 if(call==SYS_openat) {
301 if(denied) { r.regs[0]=(uint64_t)-(int64_t)denied; setregs(&r); }
302 if(changed) wr(a[1],old,strlen(old)+1);
303 if(changed && !strcmp(old,prefix)) topology_fd=(int)r.regs[0];
304 if(kind) { CHECK((int64_t)r.regs[0]>=0); int fd=(int)r.regs[0];
305 if(kind==1) { kfd=fd; kback=backing(fd); CHECK(!ftruncate(kback,8192)); }
306 else { drm=fd; dback=backing(fd); }
307 }
308 } else if(call==SYS_ioctl) { ioctl_reply(a[1],a[2]); r.regs[0]=0; setregs(&r); }
309 else if(call==SYS_fstat && ((int)a[0]==kfd || (int)a[0]==drm)) {
310 struct stat s; rd(a[1],&s,sizeof s); s.st_mode=S_IFCHR|0600;
311 s.st_rdev=(int)a[0]==drm?makedev(226,expected_minor):makedev(234,0); wr(a[1],&s,sizeof s);
312 } else if(call==SYS_newfstatat) {
313 struct stat s={0}; s.st_mode=S_IFCHR|0600; s.st_rdev=makedev(234,0); wr(a[2],&s,sizeof s); r.regs[0]=0; setregs(&r);
314 } else if(call==SYS_getdents64 && (int)a[0]==topology_fd && (int64_t)r.regs[0]>0 && getenv("KFD_TEST_SORT_NODES")) {
315 ordered_entries(a[1],(size_t)r.regs[0]);
316 } else if(call==SYS_clock_gettime && queued && ++clocks>=2) {
317 if(clocks==2) {
318 fprintf(stderr,"TEST_SIM packet_kernel_object=%llu\n",(unsigned long long)word(queue.ring_base_address+32));
319 fprintf(stderr,"TEST_SIM packet_kernarg_address=%llu\n",(unsigned long long)word(queue.ring_base_address+40));
320 }
321 if(getenv("KFD_TEST_EXPIRE") && word(queue.write_pointer_address)>(uint64_t)packets) elapsed_seconds+=11;
322 if(!getenv("KFD_TEST_NO_COMPLETE")) complete_packets();
323 if(getenv("KFD_TEST_EXPIRE")) {
324 struct timespec now; rd(a[1],&now,sizeof now); now.tv_sec+=elapsed_seconds; wr(a[1],&now,sizeof now);
325 }
326 }
327 }
328 enter=!enter;
329 }
330}