blob: 6665000a03dff7a07d73eeaad05171835ec12998 [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_ALTERNATIVE_H
2#define _ASM_X86_ALTERNATIVE_H
H. Peter Anvin6b592572008-01-30 13:30:30 +01003
Andy Lutomirskif005f5d2016-04-26 12:23:25 -07004#ifndef __ASSEMBLY__
5
H. Peter Anvin6b592572008-01-30 13:30:30 +01006#include <linux/types.h>
7#include <linux/stddef.h>
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -04008#include <linux/stringify.h>
H. Peter Anvin6b592572008-01-30 13:30:30 +01009#include <asm/asm.h>
10
11/*
12 * Alternative inline assembly for SMP.
13 *
14 * The LOCK_PREFIX macro defined here replaces the LOCK and
15 * LOCK_PREFIX macros used everywhere in the source tree.
16 *
17 * SMP alternatives use the same data structures as the other
18 * alternatives and the X86_FEATURE_UP flag to indicate the case of a
19 * UP system running a SMP kernel. The existing apply_alternatives()
20 * works fine for patching a SMP kernel for UP.
21 *
22 * The SMP alternative tables can be kept after boot and contain both
23 * UP and SMP versions of the instructions to allow switching back to
24 * SMP at runtime, when hotplugging in a new CPU, which is especially
25 * useful in virtualized environments.
26 *
27 * The very common lock prefix is handled as special case in a
28 * separate table which is a pure address list without replacement ptr
29 * and size information. That keeps the table sizes small.
30 */
31
32#ifdef CONFIG_SMP
Luca Barbierib3ac8912010-02-24 10:54:22 +010033#define LOCK_PREFIX_HERE \
H. Peter Anvin9cebed42012-09-21 12:43:08 -070034 ".pushsection .smp_locks,\"a\"\n" \
35 ".balign 4\n" \
36 ".long 671f - .\n" /* offset */ \
37 ".popsection\n" \
Luca Barbierib3ac8912010-02-24 10:54:22 +010038 "671:"
39
40#define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
H. Peter Anvin6b592572008-01-30 13:30:30 +010041
42#else /* ! CONFIG_SMP */
H. Peter Anvinb701a472010-04-29 16:03:57 -070043#define LOCK_PREFIX_HERE ""
H. Peter Anvin6b592572008-01-30 13:30:30 +010044#define LOCK_PREFIX ""
Thomas Gleixner96a388d2007-10-11 11:20:03 +020045#endif
H. Peter Anvin6b592572008-01-30 13:30:30 +010046
H. Peter Anvin6b592572008-01-30 13:30:30 +010047struct alt_instr {
Andy Lutomirski59e97e42011-07-13 09:24:10 -040048 s32 instr_offset; /* original instruction */
49 s32 repl_offset; /* offset to replacement instruction */
H. Peter Anvin83a7a2a2010-06-10 00:10:43 +000050 u16 cpuid; /* cpuid bit set for replacement */
H. Peter Anvin6b592572008-01-30 13:30:30 +010051 u8 instrlen; /* length of original instruction */
Borislav Petkov43321952014-12-27 10:41:52 +010052 u8 replacementlen; /* length of new instruction */
53 u8 padlen; /* length of build-time padding */
54} __packed;
H. Peter Anvin6b592572008-01-30 13:30:30 +010055
Ingo Molnar5e907bb2015-04-30 09:09:26 +020056/*
57 * Debug flag that can be tested to see whether alternative
58 * instructions were patched in already:
59 */
60extern int alternatives_patched;
61
H. Peter Anvin6b592572008-01-30 13:30:30 +010062extern void alternative_instructions(void);
63extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
64
65struct module;
66
67#ifdef CONFIG_SMP
68extern void alternatives_smp_module_add(struct module *mod, char *name,
69 void *locks, void *locks_end,
70 void *text, void *text_end);
71extern void alternatives_smp_module_del(struct module *mod);
Rusty Russell816afe42012-08-06 17:29:49 +093072extern void alternatives_enable_smp(void);
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -050073extern int alternatives_text_reserved(void *start, void *end);
Suresh Siddha3fb82d52010-11-23 16:11:40 -080074extern bool skip_smp_alternatives;
H. Peter Anvin6b592572008-01-30 13:30:30 +010075#else
76static inline void alternatives_smp_module_add(struct module *mod, char *name,
Joe Perches2ac1ea72008-03-23 01:01:37 -070077 void *locks, void *locks_end,
78 void *text, void *text_end) {}
H. Peter Anvin6b592572008-01-30 13:30:30 +010079static inline void alternatives_smp_module_del(struct module *mod) {}
Rusty Russell816afe42012-08-06 17:29:49 +093080static inline void alternatives_enable_smp(void) {}
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -050081static inline int alternatives_text_reserved(void *start, void *end)
82{
83 return 0;
84}
H. Peter Anvin6b592572008-01-30 13:30:30 +010085#endif /* CONFIG_SMP */
86
Borislav Petkov43321952014-12-27 10:41:52 +010087#define b_replacement(num) "664"#num
88#define e_replacement(num) "665"#num
Fenghua Yu954e4822012-05-24 18:19:45 -070089
Borislav Petkov43321952014-12-27 10:41:52 +010090#define alt_end_marker "663"
91#define alt_slen "662b-661b"
92#define alt_pad_len alt_end_marker"b-662b"
93#define alt_total_slen alt_end_marker"b-661b"
94#define alt_rlen(num) e_replacement(num)"f-"b_replacement(num)"f"
Fenghua Yu954e4822012-05-24 18:19:45 -070095
Borislav Petkov43321952014-12-27 10:41:52 +010096#define __OLDINSTR(oldinstr, num) \
97 "661:\n\t" oldinstr "\n662:\n" \
98 ".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * " \
99 "((" alt_rlen(num) ")-(" alt_slen ")),0x90\n"
Fenghua Yu954e4822012-05-24 18:19:45 -0700100
Borislav Petkov43321952014-12-27 10:41:52 +0100101#define OLDINSTR(oldinstr, num) \
102 __OLDINSTR(oldinstr, num) \
103 alt_end_marker ":\n"
104
105/*
Mathias Krausefb6da442017-10-05 20:30:12 +0200106 * gas compatible max based on the idea from:
Borislav Petkovdbe40582015-04-04 15:34:43 +0200107 * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
108 *
Mathias Krausefb6da442017-10-05 20:30:12 +0200109 * The additional "-" is needed because gas uses a "true" value of -1.
Borislav Petkovdbe40582015-04-04 15:34:43 +0200110 */
Mathias Krausefb6da442017-10-05 20:30:12 +0200111#define alt_max_short(a, b) "((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") < (" b ")))))"
Borislav Petkovdbe40582015-04-04 15:34:43 +0200112
113/*
Borislav Petkov43321952014-12-27 10:41:52 +0100114 * Pad the second replacement alternative with additional NOPs if it is
115 * additionally longer than the first replacement alternative.
116 */
Borislav Petkovdbe40582015-04-04 15:34:43 +0200117#define OLDINSTR_2(oldinstr, num1, num2) \
118 "661:\n\t" oldinstr "\n662:\n" \
119 ".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * " \
120 "(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n" \
Borislav Petkov43321952014-12-27 10:41:52 +0100121 alt_end_marker ":\n"
122
123#define ALTINSTR_ENTRY(feature, num) \
Fenghua Yu954e4822012-05-24 18:19:45 -0700124 " .long 661b - .\n" /* label */ \
Borislav Petkov43321952014-12-27 10:41:52 +0100125 " .long " b_replacement(num)"f - .\n" /* new instruction */ \
Fenghua Yu954e4822012-05-24 18:19:45 -0700126 " .word " __stringify(feature) "\n" /* feature bit */ \
Borislav Petkov43321952014-12-27 10:41:52 +0100127 " .byte " alt_total_slen "\n" /* source len */ \
128 " .byte " alt_rlen(num) "\n" /* replacement len */ \
129 " .byte " alt_pad_len "\n" /* pad len */
Fenghua Yu954e4822012-05-24 18:19:45 -0700130
Borislav Petkov43321952014-12-27 10:41:52 +0100131#define ALTINSTR_REPLACEMENT(newinstr, feature, num) /* replacement */ \
132 b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n\t"
Fenghua Yu954e4822012-05-24 18:19:45 -0700133
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400134/* alternative assembly primitive: */
135#define ALTERNATIVE(oldinstr, newinstr, feature) \
Borislav Petkov43321952014-12-27 10:41:52 +0100136 OLDINSTR(oldinstr, 1) \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700137 ".pushsection .altinstructions,\"a\"\n" \
Fenghua Yu954e4822012-05-24 18:19:45 -0700138 ALTINSTR_ENTRY(feature, 1) \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700139 ".popsection\n" \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700140 ".pushsection .altinstr_replacement, \"ax\"\n" \
Fenghua Yu954e4822012-05-24 18:19:45 -0700141 ALTINSTR_REPLACEMENT(newinstr, feature, 1) \
David Woodhouse91b7e5c2018-01-04 14:37:05 +0000142 ".popsection\n"
Fenghua Yu954e4822012-05-24 18:19:45 -0700143
144#define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
Borislav Petkov43321952014-12-27 10:41:52 +0100145 OLDINSTR_2(oldinstr, 1, 2) \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700146 ".pushsection .altinstructions,\"a\"\n" \
Fenghua Yu954e4822012-05-24 18:19:45 -0700147 ALTINSTR_ENTRY(feature1, 1) \
148 ALTINSTR_ENTRY(feature2, 2) \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700149 ".popsection\n" \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700150 ".pushsection .altinstr_replacement, \"ax\"\n" \
Fenghua Yu954e4822012-05-24 18:19:45 -0700151 ALTINSTR_REPLACEMENT(newinstr1, feature1, 1) \
152 ALTINSTR_REPLACEMENT(newinstr2, feature2, 2) \
David Woodhouse91b7e5c2018-01-04 14:37:05 +0000153 ".popsection\n"
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400154
H. Peter Anvin6b592572008-01-30 13:30:30 +0100155/*
156 * Alternative instructions for different CPU types or capabilities.
157 *
158 * This allows to use optimized instructions even on generic binary
159 * kernels.
160 *
161 * length of oldinstr must be longer or equal the length of newinstr
162 * It can be padded with nops as needed.
163 *
164 * For non barrier like inlines please define new variants
165 * without volatile and memory clobber.
166 */
167#define alternative(oldinstr, newinstr, feature) \
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400168 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
H. Peter Anvin6b592572008-01-30 13:30:30 +0100169
Borislav Petkov43321952014-12-27 10:41:52 +0100170#define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \
171 asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory")
172
H. Peter Anvin6b592572008-01-30 13:30:30 +0100173/*
174 * Alternative inline assembly with input.
175 *
176 * Pecularities:
177 * No memory clobber here.
178 * Argument numbers start with 1.
179 * Best is to use constraints that are fixed size (like (%1) ... "r")
180 * If you use variable sized constraints like "m" or "g" in the
181 * replacement make sure to pad to the worst case length.
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400182 * Leaving an unused argument 0 to keep API compatibility.
H. Peter Anvin6b592572008-01-30 13:30:30 +0100183 */
184#define alternative_input(oldinstr, newinstr, feature, input...) \
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400185 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
186 : : "i" (0), ## input)
H. Peter Anvin6b592572008-01-30 13:30:30 +0100187
Fenghua Yu5b3e83f2014-05-29 11:12:32 -0700188/*
189 * This is similar to alternative_input. But it has two features and
190 * respective instructions.
191 *
192 * If CPU has feature2, newinstr2 is used.
193 * Otherwise, if CPU has feature1, newinstr1 is used.
194 * Otherwise, oldinstr is used.
195 */
196#define alternative_input_2(oldinstr, newinstr1, feature1, newinstr2, \
197 feature2, input...) \
198 asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, \
199 newinstr2, feature2) \
200 : : "i" (0), ## input)
201
H. Peter Anvin6b592572008-01-30 13:30:30 +0100202/* Like alternative_input, but with a single output argument */
203#define alternative_io(oldinstr, newinstr, feature, output, input...) \
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400204 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
205 : output : "i" (0), ## input)
H. Peter Anvin6b592572008-01-30 13:30:30 +0100206
Jan Beulich1b1d9252009-12-18 16:12:56 +0000207/* Like alternative_io, but for replacing a direct call with another one. */
208#define alternative_call(oldfunc, newfunc, feature, output, input...) \
209 asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
210 : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
211
H. Peter Anvin6b592572008-01-30 13:30:30 +0100212/*
Fenghua Yu954e4822012-05-24 18:19:45 -0700213 * Like alternative_call, but there are two features and respective functions.
214 * If CPU has feature2, function2 is used.
215 * Otherwise, if CPU has feature1, function1 is used.
216 * Otherwise, old function is used.
217 */
218#define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2, \
219 output, input...) \
Josh Poimboeuf317c2ce2016-09-23 16:49:39 -0500220{ \
Fenghua Yu954e4822012-05-24 18:19:45 -0700221 asm volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\
222 "call %P[new2]", feature2) \
Josh Poimboeufd5ea93e2017-09-20 16:24:33 -0500223 : output, ASM_CALL_CONSTRAINT \
Josh Poimboeuf317c2ce2016-09-23 16:49:39 -0500224 : [old] "i" (oldfunc), [new1] "i" (newfunc1), \
225 [new2] "i" (newfunc2), ## input); \
226}
Fenghua Yu954e4822012-05-24 18:19:45 -0700227
228/*
H. Peter Anvin6b592572008-01-30 13:30:30 +0100229 * use this macro(s) if you need more than one output parameter
230 * in alternative_io
231 */
Jan Beulich1b1d9252009-12-18 16:12:56 +0000232#define ASM_OUTPUT2(a...) a
H. Peter Anvin6b592572008-01-30 13:30:30 +0100233
Jan Beulich819165f2012-01-20 16:21:41 +0000234/*
235 * use this macro if you need clobbers but no inputs in
236 * alternative_{input,io,call}()
237 */
238#define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
239
Andy Lutomirskif005f5d2016-04-26 12:23:25 -0700240#endif /* __ASSEMBLY__ */
241
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700242#endif /* _ASM_X86_ALTERNATIVE_H */