2 * Copyright (C) 2005 Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
3 * Licensed under the GPL
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"
15 #include "choose-mode.h"
17 #include "kern_util.h"
18 #include "mode_kern.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
)
37 ret
= os_set_thread_area(info
, userspace_pid
[cpu
]);
42 int do_get_thread_area_skas(struct user_desc
*info
)
48 ret
= os_get_thread_area(info
, userspace_pid
[cpu
]);
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
;
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
;
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;
89 static int load_TLS(int flags
, struct task_struct
*to
)
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.*/
100 if (!curr
->flushed
) {
101 clear_user_desc(&curr
->tls
);
102 curr
->tls
.entry_number
= idx
;
104 WARN_ON(!LDT_empty(&curr
->tls
));
109 if (!(flags
& O_FORCE
) && curr
->flushed
)
112 ret
= do_set_thread_area(&curr
->tls
);
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
)
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. */
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
)
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. */
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
171 int arch_switch_tls_skas(struct task_struct
*from
, struct task_struct
*to
)
173 if (!host_supports_tls
)
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. */
180 return load_TLS(O_FORCE
, to
);
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
)
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
;
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
),
211 if (LDT_empty(&info
))
214 idx
= info
.entry_number
;
216 ret
= set_tls_entry(new, &info
, idx
, 0);
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
)
229 if (idx
< GDT_ENTRY_TLS_MIN
|| idx
> GDT_ENTRY_TLS_MAX
)
232 if (!t
->arch
.tls_array
[idx
- GDT_ENTRY_TLS_MIN
].present
)
235 *info
= t
->arch
.tls_array
[idx
- GDT_ENTRY_TLS_MIN
].tls
;
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
);
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
;
257 asmlinkage
int sys_set_thread_area(struct user_desc __user
*user_desc
)
259 struct user_desc info
;
262 if (!host_supports_tls
)
265 if (copy_from_user(&info
, user_desc
, sizeof(info
)))
268 idx
= info
.entry_number
;
271 idx
= get_free_idx(current
);
274 info
.entry_number
= idx
;
275 /* Tell the user which slot we chose for him.*/
276 if (put_user(idx
, &user_desc
->entry_number
))
280 ret
= CHOOSE_MODE_PROC(do_set_thread_area_tt
, do_set_thread_area_skas
, &info
);
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
)
299 if (copy_from_user(&info
, user_desc
, sizeof(info
)))
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
;
310 if (!host_supports_tls
)
313 if (get_user(idx
, &user_desc
->entry_number
))
316 ret
= get_tls_entry(current
, &info
, idx
);
320 if (copy_to_user(user_desc
, &info
, sizeof(info
)))
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
;
336 if (!host_supports_tls
)
339 ret
= get_tls_entry(child
, &info
, idx
);
343 if (copy_to_user(user_desc
, &info
, sizeof(info
)))
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
:
362 case GDT_ENTRY_TLS_MIN_X86_64
:
367 printk(KERN_ERR
" Host TLS support NOT detected! "
368 "TLS support inside UML will not work\n");
372 __initcall(__setup_host_supports_tls
);