Merge to Fedora kernel-2.6.18-1.2255_FC5-vs2.0.2.2-rc9 patched with stable patch...
[linux-2.6.git] / arch / x86_64 / ia32 / sys_ia32.c
1 /*
2  * sys_ia32.c: Conversion between 32bit and 64bit native syscalls. Based on
3  *             sys_sparc32 
4  *
5  * Copyright (C) 2000           VA Linux Co
6  * Copyright (C) 2000           Don Dugger <n0ano@valinux.com>
7  * Copyright (C) 1999           Arun Sharma <arun.sharma@intel.com>
8  * Copyright (C) 1997,1998      Jakub Jelinek (jj@sunsite.mff.cuni.cz)
9  * Copyright (C) 1997           David S. Miller (davem@caip.rutgers.edu)
10  * Copyright (C) 2000           Hewlett-Packard Co.
11  * Copyright (C) 2000           David Mosberger-Tang <davidm@hpl.hp.com>
12  * Copyright (C) 2000,2001,2002 Andi Kleen, SuSE Labs (x86-64 port) 
13  *
14  * These routines maintain argument size conversion between 32bit and 64bit
15  * environment. In 2.5 most of this should be moved to a generic directory. 
16  *
17  * This file assumes that there is a hole at the end of user address space.
18  * 
19  * Some of the functions are LE specific currently. These are hopefully all marked.
20  * This should be fixed.
21  */
22
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/fs.h> 
26 #include <linux/file.h> 
27 #include <linux/signal.h>
28 #include <linux/syscalls.h>
29 #include <linux/resource.h>
30 #include <linux/times.h>
31 #include <linux/utsname.h>
32 #include <linux/smp.h>
33 #include <linux/smp_lock.h>
34 #include <linux/sem.h>
35 #include <linux/msg.h>
36 #include <linux/mm.h>
37 #include <linux/shm.h>
38 #include <linux/slab.h>
39 #include <linux/uio.h>
40 #include <linux/nfs_fs.h>
41 #include <linux/quota.h>
42 #include <linux/module.h>
43 #include <linux/sunrpc/svc.h>
44 #include <linux/nfsd/nfsd.h>
45 #include <linux/nfsd/cache.h>
46 #include <linux/nfsd/xdr.h>
47 #include <linux/nfsd/syscall.h>
48 #include <linux/poll.h>
49 #include <linux/personality.h>
50 #include <linux/stat.h>
51 #include <linux/ipc.h>
52 #include <linux/rwsem.h>
53 #include <linux/binfmts.h>
54 #include <linux/init.h>
55 #include <linux/aio_abi.h>
56 #include <linux/aio.h>
57 #include <linux/compat.h>
58 #include <linux/vfs.h>
59 #include <linux/ptrace.h>
60 #include <linux/highuid.h>
61 #include <linux/vmalloc.h>
62 #include <linux/fsnotify.h>
63 #include <linux/vs_cvirt.h>
64 #include <asm/mman.h>
65 #include <asm/types.h>
66 #include <asm/uaccess.h>
67 #include <asm/semaphore.h>
68 #include <asm/atomic.h>
69 #include <asm/ldt.h>
70
71 #include <net/scm.h>
72 #include <net/sock.h>
73 #include <asm/ia32.h>
74
75 #define AA(__x)         ((unsigned long)(__x))
76
77 int cp_compat_stat(struct kstat *kbuf, struct compat_stat __user *ubuf)
78 {
79         compat_ino_t ino;
80
81         typeof(ubuf->st_uid) uid = 0;
82         typeof(ubuf->st_gid) gid = 0;
83         SET_UID(uid, kbuf->uid);
84         SET_GID(gid, kbuf->gid);
85         if (!old_valid_dev(kbuf->dev) || !old_valid_dev(kbuf->rdev))
86                 return -EOVERFLOW;
87         if (kbuf->size >= 0x7fffffff)
88                 return -EOVERFLOW;
89         ino = kbuf->ino;
90         if (sizeof(ino) < sizeof(kbuf->ino) && ino != kbuf->ino)
91                 return -EOVERFLOW;
92         if (!access_ok(VERIFY_WRITE, ubuf, sizeof(struct compat_stat)) ||
93             __put_user (old_encode_dev(kbuf->dev), &ubuf->st_dev) ||
94             __put_user (ino, &ubuf->st_ino) ||
95             __put_user (kbuf->mode, &ubuf->st_mode) ||
96             __put_user (kbuf->nlink, &ubuf->st_nlink) ||
97             __put_user (uid, &ubuf->st_uid) ||
98             __put_user (gid, &ubuf->st_gid) ||
99             __put_user (old_encode_dev(kbuf->rdev), &ubuf->st_rdev) ||
100             __put_user (kbuf->size, &ubuf->st_size) ||
101             __put_user (kbuf->atime.tv_sec, &ubuf->st_atime) ||
102             __put_user (kbuf->atime.tv_nsec, &ubuf->st_atime_nsec) ||
103             __put_user (kbuf->mtime.tv_sec, &ubuf->st_mtime) ||
104             __put_user (kbuf->mtime.tv_nsec, &ubuf->st_mtime_nsec) ||
105             __put_user (kbuf->ctime.tv_sec, &ubuf->st_ctime) ||
106             __put_user (kbuf->ctime.tv_nsec, &ubuf->st_ctime_nsec) ||
107             __put_user (kbuf->blksize, &ubuf->st_blksize) ||
108             __put_user (kbuf->blocks, &ubuf->st_blocks))
109                 return -EFAULT;
110         return 0;
111 }
112
113 asmlinkage long
114 sys32_truncate64(char __user * filename, unsigned long offset_low, unsigned long offset_high)
115 {
116        return sys_truncate(filename, ((loff_t) offset_high << 32) | offset_low);
117 }
118
119 asmlinkage long
120 sys32_ftruncate64(unsigned int fd, unsigned long offset_low, unsigned long offset_high)
121 {
122        return sys_ftruncate(fd, ((loff_t) offset_high << 32) | offset_low);
123 }
124
125 /* Another set for IA32/LFS -- x86_64 struct stat is different due to 
126    support for 64bit inode numbers. */
127
128 static int
129 cp_stat64(struct stat64 __user *ubuf, struct kstat *stat)
130 {
131         typeof(ubuf->st_uid) uid = 0;
132         typeof(ubuf->st_gid) gid = 0;
133         SET_UID(uid, stat->uid);
134         SET_GID(gid, stat->gid);
135         if (!access_ok(VERIFY_WRITE, ubuf, sizeof(struct stat64)) ||
136             __put_user(huge_encode_dev(stat->dev), &ubuf->st_dev) ||
137             __put_user (stat->ino, &ubuf->__st_ino) ||
138             __put_user (stat->ino, &ubuf->st_ino) ||
139             __put_user (stat->mode, &ubuf->st_mode) ||
140             __put_user (stat->nlink, &ubuf->st_nlink) ||
141             __put_user (uid, &ubuf->st_uid) ||
142             __put_user (gid, &ubuf->st_gid) ||
143             __put_user (huge_encode_dev(stat->rdev), &ubuf->st_rdev) ||
144             __put_user (stat->size, &ubuf->st_size) ||
145             __put_user (stat->atime.tv_sec, &ubuf->st_atime) ||
146             __put_user (stat->atime.tv_nsec, &ubuf->st_atime_nsec) ||
147             __put_user (stat->mtime.tv_sec, &ubuf->st_mtime) ||
148             __put_user (stat->mtime.tv_nsec, &ubuf->st_mtime_nsec) ||
149             __put_user (stat->ctime.tv_sec, &ubuf->st_ctime) ||
150             __put_user (stat->ctime.tv_nsec, &ubuf->st_ctime_nsec) ||
151             __put_user (stat->blksize, &ubuf->st_blksize) ||
152             __put_user (stat->blocks, &ubuf->st_blocks))
153                 return -EFAULT;
154         return 0;
155 }
156
157 asmlinkage long
158 sys32_stat64(char __user * filename, struct stat64 __user *statbuf)
159 {
160         struct kstat stat;
161         int ret = vfs_stat(filename, &stat);
162         if (!ret)
163                 ret = cp_stat64(statbuf, &stat);
164         return ret;
165 }
166
167 asmlinkage long
168 sys32_lstat64(char __user * filename, struct stat64 __user *statbuf)
169 {
170         struct kstat stat;
171         int ret = vfs_lstat(filename, &stat);
172         if (!ret)
173                 ret = cp_stat64(statbuf, &stat);
174         return ret;
175 }
176
177 asmlinkage long
178 sys32_fstat64(unsigned int fd, struct stat64 __user *statbuf)
179 {
180         struct kstat stat;
181         int ret = vfs_fstat(fd, &stat);
182         if (!ret)
183                 ret = cp_stat64(statbuf, &stat);
184         return ret;
185 }
186
187 asmlinkage long
188 sys32_fstatat(unsigned int dfd, char __user *filename,
189               struct stat64 __user* statbuf, int flag)
190 {
191         struct kstat stat;
192         int error = -EINVAL;
193
194         if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
195                 goto out;
196
197         if (flag & AT_SYMLINK_NOFOLLOW)
198                 error = vfs_lstat_fd(dfd, filename, &stat);
199         else
200                 error = vfs_stat_fd(dfd, filename, &stat);
201
202         if (!error)
203                 error = cp_stat64(statbuf, &stat);
204
205 out:
206         return error;
207 }
208
209 /*
210  * Linux/i386 didn't use to be able to handle more than
211  * 4 system call parameters, so these system calls used a memory
212  * block for parameter passing..
213  */
214
215 struct mmap_arg_struct {
216         unsigned int addr;
217         unsigned int len;
218         unsigned int prot;
219         unsigned int flags;
220         unsigned int fd;
221         unsigned int offset;
222 };
223
224 asmlinkage long
225 sys32_mmap(struct mmap_arg_struct __user *arg)
226 {
227         struct mmap_arg_struct a;
228         struct file *file = NULL;
229         unsigned long retval;
230         struct mm_struct *mm ;
231
232         if (copy_from_user(&a, arg, sizeof(a)))
233                 return -EFAULT;
234
235         if (a.offset & ~PAGE_MASK)
236                 return -EINVAL; 
237
238         if (!(a.flags & MAP_ANONYMOUS)) {
239                 file = fget(a.fd);
240                 if (!file)
241                         return -EBADF;
242         }
243         
244         mm = current->mm; 
245         down_write(&mm->mmap_sem); 
246         retval = do_mmap_pgoff(file, a.addr, a.len, a.prot, a.flags, a.offset>>PAGE_SHIFT);
247         if (file)
248                 fput(file);
249
250         up_write(&mm->mmap_sem); 
251
252         return retval;
253 }
254
255 asmlinkage long 
256 sys32_mprotect(unsigned long start, size_t len, unsigned long prot)
257 {
258         return sys_mprotect(start,len,prot); 
259 }
260
261 asmlinkage long
262 sys32_pipe(int __user *fd)
263 {
264         int retval;
265         int fds[2];
266
267         retval = do_pipe(fds);
268         if (retval)
269                 goto out;
270         if (copy_to_user(fd, fds, sizeof(fds)))
271                 retval = -EFAULT;
272   out:
273         return retval;
274 }
275
276 asmlinkage long
277 sys32_rt_sigaction(int sig, struct sigaction32 __user *act,
278                    struct sigaction32 __user *oact,  unsigned int sigsetsize)
279 {
280         struct k_sigaction new_ka, old_ka;
281         int ret;
282         compat_sigset_t set32;
283
284         /* XXX: Don't preclude handling different sized sigset_t's.  */
285         if (sigsetsize != sizeof(compat_sigset_t))
286                 return -EINVAL;
287
288         if (act) {
289                 compat_uptr_t handler, restorer;
290
291                 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
292                     __get_user(handler, &act->sa_handler) ||
293                     __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
294                     __get_user(restorer, &act->sa_restorer)||
295                     __copy_from_user(&set32, &act->sa_mask, sizeof(compat_sigset_t)))
296                         return -EFAULT;
297                 new_ka.sa.sa_handler = compat_ptr(handler);
298                 new_ka.sa.sa_restorer = compat_ptr(restorer);
299                 /* FIXME: here we rely on _COMPAT_NSIG_WORS to be >= than _NSIG_WORDS << 1 */
300                 switch (_NSIG_WORDS) {
301                 case 4: new_ka.sa.sa_mask.sig[3] = set32.sig[6]
302                                 | (((long)set32.sig[7]) << 32);
303                 case 3: new_ka.sa.sa_mask.sig[2] = set32.sig[4]
304                                 | (((long)set32.sig[5]) << 32);
305                 case 2: new_ka.sa.sa_mask.sig[1] = set32.sig[2]
306                                 | (((long)set32.sig[3]) << 32);
307                 case 1: new_ka.sa.sa_mask.sig[0] = set32.sig[0]
308                                 | (((long)set32.sig[1]) << 32);
309                 }
310         }
311
312         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
313
314         if (!ret && oact) {
315                 /* FIXME: here we rely on _COMPAT_NSIG_WORS to be >= than _NSIG_WORDS << 1 */
316                 switch (_NSIG_WORDS) {
317                 case 4:
318                         set32.sig[7] = (old_ka.sa.sa_mask.sig[3] >> 32);
319                         set32.sig[6] = old_ka.sa.sa_mask.sig[3];
320                 case 3:
321                         set32.sig[5] = (old_ka.sa.sa_mask.sig[2] >> 32);
322                         set32.sig[4] = old_ka.sa.sa_mask.sig[2];
323                 case 2:
324                         set32.sig[3] = (old_ka.sa.sa_mask.sig[1] >> 32);
325                         set32.sig[2] = old_ka.sa.sa_mask.sig[1];
326                 case 1:
327                         set32.sig[1] = (old_ka.sa.sa_mask.sig[0] >> 32);
328                         set32.sig[0] = old_ka.sa.sa_mask.sig[0];
329                 }
330                 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
331                     __put_user(ptr_to_compat(old_ka.sa.sa_handler), &oact->sa_handler) ||
332                     __put_user(ptr_to_compat(old_ka.sa.sa_restorer), &oact->sa_restorer) ||
333                     __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
334                     __copy_to_user(&oact->sa_mask, &set32, sizeof(compat_sigset_t)))
335                         return -EFAULT;
336         }
337
338         return ret;
339 }
340
341 asmlinkage long
342 sys32_sigaction (int sig, struct old_sigaction32 __user *act, struct old_sigaction32 __user *oact)
343 {
344         struct k_sigaction new_ka, old_ka;
345         int ret;
346
347         if (act) {
348                 compat_old_sigset_t mask;
349                 compat_uptr_t handler, restorer;
350
351                 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
352                     __get_user(handler, &act->sa_handler) ||
353                     __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
354                     __get_user(restorer, &act->sa_restorer) ||
355                     __get_user(mask, &act->sa_mask))
356                         return -EFAULT;
357
358                 new_ka.sa.sa_handler = compat_ptr(handler);
359                 new_ka.sa.sa_restorer = compat_ptr(restorer);
360
361                 siginitset(&new_ka.sa.sa_mask, mask);
362         }
363
364         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
365
366         if (!ret && oact) {
367                 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
368                     __put_user(ptr_to_compat(old_ka.sa.sa_handler), &oact->sa_handler) ||
369                     __put_user(ptr_to_compat(old_ka.sa.sa_restorer), &oact->sa_restorer) ||
370                     __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
371                     __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
372                         return -EFAULT;
373         }
374
375         return ret;
376 }
377
378 asmlinkage long
379 sys32_rt_sigprocmask(int how, compat_sigset_t __user *set,
380                         compat_sigset_t __user *oset, unsigned int sigsetsize)
381 {
382         sigset_t s;
383         compat_sigset_t s32;
384         int ret;
385         mm_segment_t old_fs = get_fs();
386         
387         if (set) {
388                 if (copy_from_user (&s32, set, sizeof(compat_sigset_t)))
389                         return -EFAULT;
390                 switch (_NSIG_WORDS) {
391                 case 4: s.sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
392                 case 3: s.sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
393                 case 2: s.sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
394                 case 1: s.sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
395                 }
396         }
397         set_fs (KERNEL_DS);
398         ret = sys_rt_sigprocmask(how, set ? &s : NULL, oset ? &s : NULL,
399                                  sigsetsize); 
400         set_fs (old_fs);
401         if (ret) return ret;
402         if (oset) {
403                 switch (_NSIG_WORDS) {
404                 case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
405                 case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
406                 case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
407                 case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
408                 }
409                 if (copy_to_user (oset, &s32, sizeof(compat_sigset_t)))
410                         return -EFAULT;
411         }
412         return 0;
413 }
414
415 static inline long
416 get_tv32(struct timeval *o, struct compat_timeval __user *i)
417 {
418         int err = -EFAULT; 
419         if (access_ok(VERIFY_READ, i, sizeof(*i))) { 
420                 err = __get_user(o->tv_sec, &i->tv_sec);
421                 err |= __get_user(o->tv_usec, &i->tv_usec);
422         }
423         return err; 
424 }
425
426 static inline long
427 put_tv32(struct compat_timeval __user *o, struct timeval *i)
428 {
429         int err = -EFAULT;
430         if (access_ok(VERIFY_WRITE, o, sizeof(*o))) { 
431                 err = __put_user(i->tv_sec, &o->tv_sec);
432                 err |= __put_user(i->tv_usec, &o->tv_usec);
433         } 
434         return err; 
435 }
436
437 extern unsigned int alarm_setitimer(unsigned int seconds);
438
439 asmlinkage long
440 sys32_alarm(unsigned int seconds)
441 {
442         return alarm_setitimer(seconds);
443 }
444
445 /* Translations due to time_t size differences.  Which affects all
446    sorts of things, like timeval and itimerval.  */
447
448 extern struct timezone sys_tz;
449
450 asmlinkage long
451 sys32_gettimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
452 {
453         if (tv) {
454                 struct timeval ktv;
455                 do_gettimeofday(&ktv);
456                 if (put_tv32(tv, &ktv))
457                         return -EFAULT;
458         }
459         if (tz) {
460                 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
461                         return -EFAULT;
462         }
463         return 0;
464 }
465
466 asmlinkage long
467 sys32_settimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
468 {
469         struct timeval ktv;
470         struct timespec kts;
471         struct timezone ktz;
472
473         if (tv) {
474                 if (get_tv32(&ktv, tv))
475                         return -EFAULT;
476                 kts.tv_sec = ktv.tv_sec;
477                 kts.tv_nsec = ktv.tv_usec * NSEC_PER_USEC;
478         }
479         if (tz) {
480                 if (copy_from_user(&ktz, tz, sizeof(ktz)))
481                         return -EFAULT;
482         }
483
484         return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
485 }
486
487 struct sel_arg_struct {
488         unsigned int n;
489         unsigned int inp;
490         unsigned int outp;
491         unsigned int exp;
492         unsigned int tvp;
493 };
494
495 asmlinkage long
496 sys32_old_select(struct sel_arg_struct __user *arg)
497 {
498         struct sel_arg_struct a;
499
500         if (copy_from_user(&a, arg, sizeof(a)))
501                 return -EFAULT;
502         return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
503                                  compat_ptr(a.exp), compat_ptr(a.tvp));
504 }
505
506 extern asmlinkage long
507 compat_sys_wait4(compat_pid_t pid, compat_uint_t * stat_addr, int options,
508                  struct compat_rusage *ru);
509
510 asmlinkage long
511 sys32_waitpid(compat_pid_t pid, unsigned int *stat_addr, int options)
512 {
513         return compat_sys_wait4(pid, stat_addr, options, NULL);
514 }
515
516 /* 32-bit timeval and related flotsam.  */
517
518 asmlinkage long
519 sys32_sysfs(int option, u32 arg1, u32 arg2)
520 {
521         return sys_sysfs(option, arg1, arg2);
522 }
523
524 struct sysinfo32 {
525         s32 uptime;
526         u32 loads[3];
527         u32 totalram;
528         u32 freeram;
529         u32 sharedram;
530         u32 bufferram;
531         u32 totalswap;
532         u32 freeswap;
533         unsigned short procs;
534         unsigned short pad; 
535         u32 totalhigh;
536         u32 freehigh;
537         u32 mem_unit;
538         char _f[20-2*sizeof(u32)-sizeof(int)];
539 };
540
541 asmlinkage long
542 sys32_sysinfo(struct sysinfo32 __user *info)
543 {
544         struct sysinfo s;
545         int ret;
546         mm_segment_t old_fs = get_fs ();
547         int bitcount = 0;
548         
549         set_fs (KERNEL_DS);
550         ret = sys_sysinfo(&s);
551         set_fs (old_fs);
552
553         /* Check to see if any memory value is too large for 32-bit and scale
554          *  down if needed
555          */
556         if ((s.totalram >> 32) || (s.totalswap >> 32)) {
557                 while (s.mem_unit < PAGE_SIZE) {
558                         s.mem_unit <<= 1;
559                         bitcount++;
560                 }
561                 s.totalram >>= bitcount;
562                 s.freeram >>= bitcount;
563                 s.sharedram >>= bitcount;
564                 s.bufferram >>= bitcount;
565                 s.totalswap >>= bitcount;
566                 s.freeswap >>= bitcount;
567                 s.totalhigh >>= bitcount;
568                 s.freehigh >>= bitcount;
569         }
570
571         if (!access_ok(VERIFY_WRITE, info, sizeof(struct sysinfo32)) ||
572             __put_user (s.uptime, &info->uptime) ||
573             __put_user (s.loads[0], &info->loads[0]) ||
574             __put_user (s.loads[1], &info->loads[1]) ||
575             __put_user (s.loads[2], &info->loads[2]) ||
576             __put_user (s.totalram, &info->totalram) ||
577             __put_user (s.freeram, &info->freeram) ||
578             __put_user (s.sharedram, &info->sharedram) ||
579             __put_user (s.bufferram, &info->bufferram) ||
580             __put_user (s.totalswap, &info->totalswap) ||
581             __put_user (s.freeswap, &info->freeswap) ||
582             __put_user (s.procs, &info->procs) ||
583             __put_user (s.totalhigh, &info->totalhigh) || 
584             __put_user (s.freehigh, &info->freehigh) ||
585             __put_user (s.mem_unit, &info->mem_unit))
586                 return -EFAULT;
587         return 0;
588 }
589                 
590 asmlinkage long
591 sys32_sched_rr_get_interval(compat_pid_t pid, struct compat_timespec __user *interval)
592 {
593         struct timespec t;
594         int ret;
595         mm_segment_t old_fs = get_fs ();
596         
597         set_fs (KERNEL_DS);
598         ret = sys_sched_rr_get_interval(pid, &t);
599         set_fs (old_fs);
600         if (put_compat_timespec(&t, interval))
601                 return -EFAULT;
602         return ret;
603 }
604
605 asmlinkage long
606 sys32_rt_sigpending(compat_sigset_t __user *set, compat_size_t sigsetsize)
607 {
608         sigset_t s;
609         compat_sigset_t s32;
610         int ret;
611         mm_segment_t old_fs = get_fs();
612                 
613         set_fs (KERNEL_DS);
614         ret = sys_rt_sigpending(&s, sigsetsize);
615         set_fs (old_fs);
616         if (!ret) {
617                 switch (_NSIG_WORDS) {
618                 case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
619                 case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
620                 case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
621                 case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
622                 }
623                 if (copy_to_user (set, &s32, sizeof(compat_sigset_t)))
624                         return -EFAULT;
625         }
626         return ret;
627 }
628
629 asmlinkage long
630 sys32_rt_sigqueueinfo(int pid, int sig, compat_siginfo_t __user *uinfo)
631 {
632         siginfo_t info;
633         int ret;
634         mm_segment_t old_fs = get_fs();
635         
636         if (copy_siginfo_from_user32(&info, uinfo))
637                 return -EFAULT;
638         set_fs (KERNEL_DS);
639         ret = sys_rt_sigqueueinfo(pid, sig, &info);
640         set_fs (old_fs);
641         return ret;
642 }
643
644 /* These are here just in case some old ia32 binary calls it. */
645 asmlinkage long
646 sys32_pause(void)
647 {
648         current->state = TASK_INTERRUPTIBLE;
649         schedule();
650         return -ERESTARTNOHAND;
651 }
652
653
654 #ifdef CONFIG_SYSCTL
655 struct sysctl_ia32 {
656         unsigned int    name;
657         int             nlen;
658         unsigned int    oldval;
659         unsigned int    oldlenp;
660         unsigned int    newval;
661         unsigned int    newlen;
662         unsigned int    __unused[4];
663 };
664
665
666 asmlinkage long
667 sys32_sysctl(struct sysctl_ia32 __user *args32)
668 {
669         struct sysctl_ia32 a32;
670         mm_segment_t old_fs = get_fs ();
671         void __user *oldvalp, *newvalp;
672         size_t oldlen;
673         int __user *namep;
674         long ret;
675         extern int do_sysctl(int *name, int nlen, void *oldval, size_t *oldlenp,
676                      void *newval, size_t newlen);
677
678
679         if (copy_from_user(&a32, args32, sizeof (a32)))
680                 return -EFAULT;
681
682         /*
683          * We need to pre-validate these because we have to disable address checking
684          * before calling do_sysctl() because of OLDLEN but we can't run the risk of the
685          * user specifying bad addresses here.  Well, since we're dealing with 32 bit
686          * addresses, we KNOW that access_ok() will always succeed, so this is an
687          * expensive NOP, but so what...
688          */
689         namep = compat_ptr(a32.name);
690         oldvalp = compat_ptr(a32.oldval);
691         newvalp =  compat_ptr(a32.newval);
692
693         if ((oldvalp && get_user(oldlen, (int __user *)compat_ptr(a32.oldlenp)))
694             || !access_ok(VERIFY_WRITE, namep, 0)
695             || !access_ok(VERIFY_WRITE, oldvalp, 0)
696             || !access_ok(VERIFY_WRITE, newvalp, 0))
697                 return -EFAULT;
698
699         set_fs(KERNEL_DS);
700         lock_kernel();
701         ret = do_sysctl(namep, a32.nlen, oldvalp, &oldlen, newvalp, (size_t) a32.newlen);
702         unlock_kernel();
703         set_fs(old_fs);
704
705         if (oldvalp && put_user (oldlen, (int __user *)compat_ptr(a32.oldlenp)))
706                 return -EFAULT;
707
708         return ret;
709 }
710 #endif
711
712 /* warning: next two assume little endian */ 
713 asmlinkage long
714 sys32_pread(unsigned int fd, char __user *ubuf, u32 count, u32 poslo, u32 poshi)
715 {
716         return sys_pread64(fd, ubuf, count,
717                          ((loff_t)AA(poshi) << 32) | AA(poslo));
718 }
719
720 asmlinkage long
721 sys32_pwrite(unsigned int fd, char __user *ubuf, u32 count, u32 poslo, u32 poshi)
722 {
723         return sys_pwrite64(fd, ubuf, count,
724                           ((loff_t)AA(poshi) << 32) | AA(poslo));
725 }
726
727
728 asmlinkage long
729 sys32_personality(unsigned long personality)
730 {
731         int ret;
732         if (personality(current->personality) == PER_LINUX32 && 
733                 personality == PER_LINUX)
734                 personality = PER_LINUX32;
735         ret = sys_personality(personality);
736         if (ret == PER_LINUX32)
737                 ret = PER_LINUX;
738         return ret;
739 }
740
741 asmlinkage long
742 sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset, s32 count)
743 {
744         mm_segment_t old_fs = get_fs();
745         int ret;
746         off_t of;
747         
748         if (offset && get_user(of, offset))
749                 return -EFAULT;
750                 
751         set_fs(KERNEL_DS);
752         ret = sys_sendfile(out_fd, in_fd, offset ? &of : NULL, count);
753         set_fs(old_fs);
754         
755         if (offset && put_user(of, offset))
756                 return -EFAULT;
757                 
758         return ret;
759 }
760
761 asmlinkage long sys32_mmap2(unsigned long addr, unsigned long len,
762         unsigned long prot, unsigned long flags,
763         unsigned long fd, unsigned long pgoff)
764 {
765         struct mm_struct *mm = current->mm;
766         unsigned long error;
767         struct file * file = NULL;
768
769         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
770         if (!(flags & MAP_ANONYMOUS)) {
771                 file = fget(fd);
772                 if (!file)
773                         return -EBADF;
774         }
775
776         down_write(&mm->mmap_sem);
777         error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
778         up_write(&mm->mmap_sem);
779
780         if (file)
781                 fput(file);
782         return error;
783 }
784
785 asmlinkage long sys32_olduname(struct oldold_utsname __user * name)
786 {
787         int error;
788         struct new_utsname *ptr;
789
790         if (!name)
791                 return -EFAULT;
792         if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
793                 return -EFAULT;
794   
795         down_read(&uts_sem);
796         
797         ptr = vx_new_utsname();
798         error = __copy_to_user(&name->sysname,ptr->sysname,__OLD_UTS_LEN);
799          __put_user(0,name->sysname+__OLD_UTS_LEN);
800          __copy_to_user(&name->nodename,ptr->nodename,__OLD_UTS_LEN);
801          __put_user(0,name->nodename+__OLD_UTS_LEN);
802          __copy_to_user(&name->release,ptr->release,__OLD_UTS_LEN);
803          __put_user(0,name->release+__OLD_UTS_LEN);
804          __copy_to_user(&name->version,ptr->version,__OLD_UTS_LEN);
805          __put_user(0,name->version+__OLD_UTS_LEN);
806          { 
807                  char *arch = "x86_64";
808                  if (personality(current->personality) == PER_LINUX32)
809                          arch = "i686";
810                  
811                  __copy_to_user(&name->machine,arch,strlen(arch)+1);
812          }
813         
814          up_read(&uts_sem);
815          
816          error = error ? -EFAULT : 0;
817          
818          return error;
819 }
820
821 long sys32_uname(struct old_utsname __user * name)
822 {
823         int err;
824         if (!name)
825                 return -EFAULT;
826         down_read(&uts_sem);
827         err=copy_to_user(name, vx_new_utsname(), sizeof (*name));
828         up_read(&uts_sem);
829         if (personality(current->personality) == PER_LINUX32) 
830                 err |= copy_to_user(&name->machine, "i686", 5);
831         return err?-EFAULT:0;
832 }
833
834 long sys32_ustat(unsigned dev, struct ustat32 __user *u32p)
835 {
836         struct ustat u;
837         mm_segment_t seg;
838         int ret;
839         
840         seg = get_fs(); 
841         set_fs(KERNEL_DS); 
842         ret = sys_ustat(dev,&u); 
843         set_fs(seg);
844         if (ret >= 0) { 
845                 if (!access_ok(VERIFY_WRITE,u32p,sizeof(struct ustat32)) || 
846                     __put_user((__u32) u.f_tfree, &u32p->f_tfree) ||
847                     __put_user((__u32) u.f_tinode, &u32p->f_tfree) ||
848                     __copy_to_user(&u32p->f_fname, u.f_fname, sizeof(u.f_fname)) ||
849                     __copy_to_user(&u32p->f_fpack, u.f_fpack, sizeof(u.f_fpack)))
850                         ret = -EFAULT;
851         }
852         return ret;
853
854
855 asmlinkage long sys32_execve(char __user *name, compat_uptr_t __user *argv,
856                              compat_uptr_t __user *envp, struct pt_regs *regs)
857 {
858         long error;
859         char * filename;
860
861         filename = getname(name);
862         error = PTR_ERR(filename);
863         if (IS_ERR(filename))
864                 return error;
865         error = compat_do_execve(filename, argv, envp, regs);
866         if (error == 0) {
867                 task_lock(current);
868                 current->ptrace &= ~PT_DTRACE;
869                 task_unlock(current);
870         }
871         putname(filename);
872         return error;
873 }
874
875 asmlinkage long sys32_clone(unsigned int clone_flags, unsigned int newsp,
876                             struct pt_regs *regs)
877 {
878         void __user *parent_tid = (void __user *)regs->rdx;
879         void __user *child_tid = (void __user *)regs->rdi;
880         if (!newsp)
881                 newsp = regs->rsp;
882         return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
883 }
884
885 /*
886  * Some system calls that need sign extended arguments. This could be done by a generic wrapper.
887  */ 
888
889 long sys32_lseek (unsigned int fd, int offset, unsigned int whence)
890 {
891         return sys_lseek(fd, offset, whence);
892 }
893
894 long sys32_kill(int pid, int sig)
895 {
896         return sys_kill(pid, sig);
897 }
898  
899 long sys32_fadvise64_64(int fd, __u32 offset_low, __u32 offset_high, 
900                         __u32 len_low, __u32 len_high, int advice)
901
902         return sys_fadvise64_64(fd,
903                                (((u64)offset_high)<<32) | offset_low,
904                                (((u64)len_high)<<32) | len_low,
905                                advice); 
906
907
908 long sys32_vm86_warning(void)
909
910         struct task_struct *me = current;
911         static char lastcomm[sizeof(me->comm)];
912         if (strncmp(lastcomm, me->comm, sizeof(lastcomm))) {
913                 compat_printk(KERN_INFO "%s: vm86 mode not supported on 64 bit kernel\n",
914                        me->comm);
915                 strncpy(lastcomm, me->comm, sizeof(lastcomm));
916         } 
917         return -ENOSYS;
918
919
920 long sys32_lookup_dcookie(u32 addr_low, u32 addr_high,
921                           char __user * buf, size_t len)
922 {
923         return sys_lookup_dcookie(((u64)addr_high << 32) | addr_low, buf, len);
924 }
925