blob: f3ae9f3296a8a5ecc63105748933a04f19b51199 [file] [log] [blame]
Christopher Ferris7c83a1e2013-02-26 01:30:00 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
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 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29/* Assumes neon instructions and a cache line size of 32 bytes. */
30
31#include <machine/cpu-features.h>
32#include <machine/asm.h>
Christopher Ferris59a13c12013-08-01 13:13:33 -070033#include "libc_events.h"
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080034
35/*
36 * This code assumes it is running on a processor that supports all arm v7
37 * instructions, that supports neon instructions, and that has a 32 byte
38 * cache line.
39 */
40
Christopher Ferris59a13c12013-08-01 13:13:33 -070041#define CACHE_LINE_SIZE 32
42
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080043 .text
44 .fpu neon
45
Christopher Ferris59a13c12013-08-01 13:13:33 -070046ENTRY(__memcpy_chk)
47 cmp r2, r3
48 bgt fortify_check_failed
49
50 // Fall through to memcpy...
51END(__memcpy_chk)
Christopher Ferris7c83a1e2013-02-26 01:30:00 -080052
53ENTRY(memcpy)
54 .save {r0, lr}
55 /* start preloading as early as possible */
56 pld [r1, #(CACHE_LINE_SIZE*0)]
57 stmfd sp!, {r0, lr}
58 pld [r1, #(CACHE_LINE_SIZE*2)]
59
60 /* do we have at least 16-bytes to copy (needed for alignment below) */
61 cmp r2, #16
62 blo 5f
63
64 /* align destination to cache-line for the write-buffer */
65 rsb r3, r0, #0
66 ands r3, r3, #0xF
67 beq 0f
68
69 /* copy up to 15-bytes (count in r3) */
70 sub r2, r2, r3
71 movs ip, r3, lsl #31
72 ldrmib lr, [r1], #1
73 strmib lr, [r0], #1
74 ldrcsb ip, [r1], #1
75 ldrcsb lr, [r1], #1
76 strcsb ip, [r0], #1
77 strcsb lr, [r0], #1
78 movs ip, r3, lsl #29
79 bge 1f
80 // copies 4 bytes, destination 32-bits aligned
81 vld4.8 {d0[0], d1[0], d2[0], d3[0]}, [r1]!
82 vst4.8 {d0[0], d1[0], d2[0], d3[0]}, [r0, :32]!
831: bcc 2f
84 // copies 8 bytes, destination 64-bits aligned
85 vld1.8 {d0}, [r1]!
86 vst1.8 {d0}, [r0, :64]!
872:
88
890: /* preload immediately the next cache line, which we may need */
90 pld [r1, #(CACHE_LINE_SIZE*0)]
91 pld [r1, #(CACHE_LINE_SIZE*2)]
92
93 /* make sure we have at least 64 bytes to copy */
94 subs r2, r2, #64
95 blo 2f
96
97 /* Preload all the cache lines we need.
98 * NOTE: The number of pld below depends on CACHE_LINE_SIZE,
99 * ideally we would increase the distance in the main loop to
100 * avoid the goofy code below. In practice this doesn't seem to make
101 * a big difference.
102 * NOTE: The value CACHE_LINE_SIZE * 8 was chosen through
103 * experimentation.
104 */
105 pld [r1, #(CACHE_LINE_SIZE*4)]
106 pld [r1, #(CACHE_LINE_SIZE*6)]
107 pld [r1, #(CACHE_LINE_SIZE*8)]
108
1091: /* The main loop copies 64 bytes at a time */
110 vld1.8 {d0 - d3}, [r1]!
111 vld1.8 {d4 - d7}, [r1]!
112 pld [r1, #(CACHE_LINE_SIZE*8)]
113 subs r2, r2, #64
114 vst1.8 {d0 - d3}, [r0, :128]!
115 vst1.8 {d4 - d7}, [r0, :128]!
116 bhs 1b
117
1182: /* fix-up the remaining count and make sure we have >= 32 bytes left */
119 add r2, r2, #64
120 subs r2, r2, #32
121 blo 4f
122
1233: /* 32 bytes at a time. These cache lines were already preloaded */
124 vld1.8 {d0 - d3}, [r1]!
125 subs r2, r2, #32
126 vst1.8 {d0 - d3}, [r0, :128]!
127 bhs 3b
1284: /* less than 32 left */
129 add r2, r2, #32
130 tst r2, #0x10
131 beq 5f
132 // copies 16 bytes, 128-bits aligned
133 vld1.8 {d0, d1}, [r1]!
134 vst1.8 {d0, d1}, [r0, :128]!
135
1365: /* copy up to 15-bytes (count in r2) */
137 movs ip, r2, lsl #29
138 bcc 1f
139 vld1.8 {d0}, [r1]!
140 vst1.8 {d0}, [r0]!
1411: bge 2f
142 vld4.8 {d0[0], d1[0], d2[0], d3[0]}, [r1]!
143 vst4.8 {d0[0], d1[0], d2[0], d3[0]}, [r0]!
1442: movs ip, r2, lsl #31
145 ldrmib r3, [r1], #1
146 ldrcsb ip, [r1], #1
147 ldrcsb lr, [r1], #1
148 strmib r3, [r0], #1
149 strcsb ip, [r0], #1
150 strcsb lr, [r0], #1
151
152 ldmfd sp!, {r0, lr}
153 bx lr
Christopher Ferris59a13c12013-08-01 13:13:33 -0700154
155 // Only reached when the __memcpy_chk check fails.
156fortify_check_failed:
157 ldr r0, error_message
158 ldr r1, error_code
1591:
160 add r0, pc
161 bl __fortify_chk_fail
162error_code:
163 .word BIONIC_EVENT_MEMCPY_BUFFER_OVERFLOW
164error_message:
165 .word error_string-(1b+8)
Christopher Ferris7c83a1e2013-02-26 01:30:00 -0800166END(memcpy)
Christopher Ferris59a13c12013-08-01 13:13:33 -0700167
168 .data
169error_string:
170 .string "memcpy buffer overflow"