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