vserver 1.9.3
[linux-2.6.git] / arch / um / kernel / syscall_kern.c
1 /* 
2  * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.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         long ret;
42
43         current->thread.forking = 1;
44         ret = do_fork(SIGCHLD, 0, NULL, 0, NULL, NULL);
45         current->thread.forking = 0;
46         return(ret);
47 }
48
49 long sys_vfork(void)
50 {
51         long ret;
52
53         current->thread.forking = 1;
54         ret = do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0, NULL, 0, NULL,
55                       NULL);
56         current->thread.forking = 0;
57         return(ret);
58 }
59
60 /* common code for old and new mmaps */
61 static inline long do_mmap2(
62         unsigned long addr, unsigned long len,
63         unsigned long prot, unsigned long flags,
64         unsigned long fd, unsigned long pgoff)
65 {
66         int error = -EBADF;
67         struct file * file = NULL;
68
69         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
70         if (!(flags & MAP_ANONYMOUS)) {
71                 file = fget(fd);
72                 if (!file)
73                         goto out;
74         }
75
76         down_write(&current->mm->mmap_sem);
77         error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
78         up_write(&current->mm->mmap_sem);
79
80         if (file)
81                 fput(file);
82  out:
83         return error;
84 }
85
86 long sys_mmap2(unsigned long addr, unsigned long len,
87                unsigned long prot, unsigned long flags,
88                unsigned long fd, unsigned long pgoff)
89 {
90         return do_mmap2(addr, len, prot, flags, fd, pgoff);
91 }
92
93 /*
94  * Perform the select(nd, in, out, ex, tv) and mmap() system
95  * calls. Linux/i386 didn't use to be able to handle more than
96  * 4 system call parameters, so these system calls used a memory
97  * block for parameter passing..
98  */
99
100 struct mmap_arg_struct {
101         unsigned long addr;
102         unsigned long len;
103         unsigned long prot;
104         unsigned long flags;
105         unsigned long fd;
106         unsigned long offset;
107 };
108
109 int old_mmap(unsigned long addr, unsigned long len,
110              unsigned long prot, unsigned long flags,
111              unsigned long fd, unsigned long offset)
112 {
113         int err = -EINVAL;
114         if (offset & ~PAGE_MASK)
115                 goto out;
116
117         err = do_mmap2(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
118  out:
119         return err;
120 }
121 /*
122  * sys_pipe() is the normal C calling standard for creating
123  * a pipe. It's not the way unix traditionally does this, though.
124  */
125 int sys_pipe(unsigned long * fildes)
126 {
127         int fd[2];
128         int error;
129
130         error = do_pipe(fd);
131         if (!error) {
132                 if (copy_to_user(fildes, fd, sizeof(fd)))
133                         error = -EFAULT;
134         }
135         return error;
136 }
137
138 /*
139  * sys_ipc() is the de-multiplexer for the SysV IPC calls..
140  *
141  * This is really horribly ugly.
142  */
143 int sys_ipc (uint call, int first, int second,
144              int third, void *ptr, long fifth)
145 {
146         int version, ret;
147
148         version = call >> 16; /* hack for backward compatibility */
149         call &= 0xffff;
150
151         switch (call) {
152         case SEMOP:
153                 return sys_semtimedop(first, (struct sembuf *) ptr, second, 
154                                       NULL);
155         case SEMTIMEDOP:
156                 return sys_semtimedop(first, (struct sembuf *) ptr, second,
157                                       (const struct timespec *) fifth);
158         case SEMGET:
159                 return sys_semget (first, second, third);
160         case SEMCTL: {
161                 union semun fourth;
162                 if (!ptr)
163                         return -EINVAL;
164                 if (get_user(fourth.__pad, (void **) ptr))
165                         return -EFAULT;
166                 return sys_semctl (first, second, third, fourth);
167         }
168
169         case MSGSND:
170                 return sys_msgsnd (first, (struct msgbuf *) ptr, 
171                                    second, third);
172         case MSGRCV:
173                 switch (version) {
174                 case 0: {
175                         struct ipc_kludge tmp;
176                         if (!ptr)
177                                 return -EINVAL;
178                         
179                         if (copy_from_user(&tmp,
180                                            (struct ipc_kludge *) ptr, 
181                                            sizeof (tmp)))
182                                 return -EFAULT;
183                         return sys_msgrcv (first, tmp.msgp, second,
184                                            tmp.msgtyp, third);
185                 }
186                 default:
187                         panic("msgrcv with version != 0");
188                         return sys_msgrcv (first,
189                                            (struct msgbuf *) ptr,
190                                            second, fifth, third);
191                 }
192         case MSGGET:
193                 return sys_msgget ((key_t) first, second);
194         case MSGCTL:
195                 return sys_msgctl (first, second, (struct msqid_ds *) ptr);
196
197         case SHMAT:
198                 switch (version) {
199                 default: {
200                         ulong raddr;
201                         ret = do_shmat (first, (char *) ptr, second, &raddr);
202                         if (ret)
203                                 return ret;
204                         return put_user (raddr, (ulong *) third);
205                 }
206                 case 1: /* iBCS2 emulator entry point */
207                         if (!segment_eq(get_fs(), get_ds()))
208                                 return -EINVAL;
209                         return do_shmat (first, (char *) ptr, second, (ulong *) third);
210                 }
211         case SHMDT: 
212                 return sys_shmdt ((char *)ptr);
213         case SHMGET:
214                 return sys_shmget (first, second, third);
215         case SHMCTL:
216                 return sys_shmctl (first, second,
217                                    (struct shmid_ds *) ptr);
218         default:
219                 return -ENOSYS;
220         }
221 }
222
223 int sys_uname(struct old_utsname * name)
224 {
225         int err;
226         if (!name)
227                 return -EFAULT;
228         down_read(&uts_sem);
229         err=copy_to_user(name, vx_new_utsname(), sizeof (*name));
230         up_read(&uts_sem);
231         return err?-EFAULT:0;
232 }
233
234 int sys_olduname(struct oldold_utsname * name)
235 {
236         int error;
237         struct new_utsname *ptr;
238
239         if (!name)
240                 return -EFAULT;
241         if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
242                 return -EFAULT;
243   
244         down_read(&uts_sem);
245         
246         ptr = vx_new_utsname();
247         error = __copy_to_user(&name->sysname,ptr->sysname,
248                                __OLD_UTS_LEN);
249         error |= __put_user(0,name->sysname+__OLD_UTS_LEN);
250         error |= __copy_to_user(&name->nodename,ptr->nodename,
251                                 __OLD_UTS_LEN);
252         error |= __put_user(0,name->nodename+__OLD_UTS_LEN);
253         error |= __copy_to_user(&name->release,ptr->release,
254                                 __OLD_UTS_LEN);
255         error |= __put_user(0,name->release+__OLD_UTS_LEN);
256         error |= __copy_to_user(&name->version,ptr->version,
257                                 __OLD_UTS_LEN);
258         error |= __put_user(0,name->version+__OLD_UTS_LEN);
259         error |= __copy_to_user(&name->machine,ptr->machine,
260                                 __OLD_UTS_LEN);
261         error |= __put_user(0,name->machine+__OLD_UTS_LEN);
262         
263         up_read(&uts_sem);
264         
265         error = error ? -EFAULT : 0;
266
267         return error;
268 }
269
270 long execute_syscall(void *r)
271 {
272         return(CHOOSE_MODE_PROC(execute_syscall_tt, execute_syscall_skas, r));
273 }
274
275 spinlock_t syscall_lock = SPIN_LOCK_UNLOCKED;
276
277 static int syscall_index = 0;
278
279 int next_syscall_index(int limit)
280 {
281         int ret;
282
283         spin_lock(&syscall_lock);
284         ret = syscall_index;
285         if(++syscall_index == limit)
286                 syscall_index = 0;
287         spin_unlock(&syscall_lock);
288         return(ret);
289 }
290
291 /*
292  * Overrides for Emacs so that we follow Linus's tabbing style.
293  * Emacs will notice this stuff at the end of the file and automatically
294  * adjust the settings for this buffer only.  This must remain at the end
295  * of the file.
296  * ---------------------------------------------------------------------------
297  * Local variables:
298  * c-file-style: "linux"
299  * End:
300  */