cmpxchg_32.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef _ASM_X86_CMPXCHG_32_H
  2. #define _ASM_X86_CMPXCHG_32_H
  3. /*
  4. * Note: if you use set64_bit(), __cmpxchg64(), or their variants, you
  5. * you need to test for the feature in boot_cpu_data.
  6. */
  7. /*
  8. * CMPXCHG8B only writes to the target if we had the previous
  9. * value in registers, otherwise it acts as a read and gives us the
  10. * "new previous" value. That is why there is a loop. Preloading
  11. * EDX:EAX is a performance optimization: in the common case it means
  12. * we need only one locked operation.
  13. *
  14. * A SIMD/3DNOW!/MMX/FPU 64-bit store here would require at the very
  15. * least an FPU save and/or %cr0.ts manipulation.
  16. *
  17. * cmpxchg8b must be used with the lock prefix here to allow the
  18. * instruction to be executed atomically. We need to have the reader
  19. * side to see the coherent 64bit value.
  20. */
  21. static inline void set_64bit(volatile u64 *ptr, u64 value)
  22. {
  23. u32 low = value;
  24. u32 high = value >> 32;
  25. u64 prev = *ptr;
  26. asm volatile("\n1:\t"
  27. LOCK_PREFIX "cmpxchg8b %0\n\t"
  28. "jnz 1b"
  29. : "=m" (*ptr), "+A" (prev)
  30. : "b" (low), "c" (high)
  31. : "memory");
  32. }
  33. #ifdef CONFIG_X86_CMPXCHG64
  34. #define cmpxchg64(ptr, o, n) \
  35. ((__typeof__(*(ptr)))__cmpxchg64((ptr), (unsigned long long)(o), \
  36. (unsigned long long)(n)))
  37. #define cmpxchg64_local(ptr, o, n) \
  38. ((__typeof__(*(ptr)))__cmpxchg64_local((ptr), (unsigned long long)(o), \
  39. (unsigned long long)(n)))
  40. #endif
  41. static inline u64 __cmpxchg64(volatile u64 *ptr, u64 old, u64 new)
  42. {
  43. u64 prev;
  44. asm volatile(LOCK_PREFIX "cmpxchg8b %1"
  45. : "=A" (prev),
  46. "+m" (*ptr)
  47. : "b" ((u32)new),
  48. "c" ((u32)(new >> 32)),
  49. "0" (old)
  50. : "memory");
  51. return prev;
  52. }
  53. static inline u64 __cmpxchg64_local(volatile u64 *ptr, u64 old, u64 new)
  54. {
  55. u64 prev;
  56. asm volatile("cmpxchg8b %1"
  57. : "=A" (prev),
  58. "+m" (*ptr)
  59. : "b" ((u32)new),
  60. "c" ((u32)(new >> 32)),
  61. "0" (old)
  62. : "memory");
  63. return prev;
  64. }
  65. #ifndef CONFIG_X86_CMPXCHG64
  66. /*
  67. * Building a kernel capable running on 80386 and 80486. It may be necessary
  68. * to simulate the cmpxchg8b on the 80386 and 80486 CPU.
  69. */
  70. #define cmpxchg64(ptr, o, n) \
  71. ({ \
  72. __typeof__(*(ptr)) __ret; \
  73. __typeof__(*(ptr)) __old = (o); \
  74. __typeof__(*(ptr)) __new = (n); \
  75. alternative_io(LOCK_PREFIX_HERE \
  76. "call cmpxchg8b_emu", \
  77. "lock; cmpxchg8b (%%esi)" , \
  78. X86_FEATURE_CX8, \
  79. "=A" (__ret), \
  80. "S" ((ptr)), "0" (__old), \
  81. "b" ((unsigned int)__new), \
  82. "c" ((unsigned int)(__new>>32)) \
  83. : "memory"); \
  84. __ret; })
  85. #define cmpxchg64_local(ptr, o, n) \
  86. ({ \
  87. __typeof__(*(ptr)) __ret; \
  88. __typeof__(*(ptr)) __old = (o); \
  89. __typeof__(*(ptr)) __new = (n); \
  90. alternative_io("call cmpxchg8b_emu", \
  91. "cmpxchg8b (%%esi)" , \
  92. X86_FEATURE_CX8, \
  93. "=A" (__ret), \
  94. "S" ((ptr)), "0" (__old), \
  95. "b" ((unsigned int)__new), \
  96. "c" ((unsigned int)(__new>>32)) \
  97. : "memory"); \
  98. __ret; })
  99. #endif
  100. #define system_has_cmpxchg_double() cpu_has_cx8
  101. #endif /* _ASM_X86_CMPXCHG_32_H */