2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
7 #include "asm/unistd.h"
11 #include "skas_ptrace.h"
12 #include "sysdep/tls.h"
14 extern int modify_ldt(int func, void *ptr, unsigned long bytecount);
16 long write_ldt_entry(struct mm_id * mm_idp, int func, struct user_desc * desc,
17 void **addr, int done)
23 * This is a special handling for the case, that the mm to
24 * modify isn't current->active_mm.
25 * If this is called directly by modify_ldt,
26 * (current->active_mm->context.skas.u == mm_idp)
27 * will be true. So no call to __switch_mm(mm_idp) is done.
28 * If this is called in case of init_new_ldt or PTRACE_LDT,
29 * mm_idp won't belong to current->active_mm, but child->mm.
30 * So we need to switch child's mm into our userspace, then
33 * Note: I'm unsure: should interrupts be disabled here?
35 if (!current->active_mm || current->active_mm == &init_mm ||
36 mm_idp != ¤t->active_mm->context.id)
41 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
44 .bytecount = sizeof(*desc)};
52 pid = userspace_pid[cpu];
55 res = os_ptrace_ldt(pid, 0, (unsigned long) &ldt_op);
62 res = syscall_stub_data(mm_idp, (unsigned long *)desc,
63 (sizeof(*desc) + sizeof(long) - 1) &
67 unsigned long args[] = { func,
68 (unsigned long)stub_addr,
71 res = run_syscall_stub(mm_idp, __NR_modify_ldt, args,
78 * This is the second part of special handling, that makes
79 * PTRACE_LDT possible to implement.
81 if (current->active_mm && current->active_mm != &init_mm &&
82 mm_idp != ¤t->active_mm->context.id)
83 __switch_mm(¤t->active_mm->context.id);
89 static long read_ldt_from_host(void __user * ptr, unsigned long bytecount)
92 struct ptrace_ldt ptrace_ldt = (struct ptrace_ldt) {
94 .bytecount = bytecount,
95 .ptr = kmalloc(bytecount, GFP_KERNEL)};
98 if (ptrace_ldt.ptr == NULL)
102 * This is called from sys_modify_ldt only, so userspace_pid gives
103 * us the right number
107 res = os_ptrace_ldt(userspace_pid[cpu], 0, (unsigned long) &ptrace_ldt);
112 n = copy_to_user(ptr, ptrace_ldt.ptr, res);
117 kfree(ptrace_ldt.ptr);
123 * In skas mode, we hold our own ldt data in UML.
124 * Thus, the code implementing sys_modify_ldt_skas
125 * is very similar to (and mostly stolen from) sys_modify_ldt
126 * for arch/i386/kernel/ldt.c
127 * The routines copied and modified in part are:
131 * - sys_modify_ldt_skas
134 static int read_ldt(void __user * ptr, unsigned long bytecount)
138 uml_ldt_t * ldt = ¤t->mm->context.ldt;
140 if (!ldt->entry_count)
142 if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
143 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
147 return read_ldt_from_host(ptr, bytecount);
149 down(&ldt->semaphore);
150 if (ldt->entry_count <= LDT_DIRECT_ENTRIES) {
151 size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES;
152 if (size > bytecount)
154 if (copy_to_user(ptr, ldt->u.entries, size))
160 for (i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount;
163 if (size > bytecount)
165 if (copy_to_user(ptr, ldt->u.pages[i], size)) {
175 if (bytecount == 0 || err == -EFAULT)
178 if (clear_user(ptr, bytecount))
185 static int read_default_ldt(void __user * ptr, unsigned long bytecount)
189 if (bytecount > 5*LDT_ENTRY_SIZE)
190 bytecount = 5*LDT_ENTRY_SIZE;
194 * UML doesn't support lcall7 and lcall27.
195 * So, we don't really have a default ldt, but emulate
196 * an empty ldt of common host default ldt size.
198 if (clear_user(ptr, bytecount))
204 static int write_ldt(void __user * ptr, unsigned long bytecount, int func)
206 uml_ldt_t * ldt = ¤t->mm->context.ldt;
207 struct mm_id * mm_idp = ¤t->mm->context.id;
209 struct user_desc ldt_info;
210 struct ldt_entry entry0, *ldt_p;
214 if (bytecount != sizeof(ldt_info))
217 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
221 if (ldt_info.entry_number >= LDT_ENTRIES)
223 if (ldt_info.contents == 3) {
226 if (ldt_info.seg_not_present == 0)
231 down(&ldt->semaphore);
233 err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1);
236 else if (ptrace_ldt) {
237 /* With PTRACE_LDT available, this is used as a flag only */
238 ldt->entry_count = 1;
242 if (ldt_info.entry_number >= ldt->entry_count &&
243 ldt_info.entry_number >= LDT_DIRECT_ENTRIES) {
244 for (i=ldt->entry_count/LDT_ENTRIES_PER_PAGE;
245 i*LDT_ENTRIES_PER_PAGE <= ldt_info.entry_number;
248 memcpy(&entry0, ldt->u.entries,
250 ldt->u.pages[i] = (struct ldt_entry *)
251 __get_free_page(GFP_KERNEL|__GFP_ZERO);
252 if (!ldt->u.pages[i]) {
254 /* Undo the change in host */
255 memset(&ldt_info, 0, sizeof(ldt_info));
256 write_ldt_entry(mm_idp, 1, &ldt_info, &addr, 1);
260 memcpy(ldt->u.pages[0], &entry0,
262 memcpy(ldt->u.pages[0]+1, ldt->u.entries+1,
263 sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
265 ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
268 if (ldt->entry_count <= ldt_info.entry_number)
269 ldt->entry_count = ldt_info.entry_number + 1;
271 if (ldt->entry_count <= LDT_DIRECT_ENTRIES)
272 ldt_p = ldt->u.entries + ldt_info.entry_number;
274 ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
275 ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
277 if (ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
278 (func == 1 || LDT_empty(&ldt_info))) {
284 ldt_info.useable = 0;
285 ldt_p->a = LDT_entry_a(&ldt_info);
286 ldt_p->b = LDT_entry_b(&ldt_info);
296 static long do_modify_ldt_skas(int func, void __user *ptr,
297 unsigned long bytecount)
303 ret = read_ldt(ptr, bytecount);
307 ret = write_ldt(ptr, bytecount, func);
310 ret = read_default_ldt(ptr, bytecount);
316 static DEFINE_SPINLOCK(host_ldt_lock);
317 static short dummy_list[9] = {0, -1};
318 static short * host_ldt_entries = NULL;
320 static void ldt_get_host_info(void)
323 struct ldt_entry * ldt;
325 int i, size, k, order;
327 spin_lock(&host_ldt_lock);
329 if (host_ldt_entries != NULL) {
330 spin_unlock(&host_ldt_lock);
333 host_ldt_entries = dummy_list+1;
335 spin_unlock(&host_ldt_lock);
337 for (i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++)
340 ldt = (struct ldt_entry *)
341 __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
343 printk(KERN_ERR "ldt_get_host_info: couldn't allocate buffer "
348 ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
350 printk(KERN_ERR "ldt_get_host_info: couldn't read host ldt\n");
354 /* default_ldt is active, simply write an empty entry 0 */
355 host_ldt_entries = dummy_list;
359 for (i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++) {
360 if (ldt[i].a != 0 || ldt[i].b != 0)
364 if (size < ARRAY_SIZE(dummy_list))
365 host_ldt_entries = dummy_list;
367 size = (size + 1) * sizeof(dummy_list[0]);
368 tmp = kmalloc(size, GFP_KERNEL);
370 printk(KERN_ERR "ldt_get_host_info: couldn't allocate "
374 host_ldt_entries = tmp;
377 for (i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++) {
378 if (ldt[i].a != 0 || ldt[i].b != 0)
379 host_ldt_entries[k++] = i;
381 host_ldt_entries[k] = -1;
384 free_pages((unsigned long)ldt, order);
387 long init_new_ldt(struct mm_context *new_mm, struct mm_context *from_mm)
389 struct user_desc desc;
394 struct proc_mm_op copy;
398 init_MUTEX(&new_mm->ldt.semaphore);
401 memset(&desc, 0, sizeof(desc));
403 * We have to initialize a clean ldt.
407 * If the new mm was created using proc_mm, host's
408 * default-ldt currently is assigned, which normally
409 * contains the call-gates for lcall7 and lcall27.
410 * To remove these gates, we simply write an empty
411 * entry as number 0 to the host.
413 err = write_ldt_entry(&new_mm->id, 1, &desc, &addr, 1);
417 * Now we try to retrieve info about the ldt, we
418 * inherited from the host. All ldt-entries found
419 * will be reset in the following loop
422 for (num_p=host_ldt_entries; *num_p != -1; num_p++) {
423 desc.entry_number = *num_p;
424 err = write_ldt_entry(&new_mm->id, 1, &desc,
425 &addr, *(num_p + 1) == -1);
430 new_mm->ldt.entry_count = 0;
437 * We have a valid from_mm, so we now have to copy the LDT of
438 * from_mm to new_mm, because using proc_mm an new mm with
439 * an empty/default LDT was created in new_mm()
441 copy = ((struct proc_mm_op) { .op = MM_COPY_SEGMENTS,
444 from_mm->id.u.mm_fd } } );
445 i = os_write_file(new_mm->id.u.mm_fd, ©, sizeof(copy));
446 if (i != sizeof(copy))
447 printk(KERN_ERR "new_mm : /proc/mm copy_segments "
448 "failed, err = %d\n", -i);
453 * Our local LDT is used to supply the data for
454 * modify_ldt(READLDT), if PTRACE_LDT isn't available,
455 * i.e., we have to use the stub for modify_ldt, which
456 * can't handle the big read buffer of up to 64kB.
458 down(&from_mm->ldt.semaphore);
459 if (from_mm->ldt.entry_count <= LDT_DIRECT_ENTRIES)
460 memcpy(new_mm->ldt.u.entries, from_mm->ldt.u.entries,
461 sizeof(new_mm->ldt.u.entries));
463 i = from_mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
465 page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
470 new_mm->ldt.u.pages[i] =
471 (struct ldt_entry *) page;
472 memcpy(new_mm->ldt.u.pages[i],
473 from_mm->ldt.u.pages[i], PAGE_SIZE);
476 new_mm->ldt.entry_count = from_mm->ldt.entry_count;
477 up(&from_mm->ldt.semaphore);
485 void free_ldt(struct mm_context *mm)
489 if (!ptrace_ldt && mm->ldt.entry_count > LDT_DIRECT_ENTRIES) {
490 i = mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
492 free_page((long) mm->ldt.u.pages[i]);
494 mm->ldt.entry_count = 0;
497 int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
499 return do_modify_ldt_skas(func, ptr, bytecount);