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 memset(msr_bitmap, 0x0, PAGE_SIZE); 257 ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0); 258 ctrl_cpu0 |= CPU_MSR_BITMAP; 259 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0); 260 vmcs_write(MSR_BITMAP, (u64)msr_bitmap); 261 } 262 263 static void *get_msr_bitmap(void) 264 { 265 void *msr_bitmap; 266 267 if (vmcs_read(CPU_EXEC_CTRL0) & CPU_MSR_BITMAP) { 268 msr_bitmap = (void *)vmcs_read(MSR_BITMAP); 269 } else { 270 msr_bitmap = alloc_page(); 271 memset(msr_bitmap, 0xff, PAGE_SIZE); 272 vmcs_write(MSR_BITMAP, (u64)msr_bitmap); 273 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_MSR_BITMAP); 274 } 275 276 return msr_bitmap; 277 } 278 279 static void disable_intercept_for_x2apic_msrs(void) 280 { 281 unsigned long *msr_bitmap = (unsigned long *)get_msr_bitmap(); 282 u32 msr; 283 284 for (msr = APIC_BASE_MSR; 285 msr < (APIC_BASE_MSR+0xff); 286 msr += BITS_PER_LONG) { 287 unsigned int word = msr / BITS_PER_LONG; 288 289 msr_bitmap[word] = 0; 290 msr_bitmap[word + (0x800 / sizeof(long))] = 0; 291 } 292 } 293 294 static int test_ctrl_pat_init(struct vmcs *vmcs) 295 { 296 u64 ctrl_ent; 297 u64 ctrl_exi; 298 299 msr_bmp_init(); 300 if (!(ctrl_exit_rev.clr & EXI_SAVE_PAT) && 301 !(ctrl_exit_rev.clr & EXI_LOAD_PAT) && 302 !(ctrl_enter_rev.clr & ENT_LOAD_PAT)) { 303 printf("\tSave/load PAT is not supported\n"); 304 return 1; 305 } 306 307 ctrl_ent = vmcs_read(ENT_CONTROLS); 308 ctrl_exi = vmcs_read(EXI_CONTROLS); 309 ctrl_ent |= ctrl_enter_rev.clr & ENT_LOAD_PAT; 310 ctrl_exi |= ctrl_exit_rev.clr & (EXI_SAVE_PAT | EXI_LOAD_PAT); 311 vmcs_write(ENT_CONTROLS, ctrl_ent); 312 vmcs_write(EXI_CONTROLS, ctrl_exi); 313 ia32_pat = rdmsr(MSR_IA32_CR_PAT); 314 vmcs_write(GUEST_PAT, 0x0); 315 vmcs_write(HOST_PAT, ia32_pat); 316 return VMX_TEST_START; 317 } 318 319 static void test_ctrl_pat_main(void) 320 { 321 u64 guest_ia32_pat; 322 323 guest_ia32_pat = rdmsr(MSR_IA32_CR_PAT); 324 if (!(ctrl_enter_rev.clr & ENT_LOAD_PAT)) 325 printf("\tENT_LOAD_PAT is not supported.\n"); 326 else { 327 if (guest_ia32_pat != 0) { 328 report("Entry load PAT", 0); 329 return; 330 } 331 } 332 wrmsr(MSR_IA32_CR_PAT, 0x6); 333 vmcall(); 334 guest_ia32_pat = rdmsr(MSR_IA32_CR_PAT); 335 if (ctrl_enter_rev.clr & ENT_LOAD_PAT) 336 report("Entry load PAT", guest_ia32_pat == ia32_pat); 337 } 338 339 static int test_ctrl_pat_exit_handler(void) 340 { 341 u64 guest_rip; 342 ulong reason; 343 u64 guest_pat; 344 345 guest_rip = vmcs_read(GUEST_RIP); 346 reason = vmcs_read(EXI_REASON) & 0xff; 347 switch (reason) { 348 case VMX_VMCALL: 349 guest_pat = vmcs_read(GUEST_PAT); 350 if (!(ctrl_exit_rev.clr & EXI_SAVE_PAT)) { 351 printf("\tEXI_SAVE_PAT is not supported\n"); 352 vmcs_write(GUEST_PAT, 0x6); 353 } else { 354 report("Exit save PAT", guest_pat == 0x6); 355 } 356 if (!(ctrl_exit_rev.clr & EXI_LOAD_PAT)) 357 printf("\tEXI_LOAD_PAT is not supported\n"); 358 else 359 report("Exit load PAT", rdmsr(MSR_IA32_CR_PAT) == ia32_pat); 360 vmcs_write(GUEST_PAT, ia32_pat); 361 vmcs_write(GUEST_RIP, guest_rip + 3); 362 return VMX_TEST_RESUME; 363 default: 364 printf("ERROR : Undefined exit reason, reason = %ld.\n", reason); 365 break; 366 } 367 return VMX_TEST_VMEXIT; 368 } 369 370 static int test_ctrl_efer_init(struct vmcs *vmcs) 371 { 372 u64 ctrl_ent; 373 u64 ctrl_exi; 374 375 msr_bmp_init(); 376 ctrl_ent = vmcs_read(ENT_CONTROLS) | ENT_LOAD_EFER; 377 ctrl_exi = vmcs_read(EXI_CONTROLS) | EXI_SAVE_EFER | EXI_LOAD_EFER; 378 vmcs_write(ENT_CONTROLS, ctrl_ent & ctrl_enter_rev.clr); 379 vmcs_write(EXI_CONTROLS, ctrl_exi & ctrl_exit_rev.clr); 380 ia32_efer = rdmsr(MSR_EFER); 381 vmcs_write(GUEST_EFER, ia32_efer ^ EFER_NX); 382 vmcs_write(HOST_EFER, ia32_efer ^ EFER_NX); 383 return VMX_TEST_START; 384 } 385 386 static void test_ctrl_efer_main(void) 387 { 388 u64 guest_ia32_efer; 389 390 guest_ia32_efer = rdmsr(MSR_EFER); 391 if (!(ctrl_enter_rev.clr & ENT_LOAD_EFER)) 392 printf("\tENT_LOAD_EFER is not supported.\n"); 393 else { 394 if (guest_ia32_efer != (ia32_efer ^ EFER_NX)) { 395 report("Entry load EFER", 0); 396 return; 397 } 398 } 399 wrmsr(MSR_EFER, ia32_efer); 400 vmcall(); 401 guest_ia32_efer = rdmsr(MSR_EFER); 402 if (ctrl_enter_rev.clr & ENT_LOAD_EFER) 403 report("Entry load EFER", guest_ia32_efer == ia32_efer); 404 } 405 406 static int test_ctrl_efer_exit_handler(void) 407 { 408 u64 guest_rip; 409 ulong reason; 410 u64 guest_efer; 411 412 guest_rip = vmcs_read(GUEST_RIP); 413 reason = vmcs_read(EXI_REASON) & 0xff; 414 switch (reason) { 415 case VMX_VMCALL: 416 guest_efer = vmcs_read(GUEST_EFER); 417 if (!(ctrl_exit_rev.clr & EXI_SAVE_EFER)) { 418 printf("\tEXI_SAVE_EFER is not supported\n"); 419 vmcs_write(GUEST_EFER, ia32_efer); 420 } else { 421 report("Exit save EFER", guest_efer == ia32_efer); 422 } 423 if (!(ctrl_exit_rev.clr & EXI_LOAD_EFER)) { 424 printf("\tEXI_LOAD_EFER is not supported\n"); 425 wrmsr(MSR_EFER, ia32_efer ^ EFER_NX); 426 } else { 427 report("Exit load EFER", rdmsr(MSR_EFER) == (ia32_efer ^ EFER_NX)); 428 } 429 vmcs_write(GUEST_PAT, ia32_efer); 430 vmcs_write(GUEST_RIP, guest_rip + 3); 431 return VMX_TEST_RESUME; 432 default: 433 printf("ERROR : Undefined exit reason, reason = %ld.\n", reason); 434 break; 435 } 436 return VMX_TEST_VMEXIT; 437 } 438 439 u32 guest_cr0, guest_cr4; 440 441 static void cr_shadowing_main(void) 442 { 443 u32 cr0, cr4, tmp; 444 445 // Test read through 446 vmx_set_test_stage(0); 447 guest_cr0 = read_cr0(); 448 if (vmx_get_test_stage() == 1) 449 report("Read through CR0", 0); 450 else 451 vmcall(); 452 vmx_set_test_stage(1); 453 guest_cr4 = read_cr4(); 454 if (vmx_get_test_stage() == 2) 455 report("Read through CR4", 0); 456 else 457 vmcall(); 458 // Test write through 459 guest_cr0 = guest_cr0 ^ (X86_CR0_TS | X86_CR0_MP); 460 guest_cr4 = guest_cr4 ^ (X86_CR4_TSD | X86_CR4_DE); 461 vmx_set_test_stage(2); 462 write_cr0(guest_cr0); 463 if (vmx_get_test_stage() == 3) 464 report("Write throuth CR0", 0); 465 else 466 vmcall(); 467 vmx_set_test_stage(3); 468 write_cr4(guest_cr4); 469 if (vmx_get_test_stage() == 4) 470 report("Write through CR4", 0); 471 else 472 vmcall(); 473 // Test read shadow 474 vmx_set_test_stage(4); 475 vmcall(); 476 cr0 = read_cr0(); 477 if (vmx_get_test_stage() != 5) 478 report("Read shadowing CR0", cr0 == guest_cr0); 479 vmx_set_test_stage(5); 480 cr4 = read_cr4(); 481 if (vmx_get_test_stage() != 6) 482 report("Read shadowing CR4", cr4 == guest_cr4); 483 // Test write shadow (same value with shadow) 484 vmx_set_test_stage(6); 485 write_cr0(guest_cr0); 486 if (vmx_get_test_stage() == 7) 487 report("Write shadowing CR0 (same value with shadow)", 0); 488 else 489 vmcall(); 490 vmx_set_test_stage(7); 491 write_cr4(guest_cr4); 492 if (vmx_get_test_stage() == 8) 493 report("Write shadowing CR4 (same value with shadow)", 0); 494 else 495 vmcall(); 496 // Test write shadow (different value) 497 vmx_set_test_stage(8); 498 tmp = guest_cr0 ^ X86_CR0_TS; 499 asm volatile("mov %0, %%rsi\n\t" 500 "mov %%rsi, %%cr0\n\t" 501 ::"m"(tmp) 502 :"rsi", "memory", "cc"); 503 report("Write shadowing different X86_CR0_TS", vmx_get_test_stage() == 9); 504 vmx_set_test_stage(9); 505 tmp = guest_cr0 ^ X86_CR0_MP; 506 asm volatile("mov %0, %%rsi\n\t" 507 "mov %%rsi, %%cr0\n\t" 508 ::"m"(tmp) 509 :"rsi", "memory", "cc"); 510 report("Write shadowing different X86_CR0_MP", vmx_get_test_stage() == 10); 511 vmx_set_test_stage(10); 512 tmp = guest_cr4 ^ X86_CR4_TSD; 513 asm volatile("mov %0, %%rsi\n\t" 514 "mov %%rsi, %%cr4\n\t" 515 ::"m"(tmp) 516 :"rsi", "memory", "cc"); 517 report("Write shadowing different X86_CR4_TSD", vmx_get_test_stage() == 11); 518 vmx_set_test_stage(11); 519 tmp = guest_cr4 ^ X86_CR4_DE; 520 asm volatile("mov %0, %%rsi\n\t" 521 "mov %%rsi, %%cr4\n\t" 522 ::"m"(tmp) 523 :"rsi", "memory", "cc"); 524 report("Write shadowing different X86_CR4_DE", vmx_get_test_stage() == 12); 525 } 526 527 static int cr_shadowing_exit_handler(void) 528 { 529 u64 guest_rip; 530 ulong reason; 531 u32 insn_len; 532 u32 exit_qual; 533 534 guest_rip = vmcs_read(GUEST_RIP); 535 reason = vmcs_read(EXI_REASON) & 0xff; 536 insn_len = vmcs_read(EXI_INST_LEN); 537 exit_qual = vmcs_read(EXI_QUALIFICATION); 538 switch (reason) { 539 case VMX_VMCALL: 540 switch (vmx_get_test_stage()) { 541 case 0: 542 report("Read through CR0", guest_cr0 == vmcs_read(GUEST_CR0)); 543 break; 544 case 1: 545 report("Read through CR4", guest_cr4 == vmcs_read(GUEST_CR4)); 546 break; 547 case 2: 548 report("Write through CR0", guest_cr0 == vmcs_read(GUEST_CR0)); 549 break; 550 case 3: 551 report("Write through CR4", guest_cr4 == vmcs_read(GUEST_CR4)); 552 break; 553 case 4: 554 guest_cr0 = vmcs_read(GUEST_CR0) ^ (X86_CR0_TS | X86_CR0_MP); 555 guest_cr4 = vmcs_read(GUEST_CR4) ^ (X86_CR4_TSD | X86_CR4_DE); 556 vmcs_write(CR0_MASK, X86_CR0_TS | X86_CR0_MP); 557 vmcs_write(CR0_READ_SHADOW, guest_cr0 & (X86_CR0_TS | X86_CR0_MP)); 558 vmcs_write(CR4_MASK, X86_CR4_TSD | X86_CR4_DE); 559 vmcs_write(CR4_READ_SHADOW, guest_cr4 & (X86_CR4_TSD | X86_CR4_DE)); 560 break; 561 case 6: 562 report("Write shadowing CR0 (same value)", 563 guest_cr0 == (vmcs_read(GUEST_CR0) ^ (X86_CR0_TS | X86_CR0_MP))); 564 break; 565 case 7: 566 report("Write shadowing CR4 (same value)", 567 guest_cr4 == (vmcs_read(GUEST_CR4) ^ (X86_CR4_TSD | X86_CR4_DE))); 568 break; 569 default: 570 // Should not reach here 571 report("unexpected stage, %d", false, 572 vmx_get_test_stage()); 573 print_vmexit_info(); 574 return VMX_TEST_VMEXIT; 575 } 576 vmcs_write(GUEST_RIP, guest_rip + insn_len); 577 return VMX_TEST_RESUME; 578 case VMX_CR: 579 switch (vmx_get_test_stage()) { 580 case 4: 581 report("Read shadowing CR0", 0); 582 vmx_inc_test_stage(); 583 break; 584 case 5: 585 report("Read shadowing CR4", 0); 586 vmx_inc_test_stage(); 587 break; 588 case 6: 589 report("Write shadowing CR0 (same value)", 0); 590 vmx_inc_test_stage(); 591 break; 592 case 7: 593 report("Write shadowing CR4 (same value)", 0); 594 vmx_inc_test_stage(); 595 break; 596 case 8: 597 case 9: 598 // 0x600 encodes "mov %esi, %cr0" 599 if (exit_qual == 0x600) 600 vmx_inc_test_stage(); 601 break; 602 case 10: 603 case 11: 604 // 0x604 encodes "mov %esi, %cr4" 605 if (exit_qual == 0x604) 606 vmx_inc_test_stage(); 607 break; 608 default: 609 // Should not reach here 610 report("unexpected stage, %d", false, 611 vmx_get_test_stage()); 612 print_vmexit_info(); 613 return VMX_TEST_VMEXIT; 614 } 615 vmcs_write(GUEST_RIP, guest_rip + insn_len); 616 return VMX_TEST_RESUME; 617 default: 618 report("Unknown exit reason, %ld", false, reason); 619 print_vmexit_info(); 620 } 621 return VMX_TEST_VMEXIT; 622 } 623 624 static int iobmp_init(struct vmcs *vmcs) 625 { 626 u32 ctrl_cpu0; 627 628 io_bitmap_a = alloc_page(); 629 io_bitmap_b = alloc_page(); 630 memset(io_bitmap_a, 0x0, PAGE_SIZE); 631 memset(io_bitmap_b, 0x0, PAGE_SIZE); 632 ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0); 633 ctrl_cpu0 |= CPU_IO_BITMAP; 634 ctrl_cpu0 &= (~CPU_IO); 635 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0); 636 vmcs_write(IO_BITMAP_A, (u64)io_bitmap_a); 637 vmcs_write(IO_BITMAP_B, (u64)io_bitmap_b); 638 return VMX_TEST_START; 639 } 640 641 static void iobmp_main(void) 642 { 643 // stage 0, test IO pass 644 vmx_set_test_stage(0); 645 inb(0x5000); 646 outb(0x0, 0x5000); 647 report("I/O bitmap - I/O pass", vmx_get_test_stage() == 0); 648 // test IO width, in/out 649 ((u8 *)io_bitmap_a)[0] = 0xFF; 650 vmx_set_test_stage(2); 651 inb(0x0); 652 report("I/O bitmap - trap in", vmx_get_test_stage() == 3); 653 vmx_set_test_stage(3); 654 outw(0x0, 0x0); 655 report("I/O bitmap - trap out", vmx_get_test_stage() == 4); 656 vmx_set_test_stage(4); 657 inl(0x0); 658 report("I/O bitmap - I/O width, long", vmx_get_test_stage() == 5); 659 // test low/high IO port 660 vmx_set_test_stage(5); 661 ((u8 *)io_bitmap_a)[0x5000 / 8] = (1 << (0x5000 % 8)); 662 inb(0x5000); 663 report("I/O bitmap - I/O port, low part", vmx_get_test_stage() == 6); 664 vmx_set_test_stage(6); 665 ((u8 *)io_bitmap_b)[0x1000 / 8] = (1 << (0x1000 % 8)); 666 inb(0x9000); 667 report("I/O bitmap - I/O port, high part", vmx_get_test_stage() == 7); 668 // test partial pass 669 vmx_set_test_stage(7); 670 inl(0x4FFF); 671 report("I/O bitmap - partial pass", vmx_get_test_stage() == 8); 672 // test overrun 673 vmx_set_test_stage(8); 674 memset(io_bitmap_a, 0x0, PAGE_SIZE); 675 memset(io_bitmap_b, 0x0, PAGE_SIZE); 676 inl(0xFFFF); 677 report("I/O bitmap - overrun", vmx_get_test_stage() == 9); 678 vmx_set_test_stage(9); 679 vmcall(); 680 outb(0x0, 0x0); 681 report("I/O bitmap - ignore unconditional exiting", 682 vmx_get_test_stage() == 9); 683 vmx_set_test_stage(10); 684 vmcall(); 685 outb(0x0, 0x0); 686 report("I/O bitmap - unconditional exiting", 687 vmx_get_test_stage() == 11); 688 } 689 690 static int iobmp_exit_handler(void) 691 { 692 u64 guest_rip; 693 ulong reason, exit_qual; 694 u32 insn_len, ctrl_cpu0; 695 696 guest_rip = vmcs_read(GUEST_RIP); 697 reason = vmcs_read(EXI_REASON) & 0xff; 698 exit_qual = vmcs_read(EXI_QUALIFICATION); 699 insn_len = vmcs_read(EXI_INST_LEN); 700 switch (reason) { 701 case VMX_IO: 702 switch (vmx_get_test_stage()) { 703 case 0: 704 case 1: 705 vmx_inc_test_stage(); 706 break; 707 case 2: 708 report("I/O bitmap - I/O width, byte", 709 (exit_qual & VMX_IO_SIZE_MASK) == _VMX_IO_BYTE); 710 report("I/O bitmap - I/O direction, in", exit_qual & VMX_IO_IN); 711 vmx_inc_test_stage(); 712 break; 713 case 3: 714 report("I/O bitmap - I/O width, word", 715 (exit_qual & VMX_IO_SIZE_MASK) == _VMX_IO_WORD); 716 report("I/O bitmap - I/O direction, out", 717 !(exit_qual & VMX_IO_IN)); 718 vmx_inc_test_stage(); 719 break; 720 case 4: 721 report("I/O bitmap - I/O width, long", 722 (exit_qual & VMX_IO_SIZE_MASK) == _VMX_IO_LONG); 723 vmx_inc_test_stage(); 724 break; 725 case 5: 726 if (((exit_qual & VMX_IO_PORT_MASK) >> VMX_IO_PORT_SHIFT) == 0x5000) 727 vmx_inc_test_stage(); 728 break; 729 case 6: 730 if (((exit_qual & VMX_IO_PORT_MASK) >> VMX_IO_PORT_SHIFT) == 0x9000) 731 vmx_inc_test_stage(); 732 break; 733 case 7: 734 if (((exit_qual & VMX_IO_PORT_MASK) >> VMX_IO_PORT_SHIFT) == 0x4FFF) 735 vmx_inc_test_stage(); 736 break; 737 case 8: 738 if (((exit_qual & VMX_IO_PORT_MASK) >> VMX_IO_PORT_SHIFT) == 0xFFFF) 739 vmx_inc_test_stage(); 740 break; 741 case 9: 742 case 10: 743 ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0); 744 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0 & ~CPU_IO); 745 vmx_inc_test_stage(); 746 break; 747 default: 748 // Should not reach here 749 report("unexpected stage, %d", false, 750 vmx_get_test_stage()); 751 print_vmexit_info(); 752 return VMX_TEST_VMEXIT; 753 } 754 vmcs_write(GUEST_RIP, guest_rip + insn_len); 755 return VMX_TEST_RESUME; 756 case VMX_VMCALL: 757 switch (vmx_get_test_stage()) { 758 case 9: 759 ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0); 760 ctrl_cpu0 |= CPU_IO | CPU_IO_BITMAP; 761 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0); 762 break; 763 case 10: 764 ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0); 765 ctrl_cpu0 = (ctrl_cpu0 & ~CPU_IO_BITMAP) | CPU_IO; 766 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0); 767 break; 768 default: 769 // Should not reach here 770 report("unexpected stage, %d", false, 771 vmx_get_test_stage()); 772 print_vmexit_info(); 773 return VMX_TEST_VMEXIT; 774 } 775 vmcs_write(GUEST_RIP, guest_rip + insn_len); 776 return VMX_TEST_RESUME; 777 default: 778 printf("guest_rip = %#lx\n", guest_rip); 779 printf("\tERROR : Undefined exit reason, reason = %ld.\n", reason); 780 break; 781 } 782 return VMX_TEST_VMEXIT; 783 } 784 785 #define INSN_CPU0 0 786 #define INSN_CPU1 1 787 #define INSN_ALWAYS_TRAP 2 788 789 #define FIELD_EXIT_QUAL (1 << 0) 790 #define FIELD_INSN_INFO (1 << 1) 791 792 asm( 793 "insn_hlt: hlt;ret\n\t" 794 "insn_invlpg: invlpg 0x12345678;ret\n\t" 795 "insn_mwait: xor %eax, %eax; xor %ecx, %ecx; mwait;ret\n\t" 796 "insn_rdpmc: xor %ecx, %ecx; rdpmc;ret\n\t" 797 "insn_rdtsc: rdtsc;ret\n\t" 798 "insn_cr3_load: mov cr3,%rax; mov %rax,%cr3;ret\n\t" 799 "insn_cr3_store: mov %cr3,%rax;ret\n\t" 800 #ifdef __x86_64__ 801 "insn_cr8_load: xor %eax, %eax; mov %rax,%cr8;ret\n\t" 802 "insn_cr8_store: mov %cr8,%rax;ret\n\t" 803 #endif 804 "insn_monitor: xor %eax, %eax; xor %ecx, %ecx; xor %edx, %edx; monitor;ret\n\t" 805 "insn_pause: pause;ret\n\t" 806 "insn_wbinvd: wbinvd;ret\n\t" 807 "insn_cpuid: mov $10, %eax; cpuid;ret\n\t" 808 "insn_invd: invd;ret\n\t" 809 "insn_sgdt: sgdt gdt64_desc;ret\n\t" 810 "insn_lgdt: lgdt gdt64_desc;ret\n\t" 811 "insn_sidt: sidt idt_descr;ret\n\t" 812 "insn_lidt: lidt idt_descr;ret\n\t" 813 "insn_sldt: sldt %ax;ret\n\t" 814 "insn_lldt: xor %eax, %eax; lldt %ax;ret\n\t" 815 "insn_str: str %ax;ret\n\t" 816 "insn_rdrand: rdrand %rax;ret\n\t" 817 "insn_rdseed: rdseed %rax;ret\n\t" 818 ); 819 extern void insn_hlt(void); 820 extern void insn_invlpg(void); 821 extern void insn_mwait(void); 822 extern void insn_rdpmc(void); 823 extern void insn_rdtsc(void); 824 extern void insn_cr3_load(void); 825 extern void insn_cr3_store(void); 826 #ifdef __x86_64__ 827 extern void insn_cr8_load(void); 828 extern void insn_cr8_store(void); 829 #endif 830 extern void insn_monitor(void); 831 extern void insn_pause(void); 832 extern void insn_wbinvd(void); 833 extern void insn_sgdt(void); 834 extern void insn_lgdt(void); 835 extern void insn_sidt(void); 836 extern void insn_lidt(void); 837 extern void insn_sldt(void); 838 extern void insn_lldt(void); 839 extern void insn_str(void); 840 extern void insn_cpuid(void); 841 extern void insn_invd(void); 842 extern void insn_rdrand(void); 843 extern void insn_rdseed(void); 844 845 u32 cur_insn; 846 u64 cr3; 847 848 struct insn_table { 849 const char *name; 850 u32 flag; 851 void (*insn_func)(void); 852 u32 type; 853 u32 reason; 854 ulong exit_qual; 855 u32 insn_info; 856 // Use FIELD_EXIT_QUAL and FIELD_INSN_INFO to define 857 // which field need to be tested, reason is always tested 858 u32 test_field; 859 }; 860 861 /* 862 * Add more test cases of instruction intercept here. Elements in this 863 * table is: 864 * name/control flag/insn function/type/exit reason/exit qulification/ 865 * instruction info/field to test 866 * The last field defines which fields (exit_qual and insn_info) need to be 867 * tested in exit handler. If set to 0, only "reason" is checked. 868 */ 869 static struct insn_table insn_table[] = { 870 // Flags for Primary Processor-Based VM-Execution Controls 871 {"HLT", CPU_HLT, insn_hlt, INSN_CPU0, 12, 0, 0, 0}, 872 {"INVLPG", CPU_INVLPG, insn_invlpg, INSN_CPU0, 14, 873 0x12345678, 0, FIELD_EXIT_QUAL}, 874 {"MWAIT", CPU_MWAIT, insn_mwait, INSN_CPU0, 36, 0, 0, 0}, 875 {"RDPMC", CPU_RDPMC, insn_rdpmc, INSN_CPU0, 15, 0, 0, 0}, 876 {"RDTSC", CPU_RDTSC, insn_rdtsc, INSN_CPU0, 16, 0, 0, 0}, 877 {"CR3 load", CPU_CR3_LOAD, insn_cr3_load, INSN_CPU0, 28, 0x3, 0, 878 FIELD_EXIT_QUAL}, 879 {"CR3 store", CPU_CR3_STORE, insn_cr3_store, INSN_CPU0, 28, 0x13, 0, 880 FIELD_EXIT_QUAL}, 881 #ifdef __x86_64__ 882 {"CR8 load", CPU_CR8_LOAD, insn_cr8_load, INSN_CPU0, 28, 0x8, 0, 883 FIELD_EXIT_QUAL}, 884 {"CR8 store", CPU_CR8_STORE, insn_cr8_store, INSN_CPU0, 28, 0x18, 0, 885 FIELD_EXIT_QUAL}, 886 #endif 887 {"MONITOR", CPU_MONITOR, insn_monitor, INSN_CPU0, 39, 0, 0, 0}, 888 {"PAUSE", CPU_PAUSE, insn_pause, INSN_CPU0, 40, 0, 0, 0}, 889 // Flags for Secondary Processor-Based VM-Execution Controls 890 {"WBINVD", CPU_WBINVD, insn_wbinvd, INSN_CPU1, 54, 0, 0, 0}, 891 {"DESC_TABLE (SGDT)", CPU_DESC_TABLE, insn_sgdt, INSN_CPU1, 46, 0, 0, 0}, 892 {"DESC_TABLE (LGDT)", CPU_DESC_TABLE, insn_lgdt, INSN_CPU1, 46, 0, 0, 0}, 893 {"DESC_TABLE (SIDT)", CPU_DESC_TABLE, insn_sidt, INSN_CPU1, 46, 0, 0, 0}, 894 {"DESC_TABLE (LIDT)", CPU_DESC_TABLE, insn_lidt, INSN_CPU1, 46, 0, 0, 0}, 895 {"DESC_TABLE (SLDT)", CPU_DESC_TABLE, insn_sldt, INSN_CPU1, 47, 0, 0, 0}, 896 {"DESC_TABLE (LLDT)", CPU_DESC_TABLE, insn_lldt, INSN_CPU1, 47, 0, 0, 0}, 897 {"DESC_TABLE (STR)", CPU_DESC_TABLE, insn_str, INSN_CPU1, 47, 0, 0, 0}, 898 /* LTR causes a #GP if done with a busy selector, so it is not tested. */ 899 {"RDRAND", CPU_RDRAND, insn_rdrand, INSN_CPU1, VMX_RDRAND, 0, 0, 0}, 900 {"RDSEED", CPU_RDSEED, insn_rdseed, INSN_CPU1, VMX_RDSEED, 0, 0, 0}, 901 // Instructions always trap 902 {"CPUID", 0, insn_cpuid, INSN_ALWAYS_TRAP, 10, 0, 0, 0}, 903 {"INVD", 0, insn_invd, INSN_ALWAYS_TRAP, 13, 0, 0, 0}, 904 // Instructions never trap 905 {NULL}, 906 }; 907 908 static int insn_intercept_init(struct vmcs *vmcs) 909 { 910 u32 ctrl_cpu; 911 912 ctrl_cpu = ctrl_cpu_rev[0].set | CPU_SECONDARY; 913 ctrl_cpu &= ctrl_cpu_rev[0].clr; 914 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu); 915 vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu_rev[1].set); 916 cr3 = read_cr3(); 917 return VMX_TEST_START; 918 } 919 920 static void insn_intercept_main(void) 921 { 922 for (cur_insn = 0; insn_table[cur_insn].name != NULL; cur_insn++) { 923 vmx_set_test_stage(cur_insn * 2); 924 if ((insn_table[cur_insn].type == INSN_CPU0 && 925 !(ctrl_cpu_rev[0].clr & insn_table[cur_insn].flag)) || 926 (insn_table[cur_insn].type == INSN_CPU1 && 927 !(ctrl_cpu_rev[1].clr & insn_table[cur_insn].flag))) { 928 printf("\tCPU_CTRL%d.CPU_%s is not supported.\n", 929 insn_table[cur_insn].type - INSN_CPU0, 930 insn_table[cur_insn].name); 931 continue; 932 } 933 934 if ((insn_table[cur_insn].type == INSN_CPU0 && 935 !(ctrl_cpu_rev[0].set & insn_table[cur_insn].flag)) || 936 (insn_table[cur_insn].type == INSN_CPU1 && 937 !(ctrl_cpu_rev[1].set & insn_table[cur_insn].flag))) { 938 /* skip hlt, it stalls the guest and is tested below */ 939 if (insn_table[cur_insn].insn_func != insn_hlt) 940 insn_table[cur_insn].insn_func(); 941 report("execute %s", vmx_get_test_stage() == cur_insn * 2, 942 insn_table[cur_insn].name); 943 } else if (insn_table[cur_insn].type != INSN_ALWAYS_TRAP) 944 printf("\tCPU_CTRL%d.CPU_%s always traps.\n", 945 insn_table[cur_insn].type - INSN_CPU0, 946 insn_table[cur_insn].name); 947 948 vmcall(); 949 950 insn_table[cur_insn].insn_func(); 951 report("intercept %s", vmx_get_test_stage() == cur_insn * 2 + 1, 952 insn_table[cur_insn].name); 953 954 vmx_set_test_stage(cur_insn * 2 + 1); 955 vmcall(); 956 } 957 } 958 959 static int insn_intercept_exit_handler(void) 960 { 961 u64 guest_rip; 962 u32 reason; 963 ulong exit_qual; 964 u32 insn_len; 965 u32 insn_info; 966 bool pass; 967 968 guest_rip = vmcs_read(GUEST_RIP); 969 reason = vmcs_read(EXI_REASON) & 0xff; 970 exit_qual = vmcs_read(EXI_QUALIFICATION); 971 insn_len = vmcs_read(EXI_INST_LEN); 972 insn_info = vmcs_read(EXI_INST_INFO); 973 974 if (reason == VMX_VMCALL) { 975 u32 val = 0; 976 977 if (insn_table[cur_insn].type == INSN_CPU0) 978 val = vmcs_read(CPU_EXEC_CTRL0); 979 else if (insn_table[cur_insn].type == INSN_CPU1) 980 val = vmcs_read(CPU_EXEC_CTRL1); 981 982 if (vmx_get_test_stage() & 1) 983 val &= ~insn_table[cur_insn].flag; 984 else 985 val |= insn_table[cur_insn].flag; 986 987 if (insn_table[cur_insn].type == INSN_CPU0) 988 vmcs_write(CPU_EXEC_CTRL0, val | ctrl_cpu_rev[0].set); 989 else if (insn_table[cur_insn].type == INSN_CPU1) 990 vmcs_write(CPU_EXEC_CTRL1, val | ctrl_cpu_rev[1].set); 991 } else { 992 pass = (cur_insn * 2 == vmx_get_test_stage()) && 993 insn_table[cur_insn].reason == reason; 994 if (insn_table[cur_insn].test_field & FIELD_EXIT_QUAL && 995 insn_table[cur_insn].exit_qual != exit_qual) 996 pass = false; 997 if (insn_table[cur_insn].test_field & FIELD_INSN_INFO && 998 insn_table[cur_insn].insn_info != insn_info) 999 pass = false; 1000 if (pass) 1001 vmx_inc_test_stage(); 1002 } 1003 vmcs_write(GUEST_RIP, guest_rip + insn_len); 1004 return VMX_TEST_RESUME; 1005 } 1006 1007 /** 1008 * __setup_ept - Setup the VMCS fields to enable Extended Page Tables (EPT) 1009 * @hpa: Host physical address of the top-level, a.k.a. root, EPT table 1010 * @enable_ad: Whether or not to enable Access/Dirty bits for EPT entries 1011 * 1012 * Returns 0 on success, 1 on failure. 1013 * 1014 * Note that @hpa doesn't need to point at actual memory if VM-Launch is 1015 * expected to fail, e.g. setup_dummy_ept() arbitrarily passes '0' to satisfy 1016 * the various EPTP consistency checks, but doesn't ensure backing for HPA '0'. 1017 */ 1018 static int __setup_ept(u64 hpa, bool enable_ad) 1019 { 1020 if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) || 1021 !(ctrl_cpu_rev[1].clr & CPU_EPT)) { 1022 printf("\tEPT is not supported"); 1023 return 1; 1024 } 1025 if (!(ept_vpid.val & EPT_CAP_WB)) { 1026 printf("WB memtype for EPT walks not supported\n"); 1027 return 1; 1028 } 1029 if (!(ept_vpid.val & EPT_CAP_PWL4)) { 1030 printf("\tPWL4 is not supported\n"); 1031 return 1; 1032 } 1033 1034 eptp = EPT_MEM_TYPE_WB; 1035 eptp |= (3 << EPTP_PG_WALK_LEN_SHIFT); 1036 eptp |= hpa; 1037 if (enable_ad) 1038 eptp |= EPTP_AD_FLAG; 1039 1040 vmcs_write(EPTP, eptp); 1041 vmcs_write(CPU_EXEC_CTRL0, vmcs_read(CPU_EXEC_CTRL0)| CPU_SECONDARY); 1042 vmcs_write(CPU_EXEC_CTRL1, vmcs_read(CPU_EXEC_CTRL1)| CPU_EPT); 1043 1044 return 0; 1045 } 1046 1047 /** 1048 * setup_ept - Enable Extended Page Tables (EPT) and setup an identity map 1049 * @enable_ad: Whether or not to enable Access/Dirty bits for EPT entries 1050 * 1051 * Returns 0 on success, 1 on failure. 1052 * 1053 * This is the "real" function for setting up EPT tables, i.e. use this for 1054 * tests that need to run code in the guest with EPT enabled. 1055 */ 1056 static int setup_ept(bool enable_ad) 1057 { 1058 unsigned long end_of_memory; 1059 1060 pml4 = alloc_page(); 1061 1062 if (__setup_ept(virt_to_phys(pml4), enable_ad)) 1063 return 1; 1064 1065 memset(pml4, 0, PAGE_SIZE); 1066 1067 end_of_memory = fwcfg_get_u64(FW_CFG_RAM_SIZE); 1068 if (end_of_memory < (1ul << 32)) 1069 end_of_memory = (1ul << 32); 1070 /* Cannot use large EPT pages if we need to track EPT 1071 * accessed/dirty bits at 4K granularity. 1072 */ 1073 setup_ept_range(pml4, 0, end_of_memory, 0, 1074 !enable_ad && ept_2m_supported(), 1075 EPT_WA | EPT_RA | EPT_EA); 1076 return 0; 1077 } 1078 1079 /** 1080 * setup_dummy_ept - Enable Extended Page Tables (EPT) with a dummy root HPA 1081 * 1082 * Setup EPT using a semi-arbitrary dummy root HPA. This function is intended 1083 * for use by tests that need EPT enabled to verify dependent VMCS controls 1084 * but never expect to fully enter the guest, i.e. don't need setup the actual 1085 * EPT tables. 1086 */ 1087 static void setup_dummy_ept(void) 1088 { 1089 if (__setup_ept(0, false)) 1090 report_abort("EPT setup unexpectedly failed"); 1091 } 1092 1093 static int enable_unrestricted_guest(void) 1094 { 1095 if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) || 1096 !(ctrl_cpu_rev[1].clr & CPU_URG) || 1097 !(ctrl_cpu_rev[1].clr & CPU_EPT)) 1098 return 1; 1099 1100 setup_dummy_ept(); 1101 1102 vmcs_write(CPU_EXEC_CTRL0, vmcs_read(CPU_EXEC_CTRL0) | CPU_SECONDARY); 1103 vmcs_write(CPU_EXEC_CTRL1, vmcs_read(CPU_EXEC_CTRL1) | CPU_URG); 1104 1105 return 0; 1106 } 1107 1108 static void ept_enable_ad_bits(void) 1109 { 1110 eptp |= EPTP_AD_FLAG; 1111 vmcs_write(EPTP, eptp); 1112 } 1113 1114 static void ept_disable_ad_bits(void) 1115 { 1116 eptp &= ~EPTP_AD_FLAG; 1117 vmcs_write(EPTP, eptp); 1118 } 1119 1120 static void ept_enable_ad_bits_or_skip_test(void) 1121 { 1122 if (!ept_ad_bits_supported()) 1123 test_skip("EPT AD bits not supported."); 1124 ept_enable_ad_bits(); 1125 } 1126 1127 static int apic_version; 1128 1129 static int ept_init_common(bool have_ad) 1130 { 1131 int ret; 1132 struct pci_dev pcidev; 1133 1134 if (setup_ept(have_ad)) 1135 return VMX_TEST_EXIT; 1136 data_page1 = alloc_page(); 1137 data_page2 = alloc_page(); 1138 memset(data_page1, 0x0, PAGE_SIZE); 1139 memset(data_page2, 0x0, PAGE_SIZE); 1140 *((u32 *)data_page1) = MAGIC_VAL_1; 1141 *((u32 *)data_page2) = MAGIC_VAL_2; 1142 install_ept(pml4, (unsigned long)data_page1, (unsigned long)data_page2, 1143 EPT_RA | EPT_WA | EPT_EA); 1144 1145 apic_version = apic_read(APIC_LVR); 1146 1147 ret = pci_find_dev(PCI_VENDOR_ID_REDHAT, PCI_DEVICE_ID_REDHAT_TEST); 1148 if (ret != PCIDEVADDR_INVALID) { 1149 pci_dev_init(&pcidev, ret); 1150 pci_physaddr = pcidev.resource[PCI_TESTDEV_BAR_MEM]; 1151 } 1152 1153 return VMX_TEST_START; 1154 } 1155 1156 static int ept_init(struct vmcs *vmcs) 1157 { 1158 return ept_init_common(false); 1159 } 1160 1161 static void ept_common(void) 1162 { 1163 vmx_set_test_stage(0); 1164 if (*((u32 *)data_page2) != MAGIC_VAL_1 || 1165 *((u32 *)data_page1) != MAGIC_VAL_1) 1166 report("EPT basic framework - read", 0); 1167 else { 1168 *((u32 *)data_page2) = MAGIC_VAL_3; 1169 vmcall(); 1170 if (vmx_get_test_stage() == 1) { 1171 if (*((u32 *)data_page1) == MAGIC_VAL_3 && 1172 *((u32 *)data_page2) == MAGIC_VAL_2) 1173 report("EPT basic framework", 1); 1174 else 1175 report("EPT basic framework - remap", 1); 1176 } 1177 } 1178 // Test EPT Misconfigurations 1179 vmx_set_test_stage(1); 1180 vmcall(); 1181 *((u32 *)data_page1) = MAGIC_VAL_1; 1182 if (vmx_get_test_stage() != 2) { 1183 report("EPT misconfigurations", 0); 1184 goto t1; 1185 } 1186 vmx_set_test_stage(2); 1187 vmcall(); 1188 *((u32 *)data_page1) = MAGIC_VAL_1; 1189 report("EPT misconfigurations", vmx_get_test_stage() == 3); 1190 t1: 1191 // Test EPT violation 1192 vmx_set_test_stage(3); 1193 vmcall(); 1194 *((u32 *)data_page1) = MAGIC_VAL_1; 1195 report("EPT violation - page permission", vmx_get_test_stage() == 4); 1196 // Violation caused by EPT paging structure 1197 vmx_set_test_stage(4); 1198 vmcall(); 1199 *((u32 *)data_page1) = MAGIC_VAL_2; 1200 report("EPT violation - paging structure", vmx_get_test_stage() == 5); 1201 1202 // MMIO Read/Write 1203 vmx_set_test_stage(5); 1204 vmcall(); 1205 1206 *(u32 volatile *)pci_physaddr; 1207 report("MMIO EPT violation - read", vmx_get_test_stage() == 6); 1208 1209 *(u32 volatile *)pci_physaddr = MAGIC_VAL_1; 1210 report("MMIO EPT violation - write", vmx_get_test_stage() == 7); 1211 } 1212 1213 static void ept_main(void) 1214 { 1215 ept_common(); 1216 1217 // Test EPT access to L1 MMIO 1218 vmx_set_test_stage(7); 1219 report("EPT - MMIO access", *((u32 *)0xfee00030UL) == apic_version); 1220 1221 // Test invalid operand for INVEPT 1222 vmcall(); 1223 report("EPT - unsupported INVEPT", vmx_get_test_stage() == 8); 1224 } 1225 1226 static bool invept_test(int type, u64 eptp) 1227 { 1228 bool ret, supported; 1229 1230 supported = ept_vpid.val & (EPT_CAP_INVEPT_SINGLE >> INVEPT_SINGLE << type); 1231 ret = invept(type, eptp); 1232 1233 if (ret == !supported) 1234 return false; 1235 1236 if (!supported) 1237 printf("WARNING: unsupported invept passed!\n"); 1238 else 1239 printf("WARNING: invept failed!\n"); 1240 1241 return true; 1242 } 1243 1244 static int pml_exit_handler(void) 1245 { 1246 u16 index, count; 1247 ulong reason = vmcs_read(EXI_REASON) & 0xff; 1248 u64 *pmlbuf = pml_log; 1249 u64 guest_rip = vmcs_read(GUEST_RIP);; 1250 u64 guest_cr3 = vmcs_read(GUEST_CR3); 1251 u32 insn_len = vmcs_read(EXI_INST_LEN); 1252 1253 switch (reason) { 1254 case VMX_VMCALL: 1255 switch (vmx_get_test_stage()) { 1256 case 0: 1257 index = vmcs_read(GUEST_PML_INDEX); 1258 for (count = index + 1; count < PML_INDEX; count++) { 1259 if (pmlbuf[count] == (u64)data_page2) { 1260 vmx_inc_test_stage(); 1261 clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page2); 1262 break; 1263 } 1264 } 1265 break; 1266 case 1: 1267 index = vmcs_read(GUEST_PML_INDEX); 1268 /* Keep clearing the dirty bit till a overflow */ 1269 clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page2); 1270 break; 1271 default: 1272 report("unexpected stage, %d.", false, 1273 vmx_get_test_stage()); 1274 print_vmexit_info(); 1275 return VMX_TEST_VMEXIT; 1276 } 1277 vmcs_write(GUEST_RIP, guest_rip + insn_len); 1278 return VMX_TEST_RESUME; 1279 case VMX_PML_FULL: 1280 vmx_inc_test_stage(); 1281 vmcs_write(GUEST_PML_INDEX, PML_INDEX - 1); 1282 return VMX_TEST_RESUME; 1283 default: 1284 report("Unknown exit reason, %ld", false, reason); 1285 print_vmexit_info(); 1286 } 1287 return VMX_TEST_VMEXIT; 1288 } 1289 1290 static int ept_exit_handler_common(bool have_ad) 1291 { 1292 u64 guest_rip; 1293 u64 guest_cr3; 1294 ulong reason; 1295 u32 insn_len; 1296 u32 exit_qual; 1297 static unsigned long data_page1_pte, data_page1_pte_pte, memaddr_pte; 1298 1299 guest_rip = vmcs_read(GUEST_RIP); 1300 guest_cr3 = vmcs_read(GUEST_CR3); 1301 reason = vmcs_read(EXI_REASON) & 0xff; 1302 insn_len = vmcs_read(EXI_INST_LEN); 1303 exit_qual = vmcs_read(EXI_QUALIFICATION); 1304 switch (reason) { 1305 case VMX_VMCALL: 1306 switch (vmx_get_test_stage()) { 1307 case 0: 1308 check_ept_ad(pml4, guest_cr3, 1309 (unsigned long)data_page1, 1310 have_ad ? EPT_ACCESS_FLAG : 0, 1311 have_ad ? EPT_ACCESS_FLAG | EPT_DIRTY_FLAG : 0); 1312 check_ept_ad(pml4, guest_cr3, 1313 (unsigned long)data_page2, 1314 have_ad ? EPT_ACCESS_FLAG | EPT_DIRTY_FLAG : 0, 1315 have_ad ? EPT_ACCESS_FLAG | EPT_DIRTY_FLAG : 0); 1316 clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page1); 1317 clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page2); 1318 if (have_ad) 1319 ept_sync(INVEPT_SINGLE, eptp);; 1320 if (*((u32 *)data_page1) == MAGIC_VAL_3 && 1321 *((u32 *)data_page2) == MAGIC_VAL_2) { 1322 vmx_inc_test_stage(); 1323 install_ept(pml4, (unsigned long)data_page2, 1324 (unsigned long)data_page2, 1325 EPT_RA | EPT_WA | EPT_EA); 1326 } else 1327 report("EPT basic framework - write", 0); 1328 break; 1329 case 1: 1330 install_ept(pml4, (unsigned long)data_page1, 1331 (unsigned long)data_page1, EPT_WA); 1332 ept_sync(INVEPT_SINGLE, eptp); 1333 break; 1334 case 2: 1335 install_ept(pml4, (unsigned long)data_page1, 1336 (unsigned long)data_page1, 1337 EPT_RA | EPT_WA | EPT_EA | 1338 (2 << EPT_MEM_TYPE_SHIFT)); 1339 ept_sync(INVEPT_SINGLE, eptp); 1340 break; 1341 case 3: 1342 clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page1); 1343 TEST_ASSERT(get_ept_pte(pml4, (unsigned long)data_page1, 1344 1, &data_page1_pte)); 1345 set_ept_pte(pml4, (unsigned long)data_page1, 1346 1, data_page1_pte & ~EPT_PRESENT); 1347 ept_sync(INVEPT_SINGLE, eptp); 1348 break; 1349 case 4: 1350 TEST_ASSERT(get_ept_pte(pml4, (unsigned long)data_page1, 1351 2, &data_page1_pte)); 1352 data_page1_pte &= PAGE_MASK; 1353 TEST_ASSERT(get_ept_pte(pml4, data_page1_pte, 1354 2, &data_page1_pte_pte)); 1355 set_ept_pte(pml4, data_page1_pte, 2, 1356 data_page1_pte_pte & ~EPT_PRESENT); 1357 ept_sync(INVEPT_SINGLE, eptp); 1358 break; 1359 case 5: 1360 install_ept(pml4, (unsigned long)pci_physaddr, 1361 (unsigned long)pci_physaddr, 0); 1362 ept_sync(INVEPT_SINGLE, eptp); 1363 break; 1364 case 7: 1365 if (!invept_test(0, eptp)) 1366 vmx_inc_test_stage(); 1367 break; 1368 // Should not reach here 1369 default: 1370 report("ERROR - unexpected stage, %d.", false, 1371 vmx_get_test_stage()); 1372 print_vmexit_info(); 1373 return VMX_TEST_VMEXIT; 1374 } 1375 vmcs_write(GUEST_RIP, guest_rip + insn_len); 1376 return VMX_TEST_RESUME; 1377 case VMX_EPT_MISCONFIG: 1378 switch (vmx_get_test_stage()) { 1379 case 1: 1380 case 2: 1381 vmx_inc_test_stage(); 1382 install_ept(pml4, (unsigned long)data_page1, 1383 (unsigned long)data_page1, 1384 EPT_RA | EPT_WA | EPT_EA); 1385 ept_sync(INVEPT_SINGLE, eptp); 1386 break; 1387 // Should not reach here 1388 default: 1389 report("ERROR - unexpected stage, %d.", false, 1390 vmx_get_test_stage()); 1391 print_vmexit_info(); 1392 return VMX_TEST_VMEXIT; 1393 } 1394 return VMX_TEST_RESUME; 1395 case VMX_EPT_VIOLATION: 1396 switch(vmx_get_test_stage()) { 1397 case 3: 1398 check_ept_ad(pml4, guest_cr3, (unsigned long)data_page1, 0, 1399 have_ad ? EPT_ACCESS_FLAG | EPT_DIRTY_FLAG : 0); 1400 clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page1); 1401 if (exit_qual == (EPT_VLT_WR | EPT_VLT_LADDR_VLD | 1402 EPT_VLT_PADDR)) 1403 vmx_inc_test_stage(); 1404 set_ept_pte(pml4, (unsigned long)data_page1, 1405 1, data_page1_pte | (EPT_PRESENT)); 1406 ept_sync(INVEPT_SINGLE, eptp); 1407 break; 1408 case 4: 1409 check_ept_ad(pml4, guest_cr3, (unsigned long)data_page1, 0, 1410 have_ad ? EPT_ACCESS_FLAG | EPT_DIRTY_FLAG : 0); 1411 clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page1); 1412 if (exit_qual == (EPT_VLT_RD | 1413 (have_ad ? EPT_VLT_WR : 0) | 1414 EPT_VLT_LADDR_VLD)) 1415 vmx_inc_test_stage(); 1416 set_ept_pte(pml4, data_page1_pte, 2, 1417 data_page1_pte_pte | (EPT_PRESENT)); 1418 ept_sync(INVEPT_SINGLE, eptp); 1419 break; 1420 case 5: 1421 if (exit_qual & EPT_VLT_RD) 1422 vmx_inc_test_stage(); 1423 TEST_ASSERT(get_ept_pte(pml4, (unsigned long)pci_physaddr, 1424 1, &memaddr_pte)); 1425 set_ept_pte(pml4, memaddr_pte, 1, memaddr_pte | EPT_RA); 1426 ept_sync(INVEPT_SINGLE, eptp); 1427 break; 1428 case 6: 1429 if (exit_qual & EPT_VLT_WR) 1430 vmx_inc_test_stage(); 1431 TEST_ASSERT(get_ept_pte(pml4, (unsigned long)pci_physaddr, 1432 1, &memaddr_pte)); 1433 set_ept_pte(pml4, memaddr_pte, 1, memaddr_pte | EPT_RA | EPT_WA); 1434 ept_sync(INVEPT_SINGLE, eptp); 1435 break; 1436 default: 1437 // Should not reach here 1438 report("ERROR : unexpected stage, %d", false, 1439 vmx_get_test_stage()); 1440 print_vmexit_info(); 1441 return VMX_TEST_VMEXIT; 1442 } 1443 return VMX_TEST_RESUME; 1444 default: 1445 report("Unknown exit reason, %ld", false, reason); 1446 print_vmexit_info(); 1447 } 1448 return VMX_TEST_VMEXIT; 1449 } 1450 1451 static int ept_exit_handler(void) 1452 { 1453 return ept_exit_handler_common(false); 1454 } 1455 1456 static int eptad_init(struct vmcs *vmcs) 1457 { 1458 int r = ept_init_common(true); 1459 1460 if (r == VMX_TEST_EXIT) 1461 return r; 1462 1463 if ((rdmsr(MSR_IA32_VMX_EPT_VPID_CAP) & EPT_CAP_AD_FLAG) == 0) { 1464 printf("\tEPT A/D bits are not supported"); 1465 return VMX_TEST_EXIT; 1466 } 1467 1468 return r; 1469 } 1470 1471 static int pml_init(struct vmcs *vmcs) 1472 { 1473 u32 ctrl_cpu; 1474 int r = eptad_init(vmcs); 1475 1476 if (r == VMX_TEST_EXIT) 1477 return r; 1478 1479 if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) || 1480 !(ctrl_cpu_rev[1].clr & CPU_PML)) { 1481 printf("\tPML is not supported"); 1482 return VMX_TEST_EXIT; 1483 } 1484 1485 pml_log = alloc_page(); 1486 memset(pml_log, 0x0, PAGE_SIZE); 1487 vmcs_write(PMLADDR, (u64)pml_log); 1488 vmcs_write(GUEST_PML_INDEX, PML_INDEX - 1); 1489 1490 ctrl_cpu = vmcs_read(CPU_EXEC_CTRL1) | CPU_PML; 1491 vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu); 1492 1493 return VMX_TEST_START; 1494 } 1495 1496 static void pml_main(void) 1497 { 1498 int count = 0; 1499 1500 vmx_set_test_stage(0); 1501 *((u32 *)data_page2) = 0x1; 1502 vmcall(); 1503 report("PML - Dirty GPA Logging", vmx_get_test_stage() == 1); 1504 1505 while (vmx_get_test_stage() == 1) { 1506 vmcall(); 1507 *((u32 *)data_page2) = 0x1; 1508 if (count++ > PML_INDEX) 1509 break; 1510 } 1511 report("PML Full Event", vmx_get_test_stage() == 2); 1512 } 1513 1514 static void eptad_main(void) 1515 { 1516 ept_common(); 1517 } 1518 1519 static int eptad_exit_handler(void) 1520 { 1521 return ept_exit_handler_common(true); 1522 } 1523 1524 static bool invvpid_test(int type, u16 vpid) 1525 { 1526 bool ret, supported; 1527 1528 supported = ept_vpid.val & 1529 (VPID_CAP_INVVPID_ADDR >> INVVPID_ADDR << type); 1530 ret = invvpid(type, vpid, 0); 1531 1532 if (ret == !supported) 1533 return false; 1534 1535 if (!supported) 1536 printf("WARNING: unsupported invvpid passed!\n"); 1537 else 1538 printf("WARNING: invvpid failed!\n"); 1539 1540 return true; 1541 } 1542 1543 static int vpid_init(struct vmcs *vmcs) 1544 { 1545 u32 ctrl_cpu1; 1546 1547 if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) || 1548 !(ctrl_cpu_rev[1].clr & CPU_VPID)) { 1549 printf("\tVPID is not supported"); 1550 return VMX_TEST_EXIT; 1551 } 1552 1553 ctrl_cpu1 = vmcs_read(CPU_EXEC_CTRL1); 1554 ctrl_cpu1 |= CPU_VPID; 1555 vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu1); 1556 return VMX_TEST_START; 1557 } 1558 1559 static void vpid_main(void) 1560 { 1561 vmx_set_test_stage(0); 1562 vmcall(); 1563 report("INVVPID SINGLE ADDRESS", vmx_get_test_stage() == 1); 1564 vmx_set_test_stage(2); 1565 vmcall(); 1566 report("INVVPID SINGLE", vmx_get_test_stage() == 3); 1567 vmx_set_test_stage(4); 1568 vmcall(); 1569 report("INVVPID ALL", vmx_get_test_stage() == 5); 1570 } 1571 1572 static int vpid_exit_handler(void) 1573 { 1574 u64 guest_rip; 1575 ulong reason; 1576 u32 insn_len; 1577 1578 guest_rip = vmcs_read(GUEST_RIP); 1579 reason = vmcs_read(EXI_REASON) & 0xff; 1580 insn_len = vmcs_read(EXI_INST_LEN); 1581 1582 switch (reason) { 1583 case VMX_VMCALL: 1584 switch(vmx_get_test_stage()) { 1585 case 0: 1586 if (!invvpid_test(INVVPID_ADDR, 1)) 1587 vmx_inc_test_stage(); 1588 break; 1589 case 2: 1590 if (!invvpid_test(INVVPID_CONTEXT_GLOBAL, 1)) 1591 vmx_inc_test_stage(); 1592 break; 1593 case 4: 1594 if (!invvpid_test(INVVPID_ALL, 1)) 1595 vmx_inc_test_stage(); 1596 break; 1597 default: 1598 report("ERROR: unexpected stage, %d", false, 1599 vmx_get_test_stage()); 1600 print_vmexit_info(); 1601 return VMX_TEST_VMEXIT; 1602 } 1603 vmcs_write(GUEST_RIP, guest_rip + insn_len); 1604 return VMX_TEST_RESUME; 1605 default: 1606 report("Unknown exit reason, %ld", false, reason); 1607 print_vmexit_info(); 1608 } 1609 return VMX_TEST_VMEXIT; 1610 } 1611 1612 #define TIMER_VECTOR 222 1613 1614 static volatile bool timer_fired; 1615 1616 static void timer_isr(isr_regs_t *regs) 1617 { 1618 timer_fired = true; 1619 apic_write(APIC_EOI, 0); 1620 } 1621 1622 static int interrupt_init(struct vmcs *vmcs) 1623 { 1624 msr_bmp_init(); 1625 vmcs_write(PIN_CONTROLS, vmcs_read(PIN_CONTROLS) & ~PIN_EXTINT); 1626 handle_irq(TIMER_VECTOR, timer_isr); 1627 return VMX_TEST_START; 1628 } 1629 1630 static void interrupt_main(void) 1631 { 1632 long long start, loops; 1633 1634 vmx_set_test_stage(0); 1635 1636 apic_write(APIC_LVTT, TIMER_VECTOR); 1637 irq_enable(); 1638 1639 apic_write(APIC_TMICT, 1); 1640 for (loops = 0; loops < 10000000 && !timer_fired; loops++) 1641 asm volatile ("nop"); 1642 report("direct interrupt while running guest", timer_fired); 1643 1644 apic_write(APIC_TMICT, 0); 1645 irq_disable(); 1646 vmcall(); 1647 timer_fired = false; 1648 apic_write(APIC_TMICT, 1); 1649 for (loops = 0; loops < 10000000 && !timer_fired; loops++) 1650 asm volatile ("nop"); 1651 report("intercepted interrupt while running guest", timer_fired); 1652 1653 irq_enable(); 1654 apic_write(APIC_TMICT, 0); 1655 irq_disable(); 1656 vmcall(); 1657 timer_fired = false; 1658 start = rdtsc(); 1659 apic_write(APIC_TMICT, 1000000); 1660 1661 asm volatile ("sti; hlt"); 1662 1663 report("direct interrupt + hlt", 1664 rdtsc() - start > 1000000 && timer_fired); 1665 1666 apic_write(APIC_TMICT, 0); 1667 irq_disable(); 1668 vmcall(); 1669 timer_fired = false; 1670 start = rdtsc(); 1671 apic_write(APIC_TMICT, 1000000); 1672 1673 asm volatile ("sti; hlt"); 1674 1675 report("intercepted interrupt + hlt", 1676 rdtsc() - start > 10000 && timer_fired); 1677 1678 apic_write(APIC_TMICT, 0); 1679 irq_disable(); 1680 vmcall(); 1681 timer_fired = false; 1682 start = rdtsc(); 1683 apic_write(APIC_TMICT, 1000000); 1684 1685 irq_enable(); 1686 asm volatile ("nop"); 1687 vmcall(); 1688 1689 report("direct interrupt + activity state hlt", 1690 rdtsc() - start > 10000 && timer_fired); 1691 1692 apic_write(APIC_TMICT, 0); 1693 irq_disable(); 1694 vmcall(); 1695 timer_fired = false; 1696 start = rdtsc(); 1697 apic_write(APIC_TMICT, 1000000); 1698 1699 irq_enable(); 1700 asm volatile ("nop"); 1701 vmcall(); 1702 1703 report("intercepted interrupt + activity state hlt", 1704 rdtsc() - start > 10000 && timer_fired); 1705 1706 apic_write(APIC_TMICT, 0); 1707 irq_disable(); 1708 vmx_set_test_stage(7); 1709 vmcall(); 1710 timer_fired = false; 1711 apic_write(APIC_TMICT, 1); 1712 for (loops = 0; loops < 10000000 && !timer_fired; loops++) 1713 asm volatile ("nop"); 1714 report("running a guest with interrupt acknowledgement set", timer_fired); 1715 1716 apic_write(APIC_TMICT, 0); 1717 irq_enable(); 1718 timer_fired = false; 1719 vmcall(); 1720 report("Inject an event to a halted guest", timer_fired); 1721 } 1722 1723 static int interrupt_exit_handler(void) 1724 { 1725 u64 guest_rip = vmcs_read(GUEST_RIP); 1726 ulong reason = vmcs_read(EXI_REASON) & 0xff; 1727 u32 insn_len = vmcs_read(EXI_INST_LEN); 1728 1729 switch (reason) { 1730 case VMX_VMCALL: 1731 switch (vmx_get_test_stage()) { 1732 case 0: 1733 case 2: 1734 case 5: 1735 vmcs_write(PIN_CONTROLS, 1736 vmcs_read(PIN_CONTROLS) | PIN_EXTINT); 1737 break; 1738 case 7: 1739 vmcs_write(EXI_CONTROLS, vmcs_read(EXI_CONTROLS) | EXI_INTA); 1740 vmcs_write(PIN_CONTROLS, 1741 vmcs_read(PIN_CONTROLS) | PIN_EXTINT); 1742 break; 1743 case 1: 1744 case 3: 1745 vmcs_write(PIN_CONTROLS, 1746 vmcs_read(PIN_CONTROLS) & ~PIN_EXTINT); 1747 break; 1748 case 4: 1749 case 6: 1750 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 1751 break; 1752 1753 case 8: 1754 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 1755 vmcs_write(ENT_INTR_INFO, 1756 TIMER_VECTOR | 1757 (VMX_INTR_TYPE_EXT_INTR << INTR_INFO_INTR_TYPE_SHIFT) | 1758 INTR_INFO_VALID_MASK); 1759 break; 1760 } 1761 vmx_inc_test_stage(); 1762 vmcs_write(GUEST_RIP, guest_rip + insn_len); 1763 return VMX_TEST_RESUME; 1764 case VMX_EXTINT: 1765 if (vmcs_read(EXI_CONTROLS) & EXI_INTA) { 1766 int vector = vmcs_read(EXI_INTR_INFO) & 0xff; 1767 handle_external_interrupt(vector); 1768 } else { 1769 irq_enable(); 1770 asm volatile ("nop"); 1771 irq_disable(); 1772 } 1773 if (vmx_get_test_stage() >= 2) 1774 vmcs_write(GUEST_ACTV_STATE, ACTV_ACTIVE); 1775 return VMX_TEST_RESUME; 1776 default: 1777 report("Unknown exit reason, %ld", false, reason); 1778 print_vmexit_info(); 1779 } 1780 1781 return VMX_TEST_VMEXIT; 1782 } 1783 1784 static int dbgctls_init(struct vmcs *vmcs) 1785 { 1786 u64 dr7 = 0x402; 1787 u64 zero = 0; 1788 1789 msr_bmp_init(); 1790 asm volatile( 1791 "mov %0,%%dr0\n\t" 1792 "mov %0,%%dr1\n\t" 1793 "mov %0,%%dr2\n\t" 1794 "mov %1,%%dr7\n\t" 1795 : : "r" (zero), "r" (dr7)); 1796 wrmsr(MSR_IA32_DEBUGCTLMSR, 0x1); 1797 vmcs_write(GUEST_DR7, 0x404); 1798 vmcs_write(GUEST_DEBUGCTL, 0x2); 1799 1800 vmcs_write(ENT_CONTROLS, vmcs_read(ENT_CONTROLS) | ENT_LOAD_DBGCTLS); 1801 vmcs_write(EXI_CONTROLS, vmcs_read(EXI_CONTROLS) | EXI_SAVE_DBGCTLS); 1802 1803 return VMX_TEST_START; 1804 } 1805 1806 static void dbgctls_main(void) 1807 { 1808 u64 dr7, debugctl; 1809 1810 asm volatile("mov %%dr7,%0" : "=r" (dr7)); 1811 debugctl = rdmsr(MSR_IA32_DEBUGCTLMSR); 1812 /* Commented out: KVM does not support DEBUGCTL so far */ 1813 (void)debugctl; 1814 report("Load debug controls", dr7 == 0x404 /* && debugctl == 0x2 */); 1815 1816 dr7 = 0x408; 1817 asm volatile("mov %0,%%dr7" : : "r" (dr7)); 1818 wrmsr(MSR_IA32_DEBUGCTLMSR, 0x3); 1819 1820 vmx_set_test_stage(0); 1821 vmcall(); 1822 report("Save debug controls", vmx_get_test_stage() == 1); 1823 1824 if (ctrl_enter_rev.set & ENT_LOAD_DBGCTLS || 1825 ctrl_exit_rev.set & EXI_SAVE_DBGCTLS) { 1826 printf("\tDebug controls are always loaded/saved\n"); 1827 return; 1828 } 1829 vmx_set_test_stage(2); 1830 vmcall(); 1831 1832 asm volatile("mov %%dr7,%0" : "=r" (dr7)); 1833 debugctl = rdmsr(MSR_IA32_DEBUGCTLMSR); 1834 /* Commented out: KVM does not support DEBUGCTL so far */ 1835 (void)debugctl; 1836 report("Guest=host debug controls", dr7 == 0x402 /* && debugctl == 0x1 */); 1837 1838 dr7 = 0x408; 1839 asm volatile("mov %0,%%dr7" : : "r" (dr7)); 1840 wrmsr(MSR_IA32_DEBUGCTLMSR, 0x3); 1841 1842 vmx_set_test_stage(3); 1843 vmcall(); 1844 report("Don't save debug controls", vmx_get_test_stage() == 4); 1845 } 1846 1847 static int dbgctls_exit_handler(void) 1848 { 1849 unsigned int reason = vmcs_read(EXI_REASON) & 0xff; 1850 u32 insn_len = vmcs_read(EXI_INST_LEN); 1851 u64 guest_rip = vmcs_read(GUEST_RIP); 1852 u64 dr7, debugctl; 1853 1854 asm volatile("mov %%dr7,%0" : "=r" (dr7)); 1855 debugctl = rdmsr(MSR_IA32_DEBUGCTLMSR); 1856 1857 switch (reason) { 1858 case VMX_VMCALL: 1859 switch (vmx_get_test_stage()) { 1860 case 0: 1861 if (dr7 == 0x400 && debugctl == 0 && 1862 vmcs_read(GUEST_DR7) == 0x408 /* && 1863 Commented out: KVM does not support DEBUGCTL so far 1864 vmcs_read(GUEST_DEBUGCTL) == 0x3 */) 1865 vmx_inc_test_stage(); 1866 break; 1867 case 2: 1868 dr7 = 0x402; 1869 asm volatile("mov %0,%%dr7" : : "r" (dr7)); 1870 wrmsr(MSR_IA32_DEBUGCTLMSR, 0x1); 1871 vmcs_write(GUEST_DR7, 0x404); 1872 vmcs_write(GUEST_DEBUGCTL, 0x2); 1873 1874 vmcs_write(ENT_CONTROLS, 1875 vmcs_read(ENT_CONTROLS) & ~ENT_LOAD_DBGCTLS); 1876 vmcs_write(EXI_CONTROLS, 1877 vmcs_read(EXI_CONTROLS) & ~EXI_SAVE_DBGCTLS); 1878 break; 1879 case 3: 1880 if (dr7 == 0x400 && debugctl == 0 && 1881 vmcs_read(GUEST_DR7) == 0x404 /* && 1882 Commented out: KVM does not support DEBUGCTL so far 1883 vmcs_read(GUEST_DEBUGCTL) == 0x2 */) 1884 vmx_inc_test_stage(); 1885 break; 1886 } 1887 vmcs_write(GUEST_RIP, guest_rip + insn_len); 1888 return VMX_TEST_RESUME; 1889 default: 1890 report("Unknown exit reason, %d", false, reason); 1891 print_vmexit_info(); 1892 } 1893 return VMX_TEST_VMEXIT; 1894 } 1895 1896 struct vmx_msr_entry { 1897 u32 index; 1898 u32 reserved; 1899 u64 value; 1900 } __attribute__((packed)); 1901 1902 #define MSR_MAGIC 0x31415926 1903 struct vmx_msr_entry *exit_msr_store, *entry_msr_load, *exit_msr_load; 1904 1905 static int msr_switch_init(struct vmcs *vmcs) 1906 { 1907 msr_bmp_init(); 1908 exit_msr_store = alloc_page(); 1909 exit_msr_load = alloc_page(); 1910 entry_msr_load = alloc_page(); 1911 memset(exit_msr_store, 0, PAGE_SIZE); 1912 memset(exit_msr_load, 0, PAGE_SIZE); 1913 memset(entry_msr_load, 0, PAGE_SIZE); 1914 entry_msr_load[0].index = MSR_KERNEL_GS_BASE; 1915 entry_msr_load[0].value = MSR_MAGIC; 1916 1917 vmx_set_test_stage(1); 1918 vmcs_write(ENT_MSR_LD_CNT, 1); 1919 vmcs_write(ENTER_MSR_LD_ADDR, (u64)entry_msr_load); 1920 vmcs_write(EXI_MSR_ST_CNT, 1); 1921 vmcs_write(EXIT_MSR_ST_ADDR, (u64)exit_msr_store); 1922 vmcs_write(EXI_MSR_LD_CNT, 1); 1923 vmcs_write(EXIT_MSR_LD_ADDR, (u64)exit_msr_load); 1924 return VMX_TEST_START; 1925 } 1926 1927 static void msr_switch_main(void) 1928 { 1929 if (vmx_get_test_stage() == 1) { 1930 report("VM entry MSR load", 1931 rdmsr(MSR_KERNEL_GS_BASE) == MSR_MAGIC); 1932 vmx_set_test_stage(2); 1933 wrmsr(MSR_KERNEL_GS_BASE, MSR_MAGIC + 1); 1934 exit_msr_store[0].index = MSR_KERNEL_GS_BASE; 1935 exit_msr_load[0].index = MSR_KERNEL_GS_BASE; 1936 exit_msr_load[0].value = MSR_MAGIC + 2; 1937 } 1938 vmcall(); 1939 } 1940 1941 static int msr_switch_exit_handler(void) 1942 { 1943 ulong reason; 1944 1945 reason = vmcs_read(EXI_REASON); 1946 if (reason == VMX_VMCALL && vmx_get_test_stage() == 2) { 1947 report("VM exit MSR store", 1948 exit_msr_store[0].value == MSR_MAGIC + 1); 1949 report("VM exit MSR load", 1950 rdmsr(MSR_KERNEL_GS_BASE) == MSR_MAGIC + 2); 1951 vmx_set_test_stage(3); 1952 entry_msr_load[0].index = MSR_FS_BASE; 1953 return VMX_TEST_RESUME; 1954 } 1955 printf("ERROR %s: unexpected stage=%u or reason=%lu\n", 1956 __func__, vmx_get_test_stage(), reason); 1957 return VMX_TEST_EXIT; 1958 } 1959 1960 static int msr_switch_entry_failure(struct vmentry_failure *failure) 1961 { 1962 ulong reason; 1963 1964 if (failure->early) { 1965 printf("ERROR %s: early exit\n", __func__); 1966 return VMX_TEST_EXIT; 1967 } 1968 1969 reason = vmcs_read(EXI_REASON); 1970 if (reason == (VMX_ENTRY_FAILURE | VMX_FAIL_MSR) && 1971 vmx_get_test_stage() == 3) { 1972 report("VM entry MSR load: try to load FS_BASE", 1973 vmcs_read(EXI_QUALIFICATION) == 1); 1974 return VMX_TEST_VMEXIT; 1975 } 1976 printf("ERROR %s: unexpected stage=%u or reason=%lu\n", 1977 __func__, vmx_get_test_stage(), reason); 1978 return VMX_TEST_EXIT; 1979 } 1980 1981 static int vmmcall_init(struct vmcs *vmcs) 1982 { 1983 vmcs_write(EXC_BITMAP, 1 << UD_VECTOR); 1984 return VMX_TEST_START; 1985 } 1986 1987 static void vmmcall_main(void) 1988 { 1989 asm volatile( 1990 "mov $0xABCD, %%rax\n\t" 1991 "vmmcall\n\t" 1992 ::: "rax"); 1993 1994 report("VMMCALL", 0); 1995 } 1996 1997 static int vmmcall_exit_handler(void) 1998 { 1999 ulong reason; 2000 2001 reason = vmcs_read(EXI_REASON); 2002 switch (reason) { 2003 case VMX_VMCALL: 2004 printf("here\n"); 2005 report("VMMCALL triggers #UD", 0); 2006 break; 2007 case VMX_EXC_NMI: 2008 report("VMMCALL triggers #UD", 2009 (vmcs_read(EXI_INTR_INFO) & 0xff) == UD_VECTOR); 2010 break; 2011 default: 2012 report("Unknown exit reason, %ld", false, reason); 2013 print_vmexit_info(); 2014 } 2015 2016 return VMX_TEST_VMEXIT; 2017 } 2018 2019 static int disable_rdtscp_init(struct vmcs *vmcs) 2020 { 2021 u32 ctrl_cpu1; 2022 2023 if (ctrl_cpu_rev[0].clr & CPU_SECONDARY) { 2024 ctrl_cpu1 = vmcs_read(CPU_EXEC_CTRL1); 2025 ctrl_cpu1 &= ~CPU_RDTSCP; 2026 vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu1); 2027 } 2028 2029 return VMX_TEST_START; 2030 } 2031 2032 static void disable_rdtscp_ud_handler(struct ex_regs *regs) 2033 { 2034 switch (vmx_get_test_stage()) { 2035 case 0: 2036 report("RDTSCP triggers #UD", true); 2037 vmx_inc_test_stage(); 2038 regs->rip += 3; 2039 break; 2040 case 2: 2041 report("RDPID triggers #UD", true); 2042 vmx_inc_test_stage(); 2043 regs->rip += 4; 2044 break; 2045 } 2046 return; 2047 2048 } 2049 2050 static void disable_rdtscp_main(void) 2051 { 2052 /* Test that #UD is properly injected in L2. */ 2053 handle_exception(UD_VECTOR, disable_rdtscp_ud_handler); 2054 2055 vmx_set_test_stage(0); 2056 asm volatile("rdtscp" : : : "eax", "ecx", "edx"); 2057 vmcall(); 2058 asm volatile(".byte 0xf3, 0x0f, 0xc7, 0xf8" : : : "eax"); 2059 2060 handle_exception(UD_VECTOR, 0); 2061 vmcall(); 2062 } 2063 2064 static int disable_rdtscp_exit_handler(void) 2065 { 2066 unsigned int reason = vmcs_read(EXI_REASON) & 0xff; 2067 2068 switch (reason) { 2069 case VMX_VMCALL: 2070 switch (vmx_get_test_stage()) { 2071 case 0: 2072 report("RDTSCP triggers #UD", false); 2073 vmx_inc_test_stage(); 2074 /* fallthrough */ 2075 case 1: 2076 vmx_inc_test_stage(); 2077 vmcs_write(GUEST_RIP, vmcs_read(GUEST_RIP) + 3); 2078 return VMX_TEST_RESUME; 2079 case 2: 2080 report("RDPID triggers #UD", false); 2081 break; 2082 } 2083 break; 2084 2085 default: 2086 report("Unknown exit reason, %d", false, reason); 2087 print_vmexit_info(); 2088 } 2089 return VMX_TEST_VMEXIT; 2090 } 2091 2092 static int int3_init(struct vmcs *vmcs) 2093 { 2094 vmcs_write(EXC_BITMAP, ~0u); 2095 return VMX_TEST_START; 2096 } 2097 2098 static void int3_guest_main(void) 2099 { 2100 asm volatile ("int3"); 2101 } 2102 2103 static int int3_exit_handler(void) 2104 { 2105 u32 reason = vmcs_read(EXI_REASON); 2106 u32 intr_info = vmcs_read(EXI_INTR_INFO); 2107 2108 report("L1 intercepts #BP", reason == VMX_EXC_NMI && 2109 (intr_info & INTR_INFO_VALID_MASK) && 2110 (intr_info & INTR_INFO_VECTOR_MASK) == BP_VECTOR && 2111 ((intr_info & INTR_INFO_INTR_TYPE_MASK) >> 2112 INTR_INFO_INTR_TYPE_SHIFT) == VMX_INTR_TYPE_SOFT_EXCEPTION); 2113 2114 return VMX_TEST_VMEXIT; 2115 } 2116 2117 static int into_init(struct vmcs *vmcs) 2118 { 2119 vmcs_write(EXC_BITMAP, ~0u); 2120 return VMX_TEST_START; 2121 } 2122 2123 static void into_guest_main(void) 2124 { 2125 struct far_pointer32 fp = { 2126 .offset = (uintptr_t)&&into, 2127 .selector = KERNEL_CS32, 2128 }; 2129 register uintptr_t rsp asm("rsp"); 2130 2131 if (fp.offset != (uintptr_t)&&into) { 2132 printf("Code address too high.\n"); 2133 return; 2134 } 2135 if ((u32)rsp != rsp) { 2136 printf("Stack address too high.\n"); 2137 return; 2138 } 2139 2140 asm goto ("lcall *%0" : : "m" (fp) : "rax" : into); 2141 return; 2142 into: 2143 asm volatile (".code32;" 2144 "movl $0x7fffffff, %eax;" 2145 "addl %eax, %eax;" 2146 "into;" 2147 "lret;" 2148 ".code64"); 2149 __builtin_unreachable(); 2150 } 2151 2152 static int into_exit_handler(void) 2153 { 2154 u32 reason = vmcs_read(EXI_REASON); 2155 u32 intr_info = vmcs_read(EXI_INTR_INFO); 2156 2157 report("L1 intercepts #OF", reason == VMX_EXC_NMI && 2158 (intr_info & INTR_INFO_VALID_MASK) && 2159 (intr_info & INTR_INFO_VECTOR_MASK) == OF_VECTOR && 2160 ((intr_info & INTR_INFO_INTR_TYPE_MASK) >> 2161 INTR_INFO_INTR_TYPE_SHIFT) == VMX_INTR_TYPE_SOFT_EXCEPTION); 2162 2163 return VMX_TEST_VMEXIT; 2164 } 2165 2166 static void exit_monitor_from_l2_main(void) 2167 { 2168 printf("Calling exit(0) from l2...\n"); 2169 exit(0); 2170 } 2171 2172 static int exit_monitor_from_l2_handler(void) 2173 { 2174 report("The guest should have killed the VMM", false); 2175 return VMX_TEST_EXIT; 2176 } 2177 2178 static void assert_exit_reason(u64 expected) 2179 { 2180 u64 actual = vmcs_read(EXI_REASON); 2181 2182 TEST_ASSERT_EQ_MSG(expected, actual, "Expected %s, got %s.", 2183 exit_reason_description(expected), 2184 exit_reason_description(actual)); 2185 } 2186 2187 static void skip_exit_insn(void) 2188 { 2189 u64 guest_rip = vmcs_read(GUEST_RIP); 2190 u32 insn_len = vmcs_read(EXI_INST_LEN); 2191 vmcs_write(GUEST_RIP, guest_rip + insn_len); 2192 } 2193 2194 static void skip_exit_vmcall(void) 2195 { 2196 assert_exit_reason(VMX_VMCALL); 2197 skip_exit_insn(); 2198 } 2199 2200 static void v2_null_test_guest(void) 2201 { 2202 } 2203 2204 static void v2_null_test(void) 2205 { 2206 test_set_guest(v2_null_test_guest); 2207 enter_guest(); 2208 report(__func__, 1); 2209 } 2210 2211 static void v2_multiple_entries_test_guest(void) 2212 { 2213 vmx_set_test_stage(1); 2214 vmcall(); 2215 vmx_set_test_stage(2); 2216 } 2217 2218 static void v2_multiple_entries_test(void) 2219 { 2220 test_set_guest(v2_multiple_entries_test_guest); 2221 enter_guest(); 2222 TEST_ASSERT_EQ(vmx_get_test_stage(), 1); 2223 skip_exit_vmcall(); 2224 enter_guest(); 2225 TEST_ASSERT_EQ(vmx_get_test_stage(), 2); 2226 report(__func__, 1); 2227 } 2228 2229 static int fixture_test_data = 1; 2230 2231 static void fixture_test_teardown(void *data) 2232 { 2233 *((int *) data) = 1; 2234 } 2235 2236 static void fixture_test_guest(void) 2237 { 2238 fixture_test_data++; 2239 } 2240 2241 2242 static void fixture_test_setup(void) 2243 { 2244 TEST_ASSERT_EQ_MSG(1, fixture_test_data, 2245 "fixture_test_teardown didn't run?!"); 2246 fixture_test_data = 2; 2247 test_add_teardown(fixture_test_teardown, &fixture_test_data); 2248 test_set_guest(fixture_test_guest); 2249 } 2250 2251 static void fixture_test_case1(void) 2252 { 2253 fixture_test_setup(); 2254 TEST_ASSERT_EQ(2, fixture_test_data); 2255 enter_guest(); 2256 TEST_ASSERT_EQ(3, fixture_test_data); 2257 report(__func__, 1); 2258 } 2259 2260 static void fixture_test_case2(void) 2261 { 2262 fixture_test_setup(); 2263 TEST_ASSERT_EQ(2, fixture_test_data); 2264 enter_guest(); 2265 TEST_ASSERT_EQ(3, fixture_test_data); 2266 report(__func__, 1); 2267 } 2268 2269 enum ept_access_op { 2270 OP_READ, 2271 OP_WRITE, 2272 OP_EXEC, 2273 OP_FLUSH_TLB, 2274 OP_EXIT, 2275 }; 2276 2277 static struct ept_access_test_data { 2278 unsigned long gpa; 2279 unsigned long *gva; 2280 unsigned long hpa; 2281 unsigned long *hva; 2282 enum ept_access_op op; 2283 } ept_access_test_data; 2284 2285 extern unsigned char ret42_start; 2286 extern unsigned char ret42_end; 2287 2288 /* Returns 42. */ 2289 asm( 2290 ".align 64\n" 2291 "ret42_start:\n" 2292 "mov $42, %eax\n" 2293 "ret\n" 2294 "ret42_end:\n" 2295 ); 2296 2297 static void 2298 diagnose_ept_violation_qual(u64 expected, u64 actual) 2299 { 2300 2301 #define DIAGNOSE(flag) \ 2302 do { \ 2303 if ((expected & flag) != (actual & flag)) \ 2304 printf(#flag " %sexpected\n", \ 2305 (expected & flag) ? "" : "un"); \ 2306 } while (0) 2307 2308 DIAGNOSE(EPT_VLT_RD); 2309 DIAGNOSE(EPT_VLT_WR); 2310 DIAGNOSE(EPT_VLT_FETCH); 2311 DIAGNOSE(EPT_VLT_PERM_RD); 2312 DIAGNOSE(EPT_VLT_PERM_WR); 2313 DIAGNOSE(EPT_VLT_PERM_EX); 2314 DIAGNOSE(EPT_VLT_LADDR_VLD); 2315 DIAGNOSE(EPT_VLT_PADDR); 2316 2317 #undef DIAGNOSE 2318 } 2319 2320 static void do_ept_access_op(enum ept_access_op op) 2321 { 2322 ept_access_test_data.op = op; 2323 enter_guest(); 2324 } 2325 2326 /* 2327 * Force the guest to flush its TLB (i.e., flush gva -> gpa mappings). Only 2328 * needed by tests that modify guest PTEs. 2329 */ 2330 static void ept_access_test_guest_flush_tlb(void) 2331 { 2332 do_ept_access_op(OP_FLUSH_TLB); 2333 skip_exit_vmcall(); 2334 } 2335 2336 /* 2337 * Modifies the EPT entry at @level in the mapping of @gpa. First clears the 2338 * bits in @clear then sets the bits in @set. @mkhuge transforms the entry into 2339 * a huge page. 2340 */ 2341 static unsigned long ept_twiddle(unsigned long gpa, bool mkhuge, int level, 2342 unsigned long clear, unsigned long set) 2343 { 2344 struct ept_access_test_data *data = &ept_access_test_data; 2345 unsigned long orig_pte; 2346 unsigned long pte; 2347 2348 /* Screw with the mapping at the requested level. */ 2349 TEST_ASSERT(get_ept_pte(pml4, gpa, level, &orig_pte)); 2350 pte = orig_pte; 2351 if (mkhuge) 2352 pte = (orig_pte & ~EPT_ADDR_MASK) | data->hpa | EPT_LARGE_PAGE; 2353 else 2354 pte = orig_pte; 2355 pte = (pte & ~clear) | set; 2356 set_ept_pte(pml4, gpa, level, pte); 2357 ept_sync(INVEPT_SINGLE, eptp); 2358 2359 return orig_pte; 2360 } 2361 2362 static void ept_untwiddle(unsigned long gpa, int level, unsigned long orig_pte) 2363 { 2364 set_ept_pte(pml4, gpa, level, orig_pte); 2365 } 2366 2367 static void do_ept_violation(bool leaf, enum ept_access_op op, 2368 u64 expected_qual, u64 expected_paddr) 2369 { 2370 u64 qual; 2371 2372 /* Try the access and observe the violation. */ 2373 do_ept_access_op(op); 2374 2375 assert_exit_reason(VMX_EPT_VIOLATION); 2376 2377 qual = vmcs_read(EXI_QUALIFICATION); 2378 2379 diagnose_ept_violation_qual(expected_qual, qual); 2380 TEST_EXPECT_EQ(expected_qual, qual); 2381 2382 #if 0 2383 /* Disable for now otherwise every test will fail */ 2384 TEST_EXPECT_EQ(vmcs_read(GUEST_LINEAR_ADDRESS), 2385 (unsigned long) ( 2386 op == OP_EXEC ? data->gva + 1 : data->gva)); 2387 #endif 2388 /* 2389 * TODO: tests that probe expected_paddr in pages other than the one at 2390 * the beginning of the 1g region. 2391 */ 2392 TEST_EXPECT_EQ(vmcs_read(INFO_PHYS_ADDR), expected_paddr); 2393 } 2394 2395 static void 2396 ept_violation_at_level_mkhuge(bool mkhuge, int level, unsigned long clear, 2397 unsigned long set, enum ept_access_op op, 2398 u64 expected_qual) 2399 { 2400 struct ept_access_test_data *data = &ept_access_test_data; 2401 unsigned long orig_pte; 2402 2403 orig_pte = ept_twiddle(data->gpa, mkhuge, level, clear, set); 2404 2405 do_ept_violation(level == 1 || mkhuge, op, expected_qual, 2406 op == OP_EXEC ? data->gpa + sizeof(unsigned long) : 2407 data->gpa); 2408 2409 /* Fix the violation and resume the op loop. */ 2410 ept_untwiddle(data->gpa, level, orig_pte); 2411 enter_guest(); 2412 skip_exit_vmcall(); 2413 } 2414 2415 static void 2416 ept_violation_at_level(int level, unsigned long clear, unsigned long set, 2417 enum ept_access_op op, u64 expected_qual) 2418 { 2419 ept_violation_at_level_mkhuge(false, level, clear, set, op, 2420 expected_qual); 2421 if (ept_huge_pages_supported(level)) 2422 ept_violation_at_level_mkhuge(true, level, clear, set, op, 2423 expected_qual); 2424 } 2425 2426 static void ept_violation(unsigned long clear, unsigned long set, 2427 enum ept_access_op op, u64 expected_qual) 2428 { 2429 ept_violation_at_level(1, clear, set, op, expected_qual); 2430 ept_violation_at_level(2, clear, set, op, expected_qual); 2431 ept_violation_at_level(3, clear, set, op, expected_qual); 2432 ept_violation_at_level(4, clear, set, op, expected_qual); 2433 } 2434 2435 static void ept_access_violation(unsigned long access, enum ept_access_op op, 2436 u64 expected_qual) 2437 { 2438 ept_violation(EPT_PRESENT, access, op, 2439 expected_qual | EPT_VLT_LADDR_VLD | EPT_VLT_PADDR); 2440 } 2441 2442 /* 2443 * For translations that don't involve a GVA, that is physical address (paddr) 2444 * accesses, EPT violations don't set the flag EPT_VLT_PADDR. For a typical 2445 * guest memory access, the hardware does GVA -> GPA -> HPA. However, certain 2446 * translations don't involve GVAs, such as when the hardware does the guest 2447 * page table walk. For example, in translating GVA_1 -> GPA_1, the guest MMU 2448 * might try to set an A bit on a guest PTE. If the GPA_2 that the PTE resides 2449 * on isn't present in the EPT, then the EPT violation will be for GPA_2 and 2450 * the EPT_VLT_PADDR bit will be clear in the exit qualification. 2451 * 2452 * Note that paddr violations can also be triggered by loading PAE page tables 2453 * with wonky addresses. We don't test that yet. 2454 * 2455 * This function modifies the EPT entry that maps the GPA that the guest page 2456 * table entry mapping ept_access_data.gva resides on. 2457 * 2458 * @ept_access EPT permissions to set. Other permissions are cleared. 2459 * 2460 * @pte_ad Set the A/D bits on the guest PTE accordingly. 2461 * 2462 * @op Guest operation to perform with ept_access_data.gva. 2463 * 2464 * @expect_violation 2465 * Is a violation expected during the paddr access? 2466 * 2467 * @expected_qual Expected qualification for the EPT violation. 2468 * EPT_VLT_PADDR should be clear. 2469 */ 2470 static void ept_access_paddr(unsigned long ept_access, unsigned long pte_ad, 2471 enum ept_access_op op, bool expect_violation, 2472 u64 expected_qual) 2473 { 2474 struct ept_access_test_data *data = &ept_access_test_data; 2475 unsigned long *ptep; 2476 unsigned long gpa; 2477 unsigned long orig_epte; 2478 2479 /* Modify the guest PTE mapping data->gva according to @pte_ad. */ 2480 ptep = get_pte_level(current_page_table(), data->gva, /*level=*/1); 2481 TEST_ASSERT(ptep); 2482 TEST_ASSERT_EQ(*ptep & PT_ADDR_MASK, data->gpa); 2483 *ptep = (*ptep & ~PT_AD_MASK) | pte_ad; 2484 ept_access_test_guest_flush_tlb(); 2485 2486 /* 2487 * Now modify the access bits on the EPT entry for the GPA that the 2488 * guest PTE resides on. Note that by modifying a single EPT entry, 2489 * we're potentially affecting 512 guest PTEs. However, we've carefully 2490 * constructed our test such that those other 511 PTEs aren't used by 2491 * the guest: data->gva is at the beginning of a 1G huge page, thus the 2492 * PTE we're modifying is at the beginning of a 4K page and the 2493 * following 511 entires are also under our control (and not touched by 2494 * the guest). 2495 */ 2496 gpa = virt_to_phys(ptep); 2497 TEST_ASSERT_EQ(gpa & ~PAGE_MASK, 0); 2498 /* 2499 * Make sure the guest page table page is mapped with a 4K EPT entry, 2500 * otherwise our level=1 twiddling below will fail. We use the 2501 * identity map (gpa = gpa) since page tables are shared with the host. 2502 */ 2503 install_ept(pml4, gpa, gpa, EPT_PRESENT); 2504 orig_epte = ept_twiddle(gpa, /*mkhuge=*/0, /*level=*/1, 2505 /*clear=*/EPT_PRESENT, /*set=*/ept_access); 2506 2507 if (expect_violation) { 2508 do_ept_violation(/*leaf=*/true, op, 2509 expected_qual | EPT_VLT_LADDR_VLD, gpa); 2510 ept_untwiddle(gpa, /*level=*/1, orig_epte); 2511 do_ept_access_op(op); 2512 } else { 2513 do_ept_access_op(op); 2514 ept_untwiddle(gpa, /*level=*/1, orig_epte); 2515 } 2516 2517 TEST_ASSERT(*ptep & PT_ACCESSED_MASK); 2518 if ((pte_ad & PT_DIRTY_MASK) || op == OP_WRITE) 2519 TEST_ASSERT(*ptep & PT_DIRTY_MASK); 2520 2521 skip_exit_vmcall(); 2522 } 2523 2524 static void ept_access_allowed_paddr(unsigned long ept_access, 2525 unsigned long pte_ad, 2526 enum ept_access_op op) 2527 { 2528 ept_access_paddr(ept_access, pte_ad, op, /*expect_violation=*/false, 2529 /*expected_qual=*/-1); 2530 } 2531 2532 static void ept_access_violation_paddr(unsigned long ept_access, 2533 unsigned long pte_ad, 2534 enum ept_access_op op, 2535 u64 expected_qual) 2536 { 2537 ept_access_paddr(ept_access, pte_ad, op, /*expect_violation=*/true, 2538 expected_qual); 2539 } 2540 2541 2542 static void ept_allowed_at_level_mkhuge(bool mkhuge, int level, 2543 unsigned long clear, 2544 unsigned long set, 2545 enum ept_access_op op) 2546 { 2547 struct ept_access_test_data *data = &ept_access_test_data; 2548 unsigned long orig_pte; 2549 2550 orig_pte = ept_twiddle(data->gpa, mkhuge, level, clear, set); 2551 2552 /* No violation. Should proceed to vmcall. */ 2553 do_ept_access_op(op); 2554 skip_exit_vmcall(); 2555 2556 ept_untwiddle(data->gpa, level, orig_pte); 2557 } 2558 2559 static void ept_allowed_at_level(int level, unsigned long clear, 2560 unsigned long set, enum ept_access_op op) 2561 { 2562 ept_allowed_at_level_mkhuge(false, level, clear, set, op); 2563 if (ept_huge_pages_supported(level)) 2564 ept_allowed_at_level_mkhuge(true, level, clear, set, op); 2565 } 2566 2567 static void ept_allowed(unsigned long clear, unsigned long set, 2568 enum ept_access_op op) 2569 { 2570 ept_allowed_at_level(1, clear, set, op); 2571 ept_allowed_at_level(2, clear, set, op); 2572 ept_allowed_at_level(3, clear, set, op); 2573 ept_allowed_at_level(4, clear, set, op); 2574 } 2575 2576 static void ept_ignored_bit(int bit) 2577 { 2578 /* Set the bit. */ 2579 ept_allowed(0, 1ul << bit, OP_READ); 2580 ept_allowed(0, 1ul << bit, OP_WRITE); 2581 ept_allowed(0, 1ul << bit, OP_EXEC); 2582 2583 /* Clear the bit. */ 2584 ept_allowed(1ul << bit, 0, OP_READ); 2585 ept_allowed(1ul << bit, 0, OP_WRITE); 2586 ept_allowed(1ul << bit, 0, OP_EXEC); 2587 } 2588 2589 static void ept_access_allowed(unsigned long access, enum ept_access_op op) 2590 { 2591 ept_allowed(EPT_PRESENT, access, op); 2592 } 2593 2594 2595 static void ept_misconfig_at_level_mkhuge_op(bool mkhuge, int level, 2596 unsigned long clear, 2597 unsigned long set, 2598 enum ept_access_op op) 2599 { 2600 struct ept_access_test_data *data = &ept_access_test_data; 2601 unsigned long orig_pte; 2602 2603 orig_pte = ept_twiddle(data->gpa, mkhuge, level, clear, set); 2604 2605 do_ept_access_op(op); 2606 assert_exit_reason(VMX_EPT_MISCONFIG); 2607 2608 /* Intel 27.2.1, "For all other VM exits, this field is cleared." */ 2609 #if 0 2610 /* broken: */ 2611 TEST_EXPECT_EQ_MSG(vmcs_read(EXI_QUALIFICATION), 0); 2612 #endif 2613 #if 0 2614 /* 2615 * broken: 2616 * According to description of exit qual for EPT violation, 2617 * EPT_VLT_LADDR_VLD indicates if GUEST_LINEAR_ADDRESS is valid. 2618 * However, I can't find anything that says GUEST_LINEAR_ADDRESS ought 2619 * to be set for msiconfig. 2620 */ 2621 TEST_EXPECT_EQ(vmcs_read(GUEST_LINEAR_ADDRESS), 2622 (unsigned long) ( 2623 op == OP_EXEC ? data->gva + 1 : data->gva)); 2624 #endif 2625 2626 /* Fix the violation and resume the op loop. */ 2627 ept_untwiddle(data->gpa, level, orig_pte); 2628 enter_guest(); 2629 skip_exit_vmcall(); 2630 } 2631 2632 static void ept_misconfig_at_level_mkhuge(bool mkhuge, int level, 2633 unsigned long clear, 2634 unsigned long set) 2635 { 2636 /* The op shouldn't matter (read, write, exec), so try them all! */ 2637 ept_misconfig_at_level_mkhuge_op(mkhuge, level, clear, set, OP_READ); 2638 ept_misconfig_at_level_mkhuge_op(mkhuge, level, clear, set, OP_WRITE); 2639 ept_misconfig_at_level_mkhuge_op(mkhuge, level, clear, set, OP_EXEC); 2640 } 2641 2642 static void ept_misconfig_at_level(int level, unsigned long clear, 2643 unsigned long set) 2644 { 2645 ept_misconfig_at_level_mkhuge(false, level, clear, set); 2646 if (ept_huge_pages_supported(level)) 2647 ept_misconfig_at_level_mkhuge(true, level, clear, set); 2648 } 2649 2650 static void ept_misconfig(unsigned long clear, unsigned long set) 2651 { 2652 ept_misconfig_at_level(1, clear, set); 2653 ept_misconfig_at_level(2, clear, set); 2654 ept_misconfig_at_level(3, clear, set); 2655 ept_misconfig_at_level(4, clear, set); 2656 } 2657 2658 static void ept_access_misconfig(unsigned long access) 2659 { 2660 ept_misconfig(EPT_PRESENT, access); 2661 } 2662 2663 static void ept_reserved_bit_at_level_nohuge(int level, int bit) 2664 { 2665 /* Setting the bit causes a misconfig. */ 2666 ept_misconfig_at_level_mkhuge(false, level, 0, 1ul << bit); 2667 2668 /* Making the entry non-present turns reserved bits into ignored. */ 2669 ept_violation_at_level(level, EPT_PRESENT, 1ul << bit, OP_READ, 2670 EPT_VLT_RD | EPT_VLT_LADDR_VLD | EPT_VLT_PADDR); 2671 } 2672 2673 static void ept_reserved_bit_at_level_huge(int level, int bit) 2674 { 2675 /* Setting the bit causes a misconfig. */ 2676 ept_misconfig_at_level_mkhuge(true, level, 0, 1ul << bit); 2677 2678 /* Making the entry non-present turns reserved bits into ignored. */ 2679 ept_violation_at_level(level, EPT_PRESENT, 1ul << bit, OP_READ, 2680 EPT_VLT_RD | EPT_VLT_LADDR_VLD | EPT_VLT_PADDR); 2681 } 2682 2683 static void ept_reserved_bit_at_level(int level, int bit) 2684 { 2685 /* Setting the bit causes a misconfig. */ 2686 ept_misconfig_at_level(level, 0, 1ul << bit); 2687 2688 /* Making the entry non-present turns reserved bits into ignored. */ 2689 ept_violation_at_level(level, EPT_PRESENT, 1ul << bit, OP_READ, 2690 EPT_VLT_RD | EPT_VLT_LADDR_VLD | EPT_VLT_PADDR); 2691 } 2692 2693 static void ept_reserved_bit(int bit) 2694 { 2695 ept_reserved_bit_at_level(1, bit); 2696 ept_reserved_bit_at_level(2, bit); 2697 ept_reserved_bit_at_level(3, bit); 2698 ept_reserved_bit_at_level(4, bit); 2699 } 2700 2701 #define PAGE_2M_ORDER 9 2702 #define PAGE_1G_ORDER 18 2703 2704 static void *get_1g_page(void) 2705 { 2706 static void *alloc; 2707 2708 if (!alloc) 2709 alloc = alloc_pages(PAGE_1G_ORDER); 2710 return alloc; 2711 } 2712 2713 static void ept_access_test_teardown(void *unused) 2714 { 2715 /* Exit the guest cleanly. */ 2716 do_ept_access_op(OP_EXIT); 2717 } 2718 2719 static void ept_access_test_guest(void) 2720 { 2721 struct ept_access_test_data *data = &ept_access_test_data; 2722 int (*code)(void) = (int (*)(void)) &data->gva[1]; 2723 2724 while (true) { 2725 switch (data->op) { 2726 case OP_READ: 2727 TEST_ASSERT_EQ(*data->gva, MAGIC_VAL_1); 2728 break; 2729 case OP_WRITE: 2730 *data->gva = MAGIC_VAL_2; 2731 TEST_ASSERT_EQ(*data->gva, MAGIC_VAL_2); 2732 *data->gva = MAGIC_VAL_1; 2733 break; 2734 case OP_EXEC: 2735 TEST_ASSERT_EQ(42, code()); 2736 break; 2737 case OP_FLUSH_TLB: 2738 write_cr3(read_cr3()); 2739 break; 2740 case OP_EXIT: 2741 return; 2742 default: 2743 TEST_ASSERT_MSG(false, "Unknown op %d", data->op); 2744 } 2745 vmcall(); 2746 } 2747 } 2748 2749 static void ept_access_test_setup(void) 2750 { 2751 struct ept_access_test_data *data = &ept_access_test_data; 2752 unsigned long npages = 1ul << PAGE_1G_ORDER; 2753 unsigned long size = npages * PAGE_SIZE; 2754 unsigned long *page_table = current_page_table(); 2755 unsigned long pte; 2756 2757 if (setup_ept(false)) 2758 test_skip("EPT not supported"); 2759 2760 /* We use data->gpa = 1 << 39 so that test data has a separate pml4 entry */ 2761 if (cpuid_maxphyaddr() < 40) 2762 test_skip("Test needs MAXPHYADDR >= 40"); 2763 2764 test_set_guest(ept_access_test_guest); 2765 test_add_teardown(ept_access_test_teardown, NULL); 2766 2767 data->hva = get_1g_page(); 2768 TEST_ASSERT(data->hva); 2769 data->hpa = virt_to_phys(data->hva); 2770 2771 data->gpa = 1ul << 39; 2772 data->gva = (void *) ALIGN((unsigned long) alloc_vpages(npages * 2), 2773 size); 2774 TEST_ASSERT(!any_present_pages(page_table, data->gva, size)); 2775 install_pages(page_table, data->gpa, size, data->gva); 2776 2777 /* 2778 * Make sure nothing's mapped here so the tests that screw with the 2779 * pml4 entry don't inadvertently break something. 2780 */ 2781 TEST_ASSERT(get_ept_pte(pml4, data->gpa, 4, &pte) && pte == 0); 2782 TEST_ASSERT(get_ept_pte(pml4, data->gpa + size - 1, 4, &pte) && pte == 0); 2783 install_ept(pml4, data->hpa, data->gpa, EPT_PRESENT); 2784 2785 data->hva[0] = MAGIC_VAL_1; 2786 memcpy(&data->hva[1], &ret42_start, &ret42_end - &ret42_start); 2787 } 2788 2789 static void ept_access_test_not_present(void) 2790 { 2791 ept_access_test_setup(); 2792 /* --- */ 2793 ept_access_violation(0, OP_READ, EPT_VLT_RD); 2794 ept_access_violation(0, OP_WRITE, EPT_VLT_WR); 2795 ept_access_violation(0, OP_EXEC, EPT_VLT_FETCH); 2796 } 2797 2798 static void ept_access_test_read_only(void) 2799 { 2800 ept_access_test_setup(); 2801 2802 /* r-- */ 2803 ept_access_allowed(EPT_RA, OP_READ); 2804 ept_access_violation(EPT_RA, OP_WRITE, EPT_VLT_WR | EPT_VLT_PERM_RD); 2805 ept_access_violation(EPT_RA, OP_EXEC, EPT_VLT_FETCH | EPT_VLT_PERM_RD); 2806 } 2807 2808 static void ept_access_test_write_only(void) 2809 { 2810 ept_access_test_setup(); 2811 /* -w- */ 2812 ept_access_misconfig(EPT_WA); 2813 } 2814 2815 static void ept_access_test_read_write(void) 2816 { 2817 ept_access_test_setup(); 2818 /* rw- */ 2819 ept_access_allowed(EPT_RA | EPT_WA, OP_READ); 2820 ept_access_allowed(EPT_RA | EPT_WA, OP_WRITE); 2821 ept_access_violation(EPT_RA | EPT_WA, OP_EXEC, 2822 EPT_VLT_FETCH | EPT_VLT_PERM_RD | EPT_VLT_PERM_WR); 2823 } 2824 2825 2826 static void ept_access_test_execute_only(void) 2827 { 2828 ept_access_test_setup(); 2829 /* --x */ 2830 if (ept_execute_only_supported()) { 2831 ept_access_violation(EPT_EA, OP_READ, 2832 EPT_VLT_RD | EPT_VLT_PERM_EX); 2833 ept_access_violation(EPT_EA, OP_WRITE, 2834 EPT_VLT_WR | EPT_VLT_PERM_EX); 2835 ept_access_allowed(EPT_EA, OP_EXEC); 2836 } else { 2837 ept_access_misconfig(EPT_EA); 2838 } 2839 } 2840 2841 static void ept_access_test_read_execute(void) 2842 { 2843 ept_access_test_setup(); 2844 /* r-x */ 2845 ept_access_allowed(EPT_RA | EPT_EA, OP_READ); 2846 ept_access_violation(EPT_RA | EPT_EA, OP_WRITE, 2847 EPT_VLT_WR | EPT_VLT_PERM_RD | EPT_VLT_PERM_EX); 2848 ept_access_allowed(EPT_RA | EPT_EA, OP_EXEC); 2849 } 2850 2851 static void ept_access_test_write_execute(void) 2852 { 2853 ept_access_test_setup(); 2854 /* -wx */ 2855 ept_access_misconfig(EPT_WA | EPT_EA); 2856 } 2857 2858 static void ept_access_test_read_write_execute(void) 2859 { 2860 ept_access_test_setup(); 2861 /* rwx */ 2862 ept_access_allowed(EPT_RA | EPT_WA | EPT_EA, OP_READ); 2863 ept_access_allowed(EPT_RA | EPT_WA | EPT_EA, OP_WRITE); 2864 ept_access_allowed(EPT_RA | EPT_WA | EPT_EA, OP_EXEC); 2865 } 2866 2867 static void ept_access_test_reserved_bits(void) 2868 { 2869 int i; 2870 int maxphyaddr; 2871 2872 ept_access_test_setup(); 2873 2874 /* Reserved bits above maxphyaddr. */ 2875 maxphyaddr = cpuid_maxphyaddr(); 2876 for (i = maxphyaddr; i <= 51; i++) { 2877 report_prefix_pushf("reserved_bit=%d", i); 2878 ept_reserved_bit(i); 2879 report_prefix_pop(); 2880 } 2881 2882 /* Level-specific reserved bits. */ 2883 ept_reserved_bit_at_level_nohuge(2, 3); 2884 ept_reserved_bit_at_level_nohuge(2, 4); 2885 ept_reserved_bit_at_level_nohuge(2, 5); 2886 ept_reserved_bit_at_level_nohuge(2, 6); 2887 /* 2M alignment. */ 2888 for (i = 12; i < 20; i++) { 2889 report_prefix_pushf("reserved_bit=%d", i); 2890 ept_reserved_bit_at_level_huge(2, i); 2891 report_prefix_pop(); 2892 } 2893 ept_reserved_bit_at_level_nohuge(3, 3); 2894 ept_reserved_bit_at_level_nohuge(3, 4); 2895 ept_reserved_bit_at_level_nohuge(3, 5); 2896 ept_reserved_bit_at_level_nohuge(3, 6); 2897 /* 1G alignment. */ 2898 for (i = 12; i < 29; i++) { 2899 report_prefix_pushf("reserved_bit=%d", i); 2900 ept_reserved_bit_at_level_huge(3, i); 2901 report_prefix_pop(); 2902 } 2903 ept_reserved_bit_at_level(4, 3); 2904 ept_reserved_bit_at_level(4, 4); 2905 ept_reserved_bit_at_level(4, 5); 2906 ept_reserved_bit_at_level(4, 6); 2907 ept_reserved_bit_at_level(4, 7); 2908 } 2909 2910 static void ept_access_test_ignored_bits(void) 2911 { 2912 ept_access_test_setup(); 2913 /* 2914 * Bits ignored at every level. Bits 8 and 9 (A and D) are ignored as 2915 * far as translation is concerned even if AD bits are enabled in the 2916 * EPTP. Bit 63 is ignored because "EPT-violation #VE" VM-execution 2917 * control is 0. 2918 */ 2919 ept_ignored_bit(8); 2920 ept_ignored_bit(9); 2921 ept_ignored_bit(10); 2922 ept_ignored_bit(11); 2923 ept_ignored_bit(52); 2924 ept_ignored_bit(53); 2925 ept_ignored_bit(54); 2926 ept_ignored_bit(55); 2927 ept_ignored_bit(56); 2928 ept_ignored_bit(57); 2929 ept_ignored_bit(58); 2930 ept_ignored_bit(59); 2931 ept_ignored_bit(60); 2932 ept_ignored_bit(61); 2933 ept_ignored_bit(62); 2934 ept_ignored_bit(63); 2935 } 2936 2937 static void ept_access_test_paddr_not_present_ad_disabled(void) 2938 { 2939 ept_access_test_setup(); 2940 ept_disable_ad_bits(); 2941 2942 ept_access_violation_paddr(0, PT_AD_MASK, OP_READ, EPT_VLT_RD); 2943 ept_access_violation_paddr(0, PT_AD_MASK, OP_WRITE, EPT_VLT_RD); 2944 ept_access_violation_paddr(0, PT_AD_MASK, OP_EXEC, EPT_VLT_RD); 2945 } 2946 2947 static void ept_access_test_paddr_not_present_ad_enabled(void) 2948 { 2949 u64 qual = EPT_VLT_RD | EPT_VLT_WR; 2950 2951 ept_access_test_setup(); 2952 ept_enable_ad_bits_or_skip_test(); 2953 2954 ept_access_violation_paddr(0, PT_AD_MASK, OP_READ, qual); 2955 ept_access_violation_paddr(0, PT_AD_MASK, OP_WRITE, qual); 2956 ept_access_violation_paddr(0, PT_AD_MASK, OP_EXEC, qual); 2957 } 2958 2959 static void ept_access_test_paddr_read_only_ad_disabled(void) 2960 { 2961 /* 2962 * When EPT AD bits are disabled, all accesses to guest paging 2963 * structures are reported separately as a read and (after 2964 * translation of the GPA to host physical address) a read+write 2965 * if the A/D bits have to be set. 2966 */ 2967 u64 qual = EPT_VLT_WR | EPT_VLT_RD | EPT_VLT_PERM_RD; 2968 2969 ept_access_test_setup(); 2970 ept_disable_ad_bits(); 2971 2972 /* Can't update A bit, so all accesses fail. */ 2973 ept_access_violation_paddr(EPT_RA, 0, OP_READ, qual); 2974 ept_access_violation_paddr(EPT_RA, 0, OP_WRITE, qual); 2975 ept_access_violation_paddr(EPT_RA, 0, OP_EXEC, qual); 2976 /* AD bits disabled, so only writes try to update the D bit. */ 2977 ept_access_allowed_paddr(EPT_RA, PT_ACCESSED_MASK, OP_READ); 2978 ept_access_violation_paddr(EPT_RA, PT_ACCESSED_MASK, OP_WRITE, qual); 2979 ept_access_allowed_paddr(EPT_RA, PT_ACCESSED_MASK, OP_EXEC); 2980 /* Both A and D already set, so read-only is OK. */ 2981 ept_access_allowed_paddr(EPT_RA, PT_AD_MASK, OP_READ); 2982 ept_access_allowed_paddr(EPT_RA, PT_AD_MASK, OP_WRITE); 2983 ept_access_allowed_paddr(EPT_RA, PT_AD_MASK, OP_EXEC); 2984 } 2985 2986 static void ept_access_test_paddr_read_only_ad_enabled(void) 2987 { 2988 /* 2989 * When EPT AD bits are enabled, all accesses to guest paging 2990 * structures are considered writes as far as EPT translation 2991 * is concerned. 2992 */ 2993 u64 qual = EPT_VLT_WR | EPT_VLT_RD | EPT_VLT_PERM_RD; 2994 2995 ept_access_test_setup(); 2996 ept_enable_ad_bits_or_skip_test(); 2997 2998 ept_access_violation_paddr(EPT_RA, 0, OP_READ, qual); 2999 ept_access_violation_paddr(EPT_RA, 0, OP_WRITE, qual); 3000 ept_access_violation_paddr(EPT_RA, 0, OP_EXEC, qual); 3001 ept_access_violation_paddr(EPT_RA, PT_ACCESSED_MASK, OP_READ, qual); 3002 ept_access_violation_paddr(EPT_RA, PT_ACCESSED_MASK, OP_WRITE, qual); 3003 ept_access_violation_paddr(EPT_RA, PT_ACCESSED_MASK, OP_EXEC, qual); 3004 ept_access_violation_paddr(EPT_RA, PT_AD_MASK, OP_READ, qual); 3005 ept_access_violation_paddr(EPT_RA, PT_AD_MASK, OP_WRITE, qual); 3006 ept_access_violation_paddr(EPT_RA, PT_AD_MASK, OP_EXEC, qual); 3007 } 3008 3009 static void ept_access_test_paddr_read_write(void) 3010 { 3011 ept_access_test_setup(); 3012 /* Read-write access to paging structure. */ 3013 ept_access_allowed_paddr(EPT_RA | EPT_WA, 0, OP_READ); 3014 ept_access_allowed_paddr(EPT_RA | EPT_WA, 0, OP_WRITE); 3015 ept_access_allowed_paddr(EPT_RA | EPT_WA, 0, OP_EXEC); 3016 } 3017 3018 static void ept_access_test_paddr_read_write_execute(void) 3019 { 3020 ept_access_test_setup(); 3021 /* RWX access to paging structure. */ 3022 ept_access_allowed_paddr(EPT_PRESENT, 0, OP_READ); 3023 ept_access_allowed_paddr(EPT_PRESENT, 0, OP_WRITE); 3024 ept_access_allowed_paddr(EPT_PRESENT, 0, OP_EXEC); 3025 } 3026 3027 static void ept_access_test_paddr_read_execute_ad_disabled(void) 3028 { 3029 /* 3030 * When EPT AD bits are disabled, all accesses to guest paging 3031 * structures are reported separately as a read and (after 3032 * translation of the GPA to host physical address) a read+write 3033 * if the A/D bits have to be set. 3034 */ 3035 u64 qual = EPT_VLT_WR | EPT_VLT_RD | EPT_VLT_PERM_RD | EPT_VLT_PERM_EX; 3036 3037 ept_access_test_setup(); 3038 ept_disable_ad_bits(); 3039 3040 /* Can't update A bit, so all accesses fail. */ 3041 ept_access_violation_paddr(EPT_RA | EPT_EA, 0, OP_READ, qual); 3042 ept_access_violation_paddr(EPT_RA | EPT_EA, 0, OP_WRITE, qual); 3043 ept_access_violation_paddr(EPT_RA | EPT_EA, 0, OP_EXEC, qual); 3044 /* AD bits disabled, so only writes try to update the D bit. */ 3045 ept_access_allowed_paddr(EPT_RA | EPT_EA, PT_ACCESSED_MASK, OP_READ); 3046 ept_access_violation_paddr(EPT_RA | EPT_EA, PT_ACCESSED_MASK, OP_WRITE, qual); 3047 ept_access_allowed_paddr(EPT_RA | EPT_EA, PT_ACCESSED_MASK, OP_EXEC); 3048 /* Both A and D already set, so read-only is OK. */ 3049 ept_access_allowed_paddr(EPT_RA | EPT_EA, PT_AD_MASK, OP_READ); 3050 ept_access_allowed_paddr(EPT_RA | EPT_EA, PT_AD_MASK, OP_WRITE); 3051 ept_access_allowed_paddr(EPT_RA | EPT_EA, PT_AD_MASK, OP_EXEC); 3052 } 3053 3054 static void ept_access_test_paddr_read_execute_ad_enabled(void) 3055 { 3056 /* 3057 * When EPT AD bits are enabled, all accesses to guest paging 3058 * structures are considered writes as far as EPT translation 3059 * is concerned. 3060 */ 3061 u64 qual = EPT_VLT_WR | EPT_VLT_RD | EPT_VLT_PERM_RD | EPT_VLT_PERM_EX; 3062 3063 ept_access_test_setup(); 3064 ept_enable_ad_bits_or_skip_test(); 3065 3066 ept_access_violation_paddr(EPT_RA | EPT_EA, 0, OP_READ, qual); 3067 ept_access_violation_paddr(EPT_RA | EPT_EA, 0, OP_WRITE, qual); 3068 ept_access_violation_paddr(EPT_RA | EPT_EA, 0, OP_EXEC, qual); 3069 ept_access_violation_paddr(EPT_RA | EPT_EA, PT_ACCESSED_MASK, OP_READ, qual); 3070 ept_access_violation_paddr(EPT_RA | EPT_EA, PT_ACCESSED_MASK, OP_WRITE, qual); 3071 ept_access_violation_paddr(EPT_RA | EPT_EA, PT_ACCESSED_MASK, OP_EXEC, qual); 3072 ept_access_violation_paddr(EPT_RA | EPT_EA, PT_AD_MASK, OP_READ, qual); 3073 ept_access_violation_paddr(EPT_RA | EPT_EA, PT_AD_MASK, OP_WRITE, qual); 3074 ept_access_violation_paddr(EPT_RA | EPT_EA, PT_AD_MASK, OP_EXEC, qual); 3075 } 3076 3077 static void ept_access_test_paddr_not_present_page_fault(void) 3078 { 3079 ept_access_test_setup(); 3080 /* 3081 * TODO: test no EPT violation as long as guest PF occurs. e.g., GPA is 3082 * page is read-only in EPT but GVA is also mapped read only in PT. 3083 * Thus guest page fault before host takes EPT violation for trying to 3084 * update A bit. 3085 */ 3086 } 3087 3088 static void ept_access_test_force_2m_page(void) 3089 { 3090 ept_access_test_setup(); 3091 3092 TEST_ASSERT_EQ(ept_2m_supported(), true); 3093 ept_allowed_at_level_mkhuge(true, 2, 0, 0, OP_READ); 3094 ept_violation_at_level_mkhuge(true, 2, EPT_PRESENT, EPT_RA, OP_WRITE, 3095 EPT_VLT_WR | EPT_VLT_PERM_RD | 3096 EPT_VLT_LADDR_VLD | EPT_VLT_PADDR); 3097 ept_misconfig_at_level_mkhuge(true, 2, EPT_PRESENT, EPT_WA); 3098 } 3099 3100 static bool invvpid_valid(u64 type, u64 vpid, u64 gla) 3101 { 3102 u64 msr = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP); 3103 3104 TEST_ASSERT(msr & VPID_CAP_INVVPID); 3105 3106 if (type < INVVPID_ADDR || type > INVVPID_CONTEXT_LOCAL) 3107 return false; 3108 3109 if (!(msr & (1ull << (type + VPID_CAP_INVVPID_TYPES_SHIFT)))) 3110 return false; 3111 3112 if (vpid >> 16) 3113 return false; 3114 3115 if (type != INVVPID_ALL && !vpid) 3116 return false; 3117 3118 if (type == INVVPID_ADDR && !is_canonical(gla)) 3119 return false; 3120 3121 return true; 3122 } 3123 3124 static void try_invvpid(u64 type, u64 vpid, u64 gla) 3125 { 3126 int rc; 3127 bool valid = invvpid_valid(type, vpid, gla); 3128 u64 expected = valid ? VMXERR_UNSUPPORTED_VMCS_COMPONENT 3129 : VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID; 3130 /* 3131 * Set VMX_INST_ERROR to VMXERR_UNVALID_VMCS_COMPONENT, so 3132 * that we can tell if it is updated by INVVPID. 3133 */ 3134 vmcs_read(~0); 3135 rc = invvpid(type, vpid, gla); 3136 report("INVVPID type %ld VPID %lx GLA %lx %s", 3137 !rc == valid, type, vpid, gla, 3138 valid ? "passes" : "fails"); 3139 report("After %s INVVPID, VMX_INST_ERR is %ld (actual %ld)", 3140 vmcs_read(VMX_INST_ERROR) == expected, 3141 rc ? "failed" : "successful", 3142 expected, vmcs_read(VMX_INST_ERROR)); 3143 } 3144 3145 static void ds_invvpid(void *data) 3146 { 3147 u64 msr = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP); 3148 u64 type = ffs(msr >> VPID_CAP_INVVPID_TYPES_SHIFT) - 1; 3149 3150 TEST_ASSERT(type >= INVVPID_ADDR && type <= INVVPID_CONTEXT_LOCAL); 3151 asm volatile("invvpid %0, %1" 3152 : 3153 : "m"(*(struct invvpid_operand *)data), 3154 "r"(type)); 3155 } 3156 3157 /* 3158 * The SS override is ignored in 64-bit mode, so we use an addressing 3159 * mode with %rsp as the base register to generate an implicit SS 3160 * reference. 3161 */ 3162 static void ss_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("sub %%rsp,%0; invvpid (%%rsp,%0,1), %1" 3169 : "+r"(data) 3170 : "r"(type)); 3171 } 3172 3173 static void invvpid_test_gp(void) 3174 { 3175 bool fault; 3176 3177 fault = test_for_exception(GP_VECTOR, &ds_invvpid, 3178 (void *)NONCANONICAL); 3179 report("INVVPID with non-canonical DS operand raises #GP", fault); 3180 } 3181 3182 static void invvpid_test_ss(void) 3183 { 3184 bool fault; 3185 3186 fault = test_for_exception(SS_VECTOR, &ss_invvpid, 3187 (void *)NONCANONICAL); 3188 report("INVVPID with non-canonical SS operand raises #SS", fault); 3189 } 3190 3191 static void invvpid_test_pf(void) 3192 { 3193 void *vpage = alloc_vpage(); 3194 bool fault; 3195 3196 fault = test_for_exception(PF_VECTOR, &ds_invvpid, vpage); 3197 report("INVVPID with unmapped operand raises #PF", fault); 3198 } 3199 3200 static void try_compat_invvpid(void *unused) 3201 { 3202 struct far_pointer32 fp = { 3203 .offset = (uintptr_t)&&invvpid, 3204 .selector = KERNEL_CS32, 3205 }; 3206 register uintptr_t rsp asm("rsp"); 3207 3208 TEST_ASSERT_MSG(fp.offset == (uintptr_t)&&invvpid, 3209 "Code address too high."); 3210 TEST_ASSERT_MSG(rsp == (u32)rsp, "Stack address too high."); 3211 3212 asm goto ("lcall *%0" : : "m" (fp) : "rax" : invvpid); 3213 return; 3214 invvpid: 3215 asm volatile (".code32;" 3216 "invvpid (%eax), %eax;" 3217 "lret;" 3218 ".code64"); 3219 __builtin_unreachable(); 3220 } 3221 3222 static void invvpid_test_compatibility_mode(void) 3223 { 3224 bool fault; 3225 3226 fault = test_for_exception(UD_VECTOR, &try_compat_invvpid, NULL); 3227 report("Compatibility mode INVVPID raises #UD", fault); 3228 } 3229 3230 static void invvpid_test_not_in_vmx_operation(void) 3231 { 3232 bool fault; 3233 3234 TEST_ASSERT(!vmx_off()); 3235 fault = test_for_exception(UD_VECTOR, &ds_invvpid, NULL); 3236 report("INVVPID outside of VMX operation raises #UD", fault); 3237 TEST_ASSERT(!vmx_on()); 3238 } 3239 3240 /* 3241 * This does not test real-address mode, virtual-8086 mode, protected mode, 3242 * or CPL > 0. 3243 */ 3244 static void invvpid_test_v2(void) 3245 { 3246 u64 msr; 3247 int i; 3248 unsigned types = 0; 3249 unsigned type; 3250 3251 if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) || 3252 !(ctrl_cpu_rev[1].clr & CPU_VPID)) 3253 test_skip("VPID not supported"); 3254 3255 msr = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP); 3256 3257 if (!(msr & VPID_CAP_INVVPID)) 3258 test_skip("INVVPID not supported.\n"); 3259 3260 if (msr & VPID_CAP_INVVPID_ADDR) 3261 types |= 1u << INVVPID_ADDR; 3262 if (msr & VPID_CAP_INVVPID_CXTGLB) 3263 types |= 1u << INVVPID_CONTEXT_GLOBAL; 3264 if (msr & VPID_CAP_INVVPID_ALL) 3265 types |= 1u << INVVPID_ALL; 3266 if (msr & VPID_CAP_INVVPID_CXTLOC) 3267 types |= 1u << INVVPID_CONTEXT_LOCAL; 3268 3269 if (!types) 3270 test_skip("No INVVPID types supported.\n"); 3271 3272 for (i = -127; i < 128; i++) 3273 try_invvpid(i, 0xffff, 0); 3274 3275 /* 3276 * VPID must not be more than 16 bits. 3277 */ 3278 for (i = 0; i < 64; i++) 3279 for (type = 0; type < 4; type++) 3280 if (types & (1u << type)) 3281 try_invvpid(type, 1ul << i, 0); 3282 3283 /* 3284 * VPID must not be zero, except for "all contexts." 3285 */ 3286 for (type = 0; type < 4; type++) 3287 if (types & (1u << type)) 3288 try_invvpid(type, 0, 0); 3289 3290 /* 3291 * The gla operand is only validated for single-address INVVPID. 3292 */ 3293 if (types & (1u << INVVPID_ADDR)) 3294 try_invvpid(INVVPID_ADDR, 0xffff, NONCANONICAL); 3295 3296 invvpid_test_gp(); 3297 invvpid_test_ss(); 3298 invvpid_test_pf(); 3299 invvpid_test_compatibility_mode(); 3300 invvpid_test_not_in_vmx_operation(); 3301 } 3302 3303 /* 3304 * Test for early VMLAUNCH failure. Returns true if VMLAUNCH makes it 3305 * at least as far as the guest-state checks. Returns false if the 3306 * VMLAUNCH fails early and execution falls through to the next 3307 * instruction. 3308 */ 3309 static bool vmlaunch_succeeds(void) 3310 { 3311 u32 exit_reason; 3312 3313 /* 3314 * Indirectly set VMX_INST_ERR to 12 ("VMREAD/VMWRITE from/to 3315 * unsupported VMCS component"). The caller can then check 3316 * to see if a failed VM-entry sets VMX_INST_ERR as expected. 3317 */ 3318 vmcs_write(~0u, 0); 3319 3320 vmcs_write(HOST_RIP, (uintptr_t)&&success); 3321 __asm__ __volatile__ goto ("vmwrite %%rsp, %0; vmlaunch" 3322 : 3323 : "r" ((u64)HOST_RSP) 3324 : "cc", "memory" 3325 : success); 3326 return false; 3327 success: 3328 exit_reason = vmcs_read(EXI_REASON); 3329 TEST_ASSERT(exit_reason == (VMX_FAIL_STATE | VMX_ENTRY_FAILURE) || 3330 exit_reason == (VMX_FAIL_MSR | VMX_ENTRY_FAILURE)); 3331 return true; 3332 } 3333 3334 /* 3335 * Try to launch the current VMCS. 3336 */ 3337 static void test_vmx_controls(bool controls_valid, bool xfail) 3338 { 3339 bool success = vmlaunch_succeeds(); 3340 u32 vmx_inst_err; 3341 3342 report_xfail("vmlaunch %s", xfail, success == controls_valid, 3343 controls_valid ? "succeeds" : "fails"); 3344 if (!success) { 3345 vmx_inst_err = vmcs_read(VMX_INST_ERROR); 3346 report("VMX inst error is %d (actual %d)", 3347 vmx_inst_err == VMXERR_ENTRY_INVALID_CONTROL_FIELD, 3348 VMXERR_ENTRY_INVALID_CONTROL_FIELD, vmx_inst_err); 3349 } 3350 } 3351 3352 /* 3353 * Test a particular value of a VM-execution control bit, if the value 3354 * is required or if the value is zero. 3355 */ 3356 static void test_rsvd_ctl_bit_value(const char *name, union vmx_ctrl_msr msr, 3357 enum Encoding encoding, unsigned bit, 3358 unsigned val) 3359 { 3360 u32 mask = 1u << bit; 3361 bool expected; 3362 u32 controls; 3363 3364 if (msr.set & mask) 3365 TEST_ASSERT(msr.clr & mask); 3366 3367 /* 3368 * We can't arbitrarily turn on a control bit, because it may 3369 * introduce dependencies on other VMCS fields. So, we only 3370 * test turning on bits that have a required setting. 3371 */ 3372 if (val && (msr.clr & mask) && !(msr.set & mask)) 3373 return; 3374 3375 report_prefix_pushf("%s %s bit %d", 3376 val ? "Set" : "Clear", name, bit); 3377 3378 controls = vmcs_read(encoding); 3379 if (val) { 3380 vmcs_write(encoding, msr.set | mask); 3381 expected = (msr.clr & mask); 3382 } else { 3383 vmcs_write(encoding, msr.set & ~mask); 3384 expected = !(msr.set & mask); 3385 } 3386 test_vmx_controls(expected, false); 3387 vmcs_write(encoding, controls); 3388 report_prefix_pop(); 3389 } 3390 3391 /* 3392 * Test reserved values of a VM-execution control bit, based on the 3393 * allowed bit settings from the corresponding VMX capability MSR. 3394 */ 3395 static void test_rsvd_ctl_bit(const char *name, union vmx_ctrl_msr msr, 3396 enum Encoding encoding, unsigned bit) 3397 { 3398 test_rsvd_ctl_bit_value(name, msr, encoding, bit, 0); 3399 test_rsvd_ctl_bit_value(name, msr, encoding, bit, 1); 3400 } 3401 3402 /* 3403 * Reserved bits in the pin-based VM-execution controls must be set 3404 * properly. Software may consult the VMX capability MSRs to determine 3405 * the proper settings. 3406 * [Intel SDM] 3407 */ 3408 static void test_pin_based_ctls(void) 3409 { 3410 unsigned bit; 3411 3412 printf("%s: %lx\n", basic.ctrl ? "MSR_IA32_VMX_TRUE_PIN" : 3413 "MSR_IA32_VMX_PINBASED_CTLS", ctrl_pin_rev.val); 3414 for (bit = 0; bit < 32; bit++) 3415 test_rsvd_ctl_bit("pin-based controls", 3416 ctrl_pin_rev, PIN_CONTROLS, bit); 3417 } 3418 3419 /* 3420 * Reserved bits in the primary processor-based VM-execution controls 3421 * must be set properly. Software may consult the VMX capability MSRs 3422 * to determine the proper settings. 3423 * [Intel SDM] 3424 */ 3425 static void test_primary_processor_based_ctls(void) 3426 { 3427 unsigned bit; 3428 3429 printf("\n%s: %lx\n", basic.ctrl ? "MSR_IA32_VMX_TRUE_PROC" : 3430 "MSR_IA32_VMX_PROCBASED_CTLS", ctrl_cpu_rev[0].val); 3431 for (bit = 0; bit < 32; bit++) 3432 test_rsvd_ctl_bit("primary processor-based controls", 3433 ctrl_cpu_rev[0], CPU_EXEC_CTRL0, bit); 3434 } 3435 3436 /* 3437 * If the "activate secondary controls" primary processor-based 3438 * VM-execution control is 1, reserved bits in the secondary 3439 * processor-based VM-execution controls must be cleared. Software may 3440 * consult the VMX capability MSRs to determine which bits are 3441 * reserved. 3442 * If the "activate secondary controls" primary processor-based 3443 * VM-execution control is 0 (or if the processor does not support the 3444 * 1-setting of that control), no checks are performed on the 3445 * secondary processor-based VM-execution controls. 3446 * [Intel SDM] 3447 */ 3448 static void test_secondary_processor_based_ctls(void) 3449 { 3450 u32 primary; 3451 u32 secondary; 3452 unsigned bit; 3453 3454 if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY)) 3455 return; 3456 3457 primary = vmcs_read(CPU_EXEC_CTRL0); 3458 secondary = vmcs_read(CPU_EXEC_CTRL1); 3459 3460 vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY); 3461 printf("\nMSR_IA32_VMX_PROCBASED_CTLS2: %lx\n", ctrl_cpu_rev[1].val); 3462 for (bit = 0; bit < 32; bit++) 3463 test_rsvd_ctl_bit("secondary processor-based controls", 3464 ctrl_cpu_rev[1], CPU_EXEC_CTRL1, bit); 3465 3466 /* 3467 * When the "activate secondary controls" VM-execution control 3468 * is clear, there are no checks on the secondary controls. 3469 */ 3470 vmcs_write(CPU_EXEC_CTRL0, primary & ~CPU_SECONDARY); 3471 vmcs_write(CPU_EXEC_CTRL1, ~0); 3472 report("Secondary processor-based controls ignored", 3473 vmlaunch_succeeds()); 3474 vmcs_write(CPU_EXEC_CTRL1, secondary); 3475 vmcs_write(CPU_EXEC_CTRL0, primary); 3476 } 3477 3478 static void try_cr3_target_count(unsigned i, unsigned max) 3479 { 3480 report_prefix_pushf("CR3 target count 0x%x", i); 3481 vmcs_write(CR3_TARGET_COUNT, i); 3482 test_vmx_controls(i <= max, false); 3483 report_prefix_pop(); 3484 } 3485 3486 /* 3487 * The CR3-target count must not be greater than 4. Future processors 3488 * may support a different number of CR3-target values. Software 3489 * should read the VMX capability MSR IA32_VMX_MISC to determine the 3490 * number of values supported. 3491 * [Intel SDM] 3492 */ 3493 static void test_cr3_targets(void) 3494 { 3495 unsigned supported_targets = (rdmsr(MSR_IA32_VMX_MISC) >> 16) & 0x1ff; 3496 u32 cr3_targets = vmcs_read(CR3_TARGET_COUNT); 3497 unsigned i; 3498 3499 printf("\nSupported CR3 targets: %d\n", supported_targets); 3500 TEST_ASSERT(supported_targets <= 256); 3501 3502 try_cr3_target_count(-1u, supported_targets); 3503 try_cr3_target_count(0x80000000, supported_targets); 3504 try_cr3_target_count(0x7fffffff, supported_targets); 3505 for (i = 0; i <= supported_targets + 1; i++) 3506 try_cr3_target_count(i, supported_targets); 3507 vmcs_write(CR3_TARGET_COUNT, cr3_targets); 3508 } 3509 3510 /* 3511 * Test a particular address setting in the VMCS 3512 */ 3513 static void test_vmcs_addr(const char *name, 3514 enum Encoding encoding, 3515 u64 align, 3516 bool ignored, 3517 bool xfail_beyond_mapped_ram, 3518 u64 addr) 3519 { 3520 bool xfail = 3521 (xfail_beyond_mapped_ram && 3522 addr > fwcfg_get_u64(FW_CFG_RAM_SIZE) - align && 3523 addr < (1ul << cpuid_maxphyaddr())); 3524 3525 report_prefix_pushf("%s = %lx", name, addr); 3526 vmcs_write(encoding, addr); 3527 test_vmx_controls(ignored || (IS_ALIGNED(addr, align) && 3528 addr < (1ul << cpuid_maxphyaddr())), 3529 xfail); 3530 report_prefix_pop(); 3531 xfail = false; 3532 } 3533 3534 /* 3535 * Test interesting values for a VMCS address 3536 */ 3537 static void test_vmcs_addr_values(const char *name, 3538 enum Encoding encoding, 3539 u64 align, 3540 bool ignored, 3541 bool xfail_beyond_mapped_ram, 3542 u32 bit_start, u32 bit_end) 3543 { 3544 unsigned i; 3545 u64 orig_val = vmcs_read(encoding); 3546 3547 for (i = bit_start; i <= bit_end; i++) 3548 test_vmcs_addr(name, encoding, align, ignored, 3549 xfail_beyond_mapped_ram, 1ul << i); 3550 3551 test_vmcs_addr(name, encoding, align, ignored, 3552 xfail_beyond_mapped_ram, PAGE_SIZE - 1); 3553 test_vmcs_addr(name, encoding, align, ignored, 3554 xfail_beyond_mapped_ram, PAGE_SIZE); 3555 test_vmcs_addr(name, encoding, align, ignored, 3556 xfail_beyond_mapped_ram, 3557 (1ul << cpuid_maxphyaddr()) - PAGE_SIZE); 3558 test_vmcs_addr(name, encoding, align, ignored, 3559 xfail_beyond_mapped_ram, -1ul); 3560 3561 vmcs_write(encoding, orig_val); 3562 } 3563 3564 /* 3565 * Test a physical address reference in the VMCS, when the corresponding 3566 * feature is enabled and when the corresponding feature is disabled. 3567 */ 3568 static void test_vmcs_addr_reference(u32 control_bit, enum Encoding field, 3569 const char *field_name, 3570 const char *control_name, u64 align, 3571 bool xfail_beyond_mapped_ram, 3572 bool control_primary) 3573 { 3574 u32 primary = vmcs_read(CPU_EXEC_CTRL0); 3575 u32 secondary = vmcs_read(CPU_EXEC_CTRL1); 3576 u64 page_addr; 3577 3578 if (control_primary) { 3579 if (!(ctrl_cpu_rev[0].clr & control_bit)) 3580 return; 3581 } else { 3582 if (!(ctrl_cpu_rev[1].clr & control_bit)) 3583 return; 3584 } 3585 3586 page_addr = vmcs_read(field); 3587 3588 report_prefix_pushf("%s enabled", control_name); 3589 if (control_primary) { 3590 vmcs_write(CPU_EXEC_CTRL0, primary | control_bit); 3591 } else { 3592 vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY); 3593 vmcs_write(CPU_EXEC_CTRL1, secondary | control_bit); 3594 } 3595 3596 test_vmcs_addr_values(field_name, field, align, false, 3597 xfail_beyond_mapped_ram, 0, 63); 3598 report_prefix_pop(); 3599 3600 report_prefix_pushf("%s disabled", control_name); 3601 if (control_primary) { 3602 vmcs_write(CPU_EXEC_CTRL0, primary & ~control_bit); 3603 } else { 3604 vmcs_write(CPU_EXEC_CTRL0, primary & ~CPU_SECONDARY); 3605 vmcs_write(CPU_EXEC_CTRL1, secondary & ~control_bit); 3606 } 3607 3608 test_vmcs_addr_values(field_name, field, align, true, false, 0, 63); 3609 report_prefix_pop(); 3610 3611 vmcs_write(field, page_addr); 3612 vmcs_write(CPU_EXEC_CTRL0, primary); 3613 } 3614 3615 /* 3616 * If the "use I/O bitmaps" VM-execution control is 1, bits 11:0 of 3617 * each I/O-bitmap address must be 0. Neither address should set any 3618 * bits beyond the processor's physical-address width. 3619 * [Intel SDM] 3620 */ 3621 static void test_io_bitmaps(void) 3622 { 3623 test_vmcs_addr_reference(CPU_IO_BITMAP, IO_BITMAP_A, 3624 "I/O bitmap A", "Use I/O bitmaps", 3625 PAGE_SIZE, false, true); 3626 test_vmcs_addr_reference(CPU_IO_BITMAP, IO_BITMAP_B, 3627 "I/O bitmap B", "Use I/O bitmaps", 3628 PAGE_SIZE, false, true); 3629 } 3630 3631 /* 3632 * If the "use MSR bitmaps" VM-execution control is 1, bits 11:0 of 3633 * the MSR-bitmap address must be 0. The address should not set any 3634 * bits beyond the processor's physical-address width. 3635 * [Intel SDM] 3636 */ 3637 static void test_msr_bitmap(void) 3638 { 3639 test_vmcs_addr_reference(CPU_MSR_BITMAP, MSR_BITMAP, 3640 "MSR bitmap", "Use MSR bitmaps", 3641 PAGE_SIZE, false, true); 3642 } 3643 3644 /* 3645 * If the "use TPR shadow" VM-execution control is 1, the virtual-APIC 3646 * address must satisfy the following checks: 3647 * - Bits 11:0 of the address must be 0. 3648 * - The address should not set any bits beyond the processor's 3649 * physical-address width. 3650 * [Intel SDM] 3651 */ 3652 static void test_apic_virt_addr(void) 3653 { 3654 test_vmcs_addr_reference(CPU_TPR_SHADOW, APIC_VIRT_ADDR, 3655 "virtual-APIC address", "Use TPR shadow", 3656 PAGE_SIZE, true, true); 3657 } 3658 3659 /* 3660 * If the "virtualize APIC-accesses" VM-execution control is 1, the 3661 * APIC-access address must satisfy the following checks: 3662 * - Bits 11:0 of the address must be 0. 3663 * - The address should not set any bits beyond the processor's 3664 * physical-address width. 3665 * [Intel SDM] 3666 */ 3667 static void test_apic_access_addr(void) 3668 { 3669 void *apic_access_page = alloc_page(); 3670 3671 vmcs_write(APIC_ACCS_ADDR, virt_to_phys(apic_access_page)); 3672 3673 test_vmcs_addr_reference(CPU_VIRT_APIC_ACCESSES, APIC_ACCS_ADDR, 3674 "APIC-access address", 3675 "virtualize APIC-accesses", PAGE_SIZE, 3676 false, false); 3677 } 3678 3679 static bool set_bit_pattern(u8 mask, u32 *secondary) 3680 { 3681 u8 i; 3682 bool flag = false; 3683 u32 test_bits[3] = { 3684 CPU_VIRT_X2APIC, 3685 CPU_APIC_REG_VIRT, 3686 CPU_VINTD 3687 }; 3688 3689 for (i = 0; i < ARRAY_SIZE(test_bits); i++) { 3690 if ((mask & (1u << i)) && 3691 (ctrl_cpu_rev[1].clr & test_bits[i])) { 3692 *secondary |= test_bits[i]; 3693 flag = true; 3694 } 3695 } 3696 3697 return (flag); 3698 } 3699 3700 /* 3701 * If the "use TPR shadow" VM-execution control is 0, the following 3702 * VM-execution controls must also be 0: 3703 * - virtualize x2APIC mode 3704 * - APIC-register virtualization 3705 * - virtual-interrupt delivery 3706 * [Intel SDM] 3707 * 3708 * 2. If the "virtualize x2APIC mode" VM-execution control is 1, the 3709 * "virtualize APIC accesses" VM-execution control must be 0. 3710 * [Intel SDM] 3711 */ 3712 static void test_apic_virtual_ctls(void) 3713 { 3714 u32 saved_primary = vmcs_read(CPU_EXEC_CTRL0); 3715 u32 saved_secondary = vmcs_read(CPU_EXEC_CTRL1); 3716 u32 primary = saved_primary; 3717 u32 secondary = saved_secondary; 3718 bool ctrl = false; 3719 char str[10] = "disabled"; 3720 u8 i = 0, j; 3721 3722 /* 3723 * First test 3724 */ 3725 if (!((ctrl_cpu_rev[0].clr & (CPU_SECONDARY | CPU_TPR_SHADOW)) == 3726 (CPU_SECONDARY | CPU_TPR_SHADOW))) 3727 return; 3728 3729 primary |= CPU_SECONDARY; 3730 primary &= ~CPU_TPR_SHADOW; 3731 vmcs_write(CPU_EXEC_CTRL0, primary); 3732 3733 while (1) { 3734 for (j = 1; j < 8; j++) { 3735 secondary &= ~(CPU_VIRT_X2APIC | CPU_APIC_REG_VIRT | CPU_VINTD); 3736 if (primary & CPU_TPR_SHADOW) { 3737 ctrl = true; 3738 } else { 3739 if (! set_bit_pattern(j, &secondary)) 3740 ctrl = true; 3741 else 3742 ctrl = false; 3743 } 3744 3745 vmcs_write(CPU_EXEC_CTRL1, secondary); 3746 report_prefix_pushf("Use TPR shadow %s, virtualize x2APIC mode %s, APIC-register virtualization %s, virtual-interrupt delivery %s", 3747 str, (secondary & CPU_VIRT_X2APIC) ? "enabled" : "disabled", (secondary & CPU_APIC_REG_VIRT) ? "enabled" : "disabled", (secondary & CPU_VINTD) ? "enabled" : "disabled"); 3748 test_vmx_controls(ctrl, false); 3749 report_prefix_pop(); 3750 } 3751 3752 if (i == 1) 3753 break; 3754 i++; 3755 3756 primary |= CPU_TPR_SHADOW; 3757 vmcs_write(CPU_EXEC_CTRL0, primary); 3758 strcpy(str, "enabled"); 3759 } 3760 3761 /* 3762 * Second test 3763 */ 3764 u32 apic_virt_ctls = (CPU_VIRT_X2APIC | CPU_VIRT_APIC_ACCESSES); 3765 3766 primary = saved_primary; 3767 secondary = saved_secondary; 3768 if (!((ctrl_cpu_rev[1].clr & apic_virt_ctls) == apic_virt_ctls)) 3769 return; 3770 3771 vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY); 3772 secondary &= ~CPU_VIRT_APIC_ACCESSES; 3773 vmcs_write(CPU_EXEC_CTRL1, secondary & ~CPU_VIRT_X2APIC); 3774 report_prefix_pushf("Virtualize x2APIC mode disabled; virtualize APIC access disabled"); 3775 test_vmx_controls(true, false); 3776 report_prefix_pop(); 3777 3778 vmcs_write(CPU_EXEC_CTRL1, secondary | CPU_VIRT_APIC_ACCESSES); 3779 report_prefix_pushf("Virtualize x2APIC mode disabled; virtualize APIC access enabled"); 3780 test_vmx_controls(true, false); 3781 report_prefix_pop(); 3782 3783 vmcs_write(CPU_EXEC_CTRL1, secondary | CPU_VIRT_X2APIC); 3784 report_prefix_pushf("Virtualize x2APIC mode enabled; virtualize APIC access enabled"); 3785 test_vmx_controls(false, false); 3786 report_prefix_pop(); 3787 3788 vmcs_write(CPU_EXEC_CTRL1, secondary & ~CPU_VIRT_APIC_ACCESSES); 3789 report_prefix_pushf("Virtualize x2APIC mode enabled; virtualize APIC access disabled"); 3790 test_vmx_controls(true, false); 3791 report_prefix_pop(); 3792 3793 vmcs_write(CPU_EXEC_CTRL0, saved_primary); 3794 vmcs_write(CPU_EXEC_CTRL1, saved_secondary); 3795 } 3796 3797 /* 3798 * If the "virtual-interrupt delivery" VM-execution control is 1, the 3799 * "external-interrupt exiting" VM-execution control must be 1. 3800 * [Intel SDM] 3801 */ 3802 static void test_virtual_intr_ctls(void) 3803 { 3804 u32 saved_primary = vmcs_read(CPU_EXEC_CTRL0); 3805 u32 saved_secondary = vmcs_read(CPU_EXEC_CTRL1); 3806 u32 saved_pin = vmcs_read(PIN_CONTROLS); 3807 u32 primary = saved_primary; 3808 u32 secondary = saved_secondary; 3809 u32 pin = saved_pin; 3810 3811 if (!((ctrl_cpu_rev[1].clr & CPU_VINTD) && 3812 (ctrl_pin_rev.clr & PIN_EXTINT))) 3813 return; 3814 3815 vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY | CPU_TPR_SHADOW); 3816 vmcs_write(CPU_EXEC_CTRL1, secondary & ~CPU_VINTD); 3817 vmcs_write(PIN_CONTROLS, pin & ~PIN_EXTINT); 3818 report_prefix_pushf("Virtualize interrupt-delivery disabled; external-interrupt exiting disabled"); 3819 test_vmx_controls(true, false); 3820 report_prefix_pop(); 3821 3822 vmcs_write(CPU_EXEC_CTRL1, secondary | CPU_VINTD); 3823 report_prefix_pushf("Virtualize interrupt-delivery enabled; external-interrupt exiting disabled"); 3824 test_vmx_controls(false, false); 3825 report_prefix_pop(); 3826 3827 vmcs_write(PIN_CONTROLS, pin | PIN_EXTINT); 3828 report_prefix_pushf("Virtualize interrupt-delivery enabled; external-interrupt exiting enabled"); 3829 test_vmx_controls(true, false); 3830 report_prefix_pop(); 3831 3832 vmcs_write(PIN_CONTROLS, pin & ~PIN_EXTINT); 3833 report_prefix_pushf("Virtualize interrupt-delivery enabled; external-interrupt exiting disabled"); 3834 test_vmx_controls(false, false); 3835 report_prefix_pop(); 3836 3837 vmcs_write(CPU_EXEC_CTRL0, saved_primary); 3838 vmcs_write(CPU_EXEC_CTRL1, saved_secondary); 3839 vmcs_write(PIN_CONTROLS, saved_pin); 3840 } 3841 3842 static void test_pi_desc_addr(u64 addr, bool ctrl) 3843 { 3844 vmcs_write(POSTED_INTR_DESC_ADDR, addr); 3845 report_prefix_pushf("Process-posted-interrupts enabled; posted-interrupt-descriptor-address 0x%lx", addr); 3846 test_vmx_controls(ctrl, false); 3847 report_prefix_pop(); 3848 } 3849 3850 /* 3851 * If the “process posted interrupts†VM-execution control is 1, the 3852 * following must be true: 3853 * 3854 * - The “virtual-interrupt delivery†VM-execution control is 1. 3855 * - The “acknowledge interrupt on exit†VM-exit control is 1. 3856 * - The posted-interrupt notification vector has a value in the 3857 * - range 0–255 (bits 15:8 are all 0). 3858 * - Bits 5:0 of the posted-interrupt descriptor address are all 0. 3859 * - The posted-interrupt descriptor address does not set any bits 3860 * beyond the processor's physical-address width. 3861 * [Intel SDM] 3862 */ 3863 static void test_posted_intr(void) 3864 { 3865 u32 saved_primary = vmcs_read(CPU_EXEC_CTRL0); 3866 u32 saved_secondary = vmcs_read(CPU_EXEC_CTRL1); 3867 u32 saved_pin = vmcs_read(PIN_CONTROLS); 3868 u32 exit_ctl_saved = vmcs_read(EXI_CONTROLS); 3869 u32 primary = saved_primary; 3870 u32 secondary = saved_secondary; 3871 u32 pin = saved_pin; 3872 u32 exit_ctl = exit_ctl_saved; 3873 u16 vec; 3874 int i; 3875 3876 if (!((ctrl_pin_rev.clr & PIN_POST_INTR) && 3877 (ctrl_cpu_rev[1].clr & CPU_VINTD) && 3878 (ctrl_exit_rev.clr & EXI_INTA))) 3879 return; 3880 3881 vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY | CPU_TPR_SHADOW); 3882 3883 /* 3884 * Test virtual-interrupt-delivery and acknowledge-interrupt-on-exit 3885 */ 3886 pin |= PIN_POST_INTR; 3887 vmcs_write(PIN_CONTROLS, pin); 3888 secondary &= ~CPU_VINTD; 3889 vmcs_write(CPU_EXEC_CTRL1, secondary); 3890 report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery disabled"); 3891 test_vmx_controls(false, false); 3892 report_prefix_pop(); 3893 3894 secondary |= CPU_VINTD; 3895 vmcs_write(CPU_EXEC_CTRL1, secondary); 3896 report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery enabled"); 3897 test_vmx_controls(false, false); 3898 report_prefix_pop(); 3899 3900 exit_ctl &= ~EXI_INTA; 3901 vmcs_write(EXI_CONTROLS, exit_ctl); 3902 report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery enabled; acknowledge-interrupt-on-exit disabled"); 3903 test_vmx_controls(false, false); 3904 report_prefix_pop(); 3905 3906 exit_ctl |= EXI_INTA; 3907 vmcs_write(EXI_CONTROLS, exit_ctl); 3908 report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery enabled; acknowledge-interrupt-on-exit enabled"); 3909 test_vmx_controls(true, false); 3910 report_prefix_pop(); 3911 3912 secondary &= ~CPU_VINTD; 3913 vmcs_write(CPU_EXEC_CTRL1, secondary); 3914 report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery disabled; acknowledge-interrupt-on-exit enabled"); 3915 test_vmx_controls(false, false); 3916 report_prefix_pop(); 3917 3918 secondary |= CPU_VINTD; 3919 vmcs_write(CPU_EXEC_CTRL1, secondary); 3920 report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery enabled; acknowledge-interrupt-on-exit enabled"); 3921 test_vmx_controls(true, false); 3922 report_prefix_pop(); 3923 3924 /* 3925 * Test posted-interrupt notification vector 3926 */ 3927 for (i = 0; i < 8; i++) { 3928 vec = (1ul << i); 3929 vmcs_write(PINV, vec); 3930 report_prefix_pushf("Process-posted-interrupts enabled; posted-interrupt-notification-vector %u", vec); 3931 test_vmx_controls(true, false); 3932 report_prefix_pop(); 3933 } 3934 for (i = 8; i < 16; i++) { 3935 vec = (1ul << i); 3936 vmcs_write(PINV, vec); 3937 report_prefix_pushf("Process-posted-interrupts enabled; posted-interrupt-notification-vector %u", vec); 3938 test_vmx_controls(false, false); 3939 report_prefix_pop(); 3940 } 3941 3942 vec &= ~(0xff << 8); 3943 vmcs_write(PINV, vec); 3944 report_prefix_pushf("Process-posted-interrupts enabled; posted-interrupt-notification-vector %u", vec); 3945 test_vmx_controls(true, false); 3946 report_prefix_pop(); 3947 3948 /* 3949 * Test posted-interrupt descriptor addresss 3950 */ 3951 for (i = 0; i < 6; i++) { 3952 test_pi_desc_addr(1ul << i, false); 3953 } 3954 3955 test_pi_desc_addr(0xf0, false); 3956 test_pi_desc_addr(0xff, false); 3957 test_pi_desc_addr(0x0f, false); 3958 test_pi_desc_addr(0x8000, true); 3959 test_pi_desc_addr(0x00, true); 3960 test_pi_desc_addr(0xc000, true); 3961 3962 test_vmcs_addr_values("process-posted interrupts", 3963 POSTED_INTR_DESC_ADDR, 64, 3964 false, false, 0, 63); 3965 3966 vmcs_write(CPU_EXEC_CTRL0, saved_primary); 3967 vmcs_write(CPU_EXEC_CTRL1, saved_secondary); 3968 vmcs_write(PIN_CONTROLS, saved_pin); 3969 } 3970 3971 static void test_apic_ctls(void) 3972 { 3973 test_apic_virt_addr(); 3974 test_apic_access_addr(); 3975 test_apic_virtual_ctls(); 3976 test_virtual_intr_ctls(); 3977 test_posted_intr(); 3978 } 3979 3980 /* 3981 * If the “enable VPID†VM-execution control is 1, the value of the 3982 * of the VPID VM-execution control field must not be 0000H. 3983 * [Intel SDM] 3984 */ 3985 static void test_vpid(void) 3986 { 3987 u32 saved_primary = vmcs_read(CPU_EXEC_CTRL0); 3988 u32 saved_secondary = vmcs_read(CPU_EXEC_CTRL1); 3989 u16 vpid = 0x0000; 3990 int i; 3991 3992 if (!((ctrl_cpu_rev[0].clr & CPU_SECONDARY) && 3993 (ctrl_cpu_rev[1].clr & CPU_VPID))) { 3994 test_skip("Secondary controls and/or VPID not supported"); 3995 return; 3996 } 3997 3998 vmcs_write(CPU_EXEC_CTRL0, saved_primary | CPU_SECONDARY); 3999 vmcs_write(CPU_EXEC_CTRL1, saved_secondary & ~CPU_VPID); 4000 vmcs_write(VPID, vpid); 4001 report_prefix_pushf("VPID disabled; VPID value %x", vpid); 4002 test_vmx_controls(true, false); 4003 report_prefix_pop(); 4004 4005 vmcs_write(CPU_EXEC_CTRL1, saved_secondary | CPU_VPID); 4006 report_prefix_pushf("VPID enabled; VPID value %x", vpid); 4007 test_vmx_controls(false, false); 4008 report_prefix_pop(); 4009 4010 for (i = 0; i < 16; i++) { 4011 vpid = (short)1 << i;; 4012 vmcs_write(VPID, vpid); 4013 report_prefix_pushf("VPID enabled; VPID value %x", vpid); 4014 test_vmx_controls(true, false); 4015 report_prefix_pop(); 4016 } 4017 4018 vmcs_write(CPU_EXEC_CTRL0, saved_primary); 4019 vmcs_write(CPU_EXEC_CTRL1, saved_secondary); 4020 } 4021 4022 static void set_vtpr(unsigned vtpr) 4023 { 4024 *(u32 *)phys_to_virt(vmcs_read(APIC_VIRT_ADDR) + APIC_TASKPRI) = vtpr; 4025 } 4026 4027 static void try_tpr_threshold_and_vtpr(unsigned threshold, unsigned vtpr) 4028 { 4029 bool valid = true; 4030 u32 primary = vmcs_read(CPU_EXEC_CTRL0); 4031 u32 secondary = vmcs_read(CPU_EXEC_CTRL1); 4032 4033 if ((primary & CPU_TPR_SHADOW) && 4034 (!(primary & CPU_SECONDARY) || 4035 !(secondary & (CPU_VINTD | CPU_VIRT_APIC_ACCESSES)))) 4036 valid = (threshold & 0xf) <= ((vtpr >> 4) & 0xf); 4037 4038 set_vtpr(vtpr); 4039 report_prefix_pushf("TPR threshold 0x%x, VTPR.class 0x%x", 4040 threshold, (vtpr >> 4) & 0xf); 4041 test_vmx_controls(valid, false); 4042 report_prefix_pop(); 4043 } 4044 4045 static void test_invalid_event_injection(void) 4046 { 4047 u32 ent_intr_info_save = vmcs_read(ENT_INTR_INFO); 4048 u32 ent_intr_error_save = vmcs_read(ENT_INTR_ERROR); 4049 u32 ent_inst_len_save = vmcs_read(ENT_INST_LEN); 4050 u32 primary_save = vmcs_read(CPU_EXEC_CTRL0); 4051 u32 secondary_save = vmcs_read(CPU_EXEC_CTRL1); 4052 u64 guest_cr0_save = vmcs_read(GUEST_CR0); 4053 u32 ent_intr_info_base = INTR_INFO_VALID_MASK; 4054 u32 ent_intr_info, ent_intr_err, ent_intr_len; 4055 u32 cnt; 4056 4057 /* Setup */ 4058 report_prefix_push("invalid event injection"); 4059 vmcs_write(ENT_INTR_ERROR, 0x00000000); 4060 vmcs_write(ENT_INST_LEN, 0x00000001); 4061 4062 /* The field’s interruption type is not set to a reserved value. */ 4063 ent_intr_info = ent_intr_info_base | INTR_TYPE_RESERVED | DE_VECTOR; 4064 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4065 "RESERVED interruption type invalid [-]", 4066 ent_intr_info); 4067 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4068 test_vmx_controls(false, false); 4069 report_prefix_pop(); 4070 4071 ent_intr_info = ent_intr_info_base | INTR_TYPE_EXT_INTR | 4072 DE_VECTOR; 4073 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4074 "RESERVED interruption type invalid [+]", 4075 ent_intr_info); 4076 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4077 test_vmx_controls(true, false); 4078 report_prefix_pop(); 4079 4080 /* If the interruption type is other event, the vector is 0. */ 4081 ent_intr_info = ent_intr_info_base | INTR_TYPE_OTHER_EVENT | DB_VECTOR; 4082 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4083 "(OTHER EVENT && vector != 0) invalid [-]", 4084 ent_intr_info); 4085 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4086 test_vmx_controls(false, false); 4087 report_prefix_pop(); 4088 4089 /* If the interruption type is NMI, the vector is 2 (negative case). */ 4090 ent_intr_info = ent_intr_info_base | INTR_TYPE_NMI_INTR | DE_VECTOR; 4091 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4092 "(NMI && vector != 2) invalid [-]", ent_intr_info); 4093 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4094 test_vmx_controls(false, false); 4095 report_prefix_pop(); 4096 4097 /* If the interruption type is NMI, the vector is 2 (positive case). */ 4098 ent_intr_info = ent_intr_info_base | INTR_TYPE_NMI_INTR | NMI_VECTOR; 4099 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4100 "(NMI && vector == 2) valid [+]", ent_intr_info); 4101 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4102 test_vmx_controls(true, false); 4103 report_prefix_pop(); 4104 4105 /* 4106 * If the interruption type 4107 * is HW exception, the vector is at most 31. 4108 */ 4109 ent_intr_info = ent_intr_info_base | INTR_TYPE_HARD_EXCEPTION | 0x20; 4110 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4111 "(HW exception && vector > 31) invalid [-]", 4112 ent_intr_info); 4113 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4114 test_vmx_controls(false, false); 4115 report_prefix_pop(); 4116 4117 /* 4118 * deliver-error-code is 1 iff either 4119 * (a) the "unrestricted guest" VM-execution control is 0 4120 * (b) CR0.PE is set. 4121 */ 4122 4123 /* Assert that unrestricted guest is disabled or unsupported */ 4124 assert(!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) || 4125 !(secondary_save & CPU_URG)); 4126 4127 ent_intr_info = ent_intr_info_base | INTR_TYPE_HARD_EXCEPTION | 4128 GP_VECTOR; 4129 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4130 "error code <-> (!URG || prot_mode) [-]", 4131 ent_intr_info); 4132 vmcs_write(GUEST_CR0, guest_cr0_save & ~X86_CR0_PE & ~X86_CR0_PG); 4133 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4134 test_vmx_controls(false, false); 4135 report_prefix_pop(); 4136 4137 ent_intr_info = ent_intr_info_base | INTR_INFO_DELIVER_CODE_MASK | 4138 INTR_TYPE_HARD_EXCEPTION | GP_VECTOR; 4139 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4140 "error code <-> (!URG || prot_mode) [+]", 4141 ent_intr_info); 4142 vmcs_write(GUEST_CR0, guest_cr0_save & ~X86_CR0_PE & ~X86_CR0_PG); 4143 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4144 test_vmx_controls(true, false); 4145 report_prefix_pop(); 4146 4147 if (enable_unrestricted_guest()) 4148 goto skip_unrestricted_guest; 4149 4150 ent_intr_info = ent_intr_info_base | INTR_INFO_DELIVER_CODE_MASK | 4151 INTR_TYPE_HARD_EXCEPTION | GP_VECTOR; 4152 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4153 "error code <-> (!URG || prot_mode) [-]", 4154 ent_intr_info); 4155 vmcs_write(GUEST_CR0, guest_cr0_save & ~X86_CR0_PE & ~X86_CR0_PG); 4156 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4157 test_vmx_controls(false, false); 4158 report_prefix_pop(); 4159 4160 ent_intr_info = ent_intr_info_base | INTR_TYPE_HARD_EXCEPTION | 4161 GP_VECTOR; 4162 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4163 "error code <-> (!URG || prot_mode) [-]", 4164 ent_intr_info); 4165 vmcs_write(GUEST_CR0, guest_cr0_save | X86_CR0_PE); 4166 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4167 test_vmx_controls(false, false); 4168 report_prefix_pop(); 4169 4170 vmcs_write(CPU_EXEC_CTRL1, secondary_save); 4171 vmcs_write(CPU_EXEC_CTRL0, primary_save); 4172 4173 skip_unrestricted_guest: 4174 vmcs_write(GUEST_CR0, guest_cr0_save); 4175 4176 /* deliver-error-code is 1 iff the interruption type is HW exception */ 4177 report_prefix_push("error code <-> HW exception"); 4178 for (cnt = 0; cnt < 8; cnt++) { 4179 u32 exception_type_mask = cnt << 8; 4180 u32 deliver_error_code_mask = 4181 exception_type_mask != INTR_TYPE_HARD_EXCEPTION ? 4182 INTR_INFO_DELIVER_CODE_MASK : 0; 4183 4184 ent_intr_info = ent_intr_info_base | deliver_error_code_mask | 4185 exception_type_mask | GP_VECTOR; 4186 report_prefix_pushf("VM-entry intr info=0x%x [-]", 4187 ent_intr_info); 4188 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4189 test_vmx_controls(false, false); 4190 report_prefix_pop(); 4191 } 4192 report_prefix_pop(); 4193 4194 /* 4195 * deliver-error-code is 1 iff the the vector 4196 * indicates an exception that would normally deliver an error code 4197 */ 4198 report_prefix_push("error code <-> vector delivers error code"); 4199 for (cnt = 0; cnt < 32; cnt++) { 4200 bool has_error_code = false; 4201 u32 deliver_error_code_mask; 4202 4203 switch (cnt) { 4204 case DF_VECTOR: 4205 case TS_VECTOR: 4206 case NP_VECTOR: 4207 case SS_VECTOR: 4208 case GP_VECTOR: 4209 case PF_VECTOR: 4210 case AC_VECTOR: 4211 has_error_code = true; 4212 } 4213 4214 /* Negative case */ 4215 deliver_error_code_mask = has_error_code ? 4216 0 : 4217 INTR_INFO_DELIVER_CODE_MASK; 4218 ent_intr_info = ent_intr_info_base | deliver_error_code_mask | 4219 INTR_TYPE_HARD_EXCEPTION | cnt; 4220 report_prefix_pushf("VM-entry intr info=0x%x [-]", 4221 ent_intr_info); 4222 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4223 test_vmx_controls(false, false); 4224 report_prefix_pop(); 4225 4226 /* Positive case */ 4227 deliver_error_code_mask = has_error_code ? 4228 INTR_INFO_DELIVER_CODE_MASK : 4229 0; 4230 ent_intr_info = ent_intr_info_base | deliver_error_code_mask | 4231 INTR_TYPE_HARD_EXCEPTION | cnt; 4232 report_prefix_pushf("VM-entry intr info=0x%x [+]", 4233 ent_intr_info); 4234 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4235 test_vmx_controls(true, false); 4236 report_prefix_pop(); 4237 } 4238 report_prefix_pop(); 4239 4240 /* Reserved bits in the field (30:12) are 0. */ 4241 report_prefix_push("reserved bits clear"); 4242 for (cnt = 12; cnt <= 30; cnt++) { 4243 ent_intr_info = ent_intr_info_base | 4244 INTR_INFO_DELIVER_CODE_MASK | 4245 INTR_TYPE_HARD_EXCEPTION | GP_VECTOR | 4246 (1U << cnt); 4247 report_prefix_pushf("VM-entry intr info=0x%x [-]", 4248 ent_intr_info); 4249 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4250 test_vmx_controls(false, false); 4251 report_prefix_pop(); 4252 } 4253 report_prefix_pop(); 4254 4255 /* 4256 * If deliver-error-code is 1 4257 * bits 31:15 of the VM-entry exception error-code field are 0. 4258 */ 4259 ent_intr_info = ent_intr_info_base | INTR_INFO_DELIVER_CODE_MASK | 4260 INTR_TYPE_HARD_EXCEPTION | GP_VECTOR; 4261 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4262 "VM-entry exception error code[31:15] clear", 4263 ent_intr_info); 4264 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4265 for (cnt = 15; cnt <= 31; cnt++) { 4266 ent_intr_err = 1U << cnt; 4267 report_prefix_pushf("VM-entry intr error=0x%x [-]", 4268 ent_intr_err); 4269 vmcs_write(ENT_INTR_ERROR, ent_intr_err); 4270 test_vmx_controls(false, false); 4271 report_prefix_pop(); 4272 } 4273 vmcs_write(ENT_INTR_ERROR, 0x00000000); 4274 report_prefix_pop(); 4275 4276 /* 4277 * If the interruption type is software interrupt, software exception, 4278 * or privileged software exception, the VM-entry instruction-length 4279 * field is in the range 0–15. 4280 */ 4281 4282 for (cnt = 0; cnt < 3; cnt++) { 4283 switch (cnt) { 4284 case 0: 4285 ent_intr_info = ent_intr_info_base | 4286 INTR_TYPE_SOFT_INTR; 4287 break; 4288 case 1: 4289 ent_intr_info = ent_intr_info_base | 4290 INTR_TYPE_SOFT_EXCEPTION; 4291 break; 4292 case 2: 4293 ent_intr_info = ent_intr_info_base | 4294 INTR_TYPE_PRIV_SW_EXCEPTION; 4295 break; 4296 } 4297 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4298 "VM-entry instruction-length check", 4299 ent_intr_info); 4300 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4301 4302 /* Instruction length set to -1 (0xFFFFFFFF) should fail */ 4303 ent_intr_len = -1; 4304 report_prefix_pushf("VM-entry intr length = 0x%x [-]", 4305 ent_intr_len); 4306 vmcs_write(ENT_INST_LEN, ent_intr_len); 4307 test_vmx_controls(false, false); 4308 report_prefix_pop(); 4309 4310 /* Instruction length set to 16 should fail */ 4311 ent_intr_len = 0x00000010; 4312 report_prefix_pushf("VM-entry intr length = 0x%x [-]", 4313 ent_intr_len); 4314 vmcs_write(ENT_INST_LEN, 0x00000010); 4315 test_vmx_controls(false, false); 4316 report_prefix_pop(); 4317 4318 report_prefix_pop(); 4319 } 4320 4321 /* Cleanup */ 4322 vmcs_write(ENT_INTR_INFO, ent_intr_info_save); 4323 vmcs_write(ENT_INTR_ERROR, ent_intr_error_save); 4324 vmcs_write(ENT_INST_LEN, ent_inst_len_save); 4325 vmcs_write(CPU_EXEC_CTRL0, primary_save); 4326 vmcs_write(CPU_EXEC_CTRL1, secondary_save); 4327 vmcs_write(GUEST_CR0, guest_cr0_save); 4328 report_prefix_pop(); 4329 } 4330 4331 /* 4332 * Test interesting vTPR values for a given TPR threshold. 4333 */ 4334 static void test_vtpr_values(unsigned threshold) 4335 { 4336 try_tpr_threshold_and_vtpr(threshold, (threshold - 1) << 4); 4337 try_tpr_threshold_and_vtpr(threshold, threshold << 4); 4338 try_tpr_threshold_and_vtpr(threshold, (threshold + 1) << 4); 4339 } 4340 4341 static void try_tpr_threshold(unsigned threshold) 4342 { 4343 bool valid = true; 4344 4345 u32 primary = vmcs_read(CPU_EXEC_CTRL0); 4346 u32 secondary = vmcs_read(CPU_EXEC_CTRL1); 4347 4348 if ((primary & CPU_TPR_SHADOW) && !((primary & CPU_SECONDARY) && 4349 (secondary & CPU_VINTD))) 4350 valid = !(threshold >> 4); 4351 4352 set_vtpr(-1); 4353 vmcs_write(TPR_THRESHOLD, threshold); 4354 report_prefix_pushf("TPR threshold 0x%x, VTPR.class 0xf", threshold); 4355 test_vmx_controls(valid, false); 4356 report_prefix_pop(); 4357 4358 if (valid) 4359 test_vtpr_values(threshold); 4360 } 4361 4362 /* 4363 * Test interesting TPR threshold values. 4364 */ 4365 static void test_tpr_threshold_values(void) 4366 { 4367 unsigned i; 4368 4369 for (i = 0; i < 0x10; i++) 4370 try_tpr_threshold(i); 4371 for (i = 4; i < 32; i++) 4372 try_tpr_threshold(1u << i); 4373 try_tpr_threshold(-1u); 4374 try_tpr_threshold(0x7fffffff); 4375 } 4376 4377 /* 4378 * This test covers the following two VM entry checks: 4379 * 4380 * i) If the "use TPR shadow" VM-execution control is 1 and the 4381 * "virtual-interrupt delivery" VM-execution control is 0, bits 4382 * 31:4 of the TPR threshold VM-execution control field must 4383 be 0. 4384 * [Intel SDM] 4385 * 4386 * ii) If the "use TPR shadow" VM-execution control is 1, the 4387 * "virtual-interrupt delivery" VM-execution control is 0 4388 * and the "virtualize APIC accesses" VM-execution control 4389 * is 0, the value of bits 3:0 of the TPR threshold VM-execution 4390 * control field must not be greater than the value of bits 4391 * 7:4 of VTPR. 4392 * [Intel SDM] 4393 */ 4394 static void test_tpr_threshold(void) 4395 { 4396 u32 primary = vmcs_read(CPU_EXEC_CTRL0); 4397 void *virtual_apic_page; 4398 4399 if (!(ctrl_cpu_rev[0].clr & CPU_TPR_SHADOW)) 4400 return; 4401 4402 virtual_apic_page = alloc_page(); 4403 memset(virtual_apic_page, 0xff, PAGE_SIZE); 4404 vmcs_write(APIC_VIRT_ADDR, virt_to_phys(virtual_apic_page)); 4405 4406 vmcs_write(CPU_EXEC_CTRL0, primary & ~(CPU_TPR_SHADOW | CPU_SECONDARY)); 4407 report_prefix_pushf("Use TPR shadow disabled, secondary controls disabled"); 4408 test_tpr_threshold_values(); 4409 report_prefix_pop(); 4410 vmcs_write(CPU_EXEC_CTRL0, vmcs_read(CPU_EXEC_CTRL0) | CPU_TPR_SHADOW); 4411 report_prefix_pushf("Use TPR shadow enabled, secondary controls disabled"); 4412 test_tpr_threshold_values(); 4413 report_prefix_pop(); 4414 4415 if (!((ctrl_cpu_rev[0].clr & CPU_SECONDARY) && 4416 (ctrl_cpu_rev[1].clr & (CPU_VINTD | CPU_VIRT_APIC_ACCESSES)))) { 4417 vmcs_write(CPU_EXEC_CTRL0, primary); 4418 return; 4419 } 4420 4421 u32 secondary = vmcs_read(CPU_EXEC_CTRL1); 4422 4423 if (ctrl_cpu_rev[1].clr & CPU_VINTD) { 4424 vmcs_write(CPU_EXEC_CTRL1, CPU_VINTD); 4425 report_prefix_pushf("Use TPR shadow enabled; secondary controls disabled; virtual-interrupt delivery enabled; virtualize APIC accesses disabled"); 4426 test_tpr_threshold_values(); 4427 report_prefix_pop(); 4428 4429 vmcs_write(CPU_EXEC_CTRL0, 4430 vmcs_read(CPU_EXEC_CTRL0) | CPU_SECONDARY); 4431 report_prefix_pushf("Use TPR shadow enabled; secondary controls enabled; virtual-interrupt delivery enabled; virtualize APIC accesses disabled"); 4432 test_tpr_threshold_values(); 4433 report_prefix_pop(); 4434 } 4435 4436 if (ctrl_cpu_rev[1].clr & CPU_VIRT_APIC_ACCESSES) { 4437 vmcs_write(CPU_EXEC_CTRL0, 4438 vmcs_read(CPU_EXEC_CTRL0) & ~CPU_SECONDARY); 4439 vmcs_write(CPU_EXEC_CTRL1, CPU_VIRT_APIC_ACCESSES); 4440 report_prefix_pushf("Use TPR shadow enabled; secondary controls disabled; virtual-interrupt delivery enabled; virtualize APIC accesses enabled"); 4441 test_tpr_threshold_values(); 4442 report_prefix_pop(); 4443 4444 vmcs_write(CPU_EXEC_CTRL0, 4445 vmcs_read(CPU_EXEC_CTRL0) | CPU_SECONDARY); 4446 report_prefix_pushf("Use TPR shadow enabled; secondary controls enabled; virtual-interrupt delivery enabled; virtualize APIC accesses enabled"); 4447 test_tpr_threshold_values(); 4448 report_prefix_pop(); 4449 } 4450 4451 if ((ctrl_cpu_rev[1].clr & 4452 (CPU_VINTD | CPU_VIRT_APIC_ACCESSES)) == 4453 (CPU_VINTD | CPU_VIRT_APIC_ACCESSES)) { 4454 vmcs_write(CPU_EXEC_CTRL0, 4455 vmcs_read(CPU_EXEC_CTRL0) & ~CPU_SECONDARY); 4456 vmcs_write(CPU_EXEC_CTRL1, 4457 CPU_VINTD | CPU_VIRT_APIC_ACCESSES); 4458 report_prefix_pushf("Use TPR shadow enabled; secondary controls disabled; virtual-interrupt delivery enabled; virtualize APIC accesses enabled"); 4459 test_tpr_threshold_values(); 4460 report_prefix_pop(); 4461 4462 vmcs_write(CPU_EXEC_CTRL0, 4463 vmcs_read(CPU_EXEC_CTRL0) | CPU_SECONDARY); 4464 report_prefix_pushf("Use TPR shadow enabled; secondary controls enabled; virtual-interrupt delivery enabled; virtualize APIC accesses enabled"); 4465 test_tpr_threshold_values(); 4466 report_prefix_pop(); 4467 } 4468 4469 vmcs_write(CPU_EXEC_CTRL1, secondary); 4470 vmcs_write(CPU_EXEC_CTRL0, primary); 4471 } 4472 4473 /* 4474 * This test verifies the following two vmentry checks: 4475 * 4476 * If the "NMI exiting" VM-execution control is 0, "Virtual NMIs" 4477 * VM-execution control must be 0. 4478 * [Intel SDM] 4479 * 4480 * If the “virtual NMIs” VM-execution control is 0, the “NMI-window 4481 * exiting” VM-execution control must be 0. 4482 * [Intel SDM] 4483 */ 4484 static void test_nmi_ctrls(void) 4485 { 4486 u32 pin_ctrls, cpu_ctrls0, test_pin_ctrls, test_cpu_ctrls0; 4487 4488 if ((ctrl_pin_rev.clr & (PIN_NMI | PIN_VIRT_NMI)) != 4489 (PIN_NMI | PIN_VIRT_NMI)) { 4490 test_skip("NMI exiting and Virtual NMIs are not supported !"); 4491 return; 4492 } 4493 4494 /* Save the controls so that we can restore them after our tests */ 4495 pin_ctrls = vmcs_read(PIN_CONTROLS); 4496 cpu_ctrls0 = vmcs_read(CPU_EXEC_CTRL0); 4497 4498 test_pin_ctrls = pin_ctrls & ~(PIN_NMI | PIN_VIRT_NMI); 4499 test_cpu_ctrls0 = cpu_ctrls0 & ~CPU_NMI_WINDOW; 4500 4501 vmcs_write(PIN_CONTROLS, test_pin_ctrls); 4502 report_prefix_pushf("NMI-exiting disabled, virtual-NMIs disabled"); 4503 test_vmx_controls(true, false); 4504 report_prefix_pop(); 4505 4506 vmcs_write(PIN_CONTROLS, test_pin_ctrls | PIN_VIRT_NMI); 4507 report_prefix_pushf("NMI-exiting disabled, virtual-NMIs enabled"); 4508 test_vmx_controls(false, false); 4509 report_prefix_pop(); 4510 4511 vmcs_write(PIN_CONTROLS, test_pin_ctrls | (PIN_NMI | PIN_VIRT_NMI)); 4512 report_prefix_pushf("NMI-exiting enabled, virtual-NMIs enabled"); 4513 test_vmx_controls(true, false); 4514 report_prefix_pop(); 4515 4516 vmcs_write(PIN_CONTROLS, test_pin_ctrls | PIN_NMI); 4517 report_prefix_pushf("NMI-exiting enabled, virtual-NMIs disabled"); 4518 test_vmx_controls(true, false); 4519 report_prefix_pop(); 4520 4521 if (!(ctrl_cpu_rev[0].clr & CPU_NMI_WINDOW)) { 4522 report_info("NMI-window exiting is not supported, skipping..."); 4523 goto done; 4524 } 4525 4526 vmcs_write(PIN_CONTROLS, test_pin_ctrls); 4527 vmcs_write(CPU_EXEC_CTRL0, test_cpu_ctrls0 | CPU_NMI_WINDOW); 4528 report_prefix_pushf("Virtual-NMIs disabled, NMI-window-exiting enabled"); 4529 test_vmx_controls(false, false); 4530 report_prefix_pop(); 4531 4532 vmcs_write(PIN_CONTROLS, test_pin_ctrls); 4533 vmcs_write(CPU_EXEC_CTRL0, test_cpu_ctrls0); 4534 report_prefix_pushf("Virtual-NMIs disabled, NMI-window-exiting disabled"); 4535 test_vmx_controls(true, false); 4536 report_prefix_pop(); 4537 4538 vmcs_write(PIN_CONTROLS, test_pin_ctrls | (PIN_NMI | PIN_VIRT_NMI)); 4539 vmcs_write(CPU_EXEC_CTRL0, test_cpu_ctrls0 | CPU_NMI_WINDOW); 4540 report_prefix_pushf("Virtual-NMIs enabled, NMI-window-exiting enabled"); 4541 test_vmx_controls(true, false); 4542 report_prefix_pop(); 4543 4544 vmcs_write(PIN_CONTROLS, test_pin_ctrls | (PIN_NMI | PIN_VIRT_NMI)); 4545 vmcs_write(CPU_EXEC_CTRL0, test_cpu_ctrls0); 4546 report_prefix_pushf("Virtual-NMIs enabled, NMI-window-exiting disabled"); 4547 test_vmx_controls(true, false); 4548 report_prefix_pop(); 4549 4550 /* Restore the controls to their original values */ 4551 vmcs_write(CPU_EXEC_CTRL0, cpu_ctrls0); 4552 done: 4553 vmcs_write(PIN_CONTROLS, pin_ctrls); 4554 } 4555 4556 static void test_eptp_ad_bit(u64 eptp, bool ctrl) 4557 { 4558 vmcs_write(EPTP, eptp); 4559 report_prefix_pushf("Enable-EPT enabled; EPT accessed and dirty flag %s", 4560 (eptp & EPTP_AD_FLAG) ? "1": "0"); 4561 test_vmx_controls(ctrl, false); 4562 report_prefix_pop(); 4563 4564 } 4565 4566 /* 4567 * 1. If the "enable EPT" VM-execution control is 1, the "EPTP VM-execution" 4568 * control field must satisfy the following checks: 4569 * 4570 * - The EPT memory type (bits 2:0) must be a value supported by the 4571 * processor as indicated in the IA32_VMX_EPT_VPID_CAP MSR. 4572 * - Bits 5:3 (1 less than the EPT page-walk length) must be 3, 4573 * indicating an EPT page-walk length of 4. 4574 * - Bit 6 (enable bit for accessed and dirty flags for EPT) must be 4575 * 0 if bit 21 of the IA32_VMX_EPT_VPID_CAP MSR is read as 0, 4576 * indicating that the processor does not support accessed and dirty 4577 * dirty flags for EPT. 4578 * - Reserved bits 11:7 and 63:N (where N is the processor's 4579 * physical-address width) must all be 0. 4580 * 4581 * 2. If the "unrestricted guest" VM-execution control is 1, the 4582 * "enable EPT" VM-execution control must also be 1. 4583 */ 4584 static void test_ept_eptp(void) 4585 { 4586 u32 primary_saved = vmcs_read(CPU_EXEC_CTRL0); 4587 u32 secondary_saved = vmcs_read(CPU_EXEC_CTRL1); 4588 u64 eptp_saved = vmcs_read(EPTP); 4589 u32 primary = primary_saved; 4590 u32 secondary = secondary_saved; 4591 u64 msr, eptp = eptp_saved; 4592 bool un_cache = false; 4593 bool wr_bk = false; 4594 bool ctrl; 4595 u32 i, maxphysaddr; 4596 u64 j, resv_bits_mask = 0; 4597 4598 if (!((ctrl_cpu_rev[0].clr & CPU_SECONDARY) && 4599 (ctrl_cpu_rev[1].clr & CPU_EPT))) { 4600 test_skip("\"CPU secondary\" and/or \"enable EPT\" execution controls are not supported !"); 4601 return; 4602 } 4603 4604 /* 4605 * Memory type (bits 2:0) 4606 */ 4607 msr = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP); 4608 if (msr & EPT_CAP_UC) 4609 un_cache = true; 4610 if (msr & EPT_CAP_WB) 4611 wr_bk = true; 4612 4613 primary |= CPU_SECONDARY; 4614 vmcs_write(CPU_EXEC_CTRL0, primary); 4615 secondary |= CPU_EPT; 4616 vmcs_write(CPU_EXEC_CTRL1, secondary); 4617 eptp = (eptp & ~EPTP_PG_WALK_LEN_MASK) | 4618 (3ul << EPTP_PG_WALK_LEN_SHIFT); 4619 vmcs_write(EPTP, eptp); 4620 4621 for (i = 0; i < 8; i++) { 4622 if (i == 0) { 4623 if (un_cache) { 4624 report_info("EPT paging structure memory-type is Un-cacheable\n"); 4625 ctrl = true; 4626 } else { 4627 ctrl = false; 4628 } 4629 } else if (i == 6) { 4630 if (wr_bk) { 4631 report_info("EPT paging structure memory-type is Write-back\n"); 4632 ctrl = true; 4633 } else { 4634 ctrl = false; 4635 } 4636 } else { 4637 ctrl = false; 4638 } 4639 4640 eptp = (eptp & ~EPT_MEM_TYPE_MASK) | i; 4641 vmcs_write(EPTP, eptp); 4642 report_prefix_pushf("Enable-EPT enabled; EPT memory type %lu", 4643 eptp & EPT_MEM_TYPE_MASK); 4644 test_vmx_controls(ctrl, false); 4645 report_prefix_pop(); 4646 } 4647 4648 eptp = (eptp & ~EPT_MEM_TYPE_MASK) | 6ul; 4649 4650 /* 4651 * Page walk length (bits 5:3) 4652 */ 4653 for (i = 0; i < 8; i++) { 4654 eptp = (eptp & ~EPTP_PG_WALK_LEN_MASK) | 4655 (i << EPTP_PG_WALK_LEN_SHIFT); 4656 if (i == 3) 4657 ctrl = true; 4658 else 4659 ctrl = false; 4660 4661 vmcs_write(EPTP, eptp); 4662 report_prefix_pushf("Enable-EPT enabled; EPT page walk length %lu", 4663 eptp & EPTP_PG_WALK_LEN_MASK); 4664 test_vmx_controls(ctrl, false); 4665 report_prefix_pop(); 4666 } 4667 4668 eptp = (eptp & ~EPTP_PG_WALK_LEN_MASK) | 4669 3ul << EPTP_PG_WALK_LEN_SHIFT; 4670 4671 /* 4672 * Accessed and dirty flag (bit 6) 4673 */ 4674 if (msr & EPT_CAP_AD_FLAG) { 4675 report_info("Processor supports accessed and dirty flag"); 4676 eptp &= ~EPTP_AD_FLAG; 4677 test_eptp_ad_bit(eptp, true); 4678 4679 eptp |= EPTP_AD_FLAG; 4680 test_eptp_ad_bit(eptp, true); 4681 } else { 4682 report_info("Processor does not supports accessed and dirty flag"); 4683 eptp &= ~EPTP_AD_FLAG; 4684 test_eptp_ad_bit(eptp, true); 4685 4686 eptp |= EPTP_AD_FLAG; 4687 test_eptp_ad_bit(eptp, false); 4688 } 4689 4690 /* 4691 * Reserved bits [11:7] and [63:N] 4692 */ 4693 for (i = 0; i < 32; i++) { 4694 if (i == 0) 4695 ctrl = true; 4696 else 4697 ctrl = false; 4698 4699 eptp = (eptp & 4700 ~(EPTP_RESERV_BITS_MASK << EPTP_RESERV_BITS_SHIFT)) | 4701 (i << EPTP_RESERV_BITS_SHIFT); 4702 vmcs_write(EPTP, eptp); 4703 report_prefix_pushf("Enable-EPT enabled; reserved bits [11:7] %lu", 4704 (eptp >> EPTP_RESERV_BITS_SHIFT) & 4705 EPTP_RESERV_BITS_MASK); 4706 test_vmx_controls(ctrl, false); 4707 report_prefix_pop(); 4708 } 4709 4710 eptp = (eptp & ~(EPTP_RESERV_BITS_MASK << EPTP_RESERV_BITS_SHIFT)); 4711 4712 maxphysaddr = cpuid_maxphyaddr(); 4713 for (i = 0; i < (63 - maxphysaddr + 1); i++) { 4714 resv_bits_mask |= 1ul << i; 4715 } 4716 4717 for (j = 0; j < (63 - maxphysaddr + 1); j++) { 4718 if (j == 0) 4719 ctrl = true; 4720 else 4721 ctrl = false; 4722 4723 eptp = (eptp & ~(resv_bits_mask << maxphysaddr)) | 4724 (j << maxphysaddr); 4725 vmcs_write(EPTP, eptp); 4726 report_prefix_pushf("Enable-EPT enabled; reserved bits [63:N] %lu", 4727 (eptp >> maxphysaddr) & resv_bits_mask); 4728 test_vmx_controls(ctrl, false); 4729 report_prefix_pop(); 4730 } 4731 4732 secondary &= ~(CPU_EPT | CPU_URG); 4733 vmcs_write(CPU_EXEC_CTRL1, secondary); 4734 report_prefix_pushf("Enable-EPT disabled, unrestricted-guest disabled"); 4735 test_vmx_controls(true, false); 4736 report_prefix_pop(); 4737 4738 secondary |= CPU_URG; 4739 vmcs_write(CPU_EXEC_CTRL1, secondary); 4740 report_prefix_pushf("Enable-EPT disabled, unrestricted-guest enabled"); 4741 test_vmx_controls(false, false); 4742 report_prefix_pop(); 4743 4744 secondary |= CPU_EPT; 4745 setup_dummy_ept(); 4746 report_prefix_pushf("Enable-EPT enabled, unrestricted-guest enabled"); 4747 test_vmx_controls(true, false); 4748 report_prefix_pop(); 4749 4750 secondary &= ~CPU_URG; 4751 vmcs_write(CPU_EXEC_CTRL1, secondary); 4752 report_prefix_pushf("Enable-EPT enabled, unrestricted-guest disabled"); 4753 test_vmx_controls(true, false); 4754 report_prefix_pop(); 4755 4756 vmcs_write(CPU_EXEC_CTRL0, primary_saved); 4757 vmcs_write(CPU_EXEC_CTRL1, secondary_saved); 4758 vmcs_write(EPTP, eptp_saved); 4759 } 4760 4761 /* 4762 * If the 'enable PML' VM-execution control is 1, the 'enable EPT' 4763 * VM-execution control must also be 1. In addition, the PML address 4764 * must satisfy the following checks: 4765 * 4766 * * Bits 11:0 of the address must be 0. 4767 * * The address should not set any bits beyond the processor's 4768 * physical-address width. 4769 * 4770 * [Intel SDM] 4771 */ 4772 static void test_pml(void) 4773 { 4774 u32 primary_saved = vmcs_read(CPU_EXEC_CTRL0); 4775 u32 secondary_saved = vmcs_read(CPU_EXEC_CTRL1); 4776 u32 primary = primary_saved; 4777 u32 secondary = secondary_saved; 4778 4779 if (!((ctrl_cpu_rev[0].clr & CPU_SECONDARY) && 4780 (ctrl_cpu_rev[1].clr & CPU_EPT) && (ctrl_cpu_rev[1].clr & CPU_PML))) { 4781 test_skip("\"Secondary execution\" control or \"enable EPT\" control or \"enable PML\" control is not supported !"); 4782 return; 4783 } 4784 4785 primary |= CPU_SECONDARY; 4786 vmcs_write(CPU_EXEC_CTRL0, primary); 4787 secondary &= ~(CPU_PML | CPU_EPT); 4788 vmcs_write(CPU_EXEC_CTRL1, secondary); 4789 report_prefix_pushf("enable-PML disabled, enable-EPT disabled"); 4790 test_vmx_controls(true, false); 4791 report_prefix_pop(); 4792 4793 secondary |= CPU_PML; 4794 vmcs_write(CPU_EXEC_CTRL1, secondary); 4795 report_prefix_pushf("enable-PML enabled, enable-EPT disabled"); 4796 test_vmx_controls(false, false); 4797 report_prefix_pop(); 4798 4799 secondary |= CPU_EPT; 4800 setup_dummy_ept(); 4801 report_prefix_pushf("enable-PML enabled, enable-EPT enabled"); 4802 test_vmx_controls(true, false); 4803 report_prefix_pop(); 4804 4805 secondary &= ~CPU_PML; 4806 vmcs_write(CPU_EXEC_CTRL1, secondary); 4807 report_prefix_pushf("enable-PML disabled, enable EPT enabled"); 4808 test_vmx_controls(true, false); 4809 report_prefix_pop(); 4810 4811 test_vmcs_addr_reference(CPU_PML, PMLADDR, "PML address", "PML", 4812 PAGE_SIZE, false, false); 4813 4814 vmcs_write(CPU_EXEC_CTRL0, primary_saved); 4815 vmcs_write(CPU_EXEC_CTRL1, secondary_saved); 4816 } 4817 4818 /* 4819 * If the "activate VMX-preemption timer" VM-execution control is 0, the 4820 * the "save VMX-preemption timer value" VM-exit control must also be 0. 4821 * 4822 * [Intel SDM] 4823 */ 4824 static void test_vmx_preemption_timer(void) 4825 { 4826 u32 saved_pin = vmcs_read(PIN_CONTROLS); 4827 u32 saved_exit = vmcs_read(EXI_CONTROLS); 4828 u32 pin = saved_pin; 4829 u32 exit = saved_exit; 4830 4831 if (!((ctrl_exit_rev.clr & EXI_SAVE_PREEMPT) || 4832 (ctrl_pin_rev.clr & PIN_PREEMPT))) { 4833 printf("\"Save-VMX-preemption-timer\" control and/or \"Enable-VMX-preemption-timer\" control is not supported\n"); 4834 return; 4835 } 4836 4837 pin |= PIN_PREEMPT; 4838 vmcs_write(PIN_CONTROLS, pin); 4839 exit &= ~EXI_SAVE_PREEMPT; 4840 vmcs_write(EXI_CONTROLS, exit); 4841 report_prefix_pushf("enable-VMX-preemption-timer enabled, save-VMX-preemption-timer disabled"); 4842 test_vmx_controls(true, false); 4843 report_prefix_pop(); 4844 4845 exit |= EXI_SAVE_PREEMPT; 4846 vmcs_write(EXI_CONTROLS, exit); 4847 report_prefix_pushf("enable-VMX-preemption-timer enabled, save-VMX-preemption-timer enabled"); 4848 test_vmx_controls(true, false); 4849 report_prefix_pop(); 4850 4851 pin &= ~PIN_PREEMPT; 4852 vmcs_write(PIN_CONTROLS, pin); 4853 report_prefix_pushf("enable-VMX-preemption-timer disabled, save-VMX-preemption-timer enabled"); 4854 test_vmx_controls(false, false); 4855 report_prefix_pop(); 4856 4857 exit &= ~EXI_SAVE_PREEMPT; 4858 vmcs_write(EXI_CONTROLS, exit); 4859 report_prefix_pushf("enable-VMX-preemption-timer disabled, save-VMX-preemption-timer disabled"); 4860 test_vmx_controls(true, false); 4861 report_prefix_pop(); 4862 4863 vmcs_write(PIN_CONTROLS, saved_pin); 4864 vmcs_write(EXI_CONTROLS, saved_exit); 4865 } 4866 4867 /* 4868 * Tests for VM-execution control fields 4869 */ 4870 static void test_vm_execution_ctls(void) 4871 { 4872 test_pin_based_ctls(); 4873 test_primary_processor_based_ctls(); 4874 test_secondary_processor_based_ctls(); 4875 test_cr3_targets(); 4876 test_io_bitmaps(); 4877 test_msr_bitmap(); 4878 test_apic_ctls(); 4879 test_tpr_threshold(); 4880 test_nmi_ctrls(); 4881 test_pml(); 4882 test_vpid(); 4883 test_ept_eptp(); 4884 test_vmx_preemption_timer(); 4885 } 4886 4887 /* 4888 * The following checks are performed for the VM-entry MSR-load address if 4889 * the VM-entry MSR-load count field is non-zero: 4890 * 4891 * - The lower 4 bits of the VM-entry MSR-load address must be 0. 4892 * The address should not set any bits beyond the processor’s 4893 * physical-address width. 4894 * 4895 * - The address of the last byte in the VM-entry MSR-load area 4896 * should not set any bits beyond the processor’s physical-address 4897 * width. The address of this last byte is VM-entry MSR-load address 4898 * + (MSR count * 16) - 1. (The arithmetic used for the computation 4899 * uses more bits than the processor’s physical-address width.) 4900 * 4901 * 4902 * [Intel SDM] 4903 */ 4904 static void test_entry_msr_load(void) 4905 { 4906 entry_msr_load = alloc_page(); 4907 u64 tmp; 4908 u32 entry_msr_ld_cnt = 1; 4909 int i; 4910 u32 addr_len = 64; 4911 4912 vmcs_write(ENT_MSR_LD_CNT, entry_msr_ld_cnt); 4913 4914 /* Check first 4 bits of VM-entry MSR-load address */ 4915 for (i = 0; i < 4; i++) { 4916 tmp = (u64)entry_msr_load | 1ull << i; 4917 vmcs_write(ENTER_MSR_LD_ADDR, tmp); 4918 report_prefix_pushf("VM-entry MSR-load addr [4:0] %lx", 4919 tmp & 0xf); 4920 test_vmx_controls(false, false); 4921 report_prefix_pop(); 4922 } 4923 4924 if (basic.val & (1ul << 48)) 4925 addr_len = 32; 4926 4927 test_vmcs_addr_values("VM-entry-MSR-load address", 4928 ENTER_MSR_LD_ADDR, 16, false, false, 4929 4, addr_len - 1); 4930 4931 /* 4932 * Check last byte of VM-entry MSR-load address 4933 */ 4934 entry_msr_load = (struct vmx_msr_entry *)((u64)entry_msr_load & ~0xf); 4935 4936 for (i = (addr_len == 64 ? cpuid_maxphyaddr(): addr_len); 4937 i < 64; i++) { 4938 tmp = ((u64)entry_msr_load + entry_msr_ld_cnt * 16 - 1) | 4939 1ul << i; 4940 vmcs_write(ENTER_MSR_LD_ADDR, 4941 tmp - (entry_msr_ld_cnt * 16 - 1)); 4942 test_vmx_controls(false, false); 4943 } 4944 4945 vmcs_write(ENT_MSR_LD_CNT, 2); 4946 vmcs_write(ENTER_MSR_LD_ADDR, (1ULL << cpuid_maxphyaddr()) - 16); 4947 test_vmx_controls(false, false); 4948 vmcs_write(ENTER_MSR_LD_ADDR, (1ULL << cpuid_maxphyaddr()) - 32); 4949 test_vmx_controls(true, false); 4950 vmcs_write(ENTER_MSR_LD_ADDR, (1ULL << cpuid_maxphyaddr()) - 48); 4951 test_vmx_controls(true, false); 4952 } 4953 4954 /* 4955 * Tests for VM-entry control fields 4956 */ 4957 static void test_vm_entry_ctls(void) 4958 { 4959 test_invalid_event_injection(); 4960 test_entry_msr_load(); 4961 } 4962 4963 /* 4964 * The following checks are performed for the VM-exit MSR-store address if 4965 * the VM-exit MSR-store count field is non-zero: 4966 * 4967 * - The lower 4 bits of the VM-exit MSR-store address must be 0. 4968 * The address should not set any bits beyond the processor’s 4969 * physical-address width. 4970 * 4971 * - The address of the last byte in the VM-exit MSR-store area 4972 * should not set any bits beyond the processor’s physical-address 4973 * width. The address of this last byte is VM-exit MSR-store address 4974 * + (MSR count * 16) - 1. (The arithmetic used for the computation 4975 * uses more bits than the processor’s physical-address width.) 4976 * 4977 * If IA32_VMX_BASIC[48] is read as 1, neither address should set any bits 4978 * in the range 63:32. 4979 * 4980 * [Intel SDM] 4981 */ 4982 static void test_exit_msr_store(void) 4983 { 4984 exit_msr_store = alloc_page(); 4985 u64 tmp; 4986 u32 exit_msr_st_cnt = 1; 4987 int i; 4988 u32 addr_len = 64; 4989 4990 vmcs_write(EXI_MSR_ST_CNT, exit_msr_st_cnt); 4991 4992 /* Check first 4 bits of VM-exit MSR-store address */ 4993 for (i = 0; i < 4; i++) { 4994 tmp = (u64)exit_msr_store | 1ull << i; 4995 vmcs_write(EXIT_MSR_ST_ADDR, tmp); 4996 report_prefix_pushf("VM-exit MSR-store addr [4:0] %lx", 4997 tmp & 0xf); 4998 test_vmx_controls(false, false); 4999 report_prefix_pop(); 5000 } 5001 5002 if (basic.val & (1ul << 48)) 5003 addr_len = 32; 5004 5005 test_vmcs_addr_values("VM-exit-MSR-store address", 5006 EXIT_MSR_ST_ADDR, 16, false, false, 5007 4, addr_len - 1); 5008 5009 /* 5010 * Check last byte of VM-exit MSR-store address 5011 */ 5012 exit_msr_store = (struct vmx_msr_entry *)((u64)exit_msr_store & ~0xf); 5013 5014 for (i = (addr_len == 64 ? cpuid_maxphyaddr(): addr_len); 5015 i < 64; i++) { 5016 tmp = ((u64)exit_msr_store + exit_msr_st_cnt * 16 - 1) | 5017 1ul << i; 5018 vmcs_write(EXIT_MSR_ST_ADDR, 5019 tmp - (exit_msr_st_cnt * 16 - 1)); 5020 test_vmx_controls(false, false); 5021 } 5022 5023 vmcs_write(EXI_MSR_ST_CNT, 2); 5024 vmcs_write(EXIT_MSR_ST_ADDR, (1ULL << cpuid_maxphyaddr()) - 16); 5025 test_vmx_controls(false, false); 5026 vmcs_write(EXIT_MSR_ST_ADDR, (1ULL << cpuid_maxphyaddr()) - 32); 5027 test_vmx_controls(true, false); 5028 vmcs_write(EXIT_MSR_ST_ADDR, (1ULL << cpuid_maxphyaddr()) - 48); 5029 test_vmx_controls(true, false); 5030 } 5031 5032 /* 5033 * Tests for VM-exit controls 5034 */ 5035 static void test_vm_exit_ctls(void) 5036 { 5037 test_exit_msr_store(); 5038 } 5039 5040 /* 5041 * Check that the virtual CPU checks all of the VMX controls as 5042 * documented in the Intel SDM. 5043 */ 5044 static void vmx_controls_test(void) 5045 { 5046 /* 5047 * Bit 1 of the guest's RFLAGS must be 1, or VM-entry will 5048 * fail due to invalid guest state, should we make it that 5049 * far. 5050 */ 5051 vmcs_write(GUEST_RFLAGS, 0); 5052 5053 test_vm_execution_ctls(); 5054 test_vm_exit_ctls(); 5055 test_vm_entry_ctls(); 5056 } 5057 5058 static bool valid_vmcs_for_vmentry(void) 5059 { 5060 struct vmcs *current_vmcs = NULL; 5061 5062 if (vmcs_save(¤t_vmcs)) 5063 return false; 5064 5065 return current_vmcs && !current_vmcs->hdr.shadow_vmcs; 5066 } 5067 5068 static void try_vmentry_in_movss_shadow(void) 5069 { 5070 u32 vm_inst_err; 5071 u32 flags; 5072 bool early_failure = false; 5073 u32 expected_flags = X86_EFLAGS_FIXED; 5074 bool valid_vmcs = valid_vmcs_for_vmentry(); 5075 5076 expected_flags |= valid_vmcs ? X86_EFLAGS_ZF : X86_EFLAGS_CF; 5077 5078 /* 5079 * Indirectly set VM_INST_ERR to 12 ("VMREAD/VMWRITE from/to 5080 * unsupported VMCS component"). 5081 */ 5082 vmcs_write(~0u, 0); 5083 5084 __asm__ __volatile__ ("mov %[host_rsp], %%edx;" 5085 "vmwrite %%rsp, %%rdx;" 5086 "mov 0f, %%rax;" 5087 "mov %[host_rip], %%edx;" 5088 "vmwrite %%rax, %%rdx;" 5089 "mov $-1, %%ah;" 5090 "sahf;" 5091 "mov %%ss, %%ax;" 5092 "mov %%ax, %%ss;" 5093 "vmlaunch;" 5094 "mov $1, %[early_failure];" 5095 "0: lahf;" 5096 "movzbl %%ah, %[flags]" 5097 : [early_failure] "+r" (early_failure), 5098 [flags] "=&a" (flags) 5099 : [host_rsp] "i" (HOST_RSP), 5100 [host_rip] "i" (HOST_RIP) 5101 : "rdx", "cc", "memory"); 5102 vm_inst_err = vmcs_read(VMX_INST_ERROR); 5103 5104 report("Early VM-entry failure", early_failure); 5105 report("RFLAGS[8:0] is %x (actual %x)", flags == expected_flags, 5106 expected_flags, flags); 5107 if (valid_vmcs) 5108 report("VM-instruction error is %d (actual %d)", 5109 vm_inst_err == VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS, 5110 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS, vm_inst_err); 5111 } 5112 5113 static void vmentry_movss_shadow_test(void) 5114 { 5115 struct vmcs *orig_vmcs; 5116 5117 TEST_ASSERT(!vmcs_save(&orig_vmcs)); 5118 5119 /* 5120 * Set the launched flag on the current VMCS to verify the correct 5121 * error priority, below. 5122 */ 5123 test_set_guest(v2_null_test_guest); 5124 enter_guest(); 5125 5126 /* 5127 * With bit 1 of the guest's RFLAGS clear, VM-entry should 5128 * fail due to invalid guest state (if we make it that far). 5129 */ 5130 vmcs_write(GUEST_RFLAGS, 0); 5131 5132 /* 5133 * "VM entry with events blocked by MOV SS" takes precedence over 5134 * "VMLAUNCH with non-clear VMCS." 5135 */ 5136 report_prefix_push("valid current-VMCS"); 5137 try_vmentry_in_movss_shadow(); 5138 report_prefix_pop(); 5139 5140 /* 5141 * VMfailInvalid takes precedence over "VM entry with events 5142 * blocked by MOV SS." 5143 */ 5144 TEST_ASSERT(!vmcs_clear(orig_vmcs)); 5145 report_prefix_push("no current-VMCS"); 5146 try_vmentry_in_movss_shadow(); 5147 report_prefix_pop(); 5148 5149 TEST_ASSERT(!make_vmcs_current(orig_vmcs)); 5150 vmcs_write(GUEST_RFLAGS, X86_EFLAGS_FIXED); 5151 } 5152 5153 #define X86_FEATURE_PCID (1 << 17) 5154 #define X86_FEATURE_MCE (1 << 7) 5155 5156 static int write_cr4_checking(unsigned long val) 5157 { 5158 asm volatile(ASM_TRY("1f") 5159 "mov %0, %%cr4\n\t" 5160 "1:": : "r" (val)); 5161 return exception_vector(); 5162 } 5163 5164 static void vmx_cr_load_test(void) 5165 { 5166 struct cpuid _cpuid = cpuid(1); 5167 unsigned long cr4 = read_cr4(), cr3 = read_cr3(); 5168 5169 if (!(_cpuid.c & X86_FEATURE_PCID)) { 5170 report_skip("PCID not detected"); 5171 return; 5172 } 5173 if (!(_cpuid.d & X86_FEATURE_MCE)) { 5174 report_skip("MCE not detected"); 5175 return; 5176 } 5177 5178 TEST_ASSERT(!(cr4 & (X86_CR4_PCIDE | X86_CR4_MCE))); 5179 TEST_ASSERT(!(cr3 & X86_CR3_PCID_MASK)); 5180 5181 /* Enable PCID for L1. */ 5182 cr4 |= X86_CR4_PCIDE; 5183 cr3 |= 0x1; 5184 TEST_ASSERT(!write_cr4_checking(cr4)); 5185 write_cr3(cr3); 5186 5187 test_set_guest(v2_null_test_guest); 5188 vmcs_write(HOST_CR4, cr4); 5189 vmcs_write(HOST_CR3, cr3); 5190 enter_guest(); 5191 5192 /* 5193 * No exception is expected. 5194 * 5195 * NB. KVM loads the last guest write to CR4 into CR4 read 5196 * shadow. In order to trigger an exit to KVM, we can set a 5197 * bit that was zero in the above CR4 write and is owned by 5198 * KVM. We choose to set CR4.MCE, which shall have no side 5199 * effect because normally no guest MCE (e.g., as the result 5200 * of bad memory) would happen during this test. 5201 */ 5202 TEST_ASSERT(!write_cr4_checking(cr4 | X86_CR4_MCE)); 5203 5204 /* Cleanup L1 state: disable PCID. */ 5205 write_cr3(cr3 & ~X86_CR3_PCID_MASK); 5206 TEST_ASSERT(!write_cr4_checking(cr4 & ~X86_CR4_PCIDE)); 5207 } 5208 5209 static void vmx_nm_test_guest(void) 5210 { 5211 write_cr0(read_cr0() | X86_CR0_TS); 5212 asm volatile("fnop"); 5213 } 5214 5215 static void check_nm_exit(const char *test) 5216 { 5217 u32 reason = vmcs_read(EXI_REASON); 5218 u32 intr_info = vmcs_read(EXI_INTR_INFO); 5219 const u32 expected = INTR_INFO_VALID_MASK | INTR_TYPE_HARD_EXCEPTION | 5220 NM_VECTOR; 5221 5222 report("%s", reason == VMX_EXC_NMI && intr_info == expected, test); 5223 } 5224 5225 /* 5226 * This test checks that: 5227 * 5228 * (a) If L2 launches with CR0.TS clear, but later sets CR0.TS, then 5229 * a subsequent #NM VM-exit is reflected to L1. 5230 * 5231 * (b) If L2 launches with CR0.TS clear and CR0.EM set, then a 5232 * subsequent #NM VM-exit is reflected to L1. 5233 */ 5234 static void vmx_nm_test(void) 5235 { 5236 unsigned long cr0 = read_cr0(); 5237 5238 test_set_guest(vmx_nm_test_guest); 5239 5240 /* 5241 * L1 wants to intercept #NM exceptions encountered in L2. 5242 */ 5243 vmcs_write(EXC_BITMAP, 1 << NM_VECTOR); 5244 5245 /* 5246 * Launch L2 with CR0.TS clear, but don't claim host ownership of 5247 * any CR0 bits. L2 will set CR0.TS and then try to execute fnop, 5248 * which will raise #NM. L0 should reflect the #NM VM-exit to L1. 5249 */ 5250 vmcs_write(CR0_MASK, 0); 5251 vmcs_write(GUEST_CR0, cr0 & ~X86_CR0_TS); 5252 enter_guest(); 5253 check_nm_exit("fnop with CR0.TS set in L2 triggers #NM VM-exit to L1"); 5254 5255 /* 5256 * Re-enter L2 at the fnop instruction, with CR0.TS clear but 5257 * CR0.EM set. The fnop will still raise #NM, and L0 should 5258 * reflect the #NM VM-exit to L1. 5259 */ 5260 vmcs_write(GUEST_CR0, (cr0 & ~X86_CR0_TS) | X86_CR0_EM); 5261 enter_guest(); 5262 check_nm_exit("fnop with CR0.EM set in L2 triggers #NM VM-exit to L1"); 5263 5264 /* 5265 * Re-enter L2 at the fnop instruction, with both CR0.TS and 5266 * CR0.EM clear. There will be no #NM, and the L2 guest should 5267 * exit normally. 5268 */ 5269 vmcs_write(GUEST_CR0, cr0 & ~(X86_CR0_TS | X86_CR0_EM)); 5270 enter_guest(); 5271 } 5272 5273 bool vmx_pending_event_ipi_fired; 5274 static void vmx_pending_event_ipi_isr(isr_regs_t *regs) 5275 { 5276 vmx_pending_event_ipi_fired = true; 5277 eoi(); 5278 } 5279 5280 bool vmx_pending_event_guest_run; 5281 static void vmx_pending_event_guest(void) 5282 { 5283 vmcall(); 5284 vmx_pending_event_guest_run = true; 5285 } 5286 5287 static void vmx_pending_event_test_core(bool guest_hlt) 5288 { 5289 int ipi_vector = 0xf1; 5290 5291 vmx_pending_event_ipi_fired = false; 5292 handle_irq(ipi_vector, vmx_pending_event_ipi_isr); 5293 5294 vmx_pending_event_guest_run = false; 5295 test_set_guest(vmx_pending_event_guest); 5296 5297 vmcs_set_bits(PIN_CONTROLS, PIN_EXTINT); 5298 5299 enter_guest(); 5300 skip_exit_vmcall(); 5301 5302 if (guest_hlt) 5303 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 5304 5305 irq_disable(); 5306 apic_icr_write(APIC_DEST_SELF | APIC_DEST_PHYSICAL | 5307 APIC_DM_FIXED | ipi_vector, 5308 0); 5309 5310 enter_guest(); 5311 5312 assert_exit_reason(VMX_EXTINT); 5313 report("Guest did not run before host received IPI", 5314 !vmx_pending_event_guest_run); 5315 5316 irq_enable(); 5317 asm volatile ("nop"); 5318 irq_disable(); 5319 report("Got pending interrupt after IRQ enabled", 5320 vmx_pending_event_ipi_fired); 5321 5322 if (guest_hlt) 5323 vmcs_write(GUEST_ACTV_STATE, ACTV_ACTIVE); 5324 5325 enter_guest(); 5326 report("Guest finished running when no interrupt", 5327 vmx_pending_event_guest_run); 5328 } 5329 5330 static void vmx_pending_event_test(void) 5331 { 5332 vmx_pending_event_test_core(false); 5333 } 5334 5335 static void vmx_pending_event_hlt_test(void) 5336 { 5337 vmx_pending_event_test_core(true); 5338 } 5339 5340 static int vmx_window_test_ud_count; 5341 5342 static void vmx_window_test_ud_handler(struct ex_regs *regs) 5343 { 5344 vmx_window_test_ud_count++; 5345 } 5346 5347 static void vmx_nmi_window_test_guest(void) 5348 { 5349 handle_exception(UD_VECTOR, vmx_window_test_ud_handler); 5350 5351 asm volatile("vmcall\n\t" 5352 "nop\n\t"); 5353 5354 handle_exception(UD_VECTOR, NULL); 5355 } 5356 5357 static void verify_nmi_window_exit(u64 rip) 5358 { 5359 u32 exit_reason = vmcs_read(EXI_REASON); 5360 5361 report("Exit reason (%d) is 'NMI window'", 5362 exit_reason == VMX_NMI_WINDOW, exit_reason); 5363 report("RIP (%#lx) is %#lx", vmcs_read(GUEST_RIP) == rip, 5364 vmcs_read(GUEST_RIP), rip); 5365 report("Activity state (%ld) is 'ACTIVE'", 5366 vmcs_read(GUEST_ACTV_STATE) == ACTV_ACTIVE, 5367 vmcs_read(GUEST_ACTV_STATE)); 5368 } 5369 5370 static void vmx_nmi_window_test(void) 5371 { 5372 u64 nop_addr; 5373 void *ud_fault_addr = get_idt_addr(&boot_idt[UD_VECTOR]); 5374 5375 if (!(ctrl_pin_rev.clr & PIN_VIRT_NMI)) { 5376 report_skip("CPU does not support the \"Virtual NMIs\" VM-execution control."); 5377 return; 5378 } 5379 5380 if (!(ctrl_cpu_rev[0].clr & CPU_NMI_WINDOW)) { 5381 report_skip("CPU does not support the \"NMI-window exiting\" VM-execution control."); 5382 return; 5383 } 5384 5385 vmx_window_test_ud_count = 0; 5386 5387 report_prefix_push("NMI-window"); 5388 test_set_guest(vmx_nmi_window_test_guest); 5389 vmcs_set_bits(PIN_CONTROLS, PIN_VIRT_NMI); 5390 enter_guest(); 5391 skip_exit_vmcall(); 5392 nop_addr = vmcs_read(GUEST_RIP); 5393 5394 /* 5395 * Ask for "NMI-window exiting," and expect an immediate VM-exit. 5396 * RIP will not advance. 5397 */ 5398 report_prefix_push("active, no blocking"); 5399 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_NMI_WINDOW); 5400 enter_guest(); 5401 verify_nmi_window_exit(nop_addr); 5402 report_prefix_pop(); 5403 5404 /* 5405 * Ask for "NMI-window exiting" in a MOV-SS shadow, and expect 5406 * a VM-exit on the next instruction after the nop. (The nop 5407 * is one byte.) 5408 */ 5409 report_prefix_push("active, blocking by MOV-SS"); 5410 vmcs_write(GUEST_INTR_STATE, GUEST_INTR_STATE_MOVSS); 5411 enter_guest(); 5412 verify_nmi_window_exit(nop_addr + 1); 5413 report_prefix_pop(); 5414 5415 /* 5416 * Ask for "NMI-window exiting" (with event injection), and 5417 * expect a VM-exit after the event is injected. (RIP should 5418 * be at the address specified in the IDT entry for #UD.) 5419 */ 5420 report_prefix_push("active, no blocking, injecting #UD"); 5421 vmcs_write(ENT_INTR_INFO, 5422 INTR_INFO_VALID_MASK | INTR_TYPE_HARD_EXCEPTION | UD_VECTOR); 5423 enter_guest(); 5424 verify_nmi_window_exit((u64)ud_fault_addr); 5425 report_prefix_pop(); 5426 5427 /* 5428 * Ask for "NMI-window exiting" with NMI blocking, and expect 5429 * a VM-exit after the next IRET (i.e. after the #UD handler 5430 * returns). So, RIP should be back at one byte past the nop. 5431 */ 5432 report_prefix_push("active, blocking by NMI"); 5433 vmcs_write(GUEST_INTR_STATE, GUEST_INTR_STATE_NMI); 5434 enter_guest(); 5435 verify_nmi_window_exit(nop_addr + 1); 5436 report("#UD handler executed once (actual %d times)", 5437 vmx_window_test_ud_count == 1, 5438 vmx_window_test_ud_count); 5439 report_prefix_pop(); 5440 5441 if (!(rdmsr(MSR_IA32_VMX_MISC) & (1 << 6))) { 5442 report_skip("CPU does not support activity state HLT."); 5443 } else { 5444 /* 5445 * Ask for "NMI-window exiting" when entering activity 5446 * state HLT, and expect an immediate VM-exit. RIP is 5447 * still one byte past the nop. 5448 */ 5449 report_prefix_push("halted, no blocking"); 5450 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 5451 enter_guest(); 5452 verify_nmi_window_exit(nop_addr + 1); 5453 report_prefix_pop(); 5454 5455 /* 5456 * Ask for "NMI-window exiting" when entering activity 5457 * state HLT (with event injection), and expect a 5458 * VM-exit after the event is injected. (RIP should be 5459 * at the address specified in the IDT entry for #UD.) 5460 */ 5461 report_prefix_push("halted, no blocking, injecting #UD"); 5462 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 5463 vmcs_write(ENT_INTR_INFO, 5464 INTR_INFO_VALID_MASK | INTR_TYPE_HARD_EXCEPTION | 5465 UD_VECTOR); 5466 enter_guest(); 5467 verify_nmi_window_exit((u64)ud_fault_addr); 5468 report_prefix_pop(); 5469 } 5470 5471 vmcs_clear_bits(CPU_EXEC_CTRL0, CPU_NMI_WINDOW); 5472 enter_guest(); 5473 report_prefix_pop(); 5474 } 5475 5476 static void vmx_intr_window_test_guest(void) 5477 { 5478 handle_exception(UD_VECTOR, vmx_window_test_ud_handler); 5479 5480 /* 5481 * The two consecutive STIs are to ensure that only the first 5482 * one has a shadow. Note that NOP and STI are one byte 5483 * instructions. 5484 */ 5485 asm volatile("vmcall\n\t" 5486 "nop\n\t" 5487 "sti\n\t" 5488 "sti\n\t"); 5489 5490 handle_exception(UD_VECTOR, NULL); 5491 } 5492 5493 static void verify_intr_window_exit(u64 rip) 5494 { 5495 u32 exit_reason = vmcs_read(EXI_REASON); 5496 5497 report("Exit reason (%d) is 'interrupt window'", 5498 exit_reason == VMX_INTR_WINDOW, exit_reason); 5499 report("RIP (%#lx) is %#lx", vmcs_read(GUEST_RIP) == rip, 5500 vmcs_read(GUEST_RIP), rip); 5501 report("Activity state (%ld) is 'ACTIVE'", 5502 vmcs_read(GUEST_ACTV_STATE) == ACTV_ACTIVE, 5503 vmcs_read(GUEST_ACTV_STATE)); 5504 } 5505 5506 static void vmx_intr_window_test(void) 5507 { 5508 u64 vmcall_addr; 5509 u64 nop_addr; 5510 unsigned int orig_ud_gate_type; 5511 void *ud_fault_addr = get_idt_addr(&boot_idt[UD_VECTOR]); 5512 5513 if (!(ctrl_cpu_rev[0].clr & CPU_INTR_WINDOW)) { 5514 report_skip("CPU does not support the \"interrupt-window exiting\" VM-execution control."); 5515 return; 5516 } 5517 5518 /* 5519 * Change the IDT entry for #UD from interrupt gate to trap gate, 5520 * so that it won't clear RFLAGS.IF. We don't want interrupts to 5521 * be disabled after vectoring a #UD. 5522 */ 5523 orig_ud_gate_type = boot_idt[UD_VECTOR].type; 5524 boot_idt[UD_VECTOR].type = 15; 5525 5526 report_prefix_push("interrupt-window"); 5527 test_set_guest(vmx_intr_window_test_guest); 5528 enter_guest(); 5529 assert_exit_reason(VMX_VMCALL); 5530 vmcall_addr = vmcs_read(GUEST_RIP); 5531 5532 /* 5533 * Ask for "interrupt-window exiting" with RFLAGS.IF set and 5534 * no blocking; expect an immediate VM-exit. Note that we have 5535 * not advanced past the vmcall instruction yet, so RIP should 5536 * point to the vmcall instruction. 5537 */ 5538 report_prefix_push("active, no blocking, RFLAGS.IF=1"); 5539 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_INTR_WINDOW); 5540 vmcs_write(GUEST_RFLAGS, X86_EFLAGS_FIXED | X86_EFLAGS_IF); 5541 enter_guest(); 5542 verify_intr_window_exit(vmcall_addr); 5543 report_prefix_pop(); 5544 5545 /* 5546 * Ask for "interrupt-window exiting" (with event injection) 5547 * with RFLAGS.IF set and no blocking; expect a VM-exit after 5548 * the event is injected. That is, RIP should should be at the 5549 * address specified in the IDT entry for #UD. 5550 */ 5551 report_prefix_push("active, no blocking, RFLAGS.IF=1, injecting #UD"); 5552 vmcs_write(ENT_INTR_INFO, 5553 INTR_INFO_VALID_MASK | INTR_TYPE_HARD_EXCEPTION | UD_VECTOR); 5554 vmcall_addr = vmcs_read(GUEST_RIP); 5555 enter_guest(); 5556 verify_intr_window_exit((u64)ud_fault_addr); 5557 report_prefix_pop(); 5558 5559 /* 5560 * Let the L2 guest run through the IRET, back to the VMCALL. 5561 * We have to clear the "interrupt-window exiting" 5562 * VM-execution control, or it would just keep causing 5563 * VM-exits. Then, advance past the VMCALL and set the 5564 * "interrupt-window exiting" VM-execution control again. 5565 */ 5566 vmcs_clear_bits(CPU_EXEC_CTRL0, CPU_INTR_WINDOW); 5567 enter_guest(); 5568 skip_exit_vmcall(); 5569 nop_addr = vmcs_read(GUEST_RIP); 5570 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_INTR_WINDOW); 5571 5572 /* 5573 * Ask for "interrupt-window exiting" in a MOV-SS shadow with 5574 * RFLAGS.IF set, and expect a VM-exit on the next 5575 * instruction. (NOP is one byte.) 5576 */ 5577 report_prefix_push("active, blocking by MOV-SS, RFLAGS.IF=1"); 5578 vmcs_write(GUEST_INTR_STATE, GUEST_INTR_STATE_MOVSS); 5579 enter_guest(); 5580 verify_intr_window_exit(nop_addr + 1); 5581 report_prefix_pop(); 5582 5583 /* 5584 * Back up to the NOP and ask for "interrupt-window exiting" 5585 * in an STI shadow with RFLAGS.IF set, and expect a VM-exit 5586 * on the next instruction. (NOP is one byte.) 5587 */ 5588 report_prefix_push("active, blocking by STI, RFLAGS.IF=1"); 5589 vmcs_write(GUEST_RIP, nop_addr); 5590 vmcs_write(GUEST_INTR_STATE, GUEST_INTR_STATE_STI); 5591 enter_guest(); 5592 verify_intr_window_exit(nop_addr + 1); 5593 report_prefix_pop(); 5594 5595 /* 5596 * Ask for "interrupt-window exiting" with RFLAGS.IF clear, 5597 * and expect a VM-exit on the instruction following the STI 5598 * shadow. Only the first STI (which is one byte past the NOP) 5599 * should have a shadow. The second STI (which is two bytes 5600 * past the NOP) has no shadow. Therefore, the interrupt 5601 * window opens at three bytes past the NOP. 5602 */ 5603 report_prefix_push("active, RFLAGS.IF = 0"); 5604 vmcs_write(GUEST_RFLAGS, X86_EFLAGS_FIXED); 5605 enter_guest(); 5606 verify_intr_window_exit(nop_addr + 3); 5607 report_prefix_pop(); 5608 5609 if (!(rdmsr(MSR_IA32_VMX_MISC) & (1 << 6))) { 5610 report_skip("CPU does not support activity state HLT."); 5611 } else { 5612 /* 5613 * Ask for "interrupt-window exiting" when entering 5614 * activity state HLT, and expect an immediate 5615 * VM-exit. RIP is still three bytes past the nop. 5616 */ 5617 report_prefix_push("halted, no blocking"); 5618 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 5619 enter_guest(); 5620 verify_intr_window_exit(nop_addr + 3); 5621 report_prefix_pop(); 5622 5623 /* 5624 * Ask for "interrupt-window exiting" when entering 5625 * activity state HLT (with event injection), and 5626 * expect a VM-exit after the event is injected. That 5627 * is, RIP should should be at the address specified 5628 * in the IDT entry for #UD. 5629 */ 5630 report_prefix_push("halted, no blocking, injecting #UD"); 5631 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 5632 vmcs_write(ENT_INTR_INFO, 5633 INTR_INFO_VALID_MASK | INTR_TYPE_HARD_EXCEPTION | 5634 UD_VECTOR); 5635 enter_guest(); 5636 verify_intr_window_exit((u64)ud_fault_addr); 5637 report_prefix_pop(); 5638 } 5639 5640 boot_idt[UD_VECTOR].type = orig_ud_gate_type; 5641 vmcs_clear_bits(CPU_EXEC_CTRL0, CPU_INTR_WINDOW); 5642 enter_guest(); 5643 report_prefix_pop(); 5644 } 5645 5646 #define GUEST_TSC_OFFSET (1u << 30) 5647 5648 static u64 guest_tsc; 5649 5650 static void vmx_store_tsc_test_guest(void) 5651 { 5652 guest_tsc = rdtsc(); 5653 } 5654 5655 /* 5656 * This test ensures that when IA32_TSC is in the VM-exit MSR-store 5657 * list, the value saved is not subject to the TSC offset that is 5658 * applied to RDTSC/RDTSCP/RDMSR(IA32_TSC) in guest execution. 5659 */ 5660 static void vmx_store_tsc_test(void) 5661 { 5662 struct vmx_msr_entry msr_entry = { .index = MSR_IA32_TSC }; 5663 u64 low, high; 5664 5665 if (!(ctrl_cpu_rev[0].clr & CPU_USE_TSC_OFFSET)) { 5666 report_skip("'Use TSC offsetting' not supported"); 5667 return; 5668 } 5669 5670 test_set_guest(vmx_store_tsc_test_guest); 5671 5672 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_USE_TSC_OFFSET); 5673 vmcs_write(EXI_MSR_ST_CNT, 1); 5674 vmcs_write(EXIT_MSR_ST_ADDR, virt_to_phys(&msr_entry)); 5675 vmcs_write(TSC_OFFSET, GUEST_TSC_OFFSET); 5676 5677 low = rdtsc(); 5678 enter_guest(); 5679 high = rdtsc(); 5680 5681 report("RDTSC value in the guest (%lu) is in range [%lu, %lu]", 5682 low + GUEST_TSC_OFFSET <= guest_tsc && 5683 guest_tsc <= high + GUEST_TSC_OFFSET, 5684 guest_tsc, low + GUEST_TSC_OFFSET, high + GUEST_TSC_OFFSET); 5685 report("IA32_TSC value saved in the VM-exit MSR-store list (%lu) is in range [%lu, %lu]", 5686 low <= msr_entry.value && msr_entry.value <= high, 5687 msr_entry.value, low, high); 5688 } 5689 5690 static void vmx_db_test_guest(void) 5691 { 5692 /* 5693 * For a hardware generated single-step #DB. 5694 */ 5695 asm volatile("vmcall;" 5696 "nop;" 5697 ".Lpost_nop:"); 5698 /* 5699 * ...in a MOVSS shadow, with pending debug exceptions. 5700 */ 5701 asm volatile("vmcall;" 5702 "nop;" 5703 ".Lpost_movss_nop:"); 5704 /* 5705 * For an L0 synthesized single-step #DB. (L0 intercepts WBINVD and 5706 * emulates it in software.) 5707 */ 5708 asm volatile("vmcall;" 5709 "wbinvd;" 5710 ".Lpost_wbinvd:"); 5711 /* 5712 * ...in a MOVSS shadow, with pending debug exceptions. 5713 */ 5714 asm volatile("vmcall;" 5715 "wbinvd;" 5716 ".Lpost_movss_wbinvd:"); 5717 /* 5718 * For a hardware generated single-step #DB in a transactional region. 5719 */ 5720 asm volatile("vmcall;" 5721 ".Lxbegin: xbegin .Lskip_rtm;" 5722 "xend;" 5723 ".Lskip_rtm:"); 5724 } 5725 5726 /* 5727 * Clear the pending debug exceptions and RFLAGS.TF and re-enter 5728 * L2. No #DB is delivered and L2 continues to the next point of 5729 * interest. 5730 */ 5731 static void dismiss_db(void) 5732 { 5733 vmcs_write(GUEST_PENDING_DEBUG, 0); 5734 vmcs_write(GUEST_RFLAGS, X86_EFLAGS_FIXED); 5735 enter_guest(); 5736 } 5737 5738 /* 5739 * Check a variety of VMCS fields relevant to an intercepted #DB exception. 5740 * Then throw away the #DB exception and resume L2. 5741 */ 5742 static void check_db_exit(bool xfail_qual, bool xfail_dr6, bool xfail_pdbg, 5743 void *expected_rip, u64 expected_exit_qual, 5744 u64 expected_dr6) 5745 { 5746 u32 reason = vmcs_read(EXI_REASON); 5747 u32 intr_info = vmcs_read(EXI_INTR_INFO); 5748 u64 exit_qual = vmcs_read(EXI_QUALIFICATION); 5749 u64 guest_rip = vmcs_read(GUEST_RIP); 5750 u64 guest_pending_dbg = vmcs_read(GUEST_PENDING_DEBUG); 5751 u64 dr6 = read_dr6(); 5752 const u32 expected_intr_info = INTR_INFO_VALID_MASK | 5753 INTR_TYPE_HARD_EXCEPTION | DB_VECTOR; 5754 5755 report("Expected #DB VM-exit", 5756 reason == VMX_EXC_NMI && intr_info == expected_intr_info); 5757 report("Expected RIP %p (actual %lx)", (u64)expected_rip == guest_rip, 5758 expected_rip, guest_rip); 5759 report_xfail("Expected pending debug exceptions 0 (actual %lx)", 5760 xfail_pdbg, 0 == guest_pending_dbg, guest_pending_dbg); 5761 report_xfail("Expected exit qualification %lx (actual %lx)", xfail_qual, 5762 expected_exit_qual == exit_qual, 5763 expected_exit_qual, exit_qual); 5764 report_xfail("Expected DR6 %lx (actual %lx)", xfail_dr6, 5765 expected_dr6 == dr6, expected_dr6, dr6); 5766 dismiss_db(); 5767 } 5768 5769 /* 5770 * Assuming the guest has just exited on a VMCALL instruction, skip 5771 * over the vmcall, and set the guest's RFLAGS.TF in the VMCS. If 5772 * pending debug exceptions are non-zero, set the VMCS up as if the 5773 * previous instruction was a MOVSS that generated the indicated 5774 * pending debug exceptions. Then enter L2. 5775 */ 5776 static void single_step_guest(const char *test_name, u64 starting_dr6, 5777 u64 pending_debug_exceptions) 5778 { 5779 printf("\n%s\n", test_name); 5780 skip_exit_vmcall(); 5781 write_dr6(starting_dr6); 5782 vmcs_write(GUEST_RFLAGS, X86_EFLAGS_FIXED | X86_EFLAGS_TF); 5783 if (pending_debug_exceptions) { 5784 vmcs_write(GUEST_PENDING_DEBUG, pending_debug_exceptions); 5785 vmcs_write(GUEST_INTR_STATE, GUEST_INTR_STATE_MOVSS); 5786 } 5787 enter_guest(); 5788 } 5789 5790 /* 5791 * When L1 intercepts #DB, verify that a single-step trap clears 5792 * pending debug exceptions, populates the exit qualification field 5793 * properly, and that DR6 is not prematurely clobbered. In a 5794 * (simulated) MOVSS shadow, make sure that the pending debug 5795 * exception bits are properly accumulated into the exit qualification 5796 * field. 5797 */ 5798 static void vmx_db_test(void) 5799 { 5800 /* 5801 * We are going to set a few arbitrary bits in DR6 to verify that 5802 * (a) DR6 is not modified by an intercepted #DB, and 5803 * (b) stale bits in DR6 (DR6.BD, in particular) don't leak into 5804 * the exit qualification field for a subsequent #DB exception. 5805 */ 5806 const u64 starting_dr6 = DR6_RESERVED | BIT(13) | DR_TRAP3 | DR_TRAP1; 5807 extern char post_nop asm(".Lpost_nop"); 5808 extern char post_movss_nop asm(".Lpost_movss_nop"); 5809 extern char post_wbinvd asm(".Lpost_wbinvd"); 5810 extern char post_movss_wbinvd asm(".Lpost_movss_wbinvd"); 5811 extern char xbegin asm(".Lxbegin"); 5812 extern char skip_rtm asm(".Lskip_rtm"); 5813 5814 /* 5815 * L1 wants to intercept #DB exceptions encountered in L2. 5816 */ 5817 vmcs_write(EXC_BITMAP, BIT(DB_VECTOR)); 5818 5819 /* 5820 * Start L2 and run it up to the first point of interest. 5821 */ 5822 test_set_guest(vmx_db_test_guest); 5823 enter_guest(); 5824 5825 /* 5826 * Hardware-delivered #DB trap for single-step sets the 5827 * standard that L0 has to follow for emulated instructions. 5828 */ 5829 single_step_guest("Hardware delivered single-step", starting_dr6, 0); 5830 check_db_exit(false, false, false, &post_nop, DR_STEP, starting_dr6); 5831 5832 /* 5833 * Hardware-delivered #DB trap for single-step in MOVSS shadow 5834 * also sets the standard that L0 has to follow for emulated 5835 * instructions. Here, we establish the VMCS pending debug 5836 * exceptions to indicate that the simulated MOVSS triggered a 5837 * data breakpoint as well as the single-step trap. 5838 */ 5839 single_step_guest("Hardware delivered single-step in MOVSS shadow", 5840 starting_dr6, BIT(12) | DR_STEP | DR_TRAP0 ); 5841 check_db_exit(false, false, false, &post_movss_nop, DR_STEP | DR_TRAP0, 5842 starting_dr6); 5843 5844 /* 5845 * L0 synthesized #DB trap for single-step is buggy, because 5846 * kvm (a) clobbers DR6 too early, and (b) tries its best to 5847 * reconstitute the exit qualification from the prematurely 5848 * modified DR6, but fails miserably. 5849 */ 5850 single_step_guest("Software synthesized single-step", starting_dr6, 0); 5851 check_db_exit(true, true, false, &post_wbinvd, DR_STEP, starting_dr6); 5852 5853 /* 5854 * L0 synthesized #DB trap for single-step in MOVSS shadow is 5855 * even worse, because L0 also leaves the pending debug 5856 * exceptions in the VMCS instead of accumulating them into 5857 * the exit qualification field for the #DB exception. 5858 */ 5859 single_step_guest("Software synthesized single-step in MOVSS shadow", 5860 starting_dr6, BIT(12) | DR_STEP | DR_TRAP0); 5861 check_db_exit(true, true, true, &post_movss_wbinvd, DR_STEP | DR_TRAP0, 5862 starting_dr6); 5863 5864 /* 5865 * Optional RTM test for hardware that supports RTM, to 5866 * demonstrate that the current volume 3 of the SDM 5867 * (325384-067US), table 27-1 is incorrect. Bit 16 of the exit 5868 * qualification for debug exceptions is not reserved. It is 5869 * set to 1 if a debug exception (#DB) or a breakpoint 5870 * exception (#BP) occurs inside an RTM region while advanced 5871 * debugging of RTM transactional regions is enabled. 5872 */ 5873 if (cpuid(7).b & BIT(11)) { 5874 vmcs_write(ENT_CONTROLS, 5875 vmcs_read(ENT_CONTROLS) | ENT_LOAD_DBGCTLS); 5876 /* 5877 * Set DR7.RTM[bit 11] and IA32_DEBUGCTL.RTM[bit 15] 5878 * in the guest to enable advanced debugging of RTM 5879 * transactional regions. 5880 */ 5881 vmcs_write(GUEST_DR7, BIT(11)); 5882 vmcs_write(GUEST_DEBUGCTL, BIT(15)); 5883 single_step_guest("Hardware delivered single-step in " 5884 "transactional region", starting_dr6, 0); 5885 check_db_exit(false, false, false, &xbegin, BIT(16), 5886 starting_dr6); 5887 } else { 5888 vmcs_write(GUEST_RIP, (u64)&skip_rtm); 5889 enter_guest(); 5890 } 5891 } 5892 5893 static bool cpu_has_apicv(void) 5894 { 5895 return ((ctrl_cpu_rev[1].clr & CPU_APIC_REG_VIRT) && 5896 (ctrl_cpu_rev[1].clr & CPU_VINTD) && 5897 (ctrl_pin_rev.clr & PIN_POST_INTR)); 5898 } 5899 5900 static void enable_vid(void) 5901 { 5902 void *virtual_apic_page; 5903 5904 assert(cpu_has_apicv()); 5905 5906 disable_intercept_for_x2apic_msrs(); 5907 5908 virtual_apic_page = alloc_page(); 5909 vmcs_write(APIC_VIRT_ADDR, (u64)virtual_apic_page); 5910 5911 vmcs_set_bits(PIN_CONTROLS, PIN_EXTINT); 5912 5913 vmcs_write(EOI_EXIT_BITMAP0, 0x0); 5914 vmcs_write(EOI_EXIT_BITMAP1, 0x0); 5915 vmcs_write(EOI_EXIT_BITMAP2, 0x0); 5916 vmcs_write(EOI_EXIT_BITMAP3, 0x0); 5917 5918 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_SECONDARY | CPU_TPR_SHADOW); 5919 vmcs_set_bits(CPU_EXEC_CTRL1, CPU_VINTD | CPU_VIRT_X2APIC); 5920 } 5921 5922 static void trigger_ioapic_scan_thread(void *data) 5923 { 5924 /* Wait until other CPU entered L2 */ 5925 while (vmx_get_test_stage() != 1) 5926 ; 5927 5928 /* Trigger ioapic scan */ 5929 ioapic_set_redir(0xf, 0x79, TRIGGER_LEVEL); 5930 vmx_set_test_stage(2); 5931 } 5932 5933 static void irq_79_handler_guest(isr_regs_t *regs) 5934 { 5935 eoi(); 5936 5937 /* L1 expects vmexit on VMX_VMCALL and not VMX_EOI_INDUCED */ 5938 vmcall(); 5939 } 5940 5941 /* 5942 * Constant for num of busy-loop iterations after which 5943 * a timer interrupt should have happened in host 5944 */ 5945 #define TIMER_INTERRUPT_DELAY 100000000 5946 5947 static void vmx_eoi_bitmap_ioapic_scan_test_guest(void) 5948 { 5949 handle_irq(0x79, irq_79_handler_guest); 5950 irq_enable(); 5951 5952 /* Signal to L1 CPU to trigger ioapic scan */ 5953 vmx_set_test_stage(1); 5954 /* Wait until L1 CPU to trigger ioapic scan */ 5955 while (vmx_get_test_stage() != 2) 5956 ; 5957 5958 /* 5959 * Wait for L0 timer interrupt to be raised while we run in L2 5960 * such that L0 will process the IOAPIC scan request before 5961 * resuming L2 5962 */ 5963 delay(TIMER_INTERRUPT_DELAY); 5964 5965 asm volatile ("int $0x79"); 5966 } 5967 5968 static void vmx_eoi_bitmap_ioapic_scan_test(void) 5969 { 5970 if (!cpu_has_apicv() || (cpu_count() < 2)) { 5971 report_skip(__func__); 5972 return; 5973 } 5974 5975 enable_vid(); 5976 5977 on_cpu_async(1, trigger_ioapic_scan_thread, NULL); 5978 test_set_guest(vmx_eoi_bitmap_ioapic_scan_test_guest); 5979 5980 /* 5981 * Launch L2. 5982 * We expect the exit reason to be VMX_VMCALL (and not EOI INDUCED). 5983 * In case the reason isn't VMX_VMCALL, the asserion inside 5984 * skip_exit_vmcall() will fail. 5985 */ 5986 enter_guest(); 5987 skip_exit_vmcall(); 5988 5989 /* Let L2 finish */ 5990 enter_guest(); 5991 report(__func__, 1); 5992 } 5993 5994 #define HLT_WITH_RVI_VECTOR (0xf1) 5995 5996 bool vmx_hlt_with_rvi_guest_isr_fired; 5997 static void vmx_hlt_with_rvi_guest_isr(isr_regs_t *regs) 5998 { 5999 vmx_hlt_with_rvi_guest_isr_fired = true; 6000 eoi(); 6001 } 6002 6003 static void vmx_hlt_with_rvi_guest(void) 6004 { 6005 handle_irq(HLT_WITH_RVI_VECTOR, vmx_hlt_with_rvi_guest_isr); 6006 6007 irq_enable(); 6008 asm volatile ("nop"); 6009 6010 vmcall(); 6011 } 6012 6013 static void vmx_hlt_with_rvi_test(void) 6014 { 6015 if (!cpu_has_apicv()) { 6016 report_skip(__func__); 6017 return; 6018 } 6019 6020 enable_vid(); 6021 6022 vmx_hlt_with_rvi_guest_isr_fired = false; 6023 test_set_guest(vmx_hlt_with_rvi_guest); 6024 6025 enter_guest(); 6026 skip_exit_vmcall(); 6027 6028 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 6029 vmcs_write(GUEST_INT_STATUS, HLT_WITH_RVI_VECTOR); 6030 enter_guest(); 6031 6032 report("Interrupt raised in guest", vmx_hlt_with_rvi_guest_isr_fired); 6033 } 6034 6035 static void set_irq_line_thread(void *data) 6036 { 6037 /* Wait until other CPU entered L2 */ 6038 while (vmx_get_test_stage() != 1) 6039 ; 6040 6041 /* Set irq-line 0xf to raise vector 0x78 for vCPU 0 */ 6042 ioapic_set_redir(0xf, 0x78, TRIGGER_LEVEL); 6043 vmx_set_test_stage(2); 6044 } 6045 6046 static bool irq_78_handler_vmcall_before_eoi; 6047 static void irq_78_handler_guest(isr_regs_t *regs) 6048 { 6049 set_irq_line(0xf, 0); 6050 if (irq_78_handler_vmcall_before_eoi) 6051 vmcall(); 6052 eoi(); 6053 vmcall(); 6054 } 6055 6056 static void vmx_apic_passthrough_guest(void) 6057 { 6058 handle_irq(0x78, irq_78_handler_guest); 6059 irq_enable(); 6060 6061 /* If requested, wait for other CPU to trigger ioapic scan */ 6062 if (vmx_get_test_stage() < 1) { 6063 vmx_set_test_stage(1); 6064 while (vmx_get_test_stage() != 2) 6065 ; 6066 } 6067 6068 set_irq_line(0xf, 1); 6069 } 6070 6071 static void vmx_apic_passthrough(bool set_irq_line_from_thread) 6072 { 6073 if (set_irq_line_from_thread && (cpu_count() < 2)) { 6074 report_skip(__func__); 6075 return; 6076 } 6077 6078 u64 cpu_ctrl_0 = CPU_SECONDARY; 6079 u64 cpu_ctrl_1 = 0; 6080 6081 disable_intercept_for_x2apic_msrs(); 6082 6083 vmcs_write(PIN_CONTROLS, vmcs_read(PIN_CONTROLS) & ~PIN_EXTINT); 6084 6085 vmcs_write(CPU_EXEC_CTRL0, vmcs_read(CPU_EXEC_CTRL0) | cpu_ctrl_0); 6086 vmcs_write(CPU_EXEC_CTRL1, vmcs_read(CPU_EXEC_CTRL1) | cpu_ctrl_1); 6087 6088 if (set_irq_line_from_thread) { 6089 irq_78_handler_vmcall_before_eoi = false; 6090 on_cpu_async(1, set_irq_line_thread, NULL); 6091 } else { 6092 irq_78_handler_vmcall_before_eoi = true; 6093 ioapic_set_redir(0xf, 0x78, TRIGGER_LEVEL); 6094 vmx_set_test_stage(2); 6095 } 6096 test_set_guest(vmx_apic_passthrough_guest); 6097 6098 if (irq_78_handler_vmcall_before_eoi) { 6099 /* Before EOI remote_irr should still be set */ 6100 enter_guest(); 6101 skip_exit_vmcall(); 6102 TEST_ASSERT_EQ_MSG(1, (int)ioapic_read_redir(0xf).remote_irr, 6103 "IOAPIC pass-through: remote_irr=1 before EOI"); 6104 } 6105 6106 /* After EOI remote_irr should be cleared */ 6107 enter_guest(); 6108 skip_exit_vmcall(); 6109 TEST_ASSERT_EQ_MSG(0, (int)ioapic_read_redir(0xf).remote_irr, 6110 "IOAPIC pass-through: remote_irr=0 after EOI"); 6111 6112 /* Let L2 finish */ 6113 enter_guest(); 6114 report(__func__, 1); 6115 } 6116 6117 static void vmx_apic_passthrough_test(void) 6118 { 6119 vmx_apic_passthrough(false); 6120 } 6121 6122 static void vmx_apic_passthrough_thread_test(void) 6123 { 6124 vmx_apic_passthrough(true); 6125 } 6126 6127 enum vmcs_access { 6128 ACCESS_VMREAD, 6129 ACCESS_VMWRITE, 6130 ACCESS_NONE, 6131 }; 6132 6133 struct vmcs_shadow_test_common { 6134 enum vmcs_access op; 6135 enum Reason reason; 6136 u64 field; 6137 u64 value; 6138 u64 flags; 6139 u64 time; 6140 } l1_l2_common; 6141 6142 static inline u64 vmread_flags(u64 field, u64 *val) 6143 { 6144 u64 flags; 6145 6146 asm volatile ("vmread %2, %1; pushf; pop %0" 6147 : "=r" (flags), "=rm" (*val) : "r" (field) : "cc"); 6148 return flags & X86_EFLAGS_ALU; 6149 } 6150 6151 static inline u64 vmwrite_flags(u64 field, u64 val) 6152 { 6153 u64 flags; 6154 6155 asm volatile ("vmwrite %1, %2; pushf; pop %0" 6156 : "=r"(flags) : "rm" (val), "r" (field) : "cc"); 6157 return flags & X86_EFLAGS_ALU; 6158 } 6159 6160 static void vmx_vmcs_shadow_test_guest(void) 6161 { 6162 struct vmcs_shadow_test_common *c = &l1_l2_common; 6163 u64 start; 6164 6165 while (c->op != ACCESS_NONE) { 6166 start = rdtsc(); 6167 switch (c->op) { 6168 default: 6169 c->flags = -1ull; 6170 break; 6171 case ACCESS_VMREAD: 6172 c->flags = vmread_flags(c->field, &c->value); 6173 break; 6174 case ACCESS_VMWRITE: 6175 c->flags = vmwrite_flags(c->field, 0); 6176 break; 6177 } 6178 c->time = rdtsc() - start; 6179 vmcall(); 6180 } 6181 } 6182 6183 static u64 vmread_from_shadow(u64 field) 6184 { 6185 struct vmcs *primary; 6186 struct vmcs *shadow; 6187 u64 value; 6188 6189 TEST_ASSERT(!vmcs_save(&primary)); 6190 shadow = (struct vmcs *)vmcs_read(VMCS_LINK_PTR); 6191 TEST_ASSERT(!make_vmcs_current(shadow)); 6192 value = vmcs_read(field); 6193 TEST_ASSERT(!make_vmcs_current(primary)); 6194 return value; 6195 } 6196 6197 static u64 vmwrite_to_shadow(u64 field, u64 value) 6198 { 6199 struct vmcs *primary; 6200 struct vmcs *shadow; 6201 6202 TEST_ASSERT(!vmcs_save(&primary)); 6203 shadow = (struct vmcs *)vmcs_read(VMCS_LINK_PTR); 6204 TEST_ASSERT(!make_vmcs_current(shadow)); 6205 vmcs_write(field, value); 6206 value = vmcs_read(field); 6207 TEST_ASSERT(!make_vmcs_current(primary)); 6208 return value; 6209 } 6210 6211 static void vmcs_shadow_test_access(u8 *bitmap[2], enum vmcs_access access) 6212 { 6213 struct vmcs_shadow_test_common *c = &l1_l2_common; 6214 6215 c->op = access; 6216 vmcs_write(VMX_INST_ERROR, 0); 6217 enter_guest(); 6218 c->reason = vmcs_read(EXI_REASON) & 0xffff; 6219 if (c->reason != VMX_VMCALL) { 6220 skip_exit_insn(); 6221 enter_guest(); 6222 } 6223 skip_exit_vmcall(); 6224 } 6225 6226 static void vmcs_shadow_test_field(u8 *bitmap[2], u64 field) 6227 { 6228 struct vmcs_shadow_test_common *c = &l1_l2_common; 6229 struct vmcs *shadow; 6230 u64 value; 6231 uintptr_t flags[2]; 6232 bool good_shadow; 6233 u32 vmx_inst_error; 6234 6235 report_prefix_pushf("field %lx", field); 6236 c->field = field; 6237 6238 shadow = (struct vmcs *)vmcs_read(VMCS_LINK_PTR); 6239 if (shadow != (struct vmcs *)-1ull) { 6240 flags[ACCESS_VMREAD] = vmread_flags(field, &value); 6241 flags[ACCESS_VMWRITE] = vmwrite_flags(field, value); 6242 good_shadow = !flags[ACCESS_VMREAD] && !flags[ACCESS_VMWRITE]; 6243 } else { 6244 /* 6245 * When VMCS link pointer is -1ull, VMWRITE/VMREAD on 6246 * shadowed-fields should fail with setting RFLAGS.CF. 6247 */ 6248 flags[ACCESS_VMREAD] = X86_EFLAGS_CF; 6249 flags[ACCESS_VMWRITE] = X86_EFLAGS_CF; 6250 good_shadow = false; 6251 } 6252 6253 /* Intercept both VMREAD and VMWRITE. */ 6254 report_prefix_push("no VMREAD/VMWRITE permission"); 6255 /* VMWRITE/VMREAD done on reserved-bit should always intercept */ 6256 if (!(field >> VMCS_FIELD_RESERVED_SHIFT)) { 6257 set_bit(field, bitmap[ACCESS_VMREAD]); 6258 set_bit(field, bitmap[ACCESS_VMWRITE]); 6259 } 6260 vmcs_shadow_test_access(bitmap, ACCESS_VMWRITE); 6261 report("not shadowed for VMWRITE", c->reason == VMX_VMWRITE); 6262 vmcs_shadow_test_access(bitmap, ACCESS_VMREAD); 6263 report("not shadowed for VMREAD", c->reason == VMX_VMREAD); 6264 report_prefix_pop(); 6265 6266 if (field >> VMCS_FIELD_RESERVED_SHIFT) 6267 goto out; 6268 6269 /* Permit shadowed VMREAD. */ 6270 report_prefix_push("VMREAD permission only"); 6271 clear_bit(field, bitmap[ACCESS_VMREAD]); 6272 set_bit(field, bitmap[ACCESS_VMWRITE]); 6273 if (good_shadow) 6274 value = vmwrite_to_shadow(field, MAGIC_VAL_1 + field); 6275 vmcs_shadow_test_access(bitmap, ACCESS_VMWRITE); 6276 report("not shadowed for VMWRITE", c->reason == VMX_VMWRITE); 6277 vmcs_shadow_test_access(bitmap, ACCESS_VMREAD); 6278 vmx_inst_error = vmcs_read(VMX_INST_ERROR); 6279 report("shadowed for VMREAD (in %ld cycles)", c->reason == VMX_VMCALL, 6280 c->time); 6281 report("ALU flags after VMREAD (%lx) are as expected (%lx)", 6282 c->flags == flags[ACCESS_VMREAD], 6283 c->flags, flags[ACCESS_VMREAD]); 6284 if (good_shadow) 6285 report("value read from shadow (%lx) is as expected (%lx)", 6286 c->value == value, c->value, value); 6287 else if (shadow != (struct vmcs *)-1ull && flags[ACCESS_VMREAD]) 6288 report("VMX_INST_ERROR (%d) is as expected (%d)", 6289 vmx_inst_error == VMXERR_UNSUPPORTED_VMCS_COMPONENT, 6290 vmx_inst_error, VMXERR_UNSUPPORTED_VMCS_COMPONENT); 6291 report_prefix_pop(); 6292 6293 /* Permit shadowed VMWRITE. */ 6294 report_prefix_push("VMWRITE permission only"); 6295 set_bit(field, bitmap[ACCESS_VMREAD]); 6296 clear_bit(field, bitmap[ACCESS_VMWRITE]); 6297 if (good_shadow) 6298 vmwrite_to_shadow(field, MAGIC_VAL_1 + field); 6299 vmcs_shadow_test_access(bitmap, ACCESS_VMWRITE); 6300 vmx_inst_error = vmcs_read(VMX_INST_ERROR); 6301 report("shadowed for VMWRITE (in %ld cycles)", c->reason == VMX_VMCALL, 6302 c->time); 6303 report("ALU flags after VMWRITE (%lx) are as expected (%lx)", 6304 c->flags == flags[ACCESS_VMREAD], 6305 c->flags, flags[ACCESS_VMREAD]); 6306 if (good_shadow) { 6307 value = vmread_from_shadow(field); 6308 report("shadow VMCS value (%lx) is as expected (%lx)", 6309 value == 0, value, 0ul); 6310 } else if (shadow != (struct vmcs *)-1ull && flags[ACCESS_VMWRITE]) { 6311 report("VMX_INST_ERROR (%d) is as expected (%d)", 6312 vmx_inst_error == VMXERR_UNSUPPORTED_VMCS_COMPONENT, 6313 vmx_inst_error, VMXERR_UNSUPPORTED_VMCS_COMPONENT); 6314 } 6315 vmcs_shadow_test_access(bitmap, ACCESS_VMREAD); 6316 report("not shadowed for VMREAD", c->reason == VMX_VMREAD); 6317 report_prefix_pop(); 6318 6319 /* Permit shadowed VMREAD and VMWRITE. */ 6320 report_prefix_push("VMREAD and VMWRITE permission"); 6321 clear_bit(field, bitmap[ACCESS_VMREAD]); 6322 clear_bit(field, bitmap[ACCESS_VMWRITE]); 6323 if (good_shadow) 6324 vmwrite_to_shadow(field, MAGIC_VAL_1 + field); 6325 vmcs_shadow_test_access(bitmap, ACCESS_VMWRITE); 6326 vmx_inst_error = vmcs_read(VMX_INST_ERROR); 6327 report("shadowed for VMWRITE (in %ld cycles)", c->reason == VMX_VMCALL, 6328 c->time); 6329 report("ALU flags after VMWRITE (%lx) are as expected (%lx)", 6330 c->flags == flags[ACCESS_VMREAD], 6331 c->flags, flags[ACCESS_VMREAD]); 6332 if (good_shadow) { 6333 value = vmread_from_shadow(field); 6334 report("shadow VMCS value (%lx) is as expected (%lx)", 6335 value == 0, value, 0ul); 6336 } else if (shadow != (struct vmcs *)-1ull && flags[ACCESS_VMWRITE]) { 6337 report("VMX_INST_ERROR (%d) is as expected (%d)", 6338 vmx_inst_error == VMXERR_UNSUPPORTED_VMCS_COMPONENT, 6339 vmx_inst_error, VMXERR_UNSUPPORTED_VMCS_COMPONENT); 6340 } 6341 vmcs_shadow_test_access(bitmap, ACCESS_VMREAD); 6342 vmx_inst_error = vmcs_read(VMX_INST_ERROR); 6343 report("shadowed for VMREAD (in %ld cycles)", c->reason == VMX_VMCALL, 6344 c->time); 6345 report("ALU flags after VMREAD (%lx) are as expected (%lx)", 6346 c->flags == flags[ACCESS_VMREAD], 6347 c->flags, flags[ACCESS_VMREAD]); 6348 if (good_shadow) 6349 report("value read from shadow (%lx) is as expected (%lx)", 6350 c->value == 0, c->value, 0ul); 6351 else if (shadow != (struct vmcs *)-1ull && flags[ACCESS_VMREAD]) 6352 report("VMX_INST_ERROR (%d) is as expected (%d)", 6353 vmx_inst_error == VMXERR_UNSUPPORTED_VMCS_COMPONENT, 6354 vmx_inst_error, VMXERR_UNSUPPORTED_VMCS_COMPONENT); 6355 report_prefix_pop(); 6356 6357 out: 6358 report_prefix_pop(); 6359 } 6360 6361 static void vmx_vmcs_shadow_test_body(u8 *bitmap[2]) 6362 { 6363 unsigned base; 6364 unsigned index; 6365 unsigned bit; 6366 unsigned highest_index = rdmsr(MSR_IA32_VMX_VMCS_ENUM); 6367 6368 /* Run test on all possible valid VMCS fields */ 6369 for (base = 0; 6370 base < (1 << VMCS_FIELD_RESERVED_SHIFT); 6371 base += (1 << VMCS_FIELD_TYPE_SHIFT)) 6372 for (index = 0; index <= highest_index; index++) 6373 vmcs_shadow_test_field(bitmap, base + index); 6374 6375 /* 6376 * Run tests on some invalid VMCS fields 6377 * (Have reserved bit set). 6378 */ 6379 for (bit = VMCS_FIELD_RESERVED_SHIFT; bit < VMCS_FIELD_BIT_SIZE; bit++) 6380 vmcs_shadow_test_field(bitmap, (1ull << bit)); 6381 } 6382 6383 static void vmx_vmcs_shadow_test(void) 6384 { 6385 u8 *bitmap[2]; 6386 struct vmcs *shadow; 6387 6388 if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY)) { 6389 printf("\t'Activate secondary controls' not supported.\n"); 6390 return; 6391 } 6392 6393 if (!(ctrl_cpu_rev[1].clr & CPU_SHADOW_VMCS)) { 6394 printf("\t'VMCS shadowing' not supported.\n"); 6395 return; 6396 } 6397 6398 if (!(rdmsr(MSR_IA32_VMX_MISC) & 6399 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) { 6400 printf("\tVMWRITE can't modify VM-exit information fields.\n"); 6401 return; 6402 } 6403 6404 test_set_guest(vmx_vmcs_shadow_test_guest); 6405 6406 bitmap[ACCESS_VMREAD] = alloc_page(); 6407 bitmap[ACCESS_VMWRITE] = alloc_page(); 6408 6409 vmcs_write(VMREAD_BITMAP, virt_to_phys(bitmap[ACCESS_VMREAD])); 6410 vmcs_write(VMWRITE_BITMAP, virt_to_phys(bitmap[ACCESS_VMWRITE])); 6411 6412 shadow = alloc_page(); 6413 shadow->hdr.revision_id = basic.revision; 6414 shadow->hdr.shadow_vmcs = 1; 6415 TEST_ASSERT(!vmcs_clear(shadow)); 6416 6417 vmcs_clear_bits(CPU_EXEC_CTRL0, CPU_RDTSC); 6418 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_SECONDARY); 6419 vmcs_set_bits(CPU_EXEC_CTRL1, CPU_SHADOW_VMCS); 6420 6421 vmcs_write(VMCS_LINK_PTR, virt_to_phys(shadow)); 6422 report_prefix_push("valid link pointer"); 6423 vmx_vmcs_shadow_test_body(bitmap); 6424 report_prefix_pop(); 6425 6426 vmcs_write(VMCS_LINK_PTR, -1ull); 6427 report_prefix_push("invalid link pointer"); 6428 vmx_vmcs_shadow_test_body(bitmap); 6429 report_prefix_pop(); 6430 6431 l1_l2_common.op = ACCESS_NONE; 6432 enter_guest(); 6433 } 6434 6435 6436 6437 static int invalid_msr_init(struct vmcs *vmcs) 6438 { 6439 if (!(ctrl_pin_rev.clr & PIN_PREEMPT)) { 6440 printf("\tPreemption timer is not supported\n"); 6441 return VMX_TEST_EXIT; 6442 } 6443 vmcs_write(PIN_CONTROLS, vmcs_read(PIN_CONTROLS) | PIN_PREEMPT); 6444 preempt_val = 10000000; 6445 vmcs_write(PREEMPT_TIMER_VALUE, preempt_val); 6446 preempt_scale = rdmsr(MSR_IA32_VMX_MISC) & 0x1F; 6447 6448 if (!(ctrl_exit_rev.clr & EXI_SAVE_PREEMPT)) 6449 printf("\tSave preemption value is not supported\n"); 6450 6451 vmcs_write(ENT_MSR_LD_CNT, 1); 6452 vmcs_write(ENTER_MSR_LD_ADDR, (u64)0x13370000); 6453 6454 return VMX_TEST_START; 6455 } 6456 6457 6458 static void invalid_msr_main(void) 6459 { 6460 report("Invalid MSR load", 0); 6461 } 6462 6463 static int invalid_msr_exit_handler(void) 6464 { 6465 report("Invalid MSR load", 0); 6466 print_vmexit_info(); 6467 return VMX_TEST_EXIT; 6468 } 6469 6470 static int invalid_msr_entry_failure(struct vmentry_failure *failure) 6471 { 6472 ulong reason; 6473 6474 reason = vmcs_read(EXI_REASON); 6475 report("Invalid MSR load", reason == (0x80000000u | VMX_FAIL_MSR)); 6476 return VMX_TEST_VMEXIT; 6477 } 6478 6479 6480 #define TEST(name) { #name, .v2 = name } 6481 6482 /* name/init/guest_main/exit_handler/syscall_handler/guest_regs */ 6483 struct vmx_test vmx_tests[] = { 6484 { "null", NULL, basic_guest_main, basic_exit_handler, NULL, {0} }, 6485 { "vmenter", NULL, vmenter_main, vmenter_exit_handler, NULL, {0} }, 6486 { "preemption timer", preemption_timer_init, preemption_timer_main, 6487 preemption_timer_exit_handler, NULL, {0} }, 6488 { "control field PAT", test_ctrl_pat_init, test_ctrl_pat_main, 6489 test_ctrl_pat_exit_handler, NULL, {0} }, 6490 { "control field EFER", test_ctrl_efer_init, test_ctrl_efer_main, 6491 test_ctrl_efer_exit_handler, NULL, {0} }, 6492 { "CR shadowing", NULL, cr_shadowing_main, 6493 cr_shadowing_exit_handler, NULL, {0} }, 6494 { "I/O bitmap", iobmp_init, iobmp_main, iobmp_exit_handler, 6495 NULL, {0} }, 6496 { "instruction intercept", insn_intercept_init, insn_intercept_main, 6497 insn_intercept_exit_handler, NULL, {0} }, 6498 { "EPT A/D disabled", ept_init, ept_main, ept_exit_handler, NULL, {0} }, 6499 { "EPT A/D enabled", eptad_init, eptad_main, eptad_exit_handler, NULL, {0} }, 6500 { "PML", pml_init, pml_main, pml_exit_handler, NULL, {0} }, 6501 { "VPID", vpid_init, vpid_main, vpid_exit_handler, NULL, {0} }, 6502 { "interrupt", interrupt_init, interrupt_main, 6503 interrupt_exit_handler, NULL, {0} }, 6504 { "debug controls", dbgctls_init, dbgctls_main, dbgctls_exit_handler, 6505 NULL, {0} }, 6506 { "MSR switch", msr_switch_init, msr_switch_main, 6507 msr_switch_exit_handler, NULL, {0}, msr_switch_entry_failure }, 6508 { "vmmcall", vmmcall_init, vmmcall_main, vmmcall_exit_handler, NULL, {0} }, 6509 { "disable RDTSCP", disable_rdtscp_init, disable_rdtscp_main, 6510 disable_rdtscp_exit_handler, NULL, {0} }, 6511 { "int3", int3_init, int3_guest_main, int3_exit_handler, NULL, {0} }, 6512 { "into", into_init, into_guest_main, into_exit_handler, NULL, {0} }, 6513 { "exit_monitor_from_l2_test", NULL, exit_monitor_from_l2_main, 6514 exit_monitor_from_l2_handler, NULL, {0} }, 6515 { "invalid_msr", invalid_msr_init, invalid_msr_main, 6516 invalid_msr_exit_handler, NULL, {0}, invalid_msr_entry_failure}, 6517 /* Basic V2 tests. */ 6518 TEST(v2_null_test), 6519 TEST(v2_multiple_entries_test), 6520 TEST(fixture_test_case1), 6521 TEST(fixture_test_case2), 6522 /* Opcode tests. */ 6523 TEST(invvpid_test_v2), 6524 /* VM-entry tests */ 6525 TEST(vmx_controls_test), 6526 TEST(vmentry_movss_shadow_test), 6527 /* APICv tests */ 6528 TEST(vmx_eoi_bitmap_ioapic_scan_test), 6529 TEST(vmx_hlt_with_rvi_test), 6530 /* APIC pass-through tests */ 6531 TEST(vmx_apic_passthrough_test), 6532 TEST(vmx_apic_passthrough_thread_test), 6533 /* VMCS Shadowing tests */ 6534 TEST(vmx_vmcs_shadow_test), 6535 /* Regression tests */ 6536 TEST(vmx_cr_load_test), 6537 TEST(vmx_nm_test), 6538 TEST(vmx_db_test), 6539 TEST(vmx_nmi_window_test), 6540 TEST(vmx_intr_window_test), 6541 TEST(vmx_pending_event_test), 6542 TEST(vmx_pending_event_hlt_test), 6543 TEST(vmx_store_tsc_test), 6544 /* EPT access tests. */ 6545 TEST(ept_access_test_not_present), 6546 TEST(ept_access_test_read_only), 6547 TEST(ept_access_test_write_only), 6548 TEST(ept_access_test_read_write), 6549 TEST(ept_access_test_execute_only), 6550 TEST(ept_access_test_read_execute), 6551 TEST(ept_access_test_write_execute), 6552 TEST(ept_access_test_read_write_execute), 6553 TEST(ept_access_test_reserved_bits), 6554 TEST(ept_access_test_ignored_bits), 6555 TEST(ept_access_test_paddr_not_present_ad_disabled), 6556 TEST(ept_access_test_paddr_not_present_ad_enabled), 6557 TEST(ept_access_test_paddr_read_only_ad_disabled), 6558 TEST(ept_access_test_paddr_read_only_ad_enabled), 6559 TEST(ept_access_test_paddr_read_write), 6560 TEST(ept_access_test_paddr_read_write_execute), 6561 TEST(ept_access_test_paddr_read_execute_ad_disabled), 6562 TEST(ept_access_test_paddr_read_execute_ad_enabled), 6563 TEST(ept_access_test_paddr_not_present_page_fault), 6564 TEST(ept_access_test_force_2m_page), 6565 { NULL, NULL, NULL, NULL, NULL, {0} }, 6566 }; 6567