1 #ifndef _URCU_ARCH_UATOMIC_PPC_H
2 #define _URCU_ARCH_UATOMIC_PPC_H
5 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
6 * Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
7 * Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
8 * Copyright (c) 2009 Mathieu Desnoyers
10 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
11 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
13 * Permission is hereby granted to use or copy this program
14 * for any purpose, provided the above notices are retained on all copies.
15 * Permission to modify the code and to distribute modified code is granted,
16 * provided the above notices are retained, and a notice that the code was
17 * modified is included with the above copyright notice.
19 * Code inspired from libuatomic_ops-1.2, inherited in part from the
20 * Boehm-Demers-Weiser conservative garbage collector.
23 #include <urcu/compiler.h>
24 #include <urcu/system.h>
30 #define ILLEGAL_INSTR ".long 0xd00d00"
33 * Providing sequential consistency semantic with respect to other
34 * instructions for cmpxchg and add_return family of atomic primitives.
36 * This is achieved with:
37 * lwsync (prior loads can be reordered after following load)
40 * test if success (retry)
43 * Explanation of the sequential consistency provided by this scheme
44 * from Paul E. McKenney:
46 * The reason we can get away with the lwsync before is that if a prior
47 * store reorders with the lwarx, then you have to store to the atomic
48 * variable from some other CPU to detect it.
50 * And if you do that, the lwarx will lose its reservation, so the stwcx
51 * will fail. The atomic operation will retry, so that the caller won't be
52 * able to see the misordering.
57 static inline __attribute__((always_inline
))
58 unsigned long _uatomic_exchange(void *addr
, unsigned long val
, int len
)
67 "1:\t" "lwarx %0,0,%1\n" /* load and reserve */
68 "stwcx. %2,0,%1\n" /* else store conditional */
69 "bne- 1b\n" /* retry if lost reservation */
77 #if (CAA_BITS_PER_LONG == 64)
84 "1:\t" "ldarx %0,0,%1\n" /* load and reserve */
85 "stdcx. %2,0,%1\n" /* else store conditional */
86 "bne- 1b\n" /* retry if lost reservation */
97 * generate an illegal instruction. Cannot catch this with
98 * linker tricks when optimizations are disabled.
100 __asm__
__volatile__(ILLEGAL_INSTR
);
104 #define uatomic_xchg(addr, v) \
105 ((__typeof__(*(addr))) _uatomic_exchange((addr), \
106 caa_cast_long_keep_sign(v), \
110 static inline __attribute__((always_inline
))
111 unsigned long _uatomic_cmpxchg(void *addr
, unsigned long old
,
112 unsigned long _new
, int len
)
117 unsigned int old_val
;
119 __asm__
__volatile__(
121 "1:\t" "lwarx %0,0,%1\n" /* load and reserve */
122 "cmpw %0,%3\n" /* if load is not equal to */
123 "bne 2f\n" /* old, fail */
124 "stwcx. %2,0,%1\n" /* else store conditional */
125 "bne- 1b\n" /* retry if lost reservation */
129 : "r"(addr
), "r"((unsigned int)_new
),
130 "r"((unsigned int)old
)
135 #if (CAA_BITS_PER_LONG == 64)
138 unsigned long old_val
;
140 __asm__
__volatile__(
142 "1:\t" "ldarx %0,0,%1\n" /* load and reserve */
143 "cmpd %0,%3\n" /* if load is not equal to */
144 "bne 2f\n" /* old, fail */
145 "stdcx. %2,0,%1\n" /* else store conditional */
146 "bne- 1b\n" /* retry if lost reservation */
150 : "r"(addr
), "r"((unsigned long)_new
),
151 "r"((unsigned long)old
)
159 * generate an illegal instruction. Cannot catch this with
160 * linker tricks when optimizations are disabled.
162 __asm__
__volatile__(ILLEGAL_INSTR
);
167 #define uatomic_cmpxchg(addr, old, _new) \
168 ((__typeof__(*(addr))) _uatomic_cmpxchg((addr), \
169 caa_cast_long_keep_sign(old), \
170 caa_cast_long_keep_sign(_new),\
173 /* uatomic_add_return */
175 static inline __attribute__((always_inline
))
176 unsigned long _uatomic_add_return(void *addr
, unsigned long val
,
184 __asm__
__volatile__(
186 "1:\t" "lwarx %0,0,%1\n" /* load and reserve */
187 "add %0,%2,%0\n" /* add val to value loaded */
188 "stwcx. %0,0,%1\n" /* store conditional */
189 "bne- 1b\n" /* retry if lost reservation */
192 : "r"(addr
), "r"(val
)
197 #if (CAA_BITS_PER_LONG == 64)
200 unsigned long result
;
202 __asm__
__volatile__(
204 "1:\t" "ldarx %0,0,%1\n" /* load and reserve */
205 "add %0,%2,%0\n" /* add val to value loaded */
206 "stdcx. %0,0,%1\n" /* store conditional */
207 "bne- 1b\n" /* retry if lost reservation */
210 : "r"(addr
), "r"(val
)
218 * generate an illegal instruction. Cannot catch this with
219 * linker tricks when optimizations are disabled.
221 __asm__
__volatile__(ILLEGAL_INSTR
);
226 #define uatomic_add_return(addr, v) \
227 ((__typeof__(*(addr))) _uatomic_add_return((addr), \
228 caa_cast_long_keep_sign(v), \
235 #include <urcu/uatomic/generic.h>
237 #endif /* _URCU_ARCH_UATOMIC_PPC_H */
This page took 0.065239 seconds and 4 git commands to generate.