xref: /illumos-gate/usr/src/cmd/bhyvectl/bhyvectl.c (revision f0a052391861a2b96cf28973c3b7f2854591aa79)
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 #ifdef __FreeBSD__
584 static int
585 vm_get_vmcb_field(struct vmctx *ctx, int vcpu, int off, int bytes,
586 	uint64_t *ret_val)
587 {
588 
589 	return (vm_get_register(ctx, vcpu, VMCB_ACCESS(off, bytes), ret_val));
590 }
591 
592 static int
593 vm_set_vmcb_field(struct vmctx *ctx, int vcpu, int off, int bytes,
594 	uint64_t val)
595 {
596 
597 	return (vm_set_register(ctx, vcpu, VMCB_ACCESS(off, bytes), val));
598 }
599 #else /* __FreeBSD__ */
600 /* Arbitrary VMCB read/write is not allowed */
601 static int
602 vm_get_vmcb_field(struct vmctx *ctx, int vcpu, int off, int bytes,
603 	uint64_t *ret_val)
604 {
605 	*ret_val = 0;
606 	return (0);
607 }
608 
609 static int
610 vm_set_vmcb_field(struct vmctx *ctx, int vcpu, int off, int bytes,
611 	uint64_t val)
612 {
613 	return (EINVAL);
614 }
615 #endif /* __FreeBSD__ */
616 
617 enum {
618 	VMNAME = 1000,	/* avoid collision with return values from getopt */
619 	VCPU,
620 	SET_MEM,
621 	SET_EFER,
622 	SET_CR0,
623 	SET_CR2,
624 	SET_CR3,
625 	SET_CR4,
626 	SET_DR0,
627 	SET_DR1,
628 	SET_DR2,
629 	SET_DR3,
630 	SET_DR6,
631 	SET_DR7,
632 	SET_RSP,
633 	SET_RIP,
634 	SET_RAX,
635 	SET_RFLAGS,
636 	DESC_BASE,
637 	DESC_LIMIT,
638 	DESC_ACCESS,
639 	SET_CS,
640 	SET_DS,
641 	SET_ES,
642 	SET_FS,
643 	SET_GS,
644 	SET_SS,
645 	SET_TR,
646 	SET_LDTR,
647 	SET_X2APIC_STATE,
648 	SET_EXCEPTION_BITMAP,
649 	SET_VMCS_ENTRY_INTERRUPTION_INFO,
650 	SET_CAP,
651 	CAPNAME,
652 	UNASSIGN_PPTDEV,
653 	GET_GPA_PMAP,
654 	ASSERT_LAPIC_LVT,
655 	SET_RTC_TIME,
656 	SET_RTC_NVRAM,
657 	RTC_NVRAM_OFFSET,
658 };
659 
660 static void
661 print_cpus(const char *banner, const cpuset_t *cpus)
662 {
663 	int i;
664 	int first;
665 
666 	first = 1;
667 	printf("%s:\t", banner);
668 	if (!CPU_EMPTY(cpus)) {
669 		for (i = 0; i < CPU_SETSIZE; i++) {
670 			if (CPU_ISSET(i, cpus)) {
671 				printf("%s%d", first ? " " : ", ", i);
672 				first = 0;
673 			}
674 		}
675 	} else
676 		printf(" (none)");
677 	printf("\n");
678 }
679 
680 static void
681 print_intinfo(const char *banner, uint64_t info)
682 {
683 	int type;
684 
685 	printf("%s:\t", banner);
686 	if (info & VM_INTINFO_VALID) {
687 		type = info & VM_INTINFO_TYPE;
688 		switch (type) {
689 		case VM_INTINFO_HWINTR:
690 			printf("extint");
691 			break;
692 		case VM_INTINFO_NMI:
693 			printf("nmi");
694 			break;
695 		case VM_INTINFO_SWINTR:
696 			printf("swint");
697 			break;
698 		default:
699 			printf("exception");
700 			break;
701 		}
702 		printf(" vector %d", (int)VM_INTINFO_VECTOR(info));
703 		if (info & VM_INTINFO_DEL_ERRCODE)
704 			printf(" errcode %#x", (u_int)(info >> 32));
705 	} else {
706 		printf("n/a");
707 	}
708 	printf("\n");
709 }
710 
711 static bool
712 cpu_vendor_intel(void)
713 {
714 	u_int regs[4];
715 	char cpu_vendor[13];
716 
717 	do_cpuid(0, regs);
718 	((u_int *)&cpu_vendor)[0] = regs[1];
719 	((u_int *)&cpu_vendor)[1] = regs[3];
720 	((u_int *)&cpu_vendor)[2] = regs[2];
721 	cpu_vendor[12] = '\0';
722 
723 	if (strcmp(cpu_vendor, "AuthenticAMD") == 0) {
724 		return (false);
725 	} else if (strcmp(cpu_vendor, "HygonGenuine") == 0) {
726 		return (false);
727 	} else if (strcmp(cpu_vendor, "GenuineIntel") == 0) {
728 		return (true);
729 	} else {
730 		fprintf(stderr, "Unknown cpu vendor \"%s\"\n", cpu_vendor);
731 		exit(1);
732 	}
733 }
734 
735 static int
736 get_all_registers(struct vmctx *ctx, int vcpu)
737 {
738 	uint64_t cr0, cr2, cr3, cr4, dr0, dr1, dr2, dr3, dr6, dr7;
739 	uint64_t rsp, rip, rflags, efer;
740 	uint64_t rax, rbx, rcx, rdx, rsi, rdi, rbp;
741 	uint64_t r8, r9, r10, r11, r12, r13, r14, r15;
742 	int error = 0;
743 
744 	if (!error && (get_efer || get_all)) {
745 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_EFER, &efer);
746 		if (error == 0)
747 			printf("efer[%d]\t\t0x%016lx\n", vcpu, efer);
748 	}
749 
750 	if (!error && (get_cr0 || get_all)) {
751 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CR0, &cr0);
752 		if (error == 0)
753 			printf("cr0[%d]\t\t0x%016lx\n", vcpu, cr0);
754 	}
755 
756 	if (!error && (get_cr2 || get_all)) {
757 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CR2, &cr2);
758 		if (error == 0)
759 			printf("cr2[%d]\t\t0x%016lx\n", vcpu, cr2);
760 	}
761 
762 	if (!error && (get_cr3 || get_all)) {
763 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CR3, &cr3);
764 		if (error == 0)
765 			printf("cr3[%d]\t\t0x%016lx\n", vcpu, cr3);
766 	}
767 
768 	if (!error && (get_cr4 || get_all)) {
769 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CR4, &cr4);
770 		if (error == 0)
771 			printf("cr4[%d]\t\t0x%016lx\n", vcpu, cr4);
772 	}
773 
774 	if (!error && (get_dr0 || get_all)) {
775 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DR0, &dr0);
776 		if (error == 0)
777 			printf("dr0[%d]\t\t0x%016lx\n", vcpu, dr0);
778 	}
779 
780 	if (!error && (get_dr1 || get_all)) {
781 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DR1, &dr1);
782 		if (error == 0)
783 			printf("dr1[%d]\t\t0x%016lx\n", vcpu, dr1);
784 	}
785 
786 	if (!error && (get_dr2 || get_all)) {
787 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DR2, &dr2);
788 		if (error == 0)
789 			printf("dr2[%d]\t\t0x%016lx\n", vcpu, dr2);
790 	}
791 
792 	if (!error && (get_dr3 || get_all)) {
793 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DR3, &dr3);
794 		if (error == 0)
795 			printf("dr3[%d]\t\t0x%016lx\n", vcpu, dr3);
796 	}
797 
798 	if (!error && (get_dr6 || get_all)) {
799 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DR6, &dr6);
800 		if (error == 0)
801 			printf("dr6[%d]\t\t0x%016lx\n", vcpu, dr6);
802 	}
803 
804 	if (!error && (get_dr7 || get_all)) {
805 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DR7, &dr7);
806 		if (error == 0)
807 			printf("dr7[%d]\t\t0x%016lx\n", vcpu, dr7);
808 	}
809 
810 	if (!error && (get_rsp || get_all)) {
811 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RSP, &rsp);
812 		if (error == 0)
813 			printf("rsp[%d]\t\t0x%016lx\n", vcpu, rsp);
814 	}
815 
816 	if (!error && (get_rip || get_all)) {
817 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RIP, &rip);
818 		if (error == 0)
819 			printf("rip[%d]\t\t0x%016lx\n", vcpu, rip);
820 	}
821 
822 	if (!error && (get_rax || get_all)) {
823 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RAX, &rax);
824 		if (error == 0)
825 			printf("rax[%d]\t\t0x%016lx\n", vcpu, rax);
826 	}
827 
828 	if (!error && (get_rbx || get_all)) {
829 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RBX, &rbx);
830 		if (error == 0)
831 			printf("rbx[%d]\t\t0x%016lx\n", vcpu, rbx);
832 	}
833 
834 	if (!error && (get_rcx || get_all)) {
835 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RCX, &rcx);
836 		if (error == 0)
837 			printf("rcx[%d]\t\t0x%016lx\n", vcpu, rcx);
838 	}
839 
840 	if (!error && (get_rdx || get_all)) {
841 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RDX, &rdx);
842 		if (error == 0)
843 			printf("rdx[%d]\t\t0x%016lx\n", vcpu, rdx);
844 	}
845 
846 	if (!error && (get_rsi || get_all)) {
847 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RSI, &rsi);
848 		if (error == 0)
849 			printf("rsi[%d]\t\t0x%016lx\n", vcpu, rsi);
850 	}
851 
852 	if (!error && (get_rdi || get_all)) {
853 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RDI, &rdi);
854 		if (error == 0)
855 			printf("rdi[%d]\t\t0x%016lx\n", vcpu, rdi);
856 	}
857 
858 	if (!error && (get_rbp || get_all)) {
859 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RBP, &rbp);
860 		if (error == 0)
861 			printf("rbp[%d]\t\t0x%016lx\n", vcpu, rbp);
862 	}
863 
864 	if (!error && (get_r8 || get_all)) {
865 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R8, &r8);
866 		if (error == 0)
867 			printf("r8[%d]\t\t0x%016lx\n", vcpu, r8);
868 	}
869 
870 	if (!error && (get_r9 || get_all)) {
871 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R9, &r9);
872 		if (error == 0)
873 			printf("r9[%d]\t\t0x%016lx\n", vcpu, r9);
874 	}
875 
876 	if (!error && (get_r10 || get_all)) {
877 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R10, &r10);
878 		if (error == 0)
879 			printf("r10[%d]\t\t0x%016lx\n", vcpu, r10);
880 	}
881 
882 	if (!error && (get_r11 || get_all)) {
883 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R11, &r11);
884 		if (error == 0)
885 			printf("r11[%d]\t\t0x%016lx\n", vcpu, r11);
886 	}
887 
888 	if (!error && (get_r12 || get_all)) {
889 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R12, &r12);
890 		if (error == 0)
891 			printf("r12[%d]\t\t0x%016lx\n", vcpu, r12);
892 	}
893 
894 	if (!error && (get_r13 || get_all)) {
895 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R13, &r13);
896 		if (error == 0)
897 			printf("r13[%d]\t\t0x%016lx\n", vcpu, r13);
898 	}
899 
900 	if (!error && (get_r14 || get_all)) {
901 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R14, &r14);
902 		if (error == 0)
903 			printf("r14[%d]\t\t0x%016lx\n", vcpu, r14);
904 	}
905 
906 	if (!error && (get_r15 || get_all)) {
907 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_R15, &r15);
908 		if (error == 0)
909 			printf("r15[%d]\t\t0x%016lx\n", vcpu, r15);
910 	}
911 
912 	if (!error && (get_rflags || get_all)) {
913 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_RFLAGS,
914 					&rflags);
915 		if (error == 0)
916 			printf("rflags[%d]\t0x%016lx\n", vcpu, rflags);
917 	}
918 
919 	return (error);
920 }
921 
922 static int
923 get_all_segments(struct vmctx *ctx, int vcpu)
924 {
925 	uint64_t cs, ds, es, fs, gs, ss, tr, ldtr;
926 	int error = 0;
927 
928 	if (!error && (get_desc_ds || get_all)) {
929 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_DS,
930 				   &desc_base, &desc_limit, &desc_access);
931 		if (error == 0) {
932 			printf("ds 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_es || get_all)) {
938 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_ES,
939 				    &desc_base, &desc_limit, &desc_access);
940 		if (error == 0) {
941 			printf("es 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_fs || get_all)) {
947 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_FS,
948 				    &desc_base, &desc_limit, &desc_access);
949 		if (error == 0) {
950 			printf("fs 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_gs || get_all)) {
956 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_GS,
957 				    &desc_base, &desc_limit, &desc_access);
958 		if (error == 0) {
959 			printf("gs 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_ss || get_all)) {
965 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_SS,
966 				    &desc_base, &desc_limit, &desc_access);
967 		if (error == 0) {
968 			printf("ss 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_cs || get_all)) {
974 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_CS,
975 				    &desc_base, &desc_limit, &desc_access);
976 		if (error == 0) {
977 			printf("cs 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_tr || get_all)) {
983 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_TR,
984 				    &desc_base, &desc_limit, &desc_access);
985 		if (error == 0) {
986 			printf("tr desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
987 			       vcpu, desc_base, desc_limit, desc_access);
988 		}
989 	}
990 
991 	if (!error && (get_desc_ldtr || get_all)) {
992 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_LDTR,
993 				    &desc_base, &desc_limit, &desc_access);
994 		if (error == 0) {
995 			printf("ldtr desc[%d]\t0x%016lx/0x%08x/0x%08x\n",
996 			       vcpu, desc_base, desc_limit, desc_access);
997 		}
998 	}
999 
1000 	if (!error && (get_desc_gdtr || get_all)) {
1001 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_GDTR,
1002 				    &desc_base, &desc_limit, &desc_access);
1003 		if (error == 0) {
1004 			printf("gdtr[%d]\t\t0x%016lx/0x%08x\n",
1005 			       vcpu, desc_base, desc_limit);
1006 		}
1007 	}
1008 
1009 	if (!error && (get_desc_idtr || get_all)) {
1010 		error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_IDTR,
1011 				    &desc_base, &desc_limit, &desc_access);
1012 		if (error == 0) {
1013 			printf("idtr[%d]\t\t0x%016lx/0x%08x\n",
1014 			       vcpu, desc_base, desc_limit);
1015 		}
1016 	}
1017 
1018 	if (!error && (get_cs || get_all)) {
1019 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_CS, &cs);
1020 		if (error == 0)
1021 			printf("cs[%d]\t\t0x%04lx\n", vcpu, cs);
1022 	}
1023 
1024 	if (!error && (get_ds || get_all)) {
1025 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_DS, &ds);
1026 		if (error == 0)
1027 			printf("ds[%d]\t\t0x%04lx\n", vcpu, ds);
1028 	}
1029 
1030 	if (!error && (get_es || get_all)) {
1031 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_ES, &es);
1032 		if (error == 0)
1033 			printf("es[%d]\t\t0x%04lx\n", vcpu, es);
1034 	}
1035 
1036 	if (!error && (get_fs || get_all)) {
1037 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_FS, &fs);
1038 		if (error == 0)
1039 			printf("fs[%d]\t\t0x%04lx\n", vcpu, fs);
1040 	}
1041 
1042 	if (!error && (get_gs || get_all)) {
1043 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_GS, &gs);
1044 		if (error == 0)
1045 			printf("gs[%d]\t\t0x%04lx\n", vcpu, gs);
1046 	}
1047 
1048 	if (!error && (get_ss || get_all)) {
1049 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_SS, &ss);
1050 		if (error == 0)
1051 			printf("ss[%d]\t\t0x%04lx\n", vcpu, ss);
1052 	}
1053 
1054 	if (!error && (get_tr || get_all)) {
1055 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_TR, &tr);
1056 		if (error == 0)
1057 			printf("tr[%d]\t\t0x%04lx\n", vcpu, tr);
1058 	}
1059 
1060 	if (!error && (get_ldtr || get_all)) {
1061 		error = vm_get_register(ctx, vcpu, VM_REG_GUEST_LDTR, &ldtr);
1062 		if (error == 0)
1063 			printf("ldtr[%d]\t\t0x%04lx\n", vcpu, ldtr);
1064 	}
1065 
1066 	return (error);
1067 }
1068 
1069 static int
1070 get_misc_vmcs(struct vmctx *ctx, int vcpu)
1071 {
1072 	uint64_t ctl, cr0, cr3, cr4, rsp, rip, pat, addr, u64;
1073 	int error = 0;
1074 
1075 	if (!error && (get_cr0_mask || get_all)) {
1076 		uint64_t cr0mask;
1077 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR0_MASK, &cr0mask);
1078 		if (error == 0)
1079 			printf("cr0_mask[%d]\t\t0x%016lx\n", vcpu, cr0mask);
1080 	}
1081 
1082 	if (!error && (get_cr0_shadow || get_all)) {
1083 		uint64_t cr0shadow;
1084 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR0_SHADOW,
1085 					  &cr0shadow);
1086 		if (error == 0)
1087 			printf("cr0_shadow[%d]\t\t0x%016lx\n", vcpu, cr0shadow);
1088 	}
1089 
1090 	if (!error && (get_cr4_mask || get_all)) {
1091 		uint64_t cr4mask;
1092 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR4_MASK, &cr4mask);
1093 		if (error == 0)
1094 			printf("cr4_mask[%d]\t\t0x%016lx\n", vcpu, cr4mask);
1095 	}
1096 
1097 	if (!error && (get_cr4_shadow || get_all)) {
1098 		uint64_t cr4shadow;
1099 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR4_SHADOW,
1100 					  &cr4shadow);
1101 		if (error == 0)
1102 			printf("cr4_shadow[%d]\t\t0x%016lx\n", vcpu, cr4shadow);
1103 	}
1104 
1105 	if (!error && (get_cr3_targets || get_all)) {
1106 		uint64_t target_count, target_addr;
1107 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET_COUNT,
1108 					  &target_count);
1109 		if (error == 0) {
1110 			printf("cr3_target_count[%d]\t0x%016lx\n",
1111 				vcpu, target_count);
1112 		}
1113 
1114 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET0,
1115 					  &target_addr);
1116 		if (error == 0) {
1117 			printf("cr3_target0[%d]\t\t0x%016lx\n",
1118 				vcpu, target_addr);
1119 		}
1120 
1121 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET1,
1122 					  &target_addr);
1123 		if (error == 0) {
1124 			printf("cr3_target1[%d]\t\t0x%016lx\n",
1125 				vcpu, target_addr);
1126 		}
1127 
1128 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET2,
1129 					  &target_addr);
1130 		if (error == 0) {
1131 			printf("cr3_target2[%d]\t\t0x%016lx\n",
1132 				vcpu, target_addr);
1133 		}
1134 
1135 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_CR3_TARGET3,
1136 					  &target_addr);
1137 		if (error == 0) {
1138 			printf("cr3_target3[%d]\t\t0x%016lx\n",
1139 				vcpu, target_addr);
1140 		}
1141 	}
1142 
1143 	if (!error && (get_pinbased_ctls || get_all)) {
1144 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_PIN_BASED_CTLS, &ctl);
1145 		if (error == 0)
1146 			printf("pinbased_ctls[%d]\t0x%016lx\n", vcpu, ctl);
1147 	}
1148 
1149 	if (!error && (get_procbased_ctls || get_all)) {
1150 		error = vm_get_vmcs_field(ctx, vcpu,
1151 					  VMCS_PRI_PROC_BASED_CTLS, &ctl);
1152 		if (error == 0)
1153 			printf("procbased_ctls[%d]\t0x%016lx\n", vcpu, ctl);
1154 	}
1155 
1156 	if (!error && (get_procbased_ctls2 || get_all)) {
1157 		error = vm_get_vmcs_field(ctx, vcpu,
1158 					  VMCS_SEC_PROC_BASED_CTLS, &ctl);
1159 		if (error == 0)
1160 			printf("procbased_ctls2[%d]\t0x%016lx\n", vcpu, ctl);
1161 	}
1162 
1163 	if (!error && (get_vmcs_gla || get_all)) {
1164 		error = vm_get_vmcs_field(ctx, vcpu,
1165 					  VMCS_GUEST_LINEAR_ADDRESS, &u64);
1166 		if (error == 0)
1167 			printf("gla[%d]\t\t0x%016lx\n", vcpu, u64);
1168 	}
1169 
1170 	if (!error && (get_vmcs_gpa || get_all)) {
1171 		error = vm_get_vmcs_field(ctx, vcpu,
1172 					  VMCS_GUEST_PHYSICAL_ADDRESS, &u64);
1173 		if (error == 0)
1174 			printf("gpa[%d]\t\t0x%016lx\n", vcpu, u64);
1175 	}
1176 
1177 	if (!error && (get_vmcs_entry_interruption_info ||
1178 		get_all)) {
1179 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_ENTRY_INTR_INFO,&u64);
1180 		if (error == 0) {
1181 			printf("entry_interruption_info[%d]\t0x%016lx\n",
1182 				vcpu, u64);
1183 		}
1184 	}
1185 
1186 	if (!error && (get_tpr_threshold || get_all)) {
1187 		uint64_t threshold;
1188 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_TPR_THRESHOLD,
1189 					  &threshold);
1190 		if (error == 0)
1191 			printf("tpr_threshold[%d]\t0x%016lx\n", vcpu, threshold);
1192 	}
1193 
1194 	if (!error && (get_inst_err || get_all)) {
1195 		uint64_t insterr;
1196 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_INSTRUCTION_ERROR,
1197 					  &insterr);
1198 		if (error == 0) {
1199 			printf("instruction_error[%d]\t0x%016lx\n",
1200 				vcpu, insterr);
1201 		}
1202 	}
1203 
1204 	if (!error && (get_exit_ctls || get_all)) {
1205 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_CTLS, &ctl);
1206 		if (error == 0)
1207 			printf("exit_ctls[%d]\t\t0x%016lx\n", vcpu, ctl);
1208 	}
1209 
1210 	if (!error && (get_entry_ctls || get_all)) {
1211 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_ENTRY_CTLS, &ctl);
1212 		if (error == 0)
1213 			printf("entry_ctls[%d]\t\t0x%016lx\n", vcpu, ctl);
1214 	}
1215 
1216 	if (!error && (get_host_pat || get_all)) {
1217 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_IA32_PAT, &pat);
1218 		if (error == 0)
1219 			printf("host_pat[%d]\t\t0x%016lx\n", vcpu, pat);
1220 	}
1221 
1222 	if (!error && (get_host_cr0 || get_all)) {
1223 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_CR0, &cr0);
1224 		if (error == 0)
1225 			printf("host_cr0[%d]\t\t0x%016lx\n", vcpu, cr0);
1226 	}
1227 
1228 	if (!error && (get_host_cr3 || get_all)) {
1229 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_CR3, &cr3);
1230 		if (error == 0)
1231 			printf("host_cr3[%d]\t\t0x%016lx\n", vcpu, cr3);
1232 	}
1233 
1234 	if (!error && (get_host_cr4 || get_all)) {
1235 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_CR4, &cr4);
1236 		if (error == 0)
1237 			printf("host_cr4[%d]\t\t0x%016lx\n", vcpu, cr4);
1238 	}
1239 
1240 	if (!error && (get_host_rip || get_all)) {
1241 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_RIP, &rip);
1242 		if (error == 0)
1243 			printf("host_rip[%d]\t\t0x%016lx\n", vcpu, rip);
1244 	}
1245 
1246 	if (!error && (get_host_rsp || get_all)) {
1247 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_HOST_RSP, &rsp);
1248 		if (error == 0)
1249 			printf("host_rsp[%d]\t\t0x%016lx\n", vcpu, rsp);
1250 	}
1251 
1252 	if (!error && (get_vmcs_link || get_all)) {
1253 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_LINK_POINTER, &addr);
1254 		if (error == 0)
1255 			printf("vmcs_pointer[%d]\t0x%016lx\n", vcpu, addr);
1256 	}
1257 
1258 	if (!error && (get_vmcs_exit_interruption_info || get_all)) {
1259 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_INTR_INFO, &u64);
1260 		if (error == 0) {
1261 			printf("vmcs_exit_interruption_info[%d]\t0x%016lx\n",
1262 				vcpu, u64);
1263 		}
1264 	}
1265 
1266 	if (!error && (get_vmcs_exit_interruption_error || get_all)) {
1267 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_INTR_ERRCODE,
1268 		    			  &u64);
1269 		if (error == 0) {
1270 			printf("vmcs_exit_interruption_error[%d]\t0x%016lx\n",
1271 				vcpu, u64);
1272 		}
1273 	}
1274 
1275 	if (!error && (get_vmcs_interruptibility || get_all)) {
1276 		error = vm_get_vmcs_field(ctx, vcpu,
1277 					  VMCS_GUEST_INTERRUPTIBILITY, &u64);
1278 		if (error == 0) {
1279 			printf("vmcs_guest_interruptibility[%d]\t0x%016lx\n",
1280 				vcpu, u64);
1281 		}
1282 	}
1283 
1284 	if (!error && (get_vmcs_exit_inst_length || get_all)) {
1285 		error = vm_get_vmcs_field(ctx, vcpu,
1286 		    VMCS_EXIT_INSTRUCTION_LENGTH, &u64);
1287 		if (error == 0)
1288 			printf("vmcs_exit_inst_length[%d]\t0x%08x\n", vcpu,
1289 			    (uint32_t)u64);
1290 	}
1291 
1292 	if (!error && (get_vmcs_exit_qualification || get_all)) {
1293 		error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_QUALIFICATION,
1294 					  &u64);
1295 		if (error == 0)
1296 			printf("vmcs_exit_qualification[%d]\t0x%016lx\n",
1297 				vcpu, u64);
1298 	}
1299 
1300 	return (error);
1301 }
1302 
1303 static int
1304 get_misc_vmcb(struct vmctx *ctx, int vcpu)
1305 {
1306 	uint64_t ctl, addr;
1307 	int error = 0;
1308 
1309 	if (!error && (get_vmcb_intercept || get_all)) {
1310 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_CR_INTERCEPT, 4,
1311 		    &ctl);
1312 		if (error == 0)
1313 			printf("cr_intercept[%d]\t0x%08x\n", vcpu, (int)ctl);
1314 
1315 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_DR_INTERCEPT, 4,
1316 		    &ctl);
1317 		if (error == 0)
1318 			printf("dr_intercept[%d]\t0x%08x\n", vcpu, (int)ctl);
1319 
1320 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_EXC_INTERCEPT, 4,
1321 		    &ctl);
1322 		if (error == 0)
1323 			printf("exc_intercept[%d]\t0x%08x\n", vcpu, (int)ctl);
1324 
1325 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_INST1_INTERCEPT,
1326 		    4, &ctl);
1327 		if (error == 0)
1328 			printf("inst1_intercept[%d]\t0x%08x\n", vcpu, (int)ctl);
1329 
1330 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_INST2_INTERCEPT,
1331 		    4, &ctl);
1332 		if (error == 0)
1333 			printf("inst2_intercept[%d]\t0x%08x\n", vcpu, (int)ctl);
1334 	}
1335 
1336 	if (!error && (get_vmcb_tlb_ctrl || get_all)) {
1337 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_TLB_CTRL,
1338 					  4, &ctl);
1339 		if (error == 0)
1340 			printf("TLB ctrl[%d]\t0x%016lx\n", vcpu, ctl);
1341 	}
1342 
1343 	if (!error && (get_vmcb_exit_details || get_all)) {
1344 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_EXITINFO1,
1345 					  8, &ctl);
1346 		if (error == 0)
1347 			printf("exitinfo1[%d]\t0x%016lx\n", vcpu, ctl);
1348 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_EXITINFO2,
1349 					  8, &ctl);
1350 		if (error == 0)
1351 			printf("exitinfo2[%d]\t0x%016lx\n", vcpu, ctl);
1352 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_EXITINTINFO,
1353 					  8, &ctl);
1354 		if (error == 0)
1355 			printf("exitintinfo[%d]\t0x%016lx\n", vcpu, ctl);
1356 	}
1357 
1358 	if (!error && (get_vmcb_virq || get_all)) {
1359 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_VIRQ,
1360 					  8, &ctl);
1361 		if (error == 0)
1362 			printf("v_irq/tpr[%d]\t0x%016lx\n", vcpu, ctl);
1363 	}
1364 
1365 	if (!error && (get_apic_access_addr || get_all)) {
1366 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_AVIC_BAR, 8,
1367 					  &addr);
1368 		if (error == 0)
1369 			printf("AVIC apic_bar[%d]\t0x%016lx\n", vcpu, addr);
1370 	}
1371 
1372 	if (!error && (get_virtual_apic_addr || get_all)) {
1373 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_AVIC_PAGE, 8,
1374 					  &addr);
1375 		if (error == 0)
1376 			printf("AVIC backing page[%d]\t0x%016lx\n", vcpu, addr);
1377 	}
1378 
1379 	if (!error && (get_avic_table || get_all)) {
1380 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_AVIC_LT, 8,
1381 					  &addr);
1382 		if (error == 0)
1383 			printf("AVIC logical table[%d]\t0x%016lx\n",
1384 				vcpu, addr);
1385 		error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_AVIC_PT, 8,
1386 					  &addr);
1387 		if (error == 0)
1388 			printf("AVIC physical table[%d]\t0x%016lx\n",
1389 				vcpu, addr);
1390 	}
1391 
1392 	return (error);
1393 }
1394 
1395 static struct option *
1396 setup_options(bool cpu_intel)
1397 {
1398 	const struct option common_opts[] = {
1399 		{ "vm",		REQ_ARG,	0,	VMNAME },
1400 		{ "cpu",	REQ_ARG,	0,	VCPU },
1401 		{ "set-mem",	REQ_ARG,	0,	SET_MEM },
1402 		{ "set-efer",	REQ_ARG,	0,	SET_EFER },
1403 		{ "set-cr0",	REQ_ARG,	0,	SET_CR0 },
1404 		{ "set-cr2",	REQ_ARG,	0,	SET_CR2 },
1405 		{ "set-cr3",	REQ_ARG,	0,	SET_CR3 },
1406 		{ "set-cr4",	REQ_ARG,	0,	SET_CR4 },
1407 		{ "set-dr0",	REQ_ARG,	0,	SET_DR0 },
1408 		{ "set-dr1",	REQ_ARG,	0,	SET_DR1 },
1409 		{ "set-dr2",	REQ_ARG,	0,	SET_DR2 },
1410 		{ "set-dr3",	REQ_ARG,	0,	SET_DR3 },
1411 		{ "set-dr6",	REQ_ARG,	0,	SET_DR6 },
1412 		{ "set-dr7",	REQ_ARG,	0,	SET_DR7 },
1413 		{ "set-rsp",	REQ_ARG,	0,	SET_RSP },
1414 		{ "set-rip",	REQ_ARG,	0,	SET_RIP },
1415 		{ "set-rax",	REQ_ARG,	0,	SET_RAX },
1416 		{ "set-rflags",	REQ_ARG,	0,	SET_RFLAGS },
1417 		{ "desc-base",	REQ_ARG,	0,	DESC_BASE },
1418 		{ "desc-limit",	REQ_ARG,	0,	DESC_LIMIT },
1419 		{ "desc-access",REQ_ARG,	0,	DESC_ACCESS },
1420 		{ "set-cs",	REQ_ARG,	0,	SET_CS },
1421 		{ "set-ds",	REQ_ARG,	0,	SET_DS },
1422 		{ "set-es",	REQ_ARG,	0,	SET_ES },
1423 		{ "set-fs",	REQ_ARG,	0,	SET_FS },
1424 		{ "set-gs",	REQ_ARG,	0,	SET_GS },
1425 		{ "set-ss",	REQ_ARG,	0,	SET_SS },
1426 		{ "set-tr",	REQ_ARG,	0,	SET_TR },
1427 		{ "set-ldtr",	REQ_ARG,	0,	SET_LDTR },
1428 		{ "set-x2apic-state",REQ_ARG,	0,	SET_X2APIC_STATE },
1429 		{ "set-exception-bitmap",
1430 				REQ_ARG,	0, SET_EXCEPTION_BITMAP },
1431 		{ "capname",	REQ_ARG,	0,	CAPNAME },
1432 		{ "unassign-pptdev", REQ_ARG,	0,	UNASSIGN_PPTDEV },
1433 		{ "setcap",	REQ_ARG,	0,	SET_CAP },
1434 		{ "get-gpa-pmap", REQ_ARG,	0,	GET_GPA_PMAP },
1435 		{ "assert-lapic-lvt", REQ_ARG,	0,	ASSERT_LAPIC_LVT },
1436 		{ "get-rtc-time", NO_ARG,	&get_rtc_time,	1 },
1437 		{ "set-rtc-time", REQ_ARG,	0,	SET_RTC_TIME },
1438 		{ "rtc-nvram-offset", REQ_ARG,	0,	RTC_NVRAM_OFFSET },
1439 		{ "get-rtc-nvram", NO_ARG,	&get_rtc_nvram,	1 },
1440 		{ "set-rtc-nvram", REQ_ARG,	0,	SET_RTC_NVRAM },
1441 		{ "getcap",	NO_ARG,		&getcap,	1 },
1442 		{ "get-stats",	NO_ARG,		&get_stats,	1 },
1443 		{ "get-desc-ds",NO_ARG,		&get_desc_ds,	1 },
1444 		{ "set-desc-ds",NO_ARG,		&set_desc_ds,	1 },
1445 		{ "get-desc-es",NO_ARG,		&get_desc_es,	1 },
1446 		{ "set-desc-es",NO_ARG,		&set_desc_es,	1 },
1447 		{ "get-desc-ss",NO_ARG,		&get_desc_ss,	1 },
1448 		{ "set-desc-ss",NO_ARG,		&set_desc_ss,	1 },
1449 		{ "get-desc-cs",NO_ARG,		&get_desc_cs,	1 },
1450 		{ "set-desc-cs",NO_ARG,		&set_desc_cs,	1 },
1451 		{ "get-desc-fs",NO_ARG,		&get_desc_fs,	1 },
1452 		{ "set-desc-fs",NO_ARG,		&set_desc_fs,	1 },
1453 		{ "get-desc-gs",NO_ARG,		&get_desc_gs,	1 },
1454 		{ "set-desc-gs",NO_ARG,		&set_desc_gs,	1 },
1455 		{ "get-desc-tr",NO_ARG,		&get_desc_tr,	1 },
1456 		{ "set-desc-tr",NO_ARG,		&set_desc_tr,	1 },
1457 		{ "set-desc-ldtr", NO_ARG,	&set_desc_ldtr,	1 },
1458 		{ "get-desc-ldtr", NO_ARG,	&get_desc_ldtr,	1 },
1459 		{ "set-desc-gdtr", NO_ARG,	&set_desc_gdtr, 1 },
1460 		{ "get-desc-gdtr", NO_ARG,	&get_desc_gdtr, 1 },
1461 		{ "set-desc-idtr", NO_ARG,	&set_desc_idtr, 1 },
1462 		{ "get-desc-idtr", NO_ARG,	&get_desc_idtr, 1 },
1463 		{ "get-memmap",	NO_ARG,		&get_memmap,	1 },
1464 		{ "get-memseg", NO_ARG,		&get_memseg,	1 },
1465 		{ "get-efer",	NO_ARG,		&get_efer,	1 },
1466 		{ "get-cr0",	NO_ARG,		&get_cr0,	1 },
1467 		{ "get-cr2",	NO_ARG,		&get_cr2,	1 },
1468 		{ "get-cr3",	NO_ARG,		&get_cr3,	1 },
1469 		{ "get-cr4",	NO_ARG,		&get_cr4,	1 },
1470 		{ "get-dr0",	NO_ARG,		&get_dr0,	1 },
1471 		{ "get-dr1",	NO_ARG,		&get_dr1,	1 },
1472 		{ "get-dr2",	NO_ARG,		&get_dr2,	1 },
1473 		{ "get-dr3",	NO_ARG,		&get_dr3,	1 },
1474 		{ "get-dr6",	NO_ARG,		&get_dr6,	1 },
1475 		{ "get-dr7",	NO_ARG,		&get_dr7,	1 },
1476 		{ "get-rsp",	NO_ARG,		&get_rsp,	1 },
1477 		{ "get-rip",	NO_ARG,		&get_rip,	1 },
1478 		{ "get-rax",	NO_ARG,		&get_rax,	1 },
1479 		{ "get-rbx",	NO_ARG,		&get_rbx,	1 },
1480 		{ "get-rcx",	NO_ARG,		&get_rcx,	1 },
1481 		{ "get-rdx",	NO_ARG,		&get_rdx,	1 },
1482 		{ "get-rsi",	NO_ARG,		&get_rsi,	1 },
1483 		{ "get-rdi",	NO_ARG,		&get_rdi,	1 },
1484 		{ "get-rbp",	NO_ARG,		&get_rbp,	1 },
1485 		{ "get-r8",	NO_ARG,		&get_r8,	1 },
1486 		{ "get-r9",	NO_ARG,		&get_r9,	1 },
1487 		{ "get-r10",	NO_ARG,		&get_r10,	1 },
1488 		{ "get-r11",	NO_ARG,		&get_r11,	1 },
1489 		{ "get-r12",	NO_ARG,		&get_r12,	1 },
1490 		{ "get-r13",	NO_ARG,		&get_r13,	1 },
1491 		{ "get-r14",	NO_ARG,		&get_r14,	1 },
1492 		{ "get-r15",	NO_ARG,		&get_r15,	1 },
1493 		{ "get-rflags",	NO_ARG,		&get_rflags,	1 },
1494 		{ "get-cs",	NO_ARG,		&get_cs,	1 },
1495 		{ "get-ds",	NO_ARG,		&get_ds,	1 },
1496 		{ "get-es",	NO_ARG,		&get_es,	1 },
1497 		{ "get-fs",	NO_ARG,		&get_fs,	1 },
1498 		{ "get-gs",	NO_ARG,		&get_gs,	1 },
1499 		{ "get-ss",	NO_ARG,		&get_ss,	1 },
1500 		{ "get-tr",	NO_ARG,		&get_tr,	1 },
1501 		{ "get-ldtr",	NO_ARG,		&get_ldtr,	1 },
1502 		{ "get-eptp", 	NO_ARG,		&get_eptp,	1 },
1503 		{ "get-exception-bitmap",
1504 					NO_ARG,	&get_exception_bitmap,  1 },
1505 		{ "get-io-bitmap-address",
1506 					NO_ARG,	&get_io_bitmap,		1 },
1507 		{ "get-tsc-offset", 	NO_ARG, &get_tsc_offset, 	1 },
1508 		{ "get-msr-bitmap",
1509 					NO_ARG,	&get_msr_bitmap, 	1 },
1510 		{ "get-msr-bitmap-address",
1511 					NO_ARG,	&get_msr_bitmap_address, 1 },
1512 		{ "get-guest-pat",	NO_ARG,	&get_guest_pat,		1 },
1513 		{ "get-guest-sysenter",
1514 					NO_ARG,	&get_guest_sysenter, 	1 },
1515 		{ "get-exit-reason",
1516 					NO_ARG,	&get_exit_reason, 	1 },
1517 		{ "get-x2apic-state",	NO_ARG,	&get_x2apic_state, 	1 },
1518 		{ "get-all",		NO_ARG,	&get_all,		1 },
1519 		{ "run",		NO_ARG,	&run,			1 },
1520 		{ "create",		NO_ARG,	&create,		1 },
1521 		{ "destroy",		NO_ARG,	&destroy,		1 },
1522 		{ "inject-nmi",		NO_ARG,	&inject_nmi,		1 },
1523 		{ "force-reset",	NO_ARG,	&force_reset,		1 },
1524 		{ "force-poweroff", 	NO_ARG,	&force_poweroff, 	1 },
1525 		{ "get-active-cpus", 	NO_ARG,	&get_active_cpus, 	1 },
1526 		{ "get-suspended-cpus", NO_ARG,	&get_suspended_cpus, 	1 },
1527 		{ "get-intinfo", 	NO_ARG,	&get_intinfo,		1 },
1528 		{ "get-cpu-topology",	NO_ARG, &get_cpu_topology,	1 },
1529 #ifndef __FreeBSD__
1530 		{ "wrlock-cycle",	NO_ARG,	&wrlock_cycle,	1 },
1531 #endif
1532 	};
1533 
1534 	const struct option intel_opts[] = {
1535 		{ "get-vmcs-pinbased-ctls",
1536 				NO_ARG,		&get_pinbased_ctls, 1 },
1537 		{ "get-vmcs-procbased-ctls",
1538 				NO_ARG,		&get_procbased_ctls, 1 },
1539 		{ "get-vmcs-procbased-ctls2",
1540 				NO_ARG,		&get_procbased_ctls2, 1 },
1541 		{ "get-vmcs-guest-linear-address",
1542 				NO_ARG,		&get_vmcs_gla,	1 },
1543 		{ "get-vmcs-guest-physical-address",
1544 				NO_ARG,		&get_vmcs_gpa,	1 },
1545 		{ "get-vmcs-entry-interruption-info",
1546 				NO_ARG, &get_vmcs_entry_interruption_info, 1},
1547 		{ "get-vmcs-cr0-mask", NO_ARG,	&get_cr0_mask,	1 },
1548 		{ "get-vmcs-cr0-shadow", NO_ARG,&get_cr0_shadow, 1 },
1549 		{ "get-vmcs-cr4-mask", 		NO_ARG,	&get_cr4_mask,	  1 },
1550 		{ "get-vmcs-cr4-shadow", 	NO_ARG, &get_cr4_shadow,  1 },
1551 		{ "get-vmcs-cr3-targets", 	NO_ARG, &get_cr3_targets, 1 },
1552 		{ "get-vmcs-tpr-threshold",
1553 					NO_ARG,	&get_tpr_threshold, 1 },
1554 		{ "get-vmcs-vpid", 	NO_ARG,	&get_vpid_asid,	    1 },
1555 		{ "get-vmcs-exit-ctls", NO_ARG,	&get_exit_ctls,	    1 },
1556 		{ "get-vmcs-entry-ctls",
1557 					NO_ARG,	&get_entry_ctls, 1 },
1558 		{ "get-vmcs-instruction-error",
1559 					NO_ARG,	&get_inst_err,	1 },
1560 		{ "get-vmcs-host-pat",	NO_ARG,	&get_host_pat,	1 },
1561 		{ "get-vmcs-host-cr0",
1562 					NO_ARG,	&get_host_cr0,	1 },
1563 		{ "set-vmcs-entry-interruption-info",
1564 				REQ_ARG, 0, SET_VMCS_ENTRY_INTERRUPTION_INFO },
1565 		{ "get-vmcs-exit-qualification",
1566 				NO_ARG,	&get_vmcs_exit_qualification, 1 },
1567 		{ "get-vmcs-exit-inst-length",
1568 				NO_ARG,	&get_vmcs_exit_inst_length, 1 },
1569 		{ "get-vmcs-interruptibility",
1570 				NO_ARG, &get_vmcs_interruptibility, 1 },
1571 		{ "get-vmcs-exit-interruption-error",
1572 				NO_ARG,	&get_vmcs_exit_interruption_error, 1 },
1573 		{ "get-vmcs-exit-interruption-info",
1574 				NO_ARG,	&get_vmcs_exit_interruption_info, 1 },
1575 		{ "get-vmcs-link", 	NO_ARG,		&get_vmcs_link, 1 },
1576 		{ "get-vmcs-host-cr3",
1577 					NO_ARG,		&get_host_cr3,	1 },
1578 		{ "get-vmcs-host-cr4",
1579 				NO_ARG,		&get_host_cr4,	1 },
1580 		{ "get-vmcs-host-rip",
1581 				NO_ARG,		&get_host_rip,	1 },
1582 		{ "get-vmcs-host-rsp",
1583 				NO_ARG,		&get_host_rsp,	1 },
1584 		{ "get-apic-access-address",
1585 				NO_ARG,		&get_apic_access_addr, 1},
1586 		{ "get-virtual-apic-address",
1587 				NO_ARG,		&get_virtual_apic_addr, 1}
1588 	};
1589 
1590 	const struct option amd_opts[] = {
1591 		{ "get-vmcb-intercepts",
1592 				NO_ARG,	&get_vmcb_intercept, 	1 },
1593 		{ "get-vmcb-asid",
1594 				NO_ARG,	&get_vpid_asid,	     	1 },
1595 		{ "get-vmcb-exit-details",
1596 				NO_ARG, &get_vmcb_exit_details,	1 },
1597 		{ "get-vmcb-tlb-ctrl",
1598 				NO_ARG, &get_vmcb_tlb_ctrl, 	1 },
1599 		{ "get-vmcb-virq",
1600 				NO_ARG, &get_vmcb_virq, 	1 },
1601 		{ "get-avic-apic-bar",
1602 				NO_ARG,	&get_apic_access_addr, 	1 },
1603 		{ "get-avic-backing-page",
1604 				NO_ARG,	&get_virtual_apic_addr, 1 },
1605 		{ "get-avic-table",
1606 				NO_ARG,	&get_avic_table, 	1 }
1607 	};
1608 
1609 	const struct option null_opt = {
1610 		NULL, 0, NULL, 0
1611 	};
1612 
1613 	struct option *all_opts;
1614 	char *cp;
1615 	int optlen;
1616 
1617 	optlen = sizeof(common_opts);
1618 
1619 	if (cpu_intel)
1620 		optlen += sizeof(intel_opts);
1621 	else
1622 		optlen += sizeof(amd_opts);
1623 
1624 	optlen += sizeof(null_opt);
1625 
1626 	all_opts = malloc(optlen);
1627 
1628 	cp = (char *)all_opts;
1629 	memcpy(cp, common_opts, sizeof(common_opts));
1630 	cp += sizeof(common_opts);
1631 
1632 	if (cpu_intel) {
1633 		memcpy(cp, intel_opts, sizeof(intel_opts));
1634 		cp += sizeof(intel_opts);
1635 	} else {
1636 		memcpy(cp, amd_opts, sizeof(amd_opts));
1637 		cp += sizeof(amd_opts);
1638 	}
1639 
1640 	memcpy(cp, &null_opt, sizeof(null_opt));
1641 	cp += sizeof(null_opt);
1642 
1643 	return (all_opts);
1644 }
1645 
1646 static const char *
1647 wday_str(int idx)
1648 {
1649 	static const char *weekdays[] = {
1650 		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
1651 	};
1652 
1653 	if (idx >= 0 && idx < 7)
1654 		return (weekdays[idx]);
1655 	else
1656 		return ("UNK");
1657 }
1658 
1659 static const char *
1660 mon_str(int idx)
1661 {
1662 	static const char *months[] = {
1663 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
1664 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
1665 	};
1666 
1667 	if (idx >= 0 && idx < 12)
1668 		return (months[idx]);
1669 	else
1670 		return ("UNK");
1671 }
1672 
1673 static int
1674 show_memmap(struct vmctx *ctx)
1675 {
1676 	char name[SPECNAMELEN + 1], numbuf[8];
1677 	vm_ooffset_t segoff;
1678 	vm_paddr_t gpa;
1679 	size_t maplen, seglen;
1680 	int error, flags, prot, segid, delim;
1681 
1682 	printf("Address     Length      Segment     Offset      ");
1683 	printf("Prot  Flags\n");
1684 
1685 	gpa = 0;
1686 	while (1) {
1687 		error = vm_mmap_getnext(ctx, &gpa, &segid, &segoff, &maplen,
1688 		    &prot, &flags);
1689 		if (error)
1690 			return (errno == ENOENT ? 0 : error);
1691 
1692 		error = vm_get_memseg(ctx, segid, &seglen, name, sizeof(name));
1693 		if (error)
1694 			return (error);
1695 
1696 		printf("%-12lX", gpa);
1697 		humanize_number(numbuf, sizeof(numbuf), maplen, "B",
1698 		    HN_AUTOSCALE, HN_NOSPACE);
1699 		printf("%-12s", numbuf);
1700 
1701 		printf("%-12s", name[0] ? name : "sysmem");
1702 		printf("%-12lX", segoff);
1703 		printf("%c%c%c   ", prot & PROT_READ ? 'R' : '-',
1704 		    prot & PROT_WRITE ? 'W' : '-',
1705 		    prot & PROT_EXEC ? 'X' : '-');
1706 
1707 		delim = '\0';
1708 		if (flags & VM_MEMMAP_F_WIRED) {
1709 			printf("%cwired", delim);
1710 			delim = '/';
1711 		}
1712 		if (flags & VM_MEMMAP_F_IOMMU) {
1713 			printf("%ciommu", delim);
1714 			delim = '/';
1715 		}
1716 		printf("\n");
1717 
1718 		gpa += maplen;
1719 	}
1720 }
1721 
1722 static int
1723 show_memseg(struct vmctx *ctx)
1724 {
1725 	char name[SPECNAMELEN + 1], numbuf[8];
1726 	size_t seglen;
1727 	int error, segid;
1728 
1729 	printf("ID  Length      Name\n");
1730 
1731 	segid = 0;
1732 	while (1) {
1733 		error = vm_get_memseg(ctx, segid, &seglen, name, sizeof(name));
1734 		if (error)
1735 			return (errno == EINVAL ? 0 : error);
1736 
1737 		if (seglen) {
1738 			printf("%-4d", segid);
1739 			humanize_number(numbuf, sizeof(numbuf), seglen, "B",
1740 			    HN_AUTOSCALE, HN_NOSPACE);
1741 			printf("%-12s", numbuf);
1742 			printf("%s", name[0] ? name : "sysmem");
1743 			printf("\n");
1744 		}
1745 		segid++;
1746 	}
1747 }
1748 
1749 int
1750 main(int argc, char *argv[])
1751 {
1752 	char *vmname;
1753 	int error, ch, vcpu, ptenum;
1754 	vm_paddr_t gpa_pmap;
1755 	struct vm_exit vmexit;
1756 	uint64_t rax, cr0, cr2, cr3, cr4, dr0, dr1, dr2, dr3, dr6, dr7;
1757 	uint64_t rsp, rip, rflags, efer, pat;
1758 	uint64_t eptp, bm, addr, u64, pteval[4], *pte, info[2];
1759 	struct vmctx *ctx;
1760 	cpuset_t cpus;
1761 	bool cpu_intel;
1762 	uint64_t cs, ds, es, fs, gs, ss, tr, ldtr;
1763 	struct tm tm;
1764 	struct option *opts;
1765 
1766 	cpu_intel = cpu_vendor_intel();
1767 	opts = setup_options(cpu_intel);
1768 
1769 	vcpu = 0;
1770 	vmname = NULL;
1771 	assert_lapic_lvt = -1;
1772 	progname = basename(argv[0]);
1773 
1774 	while ((ch = getopt_long(argc, argv, "", opts, NULL)) != -1) {
1775 		switch (ch) {
1776 		case 0:
1777 			break;
1778 		case VMNAME:
1779 			vmname = optarg;
1780 			break;
1781 		case VCPU:
1782 			vcpu = atoi(optarg);
1783 			break;
1784 		case SET_MEM:
1785 			memsize = atoi(optarg) * MB;
1786 			memsize = roundup(memsize, 2 * MB);
1787 			break;
1788 		case SET_EFER:
1789 			efer = strtoul(optarg, NULL, 0);
1790 			set_efer = 1;
1791 			break;
1792 		case SET_CR0:
1793 			cr0 = strtoul(optarg, NULL, 0);
1794 			set_cr0 = 1;
1795 			break;
1796 		case SET_CR2:
1797 			cr2 = strtoul(optarg, NULL, 0);
1798 			set_cr2 = 1;
1799 			break;
1800 		case SET_CR3:
1801 			cr3 = strtoul(optarg, NULL, 0);
1802 			set_cr3 = 1;
1803 			break;
1804 		case SET_CR4:
1805 			cr4 = strtoul(optarg, NULL, 0);
1806 			set_cr4 = 1;
1807 			break;
1808 		case SET_DR0:
1809 			dr0 = strtoul(optarg, NULL, 0);
1810 			set_dr0 = 1;
1811 			break;
1812 		case SET_DR1:
1813 			dr1 = strtoul(optarg, NULL, 0);
1814 			set_dr1 = 1;
1815 			break;
1816 		case SET_DR2:
1817 			dr2 = strtoul(optarg, NULL, 0);
1818 			set_dr2 = 1;
1819 			break;
1820 		case SET_DR3:
1821 			dr3 = strtoul(optarg, NULL, 0);
1822 			set_dr3 = 1;
1823 			break;
1824 		case SET_DR6:
1825 			dr6 = strtoul(optarg, NULL, 0);
1826 			set_dr6 = 1;
1827 			break;
1828 		case SET_DR7:
1829 			dr7 = strtoul(optarg, NULL, 0);
1830 			set_dr7 = 1;
1831 			break;
1832 		case SET_RSP:
1833 			rsp = strtoul(optarg, NULL, 0);
1834 			set_rsp = 1;
1835 			break;
1836 		case SET_RIP:
1837 			rip = strtoul(optarg, NULL, 0);
1838 			set_rip = 1;
1839 			break;
1840 		case SET_RAX:
1841 			rax = strtoul(optarg, NULL, 0);
1842 			set_rax = 1;
1843 			break;
1844 		case SET_RFLAGS:
1845 			rflags = strtoul(optarg, NULL, 0);
1846 			set_rflags = 1;
1847 			break;
1848 		case DESC_BASE:
1849 			desc_base = strtoul(optarg, NULL, 0);
1850 			break;
1851 		case DESC_LIMIT:
1852 			desc_limit = strtoul(optarg, NULL, 0);
1853 			break;
1854 		case DESC_ACCESS:
1855 			desc_access = strtoul(optarg, NULL, 0);
1856 			break;
1857 		case SET_CS:
1858 			cs = strtoul(optarg, NULL, 0);
1859 			set_cs = 1;
1860 			break;
1861 		case SET_DS:
1862 			ds = strtoul(optarg, NULL, 0);
1863 			set_ds = 1;
1864 			break;
1865 		case SET_ES:
1866 			es = strtoul(optarg, NULL, 0);
1867 			set_es = 1;
1868 			break;
1869 		case SET_FS:
1870 			fs = strtoul(optarg, NULL, 0);
1871 			set_fs = 1;
1872 			break;
1873 		case SET_GS:
1874 			gs = strtoul(optarg, NULL, 0);
1875 			set_gs = 1;
1876 			break;
1877 		case SET_SS:
1878 			ss = strtoul(optarg, NULL, 0);
1879 			set_ss = 1;
1880 			break;
1881 		case SET_TR:
1882 			tr = strtoul(optarg, NULL, 0);
1883 			set_tr = 1;
1884 			break;
1885 		case SET_LDTR:
1886 			ldtr = strtoul(optarg, NULL, 0);
1887 			set_ldtr = 1;
1888 			break;
1889 		case SET_X2APIC_STATE:
1890 			x2apic_state = strtol(optarg, NULL, 0);
1891 			set_x2apic_state = 1;
1892 			break;
1893 		case SET_EXCEPTION_BITMAP:
1894 			exception_bitmap = strtoul(optarg, NULL, 0);
1895 			set_exception_bitmap = 1;
1896 			break;
1897 		case SET_VMCS_ENTRY_INTERRUPTION_INFO:
1898 			vmcs_entry_interruption_info = strtoul(optarg, NULL, 0);
1899 			set_vmcs_entry_interruption_info = 1;
1900 			break;
1901 		case SET_CAP:
1902 			capval = strtoul(optarg, NULL, 0);
1903 			setcap = 1;
1904 			break;
1905 		case SET_RTC_TIME:
1906 			rtc_secs = strtoul(optarg, NULL, 0);
1907 			set_rtc_time = 1;
1908 			break;
1909 		case SET_RTC_NVRAM:
1910 			rtc_nvram_value = (uint8_t)strtoul(optarg, NULL, 0);
1911 			set_rtc_nvram = 1;
1912 			break;
1913 		case RTC_NVRAM_OFFSET:
1914 			rtc_nvram_offset = strtoul(optarg, NULL, 0);
1915 			break;
1916 		case GET_GPA_PMAP:
1917 			gpa_pmap = strtoul(optarg, NULL, 0);
1918 			get_gpa_pmap = 1;
1919 			break;
1920 		case CAPNAME:
1921 			capname = optarg;
1922 			break;
1923 #ifdef __FreeBSD__
1924 		case UNASSIGN_PPTDEV:
1925 			unassign_pptdev = 1;
1926 			if (sscanf(optarg, "%d/%d/%d", &bus, &slot, &func) != 3)
1927 				usage(cpu_intel);
1928 			break;
1929 #endif
1930 		case ASSERT_LAPIC_LVT:
1931 			assert_lapic_lvt = atoi(optarg);
1932 			break;
1933 		default:
1934 			usage(cpu_intel);
1935 		}
1936 	}
1937 	argc -= optind;
1938 	argv += optind;
1939 
1940 	if (vmname == NULL)
1941 		usage(cpu_intel);
1942 
1943 	error = 0;
1944 
1945 	if (!error && create)
1946 		error = vm_create(vmname);
1947 
1948 	if (!error) {
1949 		ctx = vm_open(vmname);
1950 		if (ctx == NULL) {
1951 			printf("VM:%s is not created.\n", vmname);
1952 			exit (1);
1953 		}
1954 	}
1955 
1956 #ifndef __FreeBSD__
1957 	if (!error && wrlock_cycle) {
1958 		error = vm_wrlock_cycle(ctx);
1959 		exit(error);
1960 	}
1961 #endif /* __FreeBSD__ */
1962 
1963 	if (!error && memsize)
1964 		error = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
1965 
1966 	if (!error && set_efer)
1967 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_EFER, efer);
1968 
1969 	if (!error && set_cr0)
1970 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CR0, cr0);
1971 
1972 	if (!error && set_cr2)
1973 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CR2, cr2);
1974 
1975 	if (!error && set_cr3)
1976 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CR3, cr3);
1977 
1978 	if (!error && set_cr4)
1979 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CR4, cr4);
1980 
1981 	if (!error && set_dr0)
1982 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DR0, dr0);
1983 
1984 	if (!error && set_dr1)
1985 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DR1, dr1);
1986 
1987 	if (!error && set_dr2)
1988 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DR2, dr2);
1989 
1990 	if (!error && set_dr3)
1991 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DR3, dr3);
1992 
1993 	if (!error && set_dr6)
1994 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DR6, dr6);
1995 
1996 	if (!error && set_dr7)
1997 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DR7, dr7);
1998 
1999 	if (!error && set_rsp)
2000 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RSP, rsp);
2001 
2002 	if (!error && set_rip)
2003 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RIP, rip);
2004 
2005 	if (!error && set_rax)
2006 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX, rax);
2007 
2008 	if (!error && set_rflags) {
2009 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RFLAGS,
2010 					rflags);
2011 	}
2012 
2013 	if (!error && set_desc_ds) {
2014 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_DS,
2015 				    desc_base, desc_limit, desc_access);
2016 	}
2017 
2018 	if (!error && set_desc_es) {
2019 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_ES,
2020 				    desc_base, desc_limit, desc_access);
2021 	}
2022 
2023 	if (!error && set_desc_ss) {
2024 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_SS,
2025 				    desc_base, desc_limit, desc_access);
2026 	}
2027 
2028 	if (!error && set_desc_cs) {
2029 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_CS,
2030 				    desc_base, desc_limit, desc_access);
2031 	}
2032 
2033 	if (!error && set_desc_fs) {
2034 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_FS,
2035 				    desc_base, desc_limit, desc_access);
2036 	}
2037 
2038 	if (!error && set_desc_gs) {
2039 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_GS,
2040 				    desc_base, desc_limit, desc_access);
2041 	}
2042 
2043 	if (!error && set_desc_tr) {
2044 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_TR,
2045 				    desc_base, desc_limit, desc_access);
2046 	}
2047 
2048 	if (!error && set_desc_ldtr) {
2049 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_LDTR,
2050 				    desc_base, desc_limit, desc_access);
2051 	}
2052 
2053 	if (!error && set_desc_gdtr) {
2054 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_GDTR,
2055 				    desc_base, desc_limit, 0);
2056 	}
2057 
2058 	if (!error && set_desc_idtr) {
2059 		error = vm_set_desc(ctx, vcpu, VM_REG_GUEST_IDTR,
2060 				    desc_base, desc_limit, 0);
2061 	}
2062 
2063 	if (!error && set_cs)
2064 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_CS, cs);
2065 
2066 	if (!error && set_ds)
2067 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_DS, ds);
2068 
2069 	if (!error && set_es)
2070 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_ES, es);
2071 
2072 	if (!error && set_fs)
2073 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_FS, fs);
2074 
2075 	if (!error && set_gs)
2076 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_GS, gs);
2077 
2078 	if (!error && set_ss)
2079 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_SS, ss);
2080 
2081 	if (!error && set_tr)
2082 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_TR, tr);
2083 
2084 	if (!error && set_ldtr)
2085 		error = vm_set_register(ctx, vcpu, VM_REG_GUEST_LDTR, ldtr);
2086 
2087 	if (!error && set_x2apic_state)
2088 		error = vm_set_x2apic_state(ctx, vcpu, x2apic_state);
2089 
2090 #ifdef __FreeBSD__
2091 	if (!error && unassign_pptdev)
2092 		error = vm_unassign_pptdev(ctx, bus, slot, func);
2093 #endif /* __FreeBSD__ */
2094 
2095 	if (!error && set_exception_bitmap) {
2096 		if (cpu_intel)
2097 			error = vm_set_vmcs_field(ctx, vcpu,
2098 						  VMCS_EXCEPTION_BITMAP,
2099 						  exception_bitmap);
2100 		else
2101 			error = vm_set_vmcb_field(ctx, vcpu,
2102 						  VMCB_OFF_EXC_INTERCEPT,
2103 						  4, exception_bitmap);
2104 	}
2105 
2106 	if (!error && cpu_intel && set_vmcs_entry_interruption_info) {
2107 		error = vm_set_vmcs_field(ctx, vcpu, VMCS_ENTRY_INTR_INFO,
2108 					  vmcs_entry_interruption_info);
2109 	}
2110 
2111 	if (!error && inject_nmi) {
2112 		error = vm_inject_nmi(ctx, vcpu);
2113 	}
2114 
2115 	if (!error && assert_lapic_lvt != -1) {
2116 		error = vm_lapic_local_irq(ctx, vcpu, assert_lapic_lvt);
2117 	}
2118 
2119 	if (!error && (get_memseg || get_all))
2120 		error = show_memseg(ctx);
2121 
2122 	if (!error && (get_memmap || get_all))
2123 		error = show_memmap(ctx);
2124 
2125 	if (!error)
2126 		error = get_all_registers(ctx, vcpu);
2127 
2128 	if (!error)
2129 		error = get_all_segments(ctx, vcpu);
2130 
2131 	if (!error) {
2132 		if (cpu_intel)
2133 			error = get_misc_vmcs(ctx, vcpu);
2134 		else
2135 			error = get_misc_vmcb(ctx, vcpu);
2136 	}
2137 
2138 	if (!error && (get_x2apic_state || get_all)) {
2139 		error = vm_get_x2apic_state(ctx, vcpu, &x2apic_state);
2140 		if (error == 0)
2141 			printf("x2apic_state[%d]\t%d\n", vcpu, x2apic_state);
2142 	}
2143 
2144 	if (!error && (get_eptp || get_all)) {
2145 		if (cpu_intel)
2146 			error = vm_get_vmcs_field(ctx, vcpu, VMCS_EPTP, &eptp);
2147 		else
2148 			error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_NPT_BASE,
2149 						   8, &eptp);
2150 		if (error == 0)
2151 			printf("%s[%d]\t\t0x%016lx\n",
2152 				cpu_intel ? "eptp" : "rvi/npt", vcpu, eptp);
2153 	}
2154 
2155 	if (!error && (get_exception_bitmap || get_all)) {
2156 		if(cpu_intel)
2157 			error = vm_get_vmcs_field(ctx, vcpu,
2158 						VMCS_EXCEPTION_BITMAP, &bm);
2159 		else
2160 			error = vm_get_vmcb_field(ctx, vcpu,
2161 						  VMCB_OFF_EXC_INTERCEPT,
2162 						  4, &bm);
2163 		if (error == 0)
2164 			printf("exception_bitmap[%d]\t%#lx\n", vcpu, bm);
2165 	}
2166 
2167 	if (!error && (get_io_bitmap || get_all)) {
2168 		if (cpu_intel) {
2169 			error = vm_get_vmcs_field(ctx, vcpu, VMCS_IO_BITMAP_A,
2170 						  &bm);
2171 			if (error == 0)
2172 				printf("io_bitmap_a[%d]\t%#lx\n", vcpu, bm);
2173 			error = vm_get_vmcs_field(ctx, vcpu, VMCS_IO_BITMAP_B,
2174 						  &bm);
2175 			if (error == 0)
2176 				printf("io_bitmap_b[%d]\t%#lx\n", vcpu, bm);
2177 		} else {
2178 			error = vm_get_vmcb_field(ctx, vcpu,
2179 						  VMCB_OFF_IO_PERM, 8, &bm);
2180 			if (error == 0)
2181 				printf("io_bitmap[%d]\t%#lx\n", vcpu, bm);
2182 		}
2183 	}
2184 
2185 	if (!error && (get_tsc_offset || get_all)) {
2186 		uint64_t tscoff;
2187 		if (cpu_intel)
2188 			error = vm_get_vmcs_field(ctx, vcpu, VMCS_TSC_OFFSET,
2189 						  &tscoff);
2190 		else
2191 			error = vm_get_vmcb_field(ctx, vcpu,
2192 						  VMCB_OFF_TSC_OFFSET,
2193 						  8, &tscoff);
2194 		if (error == 0)
2195 			printf("tsc_offset[%d]\t0x%016lx\n", vcpu, tscoff);
2196 	}
2197 
2198 	if (!error && (get_msr_bitmap_address || get_all)) {
2199 		if (cpu_intel)
2200 			error = vm_get_vmcs_field(ctx, vcpu, VMCS_MSR_BITMAP,
2201 						  &addr);
2202 		else
2203 			error = vm_get_vmcb_field(ctx, vcpu,
2204 						  VMCB_OFF_MSR_PERM, 8, &addr);
2205 		if (error == 0)
2206 			printf("msr_bitmap[%d]\t\t%#lx\n", vcpu, addr);
2207 	}
2208 
2209 	if (!error && (get_msr_bitmap || get_all)) {
2210 		if (cpu_intel) {
2211 			error = vm_get_vmcs_field(ctx, vcpu,
2212 						  VMCS_MSR_BITMAP, &addr);
2213 		} else {
2214 			error = vm_get_vmcb_field(ctx, vcpu,
2215 						  VMCB_OFF_MSR_PERM, 8,
2216 						  &addr);
2217 		}
2218 
2219 #ifdef __FreeBSD__
2220 		if (error == 0)
2221 			error = dump_msr_bitmap(vcpu, addr, cpu_intel);
2222 #else
2223 		/*
2224 		 * Skip dumping the MSR bitmap since raw access to the VMCS is
2225 		 * currently not possible.
2226 		 */
2227 #endif /* __FreeBSD__ */
2228 	}
2229 
2230 	if (!error && (get_vpid_asid || get_all)) {
2231 		uint64_t vpid;
2232 		if (cpu_intel)
2233 			error = vm_get_vmcs_field(ctx, vcpu, VMCS_VPID, &vpid);
2234 		else
2235 			error = vm_get_vmcb_field(ctx, vcpu, VMCB_OFF_ASID,
2236 						  4, &vpid);
2237 		if (error == 0)
2238 			printf("%s[%d]\t\t0x%04lx\n",
2239 				cpu_intel ? "vpid" : "asid", vcpu, vpid);
2240 	}
2241 
2242 	if (!error && (get_guest_pat || get_all)) {
2243 		if (cpu_intel)
2244 			error = vm_get_vmcs_field(ctx, vcpu,
2245 						  VMCS_GUEST_IA32_PAT, &pat);
2246 		else
2247 			error = vm_get_vmcb_field(ctx, vcpu,
2248 						  VMCB_OFF_GUEST_PAT, 8, &pat);
2249 		if (error == 0)
2250 			printf("guest_pat[%d]\t\t0x%016lx\n", vcpu, pat);
2251 	}
2252 
2253 	if (!error && (get_guest_sysenter || get_all)) {
2254 		if (cpu_intel)
2255 			error = vm_get_vmcs_field(ctx, vcpu,
2256 						  VMCS_GUEST_IA32_SYSENTER_CS,
2257 						  &cs);
2258 		else
2259 			error = vm_get_vmcb_field(ctx, vcpu,
2260 						  VMCB_OFF_SYSENTER_CS, 8,
2261 						  &cs);
2262 
2263 		if (error == 0)
2264 			printf("guest_sysenter_cs[%d]\t%#lx\n", vcpu, cs);
2265 		if (cpu_intel)
2266 			error = vm_get_vmcs_field(ctx, vcpu,
2267 						  VMCS_GUEST_IA32_SYSENTER_ESP,
2268 						  &rsp);
2269 		else
2270 			error = vm_get_vmcb_field(ctx, vcpu,
2271 						  VMCB_OFF_SYSENTER_ESP, 8,
2272 						  &rsp);
2273 
2274 		if (error == 0)
2275 			printf("guest_sysenter_sp[%d]\t%#lx\n", vcpu, rsp);
2276 		if (cpu_intel)
2277 			error = vm_get_vmcs_field(ctx, vcpu,
2278 						  VMCS_GUEST_IA32_SYSENTER_EIP,
2279 						  &rip);
2280 		else
2281 			error = vm_get_vmcb_field(ctx, vcpu,
2282 						  VMCB_OFF_SYSENTER_EIP, 8,
2283 						  &rip);
2284 		if (error == 0)
2285 			printf("guest_sysenter_ip[%d]\t%#lx\n", vcpu, rip);
2286 	}
2287 
2288 	if (!error && (get_exit_reason || get_all)) {
2289 		if (cpu_intel)
2290 			error = vm_get_vmcs_field(ctx, vcpu, VMCS_EXIT_REASON,
2291 						  &u64);
2292 		else
2293 			error = vm_get_vmcb_field(ctx, vcpu,
2294 						  VMCB_OFF_EXIT_REASON, 8,
2295 						  &u64);
2296 		if (error == 0)
2297 			printf("exit_reason[%d]\t%#lx\n", vcpu, u64);
2298 	}
2299 
2300 	if (!error && setcap) {
2301 		int captype;
2302 		captype = vm_capability_name2type(capname);
2303 		error = vm_set_capability(ctx, vcpu, captype, capval);
2304 		if (error != 0 && errno == ENOENT)
2305 			printf("Capability \"%s\" is not available\n", capname);
2306 	}
2307 
2308 	if (!error && get_gpa_pmap) {
2309 		error = vm_get_gpa_pmap(ctx, gpa_pmap, pteval, &ptenum);
2310 		if (error == 0) {
2311 			printf("gpa %#lx:", gpa_pmap);
2312 			pte = &pteval[0];
2313 			while (ptenum-- > 0)
2314 				printf(" %#lx", *pte++);
2315 			printf("\n");
2316 		}
2317 	}
2318 
2319 	if (!error && set_rtc_nvram)
2320 		error = vm_rtc_write(ctx, rtc_nvram_offset, rtc_nvram_value);
2321 
2322 	if (!error && (get_rtc_nvram || get_all)) {
2323 		error = vm_rtc_read(ctx, rtc_nvram_offset, &rtc_nvram_value);
2324 		if (error == 0) {
2325 			printf("rtc nvram[%03d]: 0x%02x\n", rtc_nvram_offset,
2326 			    rtc_nvram_value);
2327 		}
2328 	}
2329 
2330 	if (!error && set_rtc_time)
2331 		error = vm_rtc_settime(ctx, rtc_secs);
2332 
2333 	if (!error && (get_rtc_time || get_all)) {
2334 		error = vm_rtc_gettime(ctx, &rtc_secs);
2335 		if (error == 0) {
2336 			gmtime_r(&rtc_secs, &tm);
2337 			printf("rtc time %#lx: %s %s %02d %02d:%02d:%02d %d\n",
2338 			    rtc_secs, wday_str(tm.tm_wday), mon_str(tm.tm_mon),
2339 			    tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
2340 			    1900 + tm.tm_year);
2341 		}
2342 	}
2343 
2344 	if (!error && (getcap || get_all)) {
2345 		int captype, val, getcaptype;
2346 
2347 		if (getcap && capname)
2348 			getcaptype = vm_capability_name2type(capname);
2349 		else
2350 			getcaptype = -1;
2351 
2352 		for (captype = 0; captype < VM_CAP_MAX; captype++) {
2353 			if (getcaptype >= 0 && captype != getcaptype)
2354 				continue;
2355 			error = vm_get_capability(ctx, vcpu, captype, &val);
2356 			if (error == 0) {
2357 				printf("Capability \"%s\" is %s on vcpu %d\n",
2358 					vm_capability_type2name(captype),
2359 					val ? "set" : "not set", vcpu);
2360 			} else if (errno == ENOENT) {
2361 				error = 0;
2362 				printf("Capability \"%s\" is not available\n",
2363 					vm_capability_type2name(captype));
2364 			} else {
2365 				break;
2366 			}
2367 		}
2368 	}
2369 
2370 	if (!error && (get_active_cpus || get_all)) {
2371 		error = vm_active_cpus(ctx, &cpus);
2372 		if (!error)
2373 			print_cpus("active cpus", &cpus);
2374 	}
2375 
2376 	if (!error && (get_suspended_cpus || get_all)) {
2377 		error = vm_suspended_cpus(ctx, &cpus);
2378 		if (!error)
2379 			print_cpus("suspended cpus", &cpus);
2380 	}
2381 
2382 	if (!error && (get_intinfo || get_all)) {
2383 		error = vm_get_intinfo(ctx, vcpu, &info[0], &info[1]);
2384 		if (!error) {
2385 			print_intinfo("pending", info[0]);
2386 			print_intinfo("current", info[1]);
2387 		}
2388 	}
2389 
2390 	if (!error && (get_stats || get_all)) {
2391 		int i, num_stats;
2392 		uint64_t *stats;
2393 		struct timeval tv;
2394 		const char *desc;
2395 
2396 		stats = vm_get_stats(ctx, vcpu, &tv, &num_stats);
2397 		if (stats != NULL) {
2398 			printf("vcpu%d stats:\n", vcpu);
2399 			for (i = 0; i < num_stats; i++) {
2400 				desc = vm_get_stat_desc(ctx, i);
2401 				printf("%-40s\t%ld\n", desc, stats[i]);
2402 			}
2403 		}
2404 	}
2405 
2406 	if (!error && (get_cpu_topology || get_all)) {
2407 		uint16_t sockets, cores, threads, maxcpus;
2408 
2409 		vm_get_topology(ctx, &sockets, &cores, &threads, &maxcpus);
2410 		printf("cpu_topology:\tsockets=%hu, cores=%hu, threads=%hu, "
2411 		    "maxcpus=%hu\n", sockets, cores, threads, maxcpus);
2412 	}
2413 
2414 	if (!error && run) {
2415 		struct vm_entry entry;
2416 
2417 		bzero(&entry, sizeof (entry));
2418 
2419 		error = vm_run(ctx, vcpu, &entry, &vmexit);
2420 		if (error == 0)
2421 			dump_vm_run_exitcode(&vmexit, vcpu);
2422 		else
2423 			printf("vm_run error %d\n", error);
2424 	}
2425 
2426 	if (!error && force_reset)
2427 		error = vm_suspend(ctx, VM_SUSPEND_RESET);
2428 
2429 	if (!error && force_poweroff)
2430 		error = vm_suspend(ctx, VM_SUSPEND_POWEROFF);
2431 
2432 	if (error)
2433 		printf("errno = %d\n", errno);
2434 
2435 	if (!error && destroy)
2436 		vm_destroy(ctx);
2437 
2438 	free (opts);
2439 	exit(error);
2440 }
2441