K2.6 patches and update.
[tomato.git] / release / src-rt / linux / linux-2.6 / arch / mips / kernel / syscall.c
blob54cbf54b3d23fc75337c86e37bc2b059d9cdf753
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.
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/mman.h>
17 #include <linux/ptrace.h>
18 #include <linux/sched.h>
19 #include <linux/string.h>
20 #include <linux/syscalls.h>
21 #include <linux/file.h>
22 #include <linux/slab.h>
23 #include <linux/utsname.h>
24 #include <linux/unistd.h>
25 #include <linux/sem.h>
26 #include <linux/msg.h>
27 #include <linux/shm.h>
28 #include <linux/compiler.h>
29 #include <linux/module.h>
31 #include <asm/branch.h>
32 #include <asm/cachectl.h>
33 #include <asm/cacheflush.h>
34 #include <asm/ipc.h>
35 #include <asm/asm-offsets.h>
36 #include <asm/signal.h>
37 #include <asm/sim.h>
38 #include <asm/shmparam.h>
39 #include <asm/sysmips.h>
40 #include <asm/uaccess.h>
42 asmlinkage int sys_pipe(nabi_no_regargs volatile struct pt_regs regs)
44 int fd[2];
45 int error, res;
47 error = do_pipe(fd);
48 if (error) {
49 res = error;
50 goto out;
52 regs.regs[3] = fd[1];
53 res = fd[0];
54 out:
55 return res;
58 unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
59 unsigned char shm_align_shift = PAGE_SHIFT; /* Sane caches */
61 EXPORT_SYMBOL(shm_align_mask);
62 EXPORT_SYMBOL(shm_align_shift);
64 #define COLOUR_ALIGN(addr,pgoff) \
65 ((((addr) + shm_align_mask) & ~shm_align_mask) + \
66 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
68 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
69 unsigned long len, unsigned long pgoff, unsigned long flags)
71 struct vm_area_struct * vmm;
72 int do_color_align;
73 unsigned long task_size;
75 #ifdef CONFIG_32BIT
76 task_size = TASK_SIZE;
77 #else /* Must be CONFIG_64BIT*/
78 task_size = test_thread_flag(TIF_32BIT_ADDR) ? TASK_SIZE32 : TASK_SIZE;
79 #endif
81 if (len > task_size)
82 return -ENOMEM;
84 if (flags & MAP_FIXED) {
85 /* Even MAP_FIXED mappings must reside within task_size. */
86 if (task_size - len < addr)
87 return -EINVAL;
90 * We do not accept a shared mapping if it would violate
91 * cache aliasing constraints.
93 if ((flags & MAP_SHARED) &&
94 ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
95 return -EINVAL;
96 return addr;
99 do_color_align = 0;
100 if (filp || (flags & MAP_SHARED))
101 do_color_align = 1;
102 if (addr) {
103 if (do_color_align)
104 addr = COLOUR_ALIGN(addr, pgoff);
105 else
106 addr = PAGE_ALIGN(addr);
107 vmm = find_vma(current->mm, addr);
108 if (task_size - len >= addr &&
109 (!vmm || addr + len <= vmm->vm_start))
110 return addr;
112 addr = TASK_UNMAPPED_BASE;
113 if (do_color_align)
114 addr = COLOUR_ALIGN(addr, pgoff);
115 else
116 addr = PAGE_ALIGN(addr);
118 for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
119 /* At this point: (!vmm || addr < vmm->vm_end). */
120 if (task_size - len < addr)
121 return -ENOMEM;
122 if (!vmm || addr + len <= vmm->vm_start)
123 return addr;
124 addr = vmm->vm_end;
125 if (do_color_align)
126 addr = COLOUR_ALIGN(addr, pgoff);
130 /* common code for old and new mmaps */
131 static inline unsigned long
132 do_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
133 unsigned long flags, unsigned long fd, unsigned long pgoff)
135 unsigned long error = -EBADF;
136 struct file * file = NULL;
138 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
139 if (!(flags & MAP_ANONYMOUS)) {
140 file = fget(fd);
141 if (!file)
142 goto out;
145 down_write(&current->mm->mmap_sem);
146 error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
147 up_write(&current->mm->mmap_sem);
149 if (file)
150 fput(file);
151 out:
152 return error;
155 asmlinkage unsigned long
156 old_mmap(unsigned long addr, unsigned long len, int prot,
157 int flags, int fd, off_t offset)
159 unsigned long result;
161 result = -EINVAL;
162 if (offset & ~PAGE_MASK)
163 goto out;
165 result = do_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
167 out:
168 return result;
171 asmlinkage unsigned long
172 sys_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
173 unsigned long flags, unsigned long fd, unsigned long pgoff)
175 if (pgoff & (~PAGE_MASK >> 12))
176 return -EINVAL;
178 return do_mmap2(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
181 save_static_function(sys_fork);
182 __attribute_used__ noinline static int
183 _sys_fork(nabi_no_regargs struct pt_regs regs)
185 return do_fork(SIGCHLD, regs.regs[29], &regs, 0, NULL, NULL);
188 save_static_function(sys_clone);
189 __attribute_used__ noinline static int
190 _sys_clone(nabi_no_regargs struct pt_regs regs)
192 unsigned long clone_flags;
193 unsigned long newsp;
194 int __user *parent_tidptr, *child_tidptr;
196 clone_flags = regs.regs[4];
197 newsp = regs.regs[5];
198 if (!newsp)
199 newsp = regs.regs[29];
200 parent_tidptr = (int __user *) regs.regs[6];
201 #ifdef CONFIG_32BIT
202 /* We need to fetch the fifth argument off the stack. */
203 child_tidptr = NULL;
204 if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) {
205 int __user *__user *usp = (int __user *__user *) regs.regs[29];
206 if (regs.regs[2] == __NR_syscall) {
207 if (get_user (child_tidptr, &usp[5]))
208 return -EFAULT;
210 else if (get_user (child_tidptr, &usp[4]))
211 return -EFAULT;
213 #else
214 child_tidptr = (int __user *) regs.regs[8];
215 #endif
216 return do_fork(clone_flags, newsp, &regs, 0,
217 parent_tidptr, child_tidptr);
221 * sys_execve() executes a new program.
223 asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs)
225 int error;
226 char * filename;
228 filename = getname((char __user *) (long)regs.regs[4]);
229 error = PTR_ERR(filename);
230 if (IS_ERR(filename))
231 goto out;
232 error = do_execve(filename, (char __user *__user *) (long)regs.regs[5],
233 (char __user *__user *) (long)regs.regs[6], &regs);
234 putname(filename);
236 out:
237 return error;
241 * Compacrapability ...
243 asmlinkage int sys_uname(struct old_utsname __user * name)
245 if (name && !copy_to_user(name, utsname(), sizeof (*name)))
246 return 0;
247 return -EFAULT;
251 * Compacrapability ...
253 asmlinkage int sys_olduname(struct oldold_utsname __user * name)
255 int error;
257 if (!name)
258 return -EFAULT;
259 if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
260 return -EFAULT;
262 error = __copy_to_user(&name->sysname, &utsname()->sysname,
263 __OLD_UTS_LEN);
264 error -= __put_user(0, name->sysname + __OLD_UTS_LEN);
265 error -= __copy_to_user(&name->nodename, &utsname()->nodename,
266 __OLD_UTS_LEN);
267 error -= __put_user(0, name->nodename + __OLD_UTS_LEN);
268 error -= __copy_to_user(&name->release, &utsname()->release,
269 __OLD_UTS_LEN);
270 error -= __put_user(0, name->release + __OLD_UTS_LEN);
271 error -= __copy_to_user(&name->version, &utsname()->version,
272 __OLD_UTS_LEN);
273 error -= __put_user(0, name->version + __OLD_UTS_LEN);
274 error -= __copy_to_user(&name->machine, &utsname()->machine,
275 __OLD_UTS_LEN);
276 error = __put_user(0, name->machine + __OLD_UTS_LEN);
277 error = error ? -EFAULT : 0;
279 return error;
282 asmlinkage int sys_set_thread_area(unsigned long addr)
284 struct thread_info *ti = task_thread_info(current);
286 ti->tp_value = addr;
287 if (cpu_has_userlocal)
288 write_c0_userlocal(addr);
290 return 0;
293 asmlinkage int _sys_sysmips(int cmd, long arg1, int arg2, int arg3)
295 int tmp;
297 switch(cmd) {
298 case MIPS_ATOMIC_SET:
299 printk(KERN_CRIT "How did I get here?\n");
300 return -EINVAL;
302 case MIPS_FIXADE:
303 tmp = current->thread.mflags & ~3;
304 current->thread.mflags = tmp | (arg1 & 3);
305 return 0;
307 case FLUSH_CACHE:
308 __flush_cache_all();
309 return 0;
312 return -EINVAL;
316 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
318 * This is really horribly ugly.
320 asmlinkage int sys_ipc (unsigned int call, int first, int second,
321 unsigned long third, void __user *ptr, long fifth)
323 int version, ret;
325 version = call >> 16; /* hack for backward compatibility */
326 call &= 0xffff;
328 switch (call) {
329 case SEMOP:
330 return sys_semtimedop (first, (struct sembuf __user *)ptr,
331 second, NULL);
332 case SEMTIMEDOP:
333 return sys_semtimedop (first, (struct sembuf __user *)ptr,
334 second,
335 (const struct timespec __user *)fifth);
336 case SEMGET:
337 return sys_semget (first, second, third);
338 case SEMCTL: {
339 union semun fourth;
340 if (!ptr)
341 return -EINVAL;
342 if (get_user(fourth.__pad, (void __user *__user *) ptr))
343 return -EFAULT;
344 return sys_semctl (first, second, third, fourth);
347 case MSGSND:
348 return sys_msgsnd (first, (struct msgbuf __user *) ptr,
349 second, third);
350 case MSGRCV:
351 switch (version) {
352 case 0: {
353 struct ipc_kludge tmp;
354 if (!ptr)
355 return -EINVAL;
357 if (copy_from_user(&tmp,
358 (struct ipc_kludge __user *) ptr,
359 sizeof (tmp)))
360 return -EFAULT;
361 return sys_msgrcv (first, tmp.msgp, second,
362 tmp.msgtyp, third);
364 default:
365 return sys_msgrcv (first,
366 (struct msgbuf __user *) ptr,
367 second, fifth, third);
369 case MSGGET:
370 return sys_msgget ((key_t) first, second);
371 case MSGCTL:
372 return sys_msgctl (first, second,
373 (struct msqid_ds __user *) ptr);
375 case SHMAT:
376 switch (version) {
377 default: {
378 unsigned long raddr;
379 ret = do_shmat (first, (char __user *) ptr, second,
380 &raddr);
381 if (ret)
382 return ret;
383 return put_user (raddr, (unsigned long __user *) third);
385 case 1: /* iBCS2 emulator entry point */
386 if (!segment_eq(get_fs(), get_ds()))
387 return -EINVAL;
388 return do_shmat (first, (char __user *) ptr, second,
389 (unsigned long *) third);
391 case SHMDT:
392 return sys_shmdt ((char __user *)ptr);
393 case SHMGET:
394 return sys_shmget (first, second, third);
395 case SHMCTL:
396 return sys_shmctl (first, second,
397 (struct shmid_ds __user *) ptr);
398 default:
399 return -ENOSYS;
404 * No implemented yet ...
406 asmlinkage int sys_cachectl(char *addr, int nbytes, int op)
408 return -ENOSYS;
412 * If we ever come here the user sp is bad. Zap the process right away.
413 * Due to the bad stack signaling wouldn't work.
415 asmlinkage void bad_stack(void)
417 do_exit(SIGSEGV);
421 * Do a system call from kernel instead of calling sys_execve so we
422 * end up with proper pt_regs.
424 int kernel_execve(const char *filename, char *const argv[], char *const envp[])
426 register unsigned long __a0 asm("$4") = (unsigned long) filename;
427 register unsigned long __a1 asm("$5") = (unsigned long) argv;
428 register unsigned long __a2 asm("$6") = (unsigned long) envp;
429 register unsigned long __a3 asm("$7");
430 unsigned long __v0;
432 __asm__ volatile (" \n"
433 " .set noreorder \n"
434 " li $2, %5 # __NR_execve \n"
435 " syscall \n"
436 " move %0, $2 \n"
437 " .set reorder \n"
438 : "=&r" (__v0), "=r" (__a3)
439 : "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_execve)
440 : "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24",
441 "memory");
443 if (__a3 == 0)
444 return __v0;
446 return -__v0;
450 * Build the string table for the builtin "poor man's strace".
452 #ifdef CONFIG_PRINT_SYSCALLS
453 #define SYS(fun, narg) #fun,
454 static char *sfnames[] = {
455 #include "syscalls.h"
458 #ifdef CONFIG_HWSIM
459 int do_strace = 1;
460 #else
461 int do_strace = 0;
462 #endif
464 asmlinkage void strace(struct pt_regs *regs)
466 int i;
467 unsigned long scn, narg, *pa0;
468 char *name, space[16];
469 extern char sys_call_table[];
471 if (do_strace == 0)
472 return;
474 scn = regs->regs[2];
475 pa0 = &regs->regs[4];
477 if ((scn >= __NR_Linux) && (scn < (__NR_Linux + __NR_Linux_syscalls))) {
478 name = sfnames[scn - __NR_Linux];
479 narg = *(unsigned long *)(((char *)(&sys_call_table[scn])) + 4);
480 } else {
481 sprintf(space, "sc%lu", scn);
482 name = space;
483 narg = 0;
486 printk("%lu[%s:%d]@0x%08lx: %s(", jiffies, current->comm, current->pid, regs->cp0_epc, name);
488 if (narg > 6) narg = 6;
490 for (i = 0; i < narg; i++) {
491 if (i) printk(", ");
492 if (i < 4)
493 printk("0x%08lx", pa0[i]);
494 else
495 printk("0x%08lx", regs->pad0[i]);
498 printk(")\n");
500 #endif