xref: /freebsd/usr.sbin/bhyvectl/bhyvectl.c (revision 19fae0f66023a97a9b464b3beeeabb2081f575b3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 NetApp, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/cpuset.h>
37 #include <sys/errno.h>
38 #include <sys/mman.h>
39 #include <sys/nv.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
42 #include <sys/un.h>
43 
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <stdbool.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <libgen.h>
50 #include <libutil.h>
51 #include <fcntl.h>
52 #include <getopt.h>
53 #include <time.h>
54 #include <assert.h>
55 #include <libutil.h>
56 
57 #include <machine/cpufunc.h>
58 #include <machine/specialreg.h>
59 #include <machine/vmm.h>
60 #include <machine/vmm_dev.h>
61 #include <vmmapi.h>
62 
63 #include "amd/vmcb.h"
64 #include "intel/vmcs.h"
65 
66 #ifdef BHYVE_SNAPSHOT
67 #include "snapshot.h"
68 #endif
69 
70 #define	MB	(1UL << 20)
71 #define	GB	(1UL << 30)
72 
73 #define	REQ_ARG		required_argument
74 #define	NO_ARG		no_argument
75 #define	OPT_ARG		optional_argument
76 
77 static const char *progname;
78 
79 static void
80 usage(bool cpu_intel)
81 {
82 
83 	(void)fprintf(stderr,
84 	"Usage: %s --vm=<vmname>\n"
85 	"       [--cpu=<vcpu_number>]\n"
86 	"       [--create]\n"
87 	"       [--destroy]\n"
88 #ifdef BHYVE_SNAPSHOT
89 	"       [--checkpoint=<filename> | --suspend=<filename>]\n"
90 #endif
91 	"       [--get-all]\n"
92 	"       [--get-stats]\n"
93 	"       [--set-desc-ds]\n"
94 	"       [--get-desc-ds]\n"
95 	"       [--set-desc-es]\n"
96 	"       [--get-desc-es]\n"
97 	"       [--set-desc-gs]\n"
98 	"       [--get-desc-gs]\n"
99 	"       [--set-desc-fs]\n"
100 	"       [--get-desc-fs]\n"
101 	"       [--set-desc-cs]\n"
102 	"       [--get-desc-cs]\n"
103 	"       [--set-desc-ss]\n"
104 	"       [--get-desc-ss]\n"
105 	"       [--set-desc-tr]\n"
106 	"       [--get-desc-tr]\n"
107 	"       [--set-desc-ldtr]\n"
108 	"       [--get-desc-ldtr]\n"
109 	"       [--set-desc-gdtr]\n"
110 	"       [--get-desc-gdtr]\n"
111 	"       [--set-desc-idtr]\n"
112 	"       [--get-desc-idtr]\n"
113 	"       [--run]\n"
114 	"       [--capname=<capname>]\n"
115 	"       [--getcap]\n"
116 	"       [--setcap=<0|1>]\n"
117 	"       [--desc-base=<BASE>]\n"
118 	"       [--desc-limit=<LIMIT>]\n"
119 	"       [--desc-access=<ACCESS>]\n"
120 	"       [--set-cr0=<CR0>]\n"
121 	"       [--get-cr0]\n"
122 	"       [--set-cr2=<CR2>]\n"
123 	"       [--get-cr2]\n"
124 	"       [--set-cr3=<CR3>]\n"
125 	"       [--get-cr3]\n"
126 	"       [--set-cr4=<CR4>]\n"
127 	"       [--get-cr4]\n"
128 	"       [--set-dr0=<DR0>]\n"
129 	"       [--get-dr0]\n"
130 	"       [--set-dr1=<DR1>]\n"
131 	"       [--get-dr1]\n"
132 	"       [--set-dr2=<DR2>]\n"
133 	"       [--get-dr2]\n"
134 	"       [--set-dr3=<DR3>]\n"
135 	"       [--get-dr3]\n"
136 	"       [--set-dr6=<DR6>]\n"
137 	"       [--get-dr6]\n"
138 	"       [--set-dr7=<DR7>]\n"
139 	"       [--get-dr7]\n"
140 	"       [--set-rsp=<RSP>]\n"
141 	"       [--get-rsp]\n"
142 	"       [--set-rip=<RIP>]\n"
143 	"       [--get-rip]\n"
144 	"       [--get-rax]\n"
145 	"       [--set-rax=<RAX>]\n"
146 	"       [--get-rbx]\n"
147 	"       [--get-rcx]\n"
148 	"       [--get-rdx]\n"
149 	"       [--get-rsi]\n"
150 	"       [--get-rdi]\n"
151 	"       [--get-rbp]\n"
152 	"       [--get-r8]\n"
153 	"       [--get-r9]\n"
154 	"       [--get-r10]\n"
155 	"       [--get-r11]\n"
156 	"       [--get-r12]\n"
157 	"       [--get-r13]\n"
158 	"       [--get-r14]\n"
159 	"       [--get-r15]\n"
160 	"       [--set-rflags=<RFLAGS>]\n"
161 	"       [--get-rflags]\n"
162 	"       [--set-cs]\n"
163 	"       [--get-cs]\n"
164 	"       [--set-ds]\n"
165 	"       [--get-ds]\n"
166 	"       [--set-es]\n"
167 	"       [--get-es]\n"
168 	"       [--set-fs]\n"
169 	"       [--get-fs]\n"
170 	"       [--set-gs]\n"
171 	"       [--get-gs]\n"
172 	"       [--set-ss]\n"
173 	"       [--get-ss]\n"
174 	"       [--get-tr]\n"
175 	"       [--get-ldtr]\n"
176 	"       [--set-x2apic-state=<state>]\n"
177 	"       [--get-x2apic-state]\n"
178 	"       [--unassign-pptdev=<bus/slot/func>]\n"
179 	"       [--set-mem=<memory in units of MB>]\n"
180 	"       [--get-lowmem]\n"
181 	"       [--get-highmem]\n"
182 	"       [--get-gpa-pmap]\n"
183 	"       [--assert-lapic-lvt=<pin>]\n"
184 	"       [--inject-nmi]\n"
185 	"       [--force-reset]\n"
186 	"       [--force-poweroff]\n"
187 	"       [--get-rtc-time]\n"
188 	"       [--set-rtc-time=<secs>]\n"
189 	"       [--get-rtc-nvram]\n"
190 	"       [--set-rtc-nvram=<val>]\n"
191 	"       [--rtc-nvram-offset=<offset>]\n"
192 	"       [--get-active-cpus]\n"
193 	"       [--get-debug-cpus]\n"
194 	"       [--get-suspended-cpus]\n"
195 	"       [--get-intinfo]\n"
196 	"       [--get-eptp]\n"
197 	"       [--set-exception-bitmap]\n"
198 	"       [--get-exception-bitmap]\n"
199 	"       [--get-tsc-offset]\n"
200 	"       [--get-guest-pat]\n"
201 	"       [--get-io-bitmap-address]\n"
202 	"       [--get-msr-bitmap]\n"
203 	"       [--get-msr-bitmap-address]\n"
204 	"       [--get-guest-sysenter]\n"
205 	"       [--get-exit-reason]\n"
206 	"       [--get-cpu-topology]\n",
207 	progname);
208 
209 	if (cpu_intel) {
210 		(void)fprintf(stderr,
211 		"       [--get-vmcs-pinbased-ctls]\n"
212 		"       [--get-vmcs-procbased-ctls]\n"
213 		"       [--get-vmcs-procbased-ctls2]\n"
214 		"       [--get-vmcs-entry-interruption-info]\n"
215 		"       [--set-vmcs-entry-interruption-info=<info>]\n"
216 		"       [--get-vmcs-guest-physical-address\n"
217 		"       [--get-vmcs-guest-linear-address\n"
218 		"       [--get-vmcs-host-pat]\n"
219 		"       [--get-vmcs-host-cr0]\n"
220 		"       [--get-vmcs-host-cr3]\n"
221 		"       [--get-vmcs-host-cr4]\n"
222 		"       [--get-vmcs-host-rip]\n"
223 		"       [--get-vmcs-host-rsp]\n"
224 		"       [--get-vmcs-cr0-mask]\n"
225 		"       [--get-vmcs-cr0-shadow]\n"
226 		"       [--get-vmcs-cr4-mask]\n"
227 		"       [--get-vmcs-cr4-shadow]\n"
228 		"       [--get-vmcs-cr3-targets]\n"
229 		"       [--get-vmcs-apic-access-address]\n"
230 		"       [--get-vmcs-virtual-apic-address]\n"
231 		"       [--get-vmcs-tpr-threshold]\n"
232 		"       [--get-vmcs-vpid]\n"
233 		"       [--get-vmcs-instruction-error]\n"
234 		"       [--get-vmcs-exit-ctls]\n"
235 		"       [--get-vmcs-entry-ctls]\n"
236 		"       [--get-vmcs-link]\n"
237 		"       [--get-vmcs-exit-qualification]\n"
238 		"       [--get-vmcs-exit-interruption-info]\n"
239 		"       [--get-vmcs-exit-interruption-error]\n"
240 		"       [--get-vmcs-interruptibility]\n"
241 		);
242 	} else {
243 		(void)fprintf(stderr,
244 		"       [--get-vmcb-intercepts]\n"
245 		"       [--get-vmcb-asid]\n"
246 		"       [--get-vmcb-exit-details]\n"
247 		"       [--get-vmcb-tlb-ctrl]\n"
248 		"       [--get-vmcb-virq]\n"
249 		"       [--get-avic-apic-bar]\n"
250 		"       [--get-avic-backing-page]\n"
251 		"       [--get-avic-table]\n"
252 		);
253 	}
254 	exit(1);
255 }
256 
257 static int get_rtc_time, set_rtc_time;
258 static int get_rtc_nvram, set_rtc_nvram;
259 static int rtc_nvram_offset;
260 static uint8_t rtc_nvram_value;
261 static time_t rtc_secs;
262 
263 static int get_stats, getcap, setcap, capval, get_gpa_pmap;
264 static int inject_nmi, assert_lapic_lvt;
265 static int force_reset, force_poweroff;
266 static const char *capname;
267 static int create, destroy, get_memmap, get_memseg;
268 static int get_intinfo;
269 static int get_active_cpus, get_debug_cpus, get_suspended_cpus;
270 static uint64_t memsize;
271 static int set_cr0, get_cr0, set_cr2, get_cr2, set_cr3, get_cr3;
272 static int set_cr4, get_cr4;
273 static int set_efer, get_efer;
274 static int set_dr0, get_dr0;
275 static int set_dr1, get_dr1;
276 static int set_dr2, get_dr2;
277 static int set_dr3, get_dr3;
278 static int set_dr6, get_dr6;
279 static int set_dr7, get_dr7;
280 static int set_rsp, get_rsp, set_rip, get_rip, set_rflags, get_rflags;
281 static int set_rax, get_rax;
282 static int get_rbx, get_rcx, get_rdx, get_rsi, get_rdi, get_rbp;
283 static int get_r8, get_r9, get_r10, get_r11, get_r12, get_r13, get_r14, get_r15;
284 static int set_desc_ds, get_desc_ds;
285 static int set_desc_es, get_desc_es;
286 static int set_desc_fs, get_desc_fs;
287 static int set_desc_gs, get_desc_gs;
288 static int set_desc_cs, get_desc_cs;
289 static int set_desc_ss, get_desc_ss;
290 static int set_desc_gdtr, get_desc_gdtr;
291 static int set_desc_idtr, get_desc_idtr;
292 static int set_desc_tr, get_desc_tr;
293 static int set_desc_ldtr, get_desc_ldtr;
294 static int set_cs, set_ds, set_es, set_fs, set_gs, set_ss, set_tr, set_ldtr;
295 static int get_cs, get_ds, get_es, get_fs, get_gs, get_ss, get_tr, get_ldtr;
296 static int set_x2apic_state, get_x2apic_state;
297 static enum x2apic_state x2apic_state;
298 static int unassign_pptdev, bus, slot, func;
299 static int run;
300 static int get_cpu_topology;
301 #ifdef BHYVE_SNAPSHOT
302 static int vm_suspend_opt;
303 #endif
304 
305 /*
306  * VMCB specific.
307  */
308 static int get_vmcb_intercept, get_vmcb_exit_details, get_vmcb_tlb_ctrl;
309 static int get_vmcb_virq, get_avic_table;
310 
311 /*
312  * VMCS-specific fields
313  */
314 static int get_pinbased_ctls, get_procbased_ctls, get_procbased_ctls2;
315 static int get_eptp, get_io_bitmap, get_tsc_offset;
316 static int get_vmcs_entry_interruption_info;
317 static int get_vmcs_interruptibility;
318 static int get_vmcs_gpa, get_vmcs_gla;
319 static int get_exception_bitmap;
320 static int get_cr0_mask, get_cr0_shadow;
321 static int get_cr4_mask, get_cr4_shadow;
322 static int get_cr3_targets;
323 static int get_apic_access_addr, get_virtual_apic_addr, get_tpr_threshold;
324 static int get_msr_bitmap, get_msr_bitmap_address;
325 static int get_vpid_asid;
326 static int get_inst_err, get_exit_ctls, get_entry_ctls;
327 static int get_host_cr0, get_host_cr3, get_host_cr4;
328 static int get_host_rip, get_host_rsp;
329 static int get_guest_pat, get_host_pat;
330 static int get_guest_sysenter, get_vmcs_link;
331 static int get_exit_reason, get_vmcs_exit_qualification;
332 static int get_vmcs_exit_interruption_info, get_vmcs_exit_interruption_error;
333 static int get_vmcs_exit_inst_length;
334 
335 static uint64_t desc_base;
336 static uint32_t desc_limit, desc_access;
337 
338 static int get_all;
339 
340 static void
341 dump_vm_run_exitcode(struct vm_exit *vmexit, int vcpu)
342 {
343 	printf("vm exit[%d]\n", vcpu);
344 	printf("\trip\t\t0x%016lx\n", vmexit->rip);
345 	printf("\tinst_length\t%d\n", vmexit->inst_length);
346 	switch (vmexit->exitcode) {
347 	case VM_EXITCODE_INOUT:
348 		printf("\treason\t\tINOUT\n");
349 		printf("\tdirection\t%s\n", vmexit->u.inout.in ? "IN" : "OUT");
350 		printf("\tbytes\t\t%d\n", vmexit->u.inout.bytes);
351 		printf("\tflags\t\t%s%s\n",
352 			vmexit->u.inout.string ? "STRING " : "",
353 			vmexit->u.inout.rep ? "REP " : "");
354 		printf("\tport\t\t0x%04x\n", vmexit->u.inout.port);
355 		printf("\teax\t\t0x%08x\n", vmexit->u.inout.eax);
356 		break;
357 	case VM_EXITCODE_VMX:
358 		printf("\treason\t\tVMX\n");
359 		printf("\tstatus\t\t%d\n", vmexit->u.vmx.status);
360 		printf("\texit_reason\t0x%08x (%u)\n",
361 		    vmexit->u.vmx.exit_reason, vmexit->u.vmx.exit_reason);
362 		printf("\tqualification\t0x%016lx\n",
363 			vmexit->u.vmx.exit_qualification);
364 		printf("\tinst_type\t\t%d\n", vmexit->u.vmx.inst_type);
365 		printf("\tinst_error\t\t%d\n", vmexit->u.vmx.inst_error);
366 		break;
367 	case VM_EXITCODE_SVM:
368 		printf("\treason\t\tSVM\n");
369 		printf("\texit_reason\t\t%#lx\n", vmexit->u.svm.exitcode);
370 		printf("\texitinfo1\t\t%#lx\n", vmexit->u.svm.exitinfo1);
371 		printf("\texitinfo2\t\t%#lx\n", vmexit->u.svm.exitinfo2);
372 		break;
373 	default:
374 		printf("*** unknown vm run exitcode %d\n", vmexit->exitcode);
375 		break;
376 	}
377 }
378 
379 /* AMD 6th generation and Intel compatible MSRs */
380 #define MSR_AMD6TH_START	0xC0000000
381 #define MSR_AMD6TH_END		0xC0001FFF
382 /* AMD 7th and 8th generation compatible MSRs */
383 #define MSR_AMD7TH_START	0xC0010000
384 #define MSR_AMD7TH_END		0xC0011FFF
385 
386 static const char *
387 msr_name(uint32_t msr)
388 {
389 	static char buf[32];
390 
391 	switch(msr) {
392 	case MSR_TSC:
393 		return ("MSR_TSC");
394 	case MSR_EFER:
395 		return ("MSR_EFER");
396 	case MSR_STAR:
397 		return ("MSR_STAR");
398 	case MSR_LSTAR:
399 		return ("MSR_LSTAR");
400 	case MSR_CSTAR:
401 		return ("MSR_CSTAR");
402 	case MSR_SF_MASK:
403 		return ("MSR_SF_MASK");
404 	case MSR_FSBASE:
405 		return ("MSR_FSBASE");
406 	case MSR_GSBASE:
407 		return ("MSR_GSBASE");
408 	case MSR_KGSBASE:
409 		return ("MSR_KGSBASE");
410 	case MSR_SYSENTER_CS_MSR:
411 		return ("MSR_SYSENTER_CS_MSR");
412 	case MSR_SYSENTER_ESP_MSR:
413 		return ("MSR_SYSENTER_ESP_MSR");
414 	case MSR_SYSENTER_EIP_MSR:
415 		return ("MSR_SYSENTER_EIP_MSR");
416 	case MSR_PAT:
417 		return ("MSR_PAT");
418 	}
419 	snprintf(buf, sizeof(buf), "MSR       %#08x", msr);
420 
421 	return (buf);
422 }
423 
424 static inline void
425 print_msr_pm(uint64_t msr, int vcpu, int readable, int writeable)
426 {
427 
428 	if (readable || writeable) {
429 		printf("%-20s[%d]\t\t%c%c\n", msr_name(msr), vcpu,
430 			readable ? 'R' : '-', writeable ? 'W' : '-');
431 	}
432 }
433 
434 /*
435  * Reference APM vol2, section 15.11 MSR Intercepts.
436  */
437 static void
438 dump_amd_msr_pm(const char *bitmap, int vcpu)
439 {
440 	int byte, bit, readable, writeable;
441 	uint32_t msr;
442 
443 	for (msr = 0; msr < 0x2000; msr++) {
444 		byte = msr / 4;
445 		bit = (msr % 4) * 2;
446 
447 		/* Look at MSRs in the range 0x00000000 to 0x00001FFF */
448 		readable = (bitmap[byte] & (1 << bit)) ? 0 : 1;
449 		writeable = (bitmap[byte] & (2 << bit)) ?  0 : 1;
450 		print_msr_pm(msr, vcpu, readable, writeable);
451 
452 		/* Look at MSRs in the range 0xC0000000 to 0xC0001FFF */
453 		byte += 2048;
454 		readable = (bitmap[byte] & (1 << bit)) ? 0 : 1;
455 		writeable = (bitmap[byte] & (2 << bit)) ?  0 : 1;
456 		print_msr_pm(msr + MSR_AMD6TH_START, vcpu, readable,
457 				writeable);
458 
459 		/* MSR 0xC0010000 to 0xC0011FF is only for AMD */
460 		byte += 4096;
461 		readable = (bitmap[byte] & (1 << bit)) ? 0 : 1;
462 		writeable = (bitmap[byte] & (2 << bit)) ?  0 : 1;
463 		print_msr_pm(msr + MSR_AMD7TH_START, vcpu, readable,
464 				writeable);
465 	}
466 }
467 
468 /*
469  * Reference Intel SDM Vol3 Section 24.6.9 MSR-Bitmap Address
470  */
471 static void
472 dump_intel_msr_pm(const char *bitmap, int vcpu)
473 {
474 	int byte, bit, readable, writeable;
475 	uint32_t msr;
476 
477 	for (msr = 0; msr < 0x2000; msr++) {
478 		byte = msr / 8;
479 		bit = msr & 0x7;
480 
481 		/* Look at MSRs in the range 0x00000000 to 0x00001FFF */
482 		readable = (bitmap[byte] & (1 << bit)) ? 0 : 1;
483 		writeable = (bitmap[2048 + byte] & (1 << bit)) ?  0 : 1;
484 		print_msr_pm(msr, vcpu, readable, writeable);
485 
486 		/* Look at MSRs in the range 0xC0000000 to 0xC0001FFF */
487 		byte += 1024;
488 		readable = (bitmap[byte] & (1 << bit)) ? 0 : 1;
489 		writeable = (bitmap[2048 + byte] & (1 << bit)) ?  0 : 1;
490 		print_msr_pm(msr + MSR_AMD6TH_START, vcpu, readable,
491 				writeable);
492 	}
493 }
494 
495 static int
496 dump_msr_bitmap(int vcpu, uint64_t addr, bool cpu_intel)
497 {
498 	char *bitmap;
499 	int error, fd, map_size;
500 
501 	error = -1;
502 	bitmap = MAP_FAILED;
503 
504 	fd = open("/dev/mem", O_RDONLY, 0);
505 	if (fd < 0) {
506 		perror("Couldn't open /dev/mem");
507 		goto done;
508 	}
509 
510 	if (cpu_intel)
511 		map_size = PAGE_SIZE;
512 	else
513 		map_size = 2 * PAGE_SIZE;
514 
515 	bitmap = mmap(NULL, map_size, PROT_READ, MAP_SHARED, fd, addr);
516 	if (bitmap == MAP_FAILED) {
517 		perror("mmap failed");
518 		goto done;
519 	}
520 
521 	if (cpu_intel)
522 		dump_intel_msr_pm(bitmap, vcpu);
523 	else
524 		dump_amd_msr_pm(bitmap, vcpu);
525 
526 	error = 0;
527 done:
528 	if (bitmap != MAP_FAILED)
529 		munmap((void *)bitmap, map_size);
530 	if (fd >= 0)
531 		close(fd);
532 
533 	return (error);
534 }
535 
536 static int
537 vm_get_vmcs_field(struct vcpu *vcpu, int field, uint64_t *ret_val)
538 {
539 
540 	return (vm_get_register(vcpu, VMCS_IDENT(field), ret_val));
541 }
542 
543 static int
544 vm_get_vmcb_field(struct vcpu *vcpu, int off, int bytes,
545 	uint64_t *ret_val)
546 {
547 
548 	return (vm_get_register(vcpu, VMCB_ACCESS(off, bytes), ret_val));
549 }
550 
551 enum {
552 	VMNAME = 1000,	/* avoid collision with return values from getopt */
553 	VCPU,
554 	SET_MEM,
555 	SET_EFER,
556 	SET_CR0,
557 	SET_CR2,
558 	SET_CR3,
559 	SET_CR4,
560 	SET_DR0,
561 	SET_DR1,
562 	SET_DR2,
563 	SET_DR3,
564 	SET_DR6,
565 	SET_DR7,
566 	SET_RSP,
567 	SET_RIP,
568 	SET_RAX,
569 	SET_RFLAGS,
570 	DESC_BASE,
571 	DESC_LIMIT,
572 	DESC_ACCESS,
573 	SET_CS,
574 	SET_DS,
575 	SET_ES,
576 	SET_FS,
577 	SET_GS,
578 	SET_SS,
579 	SET_TR,
580 	SET_LDTR,
581 	SET_X2APIC_STATE,
582 	SET_CAP,
583 	CAPNAME,
584 	UNASSIGN_PPTDEV,
585 	GET_GPA_PMAP,
586 	ASSERT_LAPIC_LVT,
587 	SET_RTC_TIME,
588 	SET_RTC_NVRAM,
589 	RTC_NVRAM_OFFSET,
590 #ifdef BHYVE_SNAPSHOT
591 	SET_CHECKPOINT_FILE,
592 	SET_SUSPEND_FILE,
593 #endif
594 };
595 
596 static void
597 print_cpus(const char *banner, const cpuset_t *cpus)
598 {
599 	int i, first;
600 
601 	first = 1;
602 	printf("%s:\t", banner);
603 	if (!CPU_EMPTY(cpus)) {
604 		for (i = 0; i < CPU_SETSIZE; i++) {
605 			if (CPU_ISSET(i, cpus)) {
606 				printf("%s%d", first ? " " : ", ", i);
607 				first = 0;
608 			}
609 		}
610 	} else
611 		printf(" (none)");
612 	printf("\n");
613 }
614 
615 static void
616 print_intinfo(const char *banner, uint64_t info)
617 {
618 	int type;
619 
620 	printf("%s:\t", banner);
621 	if (info & VM_INTINFO_VALID) {
622 		type = info & VM_INTINFO_TYPE;
623 		switch (type) {
624 		case VM_INTINFO_HWINTR:
625 			printf("extint");
626 			break;
627 		case VM_INTINFO_NMI:
628 			printf("nmi");
629 			break;
630 		case VM_INTINFO_SWINTR:
631 			printf("swint");
632 			break;
633 		default:
634 			printf("exception");
635 			break;
636 		}
637 		printf(" vector %d", (int)VM_INTINFO_VECTOR(info));
638 		if (info & VM_INTINFO_DEL_ERRCODE)
639 			printf(" errcode %#x", (u_int)(info >> 32));
640 	} else {
641 		printf("n/a");
642 	}
643 	printf("\n");
644 }
645 
646 static bool
647 cpu_vendor_intel(void)
648 {
649 	u_int regs[4], v[3];
650 
651 	do_cpuid(0, regs);
652 	v[0] = regs[1];
653 	v[1] = regs[3];
654 	v[2] = regs[2];
655 
656 	if (memcmp(v, "GenuineIntel", sizeof(v)) == 0)
657 		return (true);
658 	if (memcmp(v, "AuthenticAMD", sizeof(v)) == 0 ||
659 	    memcmp(v, "HygonGenuine", sizeof(v)) == 0)
660 		return (false);
661 	fprintf(stderr, "Unknown cpu vendor \"%s\"\n", (const char *)v);
662 	exit(1);
663 }
664 
665 static int
666 get_all_registers(struct vcpu *vcpu, int vcpuid)
667 {
668 	uint64_t cr0, cr2, cr3, cr4, dr0, dr1, dr2, dr3, dr6, dr7;
669 	uint64_t rsp, rip, rflags, efer;
670 	uint64_t rax, rbx, rcx, rdx, rsi, rdi, rbp;
671 	uint64_t r8, r9, r10, r11, r12, r13, r14, r15;
672 	int error = 0;
673 
674 	if (!error && (get_efer || get_all)) {
675 		error = vm_get_register(vcpu, VM_REG_GUEST_EFER, &efer);
676 		if (error == 0)
677 			printf("efer[%d]\t\t0x%016lx\n", vcpuid, efer);
678 	}
679 
680 	if (!error && (get_cr0 || get_all)) {
681 		error = vm_get_register(vcpu, VM_REG_GUEST_CR0, &cr0);
682 		if (error == 0)
683 			printf("cr0[%d]\t\t0x%016lx\n", vcpuid, cr0);
684 	}
685 
686 	if (!error && (get_cr2 || get_all)) {
687 		error = vm_get_register(vcpu, VM_REG_GUEST_CR2, &cr2);
688 		if (error == 0)
689 			printf("cr2[%d]\t\t0x%016lx\n", vcpuid, cr2);
690 	}
691 
692 	if (!error && (get_cr3 || get_all)) {
693 		error = vm_get_register(vcpu, VM_REG_GUEST_CR3, &cr3);
694 		if (error == 0)
695 			printf("cr3[%d]\t\t0x%016lx\n", vcpuid, cr3);
696 	}
697 
698 	if (!error && (get_cr4 || get_all)) {
699 		error = vm_get_register(vcpu, VM_REG_GUEST_CR4, &cr4);
700 		if (error == 0)
701 			printf("cr4[%d]\t\t0x%016lx\n", vcpuid, cr4);
702 	}
703 
704 	if (!error && (get_dr0 || get_all)) {
705 		error = vm_get_register(vcpu, VM_REG_GUEST_DR0, &dr0);
706 		if (error == 0)
707 			printf("dr0[%d]\t\t0x%016lx\n", vcpuid, dr0);
708 	}
709 
710 	if (!error && (get_dr1 || get_all)) {
711 		error = vm_get_register(vcpu, VM_REG_GUEST_DR1, &dr1);
712 		if (error == 0)
713 			printf("dr1[%d]\t\t0x%016lx\n", vcpuid, dr1);
714 	}
715 
716 	if (!error && (get_dr2 || get_all)) {
717 		error = vm_get_register(vcpu, VM_REG_GUEST_DR2, &dr2);
718 		if (error == 0)
719 			printf("dr2[%d]\t\t0x%016lx\n", vcpuid, dr2);
720 	}
721 
722 	if (!error && (get_dr3 || get_all)) {
723 		error = vm_get_register(vcpu, VM_REG_GUEST_DR3, &dr3);
724 		if (error == 0)
725 			printf("dr3[%d]\t\t0x%016lx\n", vcpuid, dr3);
726 	}
727 
728 	if (!error && (get_dr6 || get_all)) {
729 		error = vm_get_register(vcpu, VM_REG_GUEST_DR6, &dr6);
730 		if (error == 0)
731 			printf("dr6[%d]\t\t0x%016lx\n", vcpuid, dr6);
732 	}
733 
734 	if (!error && (get_dr7 || get_all)) {
735 		error = vm_get_register(vcpu, VM_REG_GUEST_DR7, &dr7);
736 		if (error == 0)
737 			printf("dr7[%d]\t\t0x%016lx\n", vcpuid, dr7);
738 	}
739 
740 	if (!error && (get_rsp || get_all)) {
741 		error = vm_get_register(vcpu, VM_REG_GUEST_RSP, &rsp);
742 		if (error == 0)
743 			printf("rsp[%d]\t\t0x%016lx\n", vcpuid, rsp);
744 	}
745 
746 	if (!error && (get_rip || get_all)) {
747 		error = vm_get_register(vcpu, VM_REG_GUEST_RIP, &rip);
748 		if (error == 0)
749 			printf("rip[%d]\t\t0x%016lx\n", vcpuid, rip);
750 	}
751 
752 	if (!error && (get_rax || get_all)) {
753 		error = vm_get_register(vcpu, VM_REG_GUEST_RAX, &rax);
754 		if (error == 0)
755 			printf("rax[%d]\t\t0x%016lx\n", vcpuid, rax);
756 	}
757 
758 	if (!error && (get_rbx || get_all)) {
759 		error = vm_get_register(vcpu, VM_REG_GUEST_RBX, &rbx);
760 		if (error == 0)
761 			printf("rbx[%d]\t\t0x%016lx\n", vcpuid, rbx);
762 	}
763 
764 	if (!error && (get_rcx || get_all)) {
765 		error = vm_get_register(vcpu, VM_REG_GUEST_RCX, &rcx);
766 		if (error == 0)
767 			printf("rcx[%d]\t\t0x%016lx\n", vcpuid, rcx);
768 	}
769 
770 	if (!error && (get_rdx || get_all)) {
771 		error = vm_get_register(vcpu, VM_REG_GUEST_RDX, &rdx);
772 		if (error == 0)
773 			printf("rdx[%d]\t\t0x%016lx\n", vcpuid, rdx);
774 	}
775 
776 	if (!error && (get_rsi || get_all)) {
777 		error = vm_get_register(vcpu, VM_REG_GUEST_RSI, &rsi);
778 		if (error == 0)
779 			printf("rsi[%d]\t\t0x%016lx\n", vcpuid, rsi);
780 	}
781 
782 	if (!error && (get_rdi || get_all)) {
783 		error = vm_get_register(vcpu, VM_REG_GUEST_RDI, &rdi);
784 		if (error == 0)
785 			printf("rdi[%d]\t\t0x%016lx\n", vcpuid, rdi);
786 	}
787 
788 	if (!error && (get_rbp || get_all)) {
789 		error = vm_get_register(vcpu, VM_REG_GUEST_RBP, &rbp);
790 		if (error == 0)
791 			printf("rbp[%d]\t\t0x%016lx\n", vcpuid, rbp);
792 	}
793 
794 	if (!error && (get_r8 || get_all)) {
795 		error = vm_get_register(vcpu, VM_REG_GUEST_R8, &r8);
796 		if (error == 0)
797 			printf("r8[%d]\t\t0x%016lx\n", vcpuid, r8);
798 	}
799 
800 	if (!error && (get_r9 || get_all)) {
801 		error = vm_get_register(vcpu, VM_REG_GUEST_R9, &r9);
802 		if (error == 0)
803 			printf("r9[%d]\t\t0x%016lx\n", vcpuid, r9);
804 	}
805 
806 	if (!error && (get_r10 || get_all)) {
807 		error = vm_get_register(vcpu, VM_REG_GUEST_R10, &r10);
808 		if (error == 0)
809 			printf("r10[%d]\t\t0x%016lx\n", vcpuid, r10);
810 	}
811 
812 	if (!error && (get_r11 || get_all)) {
813 		error = vm_get_register(vcpu, VM_REG_GUEST_R11, &r11);
814 		if (error == 0)
815 			printf("r11[%d]\t\t0x%016lx\n", vcpuid, r11);
816 	}
817 
818 	if (!error && (get_r12 || get_all)) {
819 		error = vm_get_register(vcpu, VM_REG_GUEST_R12, &r12);
820 		if (error == 0)
821 			printf("r12[%d]\t\t0x%016lx\n", vcpuid, r12);
822 	}
823 
824 	if (!error && (get_r13 || get_all)) {
825 		error = vm_get_register(vcpu, VM_REG_GUEST_R13, &r13);
826 		if (error == 0)
827 			printf("r13[%d]\t\t0x%016lx\n", vcpuid, r13);
828 	}
829 
830 	if (!error && (get_r14 || get_all)) {
831 		error = vm_get_register(vcpu, VM_REG_GUEST_R14, &r14);
832 		if (error == 0)
833 			printf("r14[%d]\t\t0x%016lx\n", vcpuid, r14);
834 	}
835 
836 	if (!error && (get_r15 || get_all)) {
837 		error = vm_get_register(vcpu, VM_REG_GUEST_R15, &r15);
838 		if (error == 0)
839 			printf("r15[%d]\t\t0x%016lx\n", vcpuid, r15);
840 	}
841 
842 	if (!error && (get_rflags || get_all)) {
843 		error = vm_get_register(vcpu, VM_REG_GUEST_RFLAGS,
844 					&rflags);
845 		if (error == 0)
846 			printf("rflags[%d]\t0x%016lx\n", vcpuid, rflags);
847 	}
848 
849 	return (error);
850 }
851 
852 static int
853 get_all_segments(struct vcpu *vcpu, int vcpuid)
854 {
855 	uint64_t cs, ds, es, fs, gs, ss, tr, ldtr;
856 	int error = 0;
857 
858 	if (!error && (get_desc_ds || get_all)) {
859 		error = vm_get_desc(vcpu, VM_REG_GUEST_DS,
860 				   &desc_base, &desc_limit, &desc_access);
861 		if (error == 0) {
862 			printf("ds desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
863 			      vcpuid, desc_base, desc_limit, desc_access);
864 		}
865 	}
866 
867 	if (!error && (get_desc_es || get_all)) {
868 		error = vm_get_desc(vcpu, VM_REG_GUEST_ES,
869 				    &desc_base, &desc_limit, &desc_access);
870 		if (error == 0) {
871 			printf("es desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
872 			       vcpuid, desc_base, desc_limit, desc_access);
873 		}
874 	}
875 
876 	if (!error && (get_desc_fs || get_all)) {
877 		error = vm_get_desc(vcpu, VM_REG_GUEST_FS,
878 				    &desc_base, &desc_limit, &desc_access);
879 		if (error == 0) {
880 			printf("fs desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
881 			       vcpuid, desc_base, desc_limit, desc_access);
882 		}
883 	}
884 
885 	if (!error && (get_desc_gs || get_all)) {
886 		error = vm_get_desc(vcpu, VM_REG_GUEST_GS,
887 				    &desc_base, &desc_limit, &desc_access);
888 		if (error == 0) {
889 			printf("gs desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
890 			       vcpuid, desc_base, desc_limit, desc_access);
891 		}
892 	}
893 
894 	if (!error && (get_desc_ss || get_all)) {
895 		error = vm_get_desc(vcpu, VM_REG_GUEST_SS,
896 				    &desc_base, &desc_limit, &desc_access);
897 		if (error == 0) {
898 			printf("ss desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
899 			       vcpuid, desc_base, desc_limit, desc_access);
900 		}
901 	}
902 
903 	if (!error && (get_desc_cs || get_all)) {
904 		error = vm_get_desc(vcpu, VM_REG_GUEST_CS,
905 				    &desc_base, &desc_limit, &desc_access);
906 		if (error == 0) {
907 			printf("cs desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
908 			       vcpuid, desc_base, desc_limit, desc_access);
909 		}
910 	}
911 
912 	if (!error && (get_desc_tr || get_all)) {
913 		error = vm_get_desc(vcpu, VM_REG_GUEST_TR,
914 				    &desc_base, &desc_limit, &desc_access);
915 		if (error == 0) {
916 			printf("tr desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
917 			       vcpuid, desc_base, desc_limit, desc_access);
918 		}
919 	}
920 
921 	if (!error && (get_desc_ldtr || get_all)) {
922 		error = vm_get_desc(vcpu, VM_REG_GUEST_LDTR,
923 				    &desc_base, &desc_limit, &desc_access);
924 		if (error == 0) {
925 			printf("ldtr desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
926 			       vcpuid, desc_base, desc_limit, desc_access);
927 		}
928 	}
929 
930 	if (!error && (get_desc_gdtr || get_all)) {
931 		error = vm_get_desc(vcpu, VM_REG_GUEST_GDTR,
932 				    &desc_base, &desc_limit, &desc_access);
933 		if (error == 0) {
934 			printf("gdtr[%d]\t\t0x%016lx/0x%08x\n",
935 			       vcpuid, desc_base, desc_limit);
936 		}
937 	}
938 
939 	if (!error && (get_desc_idtr || get_all)) {
940 		error = vm_get_desc(vcpu, VM_REG_GUEST_IDTR,
941 				    &desc_base, &desc_limit, &desc_access);
942 		if (error == 0) {
943 			printf("idtr[%d]\t\t0x%016lx/0x%08x\n",
944 			       vcpuid, desc_base, desc_limit);
945 		}
946 	}
947 
948 	if (!error && (get_cs || get_all)) {
949 		error = vm_get_register(vcpu, VM_REG_GUEST_CS, &cs);
950 		if (error == 0)
951 			printf("cs[%d]\t\t0x%04lx\n", vcpuid, cs);
952 	}
953 
954 	if (!error && (get_ds || get_all)) {
955 		error = vm_get_register(vcpu, VM_REG_GUEST_DS, &ds);
956 		if (error == 0)
957 			printf("ds[%d]\t\t0x%04lx\n", vcpuid, ds);
958 	}
959 
960 	if (!error && (get_es || get_all)) {
961 		error = vm_get_register(vcpu, VM_REG_GUEST_ES, &es);
962 		if (error == 0)
963 			printf("es[%d]\t\t0x%04lx\n", vcpuid, es);
964 	}
965 
966 	if (!error && (get_fs || get_all)) {
967 		error = vm_get_register(vcpu, VM_REG_GUEST_FS, &fs);
968 		if (error == 0)
969 			printf("fs[%d]\t\t0x%04lx\n", vcpuid, fs);
970 	}
971 
972 	if (!error && (get_gs || get_all)) {
973 		error = vm_get_register(vcpu, VM_REG_GUEST_GS, &gs);
974 		if (error == 0)
975 			printf("gs[%d]\t\t0x%04lx\n", vcpuid, gs);
976 	}
977 
978 	if (!error && (get_ss || get_all)) {
979 		error = vm_get_register(vcpu, VM_REG_GUEST_SS, &ss);
980 		if (error == 0)
981 			printf("ss[%d]\t\t0x%04lx\n", vcpuid, ss);
982 	}
983 
984 	if (!error && (get_tr || get_all)) {
985 		error = vm_get_register(vcpu, VM_REG_GUEST_TR, &tr);
986 		if (error == 0)
987 			printf("tr[%d]\t\t0x%04lx\n", vcpuid, tr);
988 	}
989 
990 	if (!error && (get_ldtr || get_all)) {
991 		error = vm_get_register(vcpu, VM_REG_GUEST_LDTR, &ldtr);
992 		if (error == 0)
993 			printf("ldtr[%d]\t\t0x%04lx\n", vcpuid, ldtr);
994 	}
995 
996 	return (error);
997 }
998 
999 static int
1000 get_misc_vmcs(struct vcpu *vcpu, int vcpuid)
1001 {
1002 	uint64_t ctl, cr0, cr3, cr4, rsp, rip, pat, addr, u64;
1003 	int error = 0;
1004 
1005 	if (!error && (get_cr0_mask || get_all)) {
1006 		uint64_t cr0mask;
1007 		error = vm_get_vmcs_field(vcpu, VMCS_CR0_MASK, &cr0mask);
1008 		if (error == 0)
1009 			printf("cr0_mask[%d]\t\t0x%016lx\n", vcpuid, cr0mask);
1010 	}
1011 
1012 	if (!error && (get_cr0_shadow || get_all)) {
1013 		uint64_t cr0shadow;
1014 		error = vm_get_vmcs_field(vcpu, VMCS_CR0_SHADOW,
1015 					  &cr0shadow);
1016 		if (error == 0)
1017 			printf("cr0_shadow[%d]\t\t0x%016lx\n", vcpuid, cr0shadow);
1018 	}
1019 
1020 	if (!error && (get_cr4_mask || get_all)) {
1021 		uint64_t cr4mask;
1022 		error = vm_get_vmcs_field(vcpu, VMCS_CR4_MASK, &cr4mask);
1023 		if (error == 0)
1024 			printf("cr4_mask[%d]\t\t0x%016lx\n", vcpuid, cr4mask);
1025 	}
1026 
1027 	if (!error && (get_cr4_shadow || get_all)) {
1028 		uint64_t cr4shadow;
1029 		error = vm_get_vmcs_field(vcpu, VMCS_CR4_SHADOW,
1030 					  &cr4shadow);
1031 		if (error == 0)
1032 			printf("cr4_shadow[%d]\t\t0x%016lx\n", vcpuid, cr4shadow);
1033 	}
1034 
1035 	if (!error && (get_cr3_targets || get_all)) {
1036 		uint64_t target_count, target_addr;
1037 		error = vm_get_vmcs_field(vcpu, VMCS_CR3_TARGET_COUNT,
1038 					  &target_count);
1039 		if (error == 0) {
1040 			printf("cr3_target_count[%d]\t0x%016lx\n",
1041 				vcpuid, target_count);
1042 		}
1043 
1044 		error = vm_get_vmcs_field(vcpu, VMCS_CR3_TARGET0,
1045 					  &target_addr);
1046 		if (error == 0) {
1047 			printf("cr3_target0[%d]\t\t0x%016lx\n",
1048 				vcpuid, target_addr);
1049 		}
1050 
1051 		error = vm_get_vmcs_field(vcpu, VMCS_CR3_TARGET1,
1052 					  &target_addr);
1053 		if (error == 0) {
1054 			printf("cr3_target1[%d]\t\t0x%016lx\n",
1055 				vcpuid, target_addr);
1056 		}
1057 
1058 		error = vm_get_vmcs_field(vcpu, VMCS_CR3_TARGET2,
1059 					  &target_addr);
1060 		if (error == 0) {
1061 			printf("cr3_target2[%d]\t\t0x%016lx\n",
1062 				vcpuid, target_addr);
1063 		}
1064 
1065 		error = vm_get_vmcs_field(vcpu, VMCS_CR3_TARGET3,
1066 					  &target_addr);
1067 		if (error == 0) {
1068 			printf("cr3_target3[%d]\t\t0x%016lx\n",
1069 				vcpuid, target_addr);
1070 		}
1071 	}
1072 
1073 	if (!error && (get_pinbased_ctls || get_all)) {
1074 		error = vm_get_vmcs_field(vcpu, VMCS_PIN_BASED_CTLS, &ctl);
1075 		if (error == 0)
1076 			printf("pinbased_ctls[%d]\t0x%016lx\n", vcpuid, ctl);
1077 	}
1078 
1079 	if (!error && (get_procbased_ctls || get_all)) {
1080 		error = vm_get_vmcs_field(vcpu,
1081 					  VMCS_PRI_PROC_BASED_CTLS, &ctl);
1082 		if (error == 0)
1083 			printf("procbased_ctls[%d]\t0x%016lx\n", vcpuid, ctl);
1084 	}
1085 
1086 	if (!error && (get_procbased_ctls2 || get_all)) {
1087 		error = vm_get_vmcs_field(vcpu,
1088 					  VMCS_SEC_PROC_BASED_CTLS, &ctl);
1089 		if (error == 0)
1090 			printf("procbased_ctls2[%d]\t0x%016lx\n", vcpuid, ctl);
1091 	}
1092 
1093 	if (!error && (get_vmcs_gla || get_all)) {
1094 		error = vm_get_vmcs_field(vcpu,
1095 					  VMCS_GUEST_LINEAR_ADDRESS, &u64);
1096 		if (error == 0)
1097 			printf("gla[%d]\t\t0x%016lx\n", vcpuid, u64);
1098 	}
1099 
1100 	if (!error && (get_vmcs_gpa || get_all)) {
1101 		error = vm_get_vmcs_field(vcpu,
1102 					  VMCS_GUEST_PHYSICAL_ADDRESS, &u64);
1103 		if (error == 0)
1104 			printf("gpa[%d]\t\t0x%016lx\n", vcpuid, u64);
1105 	}
1106 
1107 	if (!error && (get_vmcs_entry_interruption_info ||
1108 		get_all)) {
1109 		error = vm_get_vmcs_field(vcpu, VMCS_ENTRY_INTR_INFO,&u64);
1110 		if (error == 0) {
1111 			printf("entry_interruption_info[%d]\t0x%016lx\n",
1112 				vcpuid, u64);
1113 		}
1114 	}
1115 
1116 	if (!error && (get_tpr_threshold || get_all)) {
1117 		uint64_t threshold;
1118 		error = vm_get_vmcs_field(vcpu, VMCS_TPR_THRESHOLD,
1119 					  &threshold);
1120 		if (error == 0)
1121 			printf("tpr_threshold[%d]\t0x%016lx\n", vcpuid, threshold);
1122 	}
1123 
1124 	if (!error && (get_inst_err || get_all)) {
1125 		uint64_t insterr;
1126 		error = vm_get_vmcs_field(vcpu, VMCS_INSTRUCTION_ERROR,
1127 					  &insterr);
1128 		if (error == 0) {
1129 			printf("instruction_error[%d]\t0x%016lx\n",
1130 				vcpuid, insterr);
1131 		}
1132 	}
1133 
1134 	if (!error && (get_exit_ctls || get_all)) {
1135 		error = vm_get_vmcs_field(vcpu, VMCS_EXIT_CTLS, &ctl);
1136 		if (error == 0)
1137 			printf("exit_ctls[%d]\t\t0x%016lx\n", vcpuid, ctl);
1138 	}
1139 
1140 	if (!error && (get_entry_ctls || get_all)) {
1141 		error = vm_get_vmcs_field(vcpu, VMCS_ENTRY_CTLS, &ctl);
1142 		if (error == 0)
1143 			printf("entry_ctls[%d]\t\t0x%016lx\n", vcpuid, ctl);
1144 	}
1145 
1146 	if (!error && (get_host_pat || get_all)) {
1147 		error = vm_get_vmcs_field(vcpu, VMCS_HOST_IA32_PAT, &pat);
1148 		if (error == 0)
1149 			printf("host_pat[%d]\t\t0x%016lx\n", vcpuid, pat);
1150 	}
1151 
1152 	if (!error && (get_host_cr0 || get_all)) {
1153 		error = vm_get_vmcs_field(vcpu, VMCS_HOST_CR0, &cr0);
1154 		if (error == 0)
1155 			printf("host_cr0[%d]\t\t0x%016lx\n", vcpuid, cr0);
1156 	}
1157 
1158 	if (!error && (get_host_cr3 || get_all)) {
1159 		error = vm_get_vmcs_field(vcpu, VMCS_HOST_CR3, &cr3);
1160 		if (error == 0)
1161 			printf("host_cr3[%d]\t\t0x%016lx\n", vcpuid, cr3);
1162 	}
1163 
1164 	if (!error && (get_host_cr4 || get_all)) {
1165 		error = vm_get_vmcs_field(vcpu, VMCS_HOST_CR4, &cr4);
1166 		if (error == 0)
1167 			printf("host_cr4[%d]\t\t0x%016lx\n", vcpuid, cr4);
1168 	}
1169 
1170 	if (!error && (get_host_rip || get_all)) {
1171 		error = vm_get_vmcs_field(vcpu, VMCS_HOST_RIP, &rip);
1172 		if (error == 0)
1173 			printf("host_rip[%d]\t\t0x%016lx\n", vcpuid, rip);
1174 	}
1175 
1176 	if (!error && (get_host_rsp || get_all)) {
1177 		error = vm_get_vmcs_field(vcpu, VMCS_HOST_RSP, &rsp);
1178 		if (error == 0)
1179 			printf("host_rsp[%d]\t\t0x%016lx\n", vcpuid, rsp);
1180 	}
1181 
1182 	if (!error && (get_vmcs_link || get_all)) {
1183 		error = vm_get_vmcs_field(vcpu, VMCS_LINK_POINTER, &addr);
1184 		if (error == 0)
1185 			printf("vmcs_pointer[%d]\t0x%016lx\n", vcpuid, addr);
1186 	}
1187 
1188 	if (!error && (get_vmcs_exit_interruption_info || get_all)) {
1189 		error = vm_get_vmcs_field(vcpu, VMCS_EXIT_INTR_INFO, &u64);
1190 		if (error == 0) {
1191 			printf("vmcs_exit_interruption_info[%d]\t0x%016lx\n",
1192 				vcpuid, u64);
1193 		}
1194 	}
1195 
1196 	if (!error && (get_vmcs_exit_interruption_error || get_all)) {
1197 		error = vm_get_vmcs_field(vcpu, VMCS_EXIT_INTR_ERRCODE,
1198 		    			  &u64);
1199 		if (error == 0) {
1200 			printf("vmcs_exit_interruption_error[%d]\t0x%016lx\n",
1201 				vcpuid, u64);
1202 		}
1203 	}
1204 
1205 	if (!error && (get_vmcs_interruptibility || get_all)) {
1206 		error = vm_get_vmcs_field(vcpu,
1207 					  VMCS_GUEST_INTERRUPTIBILITY, &u64);
1208 		if (error == 0) {
1209 			printf("vmcs_guest_interruptibility[%d]\t0x%016lx\n",
1210 				vcpuid, u64);
1211 		}
1212 	}
1213 
1214 	if (!error && (get_vmcs_exit_inst_length || get_all)) {
1215 		error = vm_get_vmcs_field(vcpu,
1216 		    VMCS_EXIT_INSTRUCTION_LENGTH, &u64);
1217 		if (error == 0)
1218 			printf("vmcs_exit_inst_length[%d]\t0x%08x\n", vcpuid,
1219 			    (uint32_t)u64);
1220 	}
1221 
1222 	if (!error && (get_vmcs_exit_qualification || get_all)) {
1223 		error = vm_get_vmcs_field(vcpu, VMCS_EXIT_QUALIFICATION,
1224 					  &u64);
1225 		if (error == 0)
1226 			printf("vmcs_exit_qualification[%d]\t0x%016lx\n",
1227 				vcpuid, u64);
1228 	}
1229 
1230 	return (error);
1231 }
1232 
1233 static int
1234 get_misc_vmcb(struct vcpu *vcpu, int vcpuid)
1235 {
1236 	uint64_t ctl, addr;
1237 	int error = 0;
1238 
1239 	if (!error && (get_vmcb_intercept || get_all)) {
1240 		error = vm_get_vmcb_field(vcpu, VMCB_OFF_CR_INTERCEPT, 4,
1241 		    &ctl);
1242 		if (error == 0)
1243 			printf("cr_intercept[%d]\t0x%08x\n", vcpuid, (int)ctl);
1244 
1245 		error = vm_get_vmcb_field(vcpu, VMCB_OFF_DR_INTERCEPT, 4,
1246 		    &ctl);
1247 		if (error == 0)
1248 			printf("dr_intercept[%d]\t0x%08x\n", vcpuid, (int)ctl);
1249 
1250 		error = vm_get_vmcb_field(vcpu, VMCB_OFF_EXC_INTERCEPT, 4,
1251 		    &ctl);
1252 		if (error == 0)
1253 			printf("exc_intercept[%d]\t0x%08x\n", vcpuid, (int)ctl);
1254 
1255 		error = vm_get_vmcb_field(vcpu, VMCB_OFF_INST1_INTERCEPT,
1256 		    4, &ctl);
1257 		if (error == 0)
1258 			printf("inst1_intercept[%d]\t0x%08x\n", vcpuid, (int)ctl);
1259 
1260 		error = vm_get_vmcb_field(vcpu, VMCB_OFF_INST2_INTERCEPT,
1261 		    4, &ctl);
1262 		if (error == 0)
1263 			printf("inst2_intercept[%d]\t0x%08x\n", vcpuid, (int)ctl);
1264 	}
1265 
1266 	if (!error && (get_vmcb_tlb_ctrl || get_all)) {
1267 		error = vm_get_vmcb_field(vcpu, VMCB_OFF_TLB_CTRL,
1268 					  4, &ctl);
1269 		if (error == 0)
1270 			printf("TLB ctrl[%d]\t0x%016lx\n", vcpuid, ctl);
1271 	}
1272 
1273 	if (!error && (get_vmcb_exit_details || get_all)) {
1274 		error = vm_get_vmcb_field(vcpu, VMCB_OFF_EXITINFO1,
1275 					  8, &ctl);
1276 		if (error == 0)
1277 			printf("exitinfo1[%d]\t0x%016lx\n", vcpuid, ctl);
1278 		error = vm_get_vmcb_field(vcpu, VMCB_OFF_EXITINFO2,
1279 					  8, &ctl);
1280 		if (error == 0)
1281 			printf("exitinfo2[%d]\t0x%016lx\n", vcpuid, ctl);
1282 		error = vm_get_vmcb_field(vcpu, VMCB_OFF_EXITINTINFO,
1283 					  8, &ctl);
1284 		if (error == 0)
1285 			printf("exitintinfo[%d]\t0x%016lx\n", vcpuid, ctl);
1286 	}
1287 
1288 	if (!error && (get_vmcb_virq || get_all)) {
1289 		error = vm_get_vmcb_field(vcpu, VMCB_OFF_VIRQ,
1290 					  8, &ctl);
1291 		if (error == 0)
1292 			printf("v_irq/tpr[%d]\t0x%016lx\n", vcpuid, ctl);
1293 	}
1294 
1295 	if (!error && (get_apic_access_addr || get_all)) {
1296 		error = vm_get_vmcb_field(vcpu, VMCB_OFF_AVIC_BAR, 8,
1297 					  &addr);
1298 		if (error == 0)
1299 			printf("AVIC apic_bar[%d]\t0x%016lx\n", vcpuid, addr);
1300 	}
1301 
1302 	if (!error && (get_virtual_apic_addr || get_all)) {
1303 		error = vm_get_vmcb_field(vcpu, VMCB_OFF_AVIC_PAGE, 8,
1304 					  &addr);
1305 		if (error == 0)
1306 			printf("AVIC backing page[%d]\t0x%016lx\n", vcpuid, addr);
1307 	}
1308 
1309 	if (!error && (get_avic_table || get_all)) {
1310 		error = vm_get_vmcb_field(vcpu, VMCB_OFF_AVIC_LT, 8,
1311 					  &addr);
1312 		if (error == 0)
1313 			printf("AVIC logical table[%d]\t0x%016lx\n",
1314 				vcpuid, addr);
1315 		error = vm_get_vmcb_field(vcpu, VMCB_OFF_AVIC_PT, 8,
1316 					  &addr);
1317 		if (error == 0)
1318 			printf("AVIC physical table[%d]\t0x%016lx\n",
1319 				vcpuid, addr);
1320 	}
1321 
1322 	return (error);
1323 }
1324 
1325 static struct option *
1326 setup_options(bool cpu_intel)
1327 {
1328 	const struct option common_opts[] = {
1329 		{ "vm",		REQ_ARG,	0,	VMNAME },
1330 		{ "cpu",	REQ_ARG,	0,	VCPU },
1331 		{ "set-mem",	REQ_ARG,	0,	SET_MEM },
1332 		{ "set-efer",	REQ_ARG,	0,	SET_EFER },
1333 		{ "set-cr0",	REQ_ARG,	0,	SET_CR0 },
1334 		{ "set-cr2",	REQ_ARG,	0,	SET_CR2 },
1335 		{ "set-cr3",	REQ_ARG,	0,	SET_CR3 },
1336 		{ "set-cr4",	REQ_ARG,	0,	SET_CR4 },
1337 		{ "set-dr0",	REQ_ARG,	0,	SET_DR0 },
1338 		{ "set-dr1",	REQ_ARG,	0,	SET_DR1 },
1339 		{ "set-dr2",	REQ_ARG,	0,	SET_DR2 },
1340 		{ "set-dr3",	REQ_ARG,	0,	SET_DR3 },
1341 		{ "set-dr6",	REQ_ARG,	0,	SET_DR6 },
1342 		{ "set-dr7",	REQ_ARG,	0,	SET_DR7 },
1343 		{ "set-rsp",	REQ_ARG,	0,	SET_RSP },
1344 		{ "set-rip",	REQ_ARG,	0,	SET_RIP },
1345 		{ "set-rax",	REQ_ARG,	0,	SET_RAX },
1346 		{ "set-rflags",	REQ_ARG,	0,	SET_RFLAGS },
1347 		{ "desc-base",	REQ_ARG,	0,	DESC_BASE },
1348 		{ "desc-limit",	REQ_ARG,	0,	DESC_LIMIT },
1349 		{ "desc-access",REQ_ARG,	0,	DESC_ACCESS },
1350 		{ "set-cs",	REQ_ARG,	0,	SET_CS },
1351 		{ "set-ds",	REQ_ARG,	0,	SET_DS },
1352 		{ "set-es",	REQ_ARG,	0,	SET_ES },
1353 		{ "set-fs",	REQ_ARG,	0,	SET_FS },
1354 		{ "set-gs",	REQ_ARG,	0,	SET_GS },
1355 		{ "set-ss",	REQ_ARG,	0,	SET_SS },
1356 		{ "set-tr",	REQ_ARG,	0,	SET_TR },
1357 		{ "set-ldtr",	REQ_ARG,	0,	SET_LDTR },
1358 		{ "set-x2apic-state",REQ_ARG,	0,	SET_X2APIC_STATE },
1359 		{ "capname",	REQ_ARG,	0,	CAPNAME },
1360 		{ "unassign-pptdev", REQ_ARG,	0,	UNASSIGN_PPTDEV },
1361 		{ "setcap",	REQ_ARG,	0,	SET_CAP },
1362 		{ "get-gpa-pmap", REQ_ARG,	0,	GET_GPA_PMAP },
1363 		{ "assert-lapic-lvt", REQ_ARG,	0,	ASSERT_LAPIC_LVT },
1364 		{ "get-rtc-time", NO_ARG,	&get_rtc_time,	1 },
1365 		{ "set-rtc-time", REQ_ARG,	0,	SET_RTC_TIME },
1366 		{ "rtc-nvram-offset", REQ_ARG,	0,	RTC_NVRAM_OFFSET },
1367 		{ "get-rtc-nvram", NO_ARG,	&get_rtc_nvram,	1 },
1368 		{ "set-rtc-nvram", REQ_ARG,	0,	SET_RTC_NVRAM },
1369 		{ "getcap",	NO_ARG,		&getcap,	1 },
1370 		{ "get-stats",	NO_ARG,		&get_stats,	1 },
1371 		{ "get-desc-ds",NO_ARG,		&get_desc_ds,	1 },
1372 		{ "set-desc-ds",NO_ARG,		&set_desc_ds,	1 },
1373 		{ "get-desc-es",NO_ARG,		&get_desc_es,	1 },
1374 		{ "set-desc-es",NO_ARG,		&set_desc_es,	1 },
1375 		{ "get-desc-ss",NO_ARG,		&get_desc_ss,	1 },
1376 		{ "set-desc-ss",NO_ARG,		&set_desc_ss,	1 },
1377 		{ "get-desc-cs",NO_ARG,		&get_desc_cs,	1 },
1378 		{ "set-desc-cs",NO_ARG,		&set_desc_cs,	1 },
1379 		{ "get-desc-fs",NO_ARG,		&get_desc_fs,	1 },
1380 		{ "set-desc-fs",NO_ARG,		&set_desc_fs,	1 },
1381 		{ "get-desc-gs",NO_ARG,		&get_desc_gs,	1 },
1382 		{ "set-desc-gs",NO_ARG,		&set_desc_gs,	1 },
1383 		{ "get-desc-tr",NO_ARG,		&get_desc_tr,	1 },
1384 		{ "set-desc-tr",NO_ARG,		&set_desc_tr,	1 },
1385 		{ "set-desc-ldtr", NO_ARG,	&set_desc_ldtr,	1 },
1386 		{ "get-desc-ldtr", NO_ARG,	&get_desc_ldtr,	1 },
1387 		{ "set-desc-gdtr", NO_ARG,	&set_desc_gdtr, 1 },
1388 		{ "get-desc-gdtr", NO_ARG,	&get_desc_gdtr, 1 },
1389 		{ "set-desc-idtr", NO_ARG,	&set_desc_idtr, 1 },
1390 		{ "get-desc-idtr", NO_ARG,	&get_desc_idtr, 1 },
1391 		{ "get-memmap",	NO_ARG,		&get_memmap,	1 },
1392 		{ "get-memseg", NO_ARG,		&get_memseg,	1 },
1393 		{ "get-efer",	NO_ARG,		&get_efer,	1 },
1394 		{ "get-cr0",	NO_ARG,		&get_cr0,	1 },
1395 		{ "get-cr2",	NO_ARG,		&get_cr2,	1 },
1396 		{ "get-cr3",	NO_ARG,		&get_cr3,	1 },
1397 		{ "get-cr4",	NO_ARG,		&get_cr4,	1 },
1398 		{ "get-dr0",	NO_ARG,		&get_dr0,	1 },
1399 		{ "get-dr1",	NO_ARG,		&get_dr1,	1 },
1400 		{ "get-dr2",	NO_ARG,		&get_dr2,	1 },
1401 		{ "get-dr3",	NO_ARG,		&get_dr3,	1 },
1402 		{ "get-dr6",	NO_ARG,		&get_dr6,	1 },
1403 		{ "get-dr7",	NO_ARG,		&get_dr7,	1 },
1404 		{ "get-rsp",	NO_ARG,		&get_rsp,	1 },
1405 		{ "get-rip",	NO_ARG,		&get_rip,	1 },
1406 		{ "get-rax",	NO_ARG,		&get_rax,	1 },
1407 		{ "get-rbx",	NO_ARG,		&get_rbx,	1 },
1408 		{ "get-rcx",	NO_ARG,		&get_rcx,	1 },
1409 		{ "get-rdx",	NO_ARG,		&get_rdx,	1 },
1410 		{ "get-rsi",	NO_ARG,		&get_rsi,	1 },
1411 		{ "get-rdi",	NO_ARG,		&get_rdi,	1 },
1412 		{ "get-rbp",	NO_ARG,		&get_rbp,	1 },
1413 		{ "get-r8",	NO_ARG,		&get_r8,	1 },
1414 		{ "get-r9",	NO_ARG,		&get_r9,	1 },
1415 		{ "get-r10",	NO_ARG,		&get_r10,	1 },
1416 		{ "get-r11",	NO_ARG,		&get_r11,	1 },
1417 		{ "get-r12",	NO_ARG,		&get_r12,	1 },
1418 		{ "get-r13",	NO_ARG,		&get_r13,	1 },
1419 		{ "get-r14",	NO_ARG,		&get_r14,	1 },
1420 		{ "get-r15",	NO_ARG,		&get_r15,	1 },
1421 		{ "get-rflags",	NO_ARG,		&get_rflags,	1 },
1422 		{ "get-cs",	NO_ARG,		&get_cs,	1 },
1423 		{ "get-ds",	NO_ARG,		&get_ds,	1 },
1424 		{ "get-es",	NO_ARG,		&get_es,	1 },
1425 		{ "get-fs",	NO_ARG,		&get_fs,	1 },
1426 		{ "get-gs",	NO_ARG,		&get_gs,	1 },
1427 		{ "get-ss",	NO_ARG,		&get_ss,	1 },
1428 		{ "get-tr",	NO_ARG,		&get_tr,	1 },
1429 		{ "get-ldtr",	NO_ARG,		&get_ldtr,	1 },
1430 		{ "get-eptp", 	NO_ARG,		&get_eptp,	1 },
1431 		{ "get-exception-bitmap",
1432 					NO_ARG,	&get_exception_bitmap,  1 },
1433 		{ "get-io-bitmap-address",
1434 					NO_ARG,	&get_io_bitmap,		1 },
1435 		{ "get-tsc-offset", 	NO_ARG, &get_tsc_offset, 	1 },
1436 		{ "get-msr-bitmap",
1437 					NO_ARG,	&get_msr_bitmap, 	1 },
1438 		{ "get-msr-bitmap-address",
1439 					NO_ARG,	&get_msr_bitmap_address, 1 },
1440 		{ "get-guest-pat",	NO_ARG,	&get_guest_pat,		1 },
1441 		{ "get-guest-sysenter",
1442 					NO_ARG,	&get_guest_sysenter, 	1 },
1443 		{ "get-exit-reason",
1444 					NO_ARG,	&get_exit_reason, 	1 },
1445 		{ "get-x2apic-state",	NO_ARG,	&get_x2apic_state, 	1 },
1446 		{ "get-all",		NO_ARG,	&get_all,		1 },
1447 		{ "run",		NO_ARG,	&run,			1 },
1448 		{ "create",		NO_ARG,	&create,		1 },
1449 		{ "destroy",		NO_ARG,	&destroy,		1 },
1450 		{ "inject-nmi",		NO_ARG,	&inject_nmi,		1 },
1451 		{ "force-reset",	NO_ARG,	&force_reset,		1 },
1452 		{ "force-poweroff", 	NO_ARG,	&force_poweroff, 	1 },
1453 		{ "get-active-cpus", 	NO_ARG,	&get_active_cpus, 	1 },
1454 		{ "get-debug-cpus",	NO_ARG,	&get_debug_cpus,	1 },
1455 		{ "get-suspended-cpus", NO_ARG,	&get_suspended_cpus, 	1 },
1456 		{ "get-intinfo", 	NO_ARG,	&get_intinfo,		1 },
1457 		{ "get-cpu-topology",	NO_ARG, &get_cpu_topology,	1 },
1458 #ifdef BHYVE_SNAPSHOT
1459 		{ "checkpoint", 	REQ_ARG, 0,	SET_CHECKPOINT_FILE},
1460 		{ "suspend", 		REQ_ARG, 0,	SET_SUSPEND_FILE},
1461 #endif
1462 	};
1463 
1464 	const struct option intel_opts[] = {
1465 		{ "get-vmcs-pinbased-ctls",
1466 				NO_ARG,		&get_pinbased_ctls, 1 },
1467 		{ "get-vmcs-procbased-ctls",
1468 				NO_ARG,		&get_procbased_ctls, 1 },
1469 		{ "get-vmcs-procbased-ctls2",
1470 				NO_ARG,		&get_procbased_ctls2, 1 },
1471 		{ "get-vmcs-guest-linear-address",
1472 				NO_ARG,		&get_vmcs_gla,	1 },
1473 		{ "get-vmcs-guest-physical-address",
1474 				NO_ARG,		&get_vmcs_gpa,	1 },
1475 		{ "get-vmcs-entry-interruption-info",
1476 				NO_ARG, &get_vmcs_entry_interruption_info, 1},
1477 		{ "get-vmcs-cr0-mask", NO_ARG,	&get_cr0_mask,	1 },
1478 		{ "get-vmcs-cr0-shadow", NO_ARG,&get_cr0_shadow, 1 },
1479 		{ "get-vmcs-cr4-mask", 		NO_ARG,	&get_cr4_mask,	  1 },
1480 		{ "get-vmcs-cr4-shadow", 	NO_ARG, &get_cr4_shadow,  1 },
1481 		{ "get-vmcs-cr3-targets", 	NO_ARG, &get_cr3_targets, 1 },
1482 		{ "get-vmcs-tpr-threshold",
1483 					NO_ARG,	&get_tpr_threshold, 1 },
1484 		{ "get-vmcs-vpid", 	NO_ARG,	&get_vpid_asid,	    1 },
1485 		{ "get-vmcs-exit-ctls", NO_ARG,	&get_exit_ctls,	    1 },
1486 		{ "get-vmcs-entry-ctls",
1487 					NO_ARG,	&get_entry_ctls, 1 },
1488 		{ "get-vmcs-instruction-error",
1489 					NO_ARG,	&get_inst_err,	1 },
1490 		{ "get-vmcs-host-pat",	NO_ARG,	&get_host_pat,	1 },
1491 		{ "get-vmcs-host-cr0",
1492 					NO_ARG,	&get_host_cr0,	1 },
1493 		{ "get-vmcs-exit-qualification",
1494 				NO_ARG,	&get_vmcs_exit_qualification, 1 },
1495 		{ "get-vmcs-exit-inst-length",
1496 				NO_ARG,	&get_vmcs_exit_inst_length, 1 },
1497 		{ "get-vmcs-interruptibility",
1498 				NO_ARG, &get_vmcs_interruptibility, 1 },
1499 		{ "get-vmcs-exit-interruption-error",
1500 				NO_ARG,	&get_vmcs_exit_interruption_error, 1 },
1501 		{ "get-vmcs-exit-interruption-info",
1502 				NO_ARG,	&get_vmcs_exit_interruption_info, 1 },
1503 		{ "get-vmcs-link", 	NO_ARG,		&get_vmcs_link, 1 },
1504 		{ "get-vmcs-host-cr3",
1505 					NO_ARG,		&get_host_cr3,	1 },
1506 		{ "get-vmcs-host-cr4",
1507 				NO_ARG,		&get_host_cr4,	1 },
1508 		{ "get-vmcs-host-rip",
1509 				NO_ARG,		&get_host_rip,	1 },
1510 		{ "get-vmcs-host-rsp",
1511 				NO_ARG,		&get_host_rsp,	1 },
1512 		{ "get-apic-access-address",
1513 				NO_ARG,		&get_apic_access_addr, 1},
1514 		{ "get-virtual-apic-address",
1515 				NO_ARG,		&get_virtual_apic_addr, 1}
1516 	};
1517 
1518 	const struct option amd_opts[] = {
1519 		{ "get-vmcb-intercepts",
1520 				NO_ARG,	&get_vmcb_intercept, 	1 },
1521 		{ "get-vmcb-asid",
1522 				NO_ARG,	&get_vpid_asid,	     	1 },
1523 		{ "get-vmcb-exit-details",
1524 				NO_ARG, &get_vmcb_exit_details,	1 },
1525 		{ "get-vmcb-tlb-ctrl",
1526 				NO_ARG, &get_vmcb_tlb_ctrl, 	1 },
1527 		{ "get-vmcb-virq",
1528 				NO_ARG, &get_vmcb_virq, 	1 },
1529 		{ "get-avic-apic-bar",
1530 				NO_ARG,	&get_apic_access_addr, 	1 },
1531 		{ "get-avic-backing-page",
1532 				NO_ARG,	&get_virtual_apic_addr, 1 },
1533 		{ "get-avic-table",
1534 				NO_ARG,	&get_avic_table, 	1 }
1535 	};
1536 
1537 	const struct option null_opt = {
1538 		NULL, 0, NULL, 0
1539 	};
1540 
1541 	struct option *all_opts;
1542 	char *cp;
1543 	int optlen;
1544 
1545 	optlen = sizeof(common_opts);
1546 
1547 	if (cpu_intel)
1548 		optlen += sizeof(intel_opts);
1549 	else
1550 		optlen += sizeof(amd_opts);
1551 
1552 	optlen += sizeof(null_opt);
1553 
1554 	all_opts = malloc(optlen);
1555 
1556 	cp = (char *)all_opts;
1557 	memcpy(cp, common_opts, sizeof(common_opts));
1558 	cp += sizeof(common_opts);
1559 
1560 	if (cpu_intel) {
1561 		memcpy(cp, intel_opts, sizeof(intel_opts));
1562 		cp += sizeof(intel_opts);
1563 	} else {
1564 		memcpy(cp, amd_opts, sizeof(amd_opts));
1565 		cp += sizeof(amd_opts);
1566 	}
1567 
1568 	memcpy(cp, &null_opt, sizeof(null_opt));
1569 	cp += sizeof(null_opt);
1570 
1571 	return (all_opts);
1572 }
1573 
1574 static const char *
1575 wday_str(int idx)
1576 {
1577 	static const char *weekdays[] = {
1578 		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
1579 	};
1580 
1581 	if (idx >= 0 && idx < 7)
1582 		return (weekdays[idx]);
1583 	else
1584 		return ("UNK");
1585 }
1586 
1587 static const char *
1588 mon_str(int idx)
1589 {
1590 	static const char *months[] = {
1591 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
1592 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
1593 	};
1594 
1595 	if (idx >= 0 && idx < 12)
1596 		return (months[idx]);
1597 	else
1598 		return ("UNK");
1599 }
1600 
1601 static int
1602 show_memmap(struct vmctx *ctx)
1603 {
1604 	char name[SPECNAMELEN + 1], numbuf[8];
1605 	vm_ooffset_t segoff;
1606 	vm_paddr_t gpa;
1607 	size_t maplen, seglen;
1608 	int error, flags, prot, segid, delim;
1609 
1610 	printf("Address     Length      Segment     Offset      ");
1611 	printf("Prot  Flags\n");
1612 
1613 	gpa = 0;
1614 	while (1) {
1615 		error = vm_mmap_getnext(ctx, &gpa, &segid, &segoff, &maplen,
1616 		    &prot, &flags);
1617 		if (error)
1618 			return (errno == ENOENT ? 0 : error);
1619 
1620 		error = vm_get_memseg(ctx, segid, &seglen, name, sizeof(name));
1621 		if (error)
1622 			return (error);
1623 
1624 		printf("%-12lX", gpa);
1625 		humanize_number(numbuf, sizeof(numbuf), maplen, "B",
1626 		    HN_AUTOSCALE, HN_NOSPACE);
1627 		printf("%-12s", numbuf);
1628 
1629 		printf("%-12s", name[0] ? name : "sysmem");
1630 		printf("%-12lX", segoff);
1631 		printf("%c%c%c   ", prot & PROT_READ ? 'R' : '-',
1632 		    prot & PROT_WRITE ? 'W' : '-',
1633 		    prot & PROT_EXEC ? 'X' : '-');
1634 
1635 		delim = '\0';
1636 		if (flags & VM_MEMMAP_F_WIRED) {
1637 			printf("%cwired", delim);
1638 			delim = '/';
1639 		}
1640 		if (flags & VM_MEMMAP_F_IOMMU) {
1641 			printf("%ciommu", delim);
1642 			delim = '/';
1643 		}
1644 		printf("\n");
1645 
1646 		gpa += maplen;
1647 	}
1648 }
1649 
1650 static int
1651 show_memseg(struct vmctx *ctx)
1652 {
1653 	char name[SPECNAMELEN + 1], numbuf[8];
1654 	size_t seglen;
1655 	int error, segid;
1656 
1657 	printf("ID  Length      Name\n");
1658 
1659 	segid = 0;
1660 	while (1) {
1661 		error = vm_get_memseg(ctx, segid, &seglen, name, sizeof(name));
1662 		if (error)
1663 			return (errno == EINVAL ? 0 : error);
1664 
1665 		if (seglen) {
1666 			printf("%-4d", segid);
1667 			humanize_number(numbuf, sizeof(numbuf), seglen, "B",
1668 			    HN_AUTOSCALE, HN_NOSPACE);
1669 			printf("%-12s", numbuf);
1670 			printf("%s", name[0] ? name : "sysmem");
1671 			printf("\n");
1672 		}
1673 		segid++;
1674 	}
1675 }
1676 
1677 #ifdef BHYVE_SNAPSHOT
1678 static int
1679 send_message(const char *vmname, nvlist_t *nvl)
1680 {
1681 	struct sockaddr_un addr;
1682 	int err = 0, socket_fd;
1683 
1684 	socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
1685 	if (socket_fd < 0) {
1686 		perror("Error creating bhyvectl socket");
1687 		err = errno;
1688 		goto done;
1689 	}
1690 
1691 	memset(&addr, 0, sizeof(struct sockaddr_un));
1692 	snprintf(addr.sun_path, sizeof(addr.sun_path), "%s%s",
1693 	    BHYVE_RUN_DIR, vmname);
1694 	addr.sun_family = AF_UNIX;
1695 	addr.sun_len = SUN_LEN(&addr);
1696 
1697 	if (connect(socket_fd, (struct sockaddr *)&addr, addr.sun_len) != 0) {
1698 		perror("connect() failed");
1699 		err = errno;
1700 		goto done;
1701 	}
1702 
1703 	if (nvlist_send(socket_fd, nvl) < 0) {
1704 		perror("nvlist_send() failed");
1705 		err = errno;
1706 	}
1707 done:
1708 	nvlist_destroy(nvl);
1709 
1710 	if (socket_fd >= 0)
1711 		close(socket_fd);
1712 	return (err);
1713 }
1714 
1715 static int
1716 open_directory(const char *file)
1717 {
1718 	char *path;
1719 	int fd;
1720 
1721 	if ((path = strdup(file)) == NULL)
1722 		return (-1);
1723 
1724 	dirname(path);
1725 	fd = open(path, O_DIRECTORY);
1726 	free(path);
1727 
1728 	return (fd);
1729 }
1730 
1731 static int
1732 snapshot_request(const char *vmname, char *file, bool suspend)
1733 {
1734 	nvlist_t *nvl;
1735 	int fd;
1736 
1737 	if ((fd = open_directory(file)) < 0)
1738 		return (errno);
1739 
1740 	nvl = nvlist_create(0);
1741 	nvlist_add_string(nvl, "cmd", "checkpoint");
1742 	nvlist_add_string(nvl, "filename", basename(file));
1743 	nvlist_add_bool(nvl, "suspend", suspend);
1744 	nvlist_move_descriptor(nvl, "fddir", fd);
1745 
1746 	return (send_message(vmname, nvl));
1747 }
1748 #endif
1749 
1750 int
1751 main(int argc, char *argv[])
1752 {
1753 	char *vmname;
1754 	int error, ch, vcpuid, ptenum;
1755 	vm_paddr_t gpa_pmap;
1756 	struct vm_run vmrun;
1757 	uint64_t rax, cr0, cr2, cr3, cr4, dr0, dr1, dr2, dr3, dr6, dr7;
1758 	uint64_t rsp, rip, rflags, efer, pat;
1759 	uint64_t eptp, bm, addr, u64, pteval[4], *pte, info[2];
1760 	struct vmctx *ctx;
1761 	struct vcpu *vcpu;
1762 	cpuset_t cpus;
1763 	bool cpu_intel;
1764 	uint64_t cs, ds, es, fs, gs, ss, tr, ldtr;
1765 	struct tm tm;
1766 	struct option *opts;
1767 #ifdef BHYVE_SNAPSHOT
1768 	char *checkpoint_file = NULL;
1769 #endif
1770 
1771 	cpu_intel = cpu_vendor_intel();
1772 	opts = setup_options(cpu_intel);
1773 
1774 	vcpuid = 0;
1775 	vmname = NULL;
1776 	assert_lapic_lvt = -1;
1777 	progname = basename(argv[0]);
1778 
1779 	while ((ch = getopt_long(argc, argv, "", opts, NULL)) != -1) {
1780 		switch (ch) {
1781 		case 0:
1782 			break;
1783 		case VMNAME:
1784 			vmname = optarg;
1785 			break;
1786 		case VCPU:
1787 			vcpuid = atoi(optarg);
1788 			break;
1789 		case SET_MEM:
1790 			memsize = atoi(optarg) * MB;
1791 			memsize = roundup(memsize, 2 * MB);
1792 			break;
1793 		case SET_EFER:
1794 			efer = strtoul(optarg, NULL, 0);
1795 			set_efer = 1;
1796 			break;
1797 		case SET_CR0:
1798 			cr0 = strtoul(optarg, NULL, 0);
1799 			set_cr0 = 1;
1800 			break;
1801 		case SET_CR2:
1802 			cr2 = strtoul(optarg, NULL, 0);
1803 			set_cr2 = 1;
1804 			break;
1805 		case SET_CR3:
1806 			cr3 = strtoul(optarg, NULL, 0);
1807 			set_cr3 = 1;
1808 			break;
1809 		case SET_CR4:
1810 			cr4 = strtoul(optarg, NULL, 0);
1811 			set_cr4 = 1;
1812 			break;
1813 		case SET_DR0:
1814 			dr0 = strtoul(optarg, NULL, 0);
1815 			set_dr0 = 1;
1816 			break;
1817 		case SET_DR1:
1818 			dr1 = strtoul(optarg, NULL, 0);
1819 			set_dr1 = 1;
1820 			break;
1821 		case SET_DR2:
1822 			dr2 = strtoul(optarg, NULL, 0);
1823 			set_dr2 = 1;
1824 			break;
1825 		case SET_DR3:
1826 			dr3 = strtoul(optarg, NULL, 0);
1827 			set_dr3 = 1;
1828 			break;
1829 		case SET_DR6:
1830 			dr6 = strtoul(optarg, NULL, 0);
1831 			set_dr6 = 1;
1832 			break;
1833 		case SET_DR7:
1834 			dr7 = strtoul(optarg, NULL, 0);
1835 			set_dr7 = 1;
1836 			break;
1837 		case SET_RSP:
1838 			rsp = strtoul(optarg, NULL, 0);
1839 			set_rsp = 1;
1840 			break;
1841 		case SET_RIP:
1842 			rip = strtoul(optarg, NULL, 0);
1843 			set_rip = 1;
1844 			break;
1845 		case SET_RAX:
1846 			rax = strtoul(optarg, NULL, 0);
1847 			set_rax = 1;
1848 			break;
1849 		case SET_RFLAGS:
1850 			rflags = strtoul(optarg, NULL, 0);
1851 			set_rflags = 1;
1852 			break;
1853 		case DESC_BASE:
1854 			desc_base = strtoul(optarg, NULL, 0);
1855 			break;
1856 		case DESC_LIMIT:
1857 			desc_limit = strtoul(optarg, NULL, 0);
1858 			break;
1859 		case DESC_ACCESS:
1860 			desc_access = strtoul(optarg, NULL, 0);
1861 			break;
1862 		case SET_CS:
1863 			cs = strtoul(optarg, NULL, 0);
1864 			set_cs = 1;
1865 			break;
1866 		case SET_DS:
1867 			ds = strtoul(optarg, NULL, 0);
1868 			set_ds = 1;
1869 			break;
1870 		case SET_ES:
1871 			es = strtoul(optarg, NULL, 0);
1872 			set_es = 1;
1873 			break;
1874 		case SET_FS:
1875 			fs = strtoul(optarg, NULL, 0);
1876 			set_fs = 1;
1877 			break;
1878 		case SET_GS:
1879 			gs = strtoul(optarg, NULL, 0);
1880 			set_gs = 1;
1881 			break;
1882 		case SET_SS:
1883 			ss = strtoul(optarg, NULL, 0);
1884 			set_ss = 1;
1885 			break;
1886 		case SET_TR:
1887 			tr = strtoul(optarg, NULL, 0);
1888 			set_tr = 1;
1889 			break;
1890 		case SET_LDTR:
1891 			ldtr = strtoul(optarg, NULL, 0);
1892 			set_ldtr = 1;
1893 			break;
1894 		case SET_X2APIC_STATE:
1895 			x2apic_state = strtol(optarg, NULL, 0);
1896 			set_x2apic_state = 1;
1897 			break;
1898 		case SET_CAP:
1899 			capval = strtoul(optarg, NULL, 0);
1900 			setcap = 1;
1901 			break;
1902 		case SET_RTC_TIME:
1903 			rtc_secs = strtoul(optarg, NULL, 0);
1904 			set_rtc_time = 1;
1905 			break;
1906 		case SET_RTC_NVRAM:
1907 			rtc_nvram_value = (uint8_t)strtoul(optarg, NULL, 0);
1908 			set_rtc_nvram = 1;
1909 			break;
1910 		case RTC_NVRAM_OFFSET:
1911 			rtc_nvram_offset = strtoul(optarg, NULL, 0);
1912 			break;
1913 		case GET_GPA_PMAP:
1914 			gpa_pmap = strtoul(optarg, NULL, 0);
1915 			get_gpa_pmap = 1;
1916 			break;
1917 		case CAPNAME:
1918 			capname = optarg;
1919 			break;
1920 		case UNASSIGN_PPTDEV:
1921 			unassign_pptdev = 1;
1922 			if (sscanf(optarg, "%d/%d/%d", &bus, &slot, &func) != 3)
1923 				usage(cpu_intel);
1924 			break;
1925 		case ASSERT_LAPIC_LVT:
1926 			assert_lapic_lvt = atoi(optarg);
1927 			break;
1928 #ifdef BHYVE_SNAPSHOT
1929 		case SET_CHECKPOINT_FILE:
1930 		case SET_SUSPEND_FILE:
1931 			if (checkpoint_file != NULL)
1932 				usage(cpu_intel);
1933 
1934 			checkpoint_file = optarg;
1935 			vm_suspend_opt = (ch == SET_SUSPEND_FILE);
1936 			break;
1937 #endif
1938 		default:
1939 			usage(cpu_intel);
1940 		}
1941 	}
1942 	argc -= optind;
1943 	argv += optind;
1944 
1945 	if (vmname == NULL)
1946 		usage(cpu_intel);
1947 
1948 	error = 0;
1949 
1950 	if (!error && create)
1951 		error = vm_create(vmname);
1952 
1953 	if (!error) {
1954 		ctx = vm_open(vmname);
1955 		if (ctx == NULL) {
1956 			fprintf(stderr,
1957 			    "vm_open: %s could not be opened: %s\n",
1958 			    vmname, strerror(errno));
1959 			exit (1);
1960 		}
1961 		vcpu = vm_vcpu_open(ctx, vcpuid);
1962 	}
1963 
1964 	if (!error && memsize)
1965 		error = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
1966 
1967 	if (!error && set_efer)
1968 		error = vm_set_register(vcpu, VM_REG_GUEST_EFER, efer);
1969 
1970 	if (!error && set_cr0)
1971 		error = vm_set_register(vcpu, VM_REG_GUEST_CR0, cr0);
1972 
1973 	if (!error && set_cr2)
1974 		error = vm_set_register(vcpu, VM_REG_GUEST_CR2, cr2);
1975 
1976 	if (!error && set_cr3)
1977 		error = vm_set_register(vcpu, VM_REG_GUEST_CR3, cr3);
1978 
1979 	if (!error && set_cr4)
1980 		error = vm_set_register(vcpu, VM_REG_GUEST_CR4, cr4);
1981 
1982 	if (!error && set_dr0)
1983 		error = vm_set_register(vcpu, VM_REG_GUEST_DR0, dr0);
1984 
1985 	if (!error && set_dr1)
1986 		error = vm_set_register(vcpu, VM_REG_GUEST_DR1, dr1);
1987 
1988 	if (!error && set_dr2)
1989 		error = vm_set_register(vcpu, VM_REG_GUEST_DR2, dr2);
1990 
1991 	if (!error && set_dr3)
1992 		error = vm_set_register(vcpu, VM_REG_GUEST_DR3, dr3);
1993 
1994 	if (!error && set_dr6)
1995 		error = vm_set_register(vcpu, VM_REG_GUEST_DR6, dr6);
1996 
1997 	if (!error && set_dr7)
1998 		error = vm_set_register(vcpu, VM_REG_GUEST_DR7, dr7);
1999 
2000 	if (!error && set_rsp)
2001 		error = vm_set_register(vcpu, VM_REG_GUEST_RSP, rsp);
2002 
2003 	if (!error && set_rip)
2004 		error = vm_set_register(vcpu, VM_REG_GUEST_RIP, rip);
2005 
2006 	if (!error && set_rax)
2007 		error = vm_set_register(vcpu, VM_REG_GUEST_RAX, rax);
2008 
2009 	if (!error && set_rflags) {
2010 		error = vm_set_register(vcpu, VM_REG_GUEST_RFLAGS,
2011 					rflags);
2012 	}
2013 
2014 	if (!error && set_desc_ds) {
2015 		error = vm_set_desc(vcpu, VM_REG_GUEST_DS,
2016 				    desc_base, desc_limit, desc_access);
2017 	}
2018 
2019 	if (!error && set_desc_es) {
2020 		error = vm_set_desc(vcpu, VM_REG_GUEST_ES,
2021 				    desc_base, desc_limit, desc_access);
2022 	}
2023 
2024 	if (!error && set_desc_ss) {
2025 		error = vm_set_desc(vcpu, VM_REG_GUEST_SS,
2026 				    desc_base, desc_limit, desc_access);
2027 	}
2028 
2029 	if (!error && set_desc_cs) {
2030 		error = vm_set_desc(vcpu, VM_REG_GUEST_CS,
2031 				    desc_base, desc_limit, desc_access);
2032 	}
2033 
2034 	if (!error && set_desc_fs) {
2035 		error = vm_set_desc(vcpu, VM_REG_GUEST_FS,
2036 				    desc_base, desc_limit, desc_access);
2037 	}
2038 
2039 	if (!error && set_desc_gs) {
2040 		error = vm_set_desc(vcpu, VM_REG_GUEST_GS,
2041 				    desc_base, desc_limit, desc_access);
2042 	}
2043 
2044 	if (!error && set_desc_tr) {
2045 		error = vm_set_desc(vcpu, VM_REG_GUEST_TR,
2046 				    desc_base, desc_limit, desc_access);
2047 	}
2048 
2049 	if (!error && set_desc_ldtr) {
2050 		error = vm_set_desc(vcpu, VM_REG_GUEST_LDTR,
2051 				    desc_base, desc_limit, desc_access);
2052 	}
2053 
2054 	if (!error && set_desc_gdtr) {
2055 		error = vm_set_desc(vcpu, VM_REG_GUEST_GDTR,
2056 				    desc_base, desc_limit, 0);
2057 	}
2058 
2059 	if (!error && set_desc_idtr) {
2060 		error = vm_set_desc(vcpu, VM_REG_GUEST_IDTR,
2061 				    desc_base, desc_limit, 0);
2062 	}
2063 
2064 	if (!error && set_cs)
2065 		error = vm_set_register(vcpu, VM_REG_GUEST_CS, cs);
2066 
2067 	if (!error && set_ds)
2068 		error = vm_set_register(vcpu, VM_REG_GUEST_DS, ds);
2069 
2070 	if (!error && set_es)
2071 		error = vm_set_register(vcpu, VM_REG_GUEST_ES, es);
2072 
2073 	if (!error && set_fs)
2074 		error = vm_set_register(vcpu, VM_REG_GUEST_FS, fs);
2075 
2076 	if (!error && set_gs)
2077 		error = vm_set_register(vcpu, VM_REG_GUEST_GS, gs);
2078 
2079 	if (!error && set_ss)
2080 		error = vm_set_register(vcpu, VM_REG_GUEST_SS, ss);
2081 
2082 	if (!error && set_tr)
2083 		error = vm_set_register(vcpu, VM_REG_GUEST_TR, tr);
2084 
2085 	if (!error && set_ldtr)
2086 		error = vm_set_register(vcpu, VM_REG_GUEST_LDTR, ldtr);
2087 
2088 	if (!error && set_x2apic_state)
2089 		error = vm_set_x2apic_state(vcpu, x2apic_state);
2090 
2091 	if (!error && unassign_pptdev)
2092 		error = vm_unassign_pptdev(ctx, bus, slot, func);
2093 
2094 	if (!error && inject_nmi) {
2095 		error = vm_inject_nmi(vcpu);
2096 	}
2097 
2098 	if (!error && assert_lapic_lvt != -1) {
2099 		error = vm_lapic_local_irq(vcpu, assert_lapic_lvt);
2100 	}
2101 
2102 	if (!error && (get_memseg || get_all))
2103 		error = show_memseg(ctx);
2104 
2105 	if (!error && (get_memmap || get_all))
2106 		error = show_memmap(ctx);
2107 
2108 	if (!error)
2109 		error = get_all_registers(vcpu, vcpuid);
2110 
2111 	if (!error)
2112 		error = get_all_segments(vcpu, vcpuid);
2113 
2114 	if (!error) {
2115 		if (cpu_intel)
2116 			error = get_misc_vmcs(vcpu, vcpuid);
2117 		else
2118 			error = get_misc_vmcb(vcpu, vcpuid);
2119 	}
2120 
2121 	if (!error && (get_x2apic_state || get_all)) {
2122 		error = vm_get_x2apic_state(vcpu, &x2apic_state);
2123 		if (error == 0)
2124 			printf("x2apic_state[%d]\t%d\n", vcpuid, x2apic_state);
2125 	}
2126 
2127 	if (!error && (get_eptp || get_all)) {
2128 		if (cpu_intel)
2129 			error = vm_get_vmcs_field(vcpu, VMCS_EPTP, &eptp);
2130 		else
2131 			error = vm_get_vmcb_field(vcpu, VMCB_OFF_NPT_BASE,
2132 						   8, &eptp);
2133 		if (error == 0)
2134 			printf("%s[%d]\t\t0x%016lx\n",
2135 				cpu_intel ? "eptp" : "rvi/npt", vcpuid, eptp);
2136 	}
2137 
2138 	if (!error && (get_exception_bitmap || get_all)) {
2139 		if(cpu_intel)
2140 			error = vm_get_vmcs_field(vcpu,
2141 						VMCS_EXCEPTION_BITMAP, &bm);
2142 		else
2143 			error = vm_get_vmcb_field(vcpu,
2144 						  VMCB_OFF_EXC_INTERCEPT,
2145 						  4, &bm);
2146 		if (error == 0)
2147 			printf("exception_bitmap[%d]\t%#lx\n", vcpuid, bm);
2148 	}
2149 
2150 	if (!error && (get_io_bitmap || get_all)) {
2151 		if (cpu_intel) {
2152 			error = vm_get_vmcs_field(vcpu, VMCS_IO_BITMAP_A,
2153 						  &bm);
2154 			if (error == 0)
2155 				printf("io_bitmap_a[%d]\t%#lx\n", vcpuid, bm);
2156 			error = vm_get_vmcs_field(vcpu, VMCS_IO_BITMAP_B,
2157 						  &bm);
2158 			if (error == 0)
2159 				printf("io_bitmap_b[%d]\t%#lx\n", vcpuid, bm);
2160 		} else {
2161 			error = vm_get_vmcb_field(vcpu,
2162 						  VMCB_OFF_IO_PERM, 8, &bm);
2163 			if (error == 0)
2164 				printf("io_bitmap[%d]\t%#lx\n", vcpuid, bm);
2165 		}
2166 	}
2167 
2168 	if (!error && (get_tsc_offset || get_all)) {
2169 		uint64_t tscoff;
2170 		if (cpu_intel)
2171 			error = vm_get_vmcs_field(vcpu, VMCS_TSC_OFFSET,
2172 						  &tscoff);
2173 		else
2174 			error = vm_get_vmcb_field(vcpu,
2175 						  VMCB_OFF_TSC_OFFSET,
2176 						  8, &tscoff);
2177 		if (error == 0)
2178 			printf("tsc_offset[%d]\t0x%016lx\n", vcpuid, tscoff);
2179 	}
2180 
2181 	if (!error && (get_msr_bitmap_address || get_all)) {
2182 		if (cpu_intel)
2183 			error = vm_get_vmcs_field(vcpu, VMCS_MSR_BITMAP,
2184 						  &addr);
2185 		else
2186 			error = vm_get_vmcb_field(vcpu,
2187 						  VMCB_OFF_MSR_PERM, 8, &addr);
2188 		if (error == 0)
2189 			printf("msr_bitmap[%d]\t\t%#lx\n", vcpuid, addr);
2190 	}
2191 
2192 	if (!error && (get_msr_bitmap || get_all)) {
2193 		if (cpu_intel) {
2194 			error = vm_get_vmcs_field(vcpu,
2195 						  VMCS_MSR_BITMAP, &addr);
2196 		} else {
2197 			error = vm_get_vmcb_field(vcpu,
2198 						  VMCB_OFF_MSR_PERM, 8,
2199 						  &addr);
2200 		}
2201 
2202 		if (error == 0)
2203 			error = dump_msr_bitmap(vcpuid, addr, cpu_intel);
2204 	}
2205 
2206 	if (!error && (get_vpid_asid || get_all)) {
2207 		uint64_t vpid;
2208 		if (cpu_intel)
2209 			error = vm_get_vmcs_field(vcpu, VMCS_VPID, &vpid);
2210 		else
2211 			error = vm_get_vmcb_field(vcpu, VMCB_OFF_ASID,
2212 						  4, &vpid);
2213 		if (error == 0)
2214 			printf("%s[%d]\t\t0x%04lx\n",
2215 				cpu_intel ? "vpid" : "asid", vcpuid, vpid);
2216 	}
2217 
2218 	if (!error && (get_guest_pat || get_all)) {
2219 		if (cpu_intel)
2220 			error = vm_get_vmcs_field(vcpu,
2221 						  VMCS_GUEST_IA32_PAT, &pat);
2222 		else
2223 			error = vm_get_vmcb_field(vcpu,
2224 						  VMCB_OFF_GUEST_PAT, 8, &pat);
2225 		if (error == 0)
2226 			printf("guest_pat[%d]\t\t0x%016lx\n", vcpuid, pat);
2227 	}
2228 
2229 	if (!error && (get_guest_sysenter || get_all)) {
2230 		if (cpu_intel)
2231 			error = vm_get_vmcs_field(vcpu,
2232 						  VMCS_GUEST_IA32_SYSENTER_CS,
2233 						  &cs);
2234 		else
2235 			error = vm_get_vmcb_field(vcpu,
2236 						  VMCB_OFF_SYSENTER_CS, 8,
2237 						  &cs);
2238 
2239 		if (error == 0)
2240 			printf("guest_sysenter_cs[%d]\t%#lx\n", vcpuid, cs);
2241 		if (cpu_intel)
2242 			error = vm_get_vmcs_field(vcpu,
2243 						  VMCS_GUEST_IA32_SYSENTER_ESP,
2244 						  &rsp);
2245 		else
2246 			error = vm_get_vmcb_field(vcpu,
2247 						  VMCB_OFF_SYSENTER_ESP, 8,
2248 						  &rsp);
2249 
2250 		if (error == 0)
2251 			printf("guest_sysenter_sp[%d]\t%#lx\n", vcpuid, rsp);
2252 		if (cpu_intel)
2253 			error = vm_get_vmcs_field(vcpu,
2254 						  VMCS_GUEST_IA32_SYSENTER_EIP,
2255 						  &rip);
2256 		else
2257 			error = vm_get_vmcb_field(vcpu,
2258 						  VMCB_OFF_SYSENTER_EIP, 8,
2259 						  &rip);
2260 		if (error == 0)
2261 			printf("guest_sysenter_ip[%d]\t%#lx\n", vcpuid, rip);
2262 	}
2263 
2264 	if (!error && (get_exit_reason || get_all)) {
2265 		if (cpu_intel)
2266 			error = vm_get_vmcs_field(vcpu, VMCS_EXIT_REASON,
2267 						  &u64);
2268 		else
2269 			error = vm_get_vmcb_field(vcpu,
2270 						  VMCB_OFF_EXIT_REASON, 8,
2271 						  &u64);
2272 		if (error == 0)
2273 			printf("exit_reason[%d]\t%#lx\n", vcpuid, u64);
2274 	}
2275 
2276 	if (!error && setcap) {
2277 		int captype;
2278 		captype = vm_capability_name2type(capname);
2279 		error = vm_set_capability(vcpu, captype, capval);
2280 		if (error != 0 && errno == ENOENT)
2281 			printf("Capability \"%s\" is not available\n", capname);
2282 	}
2283 
2284 	if (!error && get_gpa_pmap) {
2285 		error = vm_get_gpa_pmap(ctx, gpa_pmap, pteval, &ptenum);
2286 		if (error == 0) {
2287 			printf("gpa %#lx:", gpa_pmap);
2288 			pte = &pteval[0];
2289 			while (ptenum-- > 0)
2290 				printf(" %#lx", *pte++);
2291 			printf("\n");
2292 		}
2293 	}
2294 
2295 	if (!error && set_rtc_nvram)
2296 		error = vm_rtc_write(ctx, rtc_nvram_offset, rtc_nvram_value);
2297 
2298 	if (!error && (get_rtc_nvram || get_all)) {
2299 		error = vm_rtc_read(ctx, rtc_nvram_offset, &rtc_nvram_value);
2300 		if (error == 0) {
2301 			printf("rtc nvram[%03d]: 0x%02x\n", rtc_nvram_offset,
2302 			    rtc_nvram_value);
2303 		}
2304 	}
2305 
2306 	if (!error && set_rtc_time)
2307 		error = vm_rtc_settime(ctx, rtc_secs);
2308 
2309 	if (!error && (get_rtc_time || get_all)) {
2310 		error = vm_rtc_gettime(ctx, &rtc_secs);
2311 		if (error == 0) {
2312 			gmtime_r(&rtc_secs, &tm);
2313 			printf("rtc time %#lx: %s %s %02d %02d:%02d:%02d %d\n",
2314 			    rtc_secs, wday_str(tm.tm_wday), mon_str(tm.tm_mon),
2315 			    tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
2316 			    1900 + tm.tm_year);
2317 		}
2318 	}
2319 
2320 	if (!error && (getcap || get_all)) {
2321 		int captype, val, getcaptype;
2322 
2323 		if (getcap && capname)
2324 			getcaptype = vm_capability_name2type(capname);
2325 		else
2326 			getcaptype = -1;
2327 
2328 		for (captype = 0; captype < VM_CAP_MAX; captype++) {
2329 			if (getcaptype >= 0 && captype != getcaptype)
2330 				continue;
2331 			error = vm_get_capability(vcpu, captype, &val);
2332 			if (error == 0) {
2333 				printf("Capability \"%s\" is %s on vcpu %d\n",
2334 					vm_capability_type2name(captype),
2335 					val ? "set" : "not set", vcpuid);
2336 			} else if (errno == ENOENT) {
2337 				error = 0;
2338 				printf("Capability \"%s\" is not available\n",
2339 					vm_capability_type2name(captype));
2340 			} else {
2341 				break;
2342 			}
2343 		}
2344 	}
2345 
2346 	if (!error && (get_active_cpus || get_all)) {
2347 		error = vm_active_cpus(ctx, &cpus);
2348 		if (!error)
2349 			print_cpus("active cpus", &cpus);
2350 	}
2351 
2352 	if (!error && (get_debug_cpus || get_all)) {
2353 		error = vm_debug_cpus(ctx, &cpus);
2354 		if (!error)
2355 			print_cpus("debug cpus", &cpus);
2356 	}
2357 
2358 	if (!error && (get_suspended_cpus || get_all)) {
2359 		error = vm_suspended_cpus(ctx, &cpus);
2360 		if (!error)
2361 			print_cpus("suspended cpus", &cpus);
2362 	}
2363 
2364 	if (!error && (get_intinfo || get_all)) {
2365 		error = vm_get_intinfo(vcpu, &info[0], &info[1]);
2366 		if (!error) {
2367 			print_intinfo("pending", info[0]);
2368 			print_intinfo("current", info[1]);
2369 		}
2370 	}
2371 
2372 	if (!error && (get_stats || get_all)) {
2373 		int i, num_stats;
2374 		uint64_t *stats;
2375 		struct timeval tv;
2376 		const char *desc;
2377 
2378 		stats = vm_get_stats(vcpu, &tv, &num_stats);
2379 		if (stats != NULL) {
2380 			printf("vcpu%d stats:\n", vcpuid);
2381 			for (i = 0; i < num_stats; i++) {
2382 				desc = vm_get_stat_desc(ctx, i);
2383 				printf("%-40s\t%ld\n", desc, stats[i]);
2384 			}
2385 		}
2386 	}
2387 
2388 	if (!error && (get_cpu_topology || get_all)) {
2389 		uint16_t sockets, cores, threads, maxcpus;
2390 
2391 		vm_get_topology(ctx, &sockets, &cores, &threads, &maxcpus);
2392 		printf("cpu_topology:\tsockets=%hu, cores=%hu, threads=%hu, "
2393 		    "maxcpus=%hu\n", sockets, cores, threads, maxcpus);
2394 	}
2395 
2396 	if (!error && run) {
2397 		struct vm_exit vmexit;
2398 		cpuset_t cpuset;
2399 
2400 		vmrun.vm_exit = &vmexit;
2401 		vmrun.cpuset = &cpuset;
2402 		vmrun.cpusetsize = sizeof(cpuset);
2403 		error = vm_run(vcpu, &vmrun);
2404 		if (error == 0)
2405 			dump_vm_run_exitcode(&vmexit, vcpuid);
2406 		else
2407 			printf("vm_run error %d\n", error);
2408 	}
2409 
2410 	if (!error && force_reset)
2411 		error = vm_suspend(ctx, VM_SUSPEND_RESET);
2412 
2413 	if (!error && force_poweroff)
2414 		error = vm_suspend(ctx, VM_SUSPEND_POWEROFF);
2415 
2416 	if (error)
2417 		printf("errno = %d\n", errno);
2418 
2419 	if (!error && destroy)
2420 		vm_destroy(ctx);
2421 
2422 #ifdef BHYVE_SNAPSHOT
2423 	if (!error && checkpoint_file)
2424 		error = snapshot_request(vmname, checkpoint_file, vm_suspend_opt);
2425 #endif
2426 
2427 	free (opts);
2428 	exit(error);
2429 }
2430