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