RISC-V: KVM: Forward SBI DBCN extension to user-space
authorAnup Patel <apatel@ventanamicro.com>
Fri, 22 Jul 2022 14:16:24 +0000 (19:46 +0530)
committerAnup Patel <anup@brainfault.org>
Fri, 20 Oct 2023 11:20:36 +0000 (16:50 +0530)
The frozen SBI v2.0 specification defines the SBI debug console
(DBCN) extension which replaces the legacy SBI v0.1 console
functions namely sbi_console_getchar() and sbi_console_putchar().

The SBI DBCN extension needs to be emulated in the KVM user-space
(i.e. QEMU-KVM or KVMTOOL) so we forward SBI DBCN calls from KVM
guest to the KVM user-space which can then redirect the console
input/output to wherever it wants (e.g. telnet, file, stdio, etc).

The SBI debug console is simply a early console available to KVM
guest for early prints and it does not intend to replace the proper
console devices such as 8250, VirtIO console, etc.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Signed-off-by: Anup Patel <anup@brainfault.org>
arch/riscv/include/asm/kvm_vcpu_sbi.h
arch/riscv/include/uapi/asm/kvm.h
arch/riscv/kvm/vcpu_sbi.c
arch/riscv/kvm/vcpu_sbi_replace.c

index c02bda5559d7198b697cbc4340d8854e00a53e7d..6a453f7f8b562639dc1d767648364f68fa6e49ae 100644 (file)
@@ -73,6 +73,7 @@ extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_ipi;
 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_experimental;
 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_vendor;
 
index 917d8cc2489e500989b51022ae0d5f2b5a6ec93e..60d3b21dead7d8846050d20a96ef1a0b3ad1ba20 100644 (file)
@@ -156,6 +156,7 @@ enum KVM_RISCV_SBI_EXT_ID {
        KVM_RISCV_SBI_EXT_PMU,
        KVM_RISCV_SBI_EXT_EXPERIMENTAL,
        KVM_RISCV_SBI_EXT_VENDOR,
+       KVM_RISCV_SBI_EXT_DBCN,
        KVM_RISCV_SBI_EXT_MAX,
 };
 
index bda8b0b3334327c244ecb74fdd062802dbdd903c..a04ff98085d93936a70a2ff1aec7ad3e305f7aaa 100644 (file)
@@ -66,6 +66,10 @@ static const struct kvm_riscv_sbi_extension_entry sbi_ext[] = {
                .ext_idx = KVM_RISCV_SBI_EXT_PMU,
                .ext_ptr = &vcpu_sbi_ext_pmu,
        },
+       {
+               .ext_idx = KVM_RISCV_SBI_EXT_DBCN,
+               .ext_ptr = &vcpu_sbi_ext_dbcn,
+       },
        {
                .ext_idx = KVM_RISCV_SBI_EXT_EXPERIMENTAL,
                .ext_ptr = &vcpu_sbi_ext_experimental,
index 7c4d5d38a33908891b7c7371dee0d121ea05f5fb..23b57c931b1522777ff4631abb8f24193a0bcd3f 100644 (file)
@@ -175,3 +175,35 @@ const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_srst = {
        .extid_end = SBI_EXT_SRST,
        .handler = kvm_sbi_ext_srst_handler,
 };
+
+static int kvm_sbi_ext_dbcn_handler(struct kvm_vcpu *vcpu,
+                                   struct kvm_run *run,
+                                   struct kvm_vcpu_sbi_return *retdata)
+{
+       struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
+       unsigned long funcid = cp->a6;
+
+       switch (funcid) {
+       case SBI_EXT_DBCN_CONSOLE_WRITE:
+       case SBI_EXT_DBCN_CONSOLE_READ:
+       case SBI_EXT_DBCN_CONSOLE_WRITE_BYTE:
+               /*
+                * The SBI debug console functions are unconditionally
+                * forwarded to the userspace.
+                */
+               kvm_riscv_vcpu_sbi_forward(vcpu, run);
+               retdata->uexit = true;
+               break;
+       default:
+               retdata->err_val = SBI_ERR_NOT_SUPPORTED;
+       }
+
+       return 0;
+}
+
+const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_dbcn = {
+       .extid_start = SBI_EXT_DBCN,
+       .extid_end = SBI_EXT_DBCN,
+       .default_unavail = true,
+       .handler = kvm_sbi_ext_dbcn_handler,
+};