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