fedora core 2.6.10-1.12-FC2
[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/config.h>
24 #include <linux/kernel.h>
25 #include <linux/sched.h>
26 #include <linux/fs.h> 
27 #include <linux/file.h> 
28 #include <linux/signal.h>
29 #include <linux/syscalls.h>
30 #include <linux/resource.h>
31 #include <linux/times.h>
32 #include <linux/utsname.h>
33 #include <linux/timex.h>
34 #include <linux/smp.h>
35 #include <linux/smp_lock.h>
36 #include <linux/sem.h>
37 #include <linux/msg.h>
38 #include <linux/mm.h>
39 #include <linux/shm.h>
40 #include <linux/slab.h>
41 #include <linux/uio.h>
42 #include <linux/nfs_fs.h>
43 #include <linux/quota.h>
44 #include <linux/module.h>
45 #include <linux/sunrpc/svc.h>
46 #include <linux/nfsd/nfsd.h>
47 #include <linux/nfsd/cache.h>
48 #include <linux/nfsd/xdr.h>
49 #include <linux/nfsd/syscall.h>
50 #include <linux/poll.h>
51 #include <linux/personality.h>
52 #include <linux/stat.h>
53 #include <linux/ipc.h>
54 #include <linux/rwsem.h>
55 #include <linux/binfmts.h>
56 #include <linux/init.h>
57 #include <linux/aio_abi.h>
58 #include <linux/aio.h>
59 #include <linux/compat.h>
60 #include <linux/vfs.h>
61 #include <linux/ptrace.h>
62 #include <linux/highuid.h>
63 #include <linux/vmalloc.h>
64 #include <linux/mman.h>
65 #include <asm/types.h>
66 #include <asm/uaccess.h>
67 #include <asm/semaphore.h>
68 #include <asm/ipc.h>
69 #include <asm/atomic.h>
70 #include <asm/ldt.h>
71
72 #include <net/scm.h>
73 #include <net/sock.h>
74 #include <asm/ia32.h>
75
76 #define A(__x)          ((unsigned long)(__x))
77 #define AA(__x)         ((unsigned long)(__x))
78 #define ROUND_UP(x,a)   ((__typeof__(x))(((unsigned long)(x) + ((a) - 1)) & ~((a) - 1)))
79 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
80
81 int cp_compat_stat(struct kstat *kbuf, struct compat_stat __user *ubuf)
82 {
83         typeof(ubuf->st_uid) uid = 0;
84         typeof(ubuf->st_gid) gid = 0;
85         SET_UID(uid, kbuf->uid);
86         SET_GID(gid, kbuf->gid);
87         if (!old_valid_dev(kbuf->dev) || !old_valid_dev(kbuf->rdev))
88                 return -EOVERFLOW;
89         if (kbuf->size >= 0x7fffffff)
90                 return -EOVERFLOW;
91         if (verify_area(VERIFY_WRITE, ubuf, sizeof(struct compat_stat)) ||
92             __put_user (old_encode_dev(kbuf->dev), &ubuf->st_dev) ||
93             __put_user (kbuf->ino, &ubuf->st_ino) ||
94             __put_user (kbuf->mode, &ubuf->st_mode) ||
95             __put_user (kbuf->nlink, &ubuf->st_nlink) ||
96             __put_user (uid, &ubuf->st_uid) ||
97             __put_user (gid, &ubuf->st_gid) ||
98             __put_user (old_encode_dev(kbuf->rdev), &ubuf->st_rdev) ||
99             __put_user (kbuf->size, &ubuf->st_size) ||
100             __put_user (kbuf->atime.tv_sec, &ubuf->st_atime) ||
101             __put_user (kbuf->atime.tv_nsec, &ubuf->st_atime_nsec) ||
102             __put_user (kbuf->mtime.tv_sec, &ubuf->st_mtime) ||
103             __put_user (kbuf->mtime.tv_nsec, &ubuf->st_mtime_nsec) ||
104             __put_user (kbuf->ctime.tv_sec, &ubuf->st_ctime) ||
105             __put_user (kbuf->ctime.tv_nsec, &ubuf->st_ctime_nsec) ||
106             __put_user (kbuf->blksize, &ubuf->st_blksize) ||
107             __put_user (kbuf->blocks, &ubuf->st_blocks))
108                 return -EFAULT;
109         return 0;
110 }
111
112 asmlinkage long
113 sys32_truncate64(char __user * filename, unsigned long offset_low, unsigned long offset_high)
114 {
115        return sys_truncate(filename, ((loff_t) offset_high << 32) | offset_low);
116 }
117
118 asmlinkage long
119 sys32_ftruncate64(unsigned int fd, unsigned long offset_low, unsigned long offset_high)
120 {
121        return sys_ftruncate(fd, ((loff_t) offset_high << 32) | offset_low);
122 }
123
124 /* Another set for IA32/LFS -- x86_64 struct stat is different due to 
125    support for 64bit inode numbers. */
126
127 static int
128 cp_stat64(struct stat64 __user *ubuf, struct kstat *stat)
129 {
130         typeof(ubuf->st_uid) uid = 0;
131         typeof(ubuf->st_gid) gid = 0;
132         SET_UID(uid, stat->uid);
133         SET_GID(gid, stat->gid);
134         if (verify_area(VERIFY_WRITE, ubuf, sizeof(struct stat64)) ||
135             __put_user(huge_encode_dev(stat->dev), &ubuf->st_dev) ||
136             __put_user (stat->ino, &ubuf->__st_ino) ||
137             __put_user (stat->ino, &ubuf->st_ino) ||
138             __put_user (stat->mode, &ubuf->st_mode) ||
139             __put_user (stat->nlink, &ubuf->st_nlink) ||
140             __put_user (uid, &ubuf->st_uid) ||
141             __put_user (gid, &ubuf->st_gid) ||
142             __put_user (huge_encode_dev(stat->rdev), &ubuf->st_rdev) ||
143             __put_user (stat->size, &ubuf->st_size) ||
144             __put_user (stat->atime.tv_sec, &ubuf->st_atime) ||
145             __put_user (stat->atime.tv_nsec, &ubuf->st_atime_nsec) ||
146             __put_user (stat->mtime.tv_sec, &ubuf->st_mtime) ||
147             __put_user (stat->mtime.tv_nsec, &ubuf->st_mtime_nsec) ||
148             __put_user (stat->ctime.tv_sec, &ubuf->st_ctime) ||
149             __put_user (stat->ctime.tv_nsec, &ubuf->st_ctime_nsec) ||
150             __put_user (stat->blksize, &ubuf->st_blksize) ||
151             __put_user (stat->blocks, &ubuf->st_blocks))
152                 return -EFAULT;
153         return 0;
154 }
155
156 asmlinkage long
157 sys32_stat64(char __user * filename, struct stat64 __user *statbuf)
158 {
159         struct kstat stat;
160         int ret = vfs_stat(filename, &stat);
161         if (!ret)
162                 ret = cp_stat64(statbuf, &stat);
163         return ret;
164 }
165
166 asmlinkage long
167 sys32_lstat64(char __user * filename, struct stat64 __user *statbuf)
168 {
169         struct kstat stat;
170         int ret = vfs_lstat(filename, &stat);
171         if (!ret)
172                 ret = cp_stat64(statbuf, &stat);
173         return ret;
174 }
175
176 asmlinkage long
177 sys32_fstat64(unsigned int fd, struct stat64 __user *statbuf)
178 {
179         struct kstat stat;
180         int ret = vfs_fstat(fd, &stat);
181         if (!ret)
182                 ret = cp_stat64(statbuf, &stat);
183         return ret;
184 }
185
186 /*
187  * Linux/i386 didn't use to be able to handle more than
188  * 4 system call parameters, so these system calls used a memory
189  * block for parameter passing..
190  */
191
192 struct mmap_arg_struct {
193         unsigned int addr;
194         unsigned int len;
195         unsigned int prot;
196         unsigned int flags;
197         unsigned int fd;
198         unsigned int offset;
199 };
200
201 asmlinkage long
202 sys32_mmap(struct mmap_arg_struct __user *arg)
203 {
204         struct mmap_arg_struct a;
205         struct file *file = NULL;
206         unsigned long retval;
207         struct mm_struct *mm ;
208
209         if (copy_from_user(&a, arg, sizeof(a)))
210                 return -EFAULT;
211
212         if (a.offset & ~PAGE_MASK)
213                 return -EINVAL; 
214
215         if (a.flags & MAP_FIXED) {
216                 if (a.len > IA32_PAGE_OFFSET)
217                         return -EINVAL;
218                 if (a.addr > IA32_PAGE_OFFSET - a.len)
219                         return -ENOMEM;
220         }
221
222         if (!(a.flags & MAP_ANONYMOUS)) {
223                 file = fget(a.fd);
224                 if (!file)
225                         return -EBADF;
226         }
227         
228         mm = current->mm; 
229         down_write(&mm->mmap_sem); 
230         retval = do_mmap_pgoff(file, a.addr, a.len, a.prot, a.flags, a.offset>>PAGE_SHIFT);
231         if (file)
232                 fput(file);
233
234         up_write(&mm->mmap_sem); 
235
236         return retval;
237 }
238
239 asmlinkage long
240 sys32_munmap(unsigned long start, unsigned long len)
241 {
242         if ((start + len) > IA32_PAGE_OFFSET)
243                 return -EINVAL;
244         return sys_munmap(start, len);
245 }
246
247 asmlinkage long 
248 sys32_mprotect(unsigned long start, size_t len, unsigned long prot)
249 {
250         if ((start + PAGE_ALIGN(len)) >> 32)
251                 return -ENOMEM;
252         return sys_mprotect(start,len,prot); 
253 }
254
255 sys32_brk(unsigned long brk)
256 {
257         if (brk > IA32_PAGE_OFFSET)
258                 return -EINVAL;
259         return sys_brk(brk);
260 }
261
262 extern unsigned long do_mremap(unsigned long addr,
263    unsigned long old_len, unsigned long new_len,
264    unsigned long flags, unsigned long new_addr);
265
266 asmlinkage unsigned long sys32_mremap(unsigned long addr,
267    unsigned long old_len, unsigned long new_len,
268    unsigned long flags, unsigned long new_addr)
269 {
270         struct vm_area_struct *vma;
271         unsigned long ret = -EINVAL;
272
273         if (old_len > IA32_PAGE_OFFSET || new_len > IA32_PAGE_OFFSET)
274                 goto out;
275         if (addr > IA32_PAGE_OFFSET - old_len)
276                 goto out;
277         down_write(&current->mm->mmap_sem);
278         if (flags & MREMAP_FIXED) {
279                 if (new_addr > IA32_PAGE_OFFSET - new_len)
280                         goto out_sem;
281         } else if (addr > IA32_PAGE_OFFSET - new_len) {
282                 unsigned long map_flags = 0;
283                 struct file *file = NULL;
284
285                 ret = -ENOMEM;
286                 if (!(flags & MREMAP_MAYMOVE))
287                         goto out_sem;
288
289                 vma = find_vma(current->mm, addr);
290                 if (vma) {
291                         if (vma->vm_flags & VM_SHARED)
292                                 map_flags |= MAP_SHARED;
293                         file = vma->vm_file;
294                 }
295
296                 /* MREMAP_FIXED checked above. */
297                 new_addr = get_unmapped_area(file, addr, new_len,
298                         vma ? vma->vm_pgoff : 0, map_flags);
299                 ret = new_addr;
300                 if (new_addr & ~PAGE_MASK)
301                         goto out_sem;
302                 flags |= MREMAP_FIXED;
303         }
304         ret = do_mremap(addr, old_len, new_len, flags, new_addr);
305 out_sem:
306         up_write(&current->mm->mmap_sem);
307 out:
308 return ret;
309 }
310
311
312 asmlinkage long
313 sys32_pipe(int __user *fd)
314 {
315         int retval;
316         int fds[2];
317
318         retval = do_pipe(fds);
319         if (retval)
320                 goto out;
321         if (copy_to_user(fd, fds, sizeof(fds)))
322                 retval = -EFAULT;
323   out:
324         return retval;
325 }
326
327 asmlinkage long
328 sys32_rt_sigaction(int sig, struct sigaction32 __user *act,
329                    struct sigaction32 __user *oact,  unsigned int sigsetsize)
330 {
331         struct k_sigaction new_ka, old_ka;
332         int ret;
333         compat_sigset_t set32;
334
335         /* XXX: Don't preclude handling different sized sigset_t's.  */
336         if (sigsetsize != sizeof(compat_sigset_t))
337                 return -EINVAL;
338
339         if (act) {
340                 compat_uptr_t handler, restorer;
341
342                 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
343                     __get_user(handler, &act->sa_handler) ||
344                     __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
345                     __get_user(restorer, &act->sa_restorer)||
346                     __copy_from_user(&set32, &act->sa_mask, sizeof(compat_sigset_t)))
347                         return -EFAULT;
348                 new_ka.sa.sa_handler = compat_ptr(handler);
349                 new_ka.sa.sa_restorer = compat_ptr(restorer);
350                 /* FIXME: here we rely on _COMPAT_NSIG_WORS to be >= than _NSIG_WORDS << 1 */
351                 switch (_NSIG_WORDS) {
352                 case 4: new_ka.sa.sa_mask.sig[3] = set32.sig[6]
353                                 | (((long)set32.sig[7]) << 32);
354                 case 3: new_ka.sa.sa_mask.sig[2] = set32.sig[4]
355                                 | (((long)set32.sig[5]) << 32);
356                 case 2: new_ka.sa.sa_mask.sig[1] = set32.sig[2]
357                                 | (((long)set32.sig[3]) << 32);
358                 case 1: new_ka.sa.sa_mask.sig[0] = set32.sig[0]
359                                 | (((long)set32.sig[1]) << 32);
360                 }
361         }
362
363         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
364
365         if (!ret && oact) {
366                 /* FIXME: here we rely on _COMPAT_NSIG_WORS to be >= than _NSIG_WORDS << 1 */
367                 switch (_NSIG_WORDS) {
368                 case 4:
369                         set32.sig[7] = (old_ka.sa.sa_mask.sig[3] >> 32);
370                         set32.sig[6] = old_ka.sa.sa_mask.sig[3];
371                 case 3:
372                         set32.sig[5] = (old_ka.sa.sa_mask.sig[2] >> 32);
373                         set32.sig[4] = old_ka.sa.sa_mask.sig[2];
374                 case 2:
375                         set32.sig[3] = (old_ka.sa.sa_mask.sig[1] >> 32);
376                         set32.sig[2] = old_ka.sa.sa_mask.sig[1];
377                 case 1:
378                         set32.sig[1] = (old_ka.sa.sa_mask.sig[0] >> 32);
379                         set32.sig[0] = old_ka.sa.sa_mask.sig[0];
380                 }
381                 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
382                     __put_user((long)old_ka.sa.sa_handler, &oact->sa_handler) ||
383                     __put_user((long)old_ka.sa.sa_restorer, &oact->sa_restorer) ||
384                     __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
385                     __copy_to_user(&oact->sa_mask, &set32, sizeof(compat_sigset_t)))
386                         return -EFAULT;
387         }
388
389         return ret;
390 }
391
392 asmlinkage long
393 sys32_sigaction (int sig, struct old_sigaction32 __user *act, struct old_sigaction32 __user *oact)
394 {
395         struct k_sigaction new_ka, old_ka;
396         int ret;
397
398         if (act) {
399                 compat_old_sigset_t mask;
400                 compat_uptr_t handler, restorer;
401
402                 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
403                     __get_user(handler, &act->sa_handler) ||
404                     __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
405                     __get_user(restorer, &act->sa_restorer) ||
406                     __get_user(mask, &act->sa_mask))
407                         return -EFAULT;
408
409                 new_ka.sa.sa_handler = compat_ptr(handler);
410                 new_ka.sa.sa_restorer = compat_ptr(restorer);
411
412                 siginitset(&new_ka.sa.sa_mask, mask);
413         }
414
415         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
416
417         if (!ret && oact) {
418                 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
419                     __put_user((long)old_ka.sa.sa_handler, &oact->sa_handler) ||
420                     __put_user((long)old_ka.sa.sa_restorer, &oact->sa_restorer) ||
421                     __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
422                     __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
423                         return -EFAULT;
424         }
425
426         return ret;
427 }
428
429 asmlinkage long
430 sys32_rt_sigprocmask(int how, compat_sigset_t __user *set,
431                         compat_sigset_t __user *oset, unsigned int sigsetsize)
432 {
433         sigset_t s;
434         compat_sigset_t s32;
435         int ret;
436         mm_segment_t old_fs = get_fs();
437         
438         if (set) {
439                 if (copy_from_user (&s32, set, sizeof(compat_sigset_t)))
440                         return -EFAULT;
441                 switch (_NSIG_WORDS) {
442                 case 4: s.sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
443                 case 3: s.sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
444                 case 2: s.sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
445                 case 1: s.sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
446                 }
447         }
448         set_fs (KERNEL_DS);
449         ret = sys_rt_sigprocmask(how, set ? &s : NULL, oset ? &s : NULL,
450                                  sigsetsize); 
451         set_fs (old_fs);
452         if (ret) return ret;
453         if (oset) {
454                 switch (_NSIG_WORDS) {
455                 case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
456                 case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
457                 case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
458                 case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
459                 }
460                 if (copy_to_user (oset, &s32, sizeof(compat_sigset_t)))
461                         return -EFAULT;
462         }
463         return 0;
464 }
465
466 static inline long
467 get_tv32(struct timeval *o, struct compat_timeval __user *i)
468 {
469         int err = -EFAULT; 
470         if (access_ok(VERIFY_READ, i, sizeof(*i))) { 
471                 err = __get_user(o->tv_sec, &i->tv_sec);
472                 err |= __get_user(o->tv_usec, &i->tv_usec);
473         }
474         return err; 
475 }
476
477 static inline long
478 put_tv32(struct compat_timeval __user *o, struct timeval *i)
479 {
480         int err = -EFAULT;
481         if (access_ok(VERIFY_WRITE, o, sizeof(*o))) { 
482                 err = __put_user(i->tv_sec, &o->tv_sec);
483                 err |= __put_user(i->tv_usec, &o->tv_usec);
484         } 
485         return err; 
486 }
487
488 extern int do_setitimer(int which, struct itimerval *, struct itimerval *);
489
490 asmlinkage long
491 sys32_alarm(unsigned int seconds)
492 {
493         struct itimerval it_new, it_old;
494         unsigned int oldalarm;
495
496         it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
497         it_new.it_value.tv_sec = seconds;
498         it_new.it_value.tv_usec = 0;
499         do_setitimer(ITIMER_REAL, &it_new, &it_old);
500         oldalarm = it_old.it_value.tv_sec;
501         /* ehhh.. We can't return 0 if we have an alarm pending.. */
502         /* And we'd better return too much than too little anyway */
503         if (it_old.it_value.tv_usec)
504                 oldalarm++;
505         return oldalarm;
506 }
507
508 /* Translations due to time_t size differences.  Which affects all
509    sorts of things, like timeval and itimerval.  */
510
511 extern struct timezone sys_tz;
512
513 asmlinkage long
514 sys32_gettimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
515 {
516         if (tv) {
517                 struct timeval ktv;
518                 do_gettimeofday(&ktv);
519                 if (put_tv32(tv, &ktv))
520                         return -EFAULT;
521         }
522         if (tz) {
523                 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
524                         return -EFAULT;
525         }
526         return 0;
527 }
528
529 asmlinkage long
530 sys32_settimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
531 {
532         struct timeval ktv;
533         struct timespec kts;
534         struct timezone ktz;
535
536         if (tv) {
537                 if (get_tv32(&ktv, tv))
538                         return -EFAULT;
539                 kts.tv_sec = ktv.tv_sec;
540                 kts.tv_nsec = ktv.tv_usec * NSEC_PER_USEC;
541         }
542         if (tz) {
543                 if (copy_from_user(&ktz, tz, sizeof(ktz)))
544                         return -EFAULT;
545         }
546
547         return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
548 }
549
550 struct sel_arg_struct {
551         unsigned int n;
552         unsigned int inp;
553         unsigned int outp;
554         unsigned int exp;
555         unsigned int tvp;
556 };
557
558 asmlinkage long
559 sys32_old_select(struct sel_arg_struct __user *arg)
560 {
561         struct sel_arg_struct a;
562
563         if (copy_from_user(&a, arg, sizeof(a)))
564                 return -EFAULT;
565         return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
566                                  compat_ptr(a.exp), compat_ptr(a.tvp));
567 }
568
569 /*
570  * sys_time() can be implemented in user-level using
571  * sys_gettimeofday().  x86-64 did this but i386 Linux did not
572  * so we have to implement this system call here.
573  */
574 asmlinkage long sys32_time(int __user * tloc)
575 {
576         int i;
577         struct timeval tv;
578
579         do_gettimeofday(&tv);
580         i = tv.tv_sec;
581
582         if (tloc) {
583                 if (put_user(i,tloc))
584                         i = -EFAULT;
585         }
586         return i;
587 }
588
589 extern asmlinkage long
590 compat_sys_wait4(compat_pid_t pid, compat_uint_t * stat_addr, int options,
591                  struct compat_rusage *ru);
592
593 asmlinkage long
594 sys32_waitpid(compat_pid_t pid, unsigned int *stat_addr, int options)
595 {
596         return compat_sys_wait4(pid, stat_addr, options, NULL);
597 }
598
599 int sys32_ni_syscall(int call)
600
601         struct task_struct *me = current;
602         static char lastcomm[sizeof(me->comm)];
603
604         if (strncmp(lastcomm, me->comm, sizeof(lastcomm))) {
605                 printk(KERN_INFO "IA32 syscall %d from %s not implemented\n",
606                        call, me->comm);
607                 strncpy(lastcomm, me->comm, sizeof(lastcomm));
608         } 
609         return -ENOSYS;        
610
611
612 /* 32-bit timeval and related flotsam.  */
613
614 asmlinkage long
615 sys32_sysfs(int option, u32 arg1, u32 arg2)
616 {
617         return sys_sysfs(option, arg1, arg2);
618 }
619
620 struct sysinfo32 {
621         s32 uptime;
622         u32 loads[3];
623         u32 totalram;
624         u32 freeram;
625         u32 sharedram;
626         u32 bufferram;
627         u32 totalswap;
628         u32 freeswap;
629         unsigned short procs;
630         unsigned short pad; 
631         u32 totalhigh;
632         u32 freehigh;
633         u32 mem_unit;
634         char _f[20-2*sizeof(u32)-sizeof(int)];
635 };
636
637 asmlinkage long
638 sys32_sysinfo(struct sysinfo32 __user *info)
639 {
640         struct sysinfo s;
641         int ret;
642         mm_segment_t old_fs = get_fs ();
643         int bitcount = 0;
644         
645         set_fs (KERNEL_DS);
646         ret = sys_sysinfo(&s);
647         set_fs (old_fs);
648
649         /* Check to see if any memory value is too large for 32-bit and scale
650          *  down if needed
651          */
652         if ((s.totalram >> 32) || (s.totalswap >> 32)) {
653                 while (s.mem_unit < PAGE_SIZE) {
654                         s.mem_unit <<= 1;
655                         bitcount++;
656                 }
657                 s.totalram >>= bitcount;
658                 s.freeram >>= bitcount;
659                 s.sharedram >>= bitcount;
660                 s.bufferram >>= bitcount;
661                 s.totalswap >>= bitcount;
662                 s.freeswap >>= bitcount;
663                 s.totalhigh >>= bitcount;
664                 s.freehigh >>= bitcount;
665         }
666
667         if (verify_area(VERIFY_WRITE, info, sizeof(struct sysinfo32)) ||
668             __put_user (s.uptime, &info->uptime) ||
669             __put_user (s.loads[0], &info->loads[0]) ||
670             __put_user (s.loads[1], &info->loads[1]) ||
671             __put_user (s.loads[2], &info->loads[2]) ||
672             __put_user (s.totalram, &info->totalram) ||
673             __put_user (s.freeram, &info->freeram) ||
674             __put_user (s.sharedram, &info->sharedram) ||
675             __put_user (s.bufferram, &info->bufferram) ||
676             __put_user (s.totalswap, &info->totalswap) ||
677             __put_user (s.freeswap, &info->freeswap) ||
678             __put_user (s.procs, &info->procs) ||
679             __put_user (s.totalhigh, &info->totalhigh) || 
680             __put_user (s.freehigh, &info->freehigh) ||
681             __put_user (s.mem_unit, &info->mem_unit))
682                 return -EFAULT;
683         return 0;
684 }
685                 
686 asmlinkage long
687 sys32_sched_rr_get_interval(compat_pid_t pid, struct compat_timespec __user *interval)
688 {
689         struct timespec t;
690         int ret;
691         mm_segment_t old_fs = get_fs ();
692         
693         set_fs (KERNEL_DS);
694         ret = sys_sched_rr_get_interval(pid, &t);
695         set_fs (old_fs);
696         if (put_compat_timespec(&t, interval))
697                 return -EFAULT;
698         return ret;
699 }
700
701 asmlinkage long
702 sys32_rt_sigpending(compat_sigset_t __user *set, compat_size_t sigsetsize)
703 {
704         sigset_t s;
705         compat_sigset_t s32;
706         int ret;
707         mm_segment_t old_fs = get_fs();
708                 
709         set_fs (KERNEL_DS);
710         ret = sys_rt_sigpending(&s, sigsetsize);
711         set_fs (old_fs);
712         if (!ret) {
713                 switch (_NSIG_WORDS) {
714                 case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
715                 case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
716                 case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
717                 case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
718                 }
719                 if (copy_to_user (set, &s32, sizeof(compat_sigset_t)))
720                         return -EFAULT;
721         }
722         return ret;
723 }
724
725
726 asmlinkage long
727 sys32_rt_sigtimedwait(compat_sigset_t __user *uthese, siginfo_t32 __user *uinfo,
728                       struct compat_timespec __user *uts, compat_size_t sigsetsize)
729 {
730         sigset_t s;
731         compat_sigset_t s32;
732         struct timespec t;
733         int ret;
734         mm_segment_t old_fs = get_fs();
735         siginfo_t info;
736                 
737         if (copy_from_user (&s32, uthese, sizeof(compat_sigset_t)))
738                 return -EFAULT;
739         switch (_NSIG_WORDS) {
740         case 4: s.sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
741         case 3: s.sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
742         case 2: s.sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
743         case 1: s.sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
744         }
745         if (uts && get_compat_timespec(&t, uts))
746                 return -EFAULT;
747         if (uinfo) {
748                 /* stop data leak to user space in case of structure fill mismatch
749                  * between sys_rt_sigtimedwait & ia32_copy_siginfo_to_user.
750                  */
751                 memset(&info, 0, sizeof(info));
752         }
753         set_fs (KERNEL_DS);
754         ret = sys_rt_sigtimedwait(&s, uinfo ? &info : NULL, uts ? &t : NULL,
755                         sigsetsize);
756         set_fs (old_fs);
757         if (ret >= 0 && uinfo) {
758                 if (ia32_copy_siginfo_to_user(uinfo, &info))
759                         return -EFAULT;
760         }
761         return ret;
762 }
763
764 asmlinkage long
765 sys32_rt_sigqueueinfo(int pid, int sig, siginfo_t32 __user *uinfo)
766 {
767         siginfo_t info;
768         int ret;
769         mm_segment_t old_fs = get_fs();
770         
771         if (ia32_copy_siginfo_from_user(&info, uinfo))
772                 return -EFAULT;
773         set_fs (KERNEL_DS);
774         ret = sys_rt_sigqueueinfo(pid, sig, &info);
775         set_fs (old_fs);
776         return ret;
777 }
778
779 /* These are here just in case some old ia32 binary calls it. */
780 asmlinkage long
781 sys32_pause(void)
782 {
783         current->state = TASK_INTERRUPTIBLE;
784         schedule();
785         return -ERESTARTNOHAND;
786 }
787
788
789 struct sysctl_ia32 {
790         unsigned int    name;
791         int             nlen;
792         unsigned int    oldval;
793         unsigned int    oldlenp;
794         unsigned int    newval;
795         unsigned int    newlen;
796         unsigned int    __unused[4];
797 };
798
799
800 asmlinkage long
801 sys32_sysctl(struct sysctl_ia32 __user *args32)
802 {
803 #ifndef CONFIG_SYSCTL
804         return -ENOSYS; 
805 #else
806         struct sysctl_ia32 a32;
807         mm_segment_t old_fs = get_fs ();
808         void *oldvalp, *newvalp;
809         size_t oldlen;
810         int *namep;
811         long ret;
812         extern int do_sysctl(int *name, int nlen, void *oldval, size_t *oldlenp,
813                      void *newval, size_t newlen);
814
815
816         if (copy_from_user(&a32, args32, sizeof (a32)))
817                 return -EFAULT;
818
819         /*
820          * We need to pre-validate these because we have to disable address checking
821          * before calling do_sysctl() because of OLDLEN but we can't run the risk of the
822          * user specifying bad addresses here.  Well, since we're dealing with 32 bit
823          * addresses, we KNOW that access_ok() will always succeed, so this is an
824          * expensive NOP, but so what...
825          */
826         namep = (int *) A(a32.name);
827         oldvalp = (void *) A(a32.oldval);
828         newvalp = (void *) A(a32.newval);
829
830         if ((oldvalp && get_user(oldlen, (int __user *)compat_ptr(a32.oldlenp)))
831             || !access_ok(VERIFY_WRITE, namep, 0)
832             || !access_ok(VERIFY_WRITE, oldvalp, 0)
833             || !access_ok(VERIFY_WRITE, newvalp, 0))
834                 return -EFAULT;
835
836         set_fs(KERNEL_DS);
837         lock_kernel();
838         ret = do_sysctl(namep, a32.nlen, oldvalp, &oldlen, newvalp, (size_t) a32.newlen);
839         unlock_kernel();
840         set_fs(old_fs);
841
842         if (oldvalp && put_user (oldlen, (int __user *)compat_ptr(a32.oldlenp)))
843                 return -EFAULT;
844
845         return ret;
846 #endif
847 }
848
849 /* warning: next two assume little endian */ 
850 asmlinkage long
851 sys32_pread(unsigned int fd, char __user *ubuf, u32 count, u32 poslo, u32 poshi)
852 {
853         return sys_pread64(fd, ubuf, count,
854                          ((loff_t)AA(poshi) << 32) | AA(poslo));
855 }
856
857 asmlinkage long
858 sys32_pwrite(unsigned int fd, char __user *ubuf, u32 count, u32 poslo, u32 poshi)
859 {
860         return sys_pwrite64(fd, ubuf, count,
861                           ((loff_t)AA(poshi) << 32) | AA(poslo));
862 }
863
864
865 asmlinkage long
866 sys32_personality(unsigned long personality)
867 {
868         int ret;
869         if (personality(current->personality) == PER_LINUX32 && 
870                 personality == PER_LINUX)
871                 personality = PER_LINUX32;
872         ret = sys_personality(personality);
873         if (ret == PER_LINUX32)
874                 ret = PER_LINUX;
875         return ret;
876 }
877
878 asmlinkage long
879 sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset, s32 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 /* Handle adjtimex compatibility. */
899
900 struct timex32 {
901         u32 modes;
902         s32 offset, freq, maxerror, esterror;
903         s32 status, constant, precision, tolerance;
904         struct compat_timeval time;
905         s32 tick;
906         s32 ppsfreq, jitter, shift, stabil;
907         s32 jitcnt, calcnt, errcnt, stbcnt;
908         s32  :32; s32  :32; s32  :32; s32  :32;
909         s32  :32; s32  :32; s32  :32; s32  :32;
910         s32  :32; s32  :32; s32  :32; s32  :32;
911 };
912
913 extern int do_adjtimex(struct timex *);
914
915 asmlinkage long
916 sys32_adjtimex(struct timex32 __user *utp)
917 {
918         struct timex txc;
919         int ret;
920
921         memset(&txc, 0, sizeof(struct timex));
922
923         if(verify_area(VERIFY_READ, utp, sizeof(struct timex32)) ||
924            __get_user(txc.modes, &utp->modes) ||
925            __get_user(txc.offset, &utp->offset) ||
926            __get_user(txc.freq, &utp->freq) ||
927            __get_user(txc.maxerror, &utp->maxerror) ||
928            __get_user(txc.esterror, &utp->esterror) ||
929            __get_user(txc.status, &utp->status) ||
930            __get_user(txc.constant, &utp->constant) ||
931            __get_user(txc.precision, &utp->precision) ||
932            __get_user(txc.tolerance, &utp->tolerance) ||
933            __get_user(txc.time.tv_sec, &utp->time.tv_sec) ||
934            __get_user(txc.time.tv_usec, &utp->time.tv_usec) ||
935            __get_user(txc.tick, &utp->tick) ||
936            __get_user(txc.ppsfreq, &utp->ppsfreq) ||
937            __get_user(txc.jitter, &utp->jitter) ||
938            __get_user(txc.shift, &utp->shift) ||
939            __get_user(txc.stabil, &utp->stabil) ||
940            __get_user(txc.jitcnt, &utp->jitcnt) ||
941            __get_user(txc.calcnt, &utp->calcnt) ||
942            __get_user(txc.errcnt, &utp->errcnt) ||
943            __get_user(txc.stbcnt, &utp->stbcnt))
944                 return -EFAULT;
945
946         ret = do_adjtimex(&txc);
947
948         if(verify_area(VERIFY_WRITE, utp, sizeof(struct timex32)) ||
949            __put_user(txc.modes, &utp->modes) ||
950            __put_user(txc.offset, &utp->offset) ||
951            __put_user(txc.freq, &utp->freq) ||
952            __put_user(txc.maxerror, &utp->maxerror) ||
953            __put_user(txc.esterror, &utp->esterror) ||
954            __put_user(txc.status, &utp->status) ||
955            __put_user(txc.constant, &utp->constant) ||
956            __put_user(txc.precision, &utp->precision) ||
957            __put_user(txc.tolerance, &utp->tolerance) ||
958            __put_user(txc.time.tv_sec, &utp->time.tv_sec) ||
959            __put_user(txc.time.tv_usec, &utp->time.tv_usec) ||
960            __put_user(txc.tick, &utp->tick) ||
961            __put_user(txc.ppsfreq, &utp->ppsfreq) ||
962            __put_user(txc.jitter, &utp->jitter) ||
963            __put_user(txc.shift, &utp->shift) ||
964            __put_user(txc.stabil, &utp->stabil) ||
965            __put_user(txc.jitcnt, &utp->jitcnt) ||
966            __put_user(txc.calcnt, &utp->calcnt) ||
967            __put_user(txc.errcnt, &utp->errcnt) ||
968            __put_user(txc.stbcnt, &utp->stbcnt))
969                 ret = -EFAULT;
970
971         return ret;
972 }
973
974 asmlinkage long sys32_mmap2(unsigned long addr, unsigned long len,
975         unsigned long prot, unsigned long flags,
976         unsigned long fd, unsigned long pgoff)
977 {
978         struct mm_struct *mm = current->mm;
979         unsigned long error;
980         struct file * file = NULL;
981
982         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
983
984         if (flags & MAP_FIXED) {
985                 if (len > IA32_PAGE_OFFSET)
986                         return -EINVAL;
987                 if (addr > IA32_PAGE_OFFSET - len)
988                         return -ENOMEM;
989         }
990
991         if (!(flags & MAP_ANONYMOUS)) {
992                 file = fget(fd);
993                 if (!file)
994                         return -EBADF;
995         }
996
997         down_write(&mm->mmap_sem);
998         error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
999         up_write(&mm->mmap_sem);
1000
1001         if (file)
1002                 fput(file);
1003         return error;
1004 }
1005
1006 asmlinkage long sys32_olduname(struct oldold_utsname __user * name)
1007 {
1008         int error;
1009
1010         if (!name)
1011                 return -EFAULT;
1012         if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
1013                 return -EFAULT;
1014   
1015         down_read(&uts_sem);
1016         
1017         error = __copy_to_user(&name->sysname,&system_utsname.sysname,__OLD_UTS_LEN);
1018          __put_user(0,name->sysname+__OLD_UTS_LEN);
1019          __copy_to_user(&name->nodename,&system_utsname.nodename,__OLD_UTS_LEN);
1020          __put_user(0,name->nodename+__OLD_UTS_LEN);
1021          __copy_to_user(&name->release,&system_utsname.release,__OLD_UTS_LEN);
1022          __put_user(0,name->release+__OLD_UTS_LEN);
1023          __copy_to_user(&name->version,&system_utsname.version,__OLD_UTS_LEN);
1024          __put_user(0,name->version+__OLD_UTS_LEN);
1025          { 
1026                  char *arch = "x86_64";
1027                  if (personality(current->personality) == PER_LINUX32)
1028                          arch = "i686";
1029                  
1030                  __copy_to_user(&name->machine,arch,strlen(arch)+1);
1031          }
1032         
1033          up_read(&uts_sem);
1034          
1035          error = error ? -EFAULT : 0;
1036          
1037          return error;
1038 }
1039
1040 long sys32_uname(struct old_utsname __user * name)
1041 {
1042         int err;
1043         if (!name)
1044                 return -EFAULT;
1045         down_read(&uts_sem);
1046         err=copy_to_user(name, &system_utsname, sizeof (*name));
1047         up_read(&uts_sem);
1048         if (personality(current->personality) == PER_LINUX32) 
1049                 err |= copy_to_user(&name->machine, "i686", 5);
1050         return err?-EFAULT:0;
1051 }
1052
1053 long sys32_ustat(unsigned dev, struct ustat32 __user *u32p)
1054 {
1055         struct ustat u;
1056         mm_segment_t seg;
1057         int ret;
1058         
1059         seg = get_fs(); 
1060         set_fs(KERNEL_DS); 
1061         ret = sys_ustat(dev,&u); 
1062         set_fs(seg);
1063         if (ret >= 0) { 
1064                 if (!access_ok(VERIFY_WRITE,u32p,sizeof(struct ustat32)) || 
1065                     __put_user((__u32) u.f_tfree, &u32p->f_tfree) ||
1066                     __put_user((__u32) u.f_tinode, &u32p->f_tfree) ||
1067                     __copy_to_user(&u32p->f_fname, u.f_fname, sizeof(u.f_fname)) ||
1068                     __copy_to_user(&u32p->f_fpack, u.f_fpack, sizeof(u.f_fpack)))
1069                         ret = -EFAULT;
1070         }
1071         return ret;
1072
1073
1074 asmlinkage long sys32_execve(char __user *name, compat_uptr_t __user *argv,
1075                              compat_uptr_t __user *envp, struct pt_regs *regs)
1076 {
1077         long error;
1078         char * filename;
1079
1080         filename = getname(name);
1081         error = PTR_ERR(filename);
1082         if (IS_ERR(filename))
1083                 return error;
1084         error = compat_do_execve(filename, argv, envp, regs);
1085         if (error == 0) {
1086                 task_lock(current);
1087                 current->ptrace &= ~PT_DTRACE;
1088                 task_unlock(current);
1089         }
1090         putname(filename);
1091         return error;
1092 }
1093
1094 asmlinkage long sys32_clone(unsigned int clone_flags, unsigned int newsp,
1095                             struct pt_regs *regs)
1096 {
1097         void __user *parent_tid = (void __user *)regs->rdx;
1098         void __user *child_tid = (void __user *)regs->rdi;
1099         if (!newsp)
1100                 newsp = regs->rsp;
1101         return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
1102 }
1103
1104 asmlinkage long sys32_waitid(int which, compat_pid_t pid,
1105                              siginfo_t32 __user *uinfo, int options,
1106                              struct compat_rusage __user *uru)
1107 {
1108         siginfo_t info;
1109         struct rusage ru;
1110         long ret;
1111         mm_segment_t old_fs = get_fs();
1112
1113         info.si_signo = 0;
1114         set_fs (KERNEL_DS);
1115         ret = sys_waitid(which, pid, (siginfo_t __user *) &info, options,
1116                          uru ? &ru : NULL);
1117         set_fs (old_fs);
1118
1119         if (ret < 0 || info.si_signo == 0)
1120                 return ret;
1121
1122         if (uru && (ret = put_compat_rusage(&ru, uru)))
1123                 return ret;
1124
1125         BUG_ON(info.si_code & __SI_MASK);
1126         info.si_code |= __SI_CHLD;
1127         return ia32_copy_siginfo_to_user(uinfo, &info);
1128 }
1129
1130 /*
1131  * Some system calls that need sign extended arguments. This could be done by a generic wrapper.
1132  */ 
1133
1134 long sys32_lseek (unsigned int fd, int offset, unsigned int whence)
1135 {
1136         return sys_lseek(fd, offset, whence);
1137 }
1138
1139 long sys32_kill(int pid, int sig)
1140 {
1141         return sys_kill(pid, sig);
1142 }
1143  
1144 asmlinkage long sys32_open(const char __user * filename, int flags, int mode)
1145 {
1146         char * tmp;
1147         int fd, error;
1148
1149         /* don't force O_LARGEFILE */
1150         tmp = getname(filename);
1151         fd = PTR_ERR(tmp);
1152         if (!IS_ERR(tmp)) {
1153                 fd = get_unused_fd();
1154                 if (fd >= 0) {
1155                         struct file *f = filp_open(tmp, flags, mode);
1156                         error = PTR_ERR(f);
1157                         if (IS_ERR(f)) {
1158                                 put_unused_fd(fd); 
1159                                 fd = error;
1160                         } else
1161                                 fd_install(fd, f);
1162                 }
1163                 putname(tmp);
1164         }
1165         return fd;
1166 }
1167
1168 struct sigevent32 { 
1169         u32 sigev_value;
1170         u32 sigev_signo; 
1171         u32 sigev_notify; 
1172         u32 payload[(64 / 4) - 3]; 
1173 }; 
1174
1175 extern asmlinkage long
1176 sys_timer_create(clockid_t which_clock,
1177                  struct sigevent __user *timer_event_spec,
1178                  timer_t __user * created_timer_id);
1179
1180 long
1181 sys32_timer_create(u32 clock, struct sigevent32 __user *se32, timer_t __user *timer_id)
1182 {
1183         struct sigevent __user *p = NULL;
1184         if (se32) { 
1185                 struct sigevent se;
1186                 p = compat_alloc_user_space(sizeof(struct sigevent));
1187                 memset(&se, 0, sizeof(struct sigevent)); 
1188                 if (get_user(se.sigev_value.sival_int,  &se32->sigev_value) ||
1189                     __get_user(se.sigev_signo, &se32->sigev_signo) ||
1190                     __get_user(se.sigev_notify, &se32->sigev_notify) ||
1191                     __copy_from_user(&se._sigev_un._pad, &se32->payload, 
1192                                      sizeof(se32->payload)) ||
1193                     copy_to_user(p, &se, sizeof(se)))
1194                         return -EFAULT;
1195         } 
1196         return sys_timer_create(clock, p, timer_id);
1197
1198
1199 long sys32_fadvise64_64(int fd, __u32 offset_low, __u32 offset_high, 
1200                         __u32 len_low, __u32 len_high, int advice)
1201
1202         return sys_fadvise64_64(fd,
1203                                (((u64)offset_high)<<32) | offset_low,
1204                                (((u64)len_high)<<32) | len_low,
1205                                advice); 
1206
1207
1208 long sys32_vm86_warning(void)
1209
1210         struct task_struct *me = current;
1211         static char lastcomm[sizeof(me->comm)];
1212         if (strncmp(lastcomm, me->comm, sizeof(lastcomm))) {
1213                 printk(KERN_INFO "%s: vm86 mode not supported on 64 bit kernel\n",
1214                        me->comm);
1215                 strncpy(lastcomm, me->comm, sizeof(lastcomm));
1216         } 
1217         return -ENOSYS;
1218
1219
1220 long sys32_lookup_dcookie(u32 addr_low, u32 addr_high,
1221                           char __user * buf, size_t len)
1222 {
1223         return sys_lookup_dcookie(((u64)addr_high << 32) | addr_low, buf, len);
1224 }
1225
1226 cond_syscall(sys32_ipc)
1227
1228 static int __init ia32_init (void)
1229 {
1230         printk("IA32 emulation $Id: sys_ia32.c,v 1.32 2002/03/24 13:02:28 ak Exp $\n");  
1231         return 0;
1232 }
1233
1234 __initcall(ia32_init);
1235
1236 extern unsigned long ia32_sys_call_table[];