tls.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (C) 2005 Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
  3. * Licensed under the GPL
  4. */
  5. #include "linux/config.h"
  6. #include "linux/kernel.h"
  7. #include "linux/sched.h"
  8. #include "linux/slab.h"
  9. #include "linux/types.h"
  10. #include "asm/uaccess.h"
  11. #include "asm/ptrace.h"
  12. #include "asm/segment.h"
  13. #include "asm/smp.h"
  14. #include "asm/desc.h"
  15. #include "choose-mode.h"
  16. #include "kern.h"
  17. #include "kern_util.h"
  18. #include "mode_kern.h"
  19. #include "os.h"
  20. #include "mode.h"
  21. #ifdef CONFIG_MODE_SKAS
  22. #include "skas.h"
  23. #endif
  24. #ifdef CONFIG_MODE_SKAS
  25. int do_set_thread_area_skas(struct user_desc *info)
  26. {
  27. int ret;
  28. u32 cpu;
  29. cpu = get_cpu();
  30. ret = os_set_thread_area(info, userspace_pid[cpu]);
  31. put_cpu();
  32. return ret;
  33. }
  34. int do_get_thread_area_skas(struct user_desc *info)
  35. {
  36. int ret;
  37. u32 cpu;
  38. cpu = get_cpu();
  39. ret = os_get_thread_area(info, userspace_pid[cpu]);
  40. put_cpu();
  41. return ret;
  42. }
  43. #endif
  44. /*
  45. * sys_get_thread_area: get a yet unused TLS descriptor index.
  46. * XXX: Consider leaving one free slot for glibc usage at first place. This must
  47. * be done here (and by changing GDT_ENTRY_TLS_* macros) and nowhere else.
  48. *
  49. * Also, this must be tested when compiling in SKAS mode with dinamic linking
  50. * and running against NPTL.
  51. */
  52. static int get_free_idx(struct task_struct* task)
  53. {
  54. struct thread_struct *t = &task->thread;
  55. int idx;
  56. if (!t->arch.tls_array)
  57. return GDT_ENTRY_TLS_MIN;
  58. for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
  59. if (!t->arch.tls_array[idx].present)
  60. return idx + GDT_ENTRY_TLS_MIN;
  61. return -ESRCH;
  62. }
  63. #define O_FORCE 1
  64. static inline void clear_user_desc(struct user_desc* info)
  65. {
  66. /* Postcondition: LDT_empty(info) returns true. */
  67. memset(info, 0, sizeof(*info));
  68. /* Check the LDT_empty or the i386 sys_get_thread_area code - we obtain
  69. * indeed an empty user_desc.
  70. */
  71. info->read_exec_only = 1;
  72. info->seg_not_present = 1;
  73. }
  74. static int load_TLS(int flags, struct task_struct *to)
  75. {
  76. int ret = 0;
  77. int idx;
  78. for (idx = GDT_ENTRY_TLS_MIN; idx < GDT_ENTRY_TLS_MAX; idx++) {
  79. struct uml_tls_struct* curr = &to->thread.arch.tls_array[idx - GDT_ENTRY_TLS_MIN];
  80. /* Actually, now if it wasn't flushed it gets cleared and
  81. * flushed to the host, which will clear it.*/
  82. if (!curr->present) {
  83. if (!curr->flushed) {
  84. clear_user_desc(&curr->tls);
  85. curr->tls.entry_number = idx;
  86. } else {
  87. WARN_ON(!LDT_empty(&curr->tls));
  88. continue;
  89. }
  90. }
  91. if (!(flags & O_FORCE) && curr->flushed)
  92. continue;
  93. ret = do_set_thread_area(&curr->tls);
  94. if (ret)
  95. goto out;
  96. curr->flushed = 1;
  97. }
  98. out:
  99. return ret;
  100. }
  101. /* Verify if we need to do a flush for the new process, i.e. if there are any
  102. * present desc's, only if they haven't been flushed.
  103. */
  104. static inline int needs_TLS_update(struct task_struct *task)
  105. {
  106. int i;
  107. int ret = 0;
  108. for (i = GDT_ENTRY_TLS_MIN; i < GDT_ENTRY_TLS_MAX; i++) {
  109. struct uml_tls_struct* curr = &task->thread.arch.tls_array[i - GDT_ENTRY_TLS_MIN];
  110. /* Can't test curr->present, we may need to clear a descriptor
  111. * which had a value. */
  112. if (curr->flushed)
  113. continue;
  114. ret = 1;
  115. break;
  116. }
  117. return ret;
  118. }
  119. /* On a newly forked process, the TLS descriptors haven't yet been flushed. So
  120. * we mark them as such and the first switch_to will do the job.
  121. */
  122. void clear_flushed_tls(struct task_struct *task)
  123. {
  124. int i;
  125. for (i = GDT_ENTRY_TLS_MIN; i < GDT_ENTRY_TLS_MAX; i++) {
  126. struct uml_tls_struct* curr = &task->thread.arch.tls_array[i - GDT_ENTRY_TLS_MIN];
  127. /* Still correct to do this, if it wasn't present on the host it
  128. * will remain as flushed as it was. */
  129. if (!curr->present)
  130. continue;
  131. curr->flushed = 0;
  132. }
  133. }
  134. /* This in SKAS0 does not need to be used, since we have different host
  135. * processes. Nor will this need to be used when we'll add support to the host
  136. * SKAS patch. */
  137. int arch_switch_tls_skas(struct task_struct *from, struct task_struct *to)
  138. {
  139. return load_TLS(O_FORCE, to);
  140. }
  141. int arch_switch_tls_tt(struct task_struct *from, struct task_struct *to)
  142. {
  143. if (needs_TLS_update(to))
  144. return load_TLS(0, to);
  145. return 0;
  146. }
  147. static int set_tls_entry(struct task_struct* task, struct user_desc *info,
  148. int idx, int flushed)
  149. {
  150. struct thread_struct *t = &task->thread;
  151. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  152. return -EINVAL;
  153. t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].tls = *info;
  154. t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].present = 1;
  155. t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].flushed = flushed;
  156. return 0;
  157. }
  158. int arch_copy_tls(struct task_struct *new)
  159. {
  160. struct user_desc info;
  161. int idx, ret = -EFAULT;
  162. if (copy_from_user(&info,
  163. (void __user *) UPT_ESI(&new->thread.regs.regs),
  164. sizeof(info)))
  165. goto out;
  166. ret = -EINVAL;
  167. if (LDT_empty(&info))
  168. goto out;
  169. idx = info.entry_number;
  170. ret = set_tls_entry(new, &info, idx, 0);
  171. out:
  172. return ret;
  173. }
  174. /* XXX: use do_get_thread_area to read the host value? I'm not at all sure! */
  175. static int get_tls_entry(struct task_struct* task, struct user_desc *info, int idx)
  176. {
  177. struct thread_struct *t = &task->thread;
  178. if (!t->arch.tls_array)
  179. goto clear;
  180. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  181. return -EINVAL;
  182. if (!t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].present)
  183. goto clear;
  184. *info = t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].tls;
  185. out:
  186. /* Temporary debugging check, to make sure that things have been
  187. * flushed. This could be triggered if load_TLS() failed.
  188. */
  189. if (unlikely(task == current && !t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].flushed)) {
  190. printk(KERN_ERR "get_tls_entry: task with pid %d got here "
  191. "without flushed TLS.", current->pid);
  192. }
  193. return 0;
  194. clear:
  195. /* When the TLS entry has not been set, the values read to user in the
  196. * tls_array are 0 (because it's cleared at boot, see
  197. * arch/i386/kernel/head.S:cpu_gdt_table). Emulate that.
  198. */
  199. clear_user_desc(info);
  200. info->entry_number = idx;
  201. goto out;
  202. }
  203. asmlinkage int sys_set_thread_area(struct user_desc __user *user_desc)
  204. {
  205. struct user_desc info;
  206. int idx, ret;
  207. if (copy_from_user(&info, user_desc, sizeof(info)))
  208. return -EFAULT;
  209. idx = info.entry_number;
  210. if (idx == -1) {
  211. idx = get_free_idx(current);
  212. if (idx < 0)
  213. return idx;
  214. info.entry_number = idx;
  215. /* Tell the user which slot we chose for him.*/
  216. if (put_user(idx, &user_desc->entry_number))
  217. return -EFAULT;
  218. }
  219. ret = CHOOSE_MODE_PROC(do_set_thread_area_tt, do_set_thread_area_skas, &info);
  220. if (ret)
  221. return ret;
  222. return set_tls_entry(current, &info, idx, 1);
  223. }
  224. /*
  225. * Perform set_thread_area on behalf of the traced child.
  226. * Note: error handling is not done on the deferred load, and this differ from
  227. * i386. However the only possible error are caused by bugs.
  228. */
  229. int ptrace_set_thread_area(struct task_struct *child, int idx,
  230. struct user_desc __user *user_desc)
  231. {
  232. struct user_desc info;
  233. if (copy_from_user(&info, user_desc, sizeof(info)))
  234. return -EFAULT;
  235. return set_tls_entry(child, &info, idx, 0);
  236. }
  237. asmlinkage int sys_get_thread_area(struct user_desc __user *user_desc)
  238. {
  239. struct user_desc info;
  240. int idx, ret;
  241. if (get_user(idx, &user_desc->entry_number))
  242. return -EFAULT;
  243. ret = get_tls_entry(current, &info, idx);
  244. if (ret < 0)
  245. goto out;
  246. if (copy_to_user(user_desc, &info, sizeof(info)))
  247. ret = -EFAULT;
  248. out:
  249. return ret;
  250. }
  251. /*
  252. * Perform get_thread_area on behalf of the traced child.
  253. */
  254. int ptrace_get_thread_area(struct task_struct *child, int idx,
  255. struct user_desc __user *user_desc)
  256. {
  257. struct user_desc info;
  258. int ret;
  259. ret = get_tls_entry(child, &info, idx);
  260. if (ret < 0)
  261. goto out;
  262. if (copy_to_user(user_desc, &info, sizeof(info)))
  263. ret = -EFAULT;
  264. out:
  265. return ret;
  266. }