* [PATCH v2 0/5] Enable hstateen bits lazily for the KVM RISC-V Guests
@ 2025-05-22 20:35 Atish Patra
2025-05-22 20:35 ` [PATCH v2 1/5] RISC-V: KVM: Lazy enable hstateen IMSIC & ISEL bit Atish Patra
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Atish Patra @ 2025-05-22 20:35 UTC (permalink / raw)
To: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt,
Alexandre Ghiti
Cc: kvm, kvm-riscv, linux-riscv, linux-kernel, Atish Patra
This series adds support for enabling hstateen bits lazily at runtime
instead of statically at bootime. The boot time enabling happens for
all the guests if the required extensions are present in the host and/or
guest. That may not be necessary if the guest never exercise that
feature. We can enable the hstateen bits that controls the access lazily
upon first access. This providers KVM more granular control of which
feature is enabled in the guest at runtime.
Currently, the following hstateen bits are supported to control the access
from VS mode.
1. BIT(58): IMSIC : STOPEI and IMSIC guest interrupt file
2. BIT(59): AIA : SIPH/SIEH/STOPI
3. BIT(60): AIA_ISEL : Indirect csr access via siselect/sireg
4. BIT(62): HSENVCFG : SENVCFG access
5. BIT(63): SSTATEEN0 : SSTATEEN0 access
KVM already support trap/enabling of BIT(58) and BIT(60) in order
to support sw version of the guest interrupt file. This series extends
those to enable to correpsonding hstateen bits in PATCH1. The remaining
patches adds lazy enabling support of the other bits.
I am working on a followup series to add indirect CSR extension and move the
siselect/sireg handlers out of AIA so that other features(e.g CTR) can leverage
it.
Note: This series just updates the hstateen bit in cfg so that any update
would reflect in the correct VM state during the next vcpu load.
Alternatively, we can save the hstateen state in vcpu_put to achieve this.
However, it will incur additional cost on every VM exit while the current
approach just updates the configuration once per VM life time upon first
access.
To: Anup Patel <anup@brainfault.org>
To: Atish Patra <atishp@atishpatra.org>
To: Paul Walmsley <paul.walmsley@sifive.com>
To: Palmer Dabbelt <palmer@dabbelt.com>
To: Alexandre Ghiti <alex@ghiti.fr>
Cc: kvm@vger.kernel.org
Cc: kvm-riscv@lists.infradead.org
Cc: linux-riscv@lists.infradead.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Atish Patra <atishp@rivosinc.com>
---
Changes in v2:
- Added a preventive check for lower 32 bits of hstateen.
- Link to v1: https://lore.kernel.org/r/20250505-kvm_lazy_enable_stateen-v1-0-3bfc4008373c@rivosinc.com
---
Atish Patra (5):
RISC-V: KVM: Lazy enable hstateen IMSIC & ISEL bit
RISC-V: KVM: Add a hstateen lazy enabler helper function
RISC-V: KVM: Support lazy enabling of siselect and aia bits
RISC-V: KVM: Enable envcfg and sstateen bits lazily
RISC-V: KVM: Remove the boot time enabling of hstateen bits
arch/riscv/include/asm/kvm_aia.h | 14 ++++++-
arch/riscv/include/asm/kvm_vcpu_insn.h | 4 ++
arch/riscv/kvm/aia.c | 77 ++++++++++++++++++++++++++++++++++
arch/riscv/kvm/aia_imsic.c | 8 ++++
arch/riscv/kvm/vcpu.c | 10 -----
arch/riscv/kvm/vcpu_insn.c | 61 +++++++++++++++++++++++++++
6 files changed, 163 insertions(+), 11 deletions(-)
---
base-commit: fb13a11917ea679b12b0d51905dea1cec23c015f
change-id: 20250430-kvm_lazy_enable_stateen-33c8aa9a3071
--
Regards,
Atish patra
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2 1/5] RISC-V: KVM: Lazy enable hstateen IMSIC & ISEL bit
2025-05-22 20:35 [PATCH v2 0/5] Enable hstateen bits lazily for the KVM RISC-V Guests Atish Patra
@ 2025-05-22 20:35 ` Atish Patra
2025-05-22 20:35 ` [PATCH v2 2/5] RISC-V: KVM: Add a hstateen lazy enabler helper function Atish Patra
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Atish Patra @ 2025-05-22 20:35 UTC (permalink / raw)
To: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt,
Alexandre Ghiti
Cc: kvm, kvm-riscv, linux-riscv, linux-kernel, Atish Patra
Currently, we enable the smstateen bit at vcpu configure time by
only checking the presence of required ISA extensions.
These bits are not required to be enabled if the guest never uses
the corresponding architectural state. Enable the smstaeen bits
at runtime lazily upon first access.
Signed-off-by: Atish Patra <atishp@rivosinc.com>
---
arch/riscv/include/asm/kvm_aia.h | 1 +
arch/riscv/kvm/aia.c | 43 ++++++++++++++++++++++++++++++++++++++++
arch/riscv/kvm/aia_imsic.c | 8 ++++++++
3 files changed, 52 insertions(+)
diff --git a/arch/riscv/include/asm/kvm_aia.h b/arch/riscv/include/asm/kvm_aia.h
index 1f37b600ca47..760a1aef09f7 100644
--- a/arch/riscv/include/asm/kvm_aia.h
+++ b/arch/riscv/include/asm/kvm_aia.h
@@ -112,6 +112,7 @@ int kvm_riscv_aia_aplic_has_attr(struct kvm *kvm, unsigned long type);
int kvm_riscv_aia_aplic_inject(struct kvm *kvm, u32 source, bool level);
int kvm_riscv_aia_aplic_init(struct kvm *kvm);
void kvm_riscv_aia_aplic_cleanup(struct kvm *kvm);
+bool kvm_riscv_aia_imsic_state_hw_backed(struct kvm_vcpu *vcpu);
#ifdef CONFIG_32BIT
void kvm_riscv_vcpu_aia_flush_interrupts(struct kvm_vcpu *vcpu);
diff --git a/arch/riscv/kvm/aia.c b/arch/riscv/kvm/aia.c
index 19afd1f23537..1e0d2217ade7 100644
--- a/arch/riscv/kvm/aia.c
+++ b/arch/riscv/kvm/aia.c
@@ -241,6 +241,8 @@ int kvm_riscv_vcpu_aia_rmw_topei(struct kvm_vcpu *vcpu,
unsigned long new_val,
unsigned long wr_mask)
{
+ bool vsfile_present = kvm_riscv_aia_imsic_state_hw_backed(vcpu);
+
/* If AIA not available then redirect trap */
if (!kvm_riscv_aia_available())
return KVM_INSN_ILLEGAL_TRAP;
@@ -249,6 +251,26 @@ int kvm_riscv_vcpu_aia_rmw_topei(struct kvm_vcpu *vcpu,
if (!kvm_riscv_aia_initialized(vcpu->kvm))
return KVM_INSN_EXIT_TO_USER_SPACE;
+ /* Continue if smstaeen is not present */
+ if (!riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN))
+ goto skip_hstateen;
+
+ /* Enable the bit in hstateen0 lazily upon first access */
+ if (!(vcpu->arch.cfg.hstateen0 & SMSTATEEN0_AIA_IMSIC)) {
+ vcpu->arch.cfg.hstateen0 |= SMSTATEEN0_AIA_IMSIC;
+ if (IS_ENABLED(CONFIG_32BIT))
+ csr_set(CSR_HSTATEEN0H, SMSTATEEN0_AIA_IMSIC >> 32);
+ else
+ csr_set(CSR_HSTATEEN0, SMSTATEEN0_AIA_IMSIC);
+ if (vsfile_present)
+ return KVM_INSN_CONTINUE_SAME_SEPC;
+ } else if (vsfile_present) {
+ pr_err("Unexpected trap for CSR [%x] with hstateen0 enabled and valid vsfile\n",
+ csr_num);
+ return KVM_INSN_EXIT_TO_USER_SPACE;
+ }
+
+skip_hstateen:
return kvm_riscv_vcpu_aia_imsic_rmw(vcpu, KVM_RISCV_AIA_IMSIC_TOPEI,
val, new_val, wr_mask);
}
@@ -400,11 +422,32 @@ int kvm_riscv_vcpu_aia_rmw_ireg(struct kvm_vcpu *vcpu, unsigned int csr_num,
unsigned long wr_mask)
{
unsigned int isel;
+ bool vsfile_present = kvm_riscv_aia_imsic_state_hw_backed(vcpu);
/* If AIA not available then redirect trap */
if (!kvm_riscv_aia_available())
return KVM_INSN_ILLEGAL_TRAP;
+ /* Continue if smstaeen is not present */
+ if (!riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN))
+ goto skip_hstateen;
+
+ /* Enable the bit in hstateen0 lazily upon first access */
+ if (!(vcpu->arch.cfg.hstateen0 & SMSTATEEN0_AIA_ISEL)) {
+ vcpu->arch.cfg.hstateen0 |= SMSTATEEN0_AIA_ISEL;
+ if (IS_ENABLED(CONFIG_32BIT))
+ csr_set(CSR_HSTATEEN0H, SMSTATEEN0_AIA_ISEL >> 32);
+ else
+ csr_set(CSR_HSTATEEN0, SMSTATEEN0_AIA_ISEL);
+ if (vsfile_present)
+ return KVM_INSN_CONTINUE_SAME_SEPC;
+ } else if (vsfile_present) {
+ pr_err("Unexpected trap for CSR [%x] with hstateen0 enabled and valid vsfile\n",
+ csr_num);
+ return KVM_INSN_EXIT_TO_USER_SPACE;
+ }
+
+skip_hstateen:
/* First try to emulate in kernel space */
isel = ncsr_read(CSR_VSISELECT) & ISELECT_MASK;
if (isel >= ISELECT_IPRIO0 && isel <= ISELECT_IPRIO15)
diff --git a/arch/riscv/kvm/aia_imsic.c b/arch/riscv/kvm/aia_imsic.c
index 29ef9c2133a9..d8e6f14850c0 100644
--- a/arch/riscv/kvm/aia_imsic.c
+++ b/arch/riscv/kvm/aia_imsic.c
@@ -361,6 +361,14 @@ static int imsic_mrif_rmw(struct imsic_mrif *mrif, u32 nr_eix,
return 0;
}
+bool kvm_riscv_aia_imsic_state_hw_backed(struct kvm_vcpu *vcpu)
+{
+ struct kvm_vcpu_aia *vaia = &vcpu->arch.aia_context;
+ struct imsic *imsic = vaia->imsic_state;
+
+ return imsic && imsic->vsfile_cpu >= 0;
+}
+
struct imsic_vsfile_read_data {
int hgei;
u32 nr_eix;
--
2.43.0
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 2/5] RISC-V: KVM: Add a hstateen lazy enabler helper function
2025-05-22 20:35 [PATCH v2 0/5] Enable hstateen bits lazily for the KVM RISC-V Guests Atish Patra
2025-05-22 20:35 ` [PATCH v2 1/5] RISC-V: KVM: Lazy enable hstateen IMSIC & ISEL bit Atish Patra
@ 2025-05-22 20:35 ` Atish Patra
2025-05-22 20:35 ` [PATCH v2 3/5] RISC-V: KVM: Support lazy enabling of siselect and aia bits Atish Patra
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Atish Patra @ 2025-05-22 20:35 UTC (permalink / raw)
To: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt,
Alexandre Ghiti
Cc: kvm, kvm-riscv, linux-riscv, linux-kernel, Atish Patra
Hstateen has different bits that can be enabled lazily at runtime.
Most of them have similar functionality where the hstateen bit must
be enabled if not enabled already. The correpsonding config bit in
vcpu must be enabled as well so that hstateen CSR is updated correctly
during the next vcpu load. In absesnce of Smstateen extension, exit
to the userspace in the trap because CSR access control exists
architecturally only if Smstateen extension is available.
Add a common helper function to achieve the above said objective.
Signed-off-by: Atish Patra <atishp@rivosinc.com>
---
arch/riscv/include/asm/kvm_vcpu_insn.h | 4 ++++
arch/riscv/kvm/vcpu_insn.c | 30 ++++++++++++++++++++++++++++++
2 files changed, 34 insertions(+)
diff --git a/arch/riscv/include/asm/kvm_vcpu_insn.h b/arch/riscv/include/asm/kvm_vcpu_insn.h
index 350011c83581..1125f3f1c8c4 100644
--- a/arch/riscv/include/asm/kvm_vcpu_insn.h
+++ b/arch/riscv/include/asm/kvm_vcpu_insn.h
@@ -6,6 +6,8 @@
#ifndef __KVM_VCPU_RISCV_INSN_H
#define __KVM_VCPU_RISCV_INSN_H
+#include <linux/kvm_types.h>
+
struct kvm_vcpu;
struct kvm_run;
struct kvm_cpu_trap;
@@ -44,5 +46,7 @@ int kvm_riscv_vcpu_mmio_store(struct kvm_vcpu *vcpu, struct kvm_run *run,
unsigned long fault_addr,
unsigned long htinst);
int kvm_riscv_vcpu_mmio_return(struct kvm_vcpu *vcpu, struct kvm_run *run);
+int kvm_riscv_vcpu_hstateen_lazy_enable(struct kvm_vcpu *vcpu, unsigned int csr_num,
+ uint64_t hstateen_feature_bit_mask);
#endif
diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c
index 97dec18e6989..0a7e229cfd34 100644
--- a/arch/riscv/kvm/vcpu_insn.c
+++ b/arch/riscv/kvm/vcpu_insn.c
@@ -235,6 +235,36 @@ static int seed_csr_rmw(struct kvm_vcpu *vcpu, unsigned int csr_num,
return KVM_INSN_EXIT_TO_USER_SPACE;
}
+int kvm_riscv_vcpu_hstateen_lazy_enable(struct kvm_vcpu *vcpu, unsigned int csr_num,
+ uint64_t hstateen_feature_bit_mask)
+{
+ /* Access from VS shouldn't trap if smstaeen is not present */
+ if (!riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN))
+ return KVM_INSN_EXIT_TO_USER_SPACE;
+
+ /*
+ * Make sure that KVM doesn't enable any guest visible state via sstateen (lower 32 bits)
+ * yet. Access is restricted to prevent unintended behavior.
+ */
+ if (hstateen_feature_bit_mask & GENMASK(31, 0)) {
+ pr_err("Unexpected access from lower 32 bits of hstateen0\n");
+ return KVM_INSN_EXIT_TO_USER_SPACE;
+ }
+
+ /* Enable the bit in hstateen0 lazily upon first access */
+ if (!(vcpu->arch.cfg.hstateen0 & hstateen_feature_bit_mask)) {
+ vcpu->arch.cfg.hstateen0 |= hstateen_feature_bit_mask;
+ csr_set(CSR_HSTATEEN0, hstateen_feature_bit_mask);
+ if (IS_ENABLED(CONFIG_32BIT))
+ csr_set(CSR_HSTATEEN0H, hstateen_feature_bit_mask >> 32);
+ } else {
+ return KVM_INSN_EXIT_TO_USER_SPACE;
+ }
+
+ /* Let the guest retry the instruction read after hstateen0 is modified */
+ return KVM_INSN_CONTINUE_SAME_SEPC;
+}
+
static const struct csr_func csr_funcs[] = {
KVM_RISCV_VCPU_AIA_CSR_FUNCS
KVM_RISCV_VCPU_HPMCOUNTER_CSR_FUNCS
--
2.43.0
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 3/5] RISC-V: KVM: Support lazy enabling of siselect and aia bits
2025-05-22 20:35 [PATCH v2 0/5] Enable hstateen bits lazily for the KVM RISC-V Guests Atish Patra
2025-05-22 20:35 ` [PATCH v2 1/5] RISC-V: KVM: Lazy enable hstateen IMSIC & ISEL bit Atish Patra
2025-05-22 20:35 ` [PATCH v2 2/5] RISC-V: KVM: Add a hstateen lazy enabler helper function Atish Patra
@ 2025-05-22 20:35 ` Atish Patra
2025-05-22 20:35 ` [PATCH v2 4/5] RISC-V: KVM: Enable envcfg and sstateen bits lazily Atish Patra
2025-05-22 20:35 ` [PATCH v2 5/5] RISC-V: KVM: Remove the boot time enabling of hstateen bits Atish Patra
4 siblings, 0 replies; 6+ messages in thread
From: Atish Patra @ 2025-05-22 20:35 UTC (permalink / raw)
To: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt,
Alexandre Ghiti
Cc: kvm, kvm-riscv, linux-riscv, linux-kernel, Atish Patra
Smstateen extension controls the SISELECT and SIPH/SIEH register
through hstateen.AIA bit (58). Add lazy enabling support for those
bits.
Signed-off-by: Atish Patra <atishp@rivosinc.com>
---
arch/riscv/include/asm/kvm_aia.h | 13 ++++++++++++-
arch/riscv/kvm/aia.c | 34 ++++++++++++++++++++++++++++++++++
arch/riscv/kvm/vcpu_insn.c | 3 +++
3 files changed, 49 insertions(+), 1 deletion(-)
diff --git a/arch/riscv/include/asm/kvm_aia.h b/arch/riscv/include/asm/kvm_aia.h
index 760a1aef09f7..9e39b0e15169 100644
--- a/arch/riscv/include/asm/kvm_aia.h
+++ b/arch/riscv/include/asm/kvm_aia.h
@@ -142,12 +142,23 @@ int kvm_riscv_vcpu_aia_rmw_topei(struct kvm_vcpu *vcpu,
unsigned long *val,
unsigned long new_val,
unsigned long wr_mask);
+int kvm_riscv_vcpu_aia_hstateen_enable(struct kvm_vcpu *vcpu,
+ unsigned int csr_num, unsigned long *val,
+ unsigned long new_val, unsigned long wr_mask);
+int kvm_riscv_vcpu_aia_rmw_isel(struct kvm_vcpu *vcpu, unsigned int csr_num, unsigned long *val,
+ unsigned long new_val, unsigned long wr_mask);
int kvm_riscv_vcpu_aia_rmw_ireg(struct kvm_vcpu *vcpu, unsigned int csr_num,
unsigned long *val, unsigned long new_val,
unsigned long wr_mask);
#define KVM_RISCV_VCPU_AIA_CSR_FUNCS \
{ .base = CSR_SIREG, .count = 1, .func = kvm_riscv_vcpu_aia_rmw_ireg }, \
-{ .base = CSR_STOPEI, .count = 1, .func = kvm_riscv_vcpu_aia_rmw_topei },
+{ .base = CSR_SISELECT, .count = 1, .func = kvm_riscv_vcpu_aia_rmw_isel }, \
+{ .base = CSR_STOPEI, .count = 1, .func = kvm_riscv_vcpu_aia_rmw_topei }, \
+{ .base = CSR_STOPI, .count = 1, .func = kvm_riscv_vcpu_aia_hstateen_enable }, \
+
+#define KVM_RISCV_VCPU_AIA_CSR_32BIT_FUNCS \
+{ .base = CSR_SIPH, .count = 1, .func = kvm_riscv_vcpu_aia_hstateen_enable }, \
+{ .base = CSR_SIEH, .count = 1, .func = kvm_riscv_vcpu_aia_hstateen_enable }, \
int kvm_riscv_vcpu_aia_update(struct kvm_vcpu *vcpu);
void kvm_riscv_vcpu_aia_reset(struct kvm_vcpu *vcpu);
diff --git a/arch/riscv/kvm/aia.c b/arch/riscv/kvm/aia.c
index 1e0d2217ade7..3dfabf51a4d2 100644
--- a/arch/riscv/kvm/aia.c
+++ b/arch/riscv/kvm/aia.c
@@ -235,6 +235,40 @@ int kvm_riscv_vcpu_aia_set_csr(struct kvm_vcpu *vcpu,
return 0;
}
+int kvm_riscv_vcpu_aia_hstateen_enable(struct kvm_vcpu *vcpu,
+ unsigned int csr_num,
+ unsigned long *val,
+ unsigned long new_val,
+ unsigned long wr_mask)
+{
+ /* If AIA not available then redirect trap */
+ if (!kvm_riscv_aia_available())
+ return KVM_INSN_ILLEGAL_TRAP;
+
+ /* If AIA not initialized then forward to user space */
+ if (!kvm_riscv_aia_initialized(vcpu->kvm))
+ return KVM_INSN_EXIT_TO_USER_SPACE;
+
+ return kvm_riscv_vcpu_hstateen_lazy_enable(vcpu, csr_num, SMSTATEEN0_AIA);
+}
+
+int kvm_riscv_vcpu_aia_rmw_isel(struct kvm_vcpu *vcpu,
+ unsigned int csr_num,
+ unsigned long *val,
+ unsigned long new_val,
+ unsigned long wr_mask)
+{
+ /* If AIA not available then redirect trap */
+ if (!kvm_riscv_aia_available())
+ return KVM_INSN_ILLEGAL_TRAP;
+
+ /* If AIA not initialized then forward to user space */
+ if (!kvm_riscv_aia_initialized(vcpu->kvm))
+ return KVM_INSN_EXIT_TO_USER_SPACE;
+
+ return kvm_riscv_vcpu_hstateen_lazy_enable(vcpu, csr_num, SMSTATEEN0_AIA_ISEL);
+}
+
int kvm_riscv_vcpu_aia_rmw_topei(struct kvm_vcpu *vcpu,
unsigned int csr_num,
unsigned long *val,
diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c
index 0a7e229cfd34..ef4fcb641f1c 100644
--- a/arch/riscv/kvm/vcpu_insn.c
+++ b/arch/riscv/kvm/vcpu_insn.c
@@ -269,6 +269,9 @@ static const struct csr_func csr_funcs[] = {
KVM_RISCV_VCPU_AIA_CSR_FUNCS
KVM_RISCV_VCPU_HPMCOUNTER_CSR_FUNCS
{ .base = CSR_SEED, .count = 1, .func = seed_csr_rmw },
+#ifdef CONFIG_32BIT
+ KVM_RISCV_VCPU_AIA_CSR_32BIT_FUNCS
+#endif
};
/**
--
2.43.0
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 4/5] RISC-V: KVM: Enable envcfg and sstateen bits lazily
2025-05-22 20:35 [PATCH v2 0/5] Enable hstateen bits lazily for the KVM RISC-V Guests Atish Patra
` (2 preceding siblings ...)
2025-05-22 20:35 ` [PATCH v2 3/5] RISC-V: KVM: Support lazy enabling of siselect and aia bits Atish Patra
@ 2025-05-22 20:35 ` Atish Patra
2025-05-22 20:35 ` [PATCH v2 5/5] RISC-V: KVM: Remove the boot time enabling of hstateen bits Atish Patra
4 siblings, 0 replies; 6+ messages in thread
From: Atish Patra @ 2025-05-22 20:35 UTC (permalink / raw)
To: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt,
Alexandre Ghiti
Cc: kvm, kvm-riscv, linux-riscv, linux-kernel, Atish Patra
SENVCFG and SSTATEEN CSRs are controlled by HSENVCFG(62) and
SSTATEEN0(63) bits in hstateen. Enable them lazily at runtime
instead of bootime.
Signed-off-by: Atish Patra <atishp@rivosinc.com>
---
arch/riscv/kvm/vcpu_insn.c | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c
index ef4fcb641f1c..6f2bba7533cf 100644
--- a/arch/riscv/kvm/vcpu_insn.c
+++ b/arch/riscv/kvm/vcpu_insn.c
@@ -265,9 +265,37 @@ int kvm_riscv_vcpu_hstateen_lazy_enable(struct kvm_vcpu *vcpu, unsigned int csr_
return KVM_INSN_CONTINUE_SAME_SEPC;
}
+static int kvm_riscv_vcpu_hstateen_enable_senvcfg(struct kvm_vcpu *vcpu,
+ unsigned int csr_num,
+ unsigned long *val,
+ unsigned long new_val,
+ unsigned long wr_mask)
+{
+ return kvm_riscv_vcpu_hstateen_lazy_enable(vcpu, csr_num, SMSTATEEN0_HSENVCFG);
+}
+
+static int kvm_riscv_vcpu_hstateen_enable_stateen(struct kvm_vcpu *vcpu,
+ unsigned int csr_num,
+ unsigned long *val,
+ unsigned long new_val,
+ unsigned long wr_mask)
+{
+ const unsigned long *isa = vcpu->arch.isa;
+
+ if (riscv_isa_extension_available(isa, SMSTATEEN))
+ return kvm_riscv_vcpu_hstateen_lazy_enable(vcpu, csr_num, SMSTATEEN0_SSTATEEN0);
+ else
+ return KVM_INSN_EXIT_TO_USER_SPACE;
+}
+
+#define KVM_RISCV_VCPU_STATEEN_CSR_FUNCS \
+{ .base = CSR_SENVCFG, .count = 1, .func = kvm_riscv_vcpu_hstateen_enable_senvcfg }, \
+{ .base = CSR_SSTATEEN0, .count = 1, .func = kvm_riscv_vcpu_hstateen_enable_stateen },\
+
static const struct csr_func csr_funcs[] = {
KVM_RISCV_VCPU_AIA_CSR_FUNCS
KVM_RISCV_VCPU_HPMCOUNTER_CSR_FUNCS
+ KVM_RISCV_VCPU_STATEEN_CSR_FUNCS
{ .base = CSR_SEED, .count = 1, .func = seed_csr_rmw },
#ifdef CONFIG_32BIT
KVM_RISCV_VCPU_AIA_CSR_32BIT_FUNCS
--
2.43.0
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 5/5] RISC-V: KVM: Remove the boot time enabling of hstateen bits
2025-05-22 20:35 [PATCH v2 0/5] Enable hstateen bits lazily for the KVM RISC-V Guests Atish Patra
` (3 preceding siblings ...)
2025-05-22 20:35 ` [PATCH v2 4/5] RISC-V: KVM: Enable envcfg and sstateen bits lazily Atish Patra
@ 2025-05-22 20:35 ` Atish Patra
4 siblings, 0 replies; 6+ messages in thread
From: Atish Patra @ 2025-05-22 20:35 UTC (permalink / raw)
To: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt,
Alexandre Ghiti
Cc: kvm, kvm-riscv, linux-riscv, linux-kernel, Atish Patra
All the existing hstateen bits can be enabled at runtime upon
first access now. Remove the default enabling at bootime now.
Signed-off-by: Atish Patra <atishp@rivosinc.com>
---
arch/riscv/kvm/vcpu.c | 10 ----------
1 file changed, 10 deletions(-)
diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c
index 02635bac91f1..aa8f7f67646a 100644
--- a/arch/riscv/kvm/vcpu.c
+++ b/arch/riscv/kvm/vcpu.c
@@ -562,16 +562,6 @@ static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu)
!riscv_isa_extension_available(isa, SVADE))
cfg->henvcfg |= ENVCFG_ADUE;
- if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN)) {
- cfg->hstateen0 |= SMSTATEEN0_HSENVCFG;
- if (riscv_isa_extension_available(isa, SSAIA))
- cfg->hstateen0 |= SMSTATEEN0_AIA_IMSIC |
- SMSTATEEN0_AIA |
- SMSTATEEN0_AIA_ISEL;
- if (riscv_isa_extension_available(isa, SMSTATEEN))
- cfg->hstateen0 |= SMSTATEEN0_SSTATEEN0;
- }
-
cfg->hedeleg = KVM_HEDELEG_DEFAULT;
if (vcpu->guest_debug)
cfg->hedeleg &= ~BIT(EXC_BREAKPOINT);
--
2.43.0
--
kvm-riscv mailing list
kvm-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
^ permalink raw reply related [flat|nested] 6+ messages in thread
end of thread, other threads:[~2025-05-22 20:35 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-22 20:35 [PATCH v2 0/5] Enable hstateen bits lazily for the KVM RISC-V Guests Atish Patra
2025-05-22 20:35 ` [PATCH v2 1/5] RISC-V: KVM: Lazy enable hstateen IMSIC & ISEL bit Atish Patra
2025-05-22 20:35 ` [PATCH v2 2/5] RISC-V: KVM: Add a hstateen lazy enabler helper function Atish Patra
2025-05-22 20:35 ` [PATCH v2 3/5] RISC-V: KVM: Support lazy enabling of siselect and aia bits Atish Patra
2025-05-22 20:35 ` [PATCH v2 4/5] RISC-V: KVM: Enable envcfg and sstateen bits lazily Atish Patra
2025-05-22 20:35 ` [PATCH v2 5/5] RISC-V: KVM: Remove the boot time enabling of hstateen bits Atish Patra
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).