From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B563035AC35; Wed, 8 Apr 2026 04:46:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775623597; cv=none; b=u+dYvvditIw8o8UKqLWiMzJzDWUwoxewvJ9PYB5kXfyguUS9wucNgajYqZqUzX1k0MZXiecPtEVOhXaCykU+p1eeCHWBqckPk5AyjdJEyunAzz9IcsxwpmEfrld+uzqMnN91k6PcHAD/9NrvVHnc3ZkKKlRbaBmXCfsnn1YnarY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775623597; c=relaxed/simple; bh=WKSjkgsx5mMPbNpqlTOtDYTmkaZDZ/r6A2XwILlbu1g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BDd/ODGPAxrk+iJXlP6zcDBpv3Df53NSHNJHBq67Q2AXuqcmPTBHshh/VoI4rykekxVdgwZLU5NYJXHPVFdRo7pHikLUfJAvcU8VhLtSRdJODlb6O1VErqAbiqJsjtl0XG+44ATo2+3jWVRn01MpfLJ420GGzd8MFu31Yag6RaE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AdTWFeMa; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="AdTWFeMa" Received: by smtp.kernel.org (Postfix) with ESMTPS id 899CEC2BCB6; Wed, 8 Apr 2026 04:46:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775623597; bh=WKSjkgsx5mMPbNpqlTOtDYTmkaZDZ/r6A2XwILlbu1g=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=AdTWFeMaiJoFKFtsYJwPqqWdQIeVQLXSG4FUNi3GAhZKu6VvXLZxshjYum7f2PIbg Vav7xVk6Cnb2k9I8F6GBHNfHTtBxRwzzuj3LPI1/UejXqcOw1HodJgUQvAuRNsTByJ HJxRcqJCYCRsIeRqSR0T+9uRQazbyAUuwTbQ4ahFFjVDDpOm7HDlF2HuGr5oyalMnC lAENgFHbjh7UNP7/YeDHJnjNE0mncKrmCUGUDKp2XgfkHfE9nLhr7QH98zwBaMKUV+ tWj1RNzKkYhRX+AmEi/uXdyW/42uz7qj+DOmftOMc1+UnwEw+hUn01NZfUa5/OUiX4 wAtze9uMviKTA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 79152FD5F75; Wed, 8 Apr 2026 04:46:37 +0000 (UTC) From: Charlie Jenkins via B4 Relay Date: Tue, 07 Apr 2026 21:46:00 -0700 Subject: [PATCH 12/16] riscv: kvm: Add emulated test csr Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260407-riscv_insn_table-v1-12-54b4736a1e77@gmail.com> References: <20260407-riscv_insn_table-v1-0-54b4736a1e77@gmail.com> In-Reply-To: <20260407-riscv_insn_table-v1-0-54b4736a1e77@gmail.com> To: Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti , Anup Patel , Atish Patra , Conor Dooley , Paolo Bonzini , Andrew Morton , Shuah Khan Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-kselftest@vger.kernel.org, Charlie Jenkins X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1775623594; l=5057; i=thecharlesjenkins@gmail.com; s=2026030; h=from:subject:message-id; bh=VQg+AUDh82m4CKuXNzyUaNZj3tN1rr7u9UwtjA/PLWQ=; b=IxK/M+GZdcmuYVvUFn0ArMPQ+R4X1daV9Hr4PnCTImin79iBetPAbnAEEA1OEIMgDDaIizwRR 5Dc4RJzaRRQB+rh9z8oU3MyyyUe72p/HUlaGBNatdvZEnDRq12nZQWR X-Developer-Key: i=thecharlesjenkins@gmail.com; a=ed25519; pk=vpF2USrG+aB6CTbSt34rzJKsAVe/l+GAXo1IomCMETk= X-Endpoint-Received: by B4 Relay for thecharlesjenkins@gmail.com/2026030 with auth_id=663 X-Original-From: Charlie Jenkins Reply-To: thecharlesjenkins@gmail.com From: Charlie Jenkins To test the riscv kvm implementation of emulated CSRs, add support to emulate the vsscratch csr when CONFIG_RISCV_KVM_TEST_CSR is set. Signed-off-by: Charlie Jenkins --- arch/riscv/Kconfig.debug | 1 + arch/riscv/include/asm/kvm_host.h | 10 ++++++++++ arch/riscv/include/asm/kvm_vcpu_test_csr.h | 15 +++++++++++++++ arch/riscv/kvm/Kconfig.debug | 16 ++++++++++++++++ arch/riscv/kvm/Makefile | 1 + arch/riscv/kvm/vcpu_insn.c | 5 +++++ arch/riscv/kvm/vcpu_test_csr.c | 21 +++++++++++++++++++++ 7 files changed, 69 insertions(+) diff --git a/arch/riscv/Kconfig.debug b/arch/riscv/Kconfig.debug index eafe17ebf710..be202267da6d 100644 --- a/arch/riscv/Kconfig.debug +++ b/arch/riscv/Kconfig.debug @@ -1 +1,2 @@ source "arch/riscv/kernel/tests/Kconfig.debug" +source "arch/riscv/kvm/Kconfig.debug" diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h index 24585304c02b..709a1c18ce22 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -183,6 +183,12 @@ struct kvm_vcpu_reset_state { unsigned long a1; }; +#ifdef CONFIG_RISCV_KVM_TEST_CSR +struct kvm_test_csr { + unsigned long val; +}; +#endif + struct kvm_vcpu_arch { /* VCPU ran at least once */ bool ran_atleast_once; @@ -278,6 +284,10 @@ struct kvm_vcpu_arch { gpa_t shmem; u64 last_steal; } sta; + +#ifdef CONFIG_RISCV_KVM_TEST_CSR + struct kvm_test_csr test_csr; +#endif }; /* diff --git a/arch/riscv/include/asm/kvm_vcpu_test_csr.h b/arch/riscv/include/asm/kvm_vcpu_test_csr.h new file mode 100644 index 000000000000..a844fccaafc3 --- /dev/null +++ b/arch/riscv/include/asm/kvm_vcpu_test_csr.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef __KVM_VCPU_RISCV_TEST_CSR_H +#define __KVM_VCPU_RISCV_TEST_CSR_H + +#include + +#define KVM_RISCV_VCPU_TEST_CSR_FUNCS \ + {.base = CSR_VSSCRATCH, .count = 1, .func = kvm_riscv_vcpu_test_csr }, + +int kvm_riscv_vcpu_test_csr(struct kvm_vcpu *vcpu, unsigned int csr_num, + unsigned long *val, unsigned long new_val, + unsigned long wr_mask); + +#endif /* !__KVM_VCPU_RISCV_TEST_CSR_H */ diff --git a/arch/riscv/kvm/Kconfig.debug b/arch/riscv/kvm/Kconfig.debug new file mode 100644 index 000000000000..dc76e02120a3 --- /dev/null +++ b/arch/riscv/kvm/Kconfig.debug @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: GPL-2.0-only +menu "arch/riscv/kvm testing" + +config RISCV_KVM_TEST_CSR + bool "Test KVM CSR emulation" + depends on KVM + default n + help + Enable this option to enable the emulation of a test hypervisor csr. + The KVM test csr is the vsscratch register. Once this is enabled, + reading/writing to the vsscratch register will trap into the host + supervisor and reflect the change. + + If unsure, say N. + +endmenu # "arch/riscv/kvm testing" diff --git a/arch/riscv/kvm/Makefile b/arch/riscv/kvm/Makefile index 3b8afb038b35..02fd27ff33eb 100644 --- a/arch/riscv/kvm/Makefile +++ b/arch/riscv/kvm/Makefile @@ -36,6 +36,7 @@ kvm-y += vcpu_sbi_sta.o kvm-y += vcpu_sbi_system.o kvm-$(CONFIG_RISCV_SBI_V01) += vcpu_sbi_v01.o kvm-y += vcpu_switch.o +kvm-$(CONFIG_RISCV_KVM_TEST_CSR) += vcpu_test_csr.o kvm-y += vcpu_timer.o kvm-y += vcpu_vector.o kvm-y += vm.o diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c index 1d8741d02242..c5a70de4a579 100644 --- a/arch/riscv/kvm/vcpu_insn.c +++ b/arch/riscv/kvm/vcpu_insn.c @@ -10,6 +10,8 @@ #include #include +#include + struct insn_func { unsigned long mask; unsigned long match; @@ -112,6 +114,9 @@ static int seed_csr_rmw(struct kvm_vcpu *vcpu, unsigned int csr_num, static const struct csr_func csr_funcs[] = { KVM_RISCV_VCPU_AIA_CSR_FUNCS KVM_RISCV_VCPU_HPMCOUNTER_CSR_FUNCS +#ifdef CONFIG_RISCV_KVM_TEST_CSR + KVM_RISCV_VCPU_TEST_CSR_FUNCS +#endif { .base = CSR_SEED, .count = 1, .func = seed_csr_rmw }, }; diff --git a/arch/riscv/kvm/vcpu_test_csr.c b/arch/riscv/kvm/vcpu_test_csr.c new file mode 100644 index 000000000000..b8aa503cdaba --- /dev/null +++ b/arch/riscv/kvm/vcpu_test_csr.c @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include + +#define vcpu_to_test_csr(vcpu) (&(vcpu)->arch.test_csr) + +int kvm_riscv_vcpu_test_csr(struct kvm_vcpu *vcpu, unsigned int csr_num, + unsigned long *val, unsigned long new_val, + unsigned long wr_mask) +{ + struct kvm_test_csr *test_csr = vcpu_to_test_csr(vcpu); + + *val = test_csr->val; + + if (wr_mask) + test_csr->val = (test_csr->val & ~wr_mask) | (new_val & wr_mask); + + return KVM_INSN_CONTINUE_NEXT_SEPC; +} -- 2.52.0