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