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