Skip to contentSkip to conversation

RISC-V: KVM: SBI system suspend support

PATCH In review Andrew Jones 2 participants 6 messages Oct 17, 2024 – Dec 5, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover RISC-V: KVM: SBI system suspend support

The SBI spec provides a specification for system suspend to RAM and Linux already supports invoking the SBI call for suspend to RAM when CONFIG_SUSPEND is selected. Implement support for the SBI call in KVM so guests may initiate a system suspend. However, since what a "system suspend" means for a VM may differ between VM providers, leave it up to the VMM to decide what to do. KVM only needs to ensure the entry criteria for system suspend (per the spec) is met and prepare things for the resume for when the VMM triggers it.

Tested with this kvmtool branch [1]

[1] https://github.com/jones-drew/kvmtool/commits/riscv/sbi-sys-susp/

Andrew Jones (2):
RISC-V: KVM: Add SBI system suspend support
KVM: riscv: selftests: Add SBI SUSP to get-reg-list test

 arch/riscv/include/asm/kvm_vcpu_sbi.h         |  1 +
 arch/riscv/include/uapi/asm/kvm.h             |  1 +
 arch/riscv/kvm/Makefile                       |  1 +
 arch/riscv/kvm/vcpu_sbi.c                     |  4 +
 arch/riscv/kvm/vcpu_sbi_system.c              | 73 +++++++++++++++++++
 .../selftests/kvm/riscv/get-reg-list.c        |  6 +-
 6 files changed, 85 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/kvm/vcpu_sbi_system.c
Signature · 1 line
2.46.2
#1 lore raw

PATCH 1/2 RISC-V: KVM: Add SBI system suspend support

Implement a KVM SBI SUSP extension handler. The handler only validates the system suspend entry criteria and prepares for resuming in the appropriate state at the resume_addr (as specified by the SBI spec), but then it forwards the call to the VMM where any system suspend behavior may be implemented. Since VMM support is needed, KVM disables the extension by default.

---
 arch/riscv/include/asm/kvm_vcpu_sbi.h |  1 +
 arch/riscv/include/uapi/asm/kvm.h     |  1 +
 arch/riscv/kvm/Makefile               |  1 +
 arch/riscv/kvm/vcpu_sbi.c             |  4 ++
 arch/riscv/kvm/vcpu_sbi_system.c      | 73 +++++++++++++++++++++++++++
 5 files changed, 80 insertions(+)
 create mode 100644 arch/riscv/kvm/vcpu_sbi_system.c
arch/riscv/include/asm/kvm_vcpu_sbi.h +1 -0
index b96705258cf9..4ed6203cdd30 100644
@@ -85,6 +85,7 @@ extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_rfence;
 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_srst;
 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_hsm;
 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_dbcn;
+extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_susp;
 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_sta;
 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_experimental;
 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_vendor;
arch/riscv/include/uapi/asm/kvm.h +1 -0
index e97db3296456..08e04ad8b1ab 100644
@@ -194,6 +194,7 @@ enum KVM_RISCV_SBI_EXT_ID {
 	KVM_RISCV_SBI_EXT_VENDOR,
 	KVM_RISCV_SBI_EXT_DBCN,
 	KVM_RISCV_SBI_EXT_STA,
+	KVM_RISCV_SBI_EXT_SUSP,
 	KVM_RISCV_SBI_EXT_MAX,
 };
 
arch/riscv/kvm/Makefile +1 -0
index c2cacfbc06a0..4bfc6ef0e9ad 100644
@@ -25,6 +25,7 @@ kvm-y += vcpu_sbi.o
 kvm-$(CONFIG_RISCV_SBI_V01) += vcpu_sbi_v01.o
 kvm-y += vcpu_sbi_base.o
 kvm-y += vcpu_sbi_replace.o
+kvm-y += vcpu_sbi_system.o
 kvm-y += vcpu_sbi_hsm.o
 kvm-y += vcpu_sbi_sta.o
 kvm-y += vcpu_timer.o
arch/riscv/kvm/vcpu_sbi.c +4 -0
index 7de128be8db9..7d8819640aff 100644
@@ -70,6 +70,10 @@ static const struct kvm_riscv_sbi_extension_entry sbi_ext[] = {
 		.ext_idx = KVM_RISCV_SBI_EXT_DBCN,
 		.ext_ptr = &vcpu_sbi_ext_dbcn,
 	},
+	{
+		.ext_idx = KVM_RISCV_SBI_EXT_SUSP,
+		.ext_ptr = &vcpu_sbi_ext_susp,
+	},
 	{
 		.ext_idx = KVM_RISCV_SBI_EXT_STA,
 		.ext_ptr = &vcpu_sbi_ext_sta,
arch/riscv/kvm/vcpu_sbi_system.c new file +73 -0

73 lines not on this page · raw message

Signature · 1 line
2.46.2
#3 lore raw

PATCH 2/2 KVM: riscv: selftests: Add SBI SUSP to get-reg-list test

In reply to Andrew Jones

KVM supports SBI SUSP, so add it to the get-reg-list test.

---
 tools/testing/selftests/kvm/riscv/get-reg-list.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/riscv/get-reg-list.c +5 -1
index 8e34f7fa44e9..7c33556ed59d 100644
@@ -108,6 +108,7 @@ bool filter_reg(__u64 reg)
 	case KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_HSM:
 	case KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_PMU:
 	case KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_DBCN:
+	case KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_SUSP:
 	case KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_STA:
 	case KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_EXPERIMENTAL:
 	case KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_VENDOR:
@@ -527,10 +528,11 @@ static const char *sbi_ext_single_id_to_str(__u64 reg_off)
 		KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_SRST),
 		KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_HSM),
 		KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_PMU),
+		KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_DBCN),
+		KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_SUSP),
 		KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_STA),
 		KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_EXPERIMENTAL),
 		KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_VENDOR),
-		KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_DBCN),
 	};
 
 	if (reg_off >= ARRAY_SIZE(kvm_sbi_ext_reg_name))
@@ -941,6 +943,7 @@ KVM_SBI_EXT_SUBLIST_CONFIG(base, BASE);
 KVM_SBI_EXT_SUBLIST_CONFIG(sta, STA);
 KVM_SBI_EXT_SIMPLE_CONFIG(pmu, PMU);
 KVM_SBI_EXT_SIMPLE_CONFIG(dbcn, DBCN);
+KVM_SBI_EXT_SIMPLE_CONFIG(susp, SUSP);
 
 KVM_ISA_EXT_SUBLIST_CONFIG(aia, AIA);
 KVM_ISA_EXT_SUBLIST_CONFIG(fp_f, FP_F);
@@ -1005,6 +1008,7 @@ struct vcpu_reg_list *vcpu_configs[] = {
 	&config_sbi_sta,
 	&config_sbi_pmu,
 	&config_sbi_dbcn,
+	&config_sbi_susp,
 	&config_aia,
 	&config_fp_f,
 	&config_fp_d,
Signature · 1 line
2.46.2
maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] RISC-V: KVM: Add SBI system suspend support

In reply to Andrew Jones
Quoted 9 lines · Andrew Jones

Implement a KVM SBI SUSP extension handler. The handler only validates the system suspend entry criteria and prepares for resuming in the appropriate state at the resume_addr (as specified by the SBI spec), but then it forwards the call to the VMM where any system suspend behavior may be implemented. Since VMM support is needed, KVM disables the extension by default.

LGTM.

Regards,
Anup

Quoted 142 lines
---
 arch/riscv/include/asm/kvm_vcpu_sbi.h |  1 +
 arch/riscv/include/uapi/asm/kvm.h     |  1 +
 arch/riscv/kvm/Makefile               |  1 +
 arch/riscv/kvm/vcpu_sbi.c             |  4 ++
 arch/riscv/kvm/vcpu_sbi_system.c      | 73 +++++++++++++++++++++++++++
 5 files changed, 80 insertions(+)
 create mode 100644 arch/riscv/kvm/vcpu_sbi_system.c
arch/riscv/include/asm/kvm_vcpu_sbi.h +1 -0
index b96705258cf9..4ed6203cdd30 100644
@@ -85,6 +85,7 @@ extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_rfence;
 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_srst;
 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_hsm;
 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_dbcn;
+extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_susp;
 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_sta;
 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_experimental;
 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_vendor;
arch/riscv/include/uapi/asm/kvm.h +1 -0
index e97db3296456..08e04ad8b1ab 100644
@@ -194,6 +194,7 @@ enum KVM_RISCV_SBI_EXT_ID {
        KVM_RISCV_SBI_EXT_VENDOR,
        KVM_RISCV_SBI_EXT_DBCN,
        KVM_RISCV_SBI_EXT_STA,
+       KVM_RISCV_SBI_EXT_SUSP,
        KVM_RISCV_SBI_EXT_MAX,
 };
 
arch/riscv/kvm/Makefile +1 -0
index c2cacfbc06a0..4bfc6ef0e9ad 100644
@@ -25,6 +25,7 @@ kvm-y += vcpu_sbi.o
 kvm-$(CONFIG_RISCV_SBI_V01) += vcpu_sbi_v01.o
 kvm-y += vcpu_sbi_base.o
 kvm-y += vcpu_sbi_replace.o
+kvm-y += vcpu_sbi_system.o
 kvm-y += vcpu_sbi_hsm.o
 kvm-y += vcpu_sbi_sta.o
 kvm-y += vcpu_timer.o
arch/riscv/kvm/vcpu_sbi.c +4 -0
index 7de128be8db9..7d8819640aff 100644
@@ -70,6 +70,10 @@ static const struct kvm_riscv_sbi_extension_entry sbi_ext[] = {
                .ext_idx = KVM_RISCV_SBI_EXT_DBCN,
                .ext_ptr = &vcpu_sbi_ext_dbcn,
        },
+       {
+               .ext_idx = KVM_RISCV_SBI_EXT_SUSP,
+               .ext_ptr = &vcpu_sbi_ext_susp,
+       },
        {
                .ext_idx = KVM_RISCV_SBI_EXT_STA,
                .ext_ptr = &vcpu_sbi_ext_sta,
arch/riscv/kvm/vcpu_sbi_system.c new file +73 -1
new file mode 100644
index 000000000000..5d55e08791fa
@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2024 Ventana Micro Systems Inc.
+ */
+
+#include <linux/kvm_host.h>
+
+#include <asm/kvm_vcpu_sbi.h>
+#include <asm/sbi.h>
+
+static int kvm_sbi_ext_susp_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,
+                                   struct kvm_vcpu_sbi_return *retdata)
+{
+       struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
+       struct kvm_cpu_context *reset_cntx;
+       unsigned long funcid = cp->a6;
+       unsigned long hva, i;
+       struct kvm_vcpu *tmp;
+
+       switch (funcid) {
+       case SBI_EXT_SUSP_SYSTEM_SUSPEND:
+               if (cp->a0 != SBI_SUSP_SLEEP_TYPE_SUSPEND_TO_RAM) {
+                       retdata->err_val = SBI_ERR_INVALID_PARAM;
+                       return 0;
+               }
+
+               if (!(cp->sstatus & SR_SPP)) {
+                       retdata->err_val = SBI_ERR_FAILURE;
+                       return 0;
+               }
+
+               hva = kvm_vcpu_gfn_to_hva_prot(vcpu, cp->a1 >> PAGE_SHIFT, NULL);
+               if (kvm_is_error_hva(hva)) {
+                       retdata->err_val = SBI_ERR_INVALID_ADDRESS;
+                       return 0;
+               }
+
+               kvm_for_each_vcpu(i, tmp, vcpu->kvm) {
+                       if (tmp == vcpu)
+                               continue;
+                       if (!kvm_riscv_vcpu_stopped(tmp)) {
+                               retdata->err_val = SBI_ERR_DENIED;
+                               return 0;
+                       }
+               }
+
+               spin_lock(&vcpu->arch.reset_cntx_lock);
+               reset_cntx = &vcpu->arch.guest_reset_context;
+               reset_cntx->sepc = cp->a1;
+               reset_cntx->a0 = vcpu->vcpu_id;
+               reset_cntx->a1 = cp->a2;
+               spin_unlock(&vcpu->arch.reset_cntx_lock);
+
+               kvm_make_request(KVM_REQ_VCPU_RESET, vcpu);
+
+               /* userspace provides the suspend implementation */
+               kvm_riscv_vcpu_sbi_forward(vcpu, run);
+               retdata->uexit = true;
+               break;
+       default:
+               retdata->err_val = SBI_ERR_NOT_SUPPORTED;
+               break;
+       }
+
+       return 0;
+}
+
+const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_susp = {
+       .extid_start = SBI_EXT_SUSP,
+       .extid_end = SBI_EXT_SUSP,
+       .default_disabled = true,
+       .handler = kvm_sbi_ext_susp_handler,
+};
--

2.46.2

Signature · 3 lines
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
maintainer Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] KVM: riscv: selftests: Add SBI SUSP to get-reg-list test

In reply to Andrew Jones
Quoted 4 lines · Andrew Jones

KVM supports SBI SUSP, so add it to the get-reg-list test.

LGTM.

Regards,
Anup

Quoted 48 lines
---
 tools/testing/selftests/kvm/riscv/get-reg-list.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/riscv/get-reg-list.c +5 -2
index 8e34f7fa44e9..7c33556ed59d 100644
@@ -108,6 +108,7 @@ bool filter_reg(__u64 reg)
        case KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_HSM:
        case KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_PMU:
        case KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_DBCN:
+       case KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_SUSP:
        case KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_STA:
        case KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_EXPERIMENTAL:
        case KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_VENDOR:
@@ -527,10 +528,11 @@ static const char *sbi_ext_single_id_to_str(__u64 reg_off)
                KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_SRST),
                KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_HSM),
                KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_PMU),
+               KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_DBCN),
+               KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_SUSP),
                KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_STA),
                KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_EXPERIMENTAL),
                KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_VENDOR),
-               KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_DBCN),
        };
 
        if (reg_off >= ARRAY_SIZE(kvm_sbi_ext_reg_name))
@@ -941,6 +943,7 @@ KVM_SBI_EXT_SUBLIST_CONFIG(base, BASE);
 KVM_SBI_EXT_SUBLIST_CONFIG(sta, STA);
 KVM_SBI_EXT_SIMPLE_CONFIG(pmu, PMU);
 KVM_SBI_EXT_SIMPLE_CONFIG(dbcn, DBCN);
+KVM_SBI_EXT_SIMPLE_CONFIG(susp, SUSP);
 
 KVM_ISA_EXT_SUBLIST_CONFIG(aia, AIA);
 KVM_ISA_EXT_SUBLIST_CONFIG(fp_f, FP_F);
@@ -1005,6 +1008,7 @@ struct vcpu_reg_list *vcpu_configs[] = {
        &config_sbi_sta,
        &config_sbi_pmu,
        &config_sbi_dbcn,
+       &config_sbi_susp,
        &config_aia,
        &config_fp_f,
        &config_fp_d,
--

2.46.2

Signature · 3 lines
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
maintainer
#5 lore raw
In reply to Andrew Jones
Quoted 10 lines · Andrew Jones

The SBI spec provides a specification for system suspend to RAM and Linux already supports invoking the SBI call for suspend to RAM when CONFIG_SUSPEND is selected. Implement support for the SBI call in KVM so guests may initiate a system suspend. However, since what a "system suspend" means for a VM may differ between VM providers, leave it up to the VMM to decide what to do. KVM only needs to ensure the entry criteria for system suspend (per the spec) is met and prepare things for the resume for when the VMM triggers it.

Quoted 8 lines

Tested with this kvmtool branch [1]

[1] https://github.com/jones-drew/kvmtool/commits/riscv/sbi-sys-susp/

Andrew Jones (2):
RISC-V: KVM: Add SBI system suspend support
KVM: riscv: selftests: Add SBI SUSP to get-reg-list test

Queued this series for 6.14

Thanks,
Anup

Quoted 13 lines
 arch/riscv/include/asm/kvm_vcpu_sbi.h         |  1 +
 arch/riscv/include/uapi/asm/kvm.h             |  1 +
 arch/riscv/kvm/Makefile                       |  1 +
 arch/riscv/kvm/vcpu_sbi.c                     |  4 +
 arch/riscv/kvm/vcpu_sbi_system.c              | 73 +++++++++++++++++++
 .../selftests/kvm/riscv/get-reg-list.c        |  6 +-
 6 files changed, 85 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/kvm/vcpu_sbi_system.c

--
2.46.2

Signature · 3 lines
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv