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 / publication.c

67 lines · 3288 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 * CPU-only AArch64 instruction-step observer of real Omega packet stores. */
3#define _GNU_SOURCE
4#include <asm/ptrace.h>
5#include <elf.h>
6#include <errno.h>
7#include <signal.h>
8#include <stdint.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <sys/ptrace.h>
13#include <sys/syscall.h>
14#include <sys/uio.h>
15#include <sys/wait.h>
16#include <unistd.h>
17static pid_t child;
18static void fail(const char *s) { fprintf(stderr,"PUBLICATION FAIL %s\n",s); exit(1); }
19#define CHECK(x) do { if(!(x)) fail(#x); } while(0)
20static void readmem(uint64_t a,void *p,size_t n) {
21 for(size_t i=0;i<n;i+=8) {
22 errno=0; long v=ptrace(PTRACE_PEEKDATA,child,(void *)(uintptr_t)(a+i),0);
23 CHECK(!errno); memcpy((char *)p+i,&v,n-i<8?n-i:8);
24 }
25}
26static uint64_t u64(uint64_t a) { uint64_t v; readmem(a,&v,8); return v; }
27int main(int argc,char **argv) {
28 CHECK(argc==2); child=fork(); CHECK(child>=0);
29 if(!child) { CHECK(!ptrace(PTRACE_TRACEME,0,0,0)); raise(SIGSTOP); execl(argv[1],argv[1],(char *)0); _exit(92); }
30 int st; CHECK(waitpid(child,&st,0)==child && WIFSTOPPED(st));
31 CHECK(!ptrace(PTRACE_SETOPTIONS,child,0,PTRACE_O_TRACESYSGOOD|PTRACE_O_EXITKILL));
32 int entry=1,armed=0,stepping=0,published=0,rang=0; uint64_t a[6]={0};
33 for(;;) {
34 CHECK(!ptrace(stepping?PTRACE_SINGLESTEP:PTRACE_SYSCALL,child,0,0));
35 CHECK(waitpid(child,&st,0)==child);
36 if(WIFEXITED(st)) { CHECK(WEXITSTATUS(st)==0 && published && rang); fprintf(stderr,"PUBLICATION PASS body-before-header-before-wptr-before-doorbell\n"); return 0; }
37 CHECK(WIFSTOPPED(st));
38 if(!stepping) {
39 if(WSTOPSIG(st)==SIGTRAP) continue;
40 CHECK(WSTOPSIG(st)==(SIGTRAP|0x80));
41 struct user_pt_regs r; struct iovec v={&r,sizeof r};
42 CHECK(!ptrace(PTRACE_GETREGSET,child,(void *)NT_PRSTATUS,&v));
43 if(entry && r.regs[8]==SYS_write && r.regs[0]==1) {
44 CHECK(!armed && r.regs[2]==sizeof a); readmem(r.regs[1],a,sizeof a); armed=1;
45 }
46 if(!entry && armed) stepping=1;
47 entry=!entry;
48 } else {
49 CHECK(WSTOPSIG(st)==SIGTRAP);
50 uint32_t h=(uint32_t)u64(a[0]);
51 if((h&65535)==0x1502) {
52 CHECK(h==0x21502);
53 uint16_t wg[3]; uint32_t grid[3];
54 readmem(a[0]+4,wg,sizeof wg); readmem(a[0]+12,grid,sizeof grid);
55 CHECK(wg[0]==64 && wg[1]==1 && wg[2]==1);
56 CHECK(grid[0]==256 && grid[1]==1 && grid[2]==1);
57 /* HSA dispatch private at 24, group at 28; descriptor order is the reverse. */
58 CHECK(u64(a[0]+24)==((UINT64_C(4096)<<32) | (getenv("EXPECTED_PRIVATE")?strtoul(getenv("EXPECTED_PRIVATE"),NULL,10):0)) && u64(a[0]+32)==a[1]+64);
59 CHECK(u64(a[0]+40)==a[2] && !u64(a[0]+48) && u64(a[0]+56)==a[3]);
60 if(!published) { unsigned char packet[64]; readmem(a[0],packet,64); CHECK(write(1,packet,64)==64); }
61 published=1;
62 } else CHECK(h==1);
63 if(u64(a[4])) CHECK(published && u64(a[4])==1);
64 if(u64(a[5])!=UINT64_MAX) { CHECK(published && u64(a[4])==1 && !u64(a[5])); rang=1; }
65 }
66 }
67}