15c48b108SAl Viro /*
25c48b108SAl Viro * Copyright (C) 2005 Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
35c48b108SAl Viro * Licensed under the GPL
45c48b108SAl Viro */
55c48b108SAl Viro
637185b33SAl Viro #include <linux/percpu.h>
737185b33SAl Viro #include <linux/sched.h>
82cf09666SAl Viro #include <linux/syscalls.h>
97c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
10da028d5eSRichard Weinberger #include <asm/ptrace-abi.h>
1137185b33SAl Viro #include <os.h>
1237185b33SAl Viro #include <skas.h>
1337185b33SAl Viro #include <sysdep/tls.h>
145c48b108SAl Viro
155c48b108SAl Viro /*
165c48b108SAl Viro * If needed we can detect when it's uninitialized.
175c48b108SAl Viro *
185c48b108SAl Viro * These are initialized in an initcall and unchanged thereafter.
195c48b108SAl Viro */
205c48b108SAl Viro static int host_supports_tls = -1;
215c48b108SAl Viro int host_gdt_entry_tls_min;
225c48b108SAl Viro
do_set_thread_area(struct user_desc * info)235c48b108SAl Viro int do_set_thread_area(struct user_desc *info)
245c48b108SAl Viro {
255c48b108SAl Viro int ret;
265c48b108SAl Viro u32 cpu;
275c48b108SAl Viro
285c48b108SAl Viro cpu = get_cpu();
295c48b108SAl Viro ret = os_set_thread_area(info, userspace_pid[cpu]);
305c48b108SAl Viro put_cpu();
315c48b108SAl Viro
325c48b108SAl Viro if (ret)
335c48b108SAl Viro printk(KERN_ERR "PTRACE_SET_THREAD_AREA failed, err = %d, "
345c48b108SAl Viro "index = %d\n", ret, info->entry_number);
355c48b108SAl Viro
365c48b108SAl Viro return ret;
375c48b108SAl Viro }
385c48b108SAl Viro
do_get_thread_area(struct user_desc * info)395c48b108SAl Viro int do_get_thread_area(struct user_desc *info)
405c48b108SAl Viro {
415c48b108SAl Viro int ret;
425c48b108SAl Viro u32 cpu;
435c48b108SAl Viro
445c48b108SAl Viro cpu = get_cpu();
455c48b108SAl Viro ret = os_get_thread_area(info, userspace_pid[cpu]);
465c48b108SAl Viro put_cpu();
475c48b108SAl Viro
485c48b108SAl Viro if (ret)
495c48b108SAl Viro printk(KERN_ERR "PTRACE_GET_THREAD_AREA failed, err = %d, "
505c48b108SAl Viro "index = %d\n", ret, info->entry_number);
515c48b108SAl Viro
525c48b108SAl Viro return ret;
535c48b108SAl Viro }
545c48b108SAl Viro
555c48b108SAl Viro /*
565c48b108SAl Viro * sys_get_thread_area: get a yet unused TLS descriptor index.
575c48b108SAl Viro * XXX: Consider leaving one free slot for glibc usage at first place. This must
585c48b108SAl Viro * be done here (and by changing GDT_ENTRY_TLS_* macros) and nowhere else.
595c48b108SAl Viro *
605c48b108SAl Viro * Also, this must be tested when compiling in SKAS mode with dynamic linking
615c48b108SAl Viro * and running against NPTL.
625c48b108SAl Viro */
get_free_idx(struct task_struct * task)635c48b108SAl Viro static int get_free_idx(struct task_struct* task)
645c48b108SAl Viro {
655c48b108SAl Viro struct thread_struct *t = &task->thread;
665c48b108SAl Viro int idx;
675c48b108SAl Viro
685c48b108SAl Viro for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
695c48b108SAl Viro if (!t->arch.tls_array[idx].present)
705c48b108SAl Viro return idx + GDT_ENTRY_TLS_MIN;
715c48b108SAl Viro return -ESRCH;
725c48b108SAl Viro }
735c48b108SAl Viro
clear_user_desc(struct user_desc * info)745c48b108SAl Viro static inline void clear_user_desc(struct user_desc* info)
755c48b108SAl Viro {
765c48b108SAl Viro /* Postcondition: LDT_empty(info) returns true. */
775c48b108SAl Viro memset(info, 0, sizeof(*info));
785c48b108SAl Viro
795c48b108SAl Viro /*
805c48b108SAl Viro * Check the LDT_empty or the i386 sys_get_thread_area code - we obtain
815c48b108SAl Viro * indeed an empty user_desc.
825c48b108SAl Viro */
835c48b108SAl Viro info->read_exec_only = 1;
845c48b108SAl Viro info->seg_not_present = 1;
855c48b108SAl Viro }
865c48b108SAl Viro
875c48b108SAl Viro #define O_FORCE 1
885c48b108SAl Viro
load_TLS(int flags,struct task_struct * to)895c48b108SAl Viro static int load_TLS(int flags, struct task_struct *to)
905c48b108SAl Viro {
915c48b108SAl Viro int ret = 0;
925c48b108SAl Viro int idx;
935c48b108SAl Viro
945c48b108SAl Viro for (idx = GDT_ENTRY_TLS_MIN; idx < GDT_ENTRY_TLS_MAX; idx++) {
955c48b108SAl Viro struct uml_tls_struct* curr =
965c48b108SAl Viro &to->thread.arch.tls_array[idx - GDT_ENTRY_TLS_MIN];
975c48b108SAl Viro
985c48b108SAl Viro /*
995c48b108SAl Viro * Actually, now if it wasn't flushed it gets cleared and
1005c48b108SAl Viro * flushed to the host, which will clear it.
1015c48b108SAl Viro */
1025c48b108SAl Viro if (!curr->present) {
1035c48b108SAl Viro if (!curr->flushed) {
1045c48b108SAl Viro clear_user_desc(&curr->tls);
1055c48b108SAl Viro curr->tls.entry_number = idx;
1065c48b108SAl Viro } else {
1075c48b108SAl Viro WARN_ON(!LDT_empty(&curr->tls));
1085c48b108SAl Viro continue;
1095c48b108SAl Viro }
1105c48b108SAl Viro }
1115c48b108SAl Viro
1125c48b108SAl Viro if (!(flags & O_FORCE) && curr->flushed)
1135c48b108SAl Viro continue;
1145c48b108SAl Viro
1155c48b108SAl Viro ret = do_set_thread_area(&curr->tls);
1165c48b108SAl Viro if (ret)
1175c48b108SAl Viro goto out;
1185c48b108SAl Viro
1195c48b108SAl Viro curr->flushed = 1;
1205c48b108SAl Viro }
1215c48b108SAl Viro out:
1225c48b108SAl Viro return ret;
1235c48b108SAl Viro }
1245c48b108SAl Viro
1255c48b108SAl Viro /*
1265c48b108SAl Viro * Verify if we need to do a flush for the new process, i.e. if there are any
1275c48b108SAl Viro * present desc's, only if they haven't been flushed.
1285c48b108SAl Viro */
needs_TLS_update(struct task_struct * task)1295c48b108SAl Viro static inline int needs_TLS_update(struct task_struct *task)
1305c48b108SAl Viro {
1315c48b108SAl Viro int i;
1325c48b108SAl Viro int ret = 0;
1335c48b108SAl Viro
1345c48b108SAl Viro for (i = GDT_ENTRY_TLS_MIN; i < GDT_ENTRY_TLS_MAX; i++) {
1355c48b108SAl Viro struct uml_tls_struct* curr =
1365c48b108SAl Viro &task->thread.arch.tls_array[i - GDT_ENTRY_TLS_MIN];
1375c48b108SAl Viro
1385c48b108SAl Viro /*
1395c48b108SAl Viro * Can't test curr->present, we may need to clear a descriptor
1405c48b108SAl Viro * which had a value.
1415c48b108SAl Viro */
1425c48b108SAl Viro if (curr->flushed)
1435c48b108SAl Viro continue;
1445c48b108SAl Viro ret = 1;
1455c48b108SAl Viro break;
1465c48b108SAl Viro }
1475c48b108SAl Viro return ret;
1485c48b108SAl Viro }
1495c48b108SAl Viro
1505c48b108SAl Viro /*
1515c48b108SAl Viro * On a newly forked process, the TLS descriptors haven't yet been flushed. So
1525c48b108SAl Viro * we mark them as such and the first switch_to will do the job.
1535c48b108SAl Viro */
clear_flushed_tls(struct task_struct * task)1545c48b108SAl Viro void clear_flushed_tls(struct task_struct *task)
1555c48b108SAl Viro {
1565c48b108SAl Viro int i;
1575c48b108SAl Viro
1585c48b108SAl Viro for (i = GDT_ENTRY_TLS_MIN; i < GDT_ENTRY_TLS_MAX; i++) {
1595c48b108SAl Viro struct uml_tls_struct* curr =
1605c48b108SAl Viro &task->thread.arch.tls_array[i - GDT_ENTRY_TLS_MIN];
1615c48b108SAl Viro
1625c48b108SAl Viro /*
1635c48b108SAl Viro * Still correct to do this, if it wasn't present on the host it
1645c48b108SAl Viro * will remain as flushed as it was.
1655c48b108SAl Viro */
1665c48b108SAl Viro if (!curr->present)
1675c48b108SAl Viro continue;
1685c48b108SAl Viro
1695c48b108SAl Viro curr->flushed = 0;
1705c48b108SAl Viro }
1715c48b108SAl Viro }
1725c48b108SAl Viro
1735c48b108SAl Viro /*
1745c48b108SAl Viro * In SKAS0 mode, currently, multiple guest threads sharing the same ->mm have a
1755c48b108SAl Viro * common host process. So this is needed in SKAS0 too.
1765c48b108SAl Viro *
1775c48b108SAl Viro * However, if each thread had a different host process (and this was discussed
1785c48b108SAl Viro * for SMP support) this won't be needed.
1795c48b108SAl Viro *
1805c48b108SAl Viro * And this will not need be used when (and if) we'll add support to the host
1815c48b108SAl Viro * SKAS patch.
1825c48b108SAl Viro */
1835c48b108SAl Viro
arch_switch_tls(struct task_struct * to)1845c48b108SAl Viro int arch_switch_tls(struct task_struct *to)
1855c48b108SAl Viro {
1865c48b108SAl Viro if (!host_supports_tls)
1875c48b108SAl Viro return 0;
1885c48b108SAl Viro
1895c48b108SAl Viro /*
1905c48b108SAl Viro * We have no need whatsoever to switch TLS for kernel threads; beyond
1915c48b108SAl Viro * that, that would also result in us calling os_set_thread_area with
1925c48b108SAl Viro * userspace_pid[cpu] == 0, which gives an error.
1935c48b108SAl Viro */
1945c48b108SAl Viro if (likely(to->mm))
1955c48b108SAl Viro return load_TLS(O_FORCE, to);
1965c48b108SAl Viro
1975c48b108SAl Viro return 0;
1985c48b108SAl Viro }
1995c48b108SAl Viro
set_tls_entry(struct task_struct * task,struct user_desc * info,int idx,int flushed)2005c48b108SAl Viro static int set_tls_entry(struct task_struct* task, struct user_desc *info,
2015c48b108SAl Viro int idx, int flushed)
2025c48b108SAl Viro {
2035c48b108SAl Viro struct thread_struct *t = &task->thread;
2045c48b108SAl Viro
2055c48b108SAl Viro if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
2065c48b108SAl Viro return -EINVAL;
2075c48b108SAl Viro
2085c48b108SAl Viro t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].tls = *info;
2095c48b108SAl Viro t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].present = 1;
2105c48b108SAl Viro t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].flushed = flushed;
2115c48b108SAl Viro
2125c48b108SAl Viro return 0;
2135c48b108SAl Viro }
2145c48b108SAl Viro
arch_set_tls(struct task_struct * new,unsigned long tls)215*457677c7SAmanieu d'Antras int arch_set_tls(struct task_struct *new, unsigned long tls)
2165c48b108SAl Viro {
2175c48b108SAl Viro struct user_desc info;
2185c48b108SAl Viro int idx, ret = -EFAULT;
2195c48b108SAl Viro
220*457677c7SAmanieu d'Antras if (copy_from_user(&info, (void __user *) tls, sizeof(info)))
2215c48b108SAl Viro goto out;
2225c48b108SAl Viro
2235c48b108SAl Viro ret = -EINVAL;
2245c48b108SAl Viro if (LDT_empty(&info))
2255c48b108SAl Viro goto out;
2265c48b108SAl Viro
2275c48b108SAl Viro idx = info.entry_number;
2285c48b108SAl Viro
2295c48b108SAl Viro ret = set_tls_entry(new, &info, idx, 0);
2305c48b108SAl Viro out:
2315c48b108SAl Viro return ret;
2325c48b108SAl Viro }
2335c48b108SAl Viro
2345c48b108SAl Viro /* XXX: use do_get_thread_area to read the host value? I'm not at all sure! */
get_tls_entry(struct task_struct * task,struct user_desc * info,int idx)2355c48b108SAl Viro static int get_tls_entry(struct task_struct *task, struct user_desc *info,
2365c48b108SAl Viro int idx)
2375c48b108SAl Viro {
2385c48b108SAl Viro struct thread_struct *t = &task->thread;
2395c48b108SAl Viro
2405c48b108SAl Viro if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
2415c48b108SAl Viro return -EINVAL;
2425c48b108SAl Viro
2435c48b108SAl Viro if (!t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].present)
2445c48b108SAl Viro goto clear;
2455c48b108SAl Viro
2465c48b108SAl Viro *info = t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].tls;
2475c48b108SAl Viro
2485c48b108SAl Viro out:
2495c48b108SAl Viro /*
2505c48b108SAl Viro * Temporary debugging check, to make sure that things have been
2515c48b108SAl Viro * flushed. This could be triggered if load_TLS() failed.
2525c48b108SAl Viro */
2535c48b108SAl Viro if (unlikely(task == current &&
2545c48b108SAl Viro !t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].flushed)) {
2555c48b108SAl Viro printk(KERN_ERR "get_tls_entry: task with pid %d got here "
2565c48b108SAl Viro "without flushed TLS.", current->pid);
2575c48b108SAl Viro }
2585c48b108SAl Viro
2595c48b108SAl Viro return 0;
2605c48b108SAl Viro clear:
2615c48b108SAl Viro /*
2625c48b108SAl Viro * When the TLS entry has not been set, the values read to user in the
2635c48b108SAl Viro * tls_array are 0 (because it's cleared at boot, see
2645c48b108SAl Viro * arch/i386/kernel/head.S:cpu_gdt_table). Emulate that.
2655c48b108SAl Viro */
2665c48b108SAl Viro clear_user_desc(info);
2675c48b108SAl Viro info->entry_number = idx;
2685c48b108SAl Viro goto out;
2695c48b108SAl Viro }
2705c48b108SAl Viro
SYSCALL_DEFINE1(set_thread_area,struct user_desc __user *,user_desc)2712cf09666SAl Viro SYSCALL_DEFINE1(set_thread_area, struct user_desc __user *, user_desc)
2725c48b108SAl Viro {
2735c48b108SAl Viro struct user_desc info;
2745c48b108SAl Viro int idx, ret;
2755c48b108SAl Viro
2765c48b108SAl Viro if (!host_supports_tls)
2775c48b108SAl Viro return -ENOSYS;
2785c48b108SAl Viro
2795c48b108SAl Viro if (copy_from_user(&info, user_desc, sizeof(info)))
2805c48b108SAl Viro return -EFAULT;
2815c48b108SAl Viro
2825c48b108SAl Viro idx = info.entry_number;
2835c48b108SAl Viro
2845c48b108SAl Viro if (idx == -1) {
2855c48b108SAl Viro idx = get_free_idx(current);
2865c48b108SAl Viro if (idx < 0)
2875c48b108SAl Viro return idx;
2885c48b108SAl Viro info.entry_number = idx;
2895c48b108SAl Viro /* Tell the user which slot we chose for him.*/
2905c48b108SAl Viro if (put_user(idx, &user_desc->entry_number))
2915c48b108SAl Viro return -EFAULT;
2925c48b108SAl Viro }
2935c48b108SAl Viro
2945c48b108SAl Viro ret = do_set_thread_area(&info);
2955c48b108SAl Viro if (ret)
2965c48b108SAl Viro return ret;
2975c48b108SAl Viro return set_tls_entry(current, &info, idx, 1);
2985c48b108SAl Viro }
2995c48b108SAl Viro
3005c48b108SAl Viro /*
3015c48b108SAl Viro * Perform set_thread_area on behalf of the traced child.
3025c48b108SAl Viro * Note: error handling is not done on the deferred load, and this differ from
3035c48b108SAl Viro * i386. However the only possible error are caused by bugs.
3045c48b108SAl Viro */
ptrace_set_thread_area(struct task_struct * child,int idx,struct user_desc __user * user_desc)3055c48b108SAl Viro int ptrace_set_thread_area(struct task_struct *child, int idx,
3065c48b108SAl Viro struct user_desc __user *user_desc)
3075c48b108SAl Viro {
3085c48b108SAl Viro struct user_desc info;
3095c48b108SAl Viro
3105c48b108SAl Viro if (!host_supports_tls)
3115c48b108SAl Viro return -EIO;
3125c48b108SAl Viro
3135c48b108SAl Viro if (copy_from_user(&info, user_desc, sizeof(info)))
3145c48b108SAl Viro return -EFAULT;
3155c48b108SAl Viro
3165c48b108SAl Viro return set_tls_entry(child, &info, idx, 0);
3175c48b108SAl Viro }
3185c48b108SAl Viro
SYSCALL_DEFINE1(get_thread_area,struct user_desc __user *,user_desc)3192cf09666SAl Viro SYSCALL_DEFINE1(get_thread_area, struct user_desc __user *, user_desc)
3205c48b108SAl Viro {
3215c48b108SAl Viro struct user_desc info;
3225c48b108SAl Viro int idx, ret;
3235c48b108SAl Viro
3245c48b108SAl Viro if (!host_supports_tls)
3255c48b108SAl Viro return -ENOSYS;
3265c48b108SAl Viro
3275c48b108SAl Viro if (get_user(idx, &user_desc->entry_number))
3285c48b108SAl Viro return -EFAULT;
3295c48b108SAl Viro
3305c48b108SAl Viro ret = get_tls_entry(current, &info, idx);
3315c48b108SAl Viro if (ret < 0)
3325c48b108SAl Viro goto out;
3335c48b108SAl Viro
3345c48b108SAl Viro if (copy_to_user(user_desc, &info, sizeof(info)))
3355c48b108SAl Viro ret = -EFAULT;
3365c48b108SAl Viro
3375c48b108SAl Viro out:
3385c48b108SAl Viro return ret;
3395c48b108SAl Viro }
3405c48b108SAl Viro
3415c48b108SAl Viro /*
3425c48b108SAl Viro * Perform get_thread_area on behalf of the traced child.
3435c48b108SAl Viro */
ptrace_get_thread_area(struct task_struct * child,int idx,struct user_desc __user * user_desc)3445c48b108SAl Viro int ptrace_get_thread_area(struct task_struct *child, int idx,
3455c48b108SAl Viro struct user_desc __user *user_desc)
3465c48b108SAl Viro {
3475c48b108SAl Viro struct user_desc info;
3485c48b108SAl Viro int ret;
3495c48b108SAl Viro
3505c48b108SAl Viro if (!host_supports_tls)
3515c48b108SAl Viro return -EIO;
3525c48b108SAl Viro
3535c48b108SAl Viro ret = get_tls_entry(child, &info, idx);
3545c48b108SAl Viro if (ret < 0)
3555c48b108SAl Viro goto out;
3565c48b108SAl Viro
3575c48b108SAl Viro if (copy_to_user(user_desc, &info, sizeof(info)))
3585c48b108SAl Viro ret = -EFAULT;
3595c48b108SAl Viro out:
3605c48b108SAl Viro return ret;
3615c48b108SAl Viro }
3625c48b108SAl Viro
3635c48b108SAl Viro /*
3645c48b108SAl Viro * This code is really i386-only, but it detects and logs x86_64 GDT indexes
3655c48b108SAl Viro * if a 32-bit UML is running on a 64-bit host.
3665c48b108SAl Viro */
__setup_host_supports_tls(void)3675c48b108SAl Viro static int __init __setup_host_supports_tls(void)
3685c48b108SAl Viro {
3695c48b108SAl Viro check_host_supports_tls(&host_supports_tls, &host_gdt_entry_tls_min);
3705c48b108SAl Viro if (host_supports_tls) {
3715c48b108SAl Viro printk(KERN_INFO "Host TLS support detected\n");
3725c48b108SAl Viro printk(KERN_INFO "Detected host type: ");
3735c48b108SAl Viro switch (host_gdt_entry_tls_min) {
3745c48b108SAl Viro case GDT_ENTRY_TLS_MIN_I386:
3755c48b108SAl Viro printk(KERN_CONT "i386");
3765c48b108SAl Viro break;
3775c48b108SAl Viro case GDT_ENTRY_TLS_MIN_X86_64:
3785c48b108SAl Viro printk(KERN_CONT "x86_64");
3795c48b108SAl Viro break;
3805c48b108SAl Viro }
3815c48b108SAl Viro printk(KERN_CONT " (GDT indexes %d to %d)\n",
3825c48b108SAl Viro host_gdt_entry_tls_min,
3835c48b108SAl Viro host_gdt_entry_tls_min + GDT_ENTRY_TLS_ENTRIES);
3845c48b108SAl Viro } else
3855c48b108SAl Viro printk(KERN_ERR " Host TLS support NOT detected! "
3865c48b108SAl Viro "TLS support inside UML will not work\n");
3875c48b108SAl Viro return 0;
3885c48b108SAl Viro }
3895c48b108SAl Viro
3905c48b108SAl Viro __initcall(__setup_host_supports_tls);
391