vmm.h revision 260167
1131476Spjd/*-
2142727Spjd * Copyright (c) 2011 NetApp, Inc.
3131476Spjd * All rights reserved.
4131476Spjd *
5131476Spjd * Redistribution and use in source and binary forms, with or without
6131476Spjd * modification, are permitted provided that the following conditions
7131476Spjd * are met:
8131476Spjd * 1. Redistributions of source code must retain the above copyright
9131476Spjd *    notice, this list of conditions and the following disclaimer.
10131476Spjd * 2. Redistributions in binary form must reproduce the above copyright
11131476Spjd *    notice, this list of conditions and the following disclaimer in the
12131476Spjd *    documentation and/or other materials provided with the distribution.
13155175Spjd *
14131476Spjd * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15131476Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16131476Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17131476Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18131476Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19131476Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20131476Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21131476Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22131476Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23131476Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24131476Spjd * SUCH DAMAGE.
25131476Spjd *
26131476Spjd * $FreeBSD: head/sys/amd64/include/vmm.h 260167 2014-01-01 21:17:08Z neel $
27131476Spjd */
28131476Spjd
29131476Spjd#ifndef _VMM_H_
30131476Spjd#define	_VMM_H_
31131476Spjd
32131476Spjd#ifdef _KERNEL
33131476Spjd
34131476Spjd#define	VM_MAX_NAMELEN	32
35131476Spjd
36131476Spjdstruct vm;
37131476Spjdstruct vm_memory_segment;
38131476Spjdstruct seg_desc;
39131476Spjdstruct vm_exit;
40131476Spjdstruct vm_run;
41131476Spjdstruct vhpet;
42131476Spjdstruct vioapic;
43179550Smarcelstruct vlapic;
44176852Sdelphijstruct vmspace;
45176852Sdelphijstruct vm_object;
46176852Sdelphijstruct pmap;
47176852Sdelphij
48131476Spjdenum x2apic_state;
49176852Sdelphij
50176852Sdelphijtypedef int	(*vmm_init_func_t)(void);
51131476Spjdtypedef int	(*vmm_cleanup_func_t)(void);
52131476Spjdtypedef void	(*vmm_resume_func_t)(void);
53132344Spjdtypedef void *	(*vmi_init_func_t)(struct vm *vm, struct pmap *pmap);
54132344Spjdtypedef int	(*vmi_run_func_t)(void *vmi, int vcpu, register_t rip,
55131476Spjd				  struct pmap *pmap);
56131476Spjdtypedef void	(*vmi_cleanup_func_t)(void *vmi);
57176852Sdelphijtypedef int	(*vmi_get_register_t)(void *vmi, int vcpu, int num,
58212554Spjd				      uint64_t *retval);
59143586Spjdtypedef int	(*vmi_set_register_t)(void *vmi, int vcpu, int num,
60143586Spjd				      uint64_t val);
61143586Spjdtypedef int	(*vmi_get_desc_t)(void *vmi, int vcpu, int num,
62212554Spjd				  struct seg_desc *desc);
63143586Spjdtypedef int	(*vmi_set_desc_t)(void *vmi, int vcpu, int num,
64131476Spjd				  struct seg_desc *desc);
65131476Spjdtypedef int	(*vmi_inject_event_t)(void *vmi, int vcpu,
66162868Spjd				      int type, int vector,
67131476Spjd				      uint32_t code, int code_valid);
68143586Spjdtypedef int	(*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval);
69212554Spjdtypedef int	(*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val);
70131476Spjdtypedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max);
71212554Spjdtypedef void	(*vmi_vmspace_free)(struct vmspace *vmspace);
72143586Spjdtypedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu);
73143586Spjdtypedef void	(*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic);
74143586Spjd
75212554Spjdstruct vmm_ops {
76143586Spjd	vmm_init_func_t		init;		/* module wide initialization */
77131649Spjd	vmm_cleanup_func_t	cleanup;
78131649Spjd	vmm_resume_func_t	resume;
79162868Spjd
80131649Spjd	vmi_init_func_t		vminit;		/* vm-specific initialization */
81143586Spjd	vmi_run_func_t		vmrun;
82212554Spjd	vmi_cleanup_func_t	vmcleanup;
83131649Spjd	vmi_get_register_t	vmgetreg;
84131476Spjd	vmi_set_register_t	vmsetreg;
85131476Spjd	vmi_get_desc_t		vmgetdesc;
86131476Spjd	vmi_set_desc_t		vmsetdesc;
87131476Spjd	vmi_inject_event_t	vminject;
88131476Spjd	vmi_get_cap_t		vmgetcap;
89131476Spjd	vmi_set_cap_t		vmsetcap;
90131476Spjd	vmi_vmspace_alloc	vmspace_alloc;
91131476Spjd	vmi_vmspace_free	vmspace_free;
92131476Spjd	vmi_vlapic_init		vlapic_init;
93131476Spjd	vmi_vlapic_cleanup	vlapic_cleanup;
94131476Spjd};
95131476Spjd
96131476Spjdextern struct vmm_ops vmm_ops_intel;
97153190Spjdextern struct vmm_ops vmm_ops_amd;
98131476Spjd
99131476Spjdint vm_create(const char *name, struct vm **retvm);
100131476Spjdvoid vm_destroy(struct vm *vm);
101131476Spjdconst char *vm_name(struct vm *vm);
102131476Spjdint vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len);
103131476Spjdint vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
104131476Spjdint vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len);
105131476Spjdvoid *vm_gpa_hold(struct vm *, vm_paddr_t gpa, size_t len, int prot,
106132344Spjd		  void **cookie);
107132344Spjdvoid vm_gpa_release(void *cookie);
108131476Spjdint vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase,
109131476Spjd	      struct vm_memory_segment *seg);
110131476Spjdint vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len,
111131476Spjd		  vm_offset_t *offset, struct vm_object **object);
112131476Spjdboolean_t vm_mem_allocated(struct vm *vm, vm_paddr_t gpa);
113131476Spjdint vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval);
114131476Spjdint vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val);
115131476Spjdint vm_get_seg_desc(struct vm *vm, int vcpu, int reg,
116131476Spjd		    struct seg_desc *ret_desc);
117131476Spjdint vm_set_seg_desc(struct vm *vm, int vcpu, int reg,
118153190Spjd		    struct seg_desc *desc);
119131476Spjdint vm_run(struct vm *vm, struct vm_run *vmrun);
120153190Spjdint vm_inject_event(struct vm *vm, int vcpu, int type,
121153190Spjd		    int vector, uint32_t error_code, int error_code_valid);
122131476Spjdint vm_inject_nmi(struct vm *vm, int vcpu);
123131476Spjdint vm_nmi_pending(struct vm *vm, int vcpuid);
124131476Spjdvoid vm_nmi_clear(struct vm *vm, int vcpuid);
125131476Spjduint64_t *vm_guest_msrs(struct vm *vm, int cpu);
126131476Spjdstruct vlapic *vm_lapic(struct vm *vm, int cpu);
127131476Spjdstruct vioapic *vm_ioapic(struct vm *vm);
128131476Spjdstruct vhpet *vm_hpet(struct vm *vm);
129153190Spjdint vm_get_capability(struct vm *vm, int vcpu, int type, int *val);
130131476Spjdint vm_set_capability(struct vm *vm, int vcpu, int type, int val);
131131476Spjdint vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state);
132131476Spjdint vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state);
133131476Spjdint vm_apicid2vcpuid(struct vm *vm, int apicid);
134131476Spjdvoid vm_activate_cpu(struct vm *vm, int vcpu);
135131476Spjdcpuset_t vm_active_cpus(struct vm *vm);
136131476Spjdstruct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid);
137131476Spjd
138131476Spjd/*
139153190Spjd * Return 1 if device indicated by bus/slot/func is supposed to be a
140131476Spjd * pci passthrough device.
141142727Spjd *
142142727Spjd * Return 0 otherwise.
143142727Spjd */
144142727Spjdint vmm_is_pptdev(int bus, int slot, int func);
145142727Spjd
146142727Spjdvoid *vm_iommu_domain(struct vm *vm);
147131476Spjd
148131476Spjdenum vcpu_state {
149131476Spjd	VCPU_IDLE,
150131476Spjd	VCPU_FROZEN,
151131476Spjd	VCPU_RUNNING,
152131476Spjd	VCPU_SLEEPING,
153131476Spjd};
154131476Spjd
155131476Spjdint vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state,
156131476Spjd    bool from_idle);
157131476Spjdenum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu);
158131476Spjd
159131476Spjdstatic int __inline
160131476Spjdvcpu_is_running(struct vm *vm, int vcpu, int *hostcpu)
161131476Spjd{
162131476Spjd	return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING);
163131476Spjd}
164131476Spjd
165131476Spjdvoid *vcpu_stats(struct vm *vm, int vcpu);
166153190Spjdvoid vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr);
167131476Spjdstruct vmspace *vm_get_vmspace(struct vm *vm);
168153190Spjdint vm_assign_pptdev(struct vm *vm, int bus, int slot, int func);
169153190Spjdint vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func);
170131476Spjd#endif	/* KERNEL */
171131476Spjd
172131476Spjd#include <machine/vmm_instruction_emul.h>
173131476Spjd
174153190Spjd#define	VM_MAXCPU	16			/* maximum virtual cpus */
175153190Spjd
176131476Spjd/*
177131476Spjd * Identifiers for events that can be injected into the VM
178131476Spjd */
179131476Spjdenum vm_event_type {
180131476Spjd	VM_EVENT_NONE,
181131476Spjd	VM_HW_INTR,
182131476Spjd	VM_NMI,
183131476Spjd	VM_HW_EXCEPTION,
184155175Spjd	VM_SW_INTR,
185131476Spjd	VM_PRIV_SW_EXCEPTION,
186131476Spjd	VM_SW_EXCEPTION,
187132344Spjd	VM_EVENT_MAX
188155175Spjd};
189132344Spjd
190132344Spjd/*
191132344Spjd * Identifiers for architecturally defined registers.
192155175Spjd */
193132344Spjdenum vm_reg_name {
194132344Spjd	VM_REG_GUEST_RAX,
195132344Spjd	VM_REG_GUEST_RBX,
196132344Spjd	VM_REG_GUEST_RCX,
197132344Spjd	VM_REG_GUEST_RDX,
198132344Spjd	VM_REG_GUEST_RSI,
199132344Spjd	VM_REG_GUEST_RDI,
200132344Spjd	VM_REG_GUEST_RBP,
201132344Spjd	VM_REG_GUEST_R8,
202153190Spjd	VM_REG_GUEST_R9,
203132344Spjd	VM_REG_GUEST_R10,
204153190Spjd	VM_REG_GUEST_R11,
205153190Spjd	VM_REG_GUEST_R12,
206132344Spjd	VM_REG_GUEST_R13,
207132344Spjd	VM_REG_GUEST_R14,
208132344Spjd	VM_REG_GUEST_R15,
209132344Spjd	VM_REG_GUEST_CR0,
210153190Spjd	VM_REG_GUEST_CR3,
211153190Spjd	VM_REG_GUEST_CR4,
212132344Spjd	VM_REG_GUEST_DR7,
213132344Spjd	VM_REG_GUEST_RSP,
214132344Spjd	VM_REG_GUEST_RIP,
215132344Spjd	VM_REG_GUEST_RFLAGS,
216132344Spjd	VM_REG_GUEST_ES,
217132344Spjd	VM_REG_GUEST_CS,
218132344Spjd	VM_REG_GUEST_SS,
219132344Spjd	VM_REG_GUEST_DS,
220132344Spjd	VM_REG_GUEST_FS,
221132344Spjd	VM_REG_GUEST_GS,
222132344Spjd	VM_REG_GUEST_LDTR,
223132344Spjd	VM_REG_GUEST_TR,
224132344Spjd	VM_REG_GUEST_IDTR,
225132344Spjd	VM_REG_GUEST_GDTR,
226	VM_REG_GUEST_EFER,
227	VM_REG_LAST
228};
229
230/*
231 * Identifiers for optional vmm capabilities
232 */
233enum vm_cap_type {
234	VM_CAP_HALT_EXIT,
235	VM_CAP_MTRAP_EXIT,
236	VM_CAP_PAUSE_EXIT,
237	VM_CAP_UNRESTRICTED_GUEST,
238	VM_CAP_ENABLE_INVPCID,
239	VM_CAP_MAX
240};
241
242enum x2apic_state {
243	X2APIC_ENABLED,
244	X2APIC_AVAILABLE,
245	X2APIC_DISABLED,
246	X2APIC_STATE_LAST
247};
248
249/*
250 * The 'access' field has the format specified in Table 21-2 of the Intel
251 * Architecture Manual vol 3b.
252 *
253 * XXX The contents of the 'access' field are architecturally defined except
254 * bit 16 - Segment Unusable.
255 */
256struct seg_desc {
257	uint64_t	base;
258	uint32_t	limit;
259	uint32_t	access;
260};
261
262enum vm_exitcode {
263	VM_EXITCODE_INOUT,
264	VM_EXITCODE_VMX,
265	VM_EXITCODE_BOGUS,
266	VM_EXITCODE_RDMSR,
267	VM_EXITCODE_WRMSR,
268	VM_EXITCODE_HLT,
269	VM_EXITCODE_MTRAP,
270	VM_EXITCODE_PAUSE,
271	VM_EXITCODE_PAGING,
272	VM_EXITCODE_INST_EMUL,
273	VM_EXITCODE_SPINUP_AP,
274	VM_EXITCODE_SPINDOWN_CPU,
275	VM_EXITCODE_MAX
276};
277
278struct vm_exit {
279	enum vm_exitcode	exitcode;
280	int			inst_length;	/* 0 means unknown */
281	uint64_t		rip;
282	union {
283		struct {
284			uint16_t	bytes:3;	/* 1 or 2 or 4 */
285			uint16_t	in:1;		/* out is 0, in is 1 */
286			uint16_t	string:1;
287			uint16_t	rep:1;
288			uint16_t	port;
289			uint32_t	eax;		/* valid for out */
290		} inout;
291		struct {
292			uint64_t	gpa;
293			int		fault_type;
294		} paging;
295		struct {
296			uint64_t	gpa;
297			uint64_t	gla;
298			uint64_t	cr3;
299			struct vie	vie;
300		} inst_emul;
301		/*
302		 * VMX specific payload. Used when there is no "better"
303		 * exitcode to represent the VM-exit.
304		 */
305		struct {
306			int		status;		/* vmx inst status */
307			/*
308			 * 'exit_reason' and 'exit_qualification' are valid
309			 * only if 'status' is zero.
310			 */
311			uint32_t	exit_reason;
312			uint64_t	exit_qualification;
313			/*
314			 * 'inst_error' and 'inst_type' are valid
315			 * only if 'status' is non-zero.
316			 */
317			int		inst_type;
318			int		inst_error;
319		} vmx;
320		struct {
321			uint32_t	code;		/* ecx value */
322			uint64_t	wval;
323		} msr;
324		struct {
325			int		vcpu;
326			uint64_t	rip;
327		} spinup_ap;
328		struct {
329			uint64_t	rflags;
330		} hlt;
331	} u;
332};
333
334#endif	/* _VMM_H_ */
335