sanitizer_atomic_clang_x86.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //===-- sanitizer_atomic_clang_x86.h ----------------------------*- C++ -*-===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
  9. // Not intended for direct inclusion. Include sanitizer_atomic.h.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef SANITIZER_ATOMIC_CLANG_X86_H
  13. #define SANITIZER_ATOMIC_CLANG_X86_H
  14. namespace __sanitizer {
  15. INLINE void proc_yield(int cnt) {
  16. __asm__ __volatile__("" ::: "memory");
  17. for (int i = 0; i < cnt; i++)
  18. __asm__ __volatile__("pause");
  19. __asm__ __volatile__("" ::: "memory");
  20. }
  21. template<typename T>
  22. INLINE typename T::Type atomic_load(
  23. const volatile T *a, memory_order mo) {
  24. DCHECK(mo & (memory_order_relaxed | memory_order_consume
  25. | memory_order_acquire | memory_order_seq_cst));
  26. DCHECK(!((uptr)a % sizeof(*a)));
  27. typename T::Type v;
  28. if (sizeof(*a) < 8 || sizeof(void*) == 8) {
  29. // Assume that aligned loads are atomic.
  30. if (mo == memory_order_relaxed) {
  31. v = a->val_dont_use;
  32. } else if (mo == memory_order_consume) {
  33. // Assume that processor respects data dependencies
  34. // (and that compiler won't break them).
  35. __asm__ __volatile__("" ::: "memory");
  36. v = a->val_dont_use;
  37. __asm__ __volatile__("" ::: "memory");
  38. } else if (mo == memory_order_acquire) {
  39. __asm__ __volatile__("" ::: "memory");
  40. v = a->val_dont_use;
  41. // On x86 loads are implicitly acquire.
  42. __asm__ __volatile__("" ::: "memory");
  43. } else { // seq_cst
  44. // On x86 plain MOV is enough for seq_cst store.
  45. __asm__ __volatile__("" ::: "memory");
  46. v = a->val_dont_use;
  47. __asm__ __volatile__("" ::: "memory");
  48. }
  49. } else {
  50. // 64-bit load on 32-bit platform.
  51. __asm__ __volatile__(
  52. "movq %1, %%mm0;" // Use mmx reg for 64-bit atomic moves
  53. "movq %%mm0, %0;" // (ptr could be read-only)
  54. "emms;" // Empty mmx state/Reset FP regs
  55. : "=m" (v)
  56. : "m" (a->val_dont_use)
  57. : // mark the FP stack and mmx registers as clobbered
  58. "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)",
  59. #ifdef __MMX__
  60. "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
  61. #endif // #ifdef __MMX__
  62. "memory");
  63. }
  64. return v;
  65. }
  66. template<typename T>
  67. INLINE void atomic_store(volatile T *a, typename T::Type v, memory_order mo) {
  68. DCHECK(mo & (memory_order_relaxed | memory_order_release
  69. | memory_order_seq_cst));
  70. DCHECK(!((uptr)a % sizeof(*a)));
  71. if (sizeof(*a) < 8 || sizeof(void*) == 8) {
  72. // Assume that aligned loads are atomic.
  73. if (mo == memory_order_relaxed) {
  74. a->val_dont_use = v;
  75. } else if (mo == memory_order_release) {
  76. // On x86 stores are implicitly release.
  77. __asm__ __volatile__("" ::: "memory");
  78. a->val_dont_use = v;
  79. __asm__ __volatile__("" ::: "memory");
  80. } else { // seq_cst
  81. // On x86 stores are implicitly release.
  82. __asm__ __volatile__("" ::: "memory");
  83. a->val_dont_use = v;
  84. __sync_synchronize();
  85. }
  86. } else {
  87. // 64-bit store on 32-bit platform.
  88. __asm__ __volatile__(
  89. "movq %1, %%mm0;" // Use mmx reg for 64-bit atomic moves
  90. "movq %%mm0, %0;"
  91. "emms;" // Empty mmx state/Reset FP regs
  92. : "=m" (a->val_dont_use)
  93. : "m" (v)
  94. : // mark the FP stack and mmx registers as clobbered
  95. "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)",
  96. #ifdef __MMX__
  97. "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
  98. #endif // #ifdef __MMX__
  99. "memory");
  100. if (mo == memory_order_seq_cst)
  101. __sync_synchronize();
  102. }
  103. }
  104. } // namespace __sanitizer
  105. #endif // #ifndef SANITIZER_ATOMIC_CLANG_X86_H