tsd.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2012 Apple Inc. All rights reserved.
  3. *
  4. * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apple Public Source License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. The rights granted to you under the License
  10. * may not be used to create, or enable the creation or redistribution of,
  11. * unlawful or unlicensed copies of an Apple operating system, or to
  12. * circumvent, violate, or enable the circumvention or violation of, any
  13. * terms of an Apple operating system software license agreement.
  14. *
  15. * Please obtain a copy of the License at
  16. * http://www.opensource.apple.com/apsl/ and read it before using this file.
  17. *
  18. * The Original Code and all software distributed under the License are
  19. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  20. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  21. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  23. * Please see the License for the specific language governing rights and
  24. * limitations under the License.
  25. *
  26. * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  27. */
  28. #ifndef OS_TSD_H
  29. #define OS_TSD_H
  30. /* The low nine slots of the TSD are reserved for libsyscall usage. */
  31. #define __TSD_RESERVED_BASE 0
  32. #define __TSD_RESERVED_MAX 9
  33. #define __TSD_THREAD_SELF 0
  34. #define __TSD_ERRNO 1
  35. #define __TSD_MIG_REPLY 2
  36. #define __TSD_MACH_THREAD_SELF 3
  37. #define __TSD_THREAD_QOS_CLASS 4
  38. #define __TSD_RETURN_TO_KERNEL 5
  39. /* slot 6 is reserved for Windows/WINE compatibility reasons */
  40. #define __TSD_PTR_MUNGE 7
  41. #define __TSD_MACH_SPECIAL_REPLY 8
  42. #define __TSD_SEMAPHORE_CACHE 9
  43. #ifndef __ASSEMBLER__
  44. #include <stdint.h>
  45. #ifdef __arm__
  46. #include <arm/arch.h>
  47. #endif
  48. extern void _thread_set_tsd_base(void *tsd_base);
  49. __attribute__((always_inline))
  50. static __inline__ unsigned int
  51. _os_cpu_number(void)
  52. {
  53. #if defined(__arm__)
  54. uintptr_t p;
  55. __asm__("mrc p15, 0, %[p], c13, c0, 3" : [p] "=&r" (p));
  56. return (unsigned int)(p & 0x3ul);
  57. #elif defined(__arm64__)
  58. uint64_t p;
  59. __asm__("mrs %[p], TPIDRRO_EL0" : [p] "=&r" (p));
  60. return (unsigned int)p & 0x7;
  61. #elif defined(__x86_64__) || defined(__i386__)
  62. struct { uintptr_t p1, p2; } p;
  63. __asm__("sidt %[p]" : [p] "=&m" (p));
  64. return (unsigned int)(p.p1 & 0xfff);
  65. #else
  66. #error _os_cpu_number not implemented on this architecture
  67. #endif
  68. }
  69. #if defined(__i386__) || defined(__x86_64__)
  70. #if defined(__has_attribute)
  71. #if __has_attribute(address_space)
  72. #define OS_GS_RELATIVE __attribute__((address_space(256)))
  73. #endif
  74. #endif
  75. #ifdef OS_GS_RELATIVE
  76. #define _os_tsd_get_base() ((void * OS_GS_RELATIVE *)0)
  77. #else
  78. __attribute__((always_inline))
  79. static __inline__ void*
  80. _os_tsd_get_direct(unsigned long slot)
  81. {
  82. void *ret;
  83. __asm__("mov %%gs:%1, %0" : "=r" (ret) : "m" (*(void **)(slot * sizeof(void *))));
  84. return ret;
  85. }
  86. __attribute__((always_inline))
  87. static __inline__ int
  88. _os_tsd_set_direct(unsigned long slot, void *val)
  89. {
  90. #if defined(__i386__) && defined(__PIC__)
  91. __asm__("movl %1, %%gs:%0" : "=m" (*(void **)(slot * sizeof(void *))) : "rn" (val));
  92. #elif defined(__i386__) && !defined(__PIC__)
  93. __asm__("movl %1, %%gs:%0" : "=m" (*(void **)(slot * sizeof(void *))) : "ri" (val));
  94. #else
  95. __asm__("movq %1, %%gs:%0" : "=m" (*(void **)(slot * sizeof(void *))) : "rn" (val));
  96. #endif
  97. return 0;
  98. }
  99. #endif
  100. #elif defined(__arm__) || defined(__arm64__)
  101. __attribute__((always_inline, pure))
  102. static __inline__ void**
  103. _os_tsd_get_base(void)
  104. {
  105. #if defined(__arm__)
  106. uintptr_t tsd;
  107. __asm__("mrc p15, 0, %0, c13, c0, 3\n"
  108. "bic %0, %0, #0x3\n" : "=r" (tsd));
  109. /* lower 2-bits contain CPU number */
  110. #elif defined(__arm64__)
  111. uint64_t tsd;
  112. __asm__("mrs %0, TPIDRRO_EL0\n"
  113. "bic %0, %0, #0x7\n" : "=r" (tsd));
  114. /* lower 3-bits contain CPU number */
  115. #endif
  116. return (void**)(uintptr_t)tsd;
  117. }
  118. #define _os_tsd_get_base() _os_tsd_get_base()
  119. #else
  120. #error _os_tsd_get_base not implemented on this architecture
  121. #endif
  122. #ifdef _os_tsd_get_base
  123. __attribute__((always_inline))
  124. static __inline__ void*
  125. _os_tsd_get_direct(unsigned long slot)
  126. {
  127. return _os_tsd_get_base()[slot];
  128. }
  129. __attribute__((always_inline))
  130. static __inline__ int
  131. _os_tsd_set_direct(unsigned long slot, void *val)
  132. {
  133. _os_tsd_get_base()[slot] = val;
  134. return 0;
  135. }
  136. #endif
  137. __attribute__((always_inline, pure))
  138. static __inline__ uintptr_t
  139. _os_ptr_munge_token(void)
  140. {
  141. return (uintptr_t)_os_tsd_get_direct(__TSD_PTR_MUNGE);
  142. }
  143. __attribute__((always_inline, pure))
  144. static __inline__ uintptr_t
  145. _os_ptr_munge(uintptr_t ptr)
  146. {
  147. return ptr ^ _os_ptr_munge_token();
  148. }
  149. #define _OS_PTR_MUNGE(_ptr) _os_ptr_munge((uintptr_t)(_ptr))
  150. #define _OS_PTR_UNMUNGE(_ptr) _os_ptr_munge((uintptr_t)(_ptr))
  151. #else // __ASSEMBLER__
  152. #define _OS_TSD_OFFSET(_key) \
  153. ((__POINTER_WIDTH__/__CHAR_BIT__)*_key)
  154. #if defined(__i386__) || defined(__x86_64__)
  155. #define _OS_PTR_MUNGE(_reg) \
  156. xor %gs:_OS_TSD_OFFSET(__TSD_PTR_MUNGE), _reg
  157. #define _OS_PTR_UNMUNGE(_reg) \
  158. _OS_PTR_MUNGE(_reg)
  159. #elif defined(__arm__) || defined(__arm64__)
  160. #if defined(__arm__)
  161. #define _OS_PTR_MUNGE_TOKEN(_reg, _token) \
  162. mrc p15, 0, _reg, c13, c0, 3; \
  163. bic _reg, _reg, #3; \
  164. ldr _token, [ _reg, #_OS_TSD_OFFSET(__TSD_PTR_MUNGE) ]
  165. #elif defined(__arm64__)
  166. #define _OS_PTR_MUNGE_TOKEN(_reg, _token) \
  167. mrs _reg, TPIDRRO_EL0 %% \
  168. and _reg, _reg, #~0x7 %% \
  169. ldr _token, [ _reg, #_OS_TSD_OFFSET(__TSD_PTR_MUNGE) ]
  170. #endif // defined(__arm64__)
  171. #define _OS_PTR_MUNGE(_regdest, _regsrc, _token) \
  172. eor _regdest, _regsrc, _token
  173. #define _OS_PTR_UNMUNGE(_regdest, _regsrc, _token) \
  174. _OS_PTR_MUNGE(_regdest, _regsrc, _token)
  175. #endif // defined(__arm__) || defined(__arm64__)
  176. #endif // __ASSEMBLER__
  177. #endif // OS_TSD_H