1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _ASM_X86_ALTERNATIVE_H 3 #define _ASM_X86_ALTERNATIVE_H 4 5 #include <linux/types.h> 6 #include <linux/stringify.h> 7 #include <asm/asm.h> 8 9 #define ALT_FLAGS_SHIFT 16 10 11 #define ALT_FLAG_NOT (1 << 0) 12 #define ALT_NOT(feature) ((ALT_FLAG_NOT << ALT_FLAGS_SHIFT) | (feature)) 13 #define ALT_FLAG_DIRECT_CALL (1 << 1) 14 #define ALT_DIRECT_CALL(feature) ((ALT_FLAG_DIRECT_CALL << ALT_FLAGS_SHIFT) | (feature)) 15 #define ALT_CALL_ALWAYS ALT_DIRECT_CALL(X86_FEATURE_ALWAYS) 16 17 #ifndef __ASSEMBLY__ 18 19 #include <linux/stddef.h> 20 21 /* 22 * Alternative inline assembly for SMP. 23 * 24 * The LOCK_PREFIX macro defined here replaces the LOCK and 25 * LOCK_PREFIX macros used everywhere in the source tree. 26 * 27 * SMP alternatives use the same data structures as the other 28 * alternatives and the X86_FEATURE_UP flag to indicate the case of a 29 * UP system running a SMP kernel. The existing apply_alternatives() 30 * works fine for patching a SMP kernel for UP. 31 * 32 * The SMP alternative tables can be kept after boot and contain both 33 * UP and SMP versions of the instructions to allow switching back to 34 * SMP at runtime, when hotplugging in a new CPU, which is especially 35 * useful in virtualized environments. 36 * 37 * The very common lock prefix is handled as special case in a 38 * separate table which is a pure address list without replacement ptr 39 * and size information. That keeps the table sizes small. 40 */ 41 42 #ifdef CONFIG_SMP 43 #define LOCK_PREFIX_HERE \ 44 ".pushsection .smp_locks,\"a\"\n" \ 45 ".balign 4\n" \ 46 ".long 671f - .\n" /* offset */ \ 47 ".popsection\n" \ 48 "671:" 49 50 #define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; " 51 52 #else /* ! CONFIG_SMP */ 53 #define LOCK_PREFIX_HERE "" 54 #define LOCK_PREFIX "" 55 #endif 56 57 /* 58 * objtool annotation to ignore the alternatives and only consider the original 59 * instruction(s). 60 */ 61 #define ANNOTATE_IGNORE_ALTERNATIVE \ 62 "999:\n\t" \ 63 ".pushsection .discard.ignore_alts\n\t" \ 64 ".long 999b\n\t" \ 65 ".popsection\n\t" 66 67 /* 68 * The patching flags are part of the upper bits of the @ft_flags parameter when 69 * specifying them. The split is currently like this: 70 * 71 * [31... flags ...16][15... CPUID feature bit ...0] 72 * 73 * but since this is all hidden in the macros argument being split, those fields can be 74 * extended in the future to fit in a u64 or however the need arises. 75 */ 76 struct alt_instr { 77 s32 instr_offset; /* original instruction */ 78 s32 repl_offset; /* offset to replacement instruction */ 79 80 union { 81 struct { 82 u32 cpuid: 16; /* CPUID bit set for replacement */ 83 u32 flags: 16; /* patching control flags */ 84 }; 85 u32 ft_flags; 86 }; 87 88 u8 instrlen; /* length of original instruction */ 89 u8 replacementlen; /* length of new instruction */ 90 } __packed; 91 92 extern struct alt_instr __alt_instructions[], __alt_instructions_end[]; 93 94 /* 95 * Debug flag that can be tested to see whether alternative 96 * instructions were patched in already: 97 */ 98 extern int alternatives_patched; 99 100 extern void alternative_instructions(void); 101 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end); 102 extern void apply_retpolines(s32 *start, s32 *end); 103 extern void apply_returns(s32 *start, s32 *end); 104 extern void apply_seal_endbr(s32 *start, s32 *end); 105 extern void apply_fineibt(s32 *start_retpoline, s32 *end_retpoine, 106 s32 *start_cfi, s32 *end_cfi); 107 108 struct module; 109 110 struct callthunk_sites { 111 s32 *call_start, *call_end; 112 struct alt_instr *alt_start, *alt_end; 113 }; 114 115 #ifdef CONFIG_CALL_THUNKS 116 extern void callthunks_patch_builtin_calls(void); 117 extern void callthunks_patch_module_calls(struct callthunk_sites *sites, 118 struct module *mod); 119 extern void *callthunks_translate_call_dest(void *dest); 120 extern int x86_call_depth_emit_accounting(u8 **pprog, void *func, void *ip); 121 #else 122 static __always_inline void callthunks_patch_builtin_calls(void) {} 123 static __always_inline void 124 callthunks_patch_module_calls(struct callthunk_sites *sites, 125 struct module *mod) {} 126 static __always_inline void *callthunks_translate_call_dest(void *dest) 127 { 128 return dest; 129 } 130 static __always_inline int x86_call_depth_emit_accounting(u8 **pprog, 131 void *func, void *ip) 132 { 133 return 0; 134 } 135 #endif 136 137 #ifdef CONFIG_SMP 138 extern void alternatives_smp_module_add(struct module *mod, char *name, 139 void *locks, void *locks_end, 140 void *text, void *text_end); 141 extern void alternatives_smp_module_del(struct module *mod); 142 extern void alternatives_enable_smp(void); 143 extern int alternatives_text_reserved(void *start, void *end); 144 extern bool skip_smp_alternatives; 145 #else 146 static inline void alternatives_smp_module_add(struct module *mod, char *name, 147 void *locks, void *locks_end, 148 void *text, void *text_end) {} 149 static inline void alternatives_smp_module_del(struct module *mod) {} 150 static inline void alternatives_enable_smp(void) {} 151 static inline int alternatives_text_reserved(void *start, void *end) 152 { 153 return 0; 154 } 155 #endif /* CONFIG_SMP */ 156 157 #define ALT_CALL_INSTR "call BUG_func" 158 159 #define alt_slen "772b-771b" 160 #define alt_total_slen "773b-771b" 161 #define alt_rlen "775f-774f" 162 163 #define OLDINSTR(oldinstr) \ 164 "# ALT: oldinstr\n" \ 165 "771:\n\t" oldinstr "\n772:\n" \ 166 "# ALT: padding\n" \ 167 ".skip -(((" alt_rlen ")-(" alt_slen ")) > 0) * " \ 168 "((" alt_rlen ")-(" alt_slen ")),0x90\n" \ 169 "773:\n" 170 171 #define ALTINSTR_ENTRY(ft_flags) \ 172 ".pushsection .altinstructions,\"a\"\n" \ 173 " .long 771b - .\n" /* label */ \ 174 " .long 774f - .\n" /* new instruction */ \ 175 " .4byte " __stringify(ft_flags) "\n" /* feature + flags */ \ 176 " .byte " alt_total_slen "\n" /* source len */ \ 177 " .byte " alt_rlen "\n" /* replacement len */ \ 178 ".popsection\n" 179 180 #define ALTINSTR_REPLACEMENT(newinstr) /* replacement */ \ 181 ".pushsection .altinstr_replacement, \"ax\"\n" \ 182 "# ALT: replacement\n" \ 183 "774:\n\t" newinstr "\n775:\n" \ 184 ".popsection\n" 185 186 /* alternative assembly primitive: */ 187 #define ALTERNATIVE(oldinstr, newinstr, ft_flags) \ 188 OLDINSTR(oldinstr) \ 189 ALTINSTR_ENTRY(ft_flags) \ 190 ALTINSTR_REPLACEMENT(newinstr) 191 192 #define ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) \ 193 ALTERNATIVE(ALTERNATIVE(oldinstr, newinstr1, ft_flags1), newinstr2, ft_flags2) 194 195 /* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */ 196 #define ALTERNATIVE_TERNARY(oldinstr, ft_flags, newinstr_yes, newinstr_no) \ 197 ALTERNATIVE_2(oldinstr, newinstr_no, X86_FEATURE_ALWAYS, newinstr_yes, ft_flags) 198 199 #define ALTERNATIVE_3(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2, \ 200 newinstr3, ft_flags3) \ 201 ALTERNATIVE(ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2), \ 202 newinstr3, ft_flags3) 203 204 /* 205 * Alternative instructions for different CPU types or capabilities. 206 * 207 * This allows to use optimized instructions even on generic binary 208 * kernels. 209 * 210 * length of oldinstr must be longer or equal the length of newinstr 211 * It can be padded with nops as needed. 212 * 213 * For non barrier like inlines please define new variants 214 * without volatile and memory clobber. 215 */ 216 #define alternative(oldinstr, newinstr, ft_flags) \ 217 asm_inline volatile(ALTERNATIVE(oldinstr, newinstr, ft_flags) : : : "memory") 218 219 #define alternative_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) \ 220 asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) ::: "memory") 221 222 /* 223 * Alternative inline assembly with input. 224 * 225 * Peculiarities: 226 * No memory clobber here. 227 * Argument numbers start with 1. 228 * Leaving an unused argument 0 to keep API compatibility. 229 */ 230 #define alternative_input(oldinstr, newinstr, ft_flags, input...) \ 231 asm_inline volatile(ALTERNATIVE(oldinstr, newinstr, ft_flags) \ 232 : : "i" (0), ## input) 233 234 /* Like alternative_input, but with a single output argument */ 235 #define alternative_io(oldinstr, newinstr, ft_flags, output, input...) \ 236 asm_inline volatile(ALTERNATIVE(oldinstr, newinstr, ft_flags) \ 237 : output : "i" (0), ## input) 238 239 /* 240 * Like alternative_io, but for replacing a direct call with another one. 241 * 242 * Use the %c operand modifier which is the generic way to print a bare 243 * constant expression with all syntax-specific punctuation omitted. %P 244 * is the x86-specific variant which can handle constants too, for 245 * historical reasons, but it should be used primarily for PIC 246 * references: i.e., if used for a function, it would add the PLT 247 * suffix. 248 */ 249 #define alternative_call(oldfunc, newfunc, ft_flags, output, input...) \ 250 asm_inline volatile(ALTERNATIVE("call %c[old]", "call %c[new]", ft_flags) \ 251 : ALT_OUTPUT_SP(output) \ 252 : [old] "i" (oldfunc), [new] "i" (newfunc), ## input) 253 254 /* 255 * Like alternative_call, but there are two features and respective functions. 256 * If CPU has feature2, function2 is used. 257 * Otherwise, if CPU has feature1, function1 is used. 258 * Otherwise, old function is used. 259 */ 260 #define alternative_call_2(oldfunc, newfunc1, ft_flags1, newfunc2, ft_flags2, \ 261 output, input...) \ 262 asm_inline volatile(ALTERNATIVE_2("call %c[old]", "call %c[new1]", ft_flags1, \ 263 "call %c[new2]", ft_flags2) \ 264 : ALT_OUTPUT_SP(output) \ 265 : [old] "i" (oldfunc), [new1] "i" (newfunc1), \ 266 [new2] "i" (newfunc2), ## input) 267 268 /* 269 * use this macro(s) if you need more than one output parameter 270 * in alternative_io 271 */ 272 #define ASM_OUTPUT2(a...) a 273 274 /* 275 * use this macro if you need clobbers but no inputs in 276 * alternative_{input,io,call}() 277 */ 278 #define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr 279 280 #define ALT_OUTPUT_SP(...) ASM_CALL_CONSTRAINT, ## __VA_ARGS__ 281 282 /* Macro for creating assembler functions avoiding any C magic. */ 283 #define DEFINE_ASM_FUNC(func, instr, sec) \ 284 asm (".pushsection " #sec ", \"ax\"\n" \ 285 ".global " #func "\n\t" \ 286 ".type " #func ", @function\n\t" \ 287 ASM_FUNC_ALIGN "\n" \ 288 #func ":\n\t" \ 289 ASM_ENDBR \ 290 instr "\n\t" \ 291 ASM_RET \ 292 ".size " #func ", . - " #func "\n\t" \ 293 ".popsection") 294 295 void BUG_func(void); 296 void nop_func(void); 297 298 #else /* __ASSEMBLY__ */ 299 300 #ifdef CONFIG_SMP 301 .macro LOCK_PREFIX 302 672: lock 303 .pushsection .smp_locks,"a" 304 .balign 4 305 .long 672b - . 306 .popsection 307 .endm 308 #else 309 .macro LOCK_PREFIX 310 .endm 311 #endif 312 313 /* 314 * objtool annotation to ignore the alternatives and only consider the original 315 * instruction(s). 316 */ 317 .macro ANNOTATE_IGNORE_ALTERNATIVE 318 .Lannotate_\@: 319 .pushsection .discard.ignore_alts 320 .long .Lannotate_\@ 321 .popsection 322 .endm 323 324 /* 325 * Issue one struct alt_instr descriptor entry (need to put it into 326 * the section .altinstructions, see below). This entry contains 327 * enough information for the alternatives patching code to patch an 328 * instruction. See apply_alternatives(). 329 */ 330 .macro altinstr_entry orig alt ft_flags orig_len alt_len 331 .long \orig - . 332 .long \alt - . 333 .4byte \ft_flags 334 .byte \orig_len 335 .byte \alt_len 336 .endm 337 338 .macro ALT_CALL_INSTR 339 call BUG_func 340 .endm 341 342 /* 343 * Define an alternative between two instructions. If @feature is 344 * present, early code in apply_alternatives() replaces @oldinstr with 345 * @newinstr. ".skip" directive takes care of proper instruction padding 346 * in case @newinstr is longer than @oldinstr. 347 */ 348 #define __ALTERNATIVE(oldinst, newinst, flag) \ 349 740: \ 350 oldinst ; \ 351 741: \ 352 .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;\ 353 742: \ 354 .pushsection .altinstructions,"a" ; \ 355 altinstr_entry 740b,743f,flag,742b-740b,744f-743f ; \ 356 .popsection ; \ 357 .pushsection .altinstr_replacement,"ax" ; \ 358 743: \ 359 newinst ; \ 360 744: \ 361 .popsection ; 362 363 .macro ALTERNATIVE oldinstr, newinstr, ft_flags 364 __ALTERNATIVE(\oldinstr, \newinstr, \ft_flags) 365 .endm 366 367 #define old_len 141b-140b 368 #define new_len1 144f-143f 369 #define new_len2 145f-144f 370 #define new_len3 146f-145f 371 372 /* 373 * Same as ALTERNATIVE macro above but for two alternatives. If CPU 374 * has @feature1, it replaces @oldinstr with @newinstr1. If CPU has 375 * @feature2, it replaces @oldinstr with @feature2. 376 */ 377 .macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2 378 __ALTERNATIVE(__ALTERNATIVE(\oldinstr, \newinstr1, \ft_flags1), 379 \newinstr2, \ft_flags2) 380 .endm 381 382 .macro ALTERNATIVE_3 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2, newinstr3, ft_flags3 383 __ALTERNATIVE(ALTERNATIVE_2(\oldinstr, \newinstr1, \ft_flags1, \newinstr2, \ft_flags2), 384 \newinstr3, \ft_flags3) 385 .endm 386 387 /* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */ 388 #define ALTERNATIVE_TERNARY(oldinstr, ft_flags, newinstr_yes, newinstr_no) \ 389 ALTERNATIVE_2 oldinstr, newinstr_no, X86_FEATURE_ALWAYS, \ 390 newinstr_yes, ft_flags 391 392 #endif /* __ASSEMBLY__ */ 393 394 #endif /* _ASM_X86_ALTERNATIVE_H */ 395