diff options
author | Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it> | 2006-03-31 05:30:22 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@g5.osdl.org> | 2006-03-31 15:18:52 -0500 |
commit | aa6758d4867cd07bd76105ade6177fe6148e559a (patch) | |
tree | 0053f855c885a6dc322337468a2c45b6b2f8ddd0 /arch/um/sys-i386/tls.c | |
parent | 972410b0232e97609fcefc8e408fe3037fcd607b (diff) |
[PATCH] uml: implement {get,set}_thread_area for i386
Implement sys_[gs]et_thread_area and the corresponding ptrace operations for
UML. This is the main chunk, additional parts follow. This implementation is
now well tested and has run reliably for some time, and we've understood all
the previously existing problems.
Their implementation saves the new GDT content and then forwards the call to
the host when appropriate, i.e. immediately when the target process is
running or on context switch otherwise (i.e. on fork and on ptrace() calls).
In SKAS mode, we must switch registers on each context switch (because SKAS
does not switches tls_array together with current->mm).
Also, added get_cpu() locking; this has been done for SKAS mode, since TT does
not need it (it does not use smp_processor_id()).
Signed-off-by: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Acked-by: Jeff Dike <jdike@addtoit.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'arch/um/sys-i386/tls.c')
-rw-r--r-- | arch/um/sys-i386/tls.c | 326 |
1 files changed, 326 insertions, 0 deletions
diff --git a/arch/um/sys-i386/tls.c b/arch/um/sys-i386/tls.c new file mode 100644 index 000000000000..e3c5bc593fae --- /dev/null +++ b/arch/um/sys-i386/tls.c | |||
@@ -0,0 +1,326 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2005 Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it> | ||
3 | * Licensed under the GPL | ||
4 | */ | ||
5 | |||
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" | ||
22 | |||
23 | #ifdef CONFIG_MODE_SKAS | ||
24 | #include "skas.h" | ||
25 | #endif | ||
26 | |||
27 | #ifdef CONFIG_MODE_SKAS | ||
28 | int do_set_thread_area_skas(struct user_desc *info) | ||
29 | { | ||
30 | int ret; | ||
31 | u32 cpu; | ||
32 | |||
33 | cpu = get_cpu(); | ||
34 | ret = os_set_thread_area(info, userspace_pid[cpu]); | ||
35 | put_cpu(); | ||
36 | return ret; | ||
37 | } | ||
38 | |||
39 | int do_get_thread_area_skas(struct user_desc *info) | ||
40 | { | ||
41 | int ret; | ||
42 | u32 cpu; | ||
43 | |||
44 | cpu = get_cpu(); | ||
45 | ret = os_get_thread_area(info, userspace_pid[cpu]); | ||
46 | put_cpu(); | ||
47 | return ret; | ||
48 | } | ||
49 | #endif | ||
50 | |||
51 | /* | ||
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. | ||
55 | * | ||
56 | * Also, this must be tested when compiling in SKAS mode with dinamic linking | ||
57 | * and running against NPTL. | ||
58 | */ | ||
59 | static int get_free_idx(struct task_struct* task) | ||
60 | { | ||
61 | struct thread_struct *t = &task->thread; | ||
62 | int idx; | ||
63 | |||
64 | if (!t->arch.tls_array) | ||
65 | return GDT_ENTRY_TLS_MIN; | ||
66 | |||
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; | ||
71 | } | ||
72 | |||
73 | #define O_FORCE 1 | ||
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 | static int load_TLS(int flags, struct task_struct *to) | ||
88 | { | ||
89 | int ret = 0; | ||
90 | int idx; | ||
91 | |||
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]; | ||
94 | |||
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; | ||
104 | } | ||
105 | } | ||
106 | |||
107 | if (!(flags & O_FORCE) && curr->flushed) | ||
108 | continue; | ||
109 | |||
110 | ret = do_set_thread_area(&curr->tls); | ||
111 | if (ret) | ||
112 | goto out; | ||
113 | |||
114 | curr->flushed = 1; | ||
115 | } | ||
116 | out: | ||
117 | return ret; | ||
118 | } | ||
119 | |||
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. | ||
122 | */ | ||
123 | static inline int needs_TLS_update(struct task_struct *task) | ||
124 | { | ||
125 | int i; | ||
126 | int ret = 0; | ||
127 | |||
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]; | ||
130 | |||
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; | ||
137 | } | ||
138 | return ret; | ||
139 | } | ||
140 | |||
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. | ||
143 | */ | ||
144 | void clear_flushed_tls(struct task_struct *task) | ||
145 | { | ||
146 | int i; | ||
147 | |||
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]; | ||
150 | |||
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; | ||
155 | |||
156 | curr->flushed = 0; | ||
157 | } | ||
158 | } | ||
159 | |||
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) | ||
164 | { | ||
165 | return load_TLS(O_FORCE, to); | ||
166 | } | ||
167 | |||
168 | int arch_switch_tls_tt(struct task_struct *from, struct task_struct *to) | ||
169 | { | ||
170 | if (needs_TLS_update(to)) | ||
171 | return load_TLS(0, to); | ||
172 | |||
173 | return 0; | ||
174 | } | ||
175 | |||
176 | static int set_tls_entry(struct task_struct* task, struct user_desc *info, | ||
177 | int idx, int flushed) | ||
178 | { | ||
179 | struct thread_struct *t = &task->thread; | ||
180 | |||
181 | if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX) | ||
182 | return -EINVAL; | ||
183 | |||
184 | t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].tls = *info; | ||
185 | t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].present = 1; | ||
186 | t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].flushed = flushed; | ||
187 | |||
188 | return 0; | ||
189 | } | ||
190 | |||
191 | int arch_copy_tls(struct task_struct *new) | ||
192 | { | ||
193 | struct user_desc info; | ||
194 | int idx, ret = -EFAULT; | ||
195 | |||
196 | if (copy_from_user(&info, | ||
197 | (void __user *) UPT_ESI(&new->thread.regs.regs), | ||
198 | sizeof(info))) | ||
199 | goto out; | ||
200 | |||
201 | ret = -EINVAL; | ||
202 | if (LDT_empty(&info)) | ||
203 | goto out; | ||
204 | |||
205 | idx = info.entry_number; | ||
206 | |||
207 | ret = set_tls_entry(new, &info, idx, 0); | ||
208 | out: | ||
209 | return ret; | ||
210 | } | ||
211 | |||
212 | /* XXX: use do_get_thread_area to read the host value? I'm not at all sure! */ | ||
213 | static int get_tls_entry(struct task_struct* task, struct user_desc *info, int idx) | ||
214 | { | ||
215 | struct thread_struct *t = &task->thread; | ||
216 | |||
217 | if (!t->arch.tls_array) | ||
218 | goto clear; | ||
219 | |||
220 | if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX) | ||
221 | return -EINVAL; | ||
222 | |||
223 | if (!t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].present) | ||
224 | goto clear; | ||
225 | |||
226 | *info = t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].tls; | ||
227 | |||
228 | out: | ||
229 | /* Temporary debugging check, to make sure that things have been | ||
230 | * flushed. This could be triggered if load_TLS() failed. | ||
231 | */ | ||
232 | if (unlikely(task == current && !t->arch.tls_array[idx - GDT_ENTRY_TLS_MIN].flushed)) { | ||
233 | printk(KERN_ERR "get_tls_entry: task with pid %d got here " | ||
234 | "without flushed TLS.", current->pid); | ||
235 | } | ||
236 | |||
237 | return 0; | ||
238 | clear: | ||
239 | /* When the TLS entry has not been set, the values read to user in the | ||
240 | * tls_array are 0 (because it's cleared at boot, see | ||
241 | * arch/i386/kernel/head.S:cpu_gdt_table). Emulate that. | ||
242 | */ | ||
243 | clear_user_desc(info); | ||
244 | info->entry_number = idx; | ||
245 | goto out; | ||
246 | } | ||
247 | |||
248 | asmlinkage int sys_set_thread_area(struct user_desc __user *user_desc) | ||
249 | { | ||
250 | struct user_desc info; | ||
251 | int idx, ret; | ||
252 | |||
253 | if (copy_from_user(&info, user_desc, sizeof(info))) | ||
254 | return -EFAULT; | ||
255 | |||
256 | idx = info.entry_number; | ||
257 | |||
258 | if (idx == -1) { | ||
259 | idx = get_free_idx(current); | ||
260 | if (idx < 0) | ||
261 | return idx; | ||
262 | info.entry_number = idx; | ||
263 | /* Tell the user which slot we chose for him.*/ | ||
264 | if (put_user(idx, &user_desc->entry_number)) | ||
265 | return -EFAULT; | ||
266 | } | ||
267 | |||
268 | ret = CHOOSE_MODE_PROC(do_set_thread_area_tt, do_set_thread_area_skas, &info); | ||
269 | if (ret) | ||
270 | return ret; | ||
271 | return set_tls_entry(current, &info, idx, 1); | ||
272 | } | ||
273 | |||
274 | /* | ||
275 | * Perform set_thread_area on behalf of the traced child. | ||
276 | * Note: error handling is not done on the deferred load, and this differ from | ||
277 | * i386. However the only possible error are caused by bugs. | ||
278 | */ | ||
279 | int ptrace_set_thread_area(struct task_struct *child, int idx, | ||
280 | struct user_desc __user *user_desc) | ||
281 | { | ||
282 | struct user_desc info; | ||
283 | |||
284 | if (copy_from_user(&info, user_desc, sizeof(info))) | ||
285 | return -EFAULT; | ||
286 | |||
287 | return set_tls_entry(child, &info, idx, 0); | ||
288 | } | ||
289 | |||
290 | asmlinkage int sys_get_thread_area(struct user_desc __user *user_desc) | ||
291 | { | ||
292 | struct user_desc info; | ||
293 | int idx, ret; | ||
294 | |||
295 | if (get_user(idx, &user_desc->entry_number)) | ||
296 | return -EFAULT; | ||
297 | |||
298 | ret = get_tls_entry(current, &info, idx); | ||
299 | if (ret < 0) | ||
300 | goto out; | ||
301 | |||
302 | if (copy_to_user(user_desc, &info, sizeof(info))) | ||
303 | ret = -EFAULT; | ||
304 | |||
305 | out: | ||
306 | return ret; | ||
307 | } | ||
308 | |||
309 | /* | ||
310 | * Perform get_thread_area on behalf of the traced child. | ||
311 | */ | ||
312 | int ptrace_get_thread_area(struct task_struct *child, int idx, | ||
313 | struct user_desc __user *user_desc) | ||
314 | { | ||
315 | struct user_desc info; | ||
316 | int ret; | ||
317 | |||
318 | ret = get_tls_entry(child, &info, idx); | ||
319 | if (ret < 0) | ||
320 | goto out; | ||
321 | |||
322 | if (copy_to_user(user_desc, &info, sizeof(info))) | ||
323 | ret = -EFAULT; | ||
324 | out: | ||
325 | return ret; | ||
326 | } | ||