blob: 4a2adaa9aefcc1fe2e42f7c01e29cb3ec61f5842 [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>
Jason Baronbf5438fc2010-09-17 11:09:00 -04007#include <linux/jump_label.h>
H. Peter Anvin6b592572008-01-30 13:30:30 +01008#include <asm/asm.h>
9
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 Anvin6b592572008-01-30 13:30:30 +010033 ".section .smp_locks,\"a\"\n" \
Jan Beulich5967ed82010-04-21 16:08:14 +010034 ".balign 4\n" \
H. Peter Anvind9c58412010-04-29 16:53:17 -070035 ".long 671f - .\n" /* offset */ \
H. Peter Anvin6b592572008-01-30 13:30:30 +010036 ".previous\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 {
47 u8 *instr; /* original instruction */
48 u8 *replacement;
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#ifdef CONFIG_X86_64
53 u32 pad2;
54#endif
55};
56
57extern void alternative_instructions(void);
58extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
59
60struct module;
61
62#ifdef CONFIG_SMP
63extern void alternatives_smp_module_add(struct module *mod, char *name,
64 void *locks, void *locks_end,
65 void *text, void *text_end);
66extern void alternatives_smp_module_del(struct module *mod);
67extern void alternatives_smp_switch(int smp);
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -050068extern int alternatives_text_reserved(void *start, void *end);
H. Peter Anvin6b592572008-01-30 13:30:30 +010069#else
70static inline void alternatives_smp_module_add(struct module *mod, char *name,
Joe Perches2ac1ea72008-03-23 01:01:37 -070071 void *locks, void *locks_end,
72 void *text, void *text_end) {}
H. Peter Anvin6b592572008-01-30 13:30:30 +010073static inline void alternatives_smp_module_del(struct module *mod) {}
74static inline void alternatives_smp_switch(int smp) {}
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -050075static inline int alternatives_text_reserved(void *start, void *end)
76{
77 return 0;
78}
H. Peter Anvin6b592572008-01-30 13:30:30 +010079#endif /* CONFIG_SMP */
80
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -040081/* alternative assembly primitive: */
82#define ALTERNATIVE(oldinstr, newinstr, feature) \
83 \
84 "661:\n\t" oldinstr "\n662:\n" \
85 ".section .altinstructions,\"a\"\n" \
86 _ASM_ALIGN "\n" \
87 _ASM_PTR "661b\n" /* label */ \
88 _ASM_PTR "663f\n" /* new instruction */ \
H. Peter Anvin83a7a2a2010-06-10 00:10:43 +000089 " .word " __stringify(feature) "\n" /* feature bit */ \
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -040090 " .byte 662b-661b\n" /* sourcelen */ \
91 " .byte 664f-663f\n" /* replacementlen */ \
H. Peter Anvin83a7a2a2010-06-10 00:10:43 +000092 ".previous\n" \
93 ".section .discard,\"aw\",@progbits\n" \
Jan Beulich01be50a2009-11-27 15:04:58 +000094 " .byte 0xff + (664f-663f) - (662b-661b)\n" /* rlen <= slen */ \
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -040095 ".previous\n" \
96 ".section .altinstr_replacement, \"ax\"\n" \
97 "663:\n\t" newinstr "\n664:\n" /* replacement */ \
98 ".previous"
99
H. Peter Anvin6b592572008-01-30 13:30:30 +0100100/*
Borislav Petkovd61931d2010-03-05 17:34:46 +0100101 * This must be included *after* the definition of ALTERNATIVE due to
102 * <asm/arch_hweight.h>
103 */
104#include <asm/cpufeature.h>
105
106/*
H. Peter Anvin6b592572008-01-30 13:30:30 +0100107 * Alternative instructions for different CPU types or capabilities.
108 *
109 * This allows to use optimized instructions even on generic binary
110 * kernels.
111 *
112 * length of oldinstr must be longer or equal the length of newinstr
113 * It can be padded with nops as needed.
114 *
115 * For non barrier like inlines please define new variants
116 * without volatile and memory clobber.
117 */
118#define alternative(oldinstr, newinstr, feature) \
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400119 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
H. Peter Anvin6b592572008-01-30 13:30:30 +0100120
121/*
122 * Alternative inline assembly with input.
123 *
124 * Pecularities:
125 * No memory clobber here.
126 * Argument numbers start with 1.
127 * Best is to use constraints that are fixed size (like (%1) ... "r")
128 * If you use variable sized constraints like "m" or "g" in the
129 * replacement make sure to pad to the worst case length.
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400130 * Leaving an unused argument 0 to keep API compatibility.
H. Peter Anvin6b592572008-01-30 13:30:30 +0100131 */
132#define alternative_input(oldinstr, newinstr, feature, input...) \
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400133 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
134 : : "i" (0), ## input)
H. Peter Anvin6b592572008-01-30 13:30:30 +0100135
136/* Like alternative_input, but with a single output argument */
137#define alternative_io(oldinstr, newinstr, feature, output, input...) \
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400138 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
139 : output : "i" (0), ## input)
H. Peter Anvin6b592572008-01-30 13:30:30 +0100140
Jan Beulich1b1d9252009-12-18 16:12:56 +0000141/* Like alternative_io, but for replacing a direct call with another one. */
142#define alternative_call(oldfunc, newfunc, feature, output, input...) \
143 asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
144 : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
145
H. Peter Anvin6b592572008-01-30 13:30:30 +0100146/*
147 * use this macro(s) if you need more than one output parameter
148 * in alternative_io
149 */
Jan Beulich1b1d9252009-12-18 16:12:56 +0000150#define ASM_OUTPUT2(a...) a
H. Peter Anvin6b592572008-01-30 13:30:30 +0100151
152struct paravirt_patch_site;
153#ifdef CONFIG_PARAVIRT
154void apply_paravirt(struct paravirt_patch_site *start,
155 struct paravirt_patch_site *end);
156#else
Joe Perches2ac1ea72008-03-23 01:01:37 -0700157static inline void apply_paravirt(struct paravirt_patch_site *start,
158 struct paravirt_patch_site *end)
H. Peter Anvin6b592572008-01-30 13:30:30 +0100159{}
160#define __parainstructions NULL
161#define __parainstructions_end NULL
162#endif
163
Jason Baronfa6f2cc2010-09-17 11:08:56 -0400164extern void *text_poke_early(void *addr, const void *opcode, size_t len);
165
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500166/*
167 * Clear and restore the kernel write-protection flag on the local CPU.
168 * Allows the kernel to edit read-only pages.
169 * Side-effect: any interrupt handler running between save and restore will have
170 * the ability to write to read-only pages.
171 *
172 * Warning:
173 * Code patching in the UP case is safe if NMIs and MCE handlers are stopped and
174 * no thread can be preempted in the instructions being modified (no iret to an
175 * invalid instruction possible) or if the instructions are changed from a
176 * consistent state to another consistent state atomically.
177 * More care must be taken when modifying code in the SMP case because of
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500178 * Intel's errata. text_poke_smp() takes care that errata, but still
179 * doesn't support NMI/MCE handler code modifying.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500180 * On the local CPU you need to be protected again NMI or MCE handlers seeing an
181 * inconsistent instruction while you patch.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500182 */
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900183struct text_poke_param {
184 void *addr;
185 const void *opcode;
186 size_t len;
187};
188
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500189extern void *text_poke(void *addr, const void *opcode, size_t len);
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500190extern void *text_poke_smp(void *addr, const void *opcode, size_t len);
Masami Hiramatsu7deb18d2010-12-03 18:54:22 +0900191extern void text_poke_smp_batch(struct text_poke_param *params, int n);
H. Peter Anvin6b592572008-01-30 13:30:30 +0100192
Jason Baronbf5438fc2010-09-17 11:09:00 -0400193#if defined(CONFIG_DYNAMIC_FTRACE) || defined(HAVE_JUMP_LABEL)
Jason Baronf49aa442010-09-17 11:08:51 -0400194#define IDEAL_NOP_SIZE_5 5
195extern unsigned char ideal_nop5[IDEAL_NOP_SIZE_5];
196extern void arch_init_ideal_nop5(void);
197#else
198static inline void arch_init_ideal_nop5(void) {}
199#endif
200
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700201#endif /* _ASM_X86_ALTERNATIVE_H */