You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

305 lines
13KB

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>PowerPC Hardware Transactional Memory Built-in Functions (Using the GNU Compiler Collection (GCC))</title>
  21. <meta name="description" content="PowerPC Hardware Transactional Memory Built-in Functions (Using the GNU Compiler Collection (GCC))">
  22. <meta name="keywords" content="PowerPC Hardware Transactional Memory Built-in Functions (Using the GNU Compiler Collection (GCC))">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Target-Builtins.html#Target-Builtins" rel="up" title="Target Builtins">
  30. <link href="PowerPC-Atomic-Memory-Operation-Functions.html#PowerPC-Atomic-Memory-Operation-Functions" rel="next" title="PowerPC Atomic Memory Operation Functions">
  31. <link href="PowerPC-AltiVec-Built_002din-Functions-Available-on-ISA-3_002e0.html#PowerPC-AltiVec-Built_002din-Functions-Available-on-ISA-3_002e0" rel="prev" title="PowerPC AltiVec Built-in Functions Available on ISA 3.0">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style: oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="PowerPC-Hardware-Transactional-Memory-Built_002din-Functions"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="PowerPC-Atomic-Memory-Operation-Functions.html#PowerPC-Atomic-Memory-Operation-Functions" accesskey="n" rel="next">PowerPC Atomic Memory Operation Functions</a>, Previous: <a href="PowerPC-AltiVec_002fVSX-Built_002din-Functions.html#PowerPC-AltiVec_002fVSX-Built_002din-Functions" accesskey="p" rel="prev">PowerPC AltiVec/VSX Built-in Functions</a>, Up: <a href="Target-Builtins.html#Target-Builtins" accesskey="u" rel="up">Target Builtins</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  65. </div>
  66. <hr>
  67. <a name="PowerPC-Hardware-Transactional-Memory-Built_002din-Functions-1"></a>
  68. <h4 class="subsection">6.60.24 PowerPC Hardware Transactional Memory Built-in Functions</h4>
  69. <p>GCC provides two interfaces for accessing the Hardware Transactional
  70. Memory (HTM) instructions available on some of the PowerPC family
  71. of processors (eg, POWER8). The two interfaces come in a low level
  72. interface, consisting of built-in functions specific to PowerPC and a
  73. higher level interface consisting of inline functions that are common
  74. between PowerPC and S/390.
  75. </p>
  76. <a name="PowerPC-HTM-Low-Level-Built_002din-Functions"></a>
  77. <h4 class="subsubsection">6.60.24.1 PowerPC HTM Low Level Built-in Functions</h4>
  78. <p>The following low level built-in functions are available with
  79. <samp>-mhtm</samp> or <samp>-mcpu=CPU</samp> where CPU is &lsquo;power8&rsquo; or later.
  80. They all generate the machine instruction that is part of the name.
  81. </p>
  82. <p>The HTM builtins (with the exception of <code>__builtin_tbegin</code>) return
  83. the full 4-bit condition register value set by their associated hardware
  84. instruction. The header file <code>htmintrin.h</code> defines some macros that can
  85. be used to decipher the return value. The <code>__builtin_tbegin</code> builtin
  86. returns a simple <code>true</code> or <code>false</code> value depending on whether a transaction was
  87. successfully started or not. The arguments of the builtins match exactly the
  88. type and order of the associated hardware instruction&rsquo;s operands, except for
  89. the <code>__builtin_tcheck</code> builtin, which does not take any input arguments.
  90. Refer to the ISA manual for a description of each instruction&rsquo;s operands.
  91. </p>
  92. <div class="smallexample">
  93. <pre class="smallexample">unsigned int __builtin_tbegin (unsigned int)
  94. unsigned int __builtin_tend (unsigned int)
  95. unsigned int __builtin_tabort (unsigned int)
  96. unsigned int __builtin_tabortdc (unsigned int, unsigned int, unsigned int)
  97. unsigned int __builtin_tabortdci (unsigned int, unsigned int, int)
  98. unsigned int __builtin_tabortwc (unsigned int, unsigned int, unsigned int)
  99. unsigned int __builtin_tabortwci (unsigned int, unsigned int, int)
  100. unsigned int __builtin_tcheck (void)
  101. unsigned int __builtin_treclaim (unsigned int)
  102. unsigned int __builtin_trechkpt (void)
  103. unsigned int __builtin_tsr (unsigned int)
  104. </pre></div>
  105. <p>In addition to the above HTM built-ins, we have added built-ins for
  106. some common extended mnemonics of the HTM instructions:
  107. </p>
  108. <div class="smallexample">
  109. <pre class="smallexample">unsigned int __builtin_tendall (void)
  110. unsigned int __builtin_tresume (void)
  111. unsigned int __builtin_tsuspend (void)
  112. </pre></div>
  113. <p>Note that the semantics of the above HTM builtins are required to mimic
  114. the locking semantics used for critical sections. Builtins that are used
  115. to create a new transaction or restart a suspended transaction must have
  116. lock acquisition like semantics while those builtins that end or suspend a
  117. transaction must have lock release like semantics. Specifically, this must
  118. mimic lock semantics as specified by C++11, for example: Lock acquisition is
  119. as-if an execution of __atomic_exchange_n(&amp;globallock,1,__ATOMIC_ACQUIRE)
  120. that returns 0, and lock release is as-if an execution of
  121. __atomic_store(&amp;globallock,0,__ATOMIC_RELEASE), with globallock being an
  122. implicit implementation-defined lock used for all transactions. The HTM
  123. instructions associated with with the builtins inherently provide the
  124. correct acquisition and release hardware barriers required. However,
  125. the compiler must also be prohibited from moving loads and stores across
  126. the builtins in a way that would violate their semantics. This has been
  127. accomplished by adding memory barriers to the associated HTM instructions
  128. (which is a conservative approach to provide acquire and release semantics).
  129. Earlier versions of the compiler did not treat the HTM instructions as
  130. memory barriers. A <code>__TM_FENCE__</code> macro has been added, which can
  131. be used to determine whether the current compiler treats HTM instructions
  132. as memory barriers or not. This allows the user to explicitly add memory
  133. barriers to their code when using an older version of the compiler.
  134. </p>
  135. <p>The following set of built-in functions are available to gain access
  136. to the HTM specific special purpose registers.
  137. </p>
  138. <div class="smallexample">
  139. <pre class="smallexample">unsigned long __builtin_get_texasr (void)
  140. unsigned long __builtin_get_texasru (void)
  141. unsigned long __builtin_get_tfhar (void)
  142. unsigned long __builtin_get_tfiar (void)
  143. void __builtin_set_texasr (unsigned long);
  144. void __builtin_set_texasru (unsigned long);
  145. void __builtin_set_tfhar (unsigned long);
  146. void __builtin_set_tfiar (unsigned long);
  147. </pre></div>
  148. <p>Example usage of these low level built-in functions may look like:
  149. </p>
  150. <div class="smallexample">
  151. <pre class="smallexample">#include &lt;htmintrin.h&gt;
  152. int num_retries = 10;
  153. while (1)
  154. {
  155. if (__builtin_tbegin (0))
  156. {
  157. /* Transaction State Initiated. */
  158. if (is_locked (lock))
  159. __builtin_tabort (0);
  160. ... transaction code...
  161. __builtin_tend (0);
  162. break;
  163. }
  164. else
  165. {
  166. /* Transaction State Failed. Use locks if the transaction
  167. failure is &quot;persistent&quot; or we've tried too many times. */
  168. if (num_retries-- &lt;= 0
  169. || _TEXASRU_FAILURE_PERSISTENT (__builtin_get_texasru ()))
  170. {
  171. acquire_lock (lock);
  172. ... non transactional fallback path...
  173. release_lock (lock);
  174. break;
  175. }
  176. }
  177. }
  178. </pre></div>
  179. <p>One final built-in function has been added that returns the value of
  180. the 2-bit Transaction State field of the Machine Status Register (MSR)
  181. as stored in <code>CR0</code>.
  182. </p>
  183. <div class="smallexample">
  184. <pre class="smallexample">unsigned long __builtin_ttest (void)
  185. </pre></div>
  186. <p>This built-in can be used to determine the current transaction state
  187. using the following code example:
  188. </p>
  189. <div class="smallexample">
  190. <pre class="smallexample">#include &lt;htmintrin.h&gt;
  191. unsigned char tx_state = _HTM_STATE (__builtin_ttest ());
  192. if (tx_state == _HTM_TRANSACTIONAL)
  193. {
  194. /* Code to use in transactional state. */
  195. }
  196. else if (tx_state == _HTM_NONTRANSACTIONAL)
  197. {
  198. /* Code to use in non-transactional state. */
  199. }
  200. else if (tx_state == _HTM_SUSPENDED)
  201. {
  202. /* Code to use in transaction suspended state. */
  203. }
  204. </pre></div>
  205. <a name="PowerPC-HTM-High-Level-Inline-Functions"></a>
  206. <h4 class="subsubsection">6.60.24.2 PowerPC HTM High Level Inline Functions</h4>
  207. <p>The following high level HTM interface is made available by including
  208. <code>&lt;htmxlintrin.h&gt;</code> and using <samp>-mhtm</samp> or <samp>-mcpu=CPU</samp>
  209. where CPU is &lsquo;power8&rsquo; or later. This interface is common between PowerPC
  210. and S/390, allowing users to write one HTM source implementation that
  211. can be compiled and executed on either system.
  212. </p>
  213. <div class="smallexample">
  214. <pre class="smallexample">long __TM_simple_begin (void)
  215. long __TM_begin (void* const TM_buff)
  216. long __TM_end (void)
  217. void __TM_abort (void)
  218. void __TM_named_abort (unsigned char const code)
  219. void __TM_resume (void)
  220. void __TM_suspend (void)
  221. long __TM_is_user_abort (void* const TM_buff)
  222. long __TM_is_named_user_abort (void* const TM_buff, unsigned char *code)
  223. long __TM_is_illegal (void* const TM_buff)
  224. long __TM_is_footprint_exceeded (void* const TM_buff)
  225. long __TM_nesting_depth (void* const TM_buff)
  226. long __TM_is_nested_too_deep(void* const TM_buff)
  227. long __TM_is_conflict(void* const TM_buff)
  228. long __TM_is_failure_persistent(void* const TM_buff)
  229. long __TM_failure_address(void* const TM_buff)
  230. long long __TM_failure_code(void* const TM_buff)
  231. </pre></div>
  232. <p>Using these common set of HTM inline functions, we can create
  233. a more portable version of the HTM example in the previous
  234. section that will work on either PowerPC or S/390:
  235. </p>
  236. <div class="smallexample">
  237. <pre class="smallexample">#include &lt;htmxlintrin.h&gt;
  238. int num_retries = 10;
  239. TM_buff_type TM_buff;
  240. while (1)
  241. {
  242. if (__TM_begin (TM_buff) == _HTM_TBEGIN_STARTED)
  243. {
  244. /* Transaction State Initiated. */
  245. if (is_locked (lock))
  246. __TM_abort ();
  247. ... transaction code...
  248. __TM_end ();
  249. break;
  250. }
  251. else
  252. {
  253. /* Transaction State Failed. Use locks if the transaction
  254. failure is &quot;persistent&quot; or we've tried too many times. */
  255. if (num_retries-- &lt;= 0
  256. || __TM_is_failure_persistent (TM_buff))
  257. {
  258. acquire_lock (lock);
  259. ... non transactional fallback path...
  260. release_lock (lock);
  261. break;
  262. }
  263. }
  264. }
  265. </pre></div>
  266. <hr>
  267. <div class="header">
  268. <p>
  269. Next: <a href="PowerPC-Atomic-Memory-Operation-Functions.html#PowerPC-Atomic-Memory-Operation-Functions" accesskey="n" rel="next">PowerPC Atomic Memory Operation Functions</a>, Previous: <a href="PowerPC-AltiVec_002fVSX-Built_002din-Functions.html#PowerPC-AltiVec_002fVSX-Built_002din-Functions" accesskey="p" rel="prev">PowerPC AltiVec/VSX Built-in Functions</a>, Up: <a href="Target-Builtins.html#Target-Builtins" accesskey="u" rel="up">Target Builtins</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  270. </div>
  271. </body>
  272. </html>