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