Merge acpi-2.6.12 to-akpm
[sfrench/cifs-2.6.git] / arch / xtensa / kernel / syscalls.c
1 /*
2  * arch/xtensa/kernel/syscall.c
3  *
4  * This file is subject to the terms and conditions of the GNU General Public
5  * License.  See the file "COPYING" in the main directory of this archive
6  * for more details.
7  *
8  * Copyright (C) 2001 - 2005 Tensilica Inc.
9  * Copyright (C) 2000 Silicon Graphics, Inc.
10  * Copyright (C) 1995 - 2000 by Ralf Baechle
11  *
12  * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
13  * Marc Gauthier <marc@tensilica.com, marc@alumni.uwaterloo.ca>
14  * Chris Zankel <chris@zankel.net>
15  * Kevin Chea
16  *
17  */
18
19 #define DEBUG   0
20
21 #include <linux/config.h>
22 #include <linux/linkage.h>
23 #include <linux/mm.h>
24 #include <linux/smp.h>
25 #include <linux/smp_lock.h>
26 #include <linux/mman.h>
27 #include <linux/sched.h>
28 #include <linux/file.h>
29 #include <linux/slab.h>
30 #include <linux/utsname.h>
31 #include <linux/unistd.h>
32 #include <linux/stringify.h>
33 #include <linux/syscalls.h>
34 #include <linux/sem.h>
35 #include <linux/msg.h>
36 #include <linux/shm.h>
37 #include <linux/errno.h>
38 #include <asm/ptrace.h>
39 #include <asm/signal.h>
40 #include <asm/uaccess.h>
41 #include <asm/hardirq.h>
42 #include <asm/mman.h>
43 #include <asm/shmparam.h>
44 #include <asm/page.h>
45
46 extern void do_syscall_trace(void);
47 typedef int (*syscall_t)(void *a0,...);
48 extern syscall_t sys_call_table[];
49 extern unsigned char sys_narg_table[];
50
51 /*
52  * sys_pipe() is the normal C calling standard for creating a pipe. It's not
53  * the way unix traditional does this, though.
54  */
55
56 int sys_pipe(int __user *userfds)
57 {
58         int fd[2];
59         int error;
60
61         error = do_pipe(fd);
62         if (!error) {
63                 if (copy_to_user(userfds, fd, 2 * sizeof(int)))
64                         error = -EFAULT;
65         }
66         return error;
67 }
68
69 /*
70  * Common code for old and new mmaps.
71  */
72 long sys_mmap(unsigned long addr, unsigned long len, unsigned long prot,
73               unsigned long flags, unsigned long fd, unsigned long pgoff)
74 {
75         int error = -EBADF;
76         struct file * file = NULL;
77
78         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
79         if (!(flags & MAP_ANONYMOUS)) {
80                 file = fget(fd);
81                 if (!file)
82                         goto out;
83         }
84
85         down_write(&current->mm->mmap_sem);
86         error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
87         up_write(&current->mm->mmap_sem);
88
89         if (file)
90                 fput(file);
91 out:
92         return error;
93 }
94
95 int sys_clone(struct pt_regs *regs)
96 {
97         unsigned long clone_flags;
98         unsigned long newsp;
99         int __user *parent_tidptr, *child_tidptr;
100         clone_flags = regs->areg[4];
101         newsp = regs->areg[3];
102         parent_tidptr = (int __user *)regs->areg[5];
103         child_tidptr = (int __user *)regs->areg[6];
104         if (!newsp)
105                 newsp = regs->areg[1];
106         return do_fork(clone_flags,newsp,regs,0,parent_tidptr,child_tidptr);
107 }
108
109 /*
110  * sys_execve() executes a new program.
111  */
112
113 int sys_execve(struct pt_regs *regs)
114 {
115         int error;
116         char * filename;
117
118         filename = getname((char *) (long)regs->areg[5]);
119         error = PTR_ERR(filename);
120         if (IS_ERR(filename))
121                 goto out;
122         error = do_execve(filename, (char **) (long)regs->areg[3],
123                           (char **) (long)regs->areg[4], regs);
124         putname(filename);
125
126 out:
127         return error;
128 }
129
130 int sys_uname(struct old_utsname * name)
131 {
132         if (name && !copy_to_user(name, &system_utsname, sizeof (*name)))
133                 return 0;
134         return -EFAULT;
135 }
136
137 /*
138  * Build the string table for the builtin "poor man's strace".
139  */
140
141 #if DEBUG
142 #define SYSCALL(fun, narg) #fun,
143 static char *sfnames[] = {
144 #include "syscalls.h"
145 };
146 #undef SYS
147 #endif
148
149 void system_call (struct pt_regs *regs)
150 {
151         syscall_t syscall;
152         unsigned long parm0, parm1, parm2, parm3, parm4, parm5;
153         int nargs, res;
154         unsigned int syscallnr;
155         int ps;
156
157 #if DEBUG
158         int i;
159         unsigned long parms[6];
160         char *sysname;
161 #endif
162
163         regs->syscall = regs->areg[2];
164
165         do_syscall_trace();
166
167         /* Have to load after syscall_trace because strace
168          * sometimes changes regs->syscall.
169          */
170         syscallnr = regs->syscall;
171
172         parm0 = parm1 = parm2 = parm3 = parm4 = parm5 = 0;
173
174         /* Restore interrupt level to syscall invoker's.
175          * If this were in assembly, we wouldn't disable
176          * interrupts in the first place:
177          */
178         local_save_flags (ps);
179         local_irq_restore((ps & ~XCHAL_PS_INTLEVEL_MASK) |
180                           (regs->ps & XCHAL_PS_INTLEVEL_MASK) );
181
182         if (syscallnr > __NR_Linux_syscalls) {
183                 regs->areg[2] = -ENOSYS;
184                 return;
185         }
186
187         syscall = sys_call_table[syscallnr];
188         nargs = sys_narg_table[syscallnr];
189
190         if (syscall == NULL) {
191                 regs->areg[2] = -ENOSYS;
192                 return;
193         }
194
195         /* There shouldn't be more than six arguments in the table! */
196
197         if (nargs > 6)
198                 panic("Internal error - too many syscall arguments (%d)!\n",
199                       nargs);
200
201         /* Linux takes system-call arguments in registers.  The ABI
202          * and Xtensa software conventions require the system-call
203          * number in a2.  If an argument exists in a2, we move it to
204          * the next available register.  Note that for improved
205          * efficiency, we do NOT shift all parameters down one
206          * register to maintain the original order.
207          *
208          * At best case (zero arguments), we just write the syscall
209          * number to a2.  At worst case (1 to 6 arguments), we move
210          * the argument in a2 to the next available register, then
211          * write the syscall number to a2.
212          *
213          * For clarity, the following truth table enumerates all
214          * possibilities.
215          *
216          * arguments    syscall number  arg0, arg1, arg2, arg3, arg4, arg5
217          * ---------    --------------  ----------------------------------
218          *      0             a2
219          *      1             a2        a3
220          *      2             a2        a4,   a3
221          *      3             a2        a5,   a3,   a4
222          *      4             a2        a6,   a3,   a4,   a5
223          *      5             a2        a7,   a3,   a4,   a5,   a6
224          *      6             a2        a8,   a3,   a4,   a5,   a6,   a7
225          */
226         if (nargs) {
227                 parm0 = regs->areg[nargs+2];
228                 parm1 = regs->areg[3];
229                 parm2 = regs->areg[4];
230                 parm3 = regs->areg[5];
231                 parm4 = regs->areg[6];
232                 parm5 = regs->areg[7];
233         } else /* nargs == 0 */
234                 parm0 = (unsigned long) regs;
235
236 #if DEBUG
237         parms[0] = parm0;
238         parms[1] = parm1;
239         parms[2] = parm2;
240         parms[3] = parm3;
241         parms[4] = parm4;
242         parms[5] = parm5;
243
244         sysname = sfnames[syscallnr];
245         if (strncmp(sysname, "sys_", 4) == 0)
246                 sysname = sysname + 4;
247
248         printk("\017SYSCALL:I:%x:%d:%s  %s(", regs->pc, current->pid,
249                current->comm, sysname);
250         for (i = 0; i < nargs; i++)
251                 printk((i>0) ? ", %#lx" : "%#lx", parms[i]);
252         printk(")\n");
253 #endif
254
255         res = syscall((void *)parm0, parm1, parm2, parm3, parm4, parm5);
256
257 #if DEBUG
258         printk("\017SYSCALL:O:%d:%s  %s(",current->pid, current->comm, sysname);
259         for (i = 0; i < nargs; i++)
260                 printk((i>0) ? ", %#lx" : "%#lx", parms[i]);
261         if (res < 4096)
262                 printk(") = %d\n", res);
263         else
264                 printk(") = %#x\n", res);
265 #endif /* DEBUG */
266
267         regs->areg[2] = res;
268         do_syscall_trace();
269 }