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