ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc / lib / string.S
1 /*
2  * String handling functions for PowerPC.
3  *
4  * Copyright (C) 1996 Paul Mackerras.
5  *
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.
10  */
11 #include <linux/config.h>
12 #include <asm/processor.h>
13 #include <asm/cache.h>
14 #include <asm/errno.h>
15 #include <asm/ppc_asm.h>
16
17 #define COPY_16_BYTES           \
18         lwz     r7,4(r4);       \
19         lwz     r8,8(r4);       \
20         lwz     r9,12(r4);      \
21         lwzu    r10,16(r4);     \
22         stw     r7,4(r6);       \
23         stw     r8,8(r6);       \
24         stw     r9,12(r6);      \
25         stwu    r10,16(r6)
26
27 #define COPY_16_BYTES_WITHEX(n) \
28 8 ## n ## 0:                    \
29         lwz     r7,4(r4);       \
30 8 ## n ## 1:                    \
31         lwz     r8,8(r4);       \
32 8 ## n ## 2:                    \
33         lwz     r9,12(r4);      \
34 8 ## n ## 3:                    \
35         lwzu    r10,16(r4);     \
36 8 ## n ## 4:                    \
37         stw     r7,4(r6);       \
38 8 ## n ## 5:                    \
39         stw     r8,8(r6);       \
40 8 ## n ## 6:                    \
41         stw     r9,12(r6);      \
42 8 ## n ## 7:                    \
43         stwu    r10,16(r6)
44
45 #define COPY_16_BYTES_EXCODE(n)                 \
46 9 ## n ## 0:                                    \
47         addi    r5,r5,-(16 * n);                \
48         b       104f;                           \
49 9 ## n ## 1:                                    \
50         addi    r5,r5,-(16 * n);                \
51         b       105f;                           \
52 .section __ex_table,"a";                        \
53         .align  2;                              \
54         .long   8 ## n ## 0b,9 ## n ## 0b;      \
55         .long   8 ## n ## 1b,9 ## n ## 0b;      \
56         .long   8 ## n ## 2b,9 ## n ## 0b;      \
57         .long   8 ## n ## 3b,9 ## n ## 0b;      \
58         .long   8 ## n ## 4b,9 ## n ## 1b;      \
59         .long   8 ## n ## 5b,9 ## n ## 1b;      \
60         .long   8 ## n ## 6b,9 ## n ## 1b;      \
61         .long   8 ## n ## 7b,9 ## n ## 1b;      \
62         .text
63
64         .text
65         .stabs  "arch/ppc/lib/",N_SO,0,0,0f
66         .stabs  "string.S",N_SO,0,0,0f
67
68 CACHELINE_BYTES = L1_CACHE_LINE_SIZE
69 LG_CACHELINE_BYTES = LG_L1_CACHE_LINE_SIZE
70 CACHELINE_MASK = (L1_CACHE_LINE_SIZE-1)
71
72 _GLOBAL(strcpy)
73         addi    r5,r3,-1
74         addi    r4,r4,-1
75 1:      lbzu    r0,1(r4)
76         cmpwi   0,r0,0
77         stbu    r0,1(r5)
78         bne     1b
79         blr
80
81 /* This clears out any unused part of the destination buffer,
82    just as the libc version does.  -- paulus */
83 _GLOBAL(strncpy)
84         cmpwi   0,r5,0
85         beqlr
86         mtctr   r5
87         addi    r6,r3,-1
88         addi    r4,r4,-1
89 1:      lbzu    r0,1(r4)
90         cmpwi   0,r0,0
91         stbu    r0,1(r6)
92         bdnzf   2,1b            /* dec ctr, branch if ctr != 0 && !cr0.eq */
93         bnelr                   /* if we didn't hit a null char, we're done */
94         mfctr   r5
95         cmpwi   0,r5,0          /* any space left in destination buffer? */
96         beqlr                   /* we know r0 == 0 here */
97 2:      stbu    r0,1(r6)        /* clear it out if so */
98         bdnz    2b
99         blr
100
101 _GLOBAL(strcat)
102         addi    r5,r3,-1
103         addi    r4,r4,-1
104 1:      lbzu    r0,1(r5)
105         cmpwi   0,r0,0
106         bne     1b
107         addi    r5,r5,-1
108 1:      lbzu    r0,1(r4)
109         cmpwi   0,r0,0
110         stbu    r0,1(r5)
111         bne     1b
112         blr
113
114 _GLOBAL(strcmp)
115         addi    r5,r3,-1
116         addi    r4,r4,-1
117 1:      lbzu    r3,1(r5)
118         cmpwi   1,r3,0
119         lbzu    r0,1(r4)
120         subf.   r3,r0,r3
121         beqlr   1
122         beq     1b
123         blr
124
125 _GLOBAL(strlen)
126         addi    r4,r3,-1
127 1:      lbzu    r0,1(r4)
128         cmpwi   0,r0,0
129         bne     1b
130         subf    r3,r3,r4
131         blr
132
133 /*
134  * Use dcbz on the complete cache lines in the destination
135  * to set them to zero.  This requires that the destination
136  * area is cacheable.  -- paulus
137  */
138 _GLOBAL(cacheable_memzero)
139         mr      r5,r4
140         li      r4,0
141         addi    r6,r3,-4
142         cmplwi  0,r5,4
143         blt     7f
144         stwu    r4,4(r6)
145         beqlr
146         andi.   r0,r6,3
147         add     r5,r0,r5
148         subf    r6,r0,r6
149         clrlwi  r7,r6,32-LG_CACHELINE_BYTES
150         add     r8,r7,r5
151         srwi    r9,r8,LG_CACHELINE_BYTES
152         addic.  r9,r9,-1        /* total number of complete cachelines */
153         ble     2f
154         xori    r0,r7,CACHELINE_MASK & ~3
155         srwi.   r0,r0,2
156         beq     3f
157         mtctr   r0
158 4:      stwu    r4,4(r6)
159         bdnz    4b
160 3:      mtctr   r9
161         li      r7,4
162 #if !defined(CONFIG_8xx)
163 10:     dcbz    r7,r6
164 #else
165 10:     stw     r4, 4(r6)
166         stw     r4, 8(r6)
167         stw     r4, 12(r6)
168         stw     r4, 16(r6)
169 #if CACHE_LINE_SIZE >= 32
170         stw     r4, 20(r6)
171         stw     r4, 24(r6)
172         stw     r4, 28(r6)
173         stw     r4, 32(r6)
174 #endif /* CACHE_LINE_SIZE */
175 #endif
176         addi    r6,r6,CACHELINE_BYTES
177         bdnz    10b
178         clrlwi  r5,r8,32-LG_CACHELINE_BYTES
179         addi    r5,r5,4
180 2:      srwi    r0,r5,2
181         mtctr   r0
182         bdz     6f
183 1:      stwu    r4,4(r6)
184         bdnz    1b
185 6:      andi.   r5,r5,3
186 7:      cmpwi   0,r5,0
187         beqlr
188         mtctr   r5
189         addi    r6,r6,3
190 8:      stbu    r4,1(r6)
191         bdnz    8b
192         blr
193
194 _GLOBAL(memset)
195         rlwimi  r4,r4,8,16,23
196         rlwimi  r4,r4,16,0,15
197         addi    r6,r3,-4
198         cmplwi  0,r5,4
199         blt     7f
200         stwu    r4,4(r6)
201         beqlr
202         andi.   r0,r6,3
203         add     r5,r0,r5
204         subf    r6,r0,r6
205         srwi    r0,r5,2
206         mtctr   r0
207         bdz     6f
208 1:      stwu    r4,4(r6)
209         bdnz    1b
210 6:      andi.   r5,r5,3
211 7:      cmpwi   0,r5,0
212         beqlr
213         mtctr   r5
214         addi    r6,r6,3
215 8:      stbu    r4,1(r6)
216         bdnz    8b
217         blr
218
219 _GLOBAL(bcopy)
220         mr      r6,r3
221         mr      r3,r4
222         mr      r4,r6
223         b       memcpy
224
225 /*
226  * This version uses dcbz on the complete cache lines in the
227  * destination area to reduce memory traffic.  This requires that
228  * the destination area is cacheable.
229  * We only use this version if the source and dest don't overlap.
230  * -- paulus.
231  */
232 _GLOBAL(cacheable_memcpy)
233         add     r7,r3,r5                /* test if the src & dst overlap */
234         add     r8,r4,r5
235         cmplw   0,r4,r7
236         cmplw   1,r3,r8
237         crand   0,0,4                   /* cr0.lt &= cr1.lt */
238         blt     memcpy                  /* if regions overlap */
239
240         addi    r4,r4,-4
241         addi    r6,r3,-4
242         neg     r0,r3
243         andi.   r0,r0,CACHELINE_MASK    /* # bytes to start of cache line */
244         beq     58f
245
246         cmplw   0,r5,r0                 /* is this more than total to do? */
247         blt     63f                     /* if not much to do */
248         andi.   r8,r0,3                 /* get it word-aligned first */
249         subf    r5,r0,r5
250         mtctr   r8
251         beq+    61f
252 70:     lbz     r9,4(r4)                /* do some bytes */
253         stb     r9,4(r6)
254         addi    r4,r4,1
255         addi    r6,r6,1
256         bdnz    70b
257 61:     srwi.   r0,r0,2
258         mtctr   r0
259         beq     58f
260 72:     lwzu    r9,4(r4)                /* do some words */
261         stwu    r9,4(r6)
262         bdnz    72b
263
264 58:     srwi.   r0,r5,LG_CACHELINE_BYTES /* # complete cachelines */
265         clrlwi  r5,r5,32-LG_CACHELINE_BYTES
266         li      r11,4
267         mtctr   r0
268         beq     63f
269 53:
270 #if !defined(CONFIG_8xx)
271         dcbz    r11,r6
272 #endif
273         COPY_16_BYTES
274 #if L1_CACHE_LINE_SIZE >= 32
275         COPY_16_BYTES
276 #if L1_CACHE_LINE_SIZE >= 64
277         COPY_16_BYTES
278         COPY_16_BYTES
279 #if L1_CACHE_LINE_SIZE >= 128
280         COPY_16_BYTES
281         COPY_16_BYTES
282         COPY_16_BYTES
283         COPY_16_BYTES
284 #endif
285 #endif
286 #endif
287         bdnz    53b
288
289 63:     srwi.   r0,r5,2
290         mtctr   r0
291         beq     64f
292 30:     lwzu    r0,4(r4)
293         stwu    r0,4(r6)
294         bdnz    30b
295
296 64:     andi.   r0,r5,3
297         mtctr   r0
298         beq+    65f
299 40:     lbz     r0,4(r4)
300         stb     r0,4(r6)
301         addi    r4,r4,1
302         addi    r6,r6,1
303         bdnz    40b
304 65:     blr
305
306 _GLOBAL(memmove)
307         cmplw   0,r3,r4
308         bgt     backwards_memcpy
309         /* fall through */
310
311 _GLOBAL(memcpy)
312         srwi.   r7,r5,3
313         addi    r6,r3,-4
314         addi    r4,r4,-4
315         beq     2f                      /* if less than 8 bytes to do */
316         andi.   r0,r6,3                 /* get dest word aligned */
317         mtctr   r7
318         bne     5f
319 1:      lwz     r7,4(r4)
320         lwzu    r8,8(r4)
321         stw     r7,4(r6)
322         stwu    r8,8(r6)
323         bdnz    1b
324         andi.   r5,r5,7
325 2:      cmplwi  0,r5,4
326         blt     3f
327         lwzu    r0,4(r4)
328         addi    r5,r5,-4
329         stwu    r0,4(r6)
330 3:      cmpwi   0,r5,0
331         beqlr
332         mtctr   r5
333         addi    r4,r4,3
334         addi    r6,r6,3
335 4:      lbzu    r0,1(r4)
336         stbu    r0,1(r6)
337         bdnz    4b
338         blr
339 5:      subfic  r0,r0,4
340         mtctr   r0
341 6:      lbz     r7,4(r4)
342         addi    r4,r4,1
343         stb     r7,4(r6)
344         addi    r6,r6,1
345         bdnz    6b
346         subf    r5,r0,r5
347         rlwinm. r7,r5,32-3,3,31
348         beq     2b
349         mtctr   r7
350         b       1b
351
352 _GLOBAL(backwards_memcpy)
353         rlwinm. r7,r5,32-3,3,31         /* r0 = r5 >> 3 */
354         add     r6,r3,r5
355         add     r4,r4,r5
356         beq     2f
357         andi.   r0,r6,3
358         mtctr   r7
359         bne     5f
360 1:      lwz     r7,-4(r4)
361         lwzu    r8,-8(r4)
362         stw     r7,-4(r6)
363         stwu    r8,-8(r6)
364         bdnz    1b
365         andi.   r5,r5,7
366 2:      cmplwi  0,r5,4
367         blt     3f
368         lwzu    r0,-4(r4)
369         subi    r5,r5,4
370         stwu    r0,-4(r6)
371 3:      cmpwi   0,r5,0
372         beqlr
373         mtctr   r5
374 4:      lbzu    r0,-1(r4)
375         stbu    r0,-1(r6)
376         bdnz    4b
377         blr
378 5:      mtctr   r0
379 6:      lbzu    r7,-1(r4)
380         stbu    r7,-1(r6)
381         bdnz    6b
382         subf    r5,r0,r5
383         rlwinm. r7,r5,32-3,3,31
384         beq     2b
385         mtctr   r7
386         b       1b
387
388 _GLOBAL(memcmp)
389         cmpwi   0,r5,0
390         ble-    2f
391         mtctr   r5
392         addi    r6,r3,-1
393         addi    r4,r4,-1
394 1:      lbzu    r3,1(r6)
395         lbzu    r0,1(r4)
396         subf.   r3,r0,r3
397         bdnzt   2,1b
398         blr
399 2:      li      r3,0
400         blr
401
402 _GLOBAL(memchr)
403         cmpwi   0,r5,0
404         ble-    2f
405         mtctr   r5
406         addi    r3,r3,-1
407 1:      lbzu    r0,1(r3)
408         cmpw    0,r0,r4
409         bdnzf   2,1b
410         beqlr
411 2:      li      r3,0
412         blr
413
414 _GLOBAL(__copy_tofrom_user)
415         addi    r4,r4,-4
416         addi    r6,r3,-4
417         neg     r0,r3
418         andi.   r0,r0,CACHELINE_MASK    /* # bytes to start of cache line */
419         beq     58f
420
421         cmplw   0,r5,r0                 /* is this more than total to do? */
422         blt     63f                     /* if not much to do */
423         andi.   r8,r0,3                 /* get it word-aligned first */
424         mtctr   r8
425         beq+    61f
426 70:     lbz     r9,4(r4)                /* do some bytes */
427 71:     stb     r9,4(r6)
428         addi    r4,r4,1
429         addi    r6,r6,1
430         bdnz    70b
431 61:     subf    r5,r0,r5
432         srwi.   r0,r0,2
433         mtctr   r0
434         beq     58f
435 72:     lwzu    r9,4(r4)                /* do some words */
436 73:     stwu    r9,4(r6)
437         bdnz    72b
438
439 58:     srwi.   r0,r5,LG_CACHELINE_BYTES /* # complete cachelines */
440         clrlwi  r5,r5,32-LG_CACHELINE_BYTES
441         li      r11,4
442         beq     63f
443
444 #if !defined(CONFIG_8xx)
445         /* Here we decide how far ahead to prefetch the source */
446 #if MAX_COPY_PREFETCH > 1
447         /* Heuristically, for large transfers we prefetch
448            MAX_COPY_PREFETCH cachelines ahead.  For small transfers
449            we prefetch 1 cacheline ahead. */
450         cmpwi   r0,MAX_COPY_PREFETCH
451         li      r7,1
452         li      r3,4
453         ble     111f
454         li      r7,MAX_COPY_PREFETCH
455 111:    mtctr   r7
456 112:    dcbt    r3,r4
457         addi    r3,r3,CACHELINE_BYTES
458         bdnz    112b
459 #else /* MAX_COPY_PREFETCH == 1 */
460         li      r3,CACHELINE_BYTES + 4
461         dcbt    r11,r4
462 #endif /* MAX_COPY_PREFETCH */
463 #endif /* CONFIG_8xx */
464
465         mtctr   r0
466 53:
467 #if !defined(CONFIG_8xx)
468         dcbt    r3,r4
469 54:     dcbz    r11,r6
470 #endif
471 /* had to move these to keep extable in order */
472         .section __ex_table,"a"
473         .align  2
474         .long   70b,100f
475         .long   71b,101f
476         .long   72b,102f
477         .long   73b,103f
478 #if !defined(CONFIG_8xx)
479         .long   54b,105f
480 #endif
481         .text
482 /* the main body of the cacheline loop */
483         COPY_16_BYTES_WITHEX(0)
484 #if L1_CACHE_LINE_SIZE >= 32
485         COPY_16_BYTES_WITHEX(1)
486 #if L1_CACHE_LINE_SIZE >= 64
487         COPY_16_BYTES_WITHEX(2)
488         COPY_16_BYTES_WITHEX(3)
489 #if L1_CACHE_LINE_SIZE >= 128
490         COPY_16_BYTES_WITHEX(4)
491         COPY_16_BYTES_WITHEX(5)
492         COPY_16_BYTES_WITHEX(6)
493         COPY_16_BYTES_WITHEX(7)
494 #endif
495 #endif
496 #endif
497         bdnz    53b
498
499 63:     srwi.   r0,r5,2
500         mtctr   r0
501         beq     64f
502 30:     lwzu    r0,4(r4)
503 31:     stwu    r0,4(r6)
504         bdnz    30b
505
506 64:     andi.   r0,r5,3
507         mtctr   r0
508         beq+    65f
509 40:     lbz     r0,4(r4)
510 41:     stb     r0,4(r6)
511         addi    r4,r4,1
512         addi    r6,r6,1
513         bdnz    40b
514 65:     li      r3,0
515         blr
516
517 /* read fault, initial single-byte copy */
518 100:    li      r9,0
519         b       90f
520 /* write fault, initial single-byte copy */
521 101:    li      r9,1
522 90:     subf    r5,r8,r5
523         li      r3,0
524         b       99f
525 /* read fault, initial word copy */
526 102:    li      r9,0
527         b       91f
528 /* write fault, initial word copy */
529 103:    li      r9,1
530 91:     li      r3,2
531         b       99f
532
533 /*
534  * this stuff handles faults in the cacheline loop and branches to either
535  * 104f (if in read part) or 105f (if in write part), after updating r5
536  */
537         COPY_16_BYTES_EXCODE(0)
538 #if L1_CACHE_LINE_SIZE >= 32
539         COPY_16_BYTES_EXCODE(1)
540 #if L1_CACHE_LINE_SIZE >= 64
541         COPY_16_BYTES_EXCODE(2)
542         COPY_16_BYTES_EXCODE(3)
543 #if L1_CACHE_LINE_SIZE >= 128
544         COPY_16_BYTES_EXCODE(4)
545         COPY_16_BYTES_EXCODE(5)
546         COPY_16_BYTES_EXCODE(6)
547         COPY_16_BYTES_EXCODE(7)
548 #endif
549 #endif
550 #endif
551
552 /* read fault in cacheline loop */
553 104:    li      r9,0
554         b       92f
555 /* fault on dcbz (effectively a write fault) */
556 /* or write fault in cacheline loop */
557 105:    li      r9,1
558 92:     li      r3,LG_CACHELINE_BYTES
559         b       99f
560 /* read fault in final word loop */
561 108:    li      r9,0
562         b       93f
563 /* write fault in final word loop */
564 109:    li      r9,1
565 93:     andi.   r5,r5,3
566         li      r3,2
567         b       99f
568 /* read fault in final byte loop */
569 110:    li      r9,0
570         b       94f
571 /* write fault in final byte loop */
572 111:    li      r9,1
573 94:     li      r5,0
574         li      r3,0
575 /*
576  * At this stage the number of bytes not copied is
577  * r5 + (ctr << r3), and r9 is 0 for read or 1 for write.
578  */
579 99:     mfctr   r0
580         slw     r3,r0,r3
581         add.    r3,r3,r5
582         beq     120f                    /* shouldn't happen */
583         cmpwi   0,r9,0
584         bne     120f
585 /* for a read fault, first try to continue the copy one byte at a time */
586         mtctr   r3
587 130:    lbz     r0,4(r4)
588 131:    stb     r0,4(r6)
589         addi    r4,r4,1
590         addi    r6,r6,1
591         bdnz    130b
592 /* then clear out the destination: r3 bytes starting at 4(r6) */
593 132:    mfctr   r3
594         srwi.   r0,r3,2
595         li      r9,0
596         mtctr   r0
597         beq     113f
598 112:    stwu    r9,4(r6)
599         bdnz    112b
600 113:    andi.   r0,r3,3
601         mtctr   r0
602         beq     120f
603 114:    stb     r9,4(r6)
604         addi    r6,r6,1
605         bdnz    114b
606 120:    blr
607
608         .section __ex_table,"a"
609         .align  2
610         .long   30b,108b
611         .long   31b,109b
612         .long   40b,110b
613         .long   41b,111b
614         .long   130b,132b
615         .long   131b,120b
616         .long   112b,120b
617         .long   114b,120b
618         .text
619
620 _GLOBAL(__clear_user)
621         addi    r6,r3,-4
622         li      r3,0
623         li      r5,0
624         cmplwi  0,r4,4
625         blt     7f
626         /* clear a single word */
627 11:     stwu    r5,4(r6)
628         beqlr
629         /* clear word sized chunks */
630         andi.   r0,r6,3
631         add     r4,r0,r4
632         subf    r6,r0,r6
633         srwi    r0,r4,2
634         andi.   r4,r4,3
635         mtctr   r0
636         bdz     7f
637 1:      stwu    r5,4(r6)
638         bdnz    1b
639         /* clear byte sized chunks */
640 7:      cmpwi   0,r4,0
641         beqlr
642         mtctr   r4
643         addi    r6,r6,3
644 8:      stbu    r5,1(r6)
645         bdnz    8b
646         blr
647 90:     mr      r3,r4
648         blr
649 91:     mfctr   r3
650         slwi    r3,r3,2
651         add     r3,r3,r4
652         blr
653 92:     mfctr   r3
654         blr
655
656         .section __ex_table,"a"
657         .align  2
658         .long   11b,90b
659         .long   1b,91b
660         .long   8b,92b
661         .text
662
663 _GLOBAL(__strncpy_from_user)
664         addi    r6,r3,-1
665         addi    r4,r4,-1
666         cmpwi   0,r5,0
667         beq     2f
668         mtctr   r5
669 1:      lbzu    r0,1(r4)
670         cmpwi   0,r0,0
671         stbu    r0,1(r6)
672         bdnzf   2,1b            /* dec ctr, branch if ctr != 0 && !cr0.eq */
673         beq     3f
674 2:      addi    r6,r6,1
675 3:      subf    r3,r3,r6
676         blr
677 99:     li      r3,-EFAULT
678         blr
679
680         .section __ex_table,"a"
681         .align  2
682         .long   1b,99b
683         .text
684
685 /* r3 = str, r4 = len (> 0), r5 = top (highest addr) */
686 _GLOBAL(__strnlen_user)
687         addi    r7,r3,-1
688         subf    r6,r7,r5        /* top+1 - str */
689         cmplw   0,r4,r6
690         bge     0f
691         mr      r6,r4
692 0:      mtctr   r6              /* ctr = min(len, top - str) */
693 1:      lbzu    r0,1(r7)        /* get next byte */
694         cmpwi   0,r0,0
695         bdnzf   2,1b            /* loop if --ctr != 0 && byte != 0 */
696         addi    r7,r7,1
697         subf    r3,r3,r7        /* number of bytes we have looked at */
698         beqlr                   /* return if we found a 0 byte */
699         cmpw    0,r3,r4         /* did we look at all len bytes? */
700         blt     99f             /* if not, must have hit top */
701         addi    r3,r4,1         /* return len + 1 to indicate no null found */
702         blr
703 99:     li      r3,0            /* bad address, return 0 */
704         blr
705
706         .section __ex_table,"a"
707         .align  2
708         .long   1b,99b