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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 23A47C433EF for ; Mon, 15 Nov 2021 11:21:27 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E186061C32 for ; Mon, 15 Nov 2021 11:21:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org E186061C32 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=5Ao2/hc3ZOR+2lOEXIn1moLioS0FqA9AUH+bu43j8Po=; b=CBRCt2bdqouSGx fIJ2b+aD6mAHkUmTmWtOY/edM4rxuvANYRRLmsuwa3KzpYgx8CWSucjC9np372ZqemvL5a5gFjhf4 xmXzaR728xXYZ0TZbd9aPa6UWB95wyeBBK9eL1ynxGNMEcCKlGFJUfgrs1tt+hCi5Iv/tD9LVjljt lCV6eRX6OPM+7Eoho3bhacifzvMdC9FlMY+qxoGD6luv1XwtRkgS07S/st8xHzXlOcUBQ1yizQUKO psQK9uS3bN/bZj9LMsPlOYRaJui8T3R7rQzYX2OAjCrJFFYX+MWqE0xIKhMLFN6dckKDAyk/CYwnS n1aIEszVVAKtQVd53qnQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mma1Y-00FKSs-W4; Mon, 15 Nov 2021 11:19:46 +0000 Received: from mail.kernel.org ([198.145.29.99]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mma0X-00FKB8-AN for linux-arm-kernel@lists.infradead.org; Mon, 15 Nov 2021 11:18:42 +0000 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1134B61C48; Mon, 15 Nov 2021 11:18:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1636975121; bh=dA3IIi9sIWHQ6vYOZLj9CaBbyRT8b2j6tJUCej+v5z8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lie/PYzWv3Km5B3d/pIMvmzgZ4dfaRHGKE8wUOGFBaJPfkyB2bJw5YFPIQXjGwisw g0igOG1/Nn4m/LuSl5t/4xl4pLzgoM/Z3Dz8IAJUm79px9WLVNJcy0ZraW7NdbFZXP 8Sr7hHOb1pWJppe7MvOw3nMQYYjNV2h22eokAB43t3zC1ZRnRQvj+sDXwcbOgm/XN1 WBKZJHT/aAY9BD/GeNhROGGktJ03x1Ta4Bj7zTxo+Bsh+mGp83zV8/B39W2Hn2YwC/ zJQvLrV1GMjt9HCPnL/BQcyh8KtBwaU7OaAMxTgD3Tab6NGD3npK0X1H9hAdUsTq7Y WP4z/xwJo8Zjw== From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: Ard Biesheuvel , Russell King , Nicolas Pitre , Arnd Bergmann , Kees Cook , Keith Packard , Linus Walleij , Nick Desaulniers Subject: [PATCH v3 6/7] ARM: entry: rework stack realignment code in svc_entry Date: Mon, 15 Nov 2021 12:18:15 +0100 Message-Id: <20211115111816.3911213-7-ardb@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211115111816.3911213-1-ardb@kernel.org> References: <20211115111816.3911213-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3237; h=from:subject; bh=dA3IIi9sIWHQ6vYOZLj9CaBbyRT8b2j6tJUCej+v5z8=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBhkkH11GhMzo7zHn8RNUyZGEKFd2j4fYDRbusdPK5f syMnKgmJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYZJB9QAKCRDDTyI5ktmPJO/DC/ 9RcmbGVYCk2XeGAvL/8Rcq0kNygGM0PNwQdUq/PhQZl0FwN7yDi4dEvXSbT9ASCPS32AIMBeAj6bmL tJ7viLwn/9mMUL8tbejERkd98k1apy7xkalXwXWeJNNgl1UxbuELI5EWI5sK/s+S5eReCYaWXRjldx 46QhZqfK92cWEjkTBvTLV44ljZVFqQcVPvQOFO/YjOXQS0PV8ynnO86SerjygOIFeErwPR1ukAXqQa W+xDyLktz3y9A8SHezbHsRUmVhzSquHWMVIk7f2S+TT/YYOMRkPH/btLvlToRcAXWWh/ZYEgVN/t7e /tap0rYCcNu4soPT8le/0txl7pGM7TCfN2UHu5s8pI2SiykvOR43xvuSHM9VIg6QtMG7xuIidvlSou cnWq/K+iAOdJnnGOE2mdlAfNq8KqZd/M74ZoSIfgBZ1b13BSfwkA8xa/9VUev8YNJxaFTRDGRgijyu cX4p6NDIeDrQH2q2WzdkONpVf8Fxnl1EmJuka20t6XBzc= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211115_031841_418247_C54E57E3 X-CRM114-Status: GOOD ( 17.34 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The original Thumb-2 enablement patches updated the stack realignment code in svc_entry to work around the lack of a STMIB instruction in Thumb-2, by subtracting 4 from the frame size, inverting the sense of the misaligment check, and changing to a STMIA instruction and a final stack push of a 4 byte quantity that results in the stack becoming aligned at the end of the sequence. It also pushes and pops R0 to the stack in order to have a temp register that Thumb-2 allows in general purpose ALU instructions, as TST using SP is not permitted. Both are a bit problematic for vmap'ed stacks, as using the stack is only permitted after we decide that we did not overflow the stack, or have already switched to the overflow stack. As for the alignment check: the current approach creates a corner case where, if the initial SUB of SP ends up right at the start of the stack, we will end up subtracting another 8 bytes and overflowing it. This means we would need to add the overflow check *after* the SUB that deliberately misaligns the stack. However, this would require us to keep local state (i.e., whether we performed the subtract or not) across the overflow check, but without any GPRs or stack available. So let's switch to an approach where we don't use the stack, and where the alignment check of the stack pointer occurs in the usual way, as this is guaranteed not to result in overflow. This means we will be able to do the overflow check first. Acked-by: Nicolas Pitre Signed-off-by: Ard Biesheuvel --- arch/arm/kernel/entry-armv.S | 25 +++++++++++--------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/arch/arm/kernel/entry-armv.S b/arch/arm/kernel/entry-armv.S index ce8ca29461de..b18f3aa98f42 100644 --- a/arch/arm/kernel/entry-armv.S +++ b/arch/arm/kernel/entry-armv.S @@ -191,24 +191,27 @@ ENDPROC(__und_invalid) .macro svc_entry, stack_hole=0, trace=1, uaccess=1 UNWIND(.fnstart ) UNWIND(.save {r0 - pc} ) - sub sp, sp, #(SVC_REGS_SIZE + \stack_hole - 4) + sub sp, sp, #(SVC_REGS_SIZE + \stack_hole) #ifdef CONFIG_THUMB2_KERNEL - SPFIX( str r0, [sp] ) @ temporarily saved - SPFIX( mov r0, sp ) - SPFIX( tst r0, #4 ) @ test original stack alignment - SPFIX( ldr r0, [sp] ) @ restored + add sp, r0 @ get SP in a GPR without + sub r0, sp, r0 @ using a temp register + tst r0, #4 @ test stack pointer alignment + sub r0, sp, r0 @ restore original R0 + sub sp, r0 @ restore original SP #else SPFIX( tst sp, #4 ) #endif - SPFIX( subeq sp, sp, #4 ) - stmia sp, {r1 - r12} + SPFIX( subne sp, sp, #4 ) + + ARM( stmib sp, {r1 - r12} ) + THUMB( stmia sp, {r0 - r12} ) @ No STMIB in Thumb-2 ldmia r0, {r3 - r5} - add r7, sp, #S_SP - 4 @ here for interlock avoidance + add r7, sp, #S_SP @ here for interlock avoidance mov r6, #-1 @ "" "" "" "" - add r2, sp, #(SVC_REGS_SIZE + \stack_hole - 4) - SPFIX( addeq r2, r2, #4 ) - str r3, [sp, #-4]! @ save the "real" r0 copied + add r2, sp, #(SVC_REGS_SIZE + \stack_hole) + SPFIX( addne r2, r2, #4 ) + str r3, [sp] @ save the "real" r0 copied @ from the exception stack mov r3, lr -- 2.30.2 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel