diff --git a/kernel_module/seq_file.c b/kernel_module/seq_file.c index 3078273..e269904 100644 --- a/kernel_module/seq_file.c +++ b/kernel_module/seq_file.c @@ -31,7 +31,7 @@ Bibliography: #include #include /* pr_info */ #include /* seq_read, seq_lseek, single_release */ -#include +#include /* kmalloc, kfree */ #include /* S_IRUSR */ static int max = 2; diff --git a/kernel_module/virt_to_phys.c b/kernel_module/virt_to_phys.c index 391d4db..0a7b234 100644 --- a/kernel_module/virt_to_phys.c +++ b/kernel_module/virt_to_phys.c @@ -7,6 +7,10 @@ and on QEMU monitor: xp 0x +x86 docs say it only works for kmalloc. + +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 */ @@ -18,20 +22,21 @@ and on QEMU monitor: #include #include #include /* single_open, single_release */ +#include /* kmalloc, kfree */ -static volatile u32 i = 0x12345678; +static volatile u32 *i; static struct dentry *debugfs_file; static int show(struct seq_file *m, void *v) { seq_printf(m, - "i 0x%llx\n" - "&i %p\n" + "*i 0x%llx\n" + "i %p\n" "virt_to_phys 0x%llx\n", - (unsigned long long)i, - &i, - (unsigned long long)virt_to_phys(&i) + (unsigned long long)*i, + i, + (unsigned long long)virt_to_phys((void *)i) ); return 0; } @@ -51,6 +56,8 @@ static const struct file_operations fops = { static int myinit(void) { + i = kmalloc(sizeof(loff_t), GFP_KERNEL); + *i = 0x12345678; debugfs_file = debugfs_create_file( "lkmc_virt_to_phys", S_IRUSR, NULL, NULL, &fops); return 0; @@ -59,6 +66,7 @@ static int myinit(void) static void myexit(void) { debugfs_remove(debugfs_file); + kfree((void *)i); } module_init(myinit) diff --git a/rootfs_overlay/virt_to_phys.sh b/rootfs_overlay/virt_to_phys.sh index 727cc3a..2eaa9f4 100755 --- a/rootfs_overlay/virt_to_phys.sh +++ b/rootfs_overlay/virt_to_phys.sh @@ -1,8 +1,10 @@ #!/bin/sh set -ex insmod /virt_to_phys.ko -cat /sys/kernel/debug/lkmc_virt_to_phys -addr=$(grep virt_to_phys /sys/kernel/debug/lkmc_virt_to_phys | cut -d ' ' -f 2) +cd /sys/kernel/debug +cat lkmc_virt_to_phys +addr=$(grep virt_to_phys lkmc_virt_to_phys | cut -d ' ' -f 2) devmem2 "$addr" -devmem2 "$addr" w 0x87654321 +devmem2 "$addr" w 0x9ABCDEF0 +cat lkmc_virt_to_phys rmmod virt_to_phys