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