blob: b95fe93dd646dad70290be9694bba0d7e37715c6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Randy Dunlapa9415642006-01-11 12:17:48 -080011#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/errno.h>
13#include <linux/linkage.h>
14#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040015#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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>
Ralf Baechle9ff77c42005-03-08 14:39:39 +000030#include <linux/module.h>
Adrian Bunkcba4fbb2007-10-16 23:29:24 -070031#include <linux/ipc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/branch.h>
34#include <asm/cachectl.h>
35#include <asm/cacheflush.h>
Sam Ravnborg048eb582005-09-09 22:32:31 +020036#include <asm/asm-offsets.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/signal.h>
38#include <asm/sim.h>
39#include <asm/shmparam.h>
40#include <asm/sysmips.h>
41#include <asm/uaccess.h>
42
43asmlinkage int sys_pipe(nabi_no_regargs volatile struct pt_regs regs)
44{
45 int fd[2];
46 int error, res;
47
48 error = do_pipe(fd);
49 if (error) {
50 res = error;
51 goto out;
52 }
53 regs.regs[3] = fd[1];
54 res = fd[0];
55out:
56 return res;
57}
58
59unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
60
Ralf Baechle9ff77c42005-03-08 14:39:39 +000061EXPORT_SYMBOL(shm_align_mask);
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define COLOUR_ALIGN(addr,pgoff) \
64 ((((addr) + shm_align_mask) & ~shm_align_mask) + \
65 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
66
67unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
68 unsigned long len, unsigned long pgoff, unsigned long flags)
69{
70 struct vm_area_struct * vmm;
71 int do_color_align;
72 unsigned long task_size;
73
74 task_size = STACK_TOP;
75
76 if (flags & MAP_FIXED) {
77 /*
78 * We do not accept a shared mapping if it would violate
79 * cache aliasing constraints.
80 */
81 if ((flags & MAP_SHARED) && (addr & shm_align_mask))
82 return -EINVAL;
83 return addr;
84 }
85
86 if (len > task_size)
87 return -ENOMEM;
88 do_color_align = 0;
89 if (filp || (flags & MAP_SHARED))
90 do_color_align = 1;
91 if (addr) {
92 if (do_color_align)
93 addr = COLOUR_ALIGN(addr, pgoff);
94 else
95 addr = PAGE_ALIGN(addr);
96 vmm = find_vma(current->mm, addr);
97 if (task_size - len >= addr &&
98 (!vmm || addr + len <= vmm->vm_start))
99 return addr;
100 }
101 addr = TASK_UNMAPPED_BASE;
102 if (do_color_align)
103 addr = COLOUR_ALIGN(addr, pgoff);
104 else
105 addr = PAGE_ALIGN(addr);
106
107 for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
108 /* At this point: (!vmm || addr < vmm->vm_end). */
109 if (task_size - len < addr)
110 return -ENOMEM;
111 if (!vmm || addr + len <= vmm->vm_start)
112 return addr;
113 addr = vmm->vm_end;
114 if (do_color_align)
115 addr = COLOUR_ALIGN(addr, pgoff);
116 }
117}
118
119/* common code for old and new mmaps */
120static inline unsigned long
121do_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
122 unsigned long flags, unsigned long fd, unsigned long pgoff)
123{
124 unsigned long error = -EBADF;
125 struct file * file = NULL;
126
127 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
128 if (!(flags & MAP_ANONYMOUS)) {
129 file = fget(fd);
130 if (!file)
131 goto out;
132 }
133
134 down_write(&current->mm->mmap_sem);
135 error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
136 up_write(&current->mm->mmap_sem);
137
138 if (file)
139 fput(file);
140out:
141 return error;
142}
143
144asmlinkage unsigned long
145old_mmap(unsigned long addr, unsigned long len, int prot,
146 int flags, int fd, off_t offset)
147{
148 unsigned long result;
149
150 result = -EINVAL;
151 if (offset & ~PAGE_MASK)
152 goto out;
153
154 result = do_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
155
156out:
157 return result;
158}
159
160asmlinkage unsigned long
161sys_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
162 unsigned long flags, unsigned long fd, unsigned long pgoff)
163{
H. Peter Anvin947df172006-02-24 21:20:29 -0800164 if (pgoff & (~PAGE_MASK >> 12))
165 return -EINVAL;
166
167 return do_mmap2(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170save_static_function(sys_fork);
David Rientjesf5dbeaf2007-07-22 01:01:39 -0700171static int __used noinline
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172_sys_fork(nabi_no_regargs struct pt_regs regs)
173{
174 return do_fork(SIGCHLD, regs.regs[29], &regs, 0, NULL, NULL);
175}
176
177save_static_function(sys_clone);
David Rientjesf5dbeaf2007-07-22 01:01:39 -0700178static int __used noinline
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179_sys_clone(nabi_no_regargs struct pt_regs regs)
180{
181 unsigned long clone_flags;
182 unsigned long newsp;
Ralf Baechle3c370262005-04-13 17:43:59 +0000183 int __user *parent_tidptr, *child_tidptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 clone_flags = regs.regs[4];
186 newsp = regs.regs[5];
187 if (!newsp)
188 newsp = regs.regs[29];
Ralf Baechle3c370262005-04-13 17:43:59 +0000189 parent_tidptr = (int __user *) regs.regs[6];
190#ifdef CONFIG_32BIT
191 /* We need to fetch the fifth argument off the stack. */
192 child_tidptr = NULL;
193 if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) {
194 int __user *__user *usp = (int __user *__user *) regs.regs[29];
195 if (regs.regs[2] == __NR_syscall) {
196 if (get_user (child_tidptr, &usp[5]))
197 return -EFAULT;
198 }
199 else if (get_user (child_tidptr, &usp[4]))
200 return -EFAULT;
201 }
202#else
203 child_tidptr = (int __user *) regs.regs[8];
204#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return do_fork(clone_flags, newsp, &regs, 0,
206 parent_tidptr, child_tidptr);
207}
208
209/*
210 * sys_execve() executes a new program.
211 */
212asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs)
213{
214 int error;
215 char * filename;
216
Atsushi Nemotobe6e5182006-02-08 23:39:49 +0900217 filename = getname((char __user *) (long)regs.regs[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 error = PTR_ERR(filename);
219 if (IS_ERR(filename))
220 goto out;
Atsushi Nemotobe6e5182006-02-08 23:39:49 +0900221 error = do_execve(filename, (char __user *__user *) (long)regs.regs[5],
222 (char __user *__user *) (long)regs.regs[6], &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 putname(filename);
224
225out:
226 return error;
227}
228
229/*
230 * Compacrapability ...
231 */
Atsushi Nemotobe6e5182006-02-08 23:39:49 +0900232asmlinkage int sys_uname(struct old_utsname __user * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700234 if (name && !copy_to_user(name, utsname(), sizeof (*name)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return 0;
236 return -EFAULT;
237}
238
239/*
240 * Compacrapability ...
241 */
Atsushi Nemotobe6e5182006-02-08 23:39:49 +0900242asmlinkage int sys_olduname(struct oldold_utsname __user * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 int error;
245
246 if (!name)
247 return -EFAULT;
Ralf Baechle21a151d2007-10-11 23:46:15 +0100248 if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return -EFAULT;
250
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700251 error = __copy_to_user(&name->sysname, &utsname()->sysname,
252 __OLD_UTS_LEN);
253 error -= __put_user(0, name->sysname + __OLD_UTS_LEN);
254 error -= __copy_to_user(&name->nodename, &utsname()->nodename,
255 __OLD_UTS_LEN);
256 error -= __put_user(0, name->nodename + __OLD_UTS_LEN);
257 error -= __copy_to_user(&name->release, &utsname()->release,
258 __OLD_UTS_LEN);
259 error -= __put_user(0, name->release + __OLD_UTS_LEN);
260 error -= __copy_to_user(&name->version, &utsname()->version,
261 __OLD_UTS_LEN);
262 error -= __put_user(0, name->version + __OLD_UTS_LEN);
263 error -= __copy_to_user(&name->machine, &utsname()->machine,
264 __OLD_UTS_LEN);
265 error = __put_user(0, name->machine + __OLD_UTS_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 error = error ? -EFAULT : 0;
267
268 return error;
269}
270
Ralf Baechle06be3752006-09-19 17:18:53 +0100271asmlinkage int sys_set_thread_area(unsigned long addr)
Ralf Baechle3c370262005-04-13 17:43:59 +0000272{
Al Virodc8f6022006-01-12 01:06:07 -0800273 struct thread_info *ti = task_thread_info(current);
Ralf Baechle3c370262005-04-13 17:43:59 +0000274
275 ti->tp_value = addr;
Ralf Baechlea3692022007-07-10 17:33:02 +0100276 if (cpu_has_userlocal)
277 write_c0_userlocal(addr);
Ralf Baechle06be3752006-09-19 17:18:53 +0100278
279 return 0;
Ralf Baechle3c370262005-04-13 17:43:59 +0000280}
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282asmlinkage int _sys_sysmips(int cmd, long arg1, int arg2, int arg3)
283{
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100284 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 case MIPS_ATOMIC_SET:
286 printk(KERN_CRIT "How did I get here?\n");
287 return -EINVAL;
288
289 case MIPS_FIXADE:
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100290 if (arg1 & ~3)
291 return -EINVAL;
292
293 if (arg1 & 1)
294 set_thread_flag(TIF_FIXADE);
295 else
296 clear_thread_flag(TIF_FIXADE);
297 if (arg1 & 2)
298 set_thread_flag(TIF_LOGADE);
299 else
300 clear_thread_flag(TIF_FIXADE);
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return 0;
303
304 case FLUSH_CACHE:
305 __flush_cache_all();
306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
309 return -EINVAL;
310}
311
312/*
313 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
314 *
315 * This is really horribly ugly.
316 */
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100317asmlinkage int sys_ipc(unsigned int call, int first, int second,
318 unsigned long third, void __user *ptr, long fifth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 int version, ret;
321
322 version = call >> 16; /* hack for backward compatibility */
323 call &= 0xffff;
324
325 switch (call) {
326 case SEMOP:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100327 return sys_semtimedop(first, (struct sembuf __user *)ptr,
328 second, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 case SEMTIMEDOP:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100330 return sys_semtimedop(first, (struct sembuf __user *)ptr,
331 second,
332 (const struct timespec __user *)fifth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 case SEMGET:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100334 return sys_semget(first, second, third);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 case SEMCTL: {
336 union semun fourth;
337 if (!ptr)
338 return -EINVAL;
Atsushi Nemoto219ac732006-02-21 16:05:11 +0900339 if (get_user(fourth.__pad, (void __user *__user *) ptr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return -EFAULT;
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100341 return sys_semctl(first, second, third, fourth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343
344 case MSGSND:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100345 return sys_msgsnd(first, (struct msgbuf __user *) ptr,
346 second, third);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 case MSGRCV:
348 switch (version) {
349 case 0: {
350 struct ipc_kludge tmp;
351 if (!ptr)
352 return -EINVAL;
353
354 if (copy_from_user(&tmp,
Atsushi Nemotobe6e5182006-02-08 23:39:49 +0900355 (struct ipc_kludge __user *) ptr,
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100356 sizeof(tmp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return -EFAULT;
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100358 return sys_msgrcv(first, tmp.msgp, second,
359 tmp.msgtyp, third);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361 default:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100362 return sys_msgrcv(first,
363 (struct msgbuf __user *) ptr,
364 second, fifth, third);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366 case MSGGET:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100367 return sys_msgget((key_t) first, second);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 case MSGCTL:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100369 return sys_msgctl(first, second,
370 (struct msqid_ds __user *) ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 case SHMAT:
373 switch (version) {
374 default: {
Ralf Baechlefc103342006-06-28 11:24:12 +0100375 unsigned long raddr;
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100376 ret = do_shmat(first, (char __user *) ptr, second,
377 &raddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (ret)
379 return ret;
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100380 return put_user(raddr, (unsigned long __user *) third);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382 case 1: /* iBCS2 emulator entry point */
383 if (!segment_eq(get_fs(), get_ds()))
384 return -EINVAL;
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100385 return do_shmat(first, (char __user *) ptr, second,
386 (unsigned long *) third);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 case SHMDT:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100389 return sys_shmdt((char __user *)ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 case SHMGET:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100391 return sys_shmget(first, second, third);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 case SHMCTL:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100393 return sys_shmctl(first, second,
394 (struct shmid_ds __user *) ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 default:
396 return -ENOSYS;
397 }
398}
399
400/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 * No implemented yet ...
402 */
403asmlinkage int sys_cachectl(char *addr, int nbytes, int op)
404{
405 return -ENOSYS;
406}
407
408/*
409 * If we ever come here the user sp is bad. Zap the process right away.
410 * Due to the bad stack signaling wouldn't work.
411 */
412asmlinkage void bad_stack(void)
413{
414 do_exit(SIGSEGV);
415}
Arnd Bergmannfe742902006-10-02 02:18:34 -0700416
417/*
418 * Do a system call from kernel instead of calling sys_execve so we
419 * end up with proper pt_regs.
420 */
421int kernel_execve(const char *filename, char *const argv[], char *const envp[])
422{
423 register unsigned long __a0 asm("$4") = (unsigned long) filename;
424 register unsigned long __a1 asm("$5") = (unsigned long) argv;
425 register unsigned long __a2 asm("$6") = (unsigned long) envp;
426 register unsigned long __a3 asm("$7");
427 unsigned long __v0;
428
429 __asm__ volatile (" \n"
430 " .set noreorder \n"
431 " li $2, %5 # __NR_execve \n"
432 " syscall \n"
433 " move %0, $2 \n"
434 " .set reorder \n"
435 : "=&r" (__v0), "=r" (__a3)
436 : "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_execve)
437 : "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24",
438 "memory");
439
440 if (__a3 == 0)
441 return __v0;
442
443 return -__v0;
444}