]> err.no Git - linux-2.6/blob - arch/um/sys-i386/ldt.c
uml: throw out CHOOSE_MODE
[linux-2.6] / arch / um / sys-i386 / ldt.c
1 /*
2  * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
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"
12 #include "asm/smp.h"
13 #include "asm/ldt.h"
14 #include "asm/unistd.h"
15 #include "kern.h"
16 #include "mode_kern.h"
17 #include "os.h"
18
19 extern int modify_ldt(int func, void *ptr, unsigned long bytecount);
20
21 #include "skas.h"
22 #include "skas_ptrace.h"
23 #include "asm/mmu_context.h"
24 #include "proc_mm.h"
25
26 long write_ldt_entry(struct mm_id * mm_idp, int func, struct user_desc * desc,
27                      void **addr, int done)
28 {
29         long res;
30
31         if(proc_mm){
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
40                  * later switch back.
41                  *
42                  * Note: I'm unsure: should interrupts be disabled here?
43                  */
44                 if(!current->active_mm || current->active_mm == &init_mm ||
45                    mm_idp != &current->active_mm->context.skas.id)
46                         switch_mm_skas(mm_idp);
47         }
48
49         if(ptrace_ldt) {
50                 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
51                         .func = func,
52                         .ptr = desc,
53                         .bytecount = sizeof(*desc)};
54                 u32 cpu;
55                 int pid;
56
57                 if(!proc_mm)
58                         pid = mm_idp->u.pid;
59                 else {
60                         cpu = get_cpu();
61                         pid = userspace_pid[cpu];
62                 }
63
64                 res = os_ptrace_ldt(pid, 0, (unsigned long) &ldt_op);
65
66                 if(proc_mm)
67                         put_cpu();
68         }
69         else {
70                 void *stub_addr;
71                 res = syscall_stub_data(mm_idp, (unsigned long *)desc,
72                                         (sizeof(*desc) + sizeof(long) - 1) &
73                                             ~(sizeof(long) - 1),
74                                         addr, &stub_addr);
75                 if(!res){
76                         unsigned long args[] = { func,
77                                                  (unsigned long)stub_addr,
78                                                  sizeof(*desc),
79                                                  0, 0, 0 };
80                         res = run_syscall_stub(mm_idp, __NR_modify_ldt, args,
81                                                0, addr, done);
82                 }
83         }
84
85         if(proc_mm){
86                 /* This is the second part of special handling, that makes
87                  * PTRACE_LDT possible to implement.
88                  */
89                 if(current->active_mm && current->active_mm != &init_mm &&
90                    mm_idp != &current->active_mm->context.skas.id)
91                         switch_mm_skas(&current->active_mm->context.skas.id);
92         }
93
94         return res;
95 }
96
97 static long read_ldt_from_host(void __user * ptr, unsigned long bytecount)
98 {
99         int res, n;
100         struct ptrace_ldt ptrace_ldt = (struct ptrace_ldt) {
101                         .func = 0,
102                         .bytecount = bytecount,
103                         .ptr = kmalloc(bytecount, GFP_KERNEL)};
104         u32 cpu;
105
106         if(ptrace_ldt.ptr == NULL)
107                 return -ENOMEM;
108
109         /* This is called from sys_modify_ldt only, so userspace_pid gives
110          * us the right number
111          */
112
113         cpu = get_cpu();
114         res = os_ptrace_ldt(userspace_pid[cpu], 0, (unsigned long) &ptrace_ldt);
115         put_cpu();
116         if(res < 0)
117                 goto out;
118
119         n = copy_to_user(ptr, ptrace_ldt.ptr, res);
120         if(n != 0)
121                 res = -EFAULT;
122
123   out:
124         kfree(ptrace_ldt.ptr);
125
126         return res;
127 }
128
129 /*
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:
135  * - read_ldt
136  * - read_default_ldt
137  * - write_ldt
138  * - sys_modify_ldt_skas
139  */
140
141 static int read_ldt(void __user * ptr, unsigned long bytecount)
142 {
143         int i, err = 0;
144         unsigned long size;
145         uml_ldt_t * ldt = &current->mm->context.skas.ldt;
146
147         if(!ldt->entry_count)
148                 goto out;
149         if(bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
150                 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
151         err = bytecount;
152
153         if(ptrace_ldt){
154                 return read_ldt_from_host(ptr, bytecount);
155         }
156
157         down(&ldt->semaphore);
158         if(ldt->entry_count <= LDT_DIRECT_ENTRIES){
159                 size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES;
160                 if(size > bytecount)
161                         size = bytecount;
162                 if(copy_to_user(ptr, ldt->u.entries, size))
163                         err = -EFAULT;
164                 bytecount -= size;
165                 ptr += size;
166         }
167         else {
168                 for(i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount;
169                          i++){
170                         size = PAGE_SIZE;
171                         if(size > bytecount)
172                                 size = bytecount;
173                         if(copy_to_user(ptr, ldt->u.pages[i], size)){
174                                 err = -EFAULT;
175                                 break;
176                         }
177                         bytecount -= size;
178                         ptr += size;
179                 }
180         }
181         up(&ldt->semaphore);
182
183         if(bytecount == 0 || err == -EFAULT)
184                 goto out;
185
186         if(clear_user(ptr, bytecount))
187                 err = -EFAULT;
188
189 out:
190         return err;
191 }
192
193 static int read_default_ldt(void __user * ptr, unsigned long bytecount)
194 {
195         int err;
196
197         if(bytecount > 5*LDT_ENTRY_SIZE)
198                 bytecount = 5*LDT_ENTRY_SIZE;
199
200         err = bytecount;
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.
204          */
205         if(clear_user(ptr, bytecount))
206                 err = -EFAULT;
207
208         return err;
209 }
210
211 static int write_ldt(void __user * ptr, unsigned long bytecount, int func)
212 {
213         uml_ldt_t * ldt = &current->mm->context.skas.ldt;
214         struct mm_id * mm_idp = &current->mm->context.skas.id;
215         int i, err;
216         struct user_desc ldt_info;
217         struct ldt_entry entry0, *ldt_p;
218         void *addr = NULL;
219
220         err = -EINVAL;
221         if(bytecount != sizeof(ldt_info))
222                 goto out;
223         err = -EFAULT;
224         if(copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
225                 goto out;
226
227         err = -EINVAL;
228         if(ldt_info.entry_number >= LDT_ENTRIES)
229                 goto out;
230         if(ldt_info.contents == 3){
231                 if (func == 1)
232                         goto out;
233                 if (ldt_info.seg_not_present == 0)
234                         goto out;
235         }
236
237         if(!ptrace_ldt)
238                 down(&ldt->semaphore);
239
240         err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1);
241         if(err)
242                 goto out_unlock;
243         else if(ptrace_ldt) {
244         /* With PTRACE_LDT available, this is used as a flag only */
245                 ldt->entry_count = 1;
246                 goto out;
247         }
248
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;
253                     i++){
254                         if(i == 0)
255                                 memcpy(&entry0, ldt->u.entries,
256                                        sizeof(entry0));
257                         ldt->u.pages[i] = (struct ldt_entry *)
258                                 __get_free_page(GFP_KERNEL|__GFP_ZERO);
259                         if(!ldt->u.pages[i]){
260                                 err = -ENOMEM;
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);
264                                 goto out_unlock;
265                         }
266                         if(i == 0) {
267                                 memcpy(ldt->u.pages[0], &entry0,
268                                        sizeof(entry0));
269                                 memcpy(ldt->u.pages[0]+1, ldt->u.entries+1,
270                                        sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
271                         }
272                         ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
273                 }
274         }
275         if(ldt->entry_count <= ldt_info.entry_number)
276                 ldt->entry_count = ldt_info.entry_number + 1;
277
278         if(ldt->entry_count <= LDT_DIRECT_ENTRIES)
279                 ldt_p = ldt->u.entries + ldt_info.entry_number;
280         else
281                 ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
282                         ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
283
284         if(ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
285            (func == 1 || LDT_empty(&ldt_info))){
286                 ldt_p->a = 0;
287                 ldt_p->b = 0;
288         }
289         else{
290                 if (func == 1)
291                         ldt_info.useable = 0;
292                 ldt_p->a = LDT_entry_a(&ldt_info);
293                 ldt_p->b = LDT_entry_b(&ldt_info);
294         }
295         err = 0;
296
297 out_unlock:
298         up(&ldt->semaphore);
299 out:
300         return err;
301 }
302
303 static long do_modify_ldt_skas(int func, void __user *ptr,
304                                unsigned long bytecount)
305 {
306         int ret = -ENOSYS;
307
308         switch (func) {
309                 case 0:
310                         ret = read_ldt(ptr, bytecount);
311                         break;
312                 case 1:
313                 case 0x11:
314                         ret = write_ldt(ptr, bytecount, func);
315                         break;
316                 case 2:
317                         ret = read_default_ldt(ptr, bytecount);
318                         break;
319         }
320         return ret;
321 }
322
323 static DEFINE_SPINLOCK(host_ldt_lock);
324 static short dummy_list[9] = {0, -1};
325 static short * host_ldt_entries = NULL;
326
327 static void ldt_get_host_info(void)
328 {
329         long ret;
330         struct ldt_entry * ldt;
331         short *tmp;
332         int i, size, k, order;
333
334         spin_lock(&host_ldt_lock);
335
336         if(host_ldt_entries != NULL){
337                 spin_unlock(&host_ldt_lock);
338                 return;
339         }
340         host_ldt_entries = dummy_list+1;
341
342         spin_unlock(&host_ldt_lock);
343
344         for(i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++);
345
346         ldt = (struct ldt_entry *)
347               __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
348         if(ldt == NULL) {
349                 printk("ldt_get_host_info: couldn't allocate buffer for host "
350                        "ldt\n");
351                 return;
352         }
353
354         ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
355         if(ret < 0) {
356                 printk("ldt_get_host_info: couldn't read host ldt\n");
357                 goto out_free;
358         }
359         if(ret == 0) {
360                 /* default_ldt is active, simply write an empty entry 0 */
361                 host_ldt_entries = dummy_list;
362                 goto out_free;
363         }
364
365         for(i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++){
366                 if(ldt[i].a != 0 || ldt[i].b != 0)
367                         size++;
368         }
369
370         if(size < ARRAY_SIZE(dummy_list))
371                 host_ldt_entries = dummy_list;
372         else {
373                 size = (size + 1) * sizeof(dummy_list[0]);
374                 tmp = kmalloc(size, GFP_KERNEL);
375                 if(tmp == NULL) {
376                         printk("ldt_get_host_info: couldn't allocate host ldt "
377                                "list\n");
378                         goto out_free;
379                 }
380                 host_ldt_entries = tmp;
381         }
382
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;
386                 }
387         }
388         host_ldt_entries[k] = -1;
389
390 out_free:
391         free_pages((unsigned long)ldt, order);
392 }
393
394 long init_new_ldt(struct mmu_context_skas * new_mm,
395                   struct mmu_context_skas * from_mm)
396 {
397         struct user_desc desc;
398         short * num_p;
399         int i;
400         long page, err=0;
401         void *addr = NULL;
402         struct proc_mm_op copy;
403
404
405         if(!ptrace_ldt)
406                 init_MUTEX(&new_mm->ldt.semaphore);
407
408         if(!from_mm){
409                 memset(&desc, 0, sizeof(desc));
410                 /*
411                  * We have to initialize a clean ldt.
412                  */
413                 if(proc_mm) {
414                         /*
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.
420                          */
421                         err = write_ldt_entry(&new_mm->id, 1, &desc,
422                                               &addr, 1);
423                 }
424                 else{
425                         /*
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
429                          */
430                         ldt_get_host_info();
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);
435                                 if(err)
436                                         break;
437                         }
438                 }
439                 new_mm->ldt.entry_count = 0;
440
441                 goto out;
442         }
443
444         if(proc_mm){
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()
448                  */
449                 copy = ((struct proc_mm_op) { .op       = MM_COPY_SEGMENTS,
450                                               .u        =
451                                               { .copy_segments =
452                                                         from_mm->id.u.mm_fd } } );
453                 i = os_write_file(new_mm->id.u.mm_fd, &copy, sizeof(copy));
454                 if(i != sizeof(copy))
455                         printk("new_mm : /proc/mm copy_segments failed, "
456                                "err = %d\n", -i);
457         }
458
459         if(!ptrace_ldt) {
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.
464                  */
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));
469                 }
470                 else{
471                         i = from_mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
472                         while(i-->0){
473                                 page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
474                                 if (!page){
475                                         err = -ENOMEM;
476                                         break;
477                                 }
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);
482                         }
483                 }
484                 new_mm->ldt.entry_count = from_mm->ldt.entry_count;
485                 up(&from_mm->ldt.semaphore);
486         }
487
488     out:
489         return err;
490 }
491
492
493 void free_ldt(struct mmu_context_skas * mm)
494 {
495         int i;
496
497         if(!ptrace_ldt && mm->ldt.entry_count > LDT_DIRECT_ENTRIES){
498                 i = mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
499                 while(i-- > 0){
500                         free_page((long )mm->ldt.u.pages[i]);
501                 }
502         }
503         mm->ldt.entry_count = 0;
504 }
505
506 int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
507 {
508         return do_modify_ldt_skas(func, ptr, bytecount);
509 }