7af73380ae4bc8938f6fec07e026b7730f9aa32e
[pandora-kernel.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 #include <linux/regset.h>
6
7 #include <asm/uaccess.h>
8 #include <asm/desc.h>
9 #include <asm/system.h>
10 #include <asm/ldt.h>
11 #include <asm/processor.h>
12 #include <asm/proto.h>
13 #include <asm/syscalls.h>
14
15 #include "tls.h"
16
17 /*
18  * sys_alloc_thread_area: get a yet unused TLS descriptor index.
19  */
20 static int get_free_idx(void)
21 {
22         struct thread_struct *t = &current->thread;
23         int idx;
24
25         for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
26                 if (desc_empty(&t->tls_array[idx]))
27                         return idx + GDT_ENTRY_TLS_MIN;
28         return -ESRCH;
29 }
30
31 static bool tls_desc_okay(const struct user_desc *info)
32 {
33         if (LDT_empty(info))
34                 return true;
35
36         /*
37          * espfix is required for 16-bit data segments, but espfix
38          * only works for LDT segments.
39          */
40         if (!info->seg_32bit)
41                 return false;
42
43         return true;
44 }
45
46 static void set_tls_desc(struct task_struct *p, int idx,
47                          const struct user_desc *info, int n)
48 {
49         struct thread_struct *t = &p->thread;
50         struct desc_struct *desc = &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
51         int cpu;
52
53         /*
54          * We must not get preempted while modifying the TLS.
55          */
56         cpu = get_cpu();
57
58         while (n-- > 0) {
59                 if (LDT_empty(info))
60                         desc->a = desc->b = 0;
61                 else
62                         fill_ldt(desc, info);
63                 ++info;
64                 ++desc;
65         }
66
67         if (t == &current->thread)
68                 load_TLS(t, cpu);
69
70         put_cpu();
71 }
72
73 /*
74  * Set a given TLS descriptor:
75  */
76 int do_set_thread_area(struct task_struct *p, int idx,
77                        struct user_desc __user *u_info,
78                        int can_allocate)
79 {
80         struct user_desc info;
81
82         if (copy_from_user(&info, u_info, sizeof(info)))
83                 return -EFAULT;
84
85         if (!tls_desc_okay(&info))
86                 return -EINVAL;
87
88         if (idx == -1)
89                 idx = info.entry_number;
90
91         /*
92          * index -1 means the kernel should try to find and
93          * allocate an empty descriptor:
94          */
95         if (idx == -1 && can_allocate) {
96                 idx = get_free_idx();
97                 if (idx < 0)
98                         return idx;
99                 if (put_user(idx, &u_info->entry_number))
100                         return -EFAULT;
101         }
102
103         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
104                 return -EINVAL;
105
106         set_tls_desc(p, idx, &info, 1);
107
108         return 0;
109 }
110
111 asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
112 {
113         int ret = do_set_thread_area(current, -1, u_info, 1);
114         asmlinkage_protect(1, ret, u_info);
115         return ret;
116 }
117
118
119 /*
120  * Get the current Thread-Local Storage area:
121  */
122
123 static void fill_user_desc(struct user_desc *info, int idx,
124                            const struct desc_struct *desc)
125
126 {
127         memset(info, 0, sizeof(*info));
128         info->entry_number = idx;
129         info->base_addr = get_desc_base(desc);
130         info->limit = get_desc_limit(desc);
131         info->seg_32bit = desc->d;
132         info->contents = desc->type >> 2;
133         info->read_exec_only = !(desc->type & 2);
134         info->limit_in_pages = desc->g;
135         info->seg_not_present = !desc->p;
136         info->useable = desc->avl;
137 #ifdef CONFIG_X86_64
138         info->lm = desc->l;
139 #endif
140 }
141
142 int do_get_thread_area(struct task_struct *p, int idx,
143                        struct user_desc __user *u_info)
144 {
145         struct user_desc info;
146
147         if (idx == -1 && get_user(idx, &u_info->entry_number))
148                 return -EFAULT;
149
150         if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
151                 return -EINVAL;
152
153         fill_user_desc(&info, idx,
154                        &p->thread.tls_array[idx - GDT_ENTRY_TLS_MIN]);
155
156         if (copy_to_user(u_info, &info, sizeof(info)))
157                 return -EFAULT;
158         return 0;
159 }
160
161 asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
162 {
163         int ret = do_get_thread_area(current, -1, u_info);
164         asmlinkage_protect(1, ret, u_info);
165         return ret;
166 }
167
168 int regset_tls_active(struct task_struct *target,
169                       const struct user_regset *regset)
170 {
171         struct thread_struct *t = &target->thread;
172         int n = GDT_ENTRY_TLS_ENTRIES;
173         while (n > 0 && desc_empty(&t->tls_array[n - 1]))
174                 --n;
175         return n;
176 }
177
178 int regset_tls_get(struct task_struct *target, const struct user_regset *regset,
179                    unsigned int pos, unsigned int count,
180                    void *kbuf, void __user *ubuf)
181 {
182         const struct desc_struct *tls;
183
184         if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
185             (pos % sizeof(struct user_desc)) != 0 ||
186             (count % sizeof(struct user_desc)) != 0)
187                 return -EINVAL;
188
189         pos /= sizeof(struct user_desc);
190         count /= sizeof(struct user_desc);
191
192         tls = &target->thread.tls_array[pos];
193
194         if (kbuf) {
195                 struct user_desc *info = kbuf;
196                 while (count-- > 0)
197                         fill_user_desc(info++, GDT_ENTRY_TLS_MIN + pos++,
198                                        tls++);
199         } else {
200                 struct user_desc __user *u_info = ubuf;
201                 while (count-- > 0) {
202                         struct user_desc info;
203                         fill_user_desc(&info, GDT_ENTRY_TLS_MIN + pos++, tls++);
204                         if (__copy_to_user(u_info++, &info, sizeof(info)))
205                                 return -EFAULT;
206                 }
207         }
208
209         return 0;
210 }
211
212 int regset_tls_set(struct task_struct *target, const struct user_regset *regset,
213                    unsigned int pos, unsigned int count,
214                    const void *kbuf, const void __user *ubuf)
215 {
216         struct user_desc infobuf[GDT_ENTRY_TLS_ENTRIES];
217         const struct user_desc *info;
218         int i;
219
220         if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
221             (pos % sizeof(struct user_desc)) != 0 ||
222             (count % sizeof(struct user_desc)) != 0)
223                 return -EINVAL;
224
225         if (kbuf)
226                 info = kbuf;
227         else if (__copy_from_user(infobuf, ubuf, count))
228                 return -EFAULT;
229         else
230                 info = infobuf;
231
232         for (i = 0; i < count / sizeof(struct user_desc); i++)
233                 if (!tls_desc_okay(info + i))
234                         return -EINVAL;
235
236         set_tls_desc(target,
237                      GDT_ENTRY_TLS_MIN + (pos / sizeof(struct user_desc)),
238                      info, count / sizeof(struct user_desc));
239
240         return 0;
241 }