[PATCH v1 21/23] KVM: selftests: Run debug_regs test with FRED enabled

From: Xin Li
Date: Wed Nov 08 2023 - 14:01:40 EST


Run another round of debug_regs test with FRED enabled if FRED is
available.

Signed-off-by: Xin Li <xin3.li@xxxxxxxxx>
---
.../selftests/kvm/include/x86_64/processor.h | 4 ++
.../testing/selftests/kvm/x86_64/debug_regs.c | 50 ++++++++++++++-----
2 files changed, 41 insertions(+), 13 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h
index 25bc61dac5fb..165d21fd1577 100644
--- a/tools/testing/selftests/kvm/include/x86_64/processor.h
+++ b/tools/testing/selftests/kvm/include/x86_64/processor.h
@@ -47,6 +47,7 @@ extern bool host_cpu_is_amd;
#define X86_CR4_SMEP (1ul << 20)
#define X86_CR4_SMAP (1ul << 21)
#define X86_CR4_PKE (1ul << 22)
+#define X86_CR4_FRED (1ul << 32)

struct xstate_header {
u64 xstate_bv;
@@ -163,6 +164,9 @@ struct kvm_x86_cpu_feature {
#define X86_FEATURE_SPEC_CTRL KVM_X86_CPU_FEATURE(0x7, 0, EDX, 26)
#define X86_FEATURE_ARCH_CAPABILITIES KVM_X86_CPU_FEATURE(0x7, 0, EDX, 29)
#define X86_FEATURE_PKS KVM_X86_CPU_FEATURE(0x7, 0, ECX, 31)
+#define X86_FEATURE_FRED KVM_X86_CPU_FEATURE(0x7, 1, EAX, 17)
+#define X86_FEATURE_LKGS KVM_X86_CPU_FEATURE(0x7, 1, EAX, 18)
+#define X86_FEATURE_WRMSRNS KVM_X86_CPU_FEATURE(0x7, 1, EAX, 19)
#define X86_FEATURE_XTILECFG KVM_X86_CPU_FEATURE(0xD, 0, EAX, 17)
#define X86_FEATURE_XTILEDATA KVM_X86_CPU_FEATURE(0xD, 0, EAX, 18)
#define X86_FEATURE_XSAVES KVM_X86_CPU_FEATURE(0xD, 1, EAX, 3)
diff --git a/tools/testing/selftests/kvm/x86_64/debug_regs.c b/tools/testing/selftests/kvm/x86_64/debug_regs.c
index f6b295e0b2d2..69055e764f15 100644
--- a/tools/testing/selftests/kvm/x86_64/debug_regs.c
+++ b/tools/testing/selftests/kvm/x86_64/debug_regs.c
@@ -20,7 +20,7 @@ uint32_t guest_value;

extern unsigned char sw_bp, hw_bp, write_data, ss_start, bd_start;

-static void guest_code(void)
+static void guest_test_code(void)
{
/* Create a pending interrupt on current vCPU */
x2apic_enable();
@@ -61,6 +61,15 @@ static void guest_code(void)

/* DR6.BD test */
asm volatile("bd_start: mov %%dr0, %%rax" : : : "rax");
+}
+
+static void guest_code(void)
+{
+ guest_test_code();
+
+ if (get_cr4() & X86_CR4_FRED)
+ guest_test_code();
+
GUEST_DONE();
}

@@ -75,19 +84,15 @@ static void vcpu_skip_insn(struct kvm_vcpu *vcpu, int insn_len)
vcpu_regs_set(vcpu, &regs);
}

-int main(void)
+void run_test(struct kvm_vcpu *vcpu)
{
struct kvm_guest_debug debug;
+ struct kvm_run *run = vcpu->run;
unsigned long long target_dr6, target_rip;
- struct kvm_vcpu *vcpu;
- struct kvm_run *run;
- struct kvm_vm *vm;
- struct ucall uc;
- uint64_t cmd;
int i;
/* Instruction lengths starting at ss_start */
int ss_size[6] = {
- 1, /* sti*/
+ 1, /* sti */
2, /* xor */
2, /* cpuid */
5, /* mov */
@@ -95,11 +100,6 @@ int main(void)
1, /* cli */
};

- TEST_REQUIRE(kvm_has_cap(KVM_CAP_SET_GUEST_DEBUG));
-
- vm = vm_create_with_one_vcpu(&vcpu, guest_code);
- run = vcpu->run;
-
/* Test software BPs - int3 */
memset(&debug, 0, sizeof(debug));
debug.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
@@ -202,6 +202,30 @@ int main(void)
/* Disable all debug controls, run to the end */
memset(&debug, 0, sizeof(debug));
vcpu_guest_debug_set(vcpu, &debug);
+}
+
+int main(void)
+{
+ struct kvm_vcpu *vcpu;
+ struct kvm_vm *vm;
+ struct ucall uc;
+ uint64_t cmd;
+
+ TEST_REQUIRE(kvm_has_cap(KVM_CAP_SET_GUEST_DEBUG));
+
+ vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+
+ run_test(vcpu);
+
+ if (kvm_cpu_has(X86_FEATURE_FRED)) {
+ struct kvm_sregs sregs;
+
+ vcpu_sregs_get(vcpu, &sregs);
+ sregs.cr4 |= X86_CR4_FRED;
+ vcpu_sregs_set(vcpu, &sregs);
+
+ run_test(vcpu);
+ }

vcpu_run(vcpu);
TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
--
2.42.0