Pyjong
Pyjong

Reputation: 3207

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?

Upvotes: 6

Views: 2224

Answers (2)

Pyjong
Pyjong

Reputation: 3207

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.

Upvotes: 0

Mike Robinson
Mike Robinson

Reputation: 8985

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 ...

Upvotes: 3

Related Questions