gcc_itsx.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #if !defined(__TBB_machine_H) || defined(__TBB_machine_gcc_itsx_H)
  14. #error Do not #include this internal file directly; use public TBB headers instead.
  15. #endif
  16. #define __TBB_machine_gcc_itsx_H
  17. #define __TBB_OP_XACQUIRE 0xF2
  18. #define __TBB_OP_XRELEASE 0xF3
  19. #define __TBB_OP_LOCK 0xF0
  20. #define __TBB_STRINGIZE_INTERNAL(arg) #arg
  21. #define __TBB_STRINGIZE(arg) __TBB_STRINGIZE_INTERNAL(arg)
  22. #ifdef __TBB_x86_64
  23. #define __TBB_r_out "=r"
  24. #else
  25. #define __TBB_r_out "=q"
  26. #endif
  27. inline static uint8_t __TBB_machine_try_lock_elided( volatile uint8_t* lk )
  28. {
  29. uint8_t value = 1;
  30. __asm__ volatile (".byte " __TBB_STRINGIZE(__TBB_OP_XACQUIRE)"; lock; xchgb %0, %1;"
  31. : __TBB_r_out(value), "=m"(*lk) : "0"(value), "m"(*lk) : "memory" );
  32. return uint8_t(value^1);
  33. }
  34. inline static void __TBB_machine_try_lock_elided_cancel()
  35. {
  36. // 'pause' instruction aborts HLE/RTM transactions
  37. __asm__ volatile ("pause\n" : : : "memory" );
  38. }
  39. inline static void __TBB_machine_unlock_elided( volatile uint8_t* lk )
  40. {
  41. __asm__ volatile (".byte " __TBB_STRINGIZE(__TBB_OP_XRELEASE)"; movb $0, %0"
  42. : "=m"(*lk) : "m"(*lk) : "memory" );
  43. }
  44. #if __TBB_TSX_INTRINSICS_PRESENT
  45. #include <immintrin.h>
  46. #define __TBB_machine_is_in_transaction _xtest
  47. #define __TBB_machine_begin_transaction _xbegin
  48. #define __TBB_machine_end_transaction _xend
  49. #define __TBB_machine_transaction_conflict_abort() _xabort(0xff)
  50. #else
  51. /*!
  52. * Check if the instruction is executed in a transaction or not
  53. */
  54. inline static bool __TBB_machine_is_in_transaction()
  55. {
  56. int8_t res = 0;
  57. #if __TBB_x86_32
  58. __asm__ volatile (".byte 0x0F; .byte 0x01; .byte 0xD6;\n"
  59. "setz %0" : "=q"(res) : : "memory" );
  60. #else
  61. __asm__ volatile (".byte 0x0F; .byte 0x01; .byte 0xD6;\n"
  62. "setz %0" : "=r"(res) : : "memory" );
  63. #endif
  64. return res==0;
  65. }
  66. /*!
  67. * Enter speculative execution mode.
  68. * @return -1 on success
  69. * abort cause ( or 0 ) on abort
  70. */
  71. inline static uint32_t __TBB_machine_begin_transaction()
  72. {
  73. uint32_t res = ~uint32_t(0); // success value
  74. __asm__ volatile ("1: .byte 0xC7; .byte 0xF8;\n" // XBEGIN <abort-offset>
  75. " .long 2f-1b-6\n" // 2f-1b == difference in addresses of start
  76. // of XBEGIN and the MOVL
  77. // 2f - 1b - 6 == that difference minus the size of the
  78. // XBEGIN instruction. This is the abort offset to
  79. // 2: below.
  80. " jmp 3f\n" // success (leave -1 in res)
  81. "2: movl %%eax,%0\n" // store failure code in res
  82. "3:"
  83. :"=r"(res):"0"(res):"memory","%eax");
  84. return res;
  85. }
  86. /*!
  87. * Attempt to commit/end transaction
  88. */
  89. inline static void __TBB_machine_end_transaction()
  90. {
  91. __asm__ volatile (".byte 0x0F; .byte 0x01; .byte 0xD5" :::"memory"); // XEND
  92. }
  93. /*
  94. * aborts with code 0xFF (lock already held)
  95. */
  96. inline static void __TBB_machine_transaction_conflict_abort()
  97. {
  98. __asm__ volatile (".byte 0xC6; .byte 0xF8; .byte 0xFF" :::"memory");
  99. }
  100. #endif /* __TBB_TSX_INTRINSICS_PRESENT */