Search code examples
cmmap

How to mmap() 64-bit address into 32-bit process?


I'm trying to map device memory residing on 64-bit address into 32-bit process on 64-bit OS. I'm using the following lines

baseaddr = addr & ~(sysconf(_SC_PAGE_SIZE) - 1);
fd = open("/dev/mem", O_RDONLY | O_SYNC);
base_ptr = mmap(0, 4096, PROT_READ, MAP_PRIVATE, fd, baseaddr);

baseaddr is uint64_t and is higher than 4GB.

and I compile with -D_FILE_OFFSET_BITS=64. When I run the program it returns EINVAL. It worked before without the -D_FILE_OFFSET_BITS=64, but it'd only use the lower 32-bits of baseaddr - that I conclude by output of pmap -d showing the lower 32-bits of my desired address.

Any ideas what am I doing wrong?


Solution

  • Ok my final solution was to add mixed 32/64 assembly code that switches to long mode, loads long registers with desired values and then syscalls without any sort of wrapper and switches back to protected. Works like a charm.