From patchwork Mon Feb 26 08:19:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Shi X-Patchwork-Id: 10241413 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id BBAC4605F3 for ; Mon, 26 Feb 2018 08:22:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A3E9429139 for ; Mon, 26 Feb 2018 08:22:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 985D929156; Mon, 26 Feb 2018 08:22:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 21E8A2914B for ; Mon, 26 Feb 2018 08:22:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To: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:List-Owner; bh=LgSfb7IRcetZD9e47aTWQCel7U3mk4f4lSX7tRtcfrA=; b=ex+zQqczV9223ImVjoVdOhYZuf MtCkFXmhPQxhZT4FQuPsZ/R1DpRD7SB268WfycwisdzY9QCwAF0jNUv2cB9fj55sRrdy5QZUAW+dt aghc1wg8rUllixchaZY2zhBsKn8nsK3gQMQJ+7bHwxBc7XJJuA8ukph+2qanOvqo38DG1nilkmHPf dR+WzMSswGNWotwppI/Zc7HrOFh+63a2Rdan4F+DyMix9hx8fGq7Bp1op/E3XtrLKMv1YRxWI2kNH Q0J9ruvHcMQHzsFiob5E+Ow7AqfPafRGTdKJSfcT1rmzY0nMNFRMjCVBntF7nCfSlgsFFiUb8hhZb +b0IVakQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.89 #1 (Red Hat Linux)) id 1eqE3Z-0000xH-Ut; Mon, 26 Feb 2018 08:22:45 +0000 Received: from mail-pl0-x241.google.com ([2607:f8b0:400e:c01::241]) by bombadil.infradead.org with esmtps (Exim 4.89 #1 (Red Hat Linux)) id 1eqE2c-0000Gx-Tx for linux-arm-kernel@lists.infradead.org; Mon, 26 Feb 2018 08:21:54 +0000 Received: by mail-pl0-x241.google.com with SMTP id v3so8871639plg.3 for ; Mon, 26 Feb 2018 00:21:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=1urEszs1X1+fSgF2FtrvQb+AjsP1I5AehCNh9xZoYO0=; b=FZn/iMPFSmQszE4OjKuPqTP8zGFT2OUpn7fMNK+4S7GRPdKcw8xXr9WK6FiV5pRIB7 a9hBBGLrPO2hIANqgHwoKfMhjF1lNDrEA3IdypstLIQtmfnvJTXXKZ37toGohZzN2SOn Ghrm+0JpzirQAWvLfED6dblRBvI11tfIoP2es= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=1urEszs1X1+fSgF2FtrvQb+AjsP1I5AehCNh9xZoYO0=; b=PK+3BDVdpjbflJ3YgUqXxB9WetpRNBXbU4hBz+7QLUPxJa86YrRZ7HdftvsMCKGWas WH2J/ZcBqj3Zd9E+1jPLeTaeY4zBf3MHcWOaz4EEAqJxzqzp3TzsASvy8iuKj9IRDu9b 4P0flWlkFyViA1Yz0dCYnpTMBY7lE8MmSt/n/xFEKkHNxJf+Fj1sOr650dpZUeYmrp8w FlkzQanPTn6eRtffurdCao7Mdq5yGwpRc5ZHJGHpnNhUbA7pOdxpaAAD9wjZmdYxw6Rd gWfTs1f5Yl/Sb4Si6WDucc5Po1Ev7n3x9Q2bLorbFgfwe2w0MwvNiS3nWzWK2UylTIxp xkXA== X-Gm-Message-State: APf1xPBW0zyYyC4nxikx2Tn0sY7FG6KxlFwqX1byPwSkmN6QBB0hN0Qs drPyqH//XQUKgbvRi4kTYS+2HA== X-Google-Smtp-Source: AH8x2277vzWGpvD96bgH8IXwvkpt0h496HoESY2ApiNGDKjGwK30Q8mdAg/du18NHIXpWVv0iy4EKQ== X-Received: by 2002:a17:902:5e3:: with SMTP id f90-v6mr9940812plf.413.1519633296241; Mon, 26 Feb 2018 00:21:36 -0800 (PST) Received: from localhost.localdomain (176.122.172.82.16clouds.com. [176.122.172.82]) by smtp.gmail.com with ESMTPSA id o86sm1422706pfi.87.2018.02.26.00.21.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 26 Feb 2018 00:21:35 -0800 (PST) From: Alex Shi To: Marc Zyngier , Will Deacon , Ard Biesheuvel , Catalin Marinas , stable@vger.kernel.org, linux-arm-kernel@lists.infradead.org (moderated list:ARM64 PORT (AARCH64 ARCHITECTURE)), linux-kernel@vger.kernel.org (open list) Subject: [PATCH 04/52] arm64: Implement array_index_mask_nospec() Date: Mon, 26 Feb 2018 16:19:38 +0800 Message-Id: <1519633227-29832-5-git-send-email-alex.shi@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1519633227-29832-1-git-send-email-alex.shi@linaro.org> References: <1519633227-29832-1-git-send-email-alex.shi@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180226_002147_861652_656A4CD5 X-CRM114-Status: GOOD ( 11.33 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Robin Murphy MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Robin Murphy commit 022620eed3d0 upstream. Provide an optimised, assembly implementation of array_index_mask_nospec() for arm64 so that the compiler is not in a position to transform the code in ways which affect its ability to inhibit speculation (e.g. by introducing conditional branches). This is similar to the sequence used by x86, modulo architectural differences in the carry/borrow flags. Reviewed-by: Mark Rutland Signed-off-by: Robin Murphy Signed-off-by: Will Deacon Signed-off-by: Catalin Marinas Signed-off-by: Alex Shi --- arch/arm64/include/asm/barrier.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/arch/arm64/include/asm/barrier.h b/arch/arm64/include/asm/barrier.h index c68fdc5..0b0755c 100644 --- a/arch/arm64/include/asm/barrier.h +++ b/arch/arm64/include/asm/barrier.h @@ -40,6 +40,27 @@ #define dma_rmb() dmb(oshld) #define dma_wmb() dmb(oshst) +/* + * Generate a mask for array_index__nospec() that is ~0UL when 0 <= idx < sz + * and 0 otherwise. + */ +#define array_index_mask_nospec array_index_mask_nospec +static inline unsigned long array_index_mask_nospec(unsigned long idx, + unsigned long sz) +{ + unsigned long mask; + + asm volatile( + " cmp %1, %2\n" + " sbc %0, xzr, xzr\n" + : "=r" (mask) + : "r" (idx), "Ir" (sz) + : "cc"); + + csdb(); + return mask; +} + #define __smp_mb() dmb(ish) #define __smp_rmb() dmb(ishld) #define __smp_wmb() dmb(ishst)