blob: 9587abc67f35cfec1851c609f40e08e460436356 [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 */
Randy Dunlapa9415642006-01-11 12:17:48 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/errno.h>
12#include <linux/linkage.h>
13#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040014#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/utsname.h>
23#include <linux/unistd.h>
24#include <linux/sem.h>
25#include <linux/msg.h>
26#include <linux/shm.h>
27#include <linux/compiler.h>
Ralf Baechle9ff77c42005-03-08 14:39:39 +000028#include <linux/module.h>
Adrian Bunkcba4fbb2007-10-16 23:29:24 -070029#include <linux/ipc.h>
Ralf Baechlef1e39a42009-09-17 02:25:05 +020030#include <linux/uaccess.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Ralf Baechlef1e39a42009-09-17 02:25:05 +020033#include <asm/asm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/branch.h>
35#include <asm/cachectl.h>
36#include <asm/cacheflush.h>
Sam Ravnborg048eb582005-09-09 22:32:31 +020037#include <asm/asm-offsets.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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
Ralf Baechle8213bbf2008-07-20 13:16:46 +010044/*
45 * For historic reasons the pipe(2) syscall on MIPS has an unusual calling
46 * convention. It returns results in registers $v0 / $v1 which means there
47 * is no need for it to do verify the validity of a userspace pointer
48 * argument. Historically that used to be expensive in Linux. These days
49 * the performance advantage is negligible.
50 */
51asmlinkage int sysm_pipe(nabi_no_regargs volatile struct pt_regs regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 int fd[2];
54 int error, res;
55
Ulrich Dreppered8cae82008-07-23 21:29:30 -070056 error = do_pipe_flags(fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 if (error) {
58 res = error;
59 goto out;
60 }
61 regs.regs[3] = fd[1];
62 res = fd[0];
63out:
64 return res;
65}
66
67unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
68
Ralf Baechle9ff77c42005-03-08 14:39:39 +000069EXPORT_SYMBOL(shm_align_mask);
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define COLOUR_ALIGN(addr,pgoff) \
72 ((((addr) + shm_align_mask) & ~shm_align_mask) + \
73 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
74
75unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
76 unsigned long len, unsigned long pgoff, unsigned long flags)
77{
78 struct vm_area_struct * vmm;
79 int do_color_align;
80 unsigned long task_size;
81
82 task_size = STACK_TOP;
83
David Daney098362e2007-10-27 23:10:20 -070084 if (len > task_size)
85 return -ENOMEM;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (flags & MAP_FIXED) {
David Daney098362e2007-10-27 23:10:20 -070088 /* Even MAP_FIXED mappings must reside within task_size. */
89 if (task_size - len < addr)
90 return -EINVAL;
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 /*
93 * We do not accept a shared mapping if it would violate
94 * cache aliasing constraints.
95 */
Al Viroe77414e2009-12-05 15:10:44 -050096 if ((flags & MAP_SHARED) &&
97 ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return -EINVAL;
99 return addr;
100 }
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 do_color_align = 0;
103 if (filp || (flags & MAP_SHARED))
104 do_color_align = 1;
105 if (addr) {
106 if (do_color_align)
107 addr = COLOUR_ALIGN(addr, pgoff);
108 else
109 addr = PAGE_ALIGN(addr);
110 vmm = find_vma(current->mm, addr);
111 if (task_size - len >= addr &&
112 (!vmm || addr + len <= vmm->vm_start))
113 return addr;
114 }
115 addr = TASK_UNMAPPED_BASE;
116 if (do_color_align)
117 addr = COLOUR_ALIGN(addr, pgoff);
118 else
119 addr = PAGE_ALIGN(addr);
120
121 for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
122 /* At this point: (!vmm || addr < vmm->vm_end). */
123 if (task_size - len < addr)
124 return -ENOMEM;
125 if (!vmm || addr + len <= vmm->vm_start)
126 return addr;
127 addr = vmm->vm_end;
128 if (do_color_align)
129 addr = COLOUR_ALIGN(addr, pgoff);
130 }
131}
132
Ralf Baechledbda6ac2009-02-08 16:00:26 +0000133SYSCALL_DEFINE6(mips_mmap, unsigned long, addr, unsigned long, len,
134 unsigned long, prot, unsigned long, flags, unsigned long,
135 fd, off_t, offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 unsigned long result;
138
139 result = -EINVAL;
140 if (offset & ~PAGE_MASK)
141 goto out;
142
Al Virof8b72562009-11-30 17:37:04 -0500143 result = sys_mmap_pgoff(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145out:
146 return result;
147}
148
Ralf Baechledbda6ac2009-02-08 16:00:26 +0000149SYSCALL_DEFINE6(mips_mmap2, unsigned long, addr, unsigned long, len,
150 unsigned long, prot, unsigned long, flags, unsigned long, fd,
151 unsigned long, pgoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
H. Peter Anvin947df172006-02-24 21:20:29 -0800153 if (pgoff & (~PAGE_MASK >> 12))
154 return -EINVAL;
155
Al Virof8b72562009-11-30 17:37:04 -0500156 return sys_mmap_pgoff(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159save_static_function(sys_fork);
David Rientjesf5dbeaf2007-07-22 01:01:39 -0700160static int __used noinline
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161_sys_fork(nabi_no_regargs struct pt_regs regs)
162{
163 return do_fork(SIGCHLD, regs.regs[29], &regs, 0, NULL, NULL);
164}
165
166save_static_function(sys_clone);
David Rientjesf5dbeaf2007-07-22 01:01:39 -0700167static int __used noinline
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168_sys_clone(nabi_no_regargs struct pt_regs regs)
169{
170 unsigned long clone_flags;
171 unsigned long newsp;
Ralf Baechle3c370262005-04-13 17:43:59 +0000172 int __user *parent_tidptr, *child_tidptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 clone_flags = regs.regs[4];
175 newsp = regs.regs[5];
176 if (!newsp)
177 newsp = regs.regs[29];
Ralf Baechle3c370262005-04-13 17:43:59 +0000178 parent_tidptr = (int __user *) regs.regs[6];
179#ifdef CONFIG_32BIT
180 /* We need to fetch the fifth argument off the stack. */
181 child_tidptr = NULL;
182 if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) {
183 int __user *__user *usp = (int __user *__user *) regs.regs[29];
184 if (regs.regs[2] == __NR_syscall) {
185 if (get_user (child_tidptr, &usp[5]))
186 return -EFAULT;
187 }
188 else if (get_user (child_tidptr, &usp[4]))
189 return -EFAULT;
190 }
191#else
192 child_tidptr = (int __user *) regs.regs[8];
193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return do_fork(clone_flags, newsp, &regs, 0,
195 parent_tidptr, child_tidptr);
196}
197
198/*
199 * sys_execve() executes a new program.
200 */
201asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs)
202{
203 int error;
204 char * filename;
205
Atsushi Nemotobe6e5182006-02-08 23:39:49 +0900206 filename = getname((char __user *) (long)regs.regs[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 error = PTR_ERR(filename);
208 if (IS_ERR(filename))
209 goto out;
Atsushi Nemotobe6e5182006-02-08 23:39:49 +0900210 error = do_execve(filename, (char __user *__user *) (long)regs.regs[5],
211 (char __user *__user *) (long)regs.regs[6], &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 putname(filename);
213
214out:
215 return error;
216}
217
Ralf Baechledbda6ac2009-02-08 16:00:26 +0000218SYSCALL_DEFINE1(set_thread_area, unsigned long, addr)
Ralf Baechle3c370262005-04-13 17:43:59 +0000219{
Al Virodc8f6022006-01-12 01:06:07 -0800220 struct thread_info *ti = task_thread_info(current);
Ralf Baechle3c370262005-04-13 17:43:59 +0000221
222 ti->tp_value = addr;
Ralf Baechlea3692022007-07-10 17:33:02 +0100223 if (cpu_has_userlocal)
224 write_c0_userlocal(addr);
Ralf Baechle06be3752006-09-19 17:18:53 +0100225
226 return 0;
Ralf Baechle3c370262005-04-13 17:43:59 +0000227}
228
Ralf Baechlef1e39a42009-09-17 02:25:05 +0200229static inline int mips_atomic_set(struct pt_regs *regs,
230 unsigned long addr, unsigned long new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Ralf Baechlef1e39a42009-09-17 02:25:05 +0200232 unsigned long old, tmp;
233 unsigned int err;
234
235 if (unlikely(addr & 3))
236 return -EINVAL;
237
238 if (unlikely(!access_ok(VERIFY_WRITE, addr, 4)))
239 return -EINVAL;
240
241 if (cpu_has_llsc && R10000_LLSC_WAR) {
242 __asm__ __volatile__ (
Ralf Baechlea91be9e2009-12-02 11:33:03 +0000243 " .set mips3 \n"
Ralf Baechlef1e39a42009-09-17 02:25:05 +0200244 " li %[err], 0 \n"
245 "1: ll %[old], (%[addr]) \n"
246 " move %[tmp], %[new] \n"
247 "2: sc %[tmp], (%[addr]) \n"
248 " beqzl %[tmp], 1b \n"
249 "3: \n"
250 " .section .fixup,\"ax\" \n"
251 "4: li %[err], %[efault] \n"
252 " j 3b \n"
253 " .previous \n"
254 " .section __ex_table,\"a\" \n"
255 " "STR(PTR)" 1b, 4b \n"
256 " "STR(PTR)" 2b, 4b \n"
257 " .previous \n"
Ralf Baechlea91be9e2009-12-02 11:33:03 +0000258 " .set mips0 \n"
Ralf Baechlef1e39a42009-09-17 02:25:05 +0200259 : [old] "=&r" (old),
260 [err] "=&r" (err),
261 [tmp] "=&r" (tmp)
262 : [addr] "r" (addr),
263 [new] "r" (new),
264 [efault] "i" (-EFAULT)
265 : "memory");
266 } else if (cpu_has_llsc) {
267 __asm__ __volatile__ (
Ralf Baechlea91be9e2009-12-02 11:33:03 +0000268 " .set mips3 \n"
Ralf Baechlef1e39a42009-09-17 02:25:05 +0200269 " li %[err], 0 \n"
270 "1: ll %[old], (%[addr]) \n"
271 " move %[tmp], %[new] \n"
272 "2: sc %[tmp], (%[addr]) \n"
273 " bnez %[tmp], 4f \n"
274 "3: \n"
275 " .subsection 2 \n"
276 "4: b 1b \n"
277 " .previous \n"
278 " \n"
279 " .section .fixup,\"ax\" \n"
280 "5: li %[err], %[efault] \n"
281 " j 3b \n"
282 " .previous \n"
283 " .section __ex_table,\"a\" \n"
284 " "STR(PTR)" 1b, 5b \n"
285 " "STR(PTR)" 2b, 5b \n"
286 " .previous \n"
Ralf Baechlea91be9e2009-12-02 11:33:03 +0000287 " .set mips0 \n"
Ralf Baechlef1e39a42009-09-17 02:25:05 +0200288 : [old] "=&r" (old),
289 [err] "=&r" (err),
290 [tmp] "=&r" (tmp)
291 : [addr] "r" (addr),
292 [new] "r" (new),
293 [efault] "i" (-EFAULT)
294 : "memory");
295 } else {
296 do {
297 preempt_disable();
298 ll_bit = 1;
299 ll_task = current;
300 preempt_enable();
301
302 err = __get_user(old, (unsigned int *) addr);
303 err |= __put_user(new, (unsigned int *) addr);
304 if (err)
305 break;
306 rmb();
307 } while (!ll_bit);
308 }
309
310 if (unlikely(err))
311 return err;
312
313 regs->regs[2] = old;
314 regs->regs[7] = 0; /* No error */
315
316 /*
317 * Don't let your children do this ...
318 */
319 __asm__ __volatile__(
320 " move $29, %0 \n"
321 " j syscall_exit \n"
322 : /* no outputs */
323 : "r" (regs));
324
325 /* unreached. Honestly. */
326 while (1);
327}
328
329save_static_function(sys_sysmips);
330static int __used noinline
331_sys_sysmips(nabi_no_regargs struct pt_regs regs)
332{
333 long cmd, arg1, arg2, arg3;
334
335 cmd = regs.regs[4];
336 arg1 = regs.regs[5];
337 arg2 = regs.regs[6];
338 arg3 = regs.regs[7];
339
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100340 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 case MIPS_ATOMIC_SET:
Ralf Baechlef1e39a42009-09-17 02:25:05 +0200342 return mips_atomic_set(&regs, arg1, arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 case MIPS_FIXADE:
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100345 if (arg1 & ~3)
346 return -EINVAL;
347
348 if (arg1 & 1)
349 set_thread_flag(TIF_FIXADE);
350 else
351 clear_thread_flag(TIF_FIXADE);
352 if (arg1 & 2)
353 set_thread_flag(TIF_LOGADE);
354 else
355 clear_thread_flag(TIF_FIXADE);
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0;
358
359 case FLUSH_CACHE:
360 __flush_cache_all();
361 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
364 return -EINVAL;
365}
366
367/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * No implemented yet ...
369 */
Ralf Baechledbda6ac2009-02-08 16:00:26 +0000370SYSCALL_DEFINE3(cachectl, char *, addr, int, nbytes, int, op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 return -ENOSYS;
373}
374
375/*
376 * If we ever come here the user sp is bad. Zap the process right away.
377 * Due to the bad stack signaling wouldn't work.
378 */
379asmlinkage void bad_stack(void)
380{
381 do_exit(SIGSEGV);
382}
Arnd Bergmannfe742902006-10-02 02:18:34 -0700383
384/*
385 * Do a system call from kernel instead of calling sys_execve so we
386 * end up with proper pt_regs.
387 */
388int kernel_execve(const char *filename, char *const argv[], char *const envp[])
389{
390 register unsigned long __a0 asm("$4") = (unsigned long) filename;
391 register unsigned long __a1 asm("$5") = (unsigned long) argv;
392 register unsigned long __a2 asm("$6") = (unsigned long) envp;
393 register unsigned long __a3 asm("$7");
394 unsigned long __v0;
395
396 __asm__ volatile (" \n"
397 " .set noreorder \n"
398 " li $2, %5 # __NR_execve \n"
399 " syscall \n"
400 " move %0, $2 \n"
401 " .set reorder \n"
402 : "=&r" (__v0), "=r" (__a3)
403 : "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_execve)
404 : "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24",
405 "memory");
406
407 if (__a3 == 0)
408 return __v0;
409
410 return -__v0;
411}