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