PDA

View Full Version : /Dev/Mem




tr!pf!3
May 26, 2012, 09:21 AM
I'm using currently a Mac OSX 10.6 operating system. I'm working on some c++ programs and trying to figure more out about memory. I ended up finding some things that allowed me to create /dev/mem and /dev/kmem on my computer. I sort of understand how the nm and dd commands work, however now I'm trying to figure out how to grab this data from a c++ program. I noticed that there is a mmap function, although my computer is 64 bit, and on other OS's such as linux you have mmap64() function. If you take a look at sys/mman.h there is no mmap64() function. I really don't know where to go from here, some source code below, gives me a seg fault. Do I even need to bother worrying about an mmap64() function for this?

BTW this is not my code, I grabbed it from another source.

#define MAPPED_SIZE ...
#define DDR_RAM_PHYS ...

int main(int argc, char *argv[]) {
int _fdmem;
int *map = NULL;
const char memDevice[] = "/dev/mem";

_fdmem = open( memDevice, O_RDWR | O_SYNC );

if ( _fdmem < 0 ) {
printf("Failed to open the /dev/mem !\n");
return 0;
}
else{
printf("Opened /dev/mem successfully !\n");
}

map = (int *)(mmap( 0, MAPPED_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, _fdmem, DDR_RAM_PHYS ));

for (int i = 0; i < 100; i++)
printf("content: 0x%x\n",*(map+i));

if ( munmap( map, MAPPED_SIZE ) == -1 ) {
perror("Error un-mmapping the file");
}

close(_fdmem);
}



tr!pf!3
May 26, 2012, 10:44 AM
nvm i have things sorted, I'm just experimenting with things here.