3

I'm a bit disappointed there is almost no discussion of this no matter where I look so I guess I'll have to ask.

I'm writing a cross platform memory bench marking application which requires direct physical address mapping rather than virtual addressing.

EDIT The solution would look something like the Linux/Unix system calls:

int fd = open("/dev/mem", O_RDONLY);
mmap(NULL, len, PROT_READ, MAP_SHARED, fd, PHYSICAL_ADDRESS_OFFSET);

which will require the kernel to either give you a virtual page mapping to the desired physical address or return that it failed. This does require supervisor privileges but that is ok.

I have seen a lot of information about shared memory and memory mapped files but all of these reside on disc and are thus not really useful when I'm trying to make a system dependent read. It is very similar to writing an IO driver although I do no need write permissions to the physical address.

This site gives an example of how to do it on a driver level using the Windows Driver Kit:

NT Insider: Sharing Memory between drivers and applications

This solution would probably require Visual Studio which currently I do not have access to. (I have downloaded the WDK api but it complained about my use of GCC for Windows).

I'm traditionally a Linux programmer so I'm hoping there might be something really simple I'm missing. Thanks in advance if you know something I don't!

1
  • i don not think that Microsoft will be interested in making this kind of details public, also Windows is a closed source product, and never the less your application will likely run in the user space and not in the kernel space, far away from a direct access to the memory.
    – user827992
    Jul 10, 2012 at 19:24

2 Answers 2

2

I don't think your understanding of MAP_FIXED is correct. That tells the kernel to put the mapped memory at the given virtual address within the process address space. User-mode processes always use virtual addressing. In short, I don't think you can do what you are asking in Windows or any flavor of Unix or Linux.

1
  • You're absolutely right that MAP_FIXED would not do what I intended in terms of specifying an address. To use mmap apparently I should open /dev/mem and use the offset parameter as index into physical memory. Thank you so much for pointing out I got this wrong! Hopefully someone else finds this useful. Windows I doubt would relinquish this access as easily though but I'll keep looking into it. They have to have some accessible way to reach driver memory because DMA is a necessary feature for some embedded application
    – chrisjleaf
    Jul 10, 2012 at 21:01
2

The reason that there's not much discussion of physical memory mapping for user space apps is that user space apps can't GET physical memory mappings. Part of the definition of running in user space is that you are running in a virtual address space private to your process.

mmap with MAP_FIXED just lets you tell the system where to put the memory in your process address space. In other words, you get to specify the virtual address where the memory will show up. That's NOT a physical address in any way shape or form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.