2020-05-14 14:34:35

by Oliver Swede

[permalink] [raw]
Subject: [PATCH v3 00/13] arm64: Optimise and update memcpy, user copy and string routines

Hi, I have been working on a fix for this branch. The initial
version of this patchset imported new optimizations from Linaro's
cortex-strings project, and converted the instructions to the
macro equivalents in the usercopy template, in order to allow
each of the helper usercopy functions to use expansions that
insert exception table entries for exclusively the instructions
that could potentially fault during a copy.

The 'cortex-strings' repository has since been renamed to
'optimized-routines' (this is public on GitHub at
https://github.com/ARM-software/optimized-routines),
and has been updated with further optimizations of various
library functions, and this includes a newer memcpy
implementation.

The address of page faults is exposed to the faulting instructions'
corresponding fixup code, and in v2 a fixup routine to correspond
to the previous copy algorithm was also implemented in a way that
utilized the fault address to try and efficiently calculate the
number of bytes that failed to copy. It returned the distance
between the fault and the end of the buffer, however this fixup
in [PATCH v2 2/14] had some issues due to the out-of-order nature
of the copy algorithm, and was flagged by the LTP testcase of the
preadv2() syscall (which makes multiple calls to copy_to_user).
This was due to preadv2() reporting SUCCESS for an invalid
destination address, NULL, where it expected EFAULT, because a
nonzero return value was calculated (indicating that some bytes
were copied) due to the fault not occurring at the start of the
buffer.

In this version I have imported the very latest optimized memcpy
routine, and re-written the fixup to use multiple routines that
encapsulate various properties of the algorithm (this is
explained in more detail in patches 11/13, 12/13, 13/13).
The aim is to return the exact number of bytes that haven't copied
when a fault occurs in copy_{to, in, from}_user, and to enable the
fixups to be modular so that they could be re-written without too
much trouble in the future if the copy algorithm was to be updated
again.

Initial testing indicates that the backtracking performed in the
fixup routines is accurate, and I am working on a separate
patchset containing more concise selftests that indirectly
call the usercopy functions via read()/write() - this will help
to ease the verification of expected behaviour.

I am going to post updated benchmark results, as the ~27% increase
in speed measured by Sam with the previous 'cortex-strings' memcpy
is no longer applicable due to the more recent replacement from
'optimized-routines', which should hopefully be even more efficient
and improve this further.

v1: https://lore.kernel.org/linux-arm-kernel/[email protected]/
v2: https://lore.kernel.org/linux-arm-kernel/[email protected]/

Changes since v2:

* Adds Robin's separate patch that fixes a compilation issue with KProbes fixup [1]
* Imports the most recent memcpy implementation by updating Sam's patch
(and moves this patch to occur after the cortex-strings importing so
that it's closer to the patches containing its corresponding fixups)
* Uses the stack to preserve the initial parameters
* Replaces the usercopy fixup routine in v2 with multiple longer
fixups that each make use of the fault address to return the exact
number of bytes that haven't yet copied.

[1] https://lore.kernel.org/linux-arm-kernel/e70f7b9de7e601b9e4a6fedad8eaf64d304b1637.1571326276.git.robin.murphy@arm.com/

Many thanks,
Oliver

Oliver Swede (4):
arm64: Store the arguments to copy_*_user on the stack
arm64: Use additional memcpy macros and fixups
arm64: Add fixup routines for usercopy load exceptions
arm64: Add fixup routines for usercopy store exceptions

Robin Murphy (2):
arm64: Tidy up _asm_extable_faultaddr usage
arm64: kprobes: Drop open-coded exception fixup

Sam Tebbs (7):
arm64: Allow passing fault address to fixup handlers
arm64: Import latest optimization of memcpy
arm64: Import latest version of Cortex Strings' memcmp
arm64: Import latest version of Cortex Strings' memmove
arm64: Import latest version of Cortex Strings' strcmp
arm64: Import latest version of Cortex Strings' strlen
arm64: Import latest version of Cortex Strings' strncmp

arch/arm64/include/asm/alternative.h | 36 ---
arch/arm64/include/asm/assembler.h | 13 +
arch/arm64/include/asm/extable.h | 10 +-
arch/arm64/kernel/probes/kprobes.c | 7 -
arch/arm64/lib/copy_from_user.S | 272 +++++++++++++++++--
arch/arm64/lib/copy_in_user.S | 287 ++++++++++++++++++--
arch/arm64/lib/copy_template.S | 377 +++++++++++++++------------
arch/arm64/lib/copy_template_user.S | 50 ++++
arch/arm64/lib/copy_to_user.S | 273 +++++++++++++++++--
arch/arm64/lib/copy_user_fixup.S | 277 ++++++++++++++++++++
arch/arm64/lib/memcmp.S | 333 +++++++++--------------
arch/arm64/lib/memcpy.S | 128 +++++++--
arch/arm64/lib/memmove.S | 232 ++++++-----------
arch/arm64/lib/strcmp.S | 272 ++++++++-----------
arch/arm64/lib/strlen.S | 247 ++++++++++++------
arch/arm64/lib/strncmp.S | 363 ++++++++++++--------------
arch/arm64/mm/extable.c | 13 +-
arch/arm64/mm/fault.c | 2 +-
18 files changed, 2073 insertions(+), 1119 deletions(-)
create mode 100644 arch/arm64/lib/copy_template_user.S
create mode 100644 arch/arm64/lib/copy_user_fixup.S

--
2.17.1


2020-05-14 14:34:42

by Oliver Swede

[permalink] [raw]
Subject: [PATCH v3 03/13] arm64: Import latest version of Cortex Strings' memcmp

From: Sam Tebbs <[email protected]>

Import the latest version of Cortex Strings' memcmp function.

The upstream source is src/aarch64/memcmp.S as of commit f77e4c932b4f
in https://git.linaro.org/toolchain/cortex-strings.git.

Signed-off-by: Sam Tebbs <[email protected]>
[ rm: update attribution, expand commit message ]
Signed-off-by: Robin Murphy <[email protected]>
Signed-off-by: Oliver Swede <[email protected]>
---
arch/arm64/lib/memcmp.S | 333 ++++++++++++++--------------------------
1 file changed, 117 insertions(+), 216 deletions(-)

diff --git a/arch/arm64/lib/memcmp.S b/arch/arm64/lib/memcmp.S
index c0671e793ea9..580dd0b12ccb 100644
--- a/arch/arm64/lib/memcmp.S
+++ b/arch/arm64/lib/memcmp.S
@@ -1,13 +1,12 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/*
- * Copyright (C) 2013 ARM Ltd.
- * Copyright (C) 2013 Linaro.
+ * Copyright (c) 2013, 2018 Linaro Limited. All rights reserved.
+ * Copyright (c) 2017 ARM Ltd. All rights reserved.
*
- * This code is based on glibc cortex strings work originally authored by Linaro
- * be found @
+ * This code is based on glibc Cortex Strings work originally authored by
+ * Linaro, found at:
*
- * http://bazaar.launchpad.net/~linaro-toolchain-dev/cortex-strings/trunk/
- * files/head:/src/aarch64/
+ * https://git.linaro.org/toolchain/cortex-strings.git
*/

#include <linux/linkage.h>
@@ -25,223 +24,125 @@
* x0 - a compare result, maybe less than, equal to, or greater than ZERO
*/

+#define L(l) .L ## l
+
/* Parameters and result. */
-src1 .req x0
-src2 .req x1
-limit .req x2
-result .req x0
+#define src1 x0
+#define src2 x1
+#define limit x2
+#define result w0

/* Internal variables. */
-data1 .req x3
-data1w .req w3
-data2 .req x4
-data2w .req w4
-has_nul .req x5
-diff .req x6
-endloop .req x7
-tmp1 .req x8
-tmp2 .req x9
-tmp3 .req x10
-pos .req x11
-limit_wd .req x12
-mask .req x13
+#define data1 x3
+#define data1w w3
+#define data1h x4
+#define data2 x5
+#define data2w w5
+#define data2h x6
+#define tmp1 x7
+#define tmp2 x8

SYM_FUNC_START_WEAK_PI(memcmp)
- cbz limit, .Lret0
- eor tmp1, src1, src2
- tst tmp1, #7
- b.ne .Lmisaligned8
- ands tmp1, src1, #7
- b.ne .Lmutual_align
- sub limit_wd, limit, #1 /* limit != 0, so no underflow. */
- lsr limit_wd, limit_wd, #3 /* Convert to Dwords. */
- /*
- * The input source addresses are at alignment boundary.
- * Directly compare eight bytes each time.
- */
-.Lloop_aligned:
- ldr data1, [src1], #8
- ldr data2, [src2], #8
-.Lstart_realigned:
- subs limit_wd, limit_wd, #1
- eor diff, data1, data2 /* Non-zero if differences found. */
- csinv endloop, diff, xzr, cs /* Last Dword or differences. */
- cbz endloop, .Lloop_aligned
-
- /* Not reached the limit, must have found a diff. */
- tbz limit_wd, #63, .Lnot_limit
-
- /* Limit % 8 == 0 => the diff is in the last 8 bytes. */
- ands limit, limit, #7
- b.eq .Lnot_limit
- /*
- * The remained bytes less than 8. It is needed to extract valid data
- * from last eight bytes of the intended memory range.
- */
- lsl limit, limit, #3 /* bytes-> bits. */
- mov mask, #~0
-CPU_BE( lsr mask, mask, limit )
-CPU_LE( lsl mask, mask, limit )
- bic data1, data1, mask
- bic data2, data2, mask
-
- orr diff, diff, mask
- b .Lnot_limit
-
-.Lmutual_align:
- /*
- * Sources are mutually aligned, but are not currently at an
- * alignment boundary. Round down the addresses and then mask off
- * the bytes that precede the start point.
- */
- bic src1, src1, #7
- bic src2, src2, #7
- ldr data1, [src1], #8
- ldr data2, [src2], #8
- /*
- * We can not add limit with alignment offset(tmp1) here. Since the
- * addition probably make the limit overflown.
- */
- sub limit_wd, limit, #1/*limit != 0, so no underflow.*/
- and tmp3, limit_wd, #7
- lsr limit_wd, limit_wd, #3
- add tmp3, tmp3, tmp1
- add limit_wd, limit_wd, tmp3, lsr #3
- add limit, limit, tmp1/* Adjust the limit for the extra. */
-
- lsl tmp1, tmp1, #3/* Bytes beyond alignment -> bits.*/
- neg tmp1, tmp1/* Bits to alignment -64. */
- mov tmp2, #~0
- /*mask off the non-intended bytes before the start address.*/
-CPU_BE( lsl tmp2, tmp2, tmp1 )/*Big-endian.Early bytes are at MSB*/
- /* Little-endian. Early bytes are at LSB. */
-CPU_LE( lsr tmp2, tmp2, tmp1 )
-
- orr data1, data1, tmp2
- orr data2, data2, tmp2
- b .Lstart_realigned
-
- /*src1 and src2 have different alignment offset.*/
-.Lmisaligned8:
- cmp limit, #8
- b.lo .Ltiny8proc /*limit < 8: compare byte by byte*/
-
- and tmp1, src1, #7
- neg tmp1, tmp1
- add tmp1, tmp1, #8/*valid length in the first 8 bytes of src1*/
- and tmp2, src2, #7
- neg tmp2, tmp2
- add tmp2, tmp2, #8/*valid length in the first 8 bytes of src2*/
- subs tmp3, tmp1, tmp2
- csel pos, tmp1, tmp2, hi /*Choose the maximum.*/
-
- sub limit, limit, pos
- /*compare the proceeding bytes in the first 8 byte segment.*/
-.Ltinycmp:
- ldrb data1w, [src1], #1
- ldrb data2w, [src2], #1
- subs pos, pos, #1
- ccmp data1w, data2w, #0, ne /* NZCV = 0b0000. */
- b.eq .Ltinycmp
- cbnz pos, 1f /*diff occurred before the last byte.*/
- cmp data1w, data2w
- b.eq .Lstart_align
-1:
- sub result, data1, data2
- ret
-
-.Lstart_align:
- lsr limit_wd, limit, #3
- cbz limit_wd, .Lremain8
-
- ands xzr, src1, #7
- b.eq .Lrecal_offset
- /*process more leading bytes to make src1 aligned...*/
- add src1, src1, tmp3 /*backwards src1 to alignment boundary*/
- add src2, src2, tmp3
- sub limit, limit, tmp3
- lsr limit_wd, limit, #3
- cbz limit_wd, .Lremain8
- /*load 8 bytes from aligned SRC1..*/
- ldr data1, [src1], #8
- ldr data2, [src2], #8
-
- subs limit_wd, limit_wd, #1
- eor diff, data1, data2 /*Non-zero if differences found.*/
- csinv endloop, diff, xzr, ne
- cbnz endloop, .Lunequal_proc
- /*How far is the current SRC2 from the alignment boundary...*/
- and tmp3, tmp3, #7
-
-.Lrecal_offset:/*src1 is aligned now..*/
- neg pos, tmp3
-.Lloopcmp_proc:
- /*
- * Divide the eight bytes into two parts. First,backwards the src2
- * to an alignment boundary,load eight bytes and compare from
- * the SRC2 alignment boundary. If all 8 bytes are equal,then start
- * the second part's comparison. Otherwise finish the comparison.
- * This special handle can garantee all the accesses are in the
- * thread/task space in avoid to overrange access.
- */
- ldr data1, [src1,pos]
- ldr data2, [src2,pos]
- eor diff, data1, data2 /* Non-zero if differences found. */
- cbnz diff, .Lnot_limit
-
- /*The second part process*/
- ldr data1, [src1], #8
- ldr data2, [src2], #8
- eor diff, data1, data2 /* Non-zero if differences found. */
- subs limit_wd, limit_wd, #1
- csinv endloop, diff, xzr, ne/*if limit_wd is 0,will finish the cmp*/
- cbz endloop, .Lloopcmp_proc
-.Lunequal_proc:
- cbz diff, .Lremain8
-
-/* There is difference occurred in the latest comparison. */
-.Lnot_limit:
-/*
-* For little endian,reverse the low significant equal bits into MSB,then
-* following CLZ can find how many equal bits exist.
-*/
-CPU_LE( rev diff, diff )
-CPU_LE( rev data1, data1 )
-CPU_LE( rev data2, data2 )
-
- /*
- * The MS-non-zero bit of DIFF marks either the first bit
- * that is different, or the end of the significant data.
- * Shifting left now will bring the critical information into the
- * top bits.
- */
- clz pos, diff
- lsl data1, data1, pos
- lsl data2, data2, pos
- /*
- * We need to zero-extend (char is unsigned) the value and then
- * perform a signed subtraction.
- */
- lsr data1, data1, #56
- sub result, data1, data2, lsr #56
+ subs limit, limit, 8
+ b.lo L(less8)
+
+ ldr data1, [src1], 8
+ ldr data2, [src2], 8
+ cmp data1, data2
+ b.ne L(return)
+
+ subs limit, limit, 8
+ b.gt L(more16)
+
+ ldr data1, [src1, limit]
+ ldr data2, [src2, limit]
+ b L(return)
+
+L(more16):
+ ldr data1, [src1], 8
+ ldr data2, [src2], 8
+ cmp data1, data2
+ bne L(return)
+
+ /* Jump directly to comparing the last 16 bytes for 32 byte (or less)
+ strings. */
+ subs limit, limit, 16
+ b.ls L(last_bytes)
+
+ /* We overlap loads between 0-32 bytes at either side of SRC1 when we
+ try to align, so limit it only to strings larger than 128 bytes. */
+ cmp limit, 96
+ b.ls L(loop16)
+
+ /* Align src1 and adjust src2 with bytes not yet done. */
+ and tmp1, src1, 15
+ add limit, limit, tmp1
+ sub src1, src1, tmp1
+ sub src2, src2, tmp1
+
+ /* Loop performing 16 bytes per iteration using aligned src1.
+ Limit is pre-decremented by 16 and must be larger than zero.
+ Exit if <= 16 bytes left to do or if the data is not equal. */
+ .p2align 4
+L(loop16):
+ ldp data1, data1h, [src1], 16
+ ldp data2, data2h, [src2], 16
+ subs limit, limit, 16
+ ccmp data1, data2, 0, hi
+ ccmp data1h, data2h, 0, eq
+ b.eq L(loop16)
+
+ cmp data1, data2
+ bne L(return)
+ mov data1, data1h
+ mov data2, data2h
+ cmp data1, data2
+ bne L(return)
+
+ /* Compare last 1-16 bytes using unaligned access. */
+L(last_bytes):
+ add src1, src1, limit
+ add src2, src2, limit
+ ldp data1, data1h, [src1]
+ ldp data2, data2h, [src2]
+ cmp data1, data2
+ bne L(return)
+ mov data1, data1h
+ mov data2, data2h
+ cmp data1, data2
+
+ /* Compare data bytes and set return value to 0, -1 or 1. */
+L(return):
+#ifndef __AARCH64EB__
+ rev data1, data1
+ rev data2, data2
+#endif
+ cmp data1, data2
+L(ret_eq):
+ cset result, ne
+ cneg result, result, lo
ret

-.Lremain8:
- /* Limit % 8 == 0 =>. all data are equal.*/
- ands limit, limit, #7
- b.eq .Lret0
-
-.Ltiny8proc:
- ldrb data1w, [src1], #1
- ldrb data2w, [src2], #1
- subs limit, limit, #1
-
- ccmp data1w, data2w, #0, ne /* NZCV = 0b0000. */
- b.eq .Ltiny8proc
- sub result, data1, data2
- ret
-.Lret0:
- mov result, #0
+ .p2align 4
+ /* Compare up to 8 bytes. Limit is [-8..-1]. */
+L(less8):
+ adds limit, limit, 4
+ b.lo L(less4)
+ ldr data1w, [src1], 4
+ ldr data2w, [src2], 4
+ cmp data1w, data2w
+ b.ne L(return)
+ sub limit, limit, 4
+L(less4):
+ adds limit, limit, 4
+ beq L(ret_eq)
+L(byte_loop):
+ ldrb data1w, [src1], 1
+ ldrb data2w, [src2], 1
+ subs limit, limit, 1
+ ccmp data1w, data2w, 0, ne /* NZCV = 0b0000. */
+ b.eq L(byte_loop)
+ sub result, data1w, data2w
ret
SYM_FUNC_END_PI(memcmp)
EXPORT_SYMBOL_NOKASAN(memcmp)
--
2.17.1

2020-05-14 14:34:50

by Oliver Swede

[permalink] [raw]
Subject: [PATCH v3 05/13] arm64: Import latest version of Cortex Strings' strcmp

From: Sam Tebbs <[email protected]>

Import the latest version of Cortex Strings' strcmp function.

The upstream source is src/aarch64/strcmp.S as of commit 90b61261ceb4
in https://git.linaro.org/toolchain/cortex-strings.git.

Signed-off-by: Sam Tebbs <[email protected]>
[ rm: update attribution, expand commit message ]
Signed-off-by: Robin Murphy <[email protected]>
Signed-off-by: Oliver Swede <[email protected]>
---
arch/arm64/lib/strcmp.S | 272 +++++++++++++++++-----------------------
1 file changed, 113 insertions(+), 159 deletions(-)

diff --git a/arch/arm64/lib/strcmp.S b/arch/arm64/lib/strcmp.S
index 4e79566726c8..e00ff46c4ffc 100644
--- a/arch/arm64/lib/strcmp.S
+++ b/arch/arm64/lib/strcmp.S
@@ -1,13 +1,11 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/*
- * Copyright (C) 2013 ARM Ltd.
- * Copyright (C) 2013 Linaro.
+ * Copyright (c) 2012,2018 Linaro Limited. All rights reserved.
*
- * This code is based on glibc cortex strings work originally authored by Linaro
- * be found @
+ * This code is based on glibc Cortex Strings work originally authored by
+ * Linaro, found at:
*
- * http://bazaar.launchpad.net/~linaro-toolchain-dev/cortex-strings/trunk/
- * files/head:/src/aarch64/
+ * https://git.linaro.org/toolchain/cortex-strings.git
*/

#include <linux/linkage.h>
@@ -25,60 +23,106 @@
* or be greater than s2.
*/

+#define L(label) .L ## label
+
#define REP8_01 0x0101010101010101
#define REP8_7f 0x7f7f7f7f7f7f7f7f
#define REP8_80 0x8080808080808080

/* Parameters and result. */
-src1 .req x0
-src2 .req x1
-result .req x0
+#define src1 x0
+#define src2 x1
+#define result x0

/* Internal variables. */
-data1 .req x2
-data1w .req w2
-data2 .req x3
-data2w .req w3
-has_nul .req x4
-diff .req x5
-syndrome .req x6
-tmp1 .req x7
-tmp2 .req x8
-tmp3 .req x9
-zeroones .req x10
-pos .req x11
-
+#define data1 x2
+#define data1w w2
+#define data2 x3
+#define data2w w3
+#define has_nul x4
+#define diff x5
+#define syndrome x6
+#define tmp1 x7
+#define tmp2 x8
+#define tmp3 x9
+#define zeroones x10
+#define pos x11
+
+ /* Start of performance-critical section -- one 64B cache line. */
SYM_FUNC_START_WEAK_PI(strcmp)
eor tmp1, src1, src2
mov zeroones, #REP8_01
tst tmp1, #7
- b.ne .Lmisaligned8
+ b.ne L(misaligned8)
ands tmp1, src1, #7
- b.ne .Lmutual_align
-
- /*
- * NUL detection works on the principle that (X - 1) & (~X) & 0x80
- * (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
- * can be done in parallel across the entire word.
- */
-.Lloop_aligned:
+ b.ne L(mutual_align)
+ /* NUL detection works on the principle that (X - 1) & (~X) & 0x80
+ (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
+ can be done in parallel across the entire word. */
+L(loop_aligned):
ldr data1, [src1], #8
ldr data2, [src2], #8
-.Lstart_realigned:
+L(start_realigned):
sub tmp1, data1, zeroones
orr tmp2, data1, #REP8_7f
eor diff, data1, data2 /* Non-zero if differences found. */
bic has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */
orr syndrome, diff, has_nul
- cbz syndrome, .Lloop_aligned
- b .Lcal_cmpresult
-
-.Lmutual_align:
- /*
- * Sources are mutually aligned, but are not currently at an
- * alignment boundary. Round down the addresses and then mask off
- * the bytes that preceed the start point.
- */
+ cbz syndrome, L(loop_aligned)
+ /* End of performance-critical section -- one 64B cache line. */
+
+L(end):
+CPU_LE(rev syndrome, syndrome)
+CPU_LE(rev data1, data1)
+ /* The MS-non-zero bit of the syndrome marks either the first bit
+ that is different, or the top bit of the first zero byte.
+ Shifting left now will bring the critical information into the
+ top bits. */
+CPU_LE(clz pos, syndrome)
+CPU_LE(rev data2, data2)
+CPU_LE(lsl data1, data1, pos)
+CPU_LE(lsl data2, data2, pos)
+ /* But we need to zero-extend (char is unsigned) the value and then
+ perform a signed 32-bit subtraction. */
+CPU_LE(lsr data1, data1, #56)
+CPU_LE(sub result, data1, data2, lsr #56)
+CPU_LE(ret)
+ /* For big-endian we cannot use the trick with the syndrome value
+ as carry-propagation can corrupt the upper bits if the trailing
+ bytes in the string contain 0x01. */
+ /* However, if there is no NUL byte in the dword, we can generate
+ the result directly. We can't just subtract the bytes as the
+ MSB might be significant. */
+CPU_BE(cbnz has_nul, 1f)
+CPU_BE(cmp data1, data2)
+CPU_BE(cset result, ne)
+CPU_BE(cneg result, result, lo)
+CPU_BE(ret)
+1:
+ /* Re-compute the NUL-byte detection, using a byte-reversed value. */
+CPU_BE(rev tmp3, data1)
+CPU_BE(sub tmp1, tmp3, zeroones)
+CPU_BE(orr tmp2, tmp3, #REP8_7f)
+CPU_BE(bic has_nul, tmp1, tmp2)
+CPU_BE(rev has_nul, has_nul)
+CPU_BE(orr syndrome, diff, has_nul)
+CPU_BE(clz pos, syndrome)
+ /* The MS-non-zero bit of the syndrome marks either the first bit
+ that is different, or the top bit of the first zero byte.
+ Shifting left now will bring the critical information into the
+ top bits. */
+CPU_BE(lsl data1, data1, pos)
+CPU_BE(lsl data2, data2, pos)
+ /* But we need to zero-extend (char is unsigned) the value and then
+ perform a signed 32-bit subtraction. */
+CPU_BE(lsr data1, data1, #56)
+CPU_BE(sub result, data1, data2, lsr #56)
+CPU_BE(ret)
+
+L(mutual_align):
+ /* Sources are mutually aligned, but are not currently at an
+ alignment boundary. Round down the addresses and then mask off
+ the bytes that preceed the start point. */
bic src1, src1, #7
bic src2, src2, #7
lsl tmp1, tmp1, #3 /* Bytes beyond alignment -> bits. */
@@ -87,137 +131,47 @@ SYM_FUNC_START_WEAK_PI(strcmp)
ldr data2, [src2], #8
mov tmp2, #~0
/* Big-endian. Early bytes are at MSB. */
-CPU_BE( lsl tmp2, tmp2, tmp1 ) /* Shift (tmp1 & 63). */
+CPU_BE(lsl tmp2, tmp2, tmp1) /* Shift (tmp1 & 63). */
/* Little-endian. Early bytes are at LSB. */
-CPU_LE( lsr tmp2, tmp2, tmp1 ) /* Shift (tmp1 & 63). */
-
+CPU_LE(lsr tmp2, tmp2, tmp1) /* Shift (tmp1 & 63). */
orr data1, data1, tmp2
orr data2, data2, tmp2
- b .Lstart_realigned
-
-.Lmisaligned8:
- /*
- * Get the align offset length to compare per byte first.
- * After this process, one string's address will be aligned.
- */
- and tmp1, src1, #7
- neg tmp1, tmp1
- add tmp1, tmp1, #8
- and tmp2, src2, #7
- neg tmp2, tmp2
- add tmp2, tmp2, #8
- subs tmp3, tmp1, tmp2
- csel pos, tmp1, tmp2, hi /*Choose the maximum. */
-.Ltinycmp:
+ b L(start_realigned)
+
+L(misaligned8):
+ /* Align SRC1 to 8 bytes and then compare 8 bytes at a time, always
+ checking to make sure that we don't access beyond page boundary in
+ SRC2. */
+ tst src1, #7
+ b.eq L(loop_misaligned)
+L(do_misaligned):
ldrb data1w, [src1], #1
ldrb data2w, [src2], #1
- subs pos, pos, #1
- ccmp data1w, #1, #0, ne /* NZCV = 0b0000. */
- ccmp data1w, data2w, #0, cs /* NZCV = 0b0000. */
- b.eq .Ltinycmp
- cbnz pos, 1f /*find the null or unequal...*/
cmp data1w, #1
- ccmp data1w, data2w, #0, cs
- b.eq .Lstart_align /*the last bytes are equal....*/
-1:
- sub result, data1, data2
- ret
-
-.Lstart_align:
- ands xzr, src1, #7
- b.eq .Lrecal_offset
- /*process more leading bytes to make str1 aligned...*/
- add src1, src1, tmp3
- add src2, src2, tmp3
- /*load 8 bytes from aligned str1 and non-aligned str2..*/
+ ccmp data1w, data2w, #0, cs /* NZCV = 0b0000. */
+ b.ne L(done)
+ tst src1, #7
+ b.ne L(do_misaligned)
+
+L(loop_misaligned):
+ /* Test if we are within the last dword of the end of a 4K page. If
+ yes then jump back to the misaligned loop to copy a byte at a time. */
+ and tmp1, src2, #0xff8
+ eor tmp1, tmp1, #0xff8
+ cbz tmp1, L(do_misaligned)
ldr data1, [src1], #8
ldr data2, [src2], #8

sub tmp1, data1, zeroones
orr tmp2, data1, #REP8_7f
- bic has_nul, tmp1, tmp2
- eor diff, data1, data2 /* Non-zero if differences found. */
- orr syndrome, diff, has_nul
- cbnz syndrome, .Lcal_cmpresult
- /*How far is the current str2 from the alignment boundary...*/
- and tmp3, tmp3, #7
-.Lrecal_offset:
- neg pos, tmp3
-.Lloopcmp_proc:
- /*
- * Divide the eight bytes into two parts. First,backwards the src2
- * to an alignment boundary,load eight bytes from the SRC2 alignment
- * boundary,then compare with the relative bytes from SRC1.
- * If all 8 bytes are equal,then start the second part's comparison.
- * Otherwise finish the comparison.
- * This special handle can garantee all the accesses are in the
- * thread/task space in avoid to overrange access.
- */
- ldr data1, [src1,pos]
- ldr data2, [src2,pos]
- sub tmp1, data1, zeroones
- orr tmp2, data1, #REP8_7f
- bic has_nul, tmp1, tmp2
- eor diff, data1, data2 /* Non-zero if differences found. */
- orr syndrome, diff, has_nul
- cbnz syndrome, .Lcal_cmpresult
-
- /*The second part process*/
- ldr data1, [src1], #8
- ldr data2, [src2], #8
- sub tmp1, data1, zeroones
- orr tmp2, data1, #REP8_7f
- bic has_nul, tmp1, tmp2
- eor diff, data1, data2 /* Non-zero if differences found. */
+ eor diff, data1, data2 /* Non-zero if differences found. */
+ bic has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */
orr syndrome, diff, has_nul
- cbz syndrome, .Lloopcmp_proc
+ cbz syndrome, L(loop_misaligned)
+ b L(end)

-.Lcal_cmpresult:
- /*
- * reversed the byte-order as big-endian,then CLZ can find the most
- * significant zero bits.
- */
-CPU_LE( rev syndrome, syndrome )
-CPU_LE( rev data1, data1 )
-CPU_LE( rev data2, data2 )
-
- /*
- * For big-endian we cannot use the trick with the syndrome value
- * as carry-propagation can corrupt the upper bits if the trailing
- * bytes in the string contain 0x01.
- * However, if there is no NUL byte in the dword, we can generate
- * the result directly. We cannot just subtract the bytes as the
- * MSB might be significant.
- */
-CPU_BE( cbnz has_nul, 1f )
-CPU_BE( cmp data1, data2 )
-CPU_BE( cset result, ne )
-CPU_BE( cneg result, result, lo )
-CPU_BE( ret )
-CPU_BE( 1: )
- /*Re-compute the NUL-byte detection, using a byte-reversed value. */
-CPU_BE( rev tmp3, data1 )
-CPU_BE( sub tmp1, tmp3, zeroones )
-CPU_BE( orr tmp2, tmp3, #REP8_7f )
-CPU_BE( bic has_nul, tmp1, tmp2 )
-CPU_BE( rev has_nul, has_nul )
-CPU_BE( orr syndrome, diff, has_nul )
-
- clz pos, syndrome
- /*
- * The MS-non-zero bit of the syndrome marks either the first bit
- * that is different, or the top bit of the first zero byte.
- * Shifting left now will bring the critical information into the
- * top bits.
- */
- lsl data1, data1, pos
- lsl data2, data2, pos
- /*
- * But we need to zero-extend (char is unsigned) the value and then
- * perform a signed 32-bit subtraction.
- */
- lsr data1, data1, #56
- sub result, data1, data2, lsr #56
+L(done):
+ sub result, data1, data2
ret
SYM_FUNC_END_PI(strcmp)
EXPORT_SYMBOL_NOKASAN(strcmp)
--
2.17.1

2020-05-14 14:34:52

by Oliver Swede

[permalink] [raw]
Subject: [PATCH v3 08/13] arm64: Import latest optimization of memcpy

From: Sam Tebbs <[email protected]>

Import the latest memcpy implementation into memcpy,
copy_{from, to and in}_user.
The implementation of the user routines is separated into two forms:
one for when UAO is enabled and one for when UAO is disabled, with
the two being chosen between with a runtime patch.
This avoids executing the many NOPs emitted when UAO is disabled.

The project containing optimized implementations for various library
functions has now been renamed from 'cortex-strings' to
'optimized-routines', and the new upstream source is
string/aarch64/memcpy.S as of commit 4c175c8be12 in
https://github.com/ARM-software/optimized-routines.

Signed-off-by: Sam Tebbs <[email protected]>
[ rm: add UAO fixups, streamline copy_exit paths, expand commit message ]
Signed-off-by: Robin Murphy <[email protected]>
[ os: import newer memcpy algorithm, replace innaccurate fixup routine
with placeholder, update commit message ]
Signed-off-by: Oliver Swede <[email protected]>
---
arch/arm64/include/asm/alternative.h | 36 ---
arch/arm64/lib/copy_from_user.S | 115 ++++++--
arch/arm64/lib/copy_in_user.S | 130 ++++++++--
arch/arm64/lib/copy_template.S | 375 +++++++++++++++------------
arch/arm64/lib/copy_template_user.S | 24 ++
arch/arm64/lib/copy_to_user.S | 113 ++++++--
arch/arm64/lib/copy_user_fixup.S | 9 +
arch/arm64/lib/memcpy.S | 48 ++--
8 files changed, 557 insertions(+), 293 deletions(-)
create mode 100644 arch/arm64/lib/copy_template_user.S
create mode 100644 arch/arm64/lib/copy_user_fixup.S

diff --git a/arch/arm64/include/asm/alternative.h b/arch/arm64/include/asm/alternative.h
index 5e5dc05d63a0..7ab752104170 100644
--- a/arch/arm64/include/asm/alternative.h
+++ b/arch/arm64/include/asm/alternative.h
@@ -230,36 +230,6 @@ alternative_endif
* unprivileged instructions, and USER() only works for single instructions.
*/
#ifdef CONFIG_ARM64_UAO
- .macro uao_ldp l, reg1, reg2, addr, post_inc
- alternative_if_not ARM64_HAS_UAO
-8888: ldp \reg1, \reg2, [\addr], \post_inc;
-8889: nop;
- nop;
- alternative_else
- ldtr \reg1, [\addr];
- ldtr \reg2, [\addr, #8];
- add \addr, \addr, \post_inc;
- alternative_endif
-
- _asm_extable 8888b,\l;
- _asm_extable 8889b,\l;
- .endm
-
- .macro uao_stp l, reg1, reg2, addr, post_inc
- alternative_if_not ARM64_HAS_UAO
-8888: stp \reg1, \reg2, [\addr], \post_inc;
-8889: nop;
- nop;
- alternative_else
- sttr \reg1, [\addr];
- sttr \reg2, [\addr, #8];
- add \addr, \addr, \post_inc;
- alternative_endif
-
- _asm_extable 8888b,\l;
- _asm_extable 8889b,\l;
- .endm
-
.macro uao_user_alternative l, inst, alt_inst, reg, addr, post_inc
alternative_if_not ARM64_HAS_UAO
8888: \inst \reg, [\addr], \post_inc;
@@ -272,12 +242,6 @@ alternative_endif
_asm_extable 8888b,\l;
.endm
#else
- .macro uao_ldp l, reg1, reg2, addr, post_inc
- USER(\l, ldp \reg1, \reg2, [\addr], \post_inc)
- .endm
- .macro uao_stp l, reg1, reg2, addr, post_inc
- USER(\l, stp \reg1, \reg2, [\addr], \post_inc)
- .endm
.macro uao_user_alternative l, inst, alt_inst, reg, addr, post_inc
USER(\l, \inst \reg, [\addr], \post_inc)
.endm
diff --git a/arch/arm64/lib/copy_from_user.S b/arch/arm64/lib/copy_from_user.S
index 8e25e89ad01f..dbf768cc7650 100644
--- a/arch/arm64/lib/copy_from_user.S
+++ b/arch/arm64/lib/copy_from_user.S
@@ -20,49 +20,112 @@
* x0 - bytes not copied
*/

- .macro ldrb1 ptr, regB, val
- uao_user_alternative 9998f, ldrb, ldtrb, \ptr, \regB, \val
+ .macro ldrb1 reg, ptr, offset=0
+ 8888: ldtrb \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
.endm

- .macro strb1 ptr, regB, val
- strb \ptr, [\regB], \val
+ .macro strb1 reg, ptr, offset=0
+ strb \reg, [\ptr, \offset]
.endm

- .macro ldrh1 ptr, regB, val
- uao_user_alternative 9998f, ldrh, ldtrh, \ptr, \regB, \val
+ .macro ldrb1_reg reg, ptr, offset
+ add \ptr, \ptr, \offset
+ 8888: ldtrb \reg, [\ptr]
+ sub \ptr, \ptr, \offset
+ _asm_extable_faultaddr 8888b,9998f;
.endm

- .macro strh1 ptr, regB, val
- strh \ptr, [\regB], \val
+ .macro strb1_reg reg, ptr, offset
+ strb \reg, [\ptr, \offset]
.endm

- .macro ldr1 ptr, regB, val
- uao_user_alternative 9998f, ldr, ldtr, \ptr, \regB, \val
+ .macro ldr1 reg, ptr, offset=0
+ 8888: ldtr \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
.endm

- .macro str1 ptr, regB, val
- str \ptr, [\regB], \val
+ .macro str1 reg, ptr, offset=0
+ str \reg, [\ptr, \offset]
.endm

- .macro ldp1 ptr, regB, regC, val
- uao_ldp 9998f, \ptr, \regB, \regC, \val
+ .macro ldp1 regA, regB, ptr, offset=0
+ 8888: ldtr \regA, [\ptr, \offset]
+ 8889: ldtr \regB, [\ptr, \offset + 8]
+ _asm_extable_faultaddr 8888b,9998f;
+ _asm_extable_faultaddr 8889b,9998f;
.endm

- .macro stp1 ptr, regB, regC, val
- stp \ptr, \regB, [\regC], \val
+ .macro stp1 regA, regB, ptr, offset=0
+ stp \regA, \regB, [\ptr, \offset]
+ .endm
+
+ .macro ldp1_pre regA, regB, ptr, offset
+ 8888: ldtr \regA, [\ptr, \offset]
+ 8889: ldtr \regB, [\ptr, \offset + 8]
+ add \ptr, \ptr, \offset
+ _asm_extable_faultaddr 8888b,9998f;
+ _asm_extable_faultaddr 8889b,9998f;
+ .endm
+
+ .macro stp1_pre regA, regB, ptr, offset
+ stp \regA, \regB, [\ptr, \offset]!
+ .endm
+
+ .macro ldrb1_nuao reg, ptr, offset=0
+ 8888: ldrb \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro strb1_nuao reg, ptr, offset=0
+ strb \reg, [\ptr, \offset]
+ .endm
+
+ .macro ldrb1_nuao_reg reg, ptr, offset=0
+ 8888: ldrb \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro strb1_nuao_reg reg, ptr, offset=0
+ strb \reg, [\ptr, \offset]
+ .endm
+
+ .macro ldr1_nuao reg, ptr, offset=0
+ 8888: ldr \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro str1_nuao reg, ptr, offset=0
+ str \reg, [\ptr, \offset]
+ .endm
+
+ .macro ldp1_nuao regA, regB, ptr, offset=0
+ 8888: ldp \regA, \regB, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro stp1_nuao regA, regB, ptr, offset=0
+ stp \regA, \regB, [\ptr, \offset]
+ .endm
+
+ .macro ldp1_pre_nuao regA, regB, ptr, offset
+ 8888: ldp \regA, \regB, [\ptr, \offset]!
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro stp1_pre_nuao regA, regB, ptr, offset
+ stp \regA, \regB, [\ptr, \offset]!
+ .endm
+
+ .macro copy_exit
+ b .Luaccess_finish
.endm

-end .req x5
SYM_FUNC_START(__arch_copy_from_user)
- add end, x0, x2
-#include "copy_template.S"
- mov x0, #0 // Nothing to copy
+#include "copy_template_user.S"
+.Luaccess_finish:
+ mov x0, #0
ret
SYM_FUNC_END(__arch_copy_from_user)
EXPORT_SYMBOL(__arch_copy_from_user)
-
- .section .fixup,"ax"
- .align 2
-9998: sub x0, end, dst // bytes not copied
- ret
- .previous
+#include "copy_user_fixup.S"
diff --git a/arch/arm64/lib/copy_in_user.S b/arch/arm64/lib/copy_in_user.S
index 667139013ed1..f08d4b36a857 100644
--- a/arch/arm64/lib/copy_in_user.S
+++ b/arch/arm64/lib/copy_in_user.S
@@ -21,50 +21,130 @@
* Returns:
* x0 - bytes not copied
*/
- .macro ldrb1 ptr, regB, val
- uao_user_alternative 9998f, ldrb, ldtrb, \ptr, \regB, \val
+
+ .macro ldrb1 reg, ptr, offset=0
+ 8888: ldtrb \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro strb1 reg, ptr, offset=0
+ 8888: sttrb \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro ldrb1_reg reg, ptr, offset
+ add \ptr, \ptr, \offset
+ 8888: ldtrb \reg, [\ptr]
+ sub \ptr, \ptr, \offset
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro strb1_reg reg, ptr, offset
+ add \ptr, \ptr, \offset
+ 8888: sttrb \reg, [\ptr]
+ sub \ptr, \ptr, \offset
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro ldr1 reg, ptr, offset=0
+ 8888: ldtr \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro str1 reg, ptr, offset=0
+ 8888: sttr \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro ldp1 regA, regB, ptr, offset=0
+ 8888: ldtr \regA, [\ptr, \offset]
+ 8889: ldtr \regB, [\ptr, \offset + 8]
+ _asm_extable_faultaddr 8888b,9998f;
+ _asm_extable_faultaddr 8889b,9998f;
+ .endm
+
+ .macro stp1 regA, regB, ptr, offset=0
+ 8888: sttr \regA, [\ptr, \offset]
+ 8889: sttr \regB, [\ptr, \offset + 8]
+ _asm_extable_faultaddr 8888b,9998f;
+ _asm_extable_faultaddr 8889b,9998f;
+ .endm
+
+ .macro ldp1_pre regA, regB, ptr, offset
+ 8888: ldtr \regA, [\ptr, \offset]
+ 8889: ldtr \regB, [\ptr, \offset + 8]
+ add \ptr, \ptr, \offset
+ _asm_extable_faultaddr 8888b,9998f;
+ _asm_extable_faultaddr 8889b,9998f;
+ .endm
+
+ .macro stp1_pre regA, regB, ptr, offset
+ 8888: sttr \regA, [\ptr, \offset]
+ 8889: sttr \regB, [\ptr, \offset + 8]
+ add \ptr, \ptr, \offset
+ _asm_extable_faultaddr 8888b,9998f;
+ _asm_extable_faultaddr 8889b,9998f;
+ .endm
+
+ .macro ldrb1_nuao reg, ptr, offset=0
+ 8888: ldrb \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
.endm

- .macro strb1 ptr, regB, val
- uao_user_alternative 9998f, strb, sttrb, \ptr, \regB, \val
+ .macro strb1_nuao reg, ptr, offset=0
+ 8888: strb \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
.endm

- .macro ldrh1 ptr, regB, val
- uao_user_alternative 9998f, ldrh, ldtrh, \ptr, \regB, \val
+ .macro ldrb1_nuao_reg reg, ptr, offset=0
+ 8888: ldrb \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
.endm

- .macro strh1 ptr, regB, val
- uao_user_alternative 9998f, strh, sttrh, \ptr, \regB, \val
+ .macro strb1_nuao_reg reg, ptr, offset=0
+ 8888: strb \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
.endm

- .macro ldr1 ptr, regB, val
- uao_user_alternative 9998f, ldr, ldtr, \ptr, \regB, \val
+ .macro ldr1_nuao reg, ptr, offset=0
+ 8888: ldr \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
.endm

- .macro str1 ptr, regB, val
- uao_user_alternative 9998f, str, sttr, \ptr, \regB, \val
+ .macro str1_nuao reg, ptr, offset=0
+ 8888: str \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
.endm

- .macro ldp1 ptr, regB, regC, val
- uao_ldp 9998f, \ptr, \regB, \regC, \val
+ .macro ldp1_nuao regA, regB, ptr, offset=0
+ 8888: ldp \regA, \regB, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
.endm

- .macro stp1 ptr, regB, regC, val
- uao_stp 9998f, \ptr, \regB, \regC, \val
+ .macro stp1_nuao regA, regB, ptr, offset=0
+ 8888: stp \regA, \regB, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
.endm

-end .req x5
+ .macro ldp1_pre_nuao regA, regB, ptr, offset
+ 8888: ldp \regA, \regB, [\ptr, \offset]!
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro stp1_pre_nuao regA, regB, ptr, offset
+ 8888: stp \regA, \regB, [\ptr, \offset]!
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro copy_exit
+ b .Luaccess_finish
+ .endm

SYM_FUNC_START(__arch_copy_in_user)
- add end, x0, x2
-#include "copy_template.S"
+#include "copy_template_user.S"
+.Luaccess_finish:
mov x0, #0
ret
SYM_FUNC_END(__arch_copy_in_user)
EXPORT_SYMBOL(__arch_copy_in_user)
-
- .section .fixup,"ax"
- .align 2
-9998: sub x0, end, dst // bytes not copied
- ret
- .previous
+#include "copy_user_fixup.S"
diff --git a/arch/arm64/lib/copy_template.S b/arch/arm64/lib/copy_template.S
index 488df234c49a..90b5f63ff227 100644
--- a/arch/arm64/lib/copy_template.S
+++ b/arch/arm64/lib/copy_template.S
@@ -1,13 +1,12 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/*
- * Copyright (C) 2013 ARM Ltd.
- * Copyright (C) 2013 Linaro.
+ * Copyright (c) 2012 Linaro Limited. All rights reserved.
+ * Copyright (c) 2015 ARM Ltd. All rights reserved.
*
- * This code is based on glibc cortex strings work originally authored by Linaro
- * be found @
+ * This code is based on work originally authored by Linaro,
+ * found at:
*
- * http://bazaar.launchpad.net/~linaro-toolchain-dev/cortex-strings/trunk/
- * files/head:/src/aarch64/
+ * https://github.com/ARM-software/optimized-routines
*/


@@ -21,161 +20,209 @@
* Returns:
* x0 - dest
*/
-dstin .req x0
-src .req x1
-count .req x2
-tmp1 .req x3
-tmp1w .req w3
-tmp2 .req x4
-tmp2w .req w4
-dst .req x6
-
-A_l .req x7
-A_h .req x8
-B_l .req x9
-B_h .req x10
-C_l .req x11
-C_h .req x12
-D_l .req x13
-D_h .req x14
-
- mov dst, dstin
- cmp count, #16
- /*When memory length is less than 16, the accessed are not aligned.*/
- b.lo .Ltiny15
-
- neg tmp2, src
- ands tmp2, tmp2, #15/* Bytes to reach alignment. */
- b.eq .LSrcAligned
- sub count, count, tmp2
- /*
- * Copy the leading memory data from src to dst in an increasing
- * address order.By this way,the risk of overwriting the source
- * memory data is eliminated when the distance between src and
- * dst is less than 16. The memory accesses here are alignment.
- */
- tbz tmp2, #0, 1f
- ldrb1 tmp1w, src, #1
- strb1 tmp1w, dst, #1
-1:
- tbz tmp2, #1, 2f
- ldrh1 tmp1w, src, #2
- strh1 tmp1w, dst, #2
-2:
- tbz tmp2, #2, 3f
- ldr1 tmp1w, src, #4
- str1 tmp1w, dst, #4
-3:
- tbz tmp2, #3, .LSrcAligned
- ldr1 tmp1, src, #8
- str1 tmp1, dst, #8
-
-.LSrcAligned:
- cmp count, #64
- b.ge .Lcpy_over64
- /*
- * Deal with small copies quickly by dropping straight into the
- * exit block.
- */
-.Ltail63:
- /*
- * Copy up to 48 bytes of data. At this point we only need the
- * bottom 6 bits of count to be accurate.
- */
- ands tmp1, count, #0x30
- b.eq .Ltiny15
- cmp tmp1w, #0x20
- b.eq 1f
- b.lt 2f
- ldp1 A_l, A_h, src, #16
- stp1 A_l, A_h, dst, #16
-1:
- ldp1 A_l, A_h, src, #16
- stp1 A_l, A_h, dst, #16
-2:
- ldp1 A_l, A_h, src, #16
- stp1 A_l, A_h, dst, #16
-.Ltiny15:
- /*
- * Prefer to break one ldp/stp into several load/store to access
- * memory in an increasing address order,rather than to load/store 16
- * bytes from (src-16) to (dst-16) and to backward the src to aligned
- * address,which way is used in original cortex memcpy. If keeping
- * the original memcpy process here, memmove need to satisfy the
- * precondition that src address is at least 16 bytes bigger than dst
- * address,otherwise some source data will be overwritten when memove
- * call memcpy directly. To make memmove simpler and decouple the
- * memcpy's dependency on memmove, withdrew the original process.
- */
- tbz count, #3, 1f
- ldr1 tmp1, src, #8
- str1 tmp1, dst, #8
-1:
- tbz count, #2, 2f
- ldr1 tmp1w, src, #4
- str1 tmp1w, dst, #4
-2:
- tbz count, #1, 3f
- ldrh1 tmp1w, src, #2
- strh1 tmp1w, dst, #2
-3:
- tbz count, #0, .Lexitfunc
- ldrb1 tmp1w, src, #1
- strb1 tmp1w, dst, #1
-
- b .Lexitfunc
-
-.Lcpy_over64:
- subs count, count, #128
- b.ge .Lcpy_body_large
- /*
- * Less than 128 bytes to copy, so handle 64 here and then jump
- * to the tail.
- */
- ldp1 A_l, A_h, src, #16
- stp1 A_l, A_h, dst, #16
- ldp1 B_l, B_h, src, #16
- ldp1 C_l, C_h, src, #16
- stp1 B_l, B_h, dst, #16
- stp1 C_l, C_h, dst, #16
- ldp1 D_l, D_h, src, #16
- stp1 D_l, D_h, dst, #16
-
- tst count, #0x3f
- b.ne .Ltail63
- b .Lexitfunc
-
- /*
- * Critical loop. Start at a new cache line boundary. Assuming
- * 64 bytes per line this ensures the entire loop is in one line.
- */
- .p2align L1_CACHE_SHIFT
-.Lcpy_body_large:
- /* pre-get 64 bytes data. */
- ldp1 A_l, A_h, src, #16
- ldp1 B_l, B_h, src, #16
- ldp1 C_l, C_h, src, #16
- ldp1 D_l, D_h, src, #16
-1:
- /*
- * interlace the load of next 64 bytes data block with store of the last
- * loaded 64 bytes data.
- */
- stp1 A_l, A_h, dst, #16
- ldp1 A_l, A_h, src, #16
- stp1 B_l, B_h, dst, #16
- ldp1 B_l, B_h, src, #16
- stp1 C_l, C_h, dst, #16
- ldp1 C_l, C_h, src, #16
- stp1 D_l, D_h, dst, #16
- ldp1 D_l, D_h, src, #16
- subs count, count, #64
- b.ge 1b
- stp1 A_l, A_h, dst, #16
- stp1 B_l, B_h, dst, #16
- stp1 C_l, C_h, dst, #16
- stp1 D_l, D_h, dst, #16
-
- tst count, #0x3f
- b.ne .Ltail63
-.Lexitfunc:
+ #define dstin x0
+ #define src x1
+ #define count x2
+ #define dst x3
+ #define srcend x4
+ #define dstend x5
+ #define A_l x6
+ #define A_lw w6
+ #define A_h x7
+ #define B_l x8
+ #define B_lw w8
+ #define B_h x9
+ #define C_l x10
+ #define C_lw w10
+ #define C_h x11
+ #define D_l x12
+ #define D_h x13
+ #define E_l x14
+ #define E_h x15
+ #define F_l x16
+ #define F_h x17
+ #define G_l count
+ #define G_h dst
+ #define H_l src
+ #define H_h srcend
+ #define tmp1 x14
+
+ add srcend, src, count
+ add dstend, dstin, count
+ cmp count, 128
+ b.hi L(copy_long)
+ cmp count, 32
+ b.hi L(copy32_128)
+
+ /* Small copies: 0..32 bytes. */
+ cmp count, 16
+ b.lo L(copy16)
+ ldp1 A_l, A_h, src
+ ldp1 D_l, D_h, srcend, -16
+ stp1 A_l, A_h, dstin
+ stp1 D_l, D_h, dstend, -16
+ copy_exit
+
+ /* Copy 8-15 bytes. */
+L(copy16):
+ tbz count, 3, L(copy8)
+ ldr1 A_l, src
+ ldr1 A_h, srcend, -8
+ str1 A_l, dstin
+ str1 A_h, dstend, -8
+ copy_exit
+
+ .p2align 3
+ /* Copy 4-7 bytes. */
+L(copy8):
+ tbz count, 2, L(copy4)
+ ldr1 A_lw, src
+ ldr1 B_lw, srcend, -4
+ str1 A_lw, dstin
+ str1 B_lw, dstend, -4
+ copy_exit
+
+ /* Copy 0..3 bytes using a branchless sequence. */
+L(copy4):
+ cbz count, L(copy0)
+ lsr tmp1, count, 1
+ ldrb1 A_lw, src
+ ldrb1 C_lw, srcend, -1
+ ldrb1_reg B_lw, src, tmp1
+ strb1 A_lw, dstin
+ strb1_reg B_lw, dstin, tmp1
+ strb1 C_lw, dstend, -1
+L(copy0):
+ copy_exit
+
+ .p2align 4
+ /* Medium copies: 33..128 bytes. */
+L(copy32_128):
+ ldp1 A_l, A_h, src
+ ldp1 B_l, B_h, src, 16
+ ldp1 C_l, C_h, srcend, -32
+ ldp1 D_l, D_h, srcend, -16
+ cmp count, 64
+ b.hi L(copy128)
+ stp1 A_l, A_h, dstin
+ stp1 B_l, B_h, dstin, 16
+ stp1 C_l, C_h, dstend, -32
+ stp1 D_l, D_h, dstend, -16
+ copy_exit
+
+ .p2align 4
+ /* Copy 65..128 bytes. */
+L(copy128):
+ ldp1 E_l, E_h, src, 32
+ ldp1 F_l, F_h, src, 48
+ cmp count, 96
+ b.ls L(copy96)
+ ldp1 G_l, G_h, srcend, -64
+ ldp1 H_l, H_h, srcend, -48
+ stp1 G_l, G_h, dstend, -64
+ stp1 H_l, H_h, dstend, -48
+L(copy96):
+ stp1 A_l, A_h, dstin
+ stp1 B_l, B_h, dstin, 16
+ stp1 E_l, E_h, dstin, 32
+ stp1 F_l, F_h, dstin, 48
+ stp1 C_l, C_h, dstend, -32
+ stp1 D_l, D_h, dstend, -16
+ copy_exit
+
+ .p2align 4
+ /* Copy more than 128 bytes. */
+L(copy_long):
+ /* Use backwards copy if there is an overlap. */
+ sub tmp1, dstin, src
+ cbz tmp1, L(copy0)
+ cmp tmp1, count
+ b.lo L(copy_long_backwards)
+
+ /* Copy 16 bytes and then align dst to 16-byte alignment. */
+
+ ldp1 D_l, D_h, src
+ and tmp1, dstin, 15
+ bic dst, dstin, 15
+ sub src, src, tmp1
+ add count, count, tmp1 /* Count is now 16 too large. */
+ ldp1 A_l, A_h, src, 16
+ stp1 D_l, D_h, dstin
+ ldp1 B_l, B_h, src, 32
+ ldp1 C_l, C_h, src, 48
+ ldp1_pre D_l, D_h, src, 64
+ subs count, count, 128 + 16 /* Test and readjust count. */
+ b.ls L(copy64_from_end)
+
+L(loop64):
+ stp1 A_l, A_h, dst, 16
+ ldp1 A_l, A_h, src, 16
+ stp1 B_l, B_h, dst, 32
+ ldp1 B_l, B_h, src, 32
+ stp1 C_l, C_h, dst, 48
+ ldp1 C_l, C_h, src, 48
+ stp1_pre D_l, D_h, dst, 64
+ ldp1_pre D_l, D_h, src, 64
+ subs count, count, 64
+ b.hi L(loop64)
+
+ /* Write the last iteration and copy 64 bytes from the end. */
+L(copy64_from_end):
+ ldp1 E_l, E_h, srcend, -64
+ stp1 A_l, A_h, dst, 16
+ ldp1 A_l, A_h, srcend, -48
+ stp1 B_l, B_h, dst, 32
+ ldp1 B_l, B_h, srcend, -32
+ stp1 C_l, C_h, dst, 48
+ ldp1 C_l, C_h, srcend, -16
+ stp1 D_l, D_h, dst, 64
+ stp1 E_l, E_h, dstend, -64
+ stp1 A_l, A_h, dstend, -48
+ stp1 B_l, B_h, dstend, -32
+ stp1 C_l, C_h, dstend, -16
+ copy_exit
+
+ .p2align 4
+ /* Large backwards copy for overlapping copies.
+ Copy 16 bytes and then align dst to 16-byte alignment. */
+L(copy_long_backwards):
+ ldp1 D_l, D_h, srcend, -16
+ and tmp1, dstend, 15
+ sub srcend, srcend, tmp1
+ sub count, count, tmp1
+ ldp1 A_l, A_h, srcend, -16
+ stp1 D_l, D_h, dstend, -16
+ ldp1 B_l, B_h, srcend, -32
+ ldp1 C_l, C_h, srcend, -48
+ ldp1_pre D_l, D_h, srcend, -64
+ sub dstend, dstend, tmp1
+ subs count, count, 128
+ b.ls L(copy64_from_start)
+
+L(loop64_backwards):
+ stp1 A_l, A_h, dstend, -16
+ ldp1 A_l, A_h, srcend, -16
+ stp1 B_l, B_h, dstend, -32
+ ldp1 B_l, B_h, srcend, -32
+ stp1 C_l, C_h, dstend, -48
+ ldp1 C_l, C_h, srcend, -48
+ stp1_pre D_l, D_h, dstend, -64
+ ldp1_pre D_l, D_h, srcend, -64
+ subs count, count, 64
+ b.hi L(loop64_backwards)
+
+ /* Write the last iteration and copy 64 bytes from the start. */
+L(copy64_from_start):
+ ldp1 G_l, G_h, src, 48
+ stp1 A_l, A_h, dstend, -16
+ ldp1 A_l, A_h, src, 32
+ stp1 B_l, B_h, dstend, -32
+ ldp1 B_l, B_h, src, 16
+ stp1 C_l, C_h, dstend, -48
+ ldp1 C_l, C_h, src
+ stp1 D_l, D_h, dstend, -64
+ stp1 G_l, G_h, dstin, 48
+ stp1 A_l, A_h, dstin, 32
+ stp1 B_l, B_h, dstin, 16
+ stp1 C_l, C_h, dstin
+ copy_exit
diff --git a/arch/arm64/lib/copy_template_user.S b/arch/arm64/lib/copy_template_user.S
new file mode 100644
index 000000000000..3db24dcdab05
--- /dev/null
+++ b/arch/arm64/lib/copy_template_user.S
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#define L(l) .L ## l
+
+ alternative_if_not ARM64_HAS_UAO
+ b L(copy_non_uao)
+ alternative_else_nop_endif
+#include "copy_template.S"
+
+#define ldp1 ldp1_nuao
+#define ldp1_pre ldp1_pre_nuao
+#define stp1 stp1_nuao
+#define stp1_pre stp1_pre_nuao
+#define ldr1 ldr1_nuao
+#define str1 str1_nuao
+#define ldrb1 ldrb1_nuao
+#define strb1 strb1_nuao
+#define ldrb1_reg ldrb1_nuao_reg
+#define strb1_reg strb1_nuao_reg
+
+L(copy_non_uao):
+#undef L
+#define L(l) .Lnuao ## l
+#include "copy_template.S"
diff --git a/arch/arm64/lib/copy_to_user.S b/arch/arm64/lib/copy_to_user.S
index 1a104d0089f3..e4629c83abb4 100644
--- a/arch/arm64/lib/copy_to_user.S
+++ b/arch/arm64/lib/copy_to_user.S
@@ -19,49 +19,112 @@
* Returns:
* x0 - bytes not copied
*/
- .macro ldrb1 ptr, regB, val
- ldrb \ptr, [\regB], \val
+
+ .macro ldrb1 reg, ptr, offset=0
+ ldrb \reg, [\ptr, \offset]
+ .endm
+
+ .macro strb1 reg, ptr, offset=0
+ 8888: sttrb \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro ldrb1_reg reg, ptr, offset
+ ldrb \reg, [\ptr, \offset]
+ .endm
+
+ .macro strb1_reg reg, ptr, offset
+ add \ptr, \ptr, \offset
+ 8888: sttrb \reg, [\ptr]
+ sub \ptr, \ptr, \offset
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro ldr1 reg, ptr, offset=0
+ ldr \reg, [\ptr, \offset]
+ .endm
+
+ .macro str1 reg, ptr, offset=0
+ 8888: sttr \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro ldp1 regA, regB, ptr, offset=0
+ ldp \regA, \regB, [\ptr, \offset]
+ .endm
+
+ .macro stp1 regA, regB, ptr, offset=0
+ 8888: sttr \regA, [\ptr, \offset]
+ 8889: sttr \regB, [\ptr, \offset + 8]
+ _asm_extable_faultaddr 8888b,9998f;
+ _asm_extable_faultaddr 8889b,9998f;
.endm

- .macro strb1 ptr, regB, val
- uao_user_alternative 9998f, strb, sttrb, \ptr, \regB, \val
+ .macro ldp1_pre regA, regB, ptr, offset
+ ldp \regA, \regB, [\ptr, \offset]!
.endm

- .macro ldrh1 ptr, regB, val
- ldrh \ptr, [\regB], \val
+ .macro stp1_pre regA, regB, ptr, offset
+ 8888: sttr \regA, [\ptr, \offset]
+ 8889: sttr \regB, [\ptr, \offset + 8]
+ add \ptr, \ptr, \offset
+ _asm_extable_faultaddr 8888b,9998f;
+ _asm_extable_faultaddr 8889b,9998f;
.endm

- .macro strh1 ptr, regB, val
- uao_user_alternative 9998f, strh, sttrh, \ptr, \regB, \val
+ .macro ldrb1_nuao reg, ptr, offset=0
+ ldrb \reg, [\ptr, \offset]
.endm

- .macro ldr1 ptr, regB, val
- ldr \ptr, [\regB], \val
+ .macro strb1_nuao reg, ptr, offset=0
+ 8888: strb \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
.endm

- .macro str1 ptr, regB, val
- uao_user_alternative 9998f, str, sttr, \ptr, \regB, \val
+ .macro ldrb1_nuao_reg reg, ptr, offset=0
+ ldrb \reg, [\ptr, \offset]
.endm

- .macro ldp1 ptr, regB, regC, val
- ldp \ptr, \regB, [\regC], \val
+ .macro strb1_nuao_reg reg, ptr, offset=0
+ strb \reg, [\ptr, \offset]
.endm

- .macro stp1 ptr, regB, regC, val
- uao_stp 9998f, \ptr, \regB, \regC, \val
+ .macro ldr1_nuao reg, ptr, offset=0
+ ldr \reg, [\ptr, \offset]
+ .endm
+
+ .macro str1_nuao reg, ptr, offset=0
+ 8888: str \reg, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro ldp1_nuao regA, regB, ptr, offset=0
+ ldp \regA, \regB, [\ptr, \offset]
+ .endm
+
+ .macro ldp1_pre_nuao regA, regB, ptr, offset
+ ldp \regA, \regB, [\ptr, \offset]!
+ .endm
+
+ .macro stp1_nuao regA, regB, ptr, offset=0
+ 8888: stp \regA, \regB, [\ptr, \offset]
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro stp1_pre_nuao regA, regB, ptr, offset
+ 8888: stp \regA, \regB, [\ptr, \offset]!
+ _asm_extable_faultaddr 8888b,9998f;
+ .endm
+
+ .macro copy_exit
+ b .Luaccess_finish
.endm

-end .req x5
SYM_FUNC_START(__arch_copy_to_user)
- add end, x0, x2
-#include "copy_template.S"
+#include "copy_template_user.S"
+.Luaccess_finish:
mov x0, #0
ret
SYM_FUNC_END(__arch_copy_to_user)
EXPORT_SYMBOL(__arch_copy_to_user)
-
- .section .fixup,"ax"
- .align 2
-9998: sub x0, end, dst // bytes not copied
- ret
- .previous
+#include "copy_user_fixup.S"
diff --git a/arch/arm64/lib/copy_user_fixup.S b/arch/arm64/lib/copy_user_fixup.S
new file mode 100644
index 000000000000..117c37598691
--- /dev/null
+++ b/arch/arm64/lib/copy_user_fixup.S
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+addr .req x15
+.section .fixup,"ax"
+.align 2
+9998:
+ // TODO: add accurate fixup
+ ret
+
diff --git a/arch/arm64/lib/memcpy.S b/arch/arm64/lib/memcpy.S
index 9f382adfa88a..ee84b8847184 100644
--- a/arch/arm64/lib/memcpy.S
+++ b/arch/arm64/lib/memcpy.S
@@ -24,43 +24,57 @@
* Returns:
* x0 - dest
*/
- .macro ldrb1 ptr, regB, val
- ldrb \ptr, [\regB], \val
+
+ #define L(l) .L ## l
+
+ .macro ldrb1 reg, ptr, offset=0
+ ldrb \reg, [\ptr, \offset]
+ .endm
+
+ .macro strb1 reg, ptr, offset=0
+ strb \reg, [\ptr, \offset]
+ .endm
+
+ .macro ldr1 reg, ptr, offset=0
+ ldr \reg, [\ptr, \offset]
.endm

- .macro strb1 ptr, regB, val
- strb \ptr, [\regB], \val
+ .macro str1 reg, ptr, offset=0
+ str \reg, [\ptr, \offset]
.endm

- .macro ldrh1 ptr, regB, val
- ldrh \ptr, [\regB], \val
+ .macro ldp1 regA, regB, ptr, offset=0
+ ldp \regA, \regB, [\ptr, \offset]
.endm

- .macro strh1 ptr, regB, val
- strh \ptr, [\regB], \val
+ .macro stp1 regA, regB, ptr, offset=0
+ stp \regA, \regB, [\ptr, \offset]
.endm

- .macro ldr1 ptr, regB, val
- ldr \ptr, [\regB], \val
+ .macro ldrb1_reg reg, ptr, offset
+ ldrb1 \reg, \ptr, \offset
.endm

- .macro str1 ptr, regB, val
- str \ptr, [\regB], \val
+ .macro strb1_reg reg, ptr, offset
+ strb1 \reg, \ptr, \offset
.endm

- .macro ldp1 ptr, regB, regC, val
- ldp \ptr, \regB, [\regC], \val
+ .macro ldp1_pre regA, regB, ptr, offset
+ ldp \regA, \regB, [\ptr, \offset]!
.endm

- .macro stp1 ptr, regB, regC, val
- stp \ptr, \regB, [\regC], \val
+ .macro stp1_pre regA, regB, ptr, offset
+ stp \regA, \regB, [\ptr, \offset]!
+ .endm
+
+ .macro copy_exit
+ ret
.endm

.weak memcpy
SYM_FUNC_START_ALIAS(__memcpy)
SYM_FUNC_START_PI(memcpy)
#include "copy_template.S"
- ret
SYM_FUNC_END_PI(memcpy)
EXPORT_SYMBOL(memcpy)
SYM_FUNC_END_ALIAS(__memcpy)
--
2.17.1

2020-05-14 14:34:57

by Oliver Swede

[permalink] [raw]
Subject: [PATCH v3 07/13] arm64: Import latest version of Cortex Strings' strncmp

From: Sam Tebbs <[email protected]>

Import latest version of Cortex Strings' strncmp function.

The upstream source is src/aarch64/strncmp.S as of commit 071fe283b28d
in https://git.linaro.org/toolchain/cortex-strings.git.

Signed-off-by: Sam Tebbs <[email protected]>
[ rm: update attribution, expand commit message ]
Signed-off-by: Robin Murphy <[email protected]>
Signed-off-by: Oliver Swede <[email protected]>
---
arch/arm64/lib/strncmp.S | 363 ++++++++++++++++++---------------------
1 file changed, 163 insertions(+), 200 deletions(-)

diff --git a/arch/arm64/lib/strncmp.S b/arch/arm64/lib/strncmp.S
index 2a7ee949ed47..b954e0fd93be 100644
--- a/arch/arm64/lib/strncmp.S
+++ b/arch/arm64/lib/strncmp.S
@@ -1,13 +1,11 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/*
- * Copyright (C) 2013 ARM Ltd.
- * Copyright (C) 2013 Linaro.
+ * Copyright (c) 2013,2018 Linaro Limited. All rights reserved.
*
- * This code is based on glibc cortex strings work originally authored by Linaro
- * be found @
+ * This code is based on glibc Cortex Strings work originally authored by
+ * Linaro, found at:
*
- * http://bazaar.launchpad.net/~linaro-toolchain-dev/cortex-strings/trunk/
- * files/head:/src/aarch64/
+ * https://git.linaro.org/toolchain/cortex-strings.git
*/

#include <linux/linkage.h>
@@ -30,49 +28,49 @@
#define REP8_80 0x8080808080808080

/* Parameters and result. */
-src1 .req x0
-src2 .req x1
-limit .req x2
-result .req x0
+#define src1 x0
+#define src2 x1
+#define limit x2
+#define result x0

/* Internal variables. */
-data1 .req x3
-data1w .req w3
-data2 .req x4
-data2w .req w4
-has_nul .req x5
-diff .req x6
-syndrome .req x7
-tmp1 .req x8
-tmp2 .req x9
-tmp3 .req x10
-zeroones .req x11
-pos .req x12
-limit_wd .req x13
-mask .req x14
-endloop .req x15
+#define data1 x3
+#define data1w w3
+#define data2 x4
+#define data2w w4
+#define has_nul x5
+#define diff x6
+#define syndrome x7
+#define tmp1 x8
+#define tmp2 x9
+#define tmp3 x10
+#define zeroones x11
+#define pos x12
+#define limit_wd x13
+#define mask x14
+#define endloop x15
+#define count mask

+ .p2align 6
+ .rep 7
+ nop /* Pad so that the loop below fits a cache line. */
+ .endr
SYM_FUNC_START_WEAK_PI(strncmp)
cbz limit, .Lret0
eor tmp1, src1, src2
mov zeroones, #REP8_01
tst tmp1, #7
+ and count, src1, #7
b.ne .Lmisaligned8
- ands tmp1, src1, #7
- b.ne .Lmutual_align
+ cbnz count, .Lmutual_align
/* Calculate the number of full and partial words -1. */
- /*
- * when limit is mulitply of 8, if not sub 1,
- * the judgement of last dword will wrong.
- */
- sub limit_wd, limit, #1 /* limit != 0, so no underflow. */
- lsr limit_wd, limit_wd, #3 /* Convert to Dwords. */
+ sub limit_wd, limit, #1 /* limit != 0, so no underflow. */
+ lsr limit_wd, limit_wd, #3 /* Convert to Dwords. */

- /*
- * NUL detection works on the principle that (X - 1) & (~X) & 0x80
- * (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
- * can be done in parallel across the entire word.
- */
+ /* NUL detection works on the principle that (X - 1) & (~X) & 0x80
+ (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
+ can be done in parallel across the entire word. */
+ /* Start of performance-critical section -- one 64B cache line. */
.Lloop_aligned:
ldr data1, [src1], #8
ldr data2, [src2], #8
@@ -80,23 +78,24 @@ SYM_FUNC_START_WEAK_PI(strncmp)
subs limit_wd, limit_wd, #1
sub tmp1, data1, zeroones
orr tmp2, data1, #REP8_7f
- eor diff, data1, data2 /* Non-zero if differences found. */
- csinv endloop, diff, xzr, pl /* Last Dword or differences.*/
- bics has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */
+ eor diff, data1, data2 /* Non-zero if differences found. */
+ csinv endloop, diff, xzr, pl /* Last Dword or differences. */
+ bics has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */
ccmp endloop, #0, #0, eq
b.eq .Lloop_aligned
+ /* End of performance-critical section -- one 64B cache line. */

- /*Not reached the limit, must have found the end or a diff. */
+ /* Not reached the limit, must have found the end or a diff. */
tbz limit_wd, #63, .Lnot_limit

/* Limit % 8 == 0 => all bytes significant. */
ands limit, limit, #7
b.eq .Lnot_limit

- lsl limit, limit, #3 /* Bits -> bytes. */
+ lsl limit, limit, #3 /* Bits -> bytes. */
mov mask, #~0
-CPU_BE( lsr mask, mask, limit )
-CPU_LE( lsl mask, mask, limit )
+CPU_BE(lsr mask, mask, limit)
+CPU_LE(lsl mask, mask, limit)
bic data1, data1, mask
bic data2, data2, mask

@@ -105,192 +104,156 @@ CPU_LE( lsl mask, mask, limit )

.Lnot_limit:
orr syndrome, diff, has_nul
- b .Lcal_cmpresult
+
+ CPU_LE(rev syndrome, syndrome)
+ CPU_LE(rev data1, data1)
+ /* The MS-non-zero bit of the syndrome marks either the first bit
+ that is different, or the top bit of the first zero byte.
+ Shifting left now will bring the critical information into the
+ top bits. */
+ CPU_LE(clz pos, syndrome)
+ CPU_LE(rev data2, data2)
+ CPU_LE(lsl data1, data1, pos)
+ CPU_LE(lsl data2, data2, pos)
+ /* But we need to zero-extend (char is unsigned) the value and then
+ perform a signed 32-bit subtraction. */
+ CPU_LE(lsr data1, data1, #56)
+ CPU_LE(sub result, data1, data2, lsr #56)
+ CPU_LE(ret)
+ /* For big-endian we cannot use the trick with the syndrome value
+ as carry-propagation can corrupt the upper bits if the trailing
+ bytes in the string contain 0x01. */
+ /* However, if there is no NUL byte in the dword, we can generate
+ the result directly. We can't just subtract the bytes as the
+ MSB might be significant. */
+ CPU_BE(cbnz has_nul, 1f)
+ CPU_BE(cmp data1, data2)
+ CPU_BE(cset result, ne)
+ CPU_BE(cneg result, result, lo)
+ CPU_BE(ret)
+1:
+ /* Re-compute the NUL-byte detection, using a byte-reversed value. */
+ CPU_BE(rev tmp3, data1)
+ CPU_BE(sub tmp1, tmp3, zeroones)
+ CPU_BE(orr tmp2, tmp3, #REP8_7f)
+ CPU_BE(bic has_nul, tmp1, tmp2)
+ CPU_BE(rev has_nul, has_nul)
+ CPU_BE(orr syndrome, diff, has_nul)
+ CPU_BE(clz pos, syndrome)
+ /* The MS-non-zero bit of the syndrome marks either the first bit
+ that is different, or the top bit of the first zero byte.
+ Shifting left now will bring the critical information into the
+ top bits. */
+ CPU_BE(lsl data1, data1, pos)
+ CPU_BE(lsl data2, data2, pos)
+ /* But we need to zero-extend (char is unsigned) the value and then
+ perform a signed 32-bit subtraction. */
+ CPU_BE(lsr data1, data1, #56)
+ CPU_BE(sub result, data1, data2, lsr #56)
+ CPU_BE(ret)

.Lmutual_align:
- /*
- * Sources are mutually aligned, but are not currently at an
- * alignment boundary. Round down the addresses and then mask off
- * the bytes that precede the start point.
- * We also need to adjust the limit calculations, but without
- * overflowing if the limit is near ULONG_MAX.
- */
+ /* Sources are mutually aligned, but are not currently at an
+ alignment boundary. Round down the addresses and then mask off
+ the bytes that precede the start point.
+ We also need to adjust the limit calculations, but without
+ overflowing if the limit is near ULONG_MAX. */
bic src1, src1, #7
bic src2, src2, #7
ldr data1, [src1], #8
- neg tmp3, tmp1, lsl #3 /* 64 - bits(bytes beyond align). */
+ neg tmp3, count, lsl #3 /* 64 - bits(bytes beyond align). */
ldr data2, [src2], #8
mov tmp2, #~0
- sub limit_wd, limit, #1 /* limit != 0, so no underflow. */
+ sub limit_wd, limit, #1 /* limit != 0, so no underflow. */
/* Big-endian. Early bytes are at MSB. */
-CPU_BE( lsl tmp2, tmp2, tmp3 ) /* Shift (tmp1 & 63). */
+ CPU_BE(lsl tmp2, tmp2, tmp3) /* Shift (count & 63). */
/* Little-endian. Early bytes are at LSB. */
-CPU_LE( lsr tmp2, tmp2, tmp3 ) /* Shift (tmp1 & 63). */
-
+ CPU_LE(lsr tmp2, tmp2, tmp3) /* Shift (count & 63). */
and tmp3, limit_wd, #7
lsr limit_wd, limit_wd, #3
- /* Adjust the limit. Only low 3 bits used, so overflow irrelevant.*/
- add limit, limit, tmp1
- add tmp3, tmp3, tmp1
+ /* Adjust the limit. Only low 3 bits used, so overflow irrelevant. */
+ add limit, limit, count
+ add tmp3, tmp3, count
orr data1, data1, tmp2
orr data2, data2, tmp2
add limit_wd, limit_wd, tmp3, lsr #3
b .Lstart_realigned

-/*when src1 offset is not equal to src2 offset...*/
+ .p2align 6
+ /* Don't bother with dwords for up to 16 bytes. */
.Lmisaligned8:
- cmp limit, #8
- b.lo .Ltiny8proc /*limit < 8... */
- /*
- * Get the align offset length to compare per byte first.
- * After this process, one string's address will be aligned.*/
- and tmp1, src1, #7
- neg tmp1, tmp1
- add tmp1, tmp1, #8
- and tmp2, src2, #7
- neg tmp2, tmp2
- add tmp2, tmp2, #8
- subs tmp3, tmp1, tmp2
- csel pos, tmp1, tmp2, hi /*Choose the maximum. */
- /*
- * Here, limit is not less than 8, so directly run .Ltinycmp
- * without checking the limit.*/
- sub limit, limit, pos
-.Ltinycmp:
+ cmp limit, #16
+ b.hs .Ltry_misaligned_words
+
+.Lbyte_loop:
+ /* Perhaps we can do better than this. */
ldrb data1w, [src1], #1
ldrb data2w, [src2], #1
- subs pos, pos, #1
- ccmp data1w, #1, #0, ne /* NZCV = 0b0000. */
- ccmp data1w, data2w, #0, cs /* NZCV = 0b0000. */
- b.eq .Ltinycmp
- cbnz pos, 1f /*find the null or unequal...*/
- cmp data1w, #1
- ccmp data1w, data2w, #0, cs
- b.eq .Lstart_align /*the last bytes are equal....*/
-1:
+ subs limit, limit, #1
+ ccmp data1w, #1, #0, hi /* NZCV = 0b0000. */
+ ccmp data1w, data2w, #0, cs /* NZCV = 0b0000. */
+ b.eq .Lbyte_loop
+.Ldone:
sub result, data1, data2
ret
-
-.Lstart_align:
+ /* Align the SRC1 to a dword by doing a bytewise compare and then do
+ the dword loop. */
+.Ltry_misaligned_words:
lsr limit_wd, limit, #3
- cbz limit_wd, .Lremain8
- /*process more leading bytes to make str1 aligned...*/
- ands xzr, src1, #7
- b.eq .Lrecal_offset
- add src1, src1, tmp3 /*tmp3 is positive in this branch.*/
- add src2, src2, tmp3
- ldr data1, [src1], #8
- ldr data2, [src2], #8
+ cbz count, .Ldo_misaligned

- sub limit, limit, tmp3
+ neg count, count
+ and count, count, #7
+ sub limit, limit, count
lsr limit_wd, limit, #3
- subs limit_wd, limit_wd, #1

- sub tmp1, data1, zeroones
- orr tmp2, data1, #REP8_7f
- eor diff, data1, data2 /* Non-zero if differences found. */
- csinv endloop, diff, xzr, ne/*if limit_wd is 0,will finish the cmp*/
- bics has_nul, tmp1, tmp2
- ccmp endloop, #0, #0, eq /*has_null is ZERO: no null byte*/
- b.ne .Lunequal_proc
- /*How far is the current str2 from the alignment boundary...*/
- and tmp3, tmp3, #7
-.Lrecal_offset:
- neg pos, tmp3
-.Lloopcmp_proc:
- /*
- * Divide the eight bytes into two parts. First,backwards the src2
- * to an alignment boundary,load eight bytes from the SRC2 alignment
- * boundary,then compare with the relative bytes from SRC1.
- * If all 8 bytes are equal,then start the second part's comparison.
- * Otherwise finish the comparison.
- * This special handle can garantee all the accesses are in the
- * thread/task space in avoid to overrange access.
- */
- ldr data1, [src1,pos]
- ldr data2, [src2,pos]
- sub tmp1, data1, zeroones
- orr tmp2, data1, #REP8_7f
- bics has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */
- eor diff, data1, data2 /* Non-zero if differences found. */
- csinv endloop, diff, xzr, eq
- cbnz endloop, .Lunequal_proc
+.Lpage_end_loop:
+ ldrb data1w, [src1], #1
+ ldrb data2w, [src2], #1
+ cmp data1w, #1
+ ccmp data1w, data2w, #0, cs /* NZCV = 0b0000. */
+ b.ne .Ldone
+ subs count, count, #1
+ b.hi .Lpage_end_loop
+
+.Ldo_misaligned:
+ /* Prepare ourselves for the next page crossing. Unlike the aligned
+ loop, we fetch 1 less dword because we risk crossing bounds on
+ SRC2. */
+ mov count, #8
+ subs limit_wd, limit_wd, #1
+ b.lo .Ldone_loop
+.Lloop_misaligned:
+ and tmp2, src2, #0xff8
+ eor tmp2, tmp2, #0xff8
+ cbz tmp2, .Lpage_end_loop

- /*The second part process*/
ldr data1, [src1], #8
ldr data2, [src2], #8
- subs limit_wd, limit_wd, #1
sub tmp1, data1, zeroones
orr tmp2, data1, #REP8_7f
- eor diff, data1, data2 /* Non-zero if differences found. */
- csinv endloop, diff, xzr, ne/*if limit_wd is 0,will finish the cmp*/
- bics has_nul, tmp1, tmp2
- ccmp endloop, #0, #0, eq /*has_null is ZERO: no null byte*/
- b.eq .Lloopcmp_proc
-
-.Lunequal_proc:
- orr syndrome, diff, has_nul
- cbz syndrome, .Lremain8
-.Lcal_cmpresult:
- /*
- * reversed the byte-order as big-endian,then CLZ can find the most
- * significant zero bits.
- */
-CPU_LE( rev syndrome, syndrome )
-CPU_LE( rev data1, data1 )
-CPU_LE( rev data2, data2 )
- /*
- * For big-endian we cannot use the trick with the syndrome value
- * as carry-propagation can corrupt the upper bits if the trailing
- * bytes in the string contain 0x01.
- * However, if there is no NUL byte in the dword, we can generate
- * the result directly. We can't just subtract the bytes as the
- * MSB might be significant.
- */
-CPU_BE( cbnz has_nul, 1f )
-CPU_BE( cmp data1, data2 )
-CPU_BE( cset result, ne )
-CPU_BE( cneg result, result, lo )
-CPU_BE( ret )
-CPU_BE( 1: )
- /* Re-compute the NUL-byte detection, using a byte-reversed value.*/
-CPU_BE( rev tmp3, data1 )
-CPU_BE( sub tmp1, tmp3, zeroones )
-CPU_BE( orr tmp2, tmp3, #REP8_7f )
-CPU_BE( bic has_nul, tmp1, tmp2 )
-CPU_BE( rev has_nul, has_nul )
-CPU_BE( orr syndrome, diff, has_nul )
- /*
- * The MS-non-zero bit of the syndrome marks either the first bit
- * that is different, or the top bit of the first zero byte.
- * Shifting left now will bring the critical information into the
- * top bits.
- */
- clz pos, syndrome
- lsl data1, data1, pos
- lsl data2, data2, pos
- /*
- * But we need to zero-extend (char is unsigned) the value and then
- * perform a signed 32-bit subtraction.
- */
- lsr data1, data1, #56
- sub result, data1, data2, lsr #56
- ret
-
-.Lremain8:
- /* Limit % 8 == 0 => all bytes significant. */
- ands limit, limit, #7
- b.eq .Lret0
-.Ltiny8proc:
- ldrb data1w, [src1], #1
- ldrb data2w, [src2], #1
- subs limit, limit, #1
+ eor diff, data1, data2 /* Non-zero if differences found. */
+ bics has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */
+ ccmp diff, #0, #0, eq
+ b.ne .Lnot_limit
+ subs limit_wd, limit_wd, #1
+ b.pl .Lloop_misaligned

- ccmp data1w, #1, #0, ne /* NZCV = 0b0000. */
- ccmp data1w, data2w, #0, cs /* NZCV = 0b0000. */
- b.eq .Ltiny8proc
- sub result, data1, data2
- ret
+.Ldone_loop:
+ /* We found a difference or a NULL before the limit was reached. */
+ and limit, limit, #7
+ cbz limit, .Lnot_limit
+ /* Read the last word. */
+ sub src1, src1, 8
+ sub src2, src2, 8
+ ldr data1, [src1, limit]
+ ldr data2, [src2, limit]
+ sub tmp1, data1, zeroones
+ orr tmp2, data1, #REP8_7f
+ eor diff, data1, data2 /* Non-zero if differences found. */
+ bics has_nul, tmp1, tmp2 /* Non-zero if NUL terminator. */
+ ccmp diff, #0, #0, eq
+ b.ne .Lnot_limit

.Lret0:
mov result, #0
--
2.17.1

2020-05-14 14:35:03

by Oliver Swede

[permalink] [raw]
Subject: [PATCH v3 11/13] arm64: Use additional memcpy macros and fixups

The Arm-provided memcpy routine has been updated with more recent
optimizations. The kernel's helper functions for copying to/from
user space memory, which import this algorithm and create
exception table entries for instructions that reference user
space, require new recovery code to accurately determine the number
of bytes that successfully copied before a page fault.

This adds new macro instantiations in the copy template, and
corresponding definitions to each of the copy_*_user() functions.
This allows more fixup routines to be added so that an accurate value
for the number of bytes that failed to copy can be returned.

This increases the flexibility of the fixup code, as certain
propertes can be encapsulated by the mapping to implicitly provide
the routines with more information than would otherwise be available.

In the case of the current memcpy, the number of bytes already copied
depends highly on the type of instruction that caused the fault (load
vs. store), so the use of separate fixups enables specific routines
for each case. This is an alternative to (for instance) using a
single fixup for both the loads and stores, as this would be subject
to issues relating to overlapping src and dst buffers in
copy_in_user().
The outcome also depends largely on other factors, such as if the
target address is specified relative to the start or end of the buffer,
and whether or not this access is guaranteed to be aligned with 16B.

These distinctions, obtained from analysis of the copy algorithm,
enable fixups to be written that are modular and accurate for each
case. In this way the fixup logic should be straightforward to
modify in the future, e.g. if there are further improvements to the
memcpy routine.

Signed-off-by: Oliver Swede <[email protected]>
---
arch/arm64/lib/copy_from_user.S | 196 ++++++++++++++++++++++++--
arch/arm64/lib/copy_in_user.S | 204 ++++++++++++++++++++++++++--
arch/arm64/lib/copy_template.S | 126 ++++++++---------
arch/arm64/lib/copy_template_user.S | 20 +++
arch/arm64/lib/copy_to_user.S | 178 +++++++++++++++++++++++-
arch/arm64/lib/copy_user_fixup.S | 13 +-
arch/arm64/lib/memcpy.S | 80 +++++++++++
7 files changed, 725 insertions(+), 92 deletions(-)

diff --git a/arch/arm64/lib/copy_from_user.S b/arch/arm64/lib/copy_from_user.S
index 45009fb07081..0056d1fc06eb 100644
--- a/arch/arm64/lib/copy_from_user.S
+++ b/arch/arm64/lib/copy_from_user.S
@@ -21,16 +21,44 @@
*/

.macro ldrb1 reg, ptr, offset=0
- USER_F(9998f, ldtrb \reg, [\ptr, \offset])
+ USER_F(9993f, ldtrb \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldrb2 reg, ptr, offset=0
+ USER_F(9994f, ldtrb \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldrb3 reg, ptr, offset=0
+ USER_F(9995f, ldtrb \reg, [\ptr, \offset])
.endm

.macro strb1 reg, ptr, offset=0
strb \reg, [\ptr, \offset]
.endm

+ .macro strb2 reg, ptr, offset=0
+ strb \reg, [\ptr, \offset]
+ .endm
+
+ .macro strb3 reg, ptr, offset=0
+ strb \reg, [\ptr, \offset]
+ .endm
+
.macro ldrb1_reg reg, ptr, offset
add \ptr, \ptr, \offset
- USER_F(9998f, ldtrb \reg, [\ptr])
+ USER_F(9993f, ldtrb \reg, [\ptr])
+ sub \ptr, \ptr, \offset
+ .endm
+
+ .macro ldrb2_reg reg, ptr, offset
+ add \ptr, \ptr, \offset
+ USER_F(9994f, ldtrb \reg, [\ptr])
+ sub \ptr, \ptr, \offset
+ .endm
+
+ .macro ldrb3_reg reg, ptr, offset
+ add \ptr, \ptr, \offset
+ USER_F(9995f, ldtrb \reg, [\ptr])
sub \ptr, \ptr, \offset
.endm

@@ -38,26 +66,80 @@
strb \reg, [\ptr, \offset]
.endm

+ .macro strb2_reg reg, ptr, offset
+ strb \reg, [\ptr, \offset]
+ .endm
+
+ .macro strb3_reg reg, ptr, offset
+ strb \reg, [\ptr, \offset]
+ .endm
+
.macro ldr1 reg, ptr, offset=0
- USER_F(9998f, ldtr \reg, [\ptr, \offset])
+ USER_F(9993f, ldtr \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldr2 reg, ptr, offset=0
+ USER_F(9994f, ldtr \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldr3 reg, ptr, offset=0
+ USER_F(9995f, ldtr \reg, [\ptr, \offset])
.endm

.macro str1 reg, ptr, offset=0
str \reg, [\ptr, \offset]
.endm

+ .macro str2 reg, ptr, offset=0
+ str \reg, [\ptr, \offset]
+ .endm
+
+ .macro str3 reg, ptr, offset=0
+ str \reg, [\ptr, \offset]
+ .endm
+
.macro ldp1 regA, regB, ptr, offset=0
- USER_F(9998f, ldtr \regA, [\ptr, \offset])
- USER_F(9998f, ldtr \regB, [\ptr, \offset + 8])
+ USER_F(9993f, ldtr \regA, [\ptr, \offset])
+ USER_F(9993f, ldtr \regB, [\ptr, \offset + 8])
+ .endm
+
+ .macro ldp2 regA, regB, ptr, offset=0
+ USER_F(9994f, ldtr \regA, [\ptr, \offset])
+ USER_F(9994f, ldtr \regB, [\ptr, \offset + 8])
+ .endm
+
+ .macro ldp3 regA, regB, ptr, offset=0
+ USER_F(9995f, ldtr \regA, [\ptr, \offset])
+ USER_F(9995f, ldtr \regB, [\ptr, \offset + 8])
.endm

.macro stp1 regA, regB, ptr, offset=0
stp \regA, \regB, [\ptr, \offset]
.endm

+ .macro stp2 regA, regB, ptr, offset=0
+ stp \regA, \regB, [\ptr, \offset]
+ .endm
+
+ .macro stp3 regA, regB, ptr, offset=0
+ stp \regA, \regB, [\ptr, \offset]
+ .endm
+
.macro ldp1_pre regA, regB, ptr, offset
- USER_F(9998f, ldtr \regA, [\ptr, \offset])
- USER_F(9998f, ldtr \regB, [\ptr, \offset + 8])
+ USER_F(9993f, ldtr \regA, [\ptr, \offset])
+ USER_F(9993f, ldtr \regB, [\ptr, \offset + 8])
+ add \ptr, \ptr, \offset
+ .endm
+
+ .macro ldp2_pre regA, regB, ptr, offset
+ USER_F(9994f, ldtr \regA, [\ptr, \offset])
+ USER_F(9994f, ldtr \regB, [\ptr, \offset + 8])
+ add \ptr, \ptr, \offset
+ .endm
+
+ .macro ldp3_pre regA, regB, ptr, offset
+ USER_F(9995f, ldtr \regA, [\ptr, \offset])
+ USER_F(9995f, ldtr \regB, [\ptr, \offset + 8])
add \ptr, \ptr, \offset
.endm

@@ -65,46 +147,134 @@
stp \regA, \regB, [\ptr, \offset]!
.endm

+ .macro stp2_pre regA, regB, ptr, offset
+ stp \regA, \regB, [\ptr, \offset]!
+ .endm
+
+ .macro stp3_pre regA, regB, ptr, offset
+ stp \regA, \regB, [\ptr, \offset]!
+ .endm
+
.macro ldrb1_nuao reg, ptr, offset=0
- USER_F(9998f, ldrb \reg, [\ptr, \offset])
+ USER_F(9993f, ldrb \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldrb2_nuao reg, ptr, offset=0
+ USER_F(9994f, ldrb \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldrb3_nuao reg, ptr, offset=0
+ USER_F(9995f, ldrb \reg, [\ptr, \offset])
.endm

.macro strb1_nuao reg, ptr, offset=0
strb \reg, [\ptr, \offset]
.endm

+ .macro strb2_nuao reg, ptr, offset=0
+ strb \reg, [\ptr, \offset]
+ .endm
+
+ .macro strb3_nuao reg, ptr, offset=0
+ strb \reg, [\ptr, \offset]
+ .endm
+
.macro ldrb1_nuao_reg reg, ptr, offset=0
- USER_F(9998f, ldrb \reg, [\ptr, \offset])
+ USER_F(9993f, ldrb \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldrb2_nuao_reg reg, ptr, offset=0
+ USER_F(9994f, ldrb \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldrb3_nuao_reg reg, ptr, offset=0
+ USER_F(9995f, ldrb \reg, [\ptr, \offset])
.endm

.macro strb1_nuao_reg reg, ptr, offset=0
strb \reg, [\ptr, \offset]
.endm

+ .macro strb2_nuao_reg reg, ptr, offset=0
+ strb \reg, [\ptr, \offset]
+ .endm
+
+ .macro strb3_nuao_reg reg, ptr, offset=0
+ strb \reg, [\ptr, \offset]
+ .endm
+
.macro ldr1_nuao reg, ptr, offset=0
- USER_F(9998f, ldr \reg, [\ptr, \offset])
+ USER_F(9993f, ldr \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldr2_nuao reg, ptr, offset=0
+ USER_F(9994f, ldr \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldr3_nuao reg, ptr, offset=0
+ USER_F(9995f, ldr \reg, [\ptr, \offset])
.endm

.macro str1_nuao reg, ptr, offset=0
str \reg, [\ptr, \offset]
.endm

- .macro ldp1_nuao regA, regB, ptr, offset=0
- USER_F(9998f, ldp \regA, \regB, [\ptr, \offset])
+ .macro str2_nuao reg, ptr, offset=0
+ str \reg, [\ptr, \offset]
+ .endm
+
+ .macro str3_nuao reg, ptr, offset=0
+ str \reg, [\ptr, \offset]
+ .endm
+
+ .macro ldp1_nuao regA, regB, ptr, offset=0
+ USER_F(9993f, ldp \regA, \regB, [\ptr, \offset])
+ .endm
+
+ .macro ldp2_nuao regA, regB, ptr, offset=0
+ USER_F(9994f, ldp \regA, \regB, [\ptr, \offset])
+ .endm
+
+ .macro ldp3_nuao regA, regB, ptr, offset=0
+ USER_F(9995f, ldp \regA, \regB, [\ptr, \offset])
.endm

.macro stp1_nuao regA, regB, ptr, offset=0
stp \regA, \regB, [\ptr, \offset]
.endm

+ .macro stp2_nuao regA, regB, ptr, offset=0
+ stp \regA, \regB, [\ptr, \offset]
+ .endm
+
+ .macro stp3_nuao regA, regB, ptr, offset=0
+ stp \regA, \regB, [\ptr, \offset]
+ .endm
+
.macro ldp1_pre_nuao regA, regB, ptr, offset
- USER_F(9998f, ldp \regA, \regB, [\ptr, \offset]!)
+ USER_F(9993f, ldp \regA, \regB, [\ptr, \offset]!)
+ .endm
+
+ .macro ldp2_pre_nuao regA, regB, ptr, offset
+ USER_F(9994f, ldp \regA, \regB, [\ptr, \offset]!)
+ .endm
+
+ .macro ldp3_pre_nuao regA, regB, ptr, offset
+ USER_F(9995f, ldp \regA, \regB, [\ptr, \offset]!)
.endm

.macro stp1_pre_nuao regA, regB, ptr, offset
stp \regA, \regB, [\ptr, \offset]!
.endm

+ .macro stp2_pre_nuao regA, regB, ptr, offset
+ stp \regA, \regB, [\ptr, \offset]!
+ .endm
+
+ .macro stp3_pre_nuao regA, regB, ptr, offset
+ stp \regA, \regB, [\ptr, \offset]!
+ .endm
+
.macro copy_exit
b .Luaccess_finish
.endm
diff --git a/arch/arm64/lib/copy_in_user.S b/arch/arm64/lib/copy_in_user.S
index c1647a9b3a22..4511f59dd979 100644
--- a/arch/arm64/lib/copy_in_user.S
+++ b/arch/arm64/lib/copy_in_user.S
@@ -23,92 +23,272 @@
*/

.macro ldrb1 reg, ptr, offset=0
- USER_F(9998f, ldtrb \reg, [\ptr, \offset])
+ USER_F(9993f, ldtrb \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldrb2 reg, ptr, offset=0
+ USER_F(9994f, ldtrb \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldrb3 reg, ptr, offset=0
+ USER_F(9995f, ldtrb \reg, [\ptr, \offset])
.endm

.macro strb1 reg, ptr, offset=0
+ USER_F(9996f, sttrb \reg, [\ptr, \offset])
+ .endm
+
+ .macro strb2 reg, ptr, offset=0
+ USER_F(9997f, sttrb \reg, [\ptr, \offset])
+ .endm
+
+ .macro strb3 reg, ptr, offset=0
USER_F(9998f, sttrb \reg, [\ptr, \offset])
.endm

.macro ldrb1_reg reg, ptr, offset
add \ptr, \ptr, \offset
- USER_F(9998f, ldtrb \reg, [\ptr])
+ USER_F(9993f, ldtrb \reg, [\ptr])
+ sub \ptr, \ptr, \offset
+ .endm
+
+ .macro ldrb2_reg reg, ptr, offset
+ add \ptr, \ptr, \offset
+ USER_F(9994f, ldtrb \reg, [\ptr])
+ sub \ptr, \ptr, \offset
+ .endm
+
+ .macro ldrb3_reg reg, ptr, offset
+ add \ptr, \ptr, \offset
+ USER_F(9995f, ldtrb \reg, [\ptr])
sub \ptr, \ptr, \offset
.endm

.macro strb1_reg reg, ptr, offset
add \ptr, \ptr, \offset
+ USER_F(9996f, sttrb \reg, [\ptr])
+ sub \ptr, \ptr, \offset
+ .endm
+
+ .macro strb2_reg reg, ptr, offset
+ add \ptr, \ptr, \offset
+ USER_F(9997f, sttrb \reg, [\ptr])
+ sub \ptr, \ptr, \offset
+ .endm
+
+ .macro strb3_reg reg, ptr, offset
+ add \ptr, \ptr, \offset
USER_F(9998f, sttrb \reg, [\ptr])
sub \ptr, \ptr, \offset
.endm

.macro ldr1 reg, ptr, offset=0
- USER_F(9998f, ldtr \reg, [\ptr, \offset])
+ USER_F(9993f, ldtr \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldr2 reg, ptr, offset=0
+ USER_F(9994f, ldtr \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldr3 reg, ptr, offset=0
+ USER_F(9995f, ldtr \reg, [\ptr, \offset])
.endm

.macro str1 reg, ptr, offset=0
+ USER_F(9996f, sttr \reg, [\ptr, \offset])
+ .endm
+
+ .macro str2 reg, ptr, offset=0
+ USER_F(9997f, sttr \reg, [\ptr, \offset])
+ .endm
+
+ .macro str3 reg, ptr, offset=0
USER_F(9998f, sttr \reg, [\ptr, \offset])
.endm

.macro ldp1 regA, regB, ptr, offset=0
- USER_F(9998f, ldtr \regA, [\ptr, \offset])
- USER_F(9998f, ldtr \regB, [\ptr, \offset + 8])
+ USER_F(9993f, ldtr \regA, [\ptr, \offset])
+ USER_F(9993f, ldtr \regB, [\ptr, \offset + 8])
+ .endm
+
+ .macro ldp2 regA, regB, ptr, offset=0
+ USER_F(9994f, ldtr \regA, [\ptr, \offset])
+ USER_F(9994f, ldtr \regB, [\ptr, \offset + 8])
+ .endm
+
+ .macro ldp3 regA, regB, ptr, offset=0
+ USER_F(9995f, ldtr \regA, [\ptr, \offset])
+ USER_F(9995f, ldtr \regB, [\ptr, \offset + 8])
.endm

.macro stp1 regA, regB, ptr, offset=0
+ USER_F(9996f, sttr \regA, [\ptr, \offset])
+ USER_F(9996f, sttr \regB, [\ptr, \offset + 8])
+ .endm
+
+ .macro stp2 regA, regB, ptr, offset=0
+ USER_F(9997f, sttr \regA, [\ptr, \offset])
+ USER_F(9997f, sttr \regB, [\ptr, \offset + 8])
+ .endm
+
+ .macro stp3 regA, regB, ptr, offset=0
USER_F(9998f, sttr \regA, [\ptr, \offset])
USER_F(9998f, sttr \regB, [\ptr, \offset + 8])
.endm

.macro ldp1_pre regA, regB, ptr, offset
- USER_F(9998f, ldtr \regA, [\ptr, \offset])
- USER_F(9998f, ldtr \regB, [\ptr, \offset + 8])
+ USER_F(9993f, ldtr \regA, [\ptr, \offset])
+ USER_F(9993f, ldtr \regB, [\ptr, \offset + 8])
+ add \ptr, \ptr, \offset
+ .endm
+
+ .macro ldp2_pre regA, regB, ptr, offset
+ USER_F(9994f, ldtr \regA, [\ptr, \offset])
+ USER_F(9994f, ldtr \regB, [\ptr, \offset + 8])
+ add \ptr, \ptr, \offset
+ .endm
+
+ .macro ldp3_pre regA, regB, ptr, offset
+ USER_F(9995f, ldtr \regA, [\ptr, \offset])
+ USER_F(9995f, ldtr \regB, [\ptr, \offset + 8])
add \ptr, \ptr, \offset
.endm

.macro stp1_pre regA, regB, ptr, offset
+ USER_F(9996f, sttr \regA, [\ptr, \offset])
+ USER_F(9996f, sttr \regB, [\ptr, \offset + 8])
+ add \ptr, \ptr, \offset
+ .endm
+
+ .macro stp2_pre regA, regB, ptr, offset
+ USER_F(9997f, sttr \regA, [\ptr, \offset])
+ USER_F(9997f, sttr \regB, [\ptr, \offset + 8])
+ add \ptr, \ptr, \offset
+ .endm
+
+ .macro stp3_pre regA, regB, ptr, offset
USER_F(9998f, sttr \regA, [\ptr, \offset])
USER_F(9998f, sttr \regB, [\ptr, \offset + 8])
add \ptr, \ptr, \offset
.endm

.macro ldrb1_nuao reg, ptr, offset=0
- USER_F(9998f, ldrb \reg, [\ptr, \offset])
+ USER_F(9993f, ldrb \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldrb2_nuao reg, ptr, offset=0
+ USER_F(9994f, ldrb \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldrb3_nuao reg, ptr, offset=0
+ USER_F(9995f, ldrb \reg, [\ptr, \offset])
.endm

.macro strb1_nuao reg, ptr, offset=0
+ USER_F(9996f, strb \reg, [\ptr, \offset])
+ .endm
+
+ .macro strb2_nuao reg, ptr, offset=0
+ USER_F(9997f, strb \reg, [\ptr, \offset])
+ .endm
+
+ .macro strb3_nuao reg, ptr, offset=0
USER_F(9998f, strb \reg, [\ptr, \offset])
.endm

.macro ldrb1_nuao_reg reg, ptr, offset=0
- USER_F(9998f, ldrb \reg, [\ptr, \offset])
+ USER_F(9993f, ldrb \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldrb2_nuao_reg reg, ptr, offset=0
+ USER_F(9994f, ldrb \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldrb3_nuao_reg reg, ptr, offset=0
+ USER_F(9995f, ldrb \reg, [\ptr, \offset])
.endm

.macro strb1_nuao_reg reg, ptr, offset=0
+ USER_F(9996f, strb \reg, [\ptr, \offset])
+ .endm
+
+ .macro strb2_nuao_reg reg, ptr, offset=0
+ USER_F(9997f, strb \reg, [\ptr, \offset])
+ .endm
+
+ .macro strb3_nuao_reg reg, ptr, offset=0
USER_F(9998f, strb \reg, [\ptr, \offset])
.endm

.macro ldr1_nuao reg, ptr, offset=0
- USER_F(9998f, ldr \reg, [\ptr, \offset])
+ USER_F(9993f, ldr \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldr2_nuao reg, ptr, offset=0
+ USER_F(9994f, ldr \reg, [\ptr, \offset])
+ .endm
+
+ .macro ldr3_nuao reg, ptr, offset=0
+ USER_F(9995f, ldr \reg, [\ptr, \offset])
.endm

.macro str1_nuao reg, ptr, offset=0
+ USER_F(9996f, str \reg, [\ptr, \offset])
+ .endm
+
+ .macro str2_nuao reg, ptr, offset=0
+ USER_F(9997f, str \reg, [\ptr, \offset])
+ .endm
+
+ .macro str3_nuao reg, ptr, offset=0
USER_F(9998f, str \reg, [\ptr, \offset])
.endm

.macro ldp1_nuao regA, regB, ptr, offset=0
- USER_F(9998f, ldp \regA, \regB, [\ptr, \offset])
+ USER_F(9993f, ldp \regA, \regB, [\ptr, \offset])
+ .endm
+
+ .macro ldp2_nuao regA, regB, ptr, offset=0
+ USER_F(9994f, ldp \regA, \regB, [\ptr, \offset])
+ .endm
+
+ .macro ldp3_nuao regA, regB, ptr, offset=0
+ USER_F(9995f, ldp \regA, \regB, [\ptr, \offset])
.endm

.macro stp1_nuao regA, regB, ptr, offset=0
+ USER_F(9996f, stp \regA, \regB, [\ptr, \offset])
+ .endm
+
+ .macro stp2_nuao regA, regB, ptr, offset=0
+ USER_F(9997f, stp \regA, \regB, [\ptr, \offset])
+ .endm
+
+ .macro stp3_nuao regA, regB, ptr, offset=0
USER_F(9998f, stp \regA, \regB, [\ptr, \offset])
.endm

.macro ldp1_pre_nuao regA, regB, ptr, offset
- USER_F(9998f, ldp \regA, \regB, [\ptr, \offset]!)
+ USER_F(9993f, ldp \regA, \regB, [\ptr, \offset]!)
+ .endm
+
+ .macro ldp2_pre_nuao regA, regB, ptr, offset
+ USER_F(9994f, ldp \regA, \regB, [\ptr, \offset]!)
+ .endm
+
+ .macro ldp3_pre_nuao regA, regB, ptr, offset
+ USER_F(9995f, ldp \regA, \regB, [\ptr, \offset]!)
.endm

.macro stp1_pre_nuao regA, regB, ptr, offset
+ USER_F(9996f, stp \regA, \regB, [\ptr, \offset]!)
+ .endm
+
+ .macro stp2_pre_nuao regA, regB, ptr, offset
+ USER_F(9997f, stp \regA, \regB, [\ptr, \offset]!)
+ .endm
+
+ .macro stp3_pre_nuao regA, regB, ptr, offset
USER_F(9998f, stp \regA, \regB, [\ptr, \offset]!)
.endm

diff --git a/arch/arm64/lib/copy_template.S b/arch/arm64/lib/copy_template.S
index 90b5f63ff227..0c3e39ae906d 100644
--- a/arch/arm64/lib/copy_template.S
+++ b/arch/arm64/lib/copy_template.S
@@ -58,18 +58,18 @@
cmp count, 16
b.lo L(copy16)
ldp1 A_l, A_h, src
- ldp1 D_l, D_h, srcend, -16
+ ldp2 D_l, D_h, srcend, -16
stp1 A_l, A_h, dstin
- stp1 D_l, D_h, dstend, -16
+ stp2 D_l, D_h, dstend, -16
copy_exit

/* Copy 8-15 bytes. */
L(copy16):
tbz count, 3, L(copy8)
ldr1 A_l, src
- ldr1 A_h, srcend, -8
+ ldr2 A_h, srcend, -8
str1 A_l, dstin
- str1 A_h, dstend, -8
+ str2 A_h, dstend, -8
copy_exit

.p2align 3
@@ -77,9 +77,9 @@ L(copy16):
L(copy8):
tbz count, 2, L(copy4)
ldr1 A_lw, src
- ldr1 B_lw, srcend, -4
+ ldr2 B_lw, srcend, -4
str1 A_lw, dstin
- str1 B_lw, dstend, -4
+ str2 B_lw, dstend, -4
copy_exit

/* Copy 0..3 bytes using a branchless sequence. */
@@ -87,11 +87,11 @@ L(copy4):
cbz count, L(copy0)
lsr tmp1, count, 1
ldrb1 A_lw, src
- ldrb1 C_lw, srcend, -1
+ ldrb2 C_lw, srcend, -1
ldrb1_reg B_lw, src, tmp1
strb1 A_lw, dstin
strb1_reg B_lw, dstin, tmp1
- strb1 C_lw, dstend, -1
+ strb2 C_lw, dstend, -1
L(copy0):
copy_exit

@@ -100,14 +100,14 @@ L(copy0):
L(copy32_128):
ldp1 A_l, A_h, src
ldp1 B_l, B_h, src, 16
- ldp1 C_l, C_h, srcend, -32
- ldp1 D_l, D_h, srcend, -16
+ ldp2 C_l, C_h, srcend, -32
+ ldp2 D_l, D_h, srcend, -16
cmp count, 64
b.hi L(copy128)
stp1 A_l, A_h, dstin
stp1 B_l, B_h, dstin, 16
- stp1 C_l, C_h, dstend, -32
- stp1 D_l, D_h, dstend, -16
+ stp2 C_l, C_h, dstend, -32
+ stp2 D_l, D_h, dstend, -16
copy_exit

.p2align 4
@@ -117,17 +117,17 @@ L(copy128):
ldp1 F_l, F_h, src, 48
cmp count, 96
b.ls L(copy96)
- ldp1 G_l, G_h, srcend, -64
- ldp1 H_l, H_h, srcend, -48
- stp1 G_l, G_h, dstend, -64
- stp1 H_l, H_h, dstend, -48
+ ldp2 G_l, G_h, srcend, -64
+ ldp2 H_l, H_h, srcend, -48
+ stp2 G_l, G_h, dstend, -64
+ stp2 H_l, H_h, dstend, -48
L(copy96):
stp1 A_l, A_h, dstin
stp1 B_l, B_h, dstin, 16
stp1 E_l, E_h, dstin, 32
stp1 F_l, F_h, dstin, 48
- stp1 C_l, C_h, dstend, -32
- stp1 D_l, D_h, dstend, -16
+ stp2 C_l, C_h, dstend, -32
+ stp2 D_l, D_h, dstend, -16
copy_exit

.p2align 4
@@ -146,83 +146,85 @@ L(copy_long):
bic dst, dstin, 15
sub src, src, tmp1
add count, count, tmp1 /* Count is now 16 too large. */
- ldp1 A_l, A_h, src, 16
+ ldp3 A_l, A_h, src, 16
stp1 D_l, D_h, dstin
- ldp1 B_l, B_h, src, 32
- ldp1 C_l, C_h, src, 48
- ldp1_pre D_l, D_h, src, 64
+ ldp3 B_l, B_h, src, 32
+ ldp3 C_l, C_h, src, 48
+ ldp3_pre D_l, D_h, src, 64
subs count, count, 128 + 16 /* Test and readjust count. */
b.ls L(copy64_from_end)

L(loop64):
- stp1 A_l, A_h, dst, 16
- ldp1 A_l, A_h, src, 16
- stp1 B_l, B_h, dst, 32
- ldp1 B_l, B_h, src, 32
- stp1 C_l, C_h, dst, 48
- ldp1 C_l, C_h, src, 48
- stp1_pre D_l, D_h, dst, 64
- ldp1_pre D_l, D_h, src, 64
+ stp3 A_l, A_h, dst, 16
+ ldp3 A_l, A_h, src, 16
+ stp3 B_l, B_h, dst, 32
+ ldp3 B_l, B_h, src, 32
+ stp3 C_l, C_h, dst, 48
+ ldp3 C_l, C_h, src, 48
+ stp3_pre D_l, D_h, dst, 64
+ ldp3_pre D_l, D_h, src, 64
subs count, count, 64
b.hi L(loop64)

/* Write the last iteration and copy 64 bytes from the end. */
L(copy64_from_end):
- ldp1 E_l, E_h, srcend, -64
- stp1 A_l, A_h, dst, 16
- ldp1 A_l, A_h, srcend, -48
- stp1 B_l, B_h, dst, 32
- ldp1 B_l, B_h, srcend, -32
- stp1 C_l, C_h, dst, 48
- ldp1 C_l, C_h, srcend, -16
- stp1 D_l, D_h, dst, 64
- stp1 E_l, E_h, dstend, -64
- stp1 A_l, A_h, dstend, -48
- stp1 B_l, B_h, dstend, -32
- stp1 C_l, C_h, dstend, -16
+ ldp2 E_l, E_h, srcend, -64
+ stp3 A_l, A_h, dst, 16
+ ldp2 A_l, A_h, srcend, -48
+ stp3 B_l, B_h, dst, 32
+ ldp2 B_l, B_h, srcend, -32
+ stp3 C_l, C_h, dst, 48
+ ldp2 C_l, C_h, srcend, -16
+ stp3 D_l, D_h, dst, 64
+ stp2 E_l, E_h, dstend, -64
+ stp2 A_l, A_h, dstend, -48
+ stp2 B_l, B_h, dstend, -32
+ stp2 C_l, C_h, dstend, -16
copy_exit

.p2align 4
+
/* Large backwards copy for overlapping copies.
- Copy 16 bytes and then align dst to 16-byte alignment. */
+ Copy 16 bytes and then align dst to 16-byte alignment. */
L(copy_long_backwards):
- ldp1 D_l, D_h, srcend, -16
+ ldp2 D_l, D_h, srcend, -16
and tmp1, dstend, 15
sub srcend, srcend, tmp1
sub count, count, tmp1
- ldp1 A_l, A_h, srcend, -16
- stp1 D_l, D_h, dstend, -16
- ldp1 B_l, B_h, srcend, -32
- ldp1 C_l, C_h, srcend, -48
- ldp1_pre D_l, D_h, srcend, -64
+ ldp2 A_l, A_h, srcend, -16
+ stp2 D_l, D_h, dstend, -16
+ ldp2 B_l, B_h, srcend, -32
+ ldp2 C_l, C_h, srcend, -48
+ ldp2_pre D_l, D_h, srcend, -64
sub dstend, dstend, tmp1
subs count, count, 128
b.ls L(copy64_from_start)

L(loop64_backwards):
- stp1 A_l, A_h, dstend, -16
- ldp1 A_l, A_h, srcend, -16
- stp1 B_l, B_h, dstend, -32
- ldp1 B_l, B_h, srcend, -32
- stp1 C_l, C_h, dstend, -48
- ldp1 C_l, C_h, srcend, -48
- stp1_pre D_l, D_h, dstend, -64
- ldp1_pre D_l, D_h, srcend, -64
+ stp2 A_l, A_h, dstend, -16
+ ldp2 A_l, A_h, srcend, -16
+ stp2 B_l, B_h, dstend, -32
+ ldp2 B_l, B_h, srcend, -32
+ stp2 C_l, C_h, dstend, -48
+ ldp2 C_l, C_h, srcend, -48
+ stp2_pre D_l, D_h, dstend, -64
+ ldp2_pre D_l, D_h, srcend, -64
subs count, count, 64
b.hi L(loop64_backwards)

- /* Write the last iteration and copy 64 bytes from the start. */
+ /* Write the last iteration and copy 64 bytes from the start. */
L(copy64_from_start):
ldp1 G_l, G_h, src, 48
- stp1 A_l, A_h, dstend, -16
+ stp2 A_l, A_h, dstend, -16
ldp1 A_l, A_h, src, 32
- stp1 B_l, B_h, dstend, -32
+ stp2 B_l, B_h, dstend, -32
ldp1 B_l, B_h, src, 16
- stp1 C_l, C_h, dstend, -48
+ stp2 C_l, C_h, dstend, -48
ldp1 C_l, C_h, src
- stp1 D_l, D_h, dstend, -64
+ stp2 D_l, D_h, dstend, -64
stp1 G_l, G_h, dstin, 48
stp1 A_l, A_h, dstin, 32
stp1 B_l, B_h, dstin, 16
stp1 C_l, C_h, dstin
copy_exit
+
diff --git a/arch/arm64/lib/copy_template_user.S b/arch/arm64/lib/copy_template_user.S
index 1d13daf314b0..f36c77738e42 100644
--- a/arch/arm64/lib/copy_template_user.S
+++ b/arch/arm64/lib/copy_template_user.S
@@ -8,15 +8,35 @@
#include "copy_template.S"

#define ldp1 ldp1_nuao
+#define ldp2 ldp2_nuao
+#define ldp3 ldp3_nuao
#define ldp1_pre ldp1_pre_nuao
+#define ldp2_pre ldp2_pre_nuao
+#define ldp3_pre ldp3_pre_nuao
#define stp1 stp1_nuao
+#define stp2 stp2_nuao
+#define stp3 stp3_nuao
#define stp1_pre stp1_pre_nuao
+#define stp2_pre stp2_pre_nuao
+#define stp3_pre stp3_pre_nuao
#define ldr1 ldr1_nuao
+#define ldr2 ldr2_nuao
+#define ldr3 ldr3_nuao
#define str1 str1_nuao
+#define str2 str2_nuao
+#define str3 str3_nuao
#define ldrb1 ldrb1_nuao
+#define ldrb2 ldrb2_nuao
+#define ldrb3 ldrb3_nuao
#define strb1 strb1_nuao
+#define strb2 strb2_nuao
+#define strb3 strb3_nuao
#define ldrb1_reg ldrb1_nuao_reg
+#define ldrb2_reg ldrb2_nuao_reg
+#define ldrb3_reg ldrb3_nuao_reg
#define strb1_reg strb1_nuao_reg
+#define strb2_reg strb2_nuao_reg
+#define strb3_reg strb3_nuao_reg

L(copy_non_uao):
#undef L
diff --git a/arch/arm64/lib/copy_to_user.S b/arch/arm64/lib/copy_to_user.S
index ac10d2d32b03..969e2b4ac3bf 100644
--- a/arch/arm64/lib/copy_to_user.S
+++ b/arch/arm64/lib/copy_to_user.S
@@ -24,7 +24,23 @@
ldrb \reg, [\ptr, \offset]
.endm

+ .macro ldrb2 reg, ptr, offset=0
+ ldrb \reg, [\ptr, \offset]
+ .endm
+
+ .macro ldrb3 reg, ptr, offset=0
+ ldrb \reg, [\ptr, \offset]
+ .endm
+
.macro strb1 reg, ptr, offset=0
+ USER_F(9996f, sttrb \reg, [\ptr, \offset])
+ .endm
+
+ .macro strb2 reg, ptr, offset=0
+ USER_F(9997f, sttrb \reg, [\ptr, \offset])
+ .endm
+
+ .macro strb3 reg, ptr, offset=0
USER_F(9998f, sttrb \reg, [\ptr, \offset])
.endm

@@ -32,8 +48,28 @@
ldrb \reg, [\ptr, \offset]
.endm

+ .macro ldrb2_reg reg, ptr, offset
+ ldrb \reg, [\ptr, \offset]
+ .endm
+
+ .macro ldrb3_reg reg, ptr, offset
+ ldrb \reg, [\ptr, \offset]
+ .endm
+
.macro strb1_reg reg, ptr, offset
add \ptr, \ptr, \offset
+ USER_F(9996f, sttrb \reg, [\ptr])
+ sub \ptr, \ptr, \offset
+ .endm
+
+ .macro strb2_reg reg, ptr, offset
+ add \ptr, \ptr, \offset
+ USER_F(9997f, sttrb \reg, [\ptr])
+ sub \ptr, \ptr, \offset
+ .endm
+
+ .macro strb3_reg reg, ptr, offset
+ add \ptr, \ptr, \offset
USER_F(9998f, sttrb \reg, [\ptr])
sub \ptr, \ptr, \offset
.endm
@@ -42,7 +78,23 @@
ldr \reg, [\ptr, \offset]
.endm

+ .macro ldr2 reg, ptr, offset=0
+ ldr \reg, [\ptr, \offset]
+ .endm
+
+ .macro ldr3 reg, ptr, offset=0
+ ldr \reg, [\ptr, \offset]
+ .endm
+
.macro str1 reg, ptr, offset=0
+ USER_F(9996f, sttr \reg, [\ptr, \offset])
+ .endm
+
+ .macro str2 reg, ptr, offset=0
+ USER_F(9997f, sttr \reg, [\ptr, \offset])
+ .endm
+
+ .macro str3 reg, ptr, offset=0
USER_F(9998f, sttr \reg, [\ptr, \offset])
.endm

@@ -50,7 +102,25 @@
ldp \regA, \regB, [\ptr, \offset]
.endm

+ .macro ldp2 regA, regB, ptr, offset=0
+ ldp \regA, \regB, [\ptr, \offset]
+ .endm
+
+ .macro ldp3 regA, regB, ptr, offset=0
+ ldp \regA, \regB, [\ptr, \offset]
+ .endm
+
.macro stp1 regA, regB, ptr, offset=0
+ USER_F(9996f, sttr \regA, [\ptr, \offset])
+ USER_F(9996f, sttr \regB, [\ptr, \offset + 8])
+ .endm
+
+ .macro stp2 regA, regB, ptr, offset=0
+ USER_F(9997f, sttr \regA, [\ptr, \offset])
+ USER_F(9997f, sttr \regB, [\ptr, \offset + 8])
+ .endm
+
+ .macro stp3 regA, regB, ptr, offset=0
USER_F(9998f, sttr \regA, [\ptr, \offset])
USER_F(9998f, sttr \regB, [\ptr, \offset + 8])
.endm
@@ -59,7 +129,27 @@
ldp \regA, \regB, [\ptr, \offset]!
.endm

+ .macro ldp2_pre regA, regB, ptr, offset
+ ldp \regA, \regB, [\ptr, \offset]!
+ .endm
+
+ .macro ldp3_pre regA, regB, ptr, offset
+ ldp \regA, \regB, [\ptr, \offset]!
+ .endm
+
.macro stp1_pre regA, regB, ptr, offset
+ USER_F(9996f, sttr \regA, [\ptr, \offset])
+ USER_F(9996f, sttr \regB, [\ptr, \offset + 8])
+ add \ptr, \ptr, \offset
+ .endm
+
+ .macro stp2_pre regA, regB, ptr, offset
+ USER_F(9997f, sttr \regA, [\ptr, \offset])
+ USER_F(9997f, sttr \regB, [\ptr, \offset + 8])
+ add \ptr, \ptr, \offset
+ .endm
+
+ .macro stp3_pre regA, regB, ptr, offset
USER_F(9998f, sttr \regA, [\ptr, \offset])
USER_F(9998f, sttr \regB, [\ptr, \offset + 8])
add \ptr, \ptr, \offset
@@ -69,7 +159,23 @@
ldrb \reg, [\ptr, \offset]
.endm

+ .macro ldrb2_nuao reg, ptr, offset=0
+ ldrb \reg, [\ptr, \offset]
+ .endm
+
+ .macro ldrb3_nuao reg, ptr, offset=0
+ ldrb \reg, [\ptr, \offset]
+ .endm
+
.macro strb1_nuao reg, ptr, offset=0
+ USER_F(9996f, strb \reg, [\ptr, \offset])
+ .endm
+
+ .macro strb2_nuao reg, ptr, offset=0
+ USER_F(9997f, strb \reg, [\ptr, \offset])
+ .endm
+
+ .macro strb3_nuao reg, ptr, offset=0
USER_F(9998f, strb \reg, [\ptr, \offset])
.endm

@@ -77,31 +183,95 @@
ldrb \reg, [\ptr, \offset]
.endm

+ .macro ldrb2_nuao_reg reg, ptr, offset=0
+ ldrb \reg, [\ptr, \offset]
+ .endm
+
+ .macro ldrb3_nuao_reg reg, ptr, offset=0
+ ldrb \reg, [\ptr, \offset]
+ .endm
+
.macro strb1_nuao_reg reg, ptr, offset=0
- strb \reg, [\ptr, \offset]
+ USER_F(9996f, strb \reg, [\ptr, \offset])
+ .endm
+
+ .macro strb2_nuao_reg reg, ptr, offset=0
+ USER_F(9997f, strb \reg, [\ptr, \offset])
+ .endm
+
+ .macro strb3_nuao_reg reg, ptr, offset=0
+ USER_F(9998f, strb \reg, [\ptr, \offset])
.endm

.macro ldr1_nuao reg, ptr, offset=0
ldr \reg, [\ptr, \offset]
.endm

+ .macro ldr2_nuao reg, ptr, offset=0
+ ldr \reg, [\ptr, \offset]
+ .endm
+
+ .macro ldr3_nuao reg, ptr, offset=0
+ ldr \reg, [\ptr, \offset]
+ .endm
+
.macro str1_nuao reg, ptr, offset=0
+ USER_F(9996f, str \reg, [\ptr, \offset])
+ .endm
+
+ .macro str2_nuao reg, ptr, offset=0
+ USER_F(9997f, str \reg, [\ptr, \offset])
+ .endm
+
+ .macro str3_nuao reg, ptr, offset=0
USER_F(9998f, str \reg, [\ptr, \offset])
.endm

- .macro ldp1_nuao regA, regB, ptr, offset=0
+ .macro ldp1_nuao regA, regB, ptr, offset=0
ldp \regA, \regB, [\ptr, \offset]
.endm

- .macro ldp1_pre_nuao regA, regB, ptr, offset
- ldp \regA, \regB, [\ptr, \offset]!
+ .macro ldp2_nuao regA, regB, ptr, offset=0
+ ldp \regA, \regB, [\ptr, \offset]
+ .endm
+
+ .macro ldp3_nuao regA, regB, ptr, offset=0
+ ldp \regA, \regB, [\ptr, \offset]
.endm

.macro stp1_nuao regA, regB, ptr, offset=0
+ USER_F(9996f, stp \regA, \regB, [\ptr, \offset])
+ .endm
+
+ .macro stp2_nuao regA, regB, ptr, offset=0
+ USER_F(9997f, stp \regA, \regB, [\ptr, \offset])
+ .endm
+
+ .macro stp3_nuao regA, regB, ptr, offset=0
USER_F(9998f, stp \regA, \regB, [\ptr, \offset])
.endm

+ .macro ldp1_pre_nuao regA, regB, ptr, offset
+ ldp \regA, \regB, [\ptr, \offset]!
+ .endm
+
+ .macro ldp2_pre_nuao regA, regB, ptr, offset
+ ldp \regA, \regB, [\ptr, \offset]!
+ .endm
+
+ .macro ldp3_pre_nuao regA, regB, ptr, offset
+ ldp \regA, \regB, [\ptr, \offset]!
+ .endm
+
.macro stp1_pre_nuao regA, regB, ptr, offset
+ USER_F(9996f, stp \regA, \regB, [\ptr, \offset]!)
+ .endm
+
+ .macro stp2_pre_nuao regA, regB, ptr, offset
+ USER_F(9997f, stp \regA, \regB, [\ptr, \offset]!)
+ .endm
+
+ .macro stp3_pre_nuao regA, regB, ptr, offset
USER_F(9998f, stp \regA, \regB, [\ptr, \offset]!)
.endm

diff --git a/arch/arm64/lib/copy_user_fixup.S b/arch/arm64/lib/copy_user_fixup.S
index fe9f5ac19605..f878c8831b14 100644
--- a/arch/arm64/lib/copy_user_fixup.S
+++ b/arch/arm64/lib/copy_user_fixup.S
@@ -3,8 +3,19 @@
addr .req x15
.section .fixup,"ax"
.align 2
+9993:
+9994:
+9995:
+9996:
+9997:
9998:
- // TODO: add accurate fixup
+ /* Retrieve useful information & free the stack area */
+ ldr count, [sp, #16] // x2
add sp, sp, 32
+ /*
+ * Return the initial count as the (under-estimated) number
+ * of bytes that failed to copy
+ */
+ mov x0, count
ret

diff --git a/arch/arm64/lib/memcpy.S b/arch/arm64/lib/memcpy.S
index ee84b8847184..5552d6b33132 100644
--- a/arch/arm64/lib/memcpy.S
+++ b/arch/arm64/lib/memcpy.S
@@ -31,42 +31,122 @@
ldrb \reg, [\ptr, \offset]
.endm

+ .macro ldrb2 reg, ptr, offset=0
+ ldrb \reg, [\ptr, \offset]
+ .endm
+
+ .macro ldrb3 reg, ptr, offset=0
+ ldrb \reg, [\ptr, \offset]
+ .endm
+
.macro strb1 reg, ptr, offset=0
strb \reg, [\ptr, \offset]
.endm

+ .macro strb2 reg, ptr, offset=0
+ strb \reg, [\ptr, \offset]
+ .endm
+
+ .macro strb3 reg, ptr, offset=0
+ strb \reg, [\ptr, \offset]
+ .endm
+
.macro ldr1 reg, ptr, offset=0
ldr \reg, [\ptr, \offset]
.endm

+ .macro ldr2 reg, ptr, offset=0
+ ldr \reg, [\ptr, \offset]
+ .endm
+
+ .macro ldr3 reg, ptr, offset=0
+ ldr \reg, [\ptr, \offset]
+ .endm
+
.macro str1 reg, ptr, offset=0
str \reg, [\ptr, \offset]
.endm

+ .macro str2 reg, ptr, offset=0
+ str \reg, [\ptr, \offset]
+ .endm
+
+ .macro str3 reg, ptr, offset=0
+ str \reg, [\ptr, \offset]
+ .endm
+
.macro ldp1 regA, regB, ptr, offset=0
ldp \regA, \regB, [\ptr, \offset]
.endm

+ .macro ldp2 regA, regB, ptr, offset=0
+ ldp \regA, \regB, [\ptr, \offset]
+ .endm
+
+ .macro ldp3 regA, regB, ptr, offset=0
+ ldp \regA, \regB, [\ptr, \offset]
+ .endm
+
.macro stp1 regA, regB, ptr, offset=0
stp \regA, \regB, [\ptr, \offset]
.endm

+ .macro stp2 regA, regB, ptr, offset=0
+ stp \regA, \regB, [\ptr, \offset]
+ .endm
+
+ .macro stp3 regA, regB, ptr, offset=0
+ stp \regA, \regB, [\ptr, \offset]
+ .endm
+
.macro ldrb1_reg reg, ptr, offset
ldrb1 \reg, \ptr, \offset
.endm

+ .macro ldrb2_reg reg, ptr, offset
+ ldrb2 \reg, \ptr, \offset
+ .endm
+
+ .macro ldrb3_reg reg, ptr, offset
+ ldrb3 \reg, \ptr, \offset
+ .endm
+
.macro strb1_reg reg, ptr, offset
strb1 \reg, \ptr, \offset
.endm

+ .macro strb2_reg reg, ptr, offset
+ strb2 \reg, \ptr, \offset
+ .endm
+
+ .macro strb3_reg reg, ptr, offset
+ strb3 \reg, \ptr, \offset
+ .endm
+
.macro ldp1_pre regA, regB, ptr, offset
ldp \regA, \regB, [\ptr, \offset]!
.endm

+ .macro ldp2_pre regA, regB, ptr, offset
+ ldp \regA, \regB, [\ptr, \offset]!
+ .endm
+
+ .macro ldp3_pre regA, regB, ptr, offset
+ ldp \regA, \regB, [\ptr, \offset]!
+ .endm
+
.macro stp1_pre regA, regB, ptr, offset
stp \regA, \regB, [\ptr, \offset]!
.endm

+ .macro stp2_pre regA, regB, ptr, offset
+ stp \regA, \regB, [\ptr, \offset]!
+ .endm
+
+ .macro stp3_pre regA, regB, ptr, offset
+ stp \regA, \regB, [\ptr, \offset]!
+ .endm
+
.macro copy_exit
ret
.endm
--
2.17.1

2020-05-14 14:35:06

by Oliver Swede

[permalink] [raw]
Subject: [PATCH v3 12/13] arm64: Add fixup routines for usercopy load exceptions

This adds the fixup routines for exceptions that occur on load
operations while copying, by providing the calling code with a more
accurate value for the number of bytes that failed to copy.

The three routines for load exceptions work together to analyse
the position of the fault relative to the start or the end of the
buffer, and backtrack from the optimized memcpy algorithm to
determine if some number of bytes has already been successfully
copied.

The new template uses out-of-order copying, and this fixup routine is
specific to the latest memcpy implementation. It is assumed there is
no requirement to follow through with the copying of data that may
reside in temporary registers on a fault, as this would greatly
increase the fixup's complexity.

Signed-off-by: Oliver Swede <[email protected]>
---
arch/arm64/lib/copy_user_fixup.S | 170 ++++++++++++++++++++++++++++++-
1 file changed, 165 insertions(+), 5 deletions(-)

diff --git a/arch/arm64/lib/copy_user_fixup.S b/arch/arm64/lib/copy_user_fixup.S
index f878c8831b14..256a33522749 100644
--- a/arch/arm64/lib/copy_user_fixup.S
+++ b/arch/arm64/lib/copy_user_fixup.S
@@ -3,19 +3,179 @@
addr .req x15
.section .fixup,"ax"
.align 2
+
+ /*
+ * These fixup routines assume that it is not a requirement
+ * to follow through with the copying of any intermediate
+ * data in registers: this would be highly dependent on the
+ * procedure in the copy template, which utilizes out-of-order
+ * copying and is subject to change by future optimizations.
+ *
+ * The subroutine that is excuted depends on the properties of
+ * the target address in the instruction: if it is an address
+ * relative to the start or the end of the buffer, and (in the
+ * case of copy sizes larger than 128 bytes) whether it is
+ * aligned or unaligned with 16 bytes.
+ */
+
+ /*
+ * The following three routines are directed to from faults on load
+ * instructions. In each case, nothing has been copied if either:
+ * a) the copy size is less than 128 bytes, as the algorithm does
+ * not interleave load/store instruction, so nothing has been
+ * copied and the full width (srcend-src) can be returned
+ * b) the copy size is greater than or equal to 128 bytes and the
+ * src and dst buffers overlap, as this would result in a
+ * backwards copy, but the calling code expects the return
+ * value (no. bytes not copied) to be from an in-order
+ * perspective.
+ */
9993:
+ /*
+ * This is reached from load instructions that are specified
+ * relative to the start of a user space memory buffer, and
+ * are not guaranteed to be aligned to 16B.
+ */
+
+ /* Retrieve useful information & free the stack area */
+ ldp dst, src, [sp], #16 // dst: x3, src: x1
+ ldr count, [sp], #16 // count: x2
+ add srcend, src, count
+ add dstend, dst, count
+
+ /* Copy size < 128 bytes */
+ cmp count, 128
+ b.ls L(none_copied)
+ /*
+ * Overlapping buffers:
+ * (src <= dst && dst < srcend) || (dst <= src && src < dstend)
+ */
+ cmp src, dst
+ ccmp dst, srcend, #0, le
+ b.lt L(none_copied)
+ cmp dst, src
+ ccmp src, dstend, #0, le
+ b.lt L(none_copied)
+
+ /*
+ * The fault occurred in a load instruction at the start of the
+ * algorithm's subroutine for long copies, and no bytes have
+ * been stored at this point.
+ */
+ b L(none_copied)
9994:
+ /*
+ * This is reached from load instructions that are specified
+ * relative to the end of a user space memory buffer, and
+ * are not guaranteed to be aligned to 16B.
+ */
+
+ /* Store the current dst before the original is
+ * restored */
+ mov tmp1, dst
+
+ /* Retrieve useful information & free the stack area */
+ ldp dst, src, [sp], #16 // dst: x3, src: x1
+ ldr count, [sp], #16 // count: x2
+ add srcend, src, count
+ add dstend, dst, count
+
+ /* Copy size < 128 bytes */
+ cmp count, 128
+ b.ls L(none_copied)
+ /*
+ * Overlapping buffers:
+ * (src <= dst && dst < srcend) || (dst <= src && src < dstend)
+ */
+ cmp src, dst
+ ccmp dst, srcend, #0, le
+ b.lt L(none_copied)
+ cmp dst, src
+ ccmp src, dstend, #0, le
+ b.lt L(none_copied)
+
+ /*
+ * In the case of an access relative to the end of
+ * the buffer, the long copy has reached the final
+ * 64B chunk in copy64_from_end().
+ *
+ * The fault address should fall in 1-of-4 16B blocks,
+ * each of which indicates how many bytes have been
+ * stored from the in-order perspective.
+ *
+ * A number of iterations of the loop to copy 64B
+ * chunks may have completed; tmp1 stores the
+ * latest position of the dst pointer and this can
+ * be used to deduce how many 16B copies have taken
+ * place.
+ */
+
+ /* Calculate the index of the 16B block containing
+ * containing the fault address */
+ sub x0, srcend, 64
+ cmp addr, x0
+ b.lt L(none_copied) // unexpected fault address
+ sub x0, addr, x0 // relative offset of fault in buffer
+ bic x0, x0, 15 // assume no data copied between addr and the target
+ sub tmp1, dst, tmp1 // already copied up to dst
+ add x0, x0, tmp1 // plus the difference between addr and srcend-64
+ sub x0, count, x0 // no. bytes not copied
+ b L(end_fixup)
+
9995:
+ /*
+ * This is reached from load instructions that are specified
+ * relative to the start of a user space memory buffer, and
+ * are guaranteed to be aligned to 16B.
+ */
+
+ /* Retrieve useful information & free the stack area */
+ ldp dst, src, [sp], #16 // dst: x3, src: x1
+ ldr count, [sp], #16 // count: x2
+ add srcend, src, count
+ add dstend, dst, count
+
+ /* Copy size <= 128 bytes */
+ cmp count, 128
+ b.ls L(none_copied)
+
+ /*
+ * Overlapping buffers:
+ * (src <= dst && dst < srcend) || (dst <= src && src < dstend)
+ */
+ cmp src, dst
+ ccmp dst, srcend, #0, le
+ b.lt L(none_copied)
+ cmp dst, src
+ ccmp src, dstend, #0, le
+ b.lt L(none_copied)
+
+ sub x0, addr, src
+ lsr x0, addr, 4 // calculate the index of faulting 16B block
+ /* Map the index in x0 to the no. bytes already copied */
+ cmp x0, 1
+ b.le L(none_copied) // no stores for i=0,1
+ cmp x0, 4
+ mov tmp1, x0
+ sub x0, count, 16
+ b.le L(end_fixup) // one store (16B) for i=2,3,4
+ /* Faulted in a loop: stored up to ((i-3) * 16) - (dst % 16) */
+ mov x0, tmp1
+ sub x0, x0, 3
+ lsl x0, x0, 4
+ mov tmp1, dst
+ and tmp1, tmp1, 15
+ sub x0, x0, tmp1
+ b L(end_fixup)
+
9996:
9997:
9998:
- /* Retrieve useful information & free the stack area */
- ldr count, [sp, #16] // x2
- add sp, sp, 32
+L(none_copied):
/*
- * Return the initial count as the (under-estimated) number
+ * Return the initial count as the number
* of bytes that failed to copy
*/
mov x0, count
+L(end_fixup):
ret
-
--
2.17.1

2020-05-14 14:35:10

by Oliver Swede

[permalink] [raw]
Subject: [PATCH v3 13/13] arm64: Add fixup routines for usercopy store exceptions

This adds the fixup routines for exceptions that occur on store
operations while copying, by providing the calling code with a more
accurate value for the number of bytes that failed to copy, rather
than returning the full buffer width.

The three routines for store exceptions work together to analyse
the position of the fault relative to the start or the end of the
buffer, and backtrack from the optimized memcpy algorithm to
determine if some number of bytes has already been successfully
copied.

The store operations occur mostly in-order, with the exception of
a few copy size ranges - this is specific to the new copy template,
which uses the latest memcpy implementation.

Signed-off-by: Oliver Swede <[email protected]>
---
arch/arm64/lib/copy_user_fixup.S | 96 ++++++++++++++++++++++++++++++++
1 file changed, 96 insertions(+)

diff --git a/arch/arm64/lib/copy_user_fixup.S b/arch/arm64/lib/copy_user_fixup.S
index 256a33522749..d836fa6cc333 100644
--- a/arch/arm64/lib/copy_user_fixup.S
+++ b/arch/arm64/lib/copy_user_fixup.S
@@ -168,9 +168,105 @@ addr .req x15
sub x0, x0, tmp1
b L(end_fixup)

+ /*
+ * The following three routines are directed to from faults
+ * on store instructions.
+ */
9996:
+ /*
+ * This routine is reached from faults on store instructions
+ * where the target address has been specified relative to the
+ * start of the user space memory buffer, and is also not
+ * guaranteed to be aligned to 16B.
+ *
+ * For copy sizes <= 128 bytes, the stores occur in-order,
+ * so it has copied up to (addr-dst)&~15.
+ * For copy sizes > 128 bytes, this should only be directed
+ * to from a fault on the first store of the long copy, before
+ * the algorithm aligns dst to 16B, so no bytes have copied at
+ * this point.
+ */
+
+ /* Retrieve useful information & free the stack area */
+ ldr dst, [sp], #16 // dst: x3
+ ldr count, [sp], #16 // count: x2
+
+ cmp count, 0
+ b.eq L(none_copied)
+ cmp count, 3
+ sub x0, addr, dst // relative fault offset in buffer
+ bic x0, x0, 7 // bytes already copied (steps of 8B stores)
+ sub x0, count, x0 // bytes yet to copy
+ b.le L(end_fixup)
+ cmp count, 32
+ b.le L(none_copied)
+ cmp count, 128
+ sub x0, addr, dst // relative fault offset in buffer
+ bic x0, x0, 15 // bytes already copied (steps of 16B stores)
+ sub x0, count, x0 // bytes yet to copy
+ b.le L(end_fixup)
+ b L(none_copied)
+
9997:
+ /*
+ * This routine is reached from faults on store instructions
+ * where the target address has been specified relative to the
+ * end of the user space memory buffer and is also not
+ * guaranteed to be aligned with 16B.
+ *
+ * In this scenario, the the copy is close to completion and
+ * has occurred in-order, so the last few bytes to copy can
+ * easily be calculated.
+ *
+ * This caters for the overlapping stage, as it could
+ * potentially fault on data that has already been copied.
+ */
+
+ /* Retrieve useful information & free the stack area */
+ ldr dst, [sp], #16 // dst: x3
+ ldr count, [sp], #16 // count: x2
+ add dstend, dst, count
+
+ sub x0, dstend, addr
+ bic x0, x0, 15 // remaining bytes to copy
+ b L(end_fixup)
+
9998:
+ /*
+ * This routine is reached from faults on store instructions
+ * where the target address has been specified relative to the
+ * start of the user space memory buffer, and is also guaranteed
+ * to be aligned to 16B.
+ *
+ * These instrucions occur after the algorithm aligns dst to 16B,
+ * after the very first store in a long copy. It then continues
+ * copying from dst+16 onwards.
+ *
+ * This could result in an overlapping copy if the original dst
+ * is unaligned with 16B. However, this implies that it could
+ * potentially fault on data that has already been copied, if
+ * the fault occurs in the first aligned access.
+ *
+ * We want to report that 16 bytes has already successfully copied in this case.
+ */
+
+ /* Retrieve useful information & free the stack area */
+ ldr dst, [sp], #16 // dst: x3
+ ldr count, [sp], #16 // count: x2
+
+ bic tmp1, dst, 15 // aligned dst
+ bic x0, addr, 15
+ sub x0, x0, tmp1 // relative fault offset
+ cmp x0, 0 // unexpected range for this fixup
+ b.eq L(none_copied)
+ cmp x0, 16
+ bic x0, addr, 15
+ sub x0, x0, dst
+ sub x0, count, x0
+ b.gt L(end_fixup)
+ sub x0, count, 16
+ b L(end_fixup) // initial unaligned chunk copied
+
L(none_copied):
/*
* Return the initial count as the number
--
2.17.1

2020-05-14 14:36:03

by Oliver Swede

[permalink] [raw]
Subject: [PATCH v3 06/13] arm64: Import latest version of Cortex Strings' strlen

From: Sam Tebbs <[email protected]>

Import latest version of Cortex Strings' strlen function.

The upstream source is src/aarch64/strlen.S as of commit eb80ac77a6cd
in https://git.linaro.org/toolchain/cortex-strings.git.

Signed-off-by: Sam Tebbs <[email protected]>
[ rm: update attribution, expand commit message ]
Signed-off-by: Robin Murphy <[email protected]>
Signed-off-by: Oliver Swede <[email protected]>
---
arch/arm64/lib/strlen.S | 247 +++++++++++++++++++++++++++-------------
1 file changed, 168 insertions(+), 79 deletions(-)

diff --git a/arch/arm64/lib/strlen.S b/arch/arm64/lib/strlen.S
index ee3ed882dd79..974b67dcc186 100644
--- a/arch/arm64/lib/strlen.S
+++ b/arch/arm64/lib/strlen.S
@@ -1,13 +1,11 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/*
- * Copyright (C) 2013 ARM Ltd.
- * Copyright (C) 2013 Linaro.
+ * Copyright (c) 2013-2015 Linaro Limited. All rights reserved.
*
- * This code is based on glibc cortex strings work originally authored by Linaro
- * be found @
+ * This code is based on glibc Cortex Strings work originally authored by
+ * Linaro, found at:
*
- * http://bazaar.launchpad.net/~linaro-toolchain-dev/cortex-strings/trunk/
- * files/head:/src/aarch64/
+ * https://git.linaro.org/toolchain/cortex-strings.git
*/

#include <linux/linkage.h>
@@ -23,93 +21,184 @@
*/

/* Arguments and results. */
-srcin .req x0
-len .req x0
+#define srcin x0
+#define len x0

/* Locals and temporaries. */
-src .req x1
-data1 .req x2
-data2 .req x3
-data2a .req x4
-has_nul1 .req x5
-has_nul2 .req x6
-tmp1 .req x7
-tmp2 .req x8
-tmp3 .req x9
-tmp4 .req x10
-zeroones .req x11
-pos .req x12
+#define src x1
+#define data1 x2
+#define data2 x3
+#define has_nul1 x4
+#define has_nul2 x5
+#define tmp1 x4
+#define tmp2 x5
+#define tmp3 x6
+#define tmp4 x7
+#define zeroones x8
+
+#define L(l) .L ## l
+
+ /* NUL detection works on the principle that (X - 1) & (~X) & 0x80
+ (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
+ can be done in parallel across the entire word. A faster check
+ (X - 1) & 0x80 is zero for non-NUL ASCII characters, but gives
+ false hits for characters 129..255. */

#define REP8_01 0x0101010101010101
#define REP8_7f 0x7f7f7f7f7f7f7f7f
#define REP8_80 0x8080808080808080

+#ifdef TEST_PAGE_CROSS
+# define MIN_PAGE_SIZE 15
+#else
+# define MIN_PAGE_SIZE 4096
+#endif
+
+ /* Since strings are short on average, we check the first 16 bytes
+ of the string for a NUL character. In order to do an unaligned ldp
+ safely we have to do a page cross check first. If there is a NUL
+ byte we calculate the length from the 2 8-byte words using
+ conditional select to reduce branch mispredictions (it is unlikely
+ strlen will be repeatedly called on strings with the same length).
+
+ If the string is longer than 16 bytes, we align src so don't need
+ further page cross checks, and process 32 bytes per iteration
+ using the fast NUL check. If we encounter non-ASCII characters,
+ fallback to a second loop using the full NUL check.
+
+ If the page cross check fails, we read 16 bytes from an aligned
+ address, remove any characters before the string, and continue
+ in the main loop using aligned loads. Since strings crossing a
+ page in the first 16 bytes are rare (probability of
+ 16/MIN_PAGE_SIZE ~= 0.4%), this case does not need to be optimized.
+
+ AArch64 systems have a minimum page size of 4k. We don't bother
+ checking for larger page sizes - the cost of setting up the correct
+ page size is just not worth the extra gain from a small reduction in
+ the cases taking the slow path. Note that we only care about
+ whether the first fetch, which may be misaligned, crosses a page
+ boundary. */
+
SYM_FUNC_START_WEAK_PI(strlen)
- mov zeroones, #REP8_01
- bic src, srcin, #15
- ands tmp1, srcin, #15
- b.ne .Lmisaligned
- /*
- * NUL detection works on the principle that (X - 1) & (~X) & 0x80
- * (=> (X - 1) & ~(X | 0x7f)) is non-zero iff a byte is zero, and
- * can be done in parallel across the entire word.
- */
- /*
- * The inner loop deals with two Dwords at a time. This has a
- * slightly higher start-up cost, but we should win quite quickly,
- * especially on cores with a high number of issue slots per
- * cycle, as we get much better parallelism out of the operations.
- */
-.Lloop:
- ldp data1, data2, [src], #16
-.Lrealigned:
+ and tmp1, srcin, MIN_PAGE_SIZE - 1
+ mov zeroones, REP8_01
+ cmp tmp1, MIN_PAGE_SIZE - 16
+ b.gt L(page_cross)
+ ldp data1, data2, [srcin]
+ /* For big-endian, carry propagation (if the final byte in the
+ string is 0x01) means we cannot use has_nul1/2 directly.
+ Since we expect strings to be small and early-exit,
+ byte-swap the data now so has_null1/2 will be correct. */
+CPU_BE(rev data1, data1)
+CPU_BE(rev data2, data2)
+ sub tmp1, data1, zeroones
+ orr tmp2, data1, REP8_7f
+ sub tmp3, data2, zeroones
+ orr tmp4, data2, REP8_7f
+ bics has_nul1, tmp1, tmp2
+ bic has_nul2, tmp3, tmp4
+ ccmp has_nul2, 0, 0, eq
+ beq L(main_loop_entry)
+
+ /* Enter with C = has_nul1 == 0. */
+ csel has_nul1, has_nul1, has_nul2, cc
+ mov len, 8
+ rev has_nul1, has_nul1
+ clz tmp1, has_nul1
+ csel len, xzr, len, cc
+ add len, len, tmp1, lsr 3
+ ret
+
+ /* The inner loop processes 32 bytes per iteration and uses the fast
+ NUL check. If we encounter non-ASCII characters, use a second
+ loop with the accurate NUL check. */
+ .p2align 4
+L(main_loop_entry):
+ bic src, srcin, 15
+ sub src, src, 16
+L(main_loop):
+ ldp data1, data2, [src, 32]!
+.Lpage_cross_entry:
sub tmp1, data1, zeroones
- orr tmp2, data1, #REP8_7f
sub tmp3, data2, zeroones
- orr tmp4, data2, #REP8_7f
- bic has_nul1, tmp1, tmp2
- bics has_nul2, tmp3, tmp4
- ccmp has_nul1, #0, #0, eq /* NZCV = 0000 */
- b.eq .Lloop
+ orr tmp2, tmp1, tmp3
+ tst tmp2, zeroones, lsl 7
+ bne 1f
+ ldp data1, data2, [src, 16]
+ sub tmp1, data1, zeroones
+ sub tmp3, data2, zeroones
+ orr tmp2, tmp1, tmp3
+ tst tmp2, zeroones, lsl 7
+ beq L(main_loop)
+ add src, src, 16
+1:
+ /* The fast check failed, so do the slower, accurate NUL check. */
+ orr tmp2, data1, REP8_7f
+ orr tmp4, data2, REP8_7f
+ bics has_nul1, tmp1, tmp2
+ bic has_nul2, tmp3, tmp4
+ ccmp has_nul2, 0, 0, eq
+ beq L(nonascii_loop)

+ /* Enter with C = has_nul1 == 0. */
+L(tail):
+ /* For big-endian, carry propagation (if the final byte in the
+ string is 0x01) means we cannot use has_nul1/2 directly. The
+ easiest way to get the correct byte is to byte-swap the data
+ and calculate the syndrome a second time. */
+CPU_BE(csel data1, data1, data2, cc)
+CPU_BE(rev data1, data1)
+CPU_BE(sub tmp1, data1, zeroones)
+CPU_BE(orr tmp2, data1, REP8_7f)
+CPU_BE(bic has_nul1, tmp1, tmp2)
+CPU_LE(csel has_nul1, has_nul1, has_nul2, cc)
sub len, src, srcin
- cbz has_nul1, .Lnul_in_data2
-CPU_BE( mov data2, data1 ) /*prepare data to re-calculate the syndrome*/
- sub len, len, #8
- mov has_nul2, has_nul1
-.Lnul_in_data2:
- /*
- * For big-endian, carry propagation (if the final byte in the
- * string is 0x01) means we cannot use has_nul directly. The
- * easiest way to get the correct byte is to byte-swap the data
- * and calculate the syndrome a second time.
- */
-CPU_BE( rev data2, data2 )
-CPU_BE( sub tmp1, data2, zeroones )
-CPU_BE( orr tmp2, data2, #REP8_7f )
-CPU_BE( bic has_nul2, tmp1, tmp2 )
-
- sub len, len, #8
- rev has_nul2, has_nul2
- clz pos, has_nul2
- add len, len, pos, lsr #3 /* Bits to bytes. */
+ rev has_nul1, has_nul1
+ add tmp2, len, 8
+ clz tmp1, has_nul1
+ csel len, len, tmp2, cc
+ add len, len, tmp1, lsr 3
ret

-.Lmisaligned:
- cmp tmp1, #8
- neg tmp1, tmp1
- ldp data1, data2, [src], #16
- lsl tmp1, tmp1, #3 /* Bytes beyond alignment -> bits. */
- mov tmp2, #~0
- /* Big-endian. Early bytes are at MSB. */
-CPU_BE( lsl tmp2, tmp2, tmp1 ) /* Shift (tmp1 & 63). */
- /* Little-endian. Early bytes are at LSB. */
-CPU_LE( lsr tmp2, tmp2, tmp1 ) /* Shift (tmp1 & 63). */
+L(nonascii_loop):
+ ldp data1, data2, [src, 16]!
+ sub tmp1, data1, zeroones
+ orr tmp2, data1, REP8_7f
+ sub tmp3, data2, zeroones
+ orr tmp4, data2, REP8_7f
+ bics has_nul1, tmp1, tmp2
+ bic has_nul2, tmp3, tmp4
+ ccmp has_nul2, 0, 0, eq
+ bne L(tail)
+ ldp data1, data2, [src, 16]!
+ sub tmp1, data1, zeroones
+ orr tmp2, data1, REP8_7f
+ sub tmp3, data2, zeroones
+ orr tmp4, data2, REP8_7f
+ bics has_nul1, tmp1, tmp2
+ bic has_nul2, tmp3, tmp4
+ ccmp has_nul2, 0, 0, eq
+ beq L(nonascii_loop)
+ b L(tail)

- orr data1, data1, tmp2
- orr data2a, data2, tmp2
- csinv data1, data1, xzr, le
- csel data2, data2, data2a, le
- b .Lrealigned
+ /* Load 16 bytes from [srcin & ~15] and force the bytes that precede
+ srcin to 0x7f, so we ignore any NUL bytes before the string.
+ Then continue in the aligned loop. */
+L(page_cross):
+ bic src, srcin, 15
+ ldp data1, data2, [src]
+ lsl tmp1, srcin, 3
+ mov tmp4, -1
+ /* Big-endian. Early bytes are at MSB. */
+CPU_BE(lsr tmp1, tmp4, tmp1) /* Shift (tmp1 & 63). */
+ /* Little-endian. Early bytes are at LSB. */
+CPU_LE(lsl tmp1, tmp4, tmp1) /* Shift (tmp1 & 63). */
+ orr tmp1, tmp1, REP8_80
+ orn data1, data1, tmp1
+ orn tmp2, data2, tmp1
+ tst srcin, 8
+ csel data1, data1, tmp4, eq
+ csel data2, data2, tmp2, eq
+ b L(page_cross_entry)
SYM_FUNC_END_PI(strlen)
EXPORT_SYMBOL_NOKASAN(strlen)
--
2.17.1

2020-05-14 14:36:11

by Oliver Swede

[permalink] [raw]
Subject: [PATCH v3 04/13] arm64: Import latest version of Cortex Strings' memmove

From: Sam Tebbs <[email protected]>

Import the latest version of Cortex Strings' memmove function.

The upstream source is src/aarch64/memmove.S as of commit 99b01ddb8e41
in https://git.linaro.org/toolchain/cortex-strings.git.

Signed-off-by: Sam Tebbs <[email protected]>
[ rm: update attribution, expand commit message ]
Signed-off-by: Robin Murphy <[email protected]>
Signed-off-by: Oliver Swede <[email protected]>
---
arch/arm64/lib/memmove.S | 232 +++++++++++++--------------------------
1 file changed, 78 insertions(+), 154 deletions(-)

diff --git a/arch/arm64/lib/memmove.S b/arch/arm64/lib/memmove.S
index 02cda2e33bde..d0977d0ad745 100644
--- a/arch/arm64/lib/memmove.S
+++ b/arch/arm64/lib/memmove.S
@@ -1,13 +1,12 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/*
- * Copyright (C) 2013 ARM Ltd.
- * Copyright (C) 2013 Linaro.
+ * Copyright (c) 2013 Linaro Limited. All rights reserved.
+ * Copyright (c) 2015 ARM Ltd. All rights reserved.
*
- * This code is based on glibc cortex strings work originally authored by Linaro
- * be found @
+ * This code is based on glibc Cortex Strings work originally authored by
+ * Linaro, found at:
*
- * http://bazaar.launchpad.net/~linaro-toolchain-dev/cortex-strings/trunk/
- * files/head:/src/aarch64/
+ * https://git.linaro.org/toolchain/cortex-strings.git
*/

#include <linux/linkage.h>
@@ -25,165 +24,90 @@
* Returns:
* x0 - dest
*/
-dstin .req x0
-src .req x1
-count .req x2
-tmp1 .req x3
-tmp1w .req w3
-tmp2 .req x4
-tmp2w .req w4
-tmp3 .req x5
-tmp3w .req w5
-dst .req x6
+/* Parameters and result. */
+#define dstin x0
+#define src x1
+#define count x2
+#define srcend x3
+#define dstend x4
+#define tmp1 x5
+#define A_l x6
+#define A_h x7
+#define B_l x8
+#define B_h x9
+#define C_l x10
+#define C_h x11
+#define D_l x12
+#define D_h x13
+#define E_l count
+#define E_h tmp1

-A_l .req x7
-A_h .req x8
-B_l .req x9
-B_h .req x10
-C_l .req x11
-C_h .req x12
-D_l .req x13
-D_h .req x14
+/* All memmoves up to 96 bytes are done by memcpy as it supports overlaps.
+ Larger backwards copies are also handled by memcpy. The only remaining
+ case is forward large copies. The destination is aligned, and an
+ unrolled loop processes 64 bytes per iteration.
+*/

- .weak memmove
+ .weak memmove
SYM_FUNC_START_ALIAS(__memmove)
SYM_FUNC_START_PI(memmove)
- cmp dstin, src
- b.lo __memcpy
- add tmp1, src, count
- cmp dstin, tmp1
- b.hs __memcpy /* No overlap. */
+ sub tmp1, dstin, src
+ cmp count, 96
+ ccmp tmp1, count, 2, hi
+ b.hs __memcpy

- add dst, dstin, count
- add src, src, count
- cmp count, #16
- b.lo .Ltail15 /*probably non-alignment accesses.*/
+ cbz tmp1, 3f
+ add dstend, dstin, count
+ add srcend, src, count

- ands tmp2, src, #15 /* Bytes to reach alignment. */
- b.eq .LSrcAligned
- sub count, count, tmp2
- /*
- * process the aligned offset length to make the src aligned firstly.
- * those extra instructions' cost is acceptable. It also make the
- * coming accesses are based on aligned address.
- */
- tbz tmp2, #0, 1f
- ldrb tmp1w, [src, #-1]!
- strb tmp1w, [dst, #-1]!
-1:
- tbz tmp2, #1, 2f
- ldrh tmp1w, [src, #-2]!
- strh tmp1w, [dst, #-2]!
-2:
- tbz tmp2, #2, 3f
- ldr tmp1w, [src, #-4]!
- str tmp1w, [dst, #-4]!
-3:
- tbz tmp2, #3, .LSrcAligned
- ldr tmp1, [src, #-8]!
- str tmp1, [dst, #-8]!
-
-.LSrcAligned:
- cmp count, #64
- b.ge .Lcpy_over64
+ /* Align dstend to 16 byte alignment so that we don't cross cache line
+ boundaries on both loads and stores. There are at least 96 bytes
+ to copy, so copy 16 bytes unaligned and then align. The loop
+ copies 64 bytes per iteration and prefetches one iteration ahead. */

- /*
- * Deal with small copies quickly by dropping straight into the
- * exit block.
- */
-.Ltail63:
- /*
- * Copy up to 48 bytes of data. At this point we only need the
- * bottom 6 bits of count to be accurate.
- */
- ands tmp1, count, #0x30
- b.eq .Ltail15
- cmp tmp1w, #0x20
- b.eq 1f
- b.lt 2f
- ldp A_l, A_h, [src, #-16]!
- stp A_l, A_h, [dst, #-16]!
+ and tmp1, dstend, 15
+ ldp D_l, D_h, [srcend, -16]
+ sub srcend, srcend, tmp1
+ sub count, count, tmp1
+ ldp A_l, A_h, [srcend, -16]
+ stp D_l, D_h, [dstend, -16]
+ ldp B_l, B_h, [srcend, -32]
+ ldp C_l, C_h, [srcend, -48]
+ ldp D_l, D_h, [srcend, -64]!
+ sub dstend, dstend, tmp1
+ subs count, count, 128
+ b.ls 2f
+ nop
1:
- ldp A_l, A_h, [src, #-16]!
- stp A_l, A_h, [dst, #-16]!
-2:
- ldp A_l, A_h, [src, #-16]!
- stp A_l, A_h, [dst, #-16]!
+ stp A_l, A_h, [dstend, -16]
+ ldp A_l, A_h, [srcend, -16]
+ stp B_l, B_h, [dstend, -32]
+ ldp B_l, B_h, [srcend, -32]
+ stp C_l, C_h, [dstend, -48]
+ ldp C_l, C_h, [srcend, -48]
+ stp D_l, D_h, [dstend, -64]!
+ ldp D_l, D_h, [srcend, -64]!
+ subs count, count, 64
+ b.hi 1b

-.Ltail15:
- tbz count, #3, 1f
- ldr tmp1, [src, #-8]!
- str tmp1, [dst, #-8]!
-1:
- tbz count, #2, 2f
- ldr tmp1w, [src, #-4]!
- str tmp1w, [dst, #-4]!
+ /* Write the last full set of 64 bytes. The remainder is at most 64
+ bytes, so it is safe to always copy 64 bytes from the start even if
+ there is just 1 byte left. */
2:
- tbz count, #1, 3f
- ldrh tmp1w, [src, #-2]!
- strh tmp1w, [dst, #-2]!
-3:
- tbz count, #0, .Lexitfunc
- ldrb tmp1w, [src, #-1]
- strb tmp1w, [dst, #-1]
-
-.Lexitfunc:
- ret
-
-.Lcpy_over64:
- subs count, count, #128
- b.ge .Lcpy_body_large
- /*
- * Less than 128 bytes to copy, so handle 64 bytes here and then jump
- * to the tail.
- */
- ldp A_l, A_h, [src, #-16]
- stp A_l, A_h, [dst, #-16]
- ldp B_l, B_h, [src, #-32]
- ldp C_l, C_h, [src, #-48]
- stp B_l, B_h, [dst, #-32]
- stp C_l, C_h, [dst, #-48]
- ldp D_l, D_h, [src, #-64]!
- stp D_l, D_h, [dst, #-64]!
-
- tst count, #0x3f
- b.ne .Ltail63
- ret
-
- /*
- * Critical loop. Start at a new cache line boundary. Assuming
- * 64 bytes per line this ensures the entire loop is in one line.
- */
- .p2align L1_CACHE_SHIFT
-.Lcpy_body_large:
- /* pre-load 64 bytes data. */
- ldp A_l, A_h, [src, #-16]
- ldp B_l, B_h, [src, #-32]
- ldp C_l, C_h, [src, #-48]
- ldp D_l, D_h, [src, #-64]!
-1:
- /*
- * interlace the load of next 64 bytes data block with store of the last
- * loaded 64 bytes data.
- */
- stp A_l, A_h, [dst, #-16]
- ldp A_l, A_h, [src, #-16]
- stp B_l, B_h, [dst, #-32]
- ldp B_l, B_h, [src, #-32]
- stp C_l, C_h, [dst, #-48]
- ldp C_l, C_h, [src, #-48]
- stp D_l, D_h, [dst, #-64]!
- ldp D_l, D_h, [src, #-64]!
- subs count, count, #64
- b.ge 1b
- stp A_l, A_h, [dst, #-16]
- stp B_l, B_h, [dst, #-32]
- stp C_l, C_h, [dst, #-48]
- stp D_l, D_h, [dst, #-64]!
+ ldp E_l, E_h, [src, 48]
+ stp A_l, A_h, [dstend, -16]
+ ldp A_l, A_h, [src, 32]
+ stp B_l, B_h, [dstend, -32]
+ ldp B_l, B_h, [src, 16]
+ stp C_l, C_h, [dstend, -48]
+ ldp C_l, C_h, [src]
+ stp D_l, D_h, [dstend, -64]
+ stp E_l, E_h, [dstin, 48]
+ stp A_l, A_h, [dstin, 32]
+ stp B_l, B_h, [dstin, 16]
+ stp C_l, C_h, [dstin]
+3: ret

- tst count, #0x3f
- b.ne .Ltail63
- ret
SYM_FUNC_END_PI(memmove)
EXPORT_SYMBOL(memmove)
SYM_FUNC_END_ALIAS(__memmove)
--
2.17.1

2020-05-14 14:36:43

by Oliver Swede

[permalink] [raw]
Subject: [PATCH v3 02/13] arm64: kprobes: Drop open-coded exception fixup

From: Robin Murphy <[email protected]>

The short-circuit call to fixup_exception() from kprobe_fault_handler()
poses a problem now that the former wants to consume the fault address
too, since the common kprobes API offers us no way to pass it through.
Fortunately, however, it works out to be unnecessary:

- uaccess instructions themselves are not probeable, so at most we
should only ever expect to take a fixable fault from the pre or post
handlers.
- the pre and post handler run with preemption disabled, thus for any
fault they may cause, an unhandled return from kprobe_page_fault()
will proceed directly to __do_kernel_fault() thanks to the
faulthandler_disabled() check.
- __do_kernel_fault() will immediately call fixup_exception() unless
we're in an EL1 instruction abort, and if we've somehow taken one of
those on what we think is the middle of a uaccess routine, then the
world is already very on fire.

Thus we can reasonably drop the call from kprobe_fault_handler() and
leave uaccess fixups to the regular flow.

Signed-off-by: Robin Murphy <[email protected]>
Signed-off-by: Oliver Swede <[email protected]>
---
arch/arm64/kernel/probes/kprobes.c | 7 -------
1 file changed, 7 deletions(-)

diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c
index d1c95dcf1d78..771635360110 100644
--- a/arch/arm64/kernel/probes/kprobes.c
+++ b/arch/arm64/kernel/probes/kprobes.c
@@ -334,13 +334,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
*/
if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
return 1;
-
- /*
- * In case the user-specified fault handler returned
- * zero, try to fix up.
- */
- if (fixup_exception(regs))
- return 1;
}
return 0;
}
--
2.17.1

2020-05-14 14:37:37

by Oliver Swede

[permalink] [raw]
Subject: [PATCH v3 09/13] arm64: Tidy up _asm_extable_faultaddr usage

From: Robin Murphy <[email protected]>

To match the way the USER() shorthand wraps _asm_extable entries,
introduce USER_F() to wrap _asm_extable_faultaddr and clean up a bit.

Signed-off-by: Robin Murphy <[email protected]>
Signed-off-by: Oliver Swede <[email protected]>
---
arch/arm64/include/asm/assembler.h | 4 ++
arch/arm64/lib/copy_from_user.S | 36 +++++----------
arch/arm64/lib/copy_in_user.S | 72 ++++++++++--------------------
arch/arm64/lib/copy_to_user.S | 33 +++++---------
4 files changed, 51 insertions(+), 94 deletions(-)

diff --git a/arch/arm64/include/asm/assembler.h b/arch/arm64/include/asm/assembler.h
index 7017aeb4b29a..384b6584b27f 100644
--- a/arch/arm64/include/asm/assembler.h
+++ b/arch/arm64/include/asm/assembler.h
@@ -142,6 +142,10 @@ alternative_endif
9999: x; \
_asm_extable 9999b, l

+#define USER_F(l, x...) \
+9999: x; \
+ _asm_extable_faultaddr 9999b, l
+
/*
* Register aliases.
*/
diff --git a/arch/arm64/lib/copy_from_user.S b/arch/arm64/lib/copy_from_user.S
index dbf768cc7650..9c3805725bea 100644
--- a/arch/arm64/lib/copy_from_user.S
+++ b/arch/arm64/lib/copy_from_user.S
@@ -21,8 +21,7 @@
*/

.macro ldrb1 reg, ptr, offset=0
- 8888: ldtrb \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, ldtrb \reg, [\ptr, \offset])
.endm

.macro strb1 reg, ptr, offset=0
@@ -31,9 +30,8 @@

.macro ldrb1_reg reg, ptr, offset
add \ptr, \ptr, \offset
- 8888: ldtrb \reg, [\ptr]
+ USER_F(9998f, ldtrb \reg, [\ptr])
sub \ptr, \ptr, \offset
- _asm_extable_faultaddr 8888b,9998f;
.endm

.macro strb1_reg reg, ptr, offset
@@ -41,8 +39,7 @@
.endm

.macro ldr1 reg, ptr, offset=0
- 8888: ldtr \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, ldtr \reg, [\ptr, \offset])
.endm

.macro str1 reg, ptr, offset=0
@@ -50,10 +47,8 @@
.endm

.macro ldp1 regA, regB, ptr, offset=0
- 8888: ldtr \regA, [\ptr, \offset]
- 8889: ldtr \regB, [\ptr, \offset + 8]
- _asm_extable_faultaddr 8888b,9998f;
- _asm_extable_faultaddr 8889b,9998f;
+ USER_F(9998f, ldtr \regA, [\ptr, \offset])
+ USER_F(9998f, ldtr \regB, [\ptr, \offset + 8])
.endm

.macro stp1 regA, regB, ptr, offset=0
@@ -61,11 +56,9 @@
.endm

.macro ldp1_pre regA, regB, ptr, offset
- 8888: ldtr \regA, [\ptr, \offset]
- 8889: ldtr \regB, [\ptr, \offset + 8]
+ USER_F(9998f, ldtr \regA, [\ptr, \offset])
+ USER_F(9998f, ldtr \regB, [\ptr, \offset + 8])
add \ptr, \ptr, \offset
- _asm_extable_faultaddr 8888b,9998f;
- _asm_extable_faultaddr 8889b,9998f;
.endm

.macro stp1_pre regA, regB, ptr, offset
@@ -73,8 +66,7 @@
.endm

.macro ldrb1_nuao reg, ptr, offset=0
- 8888: ldrb \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, ldrb \reg, [\ptr, \offset])
.endm

.macro strb1_nuao reg, ptr, offset=0
@@ -82,8 +74,7 @@
.endm

.macro ldrb1_nuao_reg reg, ptr, offset=0
- 8888: ldrb \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, ldrb \reg, [\ptr, \offset])
.endm

.macro strb1_nuao_reg reg, ptr, offset=0
@@ -91,8 +82,7 @@
.endm

.macro ldr1_nuao reg, ptr, offset=0
- 8888: ldr \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, ldr \reg, [\ptr, \offset])
.endm

.macro str1_nuao reg, ptr, offset=0
@@ -100,8 +90,7 @@
.endm

.macro ldp1_nuao regA, regB, ptr, offset=0
- 8888: ldp \regA, \regB, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, ldp \regA, \regB, [\ptr, \offset])
.endm

.macro stp1_nuao regA, regB, ptr, offset=0
@@ -109,8 +98,7 @@
.endm

.macro ldp1_pre_nuao regA, regB, ptr, offset
- 8888: ldp \regA, \regB, [\ptr, \offset]!
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, ldp \regA, \regB, [\ptr, \offset]!)
.endm

.macro stp1_pre_nuao regA, regB, ptr, offset
diff --git a/arch/arm64/lib/copy_in_user.S b/arch/arm64/lib/copy_in_user.S
index f08d4b36a857..bdf9bfecf31f 100644
--- a/arch/arm64/lib/copy_in_user.S
+++ b/arch/arm64/lib/copy_in_user.S
@@ -23,117 +23,93 @@
*/

.macro ldrb1 reg, ptr, offset=0
- 8888: ldtrb \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, ldtrb \reg, [\ptr, \offset])
.endm

.macro strb1 reg, ptr, offset=0
- 8888: sttrb \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, sttrb \reg, [\ptr, \offset])
.endm

.macro ldrb1_reg reg, ptr, offset
add \ptr, \ptr, \offset
- 8888: ldtrb \reg, [\ptr]
+ USER_F(9998f, ldtrb \reg, [\ptr])
sub \ptr, \ptr, \offset
- _asm_extable_faultaddr 8888b,9998f;
.endm

.macro strb1_reg reg, ptr, offset
add \ptr, \ptr, \offset
- 8888: sttrb \reg, [\ptr]
+ USER_F(9998f, sttrb \reg, [\ptr])
sub \ptr, \ptr, \offset
- _asm_extable_faultaddr 8888b,9998f;
.endm

.macro ldr1 reg, ptr, offset=0
- 8888: ldtr \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, ldtr \reg, [\ptr, \offset])
.endm

.macro str1 reg, ptr, offset=0
- 8888: sttr \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, sttr \reg, [\ptr, \offset])
.endm

.macro ldp1 regA, regB, ptr, offset=0
- 8888: ldtr \regA, [\ptr, \offset]
- 8889: ldtr \regB, [\ptr, \offset + 8]
- _asm_extable_faultaddr 8888b,9998f;
- _asm_extable_faultaddr 8889b,9998f;
+ USER_F(9998f, ldtr \regA, [\ptr, \offset])
+ USER_F(9998f, ldtr \regB, [\ptr, \offset + 8])
.endm

.macro stp1 regA, regB, ptr, offset=0
- 8888: sttr \regA, [\ptr, \offset]
- 8889: sttr \regB, [\ptr, \offset + 8]
- _asm_extable_faultaddr 8888b,9998f;
- _asm_extable_faultaddr 8889b,9998f;
+ USER_F(9998f, sttr \regA, [\ptr, \offset])
+ USER_F(9998f, sttr \regB, [\ptr, \offset + 8])
.endm

.macro ldp1_pre regA, regB, ptr, offset
- 8888: ldtr \regA, [\ptr, \offset]
- 8889: ldtr \regB, [\ptr, \offset + 8]
+ USER_F(9998f, ldtr \regA, [\ptr, \offset])
+ USER_F(9998f, ldtr \regB, [\ptr, \offset + 8])
add \ptr, \ptr, \offset
- _asm_extable_faultaddr 8888b,9998f;
- _asm_extable_faultaddr 8889b,9998f;
.endm

.macro stp1_pre regA, regB, ptr, offset
- 8888: sttr \regA, [\ptr, \offset]
- 8889: sttr \regB, [\ptr, \offset + 8]
+ USER_F(9998f, sttr \regA, [\ptr, \offset])
+ USER_F(9998f, sttr \regB, [\ptr, \offset + 8])
add \ptr, \ptr, \offset
- _asm_extable_faultaddr 8888b,9998f;
- _asm_extable_faultaddr 8889b,9998f;
.endm

.macro ldrb1_nuao reg, ptr, offset=0
- 8888: ldrb \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, ldrb \reg, [\ptr, \offset])
.endm

.macro strb1_nuao reg, ptr, offset=0
- 8888: strb \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, strb \reg, [\ptr, \offset])
.endm

.macro ldrb1_nuao_reg reg, ptr, offset=0
- 8888: ldrb \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, ldrb \reg, [\ptr, \offset])
.endm

.macro strb1_nuao_reg reg, ptr, offset=0
- 8888: strb \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, strb \reg, [\ptr, \offset])
.endm

.macro ldr1_nuao reg, ptr, offset=0
- 8888: ldr \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, ldr \reg, [\ptr, \offset])
.endm

.macro str1_nuao reg, ptr, offset=0
- 8888: str \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, str \reg, [\ptr, \offset])
.endm

.macro ldp1_nuao regA, regB, ptr, offset=0
- 8888: ldp \regA, \regB, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, ldp \regA, \regB, [\ptr, \offset])
.endm

.macro stp1_nuao regA, regB, ptr, offset=0
- 8888: stp \regA, \regB, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, stp \regA, \regB, [\ptr, \offset])
.endm

.macro ldp1_pre_nuao regA, regB, ptr, offset
- 8888: ldp \regA, \regB, [\ptr, \offset]!
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, ldp \regA, \regB, [\ptr, \offset]!)
.endm

.macro stp1_pre_nuao regA, regB, ptr, offset
- 8888: stp \regA, \regB, [\ptr, \offset]!
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, stp \regA, \regB, [\ptr, \offset]!)
.endm

.macro copy_exit
diff --git a/arch/arm64/lib/copy_to_user.S b/arch/arm64/lib/copy_to_user.S
index e4629c83abb4..b936bc10594e 100644
--- a/arch/arm64/lib/copy_to_user.S
+++ b/arch/arm64/lib/copy_to_user.S
@@ -25,8 +25,7 @@
.endm

.macro strb1 reg, ptr, offset=0
- 8888: sttrb \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, sttrb \reg, [\ptr, \offset])
.endm

.macro ldrb1_reg reg, ptr, offset
@@ -35,9 +34,8 @@

.macro strb1_reg reg, ptr, offset
add \ptr, \ptr, \offset
- 8888: sttrb \reg, [\ptr]
+ USER_F(9998f, sttrb \reg, [\ptr])
sub \ptr, \ptr, \offset
- _asm_extable_faultaddr 8888b,9998f;
.endm

.macro ldr1 reg, ptr, offset=0
@@ -45,8 +43,7 @@
.endm

.macro str1 reg, ptr, offset=0
- 8888: sttr \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, sttr \reg, [\ptr, \offset])
.endm

.macro ldp1 regA, regB, ptr, offset=0
@@ -54,10 +51,8 @@
.endm

.macro stp1 regA, regB, ptr, offset=0
- 8888: sttr \regA, [\ptr, \offset]
- 8889: sttr \regB, [\ptr, \offset + 8]
- _asm_extable_faultaddr 8888b,9998f;
- _asm_extable_faultaddr 8889b,9998f;
+ USER_F(9998f, sttr \regA, [\ptr, \offset])
+ USER_F(9998f, sttr \regB, [\ptr, \offset + 8])
.endm

.macro ldp1_pre regA, regB, ptr, offset
@@ -65,11 +60,9 @@
.endm

.macro stp1_pre regA, regB, ptr, offset
- 8888: sttr \regA, [\ptr, \offset]
- 8889: sttr \regB, [\ptr, \offset + 8]
+ USER_F(9998f, sttr \regA, [\ptr, \offset])
+ USER_F(9998f, sttr \regB, [\ptr, \offset + 8])
add \ptr, \ptr, \offset
- _asm_extable_faultaddr 8888b,9998f;
- _asm_extable_faultaddr 8889b,9998f;
.endm

.macro ldrb1_nuao reg, ptr, offset=0
@@ -77,8 +70,7 @@
.endm

.macro strb1_nuao reg, ptr, offset=0
- 8888: strb \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, strb \reg, [\ptr, \offset])
.endm

.macro ldrb1_nuao_reg reg, ptr, offset=0
@@ -94,8 +86,7 @@
.endm

.macro str1_nuao reg, ptr, offset=0
- 8888: str \reg, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, str \reg, [\ptr, \offset])
.endm

.macro ldp1_nuao regA, regB, ptr, offset=0
@@ -107,13 +98,11 @@
.endm

.macro stp1_nuao regA, regB, ptr, offset=0
- 8888: stp \regA, \regB, [\ptr, \offset]
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, stp \regA, \regB, [\ptr, \offset])
.endm

.macro stp1_pre_nuao regA, regB, ptr, offset
- 8888: stp \regA, \regB, [\ptr, \offset]!
- _asm_extable_faultaddr 8888b,9998f;
+ USER_F(9998f, stp \regA, \regB, [\ptr, \offset]!)
.endm

.macro copy_exit
--
2.17.1

2020-05-14 14:37:54

by Oliver Swede

[permalink] [raw]
Subject: [PATCH v3 10/13] arm64: Store the arguments to copy_*_user on the stack

This preserves the initial arguments of the user copy calls so that
they can be restored by the fixup routines.

The values in the relevant three registers (x0/dstin, x1/src,
x2/count) may be modified by the optimized memcpy algorithm for large
copy sizes, and this stores them before it begins executing.

The stack is used instead of other general-purpose registers due to
resource constraints: the algorithm is optimized with respect to the
Procedure Call Standard in the Arm ABI, which assumes that x0-x17
can be used as scratch registers and utilizes all of them during
copying, but leaves alone the rest that have specific uses in the
broader system. As there are no more temporary registers, the stack
can be used to preserve the initial arguments to provide fixup
routines with more information to use in the calculation of the
number of bytes that failed to copy.

The stack pointer is restored to its initial position, either from
the fixup code in the case of a fault, or at the end of the copy
algorithm otherwise (uaccess_finish is extended to restore the sp,
and this code is also moved to copy_template_user.S as it is common
to all of the copy routines that access userspace memory).

Signed-off-by: Oliver Swede <[email protected]>
---
arch/arm64/lib/copy_from_user.S | 3 ---
arch/arm64/lib/copy_in_user.S | 3 ---
arch/arm64/lib/copy_template_user.S | 6 ++++++
arch/arm64/lib/copy_to_user.S | 3 ---
arch/arm64/lib/copy_user_fixup.S | 1 +
5 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/arch/arm64/lib/copy_from_user.S b/arch/arm64/lib/copy_from_user.S
index 9c3805725bea..45009fb07081 100644
--- a/arch/arm64/lib/copy_from_user.S
+++ b/arch/arm64/lib/copy_from_user.S
@@ -111,9 +111,6 @@

SYM_FUNC_START(__arch_copy_from_user)
#include "copy_template_user.S"
-.Luaccess_finish:
- mov x0, #0
- ret
SYM_FUNC_END(__arch_copy_from_user)
EXPORT_SYMBOL(__arch_copy_from_user)
#include "copy_user_fixup.S"
diff --git a/arch/arm64/lib/copy_in_user.S b/arch/arm64/lib/copy_in_user.S
index bdf9bfecf31f..c1647a9b3a22 100644
--- a/arch/arm64/lib/copy_in_user.S
+++ b/arch/arm64/lib/copy_in_user.S
@@ -118,9 +118,6 @@

SYM_FUNC_START(__arch_copy_in_user)
#include "copy_template_user.S"
-.Luaccess_finish:
- mov x0, #0
- ret
SYM_FUNC_END(__arch_copy_in_user)
EXPORT_SYMBOL(__arch_copy_in_user)
#include "copy_user_fixup.S"
diff --git a/arch/arm64/lib/copy_template_user.S b/arch/arm64/lib/copy_template_user.S
index 3db24dcdab05..1d13daf314b0 100644
--- a/arch/arm64/lib/copy_template_user.S
+++ b/arch/arm64/lib/copy_template_user.S
@@ -21,4 +21,10 @@
L(copy_non_uao):
#undef L
#define L(l) .Lnuao ## l
+ str x2, [sp, #-16]! // count
+ stp x0, x1, [sp, #-16]! // dstin, src
#include "copy_template.S"
+.Luaccess_finish:
+ add sp, sp, 32
+ mov x0, #0
+ ret
diff --git a/arch/arm64/lib/copy_to_user.S b/arch/arm64/lib/copy_to_user.S
index b936bc10594e..ac10d2d32b03 100644
--- a/arch/arm64/lib/copy_to_user.S
+++ b/arch/arm64/lib/copy_to_user.S
@@ -111,9 +111,6 @@

SYM_FUNC_START(__arch_copy_to_user)
#include "copy_template_user.S"
-.Luaccess_finish:
- mov x0, #0
- ret
SYM_FUNC_END(__arch_copy_to_user)
EXPORT_SYMBOL(__arch_copy_to_user)
#include "copy_user_fixup.S"
diff --git a/arch/arm64/lib/copy_user_fixup.S b/arch/arm64/lib/copy_user_fixup.S
index 117c37598691..fe9f5ac19605 100644
--- a/arch/arm64/lib/copy_user_fixup.S
+++ b/arch/arm64/lib/copy_user_fixup.S
@@ -5,5 +5,6 @@ addr .req x15
.align 2
9998:
// TODO: add accurate fixup
+ add sp, sp, 32
ret

--
2.17.1