Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 1 | #ifndef _I386_ALTERNATIVE_H |
| 2 | #define _I386_ALTERNATIVE_H |
| 3 | |
| 4 | #ifdef __KERNEL__ |
| 5 | |
Kirill Smelkov | 30343d6 | 2006-06-23 02:04:33 -0700 | [diff] [blame] | 6 | #include <asm/types.h> |
| 7 | |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 8 | struct alt_instr { |
| 9 | u8 *instr; /* original instruction */ |
| 10 | u8 *replacement; |
| 11 | u8 cpuid; /* cpuid bit set for replacement */ |
| 12 | u8 instrlen; /* length of original instruction */ |
| 13 | u8 replacementlen; /* length of new instruction, <= instrlen */ |
| 14 | u8 pad; |
| 15 | }; |
| 16 | |
| 17 | extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end); |
| 18 | |
| 19 | struct module; |
| 20 | extern void alternatives_smp_module_add(struct module *mod, char *name, |
| 21 | void *locks, void *locks_end, |
| 22 | void *text, void *text_end); |
| 23 | extern void alternatives_smp_module_del(struct module *mod); |
| 24 | extern void alternatives_smp_switch(int smp); |
| 25 | |
| 26 | #endif |
| 27 | |
| 28 | /* |
| 29 | * Alternative instructions for different CPU types or capabilities. |
| 30 | * |
| 31 | * This allows to use optimized instructions even on generic binary |
| 32 | * kernels. |
| 33 | * |
| 34 | * length of oldinstr must be longer or equal the length of newinstr |
| 35 | * It can be padded with nops as needed. |
| 36 | * |
| 37 | * For non barrier like inlines please define new variants |
| 38 | * without volatile and memory clobber. |
| 39 | */ |
| 40 | #define alternative(oldinstr, newinstr, feature) \ |
| 41 | asm volatile ("661:\n\t" oldinstr "\n662:\n" \ |
| 42 | ".section .altinstructions,\"a\"\n" \ |
| 43 | " .align 4\n" \ |
| 44 | " .long 661b\n" /* label */ \ |
| 45 | " .long 663f\n" /* new instruction */ \ |
| 46 | " .byte %c0\n" /* feature bit */ \ |
| 47 | " .byte 662b-661b\n" /* sourcelen */ \ |
| 48 | " .byte 664f-663f\n" /* replacementlen */ \ |
| 49 | ".previous\n" \ |
| 50 | ".section .altinstr_replacement,\"ax\"\n" \ |
| 51 | "663:\n\t" newinstr "\n664:\n" /* replacement */\ |
| 52 | ".previous" :: "i" (feature) : "memory") |
| 53 | |
| 54 | /* |
| 55 | * Alternative inline assembly with input. |
| 56 | * |
| 57 | * Pecularities: |
| 58 | * No memory clobber here. |
| 59 | * Argument numbers start with 1. |
| 60 | * Best is to use constraints that are fixed size (like (%1) ... "r") |
| 61 | * If you use variable sized constraints like "m" or "g" in the |
| 62 | * replacement maake sure to pad to the worst case length. |
| 63 | */ |
| 64 | #define alternative_input(oldinstr, newinstr, feature, input...) \ |
| 65 | asm volatile ("661:\n\t" oldinstr "\n662:\n" \ |
| 66 | ".section .altinstructions,\"a\"\n" \ |
| 67 | " .align 4\n" \ |
| 68 | " .long 661b\n" /* label */ \ |
| 69 | " .long 663f\n" /* new instruction */ \ |
| 70 | " .byte %c0\n" /* feature bit */ \ |
| 71 | " .byte 662b-661b\n" /* sourcelen */ \ |
| 72 | " .byte 664f-663f\n" /* replacementlen */ \ |
| 73 | ".previous\n" \ |
| 74 | ".section .altinstr_replacement,\"ax\"\n" \ |
| 75 | "663:\n\t" newinstr "\n664:\n" /* replacement */\ |
| 76 | ".previous" :: "i" (feature), ##input) |
| 77 | |
| 78 | /* |
| 79 | * Alternative inline assembly for SMP. |
| 80 | * |
| 81 | * alternative_smp() takes two versions (SMP first, UP second) and is |
| 82 | * for more complex stuff such as spinlocks. |
| 83 | * |
| 84 | * The LOCK_PREFIX macro defined here replaces the LOCK and |
| 85 | * LOCK_PREFIX macros used everywhere in the source tree. |
| 86 | * |
| 87 | * SMP alternatives use the same data structures as the other |
| 88 | * alternatives and the X86_FEATURE_UP flag to indicate the case of a |
| 89 | * UP system running a SMP kernel. The existing apply_alternatives() |
| 90 | * works fine for patching a SMP kernel for UP. |
| 91 | * |
| 92 | * The SMP alternative tables can be kept after boot and contain both |
| 93 | * UP and SMP versions of the instructions to allow switching back to |
| 94 | * SMP at runtime, when hotplugging in a new CPU, which is especially |
| 95 | * useful in virtualized environments. |
| 96 | * |
| 97 | * The very common lock prefix is handled as special case in a |
| 98 | * separate table which is a pure address list without replacement ptr |
| 99 | * and size information. That keeps the table sizes small. |
| 100 | */ |
| 101 | |
| 102 | #ifdef CONFIG_SMP |
| 103 | #define alternative_smp(smpinstr, upinstr, args...) \ |
| 104 | asm volatile ("661:\n\t" smpinstr "\n662:\n" \ |
| 105 | ".section .smp_altinstructions,\"a\"\n" \ |
| 106 | " .align 4\n" \ |
| 107 | " .long 661b\n" /* label */ \ |
| 108 | " .long 663f\n" /* new instruction */ \ |
| 109 | " .byte 0x68\n" /* X86_FEATURE_UP */ \ |
| 110 | " .byte 662b-661b\n" /* sourcelen */ \ |
| 111 | " .byte 664f-663f\n" /* replacementlen */ \ |
| 112 | ".previous\n" \ |
| 113 | ".section .smp_altinstr_replacement,\"awx\"\n" \ |
| 114 | "663:\n\t" upinstr "\n" /* replacement */ \ |
| 115 | "664:\n\t.fill 662b-661b,1,0x42\n" /* space for original */ \ |
| 116 | ".previous" : args) |
| 117 | |
| 118 | #define LOCK_PREFIX \ |
| 119 | ".section .smp_locks,\"a\"\n" \ |
| 120 | " .align 4\n" \ |
| 121 | " .long 661f\n" /* address */ \ |
| 122 | ".previous\n" \ |
| 123 | "661:\n\tlock; " |
| 124 | |
| 125 | #else /* ! CONFIG_SMP */ |
| 126 | #define alternative_smp(smpinstr, upinstr, args...) \ |
| 127 | asm volatile (upinstr : args) |
| 128 | #define LOCK_PREFIX "" |
| 129 | #endif |
| 130 | |
| 131 | #endif /* _I386_ALTERNATIVE_H */ |