atomic.h revision 66695
1/*-
2 * Copyright (c) 1998 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/amd64/include/atomic.h 66695 2000-10-05 22:19:50Z jhb $
27 */
28#ifndef _MACHINE_ATOMIC_H_
29#define _MACHINE_ATOMIC_H_
30
31/*
32 * Various simple arithmetic on memory which is atomic in the presence
33 * of interrupts and multiple processors.
34 *
35 * atomic_set_char(P, V)	(*(u_char*)(P) |= (V))
36 * atomic_clear_char(P, V)	(*(u_char*)(P) &= ~(V))
37 * atomic_add_char(P, V)	(*(u_char*)(P) += (V))
38 * atomic_subtract_char(P, V)	(*(u_char*)(P) -= (V))
39 *
40 * atomic_set_short(P, V)	(*(u_short*)(P) |= (V))
41 * atomic_clear_short(P, V)	(*(u_short*)(P) &= ~(V))
42 * atomic_add_short(P, V)	(*(u_short*)(P) += (V))
43 * atomic_subtract_short(P, V)	(*(u_short*)(P) -= (V))
44 *
45 * atomic_set_int(P, V)		(*(u_int*)(P) |= (V))
46 * atomic_clear_int(P, V)	(*(u_int*)(P) &= ~(V))
47 * atomic_add_int(P, V)		(*(u_int*)(P) += (V))
48 * atomic_subtract_int(P, V)	(*(u_int*)(P) -= (V))
49 * atomic_readandclear_int(P)	(return  *(u_int*)P; *(u_int*)P = 0;)
50 *
51 * atomic_set_long(P, V)	(*(u_long*)(P) |= (V))
52 * atomic_clear_long(P, V)	(*(u_long*)(P) &= ~(V))
53 * atomic_add_long(P, V)	(*(u_long*)(P) += (V))
54 * atomic_subtract_long(P, V)	(*(u_long*)(P) -= (V))
55 * atomic_readandclear_long(P)	(return  *(u_long*)P; *(u_long*)P = 0;)
56 */
57
58/*
59 * The above functions are expanded inline in the statically-linked
60 * kernel.  Lock prefixes are generated if an SMP kernel is being
61 * built.
62 *
63 * Kernel modules call real functions which are built into the kernel.
64 * This allows kernel modules to be portable between UP and SMP systems.
65 */
66#if defined(KLD_MODULE)
67#define ATOMIC_ASM(NAME, TYPE, OP, V)			\
68	void atomic_##NAME##_##TYPE(volatile u_##TYPE *p, u_##TYPE v);
69
70int atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src);
71
72#else /* !KLD_MODULE */
73#if defined(SMP)
74#if defined(LOCORE)
75#define	MPLOCKED	lock ;
76#else
77#define MPLOCKED	"lock ; "
78#endif
79#else
80#define MPLOCKED
81#endif
82
83/*
84 * The assembly is volatilized to demark potential before-and-after side
85 * effects if an interrupt or SMP collision were to occur.
86 */
87#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 9)
88/* egcs 1.1.2+ version */
89#define ATOMIC_ASM(NAME, TYPE, OP, V)			\
90static __inline void					\
91atomic_##NAME##_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\
92{							\
93	__asm __volatile(MPLOCKED OP			\
94			 : "=m" (*p)			\
95			 :  "0" (*p), "ir" (V)); 	\
96}
97
98/*
99 * Atomic compare and set, used by the mutex functions
100 *
101 * if (*dst == exp) *dst = src (all 32 bit words)
102 *
103 * Returns 0 on failure, non-zero on success
104 */
105
106#if defined(I386_CPU)
107static __inline int
108atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src)
109{
110	int res = exp;
111
112	__asm __volatile(
113	"	pushfl ;		"
114	"	cli ;			"
115	"	cmpl	%1,%3 ;		"
116	"	jne	1f ;		"
117	"	movl	%2,%3 ;		"
118	"1:				"
119	"       sete	%%al;		"
120	"	movzbl	%%al,%0 ;	"
121	"	popfl ;			"
122	"# atomic_cmpset_int"
123	: "=a" (res)			/* 0 (result) */
124	: "0" (exp),			/* 1 */
125	  "r" (src),			/* 2 */
126	  "m" (*(dst))			/* 3 */
127	: "memory");
128
129	return (res);
130}
131#else /* defined(I386_CPU) */
132static __inline int
133atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src)
134{
135	int res = exp;
136
137	__asm __volatile (
138	"	" MPLOCKED "		"
139	"	cmpxchgl %2,%3 ;	"
140	"       setz	%%al ;		"
141	"	movzbl	%%al,%0 ;	"
142	"1:				"
143	"# atomic_cmpset_int"
144	: "=a" (res)			/* 0 (result) */
145	: "0" (exp),			/* 1 */
146	  "r" (src),			/* 2 */
147	  "m" (*(dst))			/* 3 */
148	: "memory");
149
150	return (res);
151}
152#endif /* defined(I386_CPU) */
153
154#else
155/* gcc <= 2.8 version */
156#define ATOMIC_ASM(NAME, TYPE, OP, V)			\
157static __inline void					\
158atomic_##NAME##_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\
159{							\
160	__asm __volatile(MPLOCKED OP			\
161			 : "=m" (*p)			\
162			 : "ir" (V));		 	\
163}
164#endif
165#endif /* KLD_MODULE */
166
167#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 9)
168
169/* egcs 1.1.2+ version */
170ATOMIC_ASM(set,	     char,  "orb %b2,%0",   v)
171ATOMIC_ASM(clear,    char,  "andb %b2,%0", ~v)
172ATOMIC_ASM(add,	     char,  "addb %b2,%0",  v)
173ATOMIC_ASM(subtract, char,  "subb %b2,%0",  v)
174
175ATOMIC_ASM(set,	     short, "orw %w2,%0",   v)
176ATOMIC_ASM(clear,    short, "andw %w2,%0", ~v)
177ATOMIC_ASM(add,	     short, "addw %w2,%0",  v)
178ATOMIC_ASM(subtract, short, "subw %w2,%0",  v)
179
180ATOMIC_ASM(set,	     int,   "orl %2,%0",   v)
181ATOMIC_ASM(clear,    int,   "andl %2,%0", ~v)
182ATOMIC_ASM(add,	     int,   "addl %2,%0",  v)
183ATOMIC_ASM(subtract, int,   "subl %2,%0",  v)
184
185ATOMIC_ASM(set,	     long,  "orl %2,%0",   v)
186ATOMIC_ASM(clear,    long,  "andl %2,%0", ~v)
187ATOMIC_ASM(add,	     long,  "addl %2,%0",  v)
188ATOMIC_ASM(subtract, long,  "subl %2,%0",  v)
189
190#else
191
192/* gcc <= 2.8 version */
193ATOMIC_ASM(set,	     char,  "orb %1,%0",   v)
194ATOMIC_ASM(clear,    char,  "andb %1,%0", ~v)
195ATOMIC_ASM(add,	     char,  "addb %1,%0",  v)
196ATOMIC_ASM(subtract, char,  "subb %1,%0",  v)
197
198ATOMIC_ASM(set,	     short, "orw %1,%0",   v)
199ATOMIC_ASM(clear,    short, "andw %1,%0", ~v)
200ATOMIC_ASM(add,	     short, "addw %1,%0",  v)
201ATOMIC_ASM(subtract, short, "subw %1,%0",  v)
202
203ATOMIC_ASM(set,	     int,   "orl %1,%0",   v)
204ATOMIC_ASM(clear,    int,   "andl %1,%0", ~v)
205ATOMIC_ASM(add,	     int,   "addl %1,%0",  v)
206ATOMIC_ASM(subtract, int,   "subl %1,%0",  v)
207
208ATOMIC_ASM(set,	     long,  "orl %1,%0",   v)
209ATOMIC_ASM(clear,    long,  "andl %1,%0", ~v)
210ATOMIC_ASM(add,	     long,  "addl %1,%0",  v)
211ATOMIC_ASM(subtract, long,  "subl %1,%0",  v)
212
213#endif
214
215#ifndef WANT_FUNCTIONS
216static __inline int
217atomic_cmpset_ptr(volatile void *dst, void *exp, void *src)
218{
219
220	return (
221	    atomic_cmpset_int((volatile u_int *)dst, (u_int)exp, (u_int)src));
222}
223
224static __inline u_int
225atomic_readandclear_int(volatile u_int *addr)
226{
227	u_int result;
228
229	__asm __volatile (
230	"	xorl	%0,%0 ;		"
231	"	xchgl	%1,%0 ;		"
232	"# atomic_readandclear_int"
233	: "=&r" (result)		/* 0 (result) */
234	: "m" (*addr));			/* 1 (addr) */
235
236	return (result);
237}
238
239static __inline u_long
240atomic_readandclear_long(volatile u_long *addr)
241{
242	u_long result;
243
244	__asm __volatile (
245	"	xorl	%0,%0 ;		"
246	"	xchgl	%1,%0 ;		"
247	"# atomic_readandclear_int"
248	: "=&r" (result)		/* 0 (result) */
249	: "m" (*addr));			/* 1 (addr) */
250
251	return (result);
252}
253#endif
254
255#endif /* ! _MACHINE_ATOMIC_H_ */
256