Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
| 10 | #include <linux/a.out.h> |
Randy Dunlap | a941564 | 2006-01-11 12:17:48 -0800 | [diff] [blame] | 11 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/errno.h> |
| 13 | #include <linux/linkage.h> |
| 14 | #include <linux/mm.h> |
| 15 | #include <linux/smp.h> |
| 16 | #include <linux/smp_lock.h> |
| 17 | #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 Baechle | 9ff77c4 | 2005-03-08 14:39:39 +0000 | [diff] [blame] | 30 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | #include <asm/branch.h> |
| 33 | #include <asm/cachectl.h> |
| 34 | #include <asm/cacheflush.h> |
| 35 | #include <asm/ipc.h> |
Sam Ravnborg | 048eb58 | 2005-09-09 22:32:31 +0200 | [diff] [blame] | 36 | #include <asm/asm-offsets.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #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 | |
| 43 | asmlinkage int sys_pipe(nabi_no_regargs volatile struct pt_regs regs) |
| 44 | { |
| 45 | int fd[2]; |
| 46 | int error, res; |
| 47 | |
| 48 | error = do_pipe(fd); |
| 49 | if (error) { |
| 50 | res = error; |
| 51 | goto out; |
| 52 | } |
| 53 | regs.regs[3] = fd[1]; |
| 54 | res = fd[0]; |
| 55 | out: |
| 56 | return res; |
| 57 | } |
| 58 | |
| 59 | unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */ |
| 60 | |
Ralf Baechle | 9ff77c4 | 2005-03-08 14:39:39 +0000 | [diff] [blame] | 61 | EXPORT_SYMBOL(shm_align_mask); |
| 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #define COLOUR_ALIGN(addr,pgoff) \ |
| 64 | ((((addr) + shm_align_mask) & ~shm_align_mask) + \ |
| 65 | (((pgoff) << PAGE_SHIFT) & shm_align_mask)) |
| 66 | |
| 67 | unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, |
| 68 | unsigned long len, unsigned long pgoff, unsigned long flags) |
| 69 | { |
| 70 | struct vm_area_struct * vmm; |
| 71 | int do_color_align; |
| 72 | unsigned long task_size; |
| 73 | |
| 74 | task_size = STACK_TOP; |
| 75 | |
| 76 | if (flags & MAP_FIXED) { |
| 77 | /* |
| 78 | * We do not accept a shared mapping if it would violate |
| 79 | * cache aliasing constraints. |
| 80 | */ |
| 81 | if ((flags & MAP_SHARED) && (addr & shm_align_mask)) |
| 82 | return -EINVAL; |
| 83 | return addr; |
| 84 | } |
| 85 | |
| 86 | if (len > task_size) |
| 87 | return -ENOMEM; |
| 88 | do_color_align = 0; |
| 89 | if (filp || (flags & MAP_SHARED)) |
| 90 | do_color_align = 1; |
| 91 | if (addr) { |
| 92 | if (do_color_align) |
| 93 | addr = COLOUR_ALIGN(addr, pgoff); |
| 94 | else |
| 95 | addr = PAGE_ALIGN(addr); |
| 96 | vmm = find_vma(current->mm, addr); |
| 97 | if (task_size - len >= addr && |
| 98 | (!vmm || addr + len <= vmm->vm_start)) |
| 99 | return addr; |
| 100 | } |
| 101 | addr = TASK_UNMAPPED_BASE; |
| 102 | if (do_color_align) |
| 103 | addr = COLOUR_ALIGN(addr, pgoff); |
| 104 | else |
| 105 | addr = PAGE_ALIGN(addr); |
| 106 | |
| 107 | for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) { |
| 108 | /* At this point: (!vmm || addr < vmm->vm_end). */ |
| 109 | if (task_size - len < addr) |
| 110 | return -ENOMEM; |
| 111 | if (!vmm || addr + len <= vmm->vm_start) |
| 112 | return addr; |
| 113 | addr = vmm->vm_end; |
| 114 | if (do_color_align) |
| 115 | addr = COLOUR_ALIGN(addr, pgoff); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /* common code for old and new mmaps */ |
| 120 | static inline unsigned long |
| 121 | do_mmap2(unsigned long addr, unsigned long len, unsigned long prot, |
| 122 | unsigned long flags, unsigned long fd, unsigned long pgoff) |
| 123 | { |
| 124 | unsigned long error = -EBADF; |
| 125 | struct file * file = NULL; |
| 126 | |
| 127 | flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE); |
| 128 | if (!(flags & MAP_ANONYMOUS)) { |
| 129 | file = fget(fd); |
| 130 | if (!file) |
| 131 | goto out; |
| 132 | } |
| 133 | |
| 134 | down_write(¤t->mm->mmap_sem); |
| 135 | error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff); |
| 136 | up_write(¤t->mm->mmap_sem); |
| 137 | |
| 138 | if (file) |
| 139 | fput(file); |
| 140 | out: |
| 141 | return error; |
| 142 | } |
| 143 | |
| 144 | asmlinkage unsigned long |
| 145 | old_mmap(unsigned long addr, unsigned long len, int prot, |
| 146 | int flags, int fd, off_t offset) |
| 147 | { |
| 148 | unsigned long result; |
| 149 | |
| 150 | result = -EINVAL; |
| 151 | if (offset & ~PAGE_MASK) |
| 152 | goto out; |
| 153 | |
| 154 | result = do_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT); |
| 155 | |
| 156 | out: |
| 157 | return result; |
| 158 | } |
| 159 | |
| 160 | asmlinkage unsigned long |
| 161 | sys_mmap2(unsigned long addr, unsigned long len, unsigned long prot, |
| 162 | unsigned long flags, unsigned long fd, unsigned long pgoff) |
| 163 | { |
H. Peter Anvin | 947df17 | 2006-02-24 21:20:29 -0800 | [diff] [blame] | 164 | if (pgoff & (~PAGE_MASK >> 12)) |
| 165 | return -EINVAL; |
| 166 | |
| 167 | return do_mmap2(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | save_static_function(sys_fork); |
| 171 | __attribute_used__ noinline static int |
| 172 | _sys_fork(nabi_no_regargs struct pt_regs regs) |
| 173 | { |
| 174 | return do_fork(SIGCHLD, regs.regs[29], ®s, 0, NULL, NULL); |
| 175 | } |
| 176 | |
| 177 | save_static_function(sys_clone); |
| 178 | __attribute_used__ noinline static int |
| 179 | _sys_clone(nabi_no_regargs struct pt_regs regs) |
| 180 | { |
| 181 | unsigned long clone_flags; |
| 182 | unsigned long newsp; |
Ralf Baechle | 3c37026 | 2005-04-13 17:43:59 +0000 | [diff] [blame] | 183 | int __user *parent_tidptr, *child_tidptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | clone_flags = regs.regs[4]; |
| 186 | newsp = regs.regs[5]; |
| 187 | if (!newsp) |
| 188 | newsp = regs.regs[29]; |
Ralf Baechle | 3c37026 | 2005-04-13 17:43:59 +0000 | [diff] [blame] | 189 | parent_tidptr = (int __user *) regs.regs[6]; |
| 190 | #ifdef CONFIG_32BIT |
| 191 | /* We need to fetch the fifth argument off the stack. */ |
| 192 | child_tidptr = NULL; |
| 193 | if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) { |
| 194 | int __user *__user *usp = (int __user *__user *) regs.regs[29]; |
| 195 | if (regs.regs[2] == __NR_syscall) { |
| 196 | if (get_user (child_tidptr, &usp[5])) |
| 197 | return -EFAULT; |
| 198 | } |
| 199 | else if (get_user (child_tidptr, &usp[4])) |
| 200 | return -EFAULT; |
| 201 | } |
| 202 | #else |
| 203 | child_tidptr = (int __user *) regs.regs[8]; |
| 204 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | return do_fork(clone_flags, newsp, ®s, 0, |
| 206 | parent_tidptr, child_tidptr); |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * sys_execve() executes a new program. |
| 211 | */ |
| 212 | asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs) |
| 213 | { |
| 214 | int error; |
| 215 | char * filename; |
| 216 | |
Atsushi Nemoto | be6e518 | 2006-02-08 23:39:49 +0900 | [diff] [blame] | 217 | filename = getname((char __user *) (long)regs.regs[4]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | error = PTR_ERR(filename); |
| 219 | if (IS_ERR(filename)) |
| 220 | goto out; |
Atsushi Nemoto | be6e518 | 2006-02-08 23:39:49 +0900 | [diff] [blame] | 221 | error = do_execve(filename, (char __user *__user *) (long)regs.regs[5], |
| 222 | (char __user *__user *) (long)regs.regs[6], ®s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | putname(filename); |
| 224 | |
| 225 | out: |
| 226 | return error; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Compacrapability ... |
| 231 | */ |
Atsushi Nemoto | be6e518 | 2006-02-08 23:39:49 +0900 | [diff] [blame] | 232 | asmlinkage int sys_uname(struct old_utsname __user * name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
| 234 | if (name && !copy_to_user(name, &system_utsname, sizeof (*name))) |
| 235 | return 0; |
| 236 | return -EFAULT; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Compacrapability ... |
| 241 | */ |
Atsushi Nemoto | be6e518 | 2006-02-08 23:39:49 +0900 | [diff] [blame] | 242 | asmlinkage int sys_olduname(struct oldold_utsname __user * name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { |
| 244 | int error; |
| 245 | |
| 246 | if (!name) |
| 247 | return -EFAULT; |
| 248 | if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname))) |
| 249 | return -EFAULT; |
| 250 | |
| 251 | error = __copy_to_user(&name->sysname,&system_utsname.sysname,__OLD_UTS_LEN); |
| 252 | error -= __put_user(0,name->sysname+__OLD_UTS_LEN); |
| 253 | error -= __copy_to_user(&name->nodename,&system_utsname.nodename,__OLD_UTS_LEN); |
| 254 | error -= __put_user(0,name->nodename+__OLD_UTS_LEN); |
| 255 | error -= __copy_to_user(&name->release,&system_utsname.release,__OLD_UTS_LEN); |
| 256 | error -= __put_user(0,name->release+__OLD_UTS_LEN); |
| 257 | error -= __copy_to_user(&name->version,&system_utsname.version,__OLD_UTS_LEN); |
| 258 | error -= __put_user(0,name->version+__OLD_UTS_LEN); |
| 259 | error -= __copy_to_user(&name->machine,&system_utsname.machine,__OLD_UTS_LEN); |
| 260 | error = __put_user(0,name->machine+__OLD_UTS_LEN); |
| 261 | error = error ? -EFAULT : 0; |
| 262 | |
| 263 | return error; |
| 264 | } |
| 265 | |
Ralf Baechle | 06be375 | 2006-09-19 17:18:53 +0100 | [diff] [blame^] | 266 | asmlinkage int sys_set_thread_area(unsigned long addr) |
Ralf Baechle | 3c37026 | 2005-04-13 17:43:59 +0000 | [diff] [blame] | 267 | { |
Al Viro | dc8f602 | 2006-01-12 01:06:07 -0800 | [diff] [blame] | 268 | struct thread_info *ti = task_thread_info(current); |
Ralf Baechle | 3c37026 | 2005-04-13 17:43:59 +0000 | [diff] [blame] | 269 | |
| 270 | ti->tp_value = addr; |
| 271 | |
| 272 | /* If some future MIPS implementation has this register in hardware, |
| 273 | * we will need to update it here (and in context switches). */ |
Ralf Baechle | 06be375 | 2006-09-19 17:18:53 +0100 | [diff] [blame^] | 274 | |
| 275 | return 0; |
Ralf Baechle | 3c37026 | 2005-04-13 17:43:59 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | asmlinkage int _sys_sysmips(int cmd, long arg1, int arg2, int arg3) |
| 279 | { |
Atsushi Nemoto | ecf52d3 | 2006-06-01 01:00:03 +0900 | [diff] [blame] | 280 | int tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
| 282 | switch(cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | case MIPS_ATOMIC_SET: |
| 284 | printk(KERN_CRIT "How did I get here?\n"); |
| 285 | return -EINVAL; |
| 286 | |
| 287 | case MIPS_FIXADE: |
| 288 | tmp = current->thread.mflags & ~3; |
| 289 | current->thread.mflags = tmp | (arg1 & 3); |
| 290 | return 0; |
| 291 | |
| 292 | case FLUSH_CACHE: |
| 293 | __flush_cache_all(); |
| 294 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | return -EINVAL; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * sys_ipc() is the de-multiplexer for the SysV IPC calls.. |
| 302 | * |
| 303 | * This is really horribly ugly. |
| 304 | */ |
Ralf Baechle | fc10334 | 2006-06-28 11:24:12 +0100 | [diff] [blame] | 305 | asmlinkage int sys_ipc (unsigned int call, int first, int second, |
Atsushi Nemoto | be6e518 | 2006-02-08 23:39:49 +0900 | [diff] [blame] | 306 | unsigned long third, void __user *ptr, long fifth) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | { |
| 308 | int version, ret; |
| 309 | |
| 310 | version = call >> 16; /* hack for backward compatibility */ |
| 311 | call &= 0xffff; |
| 312 | |
| 313 | switch (call) { |
| 314 | case SEMOP: |
Atsushi Nemoto | be6e518 | 2006-02-08 23:39:49 +0900 | [diff] [blame] | 315 | return sys_semtimedop (first, (struct sembuf __user *)ptr, |
| 316 | second, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | case SEMTIMEDOP: |
Atsushi Nemoto | be6e518 | 2006-02-08 23:39:49 +0900 | [diff] [blame] | 318 | return sys_semtimedop (first, (struct sembuf __user *)ptr, |
| 319 | second, |
| 320 | (const struct timespec __user *)fifth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | case SEMGET: |
| 322 | return sys_semget (first, second, third); |
| 323 | case SEMCTL: { |
| 324 | union semun fourth; |
| 325 | if (!ptr) |
| 326 | return -EINVAL; |
Atsushi Nemoto | 219ac73 | 2006-02-21 16:05:11 +0900 | [diff] [blame] | 327 | if (get_user(fourth.__pad, (void __user *__user *) ptr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | return -EFAULT; |
| 329 | return sys_semctl (first, second, third, fourth); |
| 330 | } |
| 331 | |
| 332 | case MSGSND: |
Atsushi Nemoto | be6e518 | 2006-02-08 23:39:49 +0900 | [diff] [blame] | 333 | return sys_msgsnd (first, (struct msgbuf __user *) ptr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | second, third); |
| 335 | case MSGRCV: |
| 336 | switch (version) { |
| 337 | case 0: { |
| 338 | struct ipc_kludge tmp; |
| 339 | if (!ptr) |
| 340 | return -EINVAL; |
| 341 | |
| 342 | if (copy_from_user(&tmp, |
Atsushi Nemoto | be6e518 | 2006-02-08 23:39:49 +0900 | [diff] [blame] | 343 | (struct ipc_kludge __user *) ptr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | sizeof (tmp))) |
| 345 | return -EFAULT; |
| 346 | return sys_msgrcv (first, tmp.msgp, second, |
| 347 | tmp.msgtyp, third); |
| 348 | } |
| 349 | default: |
| 350 | return sys_msgrcv (first, |
Atsushi Nemoto | be6e518 | 2006-02-08 23:39:49 +0900 | [diff] [blame] | 351 | (struct msgbuf __user *) ptr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | second, fifth, third); |
| 353 | } |
| 354 | case MSGGET: |
| 355 | return sys_msgget ((key_t) first, second); |
| 356 | case MSGCTL: |
Atsushi Nemoto | be6e518 | 2006-02-08 23:39:49 +0900 | [diff] [blame] | 357 | return sys_msgctl (first, second, |
| 358 | (struct msqid_ds __user *) ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
| 360 | case SHMAT: |
| 361 | switch (version) { |
| 362 | default: { |
Ralf Baechle | fc10334 | 2006-06-28 11:24:12 +0100 | [diff] [blame] | 363 | unsigned long raddr; |
Atsushi Nemoto | be6e518 | 2006-02-08 23:39:49 +0900 | [diff] [blame] | 364 | ret = do_shmat (first, (char __user *) ptr, second, |
| 365 | &raddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | if (ret) |
| 367 | return ret; |
Ralf Baechle | fc10334 | 2006-06-28 11:24:12 +0100 | [diff] [blame] | 368 | return put_user (raddr, (unsigned long __user *) third); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } |
| 370 | case 1: /* iBCS2 emulator entry point */ |
| 371 | if (!segment_eq(get_fs(), get_ds())) |
| 372 | return -EINVAL; |
Atsushi Nemoto | be6e518 | 2006-02-08 23:39:49 +0900 | [diff] [blame] | 373 | return do_shmat (first, (char __user *) ptr, second, |
Ralf Baechle | fc10334 | 2006-06-28 11:24:12 +0100 | [diff] [blame] | 374 | (unsigned long *) third); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | } |
| 376 | case SHMDT: |
Atsushi Nemoto | be6e518 | 2006-02-08 23:39:49 +0900 | [diff] [blame] | 377 | return sys_shmdt ((char __user *)ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | case SHMGET: |
| 379 | return sys_shmget (first, second, third); |
| 380 | case SHMCTL: |
| 381 | return sys_shmctl (first, second, |
Atsushi Nemoto | be6e518 | 2006-02-08 23:39:49 +0900 | [diff] [blame] | 382 | (struct shmid_ds __user *) ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | default: |
| 384 | return -ENOSYS; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | * No implemented yet ... |
| 390 | */ |
| 391 | asmlinkage int sys_cachectl(char *addr, int nbytes, int op) |
| 392 | { |
| 393 | return -ENOSYS; |
| 394 | } |
| 395 | |
| 396 | /* |
| 397 | * If we ever come here the user sp is bad. Zap the process right away. |
| 398 | * Due to the bad stack signaling wouldn't work. |
| 399 | */ |
| 400 | asmlinkage void bad_stack(void) |
| 401 | { |
| 402 | do_exit(SIGSEGV); |
| 403 | } |