Skip to contentSkip to conversation

RISC-V SBI debug console extension support

PATCH Superseded Anup Patel 3 participants 20 messages Oct 12, 2023 – Oct 20, 2023

Subsystems (6)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Anup Patel

author
#0 lore raw

v2 cover RISC-V SBI debug console extension support

The SBI v2.0 specification is now frozen. The SBI v2.0 specification defines SBI debug console (DBCN) extension which replaces the legacy SBI v0.1 functions sbi_console_putchar() and sbi_console_getchar(). (Refer v2.0-rc5 at https://github.com/riscv-non-isa/riscv-sbi-doc/releases)

This series adds support for SBI debug console (DBCN) extension in KVM RISC-V and Linux RISC-V.

To try these patches with KVM RISC-V, use KVMTOOL from riscv_sbi_dbcn_v1 branch at: https://github.com/avpatel/kvmtool.git

These patches can also be found in the riscv_sbi_dbcn_v2 branch at:
https://github.com/avpatel/linux.git

Changes since v1:
- Remove use of #ifdef from PATCH4 and PATCH5 of the v1 series
- Improved commit description of PATCH3 in v1 series
- Introduced new PATCH3 in this series to allow some SBI extensions (such as SBI DBCN) do to disabled by default so that older KVM user space work fine and newer KVM user space have to explicitly opt-in for emulating SBI DBCN.
- Introduced new PATCH5 in this series which adds inline version of sbi_console_getchar() and sbi_console_putchar() for the case where CONFIG_RISCV_SBI_V01 is disabled.

Anup Patel (7):
RISC-V: Add defines for SBI debug console extension
RISC-V: KVM: Change the SBI specification version to v2.0
RISC-V: KVM: Allow some SBI extensions to be disabled by default
RISC-V: KVM: Forward SBI DBCN extension to user-space
RISC-V: Add inline version of sbi_console_putchar/getchar() functions tty/serial: Add RISC-V SBI debug console based earlycon
RISC-V: Enable SBI based earlycon support

Atish Patra (1):
tty: Add SBI debug console support to HVC SBI driver

 arch/riscv/configs/defconfig            |  1 +
 arch/riscv/configs/rv32_defconfig       |  1 +
 arch/riscv/include/asm/kvm_vcpu_sbi.h   |  7 ++-
 arch/riscv/include/asm/sbi.h            | 12 ++++
 arch/riscv/include/uapi/asm/kvm.h       |  1 +
 arch/riscv/kvm/vcpu.c                   |  6 ++
 arch/riscv/kvm/vcpu_sbi.c               | 49 +++++++++-------
 arch/riscv/kvm/vcpu_sbi_replace.c       | 32 +++++++++++
 drivers/tty/hvc/Kconfig                 |  2 +-
 drivers/tty/hvc/hvc_riscv_sbi.c         | 76 ++++++++++++++++++++++---
 drivers/tty/serial/Kconfig              |  2 +-
 drivers/tty/serial/earlycon-riscv-sbi.c | 32 +++++++++--
 12 files changed, 188 insertions(+), 33 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/8 RISC-V: Add defines for SBI debug console extension

We add SBI debug console extension related defines/enum to the asm/sbi.h header.

---
 arch/riscv/include/asm/sbi.h | 7 +++++++
 1 file changed, 7 insertions(+)
arch/riscv/include/asm/sbi.h +7 -0
index 5b4a1bf5f439..12dfda6bb924 100644
@@ -30,6 +30,7 @@ enum sbi_ext_id {
 	SBI_EXT_HSM = 0x48534D,
 	SBI_EXT_SRST = 0x53525354,
 	SBI_EXT_PMU = 0x504D55,
+	SBI_EXT_DBCN = 0x4442434E,
 
 	/* Experimentals extensions must lie within this range */
 	SBI_EXT_EXPERIMENTAL_START = 0x08000000,
@@ -236,6 +237,12 @@ enum sbi_pmu_ctr_type {
 /* Flags defined for counter stop function */
 #define SBI_PMU_STOP_FLAG_RESET (1 << 0)
 
+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_SHIFT	24
 #define SBI_SPEC_VERSION_MAJOR_MASK	0x7f
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v2 2/8 RISC-V: KVM: Change the SBI specification version to v2.0

In reply to Anup Patel

We will be implementing SBI DBCN extension for KVM RISC-V so let us change the KVM RISC-V SBI specification version to v2.0.

---
 arch/riscv/include/asm/kvm_vcpu_sbi.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/include/asm/kvm_vcpu_sbi.h +1 -1
index cdcf0ff07be7..8d6d4dce8a5e 100644
@@ -11,7 +11,7 @@
 
 #define KVM_SBI_IMPID 3
 
-#define KVM_SBI_VERSION_MAJOR 1
+#define KVM_SBI_VERSION_MAJOR 2
 #define KVM_SBI_VERSION_MINOR 0
 
 enum kvm_riscv_sbi_ext_status {
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH v2 3/8 RISC-V: KVM: Allow some SBI extensions to be disabled by default

In reply to Anup Patel

Currently, all SBI extensions are enabled by default which is problematic for SBI extensions (such as DBCN) which are forwarded to the KVM user-space because we might have an older KVM user-space which is not aware/ready to handle newer SBI extensions. Ideally, the SBI extensions forwarded to the KVM user-space must be disabled by default.

To address above, we allow certain SBI extensions to be disabled by default so that KVM user-space must explicitly enable such SBI extensions to receive forwarded calls from Guest VCPU.

---
 arch/riscv/include/asm/kvm_vcpu_sbi.h |  4 +++
 arch/riscv/kvm/vcpu.c                 |  6 ++++
 arch/riscv/kvm/vcpu_sbi.c             | 45 ++++++++++++++++-----------
 3 files changed, 36 insertions(+), 19 deletions(-)
arch/riscv/include/asm/kvm_vcpu_sbi.h +4 -0
index 8d6d4dce8a5e..c02bda5559d7 100644
@@ -35,6 +35,9 @@ struct kvm_vcpu_sbi_return {
 struct kvm_vcpu_sbi_extension {
 	unsigned long extid_start;
 	unsigned long extid_end;
+
+	bool default_unavail;
+
 	/**
 	 * SBI extension handler. It can be defined for a given extension or group of
 	 * extension. But it should always return linux error codes rather than SBI
@@ -59,6 +62,7 @@ int kvm_riscv_vcpu_get_reg_sbi_ext(struct kvm_vcpu *vcpu,
 const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext(
 				struct kvm_vcpu *vcpu, unsigned long extid);
 int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run);
+void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu);
 
 #ifdef CONFIG_RISCV_SBI_V01
 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_v01;
arch/riscv/kvm/vcpu.c +6 -0
index c061a1c5fe98..e087c809073c 100644
@@ -141,6 +141,12 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 	if (rc)
 		return rc;
 
+	/*
+	 * Setup SBI extensions
+	 * NOTE: This must be the last thing to be initialized.
+	 */
+	kvm_riscv_vcpu_sbi_init(vcpu);
+
 	/* Reset VCPU */
 	kvm_riscv_reset_vcpu(vcpu);
 
arch/riscv/kvm/vcpu_sbi.c +26 -19

61 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#8 lore raw

PATCH v2 4/8 RISC-V: KVM: Forward SBI DBCN extension to user-space

In reply to Anup Patel

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.

---
 arch/riscv/include/asm/kvm_vcpu_sbi.h |  1 +
 arch/riscv/include/uapi/asm/kvm.h     |  1 +
 arch/riscv/kvm/vcpu_sbi.c             |  4 ++++
 arch/riscv/kvm/vcpu_sbi_replace.c     | 32 +++++++++++++++++++++++++++
 4 files changed, 38 insertions(+)
arch/riscv/include/asm/kvm_vcpu_sbi.h +1 -0
index c02bda5559d7..6a453f7f8b56 100644
@@ -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;
 
arch/riscv/include/uapi/asm/kvm.h +1 -0
index 917d8cc2489e..60d3b21dead7 100644
@@ -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,
 };
 
arch/riscv/kvm/vcpu_sbi.c +4 -0
index 1b1cee86efda..bb76c3cf633f 100644
@@ -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,
arch/riscv/kvm/vcpu_sbi_replace.c +32 -0
index 7c4d5d38a339..23b57c931b15 100644
@@ -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,
+};
Signature · 1 line
2.34.1
author
#11 lore raw

PATCH v2 5/8 RISC-V: Add inline version of sbi_console_putchar/getchar() functions

In reply to Anup Patel

The functions sbi_console_putchar() and sbi_console_getchar() are not defined when CONFIG_RISCV_SBI_V01 is disabled so let us add inline version of these functions to avoid "#ifdef" on user side.

---
 arch/riscv/include/asm/sbi.h | 5 +++++
 1 file changed, 5 insertions(+)
arch/riscv/include/asm/sbi.h +5 -0
index 12dfda6bb924..cbcefa344417 100644
@@ -271,8 +271,13 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
 			unsigned long arg3, unsigned long arg4,
 			unsigned long arg5);
 
+#ifdef CONFIG_RISCV_SBI_V01
 void sbi_console_putchar(int ch);
 int sbi_console_getchar(void);
+#else
+static inline void sbi_console_putchar(int ch) { }
+static inline int sbi_console_getchar(void) { return -1; }
+#endif
 long sbi_get_mvendorid(void);
 long sbi_get_marchid(void);
 long sbi_get_mimpid(void);
Signature · 1 line
2.34.1
author
#13 lore raw

PATCH v2 6/8 tty/serial: Add RISC-V SBI debug console based earlycon

In reply to Anup Patel

We extend the existing RISC-V SBI earlycon support to use the new RISC-V SBI debug console extension.

---
 drivers/tty/serial/Kconfig              |  2 +-
 drivers/tty/serial/earlycon-riscv-sbi.c | 32 +++++++++++++++++++++----
 2 files changed, 29 insertions(+), 5 deletions(-)
drivers/tty/serial/Kconfig +1 -1
index bdc568a4ab66..cec46091a716 100644
@@ -87,7 +87,7 @@ config SERIAL_EARLYCON_SEMIHOST
 
 config SERIAL_EARLYCON_RISCV_SBI
 	bool "Early console using RISC-V SBI"
-	depends on RISCV_SBI_V01
+	depends on RISCV_SBI
 	select SERIAL_CORE
 	select SERIAL_CORE_CONSOLE
 	select SERIAL_EARLYCON
drivers/tty/serial/earlycon-riscv-sbi.c +28 -4
index 27afb0b74ea7..c21cdef254e7 100644
@@ -15,17 +15,41 @@ static void sbi_putc(struct uart_port *port, unsigned char c)
 	sbi_console_putchar(c);
 }
 
-static void sbi_console_write(struct console *con,
-			      const char *s, unsigned n)
+static void sbi_0_1_console_write(struct console *con,
+				  const char *s, unsigned int n)
 {
 	struct earlycon_device *dev = con->data;
 	uart_console_write(&dev->port, s, n, sbi_putc);
 }
 
+static void sbi_dbcn_console_write(struct console *con,
+				   const char *s, unsigned int n)
+{
+	phys_addr_t pa = __pa(s);
+
+	if (IS_ENABLED(CONFIG_32BIT))
+		sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
+			  n, lower_32_bits(pa), upper_32_bits(pa), 0, 0, 0);
+	else
+		sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
+			  n, pa, 0, 0, 0, 0);
+}
+
 static int __init early_sbi_setup(struct earlycon_device *device,
 				  const char *opt)
 {
-	device->con->write = sbi_console_write;
-	return 0;
+	int ret = 0;
+
+	if ((sbi_spec_version >= sbi_mk_version(2, 0)) &&
+	    (sbi_probe_extension(SBI_EXT_DBCN) > 0)) {
+		device->con->write = sbi_dbcn_console_write;
+	} else {
+		if (IS_ENABLED(CONFIG_RISCV_SBI_V01))
+			device->con->write = sbi_0_1_console_write;
+		else
+			ret = -ENODEV;
+	}
+
+	return ret;
 }
 EARLYCON_DECLARE(sbi, early_sbi_setup);
Signature · 1 line
2.34.1
author
#15 lore raw

PATCH v2 7/8 tty: Add SBI debug console support to HVC SBI driver

In reply to Anup Patel

From: Atish Patra <[email protected]>

RISC-V SBI specification supports advanced debug console support via SBI DBCN extension.

Extend the HVC SBI driver to support it.

---
 drivers/tty/hvc/Kconfig         |  2 +-
 drivers/tty/hvc/hvc_riscv_sbi.c | 76 ++++++++++++++++++++++++++++++---
 2 files changed, 70 insertions(+), 8 deletions(-)
drivers/tty/hvc/Kconfig +1 -1
index 4f9264d005c0..6e05c5c7bca1 100644
@@ -108,7 +108,7 @@ config HVC_DCC_SERIALIZE_SMP
 
 config HVC_RISCV_SBI
 	bool "RISC-V SBI console support"
-	depends on RISCV_SBI_V01
+	depends on RISCV_SBI
 	select HVC_DRIVER
 	help
 	  This enables support for console output via RISC-V SBI calls, which
drivers/tty/hvc/hvc_riscv_sbi.c +69 -7

90 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#18 lore raw

PATCH v2 8/8 RISC-V: Enable SBI based earlycon support

In reply to Anup Patel

Let us enable SBI based earlycon support in defconfigs for both RV32 and RV64 so that "earlycon=sbi" can be used again.

---
 arch/riscv/configs/defconfig      | 1 +
 arch/riscv/configs/rv32_defconfig | 1 +
 2 files changed, 2 insertions(+)
arch/riscv/configs/defconfig +1 -0
index ab86ec3b9eab..f82700da0056 100644
@@ -132,6 +132,7 @@ CONFIG_SERIAL_8250_CONSOLE=y
 CONFIG_SERIAL_8250_DW=y
 CONFIG_SERIAL_OF_PLATFORM=y
 CONFIG_SERIAL_SH_SCI=y
+CONFIG_SERIAL_EARLYCON_RISCV_SBI=y
 CONFIG_VIRTIO_CONSOLE=y
 CONFIG_HW_RANDOM=y
 CONFIG_HW_RANDOM_VIRTIO=y
arch/riscv/configs/rv32_defconfig +1 -0
index 89b601e253a6..5721af39afd1 100644
@@ -66,6 +66,7 @@ CONFIG_INPUT_MOUSEDEV=y
 CONFIG_SERIAL_8250=y
 CONFIG_SERIAL_8250_CONSOLE=y
 CONFIG_SERIAL_OF_PLATFORM=y
+CONFIG_SERIAL_EARLYCON_RISCV_SBI=y
 CONFIG_VIRTIO_CONSOLE=y
 CONFIG_HW_RANDOM=y
 CONFIG_HW_RANDOM_VIRTIO=y
Signature · 1 line
2.34.1
#16 lore raw

Re: [PATCH v2 7/8] tty: Add SBI debug console support to HVC SBI driver

In reply to Anup Patel
Quoted 40 lines · Anup Patel

From: Atish Patra <[email protected]>

RISC-V SBI specification supports advanced debug console support via SBI DBCN extension.

Extend the HVC SBI driver to support it.

---
 drivers/tty/hvc/Kconfig         |  2 +-
 drivers/tty/hvc/hvc_riscv_sbi.c | 76 ++++++++++++++++++++++++++++++---
 2 files changed, 70 insertions(+), 8 deletions(-)
drivers/tty/hvc/Kconfig +1 -1
index 4f9264d005c0..6e05c5c7bca1 100644
@@ -108,7 +108,7 @@ config HVC_DCC_SERIALIZE_SMP
 
 config HVC_RISCV_SBI
 	bool "RISC-V SBI console support"
-	depends on RISCV_SBI_V01
+	depends on RISCV_SBI
 	select HVC_DRIVER
 	help
 	  This enables support for console output via RISC-V SBI calls, which
drivers/tty/hvc/hvc_riscv_sbi.c +1 -1
index 31f53fa77e4a..da318d7f55c5 100644
@@ -39,21 +39,83 @@ static int hvc_sbi_tty_get(uint32_t vtermno, char *buf, int count)
 	return i;
 }
 
-static const struct hv_ops hvc_sbi_ops = {
+static const struct hv_ops hvc_sbi_v01_ops = {
 	.get_chars = hvc_sbi_tty_get,
 	.put_chars = hvc_sbi_tty_put,
 };

drivers/tty/hvc/hvc_riscv_sbi.c static int hvc_sbi_tty_get(uint32_t vtermno, char *buf, int count) · line 53

Quoted 10 lines
drivers/tty/hvc/hvc_riscv_sbi.c +6 -2
 
-static int __init hvc_sbi_init(void)
+static int hvc_sbi_dbcn_tty_put(uint32_t vtermno, const char *buf, int count)
 {
-	return PTR_ERR_OR_ZERO(hvc_alloc(0, 0, &hvc_sbi_ops, 16));
+	phys_addr_t pa;
+	struct sbiret ret;
+
+	if (is_vmalloc_addr(buf))
+		pa = page_to_phys(vmalloc_to_page(buf)) + offset_in_page(buf);

What is assumed from buf here? If buf is crossing a page, you need to adjust the count, no?

Quoted 16 lines
drivers/tty/hvc/hvc_riscv_sbi.c +14 -1
+	else
+		pa = __pa(buf);
+
+	if (IS_ENABLED(CONFIG_32BIT))
+		ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
+				count, lower_32_bits(pa), upper_32_bits(pa),
+				0, 0, 0);
+	else
+		ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
+				count, pa, 0, 0, 0, 0);
+	if (ret.error)
+		return 0;
+
+	return count;
 }
-device_initcall(hvc_sbi_init);

drivers/tty/hvc/hvc_riscv_sbi.c static int hvc_sbi_tty_get(uint32_t vtermno, char *buf, int count) · line 76

Quoted 10 lines
drivers/tty/hvc/hvc_riscv_sbi.c +6 -2
 
-static int __init hvc_sbi_console_init(void)
+static int hvc_sbi_dbcn_tty_get(uint32_t vtermno, char *buf, int count)
 {
-	hvc_instantiate(0, 0, &hvc_sbi_ops);
+	phys_addr_t pa;
+	struct sbiret ret;
+
+	if (is_vmalloc_addr(buf))
+		pa = page_to_phys(vmalloc_to_page(buf)) + offset_in_page(buf);

And definitely adjust count here, if we're crossing a page!

Bj?rn

author
#17 lore raw

Re: [PATCH v2 7/8] tty: Add SBI debug console support to HVC SBI driver

Quoted 44 lines · On Thu, Oct 12, 2023 at 5:08?PM Bj?rn T?pel <[email protected]> wrote:
Quoted 41 lines · Anup Patel

From: Atish Patra <[email protected]>

RISC-V SBI specification supports advanced debug console support via SBI DBCN extension.

Extend the HVC SBI driver to support it.

---
 drivers/tty/hvc/Kconfig         |  2 +-
 drivers/tty/hvc/hvc_riscv_sbi.c | 76 ++++++++++++++++++++++++++++++---
 2 files changed, 70 insertions(+), 8 deletions(-)
drivers/tty/hvc/Kconfig +1 -1
index 4f9264d005c0..6e05c5c7bca1 100644
@@ -108,7 +108,7 @@ config HVC_DCC_SERIALIZE_SMP
 
 config HVC_RISCV_SBI
      bool "RISC-V SBI console support"
-     depends on RISCV_SBI_V01
+     depends on RISCV_SBI
      select HVC_DRIVER
      help
        This enables support for console output via RISC-V SBI calls, which
drivers/tty/hvc/hvc_riscv_sbi.c +1 -1
index 31f53fa77e4a..da318d7f55c5 100644
@@ -39,21 +39,83 @@ static int hvc_sbi_tty_get(uint32_t vtermno, char *buf, int count)
      return i;
 }
 
-static const struct hv_ops hvc_sbi_ops = {
+static const struct hv_ops hvc_sbi_v01_ops = {
      .get_chars = hvc_sbi_tty_get,
      .put_chars = hvc_sbi_tty_put,
 };
 
Quoted 12 lines

drivers/tty/hvc/hvc_riscv_sbi.c static int hvc_sbi_tty_get(uint32_t vtermno, char *buf, int count) · line 53

Quoted 9 lines
drivers/tty/hvc/hvc_riscv_sbi.c +6 -2
-static int __init hvc_sbi_init(void)
+static int hvc_sbi_dbcn_tty_put(uint32_t vtermno, const char *buf, int count)
 {
-     return PTR_ERR_OR_ZERO(hvc_alloc(0, 0, &hvc_sbi_ops, 16));
+     phys_addr_t pa;
+     struct sbiret ret;
+
+     if (is_vmalloc_addr(buf))
+             pa = page_to_phys(vmalloc_to_page(buf)) + offset_in_page(buf);

What is assumed from buf here? If buf is crossing a page, you need to adjust the count, no?

I never saw a page crossing buffer but I will certainly address this in the next revision.

Quoted 18 lines
Quoted 18 lines
drivers/tty/hvc/hvc_riscv_sbi.c +14 -1
+     else
+             pa = __pa(buf);
+
+     if (IS_ENABLED(CONFIG_32BIT))
+             ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
+                             count, lower_32_bits(pa), upper_32_bits(pa),
+                             0, 0, 0);
+     else
+             ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
+                             count, pa, 0, 0, 0, 0);
+     if (ret.error)
+             return 0;
+
+     return count;
 }
-device_initcall(hvc_sbi_init);
 
Quoted 11 lines

drivers/tty/hvc/hvc_riscv_sbi.c static int hvc_sbi_tty_get(uint32_t vtermno, char *buf, int count) · line 76

Quoted 9 lines
drivers/tty/hvc/hvc_riscv_sbi.c +6 -2
-static int __init hvc_sbi_console_init(void)
+static int hvc_sbi_dbcn_tty_get(uint32_t vtermno, char *buf, int count)
 {
-     hvc_instantiate(0, 0, &hvc_sbi_ops);
+     phys_addr_t pa;
+     struct sbiret ret;
+
+     if (is_vmalloc_addr(buf))
+             pa = page_to_phys(vmalloc_to_page(buf)) + offset_in_page(buf);

And definitely adjust count here, if we're crossing a page!

Sure, I will update here as well.

Thanks,
Anup

Reviewed-by on v2 1/8
#2 lore raw

Re: [PATCH v2 1/8] RISC-V: Add defines for SBI debug console extension

In reply to Anup Patel
Quoted 36 lines · Anup Patel

We add SBI debug console extension related defines/enum to the asm/sbi.h header.

---
 arch/riscv/include/asm/sbi.h | 7 +++++++
 1 file changed, 7 insertions(+)
arch/riscv/include/asm/sbi.h +7 -0
index 5b4a1bf5f439..12dfda6bb924 100644
@@ -30,6 +30,7 @@ enum sbi_ext_id {
 	SBI_EXT_HSM = 0x48534D,
 	SBI_EXT_SRST = 0x53525354,
 	SBI_EXT_PMU = 0x504D55,
+	SBI_EXT_DBCN = 0x4442434E,
 
 	/* Experimentals extensions must lie within this range */
 	SBI_EXT_EXPERIMENTAL_START = 0x08000000,
@@ -236,6 +237,12 @@ enum sbi_pmu_ctr_type {
 /* Flags defined for counter stop function */
 #define SBI_PMU_STOP_FLAG_RESET (1 << 0)
 
+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_SHIFT	24
 #define SBI_SPEC_VERSION_MAJOR_MASK	0x7f
Signature · 1 line
2.34.1
Reviewed-by on v2 2/8
#4 lore raw

Re: [PATCH v2 2/8] RISC-V: KVM: Change the SBI specification version to v2.0

In reply to Anup Patel
Quoted 24 lines · Anup Patel

We will be implementing SBI DBCN extension for KVM RISC-V so let us change the KVM RISC-V SBI specification version to v2.0.

---
 arch/riscv/include/asm/kvm_vcpu_sbi.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/include/asm/kvm_vcpu_sbi.h +1 -1
index cdcf0ff07be7..8d6d4dce8a5e 100644
@@ -11,7 +11,7 @@
 
 #define KVM_SBI_IMPID 3
 
-#define KVM_SBI_VERSION_MAJOR 1
+#define KVM_SBI_VERSION_MAJOR 2
 #define KVM_SBI_VERSION_MINOR 0
 
 enum kvm_riscv_sbi_ext_status {
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH v2 3/8] RISC-V: KVM: Allow some SBI extensions to be disabled by default

In reply to Anup Patel
Quoted 45 lines · Anup Patel

Currently, all SBI extensions are enabled by default which is problematic for SBI extensions (such as DBCN) which are forwarded to the KVM user-space because we might have an older KVM user-space which is not aware/ready to handle newer SBI extensions. Ideally, the SBI extensions forwarded to the KVM user-space must be disabled by default.

To address above, we allow certain SBI extensions to be disabled by default so that KVM user-space must explicitly enable such SBI extensions to receive forwarded calls from Guest VCPU.

---
 arch/riscv/include/asm/kvm_vcpu_sbi.h |  4 +++
 arch/riscv/kvm/vcpu.c                 |  6 ++++
 arch/riscv/kvm/vcpu_sbi.c             | 45 ++++++++++++++++-----------
 3 files changed, 36 insertions(+), 19 deletions(-)
arch/riscv/include/asm/kvm_vcpu_sbi.h +4 -0
index 8d6d4dce8a5e..c02bda5559d7 100644
@@ -35,6 +35,9 @@ struct kvm_vcpu_sbi_return {
 struct kvm_vcpu_sbi_extension {
 	unsigned long extid_start;
 	unsigned long extid_end;
+
+	bool default_unavail;
+
 	/**
 	 * SBI extension handler. It can be defined for a given extension or group of
 	 * extension. But it should always return linux error codes rather than SBI
@@ -59,6 +62,7 @@ int kvm_riscv_vcpu_get_reg_sbi_ext(struct kvm_vcpu *vcpu,
 const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext(
 				struct kvm_vcpu *vcpu, unsigned long extid);
 int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run);
+void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu);
 
 #ifdef CONFIG_RISCV_SBI_V01
 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_v01;
arch/riscv/kvm/vcpu.c +0 -0
index c061a1c5fe98..e087c809073c 100644

arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) · line 148

Quoted 8 lines
arch/riscv/kvm/vcpu.c +5 -0
@@ -141,6 +141,12 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 	if (rc)
 		return rc;
 
+	/*
+	 * Setup SBI extensions
+	 * NOTE: This must be the last thing to be initialized.
+	 */
+	kvm_riscv_vcpu_sbi_init(vcpu);

With this, we no longer defer probing to the first access (whether that's by the guest or KVM userspace). With our current small set of SBI extensions where only a single one has a probe function, then this simpler approach is good enough. We can always go back to the lazy approach later if needed.

Quoted 13 lines
arch/riscv/kvm/vcpu.c +1 -0
+
 	/* Reset VCPU */
 	kvm_riscv_reset_vcpu(vcpu);
 
arch/riscv/kvm/vcpu_sbi.c +0 -1
index 9cd97091c723..1b1cee86efda 100644
@@ -155,14 +155,8 @@ static int riscv_vcpu_set_sbi_ext_single(struct kvm_vcpu *vcpu,
 	if (!sext)
 		return -ENOENT;
 
-	/*

arch/riscv/kvm/vcpu_sbi.c static int riscv_vcpu_set_sbi_ext_single(struct kvm_vcpu *vcpu, · line 160

Quoted 10 lines
arch/riscv/kvm/vcpu_sbi.c +2 -7
-	 * We can't set the extension status to available here, since it may
-	 * have a probe() function which needs to confirm availability first,
-	 * but it may be too early to call that here. We can set the status to
-	 * unavailable, though.
-	 */
-	if (!reg_val)
-		scontext->ext_status[sext->ext_idx] =
+	scontext->ext_status[sext->ext_idx] = (reg_val) ?
+			KVM_RISCV_SBI_EXT_AVAILABLE :
 			KVM_RISCV_SBI_EXT_UNAVAILABLE;

We're missing the change to riscv_vcpu_get_sbi_ext_single() which should also drop the comment block explaining the limits to status knowledge without initial probing (which we now do) and then just check for available, i.e.

arch/riscv/kvm/vcpu_sbi.c +2 -9
index bb76c3cf633f..92c42d9aba1c 100644
@@ -186,15 +186,8 @@ static int riscv_vcpu_get_sbi_ext_single(struct kvm_vcpu *vcpu,
 	if (!sext)
 		return -ENOENT;
 
-	/*
-	 * If the extension status is still uninitialized, then we should probe
-	 * to determine if it's available, but it may be too early to do that
-	 * here. The best we can do is report that the extension has not been
-	 * disabled, i.e. we return 1 when the extension is available and also
-	 * when it only may be available.
-	 */
-	*reg_val = scontext->ext_status[sext->ext_idx] !=
-				KVM_RISCV_SBI_EXT_UNAVAILABLE;
+	*reg_val = scontext->ext_status[sext->ext_idx] ==
+				KVM_RISCV_SBI_EXT_AVAILABLE;
 
 	return 0;
 }
Quoted 52 lines
arch/riscv/kvm/vcpu_sbi.c +24 -11
 	return 0;
@@ -337,18 +331,8 @@ const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext(
 			    scontext->ext_status[entry->ext_idx] ==
 						KVM_RISCV_SBI_EXT_AVAILABLE)
 				return ext;
-			if (scontext->ext_status[entry->ext_idx] ==
-						KVM_RISCV_SBI_EXT_UNAVAILABLE)
-				return NULL;
-			if (ext->probe && !ext->probe(vcpu)) {
-				scontext->ext_status[entry->ext_idx] =
-					KVM_RISCV_SBI_EXT_UNAVAILABLE;
-				return NULL;
-			}
 
-			scontext->ext_status[entry->ext_idx] =
-				KVM_RISCV_SBI_EXT_AVAILABLE;
-			return ext;
+			return NULL;
 		}
 	}
 
@@ -419,3 +403,26 @@ int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run)
 
 	return ret;
 }
+
+void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu)
+{
+	struct kvm_vcpu_sbi_context *scontext = &vcpu->arch.sbi_context;
+	const struct kvm_riscv_sbi_extension_entry *entry;
+	const struct kvm_vcpu_sbi_extension *ext;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(sbi_ext); i++) {
+		entry = &sbi_ext[i];
+		ext = entry->ext_ptr;
+
+		if (ext->probe && !ext->probe(vcpu)) {
+			scontext->ext_status[entry->ext_idx] =
+				KVM_RISCV_SBI_EXT_UNAVAILABLE;
+			continue;
+		}
+
+		scontext->ext_status[entry->ext_idx] = ext->default_unavail ?
+					KVM_RISCV_SBI_EXT_UNAVAILABLE :
+					KVM_RISCV_SBI_EXT_AVAILABLE;
+	}
+}
Signature · 1 line
2.34.1

Thanks,
drew

Reviewed-by on v2 4/8
#9 lore raw

Re: [PATCH v2 4/8] RISC-V: KVM: Forward SBI DBCN extension to user-space

In reply to Anup Patel
Quoted 103 lines · Anup Patel

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.

---
 arch/riscv/include/asm/kvm_vcpu_sbi.h |  1 +
 arch/riscv/include/uapi/asm/kvm.h     |  1 +
 arch/riscv/kvm/vcpu_sbi.c             |  4 ++++
 arch/riscv/kvm/vcpu_sbi_replace.c     | 32 +++++++++++++++++++++++++++
 4 files changed, 38 insertions(+)
arch/riscv/include/asm/kvm_vcpu_sbi.h +1 -0
index c02bda5559d7..6a453f7f8b56 100644
@@ -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;
 
arch/riscv/include/uapi/asm/kvm.h +1 -0
index 917d8cc2489e..60d3b21dead7 100644
@@ -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,
 };
 
arch/riscv/kvm/vcpu_sbi.c +4 -0
index 1b1cee86efda..bb76c3cf633f 100644
@@ -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,
arch/riscv/kvm/vcpu_sbi_replace.c +32 -0
index 7c4d5d38a339..23b57c931b15 100644
@@ -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,
+};
Signature · 1 line
2.34.1
Reviewed-by on v2 5/8
#12 lore raw

Re: [PATCH v2 5/8] RISC-V: Add inline version of sbi_console_putchar/getchar() functions

In reply to Anup Patel
Quoted 3 lines · Anup Patel

The functions sbi_console_putchar() and sbi_console_getchar() are not defined when CONFIG_RISCV_SBI_V01 is disabled so let us add inline version of these functions to avoid "#ifdef" on user side.

nit: Here and in the summary I'd probably use the word "stub" instead of "inline version".

Quoted 27 lines
---
 arch/riscv/include/asm/sbi.h | 5 +++++
 1 file changed, 5 insertions(+)
arch/riscv/include/asm/sbi.h +5 -0
index 12dfda6bb924..cbcefa344417 100644
@@ -271,8 +271,13 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
 			unsigned long arg3, unsigned long arg4,
 			unsigned long arg5);
 
+#ifdef CONFIG_RISCV_SBI_V01
 void sbi_console_putchar(int ch);
 int sbi_console_getchar(void);
+#else
+static inline void sbi_console_putchar(int ch) { }
+static inline int sbi_console_getchar(void) { return -1; }
+#endif
 long sbi_get_mvendorid(void);
 long sbi_get_marchid(void);
 long sbi_get_mimpid(void);
Signature · 1 line
2.34.1

Otherwise,

Reviewed-by on v2 6/8
#14 lore raw

Re: [PATCH v2 6/8] tty/serial: Add RISC-V SBI debug console based earlycon

In reply to Anup Patel
Quoted 75 lines · Anup Patel

We extend the existing RISC-V SBI earlycon support to use the new RISC-V SBI debug console extension.

---
 drivers/tty/serial/Kconfig              |  2 +-
 drivers/tty/serial/earlycon-riscv-sbi.c | 32 +++++++++++++++++++++----
 2 files changed, 29 insertions(+), 5 deletions(-)
drivers/tty/serial/Kconfig +1 -1
index bdc568a4ab66..cec46091a716 100644
@@ -87,7 +87,7 @@ config SERIAL_EARLYCON_SEMIHOST
 
 config SERIAL_EARLYCON_RISCV_SBI
 	bool "Early console using RISC-V SBI"
-	depends on RISCV_SBI_V01
+	depends on RISCV_SBI
 	select SERIAL_CORE
 	select SERIAL_CORE_CONSOLE
 	select SERIAL_EARLYCON
drivers/tty/serial/earlycon-riscv-sbi.c +28 -4
index 27afb0b74ea7..c21cdef254e7 100644
@@ -15,17 +15,41 @@ static void sbi_putc(struct uart_port *port, unsigned char c)
 	sbi_console_putchar(c);
 }
 
-static void sbi_console_write(struct console *con,
-			      const char *s, unsigned n)
+static void sbi_0_1_console_write(struct console *con,
+				  const char *s, unsigned int n)
 {
 	struct earlycon_device *dev = con->data;
 	uart_console_write(&dev->port, s, n, sbi_putc);
 }
 
+static void sbi_dbcn_console_write(struct console *con,
+				   const char *s, unsigned int n)
+{
+	phys_addr_t pa = __pa(s);
+
+	if (IS_ENABLED(CONFIG_32BIT))
+		sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
+			  n, lower_32_bits(pa), upper_32_bits(pa), 0, 0, 0);
+	else
+		sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
+			  n, pa, 0, 0, 0, 0);
+}
+
 static int __init early_sbi_setup(struct earlycon_device *device,
 				  const char *opt)
 {
-	device->con->write = sbi_console_write;
-	return 0;
+	int ret = 0;
+
+	if ((sbi_spec_version >= sbi_mk_version(2, 0)) &&
+	    (sbi_probe_extension(SBI_EXT_DBCN) > 0)) {
+		device->con->write = sbi_dbcn_console_write;
+	} else {
+		if (IS_ENABLED(CONFIG_RISCV_SBI_V01))
+			device->con->write = sbi_0_1_console_write;
+		else
+			ret = -ENODEV;
+	}
+
+	return ret;
 }
 EARLYCON_DECLARE(sbi, early_sbi_setup);
Signature · 1 line
2.34.1
Reviewed-by on v2 8/8
#19 lore raw

Re: [PATCH v2 8/8] RISC-V: Enable SBI based earlycon support

In reply to Anup Patel
Quoted 36 lines · Anup Patel

Let us enable SBI based earlycon support in defconfigs for both RV32 and RV64 so that "earlycon=sbi" can be used again.

---
 arch/riscv/configs/defconfig      | 1 +
 arch/riscv/configs/rv32_defconfig | 1 +
 2 files changed, 2 insertions(+)
arch/riscv/configs/defconfig +1 -0
index ab86ec3b9eab..f82700da0056 100644
@@ -132,6 +132,7 @@ CONFIG_SERIAL_8250_CONSOLE=y
 CONFIG_SERIAL_8250_DW=y
 CONFIG_SERIAL_OF_PLATFORM=y
 CONFIG_SERIAL_SH_SCI=y
+CONFIG_SERIAL_EARLYCON_RISCV_SBI=y
 CONFIG_VIRTIO_CONSOLE=y
 CONFIG_HW_RANDOM=y
 CONFIG_HW_RANDOM_VIRTIO=y
arch/riscv/configs/rv32_defconfig +1 -0
index 89b601e253a6..5721af39afd1 100644
@@ -66,6 +66,7 @@ CONFIG_INPUT_MOUSEDEV=y
 CONFIG_SERIAL_8250=y
 CONFIG_SERIAL_8250_CONSOLE=y
 CONFIG_SERIAL_OF_PLATFORM=y
+CONFIG_SERIAL_EARLYCON_RISCV_SBI=y
 CONFIG_VIRTIO_CONSOLE=y
 CONFIG_HW_RANDOM=y
 CONFIG_HW_RANDOM_VIRTIO=y
Signature · 1 line
2.34.1
#10 lore raw

Re: [PATCH v2 4/8] RISC-V: KVM: Forward SBI DBCN extension to user-space

In reply to Anup Patel
Quoted 38 lines · Anup Patel

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.

---
 arch/riscv/include/asm/kvm_vcpu_sbi.h |  1 +
 arch/riscv/include/uapi/asm/kvm.h     |  1 +
 arch/riscv/kvm/vcpu_sbi.c             |  4 ++++
 arch/riscv/kvm/vcpu_sbi_replace.c     | 32 +++++++++++++++++++++++++++
 4 files changed, 38 insertions(+)
arch/riscv/include/asm/kvm_vcpu_sbi.h +1 -0
index c02bda5559d7..6a453f7f8b56 100644
@@ -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;
 
arch/riscv/include/uapi/asm/kvm.h +0 -0
index 917d8cc2489e..60d3b21dead7 100644

arch/riscv/include/uapi/asm/kvm.h enum KVM_RISCV_SBI_EXT_ID { · line 159

Quoted 4 lines
arch/riscv/include/uapi/asm/kvm.h +1 -0
@@ -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,

We should add this new register to the get-reg-list kselftest, i.e.

tools/testing/selftests/kvm/riscv/get-reg-list.c +1 -0
index 234006d035c9..4a0f8a8cfbf8 100644
@@ -565,6 +565,7 @@ static __u64 base_regs[] = {
 	KVM_REG_RISCV | KVM_REG_SIZE_ULONG | KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_SRST,
 	KVM_REG_RISCV | KVM_REG_SIZE_ULONG | KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_HSM,
 	KVM_REG_RISCV | KVM_REG_SIZE_ULONG | KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_PMU,
+	KVM_REG_RISCV | KVM_REG_SIZE_ULONG | KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_DBCN,
 	KVM_REG_RISCV | KVM_REG_SIZE_ULONG | KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_EXPERIMENTAL,
 	KVM_REG_RISCV | KVM_REG_SIZE_ULONG | KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_VENDOR,
 	KVM_REG_RISCV | KVM_REG_SIZE_ULONG | KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_MULTI_EN | 0,

Thanks,
drew

author
#7 lore raw

Re: [PATCH v2 3/8] RISC-V: KVM: Allow some SBI extensions to be disabled by default

In reply to Andrew Jones
Quoted 50 lines · On Thu, Oct 19, 2023 at 1:27?PM Andrew Jones <[email protected]> wrote:
Quoted 48 lines · Anup Patel

Currently, all SBI extensions are enabled by default which is problematic for SBI extensions (such as DBCN) which are forwarded to the KVM user-space because we might have an older KVM user-space which is not aware/ready to handle newer SBI extensions. Ideally, the SBI extensions forwarded to the KVM user-space must be disabled by default.

To address above, we allow certain SBI extensions to be disabled by default so that KVM user-space must explicitly enable such SBI extensions to receive forwarded calls from Guest VCPU.

---
 arch/riscv/include/asm/kvm_vcpu_sbi.h |  4 +++
 arch/riscv/kvm/vcpu.c                 |  6 ++++
 arch/riscv/kvm/vcpu_sbi.c             | 45 ++++++++++++++++-----------
 3 files changed, 36 insertions(+), 19 deletions(-)
arch/riscv/include/asm/kvm_vcpu_sbi.h +4 -0
index 8d6d4dce8a5e..c02bda5559d7 100644
@@ -35,6 +35,9 @@ struct kvm_vcpu_sbi_return {
 struct kvm_vcpu_sbi_extension {
      unsigned long extid_start;
      unsigned long extid_end;
+
+     bool default_unavail;
+
      /**
       * SBI extension handler. It can be defined for a given extension or group of
       * extension. But it should always return linux error codes rather than SBI
@@ -59,6 +62,7 @@ int kvm_riscv_vcpu_get_reg_sbi_ext(struct kvm_vcpu *vcpu,
 const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext(
                              struct kvm_vcpu *vcpu, unsigned long extid);
 int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run);
+void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu);
 
 #ifdef CONFIG_RISCV_SBI_V01
 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_v01;
arch/riscv/kvm/vcpu.c +0 -0
index c061a1c5fe98..e087c809073c 100644
@@ -141,6 +141,12 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
      if (rc)
              return rc;
 
Quoted 11 lines

arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) · line 148

Quoted 5 lines
arch/riscv/kvm/vcpu.c +5 -0
+     /*
+      * Setup SBI extensions
+      * NOTE: This must be the last thing to be initialized.
+      */
+     kvm_riscv_vcpu_sbi_init(vcpu);

With this, we no longer defer probing to the first access (whether that's by the guest or KVM userspace). With our current small set of SBI extensions where only a single one has a probe function, then this simpler approach is good enough. We can always go back to the lazy approach later if needed.

I agree. We can fallback to lazy probing in the future if required.

Quoted 42 lines
Quoted 14 lines
arch/riscv/kvm/vcpu.c +1 -0
+
      /* Reset VCPU */
      kvm_riscv_reset_vcpu(vcpu);
 
arch/riscv/kvm/vcpu_sbi.c +0 -1
index 9cd97091c723..1b1cee86efda 100644
@@ -155,14 +155,8 @@ static int riscv_vcpu_set_sbi_ext_single(struct kvm_vcpu *vcpu,
      if (!sext)
              return -ENOENT;
 
-     /*

arch/riscv/kvm/vcpu_sbi.c static int riscv_vcpu_set_sbi_ext_single(struct kvm_vcpu *vcpu, · line 160

Quoted 10 lines
arch/riscv/kvm/vcpu_sbi.c +2 -7
-      * We can't set the extension status to available here, since it may
-      * have a probe() function which needs to confirm availability first,
-      * but it may be too early to call that here. We can set the status to
-      * unavailable, though.
-      */
-     if (!reg_val)
-             scontext->ext_status[sext->ext_idx] =
+     scontext->ext_status[sext->ext_idx] = (reg_val) ?
+                     KVM_RISCV_SBI_EXT_AVAILABLE :
                      KVM_RISCV_SBI_EXT_UNAVAILABLE;

We're missing the change to riscv_vcpu_get_sbi_ext_single() which should also drop the comment block explaining the limits to status knowledge without initial probing (which we now do) and then just check for available, i.e.

arch/riscv/kvm/vcpu_sbi.c +0 -4
index bb76c3cf633f..92c42d9aba1c 100644
@@ -186,15 +186,8 @@ static int riscv_vcpu_get_sbi_ext_single(struct kvm_vcpu *vcpu,
        if (!sext)
                return -ENOENT;
 
-       /*
-        * If the extension status is still uninitialized, then we should probe
-        * to determine if it's available, but it may be too early to do that
-        * here. The best we can do is report that the extension has not been

arch/riscv/kvm/vcpu_sbi.c static int riscv_vcpu_get_sbi_ext_single(struct kvm_vcpu *vcpu, · line 193

Quoted 10 lines
arch/riscv/kvm/vcpu_sbi.c +2 -5
-        * disabled, i.e. we return 1 when the extension is available and also
-        * when it only may be available.
-        */
-       *reg_val = scontext->ext_status[sext->ext_idx] !=
-                               KVM_RISCV_SBI_EXT_UNAVAILABLE;
+       *reg_val = scontext->ext_status[sext->ext_idx] ==
+                               KVM_RISCV_SBI_EXT_AVAILABLE;
 
        return 0;
 }

Thanks, I will include this change in the next revision.

Quoted 48 lines
Quoted 48 lines
arch/riscv/kvm/vcpu_sbi.c +22 -11
      return 0;
@@ -337,18 +331,8 @@ const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext(
                          scontext->ext_status[entry->ext_idx] ==
                                              KVM_RISCV_SBI_EXT_AVAILABLE)
                              return ext;
-                     if (scontext->ext_status[entry->ext_idx] ==
-                                             KVM_RISCV_SBI_EXT_UNAVAILABLE)
-                             return NULL;
-                     if (ext->probe && !ext->probe(vcpu)) {
-                             scontext->ext_status[entry->ext_idx] =
-                                     KVM_RISCV_SBI_EXT_UNAVAILABLE;
-                             return NULL;
-                     }
 
-                     scontext->ext_status[entry->ext_idx] =
-                             KVM_RISCV_SBI_EXT_AVAILABLE;
-                     return ext;
+                     return NULL;
              }
      }
 
@@ -419,3 +403,26 @@ int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run)
 
      return ret;
 }
+
+void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu)
+{
+     struct kvm_vcpu_sbi_context *scontext = &vcpu->arch.sbi_context;
+     const struct kvm_riscv_sbi_extension_entry *entry;
+     const struct kvm_vcpu_sbi_extension *ext;
+     int i;
+
+     for (i = 0; i < ARRAY_SIZE(sbi_ext); i++) {
+             entry = &sbi_ext[i];
+             ext = entry->ext_ptr;
+
+             if (ext->probe && !ext->probe(vcpu)) {
+                     scontext->ext_status[entry->ext_idx] =
+                             KVM_RISCV_SBI_EXT_UNAVAILABLE;
+                     continue;
+             }
+
+             scontext->ext_status[entry->ext_idx] = ext->default_unavail ?
+                                     KVM_RISCV_SBI_EXT_UNAVAILABLE :
+                                     KVM_RISCV_SBI_EXT_AVAILABLE;
Quoted 8 lines
Quoted 5 lines
arch/riscv/kvm/vcpu_sbi.c +2 -1
+     }
+}
--

2.34.1

Thanks,
drew

Regards,
Anup