xref: /illumos-gate/usr/src/uts/intel/sys/vmm.h (revision 32640292)
1bf21cd93STycho Nightingale /*-
2*32640292SAndy Fiddaman  * SPDX-License-Identifier: BSD-2-Clause
34c87aefeSPatrick Mooney  *
4bf21cd93STycho Nightingale  * Copyright (c) 2011 NetApp, Inc.
5bf21cd93STycho Nightingale  * All rights reserved.
6bf21cd93STycho Nightingale  *
7bf21cd93STycho Nightingale  * Redistribution and use in source and binary forms, with or without
8bf21cd93STycho Nightingale  * modification, are permitted provided that the following conditions
9bf21cd93STycho Nightingale  * are met:
10bf21cd93STycho Nightingale  * 1. Redistributions of source code must retain the above copyright
11bf21cd93STycho Nightingale  *    notice, this list of conditions and the following disclaimer.
12bf21cd93STycho Nightingale  * 2. Redistributions in binary form must reproduce the above copyright
13bf21cd93STycho Nightingale  *    notice, this list of conditions and the following disclaimer in the
14bf21cd93STycho Nightingale  *    documentation and/or other materials provided with the distribution.
15bf21cd93STycho Nightingale  *
16bf21cd93STycho Nightingale  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17bf21cd93STycho Nightingale  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18bf21cd93STycho Nightingale  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19bf21cd93STycho Nightingale  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20bf21cd93STycho Nightingale  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21bf21cd93STycho Nightingale  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22bf21cd93STycho Nightingale  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23bf21cd93STycho Nightingale  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24bf21cd93STycho Nightingale  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25bf21cd93STycho Nightingale  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26bf21cd93STycho Nightingale  * SUCH DAMAGE.
27bf21cd93STycho Nightingale  */
28bf21cd93STycho Nightingale /*
29bf21cd93STycho Nightingale  * This file and its contents are supplied under the terms of the
30bf21cd93STycho Nightingale  * Common Development and Distribution License ("CDDL"), version 1.0.
31bf21cd93STycho Nightingale  * You may only use this file in accordance with the terms of version
32bf21cd93STycho Nightingale  * 1.0 of the CDDL.
33bf21cd93STycho Nightingale  *
34bf21cd93STycho Nightingale  * A full copy of the text of the CDDL should have accompanied this
35bf21cd93STycho Nightingale  * source.  A copy of the CDDL is also available via the Internet at
36bf21cd93STycho Nightingale  * http://www.illumos.org/license/CDDL.
37bf21cd93STycho Nightingale  *
38bf21cd93STycho Nightingale  * Copyright 2015 Pluribus Networks Inc.
394c87aefeSPatrick Mooney  * Copyright 2019 Joyent, Inc.
403d097f7dSPatrick Mooney  * Copyright 2022 Oxide Computer Company
41bf21cd93STycho Nightingale  */
42bf21cd93STycho Nightingale 
43bf21cd93STycho Nightingale #ifndef _VMM_H_
44bf21cd93STycho Nightingale #define	_VMM_H_
45bf21cd93STycho Nightingale 
46bf21cd93STycho Nightingale enum vm_suspend_how {
47bf21cd93STycho Nightingale 	VM_SUSPEND_NONE,
48bf21cd93STycho Nightingale 	VM_SUSPEND_RESET,
49bf21cd93STycho Nightingale 	VM_SUSPEND_POWEROFF,
50bf21cd93STycho Nightingale 	VM_SUSPEND_HALT,
514c87aefeSPatrick Mooney 	VM_SUSPEND_TRIPLEFAULT,
52bf21cd93STycho Nightingale 	VM_SUSPEND_LAST
53bf21cd93STycho Nightingale };
54bf21cd93STycho Nightingale 
55bf21cd93STycho Nightingale /*
56bf21cd93STycho Nightingale  * Identifiers for architecturally defined registers.
57bf21cd93STycho Nightingale  */
58bf21cd93STycho Nightingale enum vm_reg_name {
59bf21cd93STycho Nightingale 	VM_REG_GUEST_RAX,
60bf21cd93STycho Nightingale 	VM_REG_GUEST_RBX,
61bf21cd93STycho Nightingale 	VM_REG_GUEST_RCX,
62bf21cd93STycho Nightingale 	VM_REG_GUEST_RDX,
63bf21cd93STycho Nightingale 	VM_REG_GUEST_RSI,
64bf21cd93STycho Nightingale 	VM_REG_GUEST_RDI,
65bf21cd93STycho Nightingale 	VM_REG_GUEST_RBP,
66bf21cd93STycho Nightingale 	VM_REG_GUEST_R8,
67bf21cd93STycho Nightingale 	VM_REG_GUEST_R9,
68bf21cd93STycho Nightingale 	VM_REG_GUEST_R10,
69bf21cd93STycho Nightingale 	VM_REG_GUEST_R11,
70bf21cd93STycho Nightingale 	VM_REG_GUEST_R12,
71bf21cd93STycho Nightingale 	VM_REG_GUEST_R13,
72bf21cd93STycho Nightingale 	VM_REG_GUEST_R14,
73bf21cd93STycho Nightingale 	VM_REG_GUEST_R15,
74bf21cd93STycho Nightingale 	VM_REG_GUEST_CR0,
75bf21cd93STycho Nightingale 	VM_REG_GUEST_CR3,
76bf21cd93STycho Nightingale 	VM_REG_GUEST_CR4,
77bf21cd93STycho Nightingale 	VM_REG_GUEST_DR7,
78bf21cd93STycho Nightingale 	VM_REG_GUEST_RSP,
79bf21cd93STycho Nightingale 	VM_REG_GUEST_RIP,
80bf21cd93STycho Nightingale 	VM_REG_GUEST_RFLAGS,
81bf21cd93STycho Nightingale 	VM_REG_GUEST_ES,
82bf21cd93STycho Nightingale 	VM_REG_GUEST_CS,
83bf21cd93STycho Nightingale 	VM_REG_GUEST_SS,
84bf21cd93STycho Nightingale 	VM_REG_GUEST_DS,
85bf21cd93STycho Nightingale 	VM_REG_GUEST_FS,
86bf21cd93STycho Nightingale 	VM_REG_GUEST_GS,
87bf21cd93STycho Nightingale 	VM_REG_GUEST_LDTR,
88bf21cd93STycho Nightingale 	VM_REG_GUEST_TR,
89bf21cd93STycho Nightingale 	VM_REG_GUEST_IDTR,
90bf21cd93STycho Nightingale 	VM_REG_GUEST_GDTR,
91bf21cd93STycho Nightingale 	VM_REG_GUEST_EFER,
92bf21cd93STycho Nightingale 	VM_REG_GUEST_CR2,
934c87aefeSPatrick Mooney 	VM_REG_GUEST_PDPTE0,
944c87aefeSPatrick Mooney 	VM_REG_GUEST_PDPTE1,
954c87aefeSPatrick Mooney 	VM_REG_GUEST_PDPTE2,
964c87aefeSPatrick Mooney 	VM_REG_GUEST_PDPTE3,
974c87aefeSPatrick Mooney 	VM_REG_GUEST_INTR_SHADOW,
984c87aefeSPatrick Mooney 	VM_REG_GUEST_DR0,
994c87aefeSPatrick Mooney 	VM_REG_GUEST_DR1,
1004c87aefeSPatrick Mooney 	VM_REG_GUEST_DR2,
1014c87aefeSPatrick Mooney 	VM_REG_GUEST_DR3,
1024c87aefeSPatrick Mooney 	VM_REG_GUEST_DR6,
103154972afSPatrick Mooney 	VM_REG_GUEST_ENTRY_INST_LENGTH,
10454cf5b63SPatrick Mooney 	VM_REG_GUEST_XCR0,
105bf21cd93STycho Nightingale 	VM_REG_LAST
106bf21cd93STycho Nightingale };
107bf21cd93STycho Nightingale 
108bf21cd93STycho Nightingale enum x2apic_state {
109bf21cd93STycho Nightingale 	X2APIC_DISABLED,
110bf21cd93STycho Nightingale 	X2APIC_ENABLED,
111bf21cd93STycho Nightingale 	X2APIC_STATE_LAST
112bf21cd93STycho Nightingale };
113bf21cd93STycho Nightingale 
1143d097f7dSPatrick Mooney #define	VM_INTINFO_MASK_VECTOR	0xffUL
1153d097f7dSPatrick Mooney #define	VM_INTINFO_MASK_TYPE	0x700UL
1163d097f7dSPatrick Mooney #define	VM_INTINFO_MASK_RSVD	0x7ffff000UL
1173d097f7dSPatrick Mooney #define	VM_INTINFO_SHIFT_ERRCODE 32
1183d097f7dSPatrick Mooney 
1193d097f7dSPatrick Mooney #define	VM_INTINFO_VECTOR(val)	((val) & VM_INTINFO_MASK_VECTOR)
1203d097f7dSPatrick Mooney #define	VM_INTINFO_TYPE(val)	((val) & VM_INTINFO_MASK_TYPE)
1213d097f7dSPatrick Mooney #define	VM_INTINFO_ERRCODE(val)	((val) >> VM_INTINFO_SHIFT_ERRCODE)
1223d097f7dSPatrick Mooney #define	VM_INTINFO_PENDING(val)	(((val) & VM_INTINFO_VALID) != 0)
1233d097f7dSPatrick Mooney #define	VM_INTINFO_HAS_ERRCODE(val) (((val) & VM_INTINFO_DEL_ERRCODE) != 0)
1243d097f7dSPatrick Mooney 
1253d097f7dSPatrick Mooney #define	VM_INTINFO_VALID	(1UL << 31)
1263d097f7dSPatrick Mooney #define	VM_INTINFO_DEL_ERRCODE	(1UL << 11)
1273d097f7dSPatrick Mooney 
128bf21cd93STycho Nightingale #define	VM_INTINFO_HWINTR	(0 << 8)
129bf21cd93STycho Nightingale #define	VM_INTINFO_NMI		(2 << 8)
1303d097f7dSPatrick Mooney #define	VM_INTINFO_HWEXCP	(3 << 8)
131bf21cd93STycho Nightingale #define	VM_INTINFO_SWINTR	(4 << 8)
1323d097f7dSPatrick Mooney /* Reserved for CPU (read: Intel) specific types */
1333d097f7dSPatrick Mooney #define	VM_INTINFO_RESV1	(1 << 8)
1343d097f7dSPatrick Mooney #define	VM_INTINFO_RESV5	(5 << 8)
1353d097f7dSPatrick Mooney #define	VM_INTINFO_RESV6	(6 << 8)
1363d097f7dSPatrick Mooney #define	VM_INTINFO_RESV7	(7 << 8)
137bf21cd93STycho Nightingale 
13884659b24SMichael Zeller /*
13984659b24SMichael Zeller  * illumos doesn't have a limitation based on SPECNAMELEN like FreeBSD does.
140b57f5d3eSPatrick Mooney  * To simplify structure definitions, an arbitrary limit has been chosen.
141b57f5d3eSPatrick Mooney  * This same limit is used for memory segment names
14284659b24SMichael Zeller  */
143e0c0d44eSPatrick Mooney 
144b57f5d3eSPatrick Mooney #define	VM_MAX_NAMELEN		128
145b57f5d3eSPatrick Mooney #define	VM_MAX_SEG_NAMELEN	128
146bf21cd93STycho Nightingale 
147d7b72f7bSAndy Fiddaman #ifdef _KERNEL
1484c87aefeSPatrick Mooney #define	VM_MAXCPU	32			/* maximum virtual cpus */
149d7b72f7bSAndy Fiddaman #endif
150bf21cd93STycho Nightingale 
151bf21cd93STycho Nightingale /*
152bf21cd93STycho Nightingale  * Identifiers for optional vmm capabilities
153bf21cd93STycho Nightingale  */
154bf21cd93STycho Nightingale enum vm_cap_type {
155bf21cd93STycho Nightingale 	VM_CAP_HALT_EXIT,
156bf21cd93STycho Nightingale 	VM_CAP_MTRAP_EXIT,
157bf21cd93STycho Nightingale 	VM_CAP_PAUSE_EXIT,
158bf21cd93STycho Nightingale 	VM_CAP_ENABLE_INVPCID,
159154972afSPatrick Mooney 	VM_CAP_BPT_EXIT,
160bf21cd93STycho Nightingale 	VM_CAP_MAX
161bf21cd93STycho Nightingale };
162bf21cd93STycho Nightingale 
163c3ae3afaSPatrick Mooney enum vmx_caps {
164c3ae3afaSPatrick Mooney 	VMX_CAP_NONE		= 0,
165c3ae3afaSPatrick Mooney 	VMX_CAP_TPR_SHADOW	= (1UL << 0),
166c3ae3afaSPatrick Mooney 	VMX_CAP_APICV		= (1UL << 1),
167c3ae3afaSPatrick Mooney 	VMX_CAP_APICV_X2APIC	= (1UL << 2),
168c3ae3afaSPatrick Mooney 	VMX_CAP_APICV_PIR	= (1UL << 3),
169c3ae3afaSPatrick Mooney };
170c3ae3afaSPatrick Mooney 
171bf21cd93STycho Nightingale enum vm_intr_trigger {
172bf21cd93STycho Nightingale 	EDGE_TRIGGER,
173bf21cd93STycho Nightingale 	LEVEL_TRIGGER
174bf21cd93STycho Nightingale };
175b58b977eSPatrick Mooney 
176bf21cd93STycho Nightingale /*
177bf21cd93STycho Nightingale  * The 'access' field has the format specified in Table 21-2 of the Intel
178bf21cd93STycho Nightingale  * Architecture Manual vol 3b.
179bf21cd93STycho Nightingale  *
180bf21cd93STycho Nightingale  * XXX The contents of the 'access' field are architecturally defined except
181bf21cd93STycho Nightingale  * bit 16 - Segment Unusable.
182bf21cd93STycho Nightingale  */
183bf21cd93STycho Nightingale struct seg_desc {
184bf21cd93STycho Nightingale 	uint64_t	base;
185bf21cd93STycho Nightingale 	uint32_t	limit;
186bf21cd93STycho Nightingale 	uint32_t	access;
187bf21cd93STycho Nightingale };
188bf21cd93STycho Nightingale #define	SEG_DESC_TYPE(access)		((access) & 0x001f)
189e31d2280SPatrick Mooney #define	SEG_DESC_DPL_MASK		0x3
190e31d2280SPatrick Mooney #define	SEG_DESC_DPL_SHIFT		5
191e31d2280SPatrick Mooney #define	SEG_DESC_DPL(access)		\
192e31d2280SPatrick Mooney 	(((access) >> SEG_DESC_DPL_SHIFT) & SEG_DESC_DPL_MASK)
193bf21cd93STycho Nightingale #define	SEG_DESC_PRESENT(access)	(((access) & 0x0080) ? 1 : 0)
194bf21cd93STycho Nightingale #define	SEG_DESC_DEF32(access)		(((access) & 0x4000) ? 1 : 0)
195bf21cd93STycho Nightingale #define	SEG_DESC_GRANULARITY(access)	(((access) & 0x8000) ? 1 : 0)
196bf21cd93STycho Nightingale #define	SEG_DESC_UNUSABLE(access)	(((access) & 0x10000) ? 1 : 0)
197bf21cd93STycho Nightingale 
198bf21cd93STycho Nightingale enum vm_cpu_mode {
199bf21cd93STycho Nightingale 	CPU_MODE_REAL,
200bf21cd93STycho Nightingale 	CPU_MODE_PROTECTED,
201bf21cd93STycho Nightingale 	CPU_MODE_COMPATIBILITY,		/* IA-32E mode (CS.L = 0) */
202bf21cd93STycho Nightingale 	CPU_MODE_64BIT,			/* IA-32E mode (CS.L = 1) */
203bf21cd93STycho Nightingale };
204bf21cd93STycho Nightingale 
205bf21cd93STycho Nightingale enum vm_paging_mode {
206bf21cd93STycho Nightingale 	PAGING_MODE_FLAT,
207bf21cd93STycho Nightingale 	PAGING_MODE_32,
208bf21cd93STycho Nightingale 	PAGING_MODE_PAE,
209bf21cd93STycho Nightingale 	PAGING_MODE_64,
210bf21cd93STycho Nightingale };
211bf21cd93STycho Nightingale 
212bf21cd93STycho Nightingale struct vm_guest_paging {
213bf21cd93STycho Nightingale 	uint64_t	cr3;
214bf21cd93STycho Nightingale 	int		cpl;
215bf21cd93STycho Nightingale 	enum vm_cpu_mode cpu_mode;
216bf21cd93STycho Nightingale 	enum vm_paging_mode paging_mode;
217bf21cd93STycho Nightingale };
218bf21cd93STycho Nightingale 
219bf21cd93STycho Nightingale enum vm_exitcode {
220bf21cd93STycho Nightingale 	VM_EXITCODE_INOUT,
221bf21cd93STycho Nightingale 	VM_EXITCODE_VMX,
222bf21cd93STycho Nightingale 	VM_EXITCODE_BOGUS,
223bf21cd93STycho Nightingale 	VM_EXITCODE_RDMSR,
224bf21cd93STycho Nightingale 	VM_EXITCODE_WRMSR,
225bf21cd93STycho Nightingale 	VM_EXITCODE_HLT,
226bf21cd93STycho Nightingale 	VM_EXITCODE_MTRAP,
227bf21cd93STycho Nightingale 	VM_EXITCODE_PAUSE,
228bf21cd93STycho Nightingale 	VM_EXITCODE_PAGING,
229bf21cd93STycho Nightingale 	VM_EXITCODE_INST_EMUL,
2302606939dSPatrick Mooney 	VM_EXITCODE_RUN_STATE,
231e0c0d44eSPatrick Mooney 	VM_EXITCODE_MMIO_EMUL,
2322606939dSPatrick Mooney 	VM_EXITCODE_DEPRECATED,	/* formerly RUNBLOCK */
2334c87aefeSPatrick Mooney 	VM_EXITCODE_IOAPIC_EOI,
2344c87aefeSPatrick Mooney 	VM_EXITCODE_SUSPENDED,
235e0c0d44eSPatrick Mooney 	VM_EXITCODE_MMIO,
2364c87aefeSPatrick Mooney 	VM_EXITCODE_TASK_SWITCH,
2374c87aefeSPatrick Mooney 	VM_EXITCODE_MONITOR,
2384c87aefeSPatrick Mooney 	VM_EXITCODE_MWAIT,
2394c87aefeSPatrick Mooney 	VM_EXITCODE_SVM,
24072473353SPatrick Mooney 	VM_EXITCODE_DEPRECATED2, /* formerly REQIDLE */
2414c87aefeSPatrick Mooney 	VM_EXITCODE_DEBUG,
2424c87aefeSPatrick Mooney 	VM_EXITCODE_VMINSN,
243154972afSPatrick Mooney 	VM_EXITCODE_BPT,
2444c87aefeSPatrick Mooney 	VM_EXITCODE_HT,
245bf21cd93STycho Nightingale 	VM_EXITCODE_MAX
246bf21cd93STycho Nightingale };
247bf21cd93STycho Nightingale 
248e0c0d44eSPatrick Mooney enum inout_flags {
249e0c0d44eSPatrick Mooney 	INOUT_IN	= (1U << 0), /* direction: 'in' when set, else 'out' */
250e0c0d44eSPatrick Mooney 
251e0c0d44eSPatrick Mooney 	/*
252e0c0d44eSPatrick Mooney 	 * The following flags are used only for in-kernel emulation logic and
253e0c0d44eSPatrick Mooney 	 * are not exposed to userspace.
254e0c0d44eSPatrick Mooney 	 */
255e0c0d44eSPatrick Mooney 	INOUT_STR	= (1U << 1), /* ins/outs operation */
256e0c0d44eSPatrick Mooney 	INOUT_REP	= (1U << 2), /* 'rep' prefix present on instruction */
257e0c0d44eSPatrick Mooney };
258e0c0d44eSPatrick Mooney 
259bf21cd93STycho Nightingale struct vm_inout {
260e0c0d44eSPatrick Mooney 	uint32_t	eax;
261bf21cd93STycho Nightingale 	uint16_t	port;
262e0c0d44eSPatrick Mooney 	uint8_t		bytes;		/* 1 or 2 or 4 */
263e0c0d44eSPatrick Mooney 	uint8_t		flags;		/* see: inout_flags */
264e0c0d44eSPatrick Mooney 
265e0c0d44eSPatrick Mooney 	/*
266e0c0d44eSPatrick Mooney 	 * The address size and segment are relevant to INS/OUTS operations.
267e0c0d44eSPatrick Mooney 	 * Userspace is not concerned with them since the in-kernel emulation
268e0c0d44eSPatrick Mooney 	 * handles those specific aspects.
269e0c0d44eSPatrick Mooney 	 */
270e0c0d44eSPatrick Mooney 	uint8_t		addrsize;
271e0c0d44eSPatrick Mooney 	uint8_t		segment;
272bf21cd93STycho Nightingale };
273bf21cd93STycho Nightingale 
274e0c0d44eSPatrick Mooney struct vm_mmio {
275e0c0d44eSPatrick Mooney 	uint8_t		bytes;		/* 1/2/4/8 bytes */
276e0c0d44eSPatrick Mooney 	uint8_t		read;		/* read: 1, write: 0 */
277e0c0d44eSPatrick Mooney 	uint16_t	_pad[3];
278e0c0d44eSPatrick Mooney 	uint64_t	gpa;
279e0c0d44eSPatrick Mooney 	uint64_t	data;
280bf21cd93STycho Nightingale };
281bf21cd93STycho Nightingale 
2824c87aefeSPatrick Mooney enum task_switch_reason {
2834c87aefeSPatrick Mooney 	TSR_CALL,
2844c87aefeSPatrick Mooney 	TSR_IRET,
2854c87aefeSPatrick Mooney 	TSR_JMP,
2864c87aefeSPatrick Mooney 	TSR_IDT_GATE,	/* task gate in IDT */
2874c87aefeSPatrick Mooney };
2884c87aefeSPatrick Mooney 
2894c87aefeSPatrick Mooney struct vm_task_switch {
2904c87aefeSPatrick Mooney 	uint16_t	tsssel;		/* new TSS selector */
2914c87aefeSPatrick Mooney 	int		ext;		/* task switch due to external event */
2924c87aefeSPatrick Mooney 	uint32_t	errcode;
2934c87aefeSPatrick Mooney 	int		errcode_valid;	/* push 'errcode' on the new stack */
2944c87aefeSPatrick Mooney 	enum task_switch_reason reason;
2954c87aefeSPatrick Mooney 	struct vm_guest_paging paging;
2964c87aefeSPatrick Mooney };
2974c87aefeSPatrick Mooney 
2982606939dSPatrick Mooney enum vcpu_run_state {
2992606939dSPatrick Mooney 	VRS_HALT		= 0,
3002606939dSPatrick Mooney 	VRS_INIT		= (1 << 0),
3012606939dSPatrick Mooney 	VRS_RUN			= (1 << 1),
3022606939dSPatrick Mooney 
3032606939dSPatrick Mooney 	VRS_PEND_INIT		= (1 << 14),
3042606939dSPatrick Mooney 	VRS_PEND_SIPI		= (1 << 15),
3052606939dSPatrick Mooney };
3062606939dSPatrick Mooney #define VRS_MASK_VALID(v)	\
3072606939dSPatrick Mooney 	((v) & (VRS_INIT | VRS_RUN | VRS_PEND_SIPI | VRS_PEND_SIPI))
3082606939dSPatrick Mooney #define VRS_IS_VALID(v)		((v) == VRS_MASK_VALID(v))
3092606939dSPatrick Mooney 
310bf21cd93STycho Nightingale struct vm_exit {
311bf21cd93STycho Nightingale 	enum vm_exitcode	exitcode;
312bf21cd93STycho Nightingale 	int			inst_length;	/* 0 means unknown */
313bf21cd93STycho Nightingale 	uint64_t		rip;
314bf21cd93STycho Nightingale 	union {
315bf21cd93STycho Nightingale 		struct vm_inout	inout;
316e0c0d44eSPatrick Mooney 		struct vm_mmio	mmio;
317bf21cd93STycho Nightingale 		struct {
318bf21cd93STycho Nightingale 			uint64_t	gpa;
319bf21cd93STycho Nightingale 			int		fault_type;
320bf21cd93STycho Nightingale 		} paging;
321e0c0d44eSPatrick Mooney 		/*
322e0c0d44eSPatrick Mooney 		 * Kernel-internal MMIO decoding and emulation.
323e0c0d44eSPatrick Mooney 		 * Userspace should not expect to see this, but rather a
324e0c0d44eSPatrick Mooney 		 * VM_EXITCODE_MMIO with the above 'mmio' context.
325e0c0d44eSPatrick Mooney 		 */
326bf21cd93STycho Nightingale 		struct {
327bf21cd93STycho Nightingale 			uint64_t	gpa;
328bf21cd93STycho Nightingale 			uint64_t	gla;
329bf21cd93STycho Nightingale 			uint64_t	cs_base;
330bf21cd93STycho Nightingale 			int		cs_d;		/* CS.D */
331e0c0d44eSPatrick Mooney 		} mmio_emul;
332e0c0d44eSPatrick Mooney 		struct {
333e0c0d44eSPatrick Mooney 			uint8_t		inst[15];
334e0c0d44eSPatrick Mooney 			uint8_t		num_valid;
335bf21cd93STycho Nightingale 		} inst_emul;
336bf21cd93STycho Nightingale 		/*
337bf21cd93STycho Nightingale 		 * VMX specific payload. Used when there is no "better"
338bf21cd93STycho Nightingale 		 * exitcode to represent the VM-exit.
339bf21cd93STycho Nightingale 		 */
340bf21cd93STycho Nightingale 		struct {
341bf21cd93STycho Nightingale 			int		status;		/* vmx inst status */
342bf21cd93STycho Nightingale 			/*
343bf21cd93STycho Nightingale 			 * 'exit_reason' and 'exit_qualification' are valid
344bf21cd93STycho Nightingale 			 * only if 'status' is zero.
345bf21cd93STycho Nightingale 			 */
346bf21cd93STycho Nightingale 			uint32_t	exit_reason;
347bf21cd93STycho Nightingale 			uint64_t	exit_qualification;
348bf21cd93STycho Nightingale 			/*
349bf21cd93STycho Nightingale 			 * 'inst_error' and 'inst_type' are valid
350bf21cd93STycho Nightingale 			 * only if 'status' is non-zero.
351bf21cd93STycho Nightingale 			 */
352bf21cd93STycho Nightingale 			int		inst_type;
353bf21cd93STycho Nightingale 			int		inst_error;
354bf21cd93STycho Nightingale 		} vmx;
3554c87aefeSPatrick Mooney 		/*
3564c87aefeSPatrick Mooney 		 * SVM specific payload.
3574c87aefeSPatrick Mooney 		 */
3584c87aefeSPatrick Mooney 		struct {
3594c87aefeSPatrick Mooney 			uint64_t	exitcode;
3604c87aefeSPatrick Mooney 			uint64_t	exitinfo1;
3614c87aefeSPatrick Mooney 			uint64_t	exitinfo2;
3624c87aefeSPatrick Mooney 		} svm;
363154972afSPatrick Mooney 		struct {
364154972afSPatrick Mooney 			int		inst_length;
365154972afSPatrick Mooney 		} bpt;
366bf21cd93STycho Nightingale 		struct {
367bf21cd93STycho Nightingale 			uint32_t	code;		/* ecx value */
368bf21cd93STycho Nightingale 			uint64_t	wval;
369bf21cd93STycho Nightingale 		} msr;
370bf21cd93STycho Nightingale 		struct {
371bf21cd93STycho Nightingale 			uint64_t	rflags;
372bf21cd93STycho Nightingale 		} hlt;
3734c87aefeSPatrick Mooney 		struct {
3744c87aefeSPatrick Mooney 			int		vector;
3754c87aefeSPatrick Mooney 		} ioapic_eoi;
3764c87aefeSPatrick Mooney 		struct {
3774c87aefeSPatrick Mooney 			enum vm_suspend_how how;
37872473353SPatrick Mooney 			/*
37972473353SPatrick Mooney 			 * Source vcpuid for suspend status.  Typically -1,
38072473353SPatrick Mooney 			 * except for triple-fault events which occur on a
38172473353SPatrick Mooney 			 * specific faulting vCPU.
38272473353SPatrick Mooney 			 */
38372473353SPatrick Mooney 			int source;
38472473353SPatrick Mooney 			/*
38572473353SPatrick Mooney 			 * When suspend status was set on VM, measured in
38672473353SPatrick Mooney 			 * nanoseconds since VM boot.
38772473353SPatrick Mooney 			 */
38872473353SPatrick Mooney 			uint64_t when;
3894c87aefeSPatrick Mooney 		} suspended;
3904c87aefeSPatrick Mooney 		struct vm_task_switch task_switch;
391bf21cd93STycho Nightingale 	} u;
392bf21cd93STycho Nightingale };
393bf21cd93STycho Nightingale 
394e0c0d44eSPatrick Mooney enum vm_entry_cmds {
395e0c0d44eSPatrick Mooney 	VEC_DEFAULT = 0,
396e0c0d44eSPatrick Mooney 	VEC_DISCARD_INSTR,	/* discard inst emul state */
3972606939dSPatrick Mooney 	VEC_FULFILL_MMIO,	/* entry includes result for mmio emul */
3982606939dSPatrick Mooney 	VEC_FULFILL_INOUT,	/* entry includes result for inout emul */
39923d9a8faSPatrick Mooney 
40023d9a8faSPatrick Mooney 	/* Below are flags which can be combined with the above commands: */
40123d9a8faSPatrick Mooney 
40223d9a8faSPatrick Mooney 	/*
40323d9a8faSPatrick Mooney 	 * Exit to userspace when vCPU is in consistent state: when any pending
40423d9a8faSPatrick Mooney 	 * instruction emulation tasks have been completed and committed to the
40523d9a8faSPatrick Mooney 	 * architecturally defined state.
40623d9a8faSPatrick Mooney 	 */
40723d9a8faSPatrick Mooney 	VEC_FLAG_EXIT_CONSISTENT	= 1 << 31,
408e0c0d44eSPatrick Mooney };
409e0c0d44eSPatrick Mooney 
410e0c0d44eSPatrick Mooney struct vm_entry {
411e0c0d44eSPatrick Mooney 	int cpuid;
412e0c0d44eSPatrick Mooney 	uint_t cmd;		/* see: vm_entry_cmds */
413e0c0d44eSPatrick Mooney 	void *exit_data;
414e0c0d44eSPatrick Mooney 	union {
415e0c0d44eSPatrick Mooney 		struct vm_inout inout;
416e0c0d44eSPatrick Mooney 		struct vm_mmio mmio;
417e0c0d44eSPatrick Mooney 	} u;
418e0c0d44eSPatrick Mooney };
419e0c0d44eSPatrick Mooney 
420bf21cd93STycho Nightingale int vm_restart_instruction(void *vm, int vcpuid);
421bf21cd93STycho Nightingale 
422b57f5d3eSPatrick Mooney enum vm_create_flags {
423b57f5d3eSPatrick Mooney 	/*
424b57f5d3eSPatrick Mooney 	 * Allocate guest memory segments from existing reservoir capacity,
425b57f5d3eSPatrick Mooney 	 * rather than attempting to create transient allocations.
426b57f5d3eSPatrick Mooney 	 */
427b57f5d3eSPatrick Mooney 	VCF_RESERVOIR_MEM = (1 << 0),
4284ac713daSLuqman Aden 
4294ac713daSLuqman Aden 	/*
4304ac713daSLuqman Aden 	 * Enable dirty page tracking for the guest.
4314ac713daSLuqman Aden 	 */
4324ac713daSLuqman Aden 	VCF_TRACK_DIRTY = (1 << 1),
433b57f5d3eSPatrick Mooney };
434b57f5d3eSPatrick Mooney 
435578d9a56SPatrick Mooney /*
436578d9a56SPatrick Mooney  * Describes an entry for `cpuid` emulation.
437578d9a56SPatrick Mooney  * Used internally by bhyve (kernel) in addition to exposed ioctl(2) interface.
438578d9a56SPatrick Mooney  */
439578d9a56SPatrick Mooney struct vcpu_cpuid_entry {
440578d9a56SPatrick Mooney 	uint32_t	vce_function;
441578d9a56SPatrick Mooney 	uint32_t	vce_index;
442578d9a56SPatrick Mooney 	uint32_t	vce_flags;
443578d9a56SPatrick Mooney 	uint32_t	vce_eax;
444578d9a56SPatrick Mooney 	uint32_t	vce_ebx;
445578d9a56SPatrick Mooney 	uint32_t	vce_ecx;
446578d9a56SPatrick Mooney 	uint32_t	vce_edx;
447578d9a56SPatrick Mooney 	uint32_t	_pad;
448578d9a56SPatrick Mooney };
449578d9a56SPatrick Mooney 
450578d9a56SPatrick Mooney /*
451578d9a56SPatrick Mooney  * Defined flags for vcpu_cpuid_entry`vce_flags are below.
452578d9a56SPatrick Mooney  */
453578d9a56SPatrick Mooney 
454578d9a56SPatrick Mooney /* Use index (ecx) input value when matching entry */
455578d9a56SPatrick Mooney #define	VCE_FLAG_MATCH_INDEX		(1 << 0)
456578d9a56SPatrick Mooney 
457578d9a56SPatrick Mooney /* All valid flacts for vcpu_cpuid_entry`vce_flags */
458578d9a56SPatrick Mooney #define	VCE_FLAGS_VALID		VCE_FLAG_MATCH_INDEX
459578d9a56SPatrick Mooney 
460578d9a56SPatrick Mooney /*
461578d9a56SPatrick Mooney  * Defined flags for vcpu_cpuid configuration are below.
462578d9a56SPatrick Mooney  * These are used by both the ioctl(2) interface via vm_vcpu_cpuid_config and
463578d9a56SPatrick Mooney  * internally in the kernel vmm.
464578d9a56SPatrick Mooney  */
465578d9a56SPatrick Mooney 
466578d9a56SPatrick Mooney /* Use legacy hard-coded cpuid masking tables applied to the host CPU */
467578d9a56SPatrick Mooney #define	VCC_FLAG_LEGACY_HANDLING	(1 << 0)
468578d9a56SPatrick Mooney /*
469578d9a56SPatrick Mooney  * Emulate Intel-style fallback behavior (emit highest "standard" entry) if the
470578d9a56SPatrick Mooney  * queried function/index do not match.  If not set, emulate AMD-style, where
471578d9a56SPatrick Mooney  * all zeroes are returned in such cases.
472578d9a56SPatrick Mooney  */
473578d9a56SPatrick Mooney #define	VCC_FLAG_INTEL_FALLBACK		(1 << 1)
474578d9a56SPatrick Mooney 
475578d9a56SPatrick Mooney /* All valid flacts for vm_vcpu_cpuid_config`vvcc_flags */
476578d9a56SPatrick Mooney #define	VCC_FLAGS_VALID		\
477578d9a56SPatrick Mooney 	(VCC_FLAG_LEGACY_HANDLING | VCC_FLAG_INTEL_FALLBACK)
478578d9a56SPatrick Mooney 
479578d9a56SPatrick Mooney /* Maximum vcpu_cpuid_entry records per vCPU */
480578d9a56SPatrick Mooney #define	VMM_MAX_CPUID_ENTRIES		256
481578d9a56SPatrick Mooney 
482bf21cd93STycho Nightingale #endif	/* _VMM_H_ */
483