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