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