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>
7 #include <linux/cleanup.h>
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)
14 #define MODULE_ALIGN PAGE_SIZE
18 * enum execmem_type - types of executable memory ranges
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.
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
39 EXECMEM_MODULE_TEXT
= EXECMEM_DEFAULT
,
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
52 enum execmem_range_flags
{
53 EXECMEM_KASAN_SHADOW
= (1 << 0),
54 EXECMEM_ROX_CACHE
= (1 << 1),
57 #ifdef CONFIG_ARCH_HAS_EXECMEM_ROX
59 * execmem_fill_trapping_insns - set memory to contain instructions that
61 * @ptr: pointer to memory to fill
62 * @size: size of the range to fill
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
);
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
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.
78 * Return: 0 on success or negative error code on failure.
80 int execmem_restore_rox(void *ptr
, size_t size
);
82 static inline int execmem_restore_rox(void *ptr
, size_t size
) { return 0; }
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
97 struct execmem_range
{
100 unsigned long fallback_start
;
101 unsigned long fallback_end
;
103 unsigned int alignment
;
104 enum execmem_range_flags flags
;
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
114 struct execmem_info
{
115 struct execmem_range ranges
[EXECMEM_TYPE_MAX
];
119 * execmem_arch_setup - define parameters for allocations of executable memory
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.
125 * For architectures that do not implement this method a default set of
126 * parameters will be used
128 * Return: a structure defining architecture parameters and restrictions
129 * for allocations of executable memory
131 struct execmem_info
*execmem_arch_setup(void);
134 * execmem_alloc - allocate executable memory
135 * @type: type of the allocation
136 * @size: how many bytes of memory are required
138 * Allocates memory that will contain executable code, either generated or
139 * loaded from kernel modules.
141 * Allocates memory that will contain data coupled with executable code,
142 * like data sections in kernel modules.
144 * The memory will have protections defined by architecture for executable
145 * region of the @type.
147 * Return: a pointer to the allocated memory or %NULL
149 void *execmem_alloc(enum execmem_type type
, size_t size
);
152 * execmem_alloc_rw - allocate writable executable memory
153 * @type: type of the allocation
154 * @size: how many bytes of memory are required
156 * Allocates memory that will contain executable code, either generated or
157 * loaded from kernel modules.
159 * Allocates memory that will contain data coupled with executable code,
160 * like data sections in kernel modules.
162 * Forces writable permissions on the allocated memory and the caller is
163 * responsible to manage the permissions afterwards.
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.
169 * Return: a pointer to the allocated memory or %NULL
171 void *execmem_alloc_rw(enum execmem_type type
, size_t size
);
174 * execmem_free - free executable memory
175 * @ptr: pointer to the memory that should be freed
177 void execmem_free(void *ptr
);
179 DEFINE_FREE(execmem
, void *, if (_T
) execmem_free(_T
));
183 * execmem_vmap - create virtual mapping for EXECMEM_MODULE_DATA memory
184 * @size: size of the virtual mapping in bytes
186 * Maps virtually contiguous area in the range suitable for EXECMEM_MODULE_DATA.
188 * Return: the area descriptor on success or %NULL on failure.
190 struct vm_struct
*execmem_vmap(size_t size
);
194 * execmem_is_rox - check if execmem is read-only
195 * @type - the execmem type to check
197 * Return: %true if the @type is read-only, %false if it's writable
199 bool execmem_is_rox(enum execmem_type type
);
201 #if defined(CONFIG_EXECMEM) && !defined(CONFIG_ARCH_WANTS_EXECMEM_LATE)
202 void execmem_init(void);
204 static inline void execmem_init(void) {}
207 #endif /* _LINUX_EXECMEM_ALLOC_H */