bhyverun.c revision 293412
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 293412 2016-01-08 03:02:19Z araujo $ 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: stable/10/usr.sbin/bhyve/bhyverun.c 293412 2016-01-08 03:02:19Z araujo $"); 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 75typedef int (*vmexit_handler_t)(struct vmctx *, struct vm_exit *, int *vcpu); 76extern int vmexit_task_switch(struct vmctx *, struct vm_exit *, int *vcpu); 77 78char *vmname; 79 80int guest_ncpus; 81char *guest_uuid_str; 82 83static int guest_vmexit_on_hlt, guest_vmexit_on_pause; 84static int virtio_msix = 1; 85static int x2apic_mode = 0; /* default is xAPIC */ 86 87static int strictio; 88static int strictmsr = 1; 89 90static int acpi; 91 92static char *progname; 93static const int BSP = 0; 94 95static cpuset_t cpumask; 96 97static void vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip); 98 99static struct vm_exit vmexit[VM_MAXCPU]; 100 101struct bhyvestats { 102 uint64_t vmexit_bogus; 103 uint64_t vmexit_reqidle; 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 112struct mt_vmm_info { 113 pthread_t mt_thr; 114 struct vmctx *mt_ctx; 115 int mt_vcpu; 116} mt_vmm_info[VM_MAXCPU]; 117 118static cpuset_t *vcpumap[VM_MAXCPU] = { NULL }; 119 120static void 121usage(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 151static int 152pincpu_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 184void 185vm_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 199void * 200paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len) 201{ 202 203 return (vm_map_gpa(ctx, gaddr, len)); 204} 205 206int 207fbsdrun_vmexit_on_pause(void) 208{ 209 210 return (guest_vmexit_on_pause); 211} 212 213int 214fbsdrun_vmexit_on_hlt(void) 215{ 216 217 return (guest_vmexit_on_hlt); 218} 219 220int 221fbsdrun_virtio_msix(void) 222{ 223 224 return (virtio_msix); 225} 226 227static void * 228fbsdrun_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 247void 248fbsdrun_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 if (error != 0) 262 err(EX_OSERR, "could not activate CPU %d", newcpu); 263 264 CPU_SET_ATOMIC(newcpu, &cpumask); 265 266 /* 267 * Set up the vmexit struct to allow execution to start 268 * at the given RIP 269 */ 270 vmexit[newcpu].rip = rip; 271 vmexit[newcpu].inst_length = 0; 272 273 mt_vmm_info[newcpu].mt_ctx = ctx; 274 mt_vmm_info[newcpu].mt_vcpu = newcpu; 275 276 error = pthread_create(&mt_vmm_info[newcpu].mt_thr, NULL, 277 fbsdrun_start_thread, &mt_vmm_info[newcpu]); 278 assert(error == 0); 279} 280 281static int 282fbsdrun_deletecpu(struct vmctx *ctx, int vcpu) 283{ 284 285 if (!CPU_ISSET(vcpu, &cpumask)) { 286 fprintf(stderr, "Attempting to delete unknown cpu %d\n", vcpu); 287 exit(1); 288 } 289 290 CPU_CLR_ATOMIC(vcpu, &cpumask); 291 return (CPU_EMPTY(&cpumask)); 292} 293 294static int 295vmexit_handle_notify(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu, 296 uint32_t eax) 297{ 298#if BHYVE_DEBUG 299 /* 300 * put guest-driven debug here 301 */ 302#endif 303 return (VMEXIT_CONTINUE); 304} 305 306static int 307vmexit_inout(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu) 308{ 309 int error; 310 int bytes, port, in, out; 311 int vcpu; 312 313 vcpu = *pvcpu; 314 315 port = vme->u.inout.port; 316 bytes = vme->u.inout.bytes; 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 at 0x%lx\n", 329 in ? "in" : "out", 330 bytes == 1 ? 'b' : (bytes == 2 ? 'w' : 'l'), 331 port, vmexit->rip); 332 return (VMEXIT_ABORT); 333 } else { 334 return (VMEXIT_CONTINUE); 335 } 336} 337 338static int 339vmexit_rdmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu) 340{ 341 uint64_t val; 342 uint32_t eax, edx; 343 int error; 344 345 val = 0; 346 error = emulate_rdmsr(ctx, *pvcpu, vme->u.msr.code, &val); 347 if (error != 0) { 348 fprintf(stderr, "rdmsr to register %#x on vcpu %d\n", 349 vme->u.msr.code, *pvcpu); 350 if (strictmsr) { 351 vm_inject_gp(ctx, *pvcpu); 352 return (VMEXIT_CONTINUE); 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 367static int 368vmexit_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 vm_inject_gp(ctx, *pvcpu); 378 return (VMEXIT_CONTINUE); 379 } 380 } 381 return (VMEXIT_CONTINUE); 382} 383 384static int 385vmexit_spinup_ap(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu) 386{ 387 int newcpu; 388 int retval = VMEXIT_CONTINUE; 389 390 newcpu = spinup_ap(ctx, *pvcpu, 391 vme->u.spinup_ap.vcpu, vme->u.spinup_ap.rip); 392 393 return (retval); 394} 395 396#define DEBUG_EPT_MISCONFIG 397#ifdef DEBUG_EPT_MISCONFIG 398#define EXIT_REASON_EPT_MISCONFIG 49 399#define VMCS_GUEST_PHYSICAL_ADDRESS 0x00002400 400#define VMCS_IDENT(x) ((x) | 0x80000000) 401 402static uint64_t ept_misconfig_gpa, ept_misconfig_pte[4]; 403static int ept_misconfig_ptenum; 404#endif 405 406static int 407vmexit_vmx(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 408{ 409 410 fprintf(stderr, "vm exit[%d]\n", *pvcpu); 411 fprintf(stderr, "\treason\t\tVMX\n"); 412 fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip); 413 fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length); 414 fprintf(stderr, "\tstatus\t\t%d\n", vmexit->u.vmx.status); 415 fprintf(stderr, "\texit_reason\t%u\n", vmexit->u.vmx.exit_reason); 416 fprintf(stderr, "\tqualification\t0x%016lx\n", 417 vmexit->u.vmx.exit_qualification); 418 fprintf(stderr, "\tinst_type\t\t%d\n", vmexit->u.vmx.inst_type); 419 fprintf(stderr, "\tinst_error\t\t%d\n", vmexit->u.vmx.inst_error); 420#ifdef DEBUG_EPT_MISCONFIG 421 if (vmexit->u.vmx.exit_reason == EXIT_REASON_EPT_MISCONFIG) { 422 vm_get_register(ctx, *pvcpu, 423 VMCS_IDENT(VMCS_GUEST_PHYSICAL_ADDRESS), 424 &ept_misconfig_gpa); 425 vm_get_gpa_pmap(ctx, ept_misconfig_gpa, ept_misconfig_pte, 426 &ept_misconfig_ptenum); 427 fprintf(stderr, "\tEPT misconfiguration:\n"); 428 fprintf(stderr, "\t\tGPA: %#lx\n", ept_misconfig_gpa); 429 fprintf(stderr, "\t\tPTE(%d): %#lx %#lx %#lx %#lx\n", 430 ept_misconfig_ptenum, ept_misconfig_pte[0], 431 ept_misconfig_pte[1], ept_misconfig_pte[2], 432 ept_misconfig_pte[3]); 433 } 434#endif /* DEBUG_EPT_MISCONFIG */ 435 return (VMEXIT_ABORT); 436} 437 438static int 439vmexit_svm(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 440{ 441 442 fprintf(stderr, "vm exit[%d]\n", *pvcpu); 443 fprintf(stderr, "\treason\t\tSVM\n"); 444 fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip); 445 fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length); 446 fprintf(stderr, "\texitcode\t%#lx\n", vmexit->u.svm.exitcode); 447 fprintf(stderr, "\texitinfo1\t%#lx\n", vmexit->u.svm.exitinfo1); 448 fprintf(stderr, "\texitinfo2\t%#lx\n", vmexit->u.svm.exitinfo2); 449 return (VMEXIT_ABORT); 450} 451 452static int 453vmexit_bogus(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 454{ 455 456 assert(vmexit->inst_length == 0); 457 458 stats.vmexit_bogus++; 459 460 return (VMEXIT_CONTINUE); 461} 462 463static int 464vmexit_reqidle(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 465{ 466 467 assert(vmexit->inst_length == 0); 468 469 stats.vmexit_reqidle++; 470 471 return (VMEXIT_CONTINUE); 472} 473 474static int 475vmexit_hlt(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 476{ 477 478 stats.vmexit_hlt++; 479 480 /* 481 * Just continue execution with the next instruction. We use 482 * the HLT VM exit as a way to be friendly with the host 483 * scheduler. 484 */ 485 return (VMEXIT_CONTINUE); 486} 487 488static int 489vmexit_pause(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 490{ 491 492 stats.vmexit_pause++; 493 494 return (VMEXIT_CONTINUE); 495} 496 497static int 498vmexit_mtrap(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 499{ 500 501 assert(vmexit->inst_length == 0); 502 503 stats.vmexit_mtrap++; 504 505 return (VMEXIT_CONTINUE); 506} 507 508static int 509vmexit_inst_emul(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 510{ 511 int err, i; 512 struct vie *vie; 513 514 stats.vmexit_inst_emul++; 515 516 vie = &vmexit->u.inst_emul.vie; 517 err = emulate_mem(ctx, *pvcpu, vmexit->u.inst_emul.gpa, 518 vie, &vmexit->u.inst_emul.paging); 519 520 if (err) { 521 if (err == ESRCH) { 522 fprintf(stderr, "Unhandled memory access to 0x%lx\n", 523 vmexit->u.inst_emul.gpa); 524 } 525 526 fprintf(stderr, "Failed to emulate instruction ["); 527 for (i = 0; i < vie->num_valid; i++) { 528 fprintf(stderr, "0x%02x%s", vie->inst[i], 529 i != (vie->num_valid - 1) ? " " : ""); 530 } 531 fprintf(stderr, "] at 0x%lx\n", vmexit->rip); 532 return (VMEXIT_ABORT); 533 } 534 535 return (VMEXIT_CONTINUE); 536} 537 538static pthread_mutex_t resetcpu_mtx = PTHREAD_MUTEX_INITIALIZER; 539static pthread_cond_t resetcpu_cond = PTHREAD_COND_INITIALIZER; 540 541static int 542vmexit_suspend(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu) 543{ 544 enum vm_suspend_how how; 545 546 how = vmexit->u.suspended.how; 547 548 fbsdrun_deletecpu(ctx, *pvcpu); 549 550 if (*pvcpu != BSP) { 551 pthread_mutex_lock(&resetcpu_mtx); 552 pthread_cond_signal(&resetcpu_cond); 553 pthread_mutex_unlock(&resetcpu_mtx); 554 pthread_exit(NULL); 555 } 556 557 pthread_mutex_lock(&resetcpu_mtx); 558 while (!CPU_EMPTY(&cpumask)) { 559 pthread_cond_wait(&resetcpu_cond, &resetcpu_mtx); 560 } 561 pthread_mutex_unlock(&resetcpu_mtx); 562 563 switch (how) { 564 case VM_SUSPEND_RESET: 565 exit(0); 566 case VM_SUSPEND_POWEROFF: 567 exit(1); 568 case VM_SUSPEND_HALT: 569 exit(2); 570 case VM_SUSPEND_TRIPLEFAULT: 571 exit(3); 572 default: 573 fprintf(stderr, "vmexit_suspend: invalid reason %d\n", how); 574 exit(100); 575 } 576 return (0); /* NOTREACHED */ 577} 578 579static vmexit_handler_t handler[VM_EXITCODE_MAX] = { 580 [VM_EXITCODE_INOUT] = vmexit_inout, 581 [VM_EXITCODE_INOUT_STR] = vmexit_inout, 582 [VM_EXITCODE_VMX] = vmexit_vmx, 583 [VM_EXITCODE_SVM] = vmexit_svm, 584 [VM_EXITCODE_BOGUS] = vmexit_bogus, 585 [VM_EXITCODE_REQIDLE] = vmexit_reqidle, 586 [VM_EXITCODE_RDMSR] = vmexit_rdmsr, 587 [VM_EXITCODE_WRMSR] = vmexit_wrmsr, 588 [VM_EXITCODE_MTRAP] = vmexit_mtrap, 589 [VM_EXITCODE_INST_EMUL] = vmexit_inst_emul, 590 [VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap, 591 [VM_EXITCODE_SUSPENDED] = vmexit_suspend, 592 [VM_EXITCODE_TASK_SWITCH] = vmexit_task_switch, 593}; 594 595static void 596vm_loop(struct vmctx *ctx, int vcpu, uint64_t startrip) 597{ 598 int error, rc; 599 enum vm_exitcode exitcode; 600 cpuset_t active_cpus; 601 602 if (vcpumap[vcpu] != NULL) { 603 error = pthread_setaffinity_np(pthread_self(), 604 sizeof(cpuset_t), vcpumap[vcpu]); 605 assert(error == 0); 606 } 607 608 error = vm_active_cpus(ctx, &active_cpus); 609 assert(CPU_ISSET(vcpu, &active_cpus)); 610 611 error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RIP, startrip); 612 assert(error == 0); 613 614 while (1) { 615 error = vm_run(ctx, vcpu, &vmexit[vcpu]); 616 if (error != 0) 617 break; 618 619 exitcode = vmexit[vcpu].exitcode; 620 if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) { 621 fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n", 622 exitcode); 623 exit(1); 624 } 625 626 rc = (*handler[exitcode])(ctx, &vmexit[vcpu], &vcpu); 627 628 switch (rc) { 629 case VMEXIT_CONTINUE: 630 break; 631 case VMEXIT_ABORT: 632 abort(); 633 default: 634 exit(1); 635 } 636 } 637 fprintf(stderr, "vm_run error %d, errno %d\n", error, errno); 638} 639 640static int 641num_vcpus_allowed(struct vmctx *ctx) 642{ 643 int tmp, error; 644 645 error = vm_get_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, &tmp); 646 647 /* 648 * The guest is allowed to spinup more than one processor only if the 649 * UNRESTRICTED_GUEST capability is available. 650 */ 651 if (error == 0) 652 return (VM_MAXCPU); 653 else 654 return (1); 655} 656 657void 658fbsdrun_set_capabilities(struct vmctx *ctx, int cpu) 659{ 660 int err, tmp; 661 662 if (fbsdrun_vmexit_on_hlt()) { 663 err = vm_get_capability(ctx, cpu, VM_CAP_HALT_EXIT, &tmp); 664 if (err < 0) { 665 fprintf(stderr, "VM exit on HLT not supported\n"); 666 exit(1); 667 } 668 vm_set_capability(ctx, cpu, VM_CAP_HALT_EXIT, 1); 669 if (cpu == BSP) 670 handler[VM_EXITCODE_HLT] = vmexit_hlt; 671 } 672 673 if (fbsdrun_vmexit_on_pause()) { 674 /* 675 * pause exit support required for this mode 676 */ 677 err = vm_get_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, &tmp); 678 if (err < 0) { 679 fprintf(stderr, 680 "SMP mux requested, no pause support\n"); 681 exit(1); 682 } 683 vm_set_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, 1); 684 if (cpu == BSP) 685 handler[VM_EXITCODE_PAUSE] = vmexit_pause; 686 } 687 688 if (x2apic_mode) 689 err = vm_set_x2apic_state(ctx, cpu, X2APIC_ENABLED); 690 else 691 err = vm_set_x2apic_state(ctx, cpu, X2APIC_DISABLED); 692 693 if (err) { 694 fprintf(stderr, "Unable to set x2apic state (%d)\n", err); 695 exit(1); 696 } 697 698 vm_set_capability(ctx, cpu, VM_CAP_ENABLE_INVPCID, 1); 699} 700 701int 702main(int argc, char *argv[]) 703{ 704 int c, error, gdb_port, err, bvmcons; 705 int dump_guest_memory, max_vcpus, mptgen; 706 int rtc_localtime; 707 struct vmctx *ctx; 708 uint64_t rip; 709 size_t memsize; 710 711 bvmcons = 0; 712 dump_guest_memory = 0; 713 progname = basename(argv[0]); 714 gdb_port = 0; 715 guest_ncpus = 1; 716 memsize = 256 * MB; 717 mptgen = 1; 718 rtc_localtime = 1; 719 720 while ((c = getopt(argc, argv, "abehuwxACHIPWYp:g:c:s:m:l:U:")) != -1) { 721 switch (c) { 722 case 'a': 723 x2apic_mode = 0; 724 break; 725 case 'A': 726 acpi = 1; 727 break; 728 case 'b': 729 bvmcons = 1; 730 break; 731 case 'p': 732 if (pincpu_parse(optarg) != 0) { 733 errx(EX_USAGE, "invalid vcpu pinning " 734 "configuration '%s'", optarg); 735 } 736 break; 737 case 'c': 738 guest_ncpus = atoi(optarg); 739 break; 740 case 'C': 741 dump_guest_memory = 1; 742 break; 743 case 'g': 744 gdb_port = atoi(optarg); 745 break; 746 case 'l': 747 if (lpc_device_parse(optarg) != 0) { 748 errx(EX_USAGE, "invalid lpc device " 749 "configuration '%s'", optarg); 750 } 751 break; 752 case 's': 753 if (pci_parse_slot(optarg) != 0) 754 exit(1); 755 else 756 break; 757 case 'm': 758 error = vm_parse_memsize(optarg, &memsize); 759 if (error) 760 errx(EX_USAGE, "invalid memsize '%s'", optarg); 761 break; 762 case 'H': 763 guest_vmexit_on_hlt = 1; 764 break; 765 case 'I': 766 /* 767 * The "-I" option was used to add an ioapic to the 768 * virtual machine. 769 * 770 * An ioapic is now provided unconditionally for each 771 * virtual machine and this option is now deprecated. 772 */ 773 break; 774 case 'P': 775 guest_vmexit_on_pause = 1; 776 break; 777 case 'e': 778 strictio = 1; 779 break; 780 case 'u': 781 rtc_localtime = 0; 782 break; 783 case 'U': 784 guest_uuid_str = optarg; 785 break; 786 case 'w': 787 strictmsr = 0; 788 break; 789 case 'W': 790 virtio_msix = 0; 791 break; 792 case 'x': 793 x2apic_mode = 1; 794 break; 795 case 'Y': 796 mptgen = 0; 797 break; 798 case 'h': 799 usage(0); 800 default: 801 usage(1); 802 } 803 } 804 argc -= optind; 805 argv += optind; 806 807 if (argc != 1) 808 usage(1); 809 810 vmname = argv[0]; 811 812 ctx = vm_open(vmname); 813 if (ctx == NULL) { 814 perror("vm_open"); 815 exit(1); 816 } 817 818 if (guest_ncpus < 1) { 819 fprintf(stderr, "Invalid guest vCPUs (%d)\n", guest_ncpus); 820 exit(1); 821 } 822 823 max_vcpus = num_vcpus_allowed(ctx); 824 if (guest_ncpus > max_vcpus) { 825 fprintf(stderr, "%d vCPUs requested but only %d available\n", 826 guest_ncpus, max_vcpus); 827 exit(1); 828 } 829 830 fbsdrun_set_capabilities(ctx, BSP); 831 832 if (dump_guest_memory) 833 vm_set_memflags(ctx, VM_MEM_F_INCORE); 834 err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL); 835 if (err) { 836 fprintf(stderr, "Unable to setup memory (%d)\n", err); 837 exit(1); 838 } 839 840 error = init_msr(); 841 if (error) { 842 fprintf(stderr, "init_msr error %d", error); 843 exit(1); 844 } 845 846 init_mem(); 847 init_inout(); 848 pci_irq_init(ctx); 849 ioapic_init(ctx); 850 851 rtc_init(ctx, rtc_localtime); 852 sci_init(ctx); 853 854 /* 855 * Exit if a device emulation finds an error in it's initilization 856 */ 857 if (init_pci(ctx) != 0) 858 exit(1); 859 860 if (gdb_port != 0) 861 init_dbgport(gdb_port); 862 863 if (bvmcons) 864 init_bvmcons(); 865 866 error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip); 867 assert(error == 0); 868 869 /* 870 * build the guest tables, MP etc. 871 */ 872 if (mptgen) { 873 error = mptable_build(ctx, guest_ncpus); 874 if (error) 875 exit(1); 876 } 877 878 error = smbios_build(ctx); 879 assert(error == 0); 880 881 if (acpi) { 882 error = acpi_build(ctx, guest_ncpus); 883 assert(error == 0); 884 } 885 886 /* 887 * Change the proc title to include the VM name. 888 */ 889 setproctitle("%s", vmname); 890 891 /* 892 * Add CPU 0 893 */ 894 fbsdrun_addcpu(ctx, BSP, BSP, rip); 895 896 /* 897 * Head off to the main event dispatch loop 898 */ 899 mevent_dispatch(); 900 901 exit(1); 902} 903