Merge branch 'address-masking'
[linux-2.6-block.git] / include / linux / execmem.h
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  */
50 enum execmem_range_flags {
51         EXECMEM_KASAN_SHADOW    = (1 << 0),
52 };
53
54 /**
55  * struct execmem_range - definition of an address space suitable for code and
56  *                        related data allocations
57  * @start:      address space start
58  * @end:        address space end (inclusive)
59  * @fallback_start: start of the secondary address space range for fallback
60  *                  allocations on architectures that require it
61  * @fallback_end:   start of the secondary address space (inclusive)
62  * @pgprot:     permissions for memory in this address space
63  * @alignment:  alignment required for text allocations
64  * @flags:      options for memory allocations for this range
65  */
66 struct execmem_range {
67         unsigned long   start;
68         unsigned long   end;
69         unsigned long   fallback_start;
70         unsigned long   fallback_end;
71         pgprot_t        pgprot;
72         unsigned int    alignment;
73         enum execmem_range_flags flags;
74 };
75
76 /**
77  * struct execmem_info - architecture parameters for code allocations
78  * @ranges: array of parameter sets defining architecture specific
79  * parameters for executable memory allocations. The ranges that are not
80  * explicitly initialized by an architecture use parameters defined for
81  * @EXECMEM_DEFAULT.
82  */
83 struct execmem_info {
84         struct execmem_range    ranges[EXECMEM_TYPE_MAX];
85 };
86
87 /**
88  * execmem_arch_setup - define parameters for allocations of executable memory
89  *
90  * A hook for architectures to define parameters for allocations of
91  * executable memory. These parameters should be filled into the
92  * @execmem_info structure.
93  *
94  * For architectures that do not implement this method a default set of
95  * parameters will be used
96  *
97  * Return: a structure defining architecture parameters and restrictions
98  * for allocations of executable memory
99  */
100 struct execmem_info *execmem_arch_setup(void);
101
102 /**
103  * execmem_alloc - allocate executable memory
104  * @type: type of the allocation
105  * @size: how many bytes of memory are required
106  *
107  * Allocates memory that will contain executable code, either generated or
108  * loaded from kernel modules.
109  *
110  * Allocates memory that will contain data coupled with executable code,
111  * like data sections in kernel modules.
112  *
113  * The memory will have protections defined by architecture for executable
114  * region of the @type.
115  *
116  * Return: a pointer to the allocated memory or %NULL
117  */
118 void *execmem_alloc(enum execmem_type type, size_t size);
119
120 /**
121  * execmem_free - free executable memory
122  * @ptr: pointer to the memory that should be freed
123  */
124 void execmem_free(void *ptr);
125
126 #if defined(CONFIG_EXECMEM) && !defined(CONFIG_ARCH_WANTS_EXECMEM_LATE)
127 void execmem_init(void);
128 #else
129 static inline void execmem_init(void) {}
130 #endif
131
132 #endif /* _LINUX_EXECMEM_ALLOC_H */