xref: /OK3568_Linux_fs/kernel/arch/xtensa/include/asm/jump_label.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /* Copyright (C) 2018 Cadence Design Systems Inc. */
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun #ifndef _ASM_XTENSA_JUMP_LABEL_H
5*4882a593Smuzhiyun #define _ASM_XTENSA_JUMP_LABEL_H
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #ifndef __ASSEMBLY__
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/types.h>
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #define JUMP_LABEL_NOP_SIZE 3
12*4882a593Smuzhiyun 
arch_static_branch(struct static_key * key,bool branch)13*4882a593Smuzhiyun static __always_inline bool arch_static_branch(struct static_key *key,
14*4882a593Smuzhiyun 					       bool branch)
15*4882a593Smuzhiyun {
16*4882a593Smuzhiyun 	asm_volatile_goto("1:\n\t"
17*4882a593Smuzhiyun 			  "_nop\n\t"
18*4882a593Smuzhiyun 			  ".pushsection __jump_table,  \"aw\"\n\t"
19*4882a593Smuzhiyun 			  ".word 1b, %l[l_yes], %c0\n\t"
20*4882a593Smuzhiyun 			  ".popsection\n\t"
21*4882a593Smuzhiyun 			  : :  "i" (&((char *)key)[branch]) :  : l_yes);
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun 	return false;
24*4882a593Smuzhiyun l_yes:
25*4882a593Smuzhiyun 	return true;
26*4882a593Smuzhiyun }
27*4882a593Smuzhiyun 
arch_static_branch_jump(struct static_key * key,bool branch)28*4882a593Smuzhiyun static __always_inline bool arch_static_branch_jump(struct static_key *key,
29*4882a593Smuzhiyun 						    bool branch)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun 	/*
32*4882a593Smuzhiyun 	 * Xtensa assembler will mark certain points in the code
33*4882a593Smuzhiyun 	 * as unreachable, so that later assembler or linker relaxation
34*4882a593Smuzhiyun 	 * passes could use them. A spot right after the J instruction
35*4882a593Smuzhiyun 	 * is one such point. Assembler and/or linker may insert padding
36*4882a593Smuzhiyun 	 * or literals here, breaking code flow in case the J instruction
37*4882a593Smuzhiyun 	 * is later replaced with NOP. Put a label right after the J to
38*4882a593Smuzhiyun 	 * make it reachable and wrap both into a no-transform block
39*4882a593Smuzhiyun 	 * to avoid any assembler interference with this.
40*4882a593Smuzhiyun 	 */
41*4882a593Smuzhiyun 	asm_volatile_goto("1:\n\t"
42*4882a593Smuzhiyun 			  ".begin no-transform\n\t"
43*4882a593Smuzhiyun 			  "_j %l[l_yes]\n\t"
44*4882a593Smuzhiyun 			  "2:\n\t"
45*4882a593Smuzhiyun 			  ".end no-transform\n\t"
46*4882a593Smuzhiyun 			  ".pushsection __jump_table,  \"aw\"\n\t"
47*4882a593Smuzhiyun 			  ".word 1b, %l[l_yes], %c0\n\t"
48*4882a593Smuzhiyun 			  ".popsection\n\t"
49*4882a593Smuzhiyun 			  : :  "i" (&((char *)key)[branch]) :  : l_yes);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	return false;
52*4882a593Smuzhiyun l_yes:
53*4882a593Smuzhiyun 	return true;
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun typedef u32 jump_label_t;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun struct jump_entry {
59*4882a593Smuzhiyun 	jump_label_t code;
60*4882a593Smuzhiyun 	jump_label_t target;
61*4882a593Smuzhiyun 	jump_label_t key;
62*4882a593Smuzhiyun };
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun #endif  /* __ASSEMBLY__ */
65*4882a593Smuzhiyun #endif
66