1 #ifndef _ASM_X86_ALTERNATIVE_H
2 #define _ASM_X86_ALTERNATIVE_H
6 #include <linux/types.h>
7 #include <linux/stddef.h>
8 #include <linux/stringify.h>
12 * Alternative inline assembly for SMP.
14 * The LOCK_PREFIX macro defined here replaces the LOCK and
15 * LOCK_PREFIX macros used everywhere in the source tree.
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.
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.
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.
33 #define LOCK_PREFIX_HERE \
34 ".section .smp_locks,\"a\"\n" \
36 ".long 671f - .\n" /* offset */ \
40 #define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
42 #else /* ! CONFIG_SMP */
43 #define LOCK_PREFIX_HERE ""
44 #define LOCK_PREFIX ""
48 s32 instr_offset; /* original instruction */
49 s32 repl_offset; /* offset to replacement instruction */
50 u16 cpuid; /* cpuid bit set for replacement */
51 u8 instrlen; /* length of original instruction */
52 u8 replacementlen; /* length of new instruction */
53 u8 padlen; /* length of build-time padding */
56 extern void alternative_instructions(void);
57 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
62 extern void alternatives_smp_module_add(struct module *mod, char *name,
63 void *locks, void *locks_end,
64 void *text, void *text_end);
65 extern void alternatives_smp_module_del(struct module *mod);
66 extern void alternatives_enable_smp(void);
67 extern int alternatives_text_reserved(void *start, void *end);
68 extern bool skip_smp_alternatives;
70 static inline void alternatives_smp_module_add(struct module *mod, char *name,
71 void *locks, void *locks_end,
72 void *text, void *text_end) {}
73 static inline void alternatives_smp_module_del(struct module *mod) {}
74 static inline void alternatives_enable_smp(void) {}
75 static inline int alternatives_text_reserved(void *start, void *end)
79 #endif /* CONFIG_SMP */
81 #define b_replacement(num) "664"#num
82 #define e_replacement(num) "665"#num
84 #define alt_end_marker "663"
85 #define alt_slen "662b-661b"
86 #define alt_pad_len alt_end_marker"b-662b"
87 #define alt_total_slen alt_end_marker"b-661b"
88 #define alt_rlen(num) e_replacement(num)"f-"b_replacement(num)"f"
90 #define __OLDINSTR(oldinstr, num) \
91 "661:\n\t" oldinstr "\n662:\n" \
92 ".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * " \
93 "((" alt_rlen(num) ")-(" alt_slen ")),0x90\n"
95 #define OLDINSTR(oldinstr, num) \
96 __OLDINSTR(oldinstr, num) \
100 * max without conditionals. Idea adapted from:
101 * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
103 * The additional "-" is needed because gas works with s32s.
105 #define alt_max_short(a, b) "((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") - (" b ")))))"
108 * Pad the second replacement alternative with additional NOPs if it is
109 * additionally longer than the first replacement alternative.
111 #define OLDINSTR_2(oldinstr, num1, num2) \
112 "661:\n\t" oldinstr "\n662:\n" \
113 ".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * " \
114 "(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n" \
117 #define ALTINSTR_ENTRY(feature, num) \
118 " .long 661b - .\n" /* label */ \
119 " .long " b_replacement(num)"f - .\n" /* new instruction */ \
120 " .word " __stringify(feature) "\n" /* feature bit */ \
121 " .byte " alt_total_slen "\n" /* source len */ \
122 " .byte " alt_rlen(num) "\n" /* replacement len */ \
123 " .byte " alt_pad_len "\n" /* pad len */
125 #define ALTINSTR_REPLACEMENT(newinstr, feature, num) /* replacement */ \
126 b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n\t"
128 /* alternative assembly primitive: */
129 #define ALTERNATIVE(oldinstr, newinstr, feature) \
130 OLDINSTR(oldinstr, 1) \
131 ".pushsection .altinstructions,\"a\"\n" \
132 ALTINSTR_ENTRY(feature, 1) \
134 ".pushsection .altinstr_replacement, \"ax\"\n" \
135 ALTINSTR_REPLACEMENT(newinstr, feature, 1) \
138 #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
139 OLDINSTR_2(oldinstr, 1, 2) \
140 ".pushsection .altinstructions,\"a\"\n" \
141 ALTINSTR_ENTRY(feature1, 1) \
142 ALTINSTR_ENTRY(feature2, 2) \
144 ".pushsection .altinstr_replacement, \"ax\"\n" \
145 ALTINSTR_REPLACEMENT(newinstr1, feature1, 1) \
146 ALTINSTR_REPLACEMENT(newinstr2, feature2, 2) \
150 * This must be included *after* the definition of ALTERNATIVE due to
151 * <asm/arch_hweight.h>
153 #include <asm/cpufeature.h>
156 * Alternative instructions for different CPU types or capabilities.
158 * This allows to use optimized instructions even on generic binary
161 * length of oldinstr must be longer or equal the length of newinstr
162 * It can be padded with nops as needed.
164 * For non barrier like inlines please define new variants
165 * without volatile and memory clobber.
167 #define alternative(oldinstr, newinstr, feature) \
168 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
170 #define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \
171 asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory")
174 * Alternative inline assembly with input.
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.
182 * Leaving an unused argument 0 to keep API compatibility.
184 #define alternative_input(oldinstr, newinstr, feature, input...) \
185 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
186 : : "i" (0), ## input)
188 /* Like alternative_input, but with a single output argument */
189 #define alternative_io(oldinstr, newinstr, feature, output, input...) \
190 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
191 : output : "i" (0), ## input)
193 /* Like alternative_io, but for replacing a direct call with another one. */
194 #define alternative_call(oldfunc, newfunc, feature, output, input...) \
195 asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
196 : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
199 * use this macro(s) if you need more than one output parameter
202 #define ASM_OUTPUT2(a...) a
204 struct paravirt_patch_site;
205 #ifdef CONFIG_PARAVIRT
206 void apply_paravirt(struct paravirt_patch_site *start,
207 struct paravirt_patch_site *end);
209 static inline void apply_paravirt(struct paravirt_patch_site *start,
210 struct paravirt_patch_site *end)
212 #define __parainstructions NULL
213 #define __parainstructions_end NULL
216 extern void *text_poke_early(void *addr, const void *opcode, size_t len);
219 * Clear and restore the kernel write-protection flag on the local CPU.
220 * Allows the kernel to edit read-only pages.
221 * Side-effect: any interrupt handler running between save and restore will have
222 * the ability to write to read-only pages.
225 * Code patching in the UP case is safe if NMIs and MCE handlers are stopped and
226 * no thread can be preempted in the instructions being modified (no iret to an
227 * invalid instruction possible) or if the instructions are changed from a
228 * consistent state to another consistent state atomically.
229 * More care must be taken when modifying code in the SMP case because of
230 * Intel's errata. text_poke_smp() takes care that errata, but still
231 * doesn't support NMI/MCE handler code modifying.
232 * On the local CPU you need to be protected again NMI or MCE handlers seeing an
233 * inconsistent instruction while you patch.
235 struct text_poke_param {
241 extern void *text_poke(void *addr, const void *opcode, size_t len);
242 extern void *text_poke_smp(void *addr, const void *opcode, size_t len);
243 extern void text_poke_smp_batch(struct text_poke_param *params, int n);
245 #endif /* __ASSEMBLY__ */
247 #endif /* _ASM_X86_ALTERNATIVE_H */