1 #ifndef _X86_64_ALTERNATIVE_H
2 #define _X86_64_ALTERNATIVE_H
6 #include <linux/types.h>
7 #include <linux/stddef.h>
8 #include <asm/cpufeature.h>
11 u8 *instr; /* original instruction */
13 u8 cpuid; /* cpuid bit set for replacement */
14 u8 instrlen; /* length of original instruction */
15 u8 replacementlen; /* length of new instruction, <= instrlen */
19 extern void alternative_instructions(void);
20 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
25 extern void alternatives_smp_module_add(struct module *mod, char *name,
26 void *locks, void *locks_end,
27 void *text, void *text_end);
28 extern void alternatives_smp_module_del(struct module *mod);
29 extern void alternatives_smp_switch(int smp);
31 static inline void alternatives_smp_module_add(struct module *mod, char *name,
32 void *locks, void *locks_end,
33 void *text, void *text_end) {}
34 static inline void alternatives_smp_module_del(struct module *mod) {}
35 static inline void alternatives_smp_switch(int smp) {}
41 * Alternative instructions for different CPU types or capabilities.
43 * This allows to use optimized instructions even on generic binary
46 * length of oldinstr must be longer or equal the length of newinstr
47 * It can be padded with nops as needed.
49 * For non barrier like inlines please define new variants
50 * without volatile and memory clobber.
52 #define alternative(oldinstr, newinstr, feature) \
53 asm volatile ("661:\n\t" oldinstr "\n662:\n" \
54 ".section .altinstructions,\"a\"\n" \
56 " .quad 661b\n" /* label */ \
57 " .quad 663f\n" /* new instruction */ \
58 " .byte %c0\n" /* feature bit */ \
59 " .byte 662b-661b\n" /* sourcelen */ \
60 " .byte 664f-663f\n" /* replacementlen */ \
62 ".section .altinstr_replacement,\"ax\"\n" \
63 "663:\n\t" newinstr "\n664:\n" /* replacement */ \
64 ".previous" :: "i" (feature) : "memory")
67 * Alternative inline assembly with input.
70 * No memory clobber here.
71 * Argument numbers start with 1.
72 * Best is to use constraints that are fixed size (like (%1) ... "r")
73 * If you use variable sized constraints like "m" or "g" in the
74 * replacement make sure to pad to the worst case length.
76 #define alternative_input(oldinstr, newinstr, feature, input...) \
77 asm volatile ("661:\n\t" oldinstr "\n662:\n" \
78 ".section .altinstructions,\"a\"\n" \
80 " .quad 661b\n" /* label */ \
81 " .quad 663f\n" /* new instruction */ \
82 " .byte %c0\n" /* feature bit */ \
83 " .byte 662b-661b\n" /* sourcelen */ \
84 " .byte 664f-663f\n" /* replacementlen */ \
86 ".section .altinstr_replacement,\"ax\"\n" \
87 "663:\n\t" newinstr "\n664:\n" /* replacement */ \
88 ".previous" :: "i" (feature), ##input)
90 /* Like alternative_input, but with a single output argument */
91 #define alternative_io(oldinstr, newinstr, feature, output, input...) \
92 asm volatile ("661:\n\t" oldinstr "\n662:\n" \
93 ".section .altinstructions,\"a\"\n" \
95 " .quad 661b\n" /* label */ \
96 " .quad 663f\n" /* new instruction */ \
97 " .byte %c[feat]\n" /* feature bit */ \
98 " .byte 662b-661b\n" /* sourcelen */ \
99 " .byte 664f-663f\n" /* replacementlen */ \
101 ".section .altinstr_replacement,\"ax\"\n" \
102 "663:\n\t" newinstr "\n664:\n" /* replacement */ \
103 ".previous" : output : [feat] "i" (feature), ##input)
106 * use this macro(s) if you need more than one output parameter
109 #define ASM_OUTPUT2(a, b) a, b
112 * Alternative inline assembly for SMP.
114 * The LOCK_PREFIX macro defined here replaces the LOCK and
115 * LOCK_PREFIX macros used everywhere in the source tree.
117 * SMP alternatives use the same data structures as the other
118 * alternatives and the X86_FEATURE_UP flag to indicate the case of a
119 * UP system running a SMP kernel. The existing apply_alternatives()
120 * works fine for patching a SMP kernel for UP.
122 * The SMP alternative tables can be kept after boot and contain both
123 * UP and SMP versions of the instructions to allow switching back to
124 * SMP at runtime, when hotplugging in a new CPU, which is especially
125 * useful in virtualized environments.
127 * The very common lock prefix is handled as special case in a
128 * separate table which is a pure address list without replacement ptr
129 * and size information. That keeps the table sizes small.
133 #define LOCK_PREFIX \
134 ".section .smp_locks,\"a\"\n" \
136 " .quad 661f\n" /* address */ \
140 #else /* ! CONFIG_SMP */
141 #define LOCK_PREFIX ""
144 struct paravirt_patch;
145 #ifdef CONFIG_PARAVIRT
146 void apply_paravirt(struct paravirt_patch *start, struct paravirt_patch *end);
149 apply_paravirt(struct paravirt_patch *start, struct paravirt_patch *end)
151 #define __parainstructions NULL
152 #define __parainstructions_end NULL
155 #endif /* _X86_64_ALTERNATIVE_H */