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 | */ |
Ralf Baechle | 3c37026 | 2005-04-13 17:43:59 +0000 | [diff] [blame] | 10 | #include <linux/config.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/a.out.h> |
Randy Dunlap | a941564 | 2006-01-11 12:17:48 -0800 | [diff] [blame] | 12 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Ralf Baechle | 9ff77c4 | 2005-03-08 14:39:39 +0000 | [diff] [blame] | 31 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | #include <asm/branch.h> |
| 34 | #include <asm/cachectl.h> |
| 35 | #include <asm/cacheflush.h> |
| 36 | #include <asm/ipc.h> |
Sam Ravnborg | 048eb58 | 2005-09-09 22:32:31 +0200 | [diff] [blame] | 37 | #include <asm/asm-offsets.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 44 | asmlinkage 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]; |
| 56 | out: |
| 57 | return res; |
| 58 | } |
| 59 | |
| 60 | unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */ |
| 61 | |
Ralf Baechle | 9ff77c4 | 2005-03-08 14:39:39 +0000 | [diff] [blame] | 62 | EXPORT_SYMBOL(shm_align_mask); |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | #define COLOUR_ALIGN(addr,pgoff) \ |
| 65 | ((((addr) + shm_align_mask) & ~shm_align_mask) + \ |
| 66 | (((pgoff) << PAGE_SHIFT) & shm_align_mask)) |
| 67 | |
| 68 | unsigned 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 */ |
| 121 | static inline unsigned long |
| 122 | do_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(¤t->mm->mmap_sem); |
| 136 | error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff); |
| 137 | up_write(¤t->mm->mmap_sem); |
| 138 | |
| 139 | if (file) |
| 140 | fput(file); |
| 141 | out: |
| 142 | return error; |
| 143 | } |
| 144 | |
| 145 | asmlinkage unsigned long |
| 146 | old_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 | |
| 157 | out: |
| 158 | return result; |
| 159 | } |
| 160 | |
| 161 | asmlinkage unsigned long |
| 162 | sys_mmap2(unsigned long addr, unsigned long len, unsigned long prot, |
| 163 | unsigned long flags, unsigned long fd, unsigned long pgoff) |
| 164 | { |
| 165 | return do_mmap2(addr, len, prot, flags, fd, pgoff); |
| 166 | } |
| 167 | |
| 168 | save_static_function(sys_fork); |
| 169 | __attribute_used__ noinline static int |
| 170 | _sys_fork(nabi_no_regargs struct pt_regs regs) |
| 171 | { |
| 172 | return do_fork(SIGCHLD, regs.regs[29], ®s, 0, NULL, NULL); |
| 173 | } |
| 174 | |
| 175 | save_static_function(sys_clone); |
| 176 | __attribute_used__ noinline static int |
| 177 | _sys_clone(nabi_no_regargs struct pt_regs regs) |
| 178 | { |
| 179 | unsigned long clone_flags; |
| 180 | unsigned long newsp; |
Ralf Baechle | 3c37026 | 2005-04-13 17:43:59 +0000 | [diff] [blame] | 181 | int __user *parent_tidptr, *child_tidptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
| 183 | clone_flags = regs.regs[4]; |
| 184 | newsp = regs.regs[5]; |
| 185 | if (!newsp) |
| 186 | newsp = regs.regs[29]; |
Ralf Baechle | 3c37026 | 2005-04-13 17:43:59 +0000 | [diff] [blame] | 187 | parent_tidptr = (int __user *) regs.regs[6]; |
| 188 | #ifdef CONFIG_32BIT |
| 189 | /* We need to fetch the fifth argument off the stack. */ |
| 190 | child_tidptr = NULL; |
| 191 | if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) { |
| 192 | int __user *__user *usp = (int __user *__user *) regs.regs[29]; |
| 193 | if (regs.regs[2] == __NR_syscall) { |
| 194 | if (get_user (child_tidptr, &usp[5])) |
| 195 | return -EFAULT; |
| 196 | } |
| 197 | else if (get_user (child_tidptr, &usp[4])) |
| 198 | return -EFAULT; |
| 199 | } |
| 200 | #else |
| 201 | child_tidptr = (int __user *) regs.regs[8]; |
| 202 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | return do_fork(clone_flags, newsp, ®s, 0, |
| 204 | parent_tidptr, child_tidptr); |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * sys_execve() executes a new program. |
| 209 | */ |
| 210 | asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs) |
| 211 | { |
| 212 | int error; |
| 213 | char * filename; |
| 214 | |
| 215 | filename = getname((char *) (long)regs.regs[4]); |
| 216 | error = PTR_ERR(filename); |
| 217 | if (IS_ERR(filename)) |
| 218 | goto out; |
| 219 | error = do_execve(filename, (char **) (long)regs.regs[5], |
| 220 | (char **) (long)regs.regs[6], ®s); |
| 221 | putname(filename); |
| 222 | |
| 223 | out: |
| 224 | return error; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Compacrapability ... |
| 229 | */ |
| 230 | asmlinkage int sys_uname(struct old_utsname * name) |
| 231 | { |
| 232 | if (name && !copy_to_user(name, &system_utsname, sizeof (*name))) |
| 233 | return 0; |
| 234 | return -EFAULT; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Compacrapability ... |
| 239 | */ |
| 240 | asmlinkage int sys_olduname(struct oldold_utsname * name) |
| 241 | { |
| 242 | int error; |
| 243 | |
| 244 | if (!name) |
| 245 | return -EFAULT; |
| 246 | if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname))) |
| 247 | return -EFAULT; |
| 248 | |
| 249 | error = __copy_to_user(&name->sysname,&system_utsname.sysname,__OLD_UTS_LEN); |
| 250 | error -= __put_user(0,name->sysname+__OLD_UTS_LEN); |
| 251 | error -= __copy_to_user(&name->nodename,&system_utsname.nodename,__OLD_UTS_LEN); |
| 252 | error -= __put_user(0,name->nodename+__OLD_UTS_LEN); |
| 253 | error -= __copy_to_user(&name->release,&system_utsname.release,__OLD_UTS_LEN); |
| 254 | error -= __put_user(0,name->release+__OLD_UTS_LEN); |
| 255 | error -= __copy_to_user(&name->version,&system_utsname.version,__OLD_UTS_LEN); |
| 256 | error -= __put_user(0,name->version+__OLD_UTS_LEN); |
| 257 | error -= __copy_to_user(&name->machine,&system_utsname.machine,__OLD_UTS_LEN); |
| 258 | error = __put_user(0,name->machine+__OLD_UTS_LEN); |
| 259 | error = error ? -EFAULT : 0; |
| 260 | |
| 261 | return error; |
| 262 | } |
| 263 | |
Ralf Baechle | 3c37026 | 2005-04-13 17:43:59 +0000 | [diff] [blame] | 264 | void sys_set_thread_area(unsigned long addr) |
| 265 | { |
Al Viro | dc8f602 | 2006-01-12 01:06:07 -0800 | [diff] [blame^] | 266 | struct thread_info *ti = task_thread_info(current); |
Ralf Baechle | 3c37026 | 2005-04-13 17:43:59 +0000 | [diff] [blame] | 267 | |
| 268 | ti->tp_value = addr; |
| 269 | |
| 270 | /* If some future MIPS implementation has this register in hardware, |
| 271 | * we will need to update it here (and in context switches). */ |
| 272 | } |
| 273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | asmlinkage int _sys_sysmips(int cmd, long arg1, int arg2, int arg3) |
| 275 | { |
| 276 | int tmp, len; |
| 277 | char *name; |
| 278 | |
| 279 | switch(cmd) { |
| 280 | case SETNAME: { |
| 281 | char nodename[__NEW_UTS_LEN + 1]; |
| 282 | |
| 283 | if (!capable(CAP_SYS_ADMIN)) |
| 284 | return -EPERM; |
| 285 | |
| 286 | name = (char *) arg1; |
| 287 | |
| 288 | len = strncpy_from_user(nodename, name, __NEW_UTS_LEN); |
| 289 | if (len < 0) |
| 290 | return -EFAULT; |
| 291 | |
| 292 | down_write(&uts_sem); |
| 293 | strncpy(system_utsname.nodename, nodename, len); |
| 294 | nodename[__NEW_UTS_LEN] = '\0'; |
| 295 | strlcpy(system_utsname.nodename, nodename, |
| 296 | sizeof(system_utsname.nodename)); |
| 297 | up_write(&uts_sem); |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | case MIPS_ATOMIC_SET: |
| 302 | printk(KERN_CRIT "How did I get here?\n"); |
| 303 | return -EINVAL; |
| 304 | |
| 305 | case MIPS_FIXADE: |
| 306 | tmp = current->thread.mflags & ~3; |
| 307 | current->thread.mflags = tmp | (arg1 & 3); |
| 308 | return 0; |
| 309 | |
| 310 | case FLUSH_CACHE: |
| 311 | __flush_cache_all(); |
| 312 | return 0; |
| 313 | |
| 314 | case MIPS_RDNVRAM: |
| 315 | return -EIO; |
| 316 | } |
| 317 | |
| 318 | return -EINVAL; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * sys_ipc() is the de-multiplexer for the SysV IPC calls.. |
| 323 | * |
| 324 | * This is really horribly ugly. |
| 325 | */ |
| 326 | asmlinkage int sys_ipc (uint call, int first, int second, |
| 327 | unsigned long third, void *ptr, long fifth) |
| 328 | { |
| 329 | int version, ret; |
| 330 | |
| 331 | version = call >> 16; /* hack for backward compatibility */ |
| 332 | call &= 0xffff; |
| 333 | |
| 334 | switch (call) { |
| 335 | case SEMOP: |
| 336 | return sys_semtimedop (first, (struct sembuf *)ptr, second, |
| 337 | NULL); |
| 338 | case SEMTIMEDOP: |
| 339 | return sys_semtimedop (first, (struct sembuf *)ptr, second, |
| 340 | (const struct timespec __user *)fifth); |
| 341 | case SEMGET: |
| 342 | return sys_semget (first, second, third); |
| 343 | case SEMCTL: { |
| 344 | union semun fourth; |
| 345 | if (!ptr) |
| 346 | return -EINVAL; |
| 347 | if (get_user(fourth.__pad, (void **) ptr)) |
| 348 | return -EFAULT; |
| 349 | return sys_semctl (first, second, third, fourth); |
| 350 | } |
| 351 | |
| 352 | case MSGSND: |
| 353 | return sys_msgsnd (first, (struct msgbuf *) ptr, |
| 354 | second, third); |
| 355 | case MSGRCV: |
| 356 | switch (version) { |
| 357 | case 0: { |
| 358 | struct ipc_kludge tmp; |
| 359 | if (!ptr) |
| 360 | return -EINVAL; |
| 361 | |
| 362 | if (copy_from_user(&tmp, |
| 363 | (struct ipc_kludge *) ptr, |
| 364 | sizeof (tmp))) |
| 365 | return -EFAULT; |
| 366 | return sys_msgrcv (first, tmp.msgp, second, |
| 367 | tmp.msgtyp, third); |
| 368 | } |
| 369 | default: |
| 370 | return sys_msgrcv (first, |
| 371 | (struct msgbuf *) ptr, |
| 372 | second, fifth, third); |
| 373 | } |
| 374 | case MSGGET: |
| 375 | return sys_msgget ((key_t) first, second); |
| 376 | case MSGCTL: |
| 377 | return sys_msgctl (first, second, (struct msqid_ds *) ptr); |
| 378 | |
| 379 | case SHMAT: |
| 380 | switch (version) { |
| 381 | default: { |
| 382 | ulong raddr; |
| 383 | ret = do_shmat (first, (char *) ptr, second, &raddr); |
| 384 | if (ret) |
| 385 | return ret; |
| 386 | return put_user (raddr, (ulong *) third); |
| 387 | } |
| 388 | case 1: /* iBCS2 emulator entry point */ |
| 389 | if (!segment_eq(get_fs(), get_ds())) |
| 390 | return -EINVAL; |
| 391 | return do_shmat (first, (char *) ptr, second, (ulong *) third); |
| 392 | } |
| 393 | case SHMDT: |
| 394 | return sys_shmdt ((char *)ptr); |
| 395 | case SHMGET: |
| 396 | return sys_shmget (first, second, third); |
| 397 | case SHMCTL: |
| 398 | return sys_shmctl (first, second, |
| 399 | (struct shmid_ds *) ptr); |
| 400 | default: |
| 401 | return -ENOSYS; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | * No implemented yet ... |
| 407 | */ |
| 408 | asmlinkage int sys_cachectl(char *addr, int nbytes, int op) |
| 409 | { |
| 410 | return -ENOSYS; |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * If we ever come here the user sp is bad. Zap the process right away. |
| 415 | * Due to the bad stack signaling wouldn't work. |
| 416 | */ |
| 417 | asmlinkage void bad_stack(void) |
| 418 | { |
| 419 | do_exit(SIGSEGV); |
| 420 | } |