f11c92a3faac8a2ab91f4abb26510d392c5fb7ef
[linux-3.10.git] / arch / x86 / kernel / tls.c
1 #include <linux/kernel.h>
2 #include <linux/errno.h>
3 #include <linux/sched.h>
4 #include <linux/user.h>
5
6 #include <asm/uaccess.h>
7 #include <asm/desc.h>
8 #include <asm/system.h>
9 #include <asm/ldt.h>
10 #include <asm/processor.h>
11 #include <asm/proto.h>
12
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
21         for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
22                 if (desc_empty(&t->tls_array[idx]))
23                         return idx + GDT_ENTRY_TLS_MIN;
24         return -ESRCH;
25 }
26
27 static void set_tls_desc(struct task_struct *p, int idx,
28                          const struct user_desc *info)
29 {
30         struct thread_struct *t = &p->thread;
31         struct desc_struct *desc = &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
32         int cpu;
33
34         /*
35          * We must not get preempted while modifying the TLS.
36          */
37         cpu = get_cpu();
38
39         if (LDT_empty(info))
40                 desc->a = desc->b = 0;
41         else
42                 fill_ldt(desc, info);
43
44         if (t == &current->thread)
45                 load_TLS(t, cpu);
46
47         put_cpu();
48 }
49
50 /*
51  * Set a given TLS descriptor:
52  */
53 int do_set_thread_area(struct task_struct *p, int idx,
54                        struct user_desc __user *u_info,
55                        int can_allocate)
56 {
57         struct user_desc info;
58
59         if (copy_from_user(&info, u_info, sizeof(info)))
60                 return -EFAULT;
61
62         if (idx == -1)
63                 idx = info.entry_number;
64
65         /*
66          * index -1 means the kernel should try to find and
67          * allocate an empty descriptor:
68          */
69         if (idx == -1 && can_allocate) {
70                 idx = get_free_idx();
71                 if (idx < 0)
72                         return idx;
73                 if (put_user(idx, &u_info->entry_number))
74                         return -EFAULT;
75         }
76
77         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
78                 return -EINVAL;
79
80         set_tls_desc(p, idx, &info);
81
82         return 0;
83 }
84
85 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
86 {
87         return do_set_thread_area(current, -1, u_info, 1);
88 }
89
90
91 /*
92  * Get the current Thread-Local Storage area:
93  */
94
95 static void fill_user_desc(struct user_desc *info, int idx,
96                            const struct desc_struct *desc)
97
98 {
99         memset(info, 0, sizeof(*info));
100         info->entry_number = idx;
101         info->base_addr = get_desc_base(desc);
102         info->limit = get_desc_limit(desc);
103         info->seg_32bit = desc->d;
104         info->contents = desc->type >> 2;
105         info->read_exec_only = !(desc->type & 2);
106         info->limit_in_pages = desc->g;
107         info->seg_not_present = !desc->p;
108         info->useable = desc->avl;
109 #ifdef CONFIG_X86_64
110         info->lm = desc->l;
111 #endif
112 }
113
114 int do_get_thread_area(struct task_struct *p, int idx,
115                        struct user_desc __user *u_info)
116 {
117         struct user_desc info;
118
119         if (idx == -1 && get_user(idx, &u_info->entry_number))
120                 return -EFAULT;
121
122         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
123                 return -EINVAL;
124
125         fill_user_desc(&info, idx,
126                        &p->thread.tls_array[idx - GDT_ENTRY_TLS_MIN]);
127
128         if (copy_to_user(u_info, &info, sizeof(info)))
129                 return -EFAULT;
130         return 0;
131 }
132
133 asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
134 {
135         return do_get_thread_area(current, -1, u_info);
136 }