xref: /linux-6.15/include/linux/execmem.h (revision f6bec26c)
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) 
812af2b83SMike Rapoport (IBM) /**
912af2b83SMike Rapoport (IBM)  * enum execmem_type - types of executable memory ranges
1012af2b83SMike Rapoport (IBM)  *
1112af2b83SMike Rapoport (IBM)  * There are several subsystems that allocate executable memory.
1212af2b83SMike Rapoport (IBM)  * Architectures define different restrictions on placement,
1312af2b83SMike Rapoport (IBM)  * permissions, alignment and other parameters for memory that can be used
1412af2b83SMike Rapoport (IBM)  * by these subsystems.
1512af2b83SMike Rapoport (IBM)  * Types in this enum identify subsystems that allocate executable memory
1612af2b83SMike Rapoport (IBM)  * and let architectures define parameters for ranges suitable for
1712af2b83SMike Rapoport (IBM)  * allocations by each subsystem.
1812af2b83SMike Rapoport (IBM)  *
1912af2b83SMike Rapoport (IBM)  * @EXECMEM_DEFAULT: default parameters that would be used for types that
2012af2b83SMike Rapoport (IBM)  * are not explicitly defined.
2112af2b83SMike Rapoport (IBM)  * @EXECMEM_MODULE_TEXT: parameters for module text sections
2212af2b83SMike Rapoport (IBM)  * @EXECMEM_KPROBES: parameters for kprobes
2312af2b83SMike Rapoport (IBM)  * @EXECMEM_FTRACE: parameters for ftrace
2412af2b83SMike Rapoport (IBM)  * @EXECMEM_BPF: parameters for BPF
2512af2b83SMike Rapoport (IBM)  * @EXECMEM_TYPE_MAX:
2612af2b83SMike Rapoport (IBM)  */
2712af2b83SMike Rapoport (IBM) enum execmem_type {
2812af2b83SMike Rapoport (IBM) 	EXECMEM_DEFAULT,
2912af2b83SMike Rapoport (IBM) 	EXECMEM_MODULE_TEXT = EXECMEM_DEFAULT,
3012af2b83SMike Rapoport (IBM) 	EXECMEM_KPROBES,
3112af2b83SMike Rapoport (IBM) 	EXECMEM_FTRACE,
3212af2b83SMike Rapoport (IBM) 	EXECMEM_BPF,
3312af2b83SMike Rapoport (IBM) 	EXECMEM_TYPE_MAX,
3412af2b83SMike Rapoport (IBM) };
3512af2b83SMike Rapoport (IBM) 
3612af2b83SMike Rapoport (IBM) /**
37*f6bec26cSMike Rapoport (IBM)  * struct execmem_range - definition of an address space suitable for code and
38*f6bec26cSMike Rapoport (IBM)  *			  related data allocations
39*f6bec26cSMike Rapoport (IBM)  * @start:	address space start
40*f6bec26cSMike Rapoport (IBM)  * @end:	address space end (inclusive)
41*f6bec26cSMike Rapoport (IBM)  * @pgprot:	permissions for memory in this address space
42*f6bec26cSMike Rapoport (IBM)  * @alignment:	alignment required for text allocations
43*f6bec26cSMike Rapoport (IBM)  */
44*f6bec26cSMike Rapoport (IBM) struct execmem_range {
45*f6bec26cSMike Rapoport (IBM) 	unsigned long   start;
46*f6bec26cSMike Rapoport (IBM) 	unsigned long   end;
47*f6bec26cSMike Rapoport (IBM) 	pgprot_t        pgprot;
48*f6bec26cSMike Rapoport (IBM) 	unsigned int	alignment;
49*f6bec26cSMike Rapoport (IBM) };
50*f6bec26cSMike Rapoport (IBM) 
51*f6bec26cSMike Rapoport (IBM) /**
52*f6bec26cSMike Rapoport (IBM)  * struct execmem_info - architecture parameters for code allocations
53*f6bec26cSMike Rapoport (IBM)  * @ranges: array of parameter sets defining architecture specific
54*f6bec26cSMike Rapoport (IBM)  * parameters for executable memory allocations. The ranges that are not
55*f6bec26cSMike Rapoport (IBM)  * explicitly initialized by an architecture use parameters defined for
56*f6bec26cSMike Rapoport (IBM)  * @EXECMEM_DEFAULT.
57*f6bec26cSMike Rapoport (IBM)  */
58*f6bec26cSMike Rapoport (IBM) struct execmem_info {
59*f6bec26cSMike Rapoport (IBM) 	struct execmem_range	ranges[EXECMEM_TYPE_MAX];
60*f6bec26cSMike Rapoport (IBM) };
61*f6bec26cSMike Rapoport (IBM) 
62*f6bec26cSMike Rapoport (IBM) /**
63*f6bec26cSMike Rapoport (IBM)  * execmem_arch_setup - define parameters for allocations of executable memory
64*f6bec26cSMike Rapoport (IBM)  *
65*f6bec26cSMike Rapoport (IBM)  * A hook for architectures to define parameters for allocations of
66*f6bec26cSMike Rapoport (IBM)  * executable memory. These parameters should be filled into the
67*f6bec26cSMike Rapoport (IBM)  * @execmem_info structure.
68*f6bec26cSMike Rapoport (IBM)  *
69*f6bec26cSMike Rapoport (IBM)  * For architectures that do not implement this method a default set of
70*f6bec26cSMike Rapoport (IBM)  * parameters will be used
71*f6bec26cSMike Rapoport (IBM)  *
72*f6bec26cSMike Rapoport (IBM)  * Return: a structure defining architecture parameters and restrictions
73*f6bec26cSMike Rapoport (IBM)  * for allocations of executable memory
74*f6bec26cSMike Rapoport (IBM)  */
75*f6bec26cSMike Rapoport (IBM) struct execmem_info *execmem_arch_setup(void);
76*f6bec26cSMike Rapoport (IBM) 
77*f6bec26cSMike Rapoport (IBM) /**
7812af2b83SMike Rapoport (IBM)  * execmem_alloc - allocate executable memory
7912af2b83SMike Rapoport (IBM)  * @type: type of the allocation
8012af2b83SMike Rapoport (IBM)  * @size: how many bytes of memory are required
8112af2b83SMike Rapoport (IBM)  *
8212af2b83SMike Rapoport (IBM)  * Allocates memory that will contain executable code, either generated or
8312af2b83SMike Rapoport (IBM)  * loaded from kernel modules.
8412af2b83SMike Rapoport (IBM)  *
8512af2b83SMike Rapoport (IBM)  * The memory will have protections defined by architecture for executable
8612af2b83SMike Rapoport (IBM)  * region of the @type.
8712af2b83SMike Rapoport (IBM)  *
8812af2b83SMike Rapoport (IBM)  * Return: a pointer to the allocated memory or %NULL
8912af2b83SMike Rapoport (IBM)  */
9012af2b83SMike Rapoport (IBM) void *execmem_alloc(enum execmem_type type, size_t size);
9112af2b83SMike Rapoport (IBM) 
9212af2b83SMike Rapoport (IBM) /**
9312af2b83SMike Rapoport (IBM)  * execmem_free - free executable memory
9412af2b83SMike Rapoport (IBM)  * @ptr: pointer to the memory that should be freed
9512af2b83SMike Rapoport (IBM)  */
9612af2b83SMike Rapoport (IBM) void execmem_free(void *ptr);
9712af2b83SMike Rapoport (IBM) 
98*f6bec26cSMike Rapoport (IBM) #ifdef CONFIG_EXECMEM
99*f6bec26cSMike Rapoport (IBM) void execmem_init(void);
100*f6bec26cSMike Rapoport (IBM) #else
101*f6bec26cSMike Rapoport (IBM) static inline void execmem_init(void) {}
102*f6bec26cSMike Rapoport (IBM) #endif
103*f6bec26cSMike Rapoport (IBM) 
10412af2b83SMike Rapoport (IBM) #endif /* _LINUX_EXECMEM_ALLOC_H */
105