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