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