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>
35 #include <asm/branch.h>
36 #include <asm/cachectl.h>
37 #include <asm/cacheflush.h>
38 #include <asm/asm-offsets.h>
39 #include <asm/signal.h>
41 #include <asm/shmparam.h>
42 #include <asm/sysmips.h>
43 #include <asm/uaccess.h>
46 * For historic reasons the pipe(2) syscall on MIPS has an unusual calling
47 * convention. It returns results in registers $v0 / $v1 which means there
48 * is no need for it to do verify the validity of a userspace pointer
49 * argument. Historically that used to be expensive in Linux. These days
50 * the performance advantage is negligible.
52 asmlinkage int sysm_pipe(nabi_no_regargs volatile struct pt_regs regs)
57 error = do_pipe_flags(fd, 0);
68 unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
70 EXPORT_SYMBOL(shm_align_mask);
72 #define COLOUR_ALIGN(addr,pgoff) \
73 ((((addr) + shm_align_mask) & ~shm_align_mask) + \
74 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
76 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
77 unsigned long len, unsigned long pgoff, unsigned long flags)
79 struct vm_area_struct * vmm;
81 unsigned long task_size;
84 task_size = TASK_SIZE;
85 #else /* Must be CONFIG_64BIT*/
86 task_size = test_thread_flag(TIF_32BIT_ADDR) ? TASK_SIZE32 : TASK_SIZE;
92 if (flags & MAP_FIXED) {
93 /* Even MAP_FIXED mappings must reside within task_size. */
94 if (task_size - len < addr)
98 * We do not accept a shared mapping if it would violate
99 * cache aliasing constraints.
101 if ((flags & MAP_SHARED) &&
102 ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
108 if (filp || (flags & MAP_SHARED))
112 addr = COLOUR_ALIGN(addr, pgoff);
114 addr = PAGE_ALIGN(addr);
115 vmm = find_vma(current->mm, addr);
116 if (task_size - len >= addr &&
117 (!vmm || addr + len <= vmm->vm_start))
120 addr = current->mm->mmap_base;
122 addr = COLOUR_ALIGN(addr, pgoff);
124 addr = PAGE_ALIGN(addr);
126 for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
127 /* At this point: (!vmm || addr < vmm->vm_end). */
128 if (task_size - len < addr)
130 if (!vmm || addr + len <= vmm->vm_start)
134 addr = COLOUR_ALIGN(addr, pgoff);
138 void arch_pick_mmap_layout(struct mm_struct *mm)
140 unsigned long random_factor = 0UL;
142 if (current->flags & PF_RANDOMIZE) {
143 random_factor = get_random_int();
144 random_factor = random_factor << PAGE_SHIFT;
145 if (TASK_IS_32BIT_ADDR)
146 random_factor &= 0xfffffful;
148 random_factor &= 0xffffffful;
151 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
152 mm->get_unmapped_area = arch_get_unmapped_area;
153 mm->unmap_area = arch_unmap_area;
156 SYSCALL_DEFINE6(mips_mmap, unsigned long, addr, unsigned long, len,
157 unsigned long, prot, unsigned long, flags, unsigned long,
160 unsigned long result;
163 if (offset & ~PAGE_MASK)
166 result = sys_mmap_pgoff(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
172 SYSCALL_DEFINE6(mips_mmap2, unsigned long, addr, unsigned long, len,
173 unsigned long, prot, unsigned long, flags, unsigned long, fd,
174 unsigned long, pgoff)
176 if (pgoff & (~PAGE_MASK >> 12))
179 return sys_mmap_pgoff(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12));
182 save_static_function(sys_fork);
183 static int __used noinline
184 _sys_fork(nabi_no_regargs struct pt_regs regs)
186 return do_fork(SIGCHLD, regs.regs[29], ®s, 0, NULL, NULL);
189 save_static_function(sys_clone);
190 static int __used noinline
191 _sys_clone(nabi_no_regargs struct pt_regs regs)
193 unsigned long clone_flags;
195 int __user *parent_tidptr, *child_tidptr;
197 clone_flags = regs.regs[4];
198 newsp = regs.regs[5];
200 newsp = regs.regs[29];
201 parent_tidptr = (int __user *) regs.regs[6];
203 /* We need to fetch the fifth argument off the stack. */
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]))
211 else if (get_user (child_tidptr, &usp[4]))
215 child_tidptr = (int __user *) regs.regs[8];
217 return do_fork(clone_flags, newsp, ®s, 0,
218 parent_tidptr, child_tidptr);
222 * sys_execve() executes a new program.
224 asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs)
229 filename = getname((char __user *) (long)regs.regs[4]);
230 error = PTR_ERR(filename);
231 if (IS_ERR(filename))
233 error = do_execve(filename, (char __user *__user *) (long)regs.regs[5],
234 (char __user *__user *) (long)regs.regs[6], ®s);
241 SYSCALL_DEFINE1(set_thread_area, unsigned long, addr)
243 struct thread_info *ti = task_thread_info(current);
246 if (cpu_has_userlocal)
247 write_c0_userlocal(addr);
252 static inline int mips_atomic_set(struct pt_regs *regs,
253 unsigned long addr, unsigned long new)
255 unsigned long old, tmp;
258 if (unlikely(addr & 3))
261 if (unlikely(!access_ok(VERIFY_WRITE, addr, 4)))
264 if (cpu_has_llsc && R10000_LLSC_WAR) {
265 __asm__ __volatile__ (
268 "1: ll %[old], (%[addr]) \n"
269 " move %[tmp], %[new] \n"
270 "2: sc %[tmp], (%[addr]) \n"
271 " beqzl %[tmp], 1b \n"
273 " .section .fixup,\"ax\" \n"
274 "4: li %[err], %[efault] \n"
277 " .section __ex_table,\"a\" \n"
278 " "STR(PTR)" 1b, 4b \n"
279 " "STR(PTR)" 2b, 4b \n"
287 [efault] "i" (-EFAULT)
289 } else if (cpu_has_llsc) {
290 __asm__ __volatile__ (
293 "1: ll %[old], (%[addr]) \n"
294 " move %[tmp], %[new] \n"
295 "2: sc %[tmp], (%[addr]) \n"
296 " bnez %[tmp], 4f \n"
302 " .section .fixup,\"ax\" \n"
303 "5: li %[err], %[efault] \n"
306 " .section __ex_table,\"a\" \n"
307 " "STR(PTR)" 1b, 5b \n"
308 " "STR(PTR)" 2b, 5b \n"
316 [efault] "i" (-EFAULT)
325 err = __get_user(old, (unsigned int *) addr);
326 err |= __put_user(new, (unsigned int *) addr);
337 regs->regs[7] = 0; /* No error */
340 * Don't let your children do this ...
342 __asm__ __volatile__(
348 /* unreached. Honestly. */
352 save_static_function(sys_sysmips);
353 static int __used noinline
354 _sys_sysmips(nabi_no_regargs struct pt_regs regs)
356 long cmd, arg1, arg2, arg3;
364 case MIPS_ATOMIC_SET:
365 return mips_atomic_set(®s, arg1, arg2);
372 set_thread_flag(TIF_FIXADE);
374 clear_thread_flag(TIF_FIXADE);
376 set_thread_flag(TIF_LOGADE);
378 clear_thread_flag(TIF_FIXADE);
391 * No implemented yet ...
393 SYSCALL_DEFINE3(cachectl, char *, addr, int, nbytes, int, op)
399 * If we ever come here the user sp is bad. Zap the process right away.
400 * Due to the bad stack signaling wouldn't work.
402 asmlinkage void bad_stack(void)
408 * Do a system call from kernel instead of calling sys_execve so we
409 * end up with proper pt_regs.
411 int kernel_execve(const char *filename, char *const argv[], char *const envp[])
413 register unsigned long __a0 asm("$4") = (unsigned long) filename;
414 register unsigned long __a1 asm("$5") = (unsigned long) argv;
415 register unsigned long __a2 asm("$6") = (unsigned long) envp;
416 register unsigned long __a3 asm("$7");
419 __asm__ volatile (" \n"
421 " li $2, %5 # __NR_execve \n"
425 : "=&r" (__v0), "=r" (__a3)
426 : "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_execve)
427 : "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24",