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