Skip to contentSkip to conversation

RISC-V: KVM: Redirect AMO load/store misaligned traps to guest

PATCH In review wchen 2 participants 3 messages May 20, 2023 – Jun 6, 2023

Subsystems (3)
lore
Share & tools
lore

wchen

author
#0 lore raw

PATCH 1/1 RISC-V: KVM: Redirect AMO load/store misaligned traps to guest

The M-mode redirects an unhandled misaligned trap back to S-mode when not delegating it to VS-mode(hedeleg). However, KVM running in HS-mode terminates the VS-mode software when back from M-mode.
The KVM should redirect the trap back to VS-mode, and let VS-mode trap handler decide the next step. Here is a way to handle misaligned traps in KVM, not only directing them to VS-mode or terminate it.

---
 arch/riscv/include/asm/csr.h | 2 ++
 arch/riscv/kvm/vcpu_exit.c   | 2 ++
 2 files changed, 4 insertions(+)
arch/riscv/include/asm/csr.h +2 -0
index b6acb7ed1..917814a0f 100644
@@ -82,7 +82,9 @@
 #define EXC_INST_ACCESS		1
 #define EXC_INST_ILLEGAL	2
 #define EXC_BREAKPOINT		3
+#define EXC_LOAD_MISALIGNED	4
 #define EXC_LOAD_ACCESS		5
+#define EXC_STORE_MISALIGNED	6
 #define EXC_STORE_ACCESS	7
 #define EXC_SYSCALL		8
 #define EXC_HYPERVISOR_SYSCALL	9
arch/riscv/kvm/vcpu_exit.c +2 -0
index 4ea101a73..2415722c0 100644
@@ -183,6 +183,8 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
 	run->exit_reason = KVM_EXIT_UNKNOWN;
 	switch (trap->scause) {
 	case EXC_INST_ILLEGAL:
+	case EXC_LOAD_MISALIGNED:
+	case EXC_STORE_MISALIGNED:
 		if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) {
 			kvm_riscv_vcpu_trap_redirect(vcpu, trap);
 			ret = 1;
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · On Sat, May 20, 2023 at 8:31?PM wchen <[email protected]> wrote:

The M-mode redirects an unhandled misaligned trap back to S-mode when not delegating it to VS-mode(hedeleg). However, KVM running in HS-mode terminates the VS-mode software when back from M-mode.
The KVM should redirect the trap back to VS-mode, and let VS-mode trap handler decide the next step. Here is a way to handle misaligned traps in KVM, not only directing them to VS-mode or terminate it.

Looks good to me.

Regards,
Anup

Quoted 35 lines
---
 arch/riscv/include/asm/csr.h | 2 ++
 arch/riscv/kvm/vcpu_exit.c   | 2 ++
 2 files changed, 4 insertions(+)
arch/riscv/include/asm/csr.h +2 -0
index b6acb7ed1..917814a0f 100644
@@ -82,7 +82,9 @@
 #define EXC_INST_ACCESS                1
 #define EXC_INST_ILLEGAL       2
 #define EXC_BREAKPOINT         3
+#define EXC_LOAD_MISALIGNED    4
 #define EXC_LOAD_ACCESS                5
+#define EXC_STORE_MISALIGNED   6
 #define EXC_STORE_ACCESS       7
 #define EXC_SYSCALL            8
 #define EXC_HYPERVISOR_SYSCALL 9
arch/riscv/kvm/vcpu_exit.c +2 -1
index 4ea101a73..2415722c0 100644
@@ -183,6 +183,8 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
        run->exit_reason = KVM_EXIT_UNKNOWN;
        switch (trap->scause) {
        case EXC_INST_ILLEGAL:
+       case EXC_LOAD_MISALIGNED:
+       case EXC_STORE_MISALIGNED:
                if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) {
                        kvm_riscv_vcpu_trap_redirect(vcpu, trap);
                        ret = 1;
--

2.34.1

maintainer
#2 lore raw
In reply to wchen
Quoted 11 lines · On Sat, May 20, 2023 at 8:31?PM wchen <[email protected]> wrote:

The M-mode redirects an unhandled misaligned trap back to S-mode when not delegating it to VS-mode(hedeleg). However, KVM running in HS-mode terminates the VS-mode software when back from M-mode.
The KVM should redirect the trap back to VS-mode, and let VS-mode trap handler decide the next step. Here is a way to handle misaligned traps in KVM, not only directing them to VS-mode or terminate it.

Queued this patch for 6.5

Thanks,
Anup

Quoted 35 lines
---
 arch/riscv/include/asm/csr.h | 2 ++
 arch/riscv/kvm/vcpu_exit.c   | 2 ++
 2 files changed, 4 insertions(+)
arch/riscv/include/asm/csr.h +2 -0
index b6acb7ed1..917814a0f 100644
@@ -82,7 +82,9 @@
 #define EXC_INST_ACCESS                1
 #define EXC_INST_ILLEGAL       2
 #define EXC_BREAKPOINT         3
+#define EXC_LOAD_MISALIGNED    4
 #define EXC_LOAD_ACCESS                5
+#define EXC_STORE_MISALIGNED   6
 #define EXC_STORE_ACCESS       7
 #define EXC_SYSCALL            8
 #define EXC_HYPERVISOR_SYSCALL 9
arch/riscv/kvm/vcpu_exit.c +2 -1
index 4ea101a73..2415722c0 100644
@@ -183,6 +183,8 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
        run->exit_reason = KVM_EXIT_UNKNOWN;
        switch (trap->scause) {
        case EXC_INST_ILLEGAL:
+       case EXC_LOAD_MISALIGNED:
+       case EXC_STORE_MISALIGNED:
                if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) {
                        kvm_riscv_vcpu_trap_redirect(vcpu, trap);
                        ret = 1;
--

2.34.1