xref: /linux/arch/x86/include/asm/alternative.h (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
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 extern void apply_retpolines(s32 *start, s32 *end);
79 
80 struct module;
81 
82 #ifdef CONFIG_SMP
83 extern void alternatives_smp_module_add(struct module *mod, char *name,
84 					void *locks, void *locks_end,
85 					void *text, void *text_end);
86 extern void alternatives_smp_module_del(struct module *mod);
87 extern void alternatives_enable_smp(void);
88 extern int alternatives_text_reserved(void *start, void *end);
89 extern bool skip_smp_alternatives;
90 #else
91 static inline void alternatives_smp_module_add(struct module *mod, char *name,
92 					       void *locks, void *locks_end,
93 					       void *text, void *text_end) {}
94 static inline void alternatives_smp_module_del(struct module *mod) {}
95 static inline void alternatives_enable_smp(void) {}
96 static inline int alternatives_text_reserved(void *start, void *end)
97 {
98 	return 0;
99 }
100 #endif	/* CONFIG_SMP */
101 
102 #define b_replacement(num)	"664"#num
103 #define e_replacement(num)	"665"#num
104 
105 #define alt_end_marker		"663"
106 #define alt_slen		"662b-661b"
107 #define alt_total_slen		alt_end_marker"b-661b"
108 #define alt_rlen(num)		e_replacement(num)"f-"b_replacement(num)"f"
109 
110 #define OLDINSTR(oldinstr, num)						\
111 	"# ALT: oldnstr\n"						\
112 	"661:\n\t" oldinstr "\n662:\n"					\
113 	"# ALT: padding\n"						\
114 	".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * "		\
115 		"((" alt_rlen(num) ")-(" alt_slen ")),0x90\n"		\
116 	alt_end_marker ":\n"
117 
118 /*
119  * gas compatible max based on the idea from:
120  * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
121  *
122  * The additional "-" is needed because gas uses a "true" value of -1.
123  */
124 #define alt_max_short(a, b)	"((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") < (" b ")))))"
125 
126 /*
127  * Pad the second replacement alternative with additional NOPs if it is
128  * additionally longer than the first replacement alternative.
129  */
130 #define OLDINSTR_2(oldinstr, num1, num2) \
131 	"# ALT: oldinstr2\n"									\
132 	"661:\n\t" oldinstr "\n662:\n"								\
133 	"# ALT: padding2\n"									\
134 	".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * "	\
135 		"(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n"	\
136 	alt_end_marker ":\n"
137 
138 #define OLDINSTR_3(oldinsn, n1, n2, n3)								\
139 	"# ALT: oldinstr3\n"									\
140 	"661:\n\t" oldinsn "\n662:\n"								\
141 	"# ALT: padding3\n"									\
142 	".skip -((" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3))	\
143 		" - (" alt_slen ")) > 0) * "							\
144 		"(" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3))	\
145 		" - (" alt_slen ")), 0x90\n"							\
146 	alt_end_marker ":\n"
147 
148 #define ALTINSTR_ENTRY(feature, num)					      \
149 	" .long 661b - .\n"				/* label           */ \
150 	" .long " b_replacement(num)"f - .\n"		/* new instruction */ \
151 	" .word " __stringify(feature) "\n"		/* feature bit     */ \
152 	" .byte " alt_total_slen "\n"			/* source len      */ \
153 	" .byte " alt_rlen(num) "\n"			/* replacement len */
154 
155 #define ALTINSTR_REPLACEMENT(newinstr, num)		/* replacement */	\
156 	"# ALT: replacement " #num "\n"						\
157 	b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n"
158 
159 /* alternative assembly primitive: */
160 #define ALTERNATIVE(oldinstr, newinstr, feature)			\
161 	OLDINSTR(oldinstr, 1)						\
162 	".pushsection .altinstructions,\"a\"\n"				\
163 	ALTINSTR_ENTRY(feature, 1)					\
164 	".popsection\n"							\
165 	".pushsection .altinstr_replacement, \"ax\"\n"			\
166 	ALTINSTR_REPLACEMENT(newinstr, 1)				\
167 	".popsection\n"
168 
169 #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
170 	OLDINSTR_2(oldinstr, 1, 2)					\
171 	".pushsection .altinstructions,\"a\"\n"				\
172 	ALTINSTR_ENTRY(feature1, 1)					\
173 	ALTINSTR_ENTRY(feature2, 2)					\
174 	".popsection\n"							\
175 	".pushsection .altinstr_replacement, \"ax\"\n"			\
176 	ALTINSTR_REPLACEMENT(newinstr1, 1)				\
177 	ALTINSTR_REPLACEMENT(newinstr2, 2)				\
178 	".popsection\n"
179 
180 /* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */
181 #define ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) \
182 	ALTERNATIVE_2(oldinstr, newinstr_no, X86_FEATURE_ALWAYS,	\
183 		      newinstr_yes, feature)
184 
185 #define ALTERNATIVE_3(oldinsn, newinsn1, feat1, newinsn2, feat2, newinsn3, feat3) \
186 	OLDINSTR_3(oldinsn, 1, 2, 3)						\
187 	".pushsection .altinstructions,\"a\"\n"					\
188 	ALTINSTR_ENTRY(feat1, 1)						\
189 	ALTINSTR_ENTRY(feat2, 2)						\
190 	ALTINSTR_ENTRY(feat3, 3)						\
191 	".popsection\n"								\
192 	".pushsection .altinstr_replacement, \"ax\"\n"				\
193 	ALTINSTR_REPLACEMENT(newinsn1, 1)					\
194 	ALTINSTR_REPLACEMENT(newinsn2, 2)					\
195 	ALTINSTR_REPLACEMENT(newinsn3, 3)					\
196 	".popsection\n"
197 
198 /*
199  * Alternative instructions for different CPU types or capabilities.
200  *
201  * This allows to use optimized instructions even on generic binary
202  * kernels.
203  *
204  * length of oldinstr must be longer or equal the length of newinstr
205  * It can be padded with nops as needed.
206  *
207  * For non barrier like inlines please define new variants
208  * without volatile and memory clobber.
209  */
210 #define alternative(oldinstr, newinstr, feature)			\
211 	asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
212 
213 #define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \
214 	asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory")
215 
216 #define alternative_ternary(oldinstr, feature, newinstr_yes, newinstr_no) \
217 	asm_inline volatile(ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) ::: "memory")
218 
219 /*
220  * Alternative inline assembly with input.
221  *
222  * Peculiarities:
223  * No memory clobber here.
224  * Argument numbers start with 1.
225  * Leaving an unused argument 0 to keep API compatibility.
226  */
227 #define alternative_input(oldinstr, newinstr, feature, input...)	\
228 	asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature)	\
229 		: : "i" (0), ## input)
230 
231 /*
232  * This is similar to alternative_input. But it has two features and
233  * respective instructions.
234  *
235  * If CPU has feature2, newinstr2 is used.
236  * Otherwise, if CPU has feature1, newinstr1 is used.
237  * Otherwise, oldinstr is used.
238  */
239 #define alternative_input_2(oldinstr, newinstr1, feature1, newinstr2,	     \
240 			   feature2, input...)				     \
241 	asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1,     \
242 		newinstr2, feature2)					     \
243 		: : "i" (0), ## input)
244 
245 /* Like alternative_input, but with a single output argument */
246 #define alternative_io(oldinstr, newinstr, feature, output, input...)	\
247 	asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature)	\
248 		: output : "i" (0), ## input)
249 
250 /* Like alternative_io, but for replacing a direct call with another one. */
251 #define alternative_call(oldfunc, newfunc, feature, output, input...)	\
252 	asm_inline volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
253 		: output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
254 
255 /*
256  * Like alternative_call, but there are two features and respective functions.
257  * If CPU has feature2, function2 is used.
258  * Otherwise, if CPU has feature1, function1 is used.
259  * Otherwise, old function is used.
260  */
261 #define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2,   \
262 			   output, input...)				      \
263 	asm_inline volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\
264 		"call %P[new2]", feature2)				      \
265 		: output, ASM_CALL_CONSTRAINT				      \
266 		: [old] "i" (oldfunc), [new1] "i" (newfunc1),		      \
267 		  [new2] "i" (newfunc2), ## input)
268 
269 /*
270  * use this macro(s) if you need more than one output parameter
271  * in alternative_io
272  */
273 #define ASM_OUTPUT2(a...) a
274 
275 /*
276  * use this macro if you need clobbers but no inputs in
277  * alternative_{input,io,call}()
278  */
279 #define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
280 
281 #else /* __ASSEMBLY__ */
282 
283 #ifdef CONFIG_SMP
284 	.macro LOCK_PREFIX
285 672:	lock
286 	.pushsection .smp_locks,"a"
287 	.balign 4
288 	.long 672b - .
289 	.popsection
290 	.endm
291 #else
292 	.macro LOCK_PREFIX
293 	.endm
294 #endif
295 
296 /*
297  * objtool annotation to ignore the alternatives and only consider the original
298  * instruction(s).
299  */
300 .macro ANNOTATE_IGNORE_ALTERNATIVE
301 	.Lannotate_\@:
302 	.pushsection .discard.ignore_alts
303 	.long .Lannotate_\@ - .
304 	.popsection
305 .endm
306 
307 /*
308  * Issue one struct alt_instr descriptor entry (need to put it into
309  * the section .altinstructions, see below). This entry contains
310  * enough information for the alternatives patching code to patch an
311  * instruction. See apply_alternatives().
312  */
313 .macro altinstruction_entry orig alt feature orig_len alt_len
314 	.long \orig - .
315 	.long \alt - .
316 	.word \feature
317 	.byte \orig_len
318 	.byte \alt_len
319 .endm
320 
321 /*
322  * Define an alternative between two instructions. If @feature is
323  * present, early code in apply_alternatives() replaces @oldinstr with
324  * @newinstr. ".skip" directive takes care of proper instruction padding
325  * in case @newinstr is longer than @oldinstr.
326  */
327 .macro ALTERNATIVE oldinstr, newinstr, feature
328 140:
329 	\oldinstr
330 141:
331 	.skip -(((144f-143f)-(141b-140b)) > 0) * ((144f-143f)-(141b-140b)),0x90
332 142:
333 
334 	.pushsection .altinstructions,"a"
335 	altinstruction_entry 140b,143f,\feature,142b-140b,144f-143f
336 	.popsection
337 
338 	.pushsection .altinstr_replacement,"ax"
339 143:
340 	\newinstr
341 144:
342 	.popsection
343 .endm
344 
345 #define old_len			141b-140b
346 #define new_len1		144f-143f
347 #define new_len2		145f-144f
348 
349 /*
350  * gas compatible max based on the idea from:
351  * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
352  *
353  * The additional "-" is needed because gas uses a "true" value of -1.
354  */
355 #define alt_max_short(a, b)	((a) ^ (((a) ^ (b)) & -(-((a) < (b)))))
356 
357 
358 /*
359  * Same as ALTERNATIVE macro above but for two alternatives. If CPU
360  * has @feature1, it replaces @oldinstr with @newinstr1. If CPU has
361  * @feature2, it replaces @oldinstr with @feature2.
362  */
363 .macro ALTERNATIVE_2 oldinstr, newinstr1, feature1, newinstr2, feature2
364 140:
365 	\oldinstr
366 141:
367 	.skip -((alt_max_short(new_len1, new_len2) - (old_len)) > 0) * \
368 		(alt_max_short(new_len1, new_len2) - (old_len)),0x90
369 142:
370 
371 	.pushsection .altinstructions,"a"
372 	altinstruction_entry 140b,143f,\feature1,142b-140b,144f-143f
373 	altinstruction_entry 140b,144f,\feature2,142b-140b,145f-144f
374 	.popsection
375 
376 	.pushsection .altinstr_replacement,"ax"
377 143:
378 	\newinstr1
379 144:
380 	\newinstr2
381 145:
382 	.popsection
383 .endm
384 
385 /* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */
386 #define ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) \
387 	ALTERNATIVE_2 oldinstr, newinstr_no, X86_FEATURE_ALWAYS,	\
388 	newinstr_yes, feature
389 
390 #endif /* __ASSEMBLY__ */
391 
392 #endif /* _ASM_X86_ALTERNATIVE_H */
393