2 * Copyright (c) 2011 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
36 #include <machine/atomic.h>
37 #include <machine/segments.h>
48 #include <pthread_np.h>
51 #include <machine/vmm.h>
65 #include "smbiostbl.h"
67 #include "spinup_ap.h"
70 #define GUEST_NIO_PORT 0x488 /* guest upcalls via i/o port */
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 */
78 #define MB (1024UL * 1024)
79 #define GB (1024UL * MB)
81 typedef int (*vmexit_handler_t)(struct vmctx *, struct vm_exit *, int *vcpu);
88 static int guest_vmexit_on_hlt, guest_vmexit_on_pause;
89 static int virtio_msix = 1;
90 static int x2apic_mode = 0; /* default is xAPIC */
93 static int strictmsr = 1;
97 static char *progname;
98 static const int BSP = 0;
100 static cpuset_t cpumask;
102 static void vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip);
104 struct vm_exit vmexit[VM_MAXCPU];
107 uint64_t vmexit_bogus;
108 uint64_t vmexit_bogus_switch;
110 uint64_t vmexit_pause;
111 uint64_t vmexit_mtrap;
112 uint64_t vmexit_inst_emul;
113 uint64_t cpu_switch_rotate;
114 uint64_t cpu_switch_direct;
121 struct vmctx *mt_ctx;
123 } mt_vmm_info[VM_MAXCPU];
125 static cpuset_t *vcpumap[VM_MAXCPU] = { NULL };
132 "Usage: %s [-aehwAHIPW] [-g <gdb port>] [-s <pci>] [-c vcpus]\n"
133 " %*s [-p vcpu:hostcpu] [-m mem] [-l <lpc>] <vm>\n"
134 " -a: local apic is in xAPIC mode (deprecated)\n"
135 " -A: create an ACPI table\n"
137 " -c: # cpus (default 1)\n"
138 " -C: include guest memory in core file\n"
139 " -p: pin 'vcpu' to 'hostcpu'\n"
140 " -H: vmexit from the guest on hlt\n"
141 " -P: vmexit from the guest on pause\n"
142 " -W: force virtio to use single-vector MSI\n"
143 " -e: exit on unhandled I/O access\n"
145 " -s: <slot,driver,configinfo> PCI slot config\n"
146 " -l: LPC device configuration\n"
147 " -m: memory size in MB\n"
148 " -w: ignore unimplemented MSRs\n"
149 " -x: local apic is in x2APIC mode\n"
150 " -Y: disable MPtable generation\n"
152 progname, (int)strlen(progname), "");
158 pincpu_parse(const char *opt)
162 if (sscanf(opt, "%d:%d", &vcpu, &pcpu) != 2) {
163 fprintf(stderr, "invalid format: %s\n", opt);
167 if (vcpu < 0 || vcpu >= VM_MAXCPU) {
168 fprintf(stderr, "vcpu '%d' outside valid range from 0 to %d\n",
169 vcpu, VM_MAXCPU - 1);
173 if (pcpu < 0 || pcpu >= CPU_SETSIZE) {
174 fprintf(stderr, "hostcpu '%d' outside valid range from "
175 "0 to %d\n", pcpu, CPU_SETSIZE - 1);
179 if (vcpumap[vcpu] == NULL) {
180 if ((vcpumap[vcpu] = malloc(sizeof(cpuset_t))) == NULL) {
184 CPU_ZERO(vcpumap[vcpu]);
186 CPU_SET(pcpu, vcpumap[vcpu]);
191 paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len)
194 return (vm_map_gpa(ctx, gaddr, len));
198 fbsdrun_vmexit_on_pause(void)
201 return (guest_vmexit_on_pause);
205 fbsdrun_vmexit_on_hlt(void)
208 return (guest_vmexit_on_hlt);
212 fbsdrun_virtio_msix(void)
215 return (virtio_msix);
219 fbsdrun_start_thread(void *param)
221 char tname[MAXCOMLEN + 1];
222 struct mt_vmm_info *mtp;
228 snprintf(tname, sizeof(tname), "vcpu %d", vcpu);
229 pthread_set_name_np(mtp->mt_thr, tname);
231 vm_loop(mtp->mt_ctx, vcpu, vmexit[vcpu].rip);
239 fbsdrun_addcpu(struct vmctx *ctx, int fromcpu, int newcpu, uint64_t rip)
243 assert(fromcpu == BSP);
246 * The 'newcpu' must be activated in the context of 'fromcpu'. If
247 * vm_activate_cpu() is delayed until newcpu's pthread starts running
248 * then vmm.ko is out-of-sync with bhyve and this can create a race
251 error = vm_activate_cpu(ctx, newcpu);
254 CPU_SET_ATOMIC(newcpu, &cpumask);
257 * Set up the vmexit struct to allow execution to start
260 vmexit[newcpu].rip = rip;
261 vmexit[newcpu].inst_length = 0;
263 mt_vmm_info[newcpu].mt_ctx = ctx;
264 mt_vmm_info[newcpu].mt_vcpu = newcpu;
266 error = pthread_create(&mt_vmm_info[newcpu].mt_thr, NULL,
267 fbsdrun_start_thread, &mt_vmm_info[newcpu]);
272 fbsdrun_deletecpu(struct vmctx *ctx, int vcpu)
275 if (!CPU_ISSET(vcpu, &cpumask)) {
276 fprintf(stderr, "Attempting to delete unknown cpu %d\n", vcpu);
280 CPU_CLR_ATOMIC(vcpu, &cpumask);
281 return (CPU_EMPTY(&cpumask));
285 vmexit_handle_notify(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu,
290 * put guest-driven debug here
293 return (VMEXIT_CONTINUE);
297 vmexit_inout(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
300 int bytes, port, in, out, string;
305 port = vme->u.inout.port;
306 bytes = vme->u.inout.bytes;
307 string = vme->u.inout.string;
308 in = vme->u.inout.in;
311 /* Extra-special case of host notifications */
312 if (out && port == GUEST_NIO_PORT) {
313 error = vmexit_handle_notify(ctx, vme, pvcpu, vme->u.inout.eax);
317 error = emulate_inout(ctx, vcpu, vme, strictio);
318 if (error == INOUT_OK && in && !string) {
319 error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX,
325 return (VMEXIT_CONTINUE);
327 return (VMEXIT_RESTART);
330 return (VMEXIT_RESET);
333 return (VMEXIT_POWEROFF);
335 fprintf(stderr, "Unhandled %s%c 0x%04x\n",
337 bytes == 1 ? 'b' : (bytes == 2 ? 'w' : 'l'), port);
338 return (VMEXIT_ABORT);
343 vmexit_rdmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
350 error = emulate_rdmsr(ctx, *pvcpu, vme->u.msr.code, &val);
352 fprintf(stderr, "rdmsr to register %#x on vcpu %d\n",
353 vme->u.msr.code, *pvcpu);
355 error = vm_inject_exception2(ctx, *pvcpu, IDT_GP, 0);
357 return (VMEXIT_RESTART);
362 error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RAX, eax);
366 error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RDX, edx);
369 return (VMEXIT_CONTINUE);
373 vmexit_wrmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
377 error = emulate_wrmsr(ctx, *pvcpu, vme->u.msr.code, vme->u.msr.wval);
379 fprintf(stderr, "wrmsr to register %#x(%#lx) on vcpu %d\n",
380 vme->u.msr.code, vme->u.msr.wval, *pvcpu);
382 error = vm_inject_exception2(ctx, *pvcpu, IDT_GP, 0);
384 return (VMEXIT_RESTART);
387 return (VMEXIT_CONTINUE);
391 vmexit_spinup_ap(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
394 int retval = VMEXIT_CONTINUE;
396 newcpu = spinup_ap(ctx, *pvcpu,
397 vme->u.spinup_ap.vcpu, vme->u.spinup_ap.rip);
403 vmexit_vmx(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
406 fprintf(stderr, "vm exit[%d]\n", *pvcpu);
407 fprintf(stderr, "\treason\t\tVMX\n");
408 fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip);
409 fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length);
410 fprintf(stderr, "\tstatus\t\t%d\n", vmexit->u.vmx.status);
411 fprintf(stderr, "\texit_reason\t%u\n", vmexit->u.vmx.exit_reason);
412 fprintf(stderr, "\tqualification\t0x%016lx\n",
413 vmexit->u.vmx.exit_qualification);
414 fprintf(stderr, "\tinst_type\t\t%d\n", vmexit->u.vmx.inst_type);
415 fprintf(stderr, "\tinst_error\t\t%d\n", vmexit->u.vmx.inst_error);
417 return (VMEXIT_ABORT);
421 vmexit_bogus(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
424 stats.vmexit_bogus++;
426 return (VMEXIT_RESTART);
430 vmexit_hlt(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
436 * Just continue execution with the next instruction. We use
437 * the HLT VM exit as a way to be friendly with the host
440 return (VMEXIT_CONTINUE);
444 vmexit_pause(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
447 stats.vmexit_pause++;
449 return (VMEXIT_CONTINUE);
453 vmexit_mtrap(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
456 stats.vmexit_mtrap++;
458 return (VMEXIT_RESTART);
462 vmexit_inst_emul(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
465 stats.vmexit_inst_emul++;
467 err = emulate_mem(ctx, *pvcpu, vmexit->u.inst_emul.gpa,
468 &vmexit->u.inst_emul.vie);
473 "Failed to emulate instruction at 0x%lx\n",
475 } else if (err == ESRCH) {
476 fprintf(stderr, "Unhandled memory access to 0x%lx\n",
477 vmexit->u.inst_emul.gpa);
480 return (VMEXIT_ABORT);
483 return (VMEXIT_CONTINUE);
486 static pthread_mutex_t resetcpu_mtx = PTHREAD_MUTEX_INITIALIZER;
487 static pthread_cond_t resetcpu_cond = PTHREAD_COND_INITIALIZER;
490 vmexit_suspend(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
492 enum vm_suspend_how how;
494 how = vmexit->u.suspended.how;
496 fbsdrun_deletecpu(ctx, *pvcpu);
499 pthread_mutex_lock(&resetcpu_mtx);
500 pthread_cond_signal(&resetcpu_cond);
501 pthread_mutex_unlock(&resetcpu_mtx);
505 pthread_mutex_lock(&resetcpu_mtx);
506 while (!CPU_EMPTY(&cpumask)) {
507 pthread_cond_wait(&resetcpu_cond, &resetcpu_mtx);
509 pthread_mutex_unlock(&resetcpu_mtx);
512 case VM_SUSPEND_RESET:
514 case VM_SUSPEND_POWEROFF:
516 case VM_SUSPEND_HALT:
519 fprintf(stderr, "vmexit_suspend: invalid reason %d\n", how);
522 return (0); /* NOTREACHED */
525 static vmexit_handler_t handler[VM_EXITCODE_MAX] = {
526 [VM_EXITCODE_INOUT] = vmexit_inout,
527 [VM_EXITCODE_INOUT_STR] = vmexit_inout,
528 [VM_EXITCODE_VMX] = vmexit_vmx,
529 [VM_EXITCODE_BOGUS] = vmexit_bogus,
530 [VM_EXITCODE_RDMSR] = vmexit_rdmsr,
531 [VM_EXITCODE_WRMSR] = vmexit_wrmsr,
532 [VM_EXITCODE_MTRAP] = vmexit_mtrap,
533 [VM_EXITCODE_INST_EMUL] = vmexit_inst_emul,
534 [VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap,
535 [VM_EXITCODE_SUSPENDED] = vmexit_suspend
539 vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip)
541 int error, rc, prevcpu;
542 enum vm_exitcode exitcode;
543 enum vm_suspend_how how;
544 cpuset_t active_cpus;
546 if (vcpumap[vcpu] != NULL) {
547 error = pthread_setaffinity_np(pthread_self(),
548 sizeof(cpuset_t), vcpumap[vcpu]);
552 error = vm_active_cpus(ctx, &active_cpus);
553 assert(CPU_ISSET(vcpu, &active_cpus));
556 error = vm_run(ctx, vcpu, rip, &vmexit[vcpu]);
562 exitcode = vmexit[vcpu].exitcode;
563 if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) {
564 fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n",
569 rc = (*handler[exitcode])(ctx, &vmexit[vcpu], &vcpu);
572 case VMEXIT_CONTINUE:
573 rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length;
576 rip = vmexit[vcpu].rip;
579 case VMEXIT_POWEROFF:
580 if (rc == VMEXIT_RESET)
581 how = VM_SUSPEND_RESET;
583 how = VM_SUSPEND_POWEROFF;
584 error = vm_suspend(ctx, how);
585 assert(error == 0 || errno == EALREADY);
586 rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length;
594 fprintf(stderr, "vm_run error %d, errno %d\n", error, errno);
598 num_vcpus_allowed(struct vmctx *ctx)
602 error = vm_get_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, &tmp);
605 * The guest is allowed to spinup more than one processor only if the
606 * UNRESTRICTED_GUEST capability is available.
615 fbsdrun_set_capabilities(struct vmctx *ctx, int cpu)
619 if (fbsdrun_vmexit_on_hlt()) {
620 err = vm_get_capability(ctx, cpu, VM_CAP_HALT_EXIT, &tmp);
622 fprintf(stderr, "VM exit on HLT not supported\n");
625 vm_set_capability(ctx, cpu, VM_CAP_HALT_EXIT, 1);
627 handler[VM_EXITCODE_HLT] = vmexit_hlt;
630 if (fbsdrun_vmexit_on_pause()) {
632 * pause exit support required for this mode
634 err = vm_get_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, &tmp);
637 "SMP mux requested, no pause support\n");
640 vm_set_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, 1);
642 handler[VM_EXITCODE_PAUSE] = vmexit_pause;
646 err = vm_set_x2apic_state(ctx, cpu, X2APIC_ENABLED);
648 err = vm_set_x2apic_state(ctx, cpu, X2APIC_DISABLED);
651 fprintf(stderr, "Unable to set x2apic state (%d)\n", err);
655 vm_set_capability(ctx, cpu, VM_CAP_ENABLE_INVPCID, 1);
659 main(int argc, char *argv[])
661 int c, error, gdb_port, err, bvmcons;
662 int dump_guest_memory, max_vcpus, mptgen;
668 dump_guest_memory = 0;
669 progname = basename(argv[0]);
675 while ((c = getopt(argc, argv, "abehwxACHIPWYp:g:c:s:m:l:U:")) != -1) {
687 if (pincpu_parse(optarg) != 0) {
688 errx(EX_USAGE, "invalid vcpu pinning "
689 "configuration '%s'", optarg);
693 guest_ncpus = atoi(optarg);
696 dump_guest_memory = 1;
699 gdb_port = atoi(optarg);
702 if (lpc_device_parse(optarg) != 0) {
703 errx(EX_USAGE, "invalid lpc device "
704 "configuration '%s'", optarg);
708 if (pci_parse_slot(optarg) != 0)
713 error = vm_parse_memsize(optarg, &memsize);
715 errx(EX_USAGE, "invalid memsize '%s'", optarg);
718 guest_vmexit_on_hlt = 1;
722 * The "-I" option was used to add an ioapic to the
725 * An ioapic is now provided unconditionally for each
726 * virtual machine and this option is now deprecated.
730 guest_vmexit_on_pause = 1;
736 guest_uuid_str = optarg;
764 ctx = vm_open(vmname);
770 max_vcpus = num_vcpus_allowed(ctx);
771 if (guest_ncpus > max_vcpus) {
772 fprintf(stderr, "%d vCPUs requested but only %d available\n",
773 guest_ncpus, max_vcpus);
777 fbsdrun_set_capabilities(ctx, BSP);
779 if (dump_guest_memory)
780 vm_set_memflags(ctx, VM_MEM_F_INCORE);
781 err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
783 fprintf(stderr, "Unable to setup memory (%d)\n", err);
796 * Exit if a device emulation finds an error in it's initilization
798 if (init_pci(ctx) != 0)
802 init_dbgport(gdb_port);
807 error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip);
811 * build the guest tables, MP etc.
814 error = mptable_build(ctx, guest_ncpus);
819 error = smbios_build(ctx);
823 error = acpi_build(ctx, guest_ncpus);
828 * Change the proc title to include the VM name.
830 setproctitle("%s", vmname);
835 fbsdrun_addcpu(ctx, BSP, BSP, rip);
838 * Head off to the main event dispatch loop