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