2 * String handling functions for PowerPC.
4 * Copyright (C) 1996 Paul Mackerras.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 #include <asm/processor.h>
12 #include <asm/cache.h>
13 #include <asm/errno.h>
14 #include <asm/ppc_asm.h>
16 #define COPY_16_BYTES \
26 #define COPY_16_BYTES_WITHEX(n) \
44 #define COPY_16_BYTES_EXCODE(n) \
46 addi r5,r5,-(16 * n); \
49 addi r5,r5,-(16 * n); \
51 .section __ex_table,"a"; \
53 .long 8 ## n ## 0b,9 ## n ## 0b; \
54 .long 8 ## n ## 1b,9 ## n ## 0b; \
55 .long 8 ## n ## 2b,9 ## n ## 0b; \
56 .long 8 ## n ## 3b,9 ## n ## 0b; \
57 .long 8 ## n ## 4b,9 ## n ## 1b; \
58 .long 8 ## n ## 5b,9 ## n ## 1b; \
59 .long 8 ## n ## 6b,9 ## n ## 1b; \
60 .long 8 ## n ## 7b,9 ## n ## 1b; \
64 .stabs "arch/ppc/lib/",N_SO,0,0,0f
65 .stabs "string.S",N_SO,0,0,0f
67 CACHELINE_BYTES = L1_CACHE_BYTES
68 LG_CACHELINE_BYTES = L1_CACHE_SHIFT
69 CACHELINE_MASK = (L1_CACHE_BYTES-1)
80 /* This clears out any unused part of the destination buffer,
81 just as the libc version does. -- paulus */
91 bdnzf 2,1b /* dec ctr, branch if ctr != 0 && !cr0.eq */
92 bnelr /* if we didn't hit a null char, we're done */
94 cmpwi 0,r5,0 /* any space left in destination buffer? */
95 beqlr /* we know r0 == 0 here */
96 2: stbu r0,1(r6) /* clear it out if so */
147 * Use dcbz on the complete cache lines in the destination
148 * to set them to zero. This requires that the destination
149 * area is cacheable. -- paulus
151 _GLOBAL(cacheable_memzero)
162 clrlwi r7,r6,32-LG_CACHELINE_BYTES
164 srwi r9,r8,LG_CACHELINE_BYTES
165 addic. r9,r9,-1 /* total number of complete cachelines */
167 xori r0,r7,CACHELINE_MASK & ~3
175 #if !defined(CONFIG_8xx)
182 #if CACHE_LINE_SIZE >= 32
187 #endif /* CACHE_LINE_SIZE */
189 addi r6,r6,CACHELINE_BYTES
191 clrlwi r5,r8,32-LG_CACHELINE_BYTES
233 * This version uses dcbz on the complete cache lines in the
234 * destination area to reduce memory traffic. This requires that
235 * the destination area is cacheable.
236 * We only use this version if the source and dest don't overlap.
239 _GLOBAL(cacheable_memcpy)
240 add r7,r3,r5 /* test if the src & dst overlap */
244 crand 0,0,4 /* cr0.lt &= cr1.lt */
245 blt memcpy /* if regions overlap */
250 andi. r0,r0,CACHELINE_MASK /* # bytes to start of cache line */
253 cmplw 0,r5,r0 /* is this more than total to do? */
254 blt 63f /* if not much to do */
255 andi. r8,r0,3 /* get it word-aligned first */
259 70: lbz r9,4(r4) /* do some bytes */
267 72: lwzu r9,4(r4) /* do some words */
271 58: srwi. r0,r5,LG_CACHELINE_BYTES /* # complete cachelines */
272 clrlwi r5,r5,32-LG_CACHELINE_BYTES
277 #if !defined(CONFIG_8xx)
281 #if L1_CACHE_BYTES >= 32
283 #if L1_CACHE_BYTES >= 64
286 #if L1_CACHE_BYTES >= 128
322 beq 2f /* if less than 8 bytes to do */
323 andi. r0,r6,3 /* get dest word aligned */
354 rlwinm. r7,r5,32-3,3,31
359 _GLOBAL(backwards_memcpy)
360 rlwinm. r7,r5,32-3,3,31 /* r0 = r5 >> 3 */
390 rlwinm. r7,r5,32-3,3,31
421 _GLOBAL(__copy_tofrom_user)
425 andi. r0,r0,CACHELINE_MASK /* # bytes to start of cache line */
428 cmplw 0,r5,r0 /* is this more than total to do? */
429 blt 63f /* if not much to do */
430 andi. r8,r0,3 /* get it word-aligned first */
433 70: lbz r9,4(r4) /* do some bytes */
442 72: lwzu r9,4(r4) /* do some words */
446 .section __ex_table,"a"
454 58: srwi. r0,r5,LG_CACHELINE_BYTES /* # complete cachelines */
455 clrlwi r5,r5,32-LG_CACHELINE_BYTES
460 /* Don't use prefetch on 8xx */
463 53: COPY_16_BYTES_WITHEX(0)
466 #else /* not CONFIG_8xx */
467 /* Here we decide how far ahead to prefetch the source */
473 #if MAX_COPY_PREFETCH > 1
474 /* Heuristically, for large transfers we prefetch
475 MAX_COPY_PREFETCH cachelines ahead. For small transfers
476 we prefetch 1 cacheline ahead. */
477 cmpwi r0,MAX_COPY_PREFETCH
479 li r7,MAX_COPY_PREFETCH
482 addi r3,r3,CACHELINE_BYTES
486 addi r3,r3,CACHELINE_BYTES
487 #endif /* MAX_COPY_PREFETCH > 1 */
495 .section __ex_table,"a"
499 /* the main body of the cacheline loop */
500 COPY_16_BYTES_WITHEX(0)
501 #if L1_CACHE_BYTES >= 32
502 COPY_16_BYTES_WITHEX(1)
503 #if L1_CACHE_BYTES >= 64
504 COPY_16_BYTES_WITHEX(2)
505 COPY_16_BYTES_WITHEX(3)
506 #if L1_CACHE_BYTES >= 128
507 COPY_16_BYTES_WITHEX(4)
508 COPY_16_BYTES_WITHEX(5)
509 COPY_16_BYTES_WITHEX(6)
510 COPY_16_BYTES_WITHEX(7)
519 #endif /* CONFIG_8xx */
539 /* read fault, initial single-byte copy */
542 /* write fault, initial single-byte copy */
547 /* read fault, initial word copy */
550 /* write fault, initial word copy */
556 * this stuff handles faults in the cacheline loop and branches to either
557 * 104f (if in read part) or 105f (if in write part), after updating r5
559 COPY_16_BYTES_EXCODE(0)
560 #if L1_CACHE_BYTES >= 32
561 COPY_16_BYTES_EXCODE(1)
562 #if L1_CACHE_BYTES >= 64
563 COPY_16_BYTES_EXCODE(2)
564 COPY_16_BYTES_EXCODE(3)
565 #if L1_CACHE_BYTES >= 128
566 COPY_16_BYTES_EXCODE(4)
567 COPY_16_BYTES_EXCODE(5)
568 COPY_16_BYTES_EXCODE(6)
569 COPY_16_BYTES_EXCODE(7)
574 /* read fault in cacheline loop */
577 /* fault on dcbz (effectively a write fault) */
578 /* or write fault in cacheline loop */
580 92: li r3,LG_CACHELINE_BYTES
584 /* read fault in final word loop */
587 /* write fault in final word loop */
592 /* read fault in final byte loop */
595 /* write fault in final byte loop */
600 * At this stage the number of bytes not copied is
601 * r5 + (ctr << r3), and r9 is 0 for read or 1 for write.
606 beq 120f /* shouldn't happen */
609 /* for a read fault, first try to continue the copy one byte at a time */
616 /* then clear out the destination: r3 bytes starting at 4(r6) */
632 .section __ex_table,"a"
644 _GLOBAL(__clear_user)
650 /* clear a single word */
653 /* clear word sized chunks */
663 /* clear byte sized chunks */
680 .section __ex_table,"a"
687 _GLOBAL(__strncpy_from_user)
696 bdnzf 2,1b /* dec ctr, branch if ctr != 0 && !cr0.eq */
704 .section __ex_table,"a"
709 /* r3 = str, r4 = len (> 0), r5 = top (highest addr) */
710 _GLOBAL(__strnlen_user)
712 subf r6,r7,r5 /* top+1 - str */
716 0: mtctr r6 /* ctr = min(len, top - str) */
717 1: lbzu r0,1(r7) /* get next byte */
719 bdnzf 2,1b /* loop if --ctr != 0 && byte != 0 */
721 subf r3,r3,r7 /* number of bytes we have looked at */
722 beqlr /* return if we found a 0 byte */
723 cmpw 0,r3,r4 /* did we look at all len bytes? */
724 blt 99f /* if not, must have hit top */
725 addi r3,r4,1 /* return len + 1 to indicate no null found */
727 99: li r3,0 /* bad address, return 0 */
730 .section __ex_table,"a"