I am trying to figure out how to directly read any location in physical memory on a Power9 processor using /dev/mem. The following is the code that I have used to that end.
FILE* fp;
int _fdmem;
int *map = NULL;
const char memDevice[] = "/dev/mem";
_fdmem = open( "/dev/mem", O_RDWR | O_SYNC );
if (_fdmem < 0){
printf("Failed to open the /dev/mem !\n");
return 0;
}
else{
printf("open /dev/mem successfully !\n");
}
map= (int *)(mmap(NULL,1,PROT_READ|PROT_WRITE,MAP_PRIVATE,_fdmem,0));
fp=fopen("./memm2out.txt","w");
for (int i=0; i<131073;i++)
{
fprintf(fp, "%x",*(map+i));
}
When I run the code I get the following output.
open /dev/mem successfully !
Segmentation fault
Using GDB, this is where the segfault occurs
Program received signal SIGSEGV, Segmentation fault.
0x0000000100000a2c in main () at memmapper2.c:34
34 fprintf(fp, "%x",*(map+i));
When I use 131072 as the value for i there is no segfault, which makes me believe there is some kind of boundary to reading after 64 kB. The file is run with root permissions. The processor I'm using is running Linux kernel version 4.18. What may be restricting my access?
Edit: When the second mmap parameter is set to any value greater than 65536, mmap fails to open and gives the error message ": OPERATION NOT PERMITTED". Now, it's my understanding that what's happening here is that mmap maps the file pointer for /dev/mem to a specific location in virtual memory from which you can invoke the functionality of /dev/mem to read physical memory. Am I mistaken in this understanding?