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