aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnup Patel <anup.patel@wdc.com>2021-11-19 18:15:14 +0530
committerWill Deacon <will@kernel.org>2021-12-14 15:05:30 +0000
commit721da166a698ff80789982256bbeac37f98b9e98 (patch)
tree80d036c7ea5a056d80cdb33768fc606ca0494f81
parent7c9aac003925e52121b34c48cf81564c4077b26c (diff)
downloadkvmtool-721da166a698ff80789982256bbeac37f98b9e98.tar.gz
riscv: Handle SBI calls forwarded to user space
The kernel KVM RISC-V module will forward certain SBI calls to user space. These forwared SBI calls will usually be the SBI calls which cannot be emulated in kernel space such as PUTCHAR and GETCHAR calls. This patch extends kvm_cpu__handle_exit() to handle SBI calls forwarded to user space. Signed-off-by: Atish Patra <atish.patra@wdc.com> Signed-off-by: Anup Patel <anup.patel@wdc.com> Link: https://lore.kernel.org/r/20211119124515.89439-8-anup.patel@wdc.com Signed-off-by: Will Deacon <will@kernel.org>
-rw-r--r--riscv/include/kvm/sbi.h48
-rw-r--r--riscv/kvm-cpu.c49
2 files changed, 96 insertions, 1 deletions
diff --git a/riscv/include/kvm/sbi.h b/riscv/include/kvm/sbi.h
new file mode 100644
index 00000000..f4b41827
--- /dev/null
+++ b/riscv/include/kvm/sbi.h
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Common SBI related defines and macros to be used by RISC-V kernel,
+ * RISC-V KVM and userspace.
+ *
+ * Copyright (c) 2019 Western Digital Corporation or its affiliates.
+ */
+
+#ifndef __RISCV_SBI_H__
+#define __RISCV_SBI_H__
+
+enum sbi_ext_id {
+ SBI_EXT_0_1_SET_TIMER = 0x0,
+ SBI_EXT_0_1_CONSOLE_PUTCHAR = 0x1,
+ SBI_EXT_0_1_CONSOLE_GETCHAR = 0x2,
+ SBI_EXT_0_1_CLEAR_IPI = 0x3,
+ SBI_EXT_0_1_SEND_IPI = 0x4,
+ SBI_EXT_0_1_REMOTE_FENCE_I = 0x5,
+ SBI_EXT_0_1_REMOTE_SFENCE_VMA = 0x6,
+ SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID = 0x7,
+ SBI_EXT_0_1_SHUTDOWN = 0x8,
+ SBI_EXT_BASE = 0x10,
+};
+
+enum sbi_ext_base_fid {
+ SBI_BASE_GET_SPEC_VERSION = 0,
+ SBI_BASE_GET_IMP_ID,
+ SBI_BASE_GET_IMP_VERSION,
+ SBI_BASE_PROBE_EXT,
+ SBI_BASE_GET_MVENDORID,
+ SBI_BASE_GET_MARCHID,
+ SBI_BASE_GET_MIMPID,
+};
+
+#define SBI_SPEC_VERSION_DEFAULT 0x1
+#define SBI_SPEC_VERSION_MAJOR_OFFSET 24
+#define SBI_SPEC_VERSION_MAJOR_MASK 0x7f
+#define SBI_SPEC_VERSION_MINOR_MASK 0xffffff
+
+/* SBI return error codes */
+#define SBI_SUCCESS 0
+#define SBI_ERR_FAILURE -1
+#define SBI_ERR_NOT_SUPPORTED -2
+#define SBI_ERR_INVALID_PARAM -3
+#define SBI_ERR_DENIED -4
+#define SBI_ERR_INVALID_ADDRESS -5
+
+#endif
diff --git a/riscv/kvm-cpu.c b/riscv/kvm-cpu.c
index 8adaddd8..df90c7b9 100644
--- a/riscv/kvm-cpu.c
+++ b/riscv/kvm-cpu.c
@@ -1,6 +1,7 @@
#include "kvm/kvm-cpu.h"
#include "kvm/kvm.h"
#include "kvm/virtio.h"
+#include "kvm/sbi.h"
#include "kvm/term.h"
#include <asm/ptrace.h>
@@ -110,9 +111,55 @@ void kvm_cpu__delete(struct kvm_cpu *vcpu)
free(vcpu);
}
+static bool kvm_cpu_riscv_sbi(struct kvm_cpu *vcpu)
+{
+ char ch;
+ bool ret = true;
+ int dfd = kvm_cpu__get_debug_fd();
+
+ switch (vcpu->kvm_run->riscv_sbi.extension_id) {
+ case SBI_EXT_0_1_CONSOLE_PUTCHAR:
+ ch = vcpu->kvm_run->riscv_sbi.args[0];
+ term_putc(&ch, 1, 0);
+ vcpu->kvm_run->riscv_sbi.ret[0] = 0;
+ break;
+ case SBI_EXT_0_1_CONSOLE_GETCHAR:
+ if (term_readable(0))
+ vcpu->kvm_run->riscv_sbi.ret[0] =
+ term_getc(vcpu->kvm, 0);
+ else
+ vcpu->kvm_run->riscv_sbi.ret[0] = SBI_ERR_FAILURE;
+ break;
+ default:
+ dprintf(dfd, "Unhandled SBI call\n");
+ dprintf(dfd, "extension_id=0x%lx function_id=0x%lx\n",
+ vcpu->kvm_run->riscv_sbi.extension_id,
+ vcpu->kvm_run->riscv_sbi.function_id);
+ dprintf(dfd, "args[0]=0x%lx args[1]=0x%lx\n",
+ vcpu->kvm_run->riscv_sbi.args[0],
+ vcpu->kvm_run->riscv_sbi.args[1]);
+ dprintf(dfd, "args[2]=0x%lx args[3]=0x%lx\n",
+ vcpu->kvm_run->riscv_sbi.args[2],
+ vcpu->kvm_run->riscv_sbi.args[3]);
+ dprintf(dfd, "args[4]=0x%lx args[5]=0x%lx\n",
+ vcpu->kvm_run->riscv_sbi.args[4],
+ vcpu->kvm_run->riscv_sbi.args[5]);
+ ret = false;
+ break;
+ };
+
+ return ret;
+}
+
bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu)
{
- /* TODO: */
+ switch (vcpu->kvm_run->exit_reason) {
+ case KVM_EXIT_RISCV_SBI:
+ return kvm_cpu_riscv_sbi(vcpu);
+ default:
+ break;
+ };
+
return false;
}