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>
48 #include <machine/vmm.h>
61 #include "spinup_ap.h"
63 #define DEFAULT_GUEST_HZ 100
64 #define DEFAULT_GUEST_TSLICE 200
66 #define GUEST_NIO_PORT 0x488 /* guest upcalls via i/o port */
68 #define VMEXIT_SWITCH 0 /* force vcpu switch in mux mode */
69 #define VMEXIT_CONTINUE 1 /* continue from next instruction */
70 #define VMEXIT_RESTART 2 /* restart current instruction */
71 #define VMEXIT_ABORT 3 /* abort the vm run loop */
72 #define VMEXIT_RESET 4 /* guest machine has reset */
74 #define MB (1024UL * 1024)
75 #define GB (1024UL * MB)
77 typedef int (*vmexit_handler_t)(struct vmctx *, struct vm_exit *, int *vcpu);
79 int guest_tslice = DEFAULT_GUEST_TSLICE;
80 int guest_hz = DEFAULT_GUEST_HZ;
85 static int pincpu = -1;
86 static int guest_vcpu_mux;
87 static int guest_vmexit_on_hlt, guest_vmexit_on_pause, disable_x2apic;
95 static char *progname;
96 static const int BSP = 0;
100 static void vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip);
102 struct vm_exit vmexit[VM_MAXCPU];
105 uint64_t vmexit_bogus;
106 uint64_t vmexit_bogus_switch;
108 uint64_t vmexit_pause;
109 uint64_t vmexit_mtrap;
110 uint64_t vmexit_paging;
111 uint64_t cpu_switch_rotate;
112 uint64_t cpu_switch_direct;
118 struct vmctx *mt_ctx;
120 } mt_vmm_info[VM_MAXCPU];
127 "Usage: %s [-aehABHIP][-g <gdb port>][-z <hz>][-s <pci>]"
128 "[-S <pci>][-p pincpu][-n <pci>][-m lowmem][-M highmem]"
130 " -a: local apic is in XAPIC mode (default is X2APIC)\n"
131 " -A: create an ACPI table\n"
132 " -g: gdb port (default is %d and 0 means don't open)\n"
133 " -c: # cpus (default 1)\n"
134 " -p: pin vcpu 'n' to host cpu 'pincpu + n'\n"
135 " -B: inject breakpoint exception on vm entry\n"
136 " -H: vmexit from the guest on hlt\n"
137 " -I: present an ioapic to the guest\n"
138 " -P: vmexit from the guest on pause\n"
139 " -e: exit on unhandled i/o access\n"
141 " -z: guest hz (default is %d)\n"
142 " -s: <slot,driver,configinfo> PCI slot config\n"
143 " -S: <slot,driver,configinfo> legacy PCI slot config\n"
144 " -m: memory size in MB\n"
145 " -x: mux vcpus to 1 hcpu\n"
146 " -t: mux vcpu timeslice hz (default %d)\n",
147 progname, DEFAULT_GDB_PORT, DEFAULT_GUEST_HZ,
148 DEFAULT_GUEST_TSLICE);
153 paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len)
156 return (vm_map_gpa(ctx, gaddr, len));
160 fbsdrun_disable_x2apic(void)
163 return (disable_x2apic);
167 fbsdrun_vmexit_on_pause(void)
170 return (guest_vmexit_on_pause);
174 fbsdrun_vmexit_on_hlt(void)
177 return (guest_vmexit_on_hlt);
184 return (guest_vcpu_mux);
188 fbsdrun_start_thread(void *param)
190 char tname[MAXCOMLEN + 1];
191 struct mt_vmm_info *mtp;
197 snprintf(tname, sizeof(tname), "%s vcpu %d", vmname, vcpu);
198 pthread_set_name_np(mtp->mt_thr, tname);
200 vm_loop(mtp->mt_ctx, vcpu, vmexit[vcpu].rip);
208 fbsdrun_addcpu(struct vmctx *ctx, int vcpu, uint64_t rip)
212 if (cpumask & (1 << vcpu)) {
213 fprintf(stderr, "addcpu: attempting to add existing cpu %d\n",
218 cpumask |= 1 << vcpu;
222 * Set up the vmexit struct to allow execution to start
225 vmexit[vcpu].rip = rip;
226 vmexit[vcpu].inst_length = 0;
228 if (vcpu == BSP || !guest_vcpu_mux){
229 mt_vmm_info[vcpu].mt_ctx = ctx;
230 mt_vmm_info[vcpu].mt_vcpu = vcpu;
232 error = pthread_create(&mt_vmm_info[vcpu].mt_thr, NULL,
233 fbsdrun_start_thread, &mt_vmm_info[vcpu]);
239 fbsdrun_get_next_cpu(int curcpu)
243 * Get the next available CPU. Assumes they arrive
244 * in ascending order with no gaps.
246 return ((curcpu + 1) % foundcpus);
250 vmexit_catch_reset(void)
253 return (VMEXIT_RESET);
257 vmexit_catch_inout(void)
259 return (VMEXIT_ABORT);
263 vmexit_handle_notify(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu,
266 #if PG_DEBUG /* put all types of debug here */
269 } else if (eax == 1) {
274 vm_set_capability(ctx, *pvcpu, VM_CAP_MTRAP_EXIT, 1);
278 return (VMEXIT_CONTINUE);
282 vmexit_inout(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
285 int bytes, port, in, out;
291 port = vme->u.inout.port;
292 bytes = vme->u.inout.bytes;
293 eax = vme->u.inout.eax;
294 in = vme->u.inout.in;
297 /* We don't deal with these */
298 if (vme->u.inout.string || vme->u.inout.rep)
299 return (VMEXIT_ABORT);
301 /* Special case of guest reset */
302 if (out && port == 0x64 && (uint8_t)eax == 0xFE)
303 return (vmexit_catch_reset());
305 /* Extra-special case of host notifications */
306 if (out && port == GUEST_NIO_PORT)
307 return (vmexit_handle_notify(ctx, vme, pvcpu, eax));
309 error = emulate_inout(ctx, vcpu, in, port, bytes, &eax, strictio);
310 if (error == 0 && in)
311 error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX, eax);
314 return (VMEXIT_CONTINUE);
316 fprintf(stderr, "Unhandled %s%c 0x%04x\n",
318 bytes == 1 ? 'b' : (bytes == 2 ? 'w' : 'l'), port);
319 return (vmexit_catch_inout());
324 vmexit_rdmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
326 fprintf(stderr, "vm exit rdmsr 0x%x, cpu %d\n", vme->u.msr.code,
328 return (VMEXIT_ABORT);
332 vmexit_wrmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
335 int retval = VMEXIT_CONTINUE;
337 newcpu = emulate_wrmsr(ctx, *pvcpu, vme->u.msr.code,vme->u.msr.wval);
339 if (guest_vcpu_mux && *pvcpu != newcpu) {
340 retval = VMEXIT_SWITCH;
348 vmexit_spinup_ap(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
351 int retval = VMEXIT_CONTINUE;
353 newcpu = spinup_ap(ctx, *pvcpu,
354 vme->u.spinup_ap.vcpu, vme->u.spinup_ap.rip);
356 if (guest_vcpu_mux && *pvcpu != newcpu) {
357 retval = VMEXIT_SWITCH;
365 vmexit_vmx(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
368 fprintf(stderr, "vm exit[%d]\n", *pvcpu);
369 fprintf(stderr, "\treason\t\tVMX\n");
370 fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip);
371 fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length);
372 fprintf(stderr, "\terror\t\t%d\n", vmexit->u.vmx.error);
373 fprintf(stderr, "\texit_reason\t%u\n", vmexit->u.vmx.exit_reason);
374 fprintf(stderr, "\tqualification\t0x%016lx\n",
375 vmexit->u.vmx.exit_qualification);
377 return (VMEXIT_ABORT);
380 static int bogus_noswitch = 1;
383 vmexit_bogus(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
385 stats.vmexit_bogus++;
387 if (!guest_vcpu_mux || guest_ncpus == 1 || bogus_noswitch) {
388 return (VMEXIT_RESTART);
390 stats.vmexit_bogus_switch++;
391 vmexit->inst_length = 0;
393 return (VMEXIT_SWITCH);
398 vmexit_hlt(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
401 if (fbsdrun_muxed()) {
403 return (VMEXIT_SWITCH);
406 * Just continue execution with the next instruction. We use
407 * the HLT VM exit as a way to be friendly with the host
410 return (VMEXIT_CONTINUE);
414 static int pause_noswitch;
417 vmexit_pause(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
419 stats.vmexit_pause++;
421 if (fbsdrun_muxed() && !pause_noswitch) {
423 return (VMEXIT_SWITCH);
425 return (VMEXIT_CONTINUE);
430 vmexit_mtrap(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
432 stats.vmexit_mtrap++;
434 return (VMEXIT_RESTART);
438 vmexit_paging(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
441 stats.vmexit_paging++;
443 err = emulate_mem(ctx, *pvcpu, vmexit->u.paging.gpa,
444 &vmexit->u.paging.vie);
449 "Failed to emulate instruction at 0x%lx\n",
451 } else if (err == ESRCH) {
452 fprintf(stderr, "Unhandled memory access to 0x%lx\n",
453 vmexit->u.paging.gpa);
456 return (VMEXIT_ABORT);
459 return (VMEXIT_CONTINUE);
469 setup_timeslice(void)
472 struct itimerval itv;
476 * Setup a realtime timer to generate a SIGALRM at a
477 * frequency of 'guest_tslice' ticks per second.
479 sigemptyset(&sa.sa_mask);
481 sa.sa_handler = sigalrm;
483 error = sigaction(SIGALRM, &sa, NULL);
486 itv.it_interval.tv_sec = 0;
487 itv.it_interval.tv_usec = 1000000 / guest_tslice;
488 itv.it_value.tv_sec = 0;
489 itv.it_value.tv_usec = 1000000 / guest_tslice;
491 error = setitimer(ITIMER_REAL, &itv, NULL);
495 static vmexit_handler_t handler[VM_EXITCODE_MAX] = {
496 [VM_EXITCODE_INOUT] = vmexit_inout,
497 [VM_EXITCODE_VMX] = vmexit_vmx,
498 [VM_EXITCODE_BOGUS] = vmexit_bogus,
499 [VM_EXITCODE_RDMSR] = vmexit_rdmsr,
500 [VM_EXITCODE_WRMSR] = vmexit_wrmsr,
501 [VM_EXITCODE_MTRAP] = vmexit_mtrap,
502 [VM_EXITCODE_PAGING] = vmexit_paging,
503 [VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap,
507 vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip)
510 int error, rc, prevcpu;
517 CPU_SET(pincpu + vcpu, &mask);
518 error = pthread_setaffinity_np(pthread_self(),
519 sizeof(mask), &mask);
524 error = vm_run(ctx, vcpu, rip, &vmexit[vcpu]);
527 * It is possible that 'vmmctl' or some other process
528 * has transitioned the vcpu to CANNOT_RUN state right
529 * before we tried to transition it to RUNNING.
531 * This is expected to be temporary so just retry.
540 rc = (*handler[vmexit[vcpu].exitcode])(ctx, &vmexit[vcpu],
544 assert(guest_vcpu_mux);
546 stats.cpu_switch_rotate++;
547 vcpu = fbsdrun_get_next_cpu(prevcpu);
549 stats.cpu_switch_direct++;
552 case VMEXIT_CONTINUE:
553 rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length;
556 rip = vmexit[vcpu].rip;
564 fprintf(stderr, "vm_run error %d, errno %d\n", error, errno);
568 num_vcpus_allowed(struct vmctx *ctx)
572 error = vm_get_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, &tmp);
575 * The guest is allowed to spinup more than one processor only if the
576 * UNRESTRICTED_GUEST capability is available.
585 main(int argc, char *argv[])
587 int c, error, gdb_port, inject_bkpt, tmp, err, ioapic, bvmcons;
595 progname = basename(argv[0]);
596 gdb_port = DEFAULT_GDB_PORT;
601 while ((c = getopt(argc, argv, "abehABHIPxp:g:c:z:s:S:n:m:")) != -1) {
619 pincpu = atoi(optarg);
622 guest_ncpus = atoi(optarg);
625 gdb_port = atoi(optarg);
628 guest_hz = atoi(optarg);
631 guest_tslice = atoi(optarg);
634 if (pci_parse_slot(optarg, 0) != 0)
639 if (pci_parse_slot(optarg, 1) != 0)
644 memsize = strtoul(optarg, NULL, 0) * MB;
647 guest_vmexit_on_hlt = 1;
653 guest_vmexit_on_pause = 1;
670 /* No need to mux if guest is uni-processor */
671 if (guest_ncpus <= 1)
674 /* vmexit on hlt if guest is muxed */
675 if (guest_vcpu_mux) {
676 guest_vmexit_on_hlt = 1;
677 guest_vmexit_on_pause = 1;
682 ctx = vm_open(vmname);
688 max_vcpus = num_vcpus_allowed(ctx);
689 if (guest_ncpus > max_vcpus) {
690 fprintf(stderr, "%d vCPUs requested but only %d available\n",
691 guest_ncpus, max_vcpus);
695 if (fbsdrun_vmexit_on_hlt()) {
696 err = vm_get_capability(ctx, BSP, VM_CAP_HALT_EXIT, &tmp);
698 fprintf(stderr, "VM exit on HLT not supported\n");
701 vm_set_capability(ctx, BSP, VM_CAP_HALT_EXIT, 1);
702 handler[VM_EXITCODE_HLT] = vmexit_hlt;
705 if (fbsdrun_vmexit_on_pause()) {
707 * pause exit support required for this mode
709 err = vm_get_capability(ctx, BSP, VM_CAP_PAUSE_EXIT, &tmp);
712 "SMP mux requested, no pause support\n");
715 vm_set_capability(ctx, BSP, VM_CAP_PAUSE_EXIT, 1);
716 handler[VM_EXITCODE_PAUSE] = vmexit_pause;
719 if (fbsdrun_disable_x2apic())
720 err = vm_set_x2apic_state(ctx, BSP, X2APIC_DISABLED);
722 err = vm_set_x2apic_state(ctx, BSP, X2APIC_ENABLED);
725 fprintf(stderr, "Unable to set x2apic state (%d)\n", err);
729 err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
731 fprintf(stderr, "Unable to setup memory (%d)\n", err);
742 init_dbgport(gdb_port);
747 error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip);
751 error = vm_inject_event(ctx, BSP, VM_HW_EXCEPTION, IDT_BP);
756 * build the guest tables, MP etc.
758 mptable_build(ctx, guest_ncpus, ioapic);
761 error = acpi_build(ctx, guest_ncpus, ioapic);
768 fbsdrun_addcpu(ctx, BSP, rip);
771 * Head off to the main event dispatch loop