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/segments.h>
46 #include <pthread_np.h>
49 #include <machine/vmm.h>
62 #include "spinup_ap.h"
65 #define GUEST_NIO_PORT 0x488 /* guest upcalls via i/o port */
67 #define VMEXIT_SWITCH 0 /* force vcpu switch in mux mode */
68 #define VMEXIT_CONTINUE 1 /* continue from next instruction */
69 #define VMEXIT_RESTART 2 /* restart current instruction */
70 #define VMEXIT_ABORT 3 /* abort the vm run loop */
71 #define VMEXIT_RESET 4 /* guest machine has reset */
73 #define MB (1024UL * 1024)
74 #define GB (1024UL * MB)
76 typedef int (*vmexit_handler_t)(struct vmctx *, struct vm_exit *, int *vcpu);
82 static int pincpu = -1;
83 static int guest_vmexit_on_hlt, guest_vmexit_on_pause, disable_x2apic;
91 static char *progname;
92 static const int BSP = 0;
96 static void vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip);
98 struct vm_exit vmexit[VM_MAXCPU];
101 uint64_t vmexit_bogus;
102 uint64_t vmexit_bogus_switch;
104 uint64_t vmexit_pause;
105 uint64_t vmexit_mtrap;
106 uint64_t vmexit_inst_emul;
107 uint64_t cpu_switch_rotate;
108 uint64_t cpu_switch_direct;
114 struct vmctx *mt_ctx;
116 } mt_vmm_info[VM_MAXCPU];
123 "Usage: %s [-aehAHIP][-g <gdb port>][-s <pci>][-S <pci>]"
124 "[-c vcpus][-p pincpu][-m mem]"
126 " -a: local apic is in XAPIC mode (default is X2APIC)\n"
127 " -A: create an ACPI table\n"
129 " -c: # cpus (default 1)\n"
130 " -p: pin vcpu 'n' to host cpu 'pincpu + n'\n"
131 " -H: vmexit from the guest on hlt\n"
132 " -I: present an ioapic to the guest\n"
133 " -P: vmexit from the guest on pause\n"
134 " -e: exit on unhandled i/o access\n"
136 " -s: <slot,driver,configinfo> PCI slot config\n"
137 " -S: <slot,driver,configinfo> legacy PCI slot config\n"
138 " -m: memory size in MB\n",
145 paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len)
148 return (vm_map_gpa(ctx, gaddr, len));
152 fbsdrun_disable_x2apic(void)
155 return (disable_x2apic);
159 fbsdrun_vmexit_on_pause(void)
162 return (guest_vmexit_on_pause);
166 fbsdrun_vmexit_on_hlt(void)
169 return (guest_vmexit_on_hlt);
173 fbsdrun_start_thread(void *param)
175 char tname[MAXCOMLEN + 1];
176 struct mt_vmm_info *mtp;
182 snprintf(tname, sizeof(tname), "%s vcpu %d", vmname, vcpu);
183 pthread_set_name_np(mtp->mt_thr, tname);
185 vm_loop(mtp->mt_ctx, vcpu, vmexit[vcpu].rip);
193 fbsdrun_addcpu(struct vmctx *ctx, int vcpu, uint64_t rip)
197 if (cpumask & (1 << vcpu)) {
198 fprintf(stderr, "addcpu: attempting to add existing cpu %d\n",
203 cpumask |= 1 << vcpu;
207 * Set up the vmexit struct to allow execution to start
210 vmexit[vcpu].rip = rip;
211 vmexit[vcpu].inst_length = 0;
213 mt_vmm_info[vcpu].mt_ctx = ctx;
214 mt_vmm_info[vcpu].mt_vcpu = vcpu;
216 error = pthread_create(&mt_vmm_info[vcpu].mt_thr, NULL,
217 fbsdrun_start_thread, &mt_vmm_info[vcpu]);
222 vmexit_catch_reset(void)
225 return (VMEXIT_RESET);
229 vmexit_catch_inout(void)
231 return (VMEXIT_ABORT);
235 vmexit_handle_notify(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu,
240 * put guest-driven debug here
243 return (VMEXIT_CONTINUE);
247 vmexit_inout(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
250 int bytes, port, in, out;
256 port = vme->u.inout.port;
257 bytes = vme->u.inout.bytes;
258 eax = vme->u.inout.eax;
259 in = vme->u.inout.in;
262 /* We don't deal with these */
263 if (vme->u.inout.string || vme->u.inout.rep)
264 return (VMEXIT_ABORT);
266 /* Special case of guest reset */
267 if (out && port == 0x64 && (uint8_t)eax == 0xFE)
268 return (vmexit_catch_reset());
270 /* Extra-special case of host notifications */
271 if (out && port == GUEST_NIO_PORT)
272 return (vmexit_handle_notify(ctx, vme, pvcpu, eax));
274 error = emulate_inout(ctx, vcpu, in, port, bytes, &eax, strictio);
275 if (error == 0 && in)
276 error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX, eax);
279 return (VMEXIT_CONTINUE);
281 fprintf(stderr, "Unhandled %s%c 0x%04x\n",
283 bytes == 1 ? 'b' : (bytes == 2 ? 'w' : 'l'), port);
284 return (vmexit_catch_inout());
289 vmexit_rdmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
291 fprintf(stderr, "vm exit rdmsr 0x%x, cpu %d\n", vme->u.msr.code,
293 return (VMEXIT_ABORT);
297 vmexit_wrmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
300 int retval = VMEXIT_CONTINUE;
302 newcpu = emulate_wrmsr(ctx, *pvcpu, vme->u.msr.code,vme->u.msr.wval);
308 vmexit_spinup_ap(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
311 int retval = VMEXIT_CONTINUE;
313 newcpu = spinup_ap(ctx, *pvcpu,
314 vme->u.spinup_ap.vcpu, vme->u.spinup_ap.rip);
320 vmexit_vmx(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
323 fprintf(stderr, "vm exit[%d]\n", *pvcpu);
324 fprintf(stderr, "\treason\t\tVMX\n");
325 fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip);
326 fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length);
327 fprintf(stderr, "\terror\t\t%d\n", vmexit->u.vmx.error);
328 fprintf(stderr, "\texit_reason\t%u\n", vmexit->u.vmx.exit_reason);
329 fprintf(stderr, "\tqualification\t0x%016lx\n",
330 vmexit->u.vmx.exit_qualification);
332 return (VMEXIT_ABORT);
336 vmexit_bogus(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
339 stats.vmexit_bogus++;
341 return (VMEXIT_RESTART);
345 vmexit_hlt(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
351 * Just continue execution with the next instruction. We use
352 * the HLT VM exit as a way to be friendly with the host
355 return (VMEXIT_CONTINUE);
359 vmexit_pause(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
362 stats.vmexit_pause++;
364 return (VMEXIT_CONTINUE);
368 vmexit_mtrap(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
371 stats.vmexit_mtrap++;
373 return (VMEXIT_RESTART);
377 vmexit_inst_emul(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
380 stats.vmexit_inst_emul++;
382 err = emulate_mem(ctx, *pvcpu, vmexit->u.inst_emul.gpa,
383 &vmexit->u.inst_emul.vie);
388 "Failed to emulate instruction at 0x%lx\n",
390 } else if (err == ESRCH) {
391 fprintf(stderr, "Unhandled memory access to 0x%lx\n",
392 vmexit->u.inst_emul.gpa);
395 return (VMEXIT_ABORT);
398 return (VMEXIT_CONTINUE);
401 static vmexit_handler_t handler[VM_EXITCODE_MAX] = {
402 [VM_EXITCODE_INOUT] = vmexit_inout,
403 [VM_EXITCODE_VMX] = vmexit_vmx,
404 [VM_EXITCODE_BOGUS] = vmexit_bogus,
405 [VM_EXITCODE_RDMSR] = vmexit_rdmsr,
406 [VM_EXITCODE_WRMSR] = vmexit_wrmsr,
407 [VM_EXITCODE_MTRAP] = vmexit_mtrap,
408 [VM_EXITCODE_INST_EMUL] = vmexit_inst_emul,
409 [VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap,
413 vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip)
416 int error, rc, prevcpu;
417 enum vm_exitcode exitcode;
421 CPU_SET(pincpu + vcpu, &mask);
422 error = pthread_setaffinity_np(pthread_self(),
423 sizeof(mask), &mask);
428 error = vm_run(ctx, vcpu, rip, &vmexit[vcpu]);
431 * It is possible that 'vmmctl' or some other process
432 * has transitioned the vcpu to CANNOT_RUN state right
433 * before we tried to transition it to RUNNING.
435 * This is expected to be temporary so just retry.
445 exitcode = vmexit[vcpu].exitcode;
446 if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) {
447 fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n",
452 rc = (*handler[exitcode])(ctx, &vmexit[vcpu], &vcpu);
455 case VMEXIT_CONTINUE:
456 rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length;
459 rip = vmexit[vcpu].rip;
467 fprintf(stderr, "vm_run error %d, errno %d\n", error, errno);
471 num_vcpus_allowed(struct vmctx *ctx)
475 error = vm_get_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, &tmp);
478 * The guest is allowed to spinup more than one processor only if the
479 * UNRESTRICTED_GUEST capability is available.
488 main(int argc, char *argv[])
490 int c, error, gdb_port, tmp, err, ioapic, bvmcons;
497 progname = basename(argv[0]);
503 while ((c = getopt(argc, argv, "abehAHIPp:g:c:s:S:m:")) != -1) {
515 pincpu = atoi(optarg);
518 guest_ncpus = atoi(optarg);
521 gdb_port = atoi(optarg);
524 if (pci_parse_slot(optarg, 0) != 0)
529 if (pci_parse_slot(optarg, 1) != 0)
534 error = vm_parse_memsize(optarg, &memsize);
536 errx(EX_USAGE, "invalid memsize '%s'", optarg);
539 guest_vmexit_on_hlt = 1;
545 guest_vmexit_on_pause = 1;
564 ctx = vm_open(vmname);
570 max_vcpus = num_vcpus_allowed(ctx);
571 if (guest_ncpus > max_vcpus) {
572 fprintf(stderr, "%d vCPUs requested but only %d available\n",
573 guest_ncpus, max_vcpus);
577 if (fbsdrun_vmexit_on_hlt()) {
578 err = vm_get_capability(ctx, BSP, VM_CAP_HALT_EXIT, &tmp);
580 fprintf(stderr, "VM exit on HLT not supported\n");
583 vm_set_capability(ctx, BSP, 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, BSP, VM_CAP_PAUSE_EXIT, &tmp);
594 "SMP mux requested, no pause support\n");
597 vm_set_capability(ctx, BSP, VM_CAP_PAUSE_EXIT, 1);
598 handler[VM_EXITCODE_PAUSE] = vmexit_pause;
601 if (fbsdrun_disable_x2apic())
602 err = vm_set_x2apic_state(ctx, BSP, X2APIC_DISABLED);
604 err = vm_set_x2apic_state(ctx, BSP, X2APIC_ENABLED);
607 fprintf(stderr, "Unable to set x2apic state (%d)\n", err);
611 err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
613 fprintf(stderr, "Unable to setup memory (%d)\n", err);
623 * Exit if a device emulation finds an error in it's initilization
625 if (init_pci(ctx) != 0)
632 init_dbgport(gdb_port);
637 error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip);
641 * build the guest tables, MP etc.
643 mptable_build(ctx, guest_ncpus, ioapic);
646 error = acpi_build(ctx, guest_ncpus, ioapic);
653 fbsdrun_addcpu(ctx, BSP, rip);
656 * Head off to the main event dispatch loop