tls.c 4.8 KB

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