tls.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include <linux/kernel.h>
  2. #include <linux/errno.h>
  3. #include <linux/sched.h>
  4. #include <linux/user.h>
  5. #include <linux/regset.h>
  6. #include <linux/syscalls.h>
  7. #include <asm/uaccess.h>
  8. #include <asm/desc.h>
  9. #include <asm/ldt.h>
  10. #include <asm/processor.h>
  11. #include <asm/proto.h>
  12. #include "tls.h"
  13. /*
  14. * sys_alloc_thread_area: get a yet unused TLS descriptor index.
  15. */
  16. static int get_free_idx(void)
  17. {
  18. struct thread_struct *t = &current->thread;
  19. int idx;
  20. for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
  21. if (desc_empty(&t->tls_array[idx]))
  22. return idx + GDT_ENTRY_TLS_MIN;
  23. return -ESRCH;
  24. }
  25. static bool tls_desc_okay(const struct user_desc *info)
  26. {
  27. if (LDT_empty(info))
  28. return true;
  29. /*
  30. * espfix is required for 16-bit data segments, but espfix
  31. * only works for LDT segments.
  32. */
  33. if (!info->seg_32bit)
  34. return false;
  35. return true;
  36. }
  37. static void set_tls_desc(struct task_struct *p, int idx,
  38. const struct user_desc *info, int n)
  39. {
  40. struct thread_struct *t = &p->thread;
  41. struct desc_struct *desc = &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
  42. int cpu;
  43. /*
  44. * We must not get preempted while modifying the TLS.
  45. */
  46. cpu = get_cpu();
  47. while (n-- > 0) {
  48. if (LDT_empty(info))
  49. desc->a = desc->b = 0;
  50. else
  51. fill_ldt(desc, info);
  52. ++info;
  53. ++desc;
  54. }
  55. if (t == &current->thread)
  56. load_TLS(t, cpu);
  57. put_cpu();
  58. }
  59. /*
  60. * Set a given TLS descriptor:
  61. */
  62. int do_set_thread_area(struct task_struct *p, int idx,
  63. struct user_desc __user *u_info,
  64. int can_allocate)
  65. {
  66. struct user_desc info;
  67. if (copy_from_user(&info, u_info, sizeof(info)))
  68. return -EFAULT;
  69. if (!tls_desc_okay(&info))
  70. return -EINVAL;
  71. if (idx == -1)
  72. idx = info.entry_number;
  73. /*
  74. * index -1 means the kernel should try to find and
  75. * allocate an empty descriptor:
  76. */
  77. if (idx == -1 && can_allocate) {
  78. idx = get_free_idx();
  79. if (idx < 0)
  80. return idx;
  81. if (put_user(idx, &u_info->entry_number))
  82. return -EFAULT;
  83. }
  84. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  85. return -EINVAL;
  86. set_tls_desc(p, idx, &info, 1);
  87. return 0;
  88. }
  89. SYSCALL_DEFINE1(set_thread_area, struct user_desc __user *, u_info)
  90. {
  91. return do_set_thread_area(current, -1, u_info, 1);
  92. }
  93. /*
  94. * Get the current Thread-Local Storage area:
  95. */
  96. static void fill_user_desc(struct user_desc *info, int idx,
  97. const struct desc_struct *desc)
  98. {
  99. memset(info, 0, sizeof(*info));
  100. info->entry_number = idx;
  101. info->base_addr = get_desc_base(desc);
  102. info->limit = get_desc_limit(desc);
  103. info->seg_32bit = desc->d;
  104. info->contents = desc->type >> 2;
  105. info->read_exec_only = !(desc->type & 2);
  106. info->limit_in_pages = desc->g;
  107. info->seg_not_present = !desc->p;
  108. info->useable = desc->avl;
  109. #ifdef CONFIG_X86_64
  110. info->lm = desc->l;
  111. #endif
  112. }
  113. int do_get_thread_area(struct task_struct *p, int idx,
  114. struct user_desc __user *u_info)
  115. {
  116. struct user_desc info;
  117. if (idx == -1 && get_user(idx, &u_info->entry_number))
  118. return -EFAULT;
  119. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  120. return -EINVAL;
  121. fill_user_desc(&info, idx,
  122. &p->thread.tls_array[idx - GDT_ENTRY_TLS_MIN]);
  123. if (copy_to_user(u_info, &info, sizeof(info)))
  124. return -EFAULT;
  125. return 0;
  126. }
  127. SYSCALL_DEFINE1(get_thread_area, struct user_desc __user *, u_info)
  128. {
  129. return do_get_thread_area(current, -1, u_info);
  130. }
  131. int regset_tls_active(struct task_struct *target,
  132. const struct user_regset *regset)
  133. {
  134. struct thread_struct *t = &target->thread;
  135. int n = GDT_ENTRY_TLS_ENTRIES;
  136. while (n > 0 && desc_empty(&t->tls_array[n - 1]))
  137. --n;
  138. return n;
  139. }
  140. int regset_tls_get(struct task_struct *target, const struct user_regset *regset,
  141. unsigned int pos, unsigned int count,
  142. void *kbuf, void __user *ubuf)
  143. {
  144. const struct desc_struct *tls;
  145. if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
  146. (pos % sizeof(struct user_desc)) != 0 ||
  147. (count % sizeof(struct user_desc)) != 0)
  148. return -EINVAL;
  149. pos /= sizeof(struct user_desc);
  150. count /= sizeof(struct user_desc);
  151. tls = &target->thread.tls_array[pos];
  152. if (kbuf) {
  153. struct user_desc *info = kbuf;
  154. while (count-- > 0)
  155. fill_user_desc(info++, GDT_ENTRY_TLS_MIN + pos++,
  156. tls++);
  157. } else {
  158. struct user_desc __user *u_info = ubuf;
  159. while (count-- > 0) {
  160. struct user_desc info;
  161. fill_user_desc(&info, GDT_ENTRY_TLS_MIN + pos++, tls++);
  162. if (__copy_to_user(u_info++, &info, sizeof(info)))
  163. return -EFAULT;
  164. }
  165. }
  166. return 0;
  167. }
  168. int regset_tls_set(struct task_struct *target, const struct user_regset *regset,
  169. unsigned int pos, unsigned int count,
  170. const void *kbuf, const void __user *ubuf)
  171. {
  172. struct user_desc infobuf[GDT_ENTRY_TLS_ENTRIES];
  173. const struct user_desc *info;
  174. int i;
  175. if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
  176. (pos % sizeof(struct user_desc)) != 0 ||
  177. (count % sizeof(struct user_desc)) != 0)
  178. return -EINVAL;
  179. if (kbuf)
  180. info = kbuf;
  181. else if (__copy_from_user(infobuf, ubuf, count))
  182. return -EFAULT;
  183. else
  184. info = infobuf;
  185. for (i = 0; i < count / sizeof(struct user_desc); i++)
  186. if (!tls_desc_okay(info + i))
  187. return -EINVAL;
  188. set_tls_desc(target,
  189. GDT_ENTRY_TLS_MIN + (pos / sizeof(struct user_desc)),
  190. info, count / sizeof(struct user_desc));
  191. return 0;
  192. }