1 /* 2 * linux/arch/arm/kernel/sys_arm.c 3 * 4 * Copyright (C) People who wrote linux/arch/i386/kernel/sys_i386.c 5 * Copyright (C) 1995, 1996 Russell King. 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 * 11 * This file contains various random system calls that 12 * have a non-standard calling sequence on the Linux/arm 13 * platform. 14 */ 15 #include <linux/module.h> 16 #include <linux/errno.h> 17 #include <linux/sched.h> 18 #include <linux/slab.h> 19 #include <linux/mm.h> 20 #include <linux/sem.h> 21 #include <linux/msg.h> 22 #include <linux/shm.h> 23 #include <linux/stat.h> 24 #include <linux/syscalls.h> 25 #include <linux/mman.h> 26 #include <linux/fs.h> 27 #include <linux/file.h> 28 #include <linux/utsname.h> 29 #include <linux/ipc.h> 30 31 #include <asm/uaccess.h> 32 33 extern unsigned long do_mremap(unsigned long addr, unsigned long old_len, 34 unsigned long new_len, unsigned long flags, 35 unsigned long new_addr); 36 37 /* common code for old and new mmaps */ 38 inline long do_mmap2( 39 unsigned long addr, unsigned long len, 40 unsigned long prot, unsigned long flags, 41 unsigned long fd, unsigned long pgoff) 42 { 43 int error = -EINVAL; 44 struct file * file = NULL; 45 46 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE); 47 48 if (flags & MAP_FIXED && addr < FIRST_USER_ADDRESS) 49 goto out; 50 51 error = -EBADF; 52 if (!(flags & MAP_ANONYMOUS)) { 53 file = fget(fd); 54 if (!file) 55 goto out; 56 } 57 58 down_write(¤t->mm->mmap_sem); 59 error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff); 60 up_write(¤t->mm->mmap_sem); 61 62 if (file) 63 fput(file); 64 out: 65 return error; 66 } 67 68 struct mmap_arg_struct { 69 unsigned long addr; 70 unsigned long len; 71 unsigned long prot; 72 unsigned long flags; 73 unsigned long fd; 74 unsigned long offset; 75 }; 76 77 asmlinkage int old_mmap(struct mmap_arg_struct __user *arg) 78 { 79 int error = -EFAULT; 80 struct mmap_arg_struct a; 81 82 if (copy_from_user(&a, arg, sizeof(a))) 83 goto out; 84 85 error = -EINVAL; 86 if (a.offset & ~PAGE_MASK) 87 goto out; 88 89 error = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset >> PAGE_SHIFT); 90 out: 91 return error; 92 } 93 94 asmlinkage unsigned long 95 sys_arm_mremap(unsigned long addr, unsigned long old_len, 96 unsigned long new_len, unsigned long flags, 97 unsigned long new_addr) 98 { 99 unsigned long ret = -EINVAL; 100 101 if (flags & MREMAP_FIXED && new_addr < FIRST_USER_ADDRESS) 102 goto out; 103 104 down_write(¤t->mm->mmap_sem); 105 ret = do_mremap(addr, old_len, new_len, flags, new_addr); 106 up_write(¤t->mm->mmap_sem); 107 108 out: 109 return ret; 110 } 111 112 /* 113 * Perform the select(nd, in, out, ex, tv) and mmap() system 114 * calls. 115 */ 116 117 struct sel_arg_struct { 118 unsigned long n; 119 fd_set __user *inp, *outp, *exp; 120 struct timeval __user *tvp; 121 }; 122 123 asmlinkage int old_select(struct sel_arg_struct __user *arg) 124 { 125 struct sel_arg_struct a; 126 127 if (copy_from_user(&a, arg, sizeof(a))) 128 return -EFAULT; 129 /* sys_select() does the appropriate kernel locking */ 130 return sys_select(a.n, a.inp, a.outp, a.exp, a.tvp); 131 } 132 133 #if !defined(CONFIG_AEABI) || defined(CONFIG_OABI_COMPAT) 134 /* 135 * sys_ipc() is the de-multiplexer for the SysV IPC calls.. 136 * 137 * This is really horribly ugly. 138 */ 139 asmlinkage int sys_ipc(uint call, int first, int second, int third, 140 void __user *ptr, long fifth) 141 { 142 int version, ret; 143 144 version = call >> 16; /* hack for backward compatibility */ 145 call &= 0xffff; 146 147 switch (call) { 148 case SEMOP: 149 return sys_semtimedop (first, (struct sembuf __user *)ptr, second, NULL); 150 case SEMTIMEDOP: 151 return sys_semtimedop(first, (struct sembuf __user *)ptr, second, 152 (const struct timespec __user *)fifth); 153 154 case SEMGET: 155 return sys_semget (first, second, third); 156 case SEMCTL: { 157 union semun fourth; 158 if (!ptr) 159 return -EINVAL; 160 if (get_user(fourth.__pad, (void __user * __user *) ptr)) 161 return -EFAULT; 162 return sys_semctl (first, second, third, fourth); 163 } 164 165 case MSGSND: 166 return sys_msgsnd(first, (struct msgbuf __user *) ptr, 167 second, third); 168 case MSGRCV: 169 switch (version) { 170 case 0: { 171 struct ipc_kludge tmp; 172 if (!ptr) 173 return -EINVAL; 174 if (copy_from_user(&tmp,(struct ipc_kludge __user *)ptr, 175 sizeof (tmp))) 176 return -EFAULT; 177 return sys_msgrcv (first, tmp.msgp, second, 178 tmp.msgtyp, third); 179 } 180 default: 181 return sys_msgrcv (first, 182 (struct msgbuf __user *) ptr, 183 second, fifth, third); 184 } 185 case MSGGET: 186 return sys_msgget ((key_t) first, second); 187 case MSGCTL: 188 return sys_msgctl(first, second, (struct msqid_ds __user *)ptr); 189 190 case SHMAT: 191 switch (version) { 192 default: { 193 ulong raddr; 194 ret = do_shmat(first, (char __user *)ptr, second, &raddr); 195 if (ret) 196 return ret; 197 return put_user(raddr, (ulong __user *)third); 198 } 199 case 1: /* Of course, we don't support iBCS2! */ 200 return -EINVAL; 201 } 202 case SHMDT: 203 return sys_shmdt ((char __user *)ptr); 204 case SHMGET: 205 return sys_shmget (first, second, third); 206 case SHMCTL: 207 return sys_shmctl (first, second, 208 (struct shmid_ds __user *) ptr); 209 default: 210 return -ENOSYS; 211 } 212 } 213 #endif 214 215 /* Fork a new task - this creates a new program thread. 216 * This is called indirectly via a small wrapper 217 */ 218 asmlinkage int sys_fork(struct pt_regs *regs) 219 { 220 #ifdef CONFIG_MMU 221 return do_fork(SIGCHLD, regs->ARM_sp, regs, 0, NULL, NULL); 222 #else 223 /* can not support in nommu mode */ 224 return(-EINVAL); 225 #endif 226 } 227 228 /* Clone a task - this clones the calling program thread. 229 * This is called indirectly via a small wrapper 230 */ 231 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp, 232 int __user *parent_tidptr, int tls_val, 233 int __user *child_tidptr, struct pt_regs *regs) 234 { 235 if (!newsp) 236 newsp = regs->ARM_sp; 237 238 return do_fork(clone_flags, newsp, regs, 0, parent_tidptr, child_tidptr); 239 } 240 241 asmlinkage int sys_vfork(struct pt_regs *regs) 242 { 243 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->ARM_sp, regs, 0, NULL, NULL); 244 } 245 246 /* sys_execve() executes a new program. 247 * This is called indirectly via a small wrapper 248 */ 249 asmlinkage int sys_execve(char __user *filenamei, char __user * __user *argv, 250 char __user * __user *envp, struct pt_regs *regs) 251 { 252 int error; 253 char * filename; 254 255 filename = getname(filenamei); 256 error = PTR_ERR(filename); 257 if (IS_ERR(filename)) 258 goto out; 259 error = do_execve(filename, argv, envp, regs); 260 putname(filename); 261 out: 262 return error; 263 } 264 265 int kernel_execve(const char *filename, char *const argv[], char *const envp[]) 266 { 267 struct pt_regs regs; 268 int ret; 269 270 memset(®s, 0, sizeof(struct pt_regs)); 271 ret = do_execve((char *)filename, (char __user * __user *)argv, 272 (char __user * __user *)envp, ®s); 273 if (ret < 0) 274 goto out; 275 276 /* 277 * Save argc to the register structure for userspace. 278 */ 279 regs.ARM_r0 = ret; 280 281 /* 282 * We were successful. We won't be returning to our caller, but 283 * instead to user space by manipulating the kernel stack. 284 */ 285 asm( "add r0, %0, %1\n\t" 286 "mov r1, %2\n\t" 287 "mov r2, %3\n\t" 288 "bl memmove\n\t" /* copy regs to top of stack */ 289 "mov r8, #0\n\t" /* not a syscall */ 290 "mov r9, %0\n\t" /* thread structure */ 291 "mov sp, r0\n\t" /* reposition stack pointer */ 292 "b ret_to_user" 293 : 294 : "r" (current_thread_info()), 295 "Ir" (THREAD_START_SP - sizeof(regs)), 296 "r" (®s), 297 "Ir" (sizeof(regs)) 298 : "r0", "r1", "r2", "r3", "ip", "lr", "memory"); 299 300 out: 301 return ret; 302 } 303 EXPORT_SYMBOL(kernel_execve); 304 305 /* 306 * Since loff_t is a 64 bit type we avoid a lot of ABI hassle 307 * with a different argument ordering. 308 */ 309 asmlinkage long sys_arm_fadvise64_64(int fd, int advice, 310 loff_t offset, loff_t len) 311 { 312 return sys_fadvise64_64(fd, offset, len, advice); 313 } 314