Mention branches and keyring.
[releases.git] / x86 / kvm / vmx / vmenter.S
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <linux/linkage.h>
3 #include <asm/asm.h>
4 #include <asm/bitsperlong.h>
5 #include <asm/kvm_vcpu_regs.h>
6 #include <asm/nospec-branch.h>
7 #include <asm/percpu.h>
8 #include <asm/segment.h>
9 #include "kvm-asm-offsets.h"
10 #include "run_flags.h"
11
12 #define WORD_SIZE (BITS_PER_LONG / 8)
13
14 #define VCPU_RAX        __VCPU_REGS_RAX * WORD_SIZE
15 #define VCPU_RCX        __VCPU_REGS_RCX * WORD_SIZE
16 #define VCPU_RDX        __VCPU_REGS_RDX * WORD_SIZE
17 #define VCPU_RBX        __VCPU_REGS_RBX * WORD_SIZE
18 /* Intentionally omit RSP as it's context switched by hardware */
19 #define VCPU_RBP        __VCPU_REGS_RBP * WORD_SIZE
20 #define VCPU_RSI        __VCPU_REGS_RSI * WORD_SIZE
21 #define VCPU_RDI        __VCPU_REGS_RDI * WORD_SIZE
22
23 #ifdef CONFIG_X86_64
24 #define VCPU_R8         __VCPU_REGS_R8  * WORD_SIZE
25 #define VCPU_R9         __VCPU_REGS_R9  * WORD_SIZE
26 #define VCPU_R10        __VCPU_REGS_R10 * WORD_SIZE
27 #define VCPU_R11        __VCPU_REGS_R11 * WORD_SIZE
28 #define VCPU_R12        __VCPU_REGS_R12 * WORD_SIZE
29 #define VCPU_R13        __VCPU_REGS_R13 * WORD_SIZE
30 #define VCPU_R14        __VCPU_REGS_R14 * WORD_SIZE
31 #define VCPU_R15        __VCPU_REGS_R15 * WORD_SIZE
32 #endif
33
34 .section .noinstr.text, "ax"
35
36 /**
37  * __vmx_vcpu_run - Run a vCPU via a transition to VMX guest mode
38  * @vmx:        struct vcpu_vmx *
39  * @regs:       unsigned long * (to guest registers)
40  * @flags:      VMX_RUN_VMRESUME:       use VMRESUME instead of VMLAUNCH
41  *              VMX_RUN_SAVE_SPEC_CTRL: save guest SPEC_CTRL into vmx->spec_ctrl
42  *
43  * Returns:
44  *      0 on VM-Exit, 1 on VM-Fail
45  */
46 SYM_FUNC_START(__vmx_vcpu_run)
47         push %_ASM_BP
48         mov  %_ASM_SP, %_ASM_BP
49 #ifdef CONFIG_X86_64
50         push %r15
51         push %r14
52         push %r13
53         push %r12
54 #else
55         push %edi
56         push %esi
57 #endif
58         push %_ASM_BX
59
60         /* Save @vmx for SPEC_CTRL handling */
61         push %_ASM_ARG1
62
63         /* Save @flags for SPEC_CTRL handling */
64         push %_ASM_ARG3
65
66         /*
67          * Save @regs, _ASM_ARG2 may be modified by vmx_update_host_rsp() and
68          * @regs is needed after VM-Exit to save the guest's register values.
69          */
70         push %_ASM_ARG2
71
72         /* Copy @flags to BL, _ASM_ARG3 is volatile. */
73         mov %_ASM_ARG3B, %bl
74
75         lea (%_ASM_SP), %_ASM_ARG2
76         call vmx_update_host_rsp
77
78         ALTERNATIVE "jmp .Lspec_ctrl_done", "", X86_FEATURE_MSR_SPEC_CTRL
79
80         /*
81          * SPEC_CTRL handling: if the guest's SPEC_CTRL value differs from the
82          * host's, write the MSR.
83          *
84          * IMPORTANT: To avoid RSB underflow attacks and any other nastiness,
85          * there must not be any returns or indirect branches between this code
86          * and vmentry.
87          */
88         mov 2*WORD_SIZE(%_ASM_SP), %_ASM_DI
89         movl VMX_spec_ctrl(%_ASM_DI), %edi
90         movl PER_CPU_VAR(x86_spec_ctrl_current), %esi
91         cmp %edi, %esi
92         je .Lspec_ctrl_done
93         mov $MSR_IA32_SPEC_CTRL, %ecx
94         xor %edx, %edx
95         mov %edi, %eax
96         wrmsr
97
98 .Lspec_ctrl_done:
99
100         /*
101          * Since vmentry is serializing on affected CPUs, there's no need for
102          * an LFENCE to stop speculation from skipping the wrmsr.
103          */
104
105         /* Load @regs to RAX. */
106         mov (%_ASM_SP), %_ASM_AX
107
108         /* Check if vmlaunch or vmresume is needed */
109         bt   $VMX_RUN_VMRESUME_SHIFT, %bx
110
111         /* Load guest registers.  Don't clobber flags. */
112         mov VCPU_RCX(%_ASM_AX), %_ASM_CX
113         mov VCPU_RDX(%_ASM_AX), %_ASM_DX
114         mov VCPU_RBX(%_ASM_AX), %_ASM_BX
115         mov VCPU_RBP(%_ASM_AX), %_ASM_BP
116         mov VCPU_RSI(%_ASM_AX), %_ASM_SI
117         mov VCPU_RDI(%_ASM_AX), %_ASM_DI
118 #ifdef CONFIG_X86_64
119         mov VCPU_R8 (%_ASM_AX),  %r8
120         mov VCPU_R9 (%_ASM_AX),  %r9
121         mov VCPU_R10(%_ASM_AX), %r10
122         mov VCPU_R11(%_ASM_AX), %r11
123         mov VCPU_R12(%_ASM_AX), %r12
124         mov VCPU_R13(%_ASM_AX), %r13
125         mov VCPU_R14(%_ASM_AX), %r14
126         mov VCPU_R15(%_ASM_AX), %r15
127 #endif
128         /* Load guest RAX.  This kills the @regs pointer! */
129         mov VCPU_RAX(%_ASM_AX), %_ASM_AX
130
131         /* Clobbers EFLAGS.ZF */
132         CLEAR_CPU_BUFFERS
133
134         /* Check EFLAGS.CF from the VMX_RUN_VMRESUME bit test above. */
135         jnc .Lvmlaunch
136
137         /*
138          * After a successful VMRESUME/VMLAUNCH, control flow "magically"
139          * resumes below at 'vmx_vmexit' due to the VMCS HOST_RIP setting.
140          * So this isn't a typical function and objtool needs to be told to
141          * save the unwind state here and restore it below.
142          */
143         UNWIND_HINT_SAVE
144
145 /*
146  * If VMRESUME/VMLAUNCH and corresponding vmexit succeed, execution resumes at
147  * the 'vmx_vmexit' label below.
148  */
149 .Lvmresume:
150         vmresume
151         jmp .Lvmfail
152
153 .Lvmlaunch:
154         vmlaunch
155         jmp .Lvmfail
156
157         _ASM_EXTABLE(.Lvmresume, .Lfixup)
158         _ASM_EXTABLE(.Lvmlaunch, .Lfixup)
159
160 SYM_INNER_LABEL(vmx_vmexit, SYM_L_GLOBAL)
161
162         /* Restore unwind state from before the VMRESUME/VMLAUNCH. */
163         UNWIND_HINT_RESTORE
164         ENDBR
165
166         /* Temporarily save guest's RAX. */
167         push %_ASM_AX
168
169         /* Reload @regs to RAX. */
170         mov WORD_SIZE(%_ASM_SP), %_ASM_AX
171
172         /* Save all guest registers, including RAX from the stack */
173         pop           VCPU_RAX(%_ASM_AX)
174         mov %_ASM_CX, VCPU_RCX(%_ASM_AX)
175         mov %_ASM_DX, VCPU_RDX(%_ASM_AX)
176         mov %_ASM_BX, VCPU_RBX(%_ASM_AX)
177         mov %_ASM_BP, VCPU_RBP(%_ASM_AX)
178         mov %_ASM_SI, VCPU_RSI(%_ASM_AX)
179         mov %_ASM_DI, VCPU_RDI(%_ASM_AX)
180 #ifdef CONFIG_X86_64
181         mov %r8,  VCPU_R8 (%_ASM_AX)
182         mov %r9,  VCPU_R9 (%_ASM_AX)
183         mov %r10, VCPU_R10(%_ASM_AX)
184         mov %r11, VCPU_R11(%_ASM_AX)
185         mov %r12, VCPU_R12(%_ASM_AX)
186         mov %r13, VCPU_R13(%_ASM_AX)
187         mov %r14, VCPU_R14(%_ASM_AX)
188         mov %r15, VCPU_R15(%_ASM_AX)
189 #endif
190
191         /* Clear return value to indicate VM-Exit (as opposed to VM-Fail). */
192         xor %ebx, %ebx
193
194 .Lclear_regs:
195         /* Discard @regs.  The register is irrelevant, it just can't be RBX. */
196         pop %_ASM_AX
197
198         /*
199          * Clear all general purpose registers except RSP and RBX to prevent
200          * speculative use of the guest's values, even those that are reloaded
201          * via the stack.  In theory, an L1 cache miss when restoring registers
202          * could lead to speculative execution with the guest's values.
203          * Zeroing XORs are dirt cheap, i.e. the extra paranoia is essentially
204          * free.  RSP and RBX are exempt as RSP is restored by hardware during
205          * VM-Exit and RBX is explicitly loaded with 0 or 1 to hold the return
206          * value.
207          */
208         xor %eax, %eax
209         xor %ecx, %ecx
210         xor %edx, %edx
211         xor %ebp, %ebp
212         xor %esi, %esi
213         xor %edi, %edi
214 #ifdef CONFIG_X86_64
215         xor %r8d,  %r8d
216         xor %r9d,  %r9d
217         xor %r10d, %r10d
218         xor %r11d, %r11d
219         xor %r12d, %r12d
220         xor %r13d, %r13d
221         xor %r14d, %r14d
222         xor %r15d, %r15d
223 #endif
224
225         /*
226          * IMPORTANT: RSB filling and SPEC_CTRL handling must be done before
227          * the first unbalanced RET after vmexit!
228          *
229          * For retpoline or IBRS, RSB filling is needed to prevent poisoned RSB
230          * entries and (in some cases) RSB underflow.
231          *
232          * eIBRS has its own protection against poisoned RSB, so it doesn't
233          * need the RSB filling sequence.  But it does need to be enabled, and a
234          * single call to retire, before the first unbalanced RET.
235          */
236
237         FILL_RETURN_BUFFER %_ASM_CX, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_VMEXIT,\
238                            X86_FEATURE_RSB_VMEXIT_LITE
239
240         pop %_ASM_ARG2  /* @flags */
241         pop %_ASM_ARG1  /* @vmx */
242
243         call vmx_spec_ctrl_restore_host
244
245         CLEAR_BRANCH_HISTORY_VMEXIT
246
247         /* Put return value in AX */
248         mov %_ASM_BX, %_ASM_AX
249
250         pop %_ASM_BX
251 #ifdef CONFIG_X86_64
252         pop %r12
253         pop %r13
254         pop %r14
255         pop %r15
256 #else
257         pop %esi
258         pop %edi
259 #endif
260         pop %_ASM_BP
261         RET
262
263 .Lfixup:
264         cmpb $0, kvm_rebooting
265         jne .Lvmfail
266         ud2
267 .Lvmfail:
268         /* VM-Fail: set return value to 1 */
269         mov $1, %_ASM_BX
270         jmp .Lclear_regs
271
272 SYM_FUNC_END(__vmx_vcpu_run)
273
274
275 .section .text, "ax"
276
277 /**
278  * vmread_error_trampoline - Trampoline from inline asm to vmread_error()
279  * @field:      VMCS field encoding that failed
280  * @fault:      %true if the VMREAD faulted, %false if it failed
281
282  * Save and restore volatile registers across a call to vmread_error().  Note,
283  * all parameters are passed on the stack.
284  */
285 SYM_FUNC_START(vmread_error_trampoline)
286         push %_ASM_BP
287         mov  %_ASM_SP, %_ASM_BP
288
289         push %_ASM_AX
290         push %_ASM_CX
291         push %_ASM_DX
292 #ifdef CONFIG_X86_64
293         push %rdi
294         push %rsi
295         push %r8
296         push %r9
297         push %r10
298         push %r11
299 #endif
300
301         /* Load @field and @fault to arg1 and arg2 respectively. */
302         mov 3*WORD_SIZE(%_ASM_BP), %_ASM_ARG2
303         mov 2*WORD_SIZE(%_ASM_BP), %_ASM_ARG1
304
305         call vmread_error
306
307         /* Zero out @fault, which will be popped into the result register. */
308         _ASM_MOV $0, 3*WORD_SIZE(%_ASM_BP)
309
310 #ifdef CONFIG_X86_64
311         pop %r11
312         pop %r10
313         pop %r9
314         pop %r8
315         pop %rsi
316         pop %rdi
317 #endif
318         pop %_ASM_DX
319         pop %_ASM_CX
320         pop %_ASM_AX
321         pop %_ASM_BP
322
323         RET
324 SYM_FUNC_END(vmread_error_trampoline)
325
326 SYM_FUNC_START(vmx_do_interrupt_nmi_irqoff)
327         /*
328          * Unconditionally create a stack frame, getting the correct RSP on the
329          * stack (for x86-64) would take two instructions anyways, and RBP can
330          * be used to restore RSP to make objtool happy (see below).
331          */
332         push %_ASM_BP
333         mov %_ASM_SP, %_ASM_BP
334
335 #ifdef CONFIG_X86_64
336         /*
337          * Align RSP to a 16-byte boundary (to emulate CPU behavior) before
338          * creating the synthetic interrupt stack frame for the IRQ/NMI.
339          */
340         and  $-16, %rsp
341         push $__KERNEL_DS
342         push %rbp
343 #endif
344         pushf
345         push $__KERNEL_CS
346         CALL_NOSPEC _ASM_ARG1
347
348         /*
349          * "Restore" RSP from RBP, even though IRET has already unwound RSP to
350          * the correct value.  objtool doesn't know the callee will IRET and,
351          * without the explicit restore, thinks the stack is getting walloped.
352          * Using an unwind hint is problematic due to x86-64's dynamic alignment.
353          */
354         mov %_ASM_BP, %_ASM_SP
355         pop %_ASM_BP
356         RET
357 SYM_FUNC_END(vmx_do_interrupt_nmi_irqoff)