xref: /linux-6.15/include/linux/execmem.h (revision 223b5e57)
112af2b83SMike Rapoport (IBM) /* SPDX-License-Identifier: GPL-2.0 */
212af2b83SMike Rapoport (IBM) #ifndef _LINUX_EXECMEM_ALLOC_H
312af2b83SMike Rapoport (IBM) #define _LINUX_EXECMEM_ALLOC_H
412af2b83SMike Rapoport (IBM) 
512af2b83SMike Rapoport (IBM) #include <linux/types.h>
612af2b83SMike Rapoport (IBM) #include <linux/moduleloader.h>
712af2b83SMike Rapoport (IBM) 
8*223b5e57SMike Rapoport (IBM) #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
9*223b5e57SMike Rapoport (IBM) 		!defined(CONFIG_KASAN_VMALLOC)
10*223b5e57SMike Rapoport (IBM) #include <linux/kasan.h>
11*223b5e57SMike Rapoport (IBM) #define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT)
12*223b5e57SMike Rapoport (IBM) #else
13*223b5e57SMike Rapoport (IBM) #define MODULE_ALIGN PAGE_SIZE
14*223b5e57SMike Rapoport (IBM) #endif
15*223b5e57SMike Rapoport (IBM) 
1612af2b83SMike Rapoport (IBM) /**
1712af2b83SMike Rapoport (IBM)  * enum execmem_type - types of executable memory ranges
1812af2b83SMike Rapoport (IBM)  *
1912af2b83SMike Rapoport (IBM)  * There are several subsystems that allocate executable memory.
2012af2b83SMike Rapoport (IBM)  * Architectures define different restrictions on placement,
2112af2b83SMike Rapoport (IBM)  * permissions, alignment and other parameters for memory that can be used
2212af2b83SMike Rapoport (IBM)  * by these subsystems.
2312af2b83SMike Rapoport (IBM)  * Types in this enum identify subsystems that allocate executable memory
2412af2b83SMike Rapoport (IBM)  * and let architectures define parameters for ranges suitable for
2512af2b83SMike Rapoport (IBM)  * allocations by each subsystem.
2612af2b83SMike Rapoport (IBM)  *
2712af2b83SMike Rapoport (IBM)  * @EXECMEM_DEFAULT: default parameters that would be used for types that
2812af2b83SMike Rapoport (IBM)  * are not explicitly defined.
2912af2b83SMike Rapoport (IBM)  * @EXECMEM_MODULE_TEXT: parameters for module text sections
3012af2b83SMike Rapoport (IBM)  * @EXECMEM_KPROBES: parameters for kprobes
3112af2b83SMike Rapoport (IBM)  * @EXECMEM_FTRACE: parameters for ftrace
3212af2b83SMike Rapoport (IBM)  * @EXECMEM_BPF: parameters for BPF
33*223b5e57SMike Rapoport (IBM)  * @EXECMEM_MODULE_DATA: parameters for module data sections
3412af2b83SMike Rapoport (IBM)  * @EXECMEM_TYPE_MAX:
3512af2b83SMike Rapoport (IBM)  */
3612af2b83SMike Rapoport (IBM) enum execmem_type {
3712af2b83SMike Rapoport (IBM) 	EXECMEM_DEFAULT,
3812af2b83SMike Rapoport (IBM) 	EXECMEM_MODULE_TEXT = EXECMEM_DEFAULT,
3912af2b83SMike Rapoport (IBM) 	EXECMEM_KPROBES,
4012af2b83SMike Rapoport (IBM) 	EXECMEM_FTRACE,
4112af2b83SMike Rapoport (IBM) 	EXECMEM_BPF,
42*223b5e57SMike Rapoport (IBM) 	EXECMEM_MODULE_DATA,
4312af2b83SMike Rapoport (IBM) 	EXECMEM_TYPE_MAX,
4412af2b83SMike Rapoport (IBM) };
4512af2b83SMike Rapoport (IBM) 
4612af2b83SMike Rapoport (IBM) /**
47*223b5e57SMike Rapoport (IBM)  * enum execmem_range_flags - options for executable memory allocations
48*223b5e57SMike Rapoport (IBM)  * @EXECMEM_KASAN_SHADOW:	allocate kasan shadow
49*223b5e57SMike Rapoport (IBM)  */
50*223b5e57SMike Rapoport (IBM) enum execmem_range_flags {
51*223b5e57SMike Rapoport (IBM) 	EXECMEM_KASAN_SHADOW	= (1 << 0),
52*223b5e57SMike Rapoport (IBM) };
53*223b5e57SMike Rapoport (IBM) 
54*223b5e57SMike Rapoport (IBM) /**
55f6bec26cSMike Rapoport (IBM)  * struct execmem_range - definition of an address space suitable for code and
56f6bec26cSMike Rapoport (IBM)  *			  related data allocations
57f6bec26cSMike Rapoport (IBM)  * @start:	address space start
58f6bec26cSMike Rapoport (IBM)  * @end:	address space end (inclusive)
59*223b5e57SMike Rapoport (IBM)  * @fallback_start: start of the secondary address space range for fallback
60*223b5e57SMike Rapoport (IBM)  *                  allocations on architectures that require it
61*223b5e57SMike Rapoport (IBM)  * @fallback_end:   start of the secondary address space (inclusive)
62f6bec26cSMike Rapoport (IBM)  * @pgprot:	permissions for memory in this address space
63f6bec26cSMike Rapoport (IBM)  * @alignment:	alignment required for text allocations
64*223b5e57SMike Rapoport (IBM)  * @flags:	options for memory allocations for this range
65f6bec26cSMike Rapoport (IBM)  */
66f6bec26cSMike Rapoport (IBM) struct execmem_range {
67f6bec26cSMike Rapoport (IBM) 	unsigned long   start;
68f6bec26cSMike Rapoport (IBM) 	unsigned long   end;
69*223b5e57SMike Rapoport (IBM) 	unsigned long   fallback_start;
70*223b5e57SMike Rapoport (IBM) 	unsigned long   fallback_end;
71f6bec26cSMike Rapoport (IBM) 	pgprot_t        pgprot;
72f6bec26cSMike Rapoport (IBM) 	unsigned int	alignment;
73*223b5e57SMike Rapoport (IBM) 	enum execmem_range_flags flags;
74f6bec26cSMike Rapoport (IBM) };
75f6bec26cSMike Rapoport (IBM) 
76f6bec26cSMike Rapoport (IBM) /**
77f6bec26cSMike Rapoport (IBM)  * struct execmem_info - architecture parameters for code allocations
78f6bec26cSMike Rapoport (IBM)  * @ranges: array of parameter sets defining architecture specific
79f6bec26cSMike Rapoport (IBM)  * parameters for executable memory allocations. The ranges that are not
80f6bec26cSMike Rapoport (IBM)  * explicitly initialized by an architecture use parameters defined for
81f6bec26cSMike Rapoport (IBM)  * @EXECMEM_DEFAULT.
82f6bec26cSMike Rapoport (IBM)  */
83f6bec26cSMike Rapoport (IBM) struct execmem_info {
84f6bec26cSMike Rapoport (IBM) 	struct execmem_range	ranges[EXECMEM_TYPE_MAX];
85f6bec26cSMike Rapoport (IBM) };
86f6bec26cSMike Rapoport (IBM) 
87f6bec26cSMike Rapoport (IBM) /**
88f6bec26cSMike Rapoport (IBM)  * execmem_arch_setup - define parameters for allocations of executable memory
89f6bec26cSMike Rapoport (IBM)  *
90f6bec26cSMike Rapoport (IBM)  * A hook for architectures to define parameters for allocations of
91f6bec26cSMike Rapoport (IBM)  * executable memory. These parameters should be filled into the
92f6bec26cSMike Rapoport (IBM)  * @execmem_info structure.
93f6bec26cSMike Rapoport (IBM)  *
94f6bec26cSMike Rapoport (IBM)  * For architectures that do not implement this method a default set of
95f6bec26cSMike Rapoport (IBM)  * parameters will be used
96f6bec26cSMike Rapoport (IBM)  *
97f6bec26cSMike Rapoport (IBM)  * Return: a structure defining architecture parameters and restrictions
98f6bec26cSMike Rapoport (IBM)  * for allocations of executable memory
99f6bec26cSMike Rapoport (IBM)  */
100f6bec26cSMike Rapoport (IBM) struct execmem_info *execmem_arch_setup(void);
101f6bec26cSMike Rapoport (IBM) 
102f6bec26cSMike Rapoport (IBM) /**
10312af2b83SMike Rapoport (IBM)  * execmem_alloc - allocate executable memory
10412af2b83SMike Rapoport (IBM)  * @type: type of the allocation
10512af2b83SMike Rapoport (IBM)  * @size: how many bytes of memory are required
10612af2b83SMike Rapoport (IBM)  *
10712af2b83SMike Rapoport (IBM)  * Allocates memory that will contain executable code, either generated or
10812af2b83SMike Rapoport (IBM)  * loaded from kernel modules.
10912af2b83SMike Rapoport (IBM)  *
110*223b5e57SMike Rapoport (IBM)  * Allocates memory that will contain data coupled with executable code,
111*223b5e57SMike Rapoport (IBM)  * like data sections in kernel modules.
112*223b5e57SMike Rapoport (IBM)  *
11312af2b83SMike Rapoport (IBM)  * The memory will have protections defined by architecture for executable
11412af2b83SMike Rapoport (IBM)  * region of the @type.
11512af2b83SMike Rapoport (IBM)  *
11612af2b83SMike Rapoport (IBM)  * Return: a pointer to the allocated memory or %NULL
11712af2b83SMike Rapoport (IBM)  */
11812af2b83SMike Rapoport (IBM) void *execmem_alloc(enum execmem_type type, size_t size);
11912af2b83SMike Rapoport (IBM) 
12012af2b83SMike Rapoport (IBM) /**
12112af2b83SMike Rapoport (IBM)  * execmem_free - free executable memory
12212af2b83SMike Rapoport (IBM)  * @ptr: pointer to the memory that should be freed
12312af2b83SMike Rapoport (IBM)  */
12412af2b83SMike Rapoport (IBM) void execmem_free(void *ptr);
12512af2b83SMike Rapoport (IBM) 
126*223b5e57SMike Rapoport (IBM) #if defined(CONFIG_EXECMEM) && !defined(CONFIG_ARCH_WANTS_EXECMEM_LATE)
127f6bec26cSMike Rapoport (IBM) void execmem_init(void);
128f6bec26cSMike Rapoport (IBM) #else
129f6bec26cSMike Rapoport (IBM) static inline void execmem_init(void) {}
130f6bec26cSMike Rapoport (IBM) #endif
131f6bec26cSMike Rapoport (IBM) 
13212af2b83SMike Rapoport (IBM) #endif /* _LINUX_EXECMEM_ALLOC_H */
133