David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | |
Borislav Petkov | 7a32fc5 | 2018-01-26 13:11:37 +0100 | [diff] [blame] | 3 | #ifndef _ASM_X86_NOSPEC_BRANCH_H_ |
| 4 | #define _ASM_X86_NOSPEC_BRANCH_H_ |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 5 | |
Thomas Gleixner | fa1202e | 2018-11-25 19:33:45 +0100 | [diff] [blame] | 6 | #include <linux/static_key.h> |
| 7 | |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 8 | #include <asm/alternative.h> |
| 9 | #include <asm/alternative-asm.h> |
| 10 | #include <asm/cpufeatures.h> |
Peter Zijlstra | ea00f30 | 2018-02-13 14:28:19 +0100 | [diff] [blame] | 11 | #include <asm/msr-index.h> |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 12 | |
David Woodhouse | d1c9910 | 2018-02-19 10:50:56 +0000 | [diff] [blame] | 13 | /* |
Peter Zijlstra | ff05ab2 | 2019-03-18 14:33:07 +0100 | [diff] [blame] | 14 | * This should be used immediately before a retpoline alternative. It tells |
| 15 | * objtool where the retpolines are so that it can make sense of the control |
| 16 | * flow by just reading the original instruction(s) and ignoring the |
| 17 | * alternatives. |
| 18 | */ |
| 19 | #define ANNOTATE_NOSPEC_ALTERNATIVE \ |
| 20 | ANNOTATE_IGNORE_ALTERNATIVE |
| 21 | |
| 22 | /* |
David Woodhouse | d1c9910 | 2018-02-19 10:50:56 +0000 | [diff] [blame] | 23 | * Fill the CPU return stack buffer. |
| 24 | * |
| 25 | * Each entry in the RSB, if used for a speculative 'ret', contains an |
| 26 | * infinite 'pause; lfence; jmp' loop to capture speculative execution. |
| 27 | * |
| 28 | * This is required in various cases for retpoline and IBRS-based |
| 29 | * mitigations for the Spectre variant 2 vulnerability. Sometimes to |
| 30 | * eliminate potentially bogus entries from the RSB, and sometimes |
| 31 | * purely to ensure that it doesn't get empty, which on some CPUs would |
| 32 | * allow predictions from other (unwanted!) sources to be used. |
| 33 | * |
| 34 | * We define a CPP macro such that it can be used from both .S files and |
| 35 | * inline assembly. It's possible to do a .macro and then include that |
| 36 | * from C via asm(".include <asm/nospec-branch.h>") but let's not go there. |
| 37 | */ |
| 38 | |
| 39 | #define RSB_CLEAR_LOOPS 32 /* To forcibly overwrite all entries */ |
| 40 | #define RSB_FILL_LOOPS 16 /* To avoid underflow */ |
| 41 | |
| 42 | /* |
| 43 | * Google experimented with loop-unrolling and this turned out to be |
| 44 | * the optimal version — two calls, each with their own speculation |
| 45 | * trap should their return address end up getting used, in a loop. |
| 46 | */ |
| 47 | #define __FILL_RETURN_BUFFER(reg, nr, sp) \ |
| 48 | mov $(nr/2), reg; \ |
| 49 | 771: \ |
| 50 | call 772f; \ |
| 51 | 773: /* speculation trap */ \ |
| 52 | pause; \ |
| 53 | lfence; \ |
| 54 | jmp 773b; \ |
| 55 | 772: \ |
| 56 | call 774f; \ |
| 57 | 775: /* speculation trap */ \ |
| 58 | pause; \ |
| 59 | lfence; \ |
| 60 | jmp 775b; \ |
| 61 | 774: \ |
| 62 | dec reg; \ |
| 63 | jnz 771b; \ |
| 64 | add $(BITS_PER_LONG/8) * nr, sp; |
| 65 | |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 66 | #ifdef __ASSEMBLY__ |
| 67 | |
| 68 | /* |
Peter Zijlstra | 9e0e3c5 | 2018-01-17 22:34:34 +0100 | [diff] [blame] | 69 | * This should be used immediately before an indirect jump/call. It tells |
| 70 | * objtool the subsequent indirect jump/call is vouched safe for retpoline |
| 71 | * builds. |
| 72 | */ |
| 73 | .macro ANNOTATE_RETPOLINE_SAFE |
| 74 | .Lannotate_\@: |
| 75 | .pushsection .discard.retpoline_safe |
| 76 | _ASM_PTR .Lannotate_\@ |
| 77 | .popsection |
| 78 | .endm |
| 79 | |
| 80 | /* |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 81 | * These are the bare retpoline primitives for indirect jmp and call. |
| 82 | * Do not use these directly; they only exist to make the ALTERNATIVE |
| 83 | * invocation below less ugly. |
| 84 | */ |
| 85 | .macro RETPOLINE_JMP reg:req |
| 86 | call .Ldo_rop_\@ |
| 87 | .Lspec_trap_\@: |
| 88 | pause |
Tom Lendacky | 28d437d | 2018-01-13 17:27:30 -0600 | [diff] [blame] | 89 | lfence |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 90 | jmp .Lspec_trap_\@ |
| 91 | .Ldo_rop_\@: |
| 92 | mov \reg, (%_ASM_SP) |
| 93 | ret |
| 94 | .endm |
| 95 | |
| 96 | /* |
| 97 | * This is a wrapper around RETPOLINE_JMP so the called function in reg |
| 98 | * returns to the instruction after the macro. |
| 99 | */ |
| 100 | .macro RETPOLINE_CALL reg:req |
| 101 | jmp .Ldo_call_\@ |
| 102 | .Ldo_retpoline_jmp_\@: |
| 103 | RETPOLINE_JMP \reg |
| 104 | .Ldo_call_\@: |
| 105 | call .Ldo_retpoline_jmp_\@ |
| 106 | .endm |
| 107 | |
| 108 | /* |
| 109 | * JMP_NOSPEC and CALL_NOSPEC macros can be used instead of a simple |
| 110 | * indirect jmp/call which may be susceptible to the Spectre variant 2 |
| 111 | * attack. |
| 112 | */ |
| 113 | .macro JMP_NOSPEC reg:req |
| 114 | #ifdef CONFIG_RETPOLINE |
| 115 | ANNOTATE_NOSPEC_ALTERNATIVE |
Peter Zijlstra | 9e0e3c5 | 2018-01-17 22:34:34 +0100 | [diff] [blame] | 116 | ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; jmp *\reg), \ |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 117 | __stringify(RETPOLINE_JMP \reg), X86_FEATURE_RETPOLINE, \ |
Peter Zijlstra | 9e0e3c5 | 2018-01-17 22:34:34 +0100 | [diff] [blame] | 118 | __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; jmp *\reg), X86_FEATURE_RETPOLINE_AMD |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 119 | #else |
| 120 | jmp *\reg |
| 121 | #endif |
| 122 | .endm |
| 123 | |
| 124 | .macro CALL_NOSPEC reg:req |
| 125 | #ifdef CONFIG_RETPOLINE |
| 126 | ANNOTATE_NOSPEC_ALTERNATIVE |
Peter Zijlstra | 9e0e3c5 | 2018-01-17 22:34:34 +0100 | [diff] [blame] | 127 | ALTERNATIVE_2 __stringify(ANNOTATE_RETPOLINE_SAFE; call *\reg), \ |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 128 | __stringify(RETPOLINE_CALL \reg), X86_FEATURE_RETPOLINE,\ |
Peter Zijlstra | 9e0e3c5 | 2018-01-17 22:34:34 +0100 | [diff] [blame] | 129 | __stringify(lfence; ANNOTATE_RETPOLINE_SAFE; call *\reg), X86_FEATURE_RETPOLINE_AMD |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 130 | #else |
| 131 | call *\reg |
| 132 | #endif |
| 133 | .endm |
| 134 | |
David Woodhouse | d1c9910 | 2018-02-19 10:50:56 +0000 | [diff] [blame] | 135 | /* |
| 136 | * A simpler FILL_RETURN_BUFFER macro. Don't make people use the CPP |
| 137 | * monstrosity above, manually. |
| 138 | */ |
| 139 | .macro FILL_RETURN_BUFFER reg:req nr:req ftr:req |
David Woodhouse | 117cc7a | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 140 | #ifdef CONFIG_RETPOLINE |
David Woodhouse | d1c9910 | 2018-02-19 10:50:56 +0000 | [diff] [blame] | 141 | ANNOTATE_NOSPEC_ALTERNATIVE |
| 142 | ALTERNATIVE "jmp .Lskip_rsb_\@", \ |
| 143 | __stringify(__FILL_RETURN_BUFFER(\reg,\nr,%_ASM_SP)) \ |
| 144 | \ftr |
| 145 | .Lskip_rsb_\@: |
David Woodhouse | 117cc7a | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 146 | #endif |
| 147 | .endm |
| 148 | |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 149 | #else /* __ASSEMBLY__ */ |
| 150 | |
Peter Zijlstra | 9e0e3c5 | 2018-01-17 22:34:34 +0100 | [diff] [blame] | 151 | #define ANNOTATE_RETPOLINE_SAFE \ |
| 152 | "999:\n\t" \ |
| 153 | ".pushsection .discard.retpoline_safe\n\t" \ |
| 154 | _ASM_PTR " 999b\n\t" \ |
| 155 | ".popsection\n\t" |
| 156 | |
Zhenzhong Duan | 4cd24de | 2018-11-02 01:45:41 -0700 | [diff] [blame] | 157 | #ifdef CONFIG_RETPOLINE |
| 158 | #ifdef CONFIG_X86_64 |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 159 | |
| 160 | /* |
Zhenzhong Duan | 4cd24de | 2018-11-02 01:45:41 -0700 | [diff] [blame] | 161 | * Inline asm uses the %V modifier which is only in newer GCC |
| 162 | * which is ensured when CONFIG_RETPOLINE is defined. |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 163 | */ |
| 164 | # define CALL_NOSPEC \ |
| 165 | ANNOTATE_NOSPEC_ALTERNATIVE \ |
Zhenzhong Duan | 0cbb76d | 2018-09-18 07:45:00 -0700 | [diff] [blame] | 166 | ALTERNATIVE_2( \ |
Peter Zijlstra | 9e0e3c5 | 2018-01-17 22:34:34 +0100 | [diff] [blame] | 167 | ANNOTATE_RETPOLINE_SAFE \ |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 168 | "call *%[thunk_target]\n", \ |
| 169 | "call __x86_indirect_thunk_%V[thunk_target]\n", \ |
Zhenzhong Duan | 0cbb76d | 2018-09-18 07:45:00 -0700 | [diff] [blame] | 170 | X86_FEATURE_RETPOLINE, \ |
| 171 | "lfence;\n" \ |
| 172 | ANNOTATE_RETPOLINE_SAFE \ |
| 173 | "call *%[thunk_target]\n", \ |
| 174 | X86_FEATURE_RETPOLINE_AMD) |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 175 | # define THUNK_TARGET(addr) [thunk_target] "r" (addr) |
| 176 | |
Zhenzhong Duan | 4cd24de | 2018-11-02 01:45:41 -0700 | [diff] [blame] | 177 | #else /* CONFIG_X86_32 */ |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 178 | /* |
| 179 | * For i386 we use the original ret-equivalent retpoline, because |
| 180 | * otherwise we'll run out of registers. We don't care about CET |
| 181 | * here, anyway. |
| 182 | */ |
Andy Whitcroft | a14bff1 | 2018-03-14 11:24:27 +0000 | [diff] [blame] | 183 | # define CALL_NOSPEC \ |
Zhenzhong Duan | 0cbb76d | 2018-09-18 07:45:00 -0700 | [diff] [blame] | 184 | ANNOTATE_NOSPEC_ALTERNATIVE \ |
| 185 | ALTERNATIVE_2( \ |
Andy Whitcroft | a14bff1 | 2018-03-14 11:24:27 +0000 | [diff] [blame] | 186 | ANNOTATE_RETPOLINE_SAFE \ |
| 187 | "call *%[thunk_target]\n", \ |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 188 | " jmp 904f;\n" \ |
| 189 | " .align 16\n" \ |
| 190 | "901: call 903f;\n" \ |
| 191 | "902: pause;\n" \ |
Tom Lendacky | 28d437d | 2018-01-13 17:27:30 -0600 | [diff] [blame] | 192 | " lfence;\n" \ |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 193 | " jmp 902b;\n" \ |
| 194 | " .align 16\n" \ |
| 195 | "903: addl $4, %%esp;\n" \ |
| 196 | " pushl %[thunk_target];\n" \ |
| 197 | " ret;\n" \ |
| 198 | " .align 16\n" \ |
| 199 | "904: call 901b;\n", \ |
Zhenzhong Duan | 0cbb76d | 2018-09-18 07:45:00 -0700 | [diff] [blame] | 200 | X86_FEATURE_RETPOLINE, \ |
| 201 | "lfence;\n" \ |
| 202 | ANNOTATE_RETPOLINE_SAFE \ |
| 203 | "call *%[thunk_target]\n", \ |
| 204 | X86_FEATURE_RETPOLINE_AMD) |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 205 | |
| 206 | # define THUNK_TARGET(addr) [thunk_target] "rm" (addr) |
Zhenzhong Duan | 4cd24de | 2018-11-02 01:45:41 -0700 | [diff] [blame] | 207 | #endif |
David Woodhouse | 117cc7a | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 208 | #else /* No retpoline for C / inline asm */ |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 209 | # define CALL_NOSPEC "call *%[thunk_target]\n" |
| 210 | # define THUNK_TARGET(addr) [thunk_target] "rm" (addr) |
| 211 | #endif |
| 212 | |
David Woodhouse | da28512 | 2018-01-11 21:46:26 +0000 | [diff] [blame] | 213 | /* The Spectre V2 mitigation variants */ |
| 214 | enum spectre_v2_mitigation { |
| 215 | SPECTRE_V2_NONE, |
David Woodhouse | da28512 | 2018-01-11 21:46:26 +0000 | [diff] [blame] | 216 | SPECTRE_V2_RETPOLINE_GENERIC, |
| 217 | SPECTRE_V2_RETPOLINE_AMD, |
Sai Praneeth | 706d516 | 2018-08-01 11:42:25 -0700 | [diff] [blame] | 218 | SPECTRE_V2_IBRS_ENHANCED, |
David Woodhouse | da28512 | 2018-01-11 21:46:26 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
Thomas Gleixner | fa1202e | 2018-11-25 19:33:45 +0100 | [diff] [blame] | 221 | /* The indirect branch speculation control variants */ |
| 222 | enum spectre_v2_user_mitigation { |
| 223 | SPECTRE_V2_USER_NONE, |
| 224 | SPECTRE_V2_USER_STRICT, |
Thomas Lendacky | 20c3a2c | 2018-12-13 23:03:54 +0000 | [diff] [blame] | 225 | SPECTRE_V2_USER_STRICT_PREFERRED, |
Thomas Gleixner | 9137bb2 | 2018-11-25 19:33:53 +0100 | [diff] [blame] | 226 | SPECTRE_V2_USER_PRCTL, |
Thomas Gleixner | 6b3e64c | 2018-11-25 19:33:55 +0100 | [diff] [blame] | 227 | SPECTRE_V2_USER_SECCOMP, |
Thomas Gleixner | fa1202e | 2018-11-25 19:33:45 +0100 | [diff] [blame] | 228 | }; |
| 229 | |
Konrad Rzeszutek Wilk | 24f7fc8 | 2018-04-25 22:04:21 -0400 | [diff] [blame] | 230 | /* The Speculative Store Bypass disable variants */ |
| 231 | enum ssb_mitigation { |
| 232 | SPEC_STORE_BYPASS_NONE, |
| 233 | SPEC_STORE_BYPASS_DISABLE, |
Thomas Gleixner | a73ec77 | 2018-04-29 15:26:40 +0200 | [diff] [blame] | 234 | SPEC_STORE_BYPASS_PRCTL, |
Kees Cook | f21b53b | 2018-05-03 14:37:54 -0700 | [diff] [blame] | 235 | SPEC_STORE_BYPASS_SECCOMP, |
Konrad Rzeszutek Wilk | 24f7fc8 | 2018-04-25 22:04:21 -0400 | [diff] [blame] | 236 | }; |
| 237 | |
Masami Hiramatsu | 736e80a | 2018-01-19 01:14:21 +0900 | [diff] [blame] | 238 | extern char __indirect_thunk_start[]; |
| 239 | extern char __indirect_thunk_end[]; |
| 240 | |
David Woodhouse | 117cc7a | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 241 | /* |
| 242 | * On VMEXIT we must ensure that no RSB predictions learned in the guest |
| 243 | * can be followed in the host, by overwriting the RSB completely. Both |
| 244 | * retpoline and IBRS mitigations for Spectre v2 need this; only on future |
Darren Kenny | af189c9 | 2018-02-02 19:12:20 +0000 | [diff] [blame] | 245 | * CPUs with IBRS_ALL *might* it be avoided. |
David Woodhouse | 117cc7a | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 246 | */ |
| 247 | static inline void vmexit_fill_RSB(void) |
| 248 | { |
| 249 | #ifdef CONFIG_RETPOLINE |
David Woodhouse | d1c9910 | 2018-02-19 10:50:56 +0000 | [diff] [blame] | 250 | unsigned long loops; |
| 251 | |
| 252 | asm volatile (ANNOTATE_NOSPEC_ALTERNATIVE |
| 253 | ALTERNATIVE("jmp 910f", |
| 254 | __stringify(__FILL_RETURN_BUFFER(%0, RSB_CLEAR_LOOPS, %1)), |
| 255 | X86_FEATURE_RETPOLINE) |
| 256 | "910:" |
| 257 | : "=r" (loops), ASM_CALL_CONSTRAINT |
| 258 | : : "memory" ); |
David Woodhouse | 117cc7a | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 259 | #endif |
| 260 | } |
Andi Kleen | 3f7d875 | 2018-01-17 14:53:28 -0800 | [diff] [blame] | 261 | |
Linus Torvalds | 1aa7a57 | 2018-05-01 15:55:51 +0200 | [diff] [blame] | 262 | static __always_inline |
| 263 | void alternative_msr_write(unsigned int msr, u64 val, unsigned int feature) |
| 264 | { |
| 265 | asm volatile(ALTERNATIVE("", "wrmsr", %c[feature]) |
| 266 | : : "c" (msr), |
Jim Mattson | 5f2b745 | 2018-05-13 17:33:57 -0400 | [diff] [blame] | 267 | "a" ((u32)val), |
| 268 | "d" ((u32)(val >> 32)), |
Linus Torvalds | 1aa7a57 | 2018-05-01 15:55:51 +0200 | [diff] [blame] | 269 | [feature] "i" (feature) |
| 270 | : "memory"); |
| 271 | } |
David Woodhouse | dd84441 | 2018-02-19 10:50:54 +0000 | [diff] [blame] | 272 | |
David Woodhouse | 20ffa1c | 2018-01-25 16:14:15 +0000 | [diff] [blame] | 273 | static inline void indirect_branch_prediction_barrier(void) |
| 274 | { |
Konrad Rzeszutek Wilk | 1b86883 | 2018-04-25 22:04:18 -0400 | [diff] [blame] | 275 | u64 val = PRED_CMD_IBPB; |
| 276 | |
| 277 | alternative_msr_write(MSR_IA32_PRED_CMD, val, X86_FEATURE_USE_IBPB); |
David Woodhouse | 20ffa1c | 2018-01-25 16:14:15 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Thomas Gleixner | fa8ac49 | 2018-05-12 20:49:16 +0200 | [diff] [blame] | 280 | /* The Intel SPEC CTRL MSR base value cache */ |
| 281 | extern u64 x86_spec_ctrl_base; |
| 282 | |
David Woodhouse | dd84441 | 2018-02-19 10:50:54 +0000 | [diff] [blame] | 283 | /* |
| 284 | * With retpoline, we must use IBRS to restrict branch prediction |
| 285 | * before calling into firmware. |
Ingo Molnar | d72f4e2 | 2018-02-21 09:20:37 +0100 | [diff] [blame] | 286 | * |
| 287 | * (Implemented as CPP macros due to header hell.) |
David Woodhouse | dd84441 | 2018-02-19 10:50:54 +0000 | [diff] [blame] | 288 | */ |
Ingo Molnar | d72f4e2 | 2018-02-21 09:20:37 +0100 | [diff] [blame] | 289 | #define firmware_restrict_branch_speculation_start() \ |
| 290 | do { \ |
Thomas Gleixner | fa8ac49 | 2018-05-12 20:49:16 +0200 | [diff] [blame] | 291 | u64 val = x86_spec_ctrl_base | SPEC_CTRL_IBRS; \ |
Konrad Rzeszutek Wilk | 1b86883 | 2018-04-25 22:04:18 -0400 | [diff] [blame] | 292 | \ |
Ingo Molnar | d72f4e2 | 2018-02-21 09:20:37 +0100 | [diff] [blame] | 293 | preempt_disable(); \ |
Konrad Rzeszutek Wilk | 1b86883 | 2018-04-25 22:04:18 -0400 | [diff] [blame] | 294 | alternative_msr_write(MSR_IA32_SPEC_CTRL, val, \ |
Ingo Molnar | d72f4e2 | 2018-02-21 09:20:37 +0100 | [diff] [blame] | 295 | X86_FEATURE_USE_IBRS_FW); \ |
| 296 | } while (0) |
David Woodhouse | dd84441 | 2018-02-19 10:50:54 +0000 | [diff] [blame] | 297 | |
Ingo Molnar | d72f4e2 | 2018-02-21 09:20:37 +0100 | [diff] [blame] | 298 | #define firmware_restrict_branch_speculation_end() \ |
| 299 | do { \ |
Thomas Gleixner | fa8ac49 | 2018-05-12 20:49:16 +0200 | [diff] [blame] | 300 | u64 val = x86_spec_ctrl_base; \ |
Konrad Rzeszutek Wilk | 1b86883 | 2018-04-25 22:04:18 -0400 | [diff] [blame] | 301 | \ |
| 302 | alternative_msr_write(MSR_IA32_SPEC_CTRL, val, \ |
Ingo Molnar | d72f4e2 | 2018-02-21 09:20:37 +0100 | [diff] [blame] | 303 | X86_FEATURE_USE_IBRS_FW); \ |
| 304 | preempt_enable(); \ |
| 305 | } while (0) |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 306 | |
Thomas Gleixner | fa1202e | 2018-11-25 19:33:45 +0100 | [diff] [blame] | 307 | DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp); |
Thomas Gleixner | 4c71a2b6 | 2018-11-25 19:33:49 +0100 | [diff] [blame] | 308 | DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb); |
| 309 | DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb); |
Thomas Gleixner | fa1202e | 2018-11-25 19:33:45 +0100 | [diff] [blame] | 310 | |
Thomas Gleixner | 04dcbdb | 2019-02-18 23:42:51 +0100 | [diff] [blame] | 311 | DECLARE_STATIC_KEY_FALSE(mds_user_clear); |
Thomas Gleixner | 07f07f5 | 2019-02-18 23:04:01 +0100 | [diff] [blame] | 312 | DECLARE_STATIC_KEY_FALSE(mds_idle_clear); |
Thomas Gleixner | 04dcbdb | 2019-02-18 23:42:51 +0100 | [diff] [blame] | 313 | |
Thomas Gleixner | 6a9e529 | 2019-02-18 23:13:06 +0100 | [diff] [blame] | 314 | #include <asm/segment.h> |
| 315 | |
| 316 | /** |
| 317 | * mds_clear_cpu_buffers - Mitigation for MDS vulnerability |
| 318 | * |
| 319 | * This uses the otherwise unused and obsolete VERW instruction in |
| 320 | * combination with microcode which triggers a CPU buffer flush when the |
| 321 | * instruction is executed. |
| 322 | */ |
| 323 | static inline void mds_clear_cpu_buffers(void) |
| 324 | { |
| 325 | static const u16 ds = __KERNEL_DS; |
| 326 | |
| 327 | /* |
| 328 | * Has to be the memory-operand variant because only that |
| 329 | * guarantees the CPU buffer flush functionality according to |
| 330 | * documentation. The register-operand variant does not. |
| 331 | * Works with any segment selector, but a valid writable |
| 332 | * data segment is the fastest variant. |
| 333 | * |
| 334 | * "cc" clobber is required because VERW modifies ZF. |
| 335 | */ |
| 336 | asm volatile("verw %[ds]" : : [ds] "m" (ds) : "cc"); |
| 337 | } |
| 338 | |
Thomas Gleixner | 04dcbdb | 2019-02-18 23:42:51 +0100 | [diff] [blame] | 339 | /** |
| 340 | * mds_user_clear_cpu_buffers - Mitigation for MDS vulnerability |
| 341 | * |
| 342 | * Clear CPU buffers if the corresponding static key is enabled |
| 343 | */ |
| 344 | static inline void mds_user_clear_cpu_buffers(void) |
| 345 | { |
| 346 | if (static_branch_likely(&mds_user_clear)) |
| 347 | mds_clear_cpu_buffers(); |
| 348 | } |
| 349 | |
Thomas Gleixner | 07f07f5 | 2019-02-18 23:04:01 +0100 | [diff] [blame] | 350 | /** |
| 351 | * mds_idle_clear_cpu_buffers - Mitigation for MDS vulnerability |
| 352 | * |
| 353 | * Clear CPU buffers if the corresponding static key is enabled |
| 354 | */ |
| 355 | static inline void mds_idle_clear_cpu_buffers(void) |
| 356 | { |
| 357 | if (static_branch_likely(&mds_idle_clear)) |
| 358 | mds_clear_cpu_buffers(); |
| 359 | } |
| 360 | |
David Woodhouse | 76b0438 | 2018-01-11 21:46:25 +0000 | [diff] [blame] | 361 | #endif /* __ASSEMBLY__ */ |
Daniel Borkmann | a493a87 | 2018-02-22 15:12:53 +0100 | [diff] [blame] | 362 | |
| 363 | /* |
| 364 | * Below is used in the eBPF JIT compiler and emits the byte sequence |
| 365 | * for the following assembly: |
| 366 | * |
| 367 | * With retpolines configured: |
| 368 | * |
| 369 | * callq do_rop |
| 370 | * spec_trap: |
| 371 | * pause |
| 372 | * lfence |
| 373 | * jmp spec_trap |
| 374 | * do_rop: |
Wang YanQing | 03f5781 | 2018-05-03 14:10:43 +0800 | [diff] [blame] | 375 | * mov %rax,(%rsp) for x86_64 |
| 376 | * mov %edx,(%esp) for x86_32 |
Daniel Borkmann | a493a87 | 2018-02-22 15:12:53 +0100 | [diff] [blame] | 377 | * retq |
| 378 | * |
| 379 | * Without retpolines configured: |
| 380 | * |
Wang YanQing | 03f5781 | 2018-05-03 14:10:43 +0800 | [diff] [blame] | 381 | * jmp *%rax for x86_64 |
| 382 | * jmp *%edx for x86_32 |
Daniel Borkmann | a493a87 | 2018-02-22 15:12:53 +0100 | [diff] [blame] | 383 | */ |
| 384 | #ifdef CONFIG_RETPOLINE |
Daniel Borkmann | 3625600 | 2018-05-14 23:22:29 +0200 | [diff] [blame] | 385 | # ifdef CONFIG_X86_64 |
| 386 | # define RETPOLINE_RAX_BPF_JIT_SIZE 17 |
| 387 | # define RETPOLINE_RAX_BPF_JIT() \ |
Wang YanQing | 03f5781 | 2018-05-03 14:10:43 +0800 | [diff] [blame] | 388 | do { \ |
Daniel Borkmann | a493a87 | 2018-02-22 15:12:53 +0100 | [diff] [blame] | 389 | EMIT1_off32(0xE8, 7); /* callq do_rop */ \ |
| 390 | /* spec_trap: */ \ |
| 391 | EMIT2(0xF3, 0x90); /* pause */ \ |
| 392 | EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \ |
| 393 | EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \ |
| 394 | /* do_rop: */ \ |
| 395 | EMIT4(0x48, 0x89, 0x04, 0x24); /* mov %rax,(%rsp) */ \ |
Wang YanQing | 03f5781 | 2018-05-03 14:10:43 +0800 | [diff] [blame] | 396 | EMIT1(0xC3); /* retq */ \ |
| 397 | } while (0) |
Daniel Borkmann | 3625600 | 2018-05-14 23:22:29 +0200 | [diff] [blame] | 398 | # else /* !CONFIG_X86_64 */ |
| 399 | # define RETPOLINE_EDX_BPF_JIT() \ |
Wang YanQing | 03f5781 | 2018-05-03 14:10:43 +0800 | [diff] [blame] | 400 | do { \ |
| 401 | EMIT1_off32(0xE8, 7); /* call do_rop */ \ |
| 402 | /* spec_trap: */ \ |
| 403 | EMIT2(0xF3, 0x90); /* pause */ \ |
| 404 | EMIT3(0x0F, 0xAE, 0xE8); /* lfence */ \ |
| 405 | EMIT2(0xEB, 0xF9); /* jmp spec_trap */ \ |
| 406 | /* do_rop: */ \ |
| 407 | EMIT3(0x89, 0x14, 0x24); /* mov %edx,(%esp) */ \ |
| 408 | EMIT1(0xC3); /* ret */ \ |
| 409 | } while (0) |
Daniel Borkmann | 3625600 | 2018-05-14 23:22:29 +0200 | [diff] [blame] | 410 | # endif |
Wang YanQing | 03f5781 | 2018-05-03 14:10:43 +0800 | [diff] [blame] | 411 | #else /* !CONFIG_RETPOLINE */ |
Daniel Borkmann | 3625600 | 2018-05-14 23:22:29 +0200 | [diff] [blame] | 412 | # ifdef CONFIG_X86_64 |
| 413 | # define RETPOLINE_RAX_BPF_JIT_SIZE 2 |
| 414 | # define RETPOLINE_RAX_BPF_JIT() \ |
| 415 | EMIT2(0xFF, 0xE0); /* jmp *%rax */ |
| 416 | # else /* !CONFIG_X86_64 */ |
| 417 | # define RETPOLINE_EDX_BPF_JIT() \ |
| 418 | EMIT2(0xFF, 0xE2) /* jmp *%edx */ |
| 419 | # endif |
Daniel Borkmann | a493a87 | 2018-02-22 15:12:53 +0100 | [diff] [blame] | 420 | #endif |
| 421 | |
Borislav Petkov | 7a32fc5 | 2018-01-26 13:11:37 +0100 | [diff] [blame] | 422 | #endif /* _ASM_X86_NOSPEC_BRANCH_H_ */ |