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