vmm.h revision 260466
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 260466 2014-01-09 03:25:54Z 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;
41258579Sneelstruct vhpet;
42258075Sneelstruct vioapic;
43221828Sgrehanstruct vlapic;
44256072Sneelstruct vmspace;
45256072Sneelstruct vm_object;
46256072Sneelstruct pmap;
47221828Sgrehan
48240922Sneelenum x2apic_state;
49240922Sneel
50260466Sneeltypedef int	(*vmm_init_func_t)(int ipinum);
51221828Sgrehantypedef int	(*vmm_cleanup_func_t)(void);
52259782Sjhbtypedef void	(*vmm_resume_func_t)(void);
53256072Sneeltypedef void *	(*vmi_init_func_t)(struct vm *vm, struct pmap *pmap);
54256072Sneeltypedef int	(*vmi_run_func_t)(void *vmi, int vcpu, register_t rip,
55256072Sneel				  struct pmap *pmap);
56221828Sgrehantypedef void	(*vmi_cleanup_func_t)(void *vmi);
57221828Sgrehantypedef int	(*vmi_get_register_t)(void *vmi, int vcpu, int num,
58221828Sgrehan				      uint64_t *retval);
59221828Sgrehantypedef int	(*vmi_set_register_t)(void *vmi, int vcpu, int num,
60221828Sgrehan				      uint64_t val);
61221828Sgrehantypedef int	(*vmi_get_desc_t)(void *vmi, int vcpu, int num,
62221828Sgrehan				  struct seg_desc *desc);
63221828Sgrehantypedef int	(*vmi_set_desc_t)(void *vmi, int vcpu, int num,
64221828Sgrehan				  struct seg_desc *desc);
65221828Sgrehantypedef int	(*vmi_inject_event_t)(void *vmi, int vcpu,
66221828Sgrehan				      int type, int vector,
67221828Sgrehan				      uint32_t code, int code_valid);
68221828Sgrehantypedef int	(*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval);
69221828Sgrehantypedef int	(*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val);
70256072Sneeltypedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max);
71256072Sneeltypedef void	(*vmi_vmspace_free)(struct vmspace *vmspace);
72259863Sneeltypedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu);
73259863Sneeltypedef void	(*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic);
74221828Sgrehan
75221828Sgrehanstruct vmm_ops {
76221828Sgrehan	vmm_init_func_t		init;		/* module wide initialization */
77221828Sgrehan	vmm_cleanup_func_t	cleanup;
78259782Sjhb	vmm_resume_func_t	resume;
79221828Sgrehan
80221828Sgrehan	vmi_init_func_t		vminit;		/* vm-specific initialization */
81221828Sgrehan	vmi_run_func_t		vmrun;
82221828Sgrehan	vmi_cleanup_func_t	vmcleanup;
83221828Sgrehan	vmi_get_register_t	vmgetreg;
84221828Sgrehan	vmi_set_register_t	vmsetreg;
85221828Sgrehan	vmi_get_desc_t		vmgetdesc;
86221828Sgrehan	vmi_set_desc_t		vmsetdesc;
87221828Sgrehan	vmi_inject_event_t	vminject;
88221828Sgrehan	vmi_get_cap_t		vmgetcap;
89221828Sgrehan	vmi_set_cap_t		vmsetcap;
90256072Sneel	vmi_vmspace_alloc	vmspace_alloc;
91256072Sneel	vmi_vmspace_free	vmspace_free;
92259863Sneel	vmi_vlapic_init		vlapic_init;
93259863Sneel	vmi_vlapic_cleanup	vlapic_cleanup;
94221828Sgrehan};
95221828Sgrehan
96221828Sgrehanextern struct vmm_ops vmm_ops_intel;
97221828Sgrehanextern struct vmm_ops vmm_ops_amd;
98221828Sgrehan
99249396Sneelint vm_create(const char *name, struct vm **retvm);
100221828Sgrehanvoid vm_destroy(struct vm *vm);
101221828Sgrehanconst char *vm_name(struct vm *vm);
102241041Sneelint vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len);
103221828Sgrehanint vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
104221828Sgrehanint vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len);
105256072Sneelvoid *vm_gpa_hold(struct vm *, vm_paddr_t gpa, size_t len, int prot,
106256072Sneel		  void **cookie);
107256072Sneelvoid vm_gpa_release(void *cookie);
108221828Sgrehanint vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase,
109221828Sgrehan	      struct vm_memory_segment *seg);
110256072Sneelint vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len,
111256072Sneel		  vm_offset_t *offset, struct vm_object **object);
112256072Sneelboolean_t vm_mem_allocated(struct vm *vm, vm_paddr_t gpa);
113221828Sgrehanint vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval);
114221828Sgrehanint vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val);
115221828Sgrehanint vm_get_seg_desc(struct vm *vm, int vcpu, int reg,
116221828Sgrehan		    struct seg_desc *ret_desc);
117221828Sgrehanint vm_set_seg_desc(struct vm *vm, int vcpu, int reg,
118221828Sgrehan		    struct seg_desc *desc);
119221828Sgrehanint vm_run(struct vm *vm, struct vm_run *vmrun);
120221828Sgrehanint vm_inject_event(struct vm *vm, int vcpu, int type,
121221828Sgrehan		    int vector, uint32_t error_code, int error_code_valid);
122221828Sgrehanint vm_inject_nmi(struct vm *vm, int vcpu);
123241982Sneelint vm_nmi_pending(struct vm *vm, int vcpuid);
124241982Sneelvoid vm_nmi_clear(struct vm *vm, int vcpuid);
125221828Sgrehanuint64_t *vm_guest_msrs(struct vm *vm, int cpu);
126221828Sgrehanstruct vlapic *vm_lapic(struct vm *vm, int cpu);
127258075Sneelstruct vioapic *vm_ioapic(struct vm *vm);
128258579Sneelstruct vhpet *vm_hpet(struct vm *vm);
129221828Sgrehanint vm_get_capability(struct vm *vm, int vcpu, int type, int *val);
130221828Sgrehanint vm_set_capability(struct vm *vm, int vcpu, int type, int val);
131240922Sneelint vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state);
132240922Sneelint vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state);
133258075Sneelint vm_apicid2vcpuid(struct vm *vm, int apicid);
134221828Sgrehanvoid vm_activate_cpu(struct vm *vm, int vcpu);
135223621Sgrehancpuset_t vm_active_cpus(struct vm *vm);
136240894Sneelstruct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid);
137221828Sgrehan
138221828Sgrehan/*
139221828Sgrehan * Return 1 if device indicated by bus/slot/func is supposed to be a
140221828Sgrehan * pci passthrough device.
141221828Sgrehan *
142221828Sgrehan * Return 0 otherwise.
143221828Sgrehan */
144221828Sgrehanint vmm_is_pptdev(int bus, int slot, int func);
145221828Sgrehan
146221828Sgrehanvoid *vm_iommu_domain(struct vm *vm);
147221828Sgrehan
148241489Sneelenum vcpu_state {
149241489Sneel	VCPU_IDLE,
150256072Sneel	VCPU_FROZEN,
151241489Sneel	VCPU_RUNNING,
152256072Sneel	VCPU_SLEEPING,
153241489Sneel};
154221828Sgrehan
155259737Sneelint vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state,
156259737Sneel    bool from_idle);
157249879Sgrehanenum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu);
158221828Sgrehan
159221828Sgrehanstatic int __inline
160249879Sgrehanvcpu_is_running(struct vm *vm, int vcpu, int *hostcpu)
161221828Sgrehan{
162249879Sgrehan	return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING);
163221828Sgrehan}
164221828Sgrehan
165241489Sneelvoid *vcpu_stats(struct vm *vm, int vcpu);
166259863Sneelvoid vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr);
167256072Sneelstruct vmspace *vm_get_vmspace(struct vm *vm);
168256072Sneelint vm_assign_pptdev(struct vm *vm, int bus, int slot, int func);
169256072Sneelint vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func);
170221828Sgrehan#endif	/* KERNEL */
171221828Sgrehan
172243640Sneel#include <machine/vmm_instruction_emul.h>
173243640Sneel
174255438Sgrehan#define	VM_MAXCPU	16			/* maximum virtual cpus */
175221828Sgrehan
176221828Sgrehan/*
177221828Sgrehan * Identifiers for events that can be injected into the VM
178221828Sgrehan */
179221828Sgrehanenum vm_event_type {
180221828Sgrehan	VM_EVENT_NONE,
181221828Sgrehan	VM_HW_INTR,
182221828Sgrehan	VM_NMI,
183221828Sgrehan	VM_HW_EXCEPTION,
184221828Sgrehan	VM_SW_INTR,
185221828Sgrehan	VM_PRIV_SW_EXCEPTION,
186221828Sgrehan	VM_SW_EXCEPTION,
187221828Sgrehan	VM_EVENT_MAX
188221828Sgrehan};
189221828Sgrehan
190221828Sgrehan/*
191221828Sgrehan * Identifiers for architecturally defined registers.
192221828Sgrehan */
193221828Sgrehanenum vm_reg_name {
194221828Sgrehan	VM_REG_GUEST_RAX,
195221828Sgrehan	VM_REG_GUEST_RBX,
196221828Sgrehan	VM_REG_GUEST_RCX,
197221828Sgrehan	VM_REG_GUEST_RDX,
198221828Sgrehan	VM_REG_GUEST_RSI,
199221828Sgrehan	VM_REG_GUEST_RDI,
200221828Sgrehan	VM_REG_GUEST_RBP,
201221828Sgrehan	VM_REG_GUEST_R8,
202221828Sgrehan	VM_REG_GUEST_R9,
203221828Sgrehan	VM_REG_GUEST_R10,
204221828Sgrehan	VM_REG_GUEST_R11,
205221828Sgrehan	VM_REG_GUEST_R12,
206221828Sgrehan	VM_REG_GUEST_R13,
207221828Sgrehan	VM_REG_GUEST_R14,
208221828Sgrehan	VM_REG_GUEST_R15,
209221828Sgrehan	VM_REG_GUEST_CR0,
210221828Sgrehan	VM_REG_GUEST_CR3,
211221828Sgrehan	VM_REG_GUEST_CR4,
212221828Sgrehan	VM_REG_GUEST_DR7,
213221828Sgrehan	VM_REG_GUEST_RSP,
214221828Sgrehan	VM_REG_GUEST_RIP,
215221828Sgrehan	VM_REG_GUEST_RFLAGS,
216221828Sgrehan	VM_REG_GUEST_ES,
217221828Sgrehan	VM_REG_GUEST_CS,
218221828Sgrehan	VM_REG_GUEST_SS,
219221828Sgrehan	VM_REG_GUEST_DS,
220221828Sgrehan	VM_REG_GUEST_FS,
221221828Sgrehan	VM_REG_GUEST_GS,
222221828Sgrehan	VM_REG_GUEST_LDTR,
223221828Sgrehan	VM_REG_GUEST_TR,
224221828Sgrehan	VM_REG_GUEST_IDTR,
225221828Sgrehan	VM_REG_GUEST_GDTR,
226221828Sgrehan	VM_REG_GUEST_EFER,
227221828Sgrehan	VM_REG_LAST
228221828Sgrehan};
229221828Sgrehan
230221828Sgrehan/*
231221828Sgrehan * Identifiers for optional vmm capabilities
232221828Sgrehan */
233221828Sgrehanenum vm_cap_type {
234221828Sgrehan	VM_CAP_HALT_EXIT,
235221828Sgrehan	VM_CAP_MTRAP_EXIT,
236221828Sgrehan	VM_CAP_PAUSE_EXIT,
237221828Sgrehan	VM_CAP_UNRESTRICTED_GUEST,
238256645Sneel	VM_CAP_ENABLE_INVPCID,
239221828Sgrehan	VM_CAP_MAX
240221828Sgrehan};
241221828Sgrehan
242240922Sneelenum x2apic_state {
243240922Sneel	X2APIC_ENABLED,
244240922Sneel	X2APIC_AVAILABLE,
245240922Sneel	X2APIC_DISABLED,
246240922Sneel	X2APIC_STATE_LAST
247240922Sneel};
248240922Sneel
249221828Sgrehan/*
250221828Sgrehan * The 'access' field has the format specified in Table 21-2 of the Intel
251221828Sgrehan * Architecture Manual vol 3b.
252221828Sgrehan *
253221828Sgrehan * XXX The contents of the 'access' field are architecturally defined except
254221828Sgrehan * bit 16 - Segment Unusable.
255221828Sgrehan */
256221828Sgrehanstruct seg_desc {
257221828Sgrehan	uint64_t	base;
258221828Sgrehan	uint32_t	limit;
259221828Sgrehan	uint32_t	access;
260221828Sgrehan};
261221828Sgrehan
262221828Sgrehanenum vm_exitcode {
263221828Sgrehan	VM_EXITCODE_INOUT,
264221828Sgrehan	VM_EXITCODE_VMX,
265221828Sgrehan	VM_EXITCODE_BOGUS,
266221828Sgrehan	VM_EXITCODE_RDMSR,
267221828Sgrehan	VM_EXITCODE_WRMSR,
268221828Sgrehan	VM_EXITCODE_HLT,
269221828Sgrehan	VM_EXITCODE_MTRAP,
270221828Sgrehan	VM_EXITCODE_PAUSE,
271234761Sgrehan	VM_EXITCODE_PAGING,
272256072Sneel	VM_EXITCODE_INST_EMUL,
273240912Sneel	VM_EXITCODE_SPINUP_AP,
274259081Sneel	VM_EXITCODE_SPINDOWN_CPU,
275234761Sgrehan	VM_EXITCODE_MAX
276221828Sgrehan};
277221828Sgrehan
278221828Sgrehanstruct vm_exit {
279221828Sgrehan	enum vm_exitcode	exitcode;
280221828Sgrehan	int			inst_length;	/* 0 means unknown */
281221828Sgrehan	uint64_t		rip;
282221828Sgrehan	union {
283221828Sgrehan		struct {
284221828Sgrehan			uint16_t	bytes:3;	/* 1 or 2 or 4 */
285221828Sgrehan			uint16_t	in:1;		/* out is 0, in is 1 */
286221828Sgrehan			uint16_t	string:1;
287221828Sgrehan			uint16_t	rep:1;
288221828Sgrehan			uint16_t	port;
289221828Sgrehan			uint32_t	eax;		/* valid for out */
290221828Sgrehan		} inout;
291234761Sgrehan		struct {
292241497Sgrehan			uint64_t	gpa;
293256072Sneel			int		fault_type;
294256072Sneel		} paging;
295256072Sneel		struct {
296256072Sneel			uint64_t	gpa;
297256072Sneel			uint64_t	gla;
298256072Sneel			uint64_t	cr3;
299243640Sneel			struct vie	vie;
300256072Sneel		} inst_emul;
301221828Sgrehan		/*
302221828Sgrehan		 * VMX specific payload. Used when there is no "better"
303221828Sgrehan		 * exitcode to represent the VM-exit.
304221828Sgrehan		 */
305221828Sgrehan		struct {
306260167Sneel			int		status;		/* vmx inst status */
307260167Sneel			/*
308260167Sneel			 * 'exit_reason' and 'exit_qualification' are valid
309260167Sneel			 * only if 'status' is zero.
310260167Sneel			 */
311221828Sgrehan			uint32_t	exit_reason;
312221828Sgrehan			uint64_t	exit_qualification;
313260167Sneel			/*
314260167Sneel			 * 'inst_error' and 'inst_type' are valid
315260167Sneel			 * only if 'status' is non-zero.
316260167Sneel			 */
317260167Sneel			int		inst_type;
318260167Sneel			int		inst_error;
319221828Sgrehan		} vmx;
320221828Sgrehan		struct {
321221828Sgrehan			uint32_t	code;		/* ecx value */
322221828Sgrehan			uint64_t	wval;
323221828Sgrehan		} msr;
324240912Sneel		struct {
325240912Sneel			int		vcpu;
326240912Sneel			uint64_t	rip;
327240912Sneel		} spinup_ap;
328259081Sneel		struct {
329259081Sneel			uint64_t	rflags;
330259081Sneel		} hlt;
331221828Sgrehan	} u;
332221828Sgrehan};
333221828Sgrehan
334221828Sgrehan#endif	/* _VMM_H_ */
335