vmm.c revision 241147
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 278void 279vm_destroy(struct vm *vm) 280{ 281 int i; 282 283 ppt_unassign_all(vm); 284 285 for (i = 0; i < vm->num_mem_segs; i++) 286 vmm_mem_free(vm->mem_segs[i].hpa, vm->mem_segs[i].len); 287 288 for (i = 0; i < VM_MAXCPU; i++) 289 vcpu_cleanup(&vm->vcpu[i]); 290 291 iommu_destroy_domain(vm->iommu); 292 293 VMCLEANUP(vm->cookie); 294 295 free(vm, M_VM); 296} 297 298const char * 299vm_name(struct vm *vm) 300{ 301 return (vm->name); 302} 303 304int 305vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa) 306{ 307 const boolean_t spok = TRUE; /* superpage mappings are ok */ 308 309 return (VMMMAP_SET(vm->cookie, gpa, hpa, len, VM_MEMATTR_UNCACHEABLE, 310 VM_PROT_RW, spok)); 311} 312 313int 314vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len) 315{ 316 const boolean_t spok = TRUE; /* superpage mappings are ok */ 317 318 return (VMMMAP_SET(vm->cookie, gpa, 0, len, 0, 319 VM_PROT_NONE, spok)); 320} 321 322/* 323 * Returns TRUE if 'gpa' is available for allocation and FALSE otherwise 324 */ 325static boolean_t 326vm_gpa_available(struct vm *vm, vm_paddr_t gpa) 327{ 328 int i; 329 vm_paddr_t gpabase, gpalimit; 330 331 if (gpa & PAGE_MASK) 332 panic("vm_gpa_available: gpa (0x%016lx) not page aligned", gpa); 333 334 for (i = 0; i < vm->num_mem_segs; i++) { 335 gpabase = vm->mem_segs[i].gpa; 336 gpalimit = gpabase + vm->mem_segs[i].len; 337 if (gpa >= gpabase && gpa < gpalimit) 338 return (FALSE); 339 } 340 341 return (TRUE); 342} 343 344int 345vm_malloc(struct vm *vm, vm_paddr_t gpa, size_t len) 346{ 347 int error, available, allocated; 348 vm_paddr_t g, hpa; 349 350 const boolean_t spok = TRUE; /* superpage mappings are ok */ 351 352 if ((gpa & PAGE_MASK) || (len & PAGE_MASK) || len == 0) 353 return (EINVAL); 354 355 available = allocated = 0; 356 g = gpa; 357 while (g < gpa + len) { 358 if (vm_gpa_available(vm, g)) 359 available++; 360 else 361 allocated++; 362 363 g += PAGE_SIZE; 364 } 365 366 /* 367 * If there are some allocated and some available pages in the address 368 * range then it is an error. 369 */ 370 if (allocated && available) 371 return (EINVAL); 372 373 /* 374 * If the entire address range being requested has already been 375 * allocated then there isn't anything more to do. 376 */ 377 if (allocated && available == 0) 378 return (0); 379 380 if (vm->num_mem_segs >= VM_MAX_MEMORY_SEGMENTS) 381 return (E2BIG); 382 383 hpa = vmm_mem_alloc(len); 384 if (hpa == 0) 385 return (ENOMEM); 386 387 error = VMMMAP_SET(vm->cookie, gpa, hpa, len, VM_MEMATTR_WRITE_BACK, 388 VM_PROT_ALL, spok); 389 if (error) { 390 vmm_mem_free(hpa, len); 391 return (error); 392 } 393 394 iommu_create_mapping(vm->iommu, gpa, hpa, len); 395 396 vm->mem_segs[vm->num_mem_segs].gpa = gpa; 397 vm->mem_segs[vm->num_mem_segs].hpa = hpa; 398 vm->mem_segs[vm->num_mem_segs].len = len; 399 vm->num_mem_segs++; 400 401 return (0); 402} 403 404vm_paddr_t 405vm_gpa2hpa(struct vm *vm, vm_paddr_t gpa, size_t len) 406{ 407 408 return (VMMMAP_GET(vm->cookie, gpa)); 409} 410 411int 412vm_gpabase2memseg(struct vm *vm, vm_paddr_t gpabase, 413 struct vm_memory_segment *seg) 414{ 415 int i; 416 417 for (i = 0; i < vm->num_mem_segs; i++) { 418 if (gpabase == vm->mem_segs[i].gpa) { 419 *seg = vm->mem_segs[i]; 420 return (0); 421 } 422 } 423 return (-1); 424} 425 426int 427vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval) 428{ 429 430 if (vcpu < 0 || vcpu >= VM_MAXCPU) 431 return (EINVAL); 432 433 if (reg >= VM_REG_LAST) 434 return (EINVAL); 435 436 return (VMGETREG(vm->cookie, vcpu, reg, retval)); 437} 438 439int 440vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val) 441{ 442 443 if (vcpu < 0 || vcpu >= VM_MAXCPU) 444 return (EINVAL); 445 446 if (reg >= VM_REG_LAST) 447 return (EINVAL); 448 449 return (VMSETREG(vm->cookie, vcpu, reg, val)); 450} 451 452static boolean_t 453is_descriptor_table(int reg) 454{ 455 456 switch (reg) { 457 case VM_REG_GUEST_IDTR: 458 case VM_REG_GUEST_GDTR: 459 return (TRUE); 460 default: 461 return (FALSE); 462 } 463} 464 465static boolean_t 466is_segment_register(int reg) 467{ 468 469 switch (reg) { 470 case VM_REG_GUEST_ES: 471 case VM_REG_GUEST_CS: 472 case VM_REG_GUEST_SS: 473 case VM_REG_GUEST_DS: 474 case VM_REG_GUEST_FS: 475 case VM_REG_GUEST_GS: 476 case VM_REG_GUEST_TR: 477 case VM_REG_GUEST_LDTR: 478 return (TRUE); 479 default: 480 return (FALSE); 481 } 482} 483 484int 485vm_get_seg_desc(struct vm *vm, int vcpu, int reg, 486 struct seg_desc *desc) 487{ 488 489 if (vcpu < 0 || vcpu >= VM_MAXCPU) 490 return (EINVAL); 491 492 if (!is_segment_register(reg) && !is_descriptor_table(reg)) 493 return (EINVAL); 494 495 return (VMGETDESC(vm->cookie, vcpu, reg, desc)); 496} 497 498int 499vm_set_seg_desc(struct vm *vm, int vcpu, int reg, 500 struct seg_desc *desc) 501{ 502 if (vcpu < 0 || vcpu >= VM_MAXCPU) 503 return (EINVAL); 504 505 if (!is_segment_register(reg) && !is_descriptor_table(reg)) 506 return (EINVAL); 507 508 return (VMSETDESC(vm->cookie, vcpu, reg, desc)); 509} 510 511int 512vm_get_pinning(struct vm *vm, int vcpuid, int *cpuid) 513{ 514 515 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 516 return (EINVAL); 517 518 *cpuid = VCPU_PINCPU(vm, vcpuid); 519 520 return (0); 521} 522 523int 524vm_set_pinning(struct vm *vm, int vcpuid, int host_cpuid) 525{ 526 struct thread *td; 527 528 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 529 return (EINVAL); 530 531 td = curthread; /* XXXSMP only safe when muxing vcpus */ 532 533 /* unpin */ 534 if (host_cpuid < 0) { 535 VCPU_UNPIN(vm, vcpuid); 536 thread_lock(td); 537 sched_unbind(td); 538 thread_unlock(td); 539 return (0); 540 } 541 542 if (CPU_ABSENT(host_cpuid)) 543 return (EINVAL); 544 545 /* 546 * XXX we should check that 'host_cpuid' has not already been pinned 547 * by another vm. 548 */ 549 thread_lock(td); 550 sched_bind(td, host_cpuid); 551 thread_unlock(td); 552 VCPU_PIN(vm, vcpuid, host_cpuid); 553 554 return (0); 555} 556 557static void 558restore_guest_fpustate(struct vcpu *vcpu) 559{ 560 561 /* flush host state to the pcb */ 562 fpuexit(curthread); 563 fpu_stop_emulating(); 564 fpurestore(vcpu->guestfpu); 565} 566 567static void 568save_guest_fpustate(struct vcpu *vcpu) 569{ 570 571 fpusave(vcpu->guestfpu); 572 fpu_start_emulating(); 573} 574 575int 576vm_run(struct vm *vm, struct vm_run *vmrun) 577{ 578 int error, vcpuid; 579 struct vcpu *vcpu; 580 struct pcb *pcb; 581 uint64_t tscval; 582 583 vcpuid = vmrun->cpuid; 584 585 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 586 return (EINVAL); 587 588 vcpu = &vm->vcpu[vcpuid]; 589 590 critical_enter(); 591 592 tscval = rdtsc(); 593 594 pcb = PCPU_GET(curpcb); 595 set_pcb_flags(pcb, PCB_FULL_IRET); 596 597 vcpu->hostcpu = curcpu; 598 599 restore_guest_msrs(vm, vcpuid); 600 restore_guest_fpustate(vcpu); 601 error = VMRUN(vm->cookie, vcpuid, vmrun->rip); 602 save_guest_fpustate(vcpu); 603 restore_host_msrs(vm, vcpuid); 604 605 vmm_stat_incr(vm, vcpuid, VCPU_TOTAL_RUNTIME, rdtsc() - tscval); 606 607 /* copy the exit information */ 608 bcopy(&vcpu->exitinfo, &vmrun->vm_exit, sizeof(struct vm_exit)); 609 610 critical_exit(); 611 612 return (error); 613} 614 615int 616vm_inject_event(struct vm *vm, int vcpuid, int type, 617 int vector, uint32_t code, int code_valid) 618{ 619 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 620 return (EINVAL); 621 622 if ((type > VM_EVENT_NONE && type < VM_EVENT_MAX) == 0) 623 return (EINVAL); 624 625 if (vector < 0 || vector > 255) 626 return (EINVAL); 627 628 return (VMINJECT(vm->cookie, vcpuid, type, vector, code, code_valid)); 629} 630 631int 632vm_inject_nmi(struct vm *vm, int vcpu) 633{ 634 int error; 635 636 if (vcpu < 0 || vcpu >= VM_MAXCPU) 637 return (EINVAL); 638 639 error = VMNMI(vm->cookie, vcpu); 640 vm_interrupt_hostcpu(vm, vcpu); 641 return (error); 642} 643 644int 645vm_get_capability(struct vm *vm, int vcpu, int type, int *retval) 646{ 647 if (vcpu < 0 || vcpu >= VM_MAXCPU) 648 return (EINVAL); 649 650 if (type < 0 || type >= VM_CAP_MAX) 651 return (EINVAL); 652 653 return (VMGETCAP(vm->cookie, vcpu, type, retval)); 654} 655 656int 657vm_set_capability(struct vm *vm, int vcpu, int type, int val) 658{ 659 if (vcpu < 0 || vcpu >= VM_MAXCPU) 660 return (EINVAL); 661 662 if (type < 0 || type >= VM_CAP_MAX) 663 return (EINVAL); 664 665 return (VMSETCAP(vm->cookie, vcpu, type, val)); 666} 667 668uint64_t * 669vm_guest_msrs(struct vm *vm, int cpu) 670{ 671 return (vm->vcpu[cpu].guest_msrs); 672} 673 674struct vlapic * 675vm_lapic(struct vm *vm, int cpu) 676{ 677 return (vm->vcpu[cpu].vlapic); 678} 679 680boolean_t 681vmm_is_pptdev(int bus, int slot, int func) 682{ 683 int found, b, s, f, n; 684 char *val, *cp, *cp2; 685 686 /* 687 * setenv pptdevs "1/2/3 4/5/6 7/8/9 10/11/12" 688 */ 689 found = 0; 690 cp = val = getenv("pptdevs"); 691 while (cp != NULL && *cp != '\0') { 692 if ((cp2 = strchr(cp, ' ')) != NULL) 693 *cp2 = '\0'; 694 695 n = sscanf(cp, "%d/%d/%d", &b, &s, &f); 696 if (n == 3 && bus == b && slot == s && func == f) { 697 found = 1; 698 break; 699 } 700 701 if (cp2 != NULL) 702 *cp2++ = ' '; 703 704 cp = cp2; 705 } 706 freeenv(val); 707 return (found); 708} 709 710void * 711vm_iommu_domain(struct vm *vm) 712{ 713 714 return (vm->iommu); 715} 716 717void 718vm_set_run_state(struct vm *vm, int vcpuid, int state) 719{ 720 struct vcpu *vcpu; 721 722 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 723 panic("vm_set_run_state: invalid vcpuid %d", vcpuid); 724 725 vcpu = &vm->vcpu[vcpuid]; 726 727 if (state == VCPU_RUNNING) { 728 if (vcpu->flags & VCPU_F_RUNNING) { 729 panic("vm_set_run_state: %s[%d] is already running", 730 vm_name(vm), vcpuid); 731 } 732 vcpu->flags |= VCPU_F_RUNNING; 733 } else { 734 if ((vcpu->flags & VCPU_F_RUNNING) == 0) { 735 panic("vm_set_run_state: %s[%d] is already stopped", 736 vm_name(vm), vcpuid); 737 } 738 vcpu->flags &= ~VCPU_F_RUNNING; 739 } 740} 741 742int 743vm_get_run_state(struct vm *vm, int vcpuid, int *cpuptr) 744{ 745 int retval, hostcpu; 746 struct vcpu *vcpu; 747 748 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 749 panic("vm_get_run_state: invalid vcpuid %d", vcpuid); 750 751 vcpu = &vm->vcpu[vcpuid]; 752 if (vcpu->flags & VCPU_F_RUNNING) { 753 retval = VCPU_RUNNING; 754 hostcpu = vcpu->hostcpu; 755 } else { 756 retval = VCPU_STOPPED; 757 hostcpu = -1; 758 } 759 760 if (cpuptr) 761 *cpuptr = hostcpu; 762 763 return (retval); 764} 765 766void 767vm_activate_cpu(struct vm *vm, int vcpuid) 768{ 769 770 if (vcpuid >= 0 && vcpuid < VM_MAXCPU) 771 CPU_SET(vcpuid, &vm->active_cpus); 772} 773 774cpuset_t 775vm_active_cpus(struct vm *vm) 776{ 777 778 return (vm->active_cpus); 779} 780 781void * 782vcpu_stats(struct vm *vm, int vcpuid) 783{ 784 785 return (vm->vcpu[vcpuid].stats); 786} 787 788int 789vm_get_x2apic_state(struct vm *vm, int vcpuid, enum x2apic_state *state) 790{ 791 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 792 return (EINVAL); 793 794 *state = vm->vcpu[vcpuid].x2apic_state; 795 796 return (0); 797} 798 799int 800vm_set_x2apic_state(struct vm *vm, int vcpuid, enum x2apic_state state) 801{ 802 if (vcpuid < 0 || vcpuid >= VM_MAXCPU) 803 return (EINVAL); 804 805 if (state < 0 || state >= X2APIC_STATE_LAST) 806 return (EINVAL); 807 808 vm->vcpu[vcpuid].x2apic_state = state; 809 810 vlapic_set_x2apic_state(vm, vcpuid, state); 811 812 return (0); 813} 814