07061b0bbd556802700925ce96c4886193dd37cc
[openwrt/staging/yousong.git] / target / linux / generic / patches-3.8 / 001-ARM-7668-1-fix-memset-related-crashes-caused-by-rece.patch
1 From 455bd4c430b0c0a361f38e8658a0d6cb469942b5 Mon Sep 17 00:00:00 2001
2 From: Ivan Djelic <ivan.djelic@parrot.com>
3 Date: Wed, 6 Mar 2013 20:09:27 +0100
4 Subject: [PATCH] ARM: 7668/1: fix memset-related crashes caused by recent GCC
5 (4.7.2) optimizations
6
7 Recent GCC versions (e.g. GCC-4.7.2) perform optimizations based on
8 assumptions about the implementation of memset and similar functions.
9 The current ARM optimized memset code does not return the value of
10 its first argument, as is usually expected from standard implementations.
11
12 For instance in the following function:
13
14 void debug_mutex_lock_common(struct mutex *lock, struct mutex_waiter *waiter)
15 {
16 memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter));
17 waiter->magic = waiter;
18 INIT_LIST_HEAD(&waiter->list);
19 }
20
21 compiled as:
22
23 800554d0 <debug_mutex_lock_common>:
24 800554d0: e92d4008 push {r3, lr}
25 800554d4: e1a00001 mov r0, r1
26 800554d8: e3a02010 mov r2, #16 ; 0x10
27 800554dc: e3a01011 mov r1, #17 ; 0x11
28 800554e0: eb04426e bl 80165ea0 <memset>
29 800554e4: e1a03000 mov r3, r0
30 800554e8: e583000c str r0, [r3, #12]
31 800554ec: e5830000 str r0, [r3]
32 800554f0: e5830004 str r0, [r3, #4]
33 800554f4: e8bd8008 pop {r3, pc}
34
35 GCC assumes memset returns the value of pointer 'waiter' in register r0; causing
36 register/memory corruptions.
37
38 This patch fixes the return value of the assembly version of memset.
39 It adds a 'mov' instruction and merges an additional load+store into
40 existing load/store instructions.
41 For ease of review, here is a breakdown of the patch into 4 simple steps:
42
43 Step 1
44 ======
45 Perform the following substitutions:
46 ip -> r8, then
47 r0 -> ip,
48 and insert 'mov ip, r0' as the first statement of the function.
49 At this point, we have a memset() implementation returning the proper result,
50 but corrupting r8 on some paths (the ones that were using ip).
51
52 Step 2
53 ======
54 Make sure r8 is saved and restored when (! CALGN(1)+0) == 1:
55
56 save r8:
57 - str lr, [sp, #-4]!
58 + stmfd sp!, {r8, lr}
59
60 and restore r8 on both exit paths:
61 - ldmeqfd sp!, {pc} @ Now <64 bytes to go.
62 + ldmeqfd sp!, {r8, pc} @ Now <64 bytes to go.
63 (...)
64 tst r2, #16
65 stmneia ip!, {r1, r3, r8, lr}
66 - ldr lr, [sp], #4
67 + ldmfd sp!, {r8, lr}
68
69 Step 3
70 ======
71 Make sure r8 is saved and restored when (! CALGN(1)+0) == 0:
72
73 save r8:
74 - stmfd sp!, {r4-r7, lr}
75 + stmfd sp!, {r4-r8, lr}
76
77 and restore r8 on both exit paths:
78 bgt 3b
79 - ldmeqfd sp!, {r4-r7, pc}
80 + ldmeqfd sp!, {r4-r8, pc}
81 (...)
82 tst r2, #16
83 stmneia ip!, {r4-r7}
84 - ldmfd sp!, {r4-r7, lr}
85 + ldmfd sp!, {r4-r8, lr}
86
87 Step 4
88 ======
89 Rewrite register list "r4-r7, r8" as "r4-r8".
90
91 Signed-off-by: Ivan Djelic <ivan.djelic@parrot.com>
92 Reviewed-by: Nicolas Pitre <nico@linaro.org>
93 Signed-off-by: Dirk Behme <dirk.behme@gmail.com>
94 Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
95 ---
96 arch/arm/lib/memset.S | 85 ++++++++++++++++++++++++++-------------------------
97 1 file changed, 44 insertions(+), 41 deletions(-)
98
99 --- a/arch/arm/lib/memset.S
100 +++ b/arch/arm/lib/memset.S
101 @@ -19,9 +19,9 @@
102 1: subs r2, r2, #4 @ 1 do we have enough
103 blt 5f @ 1 bytes to align with?
104 cmp r3, #2 @ 1
105 - strltb r1, [r0], #1 @ 1
106 - strleb r1, [r0], #1 @ 1
107 - strb r1, [r0], #1 @ 1
108 + strltb r1, [ip], #1 @ 1
109 + strleb r1, [ip], #1 @ 1
110 + strb r1, [ip], #1 @ 1
111 add r2, r2, r3 @ 1 (r2 = r2 - (4 - r3))
112 /*
113 * The pointer is now aligned and the length is adjusted. Try doing the
114 @@ -29,10 +29,14 @@
115 */
116
117 ENTRY(memset)
118 - ands r3, r0, #3 @ 1 unaligned?
119 +/*
120 + * Preserve the contents of r0 for the return value.
121 + */
122 + mov ip, r0
123 + ands r3, ip, #3 @ 1 unaligned?
124 bne 1b @ 1
125 /*
126 - * we know that the pointer in r0 is aligned to a word boundary.
127 + * we know that the pointer in ip is aligned to a word boundary.
128 */
129 orr r1, r1, r1, lsl #8
130 orr r1, r1, r1, lsl #16
131 @@ -43,29 +47,28 @@ ENTRY(memset)
132 #if ! CALGN(1)+0
133
134 /*
135 - * We need an extra register for this loop - save the return address and
136 - * use the LR
137 + * We need 2 extra registers for this loop - use r8 and the LR
138 */
139 - str lr, [sp, #-4]!
140 - mov ip, r1
141 + stmfd sp!, {r8, lr}
142 + mov r8, r1
143 mov lr, r1
144
145 2: subs r2, r2, #64
146 - stmgeia r0!, {r1, r3, ip, lr} @ 64 bytes at a time.
147 - stmgeia r0!, {r1, r3, ip, lr}
148 - stmgeia r0!, {r1, r3, ip, lr}
149 - stmgeia r0!, {r1, r3, ip, lr}
150 + stmgeia ip!, {r1, r3, r8, lr} @ 64 bytes at a time.
151 + stmgeia ip!, {r1, r3, r8, lr}
152 + stmgeia ip!, {r1, r3, r8, lr}
153 + stmgeia ip!, {r1, r3, r8, lr}
154 bgt 2b
155 - ldmeqfd sp!, {pc} @ Now <64 bytes to go.
156 + ldmeqfd sp!, {r8, pc} @ Now <64 bytes to go.
157 /*
158 * No need to correct the count; we're only testing bits from now on
159 */
160 tst r2, #32
161 - stmneia r0!, {r1, r3, ip, lr}
162 - stmneia r0!, {r1, r3, ip, lr}
163 + stmneia ip!, {r1, r3, r8, lr}
164 + stmneia ip!, {r1, r3, r8, lr}
165 tst r2, #16
166 - stmneia r0!, {r1, r3, ip, lr}
167 - ldr lr, [sp], #4
168 + stmneia ip!, {r1, r3, r8, lr}
169 + ldmfd sp!, {r8, lr}
170
171 #else
172
173 @@ -74,54 +77,54 @@ ENTRY(memset)
174 * whole cache lines at once.
175 */
176
177 - stmfd sp!, {r4-r7, lr}
178 + stmfd sp!, {r4-r8, lr}
179 mov r4, r1
180 mov r5, r1
181 mov r6, r1
182 mov r7, r1
183 - mov ip, r1
184 + mov r8, r1
185 mov lr, r1
186
187 cmp r2, #96
188 - tstgt r0, #31
189 + tstgt ip, #31
190 ble 3f
191
192 - and ip, r0, #31
193 - rsb ip, ip, #32
194 - sub r2, r2, ip
195 - movs ip, ip, lsl #(32 - 4)
196 - stmcsia r0!, {r4, r5, r6, r7}
197 - stmmiia r0!, {r4, r5}
198 - tst ip, #(1 << 30)
199 - mov ip, r1
200 - strne r1, [r0], #4
201 + and r8, ip, #31
202 + rsb r8, r8, #32
203 + sub r2, r2, r8
204 + movs r8, r8, lsl #(32 - 4)
205 + stmcsia ip!, {r4, r5, r6, r7}
206 + stmmiia ip!, {r4, r5}
207 + tst r8, #(1 << 30)
208 + mov r8, r1
209 + strne r1, [ip], #4
210
211 3: subs r2, r2, #64
212 - stmgeia r0!, {r1, r3-r7, ip, lr}
213 - stmgeia r0!, {r1, r3-r7, ip, lr}
214 + stmgeia ip!, {r1, r3-r8, lr}
215 + stmgeia ip!, {r1, r3-r8, lr}
216 bgt 3b
217 - ldmeqfd sp!, {r4-r7, pc}
218 + ldmeqfd sp!, {r4-r8, pc}
219
220 tst r2, #32
221 - stmneia r0!, {r1, r3-r7, ip, lr}
222 + stmneia ip!, {r1, r3-r8, lr}
223 tst r2, #16
224 - stmneia r0!, {r4-r7}
225 - ldmfd sp!, {r4-r7, lr}
226 + stmneia ip!, {r4-r7}
227 + ldmfd sp!, {r4-r8, lr}
228
229 #endif
230
231 4: tst r2, #8
232 - stmneia r0!, {r1, r3}
233 + stmneia ip!, {r1, r3}
234 tst r2, #4
235 - strne r1, [r0], #4
236 + strne r1, [ip], #4
237 /*
238 * When we get here, we've got less than 4 bytes to zero. We
239 * may have an unaligned pointer as well.
240 */
241 5: tst r2, #2
242 - strneb r1, [r0], #1
243 - strneb r1, [r0], #1
244 + strneb r1, [ip], #1
245 + strneb r1, [ip], #1
246 tst r2, #1
247 - strneb r1, [r0], #1
248 + strneb r1, [ip], #1
249 mov pc, lr
250 ENDPROC(memset)