2d546c7502add9185976e5afb2d83939ce771628
[openwrt/staging/ldir.git] /
1 From 30f1537d9b4df93cc77f255109794e25f35dc1b4 Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Mon, 28 Nov 2016 16:50:04 +0000
4 Subject: [PATCH 0103/1085] Improve __copy_to_user and __copy_from_user
5 performance
6
7 Provide a __copy_from_user that uses memcpy. On BCM2708, use
8 optimised memcpy/memmove/memcmp/memset implementations.
9
10 arch/arm: Add mmiocpy/set aliases for memcpy/set
11
12 See: https://github.com/raspberrypi/linux/issues/1082
13
14 copy_from_user: CPU_SW_DOMAIN_PAN compatibility
15
16 The downstream copy_from_user acceleration must also play nice with
17 CONFIG_CPU_SW_DOMAIN_PAN.
18
19 See: https://github.com/raspberrypi/linux/issues/1381
20
21 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
22
23 Fix copy_from_user if BCM2835_FAST_MEMCPY=n
24
25 The change which introduced CONFIG_BCM2835_FAST_MEMCPY unconditionally
26 changed the behaviour of arm_copy_from_user. The page pinning code
27 is not safe on ARMv7 if LPAE & high memory is enabled and causes
28 crashes which look like PTE corruption.
29
30 Make __copy_from_user_memcpy conditional on CONFIG_2835_FAST_MEMCPY=y
31 which is really an ARMv6 / Pi1 optimization and not necessary on newer
32 ARM processors.
33
34 arm: fix mmap unlocks in uaccess_with_memcpy.c
35
36 This is a regression that was added with the commit 192a4e923ef092924dd013e7326f2ec520ee4783 as of rpi-5.8.y, since that is when the move to the mmap locking API was introduced - d8ed45c5dcd455fc5848d47f86883a1b872ac0d0
37
38 The issue is that when the patch to improve performance for the __copy_to_user and __copy_from_user functions were added for the Raspberry Pi, some of the mmaps were incorrectly mapped to write instead of read. This would cause a verity of issues, and in my case, prevent the booting of a squashfs filesystem on rpi-5.8-y and above. An example of the panic you would see from this can be seen at https://pastebin.com/raw/jBz5xCzL
39
40 Signed-off-by: Christian Lamparter <chunkeey@gmail.com>
41 Signed-off-by: Christopher Blake <chrisrblake93@gmail.com>
42
43 arch/arm: Add __memset alias to memset_rpi.S
44
45 memset_rpi.S is an optimised memset implementation, but doesn't define
46 __memset (which was just added to memset.S). As a result, building
47 for the BCM2835 platform causes a link failure.
48
49 Add __memset as yet another alias to our common implementation.
50
51 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
52
53 arm: Fix custom rpi __memset32 and __memset64
54
55 See: https://github.com/raspberrypi/linux/issues/4798
56
57 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
58
59 arm: Fix annoying .eh_frame section warnings
60
61 Replace the cfi directives with the UNWIND equivalents. This prevents
62 the .eh_frame section from being created, eliminating the warnings.
63
64 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
65 ---
66 arch/arm/include/asm/string.h | 5 +
67 arch/arm/include/asm/uaccess.h | 3 +
68 arch/arm/lib/Makefile | 14 +-
69 arch/arm/lib/arm-mem.h | 159 ++++++++++
70 arch/arm/lib/copy_from_user.S | 4 +-
71 arch/arm/lib/exports_rpi.c | 37 +++
72 arch/arm/lib/memcmp_rpi.S | 285 +++++++++++++++++
73 arch/arm/lib/memcpy_rpi.S | 65 ++++
74 arch/arm/lib/memcpymove.h | 488 +++++++++++++++++++++++++++++
75 arch/arm/lib/memmove_rpi.S | 63 ++++
76 arch/arm/lib/memset_rpi.S | 132 ++++++++
77 arch/arm/lib/uaccess_with_memcpy.c | 125 +++++++-
78 arch/arm/mach-bcm/Kconfig | 24 ++
79 13 files changed, 1398 insertions(+), 6 deletions(-)
80 create mode 100644 arch/arm/lib/arm-mem.h
81 create mode 100644 arch/arm/lib/exports_rpi.c
82 create mode 100644 arch/arm/lib/memcmp_rpi.S
83 create mode 100644 arch/arm/lib/memcpy_rpi.S
84 create mode 100644 arch/arm/lib/memcpymove.h
85 create mode 100644 arch/arm/lib/memmove_rpi.S
86 create mode 100644 arch/arm/lib/memset_rpi.S
87
88 --- a/arch/arm/include/asm/string.h
89 +++ b/arch/arm/include/asm/string.h
90 @@ -65,4 +65,9 @@ static inline void *memset64(uint64_t *p
91
92 #endif
93
94 +#ifdef CONFIG_BCM2835_FAST_MEMCPY
95 +#define __HAVE_ARCH_MEMCMP
96 +extern int memcmp(const void *, const void *, size_t);
97 +#endif
98 +
99 #endif
100 --- a/arch/arm/include/asm/uaccess.h
101 +++ b/arch/arm/include/asm/uaccess.h
102 @@ -509,6 +509,9 @@ do { \
103 extern unsigned long __must_check
104 arm_copy_from_user(void *to, const void __user *from, unsigned long n);
105
106 +extern unsigned long __must_check
107 +__copy_from_user_std(void *to, const void __user *from, unsigned long n);
108 +
109 static inline unsigned long __must_check
110 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
111 {
112 --- a/arch/arm/lib/Makefile
113 +++ b/arch/arm/lib/Makefile
114 @@ -7,8 +7,8 @@
115
116 lib-y := changebit.o csumipv6.o csumpartial.o \
117 csumpartialcopy.o csumpartialcopyuser.o clearbit.o \
118 - delay.o delay-loop.o findbit.o memchr.o memcpy.o \
119 - memmove.o memset.o setbit.o \
120 + delay.o delay-loop.o findbit.o memchr.o \
121 + setbit.o \
122 strchr.o strrchr.o \
123 testchangebit.o testclearbit.o testsetbit.o \
124 ashldi3.o ashrdi3.o lshrdi3.o muldi3.o \
125 @@ -25,6 +25,16 @@ else
126 lib-y += backtrace.o
127 endif
128
129 +# Choose optimised implementations for Raspberry Pi
130 +ifeq ($(CONFIG_BCM2835_FAST_MEMCPY),y)
131 + CFLAGS_uaccess_with_memcpy.o += -DCOPY_FROM_USER_THRESHOLD=1600
132 + CFLAGS_uaccess_with_memcpy.o += -DCOPY_TO_USER_THRESHOLD=672
133 + obj-$(CONFIG_MODULES) += exports_rpi.o
134 + lib-y += memcpy_rpi.o memmove_rpi.o memset_rpi.o memcmp_rpi.o
135 +else
136 + lib-y += memcpy.o memmove.o memset.o
137 +endif
138 +
139 # using lib_ here won't override already available weak symbols
140 obj-$(CONFIG_UACCESS_WITH_MEMCPY) += uaccess_with_memcpy.o
141
142 --- /dev/null
143 +++ b/arch/arm/lib/arm-mem.h
144 @@ -0,0 +1,159 @@
145 +/*
146 +Copyright (c) 2013, Raspberry Pi Foundation
147 +Copyright (c) 2013, RISC OS Open Ltd
148 +All rights reserved.
149 +
150 +Redistribution and use in source and binary forms, with or without
151 +modification, are permitted provided that the following conditions are met:
152 + * Redistributions of source code must retain the above copyright
153 + notice, this list of conditions and the following disclaimer.
154 + * Redistributions in binary form must reproduce the above copyright
155 + notice, this list of conditions and the following disclaimer in the
156 + documentation and/or other materials provided with the distribution.
157 + * Neither the name of the copyright holder nor the
158 + names of its contributors may be used to endorse or promote products
159 + derived from this software without specific prior written permission.
160 +
161 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
162 +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
163 +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
164 +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
165 +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
166 +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
167 +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
168 +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
169 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
170 +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
171 +*/
172 +
173 +.macro myfunc fname
174 + .func fname
175 + .global fname
176 +fname:
177 +.endm
178 +
179 +.macro preload_leading_step1 backwards, ptr, base
180 +/* If the destination is already 16-byte aligned, then we need to preload
181 + * between 0 and prefetch_distance (inclusive) cache lines ahead so there
182 + * are no gaps when the inner loop starts.
183 + */
184 + .if backwards
185 + sub ptr, base, #1
186 + bic ptr, ptr, #31
187 + .else
188 + bic ptr, base, #31
189 + .endif
190 + .set OFFSET, 0
191 + .rept prefetch_distance+1
192 + pld [ptr, #OFFSET]
193 + .if backwards
194 + .set OFFSET, OFFSET-32
195 + .else
196 + .set OFFSET, OFFSET+32
197 + .endif
198 + .endr
199 +.endm
200 +
201 +.macro preload_leading_step2 backwards, ptr, base, leading_bytes, tmp
202 +/* However, if the destination is not 16-byte aligned, we may need to
203 + * preload one more cache line than that. The question we need to ask is:
204 + * are the leading bytes more than the amount by which the source
205 + * pointer will be rounded down for preloading, and if so, by how many
206 + * cache lines?
207 + */
208 + .if backwards
209 +/* Here we compare against how many bytes we are into the
210 + * cache line, counting down from the highest such address.
211 + * Effectively, we want to calculate
212 + * leading_bytes = dst&15
213 + * cacheline_offset = 31-((src-leading_bytes-1)&31)
214 + * extra_needed = leading_bytes - cacheline_offset
215 + * and test if extra_needed is <= 0, or rearranging:
216 + * leading_bytes + (src-leading_bytes-1)&31 <= 31
217 + */
218 + mov tmp, base, lsl #32-5
219 + sbc tmp, tmp, leading_bytes, lsl #32-5
220 + adds tmp, tmp, leading_bytes, lsl #32-5
221 + bcc 61f
222 + pld [ptr, #-32*(prefetch_distance+1)]
223 + .else
224 +/* Effectively, we want to calculate
225 + * leading_bytes = (-dst)&15
226 + * cacheline_offset = (src+leading_bytes)&31
227 + * extra_needed = leading_bytes - cacheline_offset
228 + * and test if extra_needed is <= 0.
229 + */
230 + mov tmp, base, lsl #32-5
231 + add tmp, tmp, leading_bytes, lsl #32-5
232 + rsbs tmp, tmp, leading_bytes, lsl #32-5
233 + bls 61f
234 + pld [ptr, #32*(prefetch_distance+1)]
235 + .endif
236 +61:
237 +.endm
238 +
239 +.macro preload_trailing backwards, base, remain, tmp
240 + /* We need either 0, 1 or 2 extra preloads */
241 + .if backwards
242 + rsb tmp, base, #0
243 + mov tmp, tmp, lsl #32-5
244 + .else
245 + mov tmp, base, lsl #32-5
246 + .endif
247 + adds tmp, tmp, remain, lsl #32-5
248 + adceqs tmp, tmp, #0
249 + /* The instruction above has two effects: ensures Z is only
250 + * set if C was clear (so Z indicates that both shifted quantities
251 + * were 0), and clears C if Z was set (so C indicates that the sum
252 + * of the shifted quantities was greater and not equal to 32) */
253 + beq 82f
254 + .if backwards
255 + sub tmp, base, #1
256 + bic tmp, tmp, #31
257 + .else
258 + bic tmp, base, #31
259 + .endif
260 + bcc 81f
261 + .if backwards
262 + pld [tmp, #-32*(prefetch_distance+1)]
263 +81:
264 + pld [tmp, #-32*prefetch_distance]
265 + .else
266 + pld [tmp, #32*(prefetch_distance+2)]
267 +81:
268 + pld [tmp, #32*(prefetch_distance+1)]
269 + .endif
270 +82:
271 +.endm
272 +
273 +.macro preload_all backwards, narrow_case, shift, base, remain, tmp0, tmp1
274 + .if backwards
275 + sub tmp0, base, #1
276 + bic tmp0, tmp0, #31
277 + pld [tmp0]
278 + sub tmp1, base, remain, lsl #shift
279 + .else
280 + bic tmp0, base, #31
281 + pld [tmp0]
282 + add tmp1, base, remain, lsl #shift
283 + sub tmp1, tmp1, #1
284 + .endif
285 + bic tmp1, tmp1, #31
286 + cmp tmp1, tmp0
287 + beq 92f
288 + .if narrow_case
289 + /* In this case, all the data fits in either 1 or 2 cache lines */
290 + pld [tmp1]
291 + .else
292 +91:
293 + .if backwards
294 + sub tmp0, tmp0, #32
295 + .else
296 + add tmp0, tmp0, #32
297 + .endif
298 + cmp tmp0, tmp1
299 + pld [tmp0]
300 + bne 91b
301 + .endif
302 +92:
303 +.endm
304 --- a/arch/arm/lib/copy_from_user.S
305 +++ b/arch/arm/lib/copy_from_user.S
306 @@ -104,7 +104,8 @@ UNWIND( .save {r0, r2, r3, \regs} )
307
308 .text
309
310 -ENTRY(arm_copy_from_user)
311 +ENTRY(__copy_from_user_std)
312 +WEAK(arm_copy_from_user)
313 #ifdef CONFIG_CPU_SPECTRE
314 ldr r3, =TASK_SIZE
315 uaccess_mask_range_ptr r1, r2, r3, ip
316 @@ -113,6 +114,7 @@ ENTRY(arm_copy_from_user)
317 #include "copy_template.S"
318
319 ENDPROC(arm_copy_from_user)
320 +ENDPROC(__copy_from_user_std)
321
322 .pushsection .text.fixup,"ax"
323 .align 0
324 --- /dev/null
325 +++ b/arch/arm/lib/exports_rpi.c
326 @@ -0,0 +1,37 @@
327 +/**
328 + * Copyright (c) 2014, Raspberry Pi (Trading) Ltd.
329 + *
330 + * Redistribution and use in source and binary forms, with or without
331 + * modification, are permitted provided that the following conditions
332 + * are met:
333 + * 1. Redistributions of source code must retain the above copyright
334 + * notice, this list of conditions, and the following disclaimer,
335 + * without modification.
336 + * 2. Redistributions in binary form must reproduce the above copyright
337 + * notice, this list of conditions and the following disclaimer in the
338 + * documentation and/or other materials provided with the distribution.
339 + * 3. The names of the above-listed copyright holders may not be used
340 + * to endorse or promote products derived from this software without
341 + * specific prior written permission.
342 + *
343 + * ALTERNATIVELY, this software may be distributed under the terms of the
344 + * GNU General Public License ("GPL") version 2, as published by the Free
345 + * Software Foundation.
346 + *
347 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
348 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
349 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
350 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
351 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
352 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
353 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
354 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
355 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
356 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
357 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
358 + */
359 +
360 +#include <linux/kernel.h>
361 +#include <linux/module.h>
362 +
363 +EXPORT_SYMBOL(memcmp);
364 --- /dev/null
365 +++ b/arch/arm/lib/memcmp_rpi.S
366 @@ -0,0 +1,285 @@
367 +/*
368 +Copyright (c) 2013, Raspberry Pi Foundation
369 +Copyright (c) 2013, RISC OS Open Ltd
370 +All rights reserved.
371 +
372 +Redistribution and use in source and binary forms, with or without
373 +modification, are permitted provided that the following conditions are met:
374 + * Redistributions of source code must retain the above copyright
375 + notice, this list of conditions and the following disclaimer.
376 + * Redistributions in binary form must reproduce the above copyright
377 + notice, this list of conditions and the following disclaimer in the
378 + documentation and/or other materials provided with the distribution.
379 + * Neither the name of the copyright holder nor the
380 + names of its contributors may be used to endorse or promote products
381 + derived from this software without specific prior written permission.
382 +
383 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
384 +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
385 +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
386 +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
387 +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
388 +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
389 +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
390 +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
391 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
392 +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
393 +*/
394 +
395 +#include <linux/linkage.h>
396 +#include "arm-mem.h"
397 +
398 +/* Prevent the stack from becoming executable */
399 +#if defined(__linux__) && defined(__ELF__)
400 +.section .note.GNU-stack,"",%progbits
401 +#endif
402 +
403 + .text
404 + .arch armv6
405 + .object_arch armv4
406 + .arm
407 + .altmacro
408 + .p2align 2
409 +
410 +.macro memcmp_process_head unaligned
411 + .if unaligned
412 + ldr DAT0, [S_1], #4
413 + ldr DAT1, [S_1], #4
414 + ldr DAT2, [S_1], #4
415 + ldr DAT3, [S_1], #4
416 + .else
417 + ldmia S_1!, {DAT0, DAT1, DAT2, DAT3}
418 + .endif
419 + ldmia S_2!, {DAT4, DAT5, DAT6, DAT7}
420 +.endm
421 +
422 +.macro memcmp_process_tail
423 + cmp DAT0, DAT4
424 + cmpeq DAT1, DAT5
425 + cmpeq DAT2, DAT6
426 + cmpeq DAT3, DAT7
427 + bne 200f
428 +.endm
429 +
430 +.macro memcmp_leading_31bytes
431 + movs DAT0, OFF, lsl #31
432 + ldrmib DAT0, [S_1], #1
433 + ldrcsh DAT1, [S_1], #2
434 + ldrmib DAT4, [S_2], #1
435 + ldrcsh DAT5, [S_2], #2
436 + movpl DAT0, #0
437 + movcc DAT1, #0
438 + movpl DAT4, #0
439 + movcc DAT5, #0
440 + submi N, N, #1
441 + subcs N, N, #2
442 + cmp DAT0, DAT4
443 + cmpeq DAT1, DAT5
444 + bne 200f
445 + movs DAT0, OFF, lsl #29
446 + ldrmi DAT0, [S_1], #4
447 + ldrcs DAT1, [S_1], #4
448 + ldrcs DAT2, [S_1], #4
449 + ldrmi DAT4, [S_2], #4
450 + ldmcsia S_2!, {DAT5, DAT6}
451 + movpl DAT0, #0
452 + movcc DAT1, #0
453 + movcc DAT2, #0
454 + movpl DAT4, #0
455 + movcc DAT5, #0
456 + movcc DAT6, #0
457 + submi N, N, #4
458 + subcs N, N, #8
459 + cmp DAT0, DAT4
460 + cmpeq DAT1, DAT5
461 + cmpeq DAT2, DAT6
462 + bne 200f
463 + tst OFF, #16
464 + beq 105f
465 + memcmp_process_head 1
466 + sub N, N, #16
467 + memcmp_process_tail
468 +105:
469 +.endm
470 +
471 +.macro memcmp_trailing_15bytes unaligned
472 + movs N, N, lsl #29
473 + .if unaligned
474 + ldrcs DAT0, [S_1], #4
475 + ldrcs DAT1, [S_1], #4
476 + .else
477 + ldmcsia S_1!, {DAT0, DAT1}
478 + .endif
479 + ldrmi DAT2, [S_1], #4
480 + ldmcsia S_2!, {DAT4, DAT5}
481 + ldrmi DAT6, [S_2], #4
482 + movcc DAT0, #0
483 + movcc DAT1, #0
484 + movpl DAT2, #0
485 + movcc DAT4, #0
486 + movcc DAT5, #0
487 + movpl DAT6, #0
488 + cmp DAT0, DAT4
489 + cmpeq DAT1, DAT5
490 + cmpeq DAT2, DAT6
491 + bne 200f
492 + movs N, N, lsl #2
493 + ldrcsh DAT0, [S_1], #2
494 + ldrmib DAT1, [S_1]
495 + ldrcsh DAT4, [S_2], #2
496 + ldrmib DAT5, [S_2]
497 + movcc DAT0, #0
498 + movpl DAT1, #0
499 + movcc DAT4, #0
500 + movpl DAT5, #0
501 + cmp DAT0, DAT4
502 + cmpeq DAT1, DAT5
503 + bne 200f
504 +.endm
505 +
506 +.macro memcmp_long_inner_loop unaligned
507 +110:
508 + memcmp_process_head unaligned
509 + pld [S_2, #prefetch_distance*32 + 16]
510 + memcmp_process_tail
511 + memcmp_process_head unaligned
512 + pld [S_1, OFF]
513 + memcmp_process_tail
514 + subs N, N, #32
515 + bhs 110b
516 + /* Just before the final (prefetch_distance+1) 32-byte blocks,
517 + * deal with final preloads */
518 + preload_trailing 0, S_1, N, DAT0
519 + preload_trailing 0, S_2, N, DAT0
520 + add N, N, #(prefetch_distance+2)*32 - 16
521 +120:
522 + memcmp_process_head unaligned
523 + memcmp_process_tail
524 + subs N, N, #16
525 + bhs 120b
526 + /* Trailing words and bytes */
527 + tst N, #15
528 + beq 199f
529 + memcmp_trailing_15bytes unaligned
530 +199: /* Reached end without detecting a difference */
531 + mov a1, #0
532 + setend le
533 + pop {DAT1-DAT6, pc}
534 +.endm
535 +
536 +.macro memcmp_short_inner_loop unaligned
537 + subs N, N, #16 /* simplifies inner loop termination */
538 + blo 122f
539 +120:
540 + memcmp_process_head unaligned
541 + memcmp_process_tail
542 + subs N, N, #16
543 + bhs 120b
544 +122: /* Trailing words and bytes */
545 + tst N, #15
546 + beq 199f
547 + memcmp_trailing_15bytes unaligned
548 +199: /* Reached end without detecting a difference */
549 + mov a1, #0
550 + setend le
551 + pop {DAT1-DAT6, pc}
552 +.endm
553 +
554 +/*
555 + * int memcmp(const void *s1, const void *s2, size_t n);
556 + * On entry:
557 + * a1 = pointer to buffer 1
558 + * a2 = pointer to buffer 2
559 + * a3 = number of bytes to compare (as unsigned chars)
560 + * On exit:
561 + * a1 = >0/=0/<0 if s1 >/=/< s2
562 + */
563 +
564 +.set prefetch_distance, 2
565 +
566 +ENTRY(memcmp)
567 + S_1 .req a1
568 + S_2 .req a2
569 + N .req a3
570 + DAT0 .req a4
571 + DAT1 .req v1
572 + DAT2 .req v2
573 + DAT3 .req v3
574 + DAT4 .req v4
575 + DAT5 .req v5
576 + DAT6 .req v6
577 + DAT7 .req ip
578 + OFF .req lr
579 +
580 + push {DAT1-DAT6, lr}
581 + setend be /* lowest-addressed bytes are most significant */
582 +
583 + /* To preload ahead as we go, we need at least (prefetch_distance+2) 32-byte blocks */
584 + cmp N, #(prefetch_distance+3)*32 - 1
585 + blo 170f
586 +
587 + /* Long case */
588 + /* Adjust N so that the decrement instruction can also test for
589 + * inner loop termination. We want it to stop when there are
590 + * (prefetch_distance+1) complete blocks to go. */
591 + sub N, N, #(prefetch_distance+2)*32
592 + preload_leading_step1 0, DAT0, S_1
593 + preload_leading_step1 0, DAT1, S_2
594 + tst S_2, #31
595 + beq 154f
596 + rsb OFF, S_2, #0 /* no need to AND with 15 here */
597 + preload_leading_step2 0, DAT0, S_1, OFF, DAT2
598 + preload_leading_step2 0, DAT1, S_2, OFF, DAT2
599 + memcmp_leading_31bytes
600 +154: /* Second source now cacheline (32-byte) aligned; we have at
601 + * least one prefetch to go. */
602 + /* Prefetch offset is best selected such that it lies in the
603 + * first 8 of each 32 bytes - but it's just as easy to aim for
604 + * the first one */
605 + and OFF, S_1, #31
606 + rsb OFF, OFF, #32*prefetch_distance
607 + tst S_1, #3
608 + bne 140f
609 + memcmp_long_inner_loop 0
610 +140: memcmp_long_inner_loop 1
611 +
612 +170: /* Short case */
613 + teq N, #0
614 + beq 199f
615 + preload_all 0, 0, 0, S_1, N, DAT0, DAT1
616 + preload_all 0, 0, 0, S_2, N, DAT0, DAT1
617 + tst S_2, #3
618 + beq 174f
619 +172: subs N, N, #1
620 + blo 199f
621 + ldrb DAT0, [S_1], #1
622 + ldrb DAT4, [S_2], #1
623 + cmp DAT0, DAT4
624 + bne 200f
625 + tst S_2, #3
626 + bne 172b
627 +174: /* Second source now 4-byte aligned; we have 0 or more bytes to go */
628 + tst S_1, #3
629 + bne 140f
630 + memcmp_short_inner_loop 0
631 +140: memcmp_short_inner_loop 1
632 +
633 +200: /* Difference found: determine sign. */
634 + movhi a1, #1
635 + movlo a1, #-1
636 + setend le
637 + pop {DAT1-DAT6, pc}
638 +
639 + .unreq S_1
640 + .unreq S_2
641 + .unreq N
642 + .unreq DAT0
643 + .unreq DAT1
644 + .unreq DAT2
645 + .unreq DAT3
646 + .unreq DAT4
647 + .unreq DAT5
648 + .unreq DAT6
649 + .unreq DAT7
650 + .unreq OFF
651 +ENDPROC(memcmp)
652 --- /dev/null
653 +++ b/arch/arm/lib/memcpy_rpi.S
654 @@ -0,0 +1,65 @@
655 +/*
656 +Copyright (c) 2013, Raspberry Pi Foundation
657 +Copyright (c) 2013, RISC OS Open Ltd
658 +All rights reserved.
659 +
660 +Redistribution and use in source and binary forms, with or without
661 +modification, are permitted provided that the following conditions are met:
662 + * Redistributions of source code must retain the above copyright
663 + notice, this list of conditions and the following disclaimer.
664 + * Redistributions in binary form must reproduce the above copyright
665 + notice, this list of conditions and the following disclaimer in the
666 + documentation and/or other materials provided with the distribution.
667 + * Neither the name of the copyright holder nor the
668 + names of its contributors may be used to endorse or promote products
669 + derived from this software without specific prior written permission.
670 +
671 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
672 +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
673 +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
674 +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
675 +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
676 +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
677 +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
678 +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
679 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
680 +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
681 +*/
682 +
683 +#include <linux/linkage.h>
684 +#include <asm/assembler.h>
685 +#include <asm/unwind.h>
686 +#include "arm-mem.h"
687 +#include "memcpymove.h"
688 +
689 +/* Prevent the stack from becoming executable */
690 +#if defined(__linux__) && defined(__ELF__)
691 +.section .note.GNU-stack,"",%progbits
692 +#endif
693 +
694 + .text
695 + .arch armv6
696 + .object_arch armv4
697 + .arm
698 + .altmacro
699 + .p2align 2
700 +
701 +/*
702 + * void *memcpy(void * restrict s1, const void * restrict s2, size_t n);
703 + * On entry:
704 + * a1 = pointer to destination
705 + * a2 = pointer to source
706 + * a3 = number of bytes to copy
707 + * On exit:
708 + * a1 preserved
709 + */
710 +
711 +.set prefetch_distance, 3
712 +
713 +ENTRY(mmiocpy)
714 +ENTRY(memcpy)
715 +ENTRY(__memcpy)
716 + memcpy 0
717 +ENDPROC(__memcpy)
718 +ENDPROC(memcpy)
719 +ENDPROC(mmiocpy)
720 --- /dev/null
721 +++ b/arch/arm/lib/memcpymove.h
722 @@ -0,0 +1,488 @@
723 +/*
724 +Copyright (c) 2013, Raspberry Pi Foundation
725 +Copyright (c) 2013, RISC OS Open Ltd
726 +All rights reserved.
727 +
728 +Redistribution and use in source and binary forms, with or without
729 +modification, are permitted provided that the following conditions are met:
730 + * Redistributions of source code must retain the above copyright
731 + notice, this list of conditions and the following disclaimer.
732 + * Redistributions in binary form must reproduce the above copyright
733 + notice, this list of conditions and the following disclaimer in the
734 + documentation and/or other materials provided with the distribution.
735 + * Neither the name of the copyright holder nor the
736 + names of its contributors may be used to endorse or promote products
737 + derived from this software without specific prior written permission.
738 +
739 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
740 +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
741 +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
742 +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
743 +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
744 +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
745 +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
746 +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
747 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
748 +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
749 +*/
750 +
751 +.macro unaligned_words backwards, align, use_pld, words, r0, r1, r2, r3, r4, r5, r6, r7, r8
752 + .if words == 1
753 + .if backwards
754 + mov r1, r0, lsl #32-align*8
755 + ldr r0, [S, #-4]!
756 + orr r1, r1, r0, lsr #align*8
757 + str r1, [D, #-4]!
758 + .else
759 + mov r0, r1, lsr #align*8
760 + ldr r1, [S, #4]!
761 + orr r0, r0, r1, lsl #32-align*8
762 + str r0, [D], #4
763 + .endif
764 + .elseif words == 2
765 + .if backwards
766 + ldr r1, [S, #-4]!
767 + mov r2, r0, lsl #32-align*8
768 + ldr r0, [S, #-4]!
769 + orr r2, r2, r1, lsr #align*8
770 + mov r1, r1, lsl #32-align*8
771 + orr r1, r1, r0, lsr #align*8
772 + stmdb D!, {r1, r2}
773 + .else
774 + ldr r1, [S, #4]!
775 + mov r0, r2, lsr #align*8
776 + ldr r2, [S, #4]!
777 + orr r0, r0, r1, lsl #32-align*8
778 + mov r1, r1, lsr #align*8
779 + orr r1, r1, r2, lsl #32-align*8
780 + stmia D!, {r0, r1}
781 + .endif
782 + .elseif words == 4
783 + .if backwards
784 + ldmdb S!, {r2, r3}
785 + mov r4, r0, lsl #32-align*8
786 + ldmdb S!, {r0, r1}
787 + orr r4, r4, r3, lsr #align*8
788 + mov r3, r3, lsl #32-align*8
789 + orr r3, r3, r2, lsr #align*8
790 + mov r2, r2, lsl #32-align*8
791 + orr r2, r2, r1, lsr #align*8
792 + mov r1, r1, lsl #32-align*8
793 + orr r1, r1, r0, lsr #align*8
794 + stmdb D!, {r1, r2, r3, r4}
795 + .else
796 + ldmib S!, {r1, r2}
797 + mov r0, r4, lsr #align*8
798 + ldmib S!, {r3, r4}
799 + orr r0, r0, r1, lsl #32-align*8
800 + mov r1, r1, lsr #align*8
801 + orr r1, r1, r2, lsl #32-align*8
802 + mov r2, r2, lsr #align*8
803 + orr r2, r2, r3, lsl #32-align*8
804 + mov r3, r3, lsr #align*8
805 + orr r3, r3, r4, lsl #32-align*8
806 + stmia D!, {r0, r1, r2, r3}
807 + .endif
808 + .elseif words == 8
809 + .if backwards
810 + ldmdb S!, {r4, r5, r6, r7}
811 + mov r8, r0, lsl #32-align*8
812 + ldmdb S!, {r0, r1, r2, r3}
813 + .if use_pld
814 + pld [S, OFF]
815 + .endif
816 + orr r8, r8, r7, lsr #align*8
817 + mov r7, r7, lsl #32-align*8
818 + orr r7, r7, r6, lsr #align*8
819 + mov r6, r6, lsl #32-align*8
820 + orr r6, r6, r5, lsr #align*8
821 + mov r5, r5, lsl #32-align*8
822 + orr r5, r5, r4, lsr #align*8
823 + mov r4, r4, lsl #32-align*8
824 + orr r4, r4, r3, lsr #align*8
825 + mov r3, r3, lsl #32-align*8
826 + orr r3, r3, r2, lsr #align*8
827 + mov r2, r2, lsl #32-align*8
828 + orr r2, r2, r1, lsr #align*8
829 + mov r1, r1, lsl #32-align*8
830 + orr r1, r1, r0, lsr #align*8
831 + stmdb D!, {r5, r6, r7, r8}
832 + stmdb D!, {r1, r2, r3, r4}
833 + .else
834 + ldmib S!, {r1, r2, r3, r4}
835 + mov r0, r8, lsr #align*8
836 + ldmib S!, {r5, r6, r7, r8}
837 + .if use_pld
838 + pld [S, OFF]
839 + .endif
840 + orr r0, r0, r1, lsl #32-align*8
841 + mov r1, r1, lsr #align*8
842 + orr r1, r1, r2, lsl #32-align*8
843 + mov r2, r2, lsr #align*8
844 + orr r2, r2, r3, lsl #32-align*8
845 + mov r3, r3, lsr #align*8
846 + orr r3, r3, r4, lsl #32-align*8
847 + mov r4, r4, lsr #align*8
848 + orr r4, r4, r5, lsl #32-align*8
849 + mov r5, r5, lsr #align*8
850 + orr r5, r5, r6, lsl #32-align*8
851 + mov r6, r6, lsr #align*8
852 + orr r6, r6, r7, lsl #32-align*8
853 + mov r7, r7, lsr #align*8
854 + orr r7, r7, r8, lsl #32-align*8
855 + stmia D!, {r0, r1, r2, r3}
856 + stmia D!, {r4, r5, r6, r7}
857 + .endif
858 + .endif
859 +.endm
860 +
861 +.macro memcpy_leading_15bytes backwards, align
862 + movs DAT1, DAT2, lsl #31
863 + sub N, N, DAT2
864 + .if backwards
865 + ldrmib DAT0, [S, #-1]!
866 + ldrcsh DAT1, [S, #-2]!
867 + strmib DAT0, [D, #-1]!
868 + strcsh DAT1, [D, #-2]!
869 + .else
870 + ldrmib DAT0, [S], #1
871 + ldrcsh DAT1, [S], #2
872 + strmib DAT0, [D], #1
873 + strcsh DAT1, [D], #2
874 + .endif
875 + movs DAT1, DAT2, lsl #29
876 + .if backwards
877 + ldrmi DAT0, [S, #-4]!
878 + .if align == 0
879 + ldmcsdb S!, {DAT1, DAT2}
880 + .else
881 + ldrcs DAT2, [S, #-4]!
882 + ldrcs DAT1, [S, #-4]!
883 + .endif
884 + strmi DAT0, [D, #-4]!
885 + stmcsdb D!, {DAT1, DAT2}
886 + .else
887 + ldrmi DAT0, [S], #4
888 + .if align == 0
889 + ldmcsia S!, {DAT1, DAT2}
890 + .else
891 + ldrcs DAT1, [S], #4
892 + ldrcs DAT2, [S], #4
893 + .endif
894 + strmi DAT0, [D], #4
895 + stmcsia D!, {DAT1, DAT2}
896 + .endif
897 +.endm
898 +
899 +.macro memcpy_trailing_15bytes backwards, align
900 + movs N, N, lsl #29
901 + .if backwards
902 + .if align == 0
903 + ldmcsdb S!, {DAT0, DAT1}
904 + .else
905 + ldrcs DAT1, [S, #-4]!
906 + ldrcs DAT0, [S, #-4]!
907 + .endif
908 + ldrmi DAT2, [S, #-4]!
909 + stmcsdb D!, {DAT0, DAT1}
910 + strmi DAT2, [D, #-4]!
911 + .else
912 + .if align == 0
913 + ldmcsia S!, {DAT0, DAT1}
914 + .else
915 + ldrcs DAT0, [S], #4
916 + ldrcs DAT1, [S], #4
917 + .endif
918 + ldrmi DAT2, [S], #4
919 + stmcsia D!, {DAT0, DAT1}
920 + strmi DAT2, [D], #4
921 + .endif
922 + movs N, N, lsl #2
923 + .if backwards
924 + ldrcsh DAT0, [S, #-2]!
925 + ldrmib DAT1, [S, #-1]
926 + strcsh DAT0, [D, #-2]!
927 + strmib DAT1, [D, #-1]
928 + .else
929 + ldrcsh DAT0, [S], #2
930 + ldrmib DAT1, [S]
931 + strcsh DAT0, [D], #2
932 + strmib DAT1, [D]
933 + .endif
934 +.endm
935 +
936 +.macro memcpy_long_inner_loop backwards, align
937 + .if align != 0
938 + .if backwards
939 + ldr DAT0, [S, #-align]!
940 + .else
941 + ldr LAST, [S, #-align]!
942 + .endif
943 + .endif
944 +110:
945 + .if align == 0
946 + .if backwards
947 + ldmdb S!, {DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, LAST}
948 + pld [S, OFF]
949 + stmdb D!, {DAT4, DAT5, DAT6, LAST}
950 + stmdb D!, {DAT0, DAT1, DAT2, DAT3}
951 + .else
952 + ldmia S!, {DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, LAST}
953 + pld [S, OFF]
954 + stmia D!, {DAT0, DAT1, DAT2, DAT3}
955 + stmia D!, {DAT4, DAT5, DAT6, LAST}
956 + .endif
957 + .else
958 + unaligned_words backwards, align, 1, 8, DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, DAT7, LAST
959 + .endif
960 + subs N, N, #32
961 + bhs 110b
962 + /* Just before the final (prefetch_distance+1) 32-byte blocks, deal with final preloads */
963 + preload_trailing backwards, S, N, OFF
964 + add N, N, #(prefetch_distance+2)*32 - 32
965 +120:
966 + .if align == 0
967 + .if backwards
968 + ldmdb S!, {DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, LAST}
969 + stmdb D!, {DAT4, DAT5, DAT6, LAST}
970 + stmdb D!, {DAT0, DAT1, DAT2, DAT3}
971 + .else
972 + ldmia S!, {DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, LAST}
973 + stmia D!, {DAT0, DAT1, DAT2, DAT3}
974 + stmia D!, {DAT4, DAT5, DAT6, LAST}
975 + .endif
976 + .else
977 + unaligned_words backwards, align, 0, 8, DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, DAT7, LAST
978 + .endif
979 + subs N, N, #32
980 + bhs 120b
981 + tst N, #16
982 + .if align == 0
983 + .if backwards
984 + ldmnedb S!, {DAT0, DAT1, DAT2, LAST}
985 + stmnedb D!, {DAT0, DAT1, DAT2, LAST}
986 + .else
987 + ldmneia S!, {DAT0, DAT1, DAT2, LAST}
988 + stmneia D!, {DAT0, DAT1, DAT2, LAST}
989 + .endif
990 + .else
991 + beq 130f
992 + unaligned_words backwards, align, 0, 4, DAT0, DAT1, DAT2, DAT3, LAST
993 +130:
994 + .endif
995 + /* Trailing words and bytes */
996 + tst N, #15
997 + beq 199f
998 + .if align != 0
999 + add S, S, #align
1000 + .endif
1001 + memcpy_trailing_15bytes backwards, align
1002 +199:
1003 + pop {DAT3, DAT4, DAT5, DAT6, DAT7}
1004 + pop {D, DAT1, DAT2, pc}
1005 +.endm
1006 +
1007 +.macro memcpy_medium_inner_loop backwards, align
1008 +120:
1009 + .if backwards
1010 + .if align == 0
1011 + ldmdb S!, {DAT0, DAT1, DAT2, LAST}
1012 + .else
1013 + ldr LAST, [S, #-4]!
1014 + ldr DAT2, [S, #-4]!
1015 + ldr DAT1, [S, #-4]!
1016 + ldr DAT0, [S, #-4]!
1017 + .endif
1018 + stmdb D!, {DAT0, DAT1, DAT2, LAST}
1019 + .else
1020 + .if align == 0
1021 + ldmia S!, {DAT0, DAT1, DAT2, LAST}
1022 + .else
1023 + ldr DAT0, [S], #4
1024 + ldr DAT1, [S], #4
1025 + ldr DAT2, [S], #4
1026 + ldr LAST, [S], #4
1027 + .endif
1028 + stmia D!, {DAT0, DAT1, DAT2, LAST}
1029 + .endif
1030 + subs N, N, #16
1031 + bhs 120b
1032 + /* Trailing words and bytes */
1033 + tst N, #15
1034 + beq 199f
1035 + memcpy_trailing_15bytes backwards, align
1036 +199:
1037 + pop {D, DAT1, DAT2, pc}
1038 +.endm
1039 +
1040 +.macro memcpy_short_inner_loop backwards, align
1041 + tst N, #16
1042 + .if backwards
1043 + .if align == 0
1044 + ldmnedb S!, {DAT0, DAT1, DAT2, LAST}
1045 + .else
1046 + ldrne LAST, [S, #-4]!
1047 + ldrne DAT2, [S, #-4]!
1048 + ldrne DAT1, [S, #-4]!
1049 + ldrne DAT0, [S, #-4]!
1050 + .endif
1051 + stmnedb D!, {DAT0, DAT1, DAT2, LAST}
1052 + .else
1053 + .if align == 0
1054 + ldmneia S!, {DAT0, DAT1, DAT2, LAST}
1055 + .else
1056 + ldrne DAT0, [S], #4
1057 + ldrne DAT1, [S], #4
1058 + ldrne DAT2, [S], #4
1059 + ldrne LAST, [S], #4
1060 + .endif
1061 + stmneia D!, {DAT0, DAT1, DAT2, LAST}
1062 + .endif
1063 + memcpy_trailing_15bytes backwards, align
1064 +199:
1065 + pop {D, DAT1, DAT2, pc}
1066 +.endm
1067 +
1068 +.macro memcpy backwards
1069 + D .req a1
1070 + S .req a2
1071 + N .req a3
1072 + DAT0 .req a4
1073 + DAT1 .req v1
1074 + DAT2 .req v2
1075 + DAT3 .req v3
1076 + DAT4 .req v4
1077 + DAT5 .req v5
1078 + DAT6 .req v6
1079 + DAT7 .req sl
1080 + LAST .req ip
1081 + OFF .req lr
1082 +
1083 + UNWIND( .fnstart )
1084 +
1085 + push {D, DAT1, DAT2, lr}
1086 + UNWIND( .fnend )
1087 +
1088 + UNWIND( .fnstart )
1089 + UNWIND( .save {D, DAT1, DAT2, lr} )
1090 +
1091 + .if backwards
1092 + add D, D, N
1093 + add S, S, N
1094 + .endif
1095 +
1096 + /* See if we're guaranteed to have at least one 16-byte aligned 16-byte write */
1097 + cmp N, #31
1098 + blo 170f
1099 + /* To preload ahead as we go, we need at least (prefetch_distance+2) 32-byte blocks */
1100 + cmp N, #(prefetch_distance+3)*32 - 1
1101 + blo 160f
1102 +
1103 + /* Long case */
1104 + push {DAT3, DAT4, DAT5, DAT6, DAT7}
1105 + UNWIND( .fnend )
1106 +
1107 + UNWIND( .fnstart )
1108 + UNWIND( .save {D, DAT1, DAT2, lr} )
1109 + UNWIND( .save {DAT3, DAT4, DAT5, DAT6, DAT7} )
1110 +
1111 + /* Adjust N so that the decrement instruction can also test for
1112 + * inner loop termination. We want it to stop when there are
1113 + * (prefetch_distance+1) complete blocks to go. */
1114 + sub N, N, #(prefetch_distance+2)*32
1115 + preload_leading_step1 backwards, DAT0, S
1116 + .if backwards
1117 + /* Bug in GAS: it accepts, but mis-assembles the instruction
1118 + * ands DAT2, D, #60, 2
1119 + * which sets DAT2 to the number of leading bytes until destination is aligned and also clears C (sets borrow)
1120 + */
1121 + .word 0xE210513C
1122 + beq 154f
1123 + .else
1124 + ands DAT2, D, #15
1125 + beq 154f
1126 + rsb DAT2, DAT2, #16 /* number of leading bytes until destination aligned */
1127 + .endif
1128 + preload_leading_step2 backwards, DAT0, S, DAT2, OFF
1129 + memcpy_leading_15bytes backwards, 1
1130 +154: /* Destination now 16-byte aligned; we have at least one prefetch as well as at least one 16-byte output block */
1131 + /* 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 */
1132 + .if backwards
1133 + rsb OFF, S, #3
1134 + and OFF, OFF, #28
1135 + sub OFF, OFF, #32*(prefetch_distance+1)
1136 + .else
1137 + and OFF, S, #28
1138 + rsb OFF, OFF, #32*prefetch_distance
1139 + .endif
1140 + movs DAT0, S, lsl #31
1141 + bhi 157f
1142 + bcs 156f
1143 + bmi 155f
1144 + memcpy_long_inner_loop backwards, 0
1145 +155: memcpy_long_inner_loop backwards, 1
1146 +156: memcpy_long_inner_loop backwards, 2
1147 +157: memcpy_long_inner_loop backwards, 3
1148 +
1149 + UNWIND( .fnend )
1150 +
1151 + UNWIND( .fnstart )
1152 + UNWIND( .save {D, DAT1, DAT2, lr} )
1153 +
1154 +160: /* Medium case */
1155 + preload_all backwards, 0, 0, S, N, DAT2, OFF
1156 + sub N, N, #16 /* simplifies inner loop termination */
1157 + .if backwards
1158 + ands DAT2, D, #15
1159 + beq 164f
1160 + .else
1161 + ands DAT2, D, #15
1162 + beq 164f
1163 + rsb DAT2, DAT2, #16
1164 + .endif
1165 + memcpy_leading_15bytes backwards, align
1166 +164: /* Destination now 16-byte aligned; we have at least one 16-byte output block */
1167 + tst S, #3
1168 + bne 140f
1169 + memcpy_medium_inner_loop backwards, 0
1170 +140: memcpy_medium_inner_loop backwards, 1
1171 +
1172 +170: /* Short case, less than 31 bytes, so no guarantee of at least one 16-byte block */
1173 + teq N, #0
1174 + beq 199f
1175 + preload_all backwards, 1, 0, S, N, DAT2, LAST
1176 + tst D, #3
1177 + beq 174f
1178 +172: subs N, N, #1
1179 + blo 199f
1180 + .if backwards
1181 + ldrb DAT0, [S, #-1]!
1182 + strb DAT0, [D, #-1]!
1183 + .else
1184 + ldrb DAT0, [S], #1
1185 + strb DAT0, [D], #1
1186 + .endif
1187 + tst D, #3
1188 + bne 172b
1189 +174: /* Destination now 4-byte aligned; we have 0 or more output bytes to go */
1190 + tst S, #3
1191 + bne 140f
1192 + memcpy_short_inner_loop backwards, 0
1193 +140: memcpy_short_inner_loop backwards, 1
1194 +
1195 + UNWIND( .fnend )
1196 +
1197 + .unreq D
1198 + .unreq S
1199 + .unreq N
1200 + .unreq DAT0
1201 + .unreq DAT1
1202 + .unreq DAT2
1203 + .unreq DAT3
1204 + .unreq DAT4
1205 + .unreq DAT5
1206 + .unreq DAT6
1207 + .unreq DAT7
1208 + .unreq LAST
1209 + .unreq OFF
1210 +.endm
1211 --- /dev/null
1212 +++ b/arch/arm/lib/memmove_rpi.S
1213 @@ -0,0 +1,63 @@
1214 +/*
1215 +Copyright (c) 2013, Raspberry Pi Foundation
1216 +Copyright (c) 2013, RISC OS Open Ltd
1217 +All rights reserved.
1218 +
1219 +Redistribution and use in source and binary forms, with or without
1220 +modification, are permitted provided that the following conditions are met:
1221 + * Redistributions of source code must retain the above copyright
1222 + notice, this list of conditions and the following disclaimer.
1223 + * Redistributions in binary form must reproduce the above copyright
1224 + notice, this list of conditions and the following disclaimer in the
1225 + documentation and/or other materials provided with the distribution.
1226 + * Neither the name of the copyright holder nor the
1227 + names of its contributors may be used to endorse or promote products
1228 + derived from this software without specific prior written permission.
1229 +
1230 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1231 +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1232 +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1233 +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
1234 +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1235 +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1236 +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
1237 +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1238 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1239 +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1240 +*/
1241 +
1242 +#include <linux/linkage.h>
1243 +#include <asm/assembler.h>
1244 +#include <asm/unwind.h>
1245 +#include "arm-mem.h"
1246 +#include "memcpymove.h"
1247 +
1248 +/* Prevent the stack from becoming executable */
1249 +#if defined(__linux__) && defined(__ELF__)
1250 +.section .note.GNU-stack,"",%progbits
1251 +#endif
1252 +
1253 + .text
1254 + .arch armv6
1255 + .object_arch armv4
1256 + .arm
1257 + .altmacro
1258 + .p2align 2
1259 +
1260 +/*
1261 + * void *memmove(void *s1, const void *s2, size_t n);
1262 + * On entry:
1263 + * a1 = pointer to destination
1264 + * a2 = pointer to source
1265 + * a3 = number of bytes to copy
1266 + * On exit:
1267 + * a1 preserved
1268 + */
1269 +
1270 +.set prefetch_distance, 3
1271 +
1272 +ENTRY(memmove)
1273 + cmp a2, a1
1274 + bpl memcpy /* pl works even over -1 - 0 and 0x7fffffff - 0x80000000 boundaries */
1275 + memcpy 1
1276 +ENDPROC(memmove)
1277 --- /dev/null
1278 +++ b/arch/arm/lib/memset_rpi.S
1279 @@ -0,0 +1,132 @@
1280 +/*
1281 +Copyright (c) 2013, Raspberry Pi Foundation
1282 +Copyright (c) 2013, RISC OS Open Ltd
1283 +All rights reserved.
1284 +
1285 +Redistribution and use in source and binary forms, with or without
1286 +modification, are permitted provided that the following conditions are met:
1287 + * Redistributions of source code must retain the above copyright
1288 + notice, this list of conditions and the following disclaimer.
1289 + * Redistributions in binary form must reproduce the above copyright
1290 + notice, this list of conditions and the following disclaimer in the
1291 + documentation and/or other materials provided with the distribution.
1292 + * Neither the name of the copyright holder nor the
1293 + names of its contributors may be used to endorse or promote products
1294 + derived from this software without specific prior written permission.
1295 +
1296 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1297 +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1298 +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1299 +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
1300 +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1301 +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1302 +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
1303 +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1304 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1305 +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1306 +*/
1307 +
1308 +#include <linux/linkage.h>
1309 +#include "arm-mem.h"
1310 +
1311 +/* Prevent the stack from becoming executable */
1312 +#if defined(__linux__) && defined(__ELF__)
1313 +.section .note.GNU-stack,"",%progbits
1314 +#endif
1315 +
1316 + .text
1317 + .arch armv6
1318 + .object_arch armv4
1319 + .arm
1320 + .altmacro
1321 + .p2align 2
1322 +
1323 +/*
1324 + * void *memset(void *s, int c, size_t n);
1325 + * On entry:
1326 + * a1 = pointer to buffer to fill
1327 + * a2 = byte pattern to fill with (caller-narrowed)
1328 + * a3 = number of bytes to fill
1329 + * On exit:
1330 + * a1 preserved
1331 + */
1332 +ENTRY(mmioset)
1333 +ENTRY(memset)
1334 +ENTRY(__memset)
1335 +
1336 + S .req a1
1337 + DAT0 .req a2
1338 + N .req a3
1339 + DAT1 .req a4
1340 + DAT2 .req ip
1341 + DAT3 .req lr
1342 +
1343 + orr DAT0, DAT0, DAT0, lsl #8
1344 + orr DAT0, DAT0, DAT0, lsl #16
1345 +
1346 +ENTRY(__memset32)
1347 + mov DAT1, DAT0
1348 +
1349 +ENTRY(__memset64)
1350 + push {S, lr}
1351 +
1352 + /* See if we're guaranteed to have at least one 16-byte aligned 16-byte write */
1353 + cmp N, #31
1354 + blo 170f
1355 +
1356 +161: sub N, N, #16 /* simplifies inner loop termination */
1357 + /* Leading words and bytes */
1358 + tst S, #15
1359 + beq 164f
1360 + rsb DAT3, S, #0 /* bits 0-3 = number of leading bytes until aligned */
1361 + movs DAT2, DAT3, lsl #31
1362 + submi N, N, #1
1363 + strmib DAT0, [S], #1
1364 + subcs N, N, #2
1365 + strcsh DAT0, [S], #2
1366 + movs DAT2, DAT3, lsl #29
1367 + submi N, N, #4
1368 + strmi DAT0, [S], #4
1369 + subcs N, N, #8
1370 + stmcsia S!, {DAT0, DAT1}
1371 +164: /* Delayed set up of DAT2 and DAT3 so we could use them as scratch registers above */
1372 + mov DAT2, DAT0
1373 + mov DAT3, DAT1
1374 + /* Now the inner loop of 16-byte stores */
1375 +165: stmia S!, {DAT0, DAT1, DAT2, DAT3}
1376 + subs N, N, #16
1377 + bhs 165b
1378 +166: /* Trailing words and bytes */
1379 + movs N, N, lsl #29
1380 + stmcsia S!, {DAT0, DAT1}
1381 + strmi DAT0, [S], #4
1382 + movs N, N, lsl #2
1383 + strcsh DAT0, [S], #2
1384 + strmib DAT0, [S]
1385 +199: pop {S, pc}
1386 +
1387 +170: /* Short case */
1388 + mov DAT2, DAT0
1389 + mov DAT3, DAT1
1390 + tst S, #3
1391 + beq 174f
1392 +172: subs N, N, #1
1393 + blo 199b
1394 + strb DAT0, [S], #1
1395 + tst S, #3
1396 + bne 172b
1397 +174: tst N, #16
1398 + stmneia S!, {DAT0, DAT1, DAT2, DAT3}
1399 + b 166b
1400 +
1401 + .unreq S
1402 + .unreq DAT0
1403 + .unreq N
1404 + .unreq DAT1
1405 + .unreq DAT2
1406 + .unreq DAT3
1407 +ENDPROC(__memset64)
1408 +ENDPROC(__memset32)
1409 +ENDPROC(__memset)
1410 +ENDPROC(memset)
1411 +ENDPROC(mmioset)
1412 --- a/arch/arm/lib/uaccess_with_memcpy.c
1413 +++ b/arch/arm/lib/uaccess_with_memcpy.c
1414 @@ -19,6 +19,14 @@
1415 #include <asm/current.h>
1416 #include <asm/page.h>
1417
1418 +#ifndef COPY_FROM_USER_THRESHOLD
1419 +#define COPY_FROM_USER_THRESHOLD 64
1420 +#endif
1421 +
1422 +#ifndef COPY_TO_USER_THRESHOLD
1423 +#define COPY_TO_USER_THRESHOLD 64
1424 +#endif
1425 +
1426 static int
1427 pin_page_for_write(const void __user *_addr, pte_t **ptep, spinlock_t **ptlp)
1428 {
1429 @@ -43,7 +51,7 @@ pin_page_for_write(const void __user *_a
1430 return 0;
1431
1432 pmd = pmd_offset(pud, addr);
1433 - if (unlikely(pmd_none(*pmd)))
1434 + if (unlikely(pmd_none(*pmd) || pmd_bad(*pmd)))
1435 return 0;
1436
1437 /*
1438 @@ -89,7 +97,46 @@ pin_page_for_write(const void __user *_a
1439 return 1;
1440 }
1441
1442 -static unsigned long noinline
1443 +static int
1444 +pin_page_for_read(const void __user *_addr, pte_t **ptep, spinlock_t **ptlp)
1445 +{
1446 + unsigned long addr = (unsigned long)_addr;
1447 + pgd_t *pgd;
1448 + p4d_t *p4d;
1449 + pmd_t *pmd;
1450 + pte_t *pte;
1451 + pud_t *pud;
1452 + spinlock_t *ptl;
1453 +
1454 + pgd = pgd_offset(current->mm, addr);
1455 + if (unlikely(pgd_none(*pgd) || pgd_bad(*pgd)))
1456 + return 0;
1457 +
1458 + p4d = p4d_offset(pgd, addr);
1459 + if (unlikely(p4d_none(*p4d) || p4d_bad(*p4d)))
1460 + return 0;
1461 +
1462 + pud = pud_offset(p4d, addr);
1463 + if (unlikely(pud_none(*pud) || pud_bad(*pud)))
1464 + return 0;
1465 +
1466 + pmd = pmd_offset(pud, addr);
1467 + if (unlikely(pmd_none(*pmd) || pmd_bad(*pmd)))
1468 + return 0;
1469 +
1470 + pte = pte_offset_map_lock(current->mm, pmd, addr, &ptl);
1471 + if (unlikely(!pte_present(*pte) || !pte_young(*pte))) {
1472 + pte_unmap_unlock(pte, ptl);
1473 + return 0;
1474 + }
1475 +
1476 + *ptep = pte;
1477 + *ptlp = ptl;
1478 +
1479 + return 1;
1480 +}
1481 +
1482 +unsigned long noinline
1483 __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n)
1484 {
1485 unsigned long ua_flags;
1486 @@ -137,6 +184,52 @@ out:
1487 return n;
1488 }
1489
1490 +unsigned long noinline
1491 +__copy_from_user_memcpy(void *to, const void __user *from, unsigned long n)
1492 +{
1493 + unsigned long ua_flags;
1494 + int atomic;
1495 +
1496 + /* the mmap semaphore is taken only if not in an atomic context */
1497 + atomic = in_atomic();
1498 +
1499 + if (!atomic)
1500 + mmap_read_lock(current->mm);
1501 + while (n) {
1502 + pte_t *pte;
1503 + spinlock_t *ptl;
1504 + int tocopy;
1505 +
1506 + while (!pin_page_for_read(from, &pte, &ptl)) {
1507 + char temp;
1508 + if (!atomic)
1509 + mmap_read_unlock(current->mm);
1510 + if (__get_user(temp, (char __user *)from))
1511 + goto out;
1512 + if (!atomic)
1513 + mmap_read_lock(current->mm);
1514 + }
1515 +
1516 + tocopy = (~(unsigned long)from & ~PAGE_MASK) + 1;
1517 + if (tocopy > n)
1518 + tocopy = n;
1519 +
1520 + ua_flags = uaccess_save_and_enable();
1521 + memcpy(to, (const void *)from, tocopy);
1522 + uaccess_restore(ua_flags);
1523 + to += tocopy;
1524 + from += tocopy;
1525 + n -= tocopy;
1526 +
1527 + pte_unmap_unlock(pte, ptl);
1528 + }
1529 + if (!atomic)
1530 + mmap_read_unlock(current->mm);
1531 +
1532 +out:
1533 + return n;
1534 +}
1535 +
1536 unsigned long
1537 arm_copy_to_user(void __user *to, const void *from, unsigned long n)
1538 {
1539 @@ -147,7 +240,7 @@ arm_copy_to_user(void __user *to, const
1540 * With frame pointer disabled, tail call optimization kicks in
1541 * as well making this test almost invisible.
1542 */
1543 - if (n < 64) {
1544 + if (n < COPY_TO_USER_THRESHOLD) {
1545 unsigned long ua_flags = uaccess_save_and_enable();
1546 n = __copy_to_user_std(to, from, n);
1547 uaccess_restore(ua_flags);
1548 @@ -157,6 +250,32 @@ arm_copy_to_user(void __user *to, const
1549 }
1550 return n;
1551 }
1552 +
1553 +unsigned long __must_check
1554 +arm_copy_from_user(void *to, const void __user *from, unsigned long n)
1555 +{
1556 +#ifdef CONFIG_BCM2835_FAST_MEMCPY
1557 + /*
1558 + * This test is stubbed out of the main function above to keep
1559 + * the overhead for small copies low by avoiding a large
1560 + * register dump on the stack just to reload them right away.
1561 + * With frame pointer disabled, tail call optimization kicks in
1562 + * as well making this test almost invisible.
1563 + */
1564 + if (n < COPY_TO_USER_THRESHOLD) {
1565 + unsigned long ua_flags = uaccess_save_and_enable();
1566 + n = __copy_from_user_std(to, from, n);
1567 + uaccess_restore(ua_flags);
1568 + } else {
1569 + n = __copy_from_user_memcpy(to, from, n);
1570 + }
1571 +#else
1572 + unsigned long ua_flags = uaccess_save_and_enable();
1573 + n = __copy_from_user_std(to, from, n);
1574 + uaccess_restore(ua_flags);
1575 +#endif
1576 + return n;
1577 +}
1578
1579 static unsigned long noinline
1580 __clear_user_memset(void __user *addr, unsigned long n)
1581 --- a/arch/arm/mach-bcm/Kconfig
1582 +++ b/arch/arm/mach-bcm/Kconfig
1583 @@ -182,6 +182,13 @@ config ARCH_BCM_53573
1584 The base chip is BCM53573 and there are some packaging modifications
1585 like BCM47189 and BCM47452.
1586
1587 +config BCM2835_FAST_MEMCPY
1588 + bool "Enable optimized __copy_to_user and __copy_from_user"
1589 + depends on ARCH_BCM2835 && ARCH_MULTI_V6
1590 + default y
1591 + help
1592 + Optimized versions of __copy_to_user and __copy_from_user for Pi1.
1593 +
1594 config ARCH_BRCMSTB
1595 bool "Broadcom BCM7XXX based boards"
1596 depends on ARCH_MULTI_V7