1 #ifndef _X86_64_ALTERNATIVE_H
2 #define _X86_64_ALTERNATIVE_H
6 #include <linux/types.h>
9 u8 *instr; /* original instruction */
11 u8 cpuid; /* cpuid bit set for replacement */
12 u8 instrlen; /* length of original instruction */
13 u8 replacementlen; /* length of new instruction, <= instrlen */
17 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
22 extern void alternatives_smp_module_add(struct module *mod, char *name,
23 void *locks, void *locks_end,
24 void *text, void *text_end);
25 extern void alternatives_smp_module_del(struct module *mod);
26 extern void alternatives_smp_switch(int smp);
28 static inline void alternatives_smp_module_add(struct module *mod, char *name,
29 void *locks, void *locks_end,
30 void *text, void *text_end) {}
31 static inline void alternatives_smp_module_del(struct module *mod) {}
32 static inline void alternatives_smp_switch(int smp) {}
38 * Alternative instructions for different CPU types or capabilities.
40 * This allows to use optimized instructions even on generic binary
43 * length of oldinstr must be longer or equal the length of newinstr
44 * It can be padded with nops as needed.
46 * For non barrier like inlines please define new variants
47 * without volatile and memory clobber.
49 #define alternative(oldinstr, newinstr, feature) \
50 asm volatile ("661:\n\t" oldinstr "\n662:\n" \
51 ".section .altinstructions,\"a\"\n" \
53 " .quad 661b\n" /* label */ \
54 " .quad 663f\n" /* new instruction */ \
55 " .byte %c0\n" /* feature bit */ \
56 " .byte 662b-661b\n" /* sourcelen */ \
57 " .byte 664f-663f\n" /* replacementlen */ \
59 ".section .altinstr_replacement,\"ax\"\n" \
60 "663:\n\t" newinstr "\n664:\n" /* replacement */ \
61 ".previous" :: "i" (feature) : "memory")
64 * Alternative inline assembly with input.
67 * No memory clobber here.
68 * Argument numbers start with 1.
69 * Best is to use constraints that are fixed size (like (%1) ... "r")
70 * If you use variable sized constraints like "m" or "g" in the
71 * replacement make sure to pad to the worst case length.
73 #define alternative_input(oldinstr, newinstr, feature, input...) \
74 asm volatile ("661:\n\t" oldinstr "\n662:\n" \
75 ".section .altinstructions,\"a\"\n" \
77 " .quad 661b\n" /* label */ \
78 " .quad 663f\n" /* new instruction */ \
79 " .byte %c0\n" /* feature bit */ \
80 " .byte 662b-661b\n" /* sourcelen */ \
81 " .byte 664f-663f\n" /* replacementlen */ \
83 ".section .altinstr_replacement,\"ax\"\n" \
84 "663:\n\t" newinstr "\n664:\n" /* replacement */ \
85 ".previous" :: "i" (feature), ##input)
87 /* Like alternative_input, but with a single output argument */
88 #define alternative_io(oldinstr, newinstr, feature, output, input...) \
89 asm volatile ("661:\n\t" oldinstr "\n662:\n" \
90 ".section .altinstructions,\"a\"\n" \
92 " .quad 661b\n" /* label */ \
93 " .quad 663f\n" /* new instruction */ \
94 " .byte %c[feat]\n" /* feature bit */ \
95 " .byte 662b-661b\n" /* sourcelen */ \
96 " .byte 664f-663f\n" /* replacementlen */ \
98 ".section .altinstr_replacement,\"ax\"\n" \
99 "663:\n\t" newinstr "\n664:\n" /* replacement */ \
100 ".previous" : output : [feat] "i" (feature), ##input)
103 * Alternative inline assembly for SMP.
105 * alternative_smp() takes two versions (SMP first, UP second) and is
106 * for more complex stuff such as spinlocks.
108 * The LOCK_PREFIX macro defined here replaces the LOCK and
109 * LOCK_PREFIX macros used everywhere in the source tree.
111 * SMP alternatives use the same data structures as the other
112 * alternatives and the X86_FEATURE_UP flag to indicate the case of a
113 * UP system running a SMP kernel. The existing apply_alternatives()
114 * works fine for patching a SMP kernel for UP.
116 * The SMP alternative tables can be kept after boot and contain both
117 * UP and SMP versions of the instructions to allow switching back to
118 * SMP at runtime, when hotplugging in a new CPU, which is especially
119 * useful in virtualized environments.
121 * The very common lock prefix is handled as special case in a
122 * separate table which is a pure address list without replacement ptr
123 * and size information. That keeps the table sizes small.
127 #define alternative_smp(smpinstr, upinstr, args...) \
128 asm volatile ("661:\n\t" smpinstr "\n662:\n" \
129 ".section .smp_altinstructions,\"a\"\n" \
131 " .quad 661b\n" /* label */ \
132 " .quad 663f\n" /* new instruction */ \
133 " .byte 0x66\n" /* X86_FEATURE_UP */ \
134 " .byte 662b-661b\n" /* sourcelen */ \
135 " .byte 664f-663f\n" /* replacementlen */ \
137 ".section .smp_altinstr_replacement,\"awx\"\n" \
138 "663:\n\t" upinstr "\n" /* replacement */ \
139 "664:\n\t.fill 662b-661b,1,0x42\n" /* space for original */ \
142 #define LOCK_PREFIX \
143 ".section .smp_locks,\"a\"\n" \
145 " .quad 661f\n" /* address */ \
149 #else /* ! CONFIG_SMP */
150 #define alternative_smp(smpinstr, upinstr, args...) \
151 asm volatile (upinstr : args)
152 #define LOCK_PREFIX ""
155 #endif /* _X86_64_ALTERNATIVE_H */