xref: /linux-6.15/include/linux/execmem.h (revision 05e555b8)
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
490c133b1eSMike 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),
530c133b1eSMike Rapoport (Microsoft) 	EXECMEM_ROX_CACHE	= (1 << 1),
54223b5e57SMike Rapoport (IBM) };
55223b5e57SMike Rapoport (IBM) 
562e45474aSMike Rapoport (Microsoft) #ifdef CONFIG_ARCH_HAS_EXECMEM_ROX
572e45474aSMike Rapoport (Microsoft) /**
582e45474aSMike Rapoport (Microsoft)  * execmem_fill_trapping_insns - set memory to contain instructions that
592e45474aSMike Rapoport (Microsoft)  *				 will trap
602e45474aSMike Rapoport (Microsoft)  * @ptr:	pointer to memory to fill
612e45474aSMike Rapoport (Microsoft)  * @size:	size of the range to fill
622e45474aSMike Rapoport (Microsoft)  * @writable:	is the memory poited by @ptr is writable or ROX
632e45474aSMike Rapoport (Microsoft)  *
642e45474aSMike Rapoport (Microsoft)  * A hook for architecures to fill execmem ranges with invalid instructions.
652e45474aSMike Rapoport (Microsoft)  * Architectures that use EXECMEM_ROX_CACHE must implement this.
662e45474aSMike Rapoport (Microsoft)  */
672e45474aSMike Rapoport (Microsoft) void execmem_fill_trapping_insns(void *ptr, size_t size, bool writable);
68*05e555b8SMike Rapoport (Microsoft) 
69*05e555b8SMike Rapoport (Microsoft) /**
70*05e555b8SMike Rapoport (Microsoft)  * execmem_make_temp_rw - temporarily remap region with read-write
71*05e555b8SMike Rapoport (Microsoft)  *			  permissions
72*05e555b8SMike Rapoport (Microsoft)  * @ptr:	address of the region to remap
73*05e555b8SMike Rapoport (Microsoft)  * @size:	size of the region to remap
74*05e555b8SMike Rapoport (Microsoft)  *
75*05e555b8SMike Rapoport (Microsoft)  * Remaps a part of the cached large page in the ROX cache in the range
76*05e555b8SMike Rapoport (Microsoft)  * [@ptr, @ptr + @size) as writable and not executable. The caller must
77*05e555b8SMike Rapoport (Microsoft)  * have exclusive ownership of this range and ensure nothing will try to
78*05e555b8SMike Rapoport (Microsoft)  * execute code in this range.
79*05e555b8SMike Rapoport (Microsoft)  *
80*05e555b8SMike Rapoport (Microsoft)  * Return: 0 on success or negative error code on failure.
81*05e555b8SMike Rapoport (Microsoft)  */
82*05e555b8SMike Rapoport (Microsoft) int execmem_make_temp_rw(void *ptr, size_t size);
83*05e555b8SMike Rapoport (Microsoft) 
84*05e555b8SMike Rapoport (Microsoft) /**
85*05e555b8SMike Rapoport (Microsoft)  * execmem_restore_rox - restore read-only-execute permissions
86*05e555b8SMike Rapoport (Microsoft)  * @ptr:	address of the region to remap
87*05e555b8SMike Rapoport (Microsoft)  * @size:	size of the region to remap
88*05e555b8SMike Rapoport (Microsoft)  *
89*05e555b8SMike Rapoport (Microsoft)  * Restores read-only-execute permissions on a range [@ptr, @ptr + @size)
90*05e555b8SMike Rapoport (Microsoft)  * after it was temporarily remapped as writable. Relies on architecture
91*05e555b8SMike Rapoport (Microsoft)  * implementation of set_memory_rox() to restore mapping using large pages.
92*05e555b8SMike Rapoport (Microsoft)  *
93*05e555b8SMike Rapoport (Microsoft)  * Return: 0 on success or negative error code on failure.
94*05e555b8SMike Rapoport (Microsoft)  */
95*05e555b8SMike Rapoport (Microsoft) int execmem_restore_rox(void *ptr, size_t size);
96*05e555b8SMike Rapoport (Microsoft) #else
97*05e555b8SMike Rapoport (Microsoft) static inline int execmem_make_temp_rw(void *ptr, size_t size) { return 0; }
98*05e555b8SMike Rapoport (Microsoft) static inline int execmem_restore_rox(void *ptr, size_t size) { return 0; }
992e45474aSMike Rapoport (Microsoft) #endif
1002e45474aSMike Rapoport (Microsoft) 
101223b5e57SMike Rapoport (IBM) /**
102f6bec26cSMike Rapoport (IBM)  * struct execmem_range - definition of an address space suitable for code and
103f6bec26cSMike Rapoport (IBM)  *			  related data allocations
104f6bec26cSMike Rapoport (IBM)  * @start:	address space start
105f6bec26cSMike Rapoport (IBM)  * @end:	address space end (inclusive)
106223b5e57SMike Rapoport (IBM)  * @fallback_start: start of the secondary address space range for fallback
107223b5e57SMike Rapoport (IBM)  *                  allocations on architectures that require it
108223b5e57SMike Rapoport (IBM)  * @fallback_end:   start of the secondary address space (inclusive)
109f6bec26cSMike Rapoport (IBM)  * @pgprot:	permissions for memory in this address space
110f6bec26cSMike Rapoport (IBM)  * @alignment:	alignment required for text allocations
111223b5e57SMike Rapoport (IBM)  * @flags:	options for memory allocations for this range
112f6bec26cSMike Rapoport (IBM)  */
113f6bec26cSMike Rapoport (IBM) struct execmem_range {
114f6bec26cSMike Rapoport (IBM) 	unsigned long   start;
115f6bec26cSMike Rapoport (IBM) 	unsigned long   end;
116223b5e57SMike Rapoport (IBM) 	unsigned long   fallback_start;
117223b5e57SMike Rapoport (IBM) 	unsigned long   fallback_end;
118f6bec26cSMike Rapoport (IBM) 	pgprot_t        pgprot;
119f6bec26cSMike Rapoport (IBM) 	unsigned int	alignment;
120223b5e57SMike Rapoport (IBM) 	enum execmem_range_flags flags;
121f6bec26cSMike Rapoport (IBM) };
122f6bec26cSMike Rapoport (IBM) 
123f6bec26cSMike Rapoport (IBM) /**
124f6bec26cSMike Rapoport (IBM)  * struct execmem_info - architecture parameters for code allocations
125f6bec26cSMike Rapoport (IBM)  * @ranges: array of parameter sets defining architecture specific
126f6bec26cSMike Rapoport (IBM)  * parameters for executable memory allocations. The ranges that are not
127f6bec26cSMike Rapoport (IBM)  * explicitly initialized by an architecture use parameters defined for
128f6bec26cSMike Rapoport (IBM)  * @EXECMEM_DEFAULT.
129f6bec26cSMike Rapoport (IBM)  */
130f6bec26cSMike Rapoport (IBM) struct execmem_info {
131f6bec26cSMike Rapoport (IBM) 	struct execmem_range	ranges[EXECMEM_TYPE_MAX];
132f6bec26cSMike Rapoport (IBM) };
133f6bec26cSMike Rapoport (IBM) 
134f6bec26cSMike Rapoport (IBM) /**
135f6bec26cSMike Rapoport (IBM)  * execmem_arch_setup - define parameters for allocations of executable memory
136f6bec26cSMike Rapoport (IBM)  *
137f6bec26cSMike Rapoport (IBM)  * A hook for architectures to define parameters for allocations of
138f6bec26cSMike Rapoport (IBM)  * executable memory. These parameters should be filled into the
139f6bec26cSMike Rapoport (IBM)  * @execmem_info structure.
140f6bec26cSMike Rapoport (IBM)  *
141f6bec26cSMike Rapoport (IBM)  * For architectures that do not implement this method a default set of
142f6bec26cSMike Rapoport (IBM)  * parameters will be used
143f6bec26cSMike Rapoport (IBM)  *
144f6bec26cSMike Rapoport (IBM)  * Return: a structure defining architecture parameters and restrictions
145f6bec26cSMike Rapoport (IBM)  * for allocations of executable memory
146f6bec26cSMike Rapoport (IBM)  */
147f6bec26cSMike Rapoport (IBM) struct execmem_info *execmem_arch_setup(void);
148f6bec26cSMike Rapoport (IBM) 
149f6bec26cSMike Rapoport (IBM) /**
15012af2b83SMike Rapoport (IBM)  * execmem_alloc - allocate executable memory
15112af2b83SMike Rapoport (IBM)  * @type: type of the allocation
15212af2b83SMike Rapoport (IBM)  * @size: how many bytes of memory are required
15312af2b83SMike Rapoport (IBM)  *
15412af2b83SMike Rapoport (IBM)  * Allocates memory that will contain executable code, either generated or
15512af2b83SMike Rapoport (IBM)  * loaded from kernel modules.
15612af2b83SMike Rapoport (IBM)  *
157223b5e57SMike Rapoport (IBM)  * Allocates memory that will contain data coupled with executable code,
158223b5e57SMike Rapoport (IBM)  * like data sections in kernel modules.
159223b5e57SMike Rapoport (IBM)  *
16012af2b83SMike Rapoport (IBM)  * The memory will have protections defined by architecture for executable
16112af2b83SMike Rapoport (IBM)  * region of the @type.
16212af2b83SMike Rapoport (IBM)  *
16312af2b83SMike Rapoport (IBM)  * Return: a pointer to the allocated memory or %NULL
16412af2b83SMike Rapoport (IBM)  */
16512af2b83SMike Rapoport (IBM) void *execmem_alloc(enum execmem_type type, size_t size);
16612af2b83SMike Rapoport (IBM) 
16712af2b83SMike Rapoport (IBM) /**
16812af2b83SMike Rapoport (IBM)  * execmem_free - free executable memory
16912af2b83SMike Rapoport (IBM)  * @ptr: pointer to the memory that should be freed
17012af2b83SMike Rapoport (IBM)  */
17112af2b83SMike Rapoport (IBM) void execmem_free(void *ptr);
17212af2b83SMike Rapoport (IBM) 
1730f9b6856SSuren Baghdasaryan #ifdef CONFIG_MMU
1740f9b6856SSuren Baghdasaryan /**
1750f9b6856SSuren Baghdasaryan  * execmem_vmap - create virtual mapping for EXECMEM_MODULE_DATA memory
1760f9b6856SSuren Baghdasaryan  * @size: size of the virtual mapping in bytes
1770f9b6856SSuren Baghdasaryan  *
1780f9b6856SSuren Baghdasaryan  * Maps virtually contiguous area in the range suitable for EXECMEM_MODULE_DATA.
1790f9b6856SSuren Baghdasaryan  *
1800f9b6856SSuren Baghdasaryan  * Return: the area descriptor on success or %NULL on failure.
1810f9b6856SSuren Baghdasaryan  */
1820f9b6856SSuren Baghdasaryan struct vm_struct *execmem_vmap(size_t size);
1830f9b6856SSuren Baghdasaryan #endif
1840f9b6856SSuren Baghdasaryan 
1850c133b1eSMike Rapoport (Microsoft) /**
1860c133b1eSMike Rapoport (Microsoft)  * execmem_update_copy - copy an update to executable memory
1870c133b1eSMike Rapoport (Microsoft)  * @dst:  destination address to update
1880c133b1eSMike Rapoport (Microsoft)  * @src:  source address containing the data
1890c133b1eSMike Rapoport (Microsoft)  * @size: how many bytes of memory shold be copied
1900c133b1eSMike Rapoport (Microsoft)  *
1910c133b1eSMike Rapoport (Microsoft)  * Copy @size bytes from @src to @dst using text poking if the memory at
1920c133b1eSMike Rapoport (Microsoft)  * @dst is read-only.
1930c133b1eSMike Rapoport (Microsoft)  *
1940c133b1eSMike Rapoport (Microsoft)  * Return: a pointer to @dst or NULL on error
1950c133b1eSMike Rapoport (Microsoft)  */
1960c133b1eSMike Rapoport (Microsoft) void *execmem_update_copy(void *dst, const void *src, size_t size);
1970c133b1eSMike Rapoport (Microsoft) 
1980c133b1eSMike Rapoport (Microsoft) /**
1990c133b1eSMike Rapoport (Microsoft)  * execmem_is_rox - check if execmem is read-only
2000c133b1eSMike Rapoport (Microsoft)  * @type - the execmem type to check
2010c133b1eSMike Rapoport (Microsoft)  *
2020c133b1eSMike Rapoport (Microsoft)  * Return: %true if the @type is read-only, %false if it's writable
2030c133b1eSMike Rapoport (Microsoft)  */
2040c133b1eSMike Rapoport (Microsoft) bool execmem_is_rox(enum execmem_type type);
2050c133b1eSMike Rapoport (Microsoft) 
206223b5e57SMike Rapoport (IBM) #if defined(CONFIG_EXECMEM) && !defined(CONFIG_ARCH_WANTS_EXECMEM_LATE)
207f6bec26cSMike Rapoport (IBM) void execmem_init(void);
208f6bec26cSMike Rapoport (IBM) #else
209f6bec26cSMike Rapoport (IBM) static inline void execmem_init(void) {}
210f6bec26cSMike Rapoport (IBM) #endif
211f6bec26cSMike Rapoport (IBM) 
21212af2b83SMike Rapoport (IBM) #endif /* _LINUX_EXECMEM_ALLOC_H */
213