xref: /freebsd/usr.sbin/bhyve/bhyverun.c (revision dda5b39711dab90ae1c5624bdd6ff7453177df31)
1 /*-
2  * Copyright (c) 2011 NetApp, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 #include <sys/mman.h>
34 #include <sys/time.h>
35 
36 #include <machine/atomic.h>
37 #include <machine/segments.h>
38 
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <err.h>
43 #include <libgen.h>
44 #include <unistd.h>
45 #include <assert.h>
46 #include <errno.h>
47 #include <pthread.h>
48 #include <pthread_np.h>
49 #include <sysexits.h>
50 
51 #include <machine/vmm.h>
52 #include <vmmapi.h>
53 
54 #include "bhyverun.h"
55 #include "acpi.h"
56 #include "inout.h"
57 #include "dbgport.h"
58 #include "ioapic.h"
59 #include "mem.h"
60 #include "mevent.h"
61 #include "mptbl.h"
62 #include "pci_emul.h"
63 #include "pci_lpc.h"
64 #include "smbiostbl.h"
65 #include "xmsr.h"
66 #include "spinup_ap.h"
67 #include "rtc.h"
68 
69 #define GUEST_NIO_PORT		0x488	/* guest upcalls via i/o port */
70 
71 #define	VMEXIT_SWITCH		0	/* force vcpu switch in mux mode */
72 #define	VMEXIT_CONTINUE		1	/* continue from next instruction */
73 #define	VMEXIT_RESTART		2	/* restart current instruction */
74 #define	VMEXIT_ABORT		3	/* abort the vm run loop */
75 #define	VMEXIT_RESET		4	/* guest machine has reset */
76 #define	VMEXIT_POWEROFF		5	/* guest machine has powered off */
77 
78 #define MB		(1024UL * 1024)
79 #define GB		(1024UL * MB)
80 
81 typedef int (*vmexit_handler_t)(struct vmctx *, struct vm_exit *, int *vcpu);
82 
83 char *vmname;
84 
85 int guest_ncpus;
86 char *guest_uuid_str;
87 
88 static int pincpu = -1;
89 static int guest_vmexit_on_hlt, guest_vmexit_on_pause;
90 static int virtio_msix = 1;
91 static int x2apic_mode = 0;	/* default is xAPIC */
92 
93 static int strictio;
94 static int strictmsr = 1;
95 
96 static int acpi;
97 
98 static char *progname;
99 static const int BSP = 0;
100 
101 static int cpumask;
102 
103 static void vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip);
104 
105 struct vm_exit vmexit[VM_MAXCPU];
106 
107 struct bhyvestats {
108         uint64_t        vmexit_bogus;
109         uint64_t        vmexit_bogus_switch;
110         uint64_t        vmexit_hlt;
111         uint64_t        vmexit_pause;
112         uint64_t        vmexit_mtrap;
113         uint64_t        vmexit_inst_emul;
114         uint64_t        cpu_switch_rotate;
115         uint64_t        cpu_switch_direct;
116         int             io_reset;
117 } stats;
118 
119 struct mt_vmm_info {
120 	pthread_t	mt_thr;
121 	struct vmctx	*mt_ctx;
122 	int		mt_vcpu;
123 } mt_vmm_info[VM_MAXCPU];
124 
125 static void
126 usage(int code)
127 {
128 
129         fprintf(stderr,
130                 "Usage: %s [-aehwAHIPW] [-g <gdb port>] [-s <pci>]\n"
131 		"       %*s [-c vcpus] [-p pincpu] [-m mem] [-l <lpc>] <vm>\n"
132 		"       -a: local apic is in xAPIC mode (deprecated)\n"
133 		"       -A: create an ACPI table\n"
134 		"       -g: gdb port\n"
135 		"       -c: # cpus (default 1)\n"
136 		"       -p: pin vcpu 'n' to host cpu 'pincpu + n'\n"
137 		"       -H: vmexit from the guest on hlt\n"
138 		"       -P: vmexit from the guest on pause\n"
139 		"       -W: force virtio to use single-vector MSI\n"
140 		"       -e: exit on unhandled I/O access\n"
141 		"       -h: help\n"
142 		"       -s: <slot,driver,configinfo> PCI slot config\n"
143 		"       -l: LPC device configuration\n"
144 		"       -m: memory size in MB\n"
145 		"       -w: ignore unimplemented MSRs\n"
146 		"       -x: local apic is in x2APIC mode\n"
147 		"       -U: uuid\n",
148 		progname, (int)strlen(progname), "");
149 
150 	exit(code);
151 }
152 
153 void *
154 paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len)
155 {
156 
157 	return (vm_map_gpa(ctx, gaddr, len));
158 }
159 
160 int
161 fbsdrun_vmexit_on_pause(void)
162 {
163 
164 	return (guest_vmexit_on_pause);
165 }
166 
167 int
168 fbsdrun_vmexit_on_hlt(void)
169 {
170 
171 	return (guest_vmexit_on_hlt);
172 }
173 
174 int
175 fbsdrun_virtio_msix(void)
176 {
177 
178 	return (virtio_msix);
179 }
180 
181 static void *
182 fbsdrun_start_thread(void *param)
183 {
184 	char tname[MAXCOMLEN + 1];
185 	struct mt_vmm_info *mtp;
186 	int vcpu;
187 
188 	mtp = param;
189 	vcpu = mtp->mt_vcpu;
190 
191 	snprintf(tname, sizeof(tname), "vcpu %d", vcpu);
192 	pthread_set_name_np(mtp->mt_thr, tname);
193 
194 	vm_loop(mtp->mt_ctx, vcpu, vmexit[vcpu].rip);
195 
196 	/* not reached */
197 	exit(1);
198 	return (NULL);
199 }
200 
201 void
202 fbsdrun_addcpu(struct vmctx *ctx, int vcpu, uint64_t rip)
203 {
204 	int error;
205 
206 	if (cpumask & (1 << vcpu)) {
207 		fprintf(stderr, "addcpu: attempting to add existing cpu %d\n",
208 		    vcpu);
209 		exit(1);
210 	}
211 
212 	atomic_set_int(&cpumask, 1 << vcpu);
213 
214 	/*
215 	 * Set up the vmexit struct to allow execution to start
216 	 * at the given RIP
217 	 */
218 	vmexit[vcpu].rip = rip;
219 	vmexit[vcpu].inst_length = 0;
220 
221 	mt_vmm_info[vcpu].mt_ctx = ctx;
222 	mt_vmm_info[vcpu].mt_vcpu = vcpu;
223 
224 	error = pthread_create(&mt_vmm_info[vcpu].mt_thr, NULL,
225 	    fbsdrun_start_thread, &mt_vmm_info[vcpu]);
226 	assert(error == 0);
227 }
228 
229 static int
230 fbsdrun_deletecpu(struct vmctx *ctx, int vcpu)
231 {
232 
233 	if ((cpumask & (1 << vcpu)) == 0) {
234 		fprintf(stderr, "addcpu: attempting to delete unknown cpu %d\n",
235 		    vcpu);
236 		exit(1);
237 	}
238 
239 	atomic_clear_int(&cpumask, 1 << vcpu);
240 	return (cpumask == 0);
241 }
242 
243 static int
244 vmexit_catch_reset(void)
245 {
246         stats.io_reset++;
247         return (VMEXIT_RESET);
248 }
249 
250 static int
251 vmexit_catch_inout(void)
252 {
253 	return (VMEXIT_ABORT);
254 }
255 
256 static int
257 vmexit_handle_notify(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu,
258 		     uint32_t eax)
259 {
260 #if BHYVE_DEBUG
261 	/*
262 	 * put guest-driven debug here
263 	 */
264 #endif
265         return (VMEXIT_CONTINUE);
266 }
267 
268 static int
269 vmexit_inout(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
270 {
271 	int error;
272 	int bytes, port, in, out;
273 	uint32_t eax;
274 	int vcpu;
275 
276 	vcpu = *pvcpu;
277 
278 	port = vme->u.inout.port;
279 	bytes = vme->u.inout.bytes;
280 	eax = vme->u.inout.eax;
281 	in = vme->u.inout.in;
282 	out = !in;
283 
284 	/* We don't deal with these */
285 	if (vme->u.inout.string || vme->u.inout.rep)
286 		return (VMEXIT_ABORT);
287 
288 	/* Special case of guest reset */
289 	if (out && port == 0x64 && (uint8_t)eax == 0xFE)
290 		return (vmexit_catch_reset());
291 
292         /* Extra-special case of host notifications */
293         if (out && port == GUEST_NIO_PORT)
294                 return (vmexit_handle_notify(ctx, vme, pvcpu, eax));
295 
296 	error = emulate_inout(ctx, vcpu, in, port, bytes, &eax, strictio);
297 	if (error == INOUT_OK && in)
298 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX, eax);
299 
300 	switch (error) {
301 	case INOUT_OK:
302 		return (VMEXIT_CONTINUE);
303 	case INOUT_RESET:
304 		return (VMEXIT_RESET);
305 	case INOUT_POWEROFF:
306 		return (VMEXIT_POWEROFF);
307 	default:
308 		fprintf(stderr, "Unhandled %s%c 0x%04x\n",
309 			in ? "in" : "out",
310 			bytes == 1 ? 'b' : (bytes == 2 ? 'w' : 'l'), port);
311 		return (vmexit_catch_inout());
312 	}
313 }
314 
315 static int
316 vmexit_rdmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
317 {
318 	uint64_t val;
319 	uint32_t eax, edx;
320 	int error;
321 
322 	val = 0;
323 	error = emulate_rdmsr(ctx, *pvcpu, vme->u.msr.code, &val);
324 	if (error != 0) {
325 		fprintf(stderr, "rdmsr to register %#x on vcpu %d\n",
326 		    vme->u.msr.code, *pvcpu);
327 		if (strictmsr) {
328 			error = vm_inject_exception2(ctx, *pvcpu, IDT_GP, 0);
329 			assert(error == 0);
330 			return (VMEXIT_RESTART);
331 		}
332 	}
333 
334 	eax = val;
335 	error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RAX, eax);
336 	assert(error == 0);
337 
338 	edx = val >> 32;
339 	error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RDX, edx);
340 	assert(error == 0);
341 
342 	return (VMEXIT_CONTINUE);
343 }
344 
345 static int
346 vmexit_wrmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
347 {
348 	int error;
349 
350 	error = emulate_wrmsr(ctx, *pvcpu, vme->u.msr.code, vme->u.msr.wval);
351 	if (error != 0) {
352 		fprintf(stderr, "wrmsr to register %#x(%#lx) on vcpu %d\n",
353 		    vme->u.msr.code, vme->u.msr.wval, *pvcpu);
354 		if (strictmsr) {
355 			error = vm_inject_exception2(ctx, *pvcpu, IDT_GP, 0);
356 			assert(error == 0);
357 			return (VMEXIT_RESTART);
358 		}
359 	}
360 	return (VMEXIT_CONTINUE);
361 }
362 
363 static int
364 vmexit_spinup_ap(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
365 {
366 	int newcpu;
367 	int retval = VMEXIT_CONTINUE;
368 
369 	newcpu = spinup_ap(ctx, *pvcpu,
370 			   vme->u.spinup_ap.vcpu, vme->u.spinup_ap.rip);
371 
372 	return (retval);
373 }
374 
375 static int
376 vmexit_spindown_cpu(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
377 {
378 	int lastcpu;
379 
380 	lastcpu = fbsdrun_deletecpu(ctx, *pvcpu);
381 	if (!lastcpu)
382 		pthread_exit(NULL);
383 	return (vmexit_catch_reset());
384 }
385 
386 static int
387 vmexit_vmx(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
388 {
389 
390 	fprintf(stderr, "vm exit[%d]\n", *pvcpu);
391 	fprintf(stderr, "\treason\t\tVMX\n");
392 	fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip);
393 	fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length);
394 	fprintf(stderr, "\tstatus\t\t%d\n", vmexit->u.vmx.status);
395 	fprintf(stderr, "\texit_reason\t%u\n", vmexit->u.vmx.exit_reason);
396 	fprintf(stderr, "\tqualification\t0x%016lx\n",
397 	    vmexit->u.vmx.exit_qualification);
398 	fprintf(stderr, "\tinst_type\t\t%d\n", vmexit->u.vmx.inst_type);
399 	fprintf(stderr, "\tinst_error\t\t%d\n", vmexit->u.vmx.inst_error);
400 
401 	return (VMEXIT_ABORT);
402 }
403 
404 static int
405 vmexit_bogus(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
406 {
407 
408 	stats.vmexit_bogus++;
409 
410 	return (VMEXIT_RESTART);
411 }
412 
413 static int
414 vmexit_hlt(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
415 {
416 
417 	stats.vmexit_hlt++;
418 
419 	/*
420 	 * Just continue execution with the next instruction. We use
421 	 * the HLT VM exit as a way to be friendly with the host
422 	 * scheduler.
423 	 */
424 	return (VMEXIT_CONTINUE);
425 }
426 
427 static int
428 vmexit_pause(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
429 {
430 
431 	stats.vmexit_pause++;
432 
433 	return (VMEXIT_CONTINUE);
434 }
435 
436 static int
437 vmexit_mtrap(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
438 {
439 
440 	stats.vmexit_mtrap++;
441 
442 	return (VMEXIT_RESTART);
443 }
444 
445 static int
446 vmexit_inst_emul(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
447 {
448 	int err;
449 	stats.vmexit_inst_emul++;
450 
451 	err = emulate_mem(ctx, *pvcpu, vmexit->u.inst_emul.gpa,
452 			  &vmexit->u.inst_emul.vie);
453 
454 	if (err) {
455 		if (err == EINVAL) {
456 			fprintf(stderr,
457 			    "Failed to emulate instruction at 0x%lx\n",
458 			    vmexit->rip);
459 		} else if (err == ESRCH) {
460 			fprintf(stderr, "Unhandled memory access to 0x%lx\n",
461 			    vmexit->u.inst_emul.gpa);
462 		}
463 
464 		return (VMEXIT_ABORT);
465 	}
466 
467 	return (VMEXIT_CONTINUE);
468 }
469 
470 static vmexit_handler_t handler[VM_EXITCODE_MAX] = {
471 	[VM_EXITCODE_INOUT]  = vmexit_inout,
472 	[VM_EXITCODE_VMX]    = vmexit_vmx,
473 	[VM_EXITCODE_BOGUS]  = vmexit_bogus,
474 	[VM_EXITCODE_RDMSR]  = vmexit_rdmsr,
475 	[VM_EXITCODE_WRMSR]  = vmexit_wrmsr,
476 	[VM_EXITCODE_MTRAP]  = vmexit_mtrap,
477 	[VM_EXITCODE_INST_EMUL] = vmexit_inst_emul,
478 	[VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap,
479 	[VM_EXITCODE_SPINDOWN_CPU] = vmexit_spindown_cpu,
480 };
481 
482 static void
483 vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip)
484 {
485 	cpuset_t mask;
486 	int error, rc, prevcpu;
487 	enum vm_exitcode exitcode;
488 
489 	if (pincpu >= 0) {
490 		CPU_ZERO(&mask);
491 		CPU_SET(pincpu + vcpu, &mask);
492 		error = pthread_setaffinity_np(pthread_self(),
493 					       sizeof(mask), &mask);
494 		assert(error == 0);
495 	}
496 
497 	while (1) {
498 		error = vm_run(ctx, vcpu, rip, &vmexit[vcpu]);
499 		if (error != 0)
500 			break;
501 
502 		prevcpu = vcpu;
503 
504 		exitcode = vmexit[vcpu].exitcode;
505 		if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) {
506 			fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n",
507 			    exitcode);
508 			exit(1);
509 		}
510 
511                 rc = (*handler[exitcode])(ctx, &vmexit[vcpu], &vcpu);
512 
513 		switch (rc) {
514 		case VMEXIT_CONTINUE:
515                         rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length;
516 			break;
517 		case VMEXIT_RESTART:
518                         rip = vmexit[vcpu].rip;
519 			break;
520 		case VMEXIT_RESET:
521 			exit(0);
522 		default:
523 			exit(1);
524 		}
525 	}
526 	fprintf(stderr, "vm_run error %d, errno %d\n", error, errno);
527 }
528 
529 static int
530 num_vcpus_allowed(struct vmctx *ctx)
531 {
532 	int tmp, error;
533 
534 	error = vm_get_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, &tmp);
535 
536 	/*
537 	 * The guest is allowed to spinup more than one processor only if the
538 	 * UNRESTRICTED_GUEST capability is available.
539 	 */
540 	if (error == 0)
541 		return (VM_MAXCPU);
542 	else
543 		return (1);
544 }
545 
546 void
547 fbsdrun_set_capabilities(struct vmctx *ctx, int cpu)
548 {
549 	int err, tmp;
550 
551 	if (fbsdrun_vmexit_on_hlt()) {
552 		err = vm_get_capability(ctx, cpu, VM_CAP_HALT_EXIT, &tmp);
553 		if (err < 0) {
554 			fprintf(stderr, "VM exit on HLT not supported\n");
555 			exit(1);
556 		}
557 		vm_set_capability(ctx, cpu, VM_CAP_HALT_EXIT, 1);
558 		if (cpu == BSP)
559 			handler[VM_EXITCODE_HLT] = vmexit_hlt;
560 	}
561 
562         if (fbsdrun_vmexit_on_pause()) {
563 		/*
564 		 * pause exit support required for this mode
565 		 */
566 		err = vm_get_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, &tmp);
567 		if (err < 0) {
568 			fprintf(stderr,
569 			    "SMP mux requested, no pause support\n");
570 			exit(1);
571 		}
572 		vm_set_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, 1);
573 		if (cpu == BSP)
574 			handler[VM_EXITCODE_PAUSE] = vmexit_pause;
575         }
576 
577 	if (x2apic_mode)
578 		err = vm_set_x2apic_state(ctx, cpu, X2APIC_ENABLED);
579 	else
580 		err = vm_set_x2apic_state(ctx, cpu, X2APIC_DISABLED);
581 
582 	if (err) {
583 		fprintf(stderr, "Unable to set x2apic state (%d)\n", err);
584 		exit(1);
585 	}
586 
587 	vm_set_capability(ctx, cpu, VM_CAP_ENABLE_INVPCID, 1);
588 }
589 
590 int
591 main(int argc, char *argv[])
592 {
593 	int c, error, gdb_port, err, bvmcons;
594 	int max_vcpus;
595 	struct vmctx *ctx;
596 	uint64_t rip;
597 	size_t memsize;
598 
599 	bvmcons = 0;
600 	progname = basename(argv[0]);
601 	gdb_port = 0;
602 	guest_ncpus = 1;
603 	memsize = 256 * MB;
604 
605 	while ((c = getopt(argc, argv, "abehwxAHIPWp:g:c:s:m:l:U:")) != -1) {
606 		switch (c) {
607 		case 'a':
608 			x2apic_mode = 0;
609 			break;
610 		case 'A':
611 			acpi = 1;
612 			break;
613 		case 'b':
614 			bvmcons = 1;
615 			break;
616 		case 'p':
617 			pincpu = atoi(optarg);
618 			break;
619                 case 'c':
620 			guest_ncpus = atoi(optarg);
621 			break;
622 		case 'g':
623 			gdb_port = atoi(optarg);
624 			break;
625 		case 'l':
626 			if (lpc_device_parse(optarg) != 0) {
627 				errx(EX_USAGE, "invalid lpc device "
628 				    "configuration '%s'", optarg);
629 			}
630 			break;
631 		case 's':
632 			if (pci_parse_slot(optarg) != 0)
633 				exit(1);
634 			else
635 				break;
636                 case 'm':
637 			error = vm_parse_memsize(optarg, &memsize);
638 			if (error)
639 				errx(EX_USAGE, "invalid memsize '%s'", optarg);
640 			break;
641 		case 'H':
642 			guest_vmexit_on_hlt = 1;
643 			break;
644 		case 'I':
645 			/*
646 			 * The "-I" option was used to add an ioapic to the
647 			 * virtual machine.
648 			 *
649 			 * An ioapic is now provided unconditionally for each
650 			 * virtual machine and this option is now deprecated.
651 			 */
652 			break;
653 		case 'P':
654 			guest_vmexit_on_pause = 1;
655 			break;
656 		case 'e':
657 			strictio = 1;
658 			break;
659 		case 'U':
660 			guest_uuid_str = optarg;
661 			break;
662 		case 'w':
663 			strictmsr = 0;
664 			break;
665 		case 'W':
666 			virtio_msix = 0;
667 			break;
668 		case 'x':
669 			x2apic_mode = 1;
670 			break;
671 		case 'h':
672 			usage(0);
673 		default:
674 			usage(1);
675 		}
676 	}
677 	argc -= optind;
678 	argv += optind;
679 
680 	if (argc != 1)
681 		usage(1);
682 
683 	vmname = argv[0];
684 
685 	ctx = vm_open(vmname);
686 	if (ctx == NULL) {
687 		perror("vm_open");
688 		exit(1);
689 	}
690 
691 	max_vcpus = num_vcpus_allowed(ctx);
692 	if (guest_ncpus > max_vcpus) {
693 		fprintf(stderr, "%d vCPUs requested but only %d available\n",
694 			guest_ncpus, max_vcpus);
695 		exit(1);
696 	}
697 
698 	fbsdrun_set_capabilities(ctx, BSP);
699 
700 	err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
701 	if (err) {
702 		fprintf(stderr, "Unable to setup memory (%d)\n", err);
703 		exit(1);
704 	}
705 
706 	init_mem();
707 	init_inout();
708 	ioapic_init(ctx);
709 
710 	rtc_init(ctx);
711 
712 	/*
713 	 * Exit if a device emulation finds an error in it's initilization
714 	 */
715 	if (init_pci(ctx) != 0)
716 		exit(1);
717 
718 	if (gdb_port != 0)
719 		init_dbgport(gdb_port);
720 
721 	if (bvmcons)
722 		init_bvmcons();
723 
724 	error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip);
725 	assert(error == 0);
726 
727 	/*
728 	 * build the guest tables, MP etc.
729 	 */
730 	mptable_build(ctx, guest_ncpus);
731 
732 	error = smbios_build(ctx);
733 	assert(error == 0);
734 
735 	if (acpi) {
736 		error = acpi_build(ctx, guest_ncpus);
737 		assert(error == 0);
738 	}
739 
740 	/*
741 	 * Change the proc title to include the VM name.
742 	 */
743 	setproctitle("%s", vmname);
744 
745 	/*
746 	 * Add CPU 0
747 	 */
748 	fbsdrun_addcpu(ctx, BSP, rip);
749 
750 	/*
751 	 * Head off to the main event dispatch loop
752 	 */
753 	mevent_dispatch();
754 
755 	exit(1);
756 }
757