2 * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 #include "linux/stddef.h"
7 #include "linux/config.h"
8 #include "linux/sched.h"
9 #include "linux/slab.h"
10 #include "linux/types.h"
11 #include "linux/errno.h"
12 #include "asm/uaccess.h"
15 #include "asm/unistd.h"
16 #include "choose-mode.h"
18 #include "mode_kern.h"
21 extern int modify_ldt(int func, void *ptr, unsigned long bytecount);
25 static long do_modify_ldt_tt(int func, void __user *ptr,
26 unsigned long bytecount)
28 struct user_desc info;
31 void *p = NULL; /* What we pass to host. */
35 case 0x11: /* write_ldt */
36 /* Do this check now to avoid overflows. */
37 if (bytecount != sizeof(struct user_desc)) {
42 if(copy_from_user(&info, ptr, sizeof(info))) {
50 case 2: /* read_ldt */
52 /* The use of info avoids kmalloc on the write case, not on the
54 buf = kmalloc(bytecount, GFP_KERNEL);
66 res = modify_ldt(func, p, bytecount);
73 /* Modify_ldt was for reading and returned the number of read
75 if(copy_to_user(ptr, p, res))
87 #ifdef CONFIG_MODE_SKAS
90 #include "skas_ptrace.h"
91 #include "asm/mmu_context.h"
94 long write_ldt_entry(struct mm_id * mm_idp, int func, struct user_desc * desc,
95 void **addr, int done)
100 /* This is a special handling for the case, that the mm to
101 * modify isn't current->active_mm.
102 * If this is called directly by modify_ldt,
103 * (current->active_mm->context.skas.u == mm_idp)
104 * will be true. So no call to switch_mm_skas(mm_idp) is done.
105 * If this is called in case of init_new_ldt or PTRACE_LDT,
106 * mm_idp won't belong to current->active_mm, but child->mm.
107 * So we need to switch child's mm into our userspace, then
110 * Note: I'm unshure: should interrupts be disabled here?
112 if(!current->active_mm || current->active_mm == &init_mm ||
113 mm_idp != ¤t->active_mm->context.skas.id)
114 switch_mm_skas(mm_idp);
118 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
121 .bytecount = sizeof(*desc)};
129 pid = userspace_pid[cpu];
132 res = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
141 res = syscall_stub_data(mm_idp, (unsigned long *)desc,
142 (sizeof(*desc) + sizeof(long) - 1) &
146 unsigned long args[] = { func,
147 (unsigned long)stub_addr,
150 res = run_syscall_stub(mm_idp, __NR_modify_ldt, args,
156 /* This is the second part of special handling, that makes
157 * PTRACE_LDT possible to implement.
159 if(current->active_mm && current->active_mm != &init_mm &&
160 mm_idp != ¤t->active_mm->context.skas.id)
161 switch_mm_skas(¤t->active_mm->context.skas.id);
167 static long read_ldt_from_host(void __user * ptr, unsigned long bytecount)
170 struct ptrace_ldt ptrace_ldt = (struct ptrace_ldt) {
172 .bytecount = bytecount,
173 .ptr = (void *)kmalloc(bytecount, GFP_KERNEL)};
176 if(ptrace_ldt.ptr == NULL)
179 /* This is called from sys_modify_ldt only, so userspace_pid gives
180 * us the right number
184 res = ptrace(PTRACE_LDT, userspace_pid[cpu], 0,
185 (unsigned long) &ptrace_ldt);
190 n = copy_to_user(ptr, ptrace_ldt.ptr, res);
195 kfree(ptrace_ldt.ptr);
201 * In skas mode, we hold our own ldt data in UML.
202 * Thus, the code implementing sys_modify_ldt_skas
203 * is very similar to (and mostly stolen from) sys_modify_ldt
204 * for arch/i386/kernel/ldt.c
205 * The routines copied and modified in part are:
209 * - sys_modify_ldt_skas
212 static int read_ldt(void __user * ptr, unsigned long bytecount)
216 uml_ldt_t * ldt = ¤t->mm->context.skas.ldt;
218 if(!ldt->entry_count)
220 if(bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
221 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
225 return read_ldt_from_host(ptr, bytecount);
228 down(&ldt->semaphore);
229 if(ldt->entry_count <= LDT_DIRECT_ENTRIES){
230 size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES;
233 if(copy_to_user(ptr, ldt->u.entries, size))
239 for(i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount;
244 if(copy_to_user(ptr, ldt->u.pages[i], size)){
254 if(bytecount == 0 || err == -EFAULT)
257 if(clear_user(ptr, bytecount))
264 static int read_default_ldt(void __user * ptr, unsigned long bytecount)
268 if(bytecount > 5*LDT_ENTRY_SIZE)
269 bytecount = 5*LDT_ENTRY_SIZE;
272 /* UML doesn't support lcall7 and lcall27.
273 * So, we don't really have a default ldt, but emulate
274 * an empty ldt of common host default ldt size.
276 if(clear_user(ptr, bytecount))
282 static int write_ldt(void __user * ptr, unsigned long bytecount, int func)
284 uml_ldt_t * ldt = ¤t->mm->context.skas.ldt;
285 struct mm_id * mm_idp = ¤t->mm->context.skas.id;
287 struct user_desc ldt_info;
288 struct ldt_entry entry0, *ldt_p;
292 if(bytecount != sizeof(ldt_info))
295 if(copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
299 if(ldt_info.entry_number >= LDT_ENTRIES)
301 if(ldt_info.contents == 3){
304 if (ldt_info.seg_not_present == 0)
309 down(&ldt->semaphore);
311 err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1);
314 else if(ptrace_ldt) {
315 /* With PTRACE_LDT available, this is used as a flag only */
316 ldt->entry_count = 1;
320 if(ldt_info.entry_number >= ldt->entry_count &&
321 ldt_info.entry_number >= LDT_DIRECT_ENTRIES){
322 for(i=ldt->entry_count/LDT_ENTRIES_PER_PAGE;
323 i*LDT_ENTRIES_PER_PAGE <= ldt_info.entry_number;
326 memcpy(&entry0, ldt->u.entries,
328 ldt->u.pages[i] = (struct ldt_entry *)
329 __get_free_page(GFP_KERNEL|__GFP_ZERO);
330 if(!ldt->u.pages[i]){
332 /* Undo the change in host */
333 memset(&ldt_info, 0, sizeof(ldt_info));
334 write_ldt_entry(mm_idp, 1, &ldt_info, &addr, 1);
338 memcpy(ldt->u.pages[0], &entry0,
340 memcpy(ldt->u.pages[0]+1, ldt->u.entries+1,
341 sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
343 ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
346 if(ldt->entry_count <= ldt_info.entry_number)
347 ldt->entry_count = ldt_info.entry_number + 1;
349 if(ldt->entry_count <= LDT_DIRECT_ENTRIES)
350 ldt_p = ldt->u.entries + ldt_info.entry_number;
352 ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
353 ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
355 if(ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
356 (func == 1 || LDT_empty(&ldt_info))){
362 ldt_info.useable = 0;
363 ldt_p->a = LDT_entry_a(&ldt_info);
364 ldt_p->b = LDT_entry_b(&ldt_info);
374 static long do_modify_ldt_skas(int func, void __user *ptr,
375 unsigned long bytecount)
381 ret = read_ldt(ptr, bytecount);
385 ret = write_ldt(ptr, bytecount, func);
388 ret = read_default_ldt(ptr, bytecount);
394 short dummy_list[9] = {0, -1};
395 short * host_ldt_entries = NULL;
397 void ldt_get_host_info(void)
400 struct ldt_entry * ldt;
401 int i, size, k, order;
403 host_ldt_entries = dummy_list+1;
405 for(i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++);
407 ldt = (struct ldt_entry *)
408 __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
410 printk("ldt_get_host_info: couldn't allocate buffer for host ldt\n");
414 ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
416 printk("ldt_get_host_info: couldn't read host ldt\n");
420 /* default_ldt is active, simply write an empty entry 0 */
421 host_ldt_entries = dummy_list;
425 for(i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++){
426 if(ldt[i].a != 0 || ldt[i].b != 0)
430 if(size < sizeof(dummy_list)/sizeof(dummy_list[0])) {
431 host_ldt_entries = dummy_list;
434 size = (size + 1) * sizeof(dummy_list[0]);
435 host_ldt_entries = (short *)kmalloc(size, GFP_KERNEL);
436 if(host_ldt_entries == NULL) {
437 printk("ldt_get_host_info: couldn't allocate host ldt list\n");
442 for(i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++){
443 if(ldt[i].a != 0 || ldt[i].b != 0) {
444 host_ldt_entries[k++] = i;
447 host_ldt_entries[k] = -1;
450 free_pages((unsigned long)ldt, order);
453 long init_new_ldt(struct mmu_context_skas * new_mm,
454 struct mmu_context_skas * from_mm)
456 struct user_desc desc;
461 struct proc_mm_op copy;
465 init_MUTEX(&new_mm->ldt.semaphore);
468 memset(&desc, 0, sizeof(desc));
470 * We have to initialize a clean ldt.
474 * If the new mm was created using proc_mm, host's
475 * default-ldt currently is assigned, which normally
476 * contains the call-gates for lcall7 and lcall27.
477 * To remove these gates, we simply write an empty
478 * entry as number 0 to the host.
480 err = write_ldt_entry(&new_mm->id, 1, &desc,
485 * Now we try to retrieve info about the ldt, we
486 * inherited from the host. All ldt-entries found
487 * will be reset in the following loop
489 if(host_ldt_entries == NULL)
491 for(num_p=host_ldt_entries; *num_p != -1; num_p++){
492 desc.entry_number = *num_p;
493 err = write_ldt_entry(&new_mm->id, 1, &desc,
494 &addr, *(num_p + 1) == -1);
499 new_mm->ldt.entry_count = 0;
505 /* We have a valid from_mm, so we now have to copy the LDT of
506 * from_mm to new_mm, because using proc_mm an new mm with
507 * an empty/default LDT was created in new_mm()
509 copy = ((struct proc_mm_op) { .op = MM_COPY_SEGMENTS,
512 from_mm->id.u.mm_fd } } );
513 i = os_write_file(new_mm->id.u.mm_fd, ©, sizeof(copy));
514 if(i != sizeof(copy))
515 printk("new_mm : /proc/mm copy_segments failed, "
520 /* Our local LDT is used to supply the data for
521 * modify_ldt(READLDT), if PTRACE_LDT isn't available,
522 * i.e., we have to use the stub for modify_ldt, which
523 * can't handle the big read buffer of up to 64kB.
525 down(&from_mm->ldt.semaphore);
526 if(from_mm->ldt.entry_count <= LDT_DIRECT_ENTRIES){
527 memcpy(new_mm->ldt.u.entries, from_mm->ldt.u.entries,
528 sizeof(new_mm->ldt.u.entries));
531 i = from_mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
533 page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
538 new_mm->ldt.u.pages[i] =
539 (struct ldt_entry *) page;
540 memcpy(new_mm->ldt.u.pages[i],
541 from_mm->ldt.u.pages[i], PAGE_SIZE);
544 new_mm->ldt.entry_count = from_mm->ldt.entry_count;
545 up(&from_mm->ldt.semaphore);
553 void free_ldt(struct mmu_context_skas * mm)
557 if(!ptrace_ldt && mm->ldt.entry_count > LDT_DIRECT_ENTRIES){
558 i = mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
560 free_page((long )mm->ldt.u.pages[i]);
563 mm->ldt.entry_count = 0;
567 int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
569 return(CHOOSE_MODE_PROC(do_modify_ldt_tt, do_modify_ldt_skas, func,