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);
196 #define RLIM_INFINITY32 0x7fffffff
197 #define RESOURCE32(x) ((x > RLIM_INFINITY32) ? RLIM_INFINITY32 : x)
205 asmlinkage long sys32_truncate64(const char __user * path, unsigned long __dummy,
206 int length_hi, int length_lo)
209 asmlinkage long sys32_truncate64(const char __user * path, unsigned long __dummy,
210 int length_lo, int length_hi)
215 length = ((unsigned long) length_hi << 32) | (unsigned int) length_lo;
217 return sys_truncate(path, length);
221 asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long __dummy,
222 int length_hi, int length_lo)
225 asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long __dummy,
226 int length_lo, int length_hi)
231 length = ((unsigned long) length_hi << 32) | (unsigned int) length_lo;
233 return sys_ftruncate(fd, length);
237 get_tv32(struct timeval *o, struct compat_timeval __user *i)
239 return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
240 (__get_user(o->tv_sec, &i->tv_sec) |
241 __get_user(o->tv_usec, &i->tv_usec)));
245 put_tv32(struct compat_timeval __user *o, struct timeval *i)
247 return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
248 (__put_user(i->tv_sec, &o->tv_sec) |
249 __put_user(i->tv_usec, &o->tv_usec)));
252 extern struct timezone sys_tz;
255 sys32_gettimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
259 do_gettimeofday(&ktv);
260 if (put_tv32(tv, &ktv))
264 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
270 static inline long get_ts32(struct timespec *o, struct compat_timeval __user *i)
274 if (!access_ok(VERIFY_READ, i, sizeof(*i)))
276 if (__get_user(o->tv_sec, &i->tv_sec))
278 if (__get_user(usec, &i->tv_usec))
280 o->tv_nsec = usec * 1000;
285 sys32_settimeofday(struct compat_timeval __user *tv, struct timezone __user *tz)
291 if (get_ts32(&kts, tv))
295 if (copy_from_user(&ktz, tz, sizeof(ktz)))
299 return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
302 asmlinkage int sys32_llseek(unsigned int fd, unsigned int offset_high,
303 unsigned int offset_low, loff_t __user * result,
306 return sys_llseek(fd, offset_high, offset_low, result, origin);
309 /* From the Single Unix Spec: pread & pwrite act like lseek to pos + op +
310 lseek back to original location. They fail just like lseek does on
311 non-seekable files. */
313 asmlinkage ssize_t sys32_pread(unsigned int fd, char __user * buf,
314 size_t count, u32 unused, u64 a4, u64 a5)
316 return sys_pread64(fd, buf, count, merge_64(a4, a5));
319 asmlinkage ssize_t sys32_pwrite(unsigned int fd, const char __user * buf,
320 size_t count, u32 unused, u64 a4, u64 a5)
322 return sys_pwrite64(fd, buf, count, merge_64(a4, a5));
325 asmlinkage int sys32_sched_rr_get_interval(compat_pid_t pid,
326 struct compat_timespec __user *interval)
330 mm_segment_t old_fs = get_fs ();
333 ret = sys_sched_rr_get_interval(pid, (struct timespec __user *)&t);
335 if (put_user (t.tv_sec, &interval->tv_sec) ||
336 __put_user (t.tv_nsec, &interval->tv_nsec))
342 sys32_ipc (u32 call, int first, int second, int third, u32 ptr, u32 fifth)
346 version = call >> 16; /* hack for backward compatibility */
351 /* struct sembuf is the same on 32 and 64bit :)) */
352 err = sys_semtimedop(first, compat_ptr(ptr), second, NULL);
355 err = compat_sys_semtimedop(first, compat_ptr(ptr), second,
359 err = sys_semget(first, second, third);
362 err = compat_sys_semctl(first, second, third, compat_ptr(ptr));
365 err = compat_sys_msgsnd(first, second, third, compat_ptr(ptr));
368 err = compat_sys_msgrcv(first, second, fifth, third,
369 version, compat_ptr(ptr));
372 err = sys_msgget((key_t) first, second);
375 err = compat_sys_msgctl(first, second, compat_ptr(ptr));
378 err = compat_sys_shmat(first, second, third, version,
382 err = sys_shmdt(compat_ptr(ptr));
385 err = sys_shmget(first, (unsigned)second, third);
388 err = compat_sys_shmctl(first, second, compat_ptr(ptr));
398 #ifdef CONFIG_MIPS32_N32
399 asmlinkage long sysn32_semctl(int semid, int semnum, int cmd, u32 arg)
401 /* compat_sys_semctl expects a pointer to union semun */
402 u32 __user *uptr = compat_alloc_user_space(sizeof(u32));
403 if (put_user(arg, uptr))
405 return compat_sys_semctl(semid, semnum, cmd, uptr);
408 asmlinkage long sysn32_msgsnd(int msqid, u32 msgp, unsigned msgsz, int msgflg)
410 return compat_sys_msgsnd(msqid, msgsz, msgflg, compat_ptr(msgp));
413 asmlinkage long sysn32_msgrcv(int msqid, u32 msgp, size_t msgsz, int msgtyp,
416 return compat_sys_msgrcv(msqid, msgsz, msgtyp, msgflg, IPC_64,
425 compat_caddr_t oldval;
426 compat_caddr_t oldlenp;
427 compat_caddr_t newval;
428 compat_size_t newlen;
429 unsigned int __unused[4];
432 #ifdef CONFIG_SYSCTL_SYSCALL
434 asmlinkage long sys32_sysctl(struct sysctl_args32 __user *args)
436 struct sysctl_args32 tmp;
439 size_t __user *oldlenp = NULL;
440 unsigned long addr = (((unsigned long)&args->__unused[0]) + 7) & ~7;
442 if (copy_from_user(&tmp, args, sizeof(tmp)))
445 if (tmp.oldval && tmp.oldlenp) {
446 /* Duh, this is ugly and might not work if sysctl_args
447 is in read-only memory, but do_sysctl does indirectly
448 a lot of uaccess in both directions and we'd have to
449 basically copy the whole sysctl.c here, and
450 glibc's __sysctl uses rw memory for the structure
452 if (get_user(oldlen, (u32 __user *)A(tmp.oldlenp)) ||
453 put_user(oldlen, (size_t __user *)addr))
455 oldlenp = (size_t __user *)addr;
459 error = do_sysctl((int __user *)A(tmp.name), tmp.nlen, (void __user *)A(tmp.oldval),
460 oldlenp, (void __user *)A(tmp.newval), tmp.newlen);
464 if (get_user(oldlen, (size_t __user *)addr) ||
465 put_user(oldlen, (u32 __user *)A(tmp.oldlenp)))
468 copy_to_user(args->__unused, tmp.__unused, sizeof(tmp.__unused));
473 #endif /* CONFIG_SYSCTL_SYSCALL */
475 asmlinkage long sys32_newuname(struct new_utsname __user * name)
480 if (copy_to_user(name, utsname(), sizeof *name))
484 if (current->personality == PER_LINUX32 && !ret)
485 if (copy_to_user(name->machine, "mips\0\0\0", 8))
491 asmlinkage int sys32_personality(unsigned long personality)
494 personality &= 0xffffffff;
495 if (personality(current->personality) == PER_LINUX32 &&
496 personality == PER_LINUX)
497 personality = PER_LINUX32;
498 ret = sys_personality(personality);
499 if (ret == PER_LINUX32)
504 /* ustat compatibility */
506 compat_daddr_t f_tfree;
507 compat_ino_t f_tinode;
512 extern asmlinkage long sys_ustat(dev_t dev, struct ustat __user * ubuf);
514 asmlinkage int sys32_ustat(dev_t dev, struct ustat32 __user * ubuf32)
518 struct ustat32 tmp32;
519 mm_segment_t old_fs = get_fs();
522 err = sys_ustat(dev, (struct ustat __user *)&tmp);
528 memset(&tmp32,0,sizeof(struct ustat32));
529 tmp32.f_tfree = tmp.f_tfree;
530 tmp32.f_tinode = tmp.f_tinode;
532 err = copy_to_user(ubuf32,&tmp32,sizeof(struct ustat32)) ? -EFAULT : 0;
538 asmlinkage int sys32_sendfile(int out_fd, int in_fd, compat_off_t __user *offset,
541 mm_segment_t old_fs = get_fs();
545 if (offset && get_user(of, offset))
549 ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *)&of : NULL, count);
552 if (offset && put_user(of, offset))
558 asmlinkage ssize_t sys32_readahead(int fd, u32 pad0, u64 a2, u64 a3,
561 return sys_readahead(fd, merge_64(a2, a3), count);
564 asmlinkage long sys32_sync_file_range(int fd, int __pad,
565 unsigned long a2, unsigned long a3,
566 unsigned long a4, unsigned long a5,
569 return sys_sync_file_range(fd,
570 merge_64(a2, a3), merge_64(a4, a5),
574 /* Argument list sizes for sys_socketcall */
575 #define AL(x) ((x) * sizeof(unsigned int))
576 static unsigned char socketcall_nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
577 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
578 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
582 * System call vectors.
584 * Argument checking cleaned up. Saved 20% in size.
585 * This function doesn't need to set the kernel lock because
586 * it is set by the callees.
589 asmlinkage long sys32_socketcall(int call, unsigned int __user *args32)
595 extern asmlinkage long sys_socket(int family, int type, int protocol);
596 extern asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen);
597 extern asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen);
598 extern asmlinkage long sys_listen(int fd, int backlog);
599 extern asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int __user *upeer_addrlen);
600 extern asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len);
601 extern asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len);
602 extern asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *usockvec);
603 extern asmlinkage long sys_send(int fd, void __user * buff, size_t len, unsigned flags);
604 extern asmlinkage long sys_sendto(int fd, void __user * buff, size_t len, unsigned flags,
605 struct sockaddr __user *addr, int addr_len);
606 extern asmlinkage long sys_recv(int fd, void __user * ubuf, size_t size, unsigned flags);
607 extern asmlinkage long sys_recvfrom(int fd, void __user * ubuf, size_t size, unsigned flags,
608 struct sockaddr __user *addr, int __user *addr_len);
609 extern asmlinkage long sys_shutdown(int fd, int how);
610 extern asmlinkage long sys_setsockopt(int fd, int level, int optname, char __user *optval, int optlen);
611 extern asmlinkage long sys_getsockopt(int fd, int level, int optname, char __user *optval, int __user *optlen);
612 extern asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags);
613 extern asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flags);
616 if(call<1||call>SYS_RECVMSG)
619 /* copy_from_user should be SMP safe. */
620 if (copy_from_user(a, args32, socketcall_nargs[call]))
629 err = sys_socket(a0,a1,a[2]);
632 err = sys_bind(a0,(struct sockaddr __user *)A(a1), a[2]);
635 err = sys_connect(a0, (struct sockaddr __user *)A(a1), a[2]);
638 err = sys_listen(a0,a1);
641 err = sys_accept(a0,(struct sockaddr __user *)A(a1), (int __user *)A(a[2]));
643 case SYS_GETSOCKNAME:
644 err = sys_getsockname(a0,(struct sockaddr __user *)A(a1), (int __user *)A(a[2]));
646 case SYS_GETPEERNAME:
647 err = sys_getpeername(a0, (struct sockaddr __user *)A(a1), (int __user *)A(a[2]));
650 err = sys_socketpair(a0,a1, a[2], (int __user *)A(a[3]));
653 err = sys_send(a0, (void __user *)A(a1), a[2], a[3]);
656 err = sys_sendto(a0,(void __user *)A(a1), a[2], a[3],
657 (struct sockaddr __user *)A(a[4]), a[5]);
660 err = sys_recv(a0, (void __user *)A(a1), a[2], a[3]);
663 err = sys_recvfrom(a0, (void __user *)A(a1), a[2], a[3],
664 (struct sockaddr __user *)A(a[4]), (int __user *)A(a[5]));
667 err = sys_shutdown(a0,a1);
670 err = sys_setsockopt(a0, a1, a[2], (char __user *)A(a[3]), a[4]);
673 err = sys_getsockopt(a0, a1, a[2], (char __user *)A(a[3]), (int __user *)A(a[4]));
676 err = sys_sendmsg(a0, (struct msghdr __user *) A(a1), a[2]);
679 err = sys_recvmsg(a0, (struct msghdr __user *) A(a1), a[2]);
692 u32 payload[(64 / 4) - 3];
695 extern asmlinkage long
696 sys_timer_create(clockid_t which_clock,
697 struct sigevent __user *timer_event_spec,
698 timer_t __user * created_timer_id);
701 sys32_timer_create(u32 clock, struct sigevent32 __user *se32, timer_t __user *timer_id)
703 struct sigevent __user *p = NULL;
706 p = compat_alloc_user_space(sizeof(struct sigevent));
707 memset(&se, 0, sizeof(struct sigevent));
708 if (get_user(se.sigev_value.sival_int, &se32->sigev_value) ||
709 __get_user(se.sigev_signo, &se32->sigev_signo) ||
710 __get_user(se.sigev_notify, &se32->sigev_notify) ||
711 __copy_from_user(&se._sigev_un._pad, &se32->payload,
712 sizeof(se32->payload)) ||
713 copy_to_user(p, &se, sizeof(se)))
716 return sys_timer_create(clock, p, timer_id);
719 save_static_function(sys32_clone);
720 __attribute_used__ noinline static int
721 _sys32_clone(nabi_no_regargs struct pt_regs regs)
723 unsigned long clone_flags;
725 int __user *parent_tidptr, *child_tidptr;
727 clone_flags = regs.regs[4];
728 newsp = regs.regs[5];
730 newsp = regs.regs[29];
731 parent_tidptr = (int __user *) regs.regs[6];
733 /* Use __dummy4 instead of getting it off the stack, so that
735 child_tidptr = (int __user *) __dummy4;
736 return do_fork(clone_flags, newsp, ®s, 0,
737 parent_tidptr, child_tidptr);