]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/vmm/intel/vmx_support.S
MFV: r329072
[FreeBSD/FreeBSD.git] / sys / amd64 / vmm / intel / vmx_support.S
1 /*-
2  * Copyright (c) 2011 NetApp, Inc.
3  * Copyright (c) 2013 Neel Natu <neel@freebsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29
30 #include <machine/asmacros.h>
31
32 #include "vmx_assym.h"
33
34 #ifdef SMP
35 #define LK      lock ;
36 #else
37 #define LK
38 #endif
39
40 /* Be friendly to DTrace FBT's prologue/epilogue pattern matching */
41 #define VENTER  push %rbp ; mov %rsp,%rbp
42 #define VLEAVE  pop %rbp
43
44 /*
45  * Assumes that %rdi holds a pointer to the 'vmxctx'.
46  *
47  * On "return" all registers are updated to reflect guest state. The two
48  * exceptions are %rip and %rsp. These registers are atomically switched
49  * by hardware from the guest area of the vmcs.
50  *
51  * We modify %rsp to point to the 'vmxctx' so we can use it to restore
52  * host context in case of an error with 'vmlaunch' or 'vmresume'.
53  */
54 #define VMX_GUEST_RESTORE                                               \
55         movq    %rdi,%rsp;                                              \
56         movq    VMXCTX_GUEST_CR2(%rdi),%rsi;                            \
57         movq    %rsi,%cr2;                                              \
58         movq    VMXCTX_GUEST_RSI(%rdi),%rsi;                            \
59         movq    VMXCTX_GUEST_RDX(%rdi),%rdx;                            \
60         movq    VMXCTX_GUEST_RCX(%rdi),%rcx;                            \
61         movq    VMXCTX_GUEST_R8(%rdi),%r8;                              \
62         movq    VMXCTX_GUEST_R9(%rdi),%r9;                              \
63         movq    VMXCTX_GUEST_RAX(%rdi),%rax;                            \
64         movq    VMXCTX_GUEST_RBX(%rdi),%rbx;                            \
65         movq    VMXCTX_GUEST_RBP(%rdi),%rbp;                            \
66         movq    VMXCTX_GUEST_R10(%rdi),%r10;                            \
67         movq    VMXCTX_GUEST_R11(%rdi),%r11;                            \
68         movq    VMXCTX_GUEST_R12(%rdi),%r12;                            \
69         movq    VMXCTX_GUEST_R13(%rdi),%r13;                            \
70         movq    VMXCTX_GUEST_R14(%rdi),%r14;                            \
71         movq    VMXCTX_GUEST_R15(%rdi),%r15;                            \
72         movq    VMXCTX_GUEST_RDI(%rdi),%rdi; /* restore rdi the last */
73
74 /*
75  * Clobber the remaining registers with guest contents so they can't
76  * be misused.
77  */
78 #define VMX_GUEST_CLOBBER                                               \
79         xor     %rax, %rax;                                             \
80         xor     %rcx, %rcx;                                             \
81         xor     %rdx, %rdx;                                             \
82         xor     %rsi, %rsi;                                             \
83         xor     %r8, %r8;                                               \
84         xor     %r9, %r9;                                               \
85         xor     %r10, %r10;                                             \
86         xor     %r11, %r11;
87
88 /*
89  * Save and restore the host context.
90  *
91  * Assumes that %rdi holds a pointer to the 'vmxctx'.
92  */
93 #define VMX_HOST_SAVE                                                   \
94         movq    %r15, VMXCTX_HOST_R15(%rdi);                            \
95         movq    %r14, VMXCTX_HOST_R14(%rdi);                            \
96         movq    %r13, VMXCTX_HOST_R13(%rdi);                            \
97         movq    %r12, VMXCTX_HOST_R12(%rdi);                            \
98         movq    %rbp, VMXCTX_HOST_RBP(%rdi);                            \
99         movq    %rsp, VMXCTX_HOST_RSP(%rdi);                            \
100         movq    %rbx, VMXCTX_HOST_RBX(%rdi);                            \
101
102 #define VMX_HOST_RESTORE                                                \
103         movq    VMXCTX_HOST_R15(%rdi), %r15;                            \
104         movq    VMXCTX_HOST_R14(%rdi), %r14;                            \
105         movq    VMXCTX_HOST_R13(%rdi), %r13;                            \
106         movq    VMXCTX_HOST_R12(%rdi), %r12;                            \
107         movq    VMXCTX_HOST_RBP(%rdi), %rbp;                            \
108         movq    VMXCTX_HOST_RSP(%rdi), %rsp;                            \
109         movq    VMXCTX_HOST_RBX(%rdi), %rbx;                            \
110
111 /*
112  * vmx_enter_guest(struct vmxctx *vmxctx, int launched)
113  * %rdi: pointer to the 'vmxctx'
114  * %rsi: pointer to the 'vmx'
115  * %edx: launch state of the VMCS
116  * Interrupts must be disabled on entry.
117  */
118 ENTRY(vmx_enter_guest)
119         VENTER
120         /*
121          * Save host state before doing anything else.
122          */
123         VMX_HOST_SAVE
124
125         /*
126          * Activate guest pmap on this cpu.
127          */
128         movq    VMXCTX_PMAP(%rdi), %r11
129         movl    PCPU(CPUID), %eax
130         LK btsl %eax, PM_ACTIVE(%r11)
131
132         /*
133          * If 'vmx->eptgen[curcpu]' is not identical to 'pmap->pm_eptgen'
134          * then we must invalidate all mappings associated with this EPTP.
135          */
136         movq    PM_EPTGEN(%r11), %r10
137         cmpq    %r10, VMX_EPTGEN(%rsi, %rax, 8)
138         je      guest_restore
139
140         /* Refresh 'vmx->eptgen[curcpu]' */
141         movq    %r10, VMX_EPTGEN(%rsi, %rax, 8)
142
143         /* Setup the invept descriptor on the host stack */
144         mov     %rsp, %r11
145         movq    VMX_EPTP(%rsi), %rax
146         movq    %rax, -16(%r11)
147         movq    $0x0, -8(%r11)
148         mov     $0x1, %eax              /* Single context invalidate */
149         invept  -16(%r11), %rax
150         jbe     invept_error            /* Check invept instruction error */
151
152 guest_restore:
153         cmpl    $0, %edx
154         je      do_launch
155
156         VMX_GUEST_RESTORE
157         vmresume
158         /*
159          * In the common case 'vmresume' returns back to the host through
160          * 'vmx_exit_guest' with %rsp pointing to 'vmxctx'.
161          *
162          * If there is an error we return VMX_VMRESUME_ERROR to the caller.
163          */
164         movq    %rsp, %rdi              /* point %rdi back to 'vmxctx' */
165         movl    $VMX_VMRESUME_ERROR, %eax
166         jmp     decode_inst_error
167
168 do_launch:
169         VMX_GUEST_RESTORE
170         vmlaunch
171         /*
172          * In the common case 'vmlaunch' returns back to the host through
173          * 'vmx_exit_guest' with %rsp pointing to 'vmxctx'.
174          *
175          * If there is an error we return VMX_VMLAUNCH_ERROR to the caller.
176          */
177         movq    %rsp, %rdi              /* point %rdi back to 'vmxctx' */
178         movl    $VMX_VMLAUNCH_ERROR, %eax
179         jmp     decode_inst_error
180
181 invept_error:
182         movl    $VMX_INVEPT_ERROR, %eax
183         jmp     decode_inst_error
184
185 decode_inst_error:
186         movl    $VM_FAIL_VALID, %r11d
187         jz      inst_error
188         movl    $VM_FAIL_INVALID, %r11d
189 inst_error:
190         movl    %r11d, VMXCTX_INST_FAIL_STATUS(%rdi)
191
192         /*
193          * The return value is already populated in %eax so we cannot use
194          * it as a scratch register beyond this point.
195          */
196
197         /*
198          * Deactivate guest pmap from this cpu.
199          */
200         movq    VMXCTX_PMAP(%rdi), %r11
201         movl    PCPU(CPUID), %r10d
202         LK btrl %r10d, PM_ACTIVE(%r11)
203
204         VMX_HOST_RESTORE
205         VLEAVE
206         ret
207
208 /*
209  * Non-error VM-exit from the guest. Make this a label so it can
210  * be used by C code when setting up the VMCS.
211  * The VMCS-restored %rsp points to the struct vmxctx
212  */
213         ALIGN_TEXT
214         .globl  vmx_exit_guest
215 vmx_exit_guest:
216         /*
217          * Save guest state that is not automatically saved in the vmcs.
218          */
219         movq    %rdi,VMXCTX_GUEST_RDI(%rsp)
220         movq    %rsi,VMXCTX_GUEST_RSI(%rsp)
221         movq    %rdx,VMXCTX_GUEST_RDX(%rsp)
222         movq    %rcx,VMXCTX_GUEST_RCX(%rsp)
223         movq    %r8,VMXCTX_GUEST_R8(%rsp)
224         movq    %r9,VMXCTX_GUEST_R9(%rsp)
225         movq    %rax,VMXCTX_GUEST_RAX(%rsp)
226         movq    %rbx,VMXCTX_GUEST_RBX(%rsp)
227         movq    %rbp,VMXCTX_GUEST_RBP(%rsp)
228         movq    %r10,VMXCTX_GUEST_R10(%rsp)
229         movq    %r11,VMXCTX_GUEST_R11(%rsp)
230         movq    %r12,VMXCTX_GUEST_R12(%rsp)
231         movq    %r13,VMXCTX_GUEST_R13(%rsp)
232         movq    %r14,VMXCTX_GUEST_R14(%rsp)
233         movq    %r15,VMXCTX_GUEST_R15(%rsp)
234
235         movq    %cr2,%rdi
236         movq    %rdi,VMXCTX_GUEST_CR2(%rsp)
237
238         movq    %rsp,%rdi
239
240         /*
241          * Deactivate guest pmap from this cpu.
242          */
243         movq    VMXCTX_PMAP(%rdi), %r11
244         movl    PCPU(CPUID), %r10d
245         LK btrl %r10d, PM_ACTIVE(%r11)
246
247         VMX_HOST_RESTORE
248
249         VMX_GUEST_CLOBBER
250
251         /*
252          * This will return to the caller of 'vmx_enter_guest()' with a return
253          * value of VMX_GUEST_VMEXIT.
254          */
255         movl    $VMX_GUEST_VMEXIT, %eax
256         VLEAVE
257         ret
258 END(vmx_enter_guest)
259
260 /*
261  * %rdi = interrupt handler entry point
262  *
263  * Calling sequence described in the "Instruction Set Reference" for the "INT"
264  * instruction in Intel SDM, Vol 2.
265  */
266 ENTRY(vmx_call_isr)
267         VENTER
268         mov     %rsp, %r11                      /* save %rsp */
269         and     $~0xf, %rsp                     /* align on 16-byte boundary */
270         pushq   $KERNEL_SS                      /* %ss */
271         pushq   %r11                            /* %rsp */
272         pushfq                                  /* %rflags */
273         pushq   $KERNEL_CS                      /* %cs */
274         cli                                     /* disable interrupts */
275         callq   *%rdi                           /* push %rip and call isr */
276         VLEAVE
277         ret
278 END(vmx_call_isr)