Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #include <linux/linkage.h> |
| 3 | #include <asm/asm.h> |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 4 | #include <asm/bitsperlong.h> |
| 5 | #include <asm/kvm_vcpu_regs.h> |
Rick Edgecombe | f2fde6a | 2019-04-26 17:23:58 -0700 | [diff] [blame] | 6 | #include <asm/nospec-branch.h> |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 7 | |
| 8 | #define WORD_SIZE (BITS_PER_LONG / 8) |
| 9 | |
| 10 | #define VCPU_RAX __VCPU_REGS_RAX * WORD_SIZE |
| 11 | #define VCPU_RCX __VCPU_REGS_RCX * WORD_SIZE |
| 12 | #define VCPU_RDX __VCPU_REGS_RDX * WORD_SIZE |
| 13 | #define VCPU_RBX __VCPU_REGS_RBX * WORD_SIZE |
| 14 | /* Intentionally omit RSP as it's context switched by hardware */ |
| 15 | #define VCPU_RBP __VCPU_REGS_RBP * WORD_SIZE |
| 16 | #define VCPU_RSI __VCPU_REGS_RSI * WORD_SIZE |
| 17 | #define VCPU_RDI __VCPU_REGS_RDI * WORD_SIZE |
| 18 | |
| 19 | #ifdef CONFIG_X86_64 |
| 20 | #define VCPU_R8 __VCPU_REGS_R8 * WORD_SIZE |
| 21 | #define VCPU_R9 __VCPU_REGS_R9 * WORD_SIZE |
| 22 | #define VCPU_R10 __VCPU_REGS_R10 * WORD_SIZE |
| 23 | #define VCPU_R11 __VCPU_REGS_R11 * WORD_SIZE |
| 24 | #define VCPU_R12 __VCPU_REGS_R12 * WORD_SIZE |
| 25 | #define VCPU_R13 __VCPU_REGS_R13 * WORD_SIZE |
| 26 | #define VCPU_R14 __VCPU_REGS_R14 * WORD_SIZE |
| 27 | #define VCPU_R15 __VCPU_REGS_R15 * WORD_SIZE |
| 28 | #endif |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 29 | |
| 30 | .text |
| 31 | |
| 32 | /** |
| 33 | * vmx_vmenter - VM-Enter the current loaded VMCS |
| 34 | * |
| 35 | * %RFLAGS.ZF: !VMCS.LAUNCHED, i.e. controls VMLAUNCH vs. VMRESUME |
| 36 | * |
| 37 | * Returns: |
| 38 | * %RFLAGS.CF is set on VM-Fail Invalid |
| 39 | * %RFLAGS.ZF is set on VM-Fail Valid |
| 40 | * %RFLAGS.{CF,ZF} are cleared on VM-Success, i.e. VM-Exit |
| 41 | * |
| 42 | * Note that VMRESUME/VMLAUNCH fall-through and return directly if |
| 43 | * they VM-Fail, whereas a successful VM-Enter + VM-Exit will jump |
| 44 | * to vmx_vmexit. |
| 45 | */ |
Jiri Slaby | 6dcc562 | 2019-10-11 13:51:04 +0200 | [diff] [blame] | 46 | SYM_FUNC_START(vmx_vmenter) |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 47 | /* EFLAGS.ZF is set if VMCS.LAUNCHED == 0 */ |
| 48 | je 2f |
| 49 | |
| 50 | 1: vmresume |
| 51 | ret |
| 52 | |
| 53 | 2: vmlaunch |
| 54 | ret |
| 55 | |
| 56 | 3: cmpb $0, kvm_rebooting |
Josh Poimboeuf | 19f2d8f | 2019-07-17 20:36:38 -0500 | [diff] [blame] | 57 | je 4f |
| 58 | ret |
| 59 | 4: ud2 |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 60 | |
Uros Bizjak | da7e423 | 2020-04-06 22:21:08 +0200 | [diff] [blame] | 61 | _ASM_EXTABLE(1b, 3b) |
| 62 | _ASM_EXTABLE(2b, 3b) |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 63 | |
Jiri Slaby | 6dcc562 | 2019-10-11 13:51:04 +0200 | [diff] [blame] | 64 | SYM_FUNC_END(vmx_vmenter) |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * vmx_vmexit - Handle a VMX VM-Exit |
| 68 | * |
| 69 | * Returns: |
| 70 | * %RFLAGS.{CF,ZF} are cleared on VM-Success, i.e. VM-Exit |
| 71 | * |
| 72 | * This is vmx_vmenter's partner in crime. On a VM-Exit, control will jump |
| 73 | * here after hardware loads the host's state, i.e. this is the destination |
| 74 | * referred to by VMCS.HOST_RIP. |
| 75 | */ |
Jiri Slaby | 6dcc562 | 2019-10-11 13:51:04 +0200 | [diff] [blame] | 76 | SYM_FUNC_START(vmx_vmexit) |
Rick Edgecombe | f2fde6a | 2019-04-26 17:23:58 -0700 | [diff] [blame] | 77 | #ifdef CONFIG_RETPOLINE |
| 78 | ALTERNATIVE "jmp .Lvmexit_skip_rsb", "", X86_FEATURE_RETPOLINE |
| 79 | /* Preserve guest's RAX, it's used to stuff the RSB. */ |
| 80 | push %_ASM_AX |
| 81 | |
| 82 | /* IMPORTANT: Stuff the RSB immediately after VM-Exit, before RET! */ |
| 83 | FILL_RETURN_BUFFER %_ASM_AX, RSB_CLEAR_LOOPS, X86_FEATURE_RETPOLINE |
| 84 | |
Sean Christopherson | c7cb2d6 | 2020-05-05 20:53:55 -0700 | [diff] [blame^] | 85 | /* Clear RFLAGS.CF and RFLAGS.ZF to preserve VM-Exit, i.e. !VM-Fail. */ |
| 86 | or $1, %_ASM_AX |
| 87 | |
Rick Edgecombe | f2fde6a | 2019-04-26 17:23:58 -0700 | [diff] [blame] | 88 | pop %_ASM_AX |
| 89 | .Lvmexit_skip_rsb: |
| 90 | #endif |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 91 | ret |
Jiri Slaby | 6dcc562 | 2019-10-11 13:51:04 +0200 | [diff] [blame] | 92 | SYM_FUNC_END(vmx_vmexit) |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 93 | |
| 94 | /** |
Sean Christopherson | ee2fc63 | 2019-01-25 07:41:14 -0800 | [diff] [blame] | 95 | * __vmx_vcpu_run - Run a vCPU via a transition to VMX guest mode |
Sean Christopherson | b6852ae | 2019-08-15 13:09:31 -0700 | [diff] [blame] | 96 | * @vmx: struct vcpu_vmx * (forwarded to vmx_update_host_rsp) |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 97 | * @regs: unsigned long * (to guest registers) |
Sean Christopherson | 77df549 | 2019-01-25 07:41:16 -0800 | [diff] [blame] | 98 | * @launched: %true if the VMCS has been launched |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 99 | * |
| 100 | * Returns: |
Sean Christopherson | e75c3c3 | 2019-01-25 07:41:17 -0800 | [diff] [blame] | 101 | * 0 on VM-Exit, 1 on VM-Fail |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 102 | */ |
Jiri Slaby | 6dcc562 | 2019-10-11 13:51:04 +0200 | [diff] [blame] | 103 | SYM_FUNC_START(__vmx_vcpu_run) |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 104 | push %_ASM_BP |
| 105 | mov %_ASM_SP, %_ASM_BP |
Sean Christopherson | 3b895ef | 2019-01-25 07:41:18 -0800 | [diff] [blame] | 106 | #ifdef CONFIG_X86_64 |
| 107 | push %r15 |
| 108 | push %r14 |
| 109 | push %r13 |
| 110 | push %r12 |
| 111 | #else |
| 112 | push %edi |
| 113 | push %esi |
| 114 | #endif |
| 115 | push %_ASM_BX |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 116 | |
| 117 | /* |
| 118 | * Save @regs, _ASM_ARG2 may be modified by vmx_update_host_rsp() and |
| 119 | * @regs is needed after VM-Exit to save the guest's register values. |
| 120 | */ |
| 121 | push %_ASM_ARG2 |
| 122 | |
Sean Christopherson | 77df549 | 2019-01-25 07:41:16 -0800 | [diff] [blame] | 123 | /* Copy @launched to BL, _ASM_ARG3 is volatile. */ |
| 124 | mov %_ASM_ARG3B, %bl |
| 125 | |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 126 | /* Adjust RSP to account for the CALL to vmx_vmenter(). */ |
| 127 | lea -WORD_SIZE(%_ASM_SP), %_ASM_ARG2 |
| 128 | call vmx_update_host_rsp |
| 129 | |
Sean Christopherson | a62fd5a | 2019-01-25 07:41:15 -0800 | [diff] [blame] | 130 | /* Load @regs to RAX. */ |
| 131 | mov (%_ASM_SP), %_ASM_AX |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 132 | |
| 133 | /* Check if vmlaunch or vmresume is needed */ |
| 134 | cmpb $0, %bl |
| 135 | |
| 136 | /* Load guest registers. Don't clobber flags. */ |
Sean Christopherson | a62fd5a | 2019-01-25 07:41:15 -0800 | [diff] [blame] | 137 | mov VCPU_RCX(%_ASM_AX), %_ASM_CX |
| 138 | mov VCPU_RDX(%_ASM_AX), %_ASM_DX |
Uros Bizjak | bb03911 | 2020-03-10 18:10:24 +0100 | [diff] [blame] | 139 | mov VCPU_RBX(%_ASM_AX), %_ASM_BX |
| 140 | mov VCPU_RBP(%_ASM_AX), %_ASM_BP |
Sean Christopherson | a62fd5a | 2019-01-25 07:41:15 -0800 | [diff] [blame] | 141 | mov VCPU_RSI(%_ASM_AX), %_ASM_SI |
| 142 | mov VCPU_RDI(%_ASM_AX), %_ASM_DI |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 143 | #ifdef CONFIG_X86_64 |
Sean Christopherson | a62fd5a | 2019-01-25 07:41:15 -0800 | [diff] [blame] | 144 | mov VCPU_R8 (%_ASM_AX), %r8 |
| 145 | mov VCPU_R9 (%_ASM_AX), %r9 |
| 146 | mov VCPU_R10(%_ASM_AX), %r10 |
| 147 | mov VCPU_R11(%_ASM_AX), %r11 |
| 148 | mov VCPU_R12(%_ASM_AX), %r12 |
| 149 | mov VCPU_R13(%_ASM_AX), %r13 |
| 150 | mov VCPU_R14(%_ASM_AX), %r14 |
| 151 | mov VCPU_R15(%_ASM_AX), %r15 |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 152 | #endif |
Sean Christopherson | b6852ae | 2019-08-15 13:09:31 -0700 | [diff] [blame] | 153 | /* Load guest RAX. This kills the @regs pointer! */ |
Sean Christopherson | a62fd5a | 2019-01-25 07:41:15 -0800 | [diff] [blame] | 154 | mov VCPU_RAX(%_ASM_AX), %_ASM_AX |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 155 | |
| 156 | /* Enter guest mode */ |
| 157 | call vmx_vmenter |
| 158 | |
| 159 | /* Jump on VM-Fail. */ |
| 160 | jbe 2f |
| 161 | |
Sean Christopherson | a62fd5a | 2019-01-25 07:41:15 -0800 | [diff] [blame] | 162 | /* Temporarily save guest's RAX. */ |
| 163 | push %_ASM_AX |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 164 | |
Sean Christopherson | a62fd5a | 2019-01-25 07:41:15 -0800 | [diff] [blame] | 165 | /* Reload @regs to RAX. */ |
| 166 | mov WORD_SIZE(%_ASM_SP), %_ASM_AX |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 167 | |
Sean Christopherson | a62fd5a | 2019-01-25 07:41:15 -0800 | [diff] [blame] | 168 | /* Save all guest registers, including RAX from the stack */ |
| 169 | __ASM_SIZE(pop) VCPU_RAX(%_ASM_AX) |
Sean Christopherson | a62fd5a | 2019-01-25 07:41:15 -0800 | [diff] [blame] | 170 | mov %_ASM_CX, VCPU_RCX(%_ASM_AX) |
| 171 | mov %_ASM_DX, VCPU_RDX(%_ASM_AX) |
Uros Bizjak | bb03911 | 2020-03-10 18:10:24 +0100 | [diff] [blame] | 172 | mov %_ASM_BX, VCPU_RBX(%_ASM_AX) |
| 173 | mov %_ASM_BP, VCPU_RBP(%_ASM_AX) |
Sean Christopherson | a62fd5a | 2019-01-25 07:41:15 -0800 | [diff] [blame] | 174 | mov %_ASM_SI, VCPU_RSI(%_ASM_AX) |
| 175 | mov %_ASM_DI, VCPU_RDI(%_ASM_AX) |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 176 | #ifdef CONFIG_X86_64 |
Sean Christopherson | a62fd5a | 2019-01-25 07:41:15 -0800 | [diff] [blame] | 177 | mov %r8, VCPU_R8 (%_ASM_AX) |
| 178 | mov %r9, VCPU_R9 (%_ASM_AX) |
| 179 | mov %r10, VCPU_R10(%_ASM_AX) |
| 180 | mov %r11, VCPU_R11(%_ASM_AX) |
| 181 | mov %r12, VCPU_R12(%_ASM_AX) |
| 182 | mov %r13, VCPU_R13(%_ASM_AX) |
| 183 | mov %r14, VCPU_R14(%_ASM_AX) |
| 184 | mov %r15, VCPU_R15(%_ASM_AX) |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 185 | #endif |
| 186 | |
Sean Christopherson | e75c3c3 | 2019-01-25 07:41:17 -0800 | [diff] [blame] | 187 | /* Clear RAX to indicate VM-Exit (as opposed to VM-Fail). */ |
| 188 | xor %eax, %eax |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 189 | |
| 190 | /* |
Sean Christopherson | e75c3c3 | 2019-01-25 07:41:17 -0800 | [diff] [blame] | 191 | * Clear all general purpose registers except RSP and RAX to prevent |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 192 | * speculative use of the guest's values, even those that are reloaded |
| 193 | * via the stack. In theory, an L1 cache miss when restoring registers |
| 194 | * could lead to speculative execution with the guest's values. |
| 195 | * Zeroing XORs are dirt cheap, i.e. the extra paranoia is essentially |
Sean Christopherson | e75c3c3 | 2019-01-25 07:41:17 -0800 | [diff] [blame] | 196 | * free. RSP and RAX are exempt as RSP is restored by hardware during |
| 197 | * VM-Exit and RAX is explicitly loaded with 0 or 1 to return VM-Fail. |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 198 | */ |
Uros Bizjak | bb03911 | 2020-03-10 18:10:24 +0100 | [diff] [blame] | 199 | 1: xor %ecx, %ecx |
Sean Christopherson | 4f44c4e | 2019-01-25 07:41:20 -0800 | [diff] [blame] | 200 | xor %edx, %edx |
Uros Bizjak | bb03911 | 2020-03-10 18:10:24 +0100 | [diff] [blame] | 201 | xor %ebx, %ebx |
| 202 | xor %ebp, %ebp |
Sean Christopherson | 4f44c4e | 2019-01-25 07:41:20 -0800 | [diff] [blame] | 203 | xor %esi, %esi |
| 204 | xor %edi, %edi |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 205 | #ifdef CONFIG_X86_64 |
| 206 | xor %r8d, %r8d |
| 207 | xor %r9d, %r9d |
| 208 | xor %r10d, %r10d |
| 209 | xor %r11d, %r11d |
| 210 | xor %r12d, %r12d |
| 211 | xor %r13d, %r13d |
| 212 | xor %r14d, %r14d |
| 213 | xor %r15d, %r15d |
| 214 | #endif |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 215 | |
| 216 | /* "POP" @regs. */ |
| 217 | add $WORD_SIZE, %_ASM_SP |
Sean Christopherson | 3b895ef | 2019-01-25 07:41:18 -0800 | [diff] [blame] | 218 | pop %_ASM_BX |
| 219 | |
| 220 | #ifdef CONFIG_X86_64 |
| 221 | pop %r12 |
| 222 | pop %r13 |
| 223 | pop %r14 |
| 224 | pop %r15 |
| 225 | #else |
| 226 | pop %esi |
| 227 | pop %edi |
| 228 | #endif |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 229 | pop %_ASM_BP |
| 230 | ret |
| 231 | |
| 232 | /* VM-Fail. Out-of-line to avoid a taken Jcc after VM-Exit. */ |
Sean Christopherson | e75c3c3 | 2019-01-25 07:41:17 -0800 | [diff] [blame] | 233 | 2: mov $1, %eax |
Sean Christopherson | 5e0781d | 2019-01-25 07:41:12 -0800 | [diff] [blame] | 234 | jmp 1b |
Jiri Slaby | 6dcc562 | 2019-10-11 13:51:04 +0200 | [diff] [blame] | 235 | SYM_FUNC_END(__vmx_vcpu_run) |
Sean Christopherson | 842f4be | 2020-03-26 09:07:12 -0700 | [diff] [blame] | 236 | |
| 237 | /** |
| 238 | * vmread_error_trampoline - Trampoline from inline asm to vmread_error() |
| 239 | * @field: VMCS field encoding that failed |
| 240 | * @fault: %true if the VMREAD faulted, %false if it failed |
| 241 | |
| 242 | * Save and restore volatile registers across a call to vmread_error(). Note, |
| 243 | * all parameters are passed on the stack. |
| 244 | */ |
| 245 | SYM_FUNC_START(vmread_error_trampoline) |
| 246 | push %_ASM_BP |
| 247 | mov %_ASM_SP, %_ASM_BP |
| 248 | |
| 249 | push %_ASM_AX |
| 250 | push %_ASM_CX |
| 251 | push %_ASM_DX |
| 252 | #ifdef CONFIG_X86_64 |
| 253 | push %rdi |
| 254 | push %rsi |
| 255 | push %r8 |
| 256 | push %r9 |
| 257 | push %r10 |
| 258 | push %r11 |
| 259 | #endif |
| 260 | #ifdef CONFIG_X86_64 |
| 261 | /* Load @field and @fault to arg1 and arg2 respectively. */ |
| 262 | mov 3*WORD_SIZE(%rbp), %_ASM_ARG2 |
| 263 | mov 2*WORD_SIZE(%rbp), %_ASM_ARG1 |
| 264 | #else |
| 265 | /* Parameters are passed on the stack for 32-bit (see asmlinkage). */ |
| 266 | push 3*WORD_SIZE(%ebp) |
| 267 | push 2*WORD_SIZE(%ebp) |
| 268 | #endif |
| 269 | |
| 270 | call vmread_error |
| 271 | |
| 272 | #ifndef CONFIG_X86_64 |
| 273 | add $8, %esp |
| 274 | #endif |
| 275 | |
| 276 | /* Zero out @fault, which will be popped into the result register. */ |
| 277 | _ASM_MOV $0, 3*WORD_SIZE(%_ASM_BP) |
| 278 | |
| 279 | #ifdef CONFIG_X86_64 |
| 280 | pop %r11 |
| 281 | pop %r10 |
| 282 | pop %r9 |
| 283 | pop %r8 |
| 284 | pop %rsi |
| 285 | pop %rdi |
| 286 | #endif |
| 287 | pop %_ASM_DX |
| 288 | pop %_ASM_CX |
| 289 | pop %_ASM_AX |
| 290 | pop %_ASM_BP |
| 291 | |
| 292 | ret |
| 293 | SYM_FUNC_END(vmread_error_trampoline) |