linux 2.6.16.38 w/ vs2.0.3-rc1
[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/fsnotify.h>
65 #include <linux/vs_cvirt.h>
66 #include <asm/mman.h>
67 #include <asm/types.h>
68 #include <asm/uaccess.h>
69 #include <asm/semaphore.h>
70 #include <asm/atomic.h>
71 #include <asm/ldt.h>
72
73 #include <net/scm.h>
74 #include <net/sock.h>
75 #include <asm/ia32.h>
76
77 #define AA(__x)         ((unsigned long)(__x))
78
79 int cp_compat_stat(struct kstat *kbuf, struct compat_stat __user *ubuf)
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         if (!access_ok(VERIFY_WRITE, ubuf, sizeof(struct compat_stat)) ||
90             __put_user (old_encode_dev(kbuf->dev), &ubuf->st_dev) ||
91             __put_user (kbuf->ino, &ubuf->st_ino) ||
92             __put_user (kbuf->mode, &ubuf->st_mode) ||
93             __put_user (kbuf->nlink, &ubuf->st_nlink) ||
94             __put_user (uid, &ubuf->st_uid) ||
95             __put_user (gid, &ubuf->st_gid) ||
96             __put_user (old_encode_dev(kbuf->rdev), &ubuf->st_rdev) ||
97             __put_user (kbuf->size, &ubuf->st_size) ||
98             __put_user (kbuf->atime.tv_sec, &ubuf->st_atime) ||
99             __put_user (kbuf->atime.tv_nsec, &ubuf->st_atime_nsec) ||
100             __put_user (kbuf->mtime.tv_sec, &ubuf->st_mtime) ||
101             __put_user (kbuf->mtime.tv_nsec, &ubuf->st_mtime_nsec) ||
102             __put_user (kbuf->ctime.tv_sec, &ubuf->st_ctime) ||
103             __put_user (kbuf->ctime.tv_nsec, &ubuf->st_ctime_nsec) ||
104             __put_user (kbuf->blksize, &ubuf->st_blksize) ||
105             __put_user (kbuf->blocks, &ubuf->st_blocks))
106                 return -EFAULT;
107         return 0;
108 }
109
110 asmlinkage long
111 sys32_truncate64(char __user * filename, unsigned long offset_low, unsigned long offset_high)
112 {
113        return sys_truncate(filename, ((loff_t) offset_high << 32) | offset_low);
114 }
115
116 asmlinkage long
117 sys32_ftruncate64(unsigned int fd, unsigned long offset_low, unsigned long offset_high)
118 {
119        return sys_ftruncate(fd, ((loff_t) offset_high << 32) | offset_low);
120 }
121
122 /* Another set for IA32/LFS -- x86_64 struct stat is different due to 
123    support for 64bit inode numbers. */
124
125 static int
126 cp_stat64(struct stat64 __user *ubuf, struct kstat *stat)
127 {
128         typeof(ubuf->st_uid) uid = 0;
129         typeof(ubuf->st_gid) gid = 0;
130         SET_UID(uid, stat->uid);
131         SET_GID(gid, stat->gid);
132         if (!access_ok(VERIFY_WRITE, ubuf, sizeof(struct stat64)) ||
133             __put_user(huge_encode_dev(stat->dev), &ubuf->st_dev) ||
134             __put_user (stat->ino, &ubuf->__st_ino) ||
135             __put_user (stat->ino, &ubuf->st_ino) ||
136             __put_user (stat->mode, &ubuf->st_mode) ||
137             __put_user (stat->nlink, &ubuf->st_nlink) ||
138             __put_user (uid, &ubuf->st_uid) ||
139             __put_user (gid, &ubuf->st_gid) ||
140             __put_user (huge_encode_dev(stat->rdev), &ubuf->st_rdev) ||
141             __put_user (stat->size, &ubuf->st_size) ||
142             __put_user (stat->atime.tv_sec, &ubuf->st_atime) ||
143             __put_user (stat->atime.tv_nsec, &ubuf->st_atime_nsec) ||
144             __put_user (stat->mtime.tv_sec, &ubuf->st_mtime) ||
145             __put_user (stat->mtime.tv_nsec, &ubuf->st_mtime_nsec) ||
146             __put_user (stat->ctime.tv_sec, &ubuf->st_ctime) ||
147             __put_user (stat->ctime.tv_nsec, &ubuf->st_ctime_nsec) ||
148             __put_user (stat->blksize, &ubuf->st_blksize) ||
149             __put_user (stat->blocks, &ubuf->st_blocks))
150                 return -EFAULT;
151         return 0;
152 }
153
154 asmlinkage long
155 sys32_stat64(char __user * filename, struct stat64 __user *statbuf)
156 {
157         struct kstat stat;
158         int ret = vfs_stat(filename, &stat);
159         if (!ret)
160                 ret = cp_stat64(statbuf, &stat);
161         return ret;
162 }
163
164 asmlinkage long
165 sys32_lstat64(char __user * filename, struct stat64 __user *statbuf)
166 {
167         struct kstat stat;
168         int ret = vfs_lstat(filename, &stat);
169         if (!ret)
170                 ret = cp_stat64(statbuf, &stat);
171         return ret;
172 }
173
174 asmlinkage long
175 sys32_fstat64(unsigned int fd, struct stat64 __user *statbuf)
176 {
177         struct kstat stat;
178         int ret = vfs_fstat(fd, &stat);
179         if (!ret)
180                 ret = cp_stat64(statbuf, &stat);
181         return ret;
182 }
183
184 asmlinkage long
185 sys32_fstatat(unsigned int dfd, char __user *filename,
186               struct stat64 __user* statbuf, int flag)
187 {
188         struct kstat stat;
189         int error = -EINVAL;
190
191         if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
192                 goto out;
193
194         if (flag & AT_SYMLINK_NOFOLLOW)
195                 error = vfs_lstat_fd(dfd, filename, &stat);
196         else
197                 error = vfs_stat_fd(dfd, filename, &stat);
198
199         if (!error)
200                 error = cp_stat64(statbuf, &stat);
201
202 out:
203         return error;
204 }
205
206 /*
207  * Linux/i386 didn't use to be able to handle more than
208  * 4 system call parameters, so these system calls used a memory
209  * block for parameter passing..
210  */
211
212 struct mmap_arg_struct {
213         unsigned int addr;
214         unsigned int len;
215         unsigned int prot;
216         unsigned int flags;
217         unsigned int fd;
218         unsigned int offset;
219 };
220
221 asmlinkage long
222 sys32_mmap(struct mmap_arg_struct __user *arg)
223 {
224         struct mmap_arg_struct a;
225         struct file *file = NULL;
226         unsigned long retval;
227         struct mm_struct *mm ;
228
229         if (copy_from_user(&a, arg, sizeof(a)))
230                 return -EFAULT;
231
232         if (a.offset & ~PAGE_MASK)
233                 return -EINVAL; 
234
235         if (!(a.flags & MAP_ANONYMOUS)) {
236                 file = fget(a.fd);
237                 if (!file)
238                         return -EBADF;
239         }
240         
241         mm = current->mm; 
242         down_write(&mm->mmap_sem); 
243         retval = do_mmap_pgoff(file, a.addr, a.len, a.prot, a.flags, a.offset>>PAGE_SHIFT);
244         if (file)
245                 fput(file);
246
247         up_write(&mm->mmap_sem); 
248
249         return retval;
250 }
251
252 asmlinkage long 
253 sys32_mprotect(unsigned long start, size_t len, unsigned long prot)
254 {
255         return sys_mprotect(start,len,prot); 
256 }
257
258 asmlinkage long
259 sys32_pipe(int __user *fd)
260 {
261         int retval;
262         int fds[2];
263
264         retval = do_pipe(fds);
265         if (retval)
266                 goto out;
267         if (copy_to_user(fd, fds, sizeof(fds)))
268                 retval = -EFAULT;
269   out:
270         return retval;
271 }
272
273 asmlinkage long
274 sys32_rt_sigaction(int sig, struct sigaction32 __user *act,
275                    struct sigaction32 __user *oact,  unsigned int sigsetsize)
276 {
277         struct k_sigaction new_ka, old_ka;
278         int ret;
279         compat_sigset_t set32;
280
281         /* XXX: Don't preclude handling different sized sigset_t's.  */
282         if (sigsetsize != sizeof(compat_sigset_t))
283                 return -EINVAL;
284
285         if (act) {
286                 compat_uptr_t handler, restorer;
287
288                 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
289                     __get_user(handler, &act->sa_handler) ||
290                     __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
291                     __get_user(restorer, &act->sa_restorer)||
292                     __copy_from_user(&set32, &act->sa_mask, sizeof(compat_sigset_t)))
293                         return -EFAULT;
294                 new_ka.sa.sa_handler = compat_ptr(handler);
295                 new_ka.sa.sa_restorer = compat_ptr(restorer);
296                 /* FIXME: here we rely on _COMPAT_NSIG_WORS to be >= than _NSIG_WORDS << 1 */
297                 switch (_NSIG_WORDS) {
298                 case 4: new_ka.sa.sa_mask.sig[3] = set32.sig[6]
299                                 | (((long)set32.sig[7]) << 32);
300                 case 3: new_ka.sa.sa_mask.sig[2] = set32.sig[4]
301                                 | (((long)set32.sig[5]) << 32);
302                 case 2: new_ka.sa.sa_mask.sig[1] = set32.sig[2]
303                                 | (((long)set32.sig[3]) << 32);
304                 case 1: new_ka.sa.sa_mask.sig[0] = set32.sig[0]
305                                 | (((long)set32.sig[1]) << 32);
306                 }
307         }
308
309         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
310
311         if (!ret && oact) {
312                 /* FIXME: here we rely on _COMPAT_NSIG_WORS to be >= than _NSIG_WORDS << 1 */
313                 switch (_NSIG_WORDS) {
314                 case 4:
315                         set32.sig[7] = (old_ka.sa.sa_mask.sig[3] >> 32);
316                         set32.sig[6] = old_ka.sa.sa_mask.sig[3];
317                 case 3:
318                         set32.sig[5] = (old_ka.sa.sa_mask.sig[2] >> 32);
319                         set32.sig[4] = old_ka.sa.sa_mask.sig[2];
320                 case 2:
321                         set32.sig[3] = (old_ka.sa.sa_mask.sig[1] >> 32);
322                         set32.sig[2] = old_ka.sa.sa_mask.sig[1];
323                 case 1:
324                         set32.sig[1] = (old_ka.sa.sa_mask.sig[0] >> 32);
325                         set32.sig[0] = old_ka.sa.sa_mask.sig[0];
326                 }
327                 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
328                     __put_user(ptr_to_compat(old_ka.sa.sa_handler), &oact->sa_handler) ||
329                     __put_user(ptr_to_compat(old_ka.sa.sa_restorer), &oact->sa_restorer) ||
330                     __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
331                     __copy_to_user(&oact->sa_mask, &set32, sizeof(compat_sigset_t)))
332                         return -EFAULT;
333         }
334
335         return ret;
336 }
337
338 asmlinkage long
339 sys32_sigaction (int sig, struct old_sigaction32 __user *act, struct old_sigaction32 __user *oact)
340 {
341         struct k_sigaction new_ka, old_ka;
342         int ret;
343
344         if (act) {
345                 compat_old_sigset_t mask;
346                 compat_uptr_t handler, restorer;
347
348                 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
349                     __get_user(handler, &act->sa_handler) ||
350                     __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
351                     __get_user(restorer, &act->sa_restorer) ||
352                     __get_user(mask, &act->sa_mask))
353                         return -EFAULT;
354
355                 new_ka.sa.sa_handler = compat_ptr(handler);
356                 new_ka.sa.sa_restorer = compat_ptr(restorer);
357
358                 siginitset(&new_ka.sa.sa_mask, mask);
359         }
360
361         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
362
363         if (!ret && oact) {
364                 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
365                     __put_user(ptr_to_compat(old_ka.sa.sa_handler), &oact->sa_handler) ||
366                     __put_user(ptr_to_compat(old_ka.sa.sa_restorer), &oact->sa_restorer) ||
367                     __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
368                     __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
369                         return -EFAULT;
370         }
371
372         return ret;
373 }
374
375 asmlinkage long
376 sys32_rt_sigprocmask(int how, compat_sigset_t __user *set,
377                         compat_sigset_t __user *oset, unsigned int sigsetsize)
378 {
379         sigset_t s;
380         compat_sigset_t s32;
381         int ret;
382         mm_segment_t old_fs = get_fs();
383         
384         if (set) {
385                 if (copy_from_user (&s32, set, sizeof(compat_sigset_t)))
386                         return -EFAULT;
387                 switch (_NSIG_WORDS) {
388                 case 4: s.sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
389                 case 3: s.sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
390                 case 2: s.sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
391                 case 1: s.sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
392                 }
393         }
394         set_fs (KERNEL_DS);
395         ret = sys_rt_sigprocmask(how, set ? &s : NULL, oset ? &s : NULL,
396                                  sigsetsize); 
397         set_fs (old_fs);
398         if (ret) return ret;
399         if (oset) {
400                 switch (_NSIG_WORDS) {
401                 case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
402                 case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
403                 case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
404                 case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
405                 }
406                 if (copy_to_user (oset, &s32, sizeof(compat_sigset_t)))
407                         return -EFAULT;
408         }
409         return 0;
410 }
411
412 static inline long
413 get_tv32(struct timeval *o, struct compat_timeval __user *i)
414 {
415         int err = -EFAULT; 
416         if (access_ok(VERIFY_READ, i, sizeof(*i))) { 
417                 err = __get_user(o->tv_sec, &i->tv_sec);
418                 err |= __get_user(o->tv_usec, &i->tv_usec);
419         }
420         return err; 
421 }
422
423 static inline long
424 put_tv32(struct compat_timeval __user *o, struct timeval *i)
425 {
426         int err = -EFAULT;
427         if (access_ok(VERIFY_WRITE, o, sizeof(*o))) { 
428                 err = __put_user(i->tv_sec, &o->tv_sec);
429                 err |= __put_user(i->tv_usec, &o->tv_usec);
430         } 
431         return err; 
432 }
433
434 extern int do_setitimer(int which, struct itimerval *, struct itimerval *);
435
436 asmlinkage long
437 sys32_alarm(unsigned int seconds)
438 {
439         struct itimerval it_new, it_old;
440         unsigned int oldalarm;
441
442         it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
443         it_new.it_value.tv_sec = seconds;
444         it_new.it_value.tv_usec = 0;
445         do_setitimer(ITIMER_REAL, &it_new, &it_old);
446         oldalarm = it_old.it_value.tv_sec;
447         /* ehhh.. We can't return 0 if we have an alarm pending.. */
448         /* And we'd better return too much than too little anyway */
449         if (it_old.it_value.tv_usec)
450                 oldalarm++;
451         return oldalarm;
452 }
453
454 /* Translations due to time_t size differences.  Which affects all
455    sorts of things, like timeval and itimerval.  */
456
457 extern struct timezone sys_tz;
458
459 asmlinkage long
460 sys32_gettimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
461 {
462         if (tv) {
463                 struct timeval ktv;
464                 do_gettimeofday(&ktv);
465                 if (put_tv32(tv, &ktv))
466                         return -EFAULT;
467         }
468         if (tz) {
469                 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
470                         return -EFAULT;
471         }
472         return 0;
473 }
474
475 asmlinkage long
476 sys32_settimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
477 {
478         struct timeval ktv;
479         struct timespec kts;
480         struct timezone ktz;
481
482         if (tv) {
483                 if (get_tv32(&ktv, tv))
484                         return -EFAULT;
485                 kts.tv_sec = ktv.tv_sec;
486                 kts.tv_nsec = ktv.tv_usec * NSEC_PER_USEC;
487         }
488         if (tz) {
489                 if (copy_from_user(&ktz, tz, sizeof(ktz)))
490                         return -EFAULT;
491         }
492
493         return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
494 }
495
496 struct sel_arg_struct {
497         unsigned int n;
498         unsigned int inp;
499         unsigned int outp;
500         unsigned int exp;
501         unsigned int tvp;
502 };
503
504 asmlinkage long
505 sys32_old_select(struct sel_arg_struct __user *arg)
506 {
507         struct sel_arg_struct a;
508
509         if (copy_from_user(&a, arg, sizeof(a)))
510                 return -EFAULT;
511         return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
512                                  compat_ptr(a.exp), compat_ptr(a.tvp));
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 (!access_ok(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 asmlinkage long
652 sys32_rt_sigqueueinfo(int pid, int sig, compat_siginfo_t __user *uinfo)
653 {
654         siginfo_t info;
655         int ret;
656         mm_segment_t old_fs = get_fs();
657         
658         if (copy_siginfo_from_user32(&info, uinfo))
659                 return -EFAULT;
660         set_fs (KERNEL_DS);
661         ret = sys_rt_sigqueueinfo(pid, sig, &info);
662         set_fs (old_fs);
663         return ret;
664 }
665
666 /* These are here just in case some old ia32 binary calls it. */
667 asmlinkage long
668 sys32_pause(void)
669 {
670         current->state = TASK_INTERRUPTIBLE;
671         schedule();
672         return -ERESTARTNOHAND;
673 }
674
675
676 #ifdef CONFIG_SYSCTL
677 struct sysctl_ia32 {
678         unsigned int    name;
679         int             nlen;
680         unsigned int    oldval;
681         unsigned int    oldlenp;
682         unsigned int    newval;
683         unsigned int    newlen;
684         unsigned int    __unused[4];
685 };
686
687
688 asmlinkage long
689 sys32_sysctl(struct sysctl_ia32 __user *args32)
690 {
691         struct sysctl_ia32 a32;
692         mm_segment_t old_fs = get_fs ();
693         void __user *oldvalp, *newvalp;
694         size_t oldlen;
695         int __user *namep;
696         long ret;
697         extern int do_sysctl(int *name, int nlen, void *oldval, size_t *oldlenp,
698                      void *newval, size_t newlen);
699
700
701         if (copy_from_user(&a32, args32, sizeof (a32)))
702                 return -EFAULT;
703
704         /*
705          * We need to pre-validate these because we have to disable address checking
706          * before calling do_sysctl() because of OLDLEN but we can't run the risk of the
707          * user specifying bad addresses here.  Well, since we're dealing with 32 bit
708          * addresses, we KNOW that access_ok() will always succeed, so this is an
709          * expensive NOP, but so what...
710          */
711         namep = compat_ptr(a32.name);
712         oldvalp = compat_ptr(a32.oldval);
713         newvalp =  compat_ptr(a32.newval);
714
715         if ((oldvalp && get_user(oldlen, (int __user *)compat_ptr(a32.oldlenp)))
716             || !access_ok(VERIFY_WRITE, namep, 0)
717             || !access_ok(VERIFY_WRITE, oldvalp, 0)
718             || !access_ok(VERIFY_WRITE, newvalp, 0))
719                 return -EFAULT;
720
721         set_fs(KERNEL_DS);
722         lock_kernel();
723         ret = do_sysctl(namep, a32.nlen, oldvalp, &oldlen, newvalp, (size_t) a32.newlen);
724         unlock_kernel();
725         set_fs(old_fs);
726
727         if (oldvalp && put_user (oldlen, (int __user *)compat_ptr(a32.oldlenp)))
728                 return -EFAULT;
729
730         return ret;
731 }
732 #endif
733
734 /* warning: next two assume little endian */ 
735 asmlinkage long
736 sys32_pread(unsigned int fd, char __user *ubuf, u32 count, u32 poslo, u32 poshi)
737 {
738         return sys_pread64(fd, ubuf, count,
739                          ((loff_t)AA(poshi) << 32) | AA(poslo));
740 }
741
742 asmlinkage long
743 sys32_pwrite(unsigned int fd, char __user *ubuf, u32 count, u32 poslo, u32 poshi)
744 {
745         return sys_pwrite64(fd, ubuf, count,
746                           ((loff_t)AA(poshi) << 32) | AA(poslo));
747 }
748
749
750 asmlinkage long
751 sys32_personality(unsigned long personality)
752 {
753         int ret;
754         if (personality(current->personality) == PER_LINUX32 && 
755                 personality == PER_LINUX)
756                 personality = PER_LINUX32;
757         ret = sys_personality(personality);
758         if (ret == PER_LINUX32)
759                 ret = PER_LINUX;
760         return ret;
761 }
762
763 asmlinkage long
764 sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset, s32 count)
765 {
766         mm_segment_t old_fs = get_fs();
767         int ret;
768         off_t of;
769         
770         if (offset && get_user(of, offset))
771                 return -EFAULT;
772                 
773         set_fs(KERNEL_DS);
774         ret = sys_sendfile(out_fd, in_fd, offset ? &of : NULL, count);
775         set_fs(old_fs);
776         
777         if (offset && put_user(of, offset))
778                 return -EFAULT;
779                 
780         return ret;
781 }
782
783 /* Handle adjtimex compatibility. */
784
785 struct timex32 {
786         u32 modes;
787         s32 offset, freq, maxerror, esterror;
788         s32 status, constant, precision, tolerance;
789         struct compat_timeval time;
790         s32 tick;
791         s32 ppsfreq, jitter, shift, stabil;
792         s32 jitcnt, calcnt, errcnt, stbcnt;
793         s32  :32; s32  :32; s32  :32; s32  :32;
794         s32  :32; s32  :32; s32  :32; s32  :32;
795         s32  :32; s32  :32; s32  :32; s32  :32;
796 };
797
798 extern int do_adjtimex(struct timex *);
799
800 asmlinkage long
801 sys32_adjtimex(struct timex32 __user *utp)
802 {
803         struct timex txc;
804         int ret;
805
806         memset(&txc, 0, sizeof(struct timex));
807
808         if (!access_ok(VERIFY_READ, utp, sizeof(struct timex32)) ||
809            __get_user(txc.modes, &utp->modes) ||
810            __get_user(txc.offset, &utp->offset) ||
811            __get_user(txc.freq, &utp->freq) ||
812            __get_user(txc.maxerror, &utp->maxerror) ||
813            __get_user(txc.esterror, &utp->esterror) ||
814            __get_user(txc.status, &utp->status) ||
815            __get_user(txc.constant, &utp->constant) ||
816            __get_user(txc.precision, &utp->precision) ||
817            __get_user(txc.tolerance, &utp->tolerance) ||
818            __get_user(txc.time.tv_sec, &utp->time.tv_sec) ||
819            __get_user(txc.time.tv_usec, &utp->time.tv_usec) ||
820            __get_user(txc.tick, &utp->tick) ||
821            __get_user(txc.ppsfreq, &utp->ppsfreq) ||
822            __get_user(txc.jitter, &utp->jitter) ||
823            __get_user(txc.shift, &utp->shift) ||
824            __get_user(txc.stabil, &utp->stabil) ||
825            __get_user(txc.jitcnt, &utp->jitcnt) ||
826            __get_user(txc.calcnt, &utp->calcnt) ||
827            __get_user(txc.errcnt, &utp->errcnt) ||
828            __get_user(txc.stbcnt, &utp->stbcnt))
829                 return -EFAULT;
830
831         ret = do_adjtimex(&txc);
832
833         if (!access_ok(VERIFY_WRITE, utp, sizeof(struct timex32)) ||
834            __put_user(txc.modes, &utp->modes) ||
835            __put_user(txc.offset, &utp->offset) ||
836            __put_user(txc.freq, &utp->freq) ||
837            __put_user(txc.maxerror, &utp->maxerror) ||
838            __put_user(txc.esterror, &utp->esterror) ||
839            __put_user(txc.status, &utp->status) ||
840            __put_user(txc.constant, &utp->constant) ||
841            __put_user(txc.precision, &utp->precision) ||
842            __put_user(txc.tolerance, &utp->tolerance) ||
843            __put_user(txc.time.tv_sec, &utp->time.tv_sec) ||
844            __put_user(txc.time.tv_usec, &utp->time.tv_usec) ||
845            __put_user(txc.tick, &utp->tick) ||
846            __put_user(txc.ppsfreq, &utp->ppsfreq) ||
847            __put_user(txc.jitter, &utp->jitter) ||
848            __put_user(txc.shift, &utp->shift) ||
849            __put_user(txc.stabil, &utp->stabil) ||
850            __put_user(txc.jitcnt, &utp->jitcnt) ||
851            __put_user(txc.calcnt, &utp->calcnt) ||
852            __put_user(txc.errcnt, &utp->errcnt) ||
853            __put_user(txc.stbcnt, &utp->stbcnt))
854                 ret = -EFAULT;
855
856         return ret;
857 }
858
859 asmlinkage long sys32_mmap2(unsigned long addr, unsigned long len,
860         unsigned long prot, unsigned long flags,
861         unsigned long fd, unsigned long pgoff)
862 {
863         struct mm_struct *mm = current->mm;
864         unsigned long error;
865         struct file * file = NULL;
866
867         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
868         if (!(flags & MAP_ANONYMOUS)) {
869                 file = fget(fd);
870                 if (!file)
871                         return -EBADF;
872         }
873
874         down_write(&mm->mmap_sem);
875         error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
876         up_write(&mm->mmap_sem);
877
878         if (file)
879                 fput(file);
880         return error;
881 }
882
883 asmlinkage long sys32_olduname(struct oldold_utsname __user * name)
884 {
885         int error;
886         struct new_utsname *ptr;
887
888         if (!name)
889                 return -EFAULT;
890         if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
891                 return -EFAULT;
892   
893         down_read(&uts_sem);
894         
895         ptr = vx_new_utsname();
896         error = __copy_to_user(&name->sysname,ptr->sysname,__OLD_UTS_LEN);
897          __put_user(0,name->sysname+__OLD_UTS_LEN);
898          __copy_to_user(&name->nodename,ptr->nodename,__OLD_UTS_LEN);
899          __put_user(0,name->nodename+__OLD_UTS_LEN);
900          __copy_to_user(&name->release,ptr->release,__OLD_UTS_LEN);
901          __put_user(0,name->release+__OLD_UTS_LEN);
902          __copy_to_user(&name->version,ptr->version,__OLD_UTS_LEN);
903          __put_user(0,name->version+__OLD_UTS_LEN);
904          { 
905                  char *arch = "x86_64";
906                  if (personality(current->personality) == PER_LINUX32)
907                          arch = "i686";
908                  
909                  __copy_to_user(&name->machine,arch,strlen(arch)+1);
910          }
911         
912          up_read(&uts_sem);
913          
914          error = error ? -EFAULT : 0;
915          
916          return error;
917 }
918
919 long sys32_uname(struct old_utsname __user * name)
920 {
921         int err;
922         if (!name)
923                 return -EFAULT;
924         down_read(&uts_sem);
925         err=copy_to_user(name, vx_new_utsname(), sizeof (*name));
926         up_read(&uts_sem);
927         if (personality(current->personality) == PER_LINUX32) 
928                 err |= copy_to_user(&name->machine, "i686", 5);
929         return err?-EFAULT:0;
930 }
931
932 long sys32_ustat(unsigned dev, struct ustat32 __user *u32p)
933 {
934         struct ustat u;
935         mm_segment_t seg;
936         int ret;
937         
938         seg = get_fs(); 
939         set_fs(KERNEL_DS); 
940         ret = sys_ustat(dev,&u); 
941         set_fs(seg);
942         if (ret >= 0) { 
943                 if (!access_ok(VERIFY_WRITE,u32p,sizeof(struct ustat32)) || 
944                     __put_user((__u32) u.f_tfree, &u32p->f_tfree) ||
945                     __put_user((__u32) u.f_tinode, &u32p->f_tfree) ||
946                     __copy_to_user(&u32p->f_fname, u.f_fname, sizeof(u.f_fname)) ||
947                     __copy_to_user(&u32p->f_fpack, u.f_fpack, sizeof(u.f_fpack)))
948                         ret = -EFAULT;
949         }
950         return ret;
951
952
953 asmlinkage long sys32_execve(char __user *name, compat_uptr_t __user *argv,
954                              compat_uptr_t __user *envp, struct pt_regs *regs)
955 {
956         long error;
957         char * filename;
958
959         filename = getname(name);
960         error = PTR_ERR(filename);
961         if (IS_ERR(filename))
962                 return error;
963         error = compat_do_execve(filename, argv, envp, regs);
964         if (error == 0) {
965                 task_lock(current);
966                 current->ptrace &= ~PT_DTRACE;
967                 task_unlock(current);
968         }
969         putname(filename);
970         return error;
971 }
972
973 asmlinkage long sys32_clone(unsigned int clone_flags, unsigned int newsp,
974                             struct pt_regs *regs)
975 {
976         void __user *parent_tid = (void __user *)regs->rdx;
977         void __user *child_tid = (void __user *)regs->rdi;
978         if (!newsp)
979                 newsp = regs->rsp;
980         return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
981 }
982
983 /*
984  * Some system calls that need sign extended arguments. This could be done by a generic wrapper.
985  */ 
986
987 long sys32_lseek (unsigned int fd, int offset, unsigned int whence)
988 {
989         return sys_lseek(fd, offset, whence);
990 }
991
992 long sys32_kill(int pid, int sig)
993 {
994         return sys_kill(pid, sig);
995 }
996  
997 long sys32_fadvise64_64(int fd, __u32 offset_low, __u32 offset_high, 
998                         __u32 len_low, __u32 len_high, int advice)
999
1000         return sys_fadvise64_64(fd,
1001                                (((u64)offset_high)<<32) | offset_low,
1002                                (((u64)len_high)<<32) | len_low,
1003                                advice); 
1004
1005
1006 long sys32_vm86_warning(void)
1007
1008         struct task_struct *me = current;
1009         static char lastcomm[sizeof(me->comm)];
1010         if (strncmp(lastcomm, me->comm, sizeof(lastcomm))) {
1011                 printk(KERN_INFO "%s: vm86 mode not supported on 64 bit kernel\n",
1012                        me->comm);
1013                 strncpy(lastcomm, me->comm, sizeof(lastcomm));
1014         } 
1015         return -ENOSYS;
1016
1017
1018 long sys32_lookup_dcookie(u32 addr_low, u32 addr_high,
1019                           char __user * buf, size_t len)
1020 {
1021         return sys_lookup_dcookie(((u64)addr_high << 32) | addr_low, buf, len);
1022 }
1023
1024 static int __init ia32_init (void)
1025 {
1026         printk("IA32 emulation $Id: sys_ia32.c,v 1.32 2002/03/24 13:02:28 ak Exp $\n");  
1027         return 0;
1028 }
1029
1030 __initcall(ia32_init);
1031
1032 extern unsigned long ia32_sys_call_table[];
1033 EXPORT_SYMBOL(ia32_sys_call_table);