vmm.h revision 267447
1187938Semax/*- 2187938Semax * Copyright (c) 2011 NetApp, Inc. 3187938Semax * All rights reserved. 4187938Semax * 5187938Semax * Redistribution and use in source and binary forms, with or without 6187938Semax * modification, are permitted provided that the following conditions 7187938Semax * are met: 8187938Semax * 1. Redistributions of source code must retain the above copyright 9187938Semax * notice, this list of conditions and the following disclaimer. 10187938Semax * 2. Redistributions in binary form must reproduce the above copyright 11187938Semax * notice, this list of conditions and the following disclaimer in the 12187938Semax * documentation and/or other materials provided with the distribution. 13187938Semax * 14187938Semax * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 15187938Semax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16187938Semax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17187938Semax * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 18187938Semax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19187938Semax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20187938Semax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21187938Semax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22187938Semax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23187938Semax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24187938Semax * SUCH DAMAGE. 25187938Semax * 26187938Semax * $FreeBSD: stable/10/sys/amd64/include/vmm.h 267447 2014-06-13 19:10:40Z jhb $ 27187938Semax */ 28187938Semax 29187938Semax#ifndef _VMM_H_ 30187938Semax#define _VMM_H_ 31187938Semax 32187938Semax#ifdef _KERNEL 33187938Semax 34187938Semax#define VM_MAX_NAMELEN 32 35187938Semax 36187938Semaxstruct vm; 37187938Semaxstruct vm_exception; 38187938Semaxstruct vm_memory_segment; 39187938Semaxstruct seg_desc; 40187938Semaxstruct vm_exit; 41187938Semaxstruct vm_run; 42187938Semaxstruct vhpet; 43187938Semaxstruct vioapic; 44187938Semaxstruct vlapic; 45187938Semaxstruct vmspace; 46187938Semaxstruct vm_object; 47187938Semaxstruct pmap; 48187938Semax 49187938Semaxenum x2apic_state; 50187938Semax 51187938Semaxtypedef int (*vmm_init_func_t)(int ipinum); 52187938Semaxtypedef int (*vmm_cleanup_func_t)(void); 53187938Semaxtypedef void (*vmm_resume_func_t)(void); 54187938Semaxtypedef void * (*vmi_init_func_t)(struct vm *vm, struct pmap *pmap); 55187938Semaxtypedef int (*vmi_run_func_t)(void *vmi, int vcpu, register_t rip, 56187938Semax struct pmap *pmap, void *rendezvous_cookie); 57187938Semaxtypedef void (*vmi_cleanup_func_t)(void *vmi); 58187938Semaxtypedef int (*vmi_get_register_t)(void *vmi, int vcpu, int num, 59187938Semax uint64_t *retval); 60187938Semaxtypedef int (*vmi_set_register_t)(void *vmi, int vcpu, int num, 61187938Semax uint64_t val); 62187938Semaxtypedef int (*vmi_get_desc_t)(void *vmi, int vcpu, int num, 63187938Semax struct seg_desc *desc); 64187938Semaxtypedef int (*vmi_set_desc_t)(void *vmi, int vcpu, int num, 65187938Semax struct seg_desc *desc); 66187938Semaxtypedef int (*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval); 67187938Semaxtypedef int (*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val); 68187938Semaxtypedef struct vmspace * (*vmi_vmspace_alloc)(vm_offset_t min, vm_offset_t max); 69187938Semaxtypedef void (*vmi_vmspace_free)(struct vmspace *vmspace); 70187938Semaxtypedef struct vlapic * (*vmi_vlapic_init)(void *vmi, int vcpu); 71187938Semaxtypedef void (*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic); 72187938Semax 73187938Semaxstruct vmm_ops { 74187938Semax vmm_init_func_t init; /* module wide initialization */ 75187938Semax vmm_cleanup_func_t cleanup; 76187938Semax vmm_resume_func_t resume; 77187938Semax 78187938Semax vmi_init_func_t vminit; /* vm-specific initialization */ 79187938Semax vmi_run_func_t vmrun; 80187938Semax vmi_cleanup_func_t vmcleanup; 81187938Semax vmi_get_register_t vmgetreg; 82187938Semax vmi_set_register_t vmsetreg; 83187938Semax vmi_get_desc_t vmgetdesc; 84187938Semax vmi_set_desc_t vmsetdesc; 85187938Semax vmi_get_cap_t vmgetcap; 86187938Semax vmi_set_cap_t vmsetcap; 87187938Semax vmi_vmspace_alloc vmspace_alloc; 88187938Semax vmi_vmspace_free vmspace_free; 89187938Semax vmi_vlapic_init vlapic_init; 90187938Semax vmi_vlapic_cleanup vlapic_cleanup; 91187938Semax}; 92187938Semax 93187938Semaxextern struct vmm_ops vmm_ops_intel; 94187938Semaxextern struct vmm_ops vmm_ops_amd; 95187938Semax 96187938Semaxint vm_create(const char *name, struct vm **retvm); 97187938Semaxvoid vm_destroy(struct vm *vm); 98187938Semaxconst char *vm_name(struct vm *vm); 99187938Semaxint vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len); 100187938Semaxint vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa); 101187938Semaxint vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len); 102187938Semaxvoid *vm_gpa_hold(struct vm *, vm_paddr_t gpa, size_t len, int prot, 103187938Semax void **cookie); 104187938Semaxvoid vm_gpa_release(void *cookie); 105187938Semaxint vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, 106187938Semax struct vm_memory_segment *seg); 107187938Semaxint vm_get_memobj(struct vm *vm, vm_paddr_t gpa, size_t len, 108187938Semax vm_offset_t *offset, struct vm_object **object); 109187938Semaxboolean_t vm_mem_allocated(struct vm *vm, vm_paddr_t gpa); 110187938Semaxint vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval); 111187938Semaxint vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val); 112187938Semaxint vm_get_seg_desc(struct vm *vm, int vcpu, int reg, 113187938Semax struct seg_desc *ret_desc); 114187938Semaxint vm_set_seg_desc(struct vm *vm, int vcpu, int reg, 115187938Semax struct seg_desc *desc); 116187938Semaxint vm_run(struct vm *vm, struct vm_run *vmrun); 117187938Semaxint vm_inject_nmi(struct vm *vm, int vcpu); 118187938Semaxint vm_nmi_pending(struct vm *vm, int vcpuid); 119187938Semaxvoid vm_nmi_clear(struct vm *vm, int vcpuid); 120187938Semaxuint64_t *vm_guest_msrs(struct vm *vm, int cpu); 121187938Semaxstruct vlapic *vm_lapic(struct vm *vm, int cpu); 122187938Semaxstruct vioapic *vm_ioapic(struct vm *vm); 123187938Semaxstruct vhpet *vm_hpet(struct vm *vm); 124187938Semaxint vm_get_capability(struct vm *vm, int vcpu, int type, int *val); 125187938Semaxint vm_set_capability(struct vm *vm, int vcpu, int type, int val); 126187938Semaxint vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state); 127187938Semaxint vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state); 128187938Semaxint vm_apicid2vcpuid(struct vm *vm, int apicid); 129187938Semaxvoid vm_activate_cpu(struct vm *vm, int vcpu); 130187938Semaxcpuset_t vm_active_cpus(struct vm *vm); 131187938Semaxstruct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid); 132187938Semax 133187938Semax/* 134187938Semax * Rendezvous all vcpus specified in 'dest' and execute 'func(arg)'. 135187938Semax * The rendezvous 'func(arg)' is not allowed to do anything that will 136187938Semax * cause the thread to be put to sleep. 137187938Semax * 138187938Semax * If the rendezvous is being initiated from a vcpu context then the 139187938Semax * 'vcpuid' must refer to that vcpu, otherwise it should be set to -1. 140187938Semax * 141187938Semax * The caller cannot hold any locks when initiating the rendezvous. 142187938Semax * 143187938Semax * The implementation of this API may cause vcpus other than those specified 144187938Semax * by 'dest' to be stalled. The caller should not rely on any vcpus making 145187938Semax * forward progress when the rendezvous is in progress. 146187938Semax */ 147187938Semaxtypedef void (*vm_rendezvous_func_t)(struct vm *vm, int vcpuid, void *arg); 148187938Semaxvoid vm_smp_rendezvous(struct vm *vm, int vcpuid, cpuset_t dest, 149187938Semax vm_rendezvous_func_t func, void *arg); 150187938Semax 151187938Semaxstatic __inline int 152187938Semaxvcpu_rendezvous_pending(void *rendezvous_cookie) 153187938Semax{ 154187938Semax 155187938Semax return (*(uintptr_t *)rendezvous_cookie != 0); 156187938Semax} 157187938Semax 158187938Semax/* 159187938Semax * Return 1 if device indicated by bus/slot/func is supposed to be a 160187938Semax * pci passthrough device. 161187938Semax * 162187938Semax * Return 0 otherwise. 163187938Semax */ 164187938Semaxint vmm_is_pptdev(int bus, int slot, int func); 165187938Semax 166187938Semaxvoid *vm_iommu_domain(struct vm *vm); 167187938Semax 168187938Semaxenum vcpu_state { 169187938Semax VCPU_IDLE, 170187938Semax VCPU_FROZEN, 171187938Semax VCPU_RUNNING, 172187938Semax VCPU_SLEEPING, 173187938Semax}; 174187938Semax 175187938Semaxint vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state, 176187938Semax bool from_idle); 177187938Semaxenum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu); 178187938Semax 179187938Semaxstatic int __inline 180187938Semaxvcpu_is_running(struct vm *vm, int vcpu, int *hostcpu) 181187938Semax{ 182187938Semax return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING); 183187938Semax} 184187938Semax 185187938Semaxvoid *vcpu_stats(struct vm *vm, int vcpu); 186187938Semaxvoid vcpu_notify_event(struct vm *vm, int vcpuid, bool lapic_intr); 187187938Semaxstruct vmspace *vm_get_vmspace(struct vm *vm); 188187938Semaxint vm_assign_pptdev(struct vm *vm, int bus, int slot, int func); 189187938Semaxint vm_unassign_pptdev(struct vm *vm, int bus, int slot, int func); 190187938Semax 191187938Semax/* 192187938Semax * Inject exception 'vme' into the guest vcpu. This function returns 0 on 193 * success and non-zero on failure. 194 * 195 * Wrapper functions like 'vm_inject_gp()' should be preferred to calling 196 * this function directly because they enforce the trap-like or fault-like 197 * behavior of an exception. 198 * 199 * This function should only be called in the context of the thread that is 200 * executing this vcpu. 201 */ 202int vm_inject_exception(struct vm *vm, int vcpuid, struct vm_exception *vme); 203 204/* 205 * Returns 0 if there is no exception pending for this vcpu. Returns 1 if an 206 * exception is pending and also updates 'vme'. The pending exception is 207 * cleared when this function returns. 208 * 209 * This function should only be called in the context of the thread that is 210 * executing this vcpu. 211 */ 212int vm_exception_pending(struct vm *vm, int vcpuid, struct vm_exception *vme); 213 214void vm_inject_gp(struct vm *vm, int vcpuid); /* general protection fault */ 215void vm_inject_ud(struct vm *vm, int vcpuid); /* undefined instruction fault */ 216 217#endif /* KERNEL */ 218 219#include <machine/vmm_instruction_emul.h> 220 221#define VM_MAXCPU 16 /* maximum virtual cpus */ 222 223/* 224 * Identifiers for architecturally defined registers. 225 */ 226enum vm_reg_name { 227 VM_REG_GUEST_RAX, 228 VM_REG_GUEST_RBX, 229 VM_REG_GUEST_RCX, 230 VM_REG_GUEST_RDX, 231 VM_REG_GUEST_RSI, 232 VM_REG_GUEST_RDI, 233 VM_REG_GUEST_RBP, 234 VM_REG_GUEST_R8, 235 VM_REG_GUEST_R9, 236 VM_REG_GUEST_R10, 237 VM_REG_GUEST_R11, 238 VM_REG_GUEST_R12, 239 VM_REG_GUEST_R13, 240 VM_REG_GUEST_R14, 241 VM_REG_GUEST_R15, 242 VM_REG_GUEST_CR0, 243 VM_REG_GUEST_CR3, 244 VM_REG_GUEST_CR4, 245 VM_REG_GUEST_DR7, 246 VM_REG_GUEST_RSP, 247 VM_REG_GUEST_RIP, 248 VM_REG_GUEST_RFLAGS, 249 VM_REG_GUEST_ES, 250 VM_REG_GUEST_CS, 251 VM_REG_GUEST_SS, 252 VM_REG_GUEST_DS, 253 VM_REG_GUEST_FS, 254 VM_REG_GUEST_GS, 255 VM_REG_GUEST_LDTR, 256 VM_REG_GUEST_TR, 257 VM_REG_GUEST_IDTR, 258 VM_REG_GUEST_GDTR, 259 VM_REG_GUEST_EFER, 260 VM_REG_LAST 261}; 262 263/* 264 * Identifiers for optional vmm capabilities 265 */ 266enum vm_cap_type { 267 VM_CAP_HALT_EXIT, 268 VM_CAP_MTRAP_EXIT, 269 VM_CAP_PAUSE_EXIT, 270 VM_CAP_UNRESTRICTED_GUEST, 271 VM_CAP_ENABLE_INVPCID, 272 VM_CAP_MAX 273}; 274 275enum x2apic_state { 276 X2APIC_DISABLED, 277 X2APIC_ENABLED, 278 X2APIC_STATE_LAST 279}; 280 281/* 282 * The 'access' field has the format specified in Table 21-2 of the Intel 283 * Architecture Manual vol 3b. 284 * 285 * XXX The contents of the 'access' field are architecturally defined except 286 * bit 16 - Segment Unusable. 287 */ 288struct seg_desc { 289 uint64_t base; 290 uint32_t limit; 291 uint32_t access; 292}; 293 294enum vm_exitcode { 295 VM_EXITCODE_INOUT, 296 VM_EXITCODE_VMX, 297 VM_EXITCODE_BOGUS, 298 VM_EXITCODE_RDMSR, 299 VM_EXITCODE_WRMSR, 300 VM_EXITCODE_HLT, 301 VM_EXITCODE_MTRAP, 302 VM_EXITCODE_PAUSE, 303 VM_EXITCODE_PAGING, 304 VM_EXITCODE_INST_EMUL, 305 VM_EXITCODE_SPINUP_AP, 306 VM_EXITCODE_SPINDOWN_CPU, 307 VM_EXITCODE_RENDEZVOUS, 308 VM_EXITCODE_IOAPIC_EOI, 309 VM_EXITCODE_MAX 310}; 311 312struct vm_exit { 313 enum vm_exitcode exitcode; 314 int inst_length; /* 0 means unknown */ 315 uint64_t rip; 316 union { 317 struct { 318 uint16_t bytes:3; /* 1 or 2 or 4 */ 319 uint16_t in:1; /* out is 0, in is 1 */ 320 uint16_t string:1; 321 uint16_t rep:1; 322 uint16_t port; 323 uint32_t eax; /* valid for out */ 324 } inout; 325 struct { 326 uint64_t gpa; 327 int fault_type; 328 } paging; 329 struct { 330 uint64_t gpa; 331 uint64_t gla; 332 uint64_t cr3; 333 enum vie_cpu_mode cpu_mode; 334 enum vie_paging_mode paging_mode; 335 struct vie vie; 336 } inst_emul; 337 /* 338 * VMX specific payload. Used when there is no "better" 339 * exitcode to represent the VM-exit. 340 */ 341 struct { 342 int status; /* vmx inst status */ 343 /* 344 * 'exit_reason' and 'exit_qualification' are valid 345 * only if 'status' is zero. 346 */ 347 uint32_t exit_reason; 348 uint64_t exit_qualification; 349 /* 350 * 'inst_error' and 'inst_type' are valid 351 * only if 'status' is non-zero. 352 */ 353 int inst_type; 354 int inst_error; 355 } vmx; 356 struct { 357 uint32_t code; /* ecx value */ 358 uint64_t wval; 359 } msr; 360 struct { 361 int vcpu; 362 uint64_t rip; 363 } spinup_ap; 364 struct { 365 uint64_t rflags; 366 } hlt; 367 struct { 368 int vector; 369 } ioapic_eoi; 370 } u; 371}; 372 373#endif /* _VMM_H_ */ 374