1 From 738acf415f0e55f7ec8a2ff25eaefcff08e5d6aa Mon Sep 17 00:00:00 2001
2 From: Phil Elwell <phil@raspberrypi.org>
3 Date: Mon, 13 Oct 2014 11:47:53 +0100
4 Subject: [PATCH 070/222] Improve __copy_to_user and __copy_from_user
7 Provide a __copy_from_user that uses memcpy. On BCM2708, use
8 optimised memcpy/memmove/memcmp/memset implementations.
10 arch/arm/include/asm/string.h | 5 +
11 arch/arm/include/asm/uaccess.h | 1 +
12 arch/arm/lib/Makefile | 15 +-
13 arch/arm/lib/arm-mem.h | 159 ++++++++++++
14 arch/arm/lib/copy_from_user.S | 4 +-
15 arch/arm/lib/exports_rpi.c | 37 +++
16 arch/arm/lib/memcmp_rpi.S | 285 +++++++++++++++++++++
17 arch/arm/lib/memcpy_rpi.S | 59 +++++
18 arch/arm/lib/memcpymove.h | 506 +++++++++++++++++++++++++++++++++++++
19 arch/arm/lib/memmove_rpi.S | 61 +++++
20 arch/arm/lib/memset_rpi.S | 121 +++++++++
21 arch/arm/lib/uaccess_with_memcpy.c | 112 +++++++-
22 12 files changed, 1359 insertions(+), 6 deletions(-)
23 create mode 100644 arch/arm/lib/arm-mem.h
24 create mode 100644 arch/arm/lib/exports_rpi.c
25 create mode 100644 arch/arm/lib/memcmp_rpi.S
26 create mode 100644 arch/arm/lib/memcpy_rpi.S
27 create mode 100644 arch/arm/lib/memcpymove.h
28 create mode 100644 arch/arm/lib/memmove_rpi.S
29 create mode 100644 arch/arm/lib/memset_rpi.S
31 --- a/arch/arm/include/asm/string.h
32 +++ b/arch/arm/include/asm/string.h
33 @@ -24,6 +24,11 @@ extern void * memchr(const void *, int,
34 #define __HAVE_ARCH_MEMSET
35 extern void * memset(void *, int, __kernel_size_t);
37 +#ifdef CONFIG_MACH_BCM2708
38 +#define __HAVE_ARCH_MEMCMP
39 +extern int memcmp(const void *, const void *, size_t);
42 extern void __memzero(void *ptr, __kernel_size_t n);
44 #define memset(p,v,n) \
45 --- a/arch/arm/include/asm/uaccess.h
46 +++ b/arch/arm/include/asm/uaccess.h
47 @@ -475,6 +475,7 @@ do { \
50 extern unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n);
51 +extern unsigned long __must_check __copy_from_user_std(void *to, const void __user *from, unsigned long n);
52 extern unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n);
53 extern unsigned long __must_check __copy_to_user_std(void __user *to, const void *from, unsigned long n);
54 extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
55 --- a/arch/arm/lib/Makefile
56 +++ b/arch/arm/lib/Makefile
59 lib-y := backtrace.o changebit.o csumipv6.o csumpartial.o \
60 csumpartialcopy.o csumpartialcopyuser.o clearbit.o \
61 - delay.o delay-loop.o findbit.o memchr.o memcpy.o \
62 - memmove.o memset.o memzero.o setbit.o \
63 - strchr.o strrchr.o \
64 + delay.o delay-loop.o findbit.o memchr.o memzero.o \
65 + setbit.o strchr.o strrchr.o \
66 testchangebit.o testclearbit.o testsetbit.o \
67 ashldi3.o ashrdi3.o lshrdi3.o muldi3.o \
68 ucmpdi2.o lib1funcs.o div64.o \
69 @@ -18,6 +17,16 @@ lib-y := backtrace.o changebit.o csumip
70 mmu-y := clear_user.o copy_page.o getuser.o putuser.o \
71 copy_from_user.o copy_to_user.o
73 +# Choose optimised implementations for Raspberry Pi
74 +ifeq ($(CONFIG_MACH_BCM2708),y)
75 + CFLAGS_uaccess_with_memcpy.o += -DCOPY_FROM_USER_THRESHOLD=1600
76 + CFLAGS_uaccess_with_memcpy.o += -DCOPY_TO_USER_THRESHOLD=672
77 + obj-$(CONFIG_MODULES) += exports_rpi.o
78 + lib-y += memcpy_rpi.o memmove_rpi.o memset_rpi.o memcmp_rpi.o
80 + lib-y += memcpy.o memmove.o memset.o
83 # using lib_ here won't override already available weak symbols
84 obj-$(CONFIG_UACCESS_WITH_MEMCPY) += uaccess_with_memcpy.o
87 +++ b/arch/arm/lib/arm-mem.h
90 +Copyright (c) 2013, Raspberry Pi Foundation
91 +Copyright (c) 2013, RISC OS Open Ltd
94 +Redistribution and use in source and binary forms, with or without
95 +modification, are permitted provided that the following conditions are met:
96 + * Redistributions of source code must retain the above copyright
97 + notice, this list of conditions and the following disclaimer.
98 + * Redistributions in binary form must reproduce the above copyright
99 + notice, this list of conditions and the following disclaimer in the
100 + documentation and/or other materials provided with the distribution.
101 + * Neither the name of the copyright holder nor the
102 + names of its contributors may be used to endorse or promote products
103 + derived from this software without specific prior written permission.
105 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
106 +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
107 +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
108 +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
109 +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
110 +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
111 +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
112 +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
113 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
114 +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
123 +.macro preload_leading_step1 backwards, ptr, base
124 +/* If the destination is already 16-byte aligned, then we need to preload
125 + * between 0 and prefetch_distance (inclusive) cache lines ahead so there
126 + * are no gaps when the inner loop starts.
135 + .rept prefetch_distance+1
138 + .set OFFSET, OFFSET-32
140 + .set OFFSET, OFFSET+32
145 +.macro preload_leading_step2 backwards, ptr, base, leading_bytes, tmp
146 +/* However, if the destination is not 16-byte aligned, we may need to
147 + * preload one more cache line than that. The question we need to ask is:
148 + * are the leading bytes more than the amount by which the source
149 + * pointer will be rounded down for preloading, and if so, by how many
153 +/* Here we compare against how many bytes we are into the
154 + * cache line, counting down from the highest such address.
155 + * Effectively, we want to calculate
156 + * leading_bytes = dst&15
157 + * cacheline_offset = 31-((src-leading_bytes-1)&31)
158 + * extra_needed = leading_bytes - cacheline_offset
159 + * and test if extra_needed is <= 0, or rearranging:
160 + * leading_bytes + (src-leading_bytes-1)&31 <= 31
162 + mov tmp, base, lsl #32-5
163 + sbc tmp, tmp, leading_bytes, lsl #32-5
164 + adds tmp, tmp, leading_bytes, lsl #32-5
166 + pld [ptr, #-32*(prefetch_distance+1)]
168 +/* Effectively, we want to calculate
169 + * leading_bytes = (-dst)&15
170 + * cacheline_offset = (src+leading_bytes)&31
171 + * extra_needed = leading_bytes - cacheline_offset
172 + * and test if extra_needed is <= 0.
174 + mov tmp, base, lsl #32-5
175 + add tmp, tmp, leading_bytes, lsl #32-5
176 + rsbs tmp, tmp, leading_bytes, lsl #32-5
178 + pld [ptr, #32*(prefetch_distance+1)]
183 +.macro preload_trailing backwards, base, remain, tmp
184 + /* We need either 0, 1 or 2 extra preloads */
187 + mov tmp, tmp, lsl #32-5
189 + mov tmp, base, lsl #32-5
191 + adds tmp, tmp, remain, lsl #32-5
192 + adceqs tmp, tmp, #0
193 + /* The instruction above has two effects: ensures Z is only
194 + * set if C was clear (so Z indicates that both shifted quantities
195 + * were 0), and clears C if Z was set (so C indicates that the sum
196 + * of the shifted quantities was greater and not equal to 32) */
206 + pld [tmp, #-32*(prefetch_distance+1)]
208 + pld [tmp, #-32*prefetch_distance]
210 + pld [tmp, #32*(prefetch_distance+2)]
212 + pld [tmp, #32*(prefetch_distance+1)]
217 +.macro preload_all backwards, narrow_case, shift, base, remain, tmp0, tmp1
220 + bic tmp0, tmp0, #31
222 + sub tmp1, base, remain, lsl #shift
224 + bic tmp0, base, #31
226 + add tmp1, base, remain, lsl #shift
229 + bic tmp1, tmp1, #31
233 + /* In this case, all the data fits in either 1 or 2 cache lines */
238 + sub tmp0, tmp0, #32
240 + add tmp0, tmp0, #32
248 --- a/arch/arm/lib/copy_from_user.S
249 +++ b/arch/arm/lib/copy_from_user.S
254 -ENTRY(__copy_from_user)
255 +ENTRY(__copy_from_user_std)
256 +WEAK(__copy_from_user)
258 #include "copy_template.S"
260 ENDPROC(__copy_from_user)
261 +ENDPROC(__copy_from_user_std)
263 .pushsection .fixup,"ax"
266 +++ b/arch/arm/lib/exports_rpi.c
269 + * Copyright (c) 2014, Raspberry Pi (Trading) Ltd.
271 + * Redistribution and use in source and binary forms, with or without
272 + * modification, are permitted provided that the following conditions
274 + * 1. Redistributions of source code must retain the above copyright
275 + * notice, this list of conditions, and the following disclaimer,
276 + * without modification.
277 + * 2. Redistributions in binary form must reproduce the above copyright
278 + * notice, this list of conditions and the following disclaimer in the
279 + * documentation and/or other materials provided with the distribution.
280 + * 3. The names of the above-listed copyright holders may not be used
281 + * to endorse or promote products derived from this software without
282 + * specific prior written permission.
284 + * ALTERNATIVELY, this software may be distributed under the terms of the
285 + * GNU General Public License ("GPL") version 2, as published by the Free
286 + * Software Foundation.
288 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
289 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
290 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
292 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
293 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
294 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
295 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
296 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
297 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
298 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
301 +#include <linux/kernel.h>
302 +#include <linux/module.h>
304 +EXPORT_SYMBOL(memcmp);
306 +++ b/arch/arm/lib/memcmp_rpi.S
309 +Copyright (c) 2013, Raspberry Pi Foundation
310 +Copyright (c) 2013, RISC OS Open Ltd
311 +All rights reserved.
313 +Redistribution and use in source and binary forms, with or without
314 +modification, are permitted provided that the following conditions are met:
315 + * Redistributions of source code must retain the above copyright
316 + notice, this list of conditions and the following disclaimer.
317 + * Redistributions in binary form must reproduce the above copyright
318 + notice, this list of conditions and the following disclaimer in the
319 + documentation and/or other materials provided with the distribution.
320 + * Neither the name of the copyright holder nor the
321 + names of its contributors may be used to endorse or promote products
322 + derived from this software without specific prior written permission.
324 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
325 +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
326 +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
327 +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
328 +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
329 +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
330 +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
331 +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
332 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
333 +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
336 +#include <linux/linkage.h>
337 +#include "arm-mem.h"
339 +/* Prevent the stack from becoming executable */
340 +#if defined(__linux__) && defined(__ELF__)
341 +.section .note.GNU-stack,"",%progbits
351 +.macro memcmp_process_head unaligned
353 + ldr DAT0, [S_1], #4
354 + ldr DAT1, [S_1], #4
355 + ldr DAT2, [S_1], #4
356 + ldr DAT3, [S_1], #4
358 + ldmia S_1!, {DAT0, DAT1, DAT2, DAT3}
360 + ldmia S_2!, {DAT4, DAT5, DAT6, DAT7}
363 +.macro memcmp_process_tail
371 +.macro memcmp_leading_31bytes
372 + movs DAT0, OFF, lsl #31
373 + ldrmib DAT0, [S_1], #1
374 + ldrcsh DAT1, [S_1], #2
375 + ldrmib DAT4, [S_2], #1
376 + ldrcsh DAT5, [S_2], #2
386 + movs DAT0, OFF, lsl #29
387 + ldrmi DAT0, [S_1], #4
388 + ldrcs DAT1, [S_1], #4
389 + ldrcs DAT2, [S_1], #4
390 + ldrmi DAT4, [S_2], #4
391 + ldmcsia S_2!, {DAT5, DAT6}
406 + memcmp_process_head 1
408 + memcmp_process_tail
412 +.macro memcmp_trailing_15bytes unaligned
415 + ldrcs DAT0, [S_1], #4
416 + ldrcs DAT1, [S_1], #4
418 + ldmcsia S_1!, {DAT0, DAT1}
420 + ldrmi DAT2, [S_1], #4
421 + ldmcsia S_2!, {DAT4, DAT5}
422 + ldrmi DAT6, [S_2], #4
434 + ldrcsh DAT0, [S_1], #2
436 + ldrcsh DAT4, [S_2], #2
447 +.macro memcmp_long_inner_loop unaligned
449 + memcmp_process_head unaligned
450 + pld [S_2, #prefetch_distance*32 + 16]
451 + memcmp_process_tail
452 + memcmp_process_head unaligned
454 + memcmp_process_tail
457 + /* Just before the final (prefetch_distance+1) 32-byte blocks,
458 + * deal with final preloads */
459 + preload_trailing 0, S_1, N, DAT0
460 + preload_trailing 0, S_2, N, DAT0
461 + add N, N, #(prefetch_distance+2)*32 - 16
463 + memcmp_process_head unaligned
464 + memcmp_process_tail
467 + /* Trailing words and bytes */
470 + memcmp_trailing_15bytes unaligned
471 +199: /* Reached end without detecting a difference */
474 + pop {DAT1-DAT6, pc}
477 +.macro memcmp_short_inner_loop unaligned
478 + subs N, N, #16 /* simplifies inner loop termination */
481 + memcmp_process_head unaligned
482 + memcmp_process_tail
485 +122: /* Trailing words and bytes */
488 + memcmp_trailing_15bytes unaligned
489 +199: /* Reached end without detecting a difference */
492 + pop {DAT1-DAT6, pc}
496 + * int memcmp(const void *s1, const void *s2, size_t n);
498 + * a1 = pointer to buffer 1
499 + * a2 = pointer to buffer 2
500 + * a3 = number of bytes to compare (as unsigned chars)
502 + * a1 = >0/=0/<0 if s1 >/=/< s2
505 +.set prefetch_distance, 2
521 + push {DAT1-DAT6, lr}
522 + setend be /* lowest-addressed bytes are most significant */
524 + /* To preload ahead as we go, we need at least (prefetch_distance+2) 32-byte blocks */
525 + cmp N, #(prefetch_distance+3)*32 - 1
529 + /* Adjust N so that the decrement instruction can also test for
530 + * inner loop termination. We want it to stop when there are
531 + * (prefetch_distance+1) complete blocks to go. */
532 + sub N, N, #(prefetch_distance+2)*32
533 + preload_leading_step1 0, DAT0, S_1
534 + preload_leading_step1 0, DAT1, S_2
537 + rsb OFF, S_2, #0 /* no need to AND with 15 here */
538 + preload_leading_step2 0, DAT0, S_1, OFF, DAT2
539 + preload_leading_step2 0, DAT1, S_2, OFF, DAT2
540 + memcmp_leading_31bytes
541 +154: /* Second source now cacheline (32-byte) aligned; we have at
542 + * least one prefetch to go. */
543 + /* Prefetch offset is best selected such that it lies in the
544 + * first 8 of each 32 bytes - but it's just as easy to aim for
547 + rsb OFF, OFF, #32*prefetch_distance
550 + memcmp_long_inner_loop 0
551 +140: memcmp_long_inner_loop 1
553 +170: /* Short case */
556 + preload_all 0, 0, 0, S_1, N, DAT0, DAT1
557 + preload_all 0, 0, 0, S_2, N, DAT0, DAT1
562 + ldrb DAT0, [S_1], #1
563 + ldrb DAT4, [S_2], #1
568 +174: /* Second source now 4-byte aligned; we have 0 or more bytes to go */
571 + memcmp_short_inner_loop 0
572 +140: memcmp_short_inner_loop 1
574 +200: /* Difference found: determine sign. */
578 + pop {DAT1-DAT6, pc}
594 +++ b/arch/arm/lib/memcpy_rpi.S
597 +Copyright (c) 2013, Raspberry Pi Foundation
598 +Copyright (c) 2013, RISC OS Open Ltd
599 +All rights reserved.
601 +Redistribution and use in source and binary forms, with or without
602 +modification, are permitted provided that the following conditions are met:
603 + * Redistributions of source code must retain the above copyright
604 + notice, this list of conditions and the following disclaimer.
605 + * Redistributions in binary form must reproduce the above copyright
606 + notice, this list of conditions and the following disclaimer in the
607 + documentation and/or other materials provided with the distribution.
608 + * Neither the name of the copyright holder nor the
609 + names of its contributors may be used to endorse or promote products
610 + derived from this software without specific prior written permission.
612 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
613 +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
614 +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
615 +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
616 +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
617 +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
618 +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
619 +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
620 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
621 +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
624 +#include <linux/linkage.h>
625 +#include "arm-mem.h"
626 +#include "memcpymove.h"
628 +/* Prevent the stack from becoming executable */
629 +#if defined(__linux__) && defined(__ELF__)
630 +.section .note.GNU-stack,"",%progbits
641 + * void *memcpy(void * restrict s1, const void * restrict s2, size_t n);
643 + * a1 = pointer to destination
644 + * a2 = pointer to source
645 + * a3 = number of bytes to copy
650 +.set prefetch_distance, 3
656 +++ b/arch/arm/lib/memcpymove.h
659 +Copyright (c) 2013, Raspberry Pi Foundation
660 +Copyright (c) 2013, RISC OS Open Ltd
661 +All rights reserved.
663 +Redistribution and use in source and binary forms, with or without
664 +modification, are permitted provided that the following conditions are met:
665 + * Redistributions of source code must retain the above copyright
666 + notice, this list of conditions and the following disclaimer.
667 + * Redistributions in binary form must reproduce the above copyright
668 + notice, this list of conditions and the following disclaimer in the
669 + documentation and/or other materials provided with the distribution.
670 + * Neither the name of the copyright holder nor the
671 + names of its contributors may be used to endorse or promote products
672 + derived from this software without specific prior written permission.
674 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
675 +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
676 +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
677 +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
678 +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
679 +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
680 +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
681 +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
682 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
683 +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
686 +.macro unaligned_words backwards, align, use_pld, words, r0, r1, r2, r3, r4, r5, r6, r7, r8
689 + mov r1, r0, lsl #32-align*8
691 + orr r1, r1, r0, lsr #align*8
694 + mov r0, r1, lsr #align*8
696 + orr r0, r0, r1, lsl #32-align*8
702 + mov r2, r0, lsl #32-align*8
704 + orr r2, r2, r1, lsr #align*8
705 + mov r1, r1, lsl #32-align*8
706 + orr r1, r1, r0, lsr #align*8
710 + mov r0, r2, lsr #align*8
712 + orr r0, r0, r1, lsl #32-align*8
713 + mov r1, r1, lsr #align*8
714 + orr r1, r1, r2, lsl #32-align*8
720 + mov r4, r0, lsl #32-align*8
722 + orr r4, r4, r3, lsr #align*8
723 + mov r3, r3, lsl #32-align*8
724 + orr r3, r3, r2, lsr #align*8
725 + mov r2, r2, lsl #32-align*8
726 + orr r2, r2, r1, lsr #align*8
727 + mov r1, r1, lsl #32-align*8
728 + orr r1, r1, r0, lsr #align*8
729 + stmdb D!, {r1, r2, r3, r4}
732 + mov r0, r4, lsr #align*8
734 + orr r0, r0, r1, lsl #32-align*8
735 + mov r1, r1, lsr #align*8
736 + orr r1, r1, r2, lsl #32-align*8
737 + mov r2, r2, lsr #align*8
738 + orr r2, r2, r3, lsl #32-align*8
739 + mov r3, r3, lsr #align*8
740 + orr r3, r3, r4, lsl #32-align*8
741 + stmia D!, {r0, r1, r2, r3}
745 + ldmdb S!, {r4, r5, r6, r7}
746 + mov r8, r0, lsl #32-align*8
747 + ldmdb S!, {r0, r1, r2, r3}
751 + orr r8, r8, r7, lsr #align*8
752 + mov r7, r7, lsl #32-align*8
753 + orr r7, r7, r6, lsr #align*8
754 + mov r6, r6, lsl #32-align*8
755 + orr r6, r6, r5, lsr #align*8
756 + mov r5, r5, lsl #32-align*8
757 + orr r5, r5, r4, lsr #align*8
758 + mov r4, r4, lsl #32-align*8
759 + orr r4, r4, r3, lsr #align*8
760 + mov r3, r3, lsl #32-align*8
761 + orr r3, r3, r2, lsr #align*8
762 + mov r2, r2, lsl #32-align*8
763 + orr r2, r2, r1, lsr #align*8
764 + mov r1, r1, lsl #32-align*8
765 + orr r1, r1, r0, lsr #align*8
766 + stmdb D!, {r5, r6, r7, r8}
767 + stmdb D!, {r1, r2, r3, r4}
769 + ldmib S!, {r1, r2, r3, r4}
770 + mov r0, r8, lsr #align*8
771 + ldmib S!, {r5, r6, r7, r8}
775 + orr r0, r0, r1, lsl #32-align*8
776 + mov r1, r1, lsr #align*8
777 + orr r1, r1, r2, lsl #32-align*8
778 + mov r2, r2, lsr #align*8
779 + orr r2, r2, r3, lsl #32-align*8
780 + mov r3, r3, lsr #align*8
781 + orr r3, r3, r4, lsl #32-align*8
782 + mov r4, r4, lsr #align*8
783 + orr r4, r4, r5, lsl #32-align*8
784 + mov r5, r5, lsr #align*8
785 + orr r5, r5, r6, lsl #32-align*8
786 + mov r6, r6, lsr #align*8
787 + orr r6, r6, r7, lsl #32-align*8
788 + mov r7, r7, lsr #align*8
789 + orr r7, r7, r8, lsl #32-align*8
790 + stmia D!, {r0, r1, r2, r3}
791 + stmia D!, {r4, r5, r6, r7}
796 +.macro memcpy_leading_15bytes backwards, align
797 + movs DAT1, DAT2, lsl #31
800 + ldrmib DAT0, [S, #-1]!
801 + ldrcsh DAT1, [S, #-2]!
802 + strmib DAT0, [D, #-1]!
803 + strcsh DAT1, [D, #-2]!
805 + ldrmib DAT0, [S], #1
806 + ldrcsh DAT1, [S], #2
807 + strmib DAT0, [D], #1
808 + strcsh DAT1, [D], #2
810 + movs DAT1, DAT2, lsl #29
812 + ldrmi DAT0, [S, #-4]!
814 + ldmcsdb S!, {DAT1, DAT2}
816 + ldrcs DAT2, [S, #-4]!
817 + ldrcs DAT1, [S, #-4]!
819 + strmi DAT0, [D, #-4]!
820 + stmcsdb D!, {DAT1, DAT2}
822 + ldrmi DAT0, [S], #4
824 + ldmcsia S!, {DAT1, DAT2}
826 + ldrcs DAT1, [S], #4
827 + ldrcs DAT2, [S], #4
829 + strmi DAT0, [D], #4
830 + stmcsia D!, {DAT1, DAT2}
834 +.macro memcpy_trailing_15bytes backwards, align
838 + ldmcsdb S!, {DAT0, DAT1}
840 + ldrcs DAT1, [S, #-4]!
841 + ldrcs DAT0, [S, #-4]!
843 + ldrmi DAT2, [S, #-4]!
844 + stmcsdb D!, {DAT0, DAT1}
845 + strmi DAT2, [D, #-4]!
848 + ldmcsia S!, {DAT0, DAT1}
850 + ldrcs DAT0, [S], #4
851 + ldrcs DAT1, [S], #4
853 + ldrmi DAT2, [S], #4
854 + stmcsia D!, {DAT0, DAT1}
855 + strmi DAT2, [D], #4
859 + ldrcsh DAT0, [S, #-2]!
860 + ldrmib DAT1, [S, #-1]
861 + strcsh DAT0, [D, #-2]!
862 + strmib DAT1, [D, #-1]
864 + ldrcsh DAT0, [S], #2
866 + strcsh DAT0, [D], #2
871 +.macro memcpy_long_inner_loop backwards, align
874 + ldr DAT0, [S, #-align]!
876 + ldr LAST, [S, #-align]!
882 + ldmdb S!, {DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, LAST}
884 + stmdb D!, {DAT4, DAT5, DAT6, LAST}
885 + stmdb D!, {DAT0, DAT1, DAT2, DAT3}
887 + ldmia S!, {DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, LAST}
889 + stmia D!, {DAT0, DAT1, DAT2, DAT3}
890 + stmia D!, {DAT4, DAT5, DAT6, LAST}
893 + unaligned_words backwards, align, 1, 8, DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, DAT7, LAST
897 + /* Just before the final (prefetch_distance+1) 32-byte blocks, deal with final preloads */
898 + preload_trailing backwards, S, N, OFF
899 + add N, N, #(prefetch_distance+2)*32 - 32
903 + ldmdb S!, {DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, LAST}
904 + stmdb D!, {DAT4, DAT5, DAT6, LAST}
905 + stmdb D!, {DAT0, DAT1, DAT2, DAT3}
907 + ldmia S!, {DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, LAST}
908 + stmia D!, {DAT0, DAT1, DAT2, DAT3}
909 + stmia D!, {DAT4, DAT5, DAT6, LAST}
912 + unaligned_words backwards, align, 0, 8, DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, DAT7, LAST
919 + ldmnedb S!, {DAT0, DAT1, DAT2, LAST}
920 + stmnedb D!, {DAT0, DAT1, DAT2, LAST}
922 + ldmneia S!, {DAT0, DAT1, DAT2, LAST}
923 + stmneia D!, {DAT0, DAT1, DAT2, LAST}
927 + unaligned_words backwards, align, 0, 4, DAT0, DAT1, DAT2, DAT3, LAST
930 + /* Trailing words and bytes */
936 + memcpy_trailing_15bytes backwards, align
938 + pop {DAT3, DAT4, DAT5, DAT6, DAT7}
939 + pop {D, DAT1, DAT2, pc}
942 +.macro memcpy_medium_inner_loop backwards, align
946 + ldmdb S!, {DAT0, DAT1, DAT2, LAST}
948 + ldr LAST, [S, #-4]!
949 + ldr DAT2, [S, #-4]!
950 + ldr DAT1, [S, #-4]!
951 + ldr DAT0, [S, #-4]!
953 + stmdb D!, {DAT0, DAT1, DAT2, LAST}
956 + ldmia S!, {DAT0, DAT1, DAT2, LAST}
963 + stmia D!, {DAT0, DAT1, DAT2, LAST}
967 + /* Trailing words and bytes */
970 + memcpy_trailing_15bytes backwards, align
972 + pop {D, DAT1, DAT2, pc}
975 +.macro memcpy_short_inner_loop backwards, align
979 + ldmnedb S!, {DAT0, DAT1, DAT2, LAST}
981 + ldrne LAST, [S, #-4]!
982 + ldrne DAT2, [S, #-4]!
983 + ldrne DAT1, [S, #-4]!
984 + ldrne DAT0, [S, #-4]!
986 + stmnedb D!, {DAT0, DAT1, DAT2, LAST}
989 + ldmneia S!, {DAT0, DAT1, DAT2, LAST}
991 + ldrne DAT0, [S], #4
992 + ldrne DAT1, [S], #4
993 + ldrne DAT2, [S], #4
994 + ldrne LAST, [S], #4
996 + stmneia D!, {DAT0, DAT1, DAT2, LAST}
998 + memcpy_trailing_15bytes backwards, align
1000 + pop {D, DAT1, DAT2, pc}
1003 +.macro memcpy backwards
1020 + push {D, DAT1, DAT2, lr}
1022 + .cfi_def_cfa_offset 16
1023 + .cfi_rel_offset D, 0
1026 + .cfi_undefined DAT0
1027 + .cfi_rel_offset DAT1, 4
1028 + .cfi_rel_offset DAT2, 8
1029 + .cfi_undefined LAST
1030 + .cfi_rel_offset lr, 12
1037 + /* See if we're guaranteed to have at least one 16-byte aligned 16-byte write */
1040 + /* To preload ahead as we go, we need at least (prefetch_distance+2) 32-byte blocks */
1041 + cmp N, #(prefetch_distance+3)*32 - 1
1045 + push {DAT3, DAT4, DAT5, DAT6, DAT7}
1047 + .cfi_def_cfa_offset 36
1048 + .cfi_rel_offset D, 20
1049 + .cfi_rel_offset DAT1, 24
1050 + .cfi_rel_offset DAT2, 28
1051 + .cfi_rel_offset DAT3, 0
1052 + .cfi_rel_offset DAT4, 4
1053 + .cfi_rel_offset DAT5, 8
1054 + .cfi_rel_offset DAT6, 12
1055 + .cfi_rel_offset DAT7, 16
1056 + .cfi_rel_offset lr, 32
1058 + /* Adjust N so that the decrement instruction can also test for
1059 + * inner loop termination. We want it to stop when there are
1060 + * (prefetch_distance+1) complete blocks to go. */
1061 + sub N, N, #(prefetch_distance+2)*32
1062 + preload_leading_step1 backwards, DAT0, S
1064 + /* Bug in GAS: it accepts, but mis-assembles the instruction
1065 + * ands DAT2, D, #60, 2
1066 + * which sets DAT2 to the number of leading bytes until destination is aligned and also clears C (sets borrow)
1073 + rsb DAT2, DAT2, #16 /* number of leading bytes until destination aligned */
1075 + preload_leading_step2 backwards, DAT0, S, DAT2, OFF
1076 + memcpy_leading_15bytes backwards, 1
1077 +154: /* Destination now 16-byte aligned; we have at least one prefetch as well as at least one 16-byte output block */
1078 + /* Prefetch offset is best selected such that it lies in the first 8 of each 32 bytes - but it's just as easy to aim for the first one */
1082 + sub OFF, OFF, #32*(prefetch_distance+1)
1085 + rsb OFF, OFF, #32*prefetch_distance
1087 + movs DAT0, S, lsl #31
1091 + memcpy_long_inner_loop backwards, 0
1092 +155: memcpy_long_inner_loop backwards, 1
1093 +156: memcpy_long_inner_loop backwards, 2
1094 +157: memcpy_long_inner_loop backwards, 3
1096 + .cfi_def_cfa_offset 16
1097 + .cfi_rel_offset D, 0
1098 + .cfi_rel_offset DAT1, 4
1099 + .cfi_rel_offset DAT2, 8
1100 + .cfi_same_value DAT3
1101 + .cfi_same_value DAT4
1102 + .cfi_same_value DAT5
1103 + .cfi_same_value DAT6
1104 + .cfi_same_value DAT7
1105 + .cfi_rel_offset lr, 12
1107 +160: /* Medium case */
1108 + preload_all backwards, 0, 0, S, N, DAT2, OFF
1109 + sub N, N, #16 /* simplifies inner loop termination */
1116 + rsb DAT2, DAT2, #16
1118 + memcpy_leading_15bytes backwards, align
1119 +164: /* Destination now 16-byte aligned; we have at least one 16-byte output block */
1122 + memcpy_medium_inner_loop backwards, 0
1123 +140: memcpy_medium_inner_loop backwards, 1
1125 +170: /* Short case, less than 31 bytes, so no guarantee of at least one 16-byte block */
1128 + preload_all backwards, 1, 0, S, N, DAT2, LAST
1134 + ldrb DAT0, [S, #-1]!
1135 + strb DAT0, [D, #-1]!
1137 + ldrb DAT0, [S], #1
1138 + strb DAT0, [D], #1
1142 +174: /* Destination now 4-byte aligned; we have 0 or more output bytes to go */
1145 + memcpy_short_inner_loop backwards, 0
1146 +140: memcpy_short_inner_loop backwards, 1
1165 +++ b/arch/arm/lib/memmove_rpi.S
1168 +Copyright (c) 2013, Raspberry Pi Foundation
1169 +Copyright (c) 2013, RISC OS Open Ltd
1170 +All rights reserved.
1172 +Redistribution and use in source and binary forms, with or without
1173 +modification, are permitted provided that the following conditions are met:
1174 + * Redistributions of source code must retain the above copyright
1175 + notice, this list of conditions and the following disclaimer.
1176 + * Redistributions in binary form must reproduce the above copyright
1177 + notice, this list of conditions and the following disclaimer in the
1178 + documentation and/or other materials provided with the distribution.
1179 + * Neither the name of the copyright holder nor the
1180 + names of its contributors may be used to endorse or promote products
1181 + derived from this software without specific prior written permission.
1183 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1184 +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1185 +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1186 +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
1187 +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1188 +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1189 +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
1190 +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1191 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1192 +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1195 +#include <linux/linkage.h>
1196 +#include "arm-mem.h"
1197 +#include "memcpymove.h"
1199 +/* Prevent the stack from becoming executable */
1200 +#if defined(__linux__) && defined(__ELF__)
1201 +.section .note.GNU-stack,"",%progbits
1206 + .object_arch armv4
1212 + * void *memmove(void *s1, const void *s2, size_t n);
1214 + * a1 = pointer to destination
1215 + * a2 = pointer to source
1216 + * a3 = number of bytes to copy
1221 +.set prefetch_distance, 3
1225 + bpl memcpy /* pl works even over -1 - 0 and 0x7fffffff - 0x80000000 boundaries */
1229 +++ b/arch/arm/lib/memset_rpi.S
1232 +Copyright (c) 2013, Raspberry Pi Foundation
1233 +Copyright (c) 2013, RISC OS Open Ltd
1234 +All rights reserved.
1236 +Redistribution and use in source and binary forms, with or without
1237 +modification, are permitted provided that the following conditions are met:
1238 + * Redistributions of source code must retain the above copyright
1239 + notice, this list of conditions and the following disclaimer.
1240 + * Redistributions in binary form must reproduce the above copyright
1241 + notice, this list of conditions and the following disclaimer in the
1242 + documentation and/or other materials provided with the distribution.
1243 + * Neither the name of the copyright holder nor the
1244 + names of its contributors may be used to endorse or promote products
1245 + derived from this software without specific prior written permission.
1247 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1248 +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1249 +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1250 +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
1251 +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1252 +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1253 +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
1254 +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1255 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1256 +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1259 +#include <linux/linkage.h>
1260 +#include "arm-mem.h"
1262 +/* Prevent the stack from becoming executable */
1263 +#if defined(__linux__) && defined(__ELF__)
1264 +.section .note.GNU-stack,"",%progbits
1269 + .object_arch armv4
1275 + * void *memset(void *s, int c, size_t n);
1277 + * a1 = pointer to buffer to fill
1278 + * a2 = byte pattern to fill with (caller-narrowed)
1279 + * a3 = number of bytes to fill
1291 + orr DAT0, DAT0, lsl #8
1293 + orr DAT0, DAT0, lsl #16
1296 + /* See if we're guaranteed to have at least one 16-byte aligned 16-byte write */
1300 +161: sub N, N, #16 /* simplifies inner loop termination */
1301 + /* Leading words and bytes */
1304 + rsb DAT3, S, #0 /* bits 0-3 = number of leading bytes until aligned */
1305 + movs DAT2, DAT3, lsl #31
1307 + strmib DAT0, [S], #1
1309 + strcsh DAT0, [S], #2
1310 + movs DAT2, DAT3, lsl #29
1312 + strmi DAT0, [S], #4
1314 + stmcsia S!, {DAT0, DAT1}
1315 +164: /* Delayed set up of DAT2 and DAT3 so we could use them as scratch registers above */
1318 + /* Now the inner loop of 16-byte stores */
1319 +165: stmia S!, {DAT0, DAT1, DAT2, DAT3}
1322 +166: /* Trailing words and bytes */
1323 + movs N, N, lsl #29
1324 + stmcsia S!, {DAT0, DAT1}
1325 + strmi DAT0, [S], #4
1327 + strcsh DAT0, [S], #2
1331 +170: /* Short case */
1338 + strb DAT0, [S], #1
1342 + stmneia S!, {DAT0, DAT1, DAT2, DAT3}
1352 --- a/arch/arm/lib/uaccess_with_memcpy.c
1353 +++ b/arch/arm/lib/uaccess_with_memcpy.c
1355 #include <asm/current.h>
1356 #include <asm/page.h>
1358 +#ifndef COPY_FROM_USER_THRESHOLD
1359 +#define COPY_FROM_USER_THRESHOLD 64
1362 +#ifndef COPY_TO_USER_THRESHOLD
1363 +#define COPY_TO_USER_THRESHOLD 64
1367 pin_page_for_write(const void __user *_addr, pte_t **ptep, spinlock_t **ptlp)
1369 @@ -85,7 +93,44 @@ pin_page_for_write(const void __user *_a
1373 -static unsigned long noinline
1375 +pin_page_for_read(const void __user *_addr, pte_t **ptep, spinlock_t **ptlp)
1377 + unsigned long addr = (unsigned long)_addr;
1384 + pgd = pgd_offset(current->mm, addr);
1385 + if (unlikely(pgd_none(*pgd) || pgd_bad(*pgd)))
1389 + pud = pud_offset(pgd, addr);
1390 + if (unlikely(pud_none(*pud) || pud_bad(*pud)))
1395 + pmd = pmd_offset(pud, addr);
1396 + if (unlikely(pmd_none(*pmd) || pmd_bad(*pmd)))
1399 + pte = pte_offset_map_lock(current->mm, pmd, addr, &ptl);
1400 + if (unlikely(!pte_present(*pte) || !pte_young(*pte))) {
1401 + pte_unmap_unlock(pte, ptl);
1411 +unsigned long noinline
1412 __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n)
1415 @@ -135,6 +180,54 @@ out:
1419 +unsigned long noinline
1420 +__copy_from_user_memcpy(void *to, const void __user *from, unsigned long n)
1424 + if (unlikely(segment_eq(get_fs(), KERNEL_DS))) {
1425 + memcpy(to, (const void *)from, n);
1429 + /* the mmap semaphore is taken only if not in an atomic context */
1430 + atomic = in_atomic();
1433 + down_read(¤t->mm->mmap_sem);
1439 + while (!pin_page_for_read(from, &pte, &ptl)) {
1442 + up_read(¤t->mm->mmap_sem);
1443 + if (__get_user(temp, (char __user *)from))
1446 + down_read(¤t->mm->mmap_sem);
1449 + tocopy = (~(unsigned long)from & ~PAGE_MASK) + 1;
1453 + memcpy(to, (const void *)from, tocopy);
1458 + pte_unmap_unlock(pte, ptl);
1461 + up_read(¤t->mm->mmap_sem);
1468 __copy_to_user(void __user *to, const void *from, unsigned long n)
1470 @@ -145,10 +238,25 @@ __copy_to_user(void __user *to, const vo
1471 * With frame pointer disabled, tail call optimization kicks in
1472 * as well making this test almost invisible.
1475 + if (n < COPY_TO_USER_THRESHOLD)
1476 return __copy_to_user_std(to, from, n);
1477 return __copy_to_user_memcpy(to, from, n);
1481 +__copy_from_user(void *to, const void __user *from, unsigned long n)
1484 + * This test is stubbed out of the main function above to keep
1485 + * the overhead for small copies low by avoiding a large
1486 + * register dump on the stack just to reload them right away.
1487 + * With frame pointer disabled, tail call optimization kicks in
1488 + * as well making this test almost invisible.
1490 + if (n < COPY_FROM_USER_THRESHOLD)
1491 + return __copy_from_user_std(to, from, n);
1492 + return __copy_from_user_memcpy(to, from, n);
1495 static unsigned long noinline
1496 __clear_user_memset(void __user *addr, unsigned long n)