• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <sys/mman.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 
map_memory(const char * fn,unsigned base,unsigned size)8 static void *map_memory(const char *fn, unsigned base, unsigned size)
9 {
10     int fd;
11     void *ptr;
12 
13     fd = open(fn, O_RDWR | O_SYNC);
14     if(fd < 0) {
15         perror("cannot open %s for mapping");
16         return MAP_FAILED;
17     }
18 
19     ptr = mmap(0, size, PROT_READ | PROT_WRITE,
20                MAP_SHARED, fd, base);
21     close(fd);
22 
23     if(ptr == MAP_FAILED) {
24         fprintf(stderr,"cannot map %s (@%08x,%08x)\n", fn, base, size);
25     }
26     return ptr;
27 }
28 
29 
main(int argc,char ** argv)30 int main(int argc, char** argv)
31 {
32     void *grp_regs = map_memory("/dev/hw3d", 0, 1024 * 1024);
33     printf("GPU base mapped at %p\n", grp_regs);
34     int state_offset = 0x10140;
35     printf("GPU state = %08lx\n",
36             *((long*)((char*)grp_regs + state_offset))  );
37 
38     return 0;
39 }
40