patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / mips / kernel / syscall.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1995, 1996, 1997, 2000, 2001 by Ralf Baechle
7  * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
8  * Copyright (C) 2001 MIPS Technologies, Inc.
9  */
10 #include <linux/errno.h>
11 #include <linux/linkage.h>
12 #include <linux/mm.h>
13 #include <linux/smp.h>
14 #include <linux/smp_lock.h>
15 #include <linux/mman.h>
16 #include <linux/ptrace.h>
17 #include <linux/sched.h>
18 #include <linux/string.h>
19 #include <linux/syscalls.h>
20 #include <linux/file.h>
21 #include <linux/slab.h>
22 #include <linux/utsname.h>
23 #include <linux/unistd.h>
24 #include <linux/sem.h>
25 #include <linux/msg.h>
26 #include <linux/shm.h>
27 #include <linux/vs_cvirt.h>
28
29 #include <asm/branch.h>
30 #include <asm/cachectl.h>
31 #include <asm/cacheflush.h>
32 #include <asm/ipc.h>
33 #include <asm/offset.h>
34 #include <asm/signal.h>
35 #include <asm/sim.h>
36 #include <asm/shmparam.h>
37 #include <asm/sysmips.h>
38 #include <asm/uaccess.h>
39
40 asmlinkage int sys_pipe(nabi_no_regargs struct pt_regs regs)
41 {
42         int fd[2];
43         int error, res;
44
45         error = do_pipe(fd);
46         if (error) {
47                 res = error;
48                 goto out;
49         }
50         regs.regs[3] = fd[1];
51         res = fd[0];
52 out:
53         return res;
54 }
55
56 unsigned long shm_align_mask = PAGE_SIZE - 1;   /* Sane caches */
57
58 #define COLOUR_ALIGN(addr,pgoff)                                \
59         ((((addr) + shm_align_mask) & ~shm_align_mask) +        \
60          (((pgoff) << PAGE_SHIFT) & shm_align_mask))
61
62 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
63         unsigned long len, unsigned long pgoff, unsigned long flags)
64 {
65         struct vm_area_struct * vmm;
66         int do_color_align;
67         unsigned long task_size;
68
69 #if CONFIG_MIPS32
70         task_size = TASK_SIZE;
71 #else
72         task_size = (current->thread.mflags & MF_32BIT_ADDR) ? TASK_SIZE32 : TASK_SIZE;
73 #endif
74
75         if (flags & MAP_FIXED) {
76                 /*
77                  * We do not accept a shared mapping if it would violate
78                  * cache aliasing constraints.
79                  */
80                 if ((flags & MAP_SHARED) && (addr & shm_align_mask))
81                         return -EINVAL;
82                 return addr;
83         }
84
85         if (len > task_size)
86                 return -ENOMEM;
87         do_color_align = 0;
88         if (filp || (flags & MAP_SHARED))
89                 do_color_align = 1;
90         if (addr) {
91                 if (do_color_align)
92                         addr = COLOUR_ALIGN(addr, pgoff);
93                 else
94                         addr = PAGE_ALIGN(addr);
95                 vmm = find_vma(current->mm, addr);
96                 if (task_size - len >= addr &&
97                     (!vmm || addr + len <= vmm->vm_start))
98                         return addr;
99         }
100         addr = TASK_UNMAPPED_BASE;
101         if (do_color_align)
102                 addr = COLOUR_ALIGN(addr, pgoff);
103         else
104                 addr = PAGE_ALIGN(addr);
105
106         for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
107                 /* At this point:  (!vmm || addr < vmm->vm_end). */
108                 if (task_size - len < addr)
109                         return -ENOMEM;
110                 if (!vmm || addr + len <= vmm->vm_start)
111                         return addr;
112                 addr = vmm->vm_end;
113                 if (do_color_align)
114                         addr = COLOUR_ALIGN(addr, pgoff);
115         }
116 }
117
118 /* common code for old and new mmaps */
119 static inline long
120 do_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
121         unsigned long flags, unsigned long fd, unsigned long pgoff)
122 {
123         unsigned long error = -EBADF;
124         struct file * file = NULL;
125
126         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
127         if (!(flags & MAP_ANONYMOUS)) {
128                 file = fget(fd);
129                 if (!file)
130                         goto out;
131         }
132
133         down_write(&current->mm->mmap_sem);
134         error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
135         up_write(&current->mm->mmap_sem);
136
137         if (file)
138                 fput(file);
139 out:
140         return error;
141 }
142
143 asmlinkage unsigned long old_mmap(unsigned long addr, size_t len, int prot,
144                                   int flags, int fd, off_t offset)
145 {
146         unsigned long result;
147
148         result = -EINVAL;
149         if (offset & ~PAGE_MASK)
150                 goto out;
151
152         result = do_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
153
154 out:
155         return result;
156 }
157
158 asmlinkage long
159 sys_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
160           unsigned long flags, unsigned long fd, unsigned long pgoff)
161 {
162         return do_mmap2(addr, len, prot, flags, fd, pgoff);
163 }
164
165 save_static_function(sys_fork);
166 static_unused int _sys_fork(nabi_no_regargs struct pt_regs regs)
167 {
168         return do_fork(SIGCHLD, regs.regs[29], &regs, 0, NULL, NULL);
169 }
170
171 save_static_function(sys_clone);
172 static_unused int _sys_clone(nabi_no_regargs struct pt_regs regs)
173 {
174         unsigned long clone_flags;
175         unsigned long newsp;
176         int *parent_tidptr, *child_tidptr;
177
178         clone_flags = regs.regs[4];
179         newsp = regs.regs[5];
180         if (!newsp)
181                 newsp = regs.regs[29];
182         parent_tidptr = (int *) regs.regs[6];
183         child_tidptr = (int *) regs.regs[7];
184         return do_fork(clone_flags & ~CLONE_IDLETASK, newsp, &regs, 0,
185                        parent_tidptr, child_tidptr);
186 }
187
188 /*
189  * sys_execve() executes a new program.
190  */
191 asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs)
192 {
193         int error;
194         char * filename;
195
196         filename = getname((char *) (long)regs.regs[4]);
197         error = PTR_ERR(filename);
198         if (IS_ERR(filename))
199                 goto out;
200         error = do_execve(filename, (char **) (long)regs.regs[5],
201                           (char **) (long)regs.regs[6], &regs);
202         putname(filename);
203
204 out:
205         return error;
206 }
207
208 /*
209  * Compacrapability ...
210  */
211 asmlinkage int sys_uname(struct old_utsname * name)
212 {
213         if (name && !copy_to_user(name, vx_new_utsname(), sizeof (*name)))
214                 return 0;
215         return -EFAULT;
216 }
217
218 /*
219  * Compacrapability ...
220  */
221 asmlinkage int sys_olduname(struct oldold_utsname * name)
222 {
223         int error;
224         struct new_utsname *ptr;
225
226         if (!name)
227                 return -EFAULT;
228         if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
229                 return -EFAULT;
230
231         ptr = vx_new_utsname();
232         error = __copy_to_user(&name->sysname,ptr->sysname,__OLD_UTS_LEN);
233         error -= __put_user(0,name->sysname+__OLD_UTS_LEN);
234         error -= __copy_to_user(&name->nodename,ptr->nodename,__OLD_UTS_LEN);
235         error -= __put_user(0,name->nodename+__OLD_UTS_LEN);
236         error -= __copy_to_user(&name->release,ptr->release,__OLD_UTS_LEN);
237         error -= __put_user(0,name->release+__OLD_UTS_LEN);
238         error -= __copy_to_user(&name->version,ptr->version,__OLD_UTS_LEN);
239         error -= __put_user(0,name->version+__OLD_UTS_LEN);
240         error -= __copy_to_user(&name->machine,ptr->machine,__OLD_UTS_LEN);
241         error = __put_user(0,name->machine+__OLD_UTS_LEN);
242         error = error ? -EFAULT : 0;
243
244         return error;
245 }
246
247 asmlinkage int _sys_sysmips(int cmd, long arg1, int arg2, int arg3)
248 {
249         int     tmp, len;
250         char    *name;
251
252         switch(cmd) {
253         case SETNAME: {
254                 char nodename[__NEW_UTS_LEN + 1];
255
256                 if (!capable(CAP_SYS_ADMIN))
257                         return -EPERM;
258
259                 name = (char *) arg1;
260
261                 len = strncpy_from_user(nodename, name, __NEW_UTS_LEN);
262                 if (len < 0)
263                         return -EFAULT;
264
265                 down_write(&uts_sem);
266                 strncpy(vx_new_uts(nodename), nodename, len);
267                 nodename[__NEW_UTS_LEN] = '\0';
268                 strlcpy(vx_new_uts(nodename), nodename,
269                         sizeof(vx_new_uts(nodename)));
270                 up_write(&uts_sem);
271                 return 0;
272         }
273
274         case MIPS_ATOMIC_SET:
275                 printk(KERN_CRIT "How did I get here?\n");
276                 return -EINVAL;
277
278         case MIPS_FIXADE:
279                 tmp = current->thread.mflags & ~3;
280                 current->thread.mflags = tmp | (arg1 & 3);
281                 return 0;
282
283         case FLUSH_CACHE:
284                 __flush_cache_all();
285                 return 0;
286
287         case MIPS_RDNVRAM:
288                 return -EIO;
289         }
290
291         return -EINVAL;
292 }
293
294 /*
295  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
296  *
297  * This is really horribly ugly.
298  */
299 asmlinkage int sys_ipc (uint call, int first, int second,
300                         unsigned long third, void *ptr, long fifth)
301 {
302         int version, ret;
303
304         version = call >> 16; /* hack for backward compatibility */
305         call &= 0xffff;
306
307         switch (call) {
308         case SEMOP:
309                 return sys_semtimedop (first, (struct sembuf *)ptr, second,
310                                        NULL);
311         case SEMTIMEDOP:
312                 return sys_semtimedop (first, (struct sembuf *)ptr, second,
313                                        (const struct timespec __user *)fifth);
314         case SEMGET:
315                 return sys_semget (first, second, third);
316         case SEMCTL: {
317                 union semun fourth;
318                 if (!ptr)
319                         return -EINVAL;
320                 if (get_user(fourth.__pad, (void **) ptr))
321                         return -EFAULT;
322                 return sys_semctl (first, second, third, fourth);
323         }
324
325         case MSGSND:
326                 return sys_msgsnd (first, (struct msgbuf *) ptr,
327                                    second, third);
328         case MSGRCV:
329                 switch (version) {
330                 case 0: {
331                         struct ipc_kludge tmp;
332                         if (!ptr)
333                                 return -EINVAL;
334
335                         if (copy_from_user(&tmp,
336                                            (struct ipc_kludge *) ptr,
337                                            sizeof (tmp)))
338                                 return -EFAULT;
339                         return sys_msgrcv (first, tmp.msgp, second,
340                                            tmp.msgtyp, third);
341                 }
342                 default:
343                         return sys_msgrcv (first,
344                                            (struct msgbuf *) ptr,
345                                            second, fifth, third);
346                 }
347         case MSGGET:
348                 return sys_msgget ((key_t) first, second);
349         case MSGCTL:
350                 return sys_msgctl (first, second, (struct msqid_ds *) ptr);
351
352         case SHMAT:
353                 switch (version) {
354                 default: {
355                         ulong raddr;
356                         ret = do_shmat (first, (char *) ptr, second, &raddr);
357                         if (ret)
358                                 return ret;
359                         return put_user (raddr, (ulong *) third);
360                 }
361                 case 1: /* iBCS2 emulator entry point */
362                         if (!segment_eq(get_fs(), get_ds()))
363                                 return -EINVAL;
364                         return do_shmat (first, (char *) ptr, second, (ulong *) third);
365                 }
366         case SHMDT:
367                 return sys_shmdt ((char *)ptr);
368         case SHMGET:
369                 return sys_shmget (first, second, third);
370         case SHMCTL:
371                 return sys_shmctl (first, second,
372                                    (struct shmid_ds *) ptr);
373         default:
374                 return -ENOSYS;
375         }
376 }
377
378 /*
379  * Native ABI that is O32 or N64 version
380  */
381 asmlinkage long sys_shmat(int shmid, char __user *shmaddr,
382                           int shmflg, unsigned long *addr)
383 {
384         unsigned long raddr;
385         int err;
386
387         err = do_shmat(shmid, shmaddr, shmflg, &raddr);
388         if (err)
389                 return err;
390
391         return put_user(raddr, addr);
392 }
393
394 /*
395  * No implemented yet ...
396  */
397 asmlinkage int sys_cachectl(char *addr, int nbytes, int op)
398 {
399         return -ENOSYS;
400 }
401
402 /*
403  * If we ever come here the user sp is bad.  Zap the process right away.
404  * Due to the bad stack signaling wouldn't work.
405  */
406 asmlinkage void bad_stack(void)
407 {
408         do_exit(SIGSEGV);
409 }