blob: 96ca376bad610ae76943aa104dbdf429bb01b2b8 [file] [log] [blame]
/*
* arch/s390x/kernel/linux32.c
*
* S390 version
* Copyright (C) 2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
* Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
* Gerhard Tonn (ton@de.ibm.com)
*
* Conversion between 31bit and 64bit native syscalls.
*
* Heavily inspired by the 32-bit Sparc compat code which is
* Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
* Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
*
*/
#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/file.h>
#include <linux/signal.h>
#include <linux/utime.h>
#include <linux/resource.h>
#include <linux/times.h>
#include <linux/utsname.h>
#include <linux/timex.h>
#include <linux/smp.h>
#include <linux/smp_lock.h>
#include <linux/sem.h>
#include <linux/msg.h>
#include <linux/shm.h>
#include <linux/slab.h>
#include <linux/uio.h>
#include <linux/nfs_fs.h>
#include <linux/smb_fs.h>
#include <linux/smb_mount.h>
#include <linux/ncp_fs.h>
#include <linux/quota.h>
#include <linux/quotacompat.h>
#include <linux/module.h>
#include <linux/sunrpc/svc.h>
#include <linux/nfsd/nfsd.h>
#include <linux/nfsd/cache.h>
#include <linux/nfsd/xdr.h>
#include <linux/nfsd/syscall.h>
#include <linux/poll.h>
#include <linux/personality.h>
#include <linux/stat.h>
#include <linux/filter.h>
#include <linux/highmem.h>
#include <linux/highuid.h>
#include <linux/mman.h>
#include <linux/ipv6.h>
#include <linux/in.h>
#include <linux/icmpv6.h>
#include <linux/sysctl.h>
#include <asm/types.h>
#include <asm/ipc.h>
#include <asm/uaccess.h>
#include <asm/semaphore.h>
#include <net/scm.h>
#include <net/sock.h>
#include "linux32.h"
extern asmlinkage long sys_chown(const char *, uid_t,gid_t);
extern asmlinkage long sys_lchown(const char *, uid_t,gid_t);
extern asmlinkage long sys_fchown(unsigned int, uid_t,gid_t);
extern asmlinkage long sys_setregid(gid_t, gid_t);
extern asmlinkage long sys_setgid(gid_t);
extern asmlinkage long sys_setreuid(uid_t, uid_t);
extern asmlinkage long sys_setuid(uid_t);
extern asmlinkage long sys_setresuid(uid_t, uid_t, uid_t);
extern asmlinkage long sys_setresgid(gid_t, gid_t, gid_t);
extern asmlinkage long sys_setfsuid(uid_t);
extern asmlinkage long sys_setfsgid(gid_t);
/* For this source file, we want overflow handling. */
#undef high2lowuid
#undef high2lowgid
#undef low2highuid
#undef low2highgid
#undef SET_UID16
#undef SET_GID16
#undef NEW_TO_OLD_UID
#undef NEW_TO_OLD_GID
#undef SET_OLDSTAT_UID
#undef SET_OLDSTAT_GID
#undef SET_STAT_UID
#undef SET_STAT_GID
#define high2lowuid(uid) ((uid) > 65535) ? (u16)overflowuid : (u16)(uid)
#define high2lowgid(gid) ((gid) > 65535) ? (u16)overflowgid : (u16)(gid)
#define low2highuid(uid) ((uid) == (u16)-1) ? (uid_t)-1 : (uid_t)(uid)
#define low2highgid(gid) ((gid) == (u16)-1) ? (gid_t)-1 : (gid_t)(gid)
#define SET_UID16(var, uid) var = high2lowuid(uid)
#define SET_GID16(var, gid) var = high2lowgid(gid)
#define NEW_TO_OLD_UID(uid) high2lowuid(uid)
#define NEW_TO_OLD_GID(gid) high2lowgid(gid)
#define SET_OLDSTAT_UID(stat, uid) (stat).st_uid = high2lowuid(uid)
#define SET_OLDSTAT_GID(stat, gid) (stat).st_gid = high2lowgid(gid)
#define SET_STAT_UID(stat, uid) (stat).st_uid = high2lowuid(uid)
#define SET_STAT_GID(stat, gid) (stat).st_gid = high2lowgid(gid)
asmlinkage long sys32_chown16(const char * filename, u16 user, u16 group)
{
return sys_chown(filename, low2highuid(user), low2highgid(group));
}
asmlinkage long sys32_lchown16(const char * filename, u16 user, u16 group)
{
return sys_lchown(filename, low2highuid(user), low2highgid(group));
}
asmlinkage long sys32_fchown16(unsigned int fd, u16 user, u16 group)
{
return sys_fchown(fd, low2highuid(user), low2highgid(group));
}
asmlinkage long sys32_setregid16(u16 rgid, u16 egid)
{
return sys_setregid(low2highgid(rgid), low2highgid(egid));
}
asmlinkage long sys32_setgid16(u16 gid)
{
return sys_setgid((gid_t)gid);
}
asmlinkage long sys32_setreuid16(u16 ruid, u16 euid)
{
return sys_setreuid(low2highuid(ruid), low2highuid(euid));
}
asmlinkage long sys32_setuid16(u16 uid)
{
return sys_setuid((uid_t)uid);
}
asmlinkage long sys32_setresuid16(u16 ruid, u16 euid, u16 suid)
{
return sys_setresuid(low2highuid(ruid), low2highuid(euid),
low2highuid(suid));
}
asmlinkage long sys32_getresuid16(u16 *ruid, u16 *euid, u16 *suid)
{
int retval;
if (!(retval = put_user(high2lowuid(current->uid), ruid)) &&
!(retval = put_user(high2lowuid(current->euid), euid)))
retval = put_user(high2lowuid(current->suid), suid);
return retval;
}
asmlinkage long sys32_setresgid16(u16 rgid, u16 egid, u16 sgid)
{
return sys_setresgid(low2highgid(rgid), low2highgid(egid),
low2highgid(sgid));
}
asmlinkage long sys32_getresgid16(u16 *rgid, u16 *egid, u16 *sgid)
{
int retval;
if (!(retval = put_user(high2lowgid(current->gid), rgid)) &&
!(retval = put_user(high2lowgid(current->egid), egid)))
retval = put_user(high2lowgid(current->sgid), sgid);
return retval;
}
asmlinkage long sys32_setfsuid16(u16 uid)
{
return sys_setfsuid((uid_t)uid);
}
asmlinkage long sys32_setfsgid16(u16 gid)
{
return sys_setfsgid((gid_t)gid);
}
asmlinkage long sys32_getgroups16(int gidsetsize, u16 *grouplist)
{
u16 groups[NGROUPS];
int i,j;
if (gidsetsize < 0)
return -EINVAL;
i = current->ngroups;
if (gidsetsize) {
if (i > gidsetsize)
return -EINVAL;
for(j=0;j<i;j++)
groups[j] = current->groups[j];
if (copy_to_user(grouplist, groups, sizeof(u16)*i))
return -EFAULT;
}
return i;
}
asmlinkage long sys32_setgroups16(int gidsetsize, u16 *grouplist)
{
u16 groups[NGROUPS];
int i;
if (!capable(CAP_SETGID))
return -EPERM;
if ((unsigned) gidsetsize > NGROUPS)
return -EINVAL;
if (copy_from_user(groups, grouplist, gidsetsize * sizeof(u16)))
return -EFAULT;
for (i = 0 ; i < gidsetsize ; i++)
current->groups[i] = (gid_t)groups[i];
current->ngroups = gidsetsize;
return 0;
}
asmlinkage long sys32_getuid16(void)
{
return high2lowuid(current->uid);
}
asmlinkage long sys32_geteuid16(void)
{
return high2lowuid(current->euid);
}
asmlinkage long sys32_getgid16(void)
{
return high2lowgid(current->gid);
}
asmlinkage long sys32_getegid16(void)
{
return high2lowgid(current->egid);
}
/* 32-bit timeval and related flotsam. */
struct timeval32
{
int tv_sec, tv_usec;
};
struct itimerval32
{
struct timeval32 it_interval;
struct timeval32 it_value;
};
static inline long get_tv32(struct timeval *o, struct timeval32 *i)
{
return (!access_ok(VERIFY_READ, tv32, sizeof(*tv32)) ||
(__get_user(o->tv_sec, &i->tv_sec) |
__get_user(o->tv_usec, &i->tv_usec)));
}
static inline long put_tv32(struct timeval32 *o, struct timeval *i)
{
return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
(__put_user(i->tv_sec, &o->tv_sec) |
__put_user(i->tv_usec, &o->tv_usec)));
}
static inline long get_it32(struct itimerval *o, struct itimerval32 *i)
{
return (!access_ok(VERIFY_READ, i32, sizeof(*i32)) ||
(__get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec) |
__get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec) |
__get_user(o->it_value.tv_sec, &i->it_value.tv_sec) |
__get_user(o->it_value.tv_usec, &i->it_value.tv_usec)));
}
static inline long put_it32(struct itimerval32 *o, struct itimerval *i)
{
return (!access_ok(VERIFY_WRITE, i32, sizeof(*i32)) ||
(__put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec) |
__put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec) |
__put_user(i->it_value.tv_sec, &o->it_value.tv_sec) |
__put_user(i->it_value.tv_usec, &o->it_value.tv_usec)));
}
struct msgbuf32 { s32 mtype; char mtext[1]; };
struct ipc64_perm_ds32
{
__kernel_key_t key;
__kernel_uid32_t uid;
__kernel_gid32_t gid;
__kernel_uid32_t cuid;
__kernel_gid32_t cgid;
__kernel_mode_t32 mode;
unsigned short __pad1;
unsigned short seq;
unsigned short __pad2;
unsigned int __unused1;
unsigned int __unused2;
};
struct ipc_perm32
{
key_t key;
__kernel_uid_t32 uid;
__kernel_gid_t32 gid;
__kernel_uid_t32 cuid;
__kernel_gid_t32 cgid;
__kernel_mode_t32 mode;
unsigned short seq;
};
struct semid_ds32 {
struct ipc_perm32 sem_perm; /* permissions .. see ipc.h */
__kernel_time_t32 sem_otime; /* last semop time */
__kernel_time_t32 sem_ctime; /* last change time */
u32 sem_base; /* ptr to first semaphore in array */
u32 sem_pending; /* pending operations to be processed */
u32 sem_pending_last; /* last pending operation */
u32 undo; /* undo requests on this array */
unsigned short sem_nsems; /* no. of semaphores in array */
};
struct semid64_ds32 {
struct ipc64_perm_ds32 sem_perm;
unsigned int __pad1;
__kernel_time_t32 sem_otime;
unsigned int __pad2;
__kernel_time_t32 sem_ctime;
u32 sem_nsems;
u32 __unused1;
u32 __unused2;
};
struct msqid_ds32
{
struct ipc_perm32 msg_perm;
u32 msg_first;
u32 msg_last;
__kernel_time_t32 msg_stime;
__kernel_time_t32 msg_rtime;
__kernel_time_t32 msg_ctime;
u32 wwait;
u32 rwait;
unsigned short msg_cbytes;
unsigned short msg_qnum;
unsigned short msg_qbytes;
__kernel_ipc_pid_t32 msg_lspid;
__kernel_ipc_pid_t32 msg_lrpid;
};
struct msqid64_ds32 {
struct ipc64_perm_ds32 msg_perm;
unsigned int __pad1;
__kernel_time_t32 msg_stime;
unsigned int __pad2;
__kernel_time_t32 msg_rtime;
unsigned int __pad3;
__kernel_time_t32 msg_ctime;
unsigned int msg_cbytes;
unsigned int msg_qnum;
unsigned int msg_qbytes;
__kernel_pid_t32 msg_lspid;
__kernel_pid_t32 msg_lrpid;
unsigned int __unused1;
unsigned int __unused2;
};
struct shmid_ds32 {
struct ipc_perm32 shm_perm;
int shm_segsz;
__kernel_time_t32 shm_atime;
__kernel_time_t32 shm_dtime;
__kernel_time_t32 shm_ctime;
__kernel_ipc_pid_t32 shm_cpid;
__kernel_ipc_pid_t32 shm_lpid;
unsigned short shm_nattch;
};
struct shmid64_ds32 {
struct ipc64_perm_ds32 shm_perm;
__kernel_size_t32 shm_segsz;
__kernel_time_t32 shm_atime;
unsigned int __unused1;
__kernel_time_t32 shm_dtime;
unsigned int __unused2;
__kernel_time_t32 shm_ctime;
unsigned int __unused3;
__kernel_pid_t32 shm_cpid;
__kernel_pid_t32 shm_lpid;
unsigned int shm_nattch;
unsigned int __unused4;
unsigned int __unused5;
};
/*
* sys32_ipc() is the de-multiplexer for the SysV IPC calls in 32bit emulation..
*
* This is really horribly ugly.
*/
#define IPCOP_MASK(__x) (1UL << (__x))
static int do_sys32_semctl(int first, int second, int third, void *uptr)
{
union semun fourth;
u32 pad;
int err = -EINVAL;
if (!uptr)
goto out;
err = -EFAULT;
if (get_user (pad, (u32 *)uptr))
goto out;
if(third == SETVAL)
fourth.val = (int)pad;
else
fourth.__pad = (void *)A(pad);
if (IPCOP_MASK (third) &
(IPCOP_MASK (IPC_INFO) | IPCOP_MASK (SEM_INFO) | IPCOP_MASK (GETVAL) |
IPCOP_MASK (GETPID) | IPCOP_MASK (GETNCNT) | IPCOP_MASK (GETZCNT) |
IPCOP_MASK (GETALL) | IPCOP_MASK (SETALL) | IPCOP_MASK (IPC_RMID))) {
err = sys_semctl (first, second, third, fourth);
} else if (third & IPC_64) {
struct semid64_ds s;
struct semid64_ds32 *usp = (struct semid64_ds32 *)A(pad);
mm_segment_t old_fs;
int need_back_translation;
if (third == (IPC_SET|IPC_64)) {
err = get_user (s.sem_perm.uid, &usp->sem_perm.uid);
err |= __get_user (s.sem_perm.gid, &usp->sem_perm.gid);
err |= __get_user (s.sem_perm.mode, &usp->sem_perm.mode);
if (err)
goto out;
fourth.__pad = &s;
}
need_back_translation =
(IPCOP_MASK (third) &
(IPCOP_MASK (SEM_STAT) | IPCOP_MASK (IPC_STAT))) != 0;
if (need_back_translation)
fourth.__pad = &s;
old_fs = get_fs ();
set_fs (KERNEL_DS);
err = sys_semctl (first, second, third, fourth);
set_fs (old_fs);
if (need_back_translation) {
int err2 = put_user (s.sem_perm.key, &usp->sem_perm.key);
err2 |= __put_user (high2lowuid(s.sem_perm.uid), &usp->sem_perm.uid);
err2 |= __put_user (high2lowgid(s.sem_perm.gid), &usp->sem_perm.gid);
err2 |= __put_user (high2lowuid(s.sem_perm.cuid), &usp->sem_perm.cuid);
err2 |= __put_user (high2lowgid(s.sem_perm.cgid), &usp->sem_perm.cgid);
err2 |= __put_user (s.sem_perm.mode, &usp->sem_perm.mode);
err2 |= __put_user (s.sem_perm.seq, &usp->sem_perm.seq);
err2 |= __put_user (s.sem_otime, &usp->sem_otime);
err2 |= __put_user (s.sem_ctime, &usp->sem_ctime);
err2 |= __put_user (s.sem_nsems, &usp->sem_nsems);
if (err2) err = -EFAULT;
}
} else {
struct semid_ds s;
struct semid_ds32 *usp = (struct semid_ds32 *)A(pad);
mm_segment_t old_fs;
int need_back_translation;
if (third == IPC_SET) {
err = get_user (s.sem_perm.uid, &usp->sem_perm.uid);
err |= __get_user (s.sem_perm.gid, &usp->sem_perm.gid);
err |= __get_user (s.sem_perm.mode, &usp->sem_perm.mode);
if (err)
goto out;
fourth.__pad = &s;
}
need_back_translation =
(IPCOP_MASK (third) &
(IPCOP_MASK (SEM_STAT) | IPCOP_MASK (IPC_STAT))) != 0;
if (need_back_translation)
fourth.__pad = &s;
old_fs = get_fs ();
set_fs (KERNEL_DS);
err = sys_semctl (first, second, third, fourth);
set_fs (old_fs);
if (need_back_translation) {
int err2 = put_user (s.sem_perm.key, &usp->sem_perm.key);
err2 |= __put_user (high2lowuid(s.sem_perm.uid), &usp->sem_perm.uid);
err2 |= __put_user (high2lowgid(s.sem_perm.gid), &usp->sem_perm.gid);
err2 |= __put_user (high2lowuid(s.sem_perm.cuid), &usp->sem_perm.cuid);
err2 |= __put_user (high2lowgid(s.sem_perm.cgid), &usp->sem_perm.cgid);
err2 |= __put_user (s.sem_perm.mode, &usp->sem_perm.mode);
err2 |= __put_user (s.sem_perm.seq, &usp->sem_perm.seq);
err2 |= __put_user (s.sem_otime, &usp->sem_otime);
err2 |= __put_user (s.sem_ctime, &usp->sem_ctime);
err2 |= __put_user (s.sem_nsems, &usp->sem_nsems);
if (err2) err = -EFAULT;
}
}
out:
return err;
}
static int do_sys32_msgsnd (int first, int second, int third, void *uptr)
{
struct msgbuf *p = kmalloc (second + sizeof (struct msgbuf), GFP_USER);
struct msgbuf32 *up = (struct msgbuf32 *)uptr;
mm_segment_t old_fs;
int err;
if (!p)
return -ENOMEM;
err = -EINVAL;
if (second > MSGMAX || first < 0 || second < 0)
goto out;
err = -EFAULT;
if (!uptr)
goto out;
if (get_user (p->mtype, &up->mtype) ||
__copy_from_user (p->mtext, &up->mtext, second))
goto out;
old_fs = get_fs ();
set_fs (KERNEL_DS);
err = sys_msgsnd (first, p, second, third);
set_fs (old_fs);
out:
kfree (p);
return err;
}
static int do_sys32_msgrcv (int first, int second, int msgtyp, int third,
int version, void *uptr)
{
struct msgbuf32 *up;
struct msgbuf *p;
mm_segment_t old_fs;
int err;
if (first < 0 || second < 0)
return -EINVAL;
if (!version) {
struct ipc_kludge_32 *uipck = (struct ipc_kludge_32 *)uptr;
struct ipc_kludge_32 ipck;
err = -EINVAL;
if (!uptr)
goto out;
err = -EFAULT;
if (copy_from_user (&ipck, uipck, sizeof (struct ipc_kludge_32)))
goto out;
uptr = (void *)A(ipck.msgp);
msgtyp = ipck.msgtyp;
}
err = -ENOMEM;
p = kmalloc (second + sizeof (struct msgbuf), GFP_USER);
if (!p)
goto out;
old_fs = get_fs ();
set_fs (KERNEL_DS);
err = sys_msgrcv (first, p, second, msgtyp, third);
set_fs (old_fs);
if (err < 0)
goto free_then_out;
up = (struct msgbuf32 *)uptr;
if (put_user (p->mtype, &up->mtype) ||
__copy_to_user (&up->mtext, p->mtext, err))
err = -EFAULT;
free_then_out:
kfree (p);
out:
return err;
}
static int do_sys32_msgctl (int first, int second, void *uptr)
{
int err;
if (IPCOP_MASK (second) &
(IPCOP_MASK (IPC_INFO) | IPCOP_MASK (MSG_INFO) |
IPCOP_MASK (IPC_RMID))) {
err = sys_msgctl (first, second, (struct msqid_ds *)uptr);
} else if (second & IPC_64) {
struct msqid64_ds m;
struct msqid64_ds32 *up = (struct msqid64_ds32 *)uptr;
mm_segment_t old_fs;
if (second == (IPC_SET|IPC_64)) {
err = get_user (m.msg_perm.uid, &up->msg_perm.uid);
err |= __get_user (m.msg_perm.gid, &up->msg_perm.gid);
err |= __get_user (m.msg_perm.mode, &up->msg_perm.mode);
err |= __get_user (m.msg_qbytes, &up->msg_qbytes);
if (err)
goto out;
}
old_fs = get_fs ();
set_fs (KERNEL_DS);
err = sys_msgctl (first, second, (struct msqid_ds *)&m);
set_fs (old_fs);
if (IPCOP_MASK (second) &
(IPCOP_MASK (MSG_STAT) | IPCOP_MASK (IPC_STAT))) {
int err2 = put_user (m.msg_perm.key, &up->msg_perm.key);
err2 |= __put_user (high2lowuid(m.msg_perm.uid), &up->msg_perm.uid);
err2 |= __put_user (high2lowgid(m.msg_perm.gid), &up->msg_perm.gid);
err2 |= __put_user (high2lowuid(m.msg_perm.cuid), &up->msg_perm.cuid);
err2 |= __put_user (high2lowgid(m.msg_perm.cgid), &up->msg_perm.cgid);
err2 |= __put_user (m.msg_perm.mode, &up->msg_perm.mode);
err2 |= __put_user (m.msg_perm.seq, &up->msg_perm.seq);
err2 |= __put_user (m.msg_stime, &up->msg_stime);
err2 |= __put_user (m.msg_rtime, &up->msg_rtime);
err2 |= __put_user (m.msg_ctime, &up->msg_ctime);
err2 |= __put_user (m.msg_cbytes, &up->msg_cbytes);
err2 |= __put_user (m.msg_qnum, &up->msg_qnum);
err2 |= __put_user (m.msg_qbytes, &up->msg_qbytes);
err2 |= __put_user (m.msg_lspid, &up->msg_lspid);
err2 |= __put_user (m.msg_lrpid, &up->msg_lrpid);
if (err2)
err = -EFAULT;
}
} else {
struct msqid_ds m;
struct msqid_ds32 *up = (struct msqid_ds32 *)uptr;
mm_segment_t old_fs;
if (second == IPC_SET) {
err = get_user (m.msg_perm.uid, &up->msg_perm.uid);
err |= __get_user (m.msg_perm.gid, &up->msg_perm.gid);
err |= __get_user (m.msg_perm.mode, &up->msg_perm.mode);
err |= __get_user (m.msg_qbytes, &up->msg_qbytes);
if (err)
goto out;
}
old_fs = get_fs ();
set_fs (KERNEL_DS);
err = sys_msgctl (first, second, &m);
set_fs (old_fs);
if (IPCOP_MASK (second) &
(IPCOP_MASK (MSG_STAT) | IPCOP_MASK (IPC_STAT))) {
int err2 = put_user (m.msg_perm.key, &up->msg_perm.key);
err2 |= __put_user (high2lowuid(m.msg_perm.uid), &up->msg_perm.uid);
err2 |= __put_user (high2lowgid(m.msg_perm.gid), &up->msg_perm.gid);
err2 |= __put_user (high2lowuid(m.msg_perm.cuid), &up->msg_perm.cuid);
err2 |= __put_user (high2lowgid(m.msg_perm.cgid), &up->msg_perm.cgid);
err2 |= __put_user (m.msg_perm.mode, &up->msg_perm.mode);
err2 |= __put_user (m.msg_perm.seq, &up->msg_perm.seq);
err2 |= __put_user (m.msg_stime, &up->msg_stime);
err2 |= __put_user (m.msg_rtime, &up->msg_rtime);
err2 |= __put_user (m.msg_ctime, &up->msg_ctime);
err2 |= __put_user (m.msg_cbytes, &up->msg_cbytes);
err2 |= __put_user (m.msg_qnum, &up->msg_qnum);
err2 |= __put_user (m.msg_qbytes, &up->msg_qbytes);
err2 |= __put_user (m.msg_lspid, &up->msg_lspid);
err2 |= __put_user (m.msg_lrpid, &up->msg_lrpid);
if (err2)
err = -EFAULT;
}
}
out:
return err;
}
static int do_sys32_shmat (int first, int second, int third, int version, void *uptr)
{
unsigned long raddr;
u32 *uaddr = (u32 *)A((u32)third);
int err = -EINVAL;
if (version == 1)
goto out;
err = sys_shmat (first, uptr, second, &raddr);
if (err)
goto out;
err = put_user (raddr, uaddr);
out:
return err;
}
static int do_sys32_shmctl (int first, int second, void *uptr)
{
int err;
if (IPCOP_MASK (second) &
(IPCOP_MASK (IPC_INFO) | IPCOP_MASK (SHM_LOCK) | IPCOP_MASK (SHM_UNLOCK) |
IPCOP_MASK (IPC_RMID))) {
if (second == (IPC_INFO|IPC_64))
second = IPC_INFO; /* So that we don't have to translate it */
err = sys_shmctl (first, second, (struct shmid_ds *)uptr);
} else if ((second & IPC_64) && second != (SHM_INFO|IPC_64)) {
struct shmid64_ds s;
struct shmid64_ds32 *up = (struct shmid64_ds32 *)uptr;
mm_segment_t old_fs;
if (second == (IPC_SET|IPC_64)) {
err = get_user (s.shm_perm.uid, &up->shm_perm.uid);
err |= __get_user (s.shm_perm.gid, &up->shm_perm.gid);
err |= __get_user (s.shm_perm.mode, &up->shm_perm.mode);
if (err)
goto out;
}
old_fs = get_fs ();
set_fs (KERNEL_DS);
err = sys_shmctl (first, second, (struct shmid_ds *)&s);
set_fs (old_fs);
if (err < 0)
goto out;
/* Mask it even in this case so it becomes a CSE. */
if (IPCOP_MASK (second) &
(IPCOP_MASK (SHM_STAT) | IPCOP_MASK (IPC_STAT))) {
int err2 = put_user (s.shm_perm.key, &up->shm_perm.key);
err2 |= __put_user (high2lowuid(s.shm_perm.uid), &up->shm_perm.uid);
err2 |= __put_user (high2lowgid(s.shm_perm.gid), &up->shm_perm.gid);
err2 |= __put_user (high2lowuid(s.shm_perm.cuid), &up->shm_perm.cuid);
err2 |= __put_user (high2lowgid(s.shm_perm.cgid), &up->shm_perm.cgid);
err2 |= __put_user (s.shm_perm.mode, &up->shm_perm.mode);
err2 |= __put_user (s.shm_perm.seq, &up->shm_perm.seq);
err2 |= __put_user (s.shm_atime, &up->shm_atime);
err2 |= __put_user (s.shm_dtime, &up->shm_dtime);
err2 |= __put_user (s.shm_ctime, &up->shm_ctime);
err2 |= __put_user (s.shm_segsz, &up->shm_segsz);
err2 |= __put_user (s.shm_nattch, &up->shm_nattch);
err2 |= __put_user (s.shm_cpid, &up->shm_cpid);
err2 |= __put_user (s.shm_lpid, &up->shm_lpid);
if (err2)
err = -EFAULT;
}
} else {
struct shmid_ds s;
struct shmid_ds32 *up = (struct shmid_ds32 *)uptr;
mm_segment_t old_fs;
second &= ~IPC_64;
if (second == IPC_SET) {
err = get_user (s.shm_perm.uid, &up->shm_perm.uid);
err |= __get_user (s.shm_perm.gid, &up->shm_perm.gid);
err |= __get_user (s.shm_perm.mode, &up->shm_perm.mode);
if (err)
goto out;
}
old_fs = get_fs ();
set_fs (KERNEL_DS);
err = sys_shmctl (first, second, &s);
set_fs (old_fs);
if (err < 0)
goto out;
/* Mask it even in this case so it becomes a CSE. */
if (second == SHM_INFO) {
struct shm_info32 {
int used_ids;
u32 shm_tot, shm_rss, shm_swp;
u32 swap_attempts, swap_successes;
} *uip = (struct shm_info32 *)uptr;
struct shm_info *kp = (struct shm_info *)&s;
int err2 = put_user (kp->used_ids, &uip->used_ids);
err2 |= __put_user (kp->shm_tot, &uip->shm_tot);
err2 |= __put_user (kp->shm_rss, &uip->shm_rss);
err2 |= __put_user (kp->shm_swp, &uip->shm_swp);
err2 |= __put_user (kp->swap_attempts, &uip->swap_attempts);
err2 |= __put_user (kp->swap_successes, &uip->swap_successes);
if (err2)
err = -EFAULT;
} else if (IPCOP_MASK (second) &
(IPCOP_MASK (SHM_STAT) | IPCOP_MASK (IPC_STAT))) {
int err2 = put_user (s.shm_perm.key, &up->shm_perm.key);
err2 |= __put_user (high2lowuid(s.shm_perm.uid), &up->shm_perm.uid);
err2 |= __put_user (high2lowgid(s.shm_perm.gid), &up->shm_perm.gid);
err2 |= __put_user (high2lowuid(s.shm_perm.cuid), &up->shm_perm.cuid);
err2 |= __put_user (high2lowgid(s.shm_perm.cgid), &up->shm_perm.cgid);
err2 |= __put_user (s.shm_perm.mode, &up->shm_perm.mode);
err2 |= __put_user (s.shm_perm.seq, &up->shm_perm.seq);
err2 |= __put_user (s.shm_atime, &up->shm_atime);
err2 |= __put_user (s.shm_dtime, &up->shm_dtime);
err2 |= __put_user (s.shm_ctime, &up->shm_ctime);
err2 |= __put_user (s.shm_segsz, &up->shm_segsz);
err2 |= __put_user (s.shm_nattch, &up->shm_nattch);
err2 |= __put_user (s.shm_cpid, &up->shm_cpid);
err2 |= __put_user (s.shm_lpid, &up->shm_lpid);
if (err2)
err = -EFAULT;
}
}
out:
return err;
}
asmlinkage int sys32_ipc (u32 call, int first, int second, int third, u32 ptr, u32 fifth)
{
int version, err;
version = call >> 16; /* hack for backward compatibility */
call &= 0xffff;
if(version)
return -EINVAL;
if (call <= SEMCTL)
switch (call) {
case SEMOP:
/* struct sembuf is the same on 32 and 64bit :)) */
err = sys_semop (first, (struct sembuf *)AA(ptr), second);
goto out;
case SEMGET:
err = sys_semget (first, second, third);
goto out;
case SEMCTL:
err = do_sys32_semctl (first, second, third, (void *)AA(ptr));
goto out;
default:
err = -EINVAL;
goto out;
};
if (call <= MSGCTL)
switch (call) {
case MSGSND:
err = do_sys32_msgsnd (first, second, third, (void *)AA(ptr));
goto out;
case MSGRCV:
err = do_sys32_msgrcv (first, second, 0, third,
version, (void *)AA(ptr));
goto out;
case MSGGET:
err = sys_msgget ((key_t) first, second);
goto out;
case MSGCTL:
err = do_sys32_msgctl (first, second, (void *)AA(ptr));
goto out;
default:
err = -EINVAL;
goto out;
}
if (call <= SHMCTL)
switch (call) {
case SHMAT:
err = do_sys32_shmat (first, second, third,
version, (void *)AA(ptr));
goto out;
case SHMDT:
err = sys_shmdt ((char *)AA(ptr));
goto out;
case SHMGET:
err = sys_shmget (first, second, third);
goto out;
case SHMCTL:
err = do_sys32_shmctl (first, second, (void *)AA(ptr));
goto out;
default:
err = -EINVAL;
goto out;
}
err = -EINVAL;
out:
return err;
}
static inline int get_flock(struct flock *kfl, struct flock32 *ufl)
{
int err;
err = get_user(kfl->l_type, &ufl->l_type);
err |= __get_user(kfl->l_whence, &ufl->l_whence);
err |= __get_user(kfl->l_start, &ufl->l_start);
err |= __get_user(kfl->l_len, &ufl->l_len);
err |= __get_user(kfl->l_pid, &ufl->l_pid);
return err;
}
static inline int put_flock(struct flock *kfl, struct flock32 *ufl)
{
int err;
err = __put_user(kfl->l_type, &ufl->l_type);
err |= __put_user(kfl->l_whence, &ufl->l_whence);
err |= __put_user(kfl->l_start, &ufl->l_start);
err |= __put_user(kfl->l_len, &ufl->l_len);
err |= __put_user(kfl->l_pid, &ufl->l_pid);
return err;
}
extern asmlinkage long sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg);
asmlinkage long sys32_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case F_GETLK:
{
struct flock f;
mm_segment_t old_fs;
long ret;
if(get_flock(&f, (struct flock32 *)A(arg)))
return -EFAULT;
old_fs = get_fs(); set_fs (KERNEL_DS);
ret = sys_fcntl(fd, cmd, (unsigned long)&f);
set_fs (old_fs);
if (ret) return ret;
if (f.l_start >= 0x7fffffffUL ||
f.l_start + f.l_len >= 0x7fffffffUL)
return -EOVERFLOW;
if(put_flock(&f, (struct flock32 *)A(arg)))
return -EFAULT;
return 0;
}
case F_SETLK:
case F_SETLKW:
{
struct flock f;
mm_segment_t old_fs;
long ret;
if(get_flock(&f, (struct flock32 *)A(arg)))
return -EFAULT;
old_fs = get_fs(); set_fs (KERNEL_DS);
ret = sys_fcntl(fd, cmd, (unsigned long)&f);
set_fs (old_fs);
if (ret) return ret;
return 0;
}
default:
return sys_fcntl(fd, cmd, (unsigned long)arg);
}
}
asmlinkage long sys32_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
{
if (cmd >= F_GETLK64 && cmd <= F_SETLKW64)
return sys_fcntl(fd, cmd + F_GETLK - F_GETLK64, arg);
return sys32_fcntl(fd, cmd, arg);
}
struct user_dqblk32 {
__u32 dqb_bhardlimit;
__u32 dqb_bsoftlimit;
__u32 dqb_curblocks;
__u32 dqb_ihardlimit;
__u32 dqb_isoftlimit;
__u32 dqb_curinodes;
__kernel_time_t32 dqb_btime;
__kernel_time_t32 dqb_itime;
};
extern asmlinkage int sys_quotactl(int cmd, const char *special, int id, caddr_t addr);
asmlinkage int sys32_quotactl(int cmd, const char *special, int id, caddr_t addr)
{
int cmds = cmd >> SUBCMDSHIFT;
int err;
struct v1c_mem_dqblk d;
mm_segment_t old_fs;
char *spec;
switch (cmds) {
case Q_V1_GETQUOTA:
break;
case Q_V1_SETQUOTA:
case Q_V1_SETUSE:
case Q_V1_SETQLIM:
if (copy_from_user(&d, addr, sizeof (struct user_dqblk32)))
return -EFAULT;
d.dqb_itime = ((struct user_dqblk32 *)&d)->dqb_itime;
d.dqb_btime = ((struct user_dqblk32 *)&d)->dqb_btime;
break;
default:
return sys_quotactl(cmd, special, id, addr);
}
spec = getname(special);
err = PTR_ERR(spec);
if (IS_ERR(spec))
return err;
old_fs = get_fs();
set_fs (KERNEL_DS);
err = sys_quotactl(cmd, (const char *)spec, id, (caddr_t)&d);
set_fs(old_fs);
putname(spec);
if (err)
return err;
if (cmds == Q_V1_GETQUOTA) {
__kernel_time_t b = d.dqb_btime, i = d.dqb_itime;
((struct user_dqblk32 *)&d)->dqb_itime = i;
((struct user_dqblk32 *)&d)->dqb_btime = b;
if (copy_to_user(addr, &d, sizeof (struct user_dqblk32)))
return -EFAULT;
}
return 0;
}
static inline int put_statfs (struct statfs32 *ubuf, struct statfs *kbuf)
{
int err;
err = put_user (kbuf->f_type, &ubuf->f_type);
err |= __put_user (kbuf->f_bsize, &ubuf->f_bsize);
err |= __put_user (kbuf->f_blocks, &ubuf->f_blocks);
err |= __put_user (kbuf->f_bfree, &ubuf->f_bfree);
err |= __put_user (kbuf->f_bavail, &ubuf->f_bavail);
err |= __put_user (kbuf->f_files, &ubuf->f_files);
err |= __put_user (kbuf->f_ffree, &ubuf->f_ffree);
err |= __put_user (kbuf->f_namelen, &ubuf->f_namelen);
err |= __put_user (kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]);
err |= __put_user (kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]);
return err;
}
extern asmlinkage int sys_statfs(const char * path, struct statfs * buf);
asmlinkage int sys32_statfs(const char * path, struct statfs32 *buf)
{
int ret;
struct statfs s;
mm_segment_t old_fs = get_fs();
char *pth;
pth = getname (path);
ret = PTR_ERR(pth);
if (!IS_ERR(pth)) {
set_fs (KERNEL_DS);
ret = sys_statfs((const char *)pth, &s);
set_fs (old_fs);
putname (pth);
if (put_statfs(buf, &s))
return -EFAULT;
}
return ret;
}
extern asmlinkage int sys_fstatfs(unsigned int fd, struct statfs * buf);
asmlinkage int sys32_fstatfs(unsigned int fd, struct statfs32 *buf)
{
int ret;
struct statfs s;
mm_segment_t old_fs = get_fs();
set_fs (KERNEL_DS);
ret = sys_fstatfs(fd, &s);
set_fs (old_fs);
if (put_statfs(buf, &s))
return -EFAULT;
return ret;
}
extern asmlinkage long sys_truncate(const char * path, unsigned long length);
extern asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length);
asmlinkage int sys32_truncate64(const char * path, unsigned long high, unsigned long low)
{
if ((int)high < 0)
return -EINVAL;
else
return sys_truncate(path, (high << 32) | low);
}
asmlinkage int sys32_ftruncate64(unsigned int fd, unsigned long high, unsigned long low)
{
if ((int)high < 0)
return -EINVAL;
else
return sys_ftruncate(fd, (high << 32) | low);
}
extern asmlinkage int sys_utime(char * filename, struct utimbuf * times);
struct utimbuf32 {
__kernel_time_t32 actime, modtime;
};
asmlinkage int sys32_utime(char * filename, struct utimbuf32 *times)
{
struct utimbuf t;
mm_segment_t old_fs;
int ret;
char *filenam;
if (!times)
return sys_utime(filename, NULL);
if (get_user (t.actime, &times->actime) ||
__get_user (t.modtime, &times->modtime))
return -EFAULT;
filenam = getname (filename);
ret = PTR_ERR(filenam);
if (!IS_ERR(filenam)) {
old_fs = get_fs();
set_fs (KERNEL_DS);
ret = sys_utime(filenam, &t);
set_fs (old_fs);
putname (filenam);
}
return ret;
}
struct iovec32 { u32 iov_base; __kernel_size_t32 iov_len; };
typedef ssize_t (*io_fn_t)(struct file *, char *, size_t, loff_t *);
typedef ssize_t (*iov_fn_t)(struct file *, const struct iovec *, unsigned long, loff_t *);
static long do_readv_writev32(int type, struct file *file,
const struct iovec32 *vector, u32 count)
{
unsigned long tot_len;
struct iovec iovstack[UIO_FASTIOV];
struct iovec *iov=iovstack, *ivp;
long retval, i;
io_fn_t fn;
iov_fn_t fnv;
/* First get the "struct iovec" from user memory and
* verify all the pointers
*/
if (!count)
return 0;
if (verify_area(VERIFY_READ, vector, sizeof(struct iovec32)*count))
return -EFAULT;
if (count > UIO_MAXIOV)
return -EINVAL;
if (count > UIO_FASTIOV) {
iov = kmalloc(count*sizeof(struct iovec), GFP_KERNEL);
if (!iov)
return -ENOMEM;
}
tot_len = 0;
i = count;
ivp = iov;
while(i > 0) {
u32 len;
u32 buf;
__get_user(len, &vector->iov_len);
__get_user(buf, &vector->iov_base);
tot_len += len;
ivp->iov_base = (void *)A(buf);
ivp->iov_len = (__kernel_size_t) len;
vector++;
ivp++;
i--;
}
/* VERIFY_WRITE actually means a read, as we write to user space */
retval = rw_verify_area((type == VERIFY_WRITE ? READ : WRITE),
file, &file->f_pos, tot_len);
if (retval)
goto out;
/* VERIFY_WRITE actually means a read, as we write to user space */
fnv = (type == VERIFY_WRITE ? file->f_op->readv : file->f_op->writev);
if (fnv) {
retval = fnv(file, iov, count, &file->f_pos);
goto out;
}
fn = (type == VERIFY_WRITE ? file->f_op->read :
(io_fn_t) file->f_op->write);
ivp = iov;
while (count > 0) {
void * base;
int len, nr;
base = ivp->iov_base;
len = ivp->iov_len;
ivp++;
count--;
nr = fn(file, base, len, &file->f_pos);
if (nr < 0) {
if (!retval)
retval = nr;
break;
}
retval += nr;
if (nr != len)
break;
}
out:
if (iov != iovstack)
kfree(iov);
return retval;
}
asmlinkage long sys32_readv(int fd, struct iovec32 *vector, u32 count)
{
struct file *file;
long ret = -EBADF;
file = fget(fd);
if(!file)
goto bad_file;
if (file->f_op && (file->f_mode & FMODE_READ) &&
(file->f_op->readv || file->f_op->read))
ret = do_readv_writev32(VERIFY_WRITE, file, vector, count);
fput(file);
bad_file:
return ret;
}
asmlinkage long sys32_writev(int fd, struct iovec32 *vector, u32 count)
{
struct file *file;
int ret = -EBADF;
file = fget(fd);
if(!file)
goto bad_file;
if (file->f_op && (file->f_mode & FMODE_WRITE) &&
(file->f_op->writev || file->f_op->write))
ret = do_readv_writev32(VERIFY_READ, file, vector, count);
fput(file);
bad_file:
return ret;
}
/* readdir & getdents */
#define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
#define ROUND_UP(x) (((x)+sizeof(u32)-1) & ~(sizeof(u32)-1))
struct old_linux_dirent32 {
u32 d_ino;
u32 d_offset;
unsigned short d_namlen;
char d_name[1];
};
struct readdir_callback32 {
struct old_linux_dirent32 * dirent;
int count;
};
static int fillonedir(void * __buf, const char * name, int namlen,
loff_t offset, ino_t ino, unsigned int d_type)
{
struct readdir_callback32 * buf = (struct readdir_callback32 *) __buf;
struct old_linux_dirent32 * dirent;
if (buf->count)
return -EINVAL;
buf->count++;
dirent = buf->dirent;
put_user(ino, &dirent->d_ino);
put_user(offset, &dirent->d_offset);
put_user(namlen, &dirent->d_namlen);
copy_to_user(dirent->d_name, name, namlen);
put_user(0, dirent->d_name + namlen);
return 0;
}
asmlinkage int old32_readdir(unsigned int fd, struct old_linux_dirent32 *dirent, unsigned int count)
{
int error = -EBADF;
struct file * file;
struct readdir_callback32 buf;
file = fget(fd);
if (!file)
goto out;
buf.count = 0;
buf.dirent = dirent;
error = vfs_readdir(file, fillonedir, &buf);
if (error < 0)
goto out_putf;
error = buf.count;
out_putf:
fput(file);
out:
return error;
}
struct linux_dirent32 {
u32 d_ino;
u32 d_off;
unsigned short d_reclen;
char d_name[1];
};
struct getdents_callback32 {
struct linux_dirent32 * current_dir;
struct linux_dirent32 * previous;
int count;
int error;
};
static int filldir(void * __buf, const char * name, int namlen, loff_t offset, ino_t ino,
unsigned int d_type)
{
struct linux_dirent32 * dirent;
struct getdents_callback32 * buf = (struct getdents_callback32 *) __buf;
int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 1);
buf->error = -EINVAL; /* only used if we fail.. */
if (reclen > buf->count)
return -EINVAL;
dirent = buf->previous;
if (dirent)
put_user(offset, &dirent->d_off);
dirent = buf->current_dir;
buf->previous = dirent;
put_user(ino, &dirent->d_ino);
put_user(reclen, &dirent->d_reclen);
copy_to_user(dirent->d_name, name, namlen);
put_user(0, dirent->d_name + namlen);
((char *) dirent) += reclen;
buf->current_dir = dirent;
buf->count -= reclen;
return 0;
}
asmlinkage int sys32_getdents(unsigned int fd, struct linux_dirent32 *dirent, unsigned int count)
{
struct file * file;
struct linux_dirent32 * lastdirent;
struct getdents_callback32 buf;
int error = -EBADF;
file = fget(fd);
if (!file)
goto out;
buf.current_dir = dirent;
buf.previous = NULL;
buf.count = count;
buf.error = 0;
error = vfs_readdir(file, filldir, &buf);
if (error < 0)
goto out_putf;
lastdirent = buf.previous;
error = buf.error;
if(lastdirent) {
put_user(file->f_pos, &lastdirent->d_off);
error = count - buf.count;
}
out_putf:
fput(file);
out:
return error;
}
/* end of readdir & getdents */
/*
* Ooo, nasty. We need here to frob 32-bit unsigned longs to
* 64-bit unsigned longs.
*/
static inline int
get_fd_set32(unsigned long n, unsigned long *fdset, u32 *ufdset)
{
if (ufdset) {
unsigned long odd;
if (verify_area(VERIFY_WRITE, ufdset, n*sizeof(u32)))
return -EFAULT;
odd = n & 1UL;
n &= ~1UL;
while (n) {
unsigned long h, l;
__get_user(l, ufdset);
__get_user(h, ufdset+1);
ufdset += 2;
*fdset++ = h << 32 | l;
n -= 2;
}
if (odd)
__get_user(*fdset, ufdset);
} else {
/* Tricky, must clear full unsigned long in the
* kernel fdset at the end, this makes sure that
* actually happens.
*/
memset(fdset, 0, ((n + 1) & ~1)*sizeof(u32));
}
return 0;
}
static inline void
set_fd_set32(unsigned long n, u32 *ufdset, unsigned long *fdset)
{
unsigned long odd;
if (!ufdset)
return;
odd = n & 1UL;
n &= ~1UL;
while (n) {
unsigned long h, l;
l = *fdset++;
h = l >> 32;
__put_user(l, ufdset);
__put_user(h, ufdset+1);
ufdset += 2;
n -= 2;
}
if (odd)
__put_user(*fdset, ufdset);
}
#define MAX_SELECT_SECONDS \
((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
asmlinkage int sys32_select(int n, u32 *inp, u32 *outp, u32 *exp, u32 tvp_x)
{
fd_set_bits fds;
struct timeval32 *tvp = (struct timeval32 *)AA(tvp_x);
char *bits;
unsigned long nn;
long timeout;
int ret, size;
timeout = MAX_SCHEDULE_TIMEOUT;
if (tvp) {
int sec, usec;
if ((ret = verify_area(VERIFY_READ, tvp, sizeof(*tvp)))
|| (ret = __get_user(sec, &tvp->tv_sec))
|| (ret = __get_user(usec, &tvp->tv_usec)))
goto out_nofds;
ret = -EINVAL;
if(sec < 0 || usec < 0)
goto out_nofds;
if ((unsigned long) sec < MAX_SELECT_SECONDS) {
timeout = (usec + 1000000/HZ - 1) / (1000000/HZ);
timeout += sec * (unsigned long) HZ;
}
}
ret = -EINVAL;
if (n < 0)
goto out_nofds;
if (n > current->files->max_fdset)
n = current->files->max_fdset;
/*
* We need 6 bitmaps (in/out/ex for both incoming and outgoing),
* since we used fdset we need to allocate memory in units of
* long-words.
*/
ret = -ENOMEM;
size = FDS_BYTES(n);
bits = kmalloc(6 * size, GFP_KERNEL);
if (!bits)
goto out_nofds;
fds.in = (unsigned long *) bits;
fds.out = (unsigned long *) (bits + size);
fds.ex = (unsigned long *) (bits + 2*size);
fds.res_in = (unsigned long *) (bits + 3*size);
fds.res_out = (unsigned long *) (bits + 4*size);
fds.res_ex = (unsigned long *) (bits + 5*size);
nn = (n + 8*sizeof(u32) - 1) / (8*sizeof(u32));
if ((ret = get_fd_set32(nn, fds.in, inp)) ||
(ret = get_fd_set32(nn, fds.out, outp)) ||
(ret = get_fd_set32(nn, fds.ex, exp)))
goto out;
zero_fd_set(n, fds.res_in);
zero_fd_set(n, fds.res_out);
zero_fd_set(n, fds.res_ex);
ret = do_select(n, &fds, &timeout);
if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
int sec = 0, usec = 0;
if (timeout) {
sec = timeout / HZ;
usec = timeout % HZ;
usec *= (1000000/HZ);
}
put_user(sec, &tvp->tv_sec);
put_user(usec, &tvp->tv_usec);
}
if (ret < 0)
goto out;
if (!ret) {
ret = -ERESTARTNOHAND;
if (signal_pending(current))
goto out;
ret = 0;
}
set_fd_set32(nn, inp, fds.res_in);
set_fd_set32(nn, outp, fds.res_out);
set_fd_set32(nn, exp, fds.res_ex);
out:
kfree(bits);
out_nofds:
return ret;
}
static int cp_new_stat32(struct inode *inode, struct stat32 *statbuf)
{
unsigned long ino, blksize, blocks;
kdev_t dev, rdev;
umode_t mode;
nlink_t nlink;
uid_t uid;
gid_t gid;
off_t size;
time_t atime, mtime, ctime;
int err;
/* Stream the loads of inode data into the load buffer,
* then we push it all into the store buffer below. This
* should give optimal cache performance.
*/
ino = inode->i_ino;
dev = inode->i_dev;
mode = inode->i_mode;
nlink = inode->i_nlink;
uid = inode->i_uid;
gid = inode->i_gid;
rdev = inode->i_rdev;
size = inode->i_size;
atime = inode->i_atime;
mtime = inode->i_mtime;
ctime = inode->i_ctime;
blksize = inode->i_blksize;
blocks = inode->i_blocks;
err = put_user(kdev_t_to_nr(dev), &statbuf->st_dev);
err |= put_user(ino, &statbuf->st_ino);
err |= put_user(mode, &statbuf->st_mode);
err |= put_user(nlink, &statbuf->st_nlink);
err |= put_user(high2lowuid(uid), &statbuf->st_uid);
err |= put_user(high2lowgid(gid), &statbuf->st_gid);
err |= put_user(kdev_t_to_nr(rdev), &statbuf->st_rdev);
err |= put_user(size, &statbuf->st_size);
err |= put_user(atime, &statbuf->st_atime);
err |= put_user(0, &statbuf->__unused1);
err |= put_user(mtime, &statbuf->st_mtime);
err |= put_user(0, &statbuf->__unused2);
err |= put_user(ctime, &statbuf->st_ctime);
err |= put_user(0, &statbuf->__unused3);
if (blksize) {
err |= put_user(blksize, &statbuf->st_blksize);
err |= put_user(blocks, &statbuf->st_blocks);
} else {
unsigned int tmp_blocks;
#define D_B 7
#define I_B (BLOCK_SIZE / sizeof(unsigned short))
tmp_blocks = (size + BLOCK_SIZE - 1) / BLOCK_SIZE;
if (tmp_blocks > D_B) {
unsigned int indirect;
indirect = (tmp_blocks - D_B + I_B - 1) / I_B;
tmp_blocks += indirect;
if (indirect > 1) {
indirect = (indirect - 1 + I_B - 1) / I_B;
tmp_blocks += indirect;
if (indirect > 1)
tmp_blocks++;
}
}
err |= put_user(BLOCK_SIZE, &statbuf->st_blksize);
err |= put_user((BLOCK_SIZE / 512) * tmp_blocks, &statbuf->st_blocks);
#undef D_B
#undef I_B
}
/* fixme
err |= put_user(0, &statbuf->__unused4[0]);
err |= put_user(0, &statbuf->__unused4[1]);
*/
return err;
}
/* Perhaps this belongs in fs.h or similar. -DaveM */
static __inline__ int
do_revalidate(struct dentry *dentry)
{
struct inode * inode = dentry->d_inode;
if (inode->i_op && inode->i_op->revalidate)
return inode->i_op->revalidate(dentry);
return 0;
}
asmlinkage int sys32_newstat(char * filename, struct stat32 *statbuf)
{
struct nameidata nd;
int error;
error = user_path_walk(filename, &nd);
if (!error) {
error = do_revalidate(nd.dentry);
if (!error)
error = cp_new_stat32(nd.dentry->d_inode, statbuf);
path_release(&nd);
}
return error;
}
asmlinkage int sys32_newlstat(char * filename, struct stat32 *statbuf)
{
struct nameidata nd;
int error;
error = user_path_walk_link(filename, &nd);
if (!error) {
error = do_revalidate(nd.dentry);
if (!error)
error = cp_new_stat32(nd.dentry->d_inode, statbuf);
path_release(&nd);
}
return error;
}
asmlinkage int sys32_newfstat(unsigned int fd, struct stat32 *statbuf)
{
struct file *f;
int err = -EBADF;
f = fget(fd);
if (f) {
struct dentry * dentry = f->f_dentry;
err = do_revalidate(dentry);
if (!err)
err = cp_new_stat32(dentry->d_inode, statbuf);
fput(f);
}
return err;
}
extern asmlinkage int sys_sysfs(int option, unsigned long arg1, unsigned long arg2);
asmlinkage int sys32_sysfs(int option, u32 arg1, u32 arg2)
{
return sys_sysfs(option, arg1, arg2);
}
struct ncp_mount_data32 {
int version;
unsigned int ncp_fd;
__kernel_uid_t32 mounted_uid;
__kernel_pid_t32 wdog_pid;
unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
unsigned int time_out;
unsigned int retry_count;
unsigned int flags;
__kernel_uid_t32 uid;
__kernel_gid_t32 gid;
__kernel_mode_t32 file_mode;
__kernel_mode_t32 dir_mode;
};
static void *do_ncp_super_data_conv(void *raw_data)
{
struct ncp_mount_data *n = (struct ncp_mount_data *)raw_data;
struct ncp_mount_data32 *n32 = (struct ncp_mount_data32 *)raw_data;
n->dir_mode = n32->dir_mode;
n->file_mode = n32->file_mode;
n->gid = low2highgid(n32->gid);
n->uid = low2highuid(n32->uid);
memmove (n->mounted_vol, n32->mounted_vol, (sizeof (n32->mounted_vol) + 3 * sizeof (unsigned int)));
n->wdog_pid = n32->wdog_pid;
n->mounted_uid = low2highuid(n32->mounted_uid);
return raw_data;
}
struct smb_mount_data32 {
int version;
__kernel_uid_t32 mounted_uid;
__kernel_uid_t32 uid;
__kernel_gid_t32 gid;
__kernel_mode_t32 file_mode;
__kernel_mode_t32 dir_mode;
};
static void *do_smb_super_data_conv(void *raw_data)
{
struct smb_mount_data *s = (struct smb_mount_data *)raw_data;
struct smb_mount_data32 *s32 = (struct smb_mount_data32 *)raw_data;
s->version = s32->version;
s->mounted_uid = low2highuid(s32->mounted_uid);
s->uid = low2highuid(s32->uid);
s->gid = low2highgid(s32->gid);
s->file_mode = s32->file_mode;
s->dir_mode = s32->dir_mode;
return raw_data;
}
static int copy_mount_stuff_to_kernel(const void *user, unsigned long *kernel)
{
int i;
unsigned long page;
struct vm_area_struct *vma;
*kernel = 0;
if(!user)
return 0;
vma = find_vma(current->mm, (unsigned long)user);
if(!vma || (unsigned long)user < vma->vm_start)
return -EFAULT;
if(!(vma->vm_flags & VM_READ))
return -EFAULT;
i = vma->vm_end - (unsigned long) user;
if(PAGE_SIZE <= (unsigned long) i)
i = PAGE_SIZE - 1;
if(!(page = __get_free_page(GFP_KERNEL)))
return -ENOMEM;
if(copy_from_user((void *) page, user, i)) {
free_page(page);
return -EFAULT;
}
*kernel = page;
return 0;
}
#define SMBFS_NAME "smbfs"
#define NCPFS_NAME "ncpfs"
asmlinkage int sys32_mount(char *dev_name, char *dir_name, char *type, unsigned long new_flags, u32 data)
{
unsigned long type_page = 0;
unsigned long data_page = 0;
unsigned long dev_page = 0;
unsigned long dir_page = 0;
int err, is_smb, is_ncp;
is_smb = is_ncp = 0;
err = copy_mount_stuff_to_kernel((const void *)type, &type_page);
if (err)
goto out;
if (!type_page) {
err = -EINVAL;
goto out;
}
is_smb = !strcmp((char *)type_page, SMBFS_NAME);
is_ncp = !strcmp((char *)type_page, NCPFS_NAME);
err = copy_mount_stuff_to_kernel((const void *)AA(data), &data_page);
if (err)
goto type_out;
err = copy_mount_stuff_to_kernel(dev_name, &dev_page);
if (err)
goto data_out;
err = copy_mount_stuff_to_kernel(dir_name, &dir_page);
if (err)
goto dev_out;
if (!is_smb && !is_ncp) {
lock_kernel();
err = do_mount((char*)dev_page, (char*)dir_page,
(char*)type_page, new_flags, (char*)data_page);
unlock_kernel();
} else {
if (is_ncp)
do_ncp_super_data_conv((void *)data_page);
else
do_smb_super_data_conv((void *)data_page);
lock_kernel();
err = do_mount((char*)dev_page, (char*)dir_page,
(char*)type_page, new_flags, (char*)data_page);
unlock_kernel();
}
free_page(dir_page);
dev_out:
free_page(dev_page);
data_out:
free_page(data_page);
type_out:
free_page(type_page);
out:
return err;
}
struct rusage32 {
struct timeval32 ru_utime;
struct timeval32 ru_stime;
s32 ru_maxrss;
s32 ru_ixrss;
s32 ru_idrss;
s32 ru_isrss;
s32 ru_minflt;
s32 ru_majflt;
s32 ru_nswap;
s32 ru_inblock;
s32 ru_oublock;
s32 ru_msgsnd;
s32 ru_msgrcv;
s32 ru_nsignals;
s32 ru_nvcsw;
s32 ru_nivcsw;
};
static int put_rusage (struct rusage32 *ru, struct rusage *r)
{
int err;
err = put_user (r->ru_utime.tv_sec, &ru->ru_utime.tv_sec);
err |= __put_user (r->ru_utime.tv_usec, &ru->ru_utime.tv_usec);
err |= __put_user (r->ru_stime.tv_sec, &ru->ru_stime.tv_sec);
err |= __put_user (r->ru_stime.tv_usec, &ru->ru_stime.tv_usec);
err |= __put_user (r->ru_maxrss, &ru->ru_maxrss);
err |= __put_user (r->ru_ixrss, &ru->ru_ixrss);
err |= __put_user (r->ru_idrss, &ru->ru_idrss);
err |= __put_user (r->ru_isrss, &ru->ru_isrss);
err |= __put_user (r->ru_minflt, &ru->ru_minflt);
err |= __put_user (r->ru_majflt, &ru->ru_majflt);
err |= __put_user (r->ru_nswap, &ru->ru_nswap);
err |= __put_user (r->ru_inblock, &ru->ru_inblock);
err |= __put_user (r->ru_oublock, &ru->ru_oublock);
err |= __put_user (r->ru_msgsnd, &ru->ru_msgsnd);
err |= __put_user (r->ru_msgrcv, &ru->ru_msgrcv);
err |= __put_user (r->ru_nsignals, &ru->ru_nsignals);
err |= __put_user (r->ru_nvcsw, &ru->ru_nvcsw);
err |= __put_user (r->ru_nivcsw, &ru->ru_nivcsw);
return err;
}
asmlinkage int sys32_wait4(__kernel_pid_t32 pid, unsigned int *stat_addr, int options, struct rusage32 *ru)
{
if (!ru)
return sys_wait4(pid, stat_addr, options, NULL);
else {
struct rusage r;
int ret;
unsigned int status;
mm_segment_t old_fs = get_fs();
set_fs (KERNEL_DS);
ret = sys_wait4(pid, stat_addr ? &status : NULL, options, &r);
set_fs (old_fs);
if (put_rusage (ru, &r)) return -EFAULT;
if (stat_addr && put_user (status, stat_addr))
return -EFAULT;
return ret;
}
}
struct sysinfo32 {
s32 uptime;
u32 loads[3];
u32 totalram;
u32 freeram;
u32 sharedram;
u32 bufferram;
u32 totalswap;
u32 freeswap;
unsigned short procs;
unsigned short pad;
u32 totalhigh;
u32 freehigh;
unsigned int mem_unit;
char _f[8];
};
extern asmlinkage int sys_sysinfo(struct sysinfo *info);
asmlinkage int sys32_sysinfo(struct sysinfo32 *info)
{
struct sysinfo s;
int ret, err;
mm_segment_t old_fs = get_fs ();
set_fs (KERNEL_DS);
ret = sys_sysinfo(&s);
set_fs (old_fs);
err = put_user (s.uptime, &info->uptime);
err |= __put_user (s.loads[0], &info->loads[0]);
err |= __put_user (s.loads[1], &info->loads[1]);
err |= __put_user (s.loads[2], &info->loads[2]);
err |= __put_user (s.totalram, &info->totalram);
err |= __put_user (s.freeram, &info->freeram);
err |= __put_user (s.sharedram, &info->sharedram);
err |= __put_user (s.bufferram, &info->bufferram);
err |= __put_user (s.totalswap, &info->totalswap);
err |= __put_user (s.freeswap, &info->freeswap);
err |= __put_user (s.procs, &info->procs);
err |= __put_user (s.totalhigh, &info->totalhigh);
err |= __put_user (s.freehigh, &info->freehigh);
err |= __put_user (s.mem_unit, &info->mem_unit);
if (err)
return -EFAULT;
return ret;
}
struct timespec32 {
s32 tv_sec;
s32 tv_nsec;
};
extern asmlinkage int sys_sched_rr_get_interval(pid_t pid, struct timespec *interval);
asmlinkage int sys32_sched_rr_get_interval(__kernel_pid_t32 pid, struct timespec32 *interval)
{
struct timespec t;
int ret;
mm_segment_t old_fs = get_fs ();
set_fs (KERNEL_DS);
ret = sys_sched_rr_get_interval(pid, &t);
set_fs (old_fs);
if (put_user (t.tv_sec, &interval->tv_sec) ||
__put_user (t.tv_nsec, &interval->tv_nsec))
return -EFAULT;
return ret;
}
extern asmlinkage int sys_nanosleep(struct timespec *rqtp, struct timespec *rmtp);
asmlinkage int sys32_nanosleep(struct timespec32 *rqtp, struct timespec32 *rmtp)
{
struct timespec t;
int ret;
mm_segment_t old_fs = get_fs ();
if (get_user (t.tv_sec, &rqtp->tv_sec) ||
__get_user (t.tv_nsec, &rqtp->tv_nsec))
return -EFAULT;
set_fs (KERNEL_DS);
ret = sys_nanosleep(&t, rmtp ? &t : NULL);
set_fs (old_fs);
if (rmtp && ret == -EINTR) {
if (__put_user (t.tv_sec, &rmtp->tv_sec) ||
__put_user (t.tv_nsec, &rmtp->tv_nsec))
return -EFAULT;
}
return ret;
}
extern asmlinkage int sys_sigprocmask(int how, old_sigset_t *set, old_sigset_t *oset);
asmlinkage int sys32_sigprocmask(int how, old_sigset_t32 *set, old_sigset_t32 *oset)
{
old_sigset_t s;
int ret;
mm_segment_t old_fs = get_fs();
if (set && get_user (s, set)) return -EFAULT;
set_fs (KERNEL_DS);
ret = sys_sigprocmask(how, set ? &s : NULL, oset ? &s : NULL);
set_fs (old_fs);
if (ret) return ret;
if (oset && put_user (s, oset)) return -EFAULT;
return 0;
}
extern asmlinkage int sys_rt_sigprocmask(int how, sigset_t *set, sigset_t *oset, size_t sigsetsize);
asmlinkage int sys32_rt_sigprocmask(int how, sigset_t32 *set, sigset_t32 *oset, __kernel_size_t32 sigsetsize)
{
sigset_t s;
sigset_t32 s32;
int ret;
mm_segment_t old_fs = get_fs();
if (set) {
if (copy_from_user (&s32, set, sizeof(sigset_t32)))
return -EFAULT;
switch (_NSIG_WORDS) {
case 4: s.sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
case 3: s.sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
case 2: s.sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
case 1: s.sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
}
}
set_fs (KERNEL_DS);
ret = sys_rt_sigprocmask(how, set ? &s : NULL, oset ? &s : NULL, sigsetsize);
set_fs (old_fs);
if (ret) return ret;
if (oset) {
switch (_NSIG_WORDS) {
case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
}
if (copy_to_user (oset, &s32, sizeof(sigset_t32)))
return -EFAULT;
}
return 0;
}
extern asmlinkage int sys_sigpending(old_sigset_t *set);
asmlinkage int sys32_sigpending(old_sigset_t32 *set)
{
old_sigset_t s;
int ret;
mm_segment_t old_fs = get_fs();
set_fs (KERNEL_DS);
ret = sys_sigpending(&s);
set_fs (old_fs);
if (put_user (s, set)) return -EFAULT;
return ret;
}
extern asmlinkage int sys_rt_sigpending(sigset_t *set, size_t sigsetsize);
asmlinkage int sys32_rt_sigpending(sigset_t32 *set, __kernel_size_t32 sigsetsize)
{
sigset_t s;
sigset_t32 s32;
int ret;
mm_segment_t old_fs = get_fs();
set_fs (KERNEL_DS);
ret = sys_rt_sigpending(&s, sigsetsize);
set_fs (old_fs);
if (!ret) {
switch (_NSIG_WORDS) {
case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
}
if (copy_to_user (set, &s32, sizeof(sigset_t32)))
return -EFAULT;
}
return ret;
}
extern int
copy_siginfo_to_user32(siginfo_t32 *to, siginfo_t *from);
asmlinkage int
sys32_rt_sigtimedwait(sigset_t32 *uthese, siginfo_t32 *uinfo,
struct timespec32 *uts, __kernel_size_t32 sigsetsize)
{
int ret, sig;
sigset_t these;
sigset_t32 these32;
struct timespec ts;
siginfo_t info;
long timeout = 0;
/* XXX: Don't preclude handling different sized sigset_t's. */
if (sigsetsize != sizeof(sigset_t))
return -EINVAL;
if (copy_from_user (&these32, uthese, sizeof(sigset_t32)))
return -EFAULT;
switch (_NSIG_WORDS) {
case 4: these.sig[3] = these32.sig[6] | (((long)these32.sig[7]) << 32);
case 3: these.sig[2] = these32.sig[4] | (((long)these32.sig[5]) << 32);
case 2: these.sig[1] = these32.sig[2] | (((long)these32.sig[3]) << 32);
case 1: these.sig[0] = these32.sig[0] | (((long)these32.sig[1]) << 32);
}
/*
* Invert the set of allowed signals to get those we
* want to block.
*/
sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
signotset(&these);
if (uts) {
if (get_user (ts.tv_sec, &uts->tv_sec) ||
get_user (ts.tv_nsec, &uts->tv_nsec))
return -EINVAL;
if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
|| ts.tv_sec < 0)
return -EINVAL;
}
spin_lock_irq(&current->sigmask_lock);
sig = dequeue_signal(&these, &info);
if (!sig) {
/* None ready -- temporarily unblock those we're interested
in so that we'll be awakened when they arrive. */
sigset_t oldblocked = current->blocked;
sigandsets(&current->blocked, &current->blocked, &these);
recalc_sigpending(current);
spin_unlock_irq(&current->sigmask_lock);
timeout = MAX_SCHEDULE_TIMEOUT;
if (uts)
timeout = (timespec_to_jiffies(&ts)
+ (ts.tv_sec || ts.tv_nsec));
current->state = TASK_INTERRUPTIBLE;
timeout = schedule_timeout(timeout);
spin_lock_irq(&current->sigmask_lock);
sig = dequeue_signal(&these, &info);
current->blocked = oldblocked;
recalc_sigpending(current);
}
spin_unlock_irq(&current->sigmask_lock);
if (sig) {
ret = sig;
if (uinfo) {
if (copy_siginfo_to_user32(uinfo, &info))
ret = -EFAULT;
}
} else {
ret = -EAGAIN;
if (timeout)
ret = -EINTR;
}
return ret;
}
extern asmlinkage int
sys_rt_sigqueueinfo(int pid, int sig, siginfo_t *uinfo);
asmlinkage int
sys32_rt_sigqueueinfo(int pid, int sig, siginfo_t32 *uinfo)
{
siginfo_t info;
int ret;
mm_segment_t old_fs = get_fs();
if (copy_from_user (&info, uinfo, 3*sizeof(int)) ||
copy_from_user (info._sifields._pad, uinfo->_sifields._pad, SI_PAD_SIZE))
return -EFAULT;
set_fs (KERNEL_DS);
ret = sys_rt_sigqueueinfo(pid, sig, &info);
set_fs (old_fs);
return ret;
}
struct tms32 {
__kernel_clock_t32 tms_utime;
__kernel_clock_t32 tms_stime;
__kernel_clock_t32 tms_cutime;
__kernel_clock_t32 tms_cstime;
};
extern asmlinkage long sys_times(struct tms * tbuf);
asmlinkage long sys32_times(struct tms32 *tbuf)
{
struct tms t;
long ret;
mm_segment_t old_fs = get_fs ();
int err;
set_fs (KERNEL_DS);
ret = sys_times(tbuf ? &t : NULL);
set_fs (old_fs);
if (tbuf) {
err = put_user (t.tms_utime, &tbuf->tms_utime);
err |= __put_user (t.tms_stime, &tbuf->tms_stime);
err |= __put_user (t.tms_cutime, &tbuf->tms_cutime);
err |= __put_user (t.tms_cstime, &tbuf->tms_cstime);
if (err)
ret = -EFAULT;
}
return ret;
}
#define RLIM_OLD_INFINITY32 0x7fffffff
#define RLIM_INFINITY32 0xffffffff
#define RESOURCE32_OLD(x) ((x > RLIM_OLD_INFINITY32) ? RLIM_OLD_INFINITY32 : x)
#define RESOURCE32(x) ((x > RLIM_INFINITY32) ? RLIM_INFINITY32 : x)
struct rlimit32 {
u32 rlim_cur;
u32 rlim_max;
};
extern asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit *rlim);
asmlinkage int sys32_old_getrlimit(unsigned int resource, struct rlimit32 *rlim)
{
struct rlimit r;
int ret;
mm_segment_t old_fs = get_fs ();
set_fs (KERNEL_DS);
ret = sys_getrlimit(resource, &r);
set_fs (old_fs);
if (!ret) {
ret = put_user (RESOURCE32_OLD(r.rlim_cur), &rlim->rlim_cur);
ret |= __put_user (RESOURCE32_OLD(r.rlim_max), &rlim->rlim_max);
}
return ret;
}
asmlinkage int sys32_getrlimit(unsigned int resource, struct rlimit32 *rlim)
{
struct rlimit r;
int ret;
mm_segment_t old_fs = get_fs ();
set_fs (KERNEL_DS);
ret = sys_getrlimit(resource, &r);
set_fs (old_fs);
if (!ret) {
ret = put_user (RESOURCE32(r.rlim_cur), &rlim->rlim_cur);
ret |= __put_user (RESOURCE32(r.rlim_max), &rlim->rlim_max);
}
return ret;
}
extern asmlinkage int sys_setrlimit(unsigned int resource, struct rlimit *rlim);
asmlinkage int sys32_setrlimit(unsigned int resource, struct rlimit32 *rlim)
{
struct rlimit r;
int ret;
mm_segment_t old_fs = get_fs ();
if (resource >= RLIM_NLIMITS) return -EINVAL;
if (get_user (r.rlim_cur, &rlim->rlim_cur) ||
__get_user (r.rlim_max, &rlim->rlim_max))
return -EFAULT;
if (r.rlim_cur == RLIM_INFINITY32)
r.rlim_cur = RLIM_INFINITY;
if (r.rlim_max == RLIM_INFINITY32)
r.rlim_max = RLIM_INFINITY;
set_fs (KERNEL_DS);
ret = sys_setrlimit(resource, &r);
set_fs (old_fs);
return ret;
}
extern asmlinkage int sys_getrusage(int who, struct rusage *ru);
asmlinkage int sys32_getrusage(int who, struct rusage32 *ru)
{
struct rusage r;
int ret;
mm_segment_t old_fs = get_fs();
set_fs (KERNEL_DS);
ret = sys_getrusage(who, &r);
set_fs (old_fs);
if (put_rusage (ru, &r)) return -EFAULT;
return ret;
}
/* XXX This really belongs in some header file... -DaveM */
#define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
16 for IP, 16 for IPX,
24 for IPv6,
about 80 for AX.25 */
extern struct socket *sockfd_lookup(int fd, int *err);
/* XXX This as well... */
extern __inline__ void sockfd_put(struct socket *sock)
{
fput(sock->file);
}
struct msghdr32 {
u32 msg_name;
int msg_namelen;
u32 msg_iov;
__kernel_size_t32 msg_iovlen;
u32 msg_control;
__kernel_size_t32 msg_controllen;
unsigned msg_flags;
};
struct cmsghdr32 {
__kernel_size_t32 cmsg_len;
int cmsg_level;
int cmsg_type;
};
/* Bleech... */
#define __CMSG32_NXTHDR(ctl, len, cmsg, cmsglen) __cmsg32_nxthdr((ctl),(len),(cmsg),(cmsglen))
#define CMSG32_NXTHDR(mhdr, cmsg, cmsglen) cmsg32_nxthdr((mhdr), (cmsg), (cmsglen))
#define CMSG32_ALIGN(len) ( ((len)+sizeof(int)-1) & ~(sizeof(int)-1) )
#define CMSG32_DATA(cmsg) ((void *)((char *)(cmsg) + CMSG32_ALIGN(sizeof(struct cmsghdr32))))
#define CMSG32_SPACE(len) (CMSG32_ALIGN(sizeof(struct cmsghdr32)) + CMSG32_ALIGN(len))
#define CMSG32_LEN(len) (CMSG32_ALIGN(sizeof(struct cmsghdr32)) + (len))
#define __CMSG32_FIRSTHDR(ctl,len) ((len) >= sizeof(struct cmsghdr32) ? \
(struct cmsghdr32 *)(ctl) : \
(struct cmsghdr32 *)NULL)
#define CMSG32_FIRSTHDR(msg) __CMSG32_FIRSTHDR((msg)->msg_control, (msg)->msg_controllen)
#define CMSG32_OK(ucmlen, ucmsg, mhdr) \
((ucmlen) >= sizeof(struct cmsghdr32) && \
(ucmlen) <= (unsigned long) \
((mhdr)->msg_controllen - \
((char *)(ucmsg) - (char *)(mhdr)->msg_control)))
__inline__ struct cmsghdr32 *__cmsg32_nxthdr(void *__ctl, __kernel_size_t __size,
struct cmsghdr32 *__cmsg, int __cmsg_len)
{
struct cmsghdr32 * __ptr;
__ptr = (struct cmsghdr32 *)(((unsigned char *) __cmsg) +
CMSG32_ALIGN(__cmsg_len));
if ((unsigned long)((char*)(__ptr+1) - (char *) __ctl) > __size)
return NULL;
return __ptr;
}
__inline__ struct cmsghdr32 *cmsg32_nxthdr (struct msghdr *__msg,
struct cmsghdr32 *__cmsg,
int __cmsg_len)
{
return __cmsg32_nxthdr(__msg->msg_control, __msg->msg_controllen,
__cmsg, __cmsg_len);
}
static inline int iov_from_user32_to_kern(struct iovec *kiov,
struct iovec32 *uiov32,
int niov)
{
int tot_len = 0;
while(niov > 0) {
u32 len, buf;
if(get_user(len, &uiov32->iov_len) ||
get_user(buf, &uiov32->iov_base)) {
tot_len = -EFAULT;
break;
}
tot_len += len;
kiov->iov_base = (void *)A(buf);
kiov->iov_len = (__kernel_size_t) len;
uiov32++;
kiov++;
niov--;
}
return tot_len;
}
static inline int msghdr_from_user32_to_kern(struct msghdr *kmsg,
struct msghdr32 *umsg)
{
u32 tmp1, tmp2, tmp3;
int err;
err = get_user(tmp1, &umsg->msg_name);
err |= __get_user(tmp2, &umsg->msg_iov);
err |= __get_user(tmp3, &umsg->msg_control);
if (err)
return -EFAULT;
kmsg->msg_name = (void *)A(tmp1);
kmsg->msg_iov = (struct iovec *)A(tmp2);
kmsg->msg_control = (void *)A(tmp3);
err = get_user(kmsg->msg_namelen, &umsg->msg_namelen);
err |= get_user(kmsg->msg_iovlen, &umsg->msg_iovlen);
err |= get_user(kmsg->msg_controllen, &umsg->msg_controllen);
err |= get_user(kmsg->msg_flags, &umsg->msg_flags);
return err;
}
/* I've named the args so it is easy to tell whose space the pointers are in. */
static int verify_iovec32(struct msghdr *kern_msg, struct iovec *kern_iov,
char *kern_address, int mode)
{
int tot_len;
if(kern_msg->msg_namelen) {
if(mode==VERIFY_READ) {
int err = move_addr_to_kernel(kern_msg->msg_name,
kern_msg->msg_namelen,
kern_address);
if(err < 0)
return err;
}
kern_msg->msg_name = kern_address;
} else
kern_msg->msg_name = NULL;
if(kern_msg->msg_iovlen > UIO_FASTIOV) {
kern_iov = kmalloc(kern_msg->msg_iovlen * sizeof(struct iovec),
GFP_KERNEL);
if(!kern_iov)
return -ENOMEM;
}
tot_len = iov_from_user32_to_kern(kern_iov,
(struct iovec32 *)kern_msg->msg_iov,
kern_msg->msg_iovlen);
if(tot_len >= 0)
kern_msg->msg_iov = kern_iov;
else if(kern_msg->msg_iovlen > UIO_FASTIOV)
kfree(kern_iov);
return tot_len;
}
/* There is a lot of hair here because the alignment rules (and
* thus placement) of cmsg headers and length are different for
* 32-bit apps. -DaveM
*/
static int cmsghdr_from_user32_to_kern(struct msghdr *kmsg,
unsigned char *stackbuf, int stackbuf_size)
{
struct cmsghdr32 *ucmsg;
struct cmsghdr *kcmsg, *kcmsg_base;
__kernel_size_t32 ucmlen;
__kernel_size_t kcmlen, tmp;
int err = -EFAULT;
kcmlen = 0;
kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf;
ucmsg = CMSG32_FIRSTHDR(kmsg);
while(ucmsg != NULL) {
if (get_user(ucmlen, &ucmsg->cmsg_len))
return -EFAULT;
/* Catch bogons. */
if (!CMSG32_OK(ucmlen, ucmsg, kmsg))
return -EINVAL;
tmp = ((ucmlen - CMSG32_ALIGN(sizeof(*ucmsg))) +
CMSG_ALIGN(sizeof(struct cmsghdr)));
tmp = CMSG_ALIGN(tmp);
kcmlen += tmp;
ucmsg = CMSG32_NXTHDR(kmsg, ucmsg, ucmlen);
}
if(kcmlen == 0)
return -EINVAL;
/* The kcmlen holds the 64-bit version of the control length.
* It may not be modified as we do not stick it into the kmsg
* until we have successfully copied over all of the data
* from the user.
*/
if(kcmlen > stackbuf_size)
kcmsg_base = kcmsg = kmalloc(kcmlen, GFP_KERNEL);
if(kcmsg == NULL)
return -ENOBUFS;
/* Now copy them over neatly. */
memset(kcmsg, 0, kcmlen);
ucmsg = CMSG32_FIRSTHDR(kmsg);
while(ucmsg != NULL) {
if (__get_user(ucmlen, &ucmsg->cmsg_len))
goto Efault;
tmp = ((ucmlen - CMSG32_ALIGN(sizeof(*ucmsg))) +
CMSG_ALIGN(sizeof(struct cmsghdr)));
if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp))
goto Einval;
kcmsg->cmsg_len = tmp;
tmp = CMSG_ALIGN(tmp);
if (__get_user(kcmsg->cmsg_level, &ucmsg->cmsg_level) ||
__get_user(kcmsg->cmsg_type, &ucmsg->cmsg_type) ||
copy_from_user(CMSG_DATA(kcmsg),
CMSG32_DATA(ucmsg),
(ucmlen - CMSG32_ALIGN(sizeof(*ucmsg)))))
goto Efault;
/* Advance. */
kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp);
ucmsg = CMSG32_NXTHDR(kmsg, ucmsg, ucmlen);
}
/* Ok, looks like we made it. Hook it up and return success. */
kmsg->msg_control = kcmsg_base;
kmsg->msg_controllen = kcmlen;
return 0;
Einval:
err = -EINVAL;
Efault:
if (kcmsg_base != (struct cmsghdr *)stackbuf)
kfree(kcmsg_base);
return err;
}
static void put_cmsg32(struct msghdr *kmsg, int level, int type,
int len, void *data)
{
struct cmsghdr32 *cm = (struct cmsghdr32 *) kmsg->msg_control;
struct cmsghdr32 cmhdr;
int cmlen = CMSG32_LEN(len);
if(cm == NULL || kmsg->msg_controllen < sizeof(*cm)) {
kmsg->msg_flags |= MSG_CTRUNC;
return;
}
if(kmsg->msg_controllen < cmlen) {
kmsg->msg_flags |= MSG_CTRUNC;
cmlen = kmsg->msg_controllen;
}
cmhdr.cmsg_level = level;
cmhdr.cmsg_type = type;
cmhdr.cmsg_len = cmlen;
if(copy_to_user(cm, &cmhdr, sizeof cmhdr))
return;
if(copy_to_user(CMSG32_DATA(cm), data, cmlen - sizeof(struct cmsghdr32)))
return;
cmlen = CMSG32_SPACE(len);
kmsg->msg_control += cmlen;
kmsg->msg_controllen -= cmlen;
}
static void scm_detach_fds32(struct msghdr *kmsg, struct scm_cookie *scm)
{
struct cmsghdr32 *cm = (struct cmsghdr32 *) kmsg->msg_control;
int fdmax = (kmsg->msg_controllen - sizeof(struct cmsghdr32)) / sizeof(int);
int fdnum = scm->fp->count;
struct file **fp = scm->fp->fp;
int *cmfptr;
int err = 0, i;
if (fdnum < fdmax)
fdmax = fdnum;
for (i = 0, cmfptr = (int *) CMSG32_DATA(cm); i < fdmax; i++, cmfptr++) {
int new_fd;
err = get_unused_fd();
if (err < 0)
break;
new_fd = err;
err = put_user(new_fd, cmfptr);
if (err) {
put_unused_fd(new_fd);
break;
}
/* Bump the usage count and install the file. */
get_file(fp[i]);
fd_install(new_fd, fp[i]);
}
if (i > 0) {
int cmlen = CMSG32_LEN(i * sizeof(int));
if (!err)
err = put_user(SOL_SOCKET, &cm->cmsg_level);
if (!err)
err = put_user(SCM_RIGHTS, &cm->cmsg_type);
if (!err)
err = put_user(cmlen, &cm->cmsg_len);
if (!err) {
cmlen = CMSG32_SPACE(i * sizeof(int));
kmsg->msg_control += cmlen;
kmsg->msg_controllen -= cmlen;
}
}
if (i < fdnum)
kmsg->msg_flags |= MSG_CTRUNC;
/*
* All of the files that fit in the message have had their
* usage counts incremented, so we just free the list.
*/
__scm_destroy(scm);
}
/* In these cases we (currently) can just copy to data over verbatim
* because all CMSGs created by the kernel have well defined types which
* have the same layout in both the 32-bit and 64-bit API. One must add
* some special cased conversions here if we start sending control messages
* with incompatible types.
*
* SCM_RIGHTS and SCM_CREDENTIALS are done by hand in recvmsg32 right after
* we do our work. The remaining cases are:
*
* SOL_IP IP_PKTINFO struct in_pktinfo 32-bit clean
* IP_TTL int 32-bit clean
* IP_TOS __u8 32-bit clean
* IP_RECVOPTS variable length 32-bit clean
* IP_RETOPTS variable length 32-bit clean
* (these last two are clean because the types are defined
* by the IPv4 protocol)
* IP_RECVERR struct sock_extended_err +
* struct sockaddr_in 32-bit clean
* SOL_IPV6 IPV6_RECVERR struct sock_extended_err +
* struct sockaddr_in6 32-bit clean
* IPV6_PKTINFO struct in6_pktinfo 32-bit clean
* IPV6_HOPLIMIT int 32-bit clean
* IPV6_FLOWINFO u32 32-bit clean
* IPV6_HOPOPTS ipv6 hop exthdr 32-bit clean
* IPV6_DSTOPTS ipv6 dst exthdr(s) 32-bit clean
* IPV6_RTHDR ipv6 routing exthdr 32-bit clean
* IPV6_AUTHHDR ipv6 auth exthdr 32-bit clean
*/
static void cmsg32_recvmsg_fixup(struct msghdr *kmsg,
unsigned long orig_cmsg_uptr, __kernel_size_t orig_cmsg_len)
{
unsigned char *workbuf, *wp;
unsigned long bufsz, space_avail;
struct cmsghdr *ucmsg;
bufsz = ((unsigned long)kmsg->msg_control) - orig_cmsg_uptr;
space_avail = kmsg->msg_controllen + bufsz;
wp = workbuf = kmalloc(bufsz, GFP_KERNEL);
if(workbuf == NULL)
goto fail;
/* To make this more sane we assume the kernel sends back properly
* formatted control messages. Because of how the kernel will truncate
* the cmsg_len for MSG_TRUNC cases, we need not check that case either.
*/
ucmsg = (struct cmsghdr *) orig_cmsg_uptr;
while(((unsigned long)ucmsg) <=
(((unsigned long)kmsg->msg_control) - sizeof(struct cmsghdr))) {
struct cmsghdr32 *kcmsg32 = (struct cmsghdr32 *) wp;
int clen64, clen32;
/* UCMSG is the 64-bit format CMSG entry in user-space.
* KCMSG32 is within the kernel space temporary buffer
* we use to convert into a 32-bit style CMSG.
*/
__get_user(kcmsg32->cmsg_len, &ucmsg->cmsg_len);
__get_user(kcmsg32->cmsg_level, &ucmsg->cmsg_level);
__get_user(kcmsg32->cmsg_type, &ucmsg->cmsg_type);
clen64 = kcmsg32->cmsg_len;
if ((clen64 < CMSG_ALIGN(sizeof(*ucmsg))) ||
(clen64 > (orig_cmsg_len + wp - workbuf)))
break;
copy_from_user(CMSG32_DATA(kcmsg32), CMSG_DATA(ucmsg),
clen64 - CMSG_ALIGN(sizeof(*ucmsg)));
clen32 = ((clen64 - CMSG_ALIGN(sizeof(*ucmsg))) +
CMSG32_ALIGN(sizeof(struct cmsghdr32)));
kcmsg32->cmsg_len = clen32;
switch (kcmsg32->cmsg_type) {
/*
* The timestamp type's data needs to be converted
* from 64-bit time values to 32-bit time values
*/
case SO_TIMESTAMP: {
__kernel_time_t32* ptr_time32 = CMSG32_DATA(kcmsg32);
__kernel_time_t* ptr_time = CMSG_DATA(ucmsg);
get_user(*ptr_time32, ptr_time);
get_user(*(ptr_time32+1), ptr_time+1);
kcmsg32->cmsg_len -= 2*(sizeof(__kernel_time_t) -
sizeof(__kernel_time_t32));
}
default:;
}
ucmsg = (struct cmsghdr *) (((char *)ucmsg) + CMSG_ALIGN(clen64));
wp = (((char *)kcmsg32) + CMSG32_ALIGN(kcmsg32->cmsg_len));
}
/* Copy back fixed up data, and adjust pointers. */
bufsz = (wp - workbuf);
copy_to_user((void *)orig_cmsg_uptr, workbuf, bufsz);
kmsg->msg_control = (struct cmsghdr *)
(((char *)orig_cmsg_uptr) + bufsz);
kmsg->msg_controllen = space_avail - bufsz;
kfree(workbuf);
return;
fail:
/* If we leave the 64-bit format CMSG chunks in there,
* the application could get confused and crash. So to
* ensure greater recovery, we report no CMSGs.
*/
kmsg->msg_controllen += bufsz;
kmsg->msg_control = (void *) orig_cmsg_uptr;
}
#if 0
asmlinkage int sys32_sendmsg(int fd, struct msghdr32 *user_msg, unsigned user_flags)
{
struct socket *sock;
char address[MAX_SOCK_ADDR];
struct iovec iov[UIO_FASTIOV];
unsigned char ctl[sizeof(struct cmsghdr) + 20];
unsigned char *ctl_buf = ctl;
struct msghdr kern_msg;
int err, total_len;
if(msghdr_from_user32_to_kern(&kern_msg, user_msg))
return -EFAULT;
if(kern_msg.msg_iovlen > UIO_MAXIOV)
return -EINVAL;
err = verify_iovec32(&kern_msg, iov, address, VERIFY_READ);
if (err < 0)
goto out;
total_len = err;
if(kern_msg.msg_controllen) {
err = cmsghdr_from_user32_to_kern(&kern_msg, ctl, sizeof(ctl));
if(err)
goto out_freeiov;
ctl_buf = kern_msg.msg_control;
}
kern_msg.msg_flags = user_flags;
sock = sockfd_lookup(fd, &err);
if (sock != NULL) {
if (sock->file->f_flags & O_NONBLOCK)
kern_msg.msg_flags |= MSG_DONTWAIT;
err = sock_sendmsg(sock, &kern_msg, total_len);
sockfd_put(sock);
}
/* N.B. Use kfree here, as kern_msg.msg_controllen might change? */
if(ctl_buf != ctl)
kfree(ctl_buf);
out_freeiov:
if(kern_msg.msg_iov != iov)
kfree(kern_msg.msg_iov);
out:
return err;
}
asmlinkage int sys32_recvmsg(int fd, struct msghdr32 *user_msg, unsigned int user_flags)
{
struct iovec iovstack[UIO_FASTIOV];
struct msghdr kern_msg;
char addr[MAX_SOCK_ADDR];
struct socket *sock;
struct iovec *iov = iovstack;
struct sockaddr *uaddr;
int *uaddr_len;
unsigned long cmsg_ptr;
int err, total_len, len = 0;
if(msghdr_from_user32_to_kern(&kern_msg, user_msg))
return -EFAULT;
if(kern_msg.msg_iovlen > UIO_MAXIOV)
return -EINVAL;
uaddr = kern_msg.msg_name;
uaddr_len = &user_msg->msg_namelen;
err = verify_iovec32(&kern_msg, iov, addr, VERIFY_WRITE);
if (err < 0)
goto out;