xref: /freebsd/sys/i386/include/atomic.h (revision 47b8bc92e84f4fc3c16511c983ea1a1e1c27abd3)
1069e9bc1SDoug Rabson /*-
2069e9bc1SDoug Rabson  * Copyright (c) 1998 Doug Rabson
3069e9bc1SDoug Rabson  * All rights reserved.
4069e9bc1SDoug Rabson  *
5069e9bc1SDoug Rabson  * Redistribution and use in source and binary forms, with or without
6069e9bc1SDoug Rabson  * modification, are permitted provided that the following conditions
7069e9bc1SDoug Rabson  * are met:
8069e9bc1SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
9069e9bc1SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
10069e9bc1SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
11069e9bc1SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
12069e9bc1SDoug Rabson  *    documentation and/or other materials provided with the distribution.
13069e9bc1SDoug Rabson  *
14069e9bc1SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15069e9bc1SDoug Rabson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16069e9bc1SDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17069e9bc1SDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18069e9bc1SDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19069e9bc1SDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20069e9bc1SDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21069e9bc1SDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22069e9bc1SDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23069e9bc1SDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24069e9bc1SDoug Rabson  * SUCH DAMAGE.
25069e9bc1SDoug Rabson  *
2647b8bc92SAlan Cox  *	$Id: atomic.h,v 1.2 1999/07/13 03:32:17 alc Exp $
27069e9bc1SDoug Rabson  */
28069e9bc1SDoug Rabson #ifndef _MACHINE_ATOMIC_H_
29069e9bc1SDoug Rabson #define _MACHINE_ATOMIC_H_
30069e9bc1SDoug Rabson 
31069e9bc1SDoug Rabson /*
32069e9bc1SDoug Rabson  * Various simple arithmetic on memory which is atomic in the presence
3347b8bc92SAlan Cox  * of interrupts and multiple processors.
34069e9bc1SDoug Rabson  *
3547b8bc92SAlan Cox  * atomic_set_char(P, V)	(*(u_char*)(P) |= (V))
3647b8bc92SAlan Cox  * atomic_clear_char(P, V)	(*(u_char*)(P) &= ~(V))
3747b8bc92SAlan Cox  * atomic_add_char(P, V)	(*(u_char*)(P) += (V))
3847b8bc92SAlan Cox  * atomic_subtract_char(P, V)	(*(u_char*)(P) -= (V))
3947b8bc92SAlan Cox  *
4047b8bc92SAlan Cox  * atomic_set_short(P, V)	(*(u_short*)(P) |= (V))
4147b8bc92SAlan Cox  * atomic_clear_short(P, V)	(*(u_short*)(P) &= ~(V))
4247b8bc92SAlan Cox  * atomic_add_short(P, V)	(*(u_short*)(P) += (V))
4347b8bc92SAlan Cox  * atomic_subtract_short(P, V)	(*(u_short*)(P) -= (V))
4447b8bc92SAlan Cox  *
4547b8bc92SAlan Cox  * atomic_set_int(P, V)		(*(u_int*)(P) |= (V))
4647b8bc92SAlan Cox  * atomic_clear_int(P, V)	(*(u_int*)(P) &= ~(V))
4747b8bc92SAlan Cox  * atomic_add_int(P, V)		(*(u_int*)(P) += (V))
4847b8bc92SAlan Cox  * atomic_subtract_int(P, V)	(*(u_int*)(P) -= (V))
4947b8bc92SAlan Cox  *
5047b8bc92SAlan Cox  * atomic_set_long(P, V)	(*(u_long*)(P) |= (V))
5147b8bc92SAlan Cox  * atomic_clear_long(P, V)	(*(u_long*)(P) &= ~(V))
5247b8bc92SAlan Cox  * atomic_add_long(P, V)	(*(u_long*)(P) += (V))
5347b8bc92SAlan Cox  * atomic_subtract_long(P, V)	(*(u_long*)(P) -= (V))
54069e9bc1SDoug Rabson  */
55069e9bc1SDoug Rabson 
5647b8bc92SAlan Cox /*
5747b8bc92SAlan Cox  * Make kernel modules portable between UP and SMP.
5847b8bc92SAlan Cox  */
5947b8bc92SAlan Cox #if defined(SMP) || defined(KLD_MODULE)
6047b8bc92SAlan Cox #define MPLOCKED	"lock ; "
61e58bb1c4SAlan Cox #else
6247b8bc92SAlan Cox #define MPLOCKED
63e58bb1c4SAlan Cox #endif
64069e9bc1SDoug Rabson 
6547b8bc92SAlan Cox /*
6647b8bc92SAlan Cox  * The assembly is volatilized to demark potential before-and-after side
6747b8bc92SAlan Cox  * effects if an interrupt or SMP collision were to occur.
6847b8bc92SAlan Cox  */
6947b8bc92SAlan Cox #define ATOMIC_ASM(NAME, TYPE, OP, V)			\
7047b8bc92SAlan Cox static __inline void					\
7147b8bc92SAlan Cox atomic_##NAME##_##TYPE(void *p, u_##TYPE v)		\
7247b8bc92SAlan Cox {							\
7347b8bc92SAlan Cox 	__asm __volatile(MPLOCKED OP			\
7447b8bc92SAlan Cox 			 : "=m" (*(u_##TYPE *)p)	\
7547b8bc92SAlan Cox 			 :  "0" (*(u_##TYPE *)p), "ir" (V)); \
7647b8bc92SAlan Cox }
77069e9bc1SDoug Rabson 
7847b8bc92SAlan Cox ATOMIC_ASM(set,	     char,  "orb %2,%0",   v)
7947b8bc92SAlan Cox ATOMIC_ASM(clear,    char,  "andb %2,%0", ~v)
8047b8bc92SAlan Cox ATOMIC_ASM(add,	     char,  "addb %2,%0",  v)
8147b8bc92SAlan Cox ATOMIC_ASM(subtract, char,  "subb %2,%0",  v)
82069e9bc1SDoug Rabson 
8347b8bc92SAlan Cox ATOMIC_ASM(set,	     short, "orw %2,%0",   v)
8447b8bc92SAlan Cox ATOMIC_ASM(clear,    short, "andw %2,%0", ~v)
8547b8bc92SAlan Cox ATOMIC_ASM(add,	     short, "addw %2,%0",  v)
8647b8bc92SAlan Cox ATOMIC_ASM(subtract, short, "subw %2,%0",  v)
87e58bb1c4SAlan Cox 
8847b8bc92SAlan Cox ATOMIC_ASM(set,	     int,   "orl %2,%0",   v)
8947b8bc92SAlan Cox ATOMIC_ASM(clear,    int,   "andl %2,%0", ~v)
9047b8bc92SAlan Cox ATOMIC_ASM(add,	     int,   "addl %2,%0",  v)
9147b8bc92SAlan Cox ATOMIC_ASM(subtract, int,   "subl %2,%0",  v)
92e58bb1c4SAlan Cox 
9347b8bc92SAlan Cox ATOMIC_ASM(set,	     long,  "orl %2,%0",   v)
9447b8bc92SAlan Cox ATOMIC_ASM(clear,    long,  "andl %2,%0", ~v)
9547b8bc92SAlan Cox ATOMIC_ASM(add,	     long,  "addl %2,%0",  v)
9647b8bc92SAlan Cox ATOMIC_ASM(subtract, long,  "subl %2,%0",  v)
97069e9bc1SDoug Rabson 
98069e9bc1SDoug Rabson #endif /* ! _MACHINE_ATOMIC_H_ */
99