Skip to content

Commit

Permalink
riscv: Handle SBI DBCN calls from Guest/VM
Browse files Browse the repository at this point in the history
The new SBI DBCN functions are forwarded by in-kernel KVM RISC-V module
to user-space so let us handle these calls in kvm_cpu_riscv_sbi() function.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
  • Loading branch information
avpatel committed Oct 10, 2023
1 parent 3df2315 commit 06a373e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
14 changes: 12 additions & 2 deletions riscv/include/kvm/sbi.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ enum sbi_ext_id {
SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID = 0x7,
SBI_EXT_0_1_SHUTDOWN = 0x8,
SBI_EXT_BASE = 0x10,
SBI_EXT_DBCN = 0x4442434E,
};

enum sbi_ext_base_fid {
Expand All @@ -32,6 +33,12 @@ enum sbi_ext_base_fid {
SBI_BASE_GET_MIMPID,
};

enum sbi_ext_dbcn_fid {
SBI_EXT_DBCN_CONSOLE_WRITE = 0,
SBI_EXT_DBCN_CONSOLE_READ = 1,
SBI_EXT_DBCN_CONSOLE_WRITE_BYTE = 2,
};

#define SBI_SPEC_VERSION_DEFAULT 0x1
#define SBI_SPEC_VERSION_MAJOR_OFFSET 24
#define SBI_SPEC_VERSION_MAJOR_MASK 0x7f
Expand All @@ -41,8 +48,11 @@ enum sbi_ext_base_fid {
#define SBI_SUCCESS 0
#define SBI_ERR_FAILURE -1
#define SBI_ERR_NOT_SUPPORTED -2
#define SBI_ERR_INVALID_PARAM -3
#define SBI_ERR_INVALID_PARAM -3
#define SBI_ERR_DENIED -4
#define SBI_ERR_INVALID_ADDRESS -5
#define SBI_ERR_INVALID_ADDRESS -5
#define SBI_ERR_ALREADY_AVAILABLE -6
#define SBI_ERR_ALREADY_STARTED -7
#define SBI_ERR_ALREADY_STOPPED -8

#endif
46 changes: 46 additions & 0 deletions riscv/kvm-cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ void kvm_cpu__delete(struct kvm_cpu *vcpu)
static bool kvm_cpu_riscv_sbi(struct kvm_cpu *vcpu)
{
char ch;
u64 addr;
bool ret = true;
char *str_start, *str_end;
int dfd = kvm_cpu__get_debug_fd();

switch (vcpu->kvm_run->riscv_sbi.extension_id) {
Expand All @@ -144,6 +146,50 @@ static bool kvm_cpu_riscv_sbi(struct kvm_cpu *vcpu)
else
vcpu->kvm_run->riscv_sbi.ret[0] = SBI_ERR_FAILURE;
break;
case SBI_EXT_DBCN:
switch (vcpu->kvm_run->riscv_sbi.function_id) {
case SBI_EXT_DBCN_CONSOLE_WRITE:
case SBI_EXT_DBCN_CONSOLE_READ:
addr = vcpu->kvm_run->riscv_sbi.args[1];
#if __riscv_xlen == 32
addr |= (u64)vcpu->kvm_run->riscv_sbi.args[2] << 32;
#endif
if (!vcpu->kvm_run->riscv_sbi.args[0])
break;
str_start = guest_flat_to_host(vcpu->kvm, addr);
addr += vcpu->kvm_run->riscv_sbi.args[0] - 1;
str_end = guest_flat_to_host(vcpu->kvm, addr);
if (!str_start || !str_end) {
vcpu->kvm_run->riscv_sbi.ret[0] =
SBI_ERR_INVALID_PARAM;
break;
}
vcpu->kvm_run->riscv_sbi.ret[1] = 0;
while (str_start <= str_end) {
if (vcpu->kvm_run->riscv_sbi.function_id ==
SBI_EXT_DBCN_CONSOLE_WRITE) {
term_putc(str_start, 1, 0);
} else {
if (!term_readable(0))
break;
*str_start = term_getc(vcpu->kvm, 0);
}
vcpu->kvm_run->riscv_sbi.ret[1]++;
str_start++;
}
break;
case SBI_EXT_DBCN_CONSOLE_WRITE_BYTE:
ch = vcpu->kvm_run->riscv_sbi.args[0];
term_putc(&ch, 1, 0);
vcpu->kvm_run->riscv_sbi.ret[0] = 0;
vcpu->kvm_run->riscv_sbi.ret[1] = 0;
break;
default:
vcpu->kvm_run->riscv_sbi.ret[0] =
SBI_ERR_NOT_SUPPORTED;
break;
}
break;
default:
dprintf(dfd, "Unhandled SBI call\n");
dprintf(dfd, "extension_id=0x%lx function_id=0x%lx\n",
Expand Down

0 comments on commit 06a373e

Please sign in to comment.