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