failed virt_to_phys attempt

This commit is contained in:
Ciro Santilli
2017-07-14 13:01:24 +01:00
parent 0f1fbaf026
commit 19f5c51b6b
3 changed files with 76 additions and 0 deletions

View File

@@ -27,6 +27,7 @@
1. [dep](dep.c) 1. [dep](dep.c)
1. [dep2](dep2.c) 1. [dep2](dep2.c)
1. [character_device](character_device.c) 1. [character_device](character_device.c)
1. [virt_to_phys](virt_to_phys.c)
1. Hardware device drivers 1. Hardware device drivers
1. [pci_min](pci_min.c) 1. [pci_min](pci_min.c)
1. [pci](pci.c) 1. [pci](pci.c)

View File

@@ -0,0 +1,67 @@
/*
TODO not working. Tested with both:
/virt_to_phys.sh
and on QEMU monitor:
xp 0x<vaddr>
- 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
*/
#include <asm/io.h> /* virt_to_phys */
#include <linux/debugfs.h>
#include <linux/delay.h> /* usleep_range */
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/seq_file.h> /* single_open, single_release */
MODULE_LICENSE("GPL");
static volatile u32 i = 0x12345678;
static struct dentry *debugfs_file;
static int show(struct seq_file *m, void *v)
{
seq_printf(m,
"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)
);
return 0;
}
static int open(struct inode *inode, struct file *file)
{
return single_open(file, show, NULL);
}
static const struct file_operations fops = {
.llseek = seq_lseek,
.open = open,
.owner = THIS_MODULE,
.read = seq_read,
.release = single_release,
};
static int myinit(void)
{
debugfs_file = debugfs_create_file(
"lkmc_virt_to_phys", S_IRUSR, NULL, NULL, &fops);
return 0;
}
static void myexit(void)
{
debugfs_remove(debugfs_file);
}
module_init(myinit)
module_exit(myexit)

8
rootfs_overlay/virt_to_phys.sh Executable file
View File

@@ -0,0 +1,8 @@
#!/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)
devmem2 "$addr"
devmem2 "$addr" w 0x87654321
rmmod virt_to_phys