[PATCH 1/3] KVM: LoongArch: selftests: Add cpucfg read/write helpers
From: Song Gao
Date: Tue Mar 10 2026 - 05:55:20 EST
Add helper macros and functions to read and write CPU configuration
registers (cpucfg) from within the guest and from the VMM. This
interface is required for querying and setting CPU features, such as
PMU capabilities, in upcoming selftests.
Signed-off-by: Song Gao <gaosong@xxxxxxxxxxx>
---
.../selftests/kvm/include/loongarch/processor.h | 11 +++++++++++
tools/testing/selftests/kvm/lib/loongarch/processor.c | 8 ++++++++
2 files changed, 19 insertions(+)
diff --git a/tools/testing/selftests/kvm/include/loongarch/processor.h b/tools/testing/selftests/kvm/include/loongarch/processor.h
index 76840ddda57d..b8f6c3348ddd 100644
--- a/tools/testing/selftests/kvm/include/loongarch/processor.h
+++ b/tools/testing/selftests/kvm/include/loongarch/processor.h
@@ -150,6 +150,17 @@
__v; \
})
+#define read_cpucfg(reg) \
+({ \
+ register unsigned long __v; \
+ __asm__ __volatile__( \
+ "cpucfg %0, %1\n\t" \
+ : "=r" (__v) \
+ : "r" (reg) \
+ : "memory"); \
+ __v; \
+})
+
#define EXREGS_GPRS (32)
#ifndef __ASSEMBLER__
diff --git a/tools/testing/selftests/kvm/lib/loongarch/processor.c b/tools/testing/selftests/kvm/lib/loongarch/processor.c
index 17aa55a2047a..68eb34032a99 100644
--- a/tools/testing/selftests/kvm/lib/loongarch/processor.c
+++ b/tools/testing/selftests/kvm/lib/loongarch/processor.c
@@ -267,6 +267,14 @@ static void loongarch_set_csr(struct kvm_vcpu *vcpu, uint64_t id, uint64_t val)
__vcpu_set_reg(vcpu, csrid, val);
}
+static void loongarch_set_cpucfg(struct kvm_vcpu *vcpu, uint64_t id, uint64_t val)
+{
+ uint64_t cfgid;
+
+ cfgid = KVM_REG_LOONGARCH_CPUCFG | KVM_REG_SIZE_U64 | 8 * id;
+ __vcpu_set_reg(vcpu, cfgid, val);
+}
+
static void loongarch_vcpu_setup(struct kvm_vcpu *vcpu)
{
int width;
--
2.47.3