sanitizer_atomic_clang_x86.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //===-- sanitizer_atomic_clang_x86.h ----------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
  10. // Not intended for direct inclusion. Include sanitizer_atomic.h.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef SANITIZER_ATOMIC_CLANG_X86_H
  14. #define SANITIZER_ATOMIC_CLANG_X86_H
  15. namespace __sanitizer {
  16. inline void proc_yield(int cnt) {
  17. __asm__ __volatile__("" ::: "memory");
  18. for (int i = 0; i < cnt; i++)
  19. __asm__ __volatile__("pause");
  20. __asm__ __volatile__("" ::: "memory");
  21. }
  22. template<typename T>
  23. inline typename T::Type atomic_load(
  24. const volatile T *a, memory_order mo) {
  25. DCHECK(mo & (memory_order_relaxed | memory_order_consume
  26. | memory_order_acquire | memory_order_seq_cst));
  27. DCHECK(!((uptr)a % sizeof(*a)));
  28. typename T::Type v;
  29. if (sizeof(*a) < 8 || sizeof(void*) == 8) {
  30. // Assume that aligned loads are atomic.
  31. if (mo == memory_order_relaxed) {
  32. v = a->val_dont_use;
  33. } else if (mo == memory_order_consume) {
  34. // Assume that processor respects data dependencies
  35. // (and that compiler won't break them).
  36. __asm__ __volatile__("" ::: "memory");
  37. v = a->val_dont_use;
  38. __asm__ __volatile__("" ::: "memory");
  39. } else if (mo == memory_order_acquire) {
  40. __asm__ __volatile__("" ::: "memory");
  41. v = a->val_dont_use;
  42. // On x86 loads are implicitly acquire.
  43. __asm__ __volatile__("" ::: "memory");
  44. } else { // seq_cst
  45. // On x86 plain MOV is enough for seq_cst store.
  46. __asm__ __volatile__("" ::: "memory");
  47. v = a->val_dont_use;
  48. __asm__ __volatile__("" ::: "memory");
  49. }
  50. } else {
  51. // 64-bit load on 32-bit platform.
  52. __asm__ __volatile__(
  53. "movq %1, %%mm0;" // Use mmx reg for 64-bit atomic moves
  54. "movq %%mm0, %0;" // (ptr could be read-only)
  55. "emms;" // Empty mmx state/Reset FP regs
  56. : "=m" (v)
  57. : "m" (a->val_dont_use)
  58. : // mark the mmx registers as clobbered
  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 mmx registers as clobbered
  95. #ifdef __MMX__
  96. "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
  97. #endif // #ifdef __MMX__
  98. "memory");
  99. if (mo == memory_order_seq_cst)
  100. __sync_synchronize();
  101. }
  102. }
  103. } // namespace __sanitizer
  104. #endif // #ifndef SANITIZER_ATOMIC_CLANG_X86_H