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/capability.h> 12 #include <linux/errno.h> 13 #include <linux/linkage.h> 14 #include <linux/mm.h> 15 #include <linux/fs.h> 16 #include <linux/smp.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> 30 #include <linux/module.h> 31 #include <linux/ipc.h> 32 33 #include <asm/branch.h> 34 #include <asm/cachectl.h> 35 #include <asm/cacheflush.h> 36 #include <asm/asm-offsets.h> 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 /* 44 * For historic reasons the pipe(2) syscall on MIPS has an unusual calling 45 * convention. It returns results in registers $v0 / $v1 which means there 46 * is no need for it to do verify the validity of a userspace pointer 47 * argument. Historically that used to be expensive in Linux. These days 48 * the performance advantage is negligible. 49 */ 50 asmlinkage int sysm_pipe(nabi_no_regargs volatile struct pt_regs regs) 51 { 52 int fd[2]; 53 int error, res; 54 55 error = do_pipe_flags(fd, 0); 56 if (error) { 57 res = error; 58 goto out; 59 } 60 regs.regs[3] = fd[1]; 61 res = fd[0]; 62 out: 63 return res; 64 } 65 66 unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */ 67 68 EXPORT_SYMBOL(shm_align_mask); 69 70 #define COLOUR_ALIGN(addr,pgoff) \ 71 ((((addr) + shm_align_mask) & ~shm_align_mask) + \ 72 (((pgoff) << PAGE_SHIFT) & shm_align_mask)) 73 74 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, 75 unsigned long len, unsigned long pgoff, unsigned long flags) 76 { 77 struct vm_area_struct * vmm; 78 int do_color_align; 79 unsigned long task_size; 80 81 task_size = STACK_TOP; 82 83 if (len > task_size) 84 return -ENOMEM; 85 86 if (flags & MAP_FIXED) { 87 /* Even MAP_FIXED mappings must reside within task_size. */ 88 if (task_size - len < addr) 89 return -EINVAL; 90 91 /* 92 * We do not accept a shared mapping if it would violate 93 * cache aliasing constraints. 94 */ 95 if ((flags & MAP_SHARED) && (addr & shm_align_mask)) 96 return -EINVAL; 97 return addr; 98 } 99 100 do_color_align = 0; 101 if (filp || (flags & MAP_SHARED)) 102 do_color_align = 1; 103 if (addr) { 104 if (do_color_align) 105 addr = COLOUR_ALIGN(addr, pgoff); 106 else 107 addr = PAGE_ALIGN(addr); 108 vmm = find_vma(current->mm, addr); 109 if (task_size - len >= addr && 110 (!vmm || addr + len <= vmm->vm_start)) 111 return addr; 112 } 113 addr = TASK_UNMAPPED_BASE; 114 if (do_color_align) 115 addr = COLOUR_ALIGN(addr, pgoff); 116 else 117 addr = PAGE_ALIGN(addr); 118 119 for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) { 120 /* At this point: (!vmm || addr < vmm->vm_end). */ 121 if (task_size - len < addr) 122 return -ENOMEM; 123 if (!vmm || addr + len <= vmm->vm_start) 124 return addr; 125 addr = vmm->vm_end; 126 if (do_color_align) 127 addr = COLOUR_ALIGN(addr, pgoff); 128 } 129 } 130 131 /* common code for old and new mmaps */ 132 static inline unsigned long 133 do_mmap2(unsigned long addr, unsigned long len, unsigned long prot, 134 unsigned long flags, unsigned long fd, unsigned long pgoff) 135 { 136 unsigned long error = -EBADF; 137 struct file * file = NULL; 138 139 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE); 140 if (!(flags & MAP_ANONYMOUS)) { 141 file = fget(fd); 142 if (!file) 143 goto out; 144 } 145 146 down_write(¤t->mm->mmap_sem); 147 error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff); 148 up_write(¤t->mm->mmap_sem); 149 150 if (file) 151 fput(file); 152 out: 153 return error; 154 } 155 156 asmlinkage unsigned long 157 old_mmap(unsigned long addr, unsigned long len, int prot, 158 int flags, int fd, off_t offset) 159 { 160 unsigned long result; 161 162 result = -EINVAL; 163 if (offset & ~PAGE_MASK) 164 goto out; 165 166 result = do_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT); 167 168 out: 169 return result; 170 } 171 172 asmlinkage unsigned long 173 sys_mmap2(unsigned long addr, unsigned long len, unsigned long prot, 174 unsigned long flags, unsigned long fd, unsigned long pgoff) 175 { 176 if (pgoff & (~PAGE_MASK >> 12)) 177 return -EINVAL; 178 179 return do_mmap2(addr, len, prot, flags, fd, pgoff >> (PAGE_SHIFT-12)); 180 } 181 182 save_static_function(sys_fork); 183 static int __used noinline 184 _sys_fork(nabi_no_regargs struct pt_regs regs) 185 { 186 return do_fork(SIGCHLD, regs.regs[29], ®s, 0, NULL, NULL); 187 } 188 189 save_static_function(sys_clone); 190 static int __used noinline 191 _sys_clone(nabi_no_regargs struct pt_regs regs) 192 { 193 unsigned long clone_flags; 194 unsigned long newsp; 195 int __user *parent_tidptr, *child_tidptr; 196 197 clone_flags = regs.regs[4]; 198 newsp = regs.regs[5]; 199 if (!newsp) 200 newsp = regs.regs[29]; 201 parent_tidptr = (int __user *) regs.regs[6]; 202 #ifdef CONFIG_32BIT 203 /* We need to fetch the fifth argument off the stack. */ 204 child_tidptr = NULL; 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])) 209 return -EFAULT; 210 } 211 else if (get_user (child_tidptr, &usp[4])) 212 return -EFAULT; 213 } 214 #else 215 child_tidptr = (int __user *) regs.regs[8]; 216 #endif 217 return do_fork(clone_flags, newsp, ®s, 0, 218 parent_tidptr, child_tidptr); 219 } 220 221 /* 222 * sys_execve() executes a new program. 223 */ 224 asmlinkage int sys_execve(nabi_no_regargs struct pt_regs regs) 225 { 226 int error; 227 char * filename; 228 229 filename = getname((char __user *) (long)regs.regs[4]); 230 error = PTR_ERR(filename); 231 if (IS_ERR(filename)) 232 goto out; 233 error = do_execve(filename, (char __user *__user *) (long)regs.regs[5], 234 (char __user *__user *) (long)regs.regs[6], ®s); 235 putname(filename); 236 237 out: 238 return error; 239 } 240 241 /* 242 * Compacrapability ... 243 */ 244 asmlinkage int sys_uname(struct old_utsname __user * name) 245 { 246 if (name && !copy_to_user(name, utsname(), sizeof (*name))) 247 return 0; 248 return -EFAULT; 249 } 250 251 /* 252 * Compacrapability ... 253 */ 254 asmlinkage int sys_olduname(struct oldold_utsname __user * name) 255 { 256 int error; 257 258 if (!name) 259 return -EFAULT; 260 if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname))) 261 return -EFAULT; 262 263 error = __copy_to_user(&name->sysname, &utsname()->sysname, 264 __OLD_UTS_LEN); 265 error -= __put_user(0, name->sysname + __OLD_UTS_LEN); 266 error -= __copy_to_user(&name->nodename, &utsname()->nodename, 267 __OLD_UTS_LEN); 268 error -= __put_user(0, name->nodename + __OLD_UTS_LEN); 269 error -= __copy_to_user(&name->release, &utsname()->release, 270 __OLD_UTS_LEN); 271 error -= __put_user(0, name->release + __OLD_UTS_LEN); 272 error -= __copy_to_user(&name->version, &utsname()->version, 273 __OLD_UTS_LEN); 274 error -= __put_user(0, name->version + __OLD_UTS_LEN); 275 error -= __copy_to_user(&name->machine, &utsname()->machine, 276 __OLD_UTS_LEN); 277 error = __put_user(0, name->machine + __OLD_UTS_LEN); 278 error = error ? -EFAULT : 0; 279 280 return error; 281 } 282 283 asmlinkage int sys_set_thread_area(unsigned long addr) 284 { 285 struct thread_info *ti = task_thread_info(current); 286 287 ti->tp_value = addr; 288 if (cpu_has_userlocal) 289 write_c0_userlocal(addr); 290 291 return 0; 292 } 293 294 asmlinkage int _sys_sysmips(int cmd, long arg1, int arg2, int arg3) 295 { 296 switch (cmd) { 297 case MIPS_ATOMIC_SET: 298 printk(KERN_CRIT "How did I get here?\n"); 299 return -EINVAL; 300 301 case MIPS_FIXADE: 302 if (arg1 & ~3) 303 return -EINVAL; 304 305 if (arg1 & 1) 306 set_thread_flag(TIF_FIXADE); 307 else 308 clear_thread_flag(TIF_FIXADE); 309 if (arg1 & 2) 310 set_thread_flag(TIF_LOGADE); 311 else 312 clear_thread_flag(TIF_FIXADE); 313 314 return 0; 315 316 case FLUSH_CACHE: 317 __flush_cache_all(); 318 return 0; 319 } 320 321 return -EINVAL; 322 } 323 324 /* 325 * sys_ipc() is the de-multiplexer for the SysV IPC calls.. 326 * 327 * This is really horribly ugly. 328 */ 329 asmlinkage int sys_ipc(unsigned int call, int first, int second, 330 unsigned long third, void __user *ptr, long fifth) 331 { 332 int version, ret; 333 334 version = call >> 16; /* hack for backward compatibility */ 335 call &= 0xffff; 336 337 switch (call) { 338 case SEMOP: 339 return sys_semtimedop(first, (struct sembuf __user *)ptr, 340 second, NULL); 341 case SEMTIMEDOP: 342 return sys_semtimedop(first, (struct sembuf __user *)ptr, 343 second, 344 (const struct timespec __user *)fifth); 345 case SEMGET: 346 return sys_semget(first, second, third); 347 case SEMCTL: { 348 union semun fourth; 349 if (!ptr) 350 return -EINVAL; 351 if (get_user(fourth.__pad, (void __user *__user *) ptr)) 352 return -EFAULT; 353 return sys_semctl(first, second, third, fourth); 354 } 355 356 case MSGSND: 357 return sys_msgsnd(first, (struct msgbuf __user *) ptr, 358 second, third); 359 case MSGRCV: 360 switch (version) { 361 case 0: { 362 struct ipc_kludge tmp; 363 if (!ptr) 364 return -EINVAL; 365 366 if (copy_from_user(&tmp, 367 (struct ipc_kludge __user *) ptr, 368 sizeof(tmp))) 369 return -EFAULT; 370 return sys_msgrcv(first, tmp.msgp, second, 371 tmp.msgtyp, third); 372 } 373 default: 374 return sys_msgrcv(first, 375 (struct msgbuf __user *) ptr, 376 second, fifth, third); 377 } 378 case MSGGET: 379 return sys_msgget((key_t) first, second); 380 case MSGCTL: 381 return sys_msgctl(first, second, 382 (struct msqid_ds __user *) ptr); 383 384 case SHMAT: 385 switch (version) { 386 default: { 387 unsigned long raddr; 388 ret = do_shmat(first, (char __user *) ptr, second, 389 &raddr); 390 if (ret) 391 return ret; 392 return put_user(raddr, (unsigned long __user *) third); 393 } 394 case 1: /* iBCS2 emulator entry point */ 395 if (!segment_eq(get_fs(), get_ds())) 396 return -EINVAL; 397 return do_shmat(first, (char __user *) ptr, second, 398 (unsigned long *) third); 399 } 400 case SHMDT: 401 return sys_shmdt((char __user *)ptr); 402 case SHMGET: 403 return sys_shmget(first, second, third); 404 case SHMCTL: 405 return sys_shmctl(first, second, 406 (struct shmid_ds __user *) ptr); 407 default: 408 return -ENOSYS; 409 } 410 } 411 412 /* 413 * No implemented yet ... 414 */ 415 asmlinkage int sys_cachectl(char *addr, int nbytes, int op) 416 { 417 return -ENOSYS; 418 } 419 420 /* 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. 423 */ 424 asmlinkage void bad_stack(void) 425 { 426 do_exit(SIGSEGV); 427 } 428 429 /* 430 * Do a system call from kernel instead of calling sys_execve so we 431 * end up with proper pt_regs. 432 */ 433 int kernel_execve(const char *filename, char *const argv[], char *const envp[]) 434 { 435 register unsigned long __a0 asm("$4") = (unsigned long) filename; 436 register unsigned long __a1 asm("$5") = (unsigned long) argv; 437 register unsigned long __a2 asm("$6") = (unsigned long) envp; 438 register unsigned long __a3 asm("$7"); 439 unsigned long __v0; 440 441 __asm__ volatile (" \n" 442 " .set noreorder \n" 443 " li $2, %5 # __NR_execve \n" 444 " syscall \n" 445 " move %0, $2 \n" 446 " .set reorder \n" 447 : "=&r" (__v0), "=r" (__a3) 448 : "r" (__a0), "r" (__a1), "r" (__a2), "i" (__NR_execve) 449 : "$2", "$8", "$9", "$10", "$11", "$12", "$13", "$14", "$15", "$24", 450 "memory"); 451 452 if (__a3 == 0) 453 return __v0; 454 455 return -__v0; 456 } 457