From patchwork Thu Mar 1 12:53:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Shi X-Patchwork-Id: 10251191 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 A075860211 for ; Thu, 1 Mar 2018 12:57:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DAB28285E2 for ; Thu, 1 Mar 2018 12:57:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CF515285E5; Thu, 1 Mar 2018 12:57:47 +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 7764A285F9 for ; Thu, 1 Mar 2018 12:57:47 +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=GkeY8sSMaTHo5vtOTmQtYN25/o RibvigloZbnKH/DVXfQtyabNuMh/+A5euAFFYyq5OyhY6EDEim2McbxZDZZ5m8fiNKA/Zal3cTxlO 2alAd21bUeRkoahTLOvOlTXYKLea5bVD2VxAuVO5FAC1+W3nFoYJ/OstSjh29ad5HokLe2nNUA9Gz qLKtG4vSl/SuBBfnxj5cClJKcidxG7g+s/5RnB5QqZABwq5se480wn95GmFcBfQL7QUJi+GBCUVH9 htkLqEzSLX+pYu3w0kRg8iwC9FAlSXv6T+EcTdFMveIOxKOCrRCckwz0p58w1MVNOVfiMQA9TuFJ6 YbMHn6LA==; 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 1erNmB-0001rT-Ar; Thu, 01 Mar 2018 12:57:35 +0000 Received: from mail-pl0-x244.google.com ([2607:f8b0:400e:c01::244]) by bombadil.infradead.org with esmtps (Exim 4.89 #1 (Red Hat Linux)) id 1erNkU-0000OO-1p for linux-arm-kernel@lists.infradead.org; Thu, 01 Mar 2018 12:56:03 +0000 Received: by mail-pl0-x244.google.com with SMTP id v9-v6so3577664plp.12 for ; Thu, 01 Mar 2018 04:55:39 -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=Vm6pcX2UzAbaWdmyVa0rPvXZyO2E8/n8CFR09KxGgLr7DE84EkFSOc58SyN7MgSzHs R140O1gA1/VhJMJRrfpYoHFqDGmBjG0KNF0Pz/9niTXSBo1iMAboRvBbN0DACyGbIVAP DuTd7fju1g+IeY/LScim2LVwWOpyXitzWnwIs= 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=km1vc0xtr1i35/30iBNgv3Y9ecdarhD+JIcrInfUzWs1GfXSakOHEHurdE2E6RcGJ1 BgHV/pueiBMIHQEm89zVWhWbXwFMsxfB7JytyBSOcVVZyh7TKNB3Eynr8gy/aUN34MS0 j+94AvJBy4CkBiACyxg9C8cr+mNxEAMpMHbtXcsfYas/rRsZGVjY75SJaEP59/9grDvP sgEplJSximpZhWoDZ3GknXh7ig40Gx6aQU1wFYackAShdjemBGHaXv8O/NULL6BrL0mW 796IiLe+6UGaVel1RQW95NQia+q8kJ8nNjsq2qHzeKT1l+XT/U6KNNQ+wtgboWInsfLY DZPg== X-Gm-Message-State: APf1xPDw8iHdvcZi0Uqq1L5VPef9ObHIXw2ymBR3hfpD8/sTAadB9Hrt dFfh5aM1kNaKxOaFZrdFf8dPOA== X-Google-Smtp-Source: AG47ELvhem8BCAwUiUQI+NorcLF/2Oo05NigynpS90LKWX6sStnergawUcqw3aAPHfCS02PqULuoSA== X-Received: by 2002:a17:902:b605:: with SMTP id b5-v6mr1871768pls.354.1519908939113; Thu, 01 Mar 2018 04:55:39 -0800 (PST) Received: from localhost.localdomain (176.122.172.82.16clouds.com. [176.122.172.82]) by smtp.gmail.com with ESMTPSA id x4sm2289655pfb.46.2018.03.01.04.55.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 01 Mar 2018 04:55:38 -0800 (PST) From: Alex Shi To: Marc Zyngier , Will Deacon , Ard Biesheuvel , Catalin Marinas , stable@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH 04/45] arm64: Implement array_index_mask_nospec() Date: Thu, 1 Mar 2018 20:53:41 +0800 Message-Id: <1519908862-11425-5-git-send-email-alex.shi@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1519908862-11425-1-git-send-email-alex.shi@linaro.org> References: <1519908862-11425-1-git-send-email-alex.shi@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180301_045551_075932_E02FCB00 X-CRM114-Status: GOOD ( 11.48 ) 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: Alex Shi , 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)