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