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
120 #define atomic_cmpset_short atomic_cmpset_16
121 #define atomic_cmpset_acq_short atomic_cmpset_acq_16
122 #define atomic_cmpset_rel_short atomic_cmpset_rel_16
123 #define atomic_fcmpset_short atomic_fcmpset_16
124 #define atomic_fcmpset_acq_short atomic_fcmpset_acq_16
125 #define atomic_fcmpset_rel_short atomic_fcmpset_rel_16
128 atomic_add_32(volatile uint32_t *p, uint32_t val)
131 __asm __volatile("amoadd.w zero, %1, %0"
138 atomic_subtract_32(volatile uint32_t *p, uint32_t val)
141 __asm __volatile("amoadd.w zero, %1, %0"
148 atomic_set_32(volatile uint32_t *p, uint32_t val)
151 __asm __volatile("amoor.w zero, %1, %0"
158 atomic_clear_32(volatile uint32_t *p, uint32_t val)
161 __asm __volatile("amoand.w zero, %1, %0"
168 atomic_cmpset_32(volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
177 "li %1, 1\n" /* Preset to fail */
183 : "=&r" (tmp), "=&r" (res), "+A" (*p)
184 : "rJ" ((long)(int32_t)cmpval), "rJ" (newval)
191 atomic_fcmpset_32(volatile uint32_t *p, uint32_t *cmpval, uint32_t newval)
200 "li %1, 1\n" /* Preset to fail */
201 "lr.w %0, %2\n" /* Load old value */
202 "bne %0, %z4, 1f\n" /* Compare */
203 "sc.w %1, %z5, %2\n" /* Try to store new value */
206 "sw %0, %3\n" /* Save old value */
208 : "=&r" (tmp), "=&r" (res), "+A" (*p), "+A" (*cmpval)
209 : "rJ" ((long)(int32_t)*cmpval), "rJ" (newval)
215 static __inline uint32_t
216 atomic_fetchadd_32(volatile uint32_t *p, uint32_t val)
220 __asm __volatile("amoadd.w %0, %2, %1"
221 : "=&r" (ret), "+A" (*p)
228 static __inline uint32_t
229 atomic_readandclear_32(volatile uint32_t *p)
236 __asm __volatile("amoswap.w %0, %2, %1"
237 : "=&r"(ret), "+A" (*p)
244 #define atomic_add_int atomic_add_32
245 #define atomic_clear_int atomic_clear_32
246 #define atomic_cmpset_int atomic_cmpset_32
247 #define atomic_fcmpset_int atomic_fcmpset_32
248 #define atomic_fetchadd_int atomic_fetchadd_32
249 #define atomic_readandclear_int atomic_readandclear_32
250 #define atomic_set_int atomic_set_32
251 #define atomic_subtract_int atomic_subtract_32
253 ATOMIC_ACQ_REL(set, 32)
254 ATOMIC_ACQ_REL(clear, 32)
255 ATOMIC_ACQ_REL(add, 32)
256 ATOMIC_ACQ_REL(subtract, 32)
258 ATOMIC_CMPSET_ACQ_REL(32);
259 ATOMIC_FCMPSET_ACQ_REL(32);
261 static __inline uint32_t
262 atomic_load_acq_32(volatile uint32_t *p)
274 atomic_store_rel_32(volatile uint32_t *p, uint32_t val)
282 #define atomic_add_acq_int atomic_add_acq_32
283 #define atomic_clear_acq_int atomic_clear_acq_32
284 #define atomic_cmpset_acq_int atomic_cmpset_acq_32
285 #define atomic_fcmpset_acq_int atomic_fcmpset_acq_32
286 #define atomic_load_acq_int atomic_load_acq_32
287 #define atomic_set_acq_int atomic_set_acq_32
288 #define atomic_subtract_acq_int atomic_subtract_acq_32
290 #define atomic_add_rel_int atomic_add_rel_32
291 #define atomic_clear_rel_int atomic_clear_rel_32
292 #define atomic_cmpset_rel_int atomic_cmpset_rel_32
293 #define atomic_fcmpset_rel_int atomic_fcmpset_rel_32
294 #define atomic_set_rel_int atomic_set_rel_32
295 #define atomic_subtract_rel_int atomic_subtract_rel_32
296 #define atomic_store_rel_int atomic_store_rel_32
299 atomic_add_64(volatile uint64_t *p, uint64_t val)
302 __asm __volatile("amoadd.d zero, %1, %0"
309 atomic_subtract_64(volatile uint64_t *p, uint64_t val)
312 __asm __volatile("amoadd.d zero, %1, %0"
319 atomic_set_64(volatile uint64_t *p, uint64_t val)
322 __asm __volatile("amoor.d zero, %1, %0"
329 atomic_clear_64(volatile uint64_t *p, uint64_t val)
332 __asm __volatile("amoand.d zero, %1, %0"
339 atomic_cmpset_64(volatile uint64_t *p, uint64_t cmpval, uint64_t newval)
348 "li %1, 1\n" /* Preset to fail */
354 : "=&r" (tmp), "=&r" (res), "+A" (*p)
355 : "rJ" (cmpval), "rJ" (newval)
362 atomic_fcmpset_64(volatile uint64_t *p, uint64_t *cmpval, uint64_t newval)
371 "li %1, 1\n" /* Preset to fail */
372 "lr.d %0, %2\n" /* Load old value */
373 "bne %0, %z4, 1f\n" /* Compare */
374 "sc.d %1, %z5, %2\n" /* Try to store new value */
377 "sd %0, %3\n" /* Save old value */
379 : "=&r" (tmp), "=&r" (res), "+A" (*p), "+A" (*cmpval)
380 : "rJ" (*cmpval), "rJ" (newval)
386 static __inline uint64_t
387 atomic_fetchadd_64(volatile uint64_t *p, uint64_t val)
391 __asm __volatile("amoadd.d %0, %2, %1"
392 : "=&r" (ret), "+A" (*p)
399 static __inline uint64_t
400 atomic_readandclear_64(volatile uint64_t *p)
407 __asm __volatile("amoswap.d %0, %2, %1"
408 : "=&r"(ret), "+A" (*p)
415 static __inline uint32_t
416 atomic_swap_32(volatile uint32_t *p, uint32_t val)
420 __asm __volatile("amoswap.w %0, %2, %1"
421 : "=&r"(old), "+A" (*p)
428 static __inline uint64_t
429 atomic_swap_64(volatile uint64_t *p, uint64_t val)
433 __asm __volatile("amoswap.d %0, %2, %1"
434 : "=&r"(old), "+A" (*p)
441 #define atomic_swap_int atomic_swap_32
443 #define atomic_add_long atomic_add_64
444 #define atomic_clear_long atomic_clear_64
445 #define atomic_cmpset_long atomic_cmpset_64
446 #define atomic_fcmpset_long atomic_fcmpset_64
447 #define atomic_fetchadd_long atomic_fetchadd_64
448 #define atomic_readandclear_long atomic_readandclear_64
449 #define atomic_set_long atomic_set_64
450 #define atomic_subtract_long atomic_subtract_64
451 #define atomic_swap_long atomic_swap_64
453 #define atomic_add_ptr atomic_add_64
454 #define atomic_clear_ptr atomic_clear_64
455 #define atomic_cmpset_ptr atomic_cmpset_64
456 #define atomic_fcmpset_ptr atomic_fcmpset_64
457 #define atomic_fetchadd_ptr atomic_fetchadd_64
458 #define atomic_readandclear_ptr atomic_readandclear_64
459 #define atomic_set_ptr atomic_set_64
460 #define atomic_subtract_ptr atomic_subtract_64
461 #define atomic_swap_ptr atomic_swap_64
463 ATOMIC_ACQ_REL(set, 64)
464 ATOMIC_ACQ_REL(clear, 64)
465 ATOMIC_ACQ_REL(add, 64)
466 ATOMIC_ACQ_REL(subtract, 64)
468 ATOMIC_CMPSET_ACQ_REL(64);
469 ATOMIC_FCMPSET_ACQ_REL(64);
471 static __inline uint64_t
472 atomic_load_acq_64(volatile uint64_t *p)
484 atomic_store_rel_64(volatile uint64_t *p, uint64_t val)
492 #define atomic_add_acq_long atomic_add_acq_64
493 #define atomic_clear_acq_long atomic_clear_acq_64
494 #define atomic_cmpset_acq_long atomic_cmpset_acq_64
495 #define atomic_fcmpset_acq_long atomic_fcmpset_acq_64
496 #define atomic_load_acq_long atomic_load_acq_64
497 #define atomic_set_acq_long atomic_set_acq_64
498 #define atomic_subtract_acq_long atomic_subtract_acq_64
500 #define atomic_add_acq_ptr atomic_add_acq_64
501 #define atomic_clear_acq_ptr atomic_clear_acq_64
502 #define atomic_cmpset_acq_ptr atomic_cmpset_acq_64
503 #define atomic_fcmpset_acq_ptr atomic_fcmpset_acq_64
504 #define atomic_load_acq_ptr atomic_load_acq_64
505 #define atomic_set_acq_ptr atomic_set_acq_64
506 #define atomic_subtract_acq_ptr atomic_subtract_acq_64
508 #undef ATOMIC_ACQ_REL
511 atomic_thread_fence_acq(void)
518 atomic_thread_fence_rel(void)
525 atomic_thread_fence_acq_rel(void)
532 atomic_thread_fence_seq_cst(void)
538 #define atomic_add_rel_long atomic_add_rel_64
539 #define atomic_clear_rel_long atomic_clear_rel_64
541 #define atomic_add_rel_long atomic_add_rel_64
542 #define atomic_clear_rel_long atomic_clear_rel_64
543 #define atomic_cmpset_rel_long atomic_cmpset_rel_64
544 #define atomic_fcmpset_rel_long atomic_fcmpset_rel_64
545 #define atomic_set_rel_long atomic_set_rel_64
546 #define atomic_subtract_rel_long atomic_subtract_rel_64
547 #define atomic_store_rel_long atomic_store_rel_64
549 #define atomic_add_rel_ptr atomic_add_rel_64
550 #define atomic_clear_rel_ptr atomic_clear_rel_64
551 #define atomic_cmpset_rel_ptr atomic_cmpset_rel_64
552 #define atomic_fcmpset_rel_ptr atomic_fcmpset_rel_64
553 #define atomic_set_rel_ptr atomic_set_rel_64
554 #define atomic_subtract_rel_ptr atomic_subtract_rel_64
555 #define atomic_store_rel_ptr atomic_store_rel_64
557 #include <sys/_atomic_subword.h>
559 #endif /* _MACHINE_ATOMIC_H_ */