1/* SPDX-License-Identifier: GPL-2.0 */ 2 3#include <linux/stringify.h> 4#include <linux/linkage.h> 5#include <asm/dwarf2.h> 6#include <asm/cpufeatures.h> 7#include <asm/alternative.h> 8#include <asm/asm-offsets.h> 9#include <asm/export.h> 10#include <asm/nospec-branch.h> 11#include <asm/unwind_hints.h> 12#include <asm/percpu.h> 13#include <asm/frame.h> 14#include <asm/nops.h> 15 16 .section .text..__x86.indirect_thunk 17 18 19.macro POLINE reg 20 ANNOTATE_INTRA_FUNCTION_CALL 21 call .Ldo_rop_\@ 22 int3 23.Ldo_rop_\@: 24 mov %\reg, (%_ASM_SP) 25 UNWIND_HINT_FUNC 26.endm 27 28.macro RETPOLINE reg 29 POLINE \reg 30 RET 31.endm 32 33.macro THUNK reg 34 35 .align RETPOLINE_THUNK_SIZE 36SYM_INNER_LABEL(__x86_indirect_thunk_\reg, SYM_L_GLOBAL) 37 UNWIND_HINT_UNDEFINED 38 ANNOTATE_NOENDBR 39 40 ALTERNATIVE_2 __stringify(RETPOLINE \reg), \ 41 __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *%\reg; int3), X86_FEATURE_RETPOLINE_LFENCE, \ 42 __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *%\reg), ALT_NOT(X86_FEATURE_RETPOLINE) 43 44.endm 45 46/* 47 * Despite being an assembler file we can't just use .irp here 48 * because __KSYM_DEPS__ only uses the C preprocessor and would 49 * only see one instance of "__x86_indirect_thunk_\reg" rather 50 * than one per register with the correct names. So we do it 51 * the simple and nasty way... 52 * 53 * Worse, you can only have a single EXPORT_SYMBOL per line, 54 * and CPP can't insert newlines, so we have to repeat everything 55 * at least twice. 56 */ 57 58#define __EXPORT_THUNK(sym) _ASM_NOKPROBE(sym); EXPORT_SYMBOL(sym) 59 60 .align RETPOLINE_THUNK_SIZE 61SYM_CODE_START(__x86_indirect_thunk_array) 62 63#define GEN(reg) THUNK reg 64#include <asm/GEN-for-each-reg.h> 65#undef GEN 66 67 .align RETPOLINE_THUNK_SIZE 68SYM_CODE_END(__x86_indirect_thunk_array) 69 70#define GEN(reg) __EXPORT_THUNK(__x86_indirect_thunk_ ## reg) 71#include <asm/GEN-for-each-reg.h> 72#undef GEN 73 74#ifdef CONFIG_CALL_DEPTH_TRACKING 75.macro CALL_THUNK reg 76 .align RETPOLINE_THUNK_SIZE 77 78SYM_INNER_LABEL(__x86_indirect_call_thunk_\reg, SYM_L_GLOBAL) 79 UNWIND_HINT_UNDEFINED 80 ANNOTATE_NOENDBR 81 82 CALL_DEPTH_ACCOUNT 83 POLINE \reg 84 ANNOTATE_UNRET_SAFE 85 ret 86 int3 87.endm 88 89 .align RETPOLINE_THUNK_SIZE 90SYM_CODE_START(__x86_indirect_call_thunk_array) 91 92#define GEN(reg) CALL_THUNK reg 93#include <asm/GEN-for-each-reg.h> 94#undef GEN 95 96 .align RETPOLINE_THUNK_SIZE 97SYM_CODE_END(__x86_indirect_call_thunk_array) 98 99#define GEN(reg) __EXPORT_THUNK(__x86_indirect_call_thunk_ ## reg) 100#include <asm/GEN-for-each-reg.h> 101#undef GEN 102 103.macro JUMP_THUNK reg 104 .align RETPOLINE_THUNK_SIZE 105 106SYM_INNER_LABEL(__x86_indirect_jump_thunk_\reg, SYM_L_GLOBAL) 107 UNWIND_HINT_UNDEFINED 108 ANNOTATE_NOENDBR 109 POLINE \reg 110 ANNOTATE_UNRET_SAFE 111 ret 112 int3 113.endm 114 115 .align RETPOLINE_THUNK_SIZE 116SYM_CODE_START(__x86_indirect_jump_thunk_array) 117 118#define GEN(reg) JUMP_THUNK reg 119#include <asm/GEN-for-each-reg.h> 120#undef GEN 121 122 .align RETPOLINE_THUNK_SIZE 123SYM_CODE_END(__x86_indirect_jump_thunk_array) 124 125#define GEN(reg) __EXPORT_THUNK(__x86_indirect_jump_thunk_ ## reg) 126#include <asm/GEN-for-each-reg.h> 127#undef GEN 128#endif 129 130#ifdef CONFIG_RETHUNK 131 132 .section .text..__x86.return_thunk 133 134#ifdef CONFIG_CPU_SRSO 135 136/* 137 * srso_alias_untrain_ret() and srso_alias_safe_ret() are placed at 138 * special addresses: 139 * 140 * - srso_alias_untrain_ret() is 2M aligned 141 * - srso_alias_safe_ret() is also in the same 2M page but bits 2, 8, 14 142 * and 20 in its virtual address are set (while those bits in the 143 * srso_alias_untrain_ret() function are cleared). 144 * 145 * This guarantees that those two addresses will alias in the branch 146 * target buffer of Zen3/4 generations, leading to any potential 147 * poisoned entries at that BTB slot to get evicted. 148 * 149 * As a result, srso_alias_safe_ret() becomes a safe return. 150 */ 151 .pushsection .text..__x86.rethunk_untrain 152SYM_START(srso_alias_untrain_ret, SYM_L_GLOBAL, SYM_A_NONE) 153 UNWIND_HINT_FUNC 154 ANNOTATE_NOENDBR 155 ASM_NOP2 156 lfence 157 jmp srso_alias_return_thunk 158SYM_FUNC_END(srso_alias_untrain_ret) 159__EXPORT_THUNK(srso_alias_untrain_ret) 160 .popsection 161 162 .pushsection .text..__x86.rethunk_safe 163SYM_START(srso_alias_safe_ret, SYM_L_GLOBAL, SYM_A_NONE) 164 lea 8(%_ASM_SP), %_ASM_SP 165 UNWIND_HINT_FUNC 166 ANNOTATE_UNRET_SAFE 167 ret 168 int3 169SYM_FUNC_END(srso_alias_safe_ret) 170 171SYM_CODE_START_NOALIGN(srso_alias_return_thunk) 172 UNWIND_HINT_FUNC 173 ANNOTATE_NOENDBR 174 call srso_alias_safe_ret 175 ud2 176SYM_CODE_END(srso_alias_return_thunk) 177 .popsection 178 179/* 180 * SRSO untraining sequence for Zen1/2, similar to retbleed_untrain_ret() 181 * above. On kernel entry, srso_untrain_ret() is executed which is a 182 * 183 * movabs $0xccccc30824648d48,%rax 184 * 185 * and when the return thunk executes the inner label srso_safe_ret() 186 * later, it is a stack manipulation and a RET which is mispredicted and 187 * thus a "safe" one to use. 188 */ 189 .align 64 190 .skip 64 - (srso_safe_ret - srso_untrain_ret), 0xcc 191SYM_START(srso_untrain_ret, SYM_L_LOCAL, SYM_A_NONE) 192 ANNOTATE_NOENDBR 193 .byte 0x48, 0xb8 194 195/* 196 * This forces the function return instruction to speculate into a trap 197 * (UD2 in srso_return_thunk() below). This RET will then mispredict 198 * and execution will continue at the return site read from the top of 199 * the stack. 200 */ 201SYM_INNER_LABEL(srso_safe_ret, SYM_L_GLOBAL) 202 lea 8(%_ASM_SP), %_ASM_SP 203 ret 204 int3 205 int3 206 /* end of movabs */ 207 lfence 208 call srso_safe_ret 209 ud2 210SYM_CODE_END(srso_safe_ret) 211SYM_FUNC_END(srso_untrain_ret) 212 213SYM_CODE_START(srso_return_thunk) 214 UNWIND_HINT_FUNC 215 ANNOTATE_NOENDBR 216 call srso_safe_ret 217 ud2 218SYM_CODE_END(srso_return_thunk) 219 220#define JMP_SRSO_UNTRAIN_RET "jmp srso_untrain_ret" 221#else /* !CONFIG_CPU_SRSO */ 222#define JMP_SRSO_UNTRAIN_RET "ud2" 223/* Dummy for the alternative in CALL_UNTRAIN_RET. */ 224SYM_CODE_START(srso_alias_untrain_ret) 225 ANNOTATE_UNRET_SAFE 226 ANNOTATE_NOENDBR 227 ret 228 int3 229SYM_FUNC_END(srso_alias_untrain_ret) 230__EXPORT_THUNK(srso_alias_untrain_ret) 231#endif /* CONFIG_CPU_SRSO */ 232 233#ifdef CONFIG_CPU_UNRET_ENTRY 234 235/* 236 * Some generic notes on the untraining sequences: 237 * 238 * They are interchangeable when it comes to flushing potentially wrong 239 * RET predictions from the BTB. 240 * 241 * The SRSO Zen1/2 (MOVABS) untraining sequence is longer than the 242 * Retbleed sequence because the return sequence done there 243 * (srso_safe_ret()) is longer and the return sequence must fully nest 244 * (end before) the untraining sequence. Therefore, the untraining 245 * sequence must fully overlap the return sequence. 246 * 247 * Regarding alignment - the instructions which need to be untrained, 248 * must all start at a cacheline boundary for Zen1/2 generations. That 249 * is, instruction sequences starting at srso_safe_ret() and 250 * the respective instruction sequences at retbleed_return_thunk() 251 * must start at a cacheline boundary. 252 */ 253 254/* 255 * Safety details here pertain to the AMD Zen{1,2} microarchitecture: 256 * 1) The RET at retbleed_return_thunk must be on a 64 byte boundary, for 257 * alignment within the BTB. 258 * 2) The instruction at retbleed_untrain_ret must contain, and not 259 * end with, the 0xc3 byte of the RET. 260 * 3) STIBP must be enabled, or SMT disabled, to prevent the sibling thread 261 * from re-poisioning the BTB prediction. 262 */ 263 .align 64 264 .skip 64 - (retbleed_return_thunk - retbleed_untrain_ret), 0xcc 265SYM_START(retbleed_untrain_ret, SYM_L_GLOBAL, SYM_A_NONE) 266 ANNOTATE_NOENDBR 267 /* 268 * As executed from retbleed_untrain_ret, this is: 269 * 270 * TEST $0xcc, %bl 271 * LFENCE 272 * JMP retbleed_return_thunk 273 * 274 * Executing the TEST instruction has a side effect of evicting any BTB 275 * prediction (potentially attacker controlled) attached to the RET, as 276 * retbleed_return_thunk + 1 isn't an instruction boundary at the moment. 277 */ 278 .byte 0xf6 279 280 /* 281 * As executed from retbleed_return_thunk, this is a plain RET. 282 * 283 * As part of the TEST above, RET is the ModRM byte, and INT3 the imm8. 284 * 285 * We subsequently jump backwards and architecturally execute the RET. 286 * This creates a correct BTB prediction (type=ret), but in the 287 * meantime we suffer Straight Line Speculation (because the type was 288 * no branch) which is halted by the INT3. 289 * 290 * With SMT enabled and STIBP active, a sibling thread cannot poison 291 * RET's prediction to a type of its choice, but can evict the 292 * prediction due to competitive sharing. If the prediction is 293 * evicted, retbleed_return_thunk will suffer Straight Line Speculation 294 * which will be contained safely by the INT3. 295 */ 296SYM_INNER_LABEL(retbleed_return_thunk, SYM_L_GLOBAL) 297 ret 298 int3 299SYM_CODE_END(retbleed_return_thunk) 300 301 /* 302 * Ensure the TEST decoding / BTB invalidation is complete. 303 */ 304 lfence 305 306 /* 307 * Jump back and execute the RET in the middle of the TEST instruction. 308 * INT3 is for SLS protection. 309 */ 310 jmp retbleed_return_thunk 311 int3 312SYM_FUNC_END(retbleed_untrain_ret) 313__EXPORT_THUNK(retbleed_untrain_ret) 314 315#define JMP_RETBLEED_UNTRAIN_RET "jmp retbleed_untrain_ret" 316#else /* !CONFIG_CPU_UNRET_ENTRY */ 317#define JMP_RETBLEED_UNTRAIN_RET "ud2" 318#endif /* CONFIG_CPU_UNRET_ENTRY */ 319 320#if defined(CONFIG_CPU_UNRET_ENTRY) || defined(CONFIG_CPU_SRSO) 321 322SYM_FUNC_START(entry_untrain_ret) 323 ALTERNATIVE JMP_RETBLEED_UNTRAIN_RET, JMP_SRSO_UNTRAIN_RET, X86_FEATURE_SRSO 324SYM_FUNC_END(entry_untrain_ret) 325__EXPORT_THUNK(entry_untrain_ret) 326 327#endif /* CONFIG_CPU_UNRET_ENTRY || CONFIG_CPU_SRSO */ 328 329#ifdef CONFIG_CALL_DEPTH_TRACKING 330 331 .align 64 332SYM_FUNC_START(__x86_return_skl) 333 ANNOTATE_NOENDBR 334 /* 335 * Keep the hotpath in a 16byte I-fetch for the non-debug 336 * case. 337 */ 338 CALL_THUNKS_DEBUG_INC_RETS 339 shlq $5, PER_CPU_VAR(pcpu_hot + X86_call_depth) 340 jz 1f 341 ANNOTATE_UNRET_SAFE 342 ret 343 int3 3441: 345 CALL_THUNKS_DEBUG_INC_STUFFS 346 .rept 16 347 ANNOTATE_INTRA_FUNCTION_CALL 348 call 2f 349 int3 3502: 351 .endr 352 add $(8*16), %rsp 353 354 CREDIT_CALL_DEPTH 355 356 ANNOTATE_UNRET_SAFE 357 ret 358 int3 359SYM_FUNC_END(__x86_return_skl) 360 361#endif /* CONFIG_CALL_DEPTH_TRACKING */ 362 363/* 364 * This function name is magical and is used by -mfunction-return=thunk-extern 365 * for the compiler to generate JMPs to it. 366 * 367 * This code is only used during kernel boot or module init. All 368 * 'JMP __x86_return_thunk' sites are changed to something else by 369 * apply_returns(). 370 */ 371SYM_CODE_START(__x86_return_thunk) 372 UNWIND_HINT_FUNC 373 ANNOTATE_NOENDBR 374 ANNOTATE_UNRET_SAFE 375 ret 376 int3 377SYM_CODE_END(__x86_return_thunk) 378EXPORT_SYMBOL(__x86_return_thunk) 379 380#endif /* CONFIG_RETHUNK */ 381