bhyverun.c revision 262350
1158115Sume/*- 2158115Sume * Copyright (c) 2011 NetApp, Inc. 3158115Sume * All rights reserved. 4158115Sume * 5158115Sume * Redistribution and use in source and binary forms, with or without 6158115Sume * modification, are permitted provided that the following conditions 7158115Sume * are met: 8158115Sume * 1. Redistributions of source code must retain the above copyright 9158115Sume * notice, this list of conditions and the following disclaimer. 10158115Sume * 2. Redistributions in binary form must reproduce the above copyright 11158115Sume * notice, this list of conditions and the following disclaimer in the 12158115Sume * 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: stable/10/usr.sbin/bhyve/bhyverun.c 262350 2014-02-23 00:46:05Z jhb $ 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: stable/10/usr.sbin/bhyve/bhyverun.c 262350 2014-02-23 00:46:05Z jhb $"); 31 32#include <sys/types.h> 33#include <sys/mman.h> 34#include <sys/time.h> 35 36#include <machine/atomic.h> 37#include <machine/segments.h> 38 39#include <stdio.h> 40#include <stdlib.h> 41#include <string.h> 42#include <err.h> 43#include <libgen.h> 44#include <unistd.h> 45#include <assert.h> 46#include <errno.h> 47#include <pthread.h> 48#include <pthread_np.h> 49#include <sysexits.h> 50 51#include <machine/vmm.h> 52#include <vmmapi.h> 53 54#include "bhyverun.h" 55#include "acpi.h" 56#include "inout.h" 57#include "dbgport.h" 58#include "legacy_irq.h" 59#include "mem.h" 60#include "mevent.h" 61#include "mptbl.h" 62#include "pci_emul.h" 63#include "pci_lpc.h" 64#include "xmsr.h" 65#include "spinup_ap.h" 66#include "rtc.h" 67 68#define GUEST_NIO_PORT 0x488 /* guest upcalls via i/o port */ 69 70#define VMEXIT_SWITCH 0 /* force vcpu switch in mux mode */ 71#define VMEXIT_CONTINUE 1 /* continue from next instruction */ 72#define VMEXIT_RESTART 2 /* restart current instruction */ 73#define VMEXIT_ABORT 3 /* abort the vm run loop */ 74#define VMEXIT_RESET 4 /* guest machine has reset */ 75#define VMEXIT_POWEROFF 5 /* guest machine has powered off */ 76 77#define MB (1024UL * 1024) 78#define GB (1024UL * MB) 79 80typedef int (*vmexit_handler_t)(struct vmctx *, struct vm_exit *, int *vcpu); 81 82char *vmname; 83 84int guest_ncpus; 85 86static int pincpu = -1; 87static int guest_vmexit_on_hlt, guest_vmexit_on_pause, disable_x2apic; 88static int virtio_msix = 1; 89 90static int strictio; 91 92static int acpi; 93 94static char *progname; 95static const int BSP = 0; 96 97static int cpumask; 98 99static void vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip); 100 101struct vm_exit vmexit[VM_MAXCPU]; 102 103struct bhyvestats { 104 uint64_t vmexit_bogus; 105 uint64_t vmexit_bogus_switch; 106 uint64_t vmexit_hlt; 107 uint64_t vmexit_pause; 108 uint64_t vmexit_mtrap; 109 uint64_t vmexit_inst_emul; 110 uint64_t cpu_switch_rotate; 111 uint64_t cpu_switch_direct; 112 int io_reset; 113} stats; 114 115struct mt_vmm_info { 116 pthread_t mt_thr; 117 struct vmctx *mt_ctx; 118 int mt_vcpu; 119} mt_vmm_info[VM_MAXCPU]; 120 121static void 122usage(int code) 123{ 124 125 fprintf(stderr, 126 "Usage: %s [-aehAHIPW] [-g <gdb port>] [-s <pci>] [-S <pci>]\n" 127 " %*s [-c vcpus] [-p pincpu] [-m mem] [-l <lpc>] <vm>\n" 128 " -a: local apic is in XAPIC mode (default is X2APIC)\n" 129 " -A: create an ACPI table\n" 130 " -g: gdb port\n" 131 " -c: # cpus (default 1)\n" 132 " -p: pin vcpu 'n' to host cpu 'pincpu + n'\n" 133 " -H: vmexit from the guest on hlt\n" 134 " -P: vmexit from the guest on pause\n" 135 " -W: force virtio to use single-vector MSI\n" 136 " -e: exit on unhandled I/O access\n" 137 " -h: help\n" 138 " -s: <slot,driver,configinfo> PCI slot config\n" 139 " -S: <slot,driver,configinfo> legacy PCI slot config\n" 140 " -l: LPC device configuration\n" 141 " -m: memory size in MB\n", 142 progname, (int)strlen(progname), ""); 143 144 exit(code); 145} 146 147void * 148paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len) 149{ 150 151 return (vm_map_gpa(ctx, gaddr, len)); 152} 153 154int 155fbsdrun_disable_x2apic(void) 156{ 157 158 return (disable_x2apic); 159} 160 161int 162fbsdrun_vmexit_on_pause(void) 163{ 164 165 return (guest_vmexit_on_pause); 166} 167 168int 169fbsdrun_vmexit_on_hlt(void) 170{ 171 172 return (guest_vmexit_on_hlt); 173} 174 175int 176fbsdrun_virtio_msix(void) 177{ 178 179 return (virtio_msix); 180} 181 182static void * 183fbsdrun_start_thread(void *param) 184{ 185 char tname[MAXCOMLEN + 1]; 186 struct mt_vmm_info *mtp; 187 int vcpu; 188 189 mtp = param; 190 vcpu = mtp->mt_vcpu; 191 192 snprintf(tname, sizeof(tname), "vcpu %d", vcpu); 193 pthread_set_name_np(mtp->mt_thr, tname); 194 195 vm_loop(mtp->mt_ctx, vcpu, vmexit[vcpu].rip); 196 197 /* not reached */ 198 exit(1); 199 return (NULL); 200} 201 202void 203fbsdrun_addcpu(struct vmctx *ctx, int vcpu, uint64_t rip) 204{ 205 int error; 206 207 if (cpumask & (1 << vcpu)) { 208 fprintf(stderr, "addcpu: attempting to add existing cpu %d\n", 209 vcpu); 210 exit(1); 211 } 212 213 atomic_set_int(&cpumask, 1 << vcpu); 214 215 /* 216 * Set up the vmexit struct to allow execution to start 217 * at the given RIP 218 */ 219 vmexit[vcpu].rip = rip; 220 vmexit[vcpu].inst_length = 0; 221 222 mt_vmm_info[vcpu].mt_ctx = ctx; 223 mt_vmm_info[vcpu].mt_vcpu = vcpu; 224 225 error = pthread_create(&mt_vmm_info[vcpu].mt_thr, NULL, 226 fbsdrun_start_thread, &mt_vmm_info[vcpu]); 227 assert(error == 0); 228} 229 230static int 231fbsdrun_deletecpu(struct vmctx *ctx, int vcpu) 232{ 233 234 if ((cpumask & (1 << vcpu)) == 0) { 235 fprintf(stderr, "addcpu: attempting to delete unknown cpu %d\n", 236 vcpu); 237 exit(1); 238 } 239 240 atomic_clear_int(&cpumask, 1 << vcpu); 241 return (cpumask == 0); 242} 243 244static int 245vmexit_catch_reset(void) 246{ 247 stats.io_reset++; 248 return (VMEXIT_RESET); 249} 250 251static int 252vmexit_catch_inout(void) 253{ 254 return (VMEXIT_ABORT); 255} 256 257static int 258vmexit_handle_notify(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu, 259 uint32_t eax) 260{ 261#if BHYVE_DEBUG 262 /* 263 * put guest-driven debug here 264 */ 265#endif 266 return (VMEXIT_CONTINUE); 267} 268 269static int 270vmexit_inout(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu) 271{ 272 int error; 273 int bytes, port, in, out; 274 uint32_t eax; 275 int vcpu; 276 277 vcpu = *pvcpu; 278 279 port = vme->u.inout.port; 280 bytes = vme->u.inout.bytes; 281 eax = vme->u.inout.eax; 282 in = vme->u.inout.in; 283 out = !in; 284 285 /* We don't deal with these */ 286 if (vme->u.inout.string || vme->u.inout.rep) 287 return (VMEXIT_ABORT); 288 289 /* Special case of guest reset */ 290 if (out && port == 0x64 && (uint8_t)eax == 0xFE) 291 return (vmexit_catch_reset()); 292 293 /* Extra-special case of host notifications */ 294 if (out && port == GUEST_NIO_PORT) 295 return (vmexit_handle_notify(ctx, vme, pvcpu, eax)); 296 297 error = emulate_inout(ctx, vcpu, in, port, bytes, &eax, strictio); 298 if (error == INOUT_OK && in) 299 error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX, eax); 300 301 switch (error) { 302 case INOUT_OK: 303 return (VMEXIT_CONTINUE); 304 case INOUT_RESET: 305 return (VMEXIT_RESET); 306 case INOUT_POWEROFF: 307 return (VMEXIT_POWEROFF); 308 default: 309 fprintf(stderr, "Unhandled %s%c 0x%04x\n", 310 in ? "in" : "out", 311 bytes == 1 ? 'b' : (bytes == 2 ? 'w' : 'l'), port); 312 return (vmexit_catch_inout()); 313 } 314} 315 316static int 317vmexit_rdmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu) 318{ 319 fprintf(stderr, "vm exit rdmsr 0x%x, cpu %d\n", vme->u.msr.code, 320 *pvcpu); 321 return (VMEXIT_ABORT); 322} 323 324static int 325vmexit_wrmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu) 326{ 327 int newcpu; 328 int retval = VMEXIT_CONTINUE; 329 330 newcpu = emulate_wrmsr(ctx, *pvcpu, vme->u.msr.code,vme->u.msr.wval); 331 332 return (retval); 333} 334 335static int 336vmexit_spinup_ap(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu) 337{ 338 int newcpu; 339 int retval = VMEXIT_CONTINUE; 340 341 newcpu = spinup_ap(ctx, *pvcpu, 342 vme->u.spinup_ap.vcpu, vme->u.spinup_ap.rip); 343 344 return (retval); 345} 346 347static int 348vmexit_spindown_cpu(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu) 349{ 350 int lastcpu; 351 352 lastcpu = fbsdrun_deletecpu(ctx, *pvcpu); 353 if (!lastcpu) 354 pthread_exit(NULL); 355 return (vmexit_catch_reset()); 356} 357 358static int 359vmexit_vmx(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 360{ 361 362 fprintf(stderr, "vm exit[%d]\n", *pvcpu); 363 fprintf(stderr, "\treason\t\tVMX\n"); 364 fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip); 365 fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length); 366 fprintf(stderr, "\terror\t\t%d\n", vmexit->u.vmx.error); 367 fprintf(stderr, "\texit_reason\t%u\n", vmexit->u.vmx.exit_reason); 368 fprintf(stderr, "\tqualification\t0x%016lx\n", 369 vmexit->u.vmx.exit_qualification); 370 371 return (VMEXIT_ABORT); 372} 373 374static int 375vmexit_bogus(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 376{ 377 378 stats.vmexit_bogus++; 379 380 return (VMEXIT_RESTART); 381} 382 383static int 384vmexit_hlt(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 385{ 386 387 stats.vmexit_hlt++; 388 389 /* 390 * Just continue execution with the next instruction. We use 391 * the HLT VM exit as a way to be friendly with the host 392 * scheduler. 393 */ 394 return (VMEXIT_CONTINUE); 395} 396 397static int 398vmexit_pause(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 399{ 400 401 stats.vmexit_pause++; 402 403 return (VMEXIT_CONTINUE); 404} 405 406static int 407vmexit_mtrap(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 408{ 409 410 stats.vmexit_mtrap++; 411 412 return (VMEXIT_RESTART); 413} 414 415static int 416vmexit_inst_emul(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 417{ 418 int err; 419 stats.vmexit_inst_emul++; 420 421 err = emulate_mem(ctx, *pvcpu, vmexit->u.inst_emul.gpa, 422 &vmexit->u.inst_emul.vie); 423 424 if (err) { 425 if (err == EINVAL) { 426 fprintf(stderr, 427 "Failed to emulate instruction at 0x%lx\n", 428 vmexit->rip); 429 } else if (err == ESRCH) { 430 fprintf(stderr, "Unhandled memory access to 0x%lx\n", 431 vmexit->u.inst_emul.gpa); 432 } 433 434 return (VMEXIT_ABORT); 435 } 436 437 return (VMEXIT_CONTINUE); 438} 439 440static vmexit_handler_t handler[VM_EXITCODE_MAX] = { 441 [VM_EXITCODE_INOUT] = vmexit_inout, 442 [VM_EXITCODE_VMX] = vmexit_vmx, 443 [VM_EXITCODE_BOGUS] = vmexit_bogus, 444 [VM_EXITCODE_RDMSR] = vmexit_rdmsr, 445 [VM_EXITCODE_WRMSR] = vmexit_wrmsr, 446 [VM_EXITCODE_MTRAP] = vmexit_mtrap, 447 [VM_EXITCODE_INST_EMUL] = vmexit_inst_emul, 448 [VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap, 449 [VM_EXITCODE_SPINDOWN_CPU] = vmexit_spindown_cpu, 450}; 451 452static void 453vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip) 454{ 455 cpuset_t mask; 456 int error, rc, prevcpu; 457 enum vm_exitcode exitcode; 458 459 if (pincpu >= 0) { 460 CPU_ZERO(&mask); 461 CPU_SET(pincpu + vcpu, &mask); 462 error = pthread_setaffinity_np(pthread_self(), 463 sizeof(mask), &mask); 464 assert(error == 0); 465 } 466 467 while (1) { 468 error = vm_run(ctx, vcpu, rip, &vmexit[vcpu]); 469 if (error != 0) { 470 /* 471 * It is possible that 'vmmctl' or some other process 472 * has transitioned the vcpu to CANNOT_RUN state right 473 * before we tried to transition it to RUNNING. 474 * 475 * This is expected to be temporary so just retry. 476 */ 477 if (errno == EBUSY) 478 continue; 479 else 480 break; 481 } 482 483 prevcpu = vcpu; 484 485 exitcode = vmexit[vcpu].exitcode; 486 if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) { 487 fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n", 488 exitcode); 489 exit(1); 490 } 491 492 rc = (*handler[exitcode])(ctx, &vmexit[vcpu], &vcpu); 493 494 switch (rc) { 495 case VMEXIT_CONTINUE: 496 rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length; 497 break; 498 case VMEXIT_RESTART: 499 rip = vmexit[vcpu].rip; 500 break; 501 case VMEXIT_RESET: 502 exit(0); 503 default: 504 exit(1); 505 } 506 } 507 fprintf(stderr, "vm_run error %d, errno %d\n", error, errno); 508} 509 510static int 511num_vcpus_allowed(struct vmctx *ctx) 512{ 513 int tmp, error; 514 515 error = vm_get_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, &tmp); 516 517 /* 518 * The guest is allowed to spinup more than one processor only if the 519 * UNRESTRICTED_GUEST capability is available. 520 */ 521 if (error == 0) 522 return (VM_MAXCPU); 523 else 524 return (1); 525} 526 527void 528fbsdrun_set_capabilities(struct vmctx *ctx, int cpu) 529{ 530 int err, tmp; 531 532 if (fbsdrun_vmexit_on_hlt()) { 533 err = vm_get_capability(ctx, cpu, VM_CAP_HALT_EXIT, &tmp); 534 if (err < 0) { 535 fprintf(stderr, "VM exit on HLT not supported\n"); 536 exit(1); 537 } 538 vm_set_capability(ctx, cpu, VM_CAP_HALT_EXIT, 1); 539 if (cpu == BSP) 540 handler[VM_EXITCODE_HLT] = vmexit_hlt; 541 } 542 543 if (fbsdrun_vmexit_on_pause()) { 544 /* 545 * pause exit support required for this mode 546 */ 547 err = vm_get_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, &tmp); 548 if (err < 0) { 549 fprintf(stderr, 550 "SMP mux requested, no pause support\n"); 551 exit(1); 552 } 553 vm_set_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, 1); 554 if (cpu == BSP) 555 handler[VM_EXITCODE_PAUSE] = vmexit_pause; 556 } 557 558 if (fbsdrun_disable_x2apic()) 559 err = vm_set_x2apic_state(ctx, cpu, X2APIC_DISABLED); 560 else 561 err = vm_set_x2apic_state(ctx, cpu, X2APIC_ENABLED); 562 563 if (err) { 564 fprintf(stderr, "Unable to set x2apic state (%d)\n", err); 565 exit(1); 566 } 567 568 vm_set_capability(ctx, cpu, VM_CAP_ENABLE_INVPCID, 1); 569} 570 571int 572main(int argc, char *argv[]) 573{ 574 int c, error, gdb_port, err, bvmcons; 575 int max_vcpus; 576 struct vmctx *ctx; 577 uint64_t rip; 578 size_t memsize; 579 580 bvmcons = 0; 581 progname = basename(argv[0]); 582 gdb_port = 0; 583 guest_ncpus = 1; 584 memsize = 256 * MB; 585 586 while ((c = getopt(argc, argv, "abehAHIPWp:g:c:s:S:m:l:")) != -1) { 587 switch (c) { 588 case 'a': 589 disable_x2apic = 1; 590 break; 591 case 'A': 592 acpi = 1; 593 break; 594 case 'b': 595 bvmcons = 1; 596 break; 597 case 'p': 598 pincpu = atoi(optarg); 599 break; 600 case 'c': 601 guest_ncpus = atoi(optarg); 602 break; 603 case 'g': 604 gdb_port = atoi(optarg); 605 break; 606 case 'l': 607 if (lpc_device_parse(optarg) != 0) { 608 errx(EX_USAGE, "invalid lpc device " 609 "configuration '%s'", optarg); 610 } 611 break; 612 case 's': 613 if (pci_parse_slot(optarg, 0) != 0) 614 exit(1); 615 else 616 break; 617 case 'S': 618 if (pci_parse_slot(optarg, 1) != 0) 619 exit(1); 620 else 621 break; 622 case 'm': 623 error = vm_parse_memsize(optarg, &memsize); 624 if (error) 625 errx(EX_USAGE, "invalid memsize '%s'", optarg); 626 break; 627 case 'H': 628 guest_vmexit_on_hlt = 1; 629 break; 630 case 'I': 631 /* 632 * The "-I" option was used to add an ioapic to the 633 * virtual machine. 634 * 635 * An ioapic is now provided unconditionally for each 636 * virtual machine and this option is now deprecated. 637 */ 638 break; 639 case 'P': 640 guest_vmexit_on_pause = 1; 641 break; 642 case 'e': 643 strictio = 1; 644 break; 645 case 'W': 646 virtio_msix = 0; 647 break; 648 case 'h': 649 usage(0); 650 default: 651 usage(1); 652 } 653 } 654 argc -= optind; 655 argv += optind; 656 657 if (argc != 1) 658 usage(1); 659 660 vmname = argv[0]; 661 662 ctx = vm_open(vmname); 663 if (ctx == NULL) { 664 perror("vm_open"); 665 exit(1); 666 } 667 668 max_vcpus = num_vcpus_allowed(ctx); 669 if (guest_ncpus > max_vcpus) { 670 fprintf(stderr, "%d vCPUs requested but only %d available\n", 671 guest_ncpus, max_vcpus); 672 exit(1); 673 } 674 675 fbsdrun_set_capabilities(ctx, BSP); 676 677 err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL); 678 if (err) { 679 fprintf(stderr, "Unable to setup memory (%d)\n", err); 680 exit(1); 681 } 682 683 init_mem(); 684 init_inout(); 685 legacy_irq_init(); 686 687 rtc_init(ctx); 688 689 /* 690 * Exit if a device emulation finds an error in it's initilization 691 */ 692 if (init_pci(ctx) != 0) 693 exit(1); 694 695 if (gdb_port != 0) 696 init_dbgport(gdb_port); 697 698 if (bvmcons) 699 init_bvmcons(); 700 701 error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip); 702 assert(error == 0); 703 704 /* 705 * build the guest tables, MP etc. 706 */ 707 mptable_build(ctx, guest_ncpus); 708 709 if (acpi) { 710 error = acpi_build(ctx, guest_ncpus); 711 assert(error == 0); 712 } 713 714 /* 715 * Change the proc title to include the VM name. 716 */ 717 setproctitle("%s", vmname); 718 719 /* 720 * Add CPU 0 721 */ 722 fbsdrun_addcpu(ctx, BSP, rip); 723 724 /* 725 * Head off to the main event dispatch loop 726 */ 727 mevent_dispatch(); 728 729 exit(1); 730} 731