17.12.2.1.2. /dev/mem
/dev/mem
exposes access to physical addresses, and we use it through the convenient devmem
BusyBox utility.
First launch linux/virt_to_phys_user.out
as described at Userland physical address experiments.
Next, read from the physical address:
devmem 0x7c7b800
Possible output:
Memory mapped at address 0x7ff7dbe01000. Value at address 0X7C7B800 (0x7ff7dbe01800): 0x12345678
which shows that the physical memory contains the expected value 0x12345678
.
0x7ff7dbe01000
is a new virtual address that devmem
maps to the physical address to be able to read from it.
Modify the physical memory:
devmem 0x7c7b800 w 0x9abcdef0
After one second, we see on the screen:
i 9abcdef0 [1]+ Done ./posix/virt_to_phys_test.out
so the value changed, and the while
loop exited!
This example requires:
-
CONFIG_STRICT_DEVMEM=n
, otherwisedevmem
fails with:devmem: mmap: Operation not permitted
-
nopat
kernel parameter
which we set by default.
Bibliography: https://stackoverflow.com/questions/11891979/how-to-access-mmaped-dev-mem-without-crashing-the-linux-kernel