AMD64 - Sync AMD64 support from Jordan Gordeev's svn repository and
[dragonfly.git] / sys / cpu / amd64 / include / atomic.h
1 /*-
2  * Copyright (c) 1998 Doug Rabson.
3  * Copyright (c) 2008 The DragonFly Project.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/amd64/include/atomic.h,v 1.32 2003/11/21 03:02:00 peter Exp $
28  * $DragonFly: src/sys/cpu/amd64/include/atomic.h,v 1.3 2008/08/29 17:07:06 dillon Exp $
29  */
30 #ifndef _CPU_ATOMIC_H_
31 #define _CPU_ATOMIC_H_
32
33 #ifndef _SYS_TYPES_H_
34 #include <sys/types.h>
35 #endif
36
37 /*
38  * Various simple operations on memory, each of which is atomic in the
39  * presence of interrupts and multiple processors.
40  *
41  * atomic_set_char(P, V)        (*(u_char *)(P) |= (V))
42  * atomic_clear_char(P, V)      (*(u_char *)(P) &= ~(V))
43  * atomic_add_char(P, V)        (*(u_char *)(P) += (V))
44  * atomic_subtract_char(P, V)   (*(u_char *)(P) -= (V))
45  *
46  * atomic_set_short(P, V)       (*(u_short *)(P) |= (V))
47  * atomic_clear_short(P, V)     (*(u_short *)(P) &= ~(V))
48  * atomic_add_short(P, V)       (*(u_short *)(P) += (V))
49  * atomic_subtract_short(P, V)  (*(u_short *)(P) -= (V))
50  *
51  * atomic_set_int(P, V)         (*(u_int *)(P) |= (V))
52  * atomic_clear_int(P, V)       (*(u_int *)(P) &= ~(V))
53  * atomic_add_int(P, V)         (*(u_int *)(P) += (V))
54  * atomic_subtract_int(P, V)    (*(u_int *)(P) -= (V))
55  * atomic_readandclear_int(P)   (return (*(u_int *)(P)); *(u_int *)(P) = 0;)
56  *
57  * atomic_set_long(P, V)        (*(u_long *)(P) |= (V))
58  * atomic_clear_long(P, V)      (*(u_long *)(P) &= ~(V))
59  * atomic_add_long(P, V)        (*(u_long *)(P) += (V))
60  * atomic_subtract_long(P, V)   (*(u_long *)(P) -= (V))
61  * atomic_readandclear_long(P)  (return (*(u_long *)(P)); *(u_long *)(P) = 0;)
62  */
63
64 /*
65  * The above functions are expanded inline in the statically-linked
66  * kernel.  Lock prefixes are generated if an SMP kernel is being
67  * built.
68  *
69  * Kernel modules call real functions which are built into the kernel.
70  * This allows kernel modules to be portable between UP and SMP systems.
71  */
72 #if defined(KLD_MODULE)
73 #define ATOMIC_ASM(NAME, TYPE, OP, CONS, V)                     \
74 void atomic_##NAME##_##TYPE(volatile u_##TYPE *p, u_##TYPE v);  \
75 void atomic_##NAME##_##TYPE##_nonlocked(volatile u_##TYPE *p, u_##TYPE v);
76
77 int     atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src);
78 int     atomic_cmpset_long(volatile u_long *dst, u_long exp, u_long src);
79 u_int   atomic_fetchadd_int(volatile u_int *p, u_int v);
80 u_long  atomic_fetchadd_long(volatile u_long *p, u_long v);
81
82 #define ATOMIC_STORE_LOAD(TYPE, LOP, SOP)                       \
83 u_##TYPE        atomic_load_acq_##TYPE(volatile u_##TYPE *p);   \
84 void            atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)
85
86 #else /* !KLD_MODULE */
87
88 #ifdef __GNUC__
89
90 /*
91  * For userland, always use lock prefixes so that the binaries will run
92  * on both SMP and !SMP systems.
93  */
94 #if defined(SMP) || !defined(_KERNEL)
95 #define MPLOCKED        "lock ; "
96 #else
97 #define MPLOCKED
98 #endif
99
100 /*
101  * The assembly is volatilized to demark potential before-and-after side
102  * effects if an interrupt or SMP collision were to occur.
103  */
104 #define ATOMIC_ASM(NAME, TYPE, OP, CONS, V)             \
105 static __inline void                                    \
106 atomic_##NAME##_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\
107 {                                                       \
108         __asm __volatile(MPLOCKED OP                    \
109                          : "+m" (*p)                    \
110                          : CONS (V));                   \
111 }                                                       \
112 static __inline void                                    \
113 atomic_##NAME##_##TYPE##_nonlocked(volatile u_##TYPE *p, u_##TYPE v)\
114 {                                                       \
115         __asm __volatile(OP                             \
116                          : "+m" (*p)                    \
117                          : CONS (V));                   \
118 }
119
120 #else /* !__GNUC__ */
121
122 #define ATOMIC_ASM(NAME, TYPE, OP, CONS, V)                             \
123 extern void atomic_##NAME##_##TYPE(volatile u_##TYPE *p, u_##TYPE v);   \
124 extern void atomic_##NAME##_##TYPE##_nonlocked(volatile u_##TYPE *p, u_##TYPE v);
125
126 #endif /* __GNUC__ */
127
128 /*
129  * These functions operate on a 32 bit interrupt interlock which is defined
130  * as follows:
131  *
132  *      bit 0-30        interrupt handler disabled bits (counter)
133  *      bit 31          interrupt handler currently running bit (1 = run)
134  *
135  * atomic_intr_cond_test(P)     Determine if the interlock is in an
136  *                              acquired state.  Returns 0 if it not
137  *                              acquired, non-zero if it is.
138  *
139  * atomic_intr_cond_try(P)
140  *                              Increment the request counter and attempt to
141  *                              set bit 31 to acquire the interlock.  If
142  *                              we are unable to set bit 31 the request
143  *                              counter is decremented and we return -1,
144  *                              otherwise we return 0.
145  *
146  * atomic_intr_cond_enter(P, func, arg)
147  *                              Increment the request counter and attempt to
148  *                              set bit 31 to acquire the interlock.  If
149  *                              we are unable to set bit 31 func(arg) is
150  *                              called in a loop until we are able to set
151  *                              bit 31.
152  *
153  * atomic_intr_cond_exit(P, func, arg)
154  *                              Decrement the request counter and clear bit
155  *                              31.  If the request counter is still non-zero
156  *                              call func(arg) once.
157  *
158  * atomic_intr_handler_disable(P)
159  *                              Set bit 30, indicating that the interrupt
160  *                              handler has been disabled.  Must be called
161  *                              after the hardware is disabled.
162  *
163  *                              Returns bit 31 indicating whether a serialized
164  *                              accessor is active (typically the interrupt
165  *                              handler is running).  0 == not active,
166  *                              non-zero == active.
167  *
168  * atomic_intr_handler_enable(P)
169  *                              Clear bit 30, indicating that the interrupt
170  *                              handler has been enabled.  Must be called
171  *                              before the hardware is actually enabled.
172  *
173  * atomic_intr_handler_is_enabled(P)
174  *                              Returns bit 30, 0 indicates that the handler
175  *                              is enabled, non-zero indicates that it is
176  *                              disabled.  The request counter portion of
177  *                              the field is ignored.
178  */
179
180 #ifndef __ATOMIC_INTR_T
181 #define __ATOMIC_INTR_T
182 typedef volatile int atomic_intr_t;
183 #endif
184
185 #if defined(KLD_MODULE)
186
187 void atomic_intr_init(atomic_intr_t *p);
188 int atomic_intr_handler_disable(atomic_intr_t *p);
189 void atomic_intr_handler_enable(atomic_intr_t *p);
190 int atomic_intr_handler_is_enabled(atomic_intr_t *p);
191 int atomic_intr_cond_test(atomic_intr_t *p);
192 int atomic_intr_cond_try(atomic_intr_t *p);
193 void atomic_intr_cond_enter(atomic_intr_t *p, void (*func)(void *), void *arg);
194 void atomic_intr_cond_exit(atomic_intr_t *p, void (*func)(void *), void *arg);
195
196 #else /* !KLD_MODULE */
197
198 static __inline
199 void
200 atomic_intr_init(atomic_intr_t *p)
201 {
202         *p = 0;
203 }
204
205 static __inline
206 int
207 atomic_intr_handler_disable(atomic_intr_t *p)
208 {
209         int data;
210
211         __asm __volatile(MPLOCKED "orl $0x40000000,%1; movl %1,%%eax; " \
212                                   "andl $0x80000000,%%eax" \
213                                   : "=a"(data) , "+m"(*p));
214         return(data);
215 }
216
217 static __inline
218 void
219 atomic_intr_handler_enable(atomic_intr_t *p)
220 {
221         __asm __volatile(MPLOCKED "andl $0xBFFFFFFF,%0" : "+m" (*p));
222 }
223
224 static __inline
225 int
226 atomic_intr_handler_is_enabled(atomic_intr_t *p)
227 {
228         int data;
229
230         __asm __volatile("movl %1,%%eax; andl $0x40000000,%%eax" \
231                          : "=a"(data) : "m"(*p));
232         return(data);
233 }
234
235 static __inline
236 void
237 atomic_intr_cond_enter(atomic_intr_t *p, void (*func)(void *), void *arg)
238 {
239         __asm __volatile(MPLOCKED "incl %0; " \
240                          "1: ;" \
241                          MPLOCKED "btsl $31,%0; jnc 2f; " \
242                          "movq %2,%%rdi; call *%1; " \
243                          "jmp 1b; " \
244                          "2: ;" \
245                          : "+m" (*p) \
246                          : "r"(func), "m"(arg) \
247                          : "ax", "cx", "dx", "di");     /* XXX clobbers more regs */
248 }
249
250 /*
251  * Atomically add the value of v to the integer pointed to by p and return
252  * the previous value of *p.
253  */
254 static __inline u_int
255 atomic_fetchadd_int(volatile u_int *p, u_int v)
256 {
257
258         __asm __volatile(
259         "       " MPLOCKED "            "
260         "       xaddl   %0, %1 ;        "
261         "# atomic_fetchadd_int"
262         : "+r" (v),                     /* 0 (result) */
263           "=m" (*p)                     /* 1 */
264         : "m" (*p));                    /* 2 */
265
266         return (v);
267 }
268
269 /*
270  * Atomically add the value of v to the long integer pointed to by p and return
271  * the previous value of *p.
272  */
273 static __inline u_long
274 atomic_fetchadd_long(volatile u_long *p, u_long v)
275 {
276
277         __asm __volatile(
278         "       " MPLOCKED "            "
279         "       xaddq   %0, %1 ;        "
280         "# atomic_fetchadd_long"
281         : "+r" (v),                     /* 0 (result) */
282           "=m" (*p)                     /* 1 */
283         : "m" (*p));                    /* 2 */
284
285         return (v);
286 }
287 /*
288  * Attempt to enter the interrupt condition variable.  Returns zero on
289  * success, 1 on failure.
290  */
291 static __inline
292 int
293 atomic_intr_cond_try(atomic_intr_t *p)
294 {
295         int ret;
296
297         __asm __volatile(MPLOCKED "incl %0; " \
298                          "1: ;" \
299                          "subl %%eax,%%eax; " \
300                          MPLOCKED "btsl $31,%0; jnc 2f; " \
301                          MPLOCKED "decl %0; " \
302                          "movl $1,%%eax;" \
303                          "2: ;" \
304                          : "+m" (*p), "=a"(ret) \
305                          : : "cx", "dx");
306         return (ret);
307 }
308
309
310 static __inline
311 int
312 atomic_intr_cond_test(atomic_intr_t *p)
313 {
314         return((int)(*p & 0x80000000));
315 }
316
317 static __inline
318 void
319 atomic_intr_cond_exit(atomic_intr_t *p, void (*func)(void *), void *arg)
320 {
321         __asm __volatile(MPLOCKED "decl %0; " \
322                         MPLOCKED "btrl $31,%0; " \
323                         "testl $0x3FFFFFFF,%0; jz 1f; " \
324                          "movq %2,%%rdi; call *%1; " \
325                          "1: ;" \
326                          : "+m" (*p) \
327                          : "r"(func), "m"(arg) \
328                          : "ax", "cx", "dx", "di");     /* XXX clobbers more regs */
329 }
330
331 #endif
332
333 /*
334  * Atomic compare and set, used by the mutex functions
335  *
336  * if (*dst == exp) *dst = src (all 32 bit words)
337  *
338  * Returns 0 on failure, non-zero on success
339  */
340
341 #if defined(__GNUC__)
342
343 static __inline int
344 atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src)
345 {
346         int res = exp;
347
348         __asm __volatile (
349                 MPLOCKED
350         "       cmpxchgl %1,%2 ;        "
351         "       setz    %%al ;          "
352         "       movzbl  %%al,%0 ;       "
353         "1:                             "
354         "# atomic_cmpset_int"
355         : "+a" (res)                    /* 0 (result) */
356         : "r" (src),                    /* 1 */
357           "m" (*(dst))                  /* 2 */
358         : "memory");                             
359
360         return (res);
361 }
362
363 static __inline int
364 atomic_cmpset_long(volatile u_long *dst, u_long exp, u_long src)
365 {
366         long res = exp;
367
368         __asm __volatile (
369                 MPLOCKED
370         "       cmpxchgq %1,%2 ;        "
371         "       setz    %%al ;          "
372         "       movzbq  %%al,%0 ;       "
373         "1:                             "
374         "# atomic_cmpset_long"
375         : "+a" (res)                    /* 0 (result) */
376         : "r" (src),                    /* 1 */
377           "m" (*(dst))                  /* 2 */
378         : "memory");                             
379
380         return (res);
381 }
382 #endif /* defined(__GNUC__) */
383
384 #if defined(__GNUC__)
385
386 #define ATOMIC_STORE_LOAD(TYPE, LOP, SOP)               \
387 static __inline u_##TYPE                                \
388 atomic_load_acq_##TYPE(volatile u_##TYPE *p)            \
389 {                                                       \
390         u_##TYPE res;                                   \
391                                                         \
392         __asm __volatile(MPLOCKED LOP                   \
393         : "=a" (res),                   /* 0 (result) */\
394           "+m" (*p)                     /* 1 */         \
395         : : "memory");                                  \
396                                                         \
397         return (res);                                   \
398 }                                                       \
399                                                         \
400 /*                                                      \
401  * The XCHG instruction asserts LOCK automagically.     \
402  */                                                     \
403 static __inline void                                    \
404 atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\
405 {                                                       \
406         __asm __volatile(SOP                            \
407         : "+m" (*p),                    /* 0 */         \
408           "+r" (v)                      /* 1 */         \
409         : : "memory");                                  \
410 }                                                       \
411 struct __hack
412
413 #else /* !defined(__GNUC__) */
414
415 extern int atomic_cmpset_int(volatile u_int *, u_int, u_int);
416 extern int atomic_cmpset_long(volatile u_long *, u_long, u_long);
417
418 #define ATOMIC_STORE_LOAD(TYPE, LOP, SOP)                               \
419 extern u_##TYPE atomic_load_acq_##TYPE(volatile u_##TYPE *p);           \
420 extern void atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)
421
422 #endif /* defined(__GNUC__) */
423
424 #endif /* !KLD_MODULE */
425
426 ATOMIC_ASM(set,      char,  "orb %b1,%0",  "iq",  v);
427 ATOMIC_ASM(clear,    char,  "andb %b1,%0", "iq", ~v);
428 ATOMIC_ASM(add,      char,  "addb %b1,%0", "iq",  v);
429 ATOMIC_ASM(subtract, char,  "subb %b1,%0", "iq",  v);
430
431 ATOMIC_ASM(set,      short, "orw %w1,%0",  "ir",  v);
432 ATOMIC_ASM(clear,    short, "andw %w1,%0", "ir", ~v);
433 ATOMIC_ASM(add,      short, "addw %w1,%0", "ir",  v);
434 ATOMIC_ASM(subtract, short, "subw %w1,%0", "ir",  v);
435
436 ATOMIC_ASM(set,      int,   "orl %1,%0",   "ir",  v);
437 ATOMIC_ASM(clear,    int,   "andl %1,%0",  "ir", ~v);
438 ATOMIC_ASM(add,      int,   "addl %1,%0",  "ir",  v);
439 ATOMIC_ASM(subtract, int,   "subl %1,%0",  "ir",  v);
440
441 ATOMIC_ASM(set,      long,  "orq %1,%0",   "ir",  v);
442 ATOMIC_ASM(clear,    long,  "andq %1,%0",  "ir", ~v);
443 ATOMIC_ASM(add,      long,  "addq %1,%0",  "ir",  v);
444 ATOMIC_ASM(subtract, long,  "subq %1,%0",  "ir",  v);
445
446 ATOMIC_STORE_LOAD(char, "cmpxchgb %b0,%1", "xchgb %b1,%0");
447 ATOMIC_STORE_LOAD(short,"cmpxchgw %w0,%1", "xchgw %w1,%0");
448 ATOMIC_STORE_LOAD(int,  "cmpxchgl %0,%1",  "xchgl %1,%0");
449 ATOMIC_STORE_LOAD(long, "cmpxchgq %0,%1",  "xchgq %1,%0");
450
451 #define atomic_cmpset_32        atomic_cmpset_int
452
453 #undef ATOMIC_ASM
454 #undef ATOMIC_STORE_LOAD
455
456 #define atomic_set_acq_char             atomic_set_char
457 #define atomic_set_rel_char             atomic_set_char
458 #define atomic_clear_acq_char           atomic_clear_char
459 #define atomic_clear_rel_char           atomic_clear_char
460 #define atomic_add_acq_char             atomic_add_char
461 #define atomic_add_rel_char             atomic_add_char
462 #define atomic_subtract_acq_char        atomic_subtract_char
463 #define atomic_subtract_rel_char        atomic_subtract_char
464
465 #define atomic_set_acq_short            atomic_set_short
466 #define atomic_set_rel_short            atomic_set_short
467 #define atomic_clear_acq_short          atomic_clear_short
468 #define atomic_clear_rel_short          atomic_clear_short
469 #define atomic_add_acq_short            atomic_add_short
470 #define atomic_add_rel_short            atomic_add_short
471 #define atomic_subtract_acq_short       atomic_subtract_short
472 #define atomic_subtract_rel_short       atomic_subtract_short
473
474 #define atomic_set_acq_int              atomic_set_int
475 #define atomic_set_rel_int              atomic_set_int
476 #define atomic_clear_acq_int            atomic_clear_int
477 #define atomic_clear_rel_int            atomic_clear_int
478 #define atomic_add_acq_int              atomic_add_int
479 #define atomic_add_rel_int              atomic_add_int
480 #define atomic_subtract_acq_int         atomic_subtract_int
481 #define atomic_subtract_rel_int         atomic_subtract_int
482 #define atomic_cmpset_acq_int           atomic_cmpset_int
483 #define atomic_cmpset_rel_int           atomic_cmpset_int
484
485 #define atomic_set_acq_long             atomic_set_long
486 #define atomic_set_rel_long             atomic_set_long
487 #define atomic_clear_acq_long           atomic_clear_long
488 #define atomic_clear_rel_long           atomic_clear_long
489 #define atomic_add_acq_long             atomic_add_long
490 #define atomic_add_rel_long             atomic_add_long
491 #define atomic_subtract_acq_long        atomic_subtract_long
492 #define atomic_subtract_rel_long        atomic_subtract_long
493
494 #define atomic_set_8            atomic_set_char
495 #define atomic_set_acq_8        atomic_set_acq_char
496 #define atomic_set_rel_8        atomic_set_rel_char
497 #define atomic_clear_8          atomic_clear_char
498 #define atomic_clear_acq_8      atomic_clear_acq_char
499 #define atomic_clear_rel_8      atomic_clear_rel_char
500 #define atomic_add_8            atomic_add_char
501 #define atomic_add_acq_8        atomic_add_acq_char
502 #define atomic_add_rel_8        atomic_add_rel_char
503 #define atomic_subtract_8       atomic_subtract_char
504 #define atomic_subtract_acq_8   atomic_subtract_acq_char
505 #define atomic_subtract_rel_8   atomic_subtract_rel_char
506 #define atomic_load_acq_8       atomic_load_acq_char
507 #define atomic_store_rel_8      atomic_store_rel_char
508
509 /* Operations on 16-bit words. */
510 #define atomic_set_16           atomic_set_short
511 #define atomic_set_acq_16       atomic_set_acq_short
512 #define atomic_set_rel_16       atomic_set_rel_short
513 #define atomic_clear_16         atomic_clear_short
514 #define atomic_clear_acq_16     atomic_clear_acq_short
515 #define atomic_clear_rel_16     atomic_clear_rel_short
516 #define atomic_add_16           atomic_add_short
517 #define atomic_add_acq_16       atomic_add_acq_short
518 #define atomic_add_rel_16       atomic_add_rel_short
519 #define atomic_subtract_16      atomic_subtract_short
520 #define atomic_subtract_acq_16  atomic_subtract_acq_short
521 #define atomic_subtract_rel_16  atomic_subtract_rel_short
522 #define atomic_load_acq_16      atomic_load_acq_short
523 #define atomic_store_rel_16     atomic_store_rel_short
524
525 /* Operations on 32-bit double words. */
526 #define atomic_set_32           atomic_set_int
527 #define atomic_set_acq_32       atomic_set_acq_int
528 #define atomic_set_rel_32       atomic_set_rel_int
529 #define atomic_clear_32         atomic_clear_int
530 #define atomic_clear_acq_32     atomic_clear_acq_int
531 #define atomic_clear_rel_32     atomic_clear_rel_int
532 #define atomic_add_32           atomic_add_int
533 #define atomic_add_acq_32       atomic_add_acq_int
534 #define atomic_add_rel_32       atomic_add_rel_int
535 #define atomic_subtract_32      atomic_subtract_int
536 #define atomic_subtract_acq_32  atomic_subtract_acq_int
537 #define atomic_subtract_rel_32  atomic_subtract_rel_int
538 #define atomic_load_acq_32      atomic_load_acq_int
539 #define atomic_store_rel_32     atomic_store_rel_int
540 #define atomic_cmpset_32        atomic_cmpset_int
541 #define atomic_cmpset_acq_32    atomic_cmpset_acq_int
542 #define atomic_cmpset_rel_32    atomic_cmpset_rel_int
543 #define atomic_readandclear_32  atomic_readandclear_int
544 #define atomic_fetchadd_32      atomic_fetchadd_int
545
546 /* Operations on pointers. */
547 #define atomic_set_ptr          atomic_set_long
548 #define atomic_set_acq_ptr      atomic_set_acq_long
549 #define atomic_set_rel_ptr      atomic_set_rel_long
550 #define atomic_clear_ptr        atomic_clear_long
551 #define atomic_clear_acq_ptr    atomic_clear_acq_long
552 #define atomic_clear_rel_ptr    atomic_clear_rel_long
553 #define atomic_add_ptr          atomic_add_long
554 #define atomic_add_acq_ptr      atomic_add_acq_long
555 #define atomic_add_rel_ptr      atomic_add_rel_long
556 #define atomic_subtract_ptr     atomic_subtract_long
557 #define atomic_subtract_acq_ptr atomic_subtract_acq_long
558 #define atomic_subtract_rel_ptr atomic_subtract_rel_long
559 #define atomic_load_acq_ptr     atomic_load_acq_long
560 #define atomic_store_rel_ptr    atomic_store_rel_long
561 #define atomic_cmpset_ptr       atomic_cmpset_long
562 #define atomic_cmpset_acq_ptr   atomic_cmpset_acq_long
563 #define atomic_cmpset_rel_ptr   atomic_cmpset_rel_long
564 #define atomic_readandclear_ptr atomic_readandclear_long
565
566 #if defined(__GNUC__)
567
568 #if defined(KLD_MODULE)
569 extern u_int atomic_readandclear_int(volatile u_int *addr);
570 extern u_long atomic_readandclear_long(volatile u_long *addr);
571 #else /* !KLD_MODULE */
572 static __inline u_int
573 atomic_readandclear_int(volatile u_int *addr)
574 {
575         u_int result;
576
577         __asm __volatile (
578         "       xorl    %0,%0 ;         "
579         "       xchgl   %1,%0 ;         "
580         "# atomic_readandclear_int"
581         : "=&r" (result)                /* 0 (result) */
582         : "m" (*addr));                 /* 1 (addr) */
583
584         return (result);
585 }
586
587 static __inline u_long
588 atomic_readandclear_long(volatile u_long *addr)
589 {
590         u_long result;
591
592         __asm __volatile (
593         "       xorq    %0,%0 ;         "
594         "       xchgq   %1,%0 ;         "
595         "# atomic_readandclear_int"
596         : "=&r" (result)                /* 0 (result) */
597         : "m" (*addr));                 /* 1 (addr) */
598
599         return (result);
600 }
601 #endif /* KLD_MODULE */
602
603 #else /* !defined(__GNUC__) */
604
605 extern u_long   atomic_readandclear_long(volatile u_long *);
606 extern u_int    atomic_readandclear_int(volatile u_int *);
607
608 #endif /* defined(__GNUC__) */
609
610 #endif /* ! _CPU_ATOMIC_H_ */