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