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