target.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Copyright (C) 2012-2015 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Transactional Memory Library (libitm).
  4. Libitm is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #ifdef HAVE_SYS_AUXV_H
  20. #include <sys/auxv.h>
  21. #endif
  22. namespace GTM HIDDEN {
  23. typedef int v128 __attribute__((vector_size(16), may_alias, aligned(16)));
  24. typedef struct gtm_jmpbuf
  25. {
  26. #if defined(__ALTIVEC__) || defined(__VSX__)
  27. v128 vr[12]; /* vr20-vr31 */
  28. unsigned long long vscr; /* long long for padding only */
  29. #endif
  30. #ifndef _SOFT_FLOAT
  31. double fr[18]; /* f14-f31 */
  32. double fpscr;
  33. #endif
  34. unsigned long gr[18]; /* r14-r31 */
  35. void *cfa;
  36. unsigned long pc;
  37. unsigned long toc; /* r2 on aix, r13 on darwin */
  38. unsigned long cr;
  39. } gtm_jmpbuf;
  40. /* The size of one line in hardware caches (in bytes). */
  41. #if defined (__powerpc64__) || defined (__ppc64__)
  42. # define HW_CACHELINE_SIZE 128
  43. #else
  44. # define HW_CACHELINE_SIZE 32
  45. #endif
  46. static inline void
  47. cpu_relax (void)
  48. {
  49. __asm volatile ("" : : : "memory");
  50. }
  51. // Use HTM if it is supported by the system.
  52. // See gtm_thread::begin_transaction for how these functions are used.
  53. #if defined (__linux__) \
  54. && defined (HAVE_AS_HTM) \
  55. && defined (HAVE_GETAUXVAL) \
  56. && defined (AT_HWCAP2) \
  57. && defined (PPC_FEATURE2_HAS_HTM)
  58. #include <htmintrin.h>
  59. #define USE_HTM_FASTPATH
  60. #define _TBEGIN_STARTED 0
  61. #define _TBEGIN_INDETERMINATE 1
  62. #define _TBEGIN_PERSISTENT 2
  63. /* Number of retries for transient failures. */
  64. #define _HTM_RETRIES 10
  65. static inline bool
  66. htm_available (void)
  67. {
  68. return (getauxval (AT_HWCAP2) & PPC_FEATURE2_HAS_HTM) ? true : false;
  69. }
  70. static inline uint32_t
  71. htm_init (void)
  72. {
  73. // Maximum number of times we try to execute a transaction
  74. // as a HW transaction.
  75. return htm_available () ? _HTM_RETRIES : 0;
  76. }
  77. static inline uint32_t
  78. htm_begin (void)
  79. {
  80. if (__builtin_expect (__builtin_tbegin (0), 1))
  81. return _TBEGIN_STARTED;
  82. if (_TEXASRU_FAILURE_PERSISTENT (__builtin_get_texasru ()))
  83. return _TBEGIN_PERSISTENT;
  84. return _TBEGIN_INDETERMINATE;
  85. }
  86. static inline bool
  87. htm_begin_success (uint32_t begin_ret)
  88. {
  89. return begin_ret == _TBEGIN_STARTED;
  90. }
  91. static inline void
  92. htm_commit (void)
  93. {
  94. __builtin_tend (0);
  95. }
  96. static inline void
  97. htm_abort (void)
  98. {
  99. __builtin_tabort (0);
  100. }
  101. static inline bool
  102. htm_abort_should_retry (uint32_t begin_ret)
  103. {
  104. return begin_ret != _TBEGIN_PERSISTENT;
  105. }
  106. /* Returns true iff a hardware transaction is currently being executed. */
  107. static inline bool
  108. htm_transaction_active (void)
  109. {
  110. return (_HTM_STATE (__builtin_ttest ()) == _HTM_TRANSACTIONAL);
  111. }
  112. #endif
  113. } // namespace GTM