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