1/*
2 *  linux/arch/arm/kernel/sys_arm.c
3 *
4 *  Copyright (C) People who wrote linux/arch/i386/kernel/sys_i386.c
5 *  Copyright (C) 1995, 1996 Russell King.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 *  This file contains various random system calls that
12 *  have a non-standard calling sequence on the Linux/arm
13 *  platform.
14 */
15#include <linux/module.h>
16#include <linux/errno.h>
17#include <linux/sched.h>
18#include <linux/slab.h>
19#include <linux/mm.h>
20#include <linux/sem.h>
21#include <linux/msg.h>
22#include <linux/shm.h>
23#include <linux/stat.h>
24#include <linux/syscalls.h>
25#include <linux/mman.h>
26#include <linux/fs.h>
27#include <linux/file.h>
28#include <linux/utsname.h>
29
30#include <asm/uaccess.h>
31#include <asm/ipc.h>
32
33extern unsigned long do_mremap(unsigned long addr, unsigned long old_len,
34			       unsigned long new_len, unsigned long flags,
35			       unsigned long new_addr);
36
37/*
38 * sys_pipe() is the normal C calling standard for creating
39 * a pipe. It's not the way unix traditionally does this, though.
40 */
41asmlinkage int sys_pipe(unsigned long __user *fildes)
42{
43	int fd[2];
44	int error;
45
46	error = do_pipe(fd);
47	if (!error) {
48		if (copy_to_user(fildes, fd, 2*sizeof(int)))
49			error = -EFAULT;
50	}
51	return error;
52}
53
54/* common code for old and new mmaps */
55inline long do_mmap2(
56	unsigned long addr, unsigned long len,
57	unsigned long prot, unsigned long flags,
58	unsigned long fd, unsigned long pgoff)
59{
60	int error = -EINVAL;
61	struct file * file = NULL;
62
63	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
64
65	if (flags & MAP_FIXED && addr < FIRST_USER_ADDRESS)
66		goto out;
67
68	error = -EBADF;
69	if (!(flags & MAP_ANONYMOUS)) {
70		file = fget(fd);
71		if (!file)
72			goto out;
73	}
74
75	down_write(&current->mm->mmap_sem);
76	error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
77	up_write(&current->mm->mmap_sem);
78
79	if (file)
80		fput(file);
81out:
82	return error;
83}
84
85struct mmap_arg_struct {
86	unsigned long addr;
87	unsigned long len;
88	unsigned long prot;
89	unsigned long flags;
90	unsigned long fd;
91	unsigned long offset;
92};
93
94asmlinkage int old_mmap(struct mmap_arg_struct __user *arg)
95{
96	int error = -EFAULT;
97	struct mmap_arg_struct a;
98
99	if (copy_from_user(&a, arg, sizeof(a)))
100		goto out;
101
102	error = -EINVAL;
103	if (a.offset & ~PAGE_MASK)
104		goto out;
105
106	error = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset >> PAGE_SHIFT);
107out:
108	return error;
109}
110
111asmlinkage unsigned long
112sys_arm_mremap(unsigned long addr, unsigned long old_len,
113	       unsigned long new_len, unsigned long flags,
114	       unsigned long new_addr)
115{
116	unsigned long ret = -EINVAL;
117
118	if (flags & MREMAP_FIXED && new_addr < FIRST_USER_ADDRESS)
119		goto out;
120
121	down_write(&current->mm->mmap_sem);
122	ret = do_mremap(addr, old_len, new_len, flags, new_addr);
123	up_write(&current->mm->mmap_sem);
124
125out:
126	return ret;
127}
128
129/*
130 * Perform the select(nd, in, out, ex, tv) and mmap() system
131 * calls.
132 */
133
134struct sel_arg_struct {
135	unsigned long n;
136	fd_set __user *inp, *outp, *exp;
137	struct timeval __user *tvp;
138};
139
140asmlinkage int old_select(struct sel_arg_struct __user *arg)
141{
142	struct sel_arg_struct a;
143
144	if (copy_from_user(&a, arg, sizeof(a)))
145		return -EFAULT;
146	/* sys_select() does the appropriate kernel locking */
147	return sys_select(a.n, a.inp, a.outp, a.exp, a.tvp);
148}
149
150#if !defined(CONFIG_AEABI) || defined(CONFIG_OABI_COMPAT)
151/*
152 * sys_ipc() is the de-multiplexer for the SysV IPC calls..
153 *
154 * This is really horribly ugly.
155 */
156asmlinkage int sys_ipc(uint call, int first, int second, int third,
157		       void __user *ptr, long fifth)
158{
159	int version, ret;
160
161	version = call >> 16; /* hack for backward compatibility */
162	call &= 0xffff;
163
164	switch (call) {
165	case SEMOP:
166		return sys_semtimedop (first, (struct sembuf __user *)ptr, second, NULL);
167	case SEMTIMEDOP:
168		return sys_semtimedop(first, (struct sembuf __user *)ptr, second,
169					(const struct timespec __user *)fifth);
170
171	case SEMGET:
172		return sys_semget (first, second, third);
173	case SEMCTL: {
174		union semun fourth;
175		if (!ptr)
176			return -EINVAL;
177		if (get_user(fourth.__pad, (void __user * __user *) ptr))
178			return -EFAULT;
179		return sys_semctl (first, second, third, fourth);
180	}
181
182	case MSGSND:
183		return sys_msgsnd(first, (struct msgbuf __user *) ptr,
184				  second, third);
185	case MSGRCV:
186		switch (version) {
187		case 0: {
188			struct ipc_kludge tmp;
189			if (!ptr)
190				return -EINVAL;
191			if (copy_from_user(&tmp,(struct ipc_kludge __user *)ptr,
192					   sizeof (tmp)))
193				return -EFAULT;
194			return sys_msgrcv (first, tmp.msgp, second,
195					   tmp.msgtyp, third);
196		}
197		default:
198			return sys_msgrcv (first,
199					   (struct msgbuf __user *) ptr,
200					   second, fifth, third);
201		}
202	case MSGGET:
203		return sys_msgget ((key_t) first, second);
204	case MSGCTL:
205		return sys_msgctl(first, second, (struct msqid_ds __user *)ptr);
206
207	case SHMAT:
208		switch (version) {
209		default: {
210			ulong raddr;
211			ret = do_shmat(first, (char __user *)ptr, second, &raddr);
212			if (ret)
213				return ret;
214			return put_user(raddr, (ulong __user *)third);
215		}
216		case 1: /* Of course, we don't support iBCS2! */
217			return -EINVAL;
218		}
219	case SHMDT:
220		return sys_shmdt ((char __user *)ptr);
221	case SHMGET:
222		return sys_shmget (first, second, third);
223	case SHMCTL:
224		return sys_shmctl (first, second,
225				   (struct shmid_ds __user *) ptr);
226	default:
227		return -ENOSYS;
228	}
229}
230#endif
231
232/* Fork a new task - this creates a new program thread.
233 * This is called indirectly via a small wrapper
234 */
235asmlinkage int sys_fork(struct pt_regs *regs)
236{
237#ifdef CONFIG_MMU
238	return do_fork(SIGCHLD, regs->ARM_sp, regs, 0, NULL, NULL);
239#else
240	/* can not support in nommu mode */
241	return(-EINVAL);
242#endif
243}
244
245/* Clone a task - this clones the calling program thread.
246 * This is called indirectly via a small wrapper
247 */
248asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
249			 int __user *parent_tidptr, int tls_val,
250			 int __user *child_tidptr, struct pt_regs *regs)
251{
252	if (!newsp)
253		newsp = regs->ARM_sp;
254
255	return do_fork(clone_flags, newsp, regs, 0, parent_tidptr, child_tidptr);
256}
257
258asmlinkage int sys_vfork(struct pt_regs *regs)
259{
260	return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->ARM_sp, regs, 0, NULL, NULL);
261}
262
263/* sys_execve() executes a new program.
264 * This is called indirectly via a small wrapper
265 */
266asmlinkage int sys_execve(char __user *filenamei, char __user * __user *argv,
267			  char __user * __user *envp, struct pt_regs *regs)
268{
269	int error;
270	char * filename;
271
272	filename = getname(filenamei);
273	error = PTR_ERR(filename);
274	if (IS_ERR(filename))
275		goto out;
276	error = do_execve(filename, argv, envp, regs);
277	putname(filename);
278out:
279	return error;
280}
281
282int kernel_execve(const char *filename, char *const argv[], char *const envp[])
283{
284	struct pt_regs regs;
285	int ret;
286
287	memset(&regs, 0, sizeof(struct pt_regs));
288	ret = do_execve((char *)filename, (char __user * __user *)argv,
289			(char __user * __user *)envp, &regs);
290	if (ret < 0)
291		goto out;
292
293	/*
294	 * Save argc to the register structure for userspace.
295	 */
296	regs.ARM_r0 = ret;
297
298	/*
299	 * We were successful.  We won't be returning to our caller, but
300	 * instead to user space by manipulating the kernel stack.
301	 */
302	asm(	"add	r0, %0, %1\n\t"
303		"mov	r1, %2\n\t"
304		"mov	r2, %3\n\t"
305		"bl	memmove\n\t"	/* copy regs to top of stack */
306		"mov	r8, #0\n\t"	/* not a syscall */
307		"mov	r9, %0\n\t"	/* thread structure */
308		"mov	sp, r0\n\t"	/* reposition stack pointer */
309		"b	ret_to_user"
310		:
311		: "r" (current_thread_info()),
312		  "Ir" (THREAD_START_SP - sizeof(regs)),
313		  "r" (&regs),
314		  "Ir" (sizeof(regs))
315		: "r0", "r1", "r2", "r3", "ip", "lr", "memory");
316
317 out:
318	return ret;
319}
320EXPORT_SYMBOL(kernel_execve);
321
322/*
323 * Since loff_t is a 64 bit type we avoid a lot of ABI hassle
324 * with a different argument ordering.
325 */
326asmlinkage long sys_arm_fadvise64_64(int fd, int advice,
327				     loff_t offset, loff_t len)
328{
329	return sys_fadvise64_64(fd, offset, len, advice);
330}
331