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