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