From: Anup Patel <anup.patel@oss.qualcomm.com>
To: Paolo Bonzini <pbonzini@redhat.com>, Atish Patra <atish.patra@linux.dev>
Cc: Palmer Dabbelt <palmer@dabbelt.com>,
Paul Walmsley <pjw@kernel.org>, Alexandre Ghiti <alex@ghiti.fr>,
Shuah Khan <shuah@kernel.org>, Anup Patel <anup@brainfault.org>,
Andrew Jones <andrew.jones@oss.qualcomm.com>,
kvm-riscv@lists.infradead.org, kvm@vger.kernel.org,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org,
Anup Patel <anup.patel@oss.qualcomm.com>
Subject: [PATCH 23/27] RISC-V: KVM: Add HFENCE emulation for nested virtualization
Date: Tue, 20 Jan 2026 13:30:09 +0530 [thread overview]
Message-ID: <20260120080013.2153519-24-anup.patel@oss.qualcomm.com> (raw)
In-Reply-To: <20260120080013.2153519-1-anup.patel@oss.qualcomm.com>
The Guest HS-mode (aka L1/guest hypervisor) needs HFENCE instructions
for TLB maintenance of the nested guest physical addresses so add
corresponding HFENCE emulation.
Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>
---
arch/riscv/include/asm/insn.h | 6 ++
arch/riscv/include/asm/kvm_vcpu_nested.h | 4 ++
arch/riscv/kvm/vcpu_insn.c | 10 +++
arch/riscv/kvm/vcpu_nested_insn.c | 86 ++++++++++++++++++++++++
4 files changed, 106 insertions(+)
diff --git a/arch/riscv/include/asm/insn.h b/arch/riscv/include/asm/insn.h
index 24a8abb3283c..6896ba0581b5 100644
--- a/arch/riscv/include/asm/insn.h
+++ b/arch/riscv/include/asm/insn.h
@@ -340,6 +340,12 @@ static __always_inline bool riscv_insn_is_c_jalr(u32 code)
#define INSN_MASK_WRS 0xffffffff
#define INSN_MATCH_WRS 0x00d00073
+#define INSN_MASK_HFENCE_VVMA 0xfe007fff
+#define INSN_MATCH_HFENCE_VVMA 0x22000073
+
+#define INSN_MASK_HFENCE_GVMA 0xfe007fff
+#define INSN_MATCH_HFENCE_GVMA 0x62000073
+
#define INSN_MATCH_CSRRW 0x1073
#define INSN_MASK_CSRRW 0x707f
#define INSN_MATCH_CSRRS 0x2073
diff --git a/arch/riscv/include/asm/kvm_vcpu_nested.h b/arch/riscv/include/asm/kvm_vcpu_nested.h
index 5262ec4f37b7..db6d89cf9771 100644
--- a/arch/riscv/include/asm/kvm_vcpu_nested.h
+++ b/arch/riscv/include/asm/kvm_vcpu_nested.h
@@ -64,6 +64,10 @@ struct kvm_vcpu_nested {
#define kvm_riscv_vcpu_nested_virt(__vcpu) ((__vcpu)->arch.nested.virt)
int kvm_riscv_vcpu_nested_insn_sret(struct kvm_vcpu *vcpu, struct kvm_run *run, ulong insn);
+int kvm_riscv_vcpu_nested_insn_hfence_vvma(struct kvm_vcpu *vcpu, struct kvm_run *run,
+ ulong insn);
+int kvm_riscv_vcpu_nested_insn_hfence_gvma(struct kvm_vcpu *vcpu, struct kvm_run *run,
+ ulong insn);
int kvm_riscv_vcpu_nested_smode_csr_rmw(struct kvm_vcpu *vcpu, unsigned int csr_num,
unsigned long *val, unsigned long new_val,
diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c
index 0246ca2d5e93..8f11cda133ac 100644
--- a/arch/riscv/kvm/vcpu_insn.c
+++ b/arch/riscv/kvm/vcpu_insn.c
@@ -303,6 +303,16 @@ static const struct insn_func system_opcode_funcs[] = {
.match = INSN_MATCH_WRS,
.func = wrs_insn,
},
+ {
+ .mask = INSN_MASK_HFENCE_VVMA,
+ .match = INSN_MATCH_HFENCE_VVMA,
+ .func = kvm_riscv_vcpu_nested_insn_hfence_vvma,
+ },
+ {
+ .mask = INSN_MASK_HFENCE_GVMA,
+ .match = INSN_MATCH_HFENCE_GVMA,
+ .func = kvm_riscv_vcpu_nested_insn_hfence_gvma,
+ },
};
static int system_opcode_insn(struct kvm_vcpu *vcpu, struct kvm_run *run,
diff --git a/arch/riscv/kvm/vcpu_nested_insn.c b/arch/riscv/kvm/vcpu_nested_insn.c
index 8f5b2992dbb9..7e57d3215930 100644
--- a/arch/riscv/kvm/vcpu_nested_insn.c
+++ b/arch/riscv/kvm/vcpu_nested_insn.c
@@ -4,6 +4,7 @@
*/
#include <linux/kvm_host.h>
+#include <asm/insn.h>
#include <asm/kvm_nacl.h>
#include <asm/kvm_vcpu_insn.h>
@@ -52,3 +53,88 @@ int kvm_riscv_vcpu_nested_insn_sret(struct kvm_vcpu *vcpu, struct kvm_run *run,
return KVM_INSN_CONTINUE_SAME_SEPC;
}
+
+int kvm_riscv_vcpu_nested_insn_hfence_vvma(struct kvm_vcpu *vcpu, struct kvm_run *run,
+ ulong insn)
+{
+ unsigned int vmid = (vcpu->arch.nested.csr.hgatp & HGATP_VMID) >> HGATP_VMID_SHIFT;
+ unsigned long vaddr = GET_RS1(insn, &vcpu->arch.guest_context);
+ unsigned int asid = GET_RS2(insn, &vcpu->arch.guest_context);
+ unsigned int rs1_num = (insn >> SH_RS1) & MASK_RX;
+ unsigned int rs2_num = (insn >> SH_RS2) & MASK_RX;
+
+ /*
+ * If H-extension is not available for VCPU then forward trap
+ * as illegal instruction trap to virtual-HS mode.
+ */
+ if (!riscv_isa_extension_available(vcpu->arch.isa, h))
+ return KVM_INSN_ILLEGAL_TRAP;
+
+ /*
+ * Trap from virtual-VS and virtual-VU modes should be forwarded
+ * to virtual-HS mode as a virtual instruction trap.
+ */
+ if (kvm_riscv_vcpu_nested_virt(vcpu))
+ return KVM_INSN_VIRTUAL_TRAP;
+
+ /*
+ * H-extension instructions not allowed in virtual-U mode so
+ * forward trap as illegal instruction trap to virtual-HS mode.
+ */
+ if (!(vcpu->arch.guest_context.hstatus & HSTATUS_SPVP))
+ return KVM_INSN_ILLEGAL_TRAP;
+
+ if (!rs1_num && !rs2_num)
+ kvm_riscv_vcpu_nested_swtlb_vvma_flush(vcpu, 0, 0, 0, vmid);
+ else if (!rs1_num && rs2_num)
+ kvm_riscv_vcpu_nested_swtlb_vvma_flush_asid(vcpu, 0, 0, 0, vmid, asid);
+ else if (rs1_num && !rs2_num)
+ kvm_riscv_vcpu_nested_swtlb_vvma_flush(vcpu, vaddr, PAGE_SIZE, PAGE_SHIFT, vmid);
+ else
+ kvm_riscv_vcpu_nested_swtlb_vvma_flush_asid(vcpu, vaddr, PAGE_SIZE, PAGE_SHIFT,
+ vmid, asid);
+
+ return KVM_INSN_CONTINUE_NEXT_SEPC;
+}
+
+int kvm_riscv_vcpu_nested_insn_hfence_gvma(struct kvm_vcpu *vcpu, struct kvm_run *run,
+ ulong insn)
+{
+ unsigned int vmid = GET_RS2(insn, &vcpu->arch.guest_context);
+ gpa_t gaddr = GET_RS1(insn, &vcpu->arch.guest_context) << 2;
+ unsigned int rs1_num = (insn >> SH_RS1) & MASK_RX;
+ unsigned int rs2_num = (insn >> SH_RS2) & MASK_RX;
+
+ /*
+ * If H-extension is not available for VCPU then forward trap
+ * as illegal instruction trap to virtual-HS mode.
+ */
+ if (!riscv_isa_extension_available(vcpu->arch.isa, h))
+ return KVM_INSN_ILLEGAL_TRAP;
+
+ /*
+ * Trap from virtual-VS and virtual-VU modes should be forwarded
+ * to virtual-HS mode as a virtual instruction trap.
+ */
+ if (kvm_riscv_vcpu_nested_virt(vcpu))
+ return KVM_INSN_VIRTUAL_TRAP;
+
+ /*
+ * H-extension instructions not allowed in virtual-U mode so
+ * forward trap as illegal instruction trap to virtual-HS mode.
+ */
+ if (!(vcpu->arch.guest_context.hstatus & HSTATUS_SPVP))
+ return KVM_INSN_ILLEGAL_TRAP;
+
+ if (!rs1_num && !rs2_num)
+ kvm_riscv_vcpu_nested_swtlb_gvma_flush(vcpu, 0, 0, 0);
+ else if (!rs1_num && rs2_num)
+ kvm_riscv_vcpu_nested_swtlb_gvma_flush_vmid(vcpu, 0, 0, 0, vmid);
+ else if (rs1_num && !rs2_num)
+ kvm_riscv_vcpu_nested_swtlb_gvma_flush(vcpu, gaddr, PAGE_SIZE, PAGE_SHIFT);
+ else
+ kvm_riscv_vcpu_nested_swtlb_gvma_flush_vmid(vcpu, gaddr, PAGE_SIZE, PAGE_SHIFT,
+ vmid);
+
+ return KVM_INSN_CONTINUE_NEXT_SEPC;
+}
--
2.43.0
next prev parent reply other threads:[~2026-01-20 8:00 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-20 7:59 [PATCH 00/27] Nested virtualization for KVM RISC-V Anup Patel
2026-01-20 7:59 ` [PATCH 01/27] RISC-V: KVM: Fix error code returned for Smstateen ONE_REG Anup Patel
2026-03-06 7:04 ` Anup Patel
2026-01-20 7:59 ` [PATCH 02/27] RISC-V: KVM: Fix error code returned for Ssaia ONE_REG Anup Patel
2026-03-06 7:04 ` Anup Patel
2026-01-20 7:59 ` [PATCH 03/27] RISC-V: KVM: Check host Ssaia extension when creating AIA irqchip Anup Patel
2026-03-06 7:04 ` Anup Patel
2026-01-20 7:59 ` [PATCH 04/27] RISC-V: KVM: Introduce common kvm_riscv_isa_check_host() Anup Patel
2026-03-13 14:22 ` Radim Krčmář
2026-01-20 7:59 ` [PATCH 05/27] RISC-V: KVM: Factor-out ISA checks into separate sources Anup Patel
2026-03-13 14:14 ` Radim Krčmář
2026-04-03 12:34 ` Anup Patel
2026-01-20 7:59 ` [PATCH 06/27] RISC-V: KVM: Move timer state defines closer to struct in UAPI header Anup Patel
2026-01-20 7:59 ` [PATCH 07/27] RISC-V: KVM: Add hideleg to struct kvm_vcpu_config Anup Patel
2026-03-13 13:49 ` Radim Krčmář
2026-01-20 7:59 ` [PATCH 08/27] RISC-V: KVM: Factor-out VCPU config into separate sources Anup Patel
2026-03-13 13:46 ` Radim Krčmář
2026-01-20 7:59 ` [PATCH 09/27] RISC-V: KVM: Don't check hstateen0 when updating sstateen0 CSR Anup Patel
2026-03-13 13:27 ` Radim Krčmář
2026-01-20 7:59 ` [PATCH 10/27] RISC-V: KVM: Initial skeletal nested virtualization support Anup Patel
2026-01-20 7:59 ` [PATCH 11/27] RISC-V: KVM: Use half VMID space for nested guest Anup Patel
2026-01-20 7:59 ` [PATCH 12/27] RISC-V: KVM: Extend kvm_riscv_mmu_update_hgatp() for nested virtualization Anup Patel
2026-01-20 7:59 ` [PATCH 13/27] RISC-V: KVM: Extend kvm_riscv_vcpu_config_load() " Anup Patel
2026-01-20 8:00 ` [PATCH 14/27] RISC-V: KVM: Extend kvm_riscv_vcpu_update_timedelta() for nested virt Anup Patel
2026-01-20 8:00 ` [PATCH 15/27] RISC-V: KVM: Extend trap redirection for nested virtualization Anup Patel
2026-01-20 8:00 ` [PATCH 16/27] RISC-V: KVM: Check and inject nested virtual interrupts Anup Patel
2026-01-20 8:00 ` [PATCH 17/27] RISC-V: KVM: Extend kvm_riscv_isa_check_host() for nested virt Anup Patel
2026-01-20 8:00 ` [PATCH 18/27] RISC-V: KVM: Trap-n-emulate SRET for Guest HS-mode Anup Patel
2026-01-20 8:00 ` [PATCH 19/27] RISC-V: KVM: Redirect nested supervisor ecall and breakpoint traps Anup Patel
2026-01-20 8:00 ` [PATCH 20/27] RISC-V: KVM: Redirect nested WFI and WRS traps Anup Patel
2026-01-20 8:00 ` [PATCH 21/27] RISC-V: KVM: Implement remote HFENCE SBI calls for guest Anup Patel
2026-01-20 8:00 ` [PATCH 22/27] RISC-V: KVM: Add CSR emulation for nested virtualization Anup Patel
2026-01-20 8:00 ` Anup Patel [this message]
2026-01-20 8:00 ` [PATCH 24/27] RISC-V: KVM: Add ONE_REG interface for nested virtualization state Anup Patel
2026-01-20 8:00 ` [PATCH 25/27] RISC-V: KVM: selftests: Add nested virt state to get-reg-list test Anup Patel
2026-01-20 8:00 ` [PATCH 26/27] RISC-V: KVM: Add ONE_REG interface for nested virtualization CSRs Anup Patel
2026-01-20 8:00 ` [PATCH 27/27] RISC-V: KVM: selftests: Add nested virt CSRs to get-reg-list test Anup Patel
2026-04-03 12:36 ` [PATCH 00/27] Nested virtualization for KVM RISC-V Anup Patel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260120080013.2153519-24-anup.patel@oss.qualcomm.com \
--to=anup.patel@oss.qualcomm.com \
--cc=alex@ghiti.fr \
--cc=andrew.jones@oss.qualcomm.com \
--cc=anup@brainfault.org \
--cc=atish.patra@linux.dev \
--cc=kvm-riscv@lists.infradead.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@dabbelt.com \
--cc=pbonzini@redhat.com \
--cc=pjw@kernel.org \
--cc=shuah@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®