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