1 /* 2 * All test cases of nested virtualization should be in this file 3 * 4 * Author : Arthur Chunqi Li <yzt356@gmail.com> 5 */ 6 7 #include <asm/debugreg.h> 8 9 #include "vmx.h" 10 #include "msr.h" 11 #include "processor.h" 12 #include "vm.h" 13 #include "pci.h" 14 #include "fwcfg.h" 15 #include "isr.h" 16 #include "desc.h" 17 #include "apic.h" 18 #include "types.h" 19 #include "vmalloc.h" 20 #include "alloc_page.h" 21 #include "smp.h" 22 #include "delay.h" 23 24 #define NONCANONICAL 0xaaaaaaaaaaaaaaaaull 25 26 #define VPID_CAP_INVVPID_TYPES_SHIFT 40 27 28 u64 ia32_pat; 29 u64 ia32_efer; 30 void *io_bitmap_a, *io_bitmap_b; 31 u16 ioport; 32 33 unsigned long *pml4; 34 u64 eptp; 35 void *data_page1, *data_page2; 36 37 phys_addr_t pci_physaddr; 38 39 void *pml_log; 40 #define PML_INDEX 512 41 42 static inline unsigned ffs(unsigned x) 43 { 44 int pos = -1; 45 46 __asm__ __volatile__("bsf %1, %%eax; cmovnz %%eax, %0" 47 : "+r"(pos) : "rm"(x) : "eax"); 48 return pos + 1; 49 } 50 51 static inline void vmcall(void) 52 { 53 asm volatile("vmcall"); 54 } 55 56 static void basic_guest_main(void) 57 { 58 report(1, "Basic VMX test"); 59 } 60 61 static int basic_exit_handler(void) 62 { 63 report(0, "Basic VMX test"); 64 print_vmexit_info(); 65 return VMX_TEST_EXIT; 66 } 67 68 static void vmenter_main(void) 69 { 70 u64 rax; 71 u64 rsp, resume_rsp; 72 73 report(1, "test vmlaunch"); 74 75 asm volatile( 76 "mov %%rsp, %0\n\t" 77 "mov %3, %%rax\n\t" 78 "vmcall\n\t" 79 "mov %%rax, %1\n\t" 80 "mov %%rsp, %2\n\t" 81 : "=r"(rsp), "=r"(rax), "=r"(resume_rsp) 82 : "g"(0xABCD)); 83 report((rax == 0xFFFF) && (rsp == resume_rsp), "test vmresume"); 84 } 85 86 static int vmenter_exit_handler(void) 87 { 88 u64 guest_rip; 89 ulong reason; 90 91 guest_rip = vmcs_read(GUEST_RIP); 92 reason = vmcs_read(EXI_REASON) & 0xff; 93 switch (reason) { 94 case VMX_VMCALL: 95 if (regs.rax != 0xABCD) { 96 report(0, "test vmresume"); 97 return VMX_TEST_VMEXIT; 98 } 99 regs.rax = 0xFFFF; 100 vmcs_write(GUEST_RIP, guest_rip + 3); 101 return VMX_TEST_RESUME; 102 default: 103 report(0, "test vmresume"); 104 print_vmexit_info(); 105 } 106 return VMX_TEST_VMEXIT; 107 } 108 109 u32 preempt_scale; 110 volatile unsigned long long tsc_val; 111 volatile u32 preempt_val; 112 u64 saved_rip; 113 114 static int preemption_timer_init(struct vmcs *vmcs) 115 { 116 if (!(ctrl_pin_rev.clr & PIN_PREEMPT)) { 117 printf("\tPreemption timer is not supported\n"); 118 return VMX_TEST_EXIT; 119 } 120 vmcs_write(PIN_CONTROLS, vmcs_read(PIN_CONTROLS) | PIN_PREEMPT); 121 preempt_val = 10000000; 122 vmcs_write(PREEMPT_TIMER_VALUE, preempt_val); 123 preempt_scale = rdmsr(MSR_IA32_VMX_MISC) & 0x1F; 124 125 if (!(ctrl_exit_rev.clr & EXI_SAVE_PREEMPT)) 126 printf("\tSave preemption value is not supported\n"); 127 128 return VMX_TEST_START; 129 } 130 131 static void preemption_timer_main(void) 132 { 133 tsc_val = rdtsc(); 134 if (ctrl_exit_rev.clr & EXI_SAVE_PREEMPT) { 135 vmx_set_test_stage(0); 136 vmcall(); 137 if (vmx_get_test_stage() == 1) 138 vmcall(); 139 } 140 vmx_set_test_stage(1); 141 while (vmx_get_test_stage() == 1) { 142 if (((rdtsc() - tsc_val) >> preempt_scale) 143 > 10 * preempt_val) { 144 vmx_set_test_stage(2); 145 vmcall(); 146 } 147 } 148 tsc_val = rdtsc(); 149 asm volatile ("hlt"); 150 vmcall(); 151 vmx_set_test_stage(5); 152 vmcall(); 153 } 154 155 static int preemption_timer_exit_handler(void) 156 { 157 bool guest_halted; 158 u64 guest_rip; 159 ulong reason; 160 u32 insn_len; 161 u32 ctrl_exit; 162 163 guest_rip = vmcs_read(GUEST_RIP); 164 reason = vmcs_read(EXI_REASON) & 0xff; 165 insn_len = vmcs_read(EXI_INST_LEN); 166 switch (reason) { 167 case VMX_PREEMPT: 168 switch (vmx_get_test_stage()) { 169 case 1: 170 case 2: 171 report(((rdtsc() - tsc_val) >> preempt_scale) >= preempt_val, 172 "busy-wait for preemption timer"); 173 vmx_set_test_stage(3); 174 vmcs_write(PREEMPT_TIMER_VALUE, preempt_val); 175 return VMX_TEST_RESUME; 176 case 3: 177 guest_halted = 178 (vmcs_read(GUEST_ACTV_STATE) == ACTV_HLT); 179 report(((rdtsc() - tsc_val) >> preempt_scale) >= preempt_val 180 && guest_halted, 181 "preemption timer during hlt"); 182 vmx_set_test_stage(4); 183 vmcs_write(PIN_CONTROLS, 184 vmcs_read(PIN_CONTROLS) & ~PIN_PREEMPT); 185 vmcs_write(EXI_CONTROLS, 186 vmcs_read(EXI_CONTROLS) & ~EXI_SAVE_PREEMPT); 187 vmcs_write(GUEST_ACTV_STATE, ACTV_ACTIVE); 188 return VMX_TEST_RESUME; 189 case 4: 190 report(saved_rip == guest_rip, 191 "preemption timer with 0 value"); 192 break; 193 default: 194 report(false, "Invalid stage."); 195 print_vmexit_info(); 196 break; 197 } 198 break; 199 case VMX_VMCALL: 200 vmcs_write(GUEST_RIP, guest_rip + insn_len); 201 switch (vmx_get_test_stage()) { 202 case 0: 203 report(vmcs_read(PREEMPT_TIMER_VALUE) == preempt_val, 204 "Keep preemption value"); 205 vmx_set_test_stage(1); 206 vmcs_write(PREEMPT_TIMER_VALUE, preempt_val); 207 ctrl_exit = (vmcs_read(EXI_CONTROLS) | 208 EXI_SAVE_PREEMPT) & ctrl_exit_rev.clr; 209 vmcs_write(EXI_CONTROLS, ctrl_exit); 210 return VMX_TEST_RESUME; 211 case 1: 212 report(vmcs_read(PREEMPT_TIMER_VALUE) < preempt_val, 213 "Save preemption value"); 214 return VMX_TEST_RESUME; 215 case 2: 216 report(0, "busy-wait for preemption timer"); 217 vmx_set_test_stage(3); 218 vmcs_write(PREEMPT_TIMER_VALUE, preempt_val); 219 return VMX_TEST_RESUME; 220 case 3: 221 report(0, "preemption timer during hlt"); 222 vmx_set_test_stage(4); 223 /* fall through */ 224 case 4: 225 vmcs_write(PIN_CONTROLS, 226 vmcs_read(PIN_CONTROLS) | PIN_PREEMPT); 227 vmcs_write(PREEMPT_TIMER_VALUE, 0); 228 saved_rip = guest_rip + insn_len; 229 return VMX_TEST_RESUME; 230 case 5: 231 report(0, 232 "preemption timer with 0 value (vmcall stage 5)"); 233 break; 234 default: 235 // Should not reach here 236 report(false, "unexpected stage, %d", 237 vmx_get_test_stage()); 238 print_vmexit_info(); 239 return VMX_TEST_VMEXIT; 240 } 241 break; 242 default: 243 report(false, "Unknown exit reason, %ld", reason); 244 print_vmexit_info(); 245 } 246 vmcs_write(PIN_CONTROLS, vmcs_read(PIN_CONTROLS) & ~PIN_PREEMPT); 247 return VMX_TEST_VMEXIT; 248 } 249 250 static void msr_bmp_init(void) 251 { 252 void *msr_bitmap; 253 u32 ctrl_cpu0; 254 255 msr_bitmap = alloc_page(); 256 ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0); 257 ctrl_cpu0 |= CPU_MSR_BITMAP; 258 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0); 259 vmcs_write(MSR_BITMAP, (u64)msr_bitmap); 260 } 261 262 static void *get_msr_bitmap(void) 263 { 264 void *msr_bitmap; 265 266 if (vmcs_read(CPU_EXEC_CTRL0) & CPU_MSR_BITMAP) { 267 msr_bitmap = (void *)vmcs_read(MSR_BITMAP); 268 } else { 269 msr_bitmap = alloc_page(); 270 memset(msr_bitmap, 0xff, PAGE_SIZE); 271 vmcs_write(MSR_BITMAP, (u64)msr_bitmap); 272 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_MSR_BITMAP); 273 } 274 275 return msr_bitmap; 276 } 277 278 static void disable_intercept_for_x2apic_msrs(void) 279 { 280 unsigned long *msr_bitmap = (unsigned long *)get_msr_bitmap(); 281 u32 msr; 282 283 for (msr = APIC_BASE_MSR; 284 msr < (APIC_BASE_MSR+0xff); 285 msr += BITS_PER_LONG) { 286 unsigned int word = msr / BITS_PER_LONG; 287 288 msr_bitmap[word] = 0; 289 msr_bitmap[word + (0x800 / sizeof(long))] = 0; 290 } 291 } 292 293 static int test_ctrl_pat_init(struct vmcs *vmcs) 294 { 295 u64 ctrl_ent; 296 u64 ctrl_exi; 297 298 msr_bmp_init(); 299 if (!(ctrl_exit_rev.clr & EXI_SAVE_PAT) && 300 !(ctrl_exit_rev.clr & EXI_LOAD_PAT) && 301 !(ctrl_enter_rev.clr & ENT_LOAD_PAT)) { 302 printf("\tSave/load PAT is not supported\n"); 303 return 1; 304 } 305 306 ctrl_ent = vmcs_read(ENT_CONTROLS); 307 ctrl_exi = vmcs_read(EXI_CONTROLS); 308 ctrl_ent |= ctrl_enter_rev.clr & ENT_LOAD_PAT; 309 ctrl_exi |= ctrl_exit_rev.clr & (EXI_SAVE_PAT | EXI_LOAD_PAT); 310 vmcs_write(ENT_CONTROLS, ctrl_ent); 311 vmcs_write(EXI_CONTROLS, ctrl_exi); 312 ia32_pat = rdmsr(MSR_IA32_CR_PAT); 313 vmcs_write(GUEST_PAT, 0x0); 314 vmcs_write(HOST_PAT, ia32_pat); 315 return VMX_TEST_START; 316 } 317 318 static void test_ctrl_pat_main(void) 319 { 320 u64 guest_ia32_pat; 321 322 guest_ia32_pat = rdmsr(MSR_IA32_CR_PAT); 323 if (!(ctrl_enter_rev.clr & ENT_LOAD_PAT)) 324 printf("\tENT_LOAD_PAT is not supported.\n"); 325 else { 326 if (guest_ia32_pat != 0) { 327 report(0, "Entry load PAT"); 328 return; 329 } 330 } 331 wrmsr(MSR_IA32_CR_PAT, 0x6); 332 vmcall(); 333 guest_ia32_pat = rdmsr(MSR_IA32_CR_PAT); 334 if (ctrl_enter_rev.clr & ENT_LOAD_PAT) 335 report(guest_ia32_pat == ia32_pat, "Entry load PAT"); 336 } 337 338 static int test_ctrl_pat_exit_handler(void) 339 { 340 u64 guest_rip; 341 ulong reason; 342 u64 guest_pat; 343 344 guest_rip = vmcs_read(GUEST_RIP); 345 reason = vmcs_read(EXI_REASON) & 0xff; 346 switch (reason) { 347 case VMX_VMCALL: 348 guest_pat = vmcs_read(GUEST_PAT); 349 if (!(ctrl_exit_rev.clr & EXI_SAVE_PAT)) { 350 printf("\tEXI_SAVE_PAT is not supported\n"); 351 vmcs_write(GUEST_PAT, 0x6); 352 } else { 353 report(guest_pat == 0x6, "Exit save PAT"); 354 } 355 if (!(ctrl_exit_rev.clr & EXI_LOAD_PAT)) 356 printf("\tEXI_LOAD_PAT is not supported\n"); 357 else 358 report(rdmsr(MSR_IA32_CR_PAT) == ia32_pat, 359 "Exit load PAT"); 360 vmcs_write(GUEST_PAT, ia32_pat); 361 vmcs_write(GUEST_RIP, guest_rip + 3); 362 return VMX_TEST_RESUME; 363 default: 364 printf("ERROR : Undefined exit reason, reason = %ld.\n", reason); 365 break; 366 } 367 return VMX_TEST_VMEXIT; 368 } 369 370 static int test_ctrl_efer_init(struct vmcs *vmcs) 371 { 372 u64 ctrl_ent; 373 u64 ctrl_exi; 374 375 msr_bmp_init(); 376 ctrl_ent = vmcs_read(ENT_CONTROLS) | ENT_LOAD_EFER; 377 ctrl_exi = vmcs_read(EXI_CONTROLS) | EXI_SAVE_EFER | EXI_LOAD_EFER; 378 vmcs_write(ENT_CONTROLS, ctrl_ent & ctrl_enter_rev.clr); 379 vmcs_write(EXI_CONTROLS, ctrl_exi & ctrl_exit_rev.clr); 380 ia32_efer = rdmsr(MSR_EFER); 381 vmcs_write(GUEST_EFER, ia32_efer ^ EFER_NX); 382 vmcs_write(HOST_EFER, ia32_efer ^ EFER_NX); 383 return VMX_TEST_START; 384 } 385 386 static void test_ctrl_efer_main(void) 387 { 388 u64 guest_ia32_efer; 389 390 guest_ia32_efer = rdmsr(MSR_EFER); 391 if (!(ctrl_enter_rev.clr & ENT_LOAD_EFER)) 392 printf("\tENT_LOAD_EFER is not supported.\n"); 393 else { 394 if (guest_ia32_efer != (ia32_efer ^ EFER_NX)) { 395 report(0, "Entry load EFER"); 396 return; 397 } 398 } 399 wrmsr(MSR_EFER, ia32_efer); 400 vmcall(); 401 guest_ia32_efer = rdmsr(MSR_EFER); 402 if (ctrl_enter_rev.clr & ENT_LOAD_EFER) 403 report(guest_ia32_efer == ia32_efer, "Entry load EFER"); 404 } 405 406 static int test_ctrl_efer_exit_handler(void) 407 { 408 u64 guest_rip; 409 ulong reason; 410 u64 guest_efer; 411 412 guest_rip = vmcs_read(GUEST_RIP); 413 reason = vmcs_read(EXI_REASON) & 0xff; 414 switch (reason) { 415 case VMX_VMCALL: 416 guest_efer = vmcs_read(GUEST_EFER); 417 if (!(ctrl_exit_rev.clr & EXI_SAVE_EFER)) { 418 printf("\tEXI_SAVE_EFER is not supported\n"); 419 vmcs_write(GUEST_EFER, ia32_efer); 420 } else { 421 report(guest_efer == ia32_efer, "Exit save EFER"); 422 } 423 if (!(ctrl_exit_rev.clr & EXI_LOAD_EFER)) { 424 printf("\tEXI_LOAD_EFER is not supported\n"); 425 wrmsr(MSR_EFER, ia32_efer ^ EFER_NX); 426 } else { 427 report(rdmsr(MSR_EFER) == (ia32_efer ^ EFER_NX), 428 "Exit load EFER"); 429 } 430 vmcs_write(GUEST_PAT, ia32_efer); 431 vmcs_write(GUEST_RIP, guest_rip + 3); 432 return VMX_TEST_RESUME; 433 default: 434 printf("ERROR : Undefined exit reason, reason = %ld.\n", reason); 435 break; 436 } 437 return VMX_TEST_VMEXIT; 438 } 439 440 u32 guest_cr0, guest_cr4; 441 442 static void cr_shadowing_main(void) 443 { 444 u32 cr0, cr4, tmp; 445 446 // Test read through 447 vmx_set_test_stage(0); 448 guest_cr0 = read_cr0(); 449 if (vmx_get_test_stage() == 1) 450 report(0, "Read through CR0"); 451 else 452 vmcall(); 453 vmx_set_test_stage(1); 454 guest_cr4 = read_cr4(); 455 if (vmx_get_test_stage() == 2) 456 report(0, "Read through CR4"); 457 else 458 vmcall(); 459 // Test write through 460 guest_cr0 = guest_cr0 ^ (X86_CR0_TS | X86_CR0_MP); 461 guest_cr4 = guest_cr4 ^ (X86_CR4_TSD | X86_CR4_DE); 462 vmx_set_test_stage(2); 463 write_cr0(guest_cr0); 464 if (vmx_get_test_stage() == 3) 465 report(0, "Write throuth CR0"); 466 else 467 vmcall(); 468 vmx_set_test_stage(3); 469 write_cr4(guest_cr4); 470 if (vmx_get_test_stage() == 4) 471 report(0, "Write through CR4"); 472 else 473 vmcall(); 474 // Test read shadow 475 vmx_set_test_stage(4); 476 vmcall(); 477 cr0 = read_cr0(); 478 if (vmx_get_test_stage() != 5) 479 report(cr0 == guest_cr0, "Read shadowing CR0"); 480 vmx_set_test_stage(5); 481 cr4 = read_cr4(); 482 if (vmx_get_test_stage() != 6) 483 report(cr4 == guest_cr4, "Read shadowing CR4"); 484 // Test write shadow (same value with shadow) 485 vmx_set_test_stage(6); 486 write_cr0(guest_cr0); 487 if (vmx_get_test_stage() == 7) 488 report(0, "Write shadowing CR0 (same value with shadow)"); 489 else 490 vmcall(); 491 vmx_set_test_stage(7); 492 write_cr4(guest_cr4); 493 if (vmx_get_test_stage() == 8) 494 report(0, "Write shadowing CR4 (same value with shadow)"); 495 else 496 vmcall(); 497 // Test write shadow (different value) 498 vmx_set_test_stage(8); 499 tmp = guest_cr0 ^ X86_CR0_TS; 500 asm volatile("mov %0, %%rsi\n\t" 501 "mov %%rsi, %%cr0\n\t" 502 ::"m"(tmp) 503 :"rsi", "memory", "cc"); 504 report(vmx_get_test_stage() == 9, 505 "Write shadowing different X86_CR0_TS"); 506 vmx_set_test_stage(9); 507 tmp = guest_cr0 ^ X86_CR0_MP; 508 asm volatile("mov %0, %%rsi\n\t" 509 "mov %%rsi, %%cr0\n\t" 510 ::"m"(tmp) 511 :"rsi", "memory", "cc"); 512 report(vmx_get_test_stage() == 10, 513 "Write shadowing different X86_CR0_MP"); 514 vmx_set_test_stage(10); 515 tmp = guest_cr4 ^ X86_CR4_TSD; 516 asm volatile("mov %0, %%rsi\n\t" 517 "mov %%rsi, %%cr4\n\t" 518 ::"m"(tmp) 519 :"rsi", "memory", "cc"); 520 report(vmx_get_test_stage() == 11, 521 "Write shadowing different X86_CR4_TSD"); 522 vmx_set_test_stage(11); 523 tmp = guest_cr4 ^ X86_CR4_DE; 524 asm volatile("mov %0, %%rsi\n\t" 525 "mov %%rsi, %%cr4\n\t" 526 ::"m"(tmp) 527 :"rsi", "memory", "cc"); 528 report(vmx_get_test_stage() == 12, 529 "Write shadowing different X86_CR4_DE"); 530 } 531 532 static int cr_shadowing_exit_handler(void) 533 { 534 u64 guest_rip; 535 ulong reason; 536 u32 insn_len; 537 u32 exit_qual; 538 539 guest_rip = vmcs_read(GUEST_RIP); 540 reason = vmcs_read(EXI_REASON) & 0xff; 541 insn_len = vmcs_read(EXI_INST_LEN); 542 exit_qual = vmcs_read(EXI_QUALIFICATION); 543 switch (reason) { 544 case VMX_VMCALL: 545 switch (vmx_get_test_stage()) { 546 case 0: 547 report(guest_cr0 == vmcs_read(GUEST_CR0), 548 "Read through CR0"); 549 break; 550 case 1: 551 report(guest_cr4 == vmcs_read(GUEST_CR4), 552 "Read through CR4"); 553 break; 554 case 2: 555 report(guest_cr0 == vmcs_read(GUEST_CR0), 556 "Write through CR0"); 557 break; 558 case 3: 559 report(guest_cr4 == vmcs_read(GUEST_CR4), 560 "Write through CR4"); 561 break; 562 case 4: 563 guest_cr0 = vmcs_read(GUEST_CR0) ^ (X86_CR0_TS | X86_CR0_MP); 564 guest_cr4 = vmcs_read(GUEST_CR4) ^ (X86_CR4_TSD | X86_CR4_DE); 565 vmcs_write(CR0_MASK, X86_CR0_TS | X86_CR0_MP); 566 vmcs_write(CR0_READ_SHADOW, guest_cr0 & (X86_CR0_TS | X86_CR0_MP)); 567 vmcs_write(CR4_MASK, X86_CR4_TSD | X86_CR4_DE); 568 vmcs_write(CR4_READ_SHADOW, guest_cr4 & (X86_CR4_TSD | X86_CR4_DE)); 569 break; 570 case 6: 571 report(guest_cr0 == (vmcs_read(GUEST_CR0) ^ (X86_CR0_TS | X86_CR0_MP)), 572 "Write shadowing CR0 (same value)"); 573 break; 574 case 7: 575 report(guest_cr4 == (vmcs_read(GUEST_CR4) ^ (X86_CR4_TSD | X86_CR4_DE)), 576 "Write shadowing CR4 (same value)"); 577 break; 578 default: 579 // Should not reach here 580 report(false, "unexpected stage, %d", 581 vmx_get_test_stage()); 582 print_vmexit_info(); 583 return VMX_TEST_VMEXIT; 584 } 585 vmcs_write(GUEST_RIP, guest_rip + insn_len); 586 return VMX_TEST_RESUME; 587 case VMX_CR: 588 switch (vmx_get_test_stage()) { 589 case 4: 590 report(0, "Read shadowing CR0"); 591 vmx_inc_test_stage(); 592 break; 593 case 5: 594 report(0, "Read shadowing CR4"); 595 vmx_inc_test_stage(); 596 break; 597 case 6: 598 report(0, "Write shadowing CR0 (same value)"); 599 vmx_inc_test_stage(); 600 break; 601 case 7: 602 report(0, "Write shadowing CR4 (same value)"); 603 vmx_inc_test_stage(); 604 break; 605 case 8: 606 case 9: 607 // 0x600 encodes "mov %esi, %cr0" 608 if (exit_qual == 0x600) 609 vmx_inc_test_stage(); 610 break; 611 case 10: 612 case 11: 613 // 0x604 encodes "mov %esi, %cr4" 614 if (exit_qual == 0x604) 615 vmx_inc_test_stage(); 616 break; 617 default: 618 // Should not reach here 619 report(false, "unexpected stage, %d", 620 vmx_get_test_stage()); 621 print_vmexit_info(); 622 return VMX_TEST_VMEXIT; 623 } 624 vmcs_write(GUEST_RIP, guest_rip + insn_len); 625 return VMX_TEST_RESUME; 626 default: 627 report(false, "Unknown exit reason, %ld", reason); 628 print_vmexit_info(); 629 } 630 return VMX_TEST_VMEXIT; 631 } 632 633 static int iobmp_init(struct vmcs *vmcs) 634 { 635 u32 ctrl_cpu0; 636 637 io_bitmap_a = alloc_page(); 638 io_bitmap_b = alloc_page(); 639 ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0); 640 ctrl_cpu0 |= CPU_IO_BITMAP; 641 ctrl_cpu0 &= (~CPU_IO); 642 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0); 643 vmcs_write(IO_BITMAP_A, (u64)io_bitmap_a); 644 vmcs_write(IO_BITMAP_B, (u64)io_bitmap_b); 645 return VMX_TEST_START; 646 } 647 648 static void iobmp_main(void) 649 { 650 // stage 0, test IO pass 651 vmx_set_test_stage(0); 652 inb(0x5000); 653 outb(0x0, 0x5000); 654 report(vmx_get_test_stage() == 0, "I/O bitmap - I/O pass"); 655 // test IO width, in/out 656 ((u8 *)io_bitmap_a)[0] = 0xFF; 657 vmx_set_test_stage(2); 658 inb(0x0); 659 report(vmx_get_test_stage() == 3, "I/O bitmap - trap in"); 660 vmx_set_test_stage(3); 661 outw(0x0, 0x0); 662 report(vmx_get_test_stage() == 4, "I/O bitmap - trap out"); 663 vmx_set_test_stage(4); 664 inl(0x0); 665 report(vmx_get_test_stage() == 5, "I/O bitmap - I/O width, long"); 666 // test low/high IO port 667 vmx_set_test_stage(5); 668 ((u8 *)io_bitmap_a)[0x5000 / 8] = (1 << (0x5000 % 8)); 669 inb(0x5000); 670 report(vmx_get_test_stage() == 6, "I/O bitmap - I/O port, low part"); 671 vmx_set_test_stage(6); 672 ((u8 *)io_bitmap_b)[0x1000 / 8] = (1 << (0x1000 % 8)); 673 inb(0x9000); 674 report(vmx_get_test_stage() == 7, "I/O bitmap - I/O port, high part"); 675 // test partial pass 676 vmx_set_test_stage(7); 677 inl(0x4FFF); 678 report(vmx_get_test_stage() == 8, "I/O bitmap - partial pass"); 679 // test overrun 680 vmx_set_test_stage(8); 681 memset(io_bitmap_a, 0x0, PAGE_SIZE); 682 memset(io_bitmap_b, 0x0, PAGE_SIZE); 683 inl(0xFFFF); 684 report(vmx_get_test_stage() == 9, "I/O bitmap - overrun"); 685 vmx_set_test_stage(9); 686 vmcall(); 687 outb(0x0, 0x0); 688 report(vmx_get_test_stage() == 9, 689 "I/O bitmap - ignore unconditional exiting"); 690 vmx_set_test_stage(10); 691 vmcall(); 692 outb(0x0, 0x0); 693 report(vmx_get_test_stage() == 11, 694 "I/O bitmap - unconditional exiting"); 695 } 696 697 static int iobmp_exit_handler(void) 698 { 699 u64 guest_rip; 700 ulong reason, exit_qual; 701 u32 insn_len, ctrl_cpu0; 702 703 guest_rip = vmcs_read(GUEST_RIP); 704 reason = vmcs_read(EXI_REASON) & 0xff; 705 exit_qual = vmcs_read(EXI_QUALIFICATION); 706 insn_len = vmcs_read(EXI_INST_LEN); 707 switch (reason) { 708 case VMX_IO: 709 switch (vmx_get_test_stage()) { 710 case 0: 711 case 1: 712 vmx_inc_test_stage(); 713 break; 714 case 2: 715 report((exit_qual & VMX_IO_SIZE_MASK) == _VMX_IO_BYTE, 716 "I/O bitmap - I/O width, byte"); 717 report(exit_qual & VMX_IO_IN, 718 "I/O bitmap - I/O direction, in"); 719 vmx_inc_test_stage(); 720 break; 721 case 3: 722 report((exit_qual & VMX_IO_SIZE_MASK) == _VMX_IO_WORD, 723 "I/O bitmap - I/O width, word"); 724 report(!(exit_qual & VMX_IO_IN), 725 "I/O bitmap - I/O direction, out"); 726 vmx_inc_test_stage(); 727 break; 728 case 4: 729 report((exit_qual & VMX_IO_SIZE_MASK) == _VMX_IO_LONG, 730 "I/O bitmap - I/O width, long"); 731 vmx_inc_test_stage(); 732 break; 733 case 5: 734 if (((exit_qual & VMX_IO_PORT_MASK) >> VMX_IO_PORT_SHIFT) == 0x5000) 735 vmx_inc_test_stage(); 736 break; 737 case 6: 738 if (((exit_qual & VMX_IO_PORT_MASK) >> VMX_IO_PORT_SHIFT) == 0x9000) 739 vmx_inc_test_stage(); 740 break; 741 case 7: 742 if (((exit_qual & VMX_IO_PORT_MASK) >> VMX_IO_PORT_SHIFT) == 0x4FFF) 743 vmx_inc_test_stage(); 744 break; 745 case 8: 746 if (((exit_qual & VMX_IO_PORT_MASK) >> VMX_IO_PORT_SHIFT) == 0xFFFF) 747 vmx_inc_test_stage(); 748 break; 749 case 9: 750 case 10: 751 ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0); 752 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0 & ~CPU_IO); 753 vmx_inc_test_stage(); 754 break; 755 default: 756 // Should not reach here 757 report(false, "unexpected stage, %d", 758 vmx_get_test_stage()); 759 print_vmexit_info(); 760 return VMX_TEST_VMEXIT; 761 } 762 vmcs_write(GUEST_RIP, guest_rip + insn_len); 763 return VMX_TEST_RESUME; 764 case VMX_VMCALL: 765 switch (vmx_get_test_stage()) { 766 case 9: 767 ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0); 768 ctrl_cpu0 |= CPU_IO | CPU_IO_BITMAP; 769 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0); 770 break; 771 case 10: 772 ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0); 773 ctrl_cpu0 = (ctrl_cpu0 & ~CPU_IO_BITMAP) | CPU_IO; 774 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0); 775 break; 776 default: 777 // Should not reach here 778 report(false, "unexpected stage, %d", 779 vmx_get_test_stage()); 780 print_vmexit_info(); 781 return VMX_TEST_VMEXIT; 782 } 783 vmcs_write(GUEST_RIP, guest_rip + insn_len); 784 return VMX_TEST_RESUME; 785 default: 786 printf("guest_rip = %#lx\n", guest_rip); 787 printf("\tERROR : Undefined exit reason, reason = %ld.\n", reason); 788 break; 789 } 790 return VMX_TEST_VMEXIT; 791 } 792 793 #define INSN_CPU0 0 794 #define INSN_CPU1 1 795 #define INSN_ALWAYS_TRAP 2 796 797 #define FIELD_EXIT_QUAL (1 << 0) 798 #define FIELD_INSN_INFO (1 << 1) 799 800 asm( 801 "insn_hlt: hlt;ret\n\t" 802 "insn_invlpg: invlpg 0x12345678;ret\n\t" 803 "insn_mwait: xor %eax, %eax; xor %ecx, %ecx; mwait;ret\n\t" 804 "insn_rdpmc: xor %ecx, %ecx; rdpmc;ret\n\t" 805 "insn_rdtsc: rdtsc;ret\n\t" 806 "insn_cr3_load: mov cr3,%rax; mov %rax,%cr3;ret\n\t" 807 "insn_cr3_store: mov %cr3,%rax;ret\n\t" 808 #ifdef __x86_64__ 809 "insn_cr8_load: xor %eax, %eax; mov %rax,%cr8;ret\n\t" 810 "insn_cr8_store: mov %cr8,%rax;ret\n\t" 811 #endif 812 "insn_monitor: xor %eax, %eax; xor %ecx, %ecx; xor %edx, %edx; monitor;ret\n\t" 813 "insn_pause: pause;ret\n\t" 814 "insn_wbinvd: wbinvd;ret\n\t" 815 "insn_cpuid: mov $10, %eax; cpuid;ret\n\t" 816 "insn_invd: invd;ret\n\t" 817 "insn_sgdt: sgdt gdt64_desc;ret\n\t" 818 "insn_lgdt: lgdt gdt64_desc;ret\n\t" 819 "insn_sidt: sidt idt_descr;ret\n\t" 820 "insn_lidt: lidt idt_descr;ret\n\t" 821 "insn_sldt: sldt %ax;ret\n\t" 822 "insn_lldt: xor %eax, %eax; lldt %ax;ret\n\t" 823 "insn_str: str %ax;ret\n\t" 824 "insn_rdrand: rdrand %rax;ret\n\t" 825 "insn_rdseed: rdseed %rax;ret\n\t" 826 ); 827 extern void insn_hlt(void); 828 extern void insn_invlpg(void); 829 extern void insn_mwait(void); 830 extern void insn_rdpmc(void); 831 extern void insn_rdtsc(void); 832 extern void insn_cr3_load(void); 833 extern void insn_cr3_store(void); 834 #ifdef __x86_64__ 835 extern void insn_cr8_load(void); 836 extern void insn_cr8_store(void); 837 #endif 838 extern void insn_monitor(void); 839 extern void insn_pause(void); 840 extern void insn_wbinvd(void); 841 extern void insn_sgdt(void); 842 extern void insn_lgdt(void); 843 extern void insn_sidt(void); 844 extern void insn_lidt(void); 845 extern void insn_sldt(void); 846 extern void insn_lldt(void); 847 extern void insn_str(void); 848 extern void insn_cpuid(void); 849 extern void insn_invd(void); 850 extern void insn_rdrand(void); 851 extern void insn_rdseed(void); 852 853 u32 cur_insn; 854 u64 cr3; 855 856 #define X86_FEATURE_MONITOR (1 << 3) 857 858 typedef bool (*supported_fn)(void); 859 860 static bool monitor_supported(void) 861 { 862 return this_cpu_has(X86_FEATURE_MWAIT); 863 } 864 865 struct insn_table { 866 const char *name; 867 u32 flag; 868 void (*insn_func)(void); 869 u32 type; 870 u32 reason; 871 ulong exit_qual; 872 u32 insn_info; 873 // Use FIELD_EXIT_QUAL and FIELD_INSN_INFO to define 874 // which field need to be tested, reason is always tested 875 u32 test_field; 876 const supported_fn supported_fn; 877 u8 disabled; 878 }; 879 880 /* 881 * Add more test cases of instruction intercept here. Elements in this 882 * table is: 883 * name/control flag/insn function/type/exit reason/exit qulification/ 884 * instruction info/field to test 885 * The last field defines which fields (exit_qual and insn_info) need to be 886 * tested in exit handler. If set to 0, only "reason" is checked. 887 */ 888 static struct insn_table insn_table[] = { 889 // Flags for Primary Processor-Based VM-Execution Controls 890 {"HLT", CPU_HLT, insn_hlt, INSN_CPU0, 12, 0, 0, 0}, 891 {"INVLPG", CPU_INVLPG, insn_invlpg, INSN_CPU0, 14, 892 0x12345678, 0, FIELD_EXIT_QUAL}, 893 {"MWAIT", CPU_MWAIT, insn_mwait, INSN_CPU0, 36, 0, 0, 0, &monitor_supported}, 894 {"RDPMC", CPU_RDPMC, insn_rdpmc, INSN_CPU0, 15, 0, 0, 0}, 895 {"RDTSC", CPU_RDTSC, insn_rdtsc, INSN_CPU0, 16, 0, 0, 0}, 896 {"CR3 load", CPU_CR3_LOAD, insn_cr3_load, INSN_CPU0, 28, 0x3, 0, 897 FIELD_EXIT_QUAL}, 898 {"CR3 store", CPU_CR3_STORE, insn_cr3_store, INSN_CPU0, 28, 0x13, 0, 899 FIELD_EXIT_QUAL}, 900 #ifdef __x86_64__ 901 {"CR8 load", CPU_CR8_LOAD, insn_cr8_load, INSN_CPU0, 28, 0x8, 0, 902 FIELD_EXIT_QUAL}, 903 {"CR8 store", CPU_CR8_STORE, insn_cr8_store, INSN_CPU0, 28, 0x18, 0, 904 FIELD_EXIT_QUAL}, 905 #endif 906 {"MONITOR", CPU_MONITOR, insn_monitor, INSN_CPU0, 39, 0, 0, 0, &monitor_supported}, 907 {"PAUSE", CPU_PAUSE, insn_pause, INSN_CPU0, 40, 0, 0, 0}, 908 // Flags for Secondary Processor-Based VM-Execution Controls 909 {"WBINVD", CPU_WBINVD, insn_wbinvd, INSN_CPU1, 54, 0, 0, 0}, 910 {"DESC_TABLE (SGDT)", CPU_DESC_TABLE, insn_sgdt, INSN_CPU1, 46, 0, 0, 0}, 911 {"DESC_TABLE (LGDT)", CPU_DESC_TABLE, insn_lgdt, INSN_CPU1, 46, 0, 0, 0}, 912 {"DESC_TABLE (SIDT)", CPU_DESC_TABLE, insn_sidt, INSN_CPU1, 46, 0, 0, 0}, 913 {"DESC_TABLE (LIDT)", CPU_DESC_TABLE, insn_lidt, INSN_CPU1, 46, 0, 0, 0}, 914 {"DESC_TABLE (SLDT)", CPU_DESC_TABLE, insn_sldt, INSN_CPU1, 47, 0, 0, 0}, 915 {"DESC_TABLE (LLDT)", CPU_DESC_TABLE, insn_lldt, INSN_CPU1, 47, 0, 0, 0}, 916 {"DESC_TABLE (STR)", CPU_DESC_TABLE, insn_str, INSN_CPU1, 47, 0, 0, 0}, 917 /* LTR causes a #GP if done with a busy selector, so it is not tested. */ 918 {"RDRAND", CPU_RDRAND, insn_rdrand, INSN_CPU1, VMX_RDRAND, 0, 0, 0}, 919 {"RDSEED", CPU_RDSEED, insn_rdseed, INSN_CPU1, VMX_RDSEED, 0, 0, 0}, 920 // Instructions always trap 921 {"CPUID", 0, insn_cpuid, INSN_ALWAYS_TRAP, 10, 0, 0, 0}, 922 {"INVD", 0, insn_invd, INSN_ALWAYS_TRAP, 13, 0, 0, 0}, 923 // Instructions never trap 924 {NULL}, 925 }; 926 927 static int insn_intercept_init(struct vmcs *vmcs) 928 { 929 u32 ctrl_cpu, cur_insn; 930 931 ctrl_cpu = ctrl_cpu_rev[0].set | CPU_SECONDARY; 932 ctrl_cpu &= ctrl_cpu_rev[0].clr; 933 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu); 934 vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu_rev[1].set); 935 cr3 = read_cr3(); 936 937 for (cur_insn = 0; insn_table[cur_insn].name != NULL; cur_insn++) { 938 if (insn_table[cur_insn].supported_fn == NULL) 939 continue; 940 insn_table[cur_insn].disabled = !insn_table[cur_insn].supported_fn(); 941 } 942 return VMX_TEST_START; 943 } 944 945 static void insn_intercept_main(void) 946 { 947 for (cur_insn = 0; insn_table[cur_insn].name != NULL; cur_insn++) { 948 vmx_set_test_stage(cur_insn * 2); 949 if ((insn_table[cur_insn].type == INSN_CPU0 && 950 !(ctrl_cpu_rev[0].clr & insn_table[cur_insn].flag)) || 951 (insn_table[cur_insn].type == INSN_CPU1 && 952 !(ctrl_cpu_rev[1].clr & insn_table[cur_insn].flag))) { 953 printf("\tCPU_CTRL%d.CPU_%s is not supported.\n", 954 insn_table[cur_insn].type - INSN_CPU0, 955 insn_table[cur_insn].name); 956 continue; 957 } 958 959 if (insn_table[cur_insn].disabled) { 960 printf("\tFeature required for %s is not supported.\n", 961 insn_table[cur_insn].name); 962 continue; 963 } 964 965 if ((insn_table[cur_insn].type == INSN_CPU0 && 966 !(ctrl_cpu_rev[0].set & insn_table[cur_insn].flag)) || 967 (insn_table[cur_insn].type == INSN_CPU1 && 968 !(ctrl_cpu_rev[1].set & insn_table[cur_insn].flag))) { 969 /* skip hlt, it stalls the guest and is tested below */ 970 if (insn_table[cur_insn].insn_func != insn_hlt) 971 insn_table[cur_insn].insn_func(); 972 report(vmx_get_test_stage() == cur_insn * 2, 973 "execute %s", 974 insn_table[cur_insn].name); 975 } else if (insn_table[cur_insn].type != INSN_ALWAYS_TRAP) 976 printf("\tCPU_CTRL%d.CPU_%s always traps.\n", 977 insn_table[cur_insn].type - INSN_CPU0, 978 insn_table[cur_insn].name); 979 980 vmcall(); 981 982 insn_table[cur_insn].insn_func(); 983 report(vmx_get_test_stage() == cur_insn * 2 + 1, 984 "intercept %s", 985 insn_table[cur_insn].name); 986 987 vmx_set_test_stage(cur_insn * 2 + 1); 988 vmcall(); 989 } 990 } 991 992 static int insn_intercept_exit_handler(void) 993 { 994 u64 guest_rip; 995 u32 reason; 996 ulong exit_qual; 997 u32 insn_len; 998 u32 insn_info; 999 bool pass; 1000 1001 guest_rip = vmcs_read(GUEST_RIP); 1002 reason = vmcs_read(EXI_REASON) & 0xff; 1003 exit_qual = vmcs_read(EXI_QUALIFICATION); 1004 insn_len = vmcs_read(EXI_INST_LEN); 1005 insn_info = vmcs_read(EXI_INST_INFO); 1006 1007 if (reason == VMX_VMCALL) { 1008 u32 val = 0; 1009 1010 if (insn_table[cur_insn].type == INSN_CPU0) 1011 val = vmcs_read(CPU_EXEC_CTRL0); 1012 else if (insn_table[cur_insn].type == INSN_CPU1) 1013 val = vmcs_read(CPU_EXEC_CTRL1); 1014 1015 if (vmx_get_test_stage() & 1) 1016 val &= ~insn_table[cur_insn].flag; 1017 else 1018 val |= insn_table[cur_insn].flag; 1019 1020 if (insn_table[cur_insn].type == INSN_CPU0) 1021 vmcs_write(CPU_EXEC_CTRL0, val | ctrl_cpu_rev[0].set); 1022 else if (insn_table[cur_insn].type == INSN_CPU1) 1023 vmcs_write(CPU_EXEC_CTRL1, val | ctrl_cpu_rev[1].set); 1024 } else { 1025 pass = (cur_insn * 2 == vmx_get_test_stage()) && 1026 insn_table[cur_insn].reason == reason; 1027 if (insn_table[cur_insn].test_field & FIELD_EXIT_QUAL && 1028 insn_table[cur_insn].exit_qual != exit_qual) 1029 pass = false; 1030 if (insn_table[cur_insn].test_field & FIELD_INSN_INFO && 1031 insn_table[cur_insn].insn_info != insn_info) 1032 pass = false; 1033 if (pass) 1034 vmx_inc_test_stage(); 1035 } 1036 vmcs_write(GUEST_RIP, guest_rip + insn_len); 1037 return VMX_TEST_RESUME; 1038 } 1039 1040 /** 1041 * __setup_ept - Setup the VMCS fields to enable Extended Page Tables (EPT) 1042 * @hpa: Host physical address of the top-level, a.k.a. root, EPT table 1043 * @enable_ad: Whether or not to enable Access/Dirty bits for EPT entries 1044 * 1045 * Returns 0 on success, 1 on failure. 1046 * 1047 * Note that @hpa doesn't need to point at actual memory if VM-Launch is 1048 * expected to fail, e.g. setup_dummy_ept() arbitrarily passes '0' to satisfy 1049 * the various EPTP consistency checks, but doesn't ensure backing for HPA '0'. 1050 */ 1051 static int __setup_ept(u64 hpa, bool enable_ad) 1052 { 1053 if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) || 1054 !(ctrl_cpu_rev[1].clr & CPU_EPT)) { 1055 printf("\tEPT is not supported"); 1056 return 1; 1057 } 1058 if (!(ept_vpid.val & EPT_CAP_WB)) { 1059 printf("WB memtype for EPT walks not supported\n"); 1060 return 1; 1061 } 1062 if (!(ept_vpid.val & EPT_CAP_PWL4)) { 1063 printf("\tPWL4 is not supported\n"); 1064 return 1; 1065 } 1066 1067 eptp = EPT_MEM_TYPE_WB; 1068 eptp |= (3 << EPTP_PG_WALK_LEN_SHIFT); 1069 eptp |= hpa; 1070 if (enable_ad) 1071 eptp |= EPTP_AD_FLAG; 1072 1073 vmcs_write(EPTP, eptp); 1074 vmcs_write(CPU_EXEC_CTRL0, vmcs_read(CPU_EXEC_CTRL0)| CPU_SECONDARY); 1075 vmcs_write(CPU_EXEC_CTRL1, vmcs_read(CPU_EXEC_CTRL1)| CPU_EPT); 1076 1077 return 0; 1078 } 1079 1080 /** 1081 * setup_ept - Enable Extended Page Tables (EPT) and setup an identity map 1082 * @enable_ad: Whether or not to enable Access/Dirty bits for EPT entries 1083 * 1084 * Returns 0 on success, 1 on failure. 1085 * 1086 * This is the "real" function for setting up EPT tables, i.e. use this for 1087 * tests that need to run code in the guest with EPT enabled. 1088 */ 1089 static int setup_ept(bool enable_ad) 1090 { 1091 unsigned long end_of_memory; 1092 1093 pml4 = alloc_page(); 1094 1095 if (__setup_ept(virt_to_phys(pml4), enable_ad)) 1096 return 1; 1097 1098 end_of_memory = fwcfg_get_u64(FW_CFG_RAM_SIZE); 1099 if (end_of_memory < (1ul << 32)) 1100 end_of_memory = (1ul << 32); 1101 /* Cannot use large EPT pages if we need to track EPT 1102 * accessed/dirty bits at 4K granularity. 1103 */ 1104 setup_ept_range(pml4, 0, end_of_memory, 0, 1105 !enable_ad && ept_2m_supported(), 1106 EPT_WA | EPT_RA | EPT_EA); 1107 return 0; 1108 } 1109 1110 /** 1111 * setup_dummy_ept - Enable Extended Page Tables (EPT) with a dummy root HPA 1112 * 1113 * Setup EPT using a semi-arbitrary dummy root HPA. This function is intended 1114 * for use by tests that need EPT enabled to verify dependent VMCS controls 1115 * but never expect to fully enter the guest, i.e. don't need setup the actual 1116 * EPT tables. 1117 */ 1118 static void setup_dummy_ept(void) 1119 { 1120 if (__setup_ept(0, false)) 1121 report_abort("EPT setup unexpectedly failed"); 1122 } 1123 1124 static int enable_unrestricted_guest(void) 1125 { 1126 if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) || 1127 !(ctrl_cpu_rev[1].clr & CPU_URG) || 1128 !(ctrl_cpu_rev[1].clr & CPU_EPT)) 1129 return 1; 1130 1131 setup_dummy_ept(); 1132 1133 vmcs_write(CPU_EXEC_CTRL0, vmcs_read(CPU_EXEC_CTRL0) | CPU_SECONDARY); 1134 vmcs_write(CPU_EXEC_CTRL1, vmcs_read(CPU_EXEC_CTRL1) | CPU_URG); 1135 1136 return 0; 1137 } 1138 1139 static void ept_enable_ad_bits(void) 1140 { 1141 eptp |= EPTP_AD_FLAG; 1142 vmcs_write(EPTP, eptp); 1143 } 1144 1145 static void ept_disable_ad_bits(void) 1146 { 1147 eptp &= ~EPTP_AD_FLAG; 1148 vmcs_write(EPTP, eptp); 1149 } 1150 1151 static int ept_ad_enabled(void) 1152 { 1153 return eptp & EPTP_AD_FLAG; 1154 } 1155 1156 static void ept_enable_ad_bits_or_skip_test(void) 1157 { 1158 if (!ept_ad_bits_supported()) 1159 test_skip("EPT AD bits not supported."); 1160 ept_enable_ad_bits(); 1161 } 1162 1163 static int apic_version; 1164 1165 static int ept_init_common(bool have_ad) 1166 { 1167 int ret; 1168 struct pci_dev pcidev; 1169 1170 if (setup_ept(have_ad)) 1171 return VMX_TEST_EXIT; 1172 data_page1 = alloc_page(); 1173 data_page2 = alloc_page(); 1174 *((u32 *)data_page1) = MAGIC_VAL_1; 1175 *((u32 *)data_page2) = MAGIC_VAL_2; 1176 install_ept(pml4, (unsigned long)data_page1, (unsigned long)data_page2, 1177 EPT_RA | EPT_WA | EPT_EA); 1178 1179 apic_version = apic_read(APIC_LVR); 1180 1181 ret = pci_find_dev(PCI_VENDOR_ID_REDHAT, PCI_DEVICE_ID_REDHAT_TEST); 1182 if (ret != PCIDEVADDR_INVALID) { 1183 pci_dev_init(&pcidev, ret); 1184 pci_physaddr = pcidev.resource[PCI_TESTDEV_BAR_MEM]; 1185 } 1186 1187 return VMX_TEST_START; 1188 } 1189 1190 static int ept_init(struct vmcs *vmcs) 1191 { 1192 return ept_init_common(false); 1193 } 1194 1195 static void ept_common(void) 1196 { 1197 vmx_set_test_stage(0); 1198 if (*((u32 *)data_page2) != MAGIC_VAL_1 || 1199 *((u32 *)data_page1) != MAGIC_VAL_1) 1200 report(0, "EPT basic framework - read"); 1201 else { 1202 *((u32 *)data_page2) = MAGIC_VAL_3; 1203 vmcall(); 1204 if (vmx_get_test_stage() == 1) { 1205 if (*((u32 *)data_page1) == MAGIC_VAL_3 && 1206 *((u32 *)data_page2) == MAGIC_VAL_2) 1207 report(1, "EPT basic framework"); 1208 else 1209 report(1, "EPT basic framework - remap"); 1210 } 1211 } 1212 // Test EPT Misconfigurations 1213 vmx_set_test_stage(1); 1214 vmcall(); 1215 *((u32 *)data_page1) = MAGIC_VAL_1; 1216 if (vmx_get_test_stage() != 2) { 1217 report(0, "EPT misconfigurations"); 1218 goto t1; 1219 } 1220 vmx_set_test_stage(2); 1221 vmcall(); 1222 *((u32 *)data_page1) = MAGIC_VAL_1; 1223 report(vmx_get_test_stage() == 3, "EPT misconfigurations"); 1224 t1: 1225 // Test EPT violation 1226 vmx_set_test_stage(3); 1227 vmcall(); 1228 *((u32 *)data_page1) = MAGIC_VAL_1; 1229 report(vmx_get_test_stage() == 4, "EPT violation - page permission"); 1230 // Violation caused by EPT paging structure 1231 vmx_set_test_stage(4); 1232 vmcall(); 1233 *((u32 *)data_page1) = MAGIC_VAL_2; 1234 report(vmx_get_test_stage() == 5, "EPT violation - paging structure"); 1235 1236 // MMIO Read/Write 1237 vmx_set_test_stage(5); 1238 vmcall(); 1239 1240 *(u32 volatile *)pci_physaddr; 1241 report(vmx_get_test_stage() == 6, "MMIO EPT violation - read"); 1242 1243 *(u32 volatile *)pci_physaddr = MAGIC_VAL_1; 1244 report(vmx_get_test_stage() == 7, "MMIO EPT violation - write"); 1245 } 1246 1247 static void ept_main(void) 1248 { 1249 ept_common(); 1250 1251 // Test EPT access to L1 MMIO 1252 vmx_set_test_stage(7); 1253 report(*((u32 *)0xfee00030UL) == apic_version, "EPT - MMIO access"); 1254 1255 // Test invalid operand for INVEPT 1256 vmcall(); 1257 report(vmx_get_test_stage() == 8, "EPT - unsupported INVEPT"); 1258 } 1259 1260 static bool invept_test(int type, u64 eptp) 1261 { 1262 bool ret, supported; 1263 1264 supported = ept_vpid.val & (EPT_CAP_INVEPT_SINGLE >> INVEPT_SINGLE << type); 1265 ret = invept(type, eptp); 1266 1267 if (ret == !supported) 1268 return false; 1269 1270 if (!supported) 1271 printf("WARNING: unsupported invept passed!\n"); 1272 else 1273 printf("WARNING: invept failed!\n"); 1274 1275 return true; 1276 } 1277 1278 static int pml_exit_handler(void) 1279 { 1280 u16 index, count; 1281 ulong reason = vmcs_read(EXI_REASON) & 0xff; 1282 u64 *pmlbuf = pml_log; 1283 u64 guest_rip = vmcs_read(GUEST_RIP);; 1284 u64 guest_cr3 = vmcs_read(GUEST_CR3); 1285 u32 insn_len = vmcs_read(EXI_INST_LEN); 1286 1287 switch (reason) { 1288 case VMX_VMCALL: 1289 switch (vmx_get_test_stage()) { 1290 case 0: 1291 index = vmcs_read(GUEST_PML_INDEX); 1292 for (count = index + 1; count < PML_INDEX; count++) { 1293 if (pmlbuf[count] == (u64)data_page2) { 1294 vmx_inc_test_stage(); 1295 clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page2); 1296 break; 1297 } 1298 } 1299 break; 1300 case 1: 1301 index = vmcs_read(GUEST_PML_INDEX); 1302 /* Keep clearing the dirty bit till a overflow */ 1303 clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page2); 1304 break; 1305 default: 1306 report(false, "unexpected stage, %d.", 1307 vmx_get_test_stage()); 1308 print_vmexit_info(); 1309 return VMX_TEST_VMEXIT; 1310 } 1311 vmcs_write(GUEST_RIP, guest_rip + insn_len); 1312 return VMX_TEST_RESUME; 1313 case VMX_PML_FULL: 1314 vmx_inc_test_stage(); 1315 vmcs_write(GUEST_PML_INDEX, PML_INDEX - 1); 1316 return VMX_TEST_RESUME; 1317 default: 1318 report(false, "Unknown exit reason, %ld", reason); 1319 print_vmexit_info(); 1320 } 1321 return VMX_TEST_VMEXIT; 1322 } 1323 1324 static int ept_exit_handler_common(bool have_ad) 1325 { 1326 u64 guest_rip; 1327 u64 guest_cr3; 1328 ulong reason; 1329 u32 insn_len; 1330 u32 exit_qual; 1331 static unsigned long data_page1_pte, data_page1_pte_pte, memaddr_pte; 1332 1333 guest_rip = vmcs_read(GUEST_RIP); 1334 guest_cr3 = vmcs_read(GUEST_CR3); 1335 reason = vmcs_read(EXI_REASON) & 0xff; 1336 insn_len = vmcs_read(EXI_INST_LEN); 1337 exit_qual = vmcs_read(EXI_QUALIFICATION); 1338 switch (reason) { 1339 case VMX_VMCALL: 1340 switch (vmx_get_test_stage()) { 1341 case 0: 1342 check_ept_ad(pml4, guest_cr3, 1343 (unsigned long)data_page1, 1344 have_ad ? EPT_ACCESS_FLAG : 0, 1345 have_ad ? EPT_ACCESS_FLAG | EPT_DIRTY_FLAG : 0); 1346 check_ept_ad(pml4, guest_cr3, 1347 (unsigned long)data_page2, 1348 have_ad ? EPT_ACCESS_FLAG | EPT_DIRTY_FLAG : 0, 1349 have_ad ? EPT_ACCESS_FLAG | EPT_DIRTY_FLAG : 0); 1350 clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page1); 1351 clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page2); 1352 if (have_ad) 1353 ept_sync(INVEPT_SINGLE, eptp);; 1354 if (*((u32 *)data_page1) == MAGIC_VAL_3 && 1355 *((u32 *)data_page2) == MAGIC_VAL_2) { 1356 vmx_inc_test_stage(); 1357 install_ept(pml4, (unsigned long)data_page2, 1358 (unsigned long)data_page2, 1359 EPT_RA | EPT_WA | EPT_EA); 1360 } else 1361 report(0, "EPT basic framework - write"); 1362 break; 1363 case 1: 1364 install_ept(pml4, (unsigned long)data_page1, 1365 (unsigned long)data_page1, EPT_WA); 1366 ept_sync(INVEPT_SINGLE, eptp); 1367 break; 1368 case 2: 1369 install_ept(pml4, (unsigned long)data_page1, 1370 (unsigned long)data_page1, 1371 EPT_RA | EPT_WA | EPT_EA | 1372 (2 << EPT_MEM_TYPE_SHIFT)); 1373 ept_sync(INVEPT_SINGLE, eptp); 1374 break; 1375 case 3: 1376 clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page1); 1377 TEST_ASSERT(get_ept_pte(pml4, (unsigned long)data_page1, 1378 1, &data_page1_pte)); 1379 set_ept_pte(pml4, (unsigned long)data_page1, 1380 1, data_page1_pte & ~EPT_PRESENT); 1381 ept_sync(INVEPT_SINGLE, eptp); 1382 break; 1383 case 4: 1384 TEST_ASSERT(get_ept_pte(pml4, (unsigned long)data_page1, 1385 2, &data_page1_pte)); 1386 data_page1_pte &= PAGE_MASK; 1387 TEST_ASSERT(get_ept_pte(pml4, data_page1_pte, 1388 2, &data_page1_pte_pte)); 1389 set_ept_pte(pml4, data_page1_pte, 2, 1390 data_page1_pte_pte & ~EPT_PRESENT); 1391 ept_sync(INVEPT_SINGLE, eptp); 1392 break; 1393 case 5: 1394 install_ept(pml4, (unsigned long)pci_physaddr, 1395 (unsigned long)pci_physaddr, 0); 1396 ept_sync(INVEPT_SINGLE, eptp); 1397 break; 1398 case 7: 1399 if (!invept_test(0, eptp)) 1400 vmx_inc_test_stage(); 1401 break; 1402 // Should not reach here 1403 default: 1404 report(false, "ERROR - unexpected stage, %d.", 1405 vmx_get_test_stage()); 1406 print_vmexit_info(); 1407 return VMX_TEST_VMEXIT; 1408 } 1409 vmcs_write(GUEST_RIP, guest_rip + insn_len); 1410 return VMX_TEST_RESUME; 1411 case VMX_EPT_MISCONFIG: 1412 switch (vmx_get_test_stage()) { 1413 case 1: 1414 case 2: 1415 vmx_inc_test_stage(); 1416 install_ept(pml4, (unsigned long)data_page1, 1417 (unsigned long)data_page1, 1418 EPT_RA | EPT_WA | EPT_EA); 1419 ept_sync(INVEPT_SINGLE, eptp); 1420 break; 1421 // Should not reach here 1422 default: 1423 report(false, "ERROR - unexpected stage, %d.", 1424 vmx_get_test_stage()); 1425 print_vmexit_info(); 1426 return VMX_TEST_VMEXIT; 1427 } 1428 return VMX_TEST_RESUME; 1429 case VMX_EPT_VIOLATION: 1430 /* 1431 * Exit-qualifications are masked not to account for advanced 1432 * VM-exit information. Once KVM supports this feature, this 1433 * masking should be removed. 1434 */ 1435 exit_qual &= ~EPT_VLT_GUEST_MASK; 1436 1437 switch(vmx_get_test_stage()) { 1438 case 3: 1439 check_ept_ad(pml4, guest_cr3, (unsigned long)data_page1, 0, 1440 have_ad ? EPT_ACCESS_FLAG | EPT_DIRTY_FLAG : 0); 1441 clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page1); 1442 if (exit_qual == (EPT_VLT_WR | EPT_VLT_LADDR_VLD | 1443 EPT_VLT_PADDR)) 1444 vmx_inc_test_stage(); 1445 set_ept_pte(pml4, (unsigned long)data_page1, 1446 1, data_page1_pte | (EPT_PRESENT)); 1447 ept_sync(INVEPT_SINGLE, eptp); 1448 break; 1449 case 4: 1450 check_ept_ad(pml4, guest_cr3, (unsigned long)data_page1, 0, 1451 have_ad ? EPT_ACCESS_FLAG | EPT_DIRTY_FLAG : 0); 1452 clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page1); 1453 if (exit_qual == (EPT_VLT_RD | 1454 (have_ad ? EPT_VLT_WR : 0) | 1455 EPT_VLT_LADDR_VLD)) 1456 vmx_inc_test_stage(); 1457 set_ept_pte(pml4, data_page1_pte, 2, 1458 data_page1_pte_pte | (EPT_PRESENT)); 1459 ept_sync(INVEPT_SINGLE, eptp); 1460 break; 1461 case 5: 1462 if (exit_qual & EPT_VLT_RD) 1463 vmx_inc_test_stage(); 1464 TEST_ASSERT(get_ept_pte(pml4, (unsigned long)pci_physaddr, 1465 1, &memaddr_pte)); 1466 set_ept_pte(pml4, memaddr_pte, 1, memaddr_pte | EPT_RA); 1467 ept_sync(INVEPT_SINGLE, eptp); 1468 break; 1469 case 6: 1470 if (exit_qual & EPT_VLT_WR) 1471 vmx_inc_test_stage(); 1472 TEST_ASSERT(get_ept_pte(pml4, (unsigned long)pci_physaddr, 1473 1, &memaddr_pte)); 1474 set_ept_pte(pml4, memaddr_pte, 1, memaddr_pte | EPT_RA | EPT_WA); 1475 ept_sync(INVEPT_SINGLE, eptp); 1476 break; 1477 default: 1478 // Should not reach here 1479 report(false, "ERROR : unexpected stage, %d", 1480 vmx_get_test_stage()); 1481 print_vmexit_info(); 1482 return VMX_TEST_VMEXIT; 1483 } 1484 return VMX_TEST_RESUME; 1485 default: 1486 report(false, "Unknown exit reason, %ld", reason); 1487 print_vmexit_info(); 1488 } 1489 return VMX_TEST_VMEXIT; 1490 } 1491 1492 static int ept_exit_handler(void) 1493 { 1494 return ept_exit_handler_common(false); 1495 } 1496 1497 static int eptad_init(struct vmcs *vmcs) 1498 { 1499 int r = ept_init_common(true); 1500 1501 if (r == VMX_TEST_EXIT) 1502 return r; 1503 1504 if ((rdmsr(MSR_IA32_VMX_EPT_VPID_CAP) & EPT_CAP_AD_FLAG) == 0) { 1505 printf("\tEPT A/D bits are not supported"); 1506 return VMX_TEST_EXIT; 1507 } 1508 1509 return r; 1510 } 1511 1512 static int pml_init(struct vmcs *vmcs) 1513 { 1514 u32 ctrl_cpu; 1515 int r = eptad_init(vmcs); 1516 1517 if (r == VMX_TEST_EXIT) 1518 return r; 1519 1520 if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) || 1521 !(ctrl_cpu_rev[1].clr & CPU_PML)) { 1522 printf("\tPML is not supported"); 1523 return VMX_TEST_EXIT; 1524 } 1525 1526 pml_log = alloc_page(); 1527 vmcs_write(PMLADDR, (u64)pml_log); 1528 vmcs_write(GUEST_PML_INDEX, PML_INDEX - 1); 1529 1530 ctrl_cpu = vmcs_read(CPU_EXEC_CTRL1) | CPU_PML; 1531 vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu); 1532 1533 return VMX_TEST_START; 1534 } 1535 1536 static void pml_main(void) 1537 { 1538 int count = 0; 1539 1540 vmx_set_test_stage(0); 1541 *((u32 *)data_page2) = 0x1; 1542 vmcall(); 1543 report(vmx_get_test_stage() == 1, "PML - Dirty GPA Logging"); 1544 1545 while (vmx_get_test_stage() == 1) { 1546 vmcall(); 1547 *((u32 *)data_page2) = 0x1; 1548 if (count++ > PML_INDEX) 1549 break; 1550 } 1551 report(vmx_get_test_stage() == 2, "PML Full Event"); 1552 } 1553 1554 static void eptad_main(void) 1555 { 1556 ept_common(); 1557 } 1558 1559 static int eptad_exit_handler(void) 1560 { 1561 return ept_exit_handler_common(true); 1562 } 1563 1564 static bool invvpid_test(int type, u16 vpid) 1565 { 1566 bool ret, supported; 1567 1568 supported = ept_vpid.val & 1569 (VPID_CAP_INVVPID_ADDR >> INVVPID_ADDR << type); 1570 ret = invvpid(type, vpid, 0); 1571 1572 if (ret == !supported) 1573 return false; 1574 1575 if (!supported) 1576 printf("WARNING: unsupported invvpid passed!\n"); 1577 else 1578 printf("WARNING: invvpid failed!\n"); 1579 1580 return true; 1581 } 1582 1583 static int vpid_init(struct vmcs *vmcs) 1584 { 1585 u32 ctrl_cpu1; 1586 1587 if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) || 1588 !(ctrl_cpu_rev[1].clr & CPU_VPID)) { 1589 printf("\tVPID is not supported"); 1590 return VMX_TEST_EXIT; 1591 } 1592 1593 ctrl_cpu1 = vmcs_read(CPU_EXEC_CTRL1); 1594 ctrl_cpu1 |= CPU_VPID; 1595 vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu1); 1596 return VMX_TEST_START; 1597 } 1598 1599 static void vpid_main(void) 1600 { 1601 vmx_set_test_stage(0); 1602 vmcall(); 1603 report(vmx_get_test_stage() == 1, "INVVPID SINGLE ADDRESS"); 1604 vmx_set_test_stage(2); 1605 vmcall(); 1606 report(vmx_get_test_stage() == 3, "INVVPID SINGLE"); 1607 vmx_set_test_stage(4); 1608 vmcall(); 1609 report(vmx_get_test_stage() == 5, "INVVPID ALL"); 1610 } 1611 1612 static int vpid_exit_handler(void) 1613 { 1614 u64 guest_rip; 1615 ulong reason; 1616 u32 insn_len; 1617 1618 guest_rip = vmcs_read(GUEST_RIP); 1619 reason = vmcs_read(EXI_REASON) & 0xff; 1620 insn_len = vmcs_read(EXI_INST_LEN); 1621 1622 switch (reason) { 1623 case VMX_VMCALL: 1624 switch(vmx_get_test_stage()) { 1625 case 0: 1626 if (!invvpid_test(INVVPID_ADDR, 1)) 1627 vmx_inc_test_stage(); 1628 break; 1629 case 2: 1630 if (!invvpid_test(INVVPID_CONTEXT_GLOBAL, 1)) 1631 vmx_inc_test_stage(); 1632 break; 1633 case 4: 1634 if (!invvpid_test(INVVPID_ALL, 1)) 1635 vmx_inc_test_stage(); 1636 break; 1637 default: 1638 report(false, "ERROR: unexpected stage, %d", 1639 vmx_get_test_stage()); 1640 print_vmexit_info(); 1641 return VMX_TEST_VMEXIT; 1642 } 1643 vmcs_write(GUEST_RIP, guest_rip + insn_len); 1644 return VMX_TEST_RESUME; 1645 default: 1646 report(false, "Unknown exit reason, %ld", reason); 1647 print_vmexit_info(); 1648 } 1649 return VMX_TEST_VMEXIT; 1650 } 1651 1652 #define TIMER_VECTOR 222 1653 1654 static volatile bool timer_fired; 1655 1656 static void timer_isr(isr_regs_t *regs) 1657 { 1658 timer_fired = true; 1659 apic_write(APIC_EOI, 0); 1660 } 1661 1662 static int interrupt_init(struct vmcs *vmcs) 1663 { 1664 msr_bmp_init(); 1665 vmcs_write(PIN_CONTROLS, vmcs_read(PIN_CONTROLS) & ~PIN_EXTINT); 1666 handle_irq(TIMER_VECTOR, timer_isr); 1667 return VMX_TEST_START; 1668 } 1669 1670 static void interrupt_main(void) 1671 { 1672 long long start, loops; 1673 1674 vmx_set_test_stage(0); 1675 1676 apic_write(APIC_LVTT, TIMER_VECTOR); 1677 irq_enable(); 1678 1679 apic_write(APIC_TMICT, 1); 1680 for (loops = 0; loops < 10000000 && !timer_fired; loops++) 1681 asm volatile ("nop"); 1682 report(timer_fired, "direct interrupt while running guest"); 1683 1684 apic_write(APIC_TMICT, 0); 1685 irq_disable(); 1686 vmcall(); 1687 timer_fired = false; 1688 apic_write(APIC_TMICT, 1); 1689 for (loops = 0; loops < 10000000 && !timer_fired; loops++) 1690 asm volatile ("nop"); 1691 report(timer_fired, "intercepted interrupt while running guest"); 1692 1693 irq_enable(); 1694 apic_write(APIC_TMICT, 0); 1695 irq_disable(); 1696 vmcall(); 1697 timer_fired = false; 1698 start = rdtsc(); 1699 apic_write(APIC_TMICT, 1000000); 1700 1701 asm volatile ("sti; hlt"); 1702 1703 report(rdtsc() - start > 1000000 && timer_fired, 1704 "direct interrupt + hlt"); 1705 1706 apic_write(APIC_TMICT, 0); 1707 irq_disable(); 1708 vmcall(); 1709 timer_fired = false; 1710 start = rdtsc(); 1711 apic_write(APIC_TMICT, 1000000); 1712 1713 asm volatile ("sti; hlt"); 1714 1715 report(rdtsc() - start > 10000 && timer_fired, 1716 "intercepted interrupt + hlt"); 1717 1718 apic_write(APIC_TMICT, 0); 1719 irq_disable(); 1720 vmcall(); 1721 timer_fired = false; 1722 start = rdtsc(); 1723 apic_write(APIC_TMICT, 1000000); 1724 1725 irq_enable(); 1726 asm volatile ("nop"); 1727 vmcall(); 1728 1729 report(rdtsc() - start > 10000 && timer_fired, 1730 "direct interrupt + activity state hlt"); 1731 1732 apic_write(APIC_TMICT, 0); 1733 irq_disable(); 1734 vmcall(); 1735 timer_fired = false; 1736 start = rdtsc(); 1737 apic_write(APIC_TMICT, 1000000); 1738 1739 irq_enable(); 1740 asm volatile ("nop"); 1741 vmcall(); 1742 1743 report(rdtsc() - start > 10000 && timer_fired, 1744 "intercepted interrupt + activity state hlt"); 1745 1746 apic_write(APIC_TMICT, 0); 1747 irq_disable(); 1748 vmx_set_test_stage(7); 1749 vmcall(); 1750 timer_fired = false; 1751 apic_write(APIC_TMICT, 1); 1752 for (loops = 0; loops < 10000000 && !timer_fired; loops++) 1753 asm volatile ("nop"); 1754 report(timer_fired, 1755 "running a guest with interrupt acknowledgement set"); 1756 1757 apic_write(APIC_TMICT, 0); 1758 irq_enable(); 1759 timer_fired = false; 1760 vmcall(); 1761 report(timer_fired, "Inject an event to a halted guest"); 1762 } 1763 1764 static int interrupt_exit_handler(void) 1765 { 1766 u64 guest_rip = vmcs_read(GUEST_RIP); 1767 ulong reason = vmcs_read(EXI_REASON) & 0xff; 1768 u32 insn_len = vmcs_read(EXI_INST_LEN); 1769 1770 switch (reason) { 1771 case VMX_VMCALL: 1772 switch (vmx_get_test_stage()) { 1773 case 0: 1774 case 2: 1775 case 5: 1776 vmcs_write(PIN_CONTROLS, 1777 vmcs_read(PIN_CONTROLS) | PIN_EXTINT); 1778 break; 1779 case 7: 1780 vmcs_write(EXI_CONTROLS, vmcs_read(EXI_CONTROLS) | EXI_INTA); 1781 vmcs_write(PIN_CONTROLS, 1782 vmcs_read(PIN_CONTROLS) | PIN_EXTINT); 1783 break; 1784 case 1: 1785 case 3: 1786 vmcs_write(PIN_CONTROLS, 1787 vmcs_read(PIN_CONTROLS) & ~PIN_EXTINT); 1788 break; 1789 case 4: 1790 case 6: 1791 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 1792 break; 1793 1794 case 8: 1795 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 1796 vmcs_write(ENT_INTR_INFO, 1797 TIMER_VECTOR | 1798 (VMX_INTR_TYPE_EXT_INTR << INTR_INFO_INTR_TYPE_SHIFT) | 1799 INTR_INFO_VALID_MASK); 1800 break; 1801 } 1802 vmx_inc_test_stage(); 1803 vmcs_write(GUEST_RIP, guest_rip + insn_len); 1804 return VMX_TEST_RESUME; 1805 case VMX_EXTINT: 1806 if (vmcs_read(EXI_CONTROLS) & EXI_INTA) { 1807 int vector = vmcs_read(EXI_INTR_INFO) & 0xff; 1808 handle_external_interrupt(vector); 1809 } else { 1810 irq_enable(); 1811 asm volatile ("nop"); 1812 irq_disable(); 1813 } 1814 if (vmx_get_test_stage() >= 2) 1815 vmcs_write(GUEST_ACTV_STATE, ACTV_ACTIVE); 1816 return VMX_TEST_RESUME; 1817 default: 1818 report(false, "Unknown exit reason, %ld", reason); 1819 print_vmexit_info(); 1820 } 1821 1822 return VMX_TEST_VMEXIT; 1823 } 1824 1825 static int dbgctls_init(struct vmcs *vmcs) 1826 { 1827 u64 dr7 = 0x402; 1828 u64 zero = 0; 1829 1830 msr_bmp_init(); 1831 asm volatile( 1832 "mov %0,%%dr0\n\t" 1833 "mov %0,%%dr1\n\t" 1834 "mov %0,%%dr2\n\t" 1835 "mov %1,%%dr7\n\t" 1836 : : "r" (zero), "r" (dr7)); 1837 wrmsr(MSR_IA32_DEBUGCTLMSR, 0x1); 1838 vmcs_write(GUEST_DR7, 0x404); 1839 vmcs_write(GUEST_DEBUGCTL, 0x2); 1840 1841 vmcs_write(ENT_CONTROLS, vmcs_read(ENT_CONTROLS) | ENT_LOAD_DBGCTLS); 1842 vmcs_write(EXI_CONTROLS, vmcs_read(EXI_CONTROLS) | EXI_SAVE_DBGCTLS); 1843 1844 return VMX_TEST_START; 1845 } 1846 1847 static void dbgctls_main(void) 1848 { 1849 u64 dr7, debugctl; 1850 1851 asm volatile("mov %%dr7,%0" : "=r" (dr7)); 1852 debugctl = rdmsr(MSR_IA32_DEBUGCTLMSR); 1853 /* Commented out: KVM does not support DEBUGCTL so far */ 1854 (void)debugctl; 1855 report(dr7 == 0x404, "Load debug controls" /* && debugctl == 0x2 */); 1856 1857 dr7 = 0x408; 1858 asm volatile("mov %0,%%dr7" : : "r" (dr7)); 1859 wrmsr(MSR_IA32_DEBUGCTLMSR, 0x3); 1860 1861 vmx_set_test_stage(0); 1862 vmcall(); 1863 report(vmx_get_test_stage() == 1, "Save debug controls"); 1864 1865 if (ctrl_enter_rev.set & ENT_LOAD_DBGCTLS || 1866 ctrl_exit_rev.set & EXI_SAVE_DBGCTLS) { 1867 printf("\tDebug controls are always loaded/saved\n"); 1868 return; 1869 } 1870 vmx_set_test_stage(2); 1871 vmcall(); 1872 1873 asm volatile("mov %%dr7,%0" : "=r" (dr7)); 1874 debugctl = rdmsr(MSR_IA32_DEBUGCTLMSR); 1875 /* Commented out: KVM does not support DEBUGCTL so far */ 1876 (void)debugctl; 1877 report(dr7 == 0x402, 1878 "Guest=host debug controls" /* && debugctl == 0x1 */); 1879 1880 dr7 = 0x408; 1881 asm volatile("mov %0,%%dr7" : : "r" (dr7)); 1882 wrmsr(MSR_IA32_DEBUGCTLMSR, 0x3); 1883 1884 vmx_set_test_stage(3); 1885 vmcall(); 1886 report(vmx_get_test_stage() == 4, "Don't save debug controls"); 1887 } 1888 1889 static int dbgctls_exit_handler(void) 1890 { 1891 unsigned int reason = vmcs_read(EXI_REASON) & 0xff; 1892 u32 insn_len = vmcs_read(EXI_INST_LEN); 1893 u64 guest_rip = vmcs_read(GUEST_RIP); 1894 u64 dr7, debugctl; 1895 1896 asm volatile("mov %%dr7,%0" : "=r" (dr7)); 1897 debugctl = rdmsr(MSR_IA32_DEBUGCTLMSR); 1898 1899 switch (reason) { 1900 case VMX_VMCALL: 1901 switch (vmx_get_test_stage()) { 1902 case 0: 1903 if (dr7 == 0x400 && debugctl == 0 && 1904 vmcs_read(GUEST_DR7) == 0x408 /* && 1905 Commented out: KVM does not support DEBUGCTL so far 1906 vmcs_read(GUEST_DEBUGCTL) == 0x3 */) 1907 vmx_inc_test_stage(); 1908 break; 1909 case 2: 1910 dr7 = 0x402; 1911 asm volatile("mov %0,%%dr7" : : "r" (dr7)); 1912 wrmsr(MSR_IA32_DEBUGCTLMSR, 0x1); 1913 vmcs_write(GUEST_DR7, 0x404); 1914 vmcs_write(GUEST_DEBUGCTL, 0x2); 1915 1916 vmcs_write(ENT_CONTROLS, 1917 vmcs_read(ENT_CONTROLS) & ~ENT_LOAD_DBGCTLS); 1918 vmcs_write(EXI_CONTROLS, 1919 vmcs_read(EXI_CONTROLS) & ~EXI_SAVE_DBGCTLS); 1920 break; 1921 case 3: 1922 if (dr7 == 0x400 && debugctl == 0 && 1923 vmcs_read(GUEST_DR7) == 0x404 /* && 1924 Commented out: KVM does not support DEBUGCTL so far 1925 vmcs_read(GUEST_DEBUGCTL) == 0x2 */) 1926 vmx_inc_test_stage(); 1927 break; 1928 } 1929 vmcs_write(GUEST_RIP, guest_rip + insn_len); 1930 return VMX_TEST_RESUME; 1931 default: 1932 report(false, "Unknown exit reason, %d", reason); 1933 print_vmexit_info(); 1934 } 1935 return VMX_TEST_VMEXIT; 1936 } 1937 1938 struct vmx_msr_entry { 1939 u32 index; 1940 u32 reserved; 1941 u64 value; 1942 } __attribute__((packed)); 1943 1944 #define MSR_MAGIC 0x31415926 1945 struct vmx_msr_entry *exit_msr_store, *entry_msr_load, *exit_msr_load; 1946 1947 static int msr_switch_init(struct vmcs *vmcs) 1948 { 1949 msr_bmp_init(); 1950 exit_msr_store = alloc_page(); 1951 exit_msr_load = alloc_page(); 1952 entry_msr_load = alloc_page(); 1953 entry_msr_load[0].index = MSR_KERNEL_GS_BASE; 1954 entry_msr_load[0].value = MSR_MAGIC; 1955 1956 vmx_set_test_stage(1); 1957 vmcs_write(ENT_MSR_LD_CNT, 1); 1958 vmcs_write(ENTER_MSR_LD_ADDR, (u64)entry_msr_load); 1959 vmcs_write(EXI_MSR_ST_CNT, 1); 1960 vmcs_write(EXIT_MSR_ST_ADDR, (u64)exit_msr_store); 1961 vmcs_write(EXI_MSR_LD_CNT, 1); 1962 vmcs_write(EXIT_MSR_LD_ADDR, (u64)exit_msr_load); 1963 return VMX_TEST_START; 1964 } 1965 1966 static void msr_switch_main(void) 1967 { 1968 if (vmx_get_test_stage() == 1) { 1969 report(rdmsr(MSR_KERNEL_GS_BASE) == MSR_MAGIC, 1970 "VM entry MSR load"); 1971 vmx_set_test_stage(2); 1972 wrmsr(MSR_KERNEL_GS_BASE, MSR_MAGIC + 1); 1973 exit_msr_store[0].index = MSR_KERNEL_GS_BASE; 1974 exit_msr_load[0].index = MSR_KERNEL_GS_BASE; 1975 exit_msr_load[0].value = MSR_MAGIC + 2; 1976 } 1977 vmcall(); 1978 } 1979 1980 static int msr_switch_exit_handler(void) 1981 { 1982 ulong reason; 1983 1984 reason = vmcs_read(EXI_REASON); 1985 if (reason == VMX_VMCALL && vmx_get_test_stage() == 2) { 1986 report(exit_msr_store[0].value == MSR_MAGIC + 1, 1987 "VM exit MSR store"); 1988 report(rdmsr(MSR_KERNEL_GS_BASE) == MSR_MAGIC + 2, 1989 "VM exit MSR load"); 1990 vmx_set_test_stage(3); 1991 entry_msr_load[0].index = MSR_FS_BASE; 1992 return VMX_TEST_RESUME; 1993 } 1994 printf("ERROR %s: unexpected stage=%u or reason=%lu\n", 1995 __func__, vmx_get_test_stage(), reason); 1996 return VMX_TEST_EXIT; 1997 } 1998 1999 static int msr_switch_entry_failure(struct vmentry_failure *failure) 2000 { 2001 ulong reason; 2002 2003 if (failure->early) { 2004 printf("ERROR %s: early exit\n", __func__); 2005 return VMX_TEST_EXIT; 2006 } 2007 2008 reason = vmcs_read(EXI_REASON); 2009 if (reason == (VMX_ENTRY_FAILURE | VMX_FAIL_MSR) && 2010 vmx_get_test_stage() == 3) { 2011 report(vmcs_read(EXI_QUALIFICATION) == 1, 2012 "VM entry MSR load: try to load FS_BASE"); 2013 return VMX_TEST_VMEXIT; 2014 } 2015 printf("ERROR %s: unexpected stage=%u or reason=%lu\n", 2016 __func__, vmx_get_test_stage(), reason); 2017 return VMX_TEST_EXIT; 2018 } 2019 2020 static int vmmcall_init(struct vmcs *vmcs) 2021 { 2022 vmcs_write(EXC_BITMAP, 1 << UD_VECTOR); 2023 return VMX_TEST_START; 2024 } 2025 2026 static void vmmcall_main(void) 2027 { 2028 asm volatile( 2029 "mov $0xABCD, %%rax\n\t" 2030 "vmmcall\n\t" 2031 ::: "rax"); 2032 2033 report(0, "VMMCALL"); 2034 } 2035 2036 static int vmmcall_exit_handler(void) 2037 { 2038 ulong reason; 2039 2040 reason = vmcs_read(EXI_REASON); 2041 switch (reason) { 2042 case VMX_VMCALL: 2043 printf("here\n"); 2044 report(0, "VMMCALL triggers #UD"); 2045 break; 2046 case VMX_EXC_NMI: 2047 report((vmcs_read(EXI_INTR_INFO) & 0xff) == UD_VECTOR, 2048 "VMMCALL triggers #UD"); 2049 break; 2050 default: 2051 report(false, "Unknown exit reason, %ld", reason); 2052 print_vmexit_info(); 2053 } 2054 2055 return VMX_TEST_VMEXIT; 2056 } 2057 2058 static int disable_rdtscp_init(struct vmcs *vmcs) 2059 { 2060 u32 ctrl_cpu1; 2061 2062 if (ctrl_cpu_rev[0].clr & CPU_SECONDARY) { 2063 ctrl_cpu1 = vmcs_read(CPU_EXEC_CTRL1); 2064 ctrl_cpu1 &= ~CPU_RDTSCP; 2065 vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu1); 2066 } 2067 2068 return VMX_TEST_START; 2069 } 2070 2071 static void disable_rdtscp_ud_handler(struct ex_regs *regs) 2072 { 2073 switch (vmx_get_test_stage()) { 2074 case 0: 2075 report(true, "RDTSCP triggers #UD"); 2076 vmx_inc_test_stage(); 2077 regs->rip += 3; 2078 break; 2079 case 2: 2080 report(true, "RDPID triggers #UD"); 2081 vmx_inc_test_stage(); 2082 regs->rip += 4; 2083 break; 2084 } 2085 return; 2086 2087 } 2088 2089 static void disable_rdtscp_main(void) 2090 { 2091 /* Test that #UD is properly injected in L2. */ 2092 handle_exception(UD_VECTOR, disable_rdtscp_ud_handler); 2093 2094 vmx_set_test_stage(0); 2095 asm volatile("rdtscp" : : : "eax", "ecx", "edx"); 2096 vmcall(); 2097 asm volatile(".byte 0xf3, 0x0f, 0xc7, 0xf8" : : : "eax"); 2098 2099 handle_exception(UD_VECTOR, 0); 2100 vmcall(); 2101 } 2102 2103 static int disable_rdtscp_exit_handler(void) 2104 { 2105 unsigned int reason = vmcs_read(EXI_REASON) & 0xff; 2106 2107 switch (reason) { 2108 case VMX_VMCALL: 2109 switch (vmx_get_test_stage()) { 2110 case 0: 2111 report(false, "RDTSCP triggers #UD"); 2112 vmx_inc_test_stage(); 2113 /* fallthrough */ 2114 case 1: 2115 vmx_inc_test_stage(); 2116 vmcs_write(GUEST_RIP, vmcs_read(GUEST_RIP) + 3); 2117 return VMX_TEST_RESUME; 2118 case 2: 2119 report(false, "RDPID triggers #UD"); 2120 break; 2121 } 2122 break; 2123 2124 default: 2125 report(false, "Unknown exit reason, %d", reason); 2126 print_vmexit_info(); 2127 } 2128 return VMX_TEST_VMEXIT; 2129 } 2130 2131 static int int3_init(struct vmcs *vmcs) 2132 { 2133 vmcs_write(EXC_BITMAP, ~0u); 2134 return VMX_TEST_START; 2135 } 2136 2137 static void int3_guest_main(void) 2138 { 2139 asm volatile ("int3"); 2140 } 2141 2142 static int int3_exit_handler(void) 2143 { 2144 u32 reason = vmcs_read(EXI_REASON); 2145 u32 intr_info = vmcs_read(EXI_INTR_INFO); 2146 2147 report(reason == VMX_EXC_NMI && (intr_info & INTR_INFO_VALID_MASK) && 2148 (intr_info & INTR_INFO_VECTOR_MASK) == BP_VECTOR && 2149 ((intr_info & INTR_INFO_INTR_TYPE_MASK) >> 2150 INTR_INFO_INTR_TYPE_SHIFT) == VMX_INTR_TYPE_SOFT_EXCEPTION, 2151 "L1 intercepts #BP"); 2152 2153 return VMX_TEST_VMEXIT; 2154 } 2155 2156 static int into_init(struct vmcs *vmcs) 2157 { 2158 vmcs_write(EXC_BITMAP, ~0u); 2159 return VMX_TEST_START; 2160 } 2161 2162 static void into_guest_main(void) 2163 { 2164 struct far_pointer32 fp = { 2165 .offset = (uintptr_t)&&into, 2166 .selector = KERNEL_CS32, 2167 }; 2168 register uintptr_t rsp asm("rsp"); 2169 2170 if (fp.offset != (uintptr_t)&&into) { 2171 printf("Code address too high.\n"); 2172 return; 2173 } 2174 if ((u32)rsp != rsp) { 2175 printf("Stack address too high.\n"); 2176 return; 2177 } 2178 2179 asm goto ("lcall *%0" : : "m" (fp) : "rax" : into); 2180 return; 2181 into: 2182 asm volatile (".code32;" 2183 "movl $0x7fffffff, %eax;" 2184 "addl %eax, %eax;" 2185 "into;" 2186 "lret;" 2187 ".code64"); 2188 __builtin_unreachable(); 2189 } 2190 2191 static int into_exit_handler(void) 2192 { 2193 u32 reason = vmcs_read(EXI_REASON); 2194 u32 intr_info = vmcs_read(EXI_INTR_INFO); 2195 2196 report(reason == VMX_EXC_NMI && (intr_info & INTR_INFO_VALID_MASK) && 2197 (intr_info & INTR_INFO_VECTOR_MASK) == OF_VECTOR && 2198 ((intr_info & INTR_INFO_INTR_TYPE_MASK) >> 2199 INTR_INFO_INTR_TYPE_SHIFT) == VMX_INTR_TYPE_SOFT_EXCEPTION, 2200 "L1 intercepts #OF"); 2201 2202 return VMX_TEST_VMEXIT; 2203 } 2204 2205 static void exit_monitor_from_l2_main(void) 2206 { 2207 printf("Calling exit(0) from l2...\n"); 2208 exit(0); 2209 } 2210 2211 static int exit_monitor_from_l2_handler(void) 2212 { 2213 report(false, "The guest should have killed the VMM"); 2214 return VMX_TEST_EXIT; 2215 } 2216 2217 static void assert_exit_reason(u64 expected) 2218 { 2219 u64 actual = vmcs_read(EXI_REASON); 2220 2221 TEST_ASSERT_EQ_MSG(expected, actual, "Expected %s, got %s.", 2222 exit_reason_description(expected), 2223 exit_reason_description(actual)); 2224 } 2225 2226 static void skip_exit_insn(void) 2227 { 2228 u64 guest_rip = vmcs_read(GUEST_RIP); 2229 u32 insn_len = vmcs_read(EXI_INST_LEN); 2230 vmcs_write(GUEST_RIP, guest_rip + insn_len); 2231 } 2232 2233 static void skip_exit_vmcall(void) 2234 { 2235 assert_exit_reason(VMX_VMCALL); 2236 skip_exit_insn(); 2237 } 2238 2239 static void v2_null_test_guest(void) 2240 { 2241 } 2242 2243 static void v2_null_test(void) 2244 { 2245 test_set_guest(v2_null_test_guest); 2246 enter_guest(); 2247 report(1, __func__); 2248 } 2249 2250 static void v2_multiple_entries_test_guest(void) 2251 { 2252 vmx_set_test_stage(1); 2253 vmcall(); 2254 vmx_set_test_stage(2); 2255 } 2256 2257 static void v2_multiple_entries_test(void) 2258 { 2259 test_set_guest(v2_multiple_entries_test_guest); 2260 enter_guest(); 2261 TEST_ASSERT_EQ(vmx_get_test_stage(), 1); 2262 skip_exit_vmcall(); 2263 enter_guest(); 2264 TEST_ASSERT_EQ(vmx_get_test_stage(), 2); 2265 report(1, __func__); 2266 } 2267 2268 static int fixture_test_data = 1; 2269 2270 static void fixture_test_teardown(void *data) 2271 { 2272 *((int *) data) = 1; 2273 } 2274 2275 static void fixture_test_guest(void) 2276 { 2277 fixture_test_data++; 2278 } 2279 2280 2281 static void fixture_test_setup(void) 2282 { 2283 TEST_ASSERT_EQ_MSG(1, fixture_test_data, 2284 "fixture_test_teardown didn't run?!"); 2285 fixture_test_data = 2; 2286 test_add_teardown(fixture_test_teardown, &fixture_test_data); 2287 test_set_guest(fixture_test_guest); 2288 } 2289 2290 static void fixture_test_case1(void) 2291 { 2292 fixture_test_setup(); 2293 TEST_ASSERT_EQ(2, fixture_test_data); 2294 enter_guest(); 2295 TEST_ASSERT_EQ(3, fixture_test_data); 2296 report(1, __func__); 2297 } 2298 2299 static void fixture_test_case2(void) 2300 { 2301 fixture_test_setup(); 2302 TEST_ASSERT_EQ(2, fixture_test_data); 2303 enter_guest(); 2304 TEST_ASSERT_EQ(3, fixture_test_data); 2305 report(1, __func__); 2306 } 2307 2308 enum ept_access_op { 2309 OP_READ, 2310 OP_WRITE, 2311 OP_EXEC, 2312 OP_FLUSH_TLB, 2313 OP_EXIT, 2314 }; 2315 2316 static struct ept_access_test_data { 2317 unsigned long gpa; 2318 unsigned long *gva; 2319 unsigned long hpa; 2320 unsigned long *hva; 2321 enum ept_access_op op; 2322 } ept_access_test_data; 2323 2324 extern unsigned char ret42_start; 2325 extern unsigned char ret42_end; 2326 2327 /* Returns 42. */ 2328 asm( 2329 ".align 64\n" 2330 "ret42_start:\n" 2331 "mov $42, %eax\n" 2332 "ret\n" 2333 "ret42_end:\n" 2334 ); 2335 2336 static void 2337 diagnose_ept_violation_qual(u64 expected, u64 actual) 2338 { 2339 2340 #define DIAGNOSE(flag) \ 2341 do { \ 2342 if ((expected & flag) != (actual & flag)) \ 2343 printf(#flag " %sexpected\n", \ 2344 (expected & flag) ? "" : "un"); \ 2345 } while (0) 2346 2347 DIAGNOSE(EPT_VLT_RD); 2348 DIAGNOSE(EPT_VLT_WR); 2349 DIAGNOSE(EPT_VLT_FETCH); 2350 DIAGNOSE(EPT_VLT_PERM_RD); 2351 DIAGNOSE(EPT_VLT_PERM_WR); 2352 DIAGNOSE(EPT_VLT_PERM_EX); 2353 DIAGNOSE(EPT_VLT_LADDR_VLD); 2354 DIAGNOSE(EPT_VLT_PADDR); 2355 2356 #undef DIAGNOSE 2357 } 2358 2359 static void do_ept_access_op(enum ept_access_op op) 2360 { 2361 ept_access_test_data.op = op; 2362 enter_guest(); 2363 } 2364 2365 /* 2366 * Force the guest to flush its TLB (i.e., flush gva -> gpa mappings). Only 2367 * needed by tests that modify guest PTEs. 2368 */ 2369 static void ept_access_test_guest_flush_tlb(void) 2370 { 2371 do_ept_access_op(OP_FLUSH_TLB); 2372 skip_exit_vmcall(); 2373 } 2374 2375 /* 2376 * Modifies the EPT entry at @level in the mapping of @gpa. First clears the 2377 * bits in @clear then sets the bits in @set. @mkhuge transforms the entry into 2378 * a huge page. 2379 */ 2380 static unsigned long ept_twiddle(unsigned long gpa, bool mkhuge, int level, 2381 unsigned long clear, unsigned long set) 2382 { 2383 struct ept_access_test_data *data = &ept_access_test_data; 2384 unsigned long orig_pte; 2385 unsigned long pte; 2386 2387 /* Screw with the mapping at the requested level. */ 2388 TEST_ASSERT(get_ept_pte(pml4, gpa, level, &orig_pte)); 2389 pte = orig_pte; 2390 if (mkhuge) 2391 pte = (orig_pte & ~EPT_ADDR_MASK) | data->hpa | EPT_LARGE_PAGE; 2392 else 2393 pte = orig_pte; 2394 pte = (pte & ~clear) | set; 2395 set_ept_pte(pml4, gpa, level, pte); 2396 ept_sync(INVEPT_SINGLE, eptp); 2397 2398 return orig_pte; 2399 } 2400 2401 static void ept_untwiddle(unsigned long gpa, int level, unsigned long orig_pte) 2402 { 2403 set_ept_pte(pml4, gpa, level, orig_pte); 2404 ept_sync(INVEPT_SINGLE, eptp); 2405 } 2406 2407 static void do_ept_violation(bool leaf, enum ept_access_op op, 2408 u64 expected_qual, u64 expected_paddr) 2409 { 2410 u64 qual; 2411 2412 /* Try the access and observe the violation. */ 2413 do_ept_access_op(op); 2414 2415 assert_exit_reason(VMX_EPT_VIOLATION); 2416 2417 qual = vmcs_read(EXI_QUALIFICATION); 2418 2419 /* Mask undefined bits (which may later be defined in certain cases). */ 2420 qual &= ~(EPT_VLT_GUEST_USER | EPT_VLT_GUEST_RW | EPT_VLT_GUEST_EX | 2421 EPT_VLT_PERM_USER_EX); 2422 2423 diagnose_ept_violation_qual(expected_qual, qual); 2424 TEST_EXPECT_EQ(expected_qual, qual); 2425 2426 #if 0 2427 /* Disable for now otherwise every test will fail */ 2428 TEST_EXPECT_EQ(vmcs_read(GUEST_LINEAR_ADDRESS), 2429 (unsigned long) ( 2430 op == OP_EXEC ? data->gva + 1 : data->gva)); 2431 #endif 2432 /* 2433 * TODO: tests that probe expected_paddr in pages other than the one at 2434 * the beginning of the 1g region. 2435 */ 2436 TEST_EXPECT_EQ(vmcs_read(INFO_PHYS_ADDR), expected_paddr); 2437 } 2438 2439 static void 2440 ept_violation_at_level_mkhuge(bool mkhuge, int level, unsigned long clear, 2441 unsigned long set, enum ept_access_op op, 2442 u64 expected_qual) 2443 { 2444 struct ept_access_test_data *data = &ept_access_test_data; 2445 unsigned long orig_pte; 2446 2447 orig_pte = ept_twiddle(data->gpa, mkhuge, level, clear, set); 2448 2449 do_ept_violation(level == 1 || mkhuge, op, expected_qual, 2450 op == OP_EXEC ? data->gpa + sizeof(unsigned long) : 2451 data->gpa); 2452 2453 /* Fix the violation and resume the op loop. */ 2454 ept_untwiddle(data->gpa, level, orig_pte); 2455 enter_guest(); 2456 skip_exit_vmcall(); 2457 } 2458 2459 static void 2460 ept_violation_at_level(int level, unsigned long clear, unsigned long set, 2461 enum ept_access_op op, u64 expected_qual) 2462 { 2463 ept_violation_at_level_mkhuge(false, level, clear, set, op, 2464 expected_qual); 2465 if (ept_huge_pages_supported(level)) 2466 ept_violation_at_level_mkhuge(true, level, clear, set, op, 2467 expected_qual); 2468 } 2469 2470 static void ept_violation(unsigned long clear, unsigned long set, 2471 enum ept_access_op op, u64 expected_qual) 2472 { 2473 ept_violation_at_level(1, clear, set, op, expected_qual); 2474 ept_violation_at_level(2, clear, set, op, expected_qual); 2475 ept_violation_at_level(3, clear, set, op, expected_qual); 2476 ept_violation_at_level(4, clear, set, op, expected_qual); 2477 } 2478 2479 static void ept_access_violation(unsigned long access, enum ept_access_op op, 2480 u64 expected_qual) 2481 { 2482 ept_violation(EPT_PRESENT, access, op, 2483 expected_qual | EPT_VLT_LADDR_VLD | EPT_VLT_PADDR); 2484 } 2485 2486 /* 2487 * For translations that don't involve a GVA, that is physical address (paddr) 2488 * accesses, EPT violations don't set the flag EPT_VLT_PADDR. For a typical 2489 * guest memory access, the hardware does GVA -> GPA -> HPA. However, certain 2490 * translations don't involve GVAs, such as when the hardware does the guest 2491 * page table walk. For example, in translating GVA_1 -> GPA_1, the guest MMU 2492 * might try to set an A bit on a guest PTE. If the GPA_2 that the PTE resides 2493 * on isn't present in the EPT, then the EPT violation will be for GPA_2 and 2494 * the EPT_VLT_PADDR bit will be clear in the exit qualification. 2495 * 2496 * Note that paddr violations can also be triggered by loading PAE page tables 2497 * with wonky addresses. We don't test that yet. 2498 * 2499 * This function modifies the EPT entry that maps the GPA that the guest page 2500 * table entry mapping ept_access_test_data.gva resides on. 2501 * 2502 * @ept_access EPT permissions to set. Other permissions are cleared. 2503 * 2504 * @pte_ad Set the A/D bits on the guest PTE accordingly. 2505 * 2506 * @op Guest operation to perform with 2507 * ept_access_test_data.gva. 2508 * 2509 * @expect_violation 2510 * Is a violation expected during the paddr access? 2511 * 2512 * @expected_qual Expected qualification for the EPT violation. 2513 * EPT_VLT_PADDR should be clear. 2514 */ 2515 static void ept_access_paddr(unsigned long ept_access, unsigned long pte_ad, 2516 enum ept_access_op op, bool expect_violation, 2517 u64 expected_qual) 2518 { 2519 struct ept_access_test_data *data = &ept_access_test_data; 2520 unsigned long *ptep; 2521 unsigned long gpa; 2522 unsigned long orig_epte; 2523 unsigned long epte; 2524 int i; 2525 2526 /* Modify the guest PTE mapping data->gva according to @pte_ad. */ 2527 ptep = get_pte_level(current_page_table(), data->gva, /*level=*/1); 2528 TEST_ASSERT(ptep); 2529 TEST_ASSERT_EQ(*ptep & PT_ADDR_MASK, data->gpa); 2530 *ptep = (*ptep & ~PT_AD_MASK) | pte_ad; 2531 ept_access_test_guest_flush_tlb(); 2532 2533 /* 2534 * Now modify the access bits on the EPT entry for the GPA that the 2535 * guest PTE resides on. Note that by modifying a single EPT entry, 2536 * we're potentially affecting 512 guest PTEs. However, we've carefully 2537 * constructed our test such that those other 511 PTEs aren't used by 2538 * the guest: data->gva is at the beginning of a 1G huge page, thus the 2539 * PTE we're modifying is at the beginning of a 4K page and the 2540 * following 511 entires are also under our control (and not touched by 2541 * the guest). 2542 */ 2543 gpa = virt_to_phys(ptep); 2544 TEST_ASSERT_EQ(gpa & ~PAGE_MASK, 0); 2545 /* 2546 * Make sure the guest page table page is mapped with a 4K EPT entry, 2547 * otherwise our level=1 twiddling below will fail. We use the 2548 * identity map (gpa = gpa) since page tables are shared with the host. 2549 */ 2550 install_ept(pml4, gpa, gpa, EPT_PRESENT); 2551 orig_epte = ept_twiddle(gpa, /*mkhuge=*/0, /*level=*/1, 2552 /*clear=*/EPT_PRESENT, /*set=*/ept_access); 2553 2554 if (expect_violation) { 2555 do_ept_violation(/*leaf=*/true, op, 2556 expected_qual | EPT_VLT_LADDR_VLD, gpa); 2557 ept_untwiddle(gpa, /*level=*/1, orig_epte); 2558 do_ept_access_op(op); 2559 } else { 2560 do_ept_access_op(op); 2561 if (ept_ad_enabled()) { 2562 for (i = EPT_PAGE_LEVEL; i > 0; i--) { 2563 TEST_ASSERT(get_ept_pte(pml4, gpa, i, &epte)); 2564 TEST_ASSERT(epte & EPT_ACCESS_FLAG); 2565 if (i == 1) 2566 TEST_ASSERT(epte & EPT_DIRTY_FLAG); 2567 else 2568 TEST_ASSERT_EQ(epte & EPT_DIRTY_FLAG, 0); 2569 } 2570 } 2571 2572 ept_untwiddle(gpa, /*level=*/1, orig_epte); 2573 } 2574 2575 TEST_ASSERT(*ptep & PT_ACCESSED_MASK); 2576 if ((pte_ad & PT_DIRTY_MASK) || op == OP_WRITE) 2577 TEST_ASSERT(*ptep & PT_DIRTY_MASK); 2578 2579 skip_exit_vmcall(); 2580 } 2581 2582 static void ept_access_allowed_paddr(unsigned long ept_access, 2583 unsigned long pte_ad, 2584 enum ept_access_op op) 2585 { 2586 ept_access_paddr(ept_access, pte_ad, op, /*expect_violation=*/false, 2587 /*expected_qual=*/-1); 2588 } 2589 2590 static void ept_access_violation_paddr(unsigned long ept_access, 2591 unsigned long pte_ad, 2592 enum ept_access_op op, 2593 u64 expected_qual) 2594 { 2595 ept_access_paddr(ept_access, pte_ad, op, /*expect_violation=*/true, 2596 expected_qual); 2597 } 2598 2599 2600 static void ept_allowed_at_level_mkhuge(bool mkhuge, int level, 2601 unsigned long clear, 2602 unsigned long set, 2603 enum ept_access_op op) 2604 { 2605 struct ept_access_test_data *data = &ept_access_test_data; 2606 unsigned long orig_pte; 2607 2608 orig_pte = ept_twiddle(data->gpa, mkhuge, level, clear, set); 2609 2610 /* No violation. Should proceed to vmcall. */ 2611 do_ept_access_op(op); 2612 skip_exit_vmcall(); 2613 2614 ept_untwiddle(data->gpa, level, orig_pte); 2615 } 2616 2617 static void ept_allowed_at_level(int level, unsigned long clear, 2618 unsigned long set, enum ept_access_op op) 2619 { 2620 ept_allowed_at_level_mkhuge(false, level, clear, set, op); 2621 if (ept_huge_pages_supported(level)) 2622 ept_allowed_at_level_mkhuge(true, level, clear, set, op); 2623 } 2624 2625 static void ept_allowed(unsigned long clear, unsigned long set, 2626 enum ept_access_op op) 2627 { 2628 ept_allowed_at_level(1, clear, set, op); 2629 ept_allowed_at_level(2, clear, set, op); 2630 ept_allowed_at_level(3, clear, set, op); 2631 ept_allowed_at_level(4, clear, set, op); 2632 } 2633 2634 static void ept_ignored_bit(int bit) 2635 { 2636 /* Set the bit. */ 2637 ept_allowed(0, 1ul << bit, OP_READ); 2638 ept_allowed(0, 1ul << bit, OP_WRITE); 2639 ept_allowed(0, 1ul << bit, OP_EXEC); 2640 2641 /* Clear the bit. */ 2642 ept_allowed(1ul << bit, 0, OP_READ); 2643 ept_allowed(1ul << bit, 0, OP_WRITE); 2644 ept_allowed(1ul << bit, 0, OP_EXEC); 2645 } 2646 2647 static void ept_access_allowed(unsigned long access, enum ept_access_op op) 2648 { 2649 ept_allowed(EPT_PRESENT, access, op); 2650 } 2651 2652 2653 static void ept_misconfig_at_level_mkhuge_op(bool mkhuge, int level, 2654 unsigned long clear, 2655 unsigned long set, 2656 enum ept_access_op op) 2657 { 2658 struct ept_access_test_data *data = &ept_access_test_data; 2659 unsigned long orig_pte; 2660 2661 orig_pte = ept_twiddle(data->gpa, mkhuge, level, clear, set); 2662 2663 do_ept_access_op(op); 2664 assert_exit_reason(VMX_EPT_MISCONFIG); 2665 2666 /* Intel 27.2.1, "For all other VM exits, this field is cleared." */ 2667 #if 0 2668 /* broken: */ 2669 TEST_EXPECT_EQ_MSG(vmcs_read(EXI_QUALIFICATION), 0); 2670 #endif 2671 #if 0 2672 /* 2673 * broken: 2674 * According to description of exit qual for EPT violation, 2675 * EPT_VLT_LADDR_VLD indicates if GUEST_LINEAR_ADDRESS is valid. 2676 * However, I can't find anything that says GUEST_LINEAR_ADDRESS ought 2677 * to be set for msiconfig. 2678 */ 2679 TEST_EXPECT_EQ(vmcs_read(GUEST_LINEAR_ADDRESS), 2680 (unsigned long) ( 2681 op == OP_EXEC ? data->gva + 1 : data->gva)); 2682 #endif 2683 2684 /* Fix the violation and resume the op loop. */ 2685 ept_untwiddle(data->gpa, level, orig_pte); 2686 enter_guest(); 2687 skip_exit_vmcall(); 2688 } 2689 2690 static void ept_misconfig_at_level_mkhuge(bool mkhuge, int level, 2691 unsigned long clear, 2692 unsigned long set) 2693 { 2694 /* The op shouldn't matter (read, write, exec), so try them all! */ 2695 ept_misconfig_at_level_mkhuge_op(mkhuge, level, clear, set, OP_READ); 2696 ept_misconfig_at_level_mkhuge_op(mkhuge, level, clear, set, OP_WRITE); 2697 ept_misconfig_at_level_mkhuge_op(mkhuge, level, clear, set, OP_EXEC); 2698 } 2699 2700 static void ept_misconfig_at_level(int level, unsigned long clear, 2701 unsigned long set) 2702 { 2703 ept_misconfig_at_level_mkhuge(false, level, clear, set); 2704 if (ept_huge_pages_supported(level)) 2705 ept_misconfig_at_level_mkhuge(true, level, clear, set); 2706 } 2707 2708 static void ept_misconfig(unsigned long clear, unsigned long set) 2709 { 2710 ept_misconfig_at_level(1, clear, set); 2711 ept_misconfig_at_level(2, clear, set); 2712 ept_misconfig_at_level(3, clear, set); 2713 ept_misconfig_at_level(4, clear, set); 2714 } 2715 2716 static void ept_access_misconfig(unsigned long access) 2717 { 2718 ept_misconfig(EPT_PRESENT, access); 2719 } 2720 2721 static void ept_reserved_bit_at_level_nohuge(int level, int bit) 2722 { 2723 /* Setting the bit causes a misconfig. */ 2724 ept_misconfig_at_level_mkhuge(false, level, 0, 1ul << bit); 2725 2726 /* Making the entry non-present turns reserved bits into ignored. */ 2727 ept_violation_at_level(level, EPT_PRESENT, 1ul << bit, OP_READ, 2728 EPT_VLT_RD | EPT_VLT_LADDR_VLD | EPT_VLT_PADDR); 2729 } 2730 2731 static void ept_reserved_bit_at_level_huge(int level, int bit) 2732 { 2733 /* Setting the bit causes a misconfig. */ 2734 ept_misconfig_at_level_mkhuge(true, level, 0, 1ul << bit); 2735 2736 /* Making the entry non-present turns reserved bits into ignored. */ 2737 ept_violation_at_level(level, EPT_PRESENT, 1ul << bit, OP_READ, 2738 EPT_VLT_RD | EPT_VLT_LADDR_VLD | EPT_VLT_PADDR); 2739 } 2740 2741 static void ept_reserved_bit_at_level(int level, int bit) 2742 { 2743 /* Setting the bit causes a misconfig. */ 2744 ept_misconfig_at_level(level, 0, 1ul << bit); 2745 2746 /* Making the entry non-present turns reserved bits into ignored. */ 2747 ept_violation_at_level(level, EPT_PRESENT, 1ul << bit, OP_READ, 2748 EPT_VLT_RD | EPT_VLT_LADDR_VLD | EPT_VLT_PADDR); 2749 } 2750 2751 static void ept_reserved_bit(int bit) 2752 { 2753 ept_reserved_bit_at_level(1, bit); 2754 ept_reserved_bit_at_level(2, bit); 2755 ept_reserved_bit_at_level(3, bit); 2756 ept_reserved_bit_at_level(4, bit); 2757 } 2758 2759 #define PAGE_2M_ORDER 9 2760 #define PAGE_1G_ORDER 18 2761 2762 static void *get_1g_page(void) 2763 { 2764 static void *alloc; 2765 2766 if (!alloc) 2767 alloc = alloc_pages(PAGE_1G_ORDER); 2768 return alloc; 2769 } 2770 2771 static void ept_access_test_teardown(void *unused) 2772 { 2773 /* Exit the guest cleanly. */ 2774 do_ept_access_op(OP_EXIT); 2775 } 2776 2777 static void ept_access_test_guest(void) 2778 { 2779 struct ept_access_test_data *data = &ept_access_test_data; 2780 int (*code)(void) = (int (*)(void)) &data->gva[1]; 2781 2782 while (true) { 2783 switch (data->op) { 2784 case OP_READ: 2785 TEST_ASSERT_EQ(*data->gva, MAGIC_VAL_1); 2786 break; 2787 case OP_WRITE: 2788 *data->gva = MAGIC_VAL_2; 2789 TEST_ASSERT_EQ(*data->gva, MAGIC_VAL_2); 2790 *data->gva = MAGIC_VAL_1; 2791 break; 2792 case OP_EXEC: 2793 TEST_ASSERT_EQ(42, code()); 2794 break; 2795 case OP_FLUSH_TLB: 2796 write_cr3(read_cr3()); 2797 break; 2798 case OP_EXIT: 2799 return; 2800 default: 2801 TEST_ASSERT_MSG(false, "Unknown op %d", data->op); 2802 } 2803 vmcall(); 2804 } 2805 } 2806 2807 static void ept_access_test_setup(void) 2808 { 2809 struct ept_access_test_data *data = &ept_access_test_data; 2810 unsigned long npages = 1ul << PAGE_1G_ORDER; 2811 unsigned long size = npages * PAGE_SIZE; 2812 unsigned long *page_table = current_page_table(); 2813 unsigned long pte; 2814 2815 if (setup_ept(false)) 2816 test_skip("EPT not supported"); 2817 2818 /* We use data->gpa = 1 << 39 so that test data has a separate pml4 entry */ 2819 if (cpuid_maxphyaddr() < 40) 2820 test_skip("Test needs MAXPHYADDR >= 40"); 2821 2822 test_set_guest(ept_access_test_guest); 2823 test_add_teardown(ept_access_test_teardown, NULL); 2824 2825 data->hva = get_1g_page(); 2826 TEST_ASSERT(data->hva); 2827 data->hpa = virt_to_phys(data->hva); 2828 2829 data->gpa = 1ul << 39; 2830 data->gva = (void *) ALIGN((unsigned long) alloc_vpages(npages * 2), 2831 size); 2832 TEST_ASSERT(!any_present_pages(page_table, data->gva, size)); 2833 install_pages(page_table, data->gpa, size, data->gva); 2834 2835 /* 2836 * Make sure nothing's mapped here so the tests that screw with the 2837 * pml4 entry don't inadvertently break something. 2838 */ 2839 TEST_ASSERT(get_ept_pte(pml4, data->gpa, 4, &pte) && pte == 0); 2840 TEST_ASSERT(get_ept_pte(pml4, data->gpa + size - 1, 4, &pte) && pte == 0); 2841 install_ept(pml4, data->hpa, data->gpa, EPT_PRESENT); 2842 2843 data->hva[0] = MAGIC_VAL_1; 2844 memcpy(&data->hva[1], &ret42_start, &ret42_end - &ret42_start); 2845 } 2846 2847 static void ept_access_test_not_present(void) 2848 { 2849 ept_access_test_setup(); 2850 /* --- */ 2851 ept_access_violation(0, OP_READ, EPT_VLT_RD); 2852 ept_access_violation(0, OP_WRITE, EPT_VLT_WR); 2853 ept_access_violation(0, OP_EXEC, EPT_VLT_FETCH); 2854 } 2855 2856 static void ept_access_test_read_only(void) 2857 { 2858 ept_access_test_setup(); 2859 2860 /* r-- */ 2861 ept_access_allowed(EPT_RA, OP_READ); 2862 ept_access_violation(EPT_RA, OP_WRITE, EPT_VLT_WR | EPT_VLT_PERM_RD); 2863 ept_access_violation(EPT_RA, OP_EXEC, EPT_VLT_FETCH | EPT_VLT_PERM_RD); 2864 } 2865 2866 static void ept_access_test_write_only(void) 2867 { 2868 ept_access_test_setup(); 2869 /* -w- */ 2870 ept_access_misconfig(EPT_WA); 2871 } 2872 2873 static void ept_access_test_read_write(void) 2874 { 2875 ept_access_test_setup(); 2876 /* rw- */ 2877 ept_access_allowed(EPT_RA | EPT_WA, OP_READ); 2878 ept_access_allowed(EPT_RA | EPT_WA, OP_WRITE); 2879 ept_access_violation(EPT_RA | EPT_WA, OP_EXEC, 2880 EPT_VLT_FETCH | EPT_VLT_PERM_RD | EPT_VLT_PERM_WR); 2881 } 2882 2883 2884 static void ept_access_test_execute_only(void) 2885 { 2886 ept_access_test_setup(); 2887 /* --x */ 2888 if (ept_execute_only_supported()) { 2889 ept_access_violation(EPT_EA, OP_READ, 2890 EPT_VLT_RD | EPT_VLT_PERM_EX); 2891 ept_access_violation(EPT_EA, OP_WRITE, 2892 EPT_VLT_WR | EPT_VLT_PERM_EX); 2893 ept_access_allowed(EPT_EA, OP_EXEC); 2894 } else { 2895 ept_access_misconfig(EPT_EA); 2896 } 2897 } 2898 2899 static void ept_access_test_read_execute(void) 2900 { 2901 ept_access_test_setup(); 2902 /* r-x */ 2903 ept_access_allowed(EPT_RA | EPT_EA, OP_READ); 2904 ept_access_violation(EPT_RA | EPT_EA, OP_WRITE, 2905 EPT_VLT_WR | EPT_VLT_PERM_RD | EPT_VLT_PERM_EX); 2906 ept_access_allowed(EPT_RA | EPT_EA, OP_EXEC); 2907 } 2908 2909 static void ept_access_test_write_execute(void) 2910 { 2911 ept_access_test_setup(); 2912 /* -wx */ 2913 ept_access_misconfig(EPT_WA | EPT_EA); 2914 } 2915 2916 static void ept_access_test_read_write_execute(void) 2917 { 2918 ept_access_test_setup(); 2919 /* rwx */ 2920 ept_access_allowed(EPT_RA | EPT_WA | EPT_EA, OP_READ); 2921 ept_access_allowed(EPT_RA | EPT_WA | EPT_EA, OP_WRITE); 2922 ept_access_allowed(EPT_RA | EPT_WA | EPT_EA, OP_EXEC); 2923 } 2924 2925 static void ept_access_test_reserved_bits(void) 2926 { 2927 int i; 2928 int maxphyaddr; 2929 2930 ept_access_test_setup(); 2931 2932 /* Reserved bits above maxphyaddr. */ 2933 maxphyaddr = cpuid_maxphyaddr(); 2934 for (i = maxphyaddr; i <= 51; i++) { 2935 report_prefix_pushf("reserved_bit=%d", i); 2936 ept_reserved_bit(i); 2937 report_prefix_pop(); 2938 } 2939 2940 /* Level-specific reserved bits. */ 2941 ept_reserved_bit_at_level_nohuge(2, 3); 2942 ept_reserved_bit_at_level_nohuge(2, 4); 2943 ept_reserved_bit_at_level_nohuge(2, 5); 2944 ept_reserved_bit_at_level_nohuge(2, 6); 2945 /* 2M alignment. */ 2946 for (i = 12; i < 20; i++) { 2947 report_prefix_pushf("reserved_bit=%d", i); 2948 ept_reserved_bit_at_level_huge(2, i); 2949 report_prefix_pop(); 2950 } 2951 ept_reserved_bit_at_level_nohuge(3, 3); 2952 ept_reserved_bit_at_level_nohuge(3, 4); 2953 ept_reserved_bit_at_level_nohuge(3, 5); 2954 ept_reserved_bit_at_level_nohuge(3, 6); 2955 /* 1G alignment. */ 2956 for (i = 12; i < 29; i++) { 2957 report_prefix_pushf("reserved_bit=%d", i); 2958 ept_reserved_bit_at_level_huge(3, i); 2959 report_prefix_pop(); 2960 } 2961 ept_reserved_bit_at_level(4, 3); 2962 ept_reserved_bit_at_level(4, 4); 2963 ept_reserved_bit_at_level(4, 5); 2964 ept_reserved_bit_at_level(4, 6); 2965 ept_reserved_bit_at_level(4, 7); 2966 } 2967 2968 static void ept_access_test_ignored_bits(void) 2969 { 2970 ept_access_test_setup(); 2971 /* 2972 * Bits ignored at every level. Bits 8 and 9 (A and D) are ignored as 2973 * far as translation is concerned even if AD bits are enabled in the 2974 * EPTP. Bit 63 is ignored because "EPT-violation #VE" VM-execution 2975 * control is 0. 2976 */ 2977 ept_ignored_bit(8); 2978 ept_ignored_bit(9); 2979 ept_ignored_bit(10); 2980 ept_ignored_bit(11); 2981 ept_ignored_bit(52); 2982 ept_ignored_bit(53); 2983 ept_ignored_bit(54); 2984 ept_ignored_bit(55); 2985 ept_ignored_bit(56); 2986 ept_ignored_bit(57); 2987 ept_ignored_bit(58); 2988 ept_ignored_bit(59); 2989 ept_ignored_bit(60); 2990 ept_ignored_bit(61); 2991 ept_ignored_bit(62); 2992 ept_ignored_bit(63); 2993 } 2994 2995 static void ept_access_test_paddr_not_present_ad_disabled(void) 2996 { 2997 ept_access_test_setup(); 2998 ept_disable_ad_bits(); 2999 3000 ept_access_violation_paddr(0, PT_AD_MASK, OP_READ, EPT_VLT_RD); 3001 ept_access_violation_paddr(0, PT_AD_MASK, OP_WRITE, EPT_VLT_RD); 3002 ept_access_violation_paddr(0, PT_AD_MASK, OP_EXEC, EPT_VLT_RD); 3003 } 3004 3005 static void ept_access_test_paddr_not_present_ad_enabled(void) 3006 { 3007 u64 qual = EPT_VLT_RD | EPT_VLT_WR; 3008 3009 ept_access_test_setup(); 3010 ept_enable_ad_bits_or_skip_test(); 3011 3012 ept_access_violation_paddr(0, PT_AD_MASK, OP_READ, qual); 3013 ept_access_violation_paddr(0, PT_AD_MASK, OP_WRITE, qual); 3014 ept_access_violation_paddr(0, PT_AD_MASK, OP_EXEC, qual); 3015 } 3016 3017 static void ept_access_test_paddr_read_only_ad_disabled(void) 3018 { 3019 /* 3020 * When EPT AD bits are disabled, all accesses to guest paging 3021 * structures are reported separately as a read and (after 3022 * translation of the GPA to host physical address) a read+write 3023 * if the A/D bits have to be set. 3024 */ 3025 u64 qual = EPT_VLT_WR | EPT_VLT_RD | EPT_VLT_PERM_RD; 3026 3027 ept_access_test_setup(); 3028 ept_disable_ad_bits(); 3029 3030 /* Can't update A bit, so all accesses fail. */ 3031 ept_access_violation_paddr(EPT_RA, 0, OP_READ, qual); 3032 ept_access_violation_paddr(EPT_RA, 0, OP_WRITE, qual); 3033 ept_access_violation_paddr(EPT_RA, 0, OP_EXEC, qual); 3034 /* AD bits disabled, so only writes try to update the D bit. */ 3035 ept_access_allowed_paddr(EPT_RA, PT_ACCESSED_MASK, OP_READ); 3036 ept_access_violation_paddr(EPT_RA, PT_ACCESSED_MASK, OP_WRITE, qual); 3037 ept_access_allowed_paddr(EPT_RA, PT_ACCESSED_MASK, OP_EXEC); 3038 /* Both A and D already set, so read-only is OK. */ 3039 ept_access_allowed_paddr(EPT_RA, PT_AD_MASK, OP_READ); 3040 ept_access_allowed_paddr(EPT_RA, PT_AD_MASK, OP_WRITE); 3041 ept_access_allowed_paddr(EPT_RA, PT_AD_MASK, OP_EXEC); 3042 } 3043 3044 static void ept_access_test_paddr_read_only_ad_enabled(void) 3045 { 3046 /* 3047 * When EPT AD bits are enabled, all accesses to guest paging 3048 * structures are considered writes as far as EPT translation 3049 * is concerned. 3050 */ 3051 u64 qual = EPT_VLT_WR | EPT_VLT_RD | EPT_VLT_PERM_RD; 3052 3053 ept_access_test_setup(); 3054 ept_enable_ad_bits_or_skip_test(); 3055 3056 ept_access_violation_paddr(EPT_RA, 0, OP_READ, qual); 3057 ept_access_violation_paddr(EPT_RA, 0, OP_WRITE, qual); 3058 ept_access_violation_paddr(EPT_RA, 0, OP_EXEC, qual); 3059 ept_access_violation_paddr(EPT_RA, PT_ACCESSED_MASK, OP_READ, qual); 3060 ept_access_violation_paddr(EPT_RA, PT_ACCESSED_MASK, OP_WRITE, qual); 3061 ept_access_violation_paddr(EPT_RA, PT_ACCESSED_MASK, OP_EXEC, qual); 3062 ept_access_violation_paddr(EPT_RA, PT_AD_MASK, OP_READ, qual); 3063 ept_access_violation_paddr(EPT_RA, PT_AD_MASK, OP_WRITE, qual); 3064 ept_access_violation_paddr(EPT_RA, PT_AD_MASK, OP_EXEC, qual); 3065 } 3066 3067 static void ept_access_test_paddr_read_write(void) 3068 { 3069 ept_access_test_setup(); 3070 /* Read-write access to paging structure. */ 3071 ept_access_allowed_paddr(EPT_RA | EPT_WA, 0, OP_READ); 3072 ept_access_allowed_paddr(EPT_RA | EPT_WA, 0, OP_WRITE); 3073 ept_access_allowed_paddr(EPT_RA | EPT_WA, 0, OP_EXEC); 3074 } 3075 3076 static void ept_access_test_paddr_read_write_execute(void) 3077 { 3078 ept_access_test_setup(); 3079 /* RWX access to paging structure. */ 3080 ept_access_allowed_paddr(EPT_PRESENT, 0, OP_READ); 3081 ept_access_allowed_paddr(EPT_PRESENT, 0, OP_WRITE); 3082 ept_access_allowed_paddr(EPT_PRESENT, 0, OP_EXEC); 3083 } 3084 3085 static void ept_access_test_paddr_read_execute_ad_disabled(void) 3086 { 3087 /* 3088 * When EPT AD bits are disabled, all accesses to guest paging 3089 * structures are reported separately as a read and (after 3090 * translation of the GPA to host physical address) a read+write 3091 * if the A/D bits have to be set. 3092 */ 3093 u64 qual = EPT_VLT_WR | EPT_VLT_RD | EPT_VLT_PERM_RD | EPT_VLT_PERM_EX; 3094 3095 ept_access_test_setup(); 3096 ept_disable_ad_bits(); 3097 3098 /* Can't update A bit, so all accesses fail. */ 3099 ept_access_violation_paddr(EPT_RA | EPT_EA, 0, OP_READ, qual); 3100 ept_access_violation_paddr(EPT_RA | EPT_EA, 0, OP_WRITE, qual); 3101 ept_access_violation_paddr(EPT_RA | EPT_EA, 0, OP_EXEC, qual); 3102 /* AD bits disabled, so only writes try to update the D bit. */ 3103 ept_access_allowed_paddr(EPT_RA | EPT_EA, PT_ACCESSED_MASK, OP_READ); 3104 ept_access_violation_paddr(EPT_RA | EPT_EA, PT_ACCESSED_MASK, OP_WRITE, qual); 3105 ept_access_allowed_paddr(EPT_RA | EPT_EA, PT_ACCESSED_MASK, OP_EXEC); 3106 /* Both A and D already set, so read-only is OK. */ 3107 ept_access_allowed_paddr(EPT_RA | EPT_EA, PT_AD_MASK, OP_READ); 3108 ept_access_allowed_paddr(EPT_RA | EPT_EA, PT_AD_MASK, OP_WRITE); 3109 ept_access_allowed_paddr(EPT_RA | EPT_EA, PT_AD_MASK, OP_EXEC); 3110 } 3111 3112 static void ept_access_test_paddr_read_execute_ad_enabled(void) 3113 { 3114 /* 3115 * When EPT AD bits are enabled, all accesses to guest paging 3116 * structures are considered writes as far as EPT translation 3117 * is concerned. 3118 */ 3119 u64 qual = EPT_VLT_WR | EPT_VLT_RD | EPT_VLT_PERM_RD | EPT_VLT_PERM_EX; 3120 3121 ept_access_test_setup(); 3122 ept_enable_ad_bits_or_skip_test(); 3123 3124 ept_access_violation_paddr(EPT_RA | EPT_EA, 0, OP_READ, qual); 3125 ept_access_violation_paddr(EPT_RA | EPT_EA, 0, OP_WRITE, qual); 3126 ept_access_violation_paddr(EPT_RA | EPT_EA, 0, OP_EXEC, qual); 3127 ept_access_violation_paddr(EPT_RA | EPT_EA, PT_ACCESSED_MASK, OP_READ, qual); 3128 ept_access_violation_paddr(EPT_RA | EPT_EA, PT_ACCESSED_MASK, OP_WRITE, qual); 3129 ept_access_violation_paddr(EPT_RA | EPT_EA, PT_ACCESSED_MASK, OP_EXEC, qual); 3130 ept_access_violation_paddr(EPT_RA | EPT_EA, PT_AD_MASK, OP_READ, qual); 3131 ept_access_violation_paddr(EPT_RA | EPT_EA, PT_AD_MASK, OP_WRITE, qual); 3132 ept_access_violation_paddr(EPT_RA | EPT_EA, PT_AD_MASK, OP_EXEC, qual); 3133 } 3134 3135 static void ept_access_test_paddr_not_present_page_fault(void) 3136 { 3137 ept_access_test_setup(); 3138 /* 3139 * TODO: test no EPT violation as long as guest PF occurs. e.g., GPA is 3140 * page is read-only in EPT but GVA is also mapped read only in PT. 3141 * Thus guest page fault before host takes EPT violation for trying to 3142 * update A bit. 3143 */ 3144 } 3145 3146 static void ept_access_test_force_2m_page(void) 3147 { 3148 ept_access_test_setup(); 3149 3150 TEST_ASSERT_EQ(ept_2m_supported(), true); 3151 ept_allowed_at_level_mkhuge(true, 2, 0, 0, OP_READ); 3152 ept_violation_at_level_mkhuge(true, 2, EPT_PRESENT, EPT_RA, OP_WRITE, 3153 EPT_VLT_WR | EPT_VLT_PERM_RD | 3154 EPT_VLT_LADDR_VLD | EPT_VLT_PADDR); 3155 ept_misconfig_at_level_mkhuge(true, 2, EPT_PRESENT, EPT_WA); 3156 } 3157 3158 static bool invvpid_valid(u64 type, u64 vpid, u64 gla) 3159 { 3160 u64 msr = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP); 3161 3162 TEST_ASSERT(msr & VPID_CAP_INVVPID); 3163 3164 if (type < INVVPID_ADDR || type > INVVPID_CONTEXT_LOCAL) 3165 return false; 3166 3167 if (!(msr & (1ull << (type + VPID_CAP_INVVPID_TYPES_SHIFT)))) 3168 return false; 3169 3170 if (vpid >> 16) 3171 return false; 3172 3173 if (type != INVVPID_ALL && !vpid) 3174 return false; 3175 3176 if (type == INVVPID_ADDR && !is_canonical(gla)) 3177 return false; 3178 3179 return true; 3180 } 3181 3182 static void try_invvpid(u64 type, u64 vpid, u64 gla) 3183 { 3184 int rc; 3185 bool valid = invvpid_valid(type, vpid, gla); 3186 u64 expected = valid ? VMXERR_UNSUPPORTED_VMCS_COMPONENT 3187 : VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID; 3188 /* 3189 * Set VMX_INST_ERROR to VMXERR_UNVALID_VMCS_COMPONENT, so 3190 * that we can tell if it is updated by INVVPID. 3191 */ 3192 vmcs_read(~0); 3193 rc = invvpid(type, vpid, gla); 3194 report(!rc == valid, "INVVPID type %ld VPID %lx GLA %lx %s", type, 3195 vpid, gla, 3196 valid ? "passes" : "fails"); 3197 report(vmcs_read(VMX_INST_ERROR) == expected, 3198 "After %s INVVPID, VMX_INST_ERR is %ld (actual %ld)", 3199 rc ? "failed" : "successful", 3200 expected, vmcs_read(VMX_INST_ERROR)); 3201 } 3202 3203 static void ds_invvpid(void *data) 3204 { 3205 u64 msr = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP); 3206 u64 type = ffs(msr >> VPID_CAP_INVVPID_TYPES_SHIFT) - 1; 3207 3208 TEST_ASSERT(type >= INVVPID_ADDR && type <= INVVPID_CONTEXT_LOCAL); 3209 asm volatile("invvpid %0, %1" 3210 : 3211 : "m"(*(struct invvpid_operand *)data), 3212 "r"(type)); 3213 } 3214 3215 /* 3216 * The SS override is ignored in 64-bit mode, so we use an addressing 3217 * mode with %rsp as the base register to generate an implicit SS 3218 * reference. 3219 */ 3220 static void ss_invvpid(void *data) 3221 { 3222 u64 msr = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP); 3223 u64 type = ffs(msr >> VPID_CAP_INVVPID_TYPES_SHIFT) - 1; 3224 3225 TEST_ASSERT(type >= INVVPID_ADDR && type <= INVVPID_CONTEXT_LOCAL); 3226 asm volatile("sub %%rsp,%0; invvpid (%%rsp,%0,1), %1" 3227 : "+r"(data) 3228 : "r"(type)); 3229 } 3230 3231 static void invvpid_test_gp(void) 3232 { 3233 bool fault; 3234 3235 fault = test_for_exception(GP_VECTOR, &ds_invvpid, 3236 (void *)NONCANONICAL); 3237 report(fault, "INVVPID with non-canonical DS operand raises #GP"); 3238 } 3239 3240 static void invvpid_test_ss(void) 3241 { 3242 bool fault; 3243 3244 fault = test_for_exception(SS_VECTOR, &ss_invvpid, 3245 (void *)NONCANONICAL); 3246 report(fault, "INVVPID with non-canonical SS operand raises #SS"); 3247 } 3248 3249 static void invvpid_test_pf(void) 3250 { 3251 void *vpage = alloc_vpage(); 3252 bool fault; 3253 3254 fault = test_for_exception(PF_VECTOR, &ds_invvpid, vpage); 3255 report(fault, "INVVPID with unmapped operand raises #PF"); 3256 } 3257 3258 static void try_compat_invvpid(void *unused) 3259 { 3260 struct far_pointer32 fp = { 3261 .offset = (uintptr_t)&&invvpid, 3262 .selector = KERNEL_CS32, 3263 }; 3264 register uintptr_t rsp asm("rsp"); 3265 3266 TEST_ASSERT_MSG(fp.offset == (uintptr_t)&&invvpid, 3267 "Code address too high."); 3268 TEST_ASSERT_MSG(rsp == (u32)rsp, "Stack address too high."); 3269 3270 asm goto ("lcall *%0" : : "m" (fp) : "rax" : invvpid); 3271 return; 3272 invvpid: 3273 asm volatile (".code32;" 3274 "invvpid (%eax), %eax;" 3275 "lret;" 3276 ".code64"); 3277 __builtin_unreachable(); 3278 } 3279 3280 static void invvpid_test_compatibility_mode(void) 3281 { 3282 bool fault; 3283 3284 fault = test_for_exception(UD_VECTOR, &try_compat_invvpid, NULL); 3285 report(fault, "Compatibility mode INVVPID raises #UD"); 3286 } 3287 3288 static void invvpid_test_not_in_vmx_operation(void) 3289 { 3290 bool fault; 3291 3292 TEST_ASSERT(!vmx_off()); 3293 fault = test_for_exception(UD_VECTOR, &ds_invvpid, NULL); 3294 report(fault, "INVVPID outside of VMX operation raises #UD"); 3295 TEST_ASSERT(!vmx_on()); 3296 } 3297 3298 /* 3299 * This does not test real-address mode, virtual-8086 mode, protected mode, 3300 * or CPL > 0. 3301 */ 3302 static void invvpid_test_v2(void) 3303 { 3304 u64 msr; 3305 int i; 3306 unsigned types = 0; 3307 unsigned type; 3308 3309 if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) || 3310 !(ctrl_cpu_rev[1].clr & CPU_VPID)) 3311 test_skip("VPID not supported"); 3312 3313 msr = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP); 3314 3315 if (!(msr & VPID_CAP_INVVPID)) 3316 test_skip("INVVPID not supported.\n"); 3317 3318 if (msr & VPID_CAP_INVVPID_ADDR) 3319 types |= 1u << INVVPID_ADDR; 3320 if (msr & VPID_CAP_INVVPID_CXTGLB) 3321 types |= 1u << INVVPID_CONTEXT_GLOBAL; 3322 if (msr & VPID_CAP_INVVPID_ALL) 3323 types |= 1u << INVVPID_ALL; 3324 if (msr & VPID_CAP_INVVPID_CXTLOC) 3325 types |= 1u << INVVPID_CONTEXT_LOCAL; 3326 3327 if (!types) 3328 test_skip("No INVVPID types supported.\n"); 3329 3330 for (i = -127; i < 128; i++) 3331 try_invvpid(i, 0xffff, 0); 3332 3333 /* 3334 * VPID must not be more than 16 bits. 3335 */ 3336 for (i = 0; i < 64; i++) 3337 for (type = 0; type < 4; type++) 3338 if (types & (1u << type)) 3339 try_invvpid(type, 1ul << i, 0); 3340 3341 /* 3342 * VPID must not be zero, except for "all contexts." 3343 */ 3344 for (type = 0; type < 4; type++) 3345 if (types & (1u << type)) 3346 try_invvpid(type, 0, 0); 3347 3348 /* 3349 * The gla operand is only validated for single-address INVVPID. 3350 */ 3351 if (types & (1u << INVVPID_ADDR)) 3352 try_invvpid(INVVPID_ADDR, 0xffff, NONCANONICAL); 3353 3354 invvpid_test_gp(); 3355 invvpid_test_ss(); 3356 invvpid_test_pf(); 3357 invvpid_test_compatibility_mode(); 3358 invvpid_test_not_in_vmx_operation(); 3359 } 3360 3361 /* 3362 * Test for early VMLAUNCH failure. Returns true if VMLAUNCH makes it 3363 * at least as far as the guest-state checks. Returns false if the 3364 * VMLAUNCH fails early and execution falls through to the next 3365 * instruction. 3366 */ 3367 static bool vmlaunch_succeeds(void) 3368 { 3369 u32 exit_reason; 3370 3371 /* 3372 * Indirectly set VMX_INST_ERR to 12 ("VMREAD/VMWRITE from/to 3373 * unsupported VMCS component"). The caller can then check 3374 * to see if a failed VM-entry sets VMX_INST_ERR as expected. 3375 */ 3376 vmcs_write(~0u, 0); 3377 3378 vmcs_write(HOST_RIP, (uintptr_t)&&success); 3379 __asm__ __volatile__ goto ("vmwrite %%rsp, %0; vmlaunch" 3380 : 3381 : "r" ((u64)HOST_RSP) 3382 : "cc", "memory" 3383 : success); 3384 return false; 3385 success: 3386 exit_reason = vmcs_read(EXI_REASON); 3387 TEST_ASSERT(exit_reason == (VMX_FAIL_STATE | VMX_ENTRY_FAILURE) || 3388 exit_reason == (VMX_FAIL_MSR | VMX_ENTRY_FAILURE)); 3389 return true; 3390 } 3391 3392 /* 3393 * Try to launch the current VMCS. 3394 */ 3395 static void test_vmx_vmlaunch(u32 xerror) 3396 { 3397 bool success = vmlaunch_succeeds(); 3398 u32 vmx_inst_err; 3399 3400 report(success == !xerror, "vmlaunch %s", 3401 !xerror ? "succeeds" : "fails"); 3402 if (!success && xerror) { 3403 vmx_inst_err = vmcs_read(VMX_INST_ERROR); 3404 report(vmx_inst_err == xerror, 3405 "VMX inst error is %d (actual %d)", xerror, 3406 vmx_inst_err); 3407 } 3408 } 3409 3410 static void test_vmx_invalid_controls(void) 3411 { 3412 test_vmx_vmlaunch(VMXERR_ENTRY_INVALID_CONTROL_FIELD); 3413 } 3414 3415 static void test_vmx_valid_controls(void) 3416 { 3417 test_vmx_vmlaunch(0); 3418 } 3419 3420 /* 3421 * Test a particular value of a VM-execution control bit, if the value 3422 * is required or if the value is zero. 3423 */ 3424 static void test_rsvd_ctl_bit_value(const char *name, union vmx_ctrl_msr msr, 3425 enum Encoding encoding, unsigned bit, 3426 unsigned val) 3427 { 3428 u32 mask = 1u << bit; 3429 bool expected; 3430 u32 controls; 3431 3432 if (msr.set & mask) 3433 TEST_ASSERT(msr.clr & mask); 3434 3435 /* 3436 * We can't arbitrarily turn on a control bit, because it may 3437 * introduce dependencies on other VMCS fields. So, we only 3438 * test turning on bits that have a required setting. 3439 */ 3440 if (val && (msr.clr & mask) && !(msr.set & mask)) 3441 return; 3442 3443 report_prefix_pushf("%s %s bit %d", 3444 val ? "Set" : "Clear", name, bit); 3445 3446 controls = vmcs_read(encoding); 3447 if (val) { 3448 vmcs_write(encoding, msr.set | mask); 3449 expected = (msr.clr & mask); 3450 } else { 3451 vmcs_write(encoding, msr.set & ~mask); 3452 expected = !(msr.set & mask); 3453 } 3454 if (expected) 3455 test_vmx_valid_controls(); 3456 else 3457 test_vmx_invalid_controls(); 3458 vmcs_write(encoding, controls); 3459 report_prefix_pop(); 3460 } 3461 3462 /* 3463 * Test reserved values of a VM-execution control bit, based on the 3464 * allowed bit settings from the corresponding VMX capability MSR. 3465 */ 3466 static void test_rsvd_ctl_bit(const char *name, union vmx_ctrl_msr msr, 3467 enum Encoding encoding, unsigned bit) 3468 { 3469 test_rsvd_ctl_bit_value(name, msr, encoding, bit, 0); 3470 test_rsvd_ctl_bit_value(name, msr, encoding, bit, 1); 3471 } 3472 3473 /* 3474 * Reserved bits in the pin-based VM-execution controls must be set 3475 * properly. Software may consult the VMX capability MSRs to determine 3476 * the proper settings. 3477 * [Intel SDM] 3478 */ 3479 static void test_pin_based_ctls(void) 3480 { 3481 unsigned bit; 3482 3483 printf("%s: %lx\n", basic.ctrl ? "MSR_IA32_VMX_TRUE_PIN" : 3484 "MSR_IA32_VMX_PINBASED_CTLS", ctrl_pin_rev.val); 3485 for (bit = 0; bit < 32; bit++) 3486 test_rsvd_ctl_bit("pin-based controls", 3487 ctrl_pin_rev, PIN_CONTROLS, bit); 3488 } 3489 3490 /* 3491 * Reserved bits in the primary processor-based VM-execution controls 3492 * must be set properly. Software may consult the VMX capability MSRs 3493 * to determine the proper settings. 3494 * [Intel SDM] 3495 */ 3496 static void test_primary_processor_based_ctls(void) 3497 { 3498 unsigned bit; 3499 3500 printf("\n%s: %lx\n", basic.ctrl ? "MSR_IA32_VMX_TRUE_PROC" : 3501 "MSR_IA32_VMX_PROCBASED_CTLS", ctrl_cpu_rev[0].val); 3502 for (bit = 0; bit < 32; bit++) 3503 test_rsvd_ctl_bit("primary processor-based controls", 3504 ctrl_cpu_rev[0], CPU_EXEC_CTRL0, bit); 3505 } 3506 3507 /* 3508 * If the "activate secondary controls" primary processor-based 3509 * VM-execution control is 1, reserved bits in the secondary 3510 * processor-based VM-execution controls must be cleared. Software may 3511 * consult the VMX capability MSRs to determine which bits are 3512 * reserved. 3513 * If the "activate secondary controls" primary processor-based 3514 * VM-execution control is 0 (or if the processor does not support the 3515 * 1-setting of that control), no checks are performed on the 3516 * secondary processor-based VM-execution controls. 3517 * [Intel SDM] 3518 */ 3519 static void test_secondary_processor_based_ctls(void) 3520 { 3521 u32 primary; 3522 u32 secondary; 3523 unsigned bit; 3524 3525 if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY)) 3526 return; 3527 3528 primary = vmcs_read(CPU_EXEC_CTRL0); 3529 secondary = vmcs_read(CPU_EXEC_CTRL1); 3530 3531 vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY); 3532 printf("\nMSR_IA32_VMX_PROCBASED_CTLS2: %lx\n", ctrl_cpu_rev[1].val); 3533 for (bit = 0; bit < 32; bit++) 3534 test_rsvd_ctl_bit("secondary processor-based controls", 3535 ctrl_cpu_rev[1], CPU_EXEC_CTRL1, bit); 3536 3537 /* 3538 * When the "activate secondary controls" VM-execution control 3539 * is clear, there are no checks on the secondary controls. 3540 */ 3541 vmcs_write(CPU_EXEC_CTRL0, primary & ~CPU_SECONDARY); 3542 vmcs_write(CPU_EXEC_CTRL1, ~0); 3543 report(vmlaunch_succeeds(), 3544 "Secondary processor-based controls ignored"); 3545 vmcs_write(CPU_EXEC_CTRL1, secondary); 3546 vmcs_write(CPU_EXEC_CTRL0, primary); 3547 } 3548 3549 static void try_cr3_target_count(unsigned i, unsigned max) 3550 { 3551 report_prefix_pushf("CR3 target count 0x%x", i); 3552 vmcs_write(CR3_TARGET_COUNT, i); 3553 if (i <= max) 3554 test_vmx_valid_controls(); 3555 else 3556 test_vmx_invalid_controls(); 3557 report_prefix_pop(); 3558 } 3559 3560 /* 3561 * The CR3-target count must not be greater than 4. Future processors 3562 * may support a different number of CR3-target values. Software 3563 * should read the VMX capability MSR IA32_VMX_MISC to determine the 3564 * number of values supported. 3565 * [Intel SDM] 3566 */ 3567 static void test_cr3_targets(void) 3568 { 3569 unsigned supported_targets = (rdmsr(MSR_IA32_VMX_MISC) >> 16) & 0x1ff; 3570 u32 cr3_targets = vmcs_read(CR3_TARGET_COUNT); 3571 unsigned i; 3572 3573 printf("\nSupported CR3 targets: %d\n", supported_targets); 3574 TEST_ASSERT(supported_targets <= 256); 3575 3576 try_cr3_target_count(-1u, supported_targets); 3577 try_cr3_target_count(0x80000000, supported_targets); 3578 try_cr3_target_count(0x7fffffff, supported_targets); 3579 for (i = 0; i <= supported_targets + 1; i++) 3580 try_cr3_target_count(i, supported_targets); 3581 vmcs_write(CR3_TARGET_COUNT, cr3_targets); 3582 } 3583 3584 /* 3585 * Test a particular address setting in the VMCS 3586 */ 3587 static void test_vmcs_addr(const char *name, 3588 enum Encoding encoding, 3589 u64 align, 3590 bool ignored, 3591 bool skip_beyond_mapped_ram, 3592 u64 addr) 3593 { 3594 report_prefix_pushf("%s = %lx", name, addr); 3595 vmcs_write(encoding, addr); 3596 if (skip_beyond_mapped_ram && 3597 addr > fwcfg_get_u64(FW_CFG_RAM_SIZE) - align && 3598 addr < (1ul << cpuid_maxphyaddr())) 3599 printf("Skipping physical address beyond mapped RAM\n"); 3600 else if (ignored || (IS_ALIGNED(addr, align) && 3601 addr < (1ul << cpuid_maxphyaddr()))) 3602 test_vmx_valid_controls(); 3603 else 3604 test_vmx_invalid_controls(); 3605 report_prefix_pop(); 3606 } 3607 3608 /* 3609 * Test interesting values for a VMCS address 3610 */ 3611 static void test_vmcs_addr_values(const char *name, 3612 enum Encoding encoding, 3613 u64 align, 3614 bool ignored, 3615 bool skip_beyond_mapped_ram, 3616 u32 bit_start, u32 bit_end) 3617 { 3618 unsigned i; 3619 u64 orig_val = vmcs_read(encoding); 3620 3621 for (i = bit_start; i <= bit_end; i++) 3622 test_vmcs_addr(name, encoding, align, ignored, 3623 skip_beyond_mapped_ram, 1ul << i); 3624 3625 test_vmcs_addr(name, encoding, align, ignored, 3626 skip_beyond_mapped_ram, PAGE_SIZE - 1); 3627 test_vmcs_addr(name, encoding, align, ignored, 3628 skip_beyond_mapped_ram, PAGE_SIZE); 3629 test_vmcs_addr(name, encoding, align, ignored, 3630 skip_beyond_mapped_ram, 3631 (1ul << cpuid_maxphyaddr()) - PAGE_SIZE); 3632 test_vmcs_addr(name, encoding, align, ignored, 3633 skip_beyond_mapped_ram, -1ul); 3634 3635 vmcs_write(encoding, orig_val); 3636 } 3637 3638 /* 3639 * Test a physical address reference in the VMCS, when the corresponding 3640 * feature is enabled and when the corresponding feature is disabled. 3641 */ 3642 static void test_vmcs_addr_reference(u32 control_bit, enum Encoding field, 3643 const char *field_name, 3644 const char *control_name, u64 align, 3645 bool skip_beyond_mapped_ram, 3646 bool control_primary) 3647 { 3648 u32 primary = vmcs_read(CPU_EXEC_CTRL0); 3649 u32 secondary = vmcs_read(CPU_EXEC_CTRL1); 3650 u64 page_addr; 3651 3652 if (control_primary) { 3653 if (!(ctrl_cpu_rev[0].clr & control_bit)) 3654 return; 3655 } else { 3656 if (!(ctrl_cpu_rev[1].clr & control_bit)) 3657 return; 3658 } 3659 3660 page_addr = vmcs_read(field); 3661 3662 report_prefix_pushf("%s enabled", control_name); 3663 if (control_primary) { 3664 vmcs_write(CPU_EXEC_CTRL0, primary | control_bit); 3665 } else { 3666 vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY); 3667 vmcs_write(CPU_EXEC_CTRL1, secondary | control_bit); 3668 } 3669 3670 test_vmcs_addr_values(field_name, field, align, false, 3671 skip_beyond_mapped_ram, 0, 63); 3672 report_prefix_pop(); 3673 3674 report_prefix_pushf("%s disabled", control_name); 3675 if (control_primary) { 3676 vmcs_write(CPU_EXEC_CTRL0, primary & ~control_bit); 3677 } else { 3678 vmcs_write(CPU_EXEC_CTRL0, primary & ~CPU_SECONDARY); 3679 vmcs_write(CPU_EXEC_CTRL1, secondary & ~control_bit); 3680 } 3681 3682 test_vmcs_addr_values(field_name, field, align, true, false, 0, 63); 3683 report_prefix_pop(); 3684 3685 vmcs_write(field, page_addr); 3686 vmcs_write(CPU_EXEC_CTRL0, primary); 3687 vmcs_write(CPU_EXEC_CTRL1, secondary); 3688 } 3689 3690 /* 3691 * If the "use I/O bitmaps" VM-execution control is 1, bits 11:0 of 3692 * each I/O-bitmap address must be 0. Neither address should set any 3693 * bits beyond the processor's physical-address width. 3694 * [Intel SDM] 3695 */ 3696 static void test_io_bitmaps(void) 3697 { 3698 test_vmcs_addr_reference(CPU_IO_BITMAP, IO_BITMAP_A, 3699 "I/O bitmap A", "Use I/O bitmaps", 3700 PAGE_SIZE, false, true); 3701 test_vmcs_addr_reference(CPU_IO_BITMAP, IO_BITMAP_B, 3702 "I/O bitmap B", "Use I/O bitmaps", 3703 PAGE_SIZE, false, true); 3704 } 3705 3706 /* 3707 * If the "use MSR bitmaps" VM-execution control is 1, bits 11:0 of 3708 * the MSR-bitmap address must be 0. The address should not set any 3709 * bits beyond the processor's physical-address width. 3710 * [Intel SDM] 3711 */ 3712 static void test_msr_bitmap(void) 3713 { 3714 test_vmcs_addr_reference(CPU_MSR_BITMAP, MSR_BITMAP, 3715 "MSR bitmap", "Use MSR bitmaps", 3716 PAGE_SIZE, false, true); 3717 } 3718 3719 /* 3720 * If the "use TPR shadow" VM-execution control is 1, the virtual-APIC 3721 * address must satisfy the following checks: 3722 * - Bits 11:0 of the address must be 0. 3723 * - The address should not set any bits beyond the processor's 3724 * physical-address width. 3725 * [Intel SDM] 3726 */ 3727 static void test_apic_virt_addr(void) 3728 { 3729 /* 3730 * Ensure the processor will never use the virtual-APIC page, since 3731 * we will point it to invalid RAM. Otherwise KVM is puzzled about 3732 * what we're trying to achieve and fails vmentry. 3733 */ 3734 u32 cpu_ctrls0 = vmcs_read(CPU_EXEC_CTRL0); 3735 vmcs_write(CPU_EXEC_CTRL0, cpu_ctrls0 | CPU_CR8_LOAD | CPU_CR8_STORE); 3736 test_vmcs_addr_reference(CPU_TPR_SHADOW, APIC_VIRT_ADDR, 3737 "virtual-APIC address", "Use TPR shadow", 3738 PAGE_SIZE, false, true); 3739 vmcs_write(CPU_EXEC_CTRL0, cpu_ctrls0); 3740 } 3741 3742 /* 3743 * If the "virtualize APIC-accesses" VM-execution control is 1, the 3744 * APIC-access address must satisfy the following checks: 3745 * - Bits 11:0 of the address must be 0. 3746 * - The address should not set any bits beyond the processor's 3747 * physical-address width. 3748 * [Intel SDM] 3749 */ 3750 static void test_apic_access_addr(void) 3751 { 3752 void *apic_access_page = alloc_page(); 3753 3754 vmcs_write(APIC_ACCS_ADDR, virt_to_phys(apic_access_page)); 3755 3756 test_vmcs_addr_reference(CPU_VIRT_APIC_ACCESSES, APIC_ACCS_ADDR, 3757 "APIC-access address", 3758 "virtualize APIC-accesses", PAGE_SIZE, 3759 true, false); 3760 } 3761 3762 static bool set_bit_pattern(u8 mask, u32 *secondary) 3763 { 3764 u8 i; 3765 bool flag = false; 3766 u32 test_bits[3] = { 3767 CPU_VIRT_X2APIC, 3768 CPU_APIC_REG_VIRT, 3769 CPU_VINTD 3770 }; 3771 3772 for (i = 0; i < ARRAY_SIZE(test_bits); i++) { 3773 if ((mask & (1u << i)) && 3774 (ctrl_cpu_rev[1].clr & test_bits[i])) { 3775 *secondary |= test_bits[i]; 3776 flag = true; 3777 } 3778 } 3779 3780 return (flag); 3781 } 3782 3783 /* 3784 * If the "use TPR shadow" VM-execution control is 0, the following 3785 * VM-execution controls must also be 0: 3786 * - virtualize x2APIC mode 3787 * - APIC-register virtualization 3788 * - virtual-interrupt delivery 3789 * [Intel SDM] 3790 * 3791 * 2. If the "virtualize x2APIC mode" VM-execution control is 1, the 3792 * "virtualize APIC accesses" VM-execution control must be 0. 3793 * [Intel SDM] 3794 */ 3795 static void test_apic_virtual_ctls(void) 3796 { 3797 u32 saved_primary = vmcs_read(CPU_EXEC_CTRL0); 3798 u32 saved_secondary = vmcs_read(CPU_EXEC_CTRL1); 3799 u32 primary = saved_primary; 3800 u32 secondary = saved_secondary; 3801 bool ctrl = false; 3802 char str[10] = "disabled"; 3803 u8 i = 0, j; 3804 3805 /* 3806 * First test 3807 */ 3808 if (!((ctrl_cpu_rev[0].clr & (CPU_SECONDARY | CPU_TPR_SHADOW)) == 3809 (CPU_SECONDARY | CPU_TPR_SHADOW))) 3810 return; 3811 3812 primary |= CPU_SECONDARY; 3813 primary &= ~CPU_TPR_SHADOW; 3814 vmcs_write(CPU_EXEC_CTRL0, primary); 3815 3816 while (1) { 3817 for (j = 1; j < 8; j++) { 3818 secondary &= ~(CPU_VIRT_X2APIC | CPU_APIC_REG_VIRT | CPU_VINTD); 3819 if (primary & CPU_TPR_SHADOW) { 3820 ctrl = true; 3821 } else { 3822 if (! set_bit_pattern(j, &secondary)) 3823 ctrl = true; 3824 else 3825 ctrl = false; 3826 } 3827 3828 vmcs_write(CPU_EXEC_CTRL1, secondary); 3829 report_prefix_pushf("Use TPR shadow %s, virtualize x2APIC mode %s, APIC-register virtualization %s, virtual-interrupt delivery %s", 3830 str, (secondary & CPU_VIRT_X2APIC) ? "enabled" : "disabled", (secondary & CPU_APIC_REG_VIRT) ? "enabled" : "disabled", (secondary & CPU_VINTD) ? "enabled" : "disabled"); 3831 if (ctrl) 3832 test_vmx_valid_controls(); 3833 else 3834 test_vmx_invalid_controls(); 3835 report_prefix_pop(); 3836 } 3837 3838 if (i == 1) 3839 break; 3840 i++; 3841 3842 primary |= CPU_TPR_SHADOW; 3843 vmcs_write(CPU_EXEC_CTRL0, primary); 3844 strcpy(str, "enabled"); 3845 } 3846 3847 /* 3848 * Second test 3849 */ 3850 u32 apic_virt_ctls = (CPU_VIRT_X2APIC | CPU_VIRT_APIC_ACCESSES); 3851 3852 primary = saved_primary; 3853 secondary = saved_secondary; 3854 if (!((ctrl_cpu_rev[1].clr & apic_virt_ctls) == apic_virt_ctls)) 3855 return; 3856 3857 vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY); 3858 secondary &= ~CPU_VIRT_APIC_ACCESSES; 3859 vmcs_write(CPU_EXEC_CTRL1, secondary & ~CPU_VIRT_X2APIC); 3860 report_prefix_pushf("Virtualize x2APIC mode disabled; virtualize APIC access disabled"); 3861 test_vmx_valid_controls(); 3862 report_prefix_pop(); 3863 3864 vmcs_write(CPU_EXEC_CTRL1, secondary | CPU_VIRT_APIC_ACCESSES); 3865 report_prefix_pushf("Virtualize x2APIC mode disabled; virtualize APIC access enabled"); 3866 test_vmx_valid_controls(); 3867 report_prefix_pop(); 3868 3869 vmcs_write(CPU_EXEC_CTRL1, secondary | CPU_VIRT_X2APIC); 3870 report_prefix_pushf("Virtualize x2APIC mode enabled; virtualize APIC access enabled"); 3871 test_vmx_invalid_controls(); 3872 report_prefix_pop(); 3873 3874 vmcs_write(CPU_EXEC_CTRL1, secondary & ~CPU_VIRT_APIC_ACCESSES); 3875 report_prefix_pushf("Virtualize x2APIC mode enabled; virtualize APIC access disabled"); 3876 test_vmx_valid_controls(); 3877 report_prefix_pop(); 3878 3879 vmcs_write(CPU_EXEC_CTRL0, saved_primary); 3880 vmcs_write(CPU_EXEC_CTRL1, saved_secondary); 3881 } 3882 3883 /* 3884 * If the "virtual-interrupt delivery" VM-execution control is 1, the 3885 * "external-interrupt exiting" VM-execution control must be 1. 3886 * [Intel SDM] 3887 */ 3888 static void test_virtual_intr_ctls(void) 3889 { 3890 u32 saved_primary = vmcs_read(CPU_EXEC_CTRL0); 3891 u32 saved_secondary = vmcs_read(CPU_EXEC_CTRL1); 3892 u32 saved_pin = vmcs_read(PIN_CONTROLS); 3893 u32 primary = saved_primary; 3894 u32 secondary = saved_secondary; 3895 u32 pin = saved_pin; 3896 3897 if (!((ctrl_cpu_rev[1].clr & CPU_VINTD) && 3898 (ctrl_pin_rev.clr & PIN_EXTINT))) 3899 return; 3900 3901 vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY | CPU_TPR_SHADOW); 3902 vmcs_write(CPU_EXEC_CTRL1, secondary & ~CPU_VINTD); 3903 vmcs_write(PIN_CONTROLS, pin & ~PIN_EXTINT); 3904 report_prefix_pushf("Virtualize interrupt-delivery disabled; external-interrupt exiting disabled"); 3905 test_vmx_valid_controls(); 3906 report_prefix_pop(); 3907 3908 vmcs_write(CPU_EXEC_CTRL1, secondary | CPU_VINTD); 3909 report_prefix_pushf("Virtualize interrupt-delivery enabled; external-interrupt exiting disabled"); 3910 test_vmx_invalid_controls(); 3911 report_prefix_pop(); 3912 3913 vmcs_write(PIN_CONTROLS, pin | PIN_EXTINT); 3914 report_prefix_pushf("Virtualize interrupt-delivery enabled; external-interrupt exiting enabled"); 3915 test_vmx_valid_controls(); 3916 report_prefix_pop(); 3917 3918 vmcs_write(PIN_CONTROLS, pin & ~PIN_EXTINT); 3919 report_prefix_pushf("Virtualize interrupt-delivery enabled; external-interrupt exiting disabled"); 3920 test_vmx_invalid_controls(); 3921 report_prefix_pop(); 3922 3923 vmcs_write(CPU_EXEC_CTRL0, saved_primary); 3924 vmcs_write(CPU_EXEC_CTRL1, saved_secondary); 3925 vmcs_write(PIN_CONTROLS, saved_pin); 3926 } 3927 3928 static void test_pi_desc_addr(u64 addr, bool ctrl) 3929 { 3930 vmcs_write(POSTED_INTR_DESC_ADDR, addr); 3931 report_prefix_pushf("Process-posted-interrupts enabled; posted-interrupt-descriptor-address 0x%lx", addr); 3932 if (ctrl) 3933 test_vmx_valid_controls(); 3934 else 3935 test_vmx_invalid_controls(); 3936 report_prefix_pop(); 3937 } 3938 3939 /* 3940 * If the “process posted interrupts†VM-execution control is 1, the 3941 * following must be true: 3942 * 3943 * - The “virtual-interrupt delivery†VM-execution control is 1. 3944 * - The “acknowledge interrupt on exit†VM-exit control is 1. 3945 * - The posted-interrupt notification vector has a value in the 3946 * - range 0–255 (bits 15:8 are all 0). 3947 * - Bits 5:0 of the posted-interrupt descriptor address are all 0. 3948 * - The posted-interrupt descriptor address does not set any bits 3949 * beyond the processor's physical-address width. 3950 * [Intel SDM] 3951 */ 3952 static void test_posted_intr(void) 3953 { 3954 u32 saved_primary = vmcs_read(CPU_EXEC_CTRL0); 3955 u32 saved_secondary = vmcs_read(CPU_EXEC_CTRL1); 3956 u32 saved_pin = vmcs_read(PIN_CONTROLS); 3957 u32 exit_ctl_saved = vmcs_read(EXI_CONTROLS); 3958 u32 primary = saved_primary; 3959 u32 secondary = saved_secondary; 3960 u32 pin = saved_pin; 3961 u32 exit_ctl = exit_ctl_saved; 3962 u16 vec; 3963 int i; 3964 3965 if (!((ctrl_pin_rev.clr & PIN_POST_INTR) && 3966 (ctrl_cpu_rev[1].clr & CPU_VINTD) && 3967 (ctrl_exit_rev.clr & EXI_INTA))) 3968 return; 3969 3970 vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY | CPU_TPR_SHADOW); 3971 3972 /* 3973 * Test virtual-interrupt-delivery and acknowledge-interrupt-on-exit 3974 */ 3975 pin |= PIN_POST_INTR; 3976 vmcs_write(PIN_CONTROLS, pin); 3977 secondary &= ~CPU_VINTD; 3978 vmcs_write(CPU_EXEC_CTRL1, secondary); 3979 report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery disabled"); 3980 test_vmx_invalid_controls(); 3981 report_prefix_pop(); 3982 3983 secondary |= CPU_VINTD; 3984 vmcs_write(CPU_EXEC_CTRL1, secondary); 3985 report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery enabled"); 3986 test_vmx_invalid_controls(); 3987 report_prefix_pop(); 3988 3989 exit_ctl &= ~EXI_INTA; 3990 vmcs_write(EXI_CONTROLS, exit_ctl); 3991 report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery enabled; acknowledge-interrupt-on-exit disabled"); 3992 test_vmx_invalid_controls(); 3993 report_prefix_pop(); 3994 3995 exit_ctl |= EXI_INTA; 3996 vmcs_write(EXI_CONTROLS, exit_ctl); 3997 report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery enabled; acknowledge-interrupt-on-exit enabled"); 3998 test_vmx_valid_controls(); 3999 report_prefix_pop(); 4000 4001 secondary &= ~CPU_VINTD; 4002 vmcs_write(CPU_EXEC_CTRL1, secondary); 4003 report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery disabled; acknowledge-interrupt-on-exit enabled"); 4004 test_vmx_invalid_controls(); 4005 report_prefix_pop(); 4006 4007 secondary |= CPU_VINTD; 4008 vmcs_write(CPU_EXEC_CTRL1, secondary); 4009 report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery enabled; acknowledge-interrupt-on-exit enabled"); 4010 test_vmx_valid_controls(); 4011 report_prefix_pop(); 4012 4013 /* 4014 * Test posted-interrupt notification vector 4015 */ 4016 for (i = 0; i < 8; i++) { 4017 vec = (1ul << i); 4018 vmcs_write(PINV, vec); 4019 report_prefix_pushf("Process-posted-interrupts enabled; posted-interrupt-notification-vector %u", vec); 4020 test_vmx_valid_controls(); 4021 report_prefix_pop(); 4022 } 4023 for (i = 8; i < 16; i++) { 4024 vec = (1ul << i); 4025 vmcs_write(PINV, vec); 4026 report_prefix_pushf("Process-posted-interrupts enabled; posted-interrupt-notification-vector %u", vec); 4027 test_vmx_invalid_controls(); 4028 report_prefix_pop(); 4029 } 4030 4031 vec &= ~(0xff << 8); 4032 vmcs_write(PINV, vec); 4033 report_prefix_pushf("Process-posted-interrupts enabled; posted-interrupt-notification-vector %u", vec); 4034 test_vmx_valid_controls(); 4035 report_prefix_pop(); 4036 4037 /* 4038 * Test posted-interrupt descriptor addresss 4039 */ 4040 for (i = 0; i < 6; i++) { 4041 test_pi_desc_addr(1ul << i, false); 4042 } 4043 4044 test_pi_desc_addr(0xf0, false); 4045 test_pi_desc_addr(0xff, false); 4046 test_pi_desc_addr(0x0f, false); 4047 test_pi_desc_addr(0x8000, true); 4048 test_pi_desc_addr(0x00, true); 4049 test_pi_desc_addr(0xc000, true); 4050 4051 test_vmcs_addr_values("process-posted interrupts", 4052 POSTED_INTR_DESC_ADDR, 64, 4053 false, false, 0, 63); 4054 4055 vmcs_write(CPU_EXEC_CTRL0, saved_primary); 4056 vmcs_write(CPU_EXEC_CTRL1, saved_secondary); 4057 vmcs_write(PIN_CONTROLS, saved_pin); 4058 } 4059 4060 static void test_apic_ctls(void) 4061 { 4062 test_apic_virt_addr(); 4063 test_apic_access_addr(); 4064 test_apic_virtual_ctls(); 4065 test_virtual_intr_ctls(); 4066 test_posted_intr(); 4067 } 4068 4069 /* 4070 * If the “enable VPID†VM-execution control is 1, the value of the 4071 * of the VPID VM-execution control field must not be 0000H. 4072 * [Intel SDM] 4073 */ 4074 static void test_vpid(void) 4075 { 4076 u32 saved_primary = vmcs_read(CPU_EXEC_CTRL0); 4077 u32 saved_secondary = vmcs_read(CPU_EXEC_CTRL1); 4078 u16 vpid = 0x0000; 4079 int i; 4080 4081 if (!((ctrl_cpu_rev[0].clr & CPU_SECONDARY) && 4082 (ctrl_cpu_rev[1].clr & CPU_VPID))) { 4083 printf("Secondary controls and/or VPID not supported\n"); 4084 return; 4085 } 4086 4087 vmcs_write(CPU_EXEC_CTRL0, saved_primary | CPU_SECONDARY); 4088 vmcs_write(CPU_EXEC_CTRL1, saved_secondary & ~CPU_VPID); 4089 vmcs_write(VPID, vpid); 4090 report_prefix_pushf("VPID disabled; VPID value %x", vpid); 4091 test_vmx_valid_controls(); 4092 report_prefix_pop(); 4093 4094 vmcs_write(CPU_EXEC_CTRL1, saved_secondary | CPU_VPID); 4095 report_prefix_pushf("VPID enabled; VPID value %x", vpid); 4096 test_vmx_invalid_controls(); 4097 report_prefix_pop(); 4098 4099 for (i = 0; i < 16; i++) { 4100 vpid = (short)1 << i;; 4101 vmcs_write(VPID, vpid); 4102 report_prefix_pushf("VPID enabled; VPID value %x", vpid); 4103 test_vmx_valid_controls(); 4104 report_prefix_pop(); 4105 } 4106 4107 vmcs_write(CPU_EXEC_CTRL0, saved_primary); 4108 vmcs_write(CPU_EXEC_CTRL1, saved_secondary); 4109 } 4110 4111 static void set_vtpr(unsigned vtpr) 4112 { 4113 *(u32 *)phys_to_virt(vmcs_read(APIC_VIRT_ADDR) + APIC_TASKPRI) = vtpr; 4114 } 4115 4116 static void try_tpr_threshold_and_vtpr(unsigned threshold, unsigned vtpr) 4117 { 4118 bool valid = true; 4119 u32 primary = vmcs_read(CPU_EXEC_CTRL0); 4120 u32 secondary = vmcs_read(CPU_EXEC_CTRL1); 4121 4122 if ((primary & CPU_TPR_SHADOW) && 4123 (!(primary & CPU_SECONDARY) || 4124 !(secondary & (CPU_VINTD | CPU_VIRT_APIC_ACCESSES)))) 4125 valid = (threshold & 0xf) <= ((vtpr >> 4) & 0xf); 4126 4127 set_vtpr(vtpr); 4128 report_prefix_pushf("TPR threshold 0x%x, VTPR.class 0x%x", 4129 threshold, (vtpr >> 4) & 0xf); 4130 if (valid) 4131 test_vmx_valid_controls(); 4132 else 4133 test_vmx_invalid_controls(); 4134 report_prefix_pop(); 4135 } 4136 4137 static void test_invalid_event_injection(void) 4138 { 4139 u32 ent_intr_info_save = vmcs_read(ENT_INTR_INFO); 4140 u32 ent_intr_error_save = vmcs_read(ENT_INTR_ERROR); 4141 u32 ent_inst_len_save = vmcs_read(ENT_INST_LEN); 4142 u32 primary_save = vmcs_read(CPU_EXEC_CTRL0); 4143 u32 secondary_save = vmcs_read(CPU_EXEC_CTRL1); 4144 u64 guest_cr0_save = vmcs_read(GUEST_CR0); 4145 u32 ent_intr_info_base = INTR_INFO_VALID_MASK; 4146 u32 ent_intr_info, ent_intr_err, ent_intr_len; 4147 u32 cnt; 4148 4149 /* Setup */ 4150 report_prefix_push("invalid event injection"); 4151 vmcs_write(ENT_INTR_ERROR, 0x00000000); 4152 vmcs_write(ENT_INST_LEN, 0x00000001); 4153 4154 /* The field’s interruption type is not set to a reserved value. */ 4155 ent_intr_info = ent_intr_info_base | INTR_TYPE_RESERVED | DE_VECTOR; 4156 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4157 "RESERVED interruption type invalid [-]", 4158 ent_intr_info); 4159 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4160 test_vmx_invalid_controls(); 4161 report_prefix_pop(); 4162 4163 ent_intr_info = ent_intr_info_base | INTR_TYPE_EXT_INTR | 4164 DE_VECTOR; 4165 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4166 "RESERVED interruption type invalid [+]", 4167 ent_intr_info); 4168 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4169 test_vmx_valid_controls(); 4170 report_prefix_pop(); 4171 4172 /* If the interruption type is other event, the vector is 0. */ 4173 ent_intr_info = ent_intr_info_base | INTR_TYPE_OTHER_EVENT | DB_VECTOR; 4174 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4175 "(OTHER EVENT && vector != 0) invalid [-]", 4176 ent_intr_info); 4177 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4178 test_vmx_invalid_controls(); 4179 report_prefix_pop(); 4180 4181 /* If the interruption type is NMI, the vector is 2 (negative case). */ 4182 ent_intr_info = ent_intr_info_base | INTR_TYPE_NMI_INTR | DE_VECTOR; 4183 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4184 "(NMI && vector != 2) invalid [-]", ent_intr_info); 4185 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4186 test_vmx_invalid_controls(); 4187 report_prefix_pop(); 4188 4189 /* If the interruption type is NMI, the vector is 2 (positive case). */ 4190 ent_intr_info = ent_intr_info_base | INTR_TYPE_NMI_INTR | NMI_VECTOR; 4191 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4192 "(NMI && vector == 2) valid [+]", ent_intr_info); 4193 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4194 test_vmx_valid_controls(); 4195 report_prefix_pop(); 4196 4197 /* 4198 * If the interruption type 4199 * is HW exception, the vector is at most 31. 4200 */ 4201 ent_intr_info = ent_intr_info_base | INTR_TYPE_HARD_EXCEPTION | 0x20; 4202 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4203 "(HW exception && vector > 31) invalid [-]", 4204 ent_intr_info); 4205 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4206 test_vmx_invalid_controls(); 4207 report_prefix_pop(); 4208 4209 /* 4210 * deliver-error-code is 1 iff either 4211 * (a) the "unrestricted guest" VM-execution control is 0 4212 * (b) CR0.PE is set. 4213 */ 4214 4215 /* Assert that unrestricted guest is disabled or unsupported */ 4216 assert(!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) || 4217 !(secondary_save & CPU_URG)); 4218 4219 ent_intr_info = ent_intr_info_base | INTR_TYPE_HARD_EXCEPTION | 4220 GP_VECTOR; 4221 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4222 "error code <-> (!URG || prot_mode) [-]", 4223 ent_intr_info); 4224 vmcs_write(GUEST_CR0, guest_cr0_save & ~X86_CR0_PE & ~X86_CR0_PG); 4225 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4226 test_vmx_invalid_controls(); 4227 report_prefix_pop(); 4228 4229 ent_intr_info = ent_intr_info_base | INTR_INFO_DELIVER_CODE_MASK | 4230 INTR_TYPE_HARD_EXCEPTION | GP_VECTOR; 4231 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4232 "error code <-> (!URG || prot_mode) [+]", 4233 ent_intr_info); 4234 vmcs_write(GUEST_CR0, guest_cr0_save & ~X86_CR0_PE & ~X86_CR0_PG); 4235 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4236 test_vmx_valid_controls(); 4237 report_prefix_pop(); 4238 4239 if (enable_unrestricted_guest()) 4240 goto skip_unrestricted_guest; 4241 4242 ent_intr_info = ent_intr_info_base | INTR_INFO_DELIVER_CODE_MASK | 4243 INTR_TYPE_HARD_EXCEPTION | GP_VECTOR; 4244 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4245 "error code <-> (!URG || prot_mode) [-]", 4246 ent_intr_info); 4247 vmcs_write(GUEST_CR0, guest_cr0_save & ~X86_CR0_PE & ~X86_CR0_PG); 4248 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4249 test_vmx_invalid_controls(); 4250 report_prefix_pop(); 4251 4252 ent_intr_info = ent_intr_info_base | INTR_TYPE_HARD_EXCEPTION | 4253 GP_VECTOR; 4254 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4255 "error code <-> (!URG || prot_mode) [-]", 4256 ent_intr_info); 4257 vmcs_write(GUEST_CR0, guest_cr0_save | X86_CR0_PE); 4258 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4259 test_vmx_invalid_controls(); 4260 report_prefix_pop(); 4261 4262 vmcs_write(CPU_EXEC_CTRL1, secondary_save); 4263 vmcs_write(CPU_EXEC_CTRL0, primary_save); 4264 4265 skip_unrestricted_guest: 4266 vmcs_write(GUEST_CR0, guest_cr0_save); 4267 4268 /* deliver-error-code is 1 iff the interruption type is HW exception */ 4269 report_prefix_push("error code <-> HW exception"); 4270 for (cnt = 0; cnt < 8; cnt++) { 4271 u32 exception_type_mask = cnt << 8; 4272 u32 deliver_error_code_mask = 4273 exception_type_mask != INTR_TYPE_HARD_EXCEPTION ? 4274 INTR_INFO_DELIVER_CODE_MASK : 0; 4275 4276 ent_intr_info = ent_intr_info_base | deliver_error_code_mask | 4277 exception_type_mask | GP_VECTOR; 4278 report_prefix_pushf("VM-entry intr info=0x%x [-]", 4279 ent_intr_info); 4280 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4281 test_vmx_invalid_controls(); 4282 report_prefix_pop(); 4283 } 4284 report_prefix_pop(); 4285 4286 /* 4287 * deliver-error-code is 1 iff the the vector 4288 * indicates an exception that would normally deliver an error code 4289 */ 4290 report_prefix_push("error code <-> vector delivers error code"); 4291 for (cnt = 0; cnt < 32; cnt++) { 4292 bool has_error_code = false; 4293 u32 deliver_error_code_mask; 4294 4295 switch (cnt) { 4296 case DF_VECTOR: 4297 case TS_VECTOR: 4298 case NP_VECTOR: 4299 case SS_VECTOR: 4300 case GP_VECTOR: 4301 case PF_VECTOR: 4302 case AC_VECTOR: 4303 has_error_code = true; 4304 } 4305 4306 /* Negative case */ 4307 deliver_error_code_mask = has_error_code ? 4308 0 : 4309 INTR_INFO_DELIVER_CODE_MASK; 4310 ent_intr_info = ent_intr_info_base | deliver_error_code_mask | 4311 INTR_TYPE_HARD_EXCEPTION | cnt; 4312 report_prefix_pushf("VM-entry intr info=0x%x [-]", 4313 ent_intr_info); 4314 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4315 test_vmx_invalid_controls(); 4316 report_prefix_pop(); 4317 4318 /* Positive case */ 4319 deliver_error_code_mask = has_error_code ? 4320 INTR_INFO_DELIVER_CODE_MASK : 4321 0; 4322 ent_intr_info = ent_intr_info_base | deliver_error_code_mask | 4323 INTR_TYPE_HARD_EXCEPTION | cnt; 4324 report_prefix_pushf("VM-entry intr info=0x%x [+]", 4325 ent_intr_info); 4326 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4327 test_vmx_valid_controls(); 4328 report_prefix_pop(); 4329 } 4330 report_prefix_pop(); 4331 4332 /* Reserved bits in the field (30:12) are 0. */ 4333 report_prefix_push("reserved bits clear"); 4334 for (cnt = 12; cnt <= 30; cnt++) { 4335 ent_intr_info = ent_intr_info_base | 4336 INTR_INFO_DELIVER_CODE_MASK | 4337 INTR_TYPE_HARD_EXCEPTION | GP_VECTOR | 4338 (1U << cnt); 4339 report_prefix_pushf("VM-entry intr info=0x%x [-]", 4340 ent_intr_info); 4341 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4342 test_vmx_invalid_controls(); 4343 report_prefix_pop(); 4344 } 4345 report_prefix_pop(); 4346 4347 /* 4348 * If deliver-error-code is 1 4349 * bits 31:16 of the VM-entry exception error-code field are 0. 4350 */ 4351 ent_intr_info = ent_intr_info_base | INTR_INFO_DELIVER_CODE_MASK | 4352 INTR_TYPE_HARD_EXCEPTION | GP_VECTOR; 4353 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4354 "VM-entry exception error code[31:16] clear", 4355 ent_intr_info); 4356 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4357 for (cnt = 16; cnt <= 31; cnt++) { 4358 ent_intr_err = 1U << cnt; 4359 report_prefix_pushf("VM-entry intr error=0x%x [-]", 4360 ent_intr_err); 4361 vmcs_write(ENT_INTR_ERROR, ent_intr_err); 4362 test_vmx_invalid_controls(); 4363 report_prefix_pop(); 4364 } 4365 vmcs_write(ENT_INTR_ERROR, 0x00000000); 4366 report_prefix_pop(); 4367 4368 /* 4369 * If the interruption type is software interrupt, software exception, 4370 * or privileged software exception, the VM-entry instruction-length 4371 * field is in the range 0–15. 4372 */ 4373 4374 for (cnt = 0; cnt < 3; cnt++) { 4375 switch (cnt) { 4376 case 0: 4377 ent_intr_info = ent_intr_info_base | 4378 INTR_TYPE_SOFT_INTR; 4379 break; 4380 case 1: 4381 ent_intr_info = ent_intr_info_base | 4382 INTR_TYPE_SOFT_EXCEPTION; 4383 break; 4384 case 2: 4385 ent_intr_info = ent_intr_info_base | 4386 INTR_TYPE_PRIV_SW_EXCEPTION; 4387 break; 4388 } 4389 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4390 "VM-entry instruction-length check", 4391 ent_intr_info); 4392 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4393 4394 /* Instruction length set to -1 (0xFFFFFFFF) should fail */ 4395 ent_intr_len = -1; 4396 report_prefix_pushf("VM-entry intr length = 0x%x [-]", 4397 ent_intr_len); 4398 vmcs_write(ENT_INST_LEN, ent_intr_len); 4399 test_vmx_invalid_controls(); 4400 report_prefix_pop(); 4401 4402 /* Instruction length set to 16 should fail */ 4403 ent_intr_len = 0x00000010; 4404 report_prefix_pushf("VM-entry intr length = 0x%x [-]", 4405 ent_intr_len); 4406 vmcs_write(ENT_INST_LEN, 0x00000010); 4407 test_vmx_invalid_controls(); 4408 report_prefix_pop(); 4409 4410 report_prefix_pop(); 4411 } 4412 4413 /* Cleanup */ 4414 vmcs_write(ENT_INTR_INFO, ent_intr_info_save); 4415 vmcs_write(ENT_INTR_ERROR, ent_intr_error_save); 4416 vmcs_write(ENT_INST_LEN, ent_inst_len_save); 4417 vmcs_write(CPU_EXEC_CTRL0, primary_save); 4418 vmcs_write(CPU_EXEC_CTRL1, secondary_save); 4419 vmcs_write(GUEST_CR0, guest_cr0_save); 4420 report_prefix_pop(); 4421 } 4422 4423 /* 4424 * Test interesting vTPR values for a given TPR threshold. 4425 */ 4426 static void test_vtpr_values(unsigned threshold) 4427 { 4428 try_tpr_threshold_and_vtpr(threshold, (threshold - 1) << 4); 4429 try_tpr_threshold_and_vtpr(threshold, threshold << 4); 4430 try_tpr_threshold_and_vtpr(threshold, (threshold + 1) << 4); 4431 } 4432 4433 static void try_tpr_threshold(unsigned threshold) 4434 { 4435 bool valid = true; 4436 4437 u32 primary = vmcs_read(CPU_EXEC_CTRL0); 4438 u32 secondary = vmcs_read(CPU_EXEC_CTRL1); 4439 4440 if ((primary & CPU_TPR_SHADOW) && !((primary & CPU_SECONDARY) && 4441 (secondary & CPU_VINTD))) 4442 valid = !(threshold >> 4); 4443 4444 set_vtpr(-1); 4445 vmcs_write(TPR_THRESHOLD, threshold); 4446 report_prefix_pushf("TPR threshold 0x%x, VTPR.class 0xf", threshold); 4447 if (valid) 4448 test_vmx_valid_controls(); 4449 else 4450 test_vmx_invalid_controls(); 4451 report_prefix_pop(); 4452 4453 if (valid) 4454 test_vtpr_values(threshold); 4455 } 4456 4457 /* 4458 * Test interesting TPR threshold values. 4459 */ 4460 static void test_tpr_threshold_values(void) 4461 { 4462 unsigned i; 4463 4464 for (i = 0; i < 0x10; i++) 4465 try_tpr_threshold(i); 4466 for (i = 4; i < 32; i++) 4467 try_tpr_threshold(1u << i); 4468 try_tpr_threshold(-1u); 4469 try_tpr_threshold(0x7fffffff); 4470 } 4471 4472 /* 4473 * This test covers the following two VM entry checks: 4474 * 4475 * i) If the "use TPR shadow" VM-execution control is 1 and the 4476 * "virtual-interrupt delivery" VM-execution control is 0, bits 4477 * 31:4 of the TPR threshold VM-execution control field must 4478 be 0. 4479 * [Intel SDM] 4480 * 4481 * ii) If the "use TPR shadow" VM-execution control is 1, the 4482 * "virtual-interrupt delivery" VM-execution control is 0 4483 * and the "virtualize APIC accesses" VM-execution control 4484 * is 0, the value of bits 3:0 of the TPR threshold VM-execution 4485 * control field must not be greater than the value of bits 4486 * 7:4 of VTPR. 4487 * [Intel SDM] 4488 */ 4489 static void test_tpr_threshold(void) 4490 { 4491 u32 primary = vmcs_read(CPU_EXEC_CTRL0); 4492 u64 apic_virt_addr = vmcs_read(APIC_VIRT_ADDR); 4493 u64 threshold = vmcs_read(TPR_THRESHOLD); 4494 void *virtual_apic_page; 4495 4496 if (!(ctrl_cpu_rev[0].clr & CPU_TPR_SHADOW)) 4497 return; 4498 4499 virtual_apic_page = alloc_page(); 4500 memset(virtual_apic_page, 0xff, PAGE_SIZE); 4501 vmcs_write(APIC_VIRT_ADDR, virt_to_phys(virtual_apic_page)); 4502 4503 vmcs_write(CPU_EXEC_CTRL0, primary & ~(CPU_TPR_SHADOW | CPU_SECONDARY)); 4504 report_prefix_pushf("Use TPR shadow disabled, secondary controls disabled"); 4505 test_tpr_threshold_values(); 4506 report_prefix_pop(); 4507 vmcs_write(CPU_EXEC_CTRL0, vmcs_read(CPU_EXEC_CTRL0) | CPU_TPR_SHADOW); 4508 report_prefix_pushf("Use TPR shadow enabled, secondary controls disabled"); 4509 test_tpr_threshold_values(); 4510 report_prefix_pop(); 4511 4512 if (!((ctrl_cpu_rev[0].clr & CPU_SECONDARY) && 4513 (ctrl_cpu_rev[1].clr & (CPU_VINTD | CPU_VIRT_APIC_ACCESSES)))) 4514 goto out; 4515 u32 secondary = vmcs_read(CPU_EXEC_CTRL1); 4516 4517 if (ctrl_cpu_rev[1].clr & CPU_VINTD) { 4518 vmcs_write(CPU_EXEC_CTRL1, CPU_VINTD); 4519 report_prefix_pushf("Use TPR shadow enabled; secondary controls disabled; virtual-interrupt delivery enabled; virtualize APIC accesses disabled"); 4520 test_tpr_threshold_values(); 4521 report_prefix_pop(); 4522 4523 vmcs_write(CPU_EXEC_CTRL0, 4524 vmcs_read(CPU_EXEC_CTRL0) | CPU_SECONDARY); 4525 report_prefix_pushf("Use TPR shadow enabled; secondary controls enabled; virtual-interrupt delivery enabled; virtualize APIC accesses disabled"); 4526 test_tpr_threshold_values(); 4527 report_prefix_pop(); 4528 } 4529 4530 if (ctrl_cpu_rev[1].clr & CPU_VIRT_APIC_ACCESSES) { 4531 vmcs_write(CPU_EXEC_CTRL0, 4532 vmcs_read(CPU_EXEC_CTRL0) & ~CPU_SECONDARY); 4533 vmcs_write(CPU_EXEC_CTRL1, CPU_VIRT_APIC_ACCESSES); 4534 report_prefix_pushf("Use TPR shadow enabled; secondary controls disabled; virtual-interrupt delivery enabled; virtualize APIC accesses enabled"); 4535 test_tpr_threshold_values(); 4536 report_prefix_pop(); 4537 4538 vmcs_write(CPU_EXEC_CTRL0, 4539 vmcs_read(CPU_EXEC_CTRL0) | CPU_SECONDARY); 4540 report_prefix_pushf("Use TPR shadow enabled; secondary controls enabled; virtual-interrupt delivery enabled; virtualize APIC accesses enabled"); 4541 test_tpr_threshold_values(); 4542 report_prefix_pop(); 4543 } 4544 4545 if ((ctrl_cpu_rev[1].clr & 4546 (CPU_VINTD | CPU_VIRT_APIC_ACCESSES)) == 4547 (CPU_VINTD | CPU_VIRT_APIC_ACCESSES)) { 4548 vmcs_write(CPU_EXEC_CTRL0, 4549 vmcs_read(CPU_EXEC_CTRL0) & ~CPU_SECONDARY); 4550 vmcs_write(CPU_EXEC_CTRL1, 4551 CPU_VINTD | CPU_VIRT_APIC_ACCESSES); 4552 report_prefix_pushf("Use TPR shadow enabled; secondary controls disabled; virtual-interrupt delivery enabled; virtualize APIC accesses enabled"); 4553 test_tpr_threshold_values(); 4554 report_prefix_pop(); 4555 4556 vmcs_write(CPU_EXEC_CTRL0, 4557 vmcs_read(CPU_EXEC_CTRL0) | CPU_SECONDARY); 4558 report_prefix_pushf("Use TPR shadow enabled; secondary controls enabled; virtual-interrupt delivery enabled; virtualize APIC accesses enabled"); 4559 test_tpr_threshold_values(); 4560 report_prefix_pop(); 4561 } 4562 4563 vmcs_write(CPU_EXEC_CTRL1, secondary); 4564 out: 4565 vmcs_write(TPR_THRESHOLD, threshold); 4566 vmcs_write(APIC_VIRT_ADDR, apic_virt_addr); 4567 vmcs_write(CPU_EXEC_CTRL0, primary); 4568 } 4569 4570 /* 4571 * This test verifies the following two vmentry checks: 4572 * 4573 * If the "NMI exiting" VM-execution control is 0, "Virtual NMIs" 4574 * VM-execution control must be 0. 4575 * [Intel SDM] 4576 * 4577 * If the “virtual NMIs” VM-execution control is 0, the “NMI-window 4578 * exiting” VM-execution control must be 0. 4579 * [Intel SDM] 4580 */ 4581 static void test_nmi_ctrls(void) 4582 { 4583 u32 pin_ctrls, cpu_ctrls0, test_pin_ctrls, test_cpu_ctrls0; 4584 4585 if ((ctrl_pin_rev.clr & (PIN_NMI | PIN_VIRT_NMI)) != 4586 (PIN_NMI | PIN_VIRT_NMI)) { 4587 printf("NMI exiting and Virtual NMIs are not supported !\n"); 4588 return; 4589 } 4590 4591 /* Save the controls so that we can restore them after our tests */ 4592 pin_ctrls = vmcs_read(PIN_CONTROLS); 4593 cpu_ctrls0 = vmcs_read(CPU_EXEC_CTRL0); 4594 4595 test_pin_ctrls = pin_ctrls & ~(PIN_NMI | PIN_VIRT_NMI); 4596 test_cpu_ctrls0 = cpu_ctrls0 & ~CPU_NMI_WINDOW; 4597 4598 vmcs_write(PIN_CONTROLS, test_pin_ctrls); 4599 report_prefix_pushf("NMI-exiting disabled, virtual-NMIs disabled"); 4600 test_vmx_valid_controls(); 4601 report_prefix_pop(); 4602 4603 vmcs_write(PIN_CONTROLS, test_pin_ctrls | PIN_VIRT_NMI); 4604 report_prefix_pushf("NMI-exiting disabled, virtual-NMIs enabled"); 4605 test_vmx_invalid_controls(); 4606 report_prefix_pop(); 4607 4608 vmcs_write(PIN_CONTROLS, test_pin_ctrls | (PIN_NMI | PIN_VIRT_NMI)); 4609 report_prefix_pushf("NMI-exiting enabled, virtual-NMIs enabled"); 4610 test_vmx_valid_controls(); 4611 report_prefix_pop(); 4612 4613 vmcs_write(PIN_CONTROLS, test_pin_ctrls | PIN_NMI); 4614 report_prefix_pushf("NMI-exiting enabled, virtual-NMIs disabled"); 4615 test_vmx_valid_controls(); 4616 report_prefix_pop(); 4617 4618 if (!(ctrl_cpu_rev[0].clr & CPU_NMI_WINDOW)) { 4619 report_info("NMI-window exiting is not supported, skipping..."); 4620 goto done; 4621 } 4622 4623 vmcs_write(PIN_CONTROLS, test_pin_ctrls); 4624 vmcs_write(CPU_EXEC_CTRL0, test_cpu_ctrls0 | CPU_NMI_WINDOW); 4625 report_prefix_pushf("Virtual-NMIs disabled, NMI-window-exiting enabled"); 4626 test_vmx_invalid_controls(); 4627 report_prefix_pop(); 4628 4629 vmcs_write(PIN_CONTROLS, test_pin_ctrls); 4630 vmcs_write(CPU_EXEC_CTRL0, test_cpu_ctrls0); 4631 report_prefix_pushf("Virtual-NMIs disabled, NMI-window-exiting disabled"); 4632 test_vmx_valid_controls(); 4633 report_prefix_pop(); 4634 4635 vmcs_write(PIN_CONTROLS, test_pin_ctrls | (PIN_NMI | PIN_VIRT_NMI)); 4636 vmcs_write(CPU_EXEC_CTRL0, test_cpu_ctrls0 | CPU_NMI_WINDOW); 4637 report_prefix_pushf("Virtual-NMIs enabled, NMI-window-exiting enabled"); 4638 test_vmx_valid_controls(); 4639 report_prefix_pop(); 4640 4641 vmcs_write(PIN_CONTROLS, test_pin_ctrls | (PIN_NMI | PIN_VIRT_NMI)); 4642 vmcs_write(CPU_EXEC_CTRL0, test_cpu_ctrls0); 4643 report_prefix_pushf("Virtual-NMIs enabled, NMI-window-exiting disabled"); 4644 test_vmx_valid_controls(); 4645 report_prefix_pop(); 4646 4647 /* Restore the controls to their original values */ 4648 vmcs_write(CPU_EXEC_CTRL0, cpu_ctrls0); 4649 done: 4650 vmcs_write(PIN_CONTROLS, pin_ctrls); 4651 } 4652 4653 static void test_eptp_ad_bit(u64 eptp, bool ctrl) 4654 { 4655 vmcs_write(EPTP, eptp); 4656 report_prefix_pushf("Enable-EPT enabled; EPT accessed and dirty flag %s", 4657 (eptp & EPTP_AD_FLAG) ? "1": "0"); 4658 if (ctrl) 4659 test_vmx_valid_controls(); 4660 else 4661 test_vmx_invalid_controls(); 4662 report_prefix_pop(); 4663 4664 } 4665 4666 /* 4667 * 1. If the "enable EPT" VM-execution control is 1, the "EPTP VM-execution" 4668 * control field must satisfy the following checks: 4669 * 4670 * - The EPT memory type (bits 2:0) must be a value supported by the 4671 * processor as indicated in the IA32_VMX_EPT_VPID_CAP MSR. 4672 * - Bits 5:3 (1 less than the EPT page-walk length) must be 3, 4673 * indicating an EPT page-walk length of 4. 4674 * - Bit 6 (enable bit for accessed and dirty flags for EPT) must be 4675 * 0 if bit 21 of the IA32_VMX_EPT_VPID_CAP MSR is read as 0, 4676 * indicating that the processor does not support accessed and dirty 4677 * dirty flags for EPT. 4678 * - Reserved bits 11:7 and 63:N (where N is the processor's 4679 * physical-address width) must all be 0. 4680 * 4681 * 2. If the "unrestricted guest" VM-execution control is 1, the 4682 * "enable EPT" VM-execution control must also be 1. 4683 */ 4684 static void test_ept_eptp(void) 4685 { 4686 u32 primary_saved = vmcs_read(CPU_EXEC_CTRL0); 4687 u32 secondary_saved = vmcs_read(CPU_EXEC_CTRL1); 4688 u64 eptp_saved = vmcs_read(EPTP); 4689 u32 primary = primary_saved; 4690 u32 secondary = secondary_saved; 4691 u64 msr, eptp = eptp_saved; 4692 bool un_cache = false; 4693 bool wr_bk = false; 4694 bool ctrl; 4695 u32 i, maxphysaddr; 4696 u64 j, resv_bits_mask = 0; 4697 4698 if (!((ctrl_cpu_rev[0].clr & CPU_SECONDARY) && 4699 (ctrl_cpu_rev[1].clr & CPU_EPT))) { 4700 printf("\"CPU secondary\" and/or \"enable EPT\" execution controls are not supported !\n"); 4701 return; 4702 } 4703 4704 /* 4705 * Memory type (bits 2:0) 4706 */ 4707 msr = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP); 4708 if (msr & EPT_CAP_UC) 4709 un_cache = true; 4710 if (msr & EPT_CAP_WB) 4711 wr_bk = true; 4712 4713 primary |= CPU_SECONDARY; 4714 vmcs_write(CPU_EXEC_CTRL0, primary); 4715 secondary |= CPU_EPT; 4716 vmcs_write(CPU_EXEC_CTRL1, secondary); 4717 eptp = (eptp & ~EPTP_PG_WALK_LEN_MASK) | 4718 (3ul << EPTP_PG_WALK_LEN_SHIFT); 4719 vmcs_write(EPTP, eptp); 4720 4721 for (i = 0; i < 8; i++) { 4722 if (i == 0) { 4723 if (un_cache) { 4724 report_info("EPT paging structure memory-type is Un-cacheable\n"); 4725 ctrl = true; 4726 } else { 4727 ctrl = false; 4728 } 4729 } else if (i == 6) { 4730 if (wr_bk) { 4731 report_info("EPT paging structure memory-type is Write-back\n"); 4732 ctrl = true; 4733 } else { 4734 ctrl = false; 4735 } 4736 } else { 4737 ctrl = false; 4738 } 4739 4740 eptp = (eptp & ~EPT_MEM_TYPE_MASK) | i; 4741 vmcs_write(EPTP, eptp); 4742 report_prefix_pushf("Enable-EPT enabled; EPT memory type %lu", 4743 eptp & EPT_MEM_TYPE_MASK); 4744 if (ctrl) 4745 test_vmx_valid_controls(); 4746 else 4747 test_vmx_invalid_controls(); 4748 report_prefix_pop(); 4749 } 4750 4751 eptp = (eptp & ~EPT_MEM_TYPE_MASK) | 6ul; 4752 4753 /* 4754 * Page walk length (bits 5:3) 4755 */ 4756 for (i = 0; i < 8; i++) { 4757 eptp = (eptp & ~EPTP_PG_WALK_LEN_MASK) | 4758 (i << EPTP_PG_WALK_LEN_SHIFT); 4759 if (i == 3) 4760 ctrl = true; 4761 else 4762 ctrl = false; 4763 4764 vmcs_write(EPTP, eptp); 4765 report_prefix_pushf("Enable-EPT enabled; EPT page walk length %lu", 4766 eptp & EPTP_PG_WALK_LEN_MASK); 4767 if (ctrl) 4768 test_vmx_valid_controls(); 4769 else 4770 test_vmx_invalid_controls(); 4771 report_prefix_pop(); 4772 } 4773 4774 eptp = (eptp & ~EPTP_PG_WALK_LEN_MASK) | 4775 3ul << EPTP_PG_WALK_LEN_SHIFT; 4776 4777 /* 4778 * Accessed and dirty flag (bit 6) 4779 */ 4780 if (msr & EPT_CAP_AD_FLAG) { 4781 report_info("Processor supports accessed and dirty flag"); 4782 eptp &= ~EPTP_AD_FLAG; 4783 test_eptp_ad_bit(eptp, true); 4784 4785 eptp |= EPTP_AD_FLAG; 4786 test_eptp_ad_bit(eptp, true); 4787 } else { 4788 report_info("Processor does not supports accessed and dirty flag"); 4789 eptp &= ~EPTP_AD_FLAG; 4790 test_eptp_ad_bit(eptp, true); 4791 4792 eptp |= EPTP_AD_FLAG; 4793 test_eptp_ad_bit(eptp, false); 4794 } 4795 4796 /* 4797 * Reserved bits [11:7] and [63:N] 4798 */ 4799 for (i = 0; i < 32; i++) { 4800 eptp = (eptp & 4801 ~(EPTP_RESERV_BITS_MASK << EPTP_RESERV_BITS_SHIFT)) | 4802 (i << EPTP_RESERV_BITS_SHIFT); 4803 vmcs_write(EPTP, eptp); 4804 report_prefix_pushf("Enable-EPT enabled; reserved bits [11:7] %lu", 4805 (eptp >> EPTP_RESERV_BITS_SHIFT) & 4806 EPTP_RESERV_BITS_MASK); 4807 if (i == 0) 4808 test_vmx_valid_controls(); 4809 else 4810 test_vmx_invalid_controls(); 4811 report_prefix_pop(); 4812 } 4813 4814 eptp = (eptp & ~(EPTP_RESERV_BITS_MASK << EPTP_RESERV_BITS_SHIFT)); 4815 4816 maxphysaddr = cpuid_maxphyaddr(); 4817 for (i = 0; i < (63 - maxphysaddr + 1); i++) { 4818 resv_bits_mask |= 1ul << i; 4819 } 4820 4821 for (j = maxphysaddr - 1; j <= 63; j++) { 4822 eptp = (eptp & ~(resv_bits_mask << maxphysaddr)) | 4823 (j < maxphysaddr ? 0 : 1ul << j); 4824 vmcs_write(EPTP, eptp); 4825 report_prefix_pushf("Enable-EPT enabled; reserved bits [63:N] %lu", 4826 (eptp >> maxphysaddr) & resv_bits_mask); 4827 if (j < maxphysaddr) 4828 test_vmx_valid_controls(); 4829 else 4830 test_vmx_invalid_controls(); 4831 report_prefix_pop(); 4832 } 4833 4834 secondary &= ~(CPU_EPT | CPU_URG); 4835 vmcs_write(CPU_EXEC_CTRL1, secondary); 4836 report_prefix_pushf("Enable-EPT disabled, unrestricted-guest disabled"); 4837 test_vmx_valid_controls(); 4838 report_prefix_pop(); 4839 4840 if (!(ctrl_cpu_rev[1].clr & CPU_URG)) 4841 goto skip_unrestricted_guest; 4842 4843 secondary |= CPU_URG; 4844 vmcs_write(CPU_EXEC_CTRL1, secondary); 4845 report_prefix_pushf("Enable-EPT disabled, unrestricted-guest enabled"); 4846 test_vmx_invalid_controls(); 4847 report_prefix_pop(); 4848 4849 secondary |= CPU_EPT; 4850 setup_dummy_ept(); 4851 report_prefix_pushf("Enable-EPT enabled, unrestricted-guest enabled"); 4852 test_vmx_valid_controls(); 4853 report_prefix_pop(); 4854 4855 skip_unrestricted_guest: 4856 secondary &= ~CPU_URG; 4857 vmcs_write(CPU_EXEC_CTRL1, secondary); 4858 report_prefix_pushf("Enable-EPT enabled, unrestricted-guest disabled"); 4859 test_vmx_valid_controls(); 4860 report_prefix_pop(); 4861 4862 vmcs_write(CPU_EXEC_CTRL0, primary_saved); 4863 vmcs_write(CPU_EXEC_CTRL1, secondary_saved); 4864 vmcs_write(EPTP, eptp_saved); 4865 } 4866 4867 /* 4868 * If the 'enable PML' VM-execution control is 1, the 'enable EPT' 4869 * VM-execution control must also be 1. In addition, the PML address 4870 * must satisfy the following checks: 4871 * 4872 * * Bits 11:0 of the address must be 0. 4873 * * The address should not set any bits beyond the processor's 4874 * physical-address width. 4875 * 4876 * [Intel SDM] 4877 */ 4878 static void test_pml(void) 4879 { 4880 u32 primary_saved = vmcs_read(CPU_EXEC_CTRL0); 4881 u32 secondary_saved = vmcs_read(CPU_EXEC_CTRL1); 4882 u32 primary = primary_saved; 4883 u32 secondary = secondary_saved; 4884 4885 if (!((ctrl_cpu_rev[0].clr & CPU_SECONDARY) && 4886 (ctrl_cpu_rev[1].clr & CPU_EPT) && (ctrl_cpu_rev[1].clr & CPU_PML))) { 4887 printf("\"Secondary execution\" control or \"enable EPT\" control or \"enable PML\" control is not supported !\n"); 4888 return; 4889 } 4890 4891 primary |= CPU_SECONDARY; 4892 vmcs_write(CPU_EXEC_CTRL0, primary); 4893 secondary &= ~(CPU_PML | CPU_EPT); 4894 vmcs_write(CPU_EXEC_CTRL1, secondary); 4895 report_prefix_pushf("enable-PML disabled, enable-EPT disabled"); 4896 test_vmx_valid_controls(); 4897 report_prefix_pop(); 4898 4899 secondary |= CPU_PML; 4900 vmcs_write(CPU_EXEC_CTRL1, secondary); 4901 report_prefix_pushf("enable-PML enabled, enable-EPT disabled"); 4902 test_vmx_invalid_controls(); 4903 report_prefix_pop(); 4904 4905 secondary |= CPU_EPT; 4906 setup_dummy_ept(); 4907 report_prefix_pushf("enable-PML enabled, enable-EPT enabled"); 4908 test_vmx_valid_controls(); 4909 report_prefix_pop(); 4910 4911 secondary &= ~CPU_PML; 4912 vmcs_write(CPU_EXEC_CTRL1, secondary); 4913 report_prefix_pushf("enable-PML disabled, enable EPT enabled"); 4914 test_vmx_valid_controls(); 4915 report_prefix_pop(); 4916 4917 test_vmcs_addr_reference(CPU_PML, PMLADDR, "PML address", "PML", 4918 PAGE_SIZE, false, false); 4919 4920 vmcs_write(CPU_EXEC_CTRL0, primary_saved); 4921 vmcs_write(CPU_EXEC_CTRL1, secondary_saved); 4922 } 4923 4924 /* 4925 * If the "activate VMX-preemption timer" VM-execution control is 0, the 4926 * the "save VMX-preemption timer value" VM-exit control must also be 0. 4927 * 4928 * [Intel SDM] 4929 */ 4930 static void test_vmx_preemption_timer(void) 4931 { 4932 u32 saved_pin = vmcs_read(PIN_CONTROLS); 4933 u32 saved_exit = vmcs_read(EXI_CONTROLS); 4934 u32 pin = saved_pin; 4935 u32 exit = saved_exit; 4936 4937 if (!((ctrl_exit_rev.clr & EXI_SAVE_PREEMPT) || 4938 (ctrl_pin_rev.clr & PIN_PREEMPT))) { 4939 printf("\"Save-VMX-preemption-timer\" control and/or \"Enable-VMX-preemption-timer\" control is not supported\n"); 4940 return; 4941 } 4942 4943 pin |= PIN_PREEMPT; 4944 vmcs_write(PIN_CONTROLS, pin); 4945 exit &= ~EXI_SAVE_PREEMPT; 4946 vmcs_write(EXI_CONTROLS, exit); 4947 report_prefix_pushf("enable-VMX-preemption-timer enabled, save-VMX-preemption-timer disabled"); 4948 test_vmx_valid_controls(); 4949 report_prefix_pop(); 4950 4951 exit |= EXI_SAVE_PREEMPT; 4952 vmcs_write(EXI_CONTROLS, exit); 4953 report_prefix_pushf("enable-VMX-preemption-timer enabled, save-VMX-preemption-timer enabled"); 4954 test_vmx_valid_controls(); 4955 report_prefix_pop(); 4956 4957 pin &= ~PIN_PREEMPT; 4958 vmcs_write(PIN_CONTROLS, pin); 4959 report_prefix_pushf("enable-VMX-preemption-timer disabled, save-VMX-preemption-timer enabled"); 4960 test_vmx_invalid_controls(); 4961 report_prefix_pop(); 4962 4963 exit &= ~EXI_SAVE_PREEMPT; 4964 vmcs_write(EXI_CONTROLS, exit); 4965 report_prefix_pushf("enable-VMX-preemption-timer disabled, save-VMX-preemption-timer disabled"); 4966 test_vmx_valid_controls(); 4967 report_prefix_pop(); 4968 4969 vmcs_write(PIN_CONTROLS, saved_pin); 4970 vmcs_write(EXI_CONTROLS, saved_exit); 4971 } 4972 4973 /* 4974 * Tests for VM-execution control fields 4975 */ 4976 static void test_vm_execution_ctls(void) 4977 { 4978 test_pin_based_ctls(); 4979 test_primary_processor_based_ctls(); 4980 test_secondary_processor_based_ctls(); 4981 test_cr3_targets(); 4982 test_io_bitmaps(); 4983 test_msr_bitmap(); 4984 test_apic_ctls(); 4985 test_tpr_threshold(); 4986 test_nmi_ctrls(); 4987 test_pml(); 4988 test_vpid(); 4989 test_ept_eptp(); 4990 test_vmx_preemption_timer(); 4991 } 4992 4993 /* 4994 * The following checks are performed for the VM-entry MSR-load address if 4995 * the VM-entry MSR-load count field is non-zero: 4996 * 4997 * - The lower 4 bits of the VM-entry MSR-load address must be 0. 4998 * The address should not set any bits beyond the processor’s 4999 * physical-address width. 5000 * 5001 * - The address of the last byte in the VM-entry MSR-load area 5002 * should not set any bits beyond the processor’s physical-address 5003 * width. The address of this last byte is VM-entry MSR-load address 5004 * + (MSR count * 16) - 1. (The arithmetic used for the computation 5005 * uses more bits than the processor’s physical-address width.) 5006 * 5007 * 5008 * [Intel SDM] 5009 */ 5010 static void test_entry_msr_load(void) 5011 { 5012 entry_msr_load = alloc_page(); 5013 u64 tmp; 5014 u32 entry_msr_ld_cnt = 1; 5015 int i; 5016 u32 addr_len = 64; 5017 5018 vmcs_write(ENT_MSR_LD_CNT, entry_msr_ld_cnt); 5019 5020 /* Check first 4 bits of VM-entry MSR-load address */ 5021 for (i = 0; i < 4; i++) { 5022 tmp = (u64)entry_msr_load | 1ull << i; 5023 vmcs_write(ENTER_MSR_LD_ADDR, tmp); 5024 report_prefix_pushf("VM-entry MSR-load addr [4:0] %lx", 5025 tmp & 0xf); 5026 test_vmx_invalid_controls(); 5027 report_prefix_pop(); 5028 } 5029 5030 if (basic.val & (1ul << 48)) 5031 addr_len = 32; 5032 5033 test_vmcs_addr_values("VM-entry-MSR-load address", 5034 ENTER_MSR_LD_ADDR, 16, false, false, 5035 4, addr_len - 1); 5036 5037 /* 5038 * Check last byte of VM-entry MSR-load address 5039 */ 5040 entry_msr_load = (struct vmx_msr_entry *)((u64)entry_msr_load & ~0xf); 5041 5042 for (i = (addr_len == 64 ? cpuid_maxphyaddr(): addr_len); 5043 i < 64; i++) { 5044 tmp = ((u64)entry_msr_load + entry_msr_ld_cnt * 16 - 1) | 5045 1ul << i; 5046 vmcs_write(ENTER_MSR_LD_ADDR, 5047 tmp - (entry_msr_ld_cnt * 16 - 1)); 5048 test_vmx_invalid_controls(); 5049 } 5050 5051 vmcs_write(ENT_MSR_LD_CNT, 2); 5052 vmcs_write(ENTER_MSR_LD_ADDR, (1ULL << cpuid_maxphyaddr()) - 16); 5053 test_vmx_invalid_controls(); 5054 vmcs_write(ENTER_MSR_LD_ADDR, (1ULL << cpuid_maxphyaddr()) - 32); 5055 test_vmx_valid_controls(); 5056 vmcs_write(ENTER_MSR_LD_ADDR, (1ULL << cpuid_maxphyaddr()) - 48); 5057 test_vmx_valid_controls(); 5058 } 5059 5060 static struct vmx_state_area_test_data { 5061 u32 msr; 5062 u64 exp; 5063 bool enabled; 5064 } vmx_state_area_test_data; 5065 5066 static void guest_state_test_main(void) 5067 { 5068 u64 obs; 5069 struct vmx_state_area_test_data *data = &vmx_state_area_test_data; 5070 5071 while (1) { 5072 if (vmx_get_test_stage() == 2) 5073 break; 5074 5075 if (data->enabled) { 5076 obs = rdmsr(data->msr); 5077 report(data->exp == obs, 5078 "Guest state is 0x%lx (expected 0x%lx)", 5079 obs, data->exp); 5080 } 5081 5082 vmcall(); 5083 } 5084 5085 asm volatile("fnop"); 5086 } 5087 5088 static void advance_guest_state_test(void) 5089 { 5090 u32 reason = vmcs_read(EXI_REASON); 5091 if (! (reason & 0x80000000)) { 5092 u64 guest_rip = vmcs_read(GUEST_RIP); 5093 u32 insn_len = vmcs_read(EXI_INST_LEN); 5094 vmcs_write(GUEST_RIP, guest_rip + insn_len); 5095 } 5096 } 5097 5098 static void report_guest_state_test(const char *test, u32 xreason, 5099 u64 field, const char * field_name) 5100 { 5101 u32 reason = vmcs_read(EXI_REASON); 5102 5103 report(reason == xreason, "%s, %s %lx", test, field_name, field); 5104 advance_guest_state_test(); 5105 } 5106 5107 /* 5108 * Tests for VM-entry control fields 5109 */ 5110 static void test_vm_entry_ctls(void) 5111 { 5112 test_invalid_event_injection(); 5113 test_entry_msr_load(); 5114 } 5115 5116 /* 5117 * The following checks are performed for the VM-exit MSR-store address if 5118 * the VM-exit MSR-store count field is non-zero: 5119 * 5120 * - The lower 4 bits of the VM-exit MSR-store address must be 0. 5121 * The address should not set any bits beyond the processor’s 5122 * physical-address width. 5123 * 5124 * - The address of the last byte in the VM-exit MSR-store area 5125 * should not set any bits beyond the processor’s physical-address 5126 * width. The address of this last byte is VM-exit MSR-store address 5127 * + (MSR count * 16) - 1. (The arithmetic used for the computation 5128 * uses more bits than the processor’s physical-address width.) 5129 * 5130 * If IA32_VMX_BASIC[48] is read as 1, neither address should set any bits 5131 * in the range 63:32. 5132 * 5133 * [Intel SDM] 5134 */ 5135 static void test_exit_msr_store(void) 5136 { 5137 exit_msr_store = alloc_page(); 5138 u64 tmp; 5139 u32 exit_msr_st_cnt = 1; 5140 int i; 5141 u32 addr_len = 64; 5142 5143 vmcs_write(EXI_MSR_ST_CNT, exit_msr_st_cnt); 5144 5145 /* Check first 4 bits of VM-exit MSR-store address */ 5146 for (i = 0; i < 4; i++) { 5147 tmp = (u64)exit_msr_store | 1ull << i; 5148 vmcs_write(EXIT_MSR_ST_ADDR, tmp); 5149 report_prefix_pushf("VM-exit MSR-store addr [4:0] %lx", 5150 tmp & 0xf); 5151 test_vmx_invalid_controls(); 5152 report_prefix_pop(); 5153 } 5154 5155 if (basic.val & (1ul << 48)) 5156 addr_len = 32; 5157 5158 test_vmcs_addr_values("VM-exit-MSR-store address", 5159 EXIT_MSR_ST_ADDR, 16, false, false, 5160 4, addr_len - 1); 5161 5162 /* 5163 * Check last byte of VM-exit MSR-store address 5164 */ 5165 exit_msr_store = (struct vmx_msr_entry *)((u64)exit_msr_store & ~0xf); 5166 5167 for (i = (addr_len == 64 ? cpuid_maxphyaddr(): addr_len); 5168 i < 64; i++) { 5169 tmp = ((u64)exit_msr_store + exit_msr_st_cnt * 16 - 1) | 5170 1ul << i; 5171 vmcs_write(EXIT_MSR_ST_ADDR, 5172 tmp - (exit_msr_st_cnt * 16 - 1)); 5173 test_vmx_invalid_controls(); 5174 } 5175 5176 vmcs_write(EXI_MSR_ST_CNT, 2); 5177 vmcs_write(EXIT_MSR_ST_ADDR, (1ULL << cpuid_maxphyaddr()) - 16); 5178 test_vmx_invalid_controls(); 5179 vmcs_write(EXIT_MSR_ST_ADDR, (1ULL << cpuid_maxphyaddr()) - 32); 5180 test_vmx_valid_controls(); 5181 vmcs_write(EXIT_MSR_ST_ADDR, (1ULL << cpuid_maxphyaddr()) - 48); 5182 test_vmx_valid_controls(); 5183 } 5184 5185 /* 5186 * Tests for VM-exit controls 5187 */ 5188 static void test_vm_exit_ctls(void) 5189 { 5190 test_exit_msr_store(); 5191 } 5192 5193 /* 5194 * Check that the virtual CPU checks all of the VMX controls as 5195 * documented in the Intel SDM. 5196 */ 5197 static void vmx_controls_test(void) 5198 { 5199 /* 5200 * Bit 1 of the guest's RFLAGS must be 1, or VM-entry will 5201 * fail due to invalid guest state, should we make it that 5202 * far. 5203 */ 5204 vmcs_write(GUEST_RFLAGS, 0); 5205 5206 test_vm_execution_ctls(); 5207 test_vm_exit_ctls(); 5208 test_vm_entry_ctls(); 5209 } 5210 5211 struct apic_reg_virt_config { 5212 bool apic_register_virtualization; 5213 bool use_tpr_shadow; 5214 bool virtualize_apic_accesses; 5215 bool virtualize_x2apic_mode; 5216 bool activate_secondary_controls; 5217 }; 5218 5219 struct apic_reg_test { 5220 const char *name; 5221 struct apic_reg_virt_config apic_reg_virt_config; 5222 }; 5223 5224 struct apic_reg_virt_expectation { 5225 enum Reason rd_exit_reason; 5226 enum Reason wr_exit_reason; 5227 u32 val; 5228 u32 (*virt_fn)(u32); 5229 5230 /* 5231 * If false, accessing the APIC access address from L2 is treated as a 5232 * normal memory operation, rather than triggering virtualization. 5233 */ 5234 bool virtualize_apic_accesses; 5235 }; 5236 5237 static u32 apic_virt_identity(u32 val) 5238 { 5239 return val; 5240 } 5241 5242 static u32 apic_virt_nibble1(u32 val) 5243 { 5244 return val & 0xf0; 5245 } 5246 5247 static u32 apic_virt_byte3(u32 val) 5248 { 5249 return val & (0xff << 24); 5250 } 5251 5252 static bool apic_reg_virt_exit_expectation( 5253 u32 reg, struct apic_reg_virt_config *config, 5254 struct apic_reg_virt_expectation *expectation) 5255 { 5256 /* Good configs, where some L2 APIC accesses are virtualized. */ 5257 bool virtualize_apic_accesses_only = 5258 config->virtualize_apic_accesses && 5259 !config->use_tpr_shadow && 5260 !config->apic_register_virtualization && 5261 !config->virtualize_x2apic_mode && 5262 config->activate_secondary_controls; 5263 bool virtualize_apic_accesses_and_use_tpr_shadow = 5264 config->virtualize_apic_accesses && 5265 config->use_tpr_shadow && 5266 !config->apic_register_virtualization && 5267 !config->virtualize_x2apic_mode && 5268 config->activate_secondary_controls; 5269 bool apic_register_virtualization = 5270 config->virtualize_apic_accesses && 5271 config->use_tpr_shadow && 5272 config->apic_register_virtualization && 5273 !config->virtualize_x2apic_mode && 5274 config->activate_secondary_controls; 5275 5276 expectation->val = MAGIC_VAL_1; 5277 expectation->virt_fn = apic_virt_identity; 5278 expectation->virtualize_apic_accesses = 5279 config->virtualize_apic_accesses && 5280 config->activate_secondary_controls; 5281 if (virtualize_apic_accesses_only) { 5282 expectation->rd_exit_reason = VMX_APIC_ACCESS; 5283 expectation->wr_exit_reason = VMX_APIC_ACCESS; 5284 } else if (virtualize_apic_accesses_and_use_tpr_shadow) { 5285 switch (reg) { 5286 case APIC_TASKPRI: 5287 expectation->rd_exit_reason = VMX_VMCALL; 5288 expectation->wr_exit_reason = VMX_VMCALL; 5289 expectation->virt_fn = apic_virt_nibble1; 5290 break; 5291 default: 5292 expectation->rd_exit_reason = VMX_APIC_ACCESS; 5293 expectation->wr_exit_reason = VMX_APIC_ACCESS; 5294 } 5295 } else if (apic_register_virtualization) { 5296 expectation->rd_exit_reason = VMX_VMCALL; 5297 5298 switch (reg) { 5299 case APIC_ID: 5300 case APIC_EOI: 5301 case APIC_LDR: 5302 case APIC_DFR: 5303 case APIC_SPIV: 5304 case APIC_ESR: 5305 case APIC_ICR: 5306 case APIC_LVTT: 5307 case APIC_LVTTHMR: 5308 case APIC_LVTPC: 5309 case APIC_LVT0: 5310 case APIC_LVT1: 5311 case APIC_LVTERR: 5312 case APIC_TMICT: 5313 case APIC_TDCR: 5314 expectation->wr_exit_reason = VMX_APIC_WRITE; 5315 break; 5316 case APIC_LVR: 5317 case APIC_ISR ... APIC_ISR + 0x70: 5318 case APIC_TMR ... APIC_TMR + 0x70: 5319 case APIC_IRR ... APIC_IRR + 0x70: 5320 expectation->wr_exit_reason = VMX_APIC_ACCESS; 5321 break; 5322 case APIC_TASKPRI: 5323 expectation->wr_exit_reason = VMX_VMCALL; 5324 expectation->virt_fn = apic_virt_nibble1; 5325 break; 5326 case APIC_ICR2: 5327 expectation->wr_exit_reason = VMX_VMCALL; 5328 expectation->virt_fn = apic_virt_byte3; 5329 break; 5330 default: 5331 expectation->rd_exit_reason = VMX_APIC_ACCESS; 5332 expectation->wr_exit_reason = VMX_APIC_ACCESS; 5333 } 5334 } else if (!expectation->virtualize_apic_accesses) { 5335 /* 5336 * No APIC registers are directly virtualized. This includes 5337 * VTPR, which can be virtualized through MOV to/from CR8 via 5338 * the use TPR shadow control, but not through directly 5339 * accessing VTPR. 5340 */ 5341 expectation->rd_exit_reason = VMX_VMCALL; 5342 expectation->wr_exit_reason = VMX_VMCALL; 5343 } else { 5344 printf("Cannot parse APIC register virtualization config:\n" 5345 "\tvirtualize_apic_accesses: %d\n" 5346 "\tuse_tpr_shadow: %d\n" 5347 "\tapic_register_virtualization: %d\n" 5348 "\tvirtualize_x2apic_mode: %d\n" 5349 "\tactivate_secondary_controls: %d\n", 5350 config->virtualize_apic_accesses, 5351 config->use_tpr_shadow, 5352 config->apic_register_virtualization, 5353 config->virtualize_x2apic_mode, 5354 config->activate_secondary_controls); 5355 5356 return false; 5357 } 5358 5359 return true; 5360 } 5361 5362 struct apic_reg_test apic_reg_tests[] = { 5363 /* Good configs, where some L2 APIC accesses are virtualized. */ 5364 { 5365 .name = "Virtualize APIC accesses", 5366 .apic_reg_virt_config = { 5367 .virtualize_apic_accesses = true, 5368 .use_tpr_shadow = false, 5369 .apic_register_virtualization = false, 5370 .virtualize_x2apic_mode = false, 5371 .activate_secondary_controls = true, 5372 }, 5373 }, 5374 { 5375 .name = "Virtualize APIC accesses + Use TPR shadow", 5376 .apic_reg_virt_config = { 5377 .virtualize_apic_accesses = true, 5378 .use_tpr_shadow = true, 5379 .apic_register_virtualization = false, 5380 .virtualize_x2apic_mode = false, 5381 .activate_secondary_controls = true, 5382 }, 5383 }, 5384 { 5385 .name = "APIC-register virtualization", 5386 .apic_reg_virt_config = { 5387 .virtualize_apic_accesses = true, 5388 .use_tpr_shadow = true, 5389 .apic_register_virtualization = true, 5390 .virtualize_x2apic_mode = false, 5391 .activate_secondary_controls = true, 5392 }, 5393 }, 5394 5395 /* 5396 * Test that the secondary processor-based VM-execution controls are 5397 * correctly ignored when "activate secondary controls" is disabled. 5398 */ 5399 { 5400 .name = "Activate secondary controls off", 5401 .apic_reg_virt_config = { 5402 .virtualize_apic_accesses = true, 5403 .use_tpr_shadow = false, 5404 .apic_register_virtualization = true, 5405 .virtualize_x2apic_mode = true, 5406 .activate_secondary_controls = false, 5407 }, 5408 }, 5409 { 5410 .name = "Activate secondary controls off + Use TPR shadow", 5411 .apic_reg_virt_config = { 5412 .virtualize_apic_accesses = true, 5413 .use_tpr_shadow = true, 5414 .apic_register_virtualization = true, 5415 .virtualize_x2apic_mode = true, 5416 .activate_secondary_controls = false, 5417 }, 5418 }, 5419 5420 /* 5421 * Test that the APIC access address is treated like an arbitrary memory 5422 * address when "virtualize APIC accesses" is disabled. 5423 */ 5424 { 5425 .name = "Virtualize APIC accesses off + Use TPR shadow", 5426 .apic_reg_virt_config = { 5427 .virtualize_apic_accesses = false, 5428 .use_tpr_shadow = true, 5429 .apic_register_virtualization = true, 5430 .virtualize_x2apic_mode = true, 5431 .activate_secondary_controls = true, 5432 }, 5433 }, 5434 5435 /* 5436 * Test that VM entry fails due to invalid controls when 5437 * "APIC-register virtualization" is enabled while "use TPR shadow" is 5438 * disabled. 5439 */ 5440 { 5441 .name = "APIC-register virtualization + Use TPR shadow off", 5442 .apic_reg_virt_config = { 5443 .virtualize_apic_accesses = true, 5444 .use_tpr_shadow = false, 5445 .apic_register_virtualization = true, 5446 .virtualize_x2apic_mode = false, 5447 .activate_secondary_controls = true, 5448 }, 5449 }, 5450 5451 /* 5452 * Test that VM entry fails due to invalid controls when 5453 * "Virtualize x2APIC mode" is enabled while "use TPR shadow" is 5454 * disabled. 5455 */ 5456 { 5457 .name = "Virtualize x2APIC mode + Use TPR shadow off", 5458 .apic_reg_virt_config = { 5459 .virtualize_apic_accesses = false, 5460 .use_tpr_shadow = false, 5461 .apic_register_virtualization = false, 5462 .virtualize_x2apic_mode = true, 5463 .activate_secondary_controls = true, 5464 }, 5465 }, 5466 { 5467 .name = "Virtualize x2APIC mode + Use TPR shadow off v2", 5468 .apic_reg_virt_config = { 5469 .virtualize_apic_accesses = false, 5470 .use_tpr_shadow = false, 5471 .apic_register_virtualization = true, 5472 .virtualize_x2apic_mode = true, 5473 .activate_secondary_controls = true, 5474 }, 5475 }, 5476 5477 /* 5478 * Test that VM entry fails due to invalid controls when 5479 * "virtualize x2APIC mode" is enabled while "virtualize APIC accesses" 5480 * is enabled. 5481 */ 5482 { 5483 .name = "Virtualize x2APIC mode + Virtualize APIC accesses", 5484 .apic_reg_virt_config = { 5485 .virtualize_apic_accesses = true, 5486 .use_tpr_shadow = true, 5487 .apic_register_virtualization = false, 5488 .virtualize_x2apic_mode = true, 5489 .activate_secondary_controls = true, 5490 }, 5491 }, 5492 { 5493 .name = "Virtualize x2APIC mode + Virtualize APIC accesses v2", 5494 .apic_reg_virt_config = { 5495 .virtualize_apic_accesses = true, 5496 .use_tpr_shadow = true, 5497 .apic_register_virtualization = true, 5498 .virtualize_x2apic_mode = true, 5499 .activate_secondary_controls = true, 5500 }, 5501 }, 5502 }; 5503 5504 enum Apic_op { 5505 APIC_OP_XAPIC_RD, 5506 APIC_OP_XAPIC_WR, 5507 TERMINATE, 5508 }; 5509 5510 static u32 vmx_xapic_read(u32 *apic_access_address, u32 reg) 5511 { 5512 return *(volatile u32 *)((uintptr_t)apic_access_address + reg); 5513 } 5514 5515 static void vmx_xapic_write(u32 *apic_access_address, u32 reg, u32 val) 5516 { 5517 *(volatile u32 *)((uintptr_t)apic_access_address + reg) = val; 5518 } 5519 5520 struct apic_reg_virt_guest_args { 5521 enum Apic_op op; 5522 u32 *apic_access_address; 5523 u32 reg; 5524 u32 val; 5525 bool check_rd; 5526 u32 (*virt_fn)(u32); 5527 } apic_reg_virt_guest_args; 5528 5529 static void apic_reg_virt_guest(void) 5530 { 5531 volatile struct apic_reg_virt_guest_args *args = 5532 &apic_reg_virt_guest_args; 5533 5534 for (;;) { 5535 enum Apic_op op = args->op; 5536 u32 *apic_access_address = args->apic_access_address; 5537 u32 reg = args->reg; 5538 u32 val = args->val; 5539 bool check_rd = args->check_rd; 5540 u32 (*virt_fn)(u32) = args->virt_fn; 5541 5542 if (op == TERMINATE) 5543 break; 5544 5545 if (op == APIC_OP_XAPIC_RD) { 5546 u32 ret = vmx_xapic_read(apic_access_address, reg); 5547 5548 if (check_rd) { 5549 u32 want = virt_fn(val); 5550 u32 got = virt_fn(ret); 5551 5552 report(got == want, 5553 "read 0x%x, expected 0x%x.", got, want); 5554 } 5555 } else if (op == APIC_OP_XAPIC_WR) { 5556 vmx_xapic_write(apic_access_address, reg, val); 5557 } 5558 5559 /* 5560 * The L1 should always execute a vmcall after it's done testing 5561 * an individual APIC operation. This helps to validate that the 5562 * L1 and L2 are in sync with each other, as expected. 5563 */ 5564 vmcall(); 5565 } 5566 } 5567 5568 static void test_xapic_rd( 5569 u32 reg, struct apic_reg_virt_expectation *expectation, 5570 u32 *apic_access_address, u32 *virtual_apic_page) 5571 { 5572 u32 val = expectation->val; 5573 u32 exit_reason_want = expectation->rd_exit_reason; 5574 struct apic_reg_virt_guest_args *args = &apic_reg_virt_guest_args; 5575 5576 report_prefix_pushf("xapic - reading 0x%03x", reg); 5577 5578 /* Configure guest to do an xapic read */ 5579 args->op = APIC_OP_XAPIC_RD; 5580 args->apic_access_address = apic_access_address; 5581 args->reg = reg; 5582 args->val = val; 5583 args->check_rd = exit_reason_want == VMX_VMCALL; 5584 args->virt_fn = expectation->virt_fn; 5585 5586 /* Setup virtual APIC page */ 5587 if (!expectation->virtualize_apic_accesses) { 5588 apic_access_address[apic_reg_index(reg)] = val; 5589 virtual_apic_page[apic_reg_index(reg)] = 0; 5590 } else if (exit_reason_want == VMX_VMCALL) { 5591 apic_access_address[apic_reg_index(reg)] = 0; 5592 virtual_apic_page[apic_reg_index(reg)] = val; 5593 } 5594 5595 /* Enter guest */ 5596 enter_guest(); 5597 5598 /* 5599 * Validate the behavior and 5600 * pass a magic value back to the guest. 5601 */ 5602 if (exit_reason_want == VMX_APIC_ACCESS) { 5603 u32 apic_page_offset = vmcs_read(EXI_QUALIFICATION) & 0xfff; 5604 5605 assert_exit_reason(exit_reason_want); 5606 report(apic_page_offset == reg, 5607 "got APIC access exit @ page offset 0x%03x, want 0x%03x", 5608 apic_page_offset, reg); 5609 skip_exit_insn(); 5610 5611 /* Reenter guest so it can consume/check rcx and exit again. */ 5612 enter_guest(); 5613 } else if (exit_reason_want != VMX_VMCALL) { 5614 report(false, "Oops, bad exit expectation: %u.", 5615 exit_reason_want); 5616 } 5617 5618 skip_exit_vmcall(); 5619 report_prefix_pop(); 5620 } 5621 5622 static void test_xapic_wr( 5623 u32 reg, struct apic_reg_virt_expectation *expectation, 5624 u32 *apic_access_address, u32 *virtual_apic_page) 5625 { 5626 u32 val = expectation->val; 5627 u32 exit_reason_want = expectation->wr_exit_reason; 5628 struct apic_reg_virt_guest_args *args = &apic_reg_virt_guest_args; 5629 bool virtualized = 5630 expectation->virtualize_apic_accesses && 5631 (exit_reason_want == VMX_APIC_WRITE || 5632 exit_reason_want == VMX_VMCALL); 5633 bool checked = false; 5634 5635 report_prefix_pushf("xapic - writing 0x%x to 0x%03x", val, reg); 5636 5637 /* Configure guest to do an xapic read */ 5638 args->op = APIC_OP_XAPIC_WR; 5639 args->apic_access_address = apic_access_address; 5640 args->reg = reg; 5641 args->val = val; 5642 5643 /* Setup virtual APIC page */ 5644 if (virtualized || !expectation->virtualize_apic_accesses) { 5645 apic_access_address[apic_reg_index(reg)] = 0; 5646 virtual_apic_page[apic_reg_index(reg)] = 0; 5647 } 5648 5649 /* Enter guest */ 5650 enter_guest(); 5651 5652 /* 5653 * Validate the behavior and 5654 * pass a magic value back to the guest. 5655 */ 5656 if (exit_reason_want == VMX_APIC_ACCESS) { 5657 u32 apic_page_offset = vmcs_read(EXI_QUALIFICATION) & 0xfff; 5658 5659 assert_exit_reason(exit_reason_want); 5660 report(apic_page_offset == reg, 5661 "got APIC access exit @ page offset 0x%03x, want 0x%03x", 5662 apic_page_offset, reg); 5663 skip_exit_insn(); 5664 5665 /* Reenter guest so it can consume/check rcx and exit again. */ 5666 enter_guest(); 5667 } else if (exit_reason_want == VMX_APIC_WRITE) { 5668 assert_exit_reason(exit_reason_want); 5669 report(virtual_apic_page[apic_reg_index(reg)] == val, 5670 "got APIC write exit @ page offset 0x%03x; val is 0x%x, want 0x%x", 5671 apic_reg_index(reg), 5672 virtual_apic_page[apic_reg_index(reg)], val); 5673 checked = true; 5674 5675 /* Reenter guest so it can consume/check rcx and exit again. */ 5676 enter_guest(); 5677 } else if (exit_reason_want != VMX_VMCALL) { 5678 report(false, "Oops, bad exit expectation: %u.", 5679 exit_reason_want); 5680 } 5681 5682 assert_exit_reason(VMX_VMCALL); 5683 if (virtualized && !checked) { 5684 u32 want = expectation->virt_fn(val); 5685 u32 got = virtual_apic_page[apic_reg_index(reg)]; 5686 got = expectation->virt_fn(got); 5687 5688 report(got == want, "exitless write; val is 0x%x, want 0x%x", 5689 got, want); 5690 } else if (!expectation->virtualize_apic_accesses && !checked) { 5691 u32 got = apic_access_address[apic_reg_index(reg)]; 5692 5693 report(got == val, 5694 "non-virtualized write; val is 0x%x, want 0x%x", got, 5695 val); 5696 } else if (!expectation->virtualize_apic_accesses && checked) { 5697 report(false, 5698 "Non-virtualized write was prematurely checked!"); 5699 } 5700 5701 skip_exit_vmcall(); 5702 report_prefix_pop(); 5703 } 5704 5705 enum Config_type { 5706 CONFIG_TYPE_GOOD, 5707 CONFIG_TYPE_UNSUPPORTED, 5708 CONFIG_TYPE_VMENTRY_FAILS_EARLY, 5709 }; 5710 5711 static enum Config_type configure_apic_reg_virt_test( 5712 struct apic_reg_virt_config *apic_reg_virt_config) 5713 { 5714 u32 cpu_exec_ctrl0 = vmcs_read(CPU_EXEC_CTRL0); 5715 u32 cpu_exec_ctrl1 = vmcs_read(CPU_EXEC_CTRL1); 5716 /* Configs where L2 entry fails early, due to invalid controls. */ 5717 bool use_tpr_shadow_incorrectly_off = 5718 !apic_reg_virt_config->use_tpr_shadow && 5719 (apic_reg_virt_config->apic_register_virtualization || 5720 apic_reg_virt_config->virtualize_x2apic_mode) && 5721 apic_reg_virt_config->activate_secondary_controls; 5722 bool virtualize_apic_accesses_incorrectly_on = 5723 apic_reg_virt_config->virtualize_apic_accesses && 5724 apic_reg_virt_config->virtualize_x2apic_mode && 5725 apic_reg_virt_config->activate_secondary_controls; 5726 bool vmentry_fails_early = 5727 use_tpr_shadow_incorrectly_off || 5728 virtualize_apic_accesses_incorrectly_on; 5729 5730 if (apic_reg_virt_config->activate_secondary_controls) { 5731 if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY)) { 5732 printf("VM-execution control \"activate secondary controls\" NOT supported.\n"); 5733 return CONFIG_TYPE_UNSUPPORTED; 5734 } 5735 cpu_exec_ctrl0 |= CPU_SECONDARY; 5736 } else { 5737 cpu_exec_ctrl0 &= ~CPU_SECONDARY; 5738 } 5739 5740 if (apic_reg_virt_config->virtualize_apic_accesses) { 5741 if (!(ctrl_cpu_rev[1].clr & CPU_VIRT_APIC_ACCESSES)) { 5742 printf("VM-execution control \"virtualize APIC accesses\" NOT supported.\n"); 5743 return CONFIG_TYPE_UNSUPPORTED; 5744 } 5745 cpu_exec_ctrl1 |= CPU_VIRT_APIC_ACCESSES; 5746 } else { 5747 cpu_exec_ctrl1 &= ~CPU_VIRT_APIC_ACCESSES; 5748 } 5749 5750 if (apic_reg_virt_config->use_tpr_shadow) { 5751 if (!(ctrl_cpu_rev[0].clr & CPU_TPR_SHADOW)) { 5752 printf("VM-execution control \"use TPR shadow\" NOT supported.\n"); 5753 return CONFIG_TYPE_UNSUPPORTED; 5754 } 5755 cpu_exec_ctrl0 |= CPU_TPR_SHADOW; 5756 } else { 5757 cpu_exec_ctrl0 &= ~CPU_TPR_SHADOW; 5758 } 5759 5760 if (apic_reg_virt_config->apic_register_virtualization) { 5761 if (!(ctrl_cpu_rev[1].clr & CPU_APIC_REG_VIRT)) { 5762 printf("VM-execution control \"APIC-register virtualization\" NOT supported.\n"); 5763 return CONFIG_TYPE_UNSUPPORTED; 5764 } 5765 cpu_exec_ctrl1 |= CPU_APIC_REG_VIRT; 5766 } else { 5767 cpu_exec_ctrl1 &= ~CPU_APIC_REG_VIRT; 5768 } 5769 5770 if (apic_reg_virt_config->virtualize_x2apic_mode) { 5771 if (!(ctrl_cpu_rev[1].clr & CPU_VIRT_X2APIC)) { 5772 printf("VM-execution control \"virtualize x2APIC mode\" NOT supported.\n"); 5773 return CONFIG_TYPE_UNSUPPORTED; 5774 } 5775 cpu_exec_ctrl1 |= CPU_VIRT_X2APIC; 5776 } else { 5777 cpu_exec_ctrl1 &= ~CPU_VIRT_X2APIC; 5778 } 5779 5780 vmcs_write(CPU_EXEC_CTRL0, cpu_exec_ctrl0); 5781 vmcs_write(CPU_EXEC_CTRL1, cpu_exec_ctrl1); 5782 5783 if (vmentry_fails_early) 5784 return CONFIG_TYPE_VMENTRY_FAILS_EARLY; 5785 5786 return CONFIG_TYPE_GOOD; 5787 } 5788 5789 static bool cpu_has_apicv(void) 5790 { 5791 return ((ctrl_cpu_rev[1].clr & CPU_APIC_REG_VIRT) && 5792 (ctrl_cpu_rev[1].clr & CPU_VINTD) && 5793 (ctrl_pin_rev.clr & PIN_POST_INTR)); 5794 } 5795 5796 /* Validates APIC register access across valid virtualization configurations. */ 5797 static void apic_reg_virt_test(void) 5798 { 5799 u32 *apic_access_address; 5800 u32 *virtual_apic_page; 5801 u64 control; 5802 u64 cpu_exec_ctrl0 = vmcs_read(CPU_EXEC_CTRL0); 5803 u64 cpu_exec_ctrl1 = vmcs_read(CPU_EXEC_CTRL1); 5804 int i; 5805 struct apic_reg_virt_guest_args *args = &apic_reg_virt_guest_args; 5806 5807 if (!cpu_has_apicv()) { 5808 report_skip(__func__); 5809 return; 5810 } 5811 5812 control = cpu_exec_ctrl1; 5813 control &= ~CPU_VINTD; 5814 vmcs_write(CPU_EXEC_CTRL1, control); 5815 5816 test_set_guest(apic_reg_virt_guest); 5817 5818 /* 5819 * From the SDM: The 1-setting of the "virtualize APIC accesses" 5820 * VM-execution is guaranteed to apply only if translations to the 5821 * APIC-access address use a 4-KByte page. 5822 */ 5823 apic_access_address = alloc_page(); 5824 force_4k_page(apic_access_address); 5825 vmcs_write(APIC_ACCS_ADDR, virt_to_phys(apic_access_address)); 5826 5827 virtual_apic_page = alloc_page(); 5828 vmcs_write(APIC_VIRT_ADDR, virt_to_phys(virtual_apic_page)); 5829 5830 for (i = 0; i < ARRAY_SIZE(apic_reg_tests); i++) { 5831 struct apic_reg_test *apic_reg_test = &apic_reg_tests[i]; 5832 struct apic_reg_virt_config *apic_reg_virt_config = 5833 &apic_reg_test->apic_reg_virt_config; 5834 enum Config_type config_type; 5835 u32 reg; 5836 5837 printf("--- %s test ---\n", apic_reg_test->name); 5838 config_type = 5839 configure_apic_reg_virt_test(apic_reg_virt_config); 5840 if (config_type == CONFIG_TYPE_UNSUPPORTED) { 5841 printf("Skip because of missing features.\n"); 5842 continue; 5843 } 5844 5845 if (config_type == CONFIG_TYPE_VMENTRY_FAILS_EARLY) { 5846 enter_guest_with_bad_controls(); 5847 continue; 5848 } 5849 5850 for (reg = 0; reg < PAGE_SIZE / sizeof(u32); reg += 0x10) { 5851 struct apic_reg_virt_expectation expectation = {}; 5852 bool ok; 5853 5854 ok = apic_reg_virt_exit_expectation( 5855 reg, apic_reg_virt_config, &expectation); 5856 if (!ok) { 5857 report(false, "Malformed test."); 5858 break; 5859 } 5860 5861 test_xapic_rd(reg, &expectation, apic_access_address, 5862 virtual_apic_page); 5863 test_xapic_wr(reg, &expectation, apic_access_address, 5864 virtual_apic_page); 5865 } 5866 } 5867 5868 /* Terminate the guest */ 5869 vmcs_write(CPU_EXEC_CTRL0, cpu_exec_ctrl0); 5870 vmcs_write(CPU_EXEC_CTRL1, cpu_exec_ctrl1); 5871 args->op = TERMINATE; 5872 enter_guest(); 5873 assert_exit_reason(VMX_VMCALL); 5874 } 5875 5876 struct virt_x2apic_mode_config { 5877 struct apic_reg_virt_config apic_reg_virt_config; 5878 bool virtual_interrupt_delivery; 5879 bool use_msr_bitmaps; 5880 bool disable_x2apic_msr_intercepts; 5881 bool disable_x2apic; 5882 }; 5883 5884 struct virt_x2apic_mode_test_case { 5885 const char *name; 5886 struct virt_x2apic_mode_config virt_x2apic_mode_config; 5887 }; 5888 5889 enum Virt_x2apic_mode_behavior_type { 5890 X2APIC_ACCESS_VIRTUALIZED, 5891 X2APIC_ACCESS_PASSED_THROUGH, 5892 X2APIC_ACCESS_TRIGGERS_GP, 5893 }; 5894 5895 struct virt_x2apic_mode_expectation { 5896 enum Reason rd_exit_reason; 5897 enum Reason wr_exit_reason; 5898 5899 /* 5900 * RDMSR and WRMSR handle 64-bit values. However, except for ICR, all of 5901 * the x2APIC registers are 32 bits. Notice: 5902 * 1. vmx_x2apic_read() clears the upper 32 bits for 32-bit registers. 5903 * 2. vmx_x2apic_write() expects the val arg to be well-formed. 5904 */ 5905 u64 rd_val; 5906 u64 wr_val; 5907 5908 /* 5909 * Compares input to virtualized output; 5910 * 1st arg is pointer to return expected virtualization output. 5911 */ 5912 u64 (*virt_fn)(u64); 5913 5914 enum Virt_x2apic_mode_behavior_type rd_behavior; 5915 enum Virt_x2apic_mode_behavior_type wr_behavior; 5916 bool wr_only; 5917 }; 5918 5919 static u64 virt_x2apic_mode_identity(u64 val) 5920 { 5921 return val; 5922 } 5923 5924 static u64 virt_x2apic_mode_nibble1(u64 val) 5925 { 5926 return val & 0xf0; 5927 } 5928 5929 static void virt_x2apic_mode_rd_expectation( 5930 u32 reg, bool virt_x2apic_mode_on, bool disable_x2apic, 5931 bool apic_register_virtualization, bool virtual_interrupt_delivery, 5932 struct virt_x2apic_mode_expectation *expectation) 5933 { 5934 bool readable = 5935 !x2apic_reg_reserved(reg) && 5936 reg != APIC_EOI; 5937 5938 expectation->rd_exit_reason = VMX_VMCALL; 5939 expectation->virt_fn = virt_x2apic_mode_identity; 5940 if (virt_x2apic_mode_on && apic_register_virtualization) { 5941 expectation->rd_val = MAGIC_VAL_1; 5942 if (reg == APIC_PROCPRI && virtual_interrupt_delivery) 5943 expectation->virt_fn = virt_x2apic_mode_nibble1; 5944 else if (reg == APIC_TASKPRI) 5945 expectation->virt_fn = virt_x2apic_mode_nibble1; 5946 expectation->rd_behavior = X2APIC_ACCESS_VIRTUALIZED; 5947 } else if (virt_x2apic_mode_on && !apic_register_virtualization && 5948 reg == APIC_TASKPRI) { 5949 expectation->rd_val = MAGIC_VAL_1; 5950 expectation->virt_fn = virt_x2apic_mode_nibble1; 5951 expectation->rd_behavior = X2APIC_ACCESS_VIRTUALIZED; 5952 } else if (!disable_x2apic && readable) { 5953 expectation->rd_val = apic_read(reg); 5954 expectation->rd_behavior = X2APIC_ACCESS_PASSED_THROUGH; 5955 } else { 5956 expectation->rd_behavior = X2APIC_ACCESS_TRIGGERS_GP; 5957 } 5958 } 5959 5960 /* 5961 * get_x2apic_wr_val() creates an innocuous write value for an x2APIC register. 5962 * 5963 * For writable registers, get_x2apic_wr_val() deposits the write value into the 5964 * val pointer arg and returns true. For non-writable registers, val is not 5965 * modified and get_x2apic_wr_val() returns false. 5966 */ 5967 static bool get_x2apic_wr_val(u32 reg, u64 *val) 5968 { 5969 switch (reg) { 5970 case APIC_TASKPRI: 5971 /* Bits 31:8 are reserved. */ 5972 *val &= 0xff; 5973 break; 5974 case APIC_EOI: 5975 case APIC_ESR: 5976 case APIC_TMICT: 5977 /* 5978 * EOI, ESR: WRMSR of a non-zero value causes #GP(0). 5979 * TMICT: A write of 0 to the initial-count register effectively 5980 * stops the local APIC timer, in both one-shot and 5981 * periodic mode. 5982 */ 5983 *val = 0; 5984 break; 5985 case APIC_SPIV: 5986 case APIC_LVTT: 5987 case APIC_LVTTHMR: 5988 case APIC_LVTPC: 5989 case APIC_LVT0: 5990 case APIC_LVT1: 5991 case APIC_LVTERR: 5992 case APIC_TDCR: 5993 /* 5994 * To avoid writing a 1 to a reserved bit or causing some other 5995 * unintended side effect, read the current value and use it as 5996 * the write value. 5997 */ 5998 *val = apic_read(reg); 5999 break; 6000 case APIC_CMCI: 6001 if (!apic_lvt_entry_supported(6)) 6002 return false; 6003 *val = apic_read(reg); 6004 break; 6005 case APIC_ICR: 6006 *val = 0x40000 | 0xf1; 6007 break; 6008 case APIC_SELF_IPI: 6009 /* 6010 * With special processing (i.e., virtualize x2APIC mode + 6011 * virtual interrupt delivery), writing zero causes an 6012 * APIC-write VM exit. We plan to add a test for enabling 6013 * "virtual-interrupt delivery" in VMCS12, and that's where we 6014 * will test a self IPI with special processing. 6015 */ 6016 *val = 0x0; 6017 break; 6018 default: 6019 return false; 6020 } 6021 6022 return true; 6023 } 6024 6025 static bool special_processing_applies(u32 reg, u64 *val, 6026 bool virt_int_delivery) 6027 { 6028 bool special_processing = 6029 (reg == APIC_TASKPRI) || 6030 (virt_int_delivery && 6031 (reg == APIC_EOI || reg == APIC_SELF_IPI)); 6032 6033 if (special_processing) { 6034 TEST_ASSERT(get_x2apic_wr_val(reg, val)); 6035 return true; 6036 } 6037 6038 return false; 6039 } 6040 6041 static void virt_x2apic_mode_wr_expectation( 6042 u32 reg, bool virt_x2apic_mode_on, bool disable_x2apic, 6043 bool virt_int_delivery, 6044 struct virt_x2apic_mode_expectation *expectation) 6045 { 6046 expectation->wr_exit_reason = VMX_VMCALL; 6047 expectation->wr_val = MAGIC_VAL_1; 6048 expectation->wr_only = false; 6049 6050 if (virt_x2apic_mode_on && 6051 special_processing_applies(reg, &expectation->wr_val, 6052 virt_int_delivery)) { 6053 expectation->wr_behavior = X2APIC_ACCESS_VIRTUALIZED; 6054 if (reg == APIC_SELF_IPI) 6055 expectation->wr_exit_reason = VMX_APIC_WRITE; 6056 } else if (!disable_x2apic && 6057 get_x2apic_wr_val(reg, &expectation->wr_val)) { 6058 expectation->wr_behavior = X2APIC_ACCESS_PASSED_THROUGH; 6059 if (reg == APIC_EOI || reg == APIC_SELF_IPI) 6060 expectation->wr_only = true; 6061 if (reg == APIC_ICR) 6062 expectation->wr_exit_reason = VMX_EXTINT; 6063 } else { 6064 expectation->wr_behavior = X2APIC_ACCESS_TRIGGERS_GP; 6065 /* 6066 * Writing 1 to a reserved bit triggers a #GP. 6067 * Thus, set the write value to 0, which seems 6068 * the most likely to detect a missed #GP. 6069 */ 6070 expectation->wr_val = 0; 6071 } 6072 } 6073 6074 static void virt_x2apic_mode_exit_expectation( 6075 u32 reg, struct virt_x2apic_mode_config *config, 6076 struct virt_x2apic_mode_expectation *expectation) 6077 { 6078 struct apic_reg_virt_config *base_config = 6079 &config->apic_reg_virt_config; 6080 bool virt_x2apic_mode_on = 6081 base_config->virtualize_x2apic_mode && 6082 config->use_msr_bitmaps && 6083 config->disable_x2apic_msr_intercepts && 6084 base_config->activate_secondary_controls; 6085 6086 virt_x2apic_mode_wr_expectation( 6087 reg, virt_x2apic_mode_on, config->disable_x2apic, 6088 config->virtual_interrupt_delivery, expectation); 6089 virt_x2apic_mode_rd_expectation( 6090 reg, virt_x2apic_mode_on, config->disable_x2apic, 6091 base_config->apic_register_virtualization, 6092 config->virtual_interrupt_delivery, expectation); 6093 } 6094 6095 struct virt_x2apic_mode_test_case virt_x2apic_mode_tests[] = { 6096 /* 6097 * Baseline "virtualize x2APIC mode" configuration: 6098 * - virtualize x2APIC mode 6099 * - virtual-interrupt delivery 6100 * - APIC-register virtualization 6101 * - x2APIC MSR intercepts disabled 6102 * 6103 * Reads come from virtual APIC page, special processing applies to 6104 * VTPR, EOI, and SELF IPI, and all other writes pass through to L1 6105 * APIC. 6106 */ 6107 { 6108 .name = "Baseline", 6109 .virt_x2apic_mode_config = { 6110 .virtual_interrupt_delivery = true, 6111 .use_msr_bitmaps = true, 6112 .disable_x2apic_msr_intercepts = true, 6113 .disable_x2apic = false, 6114 .apic_reg_virt_config = { 6115 .apic_register_virtualization = true, 6116 .use_tpr_shadow = true, 6117 .virtualize_apic_accesses = false, 6118 .virtualize_x2apic_mode = true, 6119 .activate_secondary_controls = true, 6120 }, 6121 }, 6122 }, 6123 { 6124 .name = "Baseline w/ x2apic disabled", 6125 .virt_x2apic_mode_config = { 6126 .virtual_interrupt_delivery = true, 6127 .use_msr_bitmaps = true, 6128 .disable_x2apic_msr_intercepts = true, 6129 .disable_x2apic = true, 6130 .apic_reg_virt_config = { 6131 .apic_register_virtualization = true, 6132 .use_tpr_shadow = true, 6133 .virtualize_apic_accesses = false, 6134 .virtualize_x2apic_mode = true, 6135 .activate_secondary_controls = true, 6136 }, 6137 }, 6138 }, 6139 6140 /* 6141 * Baseline, minus virtual-interrupt delivery. Reads come from virtual 6142 * APIC page, special processing applies to VTPR, and all other writes 6143 * pass through to L1 APIC. 6144 */ 6145 { 6146 .name = "Baseline - virtual interrupt delivery", 6147 .virt_x2apic_mode_config = { 6148 .virtual_interrupt_delivery = false, 6149 .use_msr_bitmaps = true, 6150 .disable_x2apic_msr_intercepts = true, 6151 .disable_x2apic = false, 6152 .apic_reg_virt_config = { 6153 .apic_register_virtualization = true, 6154 .use_tpr_shadow = true, 6155 .virtualize_apic_accesses = false, 6156 .virtualize_x2apic_mode = true, 6157 .activate_secondary_controls = true, 6158 }, 6159 }, 6160 }, 6161 6162 /* 6163 * Baseline, minus APIC-register virtualization. x2APIC reads pass 6164 * through to L1's APIC, unless reading VTPR 6165 */ 6166 { 6167 .name = "Virtualize x2APIC mode, no APIC reg virt", 6168 .virt_x2apic_mode_config = { 6169 .virtual_interrupt_delivery = true, 6170 .use_msr_bitmaps = true, 6171 .disable_x2apic_msr_intercepts = true, 6172 .disable_x2apic = false, 6173 .apic_reg_virt_config = { 6174 .apic_register_virtualization = false, 6175 .use_tpr_shadow = true, 6176 .virtualize_apic_accesses = false, 6177 .virtualize_x2apic_mode = true, 6178 .activate_secondary_controls = true, 6179 }, 6180 }, 6181 }, 6182 { 6183 .name = "Virtualize x2APIC mode, no APIC reg virt, x2APIC off", 6184 .virt_x2apic_mode_config = { 6185 .virtual_interrupt_delivery = true, 6186 .use_msr_bitmaps = true, 6187 .disable_x2apic_msr_intercepts = true, 6188 .disable_x2apic = true, 6189 .apic_reg_virt_config = { 6190 .apic_register_virtualization = false, 6191 .use_tpr_shadow = true, 6192 .virtualize_apic_accesses = false, 6193 .virtualize_x2apic_mode = true, 6194 .activate_secondary_controls = true, 6195 }, 6196 }, 6197 }, 6198 6199 /* 6200 * Enable "virtualize x2APIC mode" and "APIC-register virtualization", 6201 * and disable intercepts for the x2APIC MSRs, but fail to enable 6202 * "activate secondary controls" (i.e. L2 gets access to L1's x2APIC 6203 * MSRs). 6204 */ 6205 { 6206 .name = "Fail to enable activate secondary controls", 6207 .virt_x2apic_mode_config = { 6208 .virtual_interrupt_delivery = true, 6209 .use_msr_bitmaps = true, 6210 .disable_x2apic_msr_intercepts = true, 6211 .disable_x2apic = false, 6212 .apic_reg_virt_config = { 6213 .apic_register_virtualization = true, 6214 .use_tpr_shadow = true, 6215 .virtualize_apic_accesses = false, 6216 .virtualize_x2apic_mode = true, 6217 .activate_secondary_controls = false, 6218 }, 6219 }, 6220 }, 6221 6222 /* 6223 * Enable "APIC-register virtualization" and enable "activate secondary 6224 * controls" and disable intercepts for the x2APIC MSRs, but do not 6225 * enable the "virtualize x2APIC mode" VM-execution control (i.e. L2 6226 * gets access to L1's x2APIC MSRs). 6227 */ 6228 { 6229 .name = "Fail to enable virtualize x2APIC mode", 6230 .virt_x2apic_mode_config = { 6231 .virtual_interrupt_delivery = true, 6232 .use_msr_bitmaps = true, 6233 .disable_x2apic_msr_intercepts = true, 6234 .disable_x2apic = false, 6235 .apic_reg_virt_config = { 6236 .apic_register_virtualization = true, 6237 .use_tpr_shadow = true, 6238 .virtualize_apic_accesses = false, 6239 .virtualize_x2apic_mode = false, 6240 .activate_secondary_controls = true, 6241 }, 6242 }, 6243 }, 6244 6245 /* 6246 * Disable "Virtualize x2APIC mode", disable x2APIC MSR intercepts, and 6247 * enable "APIC-register virtualization" --> L2 gets L1's x2APIC MSRs. 6248 */ 6249 { 6250 .name = "Baseline", 6251 .virt_x2apic_mode_config = { 6252 .virtual_interrupt_delivery = true, 6253 .use_msr_bitmaps = true, 6254 .disable_x2apic_msr_intercepts = true, 6255 .disable_x2apic = false, 6256 .apic_reg_virt_config = { 6257 .apic_register_virtualization = true, 6258 .use_tpr_shadow = true, 6259 .virtualize_apic_accesses = false, 6260 .virtualize_x2apic_mode = false, 6261 .activate_secondary_controls = true, 6262 }, 6263 }, 6264 }, 6265 }; 6266 6267 enum X2apic_op { 6268 X2APIC_OP_RD, 6269 X2APIC_OP_WR, 6270 X2APIC_TERMINATE, 6271 }; 6272 6273 static u64 vmx_x2apic_read(u32 reg) 6274 { 6275 u32 msr_addr = x2apic_msr(reg); 6276 u64 val; 6277 6278 val = rdmsr(msr_addr); 6279 6280 return val; 6281 } 6282 6283 static void vmx_x2apic_write(u32 reg, u64 val) 6284 { 6285 u32 msr_addr = x2apic_msr(reg); 6286 6287 wrmsr(msr_addr, val); 6288 } 6289 6290 struct virt_x2apic_mode_guest_args { 6291 enum X2apic_op op; 6292 u32 reg; 6293 u64 val; 6294 bool should_gp; 6295 u64 (*virt_fn)(u64); 6296 } virt_x2apic_mode_guest_args; 6297 6298 static volatile bool handle_x2apic_gp_ran; 6299 static volatile u32 handle_x2apic_gp_insn_len; 6300 static void handle_x2apic_gp(struct ex_regs *regs) 6301 { 6302 handle_x2apic_gp_ran = true; 6303 regs->rip += handle_x2apic_gp_insn_len; 6304 } 6305 6306 static handler setup_x2apic_gp_handler(void) 6307 { 6308 handler old_handler; 6309 6310 old_handler = handle_exception(GP_VECTOR, handle_x2apic_gp); 6311 /* RDMSR and WRMSR are both 2 bytes, assuming no prefixes. */ 6312 handle_x2apic_gp_insn_len = 2; 6313 6314 return old_handler; 6315 } 6316 6317 static void teardown_x2apic_gp_handler(handler old_handler) 6318 { 6319 handle_exception(GP_VECTOR, old_handler); 6320 6321 /* 6322 * Defensively reset instruction length, so that if the handler is 6323 * incorrectly used, it will loop infinitely, rather than run off into 6324 * la la land. 6325 */ 6326 handle_x2apic_gp_insn_len = 0; 6327 handle_x2apic_gp_ran = false; 6328 } 6329 6330 static void virt_x2apic_mode_guest(void) 6331 { 6332 volatile struct virt_x2apic_mode_guest_args *args = 6333 &virt_x2apic_mode_guest_args; 6334 6335 for (;;) { 6336 enum X2apic_op op = args->op; 6337 u32 reg = args->reg; 6338 u64 val = args->val; 6339 bool should_gp = args->should_gp; 6340 u64 (*virt_fn)(u64) = args->virt_fn; 6341 handler old_handler; 6342 6343 if (op == X2APIC_TERMINATE) 6344 break; 6345 6346 if (should_gp) { 6347 TEST_ASSERT(!handle_x2apic_gp_ran); 6348 old_handler = setup_x2apic_gp_handler(); 6349 } 6350 6351 if (op == X2APIC_OP_RD) { 6352 u64 ret = vmx_x2apic_read(reg); 6353 6354 if (!should_gp) { 6355 u64 want = virt_fn(val); 6356 u64 got = virt_fn(ret); 6357 6358 report(got == want, 6359 "APIC read; got 0x%lx, want 0x%lx.", 6360 got, want); 6361 } 6362 } else if (op == X2APIC_OP_WR) { 6363 vmx_x2apic_write(reg, val); 6364 } 6365 6366 if (should_gp) { 6367 report(handle_x2apic_gp_ran, 6368 "x2APIC op triggered GP."); 6369 teardown_x2apic_gp_handler(old_handler); 6370 } 6371 6372 /* 6373 * The L1 should always execute a vmcall after it's done testing 6374 * an individual APIC operation. This helps to validate that the 6375 * L1 and L2 are in sync with each other, as expected. 6376 */ 6377 vmcall(); 6378 } 6379 } 6380 6381 static void test_x2apic_rd( 6382 u32 reg, struct virt_x2apic_mode_expectation *expectation, 6383 u32 *virtual_apic_page) 6384 { 6385 u64 val = expectation->rd_val; 6386 u32 exit_reason_want = expectation->rd_exit_reason; 6387 struct virt_x2apic_mode_guest_args *args = &virt_x2apic_mode_guest_args; 6388 6389 report_prefix_pushf("x2apic - reading 0x%03x", reg); 6390 6391 /* Configure guest to do an x2apic read */ 6392 args->op = X2APIC_OP_RD; 6393 args->reg = reg; 6394 args->val = val; 6395 args->should_gp = expectation->rd_behavior == X2APIC_ACCESS_TRIGGERS_GP; 6396 args->virt_fn = expectation->virt_fn; 6397 6398 /* Setup virtual APIC page */ 6399 if (expectation->rd_behavior == X2APIC_ACCESS_VIRTUALIZED) 6400 virtual_apic_page[apic_reg_index(reg)] = (u32)val; 6401 6402 /* Enter guest */ 6403 enter_guest(); 6404 6405 if (exit_reason_want != VMX_VMCALL) { 6406 report(false, "Oops, bad exit expectation: %u.", 6407 exit_reason_want); 6408 } 6409 6410 skip_exit_vmcall(); 6411 report_prefix_pop(); 6412 } 6413 6414 static volatile bool handle_x2apic_ipi_ran; 6415 static void handle_x2apic_ipi(isr_regs_t *regs) 6416 { 6417 handle_x2apic_ipi_ran = true; 6418 eoi(); 6419 } 6420 6421 static void test_x2apic_wr( 6422 u32 reg, struct virt_x2apic_mode_expectation *expectation, 6423 u32 *virtual_apic_page) 6424 { 6425 u64 val = expectation->wr_val; 6426 u32 exit_reason_want = expectation->wr_exit_reason; 6427 struct virt_x2apic_mode_guest_args *args = &virt_x2apic_mode_guest_args; 6428 int ipi_vector = 0xf1; 6429 u32 restore_val = 0; 6430 6431 report_prefix_pushf("x2apic - writing 0x%lx to 0x%03x", val, reg); 6432 6433 /* Configure guest to do an x2apic read */ 6434 args->op = X2APIC_OP_WR; 6435 args->reg = reg; 6436 args->val = val; 6437 args->should_gp = expectation->wr_behavior == X2APIC_ACCESS_TRIGGERS_GP; 6438 6439 /* Setup virtual APIC page */ 6440 if (expectation->wr_behavior == X2APIC_ACCESS_VIRTUALIZED) 6441 virtual_apic_page[apic_reg_index(reg)] = 0; 6442 if (expectation->wr_behavior == X2APIC_ACCESS_PASSED_THROUGH && !expectation->wr_only) 6443 restore_val = apic_read(reg); 6444 6445 /* Setup IPI handler */ 6446 handle_x2apic_ipi_ran = false; 6447 handle_irq(ipi_vector, handle_x2apic_ipi); 6448 6449 /* Enter guest */ 6450 enter_guest(); 6451 6452 /* 6453 * Validate the behavior and 6454 * pass a magic value back to the guest. 6455 */ 6456 if (exit_reason_want == VMX_EXTINT) { 6457 assert_exit_reason(exit_reason_want); 6458 6459 /* Clear the external interrupt. */ 6460 irq_enable(); 6461 asm volatile ("nop"); 6462 irq_disable(); 6463 report(handle_x2apic_ipi_ran, 6464 "Got pending interrupt after IRQ enabled."); 6465 6466 enter_guest(); 6467 } else if (exit_reason_want == VMX_APIC_WRITE) { 6468 assert_exit_reason(exit_reason_want); 6469 report(virtual_apic_page[apic_reg_index(reg)] == val, 6470 "got APIC write exit @ page offset 0x%03x; val is 0x%x, want 0x%lx", 6471 apic_reg_index(reg), 6472 virtual_apic_page[apic_reg_index(reg)], val); 6473 6474 /* Reenter guest so it can consume/check rcx and exit again. */ 6475 enter_guest(); 6476 } else if (exit_reason_want != VMX_VMCALL) { 6477 report(false, "Oops, bad exit expectation: %u.", 6478 exit_reason_want); 6479 } 6480 6481 assert_exit_reason(VMX_VMCALL); 6482 if (expectation->wr_behavior == X2APIC_ACCESS_VIRTUALIZED) { 6483 u64 want = val; 6484 u32 got = virtual_apic_page[apic_reg_index(reg)]; 6485 6486 report(got == want, "x2APIC write; got 0x%x, want 0x%lx", got, 6487 want); 6488 } else if (expectation->wr_behavior == X2APIC_ACCESS_PASSED_THROUGH) { 6489 if (!expectation->wr_only) { 6490 u32 got = apic_read(reg); 6491 bool ok; 6492 6493 /* 6494 * When L1's TPR is passed through to L2, the lower 6495 * nibble can be lost. For example, if L2 executes 6496 * WRMSR(0x808, 0x78), then, L1 might read 0x70. 6497 * 6498 * Here's how the lower nibble can get lost: 6499 * 1. L2 executes WRMSR(0x808, 0x78). 6500 * 2. L2 exits to L0 with a WRMSR exit. 6501 * 3. L0 emulates WRMSR, by writing L1's TPR. 6502 * 4. L0 re-enters L2. 6503 * 5. L2 exits to L0 (reason doesn't matter). 6504 * 6. L0 reflects L2's exit to L1. 6505 * 7. Before entering L1, L0 exits to user-space 6506 * (e.g., to satisfy TPR access reporting). 6507 * 8. User-space executes KVM_SET_REGS ioctl, which 6508 * clears the lower nibble of L1's TPR. 6509 */ 6510 if (reg == APIC_TASKPRI) { 6511 got = apic_virt_nibble1(got); 6512 val = apic_virt_nibble1(val); 6513 } 6514 6515 ok = got == val; 6516 report(ok, 6517 "non-virtualized write; val is 0x%x, want 0x%lx", 6518 got, val); 6519 apic_write(reg, restore_val); 6520 } else { 6521 report(true, "non-virtualized and write-only OK"); 6522 } 6523 } 6524 skip_exit_insn(); 6525 6526 report_prefix_pop(); 6527 } 6528 6529 static enum Config_type configure_virt_x2apic_mode_test( 6530 struct virt_x2apic_mode_config *virt_x2apic_mode_config, 6531 u8 *msr_bitmap_page) 6532 { 6533 int msr; 6534 u32 cpu_exec_ctrl0 = vmcs_read(CPU_EXEC_CTRL0); 6535 u64 cpu_exec_ctrl1 = vmcs_read(CPU_EXEC_CTRL1); 6536 6537 /* x2apic-specific VMCS config */ 6538 if (virt_x2apic_mode_config->use_msr_bitmaps) { 6539 /* virt_x2apic_mode_test() checks for MSR bitmaps support */ 6540 cpu_exec_ctrl0 |= CPU_MSR_BITMAP; 6541 } else { 6542 cpu_exec_ctrl0 &= ~CPU_MSR_BITMAP; 6543 } 6544 6545 if (virt_x2apic_mode_config->virtual_interrupt_delivery) { 6546 if (!(ctrl_cpu_rev[1].clr & CPU_VINTD)) { 6547 report_skip("VM-execution control \"virtual-interrupt delivery\" NOT supported.\n"); 6548 return CONFIG_TYPE_UNSUPPORTED; 6549 } 6550 cpu_exec_ctrl1 |= CPU_VINTD; 6551 } else { 6552 cpu_exec_ctrl1 &= ~CPU_VINTD; 6553 } 6554 6555 vmcs_write(CPU_EXEC_CTRL0, cpu_exec_ctrl0); 6556 vmcs_write(CPU_EXEC_CTRL1, cpu_exec_ctrl1); 6557 6558 /* x2APIC MSR intercepts are usually off for "Virtualize x2APIC mode" */ 6559 for (msr = 0x800; msr <= 0x8ff; msr++) { 6560 if (virt_x2apic_mode_config->disable_x2apic_msr_intercepts) { 6561 clear_bit(msr, msr_bitmap_page + 0x000); 6562 clear_bit(msr, msr_bitmap_page + 0x800); 6563 } else { 6564 set_bit(msr, msr_bitmap_page + 0x000); 6565 set_bit(msr, msr_bitmap_page + 0x800); 6566 } 6567 } 6568 6569 /* x2APIC mode can impact virtualization */ 6570 reset_apic(); 6571 if (!virt_x2apic_mode_config->disable_x2apic) 6572 enable_x2apic(); 6573 6574 return configure_apic_reg_virt_test( 6575 &virt_x2apic_mode_config->apic_reg_virt_config); 6576 } 6577 6578 static void virt_x2apic_mode_test(void) 6579 { 6580 u32 *virtual_apic_page; 6581 u8 *msr_bitmap_page; 6582 u64 cpu_exec_ctrl0 = vmcs_read(CPU_EXEC_CTRL0); 6583 u64 cpu_exec_ctrl1 = vmcs_read(CPU_EXEC_CTRL1); 6584 int i; 6585 struct virt_x2apic_mode_guest_args *args = &virt_x2apic_mode_guest_args; 6586 6587 if (!cpu_has_apicv()) { 6588 report_skip(__func__); 6589 return; 6590 } 6591 6592 /* 6593 * This is to exercise an issue in KVM's logic to merge L0's and L1's 6594 * MSR bitmaps. Previously, an L1 could get at L0's x2APIC MSRs by 6595 * writing the IA32_SPEC_CTRL MSR or the IA32_PRED_CMD MSRs. KVM would 6596 * then proceed to manipulate the MSR bitmaps, as if VMCS12 had the 6597 * "Virtualize x2APIC mod" control set, even when it didn't. 6598 */ 6599 if (has_spec_ctrl()) 6600 wrmsr(MSR_IA32_SPEC_CTRL, 1); 6601 6602 /* 6603 * Check that VMCS12 supports: 6604 * - "Virtual-APIC address", indicated by "use TPR shadow" 6605 * - "MSR-bitmap address", indicated by "use MSR bitmaps" 6606 */ 6607 if (!(ctrl_cpu_rev[0].clr & CPU_TPR_SHADOW)) { 6608 report_skip("VM-execution control \"use TPR shadow\" NOT supported.\n"); 6609 return; 6610 } else if (!(ctrl_cpu_rev[0].clr & CPU_MSR_BITMAP)) { 6611 report_skip("VM-execution control \"use MSR bitmaps\" NOT supported.\n"); 6612 return; 6613 } 6614 6615 test_set_guest(virt_x2apic_mode_guest); 6616 6617 virtual_apic_page = alloc_page(); 6618 vmcs_write(APIC_VIRT_ADDR, virt_to_phys(virtual_apic_page)); 6619 6620 msr_bitmap_page = alloc_page(); 6621 memset(msr_bitmap_page, 0xff, PAGE_SIZE); 6622 vmcs_write(MSR_BITMAP, virt_to_phys(msr_bitmap_page)); 6623 6624 for (i = 0; i < ARRAY_SIZE(virt_x2apic_mode_tests); i++) { 6625 struct virt_x2apic_mode_test_case *virt_x2apic_mode_test_case = 6626 &virt_x2apic_mode_tests[i]; 6627 struct virt_x2apic_mode_config *virt_x2apic_mode_config = 6628 &virt_x2apic_mode_test_case->virt_x2apic_mode_config; 6629 enum Config_type config_type; 6630 u32 reg; 6631 6632 printf("--- %s test ---\n", virt_x2apic_mode_test_case->name); 6633 config_type = 6634 configure_virt_x2apic_mode_test(virt_x2apic_mode_config, 6635 msr_bitmap_page); 6636 if (config_type == CONFIG_TYPE_UNSUPPORTED) { 6637 report_skip("Skip because of missing features.\n"); 6638 continue; 6639 } else if (config_type == CONFIG_TYPE_VMENTRY_FAILS_EARLY) { 6640 enter_guest_with_bad_controls(); 6641 continue; 6642 } 6643 6644 for (reg = 0; reg < PAGE_SIZE / sizeof(u32); reg += 0x10) { 6645 struct virt_x2apic_mode_expectation expectation; 6646 6647 virt_x2apic_mode_exit_expectation( 6648 reg, virt_x2apic_mode_config, &expectation); 6649 6650 test_x2apic_rd(reg, &expectation, virtual_apic_page); 6651 test_x2apic_wr(reg, &expectation, virtual_apic_page); 6652 } 6653 } 6654 6655 6656 /* Terminate the guest */ 6657 vmcs_write(CPU_EXEC_CTRL0, cpu_exec_ctrl0); 6658 vmcs_write(CPU_EXEC_CTRL1, cpu_exec_ctrl1); 6659 args->op = X2APIC_TERMINATE; 6660 enter_guest(); 6661 assert_exit_reason(VMX_VMCALL); 6662 } 6663 6664 static void test_ctl_reg(const char *cr_name, u64 cr, u64 fixed0, u64 fixed1) 6665 { 6666 u64 val; 6667 u64 cr_saved = vmcs_read(cr); 6668 int i; 6669 6670 val = fixed0 & fixed1; 6671 if (cr == HOST_CR4) 6672 vmcs_write(cr, val | X86_CR4_PAE); 6673 else 6674 vmcs_write(cr, val); 6675 report_prefix_pushf("%s %lx", cr_name, val); 6676 if (val == fixed0) 6677 test_vmx_vmlaunch(0); 6678 else 6679 test_vmx_vmlaunch(VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); 6680 report_prefix_pop(); 6681 6682 for (i = 0; i < 64; i++) { 6683 6684 /* Set a bit when the corresponding bit in fixed1 is 0 */ 6685 if ((fixed1 & (1ull << i)) == 0) { 6686 if (cr == HOST_CR4 && ((1ull << i) & X86_CR4_SMEP || 6687 (1ull << i) & X86_CR4_SMAP)) 6688 continue; 6689 6690 vmcs_write(cr, cr_saved | (1ull << i)); 6691 report_prefix_pushf("%s %llx", cr_name, 6692 cr_saved | (1ull << i)); 6693 test_vmx_vmlaunch( 6694 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); 6695 report_prefix_pop(); 6696 } 6697 6698 /* Unset a bit when the corresponding bit in fixed0 is 1 */ 6699 if (fixed0 & (1ull << i)) { 6700 vmcs_write(cr, cr_saved & ~(1ull << i)); 6701 report_prefix_pushf("%s %llx", cr_name, 6702 cr_saved & ~(1ull << i)); 6703 test_vmx_vmlaunch( 6704 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); 6705 report_prefix_pop(); 6706 } 6707 } 6708 6709 vmcs_write(cr, cr_saved); 6710 } 6711 6712 /* 6713 * 1. The CR0 field must not set any bit to a value not supported in VMX 6714 * operation. 6715 * 2. The CR4 field must not set any bit to a value not supported in VMX 6716 * operation. 6717 * 3. On processors that support Intel 64 architecture, the CR3 field must 6718 * be such that bits 63:52 and bits in the range 51:32 beyond the 6719 * processor’s physical-address width must be 0. 6720 * 6721 * [Intel SDM] 6722 */ 6723 static void test_host_ctl_regs(void) 6724 { 6725 u64 fixed0, fixed1, cr3, cr3_saved; 6726 int i; 6727 6728 /* Test CR0 */ 6729 fixed0 = rdmsr(MSR_IA32_VMX_CR0_FIXED0); 6730 fixed1 = rdmsr(MSR_IA32_VMX_CR0_FIXED1); 6731 test_ctl_reg("HOST_CR0", HOST_CR0, fixed0, fixed1); 6732 6733 /* Test CR4 */ 6734 fixed0 = rdmsr(MSR_IA32_VMX_CR4_FIXED0); 6735 fixed1 = rdmsr(MSR_IA32_VMX_CR4_FIXED1) & 6736 ~(X86_CR4_SMEP | X86_CR4_SMAP); 6737 test_ctl_reg("HOST_CR4", HOST_CR4, fixed0, fixed1); 6738 6739 /* Test CR3 */ 6740 cr3_saved = vmcs_read(HOST_CR3); 6741 for (i = cpuid_maxphyaddr(); i < 64; i++) { 6742 cr3 = cr3_saved | (1ul << i); 6743 vmcs_write(HOST_CR3, cr3); 6744 report_prefix_pushf("HOST_CR3 %lx", cr3); 6745 test_vmx_vmlaunch(VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); 6746 report_prefix_pop(); 6747 } 6748 6749 vmcs_write(HOST_CR3, cr3_saved); 6750 } 6751 6752 static void test_efer_vmlaunch(u32 fld, bool ok) 6753 { 6754 if (fld == HOST_EFER) { 6755 if (ok) 6756 test_vmx_vmlaunch(0); 6757 else 6758 test_vmx_vmlaunch(VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); 6759 } else { 6760 if (ok) { 6761 enter_guest(); 6762 report(vmcs_read(EXI_REASON) == VMX_VMCALL, 6763 "vmlaunch succeeds"); 6764 } else { 6765 enter_guest_with_invalid_guest_state(); 6766 report(vmcs_read(EXI_REASON) == (VMX_ENTRY_FAILURE | VMX_FAIL_STATE), 6767 "vmlaunch fails"); 6768 } 6769 advance_guest_state_test(); 6770 } 6771 } 6772 6773 static void test_efer_one(u32 fld, const char * fld_name, u64 efer, 6774 u32 ctrl_fld, u64 ctrl, 6775 int i, const char *efer_bit_name) 6776 { 6777 bool ok; 6778 6779 ok = true; 6780 if (ctrl_fld == EXI_CONTROLS && (ctrl & EXI_LOAD_EFER)) { 6781 if (!!(efer & EFER_LMA) != !!(ctrl & EXI_HOST_64)) 6782 ok = false; 6783 if (!!(efer & EFER_LME) != !!(ctrl & EXI_HOST_64)) 6784 ok = false; 6785 } 6786 if (ctrl_fld == ENT_CONTROLS && (ctrl & ENT_LOAD_EFER)) { 6787 /* Check LMA too since CR0.PG is set. */ 6788 if (!!(efer & EFER_LMA) != !!(ctrl & ENT_GUEST_64)) 6789 ok = false; 6790 if (!!(efer & EFER_LME) != !!(ctrl & ENT_GUEST_64)) 6791 ok = false; 6792 } 6793 6794 /* 6795 * Skip the test if it would enter the guest in 32-bit mode. 6796 * Perhaps write the test in assembly and make sure it 6797 * can be run in either mode? 6798 */ 6799 if (fld == GUEST_EFER && ok && !(ctrl & ENT_GUEST_64)) 6800 return; 6801 6802 vmcs_write(ctrl_fld, ctrl); 6803 vmcs_write(fld, efer); 6804 report_prefix_pushf("%s %s bit turned %s, controls %s", 6805 fld_name, efer_bit_name, 6806 (i & 1) ? "on" : "off", 6807 (i & 2) ? "on" : "off"); 6808 6809 test_efer_vmlaunch(fld, ok); 6810 report_prefix_pop(); 6811 } 6812 6813 static void test_efer_bit(u32 fld, const char * fld_name, 6814 u32 ctrl_fld, u64 ctrl_bit, u64 efer_bit, 6815 const char *efer_bit_name) 6816 { 6817 u64 efer_saved = vmcs_read(fld); 6818 u32 ctrl_saved = vmcs_read(ctrl_fld); 6819 int i; 6820 6821 for (i = 0; i < 4; i++) { 6822 u64 efer = efer_saved & ~efer_bit; 6823 u64 ctrl = ctrl_saved & ~ctrl_bit; 6824 6825 if (i & 1) 6826 efer |= efer_bit; 6827 if (i & 2) 6828 ctrl |= ctrl_bit; 6829 6830 test_efer_one(fld, fld_name, efer, ctrl_fld, ctrl, 6831 i, efer_bit_name); 6832 } 6833 6834 vmcs_write(ctrl_fld, ctrl_saved); 6835 vmcs_write(fld, efer_saved); 6836 } 6837 6838 static void test_efer(u32 fld, const char * fld_name, u32 ctrl_fld, 6839 u64 ctrl_bit1, u64 ctrl_bit2) 6840 { 6841 u64 efer_saved = vmcs_read(fld); 6842 u32 ctrl_saved = vmcs_read(ctrl_fld); 6843 u64 efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA)); 6844 u64 i; 6845 u64 efer; 6846 6847 if (cpu_has_efer_nx()) 6848 efer_reserved_bits &= ~EFER_NX; 6849 6850 if (!ctrl_bit1) { 6851 printf("\"Load-IA32-EFER\" exit control not supported\n"); 6852 goto test_entry_exit_mode; 6853 } 6854 6855 report_prefix_pushf("%s %lx", fld_name, efer_saved); 6856 test_efer_vmlaunch(fld, true); 6857 report_prefix_pop(); 6858 6859 /* 6860 * Check reserved bits 6861 */ 6862 vmcs_write(ctrl_fld, ctrl_saved & ~ctrl_bit1); 6863 for (i = 0; i < 64; i++) { 6864 if ((1ull << i) & efer_reserved_bits) { 6865 efer = efer_saved | (1ull << i); 6866 vmcs_write(fld, efer); 6867 report_prefix_pushf("%s %lx", fld_name, efer); 6868 test_efer_vmlaunch(fld, true); 6869 report_prefix_pop(); 6870 } 6871 } 6872 6873 vmcs_write(ctrl_fld, ctrl_saved | ctrl_bit1); 6874 for (i = 0; i < 64; i++) { 6875 if ((1ull << i) & efer_reserved_bits) { 6876 efer = efer_saved | (1ull << i); 6877 vmcs_write(fld, efer); 6878 report_prefix_pushf("%s %lx", fld_name, efer); 6879 test_efer_vmlaunch(fld, false); 6880 report_prefix_pop(); 6881 } 6882 } 6883 6884 vmcs_write(ctrl_fld, ctrl_saved); 6885 vmcs_write(fld, efer_saved); 6886 6887 /* 6888 * Check LMA and LME bits 6889 */ 6890 test_efer_bit(fld, fld_name, 6891 ctrl_fld, ctrl_bit1, 6892 EFER_LMA, 6893 "EFER_LMA"); 6894 test_efer_bit(fld, fld_name, 6895 ctrl_fld, ctrl_bit1, 6896 EFER_LME, 6897 "EFER_LME"); 6898 6899 test_entry_exit_mode: 6900 test_efer_bit(fld, fld_name, 6901 ctrl_fld, ctrl_bit2, 6902 EFER_LMA, 6903 "EFER_LMA"); 6904 test_efer_bit(fld, fld_name, 6905 ctrl_fld, ctrl_bit2, 6906 EFER_LME, 6907 "EFER_LME"); 6908 } 6909 6910 /* 6911 * If the 'load IA32_EFER' VM-exit control is 1, bits reserved in the 6912 * IA32_EFER MSR must be 0 in the field for that register. In addition, 6913 * the values of the LMA and LME bits in the field must each be that of 6914 * the 'host address-space size' VM-exit control. 6915 * 6916 * [Intel SDM] 6917 */ 6918 static void test_host_efer(void) 6919 { 6920 test_efer(HOST_EFER, "HOST_EFER", EXI_CONTROLS, 6921 ctrl_exit_rev.clr & EXI_LOAD_EFER, 6922 EXI_HOST_64); 6923 } 6924 6925 /* 6926 * If the 'load IA32_EFER' VM-enter control is 1, bits reserved in the 6927 * IA32_EFER MSR must be 0 in the field for that register. In addition, 6928 * the values of the LMA and LME bits in the field must each be that of 6929 * the 'IA32e-mode guest' VM-exit control. 6930 */ 6931 static void test_guest_efer(void) 6932 { 6933 if (!(ctrl_enter_rev.clr & ENT_LOAD_EFER)) { 6934 printf("\"Load-IA32-EFER\" entry control not supported\n"); 6935 return; 6936 } 6937 6938 vmcs_write(GUEST_EFER, rdmsr(MSR_EFER)); 6939 test_efer(GUEST_EFER, "GUEST_EFER", ENT_CONTROLS, 6940 ctrl_enter_rev.clr & ENT_LOAD_EFER, 6941 ENT_GUEST_64); 6942 } 6943 6944 /* 6945 * PAT values higher than 8 are uninteresting since they're likely lumped 6946 * in with "8". We only test values above 8 one bit at a time, 6947 * in order to reduce the number of VM-Entries and keep the runtime reasonable. 6948 */ 6949 #define PAT_VAL_LIMIT 8 6950 6951 static void test_pat(u32 field, const char * field_name, u32 ctrl_field, 6952 u64 ctrl_bit) 6953 { 6954 u32 ctrl_saved = vmcs_read(ctrl_field); 6955 u64 pat_saved = vmcs_read(field); 6956 u64 i, val; 6957 u32 j; 6958 int error; 6959 6960 vmcs_clear_bits(ctrl_field, ctrl_bit); 6961 6962 for (i = 0; i < 256; i = (i < PAT_VAL_LIMIT) ? i + 1 : i * 2) { 6963 /* Test PAT0..PAT7 fields */ 6964 for (j = 0; j < (i ? 8 : 1); j++) { 6965 val = i << j * 8; 6966 vmcs_write(field, val); 6967 if (field == HOST_PAT) { 6968 report_prefix_pushf("%s %lx", field_name, val); 6969 test_vmx_vmlaunch(0); 6970 report_prefix_pop(); 6971 6972 } else { // GUEST_PAT 6973 enter_guest(); 6974 report_guest_state_test("ENT_LOAD_PAT enabled", 6975 VMX_VMCALL, val, 6976 "GUEST_PAT"); 6977 } 6978 } 6979 } 6980 6981 vmcs_set_bits(ctrl_field, ctrl_bit); 6982 for (i = 0; i < 256; i = (i < PAT_VAL_LIMIT) ? i + 1 : i * 2) { 6983 /* Test PAT0..PAT7 fields */ 6984 for (j = 0; j < (i ? 8 : 1); j++) { 6985 val = i << j * 8; 6986 vmcs_write(field, val); 6987 6988 if (field == HOST_PAT) { 6989 report_prefix_pushf("%s %lx", field_name, val); 6990 if (i == 0x2 || i == 0x3 || i >= 0x8) 6991 error = 6992 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD; 6993 else 6994 error = 0; 6995 6996 test_vmx_vmlaunch(error); 6997 report_prefix_pop(); 6998 6999 } else { // GUEST_PAT 7000 if (i == 0x2 || i == 0x3 || i >= 0x8) { 7001 enter_guest_with_invalid_guest_state(); 7002 report_guest_state_test("ENT_LOAD_PAT " 7003 "enabled", 7004 VMX_FAIL_STATE | VMX_ENTRY_FAILURE, 7005 val, 7006 "GUEST_PAT"); 7007 } else { 7008 enter_guest(); 7009 report_guest_state_test("ENT_LOAD_PAT " 7010 "enabled", 7011 VMX_VMCALL, 7012 val, 7013 "GUEST_PAT"); 7014 } 7015 } 7016 7017 } 7018 } 7019 7020 vmcs_write(ctrl_field, ctrl_saved); 7021 vmcs_write(field, pat_saved); 7022 } 7023 7024 /* 7025 * If the "load IA32_PAT" VM-exit control is 1, the value of the field 7026 * for the IA32_PAT MSR must be one that could be written by WRMSR 7027 * without fault at CPL 0. Specifically, each of the 8 bytes in the 7028 * field must have one of the values 0 (UC), 1 (WC), 4 (WT), 5 (WP), 7029 * 6 (WB), or 7 (UC-). 7030 * 7031 * [Intel SDM] 7032 */ 7033 static void test_load_host_pat(void) 7034 { 7035 /* 7036 * "load IA32_PAT" VM-exit control 7037 */ 7038 if (!(ctrl_exit_rev.clr & EXI_LOAD_PAT)) { 7039 printf("\"Load-IA32-PAT\" exit control not supported\n"); 7040 return; 7041 } 7042 7043 test_pat(HOST_PAT, "HOST_PAT", EXI_CONTROLS, EXI_LOAD_PAT); 7044 } 7045 7046 union cpuidA_eax { 7047 struct { 7048 unsigned int version_id:8; 7049 unsigned int num_counters_gp:8; 7050 unsigned int bit_width:8; 7051 unsigned int mask_length:8; 7052 } split; 7053 unsigned int full; 7054 }; 7055 7056 union cpuidA_edx { 7057 struct { 7058 unsigned int num_counters_fixed:5; 7059 unsigned int bit_width_fixed:8; 7060 unsigned int reserved:9; 7061 } split; 7062 unsigned int full; 7063 }; 7064 7065 static bool valid_pgc(u64 val) 7066 { 7067 struct cpuid id; 7068 union cpuidA_eax eax; 7069 union cpuidA_edx edx; 7070 u64 mask; 7071 7072 id = cpuid(0xA); 7073 eax.full = id.a; 7074 edx.full = id.d; 7075 mask = ~(((1ull << eax.split.num_counters_gp) - 1) | 7076 (((1ull << edx.split.num_counters_fixed) - 1) << 32)); 7077 7078 return !(val & mask); 7079 } 7080 7081 static void test_pgc_vmlaunch(u32 xerror, u32 xreason, bool xfail, bool host) 7082 { 7083 u32 inst_err; 7084 u64 obs; 7085 bool success; 7086 struct vmx_state_area_test_data *data = &vmx_state_area_test_data; 7087 7088 if (host) { 7089 success = vmlaunch_succeeds(); 7090 obs = rdmsr(data->msr); 7091 if (!success) { 7092 inst_err = vmcs_read(VMX_INST_ERROR); 7093 report(xerror == inst_err, "vmlaunch failed, " 7094 "VMX Inst Error is %d (expected %d)", 7095 inst_err, xerror); 7096 } else { 7097 report(!data->enabled || data->exp == obs, 7098 "Host state is 0x%lx (expected 0x%lx)", 7099 obs, data->exp); 7100 report(success != xfail, "vmlaunch succeeded"); 7101 } 7102 } else { 7103 if (xfail) { 7104 enter_guest_with_invalid_guest_state(); 7105 } else { 7106 enter_guest(); 7107 } 7108 report_guest_state_test("load GUEST_PERF_GLOBAL_CTRL", 7109 xreason, GUEST_PERF_GLOBAL_CTRL, 7110 "GUEST_PERF_GLOBAL_CTRL"); 7111 } 7112 } 7113 7114 /* 7115 * test_load_perf_global_ctrl is a generic function for testing the 7116 * "load IA32_PERF_GLOBAL_CTRL" VM-{Entry,Exit} controls. This test function 7117 * tests the provided ctrl_val when disabled and enabled. 7118 * 7119 * @nr: VMCS field number corresponding to the host/guest state field 7120 * @name: Name of the above VMCS field for printing in test report 7121 * @ctrl_nr: VMCS field number corresponding to the VM-{Entry,Exit} control 7122 * @ctrl_val: Bit to set on the ctrl_field 7123 */ 7124 static void test_perf_global_ctrl(u32 nr, const char *name, u32 ctrl_nr, 7125 const char *ctrl_name, u64 ctrl_val) 7126 { 7127 u64 ctrl_saved = vmcs_read(ctrl_nr); 7128 u64 pgc_saved = vmcs_read(nr); 7129 u64 i, val; 7130 bool host = nr == HOST_PERF_GLOBAL_CTRL; 7131 struct vmx_state_area_test_data *data = &vmx_state_area_test_data; 7132 7133 data->msr = MSR_CORE_PERF_GLOBAL_CTRL; 7134 msr_bmp_init(); 7135 vmcs_write(ctrl_nr, ctrl_saved & ~ctrl_val); 7136 data->enabled = false; 7137 report_prefix_pushf("\"load IA32_PERF_GLOBAL_CTRL\"=0 on %s", 7138 ctrl_name); 7139 7140 for (i = 0; i < 64; i++) { 7141 val = 1ull << i; 7142 vmcs_write(nr, val); 7143 report_prefix_pushf("%s = 0x%lx", name, val); 7144 test_pgc_vmlaunch(0, VMX_VMCALL, false, host); 7145 report_prefix_pop(); 7146 } 7147 report_prefix_pop(); 7148 7149 vmcs_write(ctrl_nr, ctrl_saved | ctrl_val); 7150 data->enabled = true; 7151 report_prefix_pushf("\"load IA32_PERF_GLOBAL_CTRL\"=1 on %s", 7152 ctrl_name); 7153 for (i = 0; i < 64; i++) { 7154 val = 1ull << i; 7155 data->exp = val; 7156 vmcs_write(nr, val); 7157 report_prefix_pushf("%s = 0x%lx", name, val); 7158 if (valid_pgc(val)) { 7159 test_pgc_vmlaunch(0, VMX_VMCALL, false, host); 7160 } else { 7161 if (host) 7162 test_pgc_vmlaunch( 7163 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD, 7164 0, 7165 true, 7166 host); 7167 else 7168 test_pgc_vmlaunch( 7169 0, 7170 VMX_ENTRY_FAILURE | VMX_FAIL_STATE, 7171 true, 7172 host); 7173 } 7174 report_prefix_pop(); 7175 } 7176 7177 data->enabled = false; 7178 report_prefix_pop(); 7179 vmcs_write(ctrl_nr, ctrl_saved); 7180 vmcs_write(nr, pgc_saved); 7181 } 7182 7183 static void test_load_host_perf_global_ctrl(void) 7184 { 7185 if (!(ctrl_exit_rev.clr & EXI_LOAD_PERF)) { 7186 printf("\"load IA32_PERF_GLOBAL_CTRL\" exit control not supported\n"); 7187 return; 7188 } 7189 7190 test_perf_global_ctrl(HOST_PERF_GLOBAL_CTRL, "HOST_PERF_GLOBAL_CTRL", 7191 EXI_CONTROLS, "EXI_CONTROLS", EXI_LOAD_PERF); 7192 } 7193 7194 7195 static void test_load_guest_perf_global_ctrl(void) 7196 { 7197 if (!(ctrl_enter_rev.clr & ENT_LOAD_PERF)) { 7198 printf("\"load IA32_PERF_GLOBAL_CTRL\" entry control not supported\n"); 7199 return; 7200 } 7201 7202 test_perf_global_ctrl(GUEST_PERF_GLOBAL_CTRL, "GUEST_PERF_GLOBAL_CTRL", 7203 ENT_CONTROLS, "ENT_CONTROLS", ENT_LOAD_PERF); 7204 } 7205 7206 7207 /* 7208 * test_vmcs_field - test a value for the given VMCS field 7209 * @field: VMCS field 7210 * @field_name: string name of VMCS field 7211 * @bit_start: starting bit 7212 * @bit_end: ending bit 7213 * @val: value that the bit range must or must not contain 7214 * @valid_val: whether value given in 'val' must be valid or not 7215 * @error: expected VMCS error when vmentry fails for an invalid value 7216 */ 7217 static void test_vmcs_field(u64 field, const char *field_name, u32 bit_start, 7218 u32 bit_end, u64 val, bool valid_val, u32 error) 7219 { 7220 u64 field_saved = vmcs_read(field); 7221 u32 i; 7222 u64 tmp; 7223 u32 bit_on; 7224 u64 mask = ~0ull; 7225 7226 mask = (mask >> bit_end) << bit_end; 7227 mask = mask | ((1 << bit_start) - 1); 7228 tmp = (field_saved & mask) | (val << bit_start); 7229 7230 vmcs_write(field, tmp); 7231 report_prefix_pushf("%s %lx", field_name, tmp); 7232 if (valid_val) 7233 test_vmx_vmlaunch(0); 7234 else 7235 test_vmx_vmlaunch(error); 7236 report_prefix_pop(); 7237 7238 for (i = bit_start; i <= bit_end; i = i + 2) { 7239 bit_on = ((1ull < i) & (val << bit_start)) ? 0 : 1; 7240 if (bit_on) 7241 tmp = field_saved | (1ull << i); 7242 else 7243 tmp = field_saved & ~(1ull << i); 7244 vmcs_write(field, tmp); 7245 report_prefix_pushf("%s %lx", field_name, tmp); 7246 if (valid_val) 7247 test_vmx_vmlaunch(error); 7248 else 7249 test_vmx_vmlaunch(0); 7250 report_prefix_pop(); 7251 } 7252 7253 vmcs_write(field, field_saved); 7254 } 7255 7256 static void test_canonical(u64 field, const char * field_name, bool host) 7257 { 7258 u64 addr_saved = vmcs_read(field); 7259 7260 if (is_canonical(addr_saved)) { 7261 if (host) { 7262 report_prefix_pushf("%s %lx", field_name, addr_saved); 7263 test_vmx_vmlaunch(0); 7264 report_prefix_pop(); 7265 } else { 7266 enter_guest(); 7267 report_guest_state_test("Test canonical address", 7268 VMX_VMCALL, addr_saved, 7269 field_name); 7270 } 7271 7272 vmcs_write(field, NONCANONICAL); 7273 7274 if (host) { 7275 report_prefix_pushf("%s %llx", field_name, NONCANONICAL); 7276 test_vmx_vmlaunch(VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); 7277 report_prefix_pop(); 7278 } else { 7279 enter_guest_with_invalid_guest_state(); 7280 report_guest_state_test("Test canonical address", 7281 VMX_FAIL_STATE | VMX_ENTRY_FAILURE, 7282 NONCANONICAL, field_name); 7283 } 7284 7285 vmcs_write(field, addr_saved); 7286 } else { 7287 if (host) { 7288 report_prefix_pushf("%s %llx", field_name, NONCANONICAL); 7289 test_vmx_vmlaunch(VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); 7290 report_prefix_pop(); 7291 } else { 7292 enter_guest_with_invalid_guest_state(); 7293 report_guest_state_test("Test canonical address", 7294 VMX_FAIL_STATE | VMX_ENTRY_FAILURE, 7295 NONCANONICAL, field_name); 7296 } 7297 } 7298 } 7299 7300 #define TEST_RPL_TI_FLAGS(reg, name) \ 7301 test_vmcs_field(reg, name, 0, 2, 0x0, true, \ 7302 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); 7303 7304 #define TEST_CS_TR_FLAGS(reg, name) \ 7305 test_vmcs_field(reg, name, 3, 15, 0x0000, false, \ 7306 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); 7307 7308 /* 7309 * 1. In the selector field for each of CS, SS, DS, ES, FS, GS and TR, the 7310 * RPL (bits 1:0) and the TI flag (bit 2) must be 0. 7311 * 2. The selector fields for CS and TR cannot be 0000H. 7312 * 3. The selector field for SS cannot be 0000H if the "host address-space 7313 * size" VM-exit control is 0. 7314 * 4. On processors that support Intel 64 architecture, the base-address 7315 * fields for FS, GS and TR must contain canonical addresses. 7316 */ 7317 static void test_host_segment_regs(void) 7318 { 7319 u16 selector_saved; 7320 7321 /* 7322 * Test RPL and TI flags 7323 */ 7324 TEST_RPL_TI_FLAGS(HOST_SEL_CS, "HOST_SEL_CS"); 7325 TEST_RPL_TI_FLAGS(HOST_SEL_SS, "HOST_SEL_SS"); 7326 TEST_RPL_TI_FLAGS(HOST_SEL_DS, "HOST_SEL_DS"); 7327 TEST_RPL_TI_FLAGS(HOST_SEL_ES, "HOST_SEL_ES"); 7328 TEST_RPL_TI_FLAGS(HOST_SEL_FS, "HOST_SEL_FS"); 7329 TEST_RPL_TI_FLAGS(HOST_SEL_GS, "HOST_SEL_GS"); 7330 TEST_RPL_TI_FLAGS(HOST_SEL_TR, "HOST_SEL_TR"); 7331 7332 /* 7333 * Test that CS and TR fields can not be 0x0000 7334 */ 7335 TEST_CS_TR_FLAGS(HOST_SEL_CS, "HOST_SEL_CS"); 7336 TEST_CS_TR_FLAGS(HOST_SEL_TR, "HOST_SEL_TR"); 7337 7338 /* 7339 * SS field can not be 0x0000 if "host address-space size" VM-exit 7340 * control is 0 7341 */ 7342 selector_saved = vmcs_read(HOST_SEL_SS); 7343 vmcs_write(HOST_SEL_SS, 0); 7344 report_prefix_pushf("HOST_SEL_SS 0"); 7345 if (vmcs_read(EXI_CONTROLS) & EXI_HOST_64) { 7346 test_vmx_vmlaunch(0); 7347 } else { 7348 test_vmx_vmlaunch(VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); 7349 } 7350 report_prefix_pop(); 7351 7352 vmcs_write(HOST_SEL_SS, selector_saved); 7353 7354 #ifdef __x86_64__ 7355 /* 7356 * Base address for FS, GS and TR must be canonical 7357 */ 7358 test_canonical(HOST_BASE_FS, "HOST_BASE_FS", true); 7359 test_canonical(HOST_BASE_GS, "HOST_BASE_GS", true); 7360 test_canonical(HOST_BASE_TR, "HOST_BASE_TR", true); 7361 #endif 7362 } 7363 7364 /* 7365 * On processors that support Intel 64 architecture, the base-address 7366 * fields for GDTR and IDTR must contain canonical addresses. 7367 */ 7368 static void test_host_desc_tables(void) 7369 { 7370 #ifdef __x86_64__ 7371 test_canonical(HOST_BASE_GDTR, "HOST_BASE_GDTR", true); 7372 test_canonical(HOST_BASE_IDTR, "HOST_BASE_IDTR", true); 7373 #endif 7374 } 7375 7376 /* 7377 * If the "host address-space size" VM-exit control is 0, the following must 7378 * hold: 7379 * - The "IA-32e mode guest" VM-entry control is 0. 7380 * - Bit 17 of the CR4 field (corresponding to CR4.PCIDE) is 0. 7381 * - Bits 63:32 in the RIP field are 0. 7382 * 7383 * If the "host address-space size" VM-exit control is 1, the following must 7384 * hold: 7385 * - Bit 5 of the CR4 field (corresponding to CR4.PAE) is 1. 7386 * - The RIP field contains a canonical address. 7387 * 7388 */ 7389 static void test_host_addr_size(void) 7390 { 7391 u64 cr4_saved = vmcs_read(HOST_CR4); 7392 u64 rip_saved = vmcs_read(HOST_RIP); 7393 u64 entry_ctrl_saved = vmcs_read(ENT_CONTROLS); 7394 int i; 7395 u64 tmp; 7396 7397 if (vmcs_read(EXI_CONTROLS) & EXI_HOST_64) { 7398 vmcs_write(ENT_CONTROLS, entry_ctrl_saved | ENT_GUEST_64); 7399 report_prefix_pushf("\"IA-32e mode guest\" enabled"); 7400 test_vmx_vmlaunch(0); 7401 report_prefix_pop(); 7402 7403 vmcs_write(HOST_CR4, cr4_saved | X86_CR4_PCIDE); 7404 report_prefix_pushf("\"CR4.PCIDE\" set"); 7405 test_vmx_vmlaunch(0); 7406 report_prefix_pop(); 7407 7408 for (i = 32; i <= 63; i = i + 4) { 7409 tmp = rip_saved | 1ull << i; 7410 vmcs_write(HOST_RIP, tmp); 7411 report_prefix_pushf("HOST_RIP %lx", tmp); 7412 test_vmx_vmlaunch(0); 7413 report_prefix_pop(); 7414 } 7415 7416 if (cr4_saved & X86_CR4_PAE) { 7417 vmcs_write(HOST_CR4, cr4_saved & ~X86_CR4_PAE); 7418 report_prefix_pushf("\"CR4.PAE\" unset"); 7419 test_vmx_vmlaunch(VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); 7420 } else { 7421 report_prefix_pushf("\"CR4.PAE\" set"); 7422 test_vmx_vmlaunch(0); 7423 } 7424 report_prefix_pop(); 7425 7426 vmcs_write(HOST_RIP, NONCANONICAL); 7427 report_prefix_pushf("HOST_RIP %llx", NONCANONICAL); 7428 test_vmx_vmlaunch(VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); 7429 report_prefix_pop(); 7430 7431 vmcs_write(ENT_CONTROLS, entry_ctrl_saved | ENT_GUEST_64); 7432 vmcs_write(HOST_RIP, rip_saved); 7433 vmcs_write(HOST_CR4, cr4_saved); 7434 } 7435 } 7436 7437 /* 7438 * Check that the virtual CPU checks the VMX Host State Area as 7439 * documented in the Intel SDM. 7440 */ 7441 static void vmx_host_state_area_test(void) 7442 { 7443 /* 7444 * Bit 1 of the guest's RFLAGS must be 1, or VM-entry will 7445 * fail due to invalid guest state, should we make it that 7446 * far. 7447 */ 7448 vmcs_write(GUEST_RFLAGS, 0); 7449 7450 test_host_ctl_regs(); 7451 7452 test_canonical(HOST_SYSENTER_ESP, "HOST_SYSENTER_ESP", true); 7453 test_canonical(HOST_SYSENTER_EIP, "HOST_SYSENTER_EIP", true); 7454 7455 test_host_efer(); 7456 test_load_host_pat(); 7457 test_host_segment_regs(); 7458 test_host_desc_tables(); 7459 test_host_addr_size(); 7460 test_load_host_perf_global_ctrl(); 7461 } 7462 7463 /* 7464 * If the "load debug controls" VM-entry control is 1, bits 63:32 in 7465 * the DR7 field must be 0. 7466 * 7467 * [Intel SDM] 7468 */ 7469 static void test_guest_dr7(void) 7470 { 7471 u32 ent_saved = vmcs_read(ENT_CONTROLS); 7472 u64 dr7_saved = vmcs_read(GUEST_DR7); 7473 u64 val; 7474 int i; 7475 7476 if (ctrl_enter_rev.set & ENT_LOAD_DBGCTLS) { 7477 vmcs_clear_bits(ENT_CONTROLS, ENT_LOAD_DBGCTLS); 7478 for (i = 0; i < 64; i++) { 7479 val = 1ull << i; 7480 vmcs_write(GUEST_DR7, val); 7481 enter_guest(); 7482 report_guest_state_test("ENT_LOAD_DBGCTLS disabled", 7483 VMX_VMCALL, val, "GUEST_DR7"); 7484 } 7485 } 7486 if (ctrl_enter_rev.clr & ENT_LOAD_DBGCTLS) { 7487 vmcs_set_bits(ENT_CONTROLS, ENT_LOAD_DBGCTLS); 7488 for (i = 0; i < 64; i++) { 7489 val = 1ull << i; 7490 vmcs_write(GUEST_DR7, val); 7491 if (i < 32) 7492 enter_guest(); 7493 else 7494 enter_guest_with_invalid_guest_state(); 7495 report_guest_state_test("ENT_LOAD_DBGCTLS enabled", 7496 i < 32 ? VMX_VMCALL : 7497 VMX_ENTRY_FAILURE | 7498 VMX_FAIL_STATE, 7499 val, "GUEST_DR7"); 7500 } 7501 } 7502 vmcs_write(GUEST_DR7, dr7_saved); 7503 vmcs_write(ENT_CONTROLS, ent_saved); 7504 } 7505 7506 /* 7507 * If the "load IA32_PAT" VM-entry control is 1, the value of the field 7508 * for the IA32_PAT MSR must be one that could be written by WRMSR 7509 * without fault at CPL 0. Specifically, each of the 8 bytes in the 7510 * field must have one of the values 0 (UC), 1 (WC), 4 (WT), 5 (WP), 7511 * 6 (WB), or 7 (UC-). 7512 * 7513 * [Intel SDM] 7514 */ 7515 static void test_load_guest_pat(void) 7516 { 7517 /* 7518 * "load IA32_PAT" VM-entry control 7519 */ 7520 if (!(ctrl_enter_rev.clr & ENT_LOAD_PAT)) { 7521 printf("\"Load-IA32-PAT\" entry control not supported\n"); 7522 return; 7523 } 7524 7525 test_pat(GUEST_PAT, "GUEST_PAT", ENT_CONTROLS, ENT_LOAD_PAT); 7526 } 7527 7528 /* 7529 * Check that the virtual CPU checks the VMX Guest State Area as 7530 * documented in the Intel SDM. 7531 */ 7532 static void vmx_guest_state_area_test(void) 7533 { 7534 vmx_set_test_stage(1); 7535 test_set_guest(guest_state_test_main); 7536 7537 /* 7538 * The IA32_SYSENTER_ESP field and the IA32_SYSENTER_EIP field 7539 * must each contain a canonical address. 7540 */ 7541 test_canonical(GUEST_SYSENTER_ESP, "GUEST_SYSENTER_ESP", false); 7542 test_canonical(GUEST_SYSENTER_EIP, "GUEST_SYSENTER_EIP", false); 7543 7544 test_guest_dr7(); 7545 test_load_guest_pat(); 7546 test_guest_efer(); 7547 test_load_guest_perf_global_ctrl(); 7548 7549 /* 7550 * Let the guest finish execution 7551 */ 7552 vmx_set_test_stage(2); 7553 enter_guest(); 7554 } 7555 7556 static bool valid_vmcs_for_vmentry(void) 7557 { 7558 struct vmcs *current_vmcs = NULL; 7559 7560 if (vmcs_save(¤t_vmcs)) 7561 return false; 7562 7563 return current_vmcs && !current_vmcs->hdr.shadow_vmcs; 7564 } 7565 7566 static void try_vmentry_in_movss_shadow(void) 7567 { 7568 u32 vm_inst_err; 7569 u32 flags; 7570 bool early_failure = false; 7571 u32 expected_flags = X86_EFLAGS_FIXED; 7572 bool valid_vmcs = valid_vmcs_for_vmentry(); 7573 7574 expected_flags |= valid_vmcs ? X86_EFLAGS_ZF : X86_EFLAGS_CF; 7575 7576 /* 7577 * Indirectly set VM_INST_ERR to 12 ("VMREAD/VMWRITE from/to 7578 * unsupported VMCS component"). 7579 */ 7580 vmcs_write(~0u, 0); 7581 7582 __asm__ __volatile__ ("mov %[host_rsp], %%edx;" 7583 "vmwrite %%rsp, %%rdx;" 7584 "mov 0f, %%rax;" 7585 "mov %[host_rip], %%edx;" 7586 "vmwrite %%rax, %%rdx;" 7587 "mov $-1, %%ah;" 7588 "sahf;" 7589 "mov %%ss, %%ax;" 7590 "mov %%ax, %%ss;" 7591 "vmlaunch;" 7592 "mov $1, %[early_failure];" 7593 "0: lahf;" 7594 "movzbl %%ah, %[flags]" 7595 : [early_failure] "+r" (early_failure), 7596 [flags] "=&a" (flags) 7597 : [host_rsp] "i" (HOST_RSP), 7598 [host_rip] "i" (HOST_RIP) 7599 : "rdx", "cc", "memory"); 7600 vm_inst_err = vmcs_read(VMX_INST_ERROR); 7601 7602 report(early_failure, "Early VM-entry failure"); 7603 report(flags == expected_flags, "RFLAGS[8:0] is %x (actual %x)", 7604 expected_flags, flags); 7605 if (valid_vmcs) 7606 report(vm_inst_err == VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS, 7607 "VM-instruction error is %d (actual %d)", 7608 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS, vm_inst_err); 7609 } 7610 7611 static void vmentry_movss_shadow_test(void) 7612 { 7613 struct vmcs *orig_vmcs; 7614 7615 TEST_ASSERT(!vmcs_save(&orig_vmcs)); 7616 7617 /* 7618 * Set the launched flag on the current VMCS to verify the correct 7619 * error priority, below. 7620 */ 7621 test_set_guest(v2_null_test_guest); 7622 enter_guest(); 7623 7624 /* 7625 * With bit 1 of the guest's RFLAGS clear, VM-entry should 7626 * fail due to invalid guest state (if we make it that far). 7627 */ 7628 vmcs_write(GUEST_RFLAGS, 0); 7629 7630 /* 7631 * "VM entry with events blocked by MOV SS" takes precedence over 7632 * "VMLAUNCH with non-clear VMCS." 7633 */ 7634 report_prefix_push("valid current-VMCS"); 7635 try_vmentry_in_movss_shadow(); 7636 report_prefix_pop(); 7637 7638 /* 7639 * VMfailInvalid takes precedence over "VM entry with events 7640 * blocked by MOV SS." 7641 */ 7642 TEST_ASSERT(!vmcs_clear(orig_vmcs)); 7643 report_prefix_push("no current-VMCS"); 7644 try_vmentry_in_movss_shadow(); 7645 report_prefix_pop(); 7646 7647 TEST_ASSERT(!make_vmcs_current(orig_vmcs)); 7648 vmcs_write(GUEST_RFLAGS, X86_EFLAGS_FIXED); 7649 } 7650 7651 static void vmx_cr_load_test(void) 7652 { 7653 unsigned long cr3, cr4, orig_cr3, orig_cr4; 7654 7655 orig_cr4 = read_cr4(); 7656 orig_cr3 = read_cr3(); 7657 7658 if (!this_cpu_has(X86_FEATURE_PCID)) { 7659 report_skip("PCID not detected"); 7660 return; 7661 } 7662 if (!this_cpu_has(X86_FEATURE_MCE)) { 7663 report_skip("MCE not detected"); 7664 return; 7665 } 7666 7667 TEST_ASSERT(!(orig_cr3 & X86_CR3_PCID_MASK)); 7668 7669 /* Enable PCID for L1. */ 7670 cr4 = orig_cr4 | X86_CR4_PCIDE; 7671 cr3 = orig_cr3 | 0x1; 7672 TEST_ASSERT(!write_cr4_checking(cr4)); 7673 write_cr3(cr3); 7674 7675 test_set_guest(v2_null_test_guest); 7676 vmcs_write(HOST_CR4, cr4); 7677 vmcs_write(HOST_CR3, cr3); 7678 enter_guest(); 7679 7680 /* 7681 * No exception is expected. 7682 * 7683 * NB. KVM loads the last guest write to CR4 into CR4 read 7684 * shadow. In order to trigger an exit to KVM, we can toggle a 7685 * bit that is owned by KVM. We use CR4.MCE, which shall 7686 * have no side effect because normally no guest MCE (e.g., as the 7687 * result of bad memory) would happen during this test. 7688 */ 7689 TEST_ASSERT(!write_cr4_checking(cr4 ^ X86_CR4_MCE)); 7690 7691 /* Cleanup L1 state. */ 7692 write_cr3(orig_cr3); 7693 TEST_ASSERT(!write_cr4_checking(orig_cr4)); 7694 } 7695 7696 static void vmx_nm_test_guest(void) 7697 { 7698 write_cr0(read_cr0() | X86_CR0_TS); 7699 asm volatile("fnop"); 7700 } 7701 7702 static void check_nm_exit(const char *test) 7703 { 7704 u32 reason = vmcs_read(EXI_REASON); 7705 u32 intr_info = vmcs_read(EXI_INTR_INFO); 7706 const u32 expected = INTR_INFO_VALID_MASK | INTR_TYPE_HARD_EXCEPTION | 7707 NM_VECTOR; 7708 7709 report(reason == VMX_EXC_NMI && intr_info == expected, "%s", test); 7710 } 7711 7712 /* 7713 * This test checks that: 7714 * 7715 * (a) If L2 launches with CR0.TS clear, but later sets CR0.TS, then 7716 * a subsequent #NM VM-exit is reflected to L1. 7717 * 7718 * (b) If L2 launches with CR0.TS clear and CR0.EM set, then a 7719 * subsequent #NM VM-exit is reflected to L1. 7720 */ 7721 static void vmx_nm_test(void) 7722 { 7723 unsigned long cr0 = read_cr0(); 7724 7725 test_set_guest(vmx_nm_test_guest); 7726 7727 /* 7728 * L1 wants to intercept #NM exceptions encountered in L2. 7729 */ 7730 vmcs_write(EXC_BITMAP, 1 << NM_VECTOR); 7731 7732 /* 7733 * Launch L2 with CR0.TS clear, but don't claim host ownership of 7734 * any CR0 bits. L2 will set CR0.TS and then try to execute fnop, 7735 * which will raise #NM. L0 should reflect the #NM VM-exit to L1. 7736 */ 7737 vmcs_write(CR0_MASK, 0); 7738 vmcs_write(GUEST_CR0, cr0 & ~X86_CR0_TS); 7739 enter_guest(); 7740 check_nm_exit("fnop with CR0.TS set in L2 triggers #NM VM-exit to L1"); 7741 7742 /* 7743 * Re-enter L2 at the fnop instruction, with CR0.TS clear but 7744 * CR0.EM set. The fnop will still raise #NM, and L0 should 7745 * reflect the #NM VM-exit to L1. 7746 */ 7747 vmcs_write(GUEST_CR0, (cr0 & ~X86_CR0_TS) | X86_CR0_EM); 7748 enter_guest(); 7749 check_nm_exit("fnop with CR0.EM set in L2 triggers #NM VM-exit to L1"); 7750 7751 /* 7752 * Re-enter L2 at the fnop instruction, with both CR0.TS and 7753 * CR0.EM clear. There will be no #NM, and the L2 guest should 7754 * exit normally. 7755 */ 7756 vmcs_write(GUEST_CR0, cr0 & ~(X86_CR0_TS | X86_CR0_EM)); 7757 enter_guest(); 7758 } 7759 7760 bool vmx_pending_event_ipi_fired; 7761 static void vmx_pending_event_ipi_isr(isr_regs_t *regs) 7762 { 7763 vmx_pending_event_ipi_fired = true; 7764 eoi(); 7765 } 7766 7767 bool vmx_pending_event_guest_run; 7768 static void vmx_pending_event_guest(void) 7769 { 7770 vmcall(); 7771 vmx_pending_event_guest_run = true; 7772 } 7773 7774 static void vmx_pending_event_test_core(bool guest_hlt) 7775 { 7776 int ipi_vector = 0xf1; 7777 7778 vmx_pending_event_ipi_fired = false; 7779 handle_irq(ipi_vector, vmx_pending_event_ipi_isr); 7780 7781 vmx_pending_event_guest_run = false; 7782 test_set_guest(vmx_pending_event_guest); 7783 7784 vmcs_set_bits(PIN_CONTROLS, PIN_EXTINT); 7785 7786 enter_guest(); 7787 skip_exit_vmcall(); 7788 7789 if (guest_hlt) 7790 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 7791 7792 irq_disable(); 7793 apic_icr_write(APIC_DEST_SELF | APIC_DEST_PHYSICAL | 7794 APIC_DM_FIXED | ipi_vector, 7795 0); 7796 7797 enter_guest(); 7798 7799 assert_exit_reason(VMX_EXTINT); 7800 report(!vmx_pending_event_guest_run, 7801 "Guest did not run before host received IPI"); 7802 7803 irq_enable(); 7804 asm volatile ("nop"); 7805 irq_disable(); 7806 report(vmx_pending_event_ipi_fired, 7807 "Got pending interrupt after IRQ enabled"); 7808 7809 if (guest_hlt) 7810 vmcs_write(GUEST_ACTV_STATE, ACTV_ACTIVE); 7811 7812 enter_guest(); 7813 report(vmx_pending_event_guest_run, 7814 "Guest finished running when no interrupt"); 7815 } 7816 7817 static void vmx_pending_event_test(void) 7818 { 7819 vmx_pending_event_test_core(false); 7820 } 7821 7822 static void vmx_pending_event_hlt_test(void) 7823 { 7824 vmx_pending_event_test_core(true); 7825 } 7826 7827 static int vmx_window_test_db_count; 7828 7829 static void vmx_window_test_db_handler(struct ex_regs *regs) 7830 { 7831 vmx_window_test_db_count++; 7832 } 7833 7834 static void vmx_nmi_window_test_guest(void) 7835 { 7836 handle_exception(DB_VECTOR, vmx_window_test_db_handler); 7837 7838 asm volatile("vmcall\n\t" 7839 "nop\n\t"); 7840 7841 handle_exception(DB_VECTOR, NULL); 7842 } 7843 7844 static void verify_nmi_window_exit(u64 rip) 7845 { 7846 u32 exit_reason = vmcs_read(EXI_REASON); 7847 7848 report(exit_reason == VMX_NMI_WINDOW, 7849 "Exit reason (%d) is 'NMI window'", exit_reason); 7850 report(vmcs_read(GUEST_RIP) == rip, "RIP (%#lx) is %#lx", 7851 vmcs_read(GUEST_RIP), rip); 7852 vmcs_write(GUEST_ACTV_STATE, ACTV_ACTIVE); 7853 } 7854 7855 static void vmx_nmi_window_test(void) 7856 { 7857 u64 nop_addr; 7858 void *db_fault_addr = get_idt_addr(&boot_idt[DB_VECTOR]); 7859 7860 if (!(ctrl_pin_rev.clr & PIN_VIRT_NMI)) { 7861 report_skip("CPU does not support the \"Virtual NMIs\" VM-execution control."); 7862 return; 7863 } 7864 7865 if (!(ctrl_cpu_rev[0].clr & CPU_NMI_WINDOW)) { 7866 report_skip("CPU does not support the \"NMI-window exiting\" VM-execution control."); 7867 return; 7868 } 7869 7870 vmx_window_test_db_count = 0; 7871 7872 report_prefix_push("NMI-window"); 7873 test_set_guest(vmx_nmi_window_test_guest); 7874 vmcs_set_bits(PIN_CONTROLS, PIN_VIRT_NMI); 7875 enter_guest(); 7876 skip_exit_vmcall(); 7877 nop_addr = vmcs_read(GUEST_RIP); 7878 7879 /* 7880 * Ask for "NMI-window exiting," and expect an immediate VM-exit. 7881 * RIP will not advance. 7882 */ 7883 report_prefix_push("active, no blocking"); 7884 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_NMI_WINDOW); 7885 enter_guest(); 7886 verify_nmi_window_exit(nop_addr); 7887 report_prefix_pop(); 7888 7889 /* 7890 * Ask for "NMI-window exiting" in a MOV-SS shadow, and expect 7891 * a VM-exit on the next instruction after the nop. (The nop 7892 * is one byte.) 7893 */ 7894 report_prefix_push("active, blocking by MOV-SS"); 7895 vmcs_write(GUEST_INTR_STATE, GUEST_INTR_STATE_MOVSS); 7896 enter_guest(); 7897 verify_nmi_window_exit(nop_addr + 1); 7898 report_prefix_pop(); 7899 7900 /* 7901 * Ask for "NMI-window exiting" (with event injection), and 7902 * expect a VM-exit after the event is injected. (RIP should 7903 * be at the address specified in the IDT entry for #DB.) 7904 */ 7905 report_prefix_push("active, no blocking, injecting #DB"); 7906 vmcs_write(ENT_INTR_INFO, 7907 INTR_INFO_VALID_MASK | INTR_TYPE_HARD_EXCEPTION | DB_VECTOR); 7908 enter_guest(); 7909 verify_nmi_window_exit((u64)db_fault_addr); 7910 report_prefix_pop(); 7911 7912 /* 7913 * Ask for "NMI-window exiting" with NMI blocking, and expect 7914 * a VM-exit after the next IRET (i.e. after the #DB handler 7915 * returns). So, RIP should be back at one byte past the nop. 7916 */ 7917 report_prefix_push("active, blocking by NMI"); 7918 vmcs_write(GUEST_INTR_STATE, GUEST_INTR_STATE_NMI); 7919 enter_guest(); 7920 verify_nmi_window_exit(nop_addr + 1); 7921 report(vmx_window_test_db_count == 1, 7922 "#DB handler executed once (actual %d times)", 7923 vmx_window_test_db_count); 7924 report_prefix_pop(); 7925 7926 if (!(rdmsr(MSR_IA32_VMX_MISC) & (1 << 6))) { 7927 report_skip("CPU does not support activity state HLT."); 7928 } else { 7929 /* 7930 * Ask for "NMI-window exiting" when entering activity 7931 * state HLT, and expect an immediate VM-exit. RIP is 7932 * still one byte past the nop. 7933 */ 7934 report_prefix_push("halted, no blocking"); 7935 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 7936 enter_guest(); 7937 verify_nmi_window_exit(nop_addr + 1); 7938 report_prefix_pop(); 7939 7940 /* 7941 * Ask for "NMI-window exiting" when entering activity 7942 * state HLT (with event injection), and expect a 7943 * VM-exit after the event is injected. (RIP should be 7944 * at the address specified in the IDT entry for #DB.) 7945 */ 7946 report_prefix_push("halted, no blocking, injecting #DB"); 7947 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 7948 vmcs_write(ENT_INTR_INFO, 7949 INTR_INFO_VALID_MASK | INTR_TYPE_HARD_EXCEPTION | 7950 DB_VECTOR); 7951 enter_guest(); 7952 verify_nmi_window_exit((u64)db_fault_addr); 7953 report_prefix_pop(); 7954 } 7955 7956 vmcs_clear_bits(CPU_EXEC_CTRL0, CPU_NMI_WINDOW); 7957 enter_guest(); 7958 report_prefix_pop(); 7959 } 7960 7961 static void vmx_intr_window_test_guest(void) 7962 { 7963 handle_exception(DB_VECTOR, vmx_window_test_db_handler); 7964 7965 /* 7966 * The two consecutive STIs are to ensure that only the first 7967 * one has a shadow. Note that NOP and STI are one byte 7968 * instructions. 7969 */ 7970 asm volatile("vmcall\n\t" 7971 "nop\n\t" 7972 "sti\n\t" 7973 "sti\n\t"); 7974 7975 handle_exception(DB_VECTOR, NULL); 7976 } 7977 7978 static void verify_intr_window_exit(u64 rip) 7979 { 7980 u32 exit_reason = vmcs_read(EXI_REASON); 7981 7982 report(exit_reason == VMX_INTR_WINDOW, 7983 "Exit reason (%d) is 'interrupt window'", exit_reason); 7984 report(vmcs_read(GUEST_RIP) == rip, "RIP (%#lx) is %#lx", 7985 vmcs_read(GUEST_RIP), rip); 7986 vmcs_write(GUEST_ACTV_STATE, ACTV_ACTIVE); 7987 } 7988 7989 static void vmx_intr_window_test(void) 7990 { 7991 u64 vmcall_addr; 7992 u64 nop_addr; 7993 unsigned int orig_db_gate_type; 7994 void *db_fault_addr = get_idt_addr(&boot_idt[DB_VECTOR]); 7995 7996 if (!(ctrl_cpu_rev[0].clr & CPU_INTR_WINDOW)) { 7997 report_skip("CPU does not support the \"interrupt-window exiting\" VM-execution control."); 7998 return; 7999 } 8000 8001 /* 8002 * Change the IDT entry for #DB from interrupt gate to trap gate, 8003 * so that it won't clear RFLAGS.IF. We don't want interrupts to 8004 * be disabled after vectoring a #DB. 8005 */ 8006 orig_db_gate_type = boot_idt[DB_VECTOR].type; 8007 boot_idt[DB_VECTOR].type = 15; 8008 8009 report_prefix_push("interrupt-window"); 8010 test_set_guest(vmx_intr_window_test_guest); 8011 enter_guest(); 8012 assert_exit_reason(VMX_VMCALL); 8013 vmcall_addr = vmcs_read(GUEST_RIP); 8014 8015 /* 8016 * Ask for "interrupt-window exiting" with RFLAGS.IF set and 8017 * no blocking; expect an immediate VM-exit. Note that we have 8018 * not advanced past the vmcall instruction yet, so RIP should 8019 * point to the vmcall instruction. 8020 */ 8021 report_prefix_push("active, no blocking, RFLAGS.IF=1"); 8022 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_INTR_WINDOW); 8023 vmcs_write(GUEST_RFLAGS, X86_EFLAGS_FIXED | X86_EFLAGS_IF); 8024 enter_guest(); 8025 verify_intr_window_exit(vmcall_addr); 8026 report_prefix_pop(); 8027 8028 /* 8029 * Ask for "interrupt-window exiting" (with event injection) 8030 * with RFLAGS.IF set and no blocking; expect a VM-exit after 8031 * the event is injected. That is, RIP should should be at the 8032 * address specified in the IDT entry for #DB. 8033 */ 8034 report_prefix_push("active, no blocking, RFLAGS.IF=1, injecting #DB"); 8035 vmcs_write(ENT_INTR_INFO, 8036 INTR_INFO_VALID_MASK | INTR_TYPE_HARD_EXCEPTION | DB_VECTOR); 8037 vmcall_addr = vmcs_read(GUEST_RIP); 8038 enter_guest(); 8039 verify_intr_window_exit((u64)db_fault_addr); 8040 report_prefix_pop(); 8041 8042 /* 8043 * Let the L2 guest run through the IRET, back to the VMCALL. 8044 * We have to clear the "interrupt-window exiting" 8045 * VM-execution control, or it would just keep causing 8046 * VM-exits. Then, advance past the VMCALL and set the 8047 * "interrupt-window exiting" VM-execution control again. 8048 */ 8049 vmcs_clear_bits(CPU_EXEC_CTRL0, CPU_INTR_WINDOW); 8050 enter_guest(); 8051 skip_exit_vmcall(); 8052 nop_addr = vmcs_read(GUEST_RIP); 8053 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_INTR_WINDOW); 8054 8055 /* 8056 * Ask for "interrupt-window exiting" in a MOV-SS shadow with 8057 * RFLAGS.IF set, and expect a VM-exit on the next 8058 * instruction. (NOP is one byte.) 8059 */ 8060 report_prefix_push("active, blocking by MOV-SS, RFLAGS.IF=1"); 8061 vmcs_write(GUEST_INTR_STATE, GUEST_INTR_STATE_MOVSS); 8062 enter_guest(); 8063 verify_intr_window_exit(nop_addr + 1); 8064 report_prefix_pop(); 8065 8066 /* 8067 * Back up to the NOP and ask for "interrupt-window exiting" 8068 * in an STI shadow with RFLAGS.IF set, and expect a VM-exit 8069 * on the next instruction. (NOP is one byte.) 8070 */ 8071 report_prefix_push("active, blocking by STI, RFLAGS.IF=1"); 8072 vmcs_write(GUEST_RIP, nop_addr); 8073 vmcs_write(GUEST_INTR_STATE, GUEST_INTR_STATE_STI); 8074 enter_guest(); 8075 verify_intr_window_exit(nop_addr + 1); 8076 report_prefix_pop(); 8077 8078 /* 8079 * Ask for "interrupt-window exiting" with RFLAGS.IF clear, 8080 * and expect a VM-exit on the instruction following the STI 8081 * shadow. Only the first STI (which is one byte past the NOP) 8082 * should have a shadow. The second STI (which is two bytes 8083 * past the NOP) has no shadow. Therefore, the interrupt 8084 * window opens at three bytes past the NOP. 8085 */ 8086 report_prefix_push("active, RFLAGS.IF = 0"); 8087 vmcs_write(GUEST_RFLAGS, X86_EFLAGS_FIXED); 8088 enter_guest(); 8089 verify_intr_window_exit(nop_addr + 3); 8090 report_prefix_pop(); 8091 8092 if (!(rdmsr(MSR_IA32_VMX_MISC) & (1 << 6))) { 8093 report_skip("CPU does not support activity state HLT."); 8094 } else { 8095 /* 8096 * Ask for "interrupt-window exiting" when entering 8097 * activity state HLT, and expect an immediate 8098 * VM-exit. RIP is still three bytes past the nop. 8099 */ 8100 report_prefix_push("halted, no blocking"); 8101 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 8102 enter_guest(); 8103 verify_intr_window_exit(nop_addr + 3); 8104 report_prefix_pop(); 8105 8106 /* 8107 * Ask for "interrupt-window exiting" when entering 8108 * activity state HLT (with event injection), and 8109 * expect a VM-exit after the event is injected. That 8110 * is, RIP should should be at the address specified 8111 * in the IDT entry for #DB. 8112 */ 8113 report_prefix_push("halted, no blocking, injecting #DB"); 8114 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 8115 vmcs_write(ENT_INTR_INFO, 8116 INTR_INFO_VALID_MASK | INTR_TYPE_HARD_EXCEPTION | 8117 DB_VECTOR); 8118 enter_guest(); 8119 verify_intr_window_exit((u64)db_fault_addr); 8120 report_prefix_pop(); 8121 } 8122 8123 boot_idt[DB_VECTOR].type = orig_db_gate_type; 8124 vmcs_clear_bits(CPU_EXEC_CTRL0, CPU_INTR_WINDOW); 8125 enter_guest(); 8126 report_prefix_pop(); 8127 } 8128 8129 #define GUEST_TSC_OFFSET (1u << 30) 8130 8131 static u64 guest_tsc; 8132 8133 static void vmx_store_tsc_test_guest(void) 8134 { 8135 guest_tsc = rdtsc(); 8136 } 8137 8138 /* 8139 * This test ensures that when IA32_TSC is in the VM-exit MSR-store 8140 * list, the value saved is not subject to the TSC offset that is 8141 * applied to RDTSC/RDTSCP/RDMSR(IA32_TSC) in guest execution. 8142 */ 8143 static void vmx_store_tsc_test(void) 8144 { 8145 struct vmx_msr_entry msr_entry = { .index = MSR_IA32_TSC }; 8146 u64 low, high; 8147 8148 if (!(ctrl_cpu_rev[0].clr & CPU_USE_TSC_OFFSET)) { 8149 report_skip("'Use TSC offsetting' not supported"); 8150 return; 8151 } 8152 8153 test_set_guest(vmx_store_tsc_test_guest); 8154 8155 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_USE_TSC_OFFSET); 8156 vmcs_write(EXI_MSR_ST_CNT, 1); 8157 vmcs_write(EXIT_MSR_ST_ADDR, virt_to_phys(&msr_entry)); 8158 vmcs_write(TSC_OFFSET, GUEST_TSC_OFFSET); 8159 8160 low = rdtsc(); 8161 enter_guest(); 8162 high = rdtsc(); 8163 8164 report(low + GUEST_TSC_OFFSET <= guest_tsc && 8165 guest_tsc <= high + GUEST_TSC_OFFSET, 8166 "RDTSC value in the guest (%lu) is in range [%lu, %lu]", 8167 guest_tsc, low + GUEST_TSC_OFFSET, high + GUEST_TSC_OFFSET); 8168 report(low <= msr_entry.value && msr_entry.value <= high, 8169 "IA32_TSC value saved in the VM-exit MSR-store list (%lu) is in range [%lu, %lu]", 8170 msr_entry.value, low, high); 8171 } 8172 8173 static void vmx_db_test_guest(void) 8174 { 8175 /* 8176 * For a hardware generated single-step #DB. 8177 */ 8178 asm volatile("vmcall;" 8179 "nop;" 8180 ".Lpost_nop:"); 8181 /* 8182 * ...in a MOVSS shadow, with pending debug exceptions. 8183 */ 8184 asm volatile("vmcall;" 8185 "nop;" 8186 ".Lpost_movss_nop:"); 8187 /* 8188 * For an L0 synthesized single-step #DB. (L0 intercepts WBINVD and 8189 * emulates it in software.) 8190 */ 8191 asm volatile("vmcall;" 8192 "wbinvd;" 8193 ".Lpost_wbinvd:"); 8194 /* 8195 * ...in a MOVSS shadow, with pending debug exceptions. 8196 */ 8197 asm volatile("vmcall;" 8198 "wbinvd;" 8199 ".Lpost_movss_wbinvd:"); 8200 /* 8201 * For a hardware generated single-step #DB in a transactional region. 8202 */ 8203 asm volatile("vmcall;" 8204 ".Lxbegin: xbegin .Lskip_rtm;" 8205 "xend;" 8206 ".Lskip_rtm:"); 8207 } 8208 8209 /* 8210 * Clear the pending debug exceptions and RFLAGS.TF and re-enter 8211 * L2. No #DB is delivered and L2 continues to the next point of 8212 * interest. 8213 */ 8214 static void dismiss_db(void) 8215 { 8216 vmcs_write(GUEST_PENDING_DEBUG, 0); 8217 vmcs_write(GUEST_RFLAGS, X86_EFLAGS_FIXED); 8218 enter_guest(); 8219 } 8220 8221 /* 8222 * Check a variety of VMCS fields relevant to an intercepted #DB exception. 8223 * Then throw away the #DB exception and resume L2. 8224 */ 8225 static void check_db_exit(bool xfail_qual, bool xfail_dr6, bool xfail_pdbg, 8226 void *expected_rip, u64 expected_exit_qual, 8227 u64 expected_dr6) 8228 { 8229 u32 reason = vmcs_read(EXI_REASON); 8230 u32 intr_info = vmcs_read(EXI_INTR_INFO); 8231 u64 exit_qual = vmcs_read(EXI_QUALIFICATION); 8232 u64 guest_rip = vmcs_read(GUEST_RIP); 8233 u64 guest_pending_dbg = vmcs_read(GUEST_PENDING_DEBUG); 8234 u64 dr6 = read_dr6(); 8235 const u32 expected_intr_info = INTR_INFO_VALID_MASK | 8236 INTR_TYPE_HARD_EXCEPTION | DB_VECTOR; 8237 8238 report(reason == VMX_EXC_NMI && intr_info == expected_intr_info, 8239 "Expected #DB VM-exit"); 8240 report((u64)expected_rip == guest_rip, "Expected RIP %p (actual %lx)", 8241 expected_rip, guest_rip); 8242 report_xfail(xfail_pdbg, 0 == guest_pending_dbg, 8243 "Expected pending debug exceptions 0 (actual %lx)", 8244 guest_pending_dbg); 8245 report_xfail(xfail_qual, expected_exit_qual == exit_qual, 8246 "Expected exit qualification %lx (actual %lx)", 8247 expected_exit_qual, exit_qual); 8248 report_xfail(xfail_dr6, expected_dr6 == dr6, 8249 "Expected DR6 %lx (actual %lx)", expected_dr6, dr6); 8250 dismiss_db(); 8251 } 8252 8253 /* 8254 * Assuming the guest has just exited on a VMCALL instruction, skip 8255 * over the vmcall, and set the guest's RFLAGS.TF in the VMCS. If 8256 * pending debug exceptions are non-zero, set the VMCS up as if the 8257 * previous instruction was a MOVSS that generated the indicated 8258 * pending debug exceptions. Then enter L2. 8259 */ 8260 static void single_step_guest(const char *test_name, u64 starting_dr6, 8261 u64 pending_debug_exceptions) 8262 { 8263 printf("\n%s\n", test_name); 8264 skip_exit_vmcall(); 8265 write_dr6(starting_dr6); 8266 vmcs_write(GUEST_RFLAGS, X86_EFLAGS_FIXED | X86_EFLAGS_TF); 8267 if (pending_debug_exceptions) { 8268 vmcs_write(GUEST_PENDING_DEBUG, pending_debug_exceptions); 8269 vmcs_write(GUEST_INTR_STATE, GUEST_INTR_STATE_MOVSS); 8270 } 8271 enter_guest(); 8272 } 8273 8274 /* 8275 * When L1 intercepts #DB, verify that a single-step trap clears 8276 * pending debug exceptions, populates the exit qualification field 8277 * properly, and that DR6 is not prematurely clobbered. In a 8278 * (simulated) MOVSS shadow, make sure that the pending debug 8279 * exception bits are properly accumulated into the exit qualification 8280 * field. 8281 */ 8282 static void vmx_db_test(void) 8283 { 8284 /* 8285 * We are going to set a few arbitrary bits in DR6 to verify that 8286 * (a) DR6 is not modified by an intercepted #DB, and 8287 * (b) stale bits in DR6 (DR6.BD, in particular) don't leak into 8288 * the exit qualification field for a subsequent #DB exception. 8289 */ 8290 const u64 starting_dr6 = DR6_RESERVED | BIT(13) | DR_TRAP3 | DR_TRAP1; 8291 extern char post_nop asm(".Lpost_nop"); 8292 extern char post_movss_nop asm(".Lpost_movss_nop"); 8293 extern char post_wbinvd asm(".Lpost_wbinvd"); 8294 extern char post_movss_wbinvd asm(".Lpost_movss_wbinvd"); 8295 extern char xbegin asm(".Lxbegin"); 8296 extern char skip_rtm asm(".Lskip_rtm"); 8297 8298 /* 8299 * L1 wants to intercept #DB exceptions encountered in L2. 8300 */ 8301 vmcs_write(EXC_BITMAP, BIT(DB_VECTOR)); 8302 8303 /* 8304 * Start L2 and run it up to the first point of interest. 8305 */ 8306 test_set_guest(vmx_db_test_guest); 8307 enter_guest(); 8308 8309 /* 8310 * Hardware-delivered #DB trap for single-step sets the 8311 * standard that L0 has to follow for emulated instructions. 8312 */ 8313 single_step_guest("Hardware delivered single-step", starting_dr6, 0); 8314 check_db_exit(false, false, false, &post_nop, DR_STEP, starting_dr6); 8315 8316 /* 8317 * Hardware-delivered #DB trap for single-step in MOVSS shadow 8318 * also sets the standard that L0 has to follow for emulated 8319 * instructions. Here, we establish the VMCS pending debug 8320 * exceptions to indicate that the simulated MOVSS triggered a 8321 * data breakpoint as well as the single-step trap. 8322 */ 8323 single_step_guest("Hardware delivered single-step in MOVSS shadow", 8324 starting_dr6, BIT(12) | DR_STEP | DR_TRAP0 ); 8325 check_db_exit(false, false, false, &post_movss_nop, DR_STEP | DR_TRAP0, 8326 starting_dr6); 8327 8328 /* 8329 * L0 synthesized #DB trap for single-step is buggy, because 8330 * kvm (a) clobbers DR6 too early, and (b) tries its best to 8331 * reconstitute the exit qualification from the prematurely 8332 * modified DR6, but fails miserably. 8333 */ 8334 single_step_guest("Software synthesized single-step", starting_dr6, 0); 8335 check_db_exit(true, true, false, &post_wbinvd, DR_STEP, starting_dr6); 8336 8337 /* 8338 * L0 synthesized #DB trap for single-step in MOVSS shadow is 8339 * even worse, because L0 also leaves the pending debug 8340 * exceptions in the VMCS instead of accumulating them into 8341 * the exit qualification field for the #DB exception. 8342 */ 8343 single_step_guest("Software synthesized single-step in MOVSS shadow", 8344 starting_dr6, BIT(12) | DR_STEP | DR_TRAP0); 8345 check_db_exit(true, true, true, &post_movss_wbinvd, DR_STEP | DR_TRAP0, 8346 starting_dr6); 8347 8348 /* 8349 * Optional RTM test for hardware that supports RTM, to 8350 * demonstrate that the current volume 3 of the SDM 8351 * (325384-067US), table 27-1 is incorrect. Bit 16 of the exit 8352 * qualification for debug exceptions is not reserved. It is 8353 * set to 1 if a debug exception (#DB) or a breakpoint 8354 * exception (#BP) occurs inside an RTM region while advanced 8355 * debugging of RTM transactional regions is enabled. 8356 */ 8357 if (this_cpu_has(X86_FEATURE_RTM)) { 8358 vmcs_write(ENT_CONTROLS, 8359 vmcs_read(ENT_CONTROLS) | ENT_LOAD_DBGCTLS); 8360 /* 8361 * Set DR7.RTM[bit 11] and IA32_DEBUGCTL.RTM[bit 15] 8362 * in the guest to enable advanced debugging of RTM 8363 * transactional regions. 8364 */ 8365 vmcs_write(GUEST_DR7, BIT(11)); 8366 vmcs_write(GUEST_DEBUGCTL, BIT(15)); 8367 single_step_guest("Hardware delivered single-step in " 8368 "transactional region", starting_dr6, 0); 8369 check_db_exit(false, false, false, &xbegin, BIT(16), 8370 starting_dr6); 8371 } else { 8372 vmcs_write(GUEST_RIP, (u64)&skip_rtm); 8373 enter_guest(); 8374 } 8375 } 8376 8377 static void enable_vid(void) 8378 { 8379 void *virtual_apic_page; 8380 8381 assert(cpu_has_apicv()); 8382 8383 disable_intercept_for_x2apic_msrs(); 8384 8385 virtual_apic_page = alloc_page(); 8386 vmcs_write(APIC_VIRT_ADDR, (u64)virtual_apic_page); 8387 8388 vmcs_set_bits(PIN_CONTROLS, PIN_EXTINT); 8389 8390 vmcs_write(EOI_EXIT_BITMAP0, 0x0); 8391 vmcs_write(EOI_EXIT_BITMAP1, 0x0); 8392 vmcs_write(EOI_EXIT_BITMAP2, 0x0); 8393 vmcs_write(EOI_EXIT_BITMAP3, 0x0); 8394 8395 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_SECONDARY | CPU_TPR_SHADOW); 8396 vmcs_set_bits(CPU_EXEC_CTRL1, CPU_VINTD | CPU_VIRT_X2APIC); 8397 } 8398 8399 static void trigger_ioapic_scan_thread(void *data) 8400 { 8401 /* Wait until other CPU entered L2 */ 8402 while (vmx_get_test_stage() != 1) 8403 ; 8404 8405 /* Trigger ioapic scan */ 8406 ioapic_set_redir(0xf, 0x79, TRIGGER_LEVEL); 8407 vmx_set_test_stage(2); 8408 } 8409 8410 static void irq_79_handler_guest(isr_regs_t *regs) 8411 { 8412 eoi(); 8413 8414 /* L1 expects vmexit on VMX_VMCALL and not VMX_EOI_INDUCED */ 8415 vmcall(); 8416 } 8417 8418 /* 8419 * Constant for num of busy-loop iterations after which 8420 * a timer interrupt should have happened in host 8421 */ 8422 #define TIMER_INTERRUPT_DELAY 100000000 8423 8424 static void vmx_eoi_bitmap_ioapic_scan_test_guest(void) 8425 { 8426 handle_irq(0x79, irq_79_handler_guest); 8427 irq_enable(); 8428 8429 /* Signal to L1 CPU to trigger ioapic scan */ 8430 vmx_set_test_stage(1); 8431 /* Wait until L1 CPU to trigger ioapic scan */ 8432 while (vmx_get_test_stage() != 2) 8433 ; 8434 8435 /* 8436 * Wait for L0 timer interrupt to be raised while we run in L2 8437 * such that L0 will process the IOAPIC scan request before 8438 * resuming L2 8439 */ 8440 delay(TIMER_INTERRUPT_DELAY); 8441 8442 asm volatile ("int $0x79"); 8443 } 8444 8445 static void vmx_eoi_bitmap_ioapic_scan_test(void) 8446 { 8447 if (!cpu_has_apicv() || (cpu_count() < 2)) { 8448 report_skip(__func__); 8449 return; 8450 } 8451 8452 enable_vid(); 8453 8454 on_cpu_async(1, trigger_ioapic_scan_thread, NULL); 8455 test_set_guest(vmx_eoi_bitmap_ioapic_scan_test_guest); 8456 8457 /* 8458 * Launch L2. 8459 * We expect the exit reason to be VMX_VMCALL (and not EOI INDUCED). 8460 * In case the reason isn't VMX_VMCALL, the asserion inside 8461 * skip_exit_vmcall() will fail. 8462 */ 8463 enter_guest(); 8464 skip_exit_vmcall(); 8465 8466 /* Let L2 finish */ 8467 enter_guest(); 8468 report(1, __func__); 8469 } 8470 8471 #define HLT_WITH_RVI_VECTOR (0xf1) 8472 8473 bool vmx_hlt_with_rvi_guest_isr_fired; 8474 static void vmx_hlt_with_rvi_guest_isr(isr_regs_t *regs) 8475 { 8476 vmx_hlt_with_rvi_guest_isr_fired = true; 8477 eoi(); 8478 } 8479 8480 static void vmx_hlt_with_rvi_guest(void) 8481 { 8482 handle_irq(HLT_WITH_RVI_VECTOR, vmx_hlt_with_rvi_guest_isr); 8483 8484 irq_enable(); 8485 asm volatile ("nop"); 8486 8487 vmcall(); 8488 } 8489 8490 static void vmx_hlt_with_rvi_test(void) 8491 { 8492 if (!cpu_has_apicv()) { 8493 report_skip(__func__); 8494 return; 8495 } 8496 8497 enable_vid(); 8498 8499 vmx_hlt_with_rvi_guest_isr_fired = false; 8500 test_set_guest(vmx_hlt_with_rvi_guest); 8501 8502 enter_guest(); 8503 skip_exit_vmcall(); 8504 8505 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 8506 vmcs_write(GUEST_INT_STATUS, HLT_WITH_RVI_VECTOR); 8507 enter_guest(); 8508 8509 report(vmx_hlt_with_rvi_guest_isr_fired, "Interrupt raised in guest"); 8510 } 8511 8512 static void set_irq_line_thread(void *data) 8513 { 8514 /* Wait until other CPU entered L2 */ 8515 while (vmx_get_test_stage() != 1) 8516 ; 8517 8518 /* Set irq-line 0xf to raise vector 0x78 for vCPU 0 */ 8519 ioapic_set_redir(0xf, 0x78, TRIGGER_LEVEL); 8520 vmx_set_test_stage(2); 8521 } 8522 8523 static bool irq_78_handler_vmcall_before_eoi; 8524 static void irq_78_handler_guest(isr_regs_t *regs) 8525 { 8526 set_irq_line(0xf, 0); 8527 if (irq_78_handler_vmcall_before_eoi) 8528 vmcall(); 8529 eoi(); 8530 vmcall(); 8531 } 8532 8533 static void vmx_apic_passthrough_guest(void) 8534 { 8535 handle_irq(0x78, irq_78_handler_guest); 8536 irq_enable(); 8537 8538 /* If requested, wait for other CPU to trigger ioapic scan */ 8539 if (vmx_get_test_stage() < 1) { 8540 vmx_set_test_stage(1); 8541 while (vmx_get_test_stage() != 2) 8542 ; 8543 } 8544 8545 set_irq_line(0xf, 1); 8546 } 8547 8548 static void vmx_apic_passthrough(bool set_irq_line_from_thread) 8549 { 8550 if (set_irq_line_from_thread && (cpu_count() < 2)) { 8551 report_skip(__func__); 8552 return; 8553 } 8554 8555 /* Test device is required for generating IRQs */ 8556 if (!test_device_enabled()) { 8557 report_skip(__func__); 8558 return; 8559 } 8560 u64 cpu_ctrl_0 = CPU_SECONDARY; 8561 u64 cpu_ctrl_1 = 0; 8562 8563 disable_intercept_for_x2apic_msrs(); 8564 8565 vmcs_write(PIN_CONTROLS, vmcs_read(PIN_CONTROLS) & ~PIN_EXTINT); 8566 8567 vmcs_write(CPU_EXEC_CTRL0, vmcs_read(CPU_EXEC_CTRL0) | cpu_ctrl_0); 8568 vmcs_write(CPU_EXEC_CTRL1, vmcs_read(CPU_EXEC_CTRL1) | cpu_ctrl_1); 8569 8570 if (set_irq_line_from_thread) { 8571 irq_78_handler_vmcall_before_eoi = false; 8572 on_cpu_async(1, set_irq_line_thread, NULL); 8573 } else { 8574 irq_78_handler_vmcall_before_eoi = true; 8575 ioapic_set_redir(0xf, 0x78, TRIGGER_LEVEL); 8576 vmx_set_test_stage(2); 8577 } 8578 test_set_guest(vmx_apic_passthrough_guest); 8579 8580 if (irq_78_handler_vmcall_before_eoi) { 8581 /* Before EOI remote_irr should still be set */ 8582 enter_guest(); 8583 skip_exit_vmcall(); 8584 TEST_ASSERT_EQ_MSG(1, (int)ioapic_read_redir(0xf).remote_irr, 8585 "IOAPIC pass-through: remote_irr=1 before EOI"); 8586 } 8587 8588 /* After EOI remote_irr should be cleared */ 8589 enter_guest(); 8590 skip_exit_vmcall(); 8591 TEST_ASSERT_EQ_MSG(0, (int)ioapic_read_redir(0xf).remote_irr, 8592 "IOAPIC pass-through: remote_irr=0 after EOI"); 8593 8594 /* Let L2 finish */ 8595 enter_guest(); 8596 report(1, __func__); 8597 } 8598 8599 static void vmx_apic_passthrough_test(void) 8600 { 8601 vmx_apic_passthrough(false); 8602 } 8603 8604 static void vmx_apic_passthrough_thread_test(void) 8605 { 8606 vmx_apic_passthrough(true); 8607 } 8608 8609 static void vmx_apic_passthrough_tpr_threshold_guest(void) 8610 { 8611 cli(); 8612 apic_set_tpr(0); 8613 } 8614 8615 static bool vmx_apic_passthrough_tpr_threshold_ipi_isr_fired; 8616 static void vmx_apic_passthrough_tpr_threshold_ipi_isr(isr_regs_t *regs) 8617 { 8618 vmx_apic_passthrough_tpr_threshold_ipi_isr_fired = true; 8619 eoi(); 8620 } 8621 8622 static void vmx_apic_passthrough_tpr_threshold_test(void) 8623 { 8624 int ipi_vector = 0xe1; 8625 8626 disable_intercept_for_x2apic_msrs(); 8627 vmcs_clear_bits(PIN_CONTROLS, PIN_EXTINT); 8628 8629 /* Raise L0 TPR-threshold by queueing vector in LAPIC IRR */ 8630 cli(); 8631 apic_set_tpr((ipi_vector >> 4) + 1); 8632 apic_icr_write(APIC_DEST_SELF | APIC_DEST_PHYSICAL | 8633 APIC_DM_FIXED | ipi_vector, 8634 0); 8635 8636 test_set_guest(vmx_apic_passthrough_tpr_threshold_guest); 8637 enter_guest(); 8638 8639 report(apic_get_tpr() == 0, "TPR was zero by guest"); 8640 8641 /* Clean pending self-IPI */ 8642 vmx_apic_passthrough_tpr_threshold_ipi_isr_fired = false; 8643 handle_irq(ipi_vector, vmx_apic_passthrough_tpr_threshold_ipi_isr); 8644 sti(); 8645 asm volatile ("nop"); 8646 report(vmx_apic_passthrough_tpr_threshold_ipi_isr_fired, "self-IPI fired"); 8647 8648 report(1, __func__); 8649 } 8650 8651 static u64 init_signal_test_exit_reason; 8652 static bool init_signal_test_thread_continued; 8653 8654 static void init_signal_test_thread(void *data) 8655 { 8656 struct vmcs *test_vmcs = data; 8657 8658 /* Enter VMX operation (i.e. exec VMXON) */ 8659 u64 *ap_vmxon_region = alloc_page(); 8660 enable_vmx(); 8661 init_vmx(ap_vmxon_region); 8662 _vmx_on(ap_vmxon_region); 8663 8664 /* Signal CPU have entered VMX operation */ 8665 vmx_set_test_stage(1); 8666 8667 /* Wait for BSP CPU to send INIT signal */ 8668 while (vmx_get_test_stage() != 2) 8669 ; 8670 8671 /* 8672 * Signal that we continue as usual as INIT signal 8673 * should be blocked while CPU is in VMX operation 8674 */ 8675 vmx_set_test_stage(3); 8676 8677 /* Wait for signal to enter VMX non-root mode */ 8678 while (vmx_get_test_stage() != 4) 8679 ; 8680 8681 /* Enter VMX non-root mode */ 8682 test_set_guest(v2_null_test_guest); 8683 make_vmcs_current(test_vmcs); 8684 enter_guest(); 8685 /* Save exit reason for BSP CPU to compare to expected result */ 8686 init_signal_test_exit_reason = vmcs_read(EXI_REASON); 8687 /* VMCLEAR test-vmcs so it could be loaded by BSP CPU */ 8688 vmcs_clear(test_vmcs); 8689 launched = false; 8690 /* Signal that CPU exited to VMX root mode */ 8691 vmx_set_test_stage(5); 8692 8693 /* Wait for BSP CPU to signal to exit VMX operation */ 8694 while (vmx_get_test_stage() != 6) 8695 ; 8696 8697 /* Exit VMX operation (i.e. exec VMXOFF) */ 8698 vmx_off(); 8699 8700 /* 8701 * Signal to BSP CPU that we continue as usual as INIT signal 8702 * should have been consumed by VMX_INIT exit from guest 8703 */ 8704 vmx_set_test_stage(7); 8705 8706 /* Wait for BSP CPU to signal to enter VMX operation */ 8707 while (vmx_get_test_stage() != 8) 8708 ; 8709 /* Enter VMX operation (i.e. exec VMXON) */ 8710 _vmx_on(ap_vmxon_region); 8711 /* Signal to BSP we are in VMX operation */ 8712 vmx_set_test_stage(9); 8713 8714 /* Wait for BSP CPU to send INIT signal */ 8715 while (vmx_get_test_stage() != 10) 8716 ; 8717 8718 /* Exit VMX operation (i.e. exec VMXOFF) */ 8719 vmx_off(); 8720 8721 /* 8722 * Exiting VMX operation should result in latched 8723 * INIT signal being processed. Therefore, we should 8724 * never reach the below code. Thus, signal to BSP 8725 * CPU if we have reached here so it is able to 8726 * report an issue if it happens. 8727 */ 8728 init_signal_test_thread_continued = true; 8729 } 8730 8731 #define INIT_SIGNAL_TEST_DELAY 100000000ULL 8732 8733 static void vmx_init_signal_test(void) 8734 { 8735 struct vmcs *test_vmcs; 8736 8737 if (cpu_count() < 2) { 8738 report_skip(__func__); 8739 return; 8740 } 8741 8742 /* VMCLEAR test-vmcs so it could be loaded by other CPU */ 8743 vmcs_save(&test_vmcs); 8744 vmcs_clear(test_vmcs); 8745 8746 vmx_set_test_stage(0); 8747 on_cpu_async(1, init_signal_test_thread, test_vmcs); 8748 8749 /* Wait for other CPU to enter VMX operation */ 8750 while (vmx_get_test_stage() != 1) 8751 ; 8752 8753 /* Send INIT signal to other CPU */ 8754 apic_icr_write(APIC_DEST_PHYSICAL | APIC_DM_INIT | APIC_INT_ASSERT, 8755 id_map[1]); 8756 /* Signal other CPU we have sent INIT signal */ 8757 vmx_set_test_stage(2); 8758 8759 /* 8760 * Wait reasonable amount of time for INIT signal to 8761 * be received on other CPU and verify that other CPU 8762 * have proceed as usual to next test stage as INIT 8763 * signal should be blocked while other CPU in 8764 * VMX operation 8765 */ 8766 delay(INIT_SIGNAL_TEST_DELAY); 8767 report(vmx_get_test_stage() == 3, 8768 "INIT signal blocked when CPU in VMX operation"); 8769 /* No point to continue if we failed at this point */ 8770 if (vmx_get_test_stage() != 3) 8771 return; 8772 8773 /* Signal other CPU to enter VMX non-root mode */ 8774 init_signal_test_exit_reason = -1ull; 8775 vmx_set_test_stage(4); 8776 /* 8777 * Wait reasonable amont of time for other CPU 8778 * to exit to VMX root mode 8779 */ 8780 delay(INIT_SIGNAL_TEST_DELAY); 8781 if (vmx_get_test_stage() != 5) { 8782 report(false, "Pending INIT signal didn't result in VMX exit"); 8783 return; 8784 } 8785 report(init_signal_test_exit_reason == VMX_INIT, 8786 "INIT signal during VMX non-root mode result in exit-reason %s (%lu)", 8787 exit_reason_description(init_signal_test_exit_reason), 8788 init_signal_test_exit_reason); 8789 8790 /* Run guest to completion */ 8791 make_vmcs_current(test_vmcs); 8792 enter_guest(); 8793 8794 /* Signal other CPU to exit VMX operation */ 8795 init_signal_test_thread_continued = false; 8796 vmx_set_test_stage(6); 8797 8798 /* Wait reasonable amount of time for other CPU to exit VMX operation */ 8799 delay(INIT_SIGNAL_TEST_DELAY); 8800 report(vmx_get_test_stage() == 7, 8801 "INIT signal consumed on VMX_INIT exit"); 8802 /* No point to continue if we failed at this point */ 8803 if (vmx_get_test_stage() != 7) 8804 return; 8805 8806 /* Signal other CPU to enter VMX operation */ 8807 vmx_set_test_stage(8); 8808 /* Wait for other CPU to enter VMX operation */ 8809 while (vmx_get_test_stage() != 9) 8810 ; 8811 8812 /* Send INIT signal to other CPU */ 8813 apic_icr_write(APIC_DEST_PHYSICAL | APIC_DM_INIT | APIC_INT_ASSERT, 8814 id_map[1]); 8815 /* Signal other CPU we have sent INIT signal */ 8816 vmx_set_test_stage(10); 8817 8818 /* 8819 * Wait reasonable amount of time for other CPU 8820 * to exit VMX operation and process INIT signal 8821 */ 8822 delay(INIT_SIGNAL_TEST_DELAY); 8823 report(!init_signal_test_thread_continued, 8824 "INIT signal processed after exit VMX operation"); 8825 8826 /* 8827 * TODO: Send SIPI to other CPU to sipi_entry (See x86/cstart64.S) 8828 * to re-init it to kvm-unit-tests standard environment. 8829 * Somehow (?) verify that SIPI was indeed received. 8830 */ 8831 } 8832 8833 enum vmcs_access { 8834 ACCESS_VMREAD, 8835 ACCESS_VMWRITE, 8836 ACCESS_NONE, 8837 }; 8838 8839 struct vmcs_shadow_test_common { 8840 enum vmcs_access op; 8841 enum Reason reason; 8842 u64 field; 8843 u64 value; 8844 u64 flags; 8845 u64 time; 8846 } l1_l2_common; 8847 8848 static inline u64 vmread_flags(u64 field, u64 *val) 8849 { 8850 u64 flags; 8851 8852 asm volatile ("vmread %2, %1; pushf; pop %0" 8853 : "=r" (flags), "=rm" (*val) : "r" (field) : "cc"); 8854 return flags & X86_EFLAGS_ALU; 8855 } 8856 8857 static inline u64 vmwrite_flags(u64 field, u64 val) 8858 { 8859 u64 flags; 8860 8861 asm volatile ("vmwrite %1, %2; pushf; pop %0" 8862 : "=r"(flags) : "rm" (val), "r" (field) : "cc"); 8863 return flags & X86_EFLAGS_ALU; 8864 } 8865 8866 static void vmx_vmcs_shadow_test_guest(void) 8867 { 8868 struct vmcs_shadow_test_common *c = &l1_l2_common; 8869 u64 start; 8870 8871 while (c->op != ACCESS_NONE) { 8872 start = rdtsc(); 8873 switch (c->op) { 8874 default: 8875 c->flags = -1ull; 8876 break; 8877 case ACCESS_VMREAD: 8878 c->flags = vmread_flags(c->field, &c->value); 8879 break; 8880 case ACCESS_VMWRITE: 8881 c->flags = vmwrite_flags(c->field, 0); 8882 break; 8883 } 8884 c->time = rdtsc() - start; 8885 vmcall(); 8886 } 8887 } 8888 8889 static u64 vmread_from_shadow(u64 field) 8890 { 8891 struct vmcs *primary; 8892 struct vmcs *shadow; 8893 u64 value; 8894 8895 TEST_ASSERT(!vmcs_save(&primary)); 8896 shadow = (struct vmcs *)vmcs_read(VMCS_LINK_PTR); 8897 TEST_ASSERT(!make_vmcs_current(shadow)); 8898 value = vmcs_read(field); 8899 TEST_ASSERT(!make_vmcs_current(primary)); 8900 return value; 8901 } 8902 8903 static u64 vmwrite_to_shadow(u64 field, u64 value) 8904 { 8905 struct vmcs *primary; 8906 struct vmcs *shadow; 8907 8908 TEST_ASSERT(!vmcs_save(&primary)); 8909 shadow = (struct vmcs *)vmcs_read(VMCS_LINK_PTR); 8910 TEST_ASSERT(!make_vmcs_current(shadow)); 8911 vmcs_write(field, value); 8912 value = vmcs_read(field); 8913 TEST_ASSERT(!make_vmcs_current(primary)); 8914 return value; 8915 } 8916 8917 static void vmcs_shadow_test_access(u8 *bitmap[2], enum vmcs_access access) 8918 { 8919 struct vmcs_shadow_test_common *c = &l1_l2_common; 8920 8921 c->op = access; 8922 vmcs_write(VMX_INST_ERROR, 0); 8923 enter_guest(); 8924 c->reason = vmcs_read(EXI_REASON) & 0xffff; 8925 if (c->reason != VMX_VMCALL) { 8926 skip_exit_insn(); 8927 enter_guest(); 8928 } 8929 skip_exit_vmcall(); 8930 } 8931 8932 static void vmcs_shadow_test_field(u8 *bitmap[2], u64 field) 8933 { 8934 struct vmcs_shadow_test_common *c = &l1_l2_common; 8935 struct vmcs *shadow; 8936 u64 value; 8937 uintptr_t flags[2]; 8938 bool good_shadow; 8939 u32 vmx_inst_error; 8940 8941 report_prefix_pushf("field %lx", field); 8942 c->field = field; 8943 8944 shadow = (struct vmcs *)vmcs_read(VMCS_LINK_PTR); 8945 if (shadow != (struct vmcs *)-1ull) { 8946 flags[ACCESS_VMREAD] = vmread_flags(field, &value); 8947 flags[ACCESS_VMWRITE] = vmwrite_flags(field, value); 8948 good_shadow = !flags[ACCESS_VMREAD] && !flags[ACCESS_VMWRITE]; 8949 } else { 8950 /* 8951 * When VMCS link pointer is -1ull, VMWRITE/VMREAD on 8952 * shadowed-fields should fail with setting RFLAGS.CF. 8953 */ 8954 flags[ACCESS_VMREAD] = X86_EFLAGS_CF; 8955 flags[ACCESS_VMWRITE] = X86_EFLAGS_CF; 8956 good_shadow = false; 8957 } 8958 8959 /* Intercept both VMREAD and VMWRITE. */ 8960 report_prefix_push("no VMREAD/VMWRITE permission"); 8961 /* VMWRITE/VMREAD done on reserved-bit should always intercept */ 8962 if (!(field >> VMCS_FIELD_RESERVED_SHIFT)) { 8963 set_bit(field, bitmap[ACCESS_VMREAD]); 8964 set_bit(field, bitmap[ACCESS_VMWRITE]); 8965 } 8966 vmcs_shadow_test_access(bitmap, ACCESS_VMWRITE); 8967 report(c->reason == VMX_VMWRITE, "not shadowed for VMWRITE"); 8968 vmcs_shadow_test_access(bitmap, ACCESS_VMREAD); 8969 report(c->reason == VMX_VMREAD, "not shadowed for VMREAD"); 8970 report_prefix_pop(); 8971 8972 if (field >> VMCS_FIELD_RESERVED_SHIFT) 8973 goto out; 8974 8975 /* Permit shadowed VMREAD. */ 8976 report_prefix_push("VMREAD permission only"); 8977 clear_bit(field, bitmap[ACCESS_VMREAD]); 8978 set_bit(field, bitmap[ACCESS_VMWRITE]); 8979 if (good_shadow) 8980 value = vmwrite_to_shadow(field, MAGIC_VAL_1 + field); 8981 vmcs_shadow_test_access(bitmap, ACCESS_VMWRITE); 8982 report(c->reason == VMX_VMWRITE, "not shadowed for VMWRITE"); 8983 vmcs_shadow_test_access(bitmap, ACCESS_VMREAD); 8984 vmx_inst_error = vmcs_read(VMX_INST_ERROR); 8985 report(c->reason == VMX_VMCALL, "shadowed for VMREAD (in %ld cycles)", 8986 c->time); 8987 report(c->flags == flags[ACCESS_VMREAD], 8988 "ALU flags after VMREAD (%lx) are as expected (%lx)", 8989 c->flags, flags[ACCESS_VMREAD]); 8990 if (good_shadow) 8991 report(c->value == value, 8992 "value read from shadow (%lx) is as expected (%lx)", 8993 c->value, value); 8994 else if (shadow != (struct vmcs *)-1ull && flags[ACCESS_VMREAD]) 8995 report(vmx_inst_error == VMXERR_UNSUPPORTED_VMCS_COMPONENT, 8996 "VMX_INST_ERROR (%d) is as expected (%d)", 8997 vmx_inst_error, VMXERR_UNSUPPORTED_VMCS_COMPONENT); 8998 report_prefix_pop(); 8999 9000 /* Permit shadowed VMWRITE. */ 9001 report_prefix_push("VMWRITE permission only"); 9002 set_bit(field, bitmap[ACCESS_VMREAD]); 9003 clear_bit(field, bitmap[ACCESS_VMWRITE]); 9004 if (good_shadow) 9005 vmwrite_to_shadow(field, MAGIC_VAL_1 + field); 9006 vmcs_shadow_test_access(bitmap, ACCESS_VMWRITE); 9007 vmx_inst_error = vmcs_read(VMX_INST_ERROR); 9008 report(c->reason == VMX_VMCALL, 9009 "shadowed for VMWRITE (in %ld cycles)", 9010 c->time); 9011 report(c->flags == flags[ACCESS_VMREAD], 9012 "ALU flags after VMWRITE (%lx) are as expected (%lx)", 9013 c->flags, flags[ACCESS_VMREAD]); 9014 if (good_shadow) { 9015 value = vmread_from_shadow(field); 9016 report(value == 0, 9017 "shadow VMCS value (%lx) is as expected (%lx)", value, 9018 0ul); 9019 } else if (shadow != (struct vmcs *)-1ull && flags[ACCESS_VMWRITE]) { 9020 report(vmx_inst_error == VMXERR_UNSUPPORTED_VMCS_COMPONENT, 9021 "VMX_INST_ERROR (%d) is as expected (%d)", 9022 vmx_inst_error, VMXERR_UNSUPPORTED_VMCS_COMPONENT); 9023 } 9024 vmcs_shadow_test_access(bitmap, ACCESS_VMREAD); 9025 report(c->reason == VMX_VMREAD, "not shadowed for VMREAD"); 9026 report_prefix_pop(); 9027 9028 /* Permit shadowed VMREAD and VMWRITE. */ 9029 report_prefix_push("VMREAD and VMWRITE permission"); 9030 clear_bit(field, bitmap[ACCESS_VMREAD]); 9031 clear_bit(field, bitmap[ACCESS_VMWRITE]); 9032 if (good_shadow) 9033 vmwrite_to_shadow(field, MAGIC_VAL_1 + field); 9034 vmcs_shadow_test_access(bitmap, ACCESS_VMWRITE); 9035 vmx_inst_error = vmcs_read(VMX_INST_ERROR); 9036 report(c->reason == VMX_VMCALL, 9037 "shadowed for VMWRITE (in %ld cycles)", 9038 c->time); 9039 report(c->flags == flags[ACCESS_VMREAD], 9040 "ALU flags after VMWRITE (%lx) are as expected (%lx)", 9041 c->flags, flags[ACCESS_VMREAD]); 9042 if (good_shadow) { 9043 value = vmread_from_shadow(field); 9044 report(value == 0, 9045 "shadow VMCS value (%lx) is as expected (%lx)", value, 9046 0ul); 9047 } else if (shadow != (struct vmcs *)-1ull && flags[ACCESS_VMWRITE]) { 9048 report(vmx_inst_error == VMXERR_UNSUPPORTED_VMCS_COMPONENT, 9049 "VMX_INST_ERROR (%d) is as expected (%d)", 9050 vmx_inst_error, VMXERR_UNSUPPORTED_VMCS_COMPONENT); 9051 } 9052 vmcs_shadow_test_access(bitmap, ACCESS_VMREAD); 9053 vmx_inst_error = vmcs_read(VMX_INST_ERROR); 9054 report(c->reason == VMX_VMCALL, "shadowed for VMREAD (in %ld cycles)", 9055 c->time); 9056 report(c->flags == flags[ACCESS_VMREAD], 9057 "ALU flags after VMREAD (%lx) are as expected (%lx)", 9058 c->flags, flags[ACCESS_VMREAD]); 9059 if (good_shadow) 9060 report(c->value == 0, 9061 "value read from shadow (%lx) is as expected (%lx)", 9062 c->value, 0ul); 9063 else if (shadow != (struct vmcs *)-1ull && flags[ACCESS_VMREAD]) 9064 report(vmx_inst_error == VMXERR_UNSUPPORTED_VMCS_COMPONENT, 9065 "VMX_INST_ERROR (%d) is as expected (%d)", 9066 vmx_inst_error, VMXERR_UNSUPPORTED_VMCS_COMPONENT); 9067 report_prefix_pop(); 9068 9069 out: 9070 report_prefix_pop(); 9071 } 9072 9073 static void vmx_vmcs_shadow_test_body(u8 *bitmap[2]) 9074 { 9075 unsigned base; 9076 unsigned index; 9077 unsigned bit; 9078 unsigned highest_index = rdmsr(MSR_IA32_VMX_VMCS_ENUM); 9079 9080 /* Run test on all possible valid VMCS fields */ 9081 for (base = 0; 9082 base < (1 << VMCS_FIELD_RESERVED_SHIFT); 9083 base += (1 << VMCS_FIELD_TYPE_SHIFT)) 9084 for (index = 0; index <= highest_index; index++) 9085 vmcs_shadow_test_field(bitmap, base + index); 9086 9087 /* 9088 * Run tests on some invalid VMCS fields 9089 * (Have reserved bit set). 9090 */ 9091 for (bit = VMCS_FIELD_RESERVED_SHIFT; bit < VMCS_FIELD_BIT_SIZE; bit++) 9092 vmcs_shadow_test_field(bitmap, (1ull << bit)); 9093 } 9094 9095 static void vmx_vmcs_shadow_test(void) 9096 { 9097 u8 *bitmap[2]; 9098 struct vmcs *shadow; 9099 9100 if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY)) { 9101 printf("\t'Activate secondary controls' not supported.\n"); 9102 return; 9103 } 9104 9105 if (!(ctrl_cpu_rev[1].clr & CPU_SHADOW_VMCS)) { 9106 printf("\t'VMCS shadowing' not supported.\n"); 9107 return; 9108 } 9109 9110 if (!(rdmsr(MSR_IA32_VMX_MISC) & 9111 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) { 9112 printf("\tVMWRITE can't modify VM-exit information fields.\n"); 9113 return; 9114 } 9115 9116 test_set_guest(vmx_vmcs_shadow_test_guest); 9117 9118 bitmap[ACCESS_VMREAD] = alloc_page(); 9119 bitmap[ACCESS_VMWRITE] = alloc_page(); 9120 9121 vmcs_write(VMREAD_BITMAP, virt_to_phys(bitmap[ACCESS_VMREAD])); 9122 vmcs_write(VMWRITE_BITMAP, virt_to_phys(bitmap[ACCESS_VMWRITE])); 9123 9124 shadow = alloc_page(); 9125 shadow->hdr.revision_id = basic.revision; 9126 shadow->hdr.shadow_vmcs = 1; 9127 TEST_ASSERT(!vmcs_clear(shadow)); 9128 9129 vmcs_clear_bits(CPU_EXEC_CTRL0, CPU_RDTSC); 9130 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_SECONDARY); 9131 vmcs_set_bits(CPU_EXEC_CTRL1, CPU_SHADOW_VMCS); 9132 9133 vmcs_write(VMCS_LINK_PTR, virt_to_phys(shadow)); 9134 report_prefix_push("valid link pointer"); 9135 vmx_vmcs_shadow_test_body(bitmap); 9136 report_prefix_pop(); 9137 9138 vmcs_write(VMCS_LINK_PTR, -1ull); 9139 report_prefix_push("invalid link pointer"); 9140 vmx_vmcs_shadow_test_body(bitmap); 9141 report_prefix_pop(); 9142 9143 l1_l2_common.op = ACCESS_NONE; 9144 enter_guest(); 9145 } 9146 9147 /* 9148 * This test monitors the difference between a guest RDTSC instruction 9149 * and the IA32_TIME_STAMP_COUNTER MSR value stored in the VMCS12 9150 * VM-exit MSR-store list when taking a VM-exit on the instruction 9151 * following RDTSC. 9152 */ 9153 #define RDTSC_DIFF_ITERS 100000 9154 #define RDTSC_DIFF_FAILS 100 9155 #define HOST_CAPTURED_GUEST_TSC_DIFF_THRESHOLD 750 9156 9157 /* 9158 * Set 'use TSC offsetting' and set the guest offset to the 9159 * inverse of the host's current TSC value, so that the guest starts running 9160 * with an effective TSC value of 0. 9161 */ 9162 static void reset_guest_tsc_to_zero(void) 9163 { 9164 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_USE_TSC_OFFSET); 9165 vmcs_write(TSC_OFFSET, -rdtsc()); 9166 } 9167 9168 static void rdtsc_vmexit_diff_test_guest(void) 9169 { 9170 int i; 9171 9172 for (i = 0; i < RDTSC_DIFF_ITERS; i++) 9173 /* Ensure rdtsc is the last instruction before the vmcall. */ 9174 asm volatile("rdtsc; vmcall" : : : "eax", "edx"); 9175 } 9176 9177 /* 9178 * This function only considers the "use TSC offsetting" VM-execution 9179 * control. It does not handle "use TSC scaling" (because the latter 9180 * isn't available to the host today.) 9181 */ 9182 static unsigned long long host_time_to_guest_time(unsigned long long t) 9183 { 9184 TEST_ASSERT(!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) || 9185 !(vmcs_read(CPU_EXEC_CTRL1) & CPU_USE_TSC_SCALING)); 9186 9187 if (vmcs_read(CPU_EXEC_CTRL0) & CPU_USE_TSC_OFFSET) 9188 t += vmcs_read(TSC_OFFSET); 9189 9190 return t; 9191 } 9192 9193 static unsigned long long rdtsc_vmexit_diff_test_iteration(void) 9194 { 9195 unsigned long long guest_tsc, host_to_guest_tsc; 9196 9197 enter_guest(); 9198 skip_exit_vmcall(); 9199 guest_tsc = (u32) regs.rax + (regs.rdx << 32); 9200 host_to_guest_tsc = host_time_to_guest_time(exit_msr_store[0].value); 9201 9202 return host_to_guest_tsc - guest_tsc; 9203 } 9204 9205 static void rdtsc_vmexit_diff_test(void) 9206 { 9207 int fail = 0; 9208 int i; 9209 9210 if (!(ctrl_cpu_rev[0].clr & CPU_USE_TSC_OFFSET)) 9211 test_skip("CPU doesn't support the 'use TSC offsetting' processor-based VM-execution control.\n"); 9212 9213 test_set_guest(rdtsc_vmexit_diff_test_guest); 9214 9215 reset_guest_tsc_to_zero(); 9216 9217 /* 9218 * Set up the VMCS12 VM-exit MSR-store list to store just one 9219 * MSR: IA32_TIME_STAMP_COUNTER. Note that the value stored is 9220 * in the host time domain (i.e., it is not adjusted according 9221 * to the TSC multiplier and TSC offset fields in the VMCS12, 9222 * as a guest RDTSC would be.) 9223 */ 9224 exit_msr_store = alloc_page(); 9225 exit_msr_store[0].index = MSR_IA32_TSC; 9226 vmcs_write(EXI_MSR_ST_CNT, 1); 9227 vmcs_write(EXIT_MSR_ST_ADDR, virt_to_phys(exit_msr_store)); 9228 9229 for (i = 0; i < RDTSC_DIFF_ITERS; i++) { 9230 if (rdtsc_vmexit_diff_test_iteration() >= 9231 HOST_CAPTURED_GUEST_TSC_DIFF_THRESHOLD) 9232 fail++; 9233 } 9234 9235 enter_guest(); 9236 9237 report(fail < RDTSC_DIFF_FAILS, 9238 "RDTSC to VM-exit delta too high in %d of %d iterations", 9239 fail, RDTSC_DIFF_ITERS); 9240 } 9241 9242 static int invalid_msr_init(struct vmcs *vmcs) 9243 { 9244 if (!(ctrl_pin_rev.clr & PIN_PREEMPT)) { 9245 printf("\tPreemption timer is not supported\n"); 9246 return VMX_TEST_EXIT; 9247 } 9248 vmcs_write(PIN_CONTROLS, vmcs_read(PIN_CONTROLS) | PIN_PREEMPT); 9249 preempt_val = 10000000; 9250 vmcs_write(PREEMPT_TIMER_VALUE, preempt_val); 9251 preempt_scale = rdmsr(MSR_IA32_VMX_MISC) & 0x1F; 9252 9253 if (!(ctrl_exit_rev.clr & EXI_SAVE_PREEMPT)) 9254 printf("\tSave preemption value is not supported\n"); 9255 9256 vmcs_write(ENT_MSR_LD_CNT, 1); 9257 vmcs_write(ENTER_MSR_LD_ADDR, (u64)0x13370000); 9258 9259 return VMX_TEST_START; 9260 } 9261 9262 9263 static void invalid_msr_main(void) 9264 { 9265 report(0, "Invalid MSR load"); 9266 } 9267 9268 static int invalid_msr_exit_handler(void) 9269 { 9270 report(0, "Invalid MSR load"); 9271 print_vmexit_info(); 9272 return VMX_TEST_EXIT; 9273 } 9274 9275 static int invalid_msr_entry_failure(struct vmentry_failure *failure) 9276 { 9277 ulong reason; 9278 9279 reason = vmcs_read(EXI_REASON); 9280 report(reason == (0x80000000u | VMX_FAIL_MSR), "Invalid MSR load"); 9281 return VMX_TEST_VMEXIT; 9282 } 9283 9284 /* 9285 * The max number of MSRs in an atomic switch MSR list is: 9286 * (111B + 1) * 512 = 4096 9287 * 9288 * Each list entry consumes: 9289 * 4-byte MSR index + 4 bytes reserved + 8-byte data = 16 bytes 9290 * 9291 * Allocate 128 kB to cover max_msr_list_size (i.e., 64 kB) and then some. 9292 */ 9293 static const u32 msr_list_page_order = 5; 9294 9295 static void atomic_switch_msr_limit_test_guest(void) 9296 { 9297 vmcall(); 9298 } 9299 9300 static void populate_msr_list(struct vmx_msr_entry *msr_list, 9301 size_t byte_capacity, int count) 9302 { 9303 int i; 9304 9305 for (i = 0; i < count; i++) { 9306 msr_list[i].index = MSR_IA32_TSC; 9307 msr_list[i].reserved = 0; 9308 msr_list[i].value = 0x1234567890abcdef; 9309 } 9310 9311 memset(msr_list + count, 0xff, 9312 byte_capacity - count * sizeof(*msr_list)); 9313 } 9314 9315 static int max_msr_list_size(void) 9316 { 9317 u32 vmx_misc = rdmsr(MSR_IA32_VMX_MISC); 9318 u32 factor = ((vmx_misc & GENMASK(27, 25)) >> 25) + 1; 9319 9320 return factor * 512; 9321 } 9322 9323 static void atomic_switch_msrs_test(int count) 9324 { 9325 struct vmx_msr_entry *vm_enter_load; 9326 struct vmx_msr_entry *vm_exit_load; 9327 struct vmx_msr_entry *vm_exit_store; 9328 int max_allowed = max_msr_list_size(); 9329 int byte_capacity = 1ul << (msr_list_page_order + PAGE_SHIFT); 9330 /* Exceeding the max MSR list size at exit trigers KVM to abort. */ 9331 int exit_count = count > max_allowed ? max_allowed : count; 9332 int cleanup_count = count > max_allowed ? 2 : 1; 9333 int i; 9334 9335 /* 9336 * Check for the IA32_TSC MSR, 9337 * available with the "TSC flag" and used to populate the MSR lists. 9338 */ 9339 if (!(cpuid(1).d & (1 << 4))) { 9340 report_skip(__func__); 9341 return; 9342 } 9343 9344 /* Set L2 guest. */ 9345 test_set_guest(atomic_switch_msr_limit_test_guest); 9346 9347 /* Setup atomic MSR switch lists. */ 9348 vm_enter_load = alloc_pages(msr_list_page_order); 9349 vm_exit_load = alloc_pages(msr_list_page_order); 9350 vm_exit_store = alloc_pages(msr_list_page_order); 9351 9352 vmcs_write(ENTER_MSR_LD_ADDR, (u64)vm_enter_load); 9353 vmcs_write(EXIT_MSR_LD_ADDR, (u64)vm_exit_load); 9354 vmcs_write(EXIT_MSR_ST_ADDR, (u64)vm_exit_store); 9355 9356 /* 9357 * VM-Enter should succeed up to the max number of MSRs per list, and 9358 * should not consume junk beyond the last entry. 9359 */ 9360 populate_msr_list(vm_enter_load, byte_capacity, count); 9361 populate_msr_list(vm_exit_load, byte_capacity, exit_count); 9362 populate_msr_list(vm_exit_store, byte_capacity, exit_count); 9363 9364 vmcs_write(ENT_MSR_LD_CNT, count); 9365 vmcs_write(EXI_MSR_LD_CNT, exit_count); 9366 vmcs_write(EXI_MSR_ST_CNT, exit_count); 9367 9368 if (count <= max_allowed) { 9369 enter_guest(); 9370 assert_exit_reason(VMX_VMCALL); 9371 skip_exit_vmcall(); 9372 } else { 9373 u32 exit_reason; 9374 u32 exit_reason_want; 9375 u32 exit_qual; 9376 9377 enter_guest_with_invalid_guest_state(); 9378 9379 exit_reason = vmcs_read(EXI_REASON); 9380 exit_reason_want = VMX_FAIL_MSR | VMX_ENTRY_FAILURE; 9381 report(exit_reason == exit_reason_want, 9382 "exit_reason, %u, is %u.", exit_reason, 9383 exit_reason_want); 9384 9385 exit_qual = vmcs_read(EXI_QUALIFICATION); 9386 report(exit_qual == max_allowed + 1, "exit_qual, %u, is %u.", 9387 exit_qual, max_allowed + 1); 9388 } 9389 9390 /* Cleanup. */ 9391 vmcs_write(ENT_MSR_LD_CNT, 0); 9392 vmcs_write(EXI_MSR_LD_CNT, 0); 9393 vmcs_write(EXI_MSR_ST_CNT, 0); 9394 for (i = 0; i < cleanup_count; i++) { 9395 enter_guest(); 9396 skip_exit_vmcall(); 9397 } 9398 free_pages_by_order(vm_enter_load, msr_list_page_order); 9399 free_pages_by_order(vm_exit_load, msr_list_page_order); 9400 free_pages_by_order(vm_exit_store, msr_list_page_order); 9401 } 9402 9403 static void atomic_switch_max_msrs_test(void) 9404 { 9405 atomic_switch_msrs_test(max_msr_list_size()); 9406 } 9407 9408 static void atomic_switch_overflow_msrs_test(void) 9409 { 9410 atomic_switch_msrs_test(max_msr_list_size() + 1); 9411 } 9412 9413 #define TEST(name) { #name, .v2 = name } 9414 9415 /* name/init/guest_main/exit_handler/syscall_handler/guest_regs */ 9416 struct vmx_test vmx_tests[] = { 9417 { "null", NULL, basic_guest_main, basic_exit_handler, NULL, {0} }, 9418 { "vmenter", NULL, vmenter_main, vmenter_exit_handler, NULL, {0} }, 9419 { "preemption timer", preemption_timer_init, preemption_timer_main, 9420 preemption_timer_exit_handler, NULL, {0} }, 9421 { "control field PAT", test_ctrl_pat_init, test_ctrl_pat_main, 9422 test_ctrl_pat_exit_handler, NULL, {0} }, 9423 { "control field EFER", test_ctrl_efer_init, test_ctrl_efer_main, 9424 test_ctrl_efer_exit_handler, NULL, {0} }, 9425 { "CR shadowing", NULL, cr_shadowing_main, 9426 cr_shadowing_exit_handler, NULL, {0} }, 9427 { "I/O bitmap", iobmp_init, iobmp_main, iobmp_exit_handler, 9428 NULL, {0} }, 9429 { "instruction intercept", insn_intercept_init, insn_intercept_main, 9430 insn_intercept_exit_handler, NULL, {0} }, 9431 { "EPT A/D disabled", ept_init, ept_main, ept_exit_handler, NULL, {0} }, 9432 { "EPT A/D enabled", eptad_init, eptad_main, eptad_exit_handler, NULL, {0} }, 9433 { "PML", pml_init, pml_main, pml_exit_handler, NULL, {0} }, 9434 { "VPID", vpid_init, vpid_main, vpid_exit_handler, NULL, {0} }, 9435 { "interrupt", interrupt_init, interrupt_main, 9436 interrupt_exit_handler, NULL, {0} }, 9437 { "debug controls", dbgctls_init, dbgctls_main, dbgctls_exit_handler, 9438 NULL, {0} }, 9439 { "MSR switch", msr_switch_init, msr_switch_main, 9440 msr_switch_exit_handler, NULL, {0}, msr_switch_entry_failure }, 9441 { "vmmcall", vmmcall_init, vmmcall_main, vmmcall_exit_handler, NULL, {0} }, 9442 { "disable RDTSCP", disable_rdtscp_init, disable_rdtscp_main, 9443 disable_rdtscp_exit_handler, NULL, {0} }, 9444 { "int3", int3_init, int3_guest_main, int3_exit_handler, NULL, {0} }, 9445 { "into", into_init, into_guest_main, into_exit_handler, NULL, {0} }, 9446 { "exit_monitor_from_l2_test", NULL, exit_monitor_from_l2_main, 9447 exit_monitor_from_l2_handler, NULL, {0} }, 9448 { "invalid_msr", invalid_msr_init, invalid_msr_main, 9449 invalid_msr_exit_handler, NULL, {0}, invalid_msr_entry_failure}, 9450 /* Basic V2 tests. */ 9451 TEST(v2_null_test), 9452 TEST(v2_multiple_entries_test), 9453 TEST(fixture_test_case1), 9454 TEST(fixture_test_case2), 9455 /* Opcode tests. */ 9456 TEST(invvpid_test_v2), 9457 /* VM-entry tests */ 9458 TEST(vmx_controls_test), 9459 TEST(vmx_host_state_area_test), 9460 TEST(vmx_guest_state_area_test), 9461 TEST(vmentry_movss_shadow_test), 9462 /* APICv tests */ 9463 TEST(vmx_eoi_bitmap_ioapic_scan_test), 9464 TEST(vmx_hlt_with_rvi_test), 9465 TEST(apic_reg_virt_test), 9466 TEST(virt_x2apic_mode_test), 9467 /* APIC pass-through tests */ 9468 TEST(vmx_apic_passthrough_test), 9469 TEST(vmx_apic_passthrough_thread_test), 9470 TEST(vmx_apic_passthrough_tpr_threshold_test), 9471 TEST(vmx_init_signal_test), 9472 /* VMCS Shadowing tests */ 9473 TEST(vmx_vmcs_shadow_test), 9474 /* Regression tests */ 9475 TEST(vmx_cr_load_test), 9476 TEST(vmx_nm_test), 9477 TEST(vmx_db_test), 9478 TEST(vmx_nmi_window_test), 9479 TEST(vmx_intr_window_test), 9480 TEST(vmx_pending_event_test), 9481 TEST(vmx_pending_event_hlt_test), 9482 TEST(vmx_store_tsc_test), 9483 /* EPT access tests. */ 9484 TEST(ept_access_test_not_present), 9485 TEST(ept_access_test_read_only), 9486 TEST(ept_access_test_write_only), 9487 TEST(ept_access_test_read_write), 9488 TEST(ept_access_test_execute_only), 9489 TEST(ept_access_test_read_execute), 9490 TEST(ept_access_test_write_execute), 9491 TEST(ept_access_test_read_write_execute), 9492 TEST(ept_access_test_reserved_bits), 9493 TEST(ept_access_test_ignored_bits), 9494 TEST(ept_access_test_paddr_not_present_ad_disabled), 9495 TEST(ept_access_test_paddr_not_present_ad_enabled), 9496 TEST(ept_access_test_paddr_read_only_ad_disabled), 9497 TEST(ept_access_test_paddr_read_only_ad_enabled), 9498 TEST(ept_access_test_paddr_read_write), 9499 TEST(ept_access_test_paddr_read_write_execute), 9500 TEST(ept_access_test_paddr_read_execute_ad_disabled), 9501 TEST(ept_access_test_paddr_read_execute_ad_enabled), 9502 TEST(ept_access_test_paddr_not_present_page_fault), 9503 TEST(ept_access_test_force_2m_page), 9504 /* Atomic MSR switch tests. */ 9505 TEST(atomic_switch_max_msrs_test), 9506 TEST(atomic_switch_overflow_msrs_test), 9507 TEST(rdtsc_vmexit_diff_test), 9508 { NULL, NULL, NULL, NULL, NULL, {0} }, 9509 }; 9510