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