2 * SPDX-License-Identifier: BSD-2-Clause
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
29 #include <sys/cdefs.h>
30 #include <sys/types.h>
31 #ifndef WITHOUT_CAPSICUM
32 #include <sys/capsicum.h>
36 #include <sys/socket.h>
44 #include <machine/atomic.h>
46 #ifndef WITHOUT_CAPSICUM
47 #include <capsicum_helpers.h>
61 #include <pthread_np.h>
77 #include "amd64/atkbdc.h"
84 #include "amd64/e820.h"
85 #include "amd64/fwctl.h"
89 #include "amd64/ioapic.h"
90 #include "amd64/kernemu_dev.h"
95 #include "amd64/mptbl.h"
99 #include "amd64/pci_irq.h"
100 #include "amd64/pci_lpc.h"
102 #include "qemu_fwcfg.h"
103 #include "smbiostbl.h"
104 #ifdef BHYVE_SNAPSHOT
105 #include "snapshot.h"
107 #include "tpm_device.h"
109 #include "amd64/rtc.h"
114 #include "amd64/xmsr.h"
117 #define MB (1024UL * 1024)
118 #define GB (1024UL * MB)
121 uint16_t cpu_cores, cpu_sockets, cpu_threads;
125 static char *progname;
126 static const int BSP = 0;
128 static cpuset_t cpumask;
130 static void vm_loop(struct vmctx *ctx, struct vcpu *vcpu);
132 static struct vcpu_info {
138 static cpuset_t **vcpumap;
145 "Usage: %s [-AaCDeHhPSuWwxY]\n"
146 " %*s [-c [[cpus=]numcpus][,sockets=n][,cores=n][,threads=n]]\n"
147 " %*s [-G port] [-k config_file] [-l lpc] [-m mem] [-o var=value]\n"
148 " %*s [-p vcpu:hostcpu] [-r file] [-s pci] [-U uuid] vmname\n"
149 " -A: create ACPI tables\n"
150 " -a: local apic is in xAPIC mode (deprecated)\n"
151 " -C: include guest memory in core file\n"
152 " -c: number of CPUs and/or topology specification\n"
153 " -D: destroy on power-off\n"
154 " -e: exit on unhandled I/O access\n"
155 " -G: start a debug server\n"
156 " -H: vmexit from the guest on HLT\n"
158 " -k: key=value flat config file\n"
159 " -K: PS2 keyboard layout\n"
160 " -l: LPC device configuration\n"
162 " -o: set config 'var' to 'value'\n"
163 " -P: vmexit from the guest on pause\n"
164 " -p: pin 'vcpu' to 'hostcpu'\n"
165 #ifdef BHYVE_SNAPSHOT
166 " -r: path to checkpoint file\n"
168 " -S: guest memory cannot be swapped\n"
169 " -s: <slot,driver,configinfo> PCI slot config\n"
171 " -u: RTC keeps UTC time\n"
172 " -W: force virtio to use single-vector MSI\n"
173 " -w: ignore unimplemented MSRs\n"
174 " -x: local APIC is in x2APIC mode\n"
175 " -Y: disable MPtable generation\n",
176 progname, (int)strlen(progname), "", (int)strlen(progname), "",
177 (int)strlen(progname), "");
183 * XXX This parser is known to have the following issues:
184 * 1. It accepts null key=value tokens ",," as setting "cpus" to an
187 * The acceptance of a null specification ('-c ""') is by design to match the
188 * manual page syntax specification, this results in a topology of 1 vCPU.
191 topology_parse(const char *opt)
193 char *cp, *str, *tofree;
196 set_config_value("sockets", "1");
197 set_config_value("cores", "1");
198 set_config_value("threads", "1");
199 set_config_value("cpus", "1");
203 tofree = str = strdup(opt);
205 errx(4, "Failed to allocate memory");
207 while ((cp = strsep(&str, ",")) != NULL) {
208 if (strncmp(cp, "cpus=", strlen("cpus=")) == 0)
209 set_config_value("cpus", cp + strlen("cpus="));
210 else if (strncmp(cp, "sockets=", strlen("sockets=")) == 0)
211 set_config_value("sockets", cp + strlen("sockets="));
212 else if (strncmp(cp, "cores=", strlen("cores=")) == 0)
213 set_config_value("cores", cp + strlen("cores="));
214 else if (strncmp(cp, "threads=", strlen("threads=")) == 0)
215 set_config_value("threads", cp + strlen("threads="));
216 else if (strchr(cp, '=') != NULL)
219 set_config_value("cpus", cp);
230 parse_int_value(const char *key, const char *value, int minval, int maxval)
236 lval = strtol(value, &cp, 0);
237 if (errno != 0 || *cp != '\0' || cp == value || lval < minval ||
239 errx(4, "Invalid value for %s: '%s'", key, value);
244 * Set the sockets, cores, threads, and guest_cpus variables based on
245 * the configured topology.
247 * The limits of UINT16_MAX are due to the types passed to
248 * vm_set_topology(). vmm.ko may enforce tighter limits.
257 value = get_config_value("cpus");
259 guest_ncpus = parse_int_value("cpus", value, 1, UINT16_MAX);
260 explicit_cpus = true;
263 explicit_cpus = false;
265 value = get_config_value("cores");
267 cpu_cores = parse_int_value("cores", value, 1, UINT16_MAX);
270 value = get_config_value("threads");
272 cpu_threads = parse_int_value("threads", value, 1, UINT16_MAX);
275 value = get_config_value("sockets");
277 cpu_sockets = parse_int_value("sockets", value, 1, UINT16_MAX);
279 cpu_sockets = guest_ncpus;
282 * Compute sockets * cores * threads avoiding overflow. The
283 * range check above insures these are 16 bit values.
285 ncpus = (uint64_t)cpu_sockets * cpu_cores * cpu_threads;
286 if (ncpus > UINT16_MAX)
287 errx(4, "Computed number of vCPUs too high: %ju",
291 if (guest_ncpus != (int)ncpus)
292 errx(4, "Topology (%d sockets, %d cores, %d threads) "
293 "does not match %d vCPUs",
294 cpu_sockets, cpu_cores, cpu_threads,
301 pincpu_parse(const char *opt)
308 if (sscanf(opt, "%d:%d", &vcpu, &pcpu) != 2) {
309 fprintf(stderr, "invalid format: %s\n", opt);
314 fprintf(stderr, "invalid vcpu '%d'\n", vcpu);
318 if (pcpu < 0 || pcpu >= CPU_SETSIZE) {
319 fprintf(stderr, "hostcpu '%d' outside valid range from "
320 "0 to %d\n", pcpu, CPU_SETSIZE - 1);
324 snprintf(key, sizeof(key), "vcpu.%d.cpuset", vcpu);
325 value = get_config_value(key);
327 if (asprintf(&newval, "%s%s%d", value != NULL ? value : "",
328 value != NULL ? "," : "", pcpu) == -1) {
329 perror("failed to build new cpuset string");
333 set_config_value(key, newval);
339 parse_cpuset(int vcpu, const char *list, cpuset_t *set)
346 token = __DECONST(char *, list);
348 pcpu = strtoul(token, &cp, 0);
350 errx(4, "invalid cpuset for vcpu %d: '%s'", vcpu, list);
351 if (pcpu < 0 || pcpu >= CPU_SETSIZE)
352 errx(4, "hostcpu '%d' outside valid range from 0 to %d",
353 pcpu, CPU_SETSIZE - 1);
359 errx(4, "Invalid hostcpu range %d-%d",
361 while (start < pcpu) {
371 errx(4, "invalid cpuset for vcpu %d: '%s'",
376 errx(4, "invalid cpuset for vcpu %d: '%s'", vcpu, list);
391 vcpumap = calloc(guest_ncpus, sizeof(*vcpumap));
392 for (vcpu = 0; vcpu < guest_ncpus; vcpu++) {
393 snprintf(key, sizeof(key), "vcpu.%d.cpuset", vcpu);
394 value = get_config_value(key);
397 vcpumap[vcpu] = malloc(sizeof(cpuset_t));
398 if (vcpumap[vcpu] == NULL)
399 err(4, "Failed to allocate cpuset for vcpu %d", vcpu);
400 parse_cpuset(vcpu, value, vcpumap[vcpu]);
405 vm_inject_fault(struct vcpu *vcpu, int vector, int errcode_valid,
408 int error, restart_instruction;
410 restart_instruction = 1;
412 error = vm_inject_exception(vcpu, vector, errcode_valid, errcode,
413 restart_instruction);
418 paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len)
421 return (vm_map_gpa(ctx, gaddr, len));
424 #ifdef BHYVE_SNAPSHOT
426 paddr_host2guest(struct vmctx *ctx, void *addr)
428 return (vm_rev_map_gpa(ctx, addr));
433 fbsdrun_virtio_msix(void)
436 return (get_config_bool_default("virtio_msix", true));
440 fbsdrun_vcpu(int vcpuid)
442 return (vcpu_info[vcpuid].vcpu);
446 fbsdrun_start_thread(void *param)
448 char tname[MAXCOMLEN + 1];
449 struct vcpu_info *vi = param;
452 snprintf(tname, sizeof(tname), "vcpu %d", vi->vcpuid);
453 pthread_set_name_np(pthread_self(), tname);
455 if (vcpumap[vi->vcpuid] != NULL) {
456 error = pthread_setaffinity_np(pthread_self(),
457 sizeof(cpuset_t), vcpumap[vi->vcpuid]);
461 #ifdef BHYVE_SNAPSHOT
462 checkpoint_cpu_add(vi->vcpuid);
464 gdb_cpu_add(vi->vcpu);
466 vm_loop(vi->ctx, vi->vcpu);
474 fbsdrun_addcpu(struct vcpu_info *vi)
479 error = vm_activate_cpu(vi->vcpu);
481 err(EX_OSERR, "could not activate CPU %d", vi->vcpuid);
483 CPU_SET_ATOMIC(vi->vcpuid, &cpumask);
485 vm_suspend_cpu(vi->vcpu);
487 error = pthread_create(&thr, NULL, fbsdrun_start_thread, vi);
492 fbsdrun_deletecpu(int vcpu)
494 static pthread_mutex_t resetcpu_mtx = PTHREAD_MUTEX_INITIALIZER;
495 static pthread_cond_t resetcpu_cond = PTHREAD_COND_INITIALIZER;
497 pthread_mutex_lock(&resetcpu_mtx);
498 if (!CPU_ISSET(vcpu, &cpumask)) {
499 fprintf(stderr, "Attempting to delete unknown cpu %d\n", vcpu);
503 CPU_CLR(vcpu, &cpumask);
506 pthread_cond_signal(&resetcpu_cond);
507 pthread_mutex_unlock(&resetcpu_mtx);
512 while (!CPU_EMPTY(&cpumask)) {
513 pthread_cond_wait(&resetcpu_cond, &resetcpu_mtx);
515 pthread_mutex_unlock(&resetcpu_mtx);
519 fbsdrun_suspendcpu(int vcpuid)
521 return (vm_suspend_cpu(vcpu_info[vcpuid].vcpu));
525 vm_loop(struct vmctx *ctx, struct vcpu *vcpu)
530 enum vm_exitcode exitcode;
531 cpuset_t active_cpus, dmask;
533 error = vm_active_cpus(ctx, &active_cpus);
534 assert(CPU_ISSET(vcpu_id(vcpu), &active_cpus));
536 vmrun.vm_exit = &vme;
537 vmrun.cpuset = &dmask;
538 vmrun.cpusetsize = sizeof(dmask);
541 error = vm_run(vcpu, &vmrun);
545 exitcode = vme.exitcode;
546 if (exitcode >= VM_EXITCODE_MAX ||
547 vmexit_handlers[exitcode] == NULL) {
548 warnx("vm_loop: unexpected exitcode 0x%x", exitcode);
552 rc = (*vmexit_handlers[exitcode])(ctx, vcpu, &vmrun);
555 case VMEXIT_CONTINUE:
563 fprintf(stderr, "vm_run error %d, errno %d\n", error, errno);
567 num_vcpus_allowed(struct vmctx *ctx, struct vcpu *vcpu)
569 uint16_t sockets, cores, threads, maxcpus;
573 * The guest is allowed to spinup more than one processor only if the
574 * UNRESTRICTED_GUEST capability is available.
576 error = vm_get_capability(vcpu, VM_CAP_UNRESTRICTED_GUEST, &tmp);
580 error = vm_get_topology(ctx, &sockets, &cores, &threads, &maxcpus);
588 fbsdrun_set_capabilities(struct vcpu *vcpu)
592 if (get_config_bool_default("x86.vmexit_on_hlt", false)) {
593 err = vm_get_capability(vcpu, VM_CAP_HALT_EXIT, &tmp);
595 fprintf(stderr, "VM exit on HLT not supported\n");
598 vm_set_capability(vcpu, VM_CAP_HALT_EXIT, 1);
601 if (get_config_bool_default("x86.vmexit_on_pause", false)) {
603 * pause exit support required for this mode
605 err = vm_get_capability(vcpu, VM_CAP_PAUSE_EXIT, &tmp);
608 "SMP mux requested, no pause support\n");
611 vm_set_capability(vcpu, VM_CAP_PAUSE_EXIT, 1);
614 if (get_config_bool_default("x86.x2apic", false))
615 err = vm_set_x2apic_state(vcpu, X2APIC_ENABLED);
617 err = vm_set_x2apic_state(vcpu, X2APIC_DISABLED);
620 fprintf(stderr, "Unable to set x2apic state (%d)\n", err);
624 vm_set_capability(vcpu, VM_CAP_ENABLE_INVPCID, 1);
626 err = vm_set_capability(vcpu, VM_CAP_IPI_EXIT, 1);
630 static struct vmctx *
631 do_open(const char *vmname)
635 bool reinit, romboot;
637 reinit = romboot = false;
644 error = vm_create(vmname);
646 if (errno == EEXIST) {
651 * The virtual machine has been setup by the
652 * userspace bootloader.
662 * If the virtual machine was just created then a
663 * bootrom must be configured to boot it.
665 fprintf(stderr, "virtual machine cannot be booted\n");
670 ctx = vm_open(vmname);
676 #ifndef WITHOUT_CAPSICUM
677 if (vm_limit_rights(ctx) != 0)
678 err(EX_OSERR, "vm_limit_rights");
682 error = vm_reinit(ctx);
688 error = vm_set_topology(ctx, cpu_sockets, cpu_cores, cpu_threads, 0);
690 errx(EX_OSERR, "vm_set_topology");
695 spinup_vcpu(struct vcpu_info *vi, bool bsp)
700 fbsdrun_set_capabilities(vi->vcpu);
703 * Enable the 'unrestricted guest' mode for APs.
705 * APs startup in power-on 16-bit mode.
707 error = vm_set_capability(vi->vcpu, VM_CAP_UNRESTRICTED_GUEST, 1);
715 parse_config_option(const char *option)
720 value = strchr(option, '=');
721 if (value == NULL || value[1] == '\0')
723 path = strndup(option, value - option);
725 err(4, "Failed to allocate memory");
726 set_config_value(path, value + 1);
731 parse_simple_config_file(const char *path)
738 fp = fopen(path, "r");
740 err(4, "Failed to open configuration file %s", path);
744 for (lineno = 1; getline(&line, &linecap, fp) > 0; lineno++) {
745 if (*line == '#' || *line == '\n')
747 cp = strchr(line, '\n');
750 if (!parse_config_option(line))
751 errx(4, "%s line %u: invalid config option '%s'", path,
759 parse_gdb_options(const char *opt)
765 set_config_bool("gdb.wait", true);
769 colon = strrchr(opt, ':');
776 set_config_value("gdb.address", opt);
779 set_config_value("gdb.port", sport);
786 set_config_bool("acpi_tables", true);
787 set_config_bool("acpi_tables_in_memory", true);
788 set_config_value("memory.size", "256M");
789 set_config_bool("x86.strictmsr", true);
790 set_config_value("lpc.fwcfg", "bhyve");
794 main(int argc, char *argv[])
797 int max_vcpus, memflags;
801 const char *optstr, *value, *vmname;
802 #ifdef BHYVE_SNAPSHOT
804 struct restore_state rstate;
811 progname = basename(argv[0]);
813 #ifdef BHYVE_SNAPSHOT
814 optstr = "aehuwxACDHIPSWYk:f:o:p:G:c:s:m:l:K:U:r:";
816 optstr = "aehuwxACDHIPSWYk:f:o:p:G:c:s:m:l:K:U:";
818 while ((c = getopt(argc, argv, optstr)) != -1) {
821 set_config_bool("x86.x2apic", false);
825 * NOP. For backward compatibility. Most systems don't
826 * work properly without sane ACPI tables. Therefore,
827 * we're always generating them.
831 set_config_bool("destroy_on_poweroff", true);
834 if (pincpu_parse(optarg) != 0) {
835 errx(EX_USAGE, "invalid vcpu pinning "
836 "configuration '%s'", optarg);
840 if (topology_parse(optarg) != 0) {
841 errx(EX_USAGE, "invalid cpu topology "
846 set_config_bool("memory.guest_in_core", true);
849 if (qemu_fwcfg_parse_cmdline_arg(optarg) != 0) {
850 errx(EX_USAGE, "invalid fwcfg item '%s'", optarg);
854 parse_gdb_options(optarg);
857 parse_simple_config_file(optarg);
860 set_config_value("keyboard.layout", optarg);
864 if (strncmp(optarg, "help", strlen(optarg)) == 0) {
865 lpc_print_supported_devices();
867 } else if (lpc_device_parse(optarg) != 0) {
868 errx(EX_USAGE, "invalid lpc device "
869 "configuration '%s'", optarg);
873 #ifdef BHYVE_SNAPSHOT
875 restore_file = optarg;
879 if (strncmp(optarg, "help", strlen(optarg)) == 0) {
880 pci_print_supported_devices();
882 } else if (pci_parse_slot(optarg) != 0)
887 set_config_bool("memory.wired", true);
890 set_config_value("memory.size", optarg);
893 if (!parse_config_option(optarg))
894 errx(EX_USAGE, "invalid configuration option '%s'", optarg);
897 set_config_bool("x86.vmexit_on_hlt", true);
901 * The "-I" option was used to add an ioapic to the
904 * An ioapic is now provided unconditionally for each
905 * virtual machine and this option is now deprecated.
909 set_config_bool("x86.vmexit_on_pause", true);
912 set_config_bool("x86.strictio", true);
916 set_config_bool("rtc.use_localtime", false);
920 set_config_value("uuid", optarg);
923 set_config_bool("x86.strictmsr", false);
926 set_config_bool("virtio_msix", false);
929 set_config_bool("x86.x2apic", true);
933 set_config_bool("x86.mptable", false);
948 #ifdef BHYVE_SNAPSHOT
949 if (restore_file != NULL) {
950 error = load_restore_file(restore_file, &rstate);
952 fprintf(stderr, "Failed to read checkpoint info from "
953 "file: '%s'.\n", restore_file);
956 vmname = lookup_vmname(&rstate);
958 set_config_value("name", vmname);
963 set_config_value("name", argv[0]);
965 vmname = get_config_value("name");
969 if (get_config_bool_default("config.dump", false)) {
977 value = get_config_value("memory.size");
978 error = vm_parse_memsize(value, &memsize);
980 errx(EX_USAGE, "invalid memsize '%s'", value);
982 ctx = do_open(vmname);
984 #ifdef BHYVE_SNAPSHOT
985 if (restore_file != NULL) {
986 guest_ncpus = lookup_guest_ncpus(&rstate);
987 memflags = lookup_memflags(&rstate);
988 memsize = lookup_memsize(&rstate);
991 if (guest_ncpus < 1) {
992 fprintf(stderr, "Invalid guest vCPUs (%d)\n", guest_ncpus);
997 bsp = vm_vcpu_open(ctx, BSP);
998 max_vcpus = num_vcpus_allowed(ctx, bsp);
999 if (guest_ncpus > max_vcpus) {
1000 fprintf(stderr, "%d vCPUs requested but only %d available\n",
1001 guest_ncpus, max_vcpus);
1005 fbsdrun_set_capabilities(bsp);
1007 /* Allocate per-VCPU resources. */
1008 vcpu_info = calloc(guest_ncpus, sizeof(*vcpu_info));
1009 for (int vcpuid = 0; vcpuid < guest_ncpus; vcpuid++) {
1010 vcpu_info[vcpuid].ctx = ctx;
1011 vcpu_info[vcpuid].vcpuid = vcpuid;
1013 vcpu_info[vcpuid].vcpu = bsp;
1015 vcpu_info[vcpuid].vcpu = vm_vcpu_open(ctx, vcpuid);
1019 if (get_config_bool_default("memory.wired", false))
1020 memflags |= VM_MEM_F_WIRED;
1021 if (get_config_bool_default("memory.guest_in_core", false))
1022 memflags |= VM_MEM_F_INCORE;
1023 vm_set_memflags(ctx, memflags);
1024 error = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
1026 fprintf(stderr, "Unable to setup memory (%d)\n", errno);
1033 fprintf(stderr, "init_msr error %d", error);
1038 init_mem(guest_ncpus);
1055 if (qemu_fwcfg_init(ctx) != 0) {
1056 fprintf(stderr, "qemu fwcfg initialization error");
1060 if (qemu_fwcfg_add_file("opt/bhyve/hw.ncpu", sizeof(guest_ncpus),
1061 &guest_ncpus) != 0) {
1062 fprintf(stderr, "Could not add qemu fwcfg opt/bhyve/hw.ncpu");
1067 if (e820_init(ctx) != 0) {
1068 fprintf(stderr, "Unable to setup E820");
1074 * Exit if a device emulation finds an error in its initialization
1076 if (init_pci(ctx) != 0) {
1077 perror("device emulation initialization error");
1080 if (init_tpm(ctx) != 0) {
1081 fprintf(stderr, "Failed to init TPM device");
1086 * Initialize after PCI, to allow a bootrom file to reserve the high
1089 if (get_config_bool("acpi_tables"))
1095 if (lpc_bootrom()) {
1096 if (vm_set_capability(bsp, VM_CAP_UNRESTRICTED_GUEST, 1)) {
1097 fprintf(stderr, "ROM boot failed: unrestricted guest "
1098 "capability not available\n");
1101 error = vcpu_reset(bsp);
1109 for (int vcpuid = 0; vcpuid < guest_ncpus; vcpuid++)
1110 spinup_vcpu(&vcpu_info[vcpuid], vcpuid == BSP);
1112 #ifdef BHYVE_SNAPSHOT
1113 if (restore_file != NULL) {
1114 fprintf(stdout, "Pausing pci devs...\r\n");
1115 if (vm_pause_devices() != 0) {
1116 fprintf(stderr, "Failed to pause PCI device state.\n");
1120 fprintf(stdout, "Restoring vm mem...\r\n");
1121 if (restore_vm_mem(ctx, &rstate) != 0) {
1122 fprintf(stderr, "Failed to restore VM memory.\n");
1126 fprintf(stdout, "Restoring pci devs...\r\n");
1127 if (vm_restore_devices(&rstate) != 0) {
1128 fprintf(stderr, "Failed to restore PCI device state.\n");
1132 fprintf(stdout, "Restoring kernel structs...\r\n");
1133 if (vm_restore_kern_structs(ctx, &rstate) != 0) {
1134 fprintf(stderr, "Failed to restore kernel structs.\n");
1138 fprintf(stdout, "Resuming pci devs...\r\n");
1139 if (vm_resume_devices() != 0) {
1140 fprintf(stderr, "Failed to resume PCI device state.\n");
1147 if (get_config_bool_default("x86.mptable", true)) {
1148 error = mptable_build(ctx, guest_ncpus);
1150 perror("error to build the guest tables");
1156 error = smbios_build(ctx);
1160 if (get_config_bool("acpi_tables")) {
1161 error = acpi_build(ctx, guest_ncpus);
1166 error = e820_finalize();
1172 if (lpc_bootrom() && strcmp(lpc_fwcfg(), "bhyve") == 0) {
1178 * Change the proc title to include the VM name.
1180 setproctitle("%s", vmname);
1182 #ifdef BHYVE_SNAPSHOT
1183 /* initialize mutex/cond variables */
1187 * checkpointing thread for communication with bhyvectl
1189 if (init_checkpoint_thread(ctx) != 0)
1190 errx(EX_OSERR, "Failed to start checkpoint thread");
1193 #ifndef WITHOUT_CAPSICUM
1194 caph_cache_catpages();
1196 if (caph_limit_stdout() == -1 || caph_limit_stderr() == -1)
1197 errx(EX_OSERR, "Unable to apply rights for sandbox");
1199 if (caph_enter() == -1)
1200 errx(EX_OSERR, "cap_enter() failed");
1203 #ifdef BHYVE_SNAPSHOT
1204 if (restore_file != NULL) {
1205 destroy_restore_state(&rstate);
1206 if (vm_restore_time(ctx) < 0)
1207 err(EX_OSERR, "Unable to restore time");
1209 for (int vcpuid = 0; vcpuid < guest_ncpus; vcpuid++)
1210 vm_resume_cpu(vcpu_info[vcpuid].vcpu);
1216 * Head off to the main event dispatch loop