]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.sbin/bhyve/bhyverun.c
MFC r270326
[FreeBSD/stable/10.git] / usr.sbin / bhyve / bhyverun.c
1 /*-
2  * Copyright (c) 2011 NetApp, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/types.h>
33 #include <sys/mman.h>
34 #include <sys/time.h>
35
36 #include <machine/atomic.h>
37 #include <machine/segments.h>
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <err.h>
43 #include <libgen.h>
44 #include <unistd.h>
45 #include <assert.h>
46 #include <errno.h>
47 #include <pthread.h>
48 #include <pthread_np.h>
49 #include <sysexits.h>
50
51 #include <machine/vmm.h>
52 #include <vmmapi.h>
53
54 #include "bhyverun.h"
55 #include "acpi.h"
56 #include "inout.h"
57 #include "dbgport.h"
58 #include "ioapic.h"
59 #include "mem.h"
60 #include "mevent.h"
61 #include "mptbl.h"
62 #include "pci_emul.h"
63 #include "pci_irq.h"
64 #include "pci_lpc.h"
65 #include "smbiostbl.h"
66 #include "xmsr.h"
67 #include "spinup_ap.h"
68 #include "rtc.h"
69
70 #define GUEST_NIO_PORT          0x488   /* guest upcalls via i/o port */
71
72 #define MB              (1024UL * 1024)
73 #define GB              (1024UL * MB)
74
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);
77
78 char *vmname;
79
80 int guest_ncpus;
81 char *guest_uuid_str;
82
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 */
86
87 static int strictio;
88 static int strictmsr = 1;
89
90 static int acpi;
91
92 static char *progname;
93 static const int BSP = 0;
94
95 static cpuset_t cpumask;
96
97 static void vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip);
98
99 static struct vm_exit vmexit[VM_MAXCPU];
100
101 struct bhyvestats {
102         uint64_t        vmexit_bogus;
103         uint64_t        vmexit_bogus_switch;
104         uint64_t        vmexit_hlt;
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;
110 } stats;
111
112 struct mt_vmm_info {
113         pthread_t       mt_thr;
114         struct vmctx    *mt_ctx;
115         int             mt_vcpu;        
116 } mt_vmm_info[VM_MAXCPU];
117
118 static cpuset_t *vcpumap[VM_MAXCPU] = { NULL };
119
120 static void
121 usage(int code)
122 {
123
124         fprintf(stderr,
125                 "Usage: %s [-abehwxACHPWY] [-c vcpus] [-g <gdb port>] [-l <lpc>]\n"
126                 "       %*s [-m mem] [-p vcpu:hostcpu] [-s <pci>] [-U uuid] <vm>\n"
127                 "       -a: local apic is in xAPIC mode (deprecated)\n"
128                 "       -A: create ACPI tables\n"
129                 "       -c: # cpus (default 1)\n"
130                 "       -C: include guest memory in core file\n"
131                 "       -e: exit on unhandled I/O access\n"
132                 "       -g: gdb port\n"
133                 "       -h: help\n"
134                 "       -H: vmexit from the guest on hlt\n"
135                 "       -l: LPC device configuration\n"
136                 "       -m: memory size in MB\n"
137                 "       -p: pin 'vcpu' to 'hostcpu'\n"
138                 "       -P: vmexit from the guest on pause\n"
139                 "       -s: <slot,driver,configinfo> PCI slot config\n"
140                 "       -U: uuid\n"
141                 "       -w: ignore unimplemented MSRs\n"
142                 "       -W: force virtio to use single-vector MSI\n"
143                 "       -x: local apic is in x2APIC mode\n"
144                 "       -Y: disable MPtable generation\n",
145                 progname, (int)strlen(progname), "");
146
147         exit(code);
148 }
149
150 static int
151 pincpu_parse(const char *opt)
152 {
153         int vcpu, pcpu;
154
155         if (sscanf(opt, "%d:%d", &vcpu, &pcpu) != 2) {
156                 fprintf(stderr, "invalid format: %s\n", opt);
157                 return (-1);
158         }
159
160         if (vcpu < 0 || vcpu >= VM_MAXCPU) {
161                 fprintf(stderr, "vcpu '%d' outside valid range from 0 to %d\n",
162                     vcpu, VM_MAXCPU - 1);
163                 return (-1);
164         }
165
166         if (pcpu < 0 || pcpu >= CPU_SETSIZE) {
167                 fprintf(stderr, "hostcpu '%d' outside valid range from "
168                     "0 to %d\n", pcpu, CPU_SETSIZE - 1);
169                 return (-1);
170         }
171
172         if (vcpumap[vcpu] == NULL) {
173                 if ((vcpumap[vcpu] = malloc(sizeof(cpuset_t))) == NULL) {
174                         perror("malloc");
175                         return (-1);
176                 }
177                 CPU_ZERO(vcpumap[vcpu]);
178         }
179         CPU_SET(pcpu, vcpumap[vcpu]);
180         return (0);
181 }
182
183 void
184 vm_inject_fault(void *arg, int vcpu, int vector, int errcode_valid,
185     int errcode)
186 {
187         struct vmctx *ctx;
188         int error;
189
190         ctx = arg;
191         if (errcode_valid)
192                 error = vm_inject_exception2(ctx, vcpu, vector, errcode);
193         else
194                 error = vm_inject_exception(ctx, vcpu, vector);
195         assert(error == 0);
196
197         /*
198          * Set the instruction length to 0 to ensure that the instruction is
199          * restarted when the fault handler returns.
200          */
201         vmexit[vcpu].inst_length = 0;
202 }
203
204 void *
205 paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len)
206 {
207
208         return (vm_map_gpa(ctx, gaddr, len));
209 }
210
211 int
212 fbsdrun_vmexit_on_pause(void)
213 {
214
215         return (guest_vmexit_on_pause);
216 }
217
218 int
219 fbsdrun_vmexit_on_hlt(void)
220 {
221
222         return (guest_vmexit_on_hlt);
223 }
224
225 int
226 fbsdrun_virtio_msix(void)
227 {
228
229         return (virtio_msix);
230 }
231
232 static void *
233 fbsdrun_start_thread(void *param)
234 {
235         char tname[MAXCOMLEN + 1];
236         struct mt_vmm_info *mtp;
237         int vcpu;
238
239         mtp = param;
240         vcpu = mtp->mt_vcpu;
241
242         snprintf(tname, sizeof(tname), "vcpu %d", vcpu);
243         pthread_set_name_np(mtp->mt_thr, tname);
244
245         vm_loop(mtp->mt_ctx, vcpu, vmexit[vcpu].rip);
246
247         /* not reached */
248         exit(1);
249         return (NULL);
250 }
251
252 void
253 fbsdrun_addcpu(struct vmctx *ctx, int fromcpu, int newcpu, uint64_t rip)
254 {
255         int error;
256
257         assert(fromcpu == BSP);
258
259         /*
260          * The 'newcpu' must be activated in the context of 'fromcpu'. If
261          * vm_activate_cpu() is delayed until newcpu's pthread starts running
262          * then vmm.ko is out-of-sync with bhyve and this can create a race
263          * with vm_suspend().
264          */
265         error = vm_activate_cpu(ctx, newcpu);
266         assert(error == 0);
267
268         CPU_SET_ATOMIC(newcpu, &cpumask);
269
270         /*
271          * Set up the vmexit struct to allow execution to start
272          * at the given RIP
273          */
274         vmexit[newcpu].rip = rip;
275         vmexit[newcpu].inst_length = 0;
276
277         mt_vmm_info[newcpu].mt_ctx = ctx;
278         mt_vmm_info[newcpu].mt_vcpu = newcpu;
279
280         error = pthread_create(&mt_vmm_info[newcpu].mt_thr, NULL,
281             fbsdrun_start_thread, &mt_vmm_info[newcpu]);
282         assert(error == 0);
283 }
284
285 static int
286 fbsdrun_deletecpu(struct vmctx *ctx, int vcpu)
287 {
288
289         if (!CPU_ISSET(vcpu, &cpumask)) {
290                 fprintf(stderr, "Attempting to delete unknown cpu %d\n", vcpu);
291                 exit(1);
292         }
293
294         CPU_CLR_ATOMIC(vcpu, &cpumask);
295         return (CPU_EMPTY(&cpumask));
296 }
297
298 static int
299 vmexit_handle_notify(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu,
300                      uint32_t eax)
301 {
302 #if BHYVE_DEBUG
303         /*
304          * put guest-driven debug here
305          */
306 #endif
307         return (VMEXIT_CONTINUE);
308 }
309
310 static int
311 vmexit_inout(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
312 {
313         int error;
314         int bytes, port, in, out, string;
315         int vcpu;
316
317         vcpu = *pvcpu;
318
319         port = vme->u.inout.port;
320         bytes = vme->u.inout.bytes;
321         string = vme->u.inout.string;
322         in = vme->u.inout.in;
323         out = !in;
324
325         /* Extra-special case of host notifications */
326         if (out && port == GUEST_NIO_PORT) {
327                 error = vmexit_handle_notify(ctx, vme, pvcpu, vme->u.inout.eax);
328                 return (error);
329         }
330
331         error = emulate_inout(ctx, vcpu, vme, strictio);
332         if (!error && in && !string) {
333                 error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX,
334                     vme->u.inout.eax);
335                 assert(error == 0);
336         }
337
338         if (error) {
339                 fprintf(stderr, "Unhandled %s%c 0x%04x\n", in ? "in" : "out",
340                     bytes == 1 ? 'b' : (bytes == 2 ? 'w' : 'l'), port);
341                 return (VMEXIT_ABORT);
342         } else {
343                 return (VMEXIT_CONTINUE);
344         }
345 }
346
347 static int
348 vmexit_rdmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
349 {
350         uint64_t val;
351         uint32_t eax, edx;
352         int error;
353
354         val = 0;
355         error = emulate_rdmsr(ctx, *pvcpu, vme->u.msr.code, &val);
356         if (error != 0) {
357                 fprintf(stderr, "rdmsr to register %#x on vcpu %d\n",
358                     vme->u.msr.code, *pvcpu);
359                 if (strictmsr) {
360                         vm_inject_gp(ctx, *pvcpu);
361                         return (VMEXIT_RESTART);
362                 }
363         }
364
365         eax = val;
366         error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RAX, eax);
367         assert(error == 0);
368
369         edx = val >> 32;
370         error = vm_set_register(ctx, *pvcpu, VM_REG_GUEST_RDX, edx);
371         assert(error == 0);
372
373         return (VMEXIT_CONTINUE);
374 }
375
376 static int
377 vmexit_wrmsr(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
378 {
379         int error;
380
381         error = emulate_wrmsr(ctx, *pvcpu, vme->u.msr.code, vme->u.msr.wval);
382         if (error != 0) {
383                 fprintf(stderr, "wrmsr to register %#x(%#lx) on vcpu %d\n",
384                     vme->u.msr.code, vme->u.msr.wval, *pvcpu);
385                 if (strictmsr) {
386                         vm_inject_gp(ctx, *pvcpu);
387                         return (VMEXIT_RESTART);
388                 }
389         }
390         return (VMEXIT_CONTINUE);
391 }
392
393 static int
394 vmexit_spinup_ap(struct vmctx *ctx, struct vm_exit *vme, int *pvcpu)
395 {
396         int newcpu;
397         int retval = VMEXIT_CONTINUE;
398
399         newcpu = spinup_ap(ctx, *pvcpu,
400                            vme->u.spinup_ap.vcpu, vme->u.spinup_ap.rip);
401
402         return (retval);
403 }
404
405 #define DEBUG_EPT_MISCONFIG
406 #ifdef DEBUG_EPT_MISCONFIG
407 #define EXIT_REASON_EPT_MISCONFIG       49
408 #define VMCS_GUEST_PHYSICAL_ADDRESS     0x00002400
409 #define VMCS_IDENT(x)                   ((x) | 0x80000000)
410
411 static uint64_t ept_misconfig_gpa, ept_misconfig_pte[4];
412 static int ept_misconfig_ptenum;
413 #endif
414
415 static int
416 vmexit_vmx(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
417 {
418
419         fprintf(stderr, "vm exit[%d]\n", *pvcpu);
420         fprintf(stderr, "\treason\t\tVMX\n");
421         fprintf(stderr, "\trip\t\t0x%016lx\n", vmexit->rip);
422         fprintf(stderr, "\tinst_length\t%d\n", vmexit->inst_length);
423         fprintf(stderr, "\tstatus\t\t%d\n", vmexit->u.vmx.status);
424         fprintf(stderr, "\texit_reason\t%u\n", vmexit->u.vmx.exit_reason);
425         fprintf(stderr, "\tqualification\t0x%016lx\n",
426             vmexit->u.vmx.exit_qualification);
427         fprintf(stderr, "\tinst_type\t\t%d\n", vmexit->u.vmx.inst_type);
428         fprintf(stderr, "\tinst_error\t\t%d\n", vmexit->u.vmx.inst_error);
429 #ifdef DEBUG_EPT_MISCONFIG
430         if (vmexit->u.vmx.exit_reason == EXIT_REASON_EPT_MISCONFIG) {
431                 vm_get_register(ctx, *pvcpu,
432                     VMCS_IDENT(VMCS_GUEST_PHYSICAL_ADDRESS),
433                     &ept_misconfig_gpa);
434                 vm_get_gpa_pmap(ctx, ept_misconfig_gpa, ept_misconfig_pte,
435                     &ept_misconfig_ptenum);
436                 fprintf(stderr, "\tEPT misconfiguration:\n");
437                 fprintf(stderr, "\t\tGPA: %#lx\n", ept_misconfig_gpa);
438                 fprintf(stderr, "\t\tPTE(%d): %#lx %#lx %#lx %#lx\n",
439                     ept_misconfig_ptenum, ept_misconfig_pte[0],
440                     ept_misconfig_pte[1], ept_misconfig_pte[2],
441                     ept_misconfig_pte[3]);
442         }
443 #endif  /* DEBUG_EPT_MISCONFIG */
444         return (VMEXIT_ABORT);
445 }
446
447 static int
448 vmexit_bogus(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
449 {
450
451         stats.vmexit_bogus++;
452
453         return (VMEXIT_RESTART);
454 }
455
456 static int
457 vmexit_hlt(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
458 {
459
460         stats.vmexit_hlt++;
461
462         /*
463          * Just continue execution with the next instruction. We use
464          * the HLT VM exit as a way to be friendly with the host
465          * scheduler.
466          */
467         return (VMEXIT_CONTINUE);
468 }
469
470 static int
471 vmexit_pause(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
472 {
473
474         stats.vmexit_pause++;
475
476         return (VMEXIT_CONTINUE);
477 }
478
479 static int
480 vmexit_mtrap(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
481 {
482
483         stats.vmexit_mtrap++;
484
485         return (VMEXIT_RESTART);
486 }
487
488 static int
489 vmexit_inst_emul(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
490 {
491         int err;
492         stats.vmexit_inst_emul++;
493
494         err = emulate_mem(ctx, *pvcpu, vmexit->u.inst_emul.gpa,
495             &vmexit->u.inst_emul.vie, &vmexit->u.inst_emul.paging);
496
497         if (err) {
498                 if (err == EINVAL) {
499                         fprintf(stderr,
500                             "Failed to emulate instruction at 0x%lx\n", 
501                             vmexit->rip);
502                 } else if (err == ESRCH) {
503                         fprintf(stderr, "Unhandled memory access to 0x%lx\n",
504                             vmexit->u.inst_emul.gpa);
505                 }
506
507                 return (VMEXIT_ABORT);
508         }
509
510         return (VMEXIT_CONTINUE);
511 }
512
513 static pthread_mutex_t resetcpu_mtx = PTHREAD_MUTEX_INITIALIZER;
514 static pthread_cond_t resetcpu_cond = PTHREAD_COND_INITIALIZER;
515
516 static int
517 vmexit_suspend(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
518 {
519         enum vm_suspend_how how;
520
521         how = vmexit->u.suspended.how;
522
523         fbsdrun_deletecpu(ctx, *pvcpu);
524
525         if (*pvcpu != BSP) {
526                 pthread_mutex_lock(&resetcpu_mtx);
527                 pthread_cond_signal(&resetcpu_cond);
528                 pthread_mutex_unlock(&resetcpu_mtx);
529                 pthread_exit(NULL);
530         }
531
532         pthread_mutex_lock(&resetcpu_mtx);
533         while (!CPU_EMPTY(&cpumask)) {
534                 pthread_cond_wait(&resetcpu_cond, &resetcpu_mtx);
535         }
536         pthread_mutex_unlock(&resetcpu_mtx);
537
538         switch (how) {
539         case VM_SUSPEND_RESET:
540                 exit(0);
541         case VM_SUSPEND_POWEROFF:
542                 exit(1);
543         case VM_SUSPEND_HALT:
544                 exit(2);
545         case VM_SUSPEND_TRIPLEFAULT:
546                 exit(3);
547         default:
548                 fprintf(stderr, "vmexit_suspend: invalid reason %d\n", how);
549                 exit(100);
550         }
551         return (0);     /* NOTREACHED */
552 }
553
554 static vmexit_handler_t handler[VM_EXITCODE_MAX] = {
555         [VM_EXITCODE_INOUT]  = vmexit_inout,
556         [VM_EXITCODE_INOUT_STR]  = vmexit_inout,
557         [VM_EXITCODE_VMX]    = vmexit_vmx,
558         [VM_EXITCODE_BOGUS]  = vmexit_bogus,
559         [VM_EXITCODE_RDMSR]  = vmexit_rdmsr,
560         [VM_EXITCODE_WRMSR]  = vmexit_wrmsr,
561         [VM_EXITCODE_MTRAP]  = vmexit_mtrap,
562         [VM_EXITCODE_INST_EMUL] = vmexit_inst_emul,
563         [VM_EXITCODE_SPINUP_AP] = vmexit_spinup_ap,
564         [VM_EXITCODE_SUSPENDED] = vmexit_suspend,
565         [VM_EXITCODE_TASK_SWITCH] = vmexit_task_switch,
566 };
567
568 static void
569 vm_loop(struct vmctx *ctx, int vcpu, uint64_t rip)
570 {
571         int error, rc, prevcpu;
572         enum vm_exitcode exitcode;
573         cpuset_t active_cpus;
574
575         if (vcpumap[vcpu] != NULL) {
576                 error = pthread_setaffinity_np(pthread_self(),
577                     sizeof(cpuset_t), vcpumap[vcpu]);
578                 assert(error == 0);
579         }
580
581         error = vm_active_cpus(ctx, &active_cpus);
582         assert(CPU_ISSET(vcpu, &active_cpus));
583
584         while (1) {
585                 error = vm_run(ctx, vcpu, rip, &vmexit[vcpu]);
586                 if (error != 0)
587                         break;
588
589                 prevcpu = vcpu;
590
591                 exitcode = vmexit[vcpu].exitcode;
592                 if (exitcode >= VM_EXITCODE_MAX || handler[exitcode] == NULL) {
593                         fprintf(stderr, "vm_loop: unexpected exitcode 0x%x\n",
594                             exitcode);
595                         exit(1);
596                 }
597
598                 rc = (*handler[exitcode])(ctx, &vmexit[vcpu], &vcpu);
599
600                 switch (rc) {
601                 case VMEXIT_CONTINUE:
602                         rip = vmexit[vcpu].rip + vmexit[vcpu].inst_length;
603                         break;
604                 case VMEXIT_RESTART:
605                         rip = vmexit[vcpu].rip;
606                         break;
607                 case VMEXIT_ABORT:
608                         abort();
609                 default:
610                         exit(1);
611                 }
612         }
613         fprintf(stderr, "vm_run error %d, errno %d\n", error, errno);
614 }
615
616 static int
617 num_vcpus_allowed(struct vmctx *ctx)
618 {
619         int tmp, error;
620
621         error = vm_get_capability(ctx, BSP, VM_CAP_UNRESTRICTED_GUEST, &tmp);
622
623         /*
624          * The guest is allowed to spinup more than one processor only if the
625          * UNRESTRICTED_GUEST capability is available.
626          */
627         if (error == 0)
628                 return (VM_MAXCPU);
629         else
630                 return (1);
631 }
632
633 void
634 fbsdrun_set_capabilities(struct vmctx *ctx, int cpu)
635 {
636         int err, tmp;
637
638         if (fbsdrun_vmexit_on_hlt()) {
639                 err = vm_get_capability(ctx, cpu, VM_CAP_HALT_EXIT, &tmp);
640                 if (err < 0) {
641                         fprintf(stderr, "VM exit on HLT not supported\n");
642                         exit(1);
643                 }
644                 vm_set_capability(ctx, cpu, VM_CAP_HALT_EXIT, 1);
645                 if (cpu == BSP)
646                         handler[VM_EXITCODE_HLT] = vmexit_hlt;
647         }
648
649         if (fbsdrun_vmexit_on_pause()) {
650                 /*
651                  * pause exit support required for this mode
652                  */
653                 err = vm_get_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, &tmp);
654                 if (err < 0) {
655                         fprintf(stderr,
656                             "SMP mux requested, no pause support\n");
657                         exit(1);
658                 }
659                 vm_set_capability(ctx, cpu, VM_CAP_PAUSE_EXIT, 1);
660                 if (cpu == BSP)
661                         handler[VM_EXITCODE_PAUSE] = vmexit_pause;
662         }
663
664         if (x2apic_mode)
665                 err = vm_set_x2apic_state(ctx, cpu, X2APIC_ENABLED);
666         else
667                 err = vm_set_x2apic_state(ctx, cpu, X2APIC_DISABLED);
668
669         if (err) {
670                 fprintf(stderr, "Unable to set x2apic state (%d)\n", err);
671                 exit(1);
672         }
673
674         vm_set_capability(ctx, cpu, VM_CAP_ENABLE_INVPCID, 1);
675 }
676
677 int
678 main(int argc, char *argv[])
679 {
680         int c, error, gdb_port, err, bvmcons;
681         int dump_guest_memory, max_vcpus, mptgen;
682         struct vmctx *ctx;
683         uint64_t rip;
684         size_t memsize;
685
686         bvmcons = 0;
687         dump_guest_memory = 0;
688         progname = basename(argv[0]);
689         gdb_port = 0;
690         guest_ncpus = 1;
691         memsize = 256 * MB;
692         mptgen = 1;
693
694         while ((c = getopt(argc, argv, "abehwxACHIPWYp:g:c:s:m:l:U:")) != -1) {
695                 switch (c) {
696                 case 'a':
697                         x2apic_mode = 0;
698                         break;
699                 case 'A':
700                         acpi = 1;
701                         break;
702                 case 'b':
703                         bvmcons = 1;
704                         break;
705                 case 'p':
706                         if (pincpu_parse(optarg) != 0) {
707                             errx(EX_USAGE, "invalid vcpu pinning "
708                                  "configuration '%s'", optarg);
709                         }
710                         break;
711                 case 'c':
712                         guest_ncpus = atoi(optarg);
713                         break;
714                 case 'C':
715                         dump_guest_memory = 1;
716                         break;
717                 case 'g':
718                         gdb_port = atoi(optarg);
719                         break;
720                 case 'l':
721                         if (lpc_device_parse(optarg) != 0) {
722                                 errx(EX_USAGE, "invalid lpc device "
723                                     "configuration '%s'", optarg);
724                         }
725                         break;
726                 case 's':
727                         if (pci_parse_slot(optarg) != 0)
728                                 exit(1);
729                         else
730                                 break;
731                 case 'm':
732                         error = vm_parse_memsize(optarg, &memsize);
733                         if (error)
734                                 errx(EX_USAGE, "invalid memsize '%s'", optarg);
735                         break;
736                 case 'H':
737                         guest_vmexit_on_hlt = 1;
738                         break;
739                 case 'I':
740                         /*
741                          * The "-I" option was used to add an ioapic to the
742                          * virtual machine.
743                          *
744                          * An ioapic is now provided unconditionally for each
745                          * virtual machine and this option is now deprecated.
746                          */
747                         break;
748                 case 'P':
749                         guest_vmexit_on_pause = 1;
750                         break;
751                 case 'e':
752                         strictio = 1;
753                         break;
754                 case 'U':
755                         guest_uuid_str = optarg;
756                         break;
757                 case 'w':
758                         strictmsr = 0;
759                         break;
760                 case 'W':
761                         virtio_msix = 0;
762                         break;
763                 case 'x':
764                         x2apic_mode = 1;
765                         break;
766                 case 'Y':
767                         mptgen = 0;
768                         break;
769                 case 'h':
770                         usage(0);                       
771                 default:
772                         usage(1);
773                 }
774         }
775         argc -= optind;
776         argv += optind;
777
778         if (argc != 1)
779                 usage(1);
780
781         vmname = argv[0];
782
783         ctx = vm_open(vmname);
784         if (ctx == NULL) {
785                 perror("vm_open");
786                 exit(1);
787         }
788
789         max_vcpus = num_vcpus_allowed(ctx);
790         if (guest_ncpus > max_vcpus) {
791                 fprintf(stderr, "%d vCPUs requested but only %d available\n",
792                         guest_ncpus, max_vcpus);
793                 exit(1);
794         }
795
796         fbsdrun_set_capabilities(ctx, BSP);
797
798         if (dump_guest_memory)
799                 vm_set_memflags(ctx, VM_MEM_F_INCORE);
800         err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
801         if (err) {
802                 fprintf(stderr, "Unable to setup memory (%d)\n", err);
803                 exit(1);
804         }
805
806         error = init_msr();
807         if (error) {
808                 fprintf(stderr, "init_msr error %d", error);
809                 exit(1);
810         }
811
812         init_mem();
813         init_inout();
814         pci_irq_init(ctx);
815         ioapic_init(ctx);
816
817         rtc_init(ctx);
818         sci_init(ctx);
819
820         /*
821          * Exit if a device emulation finds an error in it's initilization
822          */
823         if (init_pci(ctx) != 0)
824                 exit(1);
825
826         if (gdb_port != 0)
827                 init_dbgport(gdb_port);
828
829         if (bvmcons)
830                 init_bvmcons();
831
832         error = vm_get_register(ctx, BSP, VM_REG_GUEST_RIP, &rip);
833         assert(error == 0);
834
835         /*
836          * build the guest tables, MP etc.
837          */
838         if (mptgen) {
839                 error = mptable_build(ctx, guest_ncpus);
840                 if (error)
841                         exit(1);
842         }
843
844         error = smbios_build(ctx);
845         assert(error == 0);
846
847         if (acpi) {
848                 error = acpi_build(ctx, guest_ncpus);
849                 assert(error == 0);
850         }
851
852         /*
853          * Change the proc title to include the VM name.
854          */
855         setproctitle("%s", vmname); 
856         
857         /*
858          * Add CPU 0
859          */
860         fbsdrun_addcpu(ctx, BSP, BSP, rip);
861
862         /*
863          * Head off to the main event dispatch loop
864          */
865         mevent_dispatch();
866
867         exit(1);
868 }