3

The issue that I am experimenting is not related with open() or mmap() function, which are executed properly. I have disabled CONFIG_STRICT_DEVMEM in the kernel so I can read from /dev/mem without problems. Actually, I can do the following:

const char *path = "/dev/mem"
int fd = open(path, O_RDWR); /* read and write flags*/
p  = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, BASE_ADDR); /* read and write flags*/

And the code does not fail. Nonetheless, I am using this code to write in the PCI address space. So, basically the BASE_ADDR is 0xc000000, and the size is 256 MiB (0x10000000, all the PCI address space).

Said that, when I try to write on these positions (with a specific offset, BDF format), nothing is written; again the code does not fail, it just does not write anything.

In case my code was wrong, I tried BusyBox, with the following parameters:

[horro@ ~]$ sudo busybox devmem 0xc00b0a8c w 0xffffffff
[horro@ ~]$ sudo busybox devmem 0xc00b0a8c             
0x00000000

So, basically it is not writing anything.

horro
  • 1,262
  • 3
  • 20
  • 37
  • Your code has a typo: `MAP_SHARE` should be `MAP_SHARED`. But that's clearly not the problem. Please run `sudo strace -o /tmp/devmem.log.txt busybox devmem 0xc00b0a8c w 0xffffffff` and then put the file /tmp/devmem.log.txt somewhere we can see it. (It will be too long to incorporate into the question, and unfortunately we need to see the entire thing, unedited.) If you get "strace: command not found" then you will need to install the program first (most Linux distributions put it in a package also called "strace"). – zwol Oct 04 '17 at 15:58
  • Skimming the code at http://elixir.free-electrons.com/linux/latest/source/drivers/char/mem.c gives me the impression that what you are trying to do _is_ supposed to work - but I'm not a kernel hacker and I may have misunderstood it. – zwol Oct 04 '17 at 16:00
  • 1
    Are you sure that *something is mapped* to the address you're writing to? A PCI device needs to ask the BIOS or the OS for a physical mapping address. If that is not done, the device's memory space will remain isolated from the bus. – tofro Oct 04 '17 at 16:02
  • 2
    And the code does not fail. :) yes, you simply ignore the return code of open() function – Krassi Em Oct 04 '17 at 17:33
  • 1
    Also see [How to access kernel space from user space?](https://stackoverflow.com/q/9662193/608639), [How to access mmaped /dev/mem without crashing the Linux kernel?](https://stackoverflow.com/q/11891979/608639), [mmap of /dev/mem fails with invalid argument](https://stackoverflow.com/q/39134990/608639), etc. – jww Oct 04 '17 at 22:15

1 Answers1

0

There is a CONFIG_STRICT_DEVMEM kernel config option. My understanding is that it must be set at compile-time as CONFIG_STRICT_DEVMEM=n. This is a security reasons.

Dsrivast
  • 39
  • 1
  • 10