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 BIT(0) 12 #define ALT_NOT(feature) ((ALT_FLAG_NOT << ALT_FLAGS_SHIFT) | (feature)) 13 14 #ifndef __ASSEMBLY__ 15 16 #include <linux/stddef.h> 17 18 /* 19 * Alternative inline assembly for SMP. 20 * 21 * The LOCK_PREFIX macro defined here replaces the LOCK and 22 * LOCK_PREFIX macros used everywhere in the source tree. 23 * 24 * SMP alternatives use the same data structures as the other 25 * alternatives and the X86_FEATURE_UP flag to indicate the case of a 26 * UP system running a SMP kernel. The existing apply_alternatives() 27 * works fine for patching a SMP kernel for UP. 28 * 29 * The SMP alternative tables can be kept after boot and contain both 30 * UP and SMP versions of the instructions to allow switching back to 31 * SMP at runtime, when hotplugging in a new CPU, which is especially 32 * useful in virtualized environments. 33 * 34 * The very common lock prefix is handled as special case in a 35 * separate table which is a pure address list without replacement ptr 36 * and size information. That keeps the table sizes small. 37 */ 38 39 #ifdef CONFIG_SMP 40 #define LOCK_PREFIX_HERE \ 41 ".pushsection .smp_locks,\"a\"\n" \ 42 ".balign 4\n" \ 43 ".long 671f - .\n" /* offset */ \ 44 ".popsection\n" \ 45 "671:" 46 47 #define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; " 48 49 #else /* ! CONFIG_SMP */ 50 #define LOCK_PREFIX_HERE "" 51 #define LOCK_PREFIX "" 52 #endif 53 54 /* 55 * objtool annotation to ignore the alternatives and only consider the original 56 * instruction(s). 57 */ 58 #define ANNOTATE_IGNORE_ALTERNATIVE \ 59 "999:\n\t" \ 60 ".pushsection .discard.ignore_alts\n\t" \ 61 ".long 999b - .\n\t" \ 62 ".popsection\n\t" 63 64 /* 65 * The patching flags are part of the upper bits of the @ft_flags parameter when 66 * specifying them. The split is currently like this: 67 * 68 * [31... flags ...16][15... CPUID feature bit ...0] 69 * 70 * but since this is all hidden in the macros argument being split, those fields can be 71 * extended in the future to fit in a u64 or however the need arises. 72 */ 73 struct alt_instr { 74 s32 instr_offset; /* original instruction */ 75 s32 repl_offset; /* offset to replacement instruction */ 76 77 union { 78 struct { 79 u32 cpuid: 16; /* CPUID bit set for replacement */ 80 u32 flags: 16; /* patching control flags */ 81 }; 82 u32 ft_flags; 83 }; 84 85 u8 instrlen; /* length of original instruction */ 86 u8 replacementlen; /* length of new instruction */ 87 } __packed; 88 89 /* 90 * Debug flag that can be tested to see whether alternative 91 * instructions were patched in already: 92 */ 93 extern int alternatives_patched; 94 95 extern void alternative_instructions(void); 96 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end); 97 extern void apply_retpolines(s32 *start, s32 *end); 98 extern void apply_returns(s32 *start, s32 *end); 99 extern void apply_ibt_endbr(s32 *start, s32 *end); 100 extern void apply_fineibt(s32 *start_retpoline, s32 *end_retpoine, 101 s32 *start_cfi, s32 *end_cfi); 102 103 struct module; 104 struct paravirt_patch_site; 105 106 struct callthunk_sites { 107 s32 *call_start, *call_end; 108 struct paravirt_patch_site *pv_start, *pv_end; 109 }; 110 111 #ifdef CONFIG_CALL_THUNKS 112 extern void callthunks_patch_builtin_calls(void); 113 extern void callthunks_patch_module_calls(struct callthunk_sites *sites, 114 struct module *mod); 115 extern void *callthunks_translate_call_dest(void *dest); 116 extern bool is_callthunk(void *addr); 117 extern int x86_call_depth_emit_accounting(u8 **pprog, void *func); 118 #else 119 static __always_inline void callthunks_patch_builtin_calls(void) {} 120 static __always_inline void 121 callthunks_patch_module_calls(struct callthunk_sites *sites, 122 struct module *mod) {} 123 static __always_inline void *callthunks_translate_call_dest(void *dest) 124 { 125 return dest; 126 } 127 static __always_inline bool is_callthunk(void *addr) 128 { 129 return false; 130 } 131 static __always_inline int x86_call_depth_emit_accounting(u8 **pprog, 132 void *func) 133 { 134 return 0; 135 } 136 #endif 137 138 #ifdef CONFIG_SMP 139 extern void alternatives_smp_module_add(struct module *mod, char *name, 140 void *locks, void *locks_end, 141 void *text, void *text_end); 142 extern void alternatives_smp_module_del(struct module *mod); 143 extern void alternatives_enable_smp(void); 144 extern int alternatives_text_reserved(void *start, void *end); 145 extern bool skip_smp_alternatives; 146 #else 147 static inline void alternatives_smp_module_add(struct module *mod, char *name, 148 void *locks, void *locks_end, 149 void *text, void *text_end) {} 150 static inline void alternatives_smp_module_del(struct module *mod) {} 151 static inline void alternatives_enable_smp(void) {} 152 static inline int alternatives_text_reserved(void *start, void *end) 153 { 154 return 0; 155 } 156 #endif /* CONFIG_SMP */ 157 158 #define b_replacement(num) "664"#num 159 #define e_replacement(num) "665"#num 160 161 #define alt_end_marker "663" 162 #define alt_slen "662b-661b" 163 #define alt_total_slen alt_end_marker"b-661b" 164 #define alt_rlen(num) e_replacement(num)"f-"b_replacement(num)"f" 165 166 #define OLDINSTR(oldinstr, num) \ 167 "# ALT: oldnstr\n" \ 168 "661:\n\t" oldinstr "\n662:\n" \ 169 "# ALT: padding\n" \ 170 ".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * " \ 171 "((" alt_rlen(num) ")-(" alt_slen ")),0x90\n" \ 172 alt_end_marker ":\n" 173 174 /* 175 * gas compatible max based on the idea from: 176 * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax 177 * 178 * The additional "-" is needed because gas uses a "true" value of -1. 179 */ 180 #define alt_max_short(a, b) "((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") < (" b ")))))" 181 182 /* 183 * Pad the second replacement alternative with additional NOPs if it is 184 * additionally longer than the first replacement alternative. 185 */ 186 #define OLDINSTR_2(oldinstr, num1, num2) \ 187 "# ALT: oldinstr2\n" \ 188 "661:\n\t" oldinstr "\n662:\n" \ 189 "# ALT: padding2\n" \ 190 ".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * " \ 191 "(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n" \ 192 alt_end_marker ":\n" 193 194 #define OLDINSTR_3(oldinsn, n1, n2, n3) \ 195 "# ALT: oldinstr3\n" \ 196 "661:\n\t" oldinsn "\n662:\n" \ 197 "# ALT: padding3\n" \ 198 ".skip -((" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3)) \ 199 " - (" alt_slen ")) > 0) * " \ 200 "(" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3)) \ 201 " - (" alt_slen ")), 0x90\n" \ 202 alt_end_marker ":\n" 203 204 #define ALTINSTR_ENTRY(ft_flags, num) \ 205 " .long 661b - .\n" /* label */ \ 206 " .long " b_replacement(num)"f - .\n" /* new instruction */ \ 207 " .4byte " __stringify(ft_flags) "\n" /* feature + flags */ \ 208 " .byte " alt_total_slen "\n" /* source len */ \ 209 " .byte " alt_rlen(num) "\n" /* replacement len */ 210 211 #define ALTINSTR_REPLACEMENT(newinstr, num) /* replacement */ \ 212 "# ALT: replacement " #num "\n" \ 213 b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n" 214 215 /* alternative assembly primitive: */ 216 #define ALTERNATIVE(oldinstr, newinstr, ft_flags) \ 217 OLDINSTR(oldinstr, 1) \ 218 ".pushsection .altinstructions,\"a\"\n" \ 219 ALTINSTR_ENTRY(ft_flags, 1) \ 220 ".popsection\n" \ 221 ".pushsection .altinstr_replacement, \"ax\"\n" \ 222 ALTINSTR_REPLACEMENT(newinstr, 1) \ 223 ".popsection\n" 224 225 #define ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) \ 226 OLDINSTR_2(oldinstr, 1, 2) \ 227 ".pushsection .altinstructions,\"a\"\n" \ 228 ALTINSTR_ENTRY(ft_flags1, 1) \ 229 ALTINSTR_ENTRY(ft_flags2, 2) \ 230 ".popsection\n" \ 231 ".pushsection .altinstr_replacement, \"ax\"\n" \ 232 ALTINSTR_REPLACEMENT(newinstr1, 1) \ 233 ALTINSTR_REPLACEMENT(newinstr2, 2) \ 234 ".popsection\n" 235 236 /* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */ 237 #define ALTERNATIVE_TERNARY(oldinstr, ft_flags, newinstr_yes, newinstr_no) \ 238 ALTERNATIVE_2(oldinstr, newinstr_no, X86_FEATURE_ALWAYS, \ 239 newinstr_yes, ft_flags) 240 241 #define ALTERNATIVE_3(oldinsn, newinsn1, ft_flags1, newinsn2, ft_flags2, \ 242 newinsn3, ft_flags3) \ 243 OLDINSTR_3(oldinsn, 1, 2, 3) \ 244 ".pushsection .altinstructions,\"a\"\n" \ 245 ALTINSTR_ENTRY(ft_flags1, 1) \ 246 ALTINSTR_ENTRY(ft_flags2, 2) \ 247 ALTINSTR_ENTRY(ft_flags3, 3) \ 248 ".popsection\n" \ 249 ".pushsection .altinstr_replacement, \"ax\"\n" \ 250 ALTINSTR_REPLACEMENT(newinsn1, 1) \ 251 ALTINSTR_REPLACEMENT(newinsn2, 2) \ 252 ALTINSTR_REPLACEMENT(newinsn3, 3) \ 253 ".popsection\n" 254 255 /* 256 * Alternative instructions for different CPU types or capabilities. 257 * 258 * This allows to use optimized instructions even on generic binary 259 * kernels. 260 * 261 * length of oldinstr must be longer or equal the length of newinstr 262 * It can be padded with nops as needed. 263 * 264 * For non barrier like inlines please define new variants 265 * without volatile and memory clobber. 266 */ 267 #define alternative(oldinstr, newinstr, ft_flags) \ 268 asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, ft_flags) : : : "memory") 269 270 #define alternative_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) \ 271 asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) ::: "memory") 272 273 #define alternative_ternary(oldinstr, ft_flags, newinstr_yes, newinstr_no) \ 274 asm_inline volatile(ALTERNATIVE_TERNARY(oldinstr, ft_flags, newinstr_yes, newinstr_no) ::: "memory") 275 276 /* 277 * Alternative inline assembly with input. 278 * 279 * Peculiarities: 280 * No memory clobber here. 281 * Argument numbers start with 1. 282 * Leaving an unused argument 0 to keep API compatibility. 283 */ 284 #define alternative_input(oldinstr, newinstr, ft_flags, input...) \ 285 asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, ft_flags) \ 286 : : "i" (0), ## input) 287 288 /* 289 * This is similar to alternative_input. But it has two features and 290 * respective instructions. 291 * 292 * If CPU has feature2, newinstr2 is used. 293 * Otherwise, if CPU has feature1, newinstr1 is used. 294 * Otherwise, oldinstr is used. 295 */ 296 #define alternative_input_2(oldinstr, newinstr1, ft_flags1, newinstr2, \ 297 ft_flags2, input...) \ 298 asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, \ 299 newinstr2, ft_flags2) \ 300 : : "i" (0), ## input) 301 302 /* Like alternative_input, but with a single output argument */ 303 #define alternative_io(oldinstr, newinstr, ft_flags, output, input...) \ 304 asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, ft_flags) \ 305 : output : "i" (0), ## input) 306 307 /* Like alternative_io, but for replacing a direct call with another one. */ 308 #define alternative_call(oldfunc, newfunc, ft_flags, output, input...) \ 309 asm_inline volatile (ALTERNATIVE("call %P[old]", "call %P[new]", ft_flags) \ 310 : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input) 311 312 /* 313 * Like alternative_call, but there are two features and respective functions. 314 * If CPU has feature2, function2 is used. 315 * Otherwise, if CPU has feature1, function1 is used. 316 * Otherwise, old function is used. 317 */ 318 #define alternative_call_2(oldfunc, newfunc1, ft_flags1, newfunc2, ft_flags2, \ 319 output, input...) \ 320 asm_inline volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", ft_flags1,\ 321 "call %P[new2]", ft_flags2) \ 322 : output, ASM_CALL_CONSTRAINT \ 323 : [old] "i" (oldfunc), [new1] "i" (newfunc1), \ 324 [new2] "i" (newfunc2), ## input) 325 326 /* 327 * use this macro(s) if you need more than one output parameter 328 * in alternative_io 329 */ 330 #define ASM_OUTPUT2(a...) a 331 332 /* 333 * use this macro if you need clobbers but no inputs in 334 * alternative_{input,io,call}() 335 */ 336 #define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr 337 338 #else /* __ASSEMBLY__ */ 339 340 #ifdef CONFIG_SMP 341 .macro LOCK_PREFIX 342 672: lock 343 .pushsection .smp_locks,"a" 344 .balign 4 345 .long 672b - . 346 .popsection 347 .endm 348 #else 349 .macro LOCK_PREFIX 350 .endm 351 #endif 352 353 /* 354 * objtool annotation to ignore the alternatives and only consider the original 355 * instruction(s). 356 */ 357 .macro ANNOTATE_IGNORE_ALTERNATIVE 358 .Lannotate_\@: 359 .pushsection .discard.ignore_alts 360 .long .Lannotate_\@ - . 361 .popsection 362 .endm 363 364 /* 365 * Issue one struct alt_instr descriptor entry (need to put it into 366 * the section .altinstructions, see below). This entry contains 367 * enough information for the alternatives patching code to patch an 368 * instruction. See apply_alternatives(). 369 */ 370 .macro altinstr_entry orig alt ft_flags orig_len alt_len 371 .long \orig - . 372 .long \alt - . 373 .4byte \ft_flags 374 .byte \orig_len 375 .byte \alt_len 376 .endm 377 378 /* 379 * Define an alternative between two instructions. If @feature is 380 * present, early code in apply_alternatives() replaces @oldinstr with 381 * @newinstr. ".skip" directive takes care of proper instruction padding 382 * in case @newinstr is longer than @oldinstr. 383 */ 384 .macro ALTERNATIVE oldinstr, newinstr, ft_flags 385 140: 386 \oldinstr 387 141: 388 .skip -(((144f-143f)-(141b-140b)) > 0) * ((144f-143f)-(141b-140b)),0x90 389 142: 390 391 .pushsection .altinstructions,"a" 392 altinstr_entry 140b,143f,\ft_flags,142b-140b,144f-143f 393 .popsection 394 395 .pushsection .altinstr_replacement,"ax" 396 143: 397 \newinstr 398 144: 399 .popsection 400 .endm 401 402 #define old_len 141b-140b 403 #define new_len1 144f-143f 404 #define new_len2 145f-144f 405 #define new_len3 146f-145f 406 407 /* 408 * gas compatible max based on the idea from: 409 * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax 410 * 411 * The additional "-" is needed because gas uses a "true" value of -1. 412 */ 413 #define alt_max_2(a, b) ((a) ^ (((a) ^ (b)) & -(-((a) < (b))))) 414 #define alt_max_3(a, b, c) (alt_max_2(alt_max_2(a, b), c)) 415 416 417 /* 418 * Same as ALTERNATIVE macro above but for two alternatives. If CPU 419 * has @feature1, it replaces @oldinstr with @newinstr1. If CPU has 420 * @feature2, it replaces @oldinstr with @feature2. 421 */ 422 .macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2 423 140: 424 \oldinstr 425 141: 426 .skip -((alt_max_2(new_len1, new_len2) - (old_len)) > 0) * \ 427 (alt_max_2(new_len1, new_len2) - (old_len)),0x90 428 142: 429 430 .pushsection .altinstructions,"a" 431 altinstr_entry 140b,143f,\ft_flags1,142b-140b,144f-143f 432 altinstr_entry 140b,144f,\ft_flags2,142b-140b,145f-144f 433 .popsection 434 435 .pushsection .altinstr_replacement,"ax" 436 143: 437 \newinstr1 438 144: 439 \newinstr2 440 145: 441 .popsection 442 .endm 443 444 .macro ALTERNATIVE_3 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2, newinstr3, ft_flags3 445 140: 446 \oldinstr 447 141: 448 .skip -((alt_max_3(new_len1, new_len2, new_len3) - (old_len)) > 0) * \ 449 (alt_max_3(new_len1, new_len2, new_len3) - (old_len)),0x90 450 142: 451 452 .pushsection .altinstructions,"a" 453 altinstr_entry 140b,143f,\ft_flags1,142b-140b,144f-143f 454 altinstr_entry 140b,144f,\ft_flags2,142b-140b,145f-144f 455 altinstr_entry 140b,145f,\ft_flags3,142b-140b,146f-145f 456 .popsection 457 458 .pushsection .altinstr_replacement,"ax" 459 143: 460 \newinstr1 461 144: 462 \newinstr2 463 145: 464 \newinstr3 465 146: 466 .popsection 467 .endm 468 469 /* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */ 470 #define ALTERNATIVE_TERNARY(oldinstr, ft_flags, newinstr_yes, newinstr_no) \ 471 ALTERNATIVE_2 oldinstr, newinstr_no, X86_FEATURE_ALWAYS, \ 472 newinstr_yes, ft_flags 473 474 #endif /* __ASSEMBLY__ */ 475 476 #endif /* _ASM_X86_ALTERNATIVE_H */ 477