xref: /kvm-unit-tests/x86/vmx.c (revision 00b5c590b1f3f47e21166a8df6f482849eabb87c)
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 
40ce21d809SBandan Das u64 *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;
525f18e779SJan Kiszka union vmx_ctrl_msr ctrl_pin_rev;
535f18e779SJan Kiszka union vmx_ctrl_msr ctrl_cpu_rev[2];
545f18e779SJan Kiszka union vmx_ctrl_msr ctrl_exit_rev;
555f18e779SJan Kiszka union vmx_ctrl_msr 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 
65ffb1a9e0SJan Kiszka static volatile u32 stage;
66ffb1a9e0SJan Kiszka 
67ffb1a9e0SJan Kiszka void vmx_set_test_stage(u32 s)
68ffb1a9e0SJan Kiszka {
69ffb1a9e0SJan Kiszka 	barrier();
70ffb1a9e0SJan Kiszka 	stage = s;
71ffb1a9e0SJan Kiszka 	barrier();
72ffb1a9e0SJan Kiszka }
73ffb1a9e0SJan Kiszka 
74ffb1a9e0SJan Kiszka u32 vmx_get_test_stage(void)
75ffb1a9e0SJan Kiszka {
76ffb1a9e0SJan Kiszka 	u32 s;
77ffb1a9e0SJan Kiszka 
78ffb1a9e0SJan Kiszka 	barrier();
79ffb1a9e0SJan Kiszka 	s = stage;
80ffb1a9e0SJan Kiszka 	barrier();
81ffb1a9e0SJan Kiszka 	return s;
82ffb1a9e0SJan Kiszka }
83ffb1a9e0SJan Kiszka 
84ffb1a9e0SJan Kiszka void vmx_inc_test_stage(void)
85ffb1a9e0SJan Kiszka {
86ffb1a9e0SJan Kiszka 	barrier();
87ffb1a9e0SJan Kiszka 	stage++;
88ffb1a9e0SJan Kiszka 	barrier();
89ffb1a9e0SJan Kiszka }
90ffb1a9e0SJan Kiszka 
919d7eaa29SArthur Chunqi Li static int make_vmcs_current(struct vmcs *vmcs)
929d7eaa29SArthur Chunqi Li {
939d7eaa29SArthur Chunqi Li 	bool ret;
94a739f560SBandan Das 	u64 rflags = read_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF;
959d7eaa29SArthur Chunqi Li 
96a739f560SBandan Das 	asm volatile ("push %1; popf; vmptrld %2; setbe %0"
97a739f560SBandan Das 		      : "=q" (ret) : "q" (rflags), "m" (vmcs) : "cc");
989d7eaa29SArthur Chunqi Li 	return ret;
999d7eaa29SArthur Chunqi Li }
1009d7eaa29SArthur Chunqi Li 
1019d7eaa29SArthur Chunqi Li /* entry_sysenter */
1029d7eaa29SArthur Chunqi Li asm(
1039d7eaa29SArthur Chunqi Li 	".align	4, 0x90\n\t"
1049d7eaa29SArthur Chunqi Li 	".globl	entry_sysenter\n\t"
1059d7eaa29SArthur Chunqi Li 	"entry_sysenter:\n\t"
1069d7eaa29SArthur Chunqi Li 	SAVE_GPR
1079d7eaa29SArthur Chunqi Li 	"	and	$0xf, %rax\n\t"
1089d7eaa29SArthur Chunqi Li 	"	mov	%rax, %rdi\n\t"
1099d7eaa29SArthur Chunqi Li 	"	call	syscall_handler\n\t"
1109d7eaa29SArthur Chunqi Li 	LOAD_GPR
1119d7eaa29SArthur Chunqi Li 	"	vmresume\n\t"
1129d7eaa29SArthur Chunqi Li );
1139d7eaa29SArthur Chunqi Li 
1149d7eaa29SArthur Chunqi Li static void __attribute__((__used__)) syscall_handler(u64 syscall_no)
1159d7eaa29SArthur Chunqi Li {
116d5315e3dSJan Kiszka 	if (current->syscall_handler)
1179d7eaa29SArthur Chunqi Li 		current->syscall_handler(syscall_no);
1189d7eaa29SArthur Chunqi Li }
1199d7eaa29SArthur Chunqi Li 
1209d7eaa29SArthur Chunqi Li static inline int vmx_on()
1219d7eaa29SArthur Chunqi Li {
1229d7eaa29SArthur Chunqi Li 	bool ret;
123a739f560SBandan Das 	u64 rflags = read_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF;
124a739f560SBandan Das 	asm volatile ("push %1; popf; vmxon %2; setbe %0\n\t"
125a739f560SBandan Das 		      : "=q" (ret) : "q" (rflags), "m" (vmxon_region) : "cc");
1269d7eaa29SArthur Chunqi Li 	return ret;
1279d7eaa29SArthur Chunqi Li }
1289d7eaa29SArthur Chunqi Li 
1299d7eaa29SArthur Chunqi Li static inline int vmx_off()
1309d7eaa29SArthur Chunqi Li {
1319d7eaa29SArthur Chunqi Li 	bool ret;
132a739f560SBandan Das 	u64 rflags = read_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF;
133a739f560SBandan Das 
134a739f560SBandan Das 	asm volatile("push %1; popf; vmxoff; setbe %0\n\t"
135a739f560SBandan Das 		     : "=q"(ret) : "q" (rflags) : "cc");
1369d7eaa29SArthur Chunqi Li 	return ret;
1379d7eaa29SArthur Chunqi Li }
1389d7eaa29SArthur Chunqi Li 
1393ee34093SArthur Chunqi Li void print_vmexit_info()
1409d7eaa29SArthur Chunqi Li {
1419d7eaa29SArthur Chunqi Li 	u64 guest_rip, guest_rsp;
1429d7eaa29SArthur Chunqi Li 	ulong reason = vmcs_read(EXI_REASON) & 0xff;
1439d7eaa29SArthur Chunqi Li 	ulong exit_qual = vmcs_read(EXI_QUALIFICATION);
1449d7eaa29SArthur Chunqi Li 	guest_rip = vmcs_read(GUEST_RIP);
1459d7eaa29SArthur Chunqi Li 	guest_rsp = vmcs_read(GUEST_RSP);
1469d7eaa29SArthur Chunqi Li 	printf("VMEXIT info:\n");
1479d7eaa29SArthur Chunqi Li 	printf("\tvmexit reason = %d\n", reason);
1489d7eaa29SArthur Chunqi Li 	printf("\texit qualification = 0x%x\n", exit_qual);
1499d7eaa29SArthur Chunqi Li 	printf("\tBit 31 of reason = %x\n", (vmcs_read(EXI_REASON) >> 31) & 1);
1509d7eaa29SArthur Chunqi Li 	printf("\tguest_rip = 0x%llx\n", guest_rip);
1519d7eaa29SArthur Chunqi Li 	printf("\tRAX=0x%llx    RBX=0x%llx    RCX=0x%llx    RDX=0x%llx\n",
1529d7eaa29SArthur Chunqi Li 		regs.rax, regs.rbx, regs.rcx, regs.rdx);
1539d7eaa29SArthur Chunqi Li 	printf("\tRSP=0x%llx    RBP=0x%llx    RSI=0x%llx    RDI=0x%llx\n",
1549d7eaa29SArthur Chunqi Li 		guest_rsp, regs.rbp, regs.rsi, regs.rdi);
1559d7eaa29SArthur Chunqi Li 	printf("\tR8 =0x%llx    R9 =0x%llx    R10=0x%llx    R11=0x%llx\n",
1569d7eaa29SArthur Chunqi Li 		regs.r8, regs.r9, regs.r10, regs.r11);
1579d7eaa29SArthur Chunqi Li 	printf("\tR12=0x%llx    R13=0x%llx    R14=0x%llx    R15=0x%llx\n",
1589d7eaa29SArthur Chunqi Li 		regs.r12, regs.r13, regs.r14, regs.r15);
1599d7eaa29SArthur Chunqi Li }
1609d7eaa29SArthur Chunqi Li 
1619d7eaa29SArthur Chunqi Li static void test_vmclear(void)
1629d7eaa29SArthur Chunqi Li {
163daeec979SBandan Das 	struct vmcs *tmp_root;
164e2cf1c9dSEduardo Habkost 	int width = cpuid_maxphyaddr();
165daeec979SBandan Das 
166daeec979SBandan Das 	/*
167daeec979SBandan Das 	 * Note- The tests below do not necessarily have a
168daeec979SBandan Das 	 * valid VMCS, but that's ok since the invalid vmcs
169daeec979SBandan Das 	 * is only used for a specific test and is discarded
170daeec979SBandan Das 	 * without touching its contents
171daeec979SBandan Das 	 */
172daeec979SBandan Das 
173daeec979SBandan Das 	/* Unaligned page access */
174daeec979SBandan Das 	tmp_root = (struct vmcs *)((intptr_t)vmcs_root + 1);
175daeec979SBandan Das 	report("test vmclear with unaligned vmcs",
176daeec979SBandan Das 	       vmcs_clear(tmp_root) == 1);
177daeec979SBandan Das 
178daeec979SBandan Das 	/* gpa bits beyond physical address width are set*/
179daeec979SBandan Das 	tmp_root = (struct vmcs *)((intptr_t)vmcs_root |
180daeec979SBandan Das 				   ((u64)1 << (width+1)));
181daeec979SBandan Das 	report("test vmclear with vmcs address bits set beyond physical address width",
182daeec979SBandan Das 	       vmcs_clear(tmp_root) == 1);
183daeec979SBandan Das 
184daeec979SBandan Das 	/* Pass VMXON region */
185daeec979SBandan Das 	tmp_root = (struct vmcs *)vmxon_region;
186daeec979SBandan Das 	report("test vmclear with vmxon region",
187daeec979SBandan Das 	       vmcs_clear(tmp_root) == 1);
188daeec979SBandan Das 
189daeec979SBandan Das 	/* Valid VMCS */
190daeec979SBandan Das 	report("test vmclear with valid vmcs region", vmcs_clear(vmcs_root) == 0);
191daeec979SBandan Das 
1929d7eaa29SArthur Chunqi Li }
1939d7eaa29SArthur Chunqi Li 
1949d7eaa29SArthur Chunqi Li static void test_vmxoff(void)
1959d7eaa29SArthur Chunqi Li {
1969d7eaa29SArthur Chunqi Li 	int ret;
1979d7eaa29SArthur Chunqi Li 
1989d7eaa29SArthur Chunqi Li 	ret = vmx_off();
1999d7eaa29SArthur Chunqi Li 	report("test vmxoff", !ret);
2009d7eaa29SArthur Chunqi Li }
2019d7eaa29SArthur Chunqi Li 
2029d7eaa29SArthur Chunqi Li static void __attribute__((__used__)) guest_main(void)
2039d7eaa29SArthur Chunqi Li {
2049d7eaa29SArthur Chunqi Li 	current->guest_main();
2059d7eaa29SArthur Chunqi Li }
2069d7eaa29SArthur Chunqi Li 
2079d7eaa29SArthur Chunqi Li /* guest_entry */
2089d7eaa29SArthur Chunqi Li asm(
2099d7eaa29SArthur Chunqi Li 	".align	4, 0x90\n\t"
2109d7eaa29SArthur Chunqi Li 	".globl	entry_guest\n\t"
2119d7eaa29SArthur Chunqi Li 	"guest_entry:\n\t"
2129d7eaa29SArthur Chunqi Li 	"	call guest_main\n\t"
2139d7eaa29SArthur Chunqi Li 	"	mov $1, %edi\n\t"
2149d7eaa29SArthur Chunqi Li 	"	call hypercall\n\t"
2159d7eaa29SArthur Chunqi Li );
2169d7eaa29SArthur Chunqi Li 
2176884af61SArthur Chunqi Li /* EPT paging structure related functions */
2186884af61SArthur Chunqi Li /* install_ept_entry : Install a page to a given level in EPT
2196884af61SArthur Chunqi Li 		@pml4 : addr of pml4 table
2206884af61SArthur Chunqi Li 		@pte_level : level of PTE to set
2216884af61SArthur Chunqi Li 		@guest_addr : physical address of guest
2226884af61SArthur Chunqi Li 		@pte : pte value to set
2236884af61SArthur Chunqi Li 		@pt_page : address of page table, NULL for a new page
2246884af61SArthur Chunqi Li  */
2256884af61SArthur Chunqi Li void install_ept_entry(unsigned long *pml4,
2266884af61SArthur Chunqi Li 		int pte_level,
2276884af61SArthur Chunqi Li 		unsigned long guest_addr,
2286884af61SArthur Chunqi Li 		unsigned long pte,
2296884af61SArthur Chunqi Li 		unsigned long *pt_page)
2306884af61SArthur Chunqi Li {
2316884af61SArthur Chunqi Li 	int level;
2326884af61SArthur Chunqi Li 	unsigned long *pt = pml4;
2336884af61SArthur Chunqi Li 	unsigned offset;
2346884af61SArthur Chunqi Li 
2356884af61SArthur Chunqi Li 	for (level = EPT_PAGE_LEVEL; level > pte_level; --level) {
2366884af61SArthur Chunqi Li 		offset = (guest_addr >> ((level-1) * EPT_PGDIR_WIDTH + 12))
2376884af61SArthur Chunqi Li 				& EPT_PGDIR_MASK;
2386884af61SArthur Chunqi Li 		if (!(pt[offset] & (EPT_PRESENT))) {
2396884af61SArthur Chunqi Li 			unsigned long *new_pt = pt_page;
2406884af61SArthur Chunqi Li 			if (!new_pt)
2416884af61SArthur Chunqi Li 				new_pt = alloc_page();
2426884af61SArthur Chunqi Li 			else
2436884af61SArthur Chunqi Li 				pt_page = 0;
2446884af61SArthur Chunqi Li 			memset(new_pt, 0, PAGE_SIZE);
2456884af61SArthur Chunqi Li 			pt[offset] = virt_to_phys(new_pt)
2466884af61SArthur Chunqi Li 					| EPT_RA | EPT_WA | EPT_EA;
24704b0e0f3SJan Kiszka 		} else
24804b0e0f3SJan Kiszka 			pt[offset] &= ~EPT_LARGE_PAGE;
249*00b5c590SPeter Feiner 		pt = phys_to_virt(pt[offset] & EPT_ADDR_MASK);
2506884af61SArthur Chunqi Li 	}
2516884af61SArthur Chunqi Li 	offset = ((unsigned long)guest_addr >> ((level-1) *
2526884af61SArthur Chunqi Li 			EPT_PGDIR_WIDTH + 12)) & EPT_PGDIR_MASK;
2536884af61SArthur Chunqi Li 	pt[offset] = pte;
2546884af61SArthur Chunqi Li }
2556884af61SArthur Chunqi Li 
2566884af61SArthur Chunqi Li /* Map a page, @perm is the permission of the page */
2576884af61SArthur Chunqi Li void install_ept(unsigned long *pml4,
2586884af61SArthur Chunqi Li 		unsigned long phys,
2596884af61SArthur Chunqi Li 		unsigned long guest_addr,
2606884af61SArthur Chunqi Li 		u64 perm)
2616884af61SArthur Chunqi Li {
2626884af61SArthur Chunqi Li 	install_ept_entry(pml4, 1, guest_addr, (phys & PAGE_MASK) | perm, 0);
2636884af61SArthur Chunqi Li }
2646884af61SArthur Chunqi Li 
2656884af61SArthur Chunqi Li /* Map a 1G-size page */
2666884af61SArthur Chunqi Li void install_1g_ept(unsigned long *pml4,
2676884af61SArthur Chunqi Li 		unsigned long phys,
2686884af61SArthur Chunqi Li 		unsigned long guest_addr,
2696884af61SArthur Chunqi Li 		u64 perm)
2706884af61SArthur Chunqi Li {
2716884af61SArthur Chunqi Li 	install_ept_entry(pml4, 3, guest_addr,
2726884af61SArthur Chunqi Li 			(phys & PAGE_MASK) | perm | EPT_LARGE_PAGE, 0);
2736884af61SArthur Chunqi Li }
2746884af61SArthur Chunqi Li 
2756884af61SArthur Chunqi Li /* Map a 2M-size page */
2766884af61SArthur Chunqi Li void install_2m_ept(unsigned long *pml4,
2776884af61SArthur Chunqi Li 		unsigned long phys,
2786884af61SArthur Chunqi Li 		unsigned long guest_addr,
2796884af61SArthur Chunqi Li 		u64 perm)
2806884af61SArthur Chunqi Li {
2816884af61SArthur Chunqi Li 	install_ept_entry(pml4, 2, guest_addr,
2826884af61SArthur Chunqi Li 			(phys & PAGE_MASK) | perm | EPT_LARGE_PAGE, 0);
2836884af61SArthur Chunqi Li }
2846884af61SArthur Chunqi Li 
2856884af61SArthur Chunqi Li /* setup_ept_range : Setup a range of 1:1 mapped page to EPT paging structure.
2866884af61SArthur Chunqi Li 		@start : start address of guest page
2876884af61SArthur Chunqi Li 		@len : length of address to be mapped
2886884af61SArthur Chunqi Li 		@map_1g : whether 1G page map is used
2896884af61SArthur Chunqi Li 		@map_2m : whether 2M page map is used
2906884af61SArthur Chunqi Li 		@perm : permission for every page
2916884af61SArthur Chunqi Li  */
292b947e241SJan Kiszka void setup_ept_range(unsigned long *pml4, unsigned long start,
2936884af61SArthur Chunqi Li 		     unsigned long len, int map_1g, int map_2m, u64 perm)
2946884af61SArthur Chunqi Li {
2956884af61SArthur Chunqi Li 	u64 phys = start;
2966884af61SArthur Chunqi Li 	u64 max = (u64)len + (u64)start;
2976884af61SArthur Chunqi Li 
2986884af61SArthur Chunqi Li 	if (map_1g) {
2996884af61SArthur Chunqi Li 		while (phys + PAGE_SIZE_1G <= max) {
3006884af61SArthur Chunqi Li 			install_1g_ept(pml4, phys, phys, perm);
3016884af61SArthur Chunqi Li 			phys += PAGE_SIZE_1G;
3026884af61SArthur Chunqi Li 		}
3036884af61SArthur Chunqi Li 	}
3046884af61SArthur Chunqi Li 	if (map_2m) {
3056884af61SArthur Chunqi Li 		while (phys + PAGE_SIZE_2M <= max) {
3066884af61SArthur Chunqi Li 			install_2m_ept(pml4, phys, phys, perm);
3076884af61SArthur Chunqi Li 			phys += PAGE_SIZE_2M;
3086884af61SArthur Chunqi Li 		}
3096884af61SArthur Chunqi Li 	}
3106884af61SArthur Chunqi Li 	while (phys + PAGE_SIZE <= max) {
3116884af61SArthur Chunqi Li 		install_ept(pml4, phys, phys, perm);
3126884af61SArthur Chunqi Li 		phys += PAGE_SIZE;
3136884af61SArthur Chunqi Li 	}
3146884af61SArthur Chunqi Li }
3156884af61SArthur Chunqi Li 
3166884af61SArthur Chunqi Li /* get_ept_pte : Get the PTE of a given level in EPT,
3176884af61SArthur Chunqi Li     @level == 1 means get the latest level*/
3186884af61SArthur Chunqi Li unsigned long get_ept_pte(unsigned long *pml4,
3196884af61SArthur Chunqi Li 		unsigned long guest_addr, int level)
3206884af61SArthur Chunqi Li {
3216884af61SArthur Chunqi Li 	int l;
3226884af61SArthur Chunqi Li 	unsigned long *pt = pml4, pte;
3236884af61SArthur Chunqi Li 	unsigned offset;
3246884af61SArthur Chunqi Li 
3252ca6f1f3SPaolo Bonzini 	if (level < 1 || level > 3)
3262ca6f1f3SPaolo Bonzini 		return -1;
3272ca6f1f3SPaolo Bonzini 	for (l = EPT_PAGE_LEVEL; ; --l) {
3286884af61SArthur Chunqi Li 		offset = (guest_addr >> (((l-1) * EPT_PGDIR_WIDTH) + 12))
3296884af61SArthur Chunqi Li 				& EPT_PGDIR_MASK;
3306884af61SArthur Chunqi Li 		pte = pt[offset];
3316884af61SArthur Chunqi Li 		if (!(pte & (EPT_PRESENT)))
3326884af61SArthur Chunqi Li 			return 0;
3336884af61SArthur Chunqi Li 		if (l == level)
3342ca6f1f3SPaolo Bonzini 			break;
3356884af61SArthur Chunqi Li 		if (l < 4 && (pte & EPT_LARGE_PAGE))
3366884af61SArthur Chunqi Li 			return pte;
337*00b5c590SPeter Feiner 		pt = (unsigned long *)(pte & EPT_ADDR_MASK);
3386884af61SArthur Chunqi Li 	}
3396884af61SArthur Chunqi Li 	offset = (guest_addr >> (((l-1) * EPT_PGDIR_WIDTH) + 12))
3406884af61SArthur Chunqi Li 			& EPT_PGDIR_MASK;
3416884af61SArthur Chunqi Li 	pte = pt[offset];
3426884af61SArthur Chunqi Li 	return pte;
3436884af61SArthur Chunqi Li }
3446884af61SArthur Chunqi Li 
3452f888fccSBandan Das void ept_sync(int type, u64 eptp)
3462f888fccSBandan Das {
3472f888fccSBandan Das 	switch (type) {
3482f888fccSBandan Das 	case INVEPT_SINGLE:
3492f888fccSBandan Das 		if (ept_vpid.val & EPT_CAP_INVEPT_SINGLE) {
3502f888fccSBandan Das 			invept(INVEPT_SINGLE, eptp);
3512f888fccSBandan Das 			break;
3522f888fccSBandan Das 		}
3532f888fccSBandan Das 		/* else fall through */
3542f888fccSBandan Das 	case INVEPT_GLOBAL:
3552f888fccSBandan Das 		if (ept_vpid.val & EPT_CAP_INVEPT_ALL) {
3562f888fccSBandan Das 			invept(INVEPT_GLOBAL, eptp);
3572f888fccSBandan Das 			break;
3582f888fccSBandan Das 		}
3592f888fccSBandan Das 		/* else fall through */
3602f888fccSBandan Das 	default:
3612f888fccSBandan Das 		printf("WARNING: invept is not supported!\n");
3622f888fccSBandan Das 	}
3632f888fccSBandan Das }
3642f888fccSBandan Das 
3656884af61SArthur Chunqi Li int set_ept_pte(unsigned long *pml4, unsigned long guest_addr,
3666884af61SArthur Chunqi Li 		int level, u64 pte_val)
3676884af61SArthur Chunqi Li {
3686884af61SArthur Chunqi Li 	int l;
3696884af61SArthur Chunqi Li 	unsigned long *pt = pml4;
3706884af61SArthur Chunqi Li 	unsigned offset;
3716884af61SArthur Chunqi Li 
3726884af61SArthur Chunqi Li 	if (level < 1 || level > 3)
3736884af61SArthur Chunqi Li 		return -1;
3742ca6f1f3SPaolo Bonzini 	for (l = EPT_PAGE_LEVEL; ; --l) {
3756884af61SArthur Chunqi Li 		offset = (guest_addr >> (((l-1) * EPT_PGDIR_WIDTH) + 12))
3766884af61SArthur Chunqi Li 				& EPT_PGDIR_MASK;
3772ca6f1f3SPaolo Bonzini 		if (l == level)
3782ca6f1f3SPaolo Bonzini 			break;
3796884af61SArthur Chunqi Li 		if (!(pt[offset] & (EPT_PRESENT)))
3806884af61SArthur Chunqi Li 			return -1;
381*00b5c590SPeter Feiner 		pt = (unsigned long *)(pt[offset] & EPT_ADDR_MASK);
3826884af61SArthur Chunqi Li 	}
3836884af61SArthur Chunqi Li 	offset = (guest_addr >> (((l-1) * EPT_PGDIR_WIDTH) + 12))
3846884af61SArthur Chunqi Li 			& EPT_PGDIR_MASK;
3856884af61SArthur Chunqi Li 	pt[offset] = pte_val;
3866884af61SArthur Chunqi Li 	return 0;
3876884af61SArthur Chunqi Li }
3886884af61SArthur Chunqi Li 
389b093c6ceSWanpeng Li void vpid_sync(int type, u16 vpid)
390b093c6ceSWanpeng Li {
391b093c6ceSWanpeng Li 	switch(type) {
392b093c6ceSWanpeng Li 	case INVVPID_SINGLE:
393b093c6ceSWanpeng Li 		if (ept_vpid.val & VPID_CAP_INVVPID_SINGLE) {
394b093c6ceSWanpeng Li 			invvpid(INVVPID_SINGLE, vpid, 0);
395b093c6ceSWanpeng Li 			break;
396b093c6ceSWanpeng Li 		}
397b093c6ceSWanpeng Li 	case INVVPID_ALL:
398b093c6ceSWanpeng Li 		if (ept_vpid.val & VPID_CAP_INVVPID_ALL) {
399b093c6ceSWanpeng Li 			invvpid(INVVPID_ALL, vpid, 0);
400b093c6ceSWanpeng Li 			break;
401b093c6ceSWanpeng Li 		}
402b093c6ceSWanpeng Li 	default:
403b093c6ceSWanpeng Li 		printf("WARNING: invvpid is not supported\n");
404b093c6ceSWanpeng Li 	}
405b093c6ceSWanpeng Li }
4066884af61SArthur Chunqi Li 
4079d7eaa29SArthur Chunqi Li static void init_vmcs_ctrl(void)
4089d7eaa29SArthur Chunqi Li {
4099d7eaa29SArthur Chunqi Li 	/* 26.2 CHECKS ON VMX CONTROLS AND HOST-STATE AREA */
4109d7eaa29SArthur Chunqi Li 	/* 26.2.1.1 */
4119d7eaa29SArthur Chunqi Li 	vmcs_write(PIN_CONTROLS, ctrl_pin);
4129d7eaa29SArthur Chunqi Li 	/* Disable VMEXIT of IO instruction */
4139d7eaa29SArthur Chunqi Li 	vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu[0]);
4149d7eaa29SArthur Chunqi Li 	if (ctrl_cpu_rev[0].set & CPU_SECONDARY) {
4156884af61SArthur Chunqi Li 		ctrl_cpu[1] = (ctrl_cpu[1] | ctrl_cpu_rev[1].set) &
4166884af61SArthur Chunqi Li 			ctrl_cpu_rev[1].clr;
4179d7eaa29SArthur Chunqi Li 		vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu[1]);
4189d7eaa29SArthur Chunqi Li 	}
4199d7eaa29SArthur Chunqi Li 	vmcs_write(CR3_TARGET_COUNT, 0);
4209d7eaa29SArthur Chunqi Li 	vmcs_write(VPID, ++vpid_cnt);
4219d7eaa29SArthur Chunqi Li }
4229d7eaa29SArthur Chunqi Li 
4239d7eaa29SArthur Chunqi Li static void init_vmcs_host(void)
4249d7eaa29SArthur Chunqi Li {
4259d7eaa29SArthur Chunqi Li 	/* 26.2 CHECKS ON VMX CONTROLS AND HOST-STATE AREA */
4269d7eaa29SArthur Chunqi Li 	/* 26.2.1.2 */
4279d7eaa29SArthur Chunqi Li 	vmcs_write(HOST_EFER, rdmsr(MSR_EFER));
4289d7eaa29SArthur Chunqi Li 
4299d7eaa29SArthur Chunqi Li 	/* 26.2.1.3 */
4309d7eaa29SArthur Chunqi Li 	vmcs_write(ENT_CONTROLS, ctrl_enter);
4319d7eaa29SArthur Chunqi Li 	vmcs_write(EXI_CONTROLS, ctrl_exit);
4329d7eaa29SArthur Chunqi Li 
4339d7eaa29SArthur Chunqi Li 	/* 26.2.2 */
4349d7eaa29SArthur Chunqi Li 	vmcs_write(HOST_CR0, read_cr0());
4359d7eaa29SArthur Chunqi Li 	vmcs_write(HOST_CR3, read_cr3());
4369d7eaa29SArthur Chunqi Li 	vmcs_write(HOST_CR4, read_cr4());
4379d7eaa29SArthur Chunqi Li 	vmcs_write(HOST_SYSENTER_EIP, (u64)(&entry_sysenter));
43869d8fe0eSPaolo Bonzini 	vmcs_write(HOST_SYSENTER_CS,  KERNEL_CS);
4399d7eaa29SArthur Chunqi Li 
4409d7eaa29SArthur Chunqi Li 	/* 26.2.3 */
44169d8fe0eSPaolo Bonzini 	vmcs_write(HOST_SEL_CS, KERNEL_CS);
44269d8fe0eSPaolo Bonzini 	vmcs_write(HOST_SEL_SS, KERNEL_DS);
44369d8fe0eSPaolo Bonzini 	vmcs_write(HOST_SEL_DS, KERNEL_DS);
44469d8fe0eSPaolo Bonzini 	vmcs_write(HOST_SEL_ES, KERNEL_DS);
44569d8fe0eSPaolo Bonzini 	vmcs_write(HOST_SEL_FS, KERNEL_DS);
44669d8fe0eSPaolo Bonzini 	vmcs_write(HOST_SEL_GS, KERNEL_DS);
44769d8fe0eSPaolo Bonzini 	vmcs_write(HOST_SEL_TR, TSS_MAIN);
448337166aaSJan Kiszka 	vmcs_write(HOST_BASE_TR, tss_descr.base);
449337166aaSJan Kiszka 	vmcs_write(HOST_BASE_GDTR, gdt64_desc.base);
450337166aaSJan Kiszka 	vmcs_write(HOST_BASE_IDTR, idt_descr.base);
4519d7eaa29SArthur Chunqi Li 	vmcs_write(HOST_BASE_FS, 0);
4529d7eaa29SArthur Chunqi Li 	vmcs_write(HOST_BASE_GS, 0);
4539d7eaa29SArthur Chunqi Li 
4549d7eaa29SArthur Chunqi Li 	/* Set other vmcs area */
4559d7eaa29SArthur Chunqi Li 	vmcs_write(PF_ERROR_MASK, 0);
4569d7eaa29SArthur Chunqi Li 	vmcs_write(PF_ERROR_MATCH, 0);
4579d7eaa29SArthur Chunqi Li 	vmcs_write(VMCS_LINK_PTR, ~0ul);
4589d7eaa29SArthur Chunqi Li 	vmcs_write(VMCS_LINK_PTR_HI, ~0ul);
4599d7eaa29SArthur Chunqi Li 	vmcs_write(HOST_RIP, (u64)(&vmx_return));
4609d7eaa29SArthur Chunqi Li }
4619d7eaa29SArthur Chunqi Li 
4629d7eaa29SArthur Chunqi Li static void init_vmcs_guest(void)
4639d7eaa29SArthur Chunqi Li {
4649d7eaa29SArthur Chunqi Li 	/* 26.3 CHECKING AND LOADING GUEST STATE */
4659d7eaa29SArthur Chunqi Li 	ulong guest_cr0, guest_cr4, guest_cr3;
4669d7eaa29SArthur Chunqi Li 	/* 26.3.1.1 */
4679d7eaa29SArthur Chunqi Li 	guest_cr0 = read_cr0();
4689d7eaa29SArthur Chunqi Li 	guest_cr4 = read_cr4();
4699d7eaa29SArthur Chunqi Li 	guest_cr3 = read_cr3();
4709d7eaa29SArthur Chunqi Li 	if (ctrl_enter & ENT_GUEST_64) {
4719d7eaa29SArthur Chunqi Li 		guest_cr0 |= X86_CR0_PG;
4729d7eaa29SArthur Chunqi Li 		guest_cr4 |= X86_CR4_PAE;
4739d7eaa29SArthur Chunqi Li 	}
4749d7eaa29SArthur Chunqi Li 	if ((ctrl_enter & ENT_GUEST_64) == 0)
4759d7eaa29SArthur Chunqi Li 		guest_cr4 &= (~X86_CR4_PCIDE);
4769d7eaa29SArthur Chunqi Li 	if (guest_cr0 & X86_CR0_PG)
4779d7eaa29SArthur Chunqi Li 		guest_cr0 |= X86_CR0_PE;
4789d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_CR0, guest_cr0);
4799d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_CR3, guest_cr3);
4809d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_CR4, guest_cr4);
48169d8fe0eSPaolo Bonzini 	vmcs_write(GUEST_SYSENTER_CS,  KERNEL_CS);
4829d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_SYSENTER_ESP,
4839d7eaa29SArthur Chunqi Li 		(u64)(guest_syscall_stack + PAGE_SIZE - 1));
4849d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_SYSENTER_EIP, (u64)(&entry_sysenter));
4859d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_DR7, 0);
4869d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_EFER, rdmsr(MSR_EFER));
4879d7eaa29SArthur Chunqi Li 
4889d7eaa29SArthur Chunqi Li 	/* 26.3.1.2 */
48969d8fe0eSPaolo Bonzini 	vmcs_write(GUEST_SEL_CS, KERNEL_CS);
49069d8fe0eSPaolo Bonzini 	vmcs_write(GUEST_SEL_SS, KERNEL_DS);
49169d8fe0eSPaolo Bonzini 	vmcs_write(GUEST_SEL_DS, KERNEL_DS);
49269d8fe0eSPaolo Bonzini 	vmcs_write(GUEST_SEL_ES, KERNEL_DS);
49369d8fe0eSPaolo Bonzini 	vmcs_write(GUEST_SEL_FS, KERNEL_DS);
49469d8fe0eSPaolo Bonzini 	vmcs_write(GUEST_SEL_GS, KERNEL_DS);
49569d8fe0eSPaolo Bonzini 	vmcs_write(GUEST_SEL_TR, TSS_MAIN);
4969d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_SEL_LDTR, 0);
4979d7eaa29SArthur Chunqi Li 
4989d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_BASE_CS, 0);
4999d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_BASE_ES, 0);
5009d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_BASE_SS, 0);
5019d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_BASE_DS, 0);
5029d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_BASE_FS, 0);
5039d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_BASE_GS, 0);
504337166aaSJan Kiszka 	vmcs_write(GUEST_BASE_TR, tss_descr.base);
5059d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_BASE_LDTR, 0);
5069d7eaa29SArthur Chunqi Li 
5079d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_LIMIT_CS, 0xFFFFFFFF);
5089d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_LIMIT_DS, 0xFFFFFFFF);
5099d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_LIMIT_ES, 0xFFFFFFFF);
5109d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_LIMIT_SS, 0xFFFFFFFF);
5119d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_LIMIT_FS, 0xFFFFFFFF);
5129d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_LIMIT_GS, 0xFFFFFFFF);
5139d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_LIMIT_LDTR, 0xffff);
514337166aaSJan Kiszka 	vmcs_write(GUEST_LIMIT_TR, tss_descr.limit);
5159d7eaa29SArthur Chunqi Li 
5169d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_AR_CS, 0xa09b);
5179d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_AR_DS, 0xc093);
5189d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_AR_ES, 0xc093);
5199d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_AR_FS, 0xc093);
5209d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_AR_GS, 0xc093);
5219d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_AR_SS, 0xc093);
5229d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_AR_LDTR, 0x82);
5239d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_AR_TR, 0x8b);
5249d7eaa29SArthur Chunqi Li 
5259d7eaa29SArthur Chunqi Li 	/* 26.3.1.3 */
526337166aaSJan Kiszka 	vmcs_write(GUEST_BASE_GDTR, gdt64_desc.base);
527337166aaSJan Kiszka 	vmcs_write(GUEST_BASE_IDTR, idt_descr.base);
528337166aaSJan Kiszka 	vmcs_write(GUEST_LIMIT_GDTR, gdt64_desc.limit);
529337166aaSJan Kiszka 	vmcs_write(GUEST_LIMIT_IDTR, idt_descr.limit);
5309d7eaa29SArthur Chunqi Li 
5319d7eaa29SArthur Chunqi Li 	/* 26.3.1.4 */
5329d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_RIP, (u64)(&guest_entry));
5339d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_RSP, (u64)(guest_stack + PAGE_SIZE - 1));
5349d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_RFLAGS, 0x2);
5359d7eaa29SArthur Chunqi Li 
5369d7eaa29SArthur Chunqi Li 	/* 26.3.1.5 */
53717ba0dd0SJan Kiszka 	vmcs_write(GUEST_ACTV_STATE, ACTV_ACTIVE);
5389d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_INTR_STATE, 0);
5399d7eaa29SArthur Chunqi Li }
5409d7eaa29SArthur Chunqi Li 
5419d7eaa29SArthur Chunqi Li static int init_vmcs(struct vmcs **vmcs)
5429d7eaa29SArthur Chunqi Li {
5439d7eaa29SArthur Chunqi Li 	*vmcs = alloc_page();
5449d7eaa29SArthur Chunqi Li 	memset(*vmcs, 0, PAGE_SIZE);
5459d7eaa29SArthur Chunqi Li 	(*vmcs)->revision_id = basic.revision;
5469d7eaa29SArthur Chunqi Li 	/* vmclear first to init vmcs */
5479d7eaa29SArthur Chunqi Li 	if (vmcs_clear(*vmcs)) {
5489d7eaa29SArthur Chunqi Li 		printf("%s : vmcs_clear error\n", __func__);
5499d7eaa29SArthur Chunqi Li 		return 1;
5509d7eaa29SArthur Chunqi Li 	}
5519d7eaa29SArthur Chunqi Li 
5529d7eaa29SArthur Chunqi Li 	if (make_vmcs_current(*vmcs)) {
5539d7eaa29SArthur Chunqi Li 		printf("%s : make_vmcs_current error\n", __func__);
5549d7eaa29SArthur Chunqi Li 		return 1;
5559d7eaa29SArthur Chunqi Li 	}
5569d7eaa29SArthur Chunqi Li 
5579d7eaa29SArthur Chunqi Li 	/* All settings to pin/exit/enter/cpu
5589d7eaa29SArthur Chunqi Li 	   control fields should be placed here */
5599d7eaa29SArthur Chunqi Li 	ctrl_pin |= PIN_EXTINT | PIN_NMI | PIN_VIRT_NMI;
5609d7eaa29SArthur Chunqi Li 	ctrl_exit = EXI_LOAD_EFER | EXI_HOST_64;
5619d7eaa29SArthur Chunqi Li 	ctrl_enter = (ENT_LOAD_EFER | ENT_GUEST_64);
5629d7eaa29SArthur Chunqi Li 	/* DIsable IO instruction VMEXIT now */
5639d7eaa29SArthur Chunqi Li 	ctrl_cpu[0] &= (~(CPU_IO | CPU_IO_BITMAP));
5649d7eaa29SArthur Chunqi Li 	ctrl_cpu[1] = 0;
5659d7eaa29SArthur Chunqi Li 
5669d7eaa29SArthur Chunqi Li 	ctrl_pin = (ctrl_pin | ctrl_pin_rev.set) & ctrl_pin_rev.clr;
5679d7eaa29SArthur Chunqi Li 	ctrl_enter = (ctrl_enter | ctrl_enter_rev.set) & ctrl_enter_rev.clr;
5689d7eaa29SArthur Chunqi Li 	ctrl_exit = (ctrl_exit | ctrl_exit_rev.set) & ctrl_exit_rev.clr;
5699d7eaa29SArthur Chunqi Li 	ctrl_cpu[0] = (ctrl_cpu[0] | ctrl_cpu_rev[0].set) & ctrl_cpu_rev[0].clr;
5709d7eaa29SArthur Chunqi Li 
5719d7eaa29SArthur Chunqi Li 	init_vmcs_ctrl();
5729d7eaa29SArthur Chunqi Li 	init_vmcs_host();
5739d7eaa29SArthur Chunqi Li 	init_vmcs_guest();
5749d7eaa29SArthur Chunqi Li 	return 0;
5759d7eaa29SArthur Chunqi Li }
5769d7eaa29SArthur Chunqi Li 
5779d7eaa29SArthur Chunqi Li static void init_vmx(void)
5789d7eaa29SArthur Chunqi Li {
5793ee34093SArthur Chunqi Li 	ulong fix_cr0_set, fix_cr0_clr;
5803ee34093SArthur Chunqi Li 	ulong fix_cr4_set, fix_cr4_clr;
5813ee34093SArthur Chunqi Li 
5829d7eaa29SArthur Chunqi Li 	vmxon_region = alloc_page();
5839d7eaa29SArthur Chunqi Li 	memset(vmxon_region, 0, PAGE_SIZE);
5849d7eaa29SArthur Chunqi Li 
5859d7eaa29SArthur Chunqi Li 	fix_cr0_set =  rdmsr(MSR_IA32_VMX_CR0_FIXED0);
5869d7eaa29SArthur Chunqi Li 	fix_cr0_clr =  rdmsr(MSR_IA32_VMX_CR0_FIXED1);
5879d7eaa29SArthur Chunqi Li 	fix_cr4_set =  rdmsr(MSR_IA32_VMX_CR4_FIXED0);
5889d7eaa29SArthur Chunqi Li 	fix_cr4_clr = rdmsr(MSR_IA32_VMX_CR4_FIXED1);
5899d7eaa29SArthur Chunqi Li 	basic.val = rdmsr(MSR_IA32_VMX_BASIC);
5909d7eaa29SArthur Chunqi Li 	ctrl_pin_rev.val = rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_PIN
5919d7eaa29SArthur Chunqi Li 			: MSR_IA32_VMX_PINBASED_CTLS);
5929d7eaa29SArthur Chunqi Li 	ctrl_exit_rev.val = rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_EXIT
5939d7eaa29SArthur Chunqi Li 			: MSR_IA32_VMX_EXIT_CTLS);
5949d7eaa29SArthur Chunqi Li 	ctrl_enter_rev.val = rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_ENTRY
5959d7eaa29SArthur Chunqi Li 			: MSR_IA32_VMX_ENTRY_CTLS);
5969d7eaa29SArthur Chunqi Li 	ctrl_cpu_rev[0].val = rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_PROC
5979d7eaa29SArthur Chunqi Li 			: MSR_IA32_VMX_PROCBASED_CTLS);
5986884af61SArthur Chunqi Li 	if ((ctrl_cpu_rev[0].clr & CPU_SECONDARY) != 0)
5999d7eaa29SArthur Chunqi Li 		ctrl_cpu_rev[1].val = rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2);
6006884af61SArthur Chunqi Li 	else
6016884af61SArthur Chunqi Li 		ctrl_cpu_rev[1].val = 0;
6026884af61SArthur Chunqi Li 	if ((ctrl_cpu_rev[1].clr & (CPU_EPT | CPU_VPID)) != 0)
6039d7eaa29SArthur Chunqi Li 		ept_vpid.val = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP);
6046884af61SArthur Chunqi Li 	else
6056884af61SArthur Chunqi Li 		ept_vpid.val = 0;
6069d7eaa29SArthur Chunqi Li 
6079d7eaa29SArthur Chunqi Li 	write_cr0((read_cr0() & fix_cr0_clr) | fix_cr0_set);
6089d7eaa29SArthur Chunqi Li 	write_cr4((read_cr4() & fix_cr4_clr) | fix_cr4_set | X86_CR4_VMXE);
6099d7eaa29SArthur Chunqi Li 
6109d7eaa29SArthur Chunqi Li 	*vmxon_region = basic.revision;
6119d7eaa29SArthur Chunqi Li 
6129d7eaa29SArthur Chunqi Li 	guest_stack = alloc_page();
6139d7eaa29SArthur Chunqi Li 	memset(guest_stack, 0, PAGE_SIZE);
6149d7eaa29SArthur Chunqi Li 	guest_syscall_stack = alloc_page();
6159d7eaa29SArthur Chunqi Li 	memset(guest_syscall_stack, 0, PAGE_SIZE);
6169d7eaa29SArthur Chunqi Li }
6179d7eaa29SArthur Chunqi Li 
618e3f363c4SJan Kiszka static void do_vmxon_off(void *data)
6199d7eaa29SArthur Chunqi Li {
6203b127446SJan Kiszka 	vmx_on();
6213b127446SJan Kiszka 	vmx_off();
62203f37ef2SPaolo Bonzini }
6233b127446SJan Kiszka 
624e3f363c4SJan Kiszka static void do_write_feature_control(void *data)
6253b127446SJan Kiszka {
6263b127446SJan Kiszka 	wrmsr(MSR_IA32_FEATURE_CONTROL, 0);
62703f37ef2SPaolo Bonzini }
6283b127446SJan Kiszka 
6293b127446SJan Kiszka static int test_vmx_feature_control(void)
6303b127446SJan Kiszka {
6313b127446SJan Kiszka 	u64 ia32_feature_control;
6323b127446SJan Kiszka 	bool vmx_enabled;
6333b127446SJan Kiszka 
6343b127446SJan Kiszka 	ia32_feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL);
6353b127446SJan Kiszka 	vmx_enabled = ((ia32_feature_control & 0x5) == 0x5);
6363b127446SJan Kiszka 	if ((ia32_feature_control & 0x5) == 0x5) {
6373b127446SJan Kiszka 		printf("VMX enabled and locked by BIOS\n");
6383b127446SJan Kiszka 		return 0;
6393b127446SJan Kiszka 	} else if (ia32_feature_control & 0x1) {
6403b127446SJan Kiszka 		printf("ERROR: VMX locked out by BIOS!?\n");
6413b127446SJan Kiszka 		return 1;
6423b127446SJan Kiszka 	}
6433b127446SJan Kiszka 
6443b127446SJan Kiszka 	wrmsr(MSR_IA32_FEATURE_CONTROL, 0);
6453b127446SJan Kiszka 	report("test vmxon with FEATURE_CONTROL cleared",
646e3f363c4SJan Kiszka 	       test_for_exception(GP_VECTOR, &do_vmxon_off, NULL));
6473b127446SJan Kiszka 
6483b127446SJan Kiszka 	wrmsr(MSR_IA32_FEATURE_CONTROL, 0x4);
6493b127446SJan Kiszka 	report("test vmxon without FEATURE_CONTROL lock",
650e3f363c4SJan Kiszka 	       test_for_exception(GP_VECTOR, &do_vmxon_off, NULL));
6513b127446SJan Kiszka 
6523b127446SJan Kiszka 	wrmsr(MSR_IA32_FEATURE_CONTROL, 0x5);
6533b127446SJan Kiszka 	vmx_enabled = ((rdmsr(MSR_IA32_FEATURE_CONTROL) & 0x5) == 0x5);
6543b127446SJan Kiszka 	report("test enable VMX in FEATURE_CONTROL", vmx_enabled);
6553b127446SJan Kiszka 
6563b127446SJan Kiszka 	report("test FEATURE_CONTROL lock bit",
657e3f363c4SJan Kiszka 	       test_for_exception(GP_VECTOR, &do_write_feature_control, NULL));
6583b127446SJan Kiszka 
6593b127446SJan Kiszka 	return !vmx_enabled;
6609d7eaa29SArthur Chunqi Li }
6619d7eaa29SArthur Chunqi Li 
6629d7eaa29SArthur Chunqi Li static int test_vmxon(void)
6639d7eaa29SArthur Chunqi Li {
664ce21d809SBandan Das 	int ret, ret1;
665ce21d809SBandan Das 	u64 *tmp_region = vmxon_region;
666e2cf1c9dSEduardo Habkost 	int width = cpuid_maxphyaddr();
6679d7eaa29SArthur Chunqi Li 
668ce21d809SBandan Das 	/* Unaligned page access */
669ce21d809SBandan Das 	vmxon_region = (u64 *)((intptr_t)vmxon_region + 1);
670ce21d809SBandan Das 	ret1 = vmx_on();
671ce21d809SBandan Das 	report("test vmxon with unaligned vmxon region", ret1);
672ce21d809SBandan Das 	if (!ret1) {
673ce21d809SBandan Das 		ret = 1;
674ce21d809SBandan Das 		goto out;
675ce21d809SBandan Das 	}
676ce21d809SBandan Das 
677ce21d809SBandan Das 	/* gpa bits beyond physical address width are set*/
678ce21d809SBandan Das 	vmxon_region = (u64 *)((intptr_t)tmp_region | ((u64)1 << (width+1)));
679ce21d809SBandan Das 	ret1 = vmx_on();
680ce21d809SBandan Das 	report("test vmxon with bits set beyond physical address width", ret1);
681ce21d809SBandan Das 	if (!ret1) {
682ce21d809SBandan Das 		ret = 1;
683ce21d809SBandan Das 		goto out;
684ce21d809SBandan Das 	}
685ce21d809SBandan Das 
686ce21d809SBandan Das 	/* invalid revision indentifier */
687ce21d809SBandan Das 	vmxon_region = tmp_region;
688ce21d809SBandan Das 	*vmxon_region = 0xba9da9;
689ce21d809SBandan Das 	ret1 = vmx_on();
690ce21d809SBandan Das 	report("test vmxon with invalid revision identifier", ret1);
691ce21d809SBandan Das 	if (!ret1) {
692ce21d809SBandan Das 		ret = 1;
693ce21d809SBandan Das 		goto out;
694ce21d809SBandan Das 	}
695ce21d809SBandan Das 
696ce21d809SBandan Das 	/* and finally a valid region */
697ce21d809SBandan Das 	*vmxon_region = basic.revision;
6989d7eaa29SArthur Chunqi Li 	ret = vmx_on();
699ce21d809SBandan Das 	report("test vmxon with valid vmxon region", !ret);
700ce21d809SBandan Das 
701ce21d809SBandan Das out:
7029d7eaa29SArthur Chunqi Li 	return ret;
7039d7eaa29SArthur Chunqi Li }
7049d7eaa29SArthur Chunqi Li 
7059d7eaa29SArthur Chunqi Li static void test_vmptrld(void)
7069d7eaa29SArthur Chunqi Li {
707daeec979SBandan Das 	struct vmcs *vmcs, *tmp_root;
708e2cf1c9dSEduardo Habkost 	int width = cpuid_maxphyaddr();
7099d7eaa29SArthur Chunqi Li 
7109d7eaa29SArthur Chunqi Li 	vmcs = alloc_page();
7119d7eaa29SArthur Chunqi Li 	vmcs->revision_id = basic.revision;
712daeec979SBandan Das 
713daeec979SBandan Das 	/* Unaligned page access */
714daeec979SBandan Das 	tmp_root = (struct vmcs *)((intptr_t)vmcs + 1);
715daeec979SBandan Das 	report("test vmptrld with unaligned vmcs",
7169c305952SPaolo Bonzini 	       make_vmcs_current(tmp_root) == 1);
717daeec979SBandan Das 
718daeec979SBandan Das 	/* gpa bits beyond physical address width are set*/
719daeec979SBandan Das 	tmp_root = (struct vmcs *)((intptr_t)vmcs |
720daeec979SBandan Das 				   ((u64)1 << (width+1)));
721daeec979SBandan Das 	report("test vmptrld with vmcs address bits set beyond physical address width",
7229c305952SPaolo Bonzini 	       make_vmcs_current(tmp_root) == 1);
723daeec979SBandan Das 
724daeec979SBandan Das 	/* Pass VMXON region */
725daeec979SBandan Das 	tmp_root = (struct vmcs *)vmxon_region;
726daeec979SBandan Das 	report("test vmptrld with vmxon region",
7279c305952SPaolo Bonzini 	       make_vmcs_current(tmp_root) == 1);
728daeec979SBandan Das 
729daeec979SBandan Das 	report("test vmptrld with valid vmcs region", make_vmcs_current(vmcs) == 0);
7309d7eaa29SArthur Chunqi Li }
7319d7eaa29SArthur Chunqi Li 
7329d7eaa29SArthur Chunqi Li static void test_vmptrst(void)
7339d7eaa29SArthur Chunqi Li {
7349d7eaa29SArthur Chunqi Li 	int ret;
7359d7eaa29SArthur Chunqi Li 	struct vmcs *vmcs1, *vmcs2;
7369d7eaa29SArthur Chunqi Li 
7379d7eaa29SArthur Chunqi Li 	vmcs1 = alloc_page();
7389d7eaa29SArthur Chunqi Li 	memset(vmcs1, 0, PAGE_SIZE);
7399d7eaa29SArthur Chunqi Li 	init_vmcs(&vmcs1);
7409d7eaa29SArthur Chunqi Li 	ret = vmcs_save(&vmcs2);
7419d7eaa29SArthur Chunqi Li 	report("test vmptrst", (!ret) && (vmcs1 == vmcs2));
7429d7eaa29SArthur Chunqi Li }
7439d7eaa29SArthur Chunqi Li 
74469c8d31cSJan Kiszka struct vmx_ctl_msr {
74569c8d31cSJan Kiszka 	const char *name;
74669c8d31cSJan Kiszka 	u32 index, true_index;
74769c8d31cSJan Kiszka 	u32 default1;
74869c8d31cSJan Kiszka } vmx_ctl_msr[] = {
74969c8d31cSJan Kiszka 	{ "MSR_IA32_VMX_PINBASED_CTLS", MSR_IA32_VMX_PINBASED_CTLS,
75069c8d31cSJan Kiszka 	  MSR_IA32_VMX_TRUE_PIN, 0x16 },
75169c8d31cSJan Kiszka 	{ "MSR_IA32_VMX_PROCBASED_CTLS", MSR_IA32_VMX_PROCBASED_CTLS,
75269c8d31cSJan Kiszka 	  MSR_IA32_VMX_TRUE_PROC, 0x401e172 },
75369c8d31cSJan Kiszka 	{ "MSR_IA32_VMX_PROCBASED_CTLS2", MSR_IA32_VMX_PROCBASED_CTLS2,
75469c8d31cSJan Kiszka 	  MSR_IA32_VMX_PROCBASED_CTLS2, 0 },
75569c8d31cSJan Kiszka 	{ "MSR_IA32_VMX_EXIT_CTLS", MSR_IA32_VMX_EXIT_CTLS,
75669c8d31cSJan Kiszka 	  MSR_IA32_VMX_TRUE_EXIT, 0x36dff },
75769c8d31cSJan Kiszka 	{ "MSR_IA32_VMX_ENTRY_CTLS", MSR_IA32_VMX_ENTRY_CTLS,
75869c8d31cSJan Kiszka 	  MSR_IA32_VMX_TRUE_ENTRY, 0x11ff },
75969c8d31cSJan Kiszka };
76069c8d31cSJan Kiszka 
76169c8d31cSJan Kiszka static void test_vmx_caps(void)
76269c8d31cSJan Kiszka {
76369c8d31cSJan Kiszka 	u64 val, default1, fixed0, fixed1;
76469c8d31cSJan Kiszka 	union vmx_ctrl_msr ctrl, true_ctrl;
76569c8d31cSJan Kiszka 	unsigned int n;
76669c8d31cSJan Kiszka 	bool ok;
76769c8d31cSJan Kiszka 
76869c8d31cSJan Kiszka 	printf("\nTest suite: VMX capability reporting\n");
76969c8d31cSJan Kiszka 
77069c8d31cSJan Kiszka 	report("MSR_IA32_VMX_BASIC",
77169c8d31cSJan Kiszka 	       (basic.revision & (1ul << 31)) == 0 &&
77269c8d31cSJan Kiszka 	       basic.size > 0 && basic.size <= 4096 &&
77369c8d31cSJan Kiszka 	       (basic.type == 0 || basic.type == 6) &&
77469c8d31cSJan Kiszka 	       basic.reserved1 == 0 && basic.reserved2 == 0);
77569c8d31cSJan Kiszka 
77669c8d31cSJan Kiszka 	val = rdmsr(MSR_IA32_VMX_MISC);
77769c8d31cSJan Kiszka 	report("MSR_IA32_VMX_MISC",
77869c8d31cSJan Kiszka 	       (!(ctrl_cpu_rev[1].clr & CPU_URG) || val & (1ul << 5)) &&
77969c8d31cSJan Kiszka 	       ((val >> 16) & 0x1ff) <= 256 &&
78069c8d31cSJan Kiszka 	       (val & 0xc0007e00) == 0);
78169c8d31cSJan Kiszka 
78269c8d31cSJan Kiszka 	for (n = 0; n < ARRAY_SIZE(vmx_ctl_msr); n++) {
78369c8d31cSJan Kiszka 		ctrl.val = rdmsr(vmx_ctl_msr[n].index);
78469c8d31cSJan Kiszka 		default1 = vmx_ctl_msr[n].default1;
78569c8d31cSJan Kiszka 		ok = (ctrl.set & default1) == default1;
78669c8d31cSJan Kiszka 		ok = ok && (ctrl.set & ~ctrl.clr) == 0;
78769c8d31cSJan Kiszka 		if (ok && basic.ctrl) {
78869c8d31cSJan Kiszka 			true_ctrl.val = rdmsr(vmx_ctl_msr[n].true_index);
78969c8d31cSJan Kiszka 			ok = ctrl.clr == true_ctrl.clr;
79069c8d31cSJan Kiszka 			ok = ok && ctrl.set == (true_ctrl.set | default1);
79169c8d31cSJan Kiszka 		}
79269c8d31cSJan Kiszka 		report(vmx_ctl_msr[n].name, ok);
79369c8d31cSJan Kiszka 	}
79469c8d31cSJan Kiszka 
79569c8d31cSJan Kiszka 	fixed0 = rdmsr(MSR_IA32_VMX_CR0_FIXED0);
79669c8d31cSJan Kiszka 	fixed1 = rdmsr(MSR_IA32_VMX_CR0_FIXED1);
79769c8d31cSJan Kiszka 	report("MSR_IA32_VMX_IA32_VMX_CR0_FIXED0/1",
79869c8d31cSJan Kiszka 	       ((fixed0 ^ fixed1) & ~fixed1) == 0);
79969c8d31cSJan Kiszka 
80069c8d31cSJan Kiszka 	fixed0 = rdmsr(MSR_IA32_VMX_CR4_FIXED0);
80169c8d31cSJan Kiszka 	fixed1 = rdmsr(MSR_IA32_VMX_CR4_FIXED1);
80269c8d31cSJan Kiszka 	report("MSR_IA32_VMX_IA32_VMX_CR4_FIXED0/1",
80369c8d31cSJan Kiszka 	       ((fixed0 ^ fixed1) & ~fixed1) == 0);
80469c8d31cSJan Kiszka 
80569c8d31cSJan Kiszka 	val = rdmsr(MSR_IA32_VMX_VMCS_ENUM);
80669c8d31cSJan Kiszka 	report("MSR_IA32_VMX_VMCS_ENUM",
80769c8d31cSJan Kiszka 	       (val & 0x3e) >= 0x2a &&
80869c8d31cSJan Kiszka 	       (val & 0xfffffffffffffc01Ull) == 0);
80969c8d31cSJan Kiszka 
81069c8d31cSJan Kiszka 	val = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP);
81169c8d31cSJan Kiszka 	report("MSR_IA32_VMX_EPT_VPID_CAP",
81269c8d31cSJan Kiszka 	       (val & 0xfffff07ef9eebebeUll) == 0);
81369c8d31cSJan Kiszka }
81469c8d31cSJan Kiszka 
8159d7eaa29SArthur Chunqi Li /* This function can only be called in guest */
8169d7eaa29SArthur Chunqi Li static void __attribute__((__used__)) hypercall(u32 hypercall_no)
8179d7eaa29SArthur Chunqi Li {
8189d7eaa29SArthur Chunqi Li 	u64 val = 0;
8199d7eaa29SArthur Chunqi Li 	val = (hypercall_no & HYPERCALL_MASK) | HYPERCALL_BIT;
8209d7eaa29SArthur Chunqi Li 	hypercall_field = val;
8219d7eaa29SArthur Chunqi Li 	asm volatile("vmcall\n\t");
8229d7eaa29SArthur Chunqi Li }
8239d7eaa29SArthur Chunqi Li 
8249d7eaa29SArthur Chunqi Li static bool is_hypercall()
8259d7eaa29SArthur Chunqi Li {
8269d7eaa29SArthur Chunqi Li 	ulong reason, hyper_bit;
8279d7eaa29SArthur Chunqi Li 
8289d7eaa29SArthur Chunqi Li 	reason = vmcs_read(EXI_REASON) & 0xff;
8299d7eaa29SArthur Chunqi Li 	hyper_bit = hypercall_field & HYPERCALL_BIT;
8309d7eaa29SArthur Chunqi Li 	if (reason == VMX_VMCALL && hyper_bit)
8319d7eaa29SArthur Chunqi Li 		return true;
8329d7eaa29SArthur Chunqi Li 	return false;
8339d7eaa29SArthur Chunqi Li }
8349d7eaa29SArthur Chunqi Li 
8359d7eaa29SArthur Chunqi Li static int handle_hypercall()
8369d7eaa29SArthur Chunqi Li {
8379d7eaa29SArthur Chunqi Li 	ulong hypercall_no;
8389d7eaa29SArthur Chunqi Li 
8399d7eaa29SArthur Chunqi Li 	hypercall_no = hypercall_field & HYPERCALL_MASK;
8409d7eaa29SArthur Chunqi Li 	hypercall_field = 0;
8419d7eaa29SArthur Chunqi Li 	switch (hypercall_no) {
8429d7eaa29SArthur Chunqi Li 	case HYPERCALL_VMEXIT:
8439d7eaa29SArthur Chunqi Li 		return VMX_TEST_VMEXIT;
8449d7eaa29SArthur Chunqi Li 	default:
8459d7eaa29SArthur Chunqi Li 		printf("ERROR : Invalid hypercall number : %d\n", hypercall_no);
8469d7eaa29SArthur Chunqi Li 	}
8479d7eaa29SArthur Chunqi Li 	return VMX_TEST_EXIT;
8489d7eaa29SArthur Chunqi Li }
8499d7eaa29SArthur Chunqi Li 
8509d7eaa29SArthur Chunqi Li static int exit_handler()
8519d7eaa29SArthur Chunqi Li {
8529d7eaa29SArthur Chunqi Li 	int ret;
8539d7eaa29SArthur Chunqi Li 
8549d7eaa29SArthur Chunqi Li 	current->exits++;
8551d9284d0SArthur Chunqi Li 	regs.rflags = vmcs_read(GUEST_RFLAGS);
8569d7eaa29SArthur Chunqi Li 	if (is_hypercall())
8579d7eaa29SArthur Chunqi Li 		ret = handle_hypercall();
8589d7eaa29SArthur Chunqi Li 	else
8599d7eaa29SArthur Chunqi Li 		ret = current->exit_handler();
8601d9284d0SArthur Chunqi Li 	vmcs_write(GUEST_RFLAGS, regs.rflags);
8619d7eaa29SArthur Chunqi Li 	switch (ret) {
8629d7eaa29SArthur Chunqi Li 	case VMX_TEST_VMEXIT:
8639d7eaa29SArthur Chunqi Li 	case VMX_TEST_RESUME:
8649d7eaa29SArthur Chunqi Li 		return ret;
8659d7eaa29SArthur Chunqi Li 	case VMX_TEST_EXIT:
8669d7eaa29SArthur Chunqi Li 		break;
8679d7eaa29SArthur Chunqi Li 	default:
8689d7eaa29SArthur Chunqi Li 		printf("ERROR : Invalid exit_handler return val %d.\n"
8699d7eaa29SArthur Chunqi Li 			, ret);
8709d7eaa29SArthur Chunqi Li 	}
8719d7eaa29SArthur Chunqi Li 	print_vmexit_info();
8729d7eaa29SArthur Chunqi Li 	exit(-1);
8739d7eaa29SArthur Chunqi Li 	return 0;
8749d7eaa29SArthur Chunqi Li }
8759d7eaa29SArthur Chunqi Li 
8769d7eaa29SArthur Chunqi Li static int vmx_run()
8779d7eaa29SArthur Chunqi Li {
8789d7eaa29SArthur Chunqi Li 	u32 ret = 0, fail = 0;
8799d7eaa29SArthur Chunqi Li 
8809d7eaa29SArthur Chunqi Li 	while (1) {
8819d7eaa29SArthur Chunqi Li 		asm volatile (
8829d7eaa29SArthur Chunqi Li 			"mov %%rsp, %%rsi\n\t"
8839d7eaa29SArthur Chunqi Li 			"mov %2, %%rdi\n\t"
8849d7eaa29SArthur Chunqi Li 			"vmwrite %%rsi, %%rdi\n\t"
8859d7eaa29SArthur Chunqi Li 
8869d7eaa29SArthur Chunqi Li 			LOAD_GPR_C
8879d7eaa29SArthur Chunqi Li 			"cmpl $0, %1\n\t"
8889d7eaa29SArthur Chunqi Li 			"jne 1f\n\t"
8899d7eaa29SArthur Chunqi Li 			LOAD_RFLAGS
8909d7eaa29SArthur Chunqi Li 			"vmlaunch\n\t"
8919d7eaa29SArthur Chunqi Li 			"jmp 2f\n\t"
8929d7eaa29SArthur Chunqi Li 			"1: "
8939d7eaa29SArthur Chunqi Li 			"vmresume\n\t"
8949d7eaa29SArthur Chunqi Li 			"2: "
8959d7eaa29SArthur Chunqi Li 			"setbe %0\n\t"
8969d7eaa29SArthur Chunqi Li 			"vmx_return:\n\t"
8979d7eaa29SArthur Chunqi Li 			SAVE_GPR_C
8989d7eaa29SArthur Chunqi Li 			SAVE_RFLAGS
8999d7eaa29SArthur Chunqi Li 			: "=m"(fail)
9009d7eaa29SArthur Chunqi Li 			: "m"(launched), "i"(HOST_RSP)
9019d7eaa29SArthur Chunqi Li 			: "rdi", "rsi", "memory", "cc"
9029d7eaa29SArthur Chunqi Li 
9039d7eaa29SArthur Chunqi Li 		);
9049d7eaa29SArthur Chunqi Li 		if (fail)
9059d7eaa29SArthur Chunqi Li 			ret = launched ? VMX_TEST_RESUME_ERR :
9069d7eaa29SArthur Chunqi Li 				VMX_TEST_LAUNCH_ERR;
9079d7eaa29SArthur Chunqi Li 		else {
9089d7eaa29SArthur Chunqi Li 			launched = 1;
9099d7eaa29SArthur Chunqi Li 			ret = exit_handler();
9109d7eaa29SArthur Chunqi Li 		}
9119d7eaa29SArthur Chunqi Li 		if (ret != VMX_TEST_RESUME)
9129d7eaa29SArthur Chunqi Li 			break;
9139d7eaa29SArthur Chunqi Li 	}
9149d7eaa29SArthur Chunqi Li 	launched = 0;
9159d7eaa29SArthur Chunqi Li 	switch (ret) {
9169d7eaa29SArthur Chunqi Li 	case VMX_TEST_VMEXIT:
9179d7eaa29SArthur Chunqi Li 		return 0;
9189d7eaa29SArthur Chunqi Li 	case VMX_TEST_LAUNCH_ERR:
9199d7eaa29SArthur Chunqi Li 		printf("%s : vmlaunch failed.\n", __func__);
9201d9284d0SArthur Chunqi Li 		if ((!(host_rflags & X86_EFLAGS_CF) && !(host_rflags & X86_EFLAGS_ZF))
9211d9284d0SArthur Chunqi Li 			|| ((host_rflags & X86_EFLAGS_CF) && (host_rflags & X86_EFLAGS_ZF)))
9229d7eaa29SArthur Chunqi Li 			printf("\tvmlaunch set wrong flags\n");
9239d7eaa29SArthur Chunqi Li 		report("test vmlaunch", 0);
9249d7eaa29SArthur Chunqi Li 		break;
9259d7eaa29SArthur Chunqi Li 	case VMX_TEST_RESUME_ERR:
9269d7eaa29SArthur Chunqi Li 		printf("%s : vmresume failed.\n", __func__);
9271d9284d0SArthur Chunqi Li 		if ((!(host_rflags & X86_EFLAGS_CF) && !(host_rflags & X86_EFLAGS_ZF))
9281d9284d0SArthur Chunqi Li 			|| ((host_rflags & X86_EFLAGS_CF) && (host_rflags & X86_EFLAGS_ZF)))
9299d7eaa29SArthur Chunqi Li 			printf("\tvmresume set wrong flags\n");
9309d7eaa29SArthur Chunqi Li 		report("test vmresume", 0);
9319d7eaa29SArthur Chunqi Li 		break;
9329d7eaa29SArthur Chunqi Li 	default:
9339d7eaa29SArthur Chunqi Li 		printf("%s : unhandled ret from exit_handler, ret=%d.\n", __func__, ret);
9349d7eaa29SArthur Chunqi Li 		break;
9359d7eaa29SArthur Chunqi Li 	}
9369d7eaa29SArthur Chunqi Li 	return 1;
9379d7eaa29SArthur Chunqi Li }
9389d7eaa29SArthur Chunqi Li 
9399d7eaa29SArthur Chunqi Li static int test_run(struct vmx_test *test)
9409d7eaa29SArthur Chunqi Li {
9419d7eaa29SArthur Chunqi Li 	if (test->name == NULL)
9429d7eaa29SArthur Chunqi Li 		test->name = "(no name)";
9439d7eaa29SArthur Chunqi Li 	if (vmx_on()) {
9449d7eaa29SArthur Chunqi Li 		printf("%s : vmxon failed.\n", __func__);
9459d7eaa29SArthur Chunqi Li 		return 1;
9469d7eaa29SArthur Chunqi Li 	}
9479d7eaa29SArthur Chunqi Li 	init_vmcs(&(test->vmcs));
9489d7eaa29SArthur Chunqi Li 	/* Directly call test->init is ok here, init_vmcs has done
9499d7eaa29SArthur Chunqi Li 	   vmcs init, vmclear and vmptrld*/
950c592c151SJan Kiszka 	if (test->init && test->init(test->vmcs) != VMX_TEST_START)
951a0e30e71SPaolo Bonzini 		goto out;
9529d7eaa29SArthur Chunqi Li 	test->exits = 0;
9539d7eaa29SArthur Chunqi Li 	current = test;
9549d7eaa29SArthur Chunqi Li 	regs = test->guest_regs;
9559d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_RFLAGS, regs.rflags | 0x2);
9569d7eaa29SArthur Chunqi Li 	launched = 0;
9579d7eaa29SArthur Chunqi Li 	printf("\nTest suite: %s\n", test->name);
9589d7eaa29SArthur Chunqi Li 	vmx_run();
959a0e30e71SPaolo Bonzini out:
9609d7eaa29SArthur Chunqi Li 	if (vmx_off()) {
9619d7eaa29SArthur Chunqi Li 		printf("%s : vmxoff failed.\n", __func__);
9629d7eaa29SArthur Chunqi Li 		return 1;
9639d7eaa29SArthur Chunqi Li 	}
9649d7eaa29SArthur Chunqi Li 	return 0;
9659d7eaa29SArthur Chunqi Li }
9669d7eaa29SArthur Chunqi Li 
9673ee34093SArthur Chunqi Li extern struct vmx_test vmx_tests[];
9689d7eaa29SArthur Chunqi Li 
9699d7eaa29SArthur Chunqi Li int main(void)
9709d7eaa29SArthur Chunqi Li {
9713ee34093SArthur Chunqi Li 	int i = 0;
9729d7eaa29SArthur Chunqi Li 
9739d7eaa29SArthur Chunqi Li 	setup_vm();
9749d7eaa29SArthur Chunqi Li 	setup_idt();
9753ee34093SArthur Chunqi Li 	hypercall_field = 0;
9769d7eaa29SArthur Chunqi Li 
9773b127446SJan Kiszka 	if (!(cpuid(1).c & (1 << 5))) {
9783b127446SJan Kiszka 		printf("WARNING: vmx not supported, add '-cpu host'\n");
9799d7eaa29SArthur Chunqi Li 		goto exit;
9809d7eaa29SArthur Chunqi Li 	}
9819d7eaa29SArthur Chunqi Li 	init_vmx();
9823b127446SJan Kiszka 	if (test_vmx_feature_control() != 0)
9833b127446SJan Kiszka 		goto exit;
9849d7eaa29SArthur Chunqi Li 	/* Set basic test ctxt the same as "null" */
9859d7eaa29SArthur Chunqi Li 	current = &vmx_tests[0];
9869d7eaa29SArthur Chunqi Li 	if (test_vmxon() != 0)
9879d7eaa29SArthur Chunqi Li 		goto exit;
9889d7eaa29SArthur Chunqi Li 	test_vmptrld();
9899d7eaa29SArthur Chunqi Li 	test_vmclear();
9909d7eaa29SArthur Chunqi Li 	test_vmptrst();
9919d7eaa29SArthur Chunqi Li 	init_vmcs(&vmcs_root);
9929d7eaa29SArthur Chunqi Li 	if (vmx_run()) {
9939d7eaa29SArthur Chunqi Li 		report("test vmlaunch", 0);
9949d7eaa29SArthur Chunqi Li 		goto exit;
9959d7eaa29SArthur Chunqi Li 	}
9969d7eaa29SArthur Chunqi Li 	test_vmxoff();
99769c8d31cSJan Kiszka 	test_vmx_caps();
9989d7eaa29SArthur Chunqi Li 
9993ee34093SArthur Chunqi Li 	while (vmx_tests[++i].name != NULL)
10009d7eaa29SArthur Chunqi Li 		if (test_run(&vmx_tests[i]))
10019d7eaa29SArthur Chunqi Li 			goto exit;
10029d7eaa29SArthur Chunqi Li 
10039d7eaa29SArthur Chunqi Li exit:
1004f3cdd159SJan Kiszka 	return report_summary();
10059d7eaa29SArthur Chunqi Li }
1006