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

前端 未结 2 1171
遇见更好的自我
遇见更好的自我 2020-12-19 04:14

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 ke

相关标签:
2条回答
  • 2020-12-19 04:44
    /**
     * 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));
    }
    
    0 讨论(0)
  • 2020-12-19 04:56
    #include <linux/vmalloc.h>
    #include <asm/pgtype_types.h>
    ...
    char *p = __vmalloc(byte_size, GFP_KERNEL, PAGE_KERNEL_EXEC);
    ...
    if (p != NULL) vfree(p);
    
    0 讨论(0)
提交回复
热议问题