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_vmlaunch(u32 xerror, bool xfail) 3338 { 3339 bool success = vmlaunch_succeeds(); 3340 u32 vmx_inst_err; 3341 3342 report_xfail("vmlaunch %s", xfail, success == !xerror, 3343 !xerror ? "succeeds" : "fails"); 3344 if (!success && xerror) { 3345 vmx_inst_err = vmcs_read(VMX_INST_ERROR); 3346 report("VMX inst error is %d (actual %d)", 3347 vmx_inst_err == xerror, xerror, vmx_inst_err); 3348 } 3349 } 3350 3351 static void test_vmx_invalid_controls(bool xfail) 3352 { 3353 test_vmx_vmlaunch(VMXERR_ENTRY_INVALID_CONTROL_FIELD, xfail); 3354 } 3355 3356 static void test_vmx_valid_controls(bool xfail) 3357 { 3358 test_vmx_vmlaunch(0, xfail); 3359 } 3360 3361 /* 3362 * Test a particular value of a VM-execution control bit, if the value 3363 * is required or if the value is zero. 3364 */ 3365 static void test_rsvd_ctl_bit_value(const char *name, union vmx_ctrl_msr msr, 3366 enum Encoding encoding, unsigned bit, 3367 unsigned val) 3368 { 3369 u32 mask = 1u << bit; 3370 bool expected; 3371 u32 controls; 3372 3373 if (msr.set & mask) 3374 TEST_ASSERT(msr.clr & mask); 3375 3376 /* 3377 * We can't arbitrarily turn on a control bit, because it may 3378 * introduce dependencies on other VMCS fields. So, we only 3379 * test turning on bits that have a required setting. 3380 */ 3381 if (val && (msr.clr & mask) && !(msr.set & mask)) 3382 return; 3383 3384 report_prefix_pushf("%s %s bit %d", 3385 val ? "Set" : "Clear", name, bit); 3386 3387 controls = vmcs_read(encoding); 3388 if (val) { 3389 vmcs_write(encoding, msr.set | mask); 3390 expected = (msr.clr & mask); 3391 } else { 3392 vmcs_write(encoding, msr.set & ~mask); 3393 expected = !(msr.set & mask); 3394 } 3395 if (expected) 3396 test_vmx_valid_controls(false); 3397 else 3398 test_vmx_invalid_controls(false); 3399 vmcs_write(encoding, controls); 3400 report_prefix_pop(); 3401 } 3402 3403 /* 3404 * Test reserved values of a VM-execution control bit, based on the 3405 * allowed bit settings from the corresponding VMX capability MSR. 3406 */ 3407 static void test_rsvd_ctl_bit(const char *name, union vmx_ctrl_msr msr, 3408 enum Encoding encoding, unsigned bit) 3409 { 3410 test_rsvd_ctl_bit_value(name, msr, encoding, bit, 0); 3411 test_rsvd_ctl_bit_value(name, msr, encoding, bit, 1); 3412 } 3413 3414 /* 3415 * Reserved bits in the pin-based VM-execution controls must be set 3416 * properly. Software may consult the VMX capability MSRs to determine 3417 * the proper settings. 3418 * [Intel SDM] 3419 */ 3420 static void test_pin_based_ctls(void) 3421 { 3422 unsigned bit; 3423 3424 printf("%s: %lx\n", basic.ctrl ? "MSR_IA32_VMX_TRUE_PIN" : 3425 "MSR_IA32_VMX_PINBASED_CTLS", ctrl_pin_rev.val); 3426 for (bit = 0; bit < 32; bit++) 3427 test_rsvd_ctl_bit("pin-based controls", 3428 ctrl_pin_rev, PIN_CONTROLS, bit); 3429 } 3430 3431 /* 3432 * Reserved bits in the primary processor-based VM-execution controls 3433 * must be set properly. Software may consult the VMX capability MSRs 3434 * to determine the proper settings. 3435 * [Intel SDM] 3436 */ 3437 static void test_primary_processor_based_ctls(void) 3438 { 3439 unsigned bit; 3440 3441 printf("\n%s: %lx\n", basic.ctrl ? "MSR_IA32_VMX_TRUE_PROC" : 3442 "MSR_IA32_VMX_PROCBASED_CTLS", ctrl_cpu_rev[0].val); 3443 for (bit = 0; bit < 32; bit++) 3444 test_rsvd_ctl_bit("primary processor-based controls", 3445 ctrl_cpu_rev[0], CPU_EXEC_CTRL0, bit); 3446 } 3447 3448 /* 3449 * If the "activate secondary controls" primary processor-based 3450 * VM-execution control is 1, reserved bits in the secondary 3451 * processor-based VM-execution controls must be cleared. Software may 3452 * consult the VMX capability MSRs to determine which bits are 3453 * reserved. 3454 * If the "activate secondary controls" primary processor-based 3455 * VM-execution control is 0 (or if the processor does not support the 3456 * 1-setting of that control), no checks are performed on the 3457 * secondary processor-based VM-execution controls. 3458 * [Intel SDM] 3459 */ 3460 static void test_secondary_processor_based_ctls(void) 3461 { 3462 u32 primary; 3463 u32 secondary; 3464 unsigned bit; 3465 3466 if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY)) 3467 return; 3468 3469 primary = vmcs_read(CPU_EXEC_CTRL0); 3470 secondary = vmcs_read(CPU_EXEC_CTRL1); 3471 3472 vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY); 3473 printf("\nMSR_IA32_VMX_PROCBASED_CTLS2: %lx\n", ctrl_cpu_rev[1].val); 3474 for (bit = 0; bit < 32; bit++) 3475 test_rsvd_ctl_bit("secondary processor-based controls", 3476 ctrl_cpu_rev[1], CPU_EXEC_CTRL1, bit); 3477 3478 /* 3479 * When the "activate secondary controls" VM-execution control 3480 * is clear, there are no checks on the secondary controls. 3481 */ 3482 vmcs_write(CPU_EXEC_CTRL0, primary & ~CPU_SECONDARY); 3483 vmcs_write(CPU_EXEC_CTRL1, ~0); 3484 report("Secondary processor-based controls ignored", 3485 vmlaunch_succeeds()); 3486 vmcs_write(CPU_EXEC_CTRL1, secondary); 3487 vmcs_write(CPU_EXEC_CTRL0, primary); 3488 } 3489 3490 static void try_cr3_target_count(unsigned i, unsigned max) 3491 { 3492 report_prefix_pushf("CR3 target count 0x%x", i); 3493 vmcs_write(CR3_TARGET_COUNT, i); 3494 if (i <= max) 3495 test_vmx_valid_controls(false); 3496 else 3497 test_vmx_invalid_controls(false); 3498 report_prefix_pop(); 3499 } 3500 3501 /* 3502 * The CR3-target count must not be greater than 4. Future processors 3503 * may support a different number of CR3-target values. Software 3504 * should read the VMX capability MSR IA32_VMX_MISC to determine the 3505 * number of values supported. 3506 * [Intel SDM] 3507 */ 3508 static void test_cr3_targets(void) 3509 { 3510 unsigned supported_targets = (rdmsr(MSR_IA32_VMX_MISC) >> 16) & 0x1ff; 3511 u32 cr3_targets = vmcs_read(CR3_TARGET_COUNT); 3512 unsigned i; 3513 3514 printf("\nSupported CR3 targets: %d\n", supported_targets); 3515 TEST_ASSERT(supported_targets <= 256); 3516 3517 try_cr3_target_count(-1u, supported_targets); 3518 try_cr3_target_count(0x80000000, supported_targets); 3519 try_cr3_target_count(0x7fffffff, supported_targets); 3520 for (i = 0; i <= supported_targets + 1; i++) 3521 try_cr3_target_count(i, supported_targets); 3522 vmcs_write(CR3_TARGET_COUNT, cr3_targets); 3523 } 3524 3525 /* 3526 * Test a particular address setting in the VMCS 3527 */ 3528 static void test_vmcs_addr(const char *name, 3529 enum Encoding encoding, 3530 u64 align, 3531 bool ignored, 3532 bool xfail_beyond_mapped_ram, 3533 u64 addr) 3534 { 3535 bool xfail = 3536 (xfail_beyond_mapped_ram && 3537 addr > fwcfg_get_u64(FW_CFG_RAM_SIZE) - align && 3538 addr < (1ul << cpuid_maxphyaddr())); 3539 3540 report_prefix_pushf("%s = %lx", name, addr); 3541 vmcs_write(encoding, addr); 3542 if (ignored || (IS_ALIGNED(addr, align) && 3543 addr < (1ul << cpuid_maxphyaddr()))) 3544 test_vmx_valid_controls(xfail); 3545 else 3546 test_vmx_invalid_controls(xfail); 3547 report_prefix_pop(); 3548 xfail = false; 3549 } 3550 3551 /* 3552 * Test interesting values for a VMCS address 3553 */ 3554 static void test_vmcs_addr_values(const char *name, 3555 enum Encoding encoding, 3556 u64 align, 3557 bool ignored, 3558 bool xfail_beyond_mapped_ram, 3559 u32 bit_start, u32 bit_end) 3560 { 3561 unsigned i; 3562 u64 orig_val = vmcs_read(encoding); 3563 3564 for (i = bit_start; i <= bit_end; i++) 3565 test_vmcs_addr(name, encoding, align, ignored, 3566 xfail_beyond_mapped_ram, 1ul << i); 3567 3568 test_vmcs_addr(name, encoding, align, ignored, 3569 xfail_beyond_mapped_ram, PAGE_SIZE - 1); 3570 test_vmcs_addr(name, encoding, align, ignored, 3571 xfail_beyond_mapped_ram, PAGE_SIZE); 3572 test_vmcs_addr(name, encoding, align, ignored, 3573 xfail_beyond_mapped_ram, 3574 (1ul << cpuid_maxphyaddr()) - PAGE_SIZE); 3575 test_vmcs_addr(name, encoding, align, ignored, 3576 xfail_beyond_mapped_ram, -1ul); 3577 3578 vmcs_write(encoding, orig_val); 3579 } 3580 3581 /* 3582 * Test a physical address reference in the VMCS, when the corresponding 3583 * feature is enabled and when the corresponding feature is disabled. 3584 */ 3585 static void test_vmcs_addr_reference(u32 control_bit, enum Encoding field, 3586 const char *field_name, 3587 const char *control_name, u64 align, 3588 bool xfail_beyond_mapped_ram, 3589 bool control_primary) 3590 { 3591 u32 primary = vmcs_read(CPU_EXEC_CTRL0); 3592 u32 secondary = vmcs_read(CPU_EXEC_CTRL1); 3593 u64 page_addr; 3594 3595 if (control_primary) { 3596 if (!(ctrl_cpu_rev[0].clr & control_bit)) 3597 return; 3598 } else { 3599 if (!(ctrl_cpu_rev[1].clr & control_bit)) 3600 return; 3601 } 3602 3603 page_addr = vmcs_read(field); 3604 3605 report_prefix_pushf("%s enabled", control_name); 3606 if (control_primary) { 3607 vmcs_write(CPU_EXEC_CTRL0, primary | control_bit); 3608 } else { 3609 vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY); 3610 vmcs_write(CPU_EXEC_CTRL1, secondary | control_bit); 3611 } 3612 3613 test_vmcs_addr_values(field_name, field, align, false, 3614 xfail_beyond_mapped_ram, 0, 63); 3615 report_prefix_pop(); 3616 3617 report_prefix_pushf("%s disabled", control_name); 3618 if (control_primary) { 3619 vmcs_write(CPU_EXEC_CTRL0, primary & ~control_bit); 3620 } else { 3621 vmcs_write(CPU_EXEC_CTRL0, primary & ~CPU_SECONDARY); 3622 vmcs_write(CPU_EXEC_CTRL1, secondary & ~control_bit); 3623 } 3624 3625 test_vmcs_addr_values(field_name, field, align, true, false, 0, 63); 3626 report_prefix_pop(); 3627 3628 vmcs_write(field, page_addr); 3629 vmcs_write(CPU_EXEC_CTRL0, primary); 3630 } 3631 3632 /* 3633 * If the "use I/O bitmaps" VM-execution control is 1, bits 11:0 of 3634 * each I/O-bitmap address must be 0. Neither address should set any 3635 * bits beyond the processor's physical-address width. 3636 * [Intel SDM] 3637 */ 3638 static void test_io_bitmaps(void) 3639 { 3640 test_vmcs_addr_reference(CPU_IO_BITMAP, IO_BITMAP_A, 3641 "I/O bitmap A", "Use I/O bitmaps", 3642 PAGE_SIZE, false, true); 3643 test_vmcs_addr_reference(CPU_IO_BITMAP, IO_BITMAP_B, 3644 "I/O bitmap B", "Use I/O bitmaps", 3645 PAGE_SIZE, false, true); 3646 } 3647 3648 /* 3649 * If the "use MSR bitmaps" VM-execution control is 1, bits 11:0 of 3650 * the MSR-bitmap address must be 0. The address should not set any 3651 * bits beyond the processor's physical-address width. 3652 * [Intel SDM] 3653 */ 3654 static void test_msr_bitmap(void) 3655 { 3656 test_vmcs_addr_reference(CPU_MSR_BITMAP, MSR_BITMAP, 3657 "MSR bitmap", "Use MSR bitmaps", 3658 PAGE_SIZE, false, true); 3659 } 3660 3661 /* 3662 * If the "use TPR shadow" VM-execution control is 1, the virtual-APIC 3663 * address must satisfy the following checks: 3664 * - Bits 11:0 of the address must be 0. 3665 * - The address should not set any bits beyond the processor's 3666 * physical-address width. 3667 * [Intel SDM] 3668 */ 3669 static void test_apic_virt_addr(void) 3670 { 3671 test_vmcs_addr_reference(CPU_TPR_SHADOW, APIC_VIRT_ADDR, 3672 "virtual-APIC address", "Use TPR shadow", 3673 PAGE_SIZE, true, true); 3674 } 3675 3676 /* 3677 * If the "virtualize APIC-accesses" VM-execution control is 1, the 3678 * APIC-access address must satisfy the following checks: 3679 * - Bits 11:0 of the address must be 0. 3680 * - The address should not set any bits beyond the processor's 3681 * physical-address width. 3682 * [Intel SDM] 3683 */ 3684 static void test_apic_access_addr(void) 3685 { 3686 void *apic_access_page = alloc_page(); 3687 3688 vmcs_write(APIC_ACCS_ADDR, virt_to_phys(apic_access_page)); 3689 3690 test_vmcs_addr_reference(CPU_VIRT_APIC_ACCESSES, APIC_ACCS_ADDR, 3691 "APIC-access address", 3692 "virtualize APIC-accesses", PAGE_SIZE, 3693 false, false); 3694 } 3695 3696 static bool set_bit_pattern(u8 mask, u32 *secondary) 3697 { 3698 u8 i; 3699 bool flag = false; 3700 u32 test_bits[3] = { 3701 CPU_VIRT_X2APIC, 3702 CPU_APIC_REG_VIRT, 3703 CPU_VINTD 3704 }; 3705 3706 for (i = 0; i < ARRAY_SIZE(test_bits); i++) { 3707 if ((mask & (1u << i)) && 3708 (ctrl_cpu_rev[1].clr & test_bits[i])) { 3709 *secondary |= test_bits[i]; 3710 flag = true; 3711 } 3712 } 3713 3714 return (flag); 3715 } 3716 3717 /* 3718 * If the "use TPR shadow" VM-execution control is 0, the following 3719 * VM-execution controls must also be 0: 3720 * - virtualize x2APIC mode 3721 * - APIC-register virtualization 3722 * - virtual-interrupt delivery 3723 * [Intel SDM] 3724 * 3725 * 2. If the "virtualize x2APIC mode" VM-execution control is 1, the 3726 * "virtualize APIC accesses" VM-execution control must be 0. 3727 * [Intel SDM] 3728 */ 3729 static void test_apic_virtual_ctls(void) 3730 { 3731 u32 saved_primary = vmcs_read(CPU_EXEC_CTRL0); 3732 u32 saved_secondary = vmcs_read(CPU_EXEC_CTRL1); 3733 u32 primary = saved_primary; 3734 u32 secondary = saved_secondary; 3735 bool ctrl = false; 3736 char str[10] = "disabled"; 3737 u8 i = 0, j; 3738 3739 /* 3740 * First test 3741 */ 3742 if (!((ctrl_cpu_rev[0].clr & (CPU_SECONDARY | CPU_TPR_SHADOW)) == 3743 (CPU_SECONDARY | CPU_TPR_SHADOW))) 3744 return; 3745 3746 primary |= CPU_SECONDARY; 3747 primary &= ~CPU_TPR_SHADOW; 3748 vmcs_write(CPU_EXEC_CTRL0, primary); 3749 3750 while (1) { 3751 for (j = 1; j < 8; j++) { 3752 secondary &= ~(CPU_VIRT_X2APIC | CPU_APIC_REG_VIRT | CPU_VINTD); 3753 if (primary & CPU_TPR_SHADOW) { 3754 ctrl = true; 3755 } else { 3756 if (! set_bit_pattern(j, &secondary)) 3757 ctrl = true; 3758 else 3759 ctrl = false; 3760 } 3761 3762 vmcs_write(CPU_EXEC_CTRL1, secondary); 3763 report_prefix_pushf("Use TPR shadow %s, virtualize x2APIC mode %s, APIC-register virtualization %s, virtual-interrupt delivery %s", 3764 str, (secondary & CPU_VIRT_X2APIC) ? "enabled" : "disabled", (secondary & CPU_APIC_REG_VIRT) ? "enabled" : "disabled", (secondary & CPU_VINTD) ? "enabled" : "disabled"); 3765 if (ctrl) 3766 test_vmx_valid_controls(false); 3767 else 3768 test_vmx_invalid_controls(false); 3769 report_prefix_pop(); 3770 } 3771 3772 if (i == 1) 3773 break; 3774 i++; 3775 3776 primary |= CPU_TPR_SHADOW; 3777 vmcs_write(CPU_EXEC_CTRL0, primary); 3778 strcpy(str, "enabled"); 3779 } 3780 3781 /* 3782 * Second test 3783 */ 3784 u32 apic_virt_ctls = (CPU_VIRT_X2APIC | CPU_VIRT_APIC_ACCESSES); 3785 3786 primary = saved_primary; 3787 secondary = saved_secondary; 3788 if (!((ctrl_cpu_rev[1].clr & apic_virt_ctls) == apic_virt_ctls)) 3789 return; 3790 3791 vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY); 3792 secondary &= ~CPU_VIRT_APIC_ACCESSES; 3793 vmcs_write(CPU_EXEC_CTRL1, secondary & ~CPU_VIRT_X2APIC); 3794 report_prefix_pushf("Virtualize x2APIC mode disabled; virtualize APIC access disabled"); 3795 test_vmx_valid_controls(false); 3796 report_prefix_pop(); 3797 3798 vmcs_write(CPU_EXEC_CTRL1, secondary | CPU_VIRT_APIC_ACCESSES); 3799 report_prefix_pushf("Virtualize x2APIC mode disabled; virtualize APIC access enabled"); 3800 test_vmx_valid_controls(false); 3801 report_prefix_pop(); 3802 3803 vmcs_write(CPU_EXEC_CTRL1, secondary | CPU_VIRT_X2APIC); 3804 report_prefix_pushf("Virtualize x2APIC mode enabled; virtualize APIC access enabled"); 3805 test_vmx_invalid_controls(false); 3806 report_prefix_pop(); 3807 3808 vmcs_write(CPU_EXEC_CTRL1, secondary & ~CPU_VIRT_APIC_ACCESSES); 3809 report_prefix_pushf("Virtualize x2APIC mode enabled; virtualize APIC access disabled"); 3810 test_vmx_valid_controls(false); 3811 report_prefix_pop(); 3812 3813 vmcs_write(CPU_EXEC_CTRL0, saved_primary); 3814 vmcs_write(CPU_EXEC_CTRL1, saved_secondary); 3815 } 3816 3817 /* 3818 * If the "virtual-interrupt delivery" VM-execution control is 1, the 3819 * "external-interrupt exiting" VM-execution control must be 1. 3820 * [Intel SDM] 3821 */ 3822 static void test_virtual_intr_ctls(void) 3823 { 3824 u32 saved_primary = vmcs_read(CPU_EXEC_CTRL0); 3825 u32 saved_secondary = vmcs_read(CPU_EXEC_CTRL1); 3826 u32 saved_pin = vmcs_read(PIN_CONTROLS); 3827 u32 primary = saved_primary; 3828 u32 secondary = saved_secondary; 3829 u32 pin = saved_pin; 3830 3831 if (!((ctrl_cpu_rev[1].clr & CPU_VINTD) && 3832 (ctrl_pin_rev.clr & PIN_EXTINT))) 3833 return; 3834 3835 vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY | CPU_TPR_SHADOW); 3836 vmcs_write(CPU_EXEC_CTRL1, secondary & ~CPU_VINTD); 3837 vmcs_write(PIN_CONTROLS, pin & ~PIN_EXTINT); 3838 report_prefix_pushf("Virtualize interrupt-delivery disabled; external-interrupt exiting disabled"); 3839 test_vmx_valid_controls(false); 3840 report_prefix_pop(); 3841 3842 vmcs_write(CPU_EXEC_CTRL1, secondary | CPU_VINTD); 3843 report_prefix_pushf("Virtualize interrupt-delivery enabled; external-interrupt exiting disabled"); 3844 test_vmx_invalid_controls(false); 3845 report_prefix_pop(); 3846 3847 vmcs_write(PIN_CONTROLS, pin | PIN_EXTINT); 3848 report_prefix_pushf("Virtualize interrupt-delivery enabled; external-interrupt exiting enabled"); 3849 test_vmx_valid_controls(false); 3850 report_prefix_pop(); 3851 3852 vmcs_write(PIN_CONTROLS, pin & ~PIN_EXTINT); 3853 report_prefix_pushf("Virtualize interrupt-delivery enabled; external-interrupt exiting disabled"); 3854 test_vmx_invalid_controls(false); 3855 report_prefix_pop(); 3856 3857 vmcs_write(CPU_EXEC_CTRL0, saved_primary); 3858 vmcs_write(CPU_EXEC_CTRL1, saved_secondary); 3859 vmcs_write(PIN_CONTROLS, saved_pin); 3860 } 3861 3862 static void test_pi_desc_addr(u64 addr, bool ctrl) 3863 { 3864 vmcs_write(POSTED_INTR_DESC_ADDR, addr); 3865 report_prefix_pushf("Process-posted-interrupts enabled; posted-interrupt-descriptor-address 0x%lx", addr); 3866 if (ctrl) 3867 test_vmx_valid_controls(false); 3868 else 3869 test_vmx_invalid_controls(false); 3870 report_prefix_pop(); 3871 } 3872 3873 /* 3874 * If the “process posted interrupts†VM-execution control is 1, the 3875 * following must be true: 3876 * 3877 * - The “virtual-interrupt delivery†VM-execution control is 1. 3878 * - The “acknowledge interrupt on exit†VM-exit control is 1. 3879 * - The posted-interrupt notification vector has a value in the 3880 * - range 0–255 (bits 15:8 are all 0). 3881 * - Bits 5:0 of the posted-interrupt descriptor address are all 0. 3882 * - The posted-interrupt descriptor address does not set any bits 3883 * beyond the processor's physical-address width. 3884 * [Intel SDM] 3885 */ 3886 static void test_posted_intr(void) 3887 { 3888 u32 saved_primary = vmcs_read(CPU_EXEC_CTRL0); 3889 u32 saved_secondary = vmcs_read(CPU_EXEC_CTRL1); 3890 u32 saved_pin = vmcs_read(PIN_CONTROLS); 3891 u32 exit_ctl_saved = vmcs_read(EXI_CONTROLS); 3892 u32 primary = saved_primary; 3893 u32 secondary = saved_secondary; 3894 u32 pin = saved_pin; 3895 u32 exit_ctl = exit_ctl_saved; 3896 u16 vec; 3897 int i; 3898 3899 if (!((ctrl_pin_rev.clr & PIN_POST_INTR) && 3900 (ctrl_cpu_rev[1].clr & CPU_VINTD) && 3901 (ctrl_exit_rev.clr & EXI_INTA))) 3902 return; 3903 3904 vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY | CPU_TPR_SHADOW); 3905 3906 /* 3907 * Test virtual-interrupt-delivery and acknowledge-interrupt-on-exit 3908 */ 3909 pin |= PIN_POST_INTR; 3910 vmcs_write(PIN_CONTROLS, pin); 3911 secondary &= ~CPU_VINTD; 3912 vmcs_write(CPU_EXEC_CTRL1, secondary); 3913 report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery disabled"); 3914 test_vmx_invalid_controls(false); 3915 report_prefix_pop(); 3916 3917 secondary |= CPU_VINTD; 3918 vmcs_write(CPU_EXEC_CTRL1, secondary); 3919 report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery enabled"); 3920 test_vmx_invalid_controls(false); 3921 report_prefix_pop(); 3922 3923 exit_ctl &= ~EXI_INTA; 3924 vmcs_write(EXI_CONTROLS, exit_ctl); 3925 report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery enabled; acknowledge-interrupt-on-exit disabled"); 3926 test_vmx_invalid_controls(false); 3927 report_prefix_pop(); 3928 3929 exit_ctl |= EXI_INTA; 3930 vmcs_write(EXI_CONTROLS, exit_ctl); 3931 report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery enabled; acknowledge-interrupt-on-exit enabled"); 3932 test_vmx_valid_controls(false); 3933 report_prefix_pop(); 3934 3935 secondary &= ~CPU_VINTD; 3936 vmcs_write(CPU_EXEC_CTRL1, secondary); 3937 report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery disabled; acknowledge-interrupt-on-exit enabled"); 3938 test_vmx_invalid_controls(false); 3939 report_prefix_pop(); 3940 3941 secondary |= CPU_VINTD; 3942 vmcs_write(CPU_EXEC_CTRL1, secondary); 3943 report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery enabled; acknowledge-interrupt-on-exit enabled"); 3944 test_vmx_valid_controls(false); 3945 report_prefix_pop(); 3946 3947 /* 3948 * Test posted-interrupt notification vector 3949 */ 3950 for (i = 0; i < 8; i++) { 3951 vec = (1ul << i); 3952 vmcs_write(PINV, vec); 3953 report_prefix_pushf("Process-posted-interrupts enabled; posted-interrupt-notification-vector %u", vec); 3954 test_vmx_valid_controls(false); 3955 report_prefix_pop(); 3956 } 3957 for (i = 8; i < 16; i++) { 3958 vec = (1ul << i); 3959 vmcs_write(PINV, vec); 3960 report_prefix_pushf("Process-posted-interrupts enabled; posted-interrupt-notification-vector %u", vec); 3961 test_vmx_invalid_controls(false); 3962 report_prefix_pop(); 3963 } 3964 3965 vec &= ~(0xff << 8); 3966 vmcs_write(PINV, vec); 3967 report_prefix_pushf("Process-posted-interrupts enabled; posted-interrupt-notification-vector %u", vec); 3968 test_vmx_valid_controls(false); 3969 report_prefix_pop(); 3970 3971 /* 3972 * Test posted-interrupt descriptor addresss 3973 */ 3974 for (i = 0; i < 6; i++) { 3975 test_pi_desc_addr(1ul << i, false); 3976 } 3977 3978 test_pi_desc_addr(0xf0, false); 3979 test_pi_desc_addr(0xff, false); 3980 test_pi_desc_addr(0x0f, false); 3981 test_pi_desc_addr(0x8000, true); 3982 test_pi_desc_addr(0x00, true); 3983 test_pi_desc_addr(0xc000, true); 3984 3985 test_vmcs_addr_values("process-posted interrupts", 3986 POSTED_INTR_DESC_ADDR, 64, 3987 false, false, 0, 63); 3988 3989 vmcs_write(CPU_EXEC_CTRL0, saved_primary); 3990 vmcs_write(CPU_EXEC_CTRL1, saved_secondary); 3991 vmcs_write(PIN_CONTROLS, saved_pin); 3992 } 3993 3994 static void test_apic_ctls(void) 3995 { 3996 test_apic_virt_addr(); 3997 test_apic_access_addr(); 3998 test_apic_virtual_ctls(); 3999 test_virtual_intr_ctls(); 4000 test_posted_intr(); 4001 } 4002 4003 /* 4004 * If the “enable VPID†VM-execution control is 1, the value of the 4005 * of the VPID VM-execution control field must not be 0000H. 4006 * [Intel SDM] 4007 */ 4008 static void test_vpid(void) 4009 { 4010 u32 saved_primary = vmcs_read(CPU_EXEC_CTRL0); 4011 u32 saved_secondary = vmcs_read(CPU_EXEC_CTRL1); 4012 u16 vpid = 0x0000; 4013 int i; 4014 4015 if (!((ctrl_cpu_rev[0].clr & CPU_SECONDARY) && 4016 (ctrl_cpu_rev[1].clr & CPU_VPID))) { 4017 test_skip("Secondary controls and/or VPID not supported"); 4018 return; 4019 } 4020 4021 vmcs_write(CPU_EXEC_CTRL0, saved_primary | CPU_SECONDARY); 4022 vmcs_write(CPU_EXEC_CTRL1, saved_secondary & ~CPU_VPID); 4023 vmcs_write(VPID, vpid); 4024 report_prefix_pushf("VPID disabled; VPID value %x", vpid); 4025 test_vmx_valid_controls(false); 4026 report_prefix_pop(); 4027 4028 vmcs_write(CPU_EXEC_CTRL1, saved_secondary | CPU_VPID); 4029 report_prefix_pushf("VPID enabled; VPID value %x", vpid); 4030 test_vmx_invalid_controls(false); 4031 report_prefix_pop(); 4032 4033 for (i = 0; i < 16; i++) { 4034 vpid = (short)1 << i;; 4035 vmcs_write(VPID, vpid); 4036 report_prefix_pushf("VPID enabled; VPID value %x", vpid); 4037 test_vmx_valid_controls(false); 4038 report_prefix_pop(); 4039 } 4040 4041 vmcs_write(CPU_EXEC_CTRL0, saved_primary); 4042 vmcs_write(CPU_EXEC_CTRL1, saved_secondary); 4043 } 4044 4045 static void set_vtpr(unsigned vtpr) 4046 { 4047 *(u32 *)phys_to_virt(vmcs_read(APIC_VIRT_ADDR) + APIC_TASKPRI) = vtpr; 4048 } 4049 4050 static void try_tpr_threshold_and_vtpr(unsigned threshold, unsigned vtpr) 4051 { 4052 bool valid = true; 4053 u32 primary = vmcs_read(CPU_EXEC_CTRL0); 4054 u32 secondary = vmcs_read(CPU_EXEC_CTRL1); 4055 4056 if ((primary & CPU_TPR_SHADOW) && 4057 (!(primary & CPU_SECONDARY) || 4058 !(secondary & (CPU_VINTD | CPU_VIRT_APIC_ACCESSES)))) 4059 valid = (threshold & 0xf) <= ((vtpr >> 4) & 0xf); 4060 4061 set_vtpr(vtpr); 4062 report_prefix_pushf("TPR threshold 0x%x, VTPR.class 0x%x", 4063 threshold, (vtpr >> 4) & 0xf); 4064 if (valid) 4065 test_vmx_valid_controls(false); 4066 else 4067 test_vmx_invalid_controls(false); 4068 report_prefix_pop(); 4069 } 4070 4071 static void test_invalid_event_injection(void) 4072 { 4073 u32 ent_intr_info_save = vmcs_read(ENT_INTR_INFO); 4074 u32 ent_intr_error_save = vmcs_read(ENT_INTR_ERROR); 4075 u32 ent_inst_len_save = vmcs_read(ENT_INST_LEN); 4076 u32 primary_save = vmcs_read(CPU_EXEC_CTRL0); 4077 u32 secondary_save = vmcs_read(CPU_EXEC_CTRL1); 4078 u64 guest_cr0_save = vmcs_read(GUEST_CR0); 4079 u32 ent_intr_info_base = INTR_INFO_VALID_MASK; 4080 u32 ent_intr_info, ent_intr_err, ent_intr_len; 4081 u32 cnt; 4082 4083 /* Setup */ 4084 report_prefix_push("invalid event injection"); 4085 vmcs_write(ENT_INTR_ERROR, 0x00000000); 4086 vmcs_write(ENT_INST_LEN, 0x00000001); 4087 4088 /* The field’s interruption type is not set to a reserved value. */ 4089 ent_intr_info = ent_intr_info_base | INTR_TYPE_RESERVED | DE_VECTOR; 4090 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4091 "RESERVED interruption type invalid [-]", 4092 ent_intr_info); 4093 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4094 test_vmx_invalid_controls(false); 4095 report_prefix_pop(); 4096 4097 ent_intr_info = ent_intr_info_base | INTR_TYPE_EXT_INTR | 4098 DE_VECTOR; 4099 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4100 "RESERVED interruption type invalid [+]", 4101 ent_intr_info); 4102 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4103 test_vmx_valid_controls(false); 4104 report_prefix_pop(); 4105 4106 /* If the interruption type is other event, the vector is 0. */ 4107 ent_intr_info = ent_intr_info_base | INTR_TYPE_OTHER_EVENT | DB_VECTOR; 4108 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4109 "(OTHER EVENT && vector != 0) invalid [-]", 4110 ent_intr_info); 4111 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4112 test_vmx_invalid_controls(false); 4113 report_prefix_pop(); 4114 4115 /* If the interruption type is NMI, the vector is 2 (negative case). */ 4116 ent_intr_info = ent_intr_info_base | INTR_TYPE_NMI_INTR | DE_VECTOR; 4117 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4118 "(NMI && vector != 2) invalid [-]", ent_intr_info); 4119 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4120 test_vmx_invalid_controls(false); 4121 report_prefix_pop(); 4122 4123 /* If the interruption type is NMI, the vector is 2 (positive case). */ 4124 ent_intr_info = ent_intr_info_base | INTR_TYPE_NMI_INTR | NMI_VECTOR; 4125 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4126 "(NMI && vector == 2) valid [+]", ent_intr_info); 4127 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4128 test_vmx_valid_controls(false); 4129 report_prefix_pop(); 4130 4131 /* 4132 * If the interruption type 4133 * is HW exception, the vector is at most 31. 4134 */ 4135 ent_intr_info = ent_intr_info_base | INTR_TYPE_HARD_EXCEPTION | 0x20; 4136 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4137 "(HW exception && vector > 31) invalid [-]", 4138 ent_intr_info); 4139 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4140 test_vmx_invalid_controls(false); 4141 report_prefix_pop(); 4142 4143 /* 4144 * deliver-error-code is 1 iff either 4145 * (a) the "unrestricted guest" VM-execution control is 0 4146 * (b) CR0.PE is set. 4147 */ 4148 4149 /* Assert that unrestricted guest is disabled or unsupported */ 4150 assert(!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) || 4151 !(secondary_save & CPU_URG)); 4152 4153 ent_intr_info = ent_intr_info_base | INTR_TYPE_HARD_EXCEPTION | 4154 GP_VECTOR; 4155 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4156 "error code <-> (!URG || prot_mode) [-]", 4157 ent_intr_info); 4158 vmcs_write(GUEST_CR0, guest_cr0_save & ~X86_CR0_PE & ~X86_CR0_PG); 4159 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4160 test_vmx_invalid_controls(false); 4161 report_prefix_pop(); 4162 4163 ent_intr_info = ent_intr_info_base | INTR_INFO_DELIVER_CODE_MASK | 4164 INTR_TYPE_HARD_EXCEPTION | GP_VECTOR; 4165 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4166 "error code <-> (!URG || prot_mode) [+]", 4167 ent_intr_info); 4168 vmcs_write(GUEST_CR0, guest_cr0_save & ~X86_CR0_PE & ~X86_CR0_PG); 4169 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4170 test_vmx_valid_controls(false); 4171 report_prefix_pop(); 4172 4173 if (enable_unrestricted_guest()) 4174 goto skip_unrestricted_guest; 4175 4176 ent_intr_info = ent_intr_info_base | INTR_INFO_DELIVER_CODE_MASK | 4177 INTR_TYPE_HARD_EXCEPTION | GP_VECTOR; 4178 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4179 "error code <-> (!URG || prot_mode) [-]", 4180 ent_intr_info); 4181 vmcs_write(GUEST_CR0, guest_cr0_save & ~X86_CR0_PE & ~X86_CR0_PG); 4182 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4183 test_vmx_invalid_controls(false); 4184 report_prefix_pop(); 4185 4186 ent_intr_info = ent_intr_info_base | INTR_TYPE_HARD_EXCEPTION | 4187 GP_VECTOR; 4188 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4189 "error code <-> (!URG || prot_mode) [-]", 4190 ent_intr_info); 4191 vmcs_write(GUEST_CR0, guest_cr0_save | X86_CR0_PE); 4192 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4193 test_vmx_invalid_controls(false); 4194 report_prefix_pop(); 4195 4196 vmcs_write(CPU_EXEC_CTRL1, secondary_save); 4197 vmcs_write(CPU_EXEC_CTRL0, primary_save); 4198 4199 skip_unrestricted_guest: 4200 vmcs_write(GUEST_CR0, guest_cr0_save); 4201 4202 /* deliver-error-code is 1 iff the interruption type is HW exception */ 4203 report_prefix_push("error code <-> HW exception"); 4204 for (cnt = 0; cnt < 8; cnt++) { 4205 u32 exception_type_mask = cnt << 8; 4206 u32 deliver_error_code_mask = 4207 exception_type_mask != INTR_TYPE_HARD_EXCEPTION ? 4208 INTR_INFO_DELIVER_CODE_MASK : 0; 4209 4210 ent_intr_info = ent_intr_info_base | deliver_error_code_mask | 4211 exception_type_mask | GP_VECTOR; 4212 report_prefix_pushf("VM-entry intr info=0x%x [-]", 4213 ent_intr_info); 4214 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4215 test_vmx_invalid_controls(false); 4216 report_prefix_pop(); 4217 } 4218 report_prefix_pop(); 4219 4220 /* 4221 * deliver-error-code is 1 iff the the vector 4222 * indicates an exception that would normally deliver an error code 4223 */ 4224 report_prefix_push("error code <-> vector delivers error code"); 4225 for (cnt = 0; cnt < 32; cnt++) { 4226 bool has_error_code = false; 4227 u32 deliver_error_code_mask; 4228 4229 switch (cnt) { 4230 case DF_VECTOR: 4231 case TS_VECTOR: 4232 case NP_VECTOR: 4233 case SS_VECTOR: 4234 case GP_VECTOR: 4235 case PF_VECTOR: 4236 case AC_VECTOR: 4237 has_error_code = true; 4238 } 4239 4240 /* Negative case */ 4241 deliver_error_code_mask = has_error_code ? 4242 0 : 4243 INTR_INFO_DELIVER_CODE_MASK; 4244 ent_intr_info = ent_intr_info_base | deliver_error_code_mask | 4245 INTR_TYPE_HARD_EXCEPTION | cnt; 4246 report_prefix_pushf("VM-entry intr info=0x%x [-]", 4247 ent_intr_info); 4248 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4249 test_vmx_invalid_controls(false); 4250 report_prefix_pop(); 4251 4252 /* Positive case */ 4253 deliver_error_code_mask = has_error_code ? 4254 INTR_INFO_DELIVER_CODE_MASK : 4255 0; 4256 ent_intr_info = ent_intr_info_base | deliver_error_code_mask | 4257 INTR_TYPE_HARD_EXCEPTION | cnt; 4258 report_prefix_pushf("VM-entry intr info=0x%x [+]", 4259 ent_intr_info); 4260 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4261 test_vmx_valid_controls(false); 4262 report_prefix_pop(); 4263 } 4264 report_prefix_pop(); 4265 4266 /* Reserved bits in the field (30:12) are 0. */ 4267 report_prefix_push("reserved bits clear"); 4268 for (cnt = 12; cnt <= 30; cnt++) { 4269 ent_intr_info = ent_intr_info_base | 4270 INTR_INFO_DELIVER_CODE_MASK | 4271 INTR_TYPE_HARD_EXCEPTION | GP_VECTOR | 4272 (1U << cnt); 4273 report_prefix_pushf("VM-entry intr info=0x%x [-]", 4274 ent_intr_info); 4275 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4276 test_vmx_invalid_controls(false); 4277 report_prefix_pop(); 4278 } 4279 report_prefix_pop(); 4280 4281 /* 4282 * If deliver-error-code is 1 4283 * bits 31:15 of the VM-entry exception error-code field are 0. 4284 */ 4285 ent_intr_info = ent_intr_info_base | INTR_INFO_DELIVER_CODE_MASK | 4286 INTR_TYPE_HARD_EXCEPTION | GP_VECTOR; 4287 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4288 "VM-entry exception error code[31:15] clear", 4289 ent_intr_info); 4290 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4291 for (cnt = 15; cnt <= 31; cnt++) { 4292 ent_intr_err = 1U << cnt; 4293 report_prefix_pushf("VM-entry intr error=0x%x [-]", 4294 ent_intr_err); 4295 vmcs_write(ENT_INTR_ERROR, ent_intr_err); 4296 test_vmx_invalid_controls(false); 4297 report_prefix_pop(); 4298 } 4299 vmcs_write(ENT_INTR_ERROR, 0x00000000); 4300 report_prefix_pop(); 4301 4302 /* 4303 * If the interruption type is software interrupt, software exception, 4304 * or privileged software exception, the VM-entry instruction-length 4305 * field is in the range 0–15. 4306 */ 4307 4308 for (cnt = 0; cnt < 3; cnt++) { 4309 switch (cnt) { 4310 case 0: 4311 ent_intr_info = ent_intr_info_base | 4312 INTR_TYPE_SOFT_INTR; 4313 break; 4314 case 1: 4315 ent_intr_info = ent_intr_info_base | 4316 INTR_TYPE_SOFT_EXCEPTION; 4317 break; 4318 case 2: 4319 ent_intr_info = ent_intr_info_base | 4320 INTR_TYPE_PRIV_SW_EXCEPTION; 4321 break; 4322 } 4323 report_prefix_pushf("%s, VM-entry intr info=0x%x", 4324 "VM-entry instruction-length check", 4325 ent_intr_info); 4326 vmcs_write(ENT_INTR_INFO, ent_intr_info); 4327 4328 /* Instruction length set to -1 (0xFFFFFFFF) should fail */ 4329 ent_intr_len = -1; 4330 report_prefix_pushf("VM-entry intr length = 0x%x [-]", 4331 ent_intr_len); 4332 vmcs_write(ENT_INST_LEN, ent_intr_len); 4333 test_vmx_invalid_controls(false); 4334 report_prefix_pop(); 4335 4336 /* Instruction length set to 16 should fail */ 4337 ent_intr_len = 0x00000010; 4338 report_prefix_pushf("VM-entry intr length = 0x%x [-]", 4339 ent_intr_len); 4340 vmcs_write(ENT_INST_LEN, 0x00000010); 4341 test_vmx_invalid_controls(false); 4342 report_prefix_pop(); 4343 4344 report_prefix_pop(); 4345 } 4346 4347 /* Cleanup */ 4348 vmcs_write(ENT_INTR_INFO, ent_intr_info_save); 4349 vmcs_write(ENT_INTR_ERROR, ent_intr_error_save); 4350 vmcs_write(ENT_INST_LEN, ent_inst_len_save); 4351 vmcs_write(CPU_EXEC_CTRL0, primary_save); 4352 vmcs_write(CPU_EXEC_CTRL1, secondary_save); 4353 vmcs_write(GUEST_CR0, guest_cr0_save); 4354 report_prefix_pop(); 4355 } 4356 4357 /* 4358 * Test interesting vTPR values for a given TPR threshold. 4359 */ 4360 static void test_vtpr_values(unsigned threshold) 4361 { 4362 try_tpr_threshold_and_vtpr(threshold, (threshold - 1) << 4); 4363 try_tpr_threshold_and_vtpr(threshold, threshold << 4); 4364 try_tpr_threshold_and_vtpr(threshold, (threshold + 1) << 4); 4365 } 4366 4367 static void try_tpr_threshold(unsigned threshold) 4368 { 4369 bool valid = true; 4370 4371 u32 primary = vmcs_read(CPU_EXEC_CTRL0); 4372 u32 secondary = vmcs_read(CPU_EXEC_CTRL1); 4373 4374 if ((primary & CPU_TPR_SHADOW) && !((primary & CPU_SECONDARY) && 4375 (secondary & CPU_VINTD))) 4376 valid = !(threshold >> 4); 4377 4378 set_vtpr(-1); 4379 vmcs_write(TPR_THRESHOLD, threshold); 4380 report_prefix_pushf("TPR threshold 0x%x, VTPR.class 0xf", threshold); 4381 if (valid) 4382 test_vmx_valid_controls(false); 4383 else 4384 test_vmx_invalid_controls(false); 4385 report_prefix_pop(); 4386 4387 if (valid) 4388 test_vtpr_values(threshold); 4389 } 4390 4391 /* 4392 * Test interesting TPR threshold values. 4393 */ 4394 static void test_tpr_threshold_values(void) 4395 { 4396 unsigned i; 4397 4398 for (i = 0; i < 0x10; i++) 4399 try_tpr_threshold(i); 4400 for (i = 4; i < 32; i++) 4401 try_tpr_threshold(1u << i); 4402 try_tpr_threshold(-1u); 4403 try_tpr_threshold(0x7fffffff); 4404 } 4405 4406 /* 4407 * This test covers the following two VM entry checks: 4408 * 4409 * i) If the "use TPR shadow" VM-execution control is 1 and the 4410 * "virtual-interrupt delivery" VM-execution control is 0, bits 4411 * 31:4 of the TPR threshold VM-execution control field must 4412 be 0. 4413 * [Intel SDM] 4414 * 4415 * ii) If the "use TPR shadow" VM-execution control is 1, the 4416 * "virtual-interrupt delivery" VM-execution control is 0 4417 * and the "virtualize APIC accesses" VM-execution control 4418 * is 0, the value of bits 3:0 of the TPR threshold VM-execution 4419 * control field must not be greater than the value of bits 4420 * 7:4 of VTPR. 4421 * [Intel SDM] 4422 */ 4423 static void test_tpr_threshold(void) 4424 { 4425 u32 primary = vmcs_read(CPU_EXEC_CTRL0); 4426 void *virtual_apic_page; 4427 4428 if (!(ctrl_cpu_rev[0].clr & CPU_TPR_SHADOW)) 4429 return; 4430 4431 virtual_apic_page = alloc_page(); 4432 memset(virtual_apic_page, 0xff, PAGE_SIZE); 4433 vmcs_write(APIC_VIRT_ADDR, virt_to_phys(virtual_apic_page)); 4434 4435 vmcs_write(CPU_EXEC_CTRL0, primary & ~(CPU_TPR_SHADOW | CPU_SECONDARY)); 4436 report_prefix_pushf("Use TPR shadow disabled, secondary controls disabled"); 4437 test_tpr_threshold_values(); 4438 report_prefix_pop(); 4439 vmcs_write(CPU_EXEC_CTRL0, vmcs_read(CPU_EXEC_CTRL0) | CPU_TPR_SHADOW); 4440 report_prefix_pushf("Use TPR shadow enabled, secondary controls disabled"); 4441 test_tpr_threshold_values(); 4442 report_prefix_pop(); 4443 4444 if (!((ctrl_cpu_rev[0].clr & CPU_SECONDARY) && 4445 (ctrl_cpu_rev[1].clr & (CPU_VINTD | CPU_VIRT_APIC_ACCESSES)))) { 4446 vmcs_write(CPU_EXEC_CTRL0, primary); 4447 return; 4448 } 4449 4450 u32 secondary = vmcs_read(CPU_EXEC_CTRL1); 4451 4452 if (ctrl_cpu_rev[1].clr & CPU_VINTD) { 4453 vmcs_write(CPU_EXEC_CTRL1, CPU_VINTD); 4454 report_prefix_pushf("Use TPR shadow enabled; secondary controls disabled; virtual-interrupt delivery enabled; virtualize APIC accesses disabled"); 4455 test_tpr_threshold_values(); 4456 report_prefix_pop(); 4457 4458 vmcs_write(CPU_EXEC_CTRL0, 4459 vmcs_read(CPU_EXEC_CTRL0) | CPU_SECONDARY); 4460 report_prefix_pushf("Use TPR shadow enabled; secondary controls enabled; virtual-interrupt delivery enabled; virtualize APIC accesses disabled"); 4461 test_tpr_threshold_values(); 4462 report_prefix_pop(); 4463 } 4464 4465 if (ctrl_cpu_rev[1].clr & CPU_VIRT_APIC_ACCESSES) { 4466 vmcs_write(CPU_EXEC_CTRL0, 4467 vmcs_read(CPU_EXEC_CTRL0) & ~CPU_SECONDARY); 4468 vmcs_write(CPU_EXEC_CTRL1, CPU_VIRT_APIC_ACCESSES); 4469 report_prefix_pushf("Use TPR shadow enabled; secondary controls disabled; virtual-interrupt delivery enabled; virtualize APIC accesses enabled"); 4470 test_tpr_threshold_values(); 4471 report_prefix_pop(); 4472 4473 vmcs_write(CPU_EXEC_CTRL0, 4474 vmcs_read(CPU_EXEC_CTRL0) | CPU_SECONDARY); 4475 report_prefix_pushf("Use TPR shadow enabled; secondary controls enabled; virtual-interrupt delivery enabled; virtualize APIC accesses enabled"); 4476 test_tpr_threshold_values(); 4477 report_prefix_pop(); 4478 } 4479 4480 if ((ctrl_cpu_rev[1].clr & 4481 (CPU_VINTD | CPU_VIRT_APIC_ACCESSES)) == 4482 (CPU_VINTD | CPU_VIRT_APIC_ACCESSES)) { 4483 vmcs_write(CPU_EXEC_CTRL0, 4484 vmcs_read(CPU_EXEC_CTRL0) & ~CPU_SECONDARY); 4485 vmcs_write(CPU_EXEC_CTRL1, 4486 CPU_VINTD | CPU_VIRT_APIC_ACCESSES); 4487 report_prefix_pushf("Use TPR shadow enabled; secondary controls disabled; virtual-interrupt delivery enabled; virtualize APIC accesses enabled"); 4488 test_tpr_threshold_values(); 4489 report_prefix_pop(); 4490 4491 vmcs_write(CPU_EXEC_CTRL0, 4492 vmcs_read(CPU_EXEC_CTRL0) | CPU_SECONDARY); 4493 report_prefix_pushf("Use TPR shadow enabled; secondary controls enabled; virtual-interrupt delivery enabled; virtualize APIC accesses enabled"); 4494 test_tpr_threshold_values(); 4495 report_prefix_pop(); 4496 } 4497 4498 vmcs_write(CPU_EXEC_CTRL1, secondary); 4499 vmcs_write(CPU_EXEC_CTRL0, primary); 4500 } 4501 4502 /* 4503 * This test verifies the following two vmentry checks: 4504 * 4505 * If the "NMI exiting" VM-execution control is 0, "Virtual NMIs" 4506 * VM-execution control must be 0. 4507 * [Intel SDM] 4508 * 4509 * If the “virtual NMIs” VM-execution control is 0, the “NMI-window 4510 * exiting” VM-execution control must be 0. 4511 * [Intel SDM] 4512 */ 4513 static void test_nmi_ctrls(void) 4514 { 4515 u32 pin_ctrls, cpu_ctrls0, test_pin_ctrls, test_cpu_ctrls0; 4516 4517 if ((ctrl_pin_rev.clr & (PIN_NMI | PIN_VIRT_NMI)) != 4518 (PIN_NMI | PIN_VIRT_NMI)) { 4519 test_skip("NMI exiting and Virtual NMIs are not supported !"); 4520 return; 4521 } 4522 4523 /* Save the controls so that we can restore them after our tests */ 4524 pin_ctrls = vmcs_read(PIN_CONTROLS); 4525 cpu_ctrls0 = vmcs_read(CPU_EXEC_CTRL0); 4526 4527 test_pin_ctrls = pin_ctrls & ~(PIN_NMI | PIN_VIRT_NMI); 4528 test_cpu_ctrls0 = cpu_ctrls0 & ~CPU_NMI_WINDOW; 4529 4530 vmcs_write(PIN_CONTROLS, test_pin_ctrls); 4531 report_prefix_pushf("NMI-exiting disabled, virtual-NMIs disabled"); 4532 test_vmx_valid_controls(false); 4533 report_prefix_pop(); 4534 4535 vmcs_write(PIN_CONTROLS, test_pin_ctrls | PIN_VIRT_NMI); 4536 report_prefix_pushf("NMI-exiting disabled, virtual-NMIs enabled"); 4537 test_vmx_invalid_controls(false); 4538 report_prefix_pop(); 4539 4540 vmcs_write(PIN_CONTROLS, test_pin_ctrls | (PIN_NMI | PIN_VIRT_NMI)); 4541 report_prefix_pushf("NMI-exiting enabled, virtual-NMIs enabled"); 4542 test_vmx_valid_controls(false); 4543 report_prefix_pop(); 4544 4545 vmcs_write(PIN_CONTROLS, test_pin_ctrls | PIN_NMI); 4546 report_prefix_pushf("NMI-exiting enabled, virtual-NMIs disabled"); 4547 test_vmx_valid_controls(false); 4548 report_prefix_pop(); 4549 4550 if (!(ctrl_cpu_rev[0].clr & CPU_NMI_WINDOW)) { 4551 report_info("NMI-window exiting is not supported, skipping..."); 4552 goto done; 4553 } 4554 4555 vmcs_write(PIN_CONTROLS, test_pin_ctrls); 4556 vmcs_write(CPU_EXEC_CTRL0, test_cpu_ctrls0 | CPU_NMI_WINDOW); 4557 report_prefix_pushf("Virtual-NMIs disabled, NMI-window-exiting enabled"); 4558 test_vmx_invalid_controls(false); 4559 report_prefix_pop(); 4560 4561 vmcs_write(PIN_CONTROLS, test_pin_ctrls); 4562 vmcs_write(CPU_EXEC_CTRL0, test_cpu_ctrls0); 4563 report_prefix_pushf("Virtual-NMIs disabled, NMI-window-exiting disabled"); 4564 test_vmx_valid_controls(false); 4565 report_prefix_pop(); 4566 4567 vmcs_write(PIN_CONTROLS, test_pin_ctrls | (PIN_NMI | PIN_VIRT_NMI)); 4568 vmcs_write(CPU_EXEC_CTRL0, test_cpu_ctrls0 | CPU_NMI_WINDOW); 4569 report_prefix_pushf("Virtual-NMIs enabled, NMI-window-exiting enabled"); 4570 test_vmx_valid_controls(false); 4571 report_prefix_pop(); 4572 4573 vmcs_write(PIN_CONTROLS, test_pin_ctrls | (PIN_NMI | PIN_VIRT_NMI)); 4574 vmcs_write(CPU_EXEC_CTRL0, test_cpu_ctrls0); 4575 report_prefix_pushf("Virtual-NMIs enabled, NMI-window-exiting disabled"); 4576 test_vmx_valid_controls(false); 4577 report_prefix_pop(); 4578 4579 /* Restore the controls to their original values */ 4580 vmcs_write(CPU_EXEC_CTRL0, cpu_ctrls0); 4581 done: 4582 vmcs_write(PIN_CONTROLS, pin_ctrls); 4583 } 4584 4585 static void test_eptp_ad_bit(u64 eptp, bool ctrl) 4586 { 4587 vmcs_write(EPTP, eptp); 4588 report_prefix_pushf("Enable-EPT enabled; EPT accessed and dirty flag %s", 4589 (eptp & EPTP_AD_FLAG) ? "1": "0"); 4590 if (ctrl) 4591 test_vmx_valid_controls(false); 4592 else 4593 test_vmx_invalid_controls(false); 4594 report_prefix_pop(); 4595 4596 } 4597 4598 /* 4599 * 1. If the "enable EPT" VM-execution control is 1, the "EPTP VM-execution" 4600 * control field must satisfy the following checks: 4601 * 4602 * - The EPT memory type (bits 2:0) must be a value supported by the 4603 * processor as indicated in the IA32_VMX_EPT_VPID_CAP MSR. 4604 * - Bits 5:3 (1 less than the EPT page-walk length) must be 3, 4605 * indicating an EPT page-walk length of 4. 4606 * - Bit 6 (enable bit for accessed and dirty flags for EPT) must be 4607 * 0 if bit 21 of the IA32_VMX_EPT_VPID_CAP MSR is read as 0, 4608 * indicating that the processor does not support accessed and dirty 4609 * dirty flags for EPT. 4610 * - Reserved bits 11:7 and 63:N (where N is the processor's 4611 * physical-address width) must all be 0. 4612 * 4613 * 2. If the "unrestricted guest" VM-execution control is 1, the 4614 * "enable EPT" VM-execution control must also be 1. 4615 */ 4616 static void test_ept_eptp(void) 4617 { 4618 u32 primary_saved = vmcs_read(CPU_EXEC_CTRL0); 4619 u32 secondary_saved = vmcs_read(CPU_EXEC_CTRL1); 4620 u64 eptp_saved = vmcs_read(EPTP); 4621 u32 primary = primary_saved; 4622 u32 secondary = secondary_saved; 4623 u64 msr, eptp = eptp_saved; 4624 bool un_cache = false; 4625 bool wr_bk = false; 4626 bool ctrl; 4627 u32 i, maxphysaddr; 4628 u64 j, resv_bits_mask = 0; 4629 4630 if (!((ctrl_cpu_rev[0].clr & CPU_SECONDARY) && 4631 (ctrl_cpu_rev[1].clr & CPU_EPT))) { 4632 test_skip("\"CPU secondary\" and/or \"enable EPT\" execution controls are not supported !"); 4633 return; 4634 } 4635 4636 /* 4637 * Memory type (bits 2:0) 4638 */ 4639 msr = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP); 4640 if (msr & EPT_CAP_UC) 4641 un_cache = true; 4642 if (msr & EPT_CAP_WB) 4643 wr_bk = true; 4644 4645 primary |= CPU_SECONDARY; 4646 vmcs_write(CPU_EXEC_CTRL0, primary); 4647 secondary |= CPU_EPT; 4648 vmcs_write(CPU_EXEC_CTRL1, secondary); 4649 eptp = (eptp & ~EPTP_PG_WALK_LEN_MASK) | 4650 (3ul << EPTP_PG_WALK_LEN_SHIFT); 4651 vmcs_write(EPTP, eptp); 4652 4653 for (i = 0; i < 8; i++) { 4654 if (i == 0) { 4655 if (un_cache) { 4656 report_info("EPT paging structure memory-type is Un-cacheable\n"); 4657 ctrl = true; 4658 } else { 4659 ctrl = false; 4660 } 4661 } else if (i == 6) { 4662 if (wr_bk) { 4663 report_info("EPT paging structure memory-type is Write-back\n"); 4664 ctrl = true; 4665 } else { 4666 ctrl = false; 4667 } 4668 } else { 4669 ctrl = false; 4670 } 4671 4672 eptp = (eptp & ~EPT_MEM_TYPE_MASK) | i; 4673 vmcs_write(EPTP, eptp); 4674 report_prefix_pushf("Enable-EPT enabled; EPT memory type %lu", 4675 eptp & EPT_MEM_TYPE_MASK); 4676 if (ctrl) 4677 test_vmx_valid_controls(false); 4678 else 4679 test_vmx_invalid_controls(false); 4680 report_prefix_pop(); 4681 } 4682 4683 eptp = (eptp & ~EPT_MEM_TYPE_MASK) | 6ul; 4684 4685 /* 4686 * Page walk length (bits 5:3) 4687 */ 4688 for (i = 0; i < 8; i++) { 4689 eptp = (eptp & ~EPTP_PG_WALK_LEN_MASK) | 4690 (i << EPTP_PG_WALK_LEN_SHIFT); 4691 if (i == 3) 4692 ctrl = true; 4693 else 4694 ctrl = false; 4695 4696 vmcs_write(EPTP, eptp); 4697 report_prefix_pushf("Enable-EPT enabled; EPT page walk length %lu", 4698 eptp & EPTP_PG_WALK_LEN_MASK); 4699 if (ctrl) 4700 test_vmx_valid_controls(false); 4701 else 4702 test_vmx_invalid_controls(false); 4703 report_prefix_pop(); 4704 } 4705 4706 eptp = (eptp & ~EPTP_PG_WALK_LEN_MASK) | 4707 3ul << EPTP_PG_WALK_LEN_SHIFT; 4708 4709 /* 4710 * Accessed and dirty flag (bit 6) 4711 */ 4712 if (msr & EPT_CAP_AD_FLAG) { 4713 report_info("Processor supports accessed and dirty flag"); 4714 eptp &= ~EPTP_AD_FLAG; 4715 test_eptp_ad_bit(eptp, true); 4716 4717 eptp |= EPTP_AD_FLAG; 4718 test_eptp_ad_bit(eptp, true); 4719 } else { 4720 report_info("Processor does not supports accessed and dirty flag"); 4721 eptp &= ~EPTP_AD_FLAG; 4722 test_eptp_ad_bit(eptp, true); 4723 4724 eptp |= EPTP_AD_FLAG; 4725 test_eptp_ad_bit(eptp, false); 4726 } 4727 4728 /* 4729 * Reserved bits [11:7] and [63:N] 4730 */ 4731 for (i = 0; i < 32; i++) { 4732 eptp = (eptp & 4733 ~(EPTP_RESERV_BITS_MASK << EPTP_RESERV_BITS_SHIFT)) | 4734 (i << EPTP_RESERV_BITS_SHIFT); 4735 vmcs_write(EPTP, eptp); 4736 report_prefix_pushf("Enable-EPT enabled; reserved bits [11:7] %lu", 4737 (eptp >> EPTP_RESERV_BITS_SHIFT) & 4738 EPTP_RESERV_BITS_MASK); 4739 if (i == 0) 4740 test_vmx_valid_controls(false); 4741 else 4742 test_vmx_invalid_controls(false); 4743 report_prefix_pop(); 4744 } 4745 4746 eptp = (eptp & ~(EPTP_RESERV_BITS_MASK << EPTP_RESERV_BITS_SHIFT)); 4747 4748 maxphysaddr = cpuid_maxphyaddr(); 4749 for (i = 0; i < (63 - maxphysaddr + 1); i++) { 4750 resv_bits_mask |= 1ul << i; 4751 } 4752 4753 for (j = maxphysaddr - 1; j <= 63; j++) { 4754 eptp = (eptp & ~(resv_bits_mask << maxphysaddr)) | 4755 (j < maxphysaddr ? 0 : 1ul << j); 4756 vmcs_write(EPTP, eptp); 4757 report_prefix_pushf("Enable-EPT enabled; reserved bits [63:N] %lu", 4758 (eptp >> maxphysaddr) & resv_bits_mask); 4759 if (j < maxphysaddr) 4760 test_vmx_valid_controls(false); 4761 else 4762 test_vmx_invalid_controls(false); 4763 report_prefix_pop(); 4764 } 4765 4766 secondary &= ~(CPU_EPT | CPU_URG); 4767 vmcs_write(CPU_EXEC_CTRL1, secondary); 4768 report_prefix_pushf("Enable-EPT disabled, unrestricted-guest disabled"); 4769 test_vmx_valid_controls(false); 4770 report_prefix_pop(); 4771 4772 secondary |= CPU_URG; 4773 vmcs_write(CPU_EXEC_CTRL1, secondary); 4774 report_prefix_pushf("Enable-EPT disabled, unrestricted-guest enabled"); 4775 test_vmx_invalid_controls(false); 4776 report_prefix_pop(); 4777 4778 secondary |= CPU_EPT; 4779 setup_dummy_ept(); 4780 report_prefix_pushf("Enable-EPT enabled, unrestricted-guest enabled"); 4781 test_vmx_valid_controls(false); 4782 report_prefix_pop(); 4783 4784 secondary &= ~CPU_URG; 4785 vmcs_write(CPU_EXEC_CTRL1, secondary); 4786 report_prefix_pushf("Enable-EPT enabled, unrestricted-guest disabled"); 4787 test_vmx_valid_controls(false); 4788 report_prefix_pop(); 4789 4790 vmcs_write(CPU_EXEC_CTRL0, primary_saved); 4791 vmcs_write(CPU_EXEC_CTRL1, secondary_saved); 4792 vmcs_write(EPTP, eptp_saved); 4793 } 4794 4795 /* 4796 * If the 'enable PML' VM-execution control is 1, the 'enable EPT' 4797 * VM-execution control must also be 1. In addition, the PML address 4798 * must satisfy the following checks: 4799 * 4800 * * Bits 11:0 of the address must be 0. 4801 * * The address should not set any bits beyond the processor's 4802 * physical-address width. 4803 * 4804 * [Intel SDM] 4805 */ 4806 static void test_pml(void) 4807 { 4808 u32 primary_saved = vmcs_read(CPU_EXEC_CTRL0); 4809 u32 secondary_saved = vmcs_read(CPU_EXEC_CTRL1); 4810 u32 primary = primary_saved; 4811 u32 secondary = secondary_saved; 4812 4813 if (!((ctrl_cpu_rev[0].clr & CPU_SECONDARY) && 4814 (ctrl_cpu_rev[1].clr & CPU_EPT) && (ctrl_cpu_rev[1].clr & CPU_PML))) { 4815 test_skip("\"Secondary execution\" control or \"enable EPT\" control or \"enable PML\" control is not supported !"); 4816 return; 4817 } 4818 4819 primary |= CPU_SECONDARY; 4820 vmcs_write(CPU_EXEC_CTRL0, primary); 4821 secondary &= ~(CPU_PML | CPU_EPT); 4822 vmcs_write(CPU_EXEC_CTRL1, secondary); 4823 report_prefix_pushf("enable-PML disabled, enable-EPT disabled"); 4824 test_vmx_valid_controls(false); 4825 report_prefix_pop(); 4826 4827 secondary |= CPU_PML; 4828 vmcs_write(CPU_EXEC_CTRL1, secondary); 4829 report_prefix_pushf("enable-PML enabled, enable-EPT disabled"); 4830 test_vmx_invalid_controls(false); 4831 report_prefix_pop(); 4832 4833 secondary |= CPU_EPT; 4834 setup_dummy_ept(); 4835 report_prefix_pushf("enable-PML enabled, enable-EPT enabled"); 4836 test_vmx_valid_controls(false); 4837 report_prefix_pop(); 4838 4839 secondary &= ~CPU_PML; 4840 vmcs_write(CPU_EXEC_CTRL1, secondary); 4841 report_prefix_pushf("enable-PML disabled, enable EPT enabled"); 4842 test_vmx_valid_controls(false); 4843 report_prefix_pop(); 4844 4845 test_vmcs_addr_reference(CPU_PML, PMLADDR, "PML address", "PML", 4846 PAGE_SIZE, false, false); 4847 4848 vmcs_write(CPU_EXEC_CTRL0, primary_saved); 4849 vmcs_write(CPU_EXEC_CTRL1, secondary_saved); 4850 } 4851 4852 /* 4853 * If the "activate VMX-preemption timer" VM-execution control is 0, the 4854 * the "save VMX-preemption timer value" VM-exit control must also be 0. 4855 * 4856 * [Intel SDM] 4857 */ 4858 static void test_vmx_preemption_timer(void) 4859 { 4860 u32 saved_pin = vmcs_read(PIN_CONTROLS); 4861 u32 saved_exit = vmcs_read(EXI_CONTROLS); 4862 u32 pin = saved_pin; 4863 u32 exit = saved_exit; 4864 4865 if (!((ctrl_exit_rev.clr & EXI_SAVE_PREEMPT) || 4866 (ctrl_pin_rev.clr & PIN_PREEMPT))) { 4867 printf("\"Save-VMX-preemption-timer\" control and/or \"Enable-VMX-preemption-timer\" control is not supported\n"); 4868 return; 4869 } 4870 4871 pin |= PIN_PREEMPT; 4872 vmcs_write(PIN_CONTROLS, pin); 4873 exit &= ~EXI_SAVE_PREEMPT; 4874 vmcs_write(EXI_CONTROLS, exit); 4875 report_prefix_pushf("enable-VMX-preemption-timer enabled, save-VMX-preemption-timer disabled"); 4876 test_vmx_valid_controls(false); 4877 report_prefix_pop(); 4878 4879 exit |= EXI_SAVE_PREEMPT; 4880 vmcs_write(EXI_CONTROLS, exit); 4881 report_prefix_pushf("enable-VMX-preemption-timer enabled, save-VMX-preemption-timer enabled"); 4882 test_vmx_valid_controls(false); 4883 report_prefix_pop(); 4884 4885 pin &= ~PIN_PREEMPT; 4886 vmcs_write(PIN_CONTROLS, pin); 4887 report_prefix_pushf("enable-VMX-preemption-timer disabled, save-VMX-preemption-timer enabled"); 4888 test_vmx_invalid_controls(false); 4889 report_prefix_pop(); 4890 4891 exit &= ~EXI_SAVE_PREEMPT; 4892 vmcs_write(EXI_CONTROLS, exit); 4893 report_prefix_pushf("enable-VMX-preemption-timer disabled, save-VMX-preemption-timer disabled"); 4894 test_vmx_valid_controls(false); 4895 report_prefix_pop(); 4896 4897 vmcs_write(PIN_CONTROLS, saved_pin); 4898 vmcs_write(EXI_CONTROLS, saved_exit); 4899 } 4900 4901 /* 4902 * Tests for VM-execution control fields 4903 */ 4904 static void test_vm_execution_ctls(void) 4905 { 4906 test_pin_based_ctls(); 4907 test_primary_processor_based_ctls(); 4908 test_secondary_processor_based_ctls(); 4909 test_cr3_targets(); 4910 test_io_bitmaps(); 4911 test_msr_bitmap(); 4912 test_apic_ctls(); 4913 test_tpr_threshold(); 4914 test_nmi_ctrls(); 4915 test_pml(); 4916 test_vpid(); 4917 test_ept_eptp(); 4918 test_vmx_preemption_timer(); 4919 } 4920 4921 /* 4922 * The following checks are performed for the VM-entry MSR-load address if 4923 * the VM-entry MSR-load count field is non-zero: 4924 * 4925 * - The lower 4 bits of the VM-entry MSR-load address must be 0. 4926 * The address should not set any bits beyond the processor’s 4927 * physical-address width. 4928 * 4929 * - The address of the last byte in the VM-entry MSR-load area 4930 * should not set any bits beyond the processor’s physical-address 4931 * width. The address of this last byte is VM-entry MSR-load address 4932 * + (MSR count * 16) - 1. (The arithmetic used for the computation 4933 * uses more bits than the processor’s physical-address width.) 4934 * 4935 * 4936 * [Intel SDM] 4937 */ 4938 static void test_entry_msr_load(void) 4939 { 4940 entry_msr_load = alloc_page(); 4941 u64 tmp; 4942 u32 entry_msr_ld_cnt = 1; 4943 int i; 4944 u32 addr_len = 64; 4945 4946 vmcs_write(ENT_MSR_LD_CNT, entry_msr_ld_cnt); 4947 4948 /* Check first 4 bits of VM-entry MSR-load address */ 4949 for (i = 0; i < 4; i++) { 4950 tmp = (u64)entry_msr_load | 1ull << i; 4951 vmcs_write(ENTER_MSR_LD_ADDR, tmp); 4952 report_prefix_pushf("VM-entry MSR-load addr [4:0] %lx", 4953 tmp & 0xf); 4954 test_vmx_invalid_controls(false); 4955 report_prefix_pop(); 4956 } 4957 4958 if (basic.val & (1ul << 48)) 4959 addr_len = 32; 4960 4961 test_vmcs_addr_values("VM-entry-MSR-load address", 4962 ENTER_MSR_LD_ADDR, 16, false, false, 4963 4, addr_len - 1); 4964 4965 /* 4966 * Check last byte of VM-entry MSR-load address 4967 */ 4968 entry_msr_load = (struct vmx_msr_entry *)((u64)entry_msr_load & ~0xf); 4969 4970 for (i = (addr_len == 64 ? cpuid_maxphyaddr(): addr_len); 4971 i < 64; i++) { 4972 tmp = ((u64)entry_msr_load + entry_msr_ld_cnt * 16 - 1) | 4973 1ul << i; 4974 vmcs_write(ENTER_MSR_LD_ADDR, 4975 tmp - (entry_msr_ld_cnt * 16 - 1)); 4976 test_vmx_invalid_controls(false); 4977 } 4978 4979 vmcs_write(ENT_MSR_LD_CNT, 2); 4980 vmcs_write(ENTER_MSR_LD_ADDR, (1ULL << cpuid_maxphyaddr()) - 16); 4981 test_vmx_invalid_controls(false); 4982 vmcs_write(ENTER_MSR_LD_ADDR, (1ULL << cpuid_maxphyaddr()) - 32); 4983 test_vmx_valid_controls(false); 4984 vmcs_write(ENTER_MSR_LD_ADDR, (1ULL << cpuid_maxphyaddr()) - 48); 4985 test_vmx_valid_controls(false); 4986 } 4987 4988 /* 4989 * Tests for VM-entry control fields 4990 */ 4991 static void test_vm_entry_ctls(void) 4992 { 4993 test_invalid_event_injection(); 4994 test_entry_msr_load(); 4995 } 4996 4997 /* 4998 * The following checks are performed for the VM-exit MSR-store address if 4999 * the VM-exit MSR-store count field is non-zero: 5000 * 5001 * - The lower 4 bits of the VM-exit MSR-store address must be 0. 5002 * The address should not set any bits beyond the processor’s 5003 * physical-address width. 5004 * 5005 * - The address of the last byte in the VM-exit MSR-store area 5006 * should not set any bits beyond the processor’s physical-address 5007 * width. The address of this last byte is VM-exit MSR-store address 5008 * + (MSR count * 16) - 1. (The arithmetic used for the computation 5009 * uses more bits than the processor’s physical-address width.) 5010 * 5011 * If IA32_VMX_BASIC[48] is read as 1, neither address should set any bits 5012 * in the range 63:32. 5013 * 5014 * [Intel SDM] 5015 */ 5016 static void test_exit_msr_store(void) 5017 { 5018 exit_msr_store = alloc_page(); 5019 u64 tmp; 5020 u32 exit_msr_st_cnt = 1; 5021 int i; 5022 u32 addr_len = 64; 5023 5024 vmcs_write(EXI_MSR_ST_CNT, exit_msr_st_cnt); 5025 5026 /* Check first 4 bits of VM-exit MSR-store address */ 5027 for (i = 0; i < 4; i++) { 5028 tmp = (u64)exit_msr_store | 1ull << i; 5029 vmcs_write(EXIT_MSR_ST_ADDR, tmp); 5030 report_prefix_pushf("VM-exit MSR-store addr [4:0] %lx", 5031 tmp & 0xf); 5032 test_vmx_invalid_controls(false); 5033 report_prefix_pop(); 5034 } 5035 5036 if (basic.val & (1ul << 48)) 5037 addr_len = 32; 5038 5039 test_vmcs_addr_values("VM-exit-MSR-store address", 5040 EXIT_MSR_ST_ADDR, 16, false, false, 5041 4, addr_len - 1); 5042 5043 /* 5044 * Check last byte of VM-exit MSR-store address 5045 */ 5046 exit_msr_store = (struct vmx_msr_entry *)((u64)exit_msr_store & ~0xf); 5047 5048 for (i = (addr_len == 64 ? cpuid_maxphyaddr(): addr_len); 5049 i < 64; i++) { 5050 tmp = ((u64)exit_msr_store + exit_msr_st_cnt * 16 - 1) | 5051 1ul << i; 5052 vmcs_write(EXIT_MSR_ST_ADDR, 5053 tmp - (exit_msr_st_cnt * 16 - 1)); 5054 test_vmx_invalid_controls(false); 5055 } 5056 5057 vmcs_write(EXI_MSR_ST_CNT, 2); 5058 vmcs_write(EXIT_MSR_ST_ADDR, (1ULL << cpuid_maxphyaddr()) - 16); 5059 test_vmx_invalid_controls(false); 5060 vmcs_write(EXIT_MSR_ST_ADDR, (1ULL << cpuid_maxphyaddr()) - 32); 5061 test_vmx_valid_controls(false); 5062 vmcs_write(EXIT_MSR_ST_ADDR, (1ULL << cpuid_maxphyaddr()) - 48); 5063 test_vmx_valid_controls(false); 5064 } 5065 5066 /* 5067 * Tests for VM-exit controls 5068 */ 5069 static void test_vm_exit_ctls(void) 5070 { 5071 test_exit_msr_store(); 5072 } 5073 5074 /* 5075 * Check that the virtual CPU checks all of the VMX controls as 5076 * documented in the Intel SDM. 5077 */ 5078 static void vmx_controls_test(void) 5079 { 5080 /* 5081 * Bit 1 of the guest's RFLAGS must be 1, or VM-entry will 5082 * fail due to invalid guest state, should we make it that 5083 * far. 5084 */ 5085 vmcs_write(GUEST_RFLAGS, 0); 5086 5087 test_vm_execution_ctls(); 5088 test_vm_exit_ctls(); 5089 test_vm_entry_ctls(); 5090 } 5091 5092 /* 5093 * 1. The CR0 field must not set any bit to a value not supported in VMX 5094 * operation. 5095 * 2. The CR4 field must not set any bit to a value not supported in VMX 5096 * operation. 5097 * 3. On processors that support Intel 64 architecture, the CR3 field must 5098 * be such that bits 63:52 and bits in the range 51:32 beyond the 5099 * processor’s physical-address width must be 0. 5100 * 5101 * [Intel SDM] 5102 */ 5103 static void test_host_ctl_regs(void) 5104 { 5105 u64 fixed0, fixed1, cr3, cr3_saved; 5106 int i; 5107 5108 /* Test CR0 */ 5109 fixed0 = rdmsr(MSR_IA32_VMX_CR0_FIXED0); 5110 fixed1 = rdmsr(MSR_IA32_VMX_CR0_FIXED1); 5111 test_ctl_reg("HOST_CR0", HOST_CR0, fixed0, fixed1); 5112 5113 /* Test CR4 */ 5114 fixed0 = rdmsr(MSR_IA32_VMX_CR4_FIXED0); 5115 fixed1 = rdmsr(MSR_IA32_VMX_CR4_FIXED1) & 5116 ~(X86_CR4_SMEP | X86_CR4_SMAP); 5117 test_ctl_reg("HOST_CR4", HOST_CR4, fixed0, fixed1); 5118 5119 /* Test CR3 */ 5120 cr3_saved = vmcs_read(HOST_CR3); 5121 for (i = cpuid_maxphyaddr(); i < 64; i++) { 5122 cr3 = cr3_saved | (1ul << i); 5123 vmcs_write(HOST_CR3, cr3); 5124 report_prefix_pushf("HOST_CR3 %lx", cr3); 5125 test_vmx_vmlaunch(VMXERR_ENTRY_INVALID_HOST_STATE_FIELD, 5126 false); 5127 report_prefix_pop(); 5128 } 5129 5130 vmcs_write(HOST_CR3, cr3_saved); 5131 } 5132 5133 /* 5134 * Check that the virtual CPU checks the VMX Host State Area as 5135 * documented in the Intel SDM. 5136 */ 5137 static void vmx_host_state_area_test(void) 5138 { 5139 /* 5140 * Bit 1 of the guest's RFLAGS must be 1, or VM-entry will 5141 * fail due to invalid guest state, should we make it that 5142 * far. 5143 */ 5144 vmcs_write(GUEST_RFLAGS, 0); 5145 5146 test_host_ctl_regs(); 5147 } 5148 5149 static bool valid_vmcs_for_vmentry(void) 5150 { 5151 struct vmcs *current_vmcs = NULL; 5152 5153 if (vmcs_save(¤t_vmcs)) 5154 return false; 5155 5156 return current_vmcs && !current_vmcs->hdr.shadow_vmcs; 5157 } 5158 5159 static void try_vmentry_in_movss_shadow(void) 5160 { 5161 u32 vm_inst_err; 5162 u32 flags; 5163 bool early_failure = false; 5164 u32 expected_flags = X86_EFLAGS_FIXED; 5165 bool valid_vmcs = valid_vmcs_for_vmentry(); 5166 5167 expected_flags |= valid_vmcs ? X86_EFLAGS_ZF : X86_EFLAGS_CF; 5168 5169 /* 5170 * Indirectly set VM_INST_ERR to 12 ("VMREAD/VMWRITE from/to 5171 * unsupported VMCS component"). 5172 */ 5173 vmcs_write(~0u, 0); 5174 5175 __asm__ __volatile__ ("mov %[host_rsp], %%edx;" 5176 "vmwrite %%rsp, %%rdx;" 5177 "mov 0f, %%rax;" 5178 "mov %[host_rip], %%edx;" 5179 "vmwrite %%rax, %%rdx;" 5180 "mov $-1, %%ah;" 5181 "sahf;" 5182 "mov %%ss, %%ax;" 5183 "mov %%ax, %%ss;" 5184 "vmlaunch;" 5185 "mov $1, %[early_failure];" 5186 "0: lahf;" 5187 "movzbl %%ah, %[flags]" 5188 : [early_failure] "+r" (early_failure), 5189 [flags] "=&a" (flags) 5190 : [host_rsp] "i" (HOST_RSP), 5191 [host_rip] "i" (HOST_RIP) 5192 : "rdx", "cc", "memory"); 5193 vm_inst_err = vmcs_read(VMX_INST_ERROR); 5194 5195 report("Early VM-entry failure", early_failure); 5196 report("RFLAGS[8:0] is %x (actual %x)", flags == expected_flags, 5197 expected_flags, flags); 5198 if (valid_vmcs) 5199 report("VM-instruction error is %d (actual %d)", 5200 vm_inst_err == VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS, 5201 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS, vm_inst_err); 5202 } 5203 5204 static void vmentry_movss_shadow_test(void) 5205 { 5206 struct vmcs *orig_vmcs; 5207 5208 TEST_ASSERT(!vmcs_save(&orig_vmcs)); 5209 5210 /* 5211 * Set the launched flag on the current VMCS to verify the correct 5212 * error priority, below. 5213 */ 5214 test_set_guest(v2_null_test_guest); 5215 enter_guest(); 5216 5217 /* 5218 * With bit 1 of the guest's RFLAGS clear, VM-entry should 5219 * fail due to invalid guest state (if we make it that far). 5220 */ 5221 vmcs_write(GUEST_RFLAGS, 0); 5222 5223 /* 5224 * "VM entry with events blocked by MOV SS" takes precedence over 5225 * "VMLAUNCH with non-clear VMCS." 5226 */ 5227 report_prefix_push("valid current-VMCS"); 5228 try_vmentry_in_movss_shadow(); 5229 report_prefix_pop(); 5230 5231 /* 5232 * VMfailInvalid takes precedence over "VM entry with events 5233 * blocked by MOV SS." 5234 */ 5235 TEST_ASSERT(!vmcs_clear(orig_vmcs)); 5236 report_prefix_push("no current-VMCS"); 5237 try_vmentry_in_movss_shadow(); 5238 report_prefix_pop(); 5239 5240 TEST_ASSERT(!make_vmcs_current(orig_vmcs)); 5241 vmcs_write(GUEST_RFLAGS, X86_EFLAGS_FIXED); 5242 } 5243 5244 #define X86_FEATURE_PCID (1 << 17) 5245 #define X86_FEATURE_MCE (1 << 7) 5246 5247 static int write_cr4_checking(unsigned long val) 5248 { 5249 asm volatile(ASM_TRY("1f") 5250 "mov %0, %%cr4\n\t" 5251 "1:": : "r" (val)); 5252 return exception_vector(); 5253 } 5254 5255 static void vmx_cr_load_test(void) 5256 { 5257 struct cpuid _cpuid = cpuid(1); 5258 unsigned long cr4 = read_cr4(), cr3 = read_cr3(); 5259 5260 if (!(_cpuid.c & X86_FEATURE_PCID)) { 5261 report_skip("PCID not detected"); 5262 return; 5263 } 5264 if (!(_cpuid.d & X86_FEATURE_MCE)) { 5265 report_skip("MCE not detected"); 5266 return; 5267 } 5268 5269 TEST_ASSERT(!(cr4 & (X86_CR4_PCIDE | X86_CR4_MCE))); 5270 TEST_ASSERT(!(cr3 & X86_CR3_PCID_MASK)); 5271 5272 /* Enable PCID for L1. */ 5273 cr4 |= X86_CR4_PCIDE; 5274 cr3 |= 0x1; 5275 TEST_ASSERT(!write_cr4_checking(cr4)); 5276 write_cr3(cr3); 5277 5278 test_set_guest(v2_null_test_guest); 5279 vmcs_write(HOST_CR4, cr4); 5280 vmcs_write(HOST_CR3, cr3); 5281 enter_guest(); 5282 5283 /* 5284 * No exception is expected. 5285 * 5286 * NB. KVM loads the last guest write to CR4 into CR4 read 5287 * shadow. In order to trigger an exit to KVM, we can set a 5288 * bit that was zero in the above CR4 write and is owned by 5289 * KVM. We choose to set CR4.MCE, which shall have no side 5290 * effect because normally no guest MCE (e.g., as the result 5291 * of bad memory) would happen during this test. 5292 */ 5293 TEST_ASSERT(!write_cr4_checking(cr4 | X86_CR4_MCE)); 5294 5295 /* Cleanup L1 state: disable PCID. */ 5296 write_cr3(cr3 & ~X86_CR3_PCID_MASK); 5297 TEST_ASSERT(!write_cr4_checking(cr4 & ~X86_CR4_PCIDE)); 5298 } 5299 5300 static void vmx_nm_test_guest(void) 5301 { 5302 write_cr0(read_cr0() | X86_CR0_TS); 5303 asm volatile("fnop"); 5304 } 5305 5306 static void check_nm_exit(const char *test) 5307 { 5308 u32 reason = vmcs_read(EXI_REASON); 5309 u32 intr_info = vmcs_read(EXI_INTR_INFO); 5310 const u32 expected = INTR_INFO_VALID_MASK | INTR_TYPE_HARD_EXCEPTION | 5311 NM_VECTOR; 5312 5313 report("%s", reason == VMX_EXC_NMI && intr_info == expected, test); 5314 } 5315 5316 /* 5317 * This test checks that: 5318 * 5319 * (a) If L2 launches with CR0.TS clear, but later sets CR0.TS, then 5320 * a subsequent #NM VM-exit is reflected to L1. 5321 * 5322 * (b) If L2 launches with CR0.TS clear and CR0.EM set, then a 5323 * subsequent #NM VM-exit is reflected to L1. 5324 */ 5325 static void vmx_nm_test(void) 5326 { 5327 unsigned long cr0 = read_cr0(); 5328 5329 test_set_guest(vmx_nm_test_guest); 5330 5331 /* 5332 * L1 wants to intercept #NM exceptions encountered in L2. 5333 */ 5334 vmcs_write(EXC_BITMAP, 1 << NM_VECTOR); 5335 5336 /* 5337 * Launch L2 with CR0.TS clear, but don't claim host ownership of 5338 * any CR0 bits. L2 will set CR0.TS and then try to execute fnop, 5339 * which will raise #NM. L0 should reflect the #NM VM-exit to L1. 5340 */ 5341 vmcs_write(CR0_MASK, 0); 5342 vmcs_write(GUEST_CR0, cr0 & ~X86_CR0_TS); 5343 enter_guest(); 5344 check_nm_exit("fnop with CR0.TS set in L2 triggers #NM VM-exit to L1"); 5345 5346 /* 5347 * Re-enter L2 at the fnop instruction, with CR0.TS clear but 5348 * CR0.EM set. The fnop will still raise #NM, and L0 should 5349 * reflect the #NM VM-exit to L1. 5350 */ 5351 vmcs_write(GUEST_CR0, (cr0 & ~X86_CR0_TS) | X86_CR0_EM); 5352 enter_guest(); 5353 check_nm_exit("fnop with CR0.EM set in L2 triggers #NM VM-exit to L1"); 5354 5355 /* 5356 * Re-enter L2 at the fnop instruction, with both CR0.TS and 5357 * CR0.EM clear. There will be no #NM, and the L2 guest should 5358 * exit normally. 5359 */ 5360 vmcs_write(GUEST_CR0, cr0 & ~(X86_CR0_TS | X86_CR0_EM)); 5361 enter_guest(); 5362 } 5363 5364 bool vmx_pending_event_ipi_fired; 5365 static void vmx_pending_event_ipi_isr(isr_regs_t *regs) 5366 { 5367 vmx_pending_event_ipi_fired = true; 5368 eoi(); 5369 } 5370 5371 bool vmx_pending_event_guest_run; 5372 static void vmx_pending_event_guest(void) 5373 { 5374 vmcall(); 5375 vmx_pending_event_guest_run = true; 5376 } 5377 5378 static void vmx_pending_event_test_core(bool guest_hlt) 5379 { 5380 int ipi_vector = 0xf1; 5381 5382 vmx_pending_event_ipi_fired = false; 5383 handle_irq(ipi_vector, vmx_pending_event_ipi_isr); 5384 5385 vmx_pending_event_guest_run = false; 5386 test_set_guest(vmx_pending_event_guest); 5387 5388 vmcs_set_bits(PIN_CONTROLS, PIN_EXTINT); 5389 5390 enter_guest(); 5391 skip_exit_vmcall(); 5392 5393 if (guest_hlt) 5394 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 5395 5396 irq_disable(); 5397 apic_icr_write(APIC_DEST_SELF | APIC_DEST_PHYSICAL | 5398 APIC_DM_FIXED | ipi_vector, 5399 0); 5400 5401 enter_guest(); 5402 5403 assert_exit_reason(VMX_EXTINT); 5404 report("Guest did not run before host received IPI", 5405 !vmx_pending_event_guest_run); 5406 5407 irq_enable(); 5408 asm volatile ("nop"); 5409 irq_disable(); 5410 report("Got pending interrupt after IRQ enabled", 5411 vmx_pending_event_ipi_fired); 5412 5413 if (guest_hlt) 5414 vmcs_write(GUEST_ACTV_STATE, ACTV_ACTIVE); 5415 5416 enter_guest(); 5417 report("Guest finished running when no interrupt", 5418 vmx_pending_event_guest_run); 5419 } 5420 5421 static void vmx_pending_event_test(void) 5422 { 5423 vmx_pending_event_test_core(false); 5424 } 5425 5426 static void vmx_pending_event_hlt_test(void) 5427 { 5428 vmx_pending_event_test_core(true); 5429 } 5430 5431 static int vmx_window_test_ud_count; 5432 5433 static void vmx_window_test_ud_handler(struct ex_regs *regs) 5434 { 5435 vmx_window_test_ud_count++; 5436 } 5437 5438 static void vmx_nmi_window_test_guest(void) 5439 { 5440 handle_exception(UD_VECTOR, vmx_window_test_ud_handler); 5441 5442 asm volatile("vmcall\n\t" 5443 "nop\n\t"); 5444 5445 handle_exception(UD_VECTOR, NULL); 5446 } 5447 5448 static void verify_nmi_window_exit(u64 rip) 5449 { 5450 u32 exit_reason = vmcs_read(EXI_REASON); 5451 5452 report("Exit reason (%d) is 'NMI window'", 5453 exit_reason == VMX_NMI_WINDOW, exit_reason); 5454 report("RIP (%#lx) is %#lx", vmcs_read(GUEST_RIP) == rip, 5455 vmcs_read(GUEST_RIP), rip); 5456 report("Activity state (%ld) is 'ACTIVE'", 5457 vmcs_read(GUEST_ACTV_STATE) == ACTV_ACTIVE, 5458 vmcs_read(GUEST_ACTV_STATE)); 5459 } 5460 5461 static void vmx_nmi_window_test(void) 5462 { 5463 u64 nop_addr; 5464 void *ud_fault_addr = get_idt_addr(&boot_idt[UD_VECTOR]); 5465 5466 if (!(ctrl_pin_rev.clr & PIN_VIRT_NMI)) { 5467 report_skip("CPU does not support the \"Virtual NMIs\" VM-execution control."); 5468 return; 5469 } 5470 5471 if (!(ctrl_cpu_rev[0].clr & CPU_NMI_WINDOW)) { 5472 report_skip("CPU does not support the \"NMI-window exiting\" VM-execution control."); 5473 return; 5474 } 5475 5476 vmx_window_test_ud_count = 0; 5477 5478 report_prefix_push("NMI-window"); 5479 test_set_guest(vmx_nmi_window_test_guest); 5480 vmcs_set_bits(PIN_CONTROLS, PIN_VIRT_NMI); 5481 enter_guest(); 5482 skip_exit_vmcall(); 5483 nop_addr = vmcs_read(GUEST_RIP); 5484 5485 /* 5486 * Ask for "NMI-window exiting," and expect an immediate VM-exit. 5487 * RIP will not advance. 5488 */ 5489 report_prefix_push("active, no blocking"); 5490 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_NMI_WINDOW); 5491 enter_guest(); 5492 verify_nmi_window_exit(nop_addr); 5493 report_prefix_pop(); 5494 5495 /* 5496 * Ask for "NMI-window exiting" in a MOV-SS shadow, and expect 5497 * a VM-exit on the next instruction after the nop. (The nop 5498 * is one byte.) 5499 */ 5500 report_prefix_push("active, blocking by MOV-SS"); 5501 vmcs_write(GUEST_INTR_STATE, GUEST_INTR_STATE_MOVSS); 5502 enter_guest(); 5503 verify_nmi_window_exit(nop_addr + 1); 5504 report_prefix_pop(); 5505 5506 /* 5507 * Ask for "NMI-window exiting" (with event injection), and 5508 * expect a VM-exit after the event is injected. (RIP should 5509 * be at the address specified in the IDT entry for #UD.) 5510 */ 5511 report_prefix_push("active, no blocking, injecting #UD"); 5512 vmcs_write(ENT_INTR_INFO, 5513 INTR_INFO_VALID_MASK | INTR_TYPE_HARD_EXCEPTION | UD_VECTOR); 5514 enter_guest(); 5515 verify_nmi_window_exit((u64)ud_fault_addr); 5516 report_prefix_pop(); 5517 5518 /* 5519 * Ask for "NMI-window exiting" with NMI blocking, and expect 5520 * a VM-exit after the next IRET (i.e. after the #UD handler 5521 * returns). So, RIP should be back at one byte past the nop. 5522 */ 5523 report_prefix_push("active, blocking by NMI"); 5524 vmcs_write(GUEST_INTR_STATE, GUEST_INTR_STATE_NMI); 5525 enter_guest(); 5526 verify_nmi_window_exit(nop_addr + 1); 5527 report("#UD handler executed once (actual %d times)", 5528 vmx_window_test_ud_count == 1, 5529 vmx_window_test_ud_count); 5530 report_prefix_pop(); 5531 5532 if (!(rdmsr(MSR_IA32_VMX_MISC) & (1 << 6))) { 5533 report_skip("CPU does not support activity state HLT."); 5534 } else { 5535 /* 5536 * Ask for "NMI-window exiting" when entering activity 5537 * state HLT, and expect an immediate VM-exit. RIP is 5538 * still one byte past the nop. 5539 */ 5540 report_prefix_push("halted, no blocking"); 5541 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 5542 enter_guest(); 5543 verify_nmi_window_exit(nop_addr + 1); 5544 report_prefix_pop(); 5545 5546 /* 5547 * Ask for "NMI-window exiting" when entering activity 5548 * state HLT (with event injection), and expect a 5549 * VM-exit after the event is injected. (RIP should be 5550 * at the address specified in the IDT entry for #UD.) 5551 */ 5552 report_prefix_push("halted, no blocking, injecting #UD"); 5553 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 5554 vmcs_write(ENT_INTR_INFO, 5555 INTR_INFO_VALID_MASK | INTR_TYPE_HARD_EXCEPTION | 5556 UD_VECTOR); 5557 enter_guest(); 5558 verify_nmi_window_exit((u64)ud_fault_addr); 5559 report_prefix_pop(); 5560 } 5561 5562 vmcs_clear_bits(CPU_EXEC_CTRL0, CPU_NMI_WINDOW); 5563 enter_guest(); 5564 report_prefix_pop(); 5565 } 5566 5567 static void vmx_intr_window_test_guest(void) 5568 { 5569 handle_exception(UD_VECTOR, vmx_window_test_ud_handler); 5570 5571 /* 5572 * The two consecutive STIs are to ensure that only the first 5573 * one has a shadow. Note that NOP and STI are one byte 5574 * instructions. 5575 */ 5576 asm volatile("vmcall\n\t" 5577 "nop\n\t" 5578 "sti\n\t" 5579 "sti\n\t"); 5580 5581 handle_exception(UD_VECTOR, NULL); 5582 } 5583 5584 static void verify_intr_window_exit(u64 rip) 5585 { 5586 u32 exit_reason = vmcs_read(EXI_REASON); 5587 5588 report("Exit reason (%d) is 'interrupt window'", 5589 exit_reason == VMX_INTR_WINDOW, exit_reason); 5590 report("RIP (%#lx) is %#lx", vmcs_read(GUEST_RIP) == rip, 5591 vmcs_read(GUEST_RIP), rip); 5592 report("Activity state (%ld) is 'ACTIVE'", 5593 vmcs_read(GUEST_ACTV_STATE) == ACTV_ACTIVE, 5594 vmcs_read(GUEST_ACTV_STATE)); 5595 } 5596 5597 static void vmx_intr_window_test(void) 5598 { 5599 u64 vmcall_addr; 5600 u64 nop_addr; 5601 unsigned int orig_ud_gate_type; 5602 void *ud_fault_addr = get_idt_addr(&boot_idt[UD_VECTOR]); 5603 5604 if (!(ctrl_cpu_rev[0].clr & CPU_INTR_WINDOW)) { 5605 report_skip("CPU does not support the \"interrupt-window exiting\" VM-execution control."); 5606 return; 5607 } 5608 5609 /* 5610 * Change the IDT entry for #UD from interrupt gate to trap gate, 5611 * so that it won't clear RFLAGS.IF. We don't want interrupts to 5612 * be disabled after vectoring a #UD. 5613 */ 5614 orig_ud_gate_type = boot_idt[UD_VECTOR].type; 5615 boot_idt[UD_VECTOR].type = 15; 5616 5617 report_prefix_push("interrupt-window"); 5618 test_set_guest(vmx_intr_window_test_guest); 5619 enter_guest(); 5620 assert_exit_reason(VMX_VMCALL); 5621 vmcall_addr = vmcs_read(GUEST_RIP); 5622 5623 /* 5624 * Ask for "interrupt-window exiting" with RFLAGS.IF set and 5625 * no blocking; expect an immediate VM-exit. Note that we have 5626 * not advanced past the vmcall instruction yet, so RIP should 5627 * point to the vmcall instruction. 5628 */ 5629 report_prefix_push("active, no blocking, RFLAGS.IF=1"); 5630 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_INTR_WINDOW); 5631 vmcs_write(GUEST_RFLAGS, X86_EFLAGS_FIXED | X86_EFLAGS_IF); 5632 enter_guest(); 5633 verify_intr_window_exit(vmcall_addr); 5634 report_prefix_pop(); 5635 5636 /* 5637 * Ask for "interrupt-window exiting" (with event injection) 5638 * with RFLAGS.IF set and no blocking; expect a VM-exit after 5639 * the event is injected. That is, RIP should should be at the 5640 * address specified in the IDT entry for #UD. 5641 */ 5642 report_prefix_push("active, no blocking, RFLAGS.IF=1, injecting #UD"); 5643 vmcs_write(ENT_INTR_INFO, 5644 INTR_INFO_VALID_MASK | INTR_TYPE_HARD_EXCEPTION | UD_VECTOR); 5645 vmcall_addr = vmcs_read(GUEST_RIP); 5646 enter_guest(); 5647 verify_intr_window_exit((u64)ud_fault_addr); 5648 report_prefix_pop(); 5649 5650 /* 5651 * Let the L2 guest run through the IRET, back to the VMCALL. 5652 * We have to clear the "interrupt-window exiting" 5653 * VM-execution control, or it would just keep causing 5654 * VM-exits. Then, advance past the VMCALL and set the 5655 * "interrupt-window exiting" VM-execution control again. 5656 */ 5657 vmcs_clear_bits(CPU_EXEC_CTRL0, CPU_INTR_WINDOW); 5658 enter_guest(); 5659 skip_exit_vmcall(); 5660 nop_addr = vmcs_read(GUEST_RIP); 5661 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_INTR_WINDOW); 5662 5663 /* 5664 * Ask for "interrupt-window exiting" in a MOV-SS shadow with 5665 * RFLAGS.IF set, and expect a VM-exit on the next 5666 * instruction. (NOP is one byte.) 5667 */ 5668 report_prefix_push("active, blocking by MOV-SS, RFLAGS.IF=1"); 5669 vmcs_write(GUEST_INTR_STATE, GUEST_INTR_STATE_MOVSS); 5670 enter_guest(); 5671 verify_intr_window_exit(nop_addr + 1); 5672 report_prefix_pop(); 5673 5674 /* 5675 * Back up to the NOP and ask for "interrupt-window exiting" 5676 * in an STI shadow with RFLAGS.IF set, and expect a VM-exit 5677 * on the next instruction. (NOP is one byte.) 5678 */ 5679 report_prefix_push("active, blocking by STI, RFLAGS.IF=1"); 5680 vmcs_write(GUEST_RIP, nop_addr); 5681 vmcs_write(GUEST_INTR_STATE, GUEST_INTR_STATE_STI); 5682 enter_guest(); 5683 verify_intr_window_exit(nop_addr + 1); 5684 report_prefix_pop(); 5685 5686 /* 5687 * Ask for "interrupt-window exiting" with RFLAGS.IF clear, 5688 * and expect a VM-exit on the instruction following the STI 5689 * shadow. Only the first STI (which is one byte past the NOP) 5690 * should have a shadow. The second STI (which is two bytes 5691 * past the NOP) has no shadow. Therefore, the interrupt 5692 * window opens at three bytes past the NOP. 5693 */ 5694 report_prefix_push("active, RFLAGS.IF = 0"); 5695 vmcs_write(GUEST_RFLAGS, X86_EFLAGS_FIXED); 5696 enter_guest(); 5697 verify_intr_window_exit(nop_addr + 3); 5698 report_prefix_pop(); 5699 5700 if (!(rdmsr(MSR_IA32_VMX_MISC) & (1 << 6))) { 5701 report_skip("CPU does not support activity state HLT."); 5702 } else { 5703 /* 5704 * Ask for "interrupt-window exiting" when entering 5705 * activity state HLT, and expect an immediate 5706 * VM-exit. RIP is still three bytes past the nop. 5707 */ 5708 report_prefix_push("halted, no blocking"); 5709 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 5710 enter_guest(); 5711 verify_intr_window_exit(nop_addr + 3); 5712 report_prefix_pop(); 5713 5714 /* 5715 * Ask for "interrupt-window exiting" when entering 5716 * activity state HLT (with event injection), and 5717 * expect a VM-exit after the event is injected. That 5718 * is, RIP should should be at the address specified 5719 * in the IDT entry for #UD. 5720 */ 5721 report_prefix_push("halted, no blocking, injecting #UD"); 5722 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 5723 vmcs_write(ENT_INTR_INFO, 5724 INTR_INFO_VALID_MASK | INTR_TYPE_HARD_EXCEPTION | 5725 UD_VECTOR); 5726 enter_guest(); 5727 verify_intr_window_exit((u64)ud_fault_addr); 5728 report_prefix_pop(); 5729 } 5730 5731 boot_idt[UD_VECTOR].type = orig_ud_gate_type; 5732 vmcs_clear_bits(CPU_EXEC_CTRL0, CPU_INTR_WINDOW); 5733 enter_guest(); 5734 report_prefix_pop(); 5735 } 5736 5737 #define GUEST_TSC_OFFSET (1u << 30) 5738 5739 static u64 guest_tsc; 5740 5741 static void vmx_store_tsc_test_guest(void) 5742 { 5743 guest_tsc = rdtsc(); 5744 } 5745 5746 /* 5747 * This test ensures that when IA32_TSC is in the VM-exit MSR-store 5748 * list, the value saved is not subject to the TSC offset that is 5749 * applied to RDTSC/RDTSCP/RDMSR(IA32_TSC) in guest execution. 5750 */ 5751 static void vmx_store_tsc_test(void) 5752 { 5753 struct vmx_msr_entry msr_entry = { .index = MSR_IA32_TSC }; 5754 u64 low, high; 5755 5756 if (!(ctrl_cpu_rev[0].clr & CPU_USE_TSC_OFFSET)) { 5757 report_skip("'Use TSC offsetting' not supported"); 5758 return; 5759 } 5760 5761 test_set_guest(vmx_store_tsc_test_guest); 5762 5763 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_USE_TSC_OFFSET); 5764 vmcs_write(EXI_MSR_ST_CNT, 1); 5765 vmcs_write(EXIT_MSR_ST_ADDR, virt_to_phys(&msr_entry)); 5766 vmcs_write(TSC_OFFSET, GUEST_TSC_OFFSET); 5767 5768 low = rdtsc(); 5769 enter_guest(); 5770 high = rdtsc(); 5771 5772 report("RDTSC value in the guest (%lu) is in range [%lu, %lu]", 5773 low + GUEST_TSC_OFFSET <= guest_tsc && 5774 guest_tsc <= high + GUEST_TSC_OFFSET, 5775 guest_tsc, low + GUEST_TSC_OFFSET, high + GUEST_TSC_OFFSET); 5776 report("IA32_TSC value saved in the VM-exit MSR-store list (%lu) is in range [%lu, %lu]", 5777 low <= msr_entry.value && msr_entry.value <= high, 5778 msr_entry.value, low, high); 5779 } 5780 5781 static void vmx_db_test_guest(void) 5782 { 5783 /* 5784 * For a hardware generated single-step #DB. 5785 */ 5786 asm volatile("vmcall;" 5787 "nop;" 5788 ".Lpost_nop:"); 5789 /* 5790 * ...in a MOVSS shadow, with pending debug exceptions. 5791 */ 5792 asm volatile("vmcall;" 5793 "nop;" 5794 ".Lpost_movss_nop:"); 5795 /* 5796 * For an L0 synthesized single-step #DB. (L0 intercepts WBINVD and 5797 * emulates it in software.) 5798 */ 5799 asm volatile("vmcall;" 5800 "wbinvd;" 5801 ".Lpost_wbinvd:"); 5802 /* 5803 * ...in a MOVSS shadow, with pending debug exceptions. 5804 */ 5805 asm volatile("vmcall;" 5806 "wbinvd;" 5807 ".Lpost_movss_wbinvd:"); 5808 /* 5809 * For a hardware generated single-step #DB in a transactional region. 5810 */ 5811 asm volatile("vmcall;" 5812 ".Lxbegin: xbegin .Lskip_rtm;" 5813 "xend;" 5814 ".Lskip_rtm:"); 5815 } 5816 5817 /* 5818 * Clear the pending debug exceptions and RFLAGS.TF and re-enter 5819 * L2. No #DB is delivered and L2 continues to the next point of 5820 * interest. 5821 */ 5822 static void dismiss_db(void) 5823 { 5824 vmcs_write(GUEST_PENDING_DEBUG, 0); 5825 vmcs_write(GUEST_RFLAGS, X86_EFLAGS_FIXED); 5826 enter_guest(); 5827 } 5828 5829 /* 5830 * Check a variety of VMCS fields relevant to an intercepted #DB exception. 5831 * Then throw away the #DB exception and resume L2. 5832 */ 5833 static void check_db_exit(bool xfail_qual, bool xfail_dr6, bool xfail_pdbg, 5834 void *expected_rip, u64 expected_exit_qual, 5835 u64 expected_dr6) 5836 { 5837 u32 reason = vmcs_read(EXI_REASON); 5838 u32 intr_info = vmcs_read(EXI_INTR_INFO); 5839 u64 exit_qual = vmcs_read(EXI_QUALIFICATION); 5840 u64 guest_rip = vmcs_read(GUEST_RIP); 5841 u64 guest_pending_dbg = vmcs_read(GUEST_PENDING_DEBUG); 5842 u64 dr6 = read_dr6(); 5843 const u32 expected_intr_info = INTR_INFO_VALID_MASK | 5844 INTR_TYPE_HARD_EXCEPTION | DB_VECTOR; 5845 5846 report("Expected #DB VM-exit", 5847 reason == VMX_EXC_NMI && intr_info == expected_intr_info); 5848 report("Expected RIP %p (actual %lx)", (u64)expected_rip == guest_rip, 5849 expected_rip, guest_rip); 5850 report_xfail("Expected pending debug exceptions 0 (actual %lx)", 5851 xfail_pdbg, 0 == guest_pending_dbg, guest_pending_dbg); 5852 report_xfail("Expected exit qualification %lx (actual %lx)", xfail_qual, 5853 expected_exit_qual == exit_qual, 5854 expected_exit_qual, exit_qual); 5855 report_xfail("Expected DR6 %lx (actual %lx)", xfail_dr6, 5856 expected_dr6 == dr6, expected_dr6, dr6); 5857 dismiss_db(); 5858 } 5859 5860 /* 5861 * Assuming the guest has just exited on a VMCALL instruction, skip 5862 * over the vmcall, and set the guest's RFLAGS.TF in the VMCS. If 5863 * pending debug exceptions are non-zero, set the VMCS up as if the 5864 * previous instruction was a MOVSS that generated the indicated 5865 * pending debug exceptions. Then enter L2. 5866 */ 5867 static void single_step_guest(const char *test_name, u64 starting_dr6, 5868 u64 pending_debug_exceptions) 5869 { 5870 printf("\n%s\n", test_name); 5871 skip_exit_vmcall(); 5872 write_dr6(starting_dr6); 5873 vmcs_write(GUEST_RFLAGS, X86_EFLAGS_FIXED | X86_EFLAGS_TF); 5874 if (pending_debug_exceptions) { 5875 vmcs_write(GUEST_PENDING_DEBUG, pending_debug_exceptions); 5876 vmcs_write(GUEST_INTR_STATE, GUEST_INTR_STATE_MOVSS); 5877 } 5878 enter_guest(); 5879 } 5880 5881 /* 5882 * When L1 intercepts #DB, verify that a single-step trap clears 5883 * pending debug exceptions, populates the exit qualification field 5884 * properly, and that DR6 is not prematurely clobbered. In a 5885 * (simulated) MOVSS shadow, make sure that the pending debug 5886 * exception bits are properly accumulated into the exit qualification 5887 * field. 5888 */ 5889 static void vmx_db_test(void) 5890 { 5891 /* 5892 * We are going to set a few arbitrary bits in DR6 to verify that 5893 * (a) DR6 is not modified by an intercepted #DB, and 5894 * (b) stale bits in DR6 (DR6.BD, in particular) don't leak into 5895 * the exit qualification field for a subsequent #DB exception. 5896 */ 5897 const u64 starting_dr6 = DR6_RESERVED | BIT(13) | DR_TRAP3 | DR_TRAP1; 5898 extern char post_nop asm(".Lpost_nop"); 5899 extern char post_movss_nop asm(".Lpost_movss_nop"); 5900 extern char post_wbinvd asm(".Lpost_wbinvd"); 5901 extern char post_movss_wbinvd asm(".Lpost_movss_wbinvd"); 5902 extern char xbegin asm(".Lxbegin"); 5903 extern char skip_rtm asm(".Lskip_rtm"); 5904 5905 /* 5906 * L1 wants to intercept #DB exceptions encountered in L2. 5907 */ 5908 vmcs_write(EXC_BITMAP, BIT(DB_VECTOR)); 5909 5910 /* 5911 * Start L2 and run it up to the first point of interest. 5912 */ 5913 test_set_guest(vmx_db_test_guest); 5914 enter_guest(); 5915 5916 /* 5917 * Hardware-delivered #DB trap for single-step sets the 5918 * standard that L0 has to follow for emulated instructions. 5919 */ 5920 single_step_guest("Hardware delivered single-step", starting_dr6, 0); 5921 check_db_exit(false, false, false, &post_nop, DR_STEP, starting_dr6); 5922 5923 /* 5924 * Hardware-delivered #DB trap for single-step in MOVSS shadow 5925 * also sets the standard that L0 has to follow for emulated 5926 * instructions. Here, we establish the VMCS pending debug 5927 * exceptions to indicate that the simulated MOVSS triggered a 5928 * data breakpoint as well as the single-step trap. 5929 */ 5930 single_step_guest("Hardware delivered single-step in MOVSS shadow", 5931 starting_dr6, BIT(12) | DR_STEP | DR_TRAP0 ); 5932 check_db_exit(false, false, false, &post_movss_nop, DR_STEP | DR_TRAP0, 5933 starting_dr6); 5934 5935 /* 5936 * L0 synthesized #DB trap for single-step is buggy, because 5937 * kvm (a) clobbers DR6 too early, and (b) tries its best to 5938 * reconstitute the exit qualification from the prematurely 5939 * modified DR6, but fails miserably. 5940 */ 5941 single_step_guest("Software synthesized single-step", starting_dr6, 0); 5942 check_db_exit(true, true, false, &post_wbinvd, DR_STEP, starting_dr6); 5943 5944 /* 5945 * L0 synthesized #DB trap for single-step in MOVSS shadow is 5946 * even worse, because L0 also leaves the pending debug 5947 * exceptions in the VMCS instead of accumulating them into 5948 * the exit qualification field for the #DB exception. 5949 */ 5950 single_step_guest("Software synthesized single-step in MOVSS shadow", 5951 starting_dr6, BIT(12) | DR_STEP | DR_TRAP0); 5952 check_db_exit(true, true, true, &post_movss_wbinvd, DR_STEP | DR_TRAP0, 5953 starting_dr6); 5954 5955 /* 5956 * Optional RTM test for hardware that supports RTM, to 5957 * demonstrate that the current volume 3 of the SDM 5958 * (325384-067US), table 27-1 is incorrect. Bit 16 of the exit 5959 * qualification for debug exceptions is not reserved. It is 5960 * set to 1 if a debug exception (#DB) or a breakpoint 5961 * exception (#BP) occurs inside an RTM region while advanced 5962 * debugging of RTM transactional regions is enabled. 5963 */ 5964 if (cpuid(7).b & BIT(11)) { 5965 vmcs_write(ENT_CONTROLS, 5966 vmcs_read(ENT_CONTROLS) | ENT_LOAD_DBGCTLS); 5967 /* 5968 * Set DR7.RTM[bit 11] and IA32_DEBUGCTL.RTM[bit 15] 5969 * in the guest to enable advanced debugging of RTM 5970 * transactional regions. 5971 */ 5972 vmcs_write(GUEST_DR7, BIT(11)); 5973 vmcs_write(GUEST_DEBUGCTL, BIT(15)); 5974 single_step_guest("Hardware delivered single-step in " 5975 "transactional region", starting_dr6, 0); 5976 check_db_exit(false, false, false, &xbegin, BIT(16), 5977 starting_dr6); 5978 } else { 5979 vmcs_write(GUEST_RIP, (u64)&skip_rtm); 5980 enter_guest(); 5981 } 5982 } 5983 5984 static bool cpu_has_apicv(void) 5985 { 5986 return ((ctrl_cpu_rev[1].clr & CPU_APIC_REG_VIRT) && 5987 (ctrl_cpu_rev[1].clr & CPU_VINTD) && 5988 (ctrl_pin_rev.clr & PIN_POST_INTR)); 5989 } 5990 5991 static void enable_vid(void) 5992 { 5993 void *virtual_apic_page; 5994 5995 assert(cpu_has_apicv()); 5996 5997 disable_intercept_for_x2apic_msrs(); 5998 5999 virtual_apic_page = alloc_page(); 6000 vmcs_write(APIC_VIRT_ADDR, (u64)virtual_apic_page); 6001 6002 vmcs_set_bits(PIN_CONTROLS, PIN_EXTINT); 6003 6004 vmcs_write(EOI_EXIT_BITMAP0, 0x0); 6005 vmcs_write(EOI_EXIT_BITMAP1, 0x0); 6006 vmcs_write(EOI_EXIT_BITMAP2, 0x0); 6007 vmcs_write(EOI_EXIT_BITMAP3, 0x0); 6008 6009 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_SECONDARY | CPU_TPR_SHADOW); 6010 vmcs_set_bits(CPU_EXEC_CTRL1, CPU_VINTD | CPU_VIRT_X2APIC); 6011 } 6012 6013 static void trigger_ioapic_scan_thread(void *data) 6014 { 6015 /* Wait until other CPU entered L2 */ 6016 while (vmx_get_test_stage() != 1) 6017 ; 6018 6019 /* Trigger ioapic scan */ 6020 ioapic_set_redir(0xf, 0x79, TRIGGER_LEVEL); 6021 vmx_set_test_stage(2); 6022 } 6023 6024 static void irq_79_handler_guest(isr_regs_t *regs) 6025 { 6026 eoi(); 6027 6028 /* L1 expects vmexit on VMX_VMCALL and not VMX_EOI_INDUCED */ 6029 vmcall(); 6030 } 6031 6032 /* 6033 * Constant for num of busy-loop iterations after which 6034 * a timer interrupt should have happened in host 6035 */ 6036 #define TIMER_INTERRUPT_DELAY 100000000 6037 6038 static void vmx_eoi_bitmap_ioapic_scan_test_guest(void) 6039 { 6040 handle_irq(0x79, irq_79_handler_guest); 6041 irq_enable(); 6042 6043 /* Signal to L1 CPU to trigger ioapic scan */ 6044 vmx_set_test_stage(1); 6045 /* Wait until L1 CPU to trigger ioapic scan */ 6046 while (vmx_get_test_stage() != 2) 6047 ; 6048 6049 /* 6050 * Wait for L0 timer interrupt to be raised while we run in L2 6051 * such that L0 will process the IOAPIC scan request before 6052 * resuming L2 6053 */ 6054 delay(TIMER_INTERRUPT_DELAY); 6055 6056 asm volatile ("int $0x79"); 6057 } 6058 6059 static void vmx_eoi_bitmap_ioapic_scan_test(void) 6060 { 6061 if (!cpu_has_apicv() || (cpu_count() < 2)) { 6062 report_skip(__func__); 6063 return; 6064 } 6065 6066 enable_vid(); 6067 6068 on_cpu_async(1, trigger_ioapic_scan_thread, NULL); 6069 test_set_guest(vmx_eoi_bitmap_ioapic_scan_test_guest); 6070 6071 /* 6072 * Launch L2. 6073 * We expect the exit reason to be VMX_VMCALL (and not EOI INDUCED). 6074 * In case the reason isn't VMX_VMCALL, the asserion inside 6075 * skip_exit_vmcall() will fail. 6076 */ 6077 enter_guest(); 6078 skip_exit_vmcall(); 6079 6080 /* Let L2 finish */ 6081 enter_guest(); 6082 report(__func__, 1); 6083 } 6084 6085 #define HLT_WITH_RVI_VECTOR (0xf1) 6086 6087 bool vmx_hlt_with_rvi_guest_isr_fired; 6088 static void vmx_hlt_with_rvi_guest_isr(isr_regs_t *regs) 6089 { 6090 vmx_hlt_with_rvi_guest_isr_fired = true; 6091 eoi(); 6092 } 6093 6094 static void vmx_hlt_with_rvi_guest(void) 6095 { 6096 handle_irq(HLT_WITH_RVI_VECTOR, vmx_hlt_with_rvi_guest_isr); 6097 6098 irq_enable(); 6099 asm volatile ("nop"); 6100 6101 vmcall(); 6102 } 6103 6104 static void vmx_hlt_with_rvi_test(void) 6105 { 6106 if (!cpu_has_apicv()) { 6107 report_skip(__func__); 6108 return; 6109 } 6110 6111 enable_vid(); 6112 6113 vmx_hlt_with_rvi_guest_isr_fired = false; 6114 test_set_guest(vmx_hlt_with_rvi_guest); 6115 6116 enter_guest(); 6117 skip_exit_vmcall(); 6118 6119 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 6120 vmcs_write(GUEST_INT_STATUS, HLT_WITH_RVI_VECTOR); 6121 enter_guest(); 6122 6123 report("Interrupt raised in guest", vmx_hlt_with_rvi_guest_isr_fired); 6124 } 6125 6126 static void set_irq_line_thread(void *data) 6127 { 6128 /* Wait until other CPU entered L2 */ 6129 while (vmx_get_test_stage() != 1) 6130 ; 6131 6132 /* Set irq-line 0xf to raise vector 0x78 for vCPU 0 */ 6133 ioapic_set_redir(0xf, 0x78, TRIGGER_LEVEL); 6134 vmx_set_test_stage(2); 6135 } 6136 6137 static bool irq_78_handler_vmcall_before_eoi; 6138 static void irq_78_handler_guest(isr_regs_t *regs) 6139 { 6140 set_irq_line(0xf, 0); 6141 if (irq_78_handler_vmcall_before_eoi) 6142 vmcall(); 6143 eoi(); 6144 vmcall(); 6145 } 6146 6147 static void vmx_apic_passthrough_guest(void) 6148 { 6149 handle_irq(0x78, irq_78_handler_guest); 6150 irq_enable(); 6151 6152 /* If requested, wait for other CPU to trigger ioapic scan */ 6153 if (vmx_get_test_stage() < 1) { 6154 vmx_set_test_stage(1); 6155 while (vmx_get_test_stage() != 2) 6156 ; 6157 } 6158 6159 set_irq_line(0xf, 1); 6160 } 6161 6162 static void vmx_apic_passthrough(bool set_irq_line_from_thread) 6163 { 6164 if (set_irq_line_from_thread && (cpu_count() < 2)) { 6165 report_skip(__func__); 6166 return; 6167 } 6168 6169 u64 cpu_ctrl_0 = CPU_SECONDARY; 6170 u64 cpu_ctrl_1 = 0; 6171 6172 disable_intercept_for_x2apic_msrs(); 6173 6174 vmcs_write(PIN_CONTROLS, vmcs_read(PIN_CONTROLS) & ~PIN_EXTINT); 6175 6176 vmcs_write(CPU_EXEC_CTRL0, vmcs_read(CPU_EXEC_CTRL0) | cpu_ctrl_0); 6177 vmcs_write(CPU_EXEC_CTRL1, vmcs_read(CPU_EXEC_CTRL1) | cpu_ctrl_1); 6178 6179 if (set_irq_line_from_thread) { 6180 irq_78_handler_vmcall_before_eoi = false; 6181 on_cpu_async(1, set_irq_line_thread, NULL); 6182 } else { 6183 irq_78_handler_vmcall_before_eoi = true; 6184 ioapic_set_redir(0xf, 0x78, TRIGGER_LEVEL); 6185 vmx_set_test_stage(2); 6186 } 6187 test_set_guest(vmx_apic_passthrough_guest); 6188 6189 if (irq_78_handler_vmcall_before_eoi) { 6190 /* Before EOI remote_irr should still be set */ 6191 enter_guest(); 6192 skip_exit_vmcall(); 6193 TEST_ASSERT_EQ_MSG(1, (int)ioapic_read_redir(0xf).remote_irr, 6194 "IOAPIC pass-through: remote_irr=1 before EOI"); 6195 } 6196 6197 /* After EOI remote_irr should be cleared */ 6198 enter_guest(); 6199 skip_exit_vmcall(); 6200 TEST_ASSERT_EQ_MSG(0, (int)ioapic_read_redir(0xf).remote_irr, 6201 "IOAPIC pass-through: remote_irr=0 after EOI"); 6202 6203 /* Let L2 finish */ 6204 enter_guest(); 6205 report(__func__, 1); 6206 } 6207 6208 static void vmx_apic_passthrough_test(void) 6209 { 6210 vmx_apic_passthrough(false); 6211 } 6212 6213 static void vmx_apic_passthrough_thread_test(void) 6214 { 6215 vmx_apic_passthrough(true); 6216 } 6217 6218 enum vmcs_access { 6219 ACCESS_VMREAD, 6220 ACCESS_VMWRITE, 6221 ACCESS_NONE, 6222 }; 6223 6224 struct vmcs_shadow_test_common { 6225 enum vmcs_access op; 6226 enum Reason reason; 6227 u64 field; 6228 u64 value; 6229 u64 flags; 6230 u64 time; 6231 } l1_l2_common; 6232 6233 static inline u64 vmread_flags(u64 field, u64 *val) 6234 { 6235 u64 flags; 6236 6237 asm volatile ("vmread %2, %1; pushf; pop %0" 6238 : "=r" (flags), "=rm" (*val) : "r" (field) : "cc"); 6239 return flags & X86_EFLAGS_ALU; 6240 } 6241 6242 static inline u64 vmwrite_flags(u64 field, u64 val) 6243 { 6244 u64 flags; 6245 6246 asm volatile ("vmwrite %1, %2; pushf; pop %0" 6247 : "=r"(flags) : "rm" (val), "r" (field) : "cc"); 6248 return flags & X86_EFLAGS_ALU; 6249 } 6250 6251 static void vmx_vmcs_shadow_test_guest(void) 6252 { 6253 struct vmcs_shadow_test_common *c = &l1_l2_common; 6254 u64 start; 6255 6256 while (c->op != ACCESS_NONE) { 6257 start = rdtsc(); 6258 switch (c->op) { 6259 default: 6260 c->flags = -1ull; 6261 break; 6262 case ACCESS_VMREAD: 6263 c->flags = vmread_flags(c->field, &c->value); 6264 break; 6265 case ACCESS_VMWRITE: 6266 c->flags = vmwrite_flags(c->field, 0); 6267 break; 6268 } 6269 c->time = rdtsc() - start; 6270 vmcall(); 6271 } 6272 } 6273 6274 static u64 vmread_from_shadow(u64 field) 6275 { 6276 struct vmcs *primary; 6277 struct vmcs *shadow; 6278 u64 value; 6279 6280 TEST_ASSERT(!vmcs_save(&primary)); 6281 shadow = (struct vmcs *)vmcs_read(VMCS_LINK_PTR); 6282 TEST_ASSERT(!make_vmcs_current(shadow)); 6283 value = vmcs_read(field); 6284 TEST_ASSERT(!make_vmcs_current(primary)); 6285 return value; 6286 } 6287 6288 static u64 vmwrite_to_shadow(u64 field, u64 value) 6289 { 6290 struct vmcs *primary; 6291 struct vmcs *shadow; 6292 6293 TEST_ASSERT(!vmcs_save(&primary)); 6294 shadow = (struct vmcs *)vmcs_read(VMCS_LINK_PTR); 6295 TEST_ASSERT(!make_vmcs_current(shadow)); 6296 vmcs_write(field, value); 6297 value = vmcs_read(field); 6298 TEST_ASSERT(!make_vmcs_current(primary)); 6299 return value; 6300 } 6301 6302 static void vmcs_shadow_test_access(u8 *bitmap[2], enum vmcs_access access) 6303 { 6304 struct vmcs_shadow_test_common *c = &l1_l2_common; 6305 6306 c->op = access; 6307 vmcs_write(VMX_INST_ERROR, 0); 6308 enter_guest(); 6309 c->reason = vmcs_read(EXI_REASON) & 0xffff; 6310 if (c->reason != VMX_VMCALL) { 6311 skip_exit_insn(); 6312 enter_guest(); 6313 } 6314 skip_exit_vmcall(); 6315 } 6316 6317 static void vmcs_shadow_test_field(u8 *bitmap[2], u64 field) 6318 { 6319 struct vmcs_shadow_test_common *c = &l1_l2_common; 6320 struct vmcs *shadow; 6321 u64 value; 6322 uintptr_t flags[2]; 6323 bool good_shadow; 6324 u32 vmx_inst_error; 6325 6326 report_prefix_pushf("field %lx", field); 6327 c->field = field; 6328 6329 shadow = (struct vmcs *)vmcs_read(VMCS_LINK_PTR); 6330 if (shadow != (struct vmcs *)-1ull) { 6331 flags[ACCESS_VMREAD] = vmread_flags(field, &value); 6332 flags[ACCESS_VMWRITE] = vmwrite_flags(field, value); 6333 good_shadow = !flags[ACCESS_VMREAD] && !flags[ACCESS_VMWRITE]; 6334 } else { 6335 /* 6336 * When VMCS link pointer is -1ull, VMWRITE/VMREAD on 6337 * shadowed-fields should fail with setting RFLAGS.CF. 6338 */ 6339 flags[ACCESS_VMREAD] = X86_EFLAGS_CF; 6340 flags[ACCESS_VMWRITE] = X86_EFLAGS_CF; 6341 good_shadow = false; 6342 } 6343 6344 /* Intercept both VMREAD and VMWRITE. */ 6345 report_prefix_push("no VMREAD/VMWRITE permission"); 6346 /* VMWRITE/VMREAD done on reserved-bit should always intercept */ 6347 if (!(field >> VMCS_FIELD_RESERVED_SHIFT)) { 6348 set_bit(field, bitmap[ACCESS_VMREAD]); 6349 set_bit(field, bitmap[ACCESS_VMWRITE]); 6350 } 6351 vmcs_shadow_test_access(bitmap, ACCESS_VMWRITE); 6352 report("not shadowed for VMWRITE", c->reason == VMX_VMWRITE); 6353 vmcs_shadow_test_access(bitmap, ACCESS_VMREAD); 6354 report("not shadowed for VMREAD", c->reason == VMX_VMREAD); 6355 report_prefix_pop(); 6356 6357 if (field >> VMCS_FIELD_RESERVED_SHIFT) 6358 goto out; 6359 6360 /* Permit shadowed VMREAD. */ 6361 report_prefix_push("VMREAD permission only"); 6362 clear_bit(field, bitmap[ACCESS_VMREAD]); 6363 set_bit(field, bitmap[ACCESS_VMWRITE]); 6364 if (good_shadow) 6365 value = vmwrite_to_shadow(field, MAGIC_VAL_1 + field); 6366 vmcs_shadow_test_access(bitmap, ACCESS_VMWRITE); 6367 report("not shadowed for VMWRITE", c->reason == VMX_VMWRITE); 6368 vmcs_shadow_test_access(bitmap, ACCESS_VMREAD); 6369 vmx_inst_error = vmcs_read(VMX_INST_ERROR); 6370 report("shadowed for VMREAD (in %ld cycles)", c->reason == VMX_VMCALL, 6371 c->time); 6372 report("ALU flags after VMREAD (%lx) are as expected (%lx)", 6373 c->flags == flags[ACCESS_VMREAD], 6374 c->flags, flags[ACCESS_VMREAD]); 6375 if (good_shadow) 6376 report("value read from shadow (%lx) is as expected (%lx)", 6377 c->value == value, c->value, value); 6378 else if (shadow != (struct vmcs *)-1ull && flags[ACCESS_VMREAD]) 6379 report("VMX_INST_ERROR (%d) is as expected (%d)", 6380 vmx_inst_error == VMXERR_UNSUPPORTED_VMCS_COMPONENT, 6381 vmx_inst_error, VMXERR_UNSUPPORTED_VMCS_COMPONENT); 6382 report_prefix_pop(); 6383 6384 /* Permit shadowed VMWRITE. */ 6385 report_prefix_push("VMWRITE permission only"); 6386 set_bit(field, bitmap[ACCESS_VMREAD]); 6387 clear_bit(field, bitmap[ACCESS_VMWRITE]); 6388 if (good_shadow) 6389 vmwrite_to_shadow(field, MAGIC_VAL_1 + field); 6390 vmcs_shadow_test_access(bitmap, ACCESS_VMWRITE); 6391 vmx_inst_error = vmcs_read(VMX_INST_ERROR); 6392 report("shadowed for VMWRITE (in %ld cycles)", c->reason == VMX_VMCALL, 6393 c->time); 6394 report("ALU flags after VMWRITE (%lx) are as expected (%lx)", 6395 c->flags == flags[ACCESS_VMREAD], 6396 c->flags, flags[ACCESS_VMREAD]); 6397 if (good_shadow) { 6398 value = vmread_from_shadow(field); 6399 report("shadow VMCS value (%lx) is as expected (%lx)", 6400 value == 0, value, 0ul); 6401 } else if (shadow != (struct vmcs *)-1ull && flags[ACCESS_VMWRITE]) { 6402 report("VMX_INST_ERROR (%d) is as expected (%d)", 6403 vmx_inst_error == VMXERR_UNSUPPORTED_VMCS_COMPONENT, 6404 vmx_inst_error, VMXERR_UNSUPPORTED_VMCS_COMPONENT); 6405 } 6406 vmcs_shadow_test_access(bitmap, ACCESS_VMREAD); 6407 report("not shadowed for VMREAD", c->reason == VMX_VMREAD); 6408 report_prefix_pop(); 6409 6410 /* Permit shadowed VMREAD and VMWRITE. */ 6411 report_prefix_push("VMREAD and VMWRITE permission"); 6412 clear_bit(field, bitmap[ACCESS_VMREAD]); 6413 clear_bit(field, bitmap[ACCESS_VMWRITE]); 6414 if (good_shadow) 6415 vmwrite_to_shadow(field, MAGIC_VAL_1 + field); 6416 vmcs_shadow_test_access(bitmap, ACCESS_VMWRITE); 6417 vmx_inst_error = vmcs_read(VMX_INST_ERROR); 6418 report("shadowed for VMWRITE (in %ld cycles)", c->reason == VMX_VMCALL, 6419 c->time); 6420 report("ALU flags after VMWRITE (%lx) are as expected (%lx)", 6421 c->flags == flags[ACCESS_VMREAD], 6422 c->flags, flags[ACCESS_VMREAD]); 6423 if (good_shadow) { 6424 value = vmread_from_shadow(field); 6425 report("shadow VMCS value (%lx) is as expected (%lx)", 6426 value == 0, value, 0ul); 6427 } else if (shadow != (struct vmcs *)-1ull && flags[ACCESS_VMWRITE]) { 6428 report("VMX_INST_ERROR (%d) is as expected (%d)", 6429 vmx_inst_error == VMXERR_UNSUPPORTED_VMCS_COMPONENT, 6430 vmx_inst_error, VMXERR_UNSUPPORTED_VMCS_COMPONENT); 6431 } 6432 vmcs_shadow_test_access(bitmap, ACCESS_VMREAD); 6433 vmx_inst_error = vmcs_read(VMX_INST_ERROR); 6434 report("shadowed for VMREAD (in %ld cycles)", c->reason == VMX_VMCALL, 6435 c->time); 6436 report("ALU flags after VMREAD (%lx) are as expected (%lx)", 6437 c->flags == flags[ACCESS_VMREAD], 6438 c->flags, flags[ACCESS_VMREAD]); 6439 if (good_shadow) 6440 report("value read from shadow (%lx) is as expected (%lx)", 6441 c->value == 0, c->value, 0ul); 6442 else if (shadow != (struct vmcs *)-1ull && flags[ACCESS_VMREAD]) 6443 report("VMX_INST_ERROR (%d) is as expected (%d)", 6444 vmx_inst_error == VMXERR_UNSUPPORTED_VMCS_COMPONENT, 6445 vmx_inst_error, VMXERR_UNSUPPORTED_VMCS_COMPONENT); 6446 report_prefix_pop(); 6447 6448 out: 6449 report_prefix_pop(); 6450 } 6451 6452 static void vmx_vmcs_shadow_test_body(u8 *bitmap[2]) 6453 { 6454 unsigned base; 6455 unsigned index; 6456 unsigned bit; 6457 unsigned highest_index = rdmsr(MSR_IA32_VMX_VMCS_ENUM); 6458 6459 /* Run test on all possible valid VMCS fields */ 6460 for (base = 0; 6461 base < (1 << VMCS_FIELD_RESERVED_SHIFT); 6462 base += (1 << VMCS_FIELD_TYPE_SHIFT)) 6463 for (index = 0; index <= highest_index; index++) 6464 vmcs_shadow_test_field(bitmap, base + index); 6465 6466 /* 6467 * Run tests on some invalid VMCS fields 6468 * (Have reserved bit set). 6469 */ 6470 for (bit = VMCS_FIELD_RESERVED_SHIFT; bit < VMCS_FIELD_BIT_SIZE; bit++) 6471 vmcs_shadow_test_field(bitmap, (1ull << bit)); 6472 } 6473 6474 static void vmx_vmcs_shadow_test(void) 6475 { 6476 u8 *bitmap[2]; 6477 struct vmcs *shadow; 6478 6479 if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY)) { 6480 printf("\t'Activate secondary controls' not supported.\n"); 6481 return; 6482 } 6483 6484 if (!(ctrl_cpu_rev[1].clr & CPU_SHADOW_VMCS)) { 6485 printf("\t'VMCS shadowing' not supported.\n"); 6486 return; 6487 } 6488 6489 if (!(rdmsr(MSR_IA32_VMX_MISC) & 6490 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) { 6491 printf("\tVMWRITE can't modify VM-exit information fields.\n"); 6492 return; 6493 } 6494 6495 test_set_guest(vmx_vmcs_shadow_test_guest); 6496 6497 bitmap[ACCESS_VMREAD] = alloc_page(); 6498 bitmap[ACCESS_VMWRITE] = alloc_page(); 6499 6500 vmcs_write(VMREAD_BITMAP, virt_to_phys(bitmap[ACCESS_VMREAD])); 6501 vmcs_write(VMWRITE_BITMAP, virt_to_phys(bitmap[ACCESS_VMWRITE])); 6502 6503 shadow = alloc_page(); 6504 shadow->hdr.revision_id = basic.revision; 6505 shadow->hdr.shadow_vmcs = 1; 6506 TEST_ASSERT(!vmcs_clear(shadow)); 6507 6508 vmcs_clear_bits(CPU_EXEC_CTRL0, CPU_RDTSC); 6509 vmcs_set_bits(CPU_EXEC_CTRL0, CPU_SECONDARY); 6510 vmcs_set_bits(CPU_EXEC_CTRL1, CPU_SHADOW_VMCS); 6511 6512 vmcs_write(VMCS_LINK_PTR, virt_to_phys(shadow)); 6513 report_prefix_push("valid link pointer"); 6514 vmx_vmcs_shadow_test_body(bitmap); 6515 report_prefix_pop(); 6516 6517 vmcs_write(VMCS_LINK_PTR, -1ull); 6518 report_prefix_push("invalid link pointer"); 6519 vmx_vmcs_shadow_test_body(bitmap); 6520 report_prefix_pop(); 6521 6522 l1_l2_common.op = ACCESS_NONE; 6523 enter_guest(); 6524 } 6525 6526 6527 6528 static int invalid_msr_init(struct vmcs *vmcs) 6529 { 6530 if (!(ctrl_pin_rev.clr & PIN_PREEMPT)) { 6531 printf("\tPreemption timer is not supported\n"); 6532 return VMX_TEST_EXIT; 6533 } 6534 vmcs_write(PIN_CONTROLS, vmcs_read(PIN_CONTROLS) | PIN_PREEMPT); 6535 preempt_val = 10000000; 6536 vmcs_write(PREEMPT_TIMER_VALUE, preempt_val); 6537 preempt_scale = rdmsr(MSR_IA32_VMX_MISC) & 0x1F; 6538 6539 if (!(ctrl_exit_rev.clr & EXI_SAVE_PREEMPT)) 6540 printf("\tSave preemption value is not supported\n"); 6541 6542 vmcs_write(ENT_MSR_LD_CNT, 1); 6543 vmcs_write(ENTER_MSR_LD_ADDR, (u64)0x13370000); 6544 6545 return VMX_TEST_START; 6546 } 6547 6548 6549 static void invalid_msr_main(void) 6550 { 6551 report("Invalid MSR load", 0); 6552 } 6553 6554 static int invalid_msr_exit_handler(void) 6555 { 6556 report("Invalid MSR load", 0); 6557 print_vmexit_info(); 6558 return VMX_TEST_EXIT; 6559 } 6560 6561 static int invalid_msr_entry_failure(struct vmentry_failure *failure) 6562 { 6563 ulong reason; 6564 6565 reason = vmcs_read(EXI_REASON); 6566 report("Invalid MSR load", reason == (0x80000000u | VMX_FAIL_MSR)); 6567 return VMX_TEST_VMEXIT; 6568 } 6569 6570 6571 #define TEST(name) { #name, .v2 = name } 6572 6573 /* name/init/guest_main/exit_handler/syscall_handler/guest_regs */ 6574 struct vmx_test vmx_tests[] = { 6575 { "null", NULL, basic_guest_main, basic_exit_handler, NULL, {0} }, 6576 { "vmenter", NULL, vmenter_main, vmenter_exit_handler, NULL, {0} }, 6577 { "preemption timer", preemption_timer_init, preemption_timer_main, 6578 preemption_timer_exit_handler, NULL, {0} }, 6579 { "control field PAT", test_ctrl_pat_init, test_ctrl_pat_main, 6580 test_ctrl_pat_exit_handler, NULL, {0} }, 6581 { "control field EFER", test_ctrl_efer_init, test_ctrl_efer_main, 6582 test_ctrl_efer_exit_handler, NULL, {0} }, 6583 { "CR shadowing", NULL, cr_shadowing_main, 6584 cr_shadowing_exit_handler, NULL, {0} }, 6585 { "I/O bitmap", iobmp_init, iobmp_main, iobmp_exit_handler, 6586 NULL, {0} }, 6587 { "instruction intercept", insn_intercept_init, insn_intercept_main, 6588 insn_intercept_exit_handler, NULL, {0} }, 6589 { "EPT A/D disabled", ept_init, ept_main, ept_exit_handler, NULL, {0} }, 6590 { "EPT A/D enabled", eptad_init, eptad_main, eptad_exit_handler, NULL, {0} }, 6591 { "PML", pml_init, pml_main, pml_exit_handler, NULL, {0} }, 6592 { "VPID", vpid_init, vpid_main, vpid_exit_handler, NULL, {0} }, 6593 { "interrupt", interrupt_init, interrupt_main, 6594 interrupt_exit_handler, NULL, {0} }, 6595 { "debug controls", dbgctls_init, dbgctls_main, dbgctls_exit_handler, 6596 NULL, {0} }, 6597 { "MSR switch", msr_switch_init, msr_switch_main, 6598 msr_switch_exit_handler, NULL, {0}, msr_switch_entry_failure }, 6599 { "vmmcall", vmmcall_init, vmmcall_main, vmmcall_exit_handler, NULL, {0} }, 6600 { "disable RDTSCP", disable_rdtscp_init, disable_rdtscp_main, 6601 disable_rdtscp_exit_handler, NULL, {0} }, 6602 { "int3", int3_init, int3_guest_main, int3_exit_handler, NULL, {0} }, 6603 { "into", into_init, into_guest_main, into_exit_handler, NULL, {0} }, 6604 { "exit_monitor_from_l2_test", NULL, exit_monitor_from_l2_main, 6605 exit_monitor_from_l2_handler, NULL, {0} }, 6606 { "invalid_msr", invalid_msr_init, invalid_msr_main, 6607 invalid_msr_exit_handler, NULL, {0}, invalid_msr_entry_failure}, 6608 /* Basic V2 tests. */ 6609 TEST(v2_null_test), 6610 TEST(v2_multiple_entries_test), 6611 TEST(fixture_test_case1), 6612 TEST(fixture_test_case2), 6613 /* Opcode tests. */ 6614 TEST(invvpid_test_v2), 6615 /* VM-entry tests */ 6616 TEST(vmx_controls_test), 6617 TEST(vmx_host_state_area_test), 6618 TEST(vmentry_movss_shadow_test), 6619 /* APICv tests */ 6620 TEST(vmx_eoi_bitmap_ioapic_scan_test), 6621 TEST(vmx_hlt_with_rvi_test), 6622 /* APIC pass-through tests */ 6623 TEST(vmx_apic_passthrough_test), 6624 TEST(vmx_apic_passthrough_thread_test), 6625 /* VMCS Shadowing tests */ 6626 TEST(vmx_vmcs_shadow_test), 6627 /* Regression tests */ 6628 TEST(vmx_cr_load_test), 6629 TEST(vmx_nm_test), 6630 TEST(vmx_db_test), 6631 TEST(vmx_nmi_window_test), 6632 TEST(vmx_intr_window_test), 6633 TEST(vmx_pending_event_test), 6634 TEST(vmx_pending_event_hlt_test), 6635 TEST(vmx_store_tsc_test), 6636 /* EPT access tests. */ 6637 TEST(ept_access_test_not_present), 6638 TEST(ept_access_test_read_only), 6639 TEST(ept_access_test_write_only), 6640 TEST(ept_access_test_read_write), 6641 TEST(ept_access_test_execute_only), 6642 TEST(ept_access_test_read_execute), 6643 TEST(ept_access_test_write_execute), 6644 TEST(ept_access_test_read_write_execute), 6645 TEST(ept_access_test_reserved_bits), 6646 TEST(ept_access_test_ignored_bits), 6647 TEST(ept_access_test_paddr_not_present_ad_disabled), 6648 TEST(ept_access_test_paddr_not_present_ad_enabled), 6649 TEST(ept_access_test_paddr_read_only_ad_disabled), 6650 TEST(ept_access_test_paddr_read_only_ad_enabled), 6651 TEST(ept_access_test_paddr_read_write), 6652 TEST(ept_access_test_paddr_read_write_execute), 6653 TEST(ept_access_test_paddr_read_execute_ad_disabled), 6654 TEST(ept_access_test_paddr_read_execute_ad_enabled), 6655 TEST(ept_access_test_paddr_not_present_page_fault), 6656 TEST(ept_access_test_force_2m_page), 6657 { NULL, NULL, NULL, NULL, NULL, {0} }, 6658 }; 6659