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