blob: 59cd2859ce9b247e70c27728dd029eaba0fb370f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/uaccess.h>
Arnd Bergmannfe742902006-10-02 02:18:34 -070027#include <asm/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Paul Mundtf3c25752006-09-27 18:36:17 +090029unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
Paul Mundtf3c25752006-09-27 18:36:17 +090030EXPORT_SYMBOL(shm_align_mask);
31
Paul Mundt710ee0c2006-11-05 16:48:42 +090032#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
Paul Mundtf3c25752006-09-27 18:36:17 +090034 * To avoid cache aliases, we map the shared page with same color.
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 */
Paul Mundtf3c25752006-09-27 18:36:17 +090036#define COLOUR_ALIGN(addr, pgoff) \
37 ((((addr) + shm_align_mask) & ~shm_align_mask) + \
38 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
41 unsigned long len, unsigned long pgoff, unsigned long flags)
42{
43 struct mm_struct *mm = current->mm;
44 struct vm_area_struct *vma;
45 unsigned long start_addr;
Paul Mundtf3c25752006-09-27 18:36:17 +090046 int do_colour_align;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48 if (flags & MAP_FIXED) {
49 /* We do not accept a shared mapping if it would violate
50 * cache aliasing constraints.
51 */
Paul Mundtf3c25752006-09-27 18:36:17 +090052 if ((flags & MAP_SHARED) && (addr & shm_align_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return -EINVAL;
54 return addr;
55 }
56
Paul Mundtf3c25752006-09-27 18:36:17 +090057 if (unlikely(len > TASK_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 return -ENOMEM;
59
Paul Mundtf3c25752006-09-27 18:36:17 +090060 do_colour_align = 0;
61 if (filp || (flags & MAP_SHARED))
62 do_colour_align = 1;
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (addr) {
Paul Mundtf3c25752006-09-27 18:36:17 +090065 if (do_colour_align)
66 addr = COLOUR_ALIGN(addr, pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 else
Paul Mundtf3c25752006-09-27 18:36:17 +090068 addr = PAGE_ALIGN(addr);
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 vma = find_vma(mm, addr);
71 if (TASK_SIZE - len >= addr &&
72 (!vma || addr + len <= vma->vm_start))
73 return addr;
74 }
Paul Mundtf3c25752006-09-27 18:36:17 +090075
76 if (len > mm->cached_hole_size) {
77 start_addr = addr = mm->free_area_cache;
78 } else {
Wolfgang Wander1363c3c2005-06-21 17:14:49 -070079 mm->cached_hole_size = 0;
Paul Mundtf3c25752006-09-27 18:36:17 +090080 start_addr = addr = TASK_UNMAPPED_BASE;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -070081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83full_search:
Paul Mundtf3c25752006-09-27 18:36:17 +090084 if (do_colour_align)
85 addr = COLOUR_ALIGN(addr, pgoff);
86 else
87 addr = PAGE_ALIGN(mm->free_area_cache);
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
90 /* At this point: (!vma || addr < vma->vm_end). */
Paul Mundtf3c25752006-09-27 18:36:17 +090091 if (unlikely(TASK_SIZE - len < addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 /*
93 * Start a new search - just in case we missed
94 * some holes.
95 */
96 if (start_addr != TASK_UNMAPPED_BASE) {
97 start_addr = addr = TASK_UNMAPPED_BASE;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -070098 mm->cached_hole_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 goto full_search;
100 }
101 return -ENOMEM;
102 }
Paul Mundtf3c25752006-09-27 18:36:17 +0900103 if (likely(!vma || addr + len <= vma->vm_start)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /*
105 * Remember the place where we stopped the search:
106 */
107 mm->free_area_cache = addr + len;
108 return addr;
109 }
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700110 if (addr + mm->cached_hole_size < vma->vm_start)
111 mm->cached_hole_size = vma->vm_start - addr;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 addr = vma->vm_end;
Paul Mundtf3c25752006-09-27 18:36:17 +0900114 if (do_colour_align)
115 addr = COLOUR_ALIGN(addr, pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117}
Paul Mundt710ee0c2006-11-05 16:48:42 +0900118#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120static inline long
Paul Mundtbcb28e42007-11-20 15:50:59 +0900121do_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 unsigned long flags, int fd, unsigned long pgoff)
123{
124 int error = -EBADF;
125 struct file *file = NULL;
126
127 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
128 if (!(flags & MAP_ANONYMOUS)) {
129 file = fget(fd);
130 if (!file)
131 goto out;
132 }
133
134 down_write(&current->mm->mmap_sem);
135 error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
136 up_write(&current->mm->mmap_sem);
137
138 if (file)
139 fput(file);
140out:
141 return error;
142}
143
144asmlinkage int old_mmap(unsigned long addr, unsigned long len,
145 unsigned long prot, unsigned long flags,
146 int fd, unsigned long off)
147{
148 if (off & ~PAGE_MASK)
149 return -EINVAL;
150 return do_mmap2(addr, len, prot, flags, fd, off>>PAGE_SHIFT);
151}
152
153asmlinkage long sys_mmap2(unsigned long addr, unsigned long len,
154 unsigned long prot, unsigned long flags,
155 unsigned long fd, unsigned long pgoff)
156{
157 return do_mmap2(addr, len, prot, flags, fd, pgoff);
158}
159
160/*
161 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
162 *
163 * This is really horribly ugly.
164 */
165asmlinkage int sys_ipc(uint call, int first, int second,
166 int third, void __user *ptr, long fifth)
167{
168 int version, ret;
169
170 version = call >> 16; /* hack for backward compatibility */
171 call &= 0xffff;
172
173 if (call <= SEMCTL)
174 switch (call) {
175 case SEMOP:
Paul Mundtbcb28e42007-11-20 15:50:59 +0900176 return sys_semtimedop(first,
177 (struct sembuf __user *)ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 second, NULL);
179 case SEMTIMEDOP:
Paul Mundtbcb28e42007-11-20 15:50:59 +0900180 return sys_semtimedop(first,
181 (struct sembuf __user *)ptr, second,
182 (const struct timespec __user *)fifth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 case SEMGET:
184 return sys_semget (first, second, third);
185 case SEMCTL: {
186 union semun fourth;
187 if (!ptr)
188 return -EINVAL;
189 if (get_user(fourth.__pad, (void * __user *) ptr))
190 return -EFAULT;
191 return sys_semctl (first, second, third, fourth);
192 }
193 default:
194 return -EINVAL;
195 }
196
Paul Mundtbcb28e42007-11-20 15:50:59 +0900197 if (call <= MSGCTL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 switch (call) {
199 case MSGSND:
Paul Mundtbcb28e42007-11-20 15:50:59 +0900200 return sys_msgsnd (first, (struct msgbuf __user *) ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 second, third);
202 case MSGRCV:
203 switch (version) {
Paul Mundtbcb28e42007-11-20 15:50:59 +0900204 case 0:
205 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 struct ipc_kludge tmp;
Paul Mundtbcb28e42007-11-20 15:50:59 +0900207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (!ptr)
209 return -EINVAL;
Paul Mundtbcb28e42007-11-20 15:50:59 +0900210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (copy_from_user(&tmp,
Paul Mundtbcb28e42007-11-20 15:50:59 +0900212 (struct ipc_kludge __user *) ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 sizeof (tmp)))
214 return -EFAULT;
Paul Mundtbcb28e42007-11-20 15:50:59 +0900215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return sys_msgrcv (first, tmp.msgp, second,
217 tmp.msgtyp, third);
Paul Mundtbcb28e42007-11-20 15:50:59 +0900218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 default:
220 return sys_msgrcv (first,
221 (struct msgbuf __user *) ptr,
222 second, fifth, third);
223 }
224 case MSGGET:
225 return sys_msgget ((key_t) first, second);
226 case MSGCTL:
227 return sys_msgctl (first, second,
228 (struct msqid_ds __user *) ptr);
229 default:
230 return -EINVAL;
231 }
Paul Mundtbcb28e42007-11-20 15:50:59 +0900232 if (call <= SHMCTL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 switch (call) {
234 case SHMAT:
235 switch (version) {
236 default: {
237 ulong raddr;
238 ret = do_shmat (first, (char __user *) ptr,
239 second, &raddr);
240 if (ret)
241 return ret;
242 return put_user (raddr, (ulong __user *) third);
243 }
244 case 1: /* iBCS2 emulator entry point */
245 if (!segment_eq(get_fs(), get_ds()))
246 return -EINVAL;
247 return do_shmat (first, (char __user *) ptr,
248 second, (ulong *) third);
249 }
Paul Mundtbcb28e42007-11-20 15:50:59 +0900250 case SHMDT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return sys_shmdt ((char __user *)ptr);
252 case SHMGET:
253 return sys_shmget (first, second, third);
254 case SHMCTL:
255 return sys_shmctl (first, second,
256 (struct shmid_ds __user *) ptr);
257 default:
258 return -EINVAL;
259 }
Paul Mundtbcb28e42007-11-20 15:50:59 +0900260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return -EINVAL;
262}
263
264asmlinkage int sys_uname(struct old_utsname * name)
265{
266 int err;
267 if (!name)
268 return -EFAULT;
269 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700270 err = copy_to_user(name, utsname(), sizeof (*name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 up_read(&uts_sem);
272 return err?-EFAULT:0;
273}