ops_gcc_arm_common.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * Copyright (c) 2009 Helge Bahmann
  7. * Copyright (c) 2013 Tim Blechmann
  8. * Copyright (c) 2014 Andrey Semashev
  9. */
  10. /*!
  11. * \file atomic/detail/ops_gcc_arm_common.hpp
  12. *
  13. * This header contains basic utilities for gcc ARM backend.
  14. */
  15. #ifndef BOOST_ATOMIC_DETAIL_OPS_GCC_ARM_COMMON_HPP_INCLUDED_
  16. #define BOOST_ATOMIC_DETAIL_OPS_GCC_ARM_COMMON_HPP_INCLUDED_
  17. #include <boost/cstdint.hpp>
  18. #include <boost/memory_order.hpp>
  19. #include <boost/atomic/detail/config.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. #pragma once
  22. #endif
  23. namespace boost {
  24. namespace atomics {
  25. namespace detail {
  26. // A memory barrier is effected using a "co-processor 15" instruction,
  27. // though a separate assembler mnemonic is available for it in v7.
  28. //
  29. // "Thumb 1" is a subset of the ARM instruction set that uses a 16-bit encoding. It
  30. // doesn't include all instructions and in particular it doesn't include the co-processor
  31. // instruction used for the memory barrier or the load-locked/store-conditional
  32. // instructions. So, if we're compiling in "Thumb 1" mode, we need to wrap all of our
  33. // asm blocks with code to temporarily change to ARM mode.
  34. //
  35. // You can only change between ARM and Thumb modes when branching using the bx instruction.
  36. // bx takes an address specified in a register. The least significant bit of the address
  37. // indicates the mode, so 1 is added to indicate that the destination code is Thumb.
  38. // A temporary register is needed for the address and is passed as an argument to these
  39. // macros. It must be one of the "low" registers accessible to Thumb code, specified
  40. // using the "l" attribute in the asm statement.
  41. //
  42. // Architecture v7 introduces "Thumb 2", which does include (almost?) all of the ARM
  43. // instruction set. (Actually, there was an extension of v6 called v6T2 which supported
  44. // "Thumb 2" mode, but its architecture manual is no longer available, referring to v7.)
  45. // So in v7 we don't need to change to ARM mode; we can write "universal
  46. // assembler" which will assemble to Thumb 2 or ARM code as appropriate. The only thing
  47. // we need to do to make this "universal" assembler mode work is to insert "IT" instructions
  48. // to annotate the conditional instructions. These are ignored in other modes (e.g. v6),
  49. // so they can always be present.
  50. // A note about memory_order_consume. Technically, this architecture allows to avoid
  51. // unnecessary memory barrier after consume load since it supports data dependency ordering.
  52. // However, some compiler optimizations may break a seemingly valid code relying on data
  53. // dependency tracking by injecting bogus branches to aid out of order execution.
  54. // This may happen not only in Boost.Atomic code but also in user's code, which we have no
  55. // control of. See this thread: http://lists.boost.org/Archives/boost/2014/06/213890.php.
  56. // For this reason we promote memory_order_consume to memory_order_acquire.
  57. #if defined(__thumb__) && !defined(__thumb2__)
  58. #define BOOST_ATOMIC_DETAIL_ARM_ASM_START(TMPREG) "adr " #TMPREG ", 8f\n" "bx " #TMPREG "\n" ".arm\n" ".align 4\n" "8:\n"
  59. #define BOOST_ATOMIC_DETAIL_ARM_ASM_END(TMPREG) "adr " #TMPREG ", 9f + 1\n" "bx " #TMPREG "\n" ".thumb\n" ".align 2\n" "9:\n"
  60. #define BOOST_ATOMIC_DETAIL_ARM_ASM_TMPREG_CONSTRAINT(var) "=&l" (var)
  61. #else
  62. // The tmpreg may be wasted in this case, which is non-optimal.
  63. #define BOOST_ATOMIC_DETAIL_ARM_ASM_START(TMPREG)
  64. #define BOOST_ATOMIC_DETAIL_ARM_ASM_END(TMPREG)
  65. #define BOOST_ATOMIC_DETAIL_ARM_ASM_TMPREG_CONSTRAINT(var) "=&r" (var)
  66. #endif
  67. struct gcc_arm_operations_base
  68. {
  69. static BOOST_CONSTEXPR_OR_CONST bool full_cas_based = false;
  70. static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = true;
  71. static BOOST_FORCEINLINE void fence_before(memory_order order) BOOST_NOEXCEPT
  72. {
  73. if ((static_cast< unsigned int >(order) & static_cast< unsigned int >(memory_order_release)) != 0u)
  74. hardware_full_fence();
  75. }
  76. static BOOST_FORCEINLINE void fence_after(memory_order order) BOOST_NOEXCEPT
  77. {
  78. if ((static_cast< unsigned int >(order) & (static_cast< unsigned int >(memory_order_consume) | static_cast< unsigned int >(memory_order_acquire))) != 0u)
  79. hardware_full_fence();
  80. }
  81. static BOOST_FORCEINLINE void fence_after_store(memory_order order) BOOST_NOEXCEPT
  82. {
  83. if (order == memory_order_seq_cst)
  84. hardware_full_fence();
  85. }
  86. static BOOST_FORCEINLINE void hardware_full_fence() BOOST_NOEXCEPT
  87. {
  88. #if defined(BOOST_ATOMIC_DETAIL_ARM_HAS_DMB)
  89. // Older binutils (supposedly, older than 2.21.1) didn't support symbolic or numeric arguments of the "dmb" instruction such as "ish" or "#11".
  90. // As a workaround we have to inject encoded bytes of the instruction. There are two encodings for the instruction: ARM and Thumb. See ARM Architecture Reference Manual, A8.8.43.
  91. // Since we cannot detect binutils version at compile time, we'll have to always use this hack.
  92. __asm__ __volatile__
  93. (
  94. #if defined(__thumb2__)
  95. ".short 0xF3BF, 0x8F5B\n" // dmb ish
  96. #else
  97. ".word 0xF57FF05B\n" // dmb ish
  98. #endif
  99. :
  100. :
  101. : "memory"
  102. );
  103. #else
  104. uint32_t tmp;
  105. __asm__ __volatile__
  106. (
  107. BOOST_ATOMIC_DETAIL_ARM_ASM_START(%0)
  108. "mcr\tp15, 0, r0, c7, c10, 5\n"
  109. BOOST_ATOMIC_DETAIL_ARM_ASM_END(%0)
  110. : "=&l" (tmp)
  111. :
  112. : "memory"
  113. );
  114. #endif
  115. }
  116. };
  117. } // namespace detail
  118. } // namespace atomics
  119. } // namespace boost
  120. #endif // BOOST_ATOMIC_DETAIL_OPS_GCC_ARM_COMMON_HPP_INCLUDED_