at v6.14 5.8 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef _LINUX_EXECMEM_ALLOC_H 3#define _LINUX_EXECMEM_ALLOC_H 4 5#include <linux/types.h> 6#include <linux/moduleloader.h> 7 8#if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \ 9 !defined(CONFIG_KASAN_VMALLOC) 10#include <linux/kasan.h> 11#define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT) 12#else 13#define MODULE_ALIGN PAGE_SIZE 14#endif 15 16/** 17 * enum execmem_type - types of executable memory ranges 18 * 19 * There are several subsystems that allocate executable memory. 20 * Architectures define different restrictions on placement, 21 * permissions, alignment and other parameters for memory that can be used 22 * by these subsystems. 23 * Types in this enum identify subsystems that allocate executable memory 24 * and let architectures define parameters for ranges suitable for 25 * allocations by each subsystem. 26 * 27 * @EXECMEM_DEFAULT: default parameters that would be used for types that 28 * are not explicitly defined. 29 * @EXECMEM_MODULE_TEXT: parameters for module text sections 30 * @EXECMEM_KPROBES: parameters for kprobes 31 * @EXECMEM_FTRACE: parameters for ftrace 32 * @EXECMEM_BPF: parameters for BPF 33 * @EXECMEM_MODULE_DATA: parameters for module data sections 34 * @EXECMEM_TYPE_MAX: 35 */ 36enum execmem_type { 37 EXECMEM_DEFAULT, 38 EXECMEM_MODULE_TEXT = EXECMEM_DEFAULT, 39 EXECMEM_KPROBES, 40 EXECMEM_FTRACE, 41 EXECMEM_BPF, 42 EXECMEM_MODULE_DATA, 43 EXECMEM_TYPE_MAX, 44}; 45 46/** 47 * enum execmem_range_flags - options for executable memory allocations 48 * @EXECMEM_KASAN_SHADOW: allocate kasan shadow 49 * @EXECMEM_ROX_CACHE: allocations should use ROX cache of huge pages 50 */ 51enum execmem_range_flags { 52 EXECMEM_KASAN_SHADOW = (1 << 0), 53 EXECMEM_ROX_CACHE = (1 << 1), 54}; 55 56#ifdef CONFIG_ARCH_HAS_EXECMEM_ROX 57/** 58 * execmem_fill_trapping_insns - set memory to contain instructions that 59 * will trap 60 * @ptr: pointer to memory to fill 61 * @size: size of the range to fill 62 * @writable: is the memory poited by @ptr is writable or ROX 63 * 64 * A hook for architecures to fill execmem ranges with invalid instructions. 65 * Architectures that use EXECMEM_ROX_CACHE must implement this. 66 */ 67void execmem_fill_trapping_insns(void *ptr, size_t size, bool writable); 68#endif 69 70/** 71 * struct execmem_range - definition of an address space suitable for code and 72 * related data allocations 73 * @start: address space start 74 * @end: address space end (inclusive) 75 * @fallback_start: start of the secondary address space range for fallback 76 * allocations on architectures that require it 77 * @fallback_end: start of the secondary address space (inclusive) 78 * @pgprot: permissions for memory in this address space 79 * @alignment: alignment required for text allocations 80 * @flags: options for memory allocations for this range 81 */ 82struct execmem_range { 83 unsigned long start; 84 unsigned long end; 85 unsigned long fallback_start; 86 unsigned long fallback_end; 87 pgprot_t pgprot; 88 unsigned int alignment; 89 enum execmem_range_flags flags; 90}; 91 92/** 93 * struct execmem_info - architecture parameters for code allocations 94 * @ranges: array of parameter sets defining architecture specific 95 * parameters for executable memory allocations. The ranges that are not 96 * explicitly initialized by an architecture use parameters defined for 97 * @EXECMEM_DEFAULT. 98 */ 99struct execmem_info { 100 struct execmem_range ranges[EXECMEM_TYPE_MAX]; 101}; 102 103/** 104 * execmem_arch_setup - define parameters for allocations of executable memory 105 * 106 * A hook for architectures to define parameters for allocations of 107 * executable memory. These parameters should be filled into the 108 * @execmem_info structure. 109 * 110 * For architectures that do not implement this method a default set of 111 * parameters will be used 112 * 113 * Return: a structure defining architecture parameters and restrictions 114 * for allocations of executable memory 115 */ 116struct execmem_info *execmem_arch_setup(void); 117 118/** 119 * execmem_alloc - allocate executable memory 120 * @type: type of the allocation 121 * @size: how many bytes of memory are required 122 * 123 * Allocates memory that will contain executable code, either generated or 124 * loaded from kernel modules. 125 * 126 * Allocates memory that will contain data coupled with executable code, 127 * like data sections in kernel modules. 128 * 129 * The memory will have protections defined by architecture for executable 130 * region of the @type. 131 * 132 * Return: a pointer to the allocated memory or %NULL 133 */ 134void *execmem_alloc(enum execmem_type type, size_t size); 135 136/** 137 * execmem_free - free executable memory 138 * @ptr: pointer to the memory that should be freed 139 */ 140void execmem_free(void *ptr); 141 142#ifdef CONFIG_MMU 143/** 144 * execmem_vmap - create virtual mapping for EXECMEM_MODULE_DATA memory 145 * @size: size of the virtual mapping in bytes 146 * 147 * Maps virtually contiguous area in the range suitable for EXECMEM_MODULE_DATA. 148 * 149 * Return: the area descriptor on success or %NULL on failure. 150 */ 151struct vm_struct *execmem_vmap(size_t size); 152#endif 153 154/** 155 * execmem_update_copy - copy an update to executable memory 156 * @dst: destination address to update 157 * @src: source address containing the data 158 * @size: how many bytes of memory shold be copied 159 * 160 * Copy @size bytes from @src to @dst using text poking if the memory at 161 * @dst is read-only. 162 * 163 * Return: a pointer to @dst or NULL on error 164 */ 165void *execmem_update_copy(void *dst, const void *src, size_t size); 166 167/** 168 * execmem_is_rox - check if execmem is read-only 169 * @type - the execmem type to check 170 * 171 * Return: %true if the @type is read-only, %false if it's writable 172 */ 173bool execmem_is_rox(enum execmem_type type); 174 175#if defined(CONFIG_EXECMEM) && !defined(CONFIG_ARCH_WANTS_EXECMEM_LATE) 176void execmem_init(void); 177#else 178static inline void execmem_init(void) {} 179#endif 180 181#endif /* _LINUX_EXECMEM_ALLOC_H */