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