xref: /linux-6.15/include/linux/execmem.h (revision 0c133b1e)
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  */
36 enum 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  */
51 enum execmem_range_flags {
52 	EXECMEM_KASAN_SHADOW	= (1 << 0),
53 	EXECMEM_ROX_CACHE	= (1 << 1),
54 };
55 
56 /**
57  * struct execmem_range - definition of an address space suitable for code and
58  *			  related data allocations
59  * @start:	address space start
60  * @end:	address space end (inclusive)
61  * @fallback_start: start of the secondary address space range for fallback
62  *                  allocations on architectures that require it
63  * @fallback_end:   start of the secondary address space (inclusive)
64  * @pgprot:	permissions for memory in this address space
65  * @alignment:	alignment required for text allocations
66  * @flags:	options for memory allocations for this range
67  */
68 struct execmem_range {
69 	unsigned long   start;
70 	unsigned long   end;
71 	unsigned long   fallback_start;
72 	unsigned long   fallback_end;
73 	pgprot_t        pgprot;
74 	unsigned int	alignment;
75 	enum execmem_range_flags flags;
76 };
77 
78 /**
79  * struct execmem_info - architecture parameters for code allocations
80  * @ranges: array of parameter sets defining architecture specific
81  * parameters for executable memory allocations. The ranges that are not
82  * explicitly initialized by an architecture use parameters defined for
83  * @EXECMEM_DEFAULT.
84  */
85 struct execmem_info {
86 	struct execmem_range	ranges[EXECMEM_TYPE_MAX];
87 };
88 
89 /**
90  * execmem_arch_setup - define parameters for allocations of executable memory
91  *
92  * A hook for architectures to define parameters for allocations of
93  * executable memory. These parameters should be filled into the
94  * @execmem_info structure.
95  *
96  * For architectures that do not implement this method a default set of
97  * parameters will be used
98  *
99  * Return: a structure defining architecture parameters and restrictions
100  * for allocations of executable memory
101  */
102 struct execmem_info *execmem_arch_setup(void);
103 
104 /**
105  * execmem_alloc - allocate executable memory
106  * @type: type of the allocation
107  * @size: how many bytes of memory are required
108  *
109  * Allocates memory that will contain executable code, either generated or
110  * loaded from kernel modules.
111  *
112  * Allocates memory that will contain data coupled with executable code,
113  * like data sections in kernel modules.
114  *
115  * The memory will have protections defined by architecture for executable
116  * region of the @type.
117  *
118  * Return: a pointer to the allocated memory or %NULL
119  */
120 void *execmem_alloc(enum execmem_type type, size_t size);
121 
122 /**
123  * execmem_free - free executable memory
124  * @ptr: pointer to the memory that should be freed
125  */
126 void execmem_free(void *ptr);
127 
128 /**
129  * execmem_update_copy - copy an update to executable memory
130  * @dst:  destination address to update
131  * @src:  source address containing the data
132  * @size: how many bytes of memory shold be copied
133  *
134  * Copy @size bytes from @src to @dst using text poking if the memory at
135  * @dst is read-only.
136  *
137  * Return: a pointer to @dst or NULL on error
138  */
139 void *execmem_update_copy(void *dst, const void *src, size_t size);
140 
141 /**
142  * execmem_is_rox - check if execmem is read-only
143  * @type - the execmem type to check
144  *
145  * Return: %true if the @type is read-only, %false if it's writable
146  */
147 bool execmem_is_rox(enum execmem_type type);
148 
149 #if defined(CONFIG_EXECMEM) && !defined(CONFIG_ARCH_WANTS_EXECMEM_LATE)
150 void execmem_init(void);
151 #else
152 static inline void execmem_init(void) {}
153 #endif
154 
155 #endif /* _LINUX_EXECMEM_ALLOC_H */
156