tls_32.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright (C) 2005 Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
  3. * Licensed under the GPL
  4. */
  5. #include <linux/percpu.h>
  6. #include <linux/sched.h>
  7. #include <linux/syscalls.h>
  8. #include <linux/uaccess.h>
  9. #include <asm/ptrace-abi.h>
  10. #include <os.h>
  11. #include <skas.h>
  12. #include <sysdep/tls.h>
  13. #include <asm/desc.h>
  14. /*
  15. * If needed we can detect when it's uninitialized.
  16. *
  17. * These are initialized in an initcall and unchanged thereafter.
  18. */
  19. static int host_supports_tls = -1;
  20. int host_gdt_entry_tls_min;
  21. static int do_set_thread_area(struct user_desc *info)
  22. {
  23. int ret;
  24. u32 cpu;
  25. cpu = get_cpu();
  26. ret = os_set_thread_area(info, userspace_pid[cpu]);
  27. put_cpu();
  28. if (ret)
  29. printk(KERN_ERR "PTRACE_SET_THREAD_AREA failed, err = %d, "
  30. "index = %d\n", ret, info->entry_number);
  31. return ret;
  32. }
  33. /*
  34. * sys_get_thread_area: get a yet unused TLS descriptor index.
  35. * XXX: Consider leaving one free slot for glibc usage at first place. This must
  36. * be done here (and by changing GDT_ENTRY_TLS_* macros) and nowhere else.
  37. *
  38. * Also, this must be tested when compiling in SKAS mode with dynamic linking
  39. * and running against NPTL.
  40. */
  41. static int get_free_idx(struct task_struct* task)
  42. {
  43. struct thread_struct *t = &task->thread;
  44. int idx;
  45. for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
  46. if (!t->arch.tls_array[idx].present)
  47. return idx + GDT_ENTRY_TLS_MIN;
  48. return -ESRCH;
  49. }
  50. static inline void clear_user_desc(struct user_desc* info)
  51. {
  52. /* Postcondition: LDT_empty(info) returns true. */
  53. memset(info, 0, sizeof(*info));
  54. /*
  55. * Check the LDT_empty or the i386 sys_get_thread_area code - we obtain
  56. * indeed an empty user_desc.
  57. */
  58. info->read_exec_only = 1;
  59. info->seg_not_present = 1;
  60. }
  61. #define O_FORCE 1
  62. static int load_TLS(int flags, struct task_struct *to)
  63. {
  64. int ret = 0;
  65. int idx;
  66. for (idx = GDT_ENTRY_TLS_MIN; idx < GDT_ENTRY_TLS_MAX; idx++) {
  67. struct uml_tls_struct* curr =
  68. &to->thread.arch.tls_array[idx - GDT_ENTRY_TLS_MIN];
  69. /*
  70. * Actually, now if it wasn't flushed it gets cleared and
  71. * flushed to the host, which will clear it.
  72. */
  73. if (!curr->present) {
  74. if (!curr->flushed) {
  75. clear_user_desc(&curr->tls);
  76. curr->tls.entry_number = idx;
  77. } else {
  78. WARN_ON(!LDT_empty(&curr->tls));
  79. continue;
  80. }
  81. }
  82. if (!(flags & O_FORCE) && curr->flushed)
  83. continue;
  84. ret = do_set_thread_area(&curr->tls);
  85. if (ret)
  86. goto out;
  87. curr->flushed = 1;
  88. }
  89. out:
  90. return ret;
  91. }
  92. /*
  93. * Verify if we need to do a flush for the new process, i.e. if there are any
  94. * present desc's, only if they haven't been flushed.
  95. */
  96. static inline int needs_TLS_update(struct task_struct *task)
  97. {
  98. int i;
  99. int ret = 0;
  100. for (i = GDT_ENTRY_TLS_MIN; i < GDT_ENTRY_TLS_MAX; i++) {
  101. struct uml_tls_struct* curr =
  102. &task->thread.arch.tls_array[i - GDT_ENTRY_TLS_MIN];
  103. /*
  104. * Can't test curr->present, we may need to clear a descriptor
  105. * which had a value.
  106. */
  107. if (curr->flushed)
  108. continue;
  109. ret = 1;
  110. break;
  111. }
  112. return ret;
  113. }
  114. /*
  115. * On a newly forked process, the TLS descriptors haven't yet been flushed. So
  116. * we mark them as such and the first switch_to will do the job.
  117. */
  118. void clear_flushed_tls(struct task_struct *task)
  119. {
  120. int i;
  121. for (i = GDT_ENTRY_TLS_MIN; i < GDT_ENTRY_TLS_MAX; i++) {
  122. struct uml_tls_struct* curr =
  123. &task->thread.arch.tls_array[i - GDT_ENTRY_TLS_MIN];
  124. /*
  125. * Still correct to do this, if it wasn't present on the host it
  126. * will remain as flushed as it was.
  127. */
  128. if (!curr->present)
  129. continue;
  130. curr->flushed = 0;
  131. }
  132. }
  133. /*
  134. * In SKAS0 mode, currently, multiple guest threads sharing the same ->mm have a
  135. * common host process. So this is needed in SKAS0 too.
  136. *
  137. * However, if each thread had a different host process (and this was discussed
  138. * for SMP support) this won't be needed.
  139. *
  140. * And this will not need be used when (and if) we'll add support to the host
  141. * SKAS patch.
  142. */
  143. int arch_switch_tls(struct task_struct *to)
  144. {
  145. if (!host_supports_tls)
  146. return 0;
  147. /*
  148. * We have no need whatsoever to switch TLS for kernel threads; beyond
  149. * that, that would also result in us calling os_set_thread_area with
  150. * userspace_pid[cpu] == 0, which gives an error.
  151. */
  152. if (likely(to->mm))
  153. return load_TLS(O_FORCE, to);
  154. return 0;
  155. }
  156. static int set_tls_entry(struct task_struct* task, struct user_desc *info,
  157. int idx, int flushed)
  158. {
  159. struct thread_struct *t = &task->thread;
  160. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  161. return -EINVAL;
  162. t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].tls = *info;
  163. t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].present = 1;
  164. t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].flushed = flushed;
  165. return 0;
  166. }
  167. int arch_set_tls(struct task_struct *new, unsigned long tls)
  168. {
  169. struct user_desc info;
  170. int idx, ret = -EFAULT;
  171. if (copy_from_user(&info, (void __user *) tls, sizeof(info)))
  172. goto out;
  173. ret = -EINVAL;
  174. if (LDT_empty(&info))
  175. goto out;
  176. idx = info.entry_number;
  177. ret = set_tls_entry(new, &info, idx, 0);
  178. out:
  179. return ret;
  180. }
  181. static int get_tls_entry(struct task_struct *task, struct user_desc *info,
  182. int idx)
  183. {
  184. struct thread_struct *t = &task->thread;
  185. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  186. return -EINVAL;
  187. if (!t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].present)
  188. goto clear;
  189. *info = t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].tls;
  190. out:
  191. /*
  192. * Temporary debugging check, to make sure that things have been
  193. * flushed. This could be triggered if load_TLS() failed.
  194. */
  195. if (unlikely(task == current &&
  196. !t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].flushed)) {
  197. printk(KERN_ERR "get_tls_entry: task with pid %d got here "
  198. "without flushed TLS.", current->pid);
  199. }
  200. return 0;
  201. clear:
  202. /*
  203. * When the TLS entry has not been set, the values read to user in the
  204. * tls_array are 0 (because it's cleared at boot, see
  205. * arch/i386/kernel/head.S:cpu_gdt_table). Emulate that.
  206. */
  207. clear_user_desc(info);
  208. info->entry_number = idx;
  209. goto out;
  210. }
  211. SYSCALL_DEFINE1(set_thread_area, struct user_desc __user *, user_desc)
  212. {
  213. struct user_desc info;
  214. int idx, ret;
  215. if (!host_supports_tls)
  216. return -ENOSYS;
  217. if (copy_from_user(&info, user_desc, sizeof(info)))
  218. return -EFAULT;
  219. idx = info.entry_number;
  220. if (idx == -1) {
  221. idx = get_free_idx(current);
  222. if (idx < 0)
  223. return idx;
  224. info.entry_number = idx;
  225. /* Tell the user which slot we chose for him.*/
  226. if (put_user(idx, &user_desc->entry_number))
  227. return -EFAULT;
  228. }
  229. ret = do_set_thread_area(&info);
  230. if (ret)
  231. return ret;
  232. return set_tls_entry(current, &info, idx, 1);
  233. }
  234. /*
  235. * Perform set_thread_area on behalf of the traced child.
  236. * Note: error handling is not done on the deferred load, and this differ from
  237. * i386. However the only possible error are caused by bugs.
  238. */
  239. int ptrace_set_thread_area(struct task_struct *child, int idx,
  240. struct user_desc __user *user_desc)
  241. {
  242. struct user_desc info;
  243. if (!host_supports_tls)
  244. return -EIO;
  245. if (copy_from_user(&info, user_desc, sizeof(info)))
  246. return -EFAULT;
  247. return set_tls_entry(child, &info, idx, 0);
  248. }
  249. SYSCALL_DEFINE1(get_thread_area, struct user_desc __user *, user_desc)
  250. {
  251. struct user_desc info;
  252. int idx, ret;
  253. if (!host_supports_tls)
  254. return -ENOSYS;
  255. if (get_user(idx, &user_desc->entry_number))
  256. return -EFAULT;
  257. ret = get_tls_entry(current, &info, idx);
  258. if (ret < 0)
  259. goto out;
  260. if (copy_to_user(user_desc, &info, sizeof(info)))
  261. ret = -EFAULT;
  262. out:
  263. return ret;
  264. }
  265. /*
  266. * Perform get_thread_area on behalf of the traced child.
  267. */
  268. int ptrace_get_thread_area(struct task_struct *child, int idx,
  269. struct user_desc __user *user_desc)
  270. {
  271. struct user_desc info;
  272. int ret;
  273. if (!host_supports_tls)
  274. return -EIO;
  275. ret = get_tls_entry(child, &info, idx);
  276. if (ret < 0)
  277. goto out;
  278. if (copy_to_user(user_desc, &info, sizeof(info)))
  279. ret = -EFAULT;
  280. out:
  281. return ret;
  282. }
  283. /*
  284. * This code is really i386-only, but it detects and logs x86_64 GDT indexes
  285. * if a 32-bit UML is running on a 64-bit host.
  286. */
  287. static int __init __setup_host_supports_tls(void)
  288. {
  289. check_host_supports_tls(&host_supports_tls, &host_gdt_entry_tls_min);
  290. if (host_supports_tls) {
  291. printk(KERN_INFO "Host TLS support detected\n");
  292. printk(KERN_INFO "Detected host type: ");
  293. switch (host_gdt_entry_tls_min) {
  294. case GDT_ENTRY_TLS_MIN_I386:
  295. printk(KERN_CONT "i386");
  296. break;
  297. case GDT_ENTRY_TLS_MIN_X86_64:
  298. printk(KERN_CONT "x86_64");
  299. break;
  300. }
  301. printk(KERN_CONT " (GDT indexes %d to %d)\n",
  302. host_gdt_entry_tls_min,
  303. host_gdt_entry_tls_min + GDT_ENTRY_TLS_ENTRIES);
  304. } else
  305. printk(KERN_ERR " Host TLS support NOT detected! "
  306. "TLS support inside UML will not work\n");
  307. return 0;
  308. }
  309. __initcall(__setup_host_supports_tls);