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