xref: /linux/arch/x86/include/asm/jump_label.h (revision 79d2e1919a2728ef49d938eb20ebd5903c14dfb0)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_JUMP_LABEL_H
3 #define _ASM_X86_JUMP_LABEL_H
4 
5 #define HAVE_JUMP_LABEL_BATCH
6 
7 #include <asm/asm.h>
8 #include <asm/nops.h>
9 
10 #ifndef __ASSEMBLY__
11 
12 #include <linux/stringify.h>
13 #include <linux/types.h>
14 
15 #define JUMP_TABLE_ENTRY(key, label)			\
16 	".pushsection __jump_table,  \"aw\" \n\t"	\
17 	_ASM_ALIGN "\n\t"				\
18 	".long 1b - . \n\t"				\
19 	".long " label " - . \n\t"			\
20 	_ASM_PTR " " key " - . \n\t"			\
21 	".popsection \n\t"
22 
23 /* This macro is also expanded on the Rust side. */
24 #ifdef CONFIG_HAVE_JUMP_LABEL_HACK
25 #define ARCH_STATIC_BRANCH_ASM(key, label)		\
26 	"1: jmp " label " # objtool NOPs this \n\t"	\
27 	JUMP_TABLE_ENTRY(key " + 2", label)
28 #else /* !CONFIG_HAVE_JUMP_LABEL_HACK */
29 #define ARCH_STATIC_BRANCH_ASM(key, label)		\
30 	"1: .byte " __stringify(BYTES_NOP5) "\n\t"	\
31 	JUMP_TABLE_ENTRY(key, label)
32 #endif /* CONFIG_HAVE_JUMP_LABEL_HACK */
33 
34 static __always_inline bool arch_static_branch(struct static_key * const key, const bool branch)
35 {
36 	asm goto(ARCH_STATIC_BRANCH_ASM("%c0 + %c1", "%l[l_yes]")
37 		: :  "i" (key), "i" (branch) : : l_yes);
38 
39 	return false;
40 l_yes:
41 	return true;
42 }
43 
44 static __always_inline bool arch_static_branch_jump(struct static_key * const key, const bool branch)
45 {
46 	asm goto("1:"
47 		"jmp %l[l_yes]\n\t"
48 		JUMP_TABLE_ENTRY("%c0 + %c1", "%l[l_yes]")
49 		: :  "i" (key), "i" (branch) : : l_yes);
50 
51 	return false;
52 l_yes:
53 	return true;
54 }
55 
56 extern int arch_jump_entry_size(struct jump_entry *entry);
57 
58 #endif	/* __ASSEMBLY__ */
59 
60 #endif
61