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