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 MB (1024UL * 1024)
73 #define GB (1024UL * MB)
75 typedef int (*vmexit_handler_t)(struct vmctx *, struct vm_exit *, int *vcpu);
76 extern int vmexit_task_switch(struct vmctx *, struct vm_exit *, int *vcpu);
83 static int guest_vmexit_on_hlt, guest_vmexit_on_pause;
84 static int virtio_msix = 1;
85 static int x2apic_mode = 0; /* default is xAPIC */
88 static int strictmsr = 1;
92 static char *progname;
93 static const int BSP = 0;
95 static cpuset_t cpumask;
97 static void vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip);
99 struct vm_exit vmexit[VM_MAXCPU];
102 uint64_t vmexit_bogus;
103 uint64_t vmexit_bogus_switch;
105 uint64_t vmexit_pause;
106 uint64_t vmexit_mtrap;
107 uint64_t vmexit_inst_emul;
108 uint64_t cpu_switch_rotate;
109 uint64_t cpu_switch_direct;
116 struct vmctx *mt_ctx;
118 } mt_vmm_info[VM_MAXCPU];
120 static cpuset_t *vcpumap[VM_MAXCPU] = { NULL };
127 "Usage: %s [-abehwxACHPWY] [-c vcpus] [-g <gdb port>] [-l <lpc>]\n"
128 " %*s [-m mem] [-p vcpu:hostcpu] [-s <pci>] [-U uuid] <vm>\n"
129 " -a: local apic is in xAPIC mode (deprecated)\n"
130 " -A: create ACPI tables\n"
131 " -c: # cpus (default 1)\n"
132 " -C: include guest memory in core file\n"
133 " -e: exit on unhandled I/O access\n"
136 " -H: vmexit from the guest on hlt\n"
137 " -l: LPC device configuration\n"
138 " -m: memory size in MB\n"
139 " -p: pin 'vcpu' to 'hostcpu'\n"
140 " -P: vmexit from the guest on pause\n"
141 " -s: <slot,driver,configinfo> PCI slot config\n"
143 " -w: ignore unimplemented MSRs\n"
144 " -W: force virtio to use single-vector MSI\n"
145 " -x: local apic is in x2APIC mode\n"
146 " -Y: disable MPtable generation\n",
147 progname, (int)strlen(progname), "");
153 pincpu_parse(const char *opt)
157 if (sscanf(opt, "%d:%d", &vcpu, &pcpu) != 2) {
158 fprintf(stderr, "invalid format: %s\n", opt);
162 if (vcpu < 0 || vcpu >= VM_MAXCPU) {
163 fprintf(stderr, "vcpu '%d' outside valid range from 0 to %d\n",
164 vcpu, VM_MAXCPU - 1);
168 if (pcpu < 0 || pcpu >= CPU_SETSIZE) {
169 fprintf(stderr, "hostcpu '%d' outside valid range from "
170 "0 to %d\n", pcpu, CPU_SETSIZE - 1);
174 if (vcpumap[vcpu] == NULL) {
175 if ((vcpumap[vcpu] = malloc(sizeof(cpuset_t))) == NULL) {
179 CPU_ZERO(vcpumap[vcpu]);
181 CPU_SET(pcpu, vcpumap[vcpu]);
186 paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len)
189 return (vm_map_gpa(ctx, gaddr, len));
193 fbsdrun_vmexit_on_pause(void)
196 return (guest_vmexit_on_pause);
200 fbsdrun_vmexit_on_hlt(void)
203 return (guest_vmexit_on_hlt);
207 fbsdrun_virtio_msix(void)
210 return (virtio_msix);
214 fbsdrun_start_thread(void *param)
216 char tname[MAXCOMLEN + 1];
217 struct mt_vmm_info *mtp;
223 snprintf(tname, sizeof(tname), "vcpu %d", vcpu);
224 pthread_set_name_np(mtp->mt_thr, tname);
226 vm_loop(mtp->mt_ctx, vcpu, vmexit[vcpu].rip);
234 fbsdrun_addcpu(struct vmctx *ctx, int fromcpu, int newcpu, uint64_t rip)
238 assert(fromcpu == BSP);
241 * The 'newcpu' must be activated in the context of 'fromcpu'. If
242 * vm_activate_cpu() is delayed until newcpu's pthread starts running
243 * then vmm.ko is out-of-sync with bhyve and this can create a race
246 error = vm_activate_cpu(ctx, newcpu);
249 CPU_SET_ATOMIC(newcpu, &cpumask);
252 * Set up the vmexit struct to allow execution to start
255 vmexit[newcpu].rip = rip;
256 vmexit[newcpu].inst_length = 0;
258 mt_vmm_info[newcpu].mt_ctx = ctx;
259 mt_vmm_info[newcpu].mt_vcpu = newcpu;
261 error = pthread_create(&mt_vmm_info[newcpu].mt_thr, NULL,
262 fbsdrun_start_thread, &mt_vmm_info[newcpu]);
267 fbsdrun_deletecpu(struct vmctx *ctx, int vcpu)
270 if (!CPU_ISSET(vcpu, &cpumask)) {
271 fprintf(stderr, "Attempting to delete unknown cpu %d\n", vcpu);
275 CPU_CLR_ATOMIC(vcpu, &cpumask);
276 return (CPU_EMPTY(&cpumask));
280 vmexit_handle_notify(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu,
285 * put guest-driven debug here
288 return (VMEXIT_CONTINUE);
292 vmexit_inout(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
295 int bytes, port, in, out, string;
300 port = vme->u.inout.port;
301 bytes = vme->u.inout.bytes;
302 string = vme->u.inout.string;
303 in = vme->u.inout.in;
306 /* Extra-special case of host notifications */
307 if (out && port == GUEST_NIO_PORT) {
308 error = vmexit_handle_notify(ctx, vme, pvcpu, vme->u.inout.eax);
312 error = emulate_inout(ctx, vcpu, vme, strictio);
313 if (error == INOUT_OK && in && !string) {
314 error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX,
320 return (VMEXIT_CONTINUE);
322 return (VMEXIT_RESTART);
325 return (VMEXIT_RESET);
328 return (VMEXIT_POWEROFF);
330 fprintf(stderr, "Unhandled %s%c 0x%04x\n",
332 bytes == 1 ? 'b' : (bytes == 2 ? 'w' : 'l'), port);
333 return (VMEXIT_ABORT);
338 vmexit_rdmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
345 error = emulate_rdmsr(ctx, *pvcpu, vme->u.msr.code, &val);
347 fprintf(stderr, "rdmsr to register %#x on vcpu %d\n",
348 vme->u.msr.code, *pvcpu);
350 vm_inject_gp(ctx, *pvcpu, 0);
351 return (VMEXIT_RESTART);
356 error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RAX, eax);
360 error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RDX, edx);
363 return (VMEXIT_CONTINUE);
367 vmexit_wrmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
371 error = emulate_wrmsr(ctx, *pvcpu, vme->u.msr.code, vme->u.msr.wval);
373 fprintf(stderr, "wrmsr to register %#x(%#lx) on vcpu %d\n",
374 vme->u.msr.code, vme->u.msr.wval, *pvcpu);
376 vm_inject_gp(ctx, *pvcpu, 0);
377 return (VMEXIT_RESTART);
380 return (VMEXIT_CONTINUE);
384 vmexit_spinup_ap(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
387 int retval = VMEXIT_CONTINUE;
389 newcpu = spinup_ap(ctx, *pvcpu,
390 vme->u.spinup_ap.vcpu, vme->u.spinup_ap.rip);
395 #define DEBUG_EPT_MISCONFIG
396 #ifdef DEBUG_EPT_MISCONFIG
397 #define EXIT_REASON_EPT_MISCONFIG 49
398 #define VMCS_GUEST_PHYSICAL_ADDRESS 0x00002400
399 #define VMCS_IDENT(x) ((x) | 0x80000000)
401 static uint64_t ept_misconfig_gpa, ept_misconfig_pte[4];
402 static int ept_misconfig_ptenum;
406 vmexit_vmx(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
409 fprintf(stderr, "vm exit[%d]\n", *pvcpu);
410 fprintf(stderr, "\treason\t\tVMX\n");
411 fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip);
412 fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length);
413 fprintf(stderr, "\tstatus\t\t%d\n", vmexit->u.vmx.status);
414 fprintf(stderr, "\texit_reason\t%u\n", vmexit->u.vmx.exit_reason);
415 fprintf(stderr, "\tqualification\t0x%016lx\n",
416 vmexit->u.vmx.exit_qualification);
417 fprintf(stderr, "\tinst_type\t\t%d\n", vmexit->u.vmx.inst_type);
418 fprintf(stderr, "\tinst_error\t\t%d\n", vmexit->u.vmx.inst_error);
419 #ifdef DEBUG_EPT_MISCONFIG
420 if (vmexit->u.vmx.exit_reason == EXIT_REASON_EPT_MISCONFIG) {
421 vm_get_register(ctx, *pvcpu,
422 VMCS_IDENT(VMCS_GUEST_PHYSICAL_ADDRESS),
424 vm_get_gpa_pmap(ctx, ept_misconfig_gpa, ept_misconfig_pte,
425 &ept_misconfig_ptenum);
426 fprintf(stderr, "\tEPT misconfiguration:\n");
427 fprintf(stderr, "\t\tGPA: %#lx\n", ept_misconfig_gpa);
428 fprintf(stderr, "\t\tPTE(%d): %#lx %#lx %#lx %#lx\n",
429 ept_misconfig_ptenum, ept_misconfig_pte[0],
430 ept_misconfig_pte[1], ept_misconfig_pte[2],
431 ept_misconfig_pte[3]);
433 #endif /* DEBUG_EPT_MISCONFIG */
434 return (VMEXIT_ABORT);
438 vmexit_bogus(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
441 stats.vmexit_bogus++;
443 return (VMEXIT_RESTART);
447 vmexit_hlt(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
453 * Just continue execution with the next instruction. We use
454 * the HLT VM exit as a way to be friendly with the host
457 return (VMEXIT_CONTINUE);
461 vmexit_pause(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
464 stats.vmexit_pause++;
466 return (VMEXIT_CONTINUE);
470 vmexit_mtrap(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
473 stats.vmexit_mtrap++;
475 return (VMEXIT_RESTART);
479 vmexit_inst_emul(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
482 stats.vmexit_inst_emul++;
484 err = emulate_mem(ctx, *pvcpu, vmexit->u.inst_emul.gpa,
485 &vmexit->u.inst_emul.vie, &vmexit->u.inst_emul.paging);
490 "Failed to emulate instruction at 0x%lx\n",
492 } else if (err == ESRCH) {
493 fprintf(stderr, "Unhandled memory access to 0x%lx\n",
494 vmexit->u.inst_emul.gpa);
497 return (VMEXIT_ABORT);
500 return (VMEXIT_CONTINUE);
503 static pthread_mutex_t resetcpu_mtx = PTHREAD_MUTEX_INITIALIZER;
504 static pthread_cond_t resetcpu_cond = PTHREAD_COND_INITIALIZER;
507 vmexit_suspend(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
509 enum vm_suspend_how how;
511 how = vmexit->u.suspended.how;
513 fbsdrun_deletecpu(ctx, *pvcpu);
516 pthread_mutex_lock(&resetcpu_mtx);
517 pthread_cond_signal(&resetcpu_cond);
518 pthread_mutex_unlock(&resetcpu_mtx);
522 pthread_mutex_lock(&resetcpu_mtx);
523 while (!CPU_EMPTY(&cpumask)) {
524 pthread_cond_wait(&resetcpu_cond, &resetcpu_mtx);
526 pthread_mutex_unlock(&resetcpu_mtx);
529 case VM_SUSPEND_RESET:
531 case VM_SUSPEND_POWEROFF:
533 case VM_SUSPEND_HALT:
535 case VM_SUSPEND_TRIPLEFAULT:
538 fprintf(stderr, "vmexit_suspend: invalid reason %d\n", how);
541 return (0); /* NOTREACHED */
544 static vmexit_handler_t handler[VM_EXITCODE_MAX] = {
545 [VM_EXITCODE_INOUT] = vmexit_inout,
546 [VM_EXITCODE_INOUT_STR] = vmexit_inout,
547 [VM_EXITCODE_VMX] = vmexit_vmx,
548 [VM_EXITCODE_BOGUS] = vmexit_bogus,
549 [VM_EXITCODE_RDMSR] = vmexit_rdmsr,
550 [VM_EXITCODE_WRMSR] = vmexit_wrmsr,
551 [VM_EXITCODE_MTRAP] = vmexit_mtrap,
552 [VM_EXITCODE_INST_EMUL] = vmexit_inst_emul,
553 [VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap,
554 [VM_EXITCODE_SUSPENDED] = vmexit_suspend,
555 [VM_EXITCODE_TASK_SWITCH] = vmexit_task_switch,
559 vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip)
561 int error, rc, prevcpu;
562 enum vm_exitcode exitcode;
563 enum vm_suspend_how how;
564 cpuset_t active_cpus;
566 if (vcpumap[vcpu] != NULL) {
567 error = pthread_setaffinity_np(pthread_self(),
568 sizeof(cpuset_t), vcpumap[vcpu]);
572 error = vm_active_cpus(ctx, &active_cpus);
573 assert(CPU_ISSET(vcpu, &active_cpus));
576 error = vm_run(ctx, vcpu, rip, &vmexit[vcpu]);
582 exitcode = vmexit[vcpu].exitcode;
583 if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) {
584 fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n",
589 rc = (*handler[exitcode])(ctx, &vmexit[vcpu], &vcpu);
592 case VMEXIT_CONTINUE:
593 rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length;
596 rip = vmexit[vcpu].rip;
599 case VMEXIT_POWEROFF:
600 if (rc == VMEXIT_RESET)
601 how = VM_SUSPEND_RESET;
603 how = VM_SUSPEND_POWEROFF;
604 error = vm_suspend(ctx, how);
605 assert(error == 0 || errno == EALREADY);
606 rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length;
614 fprintf(stderr, "vm_run error %d, errno %d\n", error, errno);
618 num_vcpus_allowed(struct vmctx *ctx)
622 error = vm_get_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, &tmp);
625 * The guest is allowed to spinup more than one processor only if the
626 * UNRESTRICTED_GUEST capability is available.
635 fbsdrun_set_capabilities(struct vmctx *ctx, int cpu)
639 if (fbsdrun_vmexit_on_hlt()) {
640 err = vm_get_capability(ctx, cpu, VM_CAP_HALT_EXIT, &tmp);
642 fprintf(stderr, "VM exit on HLT not supported\n");
645 vm_set_capability(ctx, cpu, VM_CAP_HALT_EXIT, 1);
647 handler[VM_EXITCODE_HLT] = vmexit_hlt;
650 if (fbsdrun_vmexit_on_pause()) {
652 * pause exit support required for this mode
654 err = vm_get_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, &tmp);
657 "SMP mux requested, no pause support\n");
660 vm_set_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, 1);
662 handler[VM_EXITCODE_PAUSE] = vmexit_pause;
666 err = vm_set_x2apic_state(ctx, cpu, X2APIC_ENABLED);
668 err = vm_set_x2apic_state(ctx, cpu, X2APIC_DISABLED);
671 fprintf(stderr, "Unable to set x2apic state (%d)\n", err);
675 vm_set_capability(ctx, cpu, VM_CAP_ENABLE_INVPCID, 1);
679 main(int argc, char *argv[])
681 int c, error, gdb_port, err, bvmcons;
682 int dump_guest_memory, max_vcpus, mptgen;
688 dump_guest_memory = 0;
689 progname = basename(argv[0]);
695 while ((c = getopt(argc, argv, "abehwxACHIPWYp:g:c:s:m:l:U:")) != -1) {
707 if (pincpu_parse(optarg) != 0) {
708 errx(EX_USAGE, "invalid vcpu pinning "
709 "configuration '%s'", optarg);
713 guest_ncpus = atoi(optarg);
716 dump_guest_memory = 1;
719 gdb_port = atoi(optarg);
722 if (lpc_device_parse(optarg) != 0) {
723 errx(EX_USAGE, "invalid lpc device "
724 "configuration '%s'", optarg);
728 if (pci_parse_slot(optarg) != 0)
733 error = vm_parse_memsize(optarg, &memsize);
735 errx(EX_USAGE, "invalid memsize '%s'", optarg);
738 guest_vmexit_on_hlt = 1;
742 * The "-I" option was used to add an ioapic to the
745 * An ioapic is now provided unconditionally for each
746 * virtual machine and this option is now deprecated.
750 guest_vmexit_on_pause = 1;
756 guest_uuid_str = optarg;
784 ctx = vm_open(vmname);
790 max_vcpus = num_vcpus_allowed(ctx);
791 if (guest_ncpus > max_vcpus) {
792 fprintf(stderr, "%d vCPUs requested but only %d available\n",
793 guest_ncpus, max_vcpus);
797 fbsdrun_set_capabilities(ctx, BSP);
799 if (dump_guest_memory)
800 vm_set_memflags(ctx, VM_MEM_F_INCORE);
801 err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
803 fprintf(stderr, "Unable to setup memory (%d)\n", err);
816 * Exit if a device emulation finds an error in it's initilization
818 if (init_pci(ctx) != 0)
822 init_dbgport(gdb_port);
827 error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip);
831 * build the guest tables, MP etc.
834 error = mptable_build(ctx, guest_ncpus);
839 error = smbios_build(ctx);
843 error = acpi_build(ctx, guest_ncpus);
848 * Change the proc title to include the VM name.
850 setproctitle("%s", vmname);
855 fbsdrun_addcpu(ctx, BSP, BSP, rip);
858 * Head off to the main event dispatch loop