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