1 /* 2 * All test cases of nested virtualization should be in this file 3 * 4 * Author : Arthur Chunqi Li <yzt356@gmail.com> 5 */ 6 #include "vmx.h" 7 #include "msr.h" 8 #include "processor.h" 9 #include "vm.h" 10 #include "io.h" 11 #include "fwcfg.h" 12 #include "isr.h" 13 #include "apic.h" 14 15 u64 ia32_pat; 16 u64 ia32_efer; 17 void *io_bitmap_a, *io_bitmap_b; 18 u16 ioport; 19 20 unsigned long *pml4; 21 u64 eptp; 22 void *data_page1, *data_page2; 23 24 static inline void vmcall() 25 { 26 asm volatile("vmcall"); 27 } 28 29 void basic_guest_main() 30 { 31 } 32 33 int basic_exit_handler() 34 { 35 report("Basic VMX test", 0); 36 print_vmexit_info(); 37 return VMX_TEST_EXIT; 38 } 39 40 void vmenter_main() 41 { 42 u64 rax; 43 u64 rsp, resume_rsp; 44 45 report("test vmlaunch", 1); 46 47 asm volatile( 48 "mov %%rsp, %0\n\t" 49 "mov %3, %%rax\n\t" 50 "vmcall\n\t" 51 "mov %%rax, %1\n\t" 52 "mov %%rsp, %2\n\t" 53 : "=r"(rsp), "=r"(rax), "=r"(resume_rsp) 54 : "g"(0xABCD)); 55 report("test vmresume", (rax == 0xFFFF) && (rsp == resume_rsp)); 56 } 57 58 int vmenter_exit_handler() 59 { 60 u64 guest_rip; 61 ulong reason; 62 63 guest_rip = vmcs_read(GUEST_RIP); 64 reason = vmcs_read(EXI_REASON) & 0xff; 65 switch (reason) { 66 case VMX_VMCALL: 67 if (regs.rax != 0xABCD) { 68 report("test vmresume", 0); 69 return VMX_TEST_VMEXIT; 70 } 71 regs.rax = 0xFFFF; 72 vmcs_write(GUEST_RIP, guest_rip + 3); 73 return VMX_TEST_RESUME; 74 default: 75 report("test vmresume", 0); 76 print_vmexit_info(); 77 } 78 return VMX_TEST_VMEXIT; 79 } 80 81 u32 preempt_scale; 82 volatile unsigned long long tsc_val; 83 volatile u32 preempt_val; 84 u64 saved_rip; 85 86 int preemption_timer_init() 87 { 88 if (!(ctrl_pin_rev.clr & PIN_PREEMPT)) { 89 printf("\tPreemption timer is not supported\n"); 90 return VMX_TEST_EXIT; 91 } 92 vmcs_write(PIN_CONTROLS, vmcs_read(PIN_CONTROLS) | PIN_PREEMPT); 93 preempt_val = 10000000; 94 vmcs_write(PREEMPT_TIMER_VALUE, preempt_val); 95 preempt_scale = rdmsr(MSR_IA32_VMX_MISC) & 0x1F; 96 97 if (!(ctrl_exit_rev.clr & EXI_SAVE_PREEMPT)) 98 printf("\tSave preemption value is not supported\n"); 99 100 return VMX_TEST_START; 101 } 102 103 void preemption_timer_main() 104 { 105 tsc_val = rdtsc(); 106 if (ctrl_exit_rev.clr & EXI_SAVE_PREEMPT) { 107 vmx_set_test_stage(0); 108 vmcall(); 109 if (vmx_get_test_stage() == 1) 110 vmcall(); 111 } 112 vmx_set_test_stage(1); 113 while (vmx_get_test_stage() == 1) { 114 if (((rdtsc() - tsc_val) >> preempt_scale) 115 > 10 * preempt_val) { 116 vmx_set_test_stage(2); 117 vmcall(); 118 } 119 } 120 tsc_val = rdtsc(); 121 asm volatile ("hlt"); 122 vmcall(); 123 vmx_set_test_stage(5); 124 vmcall(); 125 } 126 127 int preemption_timer_exit_handler() 128 { 129 bool guest_halted; 130 u64 guest_rip; 131 ulong reason; 132 u32 insn_len; 133 u32 ctrl_exit; 134 135 guest_rip = vmcs_read(GUEST_RIP); 136 reason = vmcs_read(EXI_REASON) & 0xff; 137 insn_len = vmcs_read(EXI_INST_LEN); 138 switch (reason) { 139 case VMX_PREEMPT: 140 switch (vmx_get_test_stage()) { 141 case 1: 142 case 2: 143 report("busy-wait for preemption timer", 144 ((rdtsc() - tsc_val) >> preempt_scale) >= 145 preempt_val); 146 vmx_set_test_stage(3); 147 vmcs_write(PREEMPT_TIMER_VALUE, preempt_val); 148 return VMX_TEST_RESUME; 149 case 3: 150 guest_halted = 151 (vmcs_read(GUEST_ACTV_STATE) == ACTV_HLT); 152 report("preemption timer during hlt", 153 ((rdtsc() - tsc_val) >> preempt_scale) >= 154 preempt_val && guest_halted); 155 vmx_set_test_stage(4); 156 vmcs_write(PIN_CONTROLS, 157 vmcs_read(PIN_CONTROLS) & ~PIN_PREEMPT); 158 vmcs_write(GUEST_ACTV_STATE, ACTV_ACTIVE); 159 return VMX_TEST_RESUME; 160 case 4: 161 report("preemption timer with 0 value", 162 saved_rip == guest_rip); 163 break; 164 default: 165 printf("Invalid stage.\n"); 166 print_vmexit_info(); 167 break; 168 } 169 break; 170 case VMX_VMCALL: 171 vmcs_write(GUEST_RIP, guest_rip + insn_len); 172 switch (vmx_get_test_stage()) { 173 case 0: 174 report("Keep preemption value", 175 vmcs_read(PREEMPT_TIMER_VALUE) == preempt_val); 176 vmx_set_test_stage(1); 177 vmcs_write(PREEMPT_TIMER_VALUE, preempt_val); 178 ctrl_exit = (vmcs_read(EXI_CONTROLS) | 179 EXI_SAVE_PREEMPT) & ctrl_exit_rev.clr; 180 vmcs_write(EXI_CONTROLS, ctrl_exit); 181 return VMX_TEST_RESUME; 182 case 1: 183 report("Save preemption value", 184 vmcs_read(PREEMPT_TIMER_VALUE) < preempt_val); 185 return VMX_TEST_RESUME; 186 case 2: 187 report("busy-wait for preemption timer", 0); 188 vmx_set_test_stage(3); 189 vmcs_write(PREEMPT_TIMER_VALUE, preempt_val); 190 return VMX_TEST_RESUME; 191 case 3: 192 report("preemption timer during hlt", 0); 193 vmx_set_test_stage(4); 194 /* fall through */ 195 case 4: 196 vmcs_write(PIN_CONTROLS, 197 vmcs_read(PIN_CONTROLS) | PIN_PREEMPT); 198 vmcs_write(PREEMPT_TIMER_VALUE, 0); 199 saved_rip = guest_rip + insn_len; 200 return VMX_TEST_RESUME; 201 case 5: 202 report("preemption timer with 0 value (vmcall stage 5)", 0); 203 break; 204 default: 205 // Should not reach here 206 printf("ERROR : unexpected stage, %d\n", 207 vmx_get_test_stage()); 208 print_vmexit_info(); 209 return VMX_TEST_VMEXIT; 210 } 211 break; 212 default: 213 printf("Unknown exit reason, %d\n", reason); 214 print_vmexit_info(); 215 } 216 vmcs_write(PIN_CONTROLS, vmcs_read(PIN_CONTROLS) & ~PIN_PREEMPT); 217 return VMX_TEST_VMEXIT; 218 } 219 220 void msr_bmp_init() 221 { 222 void *msr_bitmap; 223 u32 ctrl_cpu0; 224 225 msr_bitmap = alloc_page(); 226 memset(msr_bitmap, 0x0, PAGE_SIZE); 227 ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0); 228 ctrl_cpu0 |= CPU_MSR_BITMAP; 229 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0); 230 vmcs_write(MSR_BITMAP, (u64)msr_bitmap); 231 } 232 233 static int test_ctrl_pat_init() 234 { 235 u64 ctrl_ent; 236 u64 ctrl_exi; 237 238 msr_bmp_init(); 239 ctrl_ent = vmcs_read(ENT_CONTROLS); 240 ctrl_exi = vmcs_read(EXI_CONTROLS); 241 vmcs_write(ENT_CONTROLS, ctrl_ent | ENT_LOAD_PAT); 242 vmcs_write(EXI_CONTROLS, ctrl_exi | (EXI_SAVE_PAT | EXI_LOAD_PAT)); 243 ia32_pat = rdmsr(MSR_IA32_CR_PAT); 244 vmcs_write(GUEST_PAT, 0x0); 245 vmcs_write(HOST_PAT, ia32_pat); 246 return VMX_TEST_START; 247 } 248 249 static void test_ctrl_pat_main() 250 { 251 u64 guest_ia32_pat; 252 253 guest_ia32_pat = rdmsr(MSR_IA32_CR_PAT); 254 if (!(ctrl_enter_rev.clr & ENT_LOAD_PAT)) 255 printf("\tENT_LOAD_PAT is not supported.\n"); 256 else { 257 if (guest_ia32_pat != 0) { 258 report("Entry load PAT", 0); 259 return; 260 } 261 } 262 wrmsr(MSR_IA32_CR_PAT, 0x6); 263 vmcall(); 264 guest_ia32_pat = rdmsr(MSR_IA32_CR_PAT); 265 if (ctrl_enter_rev.clr & ENT_LOAD_PAT) { 266 if (guest_ia32_pat != ia32_pat) { 267 report("Entry load PAT", 0); 268 return; 269 } 270 report("Entry load PAT", 1); 271 } 272 } 273 274 static int test_ctrl_pat_exit_handler() 275 { 276 u64 guest_rip; 277 ulong reason; 278 u64 guest_pat; 279 280 guest_rip = vmcs_read(GUEST_RIP); 281 reason = vmcs_read(EXI_REASON) & 0xff; 282 switch (reason) { 283 case VMX_VMCALL: 284 guest_pat = vmcs_read(GUEST_PAT); 285 if (!(ctrl_exit_rev.clr & EXI_SAVE_PAT)) { 286 printf("\tEXI_SAVE_PAT is not supported\n"); 287 vmcs_write(GUEST_PAT, 0x6); 288 } else { 289 if (guest_pat == 0x6) 290 report("Exit save PAT", 1); 291 else 292 report("Exit save PAT", 0); 293 } 294 if (!(ctrl_exit_rev.clr & EXI_LOAD_PAT)) 295 printf("\tEXI_LOAD_PAT is not supported\n"); 296 else { 297 if (rdmsr(MSR_IA32_CR_PAT) == ia32_pat) 298 report("Exit load PAT", 1); 299 else 300 report("Exit load PAT", 0); 301 } 302 vmcs_write(GUEST_PAT, ia32_pat); 303 vmcs_write(GUEST_RIP, guest_rip + 3); 304 return VMX_TEST_RESUME; 305 default: 306 printf("ERROR : Undefined exit reason, reason = %d.\n", reason); 307 break; 308 } 309 return VMX_TEST_VMEXIT; 310 } 311 312 static int test_ctrl_efer_init() 313 { 314 u64 ctrl_ent; 315 u64 ctrl_exi; 316 317 msr_bmp_init(); 318 ctrl_ent = vmcs_read(ENT_CONTROLS) | ENT_LOAD_EFER; 319 ctrl_exi = vmcs_read(EXI_CONTROLS) | EXI_SAVE_EFER | EXI_LOAD_EFER; 320 vmcs_write(ENT_CONTROLS, ctrl_ent & ctrl_enter_rev.clr); 321 vmcs_write(EXI_CONTROLS, ctrl_exi & ctrl_exit_rev.clr); 322 ia32_efer = rdmsr(MSR_EFER); 323 vmcs_write(GUEST_EFER, ia32_efer ^ EFER_NX); 324 vmcs_write(HOST_EFER, ia32_efer ^ EFER_NX); 325 return VMX_TEST_START; 326 } 327 328 static void test_ctrl_efer_main() 329 { 330 u64 guest_ia32_efer; 331 332 guest_ia32_efer = rdmsr(MSR_EFER); 333 if (!(ctrl_enter_rev.clr & ENT_LOAD_EFER)) 334 printf("\tENT_LOAD_EFER is not supported.\n"); 335 else { 336 if (guest_ia32_efer != (ia32_efer ^ EFER_NX)) { 337 report("Entry load EFER", 0); 338 return; 339 } 340 } 341 wrmsr(MSR_EFER, ia32_efer); 342 vmcall(); 343 guest_ia32_efer = rdmsr(MSR_EFER); 344 if (ctrl_enter_rev.clr & ENT_LOAD_EFER) { 345 if (guest_ia32_efer != ia32_efer) { 346 report("Entry load EFER", 0); 347 return; 348 } 349 report("Entry load EFER", 1); 350 } 351 } 352 353 static int test_ctrl_efer_exit_handler() 354 { 355 u64 guest_rip; 356 ulong reason; 357 u64 guest_efer; 358 359 guest_rip = vmcs_read(GUEST_RIP); 360 reason = vmcs_read(EXI_REASON) & 0xff; 361 switch (reason) { 362 case VMX_VMCALL: 363 guest_efer = vmcs_read(GUEST_EFER); 364 if (!(ctrl_exit_rev.clr & EXI_SAVE_EFER)) { 365 printf("\tEXI_SAVE_EFER is not supported\n"); 366 vmcs_write(GUEST_EFER, ia32_efer); 367 } else { 368 if (guest_efer == ia32_efer) 369 report("Exit save EFER", 1); 370 else 371 report("Exit save EFER", 0); 372 } 373 if (!(ctrl_exit_rev.clr & EXI_LOAD_EFER)) { 374 printf("\tEXI_LOAD_EFER is not supported\n"); 375 wrmsr(MSR_EFER, ia32_efer ^ EFER_NX); 376 } else { 377 if (rdmsr(MSR_EFER) == (ia32_efer ^ EFER_NX)) 378 report("Exit load EFER", 1); 379 else 380 report("Exit load EFER", 0); 381 } 382 vmcs_write(GUEST_PAT, ia32_efer); 383 vmcs_write(GUEST_RIP, guest_rip + 3); 384 return VMX_TEST_RESUME; 385 default: 386 printf("ERROR : Undefined exit reason, reason = %d.\n", reason); 387 break; 388 } 389 return VMX_TEST_VMEXIT; 390 } 391 392 u32 guest_cr0, guest_cr4; 393 394 static void cr_shadowing_main() 395 { 396 u32 cr0, cr4, tmp; 397 398 // Test read through 399 vmx_set_test_stage(0); 400 guest_cr0 = read_cr0(); 401 if (vmx_get_test_stage() == 1) 402 report("Read through CR0", 0); 403 else 404 vmcall(); 405 vmx_set_test_stage(1); 406 guest_cr4 = read_cr4(); 407 if (vmx_get_test_stage() == 2) 408 report("Read through CR4", 0); 409 else 410 vmcall(); 411 // Test write through 412 guest_cr0 = guest_cr0 ^ (X86_CR0_TS | X86_CR0_MP); 413 guest_cr4 = guest_cr4 ^ (X86_CR4_TSD | X86_CR4_DE); 414 vmx_set_test_stage(2); 415 write_cr0(guest_cr0); 416 if (vmx_get_test_stage() == 3) 417 report("Write throuth CR0", 0); 418 else 419 vmcall(); 420 vmx_set_test_stage(3); 421 write_cr4(guest_cr4); 422 if (vmx_get_test_stage() == 4) 423 report("Write through CR4", 0); 424 else 425 vmcall(); 426 // Test read shadow 427 vmx_set_test_stage(4); 428 vmcall(); 429 cr0 = read_cr0(); 430 if (vmx_get_test_stage() != 5) { 431 if (cr0 == guest_cr0) 432 report("Read shadowing CR0", 1); 433 else 434 report("Read shadowing CR0", 0); 435 } 436 vmx_set_test_stage(5); 437 cr4 = read_cr4(); 438 if (vmx_get_test_stage() != 6) { 439 if (cr4 == guest_cr4) 440 report("Read shadowing CR4", 1); 441 else 442 report("Read shadowing CR4", 0); 443 } 444 // Test write shadow (same value with shadow) 445 vmx_set_test_stage(6); 446 write_cr0(guest_cr0); 447 if (vmx_get_test_stage() == 7) 448 report("Write shadowing CR0 (same value with shadow)", 0); 449 else 450 vmcall(); 451 vmx_set_test_stage(7); 452 write_cr4(guest_cr4); 453 if (vmx_get_test_stage() == 8) 454 report("Write shadowing CR4 (same value with shadow)", 0); 455 else 456 vmcall(); 457 // Test write shadow (different value) 458 vmx_set_test_stage(8); 459 tmp = guest_cr0 ^ X86_CR0_TS; 460 asm volatile("mov %0, %%rsi\n\t" 461 "mov %%rsi, %%cr0\n\t" 462 ::"m"(tmp) 463 :"rsi", "memory", "cc"); 464 if (vmx_get_test_stage() != 9) 465 report("Write shadowing different X86_CR0_TS", 0); 466 else 467 report("Write shadowing different X86_CR0_TS", 1); 468 vmx_set_test_stage(9); 469 tmp = guest_cr0 ^ X86_CR0_MP; 470 asm volatile("mov %0, %%rsi\n\t" 471 "mov %%rsi, %%cr0\n\t" 472 ::"m"(tmp) 473 :"rsi", "memory", "cc"); 474 if (vmx_get_test_stage() != 10) 475 report("Write shadowing different X86_CR0_MP", 0); 476 else 477 report("Write shadowing different X86_CR0_MP", 1); 478 vmx_set_test_stage(10); 479 tmp = guest_cr4 ^ X86_CR4_TSD; 480 asm volatile("mov %0, %%rsi\n\t" 481 "mov %%rsi, %%cr4\n\t" 482 ::"m"(tmp) 483 :"rsi", "memory", "cc"); 484 if (vmx_get_test_stage() != 11) 485 report("Write shadowing different X86_CR4_TSD", 0); 486 else 487 report("Write shadowing different X86_CR4_TSD", 1); 488 vmx_set_test_stage(11); 489 tmp = guest_cr4 ^ X86_CR4_DE; 490 asm volatile("mov %0, %%rsi\n\t" 491 "mov %%rsi, %%cr4\n\t" 492 ::"m"(tmp) 493 :"rsi", "memory", "cc"); 494 if (vmx_get_test_stage() != 12) 495 report("Write shadowing different X86_CR4_DE", 0); 496 else 497 report("Write shadowing different X86_CR4_DE", 1); 498 } 499 500 static int cr_shadowing_exit_handler() 501 { 502 u64 guest_rip; 503 ulong reason; 504 u32 insn_len; 505 u32 exit_qual; 506 507 guest_rip = vmcs_read(GUEST_RIP); 508 reason = vmcs_read(EXI_REASON) & 0xff; 509 insn_len = vmcs_read(EXI_INST_LEN); 510 exit_qual = vmcs_read(EXI_QUALIFICATION); 511 switch (reason) { 512 case VMX_VMCALL: 513 switch (vmx_get_test_stage()) { 514 case 0: 515 if (guest_cr0 == vmcs_read(GUEST_CR0)) 516 report("Read through CR0", 1); 517 else 518 report("Read through CR0", 0); 519 break; 520 case 1: 521 if (guest_cr4 == vmcs_read(GUEST_CR4)) 522 report("Read through CR4", 1); 523 else 524 report("Read through CR4", 0); 525 break; 526 case 2: 527 if (guest_cr0 == vmcs_read(GUEST_CR0)) 528 report("Write through CR0", 1); 529 else 530 report("Write through CR0", 0); 531 break; 532 case 3: 533 if (guest_cr4 == vmcs_read(GUEST_CR4)) 534 report("Write through CR4", 1); 535 else 536 report("Write through CR4", 0); 537 break; 538 case 4: 539 guest_cr0 = vmcs_read(GUEST_CR0) ^ (X86_CR0_TS | X86_CR0_MP); 540 guest_cr4 = vmcs_read(GUEST_CR4) ^ (X86_CR4_TSD | X86_CR4_DE); 541 vmcs_write(CR0_MASK, X86_CR0_TS | X86_CR0_MP); 542 vmcs_write(CR0_READ_SHADOW, guest_cr0 & (X86_CR0_TS | X86_CR0_MP)); 543 vmcs_write(CR4_MASK, X86_CR4_TSD | X86_CR4_DE); 544 vmcs_write(CR4_READ_SHADOW, guest_cr4 & (X86_CR4_TSD | X86_CR4_DE)); 545 break; 546 case 6: 547 if (guest_cr0 == (vmcs_read(GUEST_CR0) ^ (X86_CR0_TS | X86_CR0_MP))) 548 report("Write shadowing CR0 (same value)", 1); 549 else 550 report("Write shadowing CR0 (same value)", 0); 551 break; 552 case 7: 553 if (guest_cr4 == (vmcs_read(GUEST_CR4) ^ (X86_CR4_TSD | X86_CR4_DE))) 554 report("Write shadowing CR4 (same value)", 1); 555 else 556 report("Write shadowing CR4 (same value)", 0); 557 break; 558 default: 559 // Should not reach here 560 printf("ERROR : unexpected stage, %d\n", 561 vmx_get_test_stage()); 562 print_vmexit_info(); 563 return VMX_TEST_VMEXIT; 564 } 565 vmcs_write(GUEST_RIP, guest_rip + insn_len); 566 return VMX_TEST_RESUME; 567 case VMX_CR: 568 switch (vmx_get_test_stage()) { 569 case 4: 570 report("Read shadowing CR0", 0); 571 vmx_inc_test_stage(); 572 break; 573 case 5: 574 report("Read shadowing CR4", 0); 575 vmx_inc_test_stage(); 576 break; 577 case 6: 578 report("Write shadowing CR0 (same value)", 0); 579 vmx_inc_test_stage(); 580 break; 581 case 7: 582 report("Write shadowing CR4 (same value)", 0); 583 vmx_inc_test_stage(); 584 break; 585 case 8: 586 case 9: 587 // 0x600 encodes "mov %esi, %cr0" 588 if (exit_qual == 0x600) 589 vmx_inc_test_stage(); 590 break; 591 case 10: 592 case 11: 593 // 0x604 encodes "mov %esi, %cr4" 594 if (exit_qual == 0x604) 595 vmx_inc_test_stage(); 596 break; 597 default: 598 // Should not reach here 599 printf("ERROR : unexpected stage, %d\n", 600 vmx_get_test_stage()); 601 print_vmexit_info(); 602 return VMX_TEST_VMEXIT; 603 } 604 vmcs_write(GUEST_RIP, guest_rip + insn_len); 605 return VMX_TEST_RESUME; 606 default: 607 printf("Unknown exit reason, %d\n", reason); 608 print_vmexit_info(); 609 } 610 return VMX_TEST_VMEXIT; 611 } 612 613 static int iobmp_init() 614 { 615 u32 ctrl_cpu0; 616 617 io_bitmap_a = alloc_page(); 618 io_bitmap_a = alloc_page(); 619 memset(io_bitmap_a, 0x0, PAGE_SIZE); 620 memset(io_bitmap_b, 0x0, PAGE_SIZE); 621 ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0); 622 ctrl_cpu0 |= CPU_IO_BITMAP; 623 ctrl_cpu0 &= (~CPU_IO); 624 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0); 625 vmcs_write(IO_BITMAP_A, (u64)io_bitmap_a); 626 vmcs_write(IO_BITMAP_B, (u64)io_bitmap_b); 627 return VMX_TEST_START; 628 } 629 630 static void iobmp_main() 631 { 632 // stage 0, test IO pass 633 vmx_set_test_stage(0); 634 inb(0x5000); 635 outb(0x0, 0x5000); 636 if (vmx_get_test_stage() != 0) 637 report("I/O bitmap - I/O pass", 0); 638 else 639 report("I/O bitmap - I/O pass", 1); 640 // test IO width, in/out 641 ((u8 *)io_bitmap_a)[0] = 0xFF; 642 vmx_set_test_stage(2); 643 inb(0x0); 644 if (vmx_get_test_stage() != 3) 645 report("I/O bitmap - trap in", 0); 646 else 647 report("I/O bitmap - trap in", 1); 648 vmx_set_test_stage(3); 649 outw(0x0, 0x0); 650 if (vmx_get_test_stage() != 4) 651 report("I/O bitmap - trap out", 0); 652 else 653 report("I/O bitmap - trap out", 1); 654 vmx_set_test_stage(4); 655 inl(0x0); 656 if (vmx_get_test_stage() != 5) 657 report("I/O bitmap - I/O width, long", 0); 658 // test low/high IO port 659 vmx_set_test_stage(5); 660 ((u8 *)io_bitmap_a)[0x5000 / 8] = (1 << (0x5000 % 8)); 661 inb(0x5000); 662 if (vmx_get_test_stage() == 6) 663 report("I/O bitmap - I/O port, low part", 1); 664 else 665 report("I/O bitmap - I/O port, low part", 0); 666 vmx_set_test_stage(6); 667 ((u8 *)io_bitmap_b)[0x1000 / 8] = (1 << (0x1000 % 8)); 668 inb(0x9000); 669 if (vmx_get_test_stage() == 7) 670 report("I/O bitmap - I/O port, high part", 1); 671 else 672 report("I/O bitmap - I/O port, high part", 0); 673 // test partial pass 674 vmx_set_test_stage(7); 675 inl(0x4FFF); 676 if (vmx_get_test_stage() == 8) 677 report("I/O bitmap - partial pass", 1); 678 else 679 report("I/O bitmap - partial pass", 0); 680 // test overrun 681 vmx_set_test_stage(8); 682 memset(io_bitmap_a, 0x0, PAGE_SIZE); 683 memset(io_bitmap_b, 0x0, PAGE_SIZE); 684 inl(0xFFFF); 685 if (vmx_get_test_stage() == 9) 686 report("I/O bitmap - overrun", 1); 687 else 688 report("I/O bitmap - overrun", 0); 689 vmx_set_test_stage(9); 690 vmcall(); 691 outb(0x0, 0x0); 692 report("I/O bitmap - ignore unconditional exiting", 693 vmx_get_test_stage() == 9); 694 vmx_set_test_stage(10); 695 vmcall(); 696 outb(0x0, 0x0); 697 report("I/O bitmap - unconditional exiting", 698 vmx_get_test_stage() == 11); 699 } 700 701 static int iobmp_exit_handler() 702 { 703 u64 guest_rip; 704 ulong reason, exit_qual; 705 u32 insn_len, ctrl_cpu0; 706 707 guest_rip = vmcs_read(GUEST_RIP); 708 reason = vmcs_read(EXI_REASON) & 0xff; 709 exit_qual = vmcs_read(EXI_QUALIFICATION); 710 insn_len = vmcs_read(EXI_INST_LEN); 711 switch (reason) { 712 case VMX_IO: 713 switch (vmx_get_test_stage()) { 714 case 0: 715 case 1: 716 vmx_inc_test_stage(); 717 break; 718 case 2: 719 if ((exit_qual & VMX_IO_SIZE_MASK) != _VMX_IO_BYTE) 720 report("I/O bitmap - I/O width, byte", 0); 721 else 722 report("I/O bitmap - I/O width, byte", 1); 723 if (!(exit_qual & VMX_IO_IN)) 724 report("I/O bitmap - I/O direction, in", 0); 725 else 726 report("I/O bitmap - I/O direction, in", 1); 727 vmx_inc_test_stage(); 728 break; 729 case 3: 730 if ((exit_qual & VMX_IO_SIZE_MASK) != _VMX_IO_WORD) 731 report("I/O bitmap - I/O width, word", 0); 732 else 733 report("I/O bitmap - I/O width, word", 1); 734 if (!(exit_qual & VMX_IO_IN)) 735 report("I/O bitmap - I/O direction, out", 1); 736 else 737 report("I/O bitmap - I/O direction, out", 0); 738 vmx_inc_test_stage(); 739 break; 740 case 4: 741 if ((exit_qual & VMX_IO_SIZE_MASK) != _VMX_IO_LONG) 742 report("I/O bitmap - I/O width, long", 0); 743 else 744 report("I/O bitmap - I/O width, long", 1); 745 vmx_inc_test_stage(); 746 break; 747 case 5: 748 if (((exit_qual & VMX_IO_PORT_MASK) >> VMX_IO_PORT_SHIFT) == 0x5000) 749 vmx_inc_test_stage(); 750 break; 751 case 6: 752 if (((exit_qual & VMX_IO_PORT_MASK) >> VMX_IO_PORT_SHIFT) == 0x9000) 753 vmx_inc_test_stage(); 754 break; 755 case 7: 756 if (((exit_qual & VMX_IO_PORT_MASK) >> VMX_IO_PORT_SHIFT) == 0x4FFF) 757 vmx_inc_test_stage(); 758 break; 759 case 8: 760 if (((exit_qual & VMX_IO_PORT_MASK) >> VMX_IO_PORT_SHIFT) == 0xFFFF) 761 vmx_inc_test_stage(); 762 break; 763 case 9: 764 case 10: 765 ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0); 766 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0 & ~CPU_IO); 767 vmx_inc_test_stage(); 768 break; 769 default: 770 // Should not reach here 771 printf("ERROR : unexpected stage, %d\n", 772 vmx_get_test_stage()); 773 print_vmexit_info(); 774 return VMX_TEST_VMEXIT; 775 } 776 vmcs_write(GUEST_RIP, guest_rip + insn_len); 777 return VMX_TEST_RESUME; 778 case VMX_VMCALL: 779 switch (vmx_get_test_stage()) { 780 case 9: 781 ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0); 782 ctrl_cpu0 |= CPU_IO | CPU_IO_BITMAP; 783 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0); 784 break; 785 case 10: 786 ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0); 787 ctrl_cpu0 = (ctrl_cpu0 & ~CPU_IO_BITMAP) | CPU_IO; 788 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0); 789 break; 790 default: 791 // Should not reach here 792 printf("ERROR : unexpected stage, %d\n", 793 vmx_get_test_stage()); 794 print_vmexit_info(); 795 return VMX_TEST_VMEXIT; 796 } 797 vmcs_write(GUEST_RIP, guest_rip + insn_len); 798 return VMX_TEST_RESUME; 799 default: 800 printf("guest_rip = 0x%llx\n", guest_rip); 801 printf("\tERROR : Undefined exit reason, reason = %d.\n", reason); 802 break; 803 } 804 return VMX_TEST_VMEXIT; 805 } 806 807 #define INSN_CPU0 0 808 #define INSN_CPU1 1 809 #define INSN_ALWAYS_TRAP 2 810 #define INSN_NEVER_TRAP 3 811 812 #define FIELD_EXIT_QUAL (1 << 0) 813 #define FIELD_INSN_INFO (1 << 1) 814 815 asm( 816 "insn_hlt: hlt;ret\n\t" 817 "insn_invlpg: invlpg 0x12345678;ret\n\t" 818 "insn_mwait: mwait;ret\n\t" 819 "insn_rdpmc: rdpmc;ret\n\t" 820 "insn_rdtsc: rdtsc;ret\n\t" 821 "insn_cr3_load: mov %rax,%cr3;ret\n\t" 822 "insn_cr3_store: mov %cr3,%rax;ret\n\t" 823 #ifdef __x86_64__ 824 "insn_cr8_load: mov %rax,%cr8;ret\n\t" 825 "insn_cr8_store: mov %cr8,%rax;ret\n\t" 826 #endif 827 "insn_monitor: monitor;ret\n\t" 828 "insn_pause: pause;ret\n\t" 829 "insn_wbinvd: wbinvd;ret\n\t" 830 "insn_cpuid: cpuid;ret\n\t" 831 "insn_invd: invd;ret\n\t" 832 ); 833 extern void insn_hlt(); 834 extern void insn_invlpg(); 835 extern void insn_mwait(); 836 extern void insn_rdpmc(); 837 extern void insn_rdtsc(); 838 extern void insn_cr3_load(); 839 extern void insn_cr3_store(); 840 #ifdef __x86_64__ 841 extern void insn_cr8_load(); 842 extern void insn_cr8_store(); 843 #endif 844 extern void insn_monitor(); 845 extern void insn_pause(); 846 extern void insn_wbinvd(); 847 extern void insn_cpuid(); 848 extern void insn_invd(); 849 850 u32 cur_insn; 851 852 struct insn_table { 853 const char *name; 854 u32 flag; 855 void (*insn_func)(); 856 u32 type; 857 u32 reason; 858 ulong exit_qual; 859 u32 insn_info; 860 // Use FIELD_EXIT_QUAL and FIELD_INSN_INFO to define 861 // which field need to be tested, reason is always tested 862 u32 test_field; 863 }; 864 865 /* 866 * Add more test cases of instruction intercept here. Elements in this 867 * table is: 868 * name/control flag/insn function/type/exit reason/exit qulification/ 869 * instruction info/field to test 870 * The last field defines which fields (exit_qual and insn_info) need to be 871 * tested in exit handler. If set to 0, only "reason" is checked. 872 */ 873 static struct insn_table insn_table[] = { 874 // Flags for Primary Processor-Based VM-Execution Controls 875 {"HLT", CPU_HLT, insn_hlt, INSN_CPU0, 12, 0, 0, 0}, 876 {"INVLPG", CPU_INVLPG, insn_invlpg, INSN_CPU0, 14, 877 0x12345678, 0, FIELD_EXIT_QUAL}, 878 {"MWAIT", CPU_MWAIT, insn_mwait, INSN_CPU0, 36, 0, 0, 0}, 879 {"RDPMC", CPU_RDPMC, insn_rdpmc, INSN_CPU0, 15, 0, 0, 0}, 880 {"RDTSC", CPU_RDTSC, insn_rdtsc, INSN_CPU0, 16, 0, 0, 0}, 881 {"CR3 load", CPU_CR3_LOAD, insn_cr3_load, INSN_CPU0, 28, 0x3, 0, 882 FIELD_EXIT_QUAL}, 883 {"CR3 store", CPU_CR3_STORE, insn_cr3_store, INSN_CPU0, 28, 0x13, 0, 884 FIELD_EXIT_QUAL}, 885 #ifdef __x86_64__ 886 {"CR8 load", CPU_CR8_LOAD, insn_cr8_load, INSN_CPU0, 28, 0x8, 0, 887 FIELD_EXIT_QUAL}, 888 {"CR8 store", CPU_CR8_STORE, insn_cr8_store, INSN_CPU0, 28, 0x18, 0, 889 FIELD_EXIT_QUAL}, 890 #endif 891 {"MONITOR", CPU_MONITOR, insn_monitor, INSN_CPU0, 39, 0, 0, 0}, 892 {"PAUSE", CPU_PAUSE, insn_pause, INSN_CPU0, 40, 0, 0, 0}, 893 // Flags for Secondary Processor-Based VM-Execution Controls 894 {"WBINVD", CPU_WBINVD, insn_wbinvd, INSN_CPU1, 54, 0, 0, 0}, 895 // Instructions always trap 896 {"CPUID", 0, insn_cpuid, INSN_ALWAYS_TRAP, 10, 0, 0, 0}, 897 {"INVD", 0, insn_invd, INSN_ALWAYS_TRAP, 13, 0, 0, 0}, 898 // Instructions never trap 899 {NULL}, 900 }; 901 902 static int insn_intercept_init() 903 { 904 u32 ctrl_cpu[2]; 905 906 ctrl_cpu[0] = vmcs_read(CPU_EXEC_CTRL0); 907 ctrl_cpu[0] |= CPU_HLT | CPU_INVLPG | CPU_MWAIT | CPU_RDPMC | CPU_RDTSC | 908 CPU_CR3_LOAD | CPU_CR3_STORE | 909 #ifdef __x86_64__ 910 CPU_CR8_LOAD | CPU_CR8_STORE | 911 #endif 912 CPU_MONITOR | CPU_PAUSE | CPU_SECONDARY; 913 ctrl_cpu[0] &= ctrl_cpu_rev[0].clr; 914 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu[0]); 915 ctrl_cpu[1] = vmcs_read(CPU_EXEC_CTRL1); 916 ctrl_cpu[1] |= CPU_WBINVD | CPU_RDRAND; 917 ctrl_cpu[1] &= ctrl_cpu_rev[1].clr; 918 vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu[1]); 919 return VMX_TEST_START; 920 } 921 922 static void insn_intercept_main() 923 { 924 cur_insn = 0; 925 while(insn_table[cur_insn].name != NULL) { 926 vmx_set_test_stage(cur_insn); 927 if ((insn_table[cur_insn].type == INSN_CPU0 928 && !(ctrl_cpu_rev[0].clr & insn_table[cur_insn].flag)) 929 || (insn_table[cur_insn].type == INSN_CPU1 930 && !(ctrl_cpu_rev[1].clr & insn_table[cur_insn].flag))) { 931 printf("\tCPU_CTRL1.CPU_%s is not supported.\n", 932 insn_table[cur_insn].name); 933 continue; 934 } 935 insn_table[cur_insn].insn_func(); 936 switch (insn_table[cur_insn].type) { 937 case INSN_CPU0: 938 case INSN_CPU1: 939 case INSN_ALWAYS_TRAP: 940 if (vmx_get_test_stage() != cur_insn + 1) 941 report(insn_table[cur_insn].name, 0); 942 else 943 report(insn_table[cur_insn].name, 1); 944 break; 945 case INSN_NEVER_TRAP: 946 if (vmx_get_test_stage() == cur_insn + 1) 947 report(insn_table[cur_insn].name, 0); 948 else 949 report(insn_table[cur_insn].name, 1); 950 break; 951 } 952 cur_insn ++; 953 } 954 } 955 956 static int insn_intercept_exit_handler() 957 { 958 u64 guest_rip; 959 u32 reason; 960 ulong exit_qual; 961 u32 insn_len; 962 u32 insn_info; 963 bool pass; 964 965 guest_rip = vmcs_read(GUEST_RIP); 966 reason = vmcs_read(EXI_REASON) & 0xff; 967 exit_qual = vmcs_read(EXI_QUALIFICATION); 968 insn_len = vmcs_read(EXI_INST_LEN); 969 insn_info = vmcs_read(EXI_INST_INFO); 970 pass = (cur_insn == vmx_get_test_stage()) && 971 insn_table[cur_insn].reason == reason; 972 if (insn_table[cur_insn].test_field & FIELD_EXIT_QUAL) 973 pass = pass && insn_table[cur_insn].exit_qual == exit_qual; 974 if (insn_table[cur_insn].test_field & FIELD_INSN_INFO) 975 pass = pass && insn_table[cur_insn].insn_info == insn_info; 976 if (pass) 977 vmx_inc_test_stage(); 978 vmcs_write(GUEST_RIP, guest_rip + insn_len); 979 return VMX_TEST_RESUME; 980 } 981 982 983 static int setup_ept() 984 { 985 int support_2m; 986 unsigned long end_of_memory; 987 988 if (!(ept_vpid.val & EPT_CAP_UC) && 989 !(ept_vpid.val & EPT_CAP_WB)) { 990 printf("\tEPT paging-structure memory type " 991 "UC&WB are not supported\n"); 992 return 1; 993 } 994 if (ept_vpid.val & EPT_CAP_UC) 995 eptp = EPT_MEM_TYPE_UC; 996 else 997 eptp = EPT_MEM_TYPE_WB; 998 if (!(ept_vpid.val & EPT_CAP_PWL4)) { 999 printf("\tPWL4 is not supported\n"); 1000 return 1; 1001 } 1002 eptp |= (3 << EPTP_PG_WALK_LEN_SHIFT); 1003 pml4 = alloc_page(); 1004 memset(pml4, 0, PAGE_SIZE); 1005 eptp |= virt_to_phys(pml4); 1006 vmcs_write(EPTP, eptp); 1007 support_2m = !!(ept_vpid.val & EPT_CAP_2M_PAGE); 1008 end_of_memory = fwcfg_get_u64(FW_CFG_RAM_SIZE); 1009 if (end_of_memory < (1ul << 32)) 1010 end_of_memory = (1ul << 32); 1011 setup_ept_range(pml4, 0, end_of_memory, 0, support_2m, 1012 EPT_WA | EPT_RA | EPT_EA); 1013 return 0; 1014 } 1015 1016 static int ept_init() 1017 { 1018 unsigned long base_addr1, base_addr2; 1019 u32 ctrl_cpu[2]; 1020 1021 if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) || 1022 !(ctrl_cpu_rev[1].clr & CPU_EPT)) { 1023 printf("\tEPT is not supported"); 1024 return VMX_TEST_EXIT; 1025 } 1026 1027 ctrl_cpu[0] = vmcs_read(CPU_EXEC_CTRL0); 1028 ctrl_cpu[1] = vmcs_read(CPU_EXEC_CTRL1); 1029 ctrl_cpu[0] = (ctrl_cpu[0] | CPU_SECONDARY) 1030 & ctrl_cpu_rev[0].clr; 1031 ctrl_cpu[1] = (ctrl_cpu[1] | CPU_EPT) 1032 & ctrl_cpu_rev[1].clr; 1033 vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu[0]); 1034 vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu[1]); 1035 if (setup_ept()) 1036 return VMX_TEST_EXIT; 1037 data_page1 = alloc_page(); 1038 data_page2 = alloc_page(); 1039 memset(data_page1, 0x0, PAGE_SIZE); 1040 memset(data_page2, 0x0, PAGE_SIZE); 1041 *((u32 *)data_page1) = MAGIC_VAL_1; 1042 *((u32 *)data_page2) = MAGIC_VAL_2; 1043 base_addr1 = (unsigned long)data_page1 & PAGE_MASK_2M; 1044 base_addr2 = (unsigned long)data_page2 & PAGE_MASK_2M; 1045 setup_ept_range(pml4, base_addr1, base_addr1 + PAGE_SIZE_2M, 0, 0, 1046 EPT_WA | EPT_RA | EPT_EA); 1047 setup_ept_range(pml4, base_addr2, base_addr2 + PAGE_SIZE_2M, 0, 0, 1048 EPT_WA | EPT_RA | EPT_EA); 1049 install_ept(pml4, (unsigned long)data_page1, (unsigned long)data_page2, 1050 EPT_RA | EPT_WA | EPT_EA); 1051 return VMX_TEST_START; 1052 } 1053 1054 static void ept_main() 1055 { 1056 vmx_set_test_stage(0); 1057 if (*((u32 *)data_page2) != MAGIC_VAL_1 || 1058 *((u32 *)data_page1) != MAGIC_VAL_1) 1059 report("EPT basic framework - read", 0); 1060 else { 1061 *((u32 *)data_page2) = MAGIC_VAL_3; 1062 vmcall(); 1063 if (vmx_get_test_stage() == 1) { 1064 if (*((u32 *)data_page1) == MAGIC_VAL_3 && 1065 *((u32 *)data_page2) == MAGIC_VAL_2) 1066 report("EPT basic framework", 1); 1067 else 1068 report("EPT basic framework - remap", 1); 1069 } 1070 } 1071 // Test EPT Misconfigurations 1072 vmx_set_test_stage(1); 1073 vmcall(); 1074 *((u32 *)data_page1) = MAGIC_VAL_1; 1075 if (vmx_get_test_stage() != 2) { 1076 report("EPT misconfigurations", 0); 1077 goto t1; 1078 } 1079 vmx_set_test_stage(2); 1080 vmcall(); 1081 *((u32 *)data_page1) = MAGIC_VAL_1; 1082 if (vmx_get_test_stage() != 3) { 1083 report("EPT misconfigurations", 0); 1084 goto t1; 1085 } 1086 report("EPT misconfigurations", 1); 1087 t1: 1088 // Test EPT violation 1089 vmx_set_test_stage(3); 1090 vmcall(); 1091 *((u32 *)data_page1) = MAGIC_VAL_1; 1092 if (vmx_get_test_stage() == 4) 1093 report("EPT violation - page permission", 1); 1094 else 1095 report("EPT violation - page permission", 0); 1096 // Violation caused by EPT paging structure 1097 vmx_set_test_stage(4); 1098 vmcall(); 1099 *((u32 *)data_page1) = MAGIC_VAL_2; 1100 if (vmx_get_test_stage() == 5) 1101 report("EPT violation - paging structure", 1); 1102 else 1103 report("EPT violation - paging structure", 0); 1104 } 1105 1106 static int ept_exit_handler() 1107 { 1108 u64 guest_rip; 1109 ulong reason; 1110 u32 insn_len; 1111 u32 exit_qual; 1112 static unsigned long data_page1_pte, data_page1_pte_pte; 1113 1114 guest_rip = vmcs_read(GUEST_RIP); 1115 reason = vmcs_read(EXI_REASON) & 0xff; 1116 insn_len = vmcs_read(EXI_INST_LEN); 1117 exit_qual = vmcs_read(EXI_QUALIFICATION); 1118 switch (reason) { 1119 case VMX_VMCALL: 1120 switch (vmx_get_test_stage()) { 1121 case 0: 1122 if (*((u32 *)data_page1) == MAGIC_VAL_3 && 1123 *((u32 *)data_page2) == MAGIC_VAL_2) { 1124 vmx_inc_test_stage(); 1125 install_ept(pml4, (unsigned long)data_page2, 1126 (unsigned long)data_page2, 1127 EPT_RA | EPT_WA | EPT_EA); 1128 } else 1129 report("EPT basic framework - write\n", 0); 1130 break; 1131 case 1: 1132 install_ept(pml4, (unsigned long)data_page1, 1133 (unsigned long)data_page1, EPT_WA); 1134 ept_sync(INVEPT_SINGLE, eptp); 1135 break; 1136 case 2: 1137 install_ept(pml4, (unsigned long)data_page1, 1138 (unsigned long)data_page1, 1139 EPT_RA | EPT_WA | EPT_EA | 1140 (2 << EPT_MEM_TYPE_SHIFT)); 1141 ept_sync(INVEPT_SINGLE, eptp); 1142 break; 1143 case 3: 1144 data_page1_pte = get_ept_pte(pml4, 1145 (unsigned long)data_page1, 1); 1146 set_ept_pte(pml4, (unsigned long)data_page1, 1147 1, data_page1_pte & (~EPT_PRESENT)); 1148 ept_sync(INVEPT_SINGLE, eptp); 1149 break; 1150 case 4: 1151 data_page1_pte = get_ept_pte(pml4, 1152 (unsigned long)data_page1, 2); 1153 data_page1_pte &= PAGE_MASK; 1154 data_page1_pte_pte = get_ept_pte(pml4, data_page1_pte, 2); 1155 set_ept_pte(pml4, data_page1_pte, 2, 1156 data_page1_pte_pte & (~EPT_PRESENT)); 1157 ept_sync(INVEPT_SINGLE, eptp); 1158 break; 1159 // Should not reach here 1160 default: 1161 printf("ERROR - unexpected stage, %d.\n", 1162 vmx_get_test_stage()); 1163 print_vmexit_info(); 1164 return VMX_TEST_VMEXIT; 1165 } 1166 vmcs_write(GUEST_RIP, guest_rip + insn_len); 1167 return VMX_TEST_RESUME; 1168 case VMX_EPT_MISCONFIG: 1169 switch (vmx_get_test_stage()) { 1170 case 1: 1171 case 2: 1172 vmx_inc_test_stage(); 1173 install_ept(pml4, (unsigned long)data_page1, 1174 (unsigned long)data_page1, 1175 EPT_RA | EPT_WA | EPT_EA); 1176 ept_sync(INVEPT_SINGLE, eptp); 1177 break; 1178 // Should not reach here 1179 default: 1180 printf("ERROR - unexpected stage, %d.\n", 1181 vmx_get_test_stage()); 1182 print_vmexit_info(); 1183 return VMX_TEST_VMEXIT; 1184 } 1185 return VMX_TEST_RESUME; 1186 case VMX_EPT_VIOLATION: 1187 switch(vmx_get_test_stage()) { 1188 case 3: 1189 if (exit_qual == (EPT_VLT_WR | EPT_VLT_LADDR_VLD | 1190 EPT_VLT_PADDR)) 1191 vmx_inc_test_stage(); 1192 set_ept_pte(pml4, (unsigned long)data_page1, 1193 1, data_page1_pte | (EPT_PRESENT)); 1194 ept_sync(INVEPT_SINGLE, eptp); 1195 break; 1196 case 4: 1197 if (exit_qual == (EPT_VLT_RD | EPT_VLT_LADDR_VLD)) 1198 vmx_inc_test_stage(); 1199 set_ept_pte(pml4, data_page1_pte, 2, 1200 data_page1_pte_pte | (EPT_PRESENT)); 1201 ept_sync(INVEPT_SINGLE, eptp); 1202 break; 1203 default: 1204 // Should not reach here 1205 printf("ERROR : unexpected stage, %d\n", 1206 vmx_get_test_stage()); 1207 print_vmexit_info(); 1208 return VMX_TEST_VMEXIT; 1209 } 1210 return VMX_TEST_RESUME; 1211 default: 1212 printf("Unknown exit reason, %d\n", reason); 1213 print_vmexit_info(); 1214 } 1215 return VMX_TEST_VMEXIT; 1216 } 1217 1218 #define TIMER_VECTOR 222 1219 1220 static volatile bool timer_fired; 1221 1222 static void timer_isr(isr_regs_t *regs) 1223 { 1224 timer_fired = true; 1225 apic_write(APIC_EOI, 0); 1226 } 1227 1228 static int interrupt_init(struct vmcs *vmcs) 1229 { 1230 msr_bmp_init(); 1231 vmcs_write(PIN_CONTROLS, vmcs_read(PIN_CONTROLS) & ~PIN_EXTINT); 1232 handle_irq(TIMER_VECTOR, timer_isr); 1233 return VMX_TEST_START; 1234 } 1235 1236 static void interrupt_main(void) 1237 { 1238 long long start, loops; 1239 1240 vmx_set_test_stage(0); 1241 1242 apic_write(APIC_LVTT, TIMER_VECTOR); 1243 irq_enable(); 1244 1245 apic_write(APIC_TMICT, 1); 1246 for (loops = 0; loops < 10000000 && !timer_fired; loops++) 1247 asm volatile ("nop"); 1248 report("direct interrupt while running guest", timer_fired); 1249 1250 apic_write(APIC_TMICT, 0); 1251 irq_disable(); 1252 vmcall(); 1253 timer_fired = false; 1254 apic_write(APIC_TMICT, 1); 1255 for (loops = 0; loops < 10000000 && !timer_fired; loops++) 1256 asm volatile ("nop"); 1257 report("intercepted interrupt while running guest", timer_fired); 1258 1259 irq_enable(); 1260 apic_write(APIC_TMICT, 0); 1261 irq_disable(); 1262 vmcall(); 1263 timer_fired = false; 1264 start = rdtsc(); 1265 apic_write(APIC_TMICT, 1000000); 1266 1267 asm volatile ("sti; hlt"); 1268 1269 report("direct interrupt + hlt", 1270 rdtsc() - start > 1000000 && timer_fired); 1271 1272 apic_write(APIC_TMICT, 0); 1273 irq_disable(); 1274 vmcall(); 1275 timer_fired = false; 1276 start = rdtsc(); 1277 apic_write(APIC_TMICT, 1000000); 1278 1279 asm volatile ("sti; hlt"); 1280 1281 report("intercepted interrupt + hlt", 1282 rdtsc() - start > 10000 && timer_fired); 1283 1284 apic_write(APIC_TMICT, 0); 1285 irq_disable(); 1286 vmcall(); 1287 timer_fired = false; 1288 start = rdtsc(); 1289 apic_write(APIC_TMICT, 1000000); 1290 1291 irq_enable(); 1292 asm volatile ("nop"); 1293 vmcall(); 1294 1295 report("direct interrupt + activity state hlt", 1296 rdtsc() - start > 10000 && timer_fired); 1297 1298 apic_write(APIC_TMICT, 0); 1299 irq_disable(); 1300 vmcall(); 1301 timer_fired = false; 1302 start = rdtsc(); 1303 apic_write(APIC_TMICT, 1000000); 1304 1305 irq_enable(); 1306 asm volatile ("nop"); 1307 vmcall(); 1308 1309 report("intercepted interrupt + activity state hlt", 1310 rdtsc() - start > 10000 && timer_fired); 1311 1312 apic_write(APIC_TMICT, 0); 1313 irq_disable(); 1314 vmx_set_test_stage(7); 1315 vmcall(); 1316 timer_fired = false; 1317 apic_write(APIC_TMICT, 1); 1318 for (loops = 0; loops < 10000000 && !timer_fired; loops++) 1319 asm volatile ("nop"); 1320 report("running a guest with interrupt acknowledgement set", timer_fired); 1321 } 1322 1323 static int interrupt_exit_handler(void) 1324 { 1325 u64 guest_rip = vmcs_read(GUEST_RIP); 1326 ulong reason = vmcs_read(EXI_REASON) & 0xff; 1327 u32 insn_len = vmcs_read(EXI_INST_LEN); 1328 1329 switch (reason) { 1330 case VMX_VMCALL: 1331 switch (vmx_get_test_stage()) { 1332 case 0: 1333 case 2: 1334 case 5: 1335 vmcs_write(PIN_CONTROLS, 1336 vmcs_read(PIN_CONTROLS) | PIN_EXTINT); 1337 break; 1338 case 7: 1339 vmcs_write(EXI_CONTROLS, vmcs_read(EXI_CONTROLS) | EXI_INTA); 1340 vmcs_write(PIN_CONTROLS, 1341 vmcs_read(PIN_CONTROLS) | PIN_EXTINT); 1342 break; 1343 case 1: 1344 case 3: 1345 vmcs_write(PIN_CONTROLS, 1346 vmcs_read(PIN_CONTROLS) & ~PIN_EXTINT); 1347 break; 1348 case 4: 1349 case 6: 1350 vmcs_write(GUEST_ACTV_STATE, ACTV_HLT); 1351 break; 1352 } 1353 vmx_inc_test_stage(); 1354 vmcs_write(GUEST_RIP, guest_rip + insn_len); 1355 return VMX_TEST_RESUME; 1356 case VMX_EXTINT: 1357 if (vmcs_read(EXI_CONTROLS) & EXI_INTA) { 1358 int vector = vmcs_read(EXI_INTR_INFO) & 0xff; 1359 handle_external_interrupt(vector); 1360 } else { 1361 irq_enable(); 1362 asm volatile ("nop"); 1363 irq_disable(); 1364 } 1365 if (vmx_get_test_stage() >= 2) { 1366 vmcs_write(GUEST_ACTV_STATE, ACTV_ACTIVE); 1367 vmcs_write(GUEST_RIP, guest_rip + insn_len); 1368 } 1369 return VMX_TEST_RESUME; 1370 default: 1371 printf("Unknown exit reason, %d\n", reason); 1372 print_vmexit_info(); 1373 } 1374 1375 return VMX_TEST_VMEXIT; 1376 } 1377 1378 /* name/init/guest_main/exit_handler/syscall_handler/guest_regs */ 1379 struct vmx_test vmx_tests[] = { 1380 { "null", NULL, basic_guest_main, basic_exit_handler, NULL, {0} }, 1381 { "vmenter", NULL, vmenter_main, vmenter_exit_handler, NULL, {0} }, 1382 { "preemption timer", preemption_timer_init, preemption_timer_main, 1383 preemption_timer_exit_handler, NULL, {0} }, 1384 { "control field PAT", test_ctrl_pat_init, test_ctrl_pat_main, 1385 test_ctrl_pat_exit_handler, NULL, {0} }, 1386 { "control field EFER", test_ctrl_efer_init, test_ctrl_efer_main, 1387 test_ctrl_efer_exit_handler, NULL, {0} }, 1388 { "CR shadowing", NULL, cr_shadowing_main, 1389 cr_shadowing_exit_handler, NULL, {0} }, 1390 { "I/O bitmap", iobmp_init, iobmp_main, iobmp_exit_handler, 1391 NULL, {0} }, 1392 { "instruction intercept", insn_intercept_init, insn_intercept_main, 1393 insn_intercept_exit_handler, NULL, {0} }, 1394 { "EPT framework", ept_init, ept_main, ept_exit_handler, NULL, {0} }, 1395 { "interrupt", interrupt_init, interrupt_main, 1396 interrupt_exit_handler, NULL, {0} }, 1397 { NULL, NULL, NULL, NULL, NULL, {0} }, 1398 }; 1399