2 * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 #include "linux/sched.h"
7 #include "linux/slab.h"
8 #include "linux/types.h"
9 #include "linux/errno.h"
10 #include "linux/spinlock.h"
11 #include "asm/uaccess.h"
14 #include "asm/unistd.h"
16 #include "mode_kern.h"
19 extern int modify_ldt(int func, void *ptr, unsigned long bytecount);
22 #include "skas_ptrace.h"
23 #include "asm/mmu_context.h"
26 long write_ldt_entry(struct mm_id * mm_idp, int func, struct user_desc * desc,
27 void **addr, int done)
32 /* This is a special handling for the case, that the mm to
33 * modify isn't current->active_mm.
34 * If this is called directly by modify_ldt,
35 * (current->active_mm->context.skas.u == mm_idp)
36 * will be true. So no call to switch_mm_skas(mm_idp) is done.
37 * If this is called in case of init_new_ldt or PTRACE_LDT,
38 * mm_idp won't belong to current->active_mm, but child->mm.
39 * So we need to switch child's mm into our userspace, then
42 * Note: I'm unsure: should interrupts be disabled here?
44 if(!current->active_mm || current->active_mm == &init_mm ||
45 mm_idp != ¤t->active_mm->context.skas.id)
46 switch_mm_skas(mm_idp);
50 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
53 .bytecount = sizeof(*desc)};
61 pid = userspace_pid[cpu];
64 res = os_ptrace_ldt(pid, 0, (unsigned long) &ldt_op);
71 res = syscall_stub_data(mm_idp, (unsigned long *)desc,
72 (sizeof(*desc) + sizeof(long) - 1) &
76 unsigned long args[] = { func,
77 (unsigned long)stub_addr,
80 res = run_syscall_stub(mm_idp, __NR_modify_ldt, args,
86 /* This is the second part of special handling, that makes
87 * PTRACE_LDT possible to implement.
89 if(current->active_mm && current->active_mm != &init_mm &&
90 mm_idp != ¤t->active_mm->context.skas.id)
91 switch_mm_skas(¤t->active_mm->context.skas.id);
97 static long read_ldt_from_host(void __user * ptr, unsigned long bytecount)
100 struct ptrace_ldt ptrace_ldt = (struct ptrace_ldt) {
102 .bytecount = bytecount,
103 .ptr = kmalloc(bytecount, GFP_KERNEL)};
106 if(ptrace_ldt.ptr == NULL)
109 /* This is called from sys_modify_ldt only, so userspace_pid gives
110 * us the right number
114 res = os_ptrace_ldt(userspace_pid[cpu], 0, (unsigned long) &ptrace_ldt);
119 n = copy_to_user(ptr, ptrace_ldt.ptr, res);
124 kfree(ptrace_ldt.ptr);
130 * In skas mode, we hold our own ldt data in UML.
131 * Thus, the code implementing sys_modify_ldt_skas
132 * is very similar to (and mostly stolen from) sys_modify_ldt
133 * for arch/i386/kernel/ldt.c
134 * The routines copied and modified in part are:
138 * - sys_modify_ldt_skas
141 static int read_ldt(void __user * ptr, unsigned long bytecount)
145 uml_ldt_t * ldt = ¤t->mm->context.skas.ldt;
147 if(!ldt->entry_count)
149 if(bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
150 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
154 return read_ldt_from_host(ptr, bytecount);
157 down(&ldt->semaphore);
158 if(ldt->entry_count <= LDT_DIRECT_ENTRIES){
159 size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES;
162 if(copy_to_user(ptr, ldt->u.entries, size))
168 for(i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount;
173 if(copy_to_user(ptr, ldt->u.pages[i], size)){
183 if(bytecount == 0 || err == -EFAULT)
186 if(clear_user(ptr, bytecount))
193 static int read_default_ldt(void __user * ptr, unsigned long bytecount)
197 if(bytecount > 5*LDT_ENTRY_SIZE)
198 bytecount = 5*LDT_ENTRY_SIZE;
201 /* UML doesn't support lcall7 and lcall27.
202 * So, we don't really have a default ldt, but emulate
203 * an empty ldt of common host default ldt size.
205 if(clear_user(ptr, bytecount))
211 static int write_ldt(void __user * ptr, unsigned long bytecount, int func)
213 uml_ldt_t * ldt = ¤t->mm->context.skas.ldt;
214 struct mm_id * mm_idp = ¤t->mm->context.skas.id;
216 struct user_desc ldt_info;
217 struct ldt_entry entry0, *ldt_p;
221 if(bytecount != sizeof(ldt_info))
224 if(copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
228 if(ldt_info.entry_number >= LDT_ENTRIES)
230 if(ldt_info.contents == 3){
233 if (ldt_info.seg_not_present == 0)
238 down(&ldt->semaphore);
240 err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1);
243 else if(ptrace_ldt) {
244 /* With PTRACE_LDT available, this is used as a flag only */
245 ldt->entry_count = 1;
249 if(ldt_info.entry_number >= ldt->entry_count &&
250 ldt_info.entry_number >= LDT_DIRECT_ENTRIES){
251 for(i=ldt->entry_count/LDT_ENTRIES_PER_PAGE;
252 i*LDT_ENTRIES_PER_PAGE <= ldt_info.entry_number;
255 memcpy(&entry0, ldt->u.entries,
257 ldt->u.pages[i] = (struct ldt_entry *)
258 __get_free_page(GFP_KERNEL|__GFP_ZERO);
259 if(!ldt->u.pages[i]){
261 /* Undo the change in host */
262 memset(&ldt_info, 0, sizeof(ldt_info));
263 write_ldt_entry(mm_idp, 1, &ldt_info, &addr, 1);
267 memcpy(ldt->u.pages[0], &entry0,
269 memcpy(ldt->u.pages[0]+1, ldt->u.entries+1,
270 sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
272 ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
275 if(ldt->entry_count <= ldt_info.entry_number)
276 ldt->entry_count = ldt_info.entry_number + 1;
278 if(ldt->entry_count <= LDT_DIRECT_ENTRIES)
279 ldt_p = ldt->u.entries + ldt_info.entry_number;
281 ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
282 ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
284 if(ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
285 (func == 1 || LDT_empty(&ldt_info))){
291 ldt_info.useable = 0;
292 ldt_p->a = LDT_entry_a(&ldt_info);
293 ldt_p->b = LDT_entry_b(&ldt_info);
303 static long do_modify_ldt_skas(int func, void __user *ptr,
304 unsigned long bytecount)
310 ret = read_ldt(ptr, bytecount);
314 ret = write_ldt(ptr, bytecount, func);
317 ret = read_default_ldt(ptr, bytecount);
323 static DEFINE_SPINLOCK(host_ldt_lock);
324 static short dummy_list[9] = {0, -1};
325 static short * host_ldt_entries = NULL;
327 static void ldt_get_host_info(void)
330 struct ldt_entry * ldt;
332 int i, size, k, order;
334 spin_lock(&host_ldt_lock);
336 if(host_ldt_entries != NULL){
337 spin_unlock(&host_ldt_lock);
340 host_ldt_entries = dummy_list+1;
342 spin_unlock(&host_ldt_lock);
344 for(i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++);
346 ldt = (struct ldt_entry *)
347 __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
349 printk("ldt_get_host_info: couldn't allocate buffer for host "
354 ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
356 printk("ldt_get_host_info: couldn't read host ldt\n");
360 /* default_ldt is active, simply write an empty entry 0 */
361 host_ldt_entries = dummy_list;
365 for(i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++){
366 if(ldt[i].a != 0 || ldt[i].b != 0)
370 if(size < ARRAY_SIZE(dummy_list))
371 host_ldt_entries = dummy_list;
373 size = (size + 1) * sizeof(dummy_list[0]);
374 tmp = kmalloc(size, GFP_KERNEL);
376 printk("ldt_get_host_info: couldn't allocate host ldt "
380 host_ldt_entries = tmp;
383 for(i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++){
384 if(ldt[i].a != 0 || ldt[i].b != 0) {
385 host_ldt_entries[k++] = i;
388 host_ldt_entries[k] = -1;
391 free_pages((unsigned long)ldt, order);
394 long init_new_ldt(struct mmu_context_skas * new_mm,
395 struct mmu_context_skas * from_mm)
397 struct user_desc desc;
402 struct proc_mm_op copy;
406 init_MUTEX(&new_mm->ldt.semaphore);
409 memset(&desc, 0, sizeof(desc));
411 * We have to initialize a clean ldt.
415 * If the new mm was created using proc_mm, host's
416 * default-ldt currently is assigned, which normally
417 * contains the call-gates for lcall7 and lcall27.
418 * To remove these gates, we simply write an empty
419 * entry as number 0 to the host.
421 err = write_ldt_entry(&new_mm->id, 1, &desc,
426 * Now we try to retrieve info about the ldt, we
427 * inherited from the host. All ldt-entries found
428 * will be reset in the following loop
431 for(num_p=host_ldt_entries; *num_p != -1; num_p++){
432 desc.entry_number = *num_p;
433 err = write_ldt_entry(&new_mm->id, 1, &desc,
434 &addr, *(num_p + 1) == -1);
439 new_mm->ldt.entry_count = 0;
445 /* We have a valid from_mm, so we now have to copy the LDT of
446 * from_mm to new_mm, because using proc_mm an new mm with
447 * an empty/default LDT was created in new_mm()
449 copy = ((struct proc_mm_op) { .op = MM_COPY_SEGMENTS,
452 from_mm->id.u.mm_fd } } );
453 i = os_write_file(new_mm->id.u.mm_fd, ©, sizeof(copy));
454 if(i != sizeof(copy))
455 printk("new_mm : /proc/mm copy_segments failed, "
460 /* Our local LDT is used to supply the data for
461 * modify_ldt(READLDT), if PTRACE_LDT isn't available,
462 * i.e., we have to use the stub for modify_ldt, which
463 * can't handle the big read buffer of up to 64kB.
465 down(&from_mm->ldt.semaphore);
466 if(from_mm->ldt.entry_count <= LDT_DIRECT_ENTRIES){
467 memcpy(new_mm->ldt.u.entries, from_mm->ldt.u.entries,
468 sizeof(new_mm->ldt.u.entries));
471 i = from_mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
473 page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
478 new_mm->ldt.u.pages[i] =
479 (struct ldt_entry *) page;
480 memcpy(new_mm->ldt.u.pages[i],
481 from_mm->ldt.u.pages[i], PAGE_SIZE);
484 new_mm->ldt.entry_count = from_mm->ldt.entry_count;
485 up(&from_mm->ldt.semaphore);
493 void free_ldt(struct mmu_context_skas * mm)
497 if(!ptrace_ldt && mm->ldt.entry_count > LDT_DIRECT_ENTRIES){
498 i = mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
500 free_page((long )mm->ldt.u.pages[i]);
503 mm->ldt.entry_count = 0;
506 int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
508 return do_modify_ldt_skas(func, ptr, bytecount);