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