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 2022 Oxide Computer Company
41 * Copyright 2022 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 int
fbsdrun_virtio_msix(void)365 fbsdrun_virtio_msix(void)
366 {
367
368 return (get_config_bool_default("virtio_msix", true));
369 }
370
371 struct vcpu *
fbsdrun_vcpu(int vcpuid)372 fbsdrun_vcpu(int vcpuid)
373 {
374 return (vcpu_info[vcpuid].vcpu);
375 }
376
377 static void *
fbsdrun_start_thread(void * param)378 fbsdrun_start_thread(void *param)
379 {
380 char tname[MAXCOMLEN + 1];
381 struct vcpu_info *vi = param;
382 #ifdef __FreeBSD__
383 int error;
384 #endif
385
386 snprintf(tname, sizeof(tname), "vcpu %d", vi->vcpuid);
387 pthread_set_name_np(pthread_self(), tname);
388
389 #ifdef __FreeBSD__
390 if (vcpumap[vi->vcpuid] != NULL) {
391 error = pthread_setaffinity_np(pthread_self(),
392 sizeof(cpuset_t), vcpumap[vi->vcpuid]);
393 assert(error == 0);
394 }
395 #endif
396
397 gdb_cpu_add(vi->vcpu);
398
399 vm_loop(vi->ctx, vi->vcpu);
400
401 /* not reached */
402 exit(1);
403 return (NULL);
404 }
405
406 void
fbsdrun_addcpu(int vcpuid,bool suspend)407 fbsdrun_addcpu(int vcpuid, bool suspend)
408 {
409 struct vcpu_info *vi;
410 pthread_t thr;
411 int error;
412
413 vi = &vcpu_info[vcpuid];
414
415 error = vm_activate_cpu(vi->vcpu);
416 if (error != 0)
417 err(EX_OSERR, "could not activate CPU %d", vi->vcpuid);
418
419 CPU_SET_ATOMIC(vcpuid, &cpumask);
420
421 if (suspend) {
422 error = vm_suspend_cpu(vi->vcpu);
423 assert(error == 0);
424 }
425
426 error = pthread_create(&thr, NULL, fbsdrun_start_thread, vi);
427 assert(error == 0);
428 }
429
430 void
fbsdrun_deletecpu(int vcpu)431 fbsdrun_deletecpu(int vcpu)
432 {
433 static pthread_mutex_t resetcpu_mtx = PTHREAD_MUTEX_INITIALIZER;
434 static pthread_cond_t resetcpu_cond = PTHREAD_COND_INITIALIZER;
435
436 pthread_mutex_lock(&resetcpu_mtx);
437 if (!CPU_ISSET(vcpu, &cpumask)) {
438 EPRINTLN("Attempting to delete unknown cpu %d", vcpu);
439 exit(4);
440 }
441
442 CPU_CLR(vcpu, &cpumask);
443
444 if (vcpu != BSP) {
445 pthread_cond_signal(&resetcpu_cond);
446 pthread_mutex_unlock(&resetcpu_mtx);
447 pthread_exit(NULL);
448 /* NOTREACHED */
449 }
450
451 while (!CPU_EMPTY(&cpumask)) {
452 pthread_cond_wait(&resetcpu_cond, &resetcpu_mtx);
453 }
454 pthread_mutex_unlock(&resetcpu_mtx);
455 }
456
457 static void
vm_loop(struct vmctx * ctx,struct vcpu * vcpu)458 vm_loop(struct vmctx *ctx, struct vcpu *vcpu)
459 {
460 struct vm_exit vme;
461 int error, rc;
462 enum vm_exitcode exitcode;
463 cpuset_t active_cpus;
464 struct vm_entry *ventry;
465
466 error = vm_active_cpus(ctx, &active_cpus);
467 assert(CPU_ISSET(vcpu_id(vcpu), &active_cpus));
468
469 ventry = vmentry_vcpu(vcpu_id(vcpu));
470
471 while (1) {
472 error = vm_run(vcpu, ventry, &vme);
473 if (error != 0)
474 break;
475
476 if (ventry->cmd != VEC_DEFAULT) {
477 /*
478 * Discard any lingering entry state after it has been
479 * submitted via vm_run().
480 */
481 bzero(ventry, sizeof (*ventry));
482 }
483
484 exitcode = vme.exitcode;
485 if (exitcode >= VM_EXITCODE_MAX ||
486 vmexit_handlers[exitcode] == NULL) {
487 fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n",
488 exitcode);
489 exit(4);
490 }
491
492 rc = (*vmexit_handlers[exitcode])(ctx, vcpu, &vme);
493
494 switch (rc) {
495 case VMEXIT_CONTINUE:
496 break;
497 case VMEXIT_ABORT:
498 abort();
499 default:
500 exit(4);
501 }
502 }
503 EPRINTLN("vm_run error %d, errno %d", error, errno);
504 }
505
506 static int
num_vcpus_allowed(struct vmctx * ctx,struct vcpu * vcpu)507 num_vcpus_allowed(struct vmctx *ctx, struct vcpu *vcpu)
508 {
509 uint16_t sockets, cores, threads, maxcpus;
510 #ifdef __FreeBSD__
511 int tmp, error;
512
513 /*
514 * The guest is allowed to spinup more than one processor only if the
515 * UNRESTRICTED_GUEST capability is available.
516 */
517 error = vm_get_capability(vcpu, VM_CAP_UNRESTRICTED_GUEST, &tmp);
518 if (error != 0)
519 return (1);
520 #else
521 int error;
522 /* Unrestricted Guest is always enabled on illumos */
523
524 #endif /* __FreeBSD__ */
525
526 error = vm_get_topology(ctx, &sockets, &cores, &threads, &maxcpus);
527 if (error == 0)
528 return (maxcpus);
529 else
530 return (1);
531 }
532
533 static struct vmctx *
do_open(const char * vmname)534 do_open(const char *vmname)
535 {
536 struct vmctx *ctx;
537 int error;
538 bool reinit, romboot;
539
540 reinit = romboot = false;
541
542 romboot = bootrom_boot();
543
544 #ifndef __FreeBSD__
545 uint64_t create_flags = 0;
546 if (get_config_bool_default("memory.use_reservoir", false)) {
547 create_flags |= VCF_RESERVOIR_MEM;
548 }
549 error = vm_create(vmname, create_flags);
550 #else
551 error = vm_create(vmname);
552 #endif /* __FreeBSD__ */
553 if (error) {
554 if (errno == EEXIST) {
555 if (romboot) {
556 reinit = true;
557 } else {
558 /*
559 * The virtual machine has been setup by the
560 * userspace bootloader.
561 */
562 }
563 } else {
564 perror("vm_create");
565 exit(4);
566 }
567 } else {
568 if (!romboot) {
569 /*
570 * If the virtual machine was just created then a
571 * bootrom must be configured to boot it.
572 */
573 fprintf(stderr, "virtual machine cannot be booted\n");
574 exit(4);
575 }
576 }
577
578 ctx = vm_open(vmname);
579 if (ctx == NULL) {
580 perror("vm_open");
581 exit(4);
582 }
583
584 #ifndef WITHOUT_CAPSICUM
585 if (vm_limit_rights(ctx) != 0)
586 err(EX_OSERR, "vm_limit_rights");
587 #endif
588
589 if (reinit) {
590 #ifndef __FreeBSD__
591 error = vm_reinit(ctx, 0);
592 #else
593 error = vm_reinit(ctx);
594 #endif
595 if (error) {
596 perror("vm_reinit");
597 exit(4);
598 }
599 }
600 error = vm_set_topology(ctx, cpu_sockets, cpu_cores, cpu_threads, 0);
601 if (error)
602 errx(EX_OSERR, "vm_set_topology");
603 return (ctx);
604 }
605
606 bool
bhyve_parse_config_option(const char * option)607 bhyve_parse_config_option(const char *option)
608 {
609 const char *value;
610 char *path;
611
612 value = strchr(option, '=');
613 if (value == NULL || value[1] == '\0')
614 return (false);
615 path = strndup(option, value - option);
616 if (path == NULL)
617 err(4, "Failed to allocate memory");
618 set_config_value(path, value + 1);
619 free(path);
620 return (true);
621 }
622
623 void
bhyve_parse_simple_config_file(const char * path)624 bhyve_parse_simple_config_file(const char *path)
625 {
626 FILE *fp;
627 char *line, *cp;
628 size_t linecap;
629 unsigned int lineno;
630
631 fp = fopen(path, "r");
632 if (fp == NULL)
633 err(4, "Failed to open configuration file %s", path);
634 line = NULL;
635 linecap = 0;
636 lineno = 1;
637 for (lineno = 1; getline(&line, &linecap, fp) > 0; lineno++) {
638 if (*line == '#' || *line == '\n')
639 continue;
640 cp = strchr(line, '\n');
641 if (cp != NULL)
642 *cp = '\0';
643 if (!bhyve_parse_config_option(line))
644 errx(4, "%s line %u: invalid config option '%s'", path,
645 lineno, line);
646 }
647 free(line);
648 fclose(fp);
649 }
650
651 void
bhyve_parse_gdb_options(const char * opt)652 bhyve_parse_gdb_options(const char *opt)
653 {
654 const char *sport;
655 char *colon;
656
657 if (opt[0] == 'w') {
658 set_config_bool("gdb.wait", true);
659 opt++;
660 }
661
662 colon = strrchr(opt, ':');
663 if (colon == NULL) {
664 sport = opt;
665 } else {
666 *colon = '\0';
667 colon++;
668 sport = colon;
669 set_config_value("gdb.address", opt);
670 }
671
672 set_config_value("gdb.port", sport);
673 }
674
675 int
main(int argc,char * argv[])676 main(int argc, char *argv[])
677 {
678 int error;
679 int max_vcpus, memflags;
680 struct vcpu *bsp;
681 struct vmctx *ctx;
682 size_t memsize;
683 const char *value, *vmname;
684
685 bhyve_init_config();
686
687 bhyve_optparse(argc, argv);
688 argc -= optind;
689 argv += optind;
690
691 if (argc > 1)
692 bhyve_usage(1);
693
694 if (argc == 1)
695 set_config_value("name", argv[0]);
696
697 vmname = get_config_value("name");
698 if (vmname == NULL)
699 bhyve_usage(1);
700
701 if (get_config_bool_default("config.dump", false)) {
702 dump_config();
703 exit(1);
704 }
705
706 #ifndef __FreeBSD__
707 illumos_priv_init();
708 #endif
709
710 calc_topology();
711
712 #ifdef __FreeBSD__
713 build_vcpumaps();
714 #endif
715
716 value = get_config_value("memory.size");
717 error = vm_parse_memsize(value, &memsize);
718 if (error)
719 errx(EX_USAGE, "invalid memsize '%s'", value);
720
721 ctx = do_open(vmname);
722
723 bsp = vm_vcpu_open(ctx, BSP);
724 max_vcpus = num_vcpus_allowed(ctx, bsp);
725 if (guest_ncpus > max_vcpus) {
726 fprintf(stderr, "%d vCPUs requested but only %d available\n",
727 guest_ncpus, max_vcpus);
728 exit(4);
729 }
730
731 bhyve_init_vcpu(bsp);
732
733 /* Allocate per-VCPU resources. */
734 vcpu_info = calloc(guest_ncpus, sizeof(*vcpu_info));
735 for (int vcpuid = 0; vcpuid < guest_ncpus; vcpuid++) {
736 vcpu_info[vcpuid].ctx = ctx;
737 vcpu_info[vcpuid].vcpuid = vcpuid;
738 if (vcpuid == BSP)
739 vcpu_info[vcpuid].vcpu = bsp;
740 else
741 vcpu_info[vcpuid].vcpu = vm_vcpu_open(ctx, vcpuid);
742 }
743
744 memflags = 0;
745 if (get_config_bool_default("memory.wired", false))
746 memflags |= VM_MEM_F_WIRED;
747 if (get_config_bool_default("memory.guest_in_core", false))
748 memflags |= VM_MEM_F_INCORE;
749 vm_set_memflags(ctx, memflags);
750 #ifdef __FreeBSD__
751 error = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
752 #else
753 int _errno;
754 do {
755 errno = 0;
756 error = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
757 _errno = errno;
758 if (error != 0 && _errno == ENOMEM) {
759 (void) fprintf(stderr, "Unable to allocate memory "
760 "(%llu), retrying in 1 second\n", memsize);
761 sleep(1);
762 }
763 } while (_errno == ENOMEM);
764 #endif
765 if (error) {
766 fprintf(stderr, "Unable to set up memory (%d)\n", errno);
767 exit(4);
768 }
769
770 init_mem(guest_ncpus);
771 init_bootrom(ctx);
772
773 if (bhyve_init_platform(ctx, bsp) != 0)
774 exit(4);
775
776 if (qemu_fwcfg_init(ctx) != 0) {
777 fprintf(stderr, "qemu fwcfg initialization error\n");
778 exit(4);
779 }
780
781 if (qemu_fwcfg_add_file("opt/bhyve/hw.ncpu", sizeof(guest_ncpus),
782 &guest_ncpus) != 0) {
783 fprintf(stderr, "Could not add qemu fwcfg opt/bhyve/hw.ncpu\n");
784 exit(4);
785 }
786
787 /*
788 * Exit if a device emulation finds an error in its initialization
789 */
790 if (init_pci(ctx) != 0) {
791 EPRINTLN("Device emulation initialization error: %s",
792 strerror(errno));
793 exit(4);
794 }
795 if (init_tpm(ctx) != 0) {
796 EPRINTLN("Failed to init TPM device");
797 exit(4);
798 }
799
800 /*
801 * Initialize after PCI, to allow a bootrom file to reserve the high
802 * region.
803 */
804 if (get_config_bool("acpi_tables"))
805 vmgenc_init(ctx);
806
807 #ifdef __FreeBSD__
808 init_gdb(ctx);
809 #else
810 if (value != NULL) {
811 int port = atoi(value);
812
813 if (port < 0)
814 init_mdb(ctx);
815 else
816 init_gdb(ctx);
817 }
818 #endif
819
820 if (bootrom_boot()) {
821 #ifdef __FreeBSD__
822 if (vm_set_capability(bsp, VM_CAP_UNRESTRICTED_GUEST, 1)) {
823 fprintf(stderr, "ROM boot failed: unrestricted guest "
824 "capability not available\n");
825 exit(4);
826 }
827 #else
828 /* Unrestricted Guest is always enabled on illumos */
829 #endif
830 error = vcpu_reset(bsp);
831 assert(error == 0);
832 }
833
834 if (bhyve_init_platform_late(ctx, bsp) != 0)
835 exit(4);
836
837 /*
838 * Change the proc title to include the VM name.
839 */
840 setproctitle("%s", vmname);
841
842 #ifndef WITHOUT_CAPSICUM
843 caph_cache_catpages();
844
845 if (caph_limit_stdout() == -1 || caph_limit_stderr() == -1)
846 errx(EX_OSERR, "Unable to apply rights for sandbox");
847
848 if (caph_enter() == -1)
849 errx(EX_OSERR, "cap_enter() failed");
850 #endif
851
852 #ifndef __FreeBSD__
853 illumos_priv_lock();
854 #endif
855
856 #ifndef __FreeBSD__
857 if (vmentry_init(guest_ncpus) != 0)
858 err(EX_OSERR, "vmentry_init() failed");
859 #endif
860
861 /*
862 * Add all vCPUs.
863 */
864 for (int vcpuid = 0; vcpuid < guest_ncpus; vcpuid++) {
865 #ifdef __FreeBSD__
866 bool suspend = (vcpuid != BSP);
867 #else
868 bool suspend = vcpuid == BSP &&
869 get_config_bool_default("suspend_at_boot", false);
870 #endif
871 bhyve_start_vcpu(vcpu_info[vcpuid].vcpu, vcpuid == BSP,
872 suspend);
873 }
874
875 /*
876 * Head off to the main event dispatch loop
877 */
878 mevent_dispatch();
879
880 exit(4);
881 }
882