linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / arch / i386 / kernel / sys_i386.c
1 /*
2  * linux/arch/i386/kernel/sys_i386.c
3  *
4  * This file contains various random system calls that
5  * have a non-standard calling sequence on the Linux/i386
6  * platform.
7  */
8
9 #include <linux/errno.h>
10 #include <linux/sched.h>
11 #include <linux/mm.h>
12 #include <linux/smp.h>
13 #include <linux/smp_lock.h>
14 #include <linux/sem.h>
15 #include <linux/msg.h>
16 #include <linux/shm.h>
17 #include <linux/stat.h>
18 #include <linux/syscalls.h>
19 #include <linux/mman.h>
20 #include <linux/file.h>
21 #include <linux/utsname.h>
22 #include <linux/vs_base.h>
23 #include <linux/vs_cvirt.h>
24
25 #include <asm/uaccess.h>
26 #include <asm/ipc.h>
27
28 /*
29  * sys_pipe() is the normal C calling standard for creating
30  * a pipe. It's not the way Unix traditionally does this, though.
31  */
32 asmlinkage int sys_pipe(unsigned long __user * fildes)
33 {
34         int fd[2];
35         int error;
36
37         error = do_pipe(fd);
38         if (!error) {
39                 if (copy_to_user(fildes, fd, 2*sizeof(int)))
40                         error = -EFAULT;
41         }
42         return error;
43 }
44
45 /* common code for old and new mmaps */
46 static inline long do_mmap2(
47         unsigned long addr, unsigned long len,
48         unsigned long prot, unsigned long flags,
49         unsigned long fd, unsigned long pgoff)
50 {
51         int error = -EBADF;
52         struct file * file = NULL;
53
54         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
55         if (!(flags & MAP_ANONYMOUS)) {
56                 file = fget(fd);
57                 if (!file)
58                         goto out;
59         }
60
61         down_write(&current->mm->mmap_sem);
62         error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
63         up_write(&current->mm->mmap_sem);
64
65         if (file)
66                 fput(file);
67 out:
68         return error;
69 }
70
71 asmlinkage long sys_mmap2(unsigned long addr, unsigned long len,
72         unsigned long prot, unsigned long flags,
73         unsigned long fd, unsigned long pgoff)
74 {
75         return do_mmap2(addr, len, prot, flags, fd, pgoff);
76 }
77
78 /*
79  * Perform the select(nd, in, out, ex, tv) and mmap() system
80  * calls. Linux/i386 didn't use to be able to handle more than
81  * 4 system call parameters, so these system calls used a memory
82  * block for parameter passing..
83  */
84
85 struct mmap_arg_struct {
86         unsigned long addr;
87         unsigned long len;
88         unsigned long prot;
89         unsigned long flags;
90         unsigned long fd;
91         unsigned long offset;
92 };
93
94 asmlinkage int old_mmap(struct mmap_arg_struct __user *arg)
95 {
96         struct mmap_arg_struct a;
97         int err = -EFAULT;
98
99         if (copy_from_user(&a, arg, sizeof(a)))
100                 goto out;
101
102         err = -EINVAL;
103         if (a.offset & ~PAGE_MASK)
104                 goto out;
105
106         err = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset >> PAGE_SHIFT);
107 out:
108         return err;
109 }
110
111
112 struct sel_arg_struct {
113         unsigned long n;
114         fd_set __user *inp, *outp, *exp;
115         struct timeval __user *tvp;
116 };
117
118 asmlinkage int old_select(struct sel_arg_struct __user *arg)
119 {
120         struct sel_arg_struct a;
121
122         if (copy_from_user(&a, arg, sizeof(a)))
123                 return -EFAULT;
124         /* sys_select() does the appropriate kernel locking */
125         return sys_select(a.n, a.inp, a.outp, a.exp, a.tvp);
126 }
127
128 /*
129  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
130  *
131  * This is really horribly ugly.
132  */
133 asmlinkage int sys_ipc (uint call, int first, int second,
134                         int third, void __user *ptr, long fifth)
135 {
136         int version, ret;
137
138         version = call >> 16; /* hack for backward compatibility */
139         call &= 0xffff;
140
141         switch (call) {
142         case SEMOP:
143                 return sys_semtimedop (first, (struct sembuf __user *)ptr, second, NULL);
144         case SEMTIMEDOP:
145                 return sys_semtimedop(first, (struct sembuf __user *)ptr, second,
146                                         (const struct timespec __user *)fifth);
147
148         case SEMGET:
149                 return sys_semget (first, second, third);
150         case SEMCTL: {
151                 union semun fourth;
152                 if (!ptr)
153                         return -EINVAL;
154                 if (get_user(fourth.__pad, (void __user * __user *) ptr))
155                         return -EFAULT;
156                 return sys_semctl (first, second, third, fourth);
157         }
158
159         case MSGSND:
160                 return sys_msgsnd (first, (struct msgbuf __user *) ptr, 
161                                    second, third);
162         case MSGRCV:
163                 switch (version) {
164                 case 0: {
165                         struct ipc_kludge tmp;
166                         if (!ptr)
167                                 return -EINVAL;
168                         
169                         if (copy_from_user(&tmp,
170                                            (struct ipc_kludge __user *) ptr, 
171                                            sizeof (tmp)))
172                                 return -EFAULT;
173                         return sys_msgrcv (first, tmp.msgp, second,
174                                            tmp.msgtyp, third);
175                 }
176                 default:
177                         return sys_msgrcv (first,
178                                            (struct msgbuf __user *) ptr,
179                                            second, fifth, third);
180                 }
181         case MSGGET:
182                 return sys_msgget ((key_t) first, second);
183         case MSGCTL:
184                 return sys_msgctl (first, second, (struct msqid_ds __user *) ptr);
185
186         case SHMAT:
187                 switch (version) {
188                 default: {
189                         ulong raddr;
190                         ret = do_shmat (first, (char __user *) ptr, second, &raddr);
191                         if (ret)
192                                 return ret;
193                         return put_user (raddr, (ulong __user *) third);
194                 }
195                 case 1: /* iBCS2 emulator entry point */
196                         if (!segment_eq(get_fs(), get_ds()))
197                                 return -EINVAL;
198                         /* The "(ulong *) third" is valid _only_ because of the kernel segment thing */
199                         return do_shmat (first, (char __user *) ptr, second, (ulong *) third);
200                 }
201         case SHMDT: 
202                 return sys_shmdt ((char __user *)ptr);
203         case SHMGET:
204                 return sys_shmget (first, second, third);
205         case SHMCTL:
206                 return sys_shmctl (first, second,
207                                    (struct shmid_ds __user *) ptr);
208         default:
209                 return -ENOSYS;
210         }
211 }
212
213 /*
214  * Old cruft
215  */
216 asmlinkage int sys_uname(struct old_utsname __user * name)
217 {
218         int err;
219         if (!name)
220                 return -EFAULT;
221         down_read(&uts_sem);
222         err=copy_to_user(name, vx_new_utsname(), sizeof (*name));
223         up_read(&uts_sem);
224         return err?-EFAULT:0;
225 }
226
227 asmlinkage int sys_olduname(struct oldold_utsname __user * name)
228 {
229         int error;
230         struct new_utsname *ptr;
231
232         if (!name)
233                 return -EFAULT;
234         if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
235                 return -EFAULT;
236   
237         down_read(&uts_sem);
238         
239         ptr = vx_new_utsname();
240         error = __copy_to_user(&name->sysname,ptr->sysname,__OLD_UTS_LEN);
241         error |= __put_user(0,name->sysname+__OLD_UTS_LEN);
242         error |= __copy_to_user(&name->nodename,ptr->nodename,__OLD_UTS_LEN);
243         error |= __put_user(0,name->nodename+__OLD_UTS_LEN);
244         error |= __copy_to_user(&name->release,ptr->release,__OLD_UTS_LEN);
245         error |= __put_user(0,name->release+__OLD_UTS_LEN);
246         error |= __copy_to_user(&name->version,ptr->version,__OLD_UTS_LEN);
247         error |= __put_user(0,name->version+__OLD_UTS_LEN);
248         error |= __copy_to_user(&name->machine,ptr->machine,__OLD_UTS_LEN);
249         error |= __put_user(0,name->machine+__OLD_UTS_LEN);
250         
251         up_read(&uts_sem);
252         
253         error = error ? -EFAULT : 0;
254
255         return error;
256 }