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