mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 5/6] ARM: semihosting: add DEBUG_LL implementation
Date: Tue, 11 Jun 2024 08:59:22 +0200	[thread overview]
Message-ID: <20240611065923.2900168-6-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20240611065923.2900168-1-a.fatoum@pengutronix.de>

Both OpenOCD and QEMU have a semihosting implementation that provides a
console suitable for low level debugging. Add a DEBUG_LL implementation
that is usable when QEMU is called with

  --semihosting-config chardev=serial0

or following OpenOCD is executed:

  arm semihosting enable

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/arm/include/asm/debug_ll.h    |  4 ++++
 arch/arm/include/asm/semihosting.h | 32 ++++++++++++++++++++++++++++++
 common/Kconfig.debug_ll            | 13 ++++++++++++
 drivers/firmware/Kconfig           |  1 +
 include/debug_ll/semihosting.h     | 15 ++++++++++++++
 5 files changed, 65 insertions(+)
 create mode 100644 include/debug_ll/semihosting.h

diff --git a/arch/arm/include/asm/debug_ll.h b/arch/arm/include/asm/debug_ll.h
index 999f1cb831a8..bac0b2106936 100644
--- a/arch/arm/include/asm/debug_ll.h
+++ b/arch/arm/include/asm/debug_ll.h
@@ -35,6 +35,10 @@
 #include <mach/layerscape/debug_ll.h>
 #endif
 
+#ifdef CONFIG_DEBUG_SEMIHOSTING
+#include <debug_ll/semihosting.h>
+#endif
+
 #ifdef CONFIG_DEBUG_QEMU_ARM64_VIRT
 #define DEBUG_LL_UART_ADDR		0x9000000
 #include <debug_ll/pl011.h>
diff --git a/arch/arm/include/asm/semihosting.h b/arch/arm/include/asm/semihosting.h
index c18aa1a5fef5..2219e858d040 100644
--- a/arch/arm/include/asm/semihosting.h
+++ b/arch/arm/include/asm/semihosting.h
@@ -4,5 +4,37 @@
 #define __ASM_ARM_SEMIHOSTING_H
 
 #include <asm-generic/semihosting.h>
+#include <asm/unified.h>
+
+static inline void semihosting_putc(void *unused, int c)
+{
+	/* We may not be relocated yet here, so we push
+	 * to stack and take address of that
+	 */
+#ifdef CONFIG_CPU_64
+	asm volatile (
+		"stp %0, xzr, [sp, #-16]!\n"
+		"mov x1, sp\n"
+		"mov x0, #0x03\n"
+	 	"hlt #0xf000\n"
+		"add sp, sp, #16\n"
+		: /* No outputs */
+		: "r" ((long)c)
+		: "x0", "x1", "x2", "x3", "x12", "memory"
+	);
+#else
+	asm volatile (
+		"push {%0}\n"
+		"mov r1, sp\n"
+		"mov r0, #0x03\n"
+	 ARM(	"bkpt #0x123456\n")
+	 THUMB(	"bkpt #0xAB\n")
+		"pop {%0}\n"
+		: /* No outputs */
+		: "r" (c)
+		: "r0", "r1", "r2", "r3", "r12", "memory"
+	);
+#endif
+}
 
 #endif
diff --git a/common/Kconfig.debug_ll b/common/Kconfig.debug_ll
index 472048177f8b..3f6f3e7c3bdb 100644
--- a/common/Kconfig.debug_ll
+++ b/common/Kconfig.debug_ll
@@ -337,6 +337,19 @@ config DEBUG_QEMU_ARM64_VIRT
 	bool "QEMU ARM64 Virt PL011 console"
 	depends on ARCH_ARM64_VIRT
 
+config DEBUG_SEMIHOSTING
+	bool "Semihosting console"
+	depends on SEMIHOSTING
+	help
+	  Semihosting enables code to use the I/O facilities on a
+	  host debugger/emulator through a simple supervisor call.
+	  The host debugger or emulator must have semihosting enabled
+	  for the supervisor call to be trapped, otherwise barebox
+	  will crash.
+
+	  Say Y here if you want low-level debugging support
+	  using semihosting writec.
+
 endchoice
 
 config DEBUG_LL_NS16550
diff --git a/drivers/firmware/Kconfig b/drivers/firmware/Kconfig
index 89c8bdeda3f0..264f7b2a5a65 100644
--- a/drivers/firmware/Kconfig
+++ b/drivers/firmware/Kconfig
@@ -3,6 +3,7 @@ menu "Firmware Drivers"
 
 config SEMIHOSTING
 	bool
+	select HAS_DEBUG_LL
 
 config FIRMWARE_ALTERA_SERIAL
 	bool "Altera SPI programming"
diff --git a/include/debug_ll/semihosting.h b/include/debug_ll/semihosting.h
new file mode 100644
index 000000000000..125d42ee88de
--- /dev/null
+++ b/include/debug_ll/semihosting.h
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __DEBUG_LL_SEMIHOSTING_H
+#define __DEBUG_LL_SEMIHOSTING_H
+
+#include <asm/semihosting.h>
+#include <linux/stddef.h>
+
+#ifdef CONFIG_DEBUG_SEMIHOSTING
+static inline void PUTC_LL(char c)
+{
+	semihosting_putc(NULL, c);
+}
+#endif
+
+#endif /* __DEBUG_LL_ARM_SEMIHOSTING_H */
-- 
2.39.2




  parent reply	other threads:[~2024-06-11  7:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-11  6:59 [PATCH 0/6] semihosting: extend support for ARM64, console Ahmad Fatoum
2024-06-11  6:59 ` [PATCH 1/6] ARM: lib32: semihosting: prepare for more general use Ahmad Fatoum
2024-06-11  6:59 ` [PATCH 2/6] firmware: semihosting: add ARMv8-A semihosting support Ahmad Fatoum
2024-06-11  6:59 ` [PATCH 3/6] firmware: semihosting: don't return error code from writec/write0 Ahmad Fatoum
2024-06-11  6:59 ` [PATCH 4/6] firmware: semihosting: translate return values in wrappers Ahmad Fatoum
2024-06-11  6:59 ` Ahmad Fatoum [this message]
2024-06-11  6:59 ` [PATCH 6/6] serial: add semihosting console Ahmad Fatoum
2024-06-13  7:18 ` [PATCH 0/6] semihosting: extend support for ARM64, console Sascha Hauer

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=20240611065923.2900168-6-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox