vmm.c revision 241982
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#include <sys/cdefs.h> 30__FBSDID("$FreeBSD$"); 31 32#include <sys/param.h> 33#include <sys/systm.h> 34#include <sys/kernel.h> 35#include <sys/module.h> 36#include <sys/sysctl.h> 37#include <sys/malloc.h> 38#include <sys/pcpu.h> 39#include <sys/lock.h> 40#include <sys/mutex.h> 41#include <sys/proc.h> 42#include <sys/sched.h> 43#include <sys/smp.h> 44#include <sys/systm.h> 45 46#include <vm/vm.h> 47 48#include <machine/vm.h> 49#include <machine/pcb.h> 50#include <machine/smp.h> 51#include <x86/apicreg.h> 52 53#include <machine/vmm.h> 54#include "vmm_mem.h" 55#include "vmm_util.h" 56#include <machine/vmm_dev.h> 57#include "vlapic.h" 58#include "vmm_msr.h" 59#include "vmm_ipi.h" 60#include "vmm_stat.h" 61 62#include "io/ppt.h" 63#include "io/iommu.h" 64 65struct vlapic; 66 67struct vcpu { 68 int flags; 69 enum vcpu_state state; 70 struct mtx mtx; 71 int pincpu; /* host cpuid this vcpu is bound to */ 72 int hostcpu; /* host cpuid this vcpu last ran on */ 73 uint64_t guest_msrs[VMM_MSR_NUM]; 74 struct vlapic *vlapic; 75 int vcpuid; 76 struct savefpu *guestfpu; /* guest fpu state */ 77 void *stats; 78 struct vm_exit exitinfo; 79 enum x2apic_state x2apic_state; 80 int nmi_pending; 81}; 82#define VCPU_F_PINNED 0x0001 83 84#define VCPU_PINCPU(vm, vcpuid) \ 85 ((vm->vcpu[vcpuid].flags & VCPU_F_PINNED) ? vm->vcpu[vcpuid].pincpu : -1) 86 87#define VCPU_UNPIN(vm, vcpuid) (vm->vcpu[vcpuid].flags &= ~VCPU_F_PINNED) 88 89#define VCPU_PIN(vm, vcpuid, host_cpuid) \ 90do { \ 91 vm->vcpu[vcpuid].flags |= VCPU_F_PINNED; \ 92 vm->vcpu[vcpuid].pincpu = host_cpuid; \ 93} while(0) 94 95#define vcpu_lock_init(v) mtx_init(&((v)->mtx), "vcpu lock", 0, MTX_DEF) 96#define vcpu_lock(v) mtx_lock(&((v)->mtx)) 97#define vcpu_unlock(v) mtx_unlock(&((v)->mtx)) 98 99#define VM_MAX_MEMORY_SEGMENTS 2 100 101struct vm { 102 void *cookie; /* processor-specific data */ 103 void *iommu; /* iommu-specific data */ 104 struct vcpu vcpu[VM_MAXCPU]; 105 int num_mem_segs; 106 struct vm_memory_segment mem_segs[VM_MAX_MEMORY_SEGMENTS]; 107 char name[VM_MAX_NAMELEN]; 108 109 /* 110 * Set of active vcpus. 111 * An active vcpu is one that has been started implicitly (BSP) or 112 * explicitly (AP) by sending it a startup ipi. 113 */ 114 cpuset_t active_cpus; 115}; 116 117static struct vmm_ops *ops; 118#define VMM_INIT() (ops != NULL ? (*ops->init)() : 0) 119#define VMM_CLEANUP() (ops != NULL ? (*ops->cleanup)() : 0) 120 121#define VMINIT(vm) (ops != NULL ? (*ops->vminit)(vm): NULL) 122#define VMRUN(vmi, vcpu, rip) \ 123 (ops != NULL ? (*ops->vmrun)(vmi, vcpu, rip) : ENXIO) 124#define VMCLEANUP(vmi) (ops != NULL ? (*ops->vmcleanup)(vmi) : NULL) 125#define VMMMAP_SET(vmi, gpa, hpa, len, attr, prot, spm) \ 126 (ops != NULL ? \ 127 (*ops->vmmmap_set)(vmi, gpa, hpa, len, attr, prot, spm) : \ 128 ENXIO) 129#define VMMMAP_GET(vmi, gpa) \ 130 (ops != NULL ? (*ops->vmmmap_get)(vmi, gpa) : ENXIO) 131#define VMGETREG(vmi, vcpu, num, retval) \ 132 (ops != NULL ? (*ops->vmgetreg)(vmi, vcpu, num, retval) : ENXIO) 133#define VMSETREG(vmi, vcpu, num, val) \ 134 (ops != NULL ? (*ops->vmsetreg)(vmi, vcpu, num, val) : ENXIO) 135#define VMGETDESC(vmi, vcpu, num, desc) \ 136 (ops != NULL ? (*ops->vmgetdesc)(vmi, vcpu, num, desc) : ENXIO) 137#define VMSETDESC(vmi, vcpu, num, desc) \ 138 (ops != NULL ? (*ops->vmsetdesc)(vmi, vcpu, num, desc) : ENXIO) 139#define VMINJECT(vmi, vcpu, type, vec, ec, ecv) \ 140 (ops != NULL ? (*ops->vminject)(vmi, vcpu, type, vec, ec, ecv) : ENXIO) 141#define VMGETCAP(vmi, vcpu, num, retval) \ 142 (ops != NULL ? (*ops->vmgetcap)(vmi, vcpu, num, retval) : ENXIO) 143#define VMSETCAP(vmi, vcpu, num, val) \ 144 (ops != NULL ? (*ops->vmsetcap)(vmi, vcpu, num, val) : ENXIO) 145 146#define fpu_start_emulating() start_emulating() 147#define fpu_stop_emulating() stop_emulating() 148 149static MALLOC_DEFINE(M_VM, "vm", "vm"); 150CTASSERT(VMM_MSR_NUM <= 64); /* msr_mask can keep track of up to 64 msrs */ 151 152/* statistics */ 153static VMM_STAT_DEFINE(VCPU_TOTAL_RUNTIME, "vcpu total runtime"); 154 155static void 156vcpu_cleanup(struct vcpu *vcpu) 157{ 158 vlapic_cleanup(vcpu->vlapic); 159 vmm_stat_free(vcpu->stats); 160 fpu_save_area_free(vcpu->guestfpu); 161} 162 163static void 164vcpu_init(struct vm *vm, uint32_t vcpu_id) 165{ 166 struct vcpu *vcpu; 167 168 vcpu = &vm->vcpu[vcpu_id]; 169 170 vcpu_lock_init(vcpu); 171 vcpu->hostcpu = NOCPU; 172 vcpu->vcpuid = vcpu_id; 173 vcpu->vlapic = vlapic_init(vm, vcpu_id); 174 vm_set_x2apic_state(vm, vcpu_id, X2APIC_ENABLED); 175 vcpu->guestfpu = fpu_save_area_alloc(); 176 fpu_save_area_reset(vcpu->guestfpu); 177 vcpu->stats = vmm_stat_alloc(); 178} 179 180struct vm_exit * 181vm_exitinfo(struct vm *vm, int cpuid) 182{ 183 struct vcpu *vcpu; 184 185 if (cpuid < 0 || cpuid >= VM_MAXCPU) 186 panic("vm_exitinfo: invalid cpuid %d", cpuid); 187 188 vcpu = &vm->vcpu[cpuid]; 189 190 return (&vcpu->exitinfo); 191} 192 193static int 194vmm_init(void) 195{ 196 int error; 197 198 vmm_ipi_init(); 199 200 error = vmm_mem_init(); 201 if (error) 202 return (error); 203 204 if (vmm_is_intel()) 205 ops = &vmm_ops_intel; 206 else if (vmm_is_amd()) 207 ops = &vmm_ops_amd; 208 else 209 return (ENXIO); 210 211 vmm_msr_init(); 212 213 return (VMM_INIT()); 214} 215 216static int 217vmm_handler(module_t mod, int what, void *arg) 218{ 219 int error; 220 221 switch (what) { 222 case MOD_LOAD: 223 vmmdev_init(); 224 iommu_init(); 225 error = vmm_init(); 226 break; 227 case MOD_UNLOAD: 228 error = vmmdev_cleanup(); 229 if (error == 0) { 230 iommu_cleanup(); 231 vmm_ipi_cleanup(); 232 error = VMM_CLEANUP(); 233 } 234 break; 235 default: 236 error = 0; 237 break; 238 } 239 return (error); 240} 241 242static moduledata_t vmm_kmod = { 243 "vmm", 244 vmm_handler, 245 NULL 246}; 247 248/* 249 * Execute the module load handler after the pci passthru driver has had 250 * a chance to claim devices. We need this information at the time we do 251 * iommu initialization. 252 */ 253DECLARE_MODULE(vmm, vmm_kmod, SI_SUB_CONFIGURE + 1, SI_ORDER_ANY); 254MODULE_VERSION(vmm, 1); 255 256SYSCTL_NODE(_hw, OID_AUTO, vmm, CTLFLAG_RW, NULL, NULL); 257 258struct vm * 259vm_create(const char *name) 260{ 261 int i; 262 struct vm *vm; 263 vm_paddr_t maxaddr; 264 265 const int BSP = 0; 266 267 if (name == NULL || strlen(name) >= VM_MAX_NAMELEN) 268 return (NULL); 269 270 vm = malloc(sizeof(struct vm), M_VM, M_WAITOK | M_ZERO); 271 strcpy(vm->name, name); 272 vm->cookie = VMINIT(vm); 273 274 for (i = 0; i < VM_MAXCPU; i++) { 275 vcpu_init(vm, i); 276 guest_msrs_init(vm, i); 277 } 278 279 maxaddr = vmm_mem_maxaddr(); 280 vm->iommu = iommu_create_domain(maxaddr); 281 vm_activate_cpu(vm, BSP); 282 283 return (vm); 284} 285 286static void 287vm_free_mem_seg(struct vm *vm, struct vm_memory_segment *seg) 288{ 289 size_t len; 290 vm_paddr_t hpa; 291 void *host_domain; 292 293 host_domain = iommu_host_domain(); 294 295 len = 0; 296 while (len < seg->len) { 297 hpa = vm_gpa2hpa(vm, seg->gpa + len, PAGE_SIZE); 298 if (hpa == (vm_paddr_t)-1) { 299 panic("vm_free_mem_segs: cannot free hpa " 300 "associated with gpa 0x%016lx", seg->gpa + len); 301 } 302 303 /* 304 * Remove the 'gpa' to 'hpa' mapping in VMs domain. 305 * And resurrect the 1:1 mapping for 'hpa' in 'host_domain'. 306 */ 307 iommu_remove_mapping(vm->iommu, seg->gpa + len, PAGE_SIZE); 308 iommu_create_mapping(host_domain, hpa, hpa, PAGE_SIZE); 309 310 vmm_mem_free(hpa, PAGE_SIZE); 311 312 len += PAGE_SIZE; 313 } 314 315 /* 316 * Invalidate cached translations associated with 'vm->iommu' since 317 * we have now moved some pages from it. 318 */ 319 iommu_invalidate_tlb(vm->iommu); 320 321 bzero(seg, sizeof(struct vm_memory_segment)); 322} 323 324void 325vm_destroy(struct vm *vm) 326{ 327 int i; 328 329 ppt_unassign_all(vm); 330 331 for (i = 0; i < vm->num_mem_segs; i++) 332 vm_free_mem_seg(vm, &vm->mem_segs[i]); 333 334 vm->num_mem_segs = 0; 335 336 for (i = 0; i < VM_MAXCPU; i++) 337 vcpu_cleanup(&vm->vcpu[i]); 338 339 iommu_destroy_domain(vm->iommu); 340 341 VMCLEANUP(vm->cookie); 342 343 free(vm, M_VM); 344} 345 346const char * 347vm_name(struct vm *vm) 348{ 349 return (vm->name); 350} 351 352int 353vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa) 354{ 355 const boolean_t spok = TRUE; /* superpage mappings are ok */ 356 357 return (VMMMAP_SET(vm->cookie, gpa, hpa, len, VM_MEMATTR_UNCACHEABLE, 358 VM_PROT_RW, spok)); 359} 360 361int 362vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len) 363{ 364 const boolean_t spok = TRUE; /* superpage mappings are ok */ 365 366 return (VMMMAP_SET(vm->cookie, gpa, 0, len, 0, 367 VM_PROT_NONE, spok)); 368} 369 370/* 371 * Returns TRUE if 'gpa' is available for allocation and FALSE otherwise 372 */ 373static boolean_t 374vm_gpa_available(struct vm *vm, vm_paddr_t gpa) 375{ 376 int i; 377 vm_paddr_t gpabase, gpalimit; 378 379 if (gpa & PAGE_MASK) 380 panic("vm_gpa_available: gpa (0x%016lx) not page aligned", gpa); 381 382 for (i = 0; i < vm->num_mem_segs; i++) { 383 gpabase = vm->mem_segs[i].gpa; 384 gpalimit = gpabase + vm->mem_segs[i].len; 385 if (gpa >= gpabase && gpa < gpalimit) 386 return (FALSE); 387 } 388 389 return (TRUE); 390} 391 392int 393vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len) 394{ 395 int error, available, allocated; 396 struct vm_memory_segment *seg; 397 vm_paddr_t g, hpa; 398 void *host_domain; 399 400 const boolean_t spok = TRUE; /* superpage mappings are ok */ 401 402 if ((gpa & PAGE_MASK) || (len & PAGE_MASK) || len == 0) 403 return (EINVAL); 404 405 available = allocated = 0; 406 g = gpa; 407 while (g < gpa + len) { 408 if (vm_gpa_available(vm, g)) 409 available++; 410 else 411 allocated++; 412 413 g += PAGE_SIZE; 414 } 415 416 /* 417 * If there are some allocated and some available pages in the address 418 * range then it is an error. 419 */ 420 if (allocated && available) 421 return (EINVAL); 422 423 /* 424 * If the entire address range being requested has already been 425 * allocated then there isn't anything more to do. 426 */ 427 if (allocated && available == 0) 428 return (0); 429 430 if (vm->num_mem_segs >= VM_MAX_MEMORY_SEGMENTS) 431 return (E2BIG); 432 433 host_domain = iommu_host_domain(); 434 435 seg = &vm->mem_segs[vm->num_mem_segs]; 436 437 error = 0; 438 seg->gpa = gpa; 439 seg->len = 0; 440 while (seg->len < len) { 441 hpa = vmm_mem_alloc(PAGE_SIZE); 442 if (hpa == 0) { 443 error = ENOMEM; 444 break; 445 } 446 447 error = VMMMAP_SET(vm->cookie, gpa + seg->len, hpa, PAGE_SIZE, 448 VM_MEMATTR_WRITE_BACK, VM_PROT_ALL, spok); 449 if (error) 450 break; 451 452 /* 453 * Remove the 1:1 mapping for 'hpa' from the 'host_domain'. 454 * Add mapping for 'gpa + seg->len' to 'hpa' in the VMs domain. 455 */ 456 iommu_remove_mapping(host_domain, hpa, PAGE_SIZE); 457 iommu_create_mapping(vm->iommu, gpa + seg->len, hpa, PAGE_SIZE); 458 459 seg->len += PAGE_SIZE; 460 } 461 462 if (error) { 463 vm_free_mem_seg(vm, seg); 464 return (error); 465 } 466 467 /* 468 * Invalidate cached translations associated with 'host_domain' since 469 * we have now moved some pages from it. 470 */ 471 iommu_invalidate_tlb(host_domain); 472 473 vm->num_mem_segs++; 474 475 return (0); 476} 477 478vm_paddr_t 479vm_gpa2hpa(struct vm *vm, vm_paddr_t gpa, size_t len) 480{ 481 vm_paddr_t nextpage; 482 483 nextpage = rounddown(gpa + PAGE_SIZE, PAGE_SIZE); 484 if (len > nextpage - gpa) 485 panic("vm_gpa2hpa: invalid gpa/len: 0x%016lx/%lu", gpa, len); 486 487 return (VMMMAP_GET(vm->cookie, gpa)); 488} 489 490int 491vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, 492 struct vm_memory_segment *seg) 493{ 494 int i; 495 496 for (i = 0; i < vm->num_mem_segs; i++) { 497 if (gpabase == vm->mem_segs[i].gpa) { 498 *seg = vm->mem_segs[i]; 499 return (0); 500 } 501 } 502 return (-1); 503} 504 505int 506vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval) 507{ 508 509 if (vcpu < 0 || vcpu >= VM_MAXCPU) 510 return (EINVAL); 511 512 if (reg >= VM_REG_LAST) 513 return (EINVAL); 514 515 return (VMGETREG(vm->cookie, vcpu, reg, retval)); 516} 517 518int 519vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val) 520{ 521 522 if (vcpu < 0 || vcpu >= VM_MAXCPU) 523 return (EINVAL); 524 525 if (reg >= VM_REG_LAST) 526 return (EINVAL); 527 528 return (VMSETREG(vm->cookie, vcpu, reg, val)); 529} 530 531static boolean_t 532is_descriptor_table(int reg) 533{ 534 535 switch (reg) { 536 case VM_REG_GUEST_IDTR: 537 case VM_REG_GUEST_GDTR: 538 return (TRUE); 539 default: 540 return (FALSE); 541 } 542} 543 544static boolean_t 545is_segment_register(int reg) 546{ 547 548 switch (reg) { 549 case VM_REG_GUEST_ES: 550 case VM_REG_GUEST_CS: 551 case VM_REG_GUEST_SS: 552 case VM_REG_GUEST_DS: 553 case VM_REG_GUEST_FS: 554 case VM_REG_GUEST_GS: 555 case VM_REG_GUEST_TR: 556 case VM_REG_GUEST_LDTR: 557 return (TRUE); 558 default: 559 return (FALSE); 560 } 561} 562 563int 564vm_get_seg_desc(struct vm *vm, int vcpu, int reg, 565 struct seg_desc *desc) 566{ 567 568 if (vcpu < 0 || vcpu >= VM_MAXCPU) 569 return (EINVAL); 570 571 if (!is_segment_register(reg) && !is_descriptor_table(reg)) 572 return (EINVAL); 573 574 return (VMGETDESC(vm->cookie, vcpu, reg, desc)); 575} 576 577int 578vm_set_seg_desc(struct vm *vm, int vcpu, int reg, 579 struct seg_desc *desc) 580{ 581 if (vcpu < 0 || vcpu >= VM_MAXCPU) 582 return (EINVAL); 583 584 if (!is_segment_register(reg) && !is_descriptor_table(reg)) 585 return (EINVAL); 586 587 return (VMSETDESC(vm->cookie, vcpu, reg, desc)); 588} 589 590int 591vm_get_pinning(struct vm *vm, int vcpuid, int *cpuid) 592{ 593 594 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 595 return (EINVAL); 596 597 *cpuid = VCPU_PINCPU(vm, vcpuid); 598 599 return (0); 600} 601 602int 603vm_set_pinning(struct vm *vm, int vcpuid, int host_cpuid) 604{ 605 struct thread *td; 606 607 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 608 return (EINVAL); 609 610 td = curthread; /* XXXSMP only safe when muxing vcpus */ 611 612 /* unpin */ 613 if (host_cpuid < 0) { 614 VCPU_UNPIN(vm, vcpuid); 615 thread_lock(td); 616 sched_unbind(td); 617 thread_unlock(td); 618 return (0); 619 } 620 621 if (CPU_ABSENT(host_cpuid)) 622 return (EINVAL); 623 624 /* 625 * XXX we should check that 'host_cpuid' has not already been pinned 626 * by another vm. 627 */ 628 thread_lock(td); 629 sched_bind(td, host_cpuid); 630 thread_unlock(td); 631 VCPU_PIN(vm, vcpuid, host_cpuid); 632 633 return (0); 634} 635 636static void 637restore_guest_fpustate(struct vcpu *vcpu) 638{ 639 640 /* flush host state to the pcb */ 641 fpuexit(curthread); 642 fpu_stop_emulating(); 643 fpurestore(vcpu->guestfpu); 644} 645 646static void 647save_guest_fpustate(struct vcpu *vcpu) 648{ 649 650 fpusave(vcpu->guestfpu); 651 fpu_start_emulating(); 652} 653 654int 655vm_run(struct vm *vm, struct vm_run *vmrun) 656{ 657 int error, vcpuid; 658 struct vcpu *vcpu; 659 struct pcb *pcb; 660 uint64_t tscval; 661 662 vcpuid = vmrun->cpuid; 663 664 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 665 return (EINVAL); 666 667 vcpu = &vm->vcpu[vcpuid]; 668 669 critical_enter(); 670 671 tscval = rdtsc(); 672 673 pcb = PCPU_GET(curpcb); 674 set_pcb_flags(pcb, PCB_FULL_IRET); 675 676 restore_guest_msrs(vm, vcpuid); 677 restore_guest_fpustate(vcpu); 678 679 vcpu->hostcpu = curcpu; 680 error = VMRUN(vm->cookie, vcpuid, vmrun->rip); 681 vcpu->hostcpu = NOCPU; 682 683 save_guest_fpustate(vcpu); 684 restore_host_msrs(vm, vcpuid); 685 686 vmm_stat_incr(vm, vcpuid, VCPU_TOTAL_RUNTIME, rdtsc() - tscval); 687 688 /* copy the exit information */ 689 bcopy(&vcpu->exitinfo, &vmrun->vm_exit, sizeof(struct vm_exit)); 690 691 critical_exit(); 692 693 return (error); 694} 695 696int 697vm_inject_event(struct vm *vm, int vcpuid, int type, 698 int vector, uint32_t code, int code_valid) 699{ 700 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 701 return (EINVAL); 702 703 if ((type > VM_EVENT_NONE && type < VM_EVENT_MAX) == 0) 704 return (EINVAL); 705 706 if (vector < 0 || vector > 255) 707 return (EINVAL); 708 709 return (VMINJECT(vm->cookie, vcpuid, type, vector, code, code_valid)); 710} 711 712VMM_STAT_DEFINE(VCPU_NMI_COUNT, "number of NMIs delivered to vcpu"); 713 714int 715vm_inject_nmi(struct vm *vm, int vcpuid) 716{ 717 struct vcpu *vcpu; 718 719 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 720 return (EINVAL); 721 722 vcpu = &vm->vcpu[vcpuid]; 723 724 vcpu->nmi_pending = 1; 725 vm_interrupt_hostcpu(vm, vcpuid); 726 return (0); 727} 728 729int 730vm_nmi_pending(struct vm *vm, int vcpuid) 731{ 732 struct vcpu *vcpu; 733 734 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 735 panic("vm_nmi_pending: invalid vcpuid %d", vcpuid); 736 737 vcpu = &vm->vcpu[vcpuid]; 738 739 return (vcpu->nmi_pending); 740} 741 742void 743vm_nmi_clear(struct vm *vm, int vcpuid) 744{ 745 struct vcpu *vcpu; 746 747 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 748 panic("vm_nmi_pending: invalid vcpuid %d", vcpuid); 749 750 vcpu = &vm->vcpu[vcpuid]; 751 752 if (vcpu->nmi_pending == 0) 753 panic("vm_nmi_clear: inconsistent nmi_pending state"); 754 755 vcpu->nmi_pending = 0; 756 vmm_stat_incr(vm, vcpuid, VCPU_NMI_COUNT, 1); 757} 758 759int 760vm_get_capability(struct vm *vm, int vcpu, int type, int *retval) 761{ 762 if (vcpu < 0 || vcpu >= VM_MAXCPU) 763 return (EINVAL); 764 765 if (type < 0 || type >= VM_CAP_MAX) 766 return (EINVAL); 767 768 return (VMGETCAP(vm->cookie, vcpu, type, retval)); 769} 770 771int 772vm_set_capability(struct vm *vm, int vcpu, int type, int val) 773{ 774 if (vcpu < 0 || vcpu >= VM_MAXCPU) 775 return (EINVAL); 776 777 if (type < 0 || type >= VM_CAP_MAX) 778 return (EINVAL); 779 780 return (VMSETCAP(vm->cookie, vcpu, type, val)); 781} 782 783uint64_t * 784vm_guest_msrs(struct vm *vm, int cpu) 785{ 786 return (vm->vcpu[cpu].guest_msrs); 787} 788 789struct vlapic * 790vm_lapic(struct vm *vm, int cpu) 791{ 792 return (vm->vcpu[cpu].vlapic); 793} 794 795boolean_t 796vmm_is_pptdev(int bus, int slot, int func) 797{ 798 int found, b, s, f, n; 799 char *val, *cp, *cp2; 800 801 /* 802 * setenv pptdevs "1/2/3 4/5/6 7/8/9 10/11/12" 803 */ 804 found = 0; 805 cp = val = getenv("pptdevs"); 806 while (cp != NULL && *cp != '\0') { 807 if ((cp2 = strchr(cp, ' ')) != NULL) 808 *cp2 = '\0'; 809 810 n = sscanf(cp, "%d/%d/%d", &b, &s, &f); 811 if (n == 3 && bus == b && slot == s && func == f) { 812 found = 1; 813 break; 814 } 815 816 if (cp2 != NULL) 817 *cp2++ = ' '; 818 819 cp = cp2; 820 } 821 freeenv(val); 822 return (found); 823} 824 825void * 826vm_iommu_domain(struct vm *vm) 827{ 828 829 return (vm->iommu); 830} 831 832int 833vcpu_set_state(struct vm *vm, int vcpuid, enum vcpu_state state) 834{ 835 int error; 836 struct vcpu *vcpu; 837 838 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 839 panic("vm_set_run_state: invalid vcpuid %d", vcpuid); 840 841 vcpu = &vm->vcpu[vcpuid]; 842 843 vcpu_lock(vcpu); 844 845 /* 846 * The following state transitions are allowed: 847 * IDLE -> RUNNING -> IDLE 848 * IDLE -> CANNOT_RUN -> IDLE 849 */ 850 if ((vcpu->state == VCPU_IDLE && state != VCPU_IDLE) || 851 (vcpu->state != VCPU_IDLE && state == VCPU_IDLE)) { 852 error = 0; 853 vcpu->state = state; 854 } else { 855 error = EBUSY; 856 } 857 858 vcpu_unlock(vcpu); 859 860 return (error); 861} 862 863enum vcpu_state 864vcpu_get_state(struct vm *vm, int vcpuid) 865{ 866 struct vcpu *vcpu; 867 enum vcpu_state state; 868 869 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 870 panic("vm_get_run_state: invalid vcpuid %d", vcpuid); 871 872 vcpu = &vm->vcpu[vcpuid]; 873 874 vcpu_lock(vcpu); 875 state = vcpu->state; 876 vcpu_unlock(vcpu); 877 878 return (state); 879} 880 881void 882vm_activate_cpu(struct vm *vm, int vcpuid) 883{ 884 885 if (vcpuid >= 0 && vcpuid < VM_MAXCPU) 886 CPU_SET(vcpuid, &vm->active_cpus); 887} 888 889cpuset_t 890vm_active_cpus(struct vm *vm) 891{ 892 893 return (vm->active_cpus); 894} 895 896void * 897vcpu_stats(struct vm *vm, int vcpuid) 898{ 899 900 return (vm->vcpu[vcpuid].stats); 901} 902 903int 904vm_get_x2apic_state(struct vm *vm, int vcpuid, enum x2apic_state *state) 905{ 906 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 907 return (EINVAL); 908 909 *state = vm->vcpu[vcpuid].x2apic_state; 910 911 return (0); 912} 913 914int 915vm_set_x2apic_state(struct vm *vm, int vcpuid, enum x2apic_state state) 916{ 917 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 918 return (EINVAL); 919 920 if (state < 0 || state >= X2APIC_STATE_LAST) 921 return (EINVAL); 922 923 vm->vcpu[vcpuid].x2apic_state = state; 924 925 vlapic_set_x2apic_state(vm, vcpuid, state); 926 927 return (0); 928} 929 930void 931vm_interrupt_hostcpu(struct vm *vm, int vcpuid) 932{ 933 int hostcpu; 934 struct vcpu *vcpu; 935 936 vcpu = &vm->vcpu[vcpuid]; 937 938 /* 939 * XXX racy but the worst case is that we'll send an unnecessary IPI 940 * to the 'hostcpu'. 941 * 942 * We cannot use vcpu_is_running() here because it acquires vcpu->mtx 943 * which is not allowed inside a critical section. 944 */ 945 hostcpu = vcpu->hostcpu; 946 if (hostcpu == NOCPU || hostcpu == curcpu) 947 return; 948 949 ipi_cpu(hostcpu, vmm_ipinum); 950} 951