useldt.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* Special definitions for ix86 machine using segment register based
  2. thread descriptor.
  3. Copyright (C) 1998, 2000, 2001, 2002 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Ulrich Drepper <drepper@cygnus.com>.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public License as
  8. published by the Free Software Foundation; either version 2.1 of the
  9. License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; see the file COPYING.LIB. If not,
  16. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. Boston, MA 02111-1307, USA. */
  18. #ifndef __ASSEMBLER__
  19. #include <stddef.h> /* For offsetof. */
  20. #include <stdlib.h> /* For abort(). */
  21. /* We don't want to include the kernel header. So duplicate the
  22. information. */
  23. /* Structure passed on `modify_ldt' call. */
  24. struct modify_ldt_ldt_s
  25. {
  26. unsigned int entry_number;
  27. unsigned long int base_addr;
  28. unsigned int limit;
  29. unsigned int seg_32bit:1;
  30. unsigned int contents:2;
  31. unsigned int read_exec_only:1;
  32. unsigned int limit_in_pages:1;
  33. unsigned int seg_not_present:1;
  34. unsigned int useable:1;
  35. unsigned int empty:25;
  36. };
  37. /* System call to set LDT entry. */
  38. extern int __modify_ldt (int, struct modify_ldt_ldt_s *, size_t);
  39. /* Return the thread descriptor for the current thread.
  40. The contained asm must *not* be marked volatile since otherwise
  41. assignments like
  42. pthread_descr self = thread_self();
  43. do not get optimized away. */
  44. #define THREAD_SELF \
  45. ({ \
  46. register pthread_descr __self; \
  47. __asm__ ("movl %%gs:%c1,%0" : "=r" (__self) \
  48. : "i" (offsetof (struct _pthread_descr_struct, \
  49. p_header.data.self))); \
  50. __self; \
  51. })
  52. /* Initialize the thread-unique value. Two possible ways to do it. */
  53. #define DO_MODIFY_LDT(descr, nr) \
  54. ({ \
  55. struct modify_ldt_ldt_s ldt_entry = \
  56. { nr, (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
  57. 1, 0, 0, 1, 0, 1, 0 }; \
  58. if (__modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0) \
  59. abort (); \
  60. asm ("movw %w0, %%gs" : : "q" (nr * 8 + 7)); \
  61. })
  62. #ifdef __PIC__
  63. # define USETLS_EBX_ARG "r"
  64. # define USETLS_LOAD_EBX "xchgl %1, %%ebx\n\t"
  65. #else
  66. # define USETLS_EBX_ARG "b"
  67. # define USETLS_LOAD_EBX
  68. #endif
  69. /* When using the new set_thread_area call, we don't need to change %gs
  70. because we inherited the value set up in the main thread by TLS setup.
  71. We need to extract that value and set up the same segment in this
  72. thread. */
  73. #if USE_TLS
  74. # define DO_SET_THREAD_AREA_REUSE(nr) 1
  75. #else
  76. /* Without TLS, we do the initialization of the main thread, where NR == 0. */
  77. # define DO_SET_THREAD_AREA_REUSE(nr) (!__builtin_constant_p (nr) || (nr))
  78. #endif
  79. #define DO_SET_THREAD_AREA(descr, nr) \
  80. ({ \
  81. int __gs; \
  82. if (DO_SET_THREAD_AREA_REUSE (nr)) \
  83. { \
  84. asm ("movw %%gs, %w0" : "=q" (__gs)); \
  85. struct modify_ldt_ldt_s ldt_entry = \
  86. { (__gs & 0xffff) >> 3, \
  87. (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
  88. 1, 0, 0, 1, 0, 1, 0 }; \
  89. \
  90. int __result; \
  91. __asm (USETLS_LOAD_EBX \
  92. "movl %2, %%eax\n\t" \
  93. "int $0x80\n\t" \
  94. USETLS_LOAD_EBX \
  95. : "=&a" (__result) \
  96. : USETLS_EBX_ARG (&ldt_entry), "i" (__NR_set_thread_area), \
  97. "m" (ldt_entry) \
  98. : "memory"); \
  99. if (__result == 0) \
  100. asm ("movw %w0, %%gs" :: "q" (__gs)); \
  101. else \
  102. __gs = -1; \
  103. } \
  104. else \
  105. { \
  106. struct modify_ldt_ldt_s ldt_entry = \
  107. { -1, \
  108. (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
  109. 1, 0, 0, 1, 0, 1, 0 }; \
  110. int __result; \
  111. __asm (USETLS_LOAD_EBX \
  112. "movl %2, %%eax\n\t" \
  113. "int $0x80\n\t" \
  114. USETLS_LOAD_EBX \
  115. : "=&a" (__result) \
  116. : USETLS_EBX_ARG (&ldt_entry), "i" (__NR_set_thread_area), \
  117. "m" (ldt_entry) \
  118. : "memory"); \
  119. if (__result == 0) \
  120. { \
  121. __gs = (ldt_entry.entry_number << 3) + 3; \
  122. asm ("movw %w0, %%gs" : : "q" (__gs)); \
  123. } \
  124. else \
  125. __gs = -1; \
  126. } \
  127. __gs; \
  128. })
  129. #if defined __ASSUME_SET_THREAD_AREA_SYSCALL
  130. # define INIT_THREAD_SELF(descr, nr) DO_SET_THREAD_AREA (descr, nr)
  131. #elif defined __NR_set_thread_area
  132. # define INIT_THREAD_SELF(descr, nr) \
  133. ({ \
  134. if (__builtin_expect (__have_no_set_thread_area, 0) \
  135. || (DO_SET_THREAD_AREA (descr, DO_SET_THREAD_AREA_REUSE (nr)) == -1 \
  136. && (__have_no_set_thread_area = 1))) \
  137. DO_MODIFY_LDT (descr, nr); \
  138. })
  139. /* Defined in pspinlock.c. */
  140. extern int __have_no_set_thread_area;
  141. #else
  142. # define INIT_THREAD_SELF(descr, nr) DO_MODIFY_LDT (descr, nr)
  143. #endif
  144. /* Free resources associated with thread descriptor. */
  145. #ifdef __ASSUME_SET_THREAD_AREA_SYSCALL
  146. #define FREE_THREAD(descr, nr) do { } while (0)
  147. #elif defined __NR_set_thread_area
  148. #define FREE_THREAD(descr, nr) \
  149. { \
  150. int __gs; \
  151. __asm__ __volatile__ ("movw %%gs, %w0" : "=q" (__gs)); \
  152. if (__builtin_expect (__gs & 4, 0)) \
  153. { \
  154. struct modify_ldt_ldt_s ldt_entry = \
  155. { nr, 0, 0, 0, 0, 1, 0, 1, 0, 0 }; \
  156. __modify_ldt (1, &ldt_entry, sizeof (ldt_entry)); \
  157. } \
  158. }
  159. #else
  160. #define FREE_THREAD(descr, nr) \
  161. { \
  162. struct modify_ldt_ldt_s ldt_entry = \
  163. { nr, 0, 0, 0, 0, 1, 0, 1, 0, 0 }; \
  164. __modify_ldt (1, &ldt_entry, sizeof (ldt_entry)); \
  165. }
  166. #endif
  167. /* Read member of the thread descriptor directly. */
  168. #define THREAD_GETMEM(descr, member) \
  169. ({ \
  170. __typeof__ (descr->member) __value; \
  171. if (sizeof (__value) == 1) \
  172. __asm__ __volatile__ ("movb %%gs:%P2,%b0" \
  173. : "=q" (__value) \
  174. : "0" (0), \
  175. "i" (offsetof (struct _pthread_descr_struct, \
  176. member))); \
  177. else if (sizeof (__value) == 4) \
  178. __asm__ __volatile__ ("movl %%gs:%P1,%0" \
  179. : "=r" (__value) \
  180. : "i" (offsetof (struct _pthread_descr_struct, \
  181. member))); \
  182. else \
  183. { \
  184. if (sizeof (__value) != 8) \
  185. /* There should not be any value with a size other than 1, 4 or 8. */\
  186. abort (); \
  187. \
  188. __asm__ __volatile__ ("movl %%gs:%P1,%%eax\n\t" \
  189. "movl %%gs:%P2,%%edx" \
  190. : "=A" (__value) \
  191. : "i" (offsetof (struct _pthread_descr_struct, \
  192. member)), \
  193. "i" (offsetof (struct _pthread_descr_struct, \
  194. member) + 4)); \
  195. } \
  196. __value; \
  197. })
  198. /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
  199. #define THREAD_GETMEM_NC(descr, member) \
  200. ({ \
  201. __typeof__ (descr->member) __value; \
  202. if (sizeof (__value) == 1) \
  203. __asm__ __volatile__ ("movb %%gs:(%2),%b0" \
  204. : "=q" (__value) \
  205. : "0" (0), \
  206. "r" (offsetof (struct _pthread_descr_struct, \
  207. member))); \
  208. else if (sizeof (__value) == 4) \
  209. __asm__ __volatile__ ("movl %%gs:(%1),%0" \
  210. : "=r" (__value) \
  211. : "r" (offsetof (struct _pthread_descr_struct, \
  212. member))); \
  213. else \
  214. { \
  215. if (sizeof (__value) != 8) \
  216. /* There should not be any value with a size other than 1, 4 or 8. */\
  217. abort (); \
  218. \
  219. __asm__ __volatile__ ("movl %%gs:(%1),%%eax\n\t" \
  220. "movl %%gs:4(%1),%%edx" \
  221. : "=&A" (__value) \
  222. : "r" (offsetof (struct _pthread_descr_struct, \
  223. member))); \
  224. } \
  225. __value; \
  226. })
  227. /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
  228. #define THREAD_SETMEM(descr, member, value) \
  229. ({ \
  230. __typeof__ (descr->member) __value = (value); \
  231. if (sizeof (__value) == 1) \
  232. __asm__ __volatile__ ("movb %0,%%gs:%P1" : \
  233. : "q" (__value), \
  234. "i" (offsetof (struct _pthread_descr_struct, \
  235. member))); \
  236. else if (sizeof (__value) == 4) \
  237. __asm__ __volatile__ ("movl %0,%%gs:%P1" : \
  238. : "r" (__value), \
  239. "i" (offsetof (struct _pthread_descr_struct, \
  240. member))); \
  241. else \
  242. { \
  243. if (sizeof (__value) != 8) \
  244. /* There should not be any value with a size other than 1, 4 or 8. */\
  245. abort (); \
  246. \
  247. __asm__ __volatile__ ("movl %%eax,%%gs:%P1\n\n" \
  248. "movl %%edx,%%gs:%P2" : \
  249. : "A" (__value), \
  250. "i" (offsetof (struct _pthread_descr_struct, \
  251. member)), \
  252. "i" (offsetof (struct _pthread_descr_struct, \
  253. member) + 4)); \
  254. } \
  255. })
  256. /* Set member of the thread descriptor directly. */
  257. #define THREAD_SETMEM_NC(descr, member, value) \
  258. ({ \
  259. __typeof__ (descr->member) __value = (value); \
  260. if (sizeof (__value) == 1) \
  261. __asm__ __volatile__ ("movb %0,%%gs:(%1)" : \
  262. : "q" (__value), \
  263. "r" (offsetof (struct _pthread_descr_struct, \
  264. member))); \
  265. else if (sizeof (__value) == 4) \
  266. __asm__ __volatile__ ("movl %0,%%gs:(%1)" : \
  267. : "r" (__value), \
  268. "r" (offsetof (struct _pthread_descr_struct, \
  269. member))); \
  270. else \
  271. { \
  272. if (sizeof (__value) != 8) \
  273. /* There should not be any value with a size other than 1, 4 or 8. */\
  274. abort (); \
  275. \
  276. __asm__ __volatile__ ("movl %%eax,%%gs:(%1)\n\t" \
  277. "movl %%edx,%%gs:4(%1)" : \
  278. : "A" (__value), \
  279. "r" (offsetof (struct _pthread_descr_struct, \
  280. member))); \
  281. } \
  282. })
  283. #endif
  284. /* We want the OS to assign stack addresses. */
  285. #define FLOATING_STACKS 1
  286. /* Maximum size of the stack if the rlimit is unlimited. */
  287. #define ARCH_STACK_MAX_SIZE 8*1024*1024