vmm.h revision 262506
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 262506 2014-02-26 00:52:05Z 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;
37262506Sneelstruct vm_exception;
38221828Sgrehanstruct vm_memory_segment;
39221828Sgrehanstruct seg_desc;
40221828Sgrehanstruct vm_exit;
41221828Sgrehanstruct vm_run;
42258579Sneelstruct vhpet;
43258075Sneelstruct vioapic;
44221828Sgrehanstruct vlapic;
45256072Sneelstruct vmspace;
46256072Sneelstruct vm_object;
47256072Sneelstruct pmap;
48221828Sgrehan
49240922Sneelenum x2apic_state;
50240922Sneel
51260466Sneeltypedef int	(*vmm_init_func_t)(int ipinum);
52221828Sgrehantypedef int	(*vmm_cleanup_func_t)(void);
53259782Sjhbtypedef void	(*vmm_resume_func_t)(void);
54256072Sneeltypedef void *	(*vmi_init_func_t)(struct vm *vm, struct pmap *pmap);
55256072Sneeltypedef int	(*vmi_run_func_t)(void *vmi, int vcpu, register_t rip,
56260619Sneel				  struct pmap *pmap, void *rendezvous_cookie);
57221828Sgrehantypedef void	(*vmi_cleanup_func_t)(void *vmi);
58221828Sgrehantypedef int	(*vmi_get_register_t)(void *vmi, int vcpu, int num,
59221828Sgrehan				      uint64_t *retval);
60221828Sgrehantypedef int	(*vmi_set_register_t)(void *vmi, int vcpu, int num,
61221828Sgrehan				      uint64_t val);
62221828Sgrehantypedef int	(*vmi_get_desc_t)(void *vmi, int vcpu, int num,
63221828Sgrehan				  struct seg_desc *desc);
64221828Sgrehantypedef int	(*vmi_set_desc_t)(void *vmi, int vcpu, int num,
65221828Sgrehan				  struct seg_desc *desc);
66221828Sgrehantypedef int	(*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval);
67221828Sgrehantypedef int	(*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val);
68256072Sneeltypedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max);
69256072Sneeltypedef void	(*vmi_vmspace_free)(struct vmspace *vmspace);
70259863Sneeltypedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu);
71259863Sneeltypedef void	(*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic);
72221828Sgrehan
73221828Sgrehanstruct vmm_ops {
74221828Sgrehan	vmm_init_func_t		init;		/* module wide initialization */
75221828Sgrehan	vmm_cleanup_func_t	cleanup;
76259782Sjhb	vmm_resume_func_t	resume;
77221828Sgrehan
78221828Sgrehan	vmi_init_func_t		vminit;		/* vm-specific initialization */
79221828Sgrehan	vmi_run_func_t		vmrun;
80221828Sgrehan	vmi_cleanup_func_t	vmcleanup;
81221828Sgrehan	vmi_get_register_t	vmgetreg;
82221828Sgrehan	vmi_set_register_t	vmsetreg;
83221828Sgrehan	vmi_get_desc_t		vmgetdesc;
84221828Sgrehan	vmi_set_desc_t		vmsetdesc;
85221828Sgrehan	vmi_get_cap_t		vmgetcap;
86221828Sgrehan	vmi_set_cap_t		vmsetcap;
87256072Sneel	vmi_vmspace_alloc	vmspace_alloc;
88256072Sneel	vmi_vmspace_free	vmspace_free;
89259863Sneel	vmi_vlapic_init		vlapic_init;
90259863Sneel	vmi_vlapic_cleanup	vlapic_cleanup;
91221828Sgrehan};
92221828Sgrehan
93221828Sgrehanextern struct vmm_ops vmm_ops_intel;
94221828Sgrehanextern struct vmm_ops vmm_ops_amd;
95221828Sgrehan
96249396Sneelint vm_create(const char *name, struct vm **retvm);
97221828Sgrehanvoid vm_destroy(struct vm *vm);
98221828Sgrehanconst char *vm_name(struct vm *vm);
99241041Sneelint vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len);
100221828Sgrehanint vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
101221828Sgrehanint vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len);
102256072Sneelvoid *vm_gpa_hold(struct vm *, vm_paddr_t gpa, size_t len, int prot,
103256072Sneel		  void **cookie);
104256072Sneelvoid vm_gpa_release(void *cookie);
105221828Sgrehanint vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase,
106221828Sgrehan	      struct vm_memory_segment *seg);
107256072Sneelint vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len,
108256072Sneel		  vm_offset_t *offset, struct vm_object **object);
109256072Sneelboolean_t vm_mem_allocated(struct vm *vm, vm_paddr_t gpa);
110221828Sgrehanint vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval);
111221828Sgrehanint vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val);
112221828Sgrehanint vm_get_seg_desc(struct vm *vm, int vcpu, int reg,
113221828Sgrehan		    struct seg_desc *ret_desc);
114221828Sgrehanint vm_set_seg_desc(struct vm *vm, int vcpu, int reg,
115221828Sgrehan		    struct seg_desc *desc);
116221828Sgrehanint vm_run(struct vm *vm, struct vm_run *vmrun);
117221828Sgrehanint vm_inject_nmi(struct vm *vm, int vcpu);
118241982Sneelint vm_nmi_pending(struct vm *vm, int vcpuid);
119241982Sneelvoid vm_nmi_clear(struct vm *vm, int vcpuid);
120221828Sgrehanuint64_t *vm_guest_msrs(struct vm *vm, int cpu);
121221828Sgrehanstruct vlapic *vm_lapic(struct vm *vm, int cpu);
122258075Sneelstruct vioapic *vm_ioapic(struct vm *vm);
123258579Sneelstruct vhpet *vm_hpet(struct vm *vm);
124221828Sgrehanint vm_get_capability(struct vm *vm, int vcpu, int type, int *val);
125221828Sgrehanint vm_set_capability(struct vm *vm, int vcpu, int type, int val);
126240922Sneelint vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state);
127240922Sneelint vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state);
128258075Sneelint vm_apicid2vcpuid(struct vm *vm, int apicid);
129221828Sgrehanvoid vm_activate_cpu(struct vm *vm, int vcpu);
130223621Sgrehancpuset_t vm_active_cpus(struct vm *vm);
131240894Sneelstruct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid);
132221828Sgrehan
133221828Sgrehan/*
134260619Sneel * Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'.
135260619Sneel * The rendezvous 'func(arg)' is not allowed to do anything that will
136260619Sneel * cause the thread to be put to sleep.
137260619Sneel *
138260619Sneel * If the rendezvous is being initiated from a vcpu context then the
139260619Sneel * 'vcpuid' must refer to that vcpu, otherwise it should be set to -1.
140260619Sneel *
141260619Sneel * The caller cannot hold any locks when initiating the rendezvous.
142260619Sneel *
143260619Sneel * The implementation of this API may cause vcpus other than those specified
144260619Sneel * by 'dest' to be stalled. The caller should not rely on any vcpus making
145260619Sneel * forward progress when the rendezvous is in progress.
146260619Sneel */
147260619Sneeltypedef void (*vm_rendezvous_func_t)(struct vm *vm, int vcpuid, void *arg);
148260619Sneelvoid vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest,
149260619Sneel    vm_rendezvous_func_t func, void *arg);
150260619Sneel
151260619Sneelstatic __inline int
152260619Sneelvcpu_rendezvous_pending(void *rendezvous_cookie)
153260619Sneel{
154260619Sneel
155260619Sneel	return (*(uintptr_t *)rendezvous_cookie != 0);
156260619Sneel}
157260619Sneel
158260619Sneel/*
159221828Sgrehan * Return 1 if device indicated by bus/slot/func is supposed to be a
160221828Sgrehan * pci passthrough device.
161221828Sgrehan *
162221828Sgrehan * Return 0 otherwise.
163221828Sgrehan */
164221828Sgrehanint vmm_is_pptdev(int bus, int slot, int func);
165221828Sgrehan
166221828Sgrehanvoid *vm_iommu_domain(struct vm *vm);
167221828Sgrehan
168241489Sneelenum vcpu_state {
169241489Sneel	VCPU_IDLE,
170256072Sneel	VCPU_FROZEN,
171241489Sneel	VCPU_RUNNING,
172256072Sneel	VCPU_SLEEPING,
173241489Sneel};
174221828Sgrehan
175259737Sneelint vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state,
176259737Sneel    bool from_idle);
177249879Sgrehanenum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu);
178221828Sgrehan
179221828Sgrehanstatic int __inline
180249879Sgrehanvcpu_is_running(struct vm *vm, int vcpu, int *hostcpu)
181221828Sgrehan{
182249879Sgrehan	return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING);
183221828Sgrehan}
184221828Sgrehan
185241489Sneelvoid *vcpu_stats(struct vm *vm, int vcpu);
186259863Sneelvoid vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr);
187256072Sneelstruct vmspace *vm_get_vmspace(struct vm *vm);
188256072Sneelint vm_assign_pptdev(struct vm *vm, int bus, int slot, int func);
189256072Sneelint vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func);
190262506Sneel
191262506Sneel/*
192262506Sneel * Inject exception 'vme' into the guest vcpu. This function returns 0 on
193262506Sneel * success and non-zero on failure.
194262506Sneel *
195262506Sneel * Wrapper functions like 'vm_inject_gp()' should be preferred to calling
196262506Sneel * this function directly because they enforce the trap-like or fault-like
197262506Sneel * behavior of an exception.
198262506Sneel *
199262506Sneel * This function should only be called in the context of the thread that is
200262506Sneel * executing this vcpu.
201262506Sneel */
202262506Sneelint vm_inject_exception(struct vm *vm, int vcpuid, struct vm_exception *vme);
203262506Sneel
204262506Sneel/*
205262506Sneel * Returns 0 if there is no exception pending for this vcpu. Returns 1 if an
206262506Sneel * exception is pending and also updates 'vme'. The pending exception is
207262506Sneel * cleared when this function returns.
208262506Sneel *
209262506Sneel * This function should only be called in the context of the thread that is
210262506Sneel * executing this vcpu.
211262506Sneel */
212262506Sneelint vm_exception_pending(struct vm *vm, int vcpuid, struct vm_exception *vme);
213262506Sneel
214262506Sneelvoid vm_inject_gp(struct vm *vm, int vcpuid); /* general protection fault */
215262506Sneelvoid vm_inject_ud(struct vm *vm, int vcpuid); /* undefined instruction fault */
216262506Sneel
217221828Sgrehan#endif	/* KERNEL */
218221828Sgrehan
219243640Sneel#include <machine/vmm_instruction_emul.h>
220243640Sneel
221255438Sgrehan#define	VM_MAXCPU	16			/* maximum virtual cpus */
222221828Sgrehan
223221828Sgrehan/*
224221828Sgrehan * Identifiers for architecturally defined registers.
225221828Sgrehan */
226221828Sgrehanenum vm_reg_name {
227221828Sgrehan	VM_REG_GUEST_RAX,
228221828Sgrehan	VM_REG_GUEST_RBX,
229221828Sgrehan	VM_REG_GUEST_RCX,
230221828Sgrehan	VM_REG_GUEST_RDX,
231221828Sgrehan	VM_REG_GUEST_RSI,
232221828Sgrehan	VM_REG_GUEST_RDI,
233221828Sgrehan	VM_REG_GUEST_RBP,
234221828Sgrehan	VM_REG_GUEST_R8,
235221828Sgrehan	VM_REG_GUEST_R9,
236221828Sgrehan	VM_REG_GUEST_R10,
237221828Sgrehan	VM_REG_GUEST_R11,
238221828Sgrehan	VM_REG_GUEST_R12,
239221828Sgrehan	VM_REG_GUEST_R13,
240221828Sgrehan	VM_REG_GUEST_R14,
241221828Sgrehan	VM_REG_GUEST_R15,
242221828Sgrehan	VM_REG_GUEST_CR0,
243221828Sgrehan	VM_REG_GUEST_CR3,
244221828Sgrehan	VM_REG_GUEST_CR4,
245221828Sgrehan	VM_REG_GUEST_DR7,
246221828Sgrehan	VM_REG_GUEST_RSP,
247221828Sgrehan	VM_REG_GUEST_RIP,
248221828Sgrehan	VM_REG_GUEST_RFLAGS,
249221828Sgrehan	VM_REG_GUEST_ES,
250221828Sgrehan	VM_REG_GUEST_CS,
251221828Sgrehan	VM_REG_GUEST_SS,
252221828Sgrehan	VM_REG_GUEST_DS,
253221828Sgrehan	VM_REG_GUEST_FS,
254221828Sgrehan	VM_REG_GUEST_GS,
255221828Sgrehan	VM_REG_GUEST_LDTR,
256221828Sgrehan	VM_REG_GUEST_TR,
257221828Sgrehan	VM_REG_GUEST_IDTR,
258221828Sgrehan	VM_REG_GUEST_GDTR,
259221828Sgrehan	VM_REG_GUEST_EFER,
260221828Sgrehan	VM_REG_LAST
261221828Sgrehan};
262221828Sgrehan
263221828Sgrehan/*
264221828Sgrehan * Identifiers for optional vmm capabilities
265221828Sgrehan */
266221828Sgrehanenum vm_cap_type {
267221828Sgrehan	VM_CAP_HALT_EXIT,
268221828Sgrehan	VM_CAP_MTRAP_EXIT,
269221828Sgrehan	VM_CAP_PAUSE_EXIT,
270221828Sgrehan	VM_CAP_UNRESTRICTED_GUEST,
271256645Sneel	VM_CAP_ENABLE_INVPCID,
272221828Sgrehan	VM_CAP_MAX
273221828Sgrehan};
274221828Sgrehan
275240922Sneelenum x2apic_state {
276262236Sneel	X2APIC_DISABLED,
277240922Sneel	X2APIC_ENABLED,
278240922Sneel	X2APIC_STATE_LAST
279240922Sneel};
280240922Sneel
281221828Sgrehan/*
282221828Sgrehan * The 'access' field has the format specified in Table 21-2 of the Intel
283221828Sgrehan * Architecture Manual vol 3b.
284221828Sgrehan *
285221828Sgrehan * XXX The contents of the 'access' field are architecturally defined except
286221828Sgrehan * bit 16 - Segment Unusable.
287221828Sgrehan */
288221828Sgrehanstruct seg_desc {
289221828Sgrehan	uint64_t	base;
290221828Sgrehan	uint32_t	limit;
291221828Sgrehan	uint32_t	access;
292221828Sgrehan};
293221828Sgrehan
294221828Sgrehanenum vm_exitcode {
295221828Sgrehan	VM_EXITCODE_INOUT,
296221828Sgrehan	VM_EXITCODE_VMX,
297221828Sgrehan	VM_EXITCODE_BOGUS,
298221828Sgrehan	VM_EXITCODE_RDMSR,
299221828Sgrehan	VM_EXITCODE_WRMSR,
300221828Sgrehan	VM_EXITCODE_HLT,
301221828Sgrehan	VM_EXITCODE_MTRAP,
302221828Sgrehan	VM_EXITCODE_PAUSE,
303234761Sgrehan	VM_EXITCODE_PAGING,
304256072Sneel	VM_EXITCODE_INST_EMUL,
305240912Sneel	VM_EXITCODE_SPINUP_AP,
306259081Sneel	VM_EXITCODE_SPINDOWN_CPU,
307260619Sneel	VM_EXITCODE_RENDEZVOUS,
308261170Sneel	VM_EXITCODE_IOAPIC_EOI,
309234761Sgrehan	VM_EXITCODE_MAX
310221828Sgrehan};
311221828Sgrehan
312221828Sgrehanstruct vm_exit {
313221828Sgrehan	enum vm_exitcode	exitcode;
314221828Sgrehan	int			inst_length;	/* 0 means unknown */
315221828Sgrehan	uint64_t		rip;
316221828Sgrehan	union {
317221828Sgrehan		struct {
318221828Sgrehan			uint16_t	bytes:3;	/* 1 or 2 or 4 */
319221828Sgrehan			uint16_t	in:1;		/* out is 0, in is 1 */
320221828Sgrehan			uint16_t	string:1;
321221828Sgrehan			uint16_t	rep:1;
322221828Sgrehan			uint16_t	port;
323221828Sgrehan			uint32_t	eax;		/* valid for out */
324221828Sgrehan		} inout;
325234761Sgrehan		struct {
326241497Sgrehan			uint64_t	gpa;
327256072Sneel			int		fault_type;
328256072Sneel		} paging;
329256072Sneel		struct {
330256072Sneel			uint64_t	gpa;
331256072Sneel			uint64_t	gla;
332256072Sneel			uint64_t	cr3;
333261504Sjhb			enum vie_cpu_mode cpu_mode;
334261504Sjhb			enum vie_paging_mode paging_mode;
335243640Sneel			struct vie	vie;
336256072Sneel		} inst_emul;
337221828Sgrehan		/*
338221828Sgrehan		 * VMX specific payload. Used when there is no "better"
339221828Sgrehan		 * exitcode to represent the VM-exit.
340221828Sgrehan		 */
341221828Sgrehan		struct {
342260167Sneel			int		status;		/* vmx inst status */
343260167Sneel			/*
344260167Sneel			 * 'exit_reason' and 'exit_qualification' are valid
345260167Sneel			 * only if 'status' is zero.
346260167Sneel			 */
347221828Sgrehan			uint32_t	exit_reason;
348221828Sgrehan			uint64_t	exit_qualification;
349260167Sneel			/*
350260167Sneel			 * 'inst_error' and 'inst_type' are valid
351260167Sneel			 * only if 'status' is non-zero.
352260167Sneel			 */
353260167Sneel			int		inst_type;
354260167Sneel			int		inst_error;
355221828Sgrehan		} vmx;
356221828Sgrehan		struct {
357221828Sgrehan			uint32_t	code;		/* ecx value */
358221828Sgrehan			uint64_t	wval;
359221828Sgrehan		} msr;
360240912Sneel		struct {
361240912Sneel			int		vcpu;
362240912Sneel			uint64_t	rip;
363240912Sneel		} spinup_ap;
364259081Sneel		struct {
365259081Sneel			uint64_t	rflags;
366259081Sneel		} hlt;
367261170Sneel		struct {
368261170Sneel			int		vector;
369261170Sneel		} ioapic_eoi;
370221828Sgrehan	} u;
371221828Sgrehan};
372221828Sgrehan
373221828Sgrehan#endif	/* _VMM_H_ */
374