From patchwork Tue Mar 22 20:59:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Anderson X-Patchwork-Id: 1608357 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=seco.com header.i=@seco.com header.a=rsa-sha256 header.s=selector1 header.b=rSepaEp/; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KNP8z4Flhz9s1l for ; Wed, 23 Mar 2022 08:03:31 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0CF2183F4F; Tue, 22 Mar 2022 22:02:28 +0100 (CET) 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="rSepaEp/"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 65D3283BF7; Tue, 22 Mar 2022 22:00:58 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from EUR02-AM5-obe.outbound.protection.outlook.com (mail-am5eur02on061b.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe07::61b]) (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 5A3F583BC1 for ; Tue, 22 Mar 2022 22:00:33 +0100 (CET) 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=CEZ5sdSkCtl9pof2Zkd52YjjHnQWF1H/cgxtD1Dtxol/aL8bJR1ya+1grkv5E8qrtom7wq1RqJ4EkCsda0FpmYWPewZQyGyiqQ857ppofT1rQXgAAzmOt1YfrOhiVUYh4WnLu6uRy1fr/zDopMqQr1+q81R60GWeIInNFZnMY5+ZvXfr+h7EkKzaCnMnDJo+4+Jtf/aq5r+ZVHtoWtvwYmu/fc6rMoJJP2LBsItPnfay13HX7GhB+OLBIl7T/cQeFCey/6PUCOt/6QX6VVpiiaRB1kydd6mRXku8qOn3qpi7ya2EOxZPgwY7uguGoy8ulNsVZtHws/UBfU+JJHQj1A== 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=v4B35fhDzWs8MhBVzbUKLcZtx72d5J5domOPTRAPUyQ=; b=XfcmzcxnLXT4lz7ruRF0/+qCCZSg6RttazWC84kFNCwf7rChUZ7i5JA1lkWKIC1mJvq317DoHLRSIYQEACT3aqsEe7WSJNItdilHIo2cblGSAfkxC+fvI3eqLgB1hMbFhEuvHiS0Zb6VE0pfFmP2D0SHrXW/13lWr2RpVmP3/LfZClsY1844Pljk0nFU0Ul/gSkxRyCjAzJL6rzQce+MoPE8AOsRItUfmjmTlcWeoBctOQHedaavJgvfJC4wY8OvwG35eQBmk7m/BumqEkSvIIFjHU/nlhXhsfIDrVfADL/kQodO85D4WyP8ug/oIldRVB5g5hDekDlk413DUFJQow== 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=v4B35fhDzWs8MhBVzbUKLcZtx72d5J5domOPTRAPUyQ=; b=rSepaEp/5Z9cfm6Zv8/GttY1QcNUqtAbYoU8FIlNOKQHRyZz1RX5ShGWQJrR+GJ5/+k9SZ2Di7igdc9j/oMoExeMHIVkKxObsUeZrQUklhMcVR/YIChcDQQre+Kfko0vNN4a7V0ZDKnBm254T8ENk/zLp9OlLSjC54k7MTAqHuwIxgcDGwlz8K8HEBbBJO4VlcrZ8PVazfnlSk62N9aLhIqTwdvVGsp1oG25ZVlw6a2t54ny+88J0CWgWf9r5rPMKQF0bZolh9maFQ7jGmyn0p/f9F4QDixPtBQh6KCNCGK80lbv095rsNaq8HV6VPn0JWqJzf5n9/nRSL6OKD54Dg== 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 VI1PR03MB4591.eurprd03.prod.outlook.com (2603:10a6:803:5f::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5081.23; Tue, 22 Mar 2022 21:00:32 +0000 Received: from DB7PR03MB4972.eurprd03.prod.outlook.com ([fe80::2414:8ad5:9fd6:3bff]) by DB7PR03MB4972.eurprd03.prod.outlook.com ([fe80::2414:8ad5:9fd6:3bff%5]) with mapi id 15.20.5081.023; Tue, 22 Mar 2022 21:00:32 +0000 From: Sean Anderson To: Tom Rini Cc: Linus Walleij , Liviu Dudau , Andre Przywara , Simon Glass , u-boot@lists.denx.de, Sean Anderson Subject: [PATCH v3 22/29] arm: smh: Add option to detect semihosting Date: Tue, 22 Mar 2022 16:59:30 -0400 Message-Id: <20220322205938.1721846-23-sean.anderson@seco.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220322205938.1721846-1-sean.anderson@seco.com> References: <20220322205938.1721846-1-sean.anderson@seco.com> X-ClientProxiedBy: MN2PR20CA0012.namprd20.prod.outlook.com (2603:10b6:208:e8::25) To DB7PR03MB4972.eurprd03.prod.outlook.com (2603:10a6:10:7d::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 727447c5-f097-4cd2-0f1a-08da0c47009e X-MS-TrafficTypeDiagnostic: VI1PR03MB4591:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Xk8uhBC8OnqoKPGiusRLp3RRM3dIvMoVKVQhqWOH58lwLe4lEyvzIhxDgc1q1ppna2V4zqDkBx7tIrtnF01iMiq8hwYsvtutF7ylxElfvH9tIW6f8DMU1jn1wHVgq0eKx9A1s82cltbXtqorNiWAclM5v4Yt5Vw/1ptKD1dQ7jt+VNTqJp8xbgfjCS3nTaRJy6jBcFbwvpHraQh0p5WD+/vtYFtGXxp7ScL/3V0A5V4D8U58//6l1N4VEDnW3C+yb0Py2KEpzcyrXAk9aHmlDI8kenHQt9BcDp5s/C2pgL53Tce6np7t40yFEahrXNgVXhGJmEmgkeJ+mCp36xLasu8H9a6Unmj4mDvLjB/X09XFxKerNc8JR9Svz86XsZ9BvmYHPUWiW8Pzpkt6E3V53YWgSX+ryY7C/bbJqQWrQ2ZNb5ZOokZ9BOxjCht9XPAfhF8/cvFhIcimHufIP2JU8JXOKrkxScxUTqhk0ojAEuRq8ADvFAqxa/FIXLsWaUwk0Y0ps9cyL0G2b/w4GqeRA1Vs+SWMWxZlIYk5JQUP5B+qA1SNUCaPySUVnGhTbvDfHfygJsKwmB0bOsDe4ARclNwcXIBjzEu1G4WMj1NfVZl/7DwNgn/V8hv2hu3QFz9HxvT9EIZtoYBaWKzkUarLH0I9dIfy64ramxqtsYb08i4hV+CADN/dvcgLZRsv7bZH0CgMwgpe3lr7lYTTCQjVdA== 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:(13230001)(366004)(2906002)(83380400001)(6486002)(186003)(26005)(107886003)(38100700002)(38350700002)(2616005)(1076003)(6506007)(6512007)(6916009)(36756003)(52116002)(54906003)(316002)(4326008)(5660300002)(8676002)(66556008)(66476007)(508600001)(86362001)(66946007)(8936002)(44832011); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: V21UiQNMjcvgn8YqqPlaFtaPHEPGaRDbuYSt6qnoAFDtyWtfvXlFhn9VY4KT/CLB42qGEbA8EbJE09a98koz1+iP9aYDT7Yr0SX7N5pXeIMrl0vr6bG4h1/S+bBjPYGejck0JBGn8k1vYa1Sf3oN+1NcdqF+7MEE+kwSWikh5v06dTs1pWiv/mu4ieq+XKrWeceje/LiORdhHz82V2GM49D0Mq2Zv9enBpy4EeIjzbe9k2+YU3GshmUB9trOAydUkZfpPkYygvTN3qPsRVKJTqo++fMgRsNpNGOEspVLL9QE/9XZLSm+rNOKm09NVVuQeqxub8ZFPUMgY5t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eCQvJBT2k8nQEB3cI/gK/rWY03/OgbaM8suW9pjD/eKAX0OGVjLQ1dDIgx9UuWUQ/hChZ23OALtgvEi9hkLv5jObEXRiCw== X-OriginatorOrg: seco.com X-MS-Exchange-CrossTenant-Network-Message-Id: 727447c5-f097-4cd2-0f1a-08da0c47009e X-MS-Exchange-CrossTenant-AuthSource: DB7PR03MB4972.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Mar 2022 21:00:32.0478 (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: A1Ht4w3FWaUp8F11a64PmDlgX0pm4tRD0JpyeGWDaB24JRTYVzNgCRhzIX3Td2Ka3hHh+lEW+Ns9QcXrpp7N5A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR03MB4591 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.5 at phobos.denx.de X-Virus-Status: Clean These functions are intended to support detecting semihosting and falling back gracefully to alternative implementations. The test starts by making semihosting call. SYS_ERRNO 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. This patch just introduces the generic code to handle detection. The next patch will implement it for arm64 (but not arm32). Signed-off-by: Sean Anderson --- Changes in v3: - Make FALLBACK depend on ARM64 - Reorder Kconfigs to group SEMIHOSTING_FALLBACK after SEMIHOSTING (instead of after SPL_SEMIHOSTING_FALLBACK) - Use SYS_ERRNO instead of SYS_ISERROR, since the latter is not implemented in older QEMUs. 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 408d96ac8e..777d318c5d 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -414,6 +414,16 @@ config 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 && 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 @@ -427,6 +437,17 @@ 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 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 7595dbc4a9..dbea2b06fb 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(SYSERRNO, NULL); + 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'