blob: 5aef6a97d80e53ac215782aa88d577658d726c2e [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_ALTERNATIVE_H
2#define _ASM_X86_ALTERNATIVE_H
H. Peter Anvin6b592572008-01-30 13:30:30 +01003
4#include <linux/types.h>
5#include <linux/stddef.h>
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -04006#include <linux/stringify.h>
H. Peter Anvin6b592572008-01-30 13:30:30 +01007#include <asm/asm.h>
Jiri Kosina17f41572013-07-23 10:09:28 +02008#include <asm/ptrace.h>
H. Peter Anvin6b592572008-01-30 13:30:30 +01009
10/*
11 * Alternative inline assembly for SMP.
12 *
13 * The LOCK_PREFIX macro defined here replaces the LOCK and
14 * LOCK_PREFIX macros used everywhere in the source tree.
15 *
16 * SMP alternatives use the same data structures as the other
17 * alternatives and the X86_FEATURE_UP flag to indicate the case of a
18 * UP system running a SMP kernel. The existing apply_alternatives()
19 * works fine for patching a SMP kernel for UP.
20 *
21 * The SMP alternative tables can be kept after boot and contain both
22 * UP and SMP versions of the instructions to allow switching back to
23 * SMP at runtime, when hotplugging in a new CPU, which is especially
24 * useful in virtualized environments.
25 *
26 * The very common lock prefix is handled as special case in a
27 * separate table which is a pure address list without replacement ptr
28 * and size information. That keeps the table sizes small.
29 */
30
31#ifdef CONFIG_SMP
Luca Barbierib3ac8912010-02-24 10:54:22 +010032#define LOCK_PREFIX_HERE \
H. Peter Anvin9cebed42012-09-21 12:43:08 -070033 ".pushsection .smp_locks,\"a\"\n" \
34 ".balign 4\n" \
35 ".long 671f - .\n" /* offset */ \
36 ".popsection\n" \
Luca Barbierib3ac8912010-02-24 10:54:22 +010037 "671:"
38
39#define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
H. Peter Anvin6b592572008-01-30 13:30:30 +010040
41#else /* ! CONFIG_SMP */
H. Peter Anvinb701a472010-04-29 16:03:57 -070042#define LOCK_PREFIX_HERE ""
H. Peter Anvin6b592572008-01-30 13:30:30 +010043#define LOCK_PREFIX ""
Thomas Gleixner96a388d2007-10-11 11:20:03 +020044#endif
H. Peter Anvin6b592572008-01-30 13:30:30 +010045
H. Peter Anvin6b592572008-01-30 13:30:30 +010046struct alt_instr {
Andy Lutomirski59e97e42011-07-13 09:24:10 -040047 s32 instr_offset; /* original instruction */
48 s32 repl_offset; /* offset to replacement instruction */
H. Peter Anvin83a7a2a2010-06-10 00:10:43 +000049 u16 cpuid; /* cpuid bit set for replacement */
H. Peter Anvin6b592572008-01-30 13:30:30 +010050 u8 instrlen; /* length of original instruction */
Borislav Petkov43321952014-12-27 10:41:52 +010051 u8 replacementlen; /* length of new instruction */
52 u8 padlen; /* length of build-time padding */
53} __packed;
H. Peter Anvin6b592572008-01-30 13:30:30 +010054
55extern void alternative_instructions(void);
56extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
57
58struct module;
59
60#ifdef CONFIG_SMP
61extern void alternatives_smp_module_add(struct module *mod, char *name,
62 void *locks, void *locks_end,
63 void *text, void *text_end);
64extern void alternatives_smp_module_del(struct module *mod);
Rusty Russell816afe42012-08-06 17:29:49 +093065extern void alternatives_enable_smp(void);
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -050066extern int alternatives_text_reserved(void *start, void *end);
Suresh Siddha3fb82d52010-11-23 16:11:40 -080067extern bool skip_smp_alternatives;
H. Peter Anvin6b592572008-01-30 13:30:30 +010068#else
69static inline void alternatives_smp_module_add(struct module *mod, char *name,
Joe Perches2ac1ea72008-03-23 01:01:37 -070070 void *locks, void *locks_end,
71 void *text, void *text_end) {}
H. Peter Anvin6b592572008-01-30 13:30:30 +010072static inline void alternatives_smp_module_del(struct module *mod) {}
Rusty Russell816afe42012-08-06 17:29:49 +093073static inline void alternatives_enable_smp(void) {}
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -050074static inline int alternatives_text_reserved(void *start, void *end)
75{
76 return 0;
77}
H. Peter Anvin6b592572008-01-30 13:30:30 +010078#endif /* CONFIG_SMP */
79
Borislav Petkov43321952014-12-27 10:41:52 +010080#define b_replacement(num) "664"#num
81#define e_replacement(num) "665"#num
Fenghua Yu954e4822012-05-24 18:19:45 -070082
Borislav Petkov43321952014-12-27 10:41:52 +010083#define alt_end_marker "663"
84#define alt_slen "662b-661b"
85#define alt_pad_len alt_end_marker"b-662b"
86#define alt_total_slen alt_end_marker"b-661b"
87#define alt_rlen(num) e_replacement(num)"f-"b_replacement(num)"f"
Fenghua Yu954e4822012-05-24 18:19:45 -070088
Borislav Petkov43321952014-12-27 10:41:52 +010089#define __OLDINSTR(oldinstr, num) \
90 "661:\n\t" oldinstr "\n662:\n" \
91 ".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * " \
92 "((" alt_rlen(num) ")-(" alt_slen ")),0x90\n"
Fenghua Yu954e4822012-05-24 18:19:45 -070093
Borislav Petkov43321952014-12-27 10:41:52 +010094#define OLDINSTR(oldinstr, num) \
95 __OLDINSTR(oldinstr, num) \
96 alt_end_marker ":\n"
97
98/*
99 * Pad the second replacement alternative with additional NOPs if it is
100 * additionally longer than the first replacement alternative.
101 */
102#define OLDINSTR_2(oldinstr, num1, num2) \
103 __OLDINSTR(oldinstr, num1) \
104 ".skip -(((" alt_rlen(num2) ")-(" alt_rlen(num1) ")-(662b-661b)) > 0) * " \
105 "((" alt_rlen(num2) ")-(" alt_rlen(num1) ")-(662b-661b)),0x90\n" \
106 alt_end_marker ":\n"
107
108#define ALTINSTR_ENTRY(feature, num) \
Fenghua Yu954e4822012-05-24 18:19:45 -0700109 " .long 661b - .\n" /* label */ \
Borislav Petkov43321952014-12-27 10:41:52 +0100110 " .long " b_replacement(num)"f - .\n" /* new instruction */ \
Fenghua Yu954e4822012-05-24 18:19:45 -0700111 " .word " __stringify(feature) "\n" /* feature bit */ \
Borislav Petkov43321952014-12-27 10:41:52 +0100112 " .byte " alt_total_slen "\n" /* source len */ \
113 " .byte " alt_rlen(num) "\n" /* replacement len */ \
114 " .byte " alt_pad_len "\n" /* pad len */
Fenghua Yu954e4822012-05-24 18:19:45 -0700115
Borislav Petkov43321952014-12-27 10:41:52 +0100116#define ALTINSTR_REPLACEMENT(newinstr, feature, num) /* replacement */ \
117 b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n\t"
Fenghua Yu954e4822012-05-24 18:19:45 -0700118
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400119/* alternative assembly primitive: */
120#define ALTERNATIVE(oldinstr, newinstr, feature) \
Borislav Petkov43321952014-12-27 10:41:52 +0100121 OLDINSTR(oldinstr, 1) \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700122 ".pushsection .altinstructions,\"a\"\n" \
Fenghua Yu954e4822012-05-24 18:19:45 -0700123 ALTINSTR_ENTRY(feature, 1) \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700124 ".popsection\n" \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700125 ".pushsection .altinstr_replacement, \"ax\"\n" \
Fenghua Yu954e4822012-05-24 18:19:45 -0700126 ALTINSTR_REPLACEMENT(newinstr, feature, 1) \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700127 ".popsection"
Fenghua Yu954e4822012-05-24 18:19:45 -0700128
129#define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
Borislav Petkov43321952014-12-27 10:41:52 +0100130 OLDINSTR_2(oldinstr, 1, 2) \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700131 ".pushsection .altinstructions,\"a\"\n" \
Fenghua Yu954e4822012-05-24 18:19:45 -0700132 ALTINSTR_ENTRY(feature1, 1) \
133 ALTINSTR_ENTRY(feature2, 2) \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700134 ".popsection\n" \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700135 ".pushsection .altinstr_replacement, \"ax\"\n" \
Fenghua Yu954e4822012-05-24 18:19:45 -0700136 ALTINSTR_REPLACEMENT(newinstr1, feature1, 1) \
137 ALTINSTR_REPLACEMENT(newinstr2, feature2, 2) \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700138 ".popsection"
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400139
H. Peter Anvin6b592572008-01-30 13:30:30 +0100140/*
Borislav Petkovd61931d2010-03-05 17:34:46 +0100141 * This must be included *after* the definition of ALTERNATIVE due to
142 * <asm/arch_hweight.h>
143 */
144#include <asm/cpufeature.h>
145
146/*
H. Peter Anvin6b592572008-01-30 13:30:30 +0100147 * Alternative instructions for different CPU types or capabilities.
148 *
149 * This allows to use optimized instructions even on generic binary
150 * kernels.
151 *
152 * length of oldinstr must be longer or equal the length of newinstr
153 * It can be padded with nops as needed.
154 *
155 * For non barrier like inlines please define new variants
156 * without volatile and memory clobber.
157 */
158#define alternative(oldinstr, newinstr, feature) \
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400159 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
H. Peter Anvin6b592572008-01-30 13:30:30 +0100160
Borislav Petkov43321952014-12-27 10:41:52 +0100161#define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \
162 asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory")
163
H. Peter Anvin6b592572008-01-30 13:30:30 +0100164/*
165 * Alternative inline assembly with input.
166 *
167 * Pecularities:
168 * No memory clobber here.
169 * Argument numbers start with 1.
170 * Best is to use constraints that are fixed size (like (%1) ... "r")
171 * If you use variable sized constraints like "m" or "g" in the
172 * replacement make sure to pad to the worst case length.
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400173 * Leaving an unused argument 0 to keep API compatibility.
H. Peter Anvin6b592572008-01-30 13:30:30 +0100174 */
175#define alternative_input(oldinstr, newinstr, feature, input...) \
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400176 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
177 : : "i" (0), ## input)
H. Peter Anvin6b592572008-01-30 13:30:30 +0100178
Fenghua Yu5b3e83f2014-05-29 11:12:32 -0700179/*
180 * This is similar to alternative_input. But it has two features and
181 * respective instructions.
182 *
183 * If CPU has feature2, newinstr2 is used.
184 * Otherwise, if CPU has feature1, newinstr1 is used.
185 * Otherwise, oldinstr is used.
186 */
187#define alternative_input_2(oldinstr, newinstr1, feature1, newinstr2, \
188 feature2, input...) \
189 asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, \
190 newinstr2, feature2) \
191 : : "i" (0), ## input)
192
H. Peter Anvin6b592572008-01-30 13:30:30 +0100193/* Like alternative_input, but with a single output argument */
194#define alternative_io(oldinstr, newinstr, feature, output, input...) \
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400195 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
196 : output : "i" (0), ## input)
H. Peter Anvin6b592572008-01-30 13:30:30 +0100197
Jan Beulich1b1d9252009-12-18 16:12:56 +0000198/* Like alternative_io, but for replacing a direct call with another one. */
199#define alternative_call(oldfunc, newfunc, feature, output, input...) \
200 asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
201 : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
202
H. Peter Anvin6b592572008-01-30 13:30:30 +0100203/*
Fenghua Yu954e4822012-05-24 18:19:45 -0700204 * Like alternative_call, but there are two features and respective functions.
205 * If CPU has feature2, function2 is used.
206 * Otherwise, if CPU has feature1, function1 is used.
207 * Otherwise, old function is used.
208 */
209#define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2, \
210 output, input...) \
211 asm volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\
212 "call %P[new2]", feature2) \
213 : output : [old] "i" (oldfunc), [new1] "i" (newfunc1), \
214 [new2] "i" (newfunc2), ## input)
215
216/*
H. Peter Anvin6b592572008-01-30 13:30:30 +0100217 * use this macro(s) if you need more than one output parameter
218 * in alternative_io
219 */
Jan Beulich1b1d9252009-12-18 16:12:56 +0000220#define ASM_OUTPUT2(a...) a
H. Peter Anvin6b592572008-01-30 13:30:30 +0100221
Jan Beulich819165f2012-01-20 16:21:41 +0000222/*
223 * use this macro if you need clobbers but no inputs in
224 * alternative_{input,io,call}()
225 */
226#define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
227
H. Peter Anvin6b592572008-01-30 13:30:30 +0100228struct paravirt_patch_site;
229#ifdef CONFIG_PARAVIRT
230void apply_paravirt(struct paravirt_patch_site *start,
231 struct paravirt_patch_site *end);
232#else
Joe Perches2ac1ea72008-03-23 01:01:37 -0700233static inline void apply_paravirt(struct paravirt_patch_site *start,
234 struct paravirt_patch_site *end)
H. Peter Anvin6b592572008-01-30 13:30:30 +0100235{}
236#define __parainstructions NULL
237#define __parainstructions_end NULL
238#endif
239
Jason Baronfa6f2cc2010-09-17 11:08:56 -0400240extern void *text_poke_early(void *addr, const void *opcode, size_t len);
241
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500242/*
243 * Clear and restore the kernel write-protection flag on the local CPU.
244 * Allows the kernel to edit read-only pages.
245 * Side-effect: any interrupt handler running between save and restore will have
246 * the ability to write to read-only pages.
247 *
248 * Warning:
249 * Code patching in the UP case is safe if NMIs and MCE handlers are stopped and
250 * no thread can be preempted in the instructions being modified (no iret to an
251 * invalid instruction possible) or if the instructions are changed from a
252 * consistent state to another consistent state atomically.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500253 * On the local CPU you need to be protected again NMI or MCE handlers seeing an
254 * inconsistent instruction while you patch.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500255 */
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500256extern void *text_poke(void *addr, const void *opcode, size_t len);
Jiri Kosina17f41572013-07-23 10:09:28 +0200257extern int poke_int3_handler(struct pt_regs *regs);
Jiri Kosinafd4363f2013-07-12 11:21:48 +0200258extern void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler);
H. Peter Anvin6b592572008-01-30 13:30:30 +0100259
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700260#endif /* _ASM_X86_ALTERNATIVE_H */