1
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
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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" \
42 ".popsection\n" \
43 "671:"
44
45#define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
46
47#else
48#define LOCK_PREFIX_HERE ""
49#define LOCK_PREFIX ""
50#endif
51
52
53
54
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
62struct alt_instr {
63 s32 instr_offset;
64 s32 repl_offset;
65 u16 cpuid;
66 u8 instrlen;
67 u8 replacementlen;
68} __packed;
69
70
71
72
73
74extern int alternatives_patched;
75
76extern void alternative_instructions(void);
77extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
78
79struct module;
80
81#ifdef CONFIG_SMP
82extern void alternatives_smp_module_add(struct module *mod, char *name,
83 void *locks, void *locks_end,
84 void *text, void *text_end);
85extern void alternatives_smp_module_del(struct module *mod);
86extern void alternatives_enable_smp(void);
87extern int alternatives_text_reserved(void *start, void *end);
88extern bool skip_smp_alternatives;
89#else
90static inline void alternatives_smp_module_add(struct module *mod, char *name,
91 void *locks, void *locks_end,
92 void *text, void *text_end) {}
93static inline void alternatives_smp_module_del(struct module *mod) {}
94static inline void alternatives_enable_smp(void) {}
95static inline int alternatives_text_reserved(void *start, void *end)
96{
97 return 0;
98}
99#endif
100
101#define b_replacement(num) "664"#num
102#define e_replacement(num) "665"#num
103
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"
108
109#define OLDINSTR(oldinstr, num) \
110 "# ALT: oldnstr\n" \
111 "661:\n\t" oldinstr "\n662:\n" \
112 "# ALT: padding\n" \
113 ".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * " \
114 "((" alt_rlen(num) ")-(" alt_slen ")),0x90\n" \
115 alt_end_marker ":\n"
116
117
118
119
120
121
122
123#define alt_max_short(a, b) "((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") < (" b ")))))"
124
125
126
127
128
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" \
135 alt_end_marker ":\n"
136
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" \
145 alt_end_marker ":\n"
146
147#define ALTINSTR_ENTRY(feature, num) \
148 " .long 661b - .\n" \
149 " .long " b_replacement(num)"f - .\n" \
150 " .word " __stringify(feature) "\n" \
151 " .byte " alt_total_slen "\n" \
152 " .byte " alt_rlen(num) "\n"
153
154#define ALTINSTR_REPLACEMENT(newinstr, num) \
155 "# ALT: replacement " #num "\n" \
156 b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n"
157
158
159#define ALTERNATIVE(oldinstr, newinstr, feature) \
160 OLDINSTR(oldinstr, 1) \
161 ".pushsection .altinstructions,\"a\"\n" \
162 ALTINSTR_ENTRY(feature, 1) \
163 ".popsection\n" \
164 ".pushsection .altinstr_replacement, \"ax\"\n" \
165 ALTINSTR_REPLACEMENT(newinstr, 1) \
166 ".popsection\n"
167
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) \
173 ".popsection\n" \
174 ".pushsection .altinstr_replacement, \"ax\"\n" \
175 ALTINSTR_REPLACEMENT(newinstr1, 1) \
176 ALTINSTR_REPLACEMENT(newinstr2, 2) \
177 ".popsection\n"
178
179
180#define ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) \
181 ALTERNATIVE_2(oldinstr, newinstr_no, X86_FEATURE_ALWAYS, \
182 newinstr_yes, feature)
183
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) \
190 ".popsection\n" \
191 ".pushsection .altinstr_replacement, \"ax\"\n" \
192 ALTINSTR_REPLACEMENT(newinsn1, 1) \
193 ALTINSTR_REPLACEMENT(newinsn2, 2) \
194 ALTINSTR_REPLACEMENT(newinsn3, 3) \
195 ".popsection\n"
196
197
198
199
200
201
202
203
204
205
206
207
208
209#define alternative(oldinstr, newinstr, feature) \
210 asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
211
212#define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \
213 asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory")
214
215#define alternative_ternary(oldinstr, feature, newinstr_yes, newinstr_no) \
216 asm_inline volatile(ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) ::: "memory")
217
218
219
220
221
222
223
224
225
226#define alternative_input(oldinstr, newinstr, feature, input...) \
227 asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
228 : : "i" (0), ## input)
229
230
231
232
233
234
235
236
237
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)
243
244
245#define alternative_io(oldinstr, newinstr, feature, output, input...) \
246 asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
247 : output : "i" (0), ## input)
248
249
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)
253
254
255
256
257
258
259
260#define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2, \
261 output, input...) \
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)
267
268
269
270
271
272#define ASM_OUTPUT2(a...) a
273
274
275
276
277
278#define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
279
280#else
281
282#ifdef CONFIG_SMP
283 .macro LOCK_PREFIX
284672: lock
285 .pushsection .smp_locks,"a"
286 .balign 4
287 .long 672b - .
288 .popsection
289 .endm
290#else
291 .macro LOCK_PREFIX
292 .endm
293#endif
294
295
296
297
298
299.macro ANNOTATE_IGNORE_ALTERNATIVE
300 .Lannotate_\@:
301 .pushsection .discard.ignore_alts
302 .long .Lannotate_\@ - .
303 .popsection
304.endm
305
306
307
308
309
310
311
312.macro altinstruction_entry orig alt feature orig_len alt_len
313 .long \orig - .
314 .long \alt - .
315 .word \feature
316 .byte \orig_len
317 .byte \alt_len
318.endm
319
320
321
322
323
324
325
326.macro ALTERNATIVE oldinstr, newinstr, feature
327140:
328 \oldinstr
329141:
330 .skip -(((144f-143f)-(141b-140b)) > 0) * ((144f-143f)-(141b-140b)),0x90
331142:
332
333 .pushsection .altinstructions,"a"
334 altinstruction_entry 140b,143f,\feature,142b-140b,144f-143f
335 .popsection
336
337 .pushsection .altinstr_replacement,"ax"
338143:
339 \newinstr
340144:
341 .popsection
342.endm
343
344#define old_len 141b-140b
345#define new_len1 144f-143f
346#define new_len2 145f-144f
347
348
349
350
351
352
353
354#define alt_max_short(a, b) ((a) ^ (((a) ^ (b)) & -(-((a) < (b)))))
355
356
357
358
359
360
361
362.macro ALTERNATIVE_2 oldinstr, newinstr1, feature1, newinstr2, feature2
363140:
364 \oldinstr
365141:
366 .skip -((alt_max_short(new_len1, new_len2) - (old_len)) > 0) * \
367 (alt_max_short(new_len1, new_len2) - (old_len)),0x90
368142:
369
370 .pushsection .altinstructions,"a"
371 altinstruction_entry 140b,143f,\feature1,142b-140b,144f-143f
372 altinstruction_entry 140b,144f,\feature2,142b-140b,145f-144f
373 .popsection
374
375 .pushsection .altinstr_replacement,"ax"
376143:
377 \newinstr1
378144:
379 \newinstr2
380145:
381 .popsection
382.endm
383
384
385#define ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) \
386 ALTERNATIVE_2 oldinstr, newinstr_no, X86_FEATURE_ALWAYS, \
387 newinstr_yes, feature
388
389#endif
390
391#endif
392