]>
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 | 4 | |
f005f5d8 AL |
5 | #ifndef __ASSEMBLY__ |
6 | ||
6b592570 PA |
7 | #include <linux/types.h> |
8 | #include <linux/stddef.h> | |
edc953fa | 9 | #include <linux/stringify.h> |
6b592570 PA |
10 | #include <asm/asm.h> |
11 | ||
12 | /* | |
13 | * Alternative inline assembly for SMP. | |
14 | * | |
15 | * The LOCK_PREFIX macro defined here replaces the LOCK and | |
16 | * LOCK_PREFIX macros used everywhere in the source tree. | |
17 | * | |
18 | * SMP alternatives use the same data structures as the other | |
19 | * alternatives and the X86_FEATURE_UP flag to indicate the case of a | |
20 | * UP system running a SMP kernel. The existing apply_alternatives() | |
21 | * works fine for patching a SMP kernel for UP. | |
22 | * | |
23 | * The SMP alternative tables can be kept after boot and contain both | |
24 | * UP and SMP versions of the instructions to allow switching back to | |
25 | * SMP at runtime, when hotplugging in a new CPU, which is especially | |
26 | * useful in virtualized environments. | |
27 | * | |
28 | * The very common lock prefix is handled as special case in a | |
29 | * separate table which is a pure address list without replacement ptr | |
30 | * and size information. That keeps the table sizes small. | |
31 | */ | |
32 | ||
33 | #ifdef CONFIG_SMP | |
b3ac891b | 34 | #define LOCK_PREFIX_HERE \ |
9cebed42 PA |
35 | ".pushsection .smp_locks,\"a\"\n" \ |
36 | ".balign 4\n" \ | |
37 | ".long 671f - .\n" /* offset */ \ | |
38 | ".popsection\n" \ | |
b3ac891b LB |
39 | "671:" |
40 | ||
41 | #define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; " | |
6b592570 PA |
42 | |
43 | #else /* ! CONFIG_SMP */ | |
b701a47b | 44 | #define LOCK_PREFIX_HERE "" |
6b592570 PA |
45 | #define LOCK_PREFIX "" |
46 | #endif | |
47 | ||
6b592570 | 48 | struct alt_instr { |
59e97e4d AL |
49 | s32 instr_offset; /* original instruction */ |
50 | s32 repl_offset; /* offset to replacement instruction */ | |
83a7a2ad | 51 | u16 cpuid; /* cpuid bit set for replacement */ |
6b592570 | 52 | u8 instrlen; /* length of original instruction */ |
4332195c BP |
53 | u8 replacementlen; /* length of new instruction */ |
54 | u8 padlen; /* length of build-time padding */ | |
55 | } __packed; | |
6b592570 | 56 | |
5e907bb0 IM |
57 | /* |
58 | * Debug flag that can be tested to see whether alternative | |
59 | * instructions were patched in already: | |
60 | */ | |
61 | extern int alternatives_patched; | |
62 | ||
6b592570 PA |
63 | extern void alternative_instructions(void); |
64 | extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end); | |
65 | ||
66 | struct module; | |
67 | ||
68 | #ifdef CONFIG_SMP | |
69 | extern void alternatives_smp_module_add(struct module *mod, char *name, | |
70 | void *locks, void *locks_end, | |
71 | void *text, void *text_end); | |
72 | extern void alternatives_smp_module_del(struct module *mod); | |
816afe4f | 73 | extern void alternatives_enable_smp(void); |
2cfa1978 | 74 | extern int alternatives_text_reserved(void *start, void *end); |
3fb82d56 | 75 | extern bool skip_smp_alternatives; |
6b592570 PA |
76 | #else |
77 | static inline void alternatives_smp_module_add(struct module *mod, char *name, | |
2ac1ea7c JP |
78 | void *locks, void *locks_end, |
79 | void *text, void *text_end) {} | |
6b592570 | 80 | static inline void alternatives_smp_module_del(struct module *mod) {} |
816afe4f | 81 | static inline void alternatives_enable_smp(void) {} |
2cfa1978 MH |
82 | static inline int alternatives_text_reserved(void *start, void *end) |
83 | { | |
84 | return 0; | |
85 | } | |
6b592570 PA |
86 | #endif /* CONFIG_SMP */ |
87 | ||
4332195c BP |
88 | #define b_replacement(num) "664"#num |
89 | #define e_replacement(num) "665"#num | |
954e482b | 90 | |
4332195c BP |
91 | #define alt_end_marker "663" |
92 | #define alt_slen "662b-661b" | |
93 | #define alt_pad_len alt_end_marker"b-662b" | |
94 | #define alt_total_slen alt_end_marker"b-661b" | |
95 | #define alt_rlen(num) e_replacement(num)"f-"b_replacement(num)"f" | |
954e482b | 96 | |
4332195c BP |
97 | #define __OLDINSTR(oldinstr, num) \ |
98 | "661:\n\t" oldinstr "\n662:\n" \ | |
99 | ".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * " \ | |
100 | "((" alt_rlen(num) ")-(" alt_slen ")),0x90\n" | |
954e482b | 101 | |
4332195c BP |
102 | #define OLDINSTR(oldinstr, num) \ |
103 | __OLDINSTR(oldinstr, num) \ | |
104 | alt_end_marker ":\n" | |
105 | ||
dbe4058a | 106 | /* |
6b32c126 | 107 | * gas compatible max based on the idea from: |
dbe4058a BP |
108 | * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax |
109 | * | |
6b32c126 | 110 | * The additional "-" is needed because gas uses a "true" value of -1. |
dbe4058a | 111 | */ |
6b32c126 | 112 | #define alt_max_short(a, b) "((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") < (" b ")))))" |
dbe4058a | 113 | |
4332195c BP |
114 | /* |
115 | * Pad the second replacement alternative with additional NOPs if it is | |
116 | * additionally longer than the first replacement alternative. | |
117 | */ | |
dbe4058a BP |
118 | #define OLDINSTR_2(oldinstr, num1, num2) \ |
119 | "661:\n\t" oldinstr "\n662:\n" \ | |
120 | ".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * " \ | |
121 | "(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n" \ | |
4332195c BP |
122 | alt_end_marker ":\n" |
123 | ||
124 | #define ALTINSTR_ENTRY(feature, num) \ | |
954e482b | 125 | " .long 661b - .\n" /* label */ \ |
4332195c | 126 | " .long " b_replacement(num)"f - .\n" /* new instruction */ \ |
954e482b | 127 | " .word " __stringify(feature) "\n" /* feature bit */ \ |
4332195c BP |
128 | " .byte " alt_total_slen "\n" /* source len */ \ |
129 | " .byte " alt_rlen(num) "\n" /* replacement len */ \ | |
130 | " .byte " alt_pad_len "\n" /* pad len */ | |
954e482b | 131 | |
4332195c BP |
132 | #define ALTINSTR_REPLACEMENT(newinstr, feature, num) /* replacement */ \ |
133 | b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n\t" | |
954e482b | 134 | |
edc953fa MD |
135 | /* alternative assembly primitive: */ |
136 | #define ALTERNATIVE(oldinstr, newinstr, feature) \ | |
4332195c | 137 | OLDINSTR(oldinstr, 1) \ |
9cebed42 | 138 | ".pushsection .altinstructions,\"a\"\n" \ |
954e482b | 139 | ALTINSTR_ENTRY(feature, 1) \ |
9cebed42 | 140 | ".popsection\n" \ |
9cebed42 | 141 | ".pushsection .altinstr_replacement, \"ax\"\n" \ |
954e482b | 142 | ALTINSTR_REPLACEMENT(newinstr, feature, 1) \ |
b9e705ef | 143 | ".popsection\n" |
954e482b FY |
144 | |
145 | #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\ | |
4332195c | 146 | OLDINSTR_2(oldinstr, 1, 2) \ |
9cebed42 | 147 | ".pushsection .altinstructions,\"a\"\n" \ |
954e482b FY |
148 | ALTINSTR_ENTRY(feature1, 1) \ |
149 | ALTINSTR_ENTRY(feature2, 2) \ | |
9cebed42 | 150 | ".popsection\n" \ |
9cebed42 | 151 | ".pushsection .altinstr_replacement, \"ax\"\n" \ |
954e482b FY |
152 | ALTINSTR_REPLACEMENT(newinstr1, feature1, 1) \ |
153 | ALTINSTR_REPLACEMENT(newinstr2, feature2, 2) \ | |
b9e705ef | 154 | ".popsection\n" |
edc953fa | 155 | |
6b592570 PA |
156 | /* |
157 | * Alternative instructions for different CPU types or capabilities. | |
158 | * | |
159 | * This allows to use optimized instructions even on generic binary | |
160 | * kernels. | |
161 | * | |
162 | * length of oldinstr must be longer or equal the length of newinstr | |
163 | * It can be padded with nops as needed. | |
164 | * | |
165 | * For non barrier like inlines please define new variants | |
166 | * without volatile and memory clobber. | |
167 | */ | |
168 | #define alternative(oldinstr, newinstr, feature) \ | |
edc953fa | 169 | asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory") |
6b592570 | 170 | |
4332195c BP |
171 | #define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \ |
172 | asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory") | |
173 | ||
6b592570 PA |
174 | /* |
175 | * Alternative inline assembly with input. | |
176 | * | |
177 | * Pecularities: | |
178 | * No memory clobber here. | |
179 | * Argument numbers start with 1. | |
180 | * Best is to use constraints that are fixed size (like (%1) ... "r") | |
181 | * If you use variable sized constraints like "m" or "g" in the | |
182 | * replacement make sure to pad to the worst case length. | |
edc953fa | 183 | * Leaving an unused argument 0 to keep API compatibility. |
6b592570 PA |
184 | */ |
185 | #define alternative_input(oldinstr, newinstr, feature, input...) \ | |
edc953fa MD |
186 | asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \ |
187 | : : "i" (0), ## input) | |
6b592570 | 188 | |
5b3e83f4 FY |
189 | /* |
190 | * This is similar to alternative_input. But it has two features and | |
191 | * respective instructions. | |
192 | * | |
193 | * If CPU has feature2, newinstr2 is used. | |
194 | * Otherwise, if CPU has feature1, newinstr1 is used. | |
195 | * Otherwise, oldinstr is used. | |
196 | */ | |
197 | #define alternative_input_2(oldinstr, newinstr1, feature1, newinstr2, \ | |
198 | feature2, input...) \ | |
199 | asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, \ | |
200 | newinstr2, feature2) \ | |
201 | : : "i" (0), ## input) | |
202 | ||
6b592570 PA |
203 | /* Like alternative_input, but with a single output argument */ |
204 | #define alternative_io(oldinstr, newinstr, feature, output, input...) \ | |
edc953fa MD |
205 | asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \ |
206 | : output : "i" (0), ## input) | |
6b592570 | 207 | |
1b1d9258 JB |
208 | /* Like alternative_io, but for replacing a direct call with another one. */ |
209 | #define alternative_call(oldfunc, newfunc, feature, output, input...) \ | |
210 | asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \ | |
211 | : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input) | |
212 | ||
954e482b FY |
213 | /* |
214 | * Like alternative_call, but there are two features and respective functions. | |
215 | * If CPU has feature2, function2 is used. | |
216 | * Otherwise, if CPU has feature1, function1 is used. | |
217 | * Otherwise, old function is used. | |
218 | */ | |
219 | #define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2, \ | |
220 | output, input...) \ | |
221 | asm volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\ | |
222 | "call %P[new2]", feature2) \ | |
f5caf621 | 223 | : output, ASM_CALL_CONSTRAINT \ |
317c2ce7 | 224 | : [old] "i" (oldfunc), [new1] "i" (newfunc1), \ |
39e0211e | 225 | [new2] "i" (newfunc2), ## input) |
954e482b | 226 | |
6b592570 PA |
227 | /* |
228 | * use this macro(s) if you need more than one output parameter | |
229 | * in alternative_io | |
230 | */ | |
1b1d9258 | 231 | #define ASM_OUTPUT2(a...) a |
6b592570 | 232 | |
819165fb JB |
233 | /* |
234 | * use this macro if you need clobbers but no inputs in | |
235 | * alternative_{input,io,call}() | |
236 | */ | |
237 | #define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr | |
238 | ||
f005f5d8 AL |
239 | #endif /* __ASSEMBLY__ */ |
240 | ||
1965aae3 | 241 | #endif /* _ASM_X86_ALTERNATIVE_H */ |