【问题标题】:How to use do_mmap() in kernel module如何在内核模块中使用 do_mmap()
【发布时间】:2019-03-23 19:51:46
【问题描述】:

我想在内核模块中使用 do_mmap()。根据this question,这应该是可能的。

这是一个最小的非工作示例:

hp_km.c:

#include <linux/module.h>
#include <linux/mm.h>

MODULE_LICENSE("GPL");

static int __init hp_km_init(void) {
   do_mmap(0, 0, 0, 0, 0, 0, 0, 0, 0);
   return 0;
}

static void __exit hp_km_exit(void) {
}

module_init(hp_km_init);
module_exit(hp_km_exit);
Makefile:

obj-m += hp_km.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

运行 make 导致 WARNING: "do_mmap" [...] undefined!

我需要在 hp_km.cMakefile 中进行哪些更改才能完成这项工作?

【问题讨论】:

  • 我认为你必须先导出符号。您可以通过在函数定义下方的 mmap.c 文件中添加EXPORT_SYMBOL(do_mmap); 来做到这一点,然后重新编译内核。您可以使用通过EXPORT_SYMBOL 导出的任何函数,但请注意do_mmap 尚未导出。

标签: c linux linux-kernel kernel-module


【解决方案1】:

除了重建内核外,还可以使用kallsyms_lookup_name查找符号对应的地址

如下:

#include <linux/module.h>
#include <linux/mm.h>
#include <linux/kallsyms.h>

MODULE_LICENSE("GPL");

unsigned long (*orig_do_mmap)(struct file *file, unsigned long addr,
                              unsigned long len, unsigned long prot,
                              unsigned long flags, vm_flags_t vm_flags,
                              unsigned long pgoff, unsigned long *populate,
                              struct list_head *uf);

static int __init hp_km_init(void)
{
    orig_do_mmap = (void*)kallsyms_lookup_name("do_mmap");
    if (orig_do_mmap == NULL)
        return -EINVAL;

    orig_do_mmap(0, 0, 0, 0, 0, 0, 0, 0, 0);
    return 0;
}

static void __exit hp_km_exit(void)
{
}

module_init(hp_km_init);
module_exit(hp_km_exit);

【讨论】:

    猜你喜欢
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 2014-04-22
    • 1970-01-01
    • 2012-01-31
    • 2012-09-27
    • 2013-01-17
    相关资源
    最近更新 更多