]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/include/atomic.h
Merge from head
[FreeBSD/FreeBSD.git] / sys / i386 / include / atomic.h
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 #ifdef _KERNEL
36 #include <machine/md_var.h>
37 #include <machine/specialreg.h>
38 #endif
39
40 #define mb()    __asm __volatile("lock; addl $0,(%%esp)" : : : "memory", "cc")
41 #define wmb()   __asm __volatile("lock; addl $0,(%%esp)" : : : "memory", "cc")
42 #define rmb()   __asm __volatile("lock; addl $0,(%%esp)" : : : "memory", "cc")
43
44 /*
45  * Various simple operations on memory, each of which is atomic in the
46  * presence of interrupts and multiple processors.
47  *
48  * atomic_set_char(P, V)        (*(u_char *)(P) |= (V))
49  * atomic_clear_char(P, V)      (*(u_char *)(P) &= ~(V))
50  * atomic_add_char(P, V)        (*(u_char *)(P) += (V))
51  * atomic_subtract_char(P, V)   (*(u_char *)(P) -= (V))
52  *
53  * atomic_set_short(P, V)       (*(u_short *)(P) |= (V))
54  * atomic_clear_short(P, V)     (*(u_short *)(P) &= ~(V))
55  * atomic_add_short(P, V)       (*(u_short *)(P) += (V))
56  * atomic_subtract_short(P, V)  (*(u_short *)(P) -= (V))
57  *
58  * atomic_set_int(P, V)         (*(u_int *)(P) |= (V))
59  * atomic_clear_int(P, V)       (*(u_int *)(P) &= ~(V))
60  * atomic_add_int(P, V)         (*(u_int *)(P) += (V))
61  * atomic_subtract_int(P, V)    (*(u_int *)(P) -= (V))
62  * atomic_swap_int(P, V)        (return (*(u_int *)(P)); *(u_int *)(P) = (V);)
63  * atomic_readandclear_int(P)   (return (*(u_int *)(P)); *(u_int *)(P) = 0;)
64  *
65  * atomic_set_long(P, V)        (*(u_long *)(P) |= (V))
66  * atomic_clear_long(P, V)      (*(u_long *)(P) &= ~(V))
67  * atomic_add_long(P, V)        (*(u_long *)(P) += (V))
68  * atomic_subtract_long(P, V)   (*(u_long *)(P) -= (V))
69  * atomic_swap_long(P, V)       (return (*(u_long *)(P)); *(u_long *)(P) = (V);)
70  * atomic_readandclear_long(P)  (return (*(u_long *)(P)); *(u_long *)(P) = 0;)
71  */
72
73 /*
74  * The above functions are expanded inline in the statically-linked
75  * kernel.  Lock prefixes are generated if an SMP kernel is being
76  * built.
77  *
78  * Kernel modules call real functions which are built into the kernel.
79  * This allows kernel modules to be portable between UP and SMP systems.
80  */
81 #if defined(KLD_MODULE) || !defined(__GNUCLIKE_ASM)
82 #define ATOMIC_ASM(NAME, TYPE, OP, CONS, V)                     \
83 void atomic_##NAME##_##TYPE(volatile u_##TYPE *p, u_##TYPE v);  \
84 void atomic_##NAME##_barr_##TYPE(volatile u_##TYPE *p, u_##TYPE v)
85
86 int     atomic_cmpset_int(volatile u_int *dst, u_int expect, u_int src);
87 u_int   atomic_fetchadd_int(volatile u_int *p, u_int v);
88 int     atomic_testandset_int(volatile u_int *p, u_int v);
89 void    atomic_thread_fence_acq(void);
90 void    atomic_thread_fence_acq_rel(void);
91 void    atomic_thread_fence_rel(void);
92 void    atomic_thread_fence_seq_cst(void);
93
94 #define ATOMIC_LOAD(TYPE)                                       \
95 u_##TYPE        atomic_load_acq_##TYPE(volatile u_##TYPE *p)
96 #define ATOMIC_STORE(TYPE)                                      \
97 void            atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)
98
99 int             atomic_cmpset_64(volatile uint64_t *, uint64_t, uint64_t);
100 uint64_t        atomic_load_acq_64(volatile uint64_t *);
101 void            atomic_store_rel_64(volatile uint64_t *, uint64_t);
102 uint64_t        atomic_swap_64(volatile uint64_t *, uint64_t);
103
104 #else /* !KLD_MODULE && __GNUCLIKE_ASM */
105
106 /*
107  * For userland, always use lock prefixes so that the binaries will run
108  * on both SMP and !SMP systems.
109  */
110 #if defined(SMP) || !defined(_KERNEL)
111 #define MPLOCKED        "lock ; "
112 #else
113 #define MPLOCKED
114 #endif
115
116 /*
117  * The assembly is volatilized to avoid code chunk removal by the compiler.
118  * GCC aggressively reorders operations and memory clobbering is necessary
119  * in order to avoid that for memory barriers.
120  */
121 #define ATOMIC_ASM(NAME, TYPE, OP, CONS, V)             \
122 static __inline void                                    \
123 atomic_##NAME##_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\
124 {                                                       \
125         __asm __volatile(MPLOCKED OP                    \
126         : "+m" (*p)                                     \
127         : CONS (V)                                      \
128         : "cc");                                        \
129 }                                                       \
130                                                         \
131 static __inline void                                    \
132 atomic_##NAME##_barr_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\
133 {                                                       \
134         __asm __volatile(MPLOCKED OP                    \
135         : "+m" (*p)                                     \
136         : CONS (V)                                      \
137         : "memory", "cc");                              \
138 }                                                       \
139 struct __hack
140
141 /*
142  * Atomic compare and set, used by the mutex functions
143  *
144  * if (*dst == expect) *dst = src (all 32 bit words)
145  *
146  * Returns 0 on failure, non-zero on success
147  */
148
149 #ifdef CPU_DISABLE_CMPXCHG
150
151 static __inline int
152 atomic_cmpset_int(volatile u_int *dst, u_int expect, u_int src)
153 {
154         u_char res;
155
156         __asm __volatile(
157         "       pushfl ;                "
158         "       cli ;                   "
159         "       cmpl    %3,%1 ;         "
160         "       jne     1f ;            "
161         "       movl    %2,%1 ;         "
162         "1:                             "
163         "       sete    %0 ;            "
164         "       popfl ;                 "
165         "# atomic_cmpset_int"
166         : "=q" (res),                   /* 0 */
167           "+m" (*dst)                   /* 1 */
168         : "r" (src),                    /* 2 */
169           "r" (expect)                  /* 3 */
170         : "memory");
171         return (res);
172 }
173
174 #else /* !CPU_DISABLE_CMPXCHG */
175
176 static __inline int
177 atomic_cmpset_int(volatile u_int *dst, u_int expect, u_int src)
178 {
179         u_char res;
180
181         __asm __volatile(
182         "       " MPLOCKED "            "
183         "       cmpxchgl %3,%1 ;        "
184         "       sete    %0 ;            "
185         "# atomic_cmpset_int"
186         : "=q" (res),                   /* 0 */
187           "+m" (*dst),                  /* 1 */
188           "+a" (expect)                 /* 2 */
189         : "r" (src)                     /* 3 */
190         : "memory", "cc");
191         return (res);
192 }
193
194 #endif /* CPU_DISABLE_CMPXCHG */
195
196 /*
197  * Atomically add the value of v to the integer pointed to by p and return
198  * the previous value of *p.
199  */
200 static __inline u_int
201 atomic_fetchadd_int(volatile u_int *p, u_int v)
202 {
203
204         __asm __volatile(
205         "       " MPLOCKED "            "
206         "       xaddl   %0,%1 ;         "
207         "# atomic_fetchadd_int"
208         : "+r" (v),                     /* 0 */
209           "+m" (*p)                     /* 1 */
210         : : "cc");
211         return (v);
212 }
213
214 static __inline int
215 atomic_testandset_int(volatile u_int *p, u_int v)
216 {
217         u_char res;
218
219         __asm __volatile(
220         "       " MPLOCKED "            "
221         "       btsl    %2,%1 ;         "
222         "       setc    %0 ;            "
223         "# atomic_testandset_int"
224         : "=q" (res),                   /* 0 */
225           "+m" (*p)                     /* 1 */
226         : "Ir" (v & 0x1f)               /* 2 */
227         : "cc");
228         return (res);
229 }
230
231 /*
232  * We assume that a = b will do atomic loads and stores.  Due to the
233  * IA32 memory model, a simple store guarantees release semantics.
234  *
235  * However, a load may pass a store if they are performed on distinct
236  * addresses, so for atomic_load_acq we introduce a Store/Load barrier
237  * before the load in SMP kernels.  We use "lock addl $0,mem", as
238  * recommended by the AMD Software Optimization Guide, and not mfence.
239  * In the kernel, we use a private per-cpu cache line as the target
240  * for the locked addition, to avoid introducing false data
241  * dependencies.  In userspace, a word at the top of the stack is
242  * utilized.
243  *
244  * For UP kernels, however, the memory of the single processor is
245  * always consistent, so we only need to stop the compiler from
246  * reordering accesses in a way that violates the semantics of acquire
247  * and release.
248  */
249 #if defined(_KERNEL)
250
251 /*
252  * OFFSETOF_MONITORBUF == __pcpu_offset(pc_monitorbuf).
253  *
254  * The open-coded number is used instead of the symbolic expression to
255  * avoid a dependency on sys/pcpu.h in machine/atomic.h consumers.
256  * An assertion in i386/vm_machdep.c ensures that the value is correct.
257  */
258 #define OFFSETOF_MONITORBUF     0x180
259
260 #if defined(SMP)
261 static __inline void
262 __storeload_barrier(void)
263 {
264
265         __asm __volatile("lock; addl $0,%%fs:%0"
266             : "+m" (*(u_int *)OFFSETOF_MONITORBUF) : : "memory", "cc");
267 }
268 #else /* _KERNEL && UP */
269 static __inline void
270 __storeload_barrier(void)
271 {
272
273         __compiler_membar();
274 }
275 #endif /* SMP */
276 #else /* !_KERNEL */
277 static __inline void
278 __storeload_barrier(void)
279 {
280
281         __asm __volatile("lock; addl $0,(%%esp)" : : : "memory", "cc");
282 }
283 #endif /* _KERNEL*/
284
285 /*
286  * C11-standard acq/rel semantics only apply when the variable in the
287  * call is the same for acq as it is for rel.  However, our previous
288  * (x86) implementations provided much stronger ordering than required
289  * (essentially what is called seq_cst order in C11).  This
290  * implementation provides the historical strong ordering since some
291  * callers depend on it.
292  */
293
294 #define ATOMIC_LOAD(TYPE)                                       \
295 static __inline u_##TYPE                                        \
296 atomic_load_acq_##TYPE(volatile u_##TYPE *p)                    \
297 {                                                               \
298         u_##TYPE res;                                           \
299                                                                 \
300         __storeload_barrier();                                  \
301         res = *p;                                               \
302         __compiler_membar();                                    \
303         return (res);                                           \
304 }                                                               \
305 struct __hack
306
307 #define ATOMIC_STORE(TYPE)                                      \
308 static __inline void                                            \
309 atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)       \
310 {                                                               \
311                                                                 \
312         __compiler_membar();                                    \
313         *p = v;                                                 \
314 }                                                               \
315 struct __hack
316
317 static __inline void
318 atomic_thread_fence_acq(void)
319 {
320
321         __compiler_membar();
322 }
323
324 static __inline void
325 atomic_thread_fence_rel(void)
326 {
327
328         __compiler_membar();
329 }
330
331 static __inline void
332 atomic_thread_fence_acq_rel(void)
333 {
334
335         __compiler_membar();
336 }
337
338 static __inline void
339 atomic_thread_fence_seq_cst(void)
340 {
341
342         __storeload_barrier();
343 }
344
345 #ifdef _KERNEL
346
347 #ifdef WANT_FUNCTIONS
348 int             atomic_cmpset_64_i386(volatile uint64_t *, uint64_t, uint64_t);
349 int             atomic_cmpset_64_i586(volatile uint64_t *, uint64_t, uint64_t);
350 uint64_t        atomic_load_acq_64_i386(volatile uint64_t *);
351 uint64_t        atomic_load_acq_64_i586(volatile uint64_t *);
352 void            atomic_store_rel_64_i386(volatile uint64_t *, uint64_t);
353 void            atomic_store_rel_64_i586(volatile uint64_t *, uint64_t);
354 uint64_t        atomic_swap_64_i386(volatile uint64_t *, uint64_t);
355 uint64_t        atomic_swap_64_i586(volatile uint64_t *, uint64_t);
356 #endif
357
358 /* I486 does not support SMP or CMPXCHG8B. */
359 static __inline int
360 atomic_cmpset_64_i386(volatile uint64_t *dst, uint64_t expect, uint64_t src)
361 {
362         volatile uint32_t *p;
363         u_char res;
364
365         p = (volatile uint32_t *)dst;
366         __asm __volatile(
367         "       pushfl ;                "
368         "       cli ;                   "
369         "       xorl    %1,%%eax ;      "
370         "       xorl    %2,%%edx ;      "
371         "       orl     %%edx,%%eax ;   "
372         "       jne     1f ;            "
373         "       movl    %4,%1 ;         "
374         "       movl    %5,%2 ;         "
375         "1:                             "
376         "       sete    %3 ;            "
377         "       popfl"
378         : "+A" (expect),                /* 0 */
379           "+m" (*p),                    /* 1 */
380           "+m" (*(p + 1)),              /* 2 */
381           "=q" (res)                    /* 3 */
382         : "r" ((uint32_t)src),          /* 4 */
383           "r" ((uint32_t)(src >> 32))   /* 5 */
384         : "memory", "cc");
385         return (res);
386 }
387
388 static __inline uint64_t
389 atomic_load_acq_64_i386(volatile uint64_t *p)
390 {
391         volatile uint32_t *q;
392         uint64_t res;
393
394         q = (volatile uint32_t *)p;
395         __asm __volatile(
396         "       pushfl ;                "
397         "       cli ;                   "
398         "       movl    %1,%%eax ;      "
399         "       movl    %2,%%edx ;      "
400         "       popfl"
401         : "=&A" (res)                   /* 0 */
402         : "m" (*q),                     /* 1 */
403           "m" (*(q + 1))                /* 2 */
404         : "memory");
405         return (res);
406 }
407
408 static __inline void
409 atomic_store_rel_64_i386(volatile uint64_t *p, uint64_t v)
410 {
411         volatile uint32_t *q;
412
413         q = (volatile uint32_t *)p;
414         __asm __volatile(
415         "       pushfl ;                "
416         "       cli ;                   "
417         "       movl    %%eax,%0 ;      "
418         "       movl    %%edx,%1 ;      "
419         "       popfl"
420         : "=m" (*q),                    /* 0 */
421           "=m" (*(q + 1))               /* 1 */
422         : "A" (v)                       /* 2 */
423         : "memory");
424 }
425
426 static __inline uint64_t
427 atomic_swap_64_i386(volatile uint64_t *p, uint64_t v)
428 {
429         volatile uint32_t *q;
430         uint64_t res;
431
432         q = (volatile uint32_t *)p;
433         __asm __volatile(
434         "       pushfl ;                "
435         "       cli ;                   "
436         "       movl    %1,%%eax ;      "
437         "       movl    %2,%%edx ;      "
438         "       movl    %4,%2 ;         "
439         "       movl    %3,%1 ;         "
440         "       popfl"
441         : "=&A" (res),                  /* 0 */
442           "+m" (*q),                    /* 1 */
443           "+m" (*(q + 1))               /* 2 */
444         : "r" ((uint32_t)v),            /* 3 */
445           "r" ((uint32_t)(v >> 32)));   /* 4 */
446         return (res);
447 }
448
449 static __inline int
450 atomic_cmpset_64_i586(volatile uint64_t *dst, uint64_t expect, uint64_t src)
451 {
452         u_char res;
453
454         __asm __volatile(
455         "       " MPLOCKED "            "
456         "       cmpxchg8b %1 ;          "
457         "       sete    %0"
458         : "=q" (res),                   /* 0 */
459           "+m" (*dst),                  /* 1 */
460           "+A" (expect)                 /* 2 */
461         : "b" ((uint32_t)src),          /* 3 */
462           "c" ((uint32_t)(src >> 32))   /* 4 */
463         : "memory", "cc");
464         return (res);
465 }
466
467 static __inline uint64_t
468 atomic_load_acq_64_i586(volatile uint64_t *p)
469 {
470         uint64_t res;
471
472         __asm __volatile(
473         "       movl    %%ebx,%%eax ;   "
474         "       movl    %%ecx,%%edx ;   "
475         "       " MPLOCKED "            "
476         "       cmpxchg8b %1"
477         : "=&A" (res),                  /* 0 */
478           "+m" (*p)                     /* 1 */
479         : : "memory", "cc");
480         return (res);
481 }
482
483 static __inline void
484 atomic_store_rel_64_i586(volatile uint64_t *p, uint64_t v)
485 {
486
487         __asm __volatile(
488         "       movl    %%eax,%%ebx ;   "
489         "       movl    %%edx,%%ecx ;   "
490         "1:                             "
491         "       " MPLOCKED "            "
492         "       cmpxchg8b %0 ;          "
493         "       jne     1b"
494         : "+m" (*p),                    /* 0 */
495           "+A" (v)                      /* 1 */
496         : : "ebx", "ecx", "memory", "cc");
497 }
498
499 static __inline uint64_t
500 atomic_swap_64_i586(volatile uint64_t *p, uint64_t v)
501 {
502
503         __asm __volatile(
504         "       movl    %%eax,%%ebx ;   "
505         "       movl    %%edx,%%ecx ;   "
506         "1:                             "
507         "       " MPLOCKED "            "
508         "       cmpxchg8b %0 ;          "
509         "       jne     1b"
510         : "+m" (*p),                    /* 0 */
511           "+A" (v)                      /* 1 */
512         : : "ebx", "ecx", "memory", "cc");
513         return (v);
514 }
515
516 static __inline int
517 atomic_cmpset_64(volatile uint64_t *dst, uint64_t expect, uint64_t src)
518 {
519
520         if ((cpu_feature & CPUID_CX8) == 0)
521                 return (atomic_cmpset_64_i386(dst, expect, src));
522         else
523                 return (atomic_cmpset_64_i586(dst, expect, src));
524 }
525
526 static __inline uint64_t
527 atomic_load_acq_64(volatile uint64_t *p)
528 {
529
530         if ((cpu_feature & CPUID_CX8) == 0)
531                 return (atomic_load_acq_64_i386(p));
532         else
533                 return (atomic_load_acq_64_i586(p));
534 }
535
536 static __inline void
537 atomic_store_rel_64(volatile uint64_t *p, uint64_t v)
538 {
539
540         if ((cpu_feature & CPUID_CX8) == 0)
541                 atomic_store_rel_64_i386(p, v);
542         else
543                 atomic_store_rel_64_i586(p, v);
544 }
545
546 static __inline uint64_t
547 atomic_swap_64(volatile uint64_t *p, uint64_t v)
548 {
549
550         if ((cpu_feature & CPUID_CX8) == 0)
551                 return (atomic_swap_64_i386(p, v));
552         else
553                 return (atomic_swap_64_i586(p, v));
554 }
555
556 #endif /* _KERNEL */
557
558 #endif /* KLD_MODULE || !__GNUCLIKE_ASM */
559
560 ATOMIC_ASM(set,      char,  "orb %b1,%0",  "iq",  v);
561 ATOMIC_ASM(clear,    char,  "andb %b1,%0", "iq", ~v);
562 ATOMIC_ASM(add,      char,  "addb %b1,%0", "iq",  v);
563 ATOMIC_ASM(subtract, char,  "subb %b1,%0", "iq",  v);
564
565 ATOMIC_ASM(set,      short, "orw %w1,%0",  "ir",  v);
566 ATOMIC_ASM(clear,    short, "andw %w1,%0", "ir", ~v);
567 ATOMIC_ASM(add,      short, "addw %w1,%0", "ir",  v);
568 ATOMIC_ASM(subtract, short, "subw %w1,%0", "ir",  v);
569
570 ATOMIC_ASM(set,      int,   "orl %1,%0",   "ir",  v);
571 ATOMIC_ASM(clear,    int,   "andl %1,%0",  "ir", ~v);
572 ATOMIC_ASM(add,      int,   "addl %1,%0",  "ir",  v);
573 ATOMIC_ASM(subtract, int,   "subl %1,%0",  "ir",  v);
574
575 ATOMIC_ASM(set,      long,  "orl %1,%0",   "ir",  v);
576 ATOMIC_ASM(clear,    long,  "andl %1,%0",  "ir", ~v);
577 ATOMIC_ASM(add,      long,  "addl %1,%0",  "ir",  v);
578 ATOMIC_ASM(subtract, long,  "subl %1,%0",  "ir",  v);
579
580 #define ATOMIC_LOADSTORE(TYPE)                          \
581         ATOMIC_LOAD(TYPE);                              \
582         ATOMIC_STORE(TYPE)
583
584 ATOMIC_LOADSTORE(char);
585 ATOMIC_LOADSTORE(short);
586 ATOMIC_LOADSTORE(int);
587 ATOMIC_LOADSTORE(long);
588
589 #undef ATOMIC_ASM
590 #undef ATOMIC_LOAD
591 #undef ATOMIC_STORE
592 #undef ATOMIC_LOADSTORE
593
594 #ifndef WANT_FUNCTIONS
595
596 static __inline int
597 atomic_cmpset_long(volatile u_long *dst, u_long expect, u_long src)
598 {
599
600         return (atomic_cmpset_int((volatile u_int *)dst, (u_int)expect,
601             (u_int)src));
602 }
603
604 static __inline u_long
605 atomic_fetchadd_long(volatile u_long *p, u_long v)
606 {
607
608         return (atomic_fetchadd_int((volatile u_int *)p, (u_int)v));
609 }
610
611 static __inline int
612 atomic_testandset_long(volatile u_long *p, u_int v)
613 {
614
615         return (atomic_testandset_int((volatile u_int *)p, v));
616 }
617
618 /* Read the current value and store a new value in the destination. */
619 #ifdef __GNUCLIKE_ASM
620
621 static __inline u_int
622 atomic_swap_int(volatile u_int *p, u_int v)
623 {
624
625         __asm __volatile(
626         "       xchgl   %1,%0 ;         "
627         "# atomic_swap_int"
628         : "+r" (v),                     /* 0 */
629           "+m" (*p));                   /* 1 */
630         return (v);
631 }
632
633 static __inline u_long
634 atomic_swap_long(volatile u_long *p, u_long v)
635 {
636
637         return (atomic_swap_int((volatile u_int *)p, (u_int)v));
638 }
639
640 #else /* !__GNUCLIKE_ASM */
641
642 u_int   atomic_swap_int(volatile u_int *p, u_int v);
643 u_long  atomic_swap_long(volatile u_long *p, u_long v);
644
645 #endif /* __GNUCLIKE_ASM */
646
647 #define atomic_set_acq_char             atomic_set_barr_char
648 #define atomic_set_rel_char             atomic_set_barr_char
649 #define atomic_clear_acq_char           atomic_clear_barr_char
650 #define atomic_clear_rel_char           atomic_clear_barr_char
651 #define atomic_add_acq_char             atomic_add_barr_char
652 #define atomic_add_rel_char             atomic_add_barr_char
653 #define atomic_subtract_acq_char        atomic_subtract_barr_char
654 #define atomic_subtract_rel_char        atomic_subtract_barr_char
655
656 #define atomic_set_acq_short            atomic_set_barr_short
657 #define atomic_set_rel_short            atomic_set_barr_short
658 #define atomic_clear_acq_short          atomic_clear_barr_short
659 #define atomic_clear_rel_short          atomic_clear_barr_short
660 #define atomic_add_acq_short            atomic_add_barr_short
661 #define atomic_add_rel_short            atomic_add_barr_short
662 #define atomic_subtract_acq_short       atomic_subtract_barr_short
663 #define atomic_subtract_rel_short       atomic_subtract_barr_short
664
665 #define atomic_set_acq_int              atomic_set_barr_int
666 #define atomic_set_rel_int              atomic_set_barr_int
667 #define atomic_clear_acq_int            atomic_clear_barr_int
668 #define atomic_clear_rel_int            atomic_clear_barr_int
669 #define atomic_add_acq_int              atomic_add_barr_int
670 #define atomic_add_rel_int              atomic_add_barr_int
671 #define atomic_subtract_acq_int         atomic_subtract_barr_int
672 #define atomic_subtract_rel_int         atomic_subtract_barr_int
673 #define atomic_cmpset_acq_int           atomic_cmpset_int
674 #define atomic_cmpset_rel_int           atomic_cmpset_int
675
676 #define atomic_set_acq_long             atomic_set_barr_long
677 #define atomic_set_rel_long             atomic_set_barr_long
678 #define atomic_clear_acq_long           atomic_clear_barr_long
679 #define atomic_clear_rel_long           atomic_clear_barr_long
680 #define atomic_add_acq_long             atomic_add_barr_long
681 #define atomic_add_rel_long             atomic_add_barr_long
682 #define atomic_subtract_acq_long        atomic_subtract_barr_long
683 #define atomic_subtract_rel_long        atomic_subtract_barr_long
684 #define atomic_cmpset_acq_long          atomic_cmpset_long
685 #define atomic_cmpset_rel_long          atomic_cmpset_long
686
687 #define atomic_readandclear_int(p)      atomic_swap_int(p, 0)
688 #define atomic_readandclear_long(p)     atomic_swap_long(p, 0)
689
690 /* Operations on 8-bit bytes. */
691 #define atomic_set_8            atomic_set_char
692 #define atomic_set_acq_8        atomic_set_acq_char
693 #define atomic_set_rel_8        atomic_set_rel_char
694 #define atomic_clear_8          atomic_clear_char
695 #define atomic_clear_acq_8      atomic_clear_acq_char
696 #define atomic_clear_rel_8      atomic_clear_rel_char
697 #define atomic_add_8            atomic_add_char
698 #define atomic_add_acq_8        atomic_add_acq_char
699 #define atomic_add_rel_8        atomic_add_rel_char
700 #define atomic_subtract_8       atomic_subtract_char
701 #define atomic_subtract_acq_8   atomic_subtract_acq_char
702 #define atomic_subtract_rel_8   atomic_subtract_rel_char
703 #define atomic_load_acq_8       atomic_load_acq_char
704 #define atomic_store_rel_8      atomic_store_rel_char
705
706 /* Operations on 16-bit words. */
707 #define atomic_set_16           atomic_set_short
708 #define atomic_set_acq_16       atomic_set_acq_short
709 #define atomic_set_rel_16       atomic_set_rel_short
710 #define atomic_clear_16         atomic_clear_short
711 #define atomic_clear_acq_16     atomic_clear_acq_short
712 #define atomic_clear_rel_16     atomic_clear_rel_short
713 #define atomic_add_16           atomic_add_short
714 #define atomic_add_acq_16       atomic_add_acq_short
715 #define atomic_add_rel_16       atomic_add_rel_short
716 #define atomic_subtract_16      atomic_subtract_short
717 #define atomic_subtract_acq_16  atomic_subtract_acq_short
718 #define atomic_subtract_rel_16  atomic_subtract_rel_short
719 #define atomic_load_acq_16      atomic_load_acq_short
720 #define atomic_store_rel_16     atomic_store_rel_short
721
722 /* Operations on 32-bit double words. */
723 #define atomic_set_32           atomic_set_int
724 #define atomic_set_acq_32       atomic_set_acq_int
725 #define atomic_set_rel_32       atomic_set_rel_int
726 #define atomic_clear_32         atomic_clear_int
727 #define atomic_clear_acq_32     atomic_clear_acq_int
728 #define atomic_clear_rel_32     atomic_clear_rel_int
729 #define atomic_add_32           atomic_add_int
730 #define atomic_add_acq_32       atomic_add_acq_int
731 #define atomic_add_rel_32       atomic_add_rel_int
732 #define atomic_subtract_32      atomic_subtract_int
733 #define atomic_subtract_acq_32  atomic_subtract_acq_int
734 #define atomic_subtract_rel_32  atomic_subtract_rel_int
735 #define atomic_load_acq_32      atomic_load_acq_int
736 #define atomic_store_rel_32     atomic_store_rel_int
737 #define atomic_cmpset_32        atomic_cmpset_int
738 #define atomic_cmpset_acq_32    atomic_cmpset_acq_int
739 #define atomic_cmpset_rel_32    atomic_cmpset_rel_int
740 #define atomic_swap_32          atomic_swap_int
741 #define atomic_readandclear_32  atomic_readandclear_int
742 #define atomic_fetchadd_32      atomic_fetchadd_int
743 #define atomic_testandset_32    atomic_testandset_int
744
745 /* Operations on pointers. */
746 #define atomic_set_ptr(p, v) \
747         atomic_set_int((volatile u_int *)(p), (u_int)(v))
748 #define atomic_set_acq_ptr(p, v) \
749         atomic_set_acq_int((volatile u_int *)(p), (u_int)(v))
750 #define atomic_set_rel_ptr(p, v) \
751         atomic_set_rel_int((volatile u_int *)(p), (u_int)(v))
752 #define atomic_clear_ptr(p, v) \
753         atomic_clear_int((volatile u_int *)(p), (u_int)(v))
754 #define atomic_clear_acq_ptr(p, v) \
755         atomic_clear_acq_int((volatile u_int *)(p), (u_int)(v))
756 #define atomic_clear_rel_ptr(p, v) \
757         atomic_clear_rel_int((volatile u_int *)(p), (u_int)(v))
758 #define atomic_add_ptr(p, v) \
759         atomic_add_int((volatile u_int *)(p), (u_int)(v))
760 #define atomic_add_acq_ptr(p, v) \
761         atomic_add_acq_int((volatile u_int *)(p), (u_int)(v))
762 #define atomic_add_rel_ptr(p, v) \
763         atomic_add_rel_int((volatile u_int *)(p), (u_int)(v))
764 #define atomic_subtract_ptr(p, v) \
765         atomic_subtract_int((volatile u_int *)(p), (u_int)(v))
766 #define atomic_subtract_acq_ptr(p, v) \
767         atomic_subtract_acq_int((volatile u_int *)(p), (u_int)(v))
768 #define atomic_subtract_rel_ptr(p, v) \
769         atomic_subtract_rel_int((volatile u_int *)(p), (u_int)(v))
770 #define atomic_load_acq_ptr(p) \
771         atomic_load_acq_int((volatile u_int *)(p))
772 #define atomic_store_rel_ptr(p, v) \
773         atomic_store_rel_int((volatile u_int *)(p), (v))
774 #define atomic_cmpset_ptr(dst, old, new) \
775         atomic_cmpset_int((volatile u_int *)(dst), (u_int)(old), (u_int)(new))
776 #define atomic_cmpset_acq_ptr(dst, old, new) \
777         atomic_cmpset_acq_int((volatile u_int *)(dst), (u_int)(old), \
778             (u_int)(new))
779 #define atomic_cmpset_rel_ptr(dst, old, new) \
780         atomic_cmpset_rel_int((volatile u_int *)(dst), (u_int)(old), \
781             (u_int)(new))
782 #define atomic_swap_ptr(p, v) \
783         atomic_swap_int((volatile u_int *)(p), (u_int)(v))
784 #define atomic_readandclear_ptr(p) \
785         atomic_readandclear_int((volatile u_int *)(p))
786
787 #endif /* !WANT_FUNCTIONS */
788
789 #endif /* !_MACHINE_ATOMIC_H_ */