BRE12
gcc_itsx.h
1 /*
2  Copyright 2005-2016 Intel Corporation. All Rights Reserved.
3 
4  This file is part of Threading Building Blocks. Threading Building Blocks is free software;
5  you can redistribute it and/or modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation. Threading Building Blocks is
7  distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9  See the GNU General Public License for more details. You should have received a copy of
10  the GNU General Public License along with Threading Building Blocks; if not, write to the
11  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12 
13  As a special exception, you may use this file as part of a free software library without
14  restriction. Specifically, if other files instantiate templates or use macros or inline
15  functions from this file, or you compile this file and link it with other files to produce
16  an executable, this file does not by itself cause the resulting executable to be covered
17  by the GNU General Public License. This exception does not however invalidate any other
18  reasons why the executable file might be covered by the GNU General Public License.
19 */
20 
21 #if !defined(__TBB_machine_H) || defined(__TBB_machine_gcc_itsx_H)
22 #error Do not #include this internal file directly; use public TBB headers instead.
23 #endif
24 
25 #define __TBB_machine_gcc_itsx_H
26 
27 #define __TBB_OP_XACQUIRE 0xF2
28 #define __TBB_OP_XRELEASE 0xF3
29 #define __TBB_OP_LOCK 0xF0
30 
31 #define __TBB_STRINGIZE_INTERNAL(arg) #arg
32 #define __TBB_STRINGIZE(arg) __TBB_STRINGIZE_INTERNAL(arg)
33 
34 #ifdef __TBB_x86_64
35 #define __TBB_r_out "=r"
36 #else
37 #define __TBB_r_out "=q"
38 #endif
39 
40 inline static uint8_t __TBB_machine_try_lock_elided( volatile uint8_t* lk )
41 {
42  uint8_t value = 1;
43  __asm__ volatile (".byte " __TBB_STRINGIZE(__TBB_OP_XACQUIRE)"; lock; xchgb %0, %1;"
44  : __TBB_r_out(value), "=m"(*lk) : "0"(value), "m"(*lk) : "memory" );
45  return uint8_t(value^1);
46 }
47 
48 inline static void __TBB_machine_try_lock_elided_cancel()
49 {
50  // 'pause' instruction aborts HLE/RTM transactions
51  __asm__ volatile ("pause\n" : : : "memory" );
52 }
53 
54 inline static void __TBB_machine_unlock_elided( volatile uint8_t* lk )
55 {
56  __asm__ volatile (".byte " __TBB_STRINGIZE(__TBB_OP_XRELEASE)"; movb $0, %0"
57  : "=m"(*lk) : "m"(*lk) : "memory" );
58 }
59 
60 #if __TBB_TSX_INTRINSICS_PRESENT
61 #include <immintrin.h>
62 
63 #define __TBB_machine_is_in_transaction _xtest
64 #define __TBB_machine_begin_transaction _xbegin
65 #define __TBB_machine_end_transaction _xend
66 #define __TBB_machine_transaction_conflict_abort() _xabort(0xff)
67 
68 #else
69 
73 inline static bool __TBB_machine_is_in_transaction()
74 {
75  int8_t res = 0;
76 #if __TBB_x86_32
77  __asm__ volatile (".byte 0x0F; .byte 0x01; .byte 0xD6;\n"
78  "setz %0" : "=q"(res) : : "memory" );
79 #else
80  __asm__ volatile (".byte 0x0F; .byte 0x01; .byte 0xD6;\n"
81  "setz %0" : "=r"(res) : : "memory" );
82 #endif
83  return res==0;
84 }
85 
91 inline static uint32_t __TBB_machine_begin_transaction()
92 {
93  uint32_t res = ~uint32_t(0); // success value
94  __asm__ volatile ("1: .byte 0xC7; .byte 0xF8;\n" // XBEGIN <abort-offset>
95  " .long 2f-1b-6\n" // 2f-1b == difference in addresses of start
96  // of XBEGIN and the MOVL
97  // 2f - 1b - 6 == that difference minus the size of the
98  // XBEGIN instruction. This is the abort offset to
99  // 2: below.
100  " jmp 3f\n" // success (leave -1 in res)
101  "2: movl %%eax,%0\n" // store failure code in res
102  "3:"
103  :"=r"(res):"0"(res):"memory","%eax");
104  return res;
105 }
106 
110 inline static void __TBB_machine_end_transaction()
111 {
112  __asm__ volatile (".byte 0x0F; .byte 0x01; .byte 0xD5" :::"memory"); // XEND
113 }
114 
115 /*
116  * aborts with code 0xFF (lock already held)
117  */
118 inline static void __TBB_machine_transaction_conflict_abort()
119 {
120  __asm__ volatile (".byte 0xC6; .byte 0xF8; .byte 0xFF" :::"memory");
121 }
122 
123 #endif /* __TBB_TSX_INTRINSICS_PRESENT */