2 * BK Id: SCCS/s.string.S 1.19 08/13/02 21:52:53 paulus
5 * String handling functions for PowerPC.
7 * Copyright (C) 1996 Paul Mackerras.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
14 #include <linux/config.h>
15 #include <asm/processor.h>
16 #include <asm/cache.h>
17 #include <asm/errno.h>
18 #include <asm/ppc_asm.h>
20 #define COPY_16_BYTES \
30 #define COPY_16_BYTES_WITHEX(n) \
48 #define COPY_16_BYTES_EXCODE(n) \
50 addi r5,r5,-(16 * n); \
53 addi r5,r5,-(16 * n); \
55 .section __ex_table,"a"; \
57 .long 8 ## n ## 0b,9 ## n ## 0b; \
58 .long 8 ## n ## 1b,9 ## n ## 0b; \
59 .long 8 ## n ## 2b,9 ## n ## 0b; \
60 .long 8 ## n ## 3b,9 ## n ## 0b; \
61 .long 8 ## n ## 4b,9 ## n ## 1b; \
62 .long 8 ## n ## 5b,9 ## n ## 1b; \
63 .long 8 ## n ## 6b,9 ## n ## 1b; \
64 .long 8 ## n ## 7b,9 ## n ## 1b; \
68 .stabs "arch/ppc/lib/",N_SO,0,0,0f
69 .stabs "string.S",N_SO,0,0,0f
71 CACHELINE_BYTES = L1_CACHE_LINE_SIZE
72 LG_CACHELINE_BYTES = LG_L1_CACHE_LINE_SIZE
73 CACHELINE_MASK = (L1_CACHE_LINE_SIZE-1)
93 bdnzf 2,1b /* dec ctr, branch if ctr != 0 && !cr0.eq */
129 * Use dcbz on the complete cache lines in the destination
130 * to set them to zero. This requires that the destination
131 * area is cacheable. -- paulus
133 _GLOBAL(cacheable_memzero)
144 clrlwi r7,r6,32-LG_CACHELINE_BYTES
146 srwi r9,r8,LG_CACHELINE_BYTES
147 addic. r9,r9,-1 /* total number of complete cachelines */
149 xori r0,r7,CACHELINE_MASK & ~3
157 #if !defined(CONFIG_8xx)
165 addi r6,r6,CACHELINE_BYTES
167 clrlwi r5,r8,32-LG_CACHELINE_BYTES
215 * This version uses dcbz on the complete cache lines in the
216 * destination area to reduce memory traffic. This requires that
217 * the destination area is cacheable.
218 * We only use this version if the source and dest don't overlap.
221 _GLOBAL(cacheable_memcpy)
222 add r7,r3,r5 /* test if the src & dst overlap */
226 crand 0,0,4 /* cr0.lt &= cr1.lt */
227 blt memcpy /* if regions overlap */
232 andi. r0,r0,CACHELINE_MASK /* # bytes to start of cache line */
235 cmplw 0,r5,r0 /* is this more than total to do? */
236 blt 63f /* if not much to do */
237 andi. r8,r0,3 /* get it word-aligned first */
241 70: lbz r9,4(r4) /* do some bytes */
249 72: lwzu r9,4(r4) /* do some words */
253 58: srwi. r0,r5,LG_CACHELINE_BYTES /* # complete cachelines */
254 clrlwi r5,r5,32-LG_CACHELINE_BYTES
259 #if !defined(CONFIG_8xx)
263 #if L1_CACHE_LINE_SIZE >= 32
265 #if L1_CACHE_LINE_SIZE >= 64
268 #if L1_CACHE_LINE_SIZE >= 128
304 beq 2f /* if less than 8 bytes to do */
305 andi. r0,r6,3 /* get dest word aligned */
336 rlwinm. r7,r5,32-3,3,31
341 _GLOBAL(backwards_memcpy)
342 rlwinm. r7,r5,32-3,3,31 /* r0 = r5 >> 3 */
372 rlwinm. r7,r5,32-3,3,31
403 _GLOBAL(__copy_tofrom_user)
407 andi. r0,r0,CACHELINE_MASK /* # bytes to start of cache line */
410 cmplw 0,r5,r0 /* is this more than total to do? */
411 blt 63f /* if not much to do */
412 andi. r8,r0,3 /* get it word-aligned first */
415 70: lbz r9,4(r4) /* do some bytes */
424 72: lwzu r9,4(r4) /* do some words */
428 58: srwi. r0,r5,LG_CACHELINE_BYTES /* # complete cachelines */
429 clrlwi r5,r5,32-LG_CACHELINE_BYTES
433 #if !defined(CONFIG_8xx)
434 /* Here we decide how far ahead to prefetch the source */
435 #if MAX_COPY_PREFETCH > 1
436 /* Heuristically, for large transfers we prefetch
437 MAX_COPY_PREFETCH cachelines ahead. For small transfers
438 we prefetch 1 cacheline ahead. */
439 cmpwi r0,MAX_COPY_PREFETCH
443 li r7,MAX_COPY_PREFETCH
446 addi r3,r3,CACHELINE_BYTES
448 #else /* MAX_COPY_PREFETCH == 1 */
449 li r3,CACHELINE_BYTES + 4
451 #endif /* MAX_COPY_PREFETCH */
452 #endif /* CONFIG_8xx */
456 #if !defined(CONFIG_8xx)
460 /* had to move these to keep extable in order */
461 .section __ex_table,"a"
467 #if !defined(CONFIG_8xx)
471 /* the main body of the cacheline loop */
472 COPY_16_BYTES_WITHEX(0)
473 #if L1_CACHE_LINE_SIZE >= 32
474 COPY_16_BYTES_WITHEX(1)
475 #if L1_CACHE_LINE_SIZE >= 64
476 COPY_16_BYTES_WITHEX(2)
477 COPY_16_BYTES_WITHEX(3)
478 #if L1_CACHE_LINE_SIZE >= 128
479 COPY_16_BYTES_WITHEX(4)
480 COPY_16_BYTES_WITHEX(5)
481 COPY_16_BYTES_WITHEX(6)
482 COPY_16_BYTES_WITHEX(7)
506 /* read fault, initial single-byte copy */
509 /* write fault, initial single-byte copy */
514 /* read fault, initial word copy */
517 /* write fault, initial word copy */
523 * this stuff handles faults in the cacheline loop and branches to either
524 * 104f (if in read part) or 105f (if in write part), after updating r5
526 COPY_16_BYTES_EXCODE(0)
527 #if L1_CACHE_LINE_SIZE >= 32
528 COPY_16_BYTES_EXCODE(1)
529 #if L1_CACHE_LINE_SIZE >= 64
530 COPY_16_BYTES_EXCODE(2)
531 COPY_16_BYTES_EXCODE(3)
532 #if L1_CACHE_LINE_SIZE >= 128
533 COPY_16_BYTES_EXCODE(4)
534 COPY_16_BYTES_EXCODE(5)
535 COPY_16_BYTES_EXCODE(6)
536 COPY_16_BYTES_EXCODE(7)
541 /* read fault in cacheline loop */
544 /* fault on dcbz (effectively a write fault) */
545 /* or write fault in cacheline loop */
547 92: li r3,LG_CACHELINE_BYTES
549 /* read fault in final word loop */
552 /* write fault in final word loop */
557 /* read fault in final byte loop */
560 /* write fault in final byte loop */
565 * At this stage the number of bytes not copied is
566 * r5 + (ctr << r3), and r4 is 0 for read or 1 for write.
573 /* for read fault, clear out the destination: r3 bytes starting at 4(r6) */
588 .section __ex_table,"a"
598 _GLOBAL(__clear_user)
604 /* clear a single word */
607 /* clear word sized chunks */
617 /* clear byte sized chunks */
634 .section __ex_table,"a"
641 _GLOBAL(__strncpy_from_user)
650 bdnzf 2,1b /* dec ctr, branch if ctr != 0 && !cr0.eq */
658 .section __ex_table,"a"
663 /* r3 = str, r4 = len (> 0), r5 = top (highest addr) */
664 _GLOBAL(__strnlen_user)
666 subf r6,r7,r5 /* top+1 - str */
670 0: mtctr r6 /* ctr = min(len, top - str) */
671 1: lbzu r0,1(r7) /* get next byte */
673 bdnzf 2,1b /* loop if --ctr != 0 && byte != 0 */
675 subf r3,r3,r7 /* number of bytes we have looked at */
676 beqlr /* return if we found a 0 byte */
677 cmpw 0,r3,r4 /* did we look at all len bytes? */
678 blt 99f /* if not, must have hit top */
679 addi r3,r4,1 /* return len + 1 to indicate no null found */
681 99: li r3,0 /* bad address, return 0 */
684 .section __ex_table,"a"