Skip to content

Commit c667ad2

Browse files
committed
RISC-V: KVM: Forward SBI DBCN extension to user-space
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 <[email protected]> Reviewed-by: Andrew Jones <[email protected]> Signed-off-by: Anup Patel <[email protected]>
1 parent 56d8a38 commit c667ad2

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

arch/riscv/include/asm/kvm_vcpu_sbi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_ipi;
7373
extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_rfence;
7474
extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_srst;
7575
extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_hsm;
76+
extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_dbcn;
7677
extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_experimental;
7778
extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_vendor;
7879

arch/riscv/include/uapi/asm/kvm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ enum KVM_RISCV_SBI_EXT_ID {
156156
KVM_RISCV_SBI_EXT_PMU,
157157
KVM_RISCV_SBI_EXT_EXPERIMENTAL,
158158
KVM_RISCV_SBI_EXT_VENDOR,
159+
KVM_RISCV_SBI_EXT_DBCN,
159160
KVM_RISCV_SBI_EXT_MAX,
160161
};
161162

arch/riscv/kvm/vcpu_sbi.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ static const struct kvm_riscv_sbi_extension_entry sbi_ext[] = {
6666
.ext_idx = KVM_RISCV_SBI_EXT_PMU,
6767
.ext_ptr = &vcpu_sbi_ext_pmu,
6868
},
69+
{
70+
.ext_idx = KVM_RISCV_SBI_EXT_DBCN,
71+
.ext_ptr = &vcpu_sbi_ext_dbcn,
72+
},
6973
{
7074
.ext_idx = KVM_RISCV_SBI_EXT_EXPERIMENTAL,
7175
.ext_ptr = &vcpu_sbi_ext_experimental,

arch/riscv/kvm/vcpu_sbi_replace.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,35 @@ const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_srst = {
175175
.extid_end = SBI_EXT_SRST,
176176
.handler = kvm_sbi_ext_srst_handler,
177177
};
178+
179+
static int kvm_sbi_ext_dbcn_handler(struct kvm_vcpu *vcpu,
180+
struct kvm_run *run,
181+
struct kvm_vcpu_sbi_return *retdata)
182+
{
183+
struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
184+
unsigned long funcid = cp->a6;
185+
186+
switch (funcid) {
187+
case SBI_EXT_DBCN_CONSOLE_WRITE:
188+
case SBI_EXT_DBCN_CONSOLE_READ:
189+
case SBI_EXT_DBCN_CONSOLE_WRITE_BYTE:
190+
/*
191+
* The SBI debug console functions are unconditionally
192+
* forwarded to the userspace.
193+
*/
194+
kvm_riscv_vcpu_sbi_forward(vcpu, run);
195+
retdata->uexit = true;
196+
break;
197+
default:
198+
retdata->err_val = SBI_ERR_NOT_SUPPORTED;
199+
}
200+
201+
return 0;
202+
}
203+
204+
const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_dbcn = {
205+
.extid_start = SBI_EXT_DBCN,
206+
.extid_end = SBI_EXT_DBCN,
207+
.default_unavail = true,
208+
.handler = kvm_sbi_ext_dbcn_handler,
209+
};

0 commit comments

Comments
 (0)