1 /*- 2 * Copyright (c) 2011 NetApp, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #ifndef _VMX_H_ 30 #define _VMX_H_ 31 32 #include "vmcs.h" 33 34 #define GUEST_MSR_MAX_ENTRIES 64 /* arbitrary */ 35 36 struct vmxctx { 37 register_t tmpstk[32]; /* vmx_return() stack */ 38 register_t tmpstktop; 39 40 register_t guest_rdi; /* Guest state */ 41 register_t guest_rsi; 42 register_t guest_rdx; 43 register_t guest_rcx; 44 register_t guest_r8; 45 register_t guest_r9; 46 register_t guest_rax; 47 register_t guest_rbx; 48 register_t guest_rbp; 49 register_t guest_r10; 50 register_t guest_r11; 51 register_t guest_r12; 52 register_t guest_r13; 53 register_t guest_r14; 54 register_t guest_r15; 55 register_t guest_cr2; 56 57 register_t host_r15; /* Host state */ 58 register_t host_r14; 59 register_t host_r13; 60 register_t host_r12; 61 register_t host_rbp; 62 register_t host_rsp; 63 register_t host_rbx; 64 register_t host_rip; 65 /* 66 * XXX todo debug registers and fpu state 67 */ 68 69 int launched; /* vmcs launch state */ 70 int launch_error; 71 }; 72 73 struct vmxcap { 74 int set; 75 uint32_t proc_ctls; 76 }; 77 78 struct vmxstate { 79 int lastcpu; /* host cpu that this 'vcpu' last ran on */ 80 uint16_t vpid; 81 }; 82 83 /* virtual machine softc */ 84 struct vmx { 85 pml4_entry_t pml4ept[NPML4EPG]; 86 struct vmcs vmcs[VM_MAXCPU]; /* one vmcs per virtual cpu */ 87 char msr_bitmap[PAGE_SIZE]; 88 struct msr_entry guest_msrs[VM_MAXCPU][GUEST_MSR_MAX_ENTRIES]; 89 struct vmxctx ctx[VM_MAXCPU]; 90 struct vmxcap cap[VM_MAXCPU]; 91 struct vmxstate state[VM_MAXCPU]; 92 struct vm *vm; 93 }; 94 CTASSERT((offsetof(struct vmx, pml4ept) & PAGE_MASK) == 0); 95 CTASSERT((offsetof(struct vmx, vmcs) & PAGE_MASK) == 0); 96 CTASSERT((offsetof(struct vmx, msr_bitmap) & PAGE_MASK) == 0); 97 CTASSERT((offsetof(struct vmx, guest_msrs) & 15) == 0); 98 99 #define VMX_RETURN_DIRECT 0 100 #define VMX_RETURN_LONGJMP 1 101 #define VMX_RETURN_VMRESUME 2 102 #define VMX_RETURN_VMLAUNCH 3 103 #define VMX_RETURN_AST 4 104 /* 105 * vmx_setjmp() returns: 106 * - 0 when it returns directly 107 * - 1 when it returns from vmx_longjmp 108 * - 2 when it returns from vmx_resume (which would only be in the error case) 109 * - 3 when it returns from vmx_launch (which would only be in the error case) 110 * - 4 when it returns from vmx_resume or vmx_launch because of AST pending 111 */ 112 int vmx_setjmp(struct vmxctx *ctx); 113 void vmx_longjmp(void); /* returns via vmx_setjmp */ 114 void vmx_launch(struct vmxctx *ctx) __dead2; /* may return via vmx_setjmp */ 115 void vmx_resume(struct vmxctx *ctx) __dead2; /* may return via vmx_setjmp */ 116 117 u_long vmx_fix_cr0(u_long cr0); 118 u_long vmx_fix_cr4(u_long cr4); 119 120 #endif 121