blob: 0a3f9c9f98d5ccf446980cfcf71b7055bb79ade8 [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 */
51 u8 replacementlen; /* length of new instruction, <= instrlen */
H. Peter Anvin6b592572008-01-30 13:30:30 +010052};
53
54extern void alternative_instructions(void);
55extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
56
57struct module;
58
59#ifdef CONFIG_SMP
60extern void alternatives_smp_module_add(struct module *mod, char *name,
61 void *locks, void *locks_end,
62 void *text, void *text_end);
63extern void alternatives_smp_module_del(struct module *mod);
Rusty Russell816afe42012-08-06 17:29:49 +093064extern void alternatives_enable_smp(void);
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -050065extern int alternatives_text_reserved(void *start, void *end);
Suresh Siddha3fb82d52010-11-23 16:11:40 -080066extern bool skip_smp_alternatives;
H. Peter Anvin6b592572008-01-30 13:30:30 +010067#else
68static inline void alternatives_smp_module_add(struct module *mod, char *name,
Joe Perches2ac1ea72008-03-23 01:01:37 -070069 void *locks, void *locks_end,
70 void *text, void *text_end) {}
H. Peter Anvin6b592572008-01-30 13:30:30 +010071static inline void alternatives_smp_module_del(struct module *mod) {}
Rusty Russell816afe42012-08-06 17:29:49 +093072static inline void alternatives_enable_smp(void) {}
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -050073static inline int alternatives_text_reserved(void *start, void *end)
74{
75 return 0;
76}
H. Peter Anvin6b592572008-01-30 13:30:30 +010077#endif /* CONFIG_SMP */
78
Fenghua Yu954e4822012-05-24 18:19:45 -070079#define OLDINSTR(oldinstr) "661:\n\t" oldinstr "\n662:\n"
80
81#define b_replacement(number) "663"#number
82#define e_replacement(number) "664"#number
83
84#define alt_slen "662b-661b"
85#define alt_rlen(number) e_replacement(number)"f-"b_replacement(number)"f"
86
87#define ALTINSTR_ENTRY(feature, number) \
88 " .long 661b - .\n" /* label */ \
89 " .long " b_replacement(number)"f - .\n" /* new instruction */ \
90 " .word " __stringify(feature) "\n" /* feature bit */ \
91 " .byte " alt_slen "\n" /* source len */ \
92 " .byte " alt_rlen(number) "\n" /* replacement len */
93
94#define DISCARD_ENTRY(number) /* rlen <= slen */ \
95 " .byte 0xff + (" alt_rlen(number) ") - (" alt_slen ")\n"
96
97#define ALTINSTR_REPLACEMENT(newinstr, feature, number) /* replacement */ \
98 b_replacement(number)":\n\t" newinstr "\n" e_replacement(number) ":\n\t"
99
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400100/* alternative assembly primitive: */
101#define ALTERNATIVE(oldinstr, newinstr, feature) \
Fenghua Yu954e4822012-05-24 18:19:45 -0700102 OLDINSTR(oldinstr) \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700103 ".pushsection .altinstructions,\"a\"\n" \
Fenghua Yu954e4822012-05-24 18:19:45 -0700104 ALTINSTR_ENTRY(feature, 1) \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700105 ".popsection\n" \
106 ".pushsection .discard,\"aw\",@progbits\n" \
Fenghua Yu954e4822012-05-24 18:19:45 -0700107 DISCARD_ENTRY(1) \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700108 ".popsection\n" \
109 ".pushsection .altinstr_replacement, \"ax\"\n" \
Fenghua Yu954e4822012-05-24 18:19:45 -0700110 ALTINSTR_REPLACEMENT(newinstr, feature, 1) \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700111 ".popsection"
Fenghua Yu954e4822012-05-24 18:19:45 -0700112
113#define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
114 OLDINSTR(oldinstr) \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700115 ".pushsection .altinstructions,\"a\"\n" \
Fenghua Yu954e4822012-05-24 18:19:45 -0700116 ALTINSTR_ENTRY(feature1, 1) \
117 ALTINSTR_ENTRY(feature2, 2) \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700118 ".popsection\n" \
119 ".pushsection .discard,\"aw\",@progbits\n" \
Fenghua Yu954e4822012-05-24 18:19:45 -0700120 DISCARD_ENTRY(1) \
121 DISCARD_ENTRY(2) \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700122 ".popsection\n" \
123 ".pushsection .altinstr_replacement, \"ax\"\n" \
Fenghua Yu954e4822012-05-24 18:19:45 -0700124 ALTINSTR_REPLACEMENT(newinstr1, feature1, 1) \
125 ALTINSTR_REPLACEMENT(newinstr2, feature2, 2) \
H. Peter Anvin9cebed42012-09-21 12:43:08 -0700126 ".popsection"
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400127
H. Peter Anvin6b592572008-01-30 13:30:30 +0100128/*
Borislav Petkovd61931d2010-03-05 17:34:46 +0100129 * This must be included *after* the definition of ALTERNATIVE due to
130 * <asm/arch_hweight.h>
131 */
132#include <asm/cpufeature.h>
133
134/*
H. Peter Anvin6b592572008-01-30 13:30:30 +0100135 * Alternative instructions for different CPU types or capabilities.
136 *
137 * This allows to use optimized instructions even on generic binary
138 * kernels.
139 *
140 * length of oldinstr must be longer or equal the length of newinstr
141 * It can be padded with nops as needed.
142 *
143 * For non barrier like inlines please define new variants
144 * without volatile and memory clobber.
145 */
146#define alternative(oldinstr, newinstr, feature) \
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400147 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
H. Peter Anvin6b592572008-01-30 13:30:30 +0100148
149/*
150 * Alternative inline assembly with input.
151 *
152 * Pecularities:
153 * No memory clobber here.
154 * Argument numbers start with 1.
155 * Best is to use constraints that are fixed size (like (%1) ... "r")
156 * If you use variable sized constraints like "m" or "g" in the
157 * replacement make sure to pad to the worst case length.
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400158 * Leaving an unused argument 0 to keep API compatibility.
H. Peter Anvin6b592572008-01-30 13:30:30 +0100159 */
160#define alternative_input(oldinstr, newinstr, feature, input...) \
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400161 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
162 : : "i" (0), ## input)
H. Peter Anvin6b592572008-01-30 13:30:30 +0100163
164/* Like alternative_input, but with a single output argument */
165#define alternative_io(oldinstr, newinstr, feature, output, input...) \
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400166 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
167 : output : "i" (0), ## input)
H. Peter Anvin6b592572008-01-30 13:30:30 +0100168
Jan Beulich1b1d9252009-12-18 16:12:56 +0000169/* Like alternative_io, but for replacing a direct call with another one. */
170#define alternative_call(oldfunc, newfunc, feature, output, input...) \
171 asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
172 : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
173
H. Peter Anvin6b592572008-01-30 13:30:30 +0100174/*
Fenghua Yu954e4822012-05-24 18:19:45 -0700175 * Like alternative_call, but there are two features and respective functions.
176 * If CPU has feature2, function2 is used.
177 * Otherwise, if CPU has feature1, function1 is used.
178 * Otherwise, old function is used.
179 */
180#define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2, \
181 output, input...) \
182 asm volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\
183 "call %P[new2]", feature2) \
184 : output : [old] "i" (oldfunc), [new1] "i" (newfunc1), \
185 [new2] "i" (newfunc2), ## input)
186
187/*
H. Peter Anvin6b592572008-01-30 13:30:30 +0100188 * use this macro(s) if you need more than one output parameter
189 * in alternative_io
190 */
Jan Beulich1b1d9252009-12-18 16:12:56 +0000191#define ASM_OUTPUT2(a...) a
H. Peter Anvin6b592572008-01-30 13:30:30 +0100192
Jan Beulich819165f2012-01-20 16:21:41 +0000193/*
194 * use this macro if you need clobbers but no inputs in
195 * alternative_{input,io,call}()
196 */
197#define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
198
H. Peter Anvin6b592572008-01-30 13:30:30 +0100199struct paravirt_patch_site;
200#ifdef CONFIG_PARAVIRT
201void apply_paravirt(struct paravirt_patch_site *start,
202 struct paravirt_patch_site *end);
203#else
Joe Perches2ac1ea72008-03-23 01:01:37 -0700204static inline void apply_paravirt(struct paravirt_patch_site *start,
205 struct paravirt_patch_site *end)
H. Peter Anvin6b592572008-01-30 13:30:30 +0100206{}
207#define __parainstructions NULL
208#define __parainstructions_end NULL
209#endif
210
Jason Baronfa6f2cc2010-09-17 11:08:56 -0400211extern void *text_poke_early(void *addr, const void *opcode, size_t len);
212
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500213/*
214 * Clear and restore the kernel write-protection flag on the local CPU.
215 * Allows the kernel to edit read-only pages.
216 * Side-effect: any interrupt handler running between save and restore will have
217 * the ability to write to read-only pages.
218 *
219 * Warning:
220 * Code patching in the UP case is safe if NMIs and MCE handlers are stopped and
221 * no thread can be preempted in the instructions being modified (no iret to an
222 * invalid instruction possible) or if the instructions are changed from a
223 * consistent state to another consistent state atomically.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500224 * On the local CPU you need to be protected again NMI or MCE handlers seeing an
225 * inconsistent instruction while you patch.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500226 */
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500227extern void *text_poke(void *addr, const void *opcode, size_t len);
Jiri Kosina17f41572013-07-23 10:09:28 +0200228extern int poke_int3_handler(struct pt_regs *regs);
Jiri Kosinafd4363f2013-07-12 11:21:48 +0200229extern void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler);
H. Peter Anvin6b592572008-01-30 13:30:30 +0100230
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700231#endif /* _ASM_X86_ALTERNATIVE_H */