patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / s390 / kernel / compat_linux.c
1 /*
2  *  arch/s390x/kernel/linux32.c
3  *
4  *  S390 version
5  *    Copyright (C) 2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
6  *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
7  *               Gerhard Tonn (ton@de.ibm.com)   
8  *               Thomas Spatzier (tspat@de.ibm.com)
9  *
10  *  Conversion between 31bit and 64bit native syscalls.
11  *
12  * Heavily inspired by the 32-bit Sparc compat code which is 
13  * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
14  * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
15  *
16  */
17
18
19 #include <linux/config.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/fs.h> 
23 #include <linux/mm.h> 
24 #include <linux/file.h> 
25 #include <linux/signal.h>
26 #include <linux/resource.h>
27 #include <linux/times.h>
28 #include <linux/utsname.h>
29 #include <linux/timex.h>
30 #include <linux/smp.h>
31 #include <linux/smp_lock.h>
32 #include <linux/sem.h>
33 #include <linux/msg.h>
34 #include <linux/shm.h>
35 #include <linux/slab.h>
36 #include <linux/uio.h>
37 #include <linux/nfs_fs.h>
38 #include <linux/quota.h>
39 #include <linux/module.h>
40 #include <linux/sunrpc/svc.h>
41 #include <linux/nfsd/nfsd.h>
42 #include <linux/nfsd/cache.h>
43 #include <linux/nfsd/xdr.h>
44 #include <linux/nfsd/syscall.h>
45 #include <linux/poll.h>
46 #include <linux/personality.h>
47 #include <linux/stat.h>
48 #include <linux/filter.h>
49 #include <linux/highmem.h>
50 #include <linux/highuid.h>
51 #include <linux/mman.h>
52 #include <linux/ipv6.h>
53 #include <linux/in.h>
54 #include <linux/icmpv6.h>
55 #include <linux/syscalls.h>
56 #include <linux/sysctl.h>
57 #include <linux/binfmts.h>
58 #include <linux/compat.h>
59 #include <linux/vfs.h>
60 #include <linux/ptrace.h>
61
62 #include <asm/types.h>
63 #include <asm/ipc.h>
64 #include <asm/uaccess.h>
65 #include <asm/semaphore.h>
66
67 #include <net/scm.h>
68 #include <net/sock.h>
69
70 #include "compat_linux.h"
71
72  
73 /* For this source file, we want overflow handling. */
74
75 #undef high2lowuid
76 #undef high2lowgid
77 #undef low2highuid
78 #undef low2highgid
79 #undef SET_UID16
80 #undef SET_GID16
81 #undef NEW_TO_OLD_UID
82 #undef NEW_TO_OLD_GID
83 #undef SET_OLDSTAT_UID
84 #undef SET_OLDSTAT_GID
85 #undef SET_STAT_UID
86 #undef SET_STAT_GID
87
88 #define high2lowuid(uid) ((uid) > 65535) ? (u16)overflowuid : (u16)(uid)
89 #define high2lowgid(gid) ((gid) > 65535) ? (u16)overflowgid : (u16)(gid)
90 #define low2highuid(uid) ((uid) == (u16)-1) ? (uid_t)-1 : (uid_t)(uid)
91 #define low2highgid(gid) ((gid) == (u16)-1) ? (gid_t)-1 : (gid_t)(gid)
92 #define SET_UID16(var, uid)     var = high2lowuid(uid)
93 #define SET_GID16(var, gid)     var = high2lowgid(gid)
94 #define NEW_TO_OLD_UID(uid)     high2lowuid(uid)
95 #define NEW_TO_OLD_GID(gid)     high2lowgid(gid)
96 #define SET_OLDSTAT_UID(stat, uid)      (stat).st_uid = high2lowuid(uid)
97 #define SET_OLDSTAT_GID(stat, gid)      (stat).st_gid = high2lowgid(gid)
98 #define SET_STAT_UID(stat, uid)         (stat).st_uid = high2lowuid(uid)
99 #define SET_STAT_GID(stat, gid)         (stat).st_gid = high2lowgid(gid)
100
101 asmlinkage long sys32_chown16(const char * filename, u16 user, u16 group)
102 {
103         return sys_chown(filename, low2highuid(user), low2highgid(group));
104 }
105
106 asmlinkage long sys32_lchown16(const char * filename, u16 user, u16 group)
107 {
108         return sys_lchown(filename, low2highuid(user), low2highgid(group));
109 }
110
111 asmlinkage long sys32_fchown16(unsigned int fd, u16 user, u16 group)
112 {
113         return sys_fchown(fd, low2highuid(user), low2highgid(group));
114 }
115
116 asmlinkage long sys32_setregid16(u16 rgid, u16 egid)
117 {
118         return sys_setregid(low2highgid(rgid), low2highgid(egid));
119 }
120
121 asmlinkage long sys32_setgid16(u16 gid)
122 {
123         return sys_setgid((gid_t)gid);
124 }
125
126 asmlinkage long sys32_setreuid16(u16 ruid, u16 euid)
127 {
128         return sys_setreuid(low2highuid(ruid), low2highuid(euid));
129 }
130
131 asmlinkage long sys32_setuid16(u16 uid)
132 {
133         return sys_setuid((uid_t)uid);
134 }
135
136 asmlinkage long sys32_setresuid16(u16 ruid, u16 euid, u16 suid)
137 {
138         return sys_setresuid(low2highuid(ruid), low2highuid(euid),
139                 low2highuid(suid));
140 }
141
142 asmlinkage long sys32_getresuid16(u16 *ruid, u16 *euid, u16 *suid)
143 {
144         int retval;
145
146         if (!(retval = put_user(high2lowuid(current->uid), ruid)) &&
147             !(retval = put_user(high2lowuid(current->euid), euid)))
148                 retval = put_user(high2lowuid(current->suid), suid);
149
150         return retval;
151 }
152
153 asmlinkage long sys32_setresgid16(u16 rgid, u16 egid, u16 sgid)
154 {
155         return sys_setresgid(low2highgid(rgid), low2highgid(egid),
156                 low2highgid(sgid));
157 }
158
159 asmlinkage long sys32_getresgid16(u16 *rgid, u16 *egid, u16 *sgid)
160 {
161         int retval;
162
163         if (!(retval = put_user(high2lowgid(current->gid), rgid)) &&
164             !(retval = put_user(high2lowgid(current->egid), egid)))
165                 retval = put_user(high2lowgid(current->sgid), sgid);
166
167         return retval;
168 }
169
170 asmlinkage long sys32_setfsuid16(u16 uid)
171 {
172         return sys_setfsuid((uid_t)uid);
173 }
174
175 asmlinkage long sys32_setfsgid16(u16 gid)
176 {
177         return sys_setfsgid((gid_t)gid);
178 }
179
180 static int groups16_to_user(u16 *grouplist, struct group_info *group_info)
181 {
182         int i;
183         u16 group;
184
185         for (i = 0; i < group_info->ngroups; i++) {
186                 group = (u16)GROUP_AT(group_info, i);
187                 if (put_user(group, grouplist+i))
188                         return -EFAULT;
189         }
190
191         return 0;
192 }
193
194 static int groups16_from_user(struct group_info *group_info, u16 *grouplist)
195 {
196         int i;
197         u16 group;
198
199         for (i = 0; i < group_info->ngroups; i++) {
200                 if (get_user(group, grouplist+i))
201                         return  -EFAULT;
202                 GROUP_AT(group_info, i) = (gid_t)group;
203         }
204
205         return 0;
206 }
207
208 asmlinkage long sys32_getgroups16(int gidsetsize, u16 *grouplist)
209 {
210         int i;
211
212         if (gidsetsize < 0)
213                 return -EINVAL;
214
215         get_group_info(current->group_info);
216         i = current->group_info->ngroups;
217         if (gidsetsize) {
218                 if (i > gidsetsize) {
219                         i = -EINVAL;
220                         goto out;
221                 }
222                 if (groups16_to_user(grouplist, current->group_info)) {
223                         i = -EFAULT;
224                         goto out;
225                 }
226         }
227 out:
228         put_group_info(current->group_info);
229         return i;
230 }
231
232 asmlinkage long sys32_setgroups16(int gidsetsize, u16 *grouplist)
233 {
234         struct group_info *group_info;
235         int retval;
236
237         if (!capable(CAP_SETGID))
238                 return -EPERM;
239         if ((unsigned)gidsetsize > NGROUPS_MAX)
240                 return -EINVAL;
241
242         group_info = groups_alloc(gidsetsize);
243         if (!group_info)
244                 return -ENOMEM;
245         retval = groups16_from_user(group_info, grouplist);
246         if (retval) {
247                 put_group_info(group_info);
248                 return retval;
249         }
250
251         retval = set_current_groups(group_info);
252         put_group_info(group_info);
253
254         return retval;
255 }
256
257 asmlinkage long sys32_getuid16(void)
258 {
259         return high2lowuid(current->uid);
260 }
261
262 asmlinkage long sys32_geteuid16(void)
263 {
264         return high2lowuid(current->euid);
265 }
266
267 asmlinkage long sys32_getgid16(void)
268 {
269         return high2lowgid(current->gid);
270 }
271
272 asmlinkage long sys32_getegid16(void)
273 {
274         return high2lowgid(current->egid);
275 }
276
277 /* 32-bit timeval and related flotsam.  */
278
279 static inline long get_tv32(struct timeval *o, struct compat_timeval *i)
280 {
281         return (!access_ok(VERIFY_READ, tv32, sizeof(*tv32)) ||
282                 (__get_user(o->tv_sec, &i->tv_sec) ||
283                  __get_user(o->tv_usec, &i->tv_usec)));
284 }
285
286 static inline long put_tv32(struct compat_timeval *o, struct timeval *i)
287 {
288         return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
289                 (__put_user(i->tv_sec, &o->tv_sec) ||
290                  __put_user(i->tv_usec, &o->tv_usec)));
291 }
292
293 /*
294  * sys32_ipc() is the de-multiplexer for the SysV IPC calls in 32bit emulation.
295  *
296  * This is really horribly ugly.
297  */
298 asmlinkage long sys32_ipc(u32 call, int first, int second, int third, u32 ptr)
299 {
300         if (call >> 16)         /* hack for backward compatibility */
301                 return -EINVAL;
302
303         call &= 0xffff;
304
305         switch (call) {
306         case SEMTIMEDOP:
307                 return compat_sys_semtimedop(first, compat_ptr(ptr),
308                                              second, compat_ptr(third));
309         case SEMOP:
310                 /* struct sembuf is the same on 32 and 64bit :)) */
311                 return sys_semtimedop(first, compat_ptr(ptr),
312                                       second, NULL);
313         case SEMGET:
314                 return sys_semget(first, second, third);
315         case SEMCTL:
316                 return compat_sys_semctl(first, second, third,
317                                          compat_ptr(ptr));
318         case MSGSND:
319                 return compat_sys_msgsnd(first, second, third,
320                                          compat_ptr(ptr));
321         case MSGRCV:
322                 return compat_sys_msgrcv(first, second, 0, third,
323                                          0, compat_ptr(ptr));
324         case MSGGET:
325                 return sys_msgget((key_t) first, second);
326         case MSGCTL:
327                 return compat_sys_msgctl(first, second, compat_ptr(ptr));
328         case SHMAT:
329                 return compat_sys_shmat(first, second, third,
330                                         0, compat_ptr(ptr));
331         case SHMDT:
332                 return sys_shmdt(compat_ptr(ptr));
333         case SHMGET:
334                 return sys_shmget(first, second, third);
335         case SHMCTL:
336                 return compat_sys_shmctl(first, second, compat_ptr(ptr));
337         }
338
339         return -ENOSYS;
340 }
341
342 asmlinkage long sys32_truncate64(const char * path, unsigned long high, unsigned long low)
343 {
344         if ((int)high < 0)
345                 return -EINVAL;
346         else
347                 return sys_truncate(path, (high << 32) | low);
348 }
349
350 asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long high, unsigned long low)
351 {
352         if ((int)high < 0)
353                 return -EINVAL;
354         else
355                 return sys_ftruncate(fd, (high << 32) | low);
356 }
357
358 /* readdir & getdents */
359
360 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
361 #define ROUND_UP(x) (((x)+sizeof(u32)-1) & ~(sizeof(u32)-1))
362
363 struct old_linux_dirent32 {
364         u32             d_ino;
365         u32             d_offset;
366         unsigned short  d_namlen;
367         char            d_name[1];
368 };
369
370 struct readdir_callback32 {
371         struct old_linux_dirent32 * dirent;
372         int count;
373 };
374
375 static int fillonedir(void * __buf, const char * name, int namlen,
376                       loff_t offset, ino_t ino, unsigned int d_type)
377 {
378         struct readdir_callback32 * buf = (struct readdir_callback32 *) __buf;
379         struct old_linux_dirent32 * dirent;
380
381         if (buf->count)
382                 return -EINVAL;
383         buf->count++;
384         dirent = buf->dirent;
385         put_user(ino, &dirent->d_ino);
386         put_user(offset, &dirent->d_offset);
387         put_user(namlen, &dirent->d_namlen);
388         copy_to_user(dirent->d_name, name, namlen);
389         put_user(0, dirent->d_name + namlen);
390         return 0;
391 }
392
393 asmlinkage long old32_readdir(unsigned int fd, struct old_linux_dirent32 *dirent, unsigned int count)
394 {
395         int error = -EBADF;
396         struct file * file;
397         struct readdir_callback32 buf;
398
399         file = fget(fd);
400         if (!file)
401                 goto out;
402
403         buf.count = 0;
404         buf.dirent = dirent;
405
406         error = vfs_readdir(file, fillonedir, &buf);
407         if (error < 0)
408                 goto out_putf;
409         error = buf.count;
410
411 out_putf:
412         fput(file);
413 out:
414         return error;
415 }
416
417 struct linux_dirent32 {
418         u32             d_ino;
419         u32             d_off;
420         unsigned short  d_reclen;
421         char            d_name[1];
422 };
423
424 struct getdents_callback32 {
425         struct linux_dirent32 * current_dir;
426         struct linux_dirent32 * previous;
427         int count;
428         int error;
429 };
430
431 static int filldir(void * __buf, const char * name, int namlen, loff_t offset, ino_t ino,
432                    unsigned int d_type)
433 {
434         struct linux_dirent32 * dirent;
435         struct getdents_callback32 * buf = (struct getdents_callback32 *) __buf;
436         int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 1);
437
438         buf->error = -EINVAL;   /* only used if we fail.. */
439         if (reclen > buf->count)
440                 return -EINVAL;
441         dirent = buf->previous;
442         if (dirent)
443                 put_user(offset, &dirent->d_off);
444         dirent = buf->current_dir;
445         buf->previous = dirent;
446         put_user(ino, &dirent->d_ino);
447         put_user(reclen, &dirent->d_reclen);
448         copy_to_user(dirent->d_name, name, namlen);
449         put_user(0, dirent->d_name + namlen);
450         buf->current_dir = ((void *)dirent) + reclen;
451         buf->count -= reclen;
452         return 0;
453 }
454
455 asmlinkage long sys32_getdents(unsigned int fd, struct linux_dirent32 *dirent, unsigned int count)
456 {
457         struct file * file;
458         struct linux_dirent32 * lastdirent;
459         struct getdents_callback32 buf;
460         int error = -EBADF;
461
462         file = fget(fd);
463         if (!file)
464                 goto out;
465
466         buf.current_dir = dirent;
467         buf.previous = NULL;
468         buf.count = count;
469         buf.error = 0;
470
471         error = vfs_readdir(file, filldir, &buf);
472         if (error < 0)
473                 goto out_putf;
474         lastdirent = buf.previous;
475         error = buf.error;
476         if(lastdirent) {
477                 put_user(file->f_pos, &lastdirent->d_off);
478                 error = count - buf.count;
479         }
480 out_putf:
481         fput(file);
482 out:
483         return error;
484 }
485
486 /* end of readdir & getdents */
487
488 int cp_compat_stat(struct kstat *stat, struct compat_stat *statbuf)
489 {
490         int err;
491
492         if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
493                 return -EOVERFLOW;
494
495         err = put_user(old_encode_dev(stat->dev), &statbuf->st_dev);
496         err |= put_user(stat->ino, &statbuf->st_ino);
497         err |= put_user(stat->mode, &statbuf->st_mode);
498         err |= put_user(stat->nlink, &statbuf->st_nlink);
499         err |= put_user(high2lowuid(stat->uid), &statbuf->st_uid);
500         err |= put_user(high2lowgid(stat->gid), &statbuf->st_gid);
501         err |= put_user(old_encode_dev(stat->rdev), &statbuf->st_rdev);
502         err |= put_user(stat->size, &statbuf->st_size);
503         err |= put_user(stat->atime.tv_sec, &statbuf->st_atime);
504         err |= put_user(stat->atime.tv_nsec, &statbuf->st_atime_nsec);
505         err |= put_user(stat->mtime.tv_sec, &statbuf->st_mtime);
506         err |= put_user(stat->mtime.tv_nsec, &statbuf->st_mtime_nsec);
507         err |= put_user(stat->ctime.tv_sec, &statbuf->st_ctime);
508         err |= put_user(stat->ctime.tv_nsec, &statbuf->st_ctime_nsec);
509         err |= put_user(stat->blksize, &statbuf->st_blksize);
510         err |= put_user(stat->blocks, &statbuf->st_blocks);
511 /* fixme
512         err |= put_user(0, &statbuf->__unused4[0]);
513         err |= put_user(0, &statbuf->__unused4[1]);
514 */
515         return err;
516 }
517
518 struct sysinfo32 {
519         s32 uptime;
520         u32 loads[3];
521         u32 totalram;
522         u32 freeram;
523         u32 sharedram;
524         u32 bufferram;
525         u32 totalswap;
526         u32 freeswap;
527         unsigned short procs;
528         unsigned short pads;
529         u32 totalhigh;
530         u32 freehigh;
531         unsigned int mem_unit;
532         char _f[8];
533 };
534
535 asmlinkage long sys32_sysinfo(struct sysinfo32 __user *info)
536 {
537         struct sysinfo s;
538         int ret, err;
539         mm_segment_t old_fs = get_fs ();
540         
541         set_fs (KERNEL_DS);
542         ret = sys_sysinfo(&s);
543         set_fs (old_fs);
544         err = put_user (s.uptime, &info->uptime);
545         err |= __put_user (s.loads[0], &info->loads[0]);
546         err |= __put_user (s.loads[1], &info->loads[1]);
547         err |= __put_user (s.loads[2], &info->loads[2]);
548         err |= __put_user (s.totalram, &info->totalram);
549         err |= __put_user (s.freeram, &info->freeram);
550         err |= __put_user (s.sharedram, &info->sharedram);
551         err |= __put_user (s.bufferram, &info->bufferram);
552         err |= __put_user (s.totalswap, &info->totalswap);
553         err |= __put_user (s.freeswap, &info->freeswap);
554         err |= __put_user (s.procs, &info->procs);
555         err |= __put_user (s.totalhigh, &info->totalhigh);
556         err |= __put_user (s.freehigh, &info->freehigh);
557         err |= __put_user (s.mem_unit, &info->mem_unit);
558         if (err)
559                 return -EFAULT;
560         return ret;
561 }
562
563 asmlinkage long sys32_sched_rr_get_interval(compat_pid_t pid,
564                                 struct compat_timespec __user *interval)
565 {
566         struct timespec t;
567         int ret;
568         mm_segment_t old_fs = get_fs ();
569         
570         set_fs (KERNEL_DS);
571         ret = sys_sched_rr_get_interval(pid, &t);
572         set_fs (old_fs);
573         if (put_compat_timespec(&t, interval))
574                 return -EFAULT;
575         return ret;
576 }
577
578 asmlinkage long sys32_rt_sigprocmask(int how, compat_sigset_t __user *set,
579                         compat_sigset_t __user *oset, size_t sigsetsize)
580 {
581         sigset_t s;
582         compat_sigset_t s32;
583         int ret;
584         mm_segment_t old_fs = get_fs();
585         
586         if (set) {
587                 if (copy_from_user (&s32, set, sizeof(compat_sigset_t)))
588                         return -EFAULT;
589                 switch (_NSIG_WORDS) {
590                 case 4: s.sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
591                 case 3: s.sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
592                 case 2: s.sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
593                 case 1: s.sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
594                 }
595         }
596         set_fs (KERNEL_DS);
597         ret = sys_rt_sigprocmask(how, set ? &s : NULL, oset ? &s : NULL, sigsetsize);
598         set_fs (old_fs);
599         if (ret) return ret;
600         if (oset) {
601                 switch (_NSIG_WORDS) {
602                 case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
603                 case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
604                 case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
605                 case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
606                 }
607                 if (copy_to_user (oset, &s32, sizeof(compat_sigset_t)))
608                         return -EFAULT;
609         }
610         return 0;
611 }
612
613 asmlinkage long sys32_rt_sigpending(compat_sigset_t __user *set,
614                                 size_t sigsetsize)
615 {
616         sigset_t s;
617         compat_sigset_t s32;
618         int ret;
619         mm_segment_t old_fs = get_fs();
620                 
621         set_fs (KERNEL_DS);
622         ret = sys_rt_sigpending(&s, sigsetsize);
623         set_fs (old_fs);
624         if (!ret) {
625                 switch (_NSIG_WORDS) {
626                 case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
627                 case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
628                 case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
629                 case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
630                 }
631                 if (copy_to_user (set, &s32, sizeof(compat_sigset_t)))
632                         return -EFAULT;
633         }
634         return ret;
635 }
636
637 extern int
638 copy_siginfo_to_user32(siginfo_t32 *to, siginfo_t *from);
639
640 asmlinkage long
641 sys32_rt_sigtimedwait(compat_sigset_t *uthese, siginfo_t32 *uinfo,
642                       struct compat_timespec *uts, size_t sigsetsize)
643 {
644         int ret, sig;
645         sigset_t these;
646         compat_sigset_t these32;
647         struct timespec ts;
648         siginfo_t info;
649         long timeout = 0;
650
651         /* XXX: Don't preclude handling different sized sigset_t's.  */
652         if (sigsetsize != sizeof(sigset_t))
653                 return -EINVAL;
654
655         if (copy_from_user (&these32, uthese, sizeof(compat_sigset_t)))
656                 return -EFAULT;
657
658         switch (_NSIG_WORDS) {
659         case 4: these.sig[3] = these32.sig[6] | (((long)these32.sig[7]) << 32);
660         case 3: these.sig[2] = these32.sig[4] | (((long)these32.sig[5]) << 32);
661         case 2: these.sig[1] = these32.sig[2] | (((long)these32.sig[3]) << 32);
662         case 1: these.sig[0] = these32.sig[0] | (((long)these32.sig[1]) << 32);
663         }
664                 
665         /*
666          * Invert the set of allowed signals to get those we
667          * want to block.
668          */
669         sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
670         signotset(&these);
671
672         if (uts) {
673                 if (get_compat_timespec(&ts, uts))
674                         return -EINVAL;
675                 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
676                     || ts.tv_sec < 0)
677                         return -EINVAL;
678         }
679
680         spin_lock_irq(&current->sighand->siglock);
681         sig = dequeue_signal(current, &these, &info);
682         if (!sig) {
683                 /* None ready -- temporarily unblock those we're interested
684                    in so that we'll be awakened when they arrive.  */
685                 current->real_blocked = current->blocked;
686                 sigandsets(&current->blocked, &current->blocked, &these);
687                 recalc_sigpending();
688                 spin_unlock_irq(&current->sighand->siglock);
689
690                 timeout = MAX_SCHEDULE_TIMEOUT;
691                 if (uts)
692                         timeout = (timespec_to_jiffies(&ts)
693                                    + (ts.tv_sec || ts.tv_nsec));
694
695                 current->state = TASK_INTERRUPTIBLE;
696                 timeout = schedule_timeout(timeout);
697
698                 spin_lock_irq(&current->sighand->siglock);
699                 sig = dequeue_signal(current, &these, &info);
700                 current->blocked = current->real_blocked;
701                 siginitset(&current->real_blocked, 0);
702                 recalc_sigpending();
703         }
704         spin_unlock_irq(&current->sighand->siglock);
705
706         if (sig) {
707                 ret = sig;
708                 if (uinfo) {
709                         if (copy_siginfo_to_user32(uinfo, &info))
710                                 ret = -EFAULT;
711                 }
712         } else {
713                 ret = -EAGAIN;
714                 if (timeout)
715                         ret = -EINTR;
716         }
717
718         return ret;
719 }
720
721 asmlinkage long
722 sys32_rt_sigqueueinfo(int pid, int sig, siginfo_t32 __user *uinfo)
723 {
724         siginfo_t info;
725         int ret;
726         mm_segment_t old_fs = get_fs();
727         
728         if (copy_from_user (&info, uinfo, 3*sizeof(int)) ||
729             copy_from_user (info._sifields._pad, uinfo->_sifields._pad, SI_PAD_SIZE))
730                 return -EFAULT;
731         set_fs (KERNEL_DS);
732         ret = sys_rt_sigqueueinfo(pid, sig, &info);
733         set_fs (old_fs);
734         return ret;
735 }
736
737 /*
738  * sys32_execve() executes a new program after the asm stub has set
739  * things up for us.  This should basically do what I want it to.
740  */
741 asmlinkage long
742 sys32_execve(struct pt_regs regs)
743 {
744         int error;
745         char * filename;
746
747         filename = getname(compat_ptr(regs.orig_gpr2));
748         error = PTR_ERR(filename);
749         if (IS_ERR(filename))
750                 goto out;
751         error = compat_do_execve(filename, compat_ptr(regs.gprs[3]),
752                                  compat_ptr(regs.gprs[4]), &regs);
753         if (error == 0)
754         {
755                 current->ptrace &= ~PT_DTRACE;
756                 current->thread.fp_regs.fpc=0;
757                 __asm__ __volatile__
758                         ("sr  0,0\n\t"
759                          "sfpc 0,0\n\t"
760                          : : :"0");
761         }
762         putname(filename);
763 out:
764         return error;
765 }
766
767
768 #ifdef CONFIG_MODULES
769
770 asmlinkage long
771 sys32_init_module(void __user *umod, unsigned long len,
772                 const char __user *uargs)
773 {
774         return sys_init_module(umod, len, uargs);
775 }
776
777 asmlinkage long
778 sys32_delete_module(const char __user *name_user, unsigned int flags)
779 {
780         return sys_delete_module(name_user, flags);
781 }
782
783 #else /* CONFIG_MODULES */
784
785 asmlinkage long
786 sys32_init_module(void __user *umod, unsigned long len,
787                 const char __user *uargs)
788 {
789         return -ENOSYS;
790 }
791
792 asmlinkage long
793 sys32_delete_module(const char __user *name_user, unsigned int flags)
794 {
795         return -ENOSYS;
796 }
797
798 #endif  /* CONFIG_MODULES */
799
800 /* Translations due to time_t size differences.  Which affects all
801    sorts of things, like timeval and itimerval.  */
802
803 extern struct timezone sys_tz;
804
805 asmlinkage long sys32_gettimeofday(struct compat_timeval *tv, struct timezone *tz)
806 {
807         if (tv) {
808                 struct timeval ktv;
809                 do_gettimeofday(&ktv);
810                 if (put_tv32(tv, &ktv))
811                         return -EFAULT;
812         }
813         if (tz) {
814                 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
815                         return -EFAULT;
816         }
817         return 0;
818 }
819
820 static inline long get_ts32(struct timespec *o, struct compat_timeval *i)
821 {
822         long usec;
823
824         if (!access_ok(VERIFY_READ, i, sizeof(*i)))
825                 return -EFAULT;
826         if (__get_user(o->tv_sec, &i->tv_sec))
827                 return -EFAULT;
828         if (__get_user(usec, &i->tv_usec))
829                 return -EFAULT;
830         o->tv_nsec = usec * 1000;
831         return 0;
832 }
833
834 asmlinkage long sys32_settimeofday(struct compat_timeval *tv, struct timezone *tz)
835 {
836         struct timespec kts;
837         struct timezone ktz;
838
839         if (tv) {
840                 if (get_ts32(&kts, tv))
841                         return -EFAULT;
842         }
843         if (tz) {
844                 if (copy_from_user(&ktz, tz, sizeof(ktz)))
845                         return -EFAULT;
846         }
847
848         return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
849 }
850
851 /* These are here just in case some old sparc32 binary calls it. */
852 asmlinkage long sys32_pause(void)
853 {
854         current->state = TASK_INTERRUPTIBLE;
855         schedule();
856         return -ERESTARTNOHAND;
857 }
858
859 asmlinkage long sys32_pread64(unsigned int fd, char *ubuf,
860                                 size_t count, u32 poshi, u32 poslo)
861 {
862         if ((compat_ssize_t) count < 0)
863                 return -EINVAL;
864         return sys_pread64(fd, ubuf, count, ((loff_t)AA(poshi) << 32) | AA(poslo));
865 }
866
867 asmlinkage long sys32_pwrite64(unsigned int fd, const char *ubuf,
868                                 size_t count, u32 poshi, u32 poslo)
869 {
870         if ((compat_ssize_t) count < 0)
871                 return -EINVAL;
872         return sys_pwrite64(fd, ubuf, count, ((loff_t)AA(poshi) << 32) | AA(poslo));
873 }
874
875 asmlinkage compat_ssize_t sys32_readahead(int fd, u32 offhi, u32 offlo, s32 count)
876 {
877         return sys_readahead(fd, ((loff_t)AA(offhi) << 32) | AA(offlo), count);
878 }
879
880 asmlinkage long sys32_sendfile(int out_fd, int in_fd, compat_off_t *offset, size_t count)
881 {
882         mm_segment_t old_fs = get_fs();
883         int ret;
884         off_t of;
885         
886         if (offset && get_user(of, offset))
887                 return -EFAULT;
888                 
889         set_fs(KERNEL_DS);
890         ret = sys_sendfile(out_fd, in_fd, offset ? &of : NULL, count);
891         set_fs(old_fs);
892         
893         if (!ret && offset && put_user(of, offset))
894                 return -EFAULT;
895                 
896         return ret;
897 }
898
899 asmlinkage long sys32_sendfile64(int out_fd, int in_fd,
900                                 compat_loff_t *offset, s32 count)
901 {
902         mm_segment_t old_fs = get_fs();
903         int ret;
904         loff_t lof;
905         
906         if (offset && get_user(lof, offset))
907                 return -EFAULT;
908                 
909         set_fs(KERNEL_DS);
910         ret = sys_sendfile64(out_fd, in_fd, offset ? &lof : NULL, count);
911         set_fs(old_fs);
912         
913         if (offset && put_user(lof, offset))
914                 return -EFAULT;
915                 
916         return ret;
917 }
918
919 /* Handle adjtimex compatibility. */
920
921 struct timex32 {
922         u32 modes;
923         s32 offset, freq, maxerror, esterror;
924         s32 status, constant, precision, tolerance;
925         struct compat_timeval time;
926         s32 tick;
927         s32 ppsfreq, jitter, shift, stabil;
928         s32 jitcnt, calcnt, errcnt, stbcnt;
929         s32  :32; s32  :32; s32  :32; s32  :32;
930         s32  :32; s32  :32; s32  :32; s32  :32;
931         s32  :32; s32  :32; s32  :32; s32  :32;
932 };
933
934 extern int do_adjtimex(struct timex *);
935
936 asmlinkage long sys32_adjtimex(struct timex32 *utp)
937 {
938         struct timex txc;
939         int ret;
940
941         memset(&txc, 0, sizeof(struct timex));
942
943         if(get_user(txc.modes, &utp->modes) ||
944            __get_user(txc.offset, &utp->offset) ||
945            __get_user(txc.freq, &utp->freq) ||
946            __get_user(txc.maxerror, &utp->maxerror) ||
947            __get_user(txc.esterror, &utp->esterror) ||
948            __get_user(txc.status, &utp->status) ||
949            __get_user(txc.constant, &utp->constant) ||
950            __get_user(txc.precision, &utp->precision) ||
951            __get_user(txc.tolerance, &utp->tolerance) ||
952            __get_user(txc.time.tv_sec, &utp->time.tv_sec) ||
953            __get_user(txc.time.tv_usec, &utp->time.tv_usec) ||
954            __get_user(txc.tick, &utp->tick) ||
955            __get_user(txc.ppsfreq, &utp->ppsfreq) ||
956            __get_user(txc.jitter, &utp->jitter) ||
957            __get_user(txc.shift, &utp->shift) ||
958            __get_user(txc.stabil, &utp->stabil) ||
959            __get_user(txc.jitcnt, &utp->jitcnt) ||
960            __get_user(txc.calcnt, &utp->calcnt) ||
961            __get_user(txc.errcnt, &utp->errcnt) ||
962            __get_user(txc.stbcnt, &utp->stbcnt))
963                 return -EFAULT;
964
965         ret = do_adjtimex(&txc);
966
967         if(put_user(txc.modes, &utp->modes) ||
968            __put_user(txc.offset, &utp->offset) ||
969            __put_user(txc.freq, &utp->freq) ||
970            __put_user(txc.maxerror, &utp->maxerror) ||
971            __put_user(txc.esterror, &utp->esterror) ||
972            __put_user(txc.status, &utp->status) ||
973            __put_user(txc.constant, &utp->constant) ||
974            __put_user(txc.precision, &utp->precision) ||
975            __put_user(txc.tolerance, &utp->tolerance) ||
976            __put_user(txc.time.tv_sec, &utp->time.tv_sec) ||
977            __put_user(txc.time.tv_usec, &utp->time.tv_usec) ||
978            __put_user(txc.tick, &utp->tick) ||
979            __put_user(txc.ppsfreq, &utp->ppsfreq) ||
980            __put_user(txc.jitter, &utp->jitter) ||
981            __put_user(txc.shift, &utp->shift) ||
982            __put_user(txc.stabil, &utp->stabil) ||
983            __put_user(txc.jitcnt, &utp->jitcnt) ||
984            __put_user(txc.calcnt, &utp->calcnt) ||
985            __put_user(txc.errcnt, &utp->errcnt) ||
986            __put_user(txc.stbcnt, &utp->stbcnt))
987                 ret = -EFAULT;
988
989         return ret;
990 }
991
992 struct __sysctl_args32 {
993         u32 name;
994         int nlen;
995         u32 oldval;
996         u32 oldlenp;
997         u32 newval;
998         u32 newlen;
999         u32 __unused[4];
1000 };
1001
1002 asmlinkage long sys32_sysctl(struct __sysctl_args32 *args)
1003 {
1004         struct __sysctl_args32 tmp;
1005         int error;
1006         size_t oldlen, *oldlenp = NULL;
1007         unsigned long addr = (((long)&args->__unused[0]) + 7) & ~7;
1008
1009         if (copy_from_user(&tmp, args, sizeof(tmp)))
1010                 return -EFAULT;
1011
1012         if (tmp.oldval && tmp.oldlenp) {
1013                 /* Duh, this is ugly and might not work if sysctl_args
1014                    is in read-only memory, but do_sysctl does indirectly
1015                    a lot of uaccess in both directions and we'd have to
1016                    basically copy the whole sysctl.c here, and
1017                    glibc's __sysctl uses rw memory for the structure
1018                    anyway.  */
1019                 if (get_user(oldlen, (u32 *)A(tmp.oldlenp)) ||
1020                     put_user(oldlen, (size_t *)addr))
1021                         return -EFAULT;
1022                 oldlenp = (size_t *)addr;
1023         }
1024
1025         lock_kernel();
1026         error = do_sysctl((int *)A(tmp.name), tmp.nlen, (void *)A(tmp.oldval),
1027                           oldlenp, (void *)A(tmp.newval), tmp.newlen);
1028         unlock_kernel();
1029         if (oldlenp) {
1030                 if (!error) {
1031                         if (get_user(oldlen, (size_t *)addr) ||
1032                             put_user(oldlen, (u32 *)A(tmp.oldlenp)))
1033                                 error = -EFAULT;
1034                 }
1035                 copy_to_user(args->__unused, tmp.__unused, sizeof(tmp.__unused));
1036         }
1037         return error;
1038 }
1039
1040 struct stat64_emu31 {
1041         unsigned long long  st_dev;
1042         unsigned int    __pad1;
1043 #define STAT64_HAS_BROKEN_ST_INO        1
1044         u32             __st_ino;
1045         unsigned int    st_mode;
1046         unsigned int    st_nlink;
1047         u32             st_uid;
1048         u32             st_gid;
1049         unsigned long long  st_rdev;
1050         unsigned int    __pad3;
1051         long            st_size;
1052         u32             st_blksize;
1053         unsigned char   __pad4[4];
1054         u32             __pad5;     /* future possible st_blocks high bits */
1055         u32             st_blocks;  /* Number 512-byte blocks allocated. */
1056         u32             st_atime;
1057         u32             __pad6;
1058         u32             st_mtime;
1059         u32             __pad7;
1060         u32             st_ctime;
1061         u32             __pad8;     /* will be high 32 bits of ctime someday */
1062         unsigned long   st_ino;
1063 };      
1064
1065 static int cp_stat64(struct stat64_emu31 *ubuf, struct kstat *stat)
1066 {
1067         struct stat64_emu31 tmp;
1068
1069         memset(&tmp, 0, sizeof(tmp));
1070
1071         tmp.st_dev = huge_encode_dev(stat->dev);
1072         tmp.st_ino = stat->ino;
1073         tmp.__st_ino = (u32)stat->ino;
1074         tmp.st_mode = stat->mode;
1075         tmp.st_nlink = (unsigned int)stat->nlink;
1076         tmp.st_uid = stat->uid;
1077         tmp.st_gid = stat->gid;
1078         tmp.st_rdev = huge_encode_dev(stat->rdev);
1079         tmp.st_size = stat->size;
1080         tmp.st_blksize = (u32)stat->blksize;
1081         tmp.st_blocks = (u32)stat->blocks;
1082         tmp.st_atime = (u32)stat->atime.tv_sec;
1083         tmp.st_mtime = (u32)stat->mtime.tv_sec;
1084         tmp.st_ctime = (u32)stat->ctime.tv_sec;
1085
1086         return copy_to_user(ubuf,&tmp,sizeof(tmp)) ? -EFAULT : 0; 
1087 }
1088
1089 asmlinkage long sys32_stat64(char * filename, struct stat64_emu31 * statbuf)
1090 {
1091         struct kstat stat;
1092         int ret = vfs_stat(filename, &stat);
1093         if (!ret)
1094                 ret = cp_stat64(statbuf, &stat);
1095         return ret;
1096 }
1097
1098 asmlinkage long sys32_lstat64(char * filename, struct stat64_emu31 * statbuf)
1099 {
1100         struct kstat stat;
1101         int ret = vfs_lstat(filename, &stat);
1102         if (!ret)
1103                 ret = cp_stat64(statbuf, &stat);
1104         return ret;
1105 }
1106
1107 asmlinkage long sys32_fstat64(unsigned long fd, struct stat64_emu31 * statbuf)
1108 {
1109         struct kstat stat;
1110         int ret = vfs_fstat(fd, &stat);
1111         if (!ret)
1112                 ret = cp_stat64(statbuf, &stat);
1113         return ret;
1114 }
1115
1116 /*
1117  * Linux/i386 didn't use to be able to handle more than
1118  * 4 system call parameters, so these system calls used a memory
1119  * block for parameter passing..
1120  */
1121
1122 struct mmap_arg_struct_emu31 {
1123         u32     addr;
1124         u32     len;
1125         u32     prot;
1126         u32     flags;
1127         u32     fd;
1128         u32     offset;
1129 };
1130
1131 /* common code for old and new mmaps */
1132 static inline long do_mmap2(
1133         unsigned long addr, unsigned long len,
1134         unsigned long prot, unsigned long flags,
1135         unsigned long fd, unsigned long pgoff)
1136 {
1137         struct file * file = NULL;
1138         unsigned long error = -EBADF;
1139
1140         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1141         if (!(flags & MAP_ANONYMOUS)) {
1142                 file = fget(fd);
1143                 if (!file)
1144                         goto out;
1145         }
1146
1147         down_write(&current->mm->mmap_sem);
1148         error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1149         if (!IS_ERR((void *) error) && error + len >= 0x80000000ULL) {
1150                 /* Result is out of bounds.  */
1151                 do_munmap(current->mm, addr, len);
1152                 error = -ENOMEM;
1153         }
1154         up_write(&current->mm->mmap_sem);
1155
1156         if (file)
1157                 fput(file);
1158 out:    
1159         return error;
1160 }
1161
1162
1163 asmlinkage unsigned long
1164 old32_mmap(struct mmap_arg_struct_emu31 *arg)
1165 {
1166         struct mmap_arg_struct_emu31 a;
1167         int error = -EFAULT;
1168
1169         if (copy_from_user(&a, arg, sizeof(a)))
1170                 goto out;
1171
1172         error = -EINVAL;
1173         if (a.offset & ~PAGE_MASK)
1174                 goto out;
1175
1176         error = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset >> PAGE_SHIFT); 
1177 out:
1178         return error;
1179 }
1180
1181 asmlinkage long 
1182 sys32_mmap2(struct mmap_arg_struct_emu31 *arg)
1183 {
1184         struct mmap_arg_struct_emu31 a;
1185         int error = -EFAULT;
1186
1187         if (copy_from_user(&a, arg, sizeof(a)))
1188                 goto out;
1189         error = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset);
1190 out:
1191         return error;
1192 }
1193
1194 asmlinkage long sys32_read(unsigned int fd, char * buf, size_t count)
1195 {
1196         if ((compat_ssize_t) count < 0)
1197                 return -EINVAL; 
1198
1199         return sys_read(fd, buf, count);
1200 }
1201
1202 asmlinkage long sys32_write(unsigned int fd, char * buf, size_t count)
1203 {
1204         if ((compat_ssize_t) count < 0)
1205                 return -EINVAL; 
1206
1207         return sys_write(fd, buf, count);
1208 }
1209
1210 asmlinkage long sys32_clone(struct pt_regs regs)
1211 {
1212         unsigned long clone_flags;
1213         unsigned long newsp;
1214         int *parent_tidptr, *child_tidptr;
1215
1216         clone_flags = regs.gprs[3] & 0xffffffffUL;
1217         newsp = regs.orig_gpr2 & 0x7fffffffUL;
1218         parent_tidptr = (int *) (regs.gprs[4] & 0x7fffffffUL);
1219         child_tidptr = (int *) (regs.gprs[5] & 0x7fffffffUL);
1220         if (!newsp)
1221                 newsp = regs.gprs[15];
1222         return do_fork(clone_flags & ~CLONE_IDLETASK, newsp, &regs, 0,
1223                        parent_tidptr, child_tidptr);
1224 }
1225
1226 /*
1227  * Wrapper function for sys_timer_create.
1228  */
1229 extern asmlinkage long
1230 sys_timer_create(clockid_t, struct sigevent *, timer_t *);
1231
1232 asmlinkage long
1233 sys32_timer_create(clockid_t which_clock, struct sigevent32 *se32,
1234                 timer_t *timer_id)
1235 {
1236         struct sigevent se;
1237         timer_t ktimer_id;
1238         mm_segment_t old_fs;
1239         long ret;
1240
1241         if (se32 == NULL)
1242                 return sys_timer_create(which_clock, NULL, timer_id);
1243
1244         /* XXX: converting se32 to se is filthy because of the
1245          * two union members. For now it is ok, because the pointers
1246          * are not touched in kernel.
1247          */
1248         memset(&se, 0, sizeof(se));
1249         if (get_user(se.sigev_value.sival_int,  &se32->sigev_value.sival_int) ||
1250             get_user(se.sigev_signo, &se32->sigev_signo) ||
1251             get_user(se.sigev_notify, &se32->sigev_notify) ||
1252             copy_from_user(&se._sigev_un._pad, &se32->_sigev_un._pad,
1253             sizeof(se._sigev_un._pad)))
1254                 return -EFAULT;
1255
1256         old_fs = get_fs();
1257         set_fs(KERNEL_DS);
1258         ret = sys_timer_create(which_clock, &se, &ktimer_id);
1259         set_fs(old_fs);
1260
1261         if (!ret)
1262                 ret = put_user (ktimer_id, timer_id);
1263
1264         return ret;
1265 }