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 #define fence() __asm __volatile("fence" ::: "memory");
45 #define ATOMIC_ACQ_REL(NAME, WIDTH) \
46 static __inline void \
47 atomic_##NAME##_acq_##WIDTH(__volatile uint##WIDTH##_t *p, uint##WIDTH##_t v)\
49 atomic_##NAME##_##WIDTH(p, v); \
53 static __inline void \
54 atomic_##NAME##_rel_##WIDTH(__volatile uint##WIDTH##_t *p, uint##WIDTH##_t v)\
57 atomic_##NAME##_##WIDTH(p, v); \
61 atomic_add_32(volatile uint32_t *p, uint32_t val)
64 __asm __volatile("amoadd.w zero, %1, %0"
71 atomic_subtract_32(volatile uint32_t *p, uint32_t val)
74 __asm __volatile("amoadd.w zero, %1, %0"
81 atomic_set_32(volatile uint32_t *p, uint32_t val)
84 __asm __volatile("amoor.w zero, %1, %0"
91 atomic_clear_32(volatile uint32_t *p, uint32_t val)
94 __asm __volatile("amoand.w zero, %1, %0"
101 atomic_cmpset_32(volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
110 "li %1, 1\n" /* Preset to fail */
116 : "=&r" (tmp), "=&r" (res), "+A" (*p)
117 : "rJ" (cmpval), "rJ" (newval)
124 atomic_fcmpset_32(volatile uint32_t *p, uint32_t *cmpval, uint32_t newval)
133 "li %1, 1\n" /* Preset to fail */
134 "lr.w %0, %2\n" /* Load old value */
135 "bne %0, %z4, 1f\n" /* Compare */
136 "sc.w %1, %z5, %2\n" /* Try to store new value */
139 "sw %0, %3\n" /* Save old value */
141 : "=&r" (tmp), "=&r" (res), "+A" (*p), "+A" (*cmpval)
142 : "rJ" (*cmpval), "rJ" (newval)
148 static __inline uint32_t
149 atomic_fetchadd_32(volatile uint32_t *p, uint32_t val)
153 __asm __volatile("amoadd.w %0, %2, %1"
154 : "=&r" (ret), "+A" (*p)
161 static __inline uint32_t
162 atomic_readandclear_32(volatile uint32_t *p)
169 __asm __volatile("amoswap.w %0, %2, %1"
170 : "=&r"(ret), "+A" (*p)
177 #define atomic_add_int atomic_add_32
178 #define atomic_clear_int atomic_clear_32
179 #define atomic_cmpset_int atomic_cmpset_32
180 #define atomic_fcmpset_int atomic_fcmpset_32
181 #define atomic_fetchadd_int atomic_fetchadd_32
182 #define atomic_readandclear_int atomic_readandclear_32
183 #define atomic_set_int atomic_set_32
184 #define atomic_subtract_int atomic_subtract_32
186 ATOMIC_ACQ_REL(set, 32)
187 ATOMIC_ACQ_REL(clear, 32)
188 ATOMIC_ACQ_REL(add, 32)
189 ATOMIC_ACQ_REL(subtract, 32)
192 atomic_cmpset_acq_32(volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
196 res = atomic_cmpset_32(p, cmpval, newval);
204 atomic_cmpset_rel_32(volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
209 return (atomic_cmpset_32(p, cmpval, newval));
213 atomic_fcmpset_acq_32(volatile uint32_t *p, uint32_t *cmpval, uint32_t newval)
217 res = atomic_fcmpset_32(p, cmpval, newval);
225 atomic_fcmpset_rel_32(volatile uint32_t *p, uint32_t *cmpval, uint32_t newval)
230 return (atomic_fcmpset_32(p, cmpval, newval));
233 static __inline uint32_t
234 atomic_load_acq_32(volatile uint32_t *p)
246 atomic_store_rel_32(volatile uint32_t *p, uint32_t val)
254 #define atomic_add_acq_int atomic_add_acq_32
255 #define atomic_clear_acq_int atomic_clear_acq_32
256 #define atomic_cmpset_acq_int atomic_cmpset_acq_32
257 #define atomic_fcmpset_acq_int atomic_fcmpset_acq_32
258 #define atomic_load_acq_int atomic_load_acq_32
259 #define atomic_set_acq_int atomic_set_acq_32
260 #define atomic_subtract_acq_int atomic_subtract_acq_32
262 #define atomic_add_rel_int atomic_add_rel_32
263 #define atomic_clear_rel_int atomic_add_rel_32
264 #define atomic_cmpset_rel_int atomic_cmpset_rel_32
265 #define atomic_fcmpset_rel_int atomic_fcmpset_rel_32
266 #define atomic_set_rel_int atomic_set_rel_32
267 #define atomic_subtract_rel_int atomic_subtract_rel_32
268 #define atomic_store_rel_int atomic_store_rel_32
271 atomic_add_64(volatile uint64_t *p, uint64_t val)
274 __asm __volatile("amoadd.d zero, %1, %0"
281 atomic_subtract_64(volatile uint64_t *p, uint64_t val)
284 __asm __volatile("amoadd.d zero, %1, %0"
291 atomic_set_64(volatile uint64_t *p, uint64_t val)
294 __asm __volatile("amoor.d zero, %1, %0"
301 atomic_clear_64(volatile uint64_t *p, uint64_t val)
304 __asm __volatile("amoand.d zero, %1, %0"
311 atomic_cmpset_64(volatile uint64_t *p, uint64_t cmpval, uint64_t newval)
320 "li %1, 1\n" /* Preset to fail */
326 : "=&r" (tmp), "=&r" (res), "+A" (*p)
327 : "rJ" (cmpval), "rJ" (newval)
334 atomic_fcmpset_64(volatile uint64_t *p, uint64_t *cmpval, uint64_t newval)
343 "li %1, 1\n" /* Preset to fail */
344 "lr.d %0, %2\n" /* Load old value */
345 "bne %0, %z4, 1f\n" /* Compare */
346 "sc.d %1, %z5, %2\n" /* Try to store new value */
349 "sd %0, %3\n" /* Save old value */
351 : "=&r" (tmp), "=&r" (res), "+A" (*p), "+A" (*cmpval)
352 : "rJ" (*cmpval), "rJ" (newval)
358 static __inline uint64_t
359 atomic_fetchadd_64(volatile uint64_t *p, uint64_t val)
363 __asm __volatile("amoadd.d %0, %2, %1"
364 : "=&r" (ret), "+A" (*p)
371 static __inline uint64_t
372 atomic_readandclear_64(volatile uint64_t *p)
379 __asm __volatile("amoswap.d %0, %2, %1"
380 : "=&r"(ret), "+A" (*p)
387 static __inline uint32_t
388 atomic_swap_32(volatile uint32_t *p, uint32_t val)
392 __asm __volatile("amoswap.w %0, %2, %1"
393 : "=&r"(old), "+A" (*p)
400 static __inline uint64_t
401 atomic_swap_64(volatile uint64_t *p, uint64_t val)
405 __asm __volatile("amoswap.d %0, %2, %1"
406 : "=&r"(old), "+A" (*p)
413 #define atomic_add_long atomic_add_64
414 #define atomic_clear_long atomic_clear_64
415 #define atomic_cmpset_long atomic_cmpset_64
416 #define atomic_fcmpset_long atomic_fcmpset_64
417 #define atomic_fetchadd_long atomic_fetchadd_64
418 #define atomic_readandclear_long atomic_readandclear_64
419 #define atomic_set_long atomic_set_64
420 #define atomic_subtract_long atomic_subtract_64
422 #define atomic_add_ptr atomic_add_64
423 #define atomic_clear_ptr atomic_clear_64
424 #define atomic_cmpset_ptr atomic_cmpset_64
425 #define atomic_fcmpset_ptr atomic_fcmpset_64
426 #define atomic_fetchadd_ptr atomic_fetchadd_64
427 #define atomic_readandclear_ptr atomic_readandclear_64
428 #define atomic_set_ptr atomic_set_64
429 #define atomic_subtract_ptr atomic_subtract_64
431 ATOMIC_ACQ_REL(set, 64)
432 ATOMIC_ACQ_REL(clear, 64)
433 ATOMIC_ACQ_REL(add, 64)
434 ATOMIC_ACQ_REL(subtract, 64)
437 atomic_cmpset_acq_64(volatile uint64_t *p, uint64_t cmpval, uint64_t newval)
441 res = atomic_cmpset_64(p, cmpval, newval);
449 atomic_cmpset_rel_64(volatile uint64_t *p, uint64_t cmpval, uint64_t newval)
454 return (atomic_cmpset_64(p, cmpval, newval));
458 atomic_fcmpset_acq_64(volatile uint64_t *p, uint64_t *cmpval, uint64_t newval)
462 res = atomic_fcmpset_64(p, cmpval, newval);
470 atomic_fcmpset_rel_64(volatile uint64_t *p, uint64_t *cmpval, uint64_t newval)
475 return (atomic_fcmpset_64(p, cmpval, newval));
478 static __inline uint64_t
479 atomic_load_acq_64(volatile uint64_t *p)
491 atomic_store_rel_64(volatile uint64_t *p, uint64_t val)
499 #define atomic_add_acq_long atomic_add_acq_64
500 #define atomic_clear_acq_long atomic_add_acq_64
501 #define atomic_cmpset_acq_long atomic_cmpset_acq_64
502 #define atomic_fcmpset_acq_long atomic_fcmpset_acq_64
503 #define atomic_load_acq_long atomic_load_acq_64
504 #define atomic_set_acq_long atomic_set_acq_64
505 #define atomic_subtract_acq_long atomic_subtract_acq_64
507 #define atomic_add_acq_ptr atomic_add_acq_64
508 #define atomic_clear_acq_ptr atomic_add_acq_64
509 #define atomic_cmpset_acq_ptr atomic_cmpset_acq_64
510 #define atomic_fcmpset_acq_ptr atomic_fcmpset_acq_64
511 #define atomic_load_acq_ptr atomic_load_acq_64
512 #define atomic_set_acq_ptr atomic_set_acq_64
513 #define atomic_subtract_acq_ptr atomic_subtract_acq_64
515 #undef ATOMIC_ACQ_REL
518 atomic_thread_fence_acq(void)
525 atomic_thread_fence_rel(void)
532 atomic_thread_fence_acq_rel(void)
539 atomic_thread_fence_seq_cst(void)
545 #define atomic_add_rel_long atomic_add_rel_64
546 #define atomic_clear_rel_long atomic_clear_rel_64
548 #define atomic_add_rel_long atomic_add_rel_64
549 #define atomic_clear_rel_long atomic_clear_rel_64
550 #define atomic_cmpset_rel_long atomic_cmpset_rel_64
551 #define atomic_fcmpset_rel_long atomic_fcmpset_rel_64
552 #define atomic_set_rel_long atomic_set_rel_64
553 #define atomic_subtract_rel_long atomic_subtract_rel_64
554 #define atomic_store_rel_long atomic_store_rel_64
556 #define atomic_add_rel_ptr atomic_add_rel_64
557 #define atomic_clear_rel_ptr atomic_clear_rel_64
558 #define atomic_cmpset_rel_ptr atomic_cmpset_rel_64
559 #define atomic_fcmpset_rel_ptr atomic_fcmpset_rel_64
560 #define atomic_set_rel_ptr atomic_set_rel_64
561 #define atomic_subtract_rel_ptr atomic_subtract_rel_64
562 #define atomic_store_rel_ptr atomic_store_rel_64
564 #endif /* _MACHINE_ATOMIC_H_ */