Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/sh/kernel/sys_sh.c |
| 3 | * |
| 4 | * This file contains various random system calls that |
| 5 | * have a non-standard calling sequence on the Linux/SuperH |
| 6 | * platform. |
| 7 | * |
| 8 | * Taken from i386 version. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/mm.h> |
| 14 | #include <linux/smp.h> |
| 15 | #include <linux/smp_lock.h> |
| 16 | #include <linux/sem.h> |
| 17 | #include <linux/msg.h> |
| 18 | #include <linux/shm.h> |
| 19 | #include <linux/stat.h> |
| 20 | #include <linux/syscalls.h> |
| 21 | #include <linux/mman.h> |
| 22 | #include <linux/file.h> |
| 23 | #include <linux/utsname.h> |
Paul Mundt | f3c2575 | 2006-09-27 18:36:17 +0900 | [diff] [blame] | 24 | #include <linux/module.h> |
Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 25 | #include <asm/cacheflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <asm/uaccess.h> |
| 27 | #include <asm/ipc.h> |
| 28 | |
| 29 | /* |
| 30 | * sys_pipe() is the normal C calling standard for creating |
| 31 | * a pipe. It's not the way Unix traditionally does this, though. |
| 32 | */ |
| 33 | asmlinkage int sys_pipe(unsigned long r4, unsigned long r5, |
| 34 | unsigned long r6, unsigned long r7, |
| 35 | struct pt_regs regs) |
| 36 | { |
| 37 | int fd[2]; |
| 38 | int error; |
| 39 | |
| 40 | error = do_pipe(fd); |
| 41 | if (!error) { |
| 42 | regs.regs[1] = fd[1]; |
| 43 | return fd[0]; |
| 44 | } |
| 45 | return error; |
| 46 | } |
| 47 | |
Paul Mundt | f3c2575 | 2006-09-27 18:36:17 +0900 | [diff] [blame] | 48 | unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */ |
| 49 | |
| 50 | EXPORT_SYMBOL(shm_align_mask); |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | /* |
Paul Mundt | f3c2575 | 2006-09-27 18:36:17 +0900 | [diff] [blame] | 53 | * To avoid cache aliases, we map the shared page with same color. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | */ |
Paul Mundt | f3c2575 | 2006-09-27 18:36:17 +0900 | [diff] [blame] | 55 | #define COLOUR_ALIGN(addr, pgoff) \ |
| 56 | ((((addr) + shm_align_mask) & ~shm_align_mask) + \ |
| 57 | (((pgoff) << PAGE_SHIFT) & shm_align_mask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, |
| 60 | unsigned long len, unsigned long pgoff, unsigned long flags) |
| 61 | { |
| 62 | struct mm_struct *mm = current->mm; |
| 63 | struct vm_area_struct *vma; |
| 64 | unsigned long start_addr; |
Paul Mundt | f3c2575 | 2006-09-27 18:36:17 +0900 | [diff] [blame] | 65 | int do_colour_align; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | if (flags & MAP_FIXED) { |
| 68 | /* We do not accept a shared mapping if it would violate |
| 69 | * cache aliasing constraints. |
| 70 | */ |
Paul Mundt | f3c2575 | 2006-09-27 18:36:17 +0900 | [diff] [blame] | 71 | if ((flags & MAP_SHARED) && (addr & shm_align_mask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | return -EINVAL; |
| 73 | return addr; |
| 74 | } |
| 75 | |
Paul Mundt | f3c2575 | 2006-09-27 18:36:17 +0900 | [diff] [blame] | 76 | if (unlikely(len > TASK_SIZE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | return -ENOMEM; |
| 78 | |
Paul Mundt | f3c2575 | 2006-09-27 18:36:17 +0900 | [diff] [blame] | 79 | do_colour_align = 0; |
| 80 | if (filp || (flags & MAP_SHARED)) |
| 81 | do_colour_align = 1; |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | if (addr) { |
Paul Mundt | f3c2575 | 2006-09-27 18:36:17 +0900 | [diff] [blame] | 84 | if (do_colour_align) |
| 85 | addr = COLOUR_ALIGN(addr, pgoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | else |
Paul Mundt | f3c2575 | 2006-09-27 18:36:17 +0900 | [diff] [blame] | 87 | addr = PAGE_ALIGN(addr); |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | vma = find_vma(mm, addr); |
| 90 | if (TASK_SIZE - len >= addr && |
| 91 | (!vma || addr + len <= vma->vm_start)) |
| 92 | return addr; |
| 93 | } |
Paul Mundt | f3c2575 | 2006-09-27 18:36:17 +0900 | [diff] [blame] | 94 | |
| 95 | if (len > mm->cached_hole_size) { |
| 96 | start_addr = addr = mm->free_area_cache; |
| 97 | } else { |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 98 | mm->cached_hole_size = 0; |
Paul Mundt | f3c2575 | 2006-09-27 18:36:17 +0900 | [diff] [blame] | 99 | start_addr = addr = TASK_UNMAPPED_BASE; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 100 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | full_search: |
Paul Mundt | f3c2575 | 2006-09-27 18:36:17 +0900 | [diff] [blame] | 103 | if (do_colour_align) |
| 104 | addr = COLOUR_ALIGN(addr, pgoff); |
| 105 | else |
| 106 | addr = PAGE_ALIGN(mm->free_area_cache); |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | for (vma = find_vma(mm, addr); ; vma = vma->vm_next) { |
| 109 | /* At this point: (!vma || addr < vma->vm_end). */ |
Paul Mundt | f3c2575 | 2006-09-27 18:36:17 +0900 | [diff] [blame] | 110 | if (unlikely(TASK_SIZE - len < addr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | /* |
| 112 | * Start a new search - just in case we missed |
| 113 | * some holes. |
| 114 | */ |
| 115 | if (start_addr != TASK_UNMAPPED_BASE) { |
| 116 | start_addr = addr = TASK_UNMAPPED_BASE; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 117 | mm->cached_hole_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | goto full_search; |
| 119 | } |
| 120 | return -ENOMEM; |
| 121 | } |
Paul Mundt | f3c2575 | 2006-09-27 18:36:17 +0900 | [diff] [blame] | 122 | if (likely(!vma || addr + len <= vma->vm_start)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | /* |
| 124 | * Remember the place where we stopped the search: |
| 125 | */ |
| 126 | mm->free_area_cache = addr + len; |
| 127 | return addr; |
| 128 | } |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 129 | if (addr + mm->cached_hole_size < vma->vm_start) |
| 130 | mm->cached_hole_size = vma->vm_start - addr; |
| 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | addr = vma->vm_end; |
Paul Mundt | f3c2575 | 2006-09-27 18:36:17 +0900 | [diff] [blame] | 133 | if (do_colour_align) |
| 134 | addr = COLOUR_ALIGN(addr, pgoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | static inline long |
| 139 | do_mmap2(unsigned long addr, unsigned long len, unsigned long prot, |
| 140 | unsigned long flags, int fd, unsigned long pgoff) |
| 141 | { |
| 142 | int error = -EBADF; |
| 143 | struct file *file = NULL; |
| 144 | |
| 145 | flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE); |
| 146 | if (!(flags & MAP_ANONYMOUS)) { |
| 147 | file = fget(fd); |
| 148 | if (!file) |
| 149 | goto out; |
| 150 | } |
| 151 | |
| 152 | down_write(¤t->mm->mmap_sem); |
| 153 | error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff); |
| 154 | up_write(¤t->mm->mmap_sem); |
| 155 | |
| 156 | if (file) |
| 157 | fput(file); |
| 158 | out: |
| 159 | return error; |
| 160 | } |
| 161 | |
| 162 | asmlinkage int old_mmap(unsigned long addr, unsigned long len, |
| 163 | unsigned long prot, unsigned long flags, |
| 164 | int fd, unsigned long off) |
| 165 | { |
| 166 | if (off & ~PAGE_MASK) |
| 167 | return -EINVAL; |
| 168 | return do_mmap2(addr, len, prot, flags, fd, off>>PAGE_SHIFT); |
| 169 | } |
| 170 | |
| 171 | asmlinkage long sys_mmap2(unsigned long addr, unsigned long len, |
| 172 | unsigned long prot, unsigned long flags, |
| 173 | unsigned long fd, unsigned long pgoff) |
| 174 | { |
| 175 | return do_mmap2(addr, len, prot, flags, fd, pgoff); |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * sys_ipc() is the de-multiplexer for the SysV IPC calls.. |
| 180 | * |
| 181 | * This is really horribly ugly. |
| 182 | */ |
| 183 | asmlinkage int sys_ipc(uint call, int first, int second, |
| 184 | int third, void __user *ptr, long fifth) |
| 185 | { |
| 186 | int version, ret; |
| 187 | |
| 188 | version = call >> 16; /* hack for backward compatibility */ |
| 189 | call &= 0xffff; |
| 190 | |
| 191 | if (call <= SEMCTL) |
| 192 | switch (call) { |
| 193 | case SEMOP: |
| 194 | return sys_semtimedop(first, (struct sembuf __user *)ptr, |
| 195 | second, NULL); |
| 196 | case SEMTIMEDOP: |
| 197 | return sys_semtimedop(first, (struct sembuf __user *)ptr, |
| 198 | second, |
| 199 | (const struct timespec __user *)fifth); |
| 200 | case SEMGET: |
| 201 | return sys_semget (first, second, third); |
| 202 | case SEMCTL: { |
| 203 | union semun fourth; |
| 204 | if (!ptr) |
| 205 | return -EINVAL; |
| 206 | if (get_user(fourth.__pad, (void * __user *) ptr)) |
| 207 | return -EFAULT; |
| 208 | return sys_semctl (first, second, third, fourth); |
| 209 | } |
| 210 | default: |
| 211 | return -EINVAL; |
| 212 | } |
| 213 | |
| 214 | if (call <= MSGCTL) |
| 215 | switch (call) { |
| 216 | case MSGSND: |
| 217 | return sys_msgsnd (first, (struct msgbuf __user *) ptr, |
| 218 | second, third); |
| 219 | case MSGRCV: |
| 220 | switch (version) { |
| 221 | case 0: { |
| 222 | struct ipc_kludge tmp; |
| 223 | if (!ptr) |
| 224 | return -EINVAL; |
| 225 | |
| 226 | if (copy_from_user(&tmp, |
| 227 | (struct ipc_kludge __user *) ptr, |
| 228 | sizeof (tmp))) |
| 229 | return -EFAULT; |
| 230 | return sys_msgrcv (first, tmp.msgp, second, |
| 231 | tmp.msgtyp, third); |
| 232 | } |
| 233 | default: |
| 234 | return sys_msgrcv (first, |
| 235 | (struct msgbuf __user *) ptr, |
| 236 | second, fifth, third); |
| 237 | } |
| 238 | case MSGGET: |
| 239 | return sys_msgget ((key_t) first, second); |
| 240 | case MSGCTL: |
| 241 | return sys_msgctl (first, second, |
| 242 | (struct msqid_ds __user *) ptr); |
| 243 | default: |
| 244 | return -EINVAL; |
| 245 | } |
| 246 | if (call <= SHMCTL) |
| 247 | switch (call) { |
| 248 | case SHMAT: |
| 249 | switch (version) { |
| 250 | default: { |
| 251 | ulong raddr; |
| 252 | ret = do_shmat (first, (char __user *) ptr, |
| 253 | second, &raddr); |
| 254 | if (ret) |
| 255 | return ret; |
| 256 | return put_user (raddr, (ulong __user *) third); |
| 257 | } |
| 258 | case 1: /* iBCS2 emulator entry point */ |
| 259 | if (!segment_eq(get_fs(), get_ds())) |
| 260 | return -EINVAL; |
| 261 | return do_shmat (first, (char __user *) ptr, |
| 262 | second, (ulong *) third); |
| 263 | } |
| 264 | case SHMDT: |
| 265 | return sys_shmdt ((char __user *)ptr); |
| 266 | case SHMGET: |
| 267 | return sys_shmget (first, second, third); |
| 268 | case SHMCTL: |
| 269 | return sys_shmctl (first, second, |
| 270 | (struct shmid_ds __user *) ptr); |
| 271 | default: |
| 272 | return -EINVAL; |
| 273 | } |
| 274 | |
| 275 | return -EINVAL; |
| 276 | } |
| 277 | |
| 278 | asmlinkage int sys_uname(struct old_utsname * name) |
| 279 | { |
| 280 | int err; |
| 281 | if (!name) |
| 282 | return -EFAULT; |
| 283 | down_read(&uts_sem); |
Serge E. Hallyn | e9ff399 | 2006-10-02 02:18:11 -0700 | [diff] [blame^] | 284 | err = copy_to_user(name, utsname(), sizeof (*name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | up_read(&uts_sem); |
| 286 | return err?-EFAULT:0; |
| 287 | } |
| 288 | |
| 289 | asmlinkage ssize_t sys_pread_wrapper(unsigned int fd, char * buf, |
| 290 | size_t count, long dummy, loff_t pos) |
| 291 | { |
| 292 | return sys_pread64(fd, buf, count, pos); |
| 293 | } |
| 294 | |
| 295 | asmlinkage ssize_t sys_pwrite_wrapper(unsigned int fd, const char * buf, |
| 296 | size_t count, long dummy, loff_t pos) |
| 297 | { |
| 298 | return sys_pwrite64(fd, buf, count, pos); |
| 299 | } |
| 300 | |
| 301 | asmlinkage int sys_fadvise64_64_wrapper(int fd, u32 offset0, u32 offset1, |
| 302 | u32 len0, u32 len1, int advice) |
| 303 | { |
| 304 | #ifdef __LITTLE_ENDIAN__ |
| 305 | return sys_fadvise64_64(fd, (u64)offset1 << 32 | offset0, |
| 306 | (u64)len1 << 32 | len0, advice); |
| 307 | #else |
| 308 | return sys_fadvise64_64(fd, (u64)offset0 << 32 | offset1, |
| 309 | (u64)len0 << 32 | len1, advice); |
| 310 | #endif |
| 311 | } |