Loading...
1/*
2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
5
6#include <linux/mm.h>
7#include <linux/sched.h>
8#include <linux/slab.h>
9#include <asm/unistd.h>
10#include <os.h>
11#include <proc_mm.h>
12#include <skas.h>
13#include <skas_ptrace.h>
14#include <sysdep/tls.h>
15
16extern int modify_ldt(int func, void *ptr, unsigned long bytecount);
17
18static long write_ldt_entry(struct mm_id *mm_idp, int func,
19 struct user_desc *desc, void **addr, int done)
20{
21 long res;
22
23 if (proc_mm) {
24 /*
25 * This is a special handling for the case, that the mm to
26 * modify isn't current->active_mm.
27 * If this is called directly by modify_ldt,
28 * (current->active_mm->context.skas.u == mm_idp)
29 * will be true. So no call to __switch_mm(mm_idp) is done.
30 * If this is called in case of init_new_ldt or PTRACE_LDT,
31 * mm_idp won't belong to current->active_mm, but child->mm.
32 * So we need to switch child's mm into our userspace, then
33 * later switch back.
34 *
35 * Note: I'm unsure: should interrupts be disabled here?
36 */
37 if (!current->active_mm || current->active_mm == &init_mm ||
38 mm_idp != ¤t->active_mm->context.id)
39 __switch_mm(mm_idp);
40 }
41
42 if (ptrace_ldt) {
43 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
44 .func = func,
45 .ptr = desc,
46 .bytecount = sizeof(*desc)};
47 u32 cpu;
48 int pid;
49
50 if (!proc_mm)
51 pid = mm_idp->u.pid;
52 else {
53 cpu = get_cpu();
54 pid = userspace_pid[cpu];
55 }
56
57 res = os_ptrace_ldt(pid, 0, (unsigned long) &ldt_op);
58
59 if (proc_mm)
60 put_cpu();
61 }
62 else {
63 void *stub_addr;
64 res = syscall_stub_data(mm_idp, (unsigned long *)desc,
65 (sizeof(*desc) + sizeof(long) - 1) &
66 ~(sizeof(long) - 1),
67 addr, &stub_addr);
68 if (!res) {
69 unsigned long args[] = { func,
70 (unsigned long)stub_addr,
71 sizeof(*desc),
72 0, 0, 0 };
73 res = run_syscall_stub(mm_idp, __NR_modify_ldt, args,
74 0, addr, done);
75 }
76 }
77
78 if (proc_mm) {
79 /*
80 * This is the second part of special handling, that makes
81 * PTRACE_LDT possible to implement.
82 */
83 if (current->active_mm && current->active_mm != &init_mm &&
84 mm_idp != ¤t->active_mm->context.id)
85 __switch_mm(¤t->active_mm->context.id);
86 }
87
88 return res;
89}
90
91static long read_ldt_from_host(void __user * ptr, unsigned long bytecount)
92{
93 int res, n;
94 struct ptrace_ldt ptrace_ldt = (struct ptrace_ldt) {
95 .func = 0,
96 .bytecount = bytecount,
97 .ptr = kmalloc(bytecount, GFP_KERNEL)};
98 u32 cpu;
99
100 if (ptrace_ldt.ptr == NULL)
101 return -ENOMEM;
102
103 /*
104 * This is called from sys_modify_ldt only, so userspace_pid gives
105 * us the right number
106 */
107
108 cpu = get_cpu();
109 res = os_ptrace_ldt(userspace_pid[cpu], 0, (unsigned long) &ptrace_ldt);
110 put_cpu();
111 if (res < 0)
112 goto out;
113
114 n = copy_to_user(ptr, ptrace_ldt.ptr, res);
115 if (n != 0)
116 res = -EFAULT;
117
118 out:
119 kfree(ptrace_ldt.ptr);
120
121 return res;
122}
123
124/*
125 * In skas mode, we hold our own ldt data in UML.
126 * Thus, the code implementing sys_modify_ldt_skas
127 * is very similar to (and mostly stolen from) sys_modify_ldt
128 * for arch/i386/kernel/ldt.c
129 * The routines copied and modified in part are:
130 * - read_ldt
131 * - read_default_ldt
132 * - write_ldt
133 * - sys_modify_ldt_skas
134 */
135
136static int read_ldt(void __user * ptr, unsigned long bytecount)
137{
138 int i, err = 0;
139 unsigned long size;
140 uml_ldt_t *ldt = ¤t->mm->context.arch.ldt;
141
142 if (!ldt->entry_count)
143 goto out;
144 if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
145 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
146 err = bytecount;
147
148 if (ptrace_ldt)
149 return read_ldt_from_host(ptr, bytecount);
150
151 mutex_lock(&ldt->lock);
152 if (ldt->entry_count <= LDT_DIRECT_ENTRIES) {
153 size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES;
154 if (size > bytecount)
155 size = bytecount;
156 if (copy_to_user(ptr, ldt->u.entries, size))
157 err = -EFAULT;
158 bytecount -= size;
159 ptr += size;
160 }
161 else {
162 for (i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount;
163 i++) {
164 size = PAGE_SIZE;
165 if (size > bytecount)
166 size = bytecount;
167 if (copy_to_user(ptr, ldt->u.pages[i], size)) {
168 err = -EFAULT;
169 break;
170 }
171 bytecount -= size;
172 ptr += size;
173 }
174 }
175 mutex_unlock(&ldt->lock);
176
177 if (bytecount == 0 || err == -EFAULT)
178 goto out;
179
180 if (clear_user(ptr, bytecount))
181 err = -EFAULT;
182
183out:
184 return err;
185}
186
187static int read_default_ldt(void __user * ptr, unsigned long bytecount)
188{
189 int err;
190
191 if (bytecount > 5*LDT_ENTRY_SIZE)
192 bytecount = 5*LDT_ENTRY_SIZE;
193
194 err = bytecount;
195 /*
196 * UML doesn't support lcall7 and lcall27.
197 * So, we don't really have a default ldt, but emulate
198 * an empty ldt of common host default ldt size.
199 */
200 if (clear_user(ptr, bytecount))
201 err = -EFAULT;
202
203 return err;
204}
205
206static int write_ldt(void __user * ptr, unsigned long bytecount, int func)
207{
208 uml_ldt_t *ldt = ¤t->mm->context.arch.ldt;
209 struct mm_id * mm_idp = ¤t->mm->context.id;
210 int i, err;
211 struct user_desc ldt_info;
212 struct ldt_entry entry0, *ldt_p;
213 void *addr = NULL;
214
215 err = -EINVAL;
216 if (bytecount != sizeof(ldt_info))
217 goto out;
218 err = -EFAULT;
219 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
220 goto out;
221
222 err = -EINVAL;
223 if (ldt_info.entry_number >= LDT_ENTRIES)
224 goto out;
225 if (ldt_info.contents == 3) {
226 if (func == 1)
227 goto out;
228 if (ldt_info.seg_not_present == 0)
229 goto out;
230 }
231
232 if (!ptrace_ldt)
233 mutex_lock(&ldt->lock);
234
235 err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1);
236 if (err)
237 goto out_unlock;
238 else if (ptrace_ldt) {
239 /* With PTRACE_LDT available, this is used as a flag only */
240 ldt->entry_count = 1;
241 goto out;
242 }
243
244 if (ldt_info.entry_number >= ldt->entry_count &&
245 ldt_info.entry_number >= LDT_DIRECT_ENTRIES) {
246 for (i=ldt->entry_count/LDT_ENTRIES_PER_PAGE;
247 i*LDT_ENTRIES_PER_PAGE <= ldt_info.entry_number;
248 i++) {
249 if (i == 0)
250 memcpy(&entry0, ldt->u.entries,
251 sizeof(entry0));
252 ldt->u.pages[i] = (struct ldt_entry *)
253 __get_free_page(GFP_KERNEL|__GFP_ZERO);
254 if (!ldt->u.pages[i]) {
255 err = -ENOMEM;
256 /* Undo the change in host */
257 memset(&ldt_info, 0, sizeof(ldt_info));
258 write_ldt_entry(mm_idp, 1, &ldt_info, &addr, 1);
259 goto out_unlock;
260 }
261 if (i == 0) {
262 memcpy(ldt->u.pages[0], &entry0,
263 sizeof(entry0));
264 memcpy(ldt->u.pages[0]+1, ldt->u.entries+1,
265 sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
266 }
267 ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
268 }
269 }
270 if (ldt->entry_count <= ldt_info.entry_number)
271 ldt->entry_count = ldt_info.entry_number + 1;
272
273 if (ldt->entry_count <= LDT_DIRECT_ENTRIES)
274 ldt_p = ldt->u.entries + ldt_info.entry_number;
275 else
276 ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
277 ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
278
279 if (ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
280 (func == 1 || LDT_empty(&ldt_info))) {
281 ldt_p->a = 0;
282 ldt_p->b = 0;
283 }
284 else{
285 if (func == 1)
286 ldt_info.useable = 0;
287 ldt_p->a = LDT_entry_a(&ldt_info);
288 ldt_p->b = LDT_entry_b(&ldt_info);
289 }
290 err = 0;
291
292out_unlock:
293 mutex_unlock(&ldt->lock);
294out:
295 return err;
296}
297
298static long do_modify_ldt_skas(int func, void __user *ptr,
299 unsigned long bytecount)
300{
301 int ret = -ENOSYS;
302
303 switch (func) {
304 case 0:
305 ret = read_ldt(ptr, bytecount);
306 break;
307 case 1:
308 case 0x11:
309 ret = write_ldt(ptr, bytecount, func);
310 break;
311 case 2:
312 ret = read_default_ldt(ptr, bytecount);
313 break;
314 }
315 return ret;
316}
317
318static DEFINE_SPINLOCK(host_ldt_lock);
319static short dummy_list[9] = {0, -1};
320static short * host_ldt_entries = NULL;
321
322static void ldt_get_host_info(void)
323{
324 long ret;
325 struct ldt_entry * ldt;
326 short *tmp;
327 int i, size, k, order;
328
329 spin_lock(&host_ldt_lock);
330
331 if (host_ldt_entries != NULL) {
332 spin_unlock(&host_ldt_lock);
333 return;
334 }
335 host_ldt_entries = dummy_list+1;
336
337 spin_unlock(&host_ldt_lock);
338
339 for (i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++)
340 ;
341
342 ldt = (struct ldt_entry *)
343 __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
344 if (ldt == NULL) {
345 printk(KERN_ERR "ldt_get_host_info: couldn't allocate buffer "
346 "for host ldt\n");
347 return;
348 }
349
350 ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
351 if (ret < 0) {
352 printk(KERN_ERR "ldt_get_host_info: couldn't read host ldt\n");
353 goto out_free;
354 }
355 if (ret == 0) {
356 /* default_ldt is active, simply write an empty entry 0 */
357 host_ldt_entries = dummy_list;
358 goto out_free;
359 }
360
361 for (i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++) {
362 if (ldt[i].a != 0 || ldt[i].b != 0)
363 size++;
364 }
365
366 if (size < ARRAY_SIZE(dummy_list))
367 host_ldt_entries = dummy_list;
368 else {
369 size = (size + 1) * sizeof(dummy_list[0]);
370 tmp = kmalloc(size, GFP_KERNEL);
371 if (tmp == NULL) {
372 printk(KERN_ERR "ldt_get_host_info: couldn't allocate "
373 "host ldt list\n");
374 goto out_free;
375 }
376 host_ldt_entries = tmp;
377 }
378
379 for (i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++) {
380 if (ldt[i].a != 0 || ldt[i].b != 0)
381 host_ldt_entries[k++] = i;
382 }
383 host_ldt_entries[k] = -1;
384
385out_free:
386 free_pages((unsigned long)ldt, order);
387}
388
389long init_new_ldt(struct mm_context *new_mm, struct mm_context *from_mm)
390{
391 struct user_desc desc;
392 short * num_p;
393 int i;
394 long page, err=0;
395 void *addr = NULL;
396 struct proc_mm_op copy;
397
398
399 if (!ptrace_ldt)
400 mutex_init(&new_mm->arch.ldt.lock);
401
402 if (!from_mm) {
403 memset(&desc, 0, sizeof(desc));
404 /*
405 * We have to initialize a clean ldt.
406 */
407 if (proc_mm) {
408 /*
409 * If the new mm was created using proc_mm, host's
410 * default-ldt currently is assigned, which normally
411 * contains the call-gates for lcall7 and lcall27.
412 * To remove these gates, we simply write an empty
413 * entry as number 0 to the host.
414 */
415 err = write_ldt_entry(&new_mm->id, 1, &desc, &addr, 1);
416 }
417 else{
418 /*
419 * Now we try to retrieve info about the ldt, we
420 * inherited from the host. All ldt-entries found
421 * will be reset in the following loop
422 */
423 ldt_get_host_info();
424 for (num_p=host_ldt_entries; *num_p != -1; num_p++) {
425 desc.entry_number = *num_p;
426 err = write_ldt_entry(&new_mm->id, 1, &desc,
427 &addr, *(num_p + 1) == -1);
428 if (err)
429 break;
430 }
431 }
432 new_mm->arch.ldt.entry_count = 0;
433
434 goto out;
435 }
436
437 if (proc_mm) {
438 /*
439 * We have a valid from_mm, so we now have to copy the LDT of
440 * from_mm to new_mm, because using proc_mm an new mm with
441 * an empty/default LDT was created in new_mm()
442 */
443 copy = ((struct proc_mm_op) { .op = MM_COPY_SEGMENTS,
444 .u =
445 { .copy_segments =
446 from_mm->id.u.mm_fd } } );
447 i = os_write_file(new_mm->id.u.mm_fd, ©, sizeof(copy));
448 if (i != sizeof(copy))
449 printk(KERN_ERR "new_mm : /proc/mm copy_segments "
450 "failed, err = %d\n", -i);
451 }
452
453 if (!ptrace_ldt) {
454 /*
455 * Our local LDT is used to supply the data for
456 * modify_ldt(READLDT), if PTRACE_LDT isn't available,
457 * i.e., we have to use the stub for modify_ldt, which
458 * can't handle the big read buffer of up to 64kB.
459 */
460 mutex_lock(&from_mm->arch.ldt.lock);
461 if (from_mm->arch.ldt.entry_count <= LDT_DIRECT_ENTRIES)
462 memcpy(new_mm->arch.ldt.u.entries, from_mm->arch.ldt.u.entries,
463 sizeof(new_mm->arch.ldt.u.entries));
464 else {
465 i = from_mm->arch.ldt.entry_count / LDT_ENTRIES_PER_PAGE;
466 while (i-->0) {
467 page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
468 if (!page) {
469 err = -ENOMEM;
470 break;
471 }
472 new_mm->arch.ldt.u.pages[i] =
473 (struct ldt_entry *) page;
474 memcpy(new_mm->arch.ldt.u.pages[i],
475 from_mm->arch.ldt.u.pages[i], PAGE_SIZE);
476 }
477 }
478 new_mm->arch.ldt.entry_count = from_mm->arch.ldt.entry_count;
479 mutex_unlock(&from_mm->arch.ldt.lock);
480 }
481
482 out:
483 return err;
484}
485
486
487void free_ldt(struct mm_context *mm)
488{
489 int i;
490
491 if (!ptrace_ldt && mm->arch.ldt.entry_count > LDT_DIRECT_ENTRIES) {
492 i = mm->arch.ldt.entry_count / LDT_ENTRIES_PER_PAGE;
493 while (i-- > 0)
494 free_page((long) mm->arch.ldt.u.pages[i]);
495 }
496 mm->arch.ldt.entry_count = 0;
497}
498
499int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
500{
501 return do_modify_ldt_skas(func, ptr, bytecount);
502}
1/*
2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
5
6#include <linux/mm.h>
7#include <linux/sched.h>
8#include <linux/slab.h>
9#include <linux/syscalls.h>
10#include <linux/uaccess.h>
11#include <asm/unistd.h>
12#include <os.h>
13#include <skas.h>
14#include <sysdep/tls.h>
15
16static inline int modify_ldt (int func, void *ptr, unsigned long bytecount)
17{
18 return syscall(__NR_modify_ldt, func, ptr, bytecount);
19}
20
21static long write_ldt_entry(struct mm_id *mm_idp, int func,
22 struct user_desc *desc, void **addr, int done)
23{
24 long res;
25 void *stub_addr;
26
27 BUILD_BUG_ON(sizeof(*desc) % sizeof(long));
28
29 res = syscall_stub_data(mm_idp, (unsigned long *)desc,
30 sizeof(*desc) / sizeof(long),
31 addr, &stub_addr);
32 if (!res) {
33 unsigned long args[] = { func,
34 (unsigned long)stub_addr,
35 sizeof(*desc),
36 0, 0, 0 };
37 res = run_syscall_stub(mm_idp, __NR_modify_ldt, args,
38 0, addr, done);
39 }
40
41 return res;
42}
43
44/*
45 * In skas mode, we hold our own ldt data in UML.
46 * Thus, the code implementing sys_modify_ldt_skas
47 * is very similar to (and mostly stolen from) sys_modify_ldt
48 * for arch/i386/kernel/ldt.c
49 * The routines copied and modified in part are:
50 * - read_ldt
51 * - read_default_ldt
52 * - write_ldt
53 * - sys_modify_ldt_skas
54 */
55
56static int read_ldt(void __user * ptr, unsigned long bytecount)
57{
58 int i, err = 0;
59 unsigned long size;
60 uml_ldt_t *ldt = ¤t->mm->context.arch.ldt;
61
62 if (!ldt->entry_count)
63 goto out;
64 if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
65 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
66 err = bytecount;
67
68 mutex_lock(&ldt->lock);
69 if (ldt->entry_count <= LDT_DIRECT_ENTRIES) {
70 size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES;
71 if (size > bytecount)
72 size = bytecount;
73 if (copy_to_user(ptr, ldt->u.entries, size))
74 err = -EFAULT;
75 bytecount -= size;
76 ptr += size;
77 }
78 else {
79 for (i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount;
80 i++) {
81 size = PAGE_SIZE;
82 if (size > bytecount)
83 size = bytecount;
84 if (copy_to_user(ptr, ldt->u.pages[i], size)) {
85 err = -EFAULT;
86 break;
87 }
88 bytecount -= size;
89 ptr += size;
90 }
91 }
92 mutex_unlock(&ldt->lock);
93
94 if (bytecount == 0 || err == -EFAULT)
95 goto out;
96
97 if (clear_user(ptr, bytecount))
98 err = -EFAULT;
99
100out:
101 return err;
102}
103
104static int read_default_ldt(void __user * ptr, unsigned long bytecount)
105{
106 int err;
107
108 if (bytecount > 5*LDT_ENTRY_SIZE)
109 bytecount = 5*LDT_ENTRY_SIZE;
110
111 err = bytecount;
112 /*
113 * UML doesn't support lcall7 and lcall27.
114 * So, we don't really have a default ldt, but emulate
115 * an empty ldt of common host default ldt size.
116 */
117 if (clear_user(ptr, bytecount))
118 err = -EFAULT;
119
120 return err;
121}
122
123static int write_ldt(void __user * ptr, unsigned long bytecount, int func)
124{
125 uml_ldt_t *ldt = ¤t->mm->context.arch.ldt;
126 struct mm_id * mm_idp = ¤t->mm->context.id;
127 int i, err;
128 struct user_desc ldt_info;
129 struct ldt_entry entry0, *ldt_p;
130 void *addr = NULL;
131
132 err = -EINVAL;
133 if (bytecount != sizeof(ldt_info))
134 goto out;
135 err = -EFAULT;
136 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
137 goto out;
138
139 err = -EINVAL;
140 if (ldt_info.entry_number >= LDT_ENTRIES)
141 goto out;
142 if (ldt_info.contents == 3) {
143 if (func == 1)
144 goto out;
145 if (ldt_info.seg_not_present == 0)
146 goto out;
147 }
148
149 mutex_lock(&ldt->lock);
150
151 err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1);
152 if (err)
153 goto out_unlock;
154
155 if (ldt_info.entry_number >= ldt->entry_count &&
156 ldt_info.entry_number >= LDT_DIRECT_ENTRIES) {
157 for (i=ldt->entry_count/LDT_ENTRIES_PER_PAGE;
158 i*LDT_ENTRIES_PER_PAGE <= ldt_info.entry_number;
159 i++) {
160 if (i == 0)
161 memcpy(&entry0, ldt->u.entries,
162 sizeof(entry0));
163 ldt->u.pages[i] = (struct ldt_entry *)
164 __get_free_page(GFP_KERNEL|__GFP_ZERO);
165 if (!ldt->u.pages[i]) {
166 err = -ENOMEM;
167 /* Undo the change in host */
168 memset(&ldt_info, 0, sizeof(ldt_info));
169 write_ldt_entry(mm_idp, 1, &ldt_info, &addr, 1);
170 goto out_unlock;
171 }
172 if (i == 0) {
173 memcpy(ldt->u.pages[0], &entry0,
174 sizeof(entry0));
175 memcpy(ldt->u.pages[0]+1, ldt->u.entries+1,
176 sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
177 }
178 ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
179 }
180 }
181 if (ldt->entry_count <= ldt_info.entry_number)
182 ldt->entry_count = ldt_info.entry_number + 1;
183
184 if (ldt->entry_count <= LDT_DIRECT_ENTRIES)
185 ldt_p = ldt->u.entries + ldt_info.entry_number;
186 else
187 ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
188 ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
189
190 if (ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
191 (func == 1 || LDT_empty(&ldt_info))) {
192 ldt_p->a = 0;
193 ldt_p->b = 0;
194 }
195 else{
196 if (func == 1)
197 ldt_info.useable = 0;
198 ldt_p->a = LDT_entry_a(&ldt_info);
199 ldt_p->b = LDT_entry_b(&ldt_info);
200 }
201 err = 0;
202
203out_unlock:
204 mutex_unlock(&ldt->lock);
205out:
206 return err;
207}
208
209static long do_modify_ldt_skas(int func, void __user *ptr,
210 unsigned long bytecount)
211{
212 int ret = -ENOSYS;
213
214 switch (func) {
215 case 0:
216 ret = read_ldt(ptr, bytecount);
217 break;
218 case 1:
219 case 0x11:
220 ret = write_ldt(ptr, bytecount, func);
221 break;
222 case 2:
223 ret = read_default_ldt(ptr, bytecount);
224 break;
225 }
226 return ret;
227}
228
229static DEFINE_SPINLOCK(host_ldt_lock);
230static short dummy_list[9] = {0, -1};
231static short * host_ldt_entries = NULL;
232
233static void ldt_get_host_info(void)
234{
235 long ret;
236 struct ldt_entry * ldt;
237 short *tmp;
238 int i, size, k, order;
239
240 spin_lock(&host_ldt_lock);
241
242 if (host_ldt_entries != NULL) {
243 spin_unlock(&host_ldt_lock);
244 return;
245 }
246 host_ldt_entries = dummy_list+1;
247
248 spin_unlock(&host_ldt_lock);
249
250 for (i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++)
251 ;
252
253 ldt = (struct ldt_entry *)
254 __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
255 if (ldt == NULL) {
256 printk(KERN_ERR "ldt_get_host_info: couldn't allocate buffer "
257 "for host ldt\n");
258 return;
259 }
260
261 ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
262 if (ret < 0) {
263 printk(KERN_ERR "ldt_get_host_info: couldn't read host ldt\n");
264 goto out_free;
265 }
266 if (ret == 0) {
267 /* default_ldt is active, simply write an empty entry 0 */
268 host_ldt_entries = dummy_list;
269 goto out_free;
270 }
271
272 for (i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++) {
273 if (ldt[i].a != 0 || ldt[i].b != 0)
274 size++;
275 }
276
277 if (size < ARRAY_SIZE(dummy_list))
278 host_ldt_entries = dummy_list;
279 else {
280 size = (size + 1) * sizeof(dummy_list[0]);
281 tmp = kmalloc(size, GFP_KERNEL);
282 if (tmp == NULL) {
283 printk(KERN_ERR "ldt_get_host_info: couldn't allocate "
284 "host ldt list\n");
285 goto out_free;
286 }
287 host_ldt_entries = tmp;
288 }
289
290 for (i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++) {
291 if (ldt[i].a != 0 || ldt[i].b != 0)
292 host_ldt_entries[k++] = i;
293 }
294 host_ldt_entries[k] = -1;
295
296out_free:
297 free_pages((unsigned long)ldt, order);
298}
299
300long init_new_ldt(struct mm_context *new_mm, struct mm_context *from_mm)
301{
302 struct user_desc desc;
303 short * num_p;
304 int i;
305 long page, err=0;
306 void *addr = NULL;
307
308
309 mutex_init(&new_mm->arch.ldt.lock);
310
311 if (!from_mm) {
312 memset(&desc, 0, sizeof(desc));
313 /*
314 * Now we try to retrieve info about the ldt, we
315 * inherited from the host. All ldt-entries found
316 * will be reset in the following loop
317 */
318 ldt_get_host_info();
319 for (num_p=host_ldt_entries; *num_p != -1; num_p++) {
320 desc.entry_number = *num_p;
321 err = write_ldt_entry(&new_mm->id, 1, &desc,
322 &addr, *(num_p + 1) == -1);
323 if (err)
324 break;
325 }
326 new_mm->arch.ldt.entry_count = 0;
327
328 goto out;
329 }
330
331 /*
332 * Our local LDT is used to supply the data for
333 * modify_ldt(READLDT), if PTRACE_LDT isn't available,
334 * i.e., we have to use the stub for modify_ldt, which
335 * can't handle the big read buffer of up to 64kB.
336 */
337 mutex_lock(&from_mm->arch.ldt.lock);
338 if (from_mm->arch.ldt.entry_count <= LDT_DIRECT_ENTRIES)
339 memcpy(new_mm->arch.ldt.u.entries, from_mm->arch.ldt.u.entries,
340 sizeof(new_mm->arch.ldt.u.entries));
341 else {
342 i = from_mm->arch.ldt.entry_count / LDT_ENTRIES_PER_PAGE;
343 while (i-->0) {
344 page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
345 if (!page) {
346 err = -ENOMEM;
347 break;
348 }
349 new_mm->arch.ldt.u.pages[i] =
350 (struct ldt_entry *) page;
351 memcpy(new_mm->arch.ldt.u.pages[i],
352 from_mm->arch.ldt.u.pages[i], PAGE_SIZE);
353 }
354 }
355 new_mm->arch.ldt.entry_count = from_mm->arch.ldt.entry_count;
356 mutex_unlock(&from_mm->arch.ldt.lock);
357
358 out:
359 return err;
360}
361
362
363void free_ldt(struct mm_context *mm)
364{
365 int i;
366
367 if (mm->arch.ldt.entry_count > LDT_DIRECT_ENTRIES) {
368 i = mm->arch.ldt.entry_count / LDT_ENTRIES_PER_PAGE;
369 while (i-- > 0)
370 free_page((long) mm->arch.ldt.u.pages[i]);
371 }
372 mm->arch.ldt.entry_count = 0;
373}
374
375SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
376 unsigned long , bytecount)
377{
378 /* See non-um modify_ldt() for why we do this cast */
379 return (unsigned int)do_modify_ldt_skas(func, ptr, bytecount);
380}