xref: /illumos-gate/usr/src/cmd/bhyvectl/bhyvectl.c (revision d15d17d4231f87f1571fa6d585377206f360f667)
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, "GenuineIntel") == 0) {
687 		return (true);
688 	} else {
689 		fprintf(stderr, "Unknown cpu vendor \"%s\"\n", cpu_vendor);
690 		exit(1);
691 	}
692 }
693 
694 static int
695 get_all_registers(struct vmctx *ctx, int vcpu)
696 {
697 	uint64_t cr0, cr2, cr3, cr4, dr0, dr1, dr2, dr3, dr6, dr7;
698 	uint64_t rsp, rip, rflags, efer;
699 	uint64_t rax, rbx, rcx, rdx, rsi, rdi, rbp;
700 	uint64_t r8, r9, r10, r11, r12, r13, r14, r15;
701 	int error = 0;
702 
703 	if (!error && (get_efer || get_all)) {
704 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_EFER, &efer);
705 		if (error == 0)
706 			printf("efer[%d]\t\t0x%016lx\n", vcpu, efer);
707 	}
708 
709 	if (!error && (get_cr0 || get_all)) {
710 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CR0, &cr0);
711 		if (error == 0)
712 			printf("cr0[%d]\t\t0x%016lx\n", vcpu, cr0);
713 	}
714 
715 	if (!error && (get_cr2 || get_all)) {
716 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CR2, &cr2);
717 		if (error == 0)
718 			printf("cr2[%d]\t\t0x%016lx\n", vcpu, cr2);
719 	}
720 
721 	if (!error && (get_cr3 || get_all)) {
722 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CR3, &cr3);
723 		if (error == 0)
724 			printf("cr3[%d]\t\t0x%016lx\n", vcpu, cr3);
725 	}
726 
727 	if (!error && (get_cr4 || get_all)) {
728 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CR4, &cr4);
729 		if (error == 0)
730 			printf("cr4[%d]\t\t0x%016lx\n", vcpu, cr4);
731 	}
732 
733 	if (!error && (get_dr0 || get_all)) {
734 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DR0, &dr0);
735 		if (error == 0)
736 			printf("dr0[%d]\t\t0x%016lx\n", vcpu, dr0);
737 	}
738 
739 	if (!error && (get_dr1 || get_all)) {
740 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DR1, &dr1);
741 		if (error == 0)
742 			printf("dr1[%d]\t\t0x%016lx\n", vcpu, dr1);
743 	}
744 
745 	if (!error && (get_dr2 || get_all)) {
746 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DR2, &dr2);
747 		if (error == 0)
748 			printf("dr2[%d]\t\t0x%016lx\n", vcpu, dr2);
749 	}
750 
751 	if (!error && (get_dr3 || get_all)) {
752 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DR3, &dr3);
753 		if (error == 0)
754 			printf("dr3[%d]\t\t0x%016lx\n", vcpu, dr3);
755 	}
756 
757 	if (!error && (get_dr6 || get_all)) {
758 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DR6, &dr6);
759 		if (error == 0)
760 			printf("dr6[%d]\t\t0x%016lx\n", vcpu, dr6);
761 	}
762 
763 	if (!error && (get_dr7 || get_all)) {
764 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DR7, &dr7);
765 		if (error == 0)
766 			printf("dr7[%d]\t\t0x%016lx\n", vcpu, dr7);
767 	}
768 
769 	if (!error && (get_rsp || get_all)) {
770 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RSP, &rsp);
771 		if (error == 0)
772 			printf("rsp[%d]\t\t0x%016lx\n", vcpu, rsp);
773 	}
774 
775 	if (!error && (get_rip || get_all)) {
776 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RIP, &rip);
777 		if (error == 0)
778 			printf("rip[%d]\t\t0x%016lx\n", vcpu, rip);
779 	}
780 
781 	if (!error && (get_rax || get_all)) {
782 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RAX, &rax);
783 		if (error == 0)
784 			printf("rax[%d]\t\t0x%016lx\n", vcpu, rax);
785 	}
786 
787 	if (!error && (get_rbx || get_all)) {
788 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RBX, &rbx);
789 		if (error == 0)
790 			printf("rbx[%d]\t\t0x%016lx\n", vcpu, rbx);
791 	}
792 
793 	if (!error && (get_rcx || get_all)) {
794 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RCX, &rcx);
795 		if (error == 0)
796 			printf("rcx[%d]\t\t0x%016lx\n", vcpu, rcx);
797 	}
798 
799 	if (!error && (get_rdx || get_all)) {
800 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RDX, &rdx);
801 		if (error == 0)
802 			printf("rdx[%d]\t\t0x%016lx\n", vcpu, rdx);
803 	}
804 
805 	if (!error && (get_rsi || get_all)) {
806 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RSI, &rsi);
807 		if (error == 0)
808 			printf("rsi[%d]\t\t0x%016lx\n", vcpu, rsi);
809 	}
810 
811 	if (!error && (get_rdi || get_all)) {
812 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RDI, &rdi);
813 		if (error == 0)
814 			printf("rdi[%d]\t\t0x%016lx\n", vcpu, rdi);
815 	}
816 
817 	if (!error && (get_rbp || get_all)) {
818 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RBP, &rbp);
819 		if (error == 0)
820 			printf("rbp[%d]\t\t0x%016lx\n", vcpu, rbp);
821 	}
822 
823 	if (!error && (get_r8 || get_all)) {
824 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R8, &r8);
825 		if (error == 0)
826 			printf("r8[%d]\t\t0x%016lx\n", vcpu, r8);
827 	}
828 
829 	if (!error && (get_r9 || get_all)) {
830 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R9, &r9);
831 		if (error == 0)
832 			printf("r9[%d]\t\t0x%016lx\n", vcpu, r9);
833 	}
834 
835 	if (!error && (get_r10 || get_all)) {
836 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R10, &r10);
837 		if (error == 0)
838 			printf("r10[%d]\t\t0x%016lx\n", vcpu, r10);
839 	}
840 
841 	if (!error && (get_r11 || get_all)) {
842 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R11, &r11);
843 		if (error == 0)
844 			printf("r11[%d]\t\t0x%016lx\n", vcpu, r11);
845 	}
846 
847 	if (!error && (get_r12 || get_all)) {
848 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R12, &r12);
849 		if (error == 0)
850 			printf("r12[%d]\t\t0x%016lx\n", vcpu, r12);
851 	}
852 
853 	if (!error && (get_r13 || get_all)) {
854 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R13, &r13);
855 		if (error == 0)
856 			printf("r13[%d]\t\t0x%016lx\n", vcpu, r13);
857 	}
858 
859 	if (!error && (get_r14 || get_all)) {
860 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R14, &r14);
861 		if (error == 0)
862 			printf("r14[%d]\t\t0x%016lx\n", vcpu, r14);
863 	}
864 
865 	if (!error && (get_r15 || get_all)) {
866 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R15, &r15);
867 		if (error == 0)
868 			printf("r15[%d]\t\t0x%016lx\n", vcpu, r15);
869 	}
870 
871 	if (!error && (get_rflags || get_all)) {
872 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RFLAGS,
873 					&rflags);
874 		if (error == 0)
875 			printf("rflags[%d]\t0x%016lx\n", vcpu, rflags);
876 	}
877 
878 	return (error);
879 }
880 
881 static int
882 get_all_segments(struct vmctx *ctx, int vcpu)
883 {
884 	uint64_t cs, ds, es, fs, gs, ss, tr, ldtr;
885 	int error = 0;
886 
887 	if (!error && (get_desc_ds || get_all)) {
888 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_DS,
889 				   &desc_base, &desc_limit, &desc_access);
890 		if (error == 0) {
891 			printf("ds desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
892 			      vcpu, desc_base, desc_limit, desc_access);
893 		}
894 	}
895 
896 	if (!error && (get_desc_es || get_all)) {
897 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_ES,
898 				    &desc_base, &desc_limit, &desc_access);
899 		if (error == 0) {
900 			printf("es desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
901 			       vcpu, desc_base, desc_limit, desc_access);
902 		}
903 	}
904 
905 	if (!error && (get_desc_fs || get_all)) {
906 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_FS,
907 				    &desc_base, &desc_limit, &desc_access);
908 		if (error == 0) {
909 			printf("fs desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
910 			       vcpu, desc_base, desc_limit, desc_access);
911 		}
912 	}
913 
914 	if (!error && (get_desc_gs || get_all)) {
915 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_GS,
916 				    &desc_base, &desc_limit, &desc_access);
917 		if (error == 0) {
918 			printf("gs desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
919 			       vcpu, desc_base, desc_limit, desc_access);
920 		}
921 	}
922 
923 	if (!error && (get_desc_ss || get_all)) {
924 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_SS,
925 				    &desc_base, &desc_limit, &desc_access);
926 		if (error == 0) {
927 			printf("ss desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
928 			       vcpu, desc_base, desc_limit, desc_access);
929 		}
930 	}
931 
932 	if (!error && (get_desc_cs || get_all)) {
933 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_CS,
934 				    &desc_base, &desc_limit, &desc_access);
935 		if (error == 0) {
936 			printf("cs desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
937 			       vcpu, desc_base, desc_limit, desc_access);
938 		}
939 	}
940 
941 	if (!error && (get_desc_tr || get_all)) {
942 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_TR,
943 				    &desc_base, &desc_limit, &desc_access);
944 		if (error == 0) {
945 			printf("tr desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
946 			       vcpu, desc_base, desc_limit, desc_access);
947 		}
948 	}
949 
950 	if (!error && (get_desc_ldtr || get_all)) {
951 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_LDTR,
952 				    &desc_base, &desc_limit, &desc_access);
953 		if (error == 0) {
954 			printf("ldtr desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
955 			       vcpu, desc_base, desc_limit, desc_access);
956 		}
957 	}
958 
959 	if (!error && (get_desc_gdtr || get_all)) {
960 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_GDTR,
961 				    &desc_base, &desc_limit, &desc_access);
962 		if (error == 0) {
963 			printf("gdtr[%d]\t\t0x%016lx/0x%08x\n",
964 			       vcpu, desc_base, desc_limit);
965 		}
966 	}
967 
968 	if (!error && (get_desc_idtr || get_all)) {
969 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_IDTR,
970 				    &desc_base, &desc_limit, &desc_access);
971 		if (error == 0) {
972 			printf("idtr[%d]\t\t0x%016lx/0x%08x\n",
973 			       vcpu, desc_base, desc_limit);
974 		}
975 	}
976 
977 	if (!error && (get_cs || get_all)) {
978 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CS, &cs);
979 		if (error == 0)
980 			printf("cs[%d]\t\t0x%04lx\n", vcpu, cs);
981 	}
982 
983 	if (!error && (get_ds || get_all)) {
984 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DS, &ds);
985 		if (error == 0)
986 			printf("ds[%d]\t\t0x%04lx\n", vcpu, ds);
987 	}
988 
989 	if (!error && (get_es || get_all)) {
990 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_ES, &es);
991 		if (error == 0)
992 			printf("es[%d]\t\t0x%04lx\n", vcpu, es);
993 	}
994 
995 	if (!error && (get_fs || get_all)) {
996 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_FS, &fs);
997 		if (error == 0)
998 			printf("fs[%d]\t\t0x%04lx\n", vcpu, fs);
999 	}
1000 
1001 	if (!error && (get_gs || get_all)) {
1002 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_GS, &gs);
1003 		if (error == 0)
1004 			printf("gs[%d]\t\t0x%04lx\n", vcpu, gs);
1005 	}
1006 
1007 	if (!error && (get_ss || get_all)) {
1008 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_SS, &ss);
1009 		if (error == 0)
1010 			printf("ss[%d]\t\t0x%04lx\n", vcpu, ss);
1011 	}
1012 
1013 	if (!error && (get_tr || get_all)) {
1014 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_TR, &tr);
1015 		if (error == 0)
1016 			printf("tr[%d]\t\t0x%04lx\n", vcpu, tr);
1017 	}
1018 
1019 	if (!error && (get_ldtr || get_all)) {
1020 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_LDTR, &ldtr);
1021 		if (error == 0)
1022 			printf("ldtr[%d]\t\t0x%04lx\n", vcpu, ldtr);
1023 	}
1024 
1025 	return (error);
1026 }
1027 
1028 static int
1029 get_misc_vmcs(struct vmctx *ctx, int vcpu)
1030 {
1031 	uint64_t ctl, cr0, cr3, cr4, rsp, rip, pat, addr, u64;
1032 	int error = 0;
1033 
1034 	if (!error && (get_cr0_mask || get_all)) {
1035 		uint64_t cr0mask;
1036 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR0_MASK, &cr0mask);
1037 		if (error == 0)
1038 			printf("cr0_mask[%d]\t\t0x%016lx\n", vcpu, cr0mask);
1039 	}
1040 
1041 	if (!error && (get_cr0_shadow || get_all)) {
1042 		uint64_t cr0shadow;
1043 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR0_SHADOW,
1044 					  &cr0shadow);
1045 		if (error == 0)
1046 			printf("cr0_shadow[%d]\t\t0x%016lx\n", vcpu, cr0shadow);
1047 	}
1048 
1049 	if (!error && (get_cr4_mask || get_all)) {
1050 		uint64_t cr4mask;
1051 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR4_MASK, &cr4mask);
1052 		if (error == 0)
1053 			printf("cr4_mask[%d]\t\t0x%016lx\n", vcpu, cr4mask);
1054 	}
1055 
1056 	if (!error && (get_cr4_shadow || get_all)) {
1057 		uint64_t cr4shadow;
1058 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR4_SHADOW,
1059 					  &cr4shadow);
1060 		if (error == 0)
1061 			printf("cr4_shadow[%d]\t\t0x%016lx\n", vcpu, cr4shadow);
1062 	}
1063 
1064 	if (!error && (get_cr3_targets || get_all)) {
1065 		uint64_t target_count, target_addr;
1066 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET_COUNT,
1067 					  &target_count);
1068 		if (error == 0) {
1069 			printf("cr3_target_count[%d]\t0x%016lx\n",
1070 				vcpu, target_count);
1071 		}
1072 
1073 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET0,
1074 					  &target_addr);
1075 		if (error == 0) {
1076 			printf("cr3_target0[%d]\t\t0x%016lx\n",
1077 				vcpu, target_addr);
1078 		}
1079 
1080 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET1,
1081 					  &target_addr);
1082 		if (error == 0) {
1083 			printf("cr3_target1[%d]\t\t0x%016lx\n",
1084 				vcpu, target_addr);
1085 		}
1086 
1087 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET2,
1088 					  &target_addr);
1089 		if (error == 0) {
1090 			printf("cr3_target2[%d]\t\t0x%016lx\n",
1091 				vcpu, target_addr);
1092 		}
1093 
1094 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET3,
1095 					  &target_addr);
1096 		if (error == 0) {
1097 			printf("cr3_target3[%d]\t\t0x%016lx\n",
1098 				vcpu, target_addr);
1099 		}
1100 	}
1101 
1102 	if (!error && (get_pinbased_ctls || get_all)) {
1103 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_PIN_BASED_CTLS, &ctl);
1104 		if (error == 0)
1105 			printf("pinbased_ctls[%d]\t0x%016lx\n", vcpu, ctl);
1106 	}
1107 
1108 	if (!error && (get_procbased_ctls || get_all)) {
1109 		error = vm_get_vmcs_field(ctx, vcpu,
1110 					  VMCS_PRI_PROC_BASED_CTLS, &ctl);
1111 		if (error == 0)
1112 			printf("procbased_ctls[%d]\t0x%016lx\n", vcpu, ctl);
1113 	}
1114 
1115 	if (!error && (get_procbased_ctls2 || get_all)) {
1116 		error = vm_get_vmcs_field(ctx, vcpu,
1117 					  VMCS_SEC_PROC_BASED_CTLS, &ctl);
1118 		if (error == 0)
1119 			printf("procbased_ctls2[%d]\t0x%016lx\n", vcpu, ctl);
1120 	}
1121 
1122 	if (!error && (get_vmcs_gla || get_all)) {
1123 		error = vm_get_vmcs_field(ctx, vcpu,
1124 					  VMCS_GUEST_LINEAR_ADDRESS, &u64);
1125 		if (error == 0)
1126 			printf("gla[%d]\t\t0x%016lx\n", vcpu, u64);
1127 	}
1128 
1129 	if (!error && (get_vmcs_gpa || get_all)) {
1130 		error = vm_get_vmcs_field(ctx, vcpu,
1131 					  VMCS_GUEST_PHYSICAL_ADDRESS, &u64);
1132 		if (error == 0)
1133 			printf("gpa[%d]\t\t0x%016lx\n", vcpu, u64);
1134 	}
1135 
1136 	if (!error && (get_vmcs_entry_interruption_info ||
1137 		get_all)) {
1138 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_ENTRY_INTR_INFO,&u64);
1139 		if (error == 0) {
1140 			printf("entry_interruption_info[%d]\t0x%016lx\n",
1141 				vcpu, u64);
1142 		}
1143 	}
1144 
1145 	if (!error && (get_tpr_threshold || get_all)) {
1146 		uint64_t threshold;
1147 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_TPR_THRESHOLD,
1148 					  &threshold);
1149 		if (error == 0)
1150 			printf("tpr_threshold[%d]\t0x%016lx\n", vcpu, threshold);
1151 	}
1152 
1153 	if (!error && (get_inst_err || get_all)) {
1154 		uint64_t insterr;
1155 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_INSTRUCTION_ERROR,
1156 					  &insterr);
1157 		if (error == 0) {
1158 			printf("instruction_error[%d]\t0x%016lx\n",
1159 				vcpu, insterr);
1160 		}
1161 	}
1162 
1163 	if (!error && (get_exit_ctls || get_all)) {
1164 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_CTLS, &ctl);
1165 		if (error == 0)
1166 			printf("exit_ctls[%d]\t\t0x%016lx\n", vcpu, ctl);
1167 	}
1168 
1169 	if (!error && (get_entry_ctls || get_all)) {
1170 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_ENTRY_CTLS, &ctl);
1171 		if (error == 0)
1172 			printf("entry_ctls[%d]\t\t0x%016lx\n", vcpu, ctl);
1173 	}
1174 
1175 	if (!error && (get_host_pat || get_all)) {
1176 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_IA32_PAT, &pat);
1177 		if (error == 0)
1178 			printf("host_pat[%d]\t\t0x%016lx\n", vcpu, pat);
1179 	}
1180 
1181 	if (!error && (get_host_cr0 || get_all)) {
1182 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_CR0, &cr0);
1183 		if (error == 0)
1184 			printf("host_cr0[%d]\t\t0x%016lx\n", vcpu, cr0);
1185 	}
1186 
1187 	if (!error && (get_host_cr3 || get_all)) {
1188 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_CR3, &cr3);
1189 		if (error == 0)
1190 			printf("host_cr3[%d]\t\t0x%016lx\n", vcpu, cr3);
1191 	}
1192 
1193 	if (!error && (get_host_cr4 || get_all)) {
1194 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_CR4, &cr4);
1195 		if (error == 0)
1196 			printf("host_cr4[%d]\t\t0x%016lx\n", vcpu, cr4);
1197 	}
1198 
1199 	if (!error && (get_host_rip || get_all)) {
1200 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_RIP, &rip);
1201 		if (error == 0)
1202 			printf("host_rip[%d]\t\t0x%016lx\n", vcpu, rip);
1203 	}
1204 
1205 	if (!error && (get_host_rsp || get_all)) {
1206 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_RSP, &rsp);
1207 		if (error == 0)
1208 			printf("host_rsp[%d]\t\t0x%016lx\n", vcpu, rsp);
1209 	}
1210 
1211 	if (!error && (get_vmcs_link || get_all)) {
1212 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_LINK_POINTER, &addr);
1213 		if (error == 0)
1214 			printf("vmcs_pointer[%d]\t0x%016lx\n", vcpu, addr);
1215 	}
1216 
1217 	if (!error && (get_vmcs_exit_interruption_info || get_all)) {
1218 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_INTR_INFO, &u64);
1219 		if (error == 0) {
1220 			printf("vmcs_exit_interruption_info[%d]\t0x%016lx\n",
1221 				vcpu, u64);
1222 		}
1223 	}
1224 
1225 	if (!error && (get_vmcs_exit_interruption_error || get_all)) {
1226 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_INTR_ERRCODE,
1227 		    			  &u64);
1228 		if (error == 0) {
1229 			printf("vmcs_exit_interruption_error[%d]\t0x%016lx\n",
1230 				vcpu, u64);
1231 		}
1232 	}
1233 
1234 	if (!error && (get_vmcs_interruptibility || get_all)) {
1235 		error = vm_get_vmcs_field(ctx, vcpu,
1236 					  VMCS_GUEST_INTERRUPTIBILITY, &u64);
1237 		if (error == 0) {
1238 			printf("vmcs_guest_interruptibility[%d]\t0x%016lx\n",
1239 				vcpu, u64);
1240 		}
1241 	}
1242 
1243 	if (!error && (get_vmcs_exit_inst_length || get_all)) {
1244 		error = vm_get_vmcs_field(ctx, vcpu,
1245 		    VMCS_EXIT_INSTRUCTION_LENGTH, &u64);
1246 		if (error == 0)
1247 			printf("vmcs_exit_inst_length[%d]\t0x%08x\n", vcpu,
1248 			    (uint32_t)u64);
1249 	}
1250 
1251 	if (!error && (get_vmcs_exit_qualification || get_all)) {
1252 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_QUALIFICATION,
1253 					  &u64);
1254 		if (error == 0)
1255 			printf("vmcs_exit_qualification[%d]\t0x%016lx\n",
1256 				vcpu, u64);
1257 	}
1258 
1259 	return (error);
1260 }
1261 
1262 static int
1263 get_misc_vmcb(struct vmctx *ctx, int vcpu)
1264 {
1265 	uint64_t ctl, addr;
1266 	int error = 0;
1267 
1268 	if (!error && (get_vmcb_intercept || get_all)) {
1269 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_CR_INTERCEPT, 4,
1270 		    &ctl);
1271 		if (error == 0)
1272 			printf("cr_intercept[%d]\t0x%08x\n", vcpu, (int)ctl);
1273 
1274 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_DR_INTERCEPT, 4,
1275 		    &ctl);
1276 		if (error == 0)
1277 			printf("dr_intercept[%d]\t0x%08x\n", vcpu, (int)ctl);
1278 
1279 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_EXC_INTERCEPT, 4,
1280 		    &ctl);
1281 		if (error == 0)
1282 			printf("exc_intercept[%d]\t0x%08x\n", vcpu, (int)ctl);
1283 
1284 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_INST1_INTERCEPT,
1285 		    4, &ctl);
1286 		if (error == 0)
1287 			printf("inst1_intercept[%d]\t0x%08x\n", vcpu, (int)ctl);
1288 
1289 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_INST2_INTERCEPT,
1290 		    4, &ctl);
1291 		if (error == 0)
1292 			printf("inst2_intercept[%d]\t0x%08x\n", vcpu, (int)ctl);
1293 	}
1294 
1295 	if (!error && (get_vmcb_tlb_ctrl || get_all)) {
1296 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_TLB_CTRL,
1297 					  4, &ctl);
1298 		if (error == 0)
1299 			printf("TLB ctrl[%d]\t0x%016lx\n", vcpu, ctl);
1300 	}
1301 
1302 	if (!error && (get_vmcb_exit_details || get_all)) {
1303 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_EXITINFO1,
1304 					  8, &ctl);
1305 		if (error == 0)
1306 			printf("exitinfo1[%d]\t0x%016lx\n", vcpu, ctl);
1307 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_EXITINFO2,
1308 					  8, &ctl);
1309 		if (error == 0)
1310 			printf("exitinfo2[%d]\t0x%016lx\n", vcpu, ctl);
1311 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_EXITINTINFO,
1312 					  8, &ctl);
1313 		if (error == 0)
1314 			printf("exitintinfo[%d]\t0x%016lx\n", vcpu, ctl);
1315 	}
1316 
1317 	if (!error && (get_vmcb_virq || get_all)) {
1318 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_VIRQ,
1319 					  8, &ctl);
1320 		if (error == 0)
1321 			printf("v_irq/tpr[%d]\t0x%016lx\n", vcpu, ctl);
1322 	}
1323 
1324 	if (!error && (get_apic_access_addr || get_all)) {
1325 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_AVIC_BAR, 8,
1326 					  &addr);
1327 		if (error == 0)
1328 			printf("AVIC apic_bar[%d]\t0x%016lx\n", vcpu, addr);
1329 	}
1330 
1331 	if (!error && (get_virtual_apic_addr || get_all)) {
1332 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_AVIC_PAGE, 8,
1333 					  &addr);
1334 		if (error == 0)
1335 			printf("AVIC backing page[%d]\t0x%016lx\n", vcpu, addr);
1336 	}
1337 
1338 	if (!error && (get_avic_table || get_all)) {
1339 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_AVIC_LT, 8,
1340 					  &addr);
1341 		if (error == 0)
1342 			printf("AVIC logical table[%d]\t0x%016lx\n",
1343 				vcpu, addr);
1344 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_AVIC_PT, 8,
1345 					  &addr);
1346 		if (error == 0)
1347 			printf("AVIC physical table[%d]\t0x%016lx\n",
1348 				vcpu, addr);
1349 	}
1350 
1351 	return (error);
1352 }
1353 
1354 static struct option *
1355 setup_options(bool cpu_intel)
1356 {
1357 	const struct option common_opts[] = {
1358 		{ "vm",		REQ_ARG,	0,	VMNAME },
1359 		{ "cpu",	REQ_ARG,	0,	VCPU },
1360 		{ "set-mem",	REQ_ARG,	0,	SET_MEM },
1361 		{ "set-efer",	REQ_ARG,	0,	SET_EFER },
1362 		{ "set-cr0",	REQ_ARG,	0,	SET_CR0 },
1363 		{ "set-cr2",	REQ_ARG,	0,	SET_CR2 },
1364 		{ "set-cr3",	REQ_ARG,	0,	SET_CR3 },
1365 		{ "set-cr4",	REQ_ARG,	0,	SET_CR4 },
1366 		{ "set-dr0",	REQ_ARG,	0,	SET_DR0 },
1367 		{ "set-dr1",	REQ_ARG,	0,	SET_DR1 },
1368 		{ "set-dr2",	REQ_ARG,	0,	SET_DR2 },
1369 		{ "set-dr3",	REQ_ARG,	0,	SET_DR3 },
1370 		{ "set-dr6",	REQ_ARG,	0,	SET_DR6 },
1371 		{ "set-dr7",	REQ_ARG,	0,	SET_DR7 },
1372 		{ "set-rsp",	REQ_ARG,	0,	SET_RSP },
1373 		{ "set-rip",	REQ_ARG,	0,	SET_RIP },
1374 		{ "set-rax",	REQ_ARG,	0,	SET_RAX },
1375 		{ "set-rflags",	REQ_ARG,	0,	SET_RFLAGS },
1376 		{ "desc-base",	REQ_ARG,	0,	DESC_BASE },
1377 		{ "desc-limit",	REQ_ARG,	0,	DESC_LIMIT },
1378 		{ "desc-access",REQ_ARG,	0,	DESC_ACCESS },
1379 		{ "set-cs",	REQ_ARG,	0,	SET_CS },
1380 		{ "set-ds",	REQ_ARG,	0,	SET_DS },
1381 		{ "set-es",	REQ_ARG,	0,	SET_ES },
1382 		{ "set-fs",	REQ_ARG,	0,	SET_FS },
1383 		{ "set-gs",	REQ_ARG,	0,	SET_GS },
1384 		{ "set-ss",	REQ_ARG,	0,	SET_SS },
1385 		{ "set-tr",	REQ_ARG,	0,	SET_TR },
1386 		{ "set-ldtr",	REQ_ARG,	0,	SET_LDTR },
1387 		{ "set-x2apic-state",REQ_ARG,	0,	SET_X2APIC_STATE },
1388 		{ "set-exception-bitmap",
1389 				REQ_ARG,	0, SET_EXCEPTION_BITMAP },
1390 		{ "capname",	REQ_ARG,	0,	CAPNAME },
1391 		{ "unassign-pptdev", REQ_ARG,	0,	UNASSIGN_PPTDEV },
1392 		{ "setcap",	REQ_ARG,	0,	SET_CAP },
1393 		{ "get-gpa-pmap", REQ_ARG,	0,	GET_GPA_PMAP },
1394 		{ "assert-lapic-lvt", REQ_ARG,	0,	ASSERT_LAPIC_LVT },
1395 		{ "get-rtc-time", NO_ARG,	&get_rtc_time,	1 },
1396 		{ "set-rtc-time", REQ_ARG,	0,	SET_RTC_TIME },
1397 		{ "rtc-nvram-offset", REQ_ARG,	0,	RTC_NVRAM_OFFSET },
1398 		{ "get-rtc-nvram", NO_ARG,	&get_rtc_nvram,	1 },
1399 		{ "set-rtc-nvram", REQ_ARG,	0,	SET_RTC_NVRAM },
1400 		{ "getcap",	NO_ARG,		&getcap,	1 },
1401 		{ "get-stats",	NO_ARG,		&get_stats,	1 },
1402 		{ "get-desc-ds",NO_ARG,		&get_desc_ds,	1 },
1403 		{ "set-desc-ds",NO_ARG,		&set_desc_ds,	1 },
1404 		{ "get-desc-es",NO_ARG,		&get_desc_es,	1 },
1405 		{ "set-desc-es",NO_ARG,		&set_desc_es,	1 },
1406 		{ "get-desc-ss",NO_ARG,		&get_desc_ss,	1 },
1407 		{ "set-desc-ss",NO_ARG,		&set_desc_ss,	1 },
1408 		{ "get-desc-cs",NO_ARG,		&get_desc_cs,	1 },
1409 		{ "set-desc-cs",NO_ARG,		&set_desc_cs,	1 },
1410 		{ "get-desc-fs",NO_ARG,		&get_desc_fs,	1 },
1411 		{ "set-desc-fs",NO_ARG,		&set_desc_fs,	1 },
1412 		{ "get-desc-gs",NO_ARG,		&get_desc_gs,	1 },
1413 		{ "set-desc-gs",NO_ARG,		&set_desc_gs,	1 },
1414 		{ "get-desc-tr",NO_ARG,		&get_desc_tr,	1 },
1415 		{ "set-desc-tr",NO_ARG,		&set_desc_tr,	1 },
1416 		{ "set-desc-ldtr", NO_ARG,	&set_desc_ldtr,	1 },
1417 		{ "get-desc-ldtr", NO_ARG,	&get_desc_ldtr,	1 },
1418 		{ "set-desc-gdtr", NO_ARG,	&set_desc_gdtr, 1 },
1419 		{ "get-desc-gdtr", NO_ARG,	&get_desc_gdtr, 1 },
1420 		{ "set-desc-idtr", NO_ARG,	&set_desc_idtr, 1 },
1421 		{ "get-desc-idtr", NO_ARG,	&get_desc_idtr, 1 },
1422 		{ "get-memmap",	NO_ARG,		&get_memmap,	1 },
1423 		{ "get-memseg", NO_ARG,		&get_memseg,	1 },
1424 		{ "get-efer",	NO_ARG,		&get_efer,	1 },
1425 		{ "get-cr0",	NO_ARG,		&get_cr0,	1 },
1426 		{ "get-cr2",	NO_ARG,		&get_cr2,	1 },
1427 		{ "get-cr3",	NO_ARG,		&get_cr3,	1 },
1428 		{ "get-cr4",	NO_ARG,		&get_cr4,	1 },
1429 		{ "get-dr0",	NO_ARG,		&get_dr0,	1 },
1430 		{ "get-dr1",	NO_ARG,		&get_dr1,	1 },
1431 		{ "get-dr2",	NO_ARG,		&get_dr2,	1 },
1432 		{ "get-dr3",	NO_ARG,		&get_dr3,	1 },
1433 		{ "get-dr6",	NO_ARG,		&get_dr6,	1 },
1434 		{ "get-dr7",	NO_ARG,		&get_dr7,	1 },
1435 		{ "get-rsp",	NO_ARG,		&get_rsp,	1 },
1436 		{ "get-rip",	NO_ARG,		&get_rip,	1 },
1437 		{ "get-rax",	NO_ARG,		&get_rax,	1 },
1438 		{ "get-rbx",	NO_ARG,		&get_rbx,	1 },
1439 		{ "get-rcx",	NO_ARG,		&get_rcx,	1 },
1440 		{ "get-rdx",	NO_ARG,		&get_rdx,	1 },
1441 		{ "get-rsi",	NO_ARG,		&get_rsi,	1 },
1442 		{ "get-rdi",	NO_ARG,		&get_rdi,	1 },
1443 		{ "get-rbp",	NO_ARG,		&get_rbp,	1 },
1444 		{ "get-r8",	NO_ARG,		&get_r8,	1 },
1445 		{ "get-r9",	NO_ARG,		&get_r9,	1 },
1446 		{ "get-r10",	NO_ARG,		&get_r10,	1 },
1447 		{ "get-r11",	NO_ARG,		&get_r11,	1 },
1448 		{ "get-r12",	NO_ARG,		&get_r12,	1 },
1449 		{ "get-r13",	NO_ARG,		&get_r13,	1 },
1450 		{ "get-r14",	NO_ARG,		&get_r14,	1 },
1451 		{ "get-r15",	NO_ARG,		&get_r15,	1 },
1452 		{ "get-rflags",	NO_ARG,		&get_rflags,	1 },
1453 		{ "get-cs",	NO_ARG,		&get_cs,	1 },
1454 		{ "get-ds",	NO_ARG,		&get_ds,	1 },
1455 		{ "get-es",	NO_ARG,		&get_es,	1 },
1456 		{ "get-fs",	NO_ARG,		&get_fs,	1 },
1457 		{ "get-gs",	NO_ARG,		&get_gs,	1 },
1458 		{ "get-ss",	NO_ARG,		&get_ss,	1 },
1459 		{ "get-tr",	NO_ARG,		&get_tr,	1 },
1460 		{ "get-ldtr",	NO_ARG,		&get_ldtr,	1 },
1461 		{ "get-eptp", 	NO_ARG,		&get_eptp,	1 },
1462 		{ "get-exception-bitmap",
1463 					NO_ARG,	&get_exception_bitmap,  1 },
1464 		{ "get-io-bitmap-address",
1465 					NO_ARG,	&get_io_bitmap,		1 },
1466 		{ "get-tsc-offset", 	NO_ARG, &get_tsc_offset, 	1 },
1467 		{ "get-msr-bitmap",
1468 					NO_ARG,	&get_msr_bitmap, 	1 },
1469 		{ "get-msr-bitmap-address",
1470 					NO_ARG,	&get_msr_bitmap_address, 1 },
1471 		{ "get-guest-pat",	NO_ARG,	&get_guest_pat,		1 },
1472 		{ "get-guest-sysenter",
1473 					NO_ARG,	&get_guest_sysenter, 	1 },
1474 		{ "get-exit-reason",
1475 					NO_ARG,	&get_exit_reason, 	1 },
1476 		{ "get-x2apic-state",	NO_ARG,	&get_x2apic_state, 	1 },
1477 		{ "get-all",		NO_ARG,	&get_all,		1 },
1478 		{ "run",		NO_ARG,	&run,			1 },
1479 		{ "create",		NO_ARG,	&create,		1 },
1480 		{ "destroy",		NO_ARG,	&destroy,		1 },
1481 		{ "inject-nmi",		NO_ARG,	&inject_nmi,		1 },
1482 		{ "force-reset",	NO_ARG,	&force_reset,		1 },
1483 		{ "force-poweroff", 	NO_ARG,	&force_poweroff, 	1 },
1484 		{ "get-active-cpus", 	NO_ARG,	&get_active_cpus, 	1 },
1485 		{ "get-suspended-cpus", NO_ARG,	&get_suspended_cpus, 	1 },
1486 		{ "get-intinfo", 	NO_ARG,	&get_intinfo,		1 },
1487 		{ "get-cpu-topology",	NO_ARG, &get_cpu_topology,	1 },
1488 #ifndef __FreeBSD__
1489 		{ "wrlock-cycle",	NO_ARG,	&wrlock_cycle,	1 },
1490 #endif
1491 	};
1492 
1493 	const struct option intel_opts[] = {
1494 		{ "get-vmcs-pinbased-ctls",
1495 				NO_ARG,		&get_pinbased_ctls, 1 },
1496 		{ "get-vmcs-procbased-ctls",
1497 				NO_ARG,		&get_procbased_ctls, 1 },
1498 		{ "get-vmcs-procbased-ctls2",
1499 				NO_ARG,		&get_procbased_ctls2, 1 },
1500 		{ "get-vmcs-guest-linear-address",
1501 				NO_ARG,		&get_vmcs_gla,	1 },
1502 		{ "get-vmcs-guest-physical-address",
1503 				NO_ARG,		&get_vmcs_gpa,	1 },
1504 		{ "get-vmcs-entry-interruption-info",
1505 				NO_ARG, &get_vmcs_entry_interruption_info, 1},
1506 		{ "get-vmcs-cr0-mask", NO_ARG,	&get_cr0_mask,	1 },
1507 		{ "get-vmcs-cr0-shadow", NO_ARG,&get_cr0_shadow, 1 },
1508 		{ "get-vmcs-cr4-mask", 		NO_ARG,	&get_cr4_mask,	  1 },
1509 		{ "get-vmcs-cr4-shadow", 	NO_ARG, &get_cr4_shadow,  1 },
1510 		{ "get-vmcs-cr3-targets", 	NO_ARG, &get_cr3_targets, 1 },
1511 		{ "get-vmcs-tpr-threshold",
1512 					NO_ARG,	&get_tpr_threshold, 1 },
1513 		{ "get-vmcs-vpid", 	NO_ARG,	&get_vpid_asid,	    1 },
1514 		{ "get-vmcs-exit-ctls", NO_ARG,	&get_exit_ctls,	    1 },
1515 		{ "get-vmcs-entry-ctls",
1516 					NO_ARG,	&get_entry_ctls, 1 },
1517 		{ "get-vmcs-instruction-error",
1518 					NO_ARG,	&get_inst_err,	1 },
1519 		{ "get-vmcs-host-pat",	NO_ARG,	&get_host_pat,	1 },
1520 		{ "get-vmcs-host-cr0",
1521 					NO_ARG,	&get_host_cr0,	1 },
1522 		{ "set-vmcs-entry-interruption-info",
1523 				REQ_ARG, 0, SET_VMCS_ENTRY_INTERRUPTION_INFO },
1524 		{ "get-vmcs-exit-qualification",
1525 				NO_ARG,	&get_vmcs_exit_qualification, 1 },
1526 		{ "get-vmcs-exit-inst-length",
1527 				NO_ARG,	&get_vmcs_exit_inst_length, 1 },
1528 		{ "get-vmcs-interruptibility",
1529 				NO_ARG, &get_vmcs_interruptibility, 1 },
1530 		{ "get-vmcs-exit-interruption-error",
1531 				NO_ARG,	&get_vmcs_exit_interruption_error, 1 },
1532 		{ "get-vmcs-exit-interruption-info",
1533 				NO_ARG,	&get_vmcs_exit_interruption_info, 1 },
1534 		{ "get-vmcs-link", 	NO_ARG,		&get_vmcs_link, 1 },
1535 		{ "get-vmcs-host-cr3",
1536 					NO_ARG,		&get_host_cr3,	1 },
1537 		{ "get-vmcs-host-cr4",
1538 				NO_ARG,		&get_host_cr4,	1 },
1539 		{ "get-vmcs-host-rip",
1540 				NO_ARG,		&get_host_rip,	1 },
1541 		{ "get-vmcs-host-rsp",
1542 				NO_ARG,		&get_host_rsp,	1 },
1543 		{ "get-apic-access-address",
1544 				NO_ARG,		&get_apic_access_addr, 1},
1545 		{ "get-virtual-apic-address",
1546 				NO_ARG,		&get_virtual_apic_addr, 1}
1547 	};
1548 
1549 	const struct option amd_opts[] = {
1550 		{ "get-vmcb-intercepts",
1551 				NO_ARG,	&get_vmcb_intercept, 	1 },
1552 		{ "get-vmcb-asid",
1553 				NO_ARG,	&get_vpid_asid,	     	1 },
1554 		{ "get-vmcb-exit-details",
1555 				NO_ARG, &get_vmcb_exit_details,	1 },
1556 		{ "get-vmcb-tlb-ctrl",
1557 				NO_ARG, &get_vmcb_tlb_ctrl, 	1 },
1558 		{ "get-vmcb-virq",
1559 				NO_ARG, &get_vmcb_virq, 	1 },
1560 		{ "get-avic-apic-bar",
1561 				NO_ARG,	&get_apic_access_addr, 	1 },
1562 		{ "get-avic-backing-page",
1563 				NO_ARG,	&get_virtual_apic_addr, 1 },
1564 		{ "get-avic-table",
1565 				NO_ARG,	&get_avic_table, 	1 }
1566 	};
1567 
1568 	const struct option null_opt = {
1569 		NULL, 0, NULL, 0
1570 	};
1571 
1572 	struct option *all_opts;
1573 	char *cp;
1574 	int optlen;
1575 
1576 	optlen = sizeof(common_opts);
1577 
1578 	if (cpu_intel)
1579 		optlen += sizeof(intel_opts);
1580 	else
1581 		optlen += sizeof(amd_opts);
1582 
1583 	optlen += sizeof(null_opt);
1584 
1585 	all_opts = malloc(optlen);
1586 
1587 	cp = (char *)all_opts;
1588 	memcpy(cp, common_opts, sizeof(common_opts));
1589 	cp += sizeof(common_opts);
1590 
1591 	if (cpu_intel) {
1592 		memcpy(cp, intel_opts, sizeof(intel_opts));
1593 		cp += sizeof(intel_opts);
1594 	} else {
1595 		memcpy(cp, amd_opts, sizeof(amd_opts));
1596 		cp += sizeof(amd_opts);
1597 	}
1598 
1599 	memcpy(cp, &null_opt, sizeof(null_opt));
1600 	cp += sizeof(null_opt);
1601 
1602 	return (all_opts);
1603 }
1604 
1605 static const char *
1606 wday_str(int idx)
1607 {
1608 	static const char *weekdays[] = {
1609 		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
1610 	};
1611 
1612 	if (idx >= 0 && idx < 7)
1613 		return (weekdays[idx]);
1614 	else
1615 		return ("UNK");
1616 }
1617 
1618 static const char *
1619 mon_str(int idx)
1620 {
1621 	static const char *months[] = {
1622 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
1623 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
1624 	};
1625 
1626 	if (idx >= 0 && idx < 12)
1627 		return (months[idx]);
1628 	else
1629 		return ("UNK");
1630 }
1631 
1632 static int
1633 show_memmap(struct vmctx *ctx)
1634 {
1635 	char name[SPECNAMELEN + 1], numbuf[8];
1636 	vm_ooffset_t segoff;
1637 	vm_paddr_t gpa;
1638 	size_t maplen, seglen;
1639 	int error, flags, prot, segid, delim;
1640 
1641 	printf("Address     Length      Segment     Offset      ");
1642 	printf("Prot  Flags\n");
1643 
1644 	gpa = 0;
1645 	while (1) {
1646 		error = vm_mmap_getnext(ctx, &gpa, &segid, &segoff, &maplen,
1647 		    &prot, &flags);
1648 		if (error)
1649 			return (errno == ENOENT ? 0 : error);
1650 
1651 		error = vm_get_memseg(ctx, segid, &seglen, name, sizeof(name));
1652 		if (error)
1653 			return (error);
1654 
1655 		printf("%-12lX", gpa);
1656 		humanize_number(numbuf, sizeof(numbuf), maplen, "B",
1657 		    HN_AUTOSCALE, HN_NOSPACE);
1658 		printf("%-12s", numbuf);
1659 
1660 		printf("%-12s", name[0] ? name : "sysmem");
1661 		printf("%-12lX", segoff);
1662 		printf("%c%c%c   ", prot & PROT_READ ? 'R' : '-',
1663 		    prot & PROT_WRITE ? 'W' : '-',
1664 		    prot & PROT_EXEC ? 'X' : '-');
1665 
1666 		delim = '\0';
1667 		if (flags & VM_MEMMAP_F_WIRED) {
1668 			printf("%cwired", delim);
1669 			delim = '/';
1670 		}
1671 		if (flags & VM_MEMMAP_F_IOMMU) {
1672 			printf("%ciommu", delim);
1673 			delim = '/';
1674 		}
1675 		printf("\n");
1676 
1677 		gpa += maplen;
1678 	}
1679 }
1680 
1681 static int
1682 show_memseg(struct vmctx *ctx)
1683 {
1684 	char name[SPECNAMELEN + 1], numbuf[8];
1685 	size_t seglen;
1686 	int error, segid;
1687 
1688 	printf("ID  Length      Name\n");
1689 
1690 	segid = 0;
1691 	while (1) {
1692 		error = vm_get_memseg(ctx, segid, &seglen, name, sizeof(name));
1693 		if (error)
1694 			return (errno == EINVAL ? 0 : error);
1695 
1696 		if (seglen) {
1697 			printf("%-4d", segid);
1698 			humanize_number(numbuf, sizeof(numbuf), seglen, "B",
1699 			    HN_AUTOSCALE, HN_NOSPACE);
1700 			printf("%-12s", numbuf);
1701 			printf("%s", name[0] ? name : "sysmem");
1702 			printf("\n");
1703 		}
1704 		segid++;
1705 	}
1706 }
1707 
1708 int
1709 main(int argc, char *argv[])
1710 {
1711 	char *vmname;
1712 	int error, ch, vcpu, ptenum;
1713 	vm_paddr_t gpa_pmap;
1714 	struct vm_exit vmexit;
1715 	uint64_t rax, cr0, cr2, cr3, cr4, dr0, dr1, dr2, dr3, dr6, dr7;
1716 	uint64_t rsp, rip, rflags, efer, pat;
1717 	uint64_t eptp, bm, addr, u64, pteval[4], *pte, info[2];
1718 	struct vmctx *ctx;
1719 	cpuset_t cpus;
1720 	bool cpu_intel;
1721 	uint64_t cs, ds, es, fs, gs, ss, tr, ldtr;
1722 	struct tm tm;
1723 	struct option *opts;
1724 
1725 	cpu_intel = cpu_vendor_intel();
1726 	opts = setup_options(cpu_intel);
1727 
1728 	vcpu = 0;
1729 	vmname = NULL;
1730 	assert_lapic_lvt = -1;
1731 	progname = basename(argv[0]);
1732 
1733 	while ((ch = getopt_long(argc, argv, "", opts, NULL)) != -1) {
1734 		switch (ch) {
1735 		case 0:
1736 			break;
1737 		case VMNAME:
1738 			vmname = optarg;
1739 			break;
1740 		case VCPU:
1741 			vcpu = atoi(optarg);
1742 			break;
1743 		case SET_MEM:
1744 			memsize = atoi(optarg) * MB;
1745 			memsize = roundup(memsize, 2 * MB);
1746 			break;
1747 		case SET_EFER:
1748 			efer = strtoul(optarg, NULL, 0);
1749 			set_efer = 1;
1750 			break;
1751 		case SET_CR0:
1752 			cr0 = strtoul(optarg, NULL, 0);
1753 			set_cr0 = 1;
1754 			break;
1755 		case SET_CR2:
1756 			cr2 = strtoul(optarg, NULL, 0);
1757 			set_cr2 = 1;
1758 			break;
1759 		case SET_CR3:
1760 			cr3 = strtoul(optarg, NULL, 0);
1761 			set_cr3 = 1;
1762 			break;
1763 		case SET_CR4:
1764 			cr4 = strtoul(optarg, NULL, 0);
1765 			set_cr4 = 1;
1766 			break;
1767 		case SET_DR0:
1768 			dr0 = strtoul(optarg, NULL, 0);
1769 			set_dr0 = 1;
1770 			break;
1771 		case SET_DR1:
1772 			dr1 = strtoul(optarg, NULL, 0);
1773 			set_dr1 = 1;
1774 			break;
1775 		case SET_DR2:
1776 			dr2 = strtoul(optarg, NULL, 0);
1777 			set_dr2 = 1;
1778 			break;
1779 		case SET_DR3:
1780 			dr3 = strtoul(optarg, NULL, 0);
1781 			set_dr3 = 1;
1782 			break;
1783 		case SET_DR6:
1784 			dr6 = strtoul(optarg, NULL, 0);
1785 			set_dr6 = 1;
1786 			break;
1787 		case SET_DR7:
1788 			dr7 = strtoul(optarg, NULL, 0);
1789 			set_dr7 = 1;
1790 			break;
1791 		case SET_RSP:
1792 			rsp = strtoul(optarg, NULL, 0);
1793 			set_rsp = 1;
1794 			break;
1795 		case SET_RIP:
1796 			rip = strtoul(optarg, NULL, 0);
1797 			set_rip = 1;
1798 			break;
1799 		case SET_RAX:
1800 			rax = strtoul(optarg, NULL, 0);
1801 			set_rax = 1;
1802 			break;
1803 		case SET_RFLAGS:
1804 			rflags = strtoul(optarg, NULL, 0);
1805 			set_rflags = 1;
1806 			break;
1807 		case DESC_BASE:
1808 			desc_base = strtoul(optarg, NULL, 0);
1809 			break;
1810 		case DESC_LIMIT:
1811 			desc_limit = strtoul(optarg, NULL, 0);
1812 			break;
1813 		case DESC_ACCESS:
1814 			desc_access = strtoul(optarg, NULL, 0);
1815 			break;
1816 		case SET_CS:
1817 			cs = strtoul(optarg, NULL, 0);
1818 			set_cs = 1;
1819 			break;
1820 		case SET_DS:
1821 			ds = strtoul(optarg, NULL, 0);
1822 			set_ds = 1;
1823 			break;
1824 		case SET_ES:
1825 			es = strtoul(optarg, NULL, 0);
1826 			set_es = 1;
1827 			break;
1828 		case SET_FS:
1829 			fs = strtoul(optarg, NULL, 0);
1830 			set_fs = 1;
1831 			break;
1832 		case SET_GS:
1833 			gs = strtoul(optarg, NULL, 0);
1834 			set_gs = 1;
1835 			break;
1836 		case SET_SS:
1837 			ss = strtoul(optarg, NULL, 0);
1838 			set_ss = 1;
1839 			break;
1840 		case SET_TR:
1841 			tr = strtoul(optarg, NULL, 0);
1842 			set_tr = 1;
1843 			break;
1844 		case SET_LDTR:
1845 			ldtr = strtoul(optarg, NULL, 0);
1846 			set_ldtr = 1;
1847 			break;
1848 		case SET_X2APIC_STATE:
1849 			x2apic_state = strtol(optarg, NULL, 0);
1850 			set_x2apic_state = 1;
1851 			break;
1852 		case SET_EXCEPTION_BITMAP:
1853 			exception_bitmap = strtoul(optarg, NULL, 0);
1854 			set_exception_bitmap = 1;
1855 			break;
1856 		case SET_VMCS_ENTRY_INTERRUPTION_INFO:
1857 			vmcs_entry_interruption_info = strtoul(optarg, NULL, 0);
1858 			set_vmcs_entry_interruption_info = 1;
1859 			break;
1860 		case SET_CAP:
1861 			capval = strtoul(optarg, NULL, 0);
1862 			setcap = 1;
1863 			break;
1864 		case SET_RTC_TIME:
1865 			rtc_secs = strtoul(optarg, NULL, 0);
1866 			set_rtc_time = 1;
1867 			break;
1868 		case SET_RTC_NVRAM:
1869 			rtc_nvram_value = (uint8_t)strtoul(optarg, NULL, 0);
1870 			set_rtc_nvram = 1;
1871 			break;
1872 		case RTC_NVRAM_OFFSET:
1873 			rtc_nvram_offset = strtoul(optarg, NULL, 0);
1874 			break;
1875 		case GET_GPA_PMAP:
1876 			gpa_pmap = strtoul(optarg, NULL, 0);
1877 			get_gpa_pmap = 1;
1878 			break;
1879 		case CAPNAME:
1880 			capname = optarg;
1881 			break;
1882 #ifdef __FreeBSD__
1883 		case UNASSIGN_PPTDEV:
1884 			unassign_pptdev = 1;
1885 			if (sscanf(optarg, "%d/%d/%d", &bus, &slot, &func) != 3)
1886 				usage(cpu_intel);
1887 			break;
1888 #endif
1889 		case ASSERT_LAPIC_LVT:
1890 			assert_lapic_lvt = atoi(optarg);
1891 			break;
1892 		default:
1893 			usage(cpu_intel);
1894 		}
1895 	}
1896 	argc -= optind;
1897 	argv += optind;
1898 
1899 	if (vmname == NULL)
1900 		usage(cpu_intel);
1901 
1902 	error = 0;
1903 
1904 	if (!error && create)
1905 		error = vm_create(vmname);
1906 
1907 	if (!error) {
1908 		ctx = vm_open(vmname);
1909 		if (ctx == NULL) {
1910 			printf("VM:%s is not created.\n", vmname);
1911 			exit (1);
1912 		}
1913 	}
1914 
1915 #ifndef __FreeBSD__
1916 	if (!error && wrlock_cycle) {
1917 		error = vm_wrlock_cycle(ctx);
1918 		exit(error);
1919 	}
1920 #endif /* __FreeBSD__ */
1921 
1922 	if (!error && memsize)
1923 		error = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
1924 
1925 	if (!error && set_efer)
1926 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_EFER, efer);
1927 
1928 	if (!error && set_cr0)
1929 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CR0, cr0);
1930 
1931 	if (!error && set_cr2)
1932 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CR2, cr2);
1933 
1934 	if (!error && set_cr3)
1935 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CR3, cr3);
1936 
1937 	if (!error && set_cr4)
1938 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CR4, cr4);
1939 
1940 	if (!error && set_dr0)
1941 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DR0, dr0);
1942 
1943 	if (!error && set_dr1)
1944 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DR1, dr1);
1945 
1946 	if (!error && set_dr2)
1947 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DR2, dr2);
1948 
1949 	if (!error && set_dr3)
1950 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DR3, dr3);
1951 
1952 	if (!error && set_dr6)
1953 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DR6, dr6);
1954 
1955 	if (!error && set_dr7)
1956 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DR7, dr7);
1957 
1958 	if (!error && set_rsp)
1959 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RSP, rsp);
1960 
1961 	if (!error && set_rip)
1962 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RIP, rip);
1963 
1964 	if (!error && set_rax)
1965 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX, rax);
1966 
1967 	if (!error && set_rflags) {
1968 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RFLAGS,
1969 					rflags);
1970 	}
1971 
1972 	if (!error && set_desc_ds) {
1973 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_DS,
1974 				    desc_base, desc_limit, desc_access);
1975 	}
1976 
1977 	if (!error && set_desc_es) {
1978 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_ES,
1979 				    desc_base, desc_limit, desc_access);
1980 	}
1981 
1982 	if (!error && set_desc_ss) {
1983 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_SS,
1984 				    desc_base, desc_limit, desc_access);
1985 	}
1986 
1987 	if (!error && set_desc_cs) {
1988 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_CS,
1989 				    desc_base, desc_limit, desc_access);
1990 	}
1991 
1992 	if (!error && set_desc_fs) {
1993 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_FS,
1994 				    desc_base, desc_limit, desc_access);
1995 	}
1996 
1997 	if (!error && set_desc_gs) {
1998 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_GS,
1999 				    desc_base, desc_limit, desc_access);
2000 	}
2001 
2002 	if (!error && set_desc_tr) {
2003 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_TR,
2004 				    desc_base, desc_limit, desc_access);
2005 	}
2006 
2007 	if (!error && set_desc_ldtr) {
2008 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_LDTR,
2009 				    desc_base, desc_limit, desc_access);
2010 	}
2011 
2012 	if (!error && set_desc_gdtr) {
2013 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_GDTR,
2014 				    desc_base, desc_limit, 0);
2015 	}
2016 
2017 	if (!error && set_desc_idtr) {
2018 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_IDTR,
2019 				    desc_base, desc_limit, 0);
2020 	}
2021 
2022 	if (!error && set_cs)
2023 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CS, cs);
2024 
2025 	if (!error && set_ds)
2026 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DS, ds);
2027 
2028 	if (!error && set_es)
2029 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_ES, es);
2030 
2031 	if (!error && set_fs)
2032 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_FS, fs);
2033 
2034 	if (!error && set_gs)
2035 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_GS, gs);
2036 
2037 	if (!error && set_ss)
2038 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_SS, ss);
2039 
2040 	if (!error && set_tr)
2041 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_TR, tr);
2042 
2043 	if (!error && set_ldtr)
2044 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_LDTR, ldtr);
2045 
2046 	if (!error && set_x2apic_state)
2047 		error = vm_set_x2apic_state(ctx, vcpu, x2apic_state);
2048 
2049 #ifdef __FreeBSD__
2050 	if (!error && unassign_pptdev)
2051 		error = vm_unassign_pptdev(ctx, bus, slot, func);
2052 #endif /* __FreeBSD__ */
2053 
2054 	if (!error && set_exception_bitmap) {
2055 		if (cpu_intel)
2056 			error = vm_set_vmcs_field(ctx, vcpu,
2057 						  VMCS_EXCEPTION_BITMAP,
2058 						  exception_bitmap);
2059 		else
2060 			error = vm_set_vmcb_field(ctx, vcpu,
2061 						  VMCB_OFF_EXC_INTERCEPT,
2062 						  4, exception_bitmap);
2063 	}
2064 
2065 	if (!error && cpu_intel && set_vmcs_entry_interruption_info) {
2066 		error = vm_set_vmcs_field(ctx, vcpu, VMCS_ENTRY_INTR_INFO,
2067 					  vmcs_entry_interruption_info);
2068 	}
2069 
2070 	if (!error && inject_nmi) {
2071 		error = vm_inject_nmi(ctx, vcpu);
2072 	}
2073 
2074 	if (!error && assert_lapic_lvt != -1) {
2075 		error = vm_lapic_local_irq(ctx, vcpu, assert_lapic_lvt);
2076 	}
2077 
2078 	if (!error && (get_memseg || get_all))
2079 		error = show_memseg(ctx);
2080 
2081 	if (!error && (get_memmap || get_all))
2082 		error = show_memmap(ctx);
2083 
2084 	if (!error)
2085 		error = get_all_registers(ctx, vcpu);
2086 
2087 	if (!error)
2088 		error = get_all_segments(ctx, vcpu);
2089 
2090 	if (!error) {
2091 		if (cpu_intel)
2092 			error = get_misc_vmcs(ctx, vcpu);
2093 		else
2094 			error = get_misc_vmcb(ctx, vcpu);
2095 	}
2096 
2097 	if (!error && (get_x2apic_state || get_all)) {
2098 		error = vm_get_x2apic_state(ctx, vcpu, &x2apic_state);
2099 		if (error == 0)
2100 			printf("x2apic_state[%d]\t%d\n", vcpu, x2apic_state);
2101 	}
2102 
2103 	if (!error && (get_eptp || get_all)) {
2104 		if (cpu_intel)
2105 			error = vm_get_vmcs_field(ctx, vcpu, VMCS_EPTP, &eptp);
2106 		else
2107 			error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_NPT_BASE,
2108 						   8, &eptp);
2109 		if (error == 0)
2110 			printf("%s[%d]\t\t0x%016lx\n",
2111 				cpu_intel ? "eptp" : "rvi/npt", vcpu, eptp);
2112 	}
2113 
2114 	if (!error && (get_exception_bitmap || get_all)) {
2115 		if(cpu_intel)
2116 			error = vm_get_vmcs_field(ctx, vcpu,
2117 						VMCS_EXCEPTION_BITMAP, &bm);
2118 		else
2119 			error = vm_get_vmcb_field(ctx, vcpu,
2120 						  VMCB_OFF_EXC_INTERCEPT,
2121 						  4, &bm);
2122 		if (error == 0)
2123 			printf("exception_bitmap[%d]\t%#lx\n", vcpu, bm);
2124 	}
2125 
2126 	if (!error && (get_io_bitmap || get_all)) {
2127 		if (cpu_intel) {
2128 			error = vm_get_vmcs_field(ctx, vcpu, VMCS_IO_BITMAP_A,
2129 						  &bm);
2130 			if (error == 0)
2131 				printf("io_bitmap_a[%d]\t%#lx\n", vcpu, bm);
2132 			error = vm_get_vmcs_field(ctx, vcpu, VMCS_IO_BITMAP_B,
2133 						  &bm);
2134 			if (error == 0)
2135 				printf("io_bitmap_b[%d]\t%#lx\n", vcpu, bm);
2136 		} else {
2137 			error = vm_get_vmcb_field(ctx, vcpu,
2138 						  VMCB_OFF_IO_PERM, 8, &bm);
2139 			if (error == 0)
2140 				printf("io_bitmap[%d]\t%#lx\n", vcpu, bm);
2141 		}
2142 	}
2143 
2144 	if (!error && (get_tsc_offset || get_all)) {
2145 		uint64_t tscoff;
2146 		if (cpu_intel)
2147 			error = vm_get_vmcs_field(ctx, vcpu, VMCS_TSC_OFFSET,
2148 						  &tscoff);
2149 		else
2150 			error = vm_get_vmcb_field(ctx, vcpu,
2151 						  VMCB_OFF_TSC_OFFSET,
2152 						  8, &tscoff);
2153 		if (error == 0)
2154 			printf("tsc_offset[%d]\t0x%016lx\n", vcpu, tscoff);
2155 	}
2156 
2157 	if (!error && (get_msr_bitmap_address || get_all)) {
2158 		if (cpu_intel)
2159 			error = vm_get_vmcs_field(ctx, vcpu, VMCS_MSR_BITMAP,
2160 						  &addr);
2161 		else
2162 			error = vm_get_vmcb_field(ctx, vcpu,
2163 						  VMCB_OFF_MSR_PERM, 8, &addr);
2164 		if (error == 0)
2165 			printf("msr_bitmap[%d]\t\t%#lx\n", vcpu, addr);
2166 	}
2167 
2168 	if (!error && (get_msr_bitmap || get_all)) {
2169 		if (cpu_intel) {
2170 			error = vm_get_vmcs_field(ctx, vcpu,
2171 						  VMCS_MSR_BITMAP, &addr);
2172 		} else {
2173 			error = vm_get_vmcb_field(ctx, vcpu,
2174 						  VMCB_OFF_MSR_PERM, 8,
2175 						  &addr);
2176 		}
2177 
2178 		if (error == 0)
2179 			error = dump_msr_bitmap(vcpu, addr, cpu_intel);
2180 	}
2181 
2182 	if (!error && (get_vpid_asid || get_all)) {
2183 		uint64_t vpid;
2184 		if (cpu_intel)
2185 			error = vm_get_vmcs_field(ctx, vcpu, VMCS_VPID, &vpid);
2186 		else
2187 			error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_ASID,
2188 						  4, &vpid);
2189 		if (error == 0)
2190 			printf("%s[%d]\t\t0x%04lx\n",
2191 				cpu_intel ? "vpid" : "asid", vcpu, vpid);
2192 	}
2193 
2194 	if (!error && (get_guest_pat || get_all)) {
2195 		if (cpu_intel)
2196 			error = vm_get_vmcs_field(ctx, vcpu,
2197 						  VMCS_GUEST_IA32_PAT, &pat);
2198 		else
2199 			error = vm_get_vmcb_field(ctx, vcpu,
2200 						  VMCB_OFF_GUEST_PAT, 8, &pat);
2201 		if (error == 0)
2202 			printf("guest_pat[%d]\t\t0x%016lx\n", vcpu, pat);
2203 	}
2204 
2205 	if (!error && (get_guest_sysenter || get_all)) {
2206 		if (cpu_intel)
2207 			error = vm_get_vmcs_field(ctx, vcpu,
2208 						  VMCS_GUEST_IA32_SYSENTER_CS,
2209 						  &cs);
2210 		else
2211 			error = vm_get_vmcb_field(ctx, vcpu,
2212 						  VMCB_OFF_SYSENTER_CS, 8,
2213 						  &cs);
2214 
2215 		if (error == 0)
2216 			printf("guest_sysenter_cs[%d]\t%#lx\n", vcpu, cs);
2217 		if (cpu_intel)
2218 			error = vm_get_vmcs_field(ctx, vcpu,
2219 						  VMCS_GUEST_IA32_SYSENTER_ESP,
2220 						  &rsp);
2221 		else
2222 			error = vm_get_vmcb_field(ctx, vcpu,
2223 						  VMCB_OFF_SYSENTER_ESP, 8,
2224 						  &rsp);
2225 
2226 		if (error == 0)
2227 			printf("guest_sysenter_sp[%d]\t%#lx\n", vcpu, rsp);
2228 		if (cpu_intel)
2229 			error = vm_get_vmcs_field(ctx, vcpu,
2230 						  VMCS_GUEST_IA32_SYSENTER_EIP,
2231 						  &rip);
2232 		else
2233 			error = vm_get_vmcb_field(ctx, vcpu,
2234 						  VMCB_OFF_SYSENTER_EIP, 8,
2235 						  &rip);
2236 		if (error == 0)
2237 			printf("guest_sysenter_ip[%d]\t%#lx\n", vcpu, rip);
2238 	}
2239 
2240 	if (!error && (get_exit_reason || get_all)) {
2241 		if (cpu_intel)
2242 			error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_REASON,
2243 						  &u64);
2244 		else
2245 			error = vm_get_vmcb_field(ctx, vcpu,
2246 						  VMCB_OFF_EXIT_REASON, 8,
2247 						  &u64);
2248 		if (error == 0)
2249 			printf("exit_reason[%d]\t%#lx\n", vcpu, u64);
2250 	}
2251 
2252 	if (!error && setcap) {
2253 		int captype;
2254 		captype = vm_capability_name2type(capname);
2255 		error = vm_set_capability(ctx, vcpu, captype, capval);
2256 		if (error != 0 && errno == ENOENT)
2257 			printf("Capability \"%s\" is not available\n", capname);
2258 	}
2259 
2260 	if (!error && get_gpa_pmap) {
2261 		error = vm_get_gpa_pmap(ctx, gpa_pmap, pteval, &ptenum);
2262 		if (error == 0) {
2263 			printf("gpa %#lx:", gpa_pmap);
2264 			pte = &pteval[0];
2265 			while (ptenum-- > 0)
2266 				printf(" %#lx", *pte++);
2267 			printf("\n");
2268 		}
2269 	}
2270 
2271 	if (!error && set_rtc_nvram)
2272 		error = vm_rtc_write(ctx, rtc_nvram_offset, rtc_nvram_value);
2273 
2274 	if (!error && (get_rtc_nvram || get_all)) {
2275 		error = vm_rtc_read(ctx, rtc_nvram_offset, &rtc_nvram_value);
2276 		if (error == 0) {
2277 			printf("rtc nvram[%03d]: 0x%02x\n", rtc_nvram_offset,
2278 			    rtc_nvram_value);
2279 		}
2280 	}
2281 
2282 	if (!error && set_rtc_time)
2283 		error = vm_rtc_settime(ctx, rtc_secs);
2284 
2285 	if (!error && (get_rtc_time || get_all)) {
2286 		error = vm_rtc_gettime(ctx, &rtc_secs);
2287 		if (error == 0) {
2288 			gmtime_r(&rtc_secs, &tm);
2289 			printf("rtc time %#lx: %s %s %02d %02d:%02d:%02d %d\n",
2290 			    rtc_secs, wday_str(tm.tm_wday), mon_str(tm.tm_mon),
2291 			    tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
2292 			    1900 + tm.tm_year);
2293 		}
2294 	}
2295 
2296 	if (!error && (getcap || get_all)) {
2297 		int captype, val, getcaptype;
2298 
2299 		if (getcap && capname)
2300 			getcaptype = vm_capability_name2type(capname);
2301 		else
2302 			getcaptype = -1;
2303 
2304 		for (captype = 0; captype < VM_CAP_MAX; captype++) {
2305 			if (getcaptype >= 0 && captype != getcaptype)
2306 				continue;
2307 			error = vm_get_capability(ctx, vcpu, captype, &val);
2308 			if (error == 0) {
2309 				printf("Capability \"%s\" is %s on vcpu %d\n",
2310 					vm_capability_type2name(captype),
2311 					val ? "set" : "not set", vcpu);
2312 			} else if (errno == ENOENT) {
2313 				error = 0;
2314 				printf("Capability \"%s\" is not available\n",
2315 					vm_capability_type2name(captype));
2316 			} else {
2317 				break;
2318 			}
2319 		}
2320 	}
2321 
2322 	if (!error && (get_active_cpus || get_all)) {
2323 		error = vm_active_cpus(ctx, &cpus);
2324 		if (!error)
2325 			print_cpus("active cpus", &cpus);
2326 	}
2327 
2328 	if (!error && (get_suspended_cpus || get_all)) {
2329 		error = vm_suspended_cpus(ctx, &cpus);
2330 		if (!error)
2331 			print_cpus("suspended cpus", &cpus);
2332 	}
2333 
2334 	if (!error && (get_intinfo || get_all)) {
2335 		error = vm_get_intinfo(ctx, vcpu, &info[0], &info[1]);
2336 		if (!error) {
2337 			print_intinfo("pending", info[0]);
2338 			print_intinfo("current", info[1]);
2339 		}
2340 	}
2341 
2342 	if (!error && (get_stats || get_all)) {
2343 		int i, num_stats;
2344 		uint64_t *stats;
2345 		struct timeval tv;
2346 		const char *desc;
2347 
2348 		stats = vm_get_stats(ctx, vcpu, &tv, &num_stats);
2349 		if (stats != NULL) {
2350 			printf("vcpu%d stats:\n", vcpu);
2351 			for (i = 0; i < num_stats; i++) {
2352 				desc = vm_get_stat_desc(ctx, i);
2353 				printf("%-40s\t%ld\n", desc, stats[i]);
2354 			}
2355 		}
2356 	}
2357 
2358 	if (!error && (get_cpu_topology || get_all)) {
2359 		uint16_t sockets, cores, threads, maxcpus;
2360 
2361 		vm_get_topology(ctx, &sockets, &cores, &threads, &maxcpus);
2362 		printf("cpu_topology:\tsockets=%hu, cores=%hu, threads=%hu, "
2363 		    "maxcpus=%hu\n", sockets, cores, threads, maxcpus);
2364 	}
2365 
2366 	if (!error && run) {
2367 		error = vm_run(ctx, vcpu, &vmexit);
2368 		if (error == 0)
2369 			dump_vm_run_exitcode(&vmexit, vcpu);
2370 		else
2371 			printf("vm_run error %d\n", error);
2372 	}
2373 
2374 	if (!error && force_reset)
2375 		error = vm_suspend(ctx, VM_SUSPEND_RESET);
2376 
2377 	if (!error && force_poweroff)
2378 		error = vm_suspend(ctx, VM_SUSPEND_POWEROFF);
2379 
2380 	if (error)
2381 		printf("errno = %d\n", errno);
2382 
2383 	if (!error && destroy)
2384 		vm_destroy(ctx);
2385 
2386 	free (opts);
2387 	exit(error);
2388 }
2389