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, union semun 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(ptr_to_compat(arg.__pad), uptr))
449 return compat_sys_semctl(semid, semnum, cmd, uptr);
457 compat_caddr_t oldval;
458 compat_caddr_t oldlenp;
459 compat_caddr_t newval;
460 compat_size_t newlen;
461 unsigned int __unused[4];
464 #ifdef CONFIG_SYSCTL_SYSCALL
466 asmlinkage long sys32_sysctl(struct sysctl_args32 __user *args)
468 struct sysctl_args32 tmp;
471 size_t __user *oldlenp = NULL;
472 unsigned long addr = (((unsigned long)&args->__unused[0]) + 7) & ~7;
474 if (copy_from_user(&tmp, args, sizeof(tmp)))
477 if (tmp.oldval && tmp.oldlenp) {
478 /* Duh, this is ugly and might not work if sysctl_args
479 is in read-only memory, but do_sysctl does indirectly
480 a lot of uaccess in both directions and we'd have to
481 basically copy the whole sysctl.c here, and
482 glibc's __sysctl uses rw memory for the structure
484 if (get_user(oldlen, (u32 __user *)A(tmp.oldlenp)) ||
485 put_user(oldlen, (size_t __user *)addr))
487 oldlenp = (size_t __user *)addr;
491 error = do_sysctl((int __user *)A(tmp.name), tmp.nlen, (void __user *)A(tmp.oldval),
492 oldlenp, (void __user *)A(tmp.newval), tmp.newlen);
496 if (get_user(oldlen, (size_t __user *)addr) ||
497 put_user(oldlen, (u32 __user *)A(tmp.oldlenp)))
500 copy_to_user(args->__unused, tmp.__unused, sizeof(tmp.__unused));
505 #endif /* CONFIG_SYSCTL_SYSCALL */
507 asmlinkage long sys32_newuname(struct new_utsname __user * name)
512 if (copy_to_user(name, utsname(), sizeof *name))
516 if (current->personality == PER_LINUX32 && !ret)
517 if (copy_to_user(name->machine, "mips\0\0\0", 8))
523 asmlinkage int sys32_personality(unsigned long personality)
526 personality &= 0xffffffff;
527 if (personality(current->personality) == PER_LINUX32 &&
528 personality == PER_LINUX)
529 personality = PER_LINUX32;
530 ret = sys_personality(personality);
531 if (ret == PER_LINUX32)
536 /* ustat compatibility */
538 compat_daddr_t f_tfree;
539 compat_ino_t f_tinode;
544 extern asmlinkage long sys_ustat(dev_t dev, struct ustat __user * ubuf);
546 asmlinkage int sys32_ustat(dev_t dev, struct ustat32 __user * ubuf32)
550 struct ustat32 tmp32;
551 mm_segment_t old_fs = get_fs();
554 err = sys_ustat(dev, (struct ustat __user *)&tmp);
560 memset(&tmp32,0,sizeof(struct ustat32));
561 tmp32.f_tfree = tmp.f_tfree;
562 tmp32.f_tinode = tmp.f_tinode;
564 err = copy_to_user(ubuf32,&tmp32,sizeof(struct ustat32)) ? -EFAULT : 0;
570 asmlinkage int sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset,
573 mm_segment_t old_fs = get_fs();
577 if (offset && get_user(of, offset))
581 ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *)&of : NULL, count);
584 if (offset && put_user(of, offset))
590 asmlinkage ssize_t sys32_readahead(int fd, u32 pad0, u64 a2, u64 a3,
593 return sys_readahead(fd, merge_64(a2, a3), count);
596 asmlinkage long sys32_sync_file_range(int fd, int __pad,
597 unsigned long a2, unsigned long a3,
598 unsigned long a4, unsigned long a5,
601 return sys_sync_file_range(fd,
602 merge_64(a2, a3), merge_64(a4, a5),
606 /* Argument list sizes for sys_socketcall */
607 #define AL(x) ((x) * sizeof(unsigned int))
608 static unsigned char socketcall_nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
609 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
610 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
614 * System call vectors.
616 * Argument checking cleaned up. Saved 20% in size.
617 * This function doesn't need to set the kernel lock because
618 * it is set by the callees.
621 asmlinkage long sys32_socketcall(int call, unsigned int __user *args32)
627 extern asmlinkage long sys_socket(int family, int type, int protocol);
628 extern asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen);
629 extern asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen);
630 extern asmlinkage long sys_listen(int fd, int backlog);
631 extern asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int __user *upeer_addrlen);
632 extern asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len);
633 extern asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len);
634 extern asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *usockvec);
635 extern asmlinkage long sys_send(int fd, void __user * buff, size_t len, unsigned flags);
636 extern asmlinkage long sys_sendto(int fd, void __user * buff, size_t len, unsigned flags,
637 struct sockaddr __user *addr, int addr_len);
638 extern asmlinkage long sys_recv(int fd, void __user * ubuf, size_t size, unsigned flags);
639 extern asmlinkage long sys_recvfrom(int fd, void __user * ubuf, size_t size, unsigned flags,
640 struct sockaddr __user *addr, int __user *addr_len);
641 extern asmlinkage long sys_shutdown(int fd, int how);
642 extern asmlinkage long sys_setsockopt(int fd, int level, int optname, char __user *optval, int optlen);
643 extern asmlinkage long sys_getsockopt(int fd, int level, int optname, char __user *optval, int __user *optlen);
644 extern asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags);
645 extern asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flags);
648 if(call<1||call>SYS_RECVMSG)
651 /* copy_from_user should be SMP safe. */
652 if (copy_from_user(a, args32, socketcall_nargs[call]))
661 err = sys_socket(a0,a1,a[2]);
664 err = sys_bind(a0,(struct sockaddr __user *)A(a1), a[2]);
667 err = sys_connect(a0, (struct sockaddr __user *)A(a1), a[2]);
670 err = sys_listen(a0,a1);
673 err = sys_accept(a0,(struct sockaddr __user *)A(a1), (int __user *)A(a[2]));
675 case SYS_GETSOCKNAME:
676 err = sys_getsockname(a0,(struct sockaddr __user *)A(a1), (int __user *)A(a[2]));
678 case SYS_GETPEERNAME:
679 err = sys_getpeername(a0, (struct sockaddr __user *)A(a1), (int __user *)A(a[2]));
682 err = sys_socketpair(a0,a1, a[2], (int __user *)A(a[3]));
685 err = sys_send(a0, (void __user *)A(a1), a[2], a[3]);
688 err = sys_sendto(a0,(void __user *)A(a1), a[2], a[3],
689 (struct sockaddr __user *)A(a[4]), a[5]);
692 err = sys_recv(a0, (void __user *)A(a1), a[2], a[3]);
695 err = sys_recvfrom(a0, (void __user *)A(a1), a[2], a[3],
696 (struct sockaddr __user *)A(a[4]), (int __user *)A(a[5]));
699 err = sys_shutdown(a0,a1);
702 err = sys_setsockopt(a0, a1, a[2], (char __user *)A(a[3]), a[4]);
705 err = sys_getsockopt(a0, a1, a[2], (char __user *)A(a[3]), (int __user *)A(a[4]));
708 err = sys_sendmsg(a0, (struct msghdr __user *) A(a1), a[2]);
711 err = sys_recvmsg(a0, (struct msghdr __user *) A(a1), a[2]);
724 u32 payload[(64 / 4) - 3];
727 extern asmlinkage long
728 sys_timer_create(clockid_t which_clock,
729 struct sigevent __user *timer_event_spec,
730 timer_t __user * created_timer_id);
733 sys32_timer_create(u32 clock, struct sigevent32 __user *se32, timer_t __user *timer_id)
735 struct sigevent __user *p = NULL;
738 p = compat_alloc_user_space(sizeof(struct sigevent));
739 memset(&se, 0, sizeof(struct sigevent));
740 if (get_user(se.sigev_value.sival_int, &se32->sigev_value) ||
741 __get_user(se.sigev_signo, &se32->sigev_signo) ||
742 __get_user(se.sigev_notify, &se32->sigev_notify) ||
743 __copy_from_user(&se._sigev_un._pad, &se32->payload,
744 sizeof(se32->payload)) ||
745 copy_to_user(p, &se, sizeof(se)))
748 return sys_timer_create(clock, p, timer_id);
751 save_static_function(sys32_clone);
752 __attribute_used__ noinline static int
753 _sys32_clone(nabi_no_regargs struct pt_regs regs)
755 unsigned long clone_flags;
757 int __user *parent_tidptr, *child_tidptr;
759 clone_flags = regs.regs[4];
760 newsp = regs.regs[5];
762 newsp = regs.regs[29];
763 parent_tidptr = (int __user *) regs.regs[6];
765 /* Use __dummy4 instead of getting it off the stack, so that
767 child_tidptr = (int __user *) __dummy4;
768 return do_fork(clone_flags, newsp, ®s, 0,
769 parent_tidptr, child_tidptr);