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