useldt.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* Special definitions for ix86 machine using segment register based
  2. thread descriptor.
  3. Copyright (C) 1998 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 Library General Public License as
  8. published by the Free Software Foundation; either version 2 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. Library General Public License for more details.
  14. You should have received a copy of the GNU Library 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. #include <stddef.h> /* For offsetof. */
  19. /* We don't want to include the kernel header. So duplicate the
  20. information. */
  21. /* Structure passed on `modify_ldt' call. */
  22. struct modify_ldt_ldt_s
  23. {
  24. unsigned int entry_number;
  25. unsigned long int base_addr;
  26. unsigned int limit;
  27. unsigned int seg_32bit:1;
  28. unsigned int contents:2;
  29. unsigned int read_exec_only:1;
  30. unsigned int limit_in_pages:1;
  31. unsigned int seg_not_present:1;
  32. unsigned int useable:1;
  33. unsigned int empty:25;
  34. };
  35. /* System call to set LDT entry. */
  36. extern int __modify_ldt (int, struct modify_ldt_ldt_s *, size_t);
  37. /* Return the thread descriptor for the current thread.
  38. The contained asm must *not* be marked volatile since otherwise
  39. assignments like
  40. pthread_descr self = thread_self();
  41. do not get optimized away. */
  42. #define THREAD_SELF \
  43. ({ \
  44. register pthread_descr __self; \
  45. __asm__ ("movl %%gs:%c1,%0" : "=r" (__self) \
  46. : "i" (offsetof (struct _pthread_descr_struct, p_self))); \
  47. __self; \
  48. })
  49. /* Initialize the thread-unique value. */
  50. #define INIT_THREAD_SELF(descr, nr) \
  51. { \
  52. struct modify_ldt_ldt_s ldt_entry = \
  53. { nr, (unsigned long int) descr, sizeof (*descr), 1, 0, 0, 0, 0, 1, 0 }; \
  54. if (__modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0) \
  55. abort (); \
  56. __asm__ __volatile__ ("movw %w0, %%gs" : : "r" (nr * 8 + 7)); \
  57. }
  58. /* Free resources associated with thread descriptor. */
  59. #define FREE_THREAD_SELF(descr, nr) \
  60. { \
  61. struct modify_ldt_ldt_s ldt_entry = \
  62. { nr, 0, 0, 0, 0, 1, 0, 1, 0, 0 }; \
  63. __asm__ __volatile__ ("movw %w0,%%gs" : : "r" (0)); \
  64. __modify_ldt (1, &ldt_entry, sizeof (ldt_entry)); \
  65. }
  66. /* Read member of the thread descriptor directly. */
  67. #define THREAD_GETMEM(descr, member) \
  68. ({ \
  69. __typeof__ (descr->member) __value; \
  70. if (sizeof (__value) == 1) \
  71. __asm__ __volatile__ ("movb %%gs:%P2,%b0" \
  72. : "=r" (__value) \
  73. : "0" (0), \
  74. "i" (offsetof (struct _pthread_descr_struct, \
  75. member))); \
  76. else \
  77. { \
  78. if (sizeof (__value) != 4) \
  79. /* There should not be any value with a size other than 1 or 4. */ \
  80. abort (); \
  81. \
  82. __asm__ __volatile__ ("movl %%gs:%P1,%0" \
  83. : "=r" (__value) \
  84. : "i" (offsetof (struct _pthread_descr_struct, \
  85. member))); \
  86. } \
  87. __value; \
  88. })
  89. /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
  90. #define THREAD_GETMEM_NC(descr, member) \
  91. ({ \
  92. __typeof__ (descr->member) __value; \
  93. if (sizeof (__value) == 1) \
  94. __asm__ __volatile__ ("movb %%gs:(%2),%b0" \
  95. : "=r" (__value) \
  96. : "0" (0), \
  97. "r" (offsetof (struct _pthread_descr_struct, \
  98. member))); \
  99. else \
  100. { \
  101. if (sizeof (__value) != 4) \
  102. /* There should not be any value with a size other than 1 or 4. */ \
  103. abort (); \
  104. \
  105. __asm__ __volatile__ ("movl %%gs:(%1),%0" \
  106. : "=r" (__value) \
  107. : "r" (offsetof (struct _pthread_descr_struct, \
  108. member))); \
  109. } \
  110. __value; \
  111. })
  112. /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
  113. #define THREAD_SETMEM(descr, member, value) \
  114. ({ \
  115. __typeof__ (descr->member) __value = (value); \
  116. if (sizeof (__value) == 1) \
  117. __asm__ __volatile__ ("movb %0,%%gs:%P1" : \
  118. : "r" (__value), \
  119. "i" (offsetof (struct _pthread_descr_struct, \
  120. member))); \
  121. else \
  122. { \
  123. if (sizeof (__value) != 4) \
  124. /* There should not be any value with a size other than 1 or 4. */ \
  125. abort (); \
  126. \
  127. __asm__ __volatile__ ("movl %0,%%gs:%P1" : \
  128. : "r" (__value), \
  129. "i" (offsetof (struct _pthread_descr_struct, \
  130. member))); \
  131. } \
  132. })
  133. /* Set member of the thread descriptor directly. */
  134. #define THREAD_SETMEM_NC(descr, member, value) \
  135. ({ \
  136. __typeof__ (descr->member) __value = (value); \
  137. if (sizeof (__value) == 1) \
  138. __asm__ __volatile__ ("movb %0,%%gs:(%1)" : \
  139. : "r" (__value), \
  140. "r" (offsetof (struct _pthread_descr_struct, \
  141. member))); \
  142. else \
  143. { \
  144. if (sizeof (__value) != 4) \
  145. /* There should not be any value with a size other than 1 or 4. */ \
  146. abort (); \
  147. \
  148. __asm__ __volatile__ ("movl %0,%%gs:(%1)" : \
  149. : "r" (__value), \
  150. "r" (offsetof (struct _pthread_descr_struct, \
  151. member))); \
  152. } \
  153. })