bhyverun.c revision 270070
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 270070 2014-08-17 00:52:07Z grehan $ 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: stable/10/usr.sbin/bhyve/bhyverun.c 270070 2014-08-17 00:52:07Z grehan $"); 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 /* 246 * The 'newcpu' must be activated in the context of 'fromcpu'. If 247 * vm_activate_cpu() is delayed until newcpu's pthread starts running 248 * then vmm.ko is out-of-sync with bhyve and this can create a race 249 * with vm_suspend(). 250 */ 251 error = vm_activate_cpu(ctx, newcpu); 252 assert(error == 0); 253 254 CPU_SET_ATOMIC(newcpu, &cpumask); 255 256 /* 257 * Set up the vmexit struct to allow execution to start 258 * at the given RIP 259 */ 260 vmexit[newcpu].rip = rip; 261 vmexit[newcpu].inst_length = 0; 262 263 mt_vmm_info[newcpu].mt_ctx = ctx; 264 mt_vmm_info[newcpu].mt_vcpu = newcpu; 265 266 error = pthread_create(&mt_vmm_info[newcpu].mt_thr, NULL, 267 fbsdrun_start_thread, &mt_vmm_info[newcpu]); 268 assert(error == 0); 269} 270 271static int 272fbsdrun_deletecpu(struct vmctx *ctx, int vcpu) 273{ 274 275 if (!CPU_ISSET(vcpu, &cpumask)) { 276 fprintf(stderr, "Attempting to delete unknown cpu %d\n", vcpu); 277 exit(1); 278 } 279 280 CPU_CLR_ATOMIC(vcpu, &cpumask); 281 return (CPU_EMPTY(&cpumask)); 282} 283 284static int 285vmexit_handle_notify(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu, 286 uint32_t eax) 287{ 288#if BHYVE_DEBUG 289 /* 290 * put guest-driven debug here 291 */ 292#endif 293 return (VMEXIT_CONTINUE); 294} 295 296static int 297vmexit_inout(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu) 298{ 299 int error; 300 int bytes, port, in, out, string; 301 int vcpu; 302 303 vcpu = *pvcpu; 304 305 port = vme->u.inout.port; 306 bytes = vme->u.inout.bytes; 307 string = vme->u.inout.string; 308 in = vme->u.inout.in; 309 out = !in; 310 311 /* Extra-special case of host notifications */ 312 if (out && port == GUEST_NIO_PORT) { 313 error = vmexit_handle_notify(ctx, vme, pvcpu, vme->u.inout.eax); 314 return (error); 315 } 316 317 error = emulate_inout(ctx, vcpu, vme, strictio); 318 if (error == INOUT_OK && in && !string) { 319 error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX, 320 vme->u.inout.eax); 321 } 322 323 switch (error) { 324 case INOUT_OK: 325 return (VMEXIT_CONTINUE); 326 case INOUT_RESTART: 327 return (VMEXIT_RESTART); 328 case INOUT_RESET: 329 stats.io_reset++; 330 return (VMEXIT_RESET); 331 case INOUT_POWEROFF: 332 stats.io_poweroff++; 333 return (VMEXIT_POWEROFF); 334 default: 335 fprintf(stderr, "Unhandled %s%c 0x%04x\n", 336 in ? "in" : "out", 337 bytes == 1 ? 'b' : (bytes == 2 ? 'w' : 'l'), port); 338 return (VMEXIT_ABORT); 339 } 340} 341 342static int 343vmexit_rdmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu) 344{ 345 uint64_t val; 346 uint32_t eax, edx; 347 int error; 348 349 val = 0; 350 error = emulate_rdmsr(ctx, *pvcpu, vme->u.msr.code, &val); 351 if (error != 0) { 352 fprintf(stderr, "rdmsr to register %#x on vcpu %d\n", 353 vme->u.msr.code, *pvcpu); 354 if (strictmsr) { 355 error = vm_inject_exception2(ctx, *pvcpu, IDT_GP, 0); 356 assert(error == 0); 357 return (VMEXIT_RESTART); 358 } 359 } 360 361 eax = val; 362 error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RAX, eax); 363 assert(error == 0); 364 365 edx = val >> 32; 366 error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RDX, edx); 367 assert(error == 0); 368 369 return (VMEXIT_CONTINUE); 370} 371 372static int 373vmexit_wrmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu) 374{ 375 int error; 376 377 error = emulate_wrmsr(ctx, *pvcpu, vme->u.msr.code, vme->u.msr.wval); 378 if (error != 0) { 379 fprintf(stderr, "wrmsr to register %#x(%#lx) on vcpu %d\n", 380 vme->u.msr.code, vme->u.msr.wval, *pvcpu); 381 if (strictmsr) { 382 error = vm_inject_exception2(ctx, *pvcpu, IDT_GP, 0); 383 assert(error == 0); 384 return (VMEXIT_RESTART); 385 } 386 } 387 return (VMEXIT_CONTINUE); 388} 389 390static int 391vmexit_spinup_ap(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu) 392{ 393 int newcpu; 394 int retval = VMEXIT_CONTINUE; 395 396 newcpu = spinup_ap(ctx, *pvcpu, 397 vme->u.spinup_ap.vcpu, vme->u.spinup_ap.rip); 398 399 return (retval); 400} 401 402static int 403vmexit_vmx(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 404{ 405 406 fprintf(stderr, "vm exit[%d]\n", *pvcpu); 407 fprintf(stderr, "\treason\t\tVMX\n"); 408 fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip); 409 fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length); 410 fprintf(stderr, "\tstatus\t\t%d\n", vmexit->u.vmx.status); 411 fprintf(stderr, "\texit_reason\t%u\n", vmexit->u.vmx.exit_reason); 412 fprintf(stderr, "\tqualification\t0x%016lx\n", 413 vmexit->u.vmx.exit_qualification); 414 fprintf(stderr, "\tinst_type\t\t%d\n", vmexit->u.vmx.inst_type); 415 fprintf(stderr, "\tinst_error\t\t%d\n", vmexit->u.vmx.inst_error); 416 417 return (VMEXIT_ABORT); 418} 419 420static int 421vmexit_bogus(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 422{ 423 424 stats.vmexit_bogus++; 425 426 return (VMEXIT_RESTART); 427} 428 429static int 430vmexit_hlt(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 431{ 432 433 stats.vmexit_hlt++; 434 435 /* 436 * Just continue execution with the next instruction. We use 437 * the HLT VM exit as a way to be friendly with the host 438 * scheduler. 439 */ 440 return (VMEXIT_CONTINUE); 441} 442 443static int 444vmexit_pause(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 445{ 446 447 stats.vmexit_pause++; 448 449 return (VMEXIT_CONTINUE); 450} 451 452static int 453vmexit_mtrap(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 454{ 455 456 stats.vmexit_mtrap++; 457 458 return (VMEXIT_RESTART); 459} 460 461static int 462vmexit_inst_emul(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 463{ 464 int err; 465 stats.vmexit_inst_emul++; 466 467 err = emulate_mem(ctx, *pvcpu, vmexit->u.inst_emul.gpa, 468 &vmexit->u.inst_emul.vie); 469 470 if (err) { 471 if (err == EINVAL) { 472 fprintf(stderr, 473 "Failed to emulate instruction at 0x%lx\n", 474 vmexit->rip); 475 } else if (err == ESRCH) { 476 fprintf(stderr, "Unhandled memory access to 0x%lx\n", 477 vmexit->u.inst_emul.gpa); 478 } 479 480 return (VMEXIT_ABORT); 481 } 482 483 return (VMEXIT_CONTINUE); 484} 485 486static pthread_mutex_t resetcpu_mtx = PTHREAD_MUTEX_INITIALIZER; 487static pthread_cond_t resetcpu_cond = PTHREAD_COND_INITIALIZER; 488 489static int 490vmexit_suspend(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 491{ 492 enum vm_suspend_how how; 493 494 how = vmexit->u.suspended.how; 495 496 fbsdrun_deletecpu(ctx, *pvcpu); 497 498 if (*pvcpu != BSP) { 499 pthread_mutex_lock(&resetcpu_mtx); 500 pthread_cond_signal(&resetcpu_cond); 501 pthread_mutex_unlock(&resetcpu_mtx); 502 pthread_exit(NULL); 503 } 504 505 pthread_mutex_lock(&resetcpu_mtx); 506 while (!CPU_EMPTY(&cpumask)) { 507 pthread_cond_wait(&resetcpu_cond, &resetcpu_mtx); 508 } 509 pthread_mutex_unlock(&resetcpu_mtx); 510 511 switch (how) { 512 case VM_SUSPEND_RESET: 513 exit(0); 514 case VM_SUSPEND_POWEROFF: 515 exit(1); 516 case VM_SUSPEND_HALT: 517 exit(2); 518 default: 519 fprintf(stderr, "vmexit_suspend: invalid reason %d\n", how); 520 exit(100); 521 } 522 return (0); /* NOTREACHED */ 523} 524 525static vmexit_handler_t handler[VM_EXITCODE_MAX] = { 526 [VM_EXITCODE_INOUT] = vmexit_inout, 527 [VM_EXITCODE_INOUT_STR] = vmexit_inout, 528 [VM_EXITCODE_VMX] = vmexit_vmx, 529 [VM_EXITCODE_BOGUS] = vmexit_bogus, 530 [VM_EXITCODE_RDMSR] = vmexit_rdmsr, 531 [VM_EXITCODE_WRMSR] = vmexit_wrmsr, 532 [VM_EXITCODE_MTRAP] = vmexit_mtrap, 533 [VM_EXITCODE_INST_EMUL] = vmexit_inst_emul, 534 [VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap, 535 [VM_EXITCODE_SUSPENDED] = vmexit_suspend 536}; 537 538static void 539vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip) 540{ 541 int error, rc, prevcpu; 542 enum vm_exitcode exitcode; 543 enum vm_suspend_how how; 544 cpuset_t active_cpus; 545 546 if (vcpumap[vcpu] != NULL) { 547 error = pthread_setaffinity_np(pthread_self(), 548 sizeof(cpuset_t), vcpumap[vcpu]); 549 assert(error == 0); 550 } 551 552 error = vm_active_cpus(ctx, &active_cpus); 553 assert(CPU_ISSET(vcpu, &active_cpus)); 554 555 while (1) { 556 error = vm_run(ctx, vcpu, rip, &vmexit[vcpu]); 557 if (error != 0) 558 break; 559 560 prevcpu = vcpu; 561 562 exitcode = vmexit[vcpu].exitcode; 563 if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) { 564 fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n", 565 exitcode); 566 exit(1); 567 } 568 569 rc = (*handler[exitcode])(ctx, &vmexit[vcpu], &vcpu); 570 571 switch (rc) { 572 case VMEXIT_CONTINUE: 573 rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length; 574 break; 575 case VMEXIT_RESTART: 576 rip = vmexit[vcpu].rip; 577 break; 578 case VMEXIT_RESET: 579 case VMEXIT_POWEROFF: 580 if (rc == VMEXIT_RESET) 581 how = VM_SUSPEND_RESET; 582 else 583 how = VM_SUSPEND_POWEROFF; 584 error = vm_suspend(ctx, how); 585 assert(error == 0 || errno == EALREADY); 586 rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length; 587 break; 588 case VMEXIT_ABORT: 589 abort(); 590 default: 591 exit(1); 592 } 593 } 594 fprintf(stderr, "vm_run error %d, errno %d\n", error, errno); 595} 596 597static int 598num_vcpus_allowed(struct vmctx *ctx) 599{ 600 int tmp, error; 601 602 error = vm_get_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, &tmp); 603 604 /* 605 * The guest is allowed to spinup more than one processor only if the 606 * UNRESTRICTED_GUEST capability is available. 607 */ 608 if (error == 0) 609 return (VM_MAXCPU); 610 else 611 return (1); 612} 613 614void 615fbsdrun_set_capabilities(struct vmctx *ctx, int cpu) 616{ 617 int err, tmp; 618 619 if (fbsdrun_vmexit_on_hlt()) { 620 err = vm_get_capability(ctx, cpu, VM_CAP_HALT_EXIT, &tmp); 621 if (err < 0) { 622 fprintf(stderr, "VM exit on HLT not supported\n"); 623 exit(1); 624 } 625 vm_set_capability(ctx, cpu, VM_CAP_HALT_EXIT, 1); 626 if (cpu == BSP) 627 handler[VM_EXITCODE_HLT] = vmexit_hlt; 628 } 629 630 if (fbsdrun_vmexit_on_pause()) { 631 /* 632 * pause exit support required for this mode 633 */ 634 err = vm_get_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, &tmp); 635 if (err < 0) { 636 fprintf(stderr, 637 "SMP mux requested, no pause support\n"); 638 exit(1); 639 } 640 vm_set_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, 1); 641 if (cpu == BSP) 642 handler[VM_EXITCODE_PAUSE] = vmexit_pause; 643 } 644 645 if (x2apic_mode) 646 err = vm_set_x2apic_state(ctx, cpu, X2APIC_ENABLED); 647 else 648 err = vm_set_x2apic_state(ctx, cpu, X2APIC_DISABLED); 649 650 if (err) { 651 fprintf(stderr, "Unable to set x2apic state (%d)\n", err); 652 exit(1); 653 } 654 655 vm_set_capability(ctx, cpu, VM_CAP_ENABLE_INVPCID, 1); 656} 657 658int 659main(int argc, char *argv[]) 660{ 661 int c, error, gdb_port, err, bvmcons; 662 int dump_guest_memory, max_vcpus, mptgen; 663 struct vmctx *ctx; 664 uint64_t rip; 665 size_t memsize; 666 667 bvmcons = 0; 668 dump_guest_memory = 0; 669 progname = basename(argv[0]); 670 gdb_port = 0; 671 guest_ncpus = 1; 672 memsize = 256 * MB; 673 mptgen = 1; 674 675 while ((c = getopt(argc, argv, "abehwxACHIPWYp:g:c:s:m:l:U:")) != -1) { 676 switch (c) { 677 case 'a': 678 x2apic_mode = 0; 679 break; 680 case 'A': 681 acpi = 1; 682 break; 683 case 'b': 684 bvmcons = 1; 685 break; 686 case 'p': 687 if (pincpu_parse(optarg) != 0) { 688 errx(EX_USAGE, "invalid vcpu pinning " 689 "configuration '%s'", optarg); 690 } 691 break; 692 case 'c': 693 guest_ncpus = atoi(optarg); 694 break; 695 case 'C': 696 dump_guest_memory = 1; 697 break; 698 case 'g': 699 gdb_port = atoi(optarg); 700 break; 701 case 'l': 702 if (lpc_device_parse(optarg) != 0) { 703 errx(EX_USAGE, "invalid lpc device " 704 "configuration '%s'", optarg); 705 } 706 break; 707 case 's': 708 if (pci_parse_slot(optarg) != 0) 709 exit(1); 710 else 711 break; 712 case 'm': 713 error = vm_parse_memsize(optarg, &memsize); 714 if (error) 715 errx(EX_USAGE, "invalid memsize '%s'", optarg); 716 break; 717 case 'H': 718 guest_vmexit_on_hlt = 1; 719 break; 720 case 'I': 721 /* 722 * The "-I" option was used to add an ioapic to the 723 * virtual machine. 724 * 725 * An ioapic is now provided unconditionally for each 726 * virtual machine and this option is now deprecated. 727 */ 728 break; 729 case 'P': 730 guest_vmexit_on_pause = 1; 731 break; 732 case 'e': 733 strictio = 1; 734 break; 735 case 'U': 736 guest_uuid_str = optarg; 737 break; 738 case 'w': 739 strictmsr = 0; 740 break; 741 case 'W': 742 virtio_msix = 0; 743 break; 744 case 'x': 745 x2apic_mode = 1; 746 break; 747 case 'Y': 748 mptgen = 0; 749 break; 750 case 'h': 751 usage(0); 752 default: 753 usage(1); 754 } 755 } 756 argc -= optind; 757 argv += optind; 758 759 if (argc != 1) 760 usage(1); 761 762 vmname = argv[0]; 763 764 ctx = vm_open(vmname); 765 if (ctx == NULL) { 766 perror("vm_open"); 767 exit(1); 768 } 769 770 max_vcpus = num_vcpus_allowed(ctx); 771 if (guest_ncpus > max_vcpus) { 772 fprintf(stderr, "%d vCPUs requested but only %d available\n", 773 guest_ncpus, max_vcpus); 774 exit(1); 775 } 776 777 fbsdrun_set_capabilities(ctx, BSP); 778 779 if (dump_guest_memory) 780 vm_set_memflags(ctx, VM_MEM_F_INCORE); 781 err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL); 782 if (err) { 783 fprintf(stderr, "Unable to setup memory (%d)\n", err); 784 exit(1); 785 } 786 787 init_mem(); 788 init_inout(); 789 pci_irq_init(ctx); 790 ioapic_init(ctx); 791 792 rtc_init(ctx); 793 sci_init(ctx); 794 795 /* 796 * Exit if a device emulation finds an error in it's initilization 797 */ 798 if (init_pci(ctx) != 0) 799 exit(1); 800 801 if (gdb_port != 0) 802 init_dbgport(gdb_port); 803 804 if (bvmcons) 805 init_bvmcons(); 806 807 error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip); 808 assert(error == 0); 809 810 /* 811 * build the guest tables, MP etc. 812 */ 813 if (mptgen) { 814 error = mptable_build(ctx, guest_ncpus); 815 if (error) 816 exit(1); 817 } 818 819 error = smbios_build(ctx); 820 assert(error == 0); 821 822 if (acpi) { 823 error = acpi_build(ctx, guest_ncpus); 824 assert(error == 0); 825 } 826 827 /* 828 * Change the proc title to include the VM name. 829 */ 830 setproctitle("%s", vmname); 831 832 /* 833 * Add CPU 0 834 */ 835 fbsdrun_addcpu(ctx, BSP, BSP, rip); 836 837 /* 838 * Head off to the main event dispatch loop 839 */ 840 mevent_dispatch(); 841 842 exit(1); 843} 844