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