bhyverun.c revision 266393
1221167Sgnn/*-
2221167Sgnn * Copyright (c) 2011 NetApp, Inc.
3221167Sgnn * All rights reserved.
4221167Sgnn *
5221167Sgnn * Redistribution and use in source and binary forms, with or without
6221167Sgnn * modification, are permitted provided that the following conditions
7221167Sgnn * are met:
8221167Sgnn * 1. Redistributions of source code must retain the above copyright
9221167Sgnn *    notice, this list of conditions and the following disclaimer.
10221167Sgnn * 2. Redistributions in binary form must reproduce the above copyright
11221167Sgnn *    notice, this list of conditions and the following disclaimer in the
12221167Sgnn *    documentation and/or other materials provided with the distribution.
13221167Sgnn *
14221167Sgnn * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15221167Sgnn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16221167Sgnn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17221167Sgnn * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18221167Sgnn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19221167Sgnn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20221167Sgnn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21221167Sgnn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22221167Sgnn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23221167Sgnn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24221167Sgnn * SUCH DAMAGE.
25221167Sgnn *
26221167Sgnn * $FreeBSD: stable/10/usr.sbin/bhyve/bhyverun.c 266393 2014-05-18 04:33:24Z jhb $
27221167Sgnn */
28221167Sgnn
29221167Sgnn#include <sys/cdefs.h>
30221167Sgnn__FBSDID("$FreeBSD: stable/10/usr.sbin/bhyve/bhyverun.c 266393 2014-05-18 04:33:24Z jhb $");
31221167Sgnn
32221167Sgnn#include <sys/types.h>
33221167Sgnn#include <sys/mman.h>
34221167Sgnn#include <sys/time.h>
35221167Sgnn
36221167Sgnn#include <machine/atomic.h>
37221167Sgnn#include <machine/segments.h>
38221167Sgnn
39221167Sgnn#include <stdio.h>
40221167Sgnn#include <stdlib.h>
41221167Sgnn#include <string.h>
42221167Sgnn#include <err.h>
43221167Sgnn#include <libgen.h>
44221167Sgnn#include <unistd.h>
45221167Sgnn#include <assert.h>
46221167Sgnn#include <errno.h>
47221167Sgnn#include <pthread.h>
48221167Sgnn#include <pthread_np.h>
49221167Sgnn#include <sysexits.h>
50221167Sgnn
51221167Sgnn#include <machine/vmm.h>
52221167Sgnn#include <vmmapi.h>
53221167Sgnn
54221167Sgnn#include "bhyverun.h"
55221167Sgnn#include "acpi.h"
56221167Sgnn#include "inout.h"
57221167Sgnn#include "dbgport.h"
58221167Sgnn#include "legacy_irq.h"
59221167Sgnn#include "mem.h"
60221167Sgnn#include "mevent.h"
61221167Sgnn#include "mptbl.h"
62221167Sgnn#include "pci_emul.h"
63221167Sgnn#include "pci_lpc.h"
64221167Sgnn#include "xmsr.h"
65221167Sgnn#include "spinup_ap.h"
66221167Sgnn#include "rtc.h"
67221167Sgnn
68221167Sgnn#define GUEST_NIO_PORT		0x488	/* guest upcalls via i/o port */
69221167Sgnn
70221167Sgnn#define	VMEXIT_SWITCH		0	/* force vcpu switch in mux mode */
71221167Sgnn#define	VMEXIT_CONTINUE		1	/* continue from next instruction */
72221167Sgnn#define	VMEXIT_RESTART		2	/* restart current instruction */
73221167Sgnn#define	VMEXIT_ABORT		3	/* abort the vm run loop */
74221167Sgnn#define	VMEXIT_RESET		4	/* guest machine has reset */
75221167Sgnn#define	VMEXIT_POWEROFF		5	/* guest machine has powered off */
76221167Sgnn
77221167Sgnn#define MB		(1024UL * 1024)
78221167Sgnn#define GB		(1024UL * MB)
79221167Sgnn
80221167Sgnntypedef int (*vmexit_handler_t)(struct vmctx *, struct vm_exit *, int *vcpu);
81221167Sgnn
82221167Sgnnchar *vmname;
83221167Sgnn
84221167Sgnnint guest_ncpus;
85221167Sgnn
86221167Sgnnstatic int pincpu = -1;
87221167Sgnnstatic int guest_vmexit_on_hlt, guest_vmexit_on_pause, disable_x2apic;
88221167Sgnnstatic int virtio_msix = 1;
89221167Sgnn
90221167Sgnnstatic int strictio;
91221167Sgnnstatic int strictmsr = 1;
92221167Sgnn
93221167Sgnnstatic int acpi;
94221167Sgnn
95221167Sgnnstatic char *progname;
96221167Sgnnstatic const int BSP = 0;
97221167Sgnn
98221167Sgnnstatic int cpumask;
99221167Sgnn
100221167Sgnnstatic void vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip);
101221167Sgnn
102221167Sgnnstruct vm_exit vmexit[VM_MAXCPU];
103221167Sgnn
104221167Sgnnstruct bhyvestats {
105221167Sgnn        uint64_t        vmexit_bogus;
106221167Sgnn        uint64_t        vmexit_bogus_switch;
107221167Sgnn        uint64_t        vmexit_hlt;
108221167Sgnn        uint64_t        vmexit_pause;
109221167Sgnn        uint64_t        vmexit_mtrap;
110221167Sgnn        uint64_t        vmexit_inst_emul;
111221167Sgnn        uint64_t        cpu_switch_rotate;
112221167Sgnn        uint64_t        cpu_switch_direct;
113221167Sgnn        int             io_reset;
114221167Sgnn} stats;
115221167Sgnn
116221167Sgnnstruct mt_vmm_info {
117221167Sgnn	pthread_t	mt_thr;
118221167Sgnn	struct vmctx	*mt_ctx;
119221167Sgnn	int		mt_vcpu;
120221167Sgnn} mt_vmm_info[VM_MAXCPU];
121221167Sgnn
122221167Sgnnstatic void
123221167Sgnnusage(int code)
124221167Sgnn{
125221167Sgnn
126221167Sgnn        fprintf(stderr,
127221167Sgnn                "Usage: %s [-aehwAHIPW] [-g <gdb port>] [-s <pci>] [-S <pci>]\n"
128221167Sgnn		"       %*s [-c vcpus] [-p pincpu] [-m mem] [-l <lpc>] <vm>\n"
129221167Sgnn		"       -a: local apic is in XAPIC mode (default is X2APIC)\n"
130221167Sgnn		"       -A: create an ACPI table\n"
131221167Sgnn		"       -g: gdb port\n"
132221167Sgnn		"       -c: # cpus (default 1)\n"
133221167Sgnn		"       -p: pin vcpu 'n' to host cpu 'pincpu + n'\n"
134221167Sgnn		"       -H: vmexit from the guest on hlt\n"
135221167Sgnn		"       -P: vmexit from the guest on pause\n"
136221167Sgnn		"       -W: force virtio to use single-vector MSI\n"
137221167Sgnn		"       -e: exit on unhandled I/O access\n"
138221167Sgnn		"       -h: help\n"
139221167Sgnn		"       -s: <slot,driver,configinfo> PCI slot config\n"
140221167Sgnn		"       -S: <slot,driver,configinfo> legacy PCI slot config\n"
141221167Sgnn		"       -l: LPC device configuration\n"
142221167Sgnn		"       -m: memory size in MB\n"
143221167Sgnn		"       -w: ignore unimplemented MSRs\n",
144221167Sgnn		progname, (int)strlen(progname), "");
145221167Sgnn
146221167Sgnn	exit(code);
147221167Sgnn}
148221167Sgnn
149221167Sgnnvoid *
150221167Sgnnpaddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len)
151221167Sgnn{
152221167Sgnn
153221167Sgnn	return (vm_map_gpa(ctx, gaddr, len));
154221167Sgnn}
155221167Sgnn
156221167Sgnnint
157221167Sgnnfbsdrun_disable_x2apic(void)
158221167Sgnn{
159221167Sgnn
160221167Sgnn	return (disable_x2apic);
161221167Sgnn}
162221167Sgnn
163221167Sgnnint
164221167Sgnnfbsdrun_vmexit_on_pause(void)
165221167Sgnn{
166221167Sgnn
167221167Sgnn	return (guest_vmexit_on_pause);
168221167Sgnn}
169221167Sgnn
170221167Sgnnint
171221167Sgnnfbsdrun_vmexit_on_hlt(void)
172221167Sgnn{
173221167Sgnn
174221167Sgnn	return (guest_vmexit_on_hlt);
175221167Sgnn}
176221167Sgnn
177221167Sgnnint
178221167Sgnnfbsdrun_virtio_msix(void)
179221167Sgnn{
180221167Sgnn
181221167Sgnn	return (virtio_msix);
182221167Sgnn}
183221167Sgnn
184221167Sgnnstatic void *
185221167Sgnnfbsdrun_start_thread(void *param)
186221167Sgnn{
187221167Sgnn	char tname[MAXCOMLEN + 1];
188221167Sgnn	struct mt_vmm_info *mtp;
189221167Sgnn	int vcpu;
190221167Sgnn
191221167Sgnn	mtp = param;
192221167Sgnn	vcpu = mtp->mt_vcpu;
193221167Sgnn
194221167Sgnn	snprintf(tname, sizeof(tname), "vcpu %d", vcpu);
195221167Sgnn	pthread_set_name_np(mtp->mt_thr, tname);
196221167Sgnn
197221167Sgnn	vm_loop(mtp->mt_ctx, vcpu, vmexit[vcpu].rip);
198221167Sgnn
199221167Sgnn	/* not reached */
200221167Sgnn	exit(1);
201221167Sgnn	return (NULL);
202221167Sgnn}
203221167Sgnn
204221167Sgnnvoid
205221167Sgnnfbsdrun_addcpu(struct vmctx *ctx, int vcpu, uint64_t rip)
206221167Sgnn{
207221167Sgnn	int error;
208221167Sgnn
209221167Sgnn	if (cpumask & (1 << vcpu)) {
210221167Sgnn		fprintf(stderr, "addcpu: attempting to add existing cpu %d\n",
211221167Sgnn		    vcpu);
212221167Sgnn		exit(1);
213221167Sgnn	}
214221167Sgnn
215221167Sgnn	atomic_set_int(&cpumask, 1 << vcpu);
216221167Sgnn
217221167Sgnn	/*
218221167Sgnn	 * Set up the vmexit struct to allow execution to start
219221167Sgnn	 * at the given RIP
220221167Sgnn	 */
221221167Sgnn	vmexit[vcpu].rip = rip;
222221167Sgnn	vmexit[vcpu].inst_length = 0;
223221167Sgnn
224221167Sgnn	mt_vmm_info[vcpu].mt_ctx = ctx;
225221167Sgnn	mt_vmm_info[vcpu].mt_vcpu = vcpu;
226221167Sgnn
227221167Sgnn	error = pthread_create(&mt_vmm_info[vcpu].mt_thr, NULL,
228221167Sgnn	    fbsdrun_start_thread, &mt_vmm_info[vcpu]);
229221167Sgnn	assert(error == 0);
230221167Sgnn}
231221167Sgnn
232221167Sgnnstatic int
233221167Sgnnfbsdrun_deletecpu(struct vmctx *ctx, int vcpu)
234221167Sgnn{
235221167Sgnn
236221167Sgnn	if ((cpumask & (1 << vcpu)) == 0) {
237221167Sgnn		fprintf(stderr, "addcpu: attempting to delete unknown cpu %d\n",
238221167Sgnn		    vcpu);
239221167Sgnn		exit(1);
240221167Sgnn	}
241221167Sgnn
242221167Sgnn	atomic_clear_int(&cpumask, 1 << vcpu);
243221167Sgnn	return (cpumask == 0);
244221167Sgnn}
245221167Sgnn
246221167Sgnnstatic int
247221167Sgnnvmexit_catch_reset(void)
248221167Sgnn{
249221167Sgnn        stats.io_reset++;
250221167Sgnn        return (VMEXIT_RESET);
251221167Sgnn}
252221167Sgnn
253221167Sgnnstatic int
254221167Sgnnvmexit_catch_inout(void)
255221167Sgnn{
256221167Sgnn	return (VMEXIT_ABORT);
257221167Sgnn}
258221167Sgnn
259221167Sgnnstatic int
260221167Sgnnvmexit_handle_notify(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu,
261221167Sgnn		     uint32_t eax)
262221167Sgnn{
263221167Sgnn#if BHYVE_DEBUG
264221167Sgnn	/*
265221167Sgnn	 * put guest-driven debug here
266221167Sgnn	 */
267221167Sgnn#endif
268221167Sgnn        return (VMEXIT_CONTINUE);
269221167Sgnn}
270221167Sgnn
271221167Sgnnstatic int
272221167Sgnnvmexit_inout(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
273221167Sgnn{
274221167Sgnn	int error;
275221167Sgnn	int bytes, port, in, out;
276221167Sgnn	uint32_t eax;
277221167Sgnn	int vcpu;
278221167Sgnn
279221167Sgnn	vcpu = *pvcpu;
280221167Sgnn
281221167Sgnn	port = vme->u.inout.port;
282221167Sgnn	bytes = vme->u.inout.bytes;
283221167Sgnn	eax = vme->u.inout.eax;
284221167Sgnn	in = vme->u.inout.in;
285221167Sgnn	out = !in;
286221167Sgnn
287221167Sgnn	/* We don't deal with these */
288221167Sgnn	if (vme->u.inout.string || vme->u.inout.rep)
289221167Sgnn		return (VMEXIT_ABORT);
290221167Sgnn
291221167Sgnn	/* Special case of guest reset */
292221167Sgnn	if (out && port == 0x64 && (uint8_t)eax == 0xFE)
293221167Sgnn		return (vmexit_catch_reset());
294221167Sgnn
295221167Sgnn        /* Extra-special case of host notifications */
296221167Sgnn        if (out && port == GUEST_NIO_PORT)
297221167Sgnn                return (vmexit_handle_notify(ctx, vme, pvcpu, eax));
298221167Sgnn
299221167Sgnn	error = emulate_inout(ctx, vcpu, in, port, bytes, &eax, strictio);
300221167Sgnn	if (error == INOUT_OK && in)
301221167Sgnn		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX, eax);
302221167Sgnn
303221167Sgnn	switch (error) {
304221167Sgnn	case INOUT_OK:
305221167Sgnn		return (VMEXIT_CONTINUE);
306221167Sgnn	case INOUT_RESET:
307221167Sgnn		return (VMEXIT_RESET);
308221167Sgnn	case INOUT_POWEROFF:
309221167Sgnn		return (VMEXIT_POWEROFF);
310221167Sgnn	default:
311221167Sgnn		fprintf(stderr, "Unhandled %s%c 0x%04x\n",
312221167Sgnn			in ? "in" : "out",
313221167Sgnn			bytes == 1 ? 'b' : (bytes == 2 ? 'w' : 'l'), port);
314221167Sgnn		return (vmexit_catch_inout());
315221167Sgnn	}
316221167Sgnn}
317221167Sgnn
318221167Sgnnstatic int
319221167Sgnnvmexit_rdmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
320221167Sgnn{
321221167Sgnn	uint64_t val;
322221167Sgnn	uint32_t eax, edx;
323221167Sgnn	int error;
324221167Sgnn
325221167Sgnn	val = 0;
326221167Sgnn	error = emulate_rdmsr(ctx, *pvcpu, vme->u.msr.code, &val);
327221167Sgnn	if (error != 0) {
328221167Sgnn		fprintf(stderr, "rdmsr to register %#x on vcpu %d\n",
329221167Sgnn		    vme->u.msr.code, *pvcpu);
330221167Sgnn		if (strictmsr)
331221167Sgnn			return (VMEXIT_ABORT);
332221167Sgnn	}
333221167Sgnn
334221167Sgnn	eax = val;
335221167Sgnn	error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RAX, eax);
336221167Sgnn	assert(error == 0);
337221167Sgnn
338221167Sgnn	edx = val >> 32;
339221167Sgnn	error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RDX, edx);
340221167Sgnn	assert(error == 0);
341221167Sgnn
342221167Sgnn	return (VMEXIT_CONTINUE);
343221167Sgnn}
344221167Sgnn
345221167Sgnnstatic int
346221167Sgnnvmexit_wrmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
347221167Sgnn{
348221167Sgnn	int error;
349221167Sgnn
350221167Sgnn	error = emulate_wrmsr(ctx, *pvcpu, vme->u.msr.code, vme->u.msr.wval);
351221167Sgnn	if (error != 0) {
352221167Sgnn		fprintf(stderr, "wrmsr to register %#x(%#lx) on vcpu %d\n",
353221167Sgnn		    vme->u.msr.code, vme->u.msr.wval, *pvcpu);
354221167Sgnn		if (strictmsr)
355221167Sgnn			return (VMEXIT_ABORT);
356221167Sgnn	}
357221167Sgnn	return (VMEXIT_CONTINUE);
358221167Sgnn}
359221167Sgnn
360221167Sgnnstatic int
361221167Sgnnvmexit_spinup_ap(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
362221167Sgnn{
363221167Sgnn	int newcpu;
364221167Sgnn	int retval = VMEXIT_CONTINUE;
365221167Sgnn
366221167Sgnn	newcpu = spinup_ap(ctx, *pvcpu,
367221167Sgnn			   vme->u.spinup_ap.vcpu, vme->u.spinup_ap.rip);
368221167Sgnn
369221167Sgnn	return (retval);
370221167Sgnn}
371221167Sgnn
372221167Sgnnstatic int
373221167Sgnnvmexit_spindown_cpu(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
374221167Sgnn{
375221167Sgnn	int lastcpu;
376221167Sgnn
377221167Sgnn	lastcpu = fbsdrun_deletecpu(ctx, *pvcpu);
378221167Sgnn	if (!lastcpu)
379221167Sgnn		pthread_exit(NULL);
380221167Sgnn	return (vmexit_catch_reset());
381221167Sgnn}
382221167Sgnn
383221167Sgnnstatic int
384221167Sgnnvmexit_vmx(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
385221167Sgnn{
386221167Sgnn
387221167Sgnn	fprintf(stderr, "vm exit[%d]\n", *pvcpu);
388221167Sgnn	fprintf(stderr, "\treason\t\tVMX\n");
389221167Sgnn	fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip);
390221167Sgnn	fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length);
391221167Sgnn	fprintf(stderr, "\tstatus\t\t%d\n", vmexit->u.vmx.status);
392221167Sgnn	fprintf(stderr, "\texit_reason\t%u\n", vmexit->u.vmx.exit_reason);
393221167Sgnn	fprintf(stderr, "\tqualification\t0x%016lx\n",
394221167Sgnn	    vmexit->u.vmx.exit_qualification);
395221167Sgnn	fprintf(stderr, "\tinst_type\t\t%d\n", vmexit->u.vmx.inst_type);
396221167Sgnn	fprintf(stderr, "\tinst_error\t\t%d\n", vmexit->u.vmx.inst_error);
397221167Sgnn
398221167Sgnn	return (VMEXIT_ABORT);
399221167Sgnn}
400221167Sgnn
401221167Sgnnstatic int
402221167Sgnnvmexit_bogus(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
403221167Sgnn{
404221167Sgnn
405221167Sgnn	stats.vmexit_bogus++;
406221167Sgnn
407221167Sgnn	return (VMEXIT_RESTART);
408221167Sgnn}
409221167Sgnn
410221167Sgnnstatic int
411221167Sgnnvmexit_hlt(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
412221167Sgnn{
413221167Sgnn
414221167Sgnn	stats.vmexit_hlt++;
415221167Sgnn
416221167Sgnn	/*
417221167Sgnn	 * Just continue execution with the next instruction. We use
418221167Sgnn	 * the HLT VM exit as a way to be friendly with the host
419221167Sgnn	 * scheduler.
420221167Sgnn	 */
421221167Sgnn	return (VMEXIT_CONTINUE);
422221167Sgnn}
423221167Sgnn
424221167Sgnnstatic int
425221167Sgnnvmexit_pause(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
426221167Sgnn{
427221167Sgnn
428221167Sgnn	stats.vmexit_pause++;
429221167Sgnn
430221167Sgnn	return (VMEXIT_CONTINUE);
431221167Sgnn}
432221167Sgnn
433221167Sgnnstatic int
434221167Sgnnvmexit_mtrap(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
435221167Sgnn{
436221167Sgnn
437221167Sgnn	stats.vmexit_mtrap++;
438221167Sgnn
439221167Sgnn	return (VMEXIT_RESTART);
440221167Sgnn}
441221167Sgnn
442221167Sgnnstatic int
443221167Sgnnvmexit_inst_emul(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
444221167Sgnn{
445221167Sgnn	int err;
446221167Sgnn	stats.vmexit_inst_emul++;
447221167Sgnn
448221167Sgnn	err = emulate_mem(ctx, *pvcpu, vmexit->u.inst_emul.gpa,
449221167Sgnn			  &vmexit->u.inst_emul.vie);
450221167Sgnn
451	if (err) {
452		if (err == EINVAL) {
453			fprintf(stderr,
454			    "Failed to emulate instruction at 0x%lx\n",
455			    vmexit->rip);
456		} else if (err == ESRCH) {
457			fprintf(stderr, "Unhandled memory access to 0x%lx\n",
458			    vmexit->u.inst_emul.gpa);
459		}
460
461		return (VMEXIT_ABORT);
462	}
463
464	return (VMEXIT_CONTINUE);
465}
466
467static vmexit_handler_t handler[VM_EXITCODE_MAX] = {
468	[VM_EXITCODE_INOUT]  = vmexit_inout,
469	[VM_EXITCODE_VMX]    = vmexit_vmx,
470	[VM_EXITCODE_BOGUS]  = vmexit_bogus,
471	[VM_EXITCODE_RDMSR]  = vmexit_rdmsr,
472	[VM_EXITCODE_WRMSR]  = vmexit_wrmsr,
473	[VM_EXITCODE_MTRAP]  = vmexit_mtrap,
474	[VM_EXITCODE_INST_EMUL] = vmexit_inst_emul,
475	[VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap,
476	[VM_EXITCODE_SPINDOWN_CPU] = vmexit_spindown_cpu,
477};
478
479static void
480vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip)
481{
482	cpuset_t mask;
483	int error, rc, prevcpu;
484	enum vm_exitcode exitcode;
485
486	if (pincpu >= 0) {
487		CPU_ZERO(&mask);
488		CPU_SET(pincpu + vcpu, &mask);
489		error = pthread_setaffinity_np(pthread_self(),
490					       sizeof(mask), &mask);
491		assert(error == 0);
492	}
493
494	while (1) {
495		error = vm_run(ctx, vcpu, rip, &vmexit[vcpu]);
496		if (error != 0)
497			break;
498
499		prevcpu = vcpu;
500
501		exitcode = vmexit[vcpu].exitcode;
502		if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) {
503			fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n",
504			    exitcode);
505			exit(1);
506		}
507
508                rc = (*handler[exitcode])(ctx, &vmexit[vcpu], &vcpu);
509
510		switch (rc) {
511		case VMEXIT_CONTINUE:
512                        rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length;
513			break;
514		case VMEXIT_RESTART:
515                        rip = vmexit[vcpu].rip;
516			break;
517		case VMEXIT_RESET:
518			exit(0);
519		default:
520			exit(1);
521		}
522	}
523	fprintf(stderr, "vm_run error %d, errno %d\n", error, errno);
524}
525
526static int
527num_vcpus_allowed(struct vmctx *ctx)
528{
529	int tmp, error;
530
531	error = vm_get_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, &tmp);
532
533	/*
534	 * The guest is allowed to spinup more than one processor only if the
535	 * UNRESTRICTED_GUEST capability is available.
536	 */
537	if (error == 0)
538		return (VM_MAXCPU);
539	else
540		return (1);
541}
542
543void
544fbsdrun_set_capabilities(struct vmctx *ctx, int cpu)
545{
546	int err, tmp;
547
548	if (fbsdrun_vmexit_on_hlt()) {
549		err = vm_get_capability(ctx, cpu, VM_CAP_HALT_EXIT, &tmp);
550		if (err < 0) {
551			fprintf(stderr, "VM exit on HLT not supported\n");
552			exit(1);
553		}
554		vm_set_capability(ctx, cpu, VM_CAP_HALT_EXIT, 1);
555		if (cpu == BSP)
556			handler[VM_EXITCODE_HLT] = vmexit_hlt;
557	}
558
559        if (fbsdrun_vmexit_on_pause()) {
560		/*
561		 * pause exit support required for this mode
562		 */
563		err = vm_get_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, &tmp);
564		if (err < 0) {
565			fprintf(stderr,
566			    "SMP mux requested, no pause support\n");
567			exit(1);
568		}
569		vm_set_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, 1);
570		if (cpu == BSP)
571			handler[VM_EXITCODE_PAUSE] = vmexit_pause;
572        }
573
574	if (fbsdrun_disable_x2apic())
575		err = vm_set_x2apic_state(ctx, cpu, X2APIC_DISABLED);
576	else
577		err = vm_set_x2apic_state(ctx, cpu, X2APIC_ENABLED);
578
579	if (err) {
580		fprintf(stderr, "Unable to set x2apic state (%d)\n", err);
581		exit(1);
582	}
583
584	vm_set_capability(ctx, cpu, VM_CAP_ENABLE_INVPCID, 1);
585}
586
587int
588main(int argc, char *argv[])
589{
590	int c, error, gdb_port, err, bvmcons;
591	int max_vcpus;
592	struct vmctx *ctx;
593	uint64_t rip;
594	size_t memsize;
595
596	bvmcons = 0;
597	progname = basename(argv[0]);
598	gdb_port = 0;
599	guest_ncpus = 1;
600	memsize = 256 * MB;
601
602	while ((c = getopt(argc, argv, "abehwAHIPWp:g:c:s:S:m:l:")) != -1) {
603		switch (c) {
604		case 'a':
605			disable_x2apic = 1;
606			break;
607		case 'A':
608			acpi = 1;
609			break;
610		case 'b':
611			bvmcons = 1;
612			break;
613		case 'p':
614			pincpu = atoi(optarg);
615			break;
616                case 'c':
617			guest_ncpus = atoi(optarg);
618			break;
619		case 'g':
620			gdb_port = atoi(optarg);
621			break;
622		case 'l':
623			if (lpc_device_parse(optarg) != 0) {
624				errx(EX_USAGE, "invalid lpc device "
625				    "configuration '%s'", optarg);
626			}
627			break;
628		case 's':
629			if (pci_parse_slot(optarg, 0) != 0)
630				exit(1);
631			else
632				break;
633		case 'S':
634			if (pci_parse_slot(optarg, 1) != 0)
635				exit(1);
636			else
637				break;
638                case 'm':
639			error = vm_parse_memsize(optarg, &memsize);
640			if (error)
641				errx(EX_USAGE, "invalid memsize '%s'", optarg);
642			break;
643		case 'H':
644			guest_vmexit_on_hlt = 1;
645			break;
646		case 'I':
647			/*
648			 * The "-I" option was used to add an ioapic to the
649			 * virtual machine.
650			 *
651			 * An ioapic is now provided unconditionally for each
652			 * virtual machine and this option is now deprecated.
653			 */
654			break;
655		case 'P':
656			guest_vmexit_on_pause = 1;
657			break;
658		case 'e':
659			strictio = 1;
660			break;
661		case 'w':
662			strictmsr = 0;
663			break;
664		case 'W':
665			virtio_msix = 0;
666			break;
667		case 'h':
668			usage(0);
669		default:
670			usage(1);
671		}
672	}
673	argc -= optind;
674	argv += optind;
675
676	if (argc != 1)
677		usage(1);
678
679	vmname = argv[0];
680
681	ctx = vm_open(vmname);
682	if (ctx == NULL) {
683		perror("vm_open");
684		exit(1);
685	}
686
687	max_vcpus = num_vcpus_allowed(ctx);
688	if (guest_ncpus > max_vcpus) {
689		fprintf(stderr, "%d vCPUs requested but only %d available\n",
690			guest_ncpus, max_vcpus);
691		exit(1);
692	}
693
694	fbsdrun_set_capabilities(ctx, BSP);
695
696	err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
697	if (err) {
698		fprintf(stderr, "Unable to setup memory (%d)\n", err);
699		exit(1);
700	}
701
702	init_mem();
703	init_inout();
704	legacy_irq_init();
705
706	rtc_init(ctx);
707
708	/*
709	 * Exit if a device emulation finds an error in it's initilization
710	 */
711	if (init_pci(ctx) != 0)
712		exit(1);
713
714	if (gdb_port != 0)
715		init_dbgport(gdb_port);
716
717	if (bvmcons)
718		init_bvmcons();
719
720	error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip);
721	assert(error == 0);
722
723	/*
724	 * build the guest tables, MP etc.
725	 */
726	mptable_build(ctx, guest_ncpus);
727
728	if (acpi) {
729		error = acpi_build(ctx, guest_ncpus);
730		assert(error == 0);
731	}
732
733	/*
734	 * Change the proc title to include the VM name.
735	 */
736	setproctitle("%s", vmname);
737
738	/*
739	 * Add CPU 0
740	 */
741	fbsdrun_addcpu(ctx, BSP, rip);
742
743	/*
744	 * Head off to the main event dispatch loop
745	 */
746	mevent_dispatch();
747
748	exit(1);
749}
750