From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jan Kiszka Subject: Re: [PATCH v4 2/2] kvm-unit-tests : The first version of VMX nested test case Date: Wed, 17 Jul 2013 08:26:48 +0200 Message-ID: <51E63928.9030406@web.de> References: <1374041153-32235-1-git-send-email-yzt356@gmail.com> <1374041153-32235-3-git-send-email-yzt356@gmail.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="----enig2VVMNQGBVPWMJUOCADQJN" Cc: kvm@vger.kernel.org, gleb@redhat.com, pbonzini@redhat.com To: Arthur Chunqi Li Return-path: Received: from mout.web.de ([212.227.15.4]:58647 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751330Ab3GQG0y (ORCPT ); Wed, 17 Jul 2013 02:26:54 -0400 In-Reply-To: <1374041153-32235-3-git-send-email-yzt356@gmail.com> Sender: kvm-owner@vger.kernel.org List-ID: This is an OpenPGP/MIME signed message (RFC 4880 and 3156) ------enig2VVMNQGBVPWMJUOCADQJN Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: quoted-printable On 2013-07-17 08:05, Arthur Chunqi Li wrote: > This is the first version for VMX nested environment test case. It > contains the basic VMX instructions test cases, including VMXON/ > VMXOFF/VMXPTRLD/VMXPTRST/VMCLEAR/VMLAUNCH/VMRESUME/VMCALL. This patch > also tests the basic execution routine in VMX nested environment and > let the VM print "Hello World" to inform its successfully run. >=20 > New files added: > x86/vmx.h : contains all VMX related macro declerations > x86/vmx.c : main file for VMX nested test case >=20 > Signed-off-by: Arthur Chunqi Li > --- > config-x86-common.mak | 2 + > config-x86_64.mak | 1 + > lib/x86/msr.h | 5 + > x86/cstart64.S | 4 + > x86/unittests.cfg | 6 + > x86/vmx.c | 676 +++++++++++++++++++++++++++++++++++++++++= ++++++++ > x86/vmx.h | 419 ++++++++++++++++++++++++++++++ > 7 files changed, 1113 insertions(+) > create mode 100644 x86/vmx.c > create mode 100644 x86/vmx.h >=20 > diff --git a/config-x86-common.mak b/config-x86-common.mak > index 455032b..34a41e1 100644 > --- a/config-x86-common.mak > +++ b/config-x86-common.mak > @@ -101,6 +101,8 @@ $(TEST_DIR)/asyncpf.elf: $(cstart.o) $(TEST_DIR)/as= yncpf.o > =20 > $(TEST_DIR)/pcid.elf: $(cstart.o) $(TEST_DIR)/pcid.o > =20 > +$(TEST_DIR)/vmx.elf: $(cstart.o) $(TEST_DIR)/vmx.o > + > arch_clean: > $(RM) $(TEST_DIR)/*.o $(TEST_DIR)/*.flat $(TEST_DIR)/*.elf \ > $(TEST_DIR)/.*.d $(TEST_DIR)/lib/.*.d $(TEST_DIR)/lib/*.o > diff --git a/config-x86_64.mak b/config-x86_64.mak > index 91ffcce..5d9b22a 100644 > --- a/config-x86_64.mak > +++ b/config-x86_64.mak > @@ -11,5 +11,6 @@ tests =3D $(TEST_DIR)/access.flat $(TEST_DIR)/apic.fl= at \ > $(TEST_DIR)/xsave.flat $(TEST_DIR)/rmap_chain.flat \ > $(TEST_DIR)/pcid.flat > tests +=3D $(TEST_DIR)/svm.flat > +tests +=3D $(TEST_DIR)/vmx.flat > =20 > include config-x86-common.mak > diff --git a/lib/x86/msr.h b/lib/x86/msr.h > index 509a421..281255a 100644 > --- a/lib/x86/msr.h > +++ b/lib/x86/msr.h > @@ -396,6 +396,11 @@ > #define MSR_IA32_VMX_VMCS_ENUM 0x0000048a > #define MSR_IA32_VMX_PROCBASED_CTLS2 0x0000048b > #define MSR_IA32_VMX_EPT_VPID_CAP 0x0000048c > +#define MSR_IA32_VMX_TRUE_PIN 0x0000048d > +#define MSR_IA32_VMX_TRUE_PROC 0x0000048e > +#define MSR_IA32_VMX_TRUE_EXIT 0x0000048f > +#define MSR_IA32_VMX_TRUE_ENTRY 0x00000490 > + > =20 > /* AMD-V MSRs */ > =20 > diff --git a/x86/cstart64.S b/x86/cstart64.S > index 24df5f8..0fe76da 100644 > --- a/x86/cstart64.S > +++ b/x86/cstart64.S > @@ -4,6 +4,10 @@ > .globl boot_idt > boot_idt =3D 0 > =20 > +.globl idt_descr > +.globl tss_descr > +.globl gdt64_desc > + > ipi_vector =3D 0x20 > =20 > max_cpus =3D 64 > diff --git a/x86/unittests.cfg b/x86/unittests.cfg > index bc9643e..85c36aa 100644 > --- a/x86/unittests.cfg > +++ b/x86/unittests.cfg > @@ -149,3 +149,9 @@ extra_params =3D --append "10000000 `date +%s`" > file =3D pcid.flat > extra_params =3D -cpu qemu64,+pcid > arch =3D x86_64 > + > +[vmx] > +file =3D vmx.flat > +extra_params =3D -cpu host,+vmx > +arch =3D x86_64 > + > diff --git a/x86/vmx.c b/x86/vmx.c > new file mode 100644 > index 0000000..af48fce > --- /dev/null > +++ b/x86/vmx.c > @@ -0,0 +1,676 @@ > +#include "libcflat.h" > +#include "processor.h" > +#include "vm.h" > +#include "desc.h" > +#include "vmx.h" > +#include "msr.h" > +#include "smp.h" > +#include "io.h" > +#include "setjmp.h" > + > +int fails =3D 0, tests =3D 0; > +u32 *vmxon_region; > +struct vmcs *vmcs_root; > +void *io_bmp1, *io_bmp2; > +void *msr_bmp; > +u32 vpid_ctr; > +char *guest_stack, *host_stack; > +char *guest_syscall_stack, *host_syscall_stack; > +u32 ctrl_pin, ctrl_enter, ctrl_exit, ctrl_cpu[2]; > +ulong fix_cr0_set, fix_cr0_clr; > +ulong fix_cr4_set, fix_cr4_clr; > +struct regs regs; > +jmp_buf env; > +struct vmx_test *current; > + > +extern u64 gdt64_desc[]; > +extern u64 idt_descr[]; > +extern u64 tss_descr[]; > +extern void *entry_vmx; > +extern void *entry_sysenter; > +extern void *guest_entry; > + > +void report(const char *name, int result) > +{ > + ++tests; > + if (result) > + printf("PASS: %s\n", name); > + else { > + printf("FAIL: %s\n", name); > + ++fails; > + } > +} > + > +inline u64 get_rflags(void) > +{ > + u64 r; > + asm volatile("pushf; pop %0\n\t" : "=3Dq"(r) : : "cc"); > + return r; > +} > + > +inline void set_rflags(u64 r) > +{ > + asm volatile("push %0; popf\n\t" : : "q"(r) : "cc"); > +} > + > +int vmcs_clear(struct vmcs *vmcs) > +{ > + bool ret; > + asm volatile ("vmclear %1; setbe %0" : "=3Dq" (ret) : "m" (vmcs) : "c= c"); > + return ret; > +} > + > +u64 vmcs_read(enum Encoding enc) > +{ > + u64 val; > + asm volatile ("vmread %1, %0" : "=3Drm" (val) : "r" ((u64)enc) : "cc"= ); > + return val; > +} > + > +int vmcs_write(enum Encoding enc, u64 val) > +{ > + bool ret; > + asm volatile ("vmwrite %1, %2; setbe %0" > + : "=3Dq"(ret) : "rm" (val), "r" ((u64)enc) : "cc"); > + return ret; > +} > + > +int make_vmcs_current(struct vmcs *vmcs) > +{ > + bool ret; > + > + asm volatile ("vmptrld %1; setbe %0" : "=3Dq" (ret) : "m" (vmcs) : "c= c"); > + return ret; > +} > + > +int save_vmcs(struct vmcs **vmcs) > +{ > + bool ret; > + > + asm volatile ("vmptrst %1; setbe %0" : "=3Dq" (ret) : "m" (*vmcs) : "= cc"); > + return ret; > +} > + > +/* entry_vmx */ > +asm( > + ".align 4, 0x90\n\t" > + ".globl entry_vmx\n\t" > + "entry_vmx:\n\t" > + SAVE_GPR > + " call default_exit_handler\n\t" > + /* Should not reach here*/ > + " mov $1, %eax\n\t" > + " call exit\n\t" > +); > + > +/* entry_sysenter */ > +asm( > + ".align 4, 0x90\n\t" > + ".globl entry_sysenter\n\t" > + "entry_sysenter:\n\t" > + SAVE_GPR > + " and $0xf, %rax\n\t" > + " push %rax\n\t" > + " call default_syscall_handler\n\t" > +); > + > +void default_syscall_handler(u64 syscall_no) > +{ > + if (current !=3D NULL && current->syscall_handler !=3D NULL) { > + current->syscall_handler(syscall_no); > + return; > + } > + printf("Here in default syscall_handler,"); > + printf("syscall_no =3D %d\n", syscall_no); > + printf("Nothing is done here.\n"); > +} > + > +static inline void vmx_run() > +{ > + bool ret; > + /* printf("Now run vm.\n"); */ > + asm volatile("vmlaunch;setbe %0\n\t" : "=3Dm"(ret)); > + printf("VMLAUNCH error, ret=3D%d\n", ret); > +} > + > +static inline void vmx_resume() > +{ > + asm volatile(LOAD_GPR > + "vmresume\n\t"); > + /* VMRESUME fail if reach here */ > +} > + > +static inline int vmx_on() > +{ > + bool ret; > + asm volatile ("vmxon %1; setbe %0\n\t" > + : "=3Dq"(ret) : "m"(vmxon_region) : "cc"); > + return ret; > +} > + > +static inline int vmx_off() > +{ > + bool ret; > + asm volatile("vmxoff; setbe %0\n\t" > + : "=3Dq"(ret) : : "cc"); > + return ret; > +} > + > +void print_vmexit_info() > +{ > + u64 guest_rip, guest_rsp; > + ulong reason =3D vmcs_read(EXI_REASON) & 0xff; > + ulong exit_qual =3D vmcs_read(EXI_QUALIFICATION); > + guest_rip =3D vmcs_read(GUEST_RIP); > + guest_rsp =3D vmcs_read(GUEST_RSP); > + printf("VMEXIT info:\n"); > + printf("\tvmexit reason =3D %d\n", reason); > + printf("\texit qualification =3D 0x%x\n", exit_qual); > + printf("\tBit 31 of reason =3D %x\n", (vmcs_read(EXI_REASON) >> 31) &= 1); > + printf("\tguest_rip =3D 0x%llx\n", guest_rip); > + printf("\tRAX=3D0x%llx RBX=3D0x%llx RCX=3D0x%llx RDX=3D0x%ll= x\n", > + regs.rax, regs.rbx, regs.rcx, regs.rdx); > + printf("\tRSP=3D0x%llx RBP=3D0x%llx RSI=3D0x%llx RDI=3D0x%ll= x\n", > + guest_rsp, regs.rbp, regs.rsi, regs.rdi); > + printf("\tR8 =3D0x%llx R9 =3D0x%llx R10=3D0x%llx R11=3D0x%ll= x\n", > + regs.r8, regs.r9, regs.r10, regs.r11); > + printf("\tR12=3D0x%llx R13=3D0x%llx R14=3D0x%llx R15=3D0x%ll= x\n", > + regs.r12, regs.r13, regs.r14, regs.r15); > +} > + > +void test_vmclear(void) > +{ > + u64 rflags; > + > + rflags =3D get_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF; > + set_rflags(rflags); > + report("test vmclear", vmcs_clear(vmcs_root) =3D=3D 0); > +} > + > +void test_vmxoff(void) > +{ > + int ret; > + u64 rflags; > + > + rflags =3D get_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF; > + set_rflags(rflags); > + ret =3D vmx_off(); > + report("test vmxoff", !ret); > +} > + > +/* This function should not return directly, only three kinds > + of return is permitted : goto vm_exit, goto vm_resume > + and longjmp to previous set jmp_buf. > + For test case defined exit_handler, only three kinds of return > + value are permitted : VMX_EXIT, VMX_RESUME and VMX_HALT, > + which coressponding to three actions above, other return > + values will cause error message. */ > +void default_exit_handler() > +{ > + u64 guest_rip; > + ulong reason; > + int ret; > + > + if (current !=3D NULL && current->exit_handler !=3D NULL) { > + current->exits ++; > + current->guest_regs =3D regs; > + ret =3D current->exit_handler(); > + regs =3D current->guest_regs; > + switch (ret) { > + case VMX_HALT: > + goto vmx_halt; > + case VMX_RESUME: > + goto vmx_resume; > + case VMX_EXIT: > + goto vmx_exit; > + default: > + printf("ERROR : Invalid exit_handler return val, %d.\n" > + , ret); > + goto vmx_exit; > + } > + } > + > + if ((read_cr4() & CR4_PAE) && (read_cr0() & CR0_PG) > + && !(rdmsr(MSR_EFER) & EFER_LMA)) > + printf("ERROR : PDPTEs should be checked\n"); > + > + guest_rip =3D vmcs_read(GUEST_RIP); > + reason =3D vmcs_read(EXI_REASON) & 0xff; > + > + switch (reason) { > + case VMX_VMCALL: > + print_vmexit_info(); > + vmcs_write(GUEST_RIP, guest_rip + 3); > + goto vmx_resume; > + case VMX_HLT: > + goto vmx_halt; > + default: > + break; > + } > + printf("ERROR : Unhandled vmx exit.\n"); > + print_vmexit_info(); > +vmx_halt: > + /* printf("VM exit.\n"); */ > + longjmp(env, 1); > + /* Should not reach here */ > +vmx_exit: > + exit(-1); > +vmx_resume: > + vmx_resume(); > + /* Should not reach here */ > + exit(-1); > +} > + > +/* guest_entry */ > +asm( > + ".align 4, 0x90\n\t" > + ".globl entry_guest\n\t" > + "guest_entry:\n\t" > + " call default_guest_main\n\t" > + " hlt\n\t" > +); > + > +void default_guest_main(void) > +{ > + if (current !=3D NULL && current->guest_main !=3D NULL) { > + current->guest_main(); > + return; > + } > + /* Here is default guest_main, print Hello World */ > + printf("\tHello World, this is default guest main!\n"); > +} > + > +void init_vmcs_ctrl(void) > +{ > + /* 26.2 CHECKS ON VMX CONTROLS AND HOST-STATE AREA */ > + /* 26.2.1.1 */ > + vmcs_write(PIN_CONTROLS, ctrl_pin); > + /* Disable VMEXIT of IO instruction */ > + vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu[0]); > + if (ctrl_cpu_rev[0].set & CPU_SECONDARY) { > + ctrl_cpu[1] |=3D ctrl_cpu_rev[1].set & ctrl_cpu_rev[1].clr; > + vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu[1]); > + } > + vmcs_write(CR3_TARGET_COUNT, 0); > + io_bmp1 =3D alloc_page(); > + io_bmp2 =3D alloc_page(); > + memset(io_bmp1, 0, PAGE_SIZE); > + memset(io_bmp2, 0, PAGE_SIZE); > + vmcs_write(IO_BITMAP_A, (u64)io_bmp1); > + vmcs_write(IO_BITMAP_B, (u64)io_bmp2); > + msr_bmp =3D alloc_page(); > + memset(msr_bmp, 0, PAGE_SIZE); > + vmcs_write(MSR_BITMAP, (u64)msr_bmp); > + vmcs_write(VPID, ++vpid_ctr); > +} > + > +void init_vmcs_host(void) > +{ > + /* 26.2 CHECKS ON VMX CONTROLS AND HOST-STATE AREA */ > + /* 26.2.1.2 */ > + vmcs_write(HOST_EFER, rdmsr(MSR_EFER)); > + > + /* 26.2.1.3 */ > + vmcs_write(ENT_CONTROLS, ctrl_enter); > + vmcs_write(EXI_CONTROLS, ctrl_exit); > + > + /* 26.2.2 */ > + vmcs_write(HOST_CR0, read_cr0()); > + vmcs_write(HOST_CR3, read_cr3()); > + vmcs_write(HOST_CR4, read_cr4()); > + vmcs_write(HOST_SYSENTER_ESP, > + (u64)(host_syscall_stack + PAGE_SIZE - 1)); > + vmcs_write(HOST_SYSENTER_EIP, (u64)(&entry_sysenter)); > + vmcs_write(HOST_SYSENTER_CS, SEL_KERN_CODE_64); > + > + /* 26.2.3 */ > + vmcs_write(HOST_SEL_CS, SEL_KERN_CODE_64); > + vmcs_write(HOST_SEL_SS, SEL_KERN_DATA_64); > + vmcs_write(HOST_SEL_DS, SEL_KERN_DATA_64); > + vmcs_write(HOST_SEL_ES, SEL_KERN_DATA_64); > + vmcs_write(HOST_SEL_FS, SEL_KERN_DATA_64); > + vmcs_write(HOST_SEL_GS, SEL_KERN_DATA_64); > + vmcs_write(HOST_SEL_TR, SEL_TSS_RUN); > + vmcs_write(HOST_BASE_TR, (u64)tss_descr); > + vmcs_write(HOST_BASE_GDTR, (u64)gdt64_desc); > + vmcs_write(HOST_BASE_IDTR, (u64)idt_descr); > + vmcs_write(HOST_BASE_FS, 0); > + vmcs_write(HOST_BASE_GS, 0); > + > + /* Set other vmcs area */ > + vmcs_write(PF_ERROR_MASK, 0); > + vmcs_write(PF_ERROR_MATCH, 0); > + vmcs_write(VMCS_LINK_PTR, ~0ul); > + vmcs_write(VMCS_LINK_PTR_HI, ~0ul); > + vmcs_write(HOST_RSP, (u64)(host_stack + PAGE_SIZE - 1)); > + vmcs_write(HOST_RIP, (u64)(&entry_vmx)); > +} > + > +void init_vmcs_guest(void) > +{ > + /* 26.3 CHECKING AND LOADING GUEST STATE */ > + ulong guest_cr0, guest_cr4, guest_cr3; > + /* 26.3.1.1 */ > + guest_cr0 =3D read_cr0(); > + guest_cr4 =3D read_cr4(); > + guest_cr3 =3D read_cr3(); > + if (ctrl_enter & ENT_GUEST_64) { > + guest_cr0 |=3D CR0_PG; > + guest_cr4 |=3D CR4_PAE; > + } > + if ((ctrl_enter & ENT_GUEST_64) =3D=3D 0) > + guest_cr4 &=3D (~CR4_PCIDE); > + if (guest_cr0 & CR0_PG) > + guest_cr0 |=3D CR0_PE; > + vmcs_write(GUEST_CR0, guest_cr0); > + vmcs_write(GUEST_CR3, guest_cr3); > + vmcs_write(GUEST_CR4, guest_cr4); > + vmcs_write(GUEST_SYSENTER_CS, SEL_KERN_CODE_64); > + vmcs_write(GUEST_SYSENTER_ESP, > + (u64)(guest_syscall_stack + PAGE_SIZE - 1)); > + vmcs_write(GUEST_SYSENTER_EIP, (u64)(&entry_sysenter)); > + vmcs_write(GUEST_DR7, 0); > + vmcs_write(GUEST_EFER, rdmsr(MSR_EFER)); > + > + /* 26.3.1.2 */ > + vmcs_write(GUEST_SEL_CS, SEL_KERN_CODE_64); > + vmcs_write(GUEST_SEL_SS, SEL_KERN_DATA_64); > + vmcs_write(GUEST_SEL_DS, SEL_KERN_DATA_64); > + vmcs_write(GUEST_SEL_ES, SEL_KERN_DATA_64); > + vmcs_write(GUEST_SEL_FS, SEL_KERN_DATA_64); > + vmcs_write(GUEST_SEL_GS, SEL_KERN_DATA_64); > + vmcs_write(GUEST_SEL_TR, SEL_TSS_RUN); > + vmcs_write(GUEST_SEL_LDTR, 0); > + > + vmcs_write(GUEST_BASE_CS, 0); > + vmcs_write(GUEST_BASE_ES, 0); > + vmcs_write(GUEST_BASE_SS, 0); > + vmcs_write(GUEST_BASE_DS, 0); > + vmcs_write(GUEST_BASE_FS, 0); > + vmcs_write(GUEST_BASE_GS, 0); > + vmcs_write(GUEST_BASE_TR, (u64)tss_descr); > + vmcs_write(GUEST_BASE_LDTR, 0); > + > + vmcs_write(GUEST_LIMIT_CS, 0xFFFFFFFF); > + vmcs_write(GUEST_LIMIT_DS, 0xFFFFFFFF); > + vmcs_write(GUEST_LIMIT_ES, 0xFFFFFFFF); > + vmcs_write(GUEST_LIMIT_SS, 0xFFFFFFFF); > + vmcs_write(GUEST_LIMIT_FS, 0xFFFFFFFF); > + vmcs_write(GUEST_LIMIT_GS, 0xFFFFFFFF); > + vmcs_write(GUEST_LIMIT_LDTR, 0xffff); > + vmcs_write(GUEST_LIMIT_TR, ((struct descr *)tss_descr)->limit); > + > + vmcs_write(GUEST_AR_CS, 0xa09b); > + vmcs_write(GUEST_AR_DS, 0xc093); > + vmcs_write(GUEST_AR_ES, 0xc093); > + vmcs_write(GUEST_AR_FS, 0xc093); > + vmcs_write(GUEST_AR_GS, 0xc093); > + vmcs_write(GUEST_AR_SS, 0xc093); > + vmcs_write(GUEST_AR_LDTR, 0x82); > + vmcs_write(GUEST_AR_TR, 0x8b); > + > + /* 26.3.1.3 */ > + vmcs_write(GUEST_BASE_GDTR, (u64)gdt64_desc); > + vmcs_write(GUEST_BASE_IDTR, (u64)idt_descr); > + vmcs_write(GUEST_LIMIT_GDTR, > + ((struct descr *)gdt64_desc)->limit & 0xffff); > + vmcs_write(GUEST_LIMIT_IDTR, > + ((struct descr *)idt_descr)->limit & 0xffff); > + > + /* 26.3.1.4 */ > + vmcs_write(GUEST_RIP, (u64)(&guest_entry)); > + vmcs_write(GUEST_RSP, (u64)(guest_stack + PAGE_SIZE - 1)); > + vmcs_write(GUEST_RFLAGS, 0x2); > + > + /* 26.3.1.5 */ > + vmcs_write(GUEST_ACTV_STATE, 0); > + vmcs_write(GUEST_INTR_STATE, 0); > +} > + > +int init_vmcs(struct vmcs **vmcs) > +{ > + *vmcs =3D alloc_page(); > + memset(*vmcs, 0, PAGE_SIZE); > + (*vmcs)->revision_id =3D basic.revision; > + /* vmclear first to init vmcs */ > + if (vmcs_clear(*vmcs)) { > + printf("%s : vmcs_clear error\n", __func__); > + return 1; > + } > + > + if (make_vmcs_current(*vmcs)) { > + printf("%s : make_vmcs_current error\n", __func__); > + return 1; > + } > + > + /* All settings to pin/exit/enter/cpu > + control fields should be placed here */ > + ctrl_pin |=3D PIN_EXTINT | PIN_NMI | PIN_VIRT_NMI; > + ctrl_exit =3D EXI_LOAD_EFER | EXI_HOST_64; > + ctrl_enter =3D (ENT_LOAD_EFER | ENT_GUEST_64); > + ctrl_cpu[0] |=3D CPU_HLT; > + /* DIsable IO instruction VMEXIT now */ > + ctrl_cpu[0] &=3D (~(CPU_IO | CPU_IO_BITMAP)); > + ctrl_cpu[1] =3D 0; > + > + ctrl_pin =3D (ctrl_pin | ctrl_pin_rev.set) & ctrl_pin_rev.clr; > + ctrl_enter =3D (ctrl_enter | ctrl_enter_rev.set) & ctrl_enter_rev.clr= ; > + ctrl_exit =3D (ctrl_exit | ctrl_exit_rev.set) & ctrl_exit_rev.clr; > + ctrl_cpu[0] =3D (ctrl_cpu[0] | ctrl_cpu_rev[0].set) & ctrl_cpu_rev[0]= =2Eclr; > + > + init_vmcs_ctrl(); > + init_vmcs_host(); > + init_vmcs_guest(); > + return 0; > +} > + > +void init_vmx(void) > +{ > + vmxon_region =3D alloc_page(); > + memset(vmxon_region, 0, PAGE_SIZE); > + > + fix_cr0_set =3D rdmsr(MSR_IA32_VMX_CR0_FIXED0); > + fix_cr0_clr =3D rdmsr(MSR_IA32_VMX_CR0_FIXED1); > + fix_cr4_set =3D rdmsr(MSR_IA32_VMX_CR4_FIXED0); > + fix_cr4_clr =3D rdmsr(MSR_IA32_VMX_CR4_FIXED1); > + basic.val =3D rdmsr(MSR_IA32_VMX_BASIC); > + ctrl_pin_rev.val =3D rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_PIN > + : MSR_IA32_VMX_PINBASED_CTLS); > + ctrl_exit_rev.val =3D rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_EXIT > + : MSR_IA32_VMX_EXIT_CTLS); > + ctrl_enter_rev.val =3D rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_ENTRY > + : MSR_IA32_VMX_ENTRY_CTLS); > + ctrl_cpu_rev[0].val =3D rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_PROC > + : MSR_IA32_VMX_PROCBASED_CTLS); > + if (ctrl_cpu_rev[0].set & CPU_SECONDARY) > + ctrl_cpu_rev[1].val =3D rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2); > + if (ctrl_cpu_rev[1].set & CPU_EPT || ctrl_cpu_rev[1].set & CPU_VPID) > + ept_vpid.val =3D rdmsr(MSR_IA32_VMX_EPT_VPID_CAP); > + > + write_cr0((read_cr0() & fix_cr0_clr) | fix_cr0_set); > + write_cr4((read_cr4() & fix_cr4_clr) | fix_cr4_set | CR4_VMXE); > + > + *vmxon_region =3D basic.revision; > + current =3D NULL; > + > + guest_stack =3D alloc_page(); > + memset(guest_stack, 0, PAGE_SIZE); > + guest_syscall_stack =3D alloc_page(); > + memset(guest_syscall_stack, 0, PAGE_SIZE); > + host_stack =3D alloc_page(); > + memset(host_stack, 0, PAGE_SIZE); > + host_syscall_stack =3D alloc_page(); > + memset(host_syscall_stack, 0, PAGE_SIZE); > +} > + > +int test_vmx_capability(void) > +{ > + struct cpuid r; > + u64 ret1, ret2; > + u64 ia32_feature_control; > + r =3D cpuid(1); > + ret1 =3D ((r.c) >> 5) & 1; > + ia32_feature_control =3D rdmsr(MSR_IA32_FEATURE_CONTROL); > + ret2 =3D ((ia32_feature_control & 0x5) =3D=3D 0x5); > + if ((!ret2) && ((ia32_feature_control & 0x1) =3D=3D 0)) { > + wrmsr(MSR_IA32_FEATURE_CONTROL, 0x5); > + ia32_feature_control =3D rdmsr(MSR_IA32_FEATURE_CONTROL); > + ret2 =3D ((ia32_feature_control & 0x5) =3D=3D 0x5); > + } > + report("test vmx capability", ret1 & ret2); > + return !(ret1 & ret2); > +} > + > +int test_vmxon(void) > +{ > + int ret; > + u64 rflags; > + > + rflags =3D get_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF; > + set_rflags(rflags); > + ret =3D vmx_on(); > + report("test vmxon", !ret); > + return ret; > +} > + > +void test_vmptrld(void) > +{ > + u64 rflags; > + struct vmcs *vmcs; > + > + vmcs =3D alloc_page(); > + vmcs->revision_id =3D basic.revision; > + rflags =3D get_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF; > + set_rflags(rflags); > + report("test vmptrld", make_vmcs_current(vmcs) =3D=3D 0); > +} > + > +void test_vmptrst(void) > +{ > + u64 rflags; > + int ret; > + struct vmcs *vmcs1, *vmcs2; > + > + vmcs1 =3D alloc_page(); > + memset(vmcs1, 0, PAGE_SIZE); > + init_vmcs(&vmcs1); > + rflags =3D get_rflags() | X86_EFLAGS_CF | X86_EFLAGS_ZF; > + set_rflags(rflags); > + ret =3D save_vmcs(&vmcs2); > + report("test vmptrst", (!ret) && (vmcs1 =3D=3D vmcs2)); > +} > + > +int test_run(struct vmx_test *test) > +{ > + if (test =3D=3D NULL) { Not possible, is it? > + printf("%s : test is NULL.\n", __func__); > + return 1; > + } > + if (test->name =3D=3D NULL) > + test->name =3D "(no name)"; > + if (vmx_on()) { > + printf("%s : vmxon failed.\n", __func__); > + return 2; > + } > + init_vmcs(&(test->vmcs)); > + /* Directly call test->init is ok here, init_vmcs has done > + vmcs init, vmclear and vmptrld*/ > + if (test->init) > + test->init(test->vmcs); > + test->exits =3D 0; > + current =3D test; > + regs =3D test->guest_regs; > + printf("\nTest suite : %s\n", test->name); > + if (setjmp(env) =3D=3D 0) { > + vmx_run(); > + /* Should not reach here */ > + printf("%s : vmx_run failed.\n", __func__); > + } > + if (vmx_off()) { > + printf("%s : vmxoff failed.\n", __func__); > + return 2; > + } > + return 0; > +} > + > +void vmenter_main() > +{ > + u64 rax; > + u64 rsp, resume_rsp; > + > + report("test vmlaunch", 1); > + > + rax =3D 0; Can already be initialized above. > + asm volatile("mov %%rsp, %0\n\t" : "=3Dr"(rsp)); > + asm volatile("mov %2, %%rax\n\t" > + "vmcall\n\t" > + "mov %%rax, %0\n\t" > + "mov %%rsp, %1\n\t" > + : "=3Dr"(rax), "=3Dr"(resume_rsp) > + : "g"(0xABCD)); > + report("test vmresume", (rax =3D=3D 0xFFFF) && (rsp =3D=3D resume_rsp= )); > +} > + > +int vmenter_exit_handler() > +{ > + u64 guest_rip; > + ulong reason; > + > + guest_rip =3D vmcs_read(GUEST_RIP); > + reason =3D vmcs_read(EXI_REASON) & 0xff; > + if (reason =3D=3D VMX_VMCALL) { I suppose any reason !=3D VMX_VMCALL or VMX_HALT means test failure, no? Then catch it. > + if (current->guest_regs.rax !=3D 0xABCD) { > + report("test vmresume", 0); > + return VMX_HALT; > + } > + current->guest_regs.rax =3D 0xFFFF; > + vmcs_write(GUEST_RIP, guest_rip + 3); > + return VMX_RESUME; > + } > + return VMX_HALT; > +} > + > + > +/* name/init/guest_main/exit_handler/syscall_handler/guest_regs > + NULL means use default func */ > +static struct vmx_test vmx_tests[] =3D { > + { "null", NULL, NULL, NULL, NULL, {0} }, Let's convert the "null" test into a proper one, removing all the current !=3D NULL checks from the default handlers. > + { "vmenter", NULL, vmenter_main, vmenter_exit_handler, NULL, {0} }, > +}; > + > +int main(void) > +{ > + int nr, i; > + > + setup_vm(); > + setup_idt(); > + > + if (test_vmx_capability() !=3D 0) { > + printf("ERROR : vmx not supported, check +vmx option\n"); > + goto exit; > + } > + init_vmx(); > + if (test_vmxon() !=3D 0) > + goto exit; > + test_vmptrld(); > + test_vmclear(); > + test_vmptrst(); > + init_vmcs(&vmcs_root); > + if (setjmp(env) =3D=3D 0) { > + vmx_run(); > + /* Should not reach here */ > + report("test vmlaunch", 0); > + goto exit; > + } > + test_vmxoff(); > + > + nr =3D ARRAY_SIZE(vmx_tests); No need for a separate nr variable here. > + for (i =3D 0; i < nr; ++i) { > + if (test_run(&vmx_tests[i])) > + goto exit; > + } > + > +exit: > + printf("\nSUMMARY: %d tests, %d failures\n", tests, fails); > + return fails ? 1 : 0; > +} Nice work! I was just wondering how hard it would be to prepare the infrastructure for multiple L2 guests as well. But that could also be done later on once there is a concrete test on the table that requires this. Jan ------enig2VVMNQGBVPWMJUOCADQJN Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.16 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iEYEARECAAYFAlHmOSoACgkQitSsb3rl5xR4IgCfRPc/d6tnnZbaJl73W2WyDsUh T6AAniLvaM5uKFU/Hg3NOd8TcgrZTsOU =gUMx -----END PGP SIGNATURE----- ------enig2VVMNQGBVPWMJUOCADQJN--