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 | |
Andy Lutomirski | f005f5d | 2016-04-26 12:23:25 -0700 | [diff] [blame] | 4 | #ifndef __ASSEMBLY__ |
| 5 | |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 6 | #include <linux/types.h> |
| 7 | #include <linux/stddef.h> |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 8 | #include <linux/stringify.h> |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 9 | #include <asm/asm.h> |
| 10 | |
| 11 | /* |
| 12 | * Alternative inline assembly for SMP. |
| 13 | * |
| 14 | * The LOCK_PREFIX macro defined here replaces the LOCK and |
| 15 | * LOCK_PREFIX macros used everywhere in the source tree. |
| 16 | * |
| 17 | * SMP alternatives use the same data structures as the other |
| 18 | * alternatives and the X86_FEATURE_UP flag to indicate the case of a |
| 19 | * UP system running a SMP kernel. The existing apply_alternatives() |
| 20 | * works fine for patching a SMP kernel for UP. |
| 21 | * |
| 22 | * The SMP alternative tables can be kept after boot and contain both |
| 23 | * UP and SMP versions of the instructions to allow switching back to |
| 24 | * SMP at runtime, when hotplugging in a new CPU, which is especially |
| 25 | * useful in virtualized environments. |
| 26 | * |
| 27 | * The very common lock prefix is handled as special case in a |
| 28 | * separate table which is a pure address list without replacement ptr |
| 29 | * and size information. That keeps the table sizes small. |
| 30 | */ |
| 31 | |
| 32 | #ifdef CONFIG_SMP |
Luca Barbieri | b3ac891 | 2010-02-24 10:54:22 +0100 | [diff] [blame] | 33 | #define LOCK_PREFIX_HERE \ |
H. Peter Anvin | 9cebed4 | 2012-09-21 12:43:08 -0700 | [diff] [blame] | 34 | ".pushsection .smp_locks,\"a\"\n" \ |
| 35 | ".balign 4\n" \ |
| 36 | ".long 671f - .\n" /* offset */ \ |
| 37 | ".popsection\n" \ |
Luca Barbieri | b3ac891 | 2010-02-24 10:54:22 +0100 | [diff] [blame] | 38 | "671:" |
| 39 | |
| 40 | #define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; " |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 41 | |
| 42 | #else /* ! CONFIG_SMP */ |
H. Peter Anvin | b701a47 | 2010-04-29 16:03:57 -0700 | [diff] [blame] | 43 | #define LOCK_PREFIX_HERE "" |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 44 | #define LOCK_PREFIX "" |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 45 | #endif |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 46 | |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 47 | struct alt_instr { |
Andy Lutomirski | 59e97e4 | 2011-07-13 09:24:10 -0400 | [diff] [blame] | 48 | s32 instr_offset; /* original instruction */ |
| 49 | s32 repl_offset; /* offset to replacement instruction */ |
H. Peter Anvin | 83a7a2a | 2010-06-10 00:10:43 +0000 | [diff] [blame] | 50 | u16 cpuid; /* cpuid bit set for replacement */ |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 51 | u8 instrlen; /* length of original instruction */ |
Borislav Petkov | 4332195 | 2014-12-27 10:41:52 +0100 | [diff] [blame] | 52 | u8 replacementlen; /* length of new instruction */ |
| 53 | u8 padlen; /* length of build-time padding */ |
| 54 | } __packed; |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 55 | |
Ingo Molnar | 5e907bb | 2015-04-30 09:09:26 +0200 | [diff] [blame] | 56 | /* |
| 57 | * Debug flag that can be tested to see whether alternative |
| 58 | * instructions were patched in already: |
| 59 | */ |
| 60 | extern int alternatives_patched; |
| 61 | |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 62 | extern void alternative_instructions(void); |
| 63 | extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end); |
| 64 | |
| 65 | struct module; |
| 66 | |
| 67 | #ifdef CONFIG_SMP |
| 68 | extern void alternatives_smp_module_add(struct module *mod, char *name, |
| 69 | void *locks, void *locks_end, |
| 70 | void *text, void *text_end); |
| 71 | extern void alternatives_smp_module_del(struct module *mod); |
Rusty Russell | 816afe4 | 2012-08-06 17:29:49 +0930 | [diff] [blame] | 72 | extern void alternatives_enable_smp(void); |
Masami Hiramatsu | 2cfa197 | 2010-02-02 16:49:11 -0500 | [diff] [blame] | 73 | extern int alternatives_text_reserved(void *start, void *end); |
Suresh Siddha | 3fb82d5 | 2010-11-23 16:11:40 -0800 | [diff] [blame] | 74 | extern bool skip_smp_alternatives; |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 75 | #else |
| 76 | static inline void alternatives_smp_module_add(struct module *mod, char *name, |
Joe Perches | 2ac1ea7 | 2008-03-23 01:01:37 -0700 | [diff] [blame] | 77 | void *locks, void *locks_end, |
| 78 | void *text, void *text_end) {} |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 79 | static inline void alternatives_smp_module_del(struct module *mod) {} |
Rusty Russell | 816afe4 | 2012-08-06 17:29:49 +0930 | [diff] [blame] | 80 | static inline void alternatives_enable_smp(void) {} |
Masami Hiramatsu | 2cfa197 | 2010-02-02 16:49:11 -0500 | [diff] [blame] | 81 | static inline int alternatives_text_reserved(void *start, void *end) |
| 82 | { |
| 83 | return 0; |
| 84 | } |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 85 | #endif /* CONFIG_SMP */ |
| 86 | |
Borislav Petkov | 4332195 | 2014-12-27 10:41:52 +0100 | [diff] [blame] | 87 | #define b_replacement(num) "664"#num |
| 88 | #define e_replacement(num) "665"#num |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 89 | |
Borislav Petkov | 4332195 | 2014-12-27 10:41:52 +0100 | [diff] [blame] | 90 | #define alt_end_marker "663" |
| 91 | #define alt_slen "662b-661b" |
| 92 | #define alt_pad_len alt_end_marker"b-662b" |
| 93 | #define alt_total_slen alt_end_marker"b-661b" |
| 94 | #define alt_rlen(num) e_replacement(num)"f-"b_replacement(num)"f" |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 95 | |
Borislav Petkov | 4332195 | 2014-12-27 10:41:52 +0100 | [diff] [blame] | 96 | #define __OLDINSTR(oldinstr, num) \ |
| 97 | "661:\n\t" oldinstr "\n662:\n" \ |
| 98 | ".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * " \ |
| 99 | "((" alt_rlen(num) ")-(" alt_slen ")),0x90\n" |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 100 | |
Borislav Petkov | 4332195 | 2014-12-27 10:41:52 +0100 | [diff] [blame] | 101 | #define OLDINSTR(oldinstr, num) \ |
| 102 | __OLDINSTR(oldinstr, num) \ |
| 103 | alt_end_marker ":\n" |
| 104 | |
| 105 | /* |
Borislav Petkov | dbe4058 | 2015-04-04 15:34:43 +0200 | [diff] [blame] | 106 | * max without conditionals. Idea adapted from: |
| 107 | * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax |
| 108 | * |
| 109 | * The additional "-" is needed because gas works with s32s. |
| 110 | */ |
| 111 | #define alt_max_short(a, b) "((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") - (" b ")))))" |
| 112 | |
| 113 | /* |
Borislav Petkov | 4332195 | 2014-12-27 10:41:52 +0100 | [diff] [blame] | 114 | * Pad the second replacement alternative with additional NOPs if it is |
| 115 | * additionally longer than the first replacement alternative. |
| 116 | */ |
Borislav Petkov | dbe4058 | 2015-04-04 15:34:43 +0200 | [diff] [blame] | 117 | #define OLDINSTR_2(oldinstr, num1, num2) \ |
| 118 | "661:\n\t" oldinstr "\n662:\n" \ |
| 119 | ".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * " \ |
| 120 | "(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n" \ |
Borislav Petkov | 4332195 | 2014-12-27 10:41:52 +0100 | [diff] [blame] | 121 | alt_end_marker ":\n" |
| 122 | |
| 123 | #define ALTINSTR_ENTRY(feature, num) \ |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 124 | " .long 661b - .\n" /* label */ \ |
Borislav Petkov | 4332195 | 2014-12-27 10:41:52 +0100 | [diff] [blame] | 125 | " .long " b_replacement(num)"f - .\n" /* new instruction */ \ |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 126 | " .word " __stringify(feature) "\n" /* feature bit */ \ |
Borislav Petkov | 4332195 | 2014-12-27 10:41:52 +0100 | [diff] [blame] | 127 | " .byte " alt_total_slen "\n" /* source len */ \ |
| 128 | " .byte " alt_rlen(num) "\n" /* replacement len */ \ |
| 129 | " .byte " alt_pad_len "\n" /* pad len */ |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 130 | |
Borislav Petkov | 4332195 | 2014-12-27 10:41:52 +0100 | [diff] [blame] | 131 | #define ALTINSTR_REPLACEMENT(newinstr, feature, num) /* replacement */ \ |
| 132 | b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n\t" |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 133 | |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 134 | /* alternative assembly primitive: */ |
| 135 | #define ALTERNATIVE(oldinstr, newinstr, feature) \ |
Borislav Petkov | 4332195 | 2014-12-27 10:41:52 +0100 | [diff] [blame] | 136 | OLDINSTR(oldinstr, 1) \ |
H. Peter Anvin | 9cebed4 | 2012-09-21 12:43:08 -0700 | [diff] [blame] | 137 | ".pushsection .altinstructions,\"a\"\n" \ |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 138 | ALTINSTR_ENTRY(feature, 1) \ |
H. Peter Anvin | 9cebed4 | 2012-09-21 12:43:08 -0700 | [diff] [blame] | 139 | ".popsection\n" \ |
H. Peter Anvin | 9cebed4 | 2012-09-21 12:43:08 -0700 | [diff] [blame] | 140 | ".pushsection .altinstr_replacement, \"ax\"\n" \ |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 141 | ALTINSTR_REPLACEMENT(newinstr, feature, 1) \ |
H. Peter Anvin | 9cebed4 | 2012-09-21 12:43:08 -0700 | [diff] [blame] | 142 | ".popsection" |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 143 | |
| 144 | #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\ |
Borislav Petkov | 4332195 | 2014-12-27 10:41:52 +0100 | [diff] [blame] | 145 | OLDINSTR_2(oldinstr, 1, 2) \ |
H. Peter Anvin | 9cebed4 | 2012-09-21 12:43:08 -0700 | [diff] [blame] | 146 | ".pushsection .altinstructions,\"a\"\n" \ |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 147 | ALTINSTR_ENTRY(feature1, 1) \ |
| 148 | ALTINSTR_ENTRY(feature2, 2) \ |
H. Peter Anvin | 9cebed4 | 2012-09-21 12:43:08 -0700 | [diff] [blame] | 149 | ".popsection\n" \ |
H. Peter Anvin | 9cebed4 | 2012-09-21 12:43:08 -0700 | [diff] [blame] | 150 | ".pushsection .altinstr_replacement, \"ax\"\n" \ |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 151 | ALTINSTR_REPLACEMENT(newinstr1, feature1, 1) \ |
| 152 | ALTINSTR_REPLACEMENT(newinstr2, feature2, 2) \ |
H. Peter Anvin | 9cebed4 | 2012-09-21 12:43:08 -0700 | [diff] [blame] | 153 | ".popsection" |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 154 | |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 155 | /* |
| 156 | * Alternative instructions for different CPU types or capabilities. |
| 157 | * |
| 158 | * This allows to use optimized instructions even on generic binary |
| 159 | * kernels. |
| 160 | * |
| 161 | * length of oldinstr must be longer or equal the length of newinstr |
| 162 | * It can be padded with nops as needed. |
| 163 | * |
| 164 | * For non barrier like inlines please define new variants |
| 165 | * without volatile and memory clobber. |
| 166 | */ |
| 167 | #define alternative(oldinstr, newinstr, feature) \ |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 168 | asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory") |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 169 | |
Borislav Petkov | 4332195 | 2014-12-27 10:41:52 +0100 | [diff] [blame] | 170 | #define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \ |
| 171 | asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory") |
| 172 | |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 173 | /* |
| 174 | * Alternative inline assembly with input. |
| 175 | * |
| 176 | * Pecularities: |
| 177 | * No memory clobber here. |
| 178 | * Argument numbers start with 1. |
| 179 | * Best is to use constraints that are fixed size (like (%1) ... "r") |
| 180 | * If you use variable sized constraints like "m" or "g" in the |
| 181 | * replacement make sure to pad to the worst case length. |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 182 | * Leaving an unused argument 0 to keep API compatibility. |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 183 | */ |
| 184 | #define alternative_input(oldinstr, newinstr, feature, input...) \ |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 185 | asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \ |
| 186 | : : "i" (0), ## input) |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 187 | |
Fenghua Yu | 5b3e83f | 2014-05-29 11:12:32 -0700 | [diff] [blame] | 188 | /* |
| 189 | * This is similar to alternative_input. But it has two features and |
| 190 | * respective instructions. |
| 191 | * |
| 192 | * If CPU has feature2, newinstr2 is used. |
| 193 | * Otherwise, if CPU has feature1, newinstr1 is used. |
| 194 | * Otherwise, oldinstr is used. |
| 195 | */ |
| 196 | #define alternative_input_2(oldinstr, newinstr1, feature1, newinstr2, \ |
| 197 | feature2, input...) \ |
| 198 | asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, \ |
| 199 | newinstr2, feature2) \ |
| 200 | : : "i" (0), ## input) |
| 201 | |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 202 | /* Like alternative_input, but with a single output argument */ |
| 203 | #define alternative_io(oldinstr, newinstr, feature, output, input...) \ |
Mathieu Desnoyers | edc953f | 2009-04-28 11:13:46 -0400 | [diff] [blame] | 204 | asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \ |
| 205 | : output : "i" (0), ## input) |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 206 | |
Jan Beulich | 1b1d925 | 2009-12-18 16:12:56 +0000 | [diff] [blame] | 207 | /* Like alternative_io, but for replacing a direct call with another one. */ |
| 208 | #define alternative_call(oldfunc, newfunc, feature, output, input...) \ |
| 209 | asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \ |
| 210 | : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input) |
| 211 | |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 212 | /* |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 213 | * Like alternative_call, but there are two features and respective functions. |
| 214 | * If CPU has feature2, function2 is used. |
| 215 | * Otherwise, if CPU has feature1, function1 is used. |
| 216 | * Otherwise, old function is used. |
| 217 | */ |
| 218 | #define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2, \ |
| 219 | output, input...) \ |
Josh Poimboeuf | 317c2ce | 2016-09-23 16:49:39 -0500 | [diff] [blame] | 220 | { \ |
| 221 | register void *__sp asm(_ASM_SP); \ |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 222 | asm volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\ |
| 223 | "call %P[new2]", feature2) \ |
Josh Poimboeuf | 317c2ce | 2016-09-23 16:49:39 -0500 | [diff] [blame] | 224 | : output, "+r" (__sp) \ |
| 225 | : [old] "i" (oldfunc), [new1] "i" (newfunc1), \ |
| 226 | [new2] "i" (newfunc2), ## input); \ |
| 227 | } |
Fenghua Yu | 954e482 | 2012-05-24 18:19:45 -0700 | [diff] [blame] | 228 | |
| 229 | /* |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 230 | * use this macro(s) if you need more than one output parameter |
| 231 | * in alternative_io |
| 232 | */ |
Jan Beulich | 1b1d925 | 2009-12-18 16:12:56 +0000 | [diff] [blame] | 233 | #define ASM_OUTPUT2(a...) a |
H. Peter Anvin | 6b59257 | 2008-01-30 13:30:30 +0100 | [diff] [blame] | 234 | |
Jan Beulich | 819165f | 2012-01-20 16:21:41 +0000 | [diff] [blame] | 235 | /* |
| 236 | * use this macro if you need clobbers but no inputs in |
| 237 | * alternative_{input,io,call}() |
| 238 | */ |
| 239 | #define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr |
| 240 | |
Andy Lutomirski | f005f5d | 2016-04-26 12:23:25 -0700 | [diff] [blame] | 241 | #endif /* __ASSEMBLY__ */ |
| 242 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 243 | #endif /* _ASM_X86_ALTERNATIVE_H */ |