blob: d545a686a201a9d010a616861e40f44cf1d4fdaf [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 */
10
11#include <linux/errno.h>
12#include <linux/sched.h>
13#include <linux/mm.h>
14#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sem.h>
16#include <linux/msg.h>
17#include <linux/shm.h>
18#include <linux/stat.h>
19#include <linux/syscalls.h>
20#include <linux/mman.h>
21#include <linux/file.h>
22#include <linux/utsname.h>
Paul Mundtf3c25752006-09-27 18:36:17 +090023#include <linux/module.h>
Paul Mundte06c4e52007-07-31 13:01:43 +090024#include <linux/fs.h>
Adrian Bunkcba4fbb2007-10-16 23:29:24 -070025#include <linux/ipc.h>
Paul Mundt26ff6c12006-09-27 15:13:36 +090026#include <asm/cacheflush.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
30/*
31 * sys_pipe() is the normal C calling standard for creating
32 * a pipe. It's not the way Unix traditionally does this, though.
33 */
34asmlinkage int sys_pipe(unsigned long r4, unsigned long r5,
35 unsigned long r6, unsigned long r7,
Stuart Menefyf0bc8142006-11-21 11:16:57 +090036 struct pt_regs __regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Stuart Menefyf0bc8142006-11-21 11:16:57 +090038 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 int fd[2];
40 int error;
41
42 error = do_pipe(fd);
43 if (!error) {
Stuart Menefyf0bc8142006-11-21 11:16:57 +090044 regs->regs[1] = fd[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 return fd[0];
46 }
47 return error;
48}
49
Paul Mundtf3c25752006-09-27 18:36:17 +090050unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
51
52EXPORT_SYMBOL(shm_align_mask);
53
Paul Mundt710ee0c2006-11-05 16:48:42 +090054#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/*
Paul Mundtf3c25752006-09-27 18:36:17 +090056 * To avoid cache aliases, we map the shared page with same color.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 */
Paul Mundtf3c25752006-09-27 18:36:17 +090058#define COLOUR_ALIGN(addr, pgoff) \
59 ((((addr) + shm_align_mask) & ~shm_align_mask) + \
60 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
63 unsigned long len, unsigned long pgoff, unsigned long flags)
64{
65 struct mm_struct *mm = current->mm;
66 struct vm_area_struct *vma;
67 unsigned long start_addr;
Paul Mundtf3c25752006-09-27 18:36:17 +090068 int do_colour_align;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 if (flags & MAP_FIXED) {
71 /* We do not accept a shared mapping if it would violate
72 * cache aliasing constraints.
73 */
Paul Mundtf3c25752006-09-27 18:36:17 +090074 if ((flags & MAP_SHARED) && (addr & shm_align_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return -EINVAL;
76 return addr;
77 }
78
Paul Mundtf3c25752006-09-27 18:36:17 +090079 if (unlikely(len > TASK_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -ENOMEM;
81
Paul Mundtf3c25752006-09-27 18:36:17 +090082 do_colour_align = 0;
83 if (filp || (flags & MAP_SHARED))
84 do_colour_align = 1;
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (addr) {
Paul Mundtf3c25752006-09-27 18:36:17 +090087 if (do_colour_align)
88 addr = COLOUR_ALIGN(addr, pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 else
Paul Mundtf3c25752006-09-27 18:36:17 +090090 addr = PAGE_ALIGN(addr);
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 vma = find_vma(mm, addr);
93 if (TASK_SIZE - len >= addr &&
94 (!vma || addr + len <= vma->vm_start))
95 return addr;
96 }
Paul Mundtf3c25752006-09-27 18:36:17 +090097
98 if (len > mm->cached_hole_size) {
99 start_addr = addr = mm->free_area_cache;
100 } else {
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700101 mm->cached_hole_size = 0;
Paul Mundtf3c25752006-09-27 18:36:17 +0900102 start_addr = addr = TASK_UNMAPPED_BASE;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105full_search:
Paul Mundtf3c25752006-09-27 18:36:17 +0900106 if (do_colour_align)
107 addr = COLOUR_ALIGN(addr, pgoff);
108 else
109 addr = PAGE_ALIGN(mm->free_area_cache);
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
112 /* At this point: (!vma || addr < vma->vm_end). */
Paul Mundtf3c25752006-09-27 18:36:17 +0900113 if (unlikely(TASK_SIZE - len < addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 /*
115 * Start a new search - just in case we missed
116 * some holes.
117 */
118 if (start_addr != TASK_UNMAPPED_BASE) {
119 start_addr = addr = TASK_UNMAPPED_BASE;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700120 mm->cached_hole_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 goto full_search;
122 }
123 return -ENOMEM;
124 }
Paul Mundtf3c25752006-09-27 18:36:17 +0900125 if (likely(!vma || addr + len <= vma->vm_start)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 /*
127 * Remember the place where we stopped the search:
128 */
129 mm->free_area_cache = addr + len;
130 return addr;
131 }
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700132 if (addr + mm->cached_hole_size < vma->vm_start)
133 mm->cached_hole_size = vma->vm_start - addr;
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 addr = vma->vm_end;
Paul Mundtf3c25752006-09-27 18:36:17 +0900136 if (do_colour_align)
137 addr = COLOUR_ALIGN(addr, pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139}
Paul Mundt710ee0c2006-11-05 16:48:42 +0900140#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142static inline long
143do_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
144 unsigned long flags, int fd, unsigned long pgoff)
145{
146 int error = -EBADF;
147 struct file *file = NULL;
148
149 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
150 if (!(flags & MAP_ANONYMOUS)) {
151 file = fget(fd);
152 if (!file)
153 goto out;
154 }
155
156 down_write(&current->mm->mmap_sem);
157 error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
158 up_write(&current->mm->mmap_sem);
159
160 if (file)
161 fput(file);
162out:
163 return error;
164}
165
166asmlinkage int old_mmap(unsigned long addr, unsigned long len,
167 unsigned long prot, unsigned long flags,
168 int fd, unsigned long off)
169{
170 if (off & ~PAGE_MASK)
171 return -EINVAL;
172 return do_mmap2(addr, len, prot, flags, fd, off>>PAGE_SHIFT);
173}
174
175asmlinkage long sys_mmap2(unsigned long addr, unsigned long len,
176 unsigned long prot, unsigned long flags,
177 unsigned long fd, unsigned long pgoff)
178{
179 return do_mmap2(addr, len, prot, flags, fd, pgoff);
180}
181
182/*
183 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
184 *
185 * This is really horribly ugly.
186 */
187asmlinkage int sys_ipc(uint call, int first, int second,
188 int third, void __user *ptr, long fifth)
189{
190 int version, ret;
191
192 version = call >> 16; /* hack for backward compatibility */
193 call &= 0xffff;
194
195 if (call <= SEMCTL)
196 switch (call) {
197 case SEMOP:
198 return sys_semtimedop(first, (struct sembuf __user *)ptr,
199 second, NULL);
200 case SEMTIMEDOP:
201 return sys_semtimedop(first, (struct sembuf __user *)ptr,
202 second,
203 (const struct timespec __user *)fifth);
204 case SEMGET:
205 return sys_semget (first, second, third);
206 case SEMCTL: {
207 union semun fourth;
208 if (!ptr)
209 return -EINVAL;
210 if (get_user(fourth.__pad, (void * __user *) ptr))
211 return -EFAULT;
212 return sys_semctl (first, second, third, fourth);
213 }
214 default:
215 return -EINVAL;
216 }
217
218 if (call <= MSGCTL)
219 switch (call) {
220 case MSGSND:
221 return sys_msgsnd (first, (struct msgbuf __user *) ptr,
222 second, third);
223 case MSGRCV:
224 switch (version) {
225 case 0: {
226 struct ipc_kludge tmp;
227 if (!ptr)
228 return -EINVAL;
229
230 if (copy_from_user(&tmp,
231 (struct ipc_kludge __user *) ptr,
232 sizeof (tmp)))
233 return -EFAULT;
234 return sys_msgrcv (first, tmp.msgp, second,
235 tmp.msgtyp, third);
236 }
237 default:
238 return sys_msgrcv (first,
239 (struct msgbuf __user *) ptr,
240 second, fifth, third);
241 }
242 case MSGGET:
243 return sys_msgget ((key_t) first, second);
244 case MSGCTL:
245 return sys_msgctl (first, second,
246 (struct msqid_ds __user *) ptr);
247 default:
248 return -EINVAL;
249 }
250 if (call <= SHMCTL)
251 switch (call) {
252 case SHMAT:
253 switch (version) {
254 default: {
255 ulong raddr;
256 ret = do_shmat (first, (char __user *) ptr,
257 second, &raddr);
258 if (ret)
259 return ret;
260 return put_user (raddr, (ulong __user *) third);
261 }
262 case 1: /* iBCS2 emulator entry point */
263 if (!segment_eq(get_fs(), get_ds()))
264 return -EINVAL;
265 return do_shmat (first, (char __user *) ptr,
266 second, (ulong *) third);
267 }
268 case SHMDT:
269 return sys_shmdt ((char __user *)ptr);
270 case SHMGET:
271 return sys_shmget (first, second, third);
272 case SHMCTL:
273 return sys_shmctl (first, second,
274 (struct shmid_ds __user *) ptr);
275 default:
276 return -EINVAL;
277 }
278
279 return -EINVAL;
280}
281
282asmlinkage int sys_uname(struct old_utsname * name)
283{
284 int err;
285 if (!name)
286 return -EFAULT;
287 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700288 err = copy_to_user(name, utsname(), sizeof (*name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 up_read(&uts_sem);
290 return err?-EFAULT:0;
291}
292
293asmlinkage ssize_t sys_pread_wrapper(unsigned int fd, char * buf,
294 size_t count, long dummy, loff_t pos)
295{
296 return sys_pread64(fd, buf, count, pos);
297}
298
299asmlinkage ssize_t sys_pwrite_wrapper(unsigned int fd, const char * buf,
300 size_t count, long dummy, loff_t pos)
301{
302 return sys_pwrite64(fd, buf, count, pos);
303}
304
305asmlinkage int sys_fadvise64_64_wrapper(int fd, u32 offset0, u32 offset1,
306 u32 len0, u32 len1, int advice)
307{
308#ifdef __LITTLE_ENDIAN__
309 return sys_fadvise64_64(fd, (u64)offset1 << 32 | offset0,
310 (u64)len1 << 32 | len0, advice);
311#else
312 return sys_fadvise64_64(fd, (u64)offset0 << 32 | offset1,
313 (u64)len0 << 32 | len1, advice);
314#endif
315}
Arnd Bergmannfe742902006-10-02 02:18:34 -0700316
Paul Mundt79890c52006-12-09 09:14:35 +0900317#if defined(CONFIG_CPU_SH2) || defined(CONFIG_CPU_SH2A)
318#define SYSCALL_ARG3 "trapa #0x23"
319#else
320#define SYSCALL_ARG3 "trapa #0x13"
321#endif
322
Arnd Bergmannfe742902006-10-02 02:18:34 -0700323/*
324 * Do a system call from kernel instead of calling sys_execve so we
325 * end up with proper pt_regs.
326 */
327int kernel_execve(const char *filename, char *const argv[], char *const envp[])
328{
329 register long __sc0 __asm__ ("r3") = __NR_execve;
330 register long __sc4 __asm__ ("r4") = (long) filename;
331 register long __sc5 __asm__ ("r5") = (long) argv;
332 register long __sc6 __asm__ ("r6") = (long) envp;
Yoshinori Sato11cbb702006-12-07 18:07:27 +0900333 __asm__ __volatile__ (SYSCALL_ARG3 : "=z" (__sc0)
Arnd Bergmannfe742902006-10-02 02:18:34 -0700334 : "0" (__sc0), "r" (__sc4), "r" (__sc5), "r" (__sc6)
335 : "memory");
336 return __sc0;
337}