2 * Copyright (c) 2011 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #ifndef WITHOUT_CAPSICUM
34 #include <sys/capsicum.h>
39 #include <machine/atomic.h>
40 #include <machine/segments.h>
52 #include <pthread_np.h>
55 #ifndef WITHOUT_CAPSICUM
60 #include <machine/vmm.h>
61 #ifndef WITHOUT_CAPSICUM
62 #include <machine/vmm_dev.h>
79 #include "smbiostbl.h"
81 #include "spinup_ap.h"
84 #define GUEST_NIO_PORT 0x488 /* guest upcalls via i/o port */
86 #define MB (1024UL * 1024)
87 #define GB (1024UL * MB)
89 typedef int (*vmexit_handler_t)(struct vmctx *, struct vm_exit *, int *vcpu);
90 extern int vmexit_task_switch(struct vmctx *, struct vm_exit *, int *vcpu);
97 static int guest_vmexit_on_hlt, guest_vmexit_on_pause;
98 static int virtio_msix = 1;
99 static int x2apic_mode = 0; /* default is xAPIC */
102 static int strictmsr = 1;
106 static char *progname;
107 static const int BSP = 0;
109 static cpuset_t cpumask;
111 static void vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip);
113 static struct vm_exit vmexit[VM_MAXCPU];
116 uint64_t vmexit_bogus;
117 uint64_t vmexit_reqidle;
119 uint64_t vmexit_pause;
120 uint64_t vmexit_mtrap;
121 uint64_t vmexit_inst_emul;
122 uint64_t cpu_switch_rotate;
123 uint64_t cpu_switch_direct;
128 struct vmctx *mt_ctx;
130 } mt_vmm_info[VM_MAXCPU];
132 static cpuset_t *vcpumap[VM_MAXCPU] = { NULL };
139 "Usage: %s [-abehuwxACHPSWY] [-c vcpus] [-g <gdb port>] [-l <lpc>]\n"
140 " %*s [-m mem] [-p vcpu:hostcpu] [-s <pci>] [-U uuid] <vm>\n"
141 " -a: local apic is in xAPIC mode (deprecated)\n"
142 " -A: create ACPI tables\n"
143 " -c: # cpus (default 1)\n"
144 " -C: include guest memory in core file\n"
145 " -e: exit on unhandled I/O access\n"
148 " -H: vmexit from the guest on hlt\n"
149 " -l: LPC device configuration\n"
150 " -m: memory size in MB\n"
151 " -p: pin 'vcpu' to 'hostcpu'\n"
152 " -P: vmexit from the guest on pause\n"
153 " -s: <slot,driver,configinfo> PCI slot config\n"
154 " -S: guest memory cannot be swapped\n"
155 " -u: RTC keeps UTC time\n"
157 " -w: ignore unimplemented MSRs\n"
158 " -W: force virtio to use single-vector MSI\n"
159 " -x: local apic is in x2APIC mode\n"
160 " -Y: disable MPtable generation\n",
161 progname, (int)strlen(progname), "");
166 #ifndef WITHOUT_CAPSICUM
168 * 11-stable capsicum helpers
171 bhyve_caph_cache_catpages(void)
174 (void)catopen("libc", NL_CAT_LOCALE);
178 bhyve_caph_limit_stdoe(void)
181 unsigned long cmds[] = { TIOCGETA, TIOCGWINSZ };
182 int i, fds[] = { STDOUT_FILENO, STDERR_FILENO };
184 cap_rights_init(&rights, CAP_FCNTL, CAP_FSTAT, CAP_IOCTL);
185 cap_rights_set(&rights, CAP_WRITE);
187 for (i = 0; i < nitems(fds); i++) {
188 if (cap_rights_limit(fds[i], &rights) < 0 && errno != ENOSYS)
191 if (cap_ioctls_limit(fds[i], cmds, nitems(cmds)) < 0 && errno != ENOSYS)
194 if (cap_fcntls_limit(fds[i], CAP_FCNTL_GETFL) < 0 && errno != ENOSYS)
204 pincpu_parse(const char *opt)
208 if (sscanf(opt, "%d:%d", &vcpu, &pcpu) != 2) {
209 fprintf(stderr, "invalid format: %s\n", opt);
213 if (vcpu < 0 || vcpu >= VM_MAXCPU) {
214 fprintf(stderr, "vcpu '%d' outside valid range from 0 to %d\n",
215 vcpu, VM_MAXCPU - 1);
219 if (pcpu < 0 || pcpu >= CPU_SETSIZE) {
220 fprintf(stderr, "hostcpu '%d' outside valid range from "
221 "0 to %d\n", pcpu, CPU_SETSIZE - 1);
225 if (vcpumap[vcpu] == NULL) {
226 if ((vcpumap[vcpu] = malloc(sizeof(cpuset_t))) == NULL) {
230 CPU_ZERO(vcpumap[vcpu]);
232 CPU_SET(pcpu, vcpumap[vcpu]);
237 vm_inject_fault(void *arg, int vcpu, int vector, int errcode_valid,
241 int error, restart_instruction;
244 restart_instruction = 1;
246 error = vm_inject_exception(ctx, vcpu, vector, errcode_valid, errcode,
247 restart_instruction);
252 paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len)
255 return (vm_map_gpa(ctx, gaddr, len));
259 fbsdrun_vmexit_on_pause(void)
262 return (guest_vmexit_on_pause);
266 fbsdrun_vmexit_on_hlt(void)
269 return (guest_vmexit_on_hlt);
273 fbsdrun_virtio_msix(void)
276 return (virtio_msix);
280 fbsdrun_start_thread(void *param)
282 char tname[MAXCOMLEN + 1];
283 struct mt_vmm_info *mtp;
289 snprintf(tname, sizeof(tname), "vcpu %d", vcpu);
290 pthread_set_name_np(mtp->mt_thr, tname);
292 vm_loop(mtp->mt_ctx, vcpu, vmexit[vcpu].rip);
300 fbsdrun_addcpu(struct vmctx *ctx, int fromcpu, int newcpu, uint64_t rip)
304 assert(fromcpu == BSP);
307 * The 'newcpu' must be activated in the context of 'fromcpu'. If
308 * vm_activate_cpu() is delayed until newcpu's pthread starts running
309 * then vmm.ko is out-of-sync with bhyve and this can create a race
312 error = vm_activate_cpu(ctx, newcpu);
314 err(EX_OSERR, "could not activate CPU %d", newcpu);
316 CPU_SET_ATOMIC(newcpu, &cpumask);
319 * Set up the vmexit struct to allow execution to start
322 vmexit[newcpu].rip = rip;
323 vmexit[newcpu].inst_length = 0;
325 mt_vmm_info[newcpu].mt_ctx = ctx;
326 mt_vmm_info[newcpu].mt_vcpu = newcpu;
328 error = pthread_create(&mt_vmm_info[newcpu].mt_thr, NULL,
329 fbsdrun_start_thread, &mt_vmm_info[newcpu]);
334 fbsdrun_deletecpu(struct vmctx *ctx, int vcpu)
337 if (!CPU_ISSET(vcpu, &cpumask)) {
338 fprintf(stderr, "Attempting to delete unknown cpu %d\n", vcpu);
342 CPU_CLR_ATOMIC(vcpu, &cpumask);
343 return (CPU_EMPTY(&cpumask));
347 vmexit_handle_notify(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu,
352 * put guest-driven debug here
355 return (VMEXIT_CONTINUE);
359 vmexit_inout(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
362 int bytes, port, in, out;
367 port = vme->u.inout.port;
368 bytes = vme->u.inout.bytes;
369 in = vme->u.inout.in;
372 /* Extra-special case of host notifications */
373 if (out && port == GUEST_NIO_PORT) {
374 error = vmexit_handle_notify(ctx, vme, pvcpu, vme->u.inout.eax);
378 error = emulate_inout(ctx, vcpu, vme, strictio);
380 fprintf(stderr, "Unhandled %s%c 0x%04x at 0x%lx\n",
382 bytes == 1 ? 'b' : (bytes == 2 ? 'w' : 'l'),
384 return (VMEXIT_ABORT);
386 return (VMEXIT_CONTINUE);
391 vmexit_rdmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
398 error = emulate_rdmsr(ctx, *pvcpu, vme->u.msr.code, &val);
400 fprintf(stderr, "rdmsr to register %#x on vcpu %d\n",
401 vme->u.msr.code, *pvcpu);
403 vm_inject_gp(ctx, *pvcpu);
404 return (VMEXIT_CONTINUE);
409 error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RAX, eax);
413 error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RDX, edx);
416 return (VMEXIT_CONTINUE);
420 vmexit_wrmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
424 error = emulate_wrmsr(ctx, *pvcpu, vme->u.msr.code, vme->u.msr.wval);
426 fprintf(stderr, "wrmsr to register %#x(%#lx) on vcpu %d\n",
427 vme->u.msr.code, vme->u.msr.wval, *pvcpu);
429 vm_inject_gp(ctx, *pvcpu);
430 return (VMEXIT_CONTINUE);
433 return (VMEXIT_CONTINUE);
437 vmexit_spinup_ap(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
440 (void)spinup_ap(ctx, *pvcpu,
441 vme->u.spinup_ap.vcpu, vme->u.spinup_ap.rip);
443 return (VMEXIT_CONTINUE);
446 #define DEBUG_EPT_MISCONFIG
447 #ifdef DEBUG_EPT_MISCONFIG
448 #define EXIT_REASON_EPT_MISCONFIG 49
449 #define VMCS_GUEST_PHYSICAL_ADDRESS 0x00002400
450 #define VMCS_IDENT(x) ((x) | 0x80000000)
452 static uint64_t ept_misconfig_gpa, ept_misconfig_pte[4];
453 static int ept_misconfig_ptenum;
457 vmexit_vmx(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
460 fprintf(stderr, "vm exit[%d]\n", *pvcpu);
461 fprintf(stderr, "\treason\t\tVMX\n");
462 fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip);
463 fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length);
464 fprintf(stderr, "\tstatus\t\t%d\n", vmexit->u.vmx.status);
465 fprintf(stderr, "\texit_reason\t%u\n", vmexit->u.vmx.exit_reason);
466 fprintf(stderr, "\tqualification\t0x%016lx\n",
467 vmexit->u.vmx.exit_qualification);
468 fprintf(stderr, "\tinst_type\t\t%d\n", vmexit->u.vmx.inst_type);
469 fprintf(stderr, "\tinst_error\t\t%d\n", vmexit->u.vmx.inst_error);
470 #ifdef DEBUG_EPT_MISCONFIG
471 if (vmexit->u.vmx.exit_reason == EXIT_REASON_EPT_MISCONFIG) {
472 vm_get_register(ctx, *pvcpu,
473 VMCS_IDENT(VMCS_GUEST_PHYSICAL_ADDRESS),
475 vm_get_gpa_pmap(ctx, ept_misconfig_gpa, ept_misconfig_pte,
476 &ept_misconfig_ptenum);
477 fprintf(stderr, "\tEPT misconfiguration:\n");
478 fprintf(stderr, "\t\tGPA: %#lx\n", ept_misconfig_gpa);
479 fprintf(stderr, "\t\tPTE(%d): %#lx %#lx %#lx %#lx\n",
480 ept_misconfig_ptenum, ept_misconfig_pte[0],
481 ept_misconfig_pte[1], ept_misconfig_pte[2],
482 ept_misconfig_pte[3]);
484 #endif /* DEBUG_EPT_MISCONFIG */
485 return (VMEXIT_ABORT);
489 vmexit_svm(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
492 fprintf(stderr, "vm exit[%d]\n", *pvcpu);
493 fprintf(stderr, "\treason\t\tSVM\n");
494 fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip);
495 fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length);
496 fprintf(stderr, "\texitcode\t%#lx\n", vmexit->u.svm.exitcode);
497 fprintf(stderr, "\texitinfo1\t%#lx\n", vmexit->u.svm.exitinfo1);
498 fprintf(stderr, "\texitinfo2\t%#lx\n", vmexit->u.svm.exitinfo2);
499 return (VMEXIT_ABORT);
503 vmexit_bogus(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
506 assert(vmexit->inst_length == 0);
508 stats.vmexit_bogus++;
510 return (VMEXIT_CONTINUE);
514 vmexit_reqidle(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
517 assert(vmexit->inst_length == 0);
519 stats.vmexit_reqidle++;
521 return (VMEXIT_CONTINUE);
525 vmexit_hlt(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
531 * Just continue execution with the next instruction. We use
532 * the HLT VM exit as a way to be friendly with the host
535 return (VMEXIT_CONTINUE);
539 vmexit_pause(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
542 stats.vmexit_pause++;
544 return (VMEXIT_CONTINUE);
548 vmexit_mtrap(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
551 assert(vmexit->inst_length == 0);
553 stats.vmexit_mtrap++;
555 return (VMEXIT_CONTINUE);
559 vmexit_inst_emul(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
564 stats.vmexit_inst_emul++;
566 vie = &vmexit->u.inst_emul.vie;
567 err = emulate_mem(ctx, *pvcpu, vmexit->u.inst_emul.gpa,
568 vie, &vmexit->u.inst_emul.paging);
572 fprintf(stderr, "Unhandled memory access to 0x%lx\n",
573 vmexit->u.inst_emul.gpa);
576 fprintf(stderr, "Failed to emulate instruction [");
577 for (i = 0; i < vie->num_valid; i++) {
578 fprintf(stderr, "0x%02x%s", vie->inst[i],
579 i != (vie->num_valid - 1) ? " " : "");
581 fprintf(stderr, "] at 0x%lx\n", vmexit->rip);
582 return (VMEXIT_ABORT);
585 return (VMEXIT_CONTINUE);
588 static pthread_mutex_t resetcpu_mtx = PTHREAD_MUTEX_INITIALIZER;
589 static pthread_cond_t resetcpu_cond = PTHREAD_COND_INITIALIZER;
592 vmexit_suspend(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
594 enum vm_suspend_how how;
596 how = vmexit->u.suspended.how;
598 fbsdrun_deletecpu(ctx, *pvcpu);
601 pthread_mutex_lock(&resetcpu_mtx);
602 pthread_cond_signal(&resetcpu_cond);
603 pthread_mutex_unlock(&resetcpu_mtx);
607 pthread_mutex_lock(&resetcpu_mtx);
608 while (!CPU_EMPTY(&cpumask)) {
609 pthread_cond_wait(&resetcpu_cond, &resetcpu_mtx);
611 pthread_mutex_unlock(&resetcpu_mtx);
614 case VM_SUSPEND_RESET:
616 case VM_SUSPEND_POWEROFF:
618 case VM_SUSPEND_HALT:
620 case VM_SUSPEND_TRIPLEFAULT:
623 fprintf(stderr, "vmexit_suspend: invalid reason %d\n", how);
626 return (0); /* NOTREACHED */
629 static vmexit_handler_t handler[VM_EXITCODE_MAX] = {
630 [VM_EXITCODE_INOUT] = vmexit_inout,
631 [VM_EXITCODE_INOUT_STR] = vmexit_inout,
632 [VM_EXITCODE_VMX] = vmexit_vmx,
633 [VM_EXITCODE_SVM] = vmexit_svm,
634 [VM_EXITCODE_BOGUS] = vmexit_bogus,
635 [VM_EXITCODE_REQIDLE] = vmexit_reqidle,
636 [VM_EXITCODE_RDMSR] = vmexit_rdmsr,
637 [VM_EXITCODE_WRMSR] = vmexit_wrmsr,
638 [VM_EXITCODE_MTRAP] = vmexit_mtrap,
639 [VM_EXITCODE_INST_EMUL] = vmexit_inst_emul,
640 [VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap,
641 [VM_EXITCODE_SUSPENDED] = vmexit_suspend,
642 [VM_EXITCODE_TASK_SWITCH] = vmexit_task_switch,
646 vm_loop(struct vmctx *ctx, int vcpu, uint64_t startrip)
649 enum vm_exitcode exitcode;
650 cpuset_t active_cpus;
652 if (vcpumap[vcpu] != NULL) {
653 error = pthread_setaffinity_np(pthread_self(),
654 sizeof(cpuset_t), vcpumap[vcpu]);
658 error = vm_active_cpus(ctx, &active_cpus);
659 assert(CPU_ISSET(vcpu, &active_cpus));
661 error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RIP, startrip);
665 error = vm_run(ctx, vcpu, &vmexit[vcpu]);
669 exitcode = vmexit[vcpu].exitcode;
670 if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) {
671 fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n",
676 rc = (*handler[exitcode])(ctx, &vmexit[vcpu], &vcpu);
679 case VMEXIT_CONTINUE:
687 fprintf(stderr, "vm_run error %d, errno %d\n", error, errno);
691 num_vcpus_allowed(struct vmctx *ctx)
695 error = vm_get_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, &tmp);
698 * The guest is allowed to spinup more than one processor only if the
699 * UNRESTRICTED_GUEST capability is available.
708 fbsdrun_set_capabilities(struct vmctx *ctx, int cpu)
712 if (fbsdrun_vmexit_on_hlt()) {
713 err = vm_get_capability(ctx, cpu, VM_CAP_HALT_EXIT, &tmp);
715 fprintf(stderr, "VM exit on HLT not supported\n");
718 vm_set_capability(ctx, cpu, VM_CAP_HALT_EXIT, 1);
720 handler[VM_EXITCODE_HLT] = vmexit_hlt;
723 if (fbsdrun_vmexit_on_pause()) {
725 * pause exit support required for this mode
727 err = vm_get_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, &tmp);
730 "SMP mux requested, no pause support\n");
733 vm_set_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, 1);
735 handler[VM_EXITCODE_PAUSE] = vmexit_pause;
739 err = vm_set_x2apic_state(ctx, cpu, X2APIC_ENABLED);
741 err = vm_set_x2apic_state(ctx, cpu, X2APIC_DISABLED);
744 fprintf(stderr, "Unable to set x2apic state (%d)\n", err);
748 vm_set_capability(ctx, cpu, VM_CAP_ENABLE_INVPCID, 1);
751 static struct vmctx *
752 do_open(const char *vmname)
756 bool reinit, romboot;
757 #ifndef WITHOUT_CAPSICUM
759 const cap_ioctl_t *cmds;
763 reinit = romboot = false;
768 error = vm_create(vmname);
770 if (errno == EEXIST) {
775 * The virtual machine has been setup by the
776 * userspace bootloader.
786 * If the virtual machine was just created then a
787 * bootrom must be configured to boot it.
789 fprintf(stderr, "virtual machine cannot be booted\n");
794 ctx = vm_open(vmname);
800 #ifndef WITHOUT_CAPSICUM
801 cap_rights_init(&rights, CAP_IOCTL, CAP_MMAP_RW);
802 if (cap_rights_limit(vm_get_device_fd(ctx), &rights) == -1 &&
804 errx(EX_OSERR, "Unable to apply rights for sandbox");
805 vm_get_ioctls(&ncmds);
806 cmds = vm_get_ioctls(NULL);
808 errx(EX_OSERR, "out of memory");
809 if (cap_ioctls_limit(vm_get_device_fd(ctx), cmds, ncmds) == -1 &&
811 errx(EX_OSERR, "Unable to apply rights for sandbox");
812 free((cap_ioctl_t *)cmds);
816 error = vm_reinit(ctx);
826 main(int argc, char *argv[])
828 int c, error, gdb_port, err, bvmcons;
829 int max_vcpus, mptgen, memflags;
837 progname = basename(argv[0]);
845 optstr = "abehuwxACHIPSWYp:g:c:s:m:l:U:";
846 while ((c = getopt(argc, argv, optstr)) != -1) {
858 if (pincpu_parse(optarg) != 0) {
859 errx(EX_USAGE, "invalid vcpu pinning "
860 "configuration '%s'", optarg);
864 guest_ncpus = atoi(optarg);
867 memflags |= VM_MEM_F_INCORE;
870 gdb_port = atoi(optarg);
873 if (lpc_device_parse(optarg) != 0) {
874 errx(EX_USAGE, "invalid lpc device "
875 "configuration '%s'", optarg);
879 if (pci_parse_slot(optarg) != 0)
884 memflags |= VM_MEM_F_WIRED;
887 error = vm_parse_memsize(optarg, &memsize);
889 errx(EX_USAGE, "invalid memsize '%s'", optarg);
892 guest_vmexit_on_hlt = 1;
896 * The "-I" option was used to add an ioapic to the
899 * An ioapic is now provided unconditionally for each
900 * virtual machine and this option is now deprecated.
904 guest_vmexit_on_pause = 1;
913 guest_uuid_str = optarg;
940 ctx = do_open(vmname);
942 if (guest_ncpus < 1) {
943 fprintf(stderr, "Invalid guest vCPUs (%d)\n", guest_ncpus);
947 max_vcpus = num_vcpus_allowed(ctx);
948 if (guest_ncpus > max_vcpus) {
949 fprintf(stderr, "%d vCPUs requested but only %d available\n",
950 guest_ncpus, max_vcpus);
954 fbsdrun_set_capabilities(ctx, BSP);
956 vm_set_memflags(ctx, memflags);
957 err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
959 fprintf(stderr, "Unable to setup memory (%d)\n", errno);
965 fprintf(stderr, "init_msr error %d", error);
975 rtc_init(ctx, rtc_localtime);
979 * Exit if a device emulation finds an error in it's initilization
981 if (init_pci(ctx) != 0)
985 init_dbgport(gdb_port);
991 if (vm_set_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, 1)) {
992 fprintf(stderr, "ROM boot failed: unrestricted guest "
993 "capability not available\n");
996 error = vcpu_reset(ctx, BSP);
1000 error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip);
1004 * build the guest tables, MP etc.
1007 error = mptable_build(ctx, guest_ncpus);
1012 error = smbios_build(ctx);
1016 error = acpi_build(ctx, guest_ncpus);
1023 #ifndef WITHOUT_CAPSICUM
1026 if (bhyve_caph_limit_stdoe() == -1)
1027 errx(EX_OSERR, "Unable to apply rights for sandbox");
1029 if (cap_enter() == -1 && errno != ENOSYS)
1030 errx(EX_OSERR, "cap_enter() failed");
1034 * Change the proc title to include the VM name.
1036 setproctitle("%s", vmname);
1041 fbsdrun_addcpu(ctx, BSP, BSP, rip);
1044 * Head off to the main event dispatch loop