From patchwork Wed Nov 11 23:29:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heinrich Schuchardt X-Patchwork-Id: 1398601 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gmx.de Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=gmx.net header.i=@gmx.net header.a=rsa-sha256 header.s=badeba3b8450 header.b=MGJDrWpn; dkim-atps=neutral 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 ozlabs.org (Postfix) with ESMTPS id 4CWgwS2Bfhz9sPB for ; Thu, 12 Nov 2020 10:31:20 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 2B6728256D; Thu, 12 Nov 2020 00:30:50 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; secure) header.d=gmx.net header.i=@gmx.net header.b="MGJDrWpn"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C767782526; Thu, 12 Nov 2020 00:30:30 +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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_FROM,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mout.gmx.net (mout.gmx.net [212.227.15.19]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 3F34282522 for ; Thu, 12 Nov 2020 00:30:20 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=xypron.glpk@gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1605137417; bh=AQ1J36XEe1qZNK/MLba0Na5CVwY2AVVdF9pGxMMpS58=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=MGJDrWpnlSUqlAwIjvPazUo845LLMI2z5QXre5BO8imXoUAOy7AxYWZidkIsj2FlO 55rz+sCpck1TP41tqv3o5bFhw3cM8i5f1SM4/nqWUE6HkHODROJ3FI58iAtGqhdKUz cLxoLJ4R3aUpyfHn4o20s/PUxjHdTMCvi6VZ94xk= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from LT02.fritz.box ([178.202.41.135]) by mail.gmx.com (mrgmx004 [212.227.17.184]) with ESMTPSA (Nemesis) id 1Mf07E-1k6fxL2A26-00gaZF; Thu, 12 Nov 2020 00:30:17 +0100 From: Heinrich Schuchardt To: Simon Glass Cc: u-boot@lists.denx.de, Alexander Graf , Heinrich Schuchardt Subject: [PATCH v2 1/4] sandbox: add handler for exceptions Date: Thu, 12 Nov 2020 00:29:56 +0100 Message-Id: <20201111232959.11241-2-xypron.glpk@gmx.de> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201111232959.11241-1-xypron.glpk@gmx.de> References: <20201111232959.11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hWxdoX6JendR0unzC20+DqW504O/zRnC8p7ZkjFaACvQlSjI2E38+++ueS fvxy7haydEJyhKUOGuOgVTNeZer6GuthUqm3NpMj/b1Vy8VPcF6JsEzG6tqmWo9cLGhHA1LpH RmxvZTXDFfKR2U7Of1Vw== X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.102.3 at phobos.denx.de X-Virus-Status: Clean Add a handler for SIGILL, SIGBUS, SIGSEGV. When an exception occurs print the program counter and the loaded UEFI binaries and reset the system if CONFIG_SANDBOX_CRASH_RESET=y or exit to the OS otherwise. Signed-off-by: Heinrich Schuchardt Reviewed-by: Simon Glass --- v2: add a customizing switch set SA_NODEFER flag for sigaction --- arch/sandbox/Kconfig | 9 ++++++++ arch/sandbox/cpu/os.c | 40 +++++++++++++++++++++++++++++++++++ arch/sandbox/cpu/start.c | 4 ++++ arch/sandbox/lib/interrupts.c | 35 ++++++++++++++++++++++++++++++ include/os.h | 17 +++++++++++++++ 5 files changed, 105 insertions(+) -- 2.28.0 diff --git a/arch/sandbox/Kconfig b/arch/sandbox/Kconfig index 65f988e736..f83282d9d5 100644 --- a/arch/sandbox/Kconfig +++ b/arch/sandbox/Kconfig @@ -51,6 +51,15 @@ config HOST_64BIT endchoice +config SANDBOX_CRASH_RESET + bool "Reset on crash" + help + If an illegal instruction or an illegal memory access occurs, the + sandbox by default writes a crash dump and exits. If you set this + flag, the sandbox is reset instead. This may be useful when running + test suites like the UEFI self certification test which continue + with the next test after a crash. + config SANDBOX_BITS_PER_LONG int default 32 if HOST_32BIT diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c index 0d8efd83f6..b56fa04a34 100644 --- a/arch/sandbox/cpu/os.c +++ b/arch/sandbox/cpu/os.c @@ -3,6 +3,8 @@ * Copyright (c) 2011 The Chromium OS Authors. */ +#define _GNU_SOURCE + #include #include #include @@ -15,11 +17,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include @@ -191,6 +195,42 @@ static void os_sigint_handler(int sig) raise(SIGINT); } +static void os_signal_handler(int sig, siginfo_t *info, void *con) +{ + ucontext_t __maybe_unused *context = con; + unsigned long pc; + +#if defined(__x86_64__) + pc = context->uc_mcontext.gregs[REG_RIP]; +#elif defined(__aarch64__) + pc = context->uc_mcontext.pc; +#elif defined(__riscv) + pc = context->uc_mcontext.__gregs[REG_PC]; +#else + const char msg[] = + "\nUnsupported architecture, cannot read program counter\n"; + + os_write(1, msg, sizeof(msg)); + pc = 0; +#endif + + os_signal_action(sig, pc); +} + +int os_setup_signal_handlers(void) +{ + struct sigaction act; + + act.sa_sigaction = os_signal_handler; + sigemptyset(&act.sa_mask); + act.sa_flags = SA_SIGINFO | SA_NODEFER; + if (sigaction(SIGILL, &act, NULL) || + sigaction(SIGBUS, &act, NULL) || + sigaction(SIGSEGV, &act, NULL)) + return -1; + return 0; +} + /* Put tty into raw mode so and work */ void os_tty_raw(int fd, bool allow_sigs) { diff --git a/arch/sandbox/cpu/start.c b/arch/sandbox/cpu/start.c index a03e5aa0b3..f6c98545e0 100644 --- a/arch/sandbox/cpu/start.c +++ b/arch/sandbox/cpu/start.c @@ -451,6 +451,10 @@ int main(int argc, char *argv[]) if (ret) goto err; + ret = os_setup_signal_handlers(); + if (ret) + goto err; + #if CONFIG_VAL(SYS_MALLOC_F_LEN) gd->malloc_base = CONFIG_MALLOC_F_ADDR; #endif diff --git a/arch/sandbox/lib/interrupts.c b/arch/sandbox/lib/interrupts.c index 21f761ac3b..9c2c60b8c6 100644 --- a/arch/sandbox/lib/interrupts.c +++ b/arch/sandbox/lib/interrupts.c @@ -6,7 +6,13 @@ */ #include +#include #include +#include +#include +#include + +DECLARE_GLOBAL_DATA_PTR; int interrupt_init(void) { @@ -21,3 +27,32 @@ int disable_interrupts(void) { return 0; } + +void os_signal_action(int sig, unsigned long pc) +{ + efi_restore_gd(); + + switch (sig) { + case SIGILL: + printf("\nIllegal instruction\n"); + break; + case SIGBUS: + printf("\nBus error\n"); + break; + case SIGSEGV: + printf("\nSegmentation violation\n"); + break; + default: + break; + } + printf("pc = 0x%lx, ", pc); + printf("pc_reloc = 0x%lx\n\n", pc - gd->reloc_off); + efi_print_image_infos((void *)pc); + + if (IS_ENABLED(CONFIG_SANDBOX_CRASH_RESET)) { + printf("resetting ...\n\n"); + sandbox_reset(); + } else { + sandbox_exit(); + } +} diff --git a/include/os.h b/include/os.h index 1fe44f3510..0913b47b3a 100644 --- a/include/os.h +++ b/include/os.h @@ -407,4 +407,21 @@ void *os_find_text_base(void); */ void os_relaunch(char *argv[]); +/** + * os_setup_signal_handlers() - setup signal handlers + * + * Install signal handlers for SIGBUS and SIGSEGV. + * + * Return: 0 for success + */ +int os_setup_signal_handlers(void); + +/** + * os_signal_action() - handle a signal + * + * @sig: signal + * @pc: program counter + */ +void os_signal_action(int sig, unsigned long pc); + #endif