mirror of
https://github.com/torvalds/linux.git
synced 2025-12-07 20:06:24 +00:00
KVM: selftests: Move nested invalid CR3 check to its own test
vmx_tsc_adjust_test currently verifies that a nested VMLAUNCH fails with an invalid CR3. This is irrelevant to TSC scaling, move it to a standalone test. Signed-off-by: Yosry Ahmed <yosry.ahmed@linux.dev> Link: https://patch.msgid.link/20251021074736.1324328-6-yosry.ahmed@linux.dev Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
committed by
Sean Christopherson
parent
e6bcdd2122
commit
4d256d00e4
@@ -91,6 +91,7 @@ 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_scaling_test
|
||||
TEST_GEN_PROGS_x86 += x86/platform_info_test
|
||||
TEST_GEN_PROGS_x86 += x86/pmu_counters_test
|
||||
|
||||
79
tools/testing/selftests/kvm/x86/nested_invalid_cr3_test.c
Normal file
79
tools/testing/selftests/kvm/x86/nested_invalid_cr3_test.c
Normal file
@@ -0,0 +1,79 @@
|
||||
// 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 "kselftest.h"
|
||||
|
||||
|
||||
#define L2_GUEST_STACK_SIZE 64
|
||||
|
||||
static void l2_guest_code(void)
|
||||
{
|
||||
vmcall();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
vm = vm_create_with_one_vcpu(&vcpu, l1_vmx_code);
|
||||
vcpu_alloc_vmx(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;
|
||||
}
|
||||
@@ -77,7 +77,6 @@ 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];
|
||||
uint32_t control;
|
||||
uintptr_t save_cr3;
|
||||
|
||||
GUEST_ASSERT(rdtsc() < TSC_ADJUST_VALUE);
|
||||
wrmsr(MSR_IA32_TSC, rdtsc() - TSC_ADJUST_VALUE);
|
||||
@@ -94,15 +93,6 @@ static void l1_guest_code(struct vmx_pages *vmx_pages)
|
||||
vmwrite(CPU_BASED_VM_EXEC_CONTROL, control);
|
||||
vmwrite(TSC_OFFSET, TSC_OFFSET_VALUE);
|
||||
|
||||
/* 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);
|
||||
|
||||
GUEST_ASSERT(!vmlaunch());
|
||||
GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user