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