patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / ppc / kernel / syscalls.c
1 /*
2  * arch/ppc/kernel/sys_ppc.c
3  *
4  *  PowerPC version
5  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6  *
7  * Derived from "arch/i386/kernel/sys_i386.c"
8  * Adapted from the i386 version by Gary Thomas
9  * Modified by Cort Dougan (cort@cs.nmt.edu)
10  * and Paul Mackerras (paulus@cs.anu.edu.au).
11  *
12  * This file contains various random system calls that
13  * have a non-standard calling sequence on the Linux/PPC
14  * platform.
15  *
16  *  This program is free software; you can redistribute it and/or
17  *  modify it under the terms of the GNU General Public License
18  *  as published by the Free Software Foundation; either version
19  *  2 of the License, or (at your option) any later version.
20  *
21  */
22
23 #include <linux/errno.h>
24 #include <linux/sched.h>
25 #include <linux/mm.h>
26 #include <linux/smp.h>
27 #include <linux/smp_lock.h>
28 #include <linux/sem.h>
29 #include <linux/msg.h>
30 #include <linux/shm.h>
31 #include <linux/stat.h>
32 #include <linux/syscalls.h>
33 #include <linux/mman.h>
34 #include <linux/sys.h>
35 #include <linux/ipc.h>
36 #include <linux/utsname.h>
37 #include <linux/file.h>
38 #include <linux/unistd.h>
39 #include <linux/vs_cvirt.h>
40
41 #include <asm/uaccess.h>
42 #include <asm/ipc.h>
43 #include <asm/semaphore.h>
44
45 void
46 check_bugs(void)
47 {
48 }
49
50 /*
51  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
52  *
53  * This is really horribly ugly.
54  */
55 int
56 sys_ipc (uint call, int first, int second, int third, void __user *ptr, long fifth)
57 {
58         int version, ret;
59
60         version = call >> 16; /* hack for backward compatibility */
61         call &= 0xffff;
62
63         ret = -ENOSYS;
64         switch (call) {
65         case SEMOP:
66                 ret = sys_semtimedop (first, (struct sembuf __user *)ptr,
67                                       second, NULL);
68                 break;
69         case SEMTIMEDOP:
70                 ret = sys_semtimedop (first, (struct sembuf __user *)ptr,
71                                       second, (const struct timespec *) fifth);
72                 break;
73         case SEMGET:
74                 ret = sys_semget (first, second, third);
75                 break;
76         case SEMCTL: {
77                 union semun fourth;
78
79                 if (!ptr)
80                         break;
81                 if ((ret = verify_area (VERIFY_READ, ptr, sizeof(long)))
82                     || (ret = get_user(fourth.__pad, (void *__user *)ptr)))
83                         break;
84                 ret = sys_semctl (first, second, third, fourth);
85                 break;
86                 }
87         case MSGSND:
88                 ret = sys_msgsnd (first, (struct msgbuf __user *) ptr, second, third);
89                 break;
90         case MSGRCV:
91                 switch (version) {
92                 case 0: {
93                         struct ipc_kludge tmp;
94
95                         if (!ptr)
96                                 break;
97                         if ((ret = verify_area (VERIFY_READ, ptr, sizeof(tmp)))
98                             || (ret = copy_from_user(&tmp,
99                                         (struct ipc_kludge __user *) ptr,
100                                         sizeof (tmp)) ? -EFAULT : 0))
101                                 break;
102                         ret = sys_msgrcv (first, tmp.msgp, second, tmp.msgtyp,
103                                           third);
104                         break;
105                         }
106                 default:
107                         ret = sys_msgrcv (first, (struct msgbuf __user *) ptr,
108                                           second, fifth, third);
109                         break;
110                 }
111                 break;
112         case MSGGET:
113                 ret = sys_msgget ((key_t) first, second);
114                 break;
115         case MSGCTL:
116                 ret = sys_msgctl (first, second, (struct msqid_ds __user *) ptr);
117                 break;
118         case SHMAT: {
119                 ulong raddr;
120
121                 if ((ret = verify_area(VERIFY_WRITE, (ulong __user *) third,
122                                        sizeof(ulong))))
123                         break;
124                 ret = do_shmat (first, (char __user *) ptr, second, &raddr);
125                 if (ret)
126                         break;
127                 ret = put_user (raddr, (ulong __user *) third);
128                 break;
129                 }
130         case SHMDT:
131                 ret = sys_shmdt ((char __user *)ptr);
132                 break;
133         case SHMGET:
134                 ret = sys_shmget (first, second, third);
135                 break;
136         case SHMCTL:
137                 ret = sys_shmctl (first, second, (struct shmid_ds __user *) ptr);
138                 break;
139         }
140
141         return ret;
142 }
143
144 /*
145  * sys_pipe() is the normal C calling standard for creating
146  * a pipe. It's not the way unix traditionally does this, though.
147  */
148 int sys_pipe(int __user *fildes)
149 {
150         int fd[2];
151         int error;
152
153         error = do_pipe(fd);
154         if (!error) {
155                 if (copy_to_user(fildes, fd, 2*sizeof(int)))
156                         error = -EFAULT;
157         }
158         return error;
159 }
160
161 static inline unsigned long
162 do_mmap2(unsigned long addr, size_t len,
163          unsigned long prot, unsigned long flags,
164          unsigned long fd, unsigned long pgoff)
165 {
166         struct file * file = NULL;
167         int ret = -EBADF;
168
169         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
170         if (!(flags & MAP_ANONYMOUS)) {
171                 if (!(file = fget(fd)))
172                         goto out;
173         }
174
175         down_write(&current->mm->mmap_sem);
176         ret = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
177         up_write(&current->mm->mmap_sem);
178         if (file)
179                 fput(file);
180 out:
181         return ret;
182 }
183
184 unsigned long sys_mmap2(unsigned long addr, size_t len,
185                         unsigned long prot, unsigned long flags,
186                         unsigned long fd, unsigned long pgoff)
187 {
188         return do_mmap2(addr, len, prot, flags, fd, pgoff);
189 }
190
191 unsigned long sys_mmap(unsigned long addr, size_t len,
192                        unsigned long prot, unsigned long flags,
193                        unsigned long fd, off_t offset)
194 {
195         int err = -EINVAL;
196
197         if (offset & ~PAGE_MASK)
198                 goto out;
199
200         err = do_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
201 out:
202         return err;
203 }
204
205 /*
206  * Due to some executables calling the wrong select we sometimes
207  * get wrong args.  This determines how the args are being passed
208  * (a single ptr to them all args passed) then calls
209  * sys_select() with the appropriate args. -- Cort
210  */
211 int
212 ppc_select(int n, fd_set *inp, fd_set *outp, fd_set *exp, struct timeval *tvp)
213 {
214         if ( (unsigned long)n >= 4096 )
215         {
216                 unsigned long __user *buffer = (unsigned long __user *)n;
217                 if (verify_area(VERIFY_READ, buffer, 5*sizeof(unsigned long))
218                     || __get_user(n, buffer)
219                     || __get_user(inp, ((fd_set **)(buffer+1)))
220                     || __get_user(outp, ((fd_set **)(buffer+2)))
221                     || __get_user(exp, ((fd_set **)(buffer+3)))
222                     || __get_user(tvp, ((struct timeval **)(buffer+4))))
223                         return -EFAULT;
224         }
225         return sys_select(n, inp, outp, exp, tvp);
226 }
227
228 int sys_uname(struct old_utsname __user * name)
229 {
230         int err = -EFAULT;
231
232         down_read(&uts_sem);
233         if (name && !copy_to_user(name, vx_new_utsname(), sizeof (*name)))
234                 err = 0;
235         up_read(&uts_sem);
236         return err;
237 }
238
239 int sys_olduname(struct oldold_utsname __user * name)
240 {
241         int error;
242         struct new_utsname *ptr;
243
244         if (!name)
245                 return -EFAULT;
246         if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
247                 return -EFAULT;
248
249         down_read(&uts_sem);
250         ptr = vx_new_utsname();
251         error = __copy_to_user(&name->sysname,ptr->sysname,__OLD_UTS_LEN);
252         error -= __put_user(0,name->sysname+__OLD_UTS_LEN);
253         error -= __copy_to_user(&name->nodename,ptr->nodename,__OLD_UTS_LEN);
254         error -= __put_user(0,name->nodename+__OLD_UTS_LEN);
255         error -= __copy_to_user(&name->release,ptr->release,__OLD_UTS_LEN);
256         error -= __put_user(0,name->release+__OLD_UTS_LEN);
257         error -= __copy_to_user(&name->version,ptr->version,__OLD_UTS_LEN);
258         error -= __put_user(0,name->version+__OLD_UTS_LEN);
259         error -= __copy_to_user(&name->machine,ptr->machine,__OLD_UTS_LEN);
260         error = __put_user(0,name->machine+__OLD_UTS_LEN);
261         up_read(&uts_sem);
262
263         error = error ? -EFAULT : 0;
264         return error;
265 }
266
267 /*
268  * We put the arguments in a different order so we only use 6
269  * registers for arguments, rather than 7 as sys_fadvise64_64 needs
270  * (because `offset' goes in r5/r6).
271  */
272 long ppc_fadvise64_64(int fd, int advice, loff_t offset, loff_t len)
273 {
274         return sys_fadvise64_64(fd, offset, len, advice);
275 }