atomic.h revision 71141
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 71141 2001-01-17 02:15:11Z 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#define ATOMIC_STORE_LOAD(TYPE, LOP, SOP) \ 73u_##TYPE atomic_load_acq_##TYPE(volatile u_##TYPE *p); \ 74void atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v); 75 76#else /* !KLD_MODULE */ 77#if defined(SMP) 78#if defined(LOCORE) 79#define MPLOCKED lock ; 80#else 81#define MPLOCKED "lock ; " 82#endif 83#else 84#define MPLOCKED 85#endif 86 87/* 88 * The assembly is volatilized to demark potential before-and-after side 89 * effects if an interrupt or SMP collision were to occur. 90 */ 91#define ATOMIC_ASM(NAME, TYPE, OP, V) \ 92static __inline void \ 93atomic_##NAME##_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\ 94{ \ 95 __asm __volatile(MPLOCKED OP \ 96 : "=m" (*p) \ 97 : "0" (*p), "ir" (V)); \ 98} 99 100/* 101 * Atomic compare and set, used by the mutex functions 102 * 103 * if (*dst == exp) *dst = src (all 32 bit words) 104 * 105 * Returns 0 on failure, non-zero on success 106 */ 107 108#if defined(I386_CPU) 109static __inline int 110atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src) 111{ 112 int res = exp; 113 114 __asm __volatile( 115 " pushfl ; " 116 " cli ; " 117 " cmpl %1,%3 ; " 118 " jne 1f ; " 119 " movl %2,%3 ; " 120 "1: " 121 " sete %%al; " 122 " movzbl %%al,%0 ; " 123 " popfl ; " 124 "# atomic_cmpset_int" 125 : "=a" (res) /* 0 (result) */ 126 : "0" (exp), /* 1 */ 127 "r" (src), /* 2 */ 128 "m" (*(dst)) /* 3 */ 129 : "memory"); 130 131 return (res); 132} 133#else /* defined(I386_CPU) */ 134static __inline int 135atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src) 136{ 137 int res = exp; 138 139 __asm __volatile ( 140 " " MPLOCKED " " 141 " cmpxchgl %2,%3 ; " 142 " setz %%al ; " 143 " movzbl %%al,%0 ; " 144 "1: " 145 "# atomic_cmpset_int" 146 : "=a" (res) /* 0 (result) */ 147 : "0" (exp), /* 1 */ 148 "r" (src), /* 2 */ 149 "m" (*(dst)) /* 3 */ 150 : "memory"); 151 152 return (res); 153} 154#endif /* defined(I386_CPU) */ 155 156#if defined(I386_CPU) 157/* 158 * We assume that a = b will do atomic loads and stores. 159 * 160 * XXX: This is _NOT_ safe on a P6 or higher because it does not guarantee 161 * memory ordering. These should only be used on a 386. 162 */ 163#define ATOMIC_STORE_LOAD(TYPE, LOP, SOP) \ 164static __inline u_##TYPE \ 165atomic_load_acq_##TYPE(volatile u_##TYPE *p) \ 166{ \ 167 return (*p); \ 168} \ 169 \ 170static __inline void \ 171atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\ 172{ \ 173 *p = v; \ 174 __asm __volatile("" : : : "memory"); \ 175} 176#else 177 178#define ATOMIC_STORE_LOAD(TYPE, LOP, SOP) \ 179static __inline u_##TYPE \ 180atomic_load_acq_##TYPE(volatile u_##TYPE *p) \ 181{ \ 182 u_##TYPE res; \ 183 \ 184 __asm __volatile(MPLOCKED LOP \ 185 : "=a" (res), /* 0 (result) */\ 186 "+m" (*p) /* 1 */ \ 187 : : "cc", "memory"); \ 188 \ 189 return (res); \ 190} \ 191 \ 192/* \ 193 * The XCHG instruction asserts LOCK automagically. \ 194 */ \ 195static __inline void \ 196atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\ 197{ \ 198 __asm __volatile(SOP \ 199 : "+m" (*p), /* 0 */ \ 200 "+r" (v) /* 1 */ \ 201 : : "memory"); \ 202} 203#endif /* defined(I386_CPU) */ 204#endif /* KLD_MODULE */ 205 206ATOMIC_ASM(set, char, "orb %b2,%0", v) 207ATOMIC_ASM(clear, char, "andb %b2,%0", ~v) 208ATOMIC_ASM(add, char, "addb %b2,%0", v) 209ATOMIC_ASM(subtract, char, "subb %b2,%0", v) 210 211ATOMIC_ASM(set, short, "orw %w2,%0", v) 212ATOMIC_ASM(clear, short, "andw %w2,%0", ~v) 213ATOMIC_ASM(add, short, "addw %w2,%0", v) 214ATOMIC_ASM(subtract, short, "subw %w2,%0", v) 215 216ATOMIC_ASM(set, int, "orl %2,%0", v) 217ATOMIC_ASM(clear, int, "andl %2,%0", ~v) 218ATOMIC_ASM(add, int, "addl %2,%0", v) 219ATOMIC_ASM(subtract, int, "subl %2,%0", v) 220 221ATOMIC_ASM(set, long, "orl %2,%0", v) 222ATOMIC_ASM(clear, long, "andl %2,%0", ~v) 223ATOMIC_ASM(add, long, "addl %2,%0", v) 224ATOMIC_ASM(subtract, long, "subl %2,%0", v) 225 226ATOMIC_STORE_LOAD(char, "cmpxchgb %b0,%1", "xchgb %b1,%0") 227ATOMIC_STORE_LOAD(short,"cmpxchgw %w0,%1", "xchgw %w1,%0") 228ATOMIC_STORE_LOAD(int, "cmpxchgl %0,%1", "xchgl %1,%0") 229ATOMIC_STORE_LOAD(long, "cmpxchgl %0,%1", "xchgl %1,%0") 230 231#undef ATOMIC_ASM 232#undef ATOMIC_STORE_LOAD 233 234#define atomic_set_acq_char atomic_set_char 235#define atomic_set_rel_char atomic_set_char 236#define atomic_clear_acq_char atomic_clear_char 237#define atomic_clear_rel_char atomic_clear_char 238#define atomic_add_acq_char atomic_add_char 239#define atomic_add_rel_char atomic_add_char 240#define atomic_subtract_acq_char atomic_subtract_char 241#define atomic_subtract_rel_char atomic_subtract_char 242 243#define atomic_set_acq_short atomic_set_short 244#define atomic_set_rel_short atomic_set_short 245#define atomic_clear_acq_short atomic_clear_short 246#define atomic_clear_rel_short atomic_clear_short 247#define atomic_add_acq_short atomic_add_short 248#define atomic_add_rel_short atomic_add_short 249#define atomic_subtract_acq_short atomic_subtract_short 250#define atomic_subtract_rel_short atomic_subtract_short 251 252#define atomic_set_acq_int atomic_set_int 253#define atomic_set_rel_int atomic_set_int 254#define atomic_clear_acq_int atomic_clear_int 255#define atomic_clear_rel_int atomic_clear_int 256#define atomic_add_acq_int atomic_add_int 257#define atomic_add_rel_int atomic_add_int 258#define atomic_subtract_acq_int atomic_subtract_int 259#define atomic_subtract_rel_int atomic_subtract_int 260#define atomic_cmpset_acq_int atomic_cmpset_int 261#define atomic_cmpset_rel_int atomic_cmpset_int 262 263#define atomic_set_acq_long atomic_set_long 264#define atomic_set_rel_long atomic_set_long 265#define atomic_clear_acq_long atomic_clear_long 266#define atomic_clear_rel_long atomic_clear_long 267#define atomic_add_acq_long atomic_add_long 268#define atomic_add_rel_long atomic_add_long 269#define atomic_subtract_acq_long atomic_subtract_long 270#define atomic_subtract_rel_long atomic_subtract_long 271#define atomic_cmpset_long atomic_cmpset_int 272#define atomic_cmpset_acq_long atomic_cmpset_acq_int 273#define atomic_cmpset_rel_long atomic_cmpset_rel_int 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) 328static __inline int 329atomic_cmpset_ptr(volatile void *dst, void *exp, void *src) 330{ 331 332 return (atomic_cmpset_int((volatile u_int *)dst, (u_int)exp, 333 (u_int)src)); 334} 335 336static __inline void * 337atomic_load_acq_ptr(volatile void *p) 338{ 339 return (void *)atomic_load_acq_int((volatile u_int *)p); 340} 341 342static __inline void 343atomic_store_rel_ptr(volatile void *p, void *v) 344{ 345 atomic_store_rel_int((volatile u_int *)p, (u_int)v); 346} 347 348#define ATOMIC_PTR(NAME) \ 349static __inline void \ 350atomic_##NAME##_ptr(volatile void *p, uintptr_t v) \ 351{ \ 352 atomic_##NAME##_int((volatile u_int *)p, v); \ 353} \ 354 \ 355static __inline void \ 356atomic_##NAME##_acq_ptr(volatile void *p, uintptr_t v) \ 357{ \ 358 atomic_##NAME##_acq_int((volatile u_int *)p, v);\ 359} \ 360 \ 361static __inline void \ 362atomic_##NAME##_rel_ptr(volatile void *p, uintptr_t v) \ 363{ \ 364 atomic_##NAME##_rel_int((volatile u_int *)p, v);\ 365} 366 367ATOMIC_PTR(set) 368ATOMIC_PTR(clear) 369ATOMIC_PTR(add) 370ATOMIC_PTR(subtract) 371 372#undef ATOMIC_PTR 373 374static __inline u_int 375atomic_readandclear_int(volatile u_int *addr) 376{ 377 u_int result; 378 379 __asm __volatile ( 380 " xorl %0,%0 ; " 381 " xchgl %1,%0 ; " 382 "# atomic_readandclear_int" 383 : "=&r" (result) /* 0 (result) */ 384 : "m" (*addr)); /* 1 (addr) */ 385 386 return (result); 387} 388 389static __inline u_long 390atomic_readandclear_long(volatile u_long *addr) 391{ 392 u_long result; 393 394 __asm __volatile ( 395 " xorl %0,%0 ; " 396 " xchgl %1,%0 ; " 397 "# atomic_readandclear_int" 398 : "=&r" (result) /* 0 (result) */ 399 : "m" (*addr)); /* 1 (addr) */ 400 401 return (result); 402} 403#endif /* !defined(WANT_FUNCTIONS) */ 404#endif /* ! _MACHINE_ATOMIC_H_ */ 405