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 |
| 31 | #define LOCK_PREFIX \ |
| 32 | ".section .smp_locks,\"a\"\n" \ |
| 33 | _ASM_ALIGN "\n" \ |
| 34 | _ASM_PTR "661f\n" /* address */ \ |
| 35 | ".previous\n" \ |
| 36 | "661:\n\tlock; " |
| 37 | |
| 38 | #else /* ! CONFIG_SMP */ |
| 39 | #define LOCK_PREFIX "" |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 40 | #endif |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 41 | |
| 42 | /* This must be included *after* the definition of LOCK_PREFIX */ |
| 43 | #include <asm/cpufeature.h> |
| 44 | |
| 45 | struct alt_instr { |
| 46 | u8 *instr; /* original instruction */ |
| 47 | u8 *replacement; |
| 48 | u8 cpuid; /* cpuid bit set for replacement */ |
| 49 | u8 instrlen; /* length of original instruction */ |
| 50 | u8 replacementlen; /* length of new instruction, <= instrlen */ |
| 51 | u8 pad1; |
| 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); |
| 68 | #else |
| 69 | static inline void alternatives_smp_module_add(struct module *mod, char *name, |
Joe Perches | 2ac1ea7 | 2008-03-23 01:01:37 -0700 | [diff] [blame] | 70 | void *locks, void *locks_end, |
| 71 | void *text, void *text_end) {} |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 72 | static inline void alternatives_smp_module_del(struct module *mod) {} |
| 73 | static inline void alternatives_smp_switch(int smp) {} |
| 74 | #endif /* CONFIG_SMP */ |
| 75 | |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 76 | /* alternative assembly primitive: */ |
| 77 | #define ALTERNATIVE(oldinstr, newinstr, feature) \ |
| 78 | \ |
| 79 | "661:\n\t" oldinstr "\n662:\n" \ |
| 80 | ".section .altinstructions,\"a\"\n" \ |
| 81 | _ASM_ALIGN "\n" \ |
| 82 | _ASM_PTR "661b\n" /* label */ \ |
| 83 | _ASM_PTR "663f\n" /* new instruction */ \ |
| 84 | " .byte " __stringify(feature) "\n" /* feature bit */ \ |
| 85 | " .byte 662b-661b\n" /* sourcelen */ \ |
| 86 | " .byte 664f-663f\n" /* replacementlen */ \ |
Jan Beulich | 01be50a | 2009-11-27 15:04:58 +0000 | [diff] [blame] | 87 | " .byte 0xff + (664f-663f) - (662b-661b)\n" /* rlen <= slen */ \ |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 88 | ".previous\n" \ |
| 89 | ".section .altinstr_replacement, \"ax\"\n" \ |
| 90 | "663:\n\t" newinstr "\n664:\n" /* replacement */ \ |
| 91 | ".previous" |
| 92 | |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 93 | /* |
| 94 | * Alternative instructions for different CPU types or capabilities. |
| 95 | * |
| 96 | * This allows to use optimized instructions even on generic binary |
| 97 | * kernels. |
| 98 | * |
| 99 | * length of oldinstr must be longer or equal the length of newinstr |
| 100 | * It can be padded with nops as needed. |
| 101 | * |
| 102 | * For non barrier like inlines please define new variants |
| 103 | * without volatile and memory clobber. |
| 104 | */ |
| 105 | #define alternative(oldinstr, newinstr, feature) \ |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 106 | asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory") |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * Alternative inline assembly with input. |
| 110 | * |
| 111 | * Pecularities: |
| 112 | * No memory clobber here. |
| 113 | * Argument numbers start with 1. |
| 114 | * Best is to use constraints that are fixed size (like (%1) ... "r") |
| 115 | * If you use variable sized constraints like "m" or "g" in the |
| 116 | * replacement make sure to pad to the worst case length. |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 117 | * Leaving an unused argument 0 to keep API compatibility. |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 118 | */ |
| 119 | #define alternative_input(oldinstr, newinstr, feature, input...) \ |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 120 | asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \ |
| 121 | : : "i" (0), ## input) |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 122 | |
| 123 | /* Like alternative_input, but with a single output argument */ |
| 124 | #define alternative_io(oldinstr, newinstr, feature, output, input...) \ |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 125 | asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \ |
| 126 | : output : "i" (0), ## input) |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 127 | |
Jan Beulich | 1b1d925 | 2009-12-18 16:12:56 +0000 | [diff] [blame^] | 128 | /* Like alternative_io, but for replacing a direct call with another one. */ |
| 129 | #define alternative_call(oldfunc, newfunc, feature, output, input...) \ |
| 130 | asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \ |
| 131 | : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input) |
| 132 | |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 133 | /* |
| 134 | * use this macro(s) if you need more than one output parameter |
| 135 | * in alternative_io |
| 136 | */ |
Jan Beulich | 1b1d925 | 2009-12-18 16:12:56 +0000 | [diff] [blame^] | 137 | #define ASM_OUTPUT2(a...) a |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 138 | |
| 139 | struct paravirt_patch_site; |
| 140 | #ifdef CONFIG_PARAVIRT |
| 141 | void apply_paravirt(struct paravirt_patch_site *start, |
| 142 | struct paravirt_patch_site *end); |
| 143 | #else |
Joe Perches | 2ac1ea7 | 2008-03-23 01:01:37 -0700 | [diff] [blame] | 144 | static inline void apply_paravirt(struct paravirt_patch_site *start, |
| 145 | struct paravirt_patch_site *end) |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 146 | {} |
| 147 | #define __parainstructions NULL |
| 148 | #define __parainstructions_end NULL |
| 149 | #endif |
| 150 | |
Mathieu Desnoyers | e587cad | 2008-03-06 08:48:49 -0500 | [diff] [blame] | 151 | /* |
| 152 | * Clear and restore the kernel write-protection flag on the local CPU. |
| 153 | * Allows the kernel to edit read-only pages. |
| 154 | * Side-effect: any interrupt handler running between save and restore will have |
| 155 | * the ability to write to read-only pages. |
| 156 | * |
| 157 | * Warning: |
| 158 | * Code patching in the UP case is safe if NMIs and MCE handlers are stopped and |
| 159 | * no thread can be preempted in the instructions being modified (no iret to an |
| 160 | * invalid instruction possible) or if the instructions are changed from a |
| 161 | * consistent state to another consistent state atomically. |
| 162 | * More care must be taken when modifying code in the SMP case because of |
| 163 | * Intel's errata. |
| 164 | * On the local CPU you need to be protected again NMI or MCE handlers seeing an |
| 165 | * inconsistent instruction while you patch. |
Mathieu Desnoyers | e587cad | 2008-03-06 08:48:49 -0500 | [diff] [blame] | 166 | */ |
Mathieu Desnoyers | e587cad | 2008-03-06 08:48:49 -0500 | [diff] [blame] | 167 | extern void *text_poke(void *addr, const void *opcode, size_t len); |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 168 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 169 | #endif /* _ASM_X86_ALTERNATIVE_H */ |