xref: /linux/arch/x86/include/asm/runtime-const.h (revision 72856afd33f2fa166c06f1536003e300e51ecf09)
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 __ASSEMBLER__
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 /* __ASSEMBLER__ */
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_mask_32(val, sym) ({			\
45 	typeof(0u+(val)) __ret = (val);				\
46 	asm_inline("and $0x12345678, %k0\n1:\n"				\
47 		   ".pushsection runtime_mask_" #sym ",\"a\"\n\t"\
48 		   ".long 1b - 4 - .\n"				\
49 		   ".popsection"				\
50 		   : "+r" (__ret));				\
51 	__ret; })
52 
53 #define runtime_const_init(type, sym) do {		\
54 	extern s32 __start_runtime_##type##_##sym[];	\
55 	extern s32 __stop_runtime_##type##_##sym[];	\
56 	runtime_const_fixup(__runtime_fixup_##type,	\
57 		(unsigned long)(sym), 			\
58 		__start_runtime_##type##_##sym,		\
59 		__stop_runtime_##type##_##sym);		\
60 } while (0)
61 
62 /*
63  * The text patching is trivial - you can only do this at init time,
64  * when the text section hasn't been marked RO, and before the text
65  * has ever been executed.
66  */
67 static inline void __runtime_fixup_ptr(void *where, unsigned long val)
68 {
69 	*(unsigned long *)where = val;
70 }
71 
72 static inline void __runtime_fixup_shift(void *where, unsigned long val)
73 {
74 	*(unsigned char *)where = val;
75 }
76 
77 static inline void __runtime_fixup_mask(void *where, unsigned long val)
78 {
79 	*(unsigned int *)where = val;
80 }
81 
82 static inline void runtime_const_fixup(void (*fn)(void *, unsigned long),
83 	unsigned long val, s32 *start, s32 *end)
84 {
85 	while (start < end) {
86 		fn(*start + (void *)start, val);
87 		start++;
88 	}
89 }
90 
91 #endif /* __ASSEMBLER__ */
92 #endif
93