8a9826bd3505c3bd047d0b189b62bee420e69870
[cacao.git] / src / mm / boehm-gc / libatomic_ops-1.2 / src / atomic_ops / sysdeps / gcc / arm.h
1 /* 
2  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
3  * Copyright (c) 1996-1999 by Silicon Graphics.  All rights reserved.
4  * Copyright (c) 1999-2003 by Hewlett-Packard Company. All rights reserved.
5  *
6  *
7  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
9  *
10  * Permission is hereby granted to use or copy this program
11  * for any purpose,  provided the above notices are retained on all copies.
12  * Permission to modify the code and to distribute modified code is granted,
13  * provided the above notices are retained, and a notice that the code was
14  * modified is included with the above copyright notice.
15  *
16  */
17
18 #include "../read_ordered.h"
19
20 #include "../test_and_set_t_is_ao_t.h" /* Probably suboptimal */
21
22 /* NEC LE-IT: ARMv6 is the first architecture providing support for simple LL/SC
23  * A data memory barrier must be raised via CP15 command (see documentation).   
24  *                                                                                                                                                              
25  * ARMv7 is compatible to ARMv6 but has a simpler command for issuing a                 
26  * memory barrier (DMB). Raising it via CP15 should still work as told me by the
27  * support engineers. If it turns out to be much quicker than we should implement
28  * custom code for ARMv7 using the asm { dmb } command.                                                                                                         
29  *
30  * If only a single processor is used, we can define AO_UNIPROCESSOR
31  * and do not need to access CP15 for ensuring a DMB  
32 */
33
34 /* NEC LE-IT: gcc has no way to easily check the arm architecture
35  * but defines only one of __ARM_ARCH_x__ to be true                    */
36 #if defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_7__)  
37
38 #include "../standard_ao_double_t.h"
39
40 AO_INLINE void
41 AO_nop_full(void)
42 {
43 #ifndef AO_UNIPROCESSOR
44         /* issue an data memory barrier (keeps ordering of memory transactions  */
45         /* before and after this operation)                                     */
46         unsigned int dest=0;
47         __asm__ __volatile__("mcr p15,0,%0,c7,c10,5" :"=&r"(dest) : : "memory");
48 #endif
49 }
50
51 #define AO_HAVE_nop_full
52
53 /* NEC LE-IT: AO_t load is simple reading */
54 AO_INLINE AO_t
55 AO_load(const volatile AO_t *addr)
56 {
57   /* Cast away the volatile for architectures like IA64 where   */
58   /* volatile adds barrier semantics.                           */
59   return (*(const AO_t *)addr);
60 }
61 #define AO_HAVE_load
62
63 /* NEC LE-IT: atomic "store" - according to ARM documentation this is
64  * the only safe way to set variables also used in LL/SC environment.
65  * A direct write won't be recognized by the LL/SC construct on the _same_ CPU.
66  * Support engineers response for behaviour of ARMv6:
67  * 
68    Core1        Core2          SUCCESS
69    ===================================
70    LDREX(x)
71    STREX(x)                    Yes
72    -----------------------------------
73    LDREX(x)
74                 STR(x)
75    STREX(x)                    No
76    -----------------------------------
77    LDREX(x)
78    STR(x)
79    STREX(x)                    Yes
80    -----------------------------------
81
82  * ARMv7 behaves similar, see documentation CortexA8 TRM, point 8.5  
83  *
84  * HB: I think this is only a problem if interrupt handlers do not clear
85  * the reservation, as they almost certainly should.  Probably change this back
86  * in a while?
87 */
88 AO_INLINE void AO_store(volatile AO_t *addr, AO_t value)
89 {
90         AO_t    flag;
91         
92         __asm__ __volatile__("@AO_store\n"
93 "1:     ldrex   %0, [%2]\n"
94 "       strex   %0, %3, [%2]\n"
95 "       teq     %0, #0\n"
96 "       bne     1b"
97         : "=&r"(flag), "+m"(*addr)
98         : "r" (addr), "r"(value)
99         : "cc");
100 }
101 #define AO_HAVE_store
102
103 /* NEC LE-IT: replace the SWAP as recommended by ARM:
104
105    "Applies to: ARM11 Cores
106         Though the SWP instruction will still work with ARM V6 cores, it is
107         recommended     to use the new V6 synchronization instructions. The SWP
108         instruction produces ‘locked’ read and write accesses which are atomic,
109         i.e. another operation cannot be done between these locked accesses which
110         ties up external bus (AHB,AXI) bandwidth and can increase worst case 
111         interrupt latencies. LDREX,STREX are more flexible, other instructions can
112         be done between the LDREX and STREX accesses. 
113    "
114 */
115 AO_INLINE AO_TS_t
116 AO_test_and_set(volatile AO_TS_t *addr) {
117         
118         AO_TS_t oldval;
119         unsigned long flag;
120
121         __asm__ __volatile__("@AO_test_and_set\n"
122 "1:     ldrex   %0, [%3]\n"
123 "       strex   %1, %4, [%3]\n"
124 "       teq             %1, #0\n"
125 "       bne             1b\n"
126         : "=&r"(oldval),"=&r"(flag), "+m"(*addr)
127         : "r"(addr), "r"(1)
128         : "cc");
129
130         return oldval;
131 }
132
133 #define AO_HAVE_test_and_set
134
135 /* NEC LE-IT: fetch and add for ARMv6 */
136 AO_INLINE AO_t
137 AO_fetch_and_add(volatile AO_t *p, AO_t incr)
138 {
139         unsigned long flag,tmp;
140         AO_t result;
141
142         __asm__ __volatile__("@AO_fetch_and_add\n"
143 "1:     ldrex   %0, [%5]\n"                     /* get original         */
144 "       add     %2, %0, %4\n"           /* sum up in incr       */
145 "       strex   %1, %2, [%5]\n"         /* store them           */
146 "       teq             %1, #0\n"
147 "       bne             1b\n"
148         : "=&r"(result),"=&r"(flag),"=&r"(tmp),"+m"(*p) /* 0..3 */
149         : "r"(incr), "r"(p)                                                             /* 4..5 */
150         : "cc");
151
152         return result;
153 }
154
155 #define AO_HAVE_fetch_and_add
156
157 /* NEC LE-IT: fetch and add1 for ARMv6 */
158 AO_INLINE AO_t
159 AO_fetch_and_add1(volatile AO_t *p)
160 {
161         unsigned long flag,tmp;
162         AO_t result;
163
164         __asm__ __volatile__("@AO_fetch_and_add1\n"
165 "1:     ldrex   %0, [%4]\n"                     /* get original   */
166 "       add     %1, %0, #1\n"           /* increment */
167 "       strex   %2, %1, [%4]\n"         /* store them */
168 "       teq             %2, #0\n"
169 "       bne             1b\n"
170         : "=&r"(result), "=&r"(tmp), "=&r"(flag), "+m"(*p)
171         : "r"(p)
172         : "cc");
173
174         return result;
175 }
176
177 #define AO_HAVE_fetch_and_add1
178
179 /* NEC LE-IT: fetch and sub for ARMv6 */
180 AO_INLINE AO_t
181 AO_fetch_and_sub1(volatile AO_t *p)
182 {
183         unsigned long flag,tmp;
184         AO_t result;
185
186         __asm__ __volatile__("@AO_fetch_and_sub1\n"
187 "1:     ldrex   %0, [%4]\n"                     /* get original   */
188 "       sub     %1, %0, #1\n"           /* decrement */
189 "       strex   %2, %1, [%4]\n"         /* store them */
190 "       teq             %2, #0\n"
191 "       bne             1b\n"
192         : "=&r"(result), "=&r"(tmp), "=&r"(flag), "+m"(*p)
193         : "r"(p)
194         : "cc");
195
196         return result;
197 }
198
199 #define AO_HAVE_fetch_and_sub1
200
201 /* NEC LE-IT: compare and swap */
202 /* Returns nonzero if the comparison succeeded. */
203 AO_INLINE int
204 AO_compare_and_swap(volatile AO_t *addr,
205                                 AO_t old_val, AO_t new_val) 
206 {
207          AO_t result,tmp;
208
209         __asm__ __volatile__("@ AO_compare_and_swap\n"
210 "1:     mov             %0, #2\n"                       /* store a flag */
211 "       ldrex   %1, [%3]\n"                     /* get original */
212 "       teq             %1, %4\n"                       /* see if match */
213 "       strexeq %0, %5, [%3]\n"         /* store new one if matched */
214 "       teq             %0, #1\n"
215 "       beq             1b\n"                           /* if update failed, repeat */
216         : "=&r"(result), "=&r"(tmp), "+m"(*addr)
217         : "r"(addr), "r"(old_val), "r"(new_val)
218         : "cc");
219
220         return !(result&2);                     /* if succeded, return 1, else 0 */
221 }
222 #define AO_HAVE_compare_and_swap
223
224 AO_INLINE int
225 AO_compare_double_and_swap_double(volatile AO_double_t *addr,
226                                                           AO_t old_val1, AO_t old_val2,
227                                                           AO_t new_val1, AO_t new_val2) 
228 {
229         double_ptr_storage old_val = ((double_ptr_storage)old_val2 << 32) | old_val1;
230         double_ptr_storage new_val = ((double_ptr_storage)new_val2 << 32) | new_val1;
231         
232     double_ptr_storage tmp;
233         int result;
234         
235         while(1) {
236                 __asm__ __volatile__("@ AO_compare_and_swap_double\n"
237                 "       ldrexd  %0, [%1]\n"                     /* get original to r1&r2*/
238                         : "=&r"(tmp)
239                         : "r"(addr)
240                         : );
241                 if(tmp != old_val)      return false;
242                 __asm__ __volatile__(
243                 "       strexd  %0, %2, [%3]\n" /* store new one if matched */
244                         : "=&r"(result),"+m"(*addr) 
245                         : "r"(new_val), "r"(addr) 
246                         : );
247                 if(!result)     return true;
248         }
249 }
250
251 #define AO_HAVE_compare_double_and_swap_double
252
253 #else
254 /* pre ARMv6 architecures ... */
255  
256 /* I found a slide set that, if I read it correctly, claims that        */
257 /* Loads followed by either a Load or Store are ordered, but nothing    */
258 /* else is.                                                             */
259 /* It appears that SWP is the only simple memory barrier.               */
260 #include "../all_atomic_load_store.h"
261
262 AO_INLINE AO_TS_VAL_t
263 AO_test_and_set_full(volatile AO_TS_t *addr) {
264   AO_TS_VAL_t oldval;
265   /* SWP on ARM is very similar to XCHG on x86.                 */
266   /* The first operand is the result, the second the value      */
267   /* to be stored.  Both registers must be different from addr. */
268   /* Make the address operand an early clobber output so it     */
269   /* doesn't overlap with the other operands.  The early clobber*/
270   /* on oldval is necessary to prevent the compiler allocating  */
271   /* them to the same register if they are both unused.         */
272   __asm__ __volatile__("swp %0, %2, [%3]"
273                         : "=&r"(oldval), "=&r"(addr)
274                         : "r"(1), "1"(addr)
275                         : "memory");
276   return oldval;
277 }
278
279 #define AO_HAVE_test_and_set_full
280
281 #endif // __ARM_ARCH_x