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>
7*872df34dSPeter Zijlstra #include <linux/cleanup.h>
812af2b83SMike Rapoport (IBM)
9223b5e57SMike Rapoport (IBM) #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
10223b5e57SMike Rapoport (IBM) !defined(CONFIG_KASAN_VMALLOC)
11223b5e57SMike Rapoport (IBM) #include <linux/kasan.h>
12223b5e57SMike Rapoport (IBM) #define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT)
13223b5e57SMike Rapoport (IBM) #else
14223b5e57SMike Rapoport (IBM) #define MODULE_ALIGN PAGE_SIZE
15223b5e57SMike Rapoport (IBM) #endif
16223b5e57SMike Rapoport (IBM)
1712af2b83SMike Rapoport (IBM) /**
1812af2b83SMike Rapoport (IBM) * enum execmem_type - types of executable memory ranges
1912af2b83SMike Rapoport (IBM) *
2012af2b83SMike Rapoport (IBM) * There are several subsystems that allocate executable memory.
2112af2b83SMike Rapoport (IBM) * Architectures define different restrictions on placement,
2212af2b83SMike Rapoport (IBM) * permissions, alignment and other parameters for memory that can be used
2312af2b83SMike Rapoport (IBM) * by these subsystems.
2412af2b83SMike Rapoport (IBM) * Types in this enum identify subsystems that allocate executable memory
2512af2b83SMike Rapoport (IBM) * and let architectures define parameters for ranges suitable for
2612af2b83SMike Rapoport (IBM) * allocations by each subsystem.
2712af2b83SMike Rapoport (IBM) *
2812af2b83SMike Rapoport (IBM) * @EXECMEM_DEFAULT: default parameters that would be used for types that
2912af2b83SMike Rapoport (IBM) * are not explicitly defined.
3012af2b83SMike Rapoport (IBM) * @EXECMEM_MODULE_TEXT: parameters for module text sections
3112af2b83SMike Rapoport (IBM) * @EXECMEM_KPROBES: parameters for kprobes
3212af2b83SMike Rapoport (IBM) * @EXECMEM_FTRACE: parameters for ftrace
3312af2b83SMike Rapoport (IBM) * @EXECMEM_BPF: parameters for BPF
34223b5e57SMike Rapoport (IBM) * @EXECMEM_MODULE_DATA: parameters for module data sections
3512af2b83SMike Rapoport (IBM) * @EXECMEM_TYPE_MAX:
3612af2b83SMike Rapoport (IBM) */
3712af2b83SMike Rapoport (IBM) enum execmem_type {
3812af2b83SMike Rapoport (IBM) EXECMEM_DEFAULT,
3912af2b83SMike Rapoport (IBM) EXECMEM_MODULE_TEXT = EXECMEM_DEFAULT,
4012af2b83SMike Rapoport (IBM) EXECMEM_KPROBES,
4112af2b83SMike Rapoport (IBM) EXECMEM_FTRACE,
4212af2b83SMike Rapoport (IBM) EXECMEM_BPF,
43223b5e57SMike Rapoport (IBM) EXECMEM_MODULE_DATA,
4412af2b83SMike Rapoport (IBM) EXECMEM_TYPE_MAX,
4512af2b83SMike Rapoport (IBM) };
4612af2b83SMike Rapoport (IBM)
4712af2b83SMike Rapoport (IBM) /**
48223b5e57SMike Rapoport (IBM) * enum execmem_range_flags - options for executable memory allocations
49223b5e57SMike Rapoport (IBM) * @EXECMEM_KASAN_SHADOW: allocate kasan shadow
500c133b1eSMike Rapoport (Microsoft) * @EXECMEM_ROX_CACHE: allocations should use ROX cache of huge pages
51223b5e57SMike Rapoport (IBM) */
52223b5e57SMike Rapoport (IBM) enum execmem_range_flags {
53223b5e57SMike Rapoport (IBM) EXECMEM_KASAN_SHADOW = (1 << 0),
540c133b1eSMike Rapoport (Microsoft) EXECMEM_ROX_CACHE = (1 << 1),
55223b5e57SMike Rapoport (IBM) };
56223b5e57SMike Rapoport (IBM)
57d6d1e3e6SPeter Zijlstra #if defined(CONFIG_ARCH_HAS_EXECMEM_ROX) && defined(CONFIG_EXECMEM)
582e45474aSMike Rapoport (Microsoft) /**
592e45474aSMike Rapoport (Microsoft) * execmem_fill_trapping_insns - set memory to contain instructions that
602e45474aSMike Rapoport (Microsoft) * will trap
612e45474aSMike Rapoport (Microsoft) * @ptr: pointer to memory to fill
622e45474aSMike Rapoport (Microsoft) * @size: size of the range to fill
632e45474aSMike Rapoport (Microsoft) * @writable: is the memory poited by @ptr is writable or ROX
642e45474aSMike Rapoport (Microsoft) *
652e45474aSMike Rapoport (Microsoft) * A hook for architecures to fill execmem ranges with invalid instructions.
662e45474aSMike Rapoport (Microsoft) * Architectures that use EXECMEM_ROX_CACHE must implement this.
672e45474aSMike Rapoport (Microsoft) */
682e45474aSMike Rapoport (Microsoft) void execmem_fill_trapping_insns(void *ptr, size_t size, bool writable);
6905e555b8SMike Rapoport (Microsoft)
7005e555b8SMike Rapoport (Microsoft) /**
7105e555b8SMike Rapoport (Microsoft) * execmem_make_temp_rw - temporarily remap region with read-write
7205e555b8SMike Rapoport (Microsoft) * permissions
7305e555b8SMike Rapoport (Microsoft) * @ptr: address of the region to remap
7405e555b8SMike Rapoport (Microsoft) * @size: size of the region to remap
7505e555b8SMike Rapoport (Microsoft) *
7605e555b8SMike Rapoport (Microsoft) * Remaps a part of the cached large page in the ROX cache in the range
7705e555b8SMike Rapoport (Microsoft) * [@ptr, @ptr + @size) as writable and not executable. The caller must
7805e555b8SMike Rapoport (Microsoft) * have exclusive ownership of this range and ensure nothing will try to
7905e555b8SMike Rapoport (Microsoft) * execute code in this range.
8005e555b8SMike Rapoport (Microsoft) *
8105e555b8SMike Rapoport (Microsoft) * Return: 0 on success or negative error code on failure.
8205e555b8SMike Rapoport (Microsoft) */
8305e555b8SMike Rapoport (Microsoft) int execmem_make_temp_rw(void *ptr, size_t size);
8405e555b8SMike Rapoport (Microsoft)
8505e555b8SMike Rapoport (Microsoft) /**
8605e555b8SMike Rapoport (Microsoft) * execmem_restore_rox - restore read-only-execute permissions
8705e555b8SMike Rapoport (Microsoft) * @ptr: address of the region to remap
8805e555b8SMike Rapoport (Microsoft) * @size: size of the region to remap
8905e555b8SMike Rapoport (Microsoft) *
9005e555b8SMike Rapoport (Microsoft) * Restores read-only-execute permissions on a range [@ptr, @ptr + @size)
9105e555b8SMike Rapoport (Microsoft) * after it was temporarily remapped as writable. Relies on architecture
9205e555b8SMike Rapoport (Microsoft) * implementation of set_memory_rox() to restore mapping using large pages.
9305e555b8SMike Rapoport (Microsoft) *
9405e555b8SMike Rapoport (Microsoft) * Return: 0 on success or negative error code on failure.
9505e555b8SMike Rapoport (Microsoft) */
9605e555b8SMike Rapoport (Microsoft) int execmem_restore_rox(void *ptr, size_t size);
97d6d1e3e6SPeter Zijlstra
98d6d1e3e6SPeter Zijlstra /*
99d6d1e3e6SPeter Zijlstra * Called from mark_readonly(), where the system transitions to ROX.
100d6d1e3e6SPeter Zijlstra */
101d6d1e3e6SPeter Zijlstra void execmem_cache_make_ro(void);
10205e555b8SMike Rapoport (Microsoft) #else
execmem_make_temp_rw(void * ptr,size_t size)10305e555b8SMike Rapoport (Microsoft) static inline int execmem_make_temp_rw(void *ptr, size_t size) { return 0; }
execmem_restore_rox(void * ptr,size_t size)10405e555b8SMike Rapoport (Microsoft) static inline int execmem_restore_rox(void *ptr, size_t size) { return 0; }
execmem_cache_make_ro(void)105d6d1e3e6SPeter Zijlstra static inline void execmem_cache_make_ro(void) { }
1062e45474aSMike Rapoport (Microsoft) #endif
1072e45474aSMike Rapoport (Microsoft)
108223b5e57SMike Rapoport (IBM) /**
109f6bec26cSMike Rapoport (IBM) * struct execmem_range - definition of an address space suitable for code and
110f6bec26cSMike Rapoport (IBM) * related data allocations
111f6bec26cSMike Rapoport (IBM) * @start: address space start
112f6bec26cSMike Rapoport (IBM) * @end: address space end (inclusive)
113223b5e57SMike Rapoport (IBM) * @fallback_start: start of the secondary address space range for fallback
114223b5e57SMike Rapoport (IBM) * allocations on architectures that require it
115223b5e57SMike Rapoport (IBM) * @fallback_end: start of the secondary address space (inclusive)
116f6bec26cSMike Rapoport (IBM) * @pgprot: permissions for memory in this address space
117f6bec26cSMike Rapoport (IBM) * @alignment: alignment required for text allocations
118223b5e57SMike Rapoport (IBM) * @flags: options for memory allocations for this range
119f6bec26cSMike Rapoport (IBM) */
120f6bec26cSMike Rapoport (IBM) struct execmem_range {
121f6bec26cSMike Rapoport (IBM) unsigned long start;
122f6bec26cSMike Rapoport (IBM) unsigned long end;
123223b5e57SMike Rapoport (IBM) unsigned long fallback_start;
124223b5e57SMike Rapoport (IBM) unsigned long fallback_end;
125f6bec26cSMike Rapoport (IBM) pgprot_t pgprot;
126f6bec26cSMike Rapoport (IBM) unsigned int alignment;
127223b5e57SMike Rapoport (IBM) enum execmem_range_flags flags;
128f6bec26cSMike Rapoport (IBM) };
129f6bec26cSMike Rapoport (IBM)
130f6bec26cSMike Rapoport (IBM) /**
131f6bec26cSMike Rapoport (IBM) * struct execmem_info - architecture parameters for code allocations
132f6bec26cSMike Rapoport (IBM) * @ranges: array of parameter sets defining architecture specific
133f6bec26cSMike Rapoport (IBM) * parameters for executable memory allocations. The ranges that are not
134f6bec26cSMike Rapoport (IBM) * explicitly initialized by an architecture use parameters defined for
135f6bec26cSMike Rapoport (IBM) * @EXECMEM_DEFAULT.
136f6bec26cSMike Rapoport (IBM) */
137f6bec26cSMike Rapoport (IBM) struct execmem_info {
138f6bec26cSMike Rapoport (IBM) struct execmem_range ranges[EXECMEM_TYPE_MAX];
139f6bec26cSMike Rapoport (IBM) };
140f6bec26cSMike Rapoport (IBM)
141f6bec26cSMike Rapoport (IBM) /**
142f6bec26cSMike Rapoport (IBM) * execmem_arch_setup - define parameters for allocations of executable memory
143f6bec26cSMike Rapoport (IBM) *
144f6bec26cSMike Rapoport (IBM) * A hook for architectures to define parameters for allocations of
145f6bec26cSMike Rapoport (IBM) * executable memory. These parameters should be filled into the
146f6bec26cSMike Rapoport (IBM) * @execmem_info structure.
147f6bec26cSMike Rapoport (IBM) *
148f6bec26cSMike Rapoport (IBM) * For architectures that do not implement this method a default set of
149f6bec26cSMike Rapoport (IBM) * parameters will be used
150f6bec26cSMike Rapoport (IBM) *
151f6bec26cSMike Rapoport (IBM) * Return: a structure defining architecture parameters and restrictions
152f6bec26cSMike Rapoport (IBM) * for allocations of executable memory
153f6bec26cSMike Rapoport (IBM) */
154f6bec26cSMike Rapoport (IBM) struct execmem_info *execmem_arch_setup(void);
155f6bec26cSMike Rapoport (IBM)
156f6bec26cSMike Rapoport (IBM) /**
15712af2b83SMike Rapoport (IBM) * execmem_alloc - allocate executable memory
15812af2b83SMike Rapoport (IBM) * @type: type of the allocation
15912af2b83SMike Rapoport (IBM) * @size: how many bytes of memory are required
16012af2b83SMike Rapoport (IBM) *
16112af2b83SMike Rapoport (IBM) * Allocates memory that will contain executable code, either generated or
16212af2b83SMike Rapoport (IBM) * loaded from kernel modules.
16312af2b83SMike Rapoport (IBM) *
164223b5e57SMike Rapoport (IBM) * Allocates memory that will contain data coupled with executable code,
165223b5e57SMike Rapoport (IBM) * like data sections in kernel modules.
166223b5e57SMike Rapoport (IBM) *
16712af2b83SMike Rapoport (IBM) * The memory will have protections defined by architecture for executable
16812af2b83SMike Rapoport (IBM) * region of the @type.
16912af2b83SMike Rapoport (IBM) *
17012af2b83SMike Rapoport (IBM) * Return: a pointer to the allocated memory or %NULL
17112af2b83SMike Rapoport (IBM) */
17212af2b83SMike Rapoport (IBM) void *execmem_alloc(enum execmem_type type, size_t size);
17312af2b83SMike Rapoport (IBM)
17412af2b83SMike Rapoport (IBM) /**
17512af2b83SMike Rapoport (IBM) * execmem_free - free executable memory
17612af2b83SMike Rapoport (IBM) * @ptr: pointer to the memory that should be freed
17712af2b83SMike Rapoport (IBM) */
17812af2b83SMike Rapoport (IBM) void execmem_free(void *ptr);
17912af2b83SMike Rapoport (IBM)
180*872df34dSPeter Zijlstra DEFINE_FREE(execmem, void *, if (_T) execmem_free(_T));
181*872df34dSPeter Zijlstra
1820f9b6856SSuren Baghdasaryan #ifdef CONFIG_MMU
1830f9b6856SSuren Baghdasaryan /**
1840f9b6856SSuren Baghdasaryan * execmem_vmap - create virtual mapping for EXECMEM_MODULE_DATA memory
1850f9b6856SSuren Baghdasaryan * @size: size of the virtual mapping in bytes
1860f9b6856SSuren Baghdasaryan *
1870f9b6856SSuren Baghdasaryan * Maps virtually contiguous area in the range suitable for EXECMEM_MODULE_DATA.
1880f9b6856SSuren Baghdasaryan *
1890f9b6856SSuren Baghdasaryan * Return: the area descriptor on success or %NULL on failure.
1900f9b6856SSuren Baghdasaryan */
1910f9b6856SSuren Baghdasaryan struct vm_struct *execmem_vmap(size_t size);
1920f9b6856SSuren Baghdasaryan #endif
1930f9b6856SSuren Baghdasaryan
1940c133b1eSMike Rapoport (Microsoft) /**
1950c133b1eSMike Rapoport (Microsoft) * execmem_update_copy - copy an update to executable memory
1960c133b1eSMike Rapoport (Microsoft) * @dst: destination address to update
1970c133b1eSMike Rapoport (Microsoft) * @src: source address containing the data
1980c133b1eSMike Rapoport (Microsoft) * @size: how many bytes of memory shold be copied
1990c133b1eSMike Rapoport (Microsoft) *
2000c133b1eSMike Rapoport (Microsoft) * Copy @size bytes from @src to @dst using text poking if the memory at
2010c133b1eSMike Rapoport (Microsoft) * @dst is read-only.
2020c133b1eSMike Rapoport (Microsoft) *
2030c133b1eSMike Rapoport (Microsoft) * Return: a pointer to @dst or NULL on error
2040c133b1eSMike Rapoport (Microsoft) */
2050c133b1eSMike Rapoport (Microsoft) void *execmem_update_copy(void *dst, const void *src, size_t size);
2060c133b1eSMike Rapoport (Microsoft)
2070c133b1eSMike Rapoport (Microsoft) /**
2080c133b1eSMike Rapoport (Microsoft) * execmem_is_rox - check if execmem is read-only
2090c133b1eSMike Rapoport (Microsoft) * @type - the execmem type to check
2100c133b1eSMike Rapoport (Microsoft) *
2110c133b1eSMike Rapoport (Microsoft) * Return: %true if the @type is read-only, %false if it's writable
2120c133b1eSMike Rapoport (Microsoft) */
2130c133b1eSMike Rapoport (Microsoft) bool execmem_is_rox(enum execmem_type type);
2140c133b1eSMike Rapoport (Microsoft)
215223b5e57SMike Rapoport (IBM) #if defined(CONFIG_EXECMEM) && !defined(CONFIG_ARCH_WANTS_EXECMEM_LATE)
216f6bec26cSMike Rapoport (IBM) void execmem_init(void);
217f6bec26cSMike Rapoport (IBM) #else
execmem_init(void)218f6bec26cSMike Rapoport (IBM) static inline void execmem_init(void) {}
219f6bec26cSMike Rapoport (IBM) #endif
220f6bec26cSMike Rapoport (IBM)
22112af2b83SMike Rapoport (IBM) #endif /* _LINUX_EXECMEM_ALLOC_H */
222