add minimal tests for 32-bit guests
Paolo Bonzini
maintainerv2 cover 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.
Rebased on top of Sean's regs rework + the brown paper bag fix at
https://lore.kernel.org/kvm/[email protected]/.
Paolo
Paolo Bonzini (4):
vmx: add test for 32-bit guest
vmx: add test for 32-bit PAE guest
svm: add test for 32-bit guest
svm: add test for 32-bit PAE guest
x86/svm_tests.c | 159 +++++++++++++++++++++++++++++++++++++++ x86/vmx_tests.c | 192 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 351 insertions(+)
Signature · 1 line
2.54.0
Paolo Bonzini
maintainerPATCH v2 1/4 vmx: add test for 32-bit guest
Signed-off-by Paolo Bonzini <[email protected]>
--- x86/vmx_tests.c | 125 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+)
x86/vmx_tests.c +125 -0
@@ -47,6 +47,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 ;
@@ -260,6 +262,127 @@ 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) +{ +struct guest_regs *regs =this_cpu_guest_regs (); +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) +{ +struct guest_regs *regs =this_cpu_guest_regs (); +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" ); +if (regs->rax !=1 ) +return VMX_TEST_VMABORT ; + +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" ); +if (regs->rax !=2 ) +return VMX_TEST_VMABORT ; +return VMX_TEST_VMEXIT ; +default : +report_fail ("Unexpected stage %d" , +vmx_get_test_stage ()); +return VMX_TEST_VMABORT ; + } +break ; +default : +report_fail ("32-bit guest: unexpected exit reason 0x%x" , + exit_reason.full); +print_vmexit_info (exit_reason); +return VMX_TEST_VMABORT ; + } +} +static void msr_bmp_init (void ) {void *msr_bitmap;
@@ -11800,6 +11923,8 @@ struct vmx_test vmx_tests[] = { { "vmenter", NULL, vmenter_main, vmenter_exit_handler, }, { "preemption timer", preemption_timer_init, preemption_timer_main, preemption_timer_exit_handler }, + { "32-bit guest 2-level paging", vmx_32bit_guest_init, + vmx_32bit_guest_main, vmx_32bit_guest_exit_handler }, { "control field PAT", test_ctrl_pat_init, test_ctrl_pat_main, test_ctrl_pat_exit_handler }, { "control field EFER", test_ctrl_efer_init, test_ctrl_efer_main,
Signature · 1 line
2.54.0
Paolo Bonzini
maintainerPATCH v2 2/4 vmx: add test for 32-bit PAE guest
In reply to Paolo BonziniSigned-off-by Paolo Bonzini <[email protected]>
--- x86/vmx_tests.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 4 deletions(-)
x86/vmx_tests.c +71 -4
@@ -279,7 +279,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 ) {
@@ -294,10 +294,33 @@ 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) {struct guest_regs *regs =this_cpu_guest_regs (); +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 );
@@ -315,11 +338,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 ;
@@ -331,6 +359,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 ();
@@ -339,7 +381,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 )
@@ -11925,6 +11988,10 @@ struct vmx_test vmx_tests[] = { preemption_timer_exit_handler }, {"32-bit guest 2-level paging" , vmx_32bit_guest_init, vmx_32bit_guest_main, vmx_32bit_guest_exit_handler }, + { "32-bit guest PAE paging", vmx_pae_guest_init, + vmx_32bit_guest_main, vmx_32bit_guest_exit_handler }, + { "32-bit guest PAE paging without EPT", vmx_pae_guest_no_ept_init, + vmx_32bit_guest_main, vmx_32bit_guest_exit_handler }, { "control field PAT", test_ctrl_pat_init, test_ctrl_pat_main, test_ctrl_pat_exit_handler }, { "control field EFER", test_ctrl_efer_init, test_ctrl_efer_main,
Signature · 1 line
2.54.0
Paolo Bonzini
maintainerPATCH v2 3/4 svm: add test for 32-bit guest
In reply to Paolo BonziniSigned-off-by Paolo Bonzini <[email protected]>
--- x86/svm_tests.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+)
x86/svm_tests.c +120 -0
@@ -770,6 +770,123 @@ 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 guest_regs *regs =this_cpu_guest_regs (); +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_PAE ; + save->cr4 |=X86_CR4_PSE ; + 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) +{ +struct guest_regs *regs =this_cpu_guest_regs (); +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 (regs->rax ==1 ,"32-bit guest first VMMCALL" ); +if (regs->rax !=1 ) +return true; + test->scratch =1 ; +return false; +case 1 : +report (regs->rax ==2 ,"32-bit guest second VMMCALL" ); +if (regs->rax !=2 ) +return true; + 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)
@@ -3693,6 +3810,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 v2 4/4 svm: add test for 32-bit PAE guest
In reply to Paolo BonziniSigned-off-by Paolo Bonzini <[email protected]>
--- x86/svm_tests.c | 49 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-)
x86/svm_tests.c +44 -5
@@ -803,7 +803,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 guest_regs *regs =this_cpu_guest_regs ();struct vmcb_save_area *save = &vmcb->save;
@@ -820,8 +837,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_PAE ; - save->cr4 |=X86_CR4_PSE ; + save->cr4 &= ~(X86_CR4_PAE |X86_CR4_PSE ); + save->cr4 |= pae ?X86_CR4_PAE :X86_CR4_PSE ; save->cr3 = cr3; regs->rax =0x123457678 ; }
@@ -830,7 +847,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)
@@ -877,8 +901,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)
@@ -3813,6 +3849,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