2 * sys_ppc32.c: Conversion between 32bit and 64bit native syscalls.
4 * Copyright (C) 2001 IBM
5 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
8 * These routines maintain argument size conversion between 32bit and 64bit
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
21 #include <linux/file.h>
22 #include <linux/signal.h>
23 #include <linux/resource.h>
24 #include <linux/times.h>
25 #include <linux/utsname.h>
26 #include <linux/smp.h>
27 #include <linux/smp_lock.h>
28 #include <linux/sem.h>
29 #include <linux/msg.h>
30 #include <linux/shm.h>
31 #include <linux/poll.h>
32 #include <linux/personality.h>
33 #include <linux/stat.h>
34 #include <linux/mman.h>
36 #include <linux/syscalls.h>
37 #include <linux/unistd.h>
38 #include <linux/sysctl.h>
39 #include <linux/binfmts.h>
40 #include <linux/security.h>
41 #include <linux/compat.h>
42 #include <linux/ptrace.h>
43 #include <linux/elf.h>
44 #include <linux/ipc.h>
46 #include <asm/ptrace.h>
47 #include <asm/types.h>
48 #include <asm/uaccess.h>
49 #include <asm/unistd.h>
51 #include <asm/mmu_context.h>
52 #include <asm/ppc-pci.h>
53 #include <asm/syscalls.h>
56 asmlinkage long ppc32_select(u32 n, compat_ulong_t __user *inp,
57 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
61 return compat_sys_select((int)n, inp, outp, exp, compat_ptr(tvp_x));
64 int cp_compat_stat(struct kstat *stat, struct compat_stat __user *statbuf)
69 if (stat->size > MAX_NON_LFS || !new_valid_dev(stat->dev) ||
70 !new_valid_dev(stat->rdev))
74 if (sizeof(ino) < sizeof(stat->ino) && ino != stat->ino)
77 err = access_ok(VERIFY_WRITE, statbuf, sizeof(*statbuf)) ? 0 : -EFAULT;
78 err |= __put_user(new_encode_dev(stat->dev), &statbuf->st_dev);
79 err |= __put_user(ino, &statbuf->st_ino);
80 err |= __put_user(stat->mode, &statbuf->st_mode);
81 err |= __put_user(stat->nlink, &statbuf->st_nlink);
82 err |= __put_user(stat->uid, &statbuf->st_uid);
83 err |= __put_user(stat->gid, &statbuf->st_gid);
84 err |= __put_user(new_encode_dev(stat->rdev), &statbuf->st_rdev);
85 err |= __put_user(stat->size, &statbuf->st_size);
86 err |= __put_user(stat->atime.tv_sec, &statbuf->st_atime);
87 err |= __put_user(stat->atime.tv_nsec, &statbuf->st_atime_nsec);
88 err |= __put_user(stat->mtime.tv_sec, &statbuf->st_mtime);
89 err |= __put_user(stat->mtime.tv_nsec, &statbuf->st_mtime_nsec);
90 err |= __put_user(stat->ctime.tv_sec, &statbuf->st_ctime);
91 err |= __put_user(stat->ctime.tv_nsec, &statbuf->st_ctime_nsec);
92 err |= __put_user(stat->blksize, &statbuf->st_blksize);
93 err |= __put_user(stat->blocks, &statbuf->st_blocks);
94 err |= __put_user(0, &statbuf->__unused4[0]);
95 err |= __put_user(0, &statbuf->__unused4[1]);
100 /* Note: it is necessary to treat option as an unsigned int,
101 * with the corresponding cast to a signed int to insure that the
102 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
103 * and the register representation of a signed int (msr in 64-bit mode) is performed.
105 asmlinkage long compat_sys_sysfs(u32 option, u32 arg1, u32 arg2)
107 return sys_sysfs((int)option, arg1, arg2);
110 static inline long get_ts32(struct timespec *o, struct compat_timeval __user *i)
114 if (!access_ok(VERIFY_READ, i, sizeof(*i)))
116 if (__get_user(o->tv_sec, &i->tv_sec))
118 if (__get_user(usec, &i->tv_usec))
120 o->tv_nsec = usec * 1000;
124 static inline long put_tv32(struct compat_timeval __user *o, struct timeval *i)
126 return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
127 (__put_user(i->tv_sec, &o->tv_sec) |
128 __put_user(i->tv_usec, &o->tv_usec)));
134 /* Translations due to time_t size differences. Which affects all
135 sorts of things, like timeval and itimerval. */
136 extern struct timezone sys_tz;
138 asmlinkage long compat_sys_gettimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
142 do_gettimeofday(&ktv);
143 if (put_tv32(tv, &ktv))
147 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
156 asmlinkage long compat_sys_settimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
162 if (get_ts32(&kts, tv))
166 if (copy_from_user(&ktz, tz, sizeof(ktz)))
170 return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
173 #ifdef CONFIG_SYSVIPC
174 long compat_sys_ipc(u32 call, u32 first, u32 second, u32 third, compat_uptr_t ptr,
179 version = call >> 16; /* hack for backward compatibility */
186 /* sign extend semid */
187 return compat_sys_semtimedop((int)first,
188 compat_ptr(ptr), second,
190 /* else fall through for normal semop() */
192 /* struct sembuf is the same on 32 and 64bit :)) */
193 /* sign extend semid */
194 return sys_semtimedop((int)first, compat_ptr(ptr), second,
197 /* sign extend key, nsems */
198 return sys_semget((int)first, (int)second, third);
200 /* sign extend semid, semnum */
201 return compat_sys_semctl((int)first, (int)second, third,
205 /* sign extend msqid */
206 return compat_sys_msgsnd((int)first, (int)second, third,
209 /* sign extend msqid, msgtyp */
210 return compat_sys_msgrcv((int)first, second, (int)fifth,
211 third, version, compat_ptr(ptr));
213 /* sign extend key */
214 return sys_msgget((int)first, second);
216 /* sign extend msqid */
217 return compat_sys_msgctl((int)first, second, compat_ptr(ptr));
220 /* sign extend shmid */
221 return compat_sys_shmat((int)first, second, third, version,
224 return sys_shmdt(compat_ptr(ptr));
226 /* sign extend key_t */
227 return sys_shmget((int)first, second, third);
229 /* sign extend shmid */
230 return compat_sys_shmctl((int)first, second, compat_ptr(ptr));
240 /* Note: it is necessary to treat out_fd and in_fd as unsigned ints,
241 * with the corresponding cast to a signed int to insure that the
242 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
243 * and the register representation of a signed int (msr in 64-bit mode) is performed.
245 asmlinkage long compat_sys_sendfile(u32 out_fd, u32 in_fd, compat_off_t __user * offset, u32 count)
247 mm_segment_t old_fs = get_fs();
252 if (offset && get_user(of, offset))
255 /* The __user pointer cast is valid because of the set_fs() */
257 up = offset ? (off_t __user *) &of : NULL;
258 ret = sys_sendfile((int)out_fd, (int)in_fd, up, count);
261 if (offset && put_user(of, offset))
267 asmlinkage int compat_sys_sendfile64(int out_fd, int in_fd, compat_loff_t __user *offset, s32 count)
269 mm_segment_t old_fs = get_fs();
274 if (offset && get_user(lof, offset))
277 /* The __user pointer cast is valid because of the set_fs() */
279 up = offset ? (loff_t __user *) &lof : NULL;
280 ret = sys_sendfile64(out_fd, in_fd, up, count);
283 if (offset && put_user(lof, offset))
289 long compat_sys_execve(unsigned long a0, unsigned long a1, unsigned long a2,
290 unsigned long a3, unsigned long a4, unsigned long a5,
291 struct pt_regs *regs)
296 filename = getname((char __user *) a0);
297 error = PTR_ERR(filename);
298 if (IS_ERR(filename))
300 flush_fp_to_thread(current);
301 flush_altivec_to_thread(current);
303 error = compat_do_execve(filename, compat_ptr(a1), compat_ptr(a2), regs);
311 /* Note: it is necessary to treat option as an unsigned int,
312 * with the corresponding cast to a signed int to insure that the
313 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
314 * and the register representation of a signed int (msr in 64-bit mode) is performed.
316 asmlinkage long compat_sys_prctl(u32 option, u32 arg2, u32 arg3, u32 arg4, u32 arg5)
318 return sys_prctl((int)option,
319 (unsigned long) arg2,
320 (unsigned long) arg3,
321 (unsigned long) arg4,
322 (unsigned long) arg5);
325 /* Note: it is necessary to treat pid as an unsigned int,
326 * with the corresponding cast to a signed int to insure that the
327 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
328 * and the register representation of a signed int (msr in 64-bit mode) is performed.
330 asmlinkage long compat_sys_sched_rr_get_interval(u32 pid, struct compat_timespec __user *interval)
334 mm_segment_t old_fs = get_fs ();
336 /* The __user pointer cast is valid because of the set_fs() */
338 ret = sys_sched_rr_get_interval((int)pid, (struct timespec __user *) &t);
340 if (put_compat_timespec(&t, interval))
345 /* Note: it is necessary to treat mode as an unsigned int,
346 * with the corresponding cast to a signed int to insure that the
347 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
348 * and the register representation of a signed int (msr in 64-bit mode) is performed.
350 asmlinkage long compat_sys_access(const char __user * filename, u32 mode)
352 return sys_access(filename, (int)mode);
356 /* Note: it is necessary to treat mode as an unsigned int,
357 * with the corresponding cast to a signed int to insure that the
358 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
359 * and the register representation of a signed int (msr in 64-bit mode) is performed.
361 asmlinkage long compat_sys_creat(const char __user * pathname, u32 mode)
363 return sys_creat(pathname, (int)mode);
367 /* Note: it is necessary to treat pid and options as unsigned ints,
368 * with the corresponding cast to a signed int to insure that the
369 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
370 * and the register representation of a signed int (msr in 64-bit mode) is performed.
372 asmlinkage long compat_sys_waitpid(u32 pid, unsigned int __user * stat_addr, u32 options)
374 return sys_waitpid((int)pid, stat_addr, (int)options);
378 /* Note: it is necessary to treat gidsetsize as an unsigned int,
379 * with the corresponding cast to a signed int to insure that the
380 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
381 * and the register representation of a signed int (msr in 64-bit mode) is performed.
383 asmlinkage long compat_sys_getgroups(u32 gidsetsize, gid_t __user *grouplist)
385 return sys_getgroups((int)gidsetsize, grouplist);
389 /* Note: it is necessary to treat pid as an unsigned int,
390 * with the corresponding cast to a signed int to insure that the
391 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
392 * and the register representation of a signed int (msr in 64-bit mode) is performed.
394 asmlinkage long compat_sys_getpgid(u32 pid)
396 return sys_getpgid((int)pid);
401 /* Note: it is necessary to treat pid as an unsigned int,
402 * with the corresponding cast to a signed int to insure that the
403 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
404 * and the register representation of a signed int (msr in 64-bit mode) is performed.
406 asmlinkage long compat_sys_getsid(u32 pid)
408 return sys_getsid((int)pid);
412 /* Note: it is necessary to treat pid and sig as unsigned ints,
413 * with the corresponding cast to a signed int to insure that the
414 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
415 * and the register representation of a signed int (msr in 64-bit mode) is performed.
417 asmlinkage long compat_sys_kill(u32 pid, u32 sig)
419 return sys_kill((int)pid, (int)sig);
423 /* Note: it is necessary to treat mode as an unsigned int,
424 * with the corresponding cast to a signed int to insure that the
425 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
426 * and the register representation of a signed int (msr in 64-bit mode) is performed.
428 asmlinkage long compat_sys_mkdir(const char __user * pathname, u32 mode)
430 return sys_mkdir(pathname, (int)mode);
433 long compat_sys_nice(u32 increment)
435 /* sign extend increment */
436 return sys_nice((int)increment);
439 off_t ppc32_lseek(unsigned int fd, u32 offset, unsigned int origin)
442 return sys_lseek(fd, (int)offset, origin);
445 /* Note: it is necessary to treat bufsiz as an unsigned int,
446 * with the corresponding cast to a signed int to insure that the
447 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
448 * and the register representation of a signed int (msr in 64-bit mode) is performed.
450 asmlinkage long compat_sys_readlink(const char __user * path, char __user * buf, u32 bufsiz)
452 return sys_readlink(path, buf, (int)bufsiz);
455 /* Note: it is necessary to treat option as an unsigned int,
456 * with the corresponding cast to a signed int to insure that the
457 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
458 * and the register representation of a signed int (msr in 64-bit mode) is performed.
460 asmlinkage long compat_sys_sched_get_priority_max(u32 policy)
462 return sys_sched_get_priority_max((int)policy);
466 /* Note: it is necessary to treat policy as an unsigned int,
467 * with the corresponding cast to a signed int to insure that the
468 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
469 * and the register representation of a signed int (msr in 64-bit mode) is performed.
471 asmlinkage long compat_sys_sched_get_priority_min(u32 policy)
473 return sys_sched_get_priority_min((int)policy);
477 /* Note: it is necessary to treat pid as an unsigned int,
478 * with the corresponding cast to a signed int to insure that the
479 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
480 * and the register representation of a signed int (msr in 64-bit mode) is performed.
482 asmlinkage long compat_sys_sched_getparam(u32 pid, struct sched_param __user *param)
484 return sys_sched_getparam((int)pid, param);
488 /* Note: it is necessary to treat pid as an unsigned int,
489 * with the corresponding cast to a signed int to insure that the
490 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
491 * and the register representation of a signed int (msr in 64-bit mode) is performed.
493 asmlinkage long compat_sys_sched_getscheduler(u32 pid)
495 return sys_sched_getscheduler((int)pid);
499 /* Note: it is necessary to treat pid as an unsigned int,
500 * with the corresponding cast to a signed int to insure that the
501 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
502 * and the register representation of a signed int (msr in 64-bit mode) is performed.
504 asmlinkage long compat_sys_sched_setparam(u32 pid, struct sched_param __user *param)
506 return sys_sched_setparam((int)pid, param);
510 /* Note: it is necessary to treat pid and policy as unsigned ints,
511 * with the corresponding cast to a signed int to insure that the
512 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
513 * and the register representation of a signed int (msr in 64-bit mode) is performed.
515 asmlinkage long compat_sys_sched_setscheduler(u32 pid, u32 policy, struct sched_param __user *param)
517 return sys_sched_setscheduler((int)pid, (int)policy, param);
521 /* Note: it is necessary to treat len as an unsigned int,
522 * with the corresponding cast to a signed int to insure that the
523 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
524 * and the register representation of a signed int (msr in 64-bit mode) is performed.
526 asmlinkage long compat_sys_setdomainname(char __user *name, u32 len)
528 return sys_setdomainname(name, (int)len);
532 /* Note: it is necessary to treat gidsetsize as an unsigned int,
533 * with the corresponding cast to a signed int to insure that the
534 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
535 * and the register representation of a signed int (msr in 64-bit mode) is performed.
537 asmlinkage long compat_sys_setgroups(u32 gidsetsize, gid_t __user *grouplist)
539 return sys_setgroups((int)gidsetsize, grouplist);
543 asmlinkage long compat_sys_sethostname(char __user *name, u32 len)
545 /* sign extend len */
546 return sys_sethostname(name, (int)len);
550 /* Note: it is necessary to treat pid and pgid as unsigned ints,
551 * with the corresponding cast to a signed int to insure that the
552 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
553 * and the register representation of a signed int (msr in 64-bit mode) is performed.
555 asmlinkage long compat_sys_setpgid(u32 pid, u32 pgid)
557 return sys_setpgid((int)pid, (int)pgid);
560 long compat_sys_getpriority(u32 which, u32 who)
562 /* sign extend which and who */
563 return sys_getpriority((int)which, (int)who);
566 long compat_sys_setpriority(u32 which, u32 who, u32 niceval)
568 /* sign extend which, who and niceval */
569 return sys_setpriority((int)which, (int)who, (int)niceval);
572 long compat_sys_ioprio_get(u32 which, u32 who)
574 /* sign extend which and who */
575 return sys_ioprio_get((int)which, (int)who);
578 long compat_sys_ioprio_set(u32 which, u32 who, u32 ioprio)
580 /* sign extend which, who and ioprio */
581 return sys_ioprio_set((int)which, (int)who, (int)ioprio);
584 /* Note: it is necessary to treat newmask as an unsigned int,
585 * with the corresponding cast to a signed int to insure that the
586 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
587 * and the register representation of a signed int (msr in 64-bit mode) is performed.
589 asmlinkage long compat_sys_ssetmask(u32 newmask)
591 return sys_ssetmask((int) newmask);
594 asmlinkage long compat_sys_syslog(u32 type, char __user * buf, u32 len)
596 /* sign extend len */
597 return sys_syslog(type, buf, (int)len);
601 /* Note: it is necessary to treat mask as an unsigned int,
602 * with the corresponding cast to a signed int to insure that the
603 * proper conversion (sign extension) between the register representation of a signed int (msr in 32-bit mode)
604 * and the register representation of a signed int (msr in 64-bit mode) is performed.
606 asmlinkage long compat_sys_umask(u32 mask)
608 return sys_umask((int)mask);
611 #ifdef CONFIG_SYSCTL_SYSCALL
612 struct __sysctl_args32 {
622 asmlinkage long compat_sys_sysctl(struct __sysctl_args32 __user *args)
624 struct __sysctl_args32 tmp;
627 size_t __user *oldlenp = NULL;
628 unsigned long addr = (((unsigned long)&args->__unused[0]) + 7) & ~7;
630 if (copy_from_user(&tmp, args, sizeof(tmp)))
633 if (tmp.oldval && tmp.oldlenp) {
634 /* Duh, this is ugly and might not work if sysctl_args
635 is in read-only memory, but do_sysctl does indirectly
636 a lot of uaccess in both directions and we'd have to
637 basically copy the whole sysctl.c here, and
638 glibc's __sysctl uses rw memory for the structure
640 oldlenp = (size_t __user *)addr;
641 if (get_user(oldlen, (compat_size_t __user *)compat_ptr(tmp.oldlenp)) ||
642 put_user(oldlen, oldlenp))
647 error = do_sysctl(compat_ptr(tmp.name), tmp.nlen,
648 compat_ptr(tmp.oldval), oldlenp,
649 compat_ptr(tmp.newval), tmp.newlen);
653 if (get_user(oldlen, oldlenp) ||
654 put_user(oldlen, (compat_size_t __user *)compat_ptr(tmp.oldlenp)))
657 copy_to_user(args->__unused, tmp.__unused, sizeof(tmp.__unused));
663 unsigned long compat_sys_mmap2(unsigned long addr, size_t len,
664 unsigned long prot, unsigned long flags,
665 unsigned long fd, unsigned long pgoff)
667 /* This should remain 12 even if PAGE_SIZE changes */
668 return sys_mmap(addr, len, prot, flags, fd, pgoff << 12);
671 long compat_sys_tgkill(u32 tgid, u32 pid, int sig)
673 /* sign extend tgid, pid */
674 return sys_tgkill((int)tgid, (int)pid, sig);
679 * The 32 bit ABI passes long longs in an odd even register pair.
682 compat_ssize_t compat_sys_pread64(unsigned int fd, char __user *ubuf, compat_size_t count,
683 u32 reg6, u32 poshi, u32 poslo)
685 return sys_pread64(fd, ubuf, count, ((loff_t)poshi << 32) | poslo);
688 compat_ssize_t compat_sys_pwrite64(unsigned int fd, char __user *ubuf, compat_size_t count,
689 u32 reg6, u32 poshi, u32 poslo)
691 return sys_pwrite64(fd, ubuf, count, ((loff_t)poshi << 32) | poslo);
694 compat_ssize_t compat_sys_readahead(int fd, u32 r4, u32 offhi, u32 offlo, u32 count)
696 return sys_readahead(fd, ((loff_t)offhi << 32) | offlo, count);
699 asmlinkage int compat_sys_truncate64(const char __user * path, u32 reg4,
700 unsigned long high, unsigned long low)
702 return sys_truncate(path, (high << 32) | low);
705 asmlinkage long compat_sys_fallocate(int fd, int mode, u32 offhi, u32 offlo,
706 u32 lenhi, u32 lenlo)
708 return sys_fallocate(fd, mode, ((loff_t)offhi << 32) | offlo,
709 ((loff_t)lenhi << 32) | lenlo);
712 asmlinkage int compat_sys_ftruncate64(unsigned int fd, u32 reg4, unsigned long high,
715 return sys_ftruncate(fd, (high << 32) | low);
718 long ppc32_lookup_dcookie(u32 cookie_high, u32 cookie_low, char __user *buf,
721 return sys_lookup_dcookie((u64)cookie_high << 32 | cookie_low,
725 long ppc32_fadvise64(int fd, u32 unused, u32 offset_high, u32 offset_low,
726 size_t len, int advice)
728 return sys_fadvise64(fd, (u64)offset_high << 32 | offset_low, len,
732 asmlinkage long compat_sys_add_key(const char __user *_type,
733 const char __user *_description,
734 const void __user *_payload,
738 return sys_add_key(_type, _description, _payload, plen, ringid);
741 asmlinkage long compat_sys_request_key(const char __user *_type,
742 const char __user *_description,
743 const char __user *_callout_info,
746 return sys_request_key(_type, _description, _callout_info, destringid);
749 asmlinkage long compat_sys_sync_file_range2(int fd, unsigned int flags,
750 unsigned offset_hi, unsigned offset_lo,
751 unsigned nbytes_hi, unsigned nbytes_lo)
753 loff_t offset = ((loff_t)offset_hi << 32) | offset_lo;
754 loff_t nbytes = ((loff_t)nbytes_hi << 32) | nbytes_lo;
756 return sys_sync_file_range(fd, offset, nbytes, flags);