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