1 /* Atomic operations. PowerPC Common version.
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Paul Mackerras <paulus@au.ibm.com>, 2003.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <bits/wordsize.h>
23 /* Atomic operations. PowerPC64 version.
24 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
25 This file is part of the GNU C Library.
26 Contributed by Paul Mackerras <paulus@au.ibm.com>, 2003.
28 The GNU C Library is free software; you can redistribute it and/or
29 modify it under the terms of the GNU Lesser General Public
30 License as published by the Free Software Foundation; either
31 version 2.1 of the License, or (at your option) any later version.
33 The GNU C Library is distributed in the hope that it will be useful,
34 but WITHOUT ANY WARRANTY; without even the implied warranty of
35 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 Lesser General Public License for more details.
38 You should have received a copy of the GNU Lesser General Public
39 License along with the GNU C Library; if not, see
40 <http://www.gnu.org/licenses/>. */
42 /* The 32-bit exchange_bool is different on powerpc64 because the subf
43 does signed 64-bit arthmatic while the lwarx is 32-bit unsigned
44 (a load word and zero (high 32) form) load.
45 In powerpc64 register values are 64-bit by default, including oldval.
46 The value in old val unknown sign extension, lwarx loads the 32-bit
47 value as unsigned. So we explicitly clear the high 32 bits in oldval. */
48 # define __arch_compare_and_exchange_bool_32_acq(mem, newval, oldval) \
50 unsigned int __tmp, __tmp2; \
51 __asm__ __volatile__ (" clrldi %1,%1,32\n" \
52 "1: lwarx %0,0,%2\n" \
57 "2: " __ARCH_ACQ_INSTR \
58 : "=&r" (__tmp), "=r" (__tmp2) \
59 : "b" (mem), "1" (oldval), "r" (newval) \
64 # define __arch_compare_and_exchange_bool_32_rel(mem, newval, oldval) \
66 unsigned int __tmp, __tmp2; \
67 __asm__ __volatile__ (__ARCH_REL_INSTR "\n" \
68 " clrldi %1,%1,32\n" \
69 "1: lwarx %0,0,%2\n" \
75 : "=&r" (__tmp), "=r" (__tmp2) \
76 : "b" (mem), "1" (oldval), "r" (newval) \
82 * Only powerpc64 processors support Load doubleword and reserve index (ldarx)
83 * and Store doubleword conditional indexed (stdcx) instructions. So here
84 * we define the 64-bit forms.
86 # define __arch_compare_and_exchange_bool_64_acq(mem, newval, oldval) \
88 unsigned long __tmp; \
89 __asm__ __volatile__ ( \
90 "1: ldarx %0,0,%1\n" \
95 "2: " __ARCH_ACQ_INSTR \
97 : "b" (mem), "r" (oldval), "r" (newval) \
102 # define __arch_compare_and_exchange_bool_64_rel(mem, newval, oldval) \
104 unsigned long __tmp; \
105 __asm__ __volatile__ (__ARCH_REL_INSTR "\n" \
106 "1: ldarx %0,0,%1\n" \
107 " subf. %0,%2,%0\n" \
109 " stdcx. %3,0,%1\n" \
113 : "b" (mem), "r" (oldval), "r" (newval) \
114 : "cr0", "memory"); \
118 #define __arch_compare_and_exchange_val_64_acq(mem, newval, oldval) \
120 __typeof (*(mem)) __tmp; \
121 __typeof (mem) __memp = (mem); \
122 __asm__ __volatile__ ( \
123 "1: ldarx %0,0,%1\n" \
126 " stdcx. %3,0,%1\n" \
128 "2: " __ARCH_ACQ_INSTR \
130 : "b" (__memp), "r" (oldval), "r" (newval) \
131 : "cr0", "memory"); \
135 #define __arch_compare_and_exchange_val_64_rel(mem, newval, oldval) \
137 __typeof (*(mem)) __tmp; \
138 __typeof (mem) __memp = (mem); \
139 __asm__ __volatile__ (__ARCH_REL_INSTR "\n" \
140 "1: ldarx %0,0,%1\n" \
143 " stdcx. %3,0,%1\n" \
147 : "b" (__memp), "r" (oldval), "r" (newval) \
148 : "cr0", "memory"); \
152 # define __arch_atomic_exchange_64_acq(mem, value) \
154 __typeof (*mem) __val; \
155 __asm__ __volatile__ (__ARCH_REL_INSTR "\n" \
156 "1: ldarx %0,0,%2\n" \
157 " stdcx. %3,0,%2\n" \
159 " " __ARCH_ACQ_INSTR \
160 : "=&r" (__val), "=m" (*mem) \
161 : "b" (mem), "r" (value), "m" (*mem) \
162 : "cr0", "memory"); \
166 # define __arch_atomic_exchange_64_rel(mem, value) \
168 __typeof (*mem) __val; \
169 __asm__ __volatile__ (__ARCH_REL_INSTR "\n" \
170 "1: ldarx %0,0,%2\n" \
171 " stdcx. %3,0,%2\n" \
173 : "=&r" (__val), "=m" (*mem) \
174 : "b" (mem), "r" (value), "m" (*mem) \
175 : "cr0", "memory"); \
179 # define __arch_atomic_exchange_and_add_64(mem, value) \
181 __typeof (*mem) __val, __tmp; \
182 __asm__ __volatile__ ("1: ldarx %0,0,%3\n" \
184 " stdcx. %1,0,%3\n" \
186 : "=&b" (__val), "=&r" (__tmp), "=m" (*mem) \
187 : "b" (mem), "r" (value), "m" (*mem) \
188 : "cr0", "memory"); \
192 # define __arch_atomic_increment_val_64(mem) \
194 __typeof (*(mem)) __val; \
195 __asm__ __volatile__ ("1: ldarx %0,0,%2\n" \
197 " stdcx. %0,0,%2\n" \
199 : "=&b" (__val), "=m" (*mem) \
200 : "b" (mem), "m" (*mem) \
201 : "cr0", "memory"); \
205 # define __arch_atomic_decrement_val_64(mem) \
207 __typeof (*(mem)) __val; \
208 __asm__ __volatile__ ("1: ldarx %0,0,%2\n" \
210 " stdcx. %0,0,%2\n" \
212 : "=&b" (__val), "=m" (*mem) \
213 : "b" (mem), "m" (*mem) \
214 : "cr0", "memory"); \
218 # define __arch_atomic_decrement_if_positive_64(mem) \
219 ({ int __val, __tmp; \
220 __asm__ __volatile__ ("1: ldarx %0,0,%3\n" \
224 " stdcx. %1,0,%3\n" \
226 "2: " __ARCH_ACQ_INSTR \
227 : "=&b" (__val), "=&r" (__tmp), "=m" (*mem) \
228 : "b" (mem), "m" (*mem) \
229 : "cr0", "memory"); \
234 * All powerpc64 processors support the new "light weight" sync (lwsync).
236 # define atomic_read_barrier() __asm__ ("lwsync" ::: "memory")
238 * "light weight" sync can also be used for the release barrier.
241 # define __ARCH_REL_INSTR "lwsync"
245 /* Atomic operations. PowerPC32 version.
246 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
247 This file is part of the GNU C Library.
248 Contributed by Paul Mackerras <paulus@au.ibm.com>, 2003.
250 The GNU C Library is free software; you can redistribute it and/or
251 modify it under the terms of the GNU Lesser General Public
252 License as published by the Free Software Foundation; either
253 version 2.1 of the License, or (at your option) any later version.
255 The GNU C Library is distributed in the hope that it will be useful,
256 but WITHOUT ANY WARRANTY; without even the implied warranty of
257 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
258 Lesser General Public License for more details.
260 You should have received a copy of the GNU Lesser General Public
261 License along with the GNU C Library; if not, see
262 <http://www.gnu.org/licenses/>. */
265 * The 32-bit exchange_bool is different on powerpc64 because the subf
266 * does signed 64-bit arthmatic while the lwarx is 32-bit unsigned
267 * (a load word and zero (high 32) form). So powerpc64 has a slightly
268 * different version in sysdeps/powerpc/powerpc64/bits/atomic.h.
270 # define __arch_compare_and_exchange_bool_32_acq(mem, newval, oldval) \
272 unsigned int __tmp; \
273 __asm__ __volatile__ ( \
274 "1: lwarx %0,0,%1\n" \
275 " subf. %0,%2,%0\n" \
277 " stwcx. %3,0,%1\n" \
279 "2: " __ARCH_ACQ_INSTR \
281 : "b" (mem), "r" (oldval), "r" (newval) \
282 : "cr0", "memory"); \
286 # define __arch_compare_and_exchange_bool_32_rel(mem, newval, oldval) \
288 unsigned int __tmp; \
289 __asm__ __volatile__ (__ARCH_REL_INSTR "\n" \
290 "1: lwarx %0,0,%1\n" \
291 " subf. %0,%2,%0\n" \
293 " stwcx. %3,0,%1\n" \
297 : "b" (mem), "r" (oldval), "r" (newval) \
298 : "cr0", "memory"); \
302 /* Powerpc32 processors don't implement the 64-bit (doubleword) forms of
303 load and reserve (ldarx) and store conditional (stdcx.) instructions.
304 So for powerpc32 we stub out the 64-bit forms. */
305 # define __arch_compare_and_exchange_bool_64_acq(mem, newval, oldval) \
308 # define __arch_compare_and_exchange_val_64_acq(mem, newval, oldval) \
309 (abort (), (__typeof (*mem)) 0)
311 # define __arch_compare_and_exchange_bool_64_rel(mem, newval, oldval) \
314 # define __arch_compare_and_exchange_val_64_rel(mem, newval, oldval) \
315 (abort (), (__typeof (*mem)) 0)
317 # define __arch_atomic_exchange_64_acq(mem, value) \
318 ({ abort (); (*mem) = (value); })
320 # define __arch_atomic_exchange_64_rel(mem, value) \
321 ({ abort (); (*mem) = (value); })
323 # define __arch_atomic_exchange_and_add_64(mem, value) \
324 ({ abort (); (*mem) = (value); })
326 # define __arch_atomic_increment_val_64(mem) \
327 ({ abort (); (*mem)++; })
329 # define __arch_atomic_decrement_val_64(mem) \
330 ({ abort (); (*mem)--; })
332 # define __arch_atomic_decrement_if_positive_64(mem) \
333 ({ abort (); (*mem)--; })
337 * Newer powerpc64 processors support the new "light weight" sync (lwsync)
338 * So if the build is using -mcpu=[power4,power5,power5+,970] we can
341 # define atomic_read_barrier() __asm__ ("lwsync" ::: "memory")
343 * "light weight" sync can also be used for the release barrier.
346 # define __ARCH_REL_INSTR "lwsync"
351 * Older powerpc32 processors don't support the new "light weight"
352 * sync (lwsync). So the only safe option is to use normal sync
353 * for all powerpc32 applications.
355 # define atomic_read_barrier() __asm__ ("sync" ::: "memory")
362 typedef int32_t atomic32_t;
363 typedef uint32_t uatomic32_t;
364 typedef int_fast32_t atomic_fast32_t;
365 typedef uint_fast32_t uatomic_fast32_t;
367 typedef int64_t atomic64_t;
368 typedef uint64_t uatomic64_t;
369 typedef int_fast64_t atomic_fast64_t;
370 typedef uint_fast64_t uatomic_fast64_t;
372 typedef intptr_t atomicptr_t;
373 typedef uintptr_t uatomicptr_t;
374 typedef intmax_t atomic_max_t;
375 typedef uintmax_t uatomic_max_t;
378 * Powerpc does not have byte and halfword forms of load and reserve and
379 * store conditional. So for powerpc we stub out the 8- and 16-bit forms.
381 #define __arch_compare_and_exchange_bool_8_acq(mem, newval, oldval) \
384 #define __arch_compare_and_exchange_bool_16_acq(mem, newval, oldval) \
387 #define __arch_compare_and_exchange_bool_8_rel(mem, newval, oldval) \
390 #define __arch_compare_and_exchange_bool_16_rel(mem, newval, oldval) \
394 # define __ARCH_ACQ_INSTR ""
395 # define __ARCH_REL_INSTR ""
397 # define __ARCH_ACQ_INSTR "isync"
398 # ifndef __ARCH_REL_INSTR
399 # define __ARCH_REL_INSTR "sync"
403 #ifndef MUTEX_HINT_ACQ
404 # define MUTEX_HINT_ACQ
406 #ifndef MUTEX_HINT_REL
407 # define MUTEX_HINT_REL
410 #define atomic_full_barrier() __asm__ ("sync" ::: "memory")
411 #define atomic_write_barrier() __asm__ ("eieio" ::: "memory")
413 #define __arch_compare_and_exchange_val_32_acq(mem, newval, oldval) \
415 __typeof (*(mem)) __tmp; \
416 __typeof (mem) __memp = (mem); \
417 __asm__ __volatile__ ( \
418 "1: lwarx %0,0,%1\n" \
421 " stwcx. %3,0,%1\n" \
423 "2: " __ARCH_ACQ_INSTR \
425 : "b" (__memp), "r" (oldval), "r" (newval) \
426 : "cr0", "memory"); \
430 #define __arch_compare_and_exchange_val_32_rel(mem, newval, oldval) \
432 __typeof (*(mem)) __tmp; \
433 __typeof (mem) __memp = (mem); \
434 __asm__ __volatile__ (__ARCH_REL_INSTR "\n" \
435 "1: lwarx %0,0,%1\n" \
438 " stwcx. %3,0,%1\n" \
442 : "b" (__memp), "r" (oldval), "r" (newval) \
443 : "cr0", "memory"); \
447 #define __arch_atomic_exchange_32_acq(mem, value) \
449 __typeof (*mem) __val; \
450 __asm__ __volatile__ ( \
451 "1: lwarx %0,0,%2\n" \
452 " stwcx. %3,0,%2\n" \
454 " " __ARCH_ACQ_INSTR \
455 : "=&r" (__val), "=m" (*mem) \
456 : "b" (mem), "r" (value), "m" (*mem) \
457 : "cr0", "memory"); \
461 #define __arch_atomic_exchange_32_rel(mem, value) \
463 __typeof (*mem) __val; \
464 __asm__ __volatile__ (__ARCH_REL_INSTR "\n" \
465 "1: lwarx %0,0,%2\n" \
466 " stwcx. %3,0,%2\n" \
468 : "=&r" (__val), "=m" (*mem) \
469 : "b" (mem), "r" (value), "m" (*mem) \
470 : "cr0", "memory"); \
474 #define __arch_atomic_exchange_and_add_32(mem, value) \
476 __typeof (*mem) __val, __tmp; \
477 __asm__ __volatile__ ("1: lwarx %0,0,%3\n" \
479 " stwcx. %1,0,%3\n" \
481 : "=&b" (__val), "=&r" (__tmp), "=m" (*mem) \
482 : "b" (mem), "r" (value), "m" (*mem) \
483 : "cr0", "memory"); \
487 #define __arch_atomic_increment_val_32(mem) \
489 __typeof (*(mem)) __val; \
490 __asm__ __volatile__ ("1: lwarx %0,0,%2\n" \
492 " stwcx. %0,0,%2\n" \
494 : "=&b" (__val), "=m" (*mem) \
495 : "b" (mem), "m" (*mem) \
496 : "cr0", "memory"); \
500 #define __arch_atomic_decrement_val_32(mem) \
502 __typeof (*(mem)) __val; \
503 __asm__ __volatile__ ("1: lwarx %0,0,%2\n" \
505 " stwcx. %0,0,%2\n" \
507 : "=&b" (__val), "=m" (*mem) \
508 : "b" (mem), "m" (*mem) \
509 : "cr0", "memory"); \
513 #define __arch_atomic_decrement_if_positive_32(mem) \
514 ({ int __val, __tmp; \
515 __asm__ __volatile__ ("1: lwarx %0,0,%3\n" \
519 " stwcx. %1,0,%3\n" \
521 "2: " __ARCH_ACQ_INSTR \
522 : "=&b" (__val), "=&r" (__tmp), "=m" (*mem) \
523 : "b" (mem), "m" (*mem) \
524 : "cr0", "memory"); \
528 #define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
530 __typeof (*(mem)) __result; \
531 if (sizeof (*mem) == 4) \
532 __result = __arch_compare_and_exchange_val_32_acq(mem, newval, oldval); \
533 else if (sizeof (*mem) == 8) \
534 __result = __arch_compare_and_exchange_val_64_acq(mem, newval, oldval); \
540 #define atomic_compare_and_exchange_val_rel(mem, newval, oldval) \
542 __typeof (*(mem)) __result; \
543 if (sizeof (*mem) == 4) \
544 __result = __arch_compare_and_exchange_val_32_rel(mem, newval, oldval); \
545 else if (sizeof (*mem) == 8) \
546 __result = __arch_compare_and_exchange_val_64_rel(mem, newval, oldval); \
552 #define atomic_exchange_acq(mem, value) \
554 __typeof (*(mem)) __result; \
555 if (sizeof (*mem) == 4) \
556 __result = __arch_atomic_exchange_32_acq (mem, value); \
557 else if (sizeof (*mem) == 8) \
558 __result = __arch_atomic_exchange_64_acq (mem, value); \
564 #define atomic_exchange_rel(mem, value) \
566 __typeof (*(mem)) __result; \
567 if (sizeof (*mem) == 4) \
568 __result = __arch_atomic_exchange_32_rel (mem, value); \
569 else if (sizeof (*mem) == 8) \
570 __result = __arch_atomic_exchange_64_rel (mem, value); \
576 #define atomic_exchange_and_add(mem, value) \
578 __typeof (*(mem)) __result; \
579 if (sizeof (*mem) == 4) \
580 __result = __arch_atomic_exchange_and_add_32 (mem, value); \
581 else if (sizeof (*mem) == 8) \
582 __result = __arch_atomic_exchange_and_add_64 (mem, value); \
588 #define atomic_increment_val(mem) \
590 __typeof (*(mem)) __result; \
591 if (sizeof (*(mem)) == 4) \
592 __result = __arch_atomic_increment_val_32 (mem); \
593 else if (sizeof (*(mem)) == 8) \
594 __result = __arch_atomic_increment_val_64 (mem); \
600 #define atomic_increment(mem) ({ atomic_increment_val (mem); (void) 0; })
602 #define atomic_decrement_val(mem) \
604 __typeof (*(mem)) __result; \
605 if (sizeof (*(mem)) == 4) \
606 __result = __arch_atomic_decrement_val_32 (mem); \
607 else if (sizeof (*(mem)) == 8) \
608 __result = __arch_atomic_decrement_val_64 (mem); \
614 #define atomic_decrement(mem) ({ atomic_decrement_val (mem); (void) 0; })
617 /* Decrement *MEM if it is > 0, and return the old value. */
618 #define atomic_decrement_if_positive(mem) \
619 ({ __typeof (*(mem)) __result; \
620 if (sizeof (*mem) == 4) \
621 __result = __arch_atomic_decrement_if_positive_32 (mem); \
622 else if (sizeof (*mem) == 8) \
623 __result = __arch_atomic_decrement_if_positive_64 (mem); \