blob: 343015a2f418fd9fb741ef6ae7b8821008e1af19 [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
Ralf Baechle8213bbf2008-07-20 13:16:46 +010043/*
44 * For historic reasons the pipe(2) syscall on MIPS has an unusual calling
45 * convention. It returns results in registers $v0 / $v1 which means there
46 * is no need for it to do verify the validity of a userspace pointer
47 * argument. Historically that used to be expensive in Linux. These days
48 * the performance advantage is negligible.
49 */
50asmlinkage int sysm_pipe(nabi_no_regargs volatile struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 int fd[2];
53 int error, res;
54
Ulrich Dreppered8cae82008-07-23 21:29:30 -070055 error = do_pipe_flags(fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 if (error) {
57 res = error;
58 goto out;
59 }
60 regs.regs[3] = fd[1];
61 res = fd[0];
62out:
63 return res;
64}
65
66unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
67
Ralf Baechle9ff77c42005-03-08 14:39:39 +000068EXPORT_SYMBOL(shm_align_mask);
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define COLOUR_ALIGN(addr,pgoff) \
71 ((((addr) + shm_align_mask) & ~shm_align_mask) + \
72 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
73
74unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
75 unsigned long len, unsigned long pgoff, unsigned long flags)
76{
77 struct vm_area_struct * vmm;
78 int do_color_align;
79 unsigned long task_size;
80
81 task_size = STACK_TOP;
82
David Daney098362e2007-10-27 23:10:20 -070083 if (len > task_size)
84 return -ENOMEM;
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (flags & MAP_FIXED) {
David Daney098362e2007-10-27 23:10:20 -070087 /* Even MAP_FIXED mappings must reside within task_size. */
88 if (task_size - len < addr)
89 return -EINVAL;
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 /*
92 * We do not accept a shared mapping if it would violate
93 * cache aliasing constraints.
94 */
95 if ((flags & MAP_SHARED) && (addr & shm_align_mask))
96 return -EINVAL;
97 return addr;
98 }
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 do_color_align = 0;
101 if (filp || (flags & MAP_SHARED))
102 do_color_align = 1;
103 if (addr) {
104 if (do_color_align)
105 addr = COLOUR_ALIGN(addr, pgoff);
106 else
107 addr = PAGE_ALIGN(addr);
108 vmm = find_vma(current->mm, addr);
109 if (task_size - len >= addr &&
110 (!vmm || addr + len <= vmm->vm_start))
111 return addr;
112 }
113 addr = TASK_UNMAPPED_BASE;
114 if (do_color_align)
115 addr = COLOUR_ALIGN(addr, pgoff);
116 else
117 addr = PAGE_ALIGN(addr);
118
119 for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
120 /* At this point: (!vmm || addr < vmm->vm_end). */
121 if (task_size - len < addr)
122 return -ENOMEM;
123 if (!vmm || addr + len <= vmm->vm_start)
124 return addr;
125 addr = vmm->vm_end;
126 if (do_color_align)
127 addr = COLOUR_ALIGN(addr, pgoff);
128 }
129}
130
131/* common code for old and new mmaps */
132static inline unsigned long
133do_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
134 unsigned long flags, unsigned long fd, unsigned long pgoff)
135{
136 unsigned long error = -EBADF;
137 struct file * file = NULL;
138
139 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
140 if (!(flags & MAP_ANONYMOUS)) {
141 file = fget(fd);
142 if (!file)
143 goto out;
144 }
145
146 down_write(&current->mm->mmap_sem);
147 error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
148 up_write(&current->mm->mmap_sem);
149
150 if (file)
151 fput(file);
152out:
153 return error;
154}
155
156asmlinkage unsigned long
157old_mmap(unsigned long addr, unsigned long len, int prot,
158 int flags, int fd, off_t offset)
159{
160 unsigned long result;
161
162 result = -EINVAL;
163 if (offset & ~PAGE_MASK)
164 goto out;
165
166 result = do_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
167
168out:
169 return result;
170}
171
172asmlinkage unsigned long
173sys_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
174 unsigned long flags, unsigned long fd, unsigned long pgoff)
175{
H. Peter Anvin947df172006-02-24 21:20:29 -0800176 if (pgoff & (~PAGE_MASK >> 12))
177 return -EINVAL;
178
179 return do_mmap2(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182save_static_function(sys_fork);
David Rientjesf5dbeaf2007-07-22 01:01:39 -0700183static int __used noinline
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184_sys_fork(nabi_no_regargs struct pt_regs regs)
185{
186 return do_fork(SIGCHLD, regs.regs[29], &regs, 0, NULL, NULL);
187}
188
189save_static_function(sys_clone);
David Rientjesf5dbeaf2007-07-22 01:01:39 -0700190static int __used noinline
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191_sys_clone(nabi_no_regargs struct pt_regs regs)
192{
193 unsigned long clone_flags;
194 unsigned long newsp;
Ralf Baechle3c370262005-04-13 17:43:59 +0000195 int __user *parent_tidptr, *child_tidptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 clone_flags = regs.regs[4];
198 newsp = regs.regs[5];
199 if (!newsp)
200 newsp = regs.regs[29];
Ralf Baechle3c370262005-04-13 17:43:59 +0000201 parent_tidptr = (int __user *) regs.regs[6];
202#ifdef CONFIG_32BIT
203 /* We need to fetch the fifth argument off the stack. */
204 child_tidptr = NULL;
205 if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) {
206 int __user *__user *usp = (int __user *__user *) regs.regs[29];
207 if (regs.regs[2] == __NR_syscall) {
208 if (get_user (child_tidptr, &usp[5]))
209 return -EFAULT;
210 }
211 else if (get_user (child_tidptr, &usp[4]))
212 return -EFAULT;
213 }
214#else
215 child_tidptr = (int __user *) regs.regs[8];
216#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return do_fork(clone_flags, newsp, &regs, 0,
218 parent_tidptr, child_tidptr);
219}
220
221/*
222 * sys_execve() executes a new program.
223 */
224asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs)
225{
226 int error;
227 char * filename;
228
Atsushi Nemotobe6e5182006-02-08 23:39:49 +0900229 filename = getname((char __user *) (long)regs.regs[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 error = PTR_ERR(filename);
231 if (IS_ERR(filename))
232 goto out;
Atsushi Nemotobe6e5182006-02-08 23:39:49 +0900233 error = do_execve(filename, (char __user *__user *) (long)regs.regs[5],
234 (char __user *__user *) (long)regs.regs[6], &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 putname(filename);
236
237out:
238 return error;
239}
240
241/*
242 * Compacrapability ...
243 */
Atsushi Nemotobe6e5182006-02-08 23:39:49 +0900244asmlinkage int sys_uname(struct old_utsname __user * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700246 if (name && !copy_to_user(name, utsname(), sizeof (*name)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return 0;
248 return -EFAULT;
249}
250
251/*
252 * Compacrapability ...
253 */
Atsushi Nemotobe6e5182006-02-08 23:39:49 +0900254asmlinkage int sys_olduname(struct oldold_utsname __user * name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 int error;
257
258 if (!name)
259 return -EFAULT;
Ralf Baechle21a151d2007-10-11 23:46:15 +0100260 if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return -EFAULT;
262
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700263 error = __copy_to_user(&name->sysname, &utsname()->sysname,
264 __OLD_UTS_LEN);
265 error -= __put_user(0, name->sysname + __OLD_UTS_LEN);
266 error -= __copy_to_user(&name->nodename, &utsname()->nodename,
267 __OLD_UTS_LEN);
268 error -= __put_user(0, name->nodename + __OLD_UTS_LEN);
269 error -= __copy_to_user(&name->release, &utsname()->release,
270 __OLD_UTS_LEN);
271 error -= __put_user(0, name->release + __OLD_UTS_LEN);
272 error -= __copy_to_user(&name->version, &utsname()->version,
273 __OLD_UTS_LEN);
274 error -= __put_user(0, name->version + __OLD_UTS_LEN);
275 error -= __copy_to_user(&name->machine, &utsname()->machine,
276 __OLD_UTS_LEN);
277 error = __put_user(0, name->machine + __OLD_UTS_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 error = error ? -EFAULT : 0;
279
280 return error;
281}
282
Ralf Baechle06be3752006-09-19 17:18:53 +0100283asmlinkage int sys_set_thread_area(unsigned long addr)
Ralf Baechle3c370262005-04-13 17:43:59 +0000284{
Al Virodc8f6022006-01-12 01:06:07 -0800285 struct thread_info *ti = task_thread_info(current);
Ralf Baechle3c370262005-04-13 17:43:59 +0000286
287 ti->tp_value = addr;
Ralf Baechlea3692022007-07-10 17:33:02 +0100288 if (cpu_has_userlocal)
289 write_c0_userlocal(addr);
Ralf Baechle06be3752006-09-19 17:18:53 +0100290
291 return 0;
Ralf Baechle3c370262005-04-13 17:43:59 +0000292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294asmlinkage int _sys_sysmips(int cmd, long arg1, int arg2, int arg3)
295{
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100296 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 case MIPS_ATOMIC_SET:
298 printk(KERN_CRIT "How did I get here?\n");
299 return -EINVAL;
300
301 case MIPS_FIXADE:
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100302 if (arg1 & ~3)
303 return -EINVAL;
304
305 if (arg1 & 1)
306 set_thread_flag(TIF_FIXADE);
307 else
308 clear_thread_flag(TIF_FIXADE);
309 if (arg1 & 2)
310 set_thread_flag(TIF_LOGADE);
311 else
312 clear_thread_flag(TIF_FIXADE);
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return 0;
315
316 case FLUSH_CACHE:
317 __flush_cache_all();
318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
321 return -EINVAL;
322}
323
324/*
325 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
326 *
327 * This is really horribly ugly.
328 */
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100329asmlinkage int sys_ipc(unsigned int call, int first, int second,
330 unsigned long third, void __user *ptr, long fifth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 int version, ret;
333
334 version = call >> 16; /* hack for backward compatibility */
335 call &= 0xffff;
336
337 switch (call) {
338 case SEMOP:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100339 return sys_semtimedop(first, (struct sembuf __user *)ptr,
340 second, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 case SEMTIMEDOP:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100342 return sys_semtimedop(first, (struct sembuf __user *)ptr,
343 second,
344 (const struct timespec __user *)fifth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 case SEMGET:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100346 return sys_semget(first, second, third);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 case SEMCTL: {
348 union semun fourth;
349 if (!ptr)
350 return -EINVAL;
Atsushi Nemoto219ac732006-02-21 16:05:11 +0900351 if (get_user(fourth.__pad, (void __user *__user *) ptr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return -EFAULT;
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100353 return sys_semctl(first, second, third, fourth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
356 case MSGSND:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100357 return sys_msgsnd(first, (struct msgbuf __user *) ptr,
358 second, third);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 case MSGRCV:
360 switch (version) {
361 case 0: {
362 struct ipc_kludge tmp;
363 if (!ptr)
364 return -EINVAL;
365
366 if (copy_from_user(&tmp,
Atsushi Nemotobe6e5182006-02-08 23:39:49 +0900367 (struct ipc_kludge __user *) ptr,
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100368 sizeof(tmp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return -EFAULT;
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100370 return sys_msgrcv(first, tmp.msgp, second,
371 tmp.msgtyp, third);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373 default:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100374 return sys_msgrcv(first,
375 (struct msgbuf __user *) ptr,
376 second, fifth, third);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378 case MSGGET:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100379 return sys_msgget((key_t) first, second);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 case MSGCTL:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100381 return sys_msgctl(first, second,
382 (struct msqid_ds __user *) ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 case SHMAT:
385 switch (version) {
386 default: {
Ralf Baechlefc103342006-06-28 11:24:12 +0100387 unsigned long raddr;
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100388 ret = do_shmat(first, (char __user *) ptr, second,
389 &raddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (ret)
391 return ret;
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100392 return put_user(raddr, (unsigned long __user *) third);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394 case 1: /* iBCS2 emulator entry point */
395 if (!segment_eq(get_fs(), get_ds()))
396 return -EINVAL;
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100397 return do_shmat(first, (char __user *) ptr, second,
398 (unsigned long *) third);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400 case SHMDT:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100401 return sys_shmdt((char __user *)ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 case SHMGET:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100403 return sys_shmget(first, second, third);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 case SHMCTL:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100405 return sys_shmctl(first, second,
406 (struct shmid_ds __user *) ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 default:
408 return -ENOSYS;
409 }
410}
411
412/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 * No implemented yet ...
414 */
415asmlinkage int sys_cachectl(char *addr, int nbytes, int op)
416{
417 return -ENOSYS;
418}
419
420/*
421 * If we ever come here the user sp is bad. Zap the process right away.
422 * Due to the bad stack signaling wouldn't work.
423 */
424asmlinkage void bad_stack(void)
425{
426 do_exit(SIGSEGV);
427}
Arnd Bergmannfe742902006-10-02 02:18:34 -0700428
429/*
430 * Do a system call from kernel instead of calling sys_execve so we
431 * end up with proper pt_regs.
432 */
433int kernel_execve(const char *filename, char *const argv[], char *const envp[])
434{
435 register unsigned long __a0 asm("$4") = (unsigned long) filename;
436 register unsigned long __a1 asm("$5") = (unsigned long) argv;
437 register unsigned long __a2 asm("$6") = (unsigned long) envp;
438 register unsigned long __a3 asm("$7");
439 unsigned long __v0;
440
441 __asm__ volatile (" \n"
442 " .set noreorder \n"
443 " li $2, %5 # __NR_execve \n"
444 " syscall \n"
445 " move %0, $2 \n"
446 " .set reorder \n"
447 : "=&r" (__v0), "=r" (__a3)
448 : "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_execve)
449 : "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24",
450 "memory");
451
452 if (__a3 == 0)
453 return __v0;
454
455 return -__v0;
456}