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: 129560 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp3355716lja; Mon, 26 Feb 2018 00:21:46 -0800 (PST) X-Google-Smtp-Source: AH8x225+3gHoMt1kWsDbszS02Pr3VbP8HAXH6BQ0AZlVRwIEVLojZ7/iY1mPAtPWbEj0NtGS7rT1 X-Received: by 10.99.170.73 with SMTP id x9mr7809734pgo.393.1519633306831; Mon, 26 Feb 2018 00:21:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519633306; cv=none; d=google.com; s=arc-20160816; b=rVXx2x3fRKkZrrWUwgXHm5tY8mIcDNt0x+Yhmm+yd7n0L7LHsqPC7xG5Uwpy6DyWWg L5ym7tQGH1bhzwUC64+QpiITK2WV3c+u16lMIu8IAWtpTCAiuQQ91lZhofeiI2A46O3j Onvz2T+bJYl7T25888bjtAc5PxFzChZouNpzooGPnks4jl1R2yaar3UsK+9sX3juDxVL CukMsQaSvzx6ZZfO48U0hHuav1DZ5Vq0hrUT9JUllM7PJ9+SYNxMPAws3JnwNguzJbJq kBZQsc2uVk8VRhGRAT3RBm+5XyJv82uxCh2K/UmpSckdBNwRYe1t4hQLNsEZAZdJvIes fMJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=1urEszs1X1+fSgF2FtrvQb+AjsP1I5AehCNh9xZoYO0=; b=pc+6jQGS0QV6qCWSaD/OAsavNZMBOpd1R/+UwPL3/9prdkSf1DRdgvJphHtN3Ljj6P yha4KPzSh2rDdsDycSt7VDWOfjSQ87zESY7N101c5nSXaVYeiohe1rRDv61knKDBoccs qATzjWUBF7k97G/XrxKfKUE5TEdDFA4xcRbWIIm6YvBJJFbCPISf1BGI9laPBjQN2aYl gTdo3MSt5gQ1+SKdhAIQzON0h8WE0mf2vkiRFIx2Lo6BPQe3lhBdhGyAAXPt8yqNZ6ld NfpvJv8i2bVwPqFcEZ7Pc33U9FIFu/hPGkXByYGcVp7PmtU6pl+2G9Epojrg3oQpyaC8 8Nbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=FZn/iMPF; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j12si5255036pgt.173.2018.02.26.00.21.46; Mon, 26 Feb 2018 00:21:46 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=FZn/iMPF; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752301AbeBZIVl (ORCPT + 28 others); Mon, 26 Feb 2018 03:21:41 -0500 Received: from mail-pl0-f68.google.com ([209.85.160.68]:42676 "EHLO mail-pl0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752045AbeBZIVh (ORCPT ); Mon, 26 Feb 2018 03:21:37 -0500 Received: by mail-pl0-f68.google.com with SMTP id 31so8853864ple.9 for ; Mon, 26 Feb 2018 00:21:37 -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=qWk/1U9S7TnRmPi8puD4MKEtlV5MlVroq0/yS5WgFC44gUTgxeF9vvAMfnL6w0HZmm QYErPoICONvpCaDNRBbHg2e9V3LvqQ7SN1HiPg/dTXdhEcYuraUg7k3Kb60wTOBpywV2 Z3miid3Bk/RnCVFAcFvcnUFVt0zOENpn3YnYstyBqqUUyOklk8cC98HQr97bWno57+hK 1YcoW9hbvUJ3Ii9uUwMre/ULlG95pp9ny3P3OXsBW1HfMG42K+c74NE7rMAHbsLDphr3 yag8npqqVdUu5yG9i1EtmbRXlFnHIj3mrViJaoKk5H19UJgu2eM9lVO+Qj02kvHG0KWE l93A== X-Gm-Message-State: APf1xPBkpgBa0yKkWtxcoiO5GAtrfUXpBTscv6U2t55DYwqQeKrNKWVz 81BqYKvGULAchU3sZE06ws2zTQ== 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) Cc: Robin Murphy 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> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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(+) -- 2.7.4 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)