ddc3bf85db33a81b7950a35987cb417024092542
[linux-2.6-microblaze.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 <asm/segment.h>
8 #include "run_flags.h"
9
10 #define WORD_SIZE (BITS_PER_LONG / 8)
11
12 #define VCPU_RAX        __VCPU_REGS_RAX * WORD_SIZE
13 #define VCPU_RCX        __VCPU_REGS_RCX * WORD_SIZE
14 #define VCPU_RDX        __VCPU_REGS_RDX * WORD_SIZE
15 #define VCPU_RBX        __VCPU_REGS_RBX * WORD_SIZE
16 /* Intentionally omit RSP as it's context switched by hardware */
17 #define VCPU_RBP        __VCPU_REGS_RBP * WORD_SIZE
18 #define VCPU_RSI        __VCPU_REGS_RSI * WORD_SIZE
19 #define VCPU_RDI        __VCPU_REGS_RDI * WORD_SIZE
20
21 #ifdef CONFIG_X86_64
22 #define VCPU_R8         __VCPU_REGS_R8  * WORD_SIZE
23 #define VCPU_R9         __VCPU_REGS_R9  * WORD_SIZE
24 #define VCPU_R10        __VCPU_REGS_R10 * WORD_SIZE
25 #define VCPU_R11        __VCPU_REGS_R11 * WORD_SIZE
26 #define VCPU_R12        __VCPU_REGS_R12 * WORD_SIZE
27 #define VCPU_R13        __VCPU_REGS_R13 * WORD_SIZE
28 #define VCPU_R14        __VCPU_REGS_R14 * WORD_SIZE
29 #define VCPU_R15        __VCPU_REGS_R15 * WORD_SIZE
30 #endif
31
32 .section .noinstr.text, "ax"
33
34 /**
35  * __vmx_vcpu_run - Run a vCPU via a transition to VMX guest mode
36  * @vmx:        struct vcpu_vmx * (forwarded to vmx_update_host_rsp)
37  * @regs:       unsigned long * (to guest registers)
38  * @flags:      VMX_RUN_VMRESUME: use VMRESUME instead of VMLAUNCH
39  *
40  * Returns:
41  *      0 on VM-Exit, 1 on VM-Fail
42  */
43 SYM_FUNC_START(__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         /*
58          * Save @regs, _ASM_ARG2 may be modified by vmx_update_host_rsp() and
59          * @regs is needed after VM-Exit to save the guest's register values.
60          */
61         push %_ASM_ARG2
62
63         /* Copy @flags to BL, _ASM_ARG3 is volatile. */
64         mov %_ASM_ARG3B, %bl
65
66         lea (%_ASM_SP), %_ASM_ARG2
67         call vmx_update_host_rsp
68
69         /* Load @regs to RAX. */
70         mov (%_ASM_SP), %_ASM_AX
71
72         /* Check if vmlaunch or vmresume is needed */
73         testb $VMX_RUN_VMRESUME, %bl
74
75         /* Load guest registers.  Don't clobber flags. */
76         mov VCPU_RCX(%_ASM_AX), %_ASM_CX
77         mov VCPU_RDX(%_ASM_AX), %_ASM_DX
78         mov VCPU_RBX(%_ASM_AX), %_ASM_BX
79         mov VCPU_RBP(%_ASM_AX), %_ASM_BP
80         mov VCPU_RSI(%_ASM_AX), %_ASM_SI
81         mov VCPU_RDI(%_ASM_AX), %_ASM_DI
82 #ifdef CONFIG_X86_64
83         mov VCPU_R8 (%_ASM_AX),  %r8
84         mov VCPU_R9 (%_ASM_AX),  %r9
85         mov VCPU_R10(%_ASM_AX), %r10
86         mov VCPU_R11(%_ASM_AX), %r11
87         mov VCPU_R12(%_ASM_AX), %r12
88         mov VCPU_R13(%_ASM_AX), %r13
89         mov VCPU_R14(%_ASM_AX), %r14
90         mov VCPU_R15(%_ASM_AX), %r15
91 #endif
92         /* Load guest RAX.  This kills the @regs pointer! */
93         mov VCPU_RAX(%_ASM_AX), %_ASM_AX
94
95         /* Check EFLAGS.ZF from 'testb' above */
96         jz .Lvmlaunch
97
98         /*
99          * After a successful VMRESUME/VMLAUNCH, control flow "magically"
100          * resumes below at 'vmx_vmexit' due to the VMCS HOST_RIP setting.
101          * So this isn't a typical function and objtool needs to be told to
102          * save the unwind state here and restore it below.
103          */
104         UNWIND_HINT_SAVE
105
106 /*
107  * If VMRESUME/VMLAUNCH and corresponding vmexit succeed, execution resumes at
108  * the 'vmx_vmexit' label below.
109  */
110 .Lvmresume:
111         vmresume
112         jmp .Lvmfail
113
114 .Lvmlaunch:
115         vmlaunch
116         jmp .Lvmfail
117
118         _ASM_EXTABLE(.Lvmresume, .Lfixup)
119         _ASM_EXTABLE(.Lvmlaunch, .Lfixup)
120
121 SYM_INNER_LABEL(vmx_vmexit, SYM_L_GLOBAL)
122
123         /* Restore unwind state from before the VMRESUME/VMLAUNCH. */
124         UNWIND_HINT_RESTORE
125         ENDBR
126
127         /* Temporarily save guest's RAX. */
128         push %_ASM_AX
129
130         /* Reload @regs to RAX. */
131         mov WORD_SIZE(%_ASM_SP), %_ASM_AX
132
133         /* Save all guest registers, including RAX from the stack */
134         pop           VCPU_RAX(%_ASM_AX)
135         mov %_ASM_CX, VCPU_RCX(%_ASM_AX)
136         mov %_ASM_DX, VCPU_RDX(%_ASM_AX)
137         mov %_ASM_BX, VCPU_RBX(%_ASM_AX)
138         mov %_ASM_BP, VCPU_RBP(%_ASM_AX)
139         mov %_ASM_SI, VCPU_RSI(%_ASM_AX)
140         mov %_ASM_DI, VCPU_RDI(%_ASM_AX)
141 #ifdef CONFIG_X86_64
142         mov %r8,  VCPU_R8 (%_ASM_AX)
143         mov %r9,  VCPU_R9 (%_ASM_AX)
144         mov %r10, VCPU_R10(%_ASM_AX)
145         mov %r11, VCPU_R11(%_ASM_AX)
146         mov %r12, VCPU_R12(%_ASM_AX)
147         mov %r13, VCPU_R13(%_ASM_AX)
148         mov %r14, VCPU_R14(%_ASM_AX)
149         mov %r15, VCPU_R15(%_ASM_AX)
150 #endif
151
152         /* IMPORTANT: RSB must be stuffed before the first return. */
153         FILL_RETURN_BUFFER %_ASM_BX, RSB_CLEAR_LOOPS, X86_FEATURE_RETPOLINE
154
155         /* Clear RAX to indicate VM-Exit (as opposed to VM-Fail). */
156         xor %eax, %eax
157
158 .Lclear_regs:
159         /*
160          * Clear all general purpose registers except RSP and RAX to prevent
161          * speculative use of the guest's values, even those that are reloaded
162          * via the stack.  In theory, an L1 cache miss when restoring registers
163          * could lead to speculative execution with the guest's values.
164          * Zeroing XORs are dirt cheap, i.e. the extra paranoia is essentially
165          * free.  RSP and RAX are exempt as RSP is restored by hardware during
166          * VM-Exit and RAX is explicitly loaded with 0 or 1 to return VM-Fail.
167          */
168         xor %ecx, %ecx
169         xor %edx, %edx
170         xor %ebx, %ebx
171         xor %ebp, %ebp
172         xor %esi, %esi
173         xor %edi, %edi
174 #ifdef CONFIG_X86_64
175         xor %r8d,  %r8d
176         xor %r9d,  %r9d
177         xor %r10d, %r10d
178         xor %r11d, %r11d
179         xor %r12d, %r12d
180         xor %r13d, %r13d
181         xor %r14d, %r14d
182         xor %r15d, %r15d
183 #endif
184
185         /* "POP" @regs. */
186         add $WORD_SIZE, %_ASM_SP
187
188         pop %_ASM_BX
189 #ifdef CONFIG_X86_64
190         pop %r12
191         pop %r13
192         pop %r14
193         pop %r15
194 #else
195         pop %esi
196         pop %edi
197 #endif
198         pop %_ASM_BP
199         RET
200
201 .Lfixup:
202         cmpb $0, kvm_rebooting
203         jne .Lvmfail
204         ud2
205 .Lvmfail:
206         /* VM-Fail: set return value to 1 */
207         mov $1, %eax
208         jmp .Lclear_regs
209
210 SYM_FUNC_END(__vmx_vcpu_run)
211
212
213 .section .text, "ax"
214
215 /**
216  * vmread_error_trampoline - Trampoline from inline asm to vmread_error()
217  * @field:      VMCS field encoding that failed
218  * @fault:      %true if the VMREAD faulted, %false if it failed
219
220  * Save and restore volatile registers across a call to vmread_error().  Note,
221  * all parameters are passed on the stack.
222  */
223 SYM_FUNC_START(vmread_error_trampoline)
224         push %_ASM_BP
225         mov  %_ASM_SP, %_ASM_BP
226
227         push %_ASM_AX
228         push %_ASM_CX
229         push %_ASM_DX
230 #ifdef CONFIG_X86_64
231         push %rdi
232         push %rsi
233         push %r8
234         push %r9
235         push %r10
236         push %r11
237 #endif
238 #ifdef CONFIG_X86_64
239         /* Load @field and @fault to arg1 and arg2 respectively. */
240         mov 3*WORD_SIZE(%rbp), %_ASM_ARG2
241         mov 2*WORD_SIZE(%rbp), %_ASM_ARG1
242 #else
243         /* Parameters are passed on the stack for 32-bit (see asmlinkage). */
244         push 3*WORD_SIZE(%ebp)
245         push 2*WORD_SIZE(%ebp)
246 #endif
247
248         call vmread_error
249
250 #ifndef CONFIG_X86_64
251         add $8, %esp
252 #endif
253
254         /* Zero out @fault, which will be popped into the result register. */
255         _ASM_MOV $0, 3*WORD_SIZE(%_ASM_BP)
256
257 #ifdef CONFIG_X86_64
258         pop %r11
259         pop %r10
260         pop %r9
261         pop %r8
262         pop %rsi
263         pop %rdi
264 #endif
265         pop %_ASM_DX
266         pop %_ASM_CX
267         pop %_ASM_AX
268         pop %_ASM_BP
269
270         RET
271 SYM_FUNC_END(vmread_error_trampoline)
272
273 SYM_FUNC_START(vmx_do_interrupt_nmi_irqoff)
274         /*
275          * Unconditionally create a stack frame, getting the correct RSP on the
276          * stack (for x86-64) would take two instructions anyways, and RBP can
277          * be used to restore RSP to make objtool happy (see below).
278          */
279         push %_ASM_BP
280         mov %_ASM_SP, %_ASM_BP
281
282 #ifdef CONFIG_X86_64
283         /*
284          * Align RSP to a 16-byte boundary (to emulate CPU behavior) before
285          * creating the synthetic interrupt stack frame for the IRQ/NMI.
286          */
287         and  $-16, %rsp
288         push $__KERNEL_DS
289         push %rbp
290 #endif
291         pushf
292         push $__KERNEL_CS
293         CALL_NOSPEC _ASM_ARG1
294
295         /*
296          * "Restore" RSP from RBP, even though IRET has already unwound RSP to
297          * the correct value.  objtool doesn't know the callee will IRET and,
298          * without the explicit restore, thinks the stack is getting walloped.
299          * Using an unwind hint is problematic due to x86-64's dynamic alignment.
300          */
301         mov %_ASM_BP, %_ASM_SP
302         pop %_ASM_BP
303         RET
304 SYM_FUNC_END(vmx_do_interrupt_nmi_irqoff)