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)
123 static __inline uint32_t
124 atomic_fetchadd_32(volatile uint32_t *p, uint32_t val)
128 __asm __volatile("amoadd.w %0, %2, %1"
129 : "=&r" (ret), "+A" (*p)
136 static __inline uint32_t
137 atomic_readandclear_32(volatile uint32_t *p)
144 __asm __volatile("amoswap.w %0, %2, %1"
145 : "=&r"(ret), "+A" (*p)
152 #define atomic_add_int atomic_add_32
153 #define atomic_clear_int atomic_clear_32
154 #define atomic_cmpset_int atomic_cmpset_32
155 #define atomic_fetchadd_int atomic_fetchadd_32
156 #define atomic_readandclear_int atomic_readandclear_32
157 #define atomic_set_int atomic_set_32
158 #define atomic_subtract_int atomic_subtract_32
160 ATOMIC_ACQ_REL(set, 32)
161 ATOMIC_ACQ_REL(clear, 32)
162 ATOMIC_ACQ_REL(add, 32)
163 ATOMIC_ACQ_REL(subtract, 32)
166 atomic_cmpset_acq_32(volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
170 res = atomic_cmpset_32(p, cmpval, newval);
178 atomic_cmpset_rel_32(volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
183 return (atomic_cmpset_32(p, cmpval, newval));
186 static __inline uint32_t
187 atomic_load_acq_32(volatile uint32_t *p)
199 atomic_store_rel_32(volatile uint32_t *p, uint32_t val)
207 #define atomic_add_acq_int atomic_add_acq_32
208 #define atomic_clear_acq_int atomic_clear_acq_32
209 #define atomic_cmpset_acq_int atomic_cmpset_acq_32
210 #define atomic_load_acq_int atomic_load_acq_32
211 #define atomic_set_acq_int atomic_set_acq_32
212 #define atomic_subtract_acq_int atomic_subtract_acq_32
214 #define atomic_add_rel_int atomic_add_rel_32
215 #define atomic_clear_rel_int atomic_add_rel_32
216 #define atomic_cmpset_rel_int atomic_cmpset_rel_32
217 #define atomic_set_rel_int atomic_set_rel_32
218 #define atomic_subtract_rel_int atomic_subtract_rel_32
219 #define atomic_store_rel_int atomic_store_rel_32
222 atomic_add_64(volatile uint64_t *p, uint64_t val)
225 __asm __volatile("amoadd.d zero, %1, %0"
232 atomic_subtract_64(volatile uint64_t *p, uint64_t val)
235 __asm __volatile("amoadd.d zero, %1, %0"
242 atomic_set_64(volatile uint64_t *p, uint64_t val)
245 __asm __volatile("amoor.d zero, %1, %0"
252 atomic_clear_64(volatile uint64_t *p, uint64_t val)
255 __asm __volatile("amoand.d zero, %1, %0"
262 atomic_cmpset_64(volatile uint64_t *p, uint64_t cmpval, uint64_t newval)
271 "li %1, 1\n" /* Preset to fail */
277 : "=&r" (tmp), "=&r" (res), "+A" (*p)
278 : "rJ" (cmpval), "rJ" (newval)
284 static __inline uint64_t
285 atomic_fetchadd_64(volatile uint64_t *p, uint64_t val)
289 __asm __volatile("amoadd.d %0, %2, %1"
290 : "=&r" (ret), "+A" (*p)
297 static __inline uint64_t
298 atomic_readandclear_64(volatile uint64_t *p)
305 __asm __volatile("amoswap.d %0, %2, %1"
306 : "=&r"(ret), "+A" (*p)
313 static __inline uint32_t
314 atomic_swap_32(volatile uint32_t *p, uint32_t val)
318 __asm __volatile("amoswap.w %0, %2, %1"
319 : "=&r"(old), "+A" (*p)
326 static __inline uint64_t
327 atomic_swap_64(volatile uint64_t *p, uint64_t val)
331 __asm __volatile("amoswap.d %0, %2, %1"
332 : "=&r"(old), "+A" (*p)
339 #define atomic_add_long atomic_add_64
340 #define atomic_clear_long atomic_clear_64
341 #define atomic_cmpset_long atomic_cmpset_64
342 #define atomic_fetchadd_long atomic_fetchadd_64
343 #define atomic_readandclear_long atomic_readandclear_64
344 #define atomic_set_long atomic_set_64
345 #define atomic_subtract_long atomic_subtract_64
347 #define atomic_add_ptr atomic_add_64
348 #define atomic_clear_ptr atomic_clear_64
349 #define atomic_cmpset_ptr atomic_cmpset_64
350 #define atomic_fetchadd_ptr atomic_fetchadd_64
351 #define atomic_readandclear_ptr atomic_readandclear_64
352 #define atomic_set_ptr atomic_set_64
353 #define atomic_subtract_ptr atomic_subtract_64
355 ATOMIC_ACQ_REL(set, 64)
356 ATOMIC_ACQ_REL(clear, 64)
357 ATOMIC_ACQ_REL(add, 64)
358 ATOMIC_ACQ_REL(subtract, 64)
361 atomic_cmpset_acq_64(volatile uint64_t *p, uint64_t cmpval, uint64_t newval)
365 res = atomic_cmpset_64(p, cmpval, newval);
373 atomic_cmpset_rel_64(volatile uint64_t *p, uint64_t cmpval, uint64_t newval)
378 return (atomic_cmpset_64(p, cmpval, newval));
381 static __inline uint64_t
382 atomic_load_acq_64(volatile uint64_t *p)
394 atomic_store_rel_64(volatile uint64_t *p, uint64_t val)
402 #define atomic_add_acq_long atomic_add_acq_64
403 #define atomic_clear_acq_long atomic_add_acq_64
404 #define atomic_cmpset_acq_long atomic_cmpset_acq_64
405 #define atomic_load_acq_long atomic_load_acq_64
406 #define atomic_set_acq_long atomic_set_acq_64
407 #define atomic_subtract_acq_long atomic_subtract_acq_64
409 #define atomic_add_acq_ptr atomic_add_acq_64
410 #define atomic_clear_acq_ptr atomic_add_acq_64
411 #define atomic_cmpset_acq_ptr atomic_cmpset_acq_64
412 #define atomic_load_acq_ptr atomic_load_acq_64
413 #define atomic_set_acq_ptr atomic_set_acq_64
414 #define atomic_subtract_acq_ptr atomic_subtract_acq_64
417 atomic_thread_fence_acq(void)
424 atomic_thread_fence_rel(void)
431 atomic_thread_fence_acq_rel(void)
438 atomic_thread_fence_seq_cst(void)
444 #define atomic_add_rel_long atomic_add_rel_64
445 #define atomic_clear_rel_long atomic_clear_rel_64
447 #define atomic_add_rel_long atomic_add_rel_64
448 #define atomic_clear_rel_long atomic_clear_rel_64
449 #define atomic_cmpset_rel_long atomic_cmpset_rel_64
450 #define atomic_set_rel_long atomic_set_rel_64
451 #define atomic_subtract_rel_long atomic_subtract_rel_64
452 #define atomic_store_rel_long atomic_store_rel_64
454 #define atomic_add_rel_ptr atomic_add_rel_64
455 #define atomic_clear_rel_ptr atomic_clear_rel_64
456 #define atomic_cmpset_rel_ptr atomic_cmpset_rel_64
457 #define atomic_set_rel_ptr atomic_set_rel_64
458 #define atomic_subtract_rel_ptr atomic_subtract_rel_64
459 #define atomic_store_rel_ptr atomic_store_rel_64
461 #endif /* _MACHINE_ATOMIC_H_ */