1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_ALTERNATIVE_H
3 #define _ASM_X86_ALTERNATIVE_H
5 #include <linux/types.h>
6 #include <linux/stringify.h>
9 #define ALTINSTR_FLAG_INV (1 << 15)
10 #define ALT_NOT(feat) ((feat) | ALTINSTR_FLAG_INV)
14 #include <linux/stddef.h>
17 * Alternative inline assembly for SMP.
19 * The LOCK_PREFIX macro defined here replaces the LOCK and
20 * LOCK_PREFIX macros used everywhere in the source tree.
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.
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.
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.
38 #define LOCK_PREFIX_HERE \
39 ".pushsection .smp_locks,\"a\"\n" \
41 ".long 671f - .\n" /* offset */ \
45 #define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
47 #else /* ! CONFIG_SMP */
48 #define LOCK_PREFIX_HERE ""
49 #define LOCK_PREFIX ""
53 * objtool annotation to ignore the alternatives and only consider the original
56 #define ANNOTATE_IGNORE_ALTERNATIVE \
58 ".pushsection .discard.ignore_alts\n\t" \
59 ".long 999b - .\n\t" \
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 */
71 * Debug flag that can be tested to see whether alternative
72 * instructions were patched in already:
74 extern int alternatives_patched;
76 extern void alternative_instructions(void);
77 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
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;
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)
99 #endif /* CONFIG_SMP */
101 #define b_replacement(num) "664"#num
102 #define e_replacement(num) "665"#num
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"
109 #define OLDINSTR(oldinstr, num) \
111 "661:\n\t" oldinstr "\n662:\n" \
113 ".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * " \
114 "((" alt_rlen(num) ")-(" alt_slen ")),0x90\n" \
118 * gas compatible max based on the idea from:
119 * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
121 * The additional "-" is needed because gas uses a "true" value of -1.
123 #define alt_max_short(a, b) "((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") < (" b ")))))"
126 * Pad the second replacement alternative with additional NOPs if it is
127 * additionally longer than the first replacement alternative.
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" \
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" \
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 */
154 #define ALTINSTR_REPLACEMENT(newinstr, num) /* replacement */ \
155 "# ALT: replacement " #num "\n" \
156 b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n"
158 /* alternative assembly primitive: */
159 #define ALTERNATIVE(oldinstr, newinstr, feature) \
160 OLDINSTR(oldinstr, 1) \
161 ".pushsection .altinstructions,\"a\"\n" \
162 ALTINSTR_ENTRY(feature, 1) \
164 ".pushsection .altinstr_replacement, \"ax\"\n" \
165 ALTINSTR_REPLACEMENT(newinstr, 1) \
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) \
174 ".pushsection .altinstr_replacement, \"ax\"\n" \
175 ALTINSTR_REPLACEMENT(newinstr1, 1) \
176 ALTINSTR_REPLACEMENT(newinstr2, 2) \
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)
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) \
191 ".pushsection .altinstr_replacement, \"ax\"\n" \
192 ALTINSTR_REPLACEMENT(newinsn1, 1) \
193 ALTINSTR_REPLACEMENT(newinsn2, 2) \
194 ALTINSTR_REPLACEMENT(newinsn3, 3) \
198 * Alternative instructions for different CPU types or capabilities.
200 * This allows to use optimized instructions even on generic binary
203 * length of oldinstr must be longer or equal the length of newinstr
204 * It can be padded with nops as needed.
206 * For non barrier like inlines please define new variants
207 * without volatile and memory clobber.
209 #define alternative(oldinstr, newinstr, feature) \
210 asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
212 #define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \
213 asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory")
215 #define alternative_ternary(oldinstr, feature, newinstr_yes, newinstr_no) \
216 asm_inline volatile(ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) ::: "memory")
219 * Alternative inline assembly with input.
222 * No memory clobber here.
223 * Argument numbers start with 1.
224 * Leaving an unused argument 0 to keep API compatibility.
226 #define alternative_input(oldinstr, newinstr, feature, input...) \
227 asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
228 : : "i" (0), ## input)
231 * This is similar to alternative_input. But it has two features and
232 * respective instructions.
234 * If CPU has feature2, newinstr2 is used.
235 * Otherwise, if CPU has feature1, newinstr1 is used.
236 * Otherwise, oldinstr is used.
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)
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)
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)
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.
260 #define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2, \
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)
269 * use this macro(s) if you need more than one output parameter
272 #define ASM_OUTPUT2(a...) a
275 * use this macro if you need clobbers but no inputs in
276 * alternative_{input,io,call}()
278 #define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
280 #else /* __ASSEMBLY__ */
285 .pushsection .smp_locks,"a"
296 * objtool annotation to ignore the alternatives and only consider the original
299 .macro ANNOTATE_IGNORE_ALTERNATIVE
301 .pushsection .discard.ignore_alts
302 .long .Lannotate_\@ - .
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().
312 .macro altinstruction_entry orig alt feature orig_len alt_len
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.
326 .macro ALTERNATIVE oldinstr, newinstr, feature
330 .skip -(((144f-143f)-(141b-140b)) > 0) * ((144f-143f)-(141b-140b)),0x90
333 .pushsection .altinstructions,"a"
334 altinstruction_entry 140b,143f,\feature,142b-140b,144f-143f
337 .pushsection .altinstr_replacement,"ax"
344 #define old_len 141b-140b
345 #define new_len1 144f-143f
346 #define new_len2 145f-144f
349 * gas compatible max based on the idea from:
350 * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
352 * The additional "-" is needed because gas uses a "true" value of -1.
354 #define alt_max_short(a, b) ((a) ^ (((a) ^ (b)) & -(-((a) < (b)))))
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.
362 .macro ALTERNATIVE_2 oldinstr, newinstr1, feature1, newinstr2, feature2
366 .skip -((alt_max_short(new_len1, new_len2) - (old_len)) > 0) * \
367 (alt_max_short(new_len1, new_len2) - (old_len)),0x90
370 .pushsection .altinstructions,"a"
371 altinstruction_entry 140b,143f,\feature1,142b-140b,144f-143f
372 altinstruction_entry 140b,144f,\feature2,142b-140b,145f-144f
375 .pushsection .altinstr_replacement,"ax"
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
389 #endif /* __ASSEMBLY__ */
391 #endif /* _ASM_X86_ALTERNATIVE_H */