useldt.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 %3, %%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. if (__result == 0) \
  98. __asm__ ("movw %w0, %%gs" :: "q" (__gs)); \
  99. else \
  100. __gs = -1; \
  101. } \
  102. else \
  103. { \
  104. struct modify_ldt_ldt_s ldt_entry = \
  105. { -1, \
  106. (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
  107. 1, 0, 0, 1, 0, 1, 0 }; \
  108. int __result; \
  109. __asm__ (USETLS_LOAD_EBX \
  110. "movl %2, %%eax\n\t" \
  111. "int $0x80\n\t" \
  112. USETLS_LOAD_EBX \
  113. : "&a" (__result) \
  114. : USETLS_EBX_ARG (&ldt_entry), "i" (__NR_set_thread_area)); \
  115. if (__result == 0) \
  116. { \
  117. __gs = (ldt_entry.entry_number << 3) + 3; \
  118. __asm__ ("movw %w0, %%gs" : : "q" (__gs)); \
  119. } \
  120. else \
  121. __gs = -1; \
  122. } \
  123. __gs; \
  124. })
  125. #if defined __ASSUME_SET_THREAD_AREA_SYSCALL
  126. # define INIT_THREAD_SELF(descr, nr) DO_SET_THREAD_AREA (descr, nr)
  127. #elif defined __NR_set_thread_area
  128. # define INIT_THREAD_SELF(descr, nr) \
  129. ({ \
  130. if (__builtin_expect (__have_no_set_thread_area, 0) \
  131. || (DO_SET_THREAD_AREA (descr, DO_SET_THREAD_AREA_REUSE (nr)) == -1 \
  132. && (__have_no_set_thread_area = 1))) \
  133. DO_MODIFY_LDT (descr, nr); \
  134. })
  135. /* Defined in pspinlock.c. */
  136. extern int __have_no_set_thread_area;
  137. #else
  138. # define INIT_THREAD_SELF(descr, nr) DO_MODIFY_LDT (descr, nr)
  139. #endif
  140. /* Free resources associated with thread descriptor. */
  141. #ifdef __ASSUME_SET_THREAD_AREA_SYSCALL
  142. #define FREE_THREAD(descr, nr) do { } while (0)
  143. #elif defined __NR_set_thread_area
  144. #define FREE_THREAD(descr, nr) \
  145. { \
  146. int __gs; \
  147. __asm__ __volatile__ ("movw %%gs, %w0" : "=q" (__gs)); \
  148. if (__builtin_expect (__gs & 4, 0)) \
  149. { \
  150. struct modify_ldt_ldt_s ldt_entry = \
  151. { nr, 0, 0, 0, 0, 1, 0, 1, 0, 0 }; \
  152. __modify_ldt (1, &ldt_entry, sizeof (ldt_entry)); \
  153. } \
  154. }
  155. #else
  156. #define FREE_THREAD(descr, nr) \
  157. { \
  158. struct modify_ldt_ldt_s ldt_entry = \
  159. { nr, 0, 0, 0, 0, 1, 0, 1, 0, 0 }; \
  160. __modify_ldt (1, &ldt_entry, sizeof (ldt_entry)); \
  161. }
  162. #endif
  163. /* Read member of the thread descriptor directly. */
  164. #define THREAD_GETMEM(descr, member) \
  165. ({ \
  166. __typeof__ (descr->member) __value; \
  167. if (sizeof (__value) == 1) \
  168. __asm__ __volatile__ ("movb %%gs:%P2,%b0" \
  169. : "=q" (__value) \
  170. : "0" (0), \
  171. "i" (offsetof (struct _pthread_descr_struct, \
  172. member))); \
  173. else if (sizeof (__value) == 4) \
  174. __asm__ __volatile__ ("movl %%gs:%P1,%0" \
  175. : "=r" (__value) \
  176. : "i" (offsetof (struct _pthread_descr_struct, \
  177. member))); \
  178. else \
  179. { \
  180. if (sizeof (__value) != 8) \
  181. /* There should not be any value with a size other than 1, 4 or 8. */\
  182. abort (); \
  183. \
  184. __asm__ __volatile__ ("movl %%gs:%P1,%%eax\n\t" \
  185. "movl %%gs:%P2,%%edx" \
  186. : "=A" (__value) \
  187. : "i" (offsetof (struct _pthread_descr_struct, \
  188. member)), \
  189. "i" (offsetof (struct _pthread_descr_struct, \
  190. member) + 4)); \
  191. } \
  192. __value; \
  193. })
  194. /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
  195. #define THREAD_GETMEM_NC(descr, member) \
  196. ({ \
  197. __typeof__ (descr->member) __value; \
  198. if (sizeof (__value) == 1) \
  199. __asm__ __volatile__ ("movb %%gs:(%2),%b0" \
  200. : "=q" (__value) \
  201. : "0" (0), \
  202. "r" (offsetof (struct _pthread_descr_struct, \
  203. member))); \
  204. else if (sizeof (__value) == 4) \
  205. __asm__ __volatile__ ("movl %%gs:(%1),%0" \
  206. : "=r" (__value) \
  207. : "r" (offsetof (struct _pthread_descr_struct, \
  208. member))); \
  209. else \
  210. { \
  211. if (sizeof (__value) != 8) \
  212. /* There should not be any value with a size other than 1, 4 or 8. */\
  213. abort (); \
  214. \
  215. __asm__ __volatile__ ("movl %%gs:(%1),%%eax\n\t" \
  216. "movl %%gs:4(%1),%%edx" \
  217. : "=&A" (__value) \
  218. : "r" (offsetof (struct _pthread_descr_struct, \
  219. member))); \
  220. } \
  221. __value; \
  222. })
  223. /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
  224. #define THREAD_SETMEM(descr, member, value) \
  225. ({ \
  226. __typeof__ (descr->member) __value = (value); \
  227. if (sizeof (__value) == 1) \
  228. __asm__ __volatile__ ("movb %0,%%gs:%P1" : \
  229. : "q" (__value), \
  230. "i" (offsetof (struct _pthread_descr_struct, \
  231. member))); \
  232. else if (sizeof (__value) == 4) \
  233. __asm__ __volatile__ ("movl %0,%%gs:%P1" : \
  234. : "r" (__value), \
  235. "i" (offsetof (struct _pthread_descr_struct, \
  236. member))); \
  237. else \
  238. { \
  239. if (sizeof (__value) != 8) \
  240. /* There should not be any value with a size other than 1, 4 or 8. */\
  241. abort (); \
  242. \
  243. __asm__ __volatile__ ("movl %%eax,%%gs:%P1\n\n" \
  244. "movl %%edx,%%gs:%P2" : \
  245. : "A" (__value), \
  246. "i" (offsetof (struct _pthread_descr_struct, \
  247. member)), \
  248. "i" (offsetof (struct _pthread_descr_struct, \
  249. member) + 4)); \
  250. } \
  251. })
  252. /* Set member of the thread descriptor directly. */
  253. #define THREAD_SETMEM_NC(descr, member, value) \
  254. ({ \
  255. __typeof__ (descr->member) __value = (value); \
  256. if (sizeof (__value) == 1) \
  257. __asm__ __volatile__ ("movb %0,%%gs:(%1)" : \
  258. : "q" (__value), \
  259. "r" (offsetof (struct _pthread_descr_struct, \
  260. member))); \
  261. else if (sizeof (__value) == 4) \
  262. __asm__ __volatile__ ("movl %0,%%gs:(%1)" : \
  263. : "r" (__value), \
  264. "r" (offsetof (struct _pthread_descr_struct, \
  265. member))); \
  266. else \
  267. { \
  268. if (sizeof (__value) != 8) \
  269. /* There should not be any value with a size other than 1, 4 or 8. */\
  270. abort (); \
  271. \
  272. __asm__ __volatile__ ("movl %%eax,%%gs:(%1)\n\t" \
  273. "movl %%edx,%%gs:4(%1)" : \
  274. : "A" (__value), \
  275. "r" (offsetof (struct _pthread_descr_struct, \
  276. member))); \
  277. } \
  278. })
  279. #endif
  280. /* We want the OS to assign stack addresses. */
  281. #define FLOATING_STACKS 1
  282. /* Maximum size of the stack if the rlimit is unlimited. */
  283. #define ARCH_STACK_MAX_SIZE 8*1024*1024