mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-27 12:04:27 +01:00
virt_to_phys improve failed attempte to use kmalloc, but still fails
This commit is contained in:
@@ -31,7 +31,7 @@ Bibliography:
|
|||||||
#include <linux/module.h>
|
#include <linux/module.h>
|
||||||
#include <linux/printk.h> /* pr_info */
|
#include <linux/printk.h> /* pr_info */
|
||||||
#include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */
|
#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 */
|
#include <uapi/linux/stat.h> /* S_IRUSR */
|
||||||
|
|
||||||
static int max = 2;
|
static int max = 2;
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ and on QEMU monitor:
|
|||||||
|
|
||||||
xp 0x<vaddr>
|
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/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/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/kthread.h>
|
||||||
#include <linux/module.h>
|
#include <linux/module.h>
|
||||||
#include <linux/seq_file.h> /* single_open, single_release */
|
#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 struct dentry *debugfs_file;
|
||||||
|
|
||||||
static int show(struct seq_file *m, void *v)
|
static int show(struct seq_file *m, void *v)
|
||||||
{
|
{
|
||||||
seq_printf(m,
|
seq_printf(m,
|
||||||
"i 0x%llx\n"
|
"*i 0x%llx\n"
|
||||||
"&i %p\n"
|
"i %p\n"
|
||||||
"virt_to_phys 0x%llx\n",
|
"virt_to_phys 0x%llx\n",
|
||||||
(unsigned long long)i,
|
(unsigned long long)*i,
|
||||||
&i,
|
i,
|
||||||
(unsigned long long)virt_to_phys(&i)
|
(unsigned long long)virt_to_phys((void *)i)
|
||||||
);
|
);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -51,6 +56,8 @@ static const struct file_operations fops = {
|
|||||||
|
|
||||||
static int myinit(void)
|
static int myinit(void)
|
||||||
{
|
{
|
||||||
|
i = kmalloc(sizeof(loff_t), GFP_KERNEL);
|
||||||
|
*i = 0x12345678;
|
||||||
debugfs_file = debugfs_create_file(
|
debugfs_file = debugfs_create_file(
|
||||||
"lkmc_virt_to_phys", S_IRUSR, NULL, NULL, &fops);
|
"lkmc_virt_to_phys", S_IRUSR, NULL, NULL, &fops);
|
||||||
return 0;
|
return 0;
|
||||||
@@ -59,6 +66,7 @@ static int myinit(void)
|
|||||||
static void myexit(void)
|
static void myexit(void)
|
||||||
{
|
{
|
||||||
debugfs_remove(debugfs_file);
|
debugfs_remove(debugfs_file);
|
||||||
|
kfree((void *)i);
|
||||||
}
|
}
|
||||||
|
|
||||||
module_init(myinit)
|
module_init(myinit)
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
set -ex
|
set -ex
|
||||||
insmod /virt_to_phys.ko
|
insmod /virt_to_phys.ko
|
||||||
cat /sys/kernel/debug/lkmc_virt_to_phys
|
cd /sys/kernel/debug
|
||||||
addr=$(grep virt_to_phys /sys/kernel/debug/lkmc_virt_to_phys | cut -d ' ' -f 2)
|
cat lkmc_virt_to_phys
|
||||||
|
addr=$(grep virt_to_phys lkmc_virt_to_phys | cut -d ' ' -f 2)
|
||||||
devmem2 "$addr"
|
devmem2 "$addr"
|
||||||
devmem2 "$addr" w 0x87654321
|
devmem2 "$addr" w 0x9ABCDEF0
|
||||||
|
cat lkmc_virt_to_phys
|
||||||
rmmod virt_to_phys
|
rmmod virt_to_phys
|
||||||
|
|||||||
Reference in New Issue
Block a user