2 * Conversion between 32-bit and 64-bit native system calls.
4 * Copyright (C) 2000 Silicon Graphics, Inc.
5 * Written by Ulf Carlsson (ulfc@engr.sgi.com)
6 * sys32_execve from ia64/ia32 code, Feb 2000, Kanoj Sarcar (kanoj@sgi.com)
8 #include <linux/compiler.h>
10 #include <linux/errno.h>
11 #include <linux/file.h>
12 #include <linux/smp_lock.h>
13 #include <linux/highuid.h>
14 #include <linux/dirent.h>
15 #include <linux/resource.h>
16 #include <linux/highmem.h>
17 #include <linux/time.h>
18 #include <linux/times.h>
19 #include <linux/poll.h>
20 #include <linux/slab.h>
21 #include <linux/skbuff.h>
22 #include <linux/filter.h>
23 #include <linux/shm.h>
24 #include <linux/sem.h>
25 #include <linux/msg.h>
26 #include <linux/icmpv6.h>
27 #include <linux/syscalls.h>
28 #include <linux/sysctl.h>
29 #include <linux/utime.h>
30 #include <linux/utsname.h>
31 #include <linux/personality.h>
32 #include <linux/dnotify.h>
33 #include <linux/module.h>
34 #include <linux/binfmts.h>
35 #include <linux/security.h>
36 #include <linux/compat.h>
37 #include <linux/vfs.h>
44 #include <asm/uaccess.h>
45 #include <asm/mmu_context.h>
48 /* Use this to get at 32-bit user passed pointers. */
49 /* A() macro should be used for places where you e.g.
50 have some internal variable u32 and just want to get
51 rid of a compiler warning. AA() has to be used in
52 places where you want to convert a function argument
53 to 32bit pointer or when you e.g. access pt_regs
54 structure and want to consider 32bit registers only.
56 #define A(__x) ((unsigned long)(__x))
57 #define AA(__x) ((unsigned long)((int)__x))
60 #define merge_64(r1,r2) ((((r1) & 0xffffffffUL) << 32) + ((r2) & 0xffffffffUL))
63 #define merge_64(r1,r2) ((((r2) & 0xffffffffUL) << 32) + ((r1) & 0xffffffffUL))
67 * Revalidate the inode. This is required for proper NFS attribute caching.
70 int cp_compat_stat(struct kstat *stat, struct compat_stat __user *statbuf)
72 struct compat_stat tmp;
74 if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev))
77 memset(&tmp, 0, sizeof(tmp));
78 tmp.st_dev = new_encode_dev(stat->dev);
79 tmp.st_ino = stat->ino;
80 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
82 tmp.st_mode = stat->mode;
83 tmp.st_nlink = stat->nlink;
84 SET_UID(tmp.st_uid, stat->uid);
85 SET_GID(tmp.st_gid, stat->gid);
86 tmp.st_rdev = new_encode_dev(stat->rdev);
87 tmp.st_size = stat->size;
88 tmp.st_atime = stat->atime.tv_sec;
89 tmp.st_mtime = stat->mtime.tv_sec;
90 tmp.st_ctime = stat->ctime.tv_sec;
92 tmp.st_atime_nsec = stat->atime.tv_nsec;
93 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
94 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
96 tmp.st_blocks = stat->blocks;
97 tmp.st_blksize = stat->blksize;
98 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
101 asmlinkage unsigned long
102 sys32_mmap2(unsigned long addr, unsigned long len, unsigned long prot,
103 unsigned long flags, unsigned long fd, unsigned long pgoff)
105 struct file * file = NULL;
109 if (pgoff & (~PAGE_MASK >> 12))
111 pgoff >>= PAGE_SHIFT-12;
113 if (!(flags & MAP_ANONYMOUS)) {
119 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
121 down_write(¤t->mm->mmap_sem);
122 error = do_mmap_pgoff(file, addr, len, prot, flags, pgoff);
123 up_write(¤t->mm->mmap_sem);
132 asmlinkage int sys_truncate64(const char __user *path, unsigned int high,
137 return sys_truncate(path, ((long) high << 32) | low);
140 asmlinkage int sys_ftruncate64(unsigned int fd, unsigned int high,
145 return sys_ftruncate(fd, ((long) high << 32) | low);
149 * sys_execve() executes a new program.
151 asmlinkage int sys32_execve(nabi_no_regargs struct pt_regs regs)
156 filename = getname(compat_ptr(regs.regs[4]));
157 error = PTR_ERR(filename);
158 if (IS_ERR(filename))
160 error = compat_do_execve(filename, compat_ptr(regs.regs[5]),
161 compat_ptr(regs.regs[6]), ®s);
169 sysn32_waitid(int which, compat_pid_t pid,
170 siginfo_t __user *uinfo, int options,
171 struct compat_rusage __user *uru)
175 mm_segment_t old_fs = get_fs();
178 if (!access_ok(VERIFY_WRITE, uinfo, sizeof(*uinfo)))
182 ret = sys_waitid(which, pid, uinfo, options,
183 uru ? (struct rusage __user *) &ru : NULL);
186 if (__get_user(si_signo, &uinfo->si_signo))
188 if (ret < 0 || si_signo == 0)
192 ret = put_compat_rusage(&ru, uru);
212 asmlinkage int sys32_sysinfo(struct sysinfo32 __user *info)
216 mm_segment_t old_fs = get_fs ();
219 ret = sys_sysinfo((struct sysinfo __user *)&s);
221 err = put_user (s.uptime, &info->uptime);
222 err |= __put_user (s.loads[0], &info->loads[0]);
223 err |= __put_user (s.loads[1], &info->loads[1]);
224 err |= __put_user (s.loads[2], &info->loads[2]);
225 err |= __put_user (s.totalram, &info->totalram);
226 err |= __put_user (s.freeram, &info->freeram);
227 err |= __put_user (s.sharedram, &info->sharedram);
228 err |= __put_user (s.bufferram, &info->bufferram);
229 err |= __put_user (s.totalswap, &info->totalswap);
230 err |= __put_user (s.freeswap, &info->freeswap);
231 err |= __put_user (s.procs, &info->procs);
232 err |= __put_user (s.totalhigh, &info->totalhigh);
233 err |= __put_user (s.freehigh, &info->freehigh);
234 err |= __put_user (s.mem_unit, &info->mem_unit);
240 #define RLIM_INFINITY32 0x7fffffff
241 #define RESOURCE32(x) ((x > RLIM_INFINITY32) ? RLIM_INFINITY32 : x)
249 asmlinkage long sys32_truncate64(const char __user * path, unsigned long __dummy,
250 int length_hi, int length_lo)
253 asmlinkage long sys32_truncate64(const char __user * path, unsigned long __dummy,
254 int length_lo, int length_hi)
259 length = ((unsigned long) length_hi << 32) | (unsigned int) length_lo;
261 return sys_truncate(path, length);
265 asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long __dummy,
266 int length_hi, int length_lo)
269 asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long __dummy,
270 int length_lo, int length_hi)
275 length = ((unsigned long) length_hi << 32) | (unsigned int) length_lo;
277 return sys_ftruncate(fd, length);
281 get_tv32(struct timeval *o, struct compat_timeval __user *i)
283 return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
284 (__get_user(o->tv_sec, &i->tv_sec) |
285 __get_user(o->tv_usec, &i->tv_usec)));
289 put_tv32(struct compat_timeval __user *o, struct timeval *i)
291 return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
292 (__put_user(i->tv_sec, &o->tv_sec) |
293 __put_user(i->tv_usec, &o->tv_usec)));
296 extern struct timezone sys_tz;
299 sys32_gettimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
303 do_gettimeofday(&ktv);
304 if (put_tv32(tv, &ktv))
308 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
314 static inline long get_ts32(struct timespec *o, struct compat_timeval __user *i)
318 if (!access_ok(VERIFY_READ, i, sizeof(*i)))
320 if (__get_user(o->tv_sec, &i->tv_sec))
322 if (__get_user(usec, &i->tv_usec))
324 o->tv_nsec = usec * 1000;
329 sys32_settimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
335 if (get_ts32(&kts, tv))
339 if (copy_from_user(&ktz, tz, sizeof(ktz)))
343 return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
346 asmlinkage int sys32_llseek(unsigned int fd, unsigned int offset_high,
347 unsigned int offset_low, loff_t __user * result,
350 return sys_llseek(fd, offset_high, offset_low, result, origin);
353 /* From the Single Unix Spec: pread & pwrite act like lseek to pos + op +
354 lseek back to original location. They fail just like lseek does on
355 non-seekable files. */
357 asmlinkage ssize_t sys32_pread(unsigned int fd, char __user * buf,
358 size_t count, u32 unused, u64 a4, u64 a5)
360 return sys_pread64(fd, buf, count, merge_64(a4, a5));
363 asmlinkage ssize_t sys32_pwrite(unsigned int fd, const char __user * buf,
364 size_t count, u32 unused, u64 a4, u64 a5)
366 return sys_pwrite64(fd, buf, count, merge_64(a4, a5));
369 asmlinkage int sys32_sched_rr_get_interval(compat_pid_t pid,
370 struct compat_timespec __user *interval)
374 mm_segment_t old_fs = get_fs ();
377 ret = sys_sched_rr_get_interval(pid, (struct timespec __user *)&t);
379 if (put_user (t.tv_sec, &interval->tv_sec) ||
380 __put_user (t.tv_nsec, &interval->tv_nsec))
386 sys32_ipc (u32 call, int first, int second, int third, u32 ptr, u32 fifth)
390 version = call >> 16; /* hack for backward compatibility */
395 /* struct sembuf is the same on 32 and 64bit :)) */
396 err = sys_semtimedop(first, compat_ptr(ptr), second, NULL);
399 err = compat_sys_semtimedop(first, compat_ptr(ptr), second,
403 err = sys_semget(first, second, third);
406 err = compat_sys_semctl(first, second, third, compat_ptr(ptr));
409 err = compat_sys_msgsnd(first, second, third, compat_ptr(ptr));
412 err = compat_sys_msgrcv(first, second, fifth, third,
413 version, compat_ptr(ptr));
416 err = sys_msgget((key_t) first, second);
419 err = compat_sys_msgctl(first, second, compat_ptr(ptr));
422 err = compat_sys_shmat(first, second, third, version,
426 err = sys_shmdt(compat_ptr(ptr));
429 err = sys_shmget(first, (unsigned)second, third);
432 err = compat_sys_shmctl(first, second, compat_ptr(ptr));
442 #ifdef CONFIG_MIPS32_N32
443 asmlinkage long sysn32_semctl(int semid, int semnum, int cmd, u32 arg)
445 /* compat_sys_semctl expects a pointer to union semun */
446 u32 __user *uptr = compat_alloc_user_space(sizeof(u32));
447 if (put_user(arg, uptr))
449 return compat_sys_semctl(semid, semnum, cmd, uptr);
452 asmlinkage long sysn32_msgsnd(int msqid, u32 msgp, unsigned msgsz, int msgflg)
454 return compat_sys_msgsnd(msqid, msgsz, msgflg, compat_ptr(msgp));
457 asmlinkage long sysn32_msgrcv(int msqid, u32 msgp, size_t msgsz, int msgtyp,
460 return compat_sys_msgrcv(msqid, msgsz, msgtyp, msgflg, IPC_64,
469 compat_caddr_t oldval;
470 compat_caddr_t oldlenp;
471 compat_caddr_t newval;
472 compat_size_t newlen;
473 unsigned int __unused[4];
476 #ifdef CONFIG_SYSCTL_SYSCALL
478 asmlinkage long sys32_sysctl(struct sysctl_args32 __user *args)
480 struct sysctl_args32 tmp;
483 size_t __user *oldlenp = NULL;
484 unsigned long addr = (((unsigned long)&args->__unused[0]) + 7) & ~7;
486 if (copy_from_user(&tmp, args, sizeof(tmp)))
489 if (tmp.oldval && tmp.oldlenp) {
490 /* Duh, this is ugly and might not work if sysctl_args
491 is in read-only memory, but do_sysctl does indirectly
492 a lot of uaccess in both directions and we'd have to
493 basically copy the whole sysctl.c here, and
494 glibc's __sysctl uses rw memory for the structure
496 if (get_user(oldlen, (u32 __user *)A(tmp.oldlenp)) ||
497 put_user(oldlen, (size_t __user *)addr))
499 oldlenp = (size_t __user *)addr;
503 error = do_sysctl((int __user *)A(tmp.name), tmp.nlen, (void __user *)A(tmp.oldval),
504 oldlenp, (void __user *)A(tmp.newval), tmp.newlen);
508 if (get_user(oldlen, (size_t __user *)addr) ||
509 put_user(oldlen, (u32 __user *)A(tmp.oldlenp)))
512 copy_to_user(args->__unused, tmp.__unused, sizeof(tmp.__unused));
517 #endif /* CONFIG_SYSCTL_SYSCALL */
519 asmlinkage long sys32_newuname(struct new_utsname __user * name)
524 if (copy_to_user(name, utsname(), sizeof *name))
528 if (current->personality == PER_LINUX32 && !ret)
529 if (copy_to_user(name->machine, "mips\0\0\0", 8))
535 asmlinkage int sys32_personality(unsigned long personality)
538 personality &= 0xffffffff;
539 if (personality(current->personality) == PER_LINUX32 &&
540 personality == PER_LINUX)
541 personality = PER_LINUX32;
542 ret = sys_personality(personality);
543 if (ret == PER_LINUX32)
548 /* ustat compatibility */
550 compat_daddr_t f_tfree;
551 compat_ino_t f_tinode;
556 extern asmlinkage long sys_ustat(dev_t dev, struct ustat __user * ubuf);
558 asmlinkage int sys32_ustat(dev_t dev, struct ustat32 __user * ubuf32)
562 struct ustat32 tmp32;
563 mm_segment_t old_fs = get_fs();
566 err = sys_ustat(dev, (struct ustat __user *)&tmp);
572 memset(&tmp32,0,sizeof(struct ustat32));
573 tmp32.f_tfree = tmp.f_tfree;
574 tmp32.f_tinode = tmp.f_tinode;
576 err = copy_to_user(ubuf32,&tmp32,sizeof(struct ustat32)) ? -EFAULT : 0;
582 asmlinkage int sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset,
585 mm_segment_t old_fs = get_fs();
589 if (offset && get_user(of, offset))
593 ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *)&of : NULL, count);
596 if (offset && put_user(of, offset))
602 asmlinkage ssize_t sys32_readahead(int fd, u32 pad0, u64 a2, u64 a3,
605 return sys_readahead(fd, merge_64(a2, a3), count);
608 asmlinkage long sys32_sync_file_range(int fd, int __pad,
609 unsigned long a2, unsigned long a3,
610 unsigned long a4, unsigned long a5,
613 return sys_sync_file_range(fd,
614 merge_64(a2, a3), merge_64(a4, a5),
618 /* Argument list sizes for sys_socketcall */
619 #define AL(x) ((x) * sizeof(unsigned int))
620 static unsigned char socketcall_nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
621 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
622 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
626 * System call vectors.
628 * Argument checking cleaned up. Saved 20% in size.
629 * This function doesn't need to set the kernel lock because
630 * it is set by the callees.
633 asmlinkage long sys32_socketcall(int call, unsigned int __user *args32)
639 extern asmlinkage long sys_socket(int family, int type, int protocol);
640 extern asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen);
641 extern asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen);
642 extern asmlinkage long sys_listen(int fd, int backlog);
643 extern asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int __user *upeer_addrlen);
644 extern asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len);
645 extern asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len);
646 extern asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *usockvec);
647 extern asmlinkage long sys_send(int fd, void __user * buff, size_t len, unsigned flags);
648 extern asmlinkage long sys_sendto(int fd, void __user * buff, size_t len, unsigned flags,
649 struct sockaddr __user *addr, int addr_len);
650 extern asmlinkage long sys_recv(int fd, void __user * ubuf, size_t size, unsigned flags);
651 extern asmlinkage long sys_recvfrom(int fd, void __user * ubuf, size_t size, unsigned flags,
652 struct sockaddr __user *addr, int __user *addr_len);
653 extern asmlinkage long sys_shutdown(int fd, int how);
654 extern asmlinkage long sys_setsockopt(int fd, int level, int optname, char __user *optval, int optlen);
655 extern asmlinkage long sys_getsockopt(int fd, int level, int optname, char __user *optval, int __user *optlen);
656 extern asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags);
657 extern asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flags);
660 if(call<1||call>SYS_RECVMSG)
663 /* copy_from_user should be SMP safe. */
664 if (copy_from_user(a, args32, socketcall_nargs[call]))
673 err = sys_socket(a0,a1,a[2]);
676 err = sys_bind(a0,(struct sockaddr __user *)A(a1), a[2]);
679 err = sys_connect(a0, (struct sockaddr __user *)A(a1), a[2]);
682 err = sys_listen(a0,a1);
685 err = sys_accept(a0,(struct sockaddr __user *)A(a1), (int __user *)A(a[2]));
687 case SYS_GETSOCKNAME:
688 err = sys_getsockname(a0,(struct sockaddr __user *)A(a1), (int __user *)A(a[2]));
690 case SYS_GETPEERNAME:
691 err = sys_getpeername(a0, (struct sockaddr __user *)A(a1), (int __user *)A(a[2]));
694 err = sys_socketpair(a0,a1, a[2], (int __user *)A(a[3]));
697 err = sys_send(a0, (void __user *)A(a1), a[2], a[3]);
700 err = sys_sendto(a0,(void __user *)A(a1), a[2], a[3],
701 (struct sockaddr __user *)A(a[4]), a[5]);
704 err = sys_recv(a0, (void __user *)A(a1), a[2], a[3]);
707 err = sys_recvfrom(a0, (void __user *)A(a1), a[2], a[3],
708 (struct sockaddr __user *)A(a[4]), (int __user *)A(a[5]));
711 err = sys_shutdown(a0,a1);
714 err = sys_setsockopt(a0, a1, a[2], (char __user *)A(a[3]), a[4]);
717 err = sys_getsockopt(a0, a1, a[2], (char __user *)A(a[3]), (int __user *)A(a[4]));
720 err = sys_sendmsg(a0, (struct msghdr __user *) A(a1), a[2]);
723 err = sys_recvmsg(a0, (struct msghdr __user *) A(a1), a[2]);
736 u32 payload[(64 / 4) - 3];
739 extern asmlinkage long
740 sys_timer_create(clockid_t which_clock,
741 struct sigevent __user *timer_event_spec,
742 timer_t __user * created_timer_id);
745 sys32_timer_create(u32 clock, struct sigevent32 __user *se32, timer_t __user *timer_id)
747 struct sigevent __user *p = NULL;
750 p = compat_alloc_user_space(sizeof(struct sigevent));
751 memset(&se, 0, sizeof(struct sigevent));
752 if (get_user(se.sigev_value.sival_int, &se32->sigev_value) ||
753 __get_user(se.sigev_signo, &se32->sigev_signo) ||
754 __get_user(se.sigev_notify, &se32->sigev_notify) ||
755 __copy_from_user(&se._sigev_un._pad, &se32->payload,
756 sizeof(se32->payload)) ||
757 copy_to_user(p, &se, sizeof(se)))
760 return sys_timer_create(clock, p, timer_id);
763 save_static_function(sys32_clone);
764 __attribute_used__ noinline static int
765 _sys32_clone(nabi_no_regargs struct pt_regs regs)
767 unsigned long clone_flags;
769 int __user *parent_tidptr, *child_tidptr;
771 clone_flags = regs.regs[4];
772 newsp = regs.regs[5];
774 newsp = regs.regs[29];
775 parent_tidptr = (int __user *) regs.regs[6];
777 /* Use __dummy4 instead of getting it off the stack, so that
779 child_tidptr = (int __user *) __dummy4;
780 return do_fork(clone_flags, newsp, ®s, 0,
781 parent_tidptr, child_tidptr);