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