vmm.h revision 265062
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: head/sys/amd64/include/vmm.h 265062 2014-04-28 22:06:40Z neel $ 27 */ 28 29#ifndef _VMM_H_ 30#define _VMM_H_ 31 32enum vm_suspend_how { 33 VM_SUSPEND_NONE, 34 VM_SUSPEND_RESET, 35 VM_SUSPEND_POWEROFF, 36 VM_SUSPEND_LAST 37}; 38 39#ifdef _KERNEL 40 41#define VM_MAX_NAMELEN 32 42 43struct vm; 44struct vm_exception; 45struct vm_memory_segment; 46struct seg_desc; 47struct vm_exit; 48struct vm_run; 49struct vhpet; 50struct vioapic; 51struct vlapic; 52struct vmspace; 53struct vm_object; 54struct pmap; 55 56enum x2apic_state; 57 58typedef int (*vmm_init_func_t)(int ipinum); 59typedef int (*vmm_cleanup_func_t)(void); 60typedef void (*vmm_resume_func_t)(void); 61typedef void * (*vmi_init_func_t)(struct vm *vm, struct pmap *pmap); 62typedef int (*vmi_run_func_t)(void *vmi, int vcpu, register_t rip, 63 struct pmap *pmap, void *rendezvous_cookie, 64 void *suspend_cookie); 65typedef void (*vmi_cleanup_func_t)(void *vmi); 66typedef int (*vmi_get_register_t)(void *vmi, int vcpu, int num, 67 uint64_t *retval); 68typedef int (*vmi_set_register_t)(void *vmi, int vcpu, int num, 69 uint64_t val); 70typedef int (*vmi_get_desc_t)(void *vmi, int vcpu, int num, 71 struct seg_desc *desc); 72typedef int (*vmi_set_desc_t)(void *vmi, int vcpu, int num, 73 struct seg_desc *desc); 74typedef int (*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval); 75typedef int (*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val); 76typedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max); 77typedef void (*vmi_vmspace_free)(struct vmspace *vmspace); 78typedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu); 79typedef void (*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic); 80 81struct vmm_ops { 82 vmm_init_func_t init; /* module wide initialization */ 83 vmm_cleanup_func_t cleanup; 84 vmm_resume_func_t resume; 85 86 vmi_init_func_t vminit; /* vm-specific initialization */ 87 vmi_run_func_t vmrun; 88 vmi_cleanup_func_t vmcleanup; 89 vmi_get_register_t vmgetreg; 90 vmi_set_register_t vmsetreg; 91 vmi_get_desc_t vmgetdesc; 92 vmi_set_desc_t vmsetdesc; 93 vmi_get_cap_t vmgetcap; 94 vmi_set_cap_t vmsetcap; 95 vmi_vmspace_alloc vmspace_alloc; 96 vmi_vmspace_free vmspace_free; 97 vmi_vlapic_init vlapic_init; 98 vmi_vlapic_cleanup vlapic_cleanup; 99}; 100 101extern struct vmm_ops vmm_ops_intel; 102extern struct vmm_ops vmm_ops_amd; 103 104int vm_create(const char *name, struct vm **retvm); 105void vm_destroy(struct vm *vm); 106const char *vm_name(struct vm *vm); 107int vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len); 108int vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa); 109int vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len); 110void *vm_gpa_hold(struct vm *, vm_paddr_t gpa, size_t len, int prot, 111 void **cookie); 112void vm_gpa_release(void *cookie); 113int vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, 114 struct vm_memory_segment *seg); 115int vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len, 116 vm_offset_t *offset, struct vm_object **object); 117boolean_t vm_mem_allocated(struct vm *vm, vm_paddr_t gpa); 118int vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval); 119int vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val); 120int vm_get_seg_desc(struct vm *vm, int vcpu, int reg, 121 struct seg_desc *ret_desc); 122int vm_set_seg_desc(struct vm *vm, int vcpu, int reg, 123 struct seg_desc *desc); 124int vm_run(struct vm *vm, struct vm_run *vmrun); 125int vm_suspend(struct vm *vm, enum vm_suspend_how how); 126int vm_inject_nmi(struct vm *vm, int vcpu); 127int vm_nmi_pending(struct vm *vm, int vcpuid); 128void vm_nmi_clear(struct vm *vm, int vcpuid); 129int vm_inject_extint(struct vm *vm, int vcpu); 130int vm_extint_pending(struct vm *vm, int vcpuid); 131void vm_extint_clear(struct vm *vm, int vcpuid); 132uint64_t *vm_guest_msrs(struct vm *vm, int cpu); 133struct vlapic *vm_lapic(struct vm *vm, int cpu); 134struct vioapic *vm_ioapic(struct vm *vm); 135struct vhpet *vm_hpet(struct vm *vm); 136int vm_get_capability(struct vm *vm, int vcpu, int type, int *val); 137int vm_set_capability(struct vm *vm, int vcpu, int type, int val); 138int vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state); 139int vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state); 140int vm_apicid2vcpuid(struct vm *vm, int apicid); 141void vm_activate_cpu(struct vm *vm, int vcpu); 142cpuset_t vm_active_cpus(struct vm *vm); 143struct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid); 144void vm_exit_suspended(struct vm *vm, int vcpuid, uint64_t rip); 145 146/* 147 * Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'. 148 * The rendezvous 'func(arg)' is not allowed to do anything that will 149 * cause the thread to be put to sleep. 150 * 151 * If the rendezvous is being initiated from a vcpu context then the 152 * 'vcpuid' must refer to that vcpu, otherwise it should be set to -1. 153 * 154 * The caller cannot hold any locks when initiating the rendezvous. 155 * 156 * The implementation of this API may cause vcpus other than those specified 157 * by 'dest' to be stalled. The caller should not rely on any vcpus making 158 * forward progress when the rendezvous is in progress. 159 */ 160typedef void (*vm_rendezvous_func_t)(struct vm *vm, int vcpuid, void *arg); 161void vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest, 162 vm_rendezvous_func_t func, void *arg); 163 164static __inline int 165vcpu_rendezvous_pending(void *rendezvous_cookie) 166{ 167 168 return (*(uintptr_t *)rendezvous_cookie != 0); 169} 170 171static __inline int 172vcpu_suspended(void *suspend_cookie) 173{ 174 175 return (*(int *)suspend_cookie); 176} 177 178/* 179 * Return 1 if device indicated by bus/slot/func is supposed to be a 180 * pci passthrough device. 181 * 182 * Return 0 otherwise. 183 */ 184int vmm_is_pptdev(int bus, int slot, int func); 185 186void *vm_iommu_domain(struct vm *vm); 187 188enum vcpu_state { 189 VCPU_IDLE, 190 VCPU_FROZEN, 191 VCPU_RUNNING, 192 VCPU_SLEEPING, 193}; 194 195int vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state, 196 bool from_idle); 197enum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu); 198 199static int __inline 200vcpu_is_running(struct vm *vm, int vcpu, int *hostcpu) 201{ 202 return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING); 203} 204 205void *vcpu_stats(struct vm *vm, int vcpu); 206void vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr); 207struct vmspace *vm_get_vmspace(struct vm *vm); 208int vm_assign_pptdev(struct vm *vm, int bus, int slot, int func); 209int vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func); 210struct vatpic *vm_atpic(struct vm *vm); 211struct vatpit *vm_atpit(struct vm *vm); 212 213/* 214 * Inject exception 'vme' into the guest vcpu. This function returns 0 on 215 * success and non-zero on failure. 216 * 217 * Wrapper functions like 'vm_inject_gp()' should be preferred to calling 218 * this function directly because they enforce the trap-like or fault-like 219 * behavior of an exception. 220 * 221 * This function should only be called in the context of the thread that is 222 * executing this vcpu. 223 */ 224int vm_inject_exception(struct vm *vm, int vcpuid, struct vm_exception *vme); 225 226/* 227 * Returns 0 if there is no exception pending for this vcpu. Returns 1 if an 228 * exception is pending and also updates 'vme'. The pending exception is 229 * cleared when this function returns. 230 * 231 * This function should only be called in the context of the thread that is 232 * executing this vcpu. 233 */ 234int vm_exception_pending(struct vm *vm, int vcpuid, struct vm_exception *vme); 235 236void vm_inject_gp(struct vm *vm, int vcpuid); /* general protection fault */ 237void vm_inject_ud(struct vm *vm, int vcpuid); /* undefined instruction fault */ 238 239#endif /* KERNEL */ 240 241#include <machine/vmm_instruction_emul.h> 242 243#define VM_MAXCPU 16 /* maximum virtual cpus */ 244 245/* 246 * Identifiers for architecturally defined registers. 247 */ 248enum vm_reg_name { 249 VM_REG_GUEST_RAX, 250 VM_REG_GUEST_RBX, 251 VM_REG_GUEST_RCX, 252 VM_REG_GUEST_RDX, 253 VM_REG_GUEST_RSI, 254 VM_REG_GUEST_RDI, 255 VM_REG_GUEST_RBP, 256 VM_REG_GUEST_R8, 257 VM_REG_GUEST_R9, 258 VM_REG_GUEST_R10, 259 VM_REG_GUEST_R11, 260 VM_REG_GUEST_R12, 261 VM_REG_GUEST_R13, 262 VM_REG_GUEST_R14, 263 VM_REG_GUEST_R15, 264 VM_REG_GUEST_CR0, 265 VM_REG_GUEST_CR3, 266 VM_REG_GUEST_CR4, 267 VM_REG_GUEST_DR7, 268 VM_REG_GUEST_RSP, 269 VM_REG_GUEST_RIP, 270 VM_REG_GUEST_RFLAGS, 271 VM_REG_GUEST_ES, 272 VM_REG_GUEST_CS, 273 VM_REG_GUEST_SS, 274 VM_REG_GUEST_DS, 275 VM_REG_GUEST_FS, 276 VM_REG_GUEST_GS, 277 VM_REG_GUEST_LDTR, 278 VM_REG_GUEST_TR, 279 VM_REG_GUEST_IDTR, 280 VM_REG_GUEST_GDTR, 281 VM_REG_GUEST_EFER, 282 VM_REG_LAST 283}; 284 285/* 286 * Identifiers for optional vmm capabilities 287 */ 288enum vm_cap_type { 289 VM_CAP_HALT_EXIT, 290 VM_CAP_MTRAP_EXIT, 291 VM_CAP_PAUSE_EXIT, 292 VM_CAP_UNRESTRICTED_GUEST, 293 VM_CAP_ENABLE_INVPCID, 294 VM_CAP_MAX 295}; 296 297enum x2apic_state { 298 X2APIC_DISABLED, 299 X2APIC_ENABLED, 300 X2APIC_STATE_LAST 301}; 302 303/* 304 * The 'access' field has the format specified in Table 21-2 of the Intel 305 * Architecture Manual vol 3b. 306 * 307 * XXX The contents of the 'access' field are architecturally defined except 308 * bit 16 - Segment Unusable. 309 */ 310struct seg_desc { 311 uint64_t base; 312 uint32_t limit; 313 uint32_t access; 314}; 315 316enum vm_exitcode { 317 VM_EXITCODE_INOUT, 318 VM_EXITCODE_VMX, 319 VM_EXITCODE_BOGUS, 320 VM_EXITCODE_RDMSR, 321 VM_EXITCODE_WRMSR, 322 VM_EXITCODE_HLT, 323 VM_EXITCODE_MTRAP, 324 VM_EXITCODE_PAUSE, 325 VM_EXITCODE_PAGING, 326 VM_EXITCODE_INST_EMUL, 327 VM_EXITCODE_SPINUP_AP, 328 VM_EXITCODE_SPINDOWN_CPU, 329 VM_EXITCODE_RENDEZVOUS, 330 VM_EXITCODE_IOAPIC_EOI, 331 VM_EXITCODE_SUSPENDED, 332 VM_EXITCODE_MAX 333}; 334 335struct vm_exit { 336 enum vm_exitcode exitcode; 337 int inst_length; /* 0 means unknown */ 338 uint64_t rip; 339 union { 340 struct { 341 uint16_t bytes:3; /* 1 or 2 or 4 */ 342 uint16_t in:1; /* out is 0, in is 1 */ 343 uint16_t string:1; 344 uint16_t rep:1; 345 uint16_t port; 346 uint32_t eax; /* valid for out */ 347 } inout; 348 struct { 349 uint64_t gpa; 350 int fault_type; 351 } paging; 352 struct { 353 uint64_t gpa; 354 uint64_t gla; 355 uint64_t cr3; 356 enum vie_cpu_mode cpu_mode; 357 enum vie_paging_mode paging_mode; 358 struct vie vie; 359 } inst_emul; 360 /* 361 * VMX specific payload. Used when there is no "better" 362 * exitcode to represent the VM-exit. 363 */ 364 struct { 365 int status; /* vmx inst status */ 366 /* 367 * 'exit_reason' and 'exit_qualification' are valid 368 * only if 'status' is zero. 369 */ 370 uint32_t exit_reason; 371 uint64_t exit_qualification; 372 /* 373 * 'inst_error' and 'inst_type' are valid 374 * only if 'status' is non-zero. 375 */ 376 int inst_type; 377 int inst_error; 378 } vmx; 379 struct { 380 uint32_t code; /* ecx value */ 381 uint64_t wval; 382 } msr; 383 struct { 384 int vcpu; 385 uint64_t rip; 386 } spinup_ap; 387 struct { 388 uint64_t rflags; 389 } hlt; 390 struct { 391 int vector; 392 } ioapic_eoi; 393 struct { 394 enum vm_suspend_how how; 395 } suspended; 396 } u; 397}; 398 399#endif /* _VMM_H_ */ 400