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