| Adhemerval Zanella | 80c535b | 2019-08-06 14:14:22 -0300 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2008 ARM Ltd |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. The name of the company may not be used to endorse or promote |
| 14 | * products derived from this software without specific prior written |
| 15 | * permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 | * IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 22 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 23 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 24 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 25 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | /* For GLIBC: |
| 30 | #include <string.h> |
| 31 | #include <memcopy.h> |
| 32 | |
| 33 | #undef strcmp |
| 34 | */ |
| 35 | |
| 36 | #ifdef __thumb2__ |
| 37 | #define magic1(REG) "#0x01010101" |
| 38 | #define magic2(REG) "#0x80808080" |
| 39 | #else |
| 40 | #define magic1(REG) #REG |
| 41 | #define magic2(REG) #REG ", lsl #7" |
| 42 | #endif |
| 43 | |
| 44 | char* __attribute__((naked)) |
| 45 | __strcpy_arm (char* dst, const char* src) |
| 46 | { |
| 47 | __asm__ ( |
| 48 | "pld [r1, #0]\n\t" |
| 49 | "eor r2, r0, r1\n\t" |
| 50 | "mov ip, r0\n\t" |
| 51 | "tst r2, #3\n\t" |
| 52 | "bne 4f\n\t" |
| 53 | "tst r1, #3\n\t" |
| 54 | "bne 3f\n" |
| 55 | "5:\n\t" |
| 56 | # ifndef __thumb2__ |
| 57 | "str r5, [sp, #-4]!\n\t" |
| 58 | "mov r5, #0x01\n\t" |
| 59 | "orr r5, r5, r5, lsl #8\n\t" |
| 60 | "orr r5, r5, r5, lsl #16\n\t" |
| 61 | # endif |
| 62 | |
| 63 | "str r4, [sp, #-4]!\n\t" |
| 64 | "tst r1, #4\n\t" |
| 65 | "ldr r3, [r1], #4\n\t" |
| 66 | "beq 2f\n\t" |
| 67 | "sub r2, r3, "magic1(r5)"\n\t" |
| 68 | "bics r2, r2, r3\n\t" |
| 69 | "tst r2, "magic2(r5)"\n\t" |
| 70 | "itt eq\n\t" |
| 71 | "streq r3, [ip], #4\n\t" |
| 72 | "ldreq r3, [r1], #4\n" |
| 73 | "bne 1f\n\t" |
| 74 | /* Inner loop. We now know that r1 is 64-bit aligned, so we |
| 75 | can safely fetch up to two words. This allows us to avoid |
| 76 | load stalls. */ |
| 77 | ".p2align 2\n" |
| 78 | "2:\n\t" |
| 79 | "pld [r1, #8]\n\t" |
| 80 | "ldr r4, [r1], #4\n\t" |
| 81 | "sub r2, r3, "magic1(r5)"\n\t" |
| 82 | "bics r2, r2, r3\n\t" |
| 83 | "tst r2, "magic2(r5)"\n\t" |
| 84 | "sub r2, r4, "magic1(r5)"\n\t" |
| 85 | "bne 1f\n\t" |
| 86 | "str r3, [ip], #4\n\t" |
| 87 | "bics r2, r2, r4\n\t" |
| 88 | "tst r2, "magic2(r5)"\n\t" |
| 89 | "itt eq\n\t" |
| 90 | "ldreq r3, [r1], #4\n\t" |
| 91 | "streq r4, [ip], #4\n\t" |
| 92 | "beq 2b\n\t" |
| 93 | "mov r3, r4\n" |
| 94 | "1:\n\t" |
| 95 | # ifdef __ARMEB__ |
| 96 | "rors r3, r3, #24\n\t" |
| 97 | # endif |
| 98 | "strb r3, [ip], #1\n\t" |
| 99 | "tst r3, #0xff\n\t" |
| 100 | # ifdef __ARMEL__ |
| 101 | "ror r3, r3, #8\n\t" |
| 102 | # endif |
| 103 | "bne 1b\n\t" |
| 104 | "ldr r4, [sp], #4\n\t" |
| 105 | # ifndef __thumb2__ |
| 106 | "ldr r5, [sp], #4\n\t" |
| 107 | # endif |
| 108 | "BX LR\n" |
| 109 | |
| 110 | /* Strings have the same offset from word alignment, but it's |
| 111 | not zero. */ |
| 112 | "3:\n\t" |
| 113 | "tst r1, #1\n\t" |
| 114 | "beq 1f\n\t" |
| 115 | "ldrb r2, [r1], #1\n\t" |
| 116 | "strb r2, [ip], #1\n\t" |
| 117 | "cmp r2, #0\n\t" |
| 118 | "it eq\n" |
| 119 | "BXEQ LR\n" |
| 120 | "1:\n\t" |
| 121 | "tst r1, #2\n\t" |
| 122 | "beq 5b\n\t" |
| 123 | "ldrh r2, [r1], #2\n\t" |
| 124 | # ifdef __ARMEB__ |
| 125 | "tst r2, #0xff00\n\t" |
| 126 | "iteet ne\n\t" |
| 127 | "strneh r2, [ip], #2\n\t" |
| 128 | "lsreq r2, r2, #8\n\t" |
| 129 | "streqb r2, [ip]\n\t" |
| 130 | "tstne r2, #0xff\n\t" |
| 131 | # else |
| 132 | "tst r2, #0xff\n\t" |
| 133 | "itet ne\n\t" |
| 134 | "strneh r2, [ip], #2\n\t" |
| 135 | "streqb r2, [ip]\n\t" |
| 136 | "tstne r2, #0xff00\n\t" |
| 137 | # endif |
| 138 | "bne 5b\n\t" |
| 139 | "BX LR\n" |
| 140 | |
| 141 | /* src and dst do not have a common word-alignement. Fall back to |
| 142 | byte copying. */ |
| 143 | "4:\n\t" |
| 144 | "ldrb r2, [r1], #1\n\t" |
| 145 | "strb r2, [ip], #1\n\t" |
| 146 | "cmp r2, #0\n\t" |
| 147 | "bne 4b\n\t" |
| 148 | "BX LR"); |
| 149 | } |
| 150 | /* For GLIBC: libc_hidden_builtin_def (strcpy) */ |