tls.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 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. SYSCALL_DEFINE1(set_thread_area, struct user_desc __user *, u_info)
  76. {
  77. return do_set_thread_area(current, -1, u_info, 1);
  78. }
  79. /*
  80. * Get the current Thread-Local Storage area:
  81. */
  82. static void fill_user_desc(struct user_desc *info, int idx,
  83. const struct desc_struct *desc)
  84. {
  85. memset(info, 0, sizeof(*info));
  86. info->entry_number = idx;
  87. info->base_addr = get_desc_base(desc);
  88. info->limit = get_desc_limit(desc);
  89. info->seg_32bit = desc->d;
  90. info->contents = desc->type >> 2;
  91. info->read_exec_only = !(desc->type & 2);
  92. info->limit_in_pages = desc->g;
  93. info->seg_not_present = !desc->p;
  94. info->useable = desc->avl;
  95. #ifdef CONFIG_X86_64
  96. info->lm = desc->l;
  97. #endif
  98. }
  99. int do_get_thread_area(struct task_struct *p, int idx,
  100. struct user_desc __user *u_info)
  101. {
  102. struct user_desc info;
  103. if (idx == -1 && get_user(idx, &u_info->entry_number))
  104. return -EFAULT;
  105. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  106. return -EINVAL;
  107. fill_user_desc(&info, idx,
  108. &p->thread.tls_array[idx - GDT_ENTRY_TLS_MIN]);
  109. if (copy_to_user(u_info, &info, sizeof(info)))
  110. return -EFAULT;
  111. return 0;
  112. }
  113. SYSCALL_DEFINE1(get_thread_area, struct user_desc __user *, u_info)
  114. {
  115. return do_get_thread_area(current, -1, u_info);
  116. }
  117. int regset_tls_active(struct task_struct *target,
  118. const struct user_regset *regset)
  119. {
  120. struct thread_struct *t = &target->thread;
  121. int n = GDT_ENTRY_TLS_ENTRIES;
  122. while (n > 0 && desc_empty(&t->tls_array[n - 1]))
  123. --n;
  124. return n;
  125. }
  126. int regset_tls_get(struct task_struct *target, const struct user_regset *regset,
  127. unsigned int pos, unsigned int count,
  128. void *kbuf, void __user *ubuf)
  129. {
  130. const struct desc_struct *tls;
  131. if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
  132. (pos % sizeof(struct user_desc)) != 0 ||
  133. (count % sizeof(struct user_desc)) != 0)
  134. return -EINVAL;
  135. pos /= sizeof(struct user_desc);
  136. count /= sizeof(struct user_desc);
  137. tls = &target->thread.tls_array[pos];
  138. if (kbuf) {
  139. struct user_desc *info = kbuf;
  140. while (count-- > 0)
  141. fill_user_desc(info++, GDT_ENTRY_TLS_MIN + pos++,
  142. tls++);
  143. } else {
  144. struct user_desc __user *u_info = ubuf;
  145. while (count-- > 0) {
  146. struct user_desc info;
  147. fill_user_desc(&info, GDT_ENTRY_TLS_MIN + pos++, tls++);
  148. if (__copy_to_user(u_info++, &info, sizeof(info)))
  149. return -EFAULT;
  150. }
  151. }
  152. return 0;
  153. }
  154. int regset_tls_set(struct task_struct *target, const struct user_regset *regset,
  155. unsigned int pos, unsigned int count,
  156. const void *kbuf, const void __user *ubuf)
  157. {
  158. struct user_desc infobuf[GDT_ENTRY_TLS_ENTRIES];
  159. const struct user_desc *info;
  160. if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
  161. (pos % sizeof(struct user_desc)) != 0 ||
  162. (count % sizeof(struct user_desc)) != 0)
  163. return -EINVAL;
  164. if (kbuf)
  165. info = kbuf;
  166. else if (__copy_from_user(infobuf, ubuf, count))
  167. return -EFAULT;
  168. else
  169. info = infobuf;
  170. set_tls_desc(target,
  171. GDT_ENTRY_TLS_MIN + (pos / sizeof(struct user_desc)),
  172. info, count / sizeof(struct user_desc));
  173. return 0;
  174. }