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 "asm/uaccess.h"
13 #include "asm/unistd.h"
14 #include "choose-mode.h"
16 #include "mode_kern.h"
19 extern int modify_ldt(int func
, void *ptr
, unsigned long bytecount
);
23 static long do_modify_ldt_tt(int func
, void __user
*ptr
,
24 unsigned long bytecount
)
26 struct user_desc info
;
29 void *p
= NULL
; /* What we pass to host. */
33 case 0x11: /* write_ldt */
34 /* Do this check now to avoid overflows. */
35 if (bytecount
!= sizeof(struct user_desc
)) {
40 if(copy_from_user(&info
, ptr
, sizeof(info
))) {
48 case 2: /* read_ldt */
50 /* The use of info avoids kmalloc on the write case, not on the
52 buf
= kmalloc(bytecount
, GFP_KERNEL
);
64 res
= modify_ldt(func
, p
, bytecount
);
71 /* Modify_ldt was for reading and returned the number of read
73 if(copy_to_user(ptr
, p
, res
))
85 #ifdef CONFIG_MODE_SKAS
88 #include "skas_ptrace.h"
89 #include "asm/mmu_context.h"
92 long write_ldt_entry(struct mm_id
* mm_idp
, int func
, struct user_desc
* desc
,
93 void **addr
, int done
)
98 /* This is a special handling for the case, that the mm to
99 * modify isn't current->active_mm.
100 * If this is called directly by modify_ldt,
101 * (current->active_mm->context.skas.u == mm_idp)
102 * will be true. So no call to switch_mm_skas(mm_idp) is done.
103 * If this is called in case of init_new_ldt or PTRACE_LDT,
104 * mm_idp won't belong to current->active_mm, but child->mm.
105 * So we need to switch child's mm into our userspace, then
108 * Note: I'm unsure: should interrupts be disabled here?
110 if(!current
->active_mm
|| current
->active_mm
== &init_mm
||
111 mm_idp
!= ¤t
->active_mm
->context
.skas
.id
)
112 switch_mm_skas(mm_idp
);
116 struct ptrace_ldt ldt_op
= (struct ptrace_ldt
) {
119 .bytecount
= sizeof(*desc
)};
127 pid
= userspace_pid
[cpu
];
130 res
= os_ptrace_ldt(pid
, 0, (unsigned long) &ldt_op
);
137 res
= syscall_stub_data(mm_idp
, (unsigned long *)desc
,
138 (sizeof(*desc
) + sizeof(long) - 1) &
142 unsigned long args
[] = { func
,
143 (unsigned long)stub_addr
,
146 res
= run_syscall_stub(mm_idp
, __NR_modify_ldt
, args
,
152 /* This is the second part of special handling, that makes
153 * PTRACE_LDT possible to implement.
155 if(current
->active_mm
&& current
->active_mm
!= &init_mm
&&
156 mm_idp
!= ¤t
->active_mm
->context
.skas
.id
)
157 switch_mm_skas(¤t
->active_mm
->context
.skas
.id
);
163 static long read_ldt_from_host(void __user
* ptr
, unsigned long bytecount
)
166 struct ptrace_ldt ptrace_ldt
= (struct ptrace_ldt
) {
168 .bytecount
= bytecount
,
169 .ptr
= kmalloc(bytecount
, GFP_KERNEL
)};
172 if(ptrace_ldt
.ptr
== NULL
)
175 /* This is called from sys_modify_ldt only, so userspace_pid gives
176 * us the right number
180 res
= os_ptrace_ldt(userspace_pid
[cpu
], 0, (unsigned long) &ptrace_ldt
);
185 n
= copy_to_user(ptr
, ptrace_ldt
.ptr
, res
);
190 kfree(ptrace_ldt
.ptr
);
196 * In skas mode, we hold our own ldt data in UML.
197 * Thus, the code implementing sys_modify_ldt_skas
198 * is very similar to (and mostly stolen from) sys_modify_ldt
199 * for arch/i386/kernel/ldt.c
200 * The routines copied and modified in part are:
204 * - sys_modify_ldt_skas
207 static int read_ldt(void __user
* ptr
, unsigned long bytecount
)
211 uml_ldt_t
* ldt
= ¤t
->mm
->context
.skas
.ldt
;
213 if(!ldt
->entry_count
)
215 if(bytecount
> LDT_ENTRY_SIZE
*LDT_ENTRIES
)
216 bytecount
= LDT_ENTRY_SIZE
*LDT_ENTRIES
;
220 return read_ldt_from_host(ptr
, bytecount
);
223 down(&ldt
->semaphore
);
224 if(ldt
->entry_count
<= LDT_DIRECT_ENTRIES
){
225 size
= LDT_ENTRY_SIZE
*LDT_DIRECT_ENTRIES
;
228 if(copy_to_user(ptr
, ldt
->u
.entries
, size
))
234 for(i
=0; i
<ldt
->entry_count
/LDT_ENTRIES_PER_PAGE
&& bytecount
;
239 if(copy_to_user(ptr
, ldt
->u
.pages
[i
], size
)){
249 if(bytecount
== 0 || err
== -EFAULT
)
252 if(clear_user(ptr
, bytecount
))
259 static int read_default_ldt(void __user
* ptr
, unsigned long bytecount
)
263 if(bytecount
> 5*LDT_ENTRY_SIZE
)
264 bytecount
= 5*LDT_ENTRY_SIZE
;
267 /* UML doesn't support lcall7 and lcall27.
268 * So, we don't really have a default ldt, but emulate
269 * an empty ldt of common host default ldt size.
271 if(clear_user(ptr
, bytecount
))
277 static int write_ldt(void __user
* ptr
, unsigned long bytecount
, int func
)
279 uml_ldt_t
* ldt
= ¤t
->mm
->context
.skas
.ldt
;
280 struct mm_id
* mm_idp
= ¤t
->mm
->context
.skas
.id
;
282 struct user_desc ldt_info
;
283 struct ldt_entry entry0
, *ldt_p
;
287 if(bytecount
!= sizeof(ldt_info
))
290 if(copy_from_user(&ldt_info
, ptr
, sizeof(ldt_info
)))
294 if(ldt_info
.entry_number
>= LDT_ENTRIES
)
296 if(ldt_info
.contents
== 3){
299 if (ldt_info
.seg_not_present
== 0)
304 down(&ldt
->semaphore
);
306 err
= write_ldt_entry(mm_idp
, func
, &ldt_info
, &addr
, 1);
309 else if(ptrace_ldt
) {
310 /* With PTRACE_LDT available, this is used as a flag only */
311 ldt
->entry_count
= 1;
315 if(ldt_info
.entry_number
>= ldt
->entry_count
&&
316 ldt_info
.entry_number
>= LDT_DIRECT_ENTRIES
){
317 for(i
=ldt
->entry_count
/LDT_ENTRIES_PER_PAGE
;
318 i
*LDT_ENTRIES_PER_PAGE
<= ldt_info
.entry_number
;
321 memcpy(&entry0
, ldt
->u
.entries
,
323 ldt
->u
.pages
[i
] = (struct ldt_entry
*)
324 __get_free_page(GFP_KERNEL
|__GFP_ZERO
);
325 if(!ldt
->u
.pages
[i
]){
327 /* Undo the change in host */
328 memset(&ldt_info
, 0, sizeof(ldt_info
));
329 write_ldt_entry(mm_idp
, 1, &ldt_info
, &addr
, 1);
333 memcpy(ldt
->u
.pages
[0], &entry0
,
335 memcpy(ldt
->u
.pages
[0]+1, ldt
->u
.entries
+1,
336 sizeof(entry0
)*(LDT_DIRECT_ENTRIES
-1));
338 ldt
->entry_count
= (i
+ 1) * LDT_ENTRIES_PER_PAGE
;
341 if(ldt
->entry_count
<= ldt_info
.entry_number
)
342 ldt
->entry_count
= ldt_info
.entry_number
+ 1;
344 if(ldt
->entry_count
<= LDT_DIRECT_ENTRIES
)
345 ldt_p
= ldt
->u
.entries
+ ldt_info
.entry_number
;
347 ldt_p
= ldt
->u
.pages
[ldt_info
.entry_number
/LDT_ENTRIES_PER_PAGE
] +
348 ldt_info
.entry_number
%LDT_ENTRIES_PER_PAGE
;
350 if(ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0 &&
351 (func
== 1 || LDT_empty(&ldt_info
))){
357 ldt_info
.useable
= 0;
358 ldt_p
->a
= LDT_entry_a(&ldt_info
);
359 ldt_p
->b
= LDT_entry_b(&ldt_info
);
369 static long do_modify_ldt_skas(int func
, void __user
*ptr
,
370 unsigned long bytecount
)
376 ret
= read_ldt(ptr
, bytecount
);
380 ret
= write_ldt(ptr
, bytecount
, func
);
383 ret
= read_default_ldt(ptr
, bytecount
);
389 short dummy_list
[9] = {0, -1};
390 short * host_ldt_entries
= NULL
;
392 void ldt_get_host_info(void)
395 struct ldt_entry
* ldt
;
396 int i
, size
, k
, order
;
398 host_ldt_entries
= dummy_list
+1;
400 for(i
= LDT_PAGES_MAX
-1, order
=0; i
; i
>>=1, order
++);
402 ldt
= (struct ldt_entry
*)
403 __get_free_pages(GFP_KERNEL
|__GFP_ZERO
, order
);
405 printk("ldt_get_host_info: couldn't allocate buffer for host ldt\n");
409 ret
= modify_ldt(0, ldt
, (1<<order
)*PAGE_SIZE
);
411 printk("ldt_get_host_info: couldn't read host ldt\n");
415 /* default_ldt is active, simply write an empty entry 0 */
416 host_ldt_entries
= dummy_list
;
420 for(i
=0, size
=0; i
<ret
/LDT_ENTRY_SIZE
; i
++){
421 if(ldt
[i
].a
!= 0 || ldt
[i
].b
!= 0)
425 if(size
< ARRAY_SIZE(dummy_list
))
426 host_ldt_entries
= dummy_list
;
428 size
= (size
+ 1) * sizeof(dummy_list
[0]);
429 host_ldt_entries
= kmalloc(size
, GFP_KERNEL
);
430 if(host_ldt_entries
== NULL
) {
431 printk("ldt_get_host_info: couldn't allocate host ldt list\n");
436 for(i
=0, k
=0; i
<ret
/LDT_ENTRY_SIZE
; i
++){
437 if(ldt
[i
].a
!= 0 || ldt
[i
].b
!= 0) {
438 host_ldt_entries
[k
++] = i
;
441 host_ldt_entries
[k
] = -1;
444 free_pages((unsigned long)ldt
, order
);
447 long init_new_ldt(struct mmu_context_skas
* new_mm
,
448 struct mmu_context_skas
* from_mm
)
450 struct user_desc desc
;
455 struct proc_mm_op copy
;
459 init_MUTEX(&new_mm
->ldt
.semaphore
);
462 memset(&desc
, 0, sizeof(desc
));
464 * We have to initialize a clean ldt.
468 * If the new mm was created using proc_mm, host's
469 * default-ldt currently is assigned, which normally
470 * contains the call-gates for lcall7 and lcall27.
471 * To remove these gates, we simply write an empty
472 * entry as number 0 to the host.
474 err
= write_ldt_entry(&new_mm
->id
, 1, &desc
,
479 * Now we try to retrieve info about the ldt, we
480 * inherited from the host. All ldt-entries found
481 * will be reset in the following loop
483 if(host_ldt_entries
== NULL
)
485 for(num_p
=host_ldt_entries
; *num_p
!= -1; num_p
++){
486 desc
.entry_number
= *num_p
;
487 err
= write_ldt_entry(&new_mm
->id
, 1, &desc
,
488 &addr
, *(num_p
+ 1) == -1);
493 new_mm
->ldt
.entry_count
= 0;
499 /* We have a valid from_mm, so we now have to copy the LDT of
500 * from_mm to new_mm, because using proc_mm an new mm with
501 * an empty/default LDT was created in new_mm()
503 copy
= ((struct proc_mm_op
) { .op
= MM_COPY_SEGMENTS
,
506 from_mm
->id
.u
.mm_fd
} } );
507 i
= os_write_file(new_mm
->id
.u
.mm_fd
, ©
, sizeof(copy
));
508 if(i
!= sizeof(copy
))
509 printk("new_mm : /proc/mm copy_segments failed, "
514 /* Our local LDT is used to supply the data for
515 * modify_ldt(READLDT), if PTRACE_LDT isn't available,
516 * i.e., we have to use the stub for modify_ldt, which
517 * can't handle the big read buffer of up to 64kB.
519 down(&from_mm
->ldt
.semaphore
);
520 if(from_mm
->ldt
.entry_count
<= LDT_DIRECT_ENTRIES
){
521 memcpy(new_mm
->ldt
.u
.entries
, from_mm
->ldt
.u
.entries
,
522 sizeof(new_mm
->ldt
.u
.entries
));
525 i
= from_mm
->ldt
.entry_count
/ LDT_ENTRIES_PER_PAGE
;
527 page
= __get_free_page(GFP_KERNEL
|__GFP_ZERO
);
532 new_mm
->ldt
.u
.pages
[i
] =
533 (struct ldt_entry
*) page
;
534 memcpy(new_mm
->ldt
.u
.pages
[i
],
535 from_mm
->ldt
.u
.pages
[i
], PAGE_SIZE
);
538 new_mm
->ldt
.entry_count
= from_mm
->ldt
.entry_count
;
539 up(&from_mm
->ldt
.semaphore
);
547 void free_ldt(struct mmu_context_skas
* mm
)
551 if(!ptrace_ldt
&& mm
->ldt
.entry_count
> LDT_DIRECT_ENTRIES
){
552 i
= mm
->ldt
.entry_count
/ LDT_ENTRIES_PER_PAGE
;
554 free_page((long )mm
->ldt
.u
.pages
[i
]);
557 mm
->ldt
.entry_count
= 0;
561 int sys_modify_ldt(int func
, void __user
*ptr
, unsigned long bytecount
)
563 return(CHOOSE_MODE_PROC(do_modify_ldt_tt
, do_modify_ldt_skas
, func
,