virt_to_phys improve failed attempte to use kmalloc, but still fails

This commit is contained in:
Ciro Santilli
2017-07-14 13:49:10 +01:00
parent 63d8bbfc89
commit 5ec0858244
3 changed files with 20 additions and 10 deletions

View File

@@ -31,7 +31,7 @@ Bibliography:
#include <linux/module.h>
#include <linux/printk.h> /* pr_info */
#include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */
#include <linux/slab.h>
#include <linux/slab.h> /* kmalloc, kfree */
#include <uapi/linux/stat.h> /* S_IRUSR */
static int max = 2;

View File

@@ -7,6 +7,10 @@ and on QEMU monitor:
xp 0x<vaddr>
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 <linux/kthread.h>
#include <linux/module.h>
#include <linux/seq_file.h> /* single_open, single_release */
#include <linux/slab.h> /* 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)

View File

@@ -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