H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_ALTERNATIVE_H |
| 2 | #define _ASM_X86_ALTERNATIVE_H |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 3 | |
| 4 | #include <linux/types.h> |
| 5 | #include <linux/stddef.h> |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 6 | #include <linux/stringify.h> |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 7 | #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 Barbieri | b3ac891 | 2010-02-24 10:54:22 +0100 | [diff] [blame] | 31 | #define LOCK_PREFIX_HERE \ |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 32 | ".section .smp_locks,\"a\"\n" \ |
Jan Beulich | 5967ed8 | 2010-04-21 16:08:14 +0100 | [diff] [blame] | 33 | ".balign 4\n" \ |
H. Peter Anvin | d9c5841 | 2010-04-29 16:53:17 -0700 | [diff] [blame] | 34 | ".long 671f - .\n" /* offset */ \ |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 35 | ".previous\n" \ |
Luca Barbieri | b3ac891 | 2010-02-24 10:54:22 +0100 | [diff] [blame] | 36 | "671:" |
| 37 | |
| 38 | #define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; " |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 39 | |
| 40 | #else /* ! CONFIG_SMP */ |
H. Peter Anvin | b701a47 | 2010-04-29 16:03:57 -0700 | [diff] [blame] | 41 | #define LOCK_PREFIX_HERE "" |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 42 | #define LOCK_PREFIX "" |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 43 | #endif |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 44 | |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 45 | struct alt_instr { |
Andy Lutomirski | 59e97e4 | 2011-07-13 09:24:10 -0400 | [diff] [blame] | 46 | s32 instr_offset; /* original instruction */ |
| 47 | s32 repl_offset; /* offset to replacement instruction */ |
H. Peter Anvin | 83a7a2a | 2010-06-10 00:10:43 +0000 | [diff] [blame] | 48 | u16 cpuid; /* cpuid bit set for replacement */ |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 49 | u8 instrlen; /* length of original instruction */ |
| 50 | u8 replacementlen; /* length of new instruction, <= instrlen */ |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | extern void alternative_instructions(void); |
| 54 | extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end); |
| 55 | |
| 56 | struct module; |
| 57 | |
| 58 | #ifdef CONFIG_SMP |
| 59 | extern void alternatives_smp_module_add(struct module *mod, char *name, |
| 60 | void *locks, void *locks_end, |
| 61 | void *text, void *text_end); |
| 62 | extern void alternatives_smp_module_del(struct module *mod); |
| 63 | extern void alternatives_smp_switch(int smp); |
Masami Hiramatsu | 2cfa197 | 2010-02-02 16:49:11 -0500 | [diff] [blame] | 64 | extern int alternatives_text_reserved(void *start, void *end); |
Suresh Siddha | 3fb82d5 | 2010-11-23 16:11:40 -0800 | [diff] [blame] | 65 | extern bool skip_smp_alternatives; |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 66 | #else |
| 67 | static inline void alternatives_smp_module_add(struct module *mod, char *name, |
Joe Perches | 2ac1ea7 | 2008-03-23 01:01:37 -0700 | [diff] [blame] | 68 | void *locks, void *locks_end, |
| 69 | void *text, void *text_end) {} |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 70 | static inline void alternatives_smp_module_del(struct module *mod) {} |
| 71 | static inline void alternatives_smp_switch(int smp) {} |
Masami Hiramatsu | 2cfa197 | 2010-02-02 16:49:11 -0500 | [diff] [blame] | 72 | static inline int alternatives_text_reserved(void *start, void *end) |
| 73 | { |
| 74 | return 0; |
| 75 | } |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 76 | #endif /* CONFIG_SMP */ |
| 77 | |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 78 | #define OLDINSTR(oldinstr) "661:\n\t" oldinstr "\n662:\n" |
| 79 | |
| 80 | #define b_replacement(number) "663"#number |
| 81 | #define e_replacement(number) "664"#number |
| 82 | |
| 83 | #define alt_slen "662b-661b" |
| 84 | #define alt_rlen(number) e_replacement(number)"f-"b_replacement(number)"f" |
| 85 | |
| 86 | #define ALTINSTR_ENTRY(feature, number) \ |
| 87 | " .long 661b - .\n" /* label */ \ |
| 88 | " .long " b_replacement(number)"f - .\n" /* new instruction */ \ |
| 89 | " .word " __stringify(feature) "\n" /* feature bit */ \ |
| 90 | " .byte " alt_slen "\n" /* source len */ \ |
| 91 | " .byte " alt_rlen(number) "\n" /* replacement len */ |
| 92 | |
| 93 | #define DISCARD_ENTRY(number) /* rlen <= slen */ \ |
| 94 | " .byte 0xff + (" alt_rlen(number) ") - (" alt_slen ")\n" |
| 95 | |
| 96 | #define ALTINSTR_REPLACEMENT(newinstr, feature, number) /* replacement */ \ |
| 97 | b_replacement(number)":\n\t" newinstr "\n" e_replacement(number) ":\n\t" |
| 98 | |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 99 | /* alternative assembly primitive: */ |
| 100 | #define ALTERNATIVE(oldinstr, newinstr, feature) \ |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 101 | OLDINSTR(oldinstr) \ |
| 102 | ".section .altinstructions,\"a\"\n" \ |
| 103 | ALTINSTR_ENTRY(feature, 1) \ |
| 104 | ".previous\n" \ |
| 105 | ".section .discard,\"aw\",@progbits\n" \ |
| 106 | DISCARD_ENTRY(1) \ |
| 107 | ".previous\n" \ |
| 108 | ".section .altinstr_replacement, \"ax\"\n" \ |
| 109 | ALTINSTR_REPLACEMENT(newinstr, feature, 1) \ |
| 110 | ".previous" |
| 111 | |
| 112 | #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\ |
| 113 | OLDINSTR(oldinstr) \ |
| 114 | ".section .altinstructions,\"a\"\n" \ |
| 115 | ALTINSTR_ENTRY(feature1, 1) \ |
| 116 | ALTINSTR_ENTRY(feature2, 2) \ |
| 117 | ".previous\n" \ |
| 118 | ".section .discard,\"aw\",@progbits\n" \ |
| 119 | DISCARD_ENTRY(1) \ |
| 120 | DISCARD_ENTRY(2) \ |
| 121 | ".previous\n" \ |
| 122 | ".section .altinstr_replacement, \"ax\"\n" \ |
| 123 | ALTINSTR_REPLACEMENT(newinstr1, feature1, 1) \ |
| 124 | ALTINSTR_REPLACEMENT(newinstr2, feature2, 2) \ |
| 125 | ".previous" |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 126 | |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 127 | /* |
Borislav Petkov | d61931d | 2010-03-05 17:34:46 +0100 | [diff] [blame] | 128 | * This must be included *after* the definition of ALTERNATIVE due to |
| 129 | * <asm/arch_hweight.h> |
| 130 | */ |
| 131 | #include <asm/cpufeature.h> |
| 132 | |
| 133 | /* |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 134 | * Alternative instructions for different CPU types or capabilities. |
| 135 | * |
| 136 | * This allows to use optimized instructions even on generic binary |
| 137 | * kernels. |
| 138 | * |
| 139 | * length of oldinstr must be longer or equal the length of newinstr |
| 140 | * It can be padded with nops as needed. |
| 141 | * |
| 142 | * For non barrier like inlines please define new variants |
| 143 | * without volatile and memory clobber. |
| 144 | */ |
| 145 | #define alternative(oldinstr, newinstr, feature) \ |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 146 | asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory") |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 147 | |
| 148 | /* |
| 149 | * Alternative inline assembly with input. |
| 150 | * |
| 151 | * Pecularities: |
| 152 | * No memory clobber here. |
| 153 | * Argument numbers start with 1. |
| 154 | * Best is to use constraints that are fixed size (like (%1) ... "r") |
| 155 | * If you use variable sized constraints like "m" or "g" in the |
| 156 | * replacement make sure to pad to the worst case length. |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 157 | * Leaving an unused argument 0 to keep API compatibility. |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 158 | */ |
| 159 | #define alternative_input(oldinstr, newinstr, feature, input...) \ |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 160 | asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \ |
| 161 | : : "i" (0), ## input) |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 162 | |
| 163 | /* Like alternative_input, but with a single output argument */ |
| 164 | #define alternative_io(oldinstr, newinstr, feature, output, input...) \ |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 165 | asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \ |
| 166 | : output : "i" (0), ## input) |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 167 | |
Jan Beulich | 1b1d925 | 2009-12-18 16:12:56 +0000 | [diff] [blame] | 168 | /* Like alternative_io, but for replacing a direct call with another one. */ |
| 169 | #define alternative_call(oldfunc, newfunc, feature, output, input...) \ |
| 170 | asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \ |
| 171 | : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input) |
| 172 | |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 173 | /* |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 174 | * Like alternative_call, but there are two features and respective functions. |
| 175 | * If CPU has feature2, function2 is used. |
| 176 | * Otherwise, if CPU has feature1, function1 is used. |
| 177 | * Otherwise, old function is used. |
| 178 | */ |
| 179 | #define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2, \ |
| 180 | output, input...) \ |
| 181 | asm volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\ |
| 182 | "call %P[new2]", feature2) \ |
| 183 | : output : [old] "i" (oldfunc), [new1] "i" (newfunc1), \ |
| 184 | [new2] "i" (newfunc2), ## input) |
| 185 | |
| 186 | /* |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 187 | * use this macro(s) if you need more than one output parameter |
| 188 | * in alternative_io |
| 189 | */ |
Jan Beulich | 1b1d925 | 2009-12-18 16:12:56 +0000 | [diff] [blame] | 190 | #define ASM_OUTPUT2(a...) a |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 191 | |
Jan Beulich | 819165f | 2012-01-20 16:21:41 +0000 | [diff] [blame] | 192 | /* |
| 193 | * use this macro if you need clobbers but no inputs in |
| 194 | * alternative_{input,io,call}() |
| 195 | */ |
| 196 | #define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr |
| 197 | |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 198 | struct paravirt_patch_site; |
| 199 | #ifdef CONFIG_PARAVIRT |
| 200 | void apply_paravirt(struct paravirt_patch_site *start, |
| 201 | struct paravirt_patch_site *end); |
| 202 | #else |
Joe Perches | 2ac1ea7 | 2008-03-23 01:01:37 -0700 | [diff] [blame] | 203 | static inline void apply_paravirt(struct paravirt_patch_site *start, |
| 204 | struct paravirt_patch_site *end) |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 205 | {} |
| 206 | #define __parainstructions NULL |
| 207 | #define __parainstructions_end NULL |
| 208 | #endif |
| 209 | |
Jason Baron | fa6f2cc | 2010-09-17 11:08:56 -0400 | [diff] [blame] | 210 | extern void *text_poke_early(void *addr, const void *opcode, size_t len); |
| 211 | |
Mathieu Desnoyers | e587cad | 2008-03-06 08:48:49 -0500 | [diff] [blame] | 212 | /* |
| 213 | * Clear and restore the kernel write-protection flag on the local CPU. |
| 214 | * Allows the kernel to edit read-only pages. |
| 215 | * Side-effect: any interrupt handler running between save and restore will have |
| 216 | * the ability to write to read-only pages. |
| 217 | * |
| 218 | * Warning: |
| 219 | * Code patching in the UP case is safe if NMIs and MCE handlers are stopped and |
| 220 | * no thread can be preempted in the instructions being modified (no iret to an |
| 221 | * invalid instruction possible) or if the instructions are changed from a |
| 222 | * consistent state to another consistent state atomically. |
| 223 | * More care must be taken when modifying code in the SMP case because of |
Masami Hiramatsu | 3d55cc8 | 2010-02-25 08:34:38 -0500 | [diff] [blame] | 224 | * Intel's errata. text_poke_smp() takes care that errata, but still |
| 225 | * doesn't support NMI/MCE handler code modifying. |
Mathieu Desnoyers | e587cad | 2008-03-06 08:48:49 -0500 | [diff] [blame] | 226 | * On the local CPU you need to be protected again NMI or MCE handlers seeing an |
| 227 | * inconsistent instruction while you patch. |
Mathieu Desnoyers | e587cad | 2008-03-06 08:48:49 -0500 | [diff] [blame] | 228 | */ |
Masami Hiramatsu | 7deb18d | 2010-12-03 18:54:22 +0900 | [diff] [blame] | 229 | struct text_poke_param { |
| 230 | void *addr; |
| 231 | const void *opcode; |
| 232 | size_t len; |
| 233 | }; |
| 234 | |
Mathieu Desnoyers | e587cad | 2008-03-06 08:48:49 -0500 | [diff] [blame] | 235 | extern void *text_poke(void *addr, const void *opcode, size_t len); |
Masami Hiramatsu | 3d55cc8 | 2010-02-25 08:34:38 -0500 | [diff] [blame] | 236 | extern void *text_poke_smp(void *addr, const void *opcode, size_t len); |
Masami Hiramatsu | 7deb18d | 2010-12-03 18:54:22 +0900 | [diff] [blame] | 237 | extern void text_poke_smp_batch(struct text_poke_param *params, int n); |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 238 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 239 | #endif /* _ASM_X86_ALTERNATIVE_H */ |