add reserved bit tests for 32-bit guests
Paolo Bonzini
maintainerv3 cover add reserved bit tests for 32-bit guests
VMX and SVM tests are only running 64-bit guests. Add a test that checks the reserved and ignored bits in CR3 and PDPTEs. Improved version of
https://lore.kernel.org/kvm/[email protected]/
Paolo
Paolo Bonzini (4):
vmx: add a test for 32-bit guest PAE paging PDPTEs
vmx: add test for 32-bit non-PAE guest
svm: add test for 32-bit PAE guest
svm: add test for 32-bit non-PAE guest
x86/svm_tests.c | 226 +++++++++++++++++++++++++++++++++++++++++++ x86/vmx.c | 10 ++ x86/vmx.h | 1 + x86/vmx_tests.c | 252 +++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 486 insertions(+), 3 deletions(-)
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH v3 1/4 vmx: add a test for 32-bit guest PAE paging PDPTEs
VMX tests are only running 64-bit guests. Add a test that checks the reserved and ignored bits in CR3 and PDPTEs for PAE mode.
--- x86/vmx.c | 10 +++ x86/vmx.h | 1 + x86/vmx_tests.c | 186 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 194 insertions(+), 3 deletions(-)
x86/vmx.c +10 -0
@@ -2016,6 +2016,16 @@ void enter_guest_with_bad_controls(void) VMXERR_ENTRY_INVALID_CONTROL_FIELD); } +bool enter_guest_report_pass (void ) +{ +struct vmentry_result result; + +__enter_guest (ABORT_ON_EARLY_VMENTRY_FAIL | +ABORT_ON_INVALID_GUEST_STATE , &result); +report (!result.vm_fail,"VM-entry succeeded" ); +return !result.vm_fail; +} +void enter_guest (void ) {struct vmentry_result result;
x86/vmx.h +1 -0
@@ -991,6 +991,7 @@ void clear_ept_ad(unsigned long *pml4, u64 guest_cr3,void __enter_guest (u8 abort_flag,struct vmentry_result *result);void enter_guest (void ); +bool enter_guest_report_pass (void );void enter_guest_with_bad_controls (void );void hypercall (u32 hypercall_no);
x86/vmx_tests.c +183 -3
222 lines not on this page · raw message
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH v3 2/4 vmx: add test for 32-bit non-PAE guest
In reply to Paolo BonziniVMX tests were only running 64-bit guests. Add a test that checks the reserved and ignored bits in CR3 for legacy 2-level page tables.
--- x86/vmx_tests.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+)
x86/vmx_tests.c +66 -0
@@ -262,6 +262,38 @@ 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); +} +/* * 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[]
@@ -5922,6 +5954,39 @@ static void vmx_pae_test(void)test_set_guest_finished (); } +/* +* Verify that VM entry with 32-bit (non-PAE) paging succeeds and that bits +* 2:0 and 11:5 of CR3 are ignored. +*/ +static void vmx_pse_test (void ) +{ +u64 cr3; +int b; + +test_setup_32bit (); + + cr3 =vmx_32bit_build_page_tables (); + +vmx_32bit_guest_init_common (cr3,NULL ); +if (enter_guest_report_pass ()) +skip_exit_vmcall (); + +for (b =0 ; b <=11 ; b++) { +if (b ==3 || b ==4 ) +continue ; +vmx_32bit_guest_init_common (cr3 | (1ull << b),NULL ); +test_guest_state ("CR3 ignored bit" , false, (1ull << b),"bit" ); + } + +for (b =cpuid_maxphyaddr (); b <64 ; b++) { +vmx_32bit_guest_init_common (cr3 | (1ull << b),NULL ); +test_guest_state ("CR3 reserved bit" , true, (1ull << b),"bit" ); + } + +free_page (phys_to_virt (cr3)); +test_set_guest_finished (); +} +/* * Tests for VM-entry control fields */
@@ -12117,6 +12182,7 @@ struct vmx_test vmx_tests[] = {TEST (vmx_mtf_test),TEST (vmx_mtf_pdpte_test),TEST (vmx_pae_test), +TEST (vmx_pse_test),TEST (vmx_pf_exception_test),TEST (vmx_pf_exception_forced_emulation_test), TEST(vmx_pf_no_vpid_test),
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH v3 3/4 svm: add test for 32-bit PAE guest
In reply to Paolo BonziniSVM tests are only running 64-bit guests. Add a test that checks the reserved and ignored bits in CR3 and PDPTEs for PAE mode.
--- x86/svm_tests.c | 185 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+)
x86/svm_tests.c +185 -0
@@ -52,6 +52,52 @@ do { \ } \ }while (0 ) +static void svm_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 void svm_pae_free_page_tables (u64 *pdpt) +{ +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); +} + +static void svm_32bit_guest_init_common (u64 cr3,bool pae) +{ +struct guest_regs *regs =this_cpu_guest_regs (); +struct vmcb_save_area *save = &vmcb->save; + +/* +* 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 |X86_CR4_PSE ); + save->cr4 |= pae ?X86_CR4_PAE :X86_CR4_PSE ; + save->cr3 = cr3; + regs->rax =0x123457678 ; +} +static void null_test (struct svm_test *test) { }
@@ -3676,6 +3722,144 @@ static void svm_unsupported_instruction_intercept_test(void) } } +/* +* 32-bit guest tests running 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. +*/ +extern void hypercall_32bit_main (void ); +asm (".code32\n" +"hypercall_32bit_main:\n" +"vmmcall\n" +"jmp hypercall_32bit_main\n" +".code64\n" ); + +static void __test_svm_guest_state (const char *test,u64 expected_exit, +u64 field,const char *field_name) +{ +u64 got =__svm_vmrun ((u64 )hypercall_32bit_main); + +report (got == expected_exit, +"%s, %s = %lx (exit 0x%lx, wanted 0x%lx)" , + test, field_name, field, got, expected_exit); +} + +/* +* xfail = false: VMRUN is expected to succeed and the guest to exit at +* VMMCALL (exit code SVM_EXIT_VMMCALL). +* xfail = true: VMRUN itself is expected to abort with SVM_EXIT_ERR. +*/ +static void test_svm_guest_state (const char *test,bool xfail,u64 field, +const char *field_name) +{ +__test_svm_guest_state (test, xfail ?SVM_EXIT_ERR :SVM_EXIT_VMMCALL , + field, field_name); +} + +static void test_svm_report_pass (void ) +{ +u64 got =__svm_vmrun ((u64 )hypercall_32bit_main); + +report (got ==SVM_EXIT_VMMCALL , +"VM-entry succeeded (exit 0x%lx, wanted 0x%x)" , + got,SVM_EXIT_VMMCALL ); +} +/* +* Verify SVM's checks on guest PAE CR3 and PDPTEs. PDPTEs are always +* loaded from memory at CR3, but when NPT is enabled SVM does not +* check PDPTE reserved bits at VMRUN and VMRUN is expected to succeed. +*/ +static void test_svm_guest_pdptes (u64 cr3,u64 *pdpt) +{ +u64 orig; +int i, b; +bool npt = vmcb->control.nested_ctl &SVM_NESTED_ENABLE ; + +vmcb_set_intercept (INTERCEPT_EXCEPTION_OFFSET +PF_VECTOR ); + +/* Basic sanity: a clean PAE setup runs to VMMCALL. */ +svm_32bit_guest_init_common (cr3, true); +test_svm_report_pass (); + +/* CR3 bits 4:0 are ignored in PAE mode. */ +for (b =0 ; b <=4 ; b++) { +svm_32bit_guest_init_common (cr3 | (1ull << b), true); +test_svm_guest_state ("CR3 ignored bit" , false, (1ull << b),"bit" ); + } +for (b =cpuid_maxphyaddr (); b <=63 ; b++) { +svm_32bit_guest_init_common (cr3 | (1ull << b), true); +test_svm_guest_state ("CR3 reserved bit" , true, (1ull << b),"bit" ); + } + +for (i =0 ; i <4 ; i++) { +u64 reserved_exit; + +/* +* When a reserved bit is set in PDPTE0, the processor notices +* but only when the guest walks through it to fetch its first +* instruction. +*/ +if (!npt) + reserved_exit =SVM_EXIT_ERR ; +else if (i ==0 ) + reserved_exit =SVM_EXIT_EXCP_BASE +PF_VECTOR ; +else + reserved_exit =SVM_EXIT_VMMCALL ; + +report_prefix_pushf ("PDPTE%d" , i); + orig = pdpt[i]; + +/* +* Bits 62:52 of a PDPTE are reserved on VMX but are not +* checked by SVM at VMRUN. For PDPTE1..3 the guest does +* not walk through the entry, so VMRUN succeeds and the +* guest hits VMMCALL. For PDPTE0 the guest faults at +* instruction fetch and exits with #PF. +*/ +for (b =52 ; b <=62 ; b++) { + pdpt[i] = orig | (1ull << b); +svm_32bit_guest_init_common (cr3, true); +__test_svm_guest_state ("reserved bit" , reserved_exit, + (1ull << b),"bit" ); + } + +/* Bits 11:9 of a PDPTE are ignored. */ +for (b =9 ; b <=11 ; b++) { + pdpt[i] = orig | (1ull << b); +svm_32bit_guest_init_common (cr3, true); +test_svm_guest_state ("ignored bit" , false, + (1ull << b),"bit" ); + } + + pdpt[i] = orig; +report_prefix_pop (); + } + +vmcb_clear_intercept (INTERCEPT_EXCEPTION_OFFSET +PF_VECTOR ); +} + +/* +* Verify that VMRUN with PAE paging loads PDPTEs from memory at CR3, and +* that SVM does not check PDPTE reserved bits. +*/ +static void svm_pae_test (void ) +{ +u64 *pdpt; + +vmcb_ident (vmcb); + pdpt =memalign_pages_flags (PAGE_SIZE ,PAGE_SIZE ,AREA_DMA32 ); +svm_32bit_build_pae_page_tables (pdpt); + +/* +* 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. +*/ +test_svm_guest_pdptes (virt_to_phys (pdpt), pdpt); + +svm_pae_free_page_tables (pdpt); +} +struct svm_test svm_tests[] = { {"null" , default_supported, default_prepare, default_prepare_gif_clear, null_test,
@@ -3795,6 +3979,7 @@ struct svm_test svm_tests[] = { {"vgif" , vgif_supported, prepare_vgif_enabled, default_prepare_gif_clear, test_vgif, vgif_finished, vgif_check }, +TEST (svm_pae_test),TEST (svm_cr4_osxsave_test),TEST (svm_guest_state_test), TEST(svm_vmrun_errata_test),
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH v3 4/4 svm: add test for 32-bit non-PAE guest
In reply to Paolo BonziniVMX tests are only running 64-bit guests. Add a test that checks the reserved and ignored bits in CR3 for legacy two-level paging.
--- x86/svm_tests.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+)
x86/svm_tests.c +41 -0
@@ -52,6 +52,17 @@ do { \ } \ }while (0 ) +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_build_pae_page_tables (u64 *pdpt) {const u64 leaf_flags =PT_PRESENT_MASK |PT_WRITABLE_MASK |PT_PAGE_SIZE_MASK ;
@@ -3860,6 +3871,35 @@ static void svm_pae_test(void)svm_pae_free_page_tables (pdpt); } +/* +* Verify that VMRUN with 32-bit (non-PAE) paging succeeds and that bits +* 2:0 and 11:5 of CR3 are ignored. +*/ +static void svm_pse_test (void ) +{ +u64 cr3; +int b; + +vmcb_ident (vmcb); + cr3 =svm_32bit_build_page_tables (); + +svm_32bit_guest_init_common (cr3, false); +test_svm_report_pass (); + +for (b =0 ; b <=11 ; b++) { +if (b ==3 || b ==4 ) +continue ; +svm_32bit_guest_init_common (cr3 | (1ull << b), false); +test_svm_guest_state ("CR3 ignored bit" , false, (1ull << b),"bit" ); + } +for (b =cpuid_maxphyaddr (); b <=63 ; b++) { +svm_32bit_guest_init_common (cr3 | (1ull << b), false); +test_svm_guest_state ("CR3 reserved bit" , true, (1ull << b),"bit" ); + } + +free_page (phys_to_virt (cr3)); +} +struct svm_test svm_tests[] = { {"null" , default_supported, default_prepare, default_prepare_gif_clear, null_test,
@@ -3980,6 +4020,7 @@ struct svm_test svm_tests[] = { default_prepare_gif_clear, test_vgif, vgif_finished, vgif_check },TEST (svm_pae_test), +TEST (svm_pse_test),TEST (svm_cr4_osxsave_test),TEST (svm_guest_state_test), TEST(svm_vmrun_errata_test),
Signature · 1 line
2.52.0