tls_32.c 8.9 KB

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