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 66980C54EE9 for ; Thu, 22 Sep 2022 17:00:23 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8A32984B6B; Thu, 22 Sep 2022 19:00:20 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=seco.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=seco.com header.i=@seco.com header.b="G5/2eHfM"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 97345849F4; Thu, 22 Sep 2022 19:00:18 +0200 (CEST) Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2067.outbound.protection.outlook.com [40.107.22.67]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 07C8E84B6B for ; Thu, 22 Sep 2022 19:00:14 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=seco.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sean.anderson@seco.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HFdYM1VHCho78yxw7Hf9/2cIjGJHe4iPPXglCAp7jZQSFVVugC4JnoNx7SYREmvgFKdaw20m408XsUOS006qMW/7F6VQZfy657U+zcaq9ZhlmR4stIpshLhQSkrNHXj9ha6blWWoeNDgulT0PLO0A9hMO6pFKRbaY9QxeRe5mi5CxBielEpI9W/EzpefHu715qb/0IeCiMlXGTOWhPoiw0EUe/1xB8lBj6tcwMCfSkYG4PZglREHiF5P8i9pykxRW2hCcZ1AEu/7ncBOxuxlFwlmJGFagh9dQq3Xh0JGquDv9ng3C/jFoDdPbYCRUQNgwg0pHh0hR4hwTvMyavDyaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=MdPAGLBtG6ilC8kvAyOgOnGbF093it8Rqjvk62is2to=; b=dNnm7JMisB7O+dacTWjf4ATUIjZ/KxI+WsBYOpnYOMsm/mGLOUPidrx2JmPLCcKVtmVyteOpjDbzLnbCV1s3EOYX4QuOLC3G2wOCDlQO9DIcfGyDShD23TWrUDntqiVEuCvy8IDN6Fvrda1vthh7j/p3UNlRXmX0S9HXX5xGWAzeuzbU/tnof7OPFhQ8c/aUB6UXBbrQOeMjhZasjO/DywIwQ1jDxjyvfsSP8ok1OADvQD47GfXkCY7rHlozvXBWH3IVwPkrnaraxeSMyfwj+aerkZ5ggm5PFoXDXy03Br6+ef3h4EAjnmLZsW0odb/uKMOBhtSBBXEJ561eiQvB/g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=seco.com; dmarc=pass action=none header.from=seco.com; dkim=pass header.d=seco.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=seco.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=MdPAGLBtG6ilC8kvAyOgOnGbF093it8Rqjvk62is2to=; b=G5/2eHfMDycD5s7dQbXB/eLGnWm7QnxIuvNRQtKpg23PAZD4a3b4ufhGkaIhWC/wdjLez9VL9M5OI6MI80Rqn28F/tAWcC3bx6GAgWyuVKGOgifh9e1peM6pOXQjMF5AUxnkW4Y6kGseOIHugxu1L32UPL0Qoc3hlS7Bl/oDmHYsTBmNnVl6FNqdGdj5Oqv2du8DSp4Zw8+9Bg2Lf7ZbMblbWiZWWzqVpgoHPVE5SzPk8k8/s910TBgIoysZXJi8D/puC5yFh/AqykofsLdZZkhCR2MONhDFzlrjPeqxKpZXUJ0DSo795jIe5dCV2o/437TIhP8AQBhAIWoiMUH9Ug== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=seco.com; Received: from DB7PR03MB4972.eurprd03.prod.outlook.com (2603:10a6:10:7d::22) by AS2PR03MB9609.eurprd03.prod.outlook.com (2603:10a6:20b:597::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5654.19; Thu, 22 Sep 2022 17:00:12 +0000 Received: from DB7PR03MB4972.eurprd03.prod.outlook.com ([fe80::204a:de22:b651:f86d]) by DB7PR03MB4972.eurprd03.prod.outlook.com ([fe80::204a:de22:b651:f86d%6]) with mapi id 15.20.5654.014; Thu, 22 Sep 2022 17:00:11 +0000 Subject: Re: [PATCH v4 1/3] lib: Add common semihosting library To: Kautuk Consul , Rayagonda Kokatanur , Rick Chen , Leo , Simon Glass , Heinrich Schuchardt , Ilias Apalodimas , Alexandru Gagniuc , Philippe Reynes , Rasmus Villemoes , Stefan Roese , Loic Poulain , Bin Meng Cc: u-boot@lists.denx.de References: <20220919114908.2780149-1-kconsul@ventanamicro.com> <20220919114908.2780149-2-kconsul@ventanamicro.com> From: Sean Anderson Message-ID: <2195194c-2cc4-8f49-9129-b4fd5c8d68bf@seco.com> Date: Thu, 22 Sep 2022 13:00:07 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 In-Reply-To: <20220919114908.2780149-2-kconsul@ventanamicro.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-ClientProxiedBy: BL0PR05CA0018.namprd05.prod.outlook.com (2603:10b6:208:91::28) To DB7PR03MB4972.eurprd03.prod.outlook.com (2603:10a6:10:7d::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DB7PR03MB4972:EE_|AS2PR03MB9609:EE_ X-MS-Office365-Filtering-Correlation-Id: 0f9cb855-b06a-481c-bcbb-08da9cbbe986 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: b4RI1UgMIwMaZEnonorVTcz5qxtkwYd1v2WtDJWq8rZy82DTq4KfkwNdl1zHgPqeV0mzcE9rvSl+sTFK+ja/Y9YlHne9xWGGFWijs6FIcvM2b2xvyoFPzsWzkBms6XZBpE2d44oCoqenldMsyvV+btLJmKTvQSBLsUtRgKqSEqw8MNRSpRPIJ8/ENoGOkZ+bP6OkBZnHG6gwuDaiijHzJJowhPKWaP7s882uFxhjRYQzd8QRRTWlfJTx0rmG7jweg/XAGVV/Nzo2jEvw1sBmh8/FL5uzzrHd7z2ORD71WJVycSt2dWL3ejp99px6KcpK54X6vTkxGTT1Hux17n6cg4uq3RxbWlTjHV3ZKZn8GFLtsjW32QDcv+3YHLFN5YoPaNaZwxpz3deqJtevuO9jiGJxcG1hjbFsBGcAAAyteCMcDQlv9cvCVXWrxieyi0B305vpAHhEbnZWntoZ1llmpc7+Rctstx7MBIgnzWo2FMeeerx16Ad7lE6BRm4FnCROH02damdC92pcmB76U7J9Iu5BnllM/Wl9fs/vFHq5mTriqeav3RUQBzOtisOCm6tEvknIMWCPJwMbAhdpjUwZlh8qxlMvDwCF0avOpZCNgDZUEiF+gvp8ewYTnOm8QhlpUTKMlvdhAsPgjJRu0qE1npoUReqq2HOCWg/ivwUe+fuho1HU6GDHlq/PzZUPaOx9e69P6izeM+7B1G7RzIqo8oN0ISpXal+gV8dKMRPPXDc3H3yozi0lgWqYHIuVEqtQ4YgIMD1Z9TcMbVgKDtlR/DREY/CAIMlqRV5dpQM2rv7LNcrdroQZ1jxwKp8f5DMn/GdQtqCaQU32QUeZbXKar0rYVHluhiRLkrXWHlM717Y= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DB7PR03MB4972.eurprd03.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(136003)(346002)(396003)(39850400004)(376002)(366004)(451199015)(30864003)(52116002)(53546011)(2616005)(6506007)(6486002)(36756003)(83380400001)(7416002)(6512007)(186003)(26005)(5660300002)(921005)(31696002)(38350700002)(38100700002)(8676002)(4326008)(66946007)(66556008)(66476007)(86362001)(478600001)(316002)(8936002)(110136005)(41300700001)(6666004)(31686004)(2906002)(44832011)(45980500001)(43740500002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?VkdNYzA4bmNtRGVnOU00S2RmU1J3Q3VZK1FsNlgxc3FhQ2FmUTJQK1czN2dD?= =?utf-8?B?VFQycnVtcTF4OXk1NmY3b29GZVdFQzg1WE12R0tFR2k3ZFJoZms1ZGg5djRi?= =?utf-8?B?MnBnZnNzNmY5K3pkaTRjTVpjWVE3YkdONy85RWlpdjFkMW11S0FIUzlBdUdu?= =?utf-8?B?ZUIyaFdQN3g0WFJWajZCUEs5N1J6TFp5bE51RHJvbzYvVDRzQ21yL1JHclRj?= =?utf-8?B?NzVvVm52RkdoaVRnZms1czgwSW1KWjI0bytJdWp2TmtnUkFxVDhUVHlZeURF?= =?utf-8?B?dTg3NUFLN3pCNU9xV1BQTVpJbDRqblhQZWpWZzFoZkIrcVcxRkYwc0FkNFRI?= =?utf-8?B?WXg4TW03QlJCc0kxUVNKK285NFY3cGtqUnRvZ1hUbG9RWVZJK3hJVGFpdnJu?= =?utf-8?B?aXNiTUVkSGE3TnZ1Vm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-Message-Id: 0f9cb855-b06a-481c-bcbb-08da9cbbe986 X-MS-Exchange-CrossTenant-AuthSource: DB7PR03MB4972.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Sep 2022 17:00:11.8491 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bebe97c3-6438-442e-ade3-ff17aa50e733 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: ocM5w56VG1de+4PiMxwHGR8aSTr2SGnz6zSBFKp7niqkJZrCtAzMcM7eQSRppudSJO1056Hie61WIoIXJh3Agw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS2PR03MB9609 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 On 9/19/22 7:49 AM, Kautuk Consul wrote: > We factor out the arch-independent parts of the ARM semihosting > implementation as a common library so that it can be shared > with RISC-V. > > Signed-off-by: Kautuk Consul > --- > arch/arm/Kconfig | 46 --------- > arch/arm/lib/semihosting.c | 181 +----------------------------------- > include/semihosting.h | 11 +++ > lib/Kconfig | 46 +++++++++ > lib/Makefile | 2 + > lib/semihosting.c | 186 +++++++++++++++++++++++++++++++++++++ > 6 files changed, 246 insertions(+), 226 deletions(-) > create mode 100644 lib/semihosting.c > > diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig > index 82cd456f51..ee6a9fadd9 100644 > --- a/arch/arm/Kconfig > +++ b/arch/arm/Kconfig > @@ -413,52 +413,6 @@ config ARM_SMCCC > This should be enabled if U-Boot needs to communicate with system > firmware (for example, PSCI) according to SMCCC. > > -config SEMIHOSTING > - bool "Support ARM 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 && ARM64 > - 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 ARM semihosting in SPL" > - depends on SPL > - 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 && ARM64 > - 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 01d652a6b8..11e7b85ee6 100644 > --- a/arch/arm/lib/semihosting.c > +++ b/arch/arm/lib/semihosting.c > @@ -10,25 +10,11 @@ > * available in silicon now, fastmodel usage makes less sense for them. > */ > #include > -#include > -#include > - > -#define SYSOPEN 0x01 > -#define SYSCLOSE 0x02 > -#define SYSWRITEC 0x03 > -#define SYSWRITE0 0x04 > -#define SYSWRITE 0x05 > -#define SYSREAD 0x06 > -#define SYSREADC 0x07 > -#define SYSISERROR 0x08 > -#define SYSSEEK 0x0A > -#define SYSFLEN 0x0C > -#define SYSERRNO 0x13 > > /* > * Call the handler > */ > -static noinline long smh_trap(unsigned int sysnum, void *addr) > +long smh_trap(unsigned int sysnum, void *addr) > { > register long result asm("r0"); > #if defined(CONFIG_ARM64) > @@ -41,168 +27,3 @@ static noinline long smh_trap(unsigned int sysnum, void *addr) > #endif > 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(SYSERRNO, NULL); > - try_semihosting = false; > - } > - > - return _semihosting_enabled; > -} > - > -void disable_semihosting(void) > -{ > - _semihosting_enabled = false; > -} > -#endif > - > -/** > - * smh_errno() - Read the host's errno > - * > - * This gets the value of the host's errno and negates it. The host's errno may > - * or may not be set, so only call this function if a previous semihosting call > - * has failed. > - * > - * Return: a negative error value > - */ > -static int smh_errno(void) > -{ > - long ret = smh_trap(SYSERRNO, NULL); > - > - if (ret > 0 && ret < INT_MAX) > - return -ret; > - return -EIO; > -} > - > -long smh_open(const char *fname, enum smh_open_mode mode) > -{ > - long fd; > - struct smh_open_s { > - const char *fname; > - unsigned long mode; > - size_t len; > - } open; > - > - debug("%s: file \'%s\', mode \'%u\'\n", __func__, fname, mode); > - > - open.fname = fname; > - open.len = strlen(fname); > - open.mode = mode; > - > - /* Open the file on the host */ > - fd = smh_trap(SYSOPEN, &open); > - if (fd == -1) > - return smh_errno(); > - return fd; > -} > - > -/** > - * struct smg_rdwr_s - Arguments for read and write > - * @fd: A file descriptor returned from smh_open() > - * @memp: Pointer to a buffer of memory of at least @len bytes > - * @len: The number of bytes to read or write > - */ > -struct smh_rdwr_s { > - long fd; > - void *memp; > - size_t len; > -}; > - > -long smh_read(long fd, void *memp, size_t len) > -{ > - long ret; > - struct smh_rdwr_s read; > - > - debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len); > - > - read.fd = fd; > - read.memp = memp; > - read.len = len; > - > - ret = smh_trap(SYSREAD, &read); > - if (ret < 0) > - return smh_errno(); > - return len - ret; > -} > - > -long smh_write(long fd, const void *memp, size_t len, ulong *written) > -{ > - long ret; > - struct smh_rdwr_s write; > - > - debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len); > - > - write.fd = fd; > - write.memp = (void *)memp; > - write.len = len; > - > - ret = smh_trap(SYSWRITE, &write); > - *written = len - ret; > - if (ret) > - return smh_errno(); > - return 0; > -} > - > -long smh_close(long fd) > -{ > - long ret; > - > - debug("%s: fd %ld\n", __func__, fd); > - > - ret = smh_trap(SYSCLOSE, &fd); > - if (ret == -1) > - return smh_errno(); > - return 0; > -} > - > -long smh_flen(long fd) > -{ > - long ret; > - > - debug("%s: fd %ld\n", __func__, fd); > - > - ret = smh_trap(SYSFLEN, &fd); > - if (ret == -1) > - return smh_errno(); > - return ret; > -} > - > -long smh_seek(long fd, long pos) > -{ > - long ret; > - struct smh_seek_s { > - long fd; > - long pos; > - } seek; > - > - debug("%s: fd %ld pos %ld\n", __func__, fd, pos); > - > - seek.fd = fd; > - seek.pos = pos; > - > - ret = smh_trap(SYSSEEK, &seek); > - if (ret) > - return smh_errno(); > - return 0; > -} > - > -int smh_getc(void) > -{ > - return smh_trap(SYSREADC, NULL); > -} > - > -void smh_putc(char ch) > -{ > - smh_trap(SYSWRITEC, &ch); > -} > - > -void smh_puts(const char *s) > -{ > - smh_trap(SYSWRITE0, (char *)s); > -} > diff --git a/include/semihosting.h b/include/semihosting.h > index f1f73464e4..4e844cbad8 100644 > --- a/include/semihosting.h > +++ b/include/semihosting.h > @@ -17,6 +17,17 @@ > #define SMH_T32_SVC 0xDFAB > #define SMH_T32_HLT 0xBABC > > +/** > + * smh_trap() - ARCH-specific semihosting call. > + * > + * Semihosting library/driver can use this function to do the > + * actual semihosting calls. > + * > + * Return: Error code defined by semihosting spec. > + */ > + > +long smh_trap(unsigned int sysnum, void *addr); > + > #if CONFIG_IS_ENABLED(SEMIHOSTING_FALLBACK) > /** > * semihosting_enabled() - Determine whether semihosting is supported > diff --git a/lib/Kconfig b/lib/Kconfig > index 6121c80dc8..97920e7552 100644 > --- a/lib/Kconfig > +++ b/lib/Kconfig > @@ -71,6 +71,52 @@ config HAVE_PRIVATE_LIBGCC > config LIB_UUID > bool > > +config SEMIHOSTING > + bool "Support semihosting" Should probably depend on ARM (and in the next patch RISCV). > + 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 && ARM64 > + 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 semihosting in SPL" > + depends on SPL ditto > + 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 && ARM64 > + 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 PRINTF > bool > default y > diff --git a/lib/Makefile b/lib/Makefile > index e3deb15287..134c4319cd 100644 > --- a/lib/Makefile > +++ b/lib/Makefile > @@ -145,6 +145,8 @@ obj-y += date.o > obj-y += rtc-lib.o > obj-$(CONFIG_LIB_ELF) += elf.o > > +obj-$(CONFIG_$(SPL_TPL_)SEMIHOSTING) += semihosting.o > + > # > # Build a fast OID lookup registry from include/linux/oid_registry.h > # > diff --git a/lib/semihosting.c b/lib/semihosting.c > new file mode 100644 > index 0000000000..831774e356 > --- /dev/null > +++ b/lib/semihosting.c > @@ -0,0 +1,186 @@ > +// SPDX-License-Identifier: GPL-2.0+ > +/* > + * Copyright (C) 2022 Sean Anderson > + * Copyright 2014 Broadcom Corporation > + */ > + > +#include > +#include > +#include > + > +#define SYSOPEN 0x01 > +#define SYSCLOSE 0x02 > +#define SYSWRITEC 0x03 > +#define SYSWRITE0 0x04 > +#define SYSWRITE 0x05 > +#define SYSREAD 0x06 > +#define SYSREADC 0x07 > +#define SYSISERROR 0x08 > +#define SYSSEEK 0x0A > +#define SYSFLEN 0x0C > +#define SYSERRNO 0x13 > + > +#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(SYSERRNO, NULL); > + try_semihosting = false; > + } > + > + return _semihosting_enabled; > +} > + > +void disable_semihosting(void) > +{ > + _semihosting_enabled = false; > +} > +#endif > + > +/** > + * smh_errno() - Read the host's errno > + * > + * This gets the value of the host's errno and negates it. The host's errno may > + * or may not be set, so only call this function if a previous semihosting call > + * has failed. > + * > + * Return: a negative error value > + */ > +static int smh_errno(void) > +{ > + long ret = smh_trap(SYSERRNO, NULL); > + > + if (ret > 0 && ret < INT_MAX) > + return -ret; > + return -EIO; > +} > + > +long smh_open(const char *fname, enum smh_open_mode mode) > +{ > + long fd; > + struct smh_open_s { > + const char *fname; > + unsigned long mode; > + size_t len; > + } open; > + > + debug("%s: file \'%s\', mode \'%u\'\n", __func__, fname, mode); > + > + open.fname = fname; > + open.len = strlen(fname); > + open.mode = mode; > + > + /* Open the file on the host */ > + fd = smh_trap(SYSOPEN, &open); > + if (fd == -1) > + return smh_errno(); > + return fd; > +} > + > +/** > + * struct smg_rdwr_s - Arguments for read and write > + * @fd: A file descriptor returned from smh_open() > + * @memp: Pointer to a buffer of memory of at least @len bytes > + * @len: The number of bytes to read or write > + */ > +struct smh_rdwr_s { > + long fd; > + void *memp; > + size_t len; > +}; > + > +long smh_read(long fd, void *memp, size_t len) > +{ > + long ret; > + struct smh_rdwr_s read; > + > + debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len); > + > + read.fd = fd; > + read.memp = memp; > + read.len = len; > + > + ret = smh_trap(SYSREAD, &read); > + if (ret < 0) > + return smh_errno(); > + return len - ret; > +} > + > +long smh_write(long fd, const void *memp, size_t len, ulong *written) > +{ > + long ret; > + struct smh_rdwr_s write; > + > + debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len); > + > + write.fd = fd; > + write.memp = (void *)memp; > + write.len = len; > + > + ret = smh_trap(SYSWRITE, &write); > + *written = len - ret; > + if (ret) > + return smh_errno(); > + return 0; > +} > + > +long smh_close(long fd) > +{ > + long ret; > + > + debug("%s: fd %ld\n", __func__, fd); > + > + ret = smh_trap(SYSCLOSE, &fd); > + if (ret == -1) > + return smh_errno(); > + return 0; > +} > + > +long smh_flen(long fd) > +{ > + long ret; > + > + debug("%s: fd %ld\n", __func__, fd); > + > + ret = smh_trap(SYSFLEN, &fd); > + if (ret == -1) > + return smh_errno(); > + return ret; > +} > + > +long smh_seek(long fd, long pos) > +{ > + long ret; > + struct smh_seek_s { > + long fd; > + long pos; > + } seek; > + > + debug("%s: fd %ld pos %ld\n", __func__, fd, pos); > + > + seek.fd = fd; > + seek.pos = pos; > + > + ret = smh_trap(SYSSEEK, &seek); > + if (ret) > + return smh_errno(); > + return 0; > +} > + > +int smh_getc(void) > +{ > + return smh_trap(SYSREADC, NULL); > +} > + > +void smh_putc(char ch) > +{ > + smh_trap(SYSWRITEC, &ch); > +} > + > +void smh_puts(const char *s) > +{ > + smh_trap(SYSWRITE0, (char *)s); > +} > Other than that, Reviewed-by: Sean Anderson