[MIPS] ioc3.h: Uses u8, so include <linux/types.h>.
[linux-2.6-block.git] / arch / mips / kernel / syscall.c
CommitLineData
1da177e4
LT
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 */
3c37026d 10#include <linux/config.h>
1da177e4 11#include <linux/a.out.h>
a9415644 12#include <linux/capability.h>
1da177e4
LT
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>
9ff77c46 31#include <linux/module.h>
1da177e4
LT
32
33#include <asm/branch.h>
34#include <asm/cachectl.h>
35#include <asm/cacheflush.h>
36#include <asm/ipc.h>
048eb582 37#include <asm/asm-offsets.h>
1da177e4
LT
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
44asmlinkage 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];
56out:
57 return res;
58}
59
60unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
61
9ff77c46
RB
62EXPORT_SYMBOL(shm_align_mask);
63
1da177e4
LT
64#define COLOUR_ALIGN(addr,pgoff) \
65 ((((addr) + shm_align_mask) & ~shm_align_mask) + \
66 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
67
68unsigned 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 */
121static inline unsigned long
122do_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);
141out:
142 return error;
143}
144
145asmlinkage unsigned long
146old_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
157out:
158 return result;
159}
160
161asmlinkage unsigned long
162sys_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
163 unsigned long flags, unsigned long fd, unsigned long pgoff)
164{
947df17c
PA
165 if (pgoff & (~PAGE_MASK >> 12))
166 return -EINVAL;
167
168 return do_mmap2(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
1da177e4
LT
169}
170
171save_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
178save_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;
3c37026d 184 int __user *parent_tidptr, *child_tidptr;
1da177e4
LT
185
186 clone_flags = regs.regs[4];
187 newsp = regs.regs[5];
188 if (!newsp)
189 newsp = regs.regs[29];
3c37026d
RB
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
1da177e4
LT
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 */
213asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs)
214{
215 int error;
216 char * filename;
217
be6e518b 218 filename = getname((char __user *) (long)regs.regs[4]);
1da177e4
LT
219 error = PTR_ERR(filename);
220 if (IS_ERR(filename))
221 goto out;
be6e518b
AN
222 error = do_execve(filename, (char __user *__user *) (long)regs.regs[5],
223 (char __user *__user *) (long)regs.regs[6], &regs);
1da177e4
LT
224 putname(filename);
225
226out:
227 return error;
228}
229
230/*
231 * Compacrapability ...
232 */
be6e518b 233asmlinkage int sys_uname(struct old_utsname __user * name)
1da177e4
LT
234{
235 if (name && !copy_to_user(name, &system_utsname, sizeof (*name)))
236 return 0;
237 return -EFAULT;
238}
239
240/*
241 * Compacrapability ...
242 */
be6e518b 243asmlinkage int sys_olduname(struct oldold_utsname __user * name)
1da177e4
LT
244{
245 int error;
246
247 if (!name)
248 return -EFAULT;
249 if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
250 return -EFAULT;
251
252 error = __copy_to_user(&name->sysname,&system_utsname.sysname,__OLD_UTS_LEN);
253 error -= __put_user(0,name->sysname+__OLD_UTS_LEN);
254 error -= __copy_to_user(&name->nodename,&system_utsname.nodename,__OLD_UTS_LEN);
255 error -= __put_user(0,name->nodename+__OLD_UTS_LEN);
256 error -= __copy_to_user(&name->release,&system_utsname.release,__OLD_UTS_LEN);
257 error -= __put_user(0,name->release+__OLD_UTS_LEN);
258 error -= __copy_to_user(&name->version,&system_utsname.version,__OLD_UTS_LEN);
259 error -= __put_user(0,name->version+__OLD_UTS_LEN);
260 error -= __copy_to_user(&name->machine,&system_utsname.machine,__OLD_UTS_LEN);
261 error = __put_user(0,name->machine+__OLD_UTS_LEN);
262 error = error ? -EFAULT : 0;
263
264 return error;
265}
266
3c37026d
RB
267void sys_set_thread_area(unsigned long addr)
268{
dc8f6029 269 struct thread_info *ti = task_thread_info(current);
3c37026d
RB
270
271 ti->tp_value = addr;
272
273 /* If some future MIPS implementation has this register in hardware,
274 * we will need to update it here (and in context switches). */
275}
276
1da177e4
LT
277asmlinkage int _sys_sysmips(int cmd, long arg1, int arg2, int arg3)
278{
ecf52d3c 279 int tmp;
1da177e4
LT
280
281 switch(cmd) {
1da177e4
LT
282 case MIPS_ATOMIC_SET:
283 printk(KERN_CRIT "How did I get here?\n");
284 return -EINVAL;
285
286 case MIPS_FIXADE:
287 tmp = current->thread.mflags & ~3;
288 current->thread.mflags = tmp | (arg1 & 3);
289 return 0;
290
291 case FLUSH_CACHE:
292 __flush_cache_all();
293 return 0;
1da177e4
LT
294 }
295
296 return -EINVAL;
297}
298
299/*
300 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
301 *
302 * This is really horribly ugly.
303 */
304asmlinkage int sys_ipc (uint call, int first, int second,
be6e518b 305 unsigned long third, void __user *ptr, long fifth)
1da177e4
LT
306{
307 int version, ret;
308
309 version = call >> 16; /* hack for backward compatibility */
310 call &= 0xffff;
311
312 switch (call) {
313 case SEMOP:
be6e518b
AN
314 return sys_semtimedop (first, (struct sembuf __user *)ptr,
315 second, NULL);
1da177e4 316 case SEMTIMEDOP:
be6e518b
AN
317 return sys_semtimedop (first, (struct sembuf __user *)ptr,
318 second,
319 (const struct timespec __user *)fifth);
1da177e4
LT
320 case SEMGET:
321 return sys_semget (first, second, third);
322 case SEMCTL: {
323 union semun fourth;
324 if (!ptr)
325 return -EINVAL;
219ac73a 326 if (get_user(fourth.__pad, (void __user *__user *) ptr))
1da177e4
LT
327 return -EFAULT;
328 return sys_semctl (first, second, third, fourth);
329 }
330
331 case MSGSND:
be6e518b 332 return sys_msgsnd (first, (struct msgbuf __user *) ptr,
1da177e4
LT
333 second, third);
334 case MSGRCV:
335 switch (version) {
336 case 0: {
337 struct ipc_kludge tmp;
338 if (!ptr)
339 return -EINVAL;
340
341 if (copy_from_user(&tmp,
be6e518b 342 (struct ipc_kludge __user *) ptr,
1da177e4
LT
343 sizeof (tmp)))
344 return -EFAULT;
345 return sys_msgrcv (first, tmp.msgp, second,
346 tmp.msgtyp, third);
347 }
348 default:
349 return sys_msgrcv (first,
be6e518b 350 (struct msgbuf __user *) ptr,
1da177e4
LT
351 second, fifth, third);
352 }
353 case MSGGET:
354 return sys_msgget ((key_t) first, second);
355 case MSGCTL:
be6e518b
AN
356 return sys_msgctl (first, second,
357 (struct msqid_ds __user *) ptr);
1da177e4
LT
358
359 case SHMAT:
360 switch (version) {
361 default: {
362 ulong raddr;
be6e518b
AN
363 ret = do_shmat (first, (char __user *) ptr, second,
364 &raddr);
1da177e4
LT
365 if (ret)
366 return ret;
be6e518b 367 return put_user (raddr, (ulong __user *) third);
1da177e4
LT
368 }
369 case 1: /* iBCS2 emulator entry point */
370 if (!segment_eq(get_fs(), get_ds()))
371 return -EINVAL;
be6e518b
AN
372 return do_shmat (first, (char __user *) ptr, second,
373 (ulong *) third);
1da177e4
LT
374 }
375 case SHMDT:
be6e518b 376 return sys_shmdt ((char __user *)ptr);
1da177e4
LT
377 case SHMGET:
378 return sys_shmget (first, second, third);
379 case SHMCTL:
380 return sys_shmctl (first, second,
be6e518b 381 (struct shmid_ds __user *) ptr);
1da177e4
LT
382 default:
383 return -ENOSYS;
384 }
385}
386
1da177e4
LT
387/*
388 * No implemented yet ...
389 */
390asmlinkage int sys_cachectl(char *addr, int nbytes, int op)
391{
392 return -ENOSYS;
393}
394
395/*
396 * If we ever come here the user sp is bad. Zap the process right away.
397 * Due to the bad stack signaling wouldn't work.
398 */
399asmlinkage void bad_stack(void)
400{
401 do_exit(SIGSEGV);
402}