flashburn flashburn - 1 year ago 94
C Question

why does mmap fail?

I'm given a physical address, specifically

. I'm trying to
it into my program. The code that I'm using is

void *mmap64;
off_t offset = 0x000000368d76c0;
int memFd = open("/dev/mem", O_RDWR);
if (-1 == memFd)
perror("Error ");

mmap64 = mmap(0, sizeof(uint64_t), PROT_WRITE | PROT_READ, MAP_SHARED, memFd, offset);
if (MAP_FAILED == mmap64) {
perror("Error ");
return -1;

For some reason when I run this code I get a failure on
. Specifically it says
Error Invalid argument
. I'm pretty sure it is because of the
value, but I don't know what is wrong with it.

I would appreciate any help on it.

Answer Source

According to mmap(2) - Linux manual page,

offset must be a multiple of the page size as returned by sysconf(_SC_PAGE_SIZE).

When the page size is 4096 (a page size used in x86 CPU), 0x000000368d76c0 is not a multiple of 4096 and will be considered as invalid.

For that reason, you will have to adjust the offset.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download