blob: 0dfb88925addc3b209b082da0b51e01fb58b7606 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/errno.h>
11#include <linux/sched.h>
12#include <linux/mm.h>
13#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/sem.h>
15#include <linux/msg.h>
16#include <linux/shm.h>
17#include <linux/stat.h>
18#include <linux/syscalls.h>
19#include <linux/mman.h>
20#include <linux/file.h>
21#include <linux/utsname.h>
Paul Mundtf3c25752006-09-27 18:36:17 +090022#include <linux/module.h>
Paul Mundte06c4e52007-07-31 13:01:43 +090023#include <linux/fs.h>
Adrian Bunkcba4fbb2007-10-16 23:29:24 -070024#include <linux/ipc.h>
Paul Mundt26ff6c12006-09-27 15:13:36 +090025#include <asm/cacheflush.h>
Paul Mundtfa439722008-09-04 18:53:58 +090026#include <asm/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/uaccess.h>
Arnd Bergmannfe742902006-10-02 02:18:34 -070028#include <asm/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Paul Mundtf3c25752006-09-27 18:36:17 +090030unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
Paul Mundtf3c25752006-09-27 18:36:17 +090031EXPORT_SYMBOL(shm_align_mask);
32
Paul Mundt710ee0c2006-11-05 16:48:42 +090033#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
Paul Mundtf3c25752006-09-27 18:36:17 +090035 * To avoid cache aliases, we map the shared page with same color.
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 */
Paul Mundtf3c25752006-09-27 18:36:17 +090037#define COLOUR_ALIGN(addr, pgoff) \
38 ((((addr) + shm_align_mask) & ~shm_align_mask) + \
39 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
42 unsigned long len, unsigned long pgoff, unsigned long flags)
43{
44 struct mm_struct *mm = current->mm;
45 struct vm_area_struct *vma;
46 unsigned long start_addr;
Paul Mundtf3c25752006-09-27 18:36:17 +090047 int do_colour_align;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 if (flags & MAP_FIXED) {
50 /* We do not accept a shared mapping if it would violate
51 * cache aliasing constraints.
52 */
Paul Mundtf3c25752006-09-27 18:36:17 +090053 if ((flags & MAP_SHARED) && (addr & shm_align_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 return -EINVAL;
55 return addr;
56 }
57
Paul Mundtf3c25752006-09-27 18:36:17 +090058 if (unlikely(len > TASK_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return -ENOMEM;
60
Paul Mundtf3c25752006-09-27 18:36:17 +090061 do_colour_align = 0;
62 if (filp || (flags & MAP_SHARED))
63 do_colour_align = 1;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (addr) {
Paul Mundtf3c25752006-09-27 18:36:17 +090066 if (do_colour_align)
67 addr = COLOUR_ALIGN(addr, pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 else
Paul Mundtf3c25752006-09-27 18:36:17 +090069 addr = PAGE_ALIGN(addr);
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 vma = find_vma(mm, addr);
72 if (TASK_SIZE - len >= addr &&
73 (!vma || addr + len <= vma->vm_start))
74 return addr;
75 }
Paul Mundtf3c25752006-09-27 18:36:17 +090076
77 if (len > mm->cached_hole_size) {
78 start_addr = addr = mm->free_area_cache;
79 } else {
Wolfgang Wander1363c3c2005-06-21 17:14:49 -070080 mm->cached_hole_size = 0;
Paul Mundtf3c25752006-09-27 18:36:17 +090081 start_addr = addr = TASK_UNMAPPED_BASE;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -070082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84full_search:
Paul Mundtf3c25752006-09-27 18:36:17 +090085 if (do_colour_align)
86 addr = COLOUR_ALIGN(addr, pgoff);
87 else
88 addr = PAGE_ALIGN(mm->free_area_cache);
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
91 /* At this point: (!vma || addr < vma->vm_end). */
Paul Mundtf3c25752006-09-27 18:36:17 +090092 if (unlikely(TASK_SIZE - len < addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 /*
94 * Start a new search - just in case we missed
95 * some holes.
96 */
97 if (start_addr != TASK_UNMAPPED_BASE) {
98 start_addr = addr = TASK_UNMAPPED_BASE;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -070099 mm->cached_hole_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 goto full_search;
101 }
102 return -ENOMEM;
103 }
Paul Mundtf3c25752006-09-27 18:36:17 +0900104 if (likely(!vma || addr + len <= vma->vm_start)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 /*
106 * Remember the place where we stopped the search:
107 */
108 mm->free_area_cache = addr + len;
109 return addr;
110 }
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700111 if (addr + mm->cached_hole_size < vma->vm_start)
112 mm->cached_hole_size = vma->vm_start - addr;
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 addr = vma->vm_end;
Paul Mundtf3c25752006-09-27 18:36:17 +0900115 if (do_colour_align)
116 addr = COLOUR_ALIGN(addr, pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118}
Paul Mundt710ee0c2006-11-05 16:48:42 +0900119#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121static inline long
Paul Mundtbcb28e42007-11-20 15:50:59 +0900122do_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 unsigned long flags, int fd, unsigned long pgoff)
124{
125 int error = -EBADF;
126 struct file *file = NULL;
127
128 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
129 if (!(flags & MAP_ANONYMOUS)) {
130 file = fget(fd);
131 if (!file)
132 goto out;
133 }
134
135 down_write(&current->mm->mmap_sem);
136 error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
137 up_write(&current->mm->mmap_sem);
138
139 if (file)
140 fput(file);
141out:
142 return error;
143}
144
145asmlinkage int old_mmap(unsigned long addr, unsigned long len,
146 unsigned long prot, unsigned long flags,
147 int fd, unsigned long off)
148{
149 if (off & ~PAGE_MASK)
150 return -EINVAL;
151 return do_mmap2(addr, len, prot, flags, fd, off>>PAGE_SHIFT);
152}
153
154asmlinkage long sys_mmap2(unsigned long addr, unsigned long len,
155 unsigned long prot, unsigned long flags,
156 unsigned long fd, unsigned long pgoff)
157{
158 return do_mmap2(addr, len, prot, flags, fd, pgoff);
159}
160
161/*
162 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
163 *
164 * This is really horribly ugly.
165 */
166asmlinkage int sys_ipc(uint call, int first, int second,
167 int third, void __user *ptr, long fifth)
168{
169 int version, ret;
170
171 version = call >> 16; /* hack for backward compatibility */
172 call &= 0xffff;
173
Yoshihiro Shimoda3c31bf72008-08-27 20:16:46 +0900174 if (call <= SEMTIMEDOP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 switch (call) {
176 case SEMOP:
Paul Mundtbcb28e42007-11-20 15:50:59 +0900177 return sys_semtimedop(first,
178 (struct sembuf __user *)ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 second, NULL);
180 case SEMTIMEDOP:
Paul Mundtbcb28e42007-11-20 15:50:59 +0900181 return sys_semtimedop(first,
182 (struct sembuf __user *)ptr, second,
183 (const struct timespec __user *)fifth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 case SEMGET:
185 return sys_semget (first, second, third);
186 case SEMCTL: {
187 union semun fourth;
188 if (!ptr)
189 return -EINVAL;
Paul Mundtfa439722008-09-04 18:53:58 +0900190 if (get_user(fourth.__pad, (void __user * __user *) ptr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return -EFAULT;
192 return sys_semctl (first, second, third, fourth);
193 }
194 default:
195 return -EINVAL;
196 }
197
Paul Mundtbcb28e42007-11-20 15:50:59 +0900198 if (call <= MSGCTL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 switch (call) {
200 case MSGSND:
Paul Mundtbcb28e42007-11-20 15:50:59 +0900201 return sys_msgsnd (first, (struct msgbuf __user *) ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 second, third);
203 case MSGRCV:
204 switch (version) {
Paul Mundtbcb28e42007-11-20 15:50:59 +0900205 case 0:
206 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 struct ipc_kludge tmp;
Paul Mundtbcb28e42007-11-20 15:50:59 +0900208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (!ptr)
210 return -EINVAL;
Paul Mundtbcb28e42007-11-20 15:50:59 +0900211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (copy_from_user(&tmp,
Paul Mundtbcb28e42007-11-20 15:50:59 +0900213 (struct ipc_kludge __user *) ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 sizeof (tmp)))
215 return -EFAULT;
Paul Mundtbcb28e42007-11-20 15:50:59 +0900216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return sys_msgrcv (first, tmp.msgp, second,
218 tmp.msgtyp, third);
Paul Mundtbcb28e42007-11-20 15:50:59 +0900219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 default:
221 return sys_msgrcv (first,
222 (struct msgbuf __user *) ptr,
223 second, fifth, third);
224 }
225 case MSGGET:
226 return sys_msgget ((key_t) first, second);
227 case MSGCTL:
228 return sys_msgctl (first, second,
229 (struct msqid_ds __user *) ptr);
230 default:
231 return -EINVAL;
232 }
Paul Mundtbcb28e42007-11-20 15:50:59 +0900233 if (call <= SHMCTL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 switch (call) {
235 case SHMAT:
236 switch (version) {
237 default: {
238 ulong raddr;
239 ret = do_shmat (first, (char __user *) ptr,
240 second, &raddr);
241 if (ret)
242 return ret;
243 return put_user (raddr, (ulong __user *) third);
244 }
245 case 1: /* iBCS2 emulator entry point */
246 if (!segment_eq(get_fs(), get_ds()))
247 return -EINVAL;
248 return do_shmat (first, (char __user *) ptr,
249 second, (ulong *) third);
250 }
Paul Mundtbcb28e42007-11-20 15:50:59 +0900251 case SHMDT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return sys_shmdt ((char __user *)ptr);
253 case SHMGET:
254 return sys_shmget (first, second, third);
255 case SHMCTL:
256 return sys_shmctl (first, second,
257 (struct shmid_ds __user *) ptr);
258 default:
259 return -EINVAL;
260 }
Paul Mundtbcb28e42007-11-20 15:50:59 +0900261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return -EINVAL;
263}
264
Paul Mundtfa439722008-09-04 18:53:58 +0900265asmlinkage int sys_uname(struct old_utsname __user *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 int err;
268 if (!name)
269 return -EFAULT;
270 down_read(&uts_sem);
Paul Mundtfa439722008-09-04 18:53:58 +0900271 err = copy_to_user(name, utsname(), sizeof(*name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 up_read(&uts_sem);
273 return err?-EFAULT:0;
274}