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) 56*2e45474aSMike Rapoport (Microsoft) #ifdef CONFIG_ARCH_HAS_EXECMEM_ROX 57*2e45474aSMike Rapoport (Microsoft) /** 58*2e45474aSMike Rapoport (Microsoft) * execmem_fill_trapping_insns - set memory to contain instructions that 59*2e45474aSMike Rapoport (Microsoft) * will trap 60*2e45474aSMike Rapoport (Microsoft) * @ptr: pointer to memory to fill 61*2e45474aSMike Rapoport (Microsoft) * @size: size of the range to fill 62*2e45474aSMike Rapoport (Microsoft) * @writable: is the memory poited by @ptr is writable or ROX 63*2e45474aSMike Rapoport (Microsoft) * 64*2e45474aSMike Rapoport (Microsoft) * A hook for architecures to fill execmem ranges with invalid instructions. 65*2e45474aSMike Rapoport (Microsoft) * Architectures that use EXECMEM_ROX_CACHE must implement this. 66*2e45474aSMike Rapoport (Microsoft) */ 67*2e45474aSMike Rapoport (Microsoft) void execmem_fill_trapping_insns(void *ptr, size_t size, bool writable); 68*2e45474aSMike Rapoport (Microsoft) #endif 69*2e45474aSMike Rapoport (Microsoft) 70223b5e57SMike Rapoport (IBM) /** 71f6bec26cSMike Rapoport (IBM) * struct execmem_range - definition of an address space suitable for code and 72f6bec26cSMike Rapoport (IBM) * related data allocations 73f6bec26cSMike Rapoport (IBM) * @start: address space start 74f6bec26cSMike Rapoport (IBM) * @end: address space end (inclusive) 75223b5e57SMike Rapoport (IBM) * @fallback_start: start of the secondary address space range for fallback 76223b5e57SMike Rapoport (IBM) * allocations on architectures that require it 77223b5e57SMike Rapoport (IBM) * @fallback_end: start of the secondary address space (inclusive) 78f6bec26cSMike Rapoport (IBM) * @pgprot: permissions for memory in this address space 79f6bec26cSMike Rapoport (IBM) * @alignment: alignment required for text allocations 80223b5e57SMike Rapoport (IBM) * @flags: options for memory allocations for this range 81f6bec26cSMike Rapoport (IBM) */ 82f6bec26cSMike Rapoport (IBM) struct execmem_range { 83f6bec26cSMike Rapoport (IBM) unsigned long start; 84f6bec26cSMike Rapoport (IBM) unsigned long end; 85223b5e57SMike Rapoport (IBM) unsigned long fallback_start; 86223b5e57SMike Rapoport (IBM) unsigned long fallback_end; 87f6bec26cSMike Rapoport (IBM) pgprot_t pgprot; 88f6bec26cSMike Rapoport (IBM) unsigned int alignment; 89223b5e57SMike Rapoport (IBM) enum execmem_range_flags flags; 90f6bec26cSMike Rapoport (IBM) }; 91f6bec26cSMike Rapoport (IBM) 92f6bec26cSMike Rapoport (IBM) /** 93f6bec26cSMike Rapoport (IBM) * struct execmem_info - architecture parameters for code allocations 94f6bec26cSMike Rapoport (IBM) * @ranges: array of parameter sets defining architecture specific 95f6bec26cSMike Rapoport (IBM) * parameters for executable memory allocations. The ranges that are not 96f6bec26cSMike Rapoport (IBM) * explicitly initialized by an architecture use parameters defined for 97f6bec26cSMike Rapoport (IBM) * @EXECMEM_DEFAULT. 98f6bec26cSMike Rapoport (IBM) */ 99f6bec26cSMike Rapoport (IBM) struct execmem_info { 100f6bec26cSMike Rapoport (IBM) struct execmem_range ranges[EXECMEM_TYPE_MAX]; 101f6bec26cSMike Rapoport (IBM) }; 102f6bec26cSMike Rapoport (IBM) 103f6bec26cSMike Rapoport (IBM) /** 104f6bec26cSMike Rapoport (IBM) * execmem_arch_setup - define parameters for allocations of executable memory 105f6bec26cSMike Rapoport (IBM) * 106f6bec26cSMike Rapoport (IBM) * A hook for architectures to define parameters for allocations of 107f6bec26cSMike Rapoport (IBM) * executable memory. These parameters should be filled into the 108f6bec26cSMike Rapoport (IBM) * @execmem_info structure. 109f6bec26cSMike Rapoport (IBM) * 110f6bec26cSMike Rapoport (IBM) * For architectures that do not implement this method a default set of 111f6bec26cSMike Rapoport (IBM) * parameters will be used 112f6bec26cSMike Rapoport (IBM) * 113f6bec26cSMike Rapoport (IBM) * Return: a structure defining architecture parameters and restrictions 114f6bec26cSMike Rapoport (IBM) * for allocations of executable memory 115f6bec26cSMike Rapoport (IBM) */ 116f6bec26cSMike Rapoport (IBM) struct execmem_info *execmem_arch_setup(void); 117f6bec26cSMike Rapoport (IBM) 118f6bec26cSMike Rapoport (IBM) /** 11912af2b83SMike Rapoport (IBM) * execmem_alloc - allocate executable memory 12012af2b83SMike Rapoport (IBM) * @type: type of the allocation 12112af2b83SMike Rapoport (IBM) * @size: how many bytes of memory are required 12212af2b83SMike Rapoport (IBM) * 12312af2b83SMike Rapoport (IBM) * Allocates memory that will contain executable code, either generated or 12412af2b83SMike Rapoport (IBM) * loaded from kernel modules. 12512af2b83SMike Rapoport (IBM) * 126223b5e57SMike Rapoport (IBM) * Allocates memory that will contain data coupled with executable code, 127223b5e57SMike Rapoport (IBM) * like data sections in kernel modules. 128223b5e57SMike Rapoport (IBM) * 12912af2b83SMike Rapoport (IBM) * The memory will have protections defined by architecture for executable 13012af2b83SMike Rapoport (IBM) * region of the @type. 13112af2b83SMike Rapoport (IBM) * 13212af2b83SMike Rapoport (IBM) * Return: a pointer to the allocated memory or %NULL 13312af2b83SMike Rapoport (IBM) */ 13412af2b83SMike Rapoport (IBM) void *execmem_alloc(enum execmem_type type, size_t size); 13512af2b83SMike Rapoport (IBM) 13612af2b83SMike Rapoport (IBM) /** 13712af2b83SMike Rapoport (IBM) * execmem_free - free executable memory 13812af2b83SMike Rapoport (IBM) * @ptr: pointer to the memory that should be freed 13912af2b83SMike Rapoport (IBM) */ 14012af2b83SMike Rapoport (IBM) void execmem_free(void *ptr); 14112af2b83SMike Rapoport (IBM) 1420c133b1eSMike Rapoport (Microsoft) /** 1430c133b1eSMike Rapoport (Microsoft) * execmem_update_copy - copy an update to executable memory 1440c133b1eSMike Rapoport (Microsoft) * @dst: destination address to update 1450c133b1eSMike Rapoport (Microsoft) * @src: source address containing the data 1460c133b1eSMike Rapoport (Microsoft) * @size: how many bytes of memory shold be copied 1470c133b1eSMike Rapoport (Microsoft) * 1480c133b1eSMike Rapoport (Microsoft) * Copy @size bytes from @src to @dst using text poking if the memory at 1490c133b1eSMike Rapoport (Microsoft) * @dst is read-only. 1500c133b1eSMike Rapoport (Microsoft) * 1510c133b1eSMike Rapoport (Microsoft) * Return: a pointer to @dst or NULL on error 1520c133b1eSMike Rapoport (Microsoft) */ 1530c133b1eSMike Rapoport (Microsoft) void *execmem_update_copy(void *dst, const void *src, size_t size); 1540c133b1eSMike Rapoport (Microsoft) 1550c133b1eSMike Rapoport (Microsoft) /** 1560c133b1eSMike Rapoport (Microsoft) * execmem_is_rox - check if execmem is read-only 1570c133b1eSMike Rapoport (Microsoft) * @type - the execmem type to check 1580c133b1eSMike Rapoport (Microsoft) * 1590c133b1eSMike Rapoport (Microsoft) * Return: %true if the @type is read-only, %false if it's writable 1600c133b1eSMike Rapoport (Microsoft) */ 1610c133b1eSMike Rapoport (Microsoft) bool execmem_is_rox(enum execmem_type type); 1620c133b1eSMike Rapoport (Microsoft) 163223b5e57SMike Rapoport (IBM) #if defined(CONFIG_EXECMEM) && !defined(CONFIG_ARCH_WANTS_EXECMEM_LATE) 164f6bec26cSMike Rapoport (IBM) void execmem_init(void); 165f6bec26cSMike Rapoport (IBM) #else 166f6bec26cSMike Rapoport (IBM) static inline void execmem_init(void) {} 167f6bec26cSMike Rapoport (IBM) #endif 168f6bec26cSMike Rapoport (IBM) 16912af2b83SMike Rapoport (IBM) #endif /* _LINUX_EXECMEM_ALLOC_H */ 170