tls.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* Copyright (C) 2008-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. #ifndef LIBITM_X86_TLS_H
  20. #define LIBITM_X86_TLS_H 1
  21. #if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 10)
  22. /* Use slots in the TCB head rather than __thread lookups.
  23. GLIBC has reserved words 10 through 13 for TM. */
  24. #define HAVE_ARCH_GTM_THREAD 1
  25. #define HAVE_ARCH_GTM_THREAD_DISP 1
  26. #endif
  27. #include "config/generic/tls.h"
  28. #if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 10)
  29. namespace GTM HIDDEN {
  30. #ifdef __x86_64__
  31. #ifdef __LP64__
  32. # define SEG_READ(OFS) "movq\t%%fs:(" #OFS "*8),%0"
  33. # define SEG_WRITE(OFS) "movq\t%0,%%fs:(" #OFS "*8)"
  34. # define SEG_DECODE_READ(OFS) SEG_READ(OFS) "\n\t" \
  35. "rorq\t$17,%0\n\t" \
  36. "xorq\t%%fs:48,%0"
  37. # define SEG_ENCODE_WRITE(OFS) "xorq\t%%fs:48,%0\n\t" \
  38. "rolq\t$17,%0\n\t" \
  39. SEG_WRITE(OFS)
  40. #else
  41. // For X32.
  42. # define SEG_READ(OFS) "movl\t%%fs:(" #OFS "*4),%0"
  43. # define SEG_WRITE(OFS) "movl\t%0,%%fs:(" #OFS "*4)"
  44. # define SEG_DECODE_READ(OFS) SEG_READ(OFS) "\n\t" \
  45. "rorl\t$9,%0\n\t" \
  46. "xorl\t%%fs:24,%0"
  47. # define SEG_ENCODE_WRITE(OFS) "xorl\t%%fs:24,%0\n\t" \
  48. "roll\t$9,%0\n\t" \
  49. SEG_WRITE(OFS)
  50. #endif
  51. #else
  52. # define SEG_READ(OFS) "movl\t%%gs:(" #OFS "*4),%0"
  53. # define SEG_WRITE(OFS) "movl\t%0,%%gs:(" #OFS "*4)"
  54. # define SEG_DECODE_READ(OFS) SEG_READ(OFS) "\n\t" \
  55. "rorl\t$9,%0\n\t" \
  56. "xorl\t%%gs:24,%0"
  57. # define SEG_ENCODE_WRITE(OFS) "xorl\t%%gs:24,%0\n\t" \
  58. "roll\t$9,%0\n\t" \
  59. SEG_WRITE(OFS)
  60. #endif
  61. static inline struct gtm_thread *gtm_thr(void)
  62. {
  63. struct gtm_thread *r;
  64. asm volatile (SEG_READ(10) : "=r"(r));
  65. return r;
  66. }
  67. static inline void set_gtm_thr(struct gtm_thread *x)
  68. {
  69. asm volatile (SEG_WRITE(10) : : "r"(x));
  70. }
  71. static inline struct abi_dispatch *abi_disp(void)
  72. {
  73. struct abi_dispatch *r;
  74. asm volatile (SEG_DECODE_READ(11) : "=r"(r));
  75. return r;
  76. }
  77. static inline void set_abi_disp(struct abi_dispatch *x)
  78. {
  79. void *scratch;
  80. asm volatile (SEG_ENCODE_WRITE(11) : "=r"(scratch) : "0"(x));
  81. }
  82. #undef SEG_READ
  83. #undef SEG_WRITE
  84. #undef SEG_DECODE_READ
  85. #undef SEG_ENCODE_WRITE
  86. } // namespace GTM
  87. #endif /* >= GLIBC 2.10 */
  88. #endif // LIBITM_X86_TLS_H