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