]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/arm/atomic-machine.h
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / arm / atomic-machine.h
1 /* Atomic operations. ARM/Linux version.
2 Copyright (C) 2002-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <stdint.h>
20
21 /* If the compiler doesn't provide a primitive, we'll use this macro
22 to get assistance from the kernel. */
23 #ifdef __thumb2__
24 # define __arm_assisted_full_barrier() \
25 __asm__ __volatile__ \
26 ("movw\tip, #0x0fa0\n\t" \
27 "movt\tip, #0xffff\n\t" \
28 "blx\tip" \
29 : : : "ip", "lr", "cc", "memory");
30 #else
31 # define __arm_assisted_full_barrier() \
32 __asm__ __volatile__ \
33 ("mov\tip, #0xffff0fff\n\t" \
34 "mov\tlr, pc\n\t" \
35 "add\tpc, ip, #(0xffff0fa0 - 0xffff0fff)" \
36 : : : "ip", "lr", "cc", "memory");
37 #endif
38
39 /* Atomic compare and exchange. This sequence relies on the kernel to
40 provide a compare and exchange operation which is atomic on the
41 current architecture, either via cleverness on pre-ARMv6 or via
42 ldrex / strex on ARMv6.
43
44 It doesn't matter what register is used for a_oldval2, but we must
45 specify one to work around GCC PR rtl-optimization/21223. Otherwise
46 it may cause a_oldval or a_tmp to be moved to a different register.
47
48 We use the union trick rather than simply using __typeof (...) in the
49 declarations of A_OLDVAL et al because when NEWVAL or OLDVAL is of the
50 form *PTR and PTR has a 'volatile ... *' type, then __typeof (*PTR) has
51 a 'volatile ...' type and this triggers -Wvolatile-register-var to
52 complain about 'register volatile ... asm ("reg")'. */
53 #ifdef __thumb2__
54 /* Thumb-2 has ldrex/strex. However it does not have barrier instructions,
55 so we still need to use the kernel helper. */
56 # define __arm_assisted_compare_and_exchange_val_32_acq(mem, newval, oldval) \
57 ({ union { __typeof (oldval) a; uint32_t v; } oldval_arg = { .a = (oldval) };\
58 union { __typeof (newval) a; uint32_t v; } newval_arg = { .a = (newval) };\
59 register uint32_t a_oldval asm ("r0"); \
60 register uint32_t a_newval asm ("r1") = newval_arg.v; \
61 register __typeof (mem) a_ptr asm ("r2") = (mem); \
62 register uint32_t a_tmp asm ("r3"); \
63 register uint32_t a_oldval2 asm ("r4") = oldval_arg.v; \
64 __asm__ __volatile__ \
65 ("0:\tldr\t%[tmp],[%[ptr]]\n\t" \
66 "cmp\t%[tmp], %[old2]\n\t" \
67 "bne\t1f\n\t" \
68 "mov\t%[old], %[old2]\n\t" \
69 "movw\t%[tmp], #0x0fc0\n\t" \
70 "movt\t%[tmp], #0xffff\n\t" \
71 "blx\t%[tmp]\n\t" \
72 "bcc\t0b\n\t" \
73 "mov\t%[tmp], %[old2]\n\t" \
74 "1:" \
75 : [old] "=&r" (a_oldval), [tmp] "=&r" (a_tmp) \
76 : [new] "r" (a_newval), [ptr] "r" (a_ptr), \
77 [old2] "r" (a_oldval2) \
78 : "ip", "lr", "cc", "memory"); \
79 (__typeof (oldval)) a_tmp; })
80 #else
81 # define __arm_assisted_compare_and_exchange_val_32_acq(mem, newval, oldval) \
82 ({ union { __typeof (oldval) a; uint32_t v; } oldval_arg = { .a = (oldval) };\
83 union { __typeof (newval) a; uint32_t v; } newval_arg = { .a = (newval) };\
84 register uint32_t a_oldval asm ("r0"); \
85 register uint32_t a_newval asm ("r1") = newval_arg.v; \
86 register __typeof (mem) a_ptr asm ("r2") = (mem); \
87 register uint32_t a_tmp asm ("r3"); \
88 register uint32_t a_oldval2 asm ("r4") = oldval_arg.v; \
89 __asm__ __volatile__ \
90 ("0:\tldr\t%[tmp],[%[ptr]]\n\t" \
91 "cmp\t%[tmp], %[old2]\n\t" \
92 "bne\t1f\n\t" \
93 "mov\t%[old], %[old2]\n\t" \
94 "mov\t%[tmp], #0xffff0fff\n\t" \
95 "mov\tlr, pc\n\t" \
96 "add\tpc, %[tmp], #(0xffff0fc0 - 0xffff0fff)\n\t" \
97 "bcc\t0b\n\t" \
98 "mov\t%[tmp], %[old2]\n\t" \
99 "1:" \
100 : [old] "=&r" (a_oldval), [tmp] "=&r" (a_tmp) \
101 : [new] "r" (a_newval), [ptr] "r" (a_ptr), \
102 [old2] "r" (a_oldval2) \
103 : "ip", "lr", "cc", "memory"); \
104 (__typeof (oldval)) a_tmp; })
105 #endif
106
107 #include <sysdeps/arm/atomic-machine.h>