useldt.h 11 KB

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