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