also update spec file ...
[linux-flexiantxendom0-3.2.10.git] / arch / mips64 / kernel / syscall.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1995 - 2000, 2001 by Ralf Baechle
7  * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
8  * Copyright (C) 2001 MIPS Technologies, Inc.
9  */
10 #include <linux/errno.h>
11 #include <linux/linkage.h>
12 #include <linux/mm.h>
13 #include <linux/smp.h>
14 #include <linux/smp_lock.h>
15 #include <linux/mman.h>
16 #include <linux/sched.h>
17 #include <linux/string.h>
18 #include <linux/file.h>
19 #include <linux/utsname.h>
20 #include <linux/unistd.h>
21 #include <linux/sem.h>
22 #include <linux/msg.h>
23 #include <linux/shm.h>
24 #include <linux/slab.h>
25 #include <asm/ipc.h>
26 #include <asm/cachectl.h>
27 #include <asm/offset.h>
28 #include <asm/pgalloc.h>
29 #include <asm/ptrace.h>
30 #include <asm/signal.h>
31 #include <asm/stackframe.h>
32 #include <asm/sysmips.h>
33 #include <asm/uaccess.h>
34
35 extern asmlinkage void syscall_trace(void);
36
37 asmlinkage int sys_pipe(abi64_no_regargs, struct pt_regs regs)
38 {
39         int fd[2];
40         int error, res;
41
42         error = do_pipe(fd);
43         if (error) {
44                 res = error;
45                 goto out;
46         }
47         regs.regs[3] = fd[1];
48         res = fd[0];
49 out:
50         return res;
51 }
52
53 unsigned long shm_align_mask = PAGE_SIZE - 1;   /* Sane caches */
54
55 #define COLOUR_ALIGN(addr,pgoff)                                \
56         ((((addr) + shm_align_mask) & ~shm_align_mask) +        \
57          (((pgoff) << PAGE_SHIFT) & shm_align_mask))
58
59 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
60         unsigned long len, unsigned long pgoff, unsigned long flags)
61 {
62         struct vm_area_struct * vmm;
63         int do_color_align;
64
65         if (flags & MAP_FIXED) {
66                 /*
67                  * We do not accept a shared mapping if it would violate
68                  * cache aliasing constraints.
69                  */
70                 if ((flags & MAP_SHARED) && (addr & shm_align_mask))
71                         return -EINVAL;
72                 return addr;
73         }
74
75         if (len > TASK_SIZE)
76                 return -ENOMEM;
77         do_color_align = 0;
78         if (filp || (flags & MAP_SHARED))
79                 do_color_align = 1;
80         if (addr) {
81                 if (do_color_align)
82                         addr = COLOUR_ALIGN(addr, pgoff);
83                 else
84                         addr = PAGE_ALIGN(addr);
85                 vmm = find_vma(current->mm, addr);
86                 if (TASK_SIZE - len >= addr &&
87                     (!vmm || addr + len <= vmm->vm_start))
88                         return addr;
89         }
90         addr = TASK_UNMAPPED_BASE;
91         if (do_color_align)
92                 addr = COLOUR_ALIGN(addr, pgoff);
93         else
94                 addr = PAGE_ALIGN(addr);
95
96         for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
97                 /* At this point:  (!vmm || addr < vmm->vm_end). */
98                 if (TASK_SIZE - len < addr)
99                         return -ENOMEM;
100                 if (!vmm || addr + len <= vmm->vm_start)
101                         return addr;
102                 addr = vmm->vm_end;
103                 if (do_color_align)
104                         addr = COLOUR_ALIGN(addr, pgoff);
105         }
106 }
107
108 asmlinkage unsigned long
109 sys_mmap(unsigned long addr, size_t len, unsigned long prot,
110          unsigned long flags, unsigned long fd, off_t offset)
111 {
112         struct file * file = NULL;
113         unsigned long error;
114
115         error = -EINVAL;
116         if (offset & ~PAGE_MASK)
117                 goto out;
118
119         if (!(flags & MAP_ANONYMOUS)) {
120                 error = -EBADF;
121                 file = fget(fd);
122                 if (!file)
123                         goto out;
124         }
125         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
126
127         down_write(&current->mm->mmap_sem);
128         error = do_mmap(file, addr, len, prot, flags, offset);
129         up_write(&current->mm->mmap_sem);
130         if (file)
131                 fput(file);
132
133 out:
134         return error;
135 }
136
137 asmlinkage int sys_fork(abi64_no_regargs, struct pt_regs regs)
138 {
139         save_static(&regs);
140         return do_fork(SIGCHLD, regs.regs[29], &regs, 0, NULL, NULL);
141 }
142
143 asmlinkage int sys_clone(abi64_no_regargs, struct pt_regs regs)
144 {
145         unsigned long clone_flags;
146         unsigned long newsp;
147         int *parent_tidptr, *child_tidptr;
148
149         save_static(&regs);
150         clone_flags = regs.regs[4];
151         newsp = regs.regs[5];
152         if (!newsp)
153                 newsp = regs.regs[29];
154         parent_tidptr = (int *) regs.regs[6];
155         child_tidptr = (int *) regs.regs[7];
156         return do_fork(clone_flags & ~CLONE_IDLETASK, newsp, &regs, 0,
157                        parent_tidptr, child_tidptr);
158 }
159
160 /*
161  * sys_execve() executes a new program.
162  */
163 asmlinkage int sys_execve(abi64_no_regargs, struct pt_regs regs)
164 {
165         int error;
166         char * filename;
167
168         filename = getname((char *) (long)regs.regs[4]);
169         error = PTR_ERR(filename);
170         if (IS_ERR(filename))
171                 goto out;
172         error = do_execve(filename, (char **) (long)regs.regs[5],
173                           (char **) (long)regs.regs[6], &regs);
174         putname(filename);
175
176 out:
177         return error;
178 }
179
180 /*
181  * Do the indirect syscall syscall.
182  *
183  * XXX This is borken.
184  */
185 asmlinkage int sys_syscall(abi64_no_regargs, struct pt_regs regs)
186 {
187         return -ENOSYS;
188 }
189
190 asmlinkage int sys_sysmips(int cmd, long arg1, int arg2, int arg3)
191 {
192         int     tmp, len;
193         char    *name;
194
195         switch(cmd) {
196         case SETNAME: {
197                 char nodename[__NEW_UTS_LEN + 1];
198
199                 if (!capable(CAP_SYS_ADMIN))
200                         return -EPERM;
201
202                 name = (char *) arg1;
203
204                 len = strncpy_from_user(nodename, name, __NEW_UTS_LEN);
205                 if (len < 0)
206                         return -EFAULT;
207
208                 down_write(&uts_sem);
209                 strncpy(system_utsname.nodename, nodename, len);
210                 nodename[__NEW_UTS_LEN] = '\0';
211                 strlcpy(system_utsname.nodename, nodename,
212                         sizeof(system_utsname.nodename));
213                 up_write(&uts_sem);
214                 return 0;
215         }
216
217         case MIPS_ATOMIC_SET:
218                 printk(KERN_CRIT "How did I get here?\n");
219                 return -EINVAL;
220
221         case MIPS_FIXADE:
222                 tmp = current->thread.mflags & ~3;
223                 current->thread.mflags = tmp | (arg1 & 3);
224                 return 0;
225
226         case FLUSH_CACHE:
227                 __flush_cache_all();
228                 return 0;
229
230         case MIPS_RDNVRAM:
231                 return -EIO;
232         }
233
234         return -EINVAL;
235 }
236
237 /*
238  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
239  *
240  * This is really horribly ugly.
241  */
242 asmlinkage int sys_ipc (uint call, int first, int second,
243                         unsigned long third, void *ptr, long fifth)
244 {
245         int version, ret;
246
247         version = call >> 16; /* hack for backward compatibility */
248         call &= 0xffff;
249
250         switch (call) {
251         case SEMOP:
252                 return sys_semop (first, (struct sembuf *)ptr, second);
253         case SEMGET:
254                 return sys_semget (first, second, third);
255         case SEMCTL: {
256                 union semun fourth;
257                 if (!ptr)
258                         return -EINVAL;
259                 if (get_user(fourth.__pad, (void **) ptr))
260                         return -EFAULT;
261                 return sys_semctl (first, second, third, fourth);
262         }
263
264         case MSGSND:
265                 return sys_msgsnd (first, (struct msgbuf *) ptr,
266                                    second, third);
267         case MSGRCV:
268                 switch (version) {
269                 case 0: {
270                         struct ipc_kludge tmp;
271                         if (!ptr)
272                                 return -EINVAL;
273
274                         if (copy_from_user(&tmp,
275                                            (struct ipc_kludge *) ptr,
276                                            sizeof (tmp)))
277                                 return -EFAULT;
278                         return sys_msgrcv (first, tmp.msgp, second,
279                                            tmp.msgtyp, third);
280                 }
281                 default:
282                         return sys_msgrcv (first,
283                                            (struct msgbuf *) ptr,
284                                            second, fifth, third);
285                 }
286         case MSGGET:
287                 return sys_msgget ((key_t) first, second);
288         case MSGCTL:
289                 return sys_msgctl (first, second, (struct msqid_ds *) ptr);
290
291         case SHMAT:
292                 switch (version) {
293                 default: {
294                         ulong raddr;
295                         ret = sys_shmat (first, (char *) ptr, second, &raddr);
296                         if (ret)
297                                 return ret;
298                         return put_user (raddr, (ulong *) third);
299                 }
300                 case 1: /* iBCS2 emulator entry point */
301                         if (!segment_eq(get_fs(), get_ds()))
302                                 return -EINVAL;
303                         return sys_shmat (first, (char *) ptr, second, (ulong *) third);
304                 }
305         case SHMDT:
306                 return sys_shmdt ((char *)ptr);
307         case SHMGET:
308                 return sys_shmget (first, second, third);
309         case SHMCTL:
310                 return sys_shmctl (first, second,
311                                    (struct shmid_ds *) ptr);
312         default:
313                 return -ENOSYS;
314         }
315 }
316
317 /*
318  * No implemented yet ...
319  */
320 asmlinkage int sys_cachectl(char *addr, int nbytes, int op)
321 {
322         return -ENOSYS;
323 }
324
325 /*
326  * If we ever come here the user sp is bad.  Zap the process right away.
327  * Due to the bad stack signaling wouldn't work.
328  */
329 asmlinkage void bad_stack(void)
330 {
331         do_exit(SIGSEGV);
332 }