OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file is an internal atomic implementation, use base/atomicops.h instead. | 5 // This file is an internal atomic implementation, use base/atomicops.h instead. |
6 | 6 |
7 // TODO(rmcilroy): Investigate whether we can use __sync__ intrinsics instead of | 7 // TODO(rmcilroy): Investigate whether we can use __sync__ intrinsics instead of |
8 // the hand coded assembly without introducing perf regressions. | 8 // the hand coded assembly without introducing perf regressions. |
9 // TODO(rmcilroy): Investigate whether we can use acquire / release versions of | 9 // TODO(rmcilroy): Investigate whether we can use acquire / release versions of |
10 // exclusive load / store assembly instructions and do away with | 10 // exclusive load / store assembly instructions and do away with |
11 // the barriers. | 11 // the barriers. |
12 | 12 |
13 #ifndef BASE_ATOMICOPS_INTERNALS_ARM64_GCC_H_ | 13 #ifndef BASE_ATOMICOPS_INTERNALS_ARM64_GCC_H_ |
14 #define BASE_ATOMICOPS_INTERNALS_ARM64_GCC_H_ | 14 #define BASE_ATOMICOPS_INTERNALS_ARM64_GCC_H_ |
15 | 15 |
16 #if defined(OS_QNX) | 16 #if defined(OS_QNX) |
17 #include <sys/cpuinline.h> | 17 #include <sys/cpuinline.h> |
18 #endif | 18 #endif |
19 | 19 |
20 namespace base { | 20 namespace base { |
21 namespace subtle { | 21 namespace subtle { |
22 | 22 |
23 inline void MemoryBarrier() { | 23 inline void MemoryBarrier() { |
24 __asm__ __volatile__ ( // NOLINT | 24 __asm__ __volatile__ ("dmb ish" ::: "memory"); // NOLINT |
25 "dmb ish \n\t" // Data memory barrier. | |
26 ::: "memory" | |
27 ); // NOLINT | |
28 } | 25 } |
29 | 26 |
| 27 // NoBarrier versions of the operation include "memory" in the clobber list. |
| 28 // This is not required for direct usage of the NoBarrier versions of the |
| 29 // operations. However this is required for correctness when they are used as |
| 30 // part of the Acquire or Release versions, to ensure that nothing from outside |
| 31 // the call is reordered between the operation and the memory barrier. This does |
| 32 // not change the code generated, so has no or minimal impact on the |
| 33 // NoBarrier operations. |
30 | 34 |
31 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr, | 35 inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr, |
32 Atomic32 old_value, | 36 Atomic32 old_value, |
33 Atomic32 new_value) { | 37 Atomic32 new_value) { |
34 Atomic32 prev; | 38 Atomic32 prev; |
35 int32_t temp; | 39 int32_t temp; |
36 | 40 |
37 __asm__ __volatile__ ( // NOLINT | 41 __asm__ __volatile__ ( // NOLINT |
38 "0: \n\t" | 42 "0: \n\t" |
39 "ldxr %w[prev], %[ptr] \n\t" // Load the previous value. | 43 "ldxr %w[prev], %[ptr] \n\t" // Load the previous value. |
40 "cmp %w[prev], %w[old_value] \n\t" | 44 "cmp %w[prev], %w[old_value] \n\t" |
41 "bne 1f \n\t" | 45 "bne 1f \n\t" |
42 "stxr %w[temp], %w[new_value], %[ptr] \n\t" // Try to store the new value. | 46 "stxr %w[temp], %w[new_value], %[ptr] \n\t" // Try to store the new value. |
43 "cbnz %w[temp], 0b \n\t" // Retry if it did not work. | 47 "cbnz %w[temp], 0b \n\t" // Retry if it did not work. |
44 "1: \n\t" | 48 "1: \n\t" |
45 "clrex \n\t" // In case we didn't swap. | |
46 : [prev]"=&r" (prev), | 49 : [prev]"=&r" (prev), |
47 [temp]"=&r" (temp), | 50 [temp]"=&r" (temp), |
48 [ptr]"+Q" (*ptr) | 51 [ptr]"+Q" (*ptr) |
49 : [old_value]"r" (old_value), | 52 : [old_value]"IJr" (old_value), |
50 [new_value]"r" (new_value) | 53 [new_value]"r" (new_value) |
51 : "memory", "cc" | 54 : "cc", "memory" |
52 ); // NOLINT | 55 ); // NOLINT |
53 | 56 |
54 return prev; | 57 return prev; |
55 } | 58 } |
56 | 59 |
57 inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, | 60 inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, |
58 Atomic32 new_value) { | 61 Atomic32 new_value) { |
59 Atomic32 result; | 62 Atomic32 result; |
60 int32_t temp; | 63 int32_t temp; |
61 | 64 |
(...skipping 19 matching lines...) Expand all Loading... |
81 | 84 |
82 __asm__ __volatile__ ( // NOLINT | 85 __asm__ __volatile__ ( // NOLINT |
83 "0: \n\t" | 86 "0: \n\t" |
84 "ldxr %w[result], %[ptr] \n\t" // Load the previous value. | 87 "ldxr %w[result], %[ptr] \n\t" // Load the previous value. |
85 "add %w[result], %w[result], %w[increment]\n\t" | 88 "add %w[result], %w[result], %w[increment]\n\t" |
86 "stxr %w[temp], %w[result], %[ptr] \n\t" // Try to store the result. | 89 "stxr %w[temp], %w[result], %[ptr] \n\t" // Try to store the result. |
87 "cbnz %w[temp], 0b \n\t" // Retry on failure. | 90 "cbnz %w[temp], 0b \n\t" // Retry on failure. |
88 : [result]"=&r" (result), | 91 : [result]"=&r" (result), |
89 [temp]"=&r" (temp), | 92 [temp]"=&r" (temp), |
90 [ptr]"+Q" (*ptr) | 93 [ptr]"+Q" (*ptr) |
91 : [increment]"r" (increment) | 94 : [increment]"IJr" (increment) |
92 : "memory" | 95 : "memory" |
93 ); // NOLINT | 96 ); // NOLINT |
94 | 97 |
95 return result; | 98 return result; |
96 } | 99 } |
97 | 100 |
98 inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr, | 101 inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr, |
99 Atomic32 increment) { | 102 Atomic32 increment) { |
100 MemoryBarrier(); | 103 MemoryBarrier(); |
101 Atomic32 result = NoBarrier_AtomicIncrement(ptr, increment); | 104 Atomic32 result = NoBarrier_AtomicIncrement(ptr, increment); |
102 MemoryBarrier(); | 105 MemoryBarrier(); |
103 | 106 |
104 return result; | 107 return result; |
105 } | 108 } |
106 | 109 |
107 inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr, | 110 inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr, |
108 Atomic32 old_value, | 111 Atomic32 old_value, |
109 Atomic32 new_value) { | 112 Atomic32 new_value) { |
110 Atomic32 prev; | 113 Atomic32 prev = NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
111 int32_t temp; | 114 MemoryBarrier(); |
112 | |
113 __asm__ __volatile__ ( // NOLINT | |
114 "0: \n\t" | |
115 "ldxr %w[prev], %[ptr] \n\t" // Load the previous value. | |
116 "cmp %w[prev], %w[old_value] \n\t" | |
117 "bne 1f \n\t" | |
118 "stxr %w[temp], %w[new_value], %[ptr] \n\t" // Try to store the new value. | |
119 "cbnz %w[temp], 0b \n\t" // Retry if it did not work. | |
120 "dmb ish \n\t" // Data memory barrier. | |
121 "1: \n\t" | |
122 // If the compare failed the 'dmb' is unnecessary, but we still need a | |
123 // 'clrex'. | |
124 "clrex \n\t" | |
125 : [prev]"=&r" (prev), | |
126 [temp]"=&r" (temp), | |
127 [ptr]"+Q" (*ptr) | |
128 : [old_value]"r" (old_value), | |
129 [new_value]"r" (new_value) | |
130 : "memory", "cc" | |
131 ); // NOLINT | |
132 | 115 |
133 return prev; | 116 return prev; |
134 } | 117 } |
135 | 118 |
136 inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr, | 119 inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr, |
137 Atomic32 old_value, | 120 Atomic32 old_value, |
138 Atomic32 new_value) { | 121 Atomic32 new_value) { |
139 Atomic32 prev; | |
140 int32_t temp; | |
141 | |
142 MemoryBarrier(); | 122 MemoryBarrier(); |
143 | 123 Atomic32 prev = NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
144 __asm__ __volatile__ ( // NOLINT | |
145 "0: \n\t" | |
146 "ldxr %w[prev], %[ptr] \n\t" // Load the previous value. | |
147 "cmp %w[prev], %w[old_value] \n\t" | |
148 "bne 1f \n\t" | |
149 "stxr %w[temp], %w[new_value], %[ptr] \n\t" // Try to store the new value. | |
150 "cbnz %w[temp], 0b \n\t" // Retry if it did not work. | |
151 "1: \n\t" | |
152 // If the compare failed the we still need a 'clrex'. | |
153 "clrex \n\t" | |
154 : [prev]"=&r" (prev), | |
155 [temp]"=&r" (temp), | |
156 [ptr]"+Q" (*ptr) | |
157 : [old_value]"r" (old_value), | |
158 [new_value]"r" (new_value) | |
159 : "memory", "cc" | |
160 ); // NOLINT | |
161 | 124 |
162 return prev; | 125 return prev; |
163 } | 126 } |
164 | 127 |
165 inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) { | 128 inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) { |
166 *ptr = value; | 129 *ptr = value; |
167 } | 130 } |
168 | 131 |
169 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) { | 132 inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) { |
170 *ptr = value; | 133 *ptr = value; |
171 MemoryBarrier(); | 134 MemoryBarrier(); |
172 } | 135 } |
173 | 136 |
174 inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) { | 137 inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) { |
175 MemoryBarrier(); | 138 __asm__ __volatile__ ( // NOLINT |
176 *ptr = value; | 139 "stlr %w[value], %[ptr] \n\t" |
| 140 : [ptr]"=Q" (*ptr) |
| 141 : [value]"r" (value) |
| 142 : "memory" |
| 143 ); // NOLINT |
177 } | 144 } |
178 | 145 |
179 inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) { | 146 inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) { |
180 return *ptr; | 147 return *ptr; |
181 } | 148 } |
182 | 149 |
183 inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) { | 150 inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) { |
184 Atomic32 value = *ptr; | 151 Atomic32 value; |
185 MemoryBarrier(); | 152 |
| 153 __asm__ __volatile__ ( // NOLINT |
| 154 "ldar %w[value], %[ptr] \n\t" |
| 155 : [value]"=r" (value) |
| 156 : [ptr]"Q" (*ptr) |
| 157 : "memory" |
| 158 ); // NOLINT |
| 159 |
186 return value; | 160 return value; |
187 } | 161 } |
188 | 162 |
189 inline Atomic32 Release_Load(volatile const Atomic32* ptr) { | 163 inline Atomic32 Release_Load(volatile const Atomic32* ptr) { |
190 MemoryBarrier(); | 164 MemoryBarrier(); |
191 return *ptr; | 165 return *ptr; |
192 } | 166 } |
193 | 167 |
194 // 64-bit versions of the operations. | 168 // 64-bit versions of the operations. |
195 // See the 32-bit versions for comments. | 169 // See the 32-bit versions for comments. |
196 | 170 |
197 inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr, | 171 inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr, |
198 Atomic64 old_value, | 172 Atomic64 old_value, |
199 Atomic64 new_value) { | 173 Atomic64 new_value) { |
200 Atomic64 prev; | 174 Atomic64 prev; |
201 int32_t temp; | 175 int32_t temp; |
202 | 176 |
203 __asm__ __volatile__ ( // NOLINT | 177 __asm__ __volatile__ ( // NOLINT |
204 "0: \n\t" | 178 "0: \n\t" |
205 "ldxr %[prev], %[ptr] \n\t" | 179 "ldxr %[prev], %[ptr] \n\t" |
206 "cmp %[prev], %[old_value] \n\t" | 180 "cmp %[prev], %[old_value] \n\t" |
207 "bne 1f \n\t" | 181 "bne 1f \n\t" |
208 "stxr %w[temp], %[new_value], %[ptr] \n\t" | 182 "stxr %w[temp], %[new_value], %[ptr] \n\t" |
209 "cbnz %w[temp], 0b \n\t" | 183 "cbnz %w[temp], 0b \n\t" |
210 "1: \n\t" | 184 "1: \n\t" |
211 "clrex \n\t" | |
212 : [prev]"=&r" (prev), | 185 : [prev]"=&r" (prev), |
213 [temp]"=&r" (temp), | 186 [temp]"=&r" (temp), |
214 [ptr]"+Q" (*ptr) | 187 [ptr]"+Q" (*ptr) |
215 : [old_value]"r" (old_value), | 188 : [old_value]"IJr" (old_value), |
216 [new_value]"r" (new_value) | 189 [new_value]"r" (new_value) |
217 : "memory", "cc" | 190 : "cc", "memory" |
218 ); // NOLINT | 191 ); // NOLINT |
219 | 192 |
220 return prev; | 193 return prev; |
221 } | 194 } |
222 | 195 |
223 inline Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, | 196 inline Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, |
224 Atomic64 new_value) { | 197 Atomic64 new_value) { |
225 Atomic64 result; | 198 Atomic64 result; |
226 int32_t temp; | 199 int32_t temp; |
227 | 200 |
(...skipping 19 matching lines...) Expand all Loading... |
247 | 220 |
248 __asm__ __volatile__ ( // NOLINT | 221 __asm__ __volatile__ ( // NOLINT |
249 "0: \n\t" | 222 "0: \n\t" |
250 "ldxr %[result], %[ptr] \n\t" | 223 "ldxr %[result], %[ptr] \n\t" |
251 "add %[result], %[result], %[increment] \n\t" | 224 "add %[result], %[result], %[increment] \n\t" |
252 "stxr %w[temp], %[result], %[ptr] \n\t" | 225 "stxr %w[temp], %[result], %[ptr] \n\t" |
253 "cbnz %w[temp], 0b \n\t" | 226 "cbnz %w[temp], 0b \n\t" |
254 : [result]"=&r" (result), | 227 : [result]"=&r" (result), |
255 [temp]"=&r" (temp), | 228 [temp]"=&r" (temp), |
256 [ptr]"+Q" (*ptr) | 229 [ptr]"+Q" (*ptr) |
257 : [increment]"r" (increment) | 230 : [increment]"IJr" (increment) |
258 : "memory" | 231 : "memory" |
259 ); // NOLINT | 232 ); // NOLINT |
260 | 233 |
261 return result; | 234 return result; |
262 } | 235 } |
263 | 236 |
264 inline Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, | 237 inline Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, |
265 Atomic64 increment) { | 238 Atomic64 increment) { |
266 MemoryBarrier(); | 239 MemoryBarrier(); |
267 Atomic64 result = NoBarrier_AtomicIncrement(ptr, increment); | 240 Atomic64 result = NoBarrier_AtomicIncrement(ptr, increment); |
268 MemoryBarrier(); | 241 MemoryBarrier(); |
269 | 242 |
270 return result; | 243 return result; |
271 } | 244 } |
272 | 245 |
273 inline Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr, | 246 inline Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr, |
274 Atomic64 old_value, | 247 Atomic64 old_value, |
275 Atomic64 new_value) { | 248 Atomic64 new_value) { |
276 Atomic64 prev; | 249 Atomic64 prev = NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
277 int32_t temp; | 250 MemoryBarrier(); |
278 | |
279 __asm__ __volatile__ ( // NOLINT | |
280 "0: \n\t" | |
281 "ldxr %[prev], %[ptr] \n\t" | |
282 "cmp %[prev], %[old_value] \n\t" | |
283 "bne 1f \n\t" | |
284 "stxr %w[temp], %[new_value], %[ptr] \n\t" | |
285 "cbnz %w[temp], 0b \n\t" | |
286 "dmb ish \n\t" | |
287 "1: \n\t" | |
288 "clrex \n\t" | |
289 : [prev]"=&r" (prev), | |
290 [temp]"=&r" (temp), | |
291 [ptr]"+Q" (*ptr) | |
292 : [old_value]"r" (old_value), | |
293 [new_value]"r" (new_value) | |
294 : "memory", "cc" | |
295 ); // NOLINT | |
296 | 251 |
297 return prev; | 252 return prev; |
298 } | 253 } |
299 | 254 |
300 inline Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr, | 255 inline Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr, |
301 Atomic64 old_value, | 256 Atomic64 old_value, |
302 Atomic64 new_value) { | 257 Atomic64 new_value) { |
303 Atomic64 prev; | |
304 int32_t temp; | |
305 | |
306 MemoryBarrier(); | 258 MemoryBarrier(); |
307 | 259 Atomic64 prev = NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
308 __asm__ __volatile__ ( // NOLINT | |
309 "0: \n\t" | |
310 "ldxr %[prev], %[ptr] \n\t" | |
311 "cmp %[prev], %[old_value] \n\t" | |
312 "bne 1f \n\t" | |
313 "stxr %w[temp], %[new_value], %[ptr] \n\t" | |
314 "cbnz %w[temp], 0b \n\t" | |
315 "1: \n\t" | |
316 "clrex \n\t" | |
317 : [prev]"=&r" (prev), | |
318 [temp]"=&r" (temp), | |
319 [ptr]"+Q" (*ptr) | |
320 : [old_value]"r" (old_value), | |
321 [new_value]"r" (new_value) | |
322 : "memory", "cc" | |
323 ); // NOLINT | |
324 | 260 |
325 return prev; | 261 return prev; |
326 } | 262 } |
327 | 263 |
328 inline void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value) { | 264 inline void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value) { |
329 *ptr = value; | 265 *ptr = value; |
330 } | 266 } |
331 | 267 |
332 inline void Acquire_Store(volatile Atomic64* ptr, Atomic64 value) { | 268 inline void Acquire_Store(volatile Atomic64* ptr, Atomic64 value) { |
333 *ptr = value; | 269 *ptr = value; |
334 MemoryBarrier(); | 270 MemoryBarrier(); |
335 } | 271 } |
336 | 272 |
337 inline void Release_Store(volatile Atomic64* ptr, Atomic64 value) { | 273 inline void Release_Store(volatile Atomic64* ptr, Atomic64 value) { |
338 MemoryBarrier(); | 274 __asm__ __volatile__ ( // NOLINT |
339 *ptr = value; | 275 "stlr %x[value], %[ptr] \n\t" |
| 276 : [ptr]"=Q" (*ptr) |
| 277 : [value]"r" (value) |
| 278 : "memory" |
| 279 ); // NOLINT |
340 } | 280 } |
341 | 281 |
342 inline Atomic64 NoBarrier_Load(volatile const Atomic64* ptr) { | 282 inline Atomic64 NoBarrier_Load(volatile const Atomic64* ptr) { |
343 return *ptr; | 283 return *ptr; |
344 } | 284 } |
345 | 285 |
346 inline Atomic64 Acquire_Load(volatile const Atomic64* ptr) { | 286 inline Atomic64 Acquire_Load(volatile const Atomic64* ptr) { |
347 Atomic64 value = *ptr; | 287 Atomic32 value; |
348 MemoryBarrier(); | 288 |
| 289 __asm__ __volatile__ ( // NOLINT |
| 290 "ldar %x[value], %[ptr] \n\t" |
| 291 : [value]"=r" (value) |
| 292 : [ptr]"Q" (*ptr) |
| 293 : "memory" |
| 294 ); // NOLINT |
| 295 |
349 return value; | 296 return value; |
350 } | 297 } |
351 | 298 |
352 inline Atomic64 Release_Load(volatile const Atomic64* ptr) { | 299 inline Atomic64 Release_Load(volatile const Atomic64* ptr) { |
353 MemoryBarrier(); | 300 MemoryBarrier(); |
354 return *ptr; | 301 return *ptr; |
355 } | 302 } |
356 | 303 |
357 } // namespace base::subtle | 304 } // namespace base::subtle |
358 } // namespace base | 305 } // namespace base |
359 | 306 |
360 #endif // BASE_ATOMICOPS_INTERNALS_ARM64_GCC_H_ | 307 #endif // BASE_ATOMICOPS_INTERNALS_ARM64_GCC_H_ |
OLD | NEW |