cmpxchg_32.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #ifdef CONFIG_X86_CMPXCHG64
  107. #define cmpxchg64(ptr, o, n) \
  108. ((__typeof__(*(ptr)))__cmpxchg64((ptr), (unsigned long long)(o), \
  109. (unsigned long long)(n)))
  110. #define cmpxchg64_local(ptr, o, n) \
  111. ((__typeof__(*(ptr)))__cmpxchg64_local((ptr), (unsigned long long)(o),\
  112. (unsigned long long)(n)))
  113. #endif
  114. static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
  115. unsigned long new, int size)
  116. {
  117. unsigned long prev;
  118. switch (size) {
  119. case 1:
  120. __asm__ __volatile__(LOCK_PREFIX "cmpxchgb %b1,%2"
  121. : "=a"(prev)
  122. : "q"(new), "m"(*__xg(ptr)), "0"(old)
  123. : "memory");
  124. return prev;
  125. case 2:
  126. __asm__ __volatile__(LOCK_PREFIX "cmpxchgw %w1,%2"
  127. : "=a"(prev)
  128. : "r"(new), "m"(*__xg(ptr)), "0"(old)
  129. : "memory");
  130. return prev;
  131. case 4:
  132. __asm__ __volatile__(LOCK_PREFIX "cmpxchgl %1,%2"
  133. : "=a"(prev)
  134. : "r"(new), "m"(*__xg(ptr)), "0"(old)
  135. : "memory");
  136. return prev;
  137. }
  138. return old;
  139. }
  140. /*
  141. * Always use locked operations when touching memory shared with a
  142. * hypervisor, since the system may be SMP even if the guest kernel
  143. * isn't.
  144. */
  145. static inline unsigned long __sync_cmpxchg(volatile void *ptr,
  146. unsigned long old,
  147. unsigned long new, int size)
  148. {
  149. unsigned long prev;
  150. switch (size) {
  151. case 1:
  152. __asm__ __volatile__("lock; cmpxchgb %b1,%2"
  153. : "=a"(prev)
  154. : "q"(new), "m"(*__xg(ptr)), "0"(old)
  155. : "memory");
  156. return prev;
  157. case 2:
  158. __asm__ __volatile__("lock; cmpxchgw %w1,%2"
  159. : "=a"(prev)
  160. : "r"(new), "m"(*__xg(ptr)), "0"(old)
  161. : "memory");
  162. return prev;
  163. case 4:
  164. __asm__ __volatile__("lock; cmpxchgl %1,%2"
  165. : "=a"(prev)
  166. : "r"(new), "m"(*__xg(ptr)), "0"(old)
  167. : "memory");
  168. return prev;
  169. }
  170. return old;
  171. }
  172. static inline unsigned long __cmpxchg_local(volatile void *ptr,
  173. unsigned long old, unsigned long new, int size)
  174. {
  175. unsigned long prev;
  176. switch (size) {
  177. case 1:
  178. __asm__ __volatile__("cmpxchgb %b1,%2"
  179. : "=a"(prev)
  180. : "q"(new), "m"(*__xg(ptr)), "0"(old)
  181. : "memory");
  182. return prev;
  183. case 2:
  184. __asm__ __volatile__("cmpxchgw %w1,%2"
  185. : "=a"(prev)
  186. : "r"(new), "m"(*__xg(ptr)), "0"(old)
  187. : "memory");
  188. return prev;
  189. case 4:
  190. __asm__ __volatile__("cmpxchgl %1,%2"
  191. : "=a"(prev)
  192. : "r"(new), "m"(*__xg(ptr)), "0"(old)
  193. : "memory");
  194. return prev;
  195. }
  196. return old;
  197. }
  198. static inline unsigned long long __cmpxchg64(volatile void *ptr,
  199. unsigned long long old, unsigned long long new)
  200. {
  201. unsigned long long prev;
  202. __asm__ __volatile__(LOCK_PREFIX "cmpxchg8b %3"
  203. : "=A"(prev)
  204. : "b"((unsigned long)new),
  205. "c"((unsigned long)(new >> 32)),
  206. "m"(*__xg(ptr)),
  207. "0"(old)
  208. : "memory");
  209. return prev;
  210. }
  211. static inline unsigned long long __cmpxchg64_local(volatile void *ptr,
  212. unsigned long long old, unsigned long long new)
  213. {
  214. unsigned long long prev;
  215. __asm__ __volatile__("cmpxchg8b %3"
  216. : "=A"(prev)
  217. : "b"((unsigned long)new),
  218. "c"((unsigned long)(new >> 32)),
  219. "m"(*__xg(ptr)),
  220. "0"(old)
  221. : "memory");
  222. return prev;
  223. }
  224. #ifndef CONFIG_X86_CMPXCHG
  225. /*
  226. * Building a kernel capable running on 80386. It may be necessary to
  227. * simulate the cmpxchg on the 80386 CPU. For that purpose we define
  228. * a function for each of the sizes we support.
  229. */
  230. extern unsigned long cmpxchg_386_u8(volatile void *, u8, u8);
  231. extern unsigned long cmpxchg_386_u16(volatile void *, u16, u16);
  232. extern unsigned long cmpxchg_386_u32(volatile void *, u32, u32);
  233. static inline unsigned long cmpxchg_386(volatile void *ptr, unsigned long old,
  234. unsigned long new, int size)
  235. {
  236. switch (size) {
  237. case 1:
  238. return cmpxchg_386_u8(ptr, old, new);
  239. case 2:
  240. return cmpxchg_386_u16(ptr, old, new);
  241. case 4:
  242. return cmpxchg_386_u32(ptr, old, new);
  243. }
  244. return old;
  245. }
  246. #define cmpxchg(ptr, o, n) \
  247. ({ \
  248. __typeof__(*(ptr)) __ret; \
  249. if (likely(boot_cpu_data.x86 > 3)) \
  250. __ret = (__typeof__(*(ptr)))__cmpxchg((ptr), \
  251. (unsigned long)(o), (unsigned long)(n), \
  252. sizeof(*(ptr))); \
  253. else \
  254. __ret = (__typeof__(*(ptr)))cmpxchg_386((ptr), \
  255. (unsigned long)(o), (unsigned long)(n), \
  256. sizeof(*(ptr))); \
  257. __ret; \
  258. })
  259. #define cmpxchg_local(ptr, o, n) \
  260. ({ \
  261. __typeof__(*(ptr)) __ret; \
  262. if (likely(boot_cpu_data.x86 > 3)) \
  263. __ret = (__typeof__(*(ptr)))__cmpxchg_local((ptr), \
  264. (unsigned long)(o), (unsigned long)(n), \
  265. sizeof(*(ptr))); \
  266. else \
  267. __ret = (__typeof__(*(ptr)))cmpxchg_386((ptr), \
  268. (unsigned long)(o), (unsigned long)(n), \
  269. sizeof(*(ptr))); \
  270. __ret; \
  271. })
  272. #endif
  273. #ifndef CONFIG_X86_CMPXCHG64
  274. /*
  275. * Building a kernel capable running on 80386 and 80486. It may be necessary
  276. * to simulate the cmpxchg8b on the 80386 and 80486 CPU.
  277. */
  278. extern unsigned long long cmpxchg_486_u64(volatile void *, u64, u64);
  279. #define cmpxchg64(ptr, o, n) \
  280. ({ \
  281. __typeof__(*(ptr)) __ret; \
  282. if (likely(boot_cpu_data.x86 > 4)) \
  283. __ret = (__typeof__(*(ptr)))__cmpxchg64((ptr), \
  284. (unsigned long long)(o), \
  285. (unsigned long long)(n)); \
  286. else \
  287. __ret = (__typeof__(*(ptr)))cmpxchg_486_u64((ptr), \
  288. (unsigned long long)(o), \
  289. (unsigned long long)(n)); \
  290. __ret; \
  291. })
  292. #define cmpxchg64_local(ptr, o, n) \
  293. ({ \
  294. __typeof__(*(ptr)) __ret; \
  295. if (likely(boot_cpu_data.x86 > 4)) \
  296. __ret = (__typeof__(*(ptr)))__cmpxchg64_local((ptr), \
  297. (unsigned long long)(o), \
  298. (unsigned long long)(n)); \
  299. else \
  300. __ret = (__typeof__(*(ptr)))cmpxchg_486_u64((ptr), \
  301. (unsigned long long)(o), \
  302. (unsigned long long)(n)); \
  303. __ret; \
  304. })
  305. #endif
  306. #endif