From 058c759ec90ac8661b7ca51e5bee127c3874a789 Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Sun, 16 Jul 2017 07:51:04 +0100 Subject: [PATCH] devmem on userland memory...!!!!!! --- kernel_module/user/usermem.c | 32 ++++++++++++++++++++++++++++++-- kernel_module/virt_to_phys.c | 1 + runqemu | 5 +++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/kernel_module/user/usermem.c b/kernel_module/user/usermem.c index cb77b52..c5831ad 100644 --- a/kernel_module/user/usermem.c +++ b/kernel_module/user/usermem.c @@ -30,6 +30,8 @@ where 0x7c7b is the PFN. To get the physical address, just add three zeros back: 0x7c7b000 +## QEMU monitor xp + Examine the physical memory from the QEMU monitor: on host: ./qemumonitor @@ -41,11 +43,37 @@ Output: Yes!!! We read the correct value from the physical address. -TODO: why does: +## /dev/mem + +Firts up, this requires: + +- CONFIG_STRICT_DEVMEM is not set. +- nopat on kernel parameters + +see: https://stackoverflow.com/questions/11891979/how-to-access-mmaped-dev-mem-without-crashing-the-linux-kernel + +Then: devmem2 0x7c7b800 -fail on the mmap? CONFIG_STRICT_DEVMEM is not set. +Possible output: + + Memory mapped at address 0x7ff7dbe01000. + Value at address 0X7C7B800 (0x7ff7dbe01800): 0x12345678 + +where 0x7ff7dbe01000 is a new virtual address that was mapped +to our physical address and given to the process that mapped /dev/mem. + +And finally, let's change the value! + + devmem2 0x7c7b800 w 0x9abcdef0 + +After one second, we see on the screen: + + i 9abcdef0 + [1]+ Done /usermem.out + +so the while loop was exited! */ #define _XOPEN_SOURCE 700 diff --git a/kernel_module/virt_to_phys.c b/kernel_module/virt_to_phys.c index 6b620fb..9066e23 100644 --- a/kernel_module/virt_to_phys.c +++ b/kernel_module/virt_to_phys.c @@ -13,6 +13,7 @@ static inline phys_addr_t virt_to_phys(volatile void *address) - https://stackoverflow.com/questions/5748492/is-there-any-api-for-determining-the-physical-address-from-virtual-address-in-li - https://stackoverflow.com/questions/43325205/can-we-use-virt-to-phys-for-user-space-memory-in-kernel-module +- https://stackoverflow.com/questions/39134990/mmap-of-dev-mem-fails-with-invalid-argument-but-address-is-page-aligned */ #include /* virt_to_phys */ diff --git a/runqemu b/runqemu index 71ab96c..4ef830e 100755 --- a/runqemu +++ b/runqemu @@ -8,7 +8,8 @@ debug=false debug_qemu='' kgdb=false nographic=false -extra_append='' +# norandmaps: reduce randomization to make our lives easier. +extra_append='norandmaps' extra_flags='' while getopts a:de:knqt: OPT; do case "$OPT" in @@ -61,7 +62,7 @@ case "$arch" in fi cmd="$qemu_common \ -M pc \ - -append 'root=/dev/vda $extra_append' \ + -append 'root=/dev/vda nopat $extra_append' \ -device edu \ -device lkmc_pci_min \ -drive file=${images_dir}/rootfs.ext2,if=virtio,format=raw \