How to allocate an executable page in a Linux kernel module?

谁说胖子不能爱 提交于 2019-12-01 01:00:47

问题


I'm writing a Linux kernel module, and I'd like to allocate an executable page. Plain kmalloc returns a pointer within a non-executable page, and I get a kernel panic when executing code there. It has to work on Ubuntu Karmic x86, 2.6.31-20-generic-pae.


回答1:


#include <linux/vmalloc.h>
#include <asm/pgtype_types.h>
...
char *p = __vmalloc(byte_size, GFP_KERNEL, PAGE_KERNEL_EXEC);
...
if (p != NULL) vfree(p);



回答2:


/**
 * 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));
}


来源:https://stackoverflow.com/questions/2458819/how-to-allocate-an-executable-page-in-a-linux-kernel-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!