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
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.
10 #include <linux/capability.h>
11 #include <linux/errno.h>
12 #include <linux/linkage.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/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>
28 #include <linux/module.h>
29 #include <linux/ipc.h>
30 #include <linux/uaccess.h>
31 #include <linux/slab.h>
32 #include <linux/random.h>
33 #include <linux/elf.h>
36 #include <asm/branch.h>
37 #include <asm/cachectl.h>
38 #include <asm/cacheflush.h>
39 #include <asm/asm-offsets.h>
40 #include <asm/signal.h>
42 #include <asm/shmparam.h>
43 #include <asm/sysmips.h>
44 #include <asm/uaccess.h>
47 * For historic reasons the pipe(2) syscall on MIPS has an unusual calling
48 * convention. It returns results in registers $v0 / $v1 which means there
49 * is no need for it to do verify the validity of a userspace pointer
50 * argument. Historically that used to be expensive in Linux. These days
51 * the performance advantage is negligible.
53 asmlinkage int sysm_pipe(nabi_no_regargs volatile struct pt_regs regs)
58 error = do_pipe_flags(fd, 0);
69 unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
71 EXPORT_SYMBOL(shm_align_mask);
73 #define COLOUR_ALIGN(addr,pgoff) \
74 ((((addr) + shm_align_mask) & ~shm_align_mask) + \
75 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
77 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
78 unsigned long len, unsigned long pgoff, unsigned long flags)
80 struct vm_area_struct * vmm;
86 if (flags & MAP_FIXED) {
87 /* Even MAP_FIXED mappings must reside within TASK_SIZE. */
88 if (TASK_SIZE - len < addr)
92 * We do not accept a shared mapping if it would violate
93 * cache aliasing constraints.
95 if ((flags & MAP_SHARED) &&
96 ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
102 if (filp || (flags & MAP_SHARED))
106 addr = COLOUR_ALIGN(addr, pgoff);
108 addr = PAGE_ALIGN(addr);
109 vmm = find_vma(current->mm, addr);
110 if (TASK_SIZE - len >= addr &&
111 (!vmm || addr + len <= vmm->vm_start))
114 addr = current->mm->mmap_base;
116 addr = COLOUR_ALIGN(addr, pgoff);
118 addr = PAGE_ALIGN(addr);
120 for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
121 /* At this point: (!vmm || addr < vmm->vm_end). */
122 if (TASK_SIZE - len < addr)
124 if (!vmm || addr + len <= vmm->vm_start)
128 addr = COLOUR_ALIGN(addr, pgoff);
132 void arch_pick_mmap_layout(struct mm_struct *mm)
134 unsigned long random_factor = 0UL;
136 if (current->flags & PF_RANDOMIZE) {
137 random_factor = get_random_int();
138 random_factor = random_factor << PAGE_SHIFT;
139 if (TASK_IS_32BIT_ADDR)
140 random_factor &= 0xfffffful;
142 random_factor &= 0xffffffful;
145 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
146 mm->get_unmapped_area = arch_get_unmapped_area;
147 mm->unmap_area = arch_unmap_area;
150 static inline unsigned long brk_rnd(void)
152 unsigned long rnd = get_random_int();
154 rnd = rnd << PAGE_SHIFT;
155 /* 8MB for 32bit, 256MB for 64bit */
156 if (TASK_IS_32BIT_ADDR)
157 rnd = rnd & 0x7ffffful;
159 rnd = rnd & 0xffffffful;
164 unsigned long arch_randomize_brk(struct mm_struct *mm)
166 unsigned long base = mm->brk;
169 ret = PAGE_ALIGN(base + brk_rnd());
177 SYSCALL_DEFINE6(mips_mmap, unsigned long, addr, unsigned long, len,
178 unsigned long, prot, unsigned long, flags, unsigned long,
181 unsigned long result;
184 if (offset & ~PAGE_MASK)
187 result = sys_mmap_pgoff(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
193 SYSCALL_DEFINE6(mips_mmap2, unsigned long, addr, unsigned long, len,
194 unsigned long, prot, unsigned long, flags, unsigned long, fd,
195 unsigned long, pgoff)
197 if (pgoff & (~PAGE_MASK >> 12))
200 return sys_mmap_pgoff(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
203 save_static_function(sys_fork);
204 static int __used noinline
205 _sys_fork(nabi_no_regargs struct pt_regs regs)
207 return do_fork(SIGCHLD, regs.regs[29], ®s, 0, NULL, NULL);
210 save_static_function(sys_clone);
211 static int __used noinline
212 _sys_clone(nabi_no_regargs struct pt_regs regs)
214 unsigned long clone_flags;
216 int __user *parent_tidptr, *child_tidptr;
218 clone_flags = regs.regs[4];
219 newsp = regs.regs[5];
221 newsp = regs.regs[29];
222 parent_tidptr = (int __user *) regs.regs[6];
224 /* We need to fetch the fifth argument off the stack. */
226 if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) {
227 int __user *__user *usp = (int __user *__user *) regs.regs[29];
228 if (regs.regs[2] == __NR_syscall) {
229 if (get_user (child_tidptr, &usp[5]))
232 else if (get_user (child_tidptr, &usp[4]))
236 child_tidptr = (int __user *) regs.regs[8];
238 return do_fork(clone_flags, newsp, ®s, 0,
239 parent_tidptr, child_tidptr);
243 * sys_execve() executes a new program.
245 asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs)
250 filename = getname((const char __user *) (long)regs.regs[4]);
251 error = PTR_ERR(filename);
252 if (IS_ERR(filename))
254 error = do_execve(filename,
255 (const char __user *const __user *) (long)regs.regs[5],
256 (const char __user *const __user *) (long)regs.regs[6],
264 SYSCALL_DEFINE1(set_thread_area, unsigned long, addr)
266 struct thread_info *ti = task_thread_info(current);
269 if (cpu_has_userlocal)
270 write_c0_userlocal(addr);
275 static inline int mips_atomic_set(struct pt_regs *regs,
276 unsigned long addr, unsigned long new)
278 unsigned long old, tmp;
281 if (unlikely(addr & 3))
284 if (unlikely(!access_ok(VERIFY_WRITE, addr, 4)))
287 if (cpu_has_llsc && R10000_LLSC_WAR) {
288 __asm__ __volatile__ (
291 "1: ll %[old], (%[addr]) \n"
292 " move %[tmp], %[new] \n"
293 "2: sc %[tmp], (%[addr]) \n"
294 " beqzl %[tmp], 1b \n"
296 " .section .fixup,\"ax\" \n"
297 "4: li %[err], %[efault] \n"
300 " .section __ex_table,\"a\" \n"
301 " "STR(PTR)" 1b, 4b \n"
302 " "STR(PTR)" 2b, 4b \n"
310 [efault] "i" (-EFAULT)
312 } else if (cpu_has_llsc) {
313 __asm__ __volatile__ (
316 "1: ll %[old], (%[addr]) \n"
317 " move %[tmp], %[new] \n"
318 "2: sc %[tmp], (%[addr]) \n"
319 " bnez %[tmp], 4f \n"
325 " .section .fixup,\"ax\" \n"
326 "5: li %[err], %[efault] \n"
329 " .section __ex_table,\"a\" \n"
330 " "STR(PTR)" 1b, 5b \n"
331 " "STR(PTR)" 2b, 5b \n"
339 [efault] "i" (-EFAULT)
348 err = __get_user(old, (unsigned int *) addr);
349 err |= __put_user(new, (unsigned int *) addr);
360 regs->regs[7] = 0; /* No error */
363 * Don't let your children do this ...
365 __asm__ __volatile__(
371 /* unreached. Honestly. */
375 save_static_function(sys_sysmips);
376 static int __used noinline
377 _sys_sysmips(nabi_no_regargs struct pt_regs regs)
379 long cmd, arg1, arg2;
386 case MIPS_ATOMIC_SET:
387 return mips_atomic_set(®s, arg1, arg2);
394 set_thread_flag(TIF_FIXADE);
396 clear_thread_flag(TIF_FIXADE);
398 set_thread_flag(TIF_LOGADE);
400 clear_thread_flag(TIF_LOGADE);
413 * No implemented yet ...
415 SYSCALL_DEFINE3(cachectl, char *, addr, int, nbytes, int, op)
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.
424 asmlinkage void bad_stack(void)
430 * Do a system call from kernel instead of calling sys_execve so we
431 * end up with proper pt_regs.
433 int kernel_execve(const char *filename,
434 const char *const argv[],
435 const char *const envp[])
437 register unsigned long __a0 asm("$4") = (unsigned long) filename;
438 register unsigned long __a1 asm("$5") = (unsigned long) argv;
439 register unsigned long __a2 asm("$6") = (unsigned long) envp;
440 register unsigned long __a3 asm("$7");
443 __asm__ volatile (" \n"
445 " li $2, %5 # __NR_execve \n"
449 : "=&r" (__v0), "=r" (__a3)
450 : "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_execve)
451 : "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24",