xref: /openbmc/linux/arch/x86/include/asm/alternative.h (revision 28dce2c4)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_ALTERNATIVE_H
3 #define _ASM_X86_ALTERNATIVE_H
4 
5 #include <linux/types.h>
6 #include <linux/stringify.h>
7 #include <asm/asm.h>
8 
9 #define ALTINSTR_FLAG_INV	(1 << 15)
10 #define ALT_NOT(feat)		((feat) | ALTINSTR_FLAG_INV)
11 
12 #ifndef __ASSEMBLY__
13 
14 #include <linux/stddef.h>
15 
16 /*
17  * Alternative inline assembly for SMP.
18  *
19  * The LOCK_PREFIX macro defined here replaces the LOCK and
20  * LOCK_PREFIX macros used everywhere in the source tree.
21  *
22  * SMP alternatives use the same data structures as the other
23  * alternatives and the X86_FEATURE_UP flag to indicate the case of a
24  * UP system running a SMP kernel.  The existing apply_alternatives()
25  * works fine for patching a SMP kernel for UP.
26  *
27  * The SMP alternative tables can be kept after boot and contain both
28  * UP and SMP versions of the instructions to allow switching back to
29  * SMP at runtime, when hotplugging in a new CPU, which is especially
30  * useful in virtualized environments.
31  *
32  * The very common lock prefix is handled as special case in a
33  * separate table which is a pure address list without replacement ptr
34  * and size information.  That keeps the table sizes small.
35  */
36 
37 #ifdef CONFIG_SMP
38 #define LOCK_PREFIX_HERE \
39 		".pushsection .smp_locks,\"a\"\n"	\
40 		".balign 4\n"				\
41 		".long 671f - .\n" /* offset */		\
42 		".popsection\n"				\
43 		"671:"
44 
45 #define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
46 
47 #else /* ! CONFIG_SMP */
48 #define LOCK_PREFIX_HERE ""
49 #define LOCK_PREFIX ""
50 #endif
51 
52 /*
53  * objtool annotation to ignore the alternatives and only consider the original
54  * instruction(s).
55  */
56 #define ANNOTATE_IGNORE_ALTERNATIVE				\
57 	"999:\n\t"						\
58 	".pushsection .discard.ignore_alts\n\t"			\
59 	".long 999b - .\n\t"					\
60 	".popsection\n\t"
61 
62 struct alt_instr {
63 	s32 instr_offset;	/* original instruction */
64 	s32 repl_offset;	/* offset to replacement instruction */
65 	u16 cpuid;		/* cpuid bit set for replacement */
66 	u8  instrlen;		/* length of original instruction */
67 	u8  replacementlen;	/* length of new instruction */
68 } __packed;
69 
70 /*
71  * Debug flag that can be tested to see whether alternative
72  * instructions were patched in already:
73  */
74 extern int alternatives_patched;
75 
76 extern void alternative_instructions(void);
77 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
78 
79 struct module;
80 
81 #ifdef CONFIG_SMP
82 extern void alternatives_smp_module_add(struct module *mod, char *name,
83 					void *locks, void *locks_end,
84 					void *text, void *text_end);
85 extern void alternatives_smp_module_del(struct module *mod);
86 extern void alternatives_enable_smp(void);
87 extern int alternatives_text_reserved(void *start, void *end);
88 extern bool skip_smp_alternatives;
89 #else
90 static inline void alternatives_smp_module_add(struct module *mod, char *name,
91 					       void *locks, void *locks_end,
92 					       void *text, void *text_end) {}
93 static inline void alternatives_smp_module_del(struct module *mod) {}
94 static inline void alternatives_enable_smp(void) {}
95 static inline int alternatives_text_reserved(void *start, void *end)
96 {
97 	return 0;
98 }
99 #endif	/* CONFIG_SMP */
100 
101 #define b_replacement(num)	"664"#num
102 #define e_replacement(num)	"665"#num
103 
104 #define alt_end_marker		"663"
105 #define alt_slen		"662b-661b"
106 #define alt_total_slen		alt_end_marker"b-661b"
107 #define alt_rlen(num)		e_replacement(num)"f-"b_replacement(num)"f"
108 
109 #define OLDINSTR(oldinstr, num)						\
110 	"# ALT: oldnstr\n"						\
111 	"661:\n\t" oldinstr "\n662:\n"					\
112 	"# ALT: padding\n"						\
113 	".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * "		\
114 		"((" alt_rlen(num) ")-(" alt_slen ")),0x90\n"		\
115 	alt_end_marker ":\n"
116 
117 /*
118  * gas compatible max based on the idea from:
119  * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
120  *
121  * The additional "-" is needed because gas uses a "true" value of -1.
122  */
123 #define alt_max_short(a, b)	"((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") < (" b ")))))"
124 
125 /*
126  * Pad the second replacement alternative with additional NOPs if it is
127  * additionally longer than the first replacement alternative.
128  */
129 #define OLDINSTR_2(oldinstr, num1, num2) \
130 	"# ALT: oldinstr2\n"									\
131 	"661:\n\t" oldinstr "\n662:\n"								\
132 	"# ALT: padding2\n"									\
133 	".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * "	\
134 		"(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n"	\
135 	alt_end_marker ":\n"
136 
137 #define OLDINSTR_3(oldinsn, n1, n2, n3)								\
138 	"# ALT: oldinstr3\n"									\
139 	"661:\n\t" oldinsn "\n662:\n"								\
140 	"# ALT: padding3\n"									\
141 	".skip -((" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3))	\
142 		" - (" alt_slen ")) > 0) * "							\
143 		"(" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3))	\
144 		" - (" alt_slen ")), 0x90\n"							\
145 	alt_end_marker ":\n"
146 
147 #define ALTINSTR_ENTRY(feature, num)					      \
148 	" .long 661b - .\n"				/* label           */ \
149 	" .long " b_replacement(num)"f - .\n"		/* new instruction */ \
150 	" .word " __stringify(feature) "\n"		/* feature bit     */ \
151 	" .byte " alt_total_slen "\n"			/* source len      */ \
152 	" .byte " alt_rlen(num) "\n"			/* replacement len */
153 
154 #define ALTINSTR_REPLACEMENT(newinstr, num)		/* replacement */	\
155 	"# ALT: replacement " #num "\n"						\
156 	b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n"
157 
158 /* alternative assembly primitive: */
159 #define ALTERNATIVE(oldinstr, newinstr, feature)			\
160 	OLDINSTR(oldinstr, 1)						\
161 	".pushsection .altinstructions,\"a\"\n"				\
162 	ALTINSTR_ENTRY(feature, 1)					\
163 	".popsection\n"							\
164 	".pushsection .altinstr_replacement, \"ax\"\n"			\
165 	ALTINSTR_REPLACEMENT(newinstr, 1)				\
166 	".popsection\n"
167 
168 #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
169 	OLDINSTR_2(oldinstr, 1, 2)					\
170 	".pushsection .altinstructions,\"a\"\n"				\
171 	ALTINSTR_ENTRY(feature1, 1)					\
172 	ALTINSTR_ENTRY(feature2, 2)					\
173 	".popsection\n"							\
174 	".pushsection .altinstr_replacement, \"ax\"\n"			\
175 	ALTINSTR_REPLACEMENT(newinstr1, 1)				\
176 	ALTINSTR_REPLACEMENT(newinstr2, 2)				\
177 	".popsection\n"
178 
179 /* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */
180 #define ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) \
181 	ALTERNATIVE_2(oldinstr, newinstr_no, X86_FEATURE_ALWAYS,	\
182 		      newinstr_yes, feature)
183 
184 #define ALTERNATIVE_3(oldinsn, newinsn1, feat1, newinsn2, feat2, newinsn3, feat3) \
185 	OLDINSTR_3(oldinsn, 1, 2, 3)						\
186 	".pushsection .altinstructions,\"a\"\n"					\
187 	ALTINSTR_ENTRY(feat1, 1)						\
188 	ALTINSTR_ENTRY(feat2, 2)						\
189 	ALTINSTR_ENTRY(feat3, 3)						\
190 	".popsection\n"								\
191 	".pushsection .altinstr_replacement, \"ax\"\n"				\
192 	ALTINSTR_REPLACEMENT(newinsn1, 1)					\
193 	ALTINSTR_REPLACEMENT(newinsn2, 2)					\
194 	ALTINSTR_REPLACEMENT(newinsn3, 3)					\
195 	".popsection\n"
196 
197 /*
198  * Alternative instructions for different CPU types or capabilities.
199  *
200  * This allows to use optimized instructions even on generic binary
201  * kernels.
202  *
203  * length of oldinstr must be longer or equal the length of newinstr
204  * It can be padded with nops as needed.
205  *
206  * For non barrier like inlines please define new variants
207  * without volatile and memory clobber.
208  */
209 #define alternative(oldinstr, newinstr, feature)			\
210 	asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
211 
212 #define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \
213 	asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory")
214 
215 #define alternative_ternary(oldinstr, feature, newinstr_yes, newinstr_no) \
216 	asm_inline volatile(ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) ::: "memory")
217 
218 /*
219  * Alternative inline assembly with input.
220  *
221  * Peculiarities:
222  * No memory clobber here.
223  * Argument numbers start with 1.
224  * Leaving an unused argument 0 to keep API compatibility.
225  */
226 #define alternative_input(oldinstr, newinstr, feature, input...)	\
227 	asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature)	\
228 		: : "i" (0), ## input)
229 
230 /*
231  * This is similar to alternative_input. But it has two features and
232  * respective instructions.
233  *
234  * If CPU has feature2, newinstr2 is used.
235  * Otherwise, if CPU has feature1, newinstr1 is used.
236  * Otherwise, oldinstr is used.
237  */
238 #define alternative_input_2(oldinstr, newinstr1, feature1, newinstr2,	     \
239 			   feature2, input...)				     \
240 	asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1,     \
241 		newinstr2, feature2)					     \
242 		: : "i" (0), ## input)
243 
244 /* Like alternative_input, but with a single output argument */
245 #define alternative_io(oldinstr, newinstr, feature, output, input...)	\
246 	asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature)	\
247 		: output : "i" (0), ## input)
248 
249 /* Like alternative_io, but for replacing a direct call with another one. */
250 #define alternative_call(oldfunc, newfunc, feature, output, input...)	\
251 	asm_inline volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
252 		: output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
253 
254 /*
255  * Like alternative_call, but there are two features and respective functions.
256  * If CPU has feature2, function2 is used.
257  * Otherwise, if CPU has feature1, function1 is used.
258  * Otherwise, old function is used.
259  */
260 #define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2,   \
261 			   output, input...)				      \
262 	asm_inline volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\
263 		"call %P[new2]", feature2)				      \
264 		: output, ASM_CALL_CONSTRAINT				      \
265 		: [old] "i" (oldfunc), [new1] "i" (newfunc1),		      \
266 		  [new2] "i" (newfunc2), ## input)
267 
268 /*
269  * use this macro(s) if you need more than one output parameter
270  * in alternative_io
271  */
272 #define ASM_OUTPUT2(a...) a
273 
274 /*
275  * use this macro if you need clobbers but no inputs in
276  * alternative_{input,io,call}()
277  */
278 #define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
279 
280 #else /* __ASSEMBLY__ */
281 
282 #ifdef CONFIG_SMP
283 	.macro LOCK_PREFIX
284 672:	lock
285 	.pushsection .smp_locks,"a"
286 	.balign 4
287 	.long 672b - .
288 	.popsection
289 	.endm
290 #else
291 	.macro LOCK_PREFIX
292 	.endm
293 #endif
294 
295 /*
296  * objtool annotation to ignore the alternatives and only consider the original
297  * instruction(s).
298  */
299 .macro ANNOTATE_IGNORE_ALTERNATIVE
300 	.Lannotate_\@:
301 	.pushsection .discard.ignore_alts
302 	.long .Lannotate_\@ - .
303 	.popsection
304 .endm
305 
306 /*
307  * Issue one struct alt_instr descriptor entry (need to put it into
308  * the section .altinstructions, see below). This entry contains
309  * enough information for the alternatives patching code to patch an
310  * instruction. See apply_alternatives().
311  */
312 .macro altinstruction_entry orig alt feature orig_len alt_len
313 	.long \orig - .
314 	.long \alt - .
315 	.word \feature
316 	.byte \orig_len
317 	.byte \alt_len
318 .endm
319 
320 /*
321  * Define an alternative between two instructions. If @feature is
322  * present, early code in apply_alternatives() replaces @oldinstr with
323  * @newinstr. ".skip" directive takes care of proper instruction padding
324  * in case @newinstr is longer than @oldinstr.
325  */
326 .macro ALTERNATIVE oldinstr, newinstr, feature
327 140:
328 	\oldinstr
329 141:
330 	.skip -(((144f-143f)-(141b-140b)) > 0) * ((144f-143f)-(141b-140b)),0x90
331 142:
332 
333 	.pushsection .altinstructions,"a"
334 	altinstruction_entry 140b,143f,\feature,142b-140b,144f-143f
335 	.popsection
336 
337 	.pushsection .altinstr_replacement,"ax"
338 143:
339 	\newinstr
340 144:
341 	.popsection
342 .endm
343 
344 #define old_len			141b-140b
345 #define new_len1		144f-143f
346 #define new_len2		145f-144f
347 
348 /*
349  * gas compatible max based on the idea from:
350  * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
351  *
352  * The additional "-" is needed because gas uses a "true" value of -1.
353  */
354 #define alt_max_short(a, b)	((a) ^ (((a) ^ (b)) & -(-((a) < (b)))))
355 
356 
357 /*
358  * Same as ALTERNATIVE macro above but for two alternatives. If CPU
359  * has @feature1, it replaces @oldinstr with @newinstr1. If CPU has
360  * @feature2, it replaces @oldinstr with @feature2.
361  */
362 .macro ALTERNATIVE_2 oldinstr, newinstr1, feature1, newinstr2, feature2
363 140:
364 	\oldinstr
365 141:
366 	.skip -((alt_max_short(new_len1, new_len2) - (old_len)) > 0) * \
367 		(alt_max_short(new_len1, new_len2) - (old_len)),0x90
368 142:
369 
370 	.pushsection .altinstructions,"a"
371 	altinstruction_entry 140b,143f,\feature1,142b-140b,144f-143f
372 	altinstruction_entry 140b,144f,\feature2,142b-140b,145f-144f
373 	.popsection
374 
375 	.pushsection .altinstr_replacement,"ax"
376 143:
377 	\newinstr1
378 144:
379 	\newinstr2
380 145:
381 	.popsection
382 .endm
383 
384 /* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */
385 #define ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) \
386 	ALTERNATIVE_2 oldinstr, newinstr_no, X86_FEATURE_ALWAYS,	\
387 	newinstr_yes, feature
388 
389 #endif /* __ASSEMBLY__ */
390 
391 #endif /* _ASM_X86_ALTERNATIVE_H */
392