arm64: Implement array_index_mask_nospec()

"Linux Kernel Mailing List" <[email protected]>
Newsgroups gmane.linux.kernel.commits.head
Message-ID <[email protected]>
Web:        https://git.kernel.org/torvalds/c/022620eed3d0bc4bf2027326f599f5ad71c2ea3f
Commit:     022620eed3d0bc4bf2027326f599f5ad71c2ea3f
Parent:     669474e772b952b14f4de4845a1558fd4c0414a4
Refname:    refs/heads/master
Author:     Robin Murphy <[email protected]>
AuthorDate: Mon Feb 5 15:34:17 2018 +0000
Committer:  Catalin Marinas <[email protected]>
CommitDate: Tue Feb 6 22:53:30 2018 +0000

    arm64: Implement array_index_mask_nospec()
    
    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 <[email protected]>
    Signed-off-by: Robin Murphy <[email protected]>
    Signed-off-by: Will Deacon <[email protected]>
    Signed-off-by: Catalin Marinas <[email protected]>
---
 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 c0a846d2c602..f11518af96a9 100644
--- a/arch/arm64/include/asm/barrier.h
+++ b/arch/arm64/include/asm/barrier.h
@@ -41,6 +41,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)
--
To unsubscribe from this list: send the line "unsubscribe git-commits-head" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.