blob: 92a9033c14d15df62bdba55c9dc42a2346e8fb7b [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>
8
9/*
10 * Alternative inline assembly for SMP.
11 *
12 * The LOCK_PREFIX macro defined here replaces the LOCK and
13 * LOCK_PREFIX macros used everywhere in the source tree.
14 *
15 * SMP alternatives use the same data structures as the other
16 * alternatives and the X86_FEATURE_UP flag to indicate the case of a
17 * UP system running a SMP kernel. The existing apply_alternatives()
18 * works fine for patching a SMP kernel for UP.
19 *
20 * The SMP alternative tables can be kept after boot and contain both
21 * UP and SMP versions of the instructions to allow switching back to
22 * SMP at runtime, when hotplugging in a new CPU, which is especially
23 * useful in virtualized environments.
24 *
25 * The very common lock prefix is handled as special case in a
26 * separate table which is a pure address list without replacement ptr
27 * and size information. That keeps the table sizes small.
28 */
29
30#ifdef CONFIG_SMP
Luca Barbierib3ac8912010-02-24 10:54:22 +010031#define LOCK_PREFIX_HERE \
H. Peter Anvin6b592572008-01-30 13:30:30 +010032 ".section .smp_locks,\"a\"\n" \
Jan Beulich5967ed82010-04-21 16:08:14 +010033 ".balign 4\n" \
H. Peter Anvind9c58412010-04-29 16:53:17 -070034 ".long 671f - .\n" /* offset */ \
H. Peter Anvin6b592572008-01-30 13:30:30 +010035 ".previous\n" \
Luca Barbierib3ac8912010-02-24 10:54:22 +010036 "671:"
37
38#define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
H. Peter Anvin6b592572008-01-30 13:30:30 +010039
40#else /* ! CONFIG_SMP */
H. Peter Anvinb701a472010-04-29 16:03:57 -070041#define LOCK_PREFIX_HERE ""
H. Peter Anvin6b592572008-01-30 13:30:30 +010042#define LOCK_PREFIX ""
Thomas Gleixner96a388d2007-10-11 11:20:03 +020043#endif
H. Peter Anvin6b592572008-01-30 13:30:30 +010044
45/* This must be included *after* the definition of LOCK_PREFIX */
46#include <asm/cpufeature.h>
47
48struct alt_instr {
49 u8 *instr; /* original instruction */
50 u8 *replacement;
51 u8 cpuid; /* cpuid bit set for replacement */
52 u8 instrlen; /* length of original instruction */
53 u8 replacementlen; /* length of new instruction, <= instrlen */
54 u8 pad1;
55#ifdef CONFIG_X86_64
56 u32 pad2;
57#endif
58};
59
60extern void alternative_instructions(void);
61extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
62
63struct module;
64
65#ifdef CONFIG_SMP
66extern void alternatives_smp_module_add(struct module *mod, char *name,
67 void *locks, void *locks_end,
68 void *text, void *text_end);
69extern void alternatives_smp_module_del(struct module *mod);
70extern void alternatives_smp_switch(int smp);
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -050071extern int alternatives_text_reserved(void *start, void *end);
H. Peter Anvin6b592572008-01-30 13:30:30 +010072#else
73static inline void alternatives_smp_module_add(struct module *mod, char *name,
Joe Perches2ac1ea72008-03-23 01:01:37 -070074 void *locks, void *locks_end,
75 void *text, void *text_end) {}
H. Peter Anvin6b592572008-01-30 13:30:30 +010076static inline void alternatives_smp_module_del(struct module *mod) {}
77static inline void alternatives_smp_switch(int smp) {}
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -050078static inline int alternatives_text_reserved(void *start, void *end)
79{
80 return 0;
81}
H. Peter Anvin6b592572008-01-30 13:30:30 +010082#endif /* CONFIG_SMP */
83
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -040084/* alternative assembly primitive: */
85#define ALTERNATIVE(oldinstr, newinstr, feature) \
86 \
87 "661:\n\t" oldinstr "\n662:\n" \
88 ".section .altinstructions,\"a\"\n" \
89 _ASM_ALIGN "\n" \
90 _ASM_PTR "661b\n" /* label */ \
91 _ASM_PTR "663f\n" /* new instruction */ \
92 " .byte " __stringify(feature) "\n" /* feature bit */ \
93 " .byte 662b-661b\n" /* sourcelen */ \
94 " .byte 664f-663f\n" /* replacementlen */ \
Jan Beulich01be50a2009-11-27 15:04:58 +000095 " .byte 0xff + (664f-663f) - (662b-661b)\n" /* rlen <= slen */ \
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -040096 ".previous\n" \
97 ".section .altinstr_replacement, \"ax\"\n" \
98 "663:\n\t" newinstr "\n664:\n" /* replacement */ \
99 ".previous"
100
H. Peter Anvin6b592572008-01-30 13:30:30 +0100101/*
102 * Alternative instructions for different CPU types or capabilities.
103 *
104 * This allows to use optimized instructions even on generic binary
105 * kernels.
106 *
107 * length of oldinstr must be longer or equal the length of newinstr
108 * It can be padded with nops as needed.
109 *
110 * For non barrier like inlines please define new variants
111 * without volatile and memory clobber.
112 */
113#define alternative(oldinstr, newinstr, feature) \
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400114 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
H. Peter Anvin6b592572008-01-30 13:30:30 +0100115
116/*
117 * Alternative inline assembly with input.
118 *
119 * Pecularities:
120 * No memory clobber here.
121 * Argument numbers start with 1.
122 * Best is to use constraints that are fixed size (like (%1) ... "r")
123 * If you use variable sized constraints like "m" or "g" in the
124 * replacement make sure to pad to the worst case length.
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400125 * Leaving an unused argument 0 to keep API compatibility.
H. Peter Anvin6b592572008-01-30 13:30:30 +0100126 */
127#define alternative_input(oldinstr, newinstr, feature, input...) \
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400128 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
129 : : "i" (0), ## input)
H. Peter Anvin6b592572008-01-30 13:30:30 +0100130
131/* Like alternative_input, but with a single output argument */
132#define alternative_io(oldinstr, newinstr, feature, output, input...) \
Mathieu Desnoyersedc953f2009-04-28 11:13:46 -0400133 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
134 : output : "i" (0), ## input)
H. Peter Anvin6b592572008-01-30 13:30:30 +0100135
Jan Beulich1b1d9252009-12-18 16:12:56 +0000136/* Like alternative_io, but for replacing a direct call with another one. */
137#define alternative_call(oldfunc, newfunc, feature, output, input...) \
138 asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
139 : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
140
H. Peter Anvin6b592572008-01-30 13:30:30 +0100141/*
142 * use this macro(s) if you need more than one output parameter
143 * in alternative_io
144 */
Jan Beulich1b1d9252009-12-18 16:12:56 +0000145#define ASM_OUTPUT2(a...) a
H. Peter Anvin6b592572008-01-30 13:30:30 +0100146
147struct paravirt_patch_site;
148#ifdef CONFIG_PARAVIRT
149void apply_paravirt(struct paravirt_patch_site *start,
150 struct paravirt_patch_site *end);
151#else
Joe Perches2ac1ea72008-03-23 01:01:37 -0700152static inline void apply_paravirt(struct paravirt_patch_site *start,
153 struct paravirt_patch_site *end)
H. Peter Anvin6b592572008-01-30 13:30:30 +0100154{}
155#define __parainstructions NULL
156#define __parainstructions_end NULL
157#endif
158
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500159/*
160 * Clear and restore the kernel write-protection flag on the local CPU.
161 * Allows the kernel to edit read-only pages.
162 * Side-effect: any interrupt handler running between save and restore will have
163 * the ability to write to read-only pages.
164 *
165 * Warning:
166 * Code patching in the UP case is safe if NMIs and MCE handlers are stopped and
167 * no thread can be preempted in the instructions being modified (no iret to an
168 * invalid instruction possible) or if the instructions are changed from a
169 * consistent state to another consistent state atomically.
170 * More care must be taken when modifying code in the SMP case because of
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500171 * Intel's errata. text_poke_smp() takes care that errata, but still
172 * doesn't support NMI/MCE handler code modifying.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500173 * On the local CPU you need to be protected again NMI or MCE handlers seeing an
174 * inconsistent instruction while you patch.
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500175 */
Mathieu Desnoyerse587cad2008-03-06 08:48:49 -0500176extern void *text_poke(void *addr, const void *opcode, size_t len);
Masami Hiramatsu3d55cc82010-02-25 08:34:38 -0500177extern void *text_poke_smp(void *addr, const void *opcode, size_t len);
H. Peter Anvin6b592572008-01-30 13:30:30 +0100178
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700179#endif /* _ASM_X86_ALTERNATIVE_H */