cmpxchg_32.h 7.9 KB

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