1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2011 NetApp, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28 /*
29 * This file and its contents are supplied under the terms of the
30 * Common Development and Distribution License ("CDDL"), version 1.0.
31 * You may only use this file in accordance with the terms of version
32 * 1.0 of the CDDL.
33 *
34 * A full copy of the text of the CDDL should have accompanied this
35 * source. A copy of the CDDL is also available via the Internet at
36 * http://www.illumos.org/license/CDDL.
37 *
38 * Copyright 2015 Pluribus Networks Inc.
39 * Copyright 2018 Joyent, Inc.
40 * Copyright 2025 Oxide Computer Company
41 * Copyright 2026 OmniOS Community Edition (OmniOSce) Association.
42 */
43
44
45 #include <sys/types.h>
46 #ifndef WITHOUT_CAPSICUM
47 #include <sys/capsicum.h>
48 #endif
49 #include <sys/mman.h>
50 #include <sys/time.h>
51
52 #ifdef __FreeBSD__
53 #include <amd64/vmm/intel/vmcs.h>
54 #else
55 #include <sys/cpuset.h>
56 #include <intel/vmcs.h>
57 #endif
58
59 #include <machine/atomic.h>
60
61 #ifndef WITHOUT_CAPSICUM
62 #include <capsicum_helpers.h>
63 #endif
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <err.h>
68 #include <errno.h>
69 #include <libgen.h>
70 #include <unistd.h>
71 #include <assert.h>
72 #include <pthread.h>
73 #include <pthread_np.h>
74 #include <sysexits.h>
75 #include <stdbool.h>
76 #include <stdint.h>
77
78 #include <machine/vmm.h>
79 #ifndef WITHOUT_CAPSICUM
80 #include <machine/vmm_dev.h>
81 #endif
82 #ifdef __FreeBSD__
83 #include <machine/vmm_instruction_emul.h>
84 #endif
85 #include <vmmapi.h>
86
87 #include "acpi.h"
88 #include "bhyverun.h"
89 #include "bootrom.h"
90 #include "config.h"
91 #include "debug.h"
92 #include "gdb.h"
93 #include "ioapic.h"
94 #include "mem.h"
95 #include "mevent.h"
96 #include "pci_emul.h"
97 #include "pci_lpc.h"
98 #include "qemu_fwcfg.h"
99 #include "tpm_device.h"
100 #include "spinup_ap.h"
101 #include "vmgenc.h"
102 #include "vmexit.h"
103 #ifndef __FreeBSD__
104 #include "smbiostbl.h"
105 #include "privileges.h"
106 #endif
107
108 #define MB (1024UL * 1024)
109 #define GB (1024UL * MB)
110
111 int guest_ncpus;
112 uint16_t cpu_cores, cpu_sockets, cpu_threads;
113
114 int raw_stdio = 0;
115
116 static const int BSP = 0;
117
118 static cpuset_t cpumask;
119
120 static void vm_loop(struct vmctx *ctx, struct vcpu *vcpu);
121
122 static struct vcpu_info {
123 struct vmctx *ctx;
124 struct vcpu *vcpu;
125 int vcpuid;
126 } *vcpu_info;
127
128 #ifdef __FreeBSD__
129 static cpuset_t **vcpumap;
130 #endif
131
132
133 /*
134 * XXX This parser is known to have the following issues:
135 * 1. It accepts null key=value tokens ",," as setting "cpus" to an
136 * empty string.
137 *
138 * The acceptance of a null specification ('-c ""') is by design to match the
139 * manual page syntax specification, this results in a topology of 1 vCPU.
140 */
141 int
bhyve_topology_parse(const char * opt)142 bhyve_topology_parse(const char *opt)
143 {
144 char *cp, *str, *tofree;
145
146 if (*opt == '\0') {
147 set_config_value("sockets", "1");
148 set_config_value("cores", "1");
149 set_config_value("threads", "1");
150 set_config_value("cpus", "1");
151 return (0);
152 }
153
154 tofree = str = strdup(opt);
155 if (str == NULL)
156 errx(4, "Failed to allocate memory");
157
158 while ((cp = strsep(&str, ",")) != NULL) {
159 if (strncmp(cp, "cpus=", strlen("cpus=")) == 0)
160 set_config_value("cpus", cp + strlen("cpus="));
161 else if (strncmp(cp, "sockets=", strlen("sockets=")) == 0)
162 set_config_value("sockets", cp + strlen("sockets="));
163 else if (strncmp(cp, "cores=", strlen("cores=")) == 0)
164 set_config_value("cores", cp + strlen("cores="));
165 else if (strncmp(cp, "threads=", strlen("threads=")) == 0)
166 set_config_value("threads", cp + strlen("threads="));
167 else if (strchr(cp, '=') != NULL)
168 goto out;
169 else
170 set_config_value("cpus", cp);
171 }
172 free(tofree);
173 return (0);
174
175 out:
176 free(tofree);
177 return (-1);
178 }
179
180 static int
parse_int_value(const char * key,const char * value,int minval,int maxval)181 parse_int_value(const char *key, const char *value, int minval, int maxval)
182 {
183 char *cp;
184 long lval;
185
186 errno = 0;
187 lval = strtol(value, &cp, 0);
188 if (errno != 0 || *cp != '\0' || cp == value || lval < minval ||
189 lval > maxval)
190 errx(4, "Invalid value for %s: '%s'", key, value);
191 return (lval);
192 }
193
194 /*
195 * Set the sockets, cores, threads, and guest_cpus variables based on
196 * the configured topology.
197 *
198 * The limits of UINT16_MAX are due to the types passed to
199 * vm_set_topology(). vmm.ko may enforce tighter limits.
200 */
201 static void
calc_topology(void)202 calc_topology(void)
203 {
204 const char *value;
205 bool explicit_cpus;
206 uint64_t ncpus;
207
208 value = get_config_value("cpus");
209 if (value != NULL) {
210 guest_ncpus = parse_int_value("cpus", value, 1, UINT16_MAX);
211 explicit_cpus = true;
212 } else {
213 guest_ncpus = 1;
214 explicit_cpus = false;
215 }
216 value = get_config_value("cores");
217 if (value != NULL)
218 cpu_cores = parse_int_value("cores", value, 1, UINT16_MAX);
219 else
220 cpu_cores = 1;
221 value = get_config_value("threads");
222 if (value != NULL)
223 cpu_threads = parse_int_value("threads", value, 1, UINT16_MAX);
224 else
225 cpu_threads = 1;
226 value = get_config_value("sockets");
227 if (value != NULL)
228 cpu_sockets = parse_int_value("sockets", value, 1, UINT16_MAX);
229 else
230 cpu_sockets = guest_ncpus;
231
232 /*
233 * Compute sockets * cores * threads avoiding overflow. The
234 * range check above insures these are 16 bit values.
235 */
236 ncpus = (uint64_t)cpu_sockets * cpu_cores * cpu_threads;
237 if (ncpus > UINT16_MAX)
238 errx(4, "Computed number of vCPUs too high: %ju",
239 (uintmax_t)ncpus);
240
241 if (explicit_cpus) {
242 if (guest_ncpus != (int)ncpus)
243 errx(4, "Topology (%d sockets, %d cores, %d threads) "
244 "does not match %d vCPUs",
245 cpu_sockets, cpu_cores, cpu_threads,
246 guest_ncpus);
247 } else
248 guest_ncpus = ncpus;
249 }
250
251 #ifdef __FreeBSD__
252 int
bhyve_pincpu_parse(const char * opt)253 bhyve_pincpu_parse(const char *opt)
254 {
255 int vcpu, pcpu;
256 const char *value;
257 char *newval;
258 char key[16];
259
260 if (sscanf(opt, "%d:%d", &vcpu, &pcpu) != 2) {
261 fprintf(stderr, "invalid format: %s\n", opt);
262 return (-1);
263 }
264
265 if (vcpu < 0) {
266 fprintf(stderr, "invalid vcpu '%d'\n", vcpu);
267 return (-1);
268 }
269
270 if (pcpu < 0 || pcpu >= CPU_SETSIZE) {
271 fprintf(stderr, "hostcpu '%d' outside valid range from "
272 "0 to %d\n", pcpu, CPU_SETSIZE - 1);
273 return (-1);
274 }
275
276 snprintf(key, sizeof(key), "vcpu.%d.cpuset", vcpu);
277 value = get_config_value(key);
278
279 if (asprintf(&newval, "%s%s%d", value != NULL ? value : "",
280 value != NULL ? "," : "", pcpu) == -1) {
281 perror("failed to build new cpuset string");
282 return (-1);
283 }
284
285 set_config_value(key, newval);
286 free(newval);
287 return (0);
288 }
289
290 static void
parse_cpuset(int vcpu,const char * list,cpuset_t * set)291 parse_cpuset(int vcpu, const char *list, cpuset_t *set)
292 {
293 char *cp, *token;
294 int pcpu, start;
295
296 CPU_ZERO(set);
297 start = -1;
298 token = __DECONST(char *, list);
299 for (;;) {
300 pcpu = strtoul(token, &cp, 0);
301 if (cp == token)
302 errx(4, "invalid cpuset for vcpu %d: '%s'", vcpu, list);
303 if (pcpu < 0 || pcpu >= CPU_SETSIZE)
304 errx(4, "hostcpu '%d' outside valid range from 0 to %d",
305 pcpu, CPU_SETSIZE - 1);
306 switch (*cp) {
307 case ',':
308 case '\0':
309 if (start >= 0) {
310 if (start > pcpu)
311 errx(4, "Invalid hostcpu range %d-%d",
312 start, pcpu);
313 while (start < pcpu) {
314 CPU_SET(start, set);
315 start++;
316 }
317 start = -1;
318 }
319 CPU_SET(pcpu, set);
320 break;
321 case '-':
322 if (start >= 0)
323 errx(4, "invalid cpuset for vcpu %d: '%s'",
324 vcpu, list);
325 start = pcpu;
326 break;
327 default:
328 errx(4, "invalid cpuset for vcpu %d: '%s'", vcpu, list);
329 }
330 if (*cp == '\0')
331 break;
332 token = cp + 1;
333 }
334 }
335
336 static void
build_vcpumaps(void)337 build_vcpumaps(void)
338 {
339 char key[16];
340 const char *value;
341 int vcpu;
342
343 vcpumap = calloc(guest_ncpus, sizeof(*vcpumap));
344 for (vcpu = 0; vcpu < guest_ncpus; vcpu++) {
345 snprintf(key, sizeof(key), "vcpu.%d.cpuset", vcpu);
346 value = get_config_value(key);
347 if (value == NULL)
348 continue;
349 vcpumap[vcpu] = malloc(sizeof(cpuset_t));
350 if (vcpumap[vcpu] == NULL)
351 err(4, "Failed to allocate cpuset for vcpu %d", vcpu);
352 parse_cpuset(vcpu, value, vcpumap[vcpu]);
353 }
354 }
355 #endif /* __FreeBSD__ */
356
357 void *
paddr_guest2host(struct vmctx * ctx,uintptr_t gaddr,size_t len)358 paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len)
359 {
360
361 return (vm_map_gpa(ctx, gaddr, len));
362 }
363
364 bool
fbsdrun_virtio_msix(void)365 fbsdrun_virtio_msix(void)
366 {
367 /*
368 * The configuration key used to be the non-namespaced `virtio_msix`.
369 * Check for that too in case anyone is using the old name in a
370 * configuration file. If either of these exists and is set to false
371 * we'll disable MSI-X for Virtio.
372 */
373 return (get_config_bool_default("virtio.msix", true) &&
374 get_config_bool_default("virtio_msix", true));
375 }
376
377 struct vcpu *
fbsdrun_vcpu(int vcpuid)378 fbsdrun_vcpu(int vcpuid)
379 {
380 return (vcpu_info[vcpuid].vcpu);
381 }
382
383 static void *
fbsdrun_start_thread(void * param)384 fbsdrun_start_thread(void *param)
385 {
386 char tname[MAXCOMLEN + 1];
387 struct vcpu_info *vi = param;
388 #ifdef __FreeBSD__
389 int error;
390 #endif
391
392 snprintf(tname, sizeof(tname), "vcpu %d", vi->vcpuid);
393 pthread_set_name_np(pthread_self(), tname);
394
395 #ifdef __FreeBSD__
396 if (vcpumap[vi->vcpuid] != NULL) {
397 error = pthread_setaffinity_np(pthread_self(),
398 sizeof(cpuset_t), vcpumap[vi->vcpuid]);
399 assert(error == 0);
400 }
401 #endif
402
403 gdb_cpu_add(vi->vcpu);
404
405 vm_loop(vi->ctx, vi->vcpu);
406
407 /* not reached */
408 exit(1);
409 return (NULL);
410 }
411
412 void
fbsdrun_addcpu(int vcpuid,bool suspend)413 fbsdrun_addcpu(int vcpuid, bool suspend)
414 {
415 struct vcpu_info *vi;
416 pthread_t thr;
417 int error;
418
419 vi = &vcpu_info[vcpuid];
420
421 error = vm_activate_cpu(vi->vcpu);
422 if (error != 0)
423 err(EX_OSERR, "could not activate CPU %d", vi->vcpuid);
424
425 CPU_SET_ATOMIC(vcpuid, &cpumask);
426
427 if (suspend) {
428 error = vm_suspend_cpu(vi->vcpu);
429 assert(error == 0);
430 }
431
432 error = pthread_create(&thr, NULL, fbsdrun_start_thread, vi);
433 assert(error == 0);
434 }
435
436 void
fbsdrun_deletecpu(int vcpu)437 fbsdrun_deletecpu(int vcpu)
438 {
439 static pthread_mutex_t resetcpu_mtx = PTHREAD_MUTEX_INITIALIZER;
440 static pthread_cond_t resetcpu_cond = PTHREAD_COND_INITIALIZER;
441
442 pthread_mutex_lock(&resetcpu_mtx);
443 if (!CPU_ISSET(vcpu, &cpumask)) {
444 EPRINTLN("Attempting to delete unknown cpu %d", vcpu);
445 exit(4);
446 }
447
448 CPU_CLR(vcpu, &cpumask);
449
450 if (vcpu != BSP) {
451 pthread_cond_signal(&resetcpu_cond);
452 pthread_mutex_unlock(&resetcpu_mtx);
453 pthread_exit(NULL);
454 /* NOTREACHED */
455 }
456
457 while (!CPU_EMPTY(&cpumask)) {
458 pthread_cond_wait(&resetcpu_cond, &resetcpu_mtx);
459 }
460 pthread_mutex_unlock(&resetcpu_mtx);
461 }
462
463 static void
vm_loop(struct vmctx * ctx,struct vcpu * vcpu)464 vm_loop(struct vmctx *ctx, struct vcpu *vcpu)
465 {
466 struct vm_exit vme;
467 int error, rc;
468 enum vm_exitcode exitcode;
469 cpuset_t active_cpus;
470 struct vm_entry *ventry;
471
472 error = vm_active_cpus(ctx, &active_cpus);
473 assert(CPU_ISSET(vcpu_id(vcpu), &active_cpus));
474
475 ventry = vmentry_vcpu(vcpu_id(vcpu));
476
477 while (1) {
478 error = vm_run(vcpu, ventry, &vme);
479 if (error != 0)
480 break;
481
482 if (ventry->cmd != VEC_DEFAULT) {
483 /*
484 * Discard any lingering entry state after it has been
485 * submitted via vm_run().
486 */
487 bzero(ventry, sizeof (*ventry));
488 }
489
490 exitcode = vme.exitcode;
491 if (exitcode >= VM_EXITCODE_MAX ||
492 vmexit_handlers[exitcode] == NULL) {
493 fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n",
494 exitcode);
495 exit(4);
496 }
497
498 rc = (*vmexit_handlers[exitcode])(ctx, vcpu, &vme);
499
500 switch (rc) {
501 case VMEXIT_CONTINUE:
502 break;
503 case VMEXIT_ABORT:
504 abort();
505 default:
506 exit(4);
507 }
508 }
509 EPRINTLN("vm_run error %d, errno %d", error, errno);
510 }
511
512 static int
num_vcpus_allowed(struct vmctx * ctx,struct vcpu * vcpu)513 num_vcpus_allowed(struct vmctx *ctx, struct vcpu *vcpu)
514 {
515 uint16_t sockets, cores, threads, maxcpus;
516 #ifdef __FreeBSD__
517 int tmp, error;
518
519 /*
520 * The guest is allowed to spinup more than one processor only if the
521 * UNRESTRICTED_GUEST capability is available.
522 */
523 error = vm_get_capability(vcpu, VM_CAP_UNRESTRICTED_GUEST, &tmp);
524 if (error != 0)
525 return (1);
526 #else
527 int error;
528 /* Unrestricted Guest is always enabled on illumos */
529
530 #endif /* __FreeBSD__ */
531
532 error = vm_get_topology(ctx, &sockets, &cores, &threads, &maxcpus);
533 if (error == 0)
534 return (maxcpus);
535 else
536 return (1);
537 }
538
539 static struct vmctx *
do_open(const char * vmname)540 do_open(const char *vmname)
541 {
542 struct vmctx *ctx;
543 int error;
544 bool reinit, romboot;
545
546 reinit = romboot = false;
547
548 romboot = bootrom_boot();
549
550 #ifndef __FreeBSD__
551 uint64_t create_flags = 0;
552 if (get_config_bool_default("memory.use_reservoir", false)) {
553 create_flags |= VCF_RESERVOIR_MEM;
554 }
555 error = vm_create(vmname, create_flags);
556 #else
557 error = vm_create(vmname);
558 #endif /* __FreeBSD__ */
559 if (error) {
560 if (errno == EEXIST) {
561 if (romboot) {
562 reinit = true;
563 } else {
564 /*
565 * The virtual machine has been setup by the
566 * userspace bootloader.
567 */
568 }
569 } else {
570 perror("vm_create");
571 exit(4);
572 }
573 } else {
574 if (!romboot) {
575 /*
576 * If the virtual machine was just created then a
577 * bootrom must be configured to boot it.
578 */
579 fprintf(stderr, "virtual machine cannot be booted\n");
580 exit(4);
581 }
582 }
583
584 ctx = vm_open(vmname);
585 if (ctx == NULL) {
586 perror("vm_open");
587 #ifndef __FreeBSD__
588 if (errno == ENOENT) {
589 fprintf(stderr,
590 "Does the VM already exist in the global or "
591 "another zone?\n");
592 }
593 #endif
594 exit(4);
595 }
596
597 #ifndef WITHOUT_CAPSICUM
598 if (vm_limit_rights(ctx) != 0)
599 err(EX_OSERR, "vm_limit_rights");
600 #endif
601
602 if (reinit) {
603 #ifndef __FreeBSD__
604 error = vm_reinit(ctx, 0);
605 #else
606 error = vm_reinit(ctx);
607 #endif
608 if (error) {
609 perror("vm_reinit");
610 exit(4);
611 }
612 }
613 error = vm_set_topology(ctx, cpu_sockets, cpu_cores, cpu_threads, 0);
614 if (error)
615 errx(EX_OSERR, "vm_set_topology");
616 return (ctx);
617 }
618
619 bool
bhyve_parse_config_option(const char * option)620 bhyve_parse_config_option(const char *option)
621 {
622 const char *value;
623 char *path;
624
625 value = strchr(option, '=');
626 if (value == NULL || value[1] == '\0')
627 return (false);
628 path = strndup(option, value - option);
629 if (path == NULL)
630 err(4, "Failed to allocate memory");
631 set_config_value(path, value + 1);
632 free(path);
633 return (true);
634 }
635
636 void
bhyve_parse_simple_config_file(const char * path)637 bhyve_parse_simple_config_file(const char *path)
638 {
639 FILE *fp;
640 char *line, *cp;
641 size_t linecap;
642 unsigned int lineno;
643
644 fp = fopen(path, "r");
645 if (fp == NULL)
646 err(4, "Failed to open configuration file %s", path);
647 line = NULL;
648 linecap = 0;
649 lineno = 1;
650 for (lineno = 1; getline(&line, &linecap, fp) > 0; lineno++) {
651 if (*line == '#' || *line == '\n')
652 continue;
653 cp = strchr(line, '\n');
654 if (cp != NULL)
655 *cp = '\0';
656 if (!bhyve_parse_config_option(line))
657 errx(4, "%s line %u: invalid config option '%s'", path,
658 lineno, line);
659 }
660 free(line);
661 fclose(fp);
662 }
663
664 void
bhyve_parse_gdb_options(const char * opt)665 bhyve_parse_gdb_options(const char *opt)
666 {
667 const char *sport;
668 char *colon;
669
670 if (opt[0] == 'w') {
671 set_config_bool("gdb.wait", true);
672 opt++;
673 }
674
675 colon = strrchr(opt, ':');
676 if (colon == NULL) {
677 sport = opt;
678 } else {
679 *colon = '\0';
680 colon++;
681 sport = colon;
682 set_config_value("gdb.address", opt);
683 }
684
685 set_config_value("gdb.port", sport);
686 }
687
688 int
main(int argc,char * argv[])689 main(int argc, char *argv[])
690 {
691 int error;
692 int max_vcpus, memflags;
693 struct vcpu *bsp;
694 struct vmctx *ctx;
695 size_t memsize;
696 const char *value, *vmname;
697
698 bhyve_init_config();
699
700 bhyve_optparse(argc, argv);
701 argc -= optind;
702 argv += optind;
703
704 if (argc > 1)
705 bhyve_usage(1);
706
707 if (argc == 1)
708 set_config_value("name", argv[0]);
709
710 vmname = get_config_value("name");
711 if (vmname == NULL)
712 bhyve_usage(1);
713
714 if (get_config_bool_default("config.dump", false)) {
715 dump_config();
716 exit(1);
717 }
718
719 #ifndef __FreeBSD__
720 illumos_priv_init();
721 #endif
722
723 calc_topology();
724
725 #ifdef __FreeBSD__
726 build_vcpumaps();
727 #endif
728
729 value = get_config_value("memory.size");
730 error = vm_parse_memsize(value, &memsize);
731 if (error)
732 errx(EX_USAGE, "invalid memsize '%s'", value);
733
734 ctx = do_open(vmname);
735
736 bsp = vm_vcpu_open(ctx, BSP);
737 max_vcpus = num_vcpus_allowed(ctx, bsp);
738 if (guest_ncpus > max_vcpus) {
739 fprintf(stderr, "%d vCPUs requested but only %d available\n",
740 guest_ncpus, max_vcpus);
741 exit(4);
742 }
743
744 bhyve_init_vcpu(bsp);
745
746 /* Allocate per-VCPU resources. */
747 vcpu_info = calloc(guest_ncpus, sizeof(*vcpu_info));
748 for (int vcpuid = 0; vcpuid < guest_ncpus; vcpuid++) {
749 vcpu_info[vcpuid].ctx = ctx;
750 vcpu_info[vcpuid].vcpuid = vcpuid;
751 if (vcpuid == BSP)
752 vcpu_info[vcpuid].vcpu = bsp;
753 else
754 vcpu_info[vcpuid].vcpu = vm_vcpu_open(ctx, vcpuid);
755 }
756
757 memflags = 0;
758 if (get_config_bool_default("memory.wired", false))
759 memflags |= VM_MEM_F_WIRED;
760 if (get_config_bool_default("memory.guest_in_core", false))
761 memflags |= VM_MEM_F_INCORE;
762 vm_set_memflags(ctx, memflags);
763 #ifdef __FreeBSD__
764 error = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
765 #else
766 int _errno;
767 do {
768 errno = 0;
769 error = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
770 _errno = errno;
771 if (error != 0 && _errno == ENOMEM) {
772 (void) fprintf(stderr, "Unable to allocate memory "
773 "(%llu), retrying in 1 second\n", memsize);
774 sleep(1);
775 }
776 } while (_errno == ENOMEM);
777 #endif
778 if (error) {
779 fprintf(stderr, "Unable to set up memory (%d)\n", errno);
780 exit(4);
781 }
782
783 init_mem(guest_ncpus);
784 init_bootrom(ctx);
785
786 if (bhyve_init_platform(ctx, bsp) != 0)
787 exit(4);
788
789 if (qemu_fwcfg_init(ctx) != 0) {
790 fprintf(stderr, "qemu fwcfg initialization error\n");
791 exit(4);
792 }
793
794 if (qemu_fwcfg_add_file("opt/bhyve/hw.ncpu", sizeof(guest_ncpus),
795 &guest_ncpus) != 0) {
796 fprintf(stderr, "Could not add qemu fwcfg opt/bhyve/hw.ncpu\n");
797 exit(4);
798 }
799
800 /*
801 * Exit if a device emulation finds an error in its initialization
802 */
803 if (init_pci(ctx) != 0) {
804 EPRINTLN("Device emulation initialization error: %s",
805 strerror(errno));
806 exit(4);
807 }
808 if (init_tpm(ctx) != 0) {
809 EPRINTLN("Failed to init TPM device");
810 exit(4);
811 }
812
813 /*
814 * Initialize after PCI, to allow a bootrom file to reserve the high
815 * region.
816 */
817 if (get_config_bool("acpi_tables"))
818 vmgenc_init(ctx);
819
820 #ifdef __FreeBSD__
821 init_gdb(ctx);
822 #else
823 if (value != NULL) {
824 int port = atoi(value);
825
826 if (port < 0)
827 init_mdb(ctx);
828 else
829 init_gdb(ctx);
830 }
831 #endif
832
833 if (bootrom_boot()) {
834 #ifdef __FreeBSD__
835 if (vm_set_capability(bsp, VM_CAP_UNRESTRICTED_GUEST, 1)) {
836 fprintf(stderr, "ROM boot failed: unrestricted guest "
837 "capability not available\n");
838 exit(4);
839 }
840 #else
841 /* Unrestricted Guest is always enabled on illumos */
842 #endif
843 error = vcpu_reset(bsp);
844 assert(error == 0);
845 }
846
847 if (bhyve_init_platform_late(ctx, bsp) != 0)
848 exit(4);
849
850 /*
851 * Change the proc title to include the VM name.
852 */
853 setproctitle("%s", vmname);
854
855 #ifndef WITHOUT_CAPSICUM
856 caph_cache_catpages();
857
858 if (caph_limit_stdout() == -1 || caph_limit_stderr() == -1)
859 errx(EX_OSERR, "Unable to apply rights for sandbox");
860
861 if (caph_enter() == -1)
862 errx(EX_OSERR, "cap_enter() failed");
863 #endif
864
865 #ifndef __FreeBSD__
866 illumos_priv_lock();
867 #endif
868
869 #ifndef __FreeBSD__
870 if (vmentry_init(guest_ncpus) != 0)
871 err(EX_OSERR, "vmentry_init() failed");
872 #endif
873
874 /*
875 * Add all vCPUs.
876 */
877 for (int vcpuid = 0; vcpuid < guest_ncpus; vcpuid++) {
878 #ifdef __FreeBSD__
879 bool suspend = (vcpuid != BSP);
880 #else
881 bool suspend = vcpuid == BSP &&
882 get_config_bool_default("suspend_at_boot", false);
883 #endif
884 bhyve_start_vcpu(vcpu_info[vcpuid].vcpu, vcpuid == BSP,
885 suspend);
886 }
887
888 /*
889 * Head off to the main event dispatch loop
890 */
891 mevent_dispatch();
892
893 exit(4);
894 }
895