add minimal tests for 32-bit guests
Paolo Bonzini
maintainercover add minimal tests for 32-bit guests
VMX and SVM tests are only running 32-bit guests. Add a minimal test that they can at least start a guest with both two-level and PAE page tables; for VMX only, check also the case where CR3 is bogus but the PDPTRs are stuffed correctly in the VMCS.
This series might conflict with Sean's regs/backtrace rework, I'll rebase it as needed.
Paolo
Paolo Bonzini (5):
vmx: add test for 32-bit guest
vmx: add test for 32-bit PAE guest
svm: cleanup regs handling
svm: add test for 32-bit guest
svm: add test for 32-bit PAE guest
x86/svm.c | 8 --- x86/svm.h | 3 +- x86/svm_tests.c | 157 +++++++++++++++++++++++++++++++++++++++- x86/vmx_tests.c | 186 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 343 insertions(+), 11 deletions(-)
Signature · 1 line
2.54.0
Paolo Bonzini
maintainerPATCH 1/5 vmx: add test for 32-bit guest
Add a simple test that runs a 32-bit code segment with two-level page tables.
--- x86/vmx_tests.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+)
x86/vmx_tests.c +119 -0
@@ -46,6 +46,8 @@ phys_addr_t pci_physaddr;void *pml_log;#define PML_INDEX 512 +static int setup_ept (bool enable_ad); +static inline unsigned ffs (unsigned x) {int pos =-1 ;
@@ -258,6 +260,121 @@ static int preemption_timer_exit_handler(union exit_reason exit_reason)return VMX_TEST_VMEXIT ; } +/* +* 32-bit guest tests running a tiny guest in 32-bit protected mode with +* paging enabled. +*/ +extern char vmx_32bit_guest_begin[]; + +asm(".code32\n\t" +"vmx_32bit_guest_begin:\n\t" +" mov $1, %eax\n\t" +" vmcall\n\t" +" mov $2, %eax\n\t" +" vmcall\n\t" +"1: jmp 1b\n\t" +".code64\n\t" ); + +/* +* Build identity-mapped page tables for a 32-bit guest covering the +* low 4 GB using 4GB pages, returns the value to load into GUEST_CR3. +*/ +static u64 vmx_32bit_build_page_tables (void ) +{ +const u64 leaf_flags =PT_PRESENT_MASK |PT_WRITABLE_MASK | +PT_PAGE_SIZE_MASK ; +int i; + +u32 *pd =memalign_pages_flags (PAGE_SIZE ,PAGE_SIZE , +AREA_DMA32 ); +for (i =0 ; i <1024 ; i++) + pd[i] = ((u32 )i <<22 ) | leaf_flags; +return virt_to_phys (pd); +} + +static int vmx_32bit_guest_init_common (u64 cr3) +{ +u64 cr0, cr4; + +vmx_set_test_stage (0 ); + +/* Disable IA-32e mode for the guest. */ +vmcs_write (ENT_CONTROLS ,vmcs_read (ENT_CONTROLS ) & ~ENT_GUEST_64 ); +vmcs_write (GUEST_EFER , +vmcs_read (GUEST_EFER ) & ~(EFER_LMA |EFER_LME )); + +/* +* 32-bit code segment access rights: +* type=B (execute/read/accessed), S=1, DPL=0, P=1, +* L=0 (32-bit), D/B=1 (32-bit operand), G=1 (4K granularity). +*/ +vmcs_write (GUEST_AR_CS ,0xc09b ); + + cr0 =vmcs_read (GUEST_CR0 ) |X86_CR0_PG |X86_CR0_PE ; + cr4 =vmcs_read (GUEST_CR4 ) & ~(X86_CR4_PCIDE |X86_CR4_PAE |X86_CR4_PSE ); + cr4 |=X86_CR4_PSE ; +vmcs_write (GUEST_CR0 , cr0); +vmcs_write (GUEST_CR4 , cr4); +vmcs_write (GUEST_CR3 , cr3); + +vmcs_write (GUEST_RIP , (u64 )(uintptr_t )vmx_32bit_guest_begin); + regs.rax =0x123457678 ; + +return VMX_TEST_START ; +} + +static void vmx_32bit_free_page_tables_cb (void *pd) +{ +free_page (pd); +} + +static int vmx_32bit_guest_init (struct vmcs *vmcs) +{ +u64 cr3 =vmx_32bit_build_page_tables (); + +if ((ctrl_cpu_rev[0 ].clr &CPU_SECONDARY ) && (ctrl_cpu_rev[1 ].clr &CPU_EPT )) +setup_ept (false); + +test_add_teardown (vmx_32bit_free_page_tables_cb,phys_to_virt (cr3)); +return vmx_32bit_guest_init_common (cr3); +} + +static void vmx_32bit_guest_main (void ) +{ +/* Unused: GUEST_RIP is overridden by the init function. */ +} + +static int vmx_32bit_guest_exit_handler (union exit_reason exit_reason) +{ +u64 guest_rip =vmcs_read (GUEST_RIP ); +u32 insn_len =vmcs_read (EXI_INST_LEN ); + +switch (exit_reason.basic) { +case VMX_VMCALL : +switch (vmx_get_test_stage ()) { +case 0 : +report (regs.rax ==1 ,"32-bit guest first VMCALL" ); +vmx_set_test_stage (1 ); +vmcs_write (GUEST_RIP , guest_rip + insn_len); +return VMX_TEST_RESUME ; +case 1 : +report (regs.rax ==2 ,"32-bit guest second VMCALL" ); +return VMX_TEST_VMEXIT ; +default : +report_fail ("Unexpected stage %d" , +vmx_get_test_stage ()); +break ; + } +break ; +default : +report_fail ("32-bit guest: unexpected exit reason 0x%x" , + exit_reason.full); +print_vmexit_info (exit_reason); +break ; + } +return VMX_TEST_VMEXIT ; +} +static void msr_bmp_init (void ) {void *msr_bitmap;
@@ -11763,6 +11880,8 @@ struct vmx_test vmx_tests[] = { { "vmenter", NULL, vmenter_main, vmenter_exit_handler, NULL, {0} }, {"preemption timer" , preemption_timer_init, preemption_timer_main, preemption_timer_exit_handler, NULL, {0} }, + { "32-bit guest 2-level paging", vmx_32bit_guest_init, + vmx_32bit_guest_main, vmx_32bit_guest_exit_handler, NULL, {0} }, { "control field PAT", test_ctrl_pat_init, test_ctrl_pat_main, test_ctrl_pat_exit_handler, NULL, {0} }, { "control field EFER", test_ctrl_efer_init, test_ctrl_efer_main,
Signature · 1 line
2.54.0
Paolo Bonzini
maintainerPATCH 2/5 vmx: add test for 32-bit PAE guest
In reply to Paolo BonziniAdd a simple test that runs a 32-bit code segment with PAE page tables. Check enabled and disabled EPT separately, because only the former uses PDPTR fields in the VMCS.
--- x86/vmx_tests.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 4 deletions(-)
x86/vmx_tests.c +71 -4
@@ -277,7 +277,7 @@ asm(".code32\n\t"/* * Build identity-mapped page tables for a 32-bit guest covering the -* low 4 GB using 4GB pages, returns the value to load into GUEST_CR3. +* low 4 GB using 4GB pages. Returns the value to load into GUEST_CR3. */ static u64 vmx_32bit_build_page_tables (void ) {
@@ -292,9 +292,32 @@ static u64 vmx_32bit_build_page_tables(void)return virt_to_phys (pd); } -static int vmx_32bit_guest_init_common (u64 cr3) +/* +* Build identity-mapped page tables for a 32-bit guest covering the +* low 4 GB using PAE pages. Fill the four PDPTRs into the pdpt[] +* array. +*/ +static void vmx_32bit_build_pae_page_tables (u64 *pdpt) { +const u64 leaf_flags =PT_PRESENT_MASK |PT_WRITABLE_MASK | +PT_PAGE_SIZE_MASK ; +int i, j; + +for (i =0 ; i <4 ; i++) { +u64 *pd =memalign_pages_flags (PAGE_SIZE ,PAGE_SIZE , +AREA_DMA32 ); +for (j =0 ; j <512 ; j++) + pd[j] = (((u64 )i <<30 ) | ((u64 )j <<21 )) | + leaf_flags; + pdpt[i] =virt_to_phys (pd) |PT_PRESENT_MASK ; + } +} + +static int vmx_32bit_guest_init_common (u64 cr3,u64 *pdpt) +{ +bool ept = (ctrl_cpu_rev[0 ].clr &CPU_SECONDARY ) && (ctrl_cpu_rev[1 ].clr &CPU_EPT );u64 cr0, cr4; +int i;vmx_set_test_stage (0 );
@@ -312,11 +335,16 @@ static int vmx_32bit_guest_init_common(u64 cr3) cr0 =vmcs_read (GUEST_CR0 ) |X86_CR0_PG |X86_CR0_PE ; cr4 =vmcs_read (GUEST_CR4 ) & ~(X86_CR4_PCIDE |X86_CR4_PAE |X86_CR4_PSE ); - cr4 |=X86_CR4_PSE ; + cr4 |= pdpt ?X86_CR4_PAE :X86_CR4_PSE ;vmcs_write (GUEST_CR0 , cr0);vmcs_write (GUEST_CR4 , cr4);vmcs_write (GUEST_CR3 , cr3); +if (pdpt && ept) { +for (i =0 ; i <4 ; i++) +vmcs_write (GUEST_PDPTE +2 * i, pdpt[i]); + } +vmcs_write (GUEST_RIP , (u64 )(uintptr_t )vmx_32bit_guest_begin); regs.rax =0x123457678 ;
@@ -328,6 +356,20 @@ static void vmx_32bit_free_page_tables_cb(void *pd)free_page (pd); } +static void vmx_pae_free_page_tables_cb (void *pdpt_) +{ +u64 *pdpt = pdpt_; +int i; + +for (i =0 ; i <4 ; i++) { +u64 pdpte = pdpt ? pdpt[i] :vmcs_read (GUEST_PDPTE +2 * i); +if (pdpte &PT_PRESENT_MASK ) +free_page (phys_to_virt (pdpte &PT_ADDR_MASK )); + } +if (pdpt) +free_page (pdpt); +} +static int vmx_32bit_guest_init (struct vmcs *vmcs) {u64 cr3 =vmx_32bit_build_page_tables ();
@@ -336,7 +378,28 @@ static int vmx_32bit_guest_init(struct vmcs *vmcs)setup_ept (false);test_add_teardown (vmx_32bit_free_page_tables_cb,phys_to_virt (cr3)); -return vmx_32bit_guest_init_common (cr3); +return vmx_32bit_guest_init_common (cr3,NULL ); +} + +static int vmx_pae_guest_init (struct vmcs *vmcs) +{ +u64 pdpt[4 ]; + +if (setup_ept (false)) +return VMX_TEST_VMSKIP ; + +vmx_32bit_build_pae_page_tables (pdpt); +test_add_teardown (vmx_pae_free_page_tables_cb,NULL ); +return vmx_32bit_guest_init_common (0x01234560 , pdpt); +} + +static int vmx_pae_guest_no_ept_init (struct vmcs *vmcs) +{ +u64 *pdpt =memalign_pages_flags (PAGE_SIZE ,PAGE_SIZE ,AREA_DMA32 ); + +vmx_32bit_build_pae_page_tables (pdpt); +test_add_teardown (vmx_pae_free_page_tables_cb, pdpt); +return vmx_32bit_guest_init_common (virt_to_phys (pdpt), pdpt); }static void vmx_32bit_guest_main (void )
@@ -11882,6 +11945,10 @@ struct vmx_test vmx_tests[] = { preemption_timer_exit_handler, NULL, {0} }, {"32-bit guest 2-level paging" , vmx_32bit_guest_init, vmx_32bit_guest_main, vmx_32bit_guest_exit_handler, NULL, {0} }, + { "32-bit guest PAE paging", vmx_pae_guest_init, + vmx_32bit_guest_main, vmx_32bit_guest_exit_handler, NULL, {0} }, + { "32-bit guest PAE paging without EPT", vmx_pae_guest_no_ept_init, + vmx_32bit_guest_main, vmx_32bit_guest_exit_handler, NULL, {0} }, { "control field PAT", test_ctrl_pat_init, test_ctrl_pat_main, test_ctrl_pat_exit_handler, NULL, {0} }, { "control field EFER", test_ctrl_efer_init, test_ctrl_efer_main,
Signature · 1 line
2.54.0
Paolo Bonzini
maintainerPATCH 3/5 svm: cleanup regs handling
In reply to Paolo BonziniRemove the unnecessary get_regs() indirection so that it is possible to write to the regs struct.
--- x86/svm.c | 8 -------- x86/svm.h | 3 ++- x86/svm_tests.c | 4 ++-- 3 files changed, 4 insertions(+), 11 deletions(-)
x86/svm.c +0 -8
@@ -225,14 +225,6 @@ void vmcb_ident(struct vmcb *vmcb)struct regs regs; -struct regs get_regs (void ) -{ -return regs; -} - -// rax handled specially below - -struct svm_test *v2_test;
x86/svm.h +2 -1
@@ -436,7 +436,6 @@ int get_test_stage(struct svm_test *test);void set_test_stage (struct svm_test *test,int s);void inc_test_stage (struct svm_test *test);void vmcb_ident (struct vmcb *vmcb); -struct regs get_regs (void );void vmmcall (void );void svm_setup_vmrun (u64 rip);int __svm_vmrun (u64 rip);
@@ -444,6 +443,7 @@ int svm_vmrun(void);void test_set_guest (test_guest_func func);extern struct vmcb *vmcb; +extern struct regs regs;static inline void stgi (void ) {
@@ -457,6 +457,7 @@ static inline void clgi(void) +// RAX handled specially in ASM_PRE_VMRUN_CMD/ASM_POST_VMRUN_CMD #define SAVE_GPR_C \ "xchg %%rbx, regs+0x8\n\t" \ "xchg %%rcx, regs+0x10\n\t" \
x86/svm_tests.c +2 -2
@@ -649,9 +649,9 @@ static bool msr_intercept_finished(struct svm_test *test)* while RAX hold its lower 32 bits. */ if (vmcb->control.exit_info_1) - test->scratch = ((get_regs ().rdx <<32 ) | (vmcb->save.rax &0xffffffff )); + test->scratch = ((regs.rdx <<32 ) | (vmcb->save.rax &0xffffffff ));else - test->scratch =get_regs ().rcx; + test->scratch = regs.rcx;return false; }
Signature · 1 line
2.54.0
Paolo Bonzini
maintainerPATCH 4/5 svm: add test for 32-bit guest
In reply to Paolo BonziniAdd a simple test that runs a 32-bit code segment with two-level page tables.
--- x86/svm_tests.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+)
x86/svm_tests.c +114 -0
@@ -769,6 +769,117 @@ static bool check_mode_switch(struct svm_test *test)return test->scratch ==2 ; } +/* +* 32-bit guest tests. These tests run a tiny guest in legacy 32-bit +* protected mode, with either 2-level (non-PAE) or 3-level (PAE) paging. +* The guest code is in the low 4 GB and is identity-mapped using large +* pages. The guest issues a few VMMCALLs and then spins; since no +* interrupts are taken, the rest of the address space need not be mapped. +* +* Unlike VMX (where the four PDPTEs are loaded from the VMCS when EPT is +* enabled), SVM always loads the PAE PDPTEs from memory at guest CR3, so +* a real PDPT is required even when NPT is enabled. +*/ +extern char svm_32bit_guest_begin[]; + +asm(".code32\n\t" +"svm_32bit_guest_begin:\n\t" +" mov $1, %eax\n\t" +" vmmcall\n\t" +" mov $2, %eax\n\t" +" vmmcall\n\t" +"1: jmp 1b\n\t" +".code64\n\t" ); + +static u64 svm_32bit_build_page_tables (void ) +{ +const u64 leaf_flags =PT_PRESENT_MASK |PT_WRITABLE_MASK |PT_PAGE_SIZE_MASK ; +u32 *pd =memalign_pages_flags (PAGE_SIZE ,PAGE_SIZE ,AREA_DMA32 ); +int i; + +for (i =0 ; i <1024 ; i++) + pd[i] = ((u32 )i <<22 ) | leaf_flags; +return virt_to_phys (pd); +} + +static void svm_32bit_guest_prepare_common (struct svm_test *test,u64 cr3) +{ +struct vmcb_save_area *save = &vmcb->save; + +vmcb_ident (vmcb); + test->scratch =0 ; + +/* +* 32-bit code segment attribute: +* type=B (execute/read/accessed), S=1, DPL=0, P=1, +* L=0 (32-bit), D/B=1 (32-bit operand), G=1 (4K granularity). +*/ + save->cs.attrib =0xc9b ; + + save->efer &= ~(EFER_LMA |EFER_LME ); + save->cr0 |=X86_CR0_PG |X86_CR0_PE ; + save->cr4 &= ~X86_CR4_PSE ; + save->cr4 |=X86_CR4_PAE ; + save->cr3 = cr3; + regs.rax =0x123457678 ; +} + +static void svm_32bit_guest_prepare (struct svm_test *test) +{ +u64 cr3 =svm_32bit_build_page_tables (); + +svm_32bit_guest_prepare_common (test, cr3); +} + +static void svm_32bit_guest_prepare_gif_clear (struct svm_test *test) +{ +if (test->scratch ==0 ) + vmcb->save.rip = (ulong )svm_32bit_guest_begin; +} + +static void svm_32bit_guest_test (struct svm_test *test) +{ +/* Unused: RIP is overridden in prepare_gif_clear. */ +} + +static bool svm_32bit_guest_finished (struct svm_test *test) +{ +if (vmcb->control.exit_code !=SVM_EXIT_VMMCALL ) { +report_fail ("32-bit guest: unexpected exit code 0x%x" , + vmcb->control.exit_code); +return true; + } + + vmcb->save.rip +=3 ; + +switch (test->scratch) { +case 0 : +report (vmcb->save.rax ==1 ,"32-bit guest first VMMCALL" ); + test->scratch =1 ; +return false; +case 1 : +report (vmcb->save.rax ==2 ,"32-bit guest second VMMCALL" ); + test->scratch =2 ; +return true; +default : +report_fail ("32-bit guest: unexpected stage %ld" , test->scratch); +return true; + } +} + +static void svm_32bit_free_page_tables (void ) +{ +u64 cr3 = vmcb->save.cr3; + +free_page (phys_to_virt (cr3)); +} + +static bool svm_32bit_guest_check (struct svm_test *test) +{ +svm_32bit_free_page_tables (); +return test->scratch ==2 ; +} +extern u8 *io_bitmap;static void prepare_ioio (struct svm_test *test)
@@ -3692,6 +3803,9 @@ struct svm_test svm_tests[] = { {"vgif" , vgif_supported, prepare_vgif_enabled, default_prepare_gif_clear, test_vgif, vgif_finished, vgif_check }, + { "32bit_guest", default_supported, svm_32bit_guest_prepare, + svm_32bit_guest_prepare_gif_clear, svm_32bit_guest_test, svm_32bit_guest_finished, + svm_32bit_guest_check },TEST (svm_cr4_osxsave_test),TEST (svm_guest_state_test), TEST(svm_vmrun_errata_test),
Signature · 1 line
2.54.0
Paolo Bonzini
maintainerPATCH 5/5 svm: add test for 32-bit PAE guest
In reply to Paolo BonziniAdd a simple test that runs a 32-bit code segment with PAE page tables.
--- x86/svm_tests.c | 49 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-)
x86/svm_tests.c +44 -5
@@ -802,7 +802,24 @@ static u64 svm_32bit_build_page_tables(void)return virt_to_phys (pd); } -static void svm_32bit_guest_prepare_common (struct svm_test *test,u64 cr3) +static u64 svm_32bit_build_pae_page_tables (void ) +{ +const u64 leaf_flags =PT_PRESENT_MASK |PT_WRITABLE_MASK |PT_PAGE_SIZE_MASK ; +u64 *pdpt =memalign_pages_flags (PAGE_SIZE ,PAGE_SIZE ,AREA_DMA32 ); +int i, j; + +for (i =0 ; i <4 ; i++) { +u64 *pd =memalign_pages_flags (PAGE_SIZE ,PAGE_SIZE , +AREA_DMA32 ); +for (j =0 ; j <512 ; j++) + pd[j] = (((u64 )i <<30 ) | ((u64 )j <<21 )) | + leaf_flags; + pdpt[i] =virt_to_phys (pd) |PT_PRESENT_MASK ; + } +return virt_to_phys (pdpt); +} + +static void svm_32bit_guest_prepare_common (struct svm_test *test,u64 cr3,bool pae) {struct vmcb_save_area *save = &vmcb->save;
@@ -818,8 +835,8 @@ static void svm_32bit_guest_prepare_common(struct svm_test *test, u64 cr3) save->efer &= ~(EFER_LMA |EFER_LME ); save->cr0 |=X86_CR0_PG |X86_CR0_PE ; - save->cr4 &= ~X86_CR4_PSE ; - save->cr4 |=X86_CR4_PAE ; + save->cr4 &= ~(X86_CR4_PAE |X86_CR4_PSE ); + save->cr4 |= pae ?X86_CR4_PAE :X86_CR4_PSE ; save->cr3 = cr3; regs.rax =0x123457678 ; }
@@ -828,7 +845,14 @@ static void svm_32bit_guest_prepare(struct svm_test *test) {u64 cr3 =svm_32bit_build_page_tables (); -svm_32bit_guest_prepare_common (test, cr3); +svm_32bit_guest_prepare_common (test, cr3, false); +} + +static void svm_32bit_guest_prepare_pae (struct svm_test *test) +{ +u64 cr3 =svm_32bit_build_pae_page_tables (); + +svm_32bit_guest_prepare_common (test, cr3, true); }static void svm_32bit_guest_prepare_gif_clear (struct svm_test *test)
@@ -870,8 +894,20 @@ static bool svm_32bit_guest_finished(struct svm_test *test)static void svm_32bit_free_page_tables (void ) {u64 cr3 = vmcb->save.cr3; +u64 cr4 = vmcb->save.cr4; -free_page (phys_to_virt (cr3)); +if (cr4 &X86_CR4_PAE ) { +u64 *pdpt =phys_to_virt (cr3); +int i; + +for (i =0 ; i <4 ; i++) { +if (pdpt[i] &PT_PRESENT_MASK ) +free_page (phys_to_virt (pdpt[i] &PT_ADDR_MASK )); + } +free_page (pdpt); + }else { +free_page (phys_to_virt (cr3)); + } }static bool svm_32bit_guest_check (struct svm_test *test)
@@ -3806,6 +3842,9 @@ struct svm_test svm_tests[] = { {"32bit_guest" , default_supported, svm_32bit_guest_prepare, svm_32bit_guest_prepare_gif_clear, svm_32bit_guest_test, svm_32bit_guest_finished, svm_32bit_guest_check }, + { "32bit_pae_guest", default_supported, svm_32bit_guest_prepare_pae, + svm_32bit_guest_prepare_gif_clear, svm_32bit_guest_test, svm_32bit_guest_finished, + svm_32bit_guest_check },TEST (svm_cr4_osxsave_test),TEST (svm_guest_state_test), TEST(svm_vmrun_errata_test),
Signature · 1 line
2.54.0