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"
18 extern int modify_ldt(int func
, void *ptr
, unsigned long bytecount
);
21 #include "skas_ptrace.h"
22 #include "asm/mmu_context.h"
25 long write_ldt_entry(struct mm_id
* mm_idp
, int func
, struct user_desc
* desc
,
26 void **addr
, int done
)
31 /* This is a special handling for the case, that the mm to
32 * modify isn't current->active_mm.
33 * If this is called directly by modify_ldt,
34 * (current->active_mm->context.skas.u == mm_idp)
35 * will be true. So no call to __switch_mm(mm_idp) is done.
36 * If this is called in case of init_new_ldt or PTRACE_LDT,
37 * mm_idp won't belong to current->active_mm, but child->mm.
38 * So we need to switch child's mm into our userspace, then
41 * Note: I'm unsure: should interrupts be disabled here?
43 if(!current
->active_mm
|| current
->active_mm
== &init_mm
||
44 mm_idp
!= ¤t
->active_mm
->context
.skas
.id
)
49 struct ptrace_ldt ldt_op
= (struct ptrace_ldt
) {
52 .bytecount
= sizeof(*desc
)};
60 pid
= userspace_pid
[cpu
];
63 res
= os_ptrace_ldt(pid
, 0, (unsigned long) &ldt_op
);
70 res
= syscall_stub_data(mm_idp
, (unsigned long *)desc
,
71 (sizeof(*desc
) + sizeof(long) - 1) &
75 unsigned long args
[] = { func
,
76 (unsigned long)stub_addr
,
79 res
= run_syscall_stub(mm_idp
, __NR_modify_ldt
, args
,
85 /* This is the second part of special handling, that makes
86 * PTRACE_LDT possible to implement.
88 if(current
->active_mm
&& current
->active_mm
!= &init_mm
&&
89 mm_idp
!= ¤t
->active_mm
->context
.skas
.id
)
90 __switch_mm(¤t
->active_mm
->context
.skas
.id
);
96 static long read_ldt_from_host(void __user
* ptr
, unsigned long bytecount
)
99 struct ptrace_ldt ptrace_ldt
= (struct ptrace_ldt
) {
101 .bytecount
= bytecount
,
102 .ptr
= kmalloc(bytecount
, GFP_KERNEL
)};
105 if(ptrace_ldt
.ptr
== NULL
)
108 /* This is called from sys_modify_ldt only, so userspace_pid gives
109 * us the right number
113 res
= os_ptrace_ldt(userspace_pid
[cpu
], 0, (unsigned long) &ptrace_ldt
);
118 n
= copy_to_user(ptr
, ptrace_ldt
.ptr
, res
);
123 kfree(ptrace_ldt
.ptr
);
129 * In skas mode, we hold our own ldt data in UML.
130 * Thus, the code implementing sys_modify_ldt_skas
131 * is very similar to (and mostly stolen from) sys_modify_ldt
132 * for arch/i386/kernel/ldt.c
133 * The routines copied and modified in part are:
137 * - sys_modify_ldt_skas
140 static int read_ldt(void __user
* ptr
, unsigned long bytecount
)
144 uml_ldt_t
* ldt
= ¤t
->mm
->context
.skas
.ldt
;
146 if(!ldt
->entry_count
)
148 if(bytecount
> LDT_ENTRY_SIZE
*LDT_ENTRIES
)
149 bytecount
= LDT_ENTRY_SIZE
*LDT_ENTRIES
;
153 return read_ldt_from_host(ptr
, bytecount
);
156 down(&ldt
->semaphore
);
157 if(ldt
->entry_count
<= LDT_DIRECT_ENTRIES
){
158 size
= LDT_ENTRY_SIZE
*LDT_DIRECT_ENTRIES
;
161 if(copy_to_user(ptr
, ldt
->u
.entries
, size
))
167 for(i
=0; i
<ldt
->entry_count
/LDT_ENTRIES_PER_PAGE
&& bytecount
;
172 if(copy_to_user(ptr
, ldt
->u
.pages
[i
], size
)){
182 if(bytecount
== 0 || err
== -EFAULT
)
185 if(clear_user(ptr
, bytecount
))
192 static int read_default_ldt(void __user
* ptr
, unsigned long bytecount
)
196 if(bytecount
> 5*LDT_ENTRY_SIZE
)
197 bytecount
= 5*LDT_ENTRY_SIZE
;
200 /* UML doesn't support lcall7 and lcall27.
201 * So, we don't really have a default ldt, but emulate
202 * an empty ldt of common host default ldt size.
204 if(clear_user(ptr
, bytecount
))
210 static int write_ldt(void __user
* ptr
, unsigned long bytecount
, int func
)
212 uml_ldt_t
* ldt
= ¤t
->mm
->context
.skas
.ldt
;
213 struct mm_id
* mm_idp
= ¤t
->mm
->context
.skas
.id
;
215 struct user_desc ldt_info
;
216 struct ldt_entry entry0
, *ldt_p
;
220 if(bytecount
!= sizeof(ldt_info
))
223 if(copy_from_user(&ldt_info
, ptr
, sizeof(ldt_info
)))
227 if(ldt_info
.entry_number
>= LDT_ENTRIES
)
229 if(ldt_info
.contents
== 3){
232 if (ldt_info
.seg_not_present
== 0)
237 down(&ldt
->semaphore
);
239 err
= write_ldt_entry(mm_idp
, func
, &ldt_info
, &addr
, 1);
242 else if(ptrace_ldt
) {
243 /* With PTRACE_LDT available, this is used as a flag only */
244 ldt
->entry_count
= 1;
248 if(ldt_info
.entry_number
>= ldt
->entry_count
&&
249 ldt_info
.entry_number
>= LDT_DIRECT_ENTRIES
){
250 for(i
=ldt
->entry_count
/LDT_ENTRIES_PER_PAGE
;
251 i
*LDT_ENTRIES_PER_PAGE
<= ldt_info
.entry_number
;
254 memcpy(&entry0
, ldt
->u
.entries
,
256 ldt
->u
.pages
[i
] = (struct ldt_entry
*)
257 __get_free_page(GFP_KERNEL
|__GFP_ZERO
);
258 if(!ldt
->u
.pages
[i
]){
260 /* Undo the change in host */
261 memset(&ldt_info
, 0, sizeof(ldt_info
));
262 write_ldt_entry(mm_idp
, 1, &ldt_info
, &addr
, 1);
266 memcpy(ldt
->u
.pages
[0], &entry0
,
268 memcpy(ldt
->u
.pages
[0]+1, ldt
->u
.entries
+1,
269 sizeof(entry0
)*(LDT_DIRECT_ENTRIES
-1));
271 ldt
->entry_count
= (i
+ 1) * LDT_ENTRIES_PER_PAGE
;
274 if(ldt
->entry_count
<= ldt_info
.entry_number
)
275 ldt
->entry_count
= ldt_info
.entry_number
+ 1;
277 if(ldt
->entry_count
<= LDT_DIRECT_ENTRIES
)
278 ldt_p
= ldt
->u
.entries
+ ldt_info
.entry_number
;
280 ldt_p
= ldt
->u
.pages
[ldt_info
.entry_number
/LDT_ENTRIES_PER_PAGE
] +
281 ldt_info
.entry_number
%LDT_ENTRIES_PER_PAGE
;
283 if(ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0 &&
284 (func
== 1 || LDT_empty(&ldt_info
))){
290 ldt_info
.useable
= 0;
291 ldt_p
->a
= LDT_entry_a(&ldt_info
);
292 ldt_p
->b
= LDT_entry_b(&ldt_info
);
302 static long do_modify_ldt_skas(int func
, void __user
*ptr
,
303 unsigned long bytecount
)
309 ret
= read_ldt(ptr
, bytecount
);
313 ret
= write_ldt(ptr
, bytecount
, func
);
316 ret
= read_default_ldt(ptr
, bytecount
);
322 static DEFINE_SPINLOCK(host_ldt_lock
);
323 static short dummy_list
[9] = {0, -1};
324 static short * host_ldt_entries
= NULL
;
326 static void ldt_get_host_info(void)
329 struct ldt_entry
* ldt
;
331 int i
, size
, k
, order
;
333 spin_lock(&host_ldt_lock
);
335 if(host_ldt_entries
!= NULL
){
336 spin_unlock(&host_ldt_lock
);
339 host_ldt_entries
= dummy_list
+1;
341 spin_unlock(&host_ldt_lock
);
343 for(i
= LDT_PAGES_MAX
-1, order
=0; i
; i
>>=1, order
++);
345 ldt
= (struct ldt_entry
*)
346 __get_free_pages(GFP_KERNEL
|__GFP_ZERO
, order
);
348 printk("ldt_get_host_info: couldn't allocate buffer for host "
353 ret
= modify_ldt(0, ldt
, (1<<order
)*PAGE_SIZE
);
355 printk("ldt_get_host_info: couldn't read host ldt\n");
359 /* default_ldt is active, simply write an empty entry 0 */
360 host_ldt_entries
= dummy_list
;
364 for(i
=0, size
=0; i
<ret
/LDT_ENTRY_SIZE
; i
++){
365 if(ldt
[i
].a
!= 0 || ldt
[i
].b
!= 0)
369 if(size
< ARRAY_SIZE(dummy_list
))
370 host_ldt_entries
= dummy_list
;
372 size
= (size
+ 1) * sizeof(dummy_list
[0]);
373 tmp
= kmalloc(size
, GFP_KERNEL
);
375 printk("ldt_get_host_info: couldn't allocate host ldt "
379 host_ldt_entries
= tmp
;
382 for(i
=0, k
=0; i
<ret
/LDT_ENTRY_SIZE
; i
++){
383 if(ldt
[i
].a
!= 0 || ldt
[i
].b
!= 0) {
384 host_ldt_entries
[k
++] = i
;
387 host_ldt_entries
[k
] = -1;
390 free_pages((unsigned long)ldt
, order
);
393 long init_new_ldt(struct mmu_context_skas
* new_mm
,
394 struct mmu_context_skas
* from_mm
)
396 struct user_desc desc
;
401 struct proc_mm_op copy
;
405 init_MUTEX(&new_mm
->ldt
.semaphore
);
408 memset(&desc
, 0, sizeof(desc
));
410 * We have to initialize a clean ldt.
414 * If the new mm was created using proc_mm, host's
415 * default-ldt currently is assigned, which normally
416 * contains the call-gates for lcall7 and lcall27.
417 * To remove these gates, we simply write an empty
418 * entry as number 0 to the host.
420 err
= write_ldt_entry(&new_mm
->id
, 1, &desc
,
425 * Now we try to retrieve info about the ldt, we
426 * inherited from the host. All ldt-entries found
427 * will be reset in the following loop
430 for(num_p
=host_ldt_entries
; *num_p
!= -1; num_p
++){
431 desc
.entry_number
= *num_p
;
432 err
= write_ldt_entry(&new_mm
->id
, 1, &desc
,
433 &addr
, *(num_p
+ 1) == -1);
438 new_mm
->ldt
.entry_count
= 0;
444 /* We have a valid from_mm, so we now have to copy the LDT of
445 * from_mm to new_mm, because using proc_mm an new mm with
446 * an empty/default LDT was created in new_mm()
448 copy
= ((struct proc_mm_op
) { .op
= MM_COPY_SEGMENTS
,
451 from_mm
->id
.u
.mm_fd
} } );
452 i
= os_write_file(new_mm
->id
.u
.mm_fd
, ©
, sizeof(copy
));
453 if(i
!= sizeof(copy
))
454 printk("new_mm : /proc/mm copy_segments failed, "
459 /* Our local LDT is used to supply the data for
460 * modify_ldt(READLDT), if PTRACE_LDT isn't available,
461 * i.e., we have to use the stub for modify_ldt, which
462 * can't handle the big read buffer of up to 64kB.
464 down(&from_mm
->ldt
.semaphore
);
465 if(from_mm
->ldt
.entry_count
<= LDT_DIRECT_ENTRIES
){
466 memcpy(new_mm
->ldt
.u
.entries
, from_mm
->ldt
.u
.entries
,
467 sizeof(new_mm
->ldt
.u
.entries
));
470 i
= from_mm
->ldt
.entry_count
/ LDT_ENTRIES_PER_PAGE
;
472 page
= __get_free_page(GFP_KERNEL
|__GFP_ZERO
);
477 new_mm
->ldt
.u
.pages
[i
] =
478 (struct ldt_entry
*) page
;
479 memcpy(new_mm
->ldt
.u
.pages
[i
],
480 from_mm
->ldt
.u
.pages
[i
], PAGE_SIZE
);
483 new_mm
->ldt
.entry_count
= from_mm
->ldt
.entry_count
;
484 up(&from_mm
->ldt
.semaphore
);
492 void free_ldt(struct mmu_context_skas
* mm
)
496 if(!ptrace_ldt
&& mm
->ldt
.entry_count
> LDT_DIRECT_ENTRIES
){
497 i
= mm
->ldt
.entry_count
/ LDT_ENTRIES_PER_PAGE
;
499 free_page((long )mm
->ldt
.u
.pages
[i
]);
502 mm
->ldt
.entry_count
= 0;
505 int sys_modify_ldt(int func
, void __user
*ptr
, unsigned long bytecount
)
507 return do_modify_ldt_skas(func
, ptr
, bytecount
);