Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * This file contains various random system calls that |
| 3 | * have a non-standard calling sequence on the Linux/i386 |
| 4 | * platform. |
| 5 | */ |
| 6 | |
| 7 | #include <linux/errno.h> |
| 8 | #include <linux/sched.h> |
| 9 | #include <linux/mm.h> |
Alexey Dobriyan | 4e950f6 | 2007-07-30 02:36:13 +0400 | [diff] [blame] | 10 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/sem.h> |
| 13 | #include <linux/msg.h> |
| 14 | #include <linux/shm.h> |
| 15 | #include <linux/stat.h> |
| 16 | #include <linux/syscalls.h> |
| 17 | #include <linux/mman.h> |
| 18 | #include <linux/file.h> |
| 19 | #include <linux/utsname.h> |
Adrian Bunk | cba4fbb | 2007-10-16 23:29:24 -0700 | [diff] [blame] | 20 | #include <linux/ipc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 22 | #include <linux/uaccess.h> |
| 23 | #include <linux/unistd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Jaswinder Singh | bbc1f69 | 2008-07-21 21:34:13 +0530 | [diff] [blame] | 25 | #include <asm/syscalls.h> |
| 26 | |
Michael Buesch | a7290ee | 2006-03-22 00:08:44 -0800 | [diff] [blame] | 27 | asmlinkage long sys_mmap2(unsigned long addr, unsigned long len, |
| 28 | unsigned long prot, unsigned long flags, |
| 29 | unsigned long fd, unsigned long pgoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
| 31 | int error = -EBADF; |
Michael Buesch | a7290ee | 2006-03-22 00:08:44 -0800 | [diff] [blame] | 32 | struct file *file = NULL; |
| 33 | struct mm_struct *mm = current->mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE); |
| 36 | if (!(flags & MAP_ANONYMOUS)) { |
| 37 | file = fget(fd); |
| 38 | if (!file) |
| 39 | goto out; |
| 40 | } |
| 41 | |
Michael Buesch | a7290ee | 2006-03-22 00:08:44 -0800 | [diff] [blame] | 42 | down_write(&mm->mmap_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff); |
Michael Buesch | a7290ee | 2006-03-22 00:08:44 -0800 | [diff] [blame] | 44 | up_write(&mm->mmap_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | if (file) |
| 47 | fput(file); |
| 48 | out: |
| 49 | return error; |
| 50 | } |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | /* |
| 53 | * Perform the select(nd, in, out, ex, tv) and mmap() system |
| 54 | * calls. Linux/i386 didn't use to be able to handle more than |
| 55 | * 4 system call parameters, so these system calls used a memory |
| 56 | * block for parameter passing.. |
| 57 | */ |
| 58 | |
| 59 | struct mmap_arg_struct { |
| 60 | unsigned long addr; |
| 61 | unsigned long len; |
| 62 | unsigned long prot; |
| 63 | unsigned long flags; |
| 64 | unsigned long fd; |
| 65 | unsigned long offset; |
| 66 | }; |
| 67 | |
| 68 | asmlinkage int old_mmap(struct mmap_arg_struct __user *arg) |
| 69 | { |
| 70 | struct mmap_arg_struct a; |
| 71 | int err = -EFAULT; |
| 72 | |
| 73 | if (copy_from_user(&a, arg, sizeof(a))) |
| 74 | goto out; |
| 75 | |
| 76 | err = -EINVAL; |
| 77 | if (a.offset & ~PAGE_MASK) |
| 78 | goto out; |
| 79 | |
Michael Buesch | a7290ee | 2006-03-22 00:08:44 -0800 | [diff] [blame] | 80 | err = sys_mmap2(a.addr, a.len, a.prot, a.flags, |
| 81 | a.fd, a.offset >> PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | out: |
| 83 | return err; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | struct sel_arg_struct { |
| 88 | unsigned long n; |
| 89 | fd_set __user *inp, *outp, *exp; |
| 90 | struct timeval __user *tvp; |
| 91 | }; |
| 92 | |
| 93 | asmlinkage int old_select(struct sel_arg_struct __user *arg) |
| 94 | { |
| 95 | struct sel_arg_struct a; |
| 96 | |
| 97 | if (copy_from_user(&a, arg, sizeof(a))) |
| 98 | return -EFAULT; |
| 99 | /* sys_select() does the appropriate kernel locking */ |
| 100 | return sys_select(a.n, a.inp, a.outp, a.exp, a.tvp); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * sys_ipc() is the de-multiplexer for the SysV IPC calls.. |
| 105 | * |
| 106 | * This is really horribly ugly. |
| 107 | */ |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 108 | asmlinkage int sys_ipc(uint call, int first, int second, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | int third, void __user *ptr, long fifth) |
| 110 | { |
| 111 | int version, ret; |
| 112 | |
| 113 | version = call >> 16; /* hack for backward compatibility */ |
| 114 | call &= 0xffff; |
| 115 | |
| 116 | switch (call) { |
| 117 | case SEMOP: |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 118 | return sys_semtimedop(first, (struct sembuf __user *)ptr, second, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | case SEMTIMEDOP: |
| 120 | return sys_semtimedop(first, (struct sembuf __user *)ptr, second, |
| 121 | (const struct timespec __user *)fifth); |
| 122 | |
| 123 | case SEMGET: |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 124 | return sys_semget(first, second, third); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | case SEMCTL: { |
| 126 | union semun fourth; |
| 127 | if (!ptr) |
| 128 | return -EINVAL; |
| 129 | if (get_user(fourth.__pad, (void __user * __user *) ptr)) |
| 130 | return -EFAULT; |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 131 | return sys_semctl(first, second, third, fourth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | case MSGSND: |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 135 | return sys_msgsnd(first, (struct msgbuf __user *) ptr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | second, third); |
| 137 | case MSGRCV: |
| 138 | switch (version) { |
| 139 | case 0: { |
| 140 | struct ipc_kludge tmp; |
| 141 | if (!ptr) |
| 142 | return -EINVAL; |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | if (copy_from_user(&tmp, |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 145 | (struct ipc_kludge __user *) ptr, |
| 146 | sizeof(tmp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | return -EFAULT; |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 148 | return sys_msgrcv(first, tmp.msgp, second, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | tmp.msgtyp, third); |
| 150 | } |
| 151 | default: |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 152 | return sys_msgrcv(first, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | (struct msgbuf __user *) ptr, |
| 154 | second, fifth, third); |
| 155 | } |
| 156 | case MSGGET: |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 157 | return sys_msgget((key_t) first, second); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | case MSGCTL: |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 159 | return sys_msgctl(first, second, (struct msqid_ds __user *) ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
| 161 | case SHMAT: |
| 162 | switch (version) { |
| 163 | default: { |
| 164 | ulong raddr; |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 165 | ret = do_shmat(first, (char __user *) ptr, second, &raddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | if (ret) |
| 167 | return ret; |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 168 | return put_user(raddr, (ulong __user *) third); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | case 1: /* iBCS2 emulator entry point */ |
| 171 | if (!segment_eq(get_fs(), get_ds())) |
| 172 | return -EINVAL; |
| 173 | /* The "(ulong *) third" is valid _only_ because of the kernel segment thing */ |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 174 | return do_shmat(first, (char __user *) ptr, second, (ulong *) third); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 176 | case SHMDT: |
| 177 | return sys_shmdt((char __user *)ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | case SHMGET: |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 179 | return sys_shmget(first, second, third); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | case SHMCTL: |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 181 | return sys_shmctl(first, second, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | (struct shmid_ds __user *) ptr); |
| 183 | default: |
| 184 | return -ENOSYS; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Old cruft |
| 190 | */ |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 191 | asmlinkage int sys_uname(struct old_utsname __user *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | int err; |
| 194 | if (!name) |
| 195 | return -EFAULT; |
| 196 | down_read(&uts_sem); |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 197 | err = copy_to_user(name, utsname(), sizeof(*name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | up_read(&uts_sem); |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 199 | return err? -EFAULT:0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 202 | asmlinkage int sys_olduname(struct oldold_utsname __user *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
| 204 | int error; |
| 205 | |
| 206 | if (!name) |
| 207 | return -EFAULT; |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 208 | if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | return -EFAULT; |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 210 | |
| 211 | down_read(&uts_sem); |
| 212 | |
Serge E. Hallyn | e9ff399 | 2006-10-02 02:18:11 -0700 | [diff] [blame] | 213 | error = __copy_to_user(&name->sysname, &utsname()->sysname, |
| 214 | __OLD_UTS_LEN); |
| 215 | error |= __put_user(0, name->sysname + __OLD_UTS_LEN); |
| 216 | error |= __copy_to_user(&name->nodename, &utsname()->nodename, |
| 217 | __OLD_UTS_LEN); |
| 218 | error |= __put_user(0, name->nodename + __OLD_UTS_LEN); |
| 219 | error |= __copy_to_user(&name->release, &utsname()->release, |
| 220 | __OLD_UTS_LEN); |
| 221 | error |= __put_user(0, name->release + __OLD_UTS_LEN); |
| 222 | error |= __copy_to_user(&name->version, &utsname()->version, |
| 223 | __OLD_UTS_LEN); |
| 224 | error |= __put_user(0, name->version + __OLD_UTS_LEN); |
| 225 | error |= __copy_to_user(&name->machine, &utsname()->machine, |
| 226 | __OLD_UTS_LEN); |
| 227 | error |= __put_user(0, name->machine + __OLD_UTS_LEN); |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | up_read(&uts_sem); |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | error = error ? -EFAULT : 0; |
| 232 | |
| 233 | return error; |
| 234 | } |
Arnd Bergmann | fe74290 | 2006-10-02 02:18:34 -0700 | [diff] [blame] | 235 | |
| 236 | |
| 237 | /* |
| 238 | * Do a system call from kernel instead of calling sys_execve so we |
| 239 | * end up with proper pt_regs. |
| 240 | */ |
| 241 | int kernel_execve(const char *filename, char *const argv[], char *const envp[]) |
| 242 | { |
| 243 | long __res; |
| 244 | asm volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" |
| 245 | : "=a" (__res) |
Paolo Ciarrocchi | 5b80fe8 | 2008-06-07 14:34:42 +0200 | [diff] [blame] | 246 | : "0" (__NR_execve), "ri" (filename), "c" (argv), "d" (envp) : "memory"); |
Arnd Bergmann | fe74290 | 2006-10-02 02:18:34 -0700 | [diff] [blame] | 247 | return __res; |
| 248 | } |