Linux-2.6.12-rc2
[safe/jmp/linux-2.6] / arch / arm / kernel / sys_arm.c
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
33 extern 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  */
41 asmlinkage 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 /*
55  * This is the lowest virtual address we can permit any user space
56  * mapping to be mapped at.  This is particularly important for
57  * non-high vector CPUs.
58  */
59 #define MIN_MAP_ADDR    (PAGE_SIZE)
60
61 /* common code for old and new mmaps */
62 inline long do_mmap2(
63         unsigned long addr, unsigned long len,
64         unsigned long prot, unsigned long flags,
65         unsigned long fd, unsigned long pgoff)
66 {
67         int error = -EINVAL;
68         struct file * file = NULL;
69
70         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
71
72         if (flags & MAP_FIXED && addr < MIN_MAP_ADDR)
73                 goto out;
74
75         error = -EBADF;
76         if (!(flags & MAP_ANONYMOUS)) {
77                 file = fget(fd);
78                 if (!file)
79                         goto out;
80         }
81
82         down_write(&current->mm->mmap_sem);
83         error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
84         up_write(&current->mm->mmap_sem);
85
86         if (file)
87                 fput(file);
88 out:
89         return error;
90 }
91
92 struct mmap_arg_struct {
93         unsigned long addr;
94         unsigned long len;
95         unsigned long prot;
96         unsigned long flags;
97         unsigned long fd;
98         unsigned long offset;
99 };
100
101 asmlinkage int old_mmap(struct mmap_arg_struct __user *arg)
102 {
103         int error = -EFAULT;
104         struct mmap_arg_struct a;
105
106         if (copy_from_user(&a, arg, sizeof(a)))
107                 goto out;
108
109         error = -EINVAL;
110         if (a.offset & ~PAGE_MASK)
111                 goto out;
112
113         error = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset >> PAGE_SHIFT);
114 out:
115         return error;
116 }
117
118 asmlinkage unsigned long
119 sys_arm_mremap(unsigned long addr, unsigned long old_len,
120                unsigned long new_len, unsigned long flags,
121                unsigned long new_addr)
122 {
123         unsigned long ret = -EINVAL;
124
125         if (flags & MREMAP_FIXED && new_addr < MIN_MAP_ADDR)
126                 goto out;
127
128         down_write(&current->mm->mmap_sem);
129         ret = do_mremap(addr, old_len, new_len, flags, new_addr);
130         up_write(&current->mm->mmap_sem);
131
132 out:
133         return ret;
134 }
135
136 /*
137  * Perform the select(nd, in, out, ex, tv) and mmap() system
138  * calls.
139  */
140
141 struct sel_arg_struct {
142         unsigned long n;
143         fd_set __user *inp, *outp, *exp;
144         struct timeval __user *tvp;
145 };
146
147 asmlinkage int old_select(struct sel_arg_struct __user *arg)
148 {
149         struct sel_arg_struct a;
150
151         if (copy_from_user(&a, arg, sizeof(a)))
152                 return -EFAULT;
153         /* sys_select() does the appropriate kernel locking */
154         return sys_select(a.n, a.inp, a.outp, a.exp, a.tvp);
155 }
156
157 /*
158  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
159  *
160  * This is really horribly ugly.
161  */
162 asmlinkage int sys_ipc(uint call, int first, int second, int third,
163                        void __user *ptr, long fifth)
164 {
165         int version, ret;
166
167         version = call >> 16; /* hack for backward compatibility */
168         call &= 0xffff;
169
170         switch (call) {
171         case SEMOP:
172                 return sys_semtimedop (first, (struct sembuf __user *)ptr, second, NULL);
173         case SEMTIMEDOP:
174                 return sys_semtimedop(first, (struct sembuf __user *)ptr, second,
175                                         (const struct timespec __user *)fifth);
176
177         case SEMGET:
178                 return sys_semget (first, second, third);
179         case SEMCTL: {
180                 union semun fourth;
181                 if (!ptr)
182                         return -EINVAL;
183                 if (get_user(fourth.__pad, (void __user * __user *) ptr))
184                         return -EFAULT;
185                 return sys_semctl (first, second, third, fourth);
186         }
187
188         case MSGSND:
189                 return sys_msgsnd(first, (struct msgbuf __user *) ptr, 
190                                   second, third);
191         case MSGRCV:
192                 switch (version) {
193                 case 0: {
194                         struct ipc_kludge tmp;
195                         if (!ptr)
196                                 return -EINVAL;
197                         if (copy_from_user(&tmp,(struct ipc_kludge __user *)ptr,
198                                            sizeof (tmp)))
199                                 return -EFAULT;
200                         return sys_msgrcv (first, tmp.msgp, second,
201                                            tmp.msgtyp, third);
202                 }
203                 default:
204                         return sys_msgrcv (first,
205                                            (struct msgbuf __user *) ptr,
206                                            second, fifth, third);
207                 }
208         case MSGGET:
209                 return sys_msgget ((key_t) first, second);
210         case MSGCTL:
211                 return sys_msgctl(first, second, (struct msqid_ds __user *)ptr);
212
213         case SHMAT:
214                 switch (version) {
215                 default: {
216                         ulong raddr;
217                         ret = do_shmat(first, (char __user *)ptr, second, &raddr);
218                         if (ret)
219                                 return ret;
220                         return put_user(raddr, (ulong __user *)third);
221                 }
222                 case 1: /* Of course, we don't support iBCS2! */
223                         return -EINVAL;
224                 }
225         case SHMDT: 
226                 return sys_shmdt ((char __user *)ptr);
227         case SHMGET:
228                 return sys_shmget (first, second, third);
229         case SHMCTL:
230                 return sys_shmctl (first, second,
231                                    (struct shmid_ds __user *) ptr);
232         default:
233                 return -ENOSYS;
234         }
235 }
236
237 asmlinkage long sys_shmat(int shmid, char __user *shmaddr, int shmflg,
238                           unsigned long __user *addr)
239 {
240         unsigned long ret;
241         long err;
242
243         err = do_shmat(shmid, shmaddr, shmflg, &ret);
244         if (err == 0)
245                 err = put_user(ret, addr);
246         return err;
247 }
248
249 /* Fork a new task - this creates a new program thread.
250  * This is called indirectly via a small wrapper
251  */
252 asmlinkage int sys_fork(struct pt_regs *regs)
253 {
254         return do_fork(SIGCHLD, regs->ARM_sp, regs, 0, NULL, NULL);
255 }
256
257 /* Clone a task - this clones the calling program thread.
258  * This is called indirectly via a small wrapper
259  */
260 asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
261                          int __user *parent_tidptr, int tls_val,
262                          int __user *child_tidptr, struct pt_regs *regs)
263 {
264         if (!newsp)
265                 newsp = regs->ARM_sp;
266
267         return do_fork(clone_flags, newsp, regs, 0, parent_tidptr, child_tidptr);
268 }
269
270 asmlinkage int sys_vfork(struct pt_regs *regs)
271 {
272         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->ARM_sp, regs, 0, NULL, NULL);
273 }
274
275 /* sys_execve() executes a new program.
276  * This is called indirectly via a small wrapper
277  */
278 asmlinkage int sys_execve(char __user *filenamei, char __user * __user *argv,
279                           char __user * __user *envp, struct pt_regs *regs)
280 {
281         int error;
282         char * filename;
283
284         filename = getname(filenamei);
285         error = PTR_ERR(filename);
286         if (IS_ERR(filename))
287                 goto out;
288         error = do_execve(filename, argv, envp, regs);
289         putname(filename);
290 out:
291         return error;
292 }
293
294 long execve(const char *filename, char **argv, char **envp)
295 {
296         struct pt_regs regs;
297         int ret;
298
299         memset(&regs, 0, sizeof(struct pt_regs));
300         ret = do_execve((char *)filename, (char __user * __user *)argv,
301                         (char __user * __user *)envp, &regs);
302         if (ret < 0)
303                 goto out;
304
305         /*
306          * Save argc to the register structure for userspace.
307          */
308         regs.ARM_r0 = ret;
309
310         /*
311          * We were successful.  We won't be returning to our caller, but
312          * instead to user space by manipulating the kernel stack.
313          */
314         asm(    "add    r0, %0, %1\n\t"
315                 "mov    r1, %2\n\t"
316                 "mov    r2, %3\n\t"
317                 "bl     memmove\n\t"    /* copy regs to top of stack */
318                 "mov    r8, #0\n\t"     /* not a syscall */
319                 "mov    r9, %0\n\t"     /* thread structure */
320                 "mov    sp, r0\n\t"     /* reposition stack pointer */
321                 "b      ret_to_user"
322                 :
323                 : "r" (current_thread_info()),
324                   "Ir" (THREAD_SIZE - 8 - sizeof(regs)),
325                   "r" (&regs),
326                   "Ir" (sizeof(regs))
327                 : "r0", "r1", "r2", "r3", "ip", "memory");
328
329  out:
330         return ret;
331 }
332 EXPORT_SYMBOL(execve);