mirror of
https://github.com/torvalds/linux.git
synced 2026-05-28 00:53:34 +02:00
KVM selftests changes for 6.19:
- Fix a math goof in mmu_stress_test when running on a single-CPU system/VM.
- Forcefully override ARCH from x86_64 to x86 to play nice with specifying
ARCH=x86_64 on the command line.
- Extend a bunch of nested VMX to validate nested SVM as well.
- Add support for LA57 in the core VM_MODE_xxx macro, and add a test to
verify KVM can save/restore nested VMX state when L1 is using 5-level
paging, but L2 is not.
- Clean up the guest paging code in anticipation of sharing the core logic for
nested EPT and nested NPT.
-----BEGIN PGP SIGNATURE-----
iQIzBAABCgAdFiEEKTobbabEP7vbhhN9OlYIJqCjN/0FAmkmUnIACgkQOlYIJqCj
N/0+Zg/+NOklTP01cgj49RDkkYyk43UQBf3VdwL72Yzq4KSHdZMx4Zggj/GbmtI6
tioIbhprSPvEw880KkSzJTh+BeJHUo0sZT23hG65AeJdWGp/nMo95SHRiW6uB5mX
g/R6RLsCUyu97rzuL+6MI9P46hBfXHCMvUh55M08UL3WAV3kX3wTh+9JSmW4e+Wc
d9C+0vtZ2GcODGaD++GVlkVt+cSqPHKhEq9KTrbgIFY5oQKuPpuxaB0HshfBQ79L
LPJozoA9qJ6fXUFWAt0QGyAXdYMg8Q1sG5PdnLwKvWUBXxYB2JMIbwFIhQqFr/NE
lZ9bxrtIclQaNrtmpYCufzyNcWl93YserfS3CJY0OtnOW/2Tcic1WEdlw0cUTgpJ
INKjwzu4CleevxpVsymHz7grZy/3qHcA8c0liX88GTk/o9r/wtK9Hgh6FpVo/6Gx
DEbyZEw5vNg1mEGFIXtHxHbuyVqoR8/HLsPhMaMSL1R6A4fqVNAFjjxnydSKsUOr
5gbqZp4R4iRan48iYrsgcb4gQ5bAXzedCEBzVnX7Gxp3wCW56YHqTMBJumBV2Dji
jdYUKUgV0TCP2xgGaA5HRrTicxHXsr/wS2IojgoqCouvtHhrsDS036DhL2Wm4nf+
f1I9nAaLdDxFhfB45I2CWjVj4KWkEZMUnr5vTbtWuwiZmVo3vMM=
=vqnp
-----END PGP SIGNATURE-----
Merge tag 'kvm-x86-selftests-6.19' of https://github.com/kvm-x86/linux into HEAD
KVM selftests changes for 6.19:
- Fix a math goof in mmu_stress_test when running on a single-CPU system/VM.
- Forcefully override ARCH from x86_64 to x86 to play nice with specifying
ARCH=x86_64 on the command line.
- Extend a bunch of nested VMX to validate nested SVM as well.
- Add support for LA57 in the core VM_MODE_xxx macro, and add a test to
verify KVM can save/restore nested VMX state when L1 is using 5-level
paging, but L2 is not.
- Clean up the guest paging code in anticipation of sharing the core logic for
nested EPT and nested NPT.
This commit is contained in:
commit
b0bf3d67a7
|
|
@ -6,7 +6,7 @@ ARCH ?= $(SUBARCH)
|
|||
ifeq ($(ARCH),$(filter $(ARCH),arm64 s390 riscv x86 x86_64 loongarch))
|
||||
# Top-level selftests allows ARCH=x86_64 :-(
|
||||
ifeq ($(ARCH),x86_64)
|
||||
ARCH := x86
|
||||
override ARCH := x86
|
||||
endif
|
||||
include Makefile.kvm
|
||||
else
|
||||
|
|
|
|||
|
|
@ -88,8 +88,12 @@ TEST_GEN_PROGS_x86 += x86/kvm_pv_test
|
|||
TEST_GEN_PROGS_x86 += x86/kvm_buslock_test
|
||||
TEST_GEN_PROGS_x86 += x86/monitor_mwait_test
|
||||
TEST_GEN_PROGS_x86 += x86/msrs_test
|
||||
TEST_GEN_PROGS_x86 += x86/nested_close_kvm_test
|
||||
TEST_GEN_PROGS_x86 += x86/nested_emulation_test
|
||||
TEST_GEN_PROGS_x86 += x86/nested_exceptions_test
|
||||
TEST_GEN_PROGS_x86 += x86/nested_invalid_cr3_test
|
||||
TEST_GEN_PROGS_x86 += x86/nested_tsc_adjust_test
|
||||
TEST_GEN_PROGS_x86 += x86/nested_tsc_scaling_test
|
||||
TEST_GEN_PROGS_x86 += x86/platform_info_test
|
||||
TEST_GEN_PROGS_x86 += x86/pmu_counters_test
|
||||
TEST_GEN_PROGS_x86 += x86/pmu_event_filter_test
|
||||
|
|
@ -111,14 +115,12 @@ TEST_GEN_PROGS_x86 += x86/ucna_injection_test
|
|||
TEST_GEN_PROGS_x86 += x86/userspace_io_test
|
||||
TEST_GEN_PROGS_x86 += x86/userspace_msr_exit_test
|
||||
TEST_GEN_PROGS_x86 += x86/vmx_apic_access_test
|
||||
TEST_GEN_PROGS_x86 += x86/vmx_close_while_nested_test
|
||||
TEST_GEN_PROGS_x86 += x86/vmx_dirty_log_test
|
||||
TEST_GEN_PROGS_x86 += x86/vmx_exception_with_invalid_guest_state
|
||||
TEST_GEN_PROGS_x86 += x86/vmx_msrs_test
|
||||
TEST_GEN_PROGS_x86 += x86/vmx_invalid_nested_guest_state
|
||||
TEST_GEN_PROGS_x86 += x86/vmx_nested_la57_state_test
|
||||
TEST_GEN_PROGS_x86 += x86/vmx_set_nested_state_test
|
||||
TEST_GEN_PROGS_x86 += x86/vmx_tsc_adjust_test
|
||||
TEST_GEN_PROGS_x86 += x86/vmx_nested_tsc_scaling_test
|
||||
TEST_GEN_PROGS_x86 += x86/apic_bus_clock_test
|
||||
TEST_GEN_PROGS_x86 += x86/xapic_ipi_test
|
||||
TEST_GEN_PROGS_x86 += x86/xapic_state_test
|
||||
|
|
|
|||
|
|
@ -178,7 +178,7 @@ enum vm_guest_mode {
|
|||
VM_MODE_P40V48_4K,
|
||||
VM_MODE_P40V48_16K,
|
||||
VM_MODE_P40V48_64K,
|
||||
VM_MODE_PXXV48_4K, /* For 48bits VA but ANY bits PA */
|
||||
VM_MODE_PXXVYY_4K, /* For 48-bit or 57-bit VA, depending on host support */
|
||||
VM_MODE_P47V64_4K,
|
||||
VM_MODE_P44V64_4K,
|
||||
VM_MODE_P36V48_4K,
|
||||
|
|
@ -220,7 +220,7 @@ extern enum vm_guest_mode vm_mode_default;
|
|||
|
||||
#elif defined(__x86_64__)
|
||||
|
||||
#define VM_MODE_DEFAULT VM_MODE_PXXV48_4K
|
||||
#define VM_MODE_DEFAULT VM_MODE_PXXVYY_4K
|
||||
#define MIN_PAGE_SHIFT 12U
|
||||
#define ptes_per_page(page_size) ((page_size) / 8)
|
||||
|
||||
|
|
@ -1203,6 +1203,7 @@ void virt_arch_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr);
|
|||
static inline void virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr)
|
||||
{
|
||||
virt_arch_pg_map(vm, vaddr, paddr);
|
||||
sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1441,7 +1441,7 @@ enum pg_level {
|
|||
PG_LEVEL_2M,
|
||||
PG_LEVEL_1G,
|
||||
PG_LEVEL_512G,
|
||||
PG_LEVEL_NUM
|
||||
PG_LEVEL_256T
|
||||
};
|
||||
|
||||
#define PG_LEVEL_SHIFT(_level) ((_level - 1) * 9 + 12)
|
||||
|
|
|
|||
|
|
@ -568,8 +568,7 @@ void nested_map_memslot(struct vmx_pages *vmx, struct kvm_vm *vm,
|
|||
void nested_identity_map_1g(struct vmx_pages *vmx, struct kvm_vm *vm,
|
||||
uint64_t addr, uint64_t size);
|
||||
bool kvm_cpu_has_ept(void);
|
||||
void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm,
|
||||
uint32_t eptp_memslot);
|
||||
void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm);
|
||||
void prepare_virtualize_apic_accesses(struct vmx_pages *vmx, struct kvm_vm *vm);
|
||||
|
||||
#endif /* SELFTEST_KVM_VMX_H */
|
||||
|
|
|
|||
|
|
@ -324,7 +324,7 @@ void aarch64_vcpu_setup(struct kvm_vcpu *vcpu, struct kvm_vcpu_init *init)
|
|||
|
||||
/* Configure base granule size */
|
||||
switch (vm->mode) {
|
||||
case VM_MODE_PXXV48_4K:
|
||||
case VM_MODE_PXXVYY_4K:
|
||||
TEST_FAIL("AArch64 does not support 4K sized pages "
|
||||
"with ANY-bit physical address ranges");
|
||||
case VM_MODE_P52V48_64K:
|
||||
|
|
|
|||
|
|
@ -201,7 +201,7 @@ const char *vm_guest_mode_string(uint32_t i)
|
|||
[VM_MODE_P40V48_4K] = "PA-bits:40, VA-bits:48, 4K pages",
|
||||
[VM_MODE_P40V48_16K] = "PA-bits:40, VA-bits:48, 16K pages",
|
||||
[VM_MODE_P40V48_64K] = "PA-bits:40, VA-bits:48, 64K pages",
|
||||
[VM_MODE_PXXV48_4K] = "PA-bits:ANY, VA-bits:48, 4K pages",
|
||||
[VM_MODE_PXXVYY_4K] = "PA-bits:ANY, VA-bits:48 or 57, 4K pages",
|
||||
[VM_MODE_P47V64_4K] = "PA-bits:47, VA-bits:64, 4K pages",
|
||||
[VM_MODE_P44V64_4K] = "PA-bits:44, VA-bits:64, 4K pages",
|
||||
[VM_MODE_P36V48_4K] = "PA-bits:36, VA-bits:48, 4K pages",
|
||||
|
|
@ -228,7 +228,7 @@ const struct vm_guest_mode_params vm_guest_mode_params[] = {
|
|||
[VM_MODE_P40V48_4K] = { 40, 48, 0x1000, 12 },
|
||||
[VM_MODE_P40V48_16K] = { 40, 48, 0x4000, 14 },
|
||||
[VM_MODE_P40V48_64K] = { 40, 48, 0x10000, 16 },
|
||||
[VM_MODE_PXXV48_4K] = { 0, 0, 0x1000, 12 },
|
||||
[VM_MODE_PXXVYY_4K] = { 0, 0, 0x1000, 12 },
|
||||
[VM_MODE_P47V64_4K] = { 47, 64, 0x1000, 12 },
|
||||
[VM_MODE_P44V64_4K] = { 44, 64, 0x1000, 12 },
|
||||
[VM_MODE_P36V48_4K] = { 36, 48, 0x1000, 12 },
|
||||
|
|
@ -310,24 +310,26 @@ struct kvm_vm *____vm_create(struct vm_shape shape)
|
|||
case VM_MODE_P36V47_16K:
|
||||
vm->pgtable_levels = 3;
|
||||
break;
|
||||
case VM_MODE_PXXV48_4K:
|
||||
case VM_MODE_PXXVYY_4K:
|
||||
#ifdef __x86_64__
|
||||
kvm_get_cpu_address_width(&vm->pa_bits, &vm->va_bits);
|
||||
kvm_init_vm_address_properties(vm);
|
||||
/*
|
||||
* Ignore KVM support for 5-level paging (vm->va_bits == 57),
|
||||
* it doesn't take effect unless a CR4.LA57 is set, which it
|
||||
* isn't for this mode (48-bit virtual address space).
|
||||
*/
|
||||
TEST_ASSERT(vm->va_bits == 48 || vm->va_bits == 57,
|
||||
"Linear address width (%d bits) not supported",
|
||||
vm->va_bits);
|
||||
|
||||
pr_debug("Guest physical address width detected: %d\n",
|
||||
vm->pa_bits);
|
||||
vm->pgtable_levels = 4;
|
||||
vm->va_bits = 48;
|
||||
pr_debug("Guest virtual address width detected: %d\n",
|
||||
vm->va_bits);
|
||||
|
||||
if (vm->va_bits == 57) {
|
||||
vm->pgtable_levels = 5;
|
||||
} else {
|
||||
TEST_ASSERT(vm->va_bits == 48,
|
||||
"Unexpected guest virtual address width: %d",
|
||||
vm->va_bits);
|
||||
vm->pgtable_levels = 4;
|
||||
}
|
||||
#else
|
||||
TEST_FAIL("VM_MODE_PXXV48_4K not supported on non-x86 platforms");
|
||||
TEST_FAIL("VM_MODE_PXXVYY_4K not supported on non-x86 platforms");
|
||||
#endif
|
||||
break;
|
||||
case VM_MODE_P47V64_4K:
|
||||
|
|
@ -1437,8 +1439,6 @@ static vm_vaddr_t ____vm_vaddr_alloc(struct kvm_vm *vm, size_t sz,
|
|||
pages--, vaddr += vm->page_size, paddr += vm->page_size) {
|
||||
|
||||
virt_pg_map(vm, vaddr, paddr);
|
||||
|
||||
sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift);
|
||||
}
|
||||
|
||||
return vaddr_start;
|
||||
|
|
@ -1552,7 +1552,6 @@ void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
|
|||
|
||||
while (npages--) {
|
||||
virt_pg_map(vm, vaddr, paddr);
|
||||
sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift);
|
||||
|
||||
vaddr += page_size;
|
||||
paddr += page_size;
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ void memstress_setup_ept(struct vmx_pages *vmx, struct kvm_vm *vm)
|
|||
{
|
||||
uint64_t start, end;
|
||||
|
||||
prepare_eptp(vmx, vm, 0);
|
||||
prepare_eptp(vmx, vm);
|
||||
|
||||
/*
|
||||
* Identity map the first 4G and the test region with 1G pages so that
|
||||
|
|
|
|||
|
|
@ -158,10 +158,10 @@ bool kvm_is_tdp_enabled(void)
|
|||
|
||||
void virt_arch_pgd_alloc(struct kvm_vm *vm)
|
||||
{
|
||||
TEST_ASSERT(vm->mode == VM_MODE_PXXV48_4K, "Attempt to use "
|
||||
"unknown or unsupported guest mode, mode: 0x%x", vm->mode);
|
||||
TEST_ASSERT(vm->mode == VM_MODE_PXXVYY_4K,
|
||||
"Unknown or unsupported guest mode: 0x%x", vm->mode);
|
||||
|
||||
/* If needed, create page map l4 table. */
|
||||
/* If needed, create the top-level page table. */
|
||||
if (!vm->pgd_created) {
|
||||
vm->pgd = vm_alloc_page_table(vm);
|
||||
vm->pgd_created = true;
|
||||
|
|
@ -218,11 +218,11 @@ static uint64_t *virt_create_upper_pte(struct kvm_vm *vm,
|
|||
void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level)
|
||||
{
|
||||
const uint64_t pg_size = PG_LEVEL_SIZE(level);
|
||||
uint64_t *pml4e, *pdpe, *pde;
|
||||
uint64_t *pte;
|
||||
uint64_t *pte = &vm->pgd;
|
||||
int current_level;
|
||||
|
||||
TEST_ASSERT(vm->mode == VM_MODE_PXXV48_4K,
|
||||
"Unknown or unsupported guest mode, mode: 0x%x", vm->mode);
|
||||
TEST_ASSERT(vm->mode == VM_MODE_PXXVYY_4K,
|
||||
"Unknown or unsupported guest mode: 0x%x", vm->mode);
|
||||
|
||||
TEST_ASSERT((vaddr % pg_size) == 0,
|
||||
"Virtual address not aligned,\n"
|
||||
|
|
@ -243,20 +243,17 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level)
|
|||
* Allocate upper level page tables, if not already present. Return
|
||||
* early if a hugepage was created.
|
||||
*/
|
||||
pml4e = virt_create_upper_pte(vm, &vm->pgd, vaddr, paddr, PG_LEVEL_512G, level);
|
||||
if (*pml4e & PTE_LARGE_MASK)
|
||||
return;
|
||||
|
||||
pdpe = virt_create_upper_pte(vm, pml4e, vaddr, paddr, PG_LEVEL_1G, level);
|
||||
if (*pdpe & PTE_LARGE_MASK)
|
||||
return;
|
||||
|
||||
pde = virt_create_upper_pte(vm, pdpe, vaddr, paddr, PG_LEVEL_2M, level);
|
||||
if (*pde & PTE_LARGE_MASK)
|
||||
return;
|
||||
for (current_level = vm->pgtable_levels;
|
||||
current_level > PG_LEVEL_4K;
|
||||
current_level--) {
|
||||
pte = virt_create_upper_pte(vm, pte, vaddr, paddr,
|
||||
current_level, level);
|
||||
if (*pte & PTE_LARGE_MASK)
|
||||
return;
|
||||
}
|
||||
|
||||
/* Fill in page table entry. */
|
||||
pte = virt_get_pte(vm, pde, vaddr, PG_LEVEL_4K);
|
||||
pte = virt_get_pte(vm, pte, vaddr, PG_LEVEL_4K);
|
||||
TEST_ASSERT(!(*pte & PTE_PRESENT_MASK),
|
||||
"PTE already present for 4k page at vaddr: 0x%lx", vaddr);
|
||||
*pte = PTE_PRESENT_MASK | PTE_WRITABLE_MASK | (paddr & PHYSICAL_PAGE_MASK);
|
||||
|
|
@ -289,6 +286,8 @@ void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
|
|||
|
||||
for (i = 0; i < nr_pages; i++) {
|
||||
__virt_pg_map(vm, vaddr, paddr, level);
|
||||
sparsebit_set_num(vm->vpages_mapped, vaddr >> vm->page_shift,
|
||||
nr_bytes / PAGE_SIZE);
|
||||
|
||||
vaddr += pg_size;
|
||||
paddr += pg_size;
|
||||
|
|
@ -310,40 +309,38 @@ static bool vm_is_target_pte(uint64_t *pte, int *level, int current_level)
|
|||
uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr,
|
||||
int *level)
|
||||
{
|
||||
uint64_t *pml4e, *pdpe, *pde;
|
||||
int va_width = 12 + (vm->pgtable_levels) * 9;
|
||||
uint64_t *pte = &vm->pgd;
|
||||
int current_level;
|
||||
|
||||
TEST_ASSERT(!vm->arch.is_pt_protected,
|
||||
"Walking page tables of protected guests is impossible");
|
||||
|
||||
TEST_ASSERT(*level >= PG_LEVEL_NONE && *level < PG_LEVEL_NUM,
|
||||
TEST_ASSERT(*level >= PG_LEVEL_NONE && *level <= vm->pgtable_levels,
|
||||
"Invalid PG_LEVEL_* '%d'", *level);
|
||||
|
||||
TEST_ASSERT(vm->mode == VM_MODE_PXXV48_4K, "Attempt to use "
|
||||
"unknown or unsupported guest mode, mode: 0x%x", vm->mode);
|
||||
TEST_ASSERT(vm->mode == VM_MODE_PXXVYY_4K,
|
||||
"Unknown or unsupported guest mode: 0x%x", vm->mode);
|
||||
TEST_ASSERT(sparsebit_is_set(vm->vpages_valid,
|
||||
(vaddr >> vm->page_shift)),
|
||||
"Invalid virtual address, vaddr: 0x%lx",
|
||||
vaddr);
|
||||
/*
|
||||
* Based on the mode check above there are 48 bits in the vaddr, so
|
||||
* shift 16 to sign extend the last bit (bit-47),
|
||||
* Check that the vaddr is a sign-extended va_width value.
|
||||
*/
|
||||
TEST_ASSERT(vaddr == (((int64_t)vaddr << 16) >> 16),
|
||||
"Canonical check failed. The virtual address is invalid.");
|
||||
TEST_ASSERT(vaddr ==
|
||||
(((int64_t)vaddr << (64 - va_width) >> (64 - va_width))),
|
||||
"Canonical check failed. The virtual address is invalid.");
|
||||
|
||||
pml4e = virt_get_pte(vm, &vm->pgd, vaddr, PG_LEVEL_512G);
|
||||
if (vm_is_target_pte(pml4e, level, PG_LEVEL_512G))
|
||||
return pml4e;
|
||||
for (current_level = vm->pgtable_levels;
|
||||
current_level > PG_LEVEL_4K;
|
||||
current_level--) {
|
||||
pte = virt_get_pte(vm, pte, vaddr, current_level);
|
||||
if (vm_is_target_pte(pte, level, current_level))
|
||||
return pte;
|
||||
}
|
||||
|
||||
pdpe = virt_get_pte(vm, pml4e, vaddr, PG_LEVEL_1G);
|
||||
if (vm_is_target_pte(pdpe, level, PG_LEVEL_1G))
|
||||
return pdpe;
|
||||
|
||||
pde = virt_get_pte(vm, pdpe, vaddr, PG_LEVEL_2M);
|
||||
if (vm_is_target_pte(pde, level, PG_LEVEL_2M))
|
||||
return pde;
|
||||
|
||||
return virt_get_pte(vm, pde, vaddr, PG_LEVEL_4K);
|
||||
return virt_get_pte(vm, pte, vaddr, PG_LEVEL_4K);
|
||||
}
|
||||
|
||||
uint64_t *vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr)
|
||||
|
|
@ -526,7 +523,8 @@ static void vcpu_init_sregs(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
|
|||
{
|
||||
struct kvm_sregs sregs;
|
||||
|
||||
TEST_ASSERT_EQ(vm->mode, VM_MODE_PXXV48_4K);
|
||||
TEST_ASSERT(vm->mode == VM_MODE_PXXVYY_4K,
|
||||
"Unknown or unsupported guest mode: 0x%x", vm->mode);
|
||||
|
||||
/* Set mode specific system register values. */
|
||||
vcpu_sregs_get(vcpu, &sregs);
|
||||
|
|
@ -540,6 +538,8 @@ static void vcpu_init_sregs(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
|
|||
sregs.cr4 |= X86_CR4_PAE | X86_CR4_OSFXSR;
|
||||
if (kvm_cpu_has(X86_FEATURE_XSAVE))
|
||||
sregs.cr4 |= X86_CR4_OSXSAVE;
|
||||
if (vm->pgtable_levels == 5)
|
||||
sregs.cr4 |= X86_CR4_LA57;
|
||||
sregs.efer |= (EFER_LME | EFER_LMA | EFER_NX);
|
||||
|
||||
kvm_seg_set_unusable(&sregs.ldt);
|
||||
|
|
|
|||
|
|
@ -401,11 +401,11 @@ void __nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm,
|
|||
struct eptPageTableEntry *pt = vmx->eptp_hva, *pte;
|
||||
uint16_t index;
|
||||
|
||||
TEST_ASSERT(vm->mode == VM_MODE_PXXV48_4K, "Attempt to use "
|
||||
"unknown or unsupported guest mode, mode: 0x%x", vm->mode);
|
||||
TEST_ASSERT(vm->mode == VM_MODE_PXXVYY_4K,
|
||||
"Unknown or unsupported guest mode: 0x%x", vm->mode);
|
||||
|
||||
TEST_ASSERT((nested_paddr >> 48) == 0,
|
||||
"Nested physical address 0x%lx requires 5-level paging",
|
||||
"Nested physical address 0x%lx is > 48-bits and requires 5-level EPT",
|
||||
nested_paddr);
|
||||
TEST_ASSERT((nested_paddr % page_size) == 0,
|
||||
"Nested physical address not on page boundary,\n"
|
||||
|
|
@ -534,8 +534,7 @@ bool kvm_cpu_has_ept(void)
|
|||
return ctrl & SECONDARY_EXEC_ENABLE_EPT;
|
||||
}
|
||||
|
||||
void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm,
|
||||
uint32_t eptp_memslot)
|
||||
void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm)
|
||||
{
|
||||
TEST_ASSERT(kvm_cpu_has_ept(), "KVM doesn't support nested EPT");
|
||||
|
||||
|
|
|
|||
|
|
@ -263,8 +263,10 @@ static void calc_default_nr_vcpus(void)
|
|||
TEST_ASSERT(!r, "sched_getaffinity failed, errno = %d (%s)",
|
||||
errno, strerror(errno));
|
||||
|
||||
nr_vcpus = CPU_COUNT(&possible_mask) * 3/4;
|
||||
nr_vcpus = CPU_COUNT(&possible_mask);
|
||||
TEST_ASSERT(nr_vcpus > 0, "Uh, no CPUs?");
|
||||
if (nr_vcpus >= 2)
|
||||
nr_vcpus = nr_vcpus * 3/4;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
|
|
@ -360,11 +362,9 @@ int main(int argc, char *argv[])
|
|||
|
||||
#ifdef __x86_64__
|
||||
/* Identity map memory in the guest using 1gb pages. */
|
||||
for (i = 0; i < slot_size; i += SZ_1G)
|
||||
__virt_pg_map(vm, gpa + i, gpa + i, PG_LEVEL_1G);
|
||||
virt_map_level(vm, gpa, gpa, slot_size, PG_LEVEL_1G);
|
||||
#else
|
||||
for (i = 0; i < slot_size; i += vm->page_size)
|
||||
virt_pg_map(vm, gpa + i, gpa + i);
|
||||
virt_map(vm, gpa, gpa, slot_size >> vm->page_shift);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,13 +17,13 @@
|
|||
#define TEST_NPAGES (TEST_SIZE / PAGE_SIZE)
|
||||
#define TEST_SLOT 10
|
||||
|
||||
static void guest_code(uint64_t base_gpa)
|
||||
static void guest_code(uint64_t base_gva)
|
||||
{
|
||||
volatile uint64_t val __used;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < TEST_NPAGES; i++) {
|
||||
uint64_t *src = (uint64_t *)(base_gpa + i * PAGE_SIZE);
|
||||
uint64_t *src = (uint64_t *)(base_gva + i * PAGE_SIZE);
|
||||
|
||||
val = *src;
|
||||
}
|
||||
|
|
@ -161,6 +161,7 @@ static void pre_fault_memory(struct kvm_vcpu *vcpu, u64 base_gpa, u64 offset,
|
|||
|
||||
static void __test_pre_fault_memory(unsigned long vm_type, bool private)
|
||||
{
|
||||
uint64_t gpa, gva, alignment, guest_page_size;
|
||||
const struct vm_shape shape = {
|
||||
.mode = VM_MODE_DEFAULT,
|
||||
.type = vm_type,
|
||||
|
|
@ -170,35 +171,30 @@ static void __test_pre_fault_memory(unsigned long vm_type, bool private)
|
|||
struct kvm_vm *vm;
|
||||
struct ucall uc;
|
||||
|
||||
uint64_t guest_test_phys_mem;
|
||||
uint64_t guest_test_virt_mem;
|
||||
uint64_t alignment, guest_page_size;
|
||||
|
||||
vm = vm_create_shape_with_one_vcpu(shape, &vcpu, guest_code);
|
||||
|
||||
alignment = guest_page_size = vm_guest_mode_params[VM_MODE_DEFAULT].page_size;
|
||||
guest_test_phys_mem = (vm->max_gfn - TEST_NPAGES) * guest_page_size;
|
||||
gpa = (vm->max_gfn - TEST_NPAGES) * guest_page_size;
|
||||
#ifdef __s390x__
|
||||
alignment = max(0x100000UL, guest_page_size);
|
||||
#else
|
||||
alignment = SZ_2M;
|
||||
#endif
|
||||
guest_test_phys_mem = align_down(guest_test_phys_mem, alignment);
|
||||
guest_test_virt_mem = guest_test_phys_mem & ((1ULL << (vm->va_bits - 1)) - 1);
|
||||
gpa = align_down(gpa, alignment);
|
||||
gva = gpa & ((1ULL << (vm->va_bits - 1)) - 1);
|
||||
|
||||
vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
|
||||
guest_test_phys_mem, TEST_SLOT, TEST_NPAGES,
|
||||
private ? KVM_MEM_GUEST_MEMFD : 0);
|
||||
virt_map(vm, guest_test_virt_mem, guest_test_phys_mem, TEST_NPAGES);
|
||||
vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa, TEST_SLOT,
|
||||
TEST_NPAGES, private ? KVM_MEM_GUEST_MEMFD : 0);
|
||||
virt_map(vm, gva, gpa, TEST_NPAGES);
|
||||
|
||||
if (private)
|
||||
vm_mem_set_private(vm, guest_test_phys_mem, TEST_SIZE);
|
||||
vm_mem_set_private(vm, gpa, TEST_SIZE);
|
||||
|
||||
pre_fault_memory(vcpu, guest_test_phys_mem, 0, SZ_2M, 0, private);
|
||||
pre_fault_memory(vcpu, guest_test_phys_mem, SZ_2M, PAGE_SIZE * 2, PAGE_SIZE, private);
|
||||
pre_fault_memory(vcpu, guest_test_phys_mem, TEST_SIZE, PAGE_SIZE, PAGE_SIZE, private);
|
||||
pre_fault_memory(vcpu, gpa, 0, SZ_2M, 0, private);
|
||||
pre_fault_memory(vcpu, gpa, SZ_2M, PAGE_SIZE * 2, PAGE_SIZE, private);
|
||||
pre_fault_memory(vcpu, gpa, TEST_SIZE, PAGE_SIZE, PAGE_SIZE, private);
|
||||
|
||||
vcpu_args_set(vcpu, 1, guest_test_virt_mem);
|
||||
vcpu_args_set(vcpu, 1, gva);
|
||||
vcpu_run(vcpu);
|
||||
|
||||
run = vcpu->run;
|
||||
|
|
|
|||
|
|
@ -94,7 +94,7 @@ static void guest_hcall(vm_vaddr_t pgs_gpa, struct hcall_data *hcall)
|
|||
|
||||
if (!(hcall->control & HV_HYPERCALL_FAST_BIT)) {
|
||||
input = pgs_gpa;
|
||||
output = pgs_gpa + 4096;
|
||||
output = pgs_gpa + PAGE_SIZE;
|
||||
} else {
|
||||
input = output = 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ static void sender_guest_code(void *hcall_page, vm_vaddr_t pgs_gpa)
|
|||
/* 'Slow' HvCallSendSyntheticClusterIpi to RECEIVER_VCPU_ID_1 */
|
||||
ipi->vector = IPI_VECTOR;
|
||||
ipi->cpu_mask = 1 << RECEIVER_VCPU_ID_1;
|
||||
hyperv_hypercall(HVCALL_SEND_IPI, pgs_gpa, pgs_gpa + 4096);
|
||||
hyperv_hypercall(HVCALL_SEND_IPI, pgs_gpa, pgs_gpa + PAGE_SIZE);
|
||||
nop_loop();
|
||||
GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ++ipis_expected[0]);
|
||||
GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ipis_expected[1]);
|
||||
|
|
@ -116,13 +116,13 @@ static void sender_guest_code(void *hcall_page, vm_vaddr_t pgs_gpa)
|
|||
GUEST_SYNC(stage++);
|
||||
|
||||
/* 'Slow' HvCallSendSyntheticClusterIpiEx to RECEIVER_VCPU_ID_1 */
|
||||
memset(hcall_page, 0, 4096);
|
||||
memset(hcall_page, 0, PAGE_SIZE);
|
||||
ipi_ex->vector = IPI_VECTOR;
|
||||
ipi_ex->vp_set.format = HV_GENERIC_SET_SPARSE_4K;
|
||||
ipi_ex->vp_set.valid_bank_mask = 1 << 0;
|
||||
ipi_ex->vp_set.bank_contents[0] = BIT(RECEIVER_VCPU_ID_1);
|
||||
hyperv_hypercall(HVCALL_SEND_IPI_EX | (1 << HV_HYPERCALL_VARHEAD_OFFSET),
|
||||
pgs_gpa, pgs_gpa + 4096);
|
||||
pgs_gpa, pgs_gpa + PAGE_SIZE);
|
||||
nop_loop();
|
||||
GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ++ipis_expected[0]);
|
||||
GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ipis_expected[1]);
|
||||
|
|
@ -138,13 +138,13 @@ static void sender_guest_code(void *hcall_page, vm_vaddr_t pgs_gpa)
|
|||
GUEST_SYNC(stage++);
|
||||
|
||||
/* 'Slow' HvCallSendSyntheticClusterIpiEx to RECEIVER_VCPU_ID_2 */
|
||||
memset(hcall_page, 0, 4096);
|
||||
memset(hcall_page, 0, PAGE_SIZE);
|
||||
ipi_ex->vector = IPI_VECTOR;
|
||||
ipi_ex->vp_set.format = HV_GENERIC_SET_SPARSE_4K;
|
||||
ipi_ex->vp_set.valid_bank_mask = 1 << 1;
|
||||
ipi_ex->vp_set.bank_contents[0] = BIT(RECEIVER_VCPU_ID_2 - 64);
|
||||
hyperv_hypercall(HVCALL_SEND_IPI_EX | (1 << HV_HYPERCALL_VARHEAD_OFFSET),
|
||||
pgs_gpa, pgs_gpa + 4096);
|
||||
pgs_gpa, pgs_gpa + PAGE_SIZE);
|
||||
nop_loop();
|
||||
GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ipis_expected[0]);
|
||||
GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ++ipis_expected[1]);
|
||||
|
|
@ -160,14 +160,14 @@ static void sender_guest_code(void *hcall_page, vm_vaddr_t pgs_gpa)
|
|||
GUEST_SYNC(stage++);
|
||||
|
||||
/* 'Slow' HvCallSendSyntheticClusterIpiEx to both RECEIVER_VCPU_ID_{1,2} */
|
||||
memset(hcall_page, 0, 4096);
|
||||
memset(hcall_page, 0, PAGE_SIZE);
|
||||
ipi_ex->vector = IPI_VECTOR;
|
||||
ipi_ex->vp_set.format = HV_GENERIC_SET_SPARSE_4K;
|
||||
ipi_ex->vp_set.valid_bank_mask = 1 << 1 | 1;
|
||||
ipi_ex->vp_set.bank_contents[0] = BIT(RECEIVER_VCPU_ID_1);
|
||||
ipi_ex->vp_set.bank_contents[1] = BIT(RECEIVER_VCPU_ID_2 - 64);
|
||||
hyperv_hypercall(HVCALL_SEND_IPI_EX | (2 << HV_HYPERCALL_VARHEAD_OFFSET),
|
||||
pgs_gpa, pgs_gpa + 4096);
|
||||
pgs_gpa, pgs_gpa + PAGE_SIZE);
|
||||
nop_loop();
|
||||
GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ++ipis_expected[0]);
|
||||
GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ++ipis_expected[1]);
|
||||
|
|
@ -183,10 +183,10 @@ static void sender_guest_code(void *hcall_page, vm_vaddr_t pgs_gpa)
|
|||
GUEST_SYNC(stage++);
|
||||
|
||||
/* 'Slow' HvCallSendSyntheticClusterIpiEx to HV_GENERIC_SET_ALL */
|
||||
memset(hcall_page, 0, 4096);
|
||||
memset(hcall_page, 0, PAGE_SIZE);
|
||||
ipi_ex->vector = IPI_VECTOR;
|
||||
ipi_ex->vp_set.format = HV_GENERIC_SET_ALL;
|
||||
hyperv_hypercall(HVCALL_SEND_IPI_EX, pgs_gpa, pgs_gpa + 4096);
|
||||
hyperv_hypercall(HVCALL_SEND_IPI_EX, pgs_gpa, pgs_gpa + PAGE_SIZE);
|
||||
nop_loop();
|
||||
GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ++ipis_expected[0]);
|
||||
GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ++ipis_expected[1]);
|
||||
|
|
|
|||
|
|
@ -621,7 +621,7 @@ int main(int argc, char *argv[])
|
|||
for (i = 0; i < NTEST_PAGES; i++) {
|
||||
pte = vm_get_page_table_entry(vm, data->test_pages + i * PAGE_SIZE);
|
||||
gpa = addr_hva2gpa(vm, pte);
|
||||
__virt_pg_map(vm, gva + PAGE_SIZE * i, gpa & PAGE_MASK, PG_LEVEL_4K);
|
||||
virt_pg_map(vm, gva + PAGE_SIZE * i, gpa & PAGE_MASK);
|
||||
data->test_pages_pte[i] = gva + (gpa & ~PAGE_MASK);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
/*
|
||||
* vmx_close_while_nested
|
||||
*
|
||||
* Copyright (C) 2019, Red Hat, Inc.
|
||||
*
|
||||
* Verify that nothing bad happens if a KVM user exits with open
|
||||
|
|
@ -12,6 +10,7 @@
|
|||
#include "kvm_util.h"
|
||||
#include "processor.h"
|
||||
#include "vmx.h"
|
||||
#include "svm_util.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
|
@ -22,6 +21,8 @@ enum {
|
|||
PORT_L0_EXIT = 0x2000,
|
||||
};
|
||||
|
||||
#define L2_GUEST_STACK_SIZE 64
|
||||
|
||||
static void l2_guest_code(void)
|
||||
{
|
||||
/* Exit to L0 */
|
||||
|
|
@ -29,9 +30,8 @@ static void l2_guest_code(void)
|
|||
: : [port] "d" (PORT_L0_EXIT) : "rax");
|
||||
}
|
||||
|
||||
static void l1_guest_code(struct vmx_pages *vmx_pages)
|
||||
static void l1_vmx_code(struct vmx_pages *vmx_pages)
|
||||
{
|
||||
#define L2_GUEST_STACK_SIZE 64
|
||||
unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
|
||||
|
||||
GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
|
||||
|
|
@ -45,19 +45,43 @@ static void l1_guest_code(struct vmx_pages *vmx_pages)
|
|||
GUEST_ASSERT(0);
|
||||
}
|
||||
|
||||
static void l1_svm_code(struct svm_test_data *svm)
|
||||
{
|
||||
unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
|
||||
|
||||
/* Prepare the VMCB for L2 execution. */
|
||||
generic_svm_setup(svm, l2_guest_code,
|
||||
&l2_guest_stack[L2_GUEST_STACK_SIZE]);
|
||||
|
||||
run_guest(svm->vmcb, svm->vmcb_gpa);
|
||||
GUEST_ASSERT(0);
|
||||
}
|
||||
|
||||
static void l1_guest_code(void *data)
|
||||
{
|
||||
if (this_cpu_has(X86_FEATURE_VMX))
|
||||
l1_vmx_code(data);
|
||||
else
|
||||
l1_svm_code(data);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
vm_vaddr_t vmx_pages_gva;
|
||||
vm_vaddr_t guest_gva;
|
||||
struct kvm_vcpu *vcpu;
|
||||
struct kvm_vm *vm;
|
||||
|
||||
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
|
||||
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX) ||
|
||||
kvm_cpu_has(X86_FEATURE_SVM));
|
||||
|
||||
vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
|
||||
|
||||
/* Allocate VMX pages and shared descriptors (vmx_pages). */
|
||||
vcpu_alloc_vmx(vm, &vmx_pages_gva);
|
||||
vcpu_args_set(vcpu, 1, vmx_pages_gva);
|
||||
if (kvm_cpu_has(X86_FEATURE_VMX))
|
||||
vcpu_alloc_vmx(vm, &guest_gva);
|
||||
else
|
||||
vcpu_alloc_svm(vm, &guest_gva);
|
||||
|
||||
vcpu_args_set(vcpu, 1, guest_gva);
|
||||
|
||||
for (;;) {
|
||||
volatile struct kvm_run *run = vcpu->run;
|
||||
116
tools/testing/selftests/kvm/x86/nested_invalid_cr3_test.c
Normal file
116
tools/testing/selftests/kvm/x86/nested_invalid_cr3_test.c
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
/*
|
||||
* Copyright (C) 2025, Google LLC.
|
||||
*
|
||||
* This test verifies that L1 fails to enter L2 with an invalid CR3, and
|
||||
* succeeds otherwise.
|
||||
*/
|
||||
#include "kvm_util.h"
|
||||
#include "vmx.h"
|
||||
#include "svm_util.h"
|
||||
#include "kselftest.h"
|
||||
|
||||
|
||||
#define L2_GUEST_STACK_SIZE 64
|
||||
|
||||
static void l2_guest_code(void)
|
||||
{
|
||||
vmcall();
|
||||
}
|
||||
|
||||
static void l1_svm_code(struct svm_test_data *svm)
|
||||
{
|
||||
unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
|
||||
uintptr_t save_cr3;
|
||||
|
||||
generic_svm_setup(svm, l2_guest_code,
|
||||
&l2_guest_stack[L2_GUEST_STACK_SIZE]);
|
||||
|
||||
/* Try to run L2 with invalid CR3 and make sure it fails */
|
||||
save_cr3 = svm->vmcb->save.cr3;
|
||||
svm->vmcb->save.cr3 = -1ull;
|
||||
run_guest(svm->vmcb, svm->vmcb_gpa);
|
||||
GUEST_ASSERT(svm->vmcb->control.exit_code == SVM_EXIT_ERR);
|
||||
|
||||
/* Now restore CR3 and make sure L2 runs successfully */
|
||||
svm->vmcb->save.cr3 = save_cr3;
|
||||
run_guest(svm->vmcb, svm->vmcb_gpa);
|
||||
GUEST_ASSERT(svm->vmcb->control.exit_code == SVM_EXIT_VMMCALL);
|
||||
|
||||
GUEST_DONE();
|
||||
}
|
||||
|
||||
static void l1_vmx_code(struct vmx_pages *vmx_pages)
|
||||
{
|
||||
unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
|
||||
uintptr_t save_cr3;
|
||||
|
||||
GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
|
||||
GUEST_ASSERT(load_vmcs(vmx_pages));
|
||||
|
||||
prepare_vmcs(vmx_pages, l2_guest_code,
|
||||
&l2_guest_stack[L2_GUEST_STACK_SIZE]);
|
||||
|
||||
/* Try to run L2 with invalid CR3 and make sure it fails */
|
||||
save_cr3 = vmreadz(GUEST_CR3);
|
||||
vmwrite(GUEST_CR3, -1ull);
|
||||
GUEST_ASSERT(!vmlaunch());
|
||||
GUEST_ASSERT(vmreadz(VM_EXIT_REASON) ==
|
||||
(EXIT_REASON_FAILED_VMENTRY | EXIT_REASON_INVALID_STATE));
|
||||
|
||||
/* Now restore CR3 and make sure L2 runs successfully */
|
||||
vmwrite(GUEST_CR3, save_cr3);
|
||||
GUEST_ASSERT(!vmlaunch());
|
||||
GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
|
||||
|
||||
GUEST_DONE();
|
||||
}
|
||||
|
||||
static void l1_guest_code(void *data)
|
||||
{
|
||||
if (this_cpu_has(X86_FEATURE_VMX))
|
||||
l1_vmx_code(data);
|
||||
else
|
||||
l1_svm_code(data);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct kvm_vcpu *vcpu;
|
||||
struct kvm_vm *vm;
|
||||
vm_vaddr_t guest_gva = 0;
|
||||
|
||||
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX) ||
|
||||
kvm_cpu_has(X86_FEATURE_SVM));
|
||||
|
||||
vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
|
||||
|
||||
if (kvm_cpu_has(X86_FEATURE_VMX))
|
||||
vcpu_alloc_vmx(vm, &guest_gva);
|
||||
else
|
||||
vcpu_alloc_svm(vm, &guest_gva);
|
||||
|
||||
vcpu_args_set(vcpu, 1, guest_gva);
|
||||
|
||||
for (;;) {
|
||||
struct ucall uc;
|
||||
|
||||
vcpu_run(vcpu);
|
||||
TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
|
||||
|
||||
switch (get_ucall(vcpu, &uc)) {
|
||||
case UCALL_ABORT:
|
||||
REPORT_GUEST_ASSERT(uc);
|
||||
case UCALL_SYNC:
|
||||
break;
|
||||
case UCALL_DONE:
|
||||
goto done;
|
||||
default:
|
||||
TEST_FAIL("Unknown ucall %lu", uc.cmd);
|
||||
}
|
||||
}
|
||||
|
||||
done:
|
||||
kvm_vm_free(vm);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,7 +1,5 @@
|
|||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
/*
|
||||
* vmx_tsc_adjust_test
|
||||
*
|
||||
* Copyright (C) 2018, Google LLC.
|
||||
*
|
||||
* IA32_TSC_ADJUST test
|
||||
|
|
@ -22,6 +20,7 @@
|
|||
#include "kvm_util.h"
|
||||
#include "processor.h"
|
||||
#include "vmx.h"
|
||||
#include "svm_util.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
|
@ -35,6 +34,8 @@
|
|||
#define TSC_ADJUST_VALUE (1ll << 32)
|
||||
#define TSC_OFFSET_VALUE -(1ll << 48)
|
||||
|
||||
#define L2_GUEST_STACK_SIZE 64
|
||||
|
||||
enum {
|
||||
PORT_ABORT = 0x1000,
|
||||
PORT_REPORT,
|
||||
|
|
@ -72,42 +73,47 @@ static void l2_guest_code(void)
|
|||
__asm__ __volatile__("vmcall");
|
||||
}
|
||||
|
||||
static void l1_guest_code(struct vmx_pages *vmx_pages)
|
||||
static void l1_guest_code(void *data)
|
||||
{
|
||||
#define L2_GUEST_STACK_SIZE 64
|
||||
unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
|
||||
uint32_t control;
|
||||
uintptr_t save_cr3;
|
||||
|
||||
/* Set TSC from L1 and make sure TSC_ADJUST is updated correctly */
|
||||
GUEST_ASSERT(rdtsc() < TSC_ADJUST_VALUE);
|
||||
wrmsr(MSR_IA32_TSC, rdtsc() - TSC_ADJUST_VALUE);
|
||||
check_ia32_tsc_adjust(-1 * TSC_ADJUST_VALUE);
|
||||
|
||||
GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
|
||||
GUEST_ASSERT(load_vmcs(vmx_pages));
|
||||
/*
|
||||
* Run L2 with TSC_OFFSET. L2 will write to TSC, and L1 is not
|
||||
* intercepting the write so it should update L1's TSC_ADJUST.
|
||||
*/
|
||||
if (this_cpu_has(X86_FEATURE_VMX)) {
|
||||
struct vmx_pages *vmx_pages = data;
|
||||
uint32_t control;
|
||||
|
||||
/* Prepare the VMCS for L2 execution. */
|
||||
prepare_vmcs(vmx_pages, l2_guest_code,
|
||||
&l2_guest_stack[L2_GUEST_STACK_SIZE]);
|
||||
control = vmreadz(CPU_BASED_VM_EXEC_CONTROL);
|
||||
control |= CPU_BASED_USE_MSR_BITMAPS | CPU_BASED_USE_TSC_OFFSETTING;
|
||||
vmwrite(CPU_BASED_VM_EXEC_CONTROL, control);
|
||||
vmwrite(TSC_OFFSET, TSC_OFFSET_VALUE);
|
||||
GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
|
||||
GUEST_ASSERT(load_vmcs(vmx_pages));
|
||||
|
||||
/* Jump into L2. First, test failure to load guest CR3. */
|
||||
save_cr3 = vmreadz(GUEST_CR3);
|
||||
vmwrite(GUEST_CR3, -1ull);
|
||||
GUEST_ASSERT(!vmlaunch());
|
||||
GUEST_ASSERT(vmreadz(VM_EXIT_REASON) ==
|
||||
(EXIT_REASON_FAILED_VMENTRY | EXIT_REASON_INVALID_STATE));
|
||||
check_ia32_tsc_adjust(-1 * TSC_ADJUST_VALUE);
|
||||
vmwrite(GUEST_CR3, save_cr3);
|
||||
prepare_vmcs(vmx_pages, l2_guest_code,
|
||||
&l2_guest_stack[L2_GUEST_STACK_SIZE]);
|
||||
control = vmreadz(CPU_BASED_VM_EXEC_CONTROL);
|
||||
control |= CPU_BASED_USE_MSR_BITMAPS | CPU_BASED_USE_TSC_OFFSETTING;
|
||||
vmwrite(CPU_BASED_VM_EXEC_CONTROL, control);
|
||||
vmwrite(TSC_OFFSET, TSC_OFFSET_VALUE);
|
||||
|
||||
GUEST_ASSERT(!vmlaunch());
|
||||
GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
|
||||
GUEST_ASSERT(!vmlaunch());
|
||||
GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
|
||||
} else {
|
||||
struct svm_test_data *svm = data;
|
||||
|
||||
generic_svm_setup(svm, l2_guest_code,
|
||||
&l2_guest_stack[L2_GUEST_STACK_SIZE]);
|
||||
|
||||
svm->vmcb->control.tsc_offset = TSC_OFFSET_VALUE;
|
||||
run_guest(svm->vmcb, svm->vmcb_gpa);
|
||||
GUEST_ASSERT(svm->vmcb->control.exit_code == SVM_EXIT_VMMCALL);
|
||||
}
|
||||
|
||||
check_ia32_tsc_adjust(-2 * TSC_ADJUST_VALUE);
|
||||
|
||||
GUEST_DONE();
|
||||
}
|
||||
|
||||
|
|
@ -119,16 +125,19 @@ static void report(int64_t val)
|
|||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
vm_vaddr_t vmx_pages_gva;
|
||||
vm_vaddr_t nested_gva;
|
||||
struct kvm_vcpu *vcpu;
|
||||
|
||||
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
|
||||
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX) ||
|
||||
kvm_cpu_has(X86_FEATURE_SVM));
|
||||
|
||||
vm = vm_create_with_one_vcpu(&vcpu, (void *) l1_guest_code);
|
||||
vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
|
||||
if (kvm_cpu_has(X86_FEATURE_VMX))
|
||||
vcpu_alloc_vmx(vm, &nested_gva);
|
||||
else
|
||||
vcpu_alloc_svm(vm, &nested_gva);
|
||||
|
||||
/* Allocate VMX pages and shared descriptors (vmx_pages). */
|
||||
vcpu_alloc_vmx(vm, &vmx_pages_gva);
|
||||
vcpu_args_set(vcpu, 1, vmx_pages_gva);
|
||||
vcpu_args_set(vcpu, 1, nested_gva);
|
||||
|
||||
for (;;) {
|
||||
struct ucall uc;
|
||||
|
|
@ -13,6 +13,7 @@
|
|||
|
||||
#include "kvm_util.h"
|
||||
#include "vmx.h"
|
||||
#include "svm_util.h"
|
||||
#include "kselftest.h"
|
||||
|
||||
/* L2 is scaled up (from L1's perspective) by this factor */
|
||||
|
|
@ -79,7 +80,30 @@ static void l2_guest_code(void)
|
|||
__asm__ __volatile__("vmcall");
|
||||
}
|
||||
|
||||
static void l1_guest_code(struct vmx_pages *vmx_pages)
|
||||
static void l1_svm_code(struct svm_test_data *svm)
|
||||
{
|
||||
unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
|
||||
|
||||
/* check that L1's frequency looks alright before launching L2 */
|
||||
check_tsc_freq(UCHECK_L1);
|
||||
|
||||
generic_svm_setup(svm, l2_guest_code,
|
||||
&l2_guest_stack[L2_GUEST_STACK_SIZE]);
|
||||
|
||||
/* enable TSC scaling for L2 */
|
||||
wrmsr(MSR_AMD64_TSC_RATIO, L2_SCALE_FACTOR << 32);
|
||||
|
||||
/* launch L2 */
|
||||
run_guest(svm->vmcb, svm->vmcb_gpa);
|
||||
GUEST_ASSERT(svm->vmcb->control.exit_code == SVM_EXIT_VMMCALL);
|
||||
|
||||
/* check that L1's frequency still looks good */
|
||||
check_tsc_freq(UCHECK_L1);
|
||||
|
||||
GUEST_DONE();
|
||||
}
|
||||
|
||||
static void l1_vmx_code(struct vmx_pages *vmx_pages)
|
||||
{
|
||||
unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
|
||||
uint32_t control;
|
||||
|
|
@ -116,11 +140,19 @@ static void l1_guest_code(struct vmx_pages *vmx_pages)
|
|||
GUEST_DONE();
|
||||
}
|
||||
|
||||
static void l1_guest_code(void *data)
|
||||
{
|
||||
if (this_cpu_has(X86_FEATURE_VMX))
|
||||
l1_vmx_code(data);
|
||||
else
|
||||
l1_svm_code(data);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct kvm_vcpu *vcpu;
|
||||
struct kvm_vm *vm;
|
||||
vm_vaddr_t vmx_pages_gva;
|
||||
vm_vaddr_t guest_gva = 0;
|
||||
|
||||
uint64_t tsc_start, tsc_end;
|
||||
uint64_t tsc_khz;
|
||||
|
|
@ -129,7 +161,8 @@ int main(int argc, char *argv[])
|
|||
uint64_t l1_tsc_freq = 0;
|
||||
uint64_t l2_tsc_freq = 0;
|
||||
|
||||
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
|
||||
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX) ||
|
||||
kvm_cpu_has(X86_FEATURE_SVM));
|
||||
TEST_REQUIRE(kvm_has_cap(KVM_CAP_TSC_CONTROL));
|
||||
TEST_REQUIRE(sys_clocksource_is_based_on_tsc());
|
||||
|
||||
|
|
@ -152,8 +185,13 @@ int main(int argc, char *argv[])
|
|||
printf("real TSC frequency is around: %"PRIu64"\n", l0_tsc_freq);
|
||||
|
||||
vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
|
||||
vcpu_alloc_vmx(vm, &vmx_pages_gva);
|
||||
vcpu_args_set(vcpu, 1, vmx_pages_gva);
|
||||
|
||||
if (kvm_cpu_has(X86_FEATURE_VMX))
|
||||
vcpu_alloc_vmx(vm, &guest_gva);
|
||||
else
|
||||
vcpu_alloc_svm(vm, &guest_gva);
|
||||
|
||||
vcpu_args_set(vcpu, 1, guest_gva);
|
||||
|
||||
tsc_khz = __vcpu_ioctl(vcpu, KVM_GET_TSC_KHZ, NULL);
|
||||
TEST_ASSERT(tsc_khz != -1, "vcpu ioctl KVM_GET_TSC_KHZ failed");
|
||||
|
|
@ -104,7 +104,7 @@ static void test_sync_vmsa(uint32_t type, uint64_t policy)
|
|||
vm_sev_launch(vm, policy, NULL);
|
||||
|
||||
/* This page is shared, so make it decrypted. */
|
||||
memset(hva, 0, 4096);
|
||||
memset(hva, 0, PAGE_SIZE);
|
||||
|
||||
vcpu_run(vcpu);
|
||||
|
||||
|
|
|
|||
|
|
@ -141,7 +141,7 @@ static void __attribute__((__flatten__)) guest_code(void *arg)
|
|||
|
||||
if (this_cpu_has(X86_FEATURE_XSAVE)) {
|
||||
uint64_t supported_xcr0 = this_cpu_supported_xcr0();
|
||||
uint8_t buffer[4096];
|
||||
uint8_t buffer[PAGE_SIZE];
|
||||
|
||||
memset(buffer, 0xcc, sizeof(buffer));
|
||||
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ int main(int argc, char *argv[])
|
|||
regs.rcx = 1;
|
||||
if (regs.rcx == 3)
|
||||
regs.rcx = 8192;
|
||||
memset((void *)run + run->io.data_offset, 0xaa, 4096);
|
||||
memset((void *)run + run->io.data_offset, 0xaa, PAGE_SIZE);
|
||||
vcpu_regs_set(vcpu, ®s);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -120,17 +120,17 @@ static void test_vmx_dirty_log(bool enable_ept)
|
|||
* GPAs as the EPT enabled case.
|
||||
*/
|
||||
if (enable_ept) {
|
||||
prepare_eptp(vmx, vm, 0);
|
||||
prepare_eptp(vmx, vm);
|
||||
nested_map_memslot(vmx, vm, 0);
|
||||
nested_map(vmx, vm, NESTED_TEST_MEM1, GUEST_TEST_MEM, 4096);
|
||||
nested_map(vmx, vm, NESTED_TEST_MEM2, GUEST_TEST_MEM, 4096);
|
||||
nested_map(vmx, vm, NESTED_TEST_MEM1, GUEST_TEST_MEM, PAGE_SIZE);
|
||||
nested_map(vmx, vm, NESTED_TEST_MEM2, GUEST_TEST_MEM, PAGE_SIZE);
|
||||
}
|
||||
|
||||
bmap = bitmap_zalloc(TEST_MEM_PAGES);
|
||||
host_test_mem = addr_gpa2hva(vm, GUEST_TEST_MEM);
|
||||
|
||||
while (!done) {
|
||||
memset(host_test_mem, 0xaa, TEST_MEM_PAGES * 4096);
|
||||
memset(host_test_mem, 0xaa, TEST_MEM_PAGES * PAGE_SIZE);
|
||||
vcpu_run(vcpu);
|
||||
TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
|
||||
|
||||
|
|
@ -153,9 +153,9 @@ static void test_vmx_dirty_log(bool enable_ept)
|
|||
}
|
||||
|
||||
TEST_ASSERT(!test_bit(1, bmap), "Page 1 incorrectly reported dirty");
|
||||
TEST_ASSERT(host_test_mem[4096 / 8] == 0xaaaaaaaaaaaaaaaaULL, "Page 1 written by guest");
|
||||
TEST_ASSERT(host_test_mem[PAGE_SIZE / 8] == 0xaaaaaaaaaaaaaaaaULL, "Page 1 written by guest");
|
||||
TEST_ASSERT(!test_bit(2, bmap), "Page 2 incorrectly reported dirty");
|
||||
TEST_ASSERT(host_test_mem[8192 / 8] == 0xaaaaaaaaaaaaaaaaULL, "Page 2 written by guest");
|
||||
TEST_ASSERT(host_test_mem[PAGE_SIZE*2 / 8] == 0xaaaaaaaaaaaaaaaaULL, "Page 2 written by guest");
|
||||
break;
|
||||
case UCALL_DONE:
|
||||
done = true;
|
||||
|
|
|
|||
132
tools/testing/selftests/kvm/x86/vmx_nested_la57_state_test.c
Normal file
132
tools/testing/selftests/kvm/x86/vmx_nested_la57_state_test.c
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
/*
|
||||
* Copyright (C) 2025, Google LLC.
|
||||
*
|
||||
* Test KVM's ability to save and restore nested state when the L1 guest
|
||||
* is using 5-level paging and the L2 guest is using 4-level paging.
|
||||
*
|
||||
* This test would have failed prior to commit 9245fd6b8531 ("KVM: x86:
|
||||
* model canonical checks more precisely").
|
||||
*/
|
||||
#include "test_util.h"
|
||||
#include "kvm_util.h"
|
||||
#include "processor.h"
|
||||
#include "vmx.h"
|
||||
|
||||
#define LA57_GS_BASE 0xff2bc0311fb00000ull
|
||||
|
||||
static void l2_guest_code(void)
|
||||
{
|
||||
/*
|
||||
* Sync with L0 to trigger save/restore. After
|
||||
* resuming, execute VMCALL to exit back to L1.
|
||||
*/
|
||||
GUEST_SYNC(1);
|
||||
vmcall();
|
||||
}
|
||||
|
||||
static void l1_guest_code(struct vmx_pages *vmx_pages)
|
||||
{
|
||||
#define L2_GUEST_STACK_SIZE 64
|
||||
unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
|
||||
u64 guest_cr4;
|
||||
vm_paddr_t pml5_pa, pml4_pa;
|
||||
u64 *pml5;
|
||||
u64 exit_reason;
|
||||
|
||||
/* Set GS_BASE to a value that is only canonical with LA57. */
|
||||
wrmsr(MSR_GS_BASE, LA57_GS_BASE);
|
||||
GUEST_ASSERT(rdmsr(MSR_GS_BASE) == LA57_GS_BASE);
|
||||
|
||||
GUEST_ASSERT(vmx_pages->vmcs_gpa);
|
||||
GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
|
||||
GUEST_ASSERT(load_vmcs(vmx_pages));
|
||||
|
||||
prepare_vmcs(vmx_pages, l2_guest_code,
|
||||
&l2_guest_stack[L2_GUEST_STACK_SIZE]);
|
||||
|
||||
/*
|
||||
* Set up L2 with a 4-level page table by pointing its CR3 to
|
||||
* L1's first PML4 table and clearing CR4.LA57. This creates
|
||||
* the CR4.LA57 mismatch that exercises the bug.
|
||||
*/
|
||||
pml5_pa = get_cr3() & PHYSICAL_PAGE_MASK;
|
||||
pml5 = (u64 *)pml5_pa;
|
||||
pml4_pa = pml5[0] & PHYSICAL_PAGE_MASK;
|
||||
vmwrite(GUEST_CR3, pml4_pa);
|
||||
|
||||
guest_cr4 = vmreadz(GUEST_CR4);
|
||||
guest_cr4 &= ~X86_CR4_LA57;
|
||||
vmwrite(GUEST_CR4, guest_cr4);
|
||||
|
||||
GUEST_ASSERT(!vmlaunch());
|
||||
|
||||
exit_reason = vmreadz(VM_EXIT_REASON);
|
||||
GUEST_ASSERT(exit_reason == EXIT_REASON_VMCALL);
|
||||
}
|
||||
|
||||
void guest_code(struct vmx_pages *vmx_pages)
|
||||
{
|
||||
l1_guest_code(vmx_pages);
|
||||
GUEST_DONE();
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
vm_vaddr_t vmx_pages_gva = 0;
|
||||
struct kvm_vm *vm;
|
||||
struct kvm_vcpu *vcpu;
|
||||
struct kvm_x86_state *state;
|
||||
struct ucall uc;
|
||||
int stage;
|
||||
|
||||
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
|
||||
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_LA57));
|
||||
TEST_REQUIRE(kvm_has_cap(KVM_CAP_NESTED_STATE));
|
||||
|
||||
vm = vm_create_with_one_vcpu(&vcpu, guest_code);
|
||||
|
||||
/*
|
||||
* L1 needs to read its own PML5 table to set up L2. Identity map
|
||||
* the PML5 table to facilitate this.
|
||||
*/
|
||||
virt_map(vm, vm->pgd, vm->pgd, 1);
|
||||
|
||||
vcpu_alloc_vmx(vm, &vmx_pages_gva);
|
||||
vcpu_args_set(vcpu, 1, vmx_pages_gva);
|
||||
|
||||
for (stage = 1;; stage++) {
|
||||
vcpu_run(vcpu);
|
||||
TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
|
||||
|
||||
switch (get_ucall(vcpu, &uc)) {
|
||||
case UCALL_ABORT:
|
||||
REPORT_GUEST_ASSERT(uc);
|
||||
/* NOT REACHED */
|
||||
case UCALL_SYNC:
|
||||
break;
|
||||
case UCALL_DONE:
|
||||
goto done;
|
||||
default:
|
||||
TEST_FAIL("Unknown ucall %lu", uc.cmd);
|
||||
}
|
||||
|
||||
TEST_ASSERT(uc.args[1] == stage,
|
||||
"Expected stage %d, got stage %lu", stage, (ulong)uc.args[1]);
|
||||
if (stage == 1) {
|
||||
pr_info("L2 is active; performing save/restore.\n");
|
||||
state = vcpu_save_state(vcpu);
|
||||
|
||||
kvm_vm_release(vm);
|
||||
|
||||
/* Restore state in a new VM. */
|
||||
vcpu = vm_recreate_with_one_vcpu(vm);
|
||||
vcpu_load_state(vcpu, state);
|
||||
kvm_x86_state_cleanup(state);
|
||||
}
|
||||
}
|
||||
|
||||
done:
|
||||
kvm_vm_free(vm);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user