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