xref: /linux-6.15/include/linux/execmem.h (revision 0c133b1e)
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) 
8223b5e57SMike Rapoport (IBM) #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
9223b5e57SMike Rapoport (IBM) 		!defined(CONFIG_KASAN_VMALLOC)
10223b5e57SMike Rapoport (IBM) #include <linux/kasan.h>
11223b5e57SMike Rapoport (IBM) #define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT)
12223b5e57SMike Rapoport (IBM) #else
13223b5e57SMike Rapoport (IBM) #define MODULE_ALIGN PAGE_SIZE
14223b5e57SMike Rapoport (IBM) #endif
15223b5e57SMike 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
33223b5e57SMike 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,
42223b5e57SMike Rapoport (IBM) 	EXECMEM_MODULE_DATA,
4312af2b83SMike Rapoport (IBM) 	EXECMEM_TYPE_MAX,
4412af2b83SMike Rapoport (IBM) };
4512af2b83SMike Rapoport (IBM) 
4612af2b83SMike Rapoport (IBM) /**
47223b5e57SMike Rapoport (IBM)  * enum execmem_range_flags - options for executable memory allocations
48223b5e57SMike Rapoport (IBM)  * @EXECMEM_KASAN_SHADOW:	allocate kasan shadow
49*0c133b1eSMike Rapoport (Microsoft)  * @EXECMEM_ROX_CACHE:		allocations should use ROX cache of huge pages
50223b5e57SMike Rapoport (IBM)  */
51223b5e57SMike Rapoport (IBM) enum execmem_range_flags {
52223b5e57SMike Rapoport (IBM) 	EXECMEM_KASAN_SHADOW	= (1 << 0),
53*0c133b1eSMike Rapoport (Microsoft) 	EXECMEM_ROX_CACHE	= (1 << 1),
54223b5e57SMike Rapoport (IBM) };
55223b5e57SMike Rapoport (IBM) 
56223b5e57SMike Rapoport (IBM) /**
57f6bec26cSMike Rapoport (IBM)  * struct execmem_range - definition of an address space suitable for code and
58f6bec26cSMike Rapoport (IBM)  *			  related data allocations
59f6bec26cSMike Rapoport (IBM)  * @start:	address space start
60f6bec26cSMike Rapoport (IBM)  * @end:	address space end (inclusive)
61223b5e57SMike Rapoport (IBM)  * @fallback_start: start of the secondary address space range for fallback
62223b5e57SMike Rapoport (IBM)  *                  allocations on architectures that require it
63223b5e57SMike Rapoport (IBM)  * @fallback_end:   start of the secondary address space (inclusive)
64f6bec26cSMike Rapoport (IBM)  * @pgprot:	permissions for memory in this address space
65f6bec26cSMike Rapoport (IBM)  * @alignment:	alignment required for text allocations
66223b5e57SMike Rapoport (IBM)  * @flags:	options for memory allocations for this range
67f6bec26cSMike Rapoport (IBM)  */
68f6bec26cSMike Rapoport (IBM) struct execmem_range {
69f6bec26cSMike Rapoport (IBM) 	unsigned long   start;
70f6bec26cSMike Rapoport (IBM) 	unsigned long   end;
71223b5e57SMike Rapoport (IBM) 	unsigned long   fallback_start;
72223b5e57SMike Rapoport (IBM) 	unsigned long   fallback_end;
73f6bec26cSMike Rapoport (IBM) 	pgprot_t        pgprot;
74f6bec26cSMike Rapoport (IBM) 	unsigned int	alignment;
75223b5e57SMike Rapoport (IBM) 	enum execmem_range_flags flags;
76f6bec26cSMike Rapoport (IBM) };
77f6bec26cSMike Rapoport (IBM) 
78f6bec26cSMike Rapoport (IBM) /**
79f6bec26cSMike Rapoport (IBM)  * struct execmem_info - architecture parameters for code allocations
80f6bec26cSMike Rapoport (IBM)  * @ranges: array of parameter sets defining architecture specific
81f6bec26cSMike Rapoport (IBM)  * parameters for executable memory allocations. The ranges that are not
82f6bec26cSMike Rapoport (IBM)  * explicitly initialized by an architecture use parameters defined for
83f6bec26cSMike Rapoport (IBM)  * @EXECMEM_DEFAULT.
84f6bec26cSMike Rapoport (IBM)  */
85f6bec26cSMike Rapoport (IBM) struct execmem_info {
86f6bec26cSMike Rapoport (IBM) 	struct execmem_range	ranges[EXECMEM_TYPE_MAX];
87f6bec26cSMike Rapoport (IBM) };
88f6bec26cSMike Rapoport (IBM) 
89f6bec26cSMike Rapoport (IBM) /**
90f6bec26cSMike Rapoport (IBM)  * execmem_arch_setup - define parameters for allocations of executable memory
91f6bec26cSMike Rapoport (IBM)  *
92f6bec26cSMike Rapoport (IBM)  * A hook for architectures to define parameters for allocations of
93f6bec26cSMike Rapoport (IBM)  * executable memory. These parameters should be filled into the
94f6bec26cSMike Rapoport (IBM)  * @execmem_info structure.
95f6bec26cSMike Rapoport (IBM)  *
96f6bec26cSMike Rapoport (IBM)  * For architectures that do not implement this method a default set of
97f6bec26cSMike Rapoport (IBM)  * parameters will be used
98f6bec26cSMike Rapoport (IBM)  *
99f6bec26cSMike Rapoport (IBM)  * Return: a structure defining architecture parameters and restrictions
100f6bec26cSMike Rapoport (IBM)  * for allocations of executable memory
101f6bec26cSMike Rapoport (IBM)  */
102f6bec26cSMike Rapoport (IBM) struct execmem_info *execmem_arch_setup(void);
103f6bec26cSMike Rapoport (IBM) 
104f6bec26cSMike Rapoport (IBM) /**
10512af2b83SMike Rapoport (IBM)  * execmem_alloc - allocate executable memory
10612af2b83SMike Rapoport (IBM)  * @type: type of the allocation
10712af2b83SMike Rapoport (IBM)  * @size: how many bytes of memory are required
10812af2b83SMike Rapoport (IBM)  *
10912af2b83SMike Rapoport (IBM)  * Allocates memory that will contain executable code, either generated or
11012af2b83SMike Rapoport (IBM)  * loaded from kernel modules.
11112af2b83SMike Rapoport (IBM)  *
112223b5e57SMike Rapoport (IBM)  * Allocates memory that will contain data coupled with executable code,
113223b5e57SMike Rapoport (IBM)  * like data sections in kernel modules.
114223b5e57SMike Rapoport (IBM)  *
11512af2b83SMike Rapoport (IBM)  * The memory will have protections defined by architecture for executable
11612af2b83SMike Rapoport (IBM)  * region of the @type.
11712af2b83SMike Rapoport (IBM)  *
11812af2b83SMike Rapoport (IBM)  * Return: a pointer to the allocated memory or %NULL
11912af2b83SMike Rapoport (IBM)  */
12012af2b83SMike Rapoport (IBM) void *execmem_alloc(enum execmem_type type, size_t size);
12112af2b83SMike Rapoport (IBM) 
12212af2b83SMike Rapoport (IBM) /**
12312af2b83SMike Rapoport (IBM)  * execmem_free - free executable memory
12412af2b83SMike Rapoport (IBM)  * @ptr: pointer to the memory that should be freed
12512af2b83SMike Rapoport (IBM)  */
12612af2b83SMike Rapoport (IBM) void execmem_free(void *ptr);
12712af2b83SMike Rapoport (IBM) 
128*0c133b1eSMike Rapoport (Microsoft) /**
129*0c133b1eSMike Rapoport (Microsoft)  * execmem_update_copy - copy an update to executable memory
130*0c133b1eSMike Rapoport (Microsoft)  * @dst:  destination address to update
131*0c133b1eSMike Rapoport (Microsoft)  * @src:  source address containing the data
132*0c133b1eSMike Rapoport (Microsoft)  * @size: how many bytes of memory shold be copied
133*0c133b1eSMike Rapoport (Microsoft)  *
134*0c133b1eSMike Rapoport (Microsoft)  * Copy @size bytes from @src to @dst using text poking if the memory at
135*0c133b1eSMike Rapoport (Microsoft)  * @dst is read-only.
136*0c133b1eSMike Rapoport (Microsoft)  *
137*0c133b1eSMike Rapoport (Microsoft)  * Return: a pointer to @dst or NULL on error
138*0c133b1eSMike Rapoport (Microsoft)  */
139*0c133b1eSMike Rapoport (Microsoft) void *execmem_update_copy(void *dst, const void *src, size_t size);
140*0c133b1eSMike Rapoport (Microsoft) 
141*0c133b1eSMike Rapoport (Microsoft) /**
142*0c133b1eSMike Rapoport (Microsoft)  * execmem_is_rox - check if execmem is read-only
143*0c133b1eSMike Rapoport (Microsoft)  * @type - the execmem type to check
144*0c133b1eSMike Rapoport (Microsoft)  *
145*0c133b1eSMike Rapoport (Microsoft)  * Return: %true if the @type is read-only, %false if it's writable
146*0c133b1eSMike Rapoport (Microsoft)  */
147*0c133b1eSMike Rapoport (Microsoft) bool execmem_is_rox(enum execmem_type type);
148*0c133b1eSMike Rapoport (Microsoft) 
149223b5e57SMike Rapoport (IBM) #if defined(CONFIG_EXECMEM) && !defined(CONFIG_ARCH_WANTS_EXECMEM_LATE)
150f6bec26cSMike Rapoport (IBM) void execmem_init(void);
151f6bec26cSMike Rapoport (IBM) #else
152f6bec26cSMike Rapoport (IBM) static inline void execmem_init(void) {}
153f6bec26cSMike Rapoport (IBM) #endif
154f6bec26cSMike Rapoport (IBM) 
15512af2b83SMike Rapoport (IBM) #endif /* _LINUX_EXECMEM_ALLOC_H */
156