vmm.h revision 347435
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 347435 2019-05-10 16:32:44Z jhb $
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);
187
188/*
189 * APIs that modify the guest memory map require all vcpus to be frozen.
190 */
191int vm_mmap_memseg(struct vm *vm, vm_paddr_t gpa, int segid, vm_ooffset_t off,
192    size_t len, int prot, int flags);
193int vm_alloc_memseg(struct vm *vm, int ident, size_t len, bool sysmem);
194void vm_free_memseg(struct vm *vm, int ident);
195int vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
196int vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len);
197int vm_assign_pptdev(struct vm *vm, int bus, int slot, int func);
198int vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func);
199
200/*
201 * APIs that inspect the guest memory map require only a *single* vcpu to
202 * be frozen. This acts like a read lock on the guest memory map since any
203 * modification requires *all* vcpus to be frozen.
204 */
205int vm_mmap_getnext(struct vm *vm, vm_paddr_t *gpa, int *segid,
206    vm_ooffset_t *segoff, size_t *len, int *prot, int *flags);
207int vm_get_memseg(struct vm *vm, int ident, size_t *len, bool *sysmem,
208    struct vm_object **objptr);
209vm_paddr_t vmm_sysmem_maxaddr(struct vm *vm);
210void *vm_gpa_hold(struct vm *, int vcpuid, vm_paddr_t gpa, size_t len,
211    int prot, void **cookie);
212void vm_gpa_release(void *cookie);
213bool vm_mem_allocated(struct vm *vm, int vcpuid, vm_paddr_t gpa);
214
215int vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval);
216int vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val);
217int vm_get_seg_desc(struct vm *vm, int vcpu, int reg,
218		    struct seg_desc *ret_desc);
219int vm_set_seg_desc(struct vm *vm, int vcpu, int reg,
220		    struct seg_desc *desc);
221int vm_run(struct vm *vm, struct vm_run *vmrun);
222int vm_suspend(struct vm *vm, enum vm_suspend_how how);
223int vm_inject_nmi(struct vm *vm, int vcpu);
224int vm_nmi_pending(struct vm *vm, int vcpuid);
225void vm_nmi_clear(struct vm *vm, int vcpuid);
226int vm_inject_extint(struct vm *vm, int vcpu);
227int vm_extint_pending(struct vm *vm, int vcpuid);
228void vm_extint_clear(struct vm *vm, int vcpuid);
229struct vlapic *vm_lapic(struct vm *vm, int cpu);
230struct vioapic *vm_ioapic(struct vm *vm);
231struct vhpet *vm_hpet(struct vm *vm);
232int vm_get_capability(struct vm *vm, int vcpu, int type, int *val);
233int vm_set_capability(struct vm *vm, int vcpu, int type, int val);
234int vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state);
235int vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state);
236int vm_apicid2vcpuid(struct vm *vm, int apicid);
237int vm_activate_cpu(struct vm *vm, int vcpu);
238struct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid);
239void vm_exit_suspended(struct vm *vm, int vcpuid, uint64_t rip);
240void vm_exit_rendezvous(struct vm *vm, int vcpuid, uint64_t rip);
241void vm_exit_astpending(struct vm *vm, int vcpuid, uint64_t rip);
242void vm_exit_reqidle(struct vm *vm, int vcpuid, uint64_t rip);
243
244#ifdef _SYS__CPUSET_H_
245/*
246 * Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'.
247 * The rendezvous 'func(arg)' is not allowed to do anything that will
248 * cause the thread to be put to sleep.
249 *
250 * If the rendezvous is being initiated from a vcpu context then the
251 * 'vcpuid' must refer to that vcpu, otherwise it should be set to -1.
252 *
253 * The caller cannot hold any locks when initiating the rendezvous.
254 *
255 * The implementation of this API may cause vcpus other than those specified
256 * by 'dest' to be stalled. The caller should not rely on any vcpus making
257 * forward progress when the rendezvous is in progress.
258 */
259typedef void (*vm_rendezvous_func_t)(struct vm *vm, int vcpuid, void *arg);
260void vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest,
261    vm_rendezvous_func_t func, void *arg);
262cpuset_t vm_active_cpus(struct vm *vm);
263cpuset_t vm_suspended_cpus(struct vm *vm);
264#endif	/* _SYS__CPUSET_H_ */
265
266static __inline int
267vcpu_rendezvous_pending(struct vm_eventinfo *info)
268{
269
270	return (*((uintptr_t *)(info->rptr)) != 0);
271}
272
273static __inline int
274vcpu_suspended(struct vm_eventinfo *info)
275{
276
277	return (*info->sptr);
278}
279
280static __inline int
281vcpu_reqidle(struct vm_eventinfo *info)
282{
283
284	return (*info->iptr);
285}
286
287/*
288 * Return 1 if device indicated by bus/slot/func is supposed to be a
289 * pci passthrough device.
290 *
291 * Return 0 otherwise.
292 */
293int vmm_is_pptdev(int bus, int slot, int func);
294
295void *vm_iommu_domain(struct vm *vm);
296
297enum vcpu_state {
298	VCPU_IDLE,
299	VCPU_FROZEN,
300	VCPU_RUNNING,
301	VCPU_SLEEPING,
302};
303
304int vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state,
305    bool from_idle);
306enum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu);
307
308static int __inline
309vcpu_is_running(struct vm *vm, int vcpu, int *hostcpu)
310{
311	return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING);
312}
313
314#ifdef _SYS_PROC_H_
315static int __inline
316vcpu_should_yield(struct vm *vm, int vcpu)
317{
318
319	if (curthread->td_flags & (TDF_ASTPENDING | TDF_NEEDRESCHED))
320		return (1);
321	else if (curthread->td_owepreempt)
322		return (1);
323	else
324		return (0);
325}
326#endif
327
328void *vcpu_stats(struct vm *vm, int vcpu);
329void vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr);
330struct vmspace *vm_get_vmspace(struct vm *vm);
331struct vatpic *vm_atpic(struct vm *vm);
332struct vatpit *vm_atpit(struct vm *vm);
333struct vpmtmr *vm_pmtmr(struct vm *vm);
334struct vrtc *vm_rtc(struct vm *vm);
335
336/*
337 * Inject exception 'vector' into the guest vcpu. This function returns 0 on
338 * success and non-zero on failure.
339 *
340 * Wrapper functions like 'vm_inject_gp()' should be preferred to calling
341 * this function directly because they enforce the trap-like or fault-like
342 * behavior of an exception.
343 *
344 * This function should only be called in the context of the thread that is
345 * executing this vcpu.
346 */
347int vm_inject_exception(struct vm *vm, int vcpuid, int vector, int err_valid,
348    uint32_t errcode, int restart_instruction);
349
350/*
351 * This function is called after a VM-exit that occurred during exception or
352 * interrupt delivery through the IDT. The format of 'intinfo' is described
353 * in Figure 15-1, "EXITINTINFO for All Intercepts", APM, Vol 2.
354 *
355 * If a VM-exit handler completes the event delivery successfully then it
356 * should call vm_exit_intinfo() to extinguish the pending event. For e.g.,
357 * if the task switch emulation is triggered via a task gate then it should
358 * call this function with 'intinfo=0' to indicate that the external event
359 * is not pending anymore.
360 *
361 * Return value is 0 on success and non-zero on failure.
362 */
363int vm_exit_intinfo(struct vm *vm, int vcpuid, uint64_t intinfo);
364
365/*
366 * This function is called before every VM-entry to retrieve a pending
367 * event that should be injected into the guest. This function combines
368 * nested events into a double or triple fault.
369 *
370 * Returns 0 if there are no events that need to be injected into the guest
371 * and non-zero otherwise.
372 */
373int vm_entry_intinfo(struct vm *vm, int vcpuid, uint64_t *info);
374
375int vm_get_intinfo(struct vm *vm, int vcpuid, uint64_t *info1, uint64_t *info2);
376
377enum vm_reg_name vm_segment_name(int seg_encoding);
378
379struct vm_copyinfo {
380	uint64_t	gpa;
381	size_t		len;
382	void		*hva;
383	void		*cookie;
384};
385
386/*
387 * Set up 'copyinfo[]' to copy to/from guest linear address space starting
388 * at 'gla' and 'len' bytes long. The 'prot' should be set to PROT_READ for
389 * a copyin or PROT_WRITE for a copyout.
390 *
391 * retval	is_fault	Interpretation
392 *   0		   0		Success
393 *   0		   1		An exception was injected into the guest
394 * EFAULT	  N/A		Unrecoverable error
395 *
396 * The 'copyinfo[]' can be passed to 'vm_copyin()' or 'vm_copyout()' only if
397 * the return value is 0. The 'copyinfo[]' resources should be freed by calling
398 * 'vm_copy_teardown()' after the copy is done.
399 */
400int vm_copy_setup(struct vm *vm, int vcpuid, struct vm_guest_paging *paging,
401    uint64_t gla, size_t len, int prot, struct vm_copyinfo *copyinfo,
402    int num_copyinfo, int *is_fault);
403void vm_copy_teardown(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo,
404    int num_copyinfo);
405void vm_copyin(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo,
406    void *kaddr, size_t len);
407void vm_copyout(struct vm *vm, int vcpuid, const void *kaddr,
408    struct vm_copyinfo *copyinfo, size_t len);
409
410int vcpu_trace_exceptions(struct vm *vm, int vcpuid);
411#endif	/* KERNEL */
412
413#define	VM_MAXCPU	16			/* maximum virtual cpus */
414
415/*
416 * Identifiers for optional vmm capabilities
417 */
418enum vm_cap_type {
419	VM_CAP_HALT_EXIT,
420	VM_CAP_MTRAP_EXIT,
421	VM_CAP_PAUSE_EXIT,
422	VM_CAP_UNRESTRICTED_GUEST,
423	VM_CAP_ENABLE_INVPCID,
424	VM_CAP_MAX
425};
426
427enum vm_intr_trigger {
428	EDGE_TRIGGER,
429	LEVEL_TRIGGER
430};
431
432/*
433 * The 'access' field has the format specified in Table 21-2 of the Intel
434 * Architecture Manual vol 3b.
435 *
436 * XXX The contents of the 'access' field are architecturally defined except
437 * bit 16 - Segment Unusable.
438 */
439struct seg_desc {
440	uint64_t	base;
441	uint32_t	limit;
442	uint32_t	access;
443};
444#define	SEG_DESC_TYPE(access)		((access) & 0x001f)
445#define	SEG_DESC_DPL(access)		(((access) >> 5) & 0x3)
446#define	SEG_DESC_PRESENT(access)	(((access) & 0x0080) ? 1 : 0)
447#define	SEG_DESC_DEF32(access)		(((access) & 0x4000) ? 1 : 0)
448#define	SEG_DESC_GRANULARITY(access)	(((access) & 0x8000) ? 1 : 0)
449#define	SEG_DESC_UNUSABLE(access)	(((access) & 0x10000) ? 1 : 0)
450
451enum vm_cpu_mode {
452	CPU_MODE_REAL,
453	CPU_MODE_PROTECTED,
454	CPU_MODE_COMPATIBILITY,		/* IA-32E mode (CS.L = 0) */
455	CPU_MODE_64BIT,			/* IA-32E mode (CS.L = 1) */
456};
457
458enum vm_paging_mode {
459	PAGING_MODE_FLAT,
460	PAGING_MODE_32,
461	PAGING_MODE_PAE,
462	PAGING_MODE_64,
463};
464
465struct vm_guest_paging {
466	uint64_t	cr3;
467	int		cpl;
468	enum vm_cpu_mode cpu_mode;
469	enum vm_paging_mode paging_mode;
470};
471
472/*
473 * The data structures 'vie' and 'vie_op' are meant to be opaque to the
474 * consumers of instruction decoding. The only reason why their contents
475 * need to be exposed is because they are part of the 'vm_exit' structure.
476 */
477struct vie_op {
478	uint8_t		op_byte;	/* actual opcode byte */
479	uint8_t		op_type;	/* type of operation (e.g. MOV) */
480	uint16_t	op_flags;
481};
482
483#define	VIE_INST_SIZE	15
484struct vie {
485	uint8_t		inst[VIE_INST_SIZE];	/* instruction bytes */
486	uint8_t		num_valid;		/* size of the instruction */
487	uint8_t		num_processed;
488
489	uint8_t		addrsize:4, opsize:4;	/* address and operand sizes */
490	uint8_t		rex_w:1,		/* REX prefix */
491			rex_r:1,
492			rex_x:1,
493			rex_b:1,
494			rex_present:1,
495			repz_present:1,		/* REP/REPE/REPZ prefix */
496			repnz_present:1,	/* REPNE/REPNZ prefix */
497			opsize_override:1,	/* Operand size override */
498			addrsize_override:1,	/* Address size override */
499			segment_override:1;	/* Segment override */
500
501	uint8_t		mod:2,			/* ModRM byte */
502			reg:4,
503			rm:4;
504
505	uint8_t		ss:2,			/* SIB byte */
506			index:4,
507			base:4;
508
509	uint8_t		disp_bytes;
510	uint8_t		imm_bytes;
511
512	uint8_t		scale;
513	int		base_register;		/* VM_REG_GUEST_xyz */
514	int		index_register;		/* VM_REG_GUEST_xyz */
515	int		segment_register;	/* VM_REG_GUEST_xyz */
516
517	int64_t		displacement;		/* optional addr displacement */
518	int64_t		immediate;		/* optional immediate operand */
519
520	uint8_t		decoded;	/* set to 1 if successfully decoded */
521
522	struct vie_op	op;			/* opcode description */
523};
524
525enum vm_exitcode {
526	VM_EXITCODE_INOUT,
527	VM_EXITCODE_VMX,
528	VM_EXITCODE_BOGUS,
529	VM_EXITCODE_RDMSR,
530	VM_EXITCODE_WRMSR,
531	VM_EXITCODE_HLT,
532	VM_EXITCODE_MTRAP,
533	VM_EXITCODE_PAUSE,
534	VM_EXITCODE_PAGING,
535	VM_EXITCODE_INST_EMUL,
536	VM_EXITCODE_SPINUP_AP,
537	VM_EXITCODE_DEPRECATED1,	/* used to be SPINDOWN_CPU */
538	VM_EXITCODE_RENDEZVOUS,
539	VM_EXITCODE_IOAPIC_EOI,
540	VM_EXITCODE_SUSPENDED,
541	VM_EXITCODE_INOUT_STR,
542	VM_EXITCODE_TASK_SWITCH,
543	VM_EXITCODE_MONITOR,
544	VM_EXITCODE_MWAIT,
545	VM_EXITCODE_SVM,
546	VM_EXITCODE_REQIDLE,
547	VM_EXITCODE_VMINSN,
548	VM_EXITCODE_MAX
549};
550
551struct vm_inout {
552	uint16_t	bytes:3;	/* 1 or 2 or 4 */
553	uint16_t	in:1;
554	uint16_t	string:1;
555	uint16_t	rep:1;
556	uint16_t	port;
557	uint32_t	eax;		/* valid for out */
558};
559
560struct vm_inout_str {
561	struct vm_inout	inout;		/* must be the first element */
562	struct vm_guest_paging paging;
563	uint64_t	rflags;
564	uint64_t	cr0;
565	uint64_t	index;
566	uint64_t	count;		/* rep=1 (%rcx), rep=0 (1) */
567	int		addrsize;
568	enum vm_reg_name seg_name;
569	struct seg_desc seg_desc;
570};
571
572enum task_switch_reason {
573	TSR_CALL,
574	TSR_IRET,
575	TSR_JMP,
576	TSR_IDT_GATE,	/* task gate in IDT */
577};
578
579struct vm_task_switch {
580	uint16_t	tsssel;		/* new TSS selector */
581	int		ext;		/* task switch due to external event */
582	uint32_t	errcode;
583	int		errcode_valid;	/* push 'errcode' on the new stack */
584	enum task_switch_reason reason;
585	struct vm_guest_paging paging;
586};
587
588struct vm_exit {
589	enum vm_exitcode	exitcode;
590	int			inst_length;	/* 0 means unknown */
591	uint64_t		rip;
592	union {
593		struct vm_inout	inout;
594		struct vm_inout_str inout_str;
595		struct {
596			uint64_t	gpa;
597			int		fault_type;
598		} paging;
599		struct {
600			uint64_t	gpa;
601			uint64_t	gla;
602			uint64_t	cs_base;
603			int		cs_d;		/* CS.D */
604			struct vm_guest_paging paging;
605			struct vie	vie;
606		} inst_emul;
607		/*
608		 * VMX specific payload. Used when there is no "better"
609		 * exitcode to represent the VM-exit.
610		 */
611		struct {
612			int		status;		/* vmx inst status */
613			/*
614			 * 'exit_reason' and 'exit_qualification' are valid
615			 * only if 'status' is zero.
616			 */
617			uint32_t	exit_reason;
618			uint64_t	exit_qualification;
619			/*
620			 * 'inst_error' and 'inst_type' are valid
621			 * only if 'status' is non-zero.
622			 */
623			int		inst_type;
624			int		inst_error;
625		} vmx;
626		/*
627		 * SVM specific payload.
628		 */
629		struct {
630			uint64_t	exitcode;
631			uint64_t	exitinfo1;
632			uint64_t	exitinfo2;
633		} svm;
634		struct {
635			uint32_t	code;		/* ecx value */
636			uint64_t	wval;
637		} msr;
638		struct {
639			int		vcpu;
640			uint64_t	rip;
641		} spinup_ap;
642		struct {
643			uint64_t	rflags;
644			uint64_t	intr_status;
645		} hlt;
646		struct {
647			int		vector;
648		} ioapic_eoi;
649		struct {
650			enum vm_suspend_how how;
651		} suspended;
652		struct vm_task_switch task_switch;
653	} u;
654};
655
656/* APIs to inject faults into the guest */
657void vm_inject_fault(void *vm, int vcpuid, int vector, int errcode_valid,
658    int errcode);
659
660static __inline void
661vm_inject_ud(void *vm, int vcpuid)
662{
663	vm_inject_fault(vm, vcpuid, IDT_UD, 0, 0);
664}
665
666static __inline void
667vm_inject_gp(void *vm, int vcpuid)
668{
669	vm_inject_fault(vm, vcpuid, IDT_GP, 1, 0);
670}
671
672static __inline void
673vm_inject_ac(void *vm, int vcpuid, int errcode)
674{
675	vm_inject_fault(vm, vcpuid, IDT_AC, 1, errcode);
676}
677
678static __inline void
679vm_inject_ss(void *vm, int vcpuid, int errcode)
680{
681	vm_inject_fault(vm, vcpuid, IDT_SS, 1, errcode);
682}
683
684void vm_inject_pf(void *vm, int vcpuid, int error_code, uint64_t cr2);
685
686int vm_restart_instruction(void *vm, int vcpuid);
687
688#endif	/* _VMM_H_ */
689