kernel.org linux-2.6.10
[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_siginfo_from_user32(&info, uinfo))
729                 return -EFAULT;
730         set_fs (KERNEL_DS);
731         ret = sys_rt_sigqueueinfo(pid, sig, &info);
732         set_fs (old_fs);
733         return ret;
734 }
735
736 /*
737  * sys32_execve() executes a new program after the asm stub has set
738  * things up for us.  This should basically do what I want it to.
739  */
740 asmlinkage long
741 sys32_execve(struct pt_regs regs)
742 {
743         int error;
744         char * filename;
745
746         filename = getname(compat_ptr(regs.orig_gpr2));
747         error = PTR_ERR(filename);
748         if (IS_ERR(filename))
749                 goto out;
750         error = compat_do_execve(filename, compat_ptr(regs.gprs[3]),
751                                  compat_ptr(regs.gprs[4]), &regs);
752         if (error == 0)
753         {
754                 task_lock(current);
755                 current->ptrace &= ~PT_DTRACE;
756                 task_unlock(current);
757                 current->thread.fp_regs.fpc=0;
758                 __asm__ __volatile__
759                         ("sr  0,0\n\t"
760                          "sfpc 0,0\n\t"
761                          : : :"0");
762         }
763         putname(filename);
764 out:
765         return error;
766 }
767
768
769 #ifdef CONFIG_MODULES
770
771 asmlinkage long
772 sys32_init_module(void __user *umod, unsigned long len,
773                 const char __user *uargs)
774 {
775         return sys_init_module(umod, len, uargs);
776 }
777
778 asmlinkage long
779 sys32_delete_module(const char __user *name_user, unsigned int flags)
780 {
781         return sys_delete_module(name_user, flags);
782 }
783
784 #else /* CONFIG_MODULES */
785
786 asmlinkage long
787 sys32_init_module(void __user *umod, unsigned long len,
788                 const char __user *uargs)
789 {
790         return -ENOSYS;
791 }
792
793 asmlinkage long
794 sys32_delete_module(const char __user *name_user, unsigned int flags)
795 {
796         return -ENOSYS;
797 }
798
799 #endif  /* CONFIG_MODULES */
800
801 /* Translations due to time_t size differences.  Which affects all
802    sorts of things, like timeval and itimerval.  */
803
804 extern struct timezone sys_tz;
805
806 asmlinkage long sys32_gettimeofday(struct compat_timeval *tv, struct timezone *tz)
807 {
808         if (tv) {
809                 struct timeval ktv;
810                 do_gettimeofday(&ktv);
811                 if (put_tv32(tv, &ktv))
812                         return -EFAULT;
813         }
814         if (tz) {
815                 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
816                         return -EFAULT;
817         }
818         return 0;
819 }
820
821 static inline long get_ts32(struct timespec *o, struct compat_timeval *i)
822 {
823         long usec;
824
825         if (!access_ok(VERIFY_READ, i, sizeof(*i)))
826                 return -EFAULT;
827         if (__get_user(o->tv_sec, &i->tv_sec))
828                 return -EFAULT;
829         if (__get_user(usec, &i->tv_usec))
830                 return -EFAULT;
831         o->tv_nsec = usec * 1000;
832         return 0;
833 }
834
835 asmlinkage long sys32_settimeofday(struct compat_timeval *tv, struct timezone *tz)
836 {
837         struct timespec kts;
838         struct timezone ktz;
839
840         if (tv) {
841                 if (get_ts32(&kts, tv))
842                         return -EFAULT;
843         }
844         if (tz) {
845                 if (copy_from_user(&ktz, tz, sizeof(ktz)))
846                         return -EFAULT;
847         }
848
849         return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
850 }
851
852 /* These are here just in case some old sparc32 binary calls it. */
853 asmlinkage long sys32_pause(void)
854 {
855         current->state = TASK_INTERRUPTIBLE;
856         schedule();
857         return -ERESTARTNOHAND;
858 }
859
860 asmlinkage long sys32_pread64(unsigned int fd, char *ubuf,
861                                 size_t count, u32 poshi, u32 poslo)
862 {
863         if ((compat_ssize_t) count < 0)
864                 return -EINVAL;
865         return sys_pread64(fd, ubuf, count, ((loff_t)AA(poshi) << 32) | AA(poslo));
866 }
867
868 asmlinkage long sys32_pwrite64(unsigned int fd, const char *ubuf,
869                                 size_t count, u32 poshi, u32 poslo)
870 {
871         if ((compat_ssize_t) count < 0)
872                 return -EINVAL;
873         return sys_pwrite64(fd, ubuf, count, ((loff_t)AA(poshi) << 32) | AA(poslo));
874 }
875
876 asmlinkage compat_ssize_t sys32_readahead(int fd, u32 offhi, u32 offlo, s32 count)
877 {
878         return sys_readahead(fd, ((loff_t)AA(offhi) << 32) | AA(offlo), count);
879 }
880
881 asmlinkage long sys32_sendfile(int out_fd, int in_fd, compat_off_t *offset, size_t count)
882 {
883         mm_segment_t old_fs = get_fs();
884         int ret;
885         off_t of;
886         
887         if (offset && get_user(of, offset))
888                 return -EFAULT;
889                 
890         set_fs(KERNEL_DS);
891         ret = sys_sendfile(out_fd, in_fd, offset ? &of : NULL, count);
892         set_fs(old_fs);
893         
894         if (!ret && offset && put_user(of, offset))
895                 return -EFAULT;
896                 
897         return ret;
898 }
899
900 asmlinkage long sys32_sendfile64(int out_fd, int in_fd,
901                                 compat_loff_t *offset, s32 count)
902 {
903         mm_segment_t old_fs = get_fs();
904         int ret;
905         loff_t lof;
906         
907         if (offset && get_user(lof, offset))
908                 return -EFAULT;
909                 
910         set_fs(KERNEL_DS);
911         ret = sys_sendfile64(out_fd, in_fd, offset ? &lof : NULL, count);
912         set_fs(old_fs);
913         
914         if (offset && put_user(lof, offset))
915                 return -EFAULT;
916                 
917         return ret;
918 }
919
920 /* Handle adjtimex compatibility. */
921
922 struct timex32 {
923         u32 modes;
924         s32 offset, freq, maxerror, esterror;
925         s32 status, constant, precision, tolerance;
926         struct compat_timeval time;
927         s32 tick;
928         s32 ppsfreq, jitter, shift, stabil;
929         s32 jitcnt, calcnt, errcnt, stbcnt;
930         s32  :32; s32  :32; s32  :32; s32  :32;
931         s32  :32; s32  :32; s32  :32; s32  :32;
932         s32  :32; s32  :32; s32  :32; s32  :32;
933 };
934
935 extern int do_adjtimex(struct timex *);
936
937 asmlinkage long sys32_adjtimex(struct timex32 *utp)
938 {
939         struct timex txc;
940         int ret;
941
942         memset(&txc, 0, sizeof(struct timex));
943
944         if(get_user(txc.modes, &utp->modes) ||
945            __get_user(txc.offset, &utp->offset) ||
946            __get_user(txc.freq, &utp->freq) ||
947            __get_user(txc.maxerror, &utp->maxerror) ||
948            __get_user(txc.esterror, &utp->esterror) ||
949            __get_user(txc.status, &utp->status) ||
950            __get_user(txc.constant, &utp->constant) ||
951            __get_user(txc.precision, &utp->precision) ||
952            __get_user(txc.tolerance, &utp->tolerance) ||
953            __get_user(txc.time.tv_sec, &utp->time.tv_sec) ||
954            __get_user(txc.time.tv_usec, &utp->time.tv_usec) ||
955            __get_user(txc.tick, &utp->tick) ||
956            __get_user(txc.ppsfreq, &utp->ppsfreq) ||
957            __get_user(txc.jitter, &utp->jitter) ||
958            __get_user(txc.shift, &utp->shift) ||
959            __get_user(txc.stabil, &utp->stabil) ||
960            __get_user(txc.jitcnt, &utp->jitcnt) ||
961            __get_user(txc.calcnt, &utp->calcnt) ||
962            __get_user(txc.errcnt, &utp->errcnt) ||
963            __get_user(txc.stbcnt, &utp->stbcnt))
964                 return -EFAULT;
965
966         ret = do_adjtimex(&txc);
967
968         if(put_user(txc.modes, &utp->modes) ||
969            __put_user(txc.offset, &utp->offset) ||
970            __put_user(txc.freq, &utp->freq) ||
971            __put_user(txc.maxerror, &utp->maxerror) ||
972            __put_user(txc.esterror, &utp->esterror) ||
973            __put_user(txc.status, &utp->status) ||
974            __put_user(txc.constant, &utp->constant) ||
975            __put_user(txc.precision, &utp->precision) ||
976            __put_user(txc.tolerance, &utp->tolerance) ||
977            __put_user(txc.time.tv_sec, &utp->time.tv_sec) ||
978            __put_user(txc.time.tv_usec, &utp->time.tv_usec) ||
979            __put_user(txc.tick, &utp->tick) ||
980            __put_user(txc.ppsfreq, &utp->ppsfreq) ||
981            __put_user(txc.jitter, &utp->jitter) ||
982            __put_user(txc.shift, &utp->shift) ||
983            __put_user(txc.stabil, &utp->stabil) ||
984            __put_user(txc.jitcnt, &utp->jitcnt) ||
985            __put_user(txc.calcnt, &utp->calcnt) ||
986            __put_user(txc.errcnt, &utp->errcnt) ||
987            __put_user(txc.stbcnt, &utp->stbcnt))
988                 ret = -EFAULT;
989
990         return ret;
991 }
992
993 struct __sysctl_args32 {
994         u32 name;
995         int nlen;
996         u32 oldval;
997         u32 oldlenp;
998         u32 newval;
999         u32 newlen;
1000         u32 __unused[4];
1001 };
1002
1003 asmlinkage long sys32_sysctl(struct __sysctl_args32 *args)
1004 {
1005         struct __sysctl_args32 tmp;
1006         int error;
1007         size_t oldlen, *oldlenp = NULL;
1008         unsigned long addr = (((long)&args->__unused[0]) + 7) & ~7;
1009
1010         if (copy_from_user(&tmp, args, sizeof(tmp)))
1011                 return -EFAULT;
1012
1013         if (tmp.oldval && tmp.oldlenp) {
1014                 /* Duh, this is ugly and might not work if sysctl_args
1015                    is in read-only memory, but do_sysctl does indirectly
1016                    a lot of uaccess in both directions and we'd have to
1017                    basically copy the whole sysctl.c here, and
1018                    glibc's __sysctl uses rw memory for the structure
1019                    anyway.  */
1020                 if (get_user(oldlen, (u32 *)A(tmp.oldlenp)) ||
1021                     put_user(oldlen, (size_t *)addr))
1022                         return -EFAULT;
1023                 oldlenp = (size_t *)addr;
1024         }
1025
1026         lock_kernel();
1027         error = do_sysctl((int *)A(tmp.name), tmp.nlen, (void *)A(tmp.oldval),
1028                           oldlenp, (void *)A(tmp.newval), tmp.newlen);
1029         unlock_kernel();
1030         if (oldlenp) {
1031                 if (!error) {
1032                         if (get_user(oldlen, (size_t *)addr) ||
1033                             put_user(oldlen, (u32 *)A(tmp.oldlenp)))
1034                                 error = -EFAULT;
1035                 }
1036                 copy_to_user(args->__unused, tmp.__unused, sizeof(tmp.__unused));
1037         }
1038         return error;
1039 }
1040
1041 struct stat64_emu31 {
1042         unsigned long long  st_dev;
1043         unsigned int    __pad1;
1044 #define STAT64_HAS_BROKEN_ST_INO        1
1045         u32             __st_ino;
1046         unsigned int    st_mode;
1047         unsigned int    st_nlink;
1048         u32             st_uid;
1049         u32             st_gid;
1050         unsigned long long  st_rdev;
1051         unsigned int    __pad3;
1052         long            st_size;
1053         u32             st_blksize;
1054         unsigned char   __pad4[4];
1055         u32             __pad5;     /* future possible st_blocks high bits */
1056         u32             st_blocks;  /* Number 512-byte blocks allocated. */
1057         u32             st_atime;
1058         u32             __pad6;
1059         u32             st_mtime;
1060         u32             __pad7;
1061         u32             st_ctime;
1062         u32             __pad8;     /* will be high 32 bits of ctime someday */
1063         unsigned long   st_ino;
1064 };      
1065
1066 static int cp_stat64(struct stat64_emu31 *ubuf, struct kstat *stat)
1067 {
1068         struct stat64_emu31 tmp;
1069
1070         memset(&tmp, 0, sizeof(tmp));
1071
1072         tmp.st_dev = huge_encode_dev(stat->dev);
1073         tmp.st_ino = stat->ino;
1074         tmp.__st_ino = (u32)stat->ino;
1075         tmp.st_mode = stat->mode;
1076         tmp.st_nlink = (unsigned int)stat->nlink;
1077         tmp.st_uid = stat->uid;
1078         tmp.st_gid = stat->gid;
1079         tmp.st_rdev = huge_encode_dev(stat->rdev);
1080         tmp.st_size = stat->size;
1081         tmp.st_blksize = (u32)stat->blksize;
1082         tmp.st_blocks = (u32)stat->blocks;
1083         tmp.st_atime = (u32)stat->atime.tv_sec;
1084         tmp.st_mtime = (u32)stat->mtime.tv_sec;
1085         tmp.st_ctime = (u32)stat->ctime.tv_sec;
1086
1087         return copy_to_user(ubuf,&tmp,sizeof(tmp)) ? -EFAULT : 0; 
1088 }
1089
1090 asmlinkage long sys32_stat64(char * filename, struct stat64_emu31 * statbuf)
1091 {
1092         struct kstat stat;
1093         int ret = vfs_stat(filename, &stat);
1094         if (!ret)
1095                 ret = cp_stat64(statbuf, &stat);
1096         return ret;
1097 }
1098
1099 asmlinkage long sys32_lstat64(char * filename, struct stat64_emu31 * statbuf)
1100 {
1101         struct kstat stat;
1102         int ret = vfs_lstat(filename, &stat);
1103         if (!ret)
1104                 ret = cp_stat64(statbuf, &stat);
1105         return ret;
1106 }
1107
1108 asmlinkage long sys32_fstat64(unsigned long fd, struct stat64_emu31 * statbuf)
1109 {
1110         struct kstat stat;
1111         int ret = vfs_fstat(fd, &stat);
1112         if (!ret)
1113                 ret = cp_stat64(statbuf, &stat);
1114         return ret;
1115 }
1116
1117 /*
1118  * Linux/i386 didn't use to be able to handle more than
1119  * 4 system call parameters, so these system calls used a memory
1120  * block for parameter passing..
1121  */
1122
1123 struct mmap_arg_struct_emu31 {
1124         u32     addr;
1125         u32     len;
1126         u32     prot;
1127         u32     flags;
1128         u32     fd;
1129         u32     offset;
1130 };
1131
1132 /* common code for old and new mmaps */
1133 static inline long do_mmap2(
1134         unsigned long addr, unsigned long len,
1135         unsigned long prot, unsigned long flags,
1136         unsigned long fd, unsigned long pgoff)
1137 {
1138         struct file * file = NULL;
1139         unsigned long error = -EBADF;
1140
1141         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
1142         if (!(flags & MAP_ANONYMOUS)) {
1143                 file = fget(fd);
1144                 if (!file)
1145                         goto out;
1146         }
1147
1148         down_write(&current->mm->mmap_sem);
1149         error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
1150         if (!IS_ERR((void *) error) && error + len >= 0x80000000ULL) {
1151                 /* Result is out of bounds.  */
1152                 do_munmap(current->mm, addr, len);
1153                 error = -ENOMEM;
1154         }
1155         up_write(&current->mm->mmap_sem);
1156
1157         if (file)
1158                 fput(file);
1159 out:    
1160         return error;
1161 }
1162
1163
1164 asmlinkage unsigned long
1165 old32_mmap(struct mmap_arg_struct_emu31 *arg)
1166 {
1167         struct mmap_arg_struct_emu31 a;
1168         int error = -EFAULT;
1169
1170         if (copy_from_user(&a, arg, sizeof(a)))
1171                 goto out;
1172
1173         error = -EINVAL;
1174         if (a.offset & ~PAGE_MASK)
1175                 goto out;
1176
1177         error = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset >> PAGE_SHIFT); 
1178 out:
1179         return error;
1180 }
1181
1182 asmlinkage long 
1183 sys32_mmap2(struct mmap_arg_struct_emu31 *arg)
1184 {
1185         struct mmap_arg_struct_emu31 a;
1186         int error = -EFAULT;
1187
1188         if (copy_from_user(&a, arg, sizeof(a)))
1189                 goto out;
1190         error = do_mmap2(a.addr, a.len, a.prot, a.flags, a.fd, a.offset);
1191 out:
1192         return error;
1193 }
1194
1195 asmlinkage long sys32_read(unsigned int fd, char * buf, size_t count)
1196 {
1197         if ((compat_ssize_t) count < 0)
1198                 return -EINVAL; 
1199
1200         return sys_read(fd, buf, count);
1201 }
1202
1203 asmlinkage long sys32_write(unsigned int fd, char * buf, size_t count)
1204 {
1205         if ((compat_ssize_t) count < 0)
1206                 return -EINVAL; 
1207
1208         return sys_write(fd, buf, count);
1209 }
1210
1211 asmlinkage long sys32_clone(struct pt_regs regs)
1212 {
1213         unsigned long clone_flags;
1214         unsigned long newsp;
1215         int *parent_tidptr, *child_tidptr;
1216
1217         clone_flags = regs.gprs[3] & 0xffffffffUL;
1218         newsp = regs.orig_gpr2 & 0x7fffffffUL;
1219         parent_tidptr = (int *) (regs.gprs[4] & 0x7fffffffUL);
1220         child_tidptr = (int *) (regs.gprs[5] & 0x7fffffffUL);
1221         if (!newsp)
1222                 newsp = regs.gprs[15];
1223         return do_fork(clone_flags, newsp, &regs, 0,
1224                        parent_tidptr, child_tidptr);
1225 }
1226
1227 /*
1228  * Wrapper function for sys_timer_create.
1229  */
1230 extern asmlinkage long
1231 sys_timer_create(clockid_t, struct sigevent *, timer_t *);
1232
1233 asmlinkage long
1234 sys32_timer_create(clockid_t which_clock, struct sigevent32 *se32,
1235                 timer_t *timer_id)
1236 {
1237         struct sigevent se;
1238         timer_t ktimer_id;
1239         mm_segment_t old_fs;
1240         long ret;
1241
1242         if (se32 == NULL)
1243                 return sys_timer_create(which_clock, NULL, timer_id);
1244
1245         /* XXX: converting se32 to se is filthy because of the
1246          * two union members. For now it is ok, because the pointers
1247          * are not touched in kernel.
1248          */
1249         memset(&se, 0, sizeof(se));
1250         if (get_user(se.sigev_value.sival_int,  &se32->sigev_value.sival_int) ||
1251             get_user(se.sigev_signo, &se32->sigev_signo) ||
1252             get_user(se.sigev_notify, &se32->sigev_notify) ||
1253             copy_from_user(&se._sigev_un._pad, &se32->_sigev_un._pad,
1254             sizeof(se._sigev_un._pad)))
1255                 return -EFAULT;
1256
1257         old_fs = get_fs();
1258         set_fs(KERNEL_DS);
1259         ret = sys_timer_create(which_clock, &se, &ktimer_id);
1260         set_fs(old_fs);
1261
1262         if (!ret)
1263                 ret = put_user (ktimer_id, timer_id);
1264
1265         return ret;
1266 }