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 #include "io.h" 399d7eaa29SArthur Chunqi Li 409d7eaa29SArthur Chunqi Li u32 *vmxon_region; 419d7eaa29SArthur Chunqi Li struct vmcs *vmcs_root; 429d7eaa29SArthur Chunqi Li u32 vpid_cnt; 439d7eaa29SArthur Chunqi Li void *guest_stack, *guest_syscall_stack; 449d7eaa29SArthur Chunqi Li u32 ctrl_pin, ctrl_enter, ctrl_exit, ctrl_cpu[2]; 459d7eaa29SArthur Chunqi Li struct regs regs; 469d7eaa29SArthur Chunqi Li struct vmx_test *current; 473ee34093SArthur Chunqi Li u64 hypercall_field; 489d7eaa29SArthur Chunqi Li bool launched; 491d9284d0SArthur Chunqi Li u64 host_rflags; 509d7eaa29SArthur Chunqi Li 513ee34093SArthur Chunqi Li union vmx_basic basic; 523ee34093SArthur Chunqi Li union vmx_ctrl_pin ctrl_pin_rev; 533ee34093SArthur Chunqi Li union vmx_ctrl_cpu ctrl_cpu_rev[2]; 543ee34093SArthur Chunqi Li union vmx_ctrl_exit ctrl_exit_rev; 553ee34093SArthur Chunqi Li union vmx_ctrl_ent ctrl_enter_rev; 563ee34093SArthur Chunqi Li union vmx_ept_vpid ept_vpid; 573ee34093SArthur Chunqi Li 58337166aaSJan Kiszka extern struct descriptor_table_ptr gdt64_desc; 59337166aaSJan Kiszka extern struct descriptor_table_ptr idt_descr; 60337166aaSJan Kiszka extern struct descriptor_table_ptr tss_descr; 619d7eaa29SArthur Chunqi Li extern void *vmx_return; 629d7eaa29SArthur Chunqi Li extern void *entry_sysenter; 639d7eaa29SArthur Chunqi Li extern void *guest_entry; 649d7eaa29SArthur Chunqi Li 65*ffb1a9e0SJan Kiszka static volatile u32 stage; 66*ffb1a9e0SJan Kiszka 67*ffb1a9e0SJan Kiszka void vmx_set_test_stage(u32 s) 68*ffb1a9e0SJan Kiszka { 69*ffb1a9e0SJan Kiszka barrier(); 70*ffb1a9e0SJan Kiszka stage = s; 71*ffb1a9e0SJan Kiszka barrier(); 72*ffb1a9e0SJan Kiszka } 73*ffb1a9e0SJan Kiszka 74*ffb1a9e0SJan Kiszka u32 vmx_get_test_stage(void) 75*ffb1a9e0SJan Kiszka { 76*ffb1a9e0SJan Kiszka u32 s; 77*ffb1a9e0SJan Kiszka 78*ffb1a9e0SJan Kiszka barrier(); 79*ffb1a9e0SJan Kiszka s = stage; 80*ffb1a9e0SJan Kiszka barrier(); 81*ffb1a9e0SJan Kiszka return s; 82*ffb1a9e0SJan Kiszka } 83*ffb1a9e0SJan Kiszka 84*ffb1a9e0SJan Kiszka void vmx_inc_test_stage(void) 85*ffb1a9e0SJan Kiszka { 86*ffb1a9e0SJan Kiszka barrier(); 87*ffb1a9e0SJan Kiszka stage++; 88*ffb1a9e0SJan Kiszka barrier(); 89*ffb1a9e0SJan Kiszka } 90*ffb1a9e0SJan Kiszka 919d7eaa29SArthur Chunqi Li static int make_vmcs_current(struct vmcs *vmcs) 929d7eaa29SArthur Chunqi Li { 939d7eaa29SArthur Chunqi Li bool ret; 949d7eaa29SArthur Chunqi Li 959d7eaa29SArthur Chunqi Li asm volatile ("vmptrld %1; setbe %0" : "=q" (ret) : "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; 1219d7eaa29SArthur Chunqi Li asm volatile ("vmxon %1; setbe %0\n\t" 1229d7eaa29SArthur Chunqi Li : "=q"(ret) : "m"(vmxon_region) : "cc"); 1239d7eaa29SArthur Chunqi Li return ret; 1249d7eaa29SArthur Chunqi Li } 1259d7eaa29SArthur Chunqi Li 1269d7eaa29SArthur Chunqi Li static inline int vmx_off() 1279d7eaa29SArthur Chunqi Li { 1289d7eaa29SArthur Chunqi Li bool ret; 1299d7eaa29SArthur Chunqi Li asm volatile("vmxoff; setbe %0\n\t" 1309d7eaa29SArthur Chunqi Li : "=q"(ret) : : "cc"); 1319d7eaa29SArthur Chunqi Li return ret; 1329d7eaa29SArthur Chunqi Li } 1339d7eaa29SArthur Chunqi Li 1343ee34093SArthur Chunqi Li void print_vmexit_info() 1359d7eaa29SArthur Chunqi Li { 1369d7eaa29SArthur Chunqi Li u64 guest_rip, guest_rsp; 1379d7eaa29SArthur Chunqi Li ulong reason = vmcs_read(EXI_REASON) & 0xff; 1389d7eaa29SArthur Chunqi Li ulong exit_qual = vmcs_read(EXI_QUALIFICATION); 1399d7eaa29SArthur Chunqi Li guest_rip = vmcs_read(GUEST_RIP); 1409d7eaa29SArthur Chunqi Li guest_rsp = vmcs_read(GUEST_RSP); 1419d7eaa29SArthur Chunqi Li printf("VMEXIT info:\n"); 1429d7eaa29SArthur Chunqi Li printf("\tvmexit reason = %d\n", reason); 1439d7eaa29SArthur Chunqi Li printf("\texit qualification = 0x%x\n", exit_qual); 1449d7eaa29SArthur Chunqi Li printf("\tBit 31 of reason = %x\n", (vmcs_read(EXI_REASON) >> 31) & 1); 1459d7eaa29SArthur Chunqi Li printf("\tguest_rip = 0x%llx\n", guest_rip); 1469d7eaa29SArthur Chunqi Li printf("\tRAX=0x%llx RBX=0x%llx RCX=0x%llx RDX=0x%llx\n", 1479d7eaa29SArthur Chunqi Li regs.rax, regs.rbx, regs.rcx, regs.rdx); 1489d7eaa29SArthur Chunqi Li printf("\tRSP=0x%llx RBP=0x%llx RSI=0x%llx RDI=0x%llx\n", 1499d7eaa29SArthur Chunqi Li guest_rsp, regs.rbp, regs.rsi, regs.rdi); 1509d7eaa29SArthur Chunqi Li printf("\tR8 =0x%llx R9 =0x%llx R10=0x%llx R11=0x%llx\n", 1519d7eaa29SArthur Chunqi Li regs.r8, regs.r9, regs.r10, regs.r11); 1529d7eaa29SArthur Chunqi Li printf("\tR12=0x%llx R13=0x%llx R14=0x%llx R15=0x%llx\n", 1539d7eaa29SArthur Chunqi Li regs.r12, regs.r13, regs.r14, regs.r15); 1549d7eaa29SArthur Chunqi Li } 1559d7eaa29SArthur Chunqi Li 1569d7eaa29SArthur Chunqi Li static void test_vmclear(void) 1579d7eaa29SArthur Chunqi Li { 1589d7eaa29SArthur Chunqi Li u64 rflags; 1599d7eaa29SArthur Chunqi Li 1609d7eaa29SArthur Chunqi Li rflags = read_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF; 1619d7eaa29SArthur Chunqi Li write_rflags(rflags); 1629d7eaa29SArthur Chunqi Li report("test vmclear", vmcs_clear(vmcs_root) == 0); 1639d7eaa29SArthur Chunqi Li } 1649d7eaa29SArthur Chunqi Li 1659d7eaa29SArthur Chunqi Li static void test_vmxoff(void) 1669d7eaa29SArthur Chunqi Li { 1679d7eaa29SArthur Chunqi Li int ret; 1689d7eaa29SArthur Chunqi Li u64 rflags; 1699d7eaa29SArthur Chunqi Li 1709d7eaa29SArthur Chunqi Li rflags = read_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF; 1719d7eaa29SArthur Chunqi Li write_rflags(rflags); 1729d7eaa29SArthur Chunqi Li ret = vmx_off(); 1739d7eaa29SArthur Chunqi Li report("test vmxoff", !ret); 1749d7eaa29SArthur Chunqi Li } 1759d7eaa29SArthur Chunqi Li 1769d7eaa29SArthur Chunqi Li static void __attribute__((__used__)) guest_main(void) 1779d7eaa29SArthur Chunqi Li { 1789d7eaa29SArthur Chunqi Li current->guest_main(); 1799d7eaa29SArthur Chunqi Li } 1809d7eaa29SArthur Chunqi Li 1819d7eaa29SArthur Chunqi Li /* guest_entry */ 1829d7eaa29SArthur Chunqi Li asm( 1839d7eaa29SArthur Chunqi Li ".align 4, 0x90\n\t" 1849d7eaa29SArthur Chunqi Li ".globl entry_guest\n\t" 1859d7eaa29SArthur Chunqi Li "guest_entry:\n\t" 1869d7eaa29SArthur Chunqi Li " call guest_main\n\t" 1879d7eaa29SArthur Chunqi Li " mov $1, %edi\n\t" 1889d7eaa29SArthur Chunqi Li " call hypercall\n\t" 1899d7eaa29SArthur Chunqi Li ); 1909d7eaa29SArthur Chunqi Li 1916884af61SArthur Chunqi Li /* EPT paging structure related functions */ 1926884af61SArthur Chunqi Li /* install_ept_entry : Install a page to a given level in EPT 1936884af61SArthur Chunqi Li @pml4 : addr of pml4 table 1946884af61SArthur Chunqi Li @pte_level : level of PTE to set 1956884af61SArthur Chunqi Li @guest_addr : physical address of guest 1966884af61SArthur Chunqi Li @pte : pte value to set 1976884af61SArthur Chunqi Li @pt_page : address of page table, NULL for a new page 1986884af61SArthur Chunqi Li */ 1996884af61SArthur Chunqi Li void install_ept_entry(unsigned long *pml4, 2006884af61SArthur Chunqi Li int pte_level, 2016884af61SArthur Chunqi Li unsigned long guest_addr, 2026884af61SArthur Chunqi Li unsigned long pte, 2036884af61SArthur Chunqi Li unsigned long *pt_page) 2046884af61SArthur Chunqi Li { 2056884af61SArthur Chunqi Li int level; 2066884af61SArthur Chunqi Li unsigned long *pt = pml4; 2076884af61SArthur Chunqi Li unsigned offset; 2086884af61SArthur Chunqi Li 2096884af61SArthur Chunqi Li for (level = EPT_PAGE_LEVEL; level > pte_level; --level) { 2106884af61SArthur Chunqi Li offset = (guest_addr >> ((level-1) * EPT_PGDIR_WIDTH + 12)) 2116884af61SArthur Chunqi Li & EPT_PGDIR_MASK; 2126884af61SArthur Chunqi Li if (!(pt[offset] & (EPT_PRESENT))) { 2136884af61SArthur Chunqi Li unsigned long *new_pt = pt_page; 2146884af61SArthur Chunqi Li if (!new_pt) 2156884af61SArthur Chunqi Li new_pt = alloc_page(); 2166884af61SArthur Chunqi Li else 2176884af61SArthur Chunqi Li pt_page = 0; 2186884af61SArthur Chunqi Li memset(new_pt, 0, PAGE_SIZE); 2196884af61SArthur Chunqi Li pt[offset] = virt_to_phys(new_pt) 2206884af61SArthur Chunqi Li | EPT_RA | EPT_WA | EPT_EA; 22104b0e0f3SJan Kiszka } else 22204b0e0f3SJan Kiszka pt[offset] &= ~EPT_LARGE_PAGE; 2236884af61SArthur Chunqi Li pt = phys_to_virt(pt[offset] & 0xffffffffff000ull); 2246884af61SArthur Chunqi Li } 2256884af61SArthur Chunqi Li offset = ((unsigned long)guest_addr >> ((level-1) * 2266884af61SArthur Chunqi Li EPT_PGDIR_WIDTH + 12)) & EPT_PGDIR_MASK; 2276884af61SArthur Chunqi Li pt[offset] = pte; 2286884af61SArthur Chunqi Li } 2296884af61SArthur Chunqi Li 2306884af61SArthur Chunqi Li /* Map a page, @perm is the permission of the page */ 2316884af61SArthur Chunqi Li void install_ept(unsigned long *pml4, 2326884af61SArthur Chunqi Li unsigned long phys, 2336884af61SArthur Chunqi Li unsigned long guest_addr, 2346884af61SArthur Chunqi Li u64 perm) 2356884af61SArthur Chunqi Li { 2366884af61SArthur Chunqi Li install_ept_entry(pml4, 1, guest_addr, (phys & PAGE_MASK) | perm, 0); 2376884af61SArthur Chunqi Li } 2386884af61SArthur Chunqi Li 2396884af61SArthur Chunqi Li /* Map a 1G-size page */ 2406884af61SArthur Chunqi Li void install_1g_ept(unsigned long *pml4, 2416884af61SArthur Chunqi Li unsigned long phys, 2426884af61SArthur Chunqi Li unsigned long guest_addr, 2436884af61SArthur Chunqi Li u64 perm) 2446884af61SArthur Chunqi Li { 2456884af61SArthur Chunqi Li install_ept_entry(pml4, 3, guest_addr, 2466884af61SArthur Chunqi Li (phys & PAGE_MASK) | perm | EPT_LARGE_PAGE, 0); 2476884af61SArthur Chunqi Li } 2486884af61SArthur Chunqi Li 2496884af61SArthur Chunqi Li /* Map a 2M-size page */ 2506884af61SArthur Chunqi Li void install_2m_ept(unsigned long *pml4, 2516884af61SArthur Chunqi Li unsigned long phys, 2526884af61SArthur Chunqi Li unsigned long guest_addr, 2536884af61SArthur Chunqi Li u64 perm) 2546884af61SArthur Chunqi Li { 2556884af61SArthur Chunqi Li install_ept_entry(pml4, 2, guest_addr, 2566884af61SArthur Chunqi Li (phys & PAGE_MASK) | perm | EPT_LARGE_PAGE, 0); 2576884af61SArthur Chunqi Li } 2586884af61SArthur Chunqi Li 2596884af61SArthur Chunqi Li /* setup_ept_range : Setup a range of 1:1 mapped page to EPT paging structure. 2606884af61SArthur Chunqi Li @start : start address of guest page 2616884af61SArthur Chunqi Li @len : length of address to be mapped 2626884af61SArthur Chunqi Li @map_1g : whether 1G page map is used 2636884af61SArthur Chunqi Li @map_2m : whether 2M page map is used 2646884af61SArthur Chunqi Li @perm : permission for every page 2656884af61SArthur Chunqi Li */ 266b947e241SJan Kiszka void setup_ept_range(unsigned long *pml4, unsigned long start, 2676884af61SArthur Chunqi Li unsigned long len, int map_1g, int map_2m, u64 perm) 2686884af61SArthur Chunqi Li { 2696884af61SArthur Chunqi Li u64 phys = start; 2706884af61SArthur Chunqi Li u64 max = (u64)len + (u64)start; 2716884af61SArthur Chunqi Li 2726884af61SArthur Chunqi Li if (map_1g) { 2736884af61SArthur Chunqi Li while (phys + PAGE_SIZE_1G <= max) { 2746884af61SArthur Chunqi Li install_1g_ept(pml4, phys, phys, perm); 2756884af61SArthur Chunqi Li phys += PAGE_SIZE_1G; 2766884af61SArthur Chunqi Li } 2776884af61SArthur Chunqi Li } 2786884af61SArthur Chunqi Li if (map_2m) { 2796884af61SArthur Chunqi Li while (phys + PAGE_SIZE_2M <= max) { 2806884af61SArthur Chunqi Li install_2m_ept(pml4, phys, phys, perm); 2816884af61SArthur Chunqi Li phys += PAGE_SIZE_2M; 2826884af61SArthur Chunqi Li } 2836884af61SArthur Chunqi Li } 2846884af61SArthur Chunqi Li while (phys + PAGE_SIZE <= max) { 2856884af61SArthur Chunqi Li install_ept(pml4, phys, phys, perm); 2866884af61SArthur Chunqi Li phys += PAGE_SIZE; 2876884af61SArthur Chunqi Li } 2886884af61SArthur Chunqi Li } 2896884af61SArthur Chunqi Li 2906884af61SArthur Chunqi Li /* get_ept_pte : Get the PTE of a given level in EPT, 2916884af61SArthur Chunqi Li @level == 1 means get the latest level*/ 2926884af61SArthur Chunqi Li unsigned long get_ept_pte(unsigned long *pml4, 2936884af61SArthur Chunqi Li unsigned long guest_addr, int level) 2946884af61SArthur Chunqi Li { 2956884af61SArthur Chunqi Li int l; 2966884af61SArthur Chunqi Li unsigned long *pt = pml4, pte; 2976884af61SArthur Chunqi Li unsigned offset; 2986884af61SArthur Chunqi Li 2996884af61SArthur Chunqi Li for (l = EPT_PAGE_LEVEL; l > 1; --l) { 3006884af61SArthur Chunqi Li offset = (guest_addr >> (((l-1) * EPT_PGDIR_WIDTH) + 12)) 3016884af61SArthur Chunqi Li & EPT_PGDIR_MASK; 3026884af61SArthur Chunqi Li pte = pt[offset]; 3036884af61SArthur Chunqi Li if (!(pte & (EPT_PRESENT))) 3046884af61SArthur Chunqi Li return 0; 3056884af61SArthur Chunqi Li if (l == level) 3066884af61SArthur Chunqi Li return pte; 3076884af61SArthur Chunqi Li if (l < 4 && (pte & EPT_LARGE_PAGE)) 3086884af61SArthur Chunqi Li return pte; 3096884af61SArthur Chunqi Li pt = (unsigned long *)(pte & 0xffffffffff000ull); 3106884af61SArthur Chunqi Li } 3116884af61SArthur Chunqi Li offset = (guest_addr >> (((l-1) * EPT_PGDIR_WIDTH) + 12)) 3126884af61SArthur Chunqi Li & EPT_PGDIR_MASK; 3136884af61SArthur Chunqi Li pte = pt[offset]; 3146884af61SArthur Chunqi Li return pte; 3156884af61SArthur Chunqi Li } 3166884af61SArthur Chunqi Li 3172f888fccSBandan Das void ept_sync(int type, u64 eptp) 3182f888fccSBandan Das { 3192f888fccSBandan Das switch (type) { 3202f888fccSBandan Das case INVEPT_SINGLE: 3212f888fccSBandan Das if (ept_vpid.val & EPT_CAP_INVEPT_SINGLE) { 3222f888fccSBandan Das invept(INVEPT_SINGLE, eptp); 3232f888fccSBandan Das break; 3242f888fccSBandan Das } 3252f888fccSBandan Das /* else fall through */ 3262f888fccSBandan Das case INVEPT_GLOBAL: 3272f888fccSBandan Das if (ept_vpid.val & EPT_CAP_INVEPT_ALL) { 3282f888fccSBandan Das invept(INVEPT_GLOBAL, eptp); 3292f888fccSBandan Das break; 3302f888fccSBandan Das } 3312f888fccSBandan Das /* else fall through */ 3322f888fccSBandan Das default: 3332f888fccSBandan Das printf("WARNING: invept is not supported!\n"); 3342f888fccSBandan Das } 3352f888fccSBandan Das } 3362f888fccSBandan Das 3376884af61SArthur Chunqi Li int set_ept_pte(unsigned long *pml4, unsigned long guest_addr, 3386884af61SArthur Chunqi Li int level, u64 pte_val) 3396884af61SArthur Chunqi Li { 3406884af61SArthur Chunqi Li int l; 3416884af61SArthur Chunqi Li unsigned long *pt = pml4; 3426884af61SArthur Chunqi Li unsigned offset; 3436884af61SArthur Chunqi Li 3446884af61SArthur Chunqi Li if (level < 1 || level > 3) 3456884af61SArthur Chunqi Li return -1; 3466884af61SArthur Chunqi Li for (l = EPT_PAGE_LEVEL; l > 1; --l) { 3476884af61SArthur Chunqi Li offset = (guest_addr >> (((l-1) * EPT_PGDIR_WIDTH) + 12)) 3486884af61SArthur Chunqi Li & EPT_PGDIR_MASK; 3496884af61SArthur Chunqi Li if (l == level) { 3506884af61SArthur Chunqi Li pt[offset] = pte_val; 3516884af61SArthur Chunqi Li return 0; 3526884af61SArthur Chunqi Li } 3536884af61SArthur Chunqi Li if (!(pt[offset] & (EPT_PRESENT))) 3546884af61SArthur Chunqi Li return -1; 3556884af61SArthur Chunqi Li pt = (unsigned long *)(pt[offset] & 0xffffffffff000ull); 3566884af61SArthur Chunqi Li } 3576884af61SArthur Chunqi Li offset = (guest_addr >> (((l-1) * EPT_PGDIR_WIDTH) + 12)) 3586884af61SArthur Chunqi Li & EPT_PGDIR_MASK; 3596884af61SArthur Chunqi Li pt[offset] = pte_val; 3606884af61SArthur Chunqi Li return 0; 3616884af61SArthur Chunqi Li } 3626884af61SArthur Chunqi Li 3636884af61SArthur Chunqi Li 3649d7eaa29SArthur Chunqi Li static void init_vmcs_ctrl(void) 3659d7eaa29SArthur Chunqi Li { 3669d7eaa29SArthur Chunqi Li /* 26.2 CHECKS ON VMX CONTROLS AND HOST-STATE AREA */ 3679d7eaa29SArthur Chunqi Li /* 26.2.1.1 */ 3689d7eaa29SArthur Chunqi Li vmcs_write(PIN_CONTROLS, ctrl_pin); 3699d7eaa29SArthur Chunqi Li /* Disable VMEXIT of IO instruction */ 3709d7eaa29SArthur Chunqi Li vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu[0]); 3719d7eaa29SArthur Chunqi Li if (ctrl_cpu_rev[0].set & CPU_SECONDARY) { 3726884af61SArthur Chunqi Li ctrl_cpu[1] = (ctrl_cpu[1] | ctrl_cpu_rev[1].set) & 3736884af61SArthur Chunqi Li ctrl_cpu_rev[1].clr; 3749d7eaa29SArthur Chunqi Li vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu[1]); 3759d7eaa29SArthur Chunqi Li } 3769d7eaa29SArthur Chunqi Li vmcs_write(CR3_TARGET_COUNT, 0); 3779d7eaa29SArthur Chunqi Li vmcs_write(VPID, ++vpid_cnt); 3789d7eaa29SArthur Chunqi Li } 3799d7eaa29SArthur Chunqi Li 3809d7eaa29SArthur Chunqi Li static void init_vmcs_host(void) 3819d7eaa29SArthur Chunqi Li { 3829d7eaa29SArthur Chunqi Li /* 26.2 CHECKS ON VMX CONTROLS AND HOST-STATE AREA */ 3839d7eaa29SArthur Chunqi Li /* 26.2.1.2 */ 3849d7eaa29SArthur Chunqi Li vmcs_write(HOST_EFER, rdmsr(MSR_EFER)); 3859d7eaa29SArthur Chunqi Li 3869d7eaa29SArthur Chunqi Li /* 26.2.1.3 */ 3879d7eaa29SArthur Chunqi Li vmcs_write(ENT_CONTROLS, ctrl_enter); 3889d7eaa29SArthur Chunqi Li vmcs_write(EXI_CONTROLS, ctrl_exit); 3899d7eaa29SArthur Chunqi Li 3909d7eaa29SArthur Chunqi Li /* 26.2.2 */ 3919d7eaa29SArthur Chunqi Li vmcs_write(HOST_CR0, read_cr0()); 3929d7eaa29SArthur Chunqi Li vmcs_write(HOST_CR3, read_cr3()); 3939d7eaa29SArthur Chunqi Li vmcs_write(HOST_CR4, read_cr4()); 3949d7eaa29SArthur Chunqi Li vmcs_write(HOST_SYSENTER_EIP, (u64)(&entry_sysenter)); 39569d8fe0eSPaolo Bonzini vmcs_write(HOST_SYSENTER_CS, KERNEL_CS); 3969d7eaa29SArthur Chunqi Li 3979d7eaa29SArthur Chunqi Li /* 26.2.3 */ 39869d8fe0eSPaolo Bonzini vmcs_write(HOST_SEL_CS, KERNEL_CS); 39969d8fe0eSPaolo Bonzini vmcs_write(HOST_SEL_SS, KERNEL_DS); 40069d8fe0eSPaolo Bonzini vmcs_write(HOST_SEL_DS, KERNEL_DS); 40169d8fe0eSPaolo Bonzini vmcs_write(HOST_SEL_ES, KERNEL_DS); 40269d8fe0eSPaolo Bonzini vmcs_write(HOST_SEL_FS, KERNEL_DS); 40369d8fe0eSPaolo Bonzini vmcs_write(HOST_SEL_GS, KERNEL_DS); 40469d8fe0eSPaolo Bonzini vmcs_write(HOST_SEL_TR, TSS_MAIN); 405337166aaSJan Kiszka vmcs_write(HOST_BASE_TR, tss_descr.base); 406337166aaSJan Kiszka vmcs_write(HOST_BASE_GDTR, gdt64_desc.base); 407337166aaSJan Kiszka vmcs_write(HOST_BASE_IDTR, idt_descr.base); 4089d7eaa29SArthur Chunqi Li vmcs_write(HOST_BASE_FS, 0); 4099d7eaa29SArthur Chunqi Li vmcs_write(HOST_BASE_GS, 0); 4109d7eaa29SArthur Chunqi Li 4119d7eaa29SArthur Chunqi Li /* Set other vmcs area */ 4129d7eaa29SArthur Chunqi Li vmcs_write(PF_ERROR_MASK, 0); 4139d7eaa29SArthur Chunqi Li vmcs_write(PF_ERROR_MATCH, 0); 4149d7eaa29SArthur Chunqi Li vmcs_write(VMCS_LINK_PTR, ~0ul); 4159d7eaa29SArthur Chunqi Li vmcs_write(VMCS_LINK_PTR_HI, ~0ul); 4169d7eaa29SArthur Chunqi Li vmcs_write(HOST_RIP, (u64)(&vmx_return)); 4179d7eaa29SArthur Chunqi Li } 4189d7eaa29SArthur Chunqi Li 4199d7eaa29SArthur Chunqi Li static void init_vmcs_guest(void) 4209d7eaa29SArthur Chunqi Li { 4219d7eaa29SArthur Chunqi Li /* 26.3 CHECKING AND LOADING GUEST STATE */ 4229d7eaa29SArthur Chunqi Li ulong guest_cr0, guest_cr4, guest_cr3; 4239d7eaa29SArthur Chunqi Li /* 26.3.1.1 */ 4249d7eaa29SArthur Chunqi Li guest_cr0 = read_cr0(); 4259d7eaa29SArthur Chunqi Li guest_cr4 = read_cr4(); 4269d7eaa29SArthur Chunqi Li guest_cr3 = read_cr3(); 4279d7eaa29SArthur Chunqi Li if (ctrl_enter & ENT_GUEST_64) { 4289d7eaa29SArthur Chunqi Li guest_cr0 |= X86_CR0_PG; 4299d7eaa29SArthur Chunqi Li guest_cr4 |= X86_CR4_PAE; 4309d7eaa29SArthur Chunqi Li } 4319d7eaa29SArthur Chunqi Li if ((ctrl_enter & ENT_GUEST_64) == 0) 4329d7eaa29SArthur Chunqi Li guest_cr4 &= (~X86_CR4_PCIDE); 4339d7eaa29SArthur Chunqi Li if (guest_cr0 & X86_CR0_PG) 4349d7eaa29SArthur Chunqi Li guest_cr0 |= X86_CR0_PE; 4359d7eaa29SArthur Chunqi Li vmcs_write(GUEST_CR0, guest_cr0); 4369d7eaa29SArthur Chunqi Li vmcs_write(GUEST_CR3, guest_cr3); 4379d7eaa29SArthur Chunqi Li vmcs_write(GUEST_CR4, guest_cr4); 43869d8fe0eSPaolo Bonzini vmcs_write(GUEST_SYSENTER_CS, KERNEL_CS); 4399d7eaa29SArthur Chunqi Li vmcs_write(GUEST_SYSENTER_ESP, 4409d7eaa29SArthur Chunqi Li (u64)(guest_syscall_stack + PAGE_SIZE - 1)); 4419d7eaa29SArthur Chunqi Li vmcs_write(GUEST_SYSENTER_EIP, (u64)(&entry_sysenter)); 4429d7eaa29SArthur Chunqi Li vmcs_write(GUEST_DR7, 0); 4439d7eaa29SArthur Chunqi Li vmcs_write(GUEST_EFER, rdmsr(MSR_EFER)); 4449d7eaa29SArthur Chunqi Li 4459d7eaa29SArthur Chunqi Li /* 26.3.1.2 */ 44669d8fe0eSPaolo Bonzini vmcs_write(GUEST_SEL_CS, KERNEL_CS); 44769d8fe0eSPaolo Bonzini vmcs_write(GUEST_SEL_SS, KERNEL_DS); 44869d8fe0eSPaolo Bonzini vmcs_write(GUEST_SEL_DS, KERNEL_DS); 44969d8fe0eSPaolo Bonzini vmcs_write(GUEST_SEL_ES, KERNEL_DS); 45069d8fe0eSPaolo Bonzini vmcs_write(GUEST_SEL_FS, KERNEL_DS); 45169d8fe0eSPaolo Bonzini vmcs_write(GUEST_SEL_GS, KERNEL_DS); 45269d8fe0eSPaolo Bonzini vmcs_write(GUEST_SEL_TR, TSS_MAIN); 4539d7eaa29SArthur Chunqi Li vmcs_write(GUEST_SEL_LDTR, 0); 4549d7eaa29SArthur Chunqi Li 4559d7eaa29SArthur Chunqi Li vmcs_write(GUEST_BASE_CS, 0); 4569d7eaa29SArthur Chunqi Li vmcs_write(GUEST_BASE_ES, 0); 4579d7eaa29SArthur Chunqi Li vmcs_write(GUEST_BASE_SS, 0); 4589d7eaa29SArthur Chunqi Li vmcs_write(GUEST_BASE_DS, 0); 4599d7eaa29SArthur Chunqi Li vmcs_write(GUEST_BASE_FS, 0); 4609d7eaa29SArthur Chunqi Li vmcs_write(GUEST_BASE_GS, 0); 461337166aaSJan Kiszka vmcs_write(GUEST_BASE_TR, tss_descr.base); 4629d7eaa29SArthur Chunqi Li vmcs_write(GUEST_BASE_LDTR, 0); 4639d7eaa29SArthur Chunqi Li 4649d7eaa29SArthur Chunqi Li vmcs_write(GUEST_LIMIT_CS, 0xFFFFFFFF); 4659d7eaa29SArthur Chunqi Li vmcs_write(GUEST_LIMIT_DS, 0xFFFFFFFF); 4669d7eaa29SArthur Chunqi Li vmcs_write(GUEST_LIMIT_ES, 0xFFFFFFFF); 4679d7eaa29SArthur Chunqi Li vmcs_write(GUEST_LIMIT_SS, 0xFFFFFFFF); 4689d7eaa29SArthur Chunqi Li vmcs_write(GUEST_LIMIT_FS, 0xFFFFFFFF); 4699d7eaa29SArthur Chunqi Li vmcs_write(GUEST_LIMIT_GS, 0xFFFFFFFF); 4709d7eaa29SArthur Chunqi Li vmcs_write(GUEST_LIMIT_LDTR, 0xffff); 471337166aaSJan Kiszka vmcs_write(GUEST_LIMIT_TR, tss_descr.limit); 4729d7eaa29SArthur Chunqi Li 4739d7eaa29SArthur Chunqi Li vmcs_write(GUEST_AR_CS, 0xa09b); 4749d7eaa29SArthur Chunqi Li vmcs_write(GUEST_AR_DS, 0xc093); 4759d7eaa29SArthur Chunqi Li vmcs_write(GUEST_AR_ES, 0xc093); 4769d7eaa29SArthur Chunqi Li vmcs_write(GUEST_AR_FS, 0xc093); 4779d7eaa29SArthur Chunqi Li vmcs_write(GUEST_AR_GS, 0xc093); 4789d7eaa29SArthur Chunqi Li vmcs_write(GUEST_AR_SS, 0xc093); 4799d7eaa29SArthur Chunqi Li vmcs_write(GUEST_AR_LDTR, 0x82); 4809d7eaa29SArthur Chunqi Li vmcs_write(GUEST_AR_TR, 0x8b); 4819d7eaa29SArthur Chunqi Li 4829d7eaa29SArthur Chunqi Li /* 26.3.1.3 */ 483337166aaSJan Kiszka vmcs_write(GUEST_BASE_GDTR, gdt64_desc.base); 484337166aaSJan Kiszka vmcs_write(GUEST_BASE_IDTR, idt_descr.base); 485337166aaSJan Kiszka vmcs_write(GUEST_LIMIT_GDTR, gdt64_desc.limit); 486337166aaSJan Kiszka vmcs_write(GUEST_LIMIT_IDTR, idt_descr.limit); 4879d7eaa29SArthur Chunqi Li 4889d7eaa29SArthur Chunqi Li /* 26.3.1.4 */ 4899d7eaa29SArthur Chunqi Li vmcs_write(GUEST_RIP, (u64)(&guest_entry)); 4909d7eaa29SArthur Chunqi Li vmcs_write(GUEST_RSP, (u64)(guest_stack + PAGE_SIZE - 1)); 4919d7eaa29SArthur Chunqi Li vmcs_write(GUEST_RFLAGS, 0x2); 4929d7eaa29SArthur Chunqi Li 4939d7eaa29SArthur Chunqi Li /* 26.3.1.5 */ 49417ba0dd0SJan Kiszka vmcs_write(GUEST_ACTV_STATE, ACTV_ACTIVE); 4959d7eaa29SArthur Chunqi Li vmcs_write(GUEST_INTR_STATE, 0); 4969d7eaa29SArthur Chunqi Li } 4979d7eaa29SArthur Chunqi Li 4989d7eaa29SArthur Chunqi Li static int init_vmcs(struct vmcs **vmcs) 4999d7eaa29SArthur Chunqi Li { 5009d7eaa29SArthur Chunqi Li *vmcs = alloc_page(); 5019d7eaa29SArthur Chunqi Li memset(*vmcs, 0, PAGE_SIZE); 5029d7eaa29SArthur Chunqi Li (*vmcs)->revision_id = basic.revision; 5039d7eaa29SArthur Chunqi Li /* vmclear first to init vmcs */ 5049d7eaa29SArthur Chunqi Li if (vmcs_clear(*vmcs)) { 5059d7eaa29SArthur Chunqi Li printf("%s : vmcs_clear error\n", __func__); 5069d7eaa29SArthur Chunqi Li return 1; 5079d7eaa29SArthur Chunqi Li } 5089d7eaa29SArthur Chunqi Li 5099d7eaa29SArthur Chunqi Li if (make_vmcs_current(*vmcs)) { 5109d7eaa29SArthur Chunqi Li printf("%s : make_vmcs_current error\n", __func__); 5119d7eaa29SArthur Chunqi Li return 1; 5129d7eaa29SArthur Chunqi Li } 5139d7eaa29SArthur Chunqi Li 5149d7eaa29SArthur Chunqi Li /* All settings to pin/exit/enter/cpu 5159d7eaa29SArthur Chunqi Li control fields should be placed here */ 5169d7eaa29SArthur Chunqi Li ctrl_pin |= PIN_EXTINT | PIN_NMI | PIN_VIRT_NMI; 5179d7eaa29SArthur Chunqi Li ctrl_exit = EXI_LOAD_EFER | EXI_HOST_64; 5189d7eaa29SArthur Chunqi Li ctrl_enter = (ENT_LOAD_EFER | ENT_GUEST_64); 5199d7eaa29SArthur Chunqi Li /* DIsable IO instruction VMEXIT now */ 5209d7eaa29SArthur Chunqi Li ctrl_cpu[0] &= (~(CPU_IO | CPU_IO_BITMAP)); 5219d7eaa29SArthur Chunqi Li ctrl_cpu[1] = 0; 5229d7eaa29SArthur Chunqi Li 5239d7eaa29SArthur Chunqi Li ctrl_pin = (ctrl_pin | ctrl_pin_rev.set) & ctrl_pin_rev.clr; 5249d7eaa29SArthur Chunqi Li ctrl_enter = (ctrl_enter | ctrl_enter_rev.set) & ctrl_enter_rev.clr; 5259d7eaa29SArthur Chunqi Li ctrl_exit = (ctrl_exit | ctrl_exit_rev.set) & ctrl_exit_rev.clr; 5269d7eaa29SArthur Chunqi Li ctrl_cpu[0] = (ctrl_cpu[0] | ctrl_cpu_rev[0].set) & ctrl_cpu_rev[0].clr; 5279d7eaa29SArthur Chunqi Li 5289d7eaa29SArthur Chunqi Li init_vmcs_ctrl(); 5299d7eaa29SArthur Chunqi Li init_vmcs_host(); 5309d7eaa29SArthur Chunqi Li init_vmcs_guest(); 5319d7eaa29SArthur Chunqi Li return 0; 5329d7eaa29SArthur Chunqi Li } 5339d7eaa29SArthur Chunqi Li 5349d7eaa29SArthur Chunqi Li static void init_vmx(void) 5359d7eaa29SArthur Chunqi Li { 5363ee34093SArthur Chunqi Li ulong fix_cr0_set, fix_cr0_clr; 5373ee34093SArthur Chunqi Li ulong fix_cr4_set, fix_cr4_clr; 5383ee34093SArthur Chunqi Li 5399d7eaa29SArthur Chunqi Li vmxon_region = alloc_page(); 5409d7eaa29SArthur Chunqi Li memset(vmxon_region, 0, PAGE_SIZE); 5419d7eaa29SArthur Chunqi Li 5429d7eaa29SArthur Chunqi Li fix_cr0_set = rdmsr(MSR_IA32_VMX_CR0_FIXED0); 5439d7eaa29SArthur Chunqi Li fix_cr0_clr = rdmsr(MSR_IA32_VMX_CR0_FIXED1); 5449d7eaa29SArthur Chunqi Li fix_cr4_set = rdmsr(MSR_IA32_VMX_CR4_FIXED0); 5459d7eaa29SArthur Chunqi Li fix_cr4_clr = rdmsr(MSR_IA32_VMX_CR4_FIXED1); 5469d7eaa29SArthur Chunqi Li basic.val = rdmsr(MSR_IA32_VMX_BASIC); 5479d7eaa29SArthur Chunqi Li ctrl_pin_rev.val = rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_PIN 5489d7eaa29SArthur Chunqi Li : MSR_IA32_VMX_PINBASED_CTLS); 5499d7eaa29SArthur Chunqi Li ctrl_exit_rev.val = rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_EXIT 5509d7eaa29SArthur Chunqi Li : MSR_IA32_VMX_EXIT_CTLS); 5519d7eaa29SArthur Chunqi Li ctrl_enter_rev.val = rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_ENTRY 5529d7eaa29SArthur Chunqi Li : MSR_IA32_VMX_ENTRY_CTLS); 5539d7eaa29SArthur Chunqi Li ctrl_cpu_rev[0].val = rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_PROC 5549d7eaa29SArthur Chunqi Li : MSR_IA32_VMX_PROCBASED_CTLS); 5556884af61SArthur Chunqi Li if ((ctrl_cpu_rev[0].clr & CPU_SECONDARY) != 0) 5569d7eaa29SArthur Chunqi Li ctrl_cpu_rev[1].val = rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2); 5576884af61SArthur Chunqi Li else 5586884af61SArthur Chunqi Li ctrl_cpu_rev[1].val = 0; 5596884af61SArthur Chunqi Li if ((ctrl_cpu_rev[1].clr & (CPU_EPT | CPU_VPID)) != 0) 5609d7eaa29SArthur Chunqi Li ept_vpid.val = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP); 5616884af61SArthur Chunqi Li else 5626884af61SArthur Chunqi Li ept_vpid.val = 0; 5639d7eaa29SArthur Chunqi Li 5649d7eaa29SArthur Chunqi Li write_cr0((read_cr0() & fix_cr0_clr) | fix_cr0_set); 5659d7eaa29SArthur Chunqi Li write_cr4((read_cr4() & fix_cr4_clr) | fix_cr4_set | X86_CR4_VMXE); 5669d7eaa29SArthur Chunqi Li 5679d7eaa29SArthur Chunqi Li *vmxon_region = basic.revision; 5689d7eaa29SArthur Chunqi Li 5699d7eaa29SArthur Chunqi Li guest_stack = alloc_page(); 5709d7eaa29SArthur Chunqi Li memset(guest_stack, 0, PAGE_SIZE); 5719d7eaa29SArthur Chunqi Li guest_syscall_stack = alloc_page(); 5729d7eaa29SArthur Chunqi Li memset(guest_syscall_stack, 0, PAGE_SIZE); 5739d7eaa29SArthur Chunqi Li } 5749d7eaa29SArthur Chunqi Li 575e3f363c4SJan Kiszka static void do_vmxon_off(void *data) 5769d7eaa29SArthur Chunqi Li { 577e3f363c4SJan Kiszka set_exception_return(&&resume); 5783b127446SJan Kiszka vmx_on(); 5793b127446SJan Kiszka vmx_off(); 5803b127446SJan Kiszka resume: 5818f225b12SJan Kiszka barrier(); 5823b127446SJan Kiszka } 5833b127446SJan Kiszka 584e3f363c4SJan Kiszka static void do_write_feature_control(void *data) 5853b127446SJan Kiszka { 586e3f363c4SJan Kiszka set_exception_return(&&resume); 5873b127446SJan Kiszka wrmsr(MSR_IA32_FEATURE_CONTROL, 0); 5883b127446SJan Kiszka resume: 5898f225b12SJan Kiszka barrier(); 5903b127446SJan Kiszka } 5913b127446SJan Kiszka 5923b127446SJan Kiszka static int test_vmx_feature_control(void) 5933b127446SJan Kiszka { 5943b127446SJan Kiszka u64 ia32_feature_control; 5953b127446SJan Kiszka bool vmx_enabled; 5963b127446SJan Kiszka 5973b127446SJan Kiszka ia32_feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL); 5983b127446SJan Kiszka vmx_enabled = ((ia32_feature_control & 0x5) == 0x5); 5993b127446SJan Kiszka if ((ia32_feature_control & 0x5) == 0x5) { 6003b127446SJan Kiszka printf("VMX enabled and locked by BIOS\n"); 6013b127446SJan Kiszka return 0; 6023b127446SJan Kiszka } else if (ia32_feature_control & 0x1) { 6033b127446SJan Kiszka printf("ERROR: VMX locked out by BIOS!?\n"); 6043b127446SJan Kiszka return 1; 6053b127446SJan Kiszka } 6063b127446SJan Kiszka 6073b127446SJan Kiszka wrmsr(MSR_IA32_FEATURE_CONTROL, 0); 6083b127446SJan Kiszka report("test vmxon with FEATURE_CONTROL cleared", 609e3f363c4SJan Kiszka test_for_exception(GP_VECTOR, &do_vmxon_off, NULL)); 6103b127446SJan Kiszka 6113b127446SJan Kiszka wrmsr(MSR_IA32_FEATURE_CONTROL, 0x4); 6123b127446SJan Kiszka report("test vmxon without FEATURE_CONTROL lock", 613e3f363c4SJan Kiszka test_for_exception(GP_VECTOR, &do_vmxon_off, NULL)); 6143b127446SJan Kiszka 6153b127446SJan Kiszka wrmsr(MSR_IA32_FEATURE_CONTROL, 0x5); 6163b127446SJan Kiszka vmx_enabled = ((rdmsr(MSR_IA32_FEATURE_CONTROL) & 0x5) == 0x5); 6173b127446SJan Kiszka report("test enable VMX in FEATURE_CONTROL", vmx_enabled); 6183b127446SJan Kiszka 6193b127446SJan Kiszka report("test FEATURE_CONTROL lock bit", 620e3f363c4SJan Kiszka test_for_exception(GP_VECTOR, &do_write_feature_control, NULL)); 6213b127446SJan Kiszka 6223b127446SJan Kiszka return !vmx_enabled; 6239d7eaa29SArthur Chunqi Li } 6249d7eaa29SArthur Chunqi Li 6259d7eaa29SArthur Chunqi Li static int test_vmxon(void) 6269d7eaa29SArthur Chunqi Li { 6279d7eaa29SArthur Chunqi Li int ret; 6289d7eaa29SArthur Chunqi Li u64 rflags; 6299d7eaa29SArthur Chunqi Li 6309d7eaa29SArthur Chunqi Li rflags = read_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF; 6319d7eaa29SArthur Chunqi Li write_rflags(rflags); 6329d7eaa29SArthur Chunqi Li ret = vmx_on(); 6339d7eaa29SArthur Chunqi Li report("test vmxon", !ret); 6349d7eaa29SArthur Chunqi Li return ret; 6359d7eaa29SArthur Chunqi Li } 6369d7eaa29SArthur Chunqi Li 6379d7eaa29SArthur Chunqi Li static void test_vmptrld(void) 6389d7eaa29SArthur Chunqi Li { 6399d7eaa29SArthur Chunqi Li u64 rflags; 6409d7eaa29SArthur Chunqi Li struct vmcs *vmcs; 6419d7eaa29SArthur Chunqi Li 6429d7eaa29SArthur Chunqi Li vmcs = alloc_page(); 6439d7eaa29SArthur Chunqi Li vmcs->revision_id = basic.revision; 6449d7eaa29SArthur Chunqi Li rflags = read_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF; 6459d7eaa29SArthur Chunqi Li write_rflags(rflags); 6469d7eaa29SArthur Chunqi Li report("test vmptrld", make_vmcs_current(vmcs) == 0); 6479d7eaa29SArthur Chunqi Li } 6489d7eaa29SArthur Chunqi Li 6499d7eaa29SArthur Chunqi Li static void test_vmptrst(void) 6509d7eaa29SArthur Chunqi Li { 6519d7eaa29SArthur Chunqi Li u64 rflags; 6529d7eaa29SArthur Chunqi Li int ret; 6539d7eaa29SArthur Chunqi Li struct vmcs *vmcs1, *vmcs2; 6549d7eaa29SArthur Chunqi Li 6559d7eaa29SArthur Chunqi Li vmcs1 = alloc_page(); 6569d7eaa29SArthur Chunqi Li memset(vmcs1, 0, PAGE_SIZE); 6579d7eaa29SArthur Chunqi Li init_vmcs(&vmcs1); 6589d7eaa29SArthur Chunqi Li rflags = read_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF; 6599d7eaa29SArthur Chunqi Li write_rflags(rflags); 6609d7eaa29SArthur Chunqi Li ret = vmcs_save(&vmcs2); 6619d7eaa29SArthur Chunqi Li report("test vmptrst", (!ret) && (vmcs1 == vmcs2)); 6629d7eaa29SArthur Chunqi Li } 6639d7eaa29SArthur Chunqi Li 6649d7eaa29SArthur Chunqi Li /* This function can only be called in guest */ 6659d7eaa29SArthur Chunqi Li static void __attribute__((__used__)) hypercall(u32 hypercall_no) 6669d7eaa29SArthur Chunqi Li { 6679d7eaa29SArthur Chunqi Li u64 val = 0; 6689d7eaa29SArthur Chunqi Li val = (hypercall_no & HYPERCALL_MASK) | HYPERCALL_BIT; 6699d7eaa29SArthur Chunqi Li hypercall_field = val; 6709d7eaa29SArthur Chunqi Li asm volatile("vmcall\n\t"); 6719d7eaa29SArthur Chunqi Li } 6729d7eaa29SArthur Chunqi Li 6739d7eaa29SArthur Chunqi Li static bool is_hypercall() 6749d7eaa29SArthur Chunqi Li { 6759d7eaa29SArthur Chunqi Li ulong reason, hyper_bit; 6769d7eaa29SArthur Chunqi Li 6779d7eaa29SArthur Chunqi Li reason = vmcs_read(EXI_REASON) & 0xff; 6789d7eaa29SArthur Chunqi Li hyper_bit = hypercall_field & HYPERCALL_BIT; 6799d7eaa29SArthur Chunqi Li if (reason == VMX_VMCALL && hyper_bit) 6809d7eaa29SArthur Chunqi Li return true; 6819d7eaa29SArthur Chunqi Li return false; 6829d7eaa29SArthur Chunqi Li } 6839d7eaa29SArthur Chunqi Li 6849d7eaa29SArthur Chunqi Li static int handle_hypercall() 6859d7eaa29SArthur Chunqi Li { 6869d7eaa29SArthur Chunqi Li ulong hypercall_no; 6879d7eaa29SArthur Chunqi Li 6889d7eaa29SArthur Chunqi Li hypercall_no = hypercall_field & HYPERCALL_MASK; 6899d7eaa29SArthur Chunqi Li hypercall_field = 0; 6909d7eaa29SArthur Chunqi Li switch (hypercall_no) { 6919d7eaa29SArthur Chunqi Li case HYPERCALL_VMEXIT: 6929d7eaa29SArthur Chunqi Li return VMX_TEST_VMEXIT; 6939d7eaa29SArthur Chunqi Li default: 6949d7eaa29SArthur Chunqi Li printf("ERROR : Invalid hypercall number : %d\n", hypercall_no); 6959d7eaa29SArthur Chunqi Li } 6969d7eaa29SArthur Chunqi Li return VMX_TEST_EXIT; 6979d7eaa29SArthur Chunqi Li } 6989d7eaa29SArthur Chunqi Li 6999d7eaa29SArthur Chunqi Li static int exit_handler() 7009d7eaa29SArthur Chunqi Li { 7019d7eaa29SArthur Chunqi Li int ret; 7029d7eaa29SArthur Chunqi Li 7039d7eaa29SArthur Chunqi Li current->exits++; 7041d9284d0SArthur Chunqi Li regs.rflags = vmcs_read(GUEST_RFLAGS); 7059d7eaa29SArthur Chunqi Li if (is_hypercall()) 7069d7eaa29SArthur Chunqi Li ret = handle_hypercall(); 7079d7eaa29SArthur Chunqi Li else 7089d7eaa29SArthur Chunqi Li ret = current->exit_handler(); 7091d9284d0SArthur Chunqi Li vmcs_write(GUEST_RFLAGS, regs.rflags); 7109d7eaa29SArthur Chunqi Li switch (ret) { 7119d7eaa29SArthur Chunqi Li case VMX_TEST_VMEXIT: 7129d7eaa29SArthur Chunqi Li case VMX_TEST_RESUME: 7139d7eaa29SArthur Chunqi Li return ret; 7149d7eaa29SArthur Chunqi Li case VMX_TEST_EXIT: 7159d7eaa29SArthur Chunqi Li break; 7169d7eaa29SArthur Chunqi Li default: 7179d7eaa29SArthur Chunqi Li printf("ERROR : Invalid exit_handler return val %d.\n" 7189d7eaa29SArthur Chunqi Li , ret); 7199d7eaa29SArthur Chunqi Li } 7209d7eaa29SArthur Chunqi Li print_vmexit_info(); 7219d7eaa29SArthur Chunqi Li exit(-1); 7229d7eaa29SArthur Chunqi Li return 0; 7239d7eaa29SArthur Chunqi Li } 7249d7eaa29SArthur Chunqi Li 7259d7eaa29SArthur Chunqi Li static int vmx_run() 7269d7eaa29SArthur Chunqi Li { 7279d7eaa29SArthur Chunqi Li u32 ret = 0, fail = 0; 7289d7eaa29SArthur Chunqi Li 7299d7eaa29SArthur Chunqi Li while (1) { 7309d7eaa29SArthur Chunqi Li asm volatile ( 7319d7eaa29SArthur Chunqi Li "mov %%rsp, %%rsi\n\t" 7329d7eaa29SArthur Chunqi Li "mov %2, %%rdi\n\t" 7339d7eaa29SArthur Chunqi Li "vmwrite %%rsi, %%rdi\n\t" 7349d7eaa29SArthur Chunqi Li 7359d7eaa29SArthur Chunqi Li LOAD_GPR_C 7369d7eaa29SArthur Chunqi Li "cmpl $0, %1\n\t" 7379d7eaa29SArthur Chunqi Li "jne 1f\n\t" 7389d7eaa29SArthur Chunqi Li LOAD_RFLAGS 7399d7eaa29SArthur Chunqi Li "vmlaunch\n\t" 7409d7eaa29SArthur Chunqi Li "jmp 2f\n\t" 7419d7eaa29SArthur Chunqi Li "1: " 7429d7eaa29SArthur Chunqi Li "vmresume\n\t" 7439d7eaa29SArthur Chunqi Li "2: " 7449d7eaa29SArthur Chunqi Li "setbe %0\n\t" 7459d7eaa29SArthur Chunqi Li "vmx_return:\n\t" 7469d7eaa29SArthur Chunqi Li SAVE_GPR_C 7479d7eaa29SArthur Chunqi Li SAVE_RFLAGS 7489d7eaa29SArthur Chunqi Li : "=m"(fail) 7499d7eaa29SArthur Chunqi Li : "m"(launched), "i"(HOST_RSP) 7509d7eaa29SArthur Chunqi Li : "rdi", "rsi", "memory", "cc" 7519d7eaa29SArthur Chunqi Li 7529d7eaa29SArthur Chunqi Li ); 7539d7eaa29SArthur Chunqi Li if (fail) 7549d7eaa29SArthur Chunqi Li ret = launched ? VMX_TEST_RESUME_ERR : 7559d7eaa29SArthur Chunqi Li VMX_TEST_LAUNCH_ERR; 7569d7eaa29SArthur Chunqi Li else { 7579d7eaa29SArthur Chunqi Li launched = 1; 7589d7eaa29SArthur Chunqi Li ret = exit_handler(); 7599d7eaa29SArthur Chunqi Li } 7609d7eaa29SArthur Chunqi Li if (ret != VMX_TEST_RESUME) 7619d7eaa29SArthur Chunqi Li break; 7629d7eaa29SArthur Chunqi Li } 7639d7eaa29SArthur Chunqi Li launched = 0; 7649d7eaa29SArthur Chunqi Li switch (ret) { 7659d7eaa29SArthur Chunqi Li case VMX_TEST_VMEXIT: 7669d7eaa29SArthur Chunqi Li return 0; 7679d7eaa29SArthur Chunqi Li case VMX_TEST_LAUNCH_ERR: 7689d7eaa29SArthur Chunqi Li printf("%s : vmlaunch failed.\n", __func__); 7691d9284d0SArthur Chunqi Li if ((!(host_rflags & X86_EFLAGS_CF) && !(host_rflags & X86_EFLAGS_ZF)) 7701d9284d0SArthur Chunqi Li || ((host_rflags & X86_EFLAGS_CF) && (host_rflags & X86_EFLAGS_ZF))) 7719d7eaa29SArthur Chunqi Li printf("\tvmlaunch set wrong flags\n"); 7729d7eaa29SArthur Chunqi Li report("test vmlaunch", 0); 7739d7eaa29SArthur Chunqi Li break; 7749d7eaa29SArthur Chunqi Li case VMX_TEST_RESUME_ERR: 7759d7eaa29SArthur Chunqi Li printf("%s : vmresume failed.\n", __func__); 7761d9284d0SArthur Chunqi Li if ((!(host_rflags & X86_EFLAGS_CF) && !(host_rflags & X86_EFLAGS_ZF)) 7771d9284d0SArthur Chunqi Li || ((host_rflags & X86_EFLAGS_CF) && (host_rflags & X86_EFLAGS_ZF))) 7789d7eaa29SArthur Chunqi Li printf("\tvmresume set wrong flags\n"); 7799d7eaa29SArthur Chunqi Li report("test vmresume", 0); 7809d7eaa29SArthur Chunqi Li break; 7819d7eaa29SArthur Chunqi Li default: 7829d7eaa29SArthur Chunqi Li printf("%s : unhandled ret from exit_handler, ret=%d.\n", __func__, ret); 7839d7eaa29SArthur Chunqi Li break; 7849d7eaa29SArthur Chunqi Li } 7859d7eaa29SArthur Chunqi Li return 1; 7869d7eaa29SArthur Chunqi Li } 7879d7eaa29SArthur Chunqi Li 7889d7eaa29SArthur Chunqi Li static int test_run(struct vmx_test *test) 7899d7eaa29SArthur Chunqi Li { 7909d7eaa29SArthur Chunqi Li if (test->name == NULL) 7919d7eaa29SArthur Chunqi Li test->name = "(no name)"; 7929d7eaa29SArthur Chunqi Li if (vmx_on()) { 7939d7eaa29SArthur Chunqi Li printf("%s : vmxon failed.\n", __func__); 7949d7eaa29SArthur Chunqi Li return 1; 7959d7eaa29SArthur Chunqi Li } 7969d7eaa29SArthur Chunqi Li init_vmcs(&(test->vmcs)); 7979d7eaa29SArthur Chunqi Li /* Directly call test->init is ok here, init_vmcs has done 7989d7eaa29SArthur Chunqi Li vmcs init, vmclear and vmptrld*/ 799c592c151SJan Kiszka if (test->init && test->init(test->vmcs) != VMX_TEST_START) 800c592c151SJan Kiszka return 0; 8019d7eaa29SArthur Chunqi Li test->exits = 0; 8029d7eaa29SArthur Chunqi Li current = test; 8039d7eaa29SArthur Chunqi Li regs = test->guest_regs; 8049d7eaa29SArthur Chunqi Li vmcs_write(GUEST_RFLAGS, regs.rflags | 0x2); 8059d7eaa29SArthur Chunqi Li launched = 0; 8069d7eaa29SArthur Chunqi Li printf("\nTest suite : %s\n", test->name); 8079d7eaa29SArthur Chunqi Li vmx_run(); 8089d7eaa29SArthur Chunqi Li if (vmx_off()) { 8099d7eaa29SArthur Chunqi Li printf("%s : vmxoff failed.\n", __func__); 8109d7eaa29SArthur Chunqi Li return 1; 8119d7eaa29SArthur Chunqi Li } 8129d7eaa29SArthur Chunqi Li return 0; 8139d7eaa29SArthur Chunqi Li } 8149d7eaa29SArthur Chunqi Li 8153ee34093SArthur Chunqi Li extern struct vmx_test vmx_tests[]; 8169d7eaa29SArthur Chunqi Li 8179d7eaa29SArthur Chunqi Li int main(void) 8189d7eaa29SArthur Chunqi Li { 8193ee34093SArthur Chunqi Li int i = 0; 8209d7eaa29SArthur Chunqi Li 8219d7eaa29SArthur Chunqi Li setup_vm(); 8229d7eaa29SArthur Chunqi Li setup_idt(); 8233ee34093SArthur Chunqi Li hypercall_field = 0; 8249d7eaa29SArthur Chunqi Li 8253b127446SJan Kiszka if (!(cpuid(1).c & (1 << 5))) { 8263b127446SJan Kiszka printf("WARNING: vmx not supported, add '-cpu host'\n"); 8279d7eaa29SArthur Chunqi Li goto exit; 8289d7eaa29SArthur Chunqi Li } 8299d7eaa29SArthur Chunqi Li init_vmx(); 8303b127446SJan Kiszka if (test_vmx_feature_control() != 0) 8313b127446SJan Kiszka goto exit; 8329d7eaa29SArthur Chunqi Li /* Set basic test ctxt the same as "null" */ 8339d7eaa29SArthur Chunqi Li current = &vmx_tests[0]; 8349d7eaa29SArthur Chunqi Li if (test_vmxon() != 0) 8359d7eaa29SArthur Chunqi Li goto exit; 8369d7eaa29SArthur Chunqi Li test_vmptrld(); 8379d7eaa29SArthur Chunqi Li test_vmclear(); 8389d7eaa29SArthur Chunqi Li test_vmptrst(); 8399d7eaa29SArthur Chunqi Li init_vmcs(&vmcs_root); 8409d7eaa29SArthur Chunqi Li if (vmx_run()) { 8419d7eaa29SArthur Chunqi Li report("test vmlaunch", 0); 8429d7eaa29SArthur Chunqi Li goto exit; 8439d7eaa29SArthur Chunqi Li } 8449d7eaa29SArthur Chunqi Li test_vmxoff(); 8459d7eaa29SArthur Chunqi Li 8463ee34093SArthur Chunqi Li while (vmx_tests[++i].name != NULL) 8479d7eaa29SArthur Chunqi Li if (test_run(&vmx_tests[i])) 8489d7eaa29SArthur Chunqi Li goto exit; 8499d7eaa29SArthur Chunqi Li 8509d7eaa29SArthur Chunqi Li exit: 851f3cdd159SJan Kiszka return report_summary(); 8529d7eaa29SArthur Chunqi Li } 853