xref: /linux/arch/x86/include/asm/runtime-const.h (revision 74d4432421a3e2669fbccc08c0f4fc2980bf0e39)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_RUNTIME_CONST_H
3 #define _ASM_RUNTIME_CONST_H
4 
5 #ifdef MODULE
6   #error "Cannot use runtime-const infrastructure from modules"
7 #endif
8 
9 #ifdef __ASSEMBLY__
10 
11 .macro RUNTIME_CONST_PTR sym reg
12 	movq	$0x0123456789abcdef, %\reg
13 	1:
14 	.pushsection runtime_ptr_\sym, "a"
15 	.long	1b - 8 - .
16 	.popsection
17 .endm
18 
19 #else /* __ASSEMBLY__ */
20 
21 #define runtime_const_ptr(sym) ({				\
22 	typeof(sym) __ret;					\
23 	asm_inline("mov %1,%0\n1:\n"				\
24 		".pushsection runtime_ptr_" #sym ",\"a\"\n\t"	\
25 		".long 1b - %c2 - .\n"				\
26 		".popsection"					\
27 		:"=r" (__ret)					\
28 		:"i" ((unsigned long)0x0123456789abcdefull),	\
29 		 "i" (sizeof(long)));				\
30 	__ret; })
31 
32 // The 'typeof' will create at _least_ a 32-bit type, but
33 // will happily also take a bigger type and the 'shrl' will
34 // clear the upper bits
35 #define runtime_const_shift_right_32(val, sym) ({		\
36 	typeof(0u+(val)) __ret = (val);				\
37 	asm_inline("shrl $12,%k0\n1:\n"				\
38 		".pushsection runtime_shift_" #sym ",\"a\"\n\t"	\
39 		".long 1b - 1 - .\n"				\
40 		".popsection"					\
41 		:"+r" (__ret));					\
42 	__ret; })
43 
44 #define runtime_const_init(type, sym) do {		\
45 	extern s32 __start_runtime_##type##_##sym[];	\
46 	extern s32 __stop_runtime_##type##_##sym[];	\
47 	runtime_const_fixup(__runtime_fixup_##type,	\
48 		(unsigned long)(sym), 			\
49 		__start_runtime_##type##_##sym,		\
50 		__stop_runtime_##type##_##sym);		\
51 } while (0)
52 
53 /*
54  * The text patching is trivial - you can only do this at init time,
55  * when the text section hasn't been marked RO, and before the text
56  * has ever been executed.
57  */
58 static inline void __runtime_fixup_ptr(void *where, unsigned long val)
59 {
60 	*(unsigned long *)where = val;
61 }
62 
63 static inline void __runtime_fixup_shift(void *where, unsigned long val)
64 {
65 	*(unsigned char *)where = val;
66 }
67 
68 static inline void runtime_const_fixup(void (*fn)(void *, unsigned long),
69 	unsigned long val, s32 *start, s32 *end)
70 {
71 	while (start < end) {
72 		fn(*start + (void *)start, val);
73 		start++;
74 	}
75 }
76 
77 #endif /* __ASSEMBLY__ */
78 #endif
79