1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_EXECMEM_ALLOC_H
3 #define _LINUX_EXECMEM_ALLOC_H
5 #include <linux/types.h>
6 #include <linux/moduleloader.h>
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)
13 #define MODULE_ALIGN PAGE_SIZE
17 * enum execmem_type - types of executable memory ranges
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.
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
38 EXECMEM_MODULE_TEXT
= EXECMEM_DEFAULT
,
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
51 enum execmem_range_flags
{
52 EXECMEM_KASAN_SHADOW
= (1 << 0),
53 EXECMEM_ROX_CACHE
= (1 << 1),
56 #ifdef CONFIG_ARCH_HAS_EXECMEM_ROX
58 * execmem_fill_trapping_insns - set memory to contain instructions that
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
64 * A hook for architecures to fill execmem ranges with invalid instructions.
65 * Architectures that use EXECMEM_ROX_CACHE must implement this.
67 void execmem_fill_trapping_insns(void *ptr
, size_t size
, bool writable
);
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
82 struct execmem_range
{
85 unsigned long fallback_start
;
86 unsigned long fallback_end
;
88 unsigned int alignment
;
89 enum execmem_range_flags flags
;
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
100 struct execmem_range ranges
[EXECMEM_TYPE_MAX
];
104 * execmem_arch_setup - define parameters for allocations of executable memory
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.
110 * For architectures that do not implement this method a default set of
111 * parameters will be used
113 * Return: a structure defining architecture parameters and restrictions
114 * for allocations of executable memory
116 struct execmem_info
*execmem_arch_setup(void);
119 * execmem_alloc - allocate executable memory
120 * @type: type of the allocation
121 * @size: how many bytes of memory are required
123 * Allocates memory that will contain executable code, either generated or
124 * loaded from kernel modules.
126 * Allocates memory that will contain data coupled with executable code,
127 * like data sections in kernel modules.
129 * The memory will have protections defined by architecture for executable
130 * region of the @type.
132 * Return: a pointer to the allocated memory or %NULL
134 void *execmem_alloc(enum execmem_type type
, size_t size
);
137 * execmem_free - free executable memory
138 * @ptr: pointer to the memory that should be freed
140 void execmem_free(void *ptr
);
144 * execmem_vmap - create virtual mapping for EXECMEM_MODULE_DATA memory
145 * @size: size of the virtual mapping in bytes
147 * Maps virtually contiguous area in the range suitable for EXECMEM_MODULE_DATA.
149 * Return: the area descriptor on success or %NULL on failure.
151 struct vm_struct
*execmem_vmap(size_t size
);
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
160 * Copy @size bytes from @src to @dst using text poking if the memory at
163 * Return: a pointer to @dst or NULL on error
165 void *execmem_update_copy(void *dst
, const void *src
, size_t size
);
168 * execmem_is_rox - check if execmem is read-only
169 * @type - the execmem type to check
171 * Return: %true if the @type is read-only, %false if it's writable
173 bool execmem_is_rox(enum execmem_type type
);
175 #if defined(CONFIG_EXECMEM) && !defined(CONFIG_ARCH_WANTS_EXECMEM_LATE)
176 void execmem_init(void);
178 static inline void execmem_init(void) {}
181 #endif /* _LINUX_EXECMEM_ALLOC_H */