[PATCH] uml: add arch_switch_to for newly forked thread
[linux-2.6.22.y-op.git] / arch / um / sys-i386 / tls.c
blob2251654c6b45b7c8b1423406e74dcb979ba20dbd
1 /*
2 * Copyright (C) 2005 Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
3 * Licensed under the GPL
4 */
6 #include "linux/config.h"
7 #include "linux/kernel.h"
8 #include "linux/sched.h"
9 #include "linux/slab.h"
10 #include "linux/types.h"
11 #include "asm/uaccess.h"
12 #include "asm/ptrace.h"
13 #include "asm/segment.h"
14 #include "asm/smp.h"
15 #include "asm/desc.h"
16 #include "choose-mode.h"
17 #include "kern.h"
18 #include "kern_util.h"
19 #include "mode_kern.h"
20 #include "os.h"
21 #include "mode.h"
23 #ifdef CONFIG_MODE_SKAS
24 #include "skas.h"
25 #endif
27 #ifdef CONFIG_MODE_SKAS
28 int do_set_thread_area_skas(struct user_desc *info)
30 int ret;
31 u32 cpu;
33 cpu = get_cpu();
34 ret = os_set_thread_area(info, userspace_pid[cpu]);
35 put_cpu();
36 return ret;
39 int do_get_thread_area_skas(struct user_desc *info)
41 int ret;
42 u32 cpu;
44 cpu = get_cpu();
45 ret = os_get_thread_area(info, userspace_pid[cpu]);
46 put_cpu();
47 return ret;
49 #endif
52 * sys_get_thread_area: get a yet unused TLS descriptor index.
53 * XXX: Consider leaving one free slot for glibc usage at first place. This must
54 * be done here (and by changing GDT_ENTRY_TLS_* macros) and nowhere else.
56 * Also, this must be tested when compiling in SKAS mode with dinamic linking
57 * and running against NPTL.
59 static int get_free_idx(struct task_struct* task)
61 struct thread_struct *t = &task->thread;
62 int idx;
64 if (!t->arch.tls_array)
65 return GDT_ENTRY_TLS_MIN;
67 for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
68 if (!t->arch.tls_array[idx].present)
69 return idx + GDT_ENTRY_TLS_MIN;
70 return -ESRCH;
73 static inline void clear_user_desc(struct user_desc* info)
75 /* Postcondition: LDT_empty(info) returns true. */
76 memset(info, 0, sizeof(*info));
78 /* Check the LDT_empty or the i386 sys_get_thread_area code - we obtain
79 * indeed an empty user_desc.
81 info->read_exec_only = 1;
82 info->seg_not_present = 1;
85 #define O_FORCE 1
87 static int load_TLS(int flags, struct task_struct *to)
89 int ret = 0;
90 int idx;
92 for (idx = GDT_ENTRY_TLS_MIN; idx < GDT_ENTRY_TLS_MAX; idx++) {
93 struct uml_tls_struct* curr = &to->thread.arch.tls_array[idx - GDT_ENTRY_TLS_MIN];
95 /* Actually, now if it wasn't flushed it gets cleared and
96 * flushed to the host, which will clear it.*/
97 if (!curr->present) {
98 if (!curr->flushed) {
99 clear_user_desc(&curr->tls);
100 curr->tls.entry_number = idx;
101 } else {
102 WARN_ON(!LDT_empty(&curr->tls));
103 continue;
107 if (!(flags & O_FORCE) && curr->flushed)
108 continue;
110 ret = do_set_thread_area(&curr->tls);
111 if (ret)
112 goto out;
114 curr->flushed = 1;
116 out:
117 return ret;
120 /* Verify if we need to do a flush for the new process, i.e. if there are any
121 * present desc's, only if they haven't been flushed.
123 static inline int needs_TLS_update(struct task_struct *task)
125 int i;
126 int ret = 0;
128 for (i = GDT_ENTRY_TLS_MIN; i < GDT_ENTRY_TLS_MAX; i++) {
129 struct uml_tls_struct* curr = &task->thread.arch.tls_array[i - GDT_ENTRY_TLS_MIN];
131 /* Can't test curr->present, we may need to clear a descriptor
132 * which had a value. */
133 if (curr->flushed)
134 continue;
135 ret = 1;
136 break;
138 return ret;
141 /* On a newly forked process, the TLS descriptors haven't yet been flushed. So
142 * we mark them as such and the first switch_to will do the job.
144 void clear_flushed_tls(struct task_struct *task)
146 int i;
148 for (i = GDT_ENTRY_TLS_MIN; i < GDT_ENTRY_TLS_MAX; i++) {
149 struct uml_tls_struct* curr = &task->thread.arch.tls_array[i - GDT_ENTRY_TLS_MIN];
151 /* Still correct to do this, if it wasn't present on the host it
152 * will remain as flushed as it was. */
153 if (!curr->present)
154 continue;
156 curr->flushed = 0;
160 /* This in SKAS0 does not need to be used, since we have different host
161 * processes. Nor will this need to be used when we'll add support to the host
162 * SKAS patch. */
163 int arch_switch_tls_skas(struct task_struct *from, struct task_struct *to)
165 /* We have no need whatsoever to switch TLS for kernel threads; beyond
166 * that, that would also result in us calling os_set_thread_area with
167 * userspace_pid[cpu] == 0, which gives an error. */
168 if (likely(to->mm))
169 return load_TLS(O_FORCE, to);
171 return 0;
174 int arch_switch_tls_tt(struct task_struct *from, struct task_struct *to)
176 if (needs_TLS_update(to))
177 return load_TLS(0, to);
179 return 0;
182 static int set_tls_entry(struct task_struct* task, struct user_desc *info,
183 int idx, int flushed)
185 struct thread_struct *t = &task->thread;
187 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
188 return -EINVAL;
190 t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].tls = *info;
191 t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].present = 1;
192 t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].flushed = flushed;
194 return 0;
197 int arch_copy_tls(struct task_struct *new)
199 struct user_desc info;
200 int idx, ret = -EFAULT;
202 if (copy_from_user(&info,
203 (void __user *) UPT_ESI(&new->thread.regs.regs),
204 sizeof(info)))
205 goto out;
207 ret = -EINVAL;
208 if (LDT_empty(&info))
209 goto out;
211 idx = info.entry_number;
213 ret = set_tls_entry(new, &info, idx, 0);
214 out:
215 return ret;
218 /* XXX: use do_get_thread_area to read the host value? I'm not at all sure! */
219 static int get_tls_entry(struct task_struct* task, struct user_desc *info, int idx)
221 struct thread_struct *t = &task->thread;
223 if (!t->arch.tls_array)
224 goto clear;
226 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
227 return -EINVAL;
229 if (!t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].present)
230 goto clear;
232 *info = t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].tls;
234 out:
235 /* Temporary debugging check, to make sure that things have been
236 * flushed. This could be triggered if load_TLS() failed.
238 if (unlikely(task == current && !t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].flushed)) {
239 printk(KERN_ERR "get_tls_entry: task with pid %d got here "
240 "without flushed TLS.", current->pid);
243 return 0;
244 clear:
245 /* When the TLS entry has not been set, the values read to user in the
246 * tls_array are 0 (because it's cleared at boot, see
247 * arch/i386/kernel/head.S:cpu_gdt_table). Emulate that.
249 clear_user_desc(info);
250 info->entry_number = idx;
251 goto out;
254 asmlinkage int sys_set_thread_area(struct user_desc __user *user_desc)
256 struct user_desc info;
257 int idx, ret;
259 if (copy_from_user(&info, user_desc, sizeof(info)))
260 return -EFAULT;
262 idx = info.entry_number;
264 if (idx == -1) {
265 idx = get_free_idx(current);
266 if (idx < 0)
267 return idx;
268 info.entry_number = idx;
269 /* Tell the user which slot we chose for him.*/
270 if (put_user(idx, &user_desc->entry_number))
271 return -EFAULT;
274 ret = CHOOSE_MODE_PROC(do_set_thread_area_tt, do_set_thread_area_skas, &info);
275 if (ret)
276 return ret;
277 return set_tls_entry(current, &info, idx, 1);
281 * Perform set_thread_area on behalf of the traced child.
282 * Note: error handling is not done on the deferred load, and this differ from
283 * i386. However the only possible error are caused by bugs.
285 int ptrace_set_thread_area(struct task_struct *child, int idx,
286 struct user_desc __user *user_desc)
288 struct user_desc info;
290 if (copy_from_user(&info, user_desc, sizeof(info)))
291 return -EFAULT;
293 return set_tls_entry(child, &info, idx, 0);
296 asmlinkage int sys_get_thread_area(struct user_desc __user *user_desc)
298 struct user_desc info;
299 int idx, ret;
301 if (get_user(idx, &user_desc->entry_number))
302 return -EFAULT;
304 ret = get_tls_entry(current, &info, idx);
305 if (ret < 0)
306 goto out;
308 if (copy_to_user(user_desc, &info, sizeof(info)))
309 ret = -EFAULT;
311 out:
312 return ret;
316 * Perform get_thread_area on behalf of the traced child.
318 int ptrace_get_thread_area(struct task_struct *child, int idx,
319 struct user_desc __user *user_desc)
321 struct user_desc info;
322 int ret;
324 ret = get_tls_entry(child, &info, idx);
325 if (ret < 0)
326 goto out;
328 if (copy_to_user(user_desc, &info, sizeof(info)))
329 ret = -EFAULT;
330 out:
331 return ret;