2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 NetApp, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
35 #ifndef WITHOUT_CAPSICUM
36 #include <sys/capsicum.h>
41 #include <machine/atomic.h>
42 #include <machine/segments.h>
54 #include <pthread_np.h>
57 #ifndef WITHOUT_CAPSICUM
62 #include <machine/vmm.h>
63 #ifndef WITHOUT_CAPSICUM
64 #include <machine/vmm_dev.h>
81 #include "smbiostbl.h"
83 #include "spinup_ap.h"
86 #define GUEST_NIO_PORT 0x488 /* guest upcalls via i/o port */
88 #define MB (1024UL * 1024)
89 #define GB (1024UL * MB)
91 typedef int (*vmexit_handler_t)(struct vmctx *, struct vm_exit *, int *vcpu);
92 extern int vmexit_task_switch(struct vmctx *, struct vm_exit *, int *vcpu);
99 static int guest_vmexit_on_hlt, guest_vmexit_on_pause;
100 static int virtio_msix = 1;
101 static int x2apic_mode = 0; /* default is xAPIC */
104 static int strictmsr = 1;
108 static char *progname;
109 static const int BSP = 0;
111 static cpuset_t cpumask;
113 static void vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip);
115 static struct vm_exit vmexit[VM_MAXCPU];
118 uint64_t vmexit_bogus;
119 uint64_t vmexit_reqidle;
121 uint64_t vmexit_pause;
122 uint64_t vmexit_mtrap;
123 uint64_t vmexit_inst_emul;
124 uint64_t cpu_switch_rotate;
125 uint64_t cpu_switch_direct;
130 struct vmctx *mt_ctx;
132 } mt_vmm_info[VM_MAXCPU];
134 static cpuset_t *vcpumap[VM_MAXCPU] = { NULL };
141 "Usage: %s [-abehuwxACHPSWY] [-c vcpus] [-g <gdb port>] [-l <lpc>]\n"
142 " %*s [-m mem] [-p vcpu:hostcpu] [-s <pci>] [-U uuid] <vm>\n"
143 " -a: local apic is in xAPIC mode (deprecated)\n"
144 " -A: create ACPI tables\n"
145 " -c: # cpus (default 1)\n"
146 " -C: include guest memory in core file\n"
147 " -e: exit on unhandled I/O access\n"
150 " -H: vmexit from the guest on hlt\n"
151 " -l: LPC device configuration\n"
152 " -m: memory size in MB\n"
153 " -p: pin 'vcpu' to 'hostcpu'\n"
154 " -P: vmexit from the guest on pause\n"
155 " -s: <slot,driver,configinfo> PCI slot config\n"
156 " -S: guest memory cannot be swapped\n"
157 " -u: RTC keeps UTC time\n"
159 " -w: ignore unimplemented MSRs\n"
160 " -W: force virtio to use single-vector MSI\n"
161 " -x: local apic is in x2APIC mode\n"
162 " -Y: disable MPtable generation\n",
163 progname, (int)strlen(progname), "");
168 #ifndef WITHOUT_CAPSICUM
170 * 11-stable capsicum helpers
173 bhyve_caph_cache_catpages(void)
176 (void)catopen("libc", NL_CAT_LOCALE);
180 bhyve_caph_limit_stdoe(void)
183 unsigned long cmds[] = { TIOCGETA, TIOCGWINSZ };
184 int i, fds[] = { STDOUT_FILENO, STDERR_FILENO };
186 cap_rights_init(&rights, CAP_FCNTL, CAP_FSTAT, CAP_IOCTL);
187 cap_rights_set(&rights, CAP_WRITE);
189 for (i = 0; i < nitems(fds); i++) {
190 if (cap_rights_limit(fds[i], &rights) < 0 && errno != ENOSYS)
193 if (cap_ioctls_limit(fds[i], cmds, nitems(cmds)) < 0 && errno != ENOSYS)
196 if (cap_fcntls_limit(fds[i], CAP_FCNTL_GETFL) < 0 && errno != ENOSYS)
206 pincpu_parse(const char *opt)
210 if (sscanf(opt, "%d:%d", &vcpu, &pcpu) != 2) {
211 fprintf(stderr, "invalid format: %s\n", opt);
215 if (vcpu < 0 || vcpu >= VM_MAXCPU) {
216 fprintf(stderr, "vcpu '%d' outside valid range from 0 to %d\n",
217 vcpu, VM_MAXCPU - 1);
221 if (pcpu < 0 || pcpu >= CPU_SETSIZE) {
222 fprintf(stderr, "hostcpu '%d' outside valid range from "
223 "0 to %d\n", pcpu, CPU_SETSIZE - 1);
227 if (vcpumap[vcpu] == NULL) {
228 if ((vcpumap[vcpu] = malloc(sizeof(cpuset_t))) == NULL) {
232 CPU_ZERO(vcpumap[vcpu]);
234 CPU_SET(pcpu, vcpumap[vcpu]);
239 vm_inject_fault(void *arg, int vcpu, int vector, int errcode_valid,
243 int error, restart_instruction;
246 restart_instruction = 1;
248 error = vm_inject_exception(ctx, vcpu, vector, errcode_valid, errcode,
249 restart_instruction);
254 paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len)
257 return (vm_map_gpa(ctx, gaddr, len));
261 fbsdrun_vmexit_on_pause(void)
264 return (guest_vmexit_on_pause);
268 fbsdrun_vmexit_on_hlt(void)
271 return (guest_vmexit_on_hlt);
275 fbsdrun_virtio_msix(void)
278 return (virtio_msix);
282 fbsdrun_start_thread(void *param)
284 char tname[MAXCOMLEN + 1];
285 struct mt_vmm_info *mtp;
291 snprintf(tname, sizeof(tname), "vcpu %d", vcpu);
292 pthread_set_name_np(mtp->mt_thr, tname);
294 vm_loop(mtp->mt_ctx, vcpu, vmexit[vcpu].rip);
302 fbsdrun_addcpu(struct vmctx *ctx, int fromcpu, int newcpu, uint64_t rip)
306 assert(fromcpu == BSP);
309 * The 'newcpu' must be activated in the context of 'fromcpu'. If
310 * vm_activate_cpu() is delayed until newcpu's pthread starts running
311 * then vmm.ko is out-of-sync with bhyve and this can create a race
314 error = vm_activate_cpu(ctx, newcpu);
316 err(EX_OSERR, "could not activate CPU %d", newcpu);
318 CPU_SET_ATOMIC(newcpu, &cpumask);
321 * Set up the vmexit struct to allow execution to start
324 vmexit[newcpu].rip = rip;
325 vmexit[newcpu].inst_length = 0;
327 mt_vmm_info[newcpu].mt_ctx = ctx;
328 mt_vmm_info[newcpu].mt_vcpu = newcpu;
330 error = pthread_create(&mt_vmm_info[newcpu].mt_thr, NULL,
331 fbsdrun_start_thread, &mt_vmm_info[newcpu]);
336 fbsdrun_deletecpu(struct vmctx *ctx, int vcpu)
339 if (!CPU_ISSET(vcpu, &cpumask)) {
340 fprintf(stderr, "Attempting to delete unknown cpu %d\n", vcpu);
344 CPU_CLR_ATOMIC(vcpu, &cpumask);
345 return (CPU_EMPTY(&cpumask));
349 vmexit_handle_notify(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu,
354 * put guest-driven debug here
357 return (VMEXIT_CONTINUE);
361 vmexit_inout(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
364 int bytes, port, in, out;
369 port = vme->u.inout.port;
370 bytes = vme->u.inout.bytes;
371 in = vme->u.inout.in;
374 /* Extra-special case of host notifications */
375 if (out && port == GUEST_NIO_PORT) {
376 error = vmexit_handle_notify(ctx, vme, pvcpu, vme->u.inout.eax);
380 error = emulate_inout(ctx, vcpu, vme, strictio);
382 fprintf(stderr, "Unhandled %s%c 0x%04x at 0x%lx\n",
384 bytes == 1 ? 'b' : (bytes == 2 ? 'w' : 'l'),
386 return (VMEXIT_ABORT);
388 return (VMEXIT_CONTINUE);
393 vmexit_rdmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
400 error = emulate_rdmsr(ctx, *pvcpu, vme->u.msr.code, &val);
402 fprintf(stderr, "rdmsr to register %#x on vcpu %d\n",
403 vme->u.msr.code, *pvcpu);
405 vm_inject_gp(ctx, *pvcpu);
406 return (VMEXIT_CONTINUE);
411 error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RAX, eax);
415 error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RDX, edx);
418 return (VMEXIT_CONTINUE);
422 vmexit_wrmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
426 error = emulate_wrmsr(ctx, *pvcpu, vme->u.msr.code, vme->u.msr.wval);
428 fprintf(stderr, "wrmsr to register %#x(%#lx) on vcpu %d\n",
429 vme->u.msr.code, vme->u.msr.wval, *pvcpu);
431 vm_inject_gp(ctx, *pvcpu);
432 return (VMEXIT_CONTINUE);
435 return (VMEXIT_CONTINUE);
439 vmexit_spinup_ap(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
442 (void)spinup_ap(ctx, *pvcpu,
443 vme->u.spinup_ap.vcpu, vme->u.spinup_ap.rip);
445 return (VMEXIT_CONTINUE);
448 #define DEBUG_EPT_MISCONFIG
449 #ifdef DEBUG_EPT_MISCONFIG
450 #define EXIT_REASON_EPT_MISCONFIG 49
451 #define VMCS_GUEST_PHYSICAL_ADDRESS 0x00002400
452 #define VMCS_IDENT(x) ((x) | 0x80000000)
454 static uint64_t ept_misconfig_gpa, ept_misconfig_pte[4];
455 static int ept_misconfig_ptenum;
459 vmexit_vmx(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
462 fprintf(stderr, "vm exit[%d]\n", *pvcpu);
463 fprintf(stderr, "\treason\t\tVMX\n");
464 fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip);
465 fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length);
466 fprintf(stderr, "\tstatus\t\t%d\n", vmexit->u.vmx.status);
467 fprintf(stderr, "\texit_reason\t%u\n", vmexit->u.vmx.exit_reason);
468 fprintf(stderr, "\tqualification\t0x%016lx\n",
469 vmexit->u.vmx.exit_qualification);
470 fprintf(stderr, "\tinst_type\t\t%d\n", vmexit->u.vmx.inst_type);
471 fprintf(stderr, "\tinst_error\t\t%d\n", vmexit->u.vmx.inst_error);
472 #ifdef DEBUG_EPT_MISCONFIG
473 if (vmexit->u.vmx.exit_reason == EXIT_REASON_EPT_MISCONFIG) {
474 vm_get_register(ctx, *pvcpu,
475 VMCS_IDENT(VMCS_GUEST_PHYSICAL_ADDRESS),
477 vm_get_gpa_pmap(ctx, ept_misconfig_gpa, ept_misconfig_pte,
478 &ept_misconfig_ptenum);
479 fprintf(stderr, "\tEPT misconfiguration:\n");
480 fprintf(stderr, "\t\tGPA: %#lx\n", ept_misconfig_gpa);
481 fprintf(stderr, "\t\tPTE(%d): %#lx %#lx %#lx %#lx\n",
482 ept_misconfig_ptenum, ept_misconfig_pte[0],
483 ept_misconfig_pte[1], ept_misconfig_pte[2],
484 ept_misconfig_pte[3]);
486 #endif /* DEBUG_EPT_MISCONFIG */
487 return (VMEXIT_ABORT);
491 vmexit_svm(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
494 fprintf(stderr, "vm exit[%d]\n", *pvcpu);
495 fprintf(stderr, "\treason\t\tSVM\n");
496 fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip);
497 fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length);
498 fprintf(stderr, "\texitcode\t%#lx\n", vmexit->u.svm.exitcode);
499 fprintf(stderr, "\texitinfo1\t%#lx\n", vmexit->u.svm.exitinfo1);
500 fprintf(stderr, "\texitinfo2\t%#lx\n", vmexit->u.svm.exitinfo2);
501 return (VMEXIT_ABORT);
505 vmexit_bogus(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
508 assert(vmexit->inst_length == 0);
510 stats.vmexit_bogus++;
512 return (VMEXIT_CONTINUE);
516 vmexit_reqidle(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
519 assert(vmexit->inst_length == 0);
521 stats.vmexit_reqidle++;
523 return (VMEXIT_CONTINUE);
527 vmexit_hlt(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
533 * Just continue execution with the next instruction. We use
534 * the HLT VM exit as a way to be friendly with the host
537 return (VMEXIT_CONTINUE);
541 vmexit_pause(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
544 stats.vmexit_pause++;
546 return (VMEXIT_CONTINUE);
550 vmexit_mtrap(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
553 assert(vmexit->inst_length == 0);
555 stats.vmexit_mtrap++;
557 return (VMEXIT_CONTINUE);
561 vmexit_inst_emul(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
566 stats.vmexit_inst_emul++;
568 vie = &vmexit->u.inst_emul.vie;
569 err = emulate_mem(ctx, *pvcpu, vmexit->u.inst_emul.gpa,
570 vie, &vmexit->u.inst_emul.paging);
574 fprintf(stderr, "Unhandled memory access to 0x%lx\n",
575 vmexit->u.inst_emul.gpa);
578 fprintf(stderr, "Failed to emulate instruction [");
579 for (i = 0; i < vie->num_valid; i++) {
580 fprintf(stderr, "0x%02x%s", vie->inst[i],
581 i != (vie->num_valid - 1) ? " " : "");
583 fprintf(stderr, "] at 0x%lx\n", vmexit->rip);
584 return (VMEXIT_ABORT);
587 return (VMEXIT_CONTINUE);
590 static pthread_mutex_t resetcpu_mtx = PTHREAD_MUTEX_INITIALIZER;
591 static pthread_cond_t resetcpu_cond = PTHREAD_COND_INITIALIZER;
594 vmexit_suspend(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
596 enum vm_suspend_how how;
598 how = vmexit->u.suspended.how;
600 fbsdrun_deletecpu(ctx, *pvcpu);
603 pthread_mutex_lock(&resetcpu_mtx);
604 pthread_cond_signal(&resetcpu_cond);
605 pthread_mutex_unlock(&resetcpu_mtx);
609 pthread_mutex_lock(&resetcpu_mtx);
610 while (!CPU_EMPTY(&cpumask)) {
611 pthread_cond_wait(&resetcpu_cond, &resetcpu_mtx);
613 pthread_mutex_unlock(&resetcpu_mtx);
616 case VM_SUSPEND_RESET:
618 case VM_SUSPEND_POWEROFF:
620 case VM_SUSPEND_HALT:
622 case VM_SUSPEND_TRIPLEFAULT:
625 fprintf(stderr, "vmexit_suspend: invalid reason %d\n", how);
628 return (0); /* NOTREACHED */
631 static vmexit_handler_t handler[VM_EXITCODE_MAX] = {
632 [VM_EXITCODE_INOUT] = vmexit_inout,
633 [VM_EXITCODE_INOUT_STR] = vmexit_inout,
634 [VM_EXITCODE_VMX] = vmexit_vmx,
635 [VM_EXITCODE_SVM] = vmexit_svm,
636 [VM_EXITCODE_BOGUS] = vmexit_bogus,
637 [VM_EXITCODE_REQIDLE] = vmexit_reqidle,
638 [VM_EXITCODE_RDMSR] = vmexit_rdmsr,
639 [VM_EXITCODE_WRMSR] = vmexit_wrmsr,
640 [VM_EXITCODE_MTRAP] = vmexit_mtrap,
641 [VM_EXITCODE_INST_EMUL] = vmexit_inst_emul,
642 [VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap,
643 [VM_EXITCODE_SUSPENDED] = vmexit_suspend,
644 [VM_EXITCODE_TASK_SWITCH] = vmexit_task_switch,
648 vm_loop(struct vmctx *ctx, int vcpu, uint64_t startrip)
651 enum vm_exitcode exitcode;
652 cpuset_t active_cpus;
654 if (vcpumap[vcpu] != NULL) {
655 error = pthread_setaffinity_np(pthread_self(),
656 sizeof(cpuset_t), vcpumap[vcpu]);
660 error = vm_active_cpus(ctx, &active_cpus);
661 assert(CPU_ISSET(vcpu, &active_cpus));
663 error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RIP, startrip);
667 error = vm_run(ctx, vcpu, &vmexit[vcpu]);
671 exitcode = vmexit[vcpu].exitcode;
672 if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) {
673 fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n",
678 rc = (*handler[exitcode])(ctx, &vmexit[vcpu], &vcpu);
681 case VMEXIT_CONTINUE:
689 fprintf(stderr, "vm_run error %d, errno %d\n", error, errno);
693 num_vcpus_allowed(struct vmctx *ctx)
697 error = vm_get_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, &tmp);
700 * The guest is allowed to spinup more than one processor only if the
701 * UNRESTRICTED_GUEST capability is available.
710 fbsdrun_set_capabilities(struct vmctx *ctx, int cpu)
714 if (fbsdrun_vmexit_on_hlt()) {
715 err = vm_get_capability(ctx, cpu, VM_CAP_HALT_EXIT, &tmp);
717 fprintf(stderr, "VM exit on HLT not supported\n");
720 vm_set_capability(ctx, cpu, VM_CAP_HALT_EXIT, 1);
722 handler[VM_EXITCODE_HLT] = vmexit_hlt;
725 if (fbsdrun_vmexit_on_pause()) {
727 * pause exit support required for this mode
729 err = vm_get_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, &tmp);
732 "SMP mux requested, no pause support\n");
735 vm_set_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, 1);
737 handler[VM_EXITCODE_PAUSE] = vmexit_pause;
741 err = vm_set_x2apic_state(ctx, cpu, X2APIC_ENABLED);
743 err = vm_set_x2apic_state(ctx, cpu, X2APIC_DISABLED);
746 fprintf(stderr, "Unable to set x2apic state (%d)\n", err);
750 vm_set_capability(ctx, cpu, VM_CAP_ENABLE_INVPCID, 1);
753 static struct vmctx *
754 do_open(const char *vmname)
758 bool reinit, romboot;
759 #ifndef WITHOUT_CAPSICUM
761 const cap_ioctl_t *cmds;
765 reinit = romboot = false;
770 error = vm_create(vmname);
772 if (errno == EEXIST) {
777 * The virtual machine has been setup by the
778 * userspace bootloader.
788 * If the virtual machine was just created then a
789 * bootrom must be configured to boot it.
791 fprintf(stderr, "virtual machine cannot be booted\n");
796 ctx = vm_open(vmname);
802 #ifndef WITHOUT_CAPSICUM
803 cap_rights_init(&rights, CAP_IOCTL, CAP_MMAP_RW);
804 if (cap_rights_limit(vm_get_device_fd(ctx), &rights) == -1 &&
806 errx(EX_OSERR, "Unable to apply rights for sandbox");
807 vm_get_ioctls(&ncmds);
808 cmds = vm_get_ioctls(NULL);
810 errx(EX_OSERR, "out of memory");
811 if (cap_ioctls_limit(vm_get_device_fd(ctx), cmds, ncmds) == -1 &&
813 errx(EX_OSERR, "Unable to apply rights for sandbox");
814 free((cap_ioctl_t *)cmds);
818 error = vm_reinit(ctx);
828 main(int argc, char *argv[])
830 int c, error, gdb_port, err, bvmcons;
831 int max_vcpus, mptgen, memflags;
839 progname = basename(argv[0]);
847 optstr = "abehuwxACHIPSWYp:g:c:s:m:l:U:";
848 while ((c = getopt(argc, argv, optstr)) != -1) {
860 if (pincpu_parse(optarg) != 0) {
861 errx(EX_USAGE, "invalid vcpu pinning "
862 "configuration '%s'", optarg);
866 guest_ncpus = atoi(optarg);
869 memflags |= VM_MEM_F_INCORE;
872 gdb_port = atoi(optarg);
875 if (lpc_device_parse(optarg) != 0) {
876 errx(EX_USAGE, "invalid lpc device "
877 "configuration '%s'", optarg);
881 if (pci_parse_slot(optarg) != 0)
886 memflags |= VM_MEM_F_WIRED;
889 error = vm_parse_memsize(optarg, &memsize);
891 errx(EX_USAGE, "invalid memsize '%s'", optarg);
894 guest_vmexit_on_hlt = 1;
898 * The "-I" option was used to add an ioapic to the
901 * An ioapic is now provided unconditionally for each
902 * virtual machine and this option is now deprecated.
906 guest_vmexit_on_pause = 1;
915 guest_uuid_str = optarg;
942 ctx = do_open(vmname);
944 if (guest_ncpus < 1) {
945 fprintf(stderr, "Invalid guest vCPUs (%d)\n", guest_ncpus);
949 max_vcpus = num_vcpus_allowed(ctx);
950 if (guest_ncpus > max_vcpus) {
951 fprintf(stderr, "%d vCPUs requested but only %d available\n",
952 guest_ncpus, max_vcpus);
956 fbsdrun_set_capabilities(ctx, BSP);
958 vm_set_memflags(ctx, memflags);
959 err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
961 fprintf(stderr, "Unable to setup memory (%d)\n", errno);
967 fprintf(stderr, "init_msr error %d", error);
977 rtc_init(ctx, rtc_localtime);
981 * Exit if a device emulation finds an error in it's initilization
983 if (init_pci(ctx) != 0)
987 init_dbgport(gdb_port);
993 if (vm_set_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, 1)) {
994 fprintf(stderr, "ROM boot failed: unrestricted guest "
995 "capability not available\n");
998 error = vcpu_reset(ctx, BSP);
1002 error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip);
1006 * build the guest tables, MP etc.
1009 error = mptable_build(ctx, guest_ncpus);
1014 error = smbios_build(ctx);
1018 error = acpi_build(ctx, guest_ncpus);
1025 #ifndef WITHOUT_CAPSICUM
1026 bhyve_caph_cache_catpages();
1028 if (bhyve_caph_limit_stdoe() == -1)
1029 errx(EX_OSERR, "Unable to apply rights for sandbox");
1031 if (cap_enter() == -1 && errno != ENOSYS)
1032 errx(EX_OSERR, "cap_enter() failed");
1036 * Change the proc title to include the VM name.
1038 setproctitle("%s", vmname);
1043 fbsdrun_addcpu(ctx, BSP, BSP, rip);
1046 * Head off to the main event dispatch loop