vmm.h revision 261504
1309260Scognet/*- 2309260Scognet * Copyright (c) 2011 NetApp, Inc. 3309260Scognet * All rights reserved. 4309260Scognet * 5309260Scognet * Redistribution and use in source and binary forms, with or without 6309260Scognet * modification, are permitted provided that the following conditions 7309260Scognet * are met: 8309260Scognet * 1. Redistributions of source code must retain the above copyright 9309260Scognet * notice, this list of conditions and the following disclaimer. 10309260Scognet * 2. Redistributions in binary form must reproduce the above copyright 11309260Scognet * notice, this list of conditions and the following disclaimer in the 12309260Scognet * documentation and/or other materials provided with the distribution. 13309260Scognet * 14309260Scognet * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 15309260Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16309260Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17309260Scognet * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 18309260Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19309260Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20309260Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21309260Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22309260Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23309260Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24309260Scognet * SUCH DAMAGE. 25309260Scognet * 26309260Scognet * $FreeBSD: head/sys/amd64/include/vmm.h 261504 2014-02-05 04:39:03Z jhb $ 27309260Scognet */ 28309260Scognet 29309260Scognet#ifndef _VMM_H_ 30309260Scognet#define _VMM_H_ 31309260Scognet 32309260Scognet#ifdef _KERNEL 33309260Scognet 34309260Scognet#define VM_MAX_NAMELEN 32 35309260Scognet 36309260Scognetstruct vm; 37309260Scognetstruct vm_memory_segment; 38309260Scognetstruct seg_desc; 39309260Scognetstruct vm_exit; 40309260Scognetstruct vm_run; 41309260Scognetstruct vhpet; 42309260Scognetstruct vioapic; 43309260Scognetstruct vlapic; 44309260Scognetstruct vmspace; 45309260Scognetstruct vm_object; 46309260Scognetstruct pmap; 47309260Scognet 48309260Scognetenum x2apic_state; 49309260Scognet 50309260Scognettypedef int (*vmm_init_func_t)(int ipinum); 51309260Scognettypedef int (*vmm_cleanup_func_t)(void); 52309260Scognettypedef void (*vmm_resume_func_t)(void); 53309260Scognettypedef void * (*vmi_init_func_t)(struct vm *vm, struct pmap *pmap); 54309260Scognettypedef int (*vmi_run_func_t)(void *vmi, int vcpu, register_t rip, 55309260Scognet struct pmap *pmap, void *rendezvous_cookie); 56309260Scognettypedef void (*vmi_cleanup_func_t)(void *vmi); 57309260Scognettypedef int (*vmi_get_register_t)(void *vmi, int vcpu, int num, 58 uint64_t *retval); 59typedef int (*vmi_set_register_t)(void *vmi, int vcpu, int num, 60 uint64_t val); 61typedef int (*vmi_get_desc_t)(void *vmi, int vcpu, int num, 62 struct seg_desc *desc); 63typedef int (*vmi_set_desc_t)(void *vmi, int vcpu, int num, 64 struct seg_desc *desc); 65typedef int (*vmi_inject_event_t)(void *vmi, int vcpu, 66 int type, int vector, 67 uint32_t code, int code_valid); 68typedef int (*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval); 69typedef int (*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val); 70typedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max); 71typedef void (*vmi_vmspace_free)(struct vmspace *vmspace); 72typedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu); 73typedef void (*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic); 74 75struct vmm_ops { 76 vmm_init_func_t init; /* module wide initialization */ 77 vmm_cleanup_func_t cleanup; 78 vmm_resume_func_t resume; 79 80 vmi_init_func_t vminit; /* vm-specific initialization */ 81 vmi_run_func_t vmrun; 82 vmi_cleanup_func_t vmcleanup; 83 vmi_get_register_t vmgetreg; 84 vmi_set_register_t vmsetreg; 85 vmi_get_desc_t vmgetdesc; 86 vmi_set_desc_t vmsetdesc; 87 vmi_inject_event_t vminject; 88 vmi_get_cap_t vmgetcap; 89 vmi_set_cap_t vmsetcap; 90 vmi_vmspace_alloc vmspace_alloc; 91 vmi_vmspace_free vmspace_free; 92 vmi_vlapic_init vlapic_init; 93 vmi_vlapic_cleanup vlapic_cleanup; 94}; 95 96extern struct vmm_ops vmm_ops_intel; 97extern struct vmm_ops vmm_ops_amd; 98 99int vm_create(const char *name, struct vm **retvm); 100void vm_destroy(struct vm *vm); 101const char *vm_name(struct vm *vm); 102int vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len); 103int vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa); 104int vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len); 105void *vm_gpa_hold(struct vm *, vm_paddr_t gpa, size_t len, int prot, 106 void **cookie); 107void vm_gpa_release(void *cookie); 108int vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, 109 struct vm_memory_segment *seg); 110int vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len, 111 vm_offset_t *offset, struct vm_object **object); 112boolean_t vm_mem_allocated(struct vm *vm, vm_paddr_t gpa); 113int vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval); 114int vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val); 115int vm_get_seg_desc(struct vm *vm, int vcpu, int reg, 116 struct seg_desc *ret_desc); 117int vm_set_seg_desc(struct vm *vm, int vcpu, int reg, 118 struct seg_desc *desc); 119int vm_run(struct vm *vm, struct vm_run *vmrun); 120int vm_inject_event(struct vm *vm, int vcpu, int type, 121 int vector, uint32_t error_code, int error_code_valid); 122int vm_inject_nmi(struct vm *vm, int vcpu); 123int vm_nmi_pending(struct vm *vm, int vcpuid); 124void vm_nmi_clear(struct vm *vm, int vcpuid); 125uint64_t *vm_guest_msrs(struct vm *vm, int cpu); 126struct vlapic *vm_lapic(struct vm *vm, int cpu); 127struct vioapic *vm_ioapic(struct vm *vm); 128struct vhpet *vm_hpet(struct vm *vm); 129int vm_get_capability(struct vm *vm, int vcpu, int type, int *val); 130int vm_set_capability(struct vm *vm, int vcpu, int type, int val); 131int vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state); 132int vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state); 133int vm_apicid2vcpuid(struct vm *vm, int apicid); 134void vm_activate_cpu(struct vm *vm, int vcpu); 135cpuset_t vm_active_cpus(struct vm *vm); 136struct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid); 137 138/* 139 * Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'. 140 * The rendezvous 'func(arg)' is not allowed to do anything that will 141 * cause the thread to be put to sleep. 142 * 143 * If the rendezvous is being initiated from a vcpu context then the 144 * 'vcpuid' must refer to that vcpu, otherwise it should be set to -1. 145 * 146 * The caller cannot hold any locks when initiating the rendezvous. 147 * 148 * The implementation of this API may cause vcpus other than those specified 149 * by 'dest' to be stalled. The caller should not rely on any vcpus making 150 * forward progress when the rendezvous is in progress. 151 */ 152typedef void (*vm_rendezvous_func_t)(struct vm *vm, int vcpuid, void *arg); 153void vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest, 154 vm_rendezvous_func_t func, void *arg); 155 156static __inline int 157vcpu_rendezvous_pending(void *rendezvous_cookie) 158{ 159 160 return (*(uintptr_t *)rendezvous_cookie != 0); 161} 162 163/* 164 * Return 1 if device indicated by bus/slot/func is supposed to be a 165 * pci passthrough device. 166 * 167 * Return 0 otherwise. 168 */ 169int vmm_is_pptdev(int bus, int slot, int func); 170 171void *vm_iommu_domain(struct vm *vm); 172 173enum vcpu_state { 174 VCPU_IDLE, 175 VCPU_FROZEN, 176 VCPU_RUNNING, 177 VCPU_SLEEPING, 178}; 179 180int vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state, 181 bool from_idle); 182enum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu); 183 184static int __inline 185vcpu_is_running(struct vm *vm, int vcpu, int *hostcpu) 186{ 187 return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING); 188} 189 190void *vcpu_stats(struct vm *vm, int vcpu); 191void vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr); 192struct vmspace *vm_get_vmspace(struct vm *vm); 193int vm_assign_pptdev(struct vm *vm, int bus, int slot, int func); 194int vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func); 195#endif /* KERNEL */ 196 197#include <machine/vmm_instruction_emul.h> 198 199#define VM_MAXCPU 16 /* maximum virtual cpus */ 200 201/* 202 * Identifiers for events that can be injected into the VM 203 */ 204enum vm_event_type { 205 VM_EVENT_NONE, 206 VM_HW_INTR, 207 VM_NMI, 208 VM_HW_EXCEPTION, 209 VM_SW_INTR, 210 VM_PRIV_SW_EXCEPTION, 211 VM_SW_EXCEPTION, 212 VM_EVENT_MAX 213}; 214 215/* 216 * Identifiers for architecturally defined registers. 217 */ 218enum vm_reg_name { 219 VM_REG_GUEST_RAX, 220 VM_REG_GUEST_RBX, 221 VM_REG_GUEST_RCX, 222 VM_REG_GUEST_RDX, 223 VM_REG_GUEST_RSI, 224 VM_REG_GUEST_RDI, 225 VM_REG_GUEST_RBP, 226 VM_REG_GUEST_R8, 227 VM_REG_GUEST_R9, 228 VM_REG_GUEST_R10, 229 VM_REG_GUEST_R11, 230 VM_REG_GUEST_R12, 231 VM_REG_GUEST_R13, 232 VM_REG_GUEST_R14, 233 VM_REG_GUEST_R15, 234 VM_REG_GUEST_CR0, 235 VM_REG_GUEST_CR3, 236 VM_REG_GUEST_CR4, 237 VM_REG_GUEST_DR7, 238 VM_REG_GUEST_RSP, 239 VM_REG_GUEST_RIP, 240 VM_REG_GUEST_RFLAGS, 241 VM_REG_GUEST_ES, 242 VM_REG_GUEST_CS, 243 VM_REG_GUEST_SS, 244 VM_REG_GUEST_DS, 245 VM_REG_GUEST_FS, 246 VM_REG_GUEST_GS, 247 VM_REG_GUEST_LDTR, 248 VM_REG_GUEST_TR, 249 VM_REG_GUEST_IDTR, 250 VM_REG_GUEST_GDTR, 251 VM_REG_GUEST_EFER, 252 VM_REG_LAST 253}; 254 255/* 256 * Identifiers for optional vmm capabilities 257 */ 258enum vm_cap_type { 259 VM_CAP_HALT_EXIT, 260 VM_CAP_MTRAP_EXIT, 261 VM_CAP_PAUSE_EXIT, 262 VM_CAP_UNRESTRICTED_GUEST, 263 VM_CAP_ENABLE_INVPCID, 264 VM_CAP_MAX 265}; 266 267enum x2apic_state { 268 X2APIC_ENABLED, 269 X2APIC_AVAILABLE, 270 X2APIC_DISABLED, 271 X2APIC_STATE_LAST 272}; 273 274/* 275 * The 'access' field has the format specified in Table 21-2 of the Intel 276 * Architecture Manual vol 3b. 277 * 278 * XXX The contents of the 'access' field are architecturally defined except 279 * bit 16 - Segment Unusable. 280 */ 281struct seg_desc { 282 uint64_t base; 283 uint32_t limit; 284 uint32_t access; 285}; 286 287enum vm_exitcode { 288 VM_EXITCODE_INOUT, 289 VM_EXITCODE_VMX, 290 VM_EXITCODE_BOGUS, 291 VM_EXITCODE_RDMSR, 292 VM_EXITCODE_WRMSR, 293 VM_EXITCODE_HLT, 294 VM_EXITCODE_MTRAP, 295 VM_EXITCODE_PAUSE, 296 VM_EXITCODE_PAGING, 297 VM_EXITCODE_INST_EMUL, 298 VM_EXITCODE_SPINUP_AP, 299 VM_EXITCODE_SPINDOWN_CPU, 300 VM_EXITCODE_RENDEZVOUS, 301 VM_EXITCODE_IOAPIC_EOI, 302 VM_EXITCODE_MAX 303}; 304 305struct vm_exit { 306 enum vm_exitcode exitcode; 307 int inst_length; /* 0 means unknown */ 308 uint64_t rip; 309 union { 310 struct { 311 uint16_t bytes:3; /* 1 or 2 or 4 */ 312 uint16_t in:1; /* out is 0, in is 1 */ 313 uint16_t string:1; 314 uint16_t rep:1; 315 uint16_t port; 316 uint32_t eax; /* valid for out */ 317 } inout; 318 struct { 319 uint64_t gpa; 320 int fault_type; 321 } paging; 322 struct { 323 uint64_t gpa; 324 uint64_t gla; 325 uint64_t cr3; 326 enum vie_cpu_mode cpu_mode; 327 enum vie_paging_mode paging_mode; 328 struct vie vie; 329 } inst_emul; 330 /* 331 * VMX specific payload. Used when there is no "better" 332 * exitcode to represent the VM-exit. 333 */ 334 struct { 335 int status; /* vmx inst status */ 336 /* 337 * 'exit_reason' and 'exit_qualification' are valid 338 * only if 'status' is zero. 339 */ 340 uint32_t exit_reason; 341 uint64_t exit_qualification; 342 /* 343 * 'inst_error' and 'inst_type' are valid 344 * only if 'status' is non-zero. 345 */ 346 int inst_type; 347 int inst_error; 348 } vmx; 349 struct { 350 uint32_t code; /* ecx value */ 351 uint64_t wval; 352 } msr; 353 struct { 354 int vcpu; 355 uint64_t rip; 356 } spinup_ap; 357 struct { 358 uint64_t rflags; 359 } hlt; 360 struct { 361 int vector; 362 } ioapic_eoi; 363 } u; 364}; 365 366#endif /* _VMM_H_ */ 367