1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 * $FreeBSD$
29 */
30 /*
31 * This file and its contents are supplied under the terms of the
32 * Common Development and Distribution License ("CDDL"), version 1.0.
33 * You may only use this file in accordance with the terms of version
34 * 1.0 of the CDDL.
35 *
36 * A full copy of the text of the CDDL should have accompanied this
37 * source. A copy of the CDDL is also available via the Internet at
38 * http://www.illumos.org/license/CDDL.
39 *
40 * Copyright 2015 Pluribus Networks Inc.
41 * Copyright 2019 Joyent, Inc.
42 * Copyright 2020 Oxide Computer Company
43 */
44
45 #ifndef _VMM_H_
46 #define _VMM_H_
47
48 enum vm_suspend_how {
49 VM_SUSPEND_NONE,
50 VM_SUSPEND_RESET,
51 VM_SUSPEND_POWEROFF,
52 VM_SUSPEND_HALT,
53 VM_SUSPEND_TRIPLEFAULT,
54 VM_SUSPEND_LAST
55 };
56
57 /*
58 * Identifiers for architecturally defined registers.
59 */
60 enum vm_reg_name {
61 VM_REG_GUEST_RAX,
62 VM_REG_GUEST_RBX,
63 VM_REG_GUEST_RCX,
64 VM_REG_GUEST_RDX,
65 VM_REG_GUEST_RSI,
66 VM_REG_GUEST_RDI,
67 VM_REG_GUEST_RBP,
68 VM_REG_GUEST_R8,
69 VM_REG_GUEST_R9,
70 VM_REG_GUEST_R10,
71 VM_REG_GUEST_R11,
72 VM_REG_GUEST_R12,
73 VM_REG_GUEST_R13,
74 VM_REG_GUEST_R14,
75 VM_REG_GUEST_R15,
76 VM_REG_GUEST_CR0,
77 VM_REG_GUEST_CR3,
78 VM_REG_GUEST_CR4,
79 VM_REG_GUEST_DR7,
80 VM_REG_GUEST_RSP,
81 VM_REG_GUEST_RIP,
82 VM_REG_GUEST_RFLAGS,
83 VM_REG_GUEST_ES,
84 VM_REG_GUEST_CS,
85 VM_REG_GUEST_SS,
86 VM_REG_GUEST_DS,
87 VM_REG_GUEST_FS,
88 VM_REG_GUEST_GS,
89 VM_REG_GUEST_LDTR,
90 VM_REG_GUEST_TR,
91 VM_REG_GUEST_IDTR,
92 VM_REG_GUEST_GDTR,
93 VM_REG_GUEST_EFER,
94 VM_REG_GUEST_CR2,
95 VM_REG_GUEST_PDPTE0,
96 VM_REG_GUEST_PDPTE1,
97 VM_REG_GUEST_PDPTE2,
98 VM_REG_GUEST_PDPTE3,
99 VM_REG_GUEST_INTR_SHADOW,
100 VM_REG_GUEST_DR0,
101 VM_REG_GUEST_DR1,
102 VM_REG_GUEST_DR2,
103 VM_REG_GUEST_DR3,
104 VM_REG_GUEST_DR6,
105 VM_REG_GUEST_ENTRY_INST_LENGTH,
106 VM_REG_LAST
107 };
108
109 enum x2apic_state {
110 X2APIC_DISABLED,
111 X2APIC_ENABLED,
112 X2APIC_STATE_LAST
113 };
114
115 #define VM_INTINFO_VECTOR(info) ((info) & 0xff)
116 #define VM_INTINFO_DEL_ERRCODE 0x800
117 #define VM_INTINFO_RSVD 0x7ffff000
118 #define VM_INTINFO_VALID 0x80000000
119 #define VM_INTINFO_TYPE 0x700
120 #define VM_INTINFO_HWINTR (0 << 8)
121 #define VM_INTINFO_NMI (2 << 8)
122 #define VM_INTINFO_HWEXCEPTION (3 << 8)
123 #define VM_INTINFO_SWINTR (4 << 8)
124
125 /*
126 * illumos doesn't have a limitation based on SPECNAMELEN like FreeBSD does.
127 * Instead of picking an arbitrary value we will just rely on the same
128 * calculation that's made below. If this calculation ever changes we need to
129 * update the the VM_MAX_NAMELEN mapping in the bhyve brand's boot.c file.
130 */
131
132 #define VM_MAX_PREFIXLEN 10
133 #define VM_MAX_SUFFIXLEN 15
134 #define VM_MIN_NAMELEN 6
135 #define VM_MAX_NAMELEN \
136 (SPECNAMELEN - VM_MAX_PREFIXLEN - VM_MAX_SUFFIXLEN - 1)
137
138 #ifdef _KERNEL
139 CTASSERT(VM_MAX_NAMELEN >= VM_MIN_NAMELEN);
140 #endif
141
142 #define VM_MAXCPU 32 /* maximum virtual cpus */
143
144 /*
145 * Identifiers for optional vmm capabilities
146 */
147 enum vm_cap_type {
148 VM_CAP_HALT_EXIT,
149 VM_CAP_MTRAP_EXIT,
150 VM_CAP_PAUSE_EXIT,
151 VM_CAP_ENABLE_INVPCID,
152 VM_CAP_BPT_EXIT,
153 VM_CAP_MAX
154 };
155
156 enum vmx_caps {
157 VMX_CAP_NONE = 0,
158 VMX_CAP_TPR_SHADOW = (1UL << 0),
159 VMX_CAP_APICV = (1UL << 1),
160 VMX_CAP_APICV_X2APIC = (1UL << 2),
161 VMX_CAP_APICV_PIR = (1UL << 3),
162 };
163
164 enum vm_intr_trigger {
165 EDGE_TRIGGER,
166 LEVEL_TRIGGER
167 };
168
169 /*
170 * The 'access' field has the format specified in Table 21-2 of the Intel
171 * Architecture Manual vol 3b.
172 *
173 * XXX The contents of the 'access' field are architecturally defined except
174 * bit 16 - Segment Unusable.
175 */
176 struct seg_desc {
177 uint64_t base;
178 uint32_t limit;
179 uint32_t access;
180 };
181 #define SEG_DESC_TYPE(access) ((access) & 0x001f)
182 #define SEG_DESC_DPL(access) (((access) >> 5) & 0x3)
183 #define SEG_DESC_PRESENT(access) (((access) & 0x0080) ? 1 : 0)
184 #define SEG_DESC_DEF32(access) (((access) & 0x4000) ? 1 : 0)
185 #define SEG_DESC_GRANULARITY(access) (((access) & 0x8000) ? 1 : 0)
186 #define SEG_DESC_UNUSABLE(access) (((access) & 0x10000) ? 1 : 0)
187
188 enum vm_cpu_mode {
189 CPU_MODE_REAL,
190 CPU_MODE_PROTECTED,
191 CPU_MODE_COMPATIBILITY, /* IA-32E mode (CS.L = 0) */
192 CPU_MODE_64BIT, /* IA-32E mode (CS.L = 1) */
193 };
194
195 enum vm_paging_mode {
196 PAGING_MODE_FLAT,
197 PAGING_MODE_32,
198 PAGING_MODE_PAE,
199 PAGING_MODE_64,
200 };
201
202 struct vm_guest_paging {
203 uint64_t cr3;
204 int cpl;
205 enum vm_cpu_mode cpu_mode;
206 enum vm_paging_mode paging_mode;
207 };
208
209 enum vm_exitcode {
210 VM_EXITCODE_INOUT,
211 VM_EXITCODE_VMX,
212 VM_EXITCODE_BOGUS,
213 VM_EXITCODE_RDMSR,
214 VM_EXITCODE_WRMSR,
215 VM_EXITCODE_HLT,
216 VM_EXITCODE_MTRAP,
217 VM_EXITCODE_PAUSE,
218 VM_EXITCODE_PAGING,
219 VM_EXITCODE_INST_EMUL,
220 VM_EXITCODE_RUN_STATE,
221 VM_EXITCODE_MMIO_EMUL,
222 VM_EXITCODE_DEPRECATED, /* formerly RUNBLOCK */
223 VM_EXITCODE_IOAPIC_EOI,
224 VM_EXITCODE_SUSPENDED,
225 VM_EXITCODE_MMIO,
226 VM_EXITCODE_TASK_SWITCH,
227 VM_EXITCODE_MONITOR,
228 VM_EXITCODE_MWAIT,
229 VM_EXITCODE_SVM,
230 VM_EXITCODE_REQIDLE,
231 VM_EXITCODE_DEBUG,
232 VM_EXITCODE_VMINSN,
233 VM_EXITCODE_BPT,
234 #ifndef __FreeBSD__
235 VM_EXITCODE_HT,
236 #endif
237 VM_EXITCODE_MAX
238 };
239
240 enum inout_flags {
241 INOUT_IN = (1U << 0), /* direction: 'in' when set, else 'out' */
242
243 /*
244 * The following flags are used only for in-kernel emulation logic and
245 * are not exposed to userspace.
246 */
247 INOUT_STR = (1U << 1), /* ins/outs operation */
248 INOUT_REP = (1U << 2), /* 'rep' prefix present on instruction */
249 };
250
251 struct vm_inout {
252 uint32_t eax;
253 uint16_t port;
254 uint8_t bytes; /* 1 or 2 or 4 */
255 uint8_t flags; /* see: inout_flags */
256
257 /*
258 * The address size and segment are relevant to INS/OUTS operations.
259 * Userspace is not concerned with them since the in-kernel emulation
260 * handles those specific aspects.
261 */
262 uint8_t addrsize;
263 uint8_t segment;
264 };
265
266 struct vm_mmio {
267 uint8_t bytes; /* 1/2/4/8 bytes */
268 uint8_t read; /* read: 1, write: 0 */
269 uint16_t _pad[3];
270 uint64_t gpa;
271 uint64_t data;
272 };
273
274 enum task_switch_reason {
275 TSR_CALL,
276 TSR_IRET,
277 TSR_JMP,
278 TSR_IDT_GATE, /* task gate in IDT */
279 };
280
281 struct vm_task_switch {
282 uint16_t tsssel; /* new TSS selector */
283 int ext; /* task switch due to external event */
284 uint32_t errcode;
285 int errcode_valid; /* push 'errcode' on the new stack */
286 enum task_switch_reason reason;
287 struct vm_guest_paging paging;
288 };
289
290 enum vcpu_run_state {
291 VRS_HALT = 0,
292 VRS_INIT = (1 << 0),
293 VRS_RUN = (1 << 1),
294
295 VRS_PEND_INIT = (1 << 14),
296 VRS_PEND_SIPI = (1 << 15),
297 };
298 #define VRS_MASK_VALID(v) \
299 ((v) & (VRS_INIT | VRS_RUN | VRS_PEND_SIPI | VRS_PEND_SIPI))
300 #define VRS_IS_VALID(v) ((v) == VRS_MASK_VALID(v))
301
302 struct vm_exit {
303 enum vm_exitcode exitcode;
304 int inst_length; /* 0 means unknown */
305 uint64_t rip;
306 union {
307 struct vm_inout inout;
308 struct vm_mmio mmio;
309 struct {
310 uint64_t gpa;
311 int fault_type;
312 } paging;
313 /*
314 * Kernel-internal MMIO decoding and emulation.
315 * Userspace should not expect to see this, but rather a
316 * VM_EXITCODE_MMIO with the above 'mmio' context.
317 */
318 struct {
319 uint64_t gpa;
320 uint64_t gla;
321 uint64_t cs_base;
322 int cs_d; /* CS.D */
323 } mmio_emul;
324 struct {
325 uint8_t inst[15];
326 uint8_t num_valid;
327 } inst_emul;
328 /*
329 * VMX specific payload. Used when there is no "better"
330 * exitcode to represent the VM-exit.
331 */
332 struct {
333 int status; /* vmx inst status */
334 /*
335 * 'exit_reason' and 'exit_qualification' are valid
336 * only if 'status' is zero.
337 */
338 uint32_t exit_reason;
339 uint64_t exit_qualification;
340 /*
341 * 'inst_error' and 'inst_type' are valid
342 * only if 'status' is non-zero.
343 */
344 int inst_type;
345 int inst_error;
346 } vmx;
347 /*
348 * SVM specific payload.
349 */
350 struct {
351 uint64_t exitcode;
352 uint64_t exitinfo1;
353 uint64_t exitinfo2;
354 } svm;
355 struct {
356 int inst_length;
357 } bpt;
358 struct {
359 uint32_t code; /* ecx value */
360 uint64_t wval;
361 } msr;
362 struct {
363 uint64_t rflags;
364 } hlt;
365 struct {
366 int vector;
367 } ioapic_eoi;
368 struct {
369 enum vm_suspend_how how;
370 } suspended;
371 struct vm_task_switch task_switch;
372 } u;
373 };
374
375 enum vm_entry_cmds {
376 VEC_DEFAULT = 0,
377 VEC_DISCARD_INSTR, /* discard inst emul state */
378 VEC_FULFILL_MMIO, /* entry includes result for mmio emul */
379 VEC_FULFILL_INOUT, /* entry includes result for inout emul */
380 };
381
382 struct vm_entry {
383 int cpuid;
384 uint_t cmd; /* see: vm_entry_cmds */
385 void *exit_data;
386 union {
387 struct vm_inout inout;
388 struct vm_mmio mmio;
389 } u;
390 };
391
392 int vm_restart_instruction(void *vm, int vcpuid);
393
394 #endif /* _VMM_H_ */