cmpxchg_32.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #ifndef _ASM_X86_CMPXCHG_32_H
  2. #define _ASM_X86_CMPXCHG_32_H
  3. #include <linux/bitops.h> /* for LOCK_PREFIX */
  4. /*
  5. * Note: if you use set64_bit(), __cmpxchg64(), or their variants, you
  6. * you need to test for the feature in boot_cpu_data.
  7. */
  8. extern void __xchg_wrong_size(void);
  9. /*
  10. * Note: no "lock" prefix even on SMP: xchg always implies lock anyway
  11. * Note 2: xchg has side effect, so that attribute volatile is necessary,
  12. * but generally the primitive is invalid, *ptr is output argument. --ANK
  13. */
  14. struct __xchg_dummy {
  15. unsigned long a[100];
  16. };
  17. #define __xg(x) ((struct __xchg_dummy *)(x))
  18. #define __xchg(x, ptr, size) \
  19. ({ \
  20. __typeof(*(ptr)) __x = (x); \
  21. switch (size) { \
  22. case 1: \
  23. asm volatile("xchgb %b0,%1" \
  24. : "=q" (__x) \
  25. : "m" (*__xg(ptr)), "0" (__x) \
  26. : "memory"); \
  27. break; \
  28. case 2: \
  29. asm volatile("xchgw %w0,%1" \
  30. : "=r" (__x) \
  31. : "m" (*__xg(ptr)), "0" (__x) \
  32. : "memory"); \
  33. break; \
  34. case 4: \
  35. asm volatile("xchgl %0,%1" \
  36. : "=r" (__x) \
  37. : "m" (*__xg(ptr)), "0" (__x) \
  38. : "memory"); \
  39. break; \
  40. default: \
  41. __xchg_wrong_size(); \
  42. } \
  43. __x; \
  44. })
  45. #define xchg(ptr, v) \
  46. __xchg((v), (ptr), sizeof(*ptr))
  47. /*
  48. * The semantics of XCHGCMP8B are a bit strange, this is why
  49. * there is a loop and the loading of %%eax and %%edx has to
  50. * be inside. This inlines well in most cases, the cached
  51. * cost is around ~38 cycles. (in the future we might want
  52. * to do an SIMD/3DNOW!/MMX/FPU 64-bit store here, but that
  53. * might have an implicit FPU-save as a cost, so it's not
  54. * clear which path to go.)
  55. *
  56. * cmpxchg8b must be used with the lock prefix here to allow
  57. * the instruction to be executed atomically, see page 3-102
  58. * of the instruction set reference 24319102.pdf. We need
  59. * the reader side to see the coherent 64bit value.
  60. */
  61. static inline void __set_64bit(unsigned long long *ptr,
  62. unsigned int low, unsigned int high)
  63. {
  64. asm volatile("\n1:\t"
  65. "movl (%0), %%eax\n\t"
  66. "movl 4(%0), %%edx\n\t"
  67. LOCK_PREFIX "cmpxchg8b (%0)\n\t"
  68. "jnz 1b"
  69. : /* no outputs */
  70. : "D"(ptr),
  71. "b"(low),
  72. "c"(high)
  73. : "ax", "dx", "memory");
  74. }
  75. static inline void __set_64bit_constant(unsigned long long *ptr,
  76. unsigned long long value)
  77. {
  78. __set_64bit(ptr, (unsigned int)value, (unsigned int)(value >> 32));
  79. }
  80. #define ll_low(x) *(((unsigned int *)&(x)) + 0)
  81. #define ll_high(x) *(((unsigned int *)&(x)) + 1)
  82. static inline void __set_64bit_var(unsigned long long *ptr,
  83. unsigned long long value)
  84. {
  85. __set_64bit(ptr, ll_low(value), ll_high(value));
  86. }
  87. #define set_64bit(ptr, value) \
  88. (__builtin_constant_p((value)) \
  89. ? __set_64bit_constant((ptr), (value)) \
  90. : __set_64bit_var((ptr), (value)))
  91. #define _set_64bit(ptr, value) \
  92. (__builtin_constant_p(value) \
  93. ? __set_64bit(ptr, (unsigned int)(value), \
  94. (unsigned int)((value) >> 32)) \
  95. : __set_64bit(ptr, ll_low((value)), ll_high((value))))
  96. extern void __cmpxchg_wrong_size(void);
  97. /*
  98. * Atomic compare and exchange. Compare OLD with MEM, if identical,
  99. * store NEW in MEM. Return the initial value in MEM. Success is
  100. * indicated by comparing RETURN with OLD.
  101. */
  102. #define __raw_cmpxchg(ptr, old, new, size, lock) \
  103. ({ \
  104. __typeof__(*(ptr)) __ret; \
  105. __typeof__(*(ptr)) __old = (old); \
  106. __typeof__(*(ptr)) __new = (new); \
  107. switch (size) { \
  108. case 1: \
  109. asm volatile(lock "cmpxchgb %b1,%2" \
  110. : "=a"(__ret) \
  111. : "q"(__new), "m"(*__xg(ptr)), "0"(__old) \
  112. : "memory"); \
  113. break; \
  114. case 2: \
  115. asm volatile(lock "cmpxchgw %w1,%2" \
  116. : "=a"(__ret) \
  117. : "r"(__new), "m"(*__xg(ptr)), "0"(__old) \
  118. : "memory"); \
  119. break; \
  120. case 4: \
  121. asm volatile(lock "cmpxchgl %1,%2" \
  122. : "=a"(__ret) \
  123. : "r"(__new), "m"(*__xg(ptr)), "0"(__old) \
  124. : "memory"); \
  125. break; \
  126. default: \
  127. __cmpxchg_wrong_size(); \
  128. } \
  129. __ret; \
  130. })
  131. #define __cmpxchg(ptr, old, new, size) \
  132. __raw_cmpxchg((ptr), (old), (new), (size), LOCK_PREFIX)
  133. #define __sync_cmpxchg(ptr, old, new, size) \
  134. __raw_cmpxchg((ptr), (old), (new), (size), "lock; ")
  135. #define __cmpxchg_local(ptr, old, new, size) \
  136. __raw_cmpxchg((ptr), (old), (new), (size), "")
  137. #ifdef CONFIG_X86_CMPXCHG
  138. #define __HAVE_ARCH_CMPXCHG 1
  139. #define cmpxchg(ptr, old, new) \
  140. __cmpxchg((ptr), (old), (new), sizeof(*ptr))
  141. #define sync_cmpxchg(ptr, old, new) \
  142. __sync_cmpxchg((ptr), (old), (new), sizeof(*ptr))
  143. #define cmpxchg_local(ptr, old, new) \
  144. __cmpxchg_local((ptr), (old), (new), sizeof(*ptr))
  145. #endif
  146. #ifdef CONFIG_X86_CMPXCHG64
  147. #define cmpxchg64(ptr, o, n) \
  148. ((__typeof__(*(ptr)))__cmpxchg64((ptr), (unsigned long long)(o), \
  149. (unsigned long long)(n)))
  150. #define cmpxchg64_local(ptr, o, n) \
  151. ((__typeof__(*(ptr)))__cmpxchg64_local((ptr), (unsigned long long)(o), \
  152. (unsigned long long)(n)))
  153. #endif
  154. static inline unsigned long long __cmpxchg64(volatile void *ptr,
  155. unsigned long long old,
  156. unsigned long long new)
  157. {
  158. unsigned long long prev;
  159. asm volatile(LOCK_PREFIX "cmpxchg8b %3"
  160. : "=A"(prev)
  161. : "b"((unsigned long)new),
  162. "c"((unsigned long)(new >> 32)),
  163. "m"(*__xg(ptr)),
  164. "0"(old)
  165. : "memory");
  166. return prev;
  167. }
  168. static inline unsigned long long __cmpxchg64_local(volatile void *ptr,
  169. unsigned long long old,
  170. unsigned long long new)
  171. {
  172. unsigned long long prev;
  173. asm volatile("cmpxchg8b %3"
  174. : "=A"(prev)
  175. : "b"((unsigned long)new),
  176. "c"((unsigned long)(new >> 32)),
  177. "m"(*__xg(ptr)),
  178. "0"(old)
  179. : "memory");
  180. return prev;
  181. }
  182. #ifndef CONFIG_X86_CMPXCHG
  183. /*
  184. * Building a kernel capable running on 80386. It may be necessary to
  185. * simulate the cmpxchg on the 80386 CPU. For that purpose we define
  186. * a function for each of the sizes we support.
  187. */
  188. extern unsigned long cmpxchg_386_u8(volatile void *, u8, u8);
  189. extern unsigned long cmpxchg_386_u16(volatile void *, u16, u16);
  190. extern unsigned long cmpxchg_386_u32(volatile void *, u32, u32);
  191. static inline unsigned long cmpxchg_386(volatile void *ptr, unsigned long old,
  192. unsigned long new, int size)
  193. {
  194. switch (size) {
  195. case 1:
  196. return cmpxchg_386_u8(ptr, old, new);
  197. case 2:
  198. return cmpxchg_386_u16(ptr, old, new);
  199. case 4:
  200. return cmpxchg_386_u32(ptr, old, new);
  201. }
  202. return old;
  203. }
  204. #define cmpxchg(ptr, o, n) \
  205. ({ \
  206. __typeof__(*(ptr)) __ret; \
  207. if (likely(boot_cpu_data.x86 > 3)) \
  208. __ret = (__typeof__(*(ptr)))__cmpxchg((ptr), \
  209. (unsigned long)(o), (unsigned long)(n), \
  210. sizeof(*(ptr))); \
  211. else \
  212. __ret = (__typeof__(*(ptr)))cmpxchg_386((ptr), \
  213. (unsigned long)(o), (unsigned long)(n), \
  214. sizeof(*(ptr))); \
  215. __ret; \
  216. })
  217. #define cmpxchg_local(ptr, o, n) \
  218. ({ \
  219. __typeof__(*(ptr)) __ret; \
  220. if (likely(boot_cpu_data.x86 > 3)) \
  221. __ret = (__typeof__(*(ptr)))__cmpxchg_local((ptr), \
  222. (unsigned long)(o), (unsigned long)(n), \
  223. sizeof(*(ptr))); \
  224. else \
  225. __ret = (__typeof__(*(ptr)))cmpxchg_386((ptr), \
  226. (unsigned long)(o), (unsigned long)(n), \
  227. sizeof(*(ptr))); \
  228. __ret; \
  229. })
  230. #endif
  231. #ifndef CONFIG_X86_CMPXCHG64
  232. /*
  233. * Building a kernel capable running on 80386 and 80486. It may be necessary
  234. * to simulate the cmpxchg8b on the 80386 and 80486 CPU.
  235. */
  236. extern unsigned long long cmpxchg_486_u64(volatile void *, u64, u64);
  237. #define cmpxchg64(ptr, o, n) \
  238. ({ \
  239. __typeof__(*(ptr)) __ret; \
  240. __typeof__(*(ptr)) __old = (o); \
  241. __typeof__(*(ptr)) __new = (n); \
  242. alternative_io("call cmpxchg8b_emu", \
  243. "lock; cmpxchg8b (%%esi)" , \
  244. X86_FEATURE_CX8, \
  245. "=A" (__ret), \
  246. "S" ((ptr)), "0" (__old), \
  247. "b" ((unsigned int)__new), \
  248. "c" ((unsigned int)(__new>>32)) \
  249. : "memory"); \
  250. __ret; })
  251. #define cmpxchg64_local(ptr, o, n) \
  252. ({ \
  253. __typeof__(*(ptr)) __ret; \
  254. if (likely(boot_cpu_data.x86 > 4)) \
  255. __ret = (__typeof__(*(ptr)))__cmpxchg64_local((ptr), \
  256. (unsigned long long)(o), \
  257. (unsigned long long)(n)); \
  258. else \
  259. __ret = (__typeof__(*(ptr)))cmpxchg_486_u64((ptr), \
  260. (unsigned long long)(o), \
  261. (unsigned long long)(n)); \
  262. __ret; \
  263. })
  264. #endif
  265. #endif /* _ASM_X86_CMPXCHG_32_H */