GNU Linux-libre 5.4.274-gnu1
[releases.git] / arch / 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 "run_flags.h"
8
9 #define WORD_SIZE (BITS_PER_LONG / 8)
10
11 #define VCPU_RAX        __VCPU_REGS_RAX * WORD_SIZE
12 #define VCPU_RCX        __VCPU_REGS_RCX * WORD_SIZE
13 #define VCPU_RDX        __VCPU_REGS_RDX * WORD_SIZE
14 #define VCPU_RBX        __VCPU_REGS_RBX * WORD_SIZE
15 /* Intentionally omit RSP as it's context switched by hardware */
16 #define VCPU_RBP        __VCPU_REGS_RBP * WORD_SIZE
17 #define VCPU_RSI        __VCPU_REGS_RSI * WORD_SIZE
18 #define VCPU_RDI        __VCPU_REGS_RDI * WORD_SIZE
19
20 #ifdef CONFIG_X86_64
21 #define VCPU_R8         __VCPU_REGS_R8  * WORD_SIZE
22 #define VCPU_R9         __VCPU_REGS_R9  * WORD_SIZE
23 #define VCPU_R10        __VCPU_REGS_R10 * WORD_SIZE
24 #define VCPU_R11        __VCPU_REGS_R11 * WORD_SIZE
25 #define VCPU_R12        __VCPU_REGS_R12 * WORD_SIZE
26 #define VCPU_R13        __VCPU_REGS_R13 * WORD_SIZE
27 #define VCPU_R14        __VCPU_REGS_R14 * WORD_SIZE
28 #define VCPU_R15        __VCPU_REGS_R15 * WORD_SIZE
29 #endif
30
31         .text
32
33 /**
34  * __vmx_vcpu_run - Run a vCPU via a transition to VMX guest mode
35  * @vmx:        struct vcpu_vmx *
36  * @regs:       unsigned long * (to guest registers)
37  * @flags:      VMX_RUN_VMRESUME:       use VMRESUME instead of VMLAUNCH
38  *              VMX_RUN_SAVE_SPEC_CTRL: save guest SPEC_CTRL into vmx->spec_ctrl
39  *
40  * Returns:
41  *      0 on VM-Exit, 1 on VM-Fail
42  */
43 ENTRY(__vmx_vcpu_run)
44         push %_ASM_BP
45         mov  %_ASM_SP, %_ASM_BP
46 #ifdef CONFIG_X86_64
47         push %r15
48         push %r14
49         push %r13
50         push %r12
51 #else
52         push %edi
53         push %esi
54 #endif
55         push %_ASM_BX
56
57         /* Save @vmx for SPEC_CTRL handling */
58         push %_ASM_ARG1
59
60         /* Save @flags for SPEC_CTRL handling */
61         push %_ASM_ARG3
62
63         /*
64          * Save @regs, _ASM_ARG2 may be modified by vmx_update_host_rsp() and
65          * @regs is needed after VM-Exit to save the guest's register values.
66          */
67         push %_ASM_ARG2
68
69         /* Copy @flags to BL, _ASM_ARG3 is volatile. */
70         mov %_ASM_ARG3B, %bl
71
72         lea (%_ASM_SP), %_ASM_ARG2
73         call vmx_update_host_rsp
74
75         /* Load @regs to RAX. */
76         mov (%_ASM_SP), %_ASM_AX
77
78         /* Check if vmlaunch or vmresume is needed */
79         testb $VMX_RUN_VMRESUME, %bl
80
81         /* Load guest registers.  Don't clobber flags. */
82         mov VCPU_RBX(%_ASM_AX), %_ASM_BX
83         mov VCPU_RCX(%_ASM_AX), %_ASM_CX
84         mov VCPU_RDX(%_ASM_AX), %_ASM_DX
85         mov VCPU_RSI(%_ASM_AX), %_ASM_SI
86         mov VCPU_RDI(%_ASM_AX), %_ASM_DI
87         mov VCPU_RBP(%_ASM_AX), %_ASM_BP
88 #ifdef CONFIG_X86_64
89         mov VCPU_R8 (%_ASM_AX),  %r8
90         mov VCPU_R9 (%_ASM_AX),  %r9
91         mov VCPU_R10(%_ASM_AX), %r10
92         mov VCPU_R11(%_ASM_AX), %r11
93         mov VCPU_R12(%_ASM_AX), %r12
94         mov VCPU_R13(%_ASM_AX), %r13
95         mov VCPU_R14(%_ASM_AX), %r14
96         mov VCPU_R15(%_ASM_AX), %r15
97 #endif
98         /* Load guest RAX.  This kills the @regs pointer! */
99         mov VCPU_RAX(%_ASM_AX), %_ASM_AX
100
101         /* Check EFLAGS.ZF from 'testb' above */
102         jz .Lvmlaunch
103
104 /*
105  * If VMRESUME/VMLAUNCH and corresponding vmexit succeed, execution resumes at
106  * the 'vmx_vmexit' label below.
107  */
108 .Lvmresume:
109         vmresume
110         jmp .Lvmfail
111
112 .Lvmlaunch:
113         vmlaunch
114         jmp .Lvmfail
115
116         _ASM_EXTABLE(.Lvmresume, .Lfixup)
117         _ASM_EXTABLE(.Lvmlaunch, .Lfixup)
118
119 SYM_INNER_LABEL(vmx_vmexit, SYM_L_GLOBAL)
120
121         /* Temporarily save guest's RAX. */
122         push %_ASM_AX
123
124         /* Reload @regs to RAX. */
125         mov WORD_SIZE(%_ASM_SP), %_ASM_AX
126
127         /* Save all guest registers, including RAX from the stack */
128         __ASM_SIZE(pop) VCPU_RAX(%_ASM_AX)
129         mov %_ASM_BX,   VCPU_RBX(%_ASM_AX)
130         mov %_ASM_CX,   VCPU_RCX(%_ASM_AX)
131         mov %_ASM_DX,   VCPU_RDX(%_ASM_AX)
132         mov %_ASM_SI,   VCPU_RSI(%_ASM_AX)
133         mov %_ASM_DI,   VCPU_RDI(%_ASM_AX)
134         mov %_ASM_BP,   VCPU_RBP(%_ASM_AX)
135 #ifdef CONFIG_X86_64
136         mov %r8,  VCPU_R8 (%_ASM_AX)
137         mov %r9,  VCPU_R9 (%_ASM_AX)
138         mov %r10, VCPU_R10(%_ASM_AX)
139         mov %r11, VCPU_R11(%_ASM_AX)
140         mov %r12, VCPU_R12(%_ASM_AX)
141         mov %r13, VCPU_R13(%_ASM_AX)
142         mov %r14, VCPU_R14(%_ASM_AX)
143         mov %r15, VCPU_R15(%_ASM_AX)
144 #endif
145
146         /* Clear return value to indicate VM-Exit (as opposed to VM-Fail). */
147         xor %ebx, %ebx
148
149 .Lclear_regs:
150         /*
151          * Clear all general purpose registers except RSP and RBX to prevent
152          * speculative use of the guest's values, even those that are reloaded
153          * via the stack.  In theory, an L1 cache miss when restoring registers
154          * could lead to speculative execution with the guest's values.
155          * Zeroing XORs are dirt cheap, i.e. the extra paranoia is essentially
156          * free.  RSP and RAX are exempt as RSP is restored by hardware during
157          * VM-Exit and RBX is explicitly loaded with 0 or 1 to hold the return
158          * value.
159          */
160         xor %eax, %eax
161         xor %ecx, %ecx
162         xor %edx, %edx
163         xor %esi, %esi
164         xor %edi, %edi
165         xor %ebp, %ebp
166 #ifdef CONFIG_X86_64
167         xor %r8d,  %r8d
168         xor %r9d,  %r9d
169         xor %r10d, %r10d
170         xor %r11d, %r11d
171         xor %r12d, %r12d
172         xor %r13d, %r13d
173         xor %r14d, %r14d
174         xor %r15d, %r15d
175 #endif
176
177         /* "POP" @regs. */
178         add $WORD_SIZE, %_ASM_SP
179
180         /*
181          * IMPORTANT: RSB filling and SPEC_CTRL handling must be done before
182          * the first unbalanced RET after vmexit!
183          *
184          * For retpoline or IBRS, RSB filling is needed to prevent poisoned RSB
185          * entries and (in some cases) RSB underflow.
186          *
187          * eIBRS has its own protection against poisoned RSB, so it doesn't
188          * need the RSB filling sequence.  But it does need to be enabled, and a
189          * single call to retire, before the first unbalanced RET.
190          */
191
192         FILL_RETURN_BUFFER %_ASM_CX, RSB_CLEAR_LOOPS, X86_FEATURE_RSB_VMEXIT,\
193                            X86_FEATURE_RSB_VMEXIT_LITE
194
195
196         pop %_ASM_ARG2  /* @flags */
197         pop %_ASM_ARG1  /* @vmx */
198
199         call vmx_spec_ctrl_restore_host
200
201         /* Put return value in AX */
202         mov %_ASM_BX, %_ASM_AX
203
204         pop %_ASM_BX
205 #ifdef CONFIG_X86_64
206         pop %r12
207         pop %r13
208         pop %r14
209         pop %r15
210 #else
211         pop %esi
212         pop %edi
213 #endif
214         pop %_ASM_BP
215         ret
216
217 .Lfixup:
218         cmpb $0, kvm_rebooting
219         jne .Lvmfail
220         ud2
221 .Lvmfail:
222         /* VM-Fail: set return value to 1 */
223         mov $1, %_ASM_BX
224         jmp .Lclear_regs
225
226 ENDPROC(__vmx_vcpu_run)
227
228
229 .section .text, "ax"
230
231 /**
232  * vmread_error_trampoline - Trampoline from inline asm to vmread_error()
233  * @field:      VMCS field encoding that failed
234  * @fault:      %true if the VMREAD faulted, %false if it failed
235
236  * Save and restore volatile registers across a call to vmread_error().  Note,
237  * all parameters are passed on the stack.
238  */
239 ENTRY(vmread_error_trampoline)
240         push %_ASM_BP
241         mov  %_ASM_SP, %_ASM_BP
242
243         push %_ASM_AX
244         push %_ASM_CX
245         push %_ASM_DX
246 #ifdef CONFIG_X86_64
247         push %rdi
248         push %rsi
249         push %r8
250         push %r9
251         push %r10
252         push %r11
253 #endif
254 #ifdef CONFIG_X86_64
255         /* Load @field and @fault to arg1 and arg2 respectively. */
256         mov 3*WORD_SIZE(%rbp), %_ASM_ARG2
257         mov 2*WORD_SIZE(%rbp), %_ASM_ARG1
258 #else
259         /* Parameters are passed on the stack for 32-bit (see asmlinkage). */
260         push 3*WORD_SIZE(%ebp)
261         push 2*WORD_SIZE(%ebp)
262 #endif
263
264         call vmread_error
265
266 #ifndef CONFIG_X86_64
267         add $8, %esp
268 #endif
269
270         /* Zero out @fault, which will be popped into the result register. */
271         _ASM_MOV $0, 3*WORD_SIZE(%_ASM_BP)
272
273 #ifdef CONFIG_X86_64
274         pop %r11
275         pop %r10
276         pop %r9
277         pop %r8
278         pop %rsi
279         pop %rdi
280 #endif
281         pop %_ASM_DX
282         pop %_ASM_CX
283         pop %_ASM_AX
284         pop %_ASM_BP
285
286         ret
287 ENDPROC(vmread_error_trampoline)