]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/amd64/include/vmm.h
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / amd64 / include / vmm.h
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$
27  */
28
29 #ifndef _VMM_H_
30 #define _VMM_H_
31
32 #ifdef _KERNEL
33
34 #define VM_MAX_NAMELEN  32
35
36 struct vm;
37 struct vm_memory_segment;
38 struct seg_desc;
39 struct vm_exit;
40 struct vm_run;
41 struct vlapic;
42 struct vmspace;
43 struct vm_object;
44 struct pmap;
45
46 enum x2apic_state;
47
48 typedef int     (*vmm_init_func_t)(void);
49 typedef int     (*vmm_cleanup_func_t)(void);
50 typedef void *  (*vmi_init_func_t)(struct vm *vm, struct pmap *pmap);
51 typedef int     (*vmi_run_func_t)(void *vmi, int vcpu, register_t rip,
52                                   struct pmap *pmap);
53 typedef void    (*vmi_cleanup_func_t)(void *vmi);
54 typedef int     (*vmi_get_register_t)(void *vmi, int vcpu, int num,
55                                       uint64_t *retval);
56 typedef int     (*vmi_set_register_t)(void *vmi, int vcpu, int num,
57                                       uint64_t val);
58 typedef int     (*vmi_get_desc_t)(void *vmi, int vcpu, int num,
59                                   struct seg_desc *desc);
60 typedef int     (*vmi_set_desc_t)(void *vmi, int vcpu, int num,
61                                   struct seg_desc *desc);
62 typedef int     (*vmi_inject_event_t)(void *vmi, int vcpu,
63                                       int type, int vector,
64                                       uint32_t code, int code_valid);
65 typedef int     (*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval);
66 typedef int     (*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val);
67 typedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max);
68 typedef void    (*vmi_vmspace_free)(struct vmspace *vmspace);
69
70 struct vmm_ops {
71         vmm_init_func_t         init;           /* module wide initialization */
72         vmm_cleanup_func_t      cleanup;
73
74         vmi_init_func_t         vminit;         /* vm-specific initialization */
75         vmi_run_func_t          vmrun;
76         vmi_cleanup_func_t      vmcleanup;
77         vmi_get_register_t      vmgetreg;
78         vmi_set_register_t      vmsetreg;
79         vmi_get_desc_t          vmgetdesc;
80         vmi_set_desc_t          vmsetdesc;
81         vmi_inject_event_t      vminject;
82         vmi_get_cap_t           vmgetcap;
83         vmi_set_cap_t           vmsetcap;
84         vmi_vmspace_alloc       vmspace_alloc;
85         vmi_vmspace_free        vmspace_free;
86 };
87
88 extern struct vmm_ops vmm_ops_intel;
89 extern struct vmm_ops vmm_ops_amd;
90
91 int vm_create(const char *name, struct vm **retvm);
92 void vm_destroy(struct vm *vm);
93 const char *vm_name(struct vm *vm);
94 int vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len);
95 int vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
96 int vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len);
97 void *vm_gpa_hold(struct vm *, vm_paddr_t gpa, size_t len, int prot,
98                   void **cookie);
99 void vm_gpa_release(void *cookie);
100 int vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase,
101               struct vm_memory_segment *seg);
102 int vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len,
103                   vm_offset_t *offset, struct vm_object **object);
104 boolean_t vm_mem_allocated(struct vm *vm, vm_paddr_t gpa);
105 int vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval);
106 int vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val);
107 int vm_get_seg_desc(struct vm *vm, int vcpu, int reg,
108                     struct seg_desc *ret_desc);
109 int vm_set_seg_desc(struct vm *vm, int vcpu, int reg,
110                     struct seg_desc *desc);
111 int vm_run(struct vm *vm, struct vm_run *vmrun);
112 int vm_inject_event(struct vm *vm, int vcpu, int type,
113                     int vector, uint32_t error_code, int error_code_valid);
114 int vm_inject_nmi(struct vm *vm, int vcpu);
115 int vm_nmi_pending(struct vm *vm, int vcpuid);
116 void vm_nmi_clear(struct vm *vm, int vcpuid);
117 uint64_t *vm_guest_msrs(struct vm *vm, int cpu);
118 struct vlapic *vm_lapic(struct vm *vm, int cpu);
119 int vm_get_capability(struct vm *vm, int vcpu, int type, int *val);
120 int vm_set_capability(struct vm *vm, int vcpu, int type, int val);
121 int vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state);
122 int vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state);
123 void vm_activate_cpu(struct vm *vm, int vcpu);
124 cpuset_t vm_active_cpus(struct vm *vm);
125 struct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid);
126
127 /*
128  * Return 1 if device indicated by bus/slot/func is supposed to be a
129  * pci passthrough device.
130  *
131  * Return 0 otherwise.
132  */
133 int vmm_is_pptdev(int bus, int slot, int func);
134
135 void *vm_iommu_domain(struct vm *vm);
136
137 enum vcpu_state {
138         VCPU_IDLE,
139         VCPU_FROZEN,
140         VCPU_RUNNING,
141         VCPU_SLEEPING,
142 };
143
144 int vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state);
145 enum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu);
146
147 static int __inline
148 vcpu_is_running(struct vm *vm, int vcpu, int *hostcpu)
149 {
150         return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING);
151 }
152
153 void *vcpu_stats(struct vm *vm, int vcpu);
154 void vm_interrupt_hostcpu(struct vm *vm, int vcpu);
155 struct vmspace *vm_get_vmspace(struct vm *vm);
156 int vm_assign_pptdev(struct vm *vm, int bus, int slot, int func);
157 int vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func);
158 #endif  /* KERNEL */
159
160 #include <machine/vmm_instruction_emul.h>
161
162 #define VM_MAXCPU       16                      /* maximum virtual cpus */
163
164 /*
165  * Identifiers for events that can be injected into the VM
166  */
167 enum vm_event_type {
168         VM_EVENT_NONE,
169         VM_HW_INTR,
170         VM_NMI,
171         VM_HW_EXCEPTION,
172         VM_SW_INTR,
173         VM_PRIV_SW_EXCEPTION,
174         VM_SW_EXCEPTION,
175         VM_EVENT_MAX
176 };
177
178 /*
179  * Identifiers for architecturally defined registers.
180  */
181 enum vm_reg_name {
182         VM_REG_GUEST_RAX,
183         VM_REG_GUEST_RBX,
184         VM_REG_GUEST_RCX,
185         VM_REG_GUEST_RDX,
186         VM_REG_GUEST_RSI,
187         VM_REG_GUEST_RDI,
188         VM_REG_GUEST_RBP,
189         VM_REG_GUEST_R8,
190         VM_REG_GUEST_R9,
191         VM_REG_GUEST_R10,
192         VM_REG_GUEST_R11,
193         VM_REG_GUEST_R12,
194         VM_REG_GUEST_R13,
195         VM_REG_GUEST_R14,
196         VM_REG_GUEST_R15,
197         VM_REG_GUEST_CR0,
198         VM_REG_GUEST_CR3,
199         VM_REG_GUEST_CR4,
200         VM_REG_GUEST_DR7,
201         VM_REG_GUEST_RSP,
202         VM_REG_GUEST_RIP,
203         VM_REG_GUEST_RFLAGS,
204         VM_REG_GUEST_ES,
205         VM_REG_GUEST_CS,
206         VM_REG_GUEST_SS,
207         VM_REG_GUEST_DS,
208         VM_REG_GUEST_FS,
209         VM_REG_GUEST_GS,
210         VM_REG_GUEST_LDTR,
211         VM_REG_GUEST_TR,
212         VM_REG_GUEST_IDTR,
213         VM_REG_GUEST_GDTR,
214         VM_REG_GUEST_EFER,
215         VM_REG_LAST
216 };
217
218 /*
219  * Identifiers for optional vmm capabilities
220  */
221 enum vm_cap_type {
222         VM_CAP_HALT_EXIT,
223         VM_CAP_MTRAP_EXIT,
224         VM_CAP_PAUSE_EXIT,
225         VM_CAP_UNRESTRICTED_GUEST,
226         VM_CAP_ENABLE_INVPCID,
227         VM_CAP_MAX
228 };
229
230 enum x2apic_state {
231         X2APIC_ENABLED,
232         X2APIC_AVAILABLE,
233         X2APIC_DISABLED,
234         X2APIC_STATE_LAST
235 };
236
237 /*
238  * The 'access' field has the format specified in Table 21-2 of the Intel
239  * Architecture Manual vol 3b.
240  *
241  * XXX The contents of the 'access' field are architecturally defined except
242  * bit 16 - Segment Unusable.
243  */
244 struct seg_desc {
245         uint64_t        base;
246         uint32_t        limit;
247         uint32_t        access;
248 };
249
250 enum vm_exitcode {
251         VM_EXITCODE_INOUT,
252         VM_EXITCODE_VMX,
253         VM_EXITCODE_BOGUS,
254         VM_EXITCODE_RDMSR,
255         VM_EXITCODE_WRMSR,
256         VM_EXITCODE_HLT,
257         VM_EXITCODE_MTRAP,
258         VM_EXITCODE_PAUSE,
259         VM_EXITCODE_PAGING,
260         VM_EXITCODE_INST_EMUL,
261         VM_EXITCODE_SPINUP_AP,
262         VM_EXITCODE_MAX
263 };
264
265 struct vm_exit {
266         enum vm_exitcode        exitcode;
267         int                     inst_length;    /* 0 means unknown */
268         uint64_t                rip;
269         union {
270                 struct {
271                         uint16_t        bytes:3;        /* 1 or 2 or 4 */
272                         uint16_t        in:1;           /* out is 0, in is 1 */
273                         uint16_t        string:1;
274                         uint16_t        rep:1;
275                         uint16_t        port;
276                         uint32_t        eax;            /* valid for out */
277                 } inout;
278                 struct {
279                         uint64_t        gpa;
280                         int             fault_type;
281                         int             protection;
282                 } paging;
283                 struct {
284                         uint64_t        gpa;
285                         uint64_t        gla;
286                         uint64_t        cr3;
287                         struct vie      vie;
288                 } inst_emul;
289                 /*
290                  * VMX specific payload. Used when there is no "better"
291                  * exitcode to represent the VM-exit.
292                  */
293                 struct {
294                         int             error;          /* vmx inst error */
295                         uint32_t        exit_reason;
296                         uint64_t        exit_qualification;
297                 } vmx;
298                 struct {
299                         uint32_t        code;           /* ecx value */
300                         uint64_t        wval;
301                 } msr;
302                 struct {
303                         int             vcpu;
304                         uint64_t        rip;
305                 } spinup_ap;
306         } u;
307 };
308
309 #endif  /* _VMM_H_ */