blob: ef53feb5ab458f9f9b62d01958bfe1fe725c1349 [file] [log] [blame]
<
/*
* Linux syscalls
*
* Copyright (c) 2003 Fabrice Bellard
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#define _ATFILE_SOURCE
#include "qemu/osdep.h"
#include "qemu/cutils.h"
#include "qemu/path.h"
#include "qemu/memfd.h"
#include "qemu/queue.h"
#include <elf.h>
#include <endian.h>
#include <grp.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include <sys/mount.h>
#include <sys/file.h>
#include <sys/fsuid.h>
#include <sys/personality.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/swap.h>
#include <linux/capability.h>
#include <sched.h>
#include <sys/timex.h>
#include <sys/socket.h>
#include <linux/sockios.h>
#include <sys/un.h>
#include <sys/uio.h>
#include <poll.h>
#include <sys/times.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/statfs.h>
#include <utime.h>
#include <sys/sysinfo.h>
#include <sys/signalfd.h>
//#include <sys/user.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <linux/wireless.h>
#include <linux/icmp.h>
#include <linux/icmpv6.h>
#include <linux/if_tun.h>
#include <linux/in6.h>
#include <linux/errqueue.h>
#include <linux/random.h>
#ifdef CONFIG_TIMERFD
#include <sys/timerfd.h>
#endif
#ifdef CONFIG_EVENTFD
#include <sys/eventfd.h>
#endif
#ifdef CONFIG_EPOLL
#include <sys/epoll.h>
#endif
#ifdef CONFIG_ATTR
#include "qemu/xattr.h"
#endif
#ifdef CONFIG_SENDFILE
#include <sys/sendfile.h>
#endif
#ifdef HAVE_SYS_KCOV_H
#include <sys/kcov.h>
#endif
#define termios host_termios
#define winsize host_winsize
#define termio host_termio
#define sgttyb host_sgttyb /* same as target */
#define tchars host_tchars /* same as target */
#define ltchars host_ltchars /* same as target */
#include <linux/termios.h>
#include <linux/unistd.h>
#include <linux/cdrom.h>
#include <linux/hdreg.h>
#include <linux/soundcard.h>
#include <linux/kd.h>
#include <linux/mtio.h>
#include <linux/fs.h>
#include <linux/fd.h>
#if defined(CONFIG_FIEMAP)
#include <linux/fiemap.h>
#endif
#include <linux/fb.h>
#if defined(CONFIG_USBFS)
#include <linux/usbdevice_fs.h>
#include <linux/usb/ch9.h>
#endif
#include <linux/vt.h>
#include <linux/dm-ioctl.h>
#include <linux/reboot.h>
#include <linux/route.h>
#include <linux/filter.h>
#include <linux/blkpg.h>
#include <netpacket/packet.h>
#include <linux/netlink.h>
#include <linux/if_alg.h>
#include <linux/rtc.h>
#include <sound/asound.h>
#ifdef HAVE_BTRFS_H
#include <linux/btrfs.h>
#endif
#ifdef HAVE_DRM_H
#include <libdrm/drm.h>
#include <libdrm/i915_drm.h>
#endif
#include "linux_loop.h"
#include "uname.h"
#include "qemu.h"
#include "user-internals.h"
#include "strace.h"
#include "signal-common.h"
#include "loader.h"
#include "user-mmap.h"
#include "user/safe-syscall.h"
#include "qemu/guest-random.h"
#include "qemu/selfmap.h"
#include "user/syscall-trace.h"
#include "special-errno.h"
#include "qapi/error.h"
#include "fd-trans.h"
#include "tcg/tcg.h"
#ifndef CLONE_IO
#define CLONE_IO 0x80000000 /* Clone io context */
#endif
/* We can't directly call the host clone syscall, because this will
* badly confuse libc (breaking mutexes, for example). So we must
* divide clone flags into:
* * flag combinations that look like pthread_create()
* * flag combinations that look like fork()
* * flags we can implement within QEMU itself
* * flags we can't support and will return an error for
*/
/* For thread creation, all these flags must be present; for
* fork, none must be present.
*/
#define CLONE_THREAD_FLAGS \
(CLONE_VM | CLONE_FS | CLONE_FILES | \
CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
/* These flags are ignored:
* CLONE_DETACHED is now ignored by the kernel;
* CLONE_IO is just an optimisation hint to the I/O scheduler
*/
#define CLONE_IGNORED_FLAGS \
(CLONE_DETACHED | CLONE_IO)
/* Flags for fork which we can implement within QEMU itself */
#define CLONE_OPTIONAL_FORK_FLAGS \
(CLONE_SETTLS | CLONE_PARENT_SETTID | \
CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
/* Flags for thread creation which we can implement within QEMU itself */
#define CLONE_OPTIONAL_THREAD_FLAGS \
(CLONE_SETTLS | CLONE_PARENT_SETTID | \
CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
#define CLONE_INVALID_FORK_FLAGS \
(~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
#define CLONE_INVALID_THREAD_FLAGS \
(~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
CLONE_IGNORED_FLAGS))
/* CLONE_VFORK is special cased early in do_fork(). The other flag bits
* have almost all been allocated. We cannot support any of
* CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
* CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
* The checks against the invalid thread masks above will catch these.
* (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
*/
/* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
* once. This exercises the codepaths for restart.
*/
//#define DEBUG_ERESTARTSYS
//#include <linux/msdos_fs.h>
#define VFAT_IOCTL_READDIR_BOTH \
_IOC(_IOC_READ, 'r', 1, (sizeof(struct linux_dirent) + 256) * 2)
#define VFAT_IOCTL_READDIR_SHORT \
_IOC(_IOC_READ, 'r', 2, (sizeof(struct linux_dirent) + 256) * 2)
#undef _syscall0
#undef _syscall1
#undef _syscall2
#undef _syscall3
#undef _syscall4
#undef _syscall5
#undef _syscall6
#define _syscall0(type,name) \
static type name (void) \
{ \
return syscall(__NR_##name); \
}
#define _syscall1(type,name,type1,arg1) \
static type name (type1 arg1) \
{ \
return syscall(__NR_##name, arg1); \
}
#define _syscall2(type,name,type1,arg1,type2,arg2) \
static type name (type1 arg1,type2 arg2) \
{ \
return syscall(__NR_##name, arg1, arg2); \
}
#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
static type name (type1 arg1,type2 arg2,type3 arg3) \
{ \
return syscall(__NR_##name, arg1, arg2, arg3); \
}
#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
{ \
return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
}
#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
type5,arg5) \
static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
{ \
return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
}
#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
type5,arg5,type6,arg6) \
static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
type6 arg6) \
{ \
return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
}
#define __NR_sys_uname __NR_uname
#define __NR_sys_getcwd1 __NR_getcwd
#define __NR_sys_getdents __NR_getdents
#define __NR_sys_getdents64 __NR_getdents64
#define __NR_sys_getpriority __NR_getpriority
#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
#define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
#define __NR_sys_syslog __NR_syslog
#if defined(__NR_futex)
# define __NR_sys_futex __NR_futex
#endif
#if defined(__NR_futex_time64)
# define __NR_sys_futex_time64 __NR_futex_time64
#endif
#define __NR_sys_statx __NR_statx
#if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
#define __NR__llseek __NR_lseek
#endif
/* Newer kernel ports have llseek() instead of _llseek() */
#if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
#define TARGET_NR__llseek TARGET_NR_llseek
#endif
/* some platforms need to mask more bits than just TARGET_O_NONBLOCK */
#ifndef TARGET_O_NONBLOCK_MASK
#define TARGET_O_NONBLOCK_MASK TARGET_O_NONBLOCK
#endif
#define __NR_sys_gettid __NR_gettid
_syscall0(int, sys_gettid)
/* For the 64-bit guest on 32-bit host case we must emulate
* getdents using getdents64, because otherwise the host
* might hand us back more dirent records than we can fit
* into the guest buffer after structure format conversion.
* Otherwise we emulate getdents with getdents if the host has it.
*/
#if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
#define EMULATE_GETDENTS_WITH_GETDENTS
#endif
#if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
_syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
#endif
#if (defined(TARGET_NR_getdents) && \
!defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
(defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
_syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
#endif
#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
loff_t *, res, uint, wh);
#endif
_syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo)
_syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig,
siginfo_t *, uinfo)
_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
#ifdef __NR_exit_group
_syscall1(int,exit_group,int,error_code)
#endif
#if defined(__NR_futex)
_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
const struct timespec *,timeout,int *,uaddr2,int,val3)
#endif
#if defined(__NR_futex_time64)
_syscall6(int,sys_futex_time64,int *,uaddr,int,op,int,val,
const struct timespec *,timeout,int *,uaddr2,int,val3)
#endif
#define __NR_sys_sched_getaffinity __NR_sched_getaffinity
_syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
unsigned long *, user_mask_ptr);
#define __NR_sys_sched_setaffinity __NR_sched_setaffinity
_syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
unsigned long *, user_mask_ptr);
/* sched_attr is not defined in glibc */
struct sched_attr {
uint32_t size;
uint32_t sched_policy;
uint64_t sched_flags;
int32_t sched_nice;
uint32_t sched_priority;
uint64_t sched_runtime;
uint64_t sched_deadline;
uint64_t sched_period;
uint32_t sched_util_min;
uint32_t sched_util_max;
};
#define __NR_sys_sched_getattr __NR_sched_getattr
_syscall4(int, sys_sched_getattr, pid_t, pid, struct sched_attr *, attr,
unsigned int, size, unsigned int, flags);
#define __NR_sys_sched_setattr __NR_sched_setattr
_syscall3(int, sys_sched_setattr, pid_t, pid, struct sched_attr *, attr,
unsigned int, flags);
#define __NR_sys_sched_getscheduler __NR_sched_getscheduler
_syscall1(int, sys_sched_getscheduler, pid_t, pid);
#define __NR_sys_sched_setscheduler __NR_sched_setscheduler
_syscall3(int, sys_sched_setscheduler, pid_t, pid, int, policy,
const struct sched_param *, param);
#define __NR_sys_sched_getparam __NR_sched_getparam
_syscall2(int, sys_sched_getparam, pid_t, pid,
struct sched_param *, param);
#define __NR_sys_sched_setparam __NR_sched_setparam
_syscall2(int, sys_sched_setparam, pid_t, pid,
const struct sched_param *, param);
#define __NR_sys_getcpu __NR_getcpu
_syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
void *, arg);
_syscall2(int, capget, struct __user_cap_header_struct *, header,
struct __user_cap_data_struct *, data);
_syscall2(int, capset, struct __user_cap_header_struct *, header,
struct __user_cap_data_struct *, data);
#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
_syscall2(int, ioprio_get, int, which, int, who)
#endif
#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
_syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
#endif
#if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
_syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
#endif
#if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
_syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
unsigned long, idx1, unsigned long, idx2)
#endif
/*
* It is assumed that struct statx is architecture independent.
*/
#if defined(TARGET_NR_statx) && defined(__NR_statx)
_syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags,
unsigned int, mask, struct target_statx *, statxbuf)
#endif
#if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
_syscall2(int, membarrier, int, cmd, int, flags)
#endif
static const bitmask_transtbl fcntl_flags_tbl[] = {
{ TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
{ TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
{ TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
{ TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
{ TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
{ TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
{ TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
{ TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
{ TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
{ TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
{ TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
{ TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
{ TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
#if defined(O_DIRECT)
{ TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
#endif
#if defined(O_NOATIME)
{ TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
#endif
#if defined(O_CLOEXEC)
{ TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
#endif
#if defined(O_PATH)
{ TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
#endif
#if defined(O_TMPFILE)
{ TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
#endif
/* Don't terminate the list prematurely on 64-bit host+guest. */
#if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
{ TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
#endif
{ 0, 0, 0, 0 }
};
_syscall2(int, sys_getcwd1, char *, buf, size_t, size)
#if defined(TARGET_NR_utimensat) || defined(TARGET_NR_utimensat_time64)
#if defined(__NR_utimensat)
#define __NR_sys_utimensat __NR_utimensat
_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
const struct timespec *,tsp,int,flags)
#else
static int sys_utimensat(int dirfd, const char *pathname,
const struct timespec times[2], int flags)
{
errno = ENOSYS;
return -1;
}
#endif
#endif /* TARGET_NR_utimensat */
#ifdef TARGET_NR_renameat2
#if defined(__NR_renameat2)
#define __NR_sys_renameat2 __NR_renameat2
_syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd,
const char *, new, unsigned int, flags)
#else
static int sys_renameat2(int oldfd, const char *old,
int newfd, const char *new, int flags)
{
if (flags == 0) {
return renameat(oldfd, old, newfd, new);
}
errno = ENOSYS;
return -1;
}
#endif
#endif /* TARGET_NR_renameat2 */
#ifdef CONFIG_INOTIFY
#include <sys/inotify.h>
#else
/* Userspace can usually survive runtime without inotify */
#undef TARGET_NR_inotify_init
#undef TARGET_NR_inotify_init1
#undef TARGET_NR_inotify_add_watch
#undef TARGET_NR_inotify_rm_watch
#endif /* CONFIG_INOTIFY */
#if defined(TARGET_NR_prlimit64)
#ifndef __NR_prlimit64
# define __NR_prlimit64 -1
#endif
#define __NR_sys_prlimit64 __NR_prlimit64
/* The glibc rlimit structure may not be that used by the underlying syscall */
struct host_rlimit64 {
uint64_t rlim_cur;
uint64_t rlim_max;
};
_syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
const struct host_rlimit64 *, new_limit,
struct host_rlimit64 *, old_limit)
#endif
#if defined(TARGET_NR_timer_create)
/* Maximum of 32 active POSIX timers allowed at any one time. */
static timer_t g_posix_timers[32] = { 0, } ;
static inline int next_free_host_timer(void)
{
int k ;
/* FIXME: Does finding the next free slot require a lock? */
for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
if (g_posix_timers[k] == 0) {
g_posix_timers[k] = (timer_t) 1;
return k;
}
}
return -1;
}
#endif
static inline int host_to_target_errno(int host_errno)
{
switch (host_errno) {
#define E(X) case X: return TARGET_##X;
#include "errnos.c.inc"
#undef E
default:
return host_errno;
}
}
static inline int target_to_host_errno(int target_errno)
{
switch (target_errno) {
#define E(X) case TARGET_##X: return X;
#include "errnos.c.inc"
#undef E
default:
return target_errno;
}
}
abi_long get_errno(abi_long ret)
{
if (ret == -1)
return -host_to_target_errno(errno);
else
return ret;
}
const char *target_strerror(int err)
{
if (err == QEMU_ERESTARTSYS) {
return "To be restarted";
}
if (err == QEMU_ESIGRETURN) {
return "Successful exit from sigreturn";
}
return strerror(target_to_host_errno(err));
}
static int check_zeroed_user(abi_long addr, size_t ksize, size_t usize)
{
int i;
uint8_t b;
if (usize <= ksize) {
return 1;
}
for (i = ksize; i < usize; i++) {
if (get_user_u8(b, addr + i)) {
return -TARGET_EFAULT;
}
if (b != 0) {
return 0;
}
}
return 1;
}
#define safe_syscall0(type, name) \
static type safe_##name(void) \
{ \
return safe_syscall(__NR_##name); \
}
#define safe_syscall1(type, name, type1, arg1) \
static type safe_##name(type1 arg1) \
{ \
return safe_syscall(__NR_##name, arg1); \
}
#define safe_syscall2(type, name, type1, arg1, type2, arg2) \
static type safe_##name(type1 arg1, type2 arg2) \
{ \
return safe_syscall(__NR_##name, arg1, arg2); \
}
#define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
{ \
return safe_syscall(__NR_##name, arg1, arg2, arg3); \
}
#define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
type4, arg4) \
static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
{ \
return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
}
#define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
type4, arg4, type5, arg5) \
static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
type5 arg5) \
{ \
return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
}
#define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
type4, arg4, type5, arg5, type6, arg6) \
static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
type5 arg5, type6 arg6) \
{ \
return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
}
safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
int, flags, mode_t, mode)
#if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
struct rusage *, rusage)
#endif
safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
int, options, struct rusage *, rusage)
safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
#endif
#if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64)
safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
struct timespec *, tsp, const sigset_t *, sigmask,
size_t, sigsetsize)
#endif
safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
int, maxevents, int, timeout, const sigset_t *, sigmask,
size_t, sigsetsize)
#if defined(__NR_futex)
safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
const struct timespec *,timeout,int *,uaddr2,int,val3)
#endif
#if defined(__NR_futex_time64)
safe_syscall6(int,futex_time64,int *,uaddr,int,op,int,val, \
const struct timespec *,timeout,int *,uaddr2,int,val3)
#endif
safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
safe_syscall2(int, kill, pid_t, pid, int, sig)
safe_syscall2(int, tkill, int, tid, int, sig)
safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
unsigned long, pos_l, unsigned long, pos_h)
safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
unsigned long, pos_l, unsigned long, pos_h)
safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
socklen_t, addrlen)
safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
safe_syscall2(int, flock, int, fd, int, operation)
#if defined(TARGET_NR_rt_sigtimedwait) || defined(TARGET_NR_rt_sigtimedwait_time64)
safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
const struct timespec *, uts, size_t, sigsetsize)
#endif
safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
int, flags)
#if defined(TARGET_NR_nanosleep)
safe_syscall2(int, nanosleep, const struct timespec *, req,
struct timespec *, rem)
#endif
#if defined(TARGET_NR_clock_nanosleep) || \
defined(TARGET_NR_clock_nanosleep_time64)
safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
const struct timespec *, req, struct timespec *, rem)
#endif
#ifdef __NR_ipc
#ifdef __s390x__
safe_syscall5(int, ipc, int, call, long, first, long, second, long, third,
void *, ptr)
#else
safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
void *, ptr, long, fifth)
#endif
#endif
#ifdef __NR_msgsnd
safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
int, flags)
#endif
#ifdef __NR_msgrcv
safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
long, msgtype, int, flags)
#endif
#ifdef __NR_semtimedop
safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
unsigned, nsops, const struct timespec *, timeout)
#endif
#if defined(TARGET_NR_mq_timedsend) || \
defined(TARGET_NR_mq_timedsend_time64)
safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
size_t, len, unsigned, prio, const struct timespec *, timeout)
#endif
#if defined(TARGET_NR_mq_timedreceive) || \
defined(TARGET_NR_mq_timedreceive_time64)
safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
size_t, len, unsigned *, prio, const struct timespec *, timeout)
#endif
#if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
safe_syscall6(ssize_t, copy_file_range, int, infd, loff_t *, pinoff,
int, outfd, loff_t *, poutoff, size_t, length,
unsigned int, flags)
#endif
/* We do ioctl like this rather than via safe_syscall3 to preserve the
* "third argument might be integer or pointer or not present" behaviour of
* the libc function.
*/
#define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
/* Similarly for fcntl. Note that callers must always:
* pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
* use the flock64 struct rather than unsuffixed flock
* This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
*/
#ifdef __NR_fcntl64
#define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
#else
#define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
#endif
static inline int host_to_target_sock_type(int host_type)
{
int target_type;
switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
case SOCK_DGRAM:
target_type = TARGET_SOCK_DGRAM;
break;
case SOCK_STREAM:
target_type = TARGET_SOCK_STREAM;
break;
default:
target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
break;
}
#if defined(SOCK_CLOEXEC)
if (host_type & SOCK_CLOEXEC) {
target_type |= TARGET_SOCK_CLOEXEC;
}
#endif
#if defined(SOCK_NONBLOCK)
if (host_type & SOCK_NONBLOCK) {
target_type |= TARGET_SOCK_NONBLOCK;
}
#endif
return target_type;
}
static abi_ulong target_brk;
static abi_ulong target_original_brk;
static abi_ulong brk_page;
void target_set_brk(abi_ulong new_brk)
{
target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
brk_page = HOST_PAGE_ALIGN(target_brk);
}
//#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
#define DEBUGF_BRK(message, args...)
/* do_brk() must return target values and target errnos. */
abi_long do_brk(abi_ulong new_brk)
{
abi_long mapped_addr;
abi_ulong new_alloc_size;
/* brk pointers are always untagged */
DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
if (!new_brk) {
DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
return target_brk;
}
if (new_brk < target_original_brk) {
DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
target_brk);
return target_brk;
}
/* If the new brk is less than the highest page reserved to the
* target heap allocation, set it and we're almost done... */
if (new_brk <= brk_page) {
/* Heap contents are initialized to zero, as for anonymous
* mapped pages. */
if (new_brk > target_brk) {
memset(g2h_untagged(target_brk), 0, new_brk - target_brk);
}
target_brk = new_brk;
DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
return target_brk;
}
/* We need to allocate more memory after the brk... Note that
* we don't use MAP_FIXED because that will map over the top of
* any existing mapping (like the one with the host libc or qemu
* itself); instead we treat "mapped but at wrong address" as
* a failure and unmap again.
*/
new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
PROT_READ|PROT_WRITE,
MAP_ANON|MAP_PRIVATE, 0, 0));
if (mapped_addr == brk_page) {
/* Heap contents are initialized to zero, as for anonymous
* mapped pages. Technically the new pages are already
* initialized to zero since they *are* anonymous mapped
* pages, however we have to take care with the contents that
* come from the remaining part of the previous page: it may
* contains garbage data due to a previous heap usage (grown
* then shrunken). */
memset(g2h_untagged(target_brk), 0, brk_page - target_brk);
target_brk = new_brk;
brk_page = HOST_PAGE_ALIGN(target_brk);
DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
target_brk);
return target_brk;
} else if (mapped_addr != -1) {
/* Mapped but at wrong address, meaning there wasn't actually
* enough space for this brk.
*/
target_munmap(mapped_addr, new_alloc_size);
mapped_addr = -1;
DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
}
else {
DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
}
#if defined(TARGET_ALPHA)
/* We (partially) emulate OSF/1 on Alpha, which requires we
return a proper errno, not an unchanged brk value. */
return -TARGET_ENOMEM;
#endif
/* For everything else, return the previous break. */
return target_brk;
}
#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
static inline abi_long copy_from_user_fdset(fd_set *fds,
abi_ulong target_fds_addr,
int n)
{
int i, nw, j, k;
abi_ulong b, *target_fds;
nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
if (!(target_fds = lock_user(VERIFY_READ,
target_fds_addr,
sizeof(abi_ulong) * nw,
1)))
return -TARGET_EFAULT;
FD_ZERO(fds);
k = 0;
for (i = 0; i < nw; i++) {
/* grab the abi_ulong */
__get_user(b, &target_fds[i]);
for (j = 0; j < TARGET_ABI_BITS; j++) {
/* check the bit inside the abi_ulong */
if ((b >> j) & 1)
FD_SET(k, fds);
k++;
}
}
unlock_user(target_fds, target_fds_addr, 0);
return 0;
}
static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
abi_ulong target_fds_addr,
int n)
{
if (target_fds_addr) {
if (copy_from_user_fdset(fds, target_fds_addr, n))
return -TARGET_EFAULT;
*fds_ptr = fds;
} else {
*fds_ptr = NULL;
}
return 0;
}
static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
const fd_set *fds,
int n)
{
int i, nw, j, k;
abi_long v;
abi_ulong *target_fds;
nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
if (!(target_fds = lock_user(VERIFY_WRITE,
target_fds_addr,
sizeof(abi_ulong) * nw,
0)))
return -TARGET_EFAULT;
k = 0;
for (i = 0; i < nw; i++) {
v = 0;
for (j = 0; j < TARGET_ABI_BITS; j++) {
v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
k++;
}
__put_user(v, &target_fds[i]);
}
unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
return 0;
}
#endif
#if defined(__alpha__)
#define HOST_HZ 1024
#else
#define HOST_HZ 100
#endif
static inline abi_long host_to_target_clock_t(long ticks)
{
#if HOST_HZ == TARGET_HZ
return ticks;
#else
return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
#endif
}
static inline abi_long host_to_target_rusage(abi_ulong target_addr,
const struct rusage *rusage)
{
struct target_rusage *target_rusage;
if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
return -TARGET_EFAULT;
target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
unlock_user_struct(target_rusage, target_addr, 1);
return 0;
}
#ifdef TARGET_NR_setrlimit
static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
{
abi_ulong target_rlim_swap;
rlim_t result;
target_rlim_swap = tswapal(target_rlim);
if (target_rlim_swap == TARGET_RLIM_INFINITY)
return RLIM_INFINITY;
result = target_rlim_swap;
if (target_rlim_swap != (rlim_t)result)
return RLIM_INFINITY;
return result;
}
#endif
#if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
static inline abi_ulong host_to_target_rlim(rlim_t rlim)
{
abi_ulong target_rlim_swap;
abi_ulong result;
if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
target_rlim_swap = TARGET_RLIM_INFINITY;
else
target_rlim_swap = rlim;
result = tswapal(target_rlim_swap);
return result;
}
#endif
static inline int target_to_host_resource(int code)
{
switch (code) {
case TARGET_RLIMIT_AS:
return RLIMIT_AS;
case TARGET_RLIMIT_CORE:
return RLIMIT_CORE;
case TARGET_RLIMIT_CPU:
return RLIMIT_CPU;
case TARGET_RLIMIT_DATA:
return RLIMIT_DATA;
case TARGET_RLIMIT_FSIZE:
return RLIMIT_FSIZE;
case TARGET_RLIMIT_LOCKS:
return RLIMIT_LOCKS;
case TARGET_RLIMIT_MEMLOCK:
return RLIMIT_MEMLOCK;
case TARGET_RLIMIT_MSGQUEUE:
return RLIMIT_MSGQUEUE;
case TARGET_RLIMIT_NICE:
return RLIMIT_NICE;
case TARGET_RLIMIT_NOFILE:
return RLIMIT_NOFILE;
case TARGET_RLIMIT_NPROC:
return RLIMIT_NPROC;
case TARGET_RLIMIT_RSS:
return RLIMIT_RSS;
case TARGET_RLIMIT_RTPRIO:
return RLIMIT_RTPRIO;
#ifdef RLIMIT_RTTIME
case TARGET_RLIMIT_RTTIME:
return RLIMIT_RTTIME;
#endif
case TARGET_RLIMIT_SIGPENDING:
return RLIMIT_SIGPENDING;
case TARGET_RLIMIT_STACK:
return RLIMIT_STACK;
default:
return code;
}
}
static inline abi_long copy_from_user_timeval(struct timeval *tv,
abi_ulong target_tv_addr)
{
struct target_timeval *target_tv;
if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
return -TARGET_EFAULT;
}
__get_user(tv->tv_sec, &target_tv->tv_sec);
__get_user(tv->tv_usec, &target_tv->tv_usec);
unlock_user_struct(target_tv, target_tv_addr, 0);
return 0;
}
static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
const struct timeval *tv)
{
struct target_timeval *target_tv;
if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
return -TARGET_EFAULT;
}
__put_user(tv->tv_sec, &target_tv->tv_sec);
__put_user(tv->tv_usec, &target_tv->tv_usec);
unlock_user_struct(target_tv, target_tv_addr, 1);
return 0;
}
#if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
static inline abi_long copy_from_user_timeval64(struct timeval *tv,
abi_ulong target_tv_addr)
{
struct target__kernel_sock_timeval *target_tv;
if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
return -TARGET_EFAULT;
}
__get_user(tv->tv_sec, &target_tv->tv_sec);
__get_user(tv->tv_usec, &target_tv->tv_usec);
unlock_user_struct(target_tv, target_tv_addr, 0);
return 0;
}
#endif
static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
const struct timeval *tv)
{
struct target__kernel_sock_timeval *target_tv;
if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
return -TARGET_EFAULT;
}
__put_user(tv->tv_sec, &target_tv->tv_sec);
__put_user(tv->tv_usec, &target_tv->tv_usec);
unlock_user_struct(target_tv, target_tv_addr, 1);
return 0;
}
#if defined(TARGET_NR_futex) || \
defined(TARGET_NR_rt_sigtimedwait) || \
defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
defined(TARGET_NR_timer_settime) || \
(defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
static inline abi_long target_to_host_timespec(struct timespec *host_ts,
abi_ulong target_addr)
{
struct target_timespec *target_ts;
if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
return -TARGET_EFAULT;
}
__get_user(host_ts->tv_sec, &target_ts->tv_sec);
__get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
unlock_user_struct(target_ts, target_addr, 0);
return 0;
}
#endif
#if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \
defined(TARGET_NR_timer_settime64) || \
defined(TARGET_NR_mq_timedsend_time64) || \
defined(TARGET_NR_mq_timedreceive_time64) || \
(defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) || \
defined(TARGET_NR_clock_nanosleep_time64) || \
defined(TARGET_NR_rt_sigtimedwait_time64) || \
defined(TARGET_NR_utimensat) || \
defined(TARGET_NR_utimensat_time64) || \
defined(TARGET_NR_semtimedop_time64) || \
defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64)
static inline abi_long target_to_host_timespec64(struct timespec *host_ts,
abi_ulong target_addr)
{
struct target__kernel_timespec *target_ts;
if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
return -TARGET_EFAULT;
}
__get_user(host_ts->tv_sec, &target_ts->tv_sec);
__get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
/* in 32bit mode, this drops the padding */
host_ts->tv_nsec = (long)(abi_long)host_ts->tv_nsec;
unlock_user_struct(target_ts, target_addr, 0);
return 0;
}
#endif
static inline abi_long host_to_target_timespec(abi_ulong target_addr,
struct timespec *host_ts)
{
struct target_timespec *target_ts;
if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
return -TARGET_EFAULT;
}
__put_user(host_ts->tv_sec, &target_ts->tv_sec);
__put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
unlock_user_struct(target_ts, target_addr, 1);
return 0;
}
static inline abi_long host_to_target_timespec64(abi_ulong target_addr,
struct timespec *host_ts)
{
struct target__kernel_timespec *target_ts;
if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
return -TARGET_EFAULT;
}
__put_user(host_ts->tv_sec, &target_ts->tv_sec);
__put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
unlock_user_struct(target_ts, target_addr, 1);
return 0;
}
#if defined(TARGET_NR_gettimeofday)
static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr,
struct timezone *tz)
{
struct target_timezone *target_tz;
if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) {
return -TARGET_EFAULT;
}
__put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
__put_user(tz->tz_dsttime, &target_tz->tz_dsttime);
unlock_user_struct(target_tz, target_tz_addr, 1);
return 0;
}
#endif
#if defined(TARGET_NR_settimeofday)
static inline abi_long copy_from_user_timezone(struct timezone *tz,
abi_ulong target_tz_addr)
{
struct target_timezone *target_tz;
if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
return -TARGET_EFAULT;
}
__get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
__get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
unlock_user_struct(target_tz, target_tz_addr, 0);
return 0;
}
#endif
#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
#include <mqueue.h>
static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
abi_ulong target_mq_attr_addr)
{
struct target_mq_attr *target_mq_attr;
if (!lock_user_struct(VERIFY_READ, target_mq_attr,
target_mq_attr_addr, 1))
return -TARGET_EFAULT;
__get_user(attr->mq_flags, &target_mq_attr->mq_flags);
__get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
__get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
__get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
return 0;
}
static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
const struct mq_attr *attr)
{
struct target_mq_attr *target_mq_attr;
if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
target_mq_attr_addr, 0))
return -TARGET_EFAULT;
__put_user(attr->mq_flags, &target_mq_attr->mq_flags);
__put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
__put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
__put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
return 0;
}
#endif
#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
/* do_select() must return target values and target errnos. */
static abi_long do_select(int n,
abi_ulong rfd_addr, abi_ulong wfd_addr,
abi_ulong efd_addr, abi_ulong target_tv_addr)
{
fd_set rfds, wfds, efds;
fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
struct timeval tv;
struct timespec ts, *ts_ptr;
abi_long ret;
ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
if (ret) {
return ret;
}
ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
if (ret) {
return ret;
}
ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
if (ret) {
return ret;
}
if (target_tv_addr) {
if (copy_from_user_timeval(&tv, target_tv_addr))
return -TARGET_EFAULT;
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec * 1000;
ts_ptr = &ts;
} else {
ts_ptr = NULL;
}
ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
ts_ptr, NULL));
if (!is_error(ret)) {
if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
return -TARGET_EFAULT;
if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
return -TARGET_EFAULT;
if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
return -TARGET_EFAULT;
if (target_tv_addr) {
tv.tv_sec = ts.tv_sec;
tv.tv_usec = ts.tv_nsec / 1000;
if (copy_to_user_timeval(target_tv_addr, &tv)) {
return -TARGET_EFAULT;
}
}
}
return ret;
}
#if defined(TARGET_WANT_OLD_SYS_SELECT)
static abi_long do_old_select(abi_ulong arg1)
{
struct target_sel_arg_struct *sel;
abi_ulong inp, outp, exp, tvp;
long nsel;
if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
return -TARGET_EFAULT;
}
nsel = tswapal(sel->n);
inp = tswapal(sel->inp);
outp = tswapal(sel->outp);
exp = tswapal(sel->exp);
tvp = tswapal(sel->tvp);
unlock_user_struct(sel, arg1, 0);
return do_select(nsel, inp, outp, exp, tvp);
}
#endif
#endif
#if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3,
abi_long arg4, abi_long arg5, abi_long arg6,
bool time64)
{
abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
fd_set rfds, wfds, efds;
fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
struct timespec ts, *ts_ptr;
abi_long ret;
/*
* The 6th arg is actually two args smashed together,
* so we cannot use the C library.
*/
struct {
sigset_t *set;
size_t size;
} sig, *sig_ptr;
abi_ulong arg_sigset, arg_sigsize, *arg7;
n = arg1;
rfd_addr = arg2;
wfd_addr = arg3;
efd_addr = arg4;
ts_addr = arg5;
ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
if (ret) {
return ret;
}
ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
if (ret) {
return ret;
}
ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
if (ret) {
return ret;
}
/*
* This takes a timespec, and not a timeval, so we cannot
* use the do_select() helper ...
*/
if (ts_addr) {
if (time64) {
if (target_to_host_timespec64(&ts, ts_addr)) {
return -TARGET_EFAULT;
}
} else {
if (target_to_host_timespec(&ts, ts_addr)) {
return -TARGET_EFAULT;
}
}
ts_ptr = &ts;
} else {
ts_ptr = NULL;
}
/* Extract the two packed args for the sigset */
sig_ptr = NULL;
if (arg6) {
arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
if (!arg7) {
return -TARGET_EFAULT;
}
arg_sigset = tswapal(arg7[0]);
arg_sigsize = tswapal(arg7[1]);
unlock_user(arg7, arg6, 0);
if (arg_sigset) {
ret = process_sigsuspend_mask(&sig.set, arg_sigset, arg_sigsize);
if (ret != 0) {
return ret;
}
sig_ptr = &sig;
sig.size = SIGSET_T_SIZE;
}
}
ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
ts_ptr, sig_ptr));
if (sig_ptr) {
finish_sigsuspend_mask(ret);
}
if (!is_error(ret)) {
if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) {
return -TARGET_EFAULT;
}
if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) {
return -TARGET_EFAULT;
}
if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) {
return -TARGET_EFAULT;
}
if (time64) {
if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) {
return -TARGET_EFAULT;
}
} else {
if (ts_addr && host_to_target_timespec(ts_addr, &ts)) {
return -TARGET_EFAULT;
}
}
}
return ret;
}
#endif
#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \
defined(TARGET_NR_ppoll_time64)
static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3,
abi_long arg4, abi_long arg5, bool ppoll, bool time64)
{
struct target_pollfd *target_pfd;
unsigned int nfds = arg2;
struct pollfd *pfd;
unsigned int i;
abi_long ret;
pfd = NULL;
target_pfd = NULL;
if (nfds) {
if (nfds > (INT_MAX / sizeof(struct target_pollfd))) {
return -TARGET_EINVAL;
}
target_pfd = lock_user(VERIFY_WRITE, arg1,
sizeof(struct target_pollfd) * nfds, 1);
if (!target_pfd) {
return -TARGET_EFAULT;
}
pfd = alloca(sizeof(struct pollfd) * nfds);
for (i = 0; i < nfds; i++) {
pfd[i].fd = tswap32(target_pfd[i].fd);
pfd[i].events = tswap16(target_pfd[i].events);
}
}
if (ppoll) {
struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
sigset_t *set = NULL;
if (arg3) {
if (time64) {
if (target_to_host_timespec64(timeout_ts, arg3)) {
unlock_user(target_pfd, arg1, 0);
return -TARGET_EFAULT;
}
} else {
if (target_to_host_timespec(timeout_ts, arg3)) {
unlock_user(target_pfd, arg1, 0);
return -TARGET_EFAULT;
}
}
} else {
timeout_ts = NULL;
}
if (arg4) {
ret = process_sigsuspend_mask(&set, arg4, arg5);
if (ret != 0) {
unlock_user(target_pfd, arg1, 0);
return ret;
}
}
ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
set, SIGSET_T_SIZE));
if (set) {
finish_sigsuspend_mask(ret);
}
if (!is_error(ret) && arg3) {
if (time64) {
if (host_to_target_timespec64(arg3, timeout_ts)) {
return -TARGET_EFAULT;
}
} else {
if (host_to_target_timespec(arg3, timeout_ts)) {
return -TARGET_EFAULT;
}
}
}
} else {
struct timespec ts, *pts;
if (arg3 >= 0) {
/* Convert ms to secs, ns */
ts.tv_sec = arg3 / 1000;
ts.tv_nsec = (arg3 % 1000) * 1000000LL;
pts = &ts;
} else {
/* -ve poll() timeout means "infinite" */
pts = NULL;
}
ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0));
}
if (!is_error(ret)) {
for (i = 0; i < nfds; i++) {
target_pfd[i].revents = tswap16(pfd[i].revents);
}
}
unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
return ret;
}
#endif
static abi_long do_pipe(CPUArchState *cpu_env, abi_ulong pipedes,
int flags, int is_pipe2)
{
int host_pipe[2];
abi_long ret;
ret = pipe2(host_pipe, flags);
if (is_error(ret))
return get_errno(ret);
/* Several targets have special calling conventions for the original
pipe syscall, but didn't replicate this into the pipe2 syscall. */
if (!is_pipe2) {
#if defined(TARGET_ALPHA)
cpu_env->ir[IR_A4] = host_pipe[1];
return host_pipe[0];
#elif defined(TARGET_MIPS)
cpu_env->active_tc.gpr[3] = host_pipe[1];
return host_pipe[0];
#elif defined(TARGET_SH4)
cpu_env->gregs[1] = host_pipe[1];
return host_pipe[0];
#elif defined(TARGET_SPARC)
cpu_env->regwptr[1] = host_pipe[1];
return host_pipe[0];
#endif
}
if (put_user_s32(host_pipe[0], pipedes)
|| put_user_s32(host_pipe[1], pipedes + sizeof(abi_int)))
return -TARGET_EFAULT;
return get_errno(ret);
}
static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
abi_ulong target_addr,
socklen_t len)
{
struct target_ip_mreqn *target_smreqn;
target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
if (!target_smreqn)
return -TARGET_EFAULT;
mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
if (len == sizeof(struct target_ip_mreqn))
mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
unlock_user(target_smreqn, target_addr, 0);
return 0;
}
static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
abi_ulong target_addr,
socklen_t len)
{
const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
sa_family_t sa_family;
struct target_sockaddr *target_saddr;
if (fd_trans_target_to_host_addr(fd)) {
return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
}
target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
if (!target_saddr)
return -TARGET_EFAULT;
sa_family = tswap16(target_saddr->sa_family);
/* Oops. The caller might send a incomplete sun_path; sun_path
* must be terminated by \0 (see the manual page), but
* unfortunately it is quite common to specify sockaddr_un
* length as "strlen(x->sun_path)" while it should be
* "strlen(...) + 1". We'll fix that here if needed.
* Linux kernel has a similar feature.
*/
if (sa_family == AF_UNIX) {
if (len < unix_maxlen && len > 0) {
char *cp = (char*)target_saddr;
if ( cp[len-1] && !cp[len] )
len++;
}
if (len > unix_maxlen)
len = unix_maxlen;
}
memcpy(addr, target_saddr, len);
addr->sa_family = sa_family;
if (sa_family == AF_NETLINK) {
struct sockaddr_nl *nladdr;
nladdr = (struct sockaddr_nl *)addr;
nladdr->nl_pid = tswap32(nladdr->nl_pid);
nladdr->nl_groups = tswap32(nladdr->nl_groups);
} else if (sa_family == AF_PACKET) {
struct target_sockaddr_ll *lladdr;
lladdr = (struct target_sockaddr_ll *)addr;
lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
}
unlock_user(target_saddr, target_addr, 0);
return 0;
}
static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
struct sockaddr *addr,
socklen_t len)
{
struct target_sockaddr *target_saddr;
if (len == 0) {
return 0;
}
assert(addr);
target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
if (!target_saddr)
return -TARGET_EFAULT;
memcpy(target_saddr, addr, len);
if (len >= offsetof(struct target_sockaddr, sa_family) +
sizeof(target_saddr->sa_family)) {
target_saddr->sa_family = tswap16(addr->sa_family);
}
if (addr->sa_family == AF_NETLINK &&
len >= sizeof(struct target_sockaddr_nl)) {
struct target_sockaddr_nl *target_nl =
(struct target_sockaddr_nl *)target_saddr;
target_nl->nl_pid = tswap32(target_nl->nl_pid);
target_nl->nl_groups = tswap32(target_nl->nl_groups);
} else if (addr->sa_family == AF_PACKET) {
struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
} else if (addr->sa_family == AF_INET6 &&
len >= sizeof(struct target_sockaddr_in6)) {
struct target_sockaddr_in6 *target_in6 =
(struct target_sockaddr_in6 *)target_saddr;
target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id);
}
unlock_user(target_saddr, target_addr, len);
return 0;
}
static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
struct target_msghdr *target_msgh)
{
struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
abi_long msg_controllen;
abi_ulong target_cmsg_addr;
struct target_cmsghdr *target_cmsg, *target_cmsg_start;
socklen_t space = 0;
msg_controllen = tswapal(target_msgh->msg_controllen);
if (msg_controllen < sizeof (struct target_cmsghdr))
goto the_end;
target_cmsg_addr = tswapal(target_msgh->msg_control);
target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
target_cmsg_start = target_cmsg;
if (!target_cmsg)
return -TARGET_EFAULT;
while (cmsg && target_cmsg) {
void *data = CMSG_DATA(cmsg);
void *target_data = TARGET_CMSG_DATA(target_cmsg);
int len = tswapal(target_cmsg->cmsg_len)
- sizeof(struct target_cmsghdr);
space += CMSG_SPACE(len);
if (space > msgh->msg_controllen) {
space -= CMSG_SPACE(len);
/* This is a QEMU bug, since we allocated the payload
* area ourselves (unlike overflow in host-to-target
* conversion, which is just the guest giving us a buffer
* that's too small). It can't happen for the payload types
* we currently support; if it becomes an issue in future
* we would need to improve our allocation strategy to
* something more intelligent than "twice the size of the
* target buffer we're reading from".
*/
qemu_log_mask(LOG_UNIMP,
("Unsupported ancillary data %d/%d: "
"unhandled msg size\n"),
tswap32(target_cmsg->cmsg_level),
tswap32(target_cmsg->cmsg_type));
break;
}
if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
cmsg->cmsg_level = SOL_SOCKET;
} else {
cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
}
cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
cmsg->cmsg_len = CMSG_LEN(len);
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
int *fd = (int *)data;
int *target_fd = (int *)target_data;
int i, numfds = len / sizeof(int);
for (i = 0; i < numfds; i++) {
__get_user(fd[i], target_fd + i);
}
} else if (cmsg->cmsg_level == SOL_SOCKET
&& cmsg->cmsg_type == SCM_CREDENTIALS) {
struct ucred *cred = (struct ucred *)data;
struct target_ucred *target_cred =
(struct target_ucred *)target_data;
__get_user(cred->pid, &target_cred->pid);
__get_user(cred->uid, &target_cred->uid);
__get_user(cred->gid, &target_cred->gid);
} else {
qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
cmsg->cmsg_level, cmsg->cmsg_type);
memcpy(data, target_data, len);
}
cmsg = CMSG_NXTHDR(msgh, cmsg);
target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
target_cmsg_start);
}
unlock_user(target_cmsg, target_cmsg_addr, 0);
the_end:
msgh->msg_controllen = space;
return 0;
}
static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
struct msghdr *msgh)
{
struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
abi_long msg_controllen;
abi_ulong target_cmsg_addr;
struct target_cmsghdr *target_cmsg, *target_cmsg_start;
socklen_t space = 0;
msg_controllen = tswapal(target_msgh->msg_controllen);
if (msg_controllen < sizeof (struct target_cmsghdr))
goto the_end;
target_cmsg_addr = tswapal(target_msgh->msg_control);
target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
target_cmsg_start = target_cmsg;
if (!target_cmsg)
return -TARGET_EFAULT;
while (cmsg && target_cmsg) {
void *data = CMSG_DATA(cmsg);
void *target_data = TARGET_CMSG_DATA(target_cmsg);
int len = cmsg->cmsg_len - sizeof(struct cmsghdr);
int tgt_len, tgt_space;
/* We never copy a half-header but may copy half-data;
* this is Linux's behaviour in put_cmsg(). Note that
* truncation here is a guest problem (which we report
* to the guest via the CTRUNC bit), unlike truncation
* in target_to_host_cmsg, which is a QEMU bug.
*/
if (msg_controllen < sizeof(struct target_cmsghdr)) {
target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
break;
}
if (cmsg->cmsg_level == SOL_SOCKET) {
target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
} else {
target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
}
target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
/* Payload types which need a different size of payload on
* the target must adjust tgt_len here.
*/
tgt_len = len;
switch (cmsg->cmsg_level) {
case SOL_SOCKET:
switch (cmsg->cmsg_type) {
case SO_TIMESTAMP:
tgt_len = sizeof(struct target_timeval);
break;
default:
break;
}
break;
default:
break;
}
if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) {
target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
tgt_len = msg_controllen - sizeof(struct target_cmsghdr);
}
/* We must now copy-and-convert len bytes of payload
* into tgt_len bytes of destination space. Bear in mind
* that in both source and destination we may be dealing
* with a truncated value!
*/
switch (cmsg->cmsg_level) {
case SOL_SOCKET:
switch (cmsg->cmsg_type) {
case SCM_RIGHTS:
{
int *fd = (int *)data;
int *target_fd = (int *)target_data;
int i, numfds = tgt_len / sizeof(int);
for (i = 0; i < numfds; i++) {
__put_user(fd[i], target_fd + i);
}
break;
}
case SO_TIMESTAMP:
{
struct timeval *tv = (struct timeval *)data;
struct target_timeval *target_tv =
(struct target_timeval *)target_data;
if (len != sizeof(struct timeval) ||
tgt_len != sizeof(struct target_timeval)) {
goto unimplemented;
}
/* copy struct timeval to target */
__put_user(tv->tv_sec, &target_tv->tv_sec);
__put_user(tv->tv_usec, &target_tv->tv_usec);
break;
}
case SCM_CREDENTIALS:
{
struct ucred *cred = (struct ucred *)data;
struct target_ucred *target_cred =
(struct target_ucred *)target_data;
__put_user(cred->pid, &target_cred->pid);
__put_user(cred->uid, &target_cred->uid);
__put_user(cred->gid, &target_cred->gid);
break;
}
default:
goto unimplemented;
}
break;
case SOL_IP:
switch (cmsg->cmsg_type) {
case IP_TTL:
{
uint32_t *v = (uint32_t *)data;
uint32_t *t_int = (uint32_t *)target_data;
if (len != sizeof(uint32_t) ||
tgt_len != sizeof(uint32_t)) {
goto unimplemented;
}
__put_user(*v, t_int);
break;
}
case IP_RECVERR:
{
struct errhdr_t {
struct sock_extended_err ee;
struct sockaddr_in offender;
};
struct errhdr_t *errh = (struct errhdr_t *)data;
struct errhdr_t *target_errh =
(struct errhdr_t *)target_data;
if (len != sizeof(struct errhdr_t) ||
tgt_len != sizeof(struct errhdr_t)) {
goto unimplemented;
}
__put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
__put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
__put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
__put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
__put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
__put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
__put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
host_to_target_sockaddr((unsigned long) &target_errh->offender,
(void *) &errh->offender, sizeof(errh->offender));
break;
}
default:
goto unimplemented;
}
break;
case SOL_IPV6:
switch (cmsg->cmsg_type) {
case IPV6_HOPLIMIT:
{
uint32_t *v = (uint32_t *)data;
uint32_t *t_int = (uint32_t *)target_data;
if (len != sizeof(uint32_t) ||
tgt_len != sizeof(uint32_t)) {
goto unimplemented;
}
__put_user(*v, t_int);
break;
}
case IPV6_RECVERR:
{
struct errhdr6_t {
struct sock_extended_err ee;
struct sockaddr_in6 offender;
};
struct errhdr6_t *errh = (struct errhdr6_t *)data;
struct errhdr6_t *target_errh =
(struct errhdr6_t *)target_data;
if (len != sizeof(struct errhdr6_t) ||
tgt_len != sizeof(struct errhdr6_t)) {
goto unimplemented;
}
__put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
__put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
__put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
__put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
__put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
__put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
__put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
host_to_target_sockaddr((unsigned long) &target_errh->offender,
(void *) &errh->offender, sizeof(errh->offender));
break;
}
default:
goto unimplemented;
}
break;
default:
unimplemented:
qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
cmsg->cmsg_level, cmsg->cmsg_type);
memcpy(target_data, data, MIN(len, tgt_len));
if (tgt_len > len) {
memset(target_data + len, 0, tgt_len - len);
}
}
target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len));
tgt_space = TARGET_CMSG_SPACE(tgt_len);
if (msg_controllen < tgt_space) {
tgt_space = msg_controllen;
}
msg_controllen -= tgt_space;
space += tgt_space;
cmsg = CMSG_NXTHDR(msgh, cmsg);
target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
target_cmsg_start);
}
unlock_user(target_cmsg, target_cmsg_addr, space);
the_end:
target_msgh->msg_controllen = tswapal(space);
return 0;
}
/* do_setsockopt() Must return target values and target errnos. */
static abi_long do_setsockopt(int sockfd, int level, int optname,
abi_ulong optval_addr, socklen_t optlen)
{
abi_long ret;
int val;
struct ip_mreqn *ip_mreq;
struct ip_mreq_source *ip_mreq_source;
switch(level) {
case SOL_TCP:
case SOL_UDP:
/* TCP and UDP options all take an 'int' value. */
if (optlen < sizeof(uint32_t))
return -TARGET_EINVAL;
if (get_user_u32(val, optval_addr))
return -TARGET_EFAULT;
ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
break;
case SOL_IP:
switch(optname) {
case IP_TOS:
case IP_TTL:
case IP_HDRINCL:
case IP_ROUTER_ALERT:
case IP_RECVOPTS:
case IP_RETOPTS:
case IP_PKTINFO:
case IP_MTU_DISCOVER:
case IP_RECVERR:
case IP_RECVTTL:
case IP_RECVTOS:
#ifdef IP_FREEBIND
case IP_FREEBIND:
#endif
case IP_MULTICAST_TTL:
case IP_MULTICAST_LOOP:
val = 0;
if (optlen >= sizeof(uint32_t)) {
if (get_user_u32(val, optval_addr))
return -TARGET_EFAULT;
} else if (optlen >= 1) {
if (get_user_u8(val, optval_addr))
return -TARGET_EFAULT;
}
ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
break;
case IP_ADD_MEMBERSHIP:
case IP_DROP_MEMBERSHIP:
if (optlen < sizeof (struct target_ip_mreq) ||
optlen > sizeof (struct target_ip_mreqn))
return -TARGET_EINVAL;
ip_mreq = (struct ip_mreqn *) alloca(optlen);
target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
break;
case IP_BLOCK_SOURCE:
case IP_UNBLOCK_SOURCE:
case IP_ADD_SOURCE_MEMBERSHIP:
case IP_DROP_SOURCE_MEMBERSHIP:
if (optlen != sizeof (struct target_ip_mreq_source))
return -TARGET_EINVAL;
ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
if (!ip_mreq_source) {
return -TARGET_EFAULT;
}
ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
unlock_user (ip_mreq_source, optval_addr, 0);
break;
default:
goto unimplemented;
}
break;
case SOL_IPV6:
switch (optname) {
case IPV6_MTU_DISCOVER:
case IPV6_MTU:
case IPV6_V6ONLY:
case IPV6_RECVPKTINFO:
case IPV6_UNICAST_HOPS:
case IPV6_MULTICAST_HOPS:
case IPV6_MULTICAST_LOOP:
case IPV6_RECVERR:
case IPV6_RECVHOPLIMIT:
case IPV6_2292HOPLIMIT:
case IPV6_CHECKSUM:
case IPV6_ADDRFORM:
case IPV6_2292PKTINFO:
case IPV6_RECVTCLASS:
case IPV6_RECVRTHDR:
case IPV6_2292RTHDR:
case IPV6_RECVHOPOPTS:
case IPV6_2292HOPOPTS:
case IPV6_RECVDSTOPTS:
case IPV6_2292DSTOPTS:
case IPV6_TCLASS:
case IPV6_ADDR_PREFERENCES:
#ifdef IPV6_RECVPATHMTU
case IPV6_RECVPATHMTU:
#endif
#ifdef IPV6_TRANSPARENT
case IPV6_TRANSPARENT:
#endif
#ifdef IPV6_FREEBIND
case IPV6_FREEBIND:
#endif
#ifdef IPV6_RECVORIGDSTADDR
case IPV6_RECVORIGDSTADDR:
#endif
val = 0;
if (optlen < sizeof(uint32_t)) {
return -TARGET_EINVAL;
}
if (get_user_u32(val, optval_addr)) {
return -TARGET_EFAULT;
}
ret = get_errno(setsockopt(sockfd, level, optname,
&val, sizeof(val)));
break;
case IPV6_PKTINFO:
{
struct in6_pktinfo pki;
if (optlen < sizeof(pki)) {
return -TARGET_EINVAL;
}
if (copy_from_user(&pki, optval_addr, sizeof(pki))) {
return -TARGET_EFAULT;
}
pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex);
ret = get_errno(setsockopt(sockfd, level, optname,
&pki, sizeof(pki)));
break;
}
case IPV6_ADD_MEMBERSHIP:
case IPV6_DROP_MEMBERSHIP:
{
struct ipv6_mreq ipv6mreq;
if (optlen < sizeof(ipv6mreq)) {
return -TARGET_EINVAL;
}
if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) {
return -TARGET_EFAULT;
}
ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
ret = get_errno(setsockopt(sockfd, level, optname,
&ipv6mreq, sizeof(ipv6mreq)));
break;
}
default:
goto unimplemented;
}
break;
case SOL_ICMPV6:
switch (optname) {
case ICMPV6_FILTER:
{
struct icmp6_filter icmp6f;
if (optlen > sizeof(icmp6f)) {
optlen = sizeof(icmp6f);
}
if (copy_from_user(&icmp6f, optval_addr, optlen)) {
return -TARGET_EFAULT;
}
for (val = 0; val < 8; val++) {
icmp6f.data[val] = tswap32(icmp6f.data[val]);
}
ret = get_errno(setsockopt(sockfd, level, optname,
&icmp6f, optlen));
break;
}
default:
goto unimplemented;
}
break;
case SOL_RAW:
switch (optname) {
case ICMP_FILTER:
case IPV6_CHECKSUM:
/* those take an u32 value */
if (optlen < sizeof(uint32_t)) {
return -TARGET_EINVAL;
}
if (get_user_u32(val, optval_addr)) {
return -TARGET_EFAULT;
}
ret = get_errno(setsockopt(sockfd, level, optname,
&val, sizeof(val)));
break;
default:
goto unimplemented;
}
break;
#if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
case SOL_ALG:
switch (optname) {
case ALG_SET_KEY:
{
char *alg_key = g_malloc(optlen);
if (!alg_key) {
return -TARGET_ENOMEM;
}
if (copy_from_user(alg_key, optval_addr, optlen)) {
g_free(alg_key);
return -TARGET_EFAULT;
}
ret = get_errno(setsockopt(sockfd, level, optname,
alg_key, optlen));
g_free(alg_key);
break;
}
case ALG_SET_AEAD_AUTHSIZE:
{
ret = get_errno(setsockopt(sockfd, level, optname,
NULL, optlen));
break;
}
default:
goto unimplemented;
}
break;
#endif
case TARGET_SOL_SOCKET:
switch (optname) {
case TARGET_SO_RCVTIMEO:
{
struct timeval tv;
optname = SO_RCVTIMEO;
set_timeout:
if (optlen != sizeof(struct target_timeval)) {
return -TARGET_EINVAL;
}
if (copy_from_user_timeval(&tv, optval_addr)) {
return -TARGET_EFAULT;
}
ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
&tv, sizeof(tv)));
return ret;
}
case TARGET_SO_SNDTIMEO:
optname = SO_SNDTIMEO;
goto set_timeout;
case TARGET_SO_ATTACH_FILTER:
{
struct target_sock_fprog *tfprog;
struct target_sock_filter *tfilter;
struct sock_fprog fprog;
struct sock_filter *filter;
int i;
if (optlen != sizeof(*tfprog)) {
return -TARGET_EINVAL;
}
if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
return -TARGET_EFAULT;
}
if (!lock_user_struct(VERIFY_READ, tfilter,
tswapal(tfprog->filter), 0)) {
unlock_user_struct(tfprog, optval_addr, 1);
return -TARGET_EFAULT;
}
fprog.len = tswap16(tfprog->len);
filter = g_try_new(struct sock_filter, fprog.len);
if (filter == NULL) {
unlock_user_struct(tfilter, tfprog->filter, 1);
unlock_user_struct(tfprog, optval_addr, 1);
return -TARGET_ENOMEM;
}
for (i = 0; i < fprog.len; i++) {
filter[i].code = tswap16(tfilter[i].code);
filter[i].jt = tfilter[i].jt;
filter[i].jf = tfilter[i].jf;
filter[i].k = tswap32(tfilter[i].k);
}
fprog.filter = filter;
ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
g_free(filter);
unlock_user_struct(tfilter, tfprog->filter, 1);
unlock_user_struct(tfprog, optval_addr, 1);
return ret;
}
case TARGET_SO_BINDTODEVICE:
{
char *dev_ifname, *addr_ifname;
if (optlen > IFNAMSIZ - 1) {
optlen = IFNAMSIZ - 1;
}
dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
if (!dev_ifname) {
return -TARGET_EFAULT;
}
optname = SO_BINDTODEVICE;
addr_ifname = alloca(IFNAMSIZ);
memcpy(addr_ifname, dev_ifname, optlen);
addr_ifname[optlen] = 0;
ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
addr_ifname, optlen));
unlock_user (dev_ifname, optval_addr, 0);
return ret;
}
case TARGET_SO_LINGER:
{
struct linger lg;
struct target_linger *tlg;
if (optlen != sizeof(struct target_linger)) {
return -TARGET_EINVAL;
}
if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) {
return -TARGET_EFAULT;
}
__get_user(lg.l_onoff, &tlg->l_onoff);
__get_user(lg.l_linger, &tlg->l_linger);
ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER,
&lg, sizeof(lg)));
unlock_user_struct(tlg, optval_addr, 0);
return ret;
}
/* Options with 'int' argument. */
case TARGET_SO_DEBUG:
optname = SO_DEBUG;
break;
case TARGET_SO_REUSEADDR:
optname = SO_REUSEADDR;
break;
#ifdef SO_REUSEPORT
case TARGET_SO_REUSEPORT:
optname = SO_REUSEPORT;
break;
#endif
case TARGET_SO_TYPE:
optname = SO_TYPE;
break;
case TARGET_SO_ERROR:
optname = SO_ERROR;
break;
case TARGET_SO_DONTROUTE:
optname = SO_DONTROUTE;
break;
case TARGET_SO_BROADCAST:
optname = SO_BROADCAST;
break;
case TARGET_SO_SNDBUF:
optname = SO_SNDBUF;
break;
case TARGET_SO_SNDBUFFORCE:
optname = SO_SNDBUFFORCE;
break;
case TARGET_SO_RCVBUF:
optname = SO_RCVBUF;
break;
case TARGET_SO_RCVBUFFORCE:
optname = SO_RCVBUFFORCE;
break;
case TARGET_SO_KEEPALIVE:
optname = SO_KEEPALIVE;
break;
case TARGET_SO_OOBINLINE:
optname = SO_OOBINLINE;
break;
case TARGET_SO_NO_CHECK:
optname = SO_NO_CHECK;
break;
case TARGET_SO_PRIORITY:
optname = SO_PRIORITY;
break;
#ifdef SO_BSDCOMPAT
case TARGET_SO_BSDCOMPAT:
optname = SO_BSDCOMPAT;
break;
#endif
case TARGET_SO_PASSCRED:
optname = SO_PASSCRED;
break;
case TARGET_SO_PASSSEC:
optname = SO_PASSSEC;
break;
case TARGET_SO_TIMESTAMP:
optname = SO_TIMESTAMP;
break;
case TARGET_SO_RCVLOWAT:
optname = SO_RCVLOWAT;
break;
default:
goto unimplemented;
}
if (optlen < sizeof(uint32_t))
return -TARGET_EINVAL;
if (get_user_u32(val, optval_addr))
return -TARGET_EFAULT;
ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
break;
#ifdef SOL_NETLINK
case SOL_NETLINK:
switch (optname) {
case NETLINK_PKTINFO:
case NETLINK_ADD_MEMBERSHIP:
case NETLINK_DROP_MEMBERSHIP:
case NETLINK_BROADCAST_ERROR:
case NETLINK_NO_ENOBUFS:
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
case NETLINK_LISTEN_ALL_NSID:
case NETLINK_CAP_ACK:
#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
case NETLINK_EXT_ACK:
#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
case NETLINK_GET_STRICT_CHK:
#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
break;
default:
goto unimplemented;
}
val = 0;
if (optlen < sizeof(uint32_t)) {
return -TARGET_EINVAL;
}
if (get_user_u32(val, optval_addr)) {
return -TARGET_EFAULT;
}
ret = get_errno(setsockopt(sockfd, SOL_NETLINK, optname, &val,
sizeof(val)));
break;
#endif /* SOL_NETLINK */
default:
unimplemented:
qemu_log_mask(LOG_UNIMP, "Unsupported setsockopt level=%d optname=%d\n",
level, optname);
ret = -TARGET_ENOPROTOOPT;
}
return ret;
}
/* do_getsockopt() Must return target values and target errnos. */
static abi_long do_getsockopt(int sockfd, int level, int optname,
abi_ulong optval_addr, abi_ulong optlen)
{
abi_long ret;
int len, val;
socklen_t lv;
switch(level) {
case TARGET_SOL_SOCKET:
level = SOL_SOCKET;
switch (optname) {
/* These don't just return a single integer */
case TARGET_SO_PEERNAME:
goto unimplemented;
case TARGET_SO_RCVTIMEO: {
struct timeval tv;
socklen_t tvlen;
optname = SO_RCVTIMEO;
get_timeout:
if (get_user_u32(len, optlen)) {
return -TARGET_EFAULT;
}
if (len < 0) {
return -TARGET_EINVAL;
}
tvlen = sizeof(tv);
ret = get_errno(getsockopt(sockfd, level, optname,
&tv, &tvlen));
if (ret < 0) {
return ret;
}
if (len > sizeof(struct target_timeval)) {
len = sizeof(struct target_timeval);
}
if (copy_to_user_timeval(optval_addr, &tv)) {
return -TARGET_EFAULT;
}
if (put_user_u32(len, optlen)) {
return -TARGET_EFAULT;
}
break;
}
case TARGET_SO_SNDTIMEO:
optname = SO_SNDTIMEO;
goto get_timeout;
case TARGET_SO_PEERCRED: {
struct ucred cr;
socklen_t crlen;
struct target_ucred *tcr;
if (get_user_u32(len, optlen)) {
return -TARGET_EFAULT;
}
if (len < 0) {
return -TARGET_EINVAL;
}
crlen = sizeof(cr);
ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
&cr, &crlen));
if (ret < 0) {
return ret;
}
if (len > crlen) {
len = crlen;
}
if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
return -TARGET_EFAULT;
}
__put_user(cr.pid, &tcr->pid);
__put_user(cr.uid, &tcr->uid);
__put_user(cr.gid, &tcr->gid);
unlock_user_struct(tcr, optval_addr, 1);
if (put_user_u32(len, optlen)) {
return -TARGET_EFAULT;
}
break;
}
case TARGET_SO_PEERSEC: {
char *name;
if (get_user_u32(len, optlen)) {
return -TARGET_EFAULT;
}
if (len < 0) {
return -TARGET_EINVAL;
}
name = lock_user(VERIFY_WRITE, optval_addr, len, 0);
if (!name) {
return -TARGET_EFAULT;
}
lv = len;
ret = get_errno(getsockopt(sockfd, level, SO_PEERSEC,
name, &lv));
if (put_user_u32(lv, optlen)) {
ret = -TARGET_EFAULT;
}
unlock_user(name, optval_addr, lv);
break;
}
case TARGET_SO_LINGER:
{
struct linger lg;
socklen_t lglen;
struct target_linger *tlg;
if (get_user_u32(len, optlen)) {
return -TARGET_EFAULT;
}
if (len < 0) {
return -TARGET_EINVAL;
}
lglen = sizeof(lg);
ret = get_errno(getsockopt(sockfd, level, SO_LINGER,
&lg, &lglen));
if (ret < 0) {
return ret;
}
if (len > lglen) {
len = lglen;
}
if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) {
return -TARGET_EFAULT;
}
__put_user(lg.l_onoff, &tlg->l_onoff);
__put_user(lg.l_linger, &tlg->l_linger);
unlock_user_struct(tlg, optval_addr, 1);
if (put_user_u32(len, optlen)) {
return -TARGET_EFAULT;
}
break;
}
/* Options with 'int' argument. */
case TARGET_SO_DEBUG:
optname = SO_DEBUG;
goto int_case;
case TARGET_SO_REUSEADDR:
optname = SO_REUSEADDR;
goto int_case;
#ifdef SO_REUSEPORT
case TARGET_SO_REUSEPORT:
optname = SO_REUSEPORT;
goto int_case;
#endif
case TARGET_SO_TYPE:
optname = SO_TYPE;
goto int_case;
case TARGET_SO_ERROR:
optname = SO_ERROR;
goto int_case;
case TARGET_SO_DONTROUTE:
optname = SO_DONTROUTE;
goto int_case;
case TARGET_SO_BROADCAST:
optname = SO_BROADCAST;
goto int_case;
case TARGET_SO_SNDBUF:
optname = SO_SNDBUF;
goto int_case;
case TARGET_SO_RCVBUF:
optname = SO_RCVBUF;
goto int_case;
case TARGET_SO_KEEPALIVE:
optname = SO_KEEPALIVE;
goto int_case;
case TARGET_SO_OOBINLINE:
optname = SO_OOBINLINE;
goto int_case;
case TARGET_SO_NO_CHECK:
optname = SO_NO_CHECK;
goto int_case;
case TARGET_SO_PRIORITY:
optname = SO_PRIORITY;
goto int_case;
#ifdef SO_BSDCOMPAT
case TARGET_SO_BSDCOMPAT:
optname = SO_BSDCOMPAT;
goto int_case;
#endif
case TARGET_SO_PASSCRED:
optname = SO_PASSCRED;
goto int_case;
case TARGET_SO_TIMESTAMP:
optname = SO_TIMESTAMP;
goto int_case;
case TARGET_SO_RCVLOWAT:
optname = SO_RCVLOWAT;
goto int_case;
case TARGET_SO_ACCEPTCONN:
optname = SO_ACCEPTCONN;
goto int_case;
case TARGET_SO_PROTOCOL:
optname = SO_PROTOCOL;
goto int_case;
case TARGET_SO_DOMAIN:
optname = SO_DOMAIN;
goto int_case;
default:
goto int_case;
}
break;
case SOL_TCP:
case SOL_UDP:
/* TCP and UDP options all take an 'int' value. */
int_case:
if (get_user_u32(len, optlen))
return -TARGET_EFAULT;
if (len < 0)
return -TARGET_EINVAL;
lv = sizeof(lv);
ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
if (ret < 0)
return ret;
if (optname == SO_TYPE) {
val = host_to_target_sock_type(val);
}
if (len > lv)
len = lv;
if (len == 4) {
if (put_user_u32(val, optval_addr))
return -TARGET_EFAULT;
} else {
if (put_user_u8(val, optval_addr))
return -TARGET_EFAULT;
}
if (put_user_u32(len, optlen))
return -TARGET_EFAULT;
break;
case SOL_IP:
switch(optname) {
case IP_TOS:
case IP_TTL:
case IP_HDRINCL:
case IP_ROUTER_ALERT:
case IP_RECVOPTS:
case IP_RETOPTS:
case IP_PKTINFO:
case IP_MTU_DISCOVER:
case IP_RECVERR:
case IP_RECVTOS:
#ifdef IP_FREEBIND
case IP_FREEBIND:
#endif
case IP_MULTICAST_TTL:
case IP_MULTICAST_LOOP:
if (get_user_u32(len, optlen))
return -TARGET_EFAULT;
if (len < 0)
return -TARGET_EINVAL;
lv = sizeof(lv);
ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
if (ret < 0)
return ret;
if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
len = 1;
if (put_user_u32(len, optlen)
|| put_user_u8(val, optval_addr))
return -TARGET_EFAULT;
} else {
if (len > sizeof(int))
len = sizeof(int);
if (put_user_u32(len, optlen)
|| put_user_u32(val, optval_addr))
return -TARGET_EFAULT;
}
break;
default:
ret = -TARGET_ENOPROTOOPT;
break;
}
break;
case SOL_IPV6:
switch (optname) {
case IPV6_MTU_DISCOVER:
case IPV6_MTU:
case IPV6_V6ONLY:
case IPV6_RECVPKTINFO:
case IPV6_UNICAST_HOPS:
case IPV6_MULTICAST_HOPS:
case IPV6_MULTICAST_LOOP:
case IPV6_RECVERR:
case IPV6_RECVHOPLIMIT:
case IPV6_2292HOPLIMIT:
case IPV6_CHECKSUM:
case IPV6_ADDRFORM:
case IPV6_2292PKTINFO:
case IPV6_RECVTCLASS:
case IPV6_RECVRTHDR:
case IPV6_2292RTHDR:
case IPV6_RECVHOPOPTS:
case IPV6_2292HOPOPTS:
case IPV6_RECVDSTOPTS:
case IPV6_2292DSTOPTS:
case IPV6_TCLASS:
case IPV6_ADDR_PREFERENCES:
#ifdef IPV6_RECVPATHMTU
case IPV6_RECVPATHMTU:
#endif
#ifdef IPV6_TRANSPARENT
case IPV6_TRANSPARENT:
#endif
#ifdef IPV6_FREEBIND
case IPV6_FREEBIND:
#endif
#ifdef IPV6_RECVORIGDSTADDR
case IPV6_RECVORIGDSTADDR:
#endif
if (get_user_u32(len, optlen))
return -TARGET_EFAULT;
if (len < 0)
return -TARGET_EINVAL;
lv = sizeof(lv);
ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
if (ret < 0)
return ret;
if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
len = 1;
if (put_user_u32(len, optlen)
|| put_user_u8(val, optval_addr))
return -TARGET_EFAULT;
} else {
if (len > sizeof(int))
len = sizeof(int);
if (put_user_u32(len, optlen)
|| put_user_u32(val, optval_addr))
return -TARGET_EFAULT;
}
break;
default:
ret = -TARGET_ENOPROTOOPT;
break;
}
break;
#ifdef SOL_NETLINK
case SOL_NETLINK:
switch (optname) {
case NETLINK_PKTINFO:
case NETLINK_BROADCAST_ERROR:
case NETLINK_NO_ENOBUFS:
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
case NETLINK_LISTEN_ALL_NSID:
case NETLINK_CAP_ACK:
#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
case NETLINK_EXT_ACK:
#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
case NETLINK_GET_STRICT_CHK:
#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
if (get_user_u32(len, optlen)) {
return -TARGET_EFAULT;
}
if (len != sizeof(val)) {
return -TARGET_EINVAL;
}
lv = len;
ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
if (ret < 0) {
return ret;
}
if (put_user_u32(lv, optlen)
|| put_user_u32(val, optval_addr)) {
return -TARGET_EFAULT;
}
break;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
case NETLINK_LIST_MEMBERSHIPS:
{
uint32_t *results;
int i;
if (get_user_u32(len, optlen)) {
return -TARGET_EFAULT;
}
if (len < 0) {
return -TARGET_EINVAL;
}
results = lock_user(VERIFY_WRITE, optval_addr, len, 1);
if (!results && len > 0) {
return -TARGET_EFAULT;
}
lv = len;
ret = get_errno(getsockopt(sockfd, level, optname, results, &lv));
if (ret < 0) {
unlock_user(results, optval_addr, 0);
return ret;
}
/* swap host endianess to target endianess. */
for (i = 0; i < (len / sizeof(uint32_t)); i++) {
results[i] = tswap32(results[i]);
}
if (put_user_u32(lv, optlen)) {
return -TARGET_EFAULT;
}
unlock_user(results, optval_addr, 0);
break;
}
#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
default:
goto unimplemented;
}
break;
#endif /* SOL_NETLINK */
default:
unimplemented:
qemu_log_mask(LOG_UNIMP,
"getsockopt level=%d optname=%d not yet supported\n",
level, optname);
ret = -TARGET_EOPNOTSUPP;
break;
}
return ret;
}
/* Convert target low/high pair representing file offset into the host
* low/high pair. This function doesn't handle offsets bigger than 64 bits
* as the kernel doesn't handle them either.
*/
static void target_to_host_low_high(abi_ulong tlow,
abi_ulong thigh,
unsigned long *hlow,
unsigned long *hhigh)