uml: throw out CONFIG_MODE_TT
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / um / sys-i386 / tls.c
blob0340b96d101b7ad2431102c53e649b9f550af17a
1 /*
2 * Copyright (C) 2005 Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
3 * Licensed under the GPL
4 */
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"
24 * If needed we can detect when it's uninitialized.
26 * These are initialized in an initcall and unchanged thereafter.
28 static int host_supports_tls = -1;
29 int host_gdt_entry_tls_min;
31 int do_set_thread_area_skas(struct user_desc *info)
33 int ret;
34 u32 cpu;
36 cpu = get_cpu();
37 ret = os_set_thread_area(info, userspace_pid[cpu]);
38 put_cpu();
39 return ret;
42 int do_get_thread_area_skas(struct user_desc *info)
44 int ret;
45 u32 cpu;
47 cpu = get_cpu();
48 ret = os_get_thread_area(info, userspace_pid[cpu]);
49 put_cpu();
50 return ret;
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.
58 * Also, this must be tested when compiling in SKAS mode with dinamic linking
59 * and running against NPTL.
61 static int get_free_idx(struct task_struct* task)
63 struct thread_struct *t = &task->thread;
64 int idx;
66 if (!t->arch.tls_array)
67 return GDT_ENTRY_TLS_MIN;
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;
75 static inline void clear_user_desc(struct user_desc* info)
77 /* Postcondition: LDT_empty(info) returns true. */
78 memset(info, 0, sizeof(*info));
80 /* Check the LDT_empty or the i386 sys_get_thread_area code - we obtain
81 * indeed an empty user_desc.
83 info->read_exec_only = 1;
84 info->seg_not_present = 1;
87 #define O_FORCE 1
89 static int load_TLS(int flags, struct task_struct *to)
91 int ret = 0;
92 int idx;
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];
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;
109 if (!(flags & O_FORCE) && curr->flushed)
110 continue;
112 ret = do_set_thread_area(&curr->tls);
113 if (ret)
114 goto out;
116 curr->flushed = 1;
118 out:
119 return ret;
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.
125 static inline int needs_TLS_update(struct task_struct *task)
127 int i;
128 int ret = 0;
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];
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;
140 return ret;
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.
146 void clear_flushed_tls(struct task_struct *task)
148 int i;
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];
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;
158 curr->flushed = 0;
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.
165 * However, if each thread had a different host process (and this was discussed
166 * for SMP support) this won't be needed.
168 * And this will not need be used when (and if) we'll add support to the host
169 * SKAS patch. */
171 int arch_switch_tls_skas(struct task_struct *from, struct task_struct *to)
173 if (!host_supports_tls)
174 return 0;
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);
182 return 0;
185 static int set_tls_entry(struct task_struct* task, struct user_desc *info,
186 int idx, int flushed)
188 struct thread_struct *t = &task->thread;
190 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
191 return -EINVAL;
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;
197 return 0;
200 int arch_copy_tls(struct task_struct *new)
202 struct user_desc info;
203 int idx, ret = -EFAULT;
205 if (copy_from_user(&info,
206 (void __user *) UPT_ESI(&new->thread.regs.regs),
207 sizeof(info)))
208 goto out;
210 ret = -EINVAL;
211 if (LDT_empty(&info))
212 goto out;
214 idx = info.entry_number;
216 ret = set_tls_entry(new, &info, idx, 0);
217 out:
218 return ret;
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)
224 struct thread_struct *t = &task->thread;
226 if (!t->arch.tls_array)
227 goto clear;
229 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
230 return -EINVAL;
232 if (!t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].present)
233 goto clear;
235 *info = t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].tls;
237 out:
238 /* Temporary debugging check, to make sure that things have been
239 * flushed. This could be triggered if load_TLS() failed.
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);
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.
252 clear_user_desc(info);
253 info->entry_number = idx;
254 goto out;
257 asmlinkage int sys_set_thread_area(struct user_desc __user *user_desc)
259 struct user_desc info;
260 int idx, ret;
262 if (!host_supports_tls)
263 return -ENOSYS;
265 if (copy_from_user(&info, user_desc, sizeof(info)))
266 return -EFAULT;
268 idx = info.entry_number;
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;
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);
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.
291 int ptrace_set_thread_area(struct task_struct *child, int idx,
292 struct user_desc __user *user_desc)
294 struct user_desc info;
296 if (!host_supports_tls)
297 return -EIO;
299 if (copy_from_user(&info, user_desc, sizeof(info)))
300 return -EFAULT;
302 return set_tls_entry(child, &info, idx, 0);
305 asmlinkage int sys_get_thread_area(struct user_desc __user *user_desc)
307 struct user_desc info;
308 int idx, ret;
310 if (!host_supports_tls)
311 return -ENOSYS;
313 if (get_user(idx, &user_desc->entry_number))
314 return -EFAULT;
316 ret = get_tls_entry(current, &info, idx);
317 if (ret < 0)
318 goto out;
320 if (copy_to_user(user_desc, &info, sizeof(info)))
321 ret = -EFAULT;
323 out:
324 return ret;
328 * Perform get_thread_area on behalf of the traced child.
330 int ptrace_get_thread_area(struct task_struct *child, int idx,
331 struct user_desc __user *user_desc)
333 struct user_desc info;
334 int ret;
336 if (!host_supports_tls)
337 return -EIO;
339 ret = get_tls_entry(child, &info, idx);
340 if (ret < 0)
341 goto out;
343 if (copy_to_user(user_desc, &info, sizeof(info)))
344 ret = -EFAULT;
345 out:
346 return ret;
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)
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;
366 } else
367 printk(KERN_ERR " Host TLS support NOT detected! "
368 "TLS support inside UML will not work\n");
369 return 0;
372 __initcall(__setup_host_supports_tls);