1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2011 NetApp, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * This file and its contents are supplied under the terms of the
31 * Common Development and Distribution License ("CDDL"), version 1.0.
32 * You may only use this file in accordance with the terms of version
33 * 1.0 of the CDDL.
34 *
35 * A full copy of the text of the CDDL should have accompanied this
36 * source. A copy of the CDDL is also available via the Internet at
37 * http://www.illumos.org/license/CDDL.
38 */
39 /* This file is dual-licensed; see usr/src/contrib/bhyve/LICENSE */
40
41 /*
42 * Copyright 2018 Joyent, Inc.
43 * Copyright 2021 Oxide Computer Company
44 */
45
46 #ifndef _VMX_H_
47 #define _VMX_H_
48
49 #include "vmcs.h"
50
51 struct vmxctx {
52 uint64_t guest_rdi; /* Guest state */
53 uint64_t guest_rsi;
54 uint64_t guest_rdx;
55 uint64_t guest_rcx;
56 uint64_t guest_r8;
57 uint64_t guest_r9;
58 uint64_t guest_rax;
59 uint64_t guest_rbx;
60 uint64_t guest_rbp;
61 uint64_t guest_r10;
62 uint64_t guest_r11;
63 uint64_t guest_r12;
64 uint64_t guest_r13;
65 uint64_t guest_r14;
66 uint64_t guest_r15;
67 uint64_t guest_cr2;
68 uint64_t guest_dr0;
69 uint64_t guest_dr1;
70 uint64_t guest_dr2;
71 uint64_t guest_dr3;
72 uint64_t guest_dr6;
73
74 uint64_t host_dr0;
75 uint64_t host_dr1;
76 uint64_t host_dr2;
77 uint64_t host_dr3;
78 uint64_t host_dr6;
79 uint64_t host_dr7;
80 uint64_t host_debugctl;
81 int host_tf;
82
83 int inst_fail_status;
84 };
85
86 struct vmxcap {
87 int set;
88 uint32_t proc_ctls;
89 uint32_t proc_ctls2;
90 uint32_t exc_bitmap;
91 };
92
93 struct vmxstate {
94 uint64_t nextrip; /* next instruction to be executed by guest */
95 int lastcpu; /* host cpu that this 'vcpu' last ran on */
96 uint16_t vpid;
97 };
98
99 struct apic_page {
100 uint32_t reg[PAGE_SIZE / 4];
101 };
102 CTASSERT(sizeof (struct apic_page) == PAGE_SIZE);
103
104 /* Posted Interrupt Descriptor (described in section 29.6 of the Intel SDM) */
105 struct pir_desc {
106 uint32_t pir[8];
107 uint64_t pending;
108 uint64_t unused[3];
109 } __aligned(64);
110 CTASSERT(sizeof (struct pir_desc) == 64);
111
112 /* Index into the 'guest_msrs[]' array */
113 enum {
114 IDX_MSR_LSTAR,
115 IDX_MSR_CSTAR,
116 IDX_MSR_STAR,
117 IDX_MSR_SF_MASK,
118 IDX_MSR_KGSBASE,
119 IDX_MSR_PAT,
120 GUEST_MSR_NUM /* must be the last enumeration */
121 };
122
123 typedef enum {
124 VS_NONE = 0x0,
125 VS_LAUNCHED = 0x1,
126 VS_LOADED = 0x2
127 } vmcs_state_t;
128
129 /* virtual machine softc */
130 struct vmx {
131 struct vmcs vmcs[VM_MAXCPU]; /* one vmcs per virtual cpu */
132 struct apic_page apic_page[VM_MAXCPU]; /* one apic page per vcpu */
133 uint8_t *msr_bitmap[VM_MAXCPU]; /* one MSR bitmap per vCPU */
134 struct pir_desc pir_desc[VM_MAXCPU];
135 uint64_t guest_msrs[VM_MAXCPU][GUEST_MSR_NUM];
136 uint64_t host_msrs[VM_MAXCPU][GUEST_MSR_NUM];
137 uint64_t tsc_offset_active[VM_MAXCPU];
138 vmcs_state_t vmcs_state[VM_MAXCPU];
139 uintptr_t vmcs_pa[VM_MAXCPU];
140 void *apic_access_page;
141 struct vmxctx ctx[VM_MAXCPU];
142 struct vmxcap cap[VM_MAXCPU];
143 struct vmxstate state[VM_MAXCPU];
144 uint64_t eptp;
145 enum vmx_caps vmx_caps;
146 struct vm *vm;
147 /*
148 * Track the latest vmspace generation as it is run on a given host CPU.
149 * This allows us to react to modifications to the vmspace (such as
150 * unmap or changed protection) which necessitate flushing any
151 * guest-physical TLB entries tagged for this guest via 'invept'.
152 */
153 uint64_t eptgen[MAXCPU];
154 };
155 CTASSERT((offsetof(struct vmx, vmcs) & PAGE_MASK) == 0);
156 CTASSERT((offsetof(struct vmx, msr_bitmap) & PAGE_MASK) == 0);
157 CTASSERT((offsetof(struct vmx, pir_desc[0]) & 63) == 0);
158
159 static __inline bool
vmx_cap_en(const struct vmx * vmx,enum vmx_caps cap)160 vmx_cap_en(const struct vmx *vmx, enum vmx_caps cap)
161 {
162 return ((vmx->vmx_caps & cap) == cap);
163 }
164
165
166 /*
167 * Section 5.2 "Conventions" from Intel Architecture Manual 2B.
168 *
169 * error
170 * VMsucceed 0
171 * VMFailInvalid 1
172 * VMFailValid 2 see also VMCS VM-Instruction Error Field
173 */
174 #define VM_SUCCESS 0
175 #define VM_FAIL_INVALID 1
176 #define VM_FAIL_VALID 2
177 #define VMX_SET_ERROR_CODE_ASM \
178 " jnc 1f;" \
179 " mov $1, %[error];" /* CF: error = 1 */ \
180 " jmp 3f;" \
181 "1: jnz 2f;" \
182 " mov $2, %[error];" /* ZF: error = 2 */ \
183 " jmp 3f;" \
184 "2: mov $0, %[error];" \
185 "3:"
186
187
188 #define VMX_GUEST_VMEXIT 0
189 #define VMX_VMRESUME_ERROR 1
190 #define VMX_VMLAUNCH_ERROR 2
191 #define VMX_INVEPT_ERROR 3
192 #define VMX_VMWRITE_ERROR 4
193
194 int vmx_enter_guest(struct vmxctx *ctx, struct vmx *vmx, int launched);
195 void vmx_call_isr(uintptr_t entry);
196
197 int vmx_set_tsc_offset(struct vmx *vmx, int vcpu, uint64_t offset);
198
199 extern char vmx_exit_guest[];
200 extern char vmx_exit_guest_flush_rsb[];
201
202 #endif
203