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