From: Kautuk Consul <kconsul@ventanamicro.com>
To: "Rick Chen" <rick@andestech.com>, Leo <ycliang@andestech.com>,
"Sean Anderson" <sean.anderson@seco.com>,
"Bin Meng" <bmeng.cn@gmail.com>,
"Anup Patel" <apatel@ventanamicro.com>,
"Heinrich Schuchardt" <xypron.glpk@gmx.de>,
"Simon Glass" <sjg@chromium.org>,
"Ilias Apalodimas" <ilias.apalodimas@linaro.org>,
"Alexandru Gagniuc" <mr.nuke.me@gmail.com>,
"Philippe Reynes" <philippe.reynes@softathome.com>,
"Rasmus Villemoes" <rasmus.villemoes@prevas.dk>,
"Eugen Hristev" <eugen.hristev@microchip.com>,
"Stefan Roese" <sr@denx.de>, "Pali Rohár" <pali@kernel.org>,
"Qu Wenruo" <wqu@suse.com>,
"Loic Poulain" <loic.poulain@linaro.org>,
"Patrick Delaunay" <patrick.delaunay@foss.st.com>
Cc: u-boot@lists.denx.de, Kautuk Consul <kconsul@ventanamicro.com>
Subject: [PATCH v2 2/3] arch/riscv: add semihosting support for RISC-V
Date: Fri, 16 Sep 2022 13:42:32 +0530 [thread overview]
Message-ID: <20220916081233.1970135-3-kconsul@ventanamicro.com> (raw)
In-Reply-To: <20220916081233.1970135-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/Kconfig | 47 ++++++++++++++++++++++++++++
arch/riscv/include/asm/semihosting.h | 11 +++++++
arch/riscv/include/asm/spl.h | 1 +
arch/riscv/lib/Makefile | 2 ++
arch/riscv/lib/interrupts.c | 11 +++++++
arch/riscv/lib/semihosting.c | 24 ++++++++++++++
6 files changed, 96 insertions(+)
create mode 100644 arch/riscv/include/asm/semihosting.h
create mode 100644 arch/riscv/lib/semihosting.c
diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 78e964db12..b15d9028bd 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -371,4 +371,51 @@ config TPL_USE_ARCH_MEMSET
endmenu
+config SEMIHOSTING
+ bool "Support RISCV semihosting"
+ select LIB_SEMIHOSTING
+ help
+ Semihosting is a method for a target to communicate with a host
+ debugger. It uses special instructions which the debugger will trap
+ on and interpret. This allows U-Boot to read/write files, print to
+ the console, and execute arbitrary commands on the host system.
+
+ Enabling this option will add support for reading and writing files
+ on the host system. If you don't have a debugger attached then trying
+ to do this will likely cause U-Boot to hang. Say 'n' if you are unsure.
+
+config SEMIHOSTING_FALLBACK
+ bool "Recover gracefully when semihosting fails"
+ depends on SEMIHOSTING && RISCV
+ default y
+ help
+ Normally, if U-Boot makes a semihosting call and no debugger is
+ attached, then it will panic due to a synchronous abort
+ exception. This config adds an exception handler which will allow
+ U-Boot to recover. Say 'y' if unsure.
+
+config SPL_SEMIHOSTING
+ bool "Support RISCV semihosting in SPL"
+ depends on SPL
+ select LIB_SEMIHOSTING
+ help
+ Semihosting is a method for a target to communicate with a host
+ debugger. It uses special instructions which the debugger will trap
+ on and interpret. This allows U-Boot to read/write files, print to
+ the console, and execute arbitrary commands on the host system.
+
+ Enabling this option will add support for reading and writing files
+ on the host system. If you don't have a debugger attached then trying
+ to do this will likely cause U-Boot to hang. Say 'n' if you are unsure.
+
+config SPL_SEMIHOSTING_FALLBACK
+ bool "Recover gracefully when semihosting fails in SPL"
+ depends on SPL_SEMIHOSTING && RISCV
+ default y
+ help
+ Normally, if U-Boot makes a semihosting call and no debugger is
+ attached, then it will panic due to a synchronous abort
+ exception. This config adds an exception handler which will allow
+ U-Boot to recover. Say 'y' if unsure.
+
endmenu
diff --git a/arch/riscv/include/asm/semihosting.h b/arch/riscv/include/asm/semihosting.h
new file mode 100644
index 0000000000..7042821e00
--- /dev/null
+++ b/arch/riscv/include/asm/semihosting.h
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2022 Ventana Micro Systems Inc.
+ */
+
+#ifndef __ASM_RISCV_SEMIHOSTING_H
+#define __ASM_RISCV_SEMIHOSTING_H
+
+long smh_trap(int sysnum, void *addr);
+
+#endif /* __ASM_RISCV_SEMIHOSTING_H */
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..bd7cd772b8 100644
--- a/arch/riscv/lib/interrupts.c
+++ b/arch/riscv/lib/interrupts.c
@@ -17,6 +17,7 @@
#include <asm/ptrace.h>
#include <asm/system.h>
#include <asm/encoding.h>
+#include <semihosting.h>
DECLARE_GLOBAL_DATA_PTR;
@@ -149,6 +150,16 @@ 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)) {
+ /* For semihosting fallback we simply skip the ebreak
+ * instruction.
+ */
+ 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..72ea9521dc
--- /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 ("\t.option push\n"
+ "\t.option norvc\n"
+ "\tj 1f\n"
+ "\t.align 4\n"
+ "\t1: slli zero, zero, 0x1f\n"
+ "\tebreak\n"
+ "\tsrai zero, zero, 7\n"
+ "\t.option pop\n"
+ : "+r" (ret) : "r" (param0) : "memory");
+
+ return ret;
+}
--
2.34.1
next prev parent reply other threads:[~2022-09-16 12:24 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-16 8:12 [PATCH v2 0/3] Add riscv semihosting support in u-boot Kautuk Consul
2022-09-16 8:12 ` [PATCH v2 1/3] lib: Add common semihosting library Kautuk Consul
2022-09-17 17:39 ` Sean Anderson
2022-09-19 11:14 ` Kautuk Consul
2022-09-16 8:12 ` Kautuk Consul [this message]
2022-09-16 8:12 ` [PATCH v2 3/3] board: qemu-riscv: enable semihosting Kautuk Consul
2022-09-16 9:08 ` [PATCH v2 0/3] Add riscv semihosting support in u-boot Pali Rohár
2022-09-16 9:10 ` Kautuk Consul
2022-09-16 9:12 ` Pali Rohár
2022-09-16 13:11 ` Sean Anderson
2022-09-16 14:32 ` Tom Rini
2022-09-17 17:24 ` Sean Anderson
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=20220916081233.1970135-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=mr.nuke.me@gmail.com \
--cc=pali@kernel.org \
--cc=patrick.delaunay@foss.st.com \
--cc=philippe.reynes@softathome.com \
--cc=rasmus.villemoes@prevas.dk \
--cc=rick@andestech.com \
--cc=sean.anderson@seco.com \
--cc=sjg@chromium.org \
--cc=sr@denx.de \
--cc=u-boot@lists.denx.de \
--cc=wqu@suse.com \
--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.