【问题标题】:How to allocate an executable page in a Linux kernel module?如何在 Linux 内核模块中分配可执行页面?
【发布时间】:2011-01-28 08:46:25
【问题描述】:

我正在编写一个 Linux 内核模块,我想分配一个可执行页面。普通的kmalloc() 在一个不可执行的页面中返回一个指针,当在那里执行代码时我得到一个内核恐慌。它必须在 Ubuntu Karmic x86, 2.6.31-20-generic-pae 上运行。

【问题讨论】:

  • 我能问一下你为什么想要一个可执行页面吗?我很好奇。

标签: c memory-management linux-kernel executable virtual-memory


【解决方案1】:
/**
 * vmalloc_exec - allocate virtually contiguous, executable memory
 * @size:     allocation size
 *
 * Kernel-internal function to allocate enough pages to cover @size
 * the page level allocator and map them into contiguous and
 * executable kernel virtual space.
 *
 * For tight control over page level allocator and protection flags
 * use __vmalloc() instead.
 *
 * Return: pointer to the allocated memory or %NULL on error
 */
void *vmalloc_exec(unsigned long size)
{
    return __vmalloc_node(size, 1, GFP_KERNEL, PAGE_KERNEL_EXEC,
                  NUMA_NO_NODE, __builtin_return_address(0));
}

【讨论】:

    【解决方案2】:
    #include <linux/vmalloc.h>
    #include <asm/pgtype_types.h>
    ...
    char *p = __vmalloc(byte_size, GFP_KERNEL, PAGE_KERNEL_EXEC);
    ...
    if (p != NULL) vfree(p);
    

    【讨论】:

      猜你喜欢
      • 2019-08-10
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 2016-03-25
      • 2019-11-10
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多