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 /* 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) \ 68 void atomic_##NAME##_##TYPE(volatile u_##TYPE *p, u_##TYPE v); 69 70 int 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) \ 90 static __inline void \ 91 atomic_##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) 107 static __inline int 108 atomic_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) */ 132 static __inline int 133 atomic_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) \ 163 static __inline void \ 164 atomic_##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 */ 178 ATOMIC_ASM(set, char, "orb %b2,%0", v) 179 ATOMIC_ASM(clear, char, "andb %b2,%0", ~v) 180 ATOMIC_ASM(add, char, "addb %b2,%0", v) 181 ATOMIC_ASM(subtract, char, "subb %b2,%0", v) 182 183 ATOMIC_ASM(set, short, "orw %w2,%0", v) 184 ATOMIC_ASM(clear, short, "andw %w2,%0", ~v) 185 ATOMIC_ASM(add, short, "addw %w2,%0", v) 186 ATOMIC_ASM(subtract, short, "subw %w2,%0", v) 187 188 ATOMIC_ASM(set, int, "orl %2,%0", v) 189 ATOMIC_ASM(clear, int, "andl %2,%0", ~v) 190 ATOMIC_ASM(add, int, "addl %2,%0", v) 191 ATOMIC_ASM(subtract, int, "subl %2,%0", v) 192 193 ATOMIC_ASM(set, long, "orl %2,%0", v) 194 ATOMIC_ASM(clear, long, "andl %2,%0", ~v) 195 ATOMIC_ASM(add, long, "addl %2,%0", v) 196 ATOMIC_ASM(subtract, long, "subl %2,%0", v) 197 198 #else 199 200 /* gcc <= 2.8 version */ 201 ATOMIC_ASM(set, char, "orb %1,%0", v) 202 ATOMIC_ASM(clear, char, "andb %1,%0", ~v) 203 ATOMIC_ASM(add, char, "addb %1,%0", v) 204 ATOMIC_ASM(subtract, char, "subb %1,%0", v) 205 206 ATOMIC_ASM(set, short, "orw %1,%0", v) 207 ATOMIC_ASM(clear, short, "andw %1,%0", ~v) 208 ATOMIC_ASM(add, short, "addw %1,%0", v) 209 ATOMIC_ASM(subtract, short, "subw %1,%0", v) 210 211 ATOMIC_ASM(set, int, "orl %1,%0", v) 212 ATOMIC_ASM(clear, int, "andl %1,%0", ~v) 213 ATOMIC_ASM(add, int, "addl %1,%0", v) 214 ATOMIC_ASM(subtract, int, "subl %1,%0", v) 215 216 ATOMIC_ASM(set, long, "orl %1,%0", v) 217 ATOMIC_ASM(clear, long, "andl %1,%0", ~v) 218 ATOMIC_ASM(add, long, "addl %1,%0", v) 219 ATOMIC_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) \ 227 static __inline void \ 228 atomic_##NAME##_acq_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\ 229 { \ 230 __asm __volatile("lock; addl $0,0(%esp)" : : : "memory");\ 231 atomic_##NAME##_##TYPE(p, v); \ 232 } \ 233 \ 234 static __inline void \ 235 atomic_##NAME##_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\ 236 { \ 237 atomic_##NAME##_##TYPE(p, v); \ 238 } 239 240 ATOMIC_ACQ_REL(set, char) 241 ATOMIC_ACQ_REL(clear, char) 242 ATOMIC_ACQ_REL(add, char) 243 ATOMIC_ACQ_REL(subtract, char) 244 ATOMIC_ACQ_REL(set, short) 245 ATOMIC_ACQ_REL(clear, short) 246 ATOMIC_ACQ_REL(add, short) 247 ATOMIC_ACQ_REL(subtract, short) 248 ATOMIC_ACQ_REL(set, int) 249 ATOMIC_ACQ_REL(clear, int) 250 ATOMIC_ACQ_REL(add, int) 251 ATOMIC_ACQ_REL(subtract, int) 252 ATOMIC_ACQ_REL(set, long) 253 ATOMIC_ACQ_REL(clear, long) 254 ATOMIC_ACQ_REL(add, long) 255 ATOMIC_ACQ_REL(subtract, long) 256 257 #undef ATOMIC_ACQ_REL 258 259 /* 260 * We assume that a = b will do atomic loads and stores. 261 */ 262 #define ATOMIC_STORE_LOAD(TYPE) \ 263 static __inline u_##TYPE \ 264 atomic_load_acq_##TYPE(volatile u_##TYPE *p) \ 265 { \ 266 __asm __volatile("lock; addl $0,0(%esp)" : : : "memory");\ 267 return (*p); \ 268 } \ 269 \ 270 static __inline void \ 271 atomic_store_rel_##TYPE(volatile u_##TYPE *p, u_##TYPE v)\ 272 { \ 273 *p = v; \ 274 __asm __volatile("" : : : "memory"); \ 275 } 276 277 ATOMIC_STORE_LOAD(char) 278 ATOMIC_STORE_LOAD(short) 279 ATOMIC_STORE_LOAD(int) 280 ATOMIC_STORE_LOAD(long) 281 282 #undef ATOMIC_STORE_LOAD 283 284 static __inline int 285 atomic_cmpset_ptr(volatile void *dst, void *exp, void *src) 286 { 287 288 return ( 289 atomic_cmpset_int((volatile u_int *)dst, (u_int)exp, (u_int)src)); 290 } 291 292 #define atomic_cmpset_acq_ptr atomic_cmpset_ptr 293 #define atomic_cmpset_rel_ptr atomic_cmpset_ptr 294 295 static __inline void * 296 atomic_load_acq_ptr(volatile void *p) 297 { 298 return (void *)atomic_load_acq_int((volatile u_int *)p); 299 } 300 301 static __inline void 302 atomic_store_rel_ptr(volatile void *p, void *v) 303 { 304 atomic_store_rel_int((volatile u_int *)p, (u_int)v); 305 } 306 307 #define ATOMIC_PTR(NAME) \ 308 static __inline void \ 309 atomic_##NAME##_ptr(volatile void *p, uintptr_t v) \ 310 { \ 311 atomic_##NAME##_int((volatile u_int *)p, v); \ 312 } \ 313 \ 314 static __inline void \ 315 atomic_##NAME##_acq_ptr(volatile void *p, uintptr_t v) \ 316 { \ 317 atomic_##NAME##_acq_int((volatile u_int *)p, v);\ 318 } \ 319 \ 320 static __inline void \ 321 atomic_##NAME##_rel_ptr(volatile void *p, uintptr_t v) \ 322 { \ 323 atomic_##NAME##_rel_int((volatile u_int *)p, v);\ 324 } 325 326 ATOMIC_PTR(set) 327 ATOMIC_PTR(clear) 328 ATOMIC_PTR(add) 329 ATOMIC_PTR(subtract) 330 331 #undef ATOMIC_PTR 332 333 static __inline u_int 334 atomic_readandclear_int(volatile u_int *addr) 335 { 336 u_int result; 337 338 __asm __volatile ( 339 " xorl %0,%0 ; " 340 " xchgl %1,%0 ; " 341 "# atomic_readandclear_int" 342 : "=&r" (result) /* 0 (result) */ 343 : "m" (*addr)); /* 1 (addr) */ 344 345 return (result); 346 } 347 348 static __inline u_long 349 atomic_readandclear_long(volatile u_long *addr) 350 { 351 u_long result; 352 353 __asm __volatile ( 354 " xorl %0,%0 ; " 355 " xchgl %1,%0 ; " 356 "# atomic_readandclear_int" 357 : "=&r" (result) /* 0 (result) */ 358 : "m" (*addr)); /* 1 (addr) */ 359 360 return (result); 361 } 362 #endif 363 364 #define atomic_set_8 atomic_set_char 365 #define atomic_set_acq_8 atomic_set_acq_char 366 #define atomic_set_rel_8 atomic_set_rel_char 367 #define atomic_clear_8 atomic_clear_char 368 #define atomic_clear_acq_8 atomic_clear_acq_char 369 #define atomic_clear_rel_8 atomic_clear_rel_char 370 #define atomic_add_8 atomic_add_char 371 #define atomic_add_acq_8 atomic_add_acq_char 372 #define atomic_add_rel_8 atomic_add_rel_char 373 #define atomic_subtract_8 atomic_subtract_char 374 #define atomic_subtract_acq_8 atomic_subtract_acq_char 375 #define atomic_subtract_rel_8 atomic_subtract_rel_char 376 #define atomic_load_acq_8 atomic_load_acq_char 377 #define atomic_store_rel_8 atomic_store_rel_char 378 379 #define atomic_set_16 atomic_set_short 380 #define atomic_set_acq_16 atomic_set_acq_short 381 #define atomic_set_rel_16 atomic_set_rel_short 382 #define atomic_clear_16 atomic_clear_short 383 #define atomic_clear_acq_16 atomic_clear_acq_short 384 #define atomic_clear_rel_16 atomic_clear_rel_short 385 #define atomic_add_16 atomic_add_short 386 #define atomic_add_acq_16 atomic_add_acq_short 387 #define atomic_add_rel_16 atomic_add_rel_short 388 #define atomic_subtract_16 atomic_subtract_short 389 #define atomic_subtract_acq_16 atomic_subtract_acq_short 390 #define atomic_subtract_rel_16 atomic_subtract_rel_short 391 #define atomic_load_acq_16 atomic_load_acq_short 392 #define atomic_store_rel_16 atomic_store_rel_short 393 394 #define atomic_set_32 atomic_set_int 395 #define atomic_set_acq_32 atomic_set_acq_int 396 #define atomic_set_rel_32 atomic_set_rel_int 397 #define atomic_clear_32 atomic_clear_int 398 #define atomic_clear_acq_32 atomic_clear_acq_int 399 #define atomic_clear_rel_32 atomic_clear_rel_int 400 #define atomic_add_32 atomic_add_int 401 #define atomic_add_acq_32 atomic_add_acq_int 402 #define atomic_add_rel_32 atomic_add_rel_int 403 #define atomic_subtract_32 atomic_subtract_int 404 #define atomic_subtract_acq_32 atomic_subtract_acq_int 405 #define atomic_subtract_rel_32 atomic_subtract_rel_int 406 #define atomic_load_acq_32 atomic_load_acq_int 407 #define atomic_store_rel_32 atomic_store_rel_int 408 #define atomic_cmpset_32 atomic_cmpset_int 409 #define atomic_cmpset_acq_32 atomic_cmpset_acq_int 410 #define atomic_cmpset_rel_32 atomic_cmpset_rel_int 411 #define atomic_readandclear_32 atomic_readandclear_int 412 413 #endif /* ! _MACHINE_ATOMIC_H_ */ 414