1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21965aae3SH. Peter Anvin #ifndef _ASM_X86_ALTERNATIVE_H
31965aae3SH. Peter Anvin #define _ASM_X86_ALTERNATIVE_H
4bb898558SAl Viro
5bb898558SAl Viro #include <linux/types.h>
6edc953faSMathieu Desnoyers #include <linux/stringify.h>
7bb898558SAl Viro #include <asm/asm.h>
8bb898558SAl Viro
95d1dd961SBorislav Petkov (AMD) #define ALT_FLAGS_SHIFT 16
105d1dd961SBorislav Petkov (AMD)
11c22ef568SWilly Tarreau #define ALT_FLAG_NOT (1 << 0)
125d1dd961SBorislav Petkov (AMD) #define ALT_NOT(feature) ((ALT_FLAG_NOT << ALT_FLAGS_SHIFT) | (feature))
13dda7bb76SJuergen Gross
145e21a3ecSJuergen Gross #ifndef __ASSEMBLY__
155e21a3ecSJuergen Gross
165e21a3ecSJuergen Gross #include <linux/stddef.h>
175e21a3ecSJuergen Gross
18bb898558SAl Viro /*
19bb898558SAl Viro * Alternative inline assembly for SMP.
20bb898558SAl Viro *
21bb898558SAl Viro * The LOCK_PREFIX macro defined here replaces the LOCK and
22bb898558SAl Viro * LOCK_PREFIX macros used everywhere in the source tree.
23bb898558SAl Viro *
24bb898558SAl Viro * SMP alternatives use the same data structures as the other
25bb898558SAl Viro * alternatives and the X86_FEATURE_UP flag to indicate the case of a
26bb898558SAl Viro * UP system running a SMP kernel. The existing apply_alternatives()
27bb898558SAl Viro * works fine for patching a SMP kernel for UP.
28bb898558SAl Viro *
29bb898558SAl Viro * The SMP alternative tables can be kept after boot and contain both
30bb898558SAl Viro * UP and SMP versions of the instructions to allow switching back to
31bb898558SAl Viro * SMP at runtime, when hotplugging in a new CPU, which is especially
32bb898558SAl Viro * useful in virtualized environments.
33bb898558SAl Viro *
34bb898558SAl Viro * The very common lock prefix is handled as special case in a
35bb898558SAl Viro * separate table which is a pure address list without replacement ptr
36bb898558SAl Viro * and size information. That keeps the table sizes small.
37bb898558SAl Viro */
38bb898558SAl Viro
39bb898558SAl Viro #ifdef CONFIG_SMP
40851a4cd7SIngo Molnar #define LOCK_PREFIX_HERE \
41851a4cd7SIngo Molnar ".pushsection .smp_locks,\"a\"\n" \
42851a4cd7SIngo Molnar ".balign 4\n" \
43851a4cd7SIngo Molnar ".long 671f - .\n" /* offset */ \
44851a4cd7SIngo Molnar ".popsection\n" \
45851a4cd7SIngo Molnar "671:"
46851a4cd7SIngo Molnar
47851a4cd7SIngo Molnar #define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
48851a4cd7SIngo Molnar
49bb898558SAl Viro #else /* ! CONFIG_SMP */
50b701a47bSH. Peter Anvin #define LOCK_PREFIX_HERE ""
51bb898558SAl Viro #define LOCK_PREFIX ""
52bb898558SAl Viro #endif
53bb898558SAl Viro
54ff05ab23SPeter Zijlstra /*
55ff05ab23SPeter Zijlstra * objtool annotation to ignore the alternatives and only consider the original
56ff05ab23SPeter Zijlstra * instruction(s).
57ff05ab23SPeter Zijlstra */
58ff05ab23SPeter Zijlstra #define ANNOTATE_IGNORE_ALTERNATIVE \
59ff05ab23SPeter Zijlstra "999:\n\t" \
60ff05ab23SPeter Zijlstra ".pushsection .discard.ignore_alts\n\t" \
6106f61af8SFangrui Song ".long 999b\n\t" \
62ff05ab23SPeter Zijlstra ".popsection\n\t"
63ff05ab23SPeter Zijlstra
645d1dd961SBorislav Petkov (AMD) /*
655d1dd961SBorislav Petkov (AMD) * The patching flags are part of the upper bits of the @ft_flags parameter when
665d1dd961SBorislav Petkov (AMD) * specifying them. The split is currently like this:
675d1dd961SBorislav Petkov (AMD) *
685d1dd961SBorislav Petkov (AMD) * [31... flags ...16][15... CPUID feature bit ...0]
695d1dd961SBorislav Petkov (AMD) *
705d1dd961SBorislav Petkov (AMD) * but since this is all hidden in the macros argument being split, those fields can be
715d1dd961SBorislav Petkov (AMD) * extended in the future to fit in a u64 or however the need arises.
725d1dd961SBorislav Petkov (AMD) */
73bb898558SAl Viro struct alt_instr {
7459e97e4dSAndy Lutomirski s32 instr_offset; /* original instruction */
7559e97e4dSAndy Lutomirski s32 repl_offset; /* offset to replacement instruction */
765d1dd961SBorislav Petkov (AMD)
775d1dd961SBorislav Petkov (AMD) union {
785d1dd961SBorislav Petkov (AMD) struct {
795d1dd961SBorislav Petkov (AMD) u32 cpuid: 16; /* CPUID bit set for replacement */
805d1dd961SBorislav Petkov (AMD) u32 flags: 16; /* patching control flags */
815d1dd961SBorislav Petkov (AMD) };
825d1dd961SBorislav Petkov (AMD) u32 ft_flags;
835d1dd961SBorislav Petkov (AMD) };
845d1dd961SBorislav Petkov (AMD)
85bb898558SAl Viro u8 instrlen; /* length of original instruction */
864332195cSBorislav Petkov u8 replacementlen; /* length of new instruction */
874332195cSBorislav Petkov } __packed;
88bb898558SAl Viro
895e907bb0SIngo Molnar /*
905e907bb0SIngo Molnar * Debug flag that can be tested to see whether alternative
915e907bb0SIngo Molnar * instructions were patched in already:
925e907bb0SIngo Molnar */
935e907bb0SIngo Molnar extern int alternatives_patched;
945e907bb0SIngo Molnar
95bb898558SAl Viro extern void alternative_instructions(void);
96bb898558SAl Viro extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
9775085009SPeter Zijlstra extern void apply_retpolines(s32 *start, s32 *end);
9815e67227SPeter Zijlstra extern void apply_returns(s32 *start, s32 *end);
99be0fffa5SPeter Zijlstra extern void apply_seal_endbr(s32 *start, s32 *end);
100931ab636SPeter Zijlstra extern void apply_fineibt(s32 *start_retpoline, s32 *end_retpoine,
101931ab636SPeter Zijlstra s32 *start_cfi, s32 *end_cfi);
102bb898558SAl Viro
103bb898558SAl Viro struct module;
104e81dc127SThomas Gleixner struct paravirt_patch_site;
105e81dc127SThomas Gleixner
106e81dc127SThomas Gleixner struct callthunk_sites {
107e81dc127SThomas Gleixner s32 *call_start, *call_end;
108e81dc127SThomas Gleixner struct paravirt_patch_site *pv_start, *pv_end;
109e81dc127SThomas Gleixner };
110e81dc127SThomas Gleixner
111e81dc127SThomas Gleixner #ifdef CONFIG_CALL_THUNKS
112e81dc127SThomas Gleixner extern void callthunks_patch_builtin_calls(void);
113eaf44c81SThomas Gleixner extern void callthunks_patch_module_calls(struct callthunk_sites *sites,
114eaf44c81SThomas Gleixner struct module *mod);
1157825451fSPeter Zijlstra extern void *callthunks_translate_call_dest(void *dest);
116b2e9dfe5SThomas Gleixner extern int x86_call_depth_emit_accounting(u8 **pprog, void *func);
117e81dc127SThomas Gleixner #else
callthunks_patch_builtin_calls(void)118e81dc127SThomas Gleixner static __always_inline void callthunks_patch_builtin_calls(void) {}
119eaf44c81SThomas Gleixner static __always_inline void
callthunks_patch_module_calls(struct callthunk_sites * sites,struct module * mod)120eaf44c81SThomas Gleixner callthunks_patch_module_calls(struct callthunk_sites *sites,
121eaf44c81SThomas Gleixner struct module *mod) {}
callthunks_translate_call_dest(void * dest)1227825451fSPeter Zijlstra static __always_inline void *callthunks_translate_call_dest(void *dest)
1237825451fSPeter Zijlstra {
1247825451fSPeter Zijlstra return dest;
1257825451fSPeter Zijlstra }
x86_call_depth_emit_accounting(u8 ** pprog,void * func)126b2e9dfe5SThomas Gleixner static __always_inline int x86_call_depth_emit_accounting(u8 **pprog,
127b2e9dfe5SThomas Gleixner void *func)
128b2e9dfe5SThomas Gleixner {
129b2e9dfe5SThomas Gleixner return 0;
130b2e9dfe5SThomas Gleixner }
131e81dc127SThomas Gleixner #endif
132bb898558SAl Viro
133bb898558SAl Viro #ifdef CONFIG_SMP
134bb898558SAl Viro extern void alternatives_smp_module_add(struct module *mod, char *name,
135bb898558SAl Viro void *locks, void *locks_end,
136bb898558SAl Viro void *text, void *text_end);
137bb898558SAl Viro extern void alternatives_smp_module_del(struct module *mod);
138816afe4fSRusty Russell extern void alternatives_enable_smp(void);
1392cfa1978SMasami Hiramatsu extern int alternatives_text_reserved(void *start, void *end);
1403fb82d56SSuresh Siddha extern bool skip_smp_alternatives;
141bb898558SAl Viro #else
alternatives_smp_module_add(struct module * mod,char * name,void * locks,void * locks_end,void * text,void * text_end)142bb898558SAl Viro static inline void alternatives_smp_module_add(struct module *mod, char *name,
143bb898558SAl Viro void *locks, void *locks_end,
144bb898558SAl Viro void *text, void *text_end) {}
alternatives_smp_module_del(struct module * mod)145bb898558SAl Viro static inline void alternatives_smp_module_del(struct module *mod) {}
alternatives_enable_smp(void)146816afe4fSRusty Russell static inline void alternatives_enable_smp(void) {}
alternatives_text_reserved(void * start,void * end)1472cfa1978SMasami Hiramatsu static inline int alternatives_text_reserved(void *start, void *end)
1482cfa1978SMasami Hiramatsu {
1492cfa1978SMasami Hiramatsu return 0;
1502cfa1978SMasami Hiramatsu }
151bb898558SAl Viro #endif /* CONFIG_SMP */
152bb898558SAl Viro
1534332195cSBorislav Petkov #define b_replacement(num) "664"#num
1544332195cSBorislav Petkov #define e_replacement(num) "665"#num
155954e482bSFenghua Yu
1564332195cSBorislav Petkov #define alt_end_marker "663"
157954e482bSFenghua Yu #define alt_slen "662b-661b"
1584332195cSBorislav Petkov #define alt_total_slen alt_end_marker"b-661b"
1594332195cSBorislav Petkov #define alt_rlen(num) e_replacement(num)"f-"b_replacement(num)"f"
160954e482bSFenghua Yu
1614332195cSBorislav Petkov #define OLDINSTR(oldinstr, num) \
1621c1ed473SBorislav Petkov "# ALT: oldnstr\n" \
1631c1ed473SBorislav Petkov "661:\n\t" oldinstr "\n662:\n" \
1641c1ed473SBorislav Petkov "# ALT: padding\n" \
1651c1ed473SBorislav Petkov ".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * " \
1661c1ed473SBorislav Petkov "((" alt_rlen(num) ")-(" alt_slen ")),0x90\n" \
1674332195cSBorislav Petkov alt_end_marker ":\n"
1684332195cSBorislav Petkov
1694332195cSBorislav Petkov /*
1706b32c126SMathias Krause * gas compatible max based on the idea from:
171dbe4058aSBorislav Petkov * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
172dbe4058aSBorislav Petkov *
1736b32c126SMathias Krause * The additional "-" is needed because gas uses a "true" value of -1.
174dbe4058aSBorislav Petkov */
1756b32c126SMathias Krause #define alt_max_short(a, b) "((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") < (" b ")))))"
176dbe4058aSBorislav Petkov
177dbe4058aSBorislav Petkov /*
1784332195cSBorislav Petkov * Pad the second replacement alternative with additional NOPs if it is
1794332195cSBorislav Petkov * additionally longer than the first replacement alternative.
1804332195cSBorislav Petkov */
1814332195cSBorislav Petkov #define OLDINSTR_2(oldinstr, num1, num2) \
1821c1ed473SBorislav Petkov "# ALT: oldinstr2\n" \
183dbe4058aSBorislav Petkov "661:\n\t" oldinstr "\n662:\n" \
1841c1ed473SBorislav Petkov "# ALT: padding2\n" \
185dbe4058aSBorislav Petkov ".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * " \
186dbe4058aSBorislav Petkov "(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n" \
1874332195cSBorislav Petkov alt_end_marker ":\n"
1884332195cSBorislav Petkov
18971a93c26SBorislav Petkov #define OLDINSTR_3(oldinsn, n1, n2, n3) \
19071a93c26SBorislav Petkov "# ALT: oldinstr3\n" \
19171a93c26SBorislav Petkov "661:\n\t" oldinsn "\n662:\n" \
19271a93c26SBorislav Petkov "# ALT: padding3\n" \
19371a93c26SBorislav Petkov ".skip -((" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3)) \
19471a93c26SBorislav Petkov " - (" alt_slen ")) > 0) * " \
19571a93c26SBorislav Petkov "(" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3)) \
19671a93c26SBorislav Petkov " - (" alt_slen ")), 0x90\n" \
19771a93c26SBorislav Petkov alt_end_marker ":\n"
19871a93c26SBorislav Petkov
1995d1dd961SBorislav Petkov (AMD) #define ALTINSTR_ENTRY(ft_flags, num) \
200954e482bSFenghua Yu " .long 661b - .\n" /* label */ \
2014332195cSBorislav Petkov " .long " b_replacement(num)"f - .\n" /* new instruction */ \
2025d1dd961SBorislav Petkov (AMD) " .4byte " __stringify(ft_flags) "\n" /* feature + flags */ \
2034332195cSBorislav Petkov " .byte " alt_total_slen "\n" /* source len */ \
20423c1ad53SPeter Zijlstra " .byte " alt_rlen(num) "\n" /* replacement len */
205954e482bSFenghua Yu
206db16e072SJuergen Gross #define ALTINSTR_REPLACEMENT(newinstr, num) /* replacement */ \
2071c1ed473SBorislav Petkov "# ALT: replacement " #num "\n" \
2081c1ed473SBorislav Petkov b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n"
209954e482bSFenghua Yu
210edc953faSMathieu Desnoyers /* alternative assembly primitive: */
2115d1dd961SBorislav Petkov (AMD) #define ALTERNATIVE(oldinstr, newinstr, ft_flags) \
2124332195cSBorislav Petkov OLDINSTR(oldinstr, 1) \
2139cebed42SH. Peter Anvin ".pushsection .altinstructions,\"a\"\n" \
2145d1dd961SBorislav Petkov (AMD) ALTINSTR_ENTRY(ft_flags, 1) \
2159cebed42SH. Peter Anvin ".popsection\n" \
2169cebed42SH. Peter Anvin ".pushsection .altinstr_replacement, \"ax\"\n" \
217db16e072SJuergen Gross ALTINSTR_REPLACEMENT(newinstr, 1) \
218b9e705efSDavid Woodhouse ".popsection\n"
219954e482bSFenghua Yu
2205d1dd961SBorislav Petkov (AMD) #define ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) \
2214332195cSBorislav Petkov OLDINSTR_2(oldinstr, 1, 2) \
2229cebed42SH. Peter Anvin ".pushsection .altinstructions,\"a\"\n" \
2235d1dd961SBorislav Petkov (AMD) ALTINSTR_ENTRY(ft_flags1, 1) \
2245d1dd961SBorislav Petkov (AMD) ALTINSTR_ENTRY(ft_flags2, 2) \
2259cebed42SH. Peter Anvin ".popsection\n" \
2269cebed42SH. Peter Anvin ".pushsection .altinstr_replacement, \"ax\"\n" \
227db16e072SJuergen Gross ALTINSTR_REPLACEMENT(newinstr1, 1) \
228db16e072SJuergen Gross ALTINSTR_REPLACEMENT(newinstr2, 2) \
229b9e705efSDavid Woodhouse ".popsection\n"
230edc953faSMathieu Desnoyers
231e208b3c4SJuergen Gross /* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */
2325d1dd961SBorislav Petkov (AMD) #define ALTERNATIVE_TERNARY(oldinstr, ft_flags, newinstr_yes, newinstr_no) \
233e208b3c4SJuergen Gross ALTERNATIVE_2(oldinstr, newinstr_no, X86_FEATURE_ALWAYS, \
2345d1dd961SBorislav Petkov (AMD) newinstr_yes, ft_flags)
235e208b3c4SJuergen Gross
2365d1dd961SBorislav Petkov (AMD) #define ALTERNATIVE_3(oldinsn, newinsn1, ft_flags1, newinsn2, ft_flags2, \
2375d1dd961SBorislav Petkov (AMD) newinsn3, ft_flags3) \
23871a93c26SBorislav Petkov OLDINSTR_3(oldinsn, 1, 2, 3) \
23971a93c26SBorislav Petkov ".pushsection .altinstructions,\"a\"\n" \
2405d1dd961SBorislav Petkov (AMD) ALTINSTR_ENTRY(ft_flags1, 1) \
2415d1dd961SBorislav Petkov (AMD) ALTINSTR_ENTRY(ft_flags2, 2) \
2425d1dd961SBorislav Petkov (AMD) ALTINSTR_ENTRY(ft_flags3, 3) \
24371a93c26SBorislav Petkov ".popsection\n" \
24471a93c26SBorislav Petkov ".pushsection .altinstr_replacement, \"ax\"\n" \
245db16e072SJuergen Gross ALTINSTR_REPLACEMENT(newinsn1, 1) \
246db16e072SJuergen Gross ALTINSTR_REPLACEMENT(newinsn2, 2) \
247db16e072SJuergen Gross ALTINSTR_REPLACEMENT(newinsn3, 3) \
24871a93c26SBorislav Petkov ".popsection\n"
24971a93c26SBorislav Petkov
250bb898558SAl Viro /*
251bb898558SAl Viro * Alternative instructions for different CPU types or capabilities.
252bb898558SAl Viro *
253bb898558SAl Viro * This allows to use optimized instructions even on generic binary
254bb898558SAl Viro * kernels.
255bb898558SAl Viro *
256bb898558SAl Viro * length of oldinstr must be longer or equal the length of newinstr
257bb898558SAl Viro * It can be padded with nops as needed.
258bb898558SAl Viro *
259bb898558SAl Viro * For non barrier like inlines please define new variants
260bb898558SAl Viro * without volatile and memory clobber.
261bb898558SAl Viro */
2625d1dd961SBorislav Petkov (AMD) #define alternative(oldinstr, newinstr, ft_flags) \
2635d1dd961SBorislav Petkov (AMD) asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, ft_flags) : : : "memory")
264bb898558SAl Viro
2655d1dd961SBorislav Petkov (AMD) #define alternative_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) \
2665d1dd961SBorislav Petkov (AMD) asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) ::: "memory")
2674332195cSBorislav Petkov
2685d1dd961SBorislav Petkov (AMD) #define alternative_ternary(oldinstr, ft_flags, newinstr_yes, newinstr_no) \
2695d1dd961SBorislav Petkov (AMD) asm_inline volatile(ALTERNATIVE_TERNARY(oldinstr, ft_flags, newinstr_yes, newinstr_no) ::: "memory")
270e208b3c4SJuergen Gross
271bb898558SAl Viro /*
272bb898558SAl Viro * Alternative inline assembly with input.
273bb898558SAl Viro *
274a97673a1SIngo Molnar * Peculiarities:
275bb898558SAl Viro * No memory clobber here.
276bb898558SAl Viro * Argument numbers start with 1.
277edc953faSMathieu Desnoyers * Leaving an unused argument 0 to keep API compatibility.
278bb898558SAl Viro */
2795d1dd961SBorislav Petkov (AMD) #define alternative_input(oldinstr, newinstr, ft_flags, input...) \
2805d1dd961SBorislav Petkov (AMD) asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, ft_flags) \
281edc953faSMathieu Desnoyers : : "i" (0), ## input)
282bb898558SAl Viro
2835b3e83f4SFenghua Yu /*
2845b3e83f4SFenghua Yu * This is similar to alternative_input. But it has two features and
2855b3e83f4SFenghua Yu * respective instructions.
2865b3e83f4SFenghua Yu *
2875b3e83f4SFenghua Yu * If CPU has feature2, newinstr2 is used.
2885b3e83f4SFenghua Yu * Otherwise, if CPU has feature1, newinstr1 is used.
2895b3e83f4SFenghua Yu * Otherwise, oldinstr is used.
2905b3e83f4SFenghua Yu */
2915d1dd961SBorislav Petkov (AMD) #define alternative_input_2(oldinstr, newinstr1, ft_flags1, newinstr2, \
2925d1dd961SBorislav Petkov (AMD) ft_flags2, input...) \
2935d1dd961SBorislav Petkov (AMD) asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, \
2945d1dd961SBorislav Petkov (AMD) newinstr2, ft_flags2) \
2955b3e83f4SFenghua Yu : : "i" (0), ## input)
2965b3e83f4SFenghua Yu
297bb898558SAl Viro /* Like alternative_input, but with a single output argument */
2985d1dd961SBorislav Petkov (AMD) #define alternative_io(oldinstr, newinstr, ft_flags, output, input...) \
2995d1dd961SBorislav Petkov (AMD) asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, ft_flags) \
300edc953faSMathieu Desnoyers : output : "i" (0), ## input)
301bb898558SAl Viro
3021b1d9258SJan Beulich /* Like alternative_io, but for replacing a direct call with another one. */
3035d1dd961SBorislav Petkov (AMD) #define alternative_call(oldfunc, newfunc, ft_flags, output, input...) \
304*5396ce9aSUros Bizjak asm_inline volatile (ALTERNATIVE("call %c[old]", "call %c[new]", ft_flags) \
3051b1d9258SJan Beulich : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
3061b1d9258SJan Beulich
307bb898558SAl Viro /*
308954e482bSFenghua Yu * Like alternative_call, but there are two features and respective functions.
309954e482bSFenghua Yu * If CPU has feature2, function2 is used.
310954e482bSFenghua Yu * Otherwise, if CPU has feature1, function1 is used.
311954e482bSFenghua Yu * Otherwise, old function is used.
312954e482bSFenghua Yu */
3135d1dd961SBorislav Petkov (AMD) #define alternative_call_2(oldfunc, newfunc1, ft_flags1, newfunc2, ft_flags2, \
314954e482bSFenghua Yu output, input...) \
315*5396ce9aSUros Bizjak asm_inline volatile (ALTERNATIVE_2("call %c[old]", "call %c[new1]", ft_flags1, \
316*5396ce9aSUros Bizjak "call %c[new2]", ft_flags2) \
317f5caf621SJosh Poimboeuf : output, ASM_CALL_CONSTRAINT \
318317c2ce7SJosh Poimboeuf : [old] "i" (oldfunc), [new1] "i" (newfunc1), \
319bd627103SAlexey Dobriyan [new2] "i" (newfunc2), ## input)
320954e482bSFenghua Yu
321954e482bSFenghua Yu /*
322bb898558SAl Viro * use this macro(s) if you need more than one output parameter
323bb898558SAl Viro * in alternative_io
324bb898558SAl Viro */
3251b1d9258SJan Beulich #define ASM_OUTPUT2(a...) a
326bb898558SAl Viro
327819165fbSJan Beulich /*
328819165fbSJan Beulich * use this macro if you need clobbers but no inputs in
329819165fbSJan Beulich * alternative_{input,io,call}()
330819165fbSJan Beulich */
331819165fbSJan Beulich #define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
332819165fbSJan Beulich
3335e21a3ecSJuergen Gross #else /* __ASSEMBLY__ */
3345e21a3ecSJuergen Gross
3355e21a3ecSJuergen Gross #ifdef CONFIG_SMP
3365e21a3ecSJuergen Gross .macro LOCK_PREFIX
3375e21a3ecSJuergen Gross 672: lock
3385e21a3ecSJuergen Gross .pushsection .smp_locks,"a"
3395e21a3ecSJuergen Gross .balign 4
3405e21a3ecSJuergen Gross .long 672b - .
3415e21a3ecSJuergen Gross .popsection
3425e21a3ecSJuergen Gross .endm
3435e21a3ecSJuergen Gross #else
3445e21a3ecSJuergen Gross .macro LOCK_PREFIX
3455e21a3ecSJuergen Gross .endm
3465e21a3ecSJuergen Gross #endif
3475e21a3ecSJuergen Gross
3485e21a3ecSJuergen Gross /*
3495e21a3ecSJuergen Gross * objtool annotation to ignore the alternatives and only consider the original
3505e21a3ecSJuergen Gross * instruction(s).
3515e21a3ecSJuergen Gross */
3525e21a3ecSJuergen Gross .macro ANNOTATE_IGNORE_ALTERNATIVE
3535e21a3ecSJuergen Gross .Lannotate_\@:
3545e21a3ecSJuergen Gross .pushsection .discard.ignore_alts
35506f61af8SFangrui Song .long .Lannotate_\@
3565e21a3ecSJuergen Gross .popsection
3575e21a3ecSJuergen Gross .endm
3585e21a3ecSJuergen Gross
3595e21a3ecSJuergen Gross /*
3605e21a3ecSJuergen Gross * Issue one struct alt_instr descriptor entry (need to put it into
3615e21a3ecSJuergen Gross * the section .altinstructions, see below). This entry contains
3625e21a3ecSJuergen Gross * enough information for the alternatives patching code to patch an
3635e21a3ecSJuergen Gross * instruction. See apply_alternatives().
3645e21a3ecSJuergen Gross */
3655d1dd961SBorislav Petkov (AMD) .macro altinstr_entry orig alt ft_flags orig_len alt_len
3665e21a3ecSJuergen Gross .long \orig - .
3675e21a3ecSJuergen Gross .long \alt - .
3685d1dd961SBorislav Petkov (AMD) .4byte \ft_flags
3695e21a3ecSJuergen Gross .byte \orig_len
3705e21a3ecSJuergen Gross .byte \alt_len
3715e21a3ecSJuergen Gross .endm
3725e21a3ecSJuergen Gross
3735e21a3ecSJuergen Gross /*
3745e21a3ecSJuergen Gross * Define an alternative between two instructions. If @feature is
3755e21a3ecSJuergen Gross * present, early code in apply_alternatives() replaces @oldinstr with
3765e21a3ecSJuergen Gross * @newinstr. ".skip" directive takes care of proper instruction padding
3775e21a3ecSJuergen Gross * in case @newinstr is longer than @oldinstr.
3785e21a3ecSJuergen Gross */
3795d1dd961SBorislav Petkov (AMD) .macro ALTERNATIVE oldinstr, newinstr, ft_flags
3805e21a3ecSJuergen Gross 140:
3815e21a3ecSJuergen Gross \oldinstr
3825e21a3ecSJuergen Gross 141:
3835e21a3ecSJuergen Gross .skip -(((144f-143f)-(141b-140b)) > 0) * ((144f-143f)-(141b-140b)),0x90
3845e21a3ecSJuergen Gross 142:
3855e21a3ecSJuergen Gross
3865e21a3ecSJuergen Gross .pushsection .altinstructions,"a"
3875d1dd961SBorislav Petkov (AMD) altinstr_entry 140b,143f,\ft_flags,142b-140b,144f-143f
3885e21a3ecSJuergen Gross .popsection
3895e21a3ecSJuergen Gross
3905e21a3ecSJuergen Gross .pushsection .altinstr_replacement,"ax"
3915e21a3ecSJuergen Gross 143:
3925e21a3ecSJuergen Gross \newinstr
3935e21a3ecSJuergen Gross 144:
3945e21a3ecSJuergen Gross .popsection
3955e21a3ecSJuergen Gross .endm
3965e21a3ecSJuergen Gross
3975e21a3ecSJuergen Gross #define old_len 141b-140b
3985e21a3ecSJuergen Gross #define new_len1 144f-143f
3995e21a3ecSJuergen Gross #define new_len2 145f-144f
40052354973SPeter Zijlstra #define new_len3 146f-145f
4015e21a3ecSJuergen Gross
4025e21a3ecSJuergen Gross /*
4035e21a3ecSJuergen Gross * gas compatible max based on the idea from:
4045e21a3ecSJuergen Gross * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
4055e21a3ecSJuergen Gross *
4065e21a3ecSJuergen Gross * The additional "-" is needed because gas uses a "true" value of -1.
4075e21a3ecSJuergen Gross */
40852354973SPeter Zijlstra #define alt_max_2(a, b) ((a) ^ (((a) ^ (b)) & -(-((a) < (b)))))
40952354973SPeter Zijlstra #define alt_max_3(a, b, c) (alt_max_2(alt_max_2(a, b), c))
4105e21a3ecSJuergen Gross
4115e21a3ecSJuergen Gross
4125e21a3ecSJuergen Gross /*
4135e21a3ecSJuergen Gross * Same as ALTERNATIVE macro above but for two alternatives. If CPU
4145e21a3ecSJuergen Gross * has @feature1, it replaces @oldinstr with @newinstr1. If CPU has
4155e21a3ecSJuergen Gross * @feature2, it replaces @oldinstr with @feature2.
4165e21a3ecSJuergen Gross */
4175d1dd961SBorislav Petkov (AMD) .macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
4185e21a3ecSJuergen Gross 140:
4195e21a3ecSJuergen Gross \oldinstr
4205e21a3ecSJuergen Gross 141:
42152354973SPeter Zijlstra .skip -((alt_max_2(new_len1, new_len2) - (old_len)) > 0) * \
42252354973SPeter Zijlstra (alt_max_2(new_len1, new_len2) - (old_len)),0x90
4235e21a3ecSJuergen Gross 142:
4245e21a3ecSJuergen Gross
4255e21a3ecSJuergen Gross .pushsection .altinstructions,"a"
4265d1dd961SBorislav Petkov (AMD) altinstr_entry 140b,143f,\ft_flags1,142b-140b,144f-143f
4275d1dd961SBorislav Petkov (AMD) altinstr_entry 140b,144f,\ft_flags2,142b-140b,145f-144f
4285e21a3ecSJuergen Gross .popsection
4295e21a3ecSJuergen Gross
4305e21a3ecSJuergen Gross .pushsection .altinstr_replacement,"ax"
4315e21a3ecSJuergen Gross 143:
4325e21a3ecSJuergen Gross \newinstr1
4335e21a3ecSJuergen Gross 144:
4345e21a3ecSJuergen Gross \newinstr2
4355e21a3ecSJuergen Gross 145:
4365e21a3ecSJuergen Gross .popsection
4375e21a3ecSJuergen Gross .endm
4385e21a3ecSJuergen Gross
4395d1dd961SBorislav Petkov (AMD) .macro ALTERNATIVE_3 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2, newinstr3, ft_flags3
44052354973SPeter Zijlstra 140:
44152354973SPeter Zijlstra \oldinstr
44252354973SPeter Zijlstra 141:
44352354973SPeter Zijlstra .skip -((alt_max_3(new_len1, new_len2, new_len3) - (old_len)) > 0) * \
44452354973SPeter Zijlstra (alt_max_3(new_len1, new_len2, new_len3) - (old_len)),0x90
44552354973SPeter Zijlstra 142:
44652354973SPeter Zijlstra
44752354973SPeter Zijlstra .pushsection .altinstructions,"a"
4485d1dd961SBorislav Petkov (AMD) altinstr_entry 140b,143f,\ft_flags1,142b-140b,144f-143f
4495d1dd961SBorislav Petkov (AMD) altinstr_entry 140b,144f,\ft_flags2,142b-140b,145f-144f
4505d1dd961SBorislav Petkov (AMD) altinstr_entry 140b,145f,\ft_flags3,142b-140b,146f-145f
45152354973SPeter Zijlstra .popsection
45252354973SPeter Zijlstra
45352354973SPeter Zijlstra .pushsection .altinstr_replacement,"ax"
45452354973SPeter Zijlstra 143:
45552354973SPeter Zijlstra \newinstr1
45652354973SPeter Zijlstra 144:
45752354973SPeter Zijlstra \newinstr2
45852354973SPeter Zijlstra 145:
45952354973SPeter Zijlstra \newinstr3
46052354973SPeter Zijlstra 146:
46152354973SPeter Zijlstra .popsection
46252354973SPeter Zijlstra .endm
46352354973SPeter Zijlstra
464e208b3c4SJuergen Gross /* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */
4655d1dd961SBorislav Petkov (AMD) #define ALTERNATIVE_TERNARY(oldinstr, ft_flags, newinstr_yes, newinstr_no) \
466e208b3c4SJuergen Gross ALTERNATIVE_2 oldinstr, newinstr_no, X86_FEATURE_ALWAYS, \
4675d1dd961SBorislav Petkov (AMD) newinstr_yes, ft_flags
468e208b3c4SJuergen Gross
469f005f5d8SAndy Lutomirski #endif /* __ASSEMBLY__ */
470f005f5d8SAndy Lutomirski
4711965aae3SH. Peter Anvin #endif /* _ASM_X86_ALTERNATIVE_H */
472