[PATCH v2 22/28] arm: smh: Add option to detect semihosting

Sean Anderson sean.anderson at seco.com
Thu Mar 10 21:50:52 CET 2022


These functions are intended to support detecting semihosting and
falling back gracefully to alternative implementations. The test starts
by making semihosting call. SYS_ISERROR is chosen because it should not
mutate any state. If this semihosting call results in an exception
(rather than being caught by the debugger), then the exception handler
should call disable_semihosting() and resume execution after the call.

Ideally, this would just be part of semihosting by default, and not a
separate config. However, to reduce space ARM SPL doesn't include
exception vectors by default. This means we can't detect if a
semihosting call failed unless we enable them. To avoid forcing them to
be enabled, we use a separate config option. It might also be possible
to try and detect whether a debugger has enabled (by reading HDE from
DSCR), but I wasn't able to figure out a way to do this from all ELs.

Signed-off-by: Sean Anderson <sean.anderson at seco.com>
---

Changes in v2:
- New

 arch/arm/Kconfig           | 21 +++++++++++++++++++++
 arch/arm/lib/semihosting.c | 21 +++++++++++++++++++++
 include/semihosting.h      | 30 ++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+)

diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index 209232aac2..5e050c278f 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -427,6 +427,27 @@ config SPL_SEMIHOSTING
 	  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
+	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_FALLBACK
+	bool "Recover gracefully when semihosting fails in SPL"
+	depends on SPL_SEMIHOSTING
+	select ARMV8_SPL_EXCEPTION_VECTORS
+	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 SYS_THUMB_BUILD
 	bool "Build U-Boot using the Thumb instruction set"
 	depends on !ARM64
diff --git a/arch/arm/lib/semihosting.c b/arch/arm/lib/semihosting.c
index 7595dbc4a9..bf33c9decb 100644
--- a/arch/arm/lib/semihosting.c
+++ b/arch/arm/lib/semihosting.c
@@ -20,6 +20,7 @@
 #define SYSWRITE	0x05
 #define SYSREAD		0x06
 #define SYSREADC	0x07
+#define SYSISERROR	0x08
 #define SYSSEEK		0x0A
 #define SYSFLEN		0x0C
 #define SYSERRNO	0x13
@@ -41,6 +42,26 @@ static noinline long smh_trap(unsigned int sysnum, void *addr)
 	return result;
 }
 
+#if CONFIG_IS_ENABLED(SEMIHOSTING_FALLBACK)
+static bool _semihosting_enabled = true;
+static bool try_semihosting = true;
+
+bool semihosting_enabled(void)
+{
+	if (try_semihosting) {
+		smh_trap(SYSISERROR, 0);
+		try_semihosting = false;
+	}
+
+	return _semihosting_enabled;
+}
+
+void disable_semihosting(void)
+{
+	_semihosting_enabled = false;
+}
+#endif
+
 /**
  * smh_errno() - Read the host's errno
  *
diff --git a/include/semihosting.h b/include/semihosting.h
index 6f3c29786c..9816233c50 100644
--- a/include/semihosting.h
+++ b/include/semihosting.h
@@ -6,6 +6,36 @@
 #ifndef _SEMIHOSTING_H
 #define _SEMIHOSTING_H
 
+#if CONFIG_IS_ENABLED(SEMIHOSTING_FALLBACK)
+/**
+ * semihosting_enabled() - Determine whether semihosting is supported
+ *
+ * Semihosting-based drivers should call this function before making other
+ * semihosting calls.
+ *
+ * Return: %true if a debugger is attached which supports semihosting, %false
+ *         otherwise
+ */
+bool semihosting_enabled(void);
+
+/**
+ * disable_semihosting() - Cause semihosting_enabled() to return false
+ *
+ * If U-Boot ever receives an unhandled exception caused by a semihosting trap,
+ * the trap handler should call this function.
+ */
+void disable_semihosting(void);
+#else
+static inline bool semihosting_enabled(void)
+{
+	return CONFIG_IS_ENABLED(SEMIHOSTING);
+}
+
+static inline void disable_semihosting(void)
+{
+}
+#endif
+
 /**
  * enum smh_open_mode - Numeric file modes for use with smh_open()
  * MODE_READ: 'r'
-- 
2.25.1



More information about the U-Boot mailing list