xref: /freebsd/sys/amd64/include/vmm.h (revision 5f13d6b60740c021951ae0e4d096903cfa1679e2)
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 #ifndef _VMM_H_
30 #define	_VMM_H_
31 
32 #include <sys/cpuset.h>
33 #include <sys/sdt.h>
34 #include <x86/segments.h>
35 
36 struct vcpu;
37 struct vm_snapshot_meta;
38 
39 #ifdef _KERNEL
40 SDT_PROVIDER_DECLARE(vmm);
41 #endif
42 
43 enum vm_suspend_how {
44 	VM_SUSPEND_NONE,
45 	VM_SUSPEND_RESET,
46 	VM_SUSPEND_POWEROFF,
47 	VM_SUSPEND_HALT,
48 	VM_SUSPEND_TRIPLEFAULT,
49 	VM_SUSPEND_DESTROY,
50 	VM_SUSPEND_LAST
51 };
52 
53 /*
54  * Identifiers for architecturally defined registers.
55  */
56 enum vm_reg_name {
57 	VM_REG_GUEST_RAX,
58 	VM_REG_GUEST_RBX,
59 	VM_REG_GUEST_RCX,
60 	VM_REG_GUEST_RDX,
61 	VM_REG_GUEST_RSI,
62 	VM_REG_GUEST_RDI,
63 	VM_REG_GUEST_RBP,
64 	VM_REG_GUEST_R8,
65 	VM_REG_GUEST_R9,
66 	VM_REG_GUEST_R10,
67 	VM_REG_GUEST_R11,
68 	VM_REG_GUEST_R12,
69 	VM_REG_GUEST_R13,
70 	VM_REG_GUEST_R14,
71 	VM_REG_GUEST_R15,
72 	VM_REG_GUEST_CR0,
73 	VM_REG_GUEST_CR3,
74 	VM_REG_GUEST_CR4,
75 	VM_REG_GUEST_DR7,
76 	VM_REG_GUEST_RSP,
77 	VM_REG_GUEST_RIP,
78 	VM_REG_GUEST_RFLAGS,
79 	VM_REG_GUEST_ES,
80 	VM_REG_GUEST_CS,
81 	VM_REG_GUEST_SS,
82 	VM_REG_GUEST_DS,
83 	VM_REG_GUEST_FS,
84 	VM_REG_GUEST_GS,
85 	VM_REG_GUEST_LDTR,
86 	VM_REG_GUEST_TR,
87 	VM_REG_GUEST_IDTR,
88 	VM_REG_GUEST_GDTR,
89 	VM_REG_GUEST_EFER,
90 	VM_REG_GUEST_CR2,
91 	VM_REG_GUEST_PDPTE0,
92 	VM_REG_GUEST_PDPTE1,
93 	VM_REG_GUEST_PDPTE2,
94 	VM_REG_GUEST_PDPTE3,
95 	VM_REG_GUEST_INTR_SHADOW,
96 	VM_REG_GUEST_DR0,
97 	VM_REG_GUEST_DR1,
98 	VM_REG_GUEST_DR2,
99 	VM_REG_GUEST_DR3,
100 	VM_REG_GUEST_DR6,
101 	VM_REG_GUEST_ENTRY_INST_LENGTH,
102 	VM_REG_GUEST_FS_BASE,
103 	VM_REG_GUEST_GS_BASE,
104 	VM_REG_GUEST_KGS_BASE,
105 	VM_REG_GUEST_TPR,
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 #ifdef _KERNEL
126 #define	VMM_VCPU_MD_FIELDS						\
127 	struct vlapic	*vlapic;	/* (i) APIC device model */	\
128 	enum x2apic_state x2apic_state;	/* (i) APIC mode */		\
129 	uint64_t	exitintinfo;	/* (i) events pending at VM exit */ \
130 	int		nmi_pending;	/* (i) NMI pending */		\
131 	int		extint_pending;	/* (i) INTR pending */		\
132 	int		exception_pending; /* (i) exception pending */	\
133 	int		exc_vector;	/* (x) exception collateral */	\
134 	int		exc_errcode_valid;				\
135 	uint32_t	exc_errcode;					\
136 	struct savefpu	*guestfpu;	/* (a,i) guest fpu state */	\
137 	uint64_t	guest_xcr0;	/* (i) guest %xcr0 register */	\
138 	struct vm_exit	exitinfo;	/* (x) exit reason and collateral */ \
139 	cpuset_t	exitinfo_cpuset; /* (x) storage for vmexit handlers */ \
140 	uint64_t	nextrip;	/* (x) next instruction to execute */ \
141 	uint64_t	tsc_offset	/* (o) TSC offsetting */
142 
143 #define	VMM_VM_MD_FIELDS						\
144 	cpuset_t	startup_cpus;	/* (i) [r] waiting for startup */ \
145 	void		*iommu;		/* (x) iommu-specific data */	\
146 	struct vioapic	*vioapic;	/* (i) virtual ioapic */	\
147 	struct vatpic	*vatpic;	/* (i) virtual atpic */		\
148 	struct vatpit	*vatpit;	/* (i) virtual atpit */		\
149 	struct vpmtmr	*vpmtmr;	/* (i) virtual ACPI PM timer */	\
150 	struct vrtc	*vrtc;		/* (o) virtual RTC */		\
151 	struct vhpet	*vhpet		/* (i) virtual HPET */
152 
153 struct vm;
154 struct vm_exception;
155 struct vm_mem;
156 struct seg_desc;
157 struct vm_exit;
158 struct vm_run;
159 struct vhpet;
160 struct vioapic;
161 struct vlapic;
162 struct vmspace;
163 struct vm_eventinfo;
164 struct vm_object;
165 struct vm_guest_paging;
166 struct pmap;
167 enum snapshot_req;
168 
169 #define	DECLARE_VMMOPS_FUNC(ret_type, opname, args)		\
170 	typedef ret_type (*vmmops_##opname##_t) args;		\
171 	ret_type vmmops_##opname args
172 
173 DECLARE_VMMOPS_FUNC(int, modinit, (int ipinum));
174 DECLARE_VMMOPS_FUNC(int, modcleanup, (void));
175 DECLARE_VMMOPS_FUNC(void, modresume, (void));
176 DECLARE_VMMOPS_FUNC(void, modsuspend, (void));
177 DECLARE_VMMOPS_FUNC(void *, init, (struct vm *vm, struct pmap *pmap));
178 DECLARE_VMMOPS_FUNC(int, run, (void *vcpui, register_t pc,
179     struct pmap *pmap, struct vm_eventinfo *info));
180 DECLARE_VMMOPS_FUNC(void, cleanup, (void *vmi));
181 DECLARE_VMMOPS_FUNC(void *, vcpu_init, (void *vmi, struct vcpu *vcpu,
182     int vcpu_id));
183 DECLARE_VMMOPS_FUNC(void, vcpu_cleanup, (void *vcpui));
184 DECLARE_VMMOPS_FUNC(int, getreg, (void *vcpui, int num, uint64_t *retval));
185 DECLARE_VMMOPS_FUNC(int, setreg, (void *vcpui, int num, uint64_t val));
186 DECLARE_VMMOPS_FUNC(int, getdesc, (void *vcpui, int num,
187     struct seg_desc *desc));
188 DECLARE_VMMOPS_FUNC(int, setdesc, (void *vcpui, int num,
189     struct seg_desc *desc));
190 DECLARE_VMMOPS_FUNC(int, getcap, (void *vcpui, int num, int *retval));
191 DECLARE_VMMOPS_FUNC(int, setcap, (void *vcpui, int num, int val));
192 DECLARE_VMMOPS_FUNC(struct vmspace *, vmspace_alloc,
193     (vm_offset_t min, vm_offset_t max));
194 DECLARE_VMMOPS_FUNC(void, vmspace_free, (struct vmspace *vmspace));
195 DECLARE_VMMOPS_FUNC(struct vlapic *, vlapic_init, (void *vcpui));
196 DECLARE_VMMOPS_FUNC(void, vlapic_cleanup, (struct vlapic *vlapic));
197 DECLARE_VMMOPS_FUNC(int, vcpu_snapshot, (void *vcpui,
198     struct vm_snapshot_meta *meta));
199 DECLARE_VMMOPS_FUNC(int, restore_tsc, (void *vcpui, uint64_t now));
200 
201 struct vmm_ops {
202 	vmmops_modinit_t	modinit;	/* module wide initialization */
203 	vmmops_modcleanup_t	modcleanup;
204 	vmmops_modresume_t	modsuspend;
205 	vmmops_modresume_t	modresume;
206 
207 	vmmops_init_t		init;		/* vm-specific initialization */
208 	vmmops_run_t		run;
209 	vmmops_cleanup_t	cleanup;
210 	vmmops_vcpu_init_t	vcpu_init;
211 	vmmops_vcpu_cleanup_t	vcpu_cleanup;
212 	vmmops_getreg_t		getreg;
213 	vmmops_setreg_t		setreg;
214 	vmmops_getdesc_t	getdesc;
215 	vmmops_setdesc_t	setdesc;
216 	vmmops_getcap_t		getcap;
217 	vmmops_setcap_t		setcap;
218 	vmmops_vmspace_alloc_t	vmspace_alloc;
219 	vmmops_vmspace_free_t	vmspace_free;
220 	vmmops_vlapic_init_t	vlapic_init;
221 	vmmops_vlapic_cleanup_t	vlapic_cleanup;
222 
223 	/* checkpoint operations */
224 	vmmops_vcpu_snapshot_t	vcpu_snapshot;
225 	vmmops_restore_tsc_t	restore_tsc;
226 };
227 
228 extern const struct vmm_ops vmm_ops_intel;
229 extern const struct vmm_ops vmm_ops_amd;
230 
231 int vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
232 int vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len);
233 int vm_assign_pptdev(struct vm *vm, int bus, int slot, int func);
234 int vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func);
235 
236 int vm_get_register(struct vcpu *vcpu, int reg, uint64_t *retval);
237 int vm_set_register(struct vcpu *vcpu, int reg, uint64_t val);
238 int vm_get_seg_desc(struct vcpu *vcpu, int reg,
239 		    struct seg_desc *ret_desc);
240 int vm_set_seg_desc(struct vcpu *vcpu, int reg,
241 		    struct seg_desc *desc);
242 int vm_run(struct vcpu *vcpu);
243 int vm_inject_nmi(struct vcpu *vcpu);
244 int vm_nmi_pending(struct vcpu *vcpu);
245 void vm_nmi_clear(struct vcpu *vcpu);
246 int vm_inject_extint(struct vcpu *vcpu);
247 int vm_extint_pending(struct vcpu *vcpu);
248 void vm_extint_clear(struct vcpu *vcpu);
249 struct vlapic *vm_lapic(struct vcpu *vcpu);
250 struct vioapic *vm_ioapic(struct vm *vm);
251 struct vhpet *vm_hpet(struct vm *vm);
252 int vm_get_capability(struct vcpu *vcpu, int type, int *val);
253 int vm_set_capability(struct vcpu *vcpu, int type, int val);
254 int vm_get_x2apic_state(struct vcpu *vcpu, enum x2apic_state *state);
255 int vm_set_x2apic_state(struct vcpu *vcpu, enum x2apic_state state);
256 int vm_apicid2vcpuid(struct vm *vm, int apicid);
257 int vm_restart_instruction(struct vcpu *vcpu);
258 struct vm_exit *vm_exitinfo(struct vcpu *vcpu);
259 cpuset_t *vm_exitinfo_cpuset(struct vcpu *vcpu);
260 void vm_exit_suspended(struct vcpu *vcpu, uint64_t rip);
261 void vm_exit_debug(struct vcpu *vcpu, uint64_t rip);
262 void vm_exit_rendezvous(struct vcpu *vcpu, uint64_t rip);
263 void vm_exit_astpending(struct vcpu *vcpu, uint64_t rip);
264 void vm_exit_reqidle(struct vcpu *vcpu, uint64_t rip);
265 int vm_snapshot_req(struct vm *vm, struct vm_snapshot_meta *meta);
266 int vm_restore_time(struct vm *vm);
267 
268 #ifdef _SYS__CPUSET_H_
269 cpuset_t vm_start_cpus(struct vm *vm, const cpuset_t *tostart);
270 void vm_await_start(struct vm *vm, const cpuset_t *waiting);
271 #endif	/* _SYS__CPUSET_H_ */
272 
273 /*
274  * Return true if device indicated by bus/slot/func is supposed to be a
275  * pci passthrough device.
276  *
277  * Return false otherwise.
278  */
279 bool vmm_is_pptdev(int bus, int slot, int func);
280 
281 void *vm_iommu_domain(struct vm *vm);
282 
283 void vcpu_notify_lapic(struct vcpu *vcpu);
284 struct vatpic *vm_atpic(struct vm *vm);
285 struct vatpit *vm_atpit(struct vm *vm);
286 struct vpmtmr *vm_pmtmr(struct vm *vm);
287 struct vrtc *vm_rtc(struct vm *vm);
288 
289 /*
290  * Inject exception 'vector' into the guest vcpu. This function returns 0 on
291  * success and non-zero on failure.
292  *
293  * Wrapper functions like 'vm_inject_gp()' should be preferred to calling
294  * this function directly because they enforce the trap-like or fault-like
295  * behavior of an exception.
296  *
297  * This function should only be called in the context of the thread that is
298  * executing this vcpu.
299  */
300 int vm_inject_exception(struct vcpu *vcpu, int vector, int err_valid,
301     uint32_t errcode, int restart_instruction);
302 
303 /*
304  * This function is called after a VM-exit that occurred during exception or
305  * interrupt delivery through the IDT. The format of 'intinfo' is described
306  * in Figure 15-1, "EXITINTINFO for All Intercepts", APM, Vol 2.
307  *
308  * If a VM-exit handler completes the event delivery successfully then it
309  * should call vm_exit_intinfo() to extinguish the pending event. For e.g.,
310  * if the task switch emulation is triggered via a task gate then it should
311  * call this function with 'intinfo=0' to indicate that the external event
312  * is not pending anymore.
313  *
314  * Return value is 0 on success and non-zero on failure.
315  */
316 int vm_exit_intinfo(struct vcpu *vcpu, uint64_t intinfo);
317 
318 /*
319  * This function is called before every VM-entry to retrieve a pending
320  * event that should be injected into the guest. This function combines
321  * nested events into a double or triple fault.
322  *
323  * Returns 0 if there are no events that need to be injected into the guest
324  * and non-zero otherwise.
325  */
326 int vm_entry_intinfo(struct vcpu *vcpu, uint64_t *info);
327 
328 int vm_get_intinfo(struct vcpu *vcpu, uint64_t *info1, uint64_t *info2);
329 
330 /*
331  * Function used to keep track of the guest's TSC offset. The
332  * offset is used by the virtualization extensions to provide a consistent
333  * value for the Time Stamp Counter to the guest.
334  */
335 void vm_set_tsc_offset(struct vcpu *vcpu, uint64_t offset);
336 
337 enum vm_reg_name vm_segment_name(int seg_encoding);
338 
339 struct vm_copyinfo {
340 	uint64_t	gpa;
341 	size_t		len;
342 	void		*hva;
343 	void		*cookie;
344 };
345 
346 /*
347  * Set up 'copyinfo[]' to copy to/from guest linear address space starting
348  * at 'gla' and 'len' bytes long. The 'prot' should be set to PROT_READ for
349  * a copyin or PROT_WRITE for a copyout.
350  *
351  * retval	is_fault	Interpretation
352  *   0		   0		Success
353  *   0		   1		An exception was injected into the guest
354  * EFAULT	  N/A		Unrecoverable error
355  *
356  * The 'copyinfo[]' can be passed to 'vm_copyin()' or 'vm_copyout()' only if
357  * the return value is 0. The 'copyinfo[]' resources should be freed by calling
358  * 'vm_copy_teardown()' after the copy is done.
359  */
360 int vm_copy_setup(struct vcpu *vcpu, struct vm_guest_paging *paging,
361     uint64_t gla, size_t len, int prot, struct vm_copyinfo *copyinfo,
362     int num_copyinfo, int *is_fault);
363 void vm_copy_teardown(struct vm_copyinfo *copyinfo, int num_copyinfo);
364 void vm_copyin(struct vm_copyinfo *copyinfo, void *kaddr, size_t len);
365 void vm_copyout(const void *kaddr, struct vm_copyinfo *copyinfo, size_t len);
366 
367 int vcpu_trace_exceptions(struct vcpu *vcpu);
368 int vcpu_trap_wbinvd(struct vcpu *vcpu);
369 #endif	/* KERNEL */
370 
371 /*
372  * Identifiers for optional vmm capabilities
373  */
374 enum vm_cap_type {
375 	VM_CAP_HALT_EXIT,
376 	VM_CAP_MTRAP_EXIT,
377 	VM_CAP_PAUSE_EXIT,
378 	VM_CAP_UNRESTRICTED_GUEST,
379 	VM_CAP_ENABLE_INVPCID,
380 	VM_CAP_BPT_EXIT,
381 	VM_CAP_RDPID,
382 	VM_CAP_RDTSCP,
383 	VM_CAP_IPI_EXIT,
384 	VM_CAP_MASK_HWINTR,
385 	VM_CAP_RFLAGS_TF,
386 	VM_CAP_MAX
387 };
388 
389 enum vm_intr_trigger {
390 	EDGE_TRIGGER,
391 	LEVEL_TRIGGER
392 };
393 
394 /*
395  * The 'access' field has the format specified in Table 21-2 of the Intel
396  * Architecture Manual vol 3b.
397  *
398  * XXX The contents of the 'access' field are architecturally defined except
399  * bit 16 - Segment Unusable.
400  */
401 struct seg_desc {
402 	uint64_t	base;
403 	uint32_t	limit;
404 	uint32_t	access;
405 };
406 #define	SEG_DESC_TYPE(access)		((access) & 0x001f)
407 #define	SEG_DESC_DPL(access)		(((access) >> 5) & 0x3)
408 #define	SEG_DESC_PRESENT(access)	(((access) & 0x0080) ? 1 : 0)
409 #define	SEG_DESC_DEF32(access)		(((access) & 0x4000) ? 1 : 0)
410 #define	SEG_DESC_GRANULARITY(access)	(((access) & 0x8000) ? 1 : 0)
411 #define	SEG_DESC_UNUSABLE(access)	(((access) & 0x10000) ? 1 : 0)
412 
413 enum vm_cpu_mode {
414 	CPU_MODE_REAL,
415 	CPU_MODE_PROTECTED,
416 	CPU_MODE_COMPATIBILITY,		/* IA-32E mode (CS.L = 0) */
417 	CPU_MODE_64BIT,			/* IA-32E mode (CS.L = 1) */
418 };
419 
420 enum vm_paging_mode {
421 	PAGING_MODE_FLAT,
422 	PAGING_MODE_32,
423 	PAGING_MODE_PAE,
424 	PAGING_MODE_64,
425 	PAGING_MODE_64_LA57,
426 };
427 
428 struct vm_guest_paging {
429 	uint64_t	cr3;
430 	int		cpl;
431 	enum vm_cpu_mode cpu_mode;
432 	enum vm_paging_mode paging_mode;
433 };
434 
435 /*
436  * The data structures 'vie' and 'vie_op' are meant to be opaque to the
437  * consumers of instruction decoding. The only reason why their contents
438  * need to be exposed is because they are part of the 'vm_exit' structure.
439  */
440 struct vie_op {
441 	uint8_t		op_byte;	/* actual opcode byte */
442 	uint8_t		op_type;	/* type of operation (e.g. MOV) */
443 	uint16_t	op_flags;
444 };
445 _Static_assert(sizeof(struct vie_op) == 4, "ABI");
446 _Static_assert(_Alignof(struct vie_op) == 2, "ABI");
447 
448 #define	VIE_INST_SIZE	15
449 struct vie {
450 	uint8_t		inst[VIE_INST_SIZE];	/* instruction bytes */
451 	uint8_t		num_valid;		/* size of the instruction */
452 
453 /* The following fields are all zeroed upon restart. */
454 #define	vie_startzero	num_processed
455 	uint8_t		num_processed;
456 
457 	uint8_t		addrsize:4, opsize:4;	/* address and operand sizes */
458 	uint8_t		rex_w:1,		/* REX prefix */
459 			rex_r:1,
460 			rex_x:1,
461 			rex_b:1,
462 			rex_present:1,
463 			repz_present:1,		/* REP/REPE/REPZ prefix */
464 			repnz_present:1,	/* REPNE/REPNZ prefix */
465 			opsize_override:1,	/* Operand size override */
466 			addrsize_override:1,	/* Address size override */
467 			segment_override:1;	/* Segment override */
468 
469 	uint8_t		mod:2,			/* ModRM byte */
470 			reg:4,
471 			rm:4;
472 
473 	uint8_t		ss:2,			/* SIB byte */
474 			vex_present:1,		/* VEX prefixed */
475 			vex_l:1,		/* L bit */
476 			index:4,		/* SIB byte */
477 			base:4;			/* SIB byte */
478 
479 	uint8_t		disp_bytes;
480 	uint8_t		imm_bytes;
481 
482 	uint8_t		scale;
483 
484 	uint8_t		vex_reg:4,		/* vvvv: first source register specifier */
485 			vex_pp:2,		/* pp */
486 			_sparebits:2;
487 
488 	uint8_t		_sparebytes[2];
489 
490 	int		base_register;		/* VM_REG_GUEST_xyz */
491 	int		index_register;		/* VM_REG_GUEST_xyz */
492 	int		segment_register;	/* VM_REG_GUEST_xyz */
493 
494 	int64_t		displacement;		/* optional addr displacement */
495 	int64_t		immediate;		/* optional immediate operand */
496 
497 	uint8_t		decoded;	/* set to 1 if successfully decoded */
498 
499 	uint8_t		_sparebyte;
500 
501 	struct vie_op	op;			/* opcode description */
502 };
503 _Static_assert(sizeof(struct vie) == 64, "ABI");
504 _Static_assert(__offsetof(struct vie, disp_bytes) == 22, "ABI");
505 _Static_assert(__offsetof(struct vie, scale) == 24, "ABI");
506 _Static_assert(__offsetof(struct vie, base_register) == 28, "ABI");
507 
508 enum vm_exitcode {
509 	VM_EXITCODE_INOUT,
510 	VM_EXITCODE_VMX,
511 	VM_EXITCODE_BOGUS,
512 	VM_EXITCODE_RDMSR,
513 	VM_EXITCODE_WRMSR,
514 	VM_EXITCODE_HLT,
515 	VM_EXITCODE_MTRAP,
516 	VM_EXITCODE_PAUSE,
517 	VM_EXITCODE_PAGING,
518 	VM_EXITCODE_INST_EMUL,
519 	VM_EXITCODE_SPINUP_AP,
520 	VM_EXITCODE_DEPRECATED1,	/* used to be SPINDOWN_CPU */
521 	VM_EXITCODE_RENDEZVOUS,
522 	VM_EXITCODE_IOAPIC_EOI,
523 	VM_EXITCODE_SUSPENDED,
524 	VM_EXITCODE_INOUT_STR,
525 	VM_EXITCODE_TASK_SWITCH,
526 	VM_EXITCODE_MONITOR,
527 	VM_EXITCODE_MWAIT,
528 	VM_EXITCODE_SVM,
529 	VM_EXITCODE_REQIDLE,
530 	VM_EXITCODE_DEBUG,
531 	VM_EXITCODE_VMINSN,
532 	VM_EXITCODE_BPT,
533 	VM_EXITCODE_IPI,
534 	VM_EXITCODE_DB,
535 	VM_EXITCODE_MAX
536 };
537 
538 struct vm_inout {
539 	uint16_t	bytes:3;	/* 1 or 2 or 4 */
540 	uint16_t	in:1;
541 	uint16_t	string:1;
542 	uint16_t	rep:1;
543 	uint16_t	port;
544 	uint32_t	eax;		/* valid for out */
545 };
546 
547 struct vm_inout_str {
548 	struct vm_inout	inout;		/* must be the first element */
549 	struct vm_guest_paging paging;
550 	uint64_t	rflags;
551 	uint64_t	cr0;
552 	uint64_t	index;
553 	uint64_t	count;		/* rep=1 (%rcx), rep=0 (1) */
554 	int		addrsize;
555 	enum vm_reg_name seg_name;
556 	struct seg_desc seg_desc;
557 	int		cs_d;
558 	uint64_t	cs_base;
559 };
560 
561 enum task_switch_reason {
562 	TSR_CALL,
563 	TSR_IRET,
564 	TSR_JMP,
565 	TSR_IDT_GATE,	/* task gate in IDT */
566 };
567 
568 struct vm_task_switch {
569 	uint16_t	tsssel;		/* new TSS selector */
570 	int		ext;		/* task switch due to external event */
571 	uint32_t	errcode;
572 	int		errcode_valid;	/* push 'errcode' on the new stack */
573 	enum task_switch_reason reason;
574 	struct vm_guest_paging paging;
575 };
576 
577 struct vm_exit {
578 	enum vm_exitcode	exitcode;
579 	int			inst_length;	/* 0 means unknown */
580 	uint64_t		rip;
581 	union {
582 		struct vm_inout	inout;
583 		struct vm_inout_str inout_str;
584 		struct {
585 			uint64_t	gpa;
586 			int		fault_type;
587 		} paging;
588 		struct {
589 			uint64_t	gpa;
590 			uint64_t	gla;
591 			uint64_t	cs_base;
592 			int		cs_d;		/* CS.D */
593 			struct vm_guest_paging paging;
594 			struct vie	vie;
595 		} inst_emul;
596 		/*
597 		 * VMX specific payload. Used when there is no "better"
598 		 * exitcode to represent the VM-exit.
599 		 */
600 		struct {
601 			int		status;		/* vmx inst status */
602 			/*
603 			 * 'exit_reason' and 'exit_qualification' are valid
604 			 * only if 'status' is zero.
605 			 */
606 			uint32_t	exit_reason;
607 			uint64_t	exit_qualification;
608 			/*
609 			 * 'inst_error' and 'inst_type' are valid
610 			 * only if 'status' is non-zero.
611 			 */
612 			int		inst_type;
613 			int		inst_error;
614 		} vmx;
615 		/*
616 		 * SVM specific payload.
617 		 */
618 		struct {
619 			uint64_t	exitcode;
620 			uint64_t	exitinfo1;
621 			uint64_t	exitinfo2;
622 		} svm;
623 		struct {
624 			int		inst_length;
625 		} bpt;
626 		struct {
627 			int		trace_trap;
628 			int		pushf_intercept;
629 			int		tf_shadow_val;
630 			struct		vm_guest_paging paging;
631 		} dbg;
632 		struct {
633 			uint32_t	code;		/* ecx value */
634 			uint64_t	wval;
635 		} msr;
636 		struct {
637 			int		vcpu;
638 			uint64_t	rip;
639 		} spinup_ap;
640 		struct {
641 			uint64_t	rflags;
642 			uint64_t	intr_status;
643 		} hlt;
644 		struct {
645 			int		vector;
646 		} ioapic_eoi;
647 		struct {
648 			enum vm_suspend_how how;
649 		} suspended;
650 		struct {
651 			/*
652 			 * The destination vCPU mask is saved in vcpu->cpuset
653 			 * and is copied out to userspace separately to avoid
654 			 * ABI concerns.
655 			 */
656 			uint32_t mode;
657 			uint8_t vector;
658 		} ipi;
659 		struct vm_task_switch task_switch;
660 	} u;
661 };
662 
663 /* APIs to inject faults into the guest */
664 void vm_inject_fault(struct vcpu *vcpu, int vector, int errcode_valid,
665     int errcode);
666 
667 static __inline void
vm_inject_ud(struct vcpu * vcpu)668 vm_inject_ud(struct vcpu *vcpu)
669 {
670 	vm_inject_fault(vcpu, IDT_UD, 0, 0);
671 }
672 
673 static __inline void
vm_inject_gp(struct vcpu * vcpu)674 vm_inject_gp(struct vcpu *vcpu)
675 {
676 	vm_inject_fault(vcpu, IDT_GP, 1, 0);
677 }
678 
679 static __inline void
vm_inject_ac(struct vcpu * vcpu,int errcode)680 vm_inject_ac(struct vcpu *vcpu, int errcode)
681 {
682 	vm_inject_fault(vcpu, IDT_AC, 1, errcode);
683 }
684 
685 static __inline void
vm_inject_ss(struct vcpu * vcpu,int errcode)686 vm_inject_ss(struct vcpu *vcpu, int errcode)
687 {
688 	vm_inject_fault(vcpu, IDT_SS, 1, errcode);
689 }
690 
691 void vm_inject_pf(struct vcpu *vcpu, int error_code, uint64_t cr2);
692 
693 #endif	/* _VMM_H_ */
694