Merge 'acpi-2.6.12' branch into to-akpm
[sfrench/cifs-2.6.git] / 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/config.h"
7 #include "linux/sched.h"
8 #include "linux/slab.h"
9 #include "linux/types.h"
10 #include "asm/uaccess.h"
11 #include "asm/ptrace.h"
12 #include "asm/smp.h"
13 #include "asm/ldt.h"
14 #include "choose-mode.h"
15 #include "kern.h"
16 #include "mode_kern.h"
17
18 #ifdef CONFIG_MODE_TT
19
20 extern int modify_ldt(int func, void *ptr, unsigned long bytecount);
21
22 static int do_modify_ldt_tt(int func, void *ptr, unsigned long bytecount)
23 {
24         return modify_ldt(func, ptr, bytecount);
25 }
26
27 #endif
28
29 #ifdef CONFIG_MODE_SKAS
30
31 #include "skas.h"
32 #include "skas_ptrace.h"
33
34 static int do_modify_ldt_skas(int func, void *ptr, unsigned long bytecount)
35 {
36         struct ptrace_ldt ldt;
37         u32 cpu;
38         int res;
39
40         ldt = ((struct ptrace_ldt) { .func      = func,
41                                      .ptr       = ptr,
42                                      .bytecount = bytecount });
43
44         cpu = get_cpu();
45         res = ptrace(PTRACE_LDT, userspace_pid[cpu], 0, (unsigned long) &ldt);
46         put_cpu();
47
48         return res;
49 }
50 #endif
51
52 int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
53 {
54         struct user_desc info;
55         int res = 0;
56         void *buf = NULL;
57         void *p = NULL; /* What we pass to host. */
58
59         switch(func){
60         case 1:
61         case 0x11: /* write_ldt */
62                 /* Do this check now to avoid overflows. */
63                 if (bytecount != sizeof(struct user_desc)) {
64                         res = -EINVAL;
65                         goto out;
66                 }
67
68                 if(copy_from_user(&info, ptr, sizeof(info))) {
69                         res = -EFAULT;
70                         goto out;
71                 }
72
73                 p = &info;
74                 break;
75         case 0:
76         case 2: /* read_ldt */
77
78                 /* The use of info avoids kmalloc on the write case, not on the
79                  * read one. */
80                 buf = kmalloc(bytecount, GFP_KERNEL);
81                 if (!buf) {
82                         res = -ENOMEM;
83                         goto out;
84                 }
85                 p = buf;
86         default:
87                 res = -ENOSYS;
88                 goto out;
89         }
90
91         res = CHOOSE_MODE_PROC(do_modify_ldt_tt, do_modify_ldt_skas, func,
92                                 p, bytecount);
93         if(res < 0)
94                 goto out;
95
96         switch(func){
97         case 0:
98         case 2:
99                 /* Modify_ldt was for reading and returned the number of read
100                  * bytes.*/
101                 if(copy_to_user(ptr, p, res))
102                         res = -EFAULT;
103                 break;
104         }
105
106 out:
107         kfree(buf);
108         return res;
109 }