1 /*- 2 * Copyright (c) 2011 NetApp, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/smp.h> 35 #include <sys/kernel.h> 36 #include <sys/malloc.h> 37 #include <sys/pcpu.h> 38 #include <sys/proc.h> 39 #include <sys/sysctl.h> 40 41 #include <vm/vm.h> 42 #include <vm/pmap.h> 43 44 #include <machine/psl.h> 45 #include <machine/cpufunc.h> 46 #include <machine/md_var.h> 47 #include <machine/segments.h> 48 #include <machine/smp.h> 49 #include <machine/specialreg.h> 50 #include <machine/vmparam.h> 51 52 #include <machine/vmm.h> 53 #include "vmm_host.h" 54 #include "vmm_ipi.h" 55 #include "vmm_msr.h" 56 #include "vmm_ktr.h" 57 #include "vmm_stat.h" 58 #include "vlapic.h" 59 #include "vlapic_priv.h" 60 61 #include "vmx_msr.h" 62 #include "ept.h" 63 #include "vmx_cpufunc.h" 64 #include "vmx.h" 65 #include "x86.h" 66 #include "vmx_controls.h" 67 68 #define PINBASED_CTLS_ONE_SETTING \ 69 (PINBASED_EXTINT_EXITING | \ 70 PINBASED_NMI_EXITING | \ 71 PINBASED_VIRTUAL_NMI) 72 #define PINBASED_CTLS_ZERO_SETTING 0 73 74 #define PROCBASED_CTLS_WINDOW_SETTING \ 75 (PROCBASED_INT_WINDOW_EXITING | \ 76 PROCBASED_NMI_WINDOW_EXITING) 77 78 #define PROCBASED_CTLS_ONE_SETTING \ 79 (PROCBASED_SECONDARY_CONTROLS | \ 80 PROCBASED_IO_EXITING | \ 81 PROCBASED_MSR_BITMAPS | \ 82 PROCBASED_CTLS_WINDOW_SETTING) 83 #define PROCBASED_CTLS_ZERO_SETTING \ 84 (PROCBASED_CR3_LOAD_EXITING | \ 85 PROCBASED_CR3_STORE_EXITING | \ 86 PROCBASED_IO_BITMAPS) 87 88 #define PROCBASED_CTLS2_ONE_SETTING PROCBASED2_ENABLE_EPT 89 #define PROCBASED_CTLS2_ZERO_SETTING 0 90 91 #define VM_EXIT_CTLS_ONE_SETTING_NO_PAT \ 92 (VM_EXIT_HOST_LMA | \ 93 VM_EXIT_SAVE_EFER | \ 94 VM_EXIT_LOAD_EFER) 95 96 #define VM_EXIT_CTLS_ONE_SETTING \ 97 (VM_EXIT_CTLS_ONE_SETTING_NO_PAT | \ 98 VM_EXIT_ACKNOWLEDGE_INTERRUPT | \ 99 VM_EXIT_SAVE_PAT | \ 100 VM_EXIT_LOAD_PAT) 101 #define VM_EXIT_CTLS_ZERO_SETTING VM_EXIT_SAVE_DEBUG_CONTROLS 102 103 #define VM_ENTRY_CTLS_ONE_SETTING_NO_PAT VM_ENTRY_LOAD_EFER 104 105 #define VM_ENTRY_CTLS_ONE_SETTING \ 106 (VM_ENTRY_CTLS_ONE_SETTING_NO_PAT | \ 107 VM_ENTRY_LOAD_PAT) 108 #define VM_ENTRY_CTLS_ZERO_SETTING \ 109 (VM_ENTRY_LOAD_DEBUG_CONTROLS | \ 110 VM_ENTRY_INTO_SMM | \ 111 VM_ENTRY_DEACTIVATE_DUAL_MONITOR) 112 113 #define guest_msr_rw(vmx, msr) \ 114 msr_bitmap_change_access((vmx)->msr_bitmap, (msr), MSR_BITMAP_ACCESS_RW) 115 116 #define HANDLED 1 117 #define UNHANDLED 0 118 119 static MALLOC_DEFINE(M_VMX, "vmx", "vmx"); 120 static MALLOC_DEFINE(M_VLAPIC, "vlapic", "vlapic"); 121 122 SYSCTL_DECL(_hw_vmm); 123 SYSCTL_NODE(_hw_vmm, OID_AUTO, vmx, CTLFLAG_RW, NULL, NULL); 124 125 int vmxon_enabled[MAXCPU]; 126 static char vmxon_region[MAXCPU][PAGE_SIZE] __aligned(PAGE_SIZE); 127 128 static uint32_t pinbased_ctls, procbased_ctls, procbased_ctls2; 129 static uint32_t exit_ctls, entry_ctls; 130 131 static uint64_t cr0_ones_mask, cr0_zeros_mask; 132 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr0_ones_mask, CTLFLAG_RD, 133 &cr0_ones_mask, 0, NULL); 134 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr0_zeros_mask, CTLFLAG_RD, 135 &cr0_zeros_mask, 0, NULL); 136 137 static uint64_t cr4_ones_mask, cr4_zeros_mask; 138 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr4_ones_mask, CTLFLAG_RD, 139 &cr4_ones_mask, 0, NULL); 140 SYSCTL_ULONG(_hw_vmm_vmx, OID_AUTO, cr4_zeros_mask, CTLFLAG_RD, 141 &cr4_zeros_mask, 0, NULL); 142 143 static int vmx_no_patmsr; 144 145 static int vmx_initialized; 146 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, initialized, CTLFLAG_RD, 147 &vmx_initialized, 0, "Intel VMX initialized"); 148 149 /* 150 * Optional capabilities 151 */ 152 static int cap_halt_exit; 153 static int cap_pause_exit; 154 static int cap_unrestricted_guest; 155 static int cap_monitor_trap; 156 static int cap_invpcid; 157 158 static int virtual_interrupt_delivery; 159 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, virtual_interrupt_delivery, CTLFLAG_RD, 160 &virtual_interrupt_delivery, 0, "APICv virtual interrupt delivery support"); 161 162 static int posted_interrupts; 163 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, posted_interrupts, CTLFLAG_RD, 164 &posted_interrupts, 0, "APICv posted interrupt support"); 165 166 static int pirvec; 167 SYSCTL_INT(_hw_vmm_vmx, OID_AUTO, posted_interrupt_vector, CTLFLAG_RD, 168 &pirvec, 0, "APICv posted interrupt vector"); 169 170 static struct unrhdr *vpid_unr; 171 static u_int vpid_alloc_failed; 172 SYSCTL_UINT(_hw_vmm_vmx, OID_AUTO, vpid_alloc_failed, CTLFLAG_RD, 173 &vpid_alloc_failed, 0, NULL); 174 175 /* 176 * Use the last page below 4GB as the APIC access address. This address is 177 * occupied by the boot firmware so it is guaranteed that it will not conflict 178 * with a page in system memory. 179 */ 180 #define APIC_ACCESS_ADDRESS 0xFFFFF000 181 182 static void vmx_inject_pir(struct vlapic *vlapic); 183 184 #ifdef KTR 185 static const char * 186 exit_reason_to_str(int reason) 187 { 188 static char reasonbuf[32]; 189 190 switch (reason) { 191 case EXIT_REASON_EXCEPTION: 192 return "exception"; 193 case EXIT_REASON_EXT_INTR: 194 return "extint"; 195 case EXIT_REASON_TRIPLE_FAULT: 196 return "triplefault"; 197 case EXIT_REASON_INIT: 198 return "init"; 199 case EXIT_REASON_SIPI: 200 return "sipi"; 201 case EXIT_REASON_IO_SMI: 202 return "iosmi"; 203 case EXIT_REASON_SMI: 204 return "smi"; 205 case EXIT_REASON_INTR_WINDOW: 206 return "intrwindow"; 207 case EXIT_REASON_NMI_WINDOW: 208 return "nmiwindow"; 209 case EXIT_REASON_TASK_SWITCH: 210 return "taskswitch"; 211 case EXIT_REASON_CPUID: 212 return "cpuid"; 213 case EXIT_REASON_GETSEC: 214 return "getsec"; 215 case EXIT_REASON_HLT: 216 return "hlt"; 217 case EXIT_REASON_INVD: 218 return "invd"; 219 case EXIT_REASON_INVLPG: 220 return "invlpg"; 221 case EXIT_REASON_RDPMC: 222 return "rdpmc"; 223 case EXIT_REASON_RDTSC: 224 return "rdtsc"; 225 case EXIT_REASON_RSM: 226 return "rsm"; 227 case EXIT_REASON_VMCALL: 228 return "vmcall"; 229 case EXIT_REASON_VMCLEAR: 230 return "vmclear"; 231 case EXIT_REASON_VMLAUNCH: 232 return "vmlaunch"; 233 case EXIT_REASON_VMPTRLD: 234 return "vmptrld"; 235 case EXIT_REASON_VMPTRST: 236 return "vmptrst"; 237 case EXIT_REASON_VMREAD: 238 return "vmread"; 239 case EXIT_REASON_VMRESUME: 240 return "vmresume"; 241 case EXIT_REASON_VMWRITE: 242 return "vmwrite"; 243 case EXIT_REASON_VMXOFF: 244 return "vmxoff"; 245 case EXIT_REASON_VMXON: 246 return "vmxon"; 247 case EXIT_REASON_CR_ACCESS: 248 return "craccess"; 249 case EXIT_REASON_DR_ACCESS: 250 return "draccess"; 251 case EXIT_REASON_INOUT: 252 return "inout"; 253 case EXIT_REASON_RDMSR: 254 return "rdmsr"; 255 case EXIT_REASON_WRMSR: 256 return "wrmsr"; 257 case EXIT_REASON_INVAL_VMCS: 258 return "invalvmcs"; 259 case EXIT_REASON_INVAL_MSR: 260 return "invalmsr"; 261 case EXIT_REASON_MWAIT: 262 return "mwait"; 263 case EXIT_REASON_MTF: 264 return "mtf"; 265 case EXIT_REASON_MONITOR: 266 return "monitor"; 267 case EXIT_REASON_PAUSE: 268 return "pause"; 269 case EXIT_REASON_MCE: 270 return "mce"; 271 case EXIT_REASON_TPR: 272 return "tpr"; 273 case EXIT_REASON_APIC_ACCESS: 274 return "apic-access"; 275 case EXIT_REASON_GDTR_IDTR: 276 return "gdtridtr"; 277 case EXIT_REASON_LDTR_TR: 278 return "ldtrtr"; 279 case EXIT_REASON_EPT_FAULT: 280 return "eptfault"; 281 case EXIT_REASON_EPT_MISCONFIG: 282 return "eptmisconfig"; 283 case EXIT_REASON_INVEPT: 284 return "invept"; 285 case EXIT_REASON_RDTSCP: 286 return "rdtscp"; 287 case EXIT_REASON_VMX_PREEMPT: 288 return "vmxpreempt"; 289 case EXIT_REASON_INVVPID: 290 return "invvpid"; 291 case EXIT_REASON_WBINVD: 292 return "wbinvd"; 293 case EXIT_REASON_XSETBV: 294 return "xsetbv"; 295 case EXIT_REASON_APIC_WRITE: 296 return "apic-write"; 297 default: 298 snprintf(reasonbuf, sizeof(reasonbuf), "%d", reason); 299 return (reasonbuf); 300 } 301 } 302 #endif /* KTR */ 303 304 u_long 305 vmx_fix_cr0(u_long cr0) 306 { 307 308 return ((cr0 | cr0_ones_mask) & ~cr0_zeros_mask); 309 } 310 311 u_long 312 vmx_fix_cr4(u_long cr4) 313 { 314 315 return ((cr4 | cr4_ones_mask) & ~cr4_zeros_mask); 316 } 317 318 static void 319 vpid_free(int vpid) 320 { 321 if (vpid < 0 || vpid > 0xffff) 322 panic("vpid_free: invalid vpid %d", vpid); 323 324 /* 325 * VPIDs [0,VM_MAXCPU] are special and are not allocated from 326 * the unit number allocator. 327 */ 328 329 if (vpid > VM_MAXCPU) 330 free_unr(vpid_unr, vpid); 331 } 332 333 static void 334 vpid_alloc(uint16_t *vpid, int num) 335 { 336 int i, x; 337 338 if (num <= 0 || num > VM_MAXCPU) 339 panic("invalid number of vpids requested: %d", num); 340 341 /* 342 * If the "enable vpid" execution control is not enabled then the 343 * VPID is required to be 0 for all vcpus. 344 */ 345 if ((procbased_ctls2 & PROCBASED2_ENABLE_VPID) == 0) { 346 for (i = 0; i < num; i++) 347 vpid[i] = 0; 348 return; 349 } 350 351 /* 352 * Allocate a unique VPID for each vcpu from the unit number allocator. 353 */ 354 for (i = 0; i < num; i++) { 355 x = alloc_unr(vpid_unr); 356 if (x == -1) 357 break; 358 else 359 vpid[i] = x; 360 } 361 362 if (i < num) { 363 atomic_add_int(&vpid_alloc_failed, 1); 364 365 /* 366 * If the unit number allocator does not have enough unique 367 * VPIDs then we need to allocate from the [1,VM_MAXCPU] range. 368 * 369 * These VPIDs are not be unique across VMs but this does not 370 * affect correctness because the combined mappings are also 371 * tagged with the EP4TA which is unique for each VM. 372 * 373 * It is still sub-optimal because the invvpid will invalidate 374 * combined mappings for a particular VPID across all EP4TAs. 375 */ 376 while (i-- > 0) 377 vpid_free(vpid[i]); 378 379 for (i = 0; i < num; i++) 380 vpid[i] = i + 1; 381 } 382 } 383 384 static void 385 vpid_init(void) 386 { 387 /* 388 * VPID 0 is required when the "enable VPID" execution control is 389 * disabled. 390 * 391 * VPIDs [1,VM_MAXCPU] are used as the "overflow namespace" when the 392 * unit number allocator does not have sufficient unique VPIDs to 393 * satisfy the allocation. 394 * 395 * The remaining VPIDs are managed by the unit number allocator. 396 */ 397 vpid_unr = new_unrhdr(VM_MAXCPU + 1, 0xffff, NULL); 398 } 399 400 static void 401 msr_save_area_init(struct msr_entry *g_area, int *g_count) 402 { 403 int cnt; 404 405 static struct msr_entry guest_msrs[] = { 406 { MSR_KGSBASE, 0, 0 }, 407 }; 408 409 cnt = sizeof(guest_msrs) / sizeof(guest_msrs[0]); 410 if (cnt > GUEST_MSR_MAX_ENTRIES) 411 panic("guest msr save area overrun"); 412 bcopy(guest_msrs, g_area, sizeof(guest_msrs)); 413 *g_count = cnt; 414 } 415 416 static void 417 vmx_disable(void *arg __unused) 418 { 419 struct invvpid_desc invvpid_desc = { 0 }; 420 struct invept_desc invept_desc = { 0 }; 421 422 if (vmxon_enabled[curcpu]) { 423 /* 424 * See sections 25.3.3.3 and 25.3.3.4 in Intel Vol 3b. 425 * 426 * VMXON or VMXOFF are not required to invalidate any TLB 427 * caching structures. This prevents potential retention of 428 * cached information in the TLB between distinct VMX episodes. 429 */ 430 invvpid(INVVPID_TYPE_ALL_CONTEXTS, invvpid_desc); 431 invept(INVEPT_TYPE_ALL_CONTEXTS, invept_desc); 432 vmxoff(); 433 } 434 load_cr4(rcr4() & ~CR4_VMXE); 435 } 436 437 static int 438 vmx_cleanup(void) 439 { 440 441 if (pirvec != 0) 442 vmm_ipi_free(pirvec); 443 444 if (vpid_unr != NULL) { 445 delete_unrhdr(vpid_unr); 446 vpid_unr = NULL; 447 } 448 449 smp_rendezvous(NULL, vmx_disable, NULL, NULL); 450 451 return (0); 452 } 453 454 static void 455 vmx_enable(void *arg __unused) 456 { 457 int error; 458 459 load_cr4(rcr4() | CR4_VMXE); 460 461 *(uint32_t *)vmxon_region[curcpu] = vmx_revision(); 462 error = vmxon(vmxon_region[curcpu]); 463 if (error == 0) 464 vmxon_enabled[curcpu] = 1; 465 } 466 467 static void 468 vmx_restore(void) 469 { 470 471 if (vmxon_enabled[curcpu]) 472 vmxon(vmxon_region[curcpu]); 473 } 474 475 static int 476 vmx_init(int ipinum) 477 { 478 int error, use_tpr_shadow; 479 uint64_t fixed0, fixed1, feature_control; 480 uint32_t tmp, procbased2_vid_bits; 481 482 /* CPUID.1:ECX[bit 5] must be 1 for processor to support VMX */ 483 if (!(cpu_feature2 & CPUID2_VMX)) { 484 printf("vmx_init: processor does not support VMX operation\n"); 485 return (ENXIO); 486 } 487 488 /* 489 * Verify that MSR_IA32_FEATURE_CONTROL lock and VMXON enable bits 490 * are set (bits 0 and 2 respectively). 491 */ 492 feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL); 493 if ((feature_control & IA32_FEATURE_CONTROL_LOCK) == 0 || 494 (feature_control & IA32_FEATURE_CONTROL_VMX_EN) == 0) { 495 printf("vmx_init: VMX operation disabled by BIOS\n"); 496 return (ENXIO); 497 } 498 499 /* Check support for primary processor-based VM-execution controls */ 500 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS, 501 MSR_VMX_TRUE_PROCBASED_CTLS, 502 PROCBASED_CTLS_ONE_SETTING, 503 PROCBASED_CTLS_ZERO_SETTING, &procbased_ctls); 504 if (error) { 505 printf("vmx_init: processor does not support desired primary " 506 "processor-based controls\n"); 507 return (error); 508 } 509 510 /* Clear the processor-based ctl bits that are set on demand */ 511 procbased_ctls &= ~PROCBASED_CTLS_WINDOW_SETTING; 512 513 /* Check support for secondary processor-based VM-execution controls */ 514 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, 515 MSR_VMX_PROCBASED_CTLS2, 516 PROCBASED_CTLS2_ONE_SETTING, 517 PROCBASED_CTLS2_ZERO_SETTING, &procbased_ctls2); 518 if (error) { 519 printf("vmx_init: processor does not support desired secondary " 520 "processor-based controls\n"); 521 return (error); 522 } 523 524 /* Check support for VPID */ 525 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, MSR_VMX_PROCBASED_CTLS2, 526 PROCBASED2_ENABLE_VPID, 0, &tmp); 527 if (error == 0) 528 procbased_ctls2 |= PROCBASED2_ENABLE_VPID; 529 530 /* Check support for pin-based VM-execution controls */ 531 error = vmx_set_ctlreg(MSR_VMX_PINBASED_CTLS, 532 MSR_VMX_TRUE_PINBASED_CTLS, 533 PINBASED_CTLS_ONE_SETTING, 534 PINBASED_CTLS_ZERO_SETTING, &pinbased_ctls); 535 if (error) { 536 printf("vmx_init: processor does not support desired " 537 "pin-based controls\n"); 538 return (error); 539 } 540 541 /* Check support for VM-exit controls */ 542 error = vmx_set_ctlreg(MSR_VMX_EXIT_CTLS, MSR_VMX_TRUE_EXIT_CTLS, 543 VM_EXIT_CTLS_ONE_SETTING, 544 VM_EXIT_CTLS_ZERO_SETTING, 545 &exit_ctls); 546 if (error) { 547 /* Try again without the PAT MSR bits */ 548 error = vmx_set_ctlreg(MSR_VMX_EXIT_CTLS, 549 MSR_VMX_TRUE_EXIT_CTLS, 550 VM_EXIT_CTLS_ONE_SETTING_NO_PAT, 551 VM_EXIT_CTLS_ZERO_SETTING, 552 &exit_ctls); 553 if (error) { 554 printf("vmx_init: processor does not support desired " 555 "exit controls\n"); 556 return (error); 557 } else { 558 if (bootverbose) 559 printf("vmm: PAT MSR access not supported\n"); 560 guest_msr_valid(MSR_PAT); 561 vmx_no_patmsr = 1; 562 } 563 } 564 565 /* Check support for VM-entry controls */ 566 if (!vmx_no_patmsr) { 567 error = vmx_set_ctlreg(MSR_VMX_ENTRY_CTLS, 568 MSR_VMX_TRUE_ENTRY_CTLS, 569 VM_ENTRY_CTLS_ONE_SETTING, 570 VM_ENTRY_CTLS_ZERO_SETTING, 571 &entry_ctls); 572 } else { 573 error = vmx_set_ctlreg(MSR_VMX_ENTRY_CTLS, 574 MSR_VMX_TRUE_ENTRY_CTLS, 575 VM_ENTRY_CTLS_ONE_SETTING_NO_PAT, 576 VM_ENTRY_CTLS_ZERO_SETTING, 577 &entry_ctls); 578 } 579 580 if (error) { 581 printf("vmx_init: processor does not support desired " 582 "entry controls\n"); 583 return (error); 584 } 585 586 /* 587 * Check support for optional features by testing them 588 * as individual bits 589 */ 590 cap_halt_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS, 591 MSR_VMX_TRUE_PROCBASED_CTLS, 592 PROCBASED_HLT_EXITING, 0, 593 &tmp) == 0); 594 595 cap_monitor_trap = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS, 596 MSR_VMX_PROCBASED_CTLS, 597 PROCBASED_MTF, 0, 598 &tmp) == 0); 599 600 cap_pause_exit = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS, 601 MSR_VMX_TRUE_PROCBASED_CTLS, 602 PROCBASED_PAUSE_EXITING, 0, 603 &tmp) == 0); 604 605 cap_unrestricted_guest = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, 606 MSR_VMX_PROCBASED_CTLS2, 607 PROCBASED2_UNRESTRICTED_GUEST, 0, 608 &tmp) == 0); 609 610 cap_invpcid = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, 611 MSR_VMX_PROCBASED_CTLS2, PROCBASED2_ENABLE_INVPCID, 0, 612 &tmp) == 0); 613 614 /* 615 * Check support for virtual interrupt delivery. 616 */ 617 procbased2_vid_bits = (PROCBASED2_VIRTUALIZE_APIC_ACCESSES | 618 PROCBASED2_VIRTUALIZE_X2APIC_MODE | 619 PROCBASED2_APIC_REGISTER_VIRTUALIZATION | 620 PROCBASED2_VIRTUAL_INTERRUPT_DELIVERY); 621 622 use_tpr_shadow = (vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS, 623 MSR_VMX_TRUE_PROCBASED_CTLS, PROCBASED_USE_TPR_SHADOW, 0, 624 &tmp) == 0); 625 626 error = vmx_set_ctlreg(MSR_VMX_PROCBASED_CTLS2, MSR_VMX_PROCBASED_CTLS2, 627 procbased2_vid_bits, 0, &tmp); 628 if (error == 0 && use_tpr_shadow) { 629 virtual_interrupt_delivery = 1; 630 TUNABLE_INT_FETCH("hw.vmm.vmx.use_apic_vid", 631 &virtual_interrupt_delivery); 632 } 633 634 if (virtual_interrupt_delivery) { 635 procbased_ctls |= PROCBASED_USE_TPR_SHADOW; 636 procbased_ctls2 |= procbased2_vid_bits; 637 procbased_ctls2 &= ~PROCBASED2_VIRTUALIZE_X2APIC_MODE; 638 639 /* 640 * Check for Posted Interrupts only if Virtual Interrupt 641 * Delivery is enabled. 642 */ 643 error = vmx_set_ctlreg(MSR_VMX_PINBASED_CTLS, 644 MSR_VMX_TRUE_PINBASED_CTLS, PINBASED_POSTED_INTERRUPT, 0, 645 &tmp); 646 if (error == 0) { 647 pirvec = vmm_ipi_alloc(); 648 if (pirvec == 0) { 649 if (bootverbose) { 650 printf("vmx_init: unable to allocate " 651 "posted interrupt vector\n"); 652 } 653 } else { 654 posted_interrupts = 1; 655 TUNABLE_INT_FETCH("hw.vmm.vmx.use_apic_pir", 656 &posted_interrupts); 657 } 658 } 659 } 660 661 if (posted_interrupts) 662 pinbased_ctls |= PINBASED_POSTED_INTERRUPT; 663 664 /* Initialize EPT */ 665 error = ept_init(ipinum); 666 if (error) { 667 printf("vmx_init: ept initialization failed (%d)\n", error); 668 return (error); 669 } 670 671 /* 672 * Stash the cr0 and cr4 bits that must be fixed to 0 or 1 673 */ 674 fixed0 = rdmsr(MSR_VMX_CR0_FIXED0); 675 fixed1 = rdmsr(MSR_VMX_CR0_FIXED1); 676 cr0_ones_mask = fixed0 & fixed1; 677 cr0_zeros_mask = ~fixed0 & ~fixed1; 678 679 /* 680 * CR0_PE and CR0_PG can be set to zero in VMX non-root operation 681 * if unrestricted guest execution is allowed. 682 */ 683 if (cap_unrestricted_guest) 684 cr0_ones_mask &= ~(CR0_PG | CR0_PE); 685 686 /* 687 * Do not allow the guest to set CR0_NW or CR0_CD. 688 */ 689 cr0_zeros_mask |= (CR0_NW | CR0_CD); 690 691 fixed0 = rdmsr(MSR_VMX_CR4_FIXED0); 692 fixed1 = rdmsr(MSR_VMX_CR4_FIXED1); 693 cr4_ones_mask = fixed0 & fixed1; 694 cr4_zeros_mask = ~fixed0 & ~fixed1; 695 696 vpid_init(); 697 698 /* enable VMX operation */ 699 smp_rendezvous(NULL, vmx_enable, NULL, NULL); 700 701 vmx_initialized = 1; 702 703 return (0); 704 } 705 706 static void 707 vmx_trigger_hostintr(int vector) 708 { 709 uintptr_t func; 710 struct gate_descriptor *gd; 711 712 gd = &idt[vector]; 713 714 KASSERT(vector >= 32 && vector <= 255, ("vmx_trigger_hostintr: " 715 "invalid vector %d", vector)); 716 KASSERT(gd->gd_p == 1, ("gate descriptor for vector %d not present", 717 vector)); 718 KASSERT(gd->gd_type == SDT_SYSIGT, ("gate descriptor for vector %d " 719 "has invalid type %d", vector, gd->gd_type)); 720 KASSERT(gd->gd_dpl == SEL_KPL, ("gate descriptor for vector %d " 721 "has invalid dpl %d", vector, gd->gd_dpl)); 722 KASSERT(gd->gd_selector == GSEL(GCODE_SEL, SEL_KPL), ("gate descriptor " 723 "for vector %d has invalid selector %d", vector, gd->gd_selector)); 724 KASSERT(gd->gd_ist == 0, ("gate descriptor for vector %d has invalid " 725 "IST %d", vector, gd->gd_ist)); 726 727 func = ((long)gd->gd_hioffset << 16 | gd->gd_looffset); 728 vmx_call_isr(func); 729 } 730 731 static int 732 vmx_setup_cr_shadow(int which, struct vmcs *vmcs, uint32_t initial) 733 { 734 int error, mask_ident, shadow_ident; 735 uint64_t mask_value; 736 737 if (which != 0 && which != 4) 738 panic("vmx_setup_cr_shadow: unknown cr%d", which); 739 740 if (which == 0) { 741 mask_ident = VMCS_CR0_MASK; 742 mask_value = cr0_ones_mask | cr0_zeros_mask; 743 shadow_ident = VMCS_CR0_SHADOW; 744 } else { 745 mask_ident = VMCS_CR4_MASK; 746 mask_value = cr4_ones_mask | cr4_zeros_mask; 747 shadow_ident = VMCS_CR4_SHADOW; 748 } 749 750 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(mask_ident), mask_value); 751 if (error) 752 return (error); 753 754 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(shadow_ident), initial); 755 if (error) 756 return (error); 757 758 return (0); 759 } 760 #define vmx_setup_cr0_shadow(vmcs,init) vmx_setup_cr_shadow(0, (vmcs), (init)) 761 #define vmx_setup_cr4_shadow(vmcs,init) vmx_setup_cr_shadow(4, (vmcs), (init)) 762 763 static void * 764 vmx_vminit(struct vm *vm, pmap_t pmap) 765 { 766 uint16_t vpid[VM_MAXCPU]; 767 int i, error, guest_msr_count; 768 struct vmx *vmx; 769 struct vmcs *vmcs; 770 771 vmx = malloc(sizeof(struct vmx), M_VMX, M_WAITOK | M_ZERO); 772 if ((uintptr_t)vmx & PAGE_MASK) { 773 panic("malloc of struct vmx not aligned on %d byte boundary", 774 PAGE_SIZE); 775 } 776 vmx->vm = vm; 777 778 vmx->eptp = eptp(vtophys((vm_offset_t)pmap->pm_pml4)); 779 780 /* 781 * Clean up EPTP-tagged guest physical and combined mappings 782 * 783 * VMX transitions are not required to invalidate any guest physical 784 * mappings. So, it may be possible for stale guest physical mappings 785 * to be present in the processor TLBs. 786 * 787 * Combined mappings for this EP4TA are also invalidated for all VPIDs. 788 */ 789 ept_invalidate_mappings(vmx->eptp); 790 791 msr_bitmap_initialize(vmx->msr_bitmap); 792 793 /* 794 * It is safe to allow direct access to MSR_GSBASE and MSR_FSBASE. 795 * The guest FSBASE and GSBASE are saved and restored during 796 * vm-exit and vm-entry respectively. The host FSBASE and GSBASE are 797 * always restored from the vmcs host state area on vm-exit. 798 * 799 * The SYSENTER_CS/ESP/EIP MSRs are identical to FS/GSBASE in 800 * how they are saved/restored so can be directly accessed by the 801 * guest. 802 * 803 * Guest KGSBASE is saved and restored in the guest MSR save area. 804 * Host KGSBASE is restored before returning to userland from the pcb. 805 * There will be a window of time when we are executing in the host 806 * kernel context with a value of KGSBASE from the guest. This is ok 807 * because the value of KGSBASE is inconsequential in kernel context. 808 * 809 * MSR_EFER is saved and restored in the guest VMCS area on a 810 * VM exit and entry respectively. It is also restored from the 811 * host VMCS area on a VM exit. 812 */ 813 if (guest_msr_rw(vmx, MSR_GSBASE) || 814 guest_msr_rw(vmx, MSR_FSBASE) || 815 guest_msr_rw(vmx, MSR_SYSENTER_CS_MSR) || 816 guest_msr_rw(vmx, MSR_SYSENTER_ESP_MSR) || 817 guest_msr_rw(vmx, MSR_SYSENTER_EIP_MSR) || 818 guest_msr_rw(vmx, MSR_KGSBASE) || 819 guest_msr_rw(vmx, MSR_EFER)) 820 panic("vmx_vminit: error setting guest msr access"); 821 822 /* 823 * MSR_PAT is saved and restored in the guest VMCS are on a VM exit 824 * and entry respectively. It is also restored from the host VMCS 825 * area on a VM exit. However, if running on a system with no 826 * MSR_PAT save/restore support, leave access disabled so accesses 827 * will be trapped. 828 */ 829 if (!vmx_no_patmsr && guest_msr_rw(vmx, MSR_PAT)) 830 panic("vmx_vminit: error setting guest pat msr access"); 831 832 vpid_alloc(vpid, VM_MAXCPU); 833 834 if (virtual_interrupt_delivery) { 835 error = vm_map_mmio(vm, DEFAULT_APIC_BASE, PAGE_SIZE, 836 APIC_ACCESS_ADDRESS); 837 /* XXX this should really return an error to the caller */ 838 KASSERT(error == 0, ("vm_map_mmio(apicbase) error %d", error)); 839 } 840 841 for (i = 0; i < VM_MAXCPU; i++) { 842 vmcs = &vmx->vmcs[i]; 843 vmcs->identifier = vmx_revision(); 844 error = vmclear(vmcs); 845 if (error != 0) { 846 panic("vmx_vminit: vmclear error %d on vcpu %d\n", 847 error, i); 848 } 849 850 error = vmcs_init(vmcs); 851 KASSERT(error == 0, ("vmcs_init error %d", error)); 852 853 VMPTRLD(vmcs); 854 error = 0; 855 error += vmwrite(VMCS_HOST_RSP, (u_long)&vmx->ctx[i]); 856 error += vmwrite(VMCS_EPTP, vmx->eptp); 857 error += vmwrite(VMCS_PIN_BASED_CTLS, pinbased_ctls); 858 error += vmwrite(VMCS_PRI_PROC_BASED_CTLS, procbased_ctls); 859 error += vmwrite(VMCS_SEC_PROC_BASED_CTLS, procbased_ctls2); 860 error += vmwrite(VMCS_EXIT_CTLS, exit_ctls); 861 error += vmwrite(VMCS_ENTRY_CTLS, entry_ctls); 862 error += vmwrite(VMCS_MSR_BITMAP, vtophys(vmx->msr_bitmap)); 863 error += vmwrite(VMCS_VPID, vpid[i]); 864 if (virtual_interrupt_delivery) { 865 error += vmwrite(VMCS_APIC_ACCESS, APIC_ACCESS_ADDRESS); 866 error += vmwrite(VMCS_VIRTUAL_APIC, 867 vtophys(&vmx->apic_page[i])); 868 error += vmwrite(VMCS_EOI_EXIT0, 0); 869 error += vmwrite(VMCS_EOI_EXIT1, 0); 870 error += vmwrite(VMCS_EOI_EXIT2, 0); 871 error += vmwrite(VMCS_EOI_EXIT3, 0); 872 } 873 if (posted_interrupts) { 874 error += vmwrite(VMCS_PIR_VECTOR, pirvec); 875 error += vmwrite(VMCS_PIR_DESC, 876 vtophys(&vmx->pir_desc[i])); 877 } 878 VMCLEAR(vmcs); 879 KASSERT(error == 0, ("vmx_vminit: error customizing the vmcs")); 880 881 vmx->cap[i].set = 0; 882 vmx->cap[i].proc_ctls = procbased_ctls; 883 vmx->cap[i].proc_ctls2 = procbased_ctls2; 884 885 vmx->state[i].lastcpu = -1; 886 vmx->state[i].vpid = vpid[i]; 887 888 msr_save_area_init(vmx->guest_msrs[i], &guest_msr_count); 889 890 error = vmcs_set_msr_save(vmcs, vtophys(vmx->guest_msrs[i]), 891 guest_msr_count); 892 if (error != 0) 893 panic("vmcs_set_msr_save error %d", error); 894 895 /* 896 * Set up the CR0/4 shadows, and init the read shadow 897 * to the power-on register value from the Intel Sys Arch. 898 * CR0 - 0x60000010 899 * CR4 - 0 900 */ 901 error = vmx_setup_cr0_shadow(vmcs, 0x60000010); 902 if (error != 0) 903 panic("vmx_setup_cr0_shadow %d", error); 904 905 error = vmx_setup_cr4_shadow(vmcs, 0); 906 if (error != 0) 907 panic("vmx_setup_cr4_shadow %d", error); 908 909 vmx->ctx[i].pmap = pmap; 910 vmx->ctx[i].eptp = vmx->eptp; 911 } 912 913 return (vmx); 914 } 915 916 static int 917 vmx_handle_cpuid(struct vm *vm, int vcpu, struct vmxctx *vmxctx) 918 { 919 int handled, func; 920 921 func = vmxctx->guest_rax; 922 923 handled = x86_emulate_cpuid(vm, vcpu, 924 (uint32_t*)(&vmxctx->guest_rax), 925 (uint32_t*)(&vmxctx->guest_rbx), 926 (uint32_t*)(&vmxctx->guest_rcx), 927 (uint32_t*)(&vmxctx->guest_rdx)); 928 return (handled); 929 } 930 931 static __inline void 932 vmx_run_trace(struct vmx *vmx, int vcpu) 933 { 934 #ifdef KTR 935 VCPU_CTR1(vmx->vm, vcpu, "Resume execution at %#lx", vmcs_guest_rip()); 936 #endif 937 } 938 939 static __inline void 940 vmx_exit_trace(struct vmx *vmx, int vcpu, uint64_t rip, uint32_t exit_reason, 941 int handled) 942 { 943 #ifdef KTR 944 VCPU_CTR3(vmx->vm, vcpu, "%s %s vmexit at 0x%0lx", 945 handled ? "handled" : "unhandled", 946 exit_reason_to_str(exit_reason), rip); 947 #endif 948 } 949 950 static __inline void 951 vmx_astpending_trace(struct vmx *vmx, int vcpu, uint64_t rip) 952 { 953 #ifdef KTR 954 VCPU_CTR1(vmx->vm, vcpu, "astpending vmexit at 0x%0lx", rip); 955 #endif 956 } 957 958 static void 959 vmx_set_pcpu_defaults(struct vmx *vmx, int vcpu) 960 { 961 int lastcpu; 962 struct vmxstate *vmxstate; 963 struct invvpid_desc invvpid_desc = { 0 }; 964 965 vmxstate = &vmx->state[vcpu]; 966 lastcpu = vmxstate->lastcpu; 967 vmxstate->lastcpu = curcpu; 968 969 if (lastcpu == curcpu) 970 return; 971 972 vmm_stat_incr(vmx->vm, vcpu, VCPU_MIGRATIONS, 1); 973 974 vmcs_write(VMCS_HOST_TR_BASE, vmm_get_host_trbase()); 975 vmcs_write(VMCS_HOST_GDTR_BASE, vmm_get_host_gdtrbase()); 976 vmcs_write(VMCS_HOST_GS_BASE, vmm_get_host_gsbase()); 977 978 /* 979 * If we are using VPIDs then invalidate all mappings tagged with 'vpid' 980 * 981 * We do this because this vcpu was executing on a different host 982 * cpu when it last ran. We do not track whether it invalidated 983 * mappings associated with its 'vpid' during that run. So we must 984 * assume that the mappings associated with 'vpid' on 'curcpu' are 985 * stale and invalidate them. 986 * 987 * Note that we incur this penalty only when the scheduler chooses to 988 * move the thread associated with this vcpu between host cpus. 989 * 990 * Note also that this will invalidate mappings tagged with 'vpid' 991 * for "all" EP4TAs. 992 */ 993 if (vmxstate->vpid != 0) { 994 invvpid_desc.vpid = vmxstate->vpid; 995 invvpid(INVVPID_TYPE_SINGLE_CONTEXT, invvpid_desc); 996 } 997 } 998 999 /* 1000 * We depend on 'procbased_ctls' to have the Interrupt Window Exiting bit set. 1001 */ 1002 CTASSERT((PROCBASED_CTLS_ONE_SETTING & PROCBASED_INT_WINDOW_EXITING) != 0); 1003 1004 static void __inline 1005 vmx_set_int_window_exiting(struct vmx *vmx, int vcpu) 1006 { 1007 1008 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) == 0) { 1009 vmx->cap[vcpu].proc_ctls |= PROCBASED_INT_WINDOW_EXITING; 1010 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls); 1011 VCPU_CTR0(vmx->vm, vcpu, "Enabling interrupt window exiting"); 1012 } 1013 } 1014 1015 static void __inline 1016 vmx_clear_int_window_exiting(struct vmx *vmx, int vcpu) 1017 { 1018 1019 KASSERT((vmx->cap[vcpu].proc_ctls & PROCBASED_INT_WINDOW_EXITING) != 0, 1020 ("intr_window_exiting not set: %#x", vmx->cap[vcpu].proc_ctls)); 1021 vmx->cap[vcpu].proc_ctls &= ~PROCBASED_INT_WINDOW_EXITING; 1022 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls); 1023 VCPU_CTR0(vmx->vm, vcpu, "Disabling interrupt window exiting"); 1024 } 1025 1026 static void __inline 1027 vmx_set_nmi_window_exiting(struct vmx *vmx, int vcpu) 1028 { 1029 1030 if ((vmx->cap[vcpu].proc_ctls & PROCBASED_NMI_WINDOW_EXITING) == 0) { 1031 vmx->cap[vcpu].proc_ctls |= PROCBASED_NMI_WINDOW_EXITING; 1032 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls); 1033 VCPU_CTR0(vmx->vm, vcpu, "Enabling NMI window exiting"); 1034 } 1035 } 1036 1037 static void __inline 1038 vmx_clear_nmi_window_exiting(struct vmx *vmx, int vcpu) 1039 { 1040 1041 KASSERT((vmx->cap[vcpu].proc_ctls & PROCBASED_NMI_WINDOW_EXITING) != 0, 1042 ("nmi_window_exiting not set %#x", vmx->cap[vcpu].proc_ctls)); 1043 vmx->cap[vcpu].proc_ctls &= ~PROCBASED_NMI_WINDOW_EXITING; 1044 vmcs_write(VMCS_PRI_PROC_BASED_CTLS, vmx->cap[vcpu].proc_ctls); 1045 VCPU_CTR0(vmx->vm, vcpu, "Disabling NMI window exiting"); 1046 } 1047 1048 #define NMI_BLOCKING (VMCS_INTERRUPTIBILITY_NMI_BLOCKING | \ 1049 VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING) 1050 #define HWINTR_BLOCKING (VMCS_INTERRUPTIBILITY_STI_BLOCKING | \ 1051 VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING) 1052 1053 static void 1054 vmx_inject_nmi(struct vmx *vmx, int vcpu) 1055 { 1056 uint32_t gi, info; 1057 1058 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY); 1059 KASSERT((gi & NMI_BLOCKING) == 0, ("vmx_inject_nmi: invalid guest " 1060 "interruptibility-state %#x", gi)); 1061 1062 info = vmcs_read(VMCS_ENTRY_INTR_INFO); 1063 KASSERT((info & VMCS_INTR_VALID) == 0, ("vmx_inject_nmi: invalid " 1064 "VM-entry interruption information %#x", info)); 1065 1066 /* 1067 * Inject the virtual NMI. The vector must be the NMI IDT entry 1068 * or the VMCS entry check will fail. 1069 */ 1070 info = IDT_NMI | VMCS_INTR_T_NMI | VMCS_INTR_VALID; 1071 vmcs_write(VMCS_ENTRY_INTR_INFO, info); 1072 1073 VCPU_CTR0(vmx->vm, vcpu, "Injecting vNMI"); 1074 1075 /* Clear the request */ 1076 vm_nmi_clear(vmx->vm, vcpu); 1077 } 1078 1079 static void 1080 vmx_inject_interrupts(struct vmx *vmx, int vcpu, struct vlapic *vlapic) 1081 { 1082 int vector, need_nmi_exiting; 1083 uint64_t rflags; 1084 uint32_t gi, info; 1085 1086 if (vm_nmi_pending(vmx->vm, vcpu)) { 1087 /* 1088 * If there are no conditions blocking NMI injection then 1089 * inject it directly here otherwise enable "NMI window 1090 * exiting" to inject it as soon as we can. 1091 * 1092 * We also check for STI_BLOCKING because some implementations 1093 * don't allow NMI injection in this case. If we are running 1094 * on a processor that doesn't have this restriction it will 1095 * immediately exit and the NMI will be injected in the 1096 * "NMI window exiting" handler. 1097 */ 1098 need_nmi_exiting = 1; 1099 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY); 1100 if ((gi & (HWINTR_BLOCKING | NMI_BLOCKING)) == 0) { 1101 info = vmcs_read(VMCS_ENTRY_INTR_INFO); 1102 if ((info & VMCS_INTR_VALID) == 0) { 1103 vmx_inject_nmi(vmx, vcpu); 1104 need_nmi_exiting = 0; 1105 } else { 1106 VCPU_CTR1(vmx->vm, vcpu, "Cannot inject NMI " 1107 "due to VM-entry intr info %#x", info); 1108 } 1109 } else { 1110 VCPU_CTR1(vmx->vm, vcpu, "Cannot inject NMI due to " 1111 "Guest Interruptibility-state %#x", gi); 1112 } 1113 1114 if (need_nmi_exiting) 1115 vmx_set_nmi_window_exiting(vmx, vcpu); 1116 } 1117 1118 if (virtual_interrupt_delivery) { 1119 vmx_inject_pir(vlapic); 1120 return; 1121 } 1122 1123 /* 1124 * If there is already an interrupt pending then just return. This 1125 * could happen for multiple reasons: 1126 * - A vectoring VM-entry was aborted due to astpending or rendezvous. 1127 * - A VM-exit happened during event injection. 1128 * - A NMI was injected above or after "NMI window exiting" VM-exit. 1129 */ 1130 info = vmcs_read(VMCS_ENTRY_INTR_INFO); 1131 if (info & VMCS_INTR_VALID) 1132 return; 1133 1134 /* Ask the local apic for a vector to inject */ 1135 if (!vlapic_pending_intr(vlapic, &vector)) 1136 return; 1137 1138 KASSERT(vector >= 32 && vector <= 255, ("invalid vector %d", vector)); 1139 1140 /* Check RFLAGS.IF and the interruptibility state of the guest */ 1141 rflags = vmcs_read(VMCS_GUEST_RFLAGS); 1142 if ((rflags & PSL_I) == 0) 1143 goto cantinject; 1144 1145 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY); 1146 if (gi & HWINTR_BLOCKING) 1147 goto cantinject; 1148 1149 /* Inject the interrupt */ 1150 info = VMCS_INTR_T_HWINTR | VMCS_INTR_VALID; 1151 info |= vector; 1152 vmcs_write(VMCS_ENTRY_INTR_INFO, info); 1153 1154 /* Update the Local APIC ISR */ 1155 vlapic_intr_accepted(vlapic, vector); 1156 1157 VCPU_CTR1(vmx->vm, vcpu, "Injecting hwintr at vector %d", vector); 1158 1159 return; 1160 1161 cantinject: 1162 /* 1163 * Set the Interrupt Window Exiting execution control so we can inject 1164 * the interrupt as soon as blocking condition goes away. 1165 */ 1166 vmx_set_int_window_exiting(vmx, vcpu); 1167 } 1168 1169 /* 1170 * If the Virtual NMIs execution control is '1' then the logical processor 1171 * tracks virtual-NMI blocking in the Guest Interruptibility-state field of 1172 * the VMCS. An IRET instruction in VMX non-root operation will remove any 1173 * virtual-NMI blocking. 1174 * 1175 * This unblocking occurs even if the IRET causes a fault. In this case the 1176 * hypervisor needs to restore virtual-NMI blocking before resuming the guest. 1177 */ 1178 static void 1179 vmx_restore_nmi_blocking(struct vmx *vmx, int vcpuid) 1180 { 1181 uint32_t gi; 1182 1183 VCPU_CTR0(vmx->vm, vcpuid, "Restore Virtual-NMI blocking"); 1184 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY); 1185 gi |= VMCS_INTERRUPTIBILITY_NMI_BLOCKING; 1186 vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi); 1187 } 1188 1189 static void 1190 vmx_clear_nmi_blocking(struct vmx *vmx, int vcpuid) 1191 { 1192 uint32_t gi; 1193 1194 VCPU_CTR0(vmx->vm, vcpuid, "Clear Virtual-NMI blocking"); 1195 gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY); 1196 gi &= ~VMCS_INTERRUPTIBILITY_NMI_BLOCKING; 1197 vmcs_write(VMCS_GUEST_INTERRUPTIBILITY, gi); 1198 } 1199 1200 static int 1201 vmx_emulate_cr_access(struct vmx *vmx, int vcpu, uint64_t exitqual) 1202 { 1203 int cr, vmcs_guest_cr, vmcs_shadow_cr; 1204 uint64_t crval, regval, ones_mask, zeros_mask; 1205 const struct vmxctx *vmxctx; 1206 1207 /* We only handle mov to %cr0 or %cr4 at this time */ 1208 if ((exitqual & 0xf0) != 0x00) 1209 return (UNHANDLED); 1210 1211 cr = exitqual & 0xf; 1212 if (cr != 0 && cr != 4) 1213 return (UNHANDLED); 1214 1215 regval = 0; /* silence gcc */ 1216 vmxctx = &vmx->ctx[vcpu]; 1217 1218 /* 1219 * We must use vmcs_write() directly here because vmcs_setreg() will 1220 * call vmclear(vmcs) as a side-effect which we certainly don't want. 1221 */ 1222 switch ((exitqual >> 8) & 0xf) { 1223 case 0: 1224 regval = vmxctx->guest_rax; 1225 break; 1226 case 1: 1227 regval = vmxctx->guest_rcx; 1228 break; 1229 case 2: 1230 regval = vmxctx->guest_rdx; 1231 break; 1232 case 3: 1233 regval = vmxctx->guest_rbx; 1234 break; 1235 case 4: 1236 regval = vmcs_read(VMCS_GUEST_RSP); 1237 break; 1238 case 5: 1239 regval = vmxctx->guest_rbp; 1240 break; 1241 case 6: 1242 regval = vmxctx->guest_rsi; 1243 break; 1244 case 7: 1245 regval = vmxctx->guest_rdi; 1246 break; 1247 case 8: 1248 regval = vmxctx->guest_r8; 1249 break; 1250 case 9: 1251 regval = vmxctx->guest_r9; 1252 break; 1253 case 10: 1254 regval = vmxctx->guest_r10; 1255 break; 1256 case 11: 1257 regval = vmxctx->guest_r11; 1258 break; 1259 case 12: 1260 regval = vmxctx->guest_r12; 1261 break; 1262 case 13: 1263 regval = vmxctx->guest_r13; 1264 break; 1265 case 14: 1266 regval = vmxctx->guest_r14; 1267 break; 1268 case 15: 1269 regval = vmxctx->guest_r15; 1270 break; 1271 } 1272 1273 if (cr == 0) { 1274 ones_mask = cr0_ones_mask; 1275 zeros_mask = cr0_zeros_mask; 1276 vmcs_guest_cr = VMCS_GUEST_CR0; 1277 vmcs_shadow_cr = VMCS_CR0_SHADOW; 1278 } else { 1279 ones_mask = cr4_ones_mask; 1280 zeros_mask = cr4_zeros_mask; 1281 vmcs_guest_cr = VMCS_GUEST_CR4; 1282 vmcs_shadow_cr = VMCS_CR4_SHADOW; 1283 } 1284 vmcs_write(vmcs_shadow_cr, regval); 1285 1286 crval = regval | ones_mask; 1287 crval &= ~zeros_mask; 1288 vmcs_write(vmcs_guest_cr, crval); 1289 1290 if (cr == 0 && regval & CR0_PG) { 1291 uint64_t efer, entry_ctls; 1292 1293 /* 1294 * If CR0.PG is 1 and EFER.LME is 1 then EFER.LMA and 1295 * the "IA-32e mode guest" bit in VM-entry control must be 1296 * equal. 1297 */ 1298 efer = vmcs_read(VMCS_GUEST_IA32_EFER); 1299 if (efer & EFER_LME) { 1300 efer |= EFER_LMA; 1301 vmcs_write(VMCS_GUEST_IA32_EFER, efer); 1302 entry_ctls = vmcs_read(VMCS_ENTRY_CTLS); 1303 entry_ctls |= VM_ENTRY_GUEST_LMA; 1304 vmcs_write(VMCS_ENTRY_CTLS, entry_ctls); 1305 } 1306 } 1307 1308 return (HANDLED); 1309 } 1310 1311 static int 1312 ept_fault_type(uint64_t ept_qual) 1313 { 1314 int fault_type; 1315 1316 if (ept_qual & EPT_VIOLATION_DATA_WRITE) 1317 fault_type = VM_PROT_WRITE; 1318 else if (ept_qual & EPT_VIOLATION_INST_FETCH) 1319 fault_type = VM_PROT_EXECUTE; 1320 else 1321 fault_type= VM_PROT_READ; 1322 1323 return (fault_type); 1324 } 1325 1326 static boolean_t 1327 ept_emulation_fault(uint64_t ept_qual) 1328 { 1329 int read, write; 1330 1331 /* EPT fault on an instruction fetch doesn't make sense here */ 1332 if (ept_qual & EPT_VIOLATION_INST_FETCH) 1333 return (FALSE); 1334 1335 /* EPT fault must be a read fault or a write fault */ 1336 read = ept_qual & EPT_VIOLATION_DATA_READ ? 1 : 0; 1337 write = ept_qual & EPT_VIOLATION_DATA_WRITE ? 1 : 0; 1338 if ((read | write) == 0) 1339 return (FALSE); 1340 1341 /* 1342 * The EPT violation must have been caused by accessing a 1343 * guest-physical address that is a translation of a guest-linear 1344 * address. 1345 */ 1346 if ((ept_qual & EPT_VIOLATION_GLA_VALID) == 0 || 1347 (ept_qual & EPT_VIOLATION_XLAT_VALID) == 0) { 1348 return (FALSE); 1349 } 1350 1351 return (TRUE); 1352 } 1353 1354 static int 1355 vmx_handle_apic_write(struct vlapic *vlapic, uint64_t qual) 1356 { 1357 int error, handled, offset; 1358 bool retu; 1359 1360 if (!virtual_interrupt_delivery) 1361 return (UNHANDLED); 1362 1363 handled = 1; 1364 offset = APIC_WRITE_OFFSET(qual); 1365 switch (offset) { 1366 case APIC_OFFSET_ID: 1367 vlapic_id_write_handler(vlapic); 1368 break; 1369 case APIC_OFFSET_LDR: 1370 vlapic_ldr_write_handler(vlapic); 1371 break; 1372 case APIC_OFFSET_DFR: 1373 vlapic_dfr_write_handler(vlapic); 1374 break; 1375 case APIC_OFFSET_SVR: 1376 vlapic_svr_write_handler(vlapic); 1377 break; 1378 case APIC_OFFSET_ESR: 1379 vlapic_esr_write_handler(vlapic); 1380 break; 1381 case APIC_OFFSET_ICR_LOW: 1382 retu = false; 1383 error = vlapic_icrlo_write_handler(vlapic, &retu); 1384 if (error != 0 || retu) 1385 handled = 0; 1386 break; 1387 case APIC_OFFSET_CMCI_LVT: 1388 case APIC_OFFSET_TIMER_LVT ... APIC_OFFSET_ERROR_LVT: 1389 vlapic_lvt_write_handler(vlapic, offset); 1390 break; 1391 case APIC_OFFSET_TIMER_ICR: 1392 vlapic_icrtmr_write_handler(vlapic); 1393 break; 1394 case APIC_OFFSET_TIMER_DCR: 1395 vlapic_dcr_write_handler(vlapic); 1396 break; 1397 default: 1398 handled = 0; 1399 break; 1400 } 1401 return (handled); 1402 } 1403 1404 static bool 1405 apic_access_fault(uint64_t gpa) 1406 { 1407 1408 if (virtual_interrupt_delivery && 1409 (gpa >= DEFAULT_APIC_BASE && gpa < DEFAULT_APIC_BASE + PAGE_SIZE)) 1410 return (true); 1411 else 1412 return (false); 1413 } 1414 1415 static int 1416 vmx_handle_apic_access(struct vmx *vmx, int vcpuid, struct vm_exit *vmexit) 1417 { 1418 uint64_t qual; 1419 int access_type, offset, allowed; 1420 1421 if (!virtual_interrupt_delivery) 1422 return (UNHANDLED); 1423 1424 qual = vmexit->u.vmx.exit_qualification; 1425 access_type = APIC_ACCESS_TYPE(qual); 1426 offset = APIC_ACCESS_OFFSET(qual); 1427 1428 allowed = 0; 1429 if (access_type == 0) { 1430 /* 1431 * Read data access to the following registers is expected. 1432 */ 1433 switch (offset) { 1434 case APIC_OFFSET_APR: 1435 case APIC_OFFSET_PPR: 1436 case APIC_OFFSET_RRR: 1437 case APIC_OFFSET_CMCI_LVT: 1438 case APIC_OFFSET_TIMER_CCR: 1439 allowed = 1; 1440 break; 1441 default: 1442 break; 1443 } 1444 } else if (access_type == 1) { 1445 /* 1446 * Write data access to the following registers is expected. 1447 */ 1448 switch (offset) { 1449 case APIC_OFFSET_VER: 1450 case APIC_OFFSET_APR: 1451 case APIC_OFFSET_PPR: 1452 case APIC_OFFSET_RRR: 1453 case APIC_OFFSET_ISR0 ... APIC_OFFSET_ISR7: 1454 case APIC_OFFSET_TMR0 ... APIC_OFFSET_TMR7: 1455 case APIC_OFFSET_IRR0 ... APIC_OFFSET_IRR7: 1456 case APIC_OFFSET_CMCI_LVT: 1457 case APIC_OFFSET_TIMER_CCR: 1458 allowed = 1; 1459 break; 1460 default: 1461 break; 1462 } 1463 } 1464 1465 if (allowed) { 1466 vmexit->exitcode = VM_EXITCODE_INST_EMUL; 1467 vmexit->u.inst_emul.gpa = DEFAULT_APIC_BASE + offset; 1468 vmexit->u.inst_emul.gla = VIE_INVALID_GLA; 1469 vmexit->u.inst_emul.cr3 = vmcs_guest_cr3(); 1470 } 1471 1472 /* 1473 * Regardless of whether the APIC-access is allowed this handler 1474 * always returns UNHANDLED: 1475 * - if the access is allowed then it is handled by emulating the 1476 * instruction that caused the VM-exit (outside the critical section) 1477 * - if the access is not allowed then it will be converted to an 1478 * exitcode of VM_EXITCODE_VMX and will be dealt with in userland. 1479 */ 1480 return (UNHANDLED); 1481 } 1482 1483 static int 1484 vmx_exit_process(struct vmx *vmx, int vcpu, struct vm_exit *vmexit) 1485 { 1486 int error, handled; 1487 struct vmxctx *vmxctx; 1488 struct vlapic *vlapic; 1489 uint32_t eax, ecx, edx, idtvec_info, idtvec_err, intr_info, reason; 1490 uint64_t qual, gpa; 1491 bool retu; 1492 1493 CTASSERT((PINBASED_CTLS_ONE_SETTING & PINBASED_VIRTUAL_NMI) != 0); 1494 1495 handled = 0; 1496 vmxctx = &vmx->ctx[vcpu]; 1497 1498 qual = vmexit->u.vmx.exit_qualification; 1499 reason = vmexit->u.vmx.exit_reason; 1500 vmexit->exitcode = VM_EXITCODE_BOGUS; 1501 1502 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_COUNT, 1); 1503 1504 /* 1505 * VM exits that could be triggered during event injection on the 1506 * previous VM entry need to be handled specially by re-injecting 1507 * the event. 1508 * 1509 * See "Information for VM Exits During Event Delivery" in Intel SDM 1510 * for details. 1511 */ 1512 switch (reason) { 1513 case EXIT_REASON_EPT_FAULT: 1514 case EXIT_REASON_EPT_MISCONFIG: 1515 case EXIT_REASON_APIC_ACCESS: 1516 case EXIT_REASON_TASK_SWITCH: 1517 case EXIT_REASON_EXCEPTION: 1518 idtvec_info = vmcs_idt_vectoring_info(); 1519 if (idtvec_info & VMCS_IDT_VEC_VALID) { 1520 idtvec_info &= ~(1 << 12); /* clear undefined bit */ 1521 vmcs_write(VMCS_ENTRY_INTR_INFO, idtvec_info); 1522 if (idtvec_info & VMCS_IDT_VEC_ERRCODE_VALID) { 1523 idtvec_err = vmcs_idt_vectoring_err(); 1524 vmcs_write(VMCS_ENTRY_EXCEPTION_ERROR, 1525 idtvec_err); 1526 } 1527 /* 1528 * If 'virtual NMIs' are being used and the VM-exit 1529 * happened while injecting an NMI during the previous 1530 * VM-entry, then clear "blocking by NMI" in the Guest 1531 * Interruptibility-state. 1532 */ 1533 if ((idtvec_info & VMCS_INTR_T_MASK) == 1534 VMCS_INTR_T_NMI) { 1535 vmx_clear_nmi_blocking(vmx, vcpu); 1536 } 1537 vmcs_write(VMCS_ENTRY_INST_LENGTH, vmexit->inst_length); 1538 } 1539 default: 1540 idtvec_info = 0; 1541 break; 1542 } 1543 1544 switch (reason) { 1545 case EXIT_REASON_CR_ACCESS: 1546 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CR_ACCESS, 1); 1547 handled = vmx_emulate_cr_access(vmx, vcpu, qual); 1548 break; 1549 case EXIT_REASON_RDMSR: 1550 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_RDMSR, 1); 1551 retu = false; 1552 ecx = vmxctx->guest_rcx; 1553 error = emulate_rdmsr(vmx->vm, vcpu, ecx, &retu); 1554 if (error) { 1555 vmexit->exitcode = VM_EXITCODE_RDMSR; 1556 vmexit->u.msr.code = ecx; 1557 } else if (!retu) { 1558 handled = 1; 1559 } else { 1560 /* Return to userspace with a valid exitcode */ 1561 KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS, 1562 ("emulate_wrmsr retu with bogus exitcode")); 1563 } 1564 break; 1565 case EXIT_REASON_WRMSR: 1566 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_WRMSR, 1); 1567 retu = false; 1568 eax = vmxctx->guest_rax; 1569 ecx = vmxctx->guest_rcx; 1570 edx = vmxctx->guest_rdx; 1571 error = emulate_wrmsr(vmx->vm, vcpu, ecx, 1572 (uint64_t)edx << 32 | eax, &retu); 1573 if (error) { 1574 vmexit->exitcode = VM_EXITCODE_WRMSR; 1575 vmexit->u.msr.code = ecx; 1576 vmexit->u.msr.wval = (uint64_t)edx << 32 | eax; 1577 } else if (!retu) { 1578 handled = 1; 1579 } else { 1580 /* Return to userspace with a valid exitcode */ 1581 KASSERT(vmexit->exitcode != VM_EXITCODE_BOGUS, 1582 ("emulate_wrmsr retu with bogus exitcode")); 1583 } 1584 break; 1585 case EXIT_REASON_HLT: 1586 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_HLT, 1); 1587 vmexit->exitcode = VM_EXITCODE_HLT; 1588 vmexit->u.hlt.rflags = vmcs_read(VMCS_GUEST_RFLAGS); 1589 break; 1590 case EXIT_REASON_MTF: 1591 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_MTRAP, 1); 1592 vmexit->exitcode = VM_EXITCODE_MTRAP; 1593 break; 1594 case EXIT_REASON_PAUSE: 1595 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_PAUSE, 1); 1596 vmexit->exitcode = VM_EXITCODE_PAUSE; 1597 break; 1598 case EXIT_REASON_INTR_WINDOW: 1599 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INTR_WINDOW, 1); 1600 vmx_clear_int_window_exiting(vmx, vcpu); 1601 return (1); 1602 case EXIT_REASON_EXT_INTR: 1603 /* 1604 * External interrupts serve only to cause VM exits and allow 1605 * the host interrupt handler to run. 1606 * 1607 * If this external interrupt triggers a virtual interrupt 1608 * to a VM, then that state will be recorded by the 1609 * host interrupt handler in the VM's softc. We will inject 1610 * this virtual interrupt during the subsequent VM enter. 1611 */ 1612 intr_info = vmcs_read(VMCS_EXIT_INTR_INFO); 1613 KASSERT((intr_info & VMCS_INTR_VALID) != 0 && 1614 (intr_info & VMCS_INTR_T_MASK) == VMCS_INTR_T_HWINTR, 1615 ("VM exit interruption info invalid: %#x", intr_info)); 1616 vmx_trigger_hostintr(intr_info & 0xff); 1617 1618 /* 1619 * This is special. We want to treat this as an 'handled' 1620 * VM-exit but not increment the instruction pointer. 1621 */ 1622 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EXTINT, 1); 1623 return (1); 1624 case EXIT_REASON_NMI_WINDOW: 1625 /* Exit to allow the pending virtual NMI to be injected */ 1626 if (vm_nmi_pending(vmx->vm, vcpu)) 1627 vmx_inject_nmi(vmx, vcpu); 1628 vmx_clear_nmi_window_exiting(vmx, vcpu); 1629 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_NMI_WINDOW, 1); 1630 return (1); 1631 case EXIT_REASON_INOUT: 1632 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_INOUT, 1); 1633 vmexit->exitcode = VM_EXITCODE_INOUT; 1634 vmexit->u.inout.bytes = (qual & 0x7) + 1; 1635 vmexit->u.inout.in = (qual & 0x8) ? 1 : 0; 1636 vmexit->u.inout.string = (qual & 0x10) ? 1 : 0; 1637 vmexit->u.inout.rep = (qual & 0x20) ? 1 : 0; 1638 vmexit->u.inout.port = (uint16_t)(qual >> 16); 1639 vmexit->u.inout.eax = (uint32_t)(vmxctx->guest_rax); 1640 break; 1641 case EXIT_REASON_CPUID: 1642 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_CPUID, 1); 1643 handled = vmx_handle_cpuid(vmx->vm, vcpu, vmxctx); 1644 break; 1645 case EXIT_REASON_EXCEPTION: 1646 intr_info = vmcs_read(VMCS_EXIT_INTR_INFO); 1647 KASSERT((intr_info & VMCS_INTR_VALID) != 0, 1648 ("VM exit interruption info invalid: %#x", intr_info)); 1649 /* 1650 * If Virtual NMIs control is 1 and the VM-exit is due to a 1651 * fault encountered during the execution of IRET then we must 1652 * restore the state of "virtual-NMI blocking" before resuming 1653 * the guest. 1654 * 1655 * See "Resuming Guest Software after Handling an Exception". 1656 */ 1657 if ((idtvec_info & VMCS_IDT_VEC_VALID) == 0 && 1658 (intr_info & 0xff) != IDT_DF && 1659 (intr_info & EXIT_QUAL_NMIUDTI) != 0) 1660 vmx_restore_nmi_blocking(vmx, vcpu); 1661 break; 1662 case EXIT_REASON_EPT_FAULT: 1663 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_EPT_FAULT, 1); 1664 /* 1665 * If 'gpa' lies within the address space allocated to 1666 * memory then this must be a nested page fault otherwise 1667 * this must be an instruction that accesses MMIO space. 1668 */ 1669 gpa = vmcs_gpa(); 1670 if (vm_mem_allocated(vmx->vm, gpa) || apic_access_fault(gpa)) { 1671 vmexit->exitcode = VM_EXITCODE_PAGING; 1672 vmexit->u.paging.gpa = gpa; 1673 vmexit->u.paging.fault_type = ept_fault_type(qual); 1674 } else if (ept_emulation_fault(qual)) { 1675 vmexit->exitcode = VM_EXITCODE_INST_EMUL; 1676 vmexit->u.inst_emul.gpa = gpa; 1677 vmexit->u.inst_emul.gla = vmcs_gla(); 1678 vmexit->u.inst_emul.cr3 = vmcs_guest_cr3(); 1679 } 1680 /* 1681 * If Virtual NMIs control is 1 and the VM-exit is due to an 1682 * EPT fault during the execution of IRET then we must restore 1683 * the state of "virtual-NMI blocking" before resuming. 1684 * 1685 * See description of "NMI unblocking due to IRET" in 1686 * "Exit Qualification for EPT Violations". 1687 */ 1688 if ((idtvec_info & VMCS_IDT_VEC_VALID) == 0 && 1689 (qual & EXIT_QUAL_NMIUDTI) != 0) 1690 vmx_restore_nmi_blocking(vmx, vcpu); 1691 break; 1692 case EXIT_REASON_APIC_ACCESS: 1693 handled = vmx_handle_apic_access(vmx, vcpu, vmexit); 1694 break; 1695 case EXIT_REASON_APIC_WRITE: 1696 /* 1697 * APIC-write VM exit is trap-like so the %rip is already 1698 * pointing to the next instruction. 1699 */ 1700 vmexit->inst_length = 0; 1701 vlapic = vm_lapic(vmx->vm, vcpu); 1702 handled = vmx_handle_apic_write(vlapic, qual); 1703 break; 1704 default: 1705 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_UNKNOWN, 1); 1706 break; 1707 } 1708 1709 if (handled) { 1710 /* 1711 * It is possible that control is returned to userland 1712 * even though we were able to handle the VM exit in the 1713 * kernel. 1714 * 1715 * In such a case we want to make sure that the userland 1716 * restarts guest execution at the instruction *after* 1717 * the one we just processed. Therefore we update the 1718 * guest rip in the VMCS and in 'vmexit'. 1719 */ 1720 vmexit->rip += vmexit->inst_length; 1721 vmexit->inst_length = 0; 1722 vmcs_write(VMCS_GUEST_RIP, vmexit->rip); 1723 } else { 1724 if (vmexit->exitcode == VM_EXITCODE_BOGUS) { 1725 /* 1726 * If this VM exit was not claimed by anybody then 1727 * treat it as a generic VMX exit. 1728 */ 1729 vmexit->exitcode = VM_EXITCODE_VMX; 1730 vmexit->u.vmx.status = VM_SUCCESS; 1731 } else { 1732 /* 1733 * The exitcode and collateral have been populated. 1734 * The VM exit will be processed further in userland. 1735 */ 1736 } 1737 } 1738 return (handled); 1739 } 1740 1741 static __inline int 1742 vmx_exit_astpending(struct vmx *vmx, int vcpu, struct vm_exit *vmexit) 1743 { 1744 1745 vmexit->rip = vmcs_guest_rip(); 1746 vmexit->inst_length = 0; 1747 vmexit->exitcode = VM_EXITCODE_BOGUS; 1748 vmx_astpending_trace(vmx, vcpu, vmexit->rip); 1749 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_ASTPENDING, 1); 1750 1751 return (HANDLED); 1752 } 1753 1754 static __inline int 1755 vmx_exit_rendezvous(struct vmx *vmx, int vcpu, struct vm_exit *vmexit) 1756 { 1757 1758 vmexit->rip = vmcs_guest_rip(); 1759 vmexit->inst_length = 0; 1760 vmexit->exitcode = VM_EXITCODE_RENDEZVOUS; 1761 vmm_stat_incr(vmx->vm, vcpu, VMEXIT_RENDEZVOUS, 1); 1762 1763 return (UNHANDLED); 1764 } 1765 1766 static __inline int 1767 vmx_exit_inst_error(struct vmxctx *vmxctx, int rc, struct vm_exit *vmexit) 1768 { 1769 1770 KASSERT(vmxctx->inst_fail_status != VM_SUCCESS, 1771 ("vmx_exit_inst_error: invalid inst_fail_status %d", 1772 vmxctx->inst_fail_status)); 1773 1774 vmexit->inst_length = 0; 1775 vmexit->exitcode = VM_EXITCODE_VMX; 1776 vmexit->u.vmx.status = vmxctx->inst_fail_status; 1777 vmexit->u.vmx.inst_error = vmcs_instruction_error(); 1778 vmexit->u.vmx.exit_reason = ~0; 1779 vmexit->u.vmx.exit_qualification = ~0; 1780 1781 switch (rc) { 1782 case VMX_VMRESUME_ERROR: 1783 case VMX_VMLAUNCH_ERROR: 1784 case VMX_INVEPT_ERROR: 1785 vmexit->u.vmx.inst_type = rc; 1786 break; 1787 default: 1788 panic("vm_exit_inst_error: vmx_enter_guest returned %d", rc); 1789 } 1790 1791 return (UNHANDLED); 1792 } 1793 1794 static int 1795 vmx_run(void *arg, int vcpu, register_t startrip, pmap_t pmap, 1796 void *rendezvous_cookie) 1797 { 1798 int rc, handled, launched; 1799 struct vmx *vmx; 1800 struct vm *vm; 1801 struct vmxctx *vmxctx; 1802 struct vmcs *vmcs; 1803 struct vm_exit *vmexit; 1804 struct vlapic *vlapic; 1805 uint64_t rip; 1806 uint32_t exit_reason; 1807 1808 vmx = arg; 1809 vm = vmx->vm; 1810 vmcs = &vmx->vmcs[vcpu]; 1811 vmxctx = &vmx->ctx[vcpu]; 1812 vlapic = vm_lapic(vm, vcpu); 1813 vmexit = vm_exitinfo(vm, vcpu); 1814 launched = 0; 1815 1816 KASSERT(vmxctx->pmap == pmap, 1817 ("pmap %p different than ctx pmap %p", pmap, vmxctx->pmap)); 1818 KASSERT(vmxctx->eptp == vmx->eptp, 1819 ("eptp %p different than ctx eptp %#lx", eptp, vmxctx->eptp)); 1820 1821 VMPTRLD(vmcs); 1822 1823 /* 1824 * XXX 1825 * We do this every time because we may setup the virtual machine 1826 * from a different process than the one that actually runs it. 1827 * 1828 * If the life of a virtual machine was spent entirely in the context 1829 * of a single process we could do this once in vmx_vminit(). 1830 */ 1831 vmcs_write(VMCS_HOST_CR3, rcr3()); 1832 1833 vmcs_write(VMCS_GUEST_RIP, startrip); 1834 vmx_set_pcpu_defaults(vmx, vcpu); 1835 do { 1836 /* 1837 * Interrupts are disabled from this point on until the 1838 * guest starts executing. This is done for the following 1839 * reasons: 1840 * 1841 * If an AST is asserted on this thread after the check below, 1842 * then the IPI_AST notification will not be lost, because it 1843 * will cause a VM exit due to external interrupt as soon as 1844 * the guest state is loaded. 1845 * 1846 * A posted interrupt after 'vmx_inject_interrupts()' will 1847 * not be "lost" because it will be held pending in the host 1848 * APIC because interrupts are disabled. The pending interrupt 1849 * will be recognized as soon as the guest state is loaded. 1850 * 1851 * The same reasoning applies to the IPI generated by 1852 * pmap_invalidate_ept(). 1853 */ 1854 disable_intr(); 1855 if (curthread->td_flags & (TDF_ASTPENDING | TDF_NEEDRESCHED)) { 1856 enable_intr(); 1857 handled = vmx_exit_astpending(vmx, vcpu, vmexit); 1858 break; 1859 } 1860 1861 if (vcpu_rendezvous_pending(rendezvous_cookie)) { 1862 enable_intr(); 1863 handled = vmx_exit_rendezvous(vmx, vcpu, vmexit); 1864 break; 1865 } 1866 1867 vmx_inject_interrupts(vmx, vcpu, vlapic); 1868 vmx_run_trace(vmx, vcpu); 1869 rc = vmx_enter_guest(vmxctx, launched); 1870 1871 enable_intr(); 1872 1873 /* Collect some information for VM exit processing */ 1874 vmexit->rip = rip = vmcs_guest_rip(); 1875 vmexit->inst_length = vmexit_instruction_length(); 1876 vmexit->u.vmx.exit_reason = exit_reason = vmcs_exit_reason(); 1877 vmexit->u.vmx.exit_qualification = vmcs_exit_qualification(); 1878 1879 if (rc == VMX_GUEST_VMEXIT) { 1880 launched = 1; 1881 handled = vmx_exit_process(vmx, vcpu, vmexit); 1882 } else { 1883 handled = vmx_exit_inst_error(vmxctx, rc, vmexit); 1884 } 1885 1886 vmx_exit_trace(vmx, vcpu, rip, exit_reason, handled); 1887 } while (handled); 1888 1889 /* 1890 * If a VM exit has been handled then the exitcode must be BOGUS 1891 * If a VM exit is not handled then the exitcode must not be BOGUS 1892 */ 1893 if ((handled && vmexit->exitcode != VM_EXITCODE_BOGUS) || 1894 (!handled && vmexit->exitcode == VM_EXITCODE_BOGUS)) { 1895 panic("Mismatch between handled (%d) and exitcode (%d)", 1896 handled, vmexit->exitcode); 1897 } 1898 1899 if (!handled) 1900 vmm_stat_incr(vm, vcpu, VMEXIT_USERSPACE, 1); 1901 1902 VCPU_CTR1(vm, vcpu, "returning from vmx_run: exitcode %d", 1903 vmexit->exitcode); 1904 1905 VMCLEAR(vmcs); 1906 return (0); 1907 } 1908 1909 static void 1910 vmx_vmcleanup(void *arg) 1911 { 1912 int i, error; 1913 struct vmx *vmx = arg; 1914 1915 if (virtual_interrupt_delivery) 1916 vm_unmap_mmio(vmx->vm, DEFAULT_APIC_BASE, PAGE_SIZE); 1917 1918 for (i = 0; i < VM_MAXCPU; i++) 1919 vpid_free(vmx->state[i].vpid); 1920 1921 /* 1922 * XXXSMP we also need to clear the VMCS active on the other vcpus. 1923 */ 1924 error = vmclear(&vmx->vmcs[0]); 1925 if (error != 0) 1926 panic("vmx_vmcleanup: vmclear error %d on vcpu 0", error); 1927 1928 free(vmx, M_VMX); 1929 1930 return; 1931 } 1932 1933 static register_t * 1934 vmxctx_regptr(struct vmxctx *vmxctx, int reg) 1935 { 1936 1937 switch (reg) { 1938 case VM_REG_GUEST_RAX: 1939 return (&vmxctx->guest_rax); 1940 case VM_REG_GUEST_RBX: 1941 return (&vmxctx->guest_rbx); 1942 case VM_REG_GUEST_RCX: 1943 return (&vmxctx->guest_rcx); 1944 case VM_REG_GUEST_RDX: 1945 return (&vmxctx->guest_rdx); 1946 case VM_REG_GUEST_RSI: 1947 return (&vmxctx->guest_rsi); 1948 case VM_REG_GUEST_RDI: 1949 return (&vmxctx->guest_rdi); 1950 case VM_REG_GUEST_RBP: 1951 return (&vmxctx->guest_rbp); 1952 case VM_REG_GUEST_R8: 1953 return (&vmxctx->guest_r8); 1954 case VM_REG_GUEST_R9: 1955 return (&vmxctx->guest_r9); 1956 case VM_REG_GUEST_R10: 1957 return (&vmxctx->guest_r10); 1958 case VM_REG_GUEST_R11: 1959 return (&vmxctx->guest_r11); 1960 case VM_REG_GUEST_R12: 1961 return (&vmxctx->guest_r12); 1962 case VM_REG_GUEST_R13: 1963 return (&vmxctx->guest_r13); 1964 case VM_REG_GUEST_R14: 1965 return (&vmxctx->guest_r14); 1966 case VM_REG_GUEST_R15: 1967 return (&vmxctx->guest_r15); 1968 default: 1969 break; 1970 } 1971 return (NULL); 1972 } 1973 1974 static int 1975 vmxctx_getreg(struct vmxctx *vmxctx, int reg, uint64_t *retval) 1976 { 1977 register_t *regp; 1978 1979 if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) { 1980 *retval = *regp; 1981 return (0); 1982 } else 1983 return (EINVAL); 1984 } 1985 1986 static int 1987 vmxctx_setreg(struct vmxctx *vmxctx, int reg, uint64_t val) 1988 { 1989 register_t *regp; 1990 1991 if ((regp = vmxctx_regptr(vmxctx, reg)) != NULL) { 1992 *regp = val; 1993 return (0); 1994 } else 1995 return (EINVAL); 1996 } 1997 1998 static int 1999 vmx_shadow_reg(int reg) 2000 { 2001 int shreg; 2002 2003 shreg = -1; 2004 2005 switch (reg) { 2006 case VM_REG_GUEST_CR0: 2007 shreg = VMCS_CR0_SHADOW; 2008 break; 2009 case VM_REG_GUEST_CR4: 2010 shreg = VMCS_CR4_SHADOW; 2011 break; 2012 default: 2013 break; 2014 } 2015 2016 return (shreg); 2017 } 2018 2019 static int 2020 vmx_getreg(void *arg, int vcpu, int reg, uint64_t *retval) 2021 { 2022 int running, hostcpu; 2023 struct vmx *vmx = arg; 2024 2025 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu); 2026 if (running && hostcpu != curcpu) 2027 panic("vmx_getreg: %s%d is running", vm_name(vmx->vm), vcpu); 2028 2029 if (vmxctx_getreg(&vmx->ctx[vcpu], reg, retval) == 0) 2030 return (0); 2031 2032 return (vmcs_getreg(&vmx->vmcs[vcpu], running, reg, retval)); 2033 } 2034 2035 static int 2036 vmx_setreg(void *arg, int vcpu, int reg, uint64_t val) 2037 { 2038 int error, hostcpu, running, shadow; 2039 uint64_t ctls; 2040 struct vmx *vmx = arg; 2041 2042 running = vcpu_is_running(vmx->vm, vcpu, &hostcpu); 2043 if (running && hostcpu != curcpu) 2044 panic("vmx_setreg: %s%d is running", vm_name(vmx->vm), vcpu); 2045 2046 if (vmxctx_setreg(&vmx->ctx[vcpu], reg, val) == 0) 2047 return (0); 2048 2049 error = vmcs_setreg(&vmx->vmcs[vcpu], running, reg, val); 2050 2051 if (error == 0) { 2052 /* 2053 * If the "load EFER" VM-entry control is 1 then the 2054 * value of EFER.LMA must be identical to "IA-32e mode guest" 2055 * bit in the VM-entry control. 2056 */ 2057 if ((entry_ctls & VM_ENTRY_LOAD_EFER) != 0 && 2058 (reg == VM_REG_GUEST_EFER)) { 2059 vmcs_getreg(&vmx->vmcs[vcpu], running, 2060 VMCS_IDENT(VMCS_ENTRY_CTLS), &ctls); 2061 if (val & EFER_LMA) 2062 ctls |= VM_ENTRY_GUEST_LMA; 2063 else 2064 ctls &= ~VM_ENTRY_GUEST_LMA; 2065 vmcs_setreg(&vmx->vmcs[vcpu], running, 2066 VMCS_IDENT(VMCS_ENTRY_CTLS), ctls); 2067 } 2068 2069 shadow = vmx_shadow_reg(reg); 2070 if (shadow > 0) { 2071 /* 2072 * Store the unmodified value in the shadow 2073 */ 2074 error = vmcs_setreg(&vmx->vmcs[vcpu], running, 2075 VMCS_IDENT(shadow), val); 2076 } 2077 } 2078 2079 return (error); 2080 } 2081 2082 static int 2083 vmx_getdesc(void *arg, int vcpu, int reg, struct seg_desc *desc) 2084 { 2085 struct vmx *vmx = arg; 2086 2087 return (vmcs_getdesc(&vmx->vmcs[vcpu], reg, desc)); 2088 } 2089 2090 static int 2091 vmx_setdesc(void *arg, int vcpu, int reg, struct seg_desc *desc) 2092 { 2093 struct vmx *vmx = arg; 2094 2095 return (vmcs_setdesc(&vmx->vmcs[vcpu], reg, desc)); 2096 } 2097 2098 static int 2099 vmx_inject(void *arg, int vcpu, int type, int vector, uint32_t code, 2100 int code_valid) 2101 { 2102 int error; 2103 uint64_t info; 2104 struct vmx *vmx = arg; 2105 struct vmcs *vmcs = &vmx->vmcs[vcpu]; 2106 2107 static uint32_t type_map[VM_EVENT_MAX] = { 2108 0x1, /* VM_EVENT_NONE */ 2109 0x0, /* VM_HW_INTR */ 2110 0x2, /* VM_NMI */ 2111 0x3, /* VM_HW_EXCEPTION */ 2112 0x4, /* VM_SW_INTR */ 2113 0x5, /* VM_PRIV_SW_EXCEPTION */ 2114 0x6, /* VM_SW_EXCEPTION */ 2115 }; 2116 2117 /* 2118 * If there is already an exception pending to be delivered to the 2119 * vcpu then just return. 2120 */ 2121 error = vmcs_getreg(vmcs, 0, VMCS_IDENT(VMCS_ENTRY_INTR_INFO), &info); 2122 if (error) 2123 return (error); 2124 2125 if (info & VMCS_INTR_VALID) 2126 return (EAGAIN); 2127 2128 info = vector | (type_map[type] << 8) | (code_valid ? 1 << 11 : 0); 2129 info |= VMCS_INTR_VALID; 2130 error = vmcs_setreg(vmcs, 0, VMCS_IDENT(VMCS_ENTRY_INTR_INFO), info); 2131 if (error != 0) 2132 return (error); 2133 2134 if (code_valid) { 2135 error = vmcs_setreg(vmcs, 0, 2136 VMCS_IDENT(VMCS_ENTRY_EXCEPTION_ERROR), 2137 code); 2138 } 2139 return (error); 2140 } 2141 2142 static int 2143 vmx_getcap(void *arg, int vcpu, int type, int *retval) 2144 { 2145 struct vmx *vmx = arg; 2146 int vcap; 2147 int ret; 2148 2149 ret = ENOENT; 2150 2151 vcap = vmx->cap[vcpu].set; 2152 2153 switch (type) { 2154 case VM_CAP_HALT_EXIT: 2155 if (cap_halt_exit) 2156 ret = 0; 2157 break; 2158 case VM_CAP_PAUSE_EXIT: 2159 if (cap_pause_exit) 2160 ret = 0; 2161 break; 2162 case VM_CAP_MTRAP_EXIT: 2163 if (cap_monitor_trap) 2164 ret = 0; 2165 break; 2166 case VM_CAP_UNRESTRICTED_GUEST: 2167 if (cap_unrestricted_guest) 2168 ret = 0; 2169 break; 2170 case VM_CAP_ENABLE_INVPCID: 2171 if (cap_invpcid) 2172 ret = 0; 2173 break; 2174 default: 2175 break; 2176 } 2177 2178 if (ret == 0) 2179 *retval = (vcap & (1 << type)) ? 1 : 0; 2180 2181 return (ret); 2182 } 2183 2184 static int 2185 vmx_setcap(void *arg, int vcpu, int type, int val) 2186 { 2187 struct vmx *vmx = arg; 2188 struct vmcs *vmcs = &vmx->vmcs[vcpu]; 2189 uint32_t baseval; 2190 uint32_t *pptr; 2191 int error; 2192 int flag; 2193 int reg; 2194 int retval; 2195 2196 retval = ENOENT; 2197 pptr = NULL; 2198 2199 switch (type) { 2200 case VM_CAP_HALT_EXIT: 2201 if (cap_halt_exit) { 2202 retval = 0; 2203 pptr = &vmx->cap[vcpu].proc_ctls; 2204 baseval = *pptr; 2205 flag = PROCBASED_HLT_EXITING; 2206 reg = VMCS_PRI_PROC_BASED_CTLS; 2207 } 2208 break; 2209 case VM_CAP_MTRAP_EXIT: 2210 if (cap_monitor_trap) { 2211 retval = 0; 2212 pptr = &vmx->cap[vcpu].proc_ctls; 2213 baseval = *pptr; 2214 flag = PROCBASED_MTF; 2215 reg = VMCS_PRI_PROC_BASED_CTLS; 2216 } 2217 break; 2218 case VM_CAP_PAUSE_EXIT: 2219 if (cap_pause_exit) { 2220 retval = 0; 2221 pptr = &vmx->cap[vcpu].proc_ctls; 2222 baseval = *pptr; 2223 flag = PROCBASED_PAUSE_EXITING; 2224 reg = VMCS_PRI_PROC_BASED_CTLS; 2225 } 2226 break; 2227 case VM_CAP_UNRESTRICTED_GUEST: 2228 if (cap_unrestricted_guest) { 2229 retval = 0; 2230 pptr = &vmx->cap[vcpu].proc_ctls2; 2231 baseval = *pptr; 2232 flag = PROCBASED2_UNRESTRICTED_GUEST; 2233 reg = VMCS_SEC_PROC_BASED_CTLS; 2234 } 2235 break; 2236 case VM_CAP_ENABLE_INVPCID: 2237 if (cap_invpcid) { 2238 retval = 0; 2239 pptr = &vmx->cap[vcpu].proc_ctls2; 2240 baseval = *pptr; 2241 flag = PROCBASED2_ENABLE_INVPCID; 2242 reg = VMCS_SEC_PROC_BASED_CTLS; 2243 } 2244 break; 2245 default: 2246 break; 2247 } 2248 2249 if (retval == 0) { 2250 if (val) { 2251 baseval |= flag; 2252 } else { 2253 baseval &= ~flag; 2254 } 2255 VMPTRLD(vmcs); 2256 error = vmwrite(reg, baseval); 2257 VMCLEAR(vmcs); 2258 2259 if (error) { 2260 retval = error; 2261 } else { 2262 /* 2263 * Update optional stored flags, and record 2264 * setting 2265 */ 2266 if (pptr != NULL) { 2267 *pptr = baseval; 2268 } 2269 2270 if (val) { 2271 vmx->cap[vcpu].set |= (1 << type); 2272 } else { 2273 vmx->cap[vcpu].set &= ~(1 << type); 2274 } 2275 } 2276 } 2277 2278 return (retval); 2279 } 2280 2281 struct vlapic_vtx { 2282 struct vlapic vlapic; 2283 struct pir_desc *pir_desc; 2284 }; 2285 2286 #define VMX_CTR_PIR(vm, vcpuid, pir_desc, notify, vector, level, msg) \ 2287 do { \ 2288 VCPU_CTR2(vm, vcpuid, msg " assert %s-triggered vector %d", \ 2289 level ? "level" : "edge", vector); \ 2290 VCPU_CTR1(vm, vcpuid, msg " pir0 0x%016lx", pir_desc->pir[0]); \ 2291 VCPU_CTR1(vm, vcpuid, msg " pir1 0x%016lx", pir_desc->pir[1]); \ 2292 VCPU_CTR1(vm, vcpuid, msg " pir2 0x%016lx", pir_desc->pir[2]); \ 2293 VCPU_CTR1(vm, vcpuid, msg " pir3 0x%016lx", pir_desc->pir[3]); \ 2294 VCPU_CTR1(vm, vcpuid, msg " notify: %s", notify ? "yes" : "no");\ 2295 } while (0) 2296 2297 /* 2298 * vlapic->ops handlers that utilize the APICv hardware assist described in 2299 * Chapter 29 of the Intel SDM. 2300 */ 2301 static int 2302 vmx_set_intr_ready(struct vlapic *vlapic, int vector, bool level) 2303 { 2304 struct vlapic_vtx *vlapic_vtx; 2305 struct pir_desc *pir_desc; 2306 uint64_t mask; 2307 int idx, notify; 2308 2309 /* 2310 * XXX need to deal with level triggered interrupts 2311 */ 2312 vlapic_vtx = (struct vlapic_vtx *)vlapic; 2313 pir_desc = vlapic_vtx->pir_desc; 2314 2315 /* 2316 * Keep track of interrupt requests in the PIR descriptor. This is 2317 * because the virtual APIC page pointed to by the VMCS cannot be 2318 * modified if the vcpu is running. 2319 */ 2320 idx = vector / 64; 2321 mask = 1UL << (vector % 64); 2322 atomic_set_long(&pir_desc->pir[idx], mask); 2323 notify = atomic_cmpset_long(&pir_desc->pending, 0, 1); 2324 2325 VMX_CTR_PIR(vlapic->vm, vlapic->vcpuid, pir_desc, notify, vector, 2326 level, "vmx_set_intr_ready"); 2327 return (notify); 2328 } 2329 2330 static int 2331 vmx_pending_intr(struct vlapic *vlapic, int *vecptr) 2332 { 2333 struct vlapic_vtx *vlapic_vtx; 2334 struct pir_desc *pir_desc; 2335 struct LAPIC *lapic; 2336 uint64_t pending, pirval; 2337 uint32_t ppr, vpr; 2338 int i; 2339 2340 /* 2341 * This function is only expected to be called from the 'HLT' exit 2342 * handler which does not care about the vector that is pending. 2343 */ 2344 KASSERT(vecptr == NULL, ("vmx_pending_intr: vecptr must be NULL")); 2345 2346 vlapic_vtx = (struct vlapic_vtx *)vlapic; 2347 pir_desc = vlapic_vtx->pir_desc; 2348 2349 pending = atomic_load_acq_long(&pir_desc->pending); 2350 if (!pending) 2351 return (0); /* common case */ 2352 2353 /* 2354 * If there is an interrupt pending then it will be recognized only 2355 * if its priority is greater than the processor priority. 2356 * 2357 * Special case: if the processor priority is zero then any pending 2358 * interrupt will be recognized. 2359 */ 2360 lapic = vlapic->apic_page; 2361 ppr = lapic->ppr & 0xf0; 2362 if (ppr == 0) 2363 return (1); 2364 2365 VCPU_CTR1(vlapic->vm, vlapic->vcpuid, "HLT with non-zero PPR %d", 2366 lapic->ppr); 2367 2368 for (i = 3; i >= 0; i--) { 2369 pirval = pir_desc->pir[i]; 2370 if (pirval != 0) { 2371 vpr = (i * 64 + flsl(pirval) - 1) & 0xf0; 2372 return (vpr > ppr); 2373 } 2374 } 2375 return (0); 2376 } 2377 2378 static void 2379 vmx_intr_accepted(struct vlapic *vlapic, int vector) 2380 { 2381 2382 panic("vmx_intr_accepted: not expected to be called"); 2383 } 2384 2385 static void 2386 vmx_post_intr(struct vlapic *vlapic, int hostcpu) 2387 { 2388 2389 ipi_cpu(hostcpu, pirvec); 2390 } 2391 2392 /* 2393 * Transfer the pending interrupts in the PIR descriptor to the IRR 2394 * in the virtual APIC page. 2395 */ 2396 static void 2397 vmx_inject_pir(struct vlapic *vlapic) 2398 { 2399 struct vlapic_vtx *vlapic_vtx; 2400 struct pir_desc *pir_desc; 2401 struct LAPIC *lapic; 2402 uint64_t val, pirval; 2403 int rvi, pirbase; 2404 uint16_t intr_status_old, intr_status_new; 2405 2406 vlapic_vtx = (struct vlapic_vtx *)vlapic; 2407 pir_desc = vlapic_vtx->pir_desc; 2408 if (atomic_cmpset_long(&pir_desc->pending, 1, 0) == 0) { 2409 VCPU_CTR0(vlapic->vm, vlapic->vcpuid, "vmx_inject_pir: " 2410 "no posted interrupt pending"); 2411 return; 2412 } 2413 2414 pirval = 0; 2415 lapic = vlapic->apic_page; 2416 2417 val = atomic_readandclear_long(&pir_desc->pir[0]); 2418 if (val != 0) { 2419 lapic->irr0 |= val; 2420 lapic->irr1 |= val >> 32; 2421 pirbase = 0; 2422 pirval = val; 2423 } 2424 2425 val = atomic_readandclear_long(&pir_desc->pir[1]); 2426 if (val != 0) { 2427 lapic->irr2 |= val; 2428 lapic->irr3 |= val >> 32; 2429 pirbase = 64; 2430 pirval = val; 2431 } 2432 2433 val = atomic_readandclear_long(&pir_desc->pir[2]); 2434 if (val != 0) { 2435 lapic->irr4 |= val; 2436 lapic->irr5 |= val >> 32; 2437 pirbase = 128; 2438 pirval = val; 2439 } 2440 2441 val = atomic_readandclear_long(&pir_desc->pir[3]); 2442 if (val != 0) { 2443 lapic->irr6 |= val; 2444 lapic->irr7 |= val >> 32; 2445 pirbase = 192; 2446 pirval = val; 2447 } 2448 VLAPIC_CTR_IRR(vlapic, "vmx_inject_pir"); 2449 2450 /* 2451 * Update RVI so the processor can evaluate pending virtual 2452 * interrupts on VM-entry. 2453 */ 2454 if (pirval != 0) { 2455 rvi = pirbase + flsl(pirval) - 1; 2456 intr_status_old = vmcs_read(VMCS_GUEST_INTR_STATUS); 2457 intr_status_new = (intr_status_old & 0xFF00) | rvi; 2458 if (intr_status_new > intr_status_old) { 2459 vmcs_write(VMCS_GUEST_INTR_STATUS, intr_status_new); 2460 VCPU_CTR2(vlapic->vm, vlapic->vcpuid, "vmx_inject_pir: " 2461 "guest_intr_status changed from 0x%04x to 0x%04x", 2462 intr_status_old, intr_status_new); 2463 } 2464 } 2465 } 2466 2467 static struct vlapic * 2468 vmx_vlapic_init(void *arg, int vcpuid) 2469 { 2470 struct vmx *vmx; 2471 struct vlapic *vlapic; 2472 struct vlapic_vtx *vlapic_vtx; 2473 2474 vmx = arg; 2475 2476 vlapic = malloc(sizeof(struct vlapic_vtx), M_VLAPIC, M_WAITOK | M_ZERO); 2477 vlapic->vm = vmx->vm; 2478 vlapic->vcpuid = vcpuid; 2479 vlapic->apic_page = (struct LAPIC *)&vmx->apic_page[vcpuid]; 2480 2481 vlapic_vtx = (struct vlapic_vtx *)vlapic; 2482 vlapic_vtx->pir_desc = &vmx->pir_desc[vcpuid]; 2483 2484 if (virtual_interrupt_delivery) { 2485 vlapic->ops.set_intr_ready = vmx_set_intr_ready; 2486 vlapic->ops.pending_intr = vmx_pending_intr; 2487 vlapic->ops.intr_accepted = vmx_intr_accepted; 2488 } 2489 2490 if (posted_interrupts) 2491 vlapic->ops.post_intr = vmx_post_intr; 2492 2493 vlapic_init(vlapic); 2494 2495 return (vlapic); 2496 } 2497 2498 static void 2499 vmx_vlapic_cleanup(void *arg, struct vlapic *vlapic) 2500 { 2501 2502 vlapic_cleanup(vlapic); 2503 free(vlapic, M_VLAPIC); 2504 } 2505 2506 struct vmm_ops vmm_ops_intel = { 2507 vmx_init, 2508 vmx_cleanup, 2509 vmx_restore, 2510 vmx_vminit, 2511 vmx_run, 2512 vmx_vmcleanup, 2513 vmx_getreg, 2514 vmx_setreg, 2515 vmx_getdesc, 2516 vmx_setdesc, 2517 vmx_inject, 2518 vmx_getcap, 2519 vmx_setcap, 2520 ept_vmspace_alloc, 2521 ept_vmspace_free, 2522 vmx_vlapic_init, 2523 vmx_vlapic_cleanup, 2524 }; 2525