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>
64 #include "smbiostbl.h"
66 #include "spinup_ap.h"
69 #define GUEST_NIO_PORT 0x488 /* guest upcalls via i/o port */
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 */
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 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 */
94 static int strictmsr = 1;
98 static char *progname;
99 static const int BSP = 0;
101 static cpuset_t cpumask;
103 static void vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip);
105 struct vm_exit vmexit[VM_MAXCPU];
108 uint64_t vmexit_bogus;
109 uint64_t vmexit_bogus_switch;
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;
122 struct vmctx *mt_ctx;
124 } mt_vmm_info[VM_MAXCPU];
131 "Usage: %s [-aehwAHIPW] [-g <gdb port>] [-s <pci>]\n"
132 " %*s [-c vcpus] [-p pincpu] [-m mem] [-l <lpc>] <vm>\n"
133 " -a: local apic is in xAPIC mode (deprecated)\n"
134 " -A: create an ACPI table\n"
136 " -c: # cpus (default 1)\n"
137 " -p: pin vcpu 'n' to host cpu 'pincpu + n'\n"
138 " -H: vmexit from the guest on hlt\n"
139 " -P: vmexit from the guest on pause\n"
140 " -W: force virtio to use single-vector MSI\n"
141 " -e: exit on unhandled I/O access\n"
143 " -s: <slot,driver,configinfo> PCI slot config\n"
144 " -l: LPC device configuration\n"
145 " -m: memory size in MB\n"
146 " -w: ignore unimplemented MSRs\n"
147 " -x: local apic is in x2APIC mode\n"
149 progname, (int)strlen(progname), "");
155 paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len)
158 return (vm_map_gpa(ctx, gaddr, len));
162 fbsdrun_vmexit_on_pause(void)
165 return (guest_vmexit_on_pause);
169 fbsdrun_vmexit_on_hlt(void)
172 return (guest_vmexit_on_hlt);
176 fbsdrun_virtio_msix(void)
179 return (virtio_msix);
183 fbsdrun_start_thread(void *param)
185 char tname[MAXCOMLEN + 1];
186 struct mt_vmm_info *mtp;
192 snprintf(tname, sizeof(tname), "vcpu %d", vcpu);
193 pthread_set_name_np(mtp->mt_thr, tname);
195 vm_loop(mtp->mt_ctx, vcpu, vmexit[vcpu].rip);
203 fbsdrun_addcpu(struct vmctx *ctx, int fromcpu, int newcpu, uint64_t rip)
207 assert(fromcpu == BSP);
209 CPU_SET_ATOMIC(newcpu, &cpumask);
212 * Set up the vmexit struct to allow execution to start
215 vmexit[newcpu].rip = rip;
216 vmexit[newcpu].inst_length = 0;
218 mt_vmm_info[newcpu].mt_ctx = ctx;
219 mt_vmm_info[newcpu].mt_vcpu = newcpu;
221 error = pthread_create(&mt_vmm_info[newcpu].mt_thr, NULL,
222 fbsdrun_start_thread, &mt_vmm_info[newcpu]);
227 fbsdrun_deletecpu(struct vmctx *ctx, int vcpu)
230 if (!CPU_ISSET(vcpu, &cpumask)) {
231 fprintf(stderr, "addcpu: attempting to delete unknown cpu %d\n",
236 CPU_CLR_ATOMIC(vcpu, &cpumask);
237 return (CPU_EMPTY(&cpumask));
241 vmexit_catch_inout(void)
243 return (VMEXIT_ABORT);
247 vmexit_handle_notify(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu,
252 * put guest-driven debug here
255 return (VMEXIT_CONTINUE);
259 vmexit_inout(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
262 int bytes, port, in, out;
268 port = vme->u.inout.port;
269 bytes = vme->u.inout.bytes;
270 eax = vme->u.inout.eax;
271 in = vme->u.inout.in;
274 /* We don't deal with these */
275 if (vme->u.inout.string || vme->u.inout.rep)
276 return (VMEXIT_ABORT);
278 /* Extra-special case of host notifications */
279 if (out && port == GUEST_NIO_PORT)
280 return (vmexit_handle_notify(ctx, vme, pvcpu, eax));
282 error = emulate_inout(ctx, vcpu, in, port, bytes, &eax, strictio);
283 if (error == INOUT_OK && in)
284 error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX, eax);
288 return (VMEXIT_CONTINUE);
291 return (VMEXIT_RESET);
294 return (VMEXIT_POWEROFF);
296 fprintf(stderr, "Unhandled %s%c 0x%04x\n",
298 bytes == 1 ? 'b' : (bytes == 2 ? 'w' : 'l'), port);
299 return (vmexit_catch_inout());
304 vmexit_rdmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
311 error = emulate_rdmsr(ctx, *pvcpu, vme->u.msr.code, &val);
313 fprintf(stderr, "rdmsr to register %#x on vcpu %d\n",
314 vme->u.msr.code, *pvcpu);
316 error = vm_inject_exception2(ctx, *pvcpu, IDT_GP, 0);
318 return (VMEXIT_RESTART);
323 error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RAX, eax);
327 error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RDX, edx);
330 return (VMEXIT_CONTINUE);
334 vmexit_wrmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
338 error = emulate_wrmsr(ctx, *pvcpu, vme->u.msr.code, vme->u.msr.wval);
340 fprintf(stderr, "wrmsr to register %#x(%#lx) on vcpu %d\n",
341 vme->u.msr.code, vme->u.msr.wval, *pvcpu);
343 error = vm_inject_exception2(ctx, *pvcpu, IDT_GP, 0);
345 return (VMEXIT_RESTART);
348 return (VMEXIT_CONTINUE);
352 vmexit_spinup_ap(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
355 int retval = VMEXIT_CONTINUE;
357 newcpu = spinup_ap(ctx, *pvcpu,
358 vme->u.spinup_ap.vcpu, vme->u.spinup_ap.rip);
364 vmexit_vmx(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
367 fprintf(stderr, "vm exit[%d]\n", *pvcpu);
368 fprintf(stderr, "\treason\t\tVMX\n");
369 fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip);
370 fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length);
371 fprintf(stderr, "\tstatus\t\t%d\n", vmexit->u.vmx.status);
372 fprintf(stderr, "\texit_reason\t%u\n", vmexit->u.vmx.exit_reason);
373 fprintf(stderr, "\tqualification\t0x%016lx\n",
374 vmexit->u.vmx.exit_qualification);
375 fprintf(stderr, "\tinst_type\t\t%d\n", vmexit->u.vmx.inst_type);
376 fprintf(stderr, "\tinst_error\t\t%d\n", vmexit->u.vmx.inst_error);
378 return (VMEXIT_ABORT);
382 vmexit_bogus(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
385 stats.vmexit_bogus++;
387 return (VMEXIT_RESTART);
391 vmexit_hlt(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
397 * Just continue execution with the next instruction. We use
398 * the HLT VM exit as a way to be friendly with the host
401 return (VMEXIT_CONTINUE);
405 vmexit_pause(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
408 stats.vmexit_pause++;
410 return (VMEXIT_CONTINUE);
414 vmexit_mtrap(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
417 stats.vmexit_mtrap++;
419 return (VMEXIT_RESTART);
423 vmexit_inst_emul(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
426 stats.vmexit_inst_emul++;
428 err = emulate_mem(ctx, *pvcpu, vmexit->u.inst_emul.gpa,
429 &vmexit->u.inst_emul.vie);
434 "Failed to emulate instruction at 0x%lx\n",
436 } else if (err == ESRCH) {
437 fprintf(stderr, "Unhandled memory access to 0x%lx\n",
438 vmexit->u.inst_emul.gpa);
441 return (VMEXIT_ABORT);
444 return (VMEXIT_CONTINUE);
447 static pthread_mutex_t resetcpu_mtx = PTHREAD_MUTEX_INITIALIZER;
448 static pthread_cond_t resetcpu_cond = PTHREAD_COND_INITIALIZER;
451 vmexit_suspend(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
453 enum vm_suspend_how how;
455 how = vmexit->u.suspended.how;
457 fbsdrun_deletecpu(ctx, *pvcpu);
460 pthread_mutex_lock(&resetcpu_mtx);
461 pthread_cond_signal(&resetcpu_cond);
462 pthread_mutex_unlock(&resetcpu_mtx);
466 pthread_mutex_lock(&resetcpu_mtx);
467 while (!CPU_EMPTY(&cpumask)) {
468 pthread_cond_wait(&resetcpu_cond, &resetcpu_mtx);
470 pthread_mutex_unlock(&resetcpu_mtx);
473 case VM_SUSPEND_RESET:
475 case VM_SUSPEND_POWEROFF:
477 case VM_SUSPEND_HALT:
480 fprintf(stderr, "vmexit_suspend: invalid reason %d\n", how);
483 return (0); /* NOTREACHED */
486 static vmexit_handler_t handler[VM_EXITCODE_MAX] = {
487 [VM_EXITCODE_INOUT] = vmexit_inout,
488 [VM_EXITCODE_VMX] = vmexit_vmx,
489 [VM_EXITCODE_BOGUS] = vmexit_bogus,
490 [VM_EXITCODE_RDMSR] = vmexit_rdmsr,
491 [VM_EXITCODE_WRMSR] = vmexit_wrmsr,
492 [VM_EXITCODE_MTRAP] = vmexit_mtrap,
493 [VM_EXITCODE_INST_EMUL] = vmexit_inst_emul,
494 [VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap,
495 [VM_EXITCODE_SUSPENDED] = vmexit_suspend
499 vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip)
502 int error, rc, prevcpu;
503 enum vm_exitcode exitcode;
504 enum vm_suspend_how how;
508 CPU_SET(pincpu + vcpu, &mask);
509 error = pthread_setaffinity_np(pthread_self(),
510 sizeof(mask), &mask);
515 error = vm_run(ctx, vcpu, rip, &vmexit[vcpu]);
521 exitcode = vmexit[vcpu].exitcode;
522 if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) {
523 fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n",
528 rc = (*handler[exitcode])(ctx, &vmexit[vcpu], &vcpu);
531 case VMEXIT_CONTINUE:
532 rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length;
535 rip = vmexit[vcpu].rip;
538 case VMEXIT_POWEROFF:
539 if (rc == VMEXIT_RESET)
540 how = VM_SUSPEND_RESET;
542 how = VM_SUSPEND_POWEROFF;
543 error = vm_suspend(ctx, how);
544 assert(error == 0 || errno == EALREADY);
545 rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length;
551 fprintf(stderr, "vm_run error %d, errno %d\n", error, errno);
555 num_vcpus_allowed(struct vmctx *ctx)
559 error = vm_get_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, &tmp);
562 * The guest is allowed to spinup more than one processor only if the
563 * UNRESTRICTED_GUEST capability is available.
572 fbsdrun_set_capabilities(struct vmctx *ctx, int cpu)
576 if (fbsdrun_vmexit_on_hlt()) {
577 err = vm_get_capability(ctx, cpu, VM_CAP_HALT_EXIT, &tmp);
579 fprintf(stderr, "VM exit on HLT not supported\n");
582 vm_set_capability(ctx, cpu, VM_CAP_HALT_EXIT, 1);
584 handler[VM_EXITCODE_HLT] = vmexit_hlt;
587 if (fbsdrun_vmexit_on_pause()) {
589 * pause exit support required for this mode
591 err = vm_get_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, &tmp);
594 "SMP mux requested, no pause support\n");
597 vm_set_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, 1);
599 handler[VM_EXITCODE_PAUSE] = vmexit_pause;
603 err = vm_set_x2apic_state(ctx, cpu, X2APIC_ENABLED);
605 err = vm_set_x2apic_state(ctx, cpu, X2APIC_DISABLED);
608 fprintf(stderr, "Unable to set x2apic state (%d)\n", err);
612 vm_set_capability(ctx, cpu, VM_CAP_ENABLE_INVPCID, 1);
616 main(int argc, char *argv[])
618 int c, error, gdb_port, err, bvmcons;
625 progname = basename(argv[0]);
630 while ((c = getopt(argc, argv, "abehwxAHIPWp:g:c:s:m:l:U:")) != -1) {
642 pincpu = atoi(optarg);
645 guest_ncpus = atoi(optarg);
648 gdb_port = atoi(optarg);
651 if (lpc_device_parse(optarg) != 0) {
652 errx(EX_USAGE, "invalid lpc device "
653 "configuration '%s'", optarg);
657 if (pci_parse_slot(optarg) != 0)
662 error = vm_parse_memsize(optarg, &memsize);
664 errx(EX_USAGE, "invalid memsize '%s'", optarg);
667 guest_vmexit_on_hlt = 1;
671 * The "-I" option was used to add an ioapic to the
674 * An ioapic is now provided unconditionally for each
675 * virtual machine and this option is now deprecated.
679 guest_vmexit_on_pause = 1;
685 guest_uuid_str = optarg;
710 ctx = vm_open(vmname);
716 max_vcpus = num_vcpus_allowed(ctx);
717 if (guest_ncpus > max_vcpus) {
718 fprintf(stderr, "%d vCPUs requested but only %d available\n",
719 guest_ncpus, max_vcpus);
723 fbsdrun_set_capabilities(ctx, BSP);
725 err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
727 fprintf(stderr, "Unable to setup memory (%d)\n", err);
738 * Exit if a device emulation finds an error in it's initilization
740 if (init_pci(ctx) != 0)
744 init_dbgport(gdb_port);
749 error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip);
753 * build the guest tables, MP etc.
755 mptable_build(ctx, guest_ncpus);
757 error = smbios_build(ctx);
761 error = acpi_build(ctx, guest_ncpus);
766 * Change the proc title to include the VM name.
768 setproctitle("%s", vmname);
773 fbsdrun_addcpu(ctx, BSP, BSP, rip);
776 * Head off to the main event dispatch loop