tls.c 4.8 KB

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