Search code examples
memory-managementlinux-kernelmmappcipci-e

How to map physical address from /proc/iomem into user space using mmap?


I want to map PCIe Memory mapped config space into the user space. I am trying to use mmap system call to map the MMCONFIG physical address into the user space. I did some search but not able to figure out what to populate in the fd parameter in-order to do the correct mapping.

mmap system call : void mmap(void addr, size_t length, int prot, int flags, int fd, off_t offset);

Physical address from /proc/iomem : e0000000-efffffff : PCI MMCONFIG 0000 [bus 00-ff]

I could access PCIe enhanced configuration space using port 0xcfc & 0xcf8 but i want to achieve the same by mapping physical address into user space.

Thanks in advance

System: RHEL / x86_64


Solution

  • mmap creates a mapping of a file in the address space of your application. You have the address you want to map to, the length you want to read, flags etc... and you need to specify which file you want to map to, and this is the fd argument.

    You have the /dev/mem file that gives you access to physical memory from user space, so you can open() it and use the returned fd to read the data at the desired physical address.

    However, I'm pretty sure that there is a kconfig thing called CONFIG_STRICT_DEVMEM that disables /dev/mem (let's you read only the first 1mb or so) that is the default setting on modern RHEL systems if I remember correctly, so you better make sure you can actually use it.