From patchwork Wed Aug 12 13:26:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 1343682 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2001:8b0:10b:1231::1; helo=merlin.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=wdc.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=merlin.20170209 header.b=NZkUNsCN; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=wdc.com header.i=@wdc.com header.a=rsa-sha256 header.s=dkim.wdc.com header.b=UGLlSM5w; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=sharedspace.onmicrosoft.com header.i=@sharedspace.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-sharedspace-onmicrosoft-com header.b=PqaajHxa; dkim-atps=neutral Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BRVqw47pwz9sTg for ; Wed, 12 Aug 2020 23:27:40 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:To:From: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=kt2s0QSDDzILVX2miVRuOcYZpbnWBbx0Kt3/fHYMZqE=; b=NZkUNsCNVvR+ijj/FBmngn5+e+ baeP4mkIg1ViEMqX/SWrmE804k1u5V7jy6HJVV+/W9qsBuPmYuk6pPW1rqIMftXaQF1AoHwycVHr4 bPYeGydLBHS+TXVnIA5rtLcqfBHqDqLYukga88Fq32DkPtXSJvufH5L0wfxvpaVCA2eiKR2lF8MnX uTHeH9y0wFjzdOHL3bR0t7W7yleFZEe85kxNVfAcLXvG54D+SSAMASRIwcAIyOqSiXpLtLaf9mWGQ 8v7j5iYkyUFM/dM8zjVCkvnWFO7BoqjUde9Q37s+v3HGFuAHAfTu0paUqz3GxfHJ1CqPU7UnkPE1L kyowignA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1k5qmz-0003cN-Ld; Wed, 12 Aug 2020 13:27:33 +0000 Received: from esa6.hgst.iphmx.com ([216.71.154.45]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1k5qmx-0003av-7B for opensbi@lists.infradead.org; Wed, 12 Aug 2020 13:27:32 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1597238851; x=1628774851; h=from:to:cc:subject:date:message-id: content-transfer-encoding:mime-version; bh=H+n0h0Yt+VgMgvn9QAKgpzl2mL0MVJO5LDdfS62Ei7c=; b=UGLlSM5wNuflYUpSEZz304pqjeoq6mxhdV9nRaG/EO8BYqRlCObkhq4b z/XVoDGFb6LqGfNfZ18il/XnpJNtGkU+OlgpZoLHtsd+W4XDjRFkqs1Pd hceUh3ZAoz5BL7deVyfwOTMwT2MhCi55phjZXkThXzZZGJ9/Ato6M34sr gk0Ute7wlzGIxdmzY+PmfGcnxcha1KLg3ROTukGvWOr1/UI63Yzf0DeGo bAepU15UJTc7uW9wrDQ2wdJYQdt0eJOjRPEWsFp0Y3XGc/ExTKTnSyf37 La6Y2TL5wGtEV22eBikzL+23XqH5DbHoyvuzrMGfbIouDXoRV0mH9gJSG g==; IronPort-SDR: oaMbNNYizvjVH3P7it9fVzZNivwO2niYUcAyrBsDamG44PxBScra7+nQJAy68Nrxy4eQlEMwit IGOgx+Pgclk6orQ7+sqQ3DZZTHQC4iVocBF7Oy0Ji0njYo/4iIDCn8w/U835MOF2YnoIAfGO0f Jj0xHt0aa/AsrJEfKffnKRjj2lwBlsVoXxtG6N7pXepful5P9NhcaQfaA6zZaZaBsr8iBDyOG8 L2e6+fbGlrLs7pUWSwF1pyKa0zY1BL82axBvZe3kUcux9s6KQOIYMobXdll7iez8KTeLE9M/gS 5+c= X-IronPort-AV: E=Sophos;i="5.76,304,1592841600"; d="scan'208";a="145990774" Received: from mail-bn8nam11lp2176.outbound.protection.outlook.com (HELO NAM11-BN8-obe.outbound.protection.outlook.com) ([104.47.58.176]) by ob1.hgst.iphmx.com with ESMTP; 12 Aug 2020 21:27:28 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ms3dsGnRXjlmzPDsyL9qFbL3FQzBrdfAeRsyQtv5TY8iosWlrDCfS/EAMfNCNvIPdvfCe8iEFauxhQz8qFRGKUvrL7XhBJRQwql/bsWqBofXSIJXdzmzjbQMeGh46Ti5vZZX9I//+IhAvgKf3SvWjt1MbTN5LrhvtOcf0Pdq/sbTSkopJyTuVVP6MTwiPSmW/LIzjPmTH9qUAn03Zk09e1bwWIN9nTcqBq/EV/TLDsRxfNsx9Ft1R/N2Qm3dJ854t3/sfaXvzqzIpSAvF0SGztnvXeiVWKaqch1quQPoc9vgEQ4cS6xOTsal7aQtpEo26gQpzrWlzRGkxwY/Tq3xiw== 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-SenderADCheck; bh=D3G+6l8dy9+pmhfTBffy1Blm2aCKV0MI2bKSupnBy5E=; b=CtpH4HO25d2g2XFDHfm8h873kOdvy4nTyqd5VrrATYcB60B8nNUQwBO433KczXWcMtTIxTLzg3qAwbQNxgy9RDVUAwCS3THV8rcXM4ZOSnZLHHIs2sx43Q2J4ULjcNn1uNdixzYfQtX7gUUSPK26oPThnVvRziMQBOHWeiDELStkY3lMTCkFmKCiIwEo1XWvKkAKA2FOf/YoQPrUXmaL9mQm15ZcnF6H06Hm9XEKZMpgpRMkOx0RW57cS7Jlo81ZwYffeVvSlZ52y/5z/5S0VVyfut2ry9fBnDqQX6x6tpPX8Jsy0Af9pCAz6RQjlUVIfHZVE3M9VMyojrhUthwj1g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=wdc.com; dmarc=pass action=none header.from=wdc.com; dkim=pass header.d=wdc.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sharedspace.onmicrosoft.com; s=selector2-sharedspace-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=D3G+6l8dy9+pmhfTBffy1Blm2aCKV0MI2bKSupnBy5E=; b=PqaajHxa0e9j+daDrG5DPoGfsjKoAYTfCLU3w5gvZAMZ4Ja20Ua3RcbE7EKSPnfcDyOly9IAXZA7jEcAnPkXYv+B+QEeQLIGud/XNuLwC/O+0M2ZkI/mKeRAY0iSIqj5IlxXBaamJuIYbJvtZDShT/pGIF3t1Kz+jrrQsXs4FQg= Authentication-Results: wdc.com; dkim=none (message not signed) header.d=none;wdc.com; dmarc=none action=none header.from=wdc.com; Received: from DM6PR04MB6201.namprd04.prod.outlook.com (2603:10b6:5:127::32) by DM5PR04MB0347.namprd04.prod.outlook.com (2603:10b6:3:6e::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3283.15; Wed, 12 Aug 2020 13:27:26 +0000 Received: from DM6PR04MB6201.namprd04.prod.outlook.com ([fe80::607a:44ed:1477:83e]) by DM6PR04MB6201.namprd04.prod.outlook.com ([fe80::607a:44ed:1477:83e%7]) with mapi id 15.20.3283.015; Wed, 12 Aug 2020 13:27:26 +0000 From: Anup Patel To: Atish Patra , Alistair Francis Subject: [PATCH v2] firmware: fw_base: Improve exception stack setup in trap handler Date: Wed, 12 Aug 2020 18:56:53 +0530 Message-Id: <20200812132653.262468-1-anup.patel@wdc.com> X-Mailer: git-send-email 2.25.1 X-ClientProxiedBy: BM1PR0101CA0014.INDPRD01.PROD.OUTLOOK.COM (2603:1096:b00:18::24) To DM6PR04MB6201.namprd04.prod.outlook.com (2603:10b6:5:127::32) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from wdc.com (103.56.182.95) by BM1PR0101CA0014.INDPRD01.PROD.OUTLOOK.COM (2603:1096:b00:18::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3283.16 via Frontend Transport; Wed, 12 Aug 2020 13:27:24 +0000 X-Mailer: git-send-email 2.25.1 X-Originating-IP: [103.56.182.95] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 226bb2b3-f3c0-4e92-9160-08d83ec37418 X-MS-TrafficTypeDiagnostic: DM5PR04MB0347: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: WDCIPOUTBOUND: EOP-TRUE X-MS-Oob-TLC-OOBClassifiers: OLM:5797; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DM6PR04MB6201.namprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(39860400002)(376002)(346002)(136003)(366004)(396003)(83380400001)(44832011)(478600001)(7696005)(55016002)(86362001)(956004)(2906002)(16526019)(26005)(316002)(52116002)(2616005)(186003)(8676002)(4326008)(8886007)(5660300002)(6636002)(6666004)(8936002)(110136005)(1076003)(36756003)(54906003)(66556008)(66476007)(66946007); DIR:OUT; SFP:1102; X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: 226bb2b3-f3c0-4e92-9160-08d83ec37418 X-MS-Exchange-CrossTenant-AuthSource: DM6PR04MB6201.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Aug 2020 13:27:26.4014 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: FR3EkSuAW+nCIy1CeTb/6RNFMu/woHa0axkzxRlBozWjT5Sl7my6FeHuYP/2EKB1rqZey5/ShvXhav96dLUgpA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR04MB0347 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200812_092731_462475_2EAB2E8B X-CRM114-Status: GOOD ( 16.48 ) X-Spam-Score: -2.5 (--) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-2.5 points) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [216.71.154.45 listed in list.dnswl.org] -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.0 MSGID_FROM_MTA_HEADER Message-Id was added by a relay X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Anup Patel , Anup Patel , opensbi@lists.infradead.org Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Currently, the low-level trap handler (i.e. _trap_handler()) uses branch instructions to conditionally setup exception stack based on which mode trap occured. This patch implements exception stack setup using xor instructions which is faster with same number of instructions due to lack of branch instructions. The new exception stack setup approach can be best described by the following pseudocode: Came_From_M_Mode = ((MSTATUS.MPP < PRV_M) ? 1 : 0) - 1; Exception_Stack = TP ^ (Came_From_M_Mode & (SP ^ TP)) Came_From_M_Mode = 0 ==> Exception_Stack = TP Came_From_M_Mode = -1 ==> Exception_Stack = SP Signed-off-by: Anup Patel Reviewed-by: Atish Patra --- Changes since v1: - Don't assume that 0b10 privilege mode will never be used - Use AND operation in-place of MUL operation --- firmware/fw_base.S | 47 +++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/firmware/fw_base.S b/firmware/fw_base.S index b66ac41..0271d9a 100644 --- a/firmware/fw_base.S +++ b/firmware/fw_base.S @@ -490,35 +490,30 @@ _trap_handler: /* Save T0 in scratch space */ REG_S t0, SBI_SCRATCH_TMP0_OFFSET(tp) - /* Check which mode we came from */ + /* + * Set T0 to appropriate exception stack + * + * Came_From_M_Mode = ((MSTATUS.MPP < PRV_M) ? 1 : 0) - 1; + * Exception_Stack = TP ^ (Came_From_M_Mode & (SP ^ TP)) + * + * Came_From_M_Mode = 0 ==> Exception_Stack = TP + * Came_From_M_Mode = -1 ==> Exception_Stack = SP + */ csrr t0, CSR_MSTATUS srl t0, t0, MSTATUS_MPP_SHIFT and t0, t0, PRV_M - xori t0, t0, PRV_M - beq t0, zero, _trap_handler_m_mode - - /* We came from S-mode or U-mode */ -_trap_handler_s_mode: - /* Set T0 to original SP */ - add t0, sp, zero - - /* Setup exception stack */ - add sp, tp, -(SBI_TRAP_REGS_SIZE) - - /* Jump to code common for all modes */ - j _trap_handler_all_mode - - /* We came from M-mode */ -_trap_handler_m_mode: - /* Set T0 to original SP */ - add t0, sp, zero - - /* Re-use current SP as exception stack */ - add sp, sp, -(SBI_TRAP_REGS_SIZE) - -_trap_handler_all_mode: - /* Save original SP (from T0) on stack */ - REG_S t0, SBI_TRAP_REGS_OFFSET(sp)(sp) + slti t0, t0, PRV_M + add t0, t0, -1 + xor sp, sp, tp + and t0, t0, sp + xor sp, sp, tp + xor t0, tp, t0 + + /* Save original SP on exception stack */ + REG_S sp, (SBI_TRAP_REGS_OFFSET(sp) - SBI_TRAP_REGS_SIZE)(t0) + + /* Set SP to exception stack and make room for trap registers */ + add sp, t0, -(SBI_TRAP_REGS_SIZE) /* Restore T0 from scratch space */ REG_L t0, SBI_SCRATCH_TMP0_OFFSET(tp)