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 unsure: 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 = os_ptrace_ldt(pid, 0, (unsigned long) &ldt_op);
139 res = syscall_stub_data(mm_idp, (unsigned long *)desc,
140 (sizeof(*desc) + sizeof(long) - 1) &
144 unsigned long args[] = { func,
145 (unsigned long)stub_addr,
148 res = run_syscall_stub(mm_idp, __NR_modify_ldt, args,
154 /* This is the second part of special handling, that makes
155 * PTRACE_LDT possible to implement.
157 if(current->active_mm && current->active_mm != &init_mm &&
158 mm_idp != ¤t->active_mm->context.skas.id)
159 switch_mm_skas(¤t->active_mm->context.skas.id);
165 static long read_ldt_from_host(void __user * ptr, unsigned long bytecount)
168 struct ptrace_ldt ptrace_ldt = (struct ptrace_ldt) {
170 .bytecount = bytecount,
171 .ptr = (void *)kmalloc(bytecount, GFP_KERNEL)};
174 if(ptrace_ldt.ptr == NULL)
177 /* This is called from sys_modify_ldt only, so userspace_pid gives
178 * us the right number
182 res = os_ptrace_ldt(userspace_pid[cpu], 0, (unsigned long) &ptrace_ldt);
187 n = copy_to_user(ptr, ptrace_ldt.ptr, res);
192 kfree(ptrace_ldt.ptr);
198 * In skas mode, we hold our own ldt data in UML.
199 * Thus, the code implementing sys_modify_ldt_skas
200 * is very similar to (and mostly stolen from) sys_modify_ldt
201 * for arch/i386/kernel/ldt.c
202 * The routines copied and modified in part are:
206 * - sys_modify_ldt_skas
209 static int read_ldt(void __user * ptr, unsigned long bytecount)
213 uml_ldt_t * ldt = ¤t->mm->context.skas.ldt;
215 if(!ldt->entry_count)
217 if(bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
218 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
222 return read_ldt_from_host(ptr, bytecount);
225 down(&ldt->semaphore);
226 if(ldt->entry_count <= LDT_DIRECT_ENTRIES){
227 size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES;
230 if(copy_to_user(ptr, ldt->u.entries, size))
236 for(i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount;
241 if(copy_to_user(ptr, ldt->u.pages[i], size)){
251 if(bytecount == 0 || err == -EFAULT)
254 if(clear_user(ptr, bytecount))
261 static int read_default_ldt(void __user * ptr, unsigned long bytecount)
265 if(bytecount > 5*LDT_ENTRY_SIZE)
266 bytecount = 5*LDT_ENTRY_SIZE;
269 /* UML doesn't support lcall7 and lcall27.
270 * So, we don't really have a default ldt, but emulate
271 * an empty ldt of common host default ldt size.
273 if(clear_user(ptr, bytecount))
279 static int write_ldt(void __user * ptr, unsigned long bytecount, int func)
281 uml_ldt_t * ldt = ¤t->mm->context.skas.ldt;
282 struct mm_id * mm_idp = ¤t->mm->context.skas.id;
284 struct user_desc ldt_info;
285 struct ldt_entry entry0, *ldt_p;
289 if(bytecount != sizeof(ldt_info))
292 if(copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
296 if(ldt_info.entry_number >= LDT_ENTRIES)
298 if(ldt_info.contents == 3){
301 if (ldt_info.seg_not_present == 0)
306 down(&ldt->semaphore);
308 err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1);
311 else if(ptrace_ldt) {
312 /* With PTRACE_LDT available, this is used as a flag only */
313 ldt->entry_count = 1;
317 if(ldt_info.entry_number >= ldt->entry_count &&
318 ldt_info.entry_number >= LDT_DIRECT_ENTRIES){
319 for(i=ldt->entry_count/LDT_ENTRIES_PER_PAGE;
320 i*LDT_ENTRIES_PER_PAGE <= ldt_info.entry_number;
323 memcpy(&entry0, ldt->u.entries,
325 ldt->u.pages[i] = (struct ldt_entry *)
326 __get_free_page(GFP_KERNEL|__GFP_ZERO);
327 if(!ldt->u.pages[i]){
329 /* Undo the change in host */
330 memset(&ldt_info, 0, sizeof(ldt_info));
331 write_ldt_entry(mm_idp, 1, &ldt_info, &addr, 1);
335 memcpy(ldt->u.pages[0], &entry0,
337 memcpy(ldt->u.pages[0]+1, ldt->u.entries+1,
338 sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
340 ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
343 if(ldt->entry_count <= ldt_info.entry_number)
344 ldt->entry_count = ldt_info.entry_number + 1;
346 if(ldt->entry_count <= LDT_DIRECT_ENTRIES)
347 ldt_p = ldt->u.entries + ldt_info.entry_number;
349 ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
350 ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
352 if(ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
353 (func == 1 || LDT_empty(&ldt_info))){
359 ldt_info.useable = 0;
360 ldt_p->a = LDT_entry_a(&ldt_info);
361 ldt_p->b = LDT_entry_b(&ldt_info);
371 static long do_modify_ldt_skas(int func, void __user *ptr,
372 unsigned long bytecount)
378 ret = read_ldt(ptr, bytecount);
382 ret = write_ldt(ptr, bytecount, func);
385 ret = read_default_ldt(ptr, bytecount);
391 short dummy_list[9] = {0, -1};
392 short * host_ldt_entries = NULL;
394 void ldt_get_host_info(void)
397 struct ldt_entry * ldt;
398 int i, size, k, order;
400 host_ldt_entries = dummy_list+1;
402 for(i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++);
404 ldt = (struct ldt_entry *)
405 __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
407 printk("ldt_get_host_info: couldn't allocate buffer for host ldt\n");
411 ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
413 printk("ldt_get_host_info: couldn't read host ldt\n");
417 /* default_ldt is active, simply write an empty entry 0 */
418 host_ldt_entries = dummy_list;
422 for(i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++){
423 if(ldt[i].a != 0 || ldt[i].b != 0)
427 if(size < sizeof(dummy_list)/sizeof(dummy_list[0])) {
428 host_ldt_entries = dummy_list;
431 size = (size + 1) * sizeof(dummy_list[0]);
432 host_ldt_entries = (short *)kmalloc(size, GFP_KERNEL);
433 if(host_ldt_entries == NULL) {
434 printk("ldt_get_host_info: couldn't allocate host ldt list\n");
439 for(i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++){
440 if(ldt[i].a != 0 || ldt[i].b != 0) {
441 host_ldt_entries[k++] = i;
444 host_ldt_entries[k] = -1;
447 free_pages((unsigned long)ldt, order);
450 long init_new_ldt(struct mmu_context_skas * new_mm,
451 struct mmu_context_skas * from_mm)
453 struct user_desc desc;
458 struct proc_mm_op copy;
462 init_MUTEX(&new_mm->ldt.semaphore);
465 memset(&desc, 0, sizeof(desc));
467 * We have to initialize a clean ldt.
471 * If the new mm was created using proc_mm, host's
472 * default-ldt currently is assigned, which normally
473 * contains the call-gates for lcall7 and lcall27.
474 * To remove these gates, we simply write an empty
475 * entry as number 0 to the host.
477 err = write_ldt_entry(&new_mm->id, 1, &desc,
482 * Now we try to retrieve info about the ldt, we
483 * inherited from the host. All ldt-entries found
484 * will be reset in the following loop
486 if(host_ldt_entries == NULL)
488 for(num_p=host_ldt_entries; *num_p != -1; num_p++){
489 desc.entry_number = *num_p;
490 err = write_ldt_entry(&new_mm->id, 1, &desc,
491 &addr, *(num_p + 1) == -1);
496 new_mm->ldt.entry_count = 0;
502 /* We have a valid from_mm, so we now have to copy the LDT of
503 * from_mm to new_mm, because using proc_mm an new mm with
504 * an empty/default LDT was created in new_mm()
506 copy = ((struct proc_mm_op) { .op = MM_COPY_SEGMENTS,
509 from_mm->id.u.mm_fd } } );
510 i = os_write_file(new_mm->id.u.mm_fd, ©, sizeof(copy));
511 if(i != sizeof(copy))
512 printk("new_mm : /proc/mm copy_segments failed, "
517 /* Our local LDT is used to supply the data for
518 * modify_ldt(READLDT), if PTRACE_LDT isn't available,
519 * i.e., we have to use the stub for modify_ldt, which
520 * can't handle the big read buffer of up to 64kB.
522 down(&from_mm->ldt.semaphore);
523 if(from_mm->ldt.entry_count <= LDT_DIRECT_ENTRIES){
524 memcpy(new_mm->ldt.u.entries, from_mm->ldt.u.entries,
525 sizeof(new_mm->ldt.u.entries));
528 i = from_mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
530 page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
535 new_mm->ldt.u.pages[i] =
536 (struct ldt_entry *) page;
537 memcpy(new_mm->ldt.u.pages[i],
538 from_mm->ldt.u.pages[i], PAGE_SIZE);
541 new_mm->ldt.entry_count = from_mm->ldt.entry_count;
542 up(&from_mm->ldt.semaphore);
550 void free_ldt(struct mmu_context_skas * mm)
554 if(!ptrace_ldt && mm->ldt.entry_count > LDT_DIRECT_ENTRIES){
555 i = mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
557 free_page((long )mm->ldt.u.pages[i]);
560 mm->ldt.entry_count = 0;
564 int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
566 return(CHOOSE_MODE_PROC(do_modify_ldt_tt, do_modify_ldt_skas, func,