xref: /freebsd/sys/amd64/include/vmm.h (revision d06955f9bdb1416d9196043ed781f9b36dae9adc)
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 #ifndef _VMM_H_
32 #define	_VMM_H_
33 
34 #include <x86/segments.h>
35 
36 enum vm_suspend_how {
37 	VM_SUSPEND_NONE,
38 	VM_SUSPEND_RESET,
39 	VM_SUSPEND_POWEROFF,
40 	VM_SUSPEND_HALT,
41 	VM_SUSPEND_TRIPLEFAULT,
42 	VM_SUSPEND_LAST
43 };
44 
45 /*
46  * Identifiers for architecturally defined registers.
47  */
48 enum vm_reg_name {
49 	VM_REG_GUEST_RAX,
50 	VM_REG_GUEST_RBX,
51 	VM_REG_GUEST_RCX,
52 	VM_REG_GUEST_RDX,
53 	VM_REG_GUEST_RSI,
54 	VM_REG_GUEST_RDI,
55 	VM_REG_GUEST_RBP,
56 	VM_REG_GUEST_R8,
57 	VM_REG_GUEST_R9,
58 	VM_REG_GUEST_R10,
59 	VM_REG_GUEST_R11,
60 	VM_REG_GUEST_R12,
61 	VM_REG_GUEST_R13,
62 	VM_REG_GUEST_R14,
63 	VM_REG_GUEST_R15,
64 	VM_REG_GUEST_CR0,
65 	VM_REG_GUEST_CR3,
66 	VM_REG_GUEST_CR4,
67 	VM_REG_GUEST_DR7,
68 	VM_REG_GUEST_RSP,
69 	VM_REG_GUEST_RIP,
70 	VM_REG_GUEST_RFLAGS,
71 	VM_REG_GUEST_ES,
72 	VM_REG_GUEST_CS,
73 	VM_REG_GUEST_SS,
74 	VM_REG_GUEST_DS,
75 	VM_REG_GUEST_FS,
76 	VM_REG_GUEST_GS,
77 	VM_REG_GUEST_LDTR,
78 	VM_REG_GUEST_TR,
79 	VM_REG_GUEST_IDTR,
80 	VM_REG_GUEST_GDTR,
81 	VM_REG_GUEST_EFER,
82 	VM_REG_GUEST_CR2,
83 	VM_REG_GUEST_PDPTE0,
84 	VM_REG_GUEST_PDPTE1,
85 	VM_REG_GUEST_PDPTE2,
86 	VM_REG_GUEST_PDPTE3,
87 	VM_REG_GUEST_INTR_SHADOW,
88 	VM_REG_LAST
89 };
90 
91 enum x2apic_state {
92 	X2APIC_DISABLED,
93 	X2APIC_ENABLED,
94 	X2APIC_STATE_LAST
95 };
96 
97 #define	VM_INTINFO_VECTOR(info)	((info) & 0xff)
98 #define	VM_INTINFO_DEL_ERRCODE	0x800
99 #define	VM_INTINFO_RSVD		0x7ffff000
100 #define	VM_INTINFO_VALID	0x80000000
101 #define	VM_INTINFO_TYPE		0x700
102 #define	VM_INTINFO_HWINTR	(0 << 8)
103 #define	VM_INTINFO_NMI		(2 << 8)
104 #define	VM_INTINFO_HWEXCEPTION	(3 << 8)
105 #define	VM_INTINFO_SWINTR	(4 << 8)
106 
107 #ifdef _KERNEL
108 
109 #define	VM_MAX_NAMELEN	32
110 
111 struct vm;
112 struct vm_exception;
113 struct seg_desc;
114 struct vm_exit;
115 struct vm_run;
116 struct vhpet;
117 struct vioapic;
118 struct vlapic;
119 struct vmspace;
120 struct vm_object;
121 struct vm_guest_paging;
122 struct pmap;
123 
124 struct vm_eventinfo {
125 	void	*rptr;		/* rendezvous cookie */
126 	int	*sptr;		/* suspend cookie */
127 	int	*iptr;		/* reqidle cookie */
128 };
129 
130 typedef int	(*vmm_init_func_t)(int ipinum);
131 typedef int	(*vmm_cleanup_func_t)(void);
132 typedef void	(*vmm_resume_func_t)(void);
133 typedef void *	(*vmi_init_func_t)(struct vm *vm, struct pmap *pmap);
134 typedef int	(*vmi_run_func_t)(void *vmi, int vcpu, register_t rip,
135 		    struct pmap *pmap, struct vm_eventinfo *info);
136 typedef void	(*vmi_cleanup_func_t)(void *vmi);
137 typedef int	(*vmi_get_register_t)(void *vmi, int vcpu, int num,
138 				      uint64_t *retval);
139 typedef int	(*vmi_set_register_t)(void *vmi, int vcpu, int num,
140 				      uint64_t val);
141 typedef int	(*vmi_get_desc_t)(void *vmi, int vcpu, int num,
142 				  struct seg_desc *desc);
143 typedef int	(*vmi_set_desc_t)(void *vmi, int vcpu, int num,
144 				  struct seg_desc *desc);
145 typedef int	(*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval);
146 typedef int	(*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val);
147 typedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max);
148 typedef void	(*vmi_vmspace_free)(struct vmspace *vmspace);
149 typedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu);
150 typedef void	(*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic);
151 
152 struct vmm_ops {
153 	vmm_init_func_t		init;		/* module wide initialization */
154 	vmm_cleanup_func_t	cleanup;
155 	vmm_resume_func_t	resume;
156 
157 	vmi_init_func_t		vminit;		/* vm-specific initialization */
158 	vmi_run_func_t		vmrun;
159 	vmi_cleanup_func_t	vmcleanup;
160 	vmi_get_register_t	vmgetreg;
161 	vmi_set_register_t	vmsetreg;
162 	vmi_get_desc_t		vmgetdesc;
163 	vmi_set_desc_t		vmsetdesc;
164 	vmi_get_cap_t		vmgetcap;
165 	vmi_set_cap_t		vmsetcap;
166 	vmi_vmspace_alloc	vmspace_alloc;
167 	vmi_vmspace_free	vmspace_free;
168 	vmi_vlapic_init		vlapic_init;
169 	vmi_vlapic_cleanup	vlapic_cleanup;
170 };
171 
172 extern struct vmm_ops vmm_ops_intel;
173 extern struct vmm_ops vmm_ops_amd;
174 
175 int vm_create(const char *name, struct vm **retvm);
176 void vm_destroy(struct vm *vm);
177 int vm_reinit(struct vm *vm);
178 const char *vm_name(struct vm *vm);
179 
180 /*
181  * APIs that modify the guest memory map require all vcpus to be frozen.
182  */
183 int vm_mmap_memseg(struct vm *vm, vm_paddr_t gpa, int segid, vm_ooffset_t off,
184     size_t len, int prot, int flags);
185 int vm_alloc_memseg(struct vm *vm, int ident, size_t len, bool sysmem);
186 void vm_free_memseg(struct vm *vm, int ident);
187 int vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
188 int vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len);
189 int vm_assign_pptdev(struct vm *vm, int bus, int slot, int func);
190 int vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func);
191 
192 /*
193  * APIs that inspect the guest memory map require only a *single* vcpu to
194  * be frozen. This acts like a read lock on the guest memory map since any
195  * modification requires *all* vcpus to be frozen.
196  */
197 int vm_mmap_getnext(struct vm *vm, vm_paddr_t *gpa, int *segid,
198     vm_ooffset_t *segoff, size_t *len, int *prot, int *flags);
199 int vm_get_memseg(struct vm *vm, int ident, size_t *len, bool *sysmem,
200     struct vm_object **objptr);
201 void *vm_gpa_hold(struct vm *, int vcpuid, vm_paddr_t gpa, size_t len,
202     int prot, void **cookie);
203 void vm_gpa_release(void *cookie);
204 bool vm_mem_allocated(struct vm *vm, int vcpuid, vm_paddr_t gpa);
205 
206 int vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval);
207 int vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val);
208 int vm_get_seg_desc(struct vm *vm, int vcpu, int reg,
209 		    struct seg_desc *ret_desc);
210 int vm_set_seg_desc(struct vm *vm, int vcpu, int reg,
211 		    struct seg_desc *desc);
212 int vm_run(struct vm *vm, struct vm_run *vmrun);
213 int vm_suspend(struct vm *vm, enum vm_suspend_how how);
214 int vm_inject_nmi(struct vm *vm, int vcpu);
215 int vm_nmi_pending(struct vm *vm, int vcpuid);
216 void vm_nmi_clear(struct vm *vm, int vcpuid);
217 int vm_inject_extint(struct vm *vm, int vcpu);
218 int vm_extint_pending(struct vm *vm, int vcpuid);
219 void vm_extint_clear(struct vm *vm, int vcpuid);
220 struct vlapic *vm_lapic(struct vm *vm, int cpu);
221 struct vioapic *vm_ioapic(struct vm *vm);
222 struct vhpet *vm_hpet(struct vm *vm);
223 int vm_get_capability(struct vm *vm, int vcpu, int type, int *val);
224 int vm_set_capability(struct vm *vm, int vcpu, int type, int val);
225 int vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state);
226 int vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state);
227 int vm_apicid2vcpuid(struct vm *vm, int apicid);
228 int vm_activate_cpu(struct vm *vm, int vcpu);
229 struct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid);
230 void vm_exit_suspended(struct vm *vm, int vcpuid, uint64_t rip);
231 void vm_exit_rendezvous(struct vm *vm, int vcpuid, uint64_t rip);
232 void vm_exit_astpending(struct vm *vm, int vcpuid, uint64_t rip);
233 void vm_exit_reqidle(struct vm *vm, int vcpuid, uint64_t rip);
234 
235 #ifdef _SYS__CPUSET_H_
236 /*
237  * Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'.
238  * The rendezvous 'func(arg)' is not allowed to do anything that will
239  * cause the thread to be put to sleep.
240  *
241  * If the rendezvous is being initiated from a vcpu context then the
242  * 'vcpuid' must refer to that vcpu, otherwise it should be set to -1.
243  *
244  * The caller cannot hold any locks when initiating the rendezvous.
245  *
246  * The implementation of this API may cause vcpus other than those specified
247  * by 'dest' to be stalled. The caller should not rely on any vcpus making
248  * forward progress when the rendezvous is in progress.
249  */
250 typedef void (*vm_rendezvous_func_t)(struct vm *vm, int vcpuid, void *arg);
251 void vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest,
252     vm_rendezvous_func_t func, void *arg);
253 cpuset_t vm_active_cpus(struct vm *vm);
254 cpuset_t vm_suspended_cpus(struct vm *vm);
255 #endif	/* _SYS__CPUSET_H_ */
256 
257 static __inline int
258 vcpu_rendezvous_pending(struct vm_eventinfo *info)
259 {
260 
261 	return (*((uintptr_t *)(info->rptr)) != 0);
262 }
263 
264 static __inline int
265 vcpu_suspended(struct vm_eventinfo *info)
266 {
267 
268 	return (*info->sptr);
269 }
270 
271 static __inline int
272 vcpu_reqidle(struct vm_eventinfo *info)
273 {
274 
275 	return (*info->iptr);
276 }
277 
278 /*
279  * Return 1 if device indicated by bus/slot/func is supposed to be a
280  * pci passthrough device.
281  *
282  * Return 0 otherwise.
283  */
284 int vmm_is_pptdev(int bus, int slot, int func);
285 
286 void *vm_iommu_domain(struct vm *vm);
287 
288 enum vcpu_state {
289 	VCPU_IDLE,
290 	VCPU_FROZEN,
291 	VCPU_RUNNING,
292 	VCPU_SLEEPING,
293 };
294 
295 int vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state,
296     bool from_idle);
297 enum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu);
298 
299 static int __inline
300 vcpu_is_running(struct vm *vm, int vcpu, int *hostcpu)
301 {
302 	return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING);
303 }
304 
305 #ifdef _SYS_PROC_H_
306 static int __inline
307 vcpu_should_yield(struct vm *vm, int vcpu)
308 {
309 
310 	if (curthread->td_flags & (TDF_ASTPENDING | TDF_NEEDRESCHED))
311 		return (1);
312 	else if (curthread->td_owepreempt)
313 		return (1);
314 	else
315 		return (0);
316 }
317 #endif
318 
319 void *vcpu_stats(struct vm *vm, int vcpu);
320 void vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr);
321 struct vmspace *vm_get_vmspace(struct vm *vm);
322 struct vatpic *vm_atpic(struct vm *vm);
323 struct vatpit *vm_atpit(struct vm *vm);
324 struct vpmtmr *vm_pmtmr(struct vm *vm);
325 struct vrtc *vm_rtc(struct vm *vm);
326 
327 /*
328  * Inject exception 'vector' into the guest vcpu. This function returns 0 on
329  * success and non-zero on failure.
330  *
331  * Wrapper functions like 'vm_inject_gp()' should be preferred to calling
332  * this function directly because they enforce the trap-like or fault-like
333  * behavior of an exception.
334  *
335  * This function should only be called in the context of the thread that is
336  * executing this vcpu.
337  */
338 int vm_inject_exception(struct vm *vm, int vcpuid, int vector, int err_valid,
339     uint32_t errcode, int restart_instruction);
340 
341 /*
342  * This function is called after a VM-exit that occurred during exception or
343  * interrupt delivery through the IDT. The format of 'intinfo' is described
344  * in Figure 15-1, "EXITINTINFO for All Intercepts", APM, Vol 2.
345  *
346  * If a VM-exit handler completes the event delivery successfully then it
347  * should call vm_exit_intinfo() to extinguish the pending event. For e.g.,
348  * if the task switch emulation is triggered via a task gate then it should
349  * call this function with 'intinfo=0' to indicate that the external event
350  * is not pending anymore.
351  *
352  * Return value is 0 on success and non-zero on failure.
353  */
354 int vm_exit_intinfo(struct vm *vm, int vcpuid, uint64_t intinfo);
355 
356 /*
357  * This function is called before every VM-entry to retrieve a pending
358  * event that should be injected into the guest. This function combines
359  * nested events into a double or triple fault.
360  *
361  * Returns 0 if there are no events that need to be injected into the guest
362  * and non-zero otherwise.
363  */
364 int vm_entry_intinfo(struct vm *vm, int vcpuid, uint64_t *info);
365 
366 int vm_get_intinfo(struct vm *vm, int vcpuid, uint64_t *info1, uint64_t *info2);
367 
368 enum vm_reg_name vm_segment_name(int seg_encoding);
369 
370 struct vm_copyinfo {
371 	uint64_t	gpa;
372 	size_t		len;
373 	void		*hva;
374 	void		*cookie;
375 };
376 
377 /*
378  * Set up 'copyinfo[]' to copy to/from guest linear address space starting
379  * at 'gla' and 'len' bytes long. The 'prot' should be set to PROT_READ for
380  * a copyin or PROT_WRITE for a copyout.
381  *
382  * retval	is_fault	Interpretation
383  *   0		   0		Success
384  *   0		   1		An exception was injected into the guest
385  * EFAULT	  N/A		Unrecoverable error
386  *
387  * The 'copyinfo[]' can be passed to 'vm_copyin()' or 'vm_copyout()' only if
388  * the return value is 0. The 'copyinfo[]' resources should be freed by calling
389  * 'vm_copy_teardown()' after the copy is done.
390  */
391 int vm_copy_setup(struct vm *vm, int vcpuid, struct vm_guest_paging *paging,
392     uint64_t gla, size_t len, int prot, struct vm_copyinfo *copyinfo,
393     int num_copyinfo, int *is_fault);
394 void vm_copy_teardown(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo,
395     int num_copyinfo);
396 void vm_copyin(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo,
397     void *kaddr, size_t len);
398 void vm_copyout(struct vm *vm, int vcpuid, const void *kaddr,
399     struct vm_copyinfo *copyinfo, size_t len);
400 
401 int vcpu_trace_exceptions(struct vm *vm, int vcpuid);
402 #endif	/* KERNEL */
403 
404 #define	VM_MAXCPU	16			/* maximum virtual cpus */
405 
406 /*
407  * Identifiers for optional vmm capabilities
408  */
409 enum vm_cap_type {
410 	VM_CAP_HALT_EXIT,
411 	VM_CAP_MTRAP_EXIT,
412 	VM_CAP_PAUSE_EXIT,
413 	VM_CAP_UNRESTRICTED_GUEST,
414 	VM_CAP_ENABLE_INVPCID,
415 	VM_CAP_MAX
416 };
417 
418 enum vm_intr_trigger {
419 	EDGE_TRIGGER,
420 	LEVEL_TRIGGER
421 };
422 
423 /*
424  * The 'access' field has the format specified in Table 21-2 of the Intel
425  * Architecture Manual vol 3b.
426  *
427  * XXX The contents of the 'access' field are architecturally defined except
428  * bit 16 - Segment Unusable.
429  */
430 struct seg_desc {
431 	uint64_t	base;
432 	uint32_t	limit;
433 	uint32_t	access;
434 };
435 #define	SEG_DESC_TYPE(access)		((access) & 0x001f)
436 #define	SEG_DESC_DPL(access)		(((access) >> 5) & 0x3)
437 #define	SEG_DESC_PRESENT(access)	(((access) & 0x0080) ? 1 : 0)
438 #define	SEG_DESC_DEF32(access)		(((access) & 0x4000) ? 1 : 0)
439 #define	SEG_DESC_GRANULARITY(access)	(((access) & 0x8000) ? 1 : 0)
440 #define	SEG_DESC_UNUSABLE(access)	(((access) & 0x10000) ? 1 : 0)
441 
442 enum vm_cpu_mode {
443 	CPU_MODE_REAL,
444 	CPU_MODE_PROTECTED,
445 	CPU_MODE_COMPATIBILITY,		/* IA-32E mode (CS.L = 0) */
446 	CPU_MODE_64BIT,			/* IA-32E mode (CS.L = 1) */
447 };
448 
449 enum vm_paging_mode {
450 	PAGING_MODE_FLAT,
451 	PAGING_MODE_32,
452 	PAGING_MODE_PAE,
453 	PAGING_MODE_64,
454 };
455 
456 struct vm_guest_paging {
457 	uint64_t	cr3;
458 	int		cpl;
459 	enum vm_cpu_mode cpu_mode;
460 	enum vm_paging_mode paging_mode;
461 };
462 
463 /*
464  * The data structures 'vie' and 'vie_op' are meant to be opaque to the
465  * consumers of instruction decoding. The only reason why their contents
466  * need to be exposed is because they are part of the 'vm_exit' structure.
467  */
468 struct vie_op {
469 	uint8_t		op_byte;	/* actual opcode byte */
470 	uint8_t		op_type;	/* type of operation (e.g. MOV) */
471 	uint16_t	op_flags;
472 };
473 
474 #define	VIE_INST_SIZE	15
475 struct vie {
476 	uint8_t		inst[VIE_INST_SIZE];	/* instruction bytes */
477 	uint8_t		num_valid;		/* size of the instruction */
478 	uint8_t		num_processed;
479 
480 	uint8_t		addrsize:4, opsize:4;	/* address and operand sizes */
481 	uint8_t		rex_w:1,		/* REX prefix */
482 			rex_r:1,
483 			rex_x:1,
484 			rex_b:1,
485 			rex_present:1,
486 			repz_present:1,		/* REP/REPE/REPZ prefix */
487 			repnz_present:1,	/* REPNE/REPNZ prefix */
488 			opsize_override:1,	/* Operand size override */
489 			addrsize_override:1,	/* Address size override */
490 			segment_override:1;	/* Segment override */
491 
492 	uint8_t		mod:2,			/* ModRM byte */
493 			reg:4,
494 			rm:4;
495 
496 	uint8_t		ss:2,			/* SIB byte */
497 			index:4,
498 			base:4;
499 
500 	uint8_t		disp_bytes;
501 	uint8_t		imm_bytes;
502 
503 	uint8_t		scale;
504 	int		base_register;		/* VM_REG_GUEST_xyz */
505 	int		index_register;		/* VM_REG_GUEST_xyz */
506 	int		segment_register;	/* VM_REG_GUEST_xyz */
507 
508 	int64_t		displacement;		/* optional addr displacement */
509 	int64_t		immediate;		/* optional immediate operand */
510 
511 	uint8_t		decoded;	/* set to 1 if successfully decoded */
512 
513 	struct vie_op	op;			/* opcode description */
514 };
515 
516 enum vm_exitcode {
517 	VM_EXITCODE_INOUT,
518 	VM_EXITCODE_VMX,
519 	VM_EXITCODE_BOGUS,
520 	VM_EXITCODE_RDMSR,
521 	VM_EXITCODE_WRMSR,
522 	VM_EXITCODE_HLT,
523 	VM_EXITCODE_MTRAP,
524 	VM_EXITCODE_PAUSE,
525 	VM_EXITCODE_PAGING,
526 	VM_EXITCODE_INST_EMUL,
527 	VM_EXITCODE_SPINUP_AP,
528 	VM_EXITCODE_DEPRECATED1,	/* used to be SPINDOWN_CPU */
529 	VM_EXITCODE_RENDEZVOUS,
530 	VM_EXITCODE_IOAPIC_EOI,
531 	VM_EXITCODE_SUSPENDED,
532 	VM_EXITCODE_INOUT_STR,
533 	VM_EXITCODE_TASK_SWITCH,
534 	VM_EXITCODE_MONITOR,
535 	VM_EXITCODE_MWAIT,
536 	VM_EXITCODE_SVM,
537 	VM_EXITCODE_REQIDLE,
538 	VM_EXITCODE_MAX
539 };
540 
541 struct vm_inout {
542 	uint16_t	bytes:3;	/* 1 or 2 or 4 */
543 	uint16_t	in:1;
544 	uint16_t	string:1;
545 	uint16_t	rep:1;
546 	uint16_t	port;
547 	uint32_t	eax;		/* valid for out */
548 };
549 
550 struct vm_inout_str {
551 	struct vm_inout	inout;		/* must be the first element */
552 	struct vm_guest_paging paging;
553 	uint64_t	rflags;
554 	uint64_t	cr0;
555 	uint64_t	index;
556 	uint64_t	count;		/* rep=1 (%rcx), rep=0 (1) */
557 	int		addrsize;
558 	enum vm_reg_name seg_name;
559 	struct seg_desc seg_desc;
560 };
561 
562 enum task_switch_reason {
563 	TSR_CALL,
564 	TSR_IRET,
565 	TSR_JMP,
566 	TSR_IDT_GATE,	/* task gate in IDT */
567 };
568 
569 struct vm_task_switch {
570 	uint16_t	tsssel;		/* new TSS selector */
571 	int		ext;		/* task switch due to external event */
572 	uint32_t	errcode;
573 	int		errcode_valid;	/* push 'errcode' on the new stack */
574 	enum task_switch_reason reason;
575 	struct vm_guest_paging paging;
576 };
577 
578 struct vm_exit {
579 	enum vm_exitcode	exitcode;
580 	int			inst_length;	/* 0 means unknown */
581 	uint64_t		rip;
582 	union {
583 		struct vm_inout	inout;
584 		struct vm_inout_str inout_str;
585 		struct {
586 			uint64_t	gpa;
587 			int		fault_type;
588 		} paging;
589 		struct {
590 			uint64_t	gpa;
591 			uint64_t	gla;
592 			uint64_t	cs_base;
593 			int		cs_d;		/* CS.D */
594 			struct vm_guest_paging paging;
595 			struct vie	vie;
596 		} inst_emul;
597 		/*
598 		 * VMX specific payload. Used when there is no "better"
599 		 * exitcode to represent the VM-exit.
600 		 */
601 		struct {
602 			int		status;		/* vmx inst status */
603 			/*
604 			 * 'exit_reason' and 'exit_qualification' are valid
605 			 * only if 'status' is zero.
606 			 */
607 			uint32_t	exit_reason;
608 			uint64_t	exit_qualification;
609 			/*
610 			 * 'inst_error' and 'inst_type' are valid
611 			 * only if 'status' is non-zero.
612 			 */
613 			int		inst_type;
614 			int		inst_error;
615 		} vmx;
616 		/*
617 		 * SVM specific payload.
618 		 */
619 		struct {
620 			uint64_t	exitcode;
621 			uint64_t	exitinfo1;
622 			uint64_t	exitinfo2;
623 		} svm;
624 		struct {
625 			uint32_t	code;		/* ecx value */
626 			uint64_t	wval;
627 		} msr;
628 		struct {
629 			int		vcpu;
630 			uint64_t	rip;
631 		} spinup_ap;
632 		struct {
633 			uint64_t	rflags;
634 		} hlt;
635 		struct {
636 			int		vector;
637 		} ioapic_eoi;
638 		struct {
639 			enum vm_suspend_how how;
640 		} suspended;
641 		struct vm_task_switch task_switch;
642 	} u;
643 };
644 
645 /* APIs to inject faults into the guest */
646 void vm_inject_fault(void *vm, int vcpuid, int vector, int errcode_valid,
647     int errcode);
648 
649 static __inline void
650 vm_inject_ud(void *vm, int vcpuid)
651 {
652 	vm_inject_fault(vm, vcpuid, IDT_UD, 0, 0);
653 }
654 
655 static __inline void
656 vm_inject_gp(void *vm, int vcpuid)
657 {
658 	vm_inject_fault(vm, vcpuid, IDT_GP, 1, 0);
659 }
660 
661 static __inline void
662 vm_inject_ac(void *vm, int vcpuid, int errcode)
663 {
664 	vm_inject_fault(vm, vcpuid, IDT_AC, 1, errcode);
665 }
666 
667 static __inline void
668 vm_inject_ss(void *vm, int vcpuid, int errcode)
669 {
670 	vm_inject_fault(vm, vcpuid, IDT_SS, 1, errcode);
671 }
672 
673 void vm_inject_pf(void *vm, int vcpuid, int error_code, uint64_t cr2);
674 
675 int vm_restart_instruction(void *vm, int vcpuid);
676 
677 #endif	/* _VMM_H_ */
678