patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / um / kernel / syscall_kern.c
1 /* 
2  * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include "linux/sched.h"
7 #include "linux/file.h"
8 #include "linux/smp_lock.h"
9 #include "linux/mm.h"
10 #include "linux/utsname.h"
11 #include "linux/msg.h"
12 #include "linux/shm.h"
13 #include "linux/sys.h"
14 #include "linux/syscalls.h"
15 #include "linux/unistd.h"
16 #include "linux/slab.h"
17 #include "linux/utime.h"
18 #include <linux/vs_cvirt.h>
19
20 #include "asm/mman.h"
21 #include "asm/uaccess.h"
22 #include "asm/ipc.h"
23 #include "kern_util.h"
24 #include "user_util.h"
25 #include "sysdep/syscalls.h"
26 #include "mode_kern.h"
27 #include "choose-mode.h"
28
29 /*  Unlocked, I don't care if this is a bit off */
30 int nsyscalls = 0;
31
32 long um_mount(char * dev_name, char * dir_name, char * type,
33               unsigned long new_flags, void * data)
34 {
35         if(type == NULL) type = "";
36         return(sys_mount(dev_name, dir_name, type, new_flags, data));
37 }
38
39 long sys_fork(void)
40 {
41         struct task_struct *p;
42
43         current->thread.forking = 1;
44         p = do_fork(SIGCHLD, 0, NULL, 0, NULL, NULL);
45         current->thread.forking = 0;
46         return(IS_ERR(p) ? PTR_ERR(p) : p->pid);
47 }
48
49 long sys_clone(unsigned long clone_flags, unsigned long newsp)
50 {
51         struct task_struct *p;
52
53         current->thread.forking = 1;
54         p = do_fork(clone_flags, newsp, NULL, 0, NULL, NULL);
55         current->thread.forking = 0;
56         return(IS_ERR(p) ? PTR_ERR(p) : p->pid);
57 }
58
59 long sys_vfork(void)
60 {
61         struct task_struct *p;
62
63         current->thread.forking = 1;
64         p = do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0, NULL, 0, NULL, NULL);
65         current->thread.forking = 0;
66         return(IS_ERR(p) ? PTR_ERR(p) : p->pid);
67 }
68
69 /* common code for old and new mmaps */
70 static inline long do_mmap2(
71         unsigned long addr, unsigned long len,
72         unsigned long prot, unsigned long flags,
73         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 long sys_mmap2(unsigned long addr, unsigned long len,
96                unsigned long prot, unsigned long flags,
97                unsigned long fd, unsigned long pgoff)
98 {
99         return do_mmap2(addr, len, prot, flags, fd, pgoff);
100 }
101
102 /*
103  * Perform the select(nd, in, out, ex, tv) and mmap() system
104  * calls. Linux/i386 didn't use to be able to handle more than
105  * 4 system call parameters, so these system calls used a memory
106  * block for parameter passing..
107  */
108
109 struct mmap_arg_struct {
110         unsigned long addr;
111         unsigned long len;
112         unsigned long prot;
113         unsigned long flags;
114         unsigned long fd;
115         unsigned long offset;
116 };
117
118 int old_mmap(unsigned long addr, unsigned long len,
119              unsigned long prot, unsigned long flags,
120              unsigned long fd, unsigned long offset)
121 {
122         int err = -EINVAL;
123         if (offset & ~PAGE_MASK)
124                 goto out;
125
126         err = do_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
127  out:
128         return err;
129 }
130 /*
131  * sys_pipe() is the normal C calling standard for creating
132  * a pipe. It's not the way unix traditionally does this, though.
133  */
134 int sys_pipe(unsigned long * fildes)
135 {
136         int fd[2];
137         int error;
138
139         error = do_pipe(fd);
140         if (!error) {
141                 if (copy_to_user(fildes, fd, 2*sizeof(int)))
142                         error = -EFAULT;
143         }
144         return error;
145 }
146
147 int sys_sigaction(int sig, const struct old_sigaction *act,
148                          struct old_sigaction *oact)
149 {
150         struct k_sigaction new_ka, old_ka;
151         int ret;
152
153         if (act) {
154                 old_sigset_t mask;
155                 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
156                     __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
157                     __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
158                         return -EFAULT;
159                 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
160                 __get_user(mask, &act->sa_mask);
161                 siginitset(&new_ka.sa.sa_mask, mask);
162         }
163
164         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
165
166         if (!ret && oact) {
167                 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
168                     __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
169                     __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
170                         return -EFAULT;
171                 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
172                 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
173         }
174
175         return ret;
176 }
177
178 /*
179  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
180  *
181  * This is really horribly ugly.
182  */
183 int sys_ipc (uint call, int first, int second,
184              int third, void *ptr, long fifth)
185 {
186         int version, ret;
187
188         version = call >> 16; /* hack for backward compatibility */
189         call &= 0xffff;
190
191         switch (call) {
192         case SEMOP:
193                 return sys_semtimedop(first, (struct sembuf *) ptr, second, 
194                                       NULL);
195         case SEMTIMEDOP:
196                 return sys_semtimedop(first, (struct sembuf *) ptr, second,
197                                       (const struct timespec *) fifth);
198         case SEMGET:
199                 return sys_semget (first, second, third);
200         case SEMCTL: {
201                 union semun fourth;
202                 if (!ptr)
203                         return -EINVAL;
204                 if (get_user(fourth.__pad, (void **) ptr))
205                         return -EFAULT;
206                 return sys_semctl (first, second, third, fourth);
207         }
208
209         case MSGSND:
210                 return sys_msgsnd (first, (struct msgbuf *) ptr, 
211                                    second, third);
212         case MSGRCV:
213                 switch (version) {
214                 case 0: {
215                         struct ipc_kludge tmp;
216                         if (!ptr)
217                                 return -EINVAL;
218                         
219                         if (copy_from_user(&tmp,
220                                            (struct ipc_kludge *) ptr, 
221                                            sizeof (tmp)))
222                                 return -EFAULT;
223                         return sys_msgrcv (first, tmp.msgp, second,
224                                            tmp.msgtyp, third);
225                 }
226                 default:
227                         panic("msgrcv with version != 0");
228                         return sys_msgrcv (first,
229                                            (struct msgbuf *) ptr,
230                                            second, fifth, third);
231                 }
232         case MSGGET:
233                 return sys_msgget ((key_t) first, second);
234         case MSGCTL:
235                 return sys_msgctl (first, second, (struct msqid_ds *) ptr);
236
237         case SHMAT:
238                 switch (version) {
239                 default: {
240                         ulong raddr;
241                         ret = do_shmat (first, (char *) ptr, second, &raddr);
242                         if (ret)
243                                 return ret;
244                         return put_user (raddr, (ulong *) third);
245                 }
246                 case 1: /* iBCS2 emulator entry point */
247                         if (!segment_eq(get_fs(), get_ds()))
248                                 return -EINVAL;
249                         return do_shmat (first, (char *) ptr, second, (ulong *) third);
250                 }
251         case SHMDT: 
252                 return sys_shmdt ((char *)ptr);
253         case SHMGET:
254                 return sys_shmget (first, second, third);
255         case SHMCTL:
256                 return sys_shmctl (first, second,
257                                    (struct shmid_ds *) ptr);
258         default:
259                 return -EINVAL;
260         }
261 }
262
263 int sys_uname(struct old_utsname * name)
264 {
265         int err;
266         if (!name)
267                 return -EFAULT;
268         down_read(&uts_sem);
269         err=copy_to_user(name, vx_new_utsname(), sizeof (*name));
270         up_read(&uts_sem);
271         return err?-EFAULT:0;
272 }
273
274 int sys_olduname(struct oldold_utsname * name)
275 {
276         int error;
277         struct new_utsname *ptr;
278
279         if (!name)
280                 return -EFAULT;
281         if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
282                 return -EFAULT;
283   
284         down_read(&uts_sem);
285         
286         ptr = vx_new_utsname();
287         error = __copy_to_user(&name->sysname,ptr->sysname,
288                                __OLD_UTS_LEN);
289         error |= __put_user(0,name->sysname+__OLD_UTS_LEN);
290         error |= __copy_to_user(&name->nodename,ptr->nodename,
291                                 __OLD_UTS_LEN);
292         error |= __put_user(0,name->nodename+__OLD_UTS_LEN);
293         error |= __copy_to_user(&name->release,ptr->release,
294                                 __OLD_UTS_LEN);
295         error |= __put_user(0,name->release+__OLD_UTS_LEN);
296         error |= __copy_to_user(&name->version,ptr->version,
297                                 __OLD_UTS_LEN);
298         error |= __put_user(0,name->version+__OLD_UTS_LEN);
299         error |= __copy_to_user(&name->machine,ptr->machine,
300                                 __OLD_UTS_LEN);
301         error |= __put_user(0,name->machine+__OLD_UTS_LEN);
302         
303         up_read(&uts_sem);
304         
305         error = error ? -EFAULT : 0;
306
307         return error;
308 }
309
310 int sys_sigaltstack(const stack_t *uss, stack_t *uoss)
311 {
312         return(do_sigaltstack(uss, uoss, PT_REGS_SP(&current->thread.regs)));
313 }
314
315 long execute_syscall(void *r)
316 {
317         return(CHOOSE_MODE_PROC(execute_syscall_tt, execute_syscall_skas, r));
318 }
319
320 spinlock_t syscall_lock = SPIN_LOCK_UNLOCKED;
321
322 static int syscall_index = 0;
323
324 int next_syscall_index(int limit)
325 {
326         int ret;
327
328         spin_lock(&syscall_lock);
329         ret = syscall_index;
330         if(++syscall_index == limit)
331                 syscall_index = 0;
332         spin_unlock(&syscall_lock);
333         return(ret);
334 }
335
336 /*
337  * Overrides for Emacs so that we follow Linus's tabbing style.
338  * Emacs will notice this stuff at the end of the file and automatically
339  * adjust the settings for this buffer only.  This must remain at the end
340  * of the file.
341  * ---------------------------------------------------------------------------
342  * Local variables:
343  * c-file-style: "linux"
344  * End:
345  */