kernel.org linux-2.6.10
[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 <asm/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_ANONYMOUS)) {
216                 file = fget(a.fd);
217                 if (!file)
218                         return -EBADF;
219         }
220         
221         mm = current->mm; 
222         down_write(&mm->mmap_sem); 
223         retval = do_mmap_pgoff(file, a.addr, a.len, a.prot, a.flags, a.offset>>PAGE_SHIFT);
224         if (file)
225                 fput(file);
226
227         up_write(&mm->mmap_sem); 
228
229         return retval;
230 }
231
232 asmlinkage long 
233 sys32_mprotect(unsigned long start, size_t len, unsigned long prot)
234 {
235         return sys_mprotect(start,len,prot); 
236 }
237
238 asmlinkage long
239 sys32_pipe(int __user *fd)
240 {
241         int retval;
242         int fds[2];
243
244         retval = do_pipe(fds);
245         if (retval)
246                 goto out;
247         if (copy_to_user(fd, fds, sizeof(fds)))
248                 retval = -EFAULT;
249   out:
250         return retval;
251 }
252
253 asmlinkage long
254 sys32_rt_sigaction(int sig, struct sigaction32 __user *act,
255                    struct sigaction32 __user *oact,  unsigned int sigsetsize)
256 {
257         struct k_sigaction new_ka, old_ka;
258         int ret;
259         compat_sigset_t set32;
260
261         /* XXX: Don't preclude handling different sized sigset_t's.  */
262         if (sigsetsize != sizeof(compat_sigset_t))
263                 return -EINVAL;
264
265         if (act) {
266                 compat_uptr_t handler, restorer;
267
268                 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
269                     __get_user(handler, &act->sa_handler) ||
270                     __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
271                     __get_user(restorer, &act->sa_restorer)||
272                     __copy_from_user(&set32, &act->sa_mask, sizeof(compat_sigset_t)))
273                         return -EFAULT;
274                 new_ka.sa.sa_handler = compat_ptr(handler);
275                 new_ka.sa.sa_restorer = compat_ptr(restorer);
276                 /* FIXME: here we rely on _COMPAT_NSIG_WORS to be >= than _NSIG_WORDS << 1 */
277                 switch (_NSIG_WORDS) {
278                 case 4: new_ka.sa.sa_mask.sig[3] = set32.sig[6]
279                                 | (((long)set32.sig[7]) << 32);
280                 case 3: new_ka.sa.sa_mask.sig[2] = set32.sig[4]
281                                 | (((long)set32.sig[5]) << 32);
282                 case 2: new_ka.sa.sa_mask.sig[1] = set32.sig[2]
283                                 | (((long)set32.sig[3]) << 32);
284                 case 1: new_ka.sa.sa_mask.sig[0] = set32.sig[0]
285                                 | (((long)set32.sig[1]) << 32);
286                 }
287         }
288
289         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
290
291         if (!ret && oact) {
292                 /* FIXME: here we rely on _COMPAT_NSIG_WORS to be >= than _NSIG_WORDS << 1 */
293                 switch (_NSIG_WORDS) {
294                 case 4:
295                         set32.sig[7] = (old_ka.sa.sa_mask.sig[3] >> 32);
296                         set32.sig[6] = old_ka.sa.sa_mask.sig[3];
297                 case 3:
298                         set32.sig[5] = (old_ka.sa.sa_mask.sig[2] >> 32);
299                         set32.sig[4] = old_ka.sa.sa_mask.sig[2];
300                 case 2:
301                         set32.sig[3] = (old_ka.sa.sa_mask.sig[1] >> 32);
302                         set32.sig[2] = old_ka.sa.sa_mask.sig[1];
303                 case 1:
304                         set32.sig[1] = (old_ka.sa.sa_mask.sig[0] >> 32);
305                         set32.sig[0] = old_ka.sa.sa_mask.sig[0];
306                 }
307                 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
308                     __put_user((long)old_ka.sa.sa_handler, &oact->sa_handler) ||
309                     __put_user((long)old_ka.sa.sa_restorer, &oact->sa_restorer) ||
310                     __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
311                     __copy_to_user(&oact->sa_mask, &set32, sizeof(compat_sigset_t)))
312                         return -EFAULT;
313         }
314
315         return ret;
316 }
317
318 asmlinkage long
319 sys32_sigaction (int sig, struct old_sigaction32 __user *act, struct old_sigaction32 __user *oact)
320 {
321         struct k_sigaction new_ka, old_ka;
322         int ret;
323
324         if (act) {
325                 compat_old_sigset_t mask;
326                 compat_uptr_t handler, restorer;
327
328                 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
329                     __get_user(handler, &act->sa_handler) ||
330                     __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
331                     __get_user(restorer, &act->sa_restorer) ||
332                     __get_user(mask, &act->sa_mask))
333                         return -EFAULT;
334
335                 new_ka.sa.sa_handler = compat_ptr(handler);
336                 new_ka.sa.sa_restorer = compat_ptr(restorer);
337
338                 siginitset(&new_ka.sa.sa_mask, mask);
339         }
340
341         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
342
343         if (!ret && oact) {
344                 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
345                     __put_user((long)old_ka.sa.sa_handler, &oact->sa_handler) ||
346                     __put_user((long)old_ka.sa.sa_restorer, &oact->sa_restorer) ||
347                     __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
348                     __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
349                         return -EFAULT;
350         }
351
352         return ret;
353 }
354
355 asmlinkage long
356 sys32_rt_sigprocmask(int how, compat_sigset_t __user *set,
357                         compat_sigset_t __user *oset, unsigned int sigsetsize)
358 {
359         sigset_t s;
360         compat_sigset_t s32;
361         int ret;
362         mm_segment_t old_fs = get_fs();
363         
364         if (set) {
365                 if (copy_from_user (&s32, set, sizeof(compat_sigset_t)))
366                         return -EFAULT;
367                 switch (_NSIG_WORDS) {
368                 case 4: s.sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
369                 case 3: s.sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
370                 case 2: s.sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
371                 case 1: s.sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
372                 }
373         }
374         set_fs (KERNEL_DS);
375         ret = sys_rt_sigprocmask(how, set ? &s : NULL, oset ? &s : NULL,
376                                  sigsetsize); 
377         set_fs (old_fs);
378         if (ret) return ret;
379         if (oset) {
380                 switch (_NSIG_WORDS) {
381                 case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
382                 case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
383                 case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
384                 case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
385                 }
386                 if (copy_to_user (oset, &s32, sizeof(compat_sigset_t)))
387                         return -EFAULT;
388         }
389         return 0;
390 }
391
392 static inline long
393 get_tv32(struct timeval *o, struct compat_timeval __user *i)
394 {
395         int err = -EFAULT; 
396         if (access_ok(VERIFY_READ, i, sizeof(*i))) { 
397                 err = __get_user(o->tv_sec, &i->tv_sec);
398                 err |= __get_user(o->tv_usec, &i->tv_usec);
399         }
400         return err; 
401 }
402
403 static inline long
404 put_tv32(struct compat_timeval __user *o, struct timeval *i)
405 {
406         int err = -EFAULT;
407         if (access_ok(VERIFY_WRITE, o, sizeof(*o))) { 
408                 err = __put_user(i->tv_sec, &o->tv_sec);
409                 err |= __put_user(i->tv_usec, &o->tv_usec);
410         } 
411         return err; 
412 }
413
414 extern int do_setitimer(int which, struct itimerval *, struct itimerval *);
415
416 asmlinkage long
417 sys32_alarm(unsigned int seconds)
418 {
419         struct itimerval it_new, it_old;
420         unsigned int oldalarm;
421
422         it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
423         it_new.it_value.tv_sec = seconds;
424         it_new.it_value.tv_usec = 0;
425         do_setitimer(ITIMER_REAL, &it_new, &it_old);
426         oldalarm = it_old.it_value.tv_sec;
427         /* ehhh.. We can't return 0 if we have an alarm pending.. */
428         /* And we'd better return too much than too little anyway */
429         if (it_old.it_value.tv_usec)
430                 oldalarm++;
431         return oldalarm;
432 }
433
434 /* Translations due to time_t size differences.  Which affects all
435    sorts of things, like timeval and itimerval.  */
436
437 extern struct timezone sys_tz;
438
439 asmlinkage long
440 sys32_gettimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
441 {
442         if (tv) {
443                 struct timeval ktv;
444                 do_gettimeofday(&ktv);
445                 if (put_tv32(tv, &ktv))
446                         return -EFAULT;
447         }
448         if (tz) {
449                 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
450                         return -EFAULT;
451         }
452         return 0;
453 }
454
455 asmlinkage long
456 sys32_settimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
457 {
458         struct timeval ktv;
459         struct timespec kts;
460         struct timezone ktz;
461
462         if (tv) {
463                 if (get_tv32(&ktv, tv))
464                         return -EFAULT;
465                 kts.tv_sec = ktv.tv_sec;
466                 kts.tv_nsec = ktv.tv_usec * NSEC_PER_USEC;
467         }
468         if (tz) {
469                 if (copy_from_user(&ktz, tz, sizeof(ktz)))
470                         return -EFAULT;
471         }
472
473         return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
474 }
475
476 struct sel_arg_struct {
477         unsigned int n;
478         unsigned int inp;
479         unsigned int outp;
480         unsigned int exp;
481         unsigned int tvp;
482 };
483
484 asmlinkage long
485 sys32_old_select(struct sel_arg_struct __user *arg)
486 {
487         struct sel_arg_struct a;
488
489         if (copy_from_user(&a, arg, sizeof(a)))
490                 return -EFAULT;
491         return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
492                                  compat_ptr(a.exp), compat_ptr(a.tvp));
493 }
494
495 /*
496  * sys_time() can be implemented in user-level using
497  * sys_gettimeofday().  x86-64 did this but i386 Linux did not
498  * so we have to implement this system call here.
499  */
500 asmlinkage long sys32_time(int __user * tloc)
501 {
502         int i;
503         struct timeval tv;
504
505         do_gettimeofday(&tv);
506         i = tv.tv_sec;
507
508         if (tloc) {
509                 if (put_user(i,tloc))
510                         i = -EFAULT;
511         }
512         return i;
513 }
514
515 extern asmlinkage long
516 compat_sys_wait4(compat_pid_t pid, compat_uint_t * stat_addr, int options,
517                  struct compat_rusage *ru);
518
519 asmlinkage long
520 sys32_waitpid(compat_pid_t pid, unsigned int *stat_addr, int options)
521 {
522         return compat_sys_wait4(pid, stat_addr, options, NULL);
523 }
524
525 int sys32_ni_syscall(int call)
526
527         struct task_struct *me = current;
528         static char lastcomm[sizeof(me->comm)];
529
530         if (strncmp(lastcomm, me->comm, sizeof(lastcomm))) {
531                 printk(KERN_INFO "IA32 syscall %d from %s not implemented\n",
532                        call, me->comm);
533                 strncpy(lastcomm, me->comm, sizeof(lastcomm));
534         } 
535         return -ENOSYS;        
536
537
538 /* 32-bit timeval and related flotsam.  */
539
540 asmlinkage long
541 sys32_sysfs(int option, u32 arg1, u32 arg2)
542 {
543         return sys_sysfs(option, arg1, arg2);
544 }
545
546 struct sysinfo32 {
547         s32 uptime;
548         u32 loads[3];
549         u32 totalram;
550         u32 freeram;
551         u32 sharedram;
552         u32 bufferram;
553         u32 totalswap;
554         u32 freeswap;
555         unsigned short procs;
556         unsigned short pad; 
557         u32 totalhigh;
558         u32 freehigh;
559         u32 mem_unit;
560         char _f[20-2*sizeof(u32)-sizeof(int)];
561 };
562
563 asmlinkage long
564 sys32_sysinfo(struct sysinfo32 __user *info)
565 {
566         struct sysinfo s;
567         int ret;
568         mm_segment_t old_fs = get_fs ();
569         int bitcount = 0;
570         
571         set_fs (KERNEL_DS);
572         ret = sys_sysinfo(&s);
573         set_fs (old_fs);
574
575         /* Check to see if any memory value is too large for 32-bit and scale
576          *  down if needed
577          */
578         if ((s.totalram >> 32) || (s.totalswap >> 32)) {
579                 while (s.mem_unit < PAGE_SIZE) {
580                         s.mem_unit <<= 1;
581                         bitcount++;
582                 }
583                 s.totalram >>= bitcount;
584                 s.freeram >>= bitcount;
585                 s.sharedram >>= bitcount;
586                 s.bufferram >>= bitcount;
587                 s.totalswap >>= bitcount;
588                 s.freeswap >>= bitcount;
589                 s.totalhigh >>= bitcount;
590                 s.freehigh >>= bitcount;
591         }
592
593         if (verify_area(VERIFY_WRITE, info, sizeof(struct sysinfo32)) ||
594             __put_user (s.uptime, &info->uptime) ||
595             __put_user (s.loads[0], &info->loads[0]) ||
596             __put_user (s.loads[1], &info->loads[1]) ||
597             __put_user (s.loads[2], &info->loads[2]) ||
598             __put_user (s.totalram, &info->totalram) ||
599             __put_user (s.freeram, &info->freeram) ||
600             __put_user (s.sharedram, &info->sharedram) ||
601             __put_user (s.bufferram, &info->bufferram) ||
602             __put_user (s.totalswap, &info->totalswap) ||
603             __put_user (s.freeswap, &info->freeswap) ||
604             __put_user (s.procs, &info->procs) ||
605             __put_user (s.totalhigh, &info->totalhigh) || 
606             __put_user (s.freehigh, &info->freehigh) ||
607             __put_user (s.mem_unit, &info->mem_unit))
608                 return -EFAULT;
609         return 0;
610 }
611                 
612 asmlinkage long
613 sys32_sched_rr_get_interval(compat_pid_t pid, struct compat_timespec __user *interval)
614 {
615         struct timespec t;
616         int ret;
617         mm_segment_t old_fs = get_fs ();
618         
619         set_fs (KERNEL_DS);
620         ret = sys_sched_rr_get_interval(pid, &t);
621         set_fs (old_fs);
622         if (put_compat_timespec(&t, interval))
623                 return -EFAULT;
624         return ret;
625 }
626
627 asmlinkage long
628 sys32_rt_sigpending(compat_sigset_t __user *set, compat_size_t sigsetsize)
629 {
630         sigset_t s;
631         compat_sigset_t s32;
632         int ret;
633         mm_segment_t old_fs = get_fs();
634                 
635         set_fs (KERNEL_DS);
636         ret = sys_rt_sigpending(&s, sigsetsize);
637         set_fs (old_fs);
638         if (!ret) {
639                 switch (_NSIG_WORDS) {
640                 case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
641                 case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
642                 case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
643                 case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
644                 }
645                 if (copy_to_user (set, &s32, sizeof(compat_sigset_t)))
646                         return -EFAULT;
647         }
648         return ret;
649 }
650
651
652 asmlinkage long
653 sys32_rt_sigtimedwait(compat_sigset_t __user *uthese, siginfo_t32 __user *uinfo,
654                       struct compat_timespec __user *uts, compat_size_t sigsetsize)
655 {
656         sigset_t s;
657         compat_sigset_t s32;
658         struct timespec t;
659         int ret;
660         mm_segment_t old_fs = get_fs();
661         siginfo_t info;
662                 
663         if (copy_from_user (&s32, uthese, sizeof(compat_sigset_t)))
664                 return -EFAULT;
665         switch (_NSIG_WORDS) {
666         case 4: s.sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
667         case 3: s.sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
668         case 2: s.sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
669         case 1: s.sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
670         }
671         if (uts && get_compat_timespec(&t, uts))
672                 return -EFAULT;
673         if (uinfo) {
674                 /* stop data leak to user space in case of structure fill mismatch
675                  * between sys_rt_sigtimedwait & ia32_copy_siginfo_to_user.
676                  */
677                 memset(&info, 0, sizeof(info));
678         }
679         set_fs (KERNEL_DS);
680         ret = sys_rt_sigtimedwait(&s, uinfo ? &info : NULL, uts ? &t : NULL,
681                         sigsetsize);
682         set_fs (old_fs);
683         if (ret >= 0 && uinfo) {
684                 if (ia32_copy_siginfo_to_user(uinfo, &info))
685                         return -EFAULT;
686         }
687         return ret;
688 }
689
690 asmlinkage long
691 sys32_rt_sigqueueinfo(int pid, int sig, siginfo_t32 __user *uinfo)
692 {
693         siginfo_t info;
694         int ret;
695         mm_segment_t old_fs = get_fs();
696         
697         if (ia32_copy_siginfo_from_user(&info, uinfo))
698                 return -EFAULT;
699         set_fs (KERNEL_DS);
700         ret = sys_rt_sigqueueinfo(pid, sig, &info);
701         set_fs (old_fs);
702         return ret;
703 }
704
705 /* These are here just in case some old ia32 binary calls it. */
706 asmlinkage long
707 sys32_pause(void)
708 {
709         current->state = TASK_INTERRUPTIBLE;
710         schedule();
711         return -ERESTARTNOHAND;
712 }
713
714
715 struct sysctl_ia32 {
716         unsigned int    name;
717         int             nlen;
718         unsigned int    oldval;
719         unsigned int    oldlenp;
720         unsigned int    newval;
721         unsigned int    newlen;
722         unsigned int    __unused[4];
723 };
724
725
726 asmlinkage long
727 sys32_sysctl(struct sysctl_ia32 __user *args32)
728 {
729 #ifndef CONFIG_SYSCTL
730         return -ENOSYS; 
731 #else
732         struct sysctl_ia32 a32;
733         mm_segment_t old_fs = get_fs ();
734         void *oldvalp, *newvalp;
735         size_t oldlen;
736         int *namep;
737         long ret;
738         extern int do_sysctl(int *name, int nlen, void *oldval, size_t *oldlenp,
739                      void *newval, size_t newlen);
740
741
742         if (copy_from_user(&a32, args32, sizeof (a32)))
743                 return -EFAULT;
744
745         /*
746          * We need to pre-validate these because we have to disable address checking
747          * before calling do_sysctl() because of OLDLEN but we can't run the risk of the
748          * user specifying bad addresses here.  Well, since we're dealing with 32 bit
749          * addresses, we KNOW that access_ok() will always succeed, so this is an
750          * expensive NOP, but so what...
751          */
752         namep = (int *) A(a32.name);
753         oldvalp = (void *) A(a32.oldval);
754         newvalp = (void *) A(a32.newval);
755
756         if ((oldvalp && get_user(oldlen, (int __user *)compat_ptr(a32.oldlenp)))
757             || !access_ok(VERIFY_WRITE, namep, 0)
758             || !access_ok(VERIFY_WRITE, oldvalp, 0)
759             || !access_ok(VERIFY_WRITE, newvalp, 0))
760                 return -EFAULT;
761
762         set_fs(KERNEL_DS);
763         lock_kernel();
764         ret = do_sysctl(namep, a32.nlen, oldvalp, &oldlen, newvalp, (size_t) a32.newlen);
765         unlock_kernel();
766         set_fs(old_fs);
767
768         if (oldvalp && put_user (oldlen, (int __user *)compat_ptr(a32.oldlenp)))
769                 return -EFAULT;
770
771         return ret;
772 #endif
773 }
774
775 /* warning: next two assume little endian */ 
776 asmlinkage long
777 sys32_pread(unsigned int fd, char __user *ubuf, u32 count, u32 poslo, u32 poshi)
778 {
779         return sys_pread64(fd, ubuf, count,
780                          ((loff_t)AA(poshi) << 32) | AA(poslo));
781 }
782
783 asmlinkage long
784 sys32_pwrite(unsigned int fd, char __user *ubuf, u32 count, u32 poslo, u32 poshi)
785 {
786         return sys_pwrite64(fd, ubuf, count,
787                           ((loff_t)AA(poshi) << 32) | AA(poslo));
788 }
789
790
791 asmlinkage long
792 sys32_personality(unsigned long personality)
793 {
794         int ret;
795         if (personality(current->personality) == PER_LINUX32 && 
796                 personality == PER_LINUX)
797                 personality = PER_LINUX32;
798         ret = sys_personality(personality);
799         if (ret == PER_LINUX32)
800                 ret = PER_LINUX;
801         return ret;
802 }
803
804 asmlinkage long
805 sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset, s32 count)
806 {
807         mm_segment_t old_fs = get_fs();
808         int ret;
809         off_t of;
810         
811         if (offset && get_user(of, offset))
812                 return -EFAULT;
813                 
814         set_fs(KERNEL_DS);
815         ret = sys_sendfile(out_fd, in_fd, offset ? &of : NULL, count);
816         set_fs(old_fs);
817         
818         if (!ret && offset && put_user(of, offset))
819                 return -EFAULT;
820                 
821         return ret;
822 }
823
824 /* Handle adjtimex compatibility. */
825
826 struct timex32 {
827         u32 modes;
828         s32 offset, freq, maxerror, esterror;
829         s32 status, constant, precision, tolerance;
830         struct compat_timeval time;
831         s32 tick;
832         s32 ppsfreq, jitter, shift, stabil;
833         s32 jitcnt, calcnt, errcnt, stbcnt;
834         s32  :32; s32  :32; s32  :32; s32  :32;
835         s32  :32; s32  :32; s32  :32; s32  :32;
836         s32  :32; s32  :32; s32  :32; s32  :32;
837 };
838
839 extern int do_adjtimex(struct timex *);
840
841 asmlinkage long
842 sys32_adjtimex(struct timex32 __user *utp)
843 {
844         struct timex txc;
845         int ret;
846
847         memset(&txc, 0, sizeof(struct timex));
848
849         if(verify_area(VERIFY_READ, utp, sizeof(struct timex32)) ||
850            __get_user(txc.modes, &utp->modes) ||
851            __get_user(txc.offset, &utp->offset) ||
852            __get_user(txc.freq, &utp->freq) ||
853            __get_user(txc.maxerror, &utp->maxerror) ||
854            __get_user(txc.esterror, &utp->esterror) ||
855            __get_user(txc.status, &utp->status) ||
856            __get_user(txc.constant, &utp->constant) ||
857            __get_user(txc.precision, &utp->precision) ||
858            __get_user(txc.tolerance, &utp->tolerance) ||
859            __get_user(txc.time.tv_sec, &utp->time.tv_sec) ||
860            __get_user(txc.time.tv_usec, &utp->time.tv_usec) ||
861            __get_user(txc.tick, &utp->tick) ||
862            __get_user(txc.ppsfreq, &utp->ppsfreq) ||
863            __get_user(txc.jitter, &utp->jitter) ||
864            __get_user(txc.shift, &utp->shift) ||
865            __get_user(txc.stabil, &utp->stabil) ||
866            __get_user(txc.jitcnt, &utp->jitcnt) ||
867            __get_user(txc.calcnt, &utp->calcnt) ||
868            __get_user(txc.errcnt, &utp->errcnt) ||
869            __get_user(txc.stbcnt, &utp->stbcnt))
870                 return -EFAULT;
871
872         ret = do_adjtimex(&txc);
873
874         if(verify_area(VERIFY_WRITE, utp, sizeof(struct timex32)) ||
875            __put_user(txc.modes, &utp->modes) ||
876            __put_user(txc.offset, &utp->offset) ||
877            __put_user(txc.freq, &utp->freq) ||
878            __put_user(txc.maxerror, &utp->maxerror) ||
879            __put_user(txc.esterror, &utp->esterror) ||
880            __put_user(txc.status, &utp->status) ||
881            __put_user(txc.constant, &utp->constant) ||
882            __put_user(txc.precision, &utp->precision) ||
883            __put_user(txc.tolerance, &utp->tolerance) ||
884            __put_user(txc.time.tv_sec, &utp->time.tv_sec) ||
885            __put_user(txc.time.tv_usec, &utp->time.tv_usec) ||
886            __put_user(txc.tick, &utp->tick) ||
887            __put_user(txc.ppsfreq, &utp->ppsfreq) ||
888            __put_user(txc.jitter, &utp->jitter) ||
889            __put_user(txc.shift, &utp->shift) ||
890            __put_user(txc.stabil, &utp->stabil) ||
891            __put_user(txc.jitcnt, &utp->jitcnt) ||
892            __put_user(txc.calcnt, &utp->calcnt) ||
893            __put_user(txc.errcnt, &utp->errcnt) ||
894            __put_user(txc.stbcnt, &utp->stbcnt))
895                 ret = -EFAULT;
896
897         return ret;
898 }
899
900 asmlinkage long sys32_mmap2(unsigned long addr, unsigned long len,
901         unsigned long prot, unsigned long flags,
902         unsigned long fd, unsigned long pgoff)
903 {
904         struct mm_struct *mm = current->mm;
905         unsigned long error;
906         struct file * file = NULL;
907
908         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
909         if (!(flags & MAP_ANONYMOUS)) {
910                 file = fget(fd);
911                 if (!file)
912                         return -EBADF;
913         }
914
915         down_write(&mm->mmap_sem);
916         error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
917         up_write(&mm->mmap_sem);
918
919         if (file)
920                 fput(file);
921         return error;
922 }
923
924 asmlinkage long sys32_olduname(struct oldold_utsname __user * name)
925 {
926         int error;
927
928         if (!name)
929                 return -EFAULT;
930         if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
931                 return -EFAULT;
932   
933         down_read(&uts_sem);
934         
935         error = __copy_to_user(&name->sysname,&system_utsname.sysname,__OLD_UTS_LEN);
936          __put_user(0,name->sysname+__OLD_UTS_LEN);
937          __copy_to_user(&name->nodename,&system_utsname.nodename,__OLD_UTS_LEN);
938          __put_user(0,name->nodename+__OLD_UTS_LEN);
939          __copy_to_user(&name->release,&system_utsname.release,__OLD_UTS_LEN);
940          __put_user(0,name->release+__OLD_UTS_LEN);
941          __copy_to_user(&name->version,&system_utsname.version,__OLD_UTS_LEN);
942          __put_user(0,name->version+__OLD_UTS_LEN);
943          { 
944                  char *arch = "x86_64";
945                  if (personality(current->personality) == PER_LINUX32)
946                          arch = "i686";
947                  
948                  __copy_to_user(&name->machine,arch,strlen(arch)+1);
949          }
950         
951          up_read(&uts_sem);
952          
953          error = error ? -EFAULT : 0;
954          
955          return error;
956 }
957
958 long sys32_uname(struct old_utsname __user * name)
959 {
960         int err;
961         if (!name)
962                 return -EFAULT;
963         down_read(&uts_sem);
964         err=copy_to_user(name, &system_utsname, sizeof (*name));
965         up_read(&uts_sem);
966         if (personality(current->personality) == PER_LINUX32) 
967                 err |= copy_to_user(&name->machine, "i686", 5);
968         return err?-EFAULT:0;
969 }
970
971 long sys32_ustat(unsigned dev, struct ustat32 __user *u32p)
972 {
973         struct ustat u;
974         mm_segment_t seg;
975         int ret;
976         
977         seg = get_fs(); 
978         set_fs(KERNEL_DS); 
979         ret = sys_ustat(dev,&u); 
980         set_fs(seg);
981         if (ret >= 0) { 
982                 if (!access_ok(VERIFY_WRITE,u32p,sizeof(struct ustat32)) || 
983                     __put_user((__u32) u.f_tfree, &u32p->f_tfree) ||
984                     __put_user((__u32) u.f_tinode, &u32p->f_tfree) ||
985                     __copy_to_user(&u32p->f_fname, u.f_fname, sizeof(u.f_fname)) ||
986                     __copy_to_user(&u32p->f_fpack, u.f_fpack, sizeof(u.f_fpack)))
987                         ret = -EFAULT;
988         }
989         return ret;
990
991
992 asmlinkage long sys32_execve(char __user *name, compat_uptr_t __user *argv,
993                              compat_uptr_t __user *envp, struct pt_regs *regs)
994 {
995         long error;
996         char * filename;
997
998         filename = getname(name);
999         error = PTR_ERR(filename);
1000         if (IS_ERR(filename))
1001                 return error;
1002         error = compat_do_execve(filename, argv, envp, regs);
1003         if (error == 0) {
1004                 task_lock(current);
1005                 current->ptrace &= ~PT_DTRACE;
1006                 task_unlock(current);
1007         }
1008         putname(filename);
1009         return error;
1010 }
1011
1012 asmlinkage long sys32_clone(unsigned int clone_flags, unsigned int newsp,
1013                             struct pt_regs *regs)
1014 {
1015         void __user *parent_tid = (void __user *)regs->rdx;
1016         void __user *child_tid = (void __user *)regs->rdi;
1017         if (!newsp)
1018                 newsp = regs->rsp;
1019         return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
1020 }
1021
1022 asmlinkage long sys32_waitid(int which, compat_pid_t pid,
1023                              siginfo_t32 __user *uinfo, int options,
1024                              struct compat_rusage __user *uru)
1025 {
1026         siginfo_t info;
1027         struct rusage ru;
1028         long ret;
1029         mm_segment_t old_fs = get_fs();
1030
1031         info.si_signo = 0;
1032         set_fs (KERNEL_DS);
1033         ret = sys_waitid(which, pid, (siginfo_t __user *) &info, options,
1034                          uru ? &ru : NULL);
1035         set_fs (old_fs);
1036
1037         if (ret < 0 || info.si_signo == 0)
1038                 return ret;
1039
1040         if (uru && (ret = put_compat_rusage(&ru, uru)))
1041                 return ret;
1042
1043         BUG_ON(info.si_code & __SI_MASK);
1044         info.si_code |= __SI_CHLD;
1045         return ia32_copy_siginfo_to_user(uinfo, &info);
1046 }
1047
1048 /*
1049  * Some system calls that need sign extended arguments. This could be done by a generic wrapper.
1050  */ 
1051
1052 long sys32_lseek (unsigned int fd, int offset, unsigned int whence)
1053 {
1054         return sys_lseek(fd, offset, whence);
1055 }
1056
1057 long sys32_kill(int pid, int sig)
1058 {
1059         return sys_kill(pid, sig);
1060 }
1061  
1062 asmlinkage long sys32_open(const char __user * filename, int flags, int mode)
1063 {
1064         char * tmp;
1065         int fd, error;
1066
1067         /* don't force O_LARGEFILE */
1068         tmp = getname(filename);
1069         fd = PTR_ERR(tmp);
1070         if (!IS_ERR(tmp)) {
1071                 fd = get_unused_fd();
1072                 if (fd >= 0) {
1073                         struct file *f = filp_open(tmp, flags, mode);
1074                         error = PTR_ERR(f);
1075                         if (IS_ERR(f)) {
1076                                 put_unused_fd(fd); 
1077                                 fd = error;
1078                         } else
1079                                 fd_install(fd, f);
1080                 }
1081                 putname(tmp);
1082         }
1083         return fd;
1084 }
1085
1086 struct sigevent32 { 
1087         u32 sigev_value;
1088         u32 sigev_signo; 
1089         u32 sigev_notify; 
1090         u32 payload[(64 / 4) - 3]; 
1091 }; 
1092
1093 extern asmlinkage long
1094 sys_timer_create(clockid_t which_clock,
1095                  struct sigevent __user *timer_event_spec,
1096                  timer_t __user * created_timer_id);
1097
1098 long
1099 sys32_timer_create(u32 clock, struct sigevent32 __user *se32, timer_t __user *timer_id)
1100 {
1101         struct sigevent __user *p = NULL;
1102         if (se32) { 
1103                 struct sigevent se;
1104                 p = compat_alloc_user_space(sizeof(struct sigevent));
1105                 memset(&se, 0, sizeof(struct sigevent)); 
1106                 if (get_user(se.sigev_value.sival_int,  &se32->sigev_value) ||
1107                     __get_user(se.sigev_signo, &se32->sigev_signo) ||
1108                     __get_user(se.sigev_notify, &se32->sigev_notify) ||
1109                     __copy_from_user(&se._sigev_un._pad, &se32->payload, 
1110                                      sizeof(se32->payload)) ||
1111                     copy_to_user(p, &se, sizeof(se)))
1112                         return -EFAULT;
1113         } 
1114         return sys_timer_create(clock, p, timer_id);
1115
1116
1117 long sys32_fadvise64_64(int fd, __u32 offset_low, __u32 offset_high, 
1118                         __u32 len_low, __u32 len_high, int advice)
1119
1120         return sys_fadvise64_64(fd,
1121                                (((u64)offset_high)<<32) | offset_low,
1122                                (((u64)len_high)<<32) | len_low,
1123                                advice); 
1124
1125
1126 long sys32_vm86_warning(void)
1127
1128         struct task_struct *me = current;
1129         static char lastcomm[sizeof(me->comm)];
1130         if (strncmp(lastcomm, me->comm, sizeof(lastcomm))) {
1131                 printk(KERN_INFO "%s: vm86 mode not supported on 64 bit kernel\n",
1132                        me->comm);
1133                 strncpy(lastcomm, me->comm, sizeof(lastcomm));
1134         } 
1135         return -ENOSYS;
1136
1137
1138 long sys32_lookup_dcookie(u32 addr_low, u32 addr_high,
1139                           char __user * buf, size_t len)
1140 {
1141         return sys_lookup_dcookie(((u64)addr_high << 32) | addr_low, buf, len);
1142 }
1143
1144 cond_syscall(sys32_ipc)
1145
1146 static int __init ia32_init (void)
1147 {
1148         printk("IA32 emulation $Id: sys_ia32.c,v 1.32 2002/03/24 13:02:28 ak Exp $\n");  
1149         return 0;
1150 }
1151
1152 __initcall(ia32_init);
1153
1154 extern unsigned long ia32_sys_call_table[];
1155 EXPORT_SYMBOL(ia32_sys_call_table);