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