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

Exception or error:

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?

How to solve:

You should be using mmap64 here. The address has to be mapped into an area that a 32-bit process can use. However, I strongly advise that you get a true 64-bit version of this application. You’re heading down a rabbit-hole here and there’s a lot of rabbit-poo in that hole, if you catch my drift …


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.

Leave a Reply

Your email address will not be published. Required fields are marked *