xref: /freebsd/sys/i386/include/atomic.h (revision a5f50ef9)
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$
27  */
28 #ifndef _MACHINE_ATOMIC_H_
29 #define _MACHINE_ATOMIC_H_
30 
31 #ifndef _SYS_CDEFS_H_
32 #error this file needs sys/cdefs.h as a prerequisite
33 #endif
34 
35 /*
36  * Various simple arithmetic on memory which is atomic in the presence
37  * of interrupts and multiple processors.
38  *
39  * atomic_set_char(P, V)	(*(u_char*)(P) |= (V))
40  * atomic_clear_char(P, V)	(*(u_char*)(P) &= ~(V))
41  * atomic_add_char(P, V)	(*(u_char*)(P) += (V))
42  * atomic_subtract_char(P, V)	(*(u_char*)(P) -= (V))
43  *
44  * atomic_set_short(P, V)	(*(u_short*)(P) |= (V))
45  * atomic_clear_short(P, V)	(*(u_short*)(P) &= ~(V))
46  * atomic_add_short(P, V)	(*(u_short*)(P) += (V))
47  * atomic_subtract_short(P, V)	(*(u_short*)(P) -= (V))
48  *
49  * atomic_set_int(P, V)		(*(u_int*)(P) |= (V))
50  * atomic_clear_int(P, V)	(*(u_int*)(P) &= ~(V))
51  * atomic_add_int(P, V)		(*(u_int*)(P) += (V))
52  * atomic_subtract_int(P, V)	(*(u_int*)(P) -= (V))
53  * atomic_readandclear_int(P)	(return  *(u_int*)P; *(u_int*)P = 0;)
54  *
55  * atomic_set_long(P, V)	(*(u_long*)(P) |= (V))
56  * atomic_clear_long(P, V)	(*(u_long*)(P) &= ~(V))
57  * atomic_add_long(P, V)	(*(u_long*)(P) += (V))
58  * atomic_subtract_long(P, V)	(*(u_long*)(P) -= (V))
59  * atomic_readandclear_long(P)	(return  *(u_long*)P; *(u_long*)P = 0;)
60  */
61 
62 /*
63  * The above functions are expanded inline in the statically-linked
64  * kernel.  Lock prefixes are generated if an SMP kernel is being
65  * built.
66  *
67  * Kernel modules call real functions which are built into the kernel.
68  * This allows kernel modules to be portable between UP and SMP systems.
69  */
70 #if defined(KLD_MODULE)
71 #define ATOMIC_ASM(NAME, TYPE, OP, CONS, V)			\
72 void atomic_##NAME##_##TYPE(volatile u_##TYPE *p, u_##TYPE v)
73 
74 int atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src);
75 
76 #define	ATOMIC_STORE_LOAD(TYPE, LOP, SOP)			\
77 u_##TYPE	atomic_load_acq_##TYPE(volatile u_##TYPE *p);	\
78 void		atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)
79 
80 #else /* !KLD_MODULE */
81 
82 #ifdef __GNUCLIKE_ASM
83 
84 /*
85  * For userland, assume the SMP case and use lock prefixes so that
86  * the binaries will run on both types of systems.
87  */
88 #if defined(SMP) || !defined(_KERNEL)
89 #define MPLOCKED	lock ;
90 #else
91 #define MPLOCKED
92 #endif
93 
94 /*
95  * The assembly is volatilized to demark potential before-and-after side
96  * effects if an interrupt or SMP collision were to occur.
97  */
98 #define ATOMIC_ASM(NAME, TYPE, OP, CONS, V)		\
99 static __inline void					\
100 atomic_##NAME##_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\
101 {							\
102 	__asm __volatile(__XSTRING(MPLOCKED) OP		\
103 			 : "+m" (*p)			\
104 			 : CONS (V));			\
105 }							\
106 struct __hack
107 
108 #else /* !__GNUCLIKE_ASM */
109 
110 #define ATOMIC_ASM(NAME, TYPE, OP, CONS, V)				\
111 extern void atomic_##NAME##_##TYPE(volatile u_##TYPE *p, u_##TYPE v)
112 
113 #endif /* __GNUCLIKE_ASM */
114 
115 /*
116  * Atomic compare and set, used by the mutex functions
117  *
118  * if (*dst == exp) *dst = src (all 32 bit words)
119  *
120  * Returns 0 on failure, non-zero on success
121  */
122 
123 #ifdef __GNUCLIKE_ASM
124 
125 #if defined(CPU_DISABLE_CMPXCHG)
126 
127 static __inline int
128 atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src)
129 {
130 	int res = exp;
131 
132 	__asm __volatile(
133 	"	pushfl ;		"
134 	"	cli ;			"
135 	"	cmpl	%0,%2 ;		"
136 	"	jne	1f ;		"
137 	"	movl	%1,%2 ;		"
138 	"1:				"
139 	"       sete	%%al;		"
140 	"	movzbl	%%al,%0 ;	"
141 	"	popfl ;			"
142 	"# atomic_cmpset_int"
143 	: "+a" (res)			/* 0 (result) */
144 	: "r" (src),			/* 1 */
145 	  "m" (*(dst))			/* 2 */
146 	: "memory");
147 
148 	return (res);
149 }
150 
151 #else /* defined(CPU_DISABLE_CMPXCHG) */
152 
153 static __inline int
154 atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src)
155 {
156 	int res = exp;
157 
158 	__asm __volatile (
159 	"	" __XSTRING(MPLOCKED) "	"
160 	"	cmpxchgl %1,%2 ;	"
161 	"       setz	%%al ;		"
162 	"	movzbl	%%al,%0 ;	"
163 	"1:				"
164 	"# atomic_cmpset_int"
165 	: "+a" (res)			/* 0 (result) */
166 	: "r" (src),			/* 1 */
167 	  "m" (*(dst))			/* 2 */
168 	: "memory");
169 
170 	return (res);
171 }
172 
173 #endif /* defined(CPU_DISABLE_CMPXCHG) */
174 
175 #endif /* __GNUCLIKE_ASM */
176 
177 #ifdef __GNUCLIKE_ASM
178 
179 #if defined(_KERNEL) && !defined(SMP)
180 
181 /*
182  * We assume that a = b will do atomic loads and stores.  However, on a
183  * PentiumPro or higher, reads may pass writes, so for that case we have
184  * to use a serializing instruction (i.e. with LOCK) to do the load in
185  * SMP kernels.  For UP kernels, however, the cache of the single processor
186  * is always consistent, so we don't need any memory barriers.
187  */
188 #define ATOMIC_STORE_LOAD(TYPE, LOP, SOP)		\
189 static __inline u_##TYPE				\
190 atomic_load_acq_##TYPE(volatile u_##TYPE *p)		\
191 {							\
192 	return (*p);					\
193 }							\
194 							\
195 static __inline void					\
196 atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\
197 {							\
198 	*p = v;						\
199 }							\
200 struct __hack
201 
202 #else /* defined(SMP) */
203 
204 #define ATOMIC_STORE_LOAD(TYPE, LOP, SOP)		\
205 static __inline u_##TYPE				\
206 atomic_load_acq_##TYPE(volatile u_##TYPE *p)		\
207 {							\
208 	u_##TYPE res;					\
209 							\
210 	__asm __volatile(__XSTRING(MPLOCKED) LOP	\
211 	: "=a" (res),			/* 0 (result) */\
212 	  "+m" (*p)			/* 1 */		\
213 	: : "memory");				 	\
214 							\
215 	return (res);					\
216 }							\
217 							\
218 /*							\
219  * The XCHG instruction asserts LOCK automagically.	\
220  */							\
221 static __inline void					\
222 atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\
223 {							\
224 	__asm __volatile(SOP				\
225 	: "+m" (*p),			/* 0 */		\
226 	  "+r" (v)			/* 1 */		\
227 	: : "memory");				 	\
228 }							\
229 struct __hack
230 
231 #endif	/* !defined(SMP) */
232 
233 #else /* !__GNUCLIKE_ASM */
234 
235 extern int atomic_cmpset_int(volatile u_int *, u_int, u_int);
236 
237 #define ATOMIC_STORE_LOAD(TYPE, LOP, SOP)				\
238 extern u_##TYPE atomic_load_acq_##TYPE(volatile u_##TYPE *p);		\
239 extern void atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)
240 
241 #endif /* __GNUCLIKE_ASM */
242 
243 #endif /* KLD_MODULE */
244 
245 ATOMIC_ASM(set,	     char,  "orb %b1,%0",  "iq",  v);
246 ATOMIC_ASM(clear,    char,  "andb %b1,%0", "iq", ~v);
247 ATOMIC_ASM(add,	     char,  "addb %b1,%0", "iq",  v);
248 ATOMIC_ASM(subtract, char,  "subb %b1,%0", "iq",  v);
249 
250 ATOMIC_ASM(set,	     short, "orw %w1,%0",  "ir",  v);
251 ATOMIC_ASM(clear,    short, "andw %w1,%0", "ir", ~v);
252 ATOMIC_ASM(add,	     short, "addw %w1,%0", "ir",  v);
253 ATOMIC_ASM(subtract, short, "subw %w1,%0", "ir",  v);
254 
255 ATOMIC_ASM(set,	     int,   "orl %1,%0",   "ir",  v);
256 ATOMIC_ASM(clear,    int,   "andl %1,%0",  "ir", ~v);
257 ATOMIC_ASM(add,	     int,   "addl %1,%0",  "ir",  v);
258 ATOMIC_ASM(subtract, int,   "subl %1,%0",  "ir",  v);
259 
260 ATOMIC_ASM(set,	     long,  "orl %1,%0",   "ir",  v);
261 ATOMIC_ASM(clear,    long,  "andl %1,%0",  "ir", ~v);
262 ATOMIC_ASM(add,	     long,  "addl %1,%0",  "ir",  v);
263 ATOMIC_ASM(subtract, long,  "subl %1,%0",  "ir",  v);
264 
265 ATOMIC_STORE_LOAD(char,	"cmpxchgb %b0,%1", "xchgb %b1,%0");
266 ATOMIC_STORE_LOAD(short,"cmpxchgw %w0,%1", "xchgw %w1,%0");
267 ATOMIC_STORE_LOAD(int,	"cmpxchgl %0,%1",  "xchgl %1,%0");
268 ATOMIC_STORE_LOAD(long,	"cmpxchgl %0,%1",  "xchgl %1,%0");
269 
270 #undef ATOMIC_ASM
271 #undef ATOMIC_STORE_LOAD
272 
273 #define	atomic_set_acq_char		atomic_set_char
274 #define	atomic_set_rel_char		atomic_set_char
275 #define	atomic_clear_acq_char		atomic_clear_char
276 #define	atomic_clear_rel_char		atomic_clear_char
277 #define	atomic_add_acq_char		atomic_add_char
278 #define	atomic_add_rel_char		atomic_add_char
279 #define	atomic_subtract_acq_char	atomic_subtract_char
280 #define	atomic_subtract_rel_char	atomic_subtract_char
281 
282 #define	atomic_set_acq_short		atomic_set_short
283 #define	atomic_set_rel_short		atomic_set_short
284 #define	atomic_clear_acq_short		atomic_clear_short
285 #define	atomic_clear_rel_short		atomic_clear_short
286 #define	atomic_add_acq_short		atomic_add_short
287 #define	atomic_add_rel_short		atomic_add_short
288 #define	atomic_subtract_acq_short	atomic_subtract_short
289 #define	atomic_subtract_rel_short	atomic_subtract_short
290 
291 #define	atomic_set_acq_int		atomic_set_int
292 #define	atomic_set_rel_int		atomic_set_int
293 #define	atomic_clear_acq_int		atomic_clear_int
294 #define	atomic_clear_rel_int		atomic_clear_int
295 #define	atomic_add_acq_int		atomic_add_int
296 #define	atomic_add_rel_int		atomic_add_int
297 #define	atomic_subtract_acq_int		atomic_subtract_int
298 #define	atomic_subtract_rel_int		atomic_subtract_int
299 #define atomic_cmpset_acq_int		atomic_cmpset_int
300 #define atomic_cmpset_rel_int		atomic_cmpset_int
301 
302 #define	atomic_set_acq_long		atomic_set_long
303 #define	atomic_set_rel_long		atomic_set_long
304 #define	atomic_clear_acq_long		atomic_clear_long
305 #define	atomic_clear_rel_long		atomic_clear_long
306 #define	atomic_add_acq_long		atomic_add_long
307 #define	atomic_add_rel_long		atomic_add_long
308 #define	atomic_subtract_acq_long	atomic_subtract_long
309 #define	atomic_subtract_rel_long	atomic_subtract_long
310 #define	atomic_cmpset_long		atomic_cmpset_int
311 #define	atomic_cmpset_acq_long		atomic_cmpset_acq_int
312 #define	atomic_cmpset_rel_long		atomic_cmpset_rel_int
313 
314 #define atomic_cmpset_acq_ptr		atomic_cmpset_ptr
315 #define atomic_cmpset_rel_ptr		atomic_cmpset_ptr
316 
317 #define	atomic_set_8		atomic_set_char
318 #define	atomic_set_acq_8	atomic_set_acq_char
319 #define	atomic_set_rel_8	atomic_set_rel_char
320 #define	atomic_clear_8		atomic_clear_char
321 #define	atomic_clear_acq_8	atomic_clear_acq_char
322 #define	atomic_clear_rel_8	atomic_clear_rel_char
323 #define	atomic_add_8		atomic_add_char
324 #define	atomic_add_acq_8	atomic_add_acq_char
325 #define	atomic_add_rel_8	atomic_add_rel_char
326 #define	atomic_subtract_8	atomic_subtract_char
327 #define	atomic_subtract_acq_8	atomic_subtract_acq_char
328 #define	atomic_subtract_rel_8	atomic_subtract_rel_char
329 #define	atomic_load_acq_8	atomic_load_acq_char
330 #define	atomic_store_rel_8	atomic_store_rel_char
331 
332 #define	atomic_set_16		atomic_set_short
333 #define	atomic_set_acq_16	atomic_set_acq_short
334 #define	atomic_set_rel_16	atomic_set_rel_short
335 #define	atomic_clear_16		atomic_clear_short
336 #define	atomic_clear_acq_16	atomic_clear_acq_short
337 #define	atomic_clear_rel_16	atomic_clear_rel_short
338 #define	atomic_add_16		atomic_add_short
339 #define	atomic_add_acq_16	atomic_add_acq_short
340 #define	atomic_add_rel_16	atomic_add_rel_short
341 #define	atomic_subtract_16	atomic_subtract_short
342 #define	atomic_subtract_acq_16	atomic_subtract_acq_short
343 #define	atomic_subtract_rel_16	atomic_subtract_rel_short
344 #define	atomic_load_acq_16	atomic_load_acq_short
345 #define	atomic_store_rel_16	atomic_store_rel_short
346 
347 #define	atomic_set_32		atomic_set_int
348 #define	atomic_set_acq_32	atomic_set_acq_int
349 #define	atomic_set_rel_32	atomic_set_rel_int
350 #define	atomic_clear_32		atomic_clear_int
351 #define	atomic_clear_acq_32	atomic_clear_acq_int
352 #define	atomic_clear_rel_32	atomic_clear_rel_int
353 #define	atomic_add_32		atomic_add_int
354 #define	atomic_add_acq_32	atomic_add_acq_int
355 #define	atomic_add_rel_32	atomic_add_rel_int
356 #define	atomic_subtract_32	atomic_subtract_int
357 #define	atomic_subtract_acq_32	atomic_subtract_acq_int
358 #define	atomic_subtract_rel_32	atomic_subtract_rel_int
359 #define	atomic_load_acq_32	atomic_load_acq_int
360 #define	atomic_store_rel_32	atomic_store_rel_int
361 #define	atomic_cmpset_32	atomic_cmpset_int
362 #define	atomic_cmpset_acq_32	atomic_cmpset_acq_int
363 #define	atomic_cmpset_rel_32	atomic_cmpset_rel_int
364 #define	atomic_readandclear_32	atomic_readandclear_int
365 
366 #if !defined(WANT_FUNCTIONS)
367 static __inline int
368 atomic_cmpset_ptr(volatile void *dst, void *exp, void *src)
369 {
370 
371 	return (atomic_cmpset_int((volatile u_int *)dst, (u_int)exp,
372 	    (u_int)src));
373 }
374 
375 static __inline void *
376 atomic_load_acq_ptr(volatile void *p)
377 {
378 	/*
379 	 * The apparently-bogus cast to intptr_t in the following is to
380 	 * avoid a warning from "gcc -Wbad-function-cast".
381 	 */
382 	return ((void *)(intptr_t)atomic_load_acq_int((volatile u_int *)p));
383 }
384 
385 static __inline void
386 atomic_store_rel_ptr(volatile void *p, void *v)
387 {
388 	atomic_store_rel_int((volatile u_int *)p, (u_int)v);
389 }
390 
391 #define ATOMIC_PTR(NAME)				\
392 static __inline void					\
393 atomic_##NAME##_ptr(volatile void *p, uintptr_t v)	\
394 {							\
395 	atomic_##NAME##_int((volatile u_int *)p, v);	\
396 }							\
397 							\
398 static __inline void					\
399 atomic_##NAME##_acq_ptr(volatile void *p, uintptr_t v)	\
400 {							\
401 	atomic_##NAME##_acq_int((volatile u_int *)p, v);\
402 }							\
403 							\
404 static __inline void					\
405 atomic_##NAME##_rel_ptr(volatile void *p, uintptr_t v)	\
406 {							\
407 	atomic_##NAME##_rel_int((volatile u_int *)p, v);\
408 }
409 
410 ATOMIC_PTR(set)
411 ATOMIC_PTR(clear)
412 ATOMIC_PTR(add)
413 ATOMIC_PTR(subtract)
414 
415 #undef ATOMIC_PTR
416 
417 #ifdef __GNUCLIKE_ASM
418 
419 static __inline u_int
420 atomic_readandclear_int(volatile u_int *addr)
421 {
422 	u_int result;
423 
424 	__asm __volatile (
425 	"	xorl	%0,%0 ;		"
426 	"	xchgl	%1,%0 ;		"
427 	"# atomic_readandclear_int"
428 	: "=&r" (result)		/* 0 (result) */
429 	: "m" (*addr));			/* 1 (addr) */
430 
431 	return (result);
432 }
433 
434 static __inline u_long
435 atomic_readandclear_long(volatile u_long *addr)
436 {
437 	u_long result;
438 
439 	__asm __volatile (
440 	"	xorl	%0,%0 ;		"
441 	"	xchgl	%1,%0 ;		"
442 	"# atomic_readandclear_int"
443 	: "=&r" (result)		/* 0 (result) */
444 	: "m" (*addr));			/* 1 (addr) */
445 
446 	return (result);
447 }
448 
449 #else /* !__GNUCLIKE_ASM */
450 
451 extern u_long	atomic_readandclear_long(volatile u_long *);
452 extern u_int	atomic_readandclear_int(volatile u_int *);
453 
454 #endif /* __GNUCLIKE_ASM */
455 
456 #endif	/* !defined(WANT_FUNCTIONS) */
457 #endif /* ! _MACHINE_ATOMIC_H_ */
458