17ada359dSArthur Chunqi Li /* 27ada359dSArthur Chunqi Li * x86/vmx.c : Framework for testing nested virtualization 37ada359dSArthur Chunqi Li * This is a framework to test nested VMX for KVM, which 47ada359dSArthur Chunqi Li * started as a project of GSoC 2013. All test cases should 57ada359dSArthur Chunqi Li * be located in x86/vmx_tests.c and framework related 67ada359dSArthur Chunqi Li * functions should be in this file. 77ada359dSArthur Chunqi Li * 87ada359dSArthur Chunqi Li * How to write test cases? 97ada359dSArthur Chunqi Li * Add callbacks of test suite in variant "vmx_tests". You can 107ada359dSArthur Chunqi Li * write: 117ada359dSArthur Chunqi Li * 1. init function used for initializing test suite 127ada359dSArthur Chunqi Li * 2. main function for codes running in L2 guest, 137ada359dSArthur Chunqi Li * 3. exit_handler to handle vmexit of L2 to L1 147ada359dSArthur Chunqi Li * 4. syscall handler to handle L2 syscall vmexit 157ada359dSArthur Chunqi Li * 5. vmenter fail handler to handle direct failure of vmenter 167ada359dSArthur Chunqi Li * 6. guest_regs is loaded when vmenter and saved when 177ada359dSArthur Chunqi Li * vmexit, you can read and set it in exit_handler 187ada359dSArthur Chunqi Li * If no special function is needed for a test suite, use 197ada359dSArthur Chunqi Li * coressponding basic_* functions as callback. More handlers 207ada359dSArthur Chunqi Li * can be added to "vmx_tests", see details of "struct vmx_test" 217ada359dSArthur Chunqi Li * and function test_run(). 227ada359dSArthur Chunqi Li * 237ada359dSArthur Chunqi Li * Currently, vmx test framework only set up one VCPU and one 247ada359dSArthur Chunqi Li * concurrent guest test environment with same paging for L2 and 257ada359dSArthur Chunqi Li * L1. For usage of EPT, only 1:1 mapped paging is used from VFN 267ada359dSArthur Chunqi Li * to PFN. 277ada359dSArthur Chunqi Li * 287ada359dSArthur Chunqi Li * Author : Arthur Chunqi Li <yzt356@gmail.com> 297ada359dSArthur Chunqi Li */ 307ada359dSArthur Chunqi Li 319d7eaa29SArthur Chunqi Li #include "libcflat.h" 329d7eaa29SArthur Chunqi Li #include "processor.h" 339d7eaa29SArthur Chunqi Li #include "vm.h" 349d7eaa29SArthur Chunqi Li #include "desc.h" 359d7eaa29SArthur Chunqi Li #include "vmx.h" 369d7eaa29SArthur Chunqi Li #include "msr.h" 379d7eaa29SArthur Chunqi Li #include "smp.h" 389d7eaa29SArthur Chunqi Li 39ce21d809SBandan Das u64 *vmxon_region; 409d7eaa29SArthur Chunqi Li struct vmcs *vmcs_root; 419d7eaa29SArthur Chunqi Li u32 vpid_cnt; 429d7eaa29SArthur Chunqi Li void *guest_stack, *guest_syscall_stack; 439d7eaa29SArthur Chunqi Li u32 ctrl_pin, ctrl_enter, ctrl_exit, ctrl_cpu[2]; 449d7eaa29SArthur Chunqi Li struct regs regs; 459d7eaa29SArthur Chunqi Li struct vmx_test *current; 463ee34093SArthur Chunqi Li u64 hypercall_field; 479d7eaa29SArthur Chunqi Li bool launched; 489d7eaa29SArthur Chunqi Li 493ee34093SArthur Chunqi Li union vmx_basic basic; 505f18e779SJan Kiszka union vmx_ctrl_msr ctrl_pin_rev; 515f18e779SJan Kiszka union vmx_ctrl_msr ctrl_cpu_rev[2]; 525f18e779SJan Kiszka union vmx_ctrl_msr ctrl_exit_rev; 535f18e779SJan Kiszka union vmx_ctrl_msr ctrl_enter_rev; 543ee34093SArthur Chunqi Li union vmx_ept_vpid ept_vpid; 553ee34093SArthur Chunqi Li 56337166aaSJan Kiszka extern struct descriptor_table_ptr gdt64_desc; 57337166aaSJan Kiszka extern struct descriptor_table_ptr idt_descr; 58337166aaSJan Kiszka extern struct descriptor_table_ptr tss_descr; 599d7eaa29SArthur Chunqi Li extern void *vmx_return; 609d7eaa29SArthur Chunqi Li extern void *entry_sysenter; 619d7eaa29SArthur Chunqi Li extern void *guest_entry; 629d7eaa29SArthur Chunqi Li 63ffb1a9e0SJan Kiszka static volatile u32 stage; 64ffb1a9e0SJan Kiszka 65ffb1a9e0SJan Kiszka void vmx_set_test_stage(u32 s) 66ffb1a9e0SJan Kiszka { 67ffb1a9e0SJan Kiszka barrier(); 68ffb1a9e0SJan Kiszka stage = s; 69ffb1a9e0SJan Kiszka barrier(); 70ffb1a9e0SJan Kiszka } 71ffb1a9e0SJan Kiszka 72ffb1a9e0SJan Kiszka u32 vmx_get_test_stage(void) 73ffb1a9e0SJan Kiszka { 74ffb1a9e0SJan Kiszka u32 s; 75ffb1a9e0SJan Kiszka 76ffb1a9e0SJan Kiszka barrier(); 77ffb1a9e0SJan Kiszka s = stage; 78ffb1a9e0SJan Kiszka barrier(); 79ffb1a9e0SJan Kiszka return s; 80ffb1a9e0SJan Kiszka } 81ffb1a9e0SJan Kiszka 82ffb1a9e0SJan Kiszka void vmx_inc_test_stage(void) 83ffb1a9e0SJan Kiszka { 84ffb1a9e0SJan Kiszka barrier(); 85ffb1a9e0SJan Kiszka stage++; 86ffb1a9e0SJan Kiszka barrier(); 87ffb1a9e0SJan Kiszka } 88ffb1a9e0SJan Kiszka 899d7eaa29SArthur Chunqi Li static int make_vmcs_current(struct vmcs *vmcs) 909d7eaa29SArthur Chunqi Li { 919d7eaa29SArthur Chunqi Li bool ret; 92a739f560SBandan Das u64 rflags = read_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF; 939d7eaa29SArthur Chunqi Li 94a739f560SBandan Das asm volatile ("push %1; popf; vmptrld %2; setbe %0" 95a739f560SBandan Das : "=q" (ret) : "q" (rflags), "m" (vmcs) : "cc"); 969d7eaa29SArthur Chunqi Li return ret; 979d7eaa29SArthur Chunqi Li } 989d7eaa29SArthur Chunqi Li 999d7eaa29SArthur Chunqi Li /* entry_sysenter */ 1009d7eaa29SArthur Chunqi Li asm( 1019d7eaa29SArthur Chunqi Li ".align 4, 0x90\n\t" 1029d7eaa29SArthur Chunqi Li ".globl entry_sysenter\n\t" 1039d7eaa29SArthur Chunqi Li "entry_sysenter:\n\t" 1049d7eaa29SArthur Chunqi Li SAVE_GPR 1059d7eaa29SArthur Chunqi Li " and $0xf, %rax\n\t" 1069d7eaa29SArthur Chunqi Li " mov %rax, %rdi\n\t" 1079d7eaa29SArthur Chunqi Li " call syscall_handler\n\t" 1089d7eaa29SArthur Chunqi Li LOAD_GPR 1099d7eaa29SArthur Chunqi Li " vmresume\n\t" 1109d7eaa29SArthur Chunqi Li ); 1119d7eaa29SArthur Chunqi Li 1129d7eaa29SArthur Chunqi Li static void __attribute__((__used__)) syscall_handler(u64 syscall_no) 1139d7eaa29SArthur Chunqi Li { 114d5315e3dSJan Kiszka if (current->syscall_handler) 1159d7eaa29SArthur Chunqi Li current->syscall_handler(syscall_no); 1169d7eaa29SArthur Chunqi Li } 1179d7eaa29SArthur Chunqi Li 1189d7eaa29SArthur Chunqi Li static inline int vmx_on() 1199d7eaa29SArthur Chunqi Li { 1209d7eaa29SArthur Chunqi Li bool ret; 121a739f560SBandan Das u64 rflags = read_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF; 122a739f560SBandan Das asm volatile ("push %1; popf; vmxon %2; setbe %0\n\t" 123a739f560SBandan Das : "=q" (ret) : "q" (rflags), "m" (vmxon_region) : "cc"); 1249d7eaa29SArthur Chunqi Li return ret; 1259d7eaa29SArthur Chunqi Li } 1269d7eaa29SArthur Chunqi Li 1279d7eaa29SArthur Chunqi Li static inline int vmx_off() 1289d7eaa29SArthur Chunqi Li { 1299d7eaa29SArthur Chunqi Li bool ret; 130a739f560SBandan Das u64 rflags = read_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF; 131a739f560SBandan Das 132a739f560SBandan Das asm volatile("push %1; popf; vmxoff; setbe %0\n\t" 133a739f560SBandan Das : "=q"(ret) : "q" (rflags) : "cc"); 1349d7eaa29SArthur Chunqi Li return ret; 1359d7eaa29SArthur Chunqi Li } 1369d7eaa29SArthur Chunqi Li 1373ee34093SArthur Chunqi Li void print_vmexit_info() 1389d7eaa29SArthur Chunqi Li { 1399d7eaa29SArthur Chunqi Li u64 guest_rip, guest_rsp; 1409d7eaa29SArthur Chunqi Li ulong reason = vmcs_read(EXI_REASON) & 0xff; 1419d7eaa29SArthur Chunqi Li ulong exit_qual = vmcs_read(EXI_QUALIFICATION); 1429d7eaa29SArthur Chunqi Li guest_rip = vmcs_read(GUEST_RIP); 1439d7eaa29SArthur Chunqi Li guest_rsp = vmcs_read(GUEST_RSP); 1449d7eaa29SArthur Chunqi Li printf("VMEXIT info:\n"); 145b006d7ebSAndrew Jones printf("\tvmexit reason = %ld\n", reason); 146b006d7ebSAndrew Jones printf("\texit qualification = 0x%lx\n", exit_qual); 147b006d7ebSAndrew Jones printf("\tBit 31 of reason = %lx\n", (vmcs_read(EXI_REASON) >> 31) & 1); 148b006d7ebSAndrew Jones printf("\tguest_rip = 0x%lx\n", guest_rip); 149b006d7ebSAndrew Jones printf("\tRAX=0x%lx RBX=0x%lx RCX=0x%lx RDX=0x%lx\n", 1509d7eaa29SArthur Chunqi Li regs.rax, regs.rbx, regs.rcx, regs.rdx); 151b006d7ebSAndrew Jones printf("\tRSP=0x%lx RBP=0x%lx RSI=0x%lx RDI=0x%lx\n", 1529d7eaa29SArthur Chunqi Li guest_rsp, regs.rbp, regs.rsi, regs.rdi); 153b006d7ebSAndrew Jones printf("\tR8 =0x%lx R9 =0x%lx R10=0x%lx R11=0x%lx\n", 1549d7eaa29SArthur Chunqi Li regs.r8, regs.r9, regs.r10, regs.r11); 155b006d7ebSAndrew Jones printf("\tR12=0x%lx R13=0x%lx R14=0x%lx R15=0x%lx\n", 1569d7eaa29SArthur Chunqi Li regs.r12, regs.r13, regs.r14, regs.r15); 1579d7eaa29SArthur Chunqi Li } 1589d7eaa29SArthur Chunqi Li 1593b50efe3SPeter Feiner void 1603b50efe3SPeter Feiner print_vmentry_failure_info(struct vmentry_failure *failure) { 1613b50efe3SPeter Feiner if (failure->early) { 1623b50efe3SPeter Feiner printf("Early %s failure: ", failure->instr); 1633b50efe3SPeter Feiner switch (failure->flags & VMX_ENTRY_FLAGS) { 164ce154ba8SPaolo Bonzini case X86_EFLAGS_CF: 1653b50efe3SPeter Feiner printf("current-VMCS pointer is not valid.\n"); 1663b50efe3SPeter Feiner break; 167ce154ba8SPaolo Bonzini case X86_EFLAGS_ZF: 1683b50efe3SPeter Feiner printf("error number is %ld. See Intel 30.4.\n", 1693b50efe3SPeter Feiner vmcs_read(VMX_INST_ERROR)); 1703b50efe3SPeter Feiner break; 1713b50efe3SPeter Feiner default: 1723b50efe3SPeter Feiner printf("unexpected flags %lx!\n", failure->flags); 1733b50efe3SPeter Feiner } 1743b50efe3SPeter Feiner } else { 1753b50efe3SPeter Feiner u64 reason = vmcs_read(EXI_REASON); 1763b50efe3SPeter Feiner u64 qual = vmcs_read(EXI_QUALIFICATION); 1773b50efe3SPeter Feiner 1783b50efe3SPeter Feiner printf("Non-early %s failure (reason=0x%lx, qual=0x%lx): ", 1793b50efe3SPeter Feiner failure->instr, reason, qual); 1803b50efe3SPeter Feiner 1813b50efe3SPeter Feiner switch (reason & 0xff) { 1823b50efe3SPeter Feiner case VMX_FAIL_STATE: 1833b50efe3SPeter Feiner printf("invalid guest state\n"); 1843b50efe3SPeter Feiner break; 1853b50efe3SPeter Feiner case VMX_FAIL_MSR: 1863b50efe3SPeter Feiner printf("MSR loading\n"); 1873b50efe3SPeter Feiner break; 1883b50efe3SPeter Feiner case VMX_FAIL_MCHECK: 1893b50efe3SPeter Feiner printf("machine-check event\n"); 1903b50efe3SPeter Feiner break; 1913b50efe3SPeter Feiner default: 1923b50efe3SPeter Feiner printf("unexpected basic exit reason %ld\n", 1933b50efe3SPeter Feiner reason & 0xff); 1943b50efe3SPeter Feiner } 1953b50efe3SPeter Feiner 1963b50efe3SPeter Feiner if (!(reason & VMX_ENTRY_FAILURE)) 1973b50efe3SPeter Feiner printf("\tVMX_ENTRY_FAILURE BIT NOT SET!\n"); 1983b50efe3SPeter Feiner 1993b50efe3SPeter Feiner if (reason & 0x7fff0000) 2003b50efe3SPeter Feiner printf("\tRESERVED BITS SET!\n"); 2013b50efe3SPeter Feiner } 2023b50efe3SPeter Feiner } 2033b50efe3SPeter Feiner 2043b50efe3SPeter Feiner 2059d7eaa29SArthur Chunqi Li static void test_vmclear(void) 2069d7eaa29SArthur Chunqi Li { 207daeec979SBandan Das struct vmcs *tmp_root; 208e2cf1c9dSEduardo Habkost int width = cpuid_maxphyaddr(); 209daeec979SBandan Das 210daeec979SBandan Das /* 211daeec979SBandan Das * Note- The tests below do not necessarily have a 212daeec979SBandan Das * valid VMCS, but that's ok since the invalid vmcs 213daeec979SBandan Das * is only used for a specific test and is discarded 214daeec979SBandan Das * without touching its contents 215daeec979SBandan Das */ 216daeec979SBandan Das 217daeec979SBandan Das /* Unaligned page access */ 218daeec979SBandan Das tmp_root = (struct vmcs *)((intptr_t)vmcs_root + 1); 219daeec979SBandan Das report("test vmclear with unaligned vmcs", 220daeec979SBandan Das vmcs_clear(tmp_root) == 1); 221daeec979SBandan Das 222daeec979SBandan Das /* gpa bits beyond physical address width are set*/ 223daeec979SBandan Das tmp_root = (struct vmcs *)((intptr_t)vmcs_root | 224daeec979SBandan Das ((u64)1 << (width+1))); 225daeec979SBandan Das report("test vmclear with vmcs address bits set beyond physical address width", 226daeec979SBandan Das vmcs_clear(tmp_root) == 1); 227daeec979SBandan Das 228daeec979SBandan Das /* Pass VMXON region */ 229daeec979SBandan Das tmp_root = (struct vmcs *)vmxon_region; 230daeec979SBandan Das report("test vmclear with vmxon region", 231daeec979SBandan Das vmcs_clear(tmp_root) == 1); 232daeec979SBandan Das 233daeec979SBandan Das /* Valid VMCS */ 234daeec979SBandan Das report("test vmclear with valid vmcs region", vmcs_clear(vmcs_root) == 0); 235daeec979SBandan Das 2369d7eaa29SArthur Chunqi Li } 2379d7eaa29SArthur Chunqi Li 2389d7eaa29SArthur Chunqi Li static void test_vmxoff(void) 2399d7eaa29SArthur Chunqi Li { 2409d7eaa29SArthur Chunqi Li int ret; 2419d7eaa29SArthur Chunqi Li 2429d7eaa29SArthur Chunqi Li ret = vmx_off(); 2439d7eaa29SArthur Chunqi Li report("test vmxoff", !ret); 2449d7eaa29SArthur Chunqi Li } 2459d7eaa29SArthur Chunqi Li 2469d7eaa29SArthur Chunqi Li static void __attribute__((__used__)) guest_main(void) 2479d7eaa29SArthur Chunqi Li { 2489d7eaa29SArthur Chunqi Li current->guest_main(); 2499d7eaa29SArthur Chunqi Li } 2509d7eaa29SArthur Chunqi Li 2519d7eaa29SArthur Chunqi Li /* guest_entry */ 2529d7eaa29SArthur Chunqi Li asm( 2539d7eaa29SArthur Chunqi Li ".align 4, 0x90\n\t" 2549d7eaa29SArthur Chunqi Li ".globl entry_guest\n\t" 2559d7eaa29SArthur Chunqi Li "guest_entry:\n\t" 2569d7eaa29SArthur Chunqi Li " call guest_main\n\t" 2579d7eaa29SArthur Chunqi Li " mov $1, %edi\n\t" 2589d7eaa29SArthur Chunqi Li " call hypercall\n\t" 2599d7eaa29SArthur Chunqi Li ); 2609d7eaa29SArthur Chunqi Li 2616884af61SArthur Chunqi Li /* EPT paging structure related functions */ 26269c531c8SPeter Feiner /* split_large_ept_entry: Split a 2M/1G large page into 512 smaller PTEs. 26369c531c8SPeter Feiner @ptep : large page table entry to split 26469c531c8SPeter Feiner @level : level of ptep (2 or 3) 26569c531c8SPeter Feiner */ 26669c531c8SPeter Feiner static void split_large_ept_entry(unsigned long *ptep, int level) 26769c531c8SPeter Feiner { 26869c531c8SPeter Feiner unsigned long *new_pt; 26969c531c8SPeter Feiner unsigned long gpa; 27069c531c8SPeter Feiner unsigned long pte; 27169c531c8SPeter Feiner unsigned long prototype; 27269c531c8SPeter Feiner int i; 27369c531c8SPeter Feiner 27469c531c8SPeter Feiner pte = *ptep; 27569c531c8SPeter Feiner assert(pte & EPT_PRESENT); 27669c531c8SPeter Feiner assert(pte & EPT_LARGE_PAGE); 27769c531c8SPeter Feiner assert(level == 2 || level == 3); 27869c531c8SPeter Feiner 27969c531c8SPeter Feiner new_pt = alloc_page(); 28069c531c8SPeter Feiner assert(new_pt); 28169c531c8SPeter Feiner memset(new_pt, 0, PAGE_SIZE); 28269c531c8SPeter Feiner 28369c531c8SPeter Feiner prototype = pte & ~EPT_ADDR_MASK; 28469c531c8SPeter Feiner if (level == 2) 28569c531c8SPeter Feiner prototype &= ~EPT_LARGE_PAGE; 28669c531c8SPeter Feiner 28769c531c8SPeter Feiner gpa = pte & EPT_ADDR_MASK; 28869c531c8SPeter Feiner for (i = 0; i < EPT_PGDIR_ENTRIES; i++) { 28969c531c8SPeter Feiner new_pt[i] = prototype | gpa; 29069c531c8SPeter Feiner gpa += 1ul << EPT_LEVEL_SHIFT(level - 1); 29169c531c8SPeter Feiner } 29269c531c8SPeter Feiner 29369c531c8SPeter Feiner pte &= ~EPT_LARGE_PAGE; 29469c531c8SPeter Feiner pte &= ~EPT_ADDR_MASK; 29569c531c8SPeter Feiner pte |= virt_to_phys(new_pt); 29669c531c8SPeter Feiner 29769c531c8SPeter Feiner *ptep = pte; 29869c531c8SPeter Feiner } 29969c531c8SPeter Feiner 3006884af61SArthur Chunqi Li /* install_ept_entry : Install a page to a given level in EPT 3016884af61SArthur Chunqi Li @pml4 : addr of pml4 table 3026884af61SArthur Chunqi Li @pte_level : level of PTE to set 3036884af61SArthur Chunqi Li @guest_addr : physical address of guest 3046884af61SArthur Chunqi Li @pte : pte value to set 3056884af61SArthur Chunqi Li @pt_page : address of page table, NULL for a new page 3066884af61SArthur Chunqi Li */ 3076884af61SArthur Chunqi Li void install_ept_entry(unsigned long *pml4, 3086884af61SArthur Chunqi Li int pte_level, 3096884af61SArthur Chunqi Li unsigned long guest_addr, 3106884af61SArthur Chunqi Li unsigned long pte, 3116884af61SArthur Chunqi Li unsigned long *pt_page) 3126884af61SArthur Chunqi Li { 3136884af61SArthur Chunqi Li int level; 3146884af61SArthur Chunqi Li unsigned long *pt = pml4; 3156884af61SArthur Chunqi Li unsigned offset; 3166884af61SArthur Chunqi Li 3176884af61SArthur Chunqi Li for (level = EPT_PAGE_LEVEL; level > pte_level; --level) { 318a969e087SPeter Feiner offset = (guest_addr >> EPT_LEVEL_SHIFT(level)) 3196884af61SArthur Chunqi Li & EPT_PGDIR_MASK; 3206884af61SArthur Chunqi Li if (!(pt[offset] & (EPT_PRESENT))) { 3216884af61SArthur Chunqi Li unsigned long *new_pt = pt_page; 3226884af61SArthur Chunqi Li if (!new_pt) 3236884af61SArthur Chunqi Li new_pt = alloc_page(); 3246884af61SArthur Chunqi Li else 3256884af61SArthur Chunqi Li pt_page = 0; 3266884af61SArthur Chunqi Li memset(new_pt, 0, PAGE_SIZE); 3276884af61SArthur Chunqi Li pt[offset] = virt_to_phys(new_pt) 3286884af61SArthur Chunqi Li | EPT_RA | EPT_WA | EPT_EA; 32969c531c8SPeter Feiner } else if (pt[offset] & EPT_LARGE_PAGE) 33069c531c8SPeter Feiner split_large_ept_entry(&pt[offset], level); 33100b5c590SPeter Feiner pt = phys_to_virt(pt[offset] & EPT_ADDR_MASK); 3326884af61SArthur Chunqi Li } 333a969e087SPeter Feiner offset = (guest_addr >> EPT_LEVEL_SHIFT(level)) & EPT_PGDIR_MASK; 3346884af61SArthur Chunqi Li pt[offset] = pte; 3356884af61SArthur Chunqi Li } 3366884af61SArthur Chunqi Li 3376884af61SArthur Chunqi Li /* Map a page, @perm is the permission of the page */ 3386884af61SArthur Chunqi Li void install_ept(unsigned long *pml4, 3396884af61SArthur Chunqi Li unsigned long phys, 3406884af61SArthur Chunqi Li unsigned long guest_addr, 3416884af61SArthur Chunqi Li u64 perm) 3426884af61SArthur Chunqi Li { 3436884af61SArthur Chunqi Li install_ept_entry(pml4, 1, guest_addr, (phys & PAGE_MASK) | perm, 0); 3446884af61SArthur Chunqi Li } 3456884af61SArthur Chunqi Li 3466884af61SArthur Chunqi Li /* Map a 1G-size page */ 3476884af61SArthur Chunqi Li void install_1g_ept(unsigned long *pml4, 3486884af61SArthur Chunqi Li unsigned long phys, 3496884af61SArthur Chunqi Li unsigned long guest_addr, 3506884af61SArthur Chunqi Li u64 perm) 3516884af61SArthur Chunqi Li { 3526884af61SArthur Chunqi Li install_ept_entry(pml4, 3, guest_addr, 3536884af61SArthur Chunqi Li (phys & PAGE_MASK) | perm | EPT_LARGE_PAGE, 0); 3546884af61SArthur Chunqi Li } 3556884af61SArthur Chunqi Li 3566884af61SArthur Chunqi Li /* Map a 2M-size page */ 3576884af61SArthur Chunqi Li void install_2m_ept(unsigned long *pml4, 3586884af61SArthur Chunqi Li unsigned long phys, 3596884af61SArthur Chunqi Li unsigned long guest_addr, 3606884af61SArthur Chunqi Li u64 perm) 3616884af61SArthur Chunqi Li { 3626884af61SArthur Chunqi Li install_ept_entry(pml4, 2, guest_addr, 3636884af61SArthur Chunqi Li (phys & PAGE_MASK) | perm | EPT_LARGE_PAGE, 0); 3646884af61SArthur Chunqi Li } 3656884af61SArthur Chunqi Li 3666884af61SArthur Chunqi Li /* setup_ept_range : Setup a range of 1:1 mapped page to EPT paging structure. 3676884af61SArthur Chunqi Li @start : start address of guest page 3686884af61SArthur Chunqi Li @len : length of address to be mapped 3696884af61SArthur Chunqi Li @map_1g : whether 1G page map is used 3706884af61SArthur Chunqi Li @map_2m : whether 2M page map is used 3716884af61SArthur Chunqi Li @perm : permission for every page 3726884af61SArthur Chunqi Li */ 373b947e241SJan Kiszka void setup_ept_range(unsigned long *pml4, unsigned long start, 3746884af61SArthur Chunqi Li unsigned long len, int map_1g, int map_2m, u64 perm) 3756884af61SArthur Chunqi Li { 3766884af61SArthur Chunqi Li u64 phys = start; 3776884af61SArthur Chunqi Li u64 max = (u64)len + (u64)start; 3786884af61SArthur Chunqi Li 3796884af61SArthur Chunqi Li if (map_1g) { 3806884af61SArthur Chunqi Li while (phys + PAGE_SIZE_1G <= max) { 3816884af61SArthur Chunqi Li install_1g_ept(pml4, phys, phys, perm); 3826884af61SArthur Chunqi Li phys += PAGE_SIZE_1G; 3836884af61SArthur Chunqi Li } 3846884af61SArthur Chunqi Li } 3856884af61SArthur Chunqi Li if (map_2m) { 3866884af61SArthur Chunqi Li while (phys + PAGE_SIZE_2M <= max) { 3876884af61SArthur Chunqi Li install_2m_ept(pml4, phys, phys, perm); 3886884af61SArthur Chunqi Li phys += PAGE_SIZE_2M; 3896884af61SArthur Chunqi Li } 3906884af61SArthur Chunqi Li } 3916884af61SArthur Chunqi Li while (phys + PAGE_SIZE <= max) { 3926884af61SArthur Chunqi Li install_ept(pml4, phys, phys, perm); 3936884af61SArthur Chunqi Li phys += PAGE_SIZE; 3946884af61SArthur Chunqi Li } 3956884af61SArthur Chunqi Li } 3966884af61SArthur Chunqi Li 3976884af61SArthur Chunqi Li /* get_ept_pte : Get the PTE of a given level in EPT, 3986884af61SArthur Chunqi Li @level == 1 means get the latest level*/ 3996884af61SArthur Chunqi Li unsigned long get_ept_pte(unsigned long *pml4, 4006884af61SArthur Chunqi Li unsigned long guest_addr, int level) 4016884af61SArthur Chunqi Li { 4026884af61SArthur Chunqi Li int l; 4036884af61SArthur Chunqi Li unsigned long *pt = pml4, pte; 4046884af61SArthur Chunqi Li unsigned offset; 4056884af61SArthur Chunqi Li 4062ca6f1f3SPaolo Bonzini if (level < 1 || level > 3) 4072ca6f1f3SPaolo Bonzini return -1; 4082ca6f1f3SPaolo Bonzini for (l = EPT_PAGE_LEVEL; ; --l) { 409a969e087SPeter Feiner offset = (guest_addr >> EPT_LEVEL_SHIFT(l)) & EPT_PGDIR_MASK; 4106884af61SArthur Chunqi Li pte = pt[offset]; 4116884af61SArthur Chunqi Li if (!(pte & (EPT_PRESENT))) 4126884af61SArthur Chunqi Li return 0; 4136884af61SArthur Chunqi Li if (l == level) 4142ca6f1f3SPaolo Bonzini break; 4156884af61SArthur Chunqi Li if (l < 4 && (pte & EPT_LARGE_PAGE)) 4166884af61SArthur Chunqi Li return pte; 41700b5c590SPeter Feiner pt = (unsigned long *)(pte & EPT_ADDR_MASK); 4186884af61SArthur Chunqi Li } 419a969e087SPeter Feiner offset = (guest_addr >> EPT_LEVEL_SHIFT(l)) & EPT_PGDIR_MASK; 4206884af61SArthur Chunqi Li pte = pt[offset]; 4216884af61SArthur Chunqi Li return pte; 4226884af61SArthur Chunqi Li } 4236884af61SArthur Chunqi Li 4242f888fccSBandan Das void ept_sync(int type, u64 eptp) 4252f888fccSBandan Das { 4262f888fccSBandan Das switch (type) { 4272f888fccSBandan Das case INVEPT_SINGLE: 4282f888fccSBandan Das if (ept_vpid.val & EPT_CAP_INVEPT_SINGLE) { 4292f888fccSBandan Das invept(INVEPT_SINGLE, eptp); 4302f888fccSBandan Das break; 4312f888fccSBandan Das } 4322f888fccSBandan Das /* else fall through */ 4332f888fccSBandan Das case INVEPT_GLOBAL: 4342f888fccSBandan Das if (ept_vpid.val & EPT_CAP_INVEPT_ALL) { 4352f888fccSBandan Das invept(INVEPT_GLOBAL, eptp); 4362f888fccSBandan Das break; 4372f888fccSBandan Das } 4382f888fccSBandan Das /* else fall through */ 4392f888fccSBandan Das default: 4402f888fccSBandan Das printf("WARNING: invept is not supported!\n"); 4412f888fccSBandan Das } 4422f888fccSBandan Das } 4432f888fccSBandan Das 4446884af61SArthur Chunqi Li int set_ept_pte(unsigned long *pml4, unsigned long guest_addr, 4456884af61SArthur Chunqi Li int level, u64 pte_val) 4466884af61SArthur Chunqi Li { 4476884af61SArthur Chunqi Li int l; 4486884af61SArthur Chunqi Li unsigned long *pt = pml4; 4496884af61SArthur Chunqi Li unsigned offset; 4506884af61SArthur Chunqi Li 4516884af61SArthur Chunqi Li if (level < 1 || level > 3) 4526884af61SArthur Chunqi Li return -1; 4532ca6f1f3SPaolo Bonzini for (l = EPT_PAGE_LEVEL; ; --l) { 454a969e087SPeter Feiner offset = (guest_addr >> EPT_LEVEL_SHIFT(l)) & EPT_PGDIR_MASK; 4552ca6f1f3SPaolo Bonzini if (l == level) 4562ca6f1f3SPaolo Bonzini break; 4576884af61SArthur Chunqi Li if (!(pt[offset] & (EPT_PRESENT))) 4586884af61SArthur Chunqi Li return -1; 45900b5c590SPeter Feiner pt = (unsigned long *)(pt[offset] & EPT_ADDR_MASK); 4606884af61SArthur Chunqi Li } 461a969e087SPeter Feiner offset = (guest_addr >> EPT_LEVEL_SHIFT(l)) & EPT_PGDIR_MASK; 4626884af61SArthur Chunqi Li pt[offset] = pte_val; 4636884af61SArthur Chunqi Li return 0; 4646884af61SArthur Chunqi Li } 4656884af61SArthur Chunqi Li 466b093c6ceSWanpeng Li void vpid_sync(int type, u16 vpid) 467b093c6ceSWanpeng Li { 468b093c6ceSWanpeng Li switch(type) { 469b093c6ceSWanpeng Li case INVVPID_SINGLE: 470b093c6ceSWanpeng Li if (ept_vpid.val & VPID_CAP_INVVPID_SINGLE) { 471b093c6ceSWanpeng Li invvpid(INVVPID_SINGLE, vpid, 0); 472b093c6ceSWanpeng Li break; 473b093c6ceSWanpeng Li } 474b093c6ceSWanpeng Li case INVVPID_ALL: 475b093c6ceSWanpeng Li if (ept_vpid.val & VPID_CAP_INVVPID_ALL) { 476b093c6ceSWanpeng Li invvpid(INVVPID_ALL, vpid, 0); 477b093c6ceSWanpeng Li break; 478b093c6ceSWanpeng Li } 479b093c6ceSWanpeng Li default: 480b093c6ceSWanpeng Li printf("WARNING: invvpid is not supported\n"); 481b093c6ceSWanpeng Li } 482b093c6ceSWanpeng Li } 4836884af61SArthur Chunqi Li 4849d7eaa29SArthur Chunqi Li static void init_vmcs_ctrl(void) 4859d7eaa29SArthur Chunqi Li { 4869d7eaa29SArthur Chunqi Li /* 26.2 CHECKS ON VMX CONTROLS AND HOST-STATE AREA */ 4879d7eaa29SArthur Chunqi Li /* 26.2.1.1 */ 4889d7eaa29SArthur Chunqi Li vmcs_write(PIN_CONTROLS, ctrl_pin); 4899d7eaa29SArthur Chunqi Li /* Disable VMEXIT of IO instruction */ 4909d7eaa29SArthur Chunqi Li vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu[0]); 4919d7eaa29SArthur Chunqi Li if (ctrl_cpu_rev[0].set & CPU_SECONDARY) { 4926884af61SArthur Chunqi Li ctrl_cpu[1] = (ctrl_cpu[1] | ctrl_cpu_rev[1].set) & 4936884af61SArthur Chunqi Li ctrl_cpu_rev[1].clr; 4949d7eaa29SArthur Chunqi Li vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu[1]); 4959d7eaa29SArthur Chunqi Li } 4969d7eaa29SArthur Chunqi Li vmcs_write(CR3_TARGET_COUNT, 0); 4979d7eaa29SArthur Chunqi Li vmcs_write(VPID, ++vpid_cnt); 4989d7eaa29SArthur Chunqi Li } 4999d7eaa29SArthur Chunqi Li 5009d7eaa29SArthur Chunqi Li static void init_vmcs_host(void) 5019d7eaa29SArthur Chunqi Li { 5029d7eaa29SArthur Chunqi Li /* 26.2 CHECKS ON VMX CONTROLS AND HOST-STATE AREA */ 5039d7eaa29SArthur Chunqi Li /* 26.2.1.2 */ 5049d7eaa29SArthur Chunqi Li vmcs_write(HOST_EFER, rdmsr(MSR_EFER)); 5059d7eaa29SArthur Chunqi Li 5069d7eaa29SArthur Chunqi Li /* 26.2.1.3 */ 5079d7eaa29SArthur Chunqi Li vmcs_write(ENT_CONTROLS, ctrl_enter); 5089d7eaa29SArthur Chunqi Li vmcs_write(EXI_CONTROLS, ctrl_exit); 5099d7eaa29SArthur Chunqi Li 5109d7eaa29SArthur Chunqi Li /* 26.2.2 */ 5119d7eaa29SArthur Chunqi Li vmcs_write(HOST_CR0, read_cr0()); 5129d7eaa29SArthur Chunqi Li vmcs_write(HOST_CR3, read_cr3()); 5139d7eaa29SArthur Chunqi Li vmcs_write(HOST_CR4, read_cr4()); 5149d7eaa29SArthur Chunqi Li vmcs_write(HOST_SYSENTER_EIP, (u64)(&entry_sysenter)); 51569d8fe0eSPaolo Bonzini vmcs_write(HOST_SYSENTER_CS, KERNEL_CS); 5169d7eaa29SArthur Chunqi Li 5179d7eaa29SArthur Chunqi Li /* 26.2.3 */ 51869d8fe0eSPaolo Bonzini vmcs_write(HOST_SEL_CS, KERNEL_CS); 51969d8fe0eSPaolo Bonzini vmcs_write(HOST_SEL_SS, KERNEL_DS); 52069d8fe0eSPaolo Bonzini vmcs_write(HOST_SEL_DS, KERNEL_DS); 52169d8fe0eSPaolo Bonzini vmcs_write(HOST_SEL_ES, KERNEL_DS); 52269d8fe0eSPaolo Bonzini vmcs_write(HOST_SEL_FS, KERNEL_DS); 52369d8fe0eSPaolo Bonzini vmcs_write(HOST_SEL_GS, KERNEL_DS); 52469d8fe0eSPaolo Bonzini vmcs_write(HOST_SEL_TR, TSS_MAIN); 525337166aaSJan Kiszka vmcs_write(HOST_BASE_TR, tss_descr.base); 526337166aaSJan Kiszka vmcs_write(HOST_BASE_GDTR, gdt64_desc.base); 527337166aaSJan Kiszka vmcs_write(HOST_BASE_IDTR, idt_descr.base); 5289d7eaa29SArthur Chunqi Li vmcs_write(HOST_BASE_FS, 0); 5299d7eaa29SArthur Chunqi Li vmcs_write(HOST_BASE_GS, 0); 5309d7eaa29SArthur Chunqi Li 5319d7eaa29SArthur Chunqi Li /* Set other vmcs area */ 5329d7eaa29SArthur Chunqi Li vmcs_write(PF_ERROR_MASK, 0); 5339d7eaa29SArthur Chunqi Li vmcs_write(PF_ERROR_MATCH, 0); 5349d7eaa29SArthur Chunqi Li vmcs_write(VMCS_LINK_PTR, ~0ul); 5359d7eaa29SArthur Chunqi Li vmcs_write(VMCS_LINK_PTR_HI, ~0ul); 5369d7eaa29SArthur Chunqi Li vmcs_write(HOST_RIP, (u64)(&vmx_return)); 5379d7eaa29SArthur Chunqi Li } 5389d7eaa29SArthur Chunqi Li 5399d7eaa29SArthur Chunqi Li static void init_vmcs_guest(void) 5409d7eaa29SArthur Chunqi Li { 5419d7eaa29SArthur Chunqi Li /* 26.3 CHECKING AND LOADING GUEST STATE */ 5429d7eaa29SArthur Chunqi Li ulong guest_cr0, guest_cr4, guest_cr3; 5439d7eaa29SArthur Chunqi Li /* 26.3.1.1 */ 5449d7eaa29SArthur Chunqi Li guest_cr0 = read_cr0(); 5459d7eaa29SArthur Chunqi Li guest_cr4 = read_cr4(); 5469d7eaa29SArthur Chunqi Li guest_cr3 = read_cr3(); 5479d7eaa29SArthur Chunqi Li if (ctrl_enter & ENT_GUEST_64) { 5489d7eaa29SArthur Chunqi Li guest_cr0 |= X86_CR0_PG; 5499d7eaa29SArthur Chunqi Li guest_cr4 |= X86_CR4_PAE; 5509d7eaa29SArthur Chunqi Li } 5519d7eaa29SArthur Chunqi Li if ((ctrl_enter & ENT_GUEST_64) == 0) 5529d7eaa29SArthur Chunqi Li guest_cr4 &= (~X86_CR4_PCIDE); 5539d7eaa29SArthur Chunqi Li if (guest_cr0 & X86_CR0_PG) 5549d7eaa29SArthur Chunqi Li guest_cr0 |= X86_CR0_PE; 5559d7eaa29SArthur Chunqi Li vmcs_write(GUEST_CR0, guest_cr0); 5569d7eaa29SArthur Chunqi Li vmcs_write(GUEST_CR3, guest_cr3); 5579d7eaa29SArthur Chunqi Li vmcs_write(GUEST_CR4, guest_cr4); 55869d8fe0eSPaolo Bonzini vmcs_write(GUEST_SYSENTER_CS, KERNEL_CS); 5599d7eaa29SArthur Chunqi Li vmcs_write(GUEST_SYSENTER_ESP, 5609d7eaa29SArthur Chunqi Li (u64)(guest_syscall_stack + PAGE_SIZE - 1)); 5619d7eaa29SArthur Chunqi Li vmcs_write(GUEST_SYSENTER_EIP, (u64)(&entry_sysenter)); 5629d7eaa29SArthur Chunqi Li vmcs_write(GUEST_DR7, 0); 5639d7eaa29SArthur Chunqi Li vmcs_write(GUEST_EFER, rdmsr(MSR_EFER)); 5649d7eaa29SArthur Chunqi Li 5659d7eaa29SArthur Chunqi Li /* 26.3.1.2 */ 56669d8fe0eSPaolo Bonzini vmcs_write(GUEST_SEL_CS, KERNEL_CS); 56769d8fe0eSPaolo Bonzini vmcs_write(GUEST_SEL_SS, KERNEL_DS); 56869d8fe0eSPaolo Bonzini vmcs_write(GUEST_SEL_DS, KERNEL_DS); 56969d8fe0eSPaolo Bonzini vmcs_write(GUEST_SEL_ES, KERNEL_DS); 57069d8fe0eSPaolo Bonzini vmcs_write(GUEST_SEL_FS, KERNEL_DS); 57169d8fe0eSPaolo Bonzini vmcs_write(GUEST_SEL_GS, KERNEL_DS); 57269d8fe0eSPaolo Bonzini vmcs_write(GUEST_SEL_TR, TSS_MAIN); 5739d7eaa29SArthur Chunqi Li vmcs_write(GUEST_SEL_LDTR, 0); 5749d7eaa29SArthur Chunqi Li 5759d7eaa29SArthur Chunqi Li vmcs_write(GUEST_BASE_CS, 0); 5769d7eaa29SArthur Chunqi Li vmcs_write(GUEST_BASE_ES, 0); 5779d7eaa29SArthur Chunqi Li vmcs_write(GUEST_BASE_SS, 0); 5789d7eaa29SArthur Chunqi Li vmcs_write(GUEST_BASE_DS, 0); 5799d7eaa29SArthur Chunqi Li vmcs_write(GUEST_BASE_FS, 0); 5809d7eaa29SArthur Chunqi Li vmcs_write(GUEST_BASE_GS, 0); 581337166aaSJan Kiszka vmcs_write(GUEST_BASE_TR, tss_descr.base); 5829d7eaa29SArthur Chunqi Li vmcs_write(GUEST_BASE_LDTR, 0); 5839d7eaa29SArthur Chunqi Li 5849d7eaa29SArthur Chunqi Li vmcs_write(GUEST_LIMIT_CS, 0xFFFFFFFF); 5859d7eaa29SArthur Chunqi Li vmcs_write(GUEST_LIMIT_DS, 0xFFFFFFFF); 5869d7eaa29SArthur Chunqi Li vmcs_write(GUEST_LIMIT_ES, 0xFFFFFFFF); 5879d7eaa29SArthur Chunqi Li vmcs_write(GUEST_LIMIT_SS, 0xFFFFFFFF); 5889d7eaa29SArthur Chunqi Li vmcs_write(GUEST_LIMIT_FS, 0xFFFFFFFF); 5899d7eaa29SArthur Chunqi Li vmcs_write(GUEST_LIMIT_GS, 0xFFFFFFFF); 5909d7eaa29SArthur Chunqi Li vmcs_write(GUEST_LIMIT_LDTR, 0xffff); 591337166aaSJan Kiszka vmcs_write(GUEST_LIMIT_TR, tss_descr.limit); 5929d7eaa29SArthur Chunqi Li 5939d7eaa29SArthur Chunqi Li vmcs_write(GUEST_AR_CS, 0xa09b); 5949d7eaa29SArthur Chunqi Li vmcs_write(GUEST_AR_DS, 0xc093); 5959d7eaa29SArthur Chunqi Li vmcs_write(GUEST_AR_ES, 0xc093); 5969d7eaa29SArthur Chunqi Li vmcs_write(GUEST_AR_FS, 0xc093); 5979d7eaa29SArthur Chunqi Li vmcs_write(GUEST_AR_GS, 0xc093); 5989d7eaa29SArthur Chunqi Li vmcs_write(GUEST_AR_SS, 0xc093); 5999d7eaa29SArthur Chunqi Li vmcs_write(GUEST_AR_LDTR, 0x82); 6009d7eaa29SArthur Chunqi Li vmcs_write(GUEST_AR_TR, 0x8b); 6019d7eaa29SArthur Chunqi Li 6029d7eaa29SArthur Chunqi Li /* 26.3.1.3 */ 603337166aaSJan Kiszka vmcs_write(GUEST_BASE_GDTR, gdt64_desc.base); 604337166aaSJan Kiszka vmcs_write(GUEST_BASE_IDTR, idt_descr.base); 605337166aaSJan Kiszka vmcs_write(GUEST_LIMIT_GDTR, gdt64_desc.limit); 606337166aaSJan Kiszka vmcs_write(GUEST_LIMIT_IDTR, idt_descr.limit); 6079d7eaa29SArthur Chunqi Li 6089d7eaa29SArthur Chunqi Li /* 26.3.1.4 */ 6099d7eaa29SArthur Chunqi Li vmcs_write(GUEST_RIP, (u64)(&guest_entry)); 6109d7eaa29SArthur Chunqi Li vmcs_write(GUEST_RSP, (u64)(guest_stack + PAGE_SIZE - 1)); 6119d7eaa29SArthur Chunqi Li vmcs_write(GUEST_RFLAGS, 0x2); 6129d7eaa29SArthur Chunqi Li 6139d7eaa29SArthur Chunqi Li /* 26.3.1.5 */ 61417ba0dd0SJan Kiszka vmcs_write(GUEST_ACTV_STATE, ACTV_ACTIVE); 6159d7eaa29SArthur Chunqi Li vmcs_write(GUEST_INTR_STATE, 0); 6169d7eaa29SArthur Chunqi Li } 6179d7eaa29SArthur Chunqi Li 6189d7eaa29SArthur Chunqi Li static int init_vmcs(struct vmcs **vmcs) 6199d7eaa29SArthur Chunqi Li { 6209d7eaa29SArthur Chunqi Li *vmcs = alloc_page(); 6219d7eaa29SArthur Chunqi Li memset(*vmcs, 0, PAGE_SIZE); 6229d7eaa29SArthur Chunqi Li (*vmcs)->revision_id = basic.revision; 6239d7eaa29SArthur Chunqi Li /* vmclear first to init vmcs */ 6249d7eaa29SArthur Chunqi Li if (vmcs_clear(*vmcs)) { 6259d7eaa29SArthur Chunqi Li printf("%s : vmcs_clear error\n", __func__); 6269d7eaa29SArthur Chunqi Li return 1; 6279d7eaa29SArthur Chunqi Li } 6289d7eaa29SArthur Chunqi Li 6299d7eaa29SArthur Chunqi Li if (make_vmcs_current(*vmcs)) { 6309d7eaa29SArthur Chunqi Li printf("%s : make_vmcs_current error\n", __func__); 6319d7eaa29SArthur Chunqi Li return 1; 6329d7eaa29SArthur Chunqi Li } 6339d7eaa29SArthur Chunqi Li 6349d7eaa29SArthur Chunqi Li /* All settings to pin/exit/enter/cpu 6359d7eaa29SArthur Chunqi Li control fields should be placed here */ 6369d7eaa29SArthur Chunqi Li ctrl_pin |= PIN_EXTINT | PIN_NMI | PIN_VIRT_NMI; 6379d7eaa29SArthur Chunqi Li ctrl_exit = EXI_LOAD_EFER | EXI_HOST_64; 6389d7eaa29SArthur Chunqi Li ctrl_enter = (ENT_LOAD_EFER | ENT_GUEST_64); 6399d7eaa29SArthur Chunqi Li /* DIsable IO instruction VMEXIT now */ 6409d7eaa29SArthur Chunqi Li ctrl_cpu[0] &= (~(CPU_IO | CPU_IO_BITMAP)); 6419d7eaa29SArthur Chunqi Li ctrl_cpu[1] = 0; 6429d7eaa29SArthur Chunqi Li 6439d7eaa29SArthur Chunqi Li ctrl_pin = (ctrl_pin | ctrl_pin_rev.set) & ctrl_pin_rev.clr; 6449d7eaa29SArthur Chunqi Li ctrl_enter = (ctrl_enter | ctrl_enter_rev.set) & ctrl_enter_rev.clr; 6459d7eaa29SArthur Chunqi Li ctrl_exit = (ctrl_exit | ctrl_exit_rev.set) & ctrl_exit_rev.clr; 6469d7eaa29SArthur Chunqi Li ctrl_cpu[0] = (ctrl_cpu[0] | ctrl_cpu_rev[0].set) & ctrl_cpu_rev[0].clr; 6479d7eaa29SArthur Chunqi Li 6489d7eaa29SArthur Chunqi Li init_vmcs_ctrl(); 6499d7eaa29SArthur Chunqi Li init_vmcs_host(); 6509d7eaa29SArthur Chunqi Li init_vmcs_guest(); 6519d7eaa29SArthur Chunqi Li return 0; 6529d7eaa29SArthur Chunqi Li } 6539d7eaa29SArthur Chunqi Li 6549d7eaa29SArthur Chunqi Li static void init_vmx(void) 6559d7eaa29SArthur Chunqi Li { 6563ee34093SArthur Chunqi Li ulong fix_cr0_set, fix_cr0_clr; 6573ee34093SArthur Chunqi Li ulong fix_cr4_set, fix_cr4_clr; 6583ee34093SArthur Chunqi Li 6599d7eaa29SArthur Chunqi Li vmxon_region = alloc_page(); 6609d7eaa29SArthur Chunqi Li memset(vmxon_region, 0, PAGE_SIZE); 6619d7eaa29SArthur Chunqi Li 6629d7eaa29SArthur Chunqi Li fix_cr0_set = rdmsr(MSR_IA32_VMX_CR0_FIXED0); 6639d7eaa29SArthur Chunqi Li fix_cr0_clr = rdmsr(MSR_IA32_VMX_CR0_FIXED1); 6649d7eaa29SArthur Chunqi Li fix_cr4_set = rdmsr(MSR_IA32_VMX_CR4_FIXED0); 6659d7eaa29SArthur Chunqi Li fix_cr4_clr = rdmsr(MSR_IA32_VMX_CR4_FIXED1); 6669d7eaa29SArthur Chunqi Li basic.val = rdmsr(MSR_IA32_VMX_BASIC); 6679d7eaa29SArthur Chunqi Li ctrl_pin_rev.val = rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_PIN 6689d7eaa29SArthur Chunqi Li : MSR_IA32_VMX_PINBASED_CTLS); 6699d7eaa29SArthur Chunqi Li ctrl_exit_rev.val = rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_EXIT 6709d7eaa29SArthur Chunqi Li : MSR_IA32_VMX_EXIT_CTLS); 6719d7eaa29SArthur Chunqi Li ctrl_enter_rev.val = rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_ENTRY 6729d7eaa29SArthur Chunqi Li : MSR_IA32_VMX_ENTRY_CTLS); 6739d7eaa29SArthur Chunqi Li ctrl_cpu_rev[0].val = rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_PROC 6749d7eaa29SArthur Chunqi Li : MSR_IA32_VMX_PROCBASED_CTLS); 6756884af61SArthur Chunqi Li if ((ctrl_cpu_rev[0].clr & CPU_SECONDARY) != 0) 6769d7eaa29SArthur Chunqi Li ctrl_cpu_rev[1].val = rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2); 6776884af61SArthur Chunqi Li else 6786884af61SArthur Chunqi Li ctrl_cpu_rev[1].val = 0; 6796884af61SArthur Chunqi Li if ((ctrl_cpu_rev[1].clr & (CPU_EPT | CPU_VPID)) != 0) 6809d7eaa29SArthur Chunqi Li ept_vpid.val = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP); 6816884af61SArthur Chunqi Li else 6826884af61SArthur Chunqi Li ept_vpid.val = 0; 6839d7eaa29SArthur Chunqi Li 6849d7eaa29SArthur Chunqi Li write_cr0((read_cr0() & fix_cr0_clr) | fix_cr0_set); 6859d7eaa29SArthur Chunqi Li write_cr4((read_cr4() & fix_cr4_clr) | fix_cr4_set | X86_CR4_VMXE); 6869d7eaa29SArthur Chunqi Li 6879d7eaa29SArthur Chunqi Li *vmxon_region = basic.revision; 6889d7eaa29SArthur Chunqi Li 6899d7eaa29SArthur Chunqi Li guest_stack = alloc_page(); 6909d7eaa29SArthur Chunqi Li memset(guest_stack, 0, PAGE_SIZE); 6919d7eaa29SArthur Chunqi Li guest_syscall_stack = alloc_page(); 6929d7eaa29SArthur Chunqi Li memset(guest_syscall_stack, 0, PAGE_SIZE); 6939d7eaa29SArthur Chunqi Li } 6949d7eaa29SArthur Chunqi Li 695e3f363c4SJan Kiszka static void do_vmxon_off(void *data) 6969d7eaa29SArthur Chunqi Li { 6973b127446SJan Kiszka vmx_on(); 6983b127446SJan Kiszka vmx_off(); 69903f37ef2SPaolo Bonzini } 7003b127446SJan Kiszka 701e3f363c4SJan Kiszka static void do_write_feature_control(void *data) 7023b127446SJan Kiszka { 7033b127446SJan Kiszka wrmsr(MSR_IA32_FEATURE_CONTROL, 0); 70403f37ef2SPaolo Bonzini } 7053b127446SJan Kiszka 7063b127446SJan Kiszka static int test_vmx_feature_control(void) 7073b127446SJan Kiszka { 7083b127446SJan Kiszka u64 ia32_feature_control; 7093b127446SJan Kiszka bool vmx_enabled; 7103b127446SJan Kiszka 7113b127446SJan Kiszka ia32_feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL); 7123b127446SJan Kiszka vmx_enabled = ((ia32_feature_control & 0x5) == 0x5); 7133b127446SJan Kiszka if ((ia32_feature_control & 0x5) == 0x5) { 7143b127446SJan Kiszka printf("VMX enabled and locked by BIOS\n"); 7153b127446SJan Kiszka return 0; 7163b127446SJan Kiszka } else if (ia32_feature_control & 0x1) { 7173b127446SJan Kiszka printf("ERROR: VMX locked out by BIOS!?\n"); 7183b127446SJan Kiszka return 1; 7193b127446SJan Kiszka } 7203b127446SJan Kiszka 7213b127446SJan Kiszka wrmsr(MSR_IA32_FEATURE_CONTROL, 0); 7223b127446SJan Kiszka report("test vmxon with FEATURE_CONTROL cleared", 723e3f363c4SJan Kiszka test_for_exception(GP_VECTOR, &do_vmxon_off, NULL)); 7243b127446SJan Kiszka 7253b127446SJan Kiszka wrmsr(MSR_IA32_FEATURE_CONTROL, 0x4); 7263b127446SJan Kiszka report("test vmxon without FEATURE_CONTROL lock", 727e3f363c4SJan Kiszka test_for_exception(GP_VECTOR, &do_vmxon_off, NULL)); 7283b127446SJan Kiszka 7293b127446SJan Kiszka wrmsr(MSR_IA32_FEATURE_CONTROL, 0x5); 7303b127446SJan Kiszka vmx_enabled = ((rdmsr(MSR_IA32_FEATURE_CONTROL) & 0x5) == 0x5); 7313b127446SJan Kiszka report("test enable VMX in FEATURE_CONTROL", vmx_enabled); 7323b127446SJan Kiszka 7333b127446SJan Kiszka report("test FEATURE_CONTROL lock bit", 734e3f363c4SJan Kiszka test_for_exception(GP_VECTOR, &do_write_feature_control, NULL)); 7353b127446SJan Kiszka 7363b127446SJan Kiszka return !vmx_enabled; 7379d7eaa29SArthur Chunqi Li } 7389d7eaa29SArthur Chunqi Li 7399d7eaa29SArthur Chunqi Li static int test_vmxon(void) 7409d7eaa29SArthur Chunqi Li { 741ce21d809SBandan Das int ret, ret1; 742ce21d809SBandan Das u64 *tmp_region = vmxon_region; 743e2cf1c9dSEduardo Habkost int width = cpuid_maxphyaddr(); 7449d7eaa29SArthur Chunqi Li 745ce21d809SBandan Das /* Unaligned page access */ 746ce21d809SBandan Das vmxon_region = (u64 *)((intptr_t)vmxon_region + 1); 747ce21d809SBandan Das ret1 = vmx_on(); 748ce21d809SBandan Das report("test vmxon with unaligned vmxon region", ret1); 749ce21d809SBandan Das if (!ret1) { 750ce21d809SBandan Das ret = 1; 751ce21d809SBandan Das goto out; 752ce21d809SBandan Das } 753ce21d809SBandan Das 754ce21d809SBandan Das /* gpa bits beyond physical address width are set*/ 755ce21d809SBandan Das vmxon_region = (u64 *)((intptr_t)tmp_region | ((u64)1 << (width+1))); 756ce21d809SBandan Das ret1 = vmx_on(); 757ce21d809SBandan Das report("test vmxon with bits set beyond physical address width", ret1); 758ce21d809SBandan Das if (!ret1) { 759ce21d809SBandan Das ret = 1; 760ce21d809SBandan Das goto out; 761ce21d809SBandan Das } 762ce21d809SBandan Das 763ce21d809SBandan Das /* invalid revision indentifier */ 764ce21d809SBandan Das vmxon_region = tmp_region; 765ce21d809SBandan Das *vmxon_region = 0xba9da9; 766ce21d809SBandan Das ret1 = vmx_on(); 767ce21d809SBandan Das report("test vmxon with invalid revision identifier", ret1); 768ce21d809SBandan Das if (!ret1) { 769ce21d809SBandan Das ret = 1; 770ce21d809SBandan Das goto out; 771ce21d809SBandan Das } 772ce21d809SBandan Das 773ce21d809SBandan Das /* and finally a valid region */ 774ce21d809SBandan Das *vmxon_region = basic.revision; 7759d7eaa29SArthur Chunqi Li ret = vmx_on(); 776ce21d809SBandan Das report("test vmxon with valid vmxon region", !ret); 777ce21d809SBandan Das 778ce21d809SBandan Das out: 7799d7eaa29SArthur Chunqi Li return ret; 7809d7eaa29SArthur Chunqi Li } 7819d7eaa29SArthur Chunqi Li 7829d7eaa29SArthur Chunqi Li static void test_vmptrld(void) 7839d7eaa29SArthur Chunqi Li { 784daeec979SBandan Das struct vmcs *vmcs, *tmp_root; 785e2cf1c9dSEduardo Habkost int width = cpuid_maxphyaddr(); 7869d7eaa29SArthur Chunqi Li 7879d7eaa29SArthur Chunqi Li vmcs = alloc_page(); 7889d7eaa29SArthur Chunqi Li vmcs->revision_id = basic.revision; 789daeec979SBandan Das 790daeec979SBandan Das /* Unaligned page access */ 791daeec979SBandan Das tmp_root = (struct vmcs *)((intptr_t)vmcs + 1); 792daeec979SBandan Das report("test vmptrld with unaligned vmcs", 7939c305952SPaolo Bonzini make_vmcs_current(tmp_root) == 1); 794daeec979SBandan Das 795daeec979SBandan Das /* gpa bits beyond physical address width are set*/ 796daeec979SBandan Das tmp_root = (struct vmcs *)((intptr_t)vmcs | 797daeec979SBandan Das ((u64)1 << (width+1))); 798daeec979SBandan Das report("test vmptrld with vmcs address bits set beyond physical address width", 7999c305952SPaolo Bonzini make_vmcs_current(tmp_root) == 1); 800daeec979SBandan Das 801daeec979SBandan Das /* Pass VMXON region */ 802daeec979SBandan Das tmp_root = (struct vmcs *)vmxon_region; 803daeec979SBandan Das report("test vmptrld with vmxon region", 8049c305952SPaolo Bonzini make_vmcs_current(tmp_root) == 1); 805daeec979SBandan Das 806daeec979SBandan Das report("test vmptrld with valid vmcs region", make_vmcs_current(vmcs) == 0); 8079d7eaa29SArthur Chunqi Li } 8089d7eaa29SArthur Chunqi Li 8099d7eaa29SArthur Chunqi Li static void test_vmptrst(void) 8109d7eaa29SArthur Chunqi Li { 8119d7eaa29SArthur Chunqi Li int ret; 8129d7eaa29SArthur Chunqi Li struct vmcs *vmcs1, *vmcs2; 8139d7eaa29SArthur Chunqi Li 8149d7eaa29SArthur Chunqi Li vmcs1 = alloc_page(); 8159d7eaa29SArthur Chunqi Li memset(vmcs1, 0, PAGE_SIZE); 8169d7eaa29SArthur Chunqi Li init_vmcs(&vmcs1); 8179d7eaa29SArthur Chunqi Li ret = vmcs_save(&vmcs2); 8189d7eaa29SArthur Chunqi Li report("test vmptrst", (!ret) && (vmcs1 == vmcs2)); 8199d7eaa29SArthur Chunqi Li } 8209d7eaa29SArthur Chunqi Li 82169c8d31cSJan Kiszka struct vmx_ctl_msr { 82269c8d31cSJan Kiszka const char *name; 82369c8d31cSJan Kiszka u32 index, true_index; 82469c8d31cSJan Kiszka u32 default1; 82569c8d31cSJan Kiszka } vmx_ctl_msr[] = { 82669c8d31cSJan Kiszka { "MSR_IA32_VMX_PINBASED_CTLS", MSR_IA32_VMX_PINBASED_CTLS, 82769c8d31cSJan Kiszka MSR_IA32_VMX_TRUE_PIN, 0x16 }, 82869c8d31cSJan Kiszka { "MSR_IA32_VMX_PROCBASED_CTLS", MSR_IA32_VMX_PROCBASED_CTLS, 82969c8d31cSJan Kiszka MSR_IA32_VMX_TRUE_PROC, 0x401e172 }, 83069c8d31cSJan Kiszka { "MSR_IA32_VMX_PROCBASED_CTLS2", MSR_IA32_VMX_PROCBASED_CTLS2, 83169c8d31cSJan Kiszka MSR_IA32_VMX_PROCBASED_CTLS2, 0 }, 83269c8d31cSJan Kiszka { "MSR_IA32_VMX_EXIT_CTLS", MSR_IA32_VMX_EXIT_CTLS, 83369c8d31cSJan Kiszka MSR_IA32_VMX_TRUE_EXIT, 0x36dff }, 83469c8d31cSJan Kiszka { "MSR_IA32_VMX_ENTRY_CTLS", MSR_IA32_VMX_ENTRY_CTLS, 83569c8d31cSJan Kiszka MSR_IA32_VMX_TRUE_ENTRY, 0x11ff }, 83669c8d31cSJan Kiszka }; 83769c8d31cSJan Kiszka 83869c8d31cSJan Kiszka static void test_vmx_caps(void) 83969c8d31cSJan Kiszka { 84069c8d31cSJan Kiszka u64 val, default1, fixed0, fixed1; 84169c8d31cSJan Kiszka union vmx_ctrl_msr ctrl, true_ctrl; 84269c8d31cSJan Kiszka unsigned int n; 84369c8d31cSJan Kiszka bool ok; 84469c8d31cSJan Kiszka 84569c8d31cSJan Kiszka printf("\nTest suite: VMX capability reporting\n"); 84669c8d31cSJan Kiszka 84769c8d31cSJan Kiszka report("MSR_IA32_VMX_BASIC", 84869c8d31cSJan Kiszka (basic.revision & (1ul << 31)) == 0 && 84969c8d31cSJan Kiszka basic.size > 0 && basic.size <= 4096 && 85069c8d31cSJan Kiszka (basic.type == 0 || basic.type == 6) && 85169c8d31cSJan Kiszka basic.reserved1 == 0 && basic.reserved2 == 0); 85269c8d31cSJan Kiszka 85369c8d31cSJan Kiszka val = rdmsr(MSR_IA32_VMX_MISC); 85469c8d31cSJan Kiszka report("MSR_IA32_VMX_MISC", 85569c8d31cSJan Kiszka (!(ctrl_cpu_rev[1].clr & CPU_URG) || val & (1ul << 5)) && 85669c8d31cSJan Kiszka ((val >> 16) & 0x1ff) <= 256 && 85769c8d31cSJan Kiszka (val & 0xc0007e00) == 0); 85869c8d31cSJan Kiszka 85969c8d31cSJan Kiszka for (n = 0; n < ARRAY_SIZE(vmx_ctl_msr); n++) { 86069c8d31cSJan Kiszka ctrl.val = rdmsr(vmx_ctl_msr[n].index); 86169c8d31cSJan Kiszka default1 = vmx_ctl_msr[n].default1; 86269c8d31cSJan Kiszka ok = (ctrl.set & default1) == default1; 86369c8d31cSJan Kiszka ok = ok && (ctrl.set & ~ctrl.clr) == 0; 86469c8d31cSJan Kiszka if (ok && basic.ctrl) { 86569c8d31cSJan Kiszka true_ctrl.val = rdmsr(vmx_ctl_msr[n].true_index); 86669c8d31cSJan Kiszka ok = ctrl.clr == true_ctrl.clr; 86769c8d31cSJan Kiszka ok = ok && ctrl.set == (true_ctrl.set | default1); 86869c8d31cSJan Kiszka } 86969c8d31cSJan Kiszka report(vmx_ctl_msr[n].name, ok); 87069c8d31cSJan Kiszka } 87169c8d31cSJan Kiszka 87269c8d31cSJan Kiszka fixed0 = rdmsr(MSR_IA32_VMX_CR0_FIXED0); 87369c8d31cSJan Kiszka fixed1 = rdmsr(MSR_IA32_VMX_CR0_FIXED1); 87469c8d31cSJan Kiszka report("MSR_IA32_VMX_IA32_VMX_CR0_FIXED0/1", 87569c8d31cSJan Kiszka ((fixed0 ^ fixed1) & ~fixed1) == 0); 87669c8d31cSJan Kiszka 87769c8d31cSJan Kiszka fixed0 = rdmsr(MSR_IA32_VMX_CR4_FIXED0); 87869c8d31cSJan Kiszka fixed1 = rdmsr(MSR_IA32_VMX_CR4_FIXED1); 87969c8d31cSJan Kiszka report("MSR_IA32_VMX_IA32_VMX_CR4_FIXED0/1", 88069c8d31cSJan Kiszka ((fixed0 ^ fixed1) & ~fixed1) == 0); 88169c8d31cSJan Kiszka 88269c8d31cSJan Kiszka val = rdmsr(MSR_IA32_VMX_VMCS_ENUM); 88369c8d31cSJan Kiszka report("MSR_IA32_VMX_VMCS_ENUM", 88469c8d31cSJan Kiszka (val & 0x3e) >= 0x2a && 88569c8d31cSJan Kiszka (val & 0xfffffffffffffc01Ull) == 0); 88669c8d31cSJan Kiszka 88769c8d31cSJan Kiszka val = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP); 88869c8d31cSJan Kiszka report("MSR_IA32_VMX_EPT_VPID_CAP", 88969c8d31cSJan Kiszka (val & 0xfffff07ef9eebebeUll) == 0); 89069c8d31cSJan Kiszka } 89169c8d31cSJan Kiszka 8929d7eaa29SArthur Chunqi Li /* This function can only be called in guest */ 8939d7eaa29SArthur Chunqi Li static void __attribute__((__used__)) hypercall(u32 hypercall_no) 8949d7eaa29SArthur Chunqi Li { 8959d7eaa29SArthur Chunqi Li u64 val = 0; 8969d7eaa29SArthur Chunqi Li val = (hypercall_no & HYPERCALL_MASK) | HYPERCALL_BIT; 8979d7eaa29SArthur Chunqi Li hypercall_field = val; 8989d7eaa29SArthur Chunqi Li asm volatile("vmcall\n\t"); 8999d7eaa29SArthur Chunqi Li } 9009d7eaa29SArthur Chunqi Li 9019d7eaa29SArthur Chunqi Li static bool is_hypercall() 9029d7eaa29SArthur Chunqi Li { 9039d7eaa29SArthur Chunqi Li ulong reason, hyper_bit; 9049d7eaa29SArthur Chunqi Li 9059d7eaa29SArthur Chunqi Li reason = vmcs_read(EXI_REASON) & 0xff; 9069d7eaa29SArthur Chunqi Li hyper_bit = hypercall_field & HYPERCALL_BIT; 9079d7eaa29SArthur Chunqi Li if (reason == VMX_VMCALL && hyper_bit) 9089d7eaa29SArthur Chunqi Li return true; 9099d7eaa29SArthur Chunqi Li return false; 9109d7eaa29SArthur Chunqi Li } 9119d7eaa29SArthur Chunqi Li 9129d7eaa29SArthur Chunqi Li static int handle_hypercall() 9139d7eaa29SArthur Chunqi Li { 9149d7eaa29SArthur Chunqi Li ulong hypercall_no; 9159d7eaa29SArthur Chunqi Li 9169d7eaa29SArthur Chunqi Li hypercall_no = hypercall_field & HYPERCALL_MASK; 9179d7eaa29SArthur Chunqi Li hypercall_field = 0; 9189d7eaa29SArthur Chunqi Li switch (hypercall_no) { 9199d7eaa29SArthur Chunqi Li case HYPERCALL_VMEXIT: 9209d7eaa29SArthur Chunqi Li return VMX_TEST_VMEXIT; 9219d7eaa29SArthur Chunqi Li default: 922b006d7ebSAndrew Jones printf("ERROR : Invalid hypercall number : %ld\n", hypercall_no); 9239d7eaa29SArthur Chunqi Li } 9249d7eaa29SArthur Chunqi Li return VMX_TEST_EXIT; 9259d7eaa29SArthur Chunqi Li } 9269d7eaa29SArthur Chunqi Li 9279d7eaa29SArthur Chunqi Li static int exit_handler() 9289d7eaa29SArthur Chunqi Li { 9299d7eaa29SArthur Chunqi Li int ret; 9309d7eaa29SArthur Chunqi Li 9319d7eaa29SArthur Chunqi Li current->exits++; 9321d9284d0SArthur Chunqi Li regs.rflags = vmcs_read(GUEST_RFLAGS); 9339d7eaa29SArthur Chunqi Li if (is_hypercall()) 9349d7eaa29SArthur Chunqi Li ret = handle_hypercall(); 9359d7eaa29SArthur Chunqi Li else 9369d7eaa29SArthur Chunqi Li ret = current->exit_handler(); 9371d9284d0SArthur Chunqi Li vmcs_write(GUEST_RFLAGS, regs.rflags); 9383b50efe3SPeter Feiner 9399d7eaa29SArthur Chunqi Li return ret; 9409d7eaa29SArthur Chunqi Li } 9413b50efe3SPeter Feiner 9423b50efe3SPeter Feiner /* 9433b50efe3SPeter Feiner * Called if vmlaunch or vmresume fails. 9443b50efe3SPeter Feiner * @early - failure due to "VMX controls and host-state area" (26.2) 9453b50efe3SPeter Feiner * @vmlaunch - was this a vmlaunch or vmresume 9463b50efe3SPeter Feiner * @rflags - host rflags 9473b50efe3SPeter Feiner */ 9483b50efe3SPeter Feiner static int 9493b50efe3SPeter Feiner entry_failure_handler(struct vmentry_failure *failure) 9503b50efe3SPeter Feiner { 9513b50efe3SPeter Feiner if (current->entry_failure_handler) 9523b50efe3SPeter Feiner return current->entry_failure_handler(failure); 9533b50efe3SPeter Feiner else 9543b50efe3SPeter Feiner return VMX_TEST_EXIT; 9559d7eaa29SArthur Chunqi Li } 9569d7eaa29SArthur Chunqi Li 9579d7eaa29SArthur Chunqi Li static int vmx_run() 9589d7eaa29SArthur Chunqi Li { 959897d8365SPeter Feiner unsigned long host_rflags; 9609d7eaa29SArthur Chunqi Li 9619d7eaa29SArthur Chunqi Li while (1) { 9623b50efe3SPeter Feiner u32 ret; 9633b50efe3SPeter Feiner u32 fail = 0; 9643b50efe3SPeter Feiner bool entered; 9653b50efe3SPeter Feiner struct vmentry_failure failure; 9664e809db5SPeter Feiner 9679d7eaa29SArthur Chunqi Li asm volatile ( 968897d8365SPeter Feiner "mov %[HOST_RSP], %%rdi\n\t" 969897d8365SPeter Feiner "vmwrite %%rsp, %%rdi\n\t" 9709d7eaa29SArthur Chunqi Li LOAD_GPR_C 971*44417388SPaolo Bonzini "cmpb $0, %[launched]\n\t" 9729d7eaa29SArthur Chunqi Li "jne 1f\n\t" 9739d7eaa29SArthur Chunqi Li "vmlaunch\n\t" 9749d7eaa29SArthur Chunqi Li "jmp 2f\n\t" 9759d7eaa29SArthur Chunqi Li "1: " 9769d7eaa29SArthur Chunqi Li "vmresume\n\t" 9779d7eaa29SArthur Chunqi Li "2: " 978f37cf4e2SPeter Feiner SAVE_GPR_C 979897d8365SPeter Feiner "pushf\n\t" 980897d8365SPeter Feiner "pop %%rdi\n\t" 981897d8365SPeter Feiner "mov %%rdi, %[host_rflags]\n\t" 982897d8365SPeter Feiner "movl $1, %[fail]\n\t" 983f37cf4e2SPeter Feiner "jmp 3f\n\t" 9849d7eaa29SArthur Chunqi Li "vmx_return:\n\t" 9859d7eaa29SArthur Chunqi Li SAVE_GPR_C 986f37cf4e2SPeter Feiner "3: \n\t" 987897d8365SPeter Feiner : [fail]"+m"(fail), [host_rflags]"=m"(host_rflags) 988897d8365SPeter Feiner : [launched]"m"(launched), [HOST_RSP]"i"(HOST_RSP) 989897d8365SPeter Feiner : "rdi", "memory", "cc" 9909d7eaa29SArthur Chunqi Li 9919d7eaa29SArthur Chunqi Li ); 9923b50efe3SPeter Feiner 9933b50efe3SPeter Feiner entered = !fail && !(vmcs_read(EXI_REASON) & VMX_ENTRY_FAILURE); 9943b50efe3SPeter Feiner 9953b50efe3SPeter Feiner if (entered) { 9963b50efe3SPeter Feiner /* 9973b50efe3SPeter Feiner * VMCS isn't in "launched" state if there's been any 9983b50efe3SPeter Feiner * entry failure (early or otherwise). 9993b50efe3SPeter Feiner */ 10009d7eaa29SArthur Chunqi Li launched = 1; 10019d7eaa29SArthur Chunqi Li ret = exit_handler(); 10023b50efe3SPeter Feiner } else { 10033b50efe3SPeter Feiner failure.flags = host_rflags; 10043b50efe3SPeter Feiner failure.vmlaunch = !launched; 10053b50efe3SPeter Feiner failure.instr = launched ? "vmresume" : "vmlaunch"; 10063b50efe3SPeter Feiner failure.early = fail; 10073b50efe3SPeter Feiner ret = entry_failure_handler(&failure); 10089d7eaa29SArthur Chunqi Li } 10093b50efe3SPeter Feiner 10109d7eaa29SArthur Chunqi Li switch (ret) { 10113b50efe3SPeter Feiner case VMX_TEST_RESUME: 10123b50efe3SPeter Feiner continue; 10139d7eaa29SArthur Chunqi Li case VMX_TEST_VMEXIT: 10149d7eaa29SArthur Chunqi Li return 0; 10153b50efe3SPeter Feiner case VMX_TEST_EXIT: 10169d7eaa29SArthur Chunqi Li break; 10179d7eaa29SArthur Chunqi Li default: 10183b50efe3SPeter Feiner printf("ERROR : Invalid %s_handler return val %d.\n", 10193b50efe3SPeter Feiner entered ? "exit" : "entry_failure", 10203b50efe3SPeter Feiner ret); 10219d7eaa29SArthur Chunqi Li break; 10229d7eaa29SArthur Chunqi Li } 10233b50efe3SPeter Feiner 10243b50efe3SPeter Feiner if (entered) 10253b50efe3SPeter Feiner print_vmexit_info(); 10263b50efe3SPeter Feiner else 10273b50efe3SPeter Feiner print_vmentry_failure_info(&failure); 10283b50efe3SPeter Feiner abort(); 10293b50efe3SPeter Feiner } 10309d7eaa29SArthur Chunqi Li } 10319d7eaa29SArthur Chunqi Li 10329d7eaa29SArthur Chunqi Li static int test_run(struct vmx_test *test) 10339d7eaa29SArthur Chunqi Li { 10349d7eaa29SArthur Chunqi Li if (test->name == NULL) 10359d7eaa29SArthur Chunqi Li test->name = "(no name)"; 10369d7eaa29SArthur Chunqi Li if (vmx_on()) { 10379d7eaa29SArthur Chunqi Li printf("%s : vmxon failed.\n", __func__); 10389d7eaa29SArthur Chunqi Li return 1; 10399d7eaa29SArthur Chunqi Li } 10409d7eaa29SArthur Chunqi Li init_vmcs(&(test->vmcs)); 10419d7eaa29SArthur Chunqi Li /* Directly call test->init is ok here, init_vmcs has done 10429d7eaa29SArthur Chunqi Li vmcs init, vmclear and vmptrld*/ 1043c592c151SJan Kiszka if (test->init && test->init(test->vmcs) != VMX_TEST_START) 1044a0e30e71SPaolo Bonzini goto out; 10459d7eaa29SArthur Chunqi Li test->exits = 0; 10469d7eaa29SArthur Chunqi Li current = test; 10479d7eaa29SArthur Chunqi Li regs = test->guest_regs; 10489d7eaa29SArthur Chunqi Li vmcs_write(GUEST_RFLAGS, regs.rflags | 0x2); 10499d7eaa29SArthur Chunqi Li launched = 0; 10509d7eaa29SArthur Chunqi Li printf("\nTest suite: %s\n", test->name); 10519d7eaa29SArthur Chunqi Li vmx_run(); 1052a0e30e71SPaolo Bonzini out: 10539d7eaa29SArthur Chunqi Li if (vmx_off()) { 10549d7eaa29SArthur Chunqi Li printf("%s : vmxoff failed.\n", __func__); 10559d7eaa29SArthur Chunqi Li return 1; 10569d7eaa29SArthur Chunqi Li } 10579d7eaa29SArthur Chunqi Li return 0; 10589d7eaa29SArthur Chunqi Li } 10599d7eaa29SArthur Chunqi Li 10603ee34093SArthur Chunqi Li extern struct vmx_test vmx_tests[]; 10619d7eaa29SArthur Chunqi Li 10629d7eaa29SArthur Chunqi Li int main(void) 10639d7eaa29SArthur Chunqi Li { 10643ee34093SArthur Chunqi Li int i = 0; 10659d7eaa29SArthur Chunqi Li 10669d7eaa29SArthur Chunqi Li setup_vm(); 10679d7eaa29SArthur Chunqi Li setup_idt(); 10683ee34093SArthur Chunqi Li hypercall_field = 0; 10699d7eaa29SArthur Chunqi Li 10703b127446SJan Kiszka if (!(cpuid(1).c & (1 << 5))) { 10713b127446SJan Kiszka printf("WARNING: vmx not supported, add '-cpu host'\n"); 10729d7eaa29SArthur Chunqi Li goto exit; 10739d7eaa29SArthur Chunqi Li } 10749d7eaa29SArthur Chunqi Li init_vmx(); 10753b127446SJan Kiszka if (test_vmx_feature_control() != 0) 10763b127446SJan Kiszka goto exit; 10779d7eaa29SArthur Chunqi Li /* Set basic test ctxt the same as "null" */ 10789d7eaa29SArthur Chunqi Li current = &vmx_tests[0]; 10799d7eaa29SArthur Chunqi Li if (test_vmxon() != 0) 10809d7eaa29SArthur Chunqi Li goto exit; 10819d7eaa29SArthur Chunqi Li test_vmptrld(); 10829d7eaa29SArthur Chunqi Li test_vmclear(); 10839d7eaa29SArthur Chunqi Li test_vmptrst(); 10849d7eaa29SArthur Chunqi Li init_vmcs(&vmcs_root); 10859d7eaa29SArthur Chunqi Li if (vmx_run()) { 10869d7eaa29SArthur Chunqi Li report("test vmlaunch", 0); 10879d7eaa29SArthur Chunqi Li goto exit; 10889d7eaa29SArthur Chunqi Li } 10899d7eaa29SArthur Chunqi Li test_vmxoff(); 109069c8d31cSJan Kiszka test_vmx_caps(); 10919d7eaa29SArthur Chunqi Li 10923ee34093SArthur Chunqi Li while (vmx_tests[++i].name != NULL) 10939d7eaa29SArthur Chunqi Li if (test_run(&vmx_tests[i])) 10949d7eaa29SArthur Chunqi Li goto exit; 10959d7eaa29SArthur Chunqi Li 10969d7eaa29SArthur Chunqi Li exit: 1097f3cdd159SJan Kiszka return report_summary(); 10989d7eaa29SArthur Chunqi Li } 1099