From 2eca9280e12dbab79ccb67d0640b2a0edc2c9ffc Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Sun, 16 Jul 2017 11:30:47 +0100 Subject: [PATCH] virt_to_phys.sh also works with nopat --- buildroot_config_fragment | 3 +++ kernel_module/user/usermem.c | 6 +++--- kernel_module/virt_to_phys.c | 6 +----- rootfs_overlay/virt_to_phys.sh | 2 ++ 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/buildroot_config_fragment b/buildroot_config_fragment index ce48faf..7578b79 100644 --- a/buildroot_config_fragment +++ b/buildroot_config_fragment @@ -58,8 +58,11 @@ BR2_PACKAGE_HOST_DTC=y # # - https://superuser.com/questions/71389/what-is-dev-mem/1214662#1214662 # - https://unix.stackexchange.com/questions/4948/shell-command-to-read-device-registers +# - man mem # # TODO: why with mmap MAP_PRIVATE (used in my previous custom naive version), # the entire register page is read?; # +# TODO: have a look at: https://github.com/kaiwan/device-memory-readwrite +# BR2_PACKAGE_DEVMEM2=y diff --git a/kernel_module/user/usermem.c b/kernel_module/user/usermem.c index c5831ad..6a42c89 100644 --- a/kernel_module/user/usermem.c +++ b/kernel_module/user/usermem.c @@ -4,7 +4,7 @@ Only tested in x86_64. Provide an allocated userland memory address for us to test out kernel memory APIs, including: - /proc/pid/maps -- /proc/pid/pagemap +- /proc/pid/pagemap. See also: https://stackoverflow.com/questions/17021214/decode-proc-pid-pagemap-entry/45126141#45126141 - /dev/mem Usage: @@ -18,13 +18,13 @@ Outputs address and pid, e.g.: Now translate the virtual address to physical for the given PID: - /pagemap2.out 110 | grep 0x600000 + /pagemap_dump.out 110 | grep 0x600000 where 0x600000 is the page that contains 0x600800. This produces a line of type: - 0x600000 0x7c7b 0 0 0 1 /pagemap_test.out + 0x600000 0x7c7b 0 0 0 1 /usermem.out where 0x7c7b is the PFN. To get the physical address, just add three zeros back: diff --git a/kernel_module/virt_to_phys.c b/kernel_module/virt_to_phys.c index 9066e23..5433827 100644 --- a/kernel_module/virt_to_phys.c +++ b/kernel_module/virt_to_phys.c @@ -1,9 +1,5 @@ /* -TODO not working. Tested with both: - - /virt_to_phys.sh - -and on QEMU monitor: +Also try on QEMU monitor: xp 0x diff --git a/rootfs_overlay/virt_to_phys.sh b/rootfs_overlay/virt_to_phys.sh index 2eaa9f4..b0ab00e 100755 --- a/rootfs_overlay/virt_to_phys.sh +++ b/rootfs_overlay/virt_to_phys.sh @@ -3,8 +3,10 @@ set -ex insmod /virt_to_phys.ko cd /sys/kernel/debug cat lkmc_virt_to_phys +# *i = 0x12345678 addr=$(grep virt_to_phys lkmc_virt_to_phys | cut -d ' ' -f 2) devmem2 "$addr" devmem2 "$addr" w 0x9ABCDEF0 cat lkmc_virt_to_phys +# *i = 0x9ABCDEF0 rmmod virt_to_phys