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