blob: a60e4fbac84b262e2d4c0b2e102d20e9759a651c [file] [log] [blame]
xf.libdd93d52023-05-12 07:10:14 -07001/* Low-level functions for atomic operations. Mips version.
2 Copyright (C) 2005-2016 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#ifndef _MIPS_ATOMIC_MACHINE_H
20#define _MIPS_ATOMIC_MACHINE_H 1
21
22#include <stdint.h>
23#include <inttypes.h>
24#include <sgidefs.h>
25
26typedef int32_t atomic32_t;
27typedef uint32_t uatomic32_t;
28typedef int_fast32_t atomic_fast32_t;
29typedef uint_fast32_t uatomic_fast32_t;
30
31typedef int64_t atomic64_t;
32typedef uint64_t uatomic64_t;
33typedef int_fast64_t atomic_fast64_t;
34typedef uint_fast64_t uatomic_fast64_t;
35
36typedef intptr_t atomicptr_t;
37typedef uintptr_t uatomicptr_t;
38typedef intmax_t atomic_max_t;
39typedef uintmax_t uatomic_max_t;
40
41#if _MIPS_SIM == _ABIO32 && __mips < 2
42#define MIPS_PUSH_MIPS2 ".set mips2\n\t"
43#else
44#define MIPS_PUSH_MIPS2
45#endif
46
47#if _MIPS_SIM == _ABIO32 || _MIPS_SIM == _ABIN32
48#define __HAVE_64B_ATOMICS 0
49#else
50#define __HAVE_64B_ATOMICS 1
51#endif
52
53/* See the comments in <sys/asm.h> about the use of the sync instruction. */
54#ifndef MIPS_SYNC
55# define MIPS_SYNC sync
56#endif
57
58/* Certain revisions of the R10000 Processor need an LL/SC Workaround
59 enabled. Revisions before 3.0 misbehave on atomic operations, and
60 Revs 2.6 and lower deadlock after several seconds due to other errata.
61
62 To quote the R10K Errata:
63 Workaround: The basic idea is to inhibit the four instructions
64 from simultaneously becoming active in R10000. Padding all
65 ll/sc sequences with nops or changing the looping branch in the
66 routines to a branch likely (which is always predicted taken
67 by R10000) will work. The nops should go after the loop, and the
68 number of them should be 28. This number could be decremented for
69 each additional instruction in the ll/sc loop such as the lock
70 modifier(s) between the ll and sc, the looping branch and its
71 delay slot. For typical short routines with one ll/sc loop, any
72 instructions after the loop could also count as a decrement. The
73 nop workaround pollutes the cache more but would be a few cycles
74 faster if all the code is in the cache and the looping branch
75 is predicted not taken. */
76
77
78#ifdef _MIPS_ARCH_R10000
79#define R10K_BEQZ_INSN "beqzl"
80#else
81#define R10K_BEQZ_INSN "beqz"
82#endif
83
84#define MIPS_SYNC_STR_2(X) #X
85#define MIPS_SYNC_STR_1(X) MIPS_SYNC_STR_2(X)
86#define MIPS_SYNC_STR MIPS_SYNC_STR_1(MIPS_SYNC)
87
88#if __GNUC_PREREQ (4, 8) || defined __mips16
89/* The __atomic_* builtins are available in GCC 4.7 and later, but MIPS
90 support for their efficient implementation was added only in GCC 4.8.
91 We still want to use them even with GCC 4.7 for MIPS16 code where we
92 have no assembly alternative available and want to avoid the __sync_*
93 builtins if at all possible. */
94
95#define USE_ATOMIC_COMPILER_BUILTINS 1
96
97/* Compare and exchange.
98 For all "bool" routines, we return FALSE if exchange succesful. */
99
100# define __arch_compare_and_exchange_bool_8_int(mem, newval, oldval, model) \
101 (abort (), 0)
102
103# define __arch_compare_and_exchange_bool_16_int(mem, newval, oldval, model) \
104 (abort (), 0)
105
106# define __arch_compare_and_exchange_bool_32_int(mem, newval, oldval, model) \
107 ({ \
108 typeof (*mem) __oldval = (oldval); \
109 !__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
110 model, __ATOMIC_RELAXED); \
111 })
112
113# define __arch_compare_and_exchange_val_8_int(mem, newval, oldval, model) \
114 (abort (), (typeof(*mem)) 0)
115
116# define __arch_compare_and_exchange_val_16_int(mem, newval, oldval, model) \
117 (abort (), (typeof(*mem)) 0)
118
119# define __arch_compare_and_exchange_val_32_int(mem, newval, oldval, model) \
120 ({ \
121 typeof (*mem) __oldval = (oldval); \
122 __atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
123 model, __ATOMIC_RELAXED); \
124 __oldval; \
125 })
126
127# if _MIPS_SIM == _ABIO32
128 /* We can't do an atomic 64-bit operation in O32. */
129# define __arch_compare_and_exchange_bool_64_int(mem, newval, oldval, model) \
130 (abort (), 0)
131# define __arch_compare_and_exchange_val_64_int(mem, newval, oldval, model) \
132 (abort (), (typeof(*mem)) 0)
133# else
134# define __arch_compare_and_exchange_bool_64_int(mem, newval, oldval, model) \
135 __arch_compare_and_exchange_bool_32_int (mem, newval, oldval, model)
136# define __arch_compare_and_exchange_val_64_int(mem, newval, oldval, model) \
137 __arch_compare_and_exchange_val_32_int (mem, newval, oldval, model)
138# endif
139
140/* Compare and exchange with "acquire" semantics, ie barrier after. */
141
142# define atomic_compare_and_exchange_bool_acq(mem, new, old) \
143 __atomic_bool_bysize (__arch_compare_and_exchange_bool, int, \
144 mem, new, old, __ATOMIC_ACQUIRE)
145
146# define atomic_compare_and_exchange_val_acq(mem, new, old) \
147 __atomic_val_bysize (__arch_compare_and_exchange_val, int, \
148 mem, new, old, __ATOMIC_ACQUIRE)
149
150/* Compare and exchange with "release" semantics, ie barrier before. */
151
152# define atomic_compare_and_exchange_bool_rel(mem, new, old) \
153 __atomic_bool_bysize (__arch_compare_and_exchange_bool, int, \
154 mem, new, old, __ATOMIC_RELEASE)
155
156# define atomic_compare_and_exchange_val_rel(mem, new, old) \
157 __atomic_val_bysize (__arch_compare_and_exchange_val, int, \
158 mem, new, old, __ATOMIC_RELEASE)
159
160
161/* Atomic exchange (without compare). */
162
163# define __arch_exchange_8_int(mem, newval, model) \
164 (abort (), (typeof(*mem)) 0)
165
166# define __arch_exchange_16_int(mem, newval, model) \
167 (abort (), (typeof(*mem)) 0)
168
169# define __arch_exchange_32_int(mem, newval, model) \
170 __atomic_exchange_n (mem, newval, model)
171
172# if _MIPS_SIM == _ABIO32
173/* We can't do an atomic 64-bit operation in O32. */
174# define __arch_exchange_64_int(mem, newval, model) \
175 (abort (), (typeof(*mem)) 0)
176# else
177# define __arch_exchange_64_int(mem, newval, model) \
178 __atomic_exchange_n (mem, newval, model)
179# endif
180
181# define atomic_exchange_acq(mem, value) \
182 __atomic_val_bysize (__arch_exchange, int, mem, value, __ATOMIC_ACQUIRE)
183
184# define atomic_exchange_rel(mem, value) \
185 __atomic_val_bysize (__arch_exchange, int, mem, value, __ATOMIC_RELEASE)
186
187
188/* Atomically add value and return the previous (unincremented) value. */
189
190# define __arch_exchange_and_add_8_int(mem, value, model) \
191 (abort (), (typeof(*mem)) 0)
192
193# define __arch_exchange_and_add_16_int(mem, value, model) \
194 (abort (), (typeof(*mem)) 0)
195
196# define __arch_exchange_and_add_32_int(mem, value, model) \
197 __atomic_fetch_add (mem, value, model)
198
199# if _MIPS_SIM == _ABIO32
200/* We can't do an atomic 64-bit operation in O32. */
201# define __arch_exchange_and_add_64_int(mem, value, model) \
202 (abort (), (typeof(*mem)) 0)
203# else
204# define __arch_exchange_and_add_64_int(mem, value, model) \
205 __atomic_fetch_add (mem, value, model)
206# endif
207
208# define atomic_exchange_and_add_acq(mem, value) \
209 __atomic_val_bysize (__arch_exchange_and_add, int, mem, value, \
210 __ATOMIC_ACQUIRE)
211
212# define atomic_exchange_and_add_rel(mem, value) \
213 __atomic_val_bysize (__arch_exchange_and_add, int, mem, value, \
214 __ATOMIC_RELEASE)
215
216#else /* !__mips16 && !__GNUC_PREREQ (4, 8) */
217/* This implementation using inline assembly will be removed once glibc
218 requires GCC 4.8 or later to build. */
219
220#define USE_ATOMIC_COMPILER_BUILTINS 0
221
222/* Compare and exchange. For all of the "xxx" routines, we expect a
223 "__prev" and a "__cmp" variable to be provided by the enclosing scope,
224 in which values are returned. */
225
226# define __arch_compare_and_exchange_xxx_8_int(mem, newval, oldval, rel, acq) \
227 (abort (), __prev = 0, __cmp = 0, (void) __cmp)
228
229# define __arch_compare_and_exchange_xxx_16_int(mem, newval, oldval, rel, acq) \
230 (abort (), __prev = 0, __cmp = 0, (void) __cmp)
231
232# define __arch_compare_and_exchange_xxx_32_int(mem, newval, oldval, rel, acq) \
233 __asm__ __volatile__ ( \
234 ".set push\n\t" \
235 MIPS_PUSH_MIPS2 \
236 rel "\n" \
237 "1:\t" \
238 "ll %0,%5\n\t" \
239 "move %1,$0\n\t" \
240 "bne %0,%3,2f\n\t" \
241 "move %1,%4\n\t" \
242 "sc %1,%2\n\t" \
243 R10K_BEQZ_INSN" %1,1b\n" \
244 acq "\n\t" \
245 ".set pop\n" \
246 "2:\n\t" \
247 : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
248 : "r" (oldval), "r" (newval), "m" (*mem) \
249 : "memory")
250
251# if _MIPS_SIM == _ABIO32
252/* We can't do an atomic 64-bit operation in O32. */
253# define __arch_compare_and_exchange_xxx_64_int(mem, newval, oldval, rel, acq) \
254 (abort (), __prev = 0, __cmp = 0, (void) __cmp)
255# else
256# define __arch_compare_and_exchange_xxx_64_int(mem, newval, oldval, rel, acq) \
257 __asm__ __volatile__ ("\n" \
258 ".set push\n\t" \
259 MIPS_PUSH_MIPS2 \
260 rel "\n" \
261 "1:\t" \
262 "lld %0,%5\n\t" \
263 "move %1,$0\n\t" \
264 "bne %0,%3,2f\n\t" \
265 "move %1,%4\n\t" \
266 "scd %1,%2\n\t" \
267 R10K_BEQZ_INSN" %1,1b\n" \
268 acq "\n\t" \
269 ".set pop\n" \
270 "2:\n\t" \
271 : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
272 : "r" (oldval), "r" (newval), "m" (*mem) \
273 : "memory")
274# endif
275
276/* For all "bool" routines, we return FALSE if exchange succesful. */
277
278# define __arch_compare_and_exchange_bool_8_int(mem, new, old, rel, acq) \
279({ typeof (*mem) __prev __attribute__ ((unused)); int __cmp; \
280 __arch_compare_and_exchange_xxx_8_int(mem, new, old, rel, acq); \
281 !__cmp; })
282
283# define __arch_compare_and_exchange_bool_16_int(mem, new, old, rel, acq) \
284({ typeof (*mem) __prev __attribute__ ((unused)); int __cmp; \
285 __arch_compare_and_exchange_xxx_16_int(mem, new, old, rel, acq); \
286 !__cmp; })
287
288# define __arch_compare_and_exchange_bool_32_int(mem, new, old, rel, acq) \
289({ typeof (*mem) __prev __attribute__ ((unused)); int __cmp; \
290 __arch_compare_and_exchange_xxx_32_int(mem, new, old, rel, acq); \
291 !__cmp; })
292
293# define __arch_compare_and_exchange_bool_64_int(mem, new, old, rel, acq) \
294({ typeof (*mem) __prev __attribute__ ((unused)); int __cmp; \
295 __arch_compare_and_exchange_xxx_64_int(mem, new, old, rel, acq); \
296 !__cmp; })
297
298/* For all "val" routines, return the old value whether exchange
299 successful or not. */
300
301# define __arch_compare_and_exchange_val_8_int(mem, new, old, rel, acq) \
302({ typeof (*mem) __prev; int __cmp; \
303 __arch_compare_and_exchange_xxx_8_int(mem, new, old, rel, acq); \
304 (typeof (*mem))__prev; })
305
306# define __arch_compare_and_exchange_val_16_int(mem, new, old, rel, acq) \
307({ typeof (*mem) __prev; int __cmp; \
308 __arch_compare_and_exchange_xxx_16_int(mem, new, old, rel, acq); \
309 (typeof (*mem))__prev; })
310
311# define __arch_compare_and_exchange_val_32_int(mem, new, old, rel, acq) \
312({ typeof (*mem) __prev; int __cmp; \
313 __arch_compare_and_exchange_xxx_32_int(mem, new, old, rel, acq); \
314 (typeof (*mem))__prev; })
315
316# define __arch_compare_and_exchange_val_64_int(mem, new, old, rel, acq) \
317({ typeof (*mem) __prev; int __cmp; \
318 __arch_compare_and_exchange_xxx_64_int(mem, new, old, rel, acq); \
319 (typeof (*mem))__prev; })
320
321/* Compare and exchange with "acquire" semantics, ie barrier after. */
322
323# define atomic_compare_and_exchange_bool_acq(mem, new, old) \
324 __atomic_bool_bysize (__arch_compare_and_exchange_bool, int, \
325 mem, new, old, "", MIPS_SYNC_STR)
326
327# define atomic_compare_and_exchange_val_acq(mem, new, old) \
328 __atomic_val_bysize (__arch_compare_and_exchange_val, int, \
329 mem, new, old, "", MIPS_SYNC_STR)
330
331/* Compare and exchange with "release" semantics, ie barrier before. */
332
333# define atomic_compare_and_exchange_bool_rel(mem, new, old) \
334 __atomic_bool_bysize (__arch_compare_and_exchange_bool, int, \
335 mem, new, old, MIPS_SYNC_STR, "")
336
337# define atomic_compare_and_exchange_val_rel(mem, new, old) \
338 __atomic_val_bysize (__arch_compare_and_exchange_val, int, \
339 mem, new, old, MIPS_SYNC_STR, "")
340
341
342
343/* Atomic exchange (without compare). */
344
345# define __arch_exchange_xxx_8_int(mem, newval, rel, acq) \
346 (abort (), (typeof(*mem)) 0)
347
348# define __arch_exchange_xxx_16_int(mem, newval, rel, acq) \
349 (abort (), (typeof(*mem)) 0)
350
351# define __arch_exchange_xxx_32_int(mem, newval, rel, acq) \
352({ typeof (*mem) __prev; int __cmp; \
353 __asm__ __volatile__ ("\n" \
354 ".set push\n\t" \
355 MIPS_PUSH_MIPS2 \
356 rel "\n" \
357 "1:\t" \
358 "ll %0,%4\n\t" \
359 "move %1,%3\n\t" \
360 "sc %1,%2\n\t" \
361 R10K_BEQZ_INSN" %1,1b\n" \
362 acq "\n\t" \
363 ".set pop\n" \
364 "2:\n\t" \
365 : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
366 : "r" (newval), "m" (*mem) \
367 : "memory"); \
368 __prev; })
369
370# if _MIPS_SIM == _ABIO32
371/* We can't do an atomic 64-bit operation in O32. */
372# define __arch_exchange_xxx_64_int(mem, newval, rel, acq) \
373 (abort (), (typeof(*mem)) 0)
374# else
375# define __arch_exchange_xxx_64_int(mem, newval, rel, acq) \
376({ typeof (*mem) __prev; int __cmp; \
377 __asm__ __volatile__ ("\n" \
378 ".set push\n\t" \
379 MIPS_PUSH_MIPS2 \
380 rel "\n" \
381 "1:\n" \
382 "lld %0,%4\n\t" \
383 "move %1,%3\n\t" \
384 "scd %1,%2\n\t" \
385 R10K_BEQZ_INSN" %1,1b\n" \
386 acq "\n\t" \
387 ".set pop\n" \
388 "2:\n\t" \
389 : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
390 : "r" (newval), "m" (*mem) \
391 : "memory"); \
392 __prev; })
393# endif
394
395# define atomic_exchange_acq(mem, value) \
396 __atomic_val_bysize (__arch_exchange_xxx, int, mem, value, "", MIPS_SYNC_STR)
397
398# define atomic_exchange_rel(mem, value) \
399 __atomic_val_bysize (__arch_exchange_xxx, int, mem, value, MIPS_SYNC_STR, "")
400
401
402/* Atomically add value and return the previous (unincremented) value. */
403
404# define __arch_exchange_and_add_8_int(mem, newval, rel, acq) \
405 (abort (), (typeof(*mem)) 0)
406
407# define __arch_exchange_and_add_16_int(mem, newval, rel, acq) \
408 (abort (), (typeof(*mem)) 0)
409
410# define __arch_exchange_and_add_32_int(mem, value, rel, acq) \
411({ typeof (*mem) __prev; int __cmp; \
412 __asm__ __volatile__ ("\n" \
413 ".set push\n\t" \
414 MIPS_PUSH_MIPS2 \
415 rel "\n" \
416 "1:\t" \
417 "ll %0,%4\n\t" \
418 "addu %1,%0,%3\n\t" \
419 "sc %1,%2\n\t" \
420 R10K_BEQZ_INSN" %1,1b\n" \
421 acq "\n\t" \
422 ".set pop\n" \
423 "2:\n\t" \
424 : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
425 : "r" (value), "m" (*mem) \
426 : "memory"); \
427 __prev; })
428
429# if _MIPS_SIM == _ABIO32
430/* We can't do an atomic 64-bit operation in O32. */
431# define __arch_exchange_and_add_64_int(mem, value, rel, acq) \
432 (abort (), (typeof(*mem)) 0)
433# else
434# define __arch_exchange_and_add_64_int(mem, value, rel, acq) \
435({ typeof (*mem) __prev; int __cmp; \
436 __asm__ __volatile__ ( \
437 ".set push\n\t" \
438 MIPS_PUSH_MIPS2 \
439 rel "\n" \
440 "1:\t" \
441 "lld %0,%4\n\t" \
442 "daddu %1,%0,%3\n\t" \
443 "scd %1,%2\n\t" \
444 R10K_BEQZ_INSN" %1,1b\n" \
445 acq "\n\t" \
446 ".set pop\n" \
447 "2:\n\t" \
448 : "=&r" (__prev), "=&r" (__cmp), "=m" (*mem) \
449 : "r" (value), "m" (*mem) \
450 : "memory"); \
451 __prev; })
452# endif
453
454# define atomic_exchange_and_add_acq(mem, value) \
455 __atomic_val_bysize (__arch_exchange_and_add, int, mem, value, \
456 "", MIPS_SYNC_STR)
457
458# define atomic_exchange_and_add_rel(mem, value) \
459 __atomic_val_bysize (__arch_exchange_and_add, int, mem, value, \
460 MIPS_SYNC_STR, "")
461
462#endif /* !__mips16 && !__GNUC_PREREQ (4, 8) */
463
464/* TODO: More atomic operations could be implemented efficiently; only the
465 basic requirements are done. */
466
467#ifdef __mips16
468# define atomic_full_barrier() __sync_synchronize ()
469
470#else /* !__mips16 */
471# define atomic_full_barrier() \
472 __asm__ __volatile__ (".set push\n\t" \
473 MIPS_PUSH_MIPS2 \
474 MIPS_SYNC_STR "\n\t" \
475 ".set pop" : : : "memory")
476#endif /* !__mips16 */
477
478#endif /* atomic-machine.h */