From: Kautuk Consul <kconsul@ventanamicro.com>
To: Rayagonda Kokatanur <rayagonda.kokatanur@broadcom.com>,
Sean Anderson <sean.anderson@seco.com>,
Rick Chen <rick@andestech.com>, Leo <ycliang@andestech.com>,
Bin Meng <bmeng.cn@gmail.com>, Simon Glass <sjg@chromium.org>,
Ilias Apalodimas <ilias.apalodimas@linaro.org>,
Alexandru Gagniuc <mr.nuke.me@gmail.com>,
Philippe Reynes <philippe.reynes@softathome.com>,
Heinrich Schuchardt <xypron.glpk@gmx.de>,
Rasmus Villemoes <rasmus.villemoes@prevas.dk>,
Eugen Hristev <eugen.hristev@microchip.com>,
Stefan Roese <sr@denx.de>, Loic Poulain <loic.poulain@linaro.org>,
Peng Fan <peng.fan@nxp.com>, Michal Simek <michal.simek@amd.com>
Cc: u-boot@lists.denx.de, Kautuk Consul <kconsul@ventanamicro.com>,
Anup Patel <apatel@ventanamicro.com>
Subject: [PATCH v5 2/3] arch/riscv: add semihosting support for RISC-V
Date: Fri, 23 Sep 2022 12:33:19 +0530 [thread overview]
Message-ID: <20220923070320.617623-3-kconsul@ventanamicro.com> (raw)
In-Reply-To: <20220923070320.617623-1-kconsul@ventanamicro.com>
We add RISC-V semihosting based serial console for JTAG based early
debugging.
The RISC-V semihosting specification is available at:
https://github.com/riscv/riscv-semihosting-spec/blob/main/riscv-semihosting-spec.adoc
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Signed-off-by: Kautuk Consul <kconsul@ventanamicro.com>
---
arch/riscv/include/asm/spl.h | 1 +
arch/riscv/lib/Makefile | 2 ++
arch/riscv/lib/interrupts.c | 25 +++++++++++++++++++++++++
arch/riscv/lib/semihosting.c | 24 ++++++++++++++++++++++++
lib/Kconfig | 10 +++++-----
5 files changed, 57 insertions(+), 5 deletions(-)
create mode 100644 arch/riscv/lib/semihosting.c
diff --git a/arch/riscv/include/asm/spl.h b/arch/riscv/include/asm/spl.h
index e8a94fcb1f..2898a770ee 100644
--- a/arch/riscv/include/asm/spl.h
+++ b/arch/riscv/include/asm/spl.h
@@ -25,6 +25,7 @@ enum {
BOOT_DEVICE_DFU,
BOOT_DEVICE_XIP,
BOOT_DEVICE_BOOTROM,
+ BOOT_DEVICE_SMH,
BOOT_DEVICE_NONE
};
diff --git a/arch/riscv/lib/Makefile b/arch/riscv/lib/Makefile
index 06020fcc2a..64e29804c1 100644
--- a/arch/riscv/lib/Makefile
+++ b/arch/riscv/lib/Makefile
@@ -42,3 +42,5 @@ extra-$(CONFIG_EFI) += $(EFI_CRT0) $(EFI_RELOC)
obj-$(CONFIG_$(SPL_TPL_)USE_ARCH_MEMSET) += memset.o
obj-$(CONFIG_$(SPL_TPL_)USE_ARCH_MEMMOVE) += memmove.o
obj-$(CONFIG_$(SPL_TPL_)USE_ARCH_MEMCPY) += memcpy.o
+
+obj-$(CONFIG_$(SPL_TPL_)SEMIHOSTING) += semihosting.o
diff --git a/arch/riscv/lib/interrupts.c b/arch/riscv/lib/interrupts.c
index 100be2e966..e966afa7e3 100644
--- a/arch/riscv/lib/interrupts.c
+++ b/arch/riscv/lib/interrupts.c
@@ -9,6 +9,7 @@
* Copyright (C) 2019 Sean Anderson <seanga2@gmail.com>
*/
+#include <linux/compat.h>
#include <common.h>
#include <efi_loader.h>
#include <hang.h>
@@ -17,6 +18,7 @@
#include <asm/ptrace.h>
#include <asm/system.h>
#include <asm/encoding.h>
+#include <semihosting.h>
DECLARE_GLOBAL_DATA_PTR;
@@ -149,6 +151,29 @@ ulong handle_trap(ulong cause, ulong epc, ulong tval, struct pt_regs *regs)
/* An UEFI application may have changed gd. Restore U-Boot's gd. */
efi_restore_gd();
+ if (cause == CAUSE_BREAKPOINT &&
+ CONFIG_IS_ENABLED(SEMIHOSTING_FALLBACK)) {
+ ulong pre_addr = epc - 4, post_addr = epc + 4;
+
+ /* Check for prior and post addresses to be in same page. */
+ if ((pre_addr & ~(PAGE_SIZE - 1)) ==
+ (post_addr & ~(PAGE_SIZE - 1))) {
+ u32 pre = *(u32 *)pre_addr;
+ u32 post = *(u32 *)post_addr;
+
+ /* Check for semihosting, i.e.:
+ * slli zero,zero,0x1f
+ * ebreak
+ * srai zero,zero,0x7
+ */
+ if (pre == 0x01f01013 && post == 0x40705013) {
+ disable_semihosting();
+ epc += 4;
+ return epc;
+ }
+ }
+ }
+
is_irq = (cause & MCAUSE_INT);
irq = (cause & ~MCAUSE_INT);
diff --git a/arch/riscv/lib/semihosting.c b/arch/riscv/lib/semihosting.c
new file mode 100644
index 0000000000..d6593b02a6
--- /dev/null
+++ b/arch/riscv/lib/semihosting.c
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2022 Ventana Micro Systems Inc.
+ */
+
+#include <common.h>
+
+long smh_trap(int sysnum, void *addr)
+{
+ register int ret asm ("a0") = sysnum;
+ register void *param0 asm ("a1") = addr;
+
+ asm volatile (".align 4\n"
+ ".option push\n"
+ ".option norvc\n"
+
+ "slli zero, zero, 0x1f\n"
+ "ebreak\n"
+ "srai zero, zero, 7\n"
+ ".option pop\n"
+ : "+r" (ret) : "r" (param0) : "memory");
+
+ return ret;
+}
diff --git a/lib/Kconfig b/lib/Kconfig
index de7e797f11..2cc556ee8a 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -73,7 +73,7 @@ config LIB_UUID
config SEMIHOSTING
bool "Support semihosting"
- depends on ARM
+ depends on ARM || RISCV
help
Semihosting is a method for a target to communicate with a host
debugger. It uses special instructions which the debugger will trap
@@ -86,7 +86,7 @@ config SEMIHOSTING
config SEMIHOSTING_FALLBACK
bool "Recover gracefully when semihosting fails"
- depends on SEMIHOSTING && ARM64
+ depends on SEMIHOSTING && (ARM64 || RISCV)
default y
help
Normally, if U-Boot makes a semihosting call and no debugger is
@@ -96,7 +96,7 @@ config SEMIHOSTING_FALLBACK
config SPL_SEMIHOSTING
bool "Support semihosting in SPL"
- depends on SPL && ARM
+ depends on SPL && (ARM || RISCV)
help
Semihosting is a method for a target to communicate with a host
debugger. It uses special instructions which the debugger will trap
@@ -109,8 +109,8 @@ config SPL_SEMIHOSTING
config SPL_SEMIHOSTING_FALLBACK
bool "Recover gracefully when semihosting fails in SPL"
- depends on SPL_SEMIHOSTING && ARM64
- select ARMV8_SPL_EXCEPTION_VECTORS
+ depends on SPL_SEMIHOSTING && (ARM64 || RISCV)
+ select ARMV8_SPL_EXCEPTION_VECTORS if ARM64
default y
help
Normally, if U-Boot makes a semihosting call and no debugger is
--
2.34.1
next prev parent reply other threads:[~2022-09-23 7:04 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-23 7:03 [PATCH v5 0/3] Add riscv semihosting support in u-boot Kautuk Consul
2022-09-23 7:03 ` [PATCH v5 1/3] lib: Add common semihosting library Kautuk Consul
2022-10-10 15:39 ` Sean Anderson
2022-09-23 7:03 ` Kautuk Consul [this message]
2022-10-10 15:43 ` [PATCH v5 2/3] arch/riscv: add semihosting support for RISC-V Sean Anderson
[not found] ` <HK0PR03MB2994013C655E85AF1F3D2CFBC1229@HK0PR03MB2994.apcprd03.prod.outlook.com>
2022-10-12 1:40 ` Rick Chen
2022-09-23 7:03 ` [PATCH v5 3/3] board: qemu-riscv: enable semihosting Kautuk Consul
2022-10-05 6:17 ` Leo Liang
2022-10-06 5:13 ` Kautuk Consul
[not found] ` <HK0PR03MB29949257906C387D549119EBC1229@HK0PR03MB2994.apcprd03.prod.outlook.com>
2022-10-12 1:33 ` Rick Chen
2022-12-03 4:14 ` Bin Meng
2022-12-05 5:51 ` Kautuk Consul
2022-12-05 13:54 ` Bin Meng
2022-12-05 15:16 ` Sean Anderson
2022-12-06 5:42 ` Kautuk Consul
2022-12-06 10:58 ` Leo Liang
2022-12-06 11:32 ` Kautuk Consul
2022-12-07 7:01 ` Leo Liang
2022-12-07 11:47 ` Kautuk Consul
2022-12-06 15:16 ` Sean Anderson
2022-11-29 6:29 ` [PATCH v5 0/3] Add riscv semihosting support in u-boot Kautuk Consul
2022-11-29 6:36 ` Bin Meng
2022-11-29 6:57 ` Kautuk Consul
2022-12-02 8:46 ` Leo Liang
2022-12-02 9:52 ` Kautuk Consul
2022-12-02 12:01 ` Leo Liang
2022-12-03 4:28 ` Bin Meng
2022-12-03 6:01 ` Bin Meng
2022-12-05 5:50 ` Kautuk Consul
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220923070320.617623-3-kconsul@ventanamicro.com \
--to=kconsul@ventanamicro.com \
--cc=apatel@ventanamicro.com \
--cc=bmeng.cn@gmail.com \
--cc=eugen.hristev@microchip.com \
--cc=ilias.apalodimas@linaro.org \
--cc=loic.poulain@linaro.org \
--cc=michal.simek@amd.com \
--cc=mr.nuke.me@gmail.com \
--cc=peng.fan@nxp.com \
--cc=philippe.reynes@softathome.com \
--cc=rasmus.villemoes@prevas.dk \
--cc=rayagonda.kokatanur@broadcom.com \
--cc=rick@andestech.com \
--cc=sean.anderson@seco.com \
--cc=sjg@chromium.org \
--cc=sr@denx.de \
--cc=u-boot@lists.denx.de \
--cc=xypron.glpk@gmx.de \
--cc=ycliang@andestech.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.