1 /* 64bit Linux-specific atomic operations for ARM EABI.
2 Copyright (C) 2008-2013 Free Software Foundation, Inc.
3 Based on linux-atomic.c
5 64 bit additions david.gilbert@linaro.org
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
28 /* 64bit helper functions for atomic operations; the compiler will
29 call these when the code is compiled for a CPU without ldrexd/strexd.
30 (If the CPU had those then the compiler inlines the operation).
32 These helpers require a kernel helper that's only present on newer
33 kernels; we check for that in an init section and bail out rather
36 extern unsigned int __write (int fd
, const void *buf
, unsigned int count
);
37 extern void abort (void);
39 /* Kernel helper for compare-and-exchange. */
40 typedef int (__kernel_cmpxchg64_t
) (const long long* oldval
,
41 const long long* newval
,
43 #define __kernel_cmpxchg64 (*(__kernel_cmpxchg64_t *) 0xffff0f60)
45 /* Kernel helper page version number. */
46 #define __kernel_helper_version (*(unsigned int *)0xffff0ffc)
48 /* Check that the kernel has a new enough version at load. */
49 static void __check_for_sync8_kernelhelper (void)
51 if (__kernel_helper_version
< 5)
53 const char err
[] = "A newer kernel is required to run this binary. "
54 "(__kernel_cmpxchg64 helper)\n";
55 /* At this point we need a way to crash with some information
56 for the user - I'm not sure I can rely on much else being
57 available at this point, so do the same as generic-morestack.c
58 write () and abort (). */
59 __write (2 /* stderr. */, err
, sizeof (err
));
64 static void (*__sync8_kernelhelper_inithook
[]) (void)
65 __attribute__ ((used
, section (".init_array"))) = {
66 &__check_for_sync8_kernelhelper
69 #define HIDDEN __attribute__ ((visibility ("hidden")))
71 #define FETCH_AND_OP_WORD64(OP, PFX_OP, INF_OP) \
73 __sync_fetch_and_##OP##_8 (long long *ptr, long long val) \
80 tmp2 = PFX_OP (tmp INF_OP val); \
81 failure = __kernel_cmpxchg64 (&tmp, &tmp2, ptr); \
82 } while (failure != 0); \
87 FETCH_AND_OP_WORD64 (add
, , +)
88 FETCH_AND_OP_WORD64 (sub
, , -)
89 FETCH_AND_OP_WORD64 (or, , |)
90 FETCH_AND_OP_WORD64 (and, , &)
91 FETCH_AND_OP_WORD64 (xor, , ^)
92 FETCH_AND_OP_WORD64 (nand
, ~, &)
94 #define NAME_oldval(OP, WIDTH) __sync_fetch_and_##OP##_##WIDTH
95 #define NAME_newval(OP, WIDTH) __sync_##OP##_and_fetch_##WIDTH
97 /* Implement both __sync_<op>_and_fetch and __sync_fetch_and_<op> for
98 subword-sized quantities. */
100 #define OP_AND_FETCH_WORD64(OP, PFX_OP, INF_OP) \
102 __sync_##OP##_and_fetch_8 (long long *ptr, long long val) \
105 long long tmp,tmp2; \
109 tmp2 = PFX_OP (tmp INF_OP val); \
110 failure = __kernel_cmpxchg64 (&tmp, &tmp2, ptr); \
111 } while (failure != 0); \
116 OP_AND_FETCH_WORD64 (add
, , +)
117 OP_AND_FETCH_WORD64 (sub
, , -)
118 OP_AND_FETCH_WORD64 (or, , |)
119 OP_AND_FETCH_WORD64 (and, , &)
120 OP_AND_FETCH_WORD64 (xor, , ^)
121 OP_AND_FETCH_WORD64 (nand
, ~, &)
124 __sync_val_compare_and_swap_8 (long long *ptr
, long long oldval
,
128 long long actual_oldval
;
132 actual_oldval
= *ptr
;
134 if (__builtin_expect (oldval
!= actual_oldval
, 0))
135 return actual_oldval
;
137 failure
= __kernel_cmpxchg64 (&actual_oldval
, &newval
, ptr
);
139 if (__builtin_expect (!failure
, 1))
144 typedef unsigned char bool;
147 __sync_bool_compare_and_swap_8 (long long *ptr
, long long oldval
,
150 int failure
= __kernel_cmpxchg64 (&oldval
, &newval
, ptr
);
151 return (failure
== 0);
155 __sync_lock_test_and_set_8 (long long *ptr
, long long val
)
162 failure
= __kernel_cmpxchg64 (&oldval
, &val
, ptr
);
163 } while (failure
!= 0);