1 #ifndef _ASM_X86_CMPXCHG_64_H
2 #define _ASM_X86_CMPXCHG_64_H
4 #include <asm/alternative.h> /* Provides LOCK_PREFIX */
6 #define __xg(x) ((volatile long *)(x))
8 static inline void set_64bit(volatile unsigned long *ptr
, unsigned long val
)
13 #define _set_64bit set_64bit
15 extern void __xchg_wrong_size(void);
16 extern void __cmpxchg_wrong_size(void);
19 * Note: no "lock" prefix even on SMP: xchg always implies lock anyway
20 * Note 2: xchg has side effect, so that attribute volatile is necessary,
21 * but generally the primitive is invalid, *ptr is output argument. --ANK
23 #define __xchg(x, ptr, size) \
25 __typeof(*(ptr)) __x = (x); \
28 asm volatile("xchgb %b0,%1" \
30 : "m" (*__xg(ptr)), "0" (__x) \
34 asm volatile("xchgw %w0,%1" \
36 : "m" (*__xg(ptr)), "0" (__x) \
40 asm volatile("xchgl %k0,%1" \
42 : "m" (*__xg(ptr)), "0" (__x) \
46 asm volatile("xchgq %0,%1" \
48 : "m" (*__xg(ptr)), "0" (__x) \
52 __xchg_wrong_size(); \
57 #define xchg(ptr, v) \
58 __xchg((v), (ptr), sizeof(*ptr))
60 #define __HAVE_ARCH_CMPXCHG 1
63 * Atomic compare and exchange. Compare OLD with MEM, if identical,
64 * store NEW in MEM. Return the initial value in MEM. Success is
65 * indicated by comparing RETURN with OLD.
67 #define __raw_cmpxchg(ptr, old, new, size, lock) \
69 __typeof__(*(ptr)) __ret; \
70 __typeof__(*(ptr)) __old = (old); \
71 __typeof__(*(ptr)) __new = (new); \
74 asm volatile(lock "cmpxchgb %b1,%2" \
76 : "q"(__new), "m"(*__xg(ptr)), "0"(__old) \
80 asm volatile(lock "cmpxchgw %w1,%2" \
82 : "r"(__new), "m"(*__xg(ptr)), "0"(__old) \
86 asm volatile(lock "cmpxchgl %k1,%2" \
88 : "r"(__new), "m"(*__xg(ptr)), "0"(__old) \
92 asm volatile(lock "cmpxchgq %1,%2" \
94 : "r"(__new), "m"(*__xg(ptr)), "0"(__old) \
98 __cmpxchg_wrong_size(); \
103 #define __cmpxchg(ptr, old, new, size) \
104 __raw_cmpxchg((ptr), (old), (new), (size), LOCK_PREFIX)
106 #define __sync_cmpxchg(ptr, old, new, size) \
107 __raw_cmpxchg((ptr), (old), (new), (size), "lock; ")
109 #define __cmpxchg_local(ptr, old, new, size) \
110 __raw_cmpxchg((ptr), (old), (new), (size), "")
112 #define cmpxchg(ptr, old, new) \
113 __cmpxchg((ptr), (old), (new), sizeof(*ptr))
115 #define sync_cmpxchg(ptr, old, new) \
116 __sync_cmpxchg((ptr), (old), (new), sizeof(*ptr))
118 #define cmpxchg_local(ptr, old, new) \
119 __cmpxchg_local((ptr), (old), (new), sizeof(*ptr))
121 #define cmpxchg64(ptr, o, n) \
123 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
124 cmpxchg((ptr), (o), (n)); \
127 #define cmpxchg64_local(ptr, o, n) \
129 BUILD_BUG_ON(sizeof(*(ptr)) != 8); \
130 cmpxchg_local((ptr), (o), (n)); \
133 #endif /* _ASM_X86_CMPXCHG_64_H */