xref: /freebsd/usr.sbin/bhyve/bhyverun.c (revision f1d705d4f431a5553aa5e64310bdc703ba27eab4)
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 #include <sys/types.h>
30 #ifndef WITHOUT_CAPSICUM
31 #include <sys/capsicum.h>
32 #endif
33 #include <sys/cpuset.h>
34 #include <sys/domainset.h>
35 #include <sys/mman.h>
36 #ifdef BHYVE_SNAPSHOT
37 #include <sys/socket.h>
38 #include <sys/stat.h>
39 #endif
40 #include <sys/time.h>
41 #ifdef BHYVE_SNAPSHOT
42 #include <sys/un.h>
43 #endif
44 
45 #include <machine/atomic.h>
46 
47 #ifndef WITHOUT_CAPSICUM
48 #include <capsicum_helpers.h>
49 #endif
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <err.h>
54 #include <errno.h>
55 #ifdef BHYVE_SNAPSHOT
56 #include <fcntl.h>
57 #endif
58 #include <libgen.h>
59 #include <libutil.h>
60 #include <unistd.h>
61 #include <assert.h>
62 #include <pthread.h>
63 #include <pthread_np.h>
64 #include <sysexits.h>
65 #include <stdbool.h>
66 #include <stdint.h>
67 #ifdef BHYVE_SNAPSHOT
68 #include <ucl.h>
69 #include <unistd.h>
70 
71 #include <libxo/xo.h>
72 #endif
73 
74 #include <dev/vmm/vmm_mem.h>
75 #include <vmmapi.h>
76 
77 #include "acpi.h"
78 #include "bhyverun.h"
79 #include "bootrom.h"
80 #include "config.h"
81 #include "debug.h"
82 #ifdef BHYVE_GDB
83 #include "gdb.h"
84 #endif
85 #include "mem.h"
86 #include "mevent.h"
87 #include "pci_emul.h"
88 #ifdef __amd64__
89 #include "amd64/pci_lpc.h"
90 #endif
91 #include "qemu_fwcfg.h"
92 #ifdef BHYVE_SNAPSHOT
93 #include "snapshot.h"
94 #endif
95 #include "tpm_device.h"
96 #include "vmgenc.h"
97 #include "vmexit.h"
98 
99 #define MB		(1024UL * 1024)
100 #define GB		(1024UL * MB)
101 
102 int guest_ncpus;
103 uint16_t cpu_cores, cpu_sockets, cpu_threads;
104 
105 int raw_stdio = 0;
106 
107 #ifdef BHYVE_SNAPSHOT
108 char *restore_file;
109 #endif
110 
111 static const int BSP = 0;
112 
113 static cpuset_t cpumask;
114 
115 static struct vm_mem_domain guest_domains[VM_MAXMEMDOM];
116 static int guest_ndomains = 0;
117 
118 static void vm_loop(struct vmctx *ctx, struct vcpu *vcpu);
119 
120 static struct vcpu_info {
121 	struct vmctx	*ctx;
122 	struct vcpu	*vcpu;
123 	int		vcpuid;
124 } *vcpu_info;
125 
126 static cpuset_t **vcpumap;
127 
128 /*
129  * XXX This parser is known to have the following issues:
130  * 1.  It accepts null key=value tokens ",," as setting "cpus" to an
131  *     empty string.
132  *
133  * The acceptance of a null specification ('-c ""') is by design to match the
134  * manual page syntax specification, this results in a topology of 1 vCPU.
135  */
136 int
bhyve_topology_parse(const char * opt)137 bhyve_topology_parse(const char *opt)
138 {
139 	char *cp, *str, *tofree;
140 
141 	if (*opt == '\0') {
142 		set_config_value("sockets", "1");
143 		set_config_value("cores", "1");
144 		set_config_value("threads", "1");
145 		set_config_value("cpus", "1");
146 		return (0);
147 	}
148 
149 	tofree = str = strdup(opt);
150 	if (str == NULL)
151 		errx(4, "Failed to allocate memory");
152 
153 	while ((cp = strsep(&str, ",")) != NULL) {
154 		if (strncmp(cp, "cpus=", strlen("cpus=")) == 0)
155 			set_config_value("cpus", cp + strlen("cpus="));
156 		else if (strncmp(cp, "sockets=", strlen("sockets=")) == 0)
157 			set_config_value("sockets", cp + strlen("sockets="));
158 		else if (strncmp(cp, "cores=", strlen("cores=")) == 0)
159 			set_config_value("cores", cp + strlen("cores="));
160 		else if (strncmp(cp, "threads=", strlen("threads=")) == 0)
161 			set_config_value("threads", cp + strlen("threads="));
162 		else if (strchr(cp, '=') != NULL)
163 			goto out;
164 		else
165 			set_config_value("cpus", cp);
166 	}
167 	free(tofree);
168 	return (0);
169 
170 out:
171 	free(tofree);
172 	return (-1);
173 }
174 
175 static int
parse_int_value(const char * key,const char * value,int minval,int maxval)176 parse_int_value(const char *key, const char *value, int minval, int maxval)
177 {
178 	char *cp;
179 	long lval;
180 
181 	errno = 0;
182 	lval = strtol(value, &cp, 0);
183 	if (errno != 0 || *cp != '\0' || cp == value || lval < minval ||
184 	    lval > maxval)
185 		errx(4, "Invalid value for %s: '%s'", key, value);
186 	return (lval);
187 }
188 
189 int
bhyve_numa_parse(const char * opt)190 bhyve_numa_parse(const char *opt)
191 {
192 	int id = -1;
193 	nvlist_t *nvl;
194 	char *cp, *str, *tofree;
195 	char pathbuf[64] = { 0 };
196 	char *size = NULL, *cpus = NULL, *domain_policy = NULL;
197 
198 	if (*opt == '\0') {
199 		return (-1);
200 	}
201 
202 	tofree = str = strdup(opt);
203 	if (str == NULL)
204 		errx(4, "Failed to allocate memory");
205 
206 	while ((cp = strsep(&str, ",")) != NULL) {
207 		if (strncmp(cp, "id=", strlen("id=")) == 0)
208 			id = parse_int_value("id", cp + strlen("id="), 0,
209 			    UINT8_MAX);
210 		else if (strncmp(cp, "size=", strlen("size=")) == 0)
211 			size = cp + strlen("size=");
212 		else if (strncmp(cp,
213 		    "domain_policy=", strlen("domain_policy=")) == 0)
214 			domain_policy = cp + strlen("domain_policy=");
215 		else if (strncmp(cp, "cpus=", strlen("cpus=")) == 0)
216 			cpus = cp + strlen("cpus=");
217 	}
218 
219 	if (id == -1) {
220 		EPRINTLN("Missing NUMA domain ID in '%s'", opt);
221 		goto out;
222 	}
223 
224 	snprintf(pathbuf, sizeof(pathbuf), "domains.%d", id);
225 	nvl = find_config_node(pathbuf);
226 	if (nvl == NULL)
227 		nvl = create_config_node(pathbuf);
228 	if (size != NULL)
229 		set_config_value_node(nvl, "size", size);
230 	if (domain_policy != NULL)
231 		set_config_value_node(nvl, "domain_policy", domain_policy);
232 	if (cpus != NULL)
233 		set_config_value_node(nvl, "cpus", cpus);
234 
235 	free(tofree);
236 	return (0);
237 
238 out:
239 	free(tofree);
240 	return (-1);
241 }
242 
243 static void
calc_mem_affinity(size_t vm_memsize)244 calc_mem_affinity(size_t vm_memsize)
245 {
246 	int i;
247 	nvlist_t *nvl;
248 	bool need_recalc;
249 	const char *value;
250 	struct vm_mem_domain *dom;
251 	char pathbuf[64] = { 0 };
252 
253 	need_recalc = false;
254 	for (i = 0; i < VM_MAXMEMDOM; i++) {
255 		dom = &guest_domains[i];
256 		snprintf(pathbuf, sizeof(pathbuf), "domains.%d", i);
257 		nvl = find_config_node(pathbuf);
258 		if (nvl == NULL) {
259 			break;
260 		}
261 
262 		value = get_config_value_node(nvl, "size");
263 		need_recalc |= value == NULL;
264 		if (value != NULL && vm_parse_memsize(value, &dom->size)) {
265 			errx(EX_USAGE, "invalid memsize for domain %d: '%s'", i,
266 			    value);
267 		}
268 
269 		dom->ds_mask = calloc(1, sizeof(domainset_t));
270 		if (dom->ds_mask == NULL) {
271 			errx(EX_OSERR, "Failed to allocate domainset mask");
272 		}
273 		dom->ds_size = sizeof(domainset_t);
274 		value = get_config_value_node(nvl, "domain_policy");
275 		if (value == NULL) {
276 			dom->ds_policy = DOMAINSET_POLICY_INVALID;
277 			DOMAINSET_ZERO(dom->ds_mask);
278 		} else if (domainset_parselist(value, dom->ds_mask, &dom->ds_policy) !=
279 		    CPUSET_PARSE_OK) {
280 				errx(EX_USAGE, "failed to parse domain policy '%s'", value);
281 		}
282 	}
283 
284 	guest_ndomains = i;
285 	if (guest_ndomains == 0) {
286 		/*
287 		 * No domains were specified - create domain
288 		 * 0 holding all CPUs and memory.
289 		 */
290 		guest_ndomains = 1;
291 		guest_domains[0].size = vm_memsize;
292 	} else if (need_recalc) {
293 		warnx("At least one domain memory size was not specified, distributing"
294 		    " total VM memory size across all domains");
295 		for (i = 0; i < guest_ndomains; i++) {
296 			guest_domains[i].size = vm_memsize / guest_ndomains;
297 		}
298 	}
299 }
300 
301 /*
302  * Set the sockets, cores, threads, and guest_cpus variables based on
303  * the configured topology.
304  *
305  * The limits of UINT16_MAX are due to the types passed to
306  * vm_set_topology().  vmm.ko may enforce tighter limits.
307  */
308 static void
calc_topology(void)309 calc_topology(void)
310 {
311 	const char *value;
312 	bool explicit_cpus;
313 	uint64_t ncpus;
314 
315 	value = get_config_value("cpus");
316 	if (value != NULL) {
317 		guest_ncpus = parse_int_value("cpus", value, 1, UINT16_MAX);
318 		explicit_cpus = true;
319 	} else {
320 		guest_ncpus = 1;
321 		explicit_cpus = false;
322 	}
323 	value = get_config_value("cores");
324 	if (value != NULL)
325 		cpu_cores = parse_int_value("cores", value, 1, UINT16_MAX);
326 	else
327 		cpu_cores = 1;
328 	value = get_config_value("threads");
329 	if (value != NULL)
330 		cpu_threads = parse_int_value("threads", value, 1, UINT16_MAX);
331 	else
332 		cpu_threads = 1;
333 	value = get_config_value("sockets");
334 	if (value != NULL)
335 		cpu_sockets = parse_int_value("sockets", value, 1, UINT16_MAX);
336 	else
337 		cpu_sockets = guest_ncpus;
338 
339 	/*
340 	 * Compute sockets * cores * threads avoiding overflow.  The
341 	 * range check above insures these are 16 bit values.
342 	 */
343 	ncpus = (uint64_t)cpu_sockets * cpu_cores * cpu_threads;
344 	if (ncpus > UINT16_MAX)
345 		errx(4, "Computed number of vCPUs too high: %ju",
346 		    (uintmax_t)ncpus);
347 
348 	if (explicit_cpus) {
349 		if (guest_ncpus != (int)ncpus)
350 			errx(4, "Topology (%d sockets, %d cores, %d threads) "
351 			    "does not match %d vCPUs",
352 			    cpu_sockets, cpu_cores, cpu_threads,
353 			    guest_ncpus);
354 	} else
355 		guest_ncpus = ncpus;
356 }
357 
358 int
bhyve_pincpu_parse(const char * opt)359 bhyve_pincpu_parse(const char *opt)
360 {
361 	const char *value;
362 	char *newval;
363 	char key[16];
364 	int vcpu, pcpu;
365 
366 	if (sscanf(opt, "%d:%d", &vcpu, &pcpu) != 2) {
367 		fprintf(stderr, "invalid format: %s\n", opt);
368 		return (-1);
369 	}
370 
371 	if (vcpu < 0) {
372 		fprintf(stderr, "invalid vcpu '%d'\n", vcpu);
373 		return (-1);
374 	}
375 
376 	if (pcpu < 0 || pcpu >= CPU_SETSIZE) {
377 		fprintf(stderr, "hostcpu '%d' outside valid range from "
378 		    "0 to %d\n", pcpu, CPU_SETSIZE - 1);
379 		return (-1);
380 	}
381 
382 	snprintf(key, sizeof(key), "vcpu.%d.cpuset", vcpu);
383 	value = get_config_value(key);
384 
385 	if (asprintf(&newval, "%s%s%d", value != NULL ? value : "",
386 	    value != NULL ? "," : "", pcpu) == -1) {
387 		perror("failed to build new cpuset string");
388 		return (-1);
389 	}
390 
391 	set_config_value(key, newval);
392 	free(newval);
393 	return (0);
394 }
395 
396 static void
parse_cpuset(int vcpu,const char * list,cpuset_t * set)397 parse_cpuset(int vcpu, const char *list, cpuset_t *set)
398 {
399 	char *cp, *token;
400 	int pcpu, start;
401 
402 	CPU_ZERO(set);
403 	start = -1;
404 	token = __DECONST(char *, list);
405 	for (;;) {
406 		pcpu = strtoul(token, &cp, 0);
407 		if (cp == token)
408 			errx(4, "invalid cpuset for vcpu %d: '%s'", vcpu, list);
409 		if (pcpu < 0 || pcpu >= CPU_SETSIZE)
410 			errx(4, "hostcpu '%d' outside valid range from 0 to %d",
411 			    pcpu, CPU_SETSIZE - 1);
412 		switch (*cp) {
413 		case ',':
414 		case '\0':
415 			if (start >= 0) {
416 				if (start > pcpu)
417 					errx(4, "Invalid hostcpu range %d-%d",
418 					    start, pcpu);
419 				while (start < pcpu) {
420 					CPU_SET(start, set);
421 					start++;
422 				}
423 				start = -1;
424 			}
425 			CPU_SET(pcpu, set);
426 			break;
427 		case '-':
428 			if (start >= 0)
429 				errx(4, "invalid cpuset for vcpu %d: '%s'",
430 				    vcpu, list);
431 			start = pcpu;
432 			break;
433 		default:
434 			errx(4, "invalid cpuset for vcpu %d: '%s'", vcpu, list);
435 		}
436 		if (*cp == '\0')
437 			break;
438 		token = cp + 1;
439 	}
440 }
441 
442 static void
build_vcpumaps(void)443 build_vcpumaps(void)
444 {
445 	char key[16];
446 	const char *value;
447 	int vcpu;
448 
449 	vcpumap = calloc(guest_ncpus, sizeof(*vcpumap));
450 	for (vcpu = 0; vcpu < guest_ncpus; vcpu++) {
451 		snprintf(key, sizeof(key), "vcpu.%d.cpuset", vcpu);
452 		value = get_config_value(key);
453 		if (value == NULL)
454 			continue;
455 		vcpumap[vcpu] = malloc(sizeof(cpuset_t));
456 		if (vcpumap[vcpu] == NULL)
457 			err(4, "Failed to allocate cpuset for vcpu %d", vcpu);
458 		parse_cpuset(vcpu, value, vcpumap[vcpu]);
459 	}
460 }
461 
462 static void
set_vcpu_affinities(void)463 set_vcpu_affinities(void)
464 {
465 	int cpu, error;
466 	nvlist_t *nvl = NULL;
467 	cpuset_t cpus;
468 	const char *value;
469 	char pathbuf[64] = { 0 };
470 
471 	for (int dom = 0; dom < guest_ndomains; dom++) {
472 		snprintf(pathbuf, sizeof(pathbuf), "domains.%d", dom);
473 		nvl = find_config_node(pathbuf);
474 		if (nvl == NULL)
475 			break;
476 
477 		value = get_config_value_node(nvl, "cpus");
478 		if (value == NULL) {
479 			EPRINTLN("Missing CPU set for domain %d", dom);
480 			exit(4);
481 		}
482 
483 		parse_cpuset(dom, value, &cpus);
484 		CPU_FOREACH_ISSET(cpu, &cpus) {
485 			error = acpi_add_vcpu_affinity(cpu, dom);
486 			if (error) {
487 				EPRINTLN(
488 				    "Unable to set vCPU %d affinity for domain %d: %s",
489 				    cpu, dom, strerror(errno));
490 				exit(4);
491 			}
492 		}
493 	}
494 	if (guest_ndomains > 1 || nvl != NULL)
495 		return;
496 
497 	/*
498 	 * If we're dealing with one domain and no cpuset was provided, create a
499 	 * default one holding all cpus.
500 	 */
501 	for (cpu = 0; cpu < guest_ncpus; cpu++) {
502 		error = acpi_add_vcpu_affinity(cpu, 0);
503 		if (error) {
504 			EPRINTLN(
505 			    "Unable to set vCPU %d affinity for domain %d: %s",
506 			    cpu, 0, strerror(errno));
507 			exit(4);
508 		}
509 	}
510 }
511 
512 void *
paddr_guest2host(struct vmctx * ctx,uintptr_t gaddr,size_t len)513 paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len)
514 {
515 
516 	return (vm_map_gpa(ctx, gaddr, len));
517 }
518 
519 #ifdef BHYVE_SNAPSHOT
520 uintptr_t
paddr_host2guest(struct vmctx * ctx,void * addr)521 paddr_host2guest(struct vmctx *ctx, void *addr)
522 {
523 	return (vm_rev_map_gpa(ctx, addr));
524 }
525 #endif
526 
527 int
fbsdrun_virtio_msix(void)528 fbsdrun_virtio_msix(void)
529 {
530 
531 	return (get_config_bool_default("virtio_msix", true));
532 }
533 
534 struct vcpu *
fbsdrun_vcpu(int vcpuid)535 fbsdrun_vcpu(int vcpuid)
536 {
537 	return (vcpu_info[vcpuid].vcpu);
538 }
539 
540 static void *
fbsdrun_start_thread(void * param)541 fbsdrun_start_thread(void *param)
542 {
543 	char tname[MAXCOMLEN + 1];
544 	struct vcpu_info *vi = param;
545 	int error;
546 
547 	snprintf(tname, sizeof(tname), "vcpu %d", vi->vcpuid);
548 	pthread_set_name_np(pthread_self(), tname);
549 
550 	if (vcpumap[vi->vcpuid] != NULL) {
551 		error = pthread_setaffinity_np(pthread_self(),
552 		    sizeof(cpuset_t), vcpumap[vi->vcpuid]);
553 		assert(error == 0);
554 	}
555 
556 #ifdef BHYVE_SNAPSHOT
557 	checkpoint_cpu_add(vi->vcpuid);
558 #endif
559 #ifdef BHYVE_GDB
560 	gdb_cpu_add(vi->vcpu);
561 #endif
562 
563 	vm_loop(vi->ctx, vi->vcpu);
564 
565 	/* not reached */
566 	exit(1);
567 	return (NULL);
568 }
569 
570 void
fbsdrun_addcpu(int vcpuid)571 fbsdrun_addcpu(int vcpuid)
572 {
573 	struct vcpu_info *vi;
574 	pthread_t thr;
575 	int error;
576 
577 	vi = &vcpu_info[vcpuid];
578 
579 	error = vm_activate_cpu(vi->vcpu);
580 	if (error != 0)
581 		err(EX_OSERR, "could not activate CPU %d", vi->vcpuid);
582 
583 	CPU_SET_ATOMIC(vcpuid, &cpumask);
584 
585 	error = vm_suspend_cpu(vi->vcpu);
586 	assert(error == 0);
587 
588 	error = pthread_create(&thr, NULL, fbsdrun_start_thread, vi);
589 	assert(error == 0);
590 }
591 
592 void
fbsdrun_deletecpu(int vcpu)593 fbsdrun_deletecpu(int vcpu)
594 {
595 	static pthread_mutex_t resetcpu_mtx = PTHREAD_MUTEX_INITIALIZER;
596 	static pthread_cond_t resetcpu_cond = PTHREAD_COND_INITIALIZER;
597 
598 	pthread_mutex_lock(&resetcpu_mtx);
599 	if (!CPU_ISSET(vcpu, &cpumask)) {
600 		EPRINTLN("Attempting to delete unknown cpu %d", vcpu);
601 		exit(4);
602 	}
603 
604 	CPU_CLR(vcpu, &cpumask);
605 
606 	if (vcpu != BSP) {
607 		pthread_cond_signal(&resetcpu_cond);
608 		pthread_mutex_unlock(&resetcpu_mtx);
609 		pthread_exit(NULL);
610 		/* NOTREACHED */
611 	}
612 
613 	while (!CPU_EMPTY(&cpumask)) {
614 		pthread_cond_wait(&resetcpu_cond, &resetcpu_mtx);
615 	}
616 	pthread_mutex_unlock(&resetcpu_mtx);
617 }
618 
619 int
fbsdrun_suspendcpu(int vcpuid)620 fbsdrun_suspendcpu(int vcpuid)
621 {
622 	return (vm_suspend_cpu(vcpu_info[vcpuid].vcpu));
623 }
624 
625 static void
vm_loop(struct vmctx * ctx,struct vcpu * vcpu)626 vm_loop(struct vmctx *ctx, struct vcpu *vcpu)
627 {
628 	struct vm_exit vme;
629 	struct vm_run vmrun;
630 	int error, rc;
631 	enum vm_exitcode exitcode;
632 	cpuset_t active_cpus, dmask;
633 
634 	error = vm_active_cpus(ctx, &active_cpus);
635 	assert(CPU_ISSET(vcpu_id(vcpu), &active_cpus));
636 
637 	vmrun.vm_exit = &vme;
638 	vmrun.cpuset = &dmask;
639 	vmrun.cpusetsize = sizeof(dmask);
640 
641 	while (1) {
642 		error = vm_run(vcpu, &vmrun);
643 		if (error != 0)
644 			break;
645 
646 		exitcode = vme.exitcode;
647 		if (exitcode >= VM_EXITCODE_MAX ||
648 		    vmexit_handlers[exitcode] == NULL) {
649 			warnx("vm_loop: unexpected exitcode 0x%x", exitcode);
650 			exit(4);
651 		}
652 
653 		rc = (*vmexit_handlers[exitcode])(ctx, vcpu, &vmrun);
654 
655 		switch (rc) {
656 		case VMEXIT_CONTINUE:
657 			break;
658 		case VMEXIT_ABORT:
659 			abort();
660 		default:
661 			exit(4);
662 		}
663 	}
664 	EPRINTLN("vm_run error %d, errno %d", error, errno);
665 }
666 
667 static int
num_vcpus_allowed(struct vmctx * ctx,struct vcpu * vcpu)668 num_vcpus_allowed(struct vmctx *ctx, struct vcpu *vcpu)
669 {
670 	uint16_t sockets, cores, threads, maxcpus;
671 	int tmp, error;
672 
673 	/*
674 	 * The guest is allowed to spinup more than one processor only if the
675 	 * UNRESTRICTED_GUEST capability is available.
676 	 */
677 	error = vm_get_capability(vcpu, VM_CAP_UNRESTRICTED_GUEST, &tmp);
678 	if (error != 0)
679 		return (1);
680 
681 	error = vm_get_topology(ctx, &sockets, &cores, &threads, &maxcpus);
682 	if (error == 0)
683 		return (maxcpus);
684 	else
685 		return (1);
686 }
687 
688 static struct vmctx *
do_open(const char * vmname)689 do_open(const char *vmname)
690 {
691 	struct vmctx *ctx;
692 	int error;
693 	bool romboot;
694 
695 	romboot = bootrom_boot();
696 
697 	/*
698 	 * If we don't have a boot ROM, the guest context must have been
699 	 * initialized by bhyveload(8) or equivalent.
700 	 */
701 	ctx = vm_openf(vmname, romboot ? VMMAPI_OPEN_REINIT : 0);
702 	if (ctx == NULL) {
703 		if (errno != ENOENT)
704 			err(4, "vm_openf");
705 		if (!romboot)
706 			errx(4, "no bootrom was configured");
707 		ctx = vm_openf(vmname, VMMAPI_OPEN_CREATE);
708 		if (ctx == NULL)
709 			err(4, "vm_openf");
710 	}
711 
712 #ifndef WITHOUT_CAPSICUM
713 	if (vm_limit_rights(ctx) != 0)
714 		err(EX_OSERR, "vm_limit_rights");
715 #endif
716 
717 	error = vm_set_topology(ctx, cpu_sockets, cpu_cores, cpu_threads, 0);
718 	if (error)
719 		errx(EX_OSERR, "vm_set_topology");
720 	return (ctx);
721 }
722 
723 bool
bhyve_parse_config_option(const char * option)724 bhyve_parse_config_option(const char *option)
725 {
726 	const char *value;
727 	char *path;
728 
729 	value = strchr(option, '=');
730 	if (value == NULL || value[1] == '\0')
731 		return (false);
732 	path = strndup(option, value - option);
733 	if (path == NULL)
734 		err(4, "Failed to allocate memory");
735 	set_config_value(path, value + 1);
736 	free(path);
737 	return (true);
738 }
739 
740 void
bhyve_parse_simple_config_file(const char * path)741 bhyve_parse_simple_config_file(const char *path)
742 {
743 	FILE *fp;
744 	char *line, *cp;
745 	size_t linecap;
746 	unsigned int lineno;
747 
748 	fp = fopen(path, "r");
749 	if (fp == NULL)
750 		err(4, "Failed to open configuration file %s", path);
751 	line = NULL;
752 	linecap = 0;
753 	lineno = 1;
754 	for (lineno = 1; getline(&line, &linecap, fp) > 0; lineno++) {
755 		if (*line == '#' || *line == '\n')
756 			continue;
757 		cp = strchr(line, '\n');
758 		if (cp != NULL)
759 			*cp = '\0';
760 		if (!bhyve_parse_config_option(line))
761 			errx(4, "%s line %u: invalid config option '%s'", path,
762 			    lineno, line);
763 	}
764 	free(line);
765 	fclose(fp);
766 }
767 
768 #ifdef BHYVE_GDB
769 void
bhyve_parse_gdb_options(const char * opt)770 bhyve_parse_gdb_options(const char *opt)
771 {
772 	const char *sport;
773 	char *colon;
774 
775 	if (opt[0] == 'w') {
776 		set_config_bool("gdb.wait", true);
777 		opt++;
778 	}
779 
780 	colon = strrchr(opt, ':');
781 	if (colon == NULL) {
782 		sport = opt;
783 	} else {
784 		*colon = '\0';
785 		colon++;
786 		sport = colon;
787 		set_config_value("gdb.address", opt);
788 	}
789 
790 	set_config_value("gdb.port", sport);
791 }
792 #endif
793 
794 int
main(int argc,char * argv[])795 main(int argc, char *argv[])
796 {
797 	int error;
798 	int max_vcpus, memflags;
799 	struct vcpu *bsp;
800 	struct vmctx *ctx;
801 	size_t memsize;
802 	const char *value, *vmname;
803 #ifdef BHYVE_SNAPSHOT
804 	struct restore_state rstate;
805 #endif
806 
807 	bhyve_init_config();
808 	bhyve_optparse(argc, argv);
809 	argc -= optind;
810 	argv += optind;
811 
812 	if (argc > 1)
813 		bhyve_usage(1);
814 
815 #ifdef BHYVE_SNAPSHOT
816 	if (restore_file != NULL) {
817 		error = load_restore_file(restore_file, &rstate);
818 		if (error) {
819 			fprintf(stderr, "Failed to read checkpoint info from "
820 					"file: '%s'.\n", restore_file);
821 			exit(1);
822 		}
823 		vmname = lookup_vmname(&rstate);
824 		if (vmname != NULL)
825 			set_config_value("name", vmname);
826 	}
827 #endif
828 
829 	if (argc == 1)
830 		set_config_value("name", argv[0]);
831 
832 	vmname = get_config_value("name");
833 	if (vmname == NULL)
834 		bhyve_usage(1);
835 
836 	if (get_config_bool_default("config.dump", false)) {
837 		dump_config();
838 		exit(1);
839 	}
840 
841 	calc_topology();
842 	build_vcpumaps();
843 
844 	value = get_config_value("memory.size");
845 	error = vm_parse_memsize(value, &memsize);
846 	if (error)
847 		errx(EX_USAGE, "invalid memsize '%s'", value);
848 
849 	ctx = do_open(vmname);
850 
851 #ifdef BHYVE_SNAPSHOT
852 	if (restore_file != NULL) {
853 		guest_ncpus = lookup_guest_ncpus(&rstate);
854 		memflags = lookup_memflags(&rstate);
855 		memsize = lookup_memsize(&rstate);
856 	}
857 
858 	if (guest_ncpus < 1) {
859 		fprintf(stderr, "Invalid guest vCPUs (%d)\n", guest_ncpus);
860 		exit(1);
861 	}
862 #endif
863 
864 	bsp = vm_vcpu_open(ctx, BSP);
865 	max_vcpus = num_vcpus_allowed(ctx, bsp);
866 	if (guest_ncpus > max_vcpus) {
867 		fprintf(stderr, "%d vCPUs requested but only %d available\n",
868 			guest_ncpus, max_vcpus);
869 		exit(4);
870 	}
871 
872 	bhyve_init_vcpu(bsp);
873 
874 	/* Allocate per-VCPU resources. */
875 	vcpu_info = calloc(guest_ncpus, sizeof(*vcpu_info));
876 	for (int vcpuid = 0; vcpuid < guest_ncpus; vcpuid++) {
877 		vcpu_info[vcpuid].ctx = ctx;
878 		vcpu_info[vcpuid].vcpuid = vcpuid;
879 		if (vcpuid == BSP)
880 			vcpu_info[vcpuid].vcpu = bsp;
881 		else
882 			vcpu_info[vcpuid].vcpu = vm_vcpu_open(ctx, vcpuid);
883 	}
884 
885 	calc_mem_affinity(memsize);
886 	memflags = 0;
887 	if (get_config_bool_default("memory.wired", false))
888 		memflags |= VM_MEM_F_WIRED;
889 	if (get_config_bool_default("memory.guest_in_core", false))
890 		memflags |= VM_MEM_F_INCORE;
891 	vm_set_memflags(ctx, memflags);
892 	error = vm_setup_memory_domains(ctx, VM_MMAP_ALL, guest_domains,
893 	    guest_ndomains);
894 	if (error) {
895 		fprintf(stderr, "Unable to setup memory (%d)\n", errno);
896 		exit(4);
897 	}
898 
899 	set_vcpu_affinities();
900 	init_mem(guest_ncpus);
901 	init_bootrom(ctx);
902 	if (bhyve_init_platform(ctx, bsp) != 0)
903 		exit(4);
904 
905 	if (qemu_fwcfg_init(ctx) != 0) {
906 		fprintf(stderr, "qemu fwcfg initialization error\n");
907 		exit(4);
908 	}
909 
910 	if (qemu_fwcfg_add_file("opt/bhyve/hw.ncpu", sizeof(guest_ncpus),
911 	    &guest_ncpus) != 0) {
912 		fprintf(stderr, "Could not add qemu fwcfg opt/bhyve/hw.ncpu\n");
913 		exit(4);
914 	}
915 
916 	/*
917 	 * Exit if a device emulation finds an error in its initialization
918 	 */
919 	if (init_pci(ctx) != 0) {
920 		EPRINTLN("Device emulation initialization error: %s",
921 		    strerror(errno));
922 		exit(4);
923 	}
924 	if (init_tpm(ctx) != 0) {
925 		EPRINTLN("Failed to init TPM device");
926 		exit(4);
927 	}
928 
929 	/*
930 	 * Initialize after PCI, to allow a bootrom file to reserve the high
931 	 * region.
932 	 */
933 	if (get_config_bool("acpi_tables"))
934 		vmgenc_init(ctx);
935 
936 #ifdef BHYVE_GDB
937 	init_gdb(ctx);
938 #endif
939 
940 	/*
941 	 * Add all vCPUs.
942 	 */
943 	for (int vcpuid = 0; vcpuid < guest_ncpus; vcpuid++)
944 		bhyve_start_vcpu(vcpu_info[vcpuid].vcpu, vcpuid == BSP);
945 
946 #ifdef BHYVE_SNAPSHOT
947 	if (restore_file != NULL) {
948 		FPRINTLN(stdout, "Pausing pci devs...");
949 		if (vm_pause_devices() != 0) {
950 			EPRINTLN("Failed to pause PCI device state.");
951 			exit(1);
952 		}
953 
954 		FPRINTLN(stdout, "Restoring vm mem...");
955 		if (restore_vm_mem(ctx, &rstate) != 0) {
956 			EPRINTLN("Failed to restore VM memory.");
957 			exit(1);
958 		}
959 
960 		FPRINTLN(stdout, "Restoring pci devs...");
961 		if (vm_restore_devices(&rstate) != 0) {
962 			EPRINTLN("Failed to restore PCI device state.");
963 			exit(1);
964 		}
965 
966 		FPRINTLN(stdout, "Restoring kernel structs...");
967 		if (vm_restore_kern_structs(ctx, &rstate) != 0) {
968 			EPRINTLN("Failed to restore kernel structs.");
969 			exit(1);
970 		}
971 
972 		FPRINTLN(stdout, "Resuming pci devs...");
973 		if (vm_resume_devices() != 0) {
974 			EPRINTLN("Failed to resume PCI device state.");
975 			exit(1);
976 		}
977 	}
978 #endif
979 
980 	if (bhyve_init_platform_late(ctx, bsp) != 0)
981 		exit(4);
982 
983 	/*
984 	 * Change the proc title to include the VM name.
985 	 */
986 	setproctitle("%s", vmname);
987 
988 #ifdef BHYVE_SNAPSHOT
989 	/*
990 	 * checkpointing thread for communication with bhyvectl
991 	 */
992 	if (init_checkpoint_thread(ctx) != 0)
993 		errx(EX_OSERR, "Failed to start checkpoint thread");
994 #endif
995 
996 #ifndef WITHOUT_CAPSICUM
997 	caph_cache_catpages();
998 
999 	if (caph_limit_stdout() == -1 || caph_limit_stderr() == -1)
1000 		errx(EX_OSERR, "Unable to apply rights for sandbox");
1001 
1002 	if (caph_enter() == -1)
1003 		errx(EX_OSERR, "cap_enter() failed");
1004 #endif
1005 
1006 #ifdef BHYVE_SNAPSHOT
1007 	if (restore_file != NULL) {
1008 		destroy_restore_state(&rstate);
1009 		if (vm_restore_time(ctx) < 0)
1010 			err(EX_OSERR, "Unable to restore time");
1011 
1012 		for (int vcpuid = 0; vcpuid < guest_ncpus; vcpuid++)
1013 			vm_resume_cpu(vcpu_info[vcpuid].vcpu);
1014 	} else
1015 #endif
1016 		vm_resume_cpu(bsp);
1017 
1018 	/*
1019 	 * Head off to the main event dispatch loop
1020 	 */
1021 	mevent_dispatch();
1022 
1023 	exit(4);
1024 }
1025