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