xref: /freebsd/sys/amd64/include/atomic.h (revision ebccf1e3a6b11b97cbf5f813dd76636e892a9035)
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 int atomic_cmpset_long(volatile u_long *dst, u_long exp, u_long src);
76 
77 #define	ATOMIC_STORE_LOAD(TYPE, LOP, SOP)			\
78 u_##TYPE	atomic_load_acq_##TYPE(volatile u_##TYPE *p);	\
79 void		atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)
80 
81 #else /* !KLD_MODULE */
82 
83 #if defined(__GNUCLIKE_ASM) && defined(__CC_SUPPORTS___INLINE)
84 
85 /*
86  * For userland, assume the SMP case and use lock prefixes so that
87  * the binaries will run on both types of systems.
88  */
89 #if defined(SMP) || !defined(_KERNEL)
90 #define MPLOCKED	lock ;
91 #else
92 #define MPLOCKED
93 #endif
94 
95 /*
96  * The assembly is volatilized to demark potential before-and-after side
97  * effects if an interrupt or SMP collision were to occur.
98  */
99 #define ATOMIC_ASM(NAME, TYPE, OP, CONS, V)		\
100 static __inline void					\
101 atomic_##NAME##_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\
102 {							\
103 	__asm __volatile(__XSTRING(MPLOCKED) OP		\
104 			 : "+m" (*p)			\
105 			 : CONS (V));			\
106 }							\
107 struct __hack
108 
109 #else /* !(__GNUCLIKE_ASM && __CC_SUPPORTS___INLINE) */
110 
111 #define ATOMIC_ASM(NAME, TYPE, OP, CONS, V)				\
112 extern void atomic_##NAME##_##TYPE(volatile u_##TYPE *p, u_##TYPE v)
113 
114 #endif /* __GNUCLIKE_ASM && __CC_SUPPORTS___INLINE */
115 
116 /*
117  * Atomic compare and set, used by the mutex functions
118  *
119  * if (*dst == exp) *dst = src (all 32 bit words)
120  *
121  * Returns 0 on failure, non-zero on success
122  */
123 
124 #if defined(__GNUCLIKE_ASM) && defined(__CC_SUPPORTS___INLINE)
125 
126 static __inline int
127 atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src)
128 {
129 	int res = exp;
130 
131 	__asm __volatile (
132 	"	" __XSTRING(MPLOCKED) "	"
133 	"	cmpxchgl %1,%2 ;	"
134 	"       setz	%%al ;		"
135 	"	movzbl	%%al,%0 ;	"
136 	"1:				"
137 	"# atomic_cmpset_int"
138 	: "+a" (res)			/* 0 (result) */
139 	: "r" (src),			/* 1 */
140 	  "m" (*(dst))			/* 2 */
141 	: "memory");
142 
143 	return (res);
144 }
145 
146 static __inline int
147 atomic_cmpset_long(volatile u_long *dst, u_long exp, u_long src)
148 {
149 	long res = exp;
150 
151 	__asm __volatile (
152 	"	" __XSTRING(MPLOCKED) "	"
153 	"	cmpxchgq %1,%2 ;	"
154 	"       setz	%%al ;		"
155 	"	movzbq	%%al,%0 ;	"
156 	"1:				"
157 	"# atomic_cmpset_long"
158 	: "+a" (res)			/* 0 (result) */
159 	: "r" (src),			/* 1 */
160 	  "m" (*(dst))			/* 2 */
161 	: "memory");
162 
163 	return (res);
164 }
165 #endif /* __GNUCLIKE_ASM && __CC_SUPPORTS___INLINE */
166 
167 #if defined(__GNUCLIKE_ASM) && defined(__CC_SUPPORTS___INLINE)
168 
169 #define ATOMIC_STORE_LOAD(TYPE, LOP, SOP)		\
170 static __inline u_##TYPE				\
171 atomic_load_acq_##TYPE(volatile u_##TYPE *p)		\
172 {							\
173 	u_##TYPE res;					\
174 							\
175 	__asm __volatile(__XSTRING(MPLOCKED) LOP	\
176 	: "=a" (res),			/* 0 (result) */\
177 	  "+m" (*p)			/* 1 */		\
178 	: : "memory");				 	\
179 							\
180 	return (res);					\
181 }							\
182 							\
183 /*							\
184  * The XCHG instruction asserts LOCK automagically.	\
185  */							\
186 static __inline void					\
187 atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\
188 {							\
189 	__asm __volatile(SOP				\
190 	: "+m" (*p),			/* 0 */		\
191 	  "+r" (v)			/* 1 */		\
192 	: : "memory");				 	\
193 }							\
194 struct __hack
195 
196 #else /* !(__GNUCLIKE_ASM && __CC_SUPPORTS___INLINE) */
197 
198 extern int atomic_cmpset_int(volatile u_int *, u_int, u_int);
199 extern int atomic_cmpset_long(volatile u_long *, u_long, u_long);
200 
201 #define ATOMIC_STORE_LOAD(TYPE, LOP, SOP)				\
202 extern u_##TYPE atomic_load_acq_##TYPE(volatile u_##TYPE *p);		\
203 extern void atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)
204 
205 #endif /* __GNUCLIKE_ASM && __CC_SUPPORTS___INLINE */
206 
207 #endif /* KLD_MODULE */
208 
209 ATOMIC_ASM(set,	     char,  "orb %b1,%0",  "iq",  v);
210 ATOMIC_ASM(clear,    char,  "andb %b1,%0", "iq", ~v);
211 ATOMIC_ASM(add,	     char,  "addb %b1,%0", "iq",  v);
212 ATOMIC_ASM(subtract, char,  "subb %b1,%0", "iq",  v);
213 
214 ATOMIC_ASM(set,	     short, "orw %w1,%0",  "ir",  v);
215 ATOMIC_ASM(clear,    short, "andw %w1,%0", "ir", ~v);
216 ATOMIC_ASM(add,	     short, "addw %w1,%0", "ir",  v);
217 ATOMIC_ASM(subtract, short, "subw %w1,%0", "ir",  v);
218 
219 ATOMIC_ASM(set,	     int,   "orl %1,%0",   "ir",  v);
220 ATOMIC_ASM(clear,    int,   "andl %1,%0",  "ir", ~v);
221 ATOMIC_ASM(add,	     int,   "addl %1,%0",  "ir",  v);
222 ATOMIC_ASM(subtract, int,   "subl %1,%0",  "ir",  v);
223 
224 ATOMIC_ASM(set,	     long,  "orq %1,%0",   "ir",  v);
225 ATOMIC_ASM(clear,    long,  "andq %1,%0",  "ir", ~v);
226 ATOMIC_ASM(add,	     long,  "addq %1,%0",  "ir",  v);
227 ATOMIC_ASM(subtract, long,  "subq %1,%0",  "ir",  v);
228 
229 ATOMIC_STORE_LOAD(char,	"cmpxchgb %b0,%1", "xchgb %b1,%0");
230 ATOMIC_STORE_LOAD(short,"cmpxchgw %w0,%1", "xchgw %w1,%0");
231 ATOMIC_STORE_LOAD(int,	"cmpxchgl %0,%1",  "xchgl %1,%0");
232 ATOMIC_STORE_LOAD(long,	"cmpxchgq %0,%1",  "xchgq %1,%0");
233 
234 #undef ATOMIC_ASM
235 #undef ATOMIC_STORE_LOAD
236 
237 #define	atomic_set_acq_char		atomic_set_char
238 #define	atomic_set_rel_char		atomic_set_char
239 #define	atomic_clear_acq_char		atomic_clear_char
240 #define	atomic_clear_rel_char		atomic_clear_char
241 #define	atomic_add_acq_char		atomic_add_char
242 #define	atomic_add_rel_char		atomic_add_char
243 #define	atomic_subtract_acq_char	atomic_subtract_char
244 #define	atomic_subtract_rel_char	atomic_subtract_char
245 
246 #define	atomic_set_acq_short		atomic_set_short
247 #define	atomic_set_rel_short		atomic_set_short
248 #define	atomic_clear_acq_short		atomic_clear_short
249 #define	atomic_clear_rel_short		atomic_clear_short
250 #define	atomic_add_acq_short		atomic_add_short
251 #define	atomic_add_rel_short		atomic_add_short
252 #define	atomic_subtract_acq_short	atomic_subtract_short
253 #define	atomic_subtract_rel_short	atomic_subtract_short
254 
255 #define	atomic_set_acq_int		atomic_set_int
256 #define	atomic_set_rel_int		atomic_set_int
257 #define	atomic_clear_acq_int		atomic_clear_int
258 #define	atomic_clear_rel_int		atomic_clear_int
259 #define	atomic_add_acq_int		atomic_add_int
260 #define	atomic_add_rel_int		atomic_add_int
261 #define	atomic_subtract_acq_int		atomic_subtract_int
262 #define	atomic_subtract_rel_int		atomic_subtract_int
263 #define atomic_cmpset_acq_int		atomic_cmpset_int
264 #define atomic_cmpset_rel_int		atomic_cmpset_int
265 
266 #define	atomic_set_acq_long		atomic_set_long
267 #define	atomic_set_rel_long		atomic_set_long
268 #define	atomic_clear_acq_long		atomic_clear_long
269 #define	atomic_clear_rel_long		atomic_clear_long
270 #define	atomic_add_acq_long		atomic_add_long
271 #define	atomic_add_rel_long		atomic_add_long
272 #define	atomic_subtract_acq_long	atomic_subtract_long
273 #define	atomic_subtract_rel_long	atomic_subtract_long
274 
275 #define atomic_cmpset_acq_ptr		atomic_cmpset_ptr
276 #define atomic_cmpset_rel_ptr		atomic_cmpset_ptr
277 
278 #define	atomic_set_8		atomic_set_char
279 #define	atomic_set_acq_8	atomic_set_acq_char
280 #define	atomic_set_rel_8	atomic_set_rel_char
281 #define	atomic_clear_8		atomic_clear_char
282 #define	atomic_clear_acq_8	atomic_clear_acq_char
283 #define	atomic_clear_rel_8	atomic_clear_rel_char
284 #define	atomic_add_8		atomic_add_char
285 #define	atomic_add_acq_8	atomic_add_acq_char
286 #define	atomic_add_rel_8	atomic_add_rel_char
287 #define	atomic_subtract_8	atomic_subtract_char
288 #define	atomic_subtract_acq_8	atomic_subtract_acq_char
289 #define	atomic_subtract_rel_8	atomic_subtract_rel_char
290 #define	atomic_load_acq_8	atomic_load_acq_char
291 #define	atomic_store_rel_8	atomic_store_rel_char
292 
293 #define	atomic_set_16		atomic_set_short
294 #define	atomic_set_acq_16	atomic_set_acq_short
295 #define	atomic_set_rel_16	atomic_set_rel_short
296 #define	atomic_clear_16		atomic_clear_short
297 #define	atomic_clear_acq_16	atomic_clear_acq_short
298 #define	atomic_clear_rel_16	atomic_clear_rel_short
299 #define	atomic_add_16		atomic_add_short
300 #define	atomic_add_acq_16	atomic_add_acq_short
301 #define	atomic_add_rel_16	atomic_add_rel_short
302 #define	atomic_subtract_16	atomic_subtract_short
303 #define	atomic_subtract_acq_16	atomic_subtract_acq_short
304 #define	atomic_subtract_rel_16	atomic_subtract_rel_short
305 #define	atomic_load_acq_16	atomic_load_acq_short
306 #define	atomic_store_rel_16	atomic_store_rel_short
307 
308 #define	atomic_set_32		atomic_set_int
309 #define	atomic_set_acq_32	atomic_set_acq_int
310 #define	atomic_set_rel_32	atomic_set_rel_int
311 #define	atomic_clear_32		atomic_clear_int
312 #define	atomic_clear_acq_32	atomic_clear_acq_int
313 #define	atomic_clear_rel_32	atomic_clear_rel_int
314 #define	atomic_add_32		atomic_add_int
315 #define	atomic_add_acq_32	atomic_add_acq_int
316 #define	atomic_add_rel_32	atomic_add_rel_int
317 #define	atomic_subtract_32	atomic_subtract_int
318 #define	atomic_subtract_acq_32	atomic_subtract_acq_int
319 #define	atomic_subtract_rel_32	atomic_subtract_rel_int
320 #define	atomic_load_acq_32	atomic_load_acq_int
321 #define	atomic_store_rel_32	atomic_store_rel_int
322 #define	atomic_cmpset_32	atomic_cmpset_int
323 #define	atomic_cmpset_acq_32	atomic_cmpset_acq_int
324 #define	atomic_cmpset_rel_32	atomic_cmpset_rel_int
325 #define	atomic_readandclear_32	atomic_readandclear_int
326 
327 #if !defined(WANT_FUNCTIONS)
328 static __inline int
329 atomic_cmpset_ptr(volatile void *dst, void *exp, void *src)
330 {
331 
332 	return (atomic_cmpset_long((volatile u_long *)dst,
333 	    (u_long)exp, (u_long)src));
334 }
335 
336 static __inline void *
337 atomic_load_acq_ptr(volatile void *p)
338 {
339 	/*
340 	 * The apparently-bogus cast to intptr_t in the following is to
341 	 * avoid a warning from "gcc -Wbad-function-cast".
342 	 */
343 	return ((void *)(intptr_t)atomic_load_acq_long((volatile u_long *)p));
344 }
345 
346 static __inline void
347 atomic_store_rel_ptr(volatile void *p, void *v)
348 {
349 	atomic_store_rel_long((volatile u_long *)p, (u_long)v);
350 }
351 
352 #define ATOMIC_PTR(NAME)				\
353 static __inline void					\
354 atomic_##NAME##_ptr(volatile void *p, uintptr_t v)	\
355 {							\
356 	atomic_##NAME##_long((volatile u_long *)p, v);	\
357 }							\
358 							\
359 static __inline void					\
360 atomic_##NAME##_acq_ptr(volatile void *p, uintptr_t v)	\
361 {							\
362 	atomic_##NAME##_acq_long((volatile u_long *)p, v);\
363 }							\
364 							\
365 static __inline void					\
366 atomic_##NAME##_rel_ptr(volatile void *p, uintptr_t v)	\
367 {							\
368 	atomic_##NAME##_rel_long((volatile u_long *)p, v);\
369 }
370 
371 ATOMIC_PTR(set)
372 ATOMIC_PTR(clear)
373 ATOMIC_PTR(add)
374 ATOMIC_PTR(subtract)
375 
376 #undef ATOMIC_PTR
377 
378 #if defined(__GNUCLIKE_ASM) && defined(__CC_SUPPORTS___INLINE)
379 
380 static __inline u_int
381 atomic_readandclear_int(volatile u_int *addr)
382 {
383 	u_int result;
384 
385 	__asm __volatile (
386 	"	xorl	%0,%0 ;		"
387 	"	xchgl	%1,%0 ;		"
388 	"# atomic_readandclear_int"
389 	: "=&r" (result)		/* 0 (result) */
390 	: "m" (*addr));			/* 1 (addr) */
391 
392 	return (result);
393 }
394 
395 static __inline u_long
396 atomic_readandclear_long(volatile u_long *addr)
397 {
398 	u_long result;
399 
400 	__asm __volatile (
401 	"	xorq	%0,%0 ;		"
402 	"	xchgq	%1,%0 ;		"
403 	"# atomic_readandclear_int"
404 	: "=&r" (result)		/* 0 (result) */
405 	: "m" (*addr));			/* 1 (addr) */
406 
407 	return (result);
408 }
409 
410 #else /* !(__GNUCLIKE_ASM && __CC_SUPPORTS___INLINE) */
411 
412 extern u_long	atomic_readandclear_long(volatile u_long *);
413 extern u_int	atomic_readandclear_int(volatile u_int *);
414 
415 #endif /* __GNUCLIKE_ASM && __CC_SUPPORTS___INLINE */
416 
417 #endif	/* !defined(WANT_FUNCTIONS) */
418 #endif /* ! _MACHINE_ATOMIC_H_ */
419