Skip to contentSkip to conversation

RISC-V: KVM: Fix privilege mode setting in kvm_riscv_vcpu_trap_redirect()

PATCH In review Anup Patel 4 participants 7 messages Jan 28, 2023 – Feb 2, 2023

Subsystems (3)
lore
Share & tools
lore

Anup Patel

author
#0 lore raw

PATCH v3 1/2 RISC-V: KVM: Fix privilege mode setting in kvm_riscv_vcpu_trap_redirect()

The kvm_riscv_vcpu_trap_redirect() should set guest privilege mode to supervisor mode because guest traps/interrupts are always handled in virtual supervisor mode.

Fixes 9f7013265112 ("RISC-V: KVM: Handle MMIO exits for VCPU")
---
 arch/riscv/kvm/vcpu_exit.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/kvm/vcpu_exit.c +3 -0
index c9f741ab26f5..af7c4bc07929 100644
@@ -160,6 +160,9 @@ void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu,
 
 	/* Set Guest PC to Guest exception vector */
 	vcpu->arch.guest_context.sepc = csr_read(CSR_VSTVEC);
+
+	/* Set Guest privilege mode to supervisor */
+	vcpu->arch.guest_context.sstatus |= SR_SPP;
 }
 
 /*
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 2/2 RISC-V: KVM: Redirect illegal instruction traps to guest

From: Andy Chiu <[email protected]>

The M-mode redirects an unhandled illegal instruction trap back to S-mode. However, KVM running in HS-mode terminates the VS-mode software when it receives illegal instruction trap. Instead, KVM should redirect the illegal instruction trap back to VS-mode, and let VS-mode trap handler decide the next step. This futher allows guest kernel to implement on-demand enabling of vector extension for a guest user space process upon first-use.

---
 arch/riscv/kvm/vcpu_exit.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/riscv/kvm/vcpu_exit.c +6 -0
index af7c4bc07929..4ea101a73d8b 100644
@@ -182,6 +182,12 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
 	ret = -EFAULT;
 	run->exit_reason = KVM_EXIT_UNKNOWN;
 	switch (trap->scause) {
+	case EXC_INST_ILLEGAL:
+		if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) {
+			kvm_riscv_vcpu_trap_redirect(vcpu, trap);
+			ret = 1;
+		}
+		break;
 	case EXC_VIRTUAL_INST_FAULT:
 		if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV)
 			ret = kvm_riscv_vcpu_virtual_insn(vcpu, run, trap);
Signature · 1 line
2.34.1
Reviewed-by on v3 2/2
#2 lore raw

Re: [PATCH v3 2/2] RISC-V: KVM: Redirect illegal instruction traps to guest

Quoted 36 lines · Anup Patel

From: Andy Chiu <[email protected]>

The M-mode redirects an unhandled illegal instruction trap back to S-mode. However, KVM running in HS-mode terminates the VS-mode software when it receives illegal instruction trap. Instead, KVM should redirect the illegal instruction trap back to VS-mode, and let VS-mode trap handler decide the next step. This futher allows guest kernel to implement on-demand enabling of vector extension for a guest user space process upon first-use.

---
 arch/riscv/kvm/vcpu_exit.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/riscv/kvm/vcpu_exit.c +6 -1
index af7c4bc07929..4ea101a73d8b 100644
@@ -182,6 +182,12 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
        ret = -EFAULT;
        run->exit_reason = KVM_EXIT_UNKNOWN;
        switch (trap->scause) {
+       case EXC_INST_ILLEGAL:
+               if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) {
+                       kvm_riscv_vcpu_trap_redirect(vcpu, trap);
+                       ret = 1;
+               }
+               break;
        case EXC_VIRTUAL_INST_FAULT:
                if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV)
                        ret = kvm_riscv_vcpu_virtual_insn(vcpu, run, trap);
--
Quoted 2 lines

2.34.1

Signature · 2 lines
Regards,
Atish
maintainer
#3 lore raw

Re: [PATCH v3 2/2] RISC-V: KVM: Redirect illegal instruction traps to guest

In reply to Anup Patel
Quoted 11 lines · Anup Patel

From: Andy Chiu <[email protected]>

The M-mode redirects an unhandled illegal instruction trap back to S-mode. However, KVM running in HS-mode terminates the VS-mode software when it receives illegal instruction trap. Instead, KVM should redirect the illegal instruction trap back to VS-mode, and let VS-mode trap handler decide the next step. This futher allows guest kernel to implement on-demand enabling of vector extension for a guest user space process upon first-use.

Quoted 3 lines

Queued this patch for Linux-6.3

Thanks,
Anup

Quoted 24 lines
---
 arch/riscv/kvm/vcpu_exit.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/riscv/kvm/vcpu_exit.c +6 -1
index af7c4bc07929..4ea101a73d8b 100644
@@ -182,6 +182,12 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
        ret = -EFAULT;
        run->exit_reason = KVM_EXIT_UNKNOWN;
        switch (trap->scause) {
+       case EXC_INST_ILLEGAL:
+               if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) {
+                       kvm_riscv_vcpu_trap_redirect(vcpu, trap);
+                       ret = 1;
+               }
+               break;
        case EXC_VIRTUAL_INST_FAULT:
                if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV)
                        ret = kvm_riscv_vcpu_virtual_insn(vcpu, run, trap);
--

2.34.1

Reviewed-by on v3 1/2
#4 lore raw
In reply to Anup Patel
Quoted 27 lines · Anup Patel

The kvm_riscv_vcpu_trap_redirect() should set guest privilege mode to supervisor mode because guest traps/interrupts are always handled in virtual supervisor mode.

Fixes 9f7013265112 ("RISC-V: KVM: Handle MMIO exits for VCPU")
---
 arch/riscv/kvm/vcpu_exit.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/kvm/vcpu_exit.c +3 -0
index c9f741ab26f5..af7c4bc07929 100644
@@ -160,6 +160,9 @@ void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu,
 
 	/* Set Guest PC to Guest exception vector */
 	vcpu->arch.guest_context.sepc = csr_read(CSR_VSTVEC);
+
+	/* Set Guest privilege mode to supervisor */
+	vcpu->arch.guest_context.sstatus |= SR_SPP;
 }
 
 /*
Signature · 1 line
2.34.1

Thanks,
drew

Reviewed-by on v3 1/2
#5 lore raw
In reply to Anup Patel
Quoted 26 lines · Anup Patel

The kvm_riscv_vcpu_trap_redirect() should set guest privilege mode to supervisor mode because guest traps/interrupts are always handled in virtual supervisor mode.

Fixes 9f7013265112 ("RISC-V: KVM: Handle MMIO exits for VCPU")
---
 arch/riscv/kvm/vcpu_exit.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/kvm/vcpu_exit.c +3 -1
index c9f741ab26f5..af7c4bc07929 100644
@@ -160,6 +160,9 @@ void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu,
 
        /* Set Guest PC to Guest exception vector */
        vcpu->arch.guest_context.sepc = csr_read(CSR_VSTVEC);
+
+       /* Set Guest privilege mode to supervisor */
+       vcpu->arch.guest_context.sstatus |= SR_SPP;
 }
 
 /*
--
Quoted 2 lines

2.34.1

Signature · 2 lines
Regards,
Atish
maintainer
#6 lore raw
In reply to Anup Patel
Quoted 7 lines · Anup Patel

The kvm_riscv_vcpu_trap_redirect() should set guest privilege mode to supervisor mode because guest traps/interrupts are always handled in virtual supervisor mode.

Fixes 9f7013265112 ("RISC-V: KVM: Handle MMIO exits for VCPU")

Queued this patch for Linux-6.3

Thanks,
Anup

Quoted 21 lines
---
 arch/riscv/kvm/vcpu_exit.c | 3 +++
 1 file changed, 3 insertions(+)
arch/riscv/kvm/vcpu_exit.c +3 -1
index c9f741ab26f5..af7c4bc07929 100644
@@ -160,6 +160,9 @@ void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu,
 
        /* Set Guest PC to Guest exception vector */
        vcpu->arch.guest_context.sepc = csr_read(CSR_VSTVEC);
+
+       /* Set Guest privilege mode to supervisor */
+       vcpu->arch.guest_context.sstatus |= SR_SPP;
 }
 
 /*
--

2.34.1