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