1 /*===---- htmxlintrin.h - XL compiler HTM execution intrinsics-------------===*\
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  *
21 \*===----------------------------------------------------------------------===*/
22 
23 #ifndef __HTMXLINTRIN_H
24 #define __HTMXLINTRIN_H
25 
26 #ifndef __HTM__
27 #error "HTM instruction set not enabled"
28 #endif
29 
30 #include <htmintrin.h>
31 
32 #ifdef __powerpc__
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #define _TEXASR_PTR(TM_BUF) ((texasr_t *)((char *)(TM_BUF) + 0))
39 #define _TEXASRU_PTR(TM_BUF) ((texasru_t *)((char *)(TM_BUF) + 0))
40 #define _TEXASRL_PTR(TM_BUF) ((texasrl_t *)((char *)(TM_BUF) + 4))
41 #define _TFIAR_PTR(TM_BUF) ((tfiar_t *)((char *)(TM_BUF) + 8))
42 
43 typedef char TM_buff_type[16];
44 
45 /* This macro can be used to determine whether a transaction was successfully
46    started from the __TM_begin() and __TM_simple_begin() intrinsic functions
47    below.  */
48 #define _HTM_TBEGIN_STARTED     1
49 
50 extern __inline long
51 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
__TM_simple_begin(void)52 __TM_simple_begin (void)
53 {
54   if (__builtin_expect (__builtin_tbegin (0), 1))
55     return _HTM_TBEGIN_STARTED;
56   return 0;
57 }
58 
59 extern __inline long
60 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
__TM_begin(void * const __TM_buff)61 __TM_begin (void* const __TM_buff)
62 {
63   *_TEXASRL_PTR (__TM_buff) = 0;
64   if (__builtin_expect (__builtin_tbegin (0), 1))
65     return _HTM_TBEGIN_STARTED;
66 #ifdef __powerpc64__
67   *_TEXASR_PTR (__TM_buff) = __builtin_get_texasr ();
68 #else
69   *_TEXASRU_PTR (__TM_buff) = __builtin_get_texasru ();
70   *_TEXASRL_PTR (__TM_buff) = __builtin_get_texasr ();
71 #endif
72   *_TFIAR_PTR (__TM_buff) = __builtin_get_tfiar ();
73   return 0;
74 }
75 
76 extern __inline long
77 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
__TM_end(void)78 __TM_end (void)
79 {
80   if (__builtin_expect (__builtin_tend (0), 1))
81     return 1;
82   return 0;
83 }
84 
85 extern __inline void
86 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
__TM_abort(void)87 __TM_abort (void)
88 {
89   __builtin_tabort (0);
90 }
91 
92 extern __inline void
93 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
__TM_named_abort(unsigned char const __code)94 __TM_named_abort (unsigned char const __code)
95 {
96   __builtin_tabort (__code);
97 }
98 
99 extern __inline void
100 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
__TM_resume(void)101 __TM_resume (void)
102 {
103   __builtin_tresume ();
104 }
105 
106 extern __inline void
107 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
__TM_suspend(void)108 __TM_suspend (void)
109 {
110   __builtin_tsuspend ();
111 }
112 
113 extern __inline long
114 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
__TM_is_user_abort(void * const __TM_buff)115 __TM_is_user_abort (void* const __TM_buff)
116 {
117   texasru_t texasru = *_TEXASRU_PTR (__TM_buff);
118   return _TEXASRU_ABORT (texasru);
119 }
120 
121 extern __inline long
122 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
__TM_is_named_user_abort(void * const __TM_buff,unsigned char * __code)123 __TM_is_named_user_abort (void* const __TM_buff, unsigned char *__code)
124 {
125   texasru_t texasru = *_TEXASRU_PTR (__TM_buff);
126 
127   *__code = _TEXASRU_FAILURE_CODE (texasru);
128   return _TEXASRU_ABORT (texasru);
129 }
130 
131 extern __inline long
132 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
__TM_is_illegal(void * const __TM_buff)133 __TM_is_illegal (void* const __TM_buff)
134 {
135   texasru_t texasru = *_TEXASRU_PTR (__TM_buff);
136   return _TEXASRU_DISALLOWED (texasru);
137 }
138 
139 extern __inline long
140 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
__TM_is_footprint_exceeded(void * const __TM_buff)141 __TM_is_footprint_exceeded (void* const __TM_buff)
142 {
143   texasru_t texasru = *_TEXASRU_PTR (__TM_buff);
144   return _TEXASRU_FOOTPRINT_OVERFLOW (texasru);
145 }
146 
147 extern __inline long
148 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
__TM_nesting_depth(void * const __TM_buff)149 __TM_nesting_depth (void* const __TM_buff)
150 {
151   texasrl_t texasrl;
152 
153   if (_HTM_STATE (__builtin_ttest ()) == _HTM_NONTRANSACTIONAL)
154     {
155       texasrl = *_TEXASRL_PTR (__TM_buff);
156       if (!_TEXASR_FAILURE_SUMMARY (texasrl))
157         texasrl = 0;
158     }
159   else
160     texasrl = (texasrl_t) __builtin_get_texasr ();
161 
162   return _TEXASR_TRANSACTION_LEVEL (texasrl);
163 }
164 
165 extern __inline long
166 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
__TM_is_nested_too_deep(void * const __TM_buff)167 __TM_is_nested_too_deep(void* const __TM_buff)
168 {
169   texasru_t texasru = *_TEXASRU_PTR (__TM_buff);
170   return _TEXASRU_NESTING_OVERFLOW (texasru);
171 }
172 
173 extern __inline long
174 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
__TM_is_conflict(void * const __TM_buff)175 __TM_is_conflict(void* const __TM_buff)
176 {
177   texasru_t texasru = *_TEXASRU_PTR (__TM_buff);
178   /* Return TEXASR bits 11 (Self-Induced Conflict) through
179      14 (Translation Invalidation Conflict).  */
180   return (_TEXASRU_EXTRACT_BITS (texasru, 14, 4)) ? 1 : 0;
181 }
182 
183 extern __inline long
184 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
__TM_is_failure_persistent(void * const __TM_buff)185 __TM_is_failure_persistent(void* const __TM_buff)
186 {
187   texasru_t texasru = *_TEXASRU_PTR (__TM_buff);
188   return _TEXASRU_FAILURE_PERSISTENT (texasru);
189 }
190 
191 extern __inline long
192 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
__TM_failure_address(void * const __TM_buff)193 __TM_failure_address(void* const __TM_buff)
194 {
195   return *_TFIAR_PTR (__TM_buff);
196 }
197 
198 extern __inline long long
199 __attribute__ ((__gnu_inline__, __always_inline__, __artificial__))
__TM_failure_code(void * const __TM_buff)200 __TM_failure_code(void* const __TM_buff)
201 {
202   return *_TEXASR_PTR (__TM_buff);
203 }
204 
205 #ifdef __cplusplus
206 }
207 #endif
208 
209 #endif /* __powerpc__ */
210 
211 #ifdef __s390__
212 
213 #include <stdint.h>
214 
215 /* These intrinsics are being made available for compatibility with
216    the IBM XL compiler.  For documentation please see the "z/OS XL
217    C/C++ Programming Guide" publicly available on the web.  */
218 
219 static __inline long __attribute__((__always_inline__, __nodebug__))
__TM_simple_begin()220 __TM_simple_begin ()
221 {
222   return __builtin_tbegin_nofloat (0);
223 }
224 
225 static __inline long __attribute__((__always_inline__, __nodebug__))
__TM_begin(void * const __tdb)226 __TM_begin (void* const __tdb)
227 {
228   return __builtin_tbegin_nofloat (__tdb);
229 }
230 
231 static __inline long __attribute__((__always_inline__, __nodebug__))
__TM_end()232 __TM_end ()
233 {
234   return __builtin_tend ();
235 }
236 
237 static __inline void __attribute__((__always_inline__))
__TM_abort()238 __TM_abort ()
239 {
240   return __builtin_tabort (_HTM_FIRST_USER_ABORT_CODE);
241 }
242 
243 static __inline void __attribute__((__always_inline__, __nodebug__))
__TM_named_abort(unsigned char const __code)244 __TM_named_abort (unsigned char const __code)
245 {
246   return __builtin_tabort ((int)_HTM_FIRST_USER_ABORT_CODE + __code);
247 }
248 
249 static __inline void __attribute__((__always_inline__, __nodebug__))
__TM_non_transactional_store(void * const __addr,long long const __value)250 __TM_non_transactional_store (void* const __addr, long long const __value)
251 {
252   __builtin_non_tx_store ((uint64_t*)__addr, (uint64_t)__value);
253 }
254 
255 static __inline long __attribute__((__always_inline__, __nodebug__))
__TM_nesting_depth(void * const __tdb_ptr)256 __TM_nesting_depth (void* const __tdb_ptr)
257 {
258   int depth = __builtin_tx_nesting_depth ();
259   struct __htm_tdb *tdb = (struct __htm_tdb*)__tdb_ptr;
260 
261   if (depth != 0)
262     return depth;
263 
264   if (tdb->format != 1)
265     return 0;
266   return tdb->nesting_depth;
267 }
268 
269 /* Transaction failure diagnostics */
270 
271 static __inline long __attribute__((__always_inline__, __nodebug__))
__TM_is_user_abort(void * const __tdb_ptr)272 __TM_is_user_abort (void* const __tdb_ptr)
273 {
274   struct __htm_tdb *tdb = (struct __htm_tdb*)__tdb_ptr;
275 
276   if (tdb->format != 1)
277     return 0;
278 
279   return !!(tdb->abort_code >= _HTM_FIRST_USER_ABORT_CODE);
280 }
281 
282 static __inline long __attribute__((__always_inline__, __nodebug__))
__TM_is_named_user_abort(void * const __tdb_ptr,unsigned char * __code)283 __TM_is_named_user_abort (void* const __tdb_ptr, unsigned char* __code)
284 {
285   struct __htm_tdb *tdb = (struct __htm_tdb*)__tdb_ptr;
286 
287   if (tdb->format != 1)
288     return 0;
289 
290   if (tdb->abort_code >= _HTM_FIRST_USER_ABORT_CODE)
291     {
292       *__code = tdb->abort_code - _HTM_FIRST_USER_ABORT_CODE;
293       return 1;
294     }
295   return 0;
296 }
297 
298 static __inline long __attribute__((__always_inline__, __nodebug__))
__TM_is_illegal(void * const __tdb_ptr)299 __TM_is_illegal (void* const __tdb_ptr)
300 {
301   struct __htm_tdb *tdb = (struct __htm_tdb*)__tdb_ptr;
302 
303   return (tdb->format == 1
304 	  && (tdb->abort_code == 4 /* unfiltered program interruption */
305 	      || tdb->abort_code == 11 /* restricted instruction */));
306 }
307 
308 static __inline long __attribute__((__always_inline__, __nodebug__))
__TM_is_footprint_exceeded(void * const __tdb_ptr)309 __TM_is_footprint_exceeded (void* const __tdb_ptr)
310 {
311   struct __htm_tdb *tdb = (struct __htm_tdb*)__tdb_ptr;
312 
313   return (tdb->format == 1
314 	  && (tdb->abort_code == 7 /* fetch overflow */
315 	      || tdb->abort_code == 8 /* store overflow */));
316 }
317 
318 static __inline long __attribute__((__always_inline__, __nodebug__))
__TM_is_nested_too_deep(void * const __tdb_ptr)319 __TM_is_nested_too_deep (void* const __tdb_ptr)
320 {
321   struct __htm_tdb *tdb = (struct __htm_tdb*)__tdb_ptr;
322 
323   return tdb->format == 1 && tdb->abort_code == 13; /* depth exceeded */
324 }
325 
326 static __inline long __attribute__((__always_inline__, __nodebug__))
__TM_is_conflict(void * const __tdb_ptr)327 __TM_is_conflict (void* const __tdb_ptr)
328 {
329   struct __htm_tdb *tdb = (struct __htm_tdb*)__tdb_ptr;
330 
331   return (tdb->format == 1
332 	  && (tdb->abort_code == 9 /* fetch conflict */
333 	      || tdb->abort_code == 10 /* store conflict */));
334 }
335 
336 static __inline long __attribute__((__always_inline__, __nodebug__))
__TM_is_failure_persistent(long const __result)337 __TM_is_failure_persistent (long const __result)
338 {
339   return __result == _HTM_TBEGIN_PERSISTENT;
340 }
341 
342 static __inline long __attribute__((__always_inline__, __nodebug__))
__TM_failure_address(void * const __tdb_ptr)343 __TM_failure_address (void* const __tdb_ptr)
344 {
345   struct __htm_tdb *tdb = (struct __htm_tdb*)__tdb_ptr;
346   return tdb->atia;
347 }
348 
349 static __inline long __attribute__((__always_inline__, __nodebug__))
__TM_failure_code(void * const __tdb_ptr)350 __TM_failure_code (void* const __tdb_ptr)
351 {
352   struct __htm_tdb *tdb = (struct __htm_tdb*)__tdb_ptr;
353 
354   return tdb->abort_code;
355 }
356 
357 #endif /* __s390__ */
358 
359 #endif /* __HTMXLINTRIN_H  */
360