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