]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/parisc/kernel/sys_parisc32.c
[PA-RISC] Fix sys32_sysctl
[karo-tx-linux.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/compat.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/fs.h> 
16 #include <linux/mm.h> 
17 #include <linux/file.h> 
18 #include <linux/signal.h>
19 #include <linux/resource.h>
20 #include <linux/times.h>
21 #include <linux/utsname.h>
22 #include <linux/time.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <linux/sem.h>
26 #include <linux/msg.h>
27 #include <linux/shm.h>
28 #include <linux/slab.h>
29 #include <linux/uio.h>
30 #include <linux/nfs_fs.h>
31 #include <linux/ncp_fs.h>
32 #include <linux/sunrpc/svc.h>
33 #include <linux/nfsd/nfsd.h>
34 #include <linux/nfsd/cache.h>
35 #include <linux/nfsd/xdr.h>
36 #include <linux/nfsd/syscall.h>
37 #include <linux/poll.h>
38 #include <linux/personality.h>
39 #include <linux/stat.h>
40 #include <linux/highmem.h>
41 #include <linux/highuid.h>
42 #include <linux/mman.h>
43 #include <linux/binfmts.h>
44 #include <linux/namei.h>
45 #include <linux/vfs.h>
46 #include <linux/ptrace.h>
47 #include <linux/swap.h>
48 #include <linux/syscalls.h>
49
50 #include <asm/types.h>
51 #include <asm/uaccess.h>
52 #include <asm/semaphore.h>
53 #include <asm/mmu_context.h>
54
55 #include "sys32.h"
56
57 #undef DEBUG
58
59 #ifdef DEBUG
60 #define DBG(x)  printk x
61 #else
62 #define DBG(x)
63 #endif
64
65 /*
66  * sys32_execve() executes a new program.
67  */
68
69 asmlinkage int sys32_execve(struct pt_regs *regs)
70 {
71         int error;
72         char *filename;
73
74         DBG(("sys32_execve(%p) r26 = 0x%lx\n", regs, regs->gr[26]));
75         filename = getname((const char __user *) regs->gr[26]);
76         error = PTR_ERR(filename);
77         if (IS_ERR(filename))
78                 goto out;
79         error = compat_do_execve(filename, compat_ptr(regs->gr[25]),
80                                  compat_ptr(regs->gr[24]), regs);
81         if (error == 0) {
82                 task_lock(current);
83                 current->ptrace &= ~PT_DTRACE;
84                 task_unlock(current);
85         }
86         putname(filename);
87 out:
88
89         return error;
90 }
91
92 asmlinkage long sys32_unimplemented(int r26, int r25, int r24, int r23,
93         int r22, int r21, int r20)
94 {
95     printk(KERN_ERR "%s(%d): Unimplemented 32 on 64 syscall #%d!\n", 
96         current->comm, current->pid, r20);
97     return -ENOSYS;
98 }
99
100 #ifdef CONFIG_SYSCTL
101
102 struct __sysctl_args32 {
103         u32 name;
104         int nlen;
105         u32 oldval;
106         u32 oldlenp;
107         u32 newval;
108         u32 newlen;
109         u32 __unused[4];
110 };
111
112 asmlinkage long sys32_sysctl(struct __sysctl_args32 __user *args)
113 {
114 #ifndef CONFIG_SYSCTL_SYSCALL
115         return -ENOSYS;
116 #else
117         struct __sysctl_args32 tmp;
118         int error;
119         unsigned int oldlen32;
120         size_t oldlen, __user *oldlenp = NULL;
121         unsigned long addr = (((long __force)&args->__unused[0]) + 7) & ~7;
122
123         DBG(("sysctl32(%p)\n", args));
124
125         if (copy_from_user(&tmp, args, sizeof(tmp)))
126                 return -EFAULT;
127
128         if (tmp.oldval && tmp.oldlenp) {
129                 /* Duh, this is ugly and might not work if sysctl_args
130                    is in read-only memory, but do_sysctl does indirectly
131                    a lot of uaccess in both directions and we'd have to
132                    basically copy the whole sysctl.c here, and
133                    glibc's __sysctl uses rw memory for the structure
134                    anyway.  */
135                 /* a possibly better hack than this, which will avoid the
136                  * problem if the struct is read only, is to push the
137                  * 'oldlen' value out to the user's stack instead. -PB
138                  */
139                 if (get_user(oldlen32, (u32 *)(u64)tmp.oldlenp))
140                         return -EFAULT;
141                 oldlen = oldlen32;
142                 if (put_user(oldlen, (size_t *)addr))
143                         return -EFAULT;
144                 oldlenp = (size_t *)addr;
145         }
146
147         lock_kernel();
148         error = do_sysctl((int __user *)(u64)tmp.name, tmp.nlen,
149                           (void __user *)(u64)tmp.oldval, oldlenp,
150                           (void __user *)(u64)tmp.newval, tmp.newlen);
151         unlock_kernel();
152         if (oldlenp) {
153                 if (!error) {
154                         if (get_user(oldlen, (size_t *)addr)) {
155                                 error = -EFAULT;
156                         } else {
157                                 oldlen32 = oldlen;
158                                 if (put_user(oldlen32, (u32 *)(u64)tmp.oldlenp))
159                                         error = -EFAULT;
160                         }
161                 }
162                 if (copy_to_user(args->__unused, tmp.__unused, sizeof(tmp.__unused)))
163                         error = -EFAULT;
164         }
165         return error;
166 #endif
167 }
168
169 #endif /* CONFIG_SYSCTL */
170
171 asmlinkage long sys32_sched_rr_get_interval(pid_t pid,
172         struct compat_timespec __user *interval)
173 {
174         struct timespec t;
175         int ret;
176
177         KERNEL_SYSCALL(ret, sys_sched_rr_get_interval, pid, (struct timespec __user *)&t);
178         if (put_compat_timespec(&t, interval))
179                 return -EFAULT;
180         return ret;
181 }
182
183 static int
184 put_compat_timeval(struct compat_timeval __user *u, struct timeval *t)
185 {
186         struct compat_timeval t32;
187         t32.tv_sec = t->tv_sec;
188         t32.tv_usec = t->tv_usec;
189         return copy_to_user(u, &t32, sizeof t32);
190 }
191
192 static inline long get_ts32(struct timespec *o, struct compat_timeval __user *i)
193 {
194         long usec;
195
196         if (__get_user(o->tv_sec, &i->tv_sec))
197                 return -EFAULT;
198         if (__get_user(usec, &i->tv_usec))
199                 return -EFAULT;
200         o->tv_nsec = usec * 1000;
201         return 0;
202 }
203
204 asmlinkage int
205 sys32_gettimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
206 {
207     extern void do_gettimeofday(struct timeval *tv);
208
209     if (tv) {
210             struct timeval ktv;
211             do_gettimeofday(&ktv);
212             if (put_compat_timeval(tv, &ktv))
213                     return -EFAULT;
214     }
215     if (tz) {
216             extern struct timezone sys_tz;
217             if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
218                     return -EFAULT;
219     }
220     return 0;
221 }
222
223 asmlinkage 
224 int sys32_settimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
225 {
226         struct timespec kts;
227         struct timezone ktz;
228
229         if (tv) {
230                 if (get_ts32(&kts, tv))
231                         return -EFAULT;
232         }
233         if (tz) {
234                 if (copy_from_user(&ktz, tz, sizeof(ktz)))
235                         return -EFAULT;
236         }
237
238         return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
239 }
240
241 int cp_compat_stat(struct kstat *stat, struct compat_stat __user *statbuf)
242 {
243         compat_ino_t ino;
244         int err;
245
246         if (stat->size > MAX_NON_LFS || !new_valid_dev(stat->dev) ||
247             !new_valid_dev(stat->rdev))
248                 return -EOVERFLOW;
249
250         ino = stat->ino;
251         if (sizeof(ino) < sizeof(stat->ino) && ino != stat->ino)
252                 return -EOVERFLOW;
253
254         err  = put_user(new_encode_dev(stat->dev), &statbuf->st_dev);
255         err |= put_user(ino, &statbuf->st_ino);
256         err |= put_user(stat->mode, &statbuf->st_mode);
257         err |= put_user(stat->nlink, &statbuf->st_nlink);
258         err |= put_user(0, &statbuf->st_reserved1);
259         err |= put_user(0, &statbuf->st_reserved2);
260         err |= put_user(new_encode_dev(stat->rdev), &statbuf->st_rdev);
261         err |= put_user(stat->size, &statbuf->st_size);
262         err |= put_user(stat->atime.tv_sec, &statbuf->st_atime);
263         err |= put_user(stat->atime.tv_nsec, &statbuf->st_atime_nsec);
264         err |= put_user(stat->mtime.tv_sec, &statbuf->st_mtime);
265         err |= put_user(stat->mtime.tv_nsec, &statbuf->st_mtime_nsec);
266         err |= put_user(stat->ctime.tv_sec, &statbuf->st_ctime);
267         err |= put_user(stat->ctime.tv_nsec, &statbuf->st_ctime_nsec);
268         err |= put_user(stat->blksize, &statbuf->st_blksize);
269         err |= put_user(stat->blocks, &statbuf->st_blocks);
270         err |= put_user(0, &statbuf->__unused1);
271         err |= put_user(0, &statbuf->__unused2);
272         err |= put_user(0, &statbuf->__unused3);
273         err |= put_user(0, &statbuf->__unused4);
274         err |= put_user(0, &statbuf->__unused5);
275         err |= put_user(0, &statbuf->st_fstype); /* not avail */
276         err |= put_user(0, &statbuf->st_realdev); /* not avail */
277         err |= put_user(0, &statbuf->st_basemode); /* not avail */
278         err |= put_user(0, &statbuf->st_spareshort);
279         err |= put_user(stat->uid, &statbuf->st_uid);
280         err |= put_user(stat->gid, &statbuf->st_gid);
281         err |= put_user(0, &statbuf->st_spare4[0]);
282         err |= put_user(0, &statbuf->st_spare4[1]);
283         err |= put_user(0, &statbuf->st_spare4[2]);
284
285         return err;
286 }
287
288 struct linux32_dirent {
289         u32             d_ino;
290         compat_off_t    d_off;
291         u16             d_reclen;
292         char            d_name[1];
293 };
294
295 struct old_linux32_dirent {
296         u32     d_ino;
297         u32     d_offset;
298         u16     d_namlen;
299         char    d_name[1];
300 };
301
302 struct getdents32_callback {
303         struct linux32_dirent __user * current_dir;
304         struct linux32_dirent __user * previous;
305         int count;
306         int error;
307 };
308
309 struct readdir32_callback {
310         struct old_linux32_dirent __user * dirent;
311         int count;
312 };
313
314 #define ROUND_UP(x,a)   ((__typeof__(x))(((unsigned long)(x) + ((a) - 1)) & ~((a) - 1)))
315 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
316 static int
317 filldir32 (void *__buf, const char *name, int namlen, loff_t offset, ino_t ino,
318            unsigned int d_type)
319 {
320         struct linux32_dirent __user * dirent;
321         struct getdents32_callback * buf = (struct getdents32_callback *) __buf;
322         int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 1, 4);
323         u32 d_ino;
324
325         buf->error = -EINVAL;   /* only used if we fail.. */
326         if (reclen > buf->count)
327                 return -EINVAL;
328         d_ino = ino;
329         if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
330                 return -EOVERFLOW;
331         dirent = buf->previous;
332         if (dirent)
333                 put_user(offset, &dirent->d_off);
334         dirent = buf->current_dir;
335         buf->previous = dirent;
336         put_user(d_ino, &dirent->d_ino);
337         put_user(reclen, &dirent->d_reclen);
338         copy_to_user(dirent->d_name, name, namlen);
339         put_user(0, dirent->d_name + namlen);
340         dirent = ((void __user *)dirent) + reclen;
341         buf->current_dir = dirent;
342         buf->count -= reclen;
343         return 0;
344 }
345
346 asmlinkage long
347 sys32_getdents (unsigned int fd, void __user * dirent, unsigned int count)
348 {
349         struct file * file;
350         struct linux32_dirent __user * lastdirent;
351         struct getdents32_callback buf;
352         int error;
353
354         error = -EBADF;
355         file = fget(fd);
356         if (!file)
357                 goto out;
358
359         buf.current_dir = (struct linux32_dirent __user *) dirent;
360         buf.previous = NULL;
361         buf.count = count;
362         buf.error = 0;
363
364         error = vfs_readdir(file, filldir32, &buf);
365         if (error < 0)
366                 goto out_putf;
367         error = buf.error;
368         lastdirent = buf.previous;
369         if (lastdirent) {
370                 put_user(file->f_pos, &lastdirent->d_off);
371                 error = count - buf.count;
372         }
373
374 out_putf:
375         fput(file);
376 out:
377         return error;
378 }
379
380 static int
381 fillonedir32 (void * __buf, const char * name, int namlen, loff_t offset, ino_t ino,
382               unsigned int d_type)
383 {
384         struct readdir32_callback * buf = (struct readdir32_callback *) __buf;
385         struct old_linux32_dirent __user * dirent;
386         u32 d_ino;
387
388         if (buf->count)
389                 return -EINVAL;
390         d_ino = ino;
391         if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
392                 return -EOVERFLOW;
393         buf->count++;
394         dirent = buf->dirent;
395         put_user(d_ino, &dirent->d_ino);
396         put_user(offset, &dirent->d_offset);
397         put_user(namlen, &dirent->d_namlen);
398         copy_to_user(dirent->d_name, name, namlen);
399         put_user(0, dirent->d_name + namlen);
400         return 0;
401 }
402
403 asmlinkage long
404 sys32_readdir (unsigned int fd, void __user * dirent, unsigned int count)
405 {
406         int error;
407         struct file * file;
408         struct readdir32_callback buf;
409
410         error = -EBADF;
411         file = fget(fd);
412         if (!file)
413                 goto out;
414
415         buf.count = 0;
416         buf.dirent = dirent;
417
418         error = vfs_readdir(file, fillonedir32, &buf);
419         if (error >= 0)
420                 error = buf.count;
421         fput(file);
422 out:
423         return error;
424 }
425
426 /*** copied from mips64 ***/
427 /*
428  * Ooo, nasty.  We need here to frob 32-bit unsigned longs to
429  * 64-bit unsigned longs.
430  */
431
432 static inline int
433 get_fd_set32(unsigned long n, u32 *ufdset, unsigned long *fdset)
434 {
435         n = (n + 8*sizeof(u32) - 1) / (8*sizeof(u32));
436         if (ufdset) {
437                 unsigned long odd;
438
439                 if (!access_ok(VERIFY_WRITE, ufdset, n*sizeof(u32)))
440                         return -EFAULT;
441
442                 odd = n & 1UL;
443                 n &= ~1UL;
444                 while (n) {
445                         unsigned long h, l;
446                         __get_user(l, ufdset);
447                         __get_user(h, ufdset+1);
448                         ufdset += 2;
449                         *fdset++ = h << 32 | l;
450                         n -= 2;
451                 }
452                 if (odd)
453                         __get_user(*fdset, ufdset);
454         } else {
455                 /* Tricky, must clear full unsigned long in the
456                  * kernel fdset at the end, this makes sure that
457                  * actually happens.
458                  */
459                 memset(fdset, 0, ((n + 1) & ~1)*sizeof(u32));
460         }
461         return 0;
462 }
463
464 static inline void
465 set_fd_set32(unsigned long n, u32 *ufdset, unsigned long *fdset)
466 {
467         unsigned long odd;
468         n = (n + 8*sizeof(u32) - 1) / (8*sizeof(u32));
469
470         if (!ufdset)
471                 return;
472
473         odd = n & 1UL;
474         n &= ~1UL;
475         while (n) {
476                 unsigned long h, l;
477                 l = *fdset++;
478                 h = l >> 32;
479                 __put_user(l, ufdset);
480                 __put_user(h, ufdset+1);
481                 ufdset += 2;
482                 n -= 2;
483         }
484         if (odd)
485                 __put_user(*fdset, ufdset);
486 }
487
488 struct msgbuf32 {
489     int mtype;
490     char mtext[1];
491 };
492
493 asmlinkage long sys32_msgsnd(int msqid,
494                                 struct msgbuf32 __user *umsgp32,
495                                 size_t msgsz, int msgflg)
496 {
497         struct msgbuf *mb;
498         struct msgbuf32 mb32;
499         int err;
500
501         if ((mb = kmalloc(msgsz + sizeof *mb + 4, GFP_KERNEL)) == NULL)
502                 return -ENOMEM;
503
504         err = get_user(mb32.mtype, &umsgp32->mtype);
505         mb->mtype = mb32.mtype;
506         err |= copy_from_user(mb->mtext, &umsgp32->mtext, msgsz);
507
508         if (err)
509                 err = -EFAULT;
510         else
511                 KERNEL_SYSCALL(err, sys_msgsnd, msqid, (struct msgbuf __user *)mb, msgsz, msgflg);
512
513         kfree(mb);
514         return err;
515 }
516
517 asmlinkage long sys32_msgrcv(int msqid,
518                                 struct msgbuf32 __user *umsgp32,
519                                 size_t msgsz, long msgtyp, int msgflg)
520 {
521         struct msgbuf *mb;
522         struct msgbuf32 mb32;
523         int err, len;
524
525         if ((mb = kmalloc(msgsz + sizeof *mb + 4, GFP_KERNEL)) == NULL)
526                 return -ENOMEM;
527
528         KERNEL_SYSCALL(err, sys_msgrcv, msqid, (struct msgbuf __user *)mb, msgsz, msgtyp, msgflg);
529
530         if (err >= 0) {
531                 len = err;
532                 mb32.mtype = mb->mtype;
533                 err = put_user(mb32.mtype, &umsgp32->mtype);
534                 err |= copy_to_user(&umsgp32->mtext, mb->mtext, len);
535                 if (err)
536                         err = -EFAULT;
537                 else
538                         err = len;
539         }
540
541         kfree(mb);
542         return err;
543 }
544
545 asmlinkage int sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset, s32 count)
546 {
547         mm_segment_t old_fs = get_fs();
548         int ret;
549         off_t of;
550
551         if (offset && get_user(of, offset))
552                 return -EFAULT;
553
554         set_fs(KERNEL_DS);
555         ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *)&of : NULL, count);
556         set_fs(old_fs);
557
558         if (offset && put_user(of, offset))
559                 return -EFAULT;
560
561         return ret;
562 }
563
564 asmlinkage int sys32_sendfile64(int out_fd, int in_fd, compat_loff_t __user *offset, s32 count)
565 {
566         mm_segment_t old_fs = get_fs();
567         int ret;
568         loff_t lof;
569         
570         if (offset && get_user(lof, offset))
571                 return -EFAULT;
572                 
573         set_fs(KERNEL_DS);
574         ret = sys_sendfile64(out_fd, in_fd, offset ? (loff_t __user *)&lof : NULL, count);
575         set_fs(old_fs);
576         
577         if (offset && put_user(lof, offset))
578                 return -EFAULT;
579                 
580         return ret;
581 }
582
583
584 struct sysinfo32 {
585         s32 uptime;
586         u32 loads[3];
587         u32 totalram;
588         u32 freeram;
589         u32 sharedram;
590         u32 bufferram;
591         u32 totalswap;
592         u32 freeswap;
593         unsigned short procs;
594         u32 totalhigh;
595         u32 freehigh;
596         u32 mem_unit;
597         char _f[12];
598 };
599
600 /* We used to call sys_sysinfo and translate the result.  But sys_sysinfo
601  * undoes the good work done elsewhere, and rather than undoing the
602  * damage, I decided to just duplicate the code from sys_sysinfo here.
603  */
604
605 asmlinkage int sys32_sysinfo(struct sysinfo32 __user *info)
606 {
607         struct sysinfo val;
608         int err;
609         unsigned long seq;
610
611         /* We don't need a memset here because we copy the
612          * struct to userspace once element at a time.
613          */
614
615         do {
616                 seq = read_seqbegin(&xtime_lock);
617                 val.uptime = jiffies / HZ;
618
619                 val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
620                 val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
621                 val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
622
623                 val.procs = nr_threads;
624         } while (read_seqretry(&xtime_lock, seq));
625
626
627         si_meminfo(&val);
628         si_swapinfo(&val);
629         
630         err = put_user (val.uptime, &info->uptime);
631         err |= __put_user (val.loads[0], &info->loads[0]);
632         err |= __put_user (val.loads[1], &info->loads[1]);
633         err |= __put_user (val.loads[2], &info->loads[2]);
634         err |= __put_user (val.totalram, &info->totalram);
635         err |= __put_user (val.freeram, &info->freeram);
636         err |= __put_user (val.sharedram, &info->sharedram);
637         err |= __put_user (val.bufferram, &info->bufferram);
638         err |= __put_user (val.totalswap, &info->totalswap);
639         err |= __put_user (val.freeswap, &info->freeswap);
640         err |= __put_user (val.procs, &info->procs);
641         err |= __put_user (val.totalhigh, &info->totalhigh);
642         err |= __put_user (val.freehigh, &info->freehigh);
643         err |= __put_user (val.mem_unit, &info->mem_unit);
644         return err ? -EFAULT : 0;
645 }
646
647
648 /* lseek() needs a wrapper because 'offset' can be negative, but the top
649  * half of the argument has been zeroed by syscall.S.
650  */
651
652 asmlinkage int sys32_lseek(unsigned int fd, int offset, unsigned int origin)
653 {
654         return sys_lseek(fd, offset, origin);
655 }
656
657 asmlinkage long sys32_semctl(int semid, int semnum, int cmd, union semun arg)
658 {
659         union semun u;
660         
661         if (cmd == SETVAL) {
662                 /* Ugh.  arg is a union of int,ptr,ptr,ptr, so is 8 bytes.
663                  * The int should be in the first 4, but our argument
664                  * frobbing has left it in the last 4.
665                  */
666                 u.val = *((int *)&arg + 1);
667                 return sys_semctl (semid, semnum, cmd, u);
668         }
669         return sys_semctl (semid, semnum, cmd, arg);
670 }
671
672 long sys32_lookup_dcookie(u32 cookie_high, u32 cookie_low, char __user *buf,
673                           size_t len)
674 {
675         return sys_lookup_dcookie((u64)cookie_high << 32 | cookie_low,
676                                   buf, len);
677 }