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