• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <linux/cleanup.h>
8 
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)
13 #else
14 #define MODULE_ALIGN PAGE_SIZE
15 #endif
16 
17 /**
18  * enum execmem_type - types of executable memory ranges
19  *
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.
27  *
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
35  * @EXECMEM_TYPE_MAX:
36  */
37 enum execmem_type {
38 	EXECMEM_DEFAULT,
39 	EXECMEM_MODULE_TEXT = EXECMEM_DEFAULT,
40 	EXECMEM_KPROBES,
41 	EXECMEM_FTRACE,
42 	EXECMEM_BPF,
43 	EXECMEM_MODULE_DATA,
44 	EXECMEM_TYPE_MAX,
45 };
46 
47 /**
48  * enum execmem_range_flags - options for executable memory allocations
49  * @EXECMEM_KASAN_SHADOW:	allocate kasan shadow
50  */
51 enum execmem_range_flags {
52 	EXECMEM_KASAN_SHADOW	= (1 << 0),
53 };
54 
55 /**
56  * struct execmem_range - definition of an address space suitable for code and
57  *			  related data allocations
58  * @start:	address space start
59  * @end:	address space end (inclusive)
60  * @fallback_start: start of the secondary address space range for fallback
61  *                  allocations on architectures that require it
62  * @fallback_end:   start of the secondary address space (inclusive)
63  * @pgprot:	permissions for memory in this address space
64  * @alignment:	alignment required for text allocations
65  * @flags:	options for memory allocations for this range
66  */
67 struct execmem_range {
68 	unsigned long   start;
69 	unsigned long   end;
70 	unsigned long   fallback_start;
71 	unsigned long   fallback_end;
72 	pgprot_t        pgprot;
73 	unsigned int	alignment;
74 	enum execmem_range_flags flags;
75 };
76 
77 /**
78  * struct execmem_info - architecture parameters for code allocations
79  * @ranges: array of parameter sets defining architecture specific
80  * parameters for executable memory allocations. The ranges that are not
81  * explicitly initialized by an architecture use parameters defined for
82  * @EXECMEM_DEFAULT.
83  */
84 struct execmem_info {
85 	struct execmem_range	ranges[EXECMEM_TYPE_MAX];
86 };
87 
88 /**
89  * execmem_arch_setup - define parameters for allocations of executable memory
90  *
91  * A hook for architectures to define parameters for allocations of
92  * executable memory. These parameters should be filled into the
93  * @execmem_info structure.
94  *
95  * For architectures that do not implement this method a default set of
96  * parameters will be used
97  *
98  * Return: a structure defining architecture parameters and restrictions
99  * for allocations of executable memory
100  */
101 struct execmem_info *execmem_arch_setup(void);
102 
103 /**
104  * execmem_alloc - allocate executable memory
105  * @type: type of the allocation
106  * @size: how many bytes of memory are required
107  *
108  * Allocates memory that will contain executable code, either generated or
109  * loaded from kernel modules.
110  *
111  * Allocates memory that will contain data coupled with executable code,
112  * like data sections in kernel modules.
113  *
114  * The memory will have protections defined by architecture for executable
115  * region of the @type.
116  *
117  * Return: a pointer to the allocated memory or %NULL
118  */
119 void *execmem_alloc(enum execmem_type type, size_t size);
120 
121 /**
122  * execmem_free - free executable memory
123  * @ptr: pointer to the memory that should be freed
124  */
125 void execmem_free(void *ptr);
126 
127 DEFINE_FREE(execmem, void *, if (_T) execmem_free(_T));
128 
129 #ifdef CONFIG_MMU
130 /**
131  * execmem_vmap - create virtual mapping for EXECMEM_MODULE_DATA memory
132  * @size: size of the virtual mapping in bytes
133  *
134  * Maps virtually contiguous area in the range suitable for EXECMEM_MODULE_DATA.
135  *
136  * Return: the area descriptor on success or %NULL on failure.
137  */
138 struct vm_struct *execmem_vmap(size_t size);
139 #endif
140 
141 #if defined(CONFIG_EXECMEM) && !defined(CONFIG_ARCH_WANTS_EXECMEM_LATE)
142 void execmem_init(void);
143 #else
execmem_init(void)144 static inline void execmem_init(void) {}
145 #endif
146 
147 #endif /* _LINUX_EXECMEM_ALLOC_H */
148