xref: /kvm-unit-tests/x86/vmx.c (revision c04259ff9acf147c6e58bf69bb9c61e9b7dd697e)
17ada359dSArthur Chunqi Li /*
27ada359dSArthur Chunqi Li  * x86/vmx.c : Framework for testing nested virtualization
37ada359dSArthur Chunqi Li  *	This is a framework to test nested VMX for KVM, which
47ada359dSArthur Chunqi Li  * 	started as a project of GSoC 2013. All test cases should
57ada359dSArthur Chunqi Li  *	be located in x86/vmx_tests.c and framework related
67ada359dSArthur Chunqi Li  *	functions should be in this file.
77ada359dSArthur Chunqi Li  *
87ada359dSArthur Chunqi Li  * How to write test cases?
97ada359dSArthur Chunqi Li  *	Add callbacks of test suite in variant "vmx_tests". You can
107ada359dSArthur Chunqi Li  *	write:
117ada359dSArthur Chunqi Li  *		1. init function used for initializing test suite
127ada359dSArthur Chunqi Li  *		2. main function for codes running in L2 guest,
137ada359dSArthur Chunqi Li  *		3. exit_handler to handle vmexit of L2 to L1
147ada359dSArthur Chunqi Li  *		4. syscall handler to handle L2 syscall vmexit
157ada359dSArthur Chunqi Li  *		5. vmenter fail handler to handle direct failure of vmenter
167ada359dSArthur Chunqi Li  *		6. guest_regs is loaded when vmenter and saved when
177ada359dSArthur Chunqi Li  *			vmexit, you can read and set it in exit_handler
187ada359dSArthur Chunqi Li  *	If no special function is needed for a test suite, use
197ada359dSArthur Chunqi Li  *	coressponding basic_* functions as callback. More handlers
207ada359dSArthur Chunqi Li  *	can be added to "vmx_tests", see details of "struct vmx_test"
217ada359dSArthur Chunqi Li  *	and function test_run().
227ada359dSArthur Chunqi Li  *
237ada359dSArthur Chunqi Li  * Currently, vmx test framework only set up one VCPU and one
247ada359dSArthur Chunqi Li  * concurrent guest test environment with same paging for L2 and
257ada359dSArthur Chunqi Li  * L1. For usage of EPT, only 1:1 mapped paging is used from VFN
267ada359dSArthur Chunqi Li  * to PFN.
277ada359dSArthur Chunqi Li  *
287ada359dSArthur Chunqi Li  * Author : Arthur Chunqi Li <yzt356@gmail.com>
297ada359dSArthur Chunqi Li  */
307ada359dSArthur Chunqi Li 
319d7eaa29SArthur Chunqi Li #include "libcflat.h"
329d7eaa29SArthur Chunqi Li #include "processor.h"
339d7eaa29SArthur Chunqi Li #include "vm.h"
349d7eaa29SArthur Chunqi Li #include "desc.h"
359d7eaa29SArthur Chunqi Li #include "vmx.h"
369d7eaa29SArthur Chunqi Li #include "msr.h"
379d7eaa29SArthur Chunqi Li #include "smp.h"
389d7eaa29SArthur Chunqi Li 
39ce21d809SBandan Das u64 *vmxon_region;
409d7eaa29SArthur Chunqi Li struct vmcs *vmcs_root;
419d7eaa29SArthur Chunqi Li u32 vpid_cnt;
429d7eaa29SArthur Chunqi Li void *guest_stack, *guest_syscall_stack;
439d7eaa29SArthur Chunqi Li u32 ctrl_pin, ctrl_enter, ctrl_exit, ctrl_cpu[2];
449d7eaa29SArthur Chunqi Li struct regs regs;
459d7eaa29SArthur Chunqi Li struct vmx_test *current;
463ee34093SArthur Chunqi Li u64 hypercall_field;
479d7eaa29SArthur Chunqi Li bool launched;
48*c04259ffSDavid Matlack static int matched;
499d7eaa29SArthur Chunqi Li 
503ee34093SArthur Chunqi Li union vmx_basic basic;
515f18e779SJan Kiszka union vmx_ctrl_msr ctrl_pin_rev;
525f18e779SJan Kiszka union vmx_ctrl_msr ctrl_cpu_rev[2];
535f18e779SJan Kiszka union vmx_ctrl_msr ctrl_exit_rev;
545f18e779SJan Kiszka union vmx_ctrl_msr ctrl_enter_rev;
553ee34093SArthur Chunqi Li union vmx_ept_vpid  ept_vpid;
563ee34093SArthur Chunqi Li 
57337166aaSJan Kiszka extern struct descriptor_table_ptr gdt64_desc;
58337166aaSJan Kiszka extern struct descriptor_table_ptr idt_descr;
59337166aaSJan Kiszka extern struct descriptor_table_ptr tss_descr;
609d7eaa29SArthur Chunqi Li extern void *vmx_return;
619d7eaa29SArthur Chunqi Li extern void *entry_sysenter;
629d7eaa29SArthur Chunqi Li extern void *guest_entry;
639d7eaa29SArthur Chunqi Li 
64ffb1a9e0SJan Kiszka static volatile u32 stage;
65ffb1a9e0SJan Kiszka 
66ffb1a9e0SJan Kiszka void vmx_set_test_stage(u32 s)
67ffb1a9e0SJan Kiszka {
68ffb1a9e0SJan Kiszka 	barrier();
69ffb1a9e0SJan Kiszka 	stage = s;
70ffb1a9e0SJan Kiszka 	barrier();
71ffb1a9e0SJan Kiszka }
72ffb1a9e0SJan Kiszka 
73ffb1a9e0SJan Kiszka u32 vmx_get_test_stage(void)
74ffb1a9e0SJan Kiszka {
75ffb1a9e0SJan Kiszka 	u32 s;
76ffb1a9e0SJan Kiszka 
77ffb1a9e0SJan Kiszka 	barrier();
78ffb1a9e0SJan Kiszka 	s = stage;
79ffb1a9e0SJan Kiszka 	barrier();
80ffb1a9e0SJan Kiszka 	return s;
81ffb1a9e0SJan Kiszka }
82ffb1a9e0SJan Kiszka 
83ffb1a9e0SJan Kiszka void vmx_inc_test_stage(void)
84ffb1a9e0SJan Kiszka {
85ffb1a9e0SJan Kiszka 	barrier();
86ffb1a9e0SJan Kiszka 	stage++;
87ffb1a9e0SJan Kiszka 	barrier();
88ffb1a9e0SJan Kiszka }
89ffb1a9e0SJan Kiszka 
909d7eaa29SArthur Chunqi Li static int make_vmcs_current(struct vmcs *vmcs)
919d7eaa29SArthur Chunqi Li {
929d7eaa29SArthur Chunqi Li 	bool ret;
93a739f560SBandan Das 	u64 rflags = read_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF;
949d7eaa29SArthur Chunqi Li 
95a739f560SBandan Das 	asm volatile ("push %1; popf; vmptrld %2; setbe %0"
96a739f560SBandan Das 		      : "=q" (ret) : "q" (rflags), "m" (vmcs) : "cc");
979d7eaa29SArthur Chunqi Li 	return ret;
989d7eaa29SArthur Chunqi Li }
999d7eaa29SArthur Chunqi Li 
1009d7eaa29SArthur Chunqi Li /* entry_sysenter */
1019d7eaa29SArthur Chunqi Li asm(
1029d7eaa29SArthur Chunqi Li 	".align	4, 0x90\n\t"
1039d7eaa29SArthur Chunqi Li 	".globl	entry_sysenter\n\t"
1049d7eaa29SArthur Chunqi Li 	"entry_sysenter:\n\t"
1059d7eaa29SArthur Chunqi Li 	SAVE_GPR
1069d7eaa29SArthur Chunqi Li 	"	and	$0xf, %rax\n\t"
1079d7eaa29SArthur Chunqi Li 	"	mov	%rax, %rdi\n\t"
1089d7eaa29SArthur Chunqi Li 	"	call	syscall_handler\n\t"
1099d7eaa29SArthur Chunqi Li 	LOAD_GPR
1109d7eaa29SArthur Chunqi Li 	"	vmresume\n\t"
1119d7eaa29SArthur Chunqi Li );
1129d7eaa29SArthur Chunqi Li 
1139d7eaa29SArthur Chunqi Li static void __attribute__((__used__)) syscall_handler(u64 syscall_no)
1149d7eaa29SArthur Chunqi Li {
115d5315e3dSJan Kiszka 	if (current->syscall_handler)
1169d7eaa29SArthur Chunqi Li 		current->syscall_handler(syscall_no);
1179d7eaa29SArthur Chunqi Li }
1189d7eaa29SArthur Chunqi Li 
1199d7eaa29SArthur Chunqi Li static inline int vmx_on()
1209d7eaa29SArthur Chunqi Li {
1219d7eaa29SArthur Chunqi Li 	bool ret;
122a739f560SBandan Das 	u64 rflags = read_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF;
123a739f560SBandan Das 	asm volatile ("push %1; popf; vmxon %2; setbe %0\n\t"
124a739f560SBandan Das 		      : "=q" (ret) : "q" (rflags), "m" (vmxon_region) : "cc");
1259d7eaa29SArthur Chunqi Li 	return ret;
1269d7eaa29SArthur Chunqi Li }
1279d7eaa29SArthur Chunqi Li 
1289d7eaa29SArthur Chunqi Li static inline int vmx_off()
1299d7eaa29SArthur Chunqi Li {
1309d7eaa29SArthur Chunqi Li 	bool ret;
131a739f560SBandan Das 	u64 rflags = read_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF;
132a739f560SBandan Das 
133a739f560SBandan Das 	asm volatile("push %1; popf; vmxoff; setbe %0\n\t"
134a739f560SBandan Das 		     : "=q"(ret) : "q" (rflags) : "cc");
1359d7eaa29SArthur Chunqi Li 	return ret;
1369d7eaa29SArthur Chunqi Li }
1379d7eaa29SArthur Chunqi Li 
1383ee34093SArthur Chunqi Li void print_vmexit_info()
1399d7eaa29SArthur Chunqi Li {
1409d7eaa29SArthur Chunqi Li 	u64 guest_rip, guest_rsp;
1419d7eaa29SArthur Chunqi Li 	ulong reason = vmcs_read(EXI_REASON) & 0xff;
1429d7eaa29SArthur Chunqi Li 	ulong exit_qual = vmcs_read(EXI_QUALIFICATION);
1439d7eaa29SArthur Chunqi Li 	guest_rip = vmcs_read(GUEST_RIP);
1449d7eaa29SArthur Chunqi Li 	guest_rsp = vmcs_read(GUEST_RSP);
1459d7eaa29SArthur Chunqi Li 	printf("VMEXIT info:\n");
146b006d7ebSAndrew Jones 	printf("\tvmexit reason = %ld\n", reason);
147b006d7ebSAndrew Jones 	printf("\texit qualification = 0x%lx\n", exit_qual);
148b006d7ebSAndrew Jones 	printf("\tBit 31 of reason = %lx\n", (vmcs_read(EXI_REASON) >> 31) & 1);
149b006d7ebSAndrew Jones 	printf("\tguest_rip = 0x%lx\n", guest_rip);
150b006d7ebSAndrew Jones 	printf("\tRAX=0x%lx    RBX=0x%lx    RCX=0x%lx    RDX=0x%lx\n",
1519d7eaa29SArthur Chunqi Li 		regs.rax, regs.rbx, regs.rcx, regs.rdx);
152b006d7ebSAndrew Jones 	printf("\tRSP=0x%lx    RBP=0x%lx    RSI=0x%lx    RDI=0x%lx\n",
1539d7eaa29SArthur Chunqi Li 		guest_rsp, regs.rbp, regs.rsi, regs.rdi);
154b006d7ebSAndrew Jones 	printf("\tR8 =0x%lx    R9 =0x%lx    R10=0x%lx    R11=0x%lx\n",
1559d7eaa29SArthur Chunqi Li 		regs.r8, regs.r9, regs.r10, regs.r11);
156b006d7ebSAndrew Jones 	printf("\tR12=0x%lx    R13=0x%lx    R14=0x%lx    R15=0x%lx\n",
1579d7eaa29SArthur Chunqi Li 		regs.r12, regs.r13, regs.r14, regs.r15);
1589d7eaa29SArthur Chunqi Li }
1599d7eaa29SArthur Chunqi Li 
1603b50efe3SPeter Feiner void
1613b50efe3SPeter Feiner print_vmentry_failure_info(struct vmentry_failure *failure) {
1623b50efe3SPeter Feiner 	if (failure->early) {
1633b50efe3SPeter Feiner 		printf("Early %s failure: ", failure->instr);
1643b50efe3SPeter Feiner 		switch (failure->flags & VMX_ENTRY_FLAGS) {
165ce154ba8SPaolo Bonzini 		case X86_EFLAGS_CF:
1663b50efe3SPeter Feiner 			printf("current-VMCS pointer is not valid.\n");
1673b50efe3SPeter Feiner 			break;
168ce154ba8SPaolo Bonzini 		case X86_EFLAGS_ZF:
1693b50efe3SPeter Feiner 			printf("error number is %ld. See Intel 30.4.\n",
1703b50efe3SPeter Feiner 			       vmcs_read(VMX_INST_ERROR));
1713b50efe3SPeter Feiner 			break;
1723b50efe3SPeter Feiner 		default:
1733b50efe3SPeter Feiner 			printf("unexpected flags %lx!\n", failure->flags);
1743b50efe3SPeter Feiner 		}
1753b50efe3SPeter Feiner 	} else {
1763b50efe3SPeter Feiner 		u64 reason = vmcs_read(EXI_REASON);
1773b50efe3SPeter Feiner 		u64 qual = vmcs_read(EXI_QUALIFICATION);
1783b50efe3SPeter Feiner 
1793b50efe3SPeter Feiner 		printf("Non-early %s failure (reason=0x%lx, qual=0x%lx): ",
1803b50efe3SPeter Feiner 			failure->instr, reason, qual);
1813b50efe3SPeter Feiner 
1823b50efe3SPeter Feiner 		switch (reason & 0xff) {
1833b50efe3SPeter Feiner 		case VMX_FAIL_STATE:
1843b50efe3SPeter Feiner 			printf("invalid guest state\n");
1853b50efe3SPeter Feiner 			break;
1863b50efe3SPeter Feiner 		case VMX_FAIL_MSR:
1873b50efe3SPeter Feiner 			printf("MSR loading\n");
1883b50efe3SPeter Feiner 			break;
1893b50efe3SPeter Feiner 		case VMX_FAIL_MCHECK:
1903b50efe3SPeter Feiner 			printf("machine-check event\n");
1913b50efe3SPeter Feiner 			break;
1923b50efe3SPeter Feiner 		default:
1933b50efe3SPeter Feiner 			printf("unexpected basic exit reason %ld\n",
1943b50efe3SPeter Feiner 			       reason & 0xff);
1953b50efe3SPeter Feiner 		}
1963b50efe3SPeter Feiner 
1973b50efe3SPeter Feiner 		if (!(reason & VMX_ENTRY_FAILURE))
1983b50efe3SPeter Feiner 			printf("\tVMX_ENTRY_FAILURE BIT NOT SET!\n");
1993b50efe3SPeter Feiner 
2003b50efe3SPeter Feiner 		if (reason & 0x7fff0000)
2013b50efe3SPeter Feiner 			printf("\tRESERVED BITS SET!\n");
2023b50efe3SPeter Feiner 	}
2033b50efe3SPeter Feiner }
2043b50efe3SPeter Feiner 
2053b50efe3SPeter Feiner 
2069d7eaa29SArthur Chunqi Li static void test_vmclear(void)
2079d7eaa29SArthur Chunqi Li {
208daeec979SBandan Das 	struct vmcs *tmp_root;
209e2cf1c9dSEduardo Habkost 	int width = cpuid_maxphyaddr();
210daeec979SBandan Das 
211daeec979SBandan Das 	/*
212daeec979SBandan Das 	 * Note- The tests below do not necessarily have a
213daeec979SBandan Das 	 * valid VMCS, but that's ok since the invalid vmcs
214daeec979SBandan Das 	 * is only used for a specific test and is discarded
215daeec979SBandan Das 	 * without touching its contents
216daeec979SBandan Das 	 */
217daeec979SBandan Das 
218daeec979SBandan Das 	/* Unaligned page access */
219daeec979SBandan Das 	tmp_root = (struct vmcs *)((intptr_t)vmcs_root + 1);
220daeec979SBandan Das 	report("test vmclear with unaligned vmcs",
221daeec979SBandan Das 	       vmcs_clear(tmp_root) == 1);
222daeec979SBandan Das 
223daeec979SBandan Das 	/* gpa bits beyond physical address width are set*/
224daeec979SBandan Das 	tmp_root = (struct vmcs *)((intptr_t)vmcs_root |
225daeec979SBandan Das 				   ((u64)1 << (width+1)));
226daeec979SBandan Das 	report("test vmclear with vmcs address bits set beyond physical address width",
227daeec979SBandan Das 	       vmcs_clear(tmp_root) == 1);
228daeec979SBandan Das 
229daeec979SBandan Das 	/* Pass VMXON region */
230daeec979SBandan Das 	tmp_root = (struct vmcs *)vmxon_region;
231daeec979SBandan Das 	report("test vmclear with vmxon region",
232daeec979SBandan Das 	       vmcs_clear(tmp_root) == 1);
233daeec979SBandan Das 
234daeec979SBandan Das 	/* Valid VMCS */
235daeec979SBandan Das 	report("test vmclear with valid vmcs region", vmcs_clear(vmcs_root) == 0);
236daeec979SBandan Das 
2379d7eaa29SArthur Chunqi Li }
2389d7eaa29SArthur Chunqi Li 
2399d7eaa29SArthur Chunqi Li static void test_vmxoff(void)
2409d7eaa29SArthur Chunqi Li {
2419d7eaa29SArthur Chunqi Li 	int ret;
2429d7eaa29SArthur Chunqi Li 
2439d7eaa29SArthur Chunqi Li 	ret = vmx_off();
2449d7eaa29SArthur Chunqi Li 	report("test vmxoff", !ret);
2459d7eaa29SArthur Chunqi Li }
2469d7eaa29SArthur Chunqi Li 
2479d7eaa29SArthur Chunqi Li static void __attribute__((__used__)) guest_main(void)
2489d7eaa29SArthur Chunqi Li {
2499d7eaa29SArthur Chunqi Li 	current->guest_main();
2509d7eaa29SArthur Chunqi Li }
2519d7eaa29SArthur Chunqi Li 
2529d7eaa29SArthur Chunqi Li /* guest_entry */
2539d7eaa29SArthur Chunqi Li asm(
2549d7eaa29SArthur Chunqi Li 	".align	4, 0x90\n\t"
2559d7eaa29SArthur Chunqi Li 	".globl	entry_guest\n\t"
2569d7eaa29SArthur Chunqi Li 	"guest_entry:\n\t"
2579d7eaa29SArthur Chunqi Li 	"	call guest_main\n\t"
2589d7eaa29SArthur Chunqi Li 	"	mov $1, %edi\n\t"
2599d7eaa29SArthur Chunqi Li 	"	call hypercall\n\t"
2609d7eaa29SArthur Chunqi Li );
2619d7eaa29SArthur Chunqi Li 
2626884af61SArthur Chunqi Li /* EPT paging structure related functions */
26369c531c8SPeter Feiner /* split_large_ept_entry: Split a 2M/1G large page into 512 smaller PTEs.
26469c531c8SPeter Feiner 		@ptep : large page table entry to split
26569c531c8SPeter Feiner 		@level : level of ptep (2 or 3)
26669c531c8SPeter Feiner  */
26769c531c8SPeter Feiner static void split_large_ept_entry(unsigned long *ptep, int level)
26869c531c8SPeter Feiner {
26969c531c8SPeter Feiner 	unsigned long *new_pt;
27069c531c8SPeter Feiner 	unsigned long gpa;
27169c531c8SPeter Feiner 	unsigned long pte;
27269c531c8SPeter Feiner 	unsigned long prototype;
27369c531c8SPeter Feiner 	int i;
27469c531c8SPeter Feiner 
27569c531c8SPeter Feiner 	pte = *ptep;
27669c531c8SPeter Feiner 	assert(pte & EPT_PRESENT);
27769c531c8SPeter Feiner 	assert(pte & EPT_LARGE_PAGE);
27869c531c8SPeter Feiner 	assert(level == 2 || level == 3);
27969c531c8SPeter Feiner 
28069c531c8SPeter Feiner 	new_pt = alloc_page();
28169c531c8SPeter Feiner 	assert(new_pt);
28269c531c8SPeter Feiner 	memset(new_pt, 0, PAGE_SIZE);
28369c531c8SPeter Feiner 
28469c531c8SPeter Feiner 	prototype = pte & ~EPT_ADDR_MASK;
28569c531c8SPeter Feiner 	if (level == 2)
28669c531c8SPeter Feiner 		prototype &= ~EPT_LARGE_PAGE;
28769c531c8SPeter Feiner 
28869c531c8SPeter Feiner 	gpa = pte & EPT_ADDR_MASK;
28969c531c8SPeter Feiner 	for (i = 0; i < EPT_PGDIR_ENTRIES; i++) {
29069c531c8SPeter Feiner 		new_pt[i] = prototype | gpa;
29169c531c8SPeter Feiner 		gpa += 1ul << EPT_LEVEL_SHIFT(level - 1);
29269c531c8SPeter Feiner 	}
29369c531c8SPeter Feiner 
29469c531c8SPeter Feiner 	pte &= ~EPT_LARGE_PAGE;
29569c531c8SPeter Feiner 	pte &= ~EPT_ADDR_MASK;
29669c531c8SPeter Feiner 	pte |= virt_to_phys(new_pt);
29769c531c8SPeter Feiner 
29869c531c8SPeter Feiner 	*ptep = pte;
29969c531c8SPeter Feiner }
30069c531c8SPeter Feiner 
3016884af61SArthur Chunqi Li /* install_ept_entry : Install a page to a given level in EPT
3026884af61SArthur Chunqi Li 		@pml4 : addr of pml4 table
3036884af61SArthur Chunqi Li 		@pte_level : level of PTE to set
3046884af61SArthur Chunqi Li 		@guest_addr : physical address of guest
3056884af61SArthur Chunqi Li 		@pte : pte value to set
3066884af61SArthur Chunqi Li 		@pt_page : address of page table, NULL for a new page
3076884af61SArthur Chunqi Li  */
3086884af61SArthur Chunqi Li void install_ept_entry(unsigned long *pml4,
3096884af61SArthur Chunqi Li 		int pte_level,
3106884af61SArthur Chunqi Li 		unsigned long guest_addr,
3116884af61SArthur Chunqi Li 		unsigned long pte,
3126884af61SArthur Chunqi Li 		unsigned long *pt_page)
3136884af61SArthur Chunqi Li {
3146884af61SArthur Chunqi Li 	int level;
3156884af61SArthur Chunqi Li 	unsigned long *pt = pml4;
3166884af61SArthur Chunqi Li 	unsigned offset;
3176884af61SArthur Chunqi Li 
3186884af61SArthur Chunqi Li 	for (level = EPT_PAGE_LEVEL; level > pte_level; --level) {
319a969e087SPeter Feiner 		offset = (guest_addr >> EPT_LEVEL_SHIFT(level))
3206884af61SArthur Chunqi Li 				& EPT_PGDIR_MASK;
3216884af61SArthur Chunqi Li 		if (!(pt[offset] & (EPT_PRESENT))) {
3226884af61SArthur Chunqi Li 			unsigned long *new_pt = pt_page;
3236884af61SArthur Chunqi Li 			if (!new_pt)
3246884af61SArthur Chunqi Li 				new_pt = alloc_page();
3256884af61SArthur Chunqi Li 			else
3266884af61SArthur Chunqi Li 				pt_page = 0;
3276884af61SArthur Chunqi Li 			memset(new_pt, 0, PAGE_SIZE);
3286884af61SArthur Chunqi Li 			pt[offset] = virt_to_phys(new_pt)
3296884af61SArthur Chunqi Li 					| EPT_RA | EPT_WA | EPT_EA;
33069c531c8SPeter Feiner 		} else if (pt[offset] & EPT_LARGE_PAGE)
33169c531c8SPeter Feiner 			split_large_ept_entry(&pt[offset], level);
33200b5c590SPeter Feiner 		pt = phys_to_virt(pt[offset] & EPT_ADDR_MASK);
3336884af61SArthur Chunqi Li 	}
334a969e087SPeter Feiner 	offset = (guest_addr >> EPT_LEVEL_SHIFT(level)) & EPT_PGDIR_MASK;
3356884af61SArthur Chunqi Li 	pt[offset] = pte;
3366884af61SArthur Chunqi Li }
3376884af61SArthur Chunqi Li 
3386884af61SArthur Chunqi Li /* Map a page, @perm is the permission of the page */
3396884af61SArthur Chunqi Li void install_ept(unsigned long *pml4,
3406884af61SArthur Chunqi Li 		unsigned long phys,
3416884af61SArthur Chunqi Li 		unsigned long guest_addr,
3426884af61SArthur Chunqi Li 		u64 perm)
3436884af61SArthur Chunqi Li {
3446884af61SArthur Chunqi Li 	install_ept_entry(pml4, 1, guest_addr, (phys & PAGE_MASK) | perm, 0);
3456884af61SArthur Chunqi Li }
3466884af61SArthur Chunqi Li 
3476884af61SArthur Chunqi Li /* Map a 1G-size page */
3486884af61SArthur Chunqi Li void install_1g_ept(unsigned long *pml4,
3496884af61SArthur Chunqi Li 		unsigned long phys,
3506884af61SArthur Chunqi Li 		unsigned long guest_addr,
3516884af61SArthur Chunqi Li 		u64 perm)
3526884af61SArthur Chunqi Li {
3536884af61SArthur Chunqi Li 	install_ept_entry(pml4, 3, guest_addr,
3546884af61SArthur Chunqi Li 			(phys & PAGE_MASK) | perm | EPT_LARGE_PAGE, 0);
3556884af61SArthur Chunqi Li }
3566884af61SArthur Chunqi Li 
3576884af61SArthur Chunqi Li /* Map a 2M-size page */
3586884af61SArthur Chunqi Li void install_2m_ept(unsigned long *pml4,
3596884af61SArthur Chunqi Li 		unsigned long phys,
3606884af61SArthur Chunqi Li 		unsigned long guest_addr,
3616884af61SArthur Chunqi Li 		u64 perm)
3626884af61SArthur Chunqi Li {
3636884af61SArthur Chunqi Li 	install_ept_entry(pml4, 2, guest_addr,
3646884af61SArthur Chunqi Li 			(phys & PAGE_MASK) | perm | EPT_LARGE_PAGE, 0);
3656884af61SArthur Chunqi Li }
3666884af61SArthur Chunqi Li 
3676884af61SArthur Chunqi Li /* setup_ept_range : Setup a range of 1:1 mapped page to EPT paging structure.
3686884af61SArthur Chunqi Li 		@start : start address of guest page
3696884af61SArthur Chunqi Li 		@len : length of address to be mapped
3706884af61SArthur Chunqi Li 		@map_1g : whether 1G page map is used
3716884af61SArthur Chunqi Li 		@map_2m : whether 2M page map is used
3726884af61SArthur Chunqi Li 		@perm : permission for every page
3736884af61SArthur Chunqi Li  */
374b947e241SJan Kiszka void setup_ept_range(unsigned long *pml4, unsigned long start,
3756884af61SArthur Chunqi Li 		     unsigned long len, int map_1g, int map_2m, u64 perm)
3766884af61SArthur Chunqi Li {
3776884af61SArthur Chunqi Li 	u64 phys = start;
3786884af61SArthur Chunqi Li 	u64 max = (u64)len + (u64)start;
3796884af61SArthur Chunqi Li 
3806884af61SArthur Chunqi Li 	if (map_1g) {
3816884af61SArthur Chunqi Li 		while (phys + PAGE_SIZE_1G <= max) {
3826884af61SArthur Chunqi Li 			install_1g_ept(pml4, phys, phys, perm);
3836884af61SArthur Chunqi Li 			phys += PAGE_SIZE_1G;
3846884af61SArthur Chunqi Li 		}
3856884af61SArthur Chunqi Li 	}
3866884af61SArthur Chunqi Li 	if (map_2m) {
3876884af61SArthur Chunqi Li 		while (phys + PAGE_SIZE_2M <= max) {
3886884af61SArthur Chunqi Li 			install_2m_ept(pml4, phys, phys, perm);
3896884af61SArthur Chunqi Li 			phys += PAGE_SIZE_2M;
3906884af61SArthur Chunqi Li 		}
3916884af61SArthur Chunqi Li 	}
3926884af61SArthur Chunqi Li 	while (phys + PAGE_SIZE <= max) {
3936884af61SArthur Chunqi Li 		install_ept(pml4, phys, phys, perm);
3946884af61SArthur Chunqi Li 		phys += PAGE_SIZE;
3956884af61SArthur Chunqi Li 	}
3966884af61SArthur Chunqi Li }
3976884af61SArthur Chunqi Li 
3986884af61SArthur Chunqi Li /* get_ept_pte : Get the PTE of a given level in EPT,
3996884af61SArthur Chunqi Li     @level == 1 means get the latest level*/
4006884af61SArthur Chunqi Li unsigned long get_ept_pte(unsigned long *pml4,
4016884af61SArthur Chunqi Li 		unsigned long guest_addr, int level)
4026884af61SArthur Chunqi Li {
4036884af61SArthur Chunqi Li 	int l;
4046884af61SArthur Chunqi Li 	unsigned long *pt = pml4, pte;
4056884af61SArthur Chunqi Li 	unsigned offset;
4066884af61SArthur Chunqi Li 
4072ca6f1f3SPaolo Bonzini 	if (level < 1 || level > 3)
4082ca6f1f3SPaolo Bonzini 		return -1;
4092ca6f1f3SPaolo Bonzini 	for (l = EPT_PAGE_LEVEL; ; --l) {
410a969e087SPeter Feiner 		offset = (guest_addr >> EPT_LEVEL_SHIFT(l)) & EPT_PGDIR_MASK;
4116884af61SArthur Chunqi Li 		pte = pt[offset];
4126884af61SArthur Chunqi Li 		if (!(pte & (EPT_PRESENT)))
4136884af61SArthur Chunqi Li 			return 0;
4146884af61SArthur Chunqi Li 		if (l == level)
4152ca6f1f3SPaolo Bonzini 			break;
4166884af61SArthur Chunqi Li 		if (l < 4 && (pte & EPT_LARGE_PAGE))
4176884af61SArthur Chunqi Li 			return pte;
41800b5c590SPeter Feiner 		pt = (unsigned long *)(pte & EPT_ADDR_MASK);
4196884af61SArthur Chunqi Li 	}
420a969e087SPeter Feiner 	offset = (guest_addr >> EPT_LEVEL_SHIFT(l)) & EPT_PGDIR_MASK;
4216884af61SArthur Chunqi Li 	pte = pt[offset];
4226884af61SArthur Chunqi Li 	return pte;
4236884af61SArthur Chunqi Li }
4246884af61SArthur Chunqi Li 
425521820dbSPaolo Bonzini static void clear_ept_ad_pte(unsigned long *pml4, unsigned long guest_addr)
426521820dbSPaolo Bonzini {
427521820dbSPaolo Bonzini 	int l;
428521820dbSPaolo Bonzini 	unsigned long *pt = pml4;
429521820dbSPaolo Bonzini 	u64 pte;
430521820dbSPaolo Bonzini 	unsigned offset;
431521820dbSPaolo Bonzini 
432521820dbSPaolo Bonzini 	for (l = EPT_PAGE_LEVEL; ; --l) {
433521820dbSPaolo Bonzini 		offset = (guest_addr >> EPT_LEVEL_SHIFT(l)) & EPT_PGDIR_MASK;
434521820dbSPaolo Bonzini 		pt[offset] &= ~(EPT_ACCESS_FLAG|EPT_DIRTY_FLAG);
435521820dbSPaolo Bonzini 		pte = pt[offset];
436521820dbSPaolo Bonzini 		if (l == 1 || (l < 4 && (pte & EPT_LARGE_PAGE)))
437521820dbSPaolo Bonzini 			break;
438521820dbSPaolo Bonzini 		pt = (unsigned long *)(pte & EPT_ADDR_MASK);
439521820dbSPaolo Bonzini 	}
440521820dbSPaolo Bonzini }
441521820dbSPaolo Bonzini 
442521820dbSPaolo Bonzini /* clear_ept_ad : Clear EPT A/D bits for the page table walk and the
443521820dbSPaolo Bonzini    final GPA of a guest address.  */
444521820dbSPaolo Bonzini void clear_ept_ad(unsigned long *pml4, u64 guest_cr3,
445521820dbSPaolo Bonzini 		  unsigned long guest_addr)
446521820dbSPaolo Bonzini {
447521820dbSPaolo Bonzini 	int l;
448521820dbSPaolo Bonzini 	unsigned long *pt = (unsigned long *)guest_cr3, gpa;
449521820dbSPaolo Bonzini 	u64 pte, offset_in_page;
450521820dbSPaolo Bonzini 	unsigned offset;
451521820dbSPaolo Bonzini 
452521820dbSPaolo Bonzini 	for (l = EPT_PAGE_LEVEL; ; --l) {
453521820dbSPaolo Bonzini 		offset = (guest_addr >> EPT_LEVEL_SHIFT(l)) & EPT_PGDIR_MASK;
454521820dbSPaolo Bonzini 
455521820dbSPaolo Bonzini 		clear_ept_ad_pte(pml4, (u64) &pt[offset]);
456521820dbSPaolo Bonzini 		pte = pt[offset];
457521820dbSPaolo Bonzini 		if (l == 1 || (l < 4 && (pte & PT_PAGE_SIZE_MASK)))
458521820dbSPaolo Bonzini 			break;
459521820dbSPaolo Bonzini 		if (!(pte & PT_PRESENT_MASK))
460521820dbSPaolo Bonzini 			return;
461521820dbSPaolo Bonzini 		pt = (unsigned long *)(pte & PT_ADDR_MASK);
462521820dbSPaolo Bonzini 	}
463521820dbSPaolo Bonzini 
464521820dbSPaolo Bonzini 	offset = (guest_addr >> EPT_LEVEL_SHIFT(l)) & EPT_PGDIR_MASK;
465521820dbSPaolo Bonzini 	offset_in_page = guest_addr & ((1 << EPT_LEVEL_SHIFT(l)) - 1);
466521820dbSPaolo Bonzini 	gpa = (pt[offset] & PT_ADDR_MASK) | (guest_addr & offset_in_page);
467521820dbSPaolo Bonzini 	clear_ept_ad_pte(pml4, gpa);
468521820dbSPaolo Bonzini }
469521820dbSPaolo Bonzini 
470521820dbSPaolo Bonzini /* check_ept_ad : Check the content of EPT A/D bits for the page table
471521820dbSPaolo Bonzini    walk and the final GPA of a guest address.  */
472521820dbSPaolo Bonzini void check_ept_ad(unsigned long *pml4, u64 guest_cr3,
473521820dbSPaolo Bonzini 		  unsigned long guest_addr, int expected_gpa_ad,
474521820dbSPaolo Bonzini 		  int expected_pt_ad)
475521820dbSPaolo Bonzini {
476521820dbSPaolo Bonzini 	int l;
477521820dbSPaolo Bonzini 	unsigned long *pt = (unsigned long *)guest_cr3, gpa;
478521820dbSPaolo Bonzini 	u64 ept_pte, pte, offset_in_page;
479521820dbSPaolo Bonzini 	unsigned offset;
480521820dbSPaolo Bonzini 	bool bad_pt_ad = false;
481521820dbSPaolo Bonzini 
482521820dbSPaolo Bonzini 	for (l = EPT_PAGE_LEVEL; ; --l) {
483521820dbSPaolo Bonzini 		offset = (guest_addr >> EPT_LEVEL_SHIFT(l)) & EPT_PGDIR_MASK;
484521820dbSPaolo Bonzini 
485521820dbSPaolo Bonzini 		ept_pte = get_ept_pte(pml4, (u64) &pt[offset], 1);
486521820dbSPaolo Bonzini 		if (ept_pte == 0)
487521820dbSPaolo Bonzini 			return;
488521820dbSPaolo Bonzini 
489521820dbSPaolo Bonzini 		if (!bad_pt_ad) {
490521820dbSPaolo Bonzini 			bad_pt_ad |= (ept_pte & (EPT_ACCESS_FLAG|EPT_DIRTY_FLAG)) != expected_pt_ad;
491521820dbSPaolo Bonzini 			if (bad_pt_ad)
492521820dbSPaolo Bonzini 				report("EPT - guest level %d page table A=%d/D=%d",
493521820dbSPaolo Bonzini 				       false, l,
494521820dbSPaolo Bonzini 				       !!(expected_pt_ad & EPT_ACCESS_FLAG),
495521820dbSPaolo Bonzini 				       !!(expected_pt_ad & EPT_DIRTY_FLAG));
496521820dbSPaolo Bonzini 		}
497521820dbSPaolo Bonzini 
498521820dbSPaolo Bonzini 		pte = pt[offset];
499521820dbSPaolo Bonzini 		if (l == 1 || (l < 4 && (pte & PT_PAGE_SIZE_MASK)))
500521820dbSPaolo Bonzini 			break;
501521820dbSPaolo Bonzini 		if (!(pte & PT_PRESENT_MASK))
502521820dbSPaolo Bonzini 			return;
503521820dbSPaolo Bonzini 		pt = (unsigned long *)(pte & PT_ADDR_MASK);
504521820dbSPaolo Bonzini 	}
505521820dbSPaolo Bonzini 
506521820dbSPaolo Bonzini 	if (!bad_pt_ad)
507521820dbSPaolo Bonzini 		report("EPT - guest page table structures A=%d/D=%d",
508521820dbSPaolo Bonzini 		       true,
509521820dbSPaolo Bonzini 		       !!(expected_pt_ad & EPT_ACCESS_FLAG),
510521820dbSPaolo Bonzini 		       !!(expected_pt_ad & EPT_DIRTY_FLAG));
511521820dbSPaolo Bonzini 
512521820dbSPaolo Bonzini 	offset = (guest_addr >> EPT_LEVEL_SHIFT(l)) & EPT_PGDIR_MASK;
513521820dbSPaolo Bonzini 	offset_in_page = guest_addr & ((1 << EPT_LEVEL_SHIFT(l)) - 1);
514521820dbSPaolo Bonzini 	gpa = (pt[offset] & PT_ADDR_MASK) | (guest_addr & offset_in_page);
515521820dbSPaolo Bonzini 
516521820dbSPaolo Bonzini 	ept_pte = get_ept_pte(pml4, gpa, 1);
517521820dbSPaolo Bonzini 	report("EPT - guest physical address A=%d/D=%d",
518521820dbSPaolo Bonzini 	       (ept_pte & (EPT_ACCESS_FLAG|EPT_DIRTY_FLAG)) == expected_gpa_ad,
519521820dbSPaolo Bonzini 	       !!(expected_gpa_ad & EPT_ACCESS_FLAG),
520521820dbSPaolo Bonzini 	       !!(expected_gpa_ad & EPT_DIRTY_FLAG));
521521820dbSPaolo Bonzini }
522521820dbSPaolo Bonzini 
523521820dbSPaolo Bonzini 
5242f888fccSBandan Das void ept_sync(int type, u64 eptp)
5252f888fccSBandan Das {
5262f888fccSBandan Das 	switch (type) {
5272f888fccSBandan Das 	case INVEPT_SINGLE:
5282f888fccSBandan Das 		if (ept_vpid.val & EPT_CAP_INVEPT_SINGLE) {
5292f888fccSBandan Das 			invept(INVEPT_SINGLE, eptp);
5302f888fccSBandan Das 			break;
5312f888fccSBandan Das 		}
5322f888fccSBandan Das 		/* else fall through */
5332f888fccSBandan Das 	case INVEPT_GLOBAL:
5342f888fccSBandan Das 		if (ept_vpid.val & EPT_CAP_INVEPT_ALL) {
5352f888fccSBandan Das 			invept(INVEPT_GLOBAL, eptp);
5362f888fccSBandan Das 			break;
5372f888fccSBandan Das 		}
5382f888fccSBandan Das 		/* else fall through */
5392f888fccSBandan Das 	default:
5402f888fccSBandan Das 		printf("WARNING: invept is not supported!\n");
5412f888fccSBandan Das 	}
5422f888fccSBandan Das }
5432f888fccSBandan Das 
5446884af61SArthur Chunqi Li int set_ept_pte(unsigned long *pml4, unsigned long guest_addr,
5456884af61SArthur Chunqi Li 		int level, u64 pte_val)
5466884af61SArthur Chunqi Li {
5476884af61SArthur Chunqi Li 	int l;
5486884af61SArthur Chunqi Li 	unsigned long *pt = pml4;
5496884af61SArthur Chunqi Li 	unsigned offset;
5506884af61SArthur Chunqi Li 
5516884af61SArthur Chunqi Li 	if (level < 1 || level > 3)
5526884af61SArthur Chunqi Li 		return -1;
5532ca6f1f3SPaolo Bonzini 	for (l = EPT_PAGE_LEVEL; ; --l) {
554a969e087SPeter Feiner 		offset = (guest_addr >> EPT_LEVEL_SHIFT(l)) & EPT_PGDIR_MASK;
5552ca6f1f3SPaolo Bonzini 		if (l == level)
5562ca6f1f3SPaolo Bonzini 			break;
5576884af61SArthur Chunqi Li 		if (!(pt[offset] & (EPT_PRESENT)))
5586884af61SArthur Chunqi Li 			return -1;
55900b5c590SPeter Feiner 		pt = (unsigned long *)(pt[offset] & EPT_ADDR_MASK);
5606884af61SArthur Chunqi Li 	}
561a969e087SPeter Feiner 	offset = (guest_addr >> EPT_LEVEL_SHIFT(l)) & EPT_PGDIR_MASK;
5626884af61SArthur Chunqi Li 	pt[offset] = pte_val;
5636884af61SArthur Chunqi Li 	return 0;
5646884af61SArthur Chunqi Li }
5656884af61SArthur Chunqi Li 
566b093c6ceSWanpeng Li void vpid_sync(int type, u16 vpid)
567b093c6ceSWanpeng Li {
568b093c6ceSWanpeng Li 	switch(type) {
569b093c6ceSWanpeng Li 	case INVVPID_SINGLE:
570b093c6ceSWanpeng Li 		if (ept_vpid.val & VPID_CAP_INVVPID_SINGLE) {
571b093c6ceSWanpeng Li 			invvpid(INVVPID_SINGLE, vpid, 0);
572b093c6ceSWanpeng Li 			break;
573b093c6ceSWanpeng Li 		}
574b093c6ceSWanpeng Li 	case INVVPID_ALL:
575b093c6ceSWanpeng Li 		if (ept_vpid.val & VPID_CAP_INVVPID_ALL) {
576b093c6ceSWanpeng Li 			invvpid(INVVPID_ALL, vpid, 0);
577b093c6ceSWanpeng Li 			break;
578b093c6ceSWanpeng Li 		}
579b093c6ceSWanpeng Li 	default:
580b093c6ceSWanpeng Li 		printf("WARNING: invvpid is not supported\n");
581b093c6ceSWanpeng Li 	}
582b093c6ceSWanpeng Li }
5836884af61SArthur Chunqi Li 
5849d7eaa29SArthur Chunqi Li static void init_vmcs_ctrl(void)
5859d7eaa29SArthur Chunqi Li {
5869d7eaa29SArthur Chunqi Li 	/* 26.2 CHECKS ON VMX CONTROLS AND HOST-STATE AREA */
5879d7eaa29SArthur Chunqi Li 	/* 26.2.1.1 */
5889d7eaa29SArthur Chunqi Li 	vmcs_write(PIN_CONTROLS, ctrl_pin);
5899d7eaa29SArthur Chunqi Li 	/* Disable VMEXIT of IO instruction */
5909d7eaa29SArthur Chunqi Li 	vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu[0]);
5919d7eaa29SArthur Chunqi Li 	if (ctrl_cpu_rev[0].set & CPU_SECONDARY) {
5926884af61SArthur Chunqi Li 		ctrl_cpu[1] = (ctrl_cpu[1] | ctrl_cpu_rev[1].set) &
5936884af61SArthur Chunqi Li 			ctrl_cpu_rev[1].clr;
5949d7eaa29SArthur Chunqi Li 		vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu[1]);
5959d7eaa29SArthur Chunqi Li 	}
5969d7eaa29SArthur Chunqi Li 	vmcs_write(CR3_TARGET_COUNT, 0);
5979d7eaa29SArthur Chunqi Li 	vmcs_write(VPID, ++vpid_cnt);
5989d7eaa29SArthur Chunqi Li }
5999d7eaa29SArthur Chunqi Li 
6009d7eaa29SArthur Chunqi Li static void init_vmcs_host(void)
6019d7eaa29SArthur Chunqi Li {
6029d7eaa29SArthur Chunqi Li 	/* 26.2 CHECKS ON VMX CONTROLS AND HOST-STATE AREA */
6039d7eaa29SArthur Chunqi Li 	/* 26.2.1.2 */
6049d7eaa29SArthur Chunqi Li 	vmcs_write(HOST_EFER, rdmsr(MSR_EFER));
6059d7eaa29SArthur Chunqi Li 
6069d7eaa29SArthur Chunqi Li 	/* 26.2.1.3 */
6079d7eaa29SArthur Chunqi Li 	vmcs_write(ENT_CONTROLS, ctrl_enter);
6089d7eaa29SArthur Chunqi Li 	vmcs_write(EXI_CONTROLS, ctrl_exit);
6099d7eaa29SArthur Chunqi Li 
6109d7eaa29SArthur Chunqi Li 	/* 26.2.2 */
6119d7eaa29SArthur Chunqi Li 	vmcs_write(HOST_CR0, read_cr0());
6129d7eaa29SArthur Chunqi Li 	vmcs_write(HOST_CR3, read_cr3());
6139d7eaa29SArthur Chunqi Li 	vmcs_write(HOST_CR4, read_cr4());
6149d7eaa29SArthur Chunqi Li 	vmcs_write(HOST_SYSENTER_EIP, (u64)(&entry_sysenter));
61569d8fe0eSPaolo Bonzini 	vmcs_write(HOST_SYSENTER_CS,  KERNEL_CS);
6169d7eaa29SArthur Chunqi Li 
6179d7eaa29SArthur Chunqi Li 	/* 26.2.3 */
61869d8fe0eSPaolo Bonzini 	vmcs_write(HOST_SEL_CS, KERNEL_CS);
61969d8fe0eSPaolo Bonzini 	vmcs_write(HOST_SEL_SS, KERNEL_DS);
62069d8fe0eSPaolo Bonzini 	vmcs_write(HOST_SEL_DS, KERNEL_DS);
62169d8fe0eSPaolo Bonzini 	vmcs_write(HOST_SEL_ES, KERNEL_DS);
62269d8fe0eSPaolo Bonzini 	vmcs_write(HOST_SEL_FS, KERNEL_DS);
62369d8fe0eSPaolo Bonzini 	vmcs_write(HOST_SEL_GS, KERNEL_DS);
62469d8fe0eSPaolo Bonzini 	vmcs_write(HOST_SEL_TR, TSS_MAIN);
625337166aaSJan Kiszka 	vmcs_write(HOST_BASE_TR, tss_descr.base);
626337166aaSJan Kiszka 	vmcs_write(HOST_BASE_GDTR, gdt64_desc.base);
627337166aaSJan Kiszka 	vmcs_write(HOST_BASE_IDTR, idt_descr.base);
6289d7eaa29SArthur Chunqi Li 	vmcs_write(HOST_BASE_FS, 0);
6299d7eaa29SArthur Chunqi Li 	vmcs_write(HOST_BASE_GS, 0);
6309d7eaa29SArthur Chunqi Li 
6319d7eaa29SArthur Chunqi Li 	/* Set other vmcs area */
6329d7eaa29SArthur Chunqi Li 	vmcs_write(PF_ERROR_MASK, 0);
6339d7eaa29SArthur Chunqi Li 	vmcs_write(PF_ERROR_MATCH, 0);
6349d7eaa29SArthur Chunqi Li 	vmcs_write(VMCS_LINK_PTR, ~0ul);
6359d7eaa29SArthur Chunqi Li 	vmcs_write(VMCS_LINK_PTR_HI, ~0ul);
6369d7eaa29SArthur Chunqi Li 	vmcs_write(HOST_RIP, (u64)(&vmx_return));
6379d7eaa29SArthur Chunqi Li }
6389d7eaa29SArthur Chunqi Li 
6399d7eaa29SArthur Chunqi Li static void init_vmcs_guest(void)
6409d7eaa29SArthur Chunqi Li {
6419d7eaa29SArthur Chunqi Li 	/* 26.3 CHECKING AND LOADING GUEST STATE */
6429d7eaa29SArthur Chunqi Li 	ulong guest_cr0, guest_cr4, guest_cr3;
6439d7eaa29SArthur Chunqi Li 	/* 26.3.1.1 */
6449d7eaa29SArthur Chunqi Li 	guest_cr0 = read_cr0();
6459d7eaa29SArthur Chunqi Li 	guest_cr4 = read_cr4();
6469d7eaa29SArthur Chunqi Li 	guest_cr3 = read_cr3();
6479d7eaa29SArthur Chunqi Li 	if (ctrl_enter & ENT_GUEST_64) {
6489d7eaa29SArthur Chunqi Li 		guest_cr0 |= X86_CR0_PG;
6499d7eaa29SArthur Chunqi Li 		guest_cr4 |= X86_CR4_PAE;
6509d7eaa29SArthur Chunqi Li 	}
6519d7eaa29SArthur Chunqi Li 	if ((ctrl_enter & ENT_GUEST_64) == 0)
6529d7eaa29SArthur Chunqi Li 		guest_cr4 &= (~X86_CR4_PCIDE);
6539d7eaa29SArthur Chunqi Li 	if (guest_cr0 & X86_CR0_PG)
6549d7eaa29SArthur Chunqi Li 		guest_cr0 |= X86_CR0_PE;
6559d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_CR0, guest_cr0);
6569d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_CR3, guest_cr3);
6579d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_CR4, guest_cr4);
65869d8fe0eSPaolo Bonzini 	vmcs_write(GUEST_SYSENTER_CS,  KERNEL_CS);
6599d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_SYSENTER_ESP,
6609d7eaa29SArthur Chunqi Li 		(u64)(guest_syscall_stack + PAGE_SIZE - 1));
6619d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_SYSENTER_EIP, (u64)(&entry_sysenter));
6629d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_DR7, 0);
6639d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_EFER, rdmsr(MSR_EFER));
6649d7eaa29SArthur Chunqi Li 
6659d7eaa29SArthur Chunqi Li 	/* 26.3.1.2 */
66669d8fe0eSPaolo Bonzini 	vmcs_write(GUEST_SEL_CS, KERNEL_CS);
66769d8fe0eSPaolo Bonzini 	vmcs_write(GUEST_SEL_SS, KERNEL_DS);
66869d8fe0eSPaolo Bonzini 	vmcs_write(GUEST_SEL_DS, KERNEL_DS);
66969d8fe0eSPaolo Bonzini 	vmcs_write(GUEST_SEL_ES, KERNEL_DS);
67069d8fe0eSPaolo Bonzini 	vmcs_write(GUEST_SEL_FS, KERNEL_DS);
67169d8fe0eSPaolo Bonzini 	vmcs_write(GUEST_SEL_GS, KERNEL_DS);
67269d8fe0eSPaolo Bonzini 	vmcs_write(GUEST_SEL_TR, TSS_MAIN);
6739d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_SEL_LDTR, 0);
6749d7eaa29SArthur Chunqi Li 
6759d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_BASE_CS, 0);
6769d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_BASE_ES, 0);
6779d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_BASE_SS, 0);
6789d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_BASE_DS, 0);
6799d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_BASE_FS, 0);
6809d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_BASE_GS, 0);
681337166aaSJan Kiszka 	vmcs_write(GUEST_BASE_TR, tss_descr.base);
6829d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_BASE_LDTR, 0);
6839d7eaa29SArthur Chunqi Li 
6849d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_LIMIT_CS, 0xFFFFFFFF);
6859d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_LIMIT_DS, 0xFFFFFFFF);
6869d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_LIMIT_ES, 0xFFFFFFFF);
6879d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_LIMIT_SS, 0xFFFFFFFF);
6889d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_LIMIT_FS, 0xFFFFFFFF);
6899d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_LIMIT_GS, 0xFFFFFFFF);
6909d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_LIMIT_LDTR, 0xffff);
691337166aaSJan Kiszka 	vmcs_write(GUEST_LIMIT_TR, tss_descr.limit);
6929d7eaa29SArthur Chunqi Li 
6939d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_AR_CS, 0xa09b);
6949d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_AR_DS, 0xc093);
6959d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_AR_ES, 0xc093);
6969d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_AR_FS, 0xc093);
6979d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_AR_GS, 0xc093);
6989d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_AR_SS, 0xc093);
6999d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_AR_LDTR, 0x82);
7009d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_AR_TR, 0x8b);
7019d7eaa29SArthur Chunqi Li 
7029d7eaa29SArthur Chunqi Li 	/* 26.3.1.3 */
703337166aaSJan Kiszka 	vmcs_write(GUEST_BASE_GDTR, gdt64_desc.base);
704337166aaSJan Kiszka 	vmcs_write(GUEST_BASE_IDTR, idt_descr.base);
705337166aaSJan Kiszka 	vmcs_write(GUEST_LIMIT_GDTR, gdt64_desc.limit);
706337166aaSJan Kiszka 	vmcs_write(GUEST_LIMIT_IDTR, idt_descr.limit);
7079d7eaa29SArthur Chunqi Li 
7089d7eaa29SArthur Chunqi Li 	/* 26.3.1.4 */
7099d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_RIP, (u64)(&guest_entry));
7109d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_RSP, (u64)(guest_stack + PAGE_SIZE - 1));
7119d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_RFLAGS, 0x2);
7129d7eaa29SArthur Chunqi Li 
7139d7eaa29SArthur Chunqi Li 	/* 26.3.1.5 */
71417ba0dd0SJan Kiszka 	vmcs_write(GUEST_ACTV_STATE, ACTV_ACTIVE);
7159d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_INTR_STATE, 0);
7169d7eaa29SArthur Chunqi Li }
7179d7eaa29SArthur Chunqi Li 
7189d7eaa29SArthur Chunqi Li static int init_vmcs(struct vmcs **vmcs)
7199d7eaa29SArthur Chunqi Li {
7209d7eaa29SArthur Chunqi Li 	*vmcs = alloc_page();
7219d7eaa29SArthur Chunqi Li 	memset(*vmcs, 0, PAGE_SIZE);
7229d7eaa29SArthur Chunqi Li 	(*vmcs)->revision_id = basic.revision;
7239d7eaa29SArthur Chunqi Li 	/* vmclear first to init vmcs */
7249d7eaa29SArthur Chunqi Li 	if (vmcs_clear(*vmcs)) {
7259d7eaa29SArthur Chunqi Li 		printf("%s : vmcs_clear error\n", __func__);
7269d7eaa29SArthur Chunqi Li 		return 1;
7279d7eaa29SArthur Chunqi Li 	}
7289d7eaa29SArthur Chunqi Li 
7299d7eaa29SArthur Chunqi Li 	if (make_vmcs_current(*vmcs)) {
7309d7eaa29SArthur Chunqi Li 		printf("%s : make_vmcs_current error\n", __func__);
7319d7eaa29SArthur Chunqi Li 		return 1;
7329d7eaa29SArthur Chunqi Li 	}
7339d7eaa29SArthur Chunqi Li 
7349d7eaa29SArthur Chunqi Li 	/* All settings to pin/exit/enter/cpu
7359d7eaa29SArthur Chunqi Li 	   control fields should be placed here */
7369d7eaa29SArthur Chunqi Li 	ctrl_pin |= PIN_EXTINT | PIN_NMI | PIN_VIRT_NMI;
7379d7eaa29SArthur Chunqi Li 	ctrl_exit = EXI_LOAD_EFER | EXI_HOST_64;
7389d7eaa29SArthur Chunqi Li 	ctrl_enter = (ENT_LOAD_EFER | ENT_GUEST_64);
7399d7eaa29SArthur Chunqi Li 	/* DIsable IO instruction VMEXIT now */
7409d7eaa29SArthur Chunqi Li 	ctrl_cpu[0] &= (~(CPU_IO | CPU_IO_BITMAP));
7419d7eaa29SArthur Chunqi Li 	ctrl_cpu[1] = 0;
7429d7eaa29SArthur Chunqi Li 
7439d7eaa29SArthur Chunqi Li 	ctrl_pin = (ctrl_pin | ctrl_pin_rev.set) & ctrl_pin_rev.clr;
7449d7eaa29SArthur Chunqi Li 	ctrl_enter = (ctrl_enter | ctrl_enter_rev.set) & ctrl_enter_rev.clr;
7459d7eaa29SArthur Chunqi Li 	ctrl_exit = (ctrl_exit | ctrl_exit_rev.set) & ctrl_exit_rev.clr;
7469d7eaa29SArthur Chunqi Li 	ctrl_cpu[0] = (ctrl_cpu[0] | ctrl_cpu_rev[0].set) & ctrl_cpu_rev[0].clr;
7479d7eaa29SArthur Chunqi Li 
7489d7eaa29SArthur Chunqi Li 	init_vmcs_ctrl();
7499d7eaa29SArthur Chunqi Li 	init_vmcs_host();
7509d7eaa29SArthur Chunqi Li 	init_vmcs_guest();
7519d7eaa29SArthur Chunqi Li 	return 0;
7529d7eaa29SArthur Chunqi Li }
7539d7eaa29SArthur Chunqi Li 
7549d7eaa29SArthur Chunqi Li static void init_vmx(void)
7559d7eaa29SArthur Chunqi Li {
7563ee34093SArthur Chunqi Li 	ulong fix_cr0_set, fix_cr0_clr;
7573ee34093SArthur Chunqi Li 	ulong fix_cr4_set, fix_cr4_clr;
7583ee34093SArthur Chunqi Li 
7599d7eaa29SArthur Chunqi Li 	vmxon_region = alloc_page();
7609d7eaa29SArthur Chunqi Li 	memset(vmxon_region, 0, PAGE_SIZE);
7619d7eaa29SArthur Chunqi Li 
7629d7eaa29SArthur Chunqi Li 	fix_cr0_set =  rdmsr(MSR_IA32_VMX_CR0_FIXED0);
7639d7eaa29SArthur Chunqi Li 	fix_cr0_clr =  rdmsr(MSR_IA32_VMX_CR0_FIXED1);
7649d7eaa29SArthur Chunqi Li 	fix_cr4_set =  rdmsr(MSR_IA32_VMX_CR4_FIXED0);
7659d7eaa29SArthur Chunqi Li 	fix_cr4_clr = rdmsr(MSR_IA32_VMX_CR4_FIXED1);
7669d7eaa29SArthur Chunqi Li 	basic.val = rdmsr(MSR_IA32_VMX_BASIC);
7679d7eaa29SArthur Chunqi Li 	ctrl_pin_rev.val = rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_PIN
7689d7eaa29SArthur Chunqi Li 			: MSR_IA32_VMX_PINBASED_CTLS);
7699d7eaa29SArthur Chunqi Li 	ctrl_exit_rev.val = rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_EXIT
7709d7eaa29SArthur Chunqi Li 			: MSR_IA32_VMX_EXIT_CTLS);
7719d7eaa29SArthur Chunqi Li 	ctrl_enter_rev.val = rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_ENTRY
7729d7eaa29SArthur Chunqi Li 			: MSR_IA32_VMX_ENTRY_CTLS);
7739d7eaa29SArthur Chunqi Li 	ctrl_cpu_rev[0].val = rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_PROC
7749d7eaa29SArthur Chunqi Li 			: MSR_IA32_VMX_PROCBASED_CTLS);
7756884af61SArthur Chunqi Li 	if ((ctrl_cpu_rev[0].clr & CPU_SECONDARY) != 0)
7769d7eaa29SArthur Chunqi Li 		ctrl_cpu_rev[1].val = rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2);
7776884af61SArthur Chunqi Li 	else
7786884af61SArthur Chunqi Li 		ctrl_cpu_rev[1].val = 0;
7796884af61SArthur Chunqi Li 	if ((ctrl_cpu_rev[1].clr & (CPU_EPT | CPU_VPID)) != 0)
7809d7eaa29SArthur Chunqi Li 		ept_vpid.val = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP);
7816884af61SArthur Chunqi Li 	else
7826884af61SArthur Chunqi Li 		ept_vpid.val = 0;
7839d7eaa29SArthur Chunqi Li 
7849d7eaa29SArthur Chunqi Li 	write_cr0((read_cr0() & fix_cr0_clr) | fix_cr0_set);
7859d7eaa29SArthur Chunqi Li 	write_cr4((read_cr4() & fix_cr4_clr) | fix_cr4_set | X86_CR4_VMXE);
7869d7eaa29SArthur Chunqi Li 
7879d7eaa29SArthur Chunqi Li 	*vmxon_region = basic.revision;
7889d7eaa29SArthur Chunqi Li 
7899d7eaa29SArthur Chunqi Li 	guest_stack = alloc_page();
7909d7eaa29SArthur Chunqi Li 	memset(guest_stack, 0, PAGE_SIZE);
7919d7eaa29SArthur Chunqi Li 	guest_syscall_stack = alloc_page();
7929d7eaa29SArthur Chunqi Li 	memset(guest_syscall_stack, 0, PAGE_SIZE);
7939d7eaa29SArthur Chunqi Li }
7949d7eaa29SArthur Chunqi Li 
795e3f363c4SJan Kiszka static void do_vmxon_off(void *data)
7969d7eaa29SArthur Chunqi Li {
7973b127446SJan Kiszka 	vmx_on();
7983b127446SJan Kiszka 	vmx_off();
79903f37ef2SPaolo Bonzini }
8003b127446SJan Kiszka 
801e3f363c4SJan Kiszka static void do_write_feature_control(void *data)
8023b127446SJan Kiszka {
8033b127446SJan Kiszka 	wrmsr(MSR_IA32_FEATURE_CONTROL, 0);
80403f37ef2SPaolo Bonzini }
8053b127446SJan Kiszka 
8063b127446SJan Kiszka static int test_vmx_feature_control(void)
8073b127446SJan Kiszka {
8083b127446SJan Kiszka 	u64 ia32_feature_control;
8093b127446SJan Kiszka 	bool vmx_enabled;
8103b127446SJan Kiszka 
8113b127446SJan Kiszka 	ia32_feature_control = rdmsr(MSR_IA32_FEATURE_CONTROL);
8123b127446SJan Kiszka 	vmx_enabled = ((ia32_feature_control & 0x5) == 0x5);
8133b127446SJan Kiszka 	if ((ia32_feature_control & 0x5) == 0x5) {
8143b127446SJan Kiszka 		printf("VMX enabled and locked by BIOS\n");
8153b127446SJan Kiszka 		return 0;
8163b127446SJan Kiszka 	} else if (ia32_feature_control & 0x1) {
8173b127446SJan Kiszka 		printf("ERROR: VMX locked out by BIOS!?\n");
8183b127446SJan Kiszka 		return 1;
8193b127446SJan Kiszka 	}
8203b127446SJan Kiszka 
8213b127446SJan Kiszka 	wrmsr(MSR_IA32_FEATURE_CONTROL, 0);
8223b127446SJan Kiszka 	report("test vmxon with FEATURE_CONTROL cleared",
823e3f363c4SJan Kiszka 	       test_for_exception(GP_VECTOR, &do_vmxon_off, NULL));
8243b127446SJan Kiszka 
8253b127446SJan Kiszka 	wrmsr(MSR_IA32_FEATURE_CONTROL, 0x4);
8263b127446SJan Kiszka 	report("test vmxon without FEATURE_CONTROL lock",
827e3f363c4SJan Kiszka 	       test_for_exception(GP_VECTOR, &do_vmxon_off, NULL));
8283b127446SJan Kiszka 
8293b127446SJan Kiszka 	wrmsr(MSR_IA32_FEATURE_CONTROL, 0x5);
8303b127446SJan Kiszka 	vmx_enabled = ((rdmsr(MSR_IA32_FEATURE_CONTROL) & 0x5) == 0x5);
8313b127446SJan Kiszka 	report("test enable VMX in FEATURE_CONTROL", vmx_enabled);
8323b127446SJan Kiszka 
8333b127446SJan Kiszka 	report("test FEATURE_CONTROL lock bit",
834e3f363c4SJan Kiszka 	       test_for_exception(GP_VECTOR, &do_write_feature_control, NULL));
8353b127446SJan Kiszka 
8363b127446SJan Kiszka 	return !vmx_enabled;
8379d7eaa29SArthur Chunqi Li }
8389d7eaa29SArthur Chunqi Li 
8399d7eaa29SArthur Chunqi Li static int test_vmxon(void)
8409d7eaa29SArthur Chunqi Li {
841ce21d809SBandan Das 	int ret, ret1;
842ce21d809SBandan Das 	u64 *tmp_region = vmxon_region;
843e2cf1c9dSEduardo Habkost 	int width = cpuid_maxphyaddr();
8449d7eaa29SArthur Chunqi Li 
845ce21d809SBandan Das 	/* Unaligned page access */
846ce21d809SBandan Das 	vmxon_region = (u64 *)((intptr_t)vmxon_region + 1);
847ce21d809SBandan Das 	ret1 = vmx_on();
848ce21d809SBandan Das 	report("test vmxon with unaligned vmxon region", ret1);
849ce21d809SBandan Das 	if (!ret1) {
850ce21d809SBandan Das 		ret = 1;
851ce21d809SBandan Das 		goto out;
852ce21d809SBandan Das 	}
853ce21d809SBandan Das 
854ce21d809SBandan Das 	/* gpa bits beyond physical address width are set*/
855ce21d809SBandan Das 	vmxon_region = (u64 *)((intptr_t)tmp_region | ((u64)1 << (width+1)));
856ce21d809SBandan Das 	ret1 = vmx_on();
857ce21d809SBandan Das 	report("test vmxon with bits set beyond physical address width", ret1);
858ce21d809SBandan Das 	if (!ret1) {
859ce21d809SBandan Das 		ret = 1;
860ce21d809SBandan Das 		goto out;
861ce21d809SBandan Das 	}
862ce21d809SBandan Das 
863ce21d809SBandan Das 	/* invalid revision indentifier */
864ce21d809SBandan Das 	vmxon_region = tmp_region;
865ce21d809SBandan Das 	*vmxon_region = 0xba9da9;
866ce21d809SBandan Das 	ret1 = vmx_on();
867ce21d809SBandan Das 	report("test vmxon with invalid revision identifier", ret1);
868ce21d809SBandan Das 	if (!ret1) {
869ce21d809SBandan Das 		ret = 1;
870ce21d809SBandan Das 		goto out;
871ce21d809SBandan Das 	}
872ce21d809SBandan Das 
873ce21d809SBandan Das 	/* and finally a valid region */
874ce21d809SBandan Das 	*vmxon_region = basic.revision;
8759d7eaa29SArthur Chunqi Li 	ret = vmx_on();
876ce21d809SBandan Das 	report("test vmxon with valid vmxon region", !ret);
877ce21d809SBandan Das 
878ce21d809SBandan Das out:
8799d7eaa29SArthur Chunqi Li 	return ret;
8809d7eaa29SArthur Chunqi Li }
8819d7eaa29SArthur Chunqi Li 
8829d7eaa29SArthur Chunqi Li static void test_vmptrld(void)
8839d7eaa29SArthur Chunqi Li {
884daeec979SBandan Das 	struct vmcs *vmcs, *tmp_root;
885e2cf1c9dSEduardo Habkost 	int width = cpuid_maxphyaddr();
8869d7eaa29SArthur Chunqi Li 
8879d7eaa29SArthur Chunqi Li 	vmcs = alloc_page();
8889d7eaa29SArthur Chunqi Li 	vmcs->revision_id = basic.revision;
889daeec979SBandan Das 
890daeec979SBandan Das 	/* Unaligned page access */
891daeec979SBandan Das 	tmp_root = (struct vmcs *)((intptr_t)vmcs + 1);
892daeec979SBandan Das 	report("test vmptrld with unaligned vmcs",
8939c305952SPaolo Bonzini 	       make_vmcs_current(tmp_root) == 1);
894daeec979SBandan Das 
895daeec979SBandan Das 	/* gpa bits beyond physical address width are set*/
896daeec979SBandan Das 	tmp_root = (struct vmcs *)((intptr_t)vmcs |
897daeec979SBandan Das 				   ((u64)1 << (width+1)));
898daeec979SBandan Das 	report("test vmptrld with vmcs address bits set beyond physical address width",
8999c305952SPaolo Bonzini 	       make_vmcs_current(tmp_root) == 1);
900daeec979SBandan Das 
901daeec979SBandan Das 	/* Pass VMXON region */
902daeec979SBandan Das 	tmp_root = (struct vmcs *)vmxon_region;
903daeec979SBandan Das 	report("test vmptrld with vmxon region",
9049c305952SPaolo Bonzini 	       make_vmcs_current(tmp_root) == 1);
905daeec979SBandan Das 
906daeec979SBandan Das 	report("test vmptrld with valid vmcs region", make_vmcs_current(vmcs) == 0);
9079d7eaa29SArthur Chunqi Li }
9089d7eaa29SArthur Chunqi Li 
9099d7eaa29SArthur Chunqi Li static void test_vmptrst(void)
9109d7eaa29SArthur Chunqi Li {
9119d7eaa29SArthur Chunqi Li 	int ret;
9129d7eaa29SArthur Chunqi Li 	struct vmcs *vmcs1, *vmcs2;
9139d7eaa29SArthur Chunqi Li 
9149d7eaa29SArthur Chunqi Li 	vmcs1 = alloc_page();
9159d7eaa29SArthur Chunqi Li 	memset(vmcs1, 0, PAGE_SIZE);
9169d7eaa29SArthur Chunqi Li 	init_vmcs(&vmcs1);
9179d7eaa29SArthur Chunqi Li 	ret = vmcs_save(&vmcs2);
9189d7eaa29SArthur Chunqi Li 	report("test vmptrst", (!ret) && (vmcs1 == vmcs2));
9199d7eaa29SArthur Chunqi Li }
9209d7eaa29SArthur Chunqi Li 
92169c8d31cSJan Kiszka struct vmx_ctl_msr {
92269c8d31cSJan Kiszka 	const char *name;
92369c8d31cSJan Kiszka 	u32 index, true_index;
92469c8d31cSJan Kiszka 	u32 default1;
92569c8d31cSJan Kiszka } vmx_ctl_msr[] = {
92669c8d31cSJan Kiszka 	{ "MSR_IA32_VMX_PINBASED_CTLS", MSR_IA32_VMX_PINBASED_CTLS,
92769c8d31cSJan Kiszka 	  MSR_IA32_VMX_TRUE_PIN, 0x16 },
92869c8d31cSJan Kiszka 	{ "MSR_IA32_VMX_PROCBASED_CTLS", MSR_IA32_VMX_PROCBASED_CTLS,
92969c8d31cSJan Kiszka 	  MSR_IA32_VMX_TRUE_PROC, 0x401e172 },
93069c8d31cSJan Kiszka 	{ "MSR_IA32_VMX_PROCBASED_CTLS2", MSR_IA32_VMX_PROCBASED_CTLS2,
93169c8d31cSJan Kiszka 	  MSR_IA32_VMX_PROCBASED_CTLS2, 0 },
93269c8d31cSJan Kiszka 	{ "MSR_IA32_VMX_EXIT_CTLS", MSR_IA32_VMX_EXIT_CTLS,
93369c8d31cSJan Kiszka 	  MSR_IA32_VMX_TRUE_EXIT, 0x36dff },
93469c8d31cSJan Kiszka 	{ "MSR_IA32_VMX_ENTRY_CTLS", MSR_IA32_VMX_ENTRY_CTLS,
93569c8d31cSJan Kiszka 	  MSR_IA32_VMX_TRUE_ENTRY, 0x11ff },
93669c8d31cSJan Kiszka };
93769c8d31cSJan Kiszka 
93869c8d31cSJan Kiszka static void test_vmx_caps(void)
93969c8d31cSJan Kiszka {
94069c8d31cSJan Kiszka 	u64 val, default1, fixed0, fixed1;
94169c8d31cSJan Kiszka 	union vmx_ctrl_msr ctrl, true_ctrl;
94269c8d31cSJan Kiszka 	unsigned int n;
94369c8d31cSJan Kiszka 	bool ok;
94469c8d31cSJan Kiszka 
94569c8d31cSJan Kiszka 	printf("\nTest suite: VMX capability reporting\n");
94669c8d31cSJan Kiszka 
94769c8d31cSJan Kiszka 	report("MSR_IA32_VMX_BASIC",
94869c8d31cSJan Kiszka 	       (basic.revision & (1ul << 31)) == 0 &&
94969c8d31cSJan Kiszka 	       basic.size > 0 && basic.size <= 4096 &&
95069c8d31cSJan Kiszka 	       (basic.type == 0 || basic.type == 6) &&
95169c8d31cSJan Kiszka 	       basic.reserved1 == 0 && basic.reserved2 == 0);
95269c8d31cSJan Kiszka 
95369c8d31cSJan Kiszka 	val = rdmsr(MSR_IA32_VMX_MISC);
95469c8d31cSJan Kiszka 	report("MSR_IA32_VMX_MISC",
95569c8d31cSJan Kiszka 	       (!(ctrl_cpu_rev[1].clr & CPU_URG) || val & (1ul << 5)) &&
95669c8d31cSJan Kiszka 	       ((val >> 16) & 0x1ff) <= 256 &&
95769c8d31cSJan Kiszka 	       (val & 0xc0007e00) == 0);
95869c8d31cSJan Kiszka 
95969c8d31cSJan Kiszka 	for (n = 0; n < ARRAY_SIZE(vmx_ctl_msr); n++) {
96069c8d31cSJan Kiszka 		ctrl.val = rdmsr(vmx_ctl_msr[n].index);
96169c8d31cSJan Kiszka 		default1 = vmx_ctl_msr[n].default1;
96269c8d31cSJan Kiszka 		ok = (ctrl.set & default1) == default1;
96369c8d31cSJan Kiszka 		ok = ok && (ctrl.set & ~ctrl.clr) == 0;
96469c8d31cSJan Kiszka 		if (ok && basic.ctrl) {
96569c8d31cSJan Kiszka 			true_ctrl.val = rdmsr(vmx_ctl_msr[n].true_index);
96669c8d31cSJan Kiszka 			ok = ctrl.clr == true_ctrl.clr;
96769c8d31cSJan Kiszka 			ok = ok && ctrl.set == (true_ctrl.set | default1);
96869c8d31cSJan Kiszka 		}
96969c8d31cSJan Kiszka 		report(vmx_ctl_msr[n].name, ok);
97069c8d31cSJan Kiszka 	}
97169c8d31cSJan Kiszka 
97269c8d31cSJan Kiszka 	fixed0 = rdmsr(MSR_IA32_VMX_CR0_FIXED0);
97369c8d31cSJan Kiszka 	fixed1 = rdmsr(MSR_IA32_VMX_CR0_FIXED1);
97469c8d31cSJan Kiszka 	report("MSR_IA32_VMX_IA32_VMX_CR0_FIXED0/1",
97569c8d31cSJan Kiszka 	       ((fixed0 ^ fixed1) & ~fixed1) == 0);
97669c8d31cSJan Kiszka 
97769c8d31cSJan Kiszka 	fixed0 = rdmsr(MSR_IA32_VMX_CR4_FIXED0);
97869c8d31cSJan Kiszka 	fixed1 = rdmsr(MSR_IA32_VMX_CR4_FIXED1);
97969c8d31cSJan Kiszka 	report("MSR_IA32_VMX_IA32_VMX_CR4_FIXED0/1",
98069c8d31cSJan Kiszka 	       ((fixed0 ^ fixed1) & ~fixed1) == 0);
98169c8d31cSJan Kiszka 
98269c8d31cSJan Kiszka 	val = rdmsr(MSR_IA32_VMX_VMCS_ENUM);
98369c8d31cSJan Kiszka 	report("MSR_IA32_VMX_VMCS_ENUM",
98469c8d31cSJan Kiszka 	       (val & 0x3e) >= 0x2a &&
98569c8d31cSJan Kiszka 	       (val & 0xfffffffffffffc01Ull) == 0);
98669c8d31cSJan Kiszka 
98769c8d31cSJan Kiszka 	val = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP);
98869c8d31cSJan Kiszka 	report("MSR_IA32_VMX_EPT_VPID_CAP",
989625f52abSPaolo Bonzini 	       (val & 0xfffff07ef98cbebeUll) == 0);
99069c8d31cSJan Kiszka }
99169c8d31cSJan Kiszka 
9929d7eaa29SArthur Chunqi Li /* This function can only be called in guest */
9939d7eaa29SArthur Chunqi Li static void __attribute__((__used__)) hypercall(u32 hypercall_no)
9949d7eaa29SArthur Chunqi Li {
9959d7eaa29SArthur Chunqi Li 	u64 val = 0;
9969d7eaa29SArthur Chunqi Li 	val = (hypercall_no & HYPERCALL_MASK) | HYPERCALL_BIT;
9979d7eaa29SArthur Chunqi Li 	hypercall_field = val;
9989d7eaa29SArthur Chunqi Li 	asm volatile("vmcall\n\t");
9999d7eaa29SArthur Chunqi Li }
10009d7eaa29SArthur Chunqi Li 
10019d7eaa29SArthur Chunqi Li static bool is_hypercall()
10029d7eaa29SArthur Chunqi Li {
10039d7eaa29SArthur Chunqi Li 	ulong reason, hyper_bit;
10049d7eaa29SArthur Chunqi Li 
10059d7eaa29SArthur Chunqi Li 	reason = vmcs_read(EXI_REASON) & 0xff;
10069d7eaa29SArthur Chunqi Li 	hyper_bit = hypercall_field & HYPERCALL_BIT;
10079d7eaa29SArthur Chunqi Li 	if (reason == VMX_VMCALL && hyper_bit)
10089d7eaa29SArthur Chunqi Li 		return true;
10099d7eaa29SArthur Chunqi Li 	return false;
10109d7eaa29SArthur Chunqi Li }
10119d7eaa29SArthur Chunqi Li 
10129d7eaa29SArthur Chunqi Li static int handle_hypercall()
10139d7eaa29SArthur Chunqi Li {
10149d7eaa29SArthur Chunqi Li 	ulong hypercall_no;
10159d7eaa29SArthur Chunqi Li 
10169d7eaa29SArthur Chunqi Li 	hypercall_no = hypercall_field & HYPERCALL_MASK;
10179d7eaa29SArthur Chunqi Li 	hypercall_field = 0;
10189d7eaa29SArthur Chunqi Li 	switch (hypercall_no) {
10199d7eaa29SArthur Chunqi Li 	case HYPERCALL_VMEXIT:
10209d7eaa29SArthur Chunqi Li 		return VMX_TEST_VMEXIT;
10219d7eaa29SArthur Chunqi Li 	default:
1022b006d7ebSAndrew Jones 		printf("ERROR : Invalid hypercall number : %ld\n", hypercall_no);
10239d7eaa29SArthur Chunqi Li 	}
10249d7eaa29SArthur Chunqi Li 	return VMX_TEST_EXIT;
10259d7eaa29SArthur Chunqi Li }
10269d7eaa29SArthur Chunqi Li 
10279d7eaa29SArthur Chunqi Li static int exit_handler()
10289d7eaa29SArthur Chunqi Li {
10299d7eaa29SArthur Chunqi Li 	int ret;
10309d7eaa29SArthur Chunqi Li 
10319d7eaa29SArthur Chunqi Li 	current->exits++;
10321d9284d0SArthur Chunqi Li 	regs.rflags = vmcs_read(GUEST_RFLAGS);
10339d7eaa29SArthur Chunqi Li 	if (is_hypercall())
10349d7eaa29SArthur Chunqi Li 		ret = handle_hypercall();
10359d7eaa29SArthur Chunqi Li 	else
10369d7eaa29SArthur Chunqi Li 		ret = current->exit_handler();
10371d9284d0SArthur Chunqi Li 	vmcs_write(GUEST_RFLAGS, regs.rflags);
10383b50efe3SPeter Feiner 
10399d7eaa29SArthur Chunqi Li 	return ret;
10409d7eaa29SArthur Chunqi Li }
10413b50efe3SPeter Feiner 
10423b50efe3SPeter Feiner /*
10433b50efe3SPeter Feiner  * Called if vmlaunch or vmresume fails.
10443b50efe3SPeter Feiner  *	@early    - failure due to "VMX controls and host-state area" (26.2)
10453b50efe3SPeter Feiner  *	@vmlaunch - was this a vmlaunch or vmresume
10463b50efe3SPeter Feiner  *	@rflags   - host rflags
10473b50efe3SPeter Feiner  */
10483b50efe3SPeter Feiner static int
10493b50efe3SPeter Feiner entry_failure_handler(struct vmentry_failure *failure)
10503b50efe3SPeter Feiner {
10513b50efe3SPeter Feiner 	if (current->entry_failure_handler)
10523b50efe3SPeter Feiner 		return current->entry_failure_handler(failure);
10533b50efe3SPeter Feiner 	else
10543b50efe3SPeter Feiner 		return VMX_TEST_EXIT;
10559d7eaa29SArthur Chunqi Li }
10569d7eaa29SArthur Chunqi Li 
10579d7eaa29SArthur Chunqi Li static int vmx_run()
10589d7eaa29SArthur Chunqi Li {
1059897d8365SPeter Feiner 	unsigned long host_rflags;
10609d7eaa29SArthur Chunqi Li 
10619d7eaa29SArthur Chunqi Li 	while (1) {
10623b50efe3SPeter Feiner 		u32 ret;
10633b50efe3SPeter Feiner 		u32 fail = 0;
10643b50efe3SPeter Feiner 		bool entered;
10653b50efe3SPeter Feiner 		struct vmentry_failure failure;
10664e809db5SPeter Feiner 
10679d7eaa29SArthur Chunqi Li 		asm volatile (
1068897d8365SPeter Feiner 			"mov %[HOST_RSP], %%rdi\n\t"
1069897d8365SPeter Feiner 			"vmwrite %%rsp, %%rdi\n\t"
10709d7eaa29SArthur Chunqi Li 			LOAD_GPR_C
107144417388SPaolo Bonzini 			"cmpb $0, %[launched]\n\t"
10729d7eaa29SArthur Chunqi Li 			"jne 1f\n\t"
10739d7eaa29SArthur Chunqi Li 			"vmlaunch\n\t"
10749d7eaa29SArthur Chunqi Li 			"jmp 2f\n\t"
10759d7eaa29SArthur Chunqi Li 			"1: "
10769d7eaa29SArthur Chunqi Li 			"vmresume\n\t"
10779d7eaa29SArthur Chunqi Li 			"2: "
1078f37cf4e2SPeter Feiner 			SAVE_GPR_C
1079897d8365SPeter Feiner 			"pushf\n\t"
1080897d8365SPeter Feiner 			"pop %%rdi\n\t"
1081897d8365SPeter Feiner 			"mov %%rdi, %[host_rflags]\n\t"
1082897d8365SPeter Feiner 			"movl $1, %[fail]\n\t"
1083f37cf4e2SPeter Feiner 			"jmp 3f\n\t"
10849d7eaa29SArthur Chunqi Li 			"vmx_return:\n\t"
10859d7eaa29SArthur Chunqi Li 			SAVE_GPR_C
1086f37cf4e2SPeter Feiner 			"3: \n\t"
1087897d8365SPeter Feiner 			: [fail]"+m"(fail), [host_rflags]"=m"(host_rflags)
1088897d8365SPeter Feiner 			: [launched]"m"(launched), [HOST_RSP]"i"(HOST_RSP)
1089897d8365SPeter Feiner 			: "rdi", "memory", "cc"
10909d7eaa29SArthur Chunqi Li 
10919d7eaa29SArthur Chunqi Li 		);
10923b50efe3SPeter Feiner 
10933b50efe3SPeter Feiner 		entered = !fail && !(vmcs_read(EXI_REASON) & VMX_ENTRY_FAILURE);
10943b50efe3SPeter Feiner 
10953b50efe3SPeter Feiner 		if (entered) {
10963b50efe3SPeter Feiner 			/*
10973b50efe3SPeter Feiner 			 * VMCS isn't in "launched" state if there's been any
10983b50efe3SPeter Feiner 			 * entry failure (early or otherwise).
10993b50efe3SPeter Feiner 			 */
11009d7eaa29SArthur Chunqi Li 			launched = 1;
11019d7eaa29SArthur Chunqi Li 			ret = exit_handler();
11023b50efe3SPeter Feiner 		} else {
11033b50efe3SPeter Feiner 			failure.flags = host_rflags;
11043b50efe3SPeter Feiner 			failure.vmlaunch = !launched;
11053b50efe3SPeter Feiner 			failure.instr = launched ? "vmresume" : "vmlaunch";
11063b50efe3SPeter Feiner 			failure.early = fail;
11073b50efe3SPeter Feiner 			ret = entry_failure_handler(&failure);
11089d7eaa29SArthur Chunqi Li 		}
11093b50efe3SPeter Feiner 
11109d7eaa29SArthur Chunqi Li 		switch (ret) {
11113b50efe3SPeter Feiner 		case VMX_TEST_RESUME:
11123b50efe3SPeter Feiner 			continue;
11139d7eaa29SArthur Chunqi Li 		case VMX_TEST_VMEXIT:
11149d7eaa29SArthur Chunqi Li 			return 0;
11153b50efe3SPeter Feiner 		case VMX_TEST_EXIT:
11169d7eaa29SArthur Chunqi Li 			break;
11179d7eaa29SArthur Chunqi Li 		default:
11183b50efe3SPeter Feiner 			printf("ERROR : Invalid %s_handler return val %d.\n",
11193b50efe3SPeter Feiner 			       entered ? "exit" : "entry_failure",
11203b50efe3SPeter Feiner 			       ret);
11219d7eaa29SArthur Chunqi Li 			break;
11229d7eaa29SArthur Chunqi Li 		}
11233b50efe3SPeter Feiner 
11243b50efe3SPeter Feiner 		if (entered)
11253b50efe3SPeter Feiner 			print_vmexit_info();
11263b50efe3SPeter Feiner 		else
11273b50efe3SPeter Feiner 			print_vmentry_failure_info(&failure);
11283b50efe3SPeter Feiner 		abort();
11293b50efe3SPeter Feiner 	}
11309d7eaa29SArthur Chunqi Li }
11319d7eaa29SArthur Chunqi Li 
11329d7eaa29SArthur Chunqi Li static int test_run(struct vmx_test *test)
11339d7eaa29SArthur Chunqi Li {
11349d7eaa29SArthur Chunqi Li 	if (test->name == NULL)
11359d7eaa29SArthur Chunqi Li 		test->name = "(no name)";
11369d7eaa29SArthur Chunqi Li 	if (vmx_on()) {
11379d7eaa29SArthur Chunqi Li 		printf("%s : vmxon failed.\n", __func__);
11389d7eaa29SArthur Chunqi Li 		return 1;
11399d7eaa29SArthur Chunqi Li 	}
11409d7eaa29SArthur Chunqi Li 	init_vmcs(&(test->vmcs));
11419d7eaa29SArthur Chunqi Li 	/* Directly call test->init is ok here, init_vmcs has done
11429d7eaa29SArthur Chunqi Li 	   vmcs init, vmclear and vmptrld*/
1143c592c151SJan Kiszka 	if (test->init && test->init(test->vmcs) != VMX_TEST_START)
1144a0e30e71SPaolo Bonzini 		goto out;
11459d7eaa29SArthur Chunqi Li 	test->exits = 0;
11469d7eaa29SArthur Chunqi Li 	current = test;
11479d7eaa29SArthur Chunqi Li 	regs = test->guest_regs;
11489d7eaa29SArthur Chunqi Li 	vmcs_write(GUEST_RFLAGS, regs.rflags | 0x2);
11499d7eaa29SArthur Chunqi Li 	launched = 0;
11509d7eaa29SArthur Chunqi Li 	printf("\nTest suite: %s\n", test->name);
11519d7eaa29SArthur Chunqi Li 	vmx_run();
1152a0e30e71SPaolo Bonzini out:
11539d7eaa29SArthur Chunqi Li 	if (vmx_off()) {
11549d7eaa29SArthur Chunqi Li 		printf("%s : vmxoff failed.\n", __func__);
11559d7eaa29SArthur Chunqi Li 		return 1;
11569d7eaa29SArthur Chunqi Li 	}
11579d7eaa29SArthur Chunqi Li 	return 0;
11589d7eaa29SArthur Chunqi Li }
11599d7eaa29SArthur Chunqi Li 
11603ee34093SArthur Chunqi Li extern struct vmx_test vmx_tests[];
11619d7eaa29SArthur Chunqi Li 
11628029cac7SPeter Feiner /* Match name with wanted allowing underscores in place of spaces. */
11638029cac7SPeter Feiner static bool test_name_wanted(const char *name, const char *wanted)
11648029cac7SPeter Feiner {
11658029cac7SPeter Feiner 	const char *n;
11668029cac7SPeter Feiner 	const char *w;
11678029cac7SPeter Feiner 
11688029cac7SPeter Feiner 	for (n = name, w = wanted; *n != '\0' && *w != '\0'; n++, w++) {
11698029cac7SPeter Feiner 		if (*n != *w && !(*n == ' ' && *w == '_'))
11708029cac7SPeter Feiner 			return false;
11718029cac7SPeter Feiner 	}
11728029cac7SPeter Feiner 	return *n == '\0' && *w == '\0';
11738029cac7SPeter Feiner }
11748029cac7SPeter Feiner 
1175*c04259ffSDavid Matlack static bool test_wanted(const char *name, char *wanted[], int nwanted)
11768029cac7SPeter Feiner {
1177*c04259ffSDavid Matlack 	bool is_wanted = true;
11788029cac7SPeter Feiner 	int i;
11798029cac7SPeter Feiner 
11808029cac7SPeter Feiner 	if (!nwanted)
1181*c04259ffSDavid Matlack 		goto out;
11828029cac7SPeter Feiner 
11838029cac7SPeter Feiner 	for (i = 0; i < nwanted; ++i) {
1184*c04259ffSDavid Matlack 		if (test_name_wanted(name, wanted[i]))
1185*c04259ffSDavid Matlack 			goto out;
11868029cac7SPeter Feiner 	}
1187*c04259ffSDavid Matlack 
1188*c04259ffSDavid Matlack 	is_wanted = false;
1189*c04259ffSDavid Matlack 
1190*c04259ffSDavid Matlack out:
1191*c04259ffSDavid Matlack 	if (is_wanted)
1192*c04259ffSDavid Matlack 		matched++;
1193*c04259ffSDavid Matlack 	return is_wanted;
11948029cac7SPeter Feiner }
11958029cac7SPeter Feiner 
11968029cac7SPeter Feiner int main(int argc, char *argv[])
11979d7eaa29SArthur Chunqi Li {
11983ee34093SArthur Chunqi Li 	int i = 0;
11999d7eaa29SArthur Chunqi Li 
12009d7eaa29SArthur Chunqi Li 	setup_vm();
12019d7eaa29SArthur Chunqi Li 	setup_idt();
12023ee34093SArthur Chunqi Li 	hypercall_field = 0;
12039d7eaa29SArthur Chunqi Li 
1204*c04259ffSDavid Matlack 	argv++;
1205*c04259ffSDavid Matlack 	argc--;
1206*c04259ffSDavid Matlack 
12073b127446SJan Kiszka 	if (!(cpuid(1).c & (1 << 5))) {
12083b127446SJan Kiszka 		printf("WARNING: vmx not supported, add '-cpu host'\n");
12099d7eaa29SArthur Chunqi Li 		goto exit;
12109d7eaa29SArthur Chunqi Li 	}
12119d7eaa29SArthur Chunqi Li 	init_vmx();
1212*c04259ffSDavid Matlack 	if (test_wanted("test_vmx_feature_control", argv, argc)) {
1213*c04259ffSDavid Matlack 		/* Sets MSR_IA32_FEATURE_CONTROL to 0x5 */
12143b127446SJan Kiszka 		if (test_vmx_feature_control() != 0)
12153b127446SJan Kiszka 			goto exit;
1216*c04259ffSDavid Matlack 	} else {
1217*c04259ffSDavid Matlack 		if ((rdmsr(MSR_IA32_FEATURE_CONTROL) & 0x5) != 0x5)
1218*c04259ffSDavid Matlack 			wrmsr(MSR_IA32_FEATURE_CONTROL, 0x5);
1219*c04259ffSDavid Matlack 	}
1220*c04259ffSDavid Matlack 
12219d7eaa29SArthur Chunqi Li 	/* Set basic test ctxt the same as "null" */
12229d7eaa29SArthur Chunqi Li 	current = &vmx_tests[0];
1223*c04259ffSDavid Matlack 
1224*c04259ffSDavid Matlack 	if (test_wanted("test_vmxon", argv, argc)) {
1225*c04259ffSDavid Matlack 		/* Enables VMX */
12269d7eaa29SArthur Chunqi Li 		if (test_vmxon() != 0)
12279d7eaa29SArthur Chunqi Li 			goto exit;
1228*c04259ffSDavid Matlack 	} else {
1229*c04259ffSDavid Matlack 		if (vmx_on()) {
1230*c04259ffSDavid Matlack 			report("vmxon", 0);
1231*c04259ffSDavid Matlack 			goto exit;
1232*c04259ffSDavid Matlack 		}
1233*c04259ffSDavid Matlack 	}
1234*c04259ffSDavid Matlack 
1235*c04259ffSDavid Matlack 	if (test_wanted("test_vmptrld", argv, argc))
12369d7eaa29SArthur Chunqi Li 		test_vmptrld();
1237*c04259ffSDavid Matlack 	if (test_wanted("test_vmclear", argv, argc))
12389d7eaa29SArthur Chunqi Li 		test_vmclear();
1239*c04259ffSDavid Matlack 	if (test_wanted("test_vmptrst", argv, argc))
12409d7eaa29SArthur Chunqi Li 		test_vmptrst();
12419d7eaa29SArthur Chunqi Li 	init_vmcs(&vmcs_root);
12429d7eaa29SArthur Chunqi Li 	if (vmx_run()) {
12439d7eaa29SArthur Chunqi Li 		report("test vmlaunch", 0);
12449d7eaa29SArthur Chunqi Li 		goto exit;
12459d7eaa29SArthur Chunqi Li 	}
1246*c04259ffSDavid Matlack 
12479d7eaa29SArthur Chunqi Li 	test_vmxoff();
1248*c04259ffSDavid Matlack 
1249*c04259ffSDavid Matlack 	if (test_wanted("test_vmx_caps", argv, argc))
125069c8d31cSJan Kiszka 		test_vmx_caps();
12519d7eaa29SArthur Chunqi Li 
12528029cac7SPeter Feiner 	while (vmx_tests[++i].name != NULL) {
1253*c04259ffSDavid Matlack 		if (!test_wanted(vmx_tests[i].name, argv, argc))
12548029cac7SPeter Feiner 			continue;
12559d7eaa29SArthur Chunqi Li 		if (test_run(&vmx_tests[i]))
12569d7eaa29SArthur Chunqi Li 			goto exit;
12578029cac7SPeter Feiner 	}
12588029cac7SPeter Feiner 
12598029cac7SPeter Feiner 	if (!matched)
12608029cac7SPeter Feiner 		report("command line didn't match any tests!", matched);
12619d7eaa29SArthur Chunqi Li 
12629d7eaa29SArthur Chunqi Li exit:
1263f3cdd159SJan Kiszka 	return report_summary();
12649d7eaa29SArthur Chunqi Li }
1265