From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 32EA0C4708E for ; Wed, 7 Dec 2022 11:43:25 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 7B53F855B4; Wed, 7 Dec 2022 12:43:19 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=ventanamicro.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=ventanamicro.com header.i=@ventanamicro.com header.b="blN80NTk"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B6B9E8559C; Wed, 7 Dec 2022 12:43:16 +0100 (CET) Received: from mail-oi1-x231.google.com (mail-oi1-x231.google.com [IPv6:2607:f8b0:4864:20::231]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 73380855B4 for ; Wed, 7 Dec 2022 12:43:09 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=ventanamicro.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kconsul@ventanamicro.com Received: by mail-oi1-x231.google.com with SMTP id s187so14209937oie.10 for ; Wed, 07 Dec 2022 03:43:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pAWPI3dIcqyIFsAM20VqQl1M9/DxgYizw5ue22X38fg=; b=blN80NTk2L9tslVuOIOweNLmAhxCOZLLjEf5kf0VK0+ixt7HgJvMm5qH2pndb9Hk9U 0SXXG/6em7qV+DAfHoIXzPhNdXWAqgBo6PUH5u/i73aBHbcYni7MDAuw/jhjf/zZVjF+ IbkMwNibRksC/UXGkCuHCkJC6i3ADHqPJP7V1ZBetnP7cThm1s+zzuG9NrSd6nI9/Uy4 DssDgHIdv37op3rrvQnaBkfO17AMcsl317dtR8R7XLTicslFd+LSS9KISzdbhWyTlGV5 s5avVaZ4DcBVFoUJByULYJ00g4Zg3Dz0odLuQ1NUtZQEap0AhYH7JSWOTEe1L6NlXO+R ryJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pAWPI3dIcqyIFsAM20VqQl1M9/DxgYizw5ue22X38fg=; b=sDIvO0tpWehut35/x6MyqKbcwADOprBts12mz/sVPYjzn2aWvHo57VF2D3ODp1maDh JRhuCEBQEL0p/acbrvC+U71NVZgP7GtQhL65+d556ZyAIrBnV9R6qLP3T5salLYKO+jJ FYi0ZL59i8gHQKC4BnaUP9f0qztWWQrZKrjC8qW7n3tdZZkrL0v6h+ZPuLlp8KH6jK1x aMtn/ze3Y2dLwuG2QGr/Uz5eaPfOCz0K0MTmHWjFENTHr0Lko1/wGa5BkucvjuQvTujb GJg230WP6cGUyX5MKanunG5d+V9NhJq+1oOFQsLlIN8I2Ymv13FqazPD+DQVk8ZbAo6h r66w== X-Gm-Message-State: ANoB5pnN3Qmc+xK3HNYy8gbyRu83kxDT10uKVMJY3uXMk/88Hyie6sqZ GqyoE7oishlVFSVUyJEGKp9Ivg== X-Google-Smtp-Source: AA0mqf4Yf10HO3B/1Dmzbi4QG7MOobbx2pEJEif6E2YHbQ3oZiTfY+Bcy6vHg8X//nZZFpw/R4tj6g== X-Received: by 2002:aca:2b0d:0:b0:35b:c865:9f25 with SMTP id i13-20020aca2b0d000000b0035bc8659f25mr17407288oik.19.1670413387769; Wed, 07 Dec 2022 03:43:07 -0800 (PST) Received: from performance-PowerEdge-T440.. ([103.97.165.210]) by smtp.googlemail.com with ESMTPSA id o31-20020a056871079f00b00143065d3e99sm12082302oap.5.2022.12.07.03.43.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Dec 2022 03:43:07 -0800 (PST) From: Kautuk Consul To: Bharat Gooty , Rayagonda Kokatanur , Sean Anderson , Andre Przywara , Simon Glass , Ilias Apalodimas , Philippe Reynes , Sughosh Ganu , Heinrich Schuchardt , Rasmus Villemoes , Eugen Hristev , =?UTF-8?q?Pali=20Roh=C3=A1r?= , Stefan Roese , Minkyu Kang , Loic Poulain , u-boot@lists.denx.de, ycliang@andestech.com, bmeng.cn@gmail.com, rickchen36@gmail.com Cc: Kautuk Consul , Anup Patel Subject: [PATCH v6 2/3] arch/riscv: add semihosting support for RISC-V Date: Wed, 7 Dec 2022 17:12:35 +0530 Message-Id: <20221207114236.2906956-3-kconsul@ventanamicro.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221207114236.2906956-1-kconsul@ventanamicro.com> References: <20221207114236.2906956-1-kconsul@ventanamicro.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean 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 Signed-off-by: Kautuk Consul --- 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 d6a8ae9728..e5a81ba722 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 */ +#include #include #include #include @@ -17,6 +18,7 @@ #include #include #include +#include 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 + +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 b8833e0183..3c5a4ab386 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