1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_ALTERNATIVE_H
3 #define _ASM_X86_ALTERNATIVE_H
7 #include <linux/types.h>
8 #include <linux/stddef.h>
9 #include <linux/stringify.h>
13 * Alternative inline assembly for SMP.
15 * The LOCK_PREFIX macro defined here replaces the LOCK and
16 * LOCK_PREFIX macros used everywhere in the source tree.
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.
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.
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.
34 #define LOCK_PREFIX_HERE \
35 ".pushsection .smp_locks,\"a\"\n" \
37 ".long 671f - .\n" /* offset */ \
41 #define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
43 #else /* ! CONFIG_SMP */
44 #define LOCK_PREFIX_HERE ""
45 #define LOCK_PREFIX ""
49 s32 instr_offset; /* original instruction */
50 s32 repl_offset; /* offset to replacement instruction */
51 u16 cpuid; /* cpuid bit set for replacement */
52 u8 instrlen; /* length of original instruction */
53 u8 replacementlen; /* length of new instruction */
54 u8 padlen; /* length of build-time padding */
58 * Debug flag that can be tested to see whether alternative
59 * instructions were patched in already:
61 extern int alternatives_patched;
63 extern void alternative_instructions(void);
64 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
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);
73 extern void alternatives_enable_smp(void);
74 extern int alternatives_text_reserved(void *start, void *end);
75 extern bool skip_smp_alternatives;
77 static inline void alternatives_smp_module_add(struct module *mod, char *name,
78 void *locks, void *locks_end,
79 void *text, void *text_end) {}
80 static inline void alternatives_smp_module_del(struct module *mod) {}
81 static inline void alternatives_enable_smp(void) {}
82 static inline int alternatives_text_reserved(void *start, void *end)
86 #endif /* CONFIG_SMP */
88 #define b_replacement(num) "664"#num
89 #define e_replacement(num) "665"#num
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"
97 #define OLDINSTR(oldinstr, num) \
99 "661:\n\t" oldinstr "\n662:\n" \
101 ".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * " \
102 "((" alt_rlen(num) ")-(" alt_slen ")),0x90\n" \
106 * gas compatible max based on the idea from:
107 * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
109 * The additional "-" is needed because gas uses a "true" value of -1.
111 #define alt_max_short(a, b) "((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") < (" b ")))))"
114 * Pad the second replacement alternative with additional NOPs if it is
115 * additionally longer than the first replacement alternative.
117 #define OLDINSTR_2(oldinstr, num1, num2) \
118 "# ALT: oldinstr2\n" \
119 "661:\n\t" oldinstr "\n662:\n" \
120 "# ALT: padding2\n" \
121 ".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * " \
122 "(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n" \
125 #define OLDINSTR_3(oldinsn, n1, n2, n3) \
126 "# ALT: oldinstr3\n" \
127 "661:\n\t" oldinsn "\n662:\n" \
128 "# ALT: padding3\n" \
129 ".skip -((" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3)) \
130 " - (" alt_slen ")) > 0) * " \
131 "(" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3)) \
132 " - (" alt_slen ")), 0x90\n" \
135 #define ALTINSTR_ENTRY(feature, num) \
136 " .long 661b - .\n" /* label */ \
137 " .long " b_replacement(num)"f - .\n" /* new instruction */ \
138 " .word " __stringify(feature) "\n" /* feature bit */ \
139 " .byte " alt_total_slen "\n" /* source len */ \
140 " .byte " alt_rlen(num) "\n" /* replacement len */ \
141 " .byte " alt_pad_len "\n" /* pad len */
143 #define ALTINSTR_REPLACEMENT(newinstr, feature, num) /* replacement */ \
144 "# ALT: replacement " #num "\n" \
145 b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n"
147 /* alternative assembly primitive: */
148 #define ALTERNATIVE(oldinstr, newinstr, feature) \
149 OLDINSTR(oldinstr, 1) \
150 ".pushsection .altinstructions,\"a\"\n" \
151 ALTINSTR_ENTRY(feature, 1) \
153 ".pushsection .altinstr_replacement, \"ax\"\n" \
154 ALTINSTR_REPLACEMENT(newinstr, feature, 1) \
157 #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
158 OLDINSTR_2(oldinstr, 1, 2) \
159 ".pushsection .altinstructions,\"a\"\n" \
160 ALTINSTR_ENTRY(feature1, 1) \
161 ALTINSTR_ENTRY(feature2, 2) \
163 ".pushsection .altinstr_replacement, \"ax\"\n" \
164 ALTINSTR_REPLACEMENT(newinstr1, feature1, 1) \
165 ALTINSTR_REPLACEMENT(newinstr2, feature2, 2) \
168 #define ALTERNATIVE_3(oldinsn, newinsn1, feat1, newinsn2, feat2, newinsn3, feat3) \
169 OLDINSTR_3(oldinsn, 1, 2, 3) \
170 ".pushsection .altinstructions,\"a\"\n" \
171 ALTINSTR_ENTRY(feat1, 1) \
172 ALTINSTR_ENTRY(feat2, 2) \
173 ALTINSTR_ENTRY(feat3, 3) \
175 ".pushsection .altinstr_replacement, \"ax\"\n" \
176 ALTINSTR_REPLACEMENT(newinsn1, feat1, 1) \
177 ALTINSTR_REPLACEMENT(newinsn2, feat2, 2) \
178 ALTINSTR_REPLACEMENT(newinsn3, feat3, 3) \
182 * Alternative instructions for different CPU types or capabilities.
184 * This allows to use optimized instructions even on generic binary
187 * length of oldinstr must be longer or equal the length of newinstr
188 * It can be padded with nops as needed.
190 * For non barrier like inlines please define new variants
191 * without volatile and memory clobber.
193 #define alternative(oldinstr, newinstr, feature) \
194 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
196 #define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \
197 asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory")
200 * Alternative inline assembly with input.
203 * No memory clobber here.
204 * Argument numbers start with 1.
205 * Best is to use constraints that are fixed size (like (%1) ... "r")
206 * If you use variable sized constraints like "m" or "g" in the
207 * replacement make sure to pad to the worst case length.
208 * Leaving an unused argument 0 to keep API compatibility.
210 #define alternative_input(oldinstr, newinstr, feature, input...) \
211 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
212 : : "i" (0), ## input)
215 * This is similar to alternative_input. But it has two features and
216 * respective instructions.
218 * If CPU has feature2, newinstr2 is used.
219 * Otherwise, if CPU has feature1, newinstr1 is used.
220 * Otherwise, oldinstr is used.
222 #define alternative_input_2(oldinstr, newinstr1, feature1, newinstr2, \
223 feature2, input...) \
224 asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, \
225 newinstr2, feature2) \
226 : : "i" (0), ## input)
228 /* Like alternative_input, but with a single output argument */
229 #define alternative_io(oldinstr, newinstr, feature, output, input...) \
230 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
231 : output : "i" (0), ## input)
233 /* Like alternative_io, but for replacing a direct call with another one. */
234 #define alternative_call(oldfunc, newfunc, feature, output, input...) \
235 asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
236 : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
239 * Like alternative_call, but there are two features and respective functions.
240 * If CPU has feature2, function2 is used.
241 * Otherwise, if CPU has feature1, function1 is used.
242 * Otherwise, old function is used.
244 #define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2, \
246 asm volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\
247 "call %P[new2]", feature2) \
248 : output, ASM_CALL_CONSTRAINT \
249 : [old] "i" (oldfunc), [new1] "i" (newfunc1), \
250 [new2] "i" (newfunc2), ## input)
253 * use this macro(s) if you need more than one output parameter
256 #define ASM_OUTPUT2(a...) a
259 * use this macro if you need clobbers but no inputs in
260 * alternative_{input,io,call}()
262 #define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
264 #endif /* __ASSEMBLY__ */
266 #endif /* _ASM_X86_ALTERNATIVE_H */