cmpxchg_32.h 9.2 KB

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