Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
69/**
70 * execmem_make_temp_rw - temporarily remap region with read-write
71 * permissions
72 * @ptr: address of the region to remap
73 * @size: size of the region to remap
74 *
75 * Remaps a part of the cached large page in the ROX cache in the range
76 * [@ptr, @ptr + @size) as writable and not executable. The caller must
77 * have exclusive ownership of this range and ensure nothing will try to
78 * execute code in this range.
79 *
80 * Return: 0 on success or negative error code on failure.
81 */
82int execmem_make_temp_rw(void *ptr, size_t size);
83
84/**
85 * execmem_restore_rox - restore read-only-execute permissions
86 * @ptr: address of the region to remap
87 * @size: size of the region to remap
88 *
89 * Restores read-only-execute permissions on a range [@ptr, @ptr + @size)
90 * after it was temporarily remapped as writable. Relies on architecture
91 * implementation of set_memory_rox() to restore mapping using large pages.
92 *
93 * Return: 0 on success or negative error code on failure.
94 */
95int execmem_restore_rox(void *ptr, size_t size);
96#else
97static inline int execmem_make_temp_rw(void *ptr, size_t size) { return 0; }
98static inline int execmem_restore_rox(void *ptr, size_t size) { return 0; }
99#endif
100
101/**
102 * struct execmem_range - definition of an address space suitable for code and
103 * related data allocations
104 * @start: address space start
105 * @end: address space end (inclusive)
106 * @fallback_start: start of the secondary address space range for fallback
107 * allocations on architectures that require it
108 * @fallback_end: start of the secondary address space (inclusive)
109 * @pgprot: permissions for memory in this address space
110 * @alignment: alignment required for text allocations
111 * @flags: options for memory allocations for this range
112 */
113struct execmem_range {
114 unsigned long start;
115 unsigned long end;
116 unsigned long fallback_start;
117 unsigned long fallback_end;
118 pgprot_t pgprot;
119 unsigned int alignment;
120 enum execmem_range_flags flags;
121};
122
123/**
124 * struct execmem_info - architecture parameters for code allocations
125 * @ranges: array of parameter sets defining architecture specific
126 * parameters for executable memory allocations. The ranges that are not
127 * explicitly initialized by an architecture use parameters defined for
128 * @EXECMEM_DEFAULT.
129 */
130struct execmem_info {
131 struct execmem_range ranges[EXECMEM_TYPE_MAX];
132};
133
134/**
135 * execmem_arch_setup - define parameters for allocations of executable memory
136 *
137 * A hook for architectures to define parameters for allocations of
138 * executable memory. These parameters should be filled into the
139 * @execmem_info structure.
140 *
141 * For architectures that do not implement this method a default set of
142 * parameters will be used
143 *
144 * Return: a structure defining architecture parameters and restrictions
145 * for allocations of executable memory
146 */
147struct execmem_info *execmem_arch_setup(void);
148
149/**
150 * execmem_alloc - allocate executable memory
151 * @type: type of the allocation
152 * @size: how many bytes of memory are required
153 *
154 * Allocates memory that will contain executable code, either generated or
155 * loaded from kernel modules.
156 *
157 * Allocates memory that will contain data coupled with executable code,
158 * like data sections in kernel modules.
159 *
160 * The memory will have protections defined by architecture for executable
161 * region of the @type.
162 *
163 * Return: a pointer to the allocated memory or %NULL
164 */
165void *execmem_alloc(enum execmem_type type, size_t size);
166
167/**
168 * execmem_free - free executable memory
169 * @ptr: pointer to the memory that should be freed
170 */
171void execmem_free(void *ptr);
172
173#ifdef CONFIG_MMU
174/**
175 * execmem_vmap - create virtual mapping for EXECMEM_MODULE_DATA memory
176 * @size: size of the virtual mapping in bytes
177 *
178 * Maps virtually contiguous area in the range suitable for EXECMEM_MODULE_DATA.
179 *
180 * Return: the area descriptor on success or %NULL on failure.
181 */
182struct vm_struct *execmem_vmap(size_t size);
183#endif
184
185/**
186 * execmem_update_copy - copy an update to executable memory
187 * @dst: destination address to update
188 * @src: source address containing the data
189 * @size: how many bytes of memory shold be copied
190 *
191 * Copy @size bytes from @src to @dst using text poking if the memory at
192 * @dst is read-only.
193 *
194 * Return: a pointer to @dst or NULL on error
195 */
196void *execmem_update_copy(void *dst, const void *src, size_t size);
197
198/**
199 * execmem_is_rox - check if execmem is read-only
200 * @type - the execmem type to check
201 *
202 * Return: %true if the @type is read-only, %false if it's writable
203 */
204bool execmem_is_rox(enum execmem_type type);
205
206#if defined(CONFIG_EXECMEM) && !defined(CONFIG_ARCH_WANTS_EXECMEM_LATE)
207void execmem_init(void);
208#else
209static inline void execmem_init(void) {}
210#endif
211
212#endif /* _LINUX_EXECMEM_ALLOC_H */