[CPUFREQ] Fix up compile of cpufreq_stats
[sfrench/cifs-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
40 #include <asm/uaccess.h>
41 #include <asm/ipc.h>
42 #include <asm/semaphore.h>
43
44
45 /*
46  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
47  *
48  * This is really horribly ugly.
49  */
50 int
51 sys_ipc (uint call, int first, int second, int third, void __user *ptr, long fifth)
52 {
53         int version, ret;
54
55         version = call >> 16; /* hack for backward compatibility */
56         call &= 0xffff;
57
58         ret = -ENOSYS;
59         switch (call) {
60         case SEMOP:
61                 ret = sys_semtimedop (first, (struct sembuf __user *)ptr,
62                                       second, NULL);
63                 break;
64         case SEMTIMEDOP:
65                 ret = sys_semtimedop (first, (struct sembuf __user *)ptr,
66                                       second, (const struct timespec __user *) fifth);
67                 break;
68         case SEMGET:
69                 ret = sys_semget (first, second, third);
70                 break;
71         case SEMCTL: {
72                 union semun fourth;
73
74                 if (!ptr)
75                         break;
76                 if ((ret = access_ok(VERIFY_READ, ptr, sizeof(long)) ? 0 : -EFAULT)
77                     || (ret = get_user(fourth.__pad, (void __user *__user *)ptr)))
78                         break;
79                 ret = sys_semctl (first, second, third, fourth);
80                 break;
81                 }
82         case MSGSND:
83                 ret = sys_msgsnd (first, (struct msgbuf __user *) ptr, second, third);
84                 break;
85         case MSGRCV:
86                 switch (version) {
87                 case 0: {
88                         struct ipc_kludge tmp;
89
90                         if (!ptr)
91                                 break;
92                         if ((ret = access_ok(VERIFY_READ, ptr, sizeof(tmp)) ? 0 : -EFAULT)
93                             || (ret = copy_from_user(&tmp,
94                                         (struct ipc_kludge __user *) ptr,
95                                         sizeof (tmp)) ? -EFAULT : 0))
96                                 break;
97                         ret = sys_msgrcv (first, tmp.msgp, second, tmp.msgtyp,
98                                           third);
99                         break;
100                         }
101                 default:
102                         ret = sys_msgrcv (first, (struct msgbuf __user *) ptr,
103                                           second, fifth, third);
104                         break;
105                 }
106                 break;
107         case MSGGET:
108                 ret = sys_msgget ((key_t) first, second);
109                 break;
110         case MSGCTL:
111                 ret = sys_msgctl (first, second, (struct msqid_ds __user *) ptr);
112                 break;
113         case SHMAT: {
114                 ulong raddr;
115
116                 if ((ret = access_ok(VERIFY_WRITE, (ulong __user *) third,
117                                        sizeof(ulong)) ? 0 : -EFAULT))
118                         break;
119                 ret = do_shmat (first, (char __user *) ptr, second, &raddr);
120                 if (ret)
121                         break;
122                 ret = put_user (raddr, (ulong __user *) third);
123                 break;
124                 }
125         case SHMDT:
126                 ret = sys_shmdt ((char __user *)ptr);
127                 break;
128         case SHMGET:
129                 ret = sys_shmget (first, second, third);
130                 break;
131         case SHMCTL:
132                 ret = sys_shmctl (first, second, (struct shmid_ds __user *) ptr);
133                 break;
134         }
135
136         return ret;
137 }
138
139 /*
140  * sys_pipe() is the normal C calling standard for creating
141  * a pipe. It's not the way unix traditionally does this, though.
142  */
143 int sys_pipe(int __user *fildes)
144 {
145         int fd[2];
146         int error;
147
148         error = do_pipe(fd);
149         if (!error) {
150                 if (copy_to_user(fildes, fd, 2*sizeof(int)))
151                         error = -EFAULT;
152         }
153         return error;
154 }
155
156 static inline unsigned long
157 do_mmap2(unsigned long addr, size_t len,
158          unsigned long prot, unsigned long flags,
159          unsigned long fd, unsigned long pgoff)
160 {
161         struct file * file = NULL;
162         int ret = -EBADF;
163
164         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
165         if (!(flags & MAP_ANONYMOUS)) {
166                 if (!(file = fget(fd)))
167                         goto out;
168         }
169
170         down_write(&current->mm->mmap_sem);
171         ret = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
172         up_write(&current->mm->mmap_sem);
173         if (file)
174                 fput(file);
175 out:
176         return ret;
177 }
178
179 unsigned long sys_mmap2(unsigned long addr, size_t len,
180                         unsigned long prot, unsigned long flags,
181                         unsigned long fd, unsigned long pgoff)
182 {
183         return do_mmap2(addr, len, prot, flags, fd, pgoff);
184 }
185
186 unsigned long sys_mmap(unsigned long addr, size_t len,
187                        unsigned long prot, unsigned long flags,
188                        unsigned long fd, off_t offset)
189 {
190         int err = -EINVAL;
191
192         if (offset & ~PAGE_MASK)
193                 goto out;
194
195         err = do_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
196 out:
197         return err;
198 }
199
200 /*
201  * Due to some executables calling the wrong select we sometimes
202  * get wrong args.  This determines how the args are being passed
203  * (a single ptr to them all args passed) then calls
204  * sys_select() with the appropriate args. -- Cort
205  */
206 int
207 ppc_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, struct timeval __user *tvp)
208 {
209         if ( (unsigned long)n >= 4096 )
210         {
211                 unsigned long __user *buffer = (unsigned long __user *)n;
212                 if (!access_ok(VERIFY_READ, buffer, 5*sizeof(unsigned long))
213                     || __get_user(n, buffer)
214                     || __get_user(inp, ((fd_set __user * __user *)(buffer+1)))
215                     || __get_user(outp, ((fd_set  __user * __user *)(buffer+2)))
216                     || __get_user(exp, ((fd_set  __user * __user *)(buffer+3)))
217                     || __get_user(tvp, ((struct timeval  __user * __user *)(buffer+4))))
218                         return -EFAULT;
219         }
220         return sys_select(n, inp, outp, exp, tvp);
221 }
222
223 int sys_uname(struct old_utsname __user * name)
224 {
225         int err = -EFAULT;
226
227         down_read(&uts_sem);
228         if (name && !copy_to_user(name, &system_utsname, sizeof (*name)))
229                 err = 0;
230         up_read(&uts_sem);
231         return err;
232 }
233
234 int sys_olduname(struct oldold_utsname __user * name)
235 {
236         int error;
237
238         if (!name)
239                 return -EFAULT;
240         if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
241                 return -EFAULT;
242
243         down_read(&uts_sem);
244         error = __copy_to_user(&name->sysname,&system_utsname.sysname,__OLD_UTS_LEN);
245         error -= __put_user(0,name->sysname+__OLD_UTS_LEN);
246         error -= __copy_to_user(&name->nodename,&system_utsname.nodename,__OLD_UTS_LEN);
247         error -= __put_user(0,name->nodename+__OLD_UTS_LEN);
248         error -= __copy_to_user(&name->release,&system_utsname.release,__OLD_UTS_LEN);
249         error -= __put_user(0,name->release+__OLD_UTS_LEN);
250         error -= __copy_to_user(&name->version,&system_utsname.version,__OLD_UTS_LEN);
251         error -= __put_user(0,name->version+__OLD_UTS_LEN);
252         error -= __copy_to_user(&name->machine,&system_utsname.machine,__OLD_UTS_LEN);
253         error = __put_user(0,name->machine+__OLD_UTS_LEN);
254         up_read(&uts_sem);
255
256         error = error ? -EFAULT : 0;
257         return error;
258 }
259
260 /*
261  * We put the arguments in a different order so we only use 6
262  * registers for arguments, rather than 7 as sys_fadvise64_64 needs
263  * (because `offset' goes in r5/r6).
264  */
265 long ppc_fadvise64_64(int fd, int advice, loff_t offset, loff_t len)
266 {
267         return sys_fadvise64_64(fd, offset, len, advice);
268 }