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