1 #ifndef __ASM_ALTERNATIVE_H
2 #define __ASM_ALTERNATIVE_H
3 
4 #include <asm/cpufeature.h>
5 
6 #ifdef CONFIG_HAS_ALTERNATIVE
7 
8 #ifndef __ASSEMBLY__
9 
10 #include <xen/init.h>
11 #include <xen/types.h>
12 #include <xen/stringify.h>
13 
14 struct alt_instr {
15 	s32 orig_offset;	/* offset to original instruction */
16 	s32 alt_offset;		/* offset to replacement instruction */
17 	u16 cpufeature;		/* cpufeature bit set for replacement */
18 	u8  orig_len;		/* size of original instruction(s) */
19 	u8  alt_len;		/* size of new instruction(s), <= orig_len */
20 };
21 
22 /* Xen: helpers used by common code. */
23 #define __ALT_PTR(a,f)		((u32 *)((void *)&(a)->f + (a)->f))
24 #define ALT_ORIG_PTR(a)		__ALT_PTR(a, orig_offset)
25 #define ALT_REPL_PTR(a)		__ALT_PTR(a, alt_offset)
26 
27 void __init apply_alternatives_all(void);
28 int apply_alternatives(const struct alt_instr *start, const struct alt_instr *end);
29 
30 #define ALTINSTR_ENTRY(feature)						      \
31 	" .word 661b - .\n"				/* label           */ \
32 	" .word 663f - .\n"				/* new instruction */ \
33 	" .hword " __stringify(feature) "\n"		/* feature bit     */ \
34 	" .byte 662b-661b\n"				/* source len      */ \
35 	" .byte 664f-663f\n"				/* replacement len */
36 
37 /*
38  * alternative assembly primitive:
39  *
40  * If any of these .org directive fail, it means that insn1 and insn2
41  * don't have the same length. This used to be written as
42  *
43  * .if ((664b-663b) != (662b-661b))
44  * 	.error "Alternatives instruction length mismatch"
45  * .endif
46  *
47  * but most assemblers die if insn1 or insn2 have a .inst. This should
48  * be fixed in a binutils release posterior to 2.25.51.0.2 (anything
49  * containing commit 4e4d08cf7399b606 or c1baaddf8861).
50  */
51 #define __ALTERNATIVE_CFG(oldinstr, newinstr, feature, cfg_enabled)	\
52 	".if "__stringify(cfg_enabled)" == 1\n"				\
53 	"661:\n\t"							\
54 	oldinstr "\n"							\
55 	"662:\n"							\
56 	".pushsection .altinstructions,\"a\"\n"				\
57 	ALTINSTR_ENTRY(feature)						\
58 	".popsection\n"							\
59 	".pushsection .altinstr_replacement, \"a\"\n"			\
60 	"663:\n\t"							\
61 	newinstr "\n"							\
62 	"664:\n\t"							\
63 	".popsection\n\t"						\
64 	".org	. - (664b-663b) + (662b-661b)\n\t"			\
65 	".org	. - (662b-661b) + (664b-663b)\n"			\
66 	".endif\n"
67 
68 #define _ALTERNATIVE_CFG(oldinstr, newinstr, feature, cfg, ...)	\
69 	__ALTERNATIVE_CFG(oldinstr, newinstr, feature, IS_ENABLED(cfg))
70 
71 #else
72 
73 #include <asm/asm_defns.h>
74 
75 .macro altinstruction_entry orig_offset alt_offset feature orig_len alt_len
76 	.word \orig_offset - .
77 	.word \alt_offset - .
78 	.hword \feature
79 	.byte \orig_len
80 	.byte \alt_len
81 .endm
82 
83 .macro alternative_insn insn1, insn2, cap, enable = 1
84 	.if \enable
85 661:	\insn1
86 662:	.pushsection .altinstructions, "a"
87 	altinstruction_entry 661b, 663f, \cap, 662b-661b, 664f-663f
88 	.popsection
89 	.pushsection .altinstr_replacement, "ax"
90 663:	\insn2
91 664:	.popsection
92 	.org	. - (664b-663b) + (662b-661b)
93 	.org	. - (662b-661b) + (664b-663b)
94 	.endif
95 .endm
96 
97 /*
98  * Begin an alternative code sequence.
99  *
100  * The code that follows this macro will be assembled and linked as
101  * normal. There are no restrictions on this code.
102  */
103 .macro alternative_if_not cap, enable = 1
104 	.if \enable
105 	.pushsection .altinstructions, "a"
106 	altinstruction_entry 661f, 663f, \cap, 662f-661f, 664f-663f
107 	.popsection
108 661:
109 	.endif
110 .endm
111 
112 /*
113  * Provide the alternative code sequence.
114  *
115  * The code that follows this macro is assembled into a special
116  * section to be used for dynamic patching. Code that follows this
117  * macro must:
118  *
119  * 1. Be exactly the same length (in bytes) as the default code
120  *    sequence.
121  *
122  * 2. Not contain a branch target that is used outside of the
123  *    alternative sequence it is defined in (branches into an
124  *    alternative sequence are not fixed up).
125  */
126 .macro alternative_else
127 662:	.pushsection .altinstr_replacement, "ax"
128 663:
129 .endm
130 
131 /*
132  * Complete an alternative code sequence.
133  */
134 .macro alternative_endif
135 664:	.popsection
136 	.org	. - (664b-663b) + (662b-661b)
137 	.org	. - (662b-661b) + (664b-663b)
138 .endm
139 
140 #define _ALTERNATIVE_CFG(insn1, insn2, cap, cfg, ...)	\
141 	alternative_insn insn1, insn2, cap, IS_ENABLED(cfg)
142 
143 #endif  /*  __ASSEMBLY__  */
144 
145 /*
146  * Usage: asm(ALTERNATIVE(oldinstr, newinstr, feature));
147  *
148  * Usage: asm(ALTERNATIVE(oldinstr, newinstr, feature, CONFIG_FOO));
149  * N.B. If CONFIG_FOO is specified, but not selected, the whole block
150  *      will be omitted, including oldinstr.
151  */
152 #define ALTERNATIVE(oldinstr, newinstr, ...)   \
153 	_ALTERNATIVE_CFG(oldinstr, newinstr, __VA_ARGS__, 1)
154 
155 #else /* !CONFIG_HAS_ALTERNATIVE */
156 
157 static inline void apply_alternatives_all(void)
158 {
159 }
160 
161 static inline int apply_alternatives(void *start, size_t length)
162 {
163     return 0;
164 }
165 
166 #endif
167 
168 #endif /* __ASM_ALTERNATIVE_H */
169