xref: /linux-6.15/include/linux/execmem.h (revision f6bec26c)
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 /**
9  * enum execmem_type - types of executable memory ranges
10  *
11  * There are several subsystems that allocate executable memory.
12  * Architectures define different restrictions on placement,
13  * permissions, alignment and other parameters for memory that can be used
14  * by these subsystems.
15  * Types in this enum identify subsystems that allocate executable memory
16  * and let architectures define parameters for ranges suitable for
17  * allocations by each subsystem.
18  *
19  * @EXECMEM_DEFAULT: default parameters that would be used for types that
20  * are not explicitly defined.
21  * @EXECMEM_MODULE_TEXT: parameters for module text sections
22  * @EXECMEM_KPROBES: parameters for kprobes
23  * @EXECMEM_FTRACE: parameters for ftrace
24  * @EXECMEM_BPF: parameters for BPF
25  * @EXECMEM_TYPE_MAX:
26  */
27 enum execmem_type {
28 	EXECMEM_DEFAULT,
29 	EXECMEM_MODULE_TEXT = EXECMEM_DEFAULT,
30 	EXECMEM_KPROBES,
31 	EXECMEM_FTRACE,
32 	EXECMEM_BPF,
33 	EXECMEM_TYPE_MAX,
34 };
35 
36 /**
37  * struct execmem_range - definition of an address space suitable for code and
38  *			  related data allocations
39  * @start:	address space start
40  * @end:	address space end (inclusive)
41  * @pgprot:	permissions for memory in this address space
42  * @alignment:	alignment required for text allocations
43  */
44 struct execmem_range {
45 	unsigned long   start;
46 	unsigned long   end;
47 	pgprot_t        pgprot;
48 	unsigned int	alignment;
49 };
50 
51 /**
52  * struct execmem_info - architecture parameters for code allocations
53  * @ranges: array of parameter sets defining architecture specific
54  * parameters for executable memory allocations. The ranges that are not
55  * explicitly initialized by an architecture use parameters defined for
56  * @EXECMEM_DEFAULT.
57  */
58 struct execmem_info {
59 	struct execmem_range	ranges[EXECMEM_TYPE_MAX];
60 };
61 
62 /**
63  * execmem_arch_setup - define parameters for allocations of executable memory
64  *
65  * A hook for architectures to define parameters for allocations of
66  * executable memory. These parameters should be filled into the
67  * @execmem_info structure.
68  *
69  * For architectures that do not implement this method a default set of
70  * parameters will be used
71  *
72  * Return: a structure defining architecture parameters and restrictions
73  * for allocations of executable memory
74  */
75 struct execmem_info *execmem_arch_setup(void);
76 
77 /**
78  * execmem_alloc - allocate executable memory
79  * @type: type of the allocation
80  * @size: how many bytes of memory are required
81  *
82  * Allocates memory that will contain executable code, either generated or
83  * loaded from kernel modules.
84  *
85  * The memory will have protections defined by architecture for executable
86  * region of the @type.
87  *
88  * Return: a pointer to the allocated memory or %NULL
89  */
90 void *execmem_alloc(enum execmem_type type, size_t size);
91 
92 /**
93  * execmem_free - free executable memory
94  * @ptr: pointer to the memory that should be freed
95  */
96 void execmem_free(void *ptr);
97 
98 #ifdef CONFIG_EXECMEM
99 void execmem_init(void);
100 #else
101 static inline void execmem_init(void) {}
102 #endif
103 
104 #endif /* _LINUX_EXECMEM_ALLOC_H */
105