vmm.h revision 256072
1221828Sgrehan/*-
2221828Sgrehan * Copyright (c) 2011 NetApp, Inc.
3221828Sgrehan * All rights reserved.
4221828Sgrehan *
5221828Sgrehan * Redistribution and use in source and binary forms, with or without
6221828Sgrehan * modification, are permitted provided that the following conditions
7221828Sgrehan * are met:
8221828Sgrehan * 1. Redistributions of source code must retain the above copyright
9221828Sgrehan *    notice, this list of conditions and the following disclaimer.
10221828Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
11221828Sgrehan *    notice, this list of conditions and the following disclaimer in the
12221828Sgrehan *    documentation and/or other materials provided with the distribution.
13221828Sgrehan *
14221828Sgrehan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15221828Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16221828Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17221828Sgrehan * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18221828Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19221828Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20221828Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21221828Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22221828Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23221828Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24221828Sgrehan * SUCH DAMAGE.
25221828Sgrehan *
26245678Sneel * $FreeBSD: head/sys/amd64/include/vmm.h 256072 2013-10-05 21:22:35Z neel $
27221828Sgrehan */
28221828Sgrehan
29221828Sgrehan#ifndef _VMM_H_
30221828Sgrehan#define	_VMM_H_
31221828Sgrehan
32221828Sgrehan#ifdef _KERNEL
33221828Sgrehan
34221828Sgrehan#define	VM_MAX_NAMELEN	32
35221828Sgrehan
36221828Sgrehanstruct vm;
37221828Sgrehanstruct vm_memory_segment;
38221828Sgrehanstruct seg_desc;
39221828Sgrehanstruct vm_exit;
40221828Sgrehanstruct vm_run;
41221828Sgrehanstruct vlapic;
42256072Sneelstruct vmspace;
43256072Sneelstruct vm_object;
44256072Sneelstruct pmap;
45221828Sgrehan
46240922Sneelenum x2apic_state;
47240922Sneel
48221828Sgrehantypedef int	(*vmm_init_func_t)(void);
49221828Sgrehantypedef int	(*vmm_cleanup_func_t)(void);
50256072Sneeltypedef void *	(*vmi_init_func_t)(struct vm *vm, struct pmap *pmap);
51256072Sneeltypedef int	(*vmi_run_func_t)(void *vmi, int vcpu, register_t rip,
52256072Sneel				  struct pmap *pmap);
53221828Sgrehantypedef void	(*vmi_cleanup_func_t)(void *vmi);
54221828Sgrehantypedef int	(*vmi_get_register_t)(void *vmi, int vcpu, int num,
55221828Sgrehan				      uint64_t *retval);
56221828Sgrehantypedef int	(*vmi_set_register_t)(void *vmi, int vcpu, int num,
57221828Sgrehan				      uint64_t val);
58221828Sgrehantypedef int	(*vmi_get_desc_t)(void *vmi, int vcpu, int num,
59221828Sgrehan				  struct seg_desc *desc);
60221828Sgrehantypedef int	(*vmi_set_desc_t)(void *vmi, int vcpu, int num,
61221828Sgrehan				  struct seg_desc *desc);
62221828Sgrehantypedef int	(*vmi_inject_event_t)(void *vmi, int vcpu,
63221828Sgrehan				      int type, int vector,
64221828Sgrehan				      uint32_t code, int code_valid);
65221828Sgrehantypedef int	(*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval);
66221828Sgrehantypedef int	(*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val);
67256072Sneeltypedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max);
68256072Sneeltypedef void	(*vmi_vmspace_free)(struct vmspace *vmspace);
69221828Sgrehan
70221828Sgrehanstruct vmm_ops {
71221828Sgrehan	vmm_init_func_t		init;		/* module wide initialization */
72221828Sgrehan	vmm_cleanup_func_t	cleanup;
73221828Sgrehan
74221828Sgrehan	vmi_init_func_t		vminit;		/* vm-specific initialization */
75221828Sgrehan	vmi_run_func_t		vmrun;
76221828Sgrehan	vmi_cleanup_func_t	vmcleanup;
77221828Sgrehan	vmi_get_register_t	vmgetreg;
78221828Sgrehan	vmi_set_register_t	vmsetreg;
79221828Sgrehan	vmi_get_desc_t		vmgetdesc;
80221828Sgrehan	vmi_set_desc_t		vmsetdesc;
81221828Sgrehan	vmi_inject_event_t	vminject;
82221828Sgrehan	vmi_get_cap_t		vmgetcap;
83221828Sgrehan	vmi_set_cap_t		vmsetcap;
84256072Sneel	vmi_vmspace_alloc	vmspace_alloc;
85256072Sneel	vmi_vmspace_free	vmspace_free;
86221828Sgrehan};
87221828Sgrehan
88221828Sgrehanextern struct vmm_ops vmm_ops_intel;
89221828Sgrehanextern struct vmm_ops vmm_ops_amd;
90221828Sgrehan
91249396Sneelint vm_create(const char *name, struct vm **retvm);
92221828Sgrehanvoid vm_destroy(struct vm *vm);
93221828Sgrehanconst char *vm_name(struct vm *vm);
94241041Sneelint vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len);
95221828Sgrehanint vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
96221828Sgrehanint vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len);
97256072Sneelvoid *vm_gpa_hold(struct vm *, vm_paddr_t gpa, size_t len, int prot,
98256072Sneel		  void **cookie);
99256072Sneelvoid vm_gpa_release(void *cookie);
100221828Sgrehanint vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase,
101221828Sgrehan	      struct vm_memory_segment *seg);
102256072Sneelint vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len,
103256072Sneel		  vm_offset_t *offset, struct vm_object **object);
104256072Sneelboolean_t vm_mem_allocated(struct vm *vm, vm_paddr_t gpa);
105221828Sgrehanint vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval);
106221828Sgrehanint vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val);
107221828Sgrehanint vm_get_seg_desc(struct vm *vm, int vcpu, int reg,
108221828Sgrehan		    struct seg_desc *ret_desc);
109221828Sgrehanint vm_set_seg_desc(struct vm *vm, int vcpu, int reg,
110221828Sgrehan		    struct seg_desc *desc);
111221828Sgrehanint vm_run(struct vm *vm, struct vm_run *vmrun);
112221828Sgrehanint vm_inject_event(struct vm *vm, int vcpu, int type,
113221828Sgrehan		    int vector, uint32_t error_code, int error_code_valid);
114221828Sgrehanint vm_inject_nmi(struct vm *vm, int vcpu);
115241982Sneelint vm_nmi_pending(struct vm *vm, int vcpuid);
116241982Sneelvoid vm_nmi_clear(struct vm *vm, int vcpuid);
117221828Sgrehanuint64_t *vm_guest_msrs(struct vm *vm, int cpu);
118221828Sgrehanstruct vlapic *vm_lapic(struct vm *vm, int cpu);
119221828Sgrehanint vm_get_capability(struct vm *vm, int vcpu, int type, int *val);
120221828Sgrehanint vm_set_capability(struct vm *vm, int vcpu, int type, int val);
121240922Sneelint vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state);
122240922Sneelint vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state);
123221828Sgrehanvoid vm_activate_cpu(struct vm *vm, int vcpu);
124223621Sgrehancpuset_t vm_active_cpus(struct vm *vm);
125240894Sneelstruct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid);
126221828Sgrehan
127221828Sgrehan/*
128221828Sgrehan * Return 1 if device indicated by bus/slot/func is supposed to be a
129221828Sgrehan * pci passthrough device.
130221828Sgrehan *
131221828Sgrehan * Return 0 otherwise.
132221828Sgrehan */
133221828Sgrehanint vmm_is_pptdev(int bus, int slot, int func);
134221828Sgrehan
135221828Sgrehanvoid *vm_iommu_domain(struct vm *vm);
136221828Sgrehan
137241489Sneelenum vcpu_state {
138241489Sneel	VCPU_IDLE,
139256072Sneel	VCPU_FROZEN,
140241489Sneel	VCPU_RUNNING,
141256072Sneel	VCPU_SLEEPING,
142241489Sneel};
143221828Sgrehan
144241489Sneelint vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state);
145249879Sgrehanenum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu);
146221828Sgrehan
147221828Sgrehanstatic int __inline
148249879Sgrehanvcpu_is_running(struct vm *vm, int vcpu, int *hostcpu)
149221828Sgrehan{
150249879Sgrehan	return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING);
151221828Sgrehan}
152221828Sgrehan
153241489Sneelvoid *vcpu_stats(struct vm *vm, int vcpu);
154241489Sneelvoid vm_interrupt_hostcpu(struct vm *vm, int vcpu);
155256072Sneelstruct vmspace *vm_get_vmspace(struct vm *vm);
156256072Sneelint vm_assign_pptdev(struct vm *vm, int bus, int slot, int func);
157256072Sneelint vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func);
158221828Sgrehan#endif	/* KERNEL */
159221828Sgrehan
160243640Sneel#include <machine/vmm_instruction_emul.h>
161243640Sneel
162255438Sgrehan#define	VM_MAXCPU	16			/* maximum virtual cpus */
163221828Sgrehan
164221828Sgrehan/*
165221828Sgrehan * Identifiers for events that can be injected into the VM
166221828Sgrehan */
167221828Sgrehanenum vm_event_type {
168221828Sgrehan	VM_EVENT_NONE,
169221828Sgrehan	VM_HW_INTR,
170221828Sgrehan	VM_NMI,
171221828Sgrehan	VM_HW_EXCEPTION,
172221828Sgrehan	VM_SW_INTR,
173221828Sgrehan	VM_PRIV_SW_EXCEPTION,
174221828Sgrehan	VM_SW_EXCEPTION,
175221828Sgrehan	VM_EVENT_MAX
176221828Sgrehan};
177221828Sgrehan
178221828Sgrehan/*
179221828Sgrehan * Identifiers for architecturally defined registers.
180221828Sgrehan */
181221828Sgrehanenum vm_reg_name {
182221828Sgrehan	VM_REG_GUEST_RAX,
183221828Sgrehan	VM_REG_GUEST_RBX,
184221828Sgrehan	VM_REG_GUEST_RCX,
185221828Sgrehan	VM_REG_GUEST_RDX,
186221828Sgrehan	VM_REG_GUEST_RSI,
187221828Sgrehan	VM_REG_GUEST_RDI,
188221828Sgrehan	VM_REG_GUEST_RBP,
189221828Sgrehan	VM_REG_GUEST_R8,
190221828Sgrehan	VM_REG_GUEST_R9,
191221828Sgrehan	VM_REG_GUEST_R10,
192221828Sgrehan	VM_REG_GUEST_R11,
193221828Sgrehan	VM_REG_GUEST_R12,
194221828Sgrehan	VM_REG_GUEST_R13,
195221828Sgrehan	VM_REG_GUEST_R14,
196221828Sgrehan	VM_REG_GUEST_R15,
197221828Sgrehan	VM_REG_GUEST_CR0,
198221828Sgrehan	VM_REG_GUEST_CR3,
199221828Sgrehan	VM_REG_GUEST_CR4,
200221828Sgrehan	VM_REG_GUEST_DR7,
201221828Sgrehan	VM_REG_GUEST_RSP,
202221828Sgrehan	VM_REG_GUEST_RIP,
203221828Sgrehan	VM_REG_GUEST_RFLAGS,
204221828Sgrehan	VM_REG_GUEST_ES,
205221828Sgrehan	VM_REG_GUEST_CS,
206221828Sgrehan	VM_REG_GUEST_SS,
207221828Sgrehan	VM_REG_GUEST_DS,
208221828Sgrehan	VM_REG_GUEST_FS,
209221828Sgrehan	VM_REG_GUEST_GS,
210221828Sgrehan	VM_REG_GUEST_LDTR,
211221828Sgrehan	VM_REG_GUEST_TR,
212221828Sgrehan	VM_REG_GUEST_IDTR,
213221828Sgrehan	VM_REG_GUEST_GDTR,
214221828Sgrehan	VM_REG_GUEST_EFER,
215221828Sgrehan	VM_REG_LAST
216221828Sgrehan};
217221828Sgrehan
218221828Sgrehan/*
219221828Sgrehan * Identifiers for optional vmm capabilities
220221828Sgrehan */
221221828Sgrehanenum vm_cap_type {
222221828Sgrehan	VM_CAP_HALT_EXIT,
223221828Sgrehan	VM_CAP_MTRAP_EXIT,
224221828Sgrehan	VM_CAP_PAUSE_EXIT,
225221828Sgrehan	VM_CAP_UNRESTRICTED_GUEST,
226221828Sgrehan	VM_CAP_MAX
227221828Sgrehan};
228221828Sgrehan
229240922Sneelenum x2apic_state {
230240922Sneel	X2APIC_ENABLED,
231240922Sneel	X2APIC_AVAILABLE,
232240922Sneel	X2APIC_DISABLED,
233240922Sneel	X2APIC_STATE_LAST
234240922Sneel};
235240922Sneel
236221828Sgrehan/*
237221828Sgrehan * The 'access' field has the format specified in Table 21-2 of the Intel
238221828Sgrehan * Architecture Manual vol 3b.
239221828Sgrehan *
240221828Sgrehan * XXX The contents of the 'access' field are architecturally defined except
241221828Sgrehan * bit 16 - Segment Unusable.
242221828Sgrehan */
243221828Sgrehanstruct seg_desc {
244221828Sgrehan	uint64_t	base;
245221828Sgrehan	uint32_t	limit;
246221828Sgrehan	uint32_t	access;
247221828Sgrehan};
248221828Sgrehan
249221828Sgrehanenum vm_exitcode {
250221828Sgrehan	VM_EXITCODE_INOUT,
251221828Sgrehan	VM_EXITCODE_VMX,
252221828Sgrehan	VM_EXITCODE_BOGUS,
253221828Sgrehan	VM_EXITCODE_RDMSR,
254221828Sgrehan	VM_EXITCODE_WRMSR,
255221828Sgrehan	VM_EXITCODE_HLT,
256221828Sgrehan	VM_EXITCODE_MTRAP,
257221828Sgrehan	VM_EXITCODE_PAUSE,
258234761Sgrehan	VM_EXITCODE_PAGING,
259256072Sneel	VM_EXITCODE_INST_EMUL,
260240912Sneel	VM_EXITCODE_SPINUP_AP,
261234761Sgrehan	VM_EXITCODE_MAX
262221828Sgrehan};
263221828Sgrehan
264221828Sgrehanstruct vm_exit {
265221828Sgrehan	enum vm_exitcode	exitcode;
266221828Sgrehan	int			inst_length;	/* 0 means unknown */
267221828Sgrehan	uint64_t		rip;
268221828Sgrehan	union {
269221828Sgrehan		struct {
270221828Sgrehan			uint16_t	bytes:3;	/* 1 or 2 or 4 */
271221828Sgrehan			uint16_t	in:1;		/* out is 0, in is 1 */
272221828Sgrehan			uint16_t	string:1;
273221828Sgrehan			uint16_t	rep:1;
274221828Sgrehan			uint16_t	port;
275221828Sgrehan			uint32_t	eax;		/* valid for out */
276221828Sgrehan		} inout;
277234761Sgrehan		struct {
278241497Sgrehan			uint64_t	gpa;
279256072Sneel			int		fault_type;
280256072Sneel			int		protection;
281256072Sneel		} paging;
282256072Sneel		struct {
283256072Sneel			uint64_t	gpa;
284256072Sneel			uint64_t	gla;
285256072Sneel			uint64_t	cr3;
286243640Sneel			struct vie	vie;
287256072Sneel		} inst_emul;
288221828Sgrehan		/*
289221828Sgrehan		 * VMX specific payload. Used when there is no "better"
290221828Sgrehan		 * exitcode to represent the VM-exit.
291221828Sgrehan		 */
292221828Sgrehan		struct {
293221828Sgrehan			int		error;		/* vmx inst error */
294221828Sgrehan			uint32_t	exit_reason;
295221828Sgrehan			uint64_t	exit_qualification;
296221828Sgrehan		} vmx;
297221828Sgrehan		struct {
298221828Sgrehan			uint32_t	code;		/* ecx value */
299221828Sgrehan			uint64_t	wval;
300221828Sgrehan		} msr;
301240912Sneel		struct {
302240912Sneel			int		vcpu;
303240912Sneel			uint64_t	rip;
304240912Sneel		} spinup_ap;
305221828Sgrehan	} u;
306221828Sgrehan};
307221828Sgrehan
308221828Sgrehan#endif	/* _VMM_H_ */
309