7159953b2c440e8a31085ce1f8c65ab4750b9641
[linux-2.6.git] / arch / parisc / kernel / sys_parisc32.c
1 /*
2  * sys_parisc32.c: Conversion between 32bit and 64bit native syscalls.
3  *
4  * Copyright (C) 2000-2001 Hewlett Packard Company
5  * Copyright (C) 2000 John Marvin
6  * Copyright (C) 2001 Matthew Wilcox
7  *
8  * These routines maintain argument size conversion between 32bit and 64bit
9  * environment. Based heavily on sys_ia32.c and sys_sparc32.c.
10  */
11
12 #include <linux/config.h>
13 #include <linux/compat.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/fs.h> 
17 #include <linux/mm.h> 
18 #include <linux/file.h> 
19 #include <linux/signal.h>
20 #include <linux/resource.h>
21 #include <linux/times.h>
22 #include <linux/utsname.h>
23 #include <linux/time.h>
24 #include <linux/timex.h>
25 #include <linux/smp.h>
26 #include <linux/smp_lock.h>
27 #include <linux/sem.h>
28 #include <linux/msg.h>
29 #include <linux/shm.h>
30 #include <linux/slab.h>
31 #include <linux/uio.h>
32 #include <linux/nfs_fs.h>
33 #include <linux/ncp_fs.h>
34 #include <linux/sunrpc/svc.h>
35 #include <linux/nfsd/nfsd.h>
36 #include <linux/nfsd/cache.h>
37 #include <linux/nfsd/xdr.h>
38 #include <linux/nfsd/syscall.h>
39 #include <linux/poll.h>
40 #include <linux/personality.h>
41 #include <linux/stat.h>
42 #include <linux/highmem.h>
43 #include <linux/highuid.h>
44 #include <linux/mman.h>
45 #include <linux/binfmts.h>
46 #include <linux/namei.h>
47 #include <linux/vfs.h>
48 #include <linux/ptrace.h>
49 #include <linux/swap.h>
50 #include <linux/syscalls.h>
51
52 #include <asm/types.h>
53 #include <asm/uaccess.h>
54 #include <asm/semaphore.h>
55 #include <asm/mmu_context.h>
56
57 #include "sys32.h"
58
59 #undef DEBUG
60
61 #ifdef DEBUG
62 #define DBG(x)  printk x
63 #else
64 #define DBG(x)
65 #endif
66
67 /*
68  * count32() counts the number of arguments/envelopes. It is basically
69  *           a copy of count() from fs/exec.c, except that it works
70  *           with 32 bit argv and envp pointers.
71  */
72
73 static int count32(u32 *argv, int max)
74 {
75         int i = 0;
76
77         if (argv != NULL) {
78                 for (;;) {
79                         u32 p;
80                         int error;
81
82                         error = get_user(p,argv);
83                         if (error)
84                                 return error;
85                         if (!p)
86                                 break;
87                         argv++;
88                         if(++i > max)
89                                 return -E2BIG;
90                 }
91         }
92         return i;
93 }
94
95
96 /*
97  * copy_strings32() is basically a copy of copy_strings() from fs/exec.c
98  *                  except that it works with 32 bit argv and envp pointers.
99  */
100
101
102 static int copy_strings32(int argc, u32 *argv, struct linux_binprm *bprm)
103 {
104         while (argc-- > 0) {
105                 u32 str;
106                 int len;
107                 unsigned long pos;
108
109                 if (get_user(str, argv + argc) ||
110                     !str ||
111                     !(len = strnlen_user((char *)compat_ptr(str), bprm->p)))
112                         return -EFAULT;
113
114                 if (bprm->p < len) 
115                         return -E2BIG; 
116
117                 bprm->p -= len;
118
119                 pos = bprm->p;
120                 while (len > 0) {
121                         char *kaddr;
122                         int i, new, err;
123                         struct page *page;
124                         int offset, bytes_to_copy;
125
126                         offset = pos % PAGE_SIZE;
127                         i = pos/PAGE_SIZE;
128                         page = bprm->page[i];
129                         new = 0;
130                         if (!page) {
131                                 page = alloc_page(GFP_HIGHUSER);
132                                 bprm->page[i] = page;
133                                 if (!page)
134                                         return -ENOMEM;
135                                 new = 1;
136                         }
137                         kaddr = (char *)kmap(page);
138
139                         if (new && offset)
140                                 memset(kaddr, 0, offset);
141                         bytes_to_copy = PAGE_SIZE - offset;
142                         if (bytes_to_copy > len) {
143                                 bytes_to_copy = len;
144                                 if (new)
145                                         memset(kaddr+offset+len, 0, PAGE_SIZE-offset-len);
146                         }
147                         err = copy_from_user(kaddr + offset, (char *)compat_ptr(str), bytes_to_copy);
148                         flush_dcache_page(page);
149                         kunmap(page);
150
151                         if (err)
152                                 return -EFAULT; 
153
154                         pos += bytes_to_copy;
155                         str += bytes_to_copy;
156                         len -= bytes_to_copy;
157                 }
158         }
159         return 0;
160 }
161
162 /*
163  * do_execve32() is mostly a copy of do_execve(), with the exception
164  * that it processes 32 bit argv and envp pointers.
165  */
166
167 static inline int 
168 do_execve32(char * filename, u32 * argv, u32 * envp, struct pt_regs * regs)
169 {
170         struct linux_binprm bprm;
171         struct file *file;
172         int retval;
173         int i;
174
175         file = open_exec(filename);
176
177         retval = PTR_ERR(file);
178         if (IS_ERR(file))
179                 return retval;
180
181         bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
182         memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
183
184         DBG(("do_execve32(%s, %p, %p, %p)\n", filename, argv, envp, regs));
185
186         bprm.file = file;
187         bprm.filename = filename;
188         bprm.interp = filename;
189         bprm.sh_bang = 0;
190         bprm.loader = 0;
191         bprm.exec = 0;
192
193         bprm.mm = mm_alloc();
194         retval = -ENOMEM;
195         if (!bprm.mm)
196                 goto out_file;
197
198         retval = init_new_context(current, bprm.mm);
199         if (retval < 0)
200                 goto out_mm;
201
202         if ((bprm.argc = count32(argv, bprm.p / sizeof(u32))) < 0) 
203                 goto out_mm;
204
205         if ((bprm.envc = count32(envp, bprm.p / sizeof(u32))) < 0) 
206                 goto out_mm;
207
208         retval = prepare_binprm(&bprm);
209         if (retval < 0)
210                 goto out;
211         
212         retval = copy_strings_kernel(1, &bprm.filename, &bprm);
213         if (retval < 0)
214                 goto out;
215
216         bprm.exec = bprm.p;
217         retval = copy_strings32(bprm.envc, envp, &bprm);
218         if (retval < 0)
219                 goto out;
220
221         retval = copy_strings32(bprm.argc, argv, &bprm);
222         if (retval < 0)
223                 goto out;
224
225         retval = search_binary_handler(&bprm,regs);
226         if (retval >= 0)
227                 /* execve success */
228                 return retval;
229
230 out:
231         /* Something went wrong, return the inode and free the argument pages*/
232         for (i = 0; i < MAX_ARG_PAGES; i++) {
233                 struct page *page = bprm.page[i];
234                 if (page)
235                         __free_page(page);
236         }
237
238 out_mm:
239         mmdrop(bprm.mm);
240
241 out_file:
242         if (bprm.file) {
243                 allow_write_access(bprm.file);
244                 fput(bprm.file);
245         }
246
247         return retval;
248 }
249
250 /*
251  * sys32_execve() executes a new program.
252  */
253
254 asmlinkage int sys32_execve(struct pt_regs *regs)
255 {
256         int error;
257         char *filename;
258
259         DBG(("sys32_execve(%p) r26 = 0x%lx\n", regs, regs->gr[26]));
260         filename = getname((char *) regs->gr[26]);
261         error = PTR_ERR(filename);
262         if (IS_ERR(filename))
263                 goto out;
264         error = do_execve32(filename, (u32 *) regs->gr[25],
265                 (u32 *) regs->gr[24], regs);
266         if (error == 0)
267                 current->ptrace &= ~PT_DTRACE;
268         putname(filename);
269 out:
270
271         return error;
272 }
273
274 asmlinkage long sys32_unimplemented(int r26, int r25, int r24, int r23,
275         int r22, int r21, int r20)
276 {
277     printk(KERN_ERR "%s(%d): Unimplemented 32 on 64 syscall #%d!\n", 
278         current->comm, current->pid, r20);
279     return -ENOSYS;
280 }
281
282 #ifdef CONFIG_SYSCTL
283
284 struct __sysctl_args32 {
285         u32 name;
286         int nlen;
287         u32 oldval;
288         u32 oldlenp;
289         u32 newval;
290         u32 newlen;
291         u32 __unused[4];
292 };
293
294 asmlinkage long sys32_sysctl(struct __sysctl_args32 *args)
295 {
296         struct __sysctl_args32 tmp;
297         int error;
298         unsigned int oldlen32;
299         size_t oldlen, *oldlenp = NULL;
300         unsigned long addr = (((long)&args->__unused[0]) + 7) & ~7;
301         extern int do_sysctl(int *name, int nlen, void *oldval, size_t *oldlenp,
302                void *newval, size_t newlen);
303
304         DBG(("sysctl32(%p)\n", args));
305
306         if (copy_from_user(&tmp, args, sizeof(tmp)))
307                 return -EFAULT;
308
309         if (tmp.oldval && tmp.oldlenp) {
310                 /* Duh, this is ugly and might not work if sysctl_args
311                    is in read-only memory, but do_sysctl does indirectly
312                    a lot of uaccess in both directions and we'd have to
313                    basically copy the whole sysctl.c here, and
314                    glibc's __sysctl uses rw memory for the structure
315                    anyway.  */
316                 /* a possibly better hack than this, which will avoid the
317                  * problem if the struct is read only, is to push the
318                  * 'oldlen' value out to the user's stack instead. -PB
319                  */
320                 if (get_user(oldlen32, (u32 *)(u64)tmp.oldlenp))
321                         return -EFAULT;
322                 oldlen = oldlen32;
323                 if (put_user(oldlen, (size_t *)addr))
324                         return -EFAULT;
325                 oldlenp = (size_t *)addr;
326         }
327
328         lock_kernel();
329         error = do_sysctl((int *)(u64)tmp.name, tmp.nlen, (void *)(u64)tmp.oldval,
330                           oldlenp, (void *)(u64)tmp.newval, tmp.newlen);
331         unlock_kernel();
332         if (oldlenp) {
333                 if (!error) {
334                         if (get_user(oldlen, (size_t *)addr)) {
335                                 error = -EFAULT;
336                         } else {
337                                 oldlen32 = oldlen;
338                                 if (put_user(oldlen32, (u32 *)(u64)tmp.oldlenp))
339                                         error = -EFAULT;
340                         }
341                 }
342                 if (copy_to_user(args->__unused, tmp.__unused, sizeof(tmp.__unused)))
343                         error = -EFAULT;
344         }
345         return error;
346 }
347
348 #else /* CONFIG_SYSCTL */
349
350 asmlinkage long sys32_sysctl(struct __sysctl_args *args)
351 {
352         return -ENOSYS;
353 }
354 #endif /* CONFIG_SYSCTL */
355
356 asmlinkage long sys32_sched_rr_get_interval(pid_t pid,
357         struct compat_timespec *interval)
358 {
359         struct timespec t;
360         int ret;
361         
362         KERNEL_SYSCALL(ret, sys_sched_rr_get_interval, pid, &t);
363         if (put_compat_timespec(&t, interval))
364                 return -EFAULT;
365         return ret;
366 }
367
368 static int
369 put_compat_timeval(struct compat_timeval *u, struct timeval *t)
370 {
371         struct compat_timeval t32;
372         t32.tv_sec = t->tv_sec;
373         t32.tv_usec = t->tv_usec;
374         return copy_to_user(u, &t32, sizeof t32);
375 }
376
377 static inline long get_ts32(struct timespec *o, struct compat_timeval *i)
378 {
379         long usec;
380
381         if (__get_user(o->tv_sec, &i->tv_sec))
382                 return -EFAULT;
383         if (__get_user(usec, &i->tv_usec))
384                 return -EFAULT;
385         o->tv_nsec = usec * 1000;
386         return 0;
387 }
388
389 asmlinkage long sys32_time(compat_time_t *tloc)
390 {
391     struct timeval tv;
392
393         do_gettimeofday(&tv);
394         compat_time_t now32 = tv.tv_sec;
395
396         if (tloc)
397                 if (put_user(now32, tloc))
398                         now32 = -EFAULT;
399
400         return now32;
401 }
402
403 asmlinkage int
404 sys32_gettimeofday(struct compat_timeval *tv, struct timezone *tz)
405 {
406     extern void do_gettimeofday(struct timeval *tv);
407
408     if (tv) {
409             struct timeval ktv;
410             do_gettimeofday(&ktv);
411             if (put_compat_timeval(tv, &ktv))
412                     return -EFAULT;
413     }
414     if (tz) {
415             extern struct timezone sys_tz;
416             if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
417                     return -EFAULT;
418     }
419     return 0;
420 }
421
422 asmlinkage 
423 int sys32_settimeofday(struct compat_timeval *tv, struct timezone *tz)
424 {
425         struct timespec kts;
426         struct timezone ktz;
427
428         if (tv) {
429                 if (get_ts32(&kts, tv))
430                         return -EFAULT;
431         }
432         if (tz) {
433                 if (copy_from_user(&ktz, tz, sizeof(ktz)))
434                         return -EFAULT;
435         }
436
437         return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
438 }
439
440 int cp_compat_stat(struct kstat *stat, struct compat_stat *statbuf)
441 {
442         int err;
443
444         if (stat->size > MAX_NON_LFS || !new_valid_dev(stat->dev) ||
445             !new_valid_dev(stat->rdev))
446                 return -EOVERFLOW;
447
448         err  = put_user(new_encode_dev(stat->dev), &statbuf->st_dev);
449         err |= put_user(stat->ino, &statbuf->st_ino);
450         err |= put_user(stat->mode, &statbuf->st_mode);
451         err |= put_user(stat->nlink, &statbuf->st_nlink);
452         err |= put_user(0, &statbuf->st_reserved1);
453         err |= put_user(0, &statbuf->st_reserved2);
454         err |= put_user(new_encode_dev(stat->rdev), &statbuf->st_rdev);
455         err |= put_user(stat->size, &statbuf->st_size);
456         err |= put_user(stat->atime.tv_sec, &statbuf->st_atime);
457         err |= put_user(stat->atime.tv_nsec, &statbuf->st_atime_nsec);
458         err |= put_user(stat->mtime.tv_sec, &statbuf->st_mtime);
459         err |= put_user(stat->mtime.tv_nsec, &statbuf->st_mtime_nsec);
460         err |= put_user(stat->ctime.tv_sec, &statbuf->st_ctime);
461         err |= put_user(stat->ctime.tv_nsec, &statbuf->st_ctime_nsec);
462         err |= put_user(stat->blksize, &statbuf->st_blksize);
463         err |= put_user(stat->blocks, &statbuf->st_blocks);
464         err |= put_user(0, &statbuf->__unused1);
465         err |= put_user(0, &statbuf->__unused2);
466         err |= put_user(0, &statbuf->__unused3);
467         err |= put_user(0, &statbuf->__unused4);
468         err |= put_user(0, &statbuf->__unused5);
469         err |= put_user(0, &statbuf->st_fstype); /* not avail */
470         err |= put_user(0, &statbuf->st_realdev); /* not avail */
471         err |= put_user(0, &statbuf->st_basemode); /* not avail */
472         err |= put_user(0, &statbuf->st_spareshort);
473         err |= put_user(stat->uid, &statbuf->st_uid);
474         err |= put_user(stat->gid, &statbuf->st_gid);
475         err |= put_user(0, &statbuf->st_spare4[0]);
476         err |= put_user(0, &statbuf->st_spare4[1]);
477         err |= put_user(0, &statbuf->st_spare4[2]);
478
479         return err;
480 }
481
482 struct linux32_dirent {
483         u32             d_ino;
484         compat_off_t    d_off;
485         u16             d_reclen;
486         char            d_name[1];
487 };
488
489 struct old_linux32_dirent {
490         u32     d_ino;
491         u32     d_offset;
492         u16     d_namlen;
493         char    d_name[1];
494 };
495
496 struct getdents32_callback {
497         struct linux32_dirent * current_dir;
498         struct linux32_dirent * previous;
499         int count;
500         int error;
501 };
502
503 struct readdir32_callback {
504         struct old_linux32_dirent * dirent;
505         int count;
506 };
507
508 #define ROUND_UP(x,a)   ((__typeof__(x))(((unsigned long)(x) + ((a) - 1)) & ~((a) - 1)))
509 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
510 static int
511 filldir32 (void *__buf, const char *name, int namlen, loff_t offset, ino_t ino,
512            unsigned int d_type)
513 {
514         struct linux32_dirent * dirent;
515         struct getdents32_callback * buf = (struct getdents32_callback *) __buf;
516         int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 1, 4);
517
518         buf->error = -EINVAL;   /* only used if we fail.. */
519         if (reclen > buf->count)
520                 return -EINVAL;
521         dirent = buf->previous;
522         if (dirent)
523                 put_user(offset, &dirent->d_off);
524         dirent = buf->current_dir;
525         buf->previous = dirent;
526         put_user(ino, &dirent->d_ino);
527         put_user(reclen, &dirent->d_reclen);
528         copy_to_user(dirent->d_name, name, namlen);
529         put_user(0, dirent->d_name + namlen);
530         ((char *) dirent) += reclen;
531         buf->current_dir = dirent;
532         buf->count -= reclen;
533         return 0;
534 }
535
536 asmlinkage long
537 sys32_getdents (unsigned int fd, void * dirent, unsigned int count)
538 {
539         struct file * file;
540         struct linux32_dirent * lastdirent;
541         struct getdents32_callback buf;
542         int error;
543
544         error = -EBADF;
545         file = fget(fd);
546         if (!file)
547                 goto out;
548
549         buf.current_dir = (struct linux32_dirent *) dirent;
550         buf.previous = NULL;
551         buf.count = count;
552         buf.error = 0;
553
554         error = vfs_readdir(file, filldir32, &buf);
555         if (error < 0)
556                 goto out_putf;
557         error = buf.error;
558         lastdirent = buf.previous;
559         if (lastdirent) {
560                 put_user(file->f_pos, &lastdirent->d_off);
561                 error = count - buf.count;
562         }
563
564 out_putf:
565         fput(file);
566 out:
567         return error;
568 }
569
570 static int
571 fillonedir32 (void * __buf, const char * name, int namlen, loff_t offset, ino_t ino,
572               unsigned int d_type)
573 {
574         struct readdir32_callback * buf = (struct readdir32_callback *) __buf;
575         struct old_linux32_dirent * dirent;
576
577         if (buf->count)
578                 return -EINVAL;
579         buf->count++;
580         dirent = buf->dirent;
581         put_user(ino, &dirent->d_ino);
582         put_user(offset, &dirent->d_offset);
583         put_user(namlen, &dirent->d_namlen);
584         copy_to_user(dirent->d_name, name, namlen);
585         put_user(0, dirent->d_name + namlen);
586         return 0;
587 }
588
589 asmlinkage long
590 sys32_readdir (unsigned int fd, void * dirent, unsigned int count)
591 {
592         int error;
593         struct file * file;
594         struct readdir32_callback buf;
595
596         error = -EBADF;
597         file = fget(fd);
598         if (!file)
599                 goto out;
600
601         buf.count = 0;
602         buf.dirent = dirent;
603
604         error = vfs_readdir(file, fillonedir32, &buf);
605         if (error >= 0)
606                 error = buf.count;
607         fput(file);
608 out:
609         return error;
610 }
611
612 /* readv/writev stolen from mips64 */
613 typedef ssize_t (*IO_fn_t)(struct file *, char *, size_t, loff_t *);
614
615 static long
616 do_readv_writev32(int type, struct file *file, const struct compat_iovec *vector,
617                   u32 count)
618 {
619         unsigned long tot_len;
620         struct iovec iovstack[UIO_FASTIOV];
621         struct iovec *iov=iovstack, *ivp;
622         struct inode *inode;
623         long retval, i;
624         IO_fn_t fn;
625
626         /* First get the "struct iovec" from user memory and
627          * verify all the pointers
628          */
629         if (!count)
630                 return 0;
631         if(verify_area(VERIFY_READ, vector, sizeof(struct compat_iovec)*count))
632                 return -EFAULT;
633         if (count > UIO_MAXIOV)
634                 return -EINVAL;
635         if (count > UIO_FASTIOV) {
636                 iov = kmalloc(count*sizeof(struct iovec), GFP_KERNEL);
637                 if (!iov)
638                         return -ENOMEM;
639         }
640
641         tot_len = 0;
642         i = count;
643         ivp = iov;
644         while (i > 0) {
645                 u32 len;
646                 u32 buf;
647
648                 __get_user(len, &vector->iov_len);
649                 __get_user(buf, &vector->iov_base);
650                 tot_len += len;
651                 ivp->iov_base = compat_ptr(buf);
652                 ivp->iov_len = (compat_size_t) len;
653                 vector++;
654                 ivp++;
655                 i--;
656         }
657
658         inode = file->f_dentry->d_inode;
659         /* VERIFY_WRITE actually means a read, as we write to user space */
660         retval = locks_verify_area((type == VERIFY_WRITE
661                                     ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE),
662                                    inode, file, file->f_pos, tot_len);
663         if (retval) {
664                 if (iov != iovstack)
665                         kfree(iov);
666                 return retval;
667         }
668
669         /* Then do the actual IO.  Note that sockets need to be handled
670          * specially as they have atomicity guarantees and can handle
671          * iovec's natively
672          */
673         if (inode->i_sock) {
674                 int err;
675                 err = sock_readv_writev(type, inode, file, iov, count, tot_len);
676                 if (iov != iovstack)
677                         kfree(iov);
678                 return err;
679         }
680
681         if (!file->f_op) {
682                 if (iov != iovstack)
683                         kfree(iov);
684                 return -EINVAL;
685         }
686         /* VERIFY_WRITE actually means a read, as we write to user space */
687         fn = file->f_op->read;
688         if (type == VERIFY_READ)
689                 fn = (IO_fn_t) file->f_op->write;               
690         ivp = iov;
691         while (count > 0) {
692                 void * base;
693                 int len, nr;
694
695                 base = ivp->iov_base;
696                 len = ivp->iov_len;
697                 ivp++;
698                 count--;
699                 nr = fn(file, base, len, &file->f_pos);
700                 if (nr < 0) {
701                         if (retval)
702                                 break;
703                         retval = nr;
704                         break;
705                 }
706                 retval += nr;
707                 if (nr != len)
708                         break;
709         }
710         if (iov != iovstack)
711                 kfree(iov);
712
713         return retval;
714 }
715
716 asmlinkage long
717 sys32_readv(int fd, struct compat_iovec *vector, u32 count)
718 {
719         struct file *file;
720         ssize_t ret;
721
722         ret = -EBADF;
723         file = fget(fd);
724         if (!file)
725                 goto bad_file;
726         if (file->f_op && (file->f_mode & FMODE_READ) &&
727             (file->f_op->readv || file->f_op->read))
728                 ret = do_readv_writev32(VERIFY_WRITE, file, vector, count);
729
730         fput(file);
731
732 bad_file:
733         return ret;
734 }
735
736 asmlinkage long
737 sys32_writev(int fd, struct compat_iovec *vector, u32 count)
738 {
739         struct file *file;
740         ssize_t ret;
741
742         ret = -EBADF;
743         file = fget(fd);
744         if(!file)
745                 goto bad_file;
746         if (file->f_op && (file->f_mode & FMODE_WRITE) &&
747             (file->f_op->writev || file->f_op->write))
748                 ret = do_readv_writev32(VERIFY_READ, file, vector, count);
749         fput(file);
750
751 bad_file:
752         return ret;
753 }
754
755 /*** copied from mips64 ***/
756 /*
757  * Ooo, nasty.  We need here to frob 32-bit unsigned longs to
758  * 64-bit unsigned longs.
759  */
760
761 static inline int
762 get_fd_set32(unsigned long n, u32 *ufdset, unsigned long *fdset)
763 {
764         n = (n + 8*sizeof(u32) - 1) / (8*sizeof(u32));
765         if (ufdset) {
766                 unsigned long odd;
767
768                 if (verify_area(VERIFY_WRITE, ufdset, n*sizeof(u32)))
769                         return -EFAULT;
770
771                 odd = n & 1UL;
772                 n &= ~1UL;
773                 while (n) {
774                         unsigned long h, l;
775                         __get_user(l, ufdset);
776                         __get_user(h, ufdset+1);
777                         ufdset += 2;
778                         *fdset++ = h << 32 | l;
779                         n -= 2;
780                 }
781                 if (odd)
782                         __get_user(*fdset, ufdset);
783         } else {
784                 /* Tricky, must clear full unsigned long in the
785                  * kernel fdset at the end, this makes sure that
786                  * actually happens.
787                  */
788                 memset(fdset, 0, ((n + 1) & ~1)*sizeof(u32));
789         }
790         return 0;
791 }
792
793 static inline void
794 set_fd_set32(unsigned long n, u32 *ufdset, unsigned long *fdset)
795 {
796         unsigned long odd;
797         n = (n + 8*sizeof(u32) - 1) / (8*sizeof(u32));
798
799         if (!ufdset)
800                 return;
801
802         odd = n & 1UL;
803         n &= ~1UL;
804         while (n) {
805                 unsigned long h, l;
806                 l = *fdset++;
807                 h = l >> 32;
808                 __put_user(l, ufdset);
809                 __put_user(h, ufdset+1);
810                 ufdset += 2;
811                 n -= 2;
812         }
813         if (odd)
814                 __put_user(*fdset, ufdset);
815 }
816
817 /*** This is a virtual copy of sys_select from fs/select.c and probably
818  *** should be compared to it from time to time
819  ***/
820 static inline void *select_bits_alloc(int size)
821 {
822         return kmalloc(6 * size, GFP_KERNEL);
823 }
824
825 static inline void select_bits_free(void *bits, int size)
826 {
827         kfree(bits);
828 }
829
830 /*
831  * We can actually return ERESTARTSYS instead of EINTR, but I'd
832  * like to be certain this leads to no problems. So I return
833  * EINTR just for safety.
834  *
835  * Update: ERESTARTSYS breaks at least the xview clock binary, so
836  * I'm trying ERESTARTNOHAND which restart only when you want to.
837  */
838 #define MAX_SELECT_SECONDS \
839         ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
840 #define DIVIDE_ROUND_UP(x,y) (((x)+(y)-1)/(y))
841
842 asmlinkage long
843 sys32_select(int n, u32 *inp, u32 *outp, u32 *exp, struct compat_timeval *tvp)
844 {
845         fd_set_bits fds;
846         char *bits;
847         long timeout;
848         int ret, size, err;
849
850         timeout = MAX_SCHEDULE_TIMEOUT;
851         if (tvp) {
852                 struct compat_timeval tv32;
853                 time_t sec, usec;
854
855                 if ((ret = copy_from_user(&tv32, tvp, sizeof tv32)))
856                         goto out_nofds;
857
858                 sec = tv32.tv_sec;
859                 usec = tv32.tv_usec;
860
861                 ret = -EINVAL;
862                 if (sec < 0 || usec < 0)
863                         goto out_nofds;
864
865                 if ((unsigned long) sec < MAX_SELECT_SECONDS) {
866                         timeout = DIVIDE_ROUND_UP(usec, 1000000/HZ);
867                         timeout += sec * (unsigned long) HZ;
868                 }
869         }
870
871         ret = -EINVAL;
872         if (n < 0)
873                 goto out_nofds;
874
875         if (n > current->files->max_fdset)
876                 n = current->files->max_fdset;
877
878         /*
879          * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
880          * since we used fdset we need to allocate memory in units of
881          * long-words. 
882          */
883         ret = -ENOMEM;
884         size = FDS_BYTES(n);
885         bits = select_bits_alloc(size);
886         if (!bits)
887                 goto out_nofds;
888         fds.in      = (unsigned long *)  bits;
889         fds.out     = (unsigned long *) (bits +   size);
890         fds.ex      = (unsigned long *) (bits + 2*size);
891         fds.res_in  = (unsigned long *) (bits + 3*size);
892         fds.res_out = (unsigned long *) (bits + 4*size);
893         fds.res_ex  = (unsigned long *) (bits + 5*size);
894
895         if ((ret = get_fd_set32(n, inp, fds.in)) ||
896             (ret = get_fd_set32(n, outp, fds.out)) ||
897             (ret = get_fd_set32(n, exp, fds.ex)))
898                 goto out;
899         zero_fd_set(n, fds.res_in);
900         zero_fd_set(n, fds.res_out);
901         zero_fd_set(n, fds.res_ex);
902
903         ret = do_select(n, &fds, &timeout);
904
905         if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
906                 time_t sec = 0, usec = 0;
907                 if (timeout) {
908                         sec = timeout / HZ;
909                         usec = timeout % HZ;
910                         usec *= (1000000/HZ);
911                 }
912                 err = put_user(sec, &tvp->tv_sec);
913                 err |= __put_user(usec, &tvp->tv_usec);
914                 if (err)
915                         ret = -EFAULT;
916         }
917
918         if (ret < 0)
919                 goto out;
920         if (!ret) {
921                 ret = -ERESTARTNOHAND;
922                 if (signal_pending(current))
923                         goto out;
924                 ret = 0;
925         }
926
927         set_fd_set32(n, inp, fds.res_in);
928         set_fd_set32(n, outp, fds.res_out);
929         set_fd_set32(n, exp, fds.res_ex);
930
931 out:
932         select_bits_free(bits, size);
933 out_nofds:
934         return ret;
935 }
936
937 struct msgbuf32 {
938     int mtype;
939     char mtext[1];
940 };
941
942 asmlinkage long sys32_msgsnd(int msqid,
943                                 struct msgbuf32 *umsgp32,
944                                 size_t msgsz, int msgflg)
945 {
946         struct msgbuf *mb;
947         struct msgbuf32 mb32;
948         int err;
949
950         if ((mb = kmalloc(msgsz + sizeof *mb + 4, GFP_KERNEL)) == NULL)
951                 return -ENOMEM;
952
953         err = get_user(mb32.mtype, &umsgp32->mtype);
954         mb->mtype = mb32.mtype;
955         err |= copy_from_user(mb->mtext, &umsgp32->mtext, msgsz);
956
957         if (err)
958                 err = -EFAULT;
959         else
960                 KERNEL_SYSCALL(err, sys_msgsnd, msqid, mb, msgsz, msgflg);
961
962         kfree(mb);
963         return err;
964 }
965
966 asmlinkage long sys32_msgrcv(int msqid,
967                                 struct msgbuf32 *umsgp32,
968                                 size_t msgsz, long msgtyp, int msgflg)
969 {
970         struct msgbuf *mb;
971         struct msgbuf32 mb32;
972         int err, len;
973
974         if ((mb = kmalloc(msgsz + sizeof *mb + 4, GFP_KERNEL)) == NULL)
975                 return -ENOMEM;
976
977         KERNEL_SYSCALL(err, sys_msgrcv, msqid, mb, msgsz, msgtyp, msgflg);
978
979         if (err >= 0) {
980                 len = err;
981                 mb32.mtype = mb->mtype;
982                 err = put_user(mb32.mtype, &umsgp32->mtype);
983                 err |= copy_to_user(&umsgp32->mtext, mb->mtext, len);
984                 if (err)
985                         err = -EFAULT;
986                 else
987                         err = len;
988         }
989
990         kfree(mb);
991         return err;
992 }
993
994
995 asmlinkage int sys32_sendfile(int out_fd, int in_fd, compat_off_t *offset, s32 count)
996 {
997         mm_segment_t old_fs = get_fs();
998         int ret;
999         off_t of;
1000
1001         if (offset && get_user(of, offset))
1002                 return -EFAULT;
1003
1004         set_fs(KERNEL_DS);
1005         ret = sys_sendfile(out_fd, in_fd, offset ? &of : NULL, count);
1006         set_fs(old_fs);
1007
1008         if (offset && put_user(of, offset))
1009                 return -EFAULT;
1010
1011         return ret;
1012 }
1013
1014 /* EXPORT/UNEXPORT */
1015 struct nfsctl_export32 {
1016         char            ex_client[NFSCLNT_IDMAX+1];
1017         char            ex_path[NFS_MAXPATHLEN+1];
1018         __kernel_old_dev_t ex_dev;
1019         compat_ino_t    ex_ino;
1020         int             ex_flags;
1021         __kernel_uid_t  ex_anon_uid;
1022         __kernel_gid_t  ex_anon_gid;
1023 };
1024
1025 struct nfsctl_arg32 {
1026         int                     ca_version;     /* safeguard */
1027         /* wide kernel places this union on 8-byte boundary, narrow on 4 */
1028         union {
1029                 struct nfsctl_svc       u_svc;
1030                 struct nfsctl_client    u_client;
1031                 struct nfsctl_export32  u_export;
1032                 struct nfsctl_fdparm    u_getfd;
1033                 struct nfsctl_fsparm    u_getfs;
1034         } u;
1035 };
1036
1037 asmlinkage int sys32_nfsservctl(int cmd, void *argp, void *resp)
1038 {
1039         int ret, tmp;
1040         struct nfsctl_arg32 n32;
1041         struct nfsctl_arg n;
1042
1043         ret = copy_from_user(&n, argp, sizeof n.ca_version);
1044         if (ret != 0)
1045                 return ret;
1046
1047         /* adjust argp to point at the union inside the user's n32 struct */
1048         tmp = (unsigned long)&n32.u - (unsigned long)&n32;
1049         argp = (void *)((unsigned long)argp + tmp);
1050         switch(cmd) {
1051         case NFSCTL_SVC:
1052                 ret = copy_from_user(&n.u, argp, sizeof n.u.u_svc);
1053                 break;
1054
1055         case NFSCTL_ADDCLIENT:
1056         case NFSCTL_DELCLIENT:
1057                 ret = copy_from_user(&n.u, argp, sizeof n.u.u_client);
1058                 break;
1059
1060         case NFSCTL_GETFD:
1061                 ret = copy_from_user(&n.u, argp, sizeof n.u.u_getfd);
1062                 break;
1063
1064         case NFSCTL_GETFS:
1065                 ret = copy_from_user(&n.u, argp, sizeof n.u.u_getfs);
1066                 break;
1067
1068         case NFSCTL_UNEXPORT:           /* nfsctl_export */
1069         case NFSCTL_EXPORT:             /* nfsctl_export */
1070                 ret = copy_from_user(&n32.u, argp, sizeof n32.u.u_export);
1071 #undef CP
1072 #define CP(x)   n.u.u_export.ex_##x = n32.u.u_export.ex_##x
1073                 memcpy(n.u.u_export.ex_client, n32.u.u_export.ex_client, sizeof n32.u.u_export.ex_client);
1074                 memcpy(n.u.u_export.ex_path, n32.u.u_export.ex_path, sizeof n32.u.u_export.ex_path);
1075                 CP(dev);
1076                 CP(ino);
1077                 CP(flags);
1078                 CP(anon_uid);
1079                 CP(anon_gid);
1080                 break;
1081
1082         default:
1083                 /* lockd probes for some other values (0x10000);
1084                  * so don't BUG() */
1085                 ret = -EINVAL;
1086                 break;
1087         }
1088
1089         if (ret == 0) {
1090                 unsigned char rbuf[NFS_FHSIZE + sizeof (struct knfsd_fh)];
1091                 KERNEL_SYSCALL(ret, sys_nfsservctl, cmd, &n, &rbuf);
1092                 if (cmd == NFSCTL_GETFD) {
1093                         ret = copy_to_user(resp, rbuf, NFS_FHSIZE);
1094                 } else if (cmd == NFSCTL_GETFS) {
1095                         ret = copy_to_user(resp, rbuf, sizeof (struct knfsd_fh));
1096                 }
1097         }
1098
1099         return ret;
1100 }
1101
1102 typedef long __kernel_loff_t32;         /* move this to asm/posix_types.h? */
1103
1104 asmlinkage int sys32_sendfile64(int out_fd, int in_fd, __kernel_loff_t32 *offset, s32 count)
1105 {
1106         mm_segment_t old_fs = get_fs();
1107         int ret;
1108         loff_t lof;
1109         
1110         if (offset && get_user(lof, offset))
1111                 return -EFAULT;
1112                 
1113         set_fs(KERNEL_DS);
1114         ret = sys_sendfile64(out_fd, in_fd, offset ? &lof : NULL, count);
1115         set_fs(old_fs);
1116         
1117         if (offset && put_user(lof, offset))
1118                 return -EFAULT;
1119                 
1120         return ret;
1121 }
1122
1123
1124 struct timex32 {
1125         unsigned int modes;     /* mode selector */
1126         int offset;             /* time offset (usec) */
1127         int freq;               /* frequency offset (scaled ppm) */
1128         int maxerror;           /* maximum error (usec) */
1129         int esterror;           /* estimated error (usec) */
1130         int status;             /* clock command/status */
1131         int constant;           /* pll time constant */
1132         int precision;          /* clock precision (usec) (read only) */
1133         int tolerance;          /* clock frequency tolerance (ppm)
1134                                  * (read only)
1135                                  */
1136         struct compat_timeval time;     /* (read only) */
1137         int tick;               /* (modified) usecs between clock ticks */
1138
1139         int ppsfreq;           /* pps frequency (scaled ppm) (ro) */
1140         int jitter;            /* pps jitter (us) (ro) */
1141         int shift;              /* interval duration (s) (shift) (ro) */
1142         int stabil;            /* pps stability (scaled ppm) (ro) */
1143         int jitcnt;            /* jitter limit exceeded (ro) */
1144         int calcnt;            /* calibration intervals (ro) */
1145         int errcnt;            /* calibration errors (ro) */
1146         int stbcnt;            /* stability limit exceeded (ro) */
1147
1148         int  :32; int  :32; int  :32; int  :32;
1149         int  :32; int  :32; int  :32; int  :32;
1150         int  :32; int  :32; int  :32; int  :32;
1151 };
1152
1153 asmlinkage long sys32_adjtimex(struct timex32 *txc_p32)
1154 {
1155         struct timex txc;
1156         struct timex32 t32;
1157         int ret;
1158         extern int do_adjtimex(struct timex *txc);
1159
1160         if(copy_from_user(&t32, txc_p32, sizeof(struct timex32)))
1161                 return -EFAULT;
1162 #undef CP
1163 #define CP(x) txc.x = t32.x
1164         CP(modes); CP(offset); CP(freq); CP(maxerror); CP(esterror);
1165         CP(status); CP(constant); CP(precision); CP(tolerance);
1166         CP(time.tv_sec); CP(time.tv_usec); CP(tick); CP(ppsfreq); CP(jitter);
1167         CP(shift); CP(stabil); CP(jitcnt); CP(calcnt); CP(errcnt);
1168         CP(stbcnt);
1169         ret = do_adjtimex(&txc);
1170 #undef CP
1171 #define CP(x) t32.x = txc.x
1172         CP(modes); CP(offset); CP(freq); CP(maxerror); CP(esterror);
1173         CP(status); CP(constant); CP(precision); CP(tolerance);
1174         CP(time.tv_sec); CP(time.tv_usec); CP(tick); CP(ppsfreq); CP(jitter);
1175         CP(shift); CP(stabil); CP(jitcnt); CP(calcnt); CP(errcnt);
1176         CP(stbcnt);
1177         return copy_to_user(txc_p32, &t32, sizeof(struct timex32)) ? -EFAULT : ret;
1178 }
1179
1180
1181 struct sysinfo32 {
1182         s32 uptime;
1183         u32 loads[3];
1184         u32 totalram;
1185         u32 freeram;
1186         u32 sharedram;
1187         u32 bufferram;
1188         u32 totalswap;
1189         u32 freeswap;
1190         unsigned short procs;
1191         u32 totalhigh;
1192         u32 freehigh;
1193         u32 mem_unit;
1194         char _f[12];
1195 };
1196
1197 /* We used to call sys_sysinfo and translate the result.  But sys_sysinfo
1198  * undoes the good work done elsewhere, and rather than undoing the
1199  * damage, I decided to just duplicate the code from sys_sysinfo here.
1200  */
1201
1202 asmlinkage int sys32_sysinfo(struct sysinfo32 *info)
1203 {
1204         struct sysinfo val;
1205         int err;
1206         unsigned long seq;
1207
1208         /* We don't need a memset here because we copy the
1209          * struct to userspace once element at a time.
1210          */
1211
1212         do {
1213                 seq = read_seqbegin(&xtime_lock);
1214                 val.uptime = jiffies / HZ;
1215
1216                 val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
1217                 val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
1218                 val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
1219
1220                 val.procs = nr_threads;
1221         } while (read_seqretry(&xtime_lock, seq));
1222
1223
1224         si_meminfo(&val);
1225         si_swapinfo(&val);
1226         
1227         err = put_user (val.uptime, &info->uptime);
1228         err |= __put_user (val.loads[0], &info->loads[0]);
1229         err |= __put_user (val.loads[1], &info->loads[1]);
1230         err |= __put_user (val.loads[2], &info->loads[2]);
1231         err |= __put_user (val.totalram, &info->totalram);
1232         err |= __put_user (val.freeram, &info->freeram);
1233         err |= __put_user (val.sharedram, &info->sharedram);
1234         err |= __put_user (val.bufferram, &info->bufferram);
1235         err |= __put_user (val.totalswap, &info->totalswap);
1236         err |= __put_user (val.freeswap, &info->freeswap);
1237         err |= __put_user (val.procs, &info->procs);
1238         err |= __put_user (val.totalhigh, &info->totalhigh);
1239         err |= __put_user (val.freehigh, &info->freehigh);
1240         err |= __put_user (val.mem_unit, &info->mem_unit);
1241         return err ? -EFAULT : 0;
1242 }
1243
1244
1245 /* lseek() needs a wrapper because 'offset' can be negative, but the top
1246  * half of the argument has been zeroed by syscall.S.
1247  */
1248
1249 asmlinkage int sys32_lseek(unsigned int fd, int offset, unsigned int origin)
1250 {
1251         return sys_lseek(fd, offset, origin);
1252 }
1253
1254 asmlinkage long sys32_semctl(int semid, int semnum, int cmd, union semun arg)
1255 {
1256         union semun u;
1257         
1258         if (cmd == SETVAL) {
1259                 /* Ugh.  arg is a union of int,ptr,ptr,ptr, so is 8 bytes.
1260                  * The int should be in the first 4, but our argument
1261                  * frobbing has left it in the last 4.
1262                  */
1263                 u.val = *((int *)&arg + 1);
1264                 return sys_semctl (semid, semnum, cmd, u);
1265         }
1266         return sys_semctl (semid, semnum, cmd, arg);
1267 }
1268
1269 long sys32_lookup_dcookie(u32 cookie_high, u32 cookie_low, char *buf,
1270                           size_t len)
1271 {
1272         return sys_lookup_dcookie((u64)cookie_high << 32 | cookie_low,
1273                                   buf, len);
1274 }