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