2 * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
5 * Portions of this software were developed by SRI International and the
6 * University of Cambridge Computer Laboratory under DARPA/AFRL contract
7 * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Portions of this software were developed by the University of Cambridge
10 * Computer Laboratory as part of the CTSRD Project, with support from the
11 * UK Higher Education Innovation Fund (HEIF).
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #ifndef _MACHINE_ATOMIC_H_
38 #define _MACHINE_ATOMIC_H_
40 #include <sys/atomic_common.h>
42 #define fence() __asm __volatile("fence" ::: "memory");
47 static __inline int atomic_cmpset_8(__volatile uint8_t *, uint8_t, uint8_t);
48 static __inline int atomic_fcmpset_8(__volatile uint8_t *, uint8_t *, uint8_t);
49 static __inline int atomic_cmpset_16(__volatile uint16_t *, uint16_t, uint16_t);
50 static __inline int atomic_fcmpset_16(__volatile uint16_t *, uint16_t *,
53 #define ATOMIC_ACQ_REL(NAME, WIDTH) \
54 static __inline void \
55 atomic_##NAME##_acq_##WIDTH(__volatile uint##WIDTH##_t *p, uint##WIDTH##_t v)\
57 atomic_##NAME##_##WIDTH(p, v); \
61 static __inline void \
62 atomic_##NAME##_rel_##WIDTH(__volatile uint##WIDTH##_t *p, uint##WIDTH##_t v)\
65 atomic_##NAME##_##WIDTH(p, v); \
68 #define ATOMIC_CMPSET_ACQ_REL(WIDTH) \
70 atomic_cmpset_acq_##WIDTH(__volatile uint##WIDTH##_t *p, \
71 uint##WIDTH##_t cmpval, uint##WIDTH##_t newval) \
75 retval = atomic_cmpset_##WIDTH(p, cmpval, newval); \
81 atomic_cmpset_rel_##WIDTH(__volatile uint##WIDTH##_t *p, \
82 uint##WIDTH##_t cmpval, uint##WIDTH##_t newval) \
85 return (atomic_cmpset_##WIDTH(p, cmpval, newval)); \
88 #define ATOMIC_FCMPSET_ACQ_REL(WIDTH) \
90 atomic_fcmpset_acq_##WIDTH(__volatile uint##WIDTH##_t *p, \
91 uint##WIDTH##_t *cmpval, uint##WIDTH##_t newval) \
95 retval = atomic_fcmpset_##WIDTH(p, cmpval, newval); \
100 static __inline int \
101 atomic_fcmpset_rel_##WIDTH(__volatile uint##WIDTH##_t *p, \
102 uint##WIDTH##_t *cmpval, uint##WIDTH##_t newval) \
105 return (atomic_fcmpset_##WIDTH(p, cmpval, newval)); \
108 ATOMIC_CMPSET_ACQ_REL(8);
109 ATOMIC_FCMPSET_ACQ_REL(8);
110 ATOMIC_CMPSET_ACQ_REL(16);
111 ATOMIC_FCMPSET_ACQ_REL(16);
113 #define atomic_cmpset_char atomic_cmpset_8
114 #define atomic_cmpset_acq_char atomic_cmpset_acq_8
115 #define atomic_cmpset_rel_char atomic_cmpset_rel_8
116 #define atomic_fcmpset_char atomic_fcmpset_8
117 #define atomic_fcmpset_acq_char atomic_fcmpset_acq_8
118 #define atomic_fcmpset_rel_char atomic_fcmpset_rel_8
121 #define atomic_cmpset_short atomic_cmpset_16
122 #define atomic_cmpset_acq_short atomic_cmpset_acq_16
123 #define atomic_cmpset_rel_short atomic_cmpset_rel_16
124 #define atomic_fcmpset_short atomic_fcmpset_16
125 #define atomic_fcmpset_acq_short atomic_fcmpset_acq_16
126 #define atomic_fcmpset_rel_short atomic_fcmpset_rel_16
129 atomic_add_32(volatile uint32_t *p, uint32_t val)
132 __asm __volatile("amoadd.w zero, %1, %0"
139 atomic_subtract_32(volatile uint32_t *p, uint32_t val)
142 __asm __volatile("amoadd.w zero, %1, %0"
149 atomic_set_32(volatile uint32_t *p, uint32_t val)
152 __asm __volatile("amoor.w zero, %1, %0"
159 atomic_clear_32(volatile uint32_t *p, uint32_t val)
162 __asm __volatile("amoand.w zero, %1, %0"
169 atomic_cmpset_32(volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
178 "li %1, 1\n" /* Preset to fail */
184 : "=&r" (tmp), "=&r" (res), "+A" (*p)
185 : "rJ" ((long)(int32_t)cmpval), "rJ" (newval)
192 atomic_fcmpset_32(volatile uint32_t *p, uint32_t *cmpval, uint32_t newval)
201 "li %1, 1\n" /* Preset to fail */
202 "lr.w %0, %2\n" /* Load old value */
203 "bne %0, %z4, 1f\n" /* Compare */
204 "sc.w %1, %z5, %2\n" /* Try to store new value */
207 "sw %0, %3\n" /* Save old value */
209 : "=&r" (tmp), "=&r" (res), "+A" (*p), "+A" (*cmpval)
210 : "rJ" ((long)(int32_t)*cmpval), "rJ" (newval)
216 static __inline uint32_t
217 atomic_fetchadd_32(volatile uint32_t *p, uint32_t val)
221 __asm __volatile("amoadd.w %0, %2, %1"
222 : "=&r" (ret), "+A" (*p)
229 static __inline uint32_t
230 atomic_readandclear_32(volatile uint32_t *p)
237 __asm __volatile("amoswap.w %0, %2, %1"
238 : "=&r"(ret), "+A" (*p)
245 #define atomic_add_int atomic_add_32
246 #define atomic_clear_int atomic_clear_32
247 #define atomic_cmpset_int atomic_cmpset_32
248 #define atomic_fcmpset_int atomic_fcmpset_32
249 #define atomic_fetchadd_int atomic_fetchadd_32
250 #define atomic_readandclear_int atomic_readandclear_32
251 #define atomic_set_int atomic_set_32
252 #define atomic_subtract_int atomic_subtract_32
254 ATOMIC_ACQ_REL(set, 32)
255 ATOMIC_ACQ_REL(clear, 32)
256 ATOMIC_ACQ_REL(add, 32)
257 ATOMIC_ACQ_REL(subtract, 32)
259 ATOMIC_CMPSET_ACQ_REL(32);
260 ATOMIC_FCMPSET_ACQ_REL(32);
262 static __inline uint32_t
263 atomic_load_acq_32(volatile uint32_t *p)
275 atomic_store_rel_32(volatile uint32_t *p, uint32_t val)
283 #define atomic_add_acq_int atomic_add_acq_32
284 #define atomic_clear_acq_int atomic_clear_acq_32
285 #define atomic_cmpset_acq_int atomic_cmpset_acq_32
286 #define atomic_fcmpset_acq_int atomic_fcmpset_acq_32
287 #define atomic_load_acq_int atomic_load_acq_32
288 #define atomic_set_acq_int atomic_set_acq_32
289 #define atomic_subtract_acq_int atomic_subtract_acq_32
291 #define atomic_add_rel_int atomic_add_rel_32
292 #define atomic_clear_rel_int atomic_add_rel_32
293 #define atomic_cmpset_rel_int atomic_cmpset_rel_32
294 #define atomic_fcmpset_rel_int atomic_fcmpset_rel_32
295 #define atomic_set_rel_int atomic_set_rel_32
296 #define atomic_subtract_rel_int atomic_subtract_rel_32
297 #define atomic_store_rel_int atomic_store_rel_32
300 atomic_add_64(volatile uint64_t *p, uint64_t val)
303 __asm __volatile("amoadd.d zero, %1, %0"
310 atomic_subtract_64(volatile uint64_t *p, uint64_t val)
313 __asm __volatile("amoadd.d zero, %1, %0"
320 atomic_set_64(volatile uint64_t *p, uint64_t val)
323 __asm __volatile("amoor.d zero, %1, %0"
330 atomic_clear_64(volatile uint64_t *p, uint64_t val)
333 __asm __volatile("amoand.d zero, %1, %0"
340 atomic_cmpset_64(volatile uint64_t *p, uint64_t cmpval, uint64_t newval)
349 "li %1, 1\n" /* Preset to fail */
355 : "=&r" (tmp), "=&r" (res), "+A" (*p)
356 : "rJ" (cmpval), "rJ" (newval)
363 atomic_fcmpset_64(volatile uint64_t *p, uint64_t *cmpval, uint64_t newval)
372 "li %1, 1\n" /* Preset to fail */
373 "lr.d %0, %2\n" /* Load old value */
374 "bne %0, %z4, 1f\n" /* Compare */
375 "sc.d %1, %z5, %2\n" /* Try to store new value */
378 "sd %0, %3\n" /* Save old value */
380 : "=&r" (tmp), "=&r" (res), "+A" (*p), "+A" (*cmpval)
381 : "rJ" (*cmpval), "rJ" (newval)
387 static __inline uint64_t
388 atomic_fetchadd_64(volatile uint64_t *p, uint64_t val)
392 __asm __volatile("amoadd.d %0, %2, %1"
393 : "=&r" (ret), "+A" (*p)
400 static __inline uint64_t
401 atomic_readandclear_64(volatile uint64_t *p)
408 __asm __volatile("amoswap.d %0, %2, %1"
409 : "=&r"(ret), "+A" (*p)
416 static __inline uint32_t
417 atomic_swap_32(volatile uint32_t *p, uint32_t val)
421 __asm __volatile("amoswap.w %0, %2, %1"
422 : "=&r"(old), "+A" (*p)
429 static __inline uint64_t
430 atomic_swap_64(volatile uint64_t *p, uint64_t val)
434 __asm __volatile("amoswap.d %0, %2, %1"
435 : "=&r"(old), "+A" (*p)
442 #define atomic_swap_int atomic_swap_32
444 #define atomic_add_long atomic_add_64
445 #define atomic_clear_long atomic_clear_64
446 #define atomic_cmpset_long atomic_cmpset_64
447 #define atomic_fcmpset_long atomic_fcmpset_64
448 #define atomic_fetchadd_long atomic_fetchadd_64
449 #define atomic_readandclear_long atomic_readandclear_64
450 #define atomic_set_long atomic_set_64
451 #define atomic_subtract_long atomic_subtract_64
452 #define atomic_swap_long atomic_swap_64
454 #define atomic_add_ptr atomic_add_64
455 #define atomic_clear_ptr atomic_clear_64
456 #define atomic_cmpset_ptr atomic_cmpset_64
457 #define atomic_fcmpset_ptr atomic_fcmpset_64
458 #define atomic_fetchadd_ptr atomic_fetchadd_64
459 #define atomic_readandclear_ptr atomic_readandclear_64
460 #define atomic_set_ptr atomic_set_64
461 #define atomic_subtract_ptr atomic_subtract_64
462 #define atomic_swap_ptr atomic_swap_64
464 ATOMIC_ACQ_REL(set, 64)
465 ATOMIC_ACQ_REL(clear, 64)
466 ATOMIC_ACQ_REL(add, 64)
467 ATOMIC_ACQ_REL(subtract, 64)
469 ATOMIC_CMPSET_ACQ_REL(64);
470 ATOMIC_FCMPSET_ACQ_REL(64);
472 static __inline uint64_t
473 atomic_load_acq_64(volatile uint64_t *p)
485 atomic_store_rel_64(volatile uint64_t *p, uint64_t val)
493 #define atomic_add_acq_long atomic_add_acq_64
494 #define atomic_clear_acq_long atomic_add_acq_64
495 #define atomic_cmpset_acq_long atomic_cmpset_acq_64
496 #define atomic_fcmpset_acq_long atomic_fcmpset_acq_64
497 #define atomic_load_acq_long atomic_load_acq_64
498 #define atomic_set_acq_long atomic_set_acq_64
499 #define atomic_subtract_acq_long atomic_subtract_acq_64
501 #define atomic_add_acq_ptr atomic_add_acq_64
502 #define atomic_clear_acq_ptr atomic_add_acq_64
503 #define atomic_cmpset_acq_ptr atomic_cmpset_acq_64
504 #define atomic_fcmpset_acq_ptr atomic_fcmpset_acq_64
505 #define atomic_load_acq_ptr atomic_load_acq_64
506 #define atomic_set_acq_ptr atomic_set_acq_64
507 #define atomic_subtract_acq_ptr atomic_subtract_acq_64
509 #undef ATOMIC_ACQ_REL
512 atomic_thread_fence_acq(void)
519 atomic_thread_fence_rel(void)
526 atomic_thread_fence_acq_rel(void)
533 atomic_thread_fence_seq_cst(void)
539 #define atomic_add_rel_long atomic_add_rel_64
540 #define atomic_clear_rel_long atomic_clear_rel_64
542 #define atomic_add_rel_long atomic_add_rel_64
543 #define atomic_clear_rel_long atomic_clear_rel_64
544 #define atomic_cmpset_rel_long atomic_cmpset_rel_64
545 #define atomic_fcmpset_rel_long atomic_fcmpset_rel_64
546 #define atomic_set_rel_long atomic_set_rel_64
547 #define atomic_subtract_rel_long atomic_subtract_rel_64
548 #define atomic_store_rel_long atomic_store_rel_64
550 #define atomic_add_rel_ptr atomic_add_rel_64
551 #define atomic_clear_rel_ptr atomic_clear_rel_64
552 #define atomic_cmpset_rel_ptr atomic_cmpset_rel_64
553 #define atomic_fcmpset_rel_ptr atomic_fcmpset_rel_64
554 #define atomic_set_rel_ptr atomic_set_rel_64
555 #define atomic_subtract_rel_ptr atomic_subtract_rel_64
556 #define atomic_store_rel_ptr atomic_store_rel_64
558 #include <sys/_atomic_subword.h>
560 #endif /* _MACHINE_ATOMIC_H_ */