mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 18:25:57 +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/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;
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user