]> git.proxmox.com Git - mirror_qemu.git/blame - linux-user/syscall.c
riscv: Clean up includes
[mirror_qemu.git] / linux-user / syscall.c
CommitLineData
31e31b8a
FB
1/*
2 * Linux syscalls
5fafdf24 3 *
31e31b8a
FB
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
8167ee88 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
31e31b8a 18 */
d5b3a9b6 19#define _ATFILE_SOURCE
d39594e9 20#include "qemu/osdep.h"
f348b6d1
VB
21#include "qemu/cutils.h"
22#include "qemu/path.h"
9bdfa4d2 23#include "qemu/memfd.h"
dc5e9ac7 24#include "qemu/queue.h"
720ace24 25#include "qemu/plugin.h"
d7ec12f8 26#include "tcg/startup.h"
fe080593 27#include "target_mman.h"
31e31b8a
FB
28#include <elf.h>
29#include <endian.h>
c56dc774 30#include <grp.h>
d08d3bb8
TS
31#include <sys/ipc.h>
32#include <sys/msg.h>
31e31b8a 33#include <sys/wait.h>
31e31b8a 34#include <sys/mount.h>
586b0bef
JS
35#include <sys/file.h>
36#include <sys/fsuid.h>
37#include <sys/personality.h>
39b9aae1 38#include <sys/prctl.h>
31e31b8a 39#include <sys/resource.h>
31e31b8a 40#include <sys/swap.h>
e0eb210e 41#include <linux/capability.h>
31e31b8a 42#include <sched.h>
19f59bce 43#include <sys/timex.h>
31e31b8a 44#include <sys/socket.h>
6d5d5dde 45#include <linux/sockios.h>
607175e0 46#include <sys/un.h>
31e31b8a 47#include <sys/uio.h>
0839f11c 48#include <poll.h>
32f36bce 49#include <sys/times.h>
8853f86e 50#include <sys/shm.h>
fa294816 51#include <sys/sem.h>
56c8f68f 52#include <sys/statfs.h>
ebc05488 53#include <utime.h>
a5448a7d 54#include <sys/sysinfo.h>
e36800c9 55#include <sys/signalfd.h>
72f03900 56//#include <sys/user.h>
22db1213 57#include <netinet/in.h>
8853f86e 58#include <netinet/ip.h>
7854b056 59#include <netinet/tcp.h>
fe51b0a5 60#include <netinet/udp.h>
86fcd946 61#include <linux/wireless.h>
920394db 62#include <linux/icmp.h>
ee1ac3a1 63#include <linux/icmpv6.h>
6addf06a 64#include <linux/if_tun.h>
22db1213 65#include <linux/in6.h>
ee1ac3a1 66#include <linux/errqueue.h>
d6d6d6fe 67#include <linux/random.h>
d80a1905
RV
68#ifdef CONFIG_TIMERFD
69#include <sys/timerfd.h>
70#endif
c2882b96
RV
71#ifdef CONFIG_EVENTFD
72#include <sys/eventfd.h>
73#endif
3b6edd16
PM
74#ifdef CONFIG_EPOLL
75#include <sys/epoll.h>
76#endif
a790ae38 77#ifdef CONFIG_ATTR
1de7afc9 78#include "qemu/xattr.h"
a790ae38 79#endif
a8fd1aba
PM
80#ifdef CONFIG_SENDFILE
81#include <sys/sendfile.h>
82#endif
4a9d5f89 83#ifdef HAVE_SYS_KCOV_H
bd27e675
AM
84#include <sys/kcov.h>
85#endif
31e31b8a
FB
86
87#define termios host_termios
88#define winsize host_winsize
89#define termio host_termio
04369ff2
FB
90#define sgttyb host_sgttyb /* same as target */
91#define tchars host_tchars /* same as target */
92#define ltchars host_ltchars /* same as target */
31e31b8a
FB
93
94#include <linux/termios.h>
95#include <linux/unistd.h>
31e31b8a
FB
96#include <linux/cdrom.h>
97#include <linux/hdreg.h>
98#include <linux/soundcard.h>
19b84f3c 99#include <linux/kd.h>
8fbd6b52 100#include <linux/mtio.h>
350d1779 101#include <linux/fs.h>
ab22b4dd 102#include <linux/fd.h>
dace20dc 103#if defined(CONFIG_FIEMAP)
285da2b9 104#include <linux/fiemap.h>
dace20dc 105#endif
f7680a55 106#include <linux/fb.h>
6c753a63
CT
107#if defined(CONFIG_USBFS)
108#include <linux/usbdevice_fs.h>
a133367e 109#include <linux/usb/ch9.h>
6c753a63 110#endif
f7680a55 111#include <linux/vt.h>
56e904ec 112#include <linux/dm-ioctl.h>
c07ecc68 113#include <linux/reboot.h>
7ff7b666 114#include <linux/route.h>
f57d4192 115#include <linux/filter.h>
fff8c539 116#include <linux/blkpg.h>
a82ea939 117#include <netpacket/packet.h>
6c5b5645 118#include <linux/netlink.h>
f31dddd2 119#include <linux/if_alg.h>
68365f96 120#include <linux/rtc.h>
1c4c6fcd 121#include <sound/asound.h>
48f670ec 122#ifdef HAVE_BTRFS_H
d6092e08
FB
123#include <linux/btrfs.h>
124#endif
e865b97f
CG
125#ifdef HAVE_DRM_H
126#include <libdrm/drm.h>
913b03c2 127#include <libdrm/i915_drm.h>
e865b97f 128#endif
d7e4036e 129#include "linux_loop.h"
18cb0088 130#include "uname.h"
31e31b8a 131
3ef693a0 132#include "qemu.h"
3b249d26 133#include "user-internals.h"
a44d57a3 134#include "strace.h"
2113aed6 135#include "signal-common.h"
3ad0a769 136#include "loader.h"
5423e6d3 137#include "user-mmap.h"
bbf15aaf 138#include "user/safe-syscall.h"
5ebdd774 139#include "qemu/guest-random.h"
01ef6b9e 140#include "qemu/selfmap.h"
c36f7a64 141#include "user/syscall-trace.h"
5da4063f 142#include "special-errno.h"
51977e25 143#include "qapi/error.h"
f7e6a401 144#include "fd-trans.h"
bd5ccd61 145#include "cpu_loop-common.h"
31e31b8a 146
5ea2fc84
PM
147#ifndef CLONE_IO
148#define CLONE_IO 0x80000000 /* Clone io context */
149#endif
150
151/* We can't directly call the host clone syscall, because this will
152 * badly confuse libc (breaking mutexes, for example). So we must
153 * divide clone flags into:
154 * * flag combinations that look like pthread_create()
155 * * flag combinations that look like fork()
156 * * flags we can implement within QEMU itself
157 * * flags we can't support and will return an error for
158 */
159/* For thread creation, all these flags must be present; for
160 * fork, none must be present.
161 */
162#define CLONE_THREAD_FLAGS \
163 (CLONE_VM | CLONE_FS | CLONE_FILES | \
164 CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
165
166/* These flags are ignored:
167 * CLONE_DETACHED is now ignored by the kernel;
168 * CLONE_IO is just an optimisation hint to the I/O scheduler
169 */
170#define CLONE_IGNORED_FLAGS \
171 (CLONE_DETACHED | CLONE_IO)
172
895ce8bb
HD
173#ifndef CLONE_PIDFD
174# define CLONE_PIDFD 0x00001000
175#endif
176
5ea2fc84
PM
177/* Flags for fork which we can implement within QEMU itself */
178#define CLONE_OPTIONAL_FORK_FLAGS \
895ce8bb 179 (CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_PIDFD | \
5ea2fc84
PM
180 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
181
182/* Flags for thread creation which we can implement within QEMU itself */
183#define CLONE_OPTIONAL_THREAD_FLAGS \
184 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
185 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
186
187#define CLONE_INVALID_FORK_FLAGS \
188 (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
189
190#define CLONE_INVALID_THREAD_FLAGS \
191 (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
192 CLONE_IGNORED_FLAGS))
193
194/* CLONE_VFORK is special cased early in do_fork(). The other flag bits
195 * have almost all been allocated. We cannot support any of
196 * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
197 * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
198 * The checks against the invalid thread masks above will catch these.
199 * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
200 */
30813cea 201
71a8f7fe
TB
202/* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
203 * once. This exercises the codepaths for restart.
204 */
205//#define DEBUG_ERESTARTSYS
31e31b8a 206
1a9353d2 207//#include <linux/msdos_fs.h>
540a736f
RH
208#define VFAT_IOCTL_READDIR_BOTH \
209 _IOC(_IOC_READ, 'r', 1, (sizeof(struct linux_dirent) + 256) * 2)
210#define VFAT_IOCTL_READDIR_SHORT \
211 _IOC(_IOC_READ, 'r', 2, (sizeof(struct linux_dirent) + 256) * 2)
1a9353d2 212
70a194b9
FB
213#undef _syscall0
214#undef _syscall1
215#undef _syscall2
216#undef _syscall3
217#undef _syscall4
218#undef _syscall5
83fcb515 219#undef _syscall6
70a194b9 220
83fcb515 221#define _syscall0(type,name) \
8fcd3692 222static type name (void) \
83fcb515
FB
223{ \
224 return syscall(__NR_##name); \
225}
70a194b9 226
83fcb515 227#define _syscall1(type,name,type1,arg1) \
8fcd3692 228static type name (type1 arg1) \
83fcb515
FB
229{ \
230 return syscall(__NR_##name, arg1); \
70a194b9
FB
231}
232
83fcb515 233#define _syscall2(type,name,type1,arg1,type2,arg2) \
8fcd3692 234static type name (type1 arg1,type2 arg2) \
83fcb515
FB
235{ \
236 return syscall(__NR_##name, arg1, arg2); \
70a194b9
FB
237}
238
83fcb515 239#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
8fcd3692 240static type name (type1 arg1,type2 arg2,type3 arg3) \
83fcb515
FB
241{ \
242 return syscall(__NR_##name, arg1, arg2, arg3); \
70a194b9
FB
243}
244
83fcb515 245#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
8fcd3692 246static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
83fcb515
FB
247{ \
248 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
70a194b9
FB
249}
250
83fcb515
FB
251#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
252 type5,arg5) \
8fcd3692 253static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
83fcb515
FB
254{ \
255 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
70a194b9
FB
256}
257
83fcb515
FB
258
259#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
260 type5,arg5,type6,arg6) \
8fcd3692
BS
261static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
262 type6 arg6) \
83fcb515
FB
263{ \
264 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
70a194b9 265}
83fcb515 266
70a194b9 267
31e31b8a 268#define __NR_sys_uname __NR_uname
72f03900 269#define __NR_sys_getcwd1 __NR_getcwd
72f03900 270#define __NR_sys_getdents __NR_getdents
dab2ed99 271#define __NR_sys_getdents64 __NR_getdents64
c6cda17a 272#define __NR_sys_getpriority __NR_getpriority
66fb9763 273#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
cf8b8bfc 274#define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
7494b0f9 275#define __NR_sys_syslog __NR_syslog
14690296
AF
276#if defined(__NR_futex)
277# define __NR_sys_futex __NR_futex
278#endif
279#if defined(__NR_futex_time64)
280# define __NR_sys_futex_time64 __NR_futex_time64
281#endif
efa92184 282#define __NR_sys_statx __NR_statx
31e31b8a 283
b1cef6d0 284#if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
9af9eaaa
FB
285#define __NR__llseek __NR_lseek
286#endif
287
a29e5ba2
JH
288/* Newer kernel ports have llseek() instead of _llseek() */
289#if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
290#define TARGET_NR__llseek TARGET_NR_llseek
291#endif
292
78721301
HD
293/* some platforms need to mask more bits than just TARGET_O_NONBLOCK */
294#ifndef TARGET_O_NONBLOCK_MASK
295#define TARGET_O_NONBLOCK_MASK TARGET_O_NONBLOCK
296#endif
297
71ba74f6
DB
298#define __NR_sys_gettid __NR_gettid
299_syscall0(int, sys_gettid)
2b3f64cb
PM
300
301/* For the 64-bit guest on 32-bit host case we must emulate
302 * getdents using getdents64, because otherwise the host
303 * might hand us back more dirent records than we can fit
304 * into the guest buffer after structure format conversion.
305 * Otherwise we emulate getdents with getdents if the host has it.
306 */
307#if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
308#define EMULATE_GETDENTS_WITH_GETDENTS
309#endif
310
311#if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
ac42f443 312_syscall3(int, sys_getdents, unsigned int, fd, struct linux_dirent *, dirp, unsigned int, count);
3307e236 313#endif
2b3f64cb
PM
314#if (defined(TARGET_NR_getdents) && \
315 !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
3307e236 316 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
ac42f443 317_syscall3(int, sys_getdents64, unsigned int, fd, struct linux_dirent64 *, dirp, unsigned int, count);
3b3f24ad 318#endif
d35b261c 319#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
ac42f443
JQ
320_syscall5(int, _llseek, unsigned int, fd, unsigned long, hi, unsigned long, lo,
321 loff_t *, res, unsigned int, wh);
3b3f24ad 322#endif
c1a402a7 323_syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo)
cf8b8bfc
MS
324_syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig,
325 siginfo_t *, uinfo)
3b3f24ad 326_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
3b3f24ad
AJ
327#ifdef __NR_exit_group
328_syscall1(int,exit_group,int,error_code)
329#endif
af804f39
HD
330#if defined(__NR_close_range) && defined(TARGET_NR_close_range)
331#define __NR_sys_close_range __NR_close_range
332_syscall3(int,sys_close_range,int,first,int,last,int,flags)
333#ifndef CLOSE_RANGE_CLOEXEC
334#define CLOSE_RANGE_CLOEXEC (1U << 2)
335#endif
336#endif
14690296 337#if defined(__NR_futex)
3b3f24ad
AJ
338_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
339 const struct timespec *,timeout,int *,uaddr2,int,val3)
340#endif
14690296
AF
341#if defined(__NR_futex_time64)
342_syscall6(int,sys_futex_time64,int *,uaddr,int,op,int,val,
343 const struct timespec *,timeout,int *,uaddr2,int,val3)
344#endif
cc054c6f
HD
345#if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
346_syscall2(int, pidfd_open, pid_t, pid, unsigned int, flags);
347#endif
348#if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
349_syscall4(int, pidfd_send_signal, int, pidfd, int, sig, siginfo_t *, info,
350 unsigned int, flags);
351#endif
352#if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
353_syscall3(int, pidfd_getfd, int, pidfd, int, targetfd, unsigned int, flags);
354#endif
737de1d1
MF
355#define __NR_sys_sched_getaffinity __NR_sched_getaffinity
356_syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
357 unsigned long *, user_mask_ptr);
358#define __NR_sys_sched_setaffinity __NR_sched_setaffinity
359_syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
360 unsigned long *, user_mask_ptr);
45ad761c
TT
361/* sched_attr is not defined in glibc */
362struct sched_attr {
363 uint32_t size;
364 uint32_t sched_policy;
365 uint64_t sched_flags;
366 int32_t sched_nice;
367 uint32_t sched_priority;
368 uint64_t sched_runtime;
369 uint64_t sched_deadline;
370 uint64_t sched_period;
371 uint32_t sched_util_min;
372 uint32_t sched_util_max;
373};
374#define __NR_sys_sched_getattr __NR_sched_getattr
375_syscall4(int, sys_sched_getattr, pid_t, pid, struct sched_attr *, attr,
376 unsigned int, size, unsigned int, flags);
377#define __NR_sys_sched_setattr __NR_sched_setattr
378_syscall3(int, sys_sched_setattr, pid_t, pid, struct sched_attr *, attr,
379 unsigned int, flags);
407a119b
TT
380#define __NR_sys_sched_getscheduler __NR_sched_getscheduler
381_syscall1(int, sys_sched_getscheduler, pid_t, pid);
382#define __NR_sys_sched_setscheduler __NR_sched_setscheduler
383_syscall3(int, sys_sched_setscheduler, pid_t, pid, int, policy,
384 const struct sched_param *, param);
385#define __NR_sys_sched_getparam __NR_sched_getparam
386_syscall2(int, sys_sched_getparam, pid_t, pid,
387 struct sched_param *, param);
388#define __NR_sys_sched_setparam __NR_sched_setparam
389_syscall2(int, sys_sched_setparam, pid_t, pid,
390 const struct sched_param *, param);
b827c3ed
ST
391#define __NR_sys_getcpu __NR_getcpu
392_syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
0f6b4d21
AG
393_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
394 void *, arg);
e0eb210e
PM
395_syscall2(int, capget, struct __user_cap_header_struct *, header,
396 struct __user_cap_data_struct *, data);
397_syscall2(int, capset, struct __user_cap_header_struct *, header,
398 struct __user_cap_data_struct *, data);
ab31cda3
PB
399#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
400_syscall2(int, ioprio_get, int, which, int, who)
401#endif
402#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
403_syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
404#endif
f894efd1
LV
405#if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
406_syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
407#endif
3b3f24ad 408
2f14788c
LV
409#if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
410_syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
411 unsigned long, idx1, unsigned long, idx2)
412#endif
413
efa92184
AR
414/*
415 * It is assumed that struct statx is architecture independent.
416 */
417#if defined(TARGET_NR_statx) && defined(__NR_statx)
418_syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags,
419 unsigned int, mask, struct target_statx *, statxbuf)
420#endif
8500476f
AS
421#if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
422_syscall2(int, membarrier, int, cmd, int, flags)
423#endif
efa92184 424
180d4ef3 425static const bitmask_transtbl fcntl_flags_tbl[] = {
3b3f24ad
AJ
426 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
427 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
428 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
429 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
430 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
431 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
432 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
433 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
afc8763f 434 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
3b3f24ad
AJ
435 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
436 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
437 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
438 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
3b3f24ad
AJ
439#if defined(O_DIRECT)
440 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
afc8763f
RH
441#endif
442#if defined(O_NOATIME)
443 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
444#endif
445#if defined(O_CLOEXEC)
446 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
447#endif
448#if defined(O_PATH)
449 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
5f9cee46
RV
450#endif
451#if defined(O_TMPFILE)
452 { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
afc8763f
RH
453#endif
454 /* Don't terminate the list prematurely on 64-bit host+guest. */
455#if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
456 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
3b3f24ad 457#endif
3b3f24ad
AJ
458};
459
0f6bb195 460_syscall2(int, sys_getcwd1, char *, buf, size_t, size)
3b3f24ad 461
cac46eb0 462#if defined(TARGET_NR_utimensat) || defined(TARGET_NR_utimensat_time64)
700fa58e 463#if defined(__NR_utimensat)
1acae9f2 464#define __NR_sys_utimensat __NR_utimensat
9007f0ef
TS
465_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
466 const struct timespec *,tsp,int,flags)
1acae9f2
PM
467#else
468static int sys_utimensat(int dirfd, const char *pathname,
469 const struct timespec times[2], int flags)
470{
471 errno = ENOSYS;
472 return -1;
473}
9007f0ef 474#endif
1acae9f2 475#endif /* TARGET_NR_utimensat */
3b3f24ad 476
95d0307c
AS
477#ifdef TARGET_NR_renameat2
478#if defined(__NR_renameat2)
479#define __NR_sys_renameat2 __NR_renameat2
480_syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd,
481 const char *, new, unsigned int, flags)
482#else
483static int sys_renameat2(int oldfd, const char *old,
484 int newfd, const char *new, int flags)
485{
486 if (flags == 0) {
487 return renameat(oldfd, old, newfd, new);
488 }
489 errno = ENOSYS;
490 return -1;
491}
492#endif
493#endif /* TARGET_NR_renameat2 */
494
3b3f24ad 495#ifdef CONFIG_INOTIFY
8690e420 496#include <sys/inotify.h>
3b3f24ad
AJ
497#else
498/* Userspace can usually survive runtime without inotify */
499#undef TARGET_NR_inotify_init
c05c7a73 500#undef TARGET_NR_inotify_init1
3b3f24ad
AJ
501#undef TARGET_NR_inotify_add_watch
502#undef TARGET_NR_inotify_rm_watch
503#endif /* CONFIG_INOTIFY */
504
163a05a8
PM
505#if defined(TARGET_NR_prlimit64)
506#ifndef __NR_prlimit64
507# define __NR_prlimit64 -1
508#endif
509#define __NR_sys_prlimit64 __NR_prlimit64
510/* The glibc rlimit structure may not be that used by the underlying syscall */
511struct host_rlimit64 {
512 uint64_t rlim_cur;
513 uint64_t rlim_max;
514};
515_syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
516 const struct host_rlimit64 *, new_limit,
517 struct host_rlimit64 *, old_limit)
518#endif
519
f4f1e10a
ECL
520
521#if defined(TARGET_NR_timer_create)
6f9ff551 522/* Maximum of 32 active POSIX timers allowed at any one time. */
9e59899f
PM
523#define GUEST_TIMER_MAX 32
524static timer_t g_posix_timers[GUEST_TIMER_MAX];
525static int g_posix_timer_allocated[GUEST_TIMER_MAX];
f4f1e10a
ECL
526
527static inline int next_free_host_timer(void)
528{
9e59899f
PM
529 int k;
530 for (k = 0; k < ARRAY_SIZE(g_posix_timer_allocated); k++) {
531 if (qatomic_xchg(g_posix_timer_allocated + k, 1) == 0) {
f4f1e10a
ECL
532 return k;
533 }
534 }
535 return -1;
536}
9e59899f
PM
537
538static inline void free_host_timer_slot(int id)
539{
540 qatomic_store_release(g_posix_timer_allocated + id, 0);
541}
f4f1e10a
ECL
542#endif
543
3ffe3268 544static inline int host_to_target_errno(int host_errno)
637947f1 545{
3ffe3268
PMD
546 switch (host_errno) {
547#define E(X) case X: return TARGET_##X;
548#include "errnos.c.inc"
549#undef E
550 default:
551 return host_errno;
2466119c 552 }
637947f1
TS
553}
554
3ffe3268 555static inline int target_to_host_errno(int target_errno)
b92c47c1 556{
3ffe3268
PMD
557 switch (target_errno) {
558#define E(X) case TARGET_##X: return X;
559#include "errnos.c.inc"
560#undef E
561 default:
562 return target_errno;
2466119c 563 }
b92c47c1
TS
564}
565
892a4f6a 566abi_long get_errno(abi_long ret)
31e31b8a
FB
567{
568 if (ret == -1)
637947f1 569 return -host_to_target_errno(errno);
31e31b8a
FB
570 else
571 return ret;
572}
573
7dcdaeaf 574const char *target_strerror(int err)
b92c47c1 575{
af254a27 576 if (err == QEMU_ERESTARTSYS) {
da2a34f7
PM
577 return "To be restarted";
578 }
57a0c938 579 if (err == QEMU_ESIGRETURN) {
da2a34f7
PM
580 return "Successful exit from sigreturn";
581 }
582
b92c47c1
TS
583 return strerror(target_to_host_errno(err));
584}
585
45ad761c
TT
586static int check_zeroed_user(abi_long addr, size_t ksize, size_t usize)
587{
588 int i;
589 uint8_t b;
590 if (usize <= ksize) {
591 return 1;
592 }
593 for (i = ksize; i < usize; i++) {
594 if (get_user_u8(b, addr + i)) {
595 return -TARGET_EFAULT;
596 }
597 if (b != 0) {
598 return 0;
599 }
600 }
601 return 1;
602}
603
4d330cee
TB
604#define safe_syscall0(type, name) \
605static type safe_##name(void) \
606{ \
607 return safe_syscall(__NR_##name); \
608}
609
610#define safe_syscall1(type, name, type1, arg1) \
611static type safe_##name(type1 arg1) \
612{ \
613 return safe_syscall(__NR_##name, arg1); \
614}
615
616#define safe_syscall2(type, name, type1, arg1, type2, arg2) \
617static type safe_##name(type1 arg1, type2 arg2) \
618{ \
619 return safe_syscall(__NR_##name, arg1, arg2); \
620}
621
622#define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
623static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
624{ \
625 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
626}
627
628#define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
629 type4, arg4) \
630static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
631{ \
632 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
633}
634
635#define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
636 type4, arg4, type5, arg5) \
637static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
638 type5 arg5) \
639{ \
640 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
641}
642
643#define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
644 type4, arg4, type5, arg5, type6, arg6) \
645static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
646 type5 arg5, type6 arg6) \
647{ \
648 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
649}
650
50afd02b
TB
651safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
652safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
c10a0738
TB
653safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
654 int, flags, mode_t, mode)
859e8a89 655#if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
4af80a37
TB
656safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
657 struct rusage *, rusage)
859e8a89 658#endif
4af80a37
TB
659safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
660 int, options, struct rusage *, rusage)
7a8d9f3a 661safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
55bbe4d5
DD
662safe_syscall5(int, execveat, int, dirfd, const char *, filename,
663 char **, argv, char **, envp, int, flags)
859e8a89 664#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
e5ce9688 665 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
6df9d38d
PM
666safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
667 fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
859e8a89 668#endif
e5ce9688 669#if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64)
a6130237
PM
670safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
671 struct timespec *, tsp, const sigset_t *, sigmask,
672 size_t, sigsetsize)
859e8a89 673#endif
227f0214
PM
674safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
675 int, maxevents, int, timeout, const sigset_t *, sigmask,
676 size_t, sigsetsize)
14690296 677#if defined(__NR_futex)
d509eeb1
PM
678safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
679 const struct timespec *,timeout,int *,uaddr2,int,val3)
859e8a89 680#endif
14690296
AF
681#if defined(__NR_futex_time64)
682safe_syscall6(int,futex_time64,int *,uaddr,int,op,int,val, \
683 const struct timespec *,timeout,int *,uaddr2,int,val3)
684#endif
2fe4fba1 685safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
bef653d9
PM
686safe_syscall2(int, kill, pid_t, pid, int, sig)
687safe_syscall2(int, tkill, int, tid, int, sig)
688safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
918c03ed
PM
689safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
690safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
0f26386c
DJ
691safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
692 unsigned long, pos_l, unsigned long, pos_h)
f8d00fba
DJ
693safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
694 unsigned long, pos_l, unsigned long, pos_h)
2a3c7619
PM
695safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
696 socklen_t, addrlen)
66687530
PM
697safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
698 int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
699safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
700 int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
701safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
702safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
2a845989 703safe_syscall2(int, flock, int, fd, int, operation)
ddcbde15 704#if defined(TARGET_NR_rt_sigtimedwait) || defined(TARGET_NR_rt_sigtimedwait_time64)
b3f82330
PM
705safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
706 const struct timespec *, uts, size_t, sigsetsize)
859e8a89 707#endif
ff6dc130
PM
708safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
709 int, flags)
859e8a89 710#if defined(TARGET_NR_nanosleep)
9e518226
PM
711safe_syscall2(int, nanosleep, const struct timespec *, req,
712 struct timespec *, rem)
859e8a89 713#endif
6ac03b2c
FB
714#if defined(TARGET_NR_clock_nanosleep) || \
715 defined(TARGET_NR_clock_nanosleep_time64)
9e518226
PM
716safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
717 const struct timespec *, req, struct timespec *, rem)
718#endif
524fa340 719#ifdef __NR_ipc
d8c08b1e
MK
720#ifdef __s390x__
721safe_syscall5(int, ipc, int, call, long, first, long, second, long, third,
722 void *, ptr)
723#else
89f9fe44
PM
724safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
725 void *, ptr, long, fifth)
86e63695 726#endif
d8c08b1e 727#endif
86e63695
LV
728#ifdef __NR_msgsnd
729safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
730 int, flags)
86e63695
LV
731#endif
732#ifdef __NR_msgrcv
733safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
734 long, msgtype, int, flags)
86e63695
LV
735#endif
736#ifdef __NR_semtimedop
737safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
738 unsigned, nsops, const struct timespec *, timeout)
89f9fe44 739#endif
d107e375
FB
740#if defined(TARGET_NR_mq_timedsend) || \
741 defined(TARGET_NR_mq_timedsend_time64)
d40ecd66
PM
742safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
743 size_t, len, unsigned, prio, const struct timespec *, timeout)
859e8a89 744#endif
d107e375
FB
745#if defined(TARGET_NR_mq_timedreceive) || \
746 defined(TARGET_NR_mq_timedreceive_time64)
d40ecd66
PM
747safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
748 size_t, len, unsigned *, prio, const struct timespec *, timeout)
749#endif
84946457
AS
750#if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
751safe_syscall6(ssize_t, copy_file_range, int, infd, loff_t *, pinoff,
752 int, outfd, loff_t *, poutoff, size_t, length,
753 unsigned int, flags)
754#endif
755
49ca6f3e
PM
756/* We do ioctl like this rather than via safe_syscall3 to preserve the
757 * "third argument might be integer or pointer or not present" behaviour of
758 * the libc function.
759 */
760#define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
435da5e7
PM
761/* Similarly for fcntl. Note that callers must always:
762 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
763 * use the flock64 struct rather than unsuffixed flock
764 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
765 */
766#ifdef __NR_fcntl64
767#define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
768#else
769#define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
770#endif
50afd02b 771
8289d112
PB
772static inline int host_to_target_sock_type(int host_type)
773{
774 int target_type;
775
776 switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
777 case SOCK_DGRAM:
778 target_type = TARGET_SOCK_DGRAM;
779 break;
780 case SOCK_STREAM:
781 target_type = TARGET_SOCK_STREAM;
782 break;
783 default:
784 target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
785 break;
786 }
787
788#if defined(SOCK_CLOEXEC)
789 if (host_type & SOCK_CLOEXEC) {
790 target_type |= TARGET_SOCK_CLOEXEC;
791 }
792#endif
793
794#if defined(SOCK_NONBLOCK)
795 if (host_type & SOCK_NONBLOCK) {
796 target_type |= TARGET_SOCK_NONBLOCK;
797 }
798#endif
799
800 return target_type;
801}
802
dfe49864 803static abi_ulong target_brk, initial_target_brk;
31e31b8a 804
992f48a0 805void target_set_brk(abi_ulong new_brk)
31e31b8a 806{
d28b3c90 807 target_brk = TARGET_PAGE_ALIGN(new_brk);
dfe49864 808 initial_target_brk = target_brk;
31e31b8a
FB
809}
810
0da46a6e 811/* do_brk() must return target values and target errnos. */
86f04735 812abi_long do_brk(abi_ulong brk_val)
31e31b8a 813{
992f48a0 814 abi_long mapped_addr;
2aea137a
AO
815 abi_ulong new_brk;
816 abi_ulong old_brk;
31e31b8a 817
ee1bf83d
RH
818 /* brk pointers are always untagged */
819
dfe49864
HD
820 /* do not allow to shrink below initial brk value */
821 if (brk_val < initial_target_brk) {
cb9d5d1f 822 return target_brk;
dfe49864
HD
823 }
824
86f04735 825 new_brk = TARGET_PAGE_ALIGN(brk_val);
2aea137a 826 old_brk = TARGET_PAGE_ALIGN(target_brk);
86f04735 827
2aea137a
AO
828 /* new and old target_brk might be on the same page */
829 if (new_brk == old_brk) {
86f04735 830 target_brk = brk_val;
7ab240ad 831 return target_brk;
4d1de87c 832 }
3b46e624 833
2cf91b9a 834 /* Release heap if necessary */
2aea137a
AO
835 if (new_brk < old_brk) {
836 target_munmap(new_brk, old_brk - new_brk);
86f04735
HD
837
838 target_brk = brk_val;
839 return target_brk;
31e31b8a
FB
840 }
841
2aea137a
AO
842 mapped_addr = target_mmap(old_brk, new_brk - old_brk,
843 PROT_READ | PROT_WRITE,
844 MAP_FIXED_NOREPLACE | MAP_ANON | MAP_PRIVATE,
845 -1, 0);
70afc343 846
2aea137a 847 if (mapped_addr == old_brk) {
86f04735 848 target_brk = brk_val;
00faf08c 849 return target_brk;
00faf08c 850 }
7ab240ad 851
7dd46c02
RH
852#if defined(TARGET_ALPHA)
853 /* We (partially) emulate OSF/1 on Alpha, which requires we
854 return a proper errno, not an unchanged brk value. */
00faf08c 855 return -TARGET_ENOMEM;
7dd46c02 856#endif
00faf08c 857 /* For everything else, return the previous break. */
7ab240ad 858 return target_brk;
31e31b8a
FB
859}
860
859e8a89 861#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
e5ce9688 862 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
26edcf41
TS
863static inline abi_long copy_from_user_fdset(fd_set *fds,
864 abi_ulong target_fds_addr,
865 int n)
31e31b8a 866{
26edcf41
TS
867 int i, nw, j, k;
868 abi_ulong b, *target_fds;
869
b1b2db29 870 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
26edcf41
TS
871 if (!(target_fds = lock_user(VERIFY_READ,
872 target_fds_addr,
873 sizeof(abi_ulong) * nw,
874 1)))
875 return -TARGET_EFAULT;
876
877 FD_ZERO(fds);
878 k = 0;
879 for (i = 0; i < nw; i++) {
880 /* grab the abi_ulong */
881 __get_user(b, &target_fds[i]);
882 for (j = 0; j < TARGET_ABI_BITS; j++) {
883 /* check the bit inside the abi_ulong */
884 if ((b >> j) & 1)
885 FD_SET(k, fds);
886 k++;
31e31b8a 887 }
31e31b8a 888 }
26edcf41
TS
889
890 unlock_user(target_fds, target_fds_addr, 0);
891
892 return 0;
31e31b8a
FB
893}
894
055e0906
MF
895static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
896 abi_ulong target_fds_addr,
897 int n)
898{
899 if (target_fds_addr) {
900 if (copy_from_user_fdset(fds, target_fds_addr, n))
901 return -TARGET_EFAULT;
902 *fds_ptr = fds;
903 } else {
904 *fds_ptr = NULL;
905 }
906 return 0;
907}
908
26edcf41
TS
909static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
910 const fd_set *fds,
911 int n)
31e31b8a 912{
31e31b8a 913 int i, nw, j, k;
992f48a0 914 abi_long v;
26edcf41 915 abi_ulong *target_fds;
31e31b8a 916
b1b2db29 917 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
26edcf41
TS
918 if (!(target_fds = lock_user(VERIFY_WRITE,
919 target_fds_addr,
920 sizeof(abi_ulong) * nw,
921 0)))
922 return -TARGET_EFAULT;
923
924 k = 0;
925 for (i = 0; i < nw; i++) {
926 v = 0;
927 for (j = 0; j < TARGET_ABI_BITS; j++) {
9ab709be 928 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
26edcf41 929 k++;
31e31b8a 930 }
26edcf41 931 __put_user(v, &target_fds[i]);
31e31b8a 932 }
26edcf41
TS
933
934 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
935
936 return 0;
31e31b8a 937}
859e8a89 938#endif
31e31b8a 939
c596ed17
FB
940#if defined(__alpha__)
941#define HOST_HZ 1024
942#else
943#define HOST_HZ 100
944#endif
945
992f48a0 946static inline abi_long host_to_target_clock_t(long ticks)
c596ed17
FB
947{
948#if HOST_HZ == TARGET_HZ
949 return ticks;
950#else
951 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
952#endif
953}
954
579a97f7
FB
955static inline abi_long host_to_target_rusage(abi_ulong target_addr,
956 const struct rusage *rusage)
b409186b 957{
53a5960a
PB
958 struct target_rusage *target_rusage;
959
579a97f7
FB
960 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
961 return -TARGET_EFAULT;
cbb21eed
MB
962 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
963 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
964 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
965 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
966 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
967 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
968 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
969 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
970 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
971 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
972 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
973 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
974 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
975 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
976 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
977 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
978 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
979 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
53a5960a 980 unlock_user_struct(target_rusage, target_addr, 1);
579a97f7
FB
981
982 return 0;
b409186b
FB
983}
984
859e8a89 985#ifdef TARGET_NR_setrlimit
cbb21eed 986static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
81bbe906 987{
cbb21eed 988 abi_ulong target_rlim_swap;
95b33b2f
WT
989 rlim_t result;
990
cbb21eed
MB
991 target_rlim_swap = tswapal(target_rlim);
992 if (target_rlim_swap == TARGET_RLIM_INFINITY)
993 return RLIM_INFINITY;
994
995 result = target_rlim_swap;
996 if (target_rlim_swap != (rlim_t)result)
997 return RLIM_INFINITY;
95b33b2f
WT
998
999 return result;
81bbe906 1000}
859e8a89 1001#endif
81bbe906 1002
859e8a89 1003#if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
cbb21eed 1004static inline abi_ulong host_to_target_rlim(rlim_t rlim)
81bbe906 1005{
cbb21eed
MB
1006 abi_ulong target_rlim_swap;
1007 abi_ulong result;
95b33b2f 1008
cbb21eed 1009 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
95b33b2f 1010 target_rlim_swap = TARGET_RLIM_INFINITY;
81bbe906 1011 else
95b33b2f 1012 target_rlim_swap = rlim;
cbb21eed 1013 result = tswapal(target_rlim_swap);
95b33b2f
WT
1014
1015 return result;
81bbe906 1016}
859e8a89 1017#endif
81bbe906 1018
e22b7015
WT
1019static inline int target_to_host_resource(int code)
1020{
1021 switch (code) {
1022 case TARGET_RLIMIT_AS:
1023 return RLIMIT_AS;
1024 case TARGET_RLIMIT_CORE:
1025 return RLIMIT_CORE;
1026 case TARGET_RLIMIT_CPU:
1027 return RLIMIT_CPU;
1028 case TARGET_RLIMIT_DATA:
1029 return RLIMIT_DATA;
1030 case TARGET_RLIMIT_FSIZE:
1031 return RLIMIT_FSIZE;
1032 case TARGET_RLIMIT_LOCKS:
1033 return RLIMIT_LOCKS;
1034 case TARGET_RLIMIT_MEMLOCK:
1035 return RLIMIT_MEMLOCK;
1036 case TARGET_RLIMIT_MSGQUEUE:
1037 return RLIMIT_MSGQUEUE;
1038 case TARGET_RLIMIT_NICE:
1039 return RLIMIT_NICE;
1040 case TARGET_RLIMIT_NOFILE:
1041 return RLIMIT_NOFILE;
1042 case TARGET_RLIMIT_NPROC:
1043 return RLIMIT_NPROC;
1044 case TARGET_RLIMIT_RSS:
1045 return RLIMIT_RSS;
1046 case TARGET_RLIMIT_RTPRIO:
1047 return RLIMIT_RTPRIO;
c3a28d71 1048#ifdef RLIMIT_RTTIME
244fd083
SB
1049 case TARGET_RLIMIT_RTTIME:
1050 return RLIMIT_RTTIME;
c3a28d71 1051#endif
e22b7015
WT
1052 case TARGET_RLIMIT_SIGPENDING:
1053 return RLIMIT_SIGPENDING;
1054 case TARGET_RLIMIT_STACK:
1055 return RLIMIT_STACK;
1056 default:
1057 return code;
1058 }
1059}
1060
788f5ec4
TS
1061static inline abi_long copy_from_user_timeval(struct timeval *tv,
1062 abi_ulong target_tv_addr)
31e31b8a 1063{
53a5960a
PB
1064 struct target_timeval *target_tv;
1065
6d5d5dde 1066 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
579a97f7 1067 return -TARGET_EFAULT;
6d5d5dde 1068 }
788f5ec4
TS
1069
1070 __get_user(tv->tv_sec, &target_tv->tv_sec);
1071 __get_user(tv->tv_usec, &target_tv->tv_usec);
1072
1073 unlock_user_struct(target_tv, target_tv_addr, 0);
579a97f7
FB
1074
1075 return 0;
31e31b8a
FB
1076}
1077
788f5ec4
TS
1078static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1079 const struct timeval *tv)
31e31b8a 1080{
53a5960a
PB
1081 struct target_timeval *target_tv;
1082
6d5d5dde
DB
1083 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
1084 return -TARGET_EFAULT;
1085 }
1086
1087 __put_user(tv->tv_sec, &target_tv->tv_sec);
1088 __put_user(tv->tv_usec, &target_tv->tv_usec);
1089
1090 unlock_user_struct(target_tv, target_tv_addr, 1);
1091
1092 return 0;
1093}
1094
6ac03b2c
FB
1095#if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
1096static inline abi_long copy_from_user_timeval64(struct timeval *tv,
1097 abi_ulong target_tv_addr)
1098{
1099 struct target__kernel_sock_timeval *target_tv;
1100
1101 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
1102 return -TARGET_EFAULT;
1103 }
1104
1105 __get_user(tv->tv_sec, &target_tv->tv_sec);
1106 __get_user(tv->tv_usec, &target_tv->tv_usec);
1107
1108 unlock_user_struct(target_tv, target_tv_addr, 0);
1109
1110 return 0;
1111}
1112#endif
1113
6d5d5dde 1114static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
6ac03b2c 1115 const struct timeval *tv)
6d5d5dde
DB
1116{
1117 struct target__kernel_sock_timeval *target_tv;
1118
1119 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
579a97f7 1120 return -TARGET_EFAULT;
6d5d5dde 1121 }
788f5ec4
TS
1122
1123 __put_user(tv->tv_sec, &target_tv->tv_sec);
1124 __put_user(tv->tv_usec, &target_tv->tv_usec);
1125
1126 unlock_user_struct(target_tv, target_tv_addr, 1);
579a97f7
FB
1127
1128 return 0;
31e31b8a
FB
1129}
1130
859e8a89
AF
1131#if defined(TARGET_NR_futex) || \
1132 defined(TARGET_NR_rt_sigtimedwait) || \
1133 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
1134 defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
1135 defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
d8c08b1e 1136 defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
2c86c90f
FB
1137 defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
1138 defined(TARGET_NR_timer_settime) || \
1139 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
6d5d5dde
DB
1140static inline abi_long target_to_host_timespec(struct timespec *host_ts,
1141 abi_ulong target_addr)
1142{
1143 struct target_timespec *target_ts;
1144
1145 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
1146 return -TARGET_EFAULT;
1147 }
1148 __get_user(host_ts->tv_sec, &target_ts->tv_sec);
1149 __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
1150 unlock_user_struct(target_ts, target_addr, 0);
1151 return 0;
1152}
859e8a89 1153#endif
6d5d5dde 1154
828cb3a1
FB
1155#if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \
1156 defined(TARGET_NR_timer_settime64) || \
d107e375
FB
1157 defined(TARGET_NR_mq_timedsend_time64) || \
1158 defined(TARGET_NR_mq_timedreceive_time64) || \
6ac03b2c 1159 (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) || \
ddcbde15 1160 defined(TARGET_NR_clock_nanosleep_time64) || \
cac46eb0
FB
1161 defined(TARGET_NR_rt_sigtimedwait_time64) || \
1162 defined(TARGET_NR_utimensat) || \
1163 defined(TARGET_NR_utimensat_time64) || \
e5ce9688
FB
1164 defined(TARGET_NR_semtimedop_time64) || \
1165 defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64)
c6c8d102
AF
1166static inline abi_long target_to_host_timespec64(struct timespec *host_ts,
1167 abi_ulong target_addr)
1168{
1169 struct target__kernel_timespec *target_ts;
1170
1171 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
1172 return -TARGET_EFAULT;
1173 }
1174 __get_user(host_ts->tv_sec, &target_ts->tv_sec);
1175 __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
00576757
LV
1176 /* in 32bit mode, this drops the padding */
1177 host_ts->tv_nsec = (long)(abi_long)host_ts->tv_nsec;
c6c8d102
AF
1178 unlock_user_struct(target_ts, target_addr, 0);
1179 return 0;
1180}
1181#endif
1182
6d5d5dde
DB
1183static inline abi_long host_to_target_timespec(abi_ulong target_addr,
1184 struct timespec *host_ts)
1185{
1186 struct target_timespec *target_ts;
1187
1188 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
1189 return -TARGET_EFAULT;
1190 }
1191 __put_user(host_ts->tv_sec, &target_ts->tv_sec);
1192 __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
1193 unlock_user_struct(target_ts, target_addr, 1);
1194 return 0;
1195}
1196
1197static inline abi_long host_to_target_timespec64(abi_ulong target_addr,
1198 struct timespec *host_ts)
1199{
1200 struct target__kernel_timespec *target_ts;
1201
1202 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
1203 return -TARGET_EFAULT;
1204 }
1205 __put_user(host_ts->tv_sec, &target_ts->tv_sec);
1206 __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
1207 unlock_user_struct(target_ts, target_addr, 1);
1208 return 0;
1209}
1210
a52f5f87
RH
1211#if defined(TARGET_NR_gettimeofday)
1212static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr,
1213 struct timezone *tz)
1214{
1215 struct target_timezone *target_tz;
1216
1217 if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) {
1218 return -TARGET_EFAULT;
1219 }
1220
1221 __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1222 __put_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1223
1224 unlock_user_struct(target_tz, target_tz_addr, 1);
1225
1226 return 0;
1227}
1228#endif
1229
859e8a89 1230#if defined(TARGET_NR_settimeofday)
ef4467e9
PB
1231static inline abi_long copy_from_user_timezone(struct timezone *tz,
1232 abi_ulong target_tz_addr)
1233{
1234 struct target_timezone *target_tz;
1235
1236 if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
1237 return -TARGET_EFAULT;
1238 }
1239
1240 __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1241 __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1242
1243 unlock_user_struct(target_tz, target_tz_addr, 0);
1244
1245 return 0;
1246}
859e8a89 1247#endif
ef4467e9 1248
8ec9cf89
NF
1249#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1250#include <mqueue.h>
1251
24e1003a
AJ
1252static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1253 abi_ulong target_mq_attr_addr)
1254{
1255 struct target_mq_attr *target_mq_attr;
1256
1257 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1258 target_mq_attr_addr, 1))
1259 return -TARGET_EFAULT;
1260
1261 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1262 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1263 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1264 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1265
1266 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1267
1268 return 0;
1269}
1270
1271static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1272 const struct mq_attr *attr)
1273{
1274 struct target_mq_attr *target_mq_attr;
1275
1276 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1277 target_mq_attr_addr, 0))
1278 return -TARGET_EFAULT;
1279
1280 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1281 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1282 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1283 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1284
1285 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1286
1287 return 0;
1288}
8ec9cf89 1289#endif
31e31b8a 1290
055e0906 1291#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
0da46a6e 1292/* do_select() must return target values and target errnos. */
992f48a0 1293static abi_long do_select(int n,
26edcf41
TS
1294 abi_ulong rfd_addr, abi_ulong wfd_addr,
1295 abi_ulong efd_addr, abi_ulong target_tv_addr)
31e31b8a
FB
1296{
1297 fd_set rfds, wfds, efds;
1298 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
6df9d38d
PM
1299 struct timeval tv;
1300 struct timespec ts, *ts_ptr;
992f48a0 1301 abi_long ret;
31e31b8a 1302
055e0906
MF
1303 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1304 if (ret) {
1305 return ret;
53a5960a 1306 }
055e0906
MF
1307 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1308 if (ret) {
1309 return ret;
53a5960a 1310 }
055e0906
MF
1311 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1312 if (ret) {
1313 return ret;
53a5960a 1314 }
3b46e624 1315
26edcf41 1316 if (target_tv_addr) {
788f5ec4
TS
1317 if (copy_from_user_timeval(&tv, target_tv_addr))
1318 return -TARGET_EFAULT;
6df9d38d
PM
1319 ts.tv_sec = tv.tv_sec;
1320 ts.tv_nsec = tv.tv_usec * 1000;
1321 ts_ptr = &ts;
31e31b8a 1322 } else {
6df9d38d 1323 ts_ptr = NULL;
31e31b8a 1324 }
26edcf41 1325
6df9d38d
PM
1326 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1327 ts_ptr, NULL));
53a5960a 1328
26edcf41
TS
1329 if (!is_error(ret)) {
1330 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1331 return -TARGET_EFAULT;
1332 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1333 return -TARGET_EFAULT;
1334 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1335 return -TARGET_EFAULT;
31e31b8a 1336
6df9d38d
PM
1337 if (target_tv_addr) {
1338 tv.tv_sec = ts.tv_sec;
1339 tv.tv_usec = ts.tv_nsec / 1000;
1340 if (copy_to_user_timeval(target_tv_addr, &tv)) {
1341 return -TARGET_EFAULT;
1342 }
1343 }
31e31b8a 1344 }
579a97f7 1345
31e31b8a
FB
1346 return ret;
1347}
5457dc9e
LV
1348
1349#if defined(TARGET_WANT_OLD_SYS_SELECT)
1350static abi_long do_old_select(abi_ulong arg1)
1351{
1352 struct target_sel_arg_struct *sel;
1353 abi_ulong inp, outp, exp, tvp;
1354 long nsel;
1355
1356 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
1357 return -TARGET_EFAULT;
1358 }
1359
1360 nsel = tswapal(sel->n);
1361 inp = tswapal(sel->inp);
1362 outp = tswapal(sel->outp);
1363 exp = tswapal(sel->exp);
1364 tvp = tswapal(sel->tvp);
1365
1366 unlock_user_struct(sel, arg1, 0);
1367
1368 return do_select(nsel, inp, outp, exp, tvp);
1369}
1370#endif
055e0906 1371#endif
31e31b8a 1372
e5ce9688
FB
1373#if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
1374static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3,
1375 abi_long arg4, abi_long arg5, abi_long arg6,
1376 bool time64)
1377{
1378 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
1379 fd_set rfds, wfds, efds;
1380 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1381 struct timespec ts, *ts_ptr;
1382 abi_long ret;
1383
1384 /*
1385 * The 6th arg is actually two args smashed together,
1386 * so we cannot use the C library.
1387 */
e5ce9688
FB
1388 struct {
1389 sigset_t *set;
1390 size_t size;
1391 } sig, *sig_ptr;
1392
1393 abi_ulong arg_sigset, arg_sigsize, *arg7;
e5ce9688
FB
1394
1395 n = arg1;
1396 rfd_addr = arg2;
1397 wfd_addr = arg3;
1398 efd_addr = arg4;
1399 ts_addr = arg5;
1400
1401 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1402 if (ret) {
1403 return ret;
1404 }
1405 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1406 if (ret) {
1407 return ret;
1408 }
1409 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1410 if (ret) {
1411 return ret;
1412 }
1413
1414 /*
1415 * This takes a timespec, and not a timeval, so we cannot
1416 * use the do_select() helper ...
1417 */
1418 if (ts_addr) {
1419 if (time64) {
1420 if (target_to_host_timespec64(&ts, ts_addr)) {
1421 return -TARGET_EFAULT;
1422 }
1423 } else {
1424 if (target_to_host_timespec(&ts, ts_addr)) {
1425 return -TARGET_EFAULT;
1426 }
1427 }
1428 ts_ptr = &ts;
1429 } else {
1430 ts_ptr = NULL;
1431 }
1432
1433 /* Extract the two packed args for the sigset */
cb226034 1434 sig_ptr = NULL;
e5ce9688 1435 if (arg6) {
e5ce9688
FB
1436 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
1437 if (!arg7) {
1438 return -TARGET_EFAULT;
1439 }
1440 arg_sigset = tswapal(arg7[0]);
1441 arg_sigsize = tswapal(arg7[1]);
1442 unlock_user(arg7, arg6, 0);
1443
1444 if (arg_sigset) {
cb226034
RH
1445 ret = process_sigsuspend_mask(&sig.set, arg_sigset, arg_sigsize);
1446 if (ret != 0) {
1447 return ret;
e5ce9688 1448 }
cb226034
RH
1449 sig_ptr = &sig;
1450 sig.size = SIGSET_T_SIZE;
e5ce9688 1451 }
e5ce9688
FB
1452 }
1453
1454 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1455 ts_ptr, sig_ptr));
1456
cb226034
RH
1457 if (sig_ptr) {
1458 finish_sigsuspend_mask(ret);
1459 }
1460
e5ce9688
FB
1461 if (!is_error(ret)) {
1462 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) {
1463 return -TARGET_EFAULT;
1464 }
1465 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) {
1466 return -TARGET_EFAULT;
1467 }
1468 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) {
1469 return -TARGET_EFAULT;
1470 }
1471 if (time64) {
1472 if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) {
1473 return -TARGET_EFAULT;
1474 }
1475 } else {
1476 if (ts_addr && host_to_target_timespec(ts_addr, &ts)) {
1477 return -TARGET_EFAULT;
1478 }
1479 }
1480 }
1481 return ret;
1482}
1483#endif
1484
1485#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \
1486 defined(TARGET_NR_ppoll_time64)
1487static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3,
1488 abi_long arg4, abi_long arg5, bool ppoll, bool time64)
1489{
1490 struct target_pollfd *target_pfd;
1491 unsigned int nfds = arg2;
1492 struct pollfd *pfd;
1493 unsigned int i;
1494 abi_long ret;
1495
1496 pfd = NULL;
1497 target_pfd = NULL;
1498 if (nfds) {
1499 if (nfds > (INT_MAX / sizeof(struct target_pollfd))) {
1500 return -TARGET_EINVAL;
1501 }
1502 target_pfd = lock_user(VERIFY_WRITE, arg1,
1503 sizeof(struct target_pollfd) * nfds, 1);
1504 if (!target_pfd) {
1505 return -TARGET_EFAULT;
1506 }
1507
1508 pfd = alloca(sizeof(struct pollfd) * nfds);
1509 for (i = 0; i < nfds; i++) {
1510 pfd[i].fd = tswap32(target_pfd[i].fd);
1511 pfd[i].events = tswap16(target_pfd[i].events);
1512 }
1513 }
1514 if (ppoll) {
1515 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
db36aa7d 1516 sigset_t *set = NULL;
e5ce9688
FB
1517
1518 if (arg3) {
1519 if (time64) {
1520 if (target_to_host_timespec64(timeout_ts, arg3)) {
1521 unlock_user(target_pfd, arg1, 0);
1522 return -TARGET_EFAULT;
1523 }
1524 } else {
1525 if (target_to_host_timespec(timeout_ts, arg3)) {
1526 unlock_user(target_pfd, arg1, 0);
1527 return -TARGET_EFAULT;
1528 }
1529 }
1530 } else {
1531 timeout_ts = NULL;
1532 }
1533
1534 if (arg4) {
db36aa7d
RH
1535 ret = process_sigsuspend_mask(&set, arg4, arg5);
1536 if (ret != 0) {
e5ce9688 1537 unlock_user(target_pfd, arg1, 0);
db36aa7d 1538 return ret;
e5ce9688 1539 }
e5ce9688
FB
1540 }
1541
1542 ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
1543 set, SIGSET_T_SIZE));
1544
db36aa7d
RH
1545 if (set) {
1546 finish_sigsuspend_mask(ret);
1547 }
e5ce9688
FB
1548 if (!is_error(ret) && arg3) {
1549 if (time64) {
1550 if (host_to_target_timespec64(arg3, timeout_ts)) {
1551 return -TARGET_EFAULT;
1552 }
1553 } else {
1554 if (host_to_target_timespec(arg3, timeout_ts)) {
1555 return -TARGET_EFAULT;
1556 }
1557 }
1558 }
e5ce9688
FB
1559 } else {
1560 struct timespec ts, *pts;
1561
1562 if (arg3 >= 0) {
1563 /* Convert ms to secs, ns */
1564 ts.tv_sec = arg3 / 1000;
1565 ts.tv_nsec = (arg3 % 1000) * 1000000LL;
1566 pts = &ts;
1567 } else {
1568 /* -ve poll() timeout means "infinite" */
1569 pts = NULL;
1570 }
1571 ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0));
1572 }
1573
1574 if (!is_error(ret)) {
1575 for (i = 0; i < nfds; i++) {
1576 target_pfd[i].revents = tswap16(pfd[i].revents);
1577 }
1578 }
1579 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
1580 return ret;
1581}
1582#endif
1583
a0939b89 1584static abi_long do_pipe(CPUArchState *cpu_env, abi_ulong pipedes,
fb41a66e 1585 int flags, int is_pipe2)
099d6b0f
RV
1586{
1587 int host_pipe[2];
1588 abi_long ret;
499d8055 1589 ret = pipe2(host_pipe, flags);
099d6b0f
RV
1590
1591 if (is_error(ret))
1592 return get_errno(ret);
fb41a66e
RH
1593
1594 /* Several targets have special calling conventions for the original
1595 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1596 if (!is_pipe2) {
1597#if defined(TARGET_ALPHA)
0effdc29 1598 cpu_env->ir[IR_A4] = host_pipe[1];
fb41a66e
RH
1599 return host_pipe[0];
1600#elif defined(TARGET_MIPS)
0effdc29 1601 cpu_env->active_tc.gpr[3] = host_pipe[1];
fb41a66e
RH
1602 return host_pipe[0];
1603#elif defined(TARGET_SH4)
0effdc29 1604 cpu_env->gregs[1] = host_pipe[1];
fb41a66e 1605 return host_pipe[0];
82f05b69 1606#elif defined(TARGET_SPARC)
0effdc29 1607 cpu_env->regwptr[1] = host_pipe[1];
82f05b69 1608 return host_pipe[0];
597c0212 1609#endif
fb41a66e
RH
1610 }
1611
099d6b0f 1612 if (put_user_s32(host_pipe[0], pipedes)
6f200f51 1613 || put_user_s32(host_pipe[1], pipedes + sizeof(abi_int)))
099d6b0f 1614 return -TARGET_EFAULT;
099d6b0f
RV
1615 return get_errno(ret);
1616}
1617
b975b83b
LL
1618static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1619 abi_ulong target_addr,
1620 socklen_t len)
1621{
1622 struct target_ip_mreqn *target_smreqn;
1623
1624 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1625 if (!target_smreqn)
1626 return -TARGET_EFAULT;
1627 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1628 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1629 if (len == sizeof(struct target_ip_mreqn))
cbb21eed 1630 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
b975b83b
LL
1631 unlock_user(target_smreqn, target_addr, 0);
1632
1633 return 0;
1634}
1635
7b36f782 1636static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
579a97f7
FB
1637 abi_ulong target_addr,
1638 socklen_t len)
7854b056 1639{
607175e0
AJ
1640 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1641 sa_family_t sa_family;
53a5960a
PB
1642 struct target_sockaddr *target_saddr;
1643
7b36f782
LV
1644 if (fd_trans_target_to_host_addr(fd)) {
1645 return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
1646 }
1647
579a97f7
FB
1648 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1649 if (!target_saddr)
1650 return -TARGET_EFAULT;
607175e0
AJ
1651
1652 sa_family = tswap16(target_saddr->sa_family);
1653
1654 /* Oops. The caller might send a incomplete sun_path; sun_path
1655 * must be terminated by \0 (see the manual page), but
1656 * unfortunately it is quite common to specify sockaddr_un
1657 * length as "strlen(x->sun_path)" while it should be
1658 * "strlen(...) + 1". We'll fix that here if needed.
1659 * Linux kernel has a similar feature.
1660 */
1661
1662 if (sa_family == AF_UNIX) {
1663 if (len < unix_maxlen && len > 0) {
1664 char *cp = (char*)target_saddr;
1665
1666 if ( cp[len-1] && !cp[len] )
1667 len++;
1668 }
1669 if (len > unix_maxlen)
1670 len = unix_maxlen;
1671 }
1672
53a5960a 1673 memcpy(addr, target_saddr, len);
607175e0 1674 addr->sa_family = sa_family;
6c5b5645
LV
1675 if (sa_family == AF_NETLINK) {
1676 struct sockaddr_nl *nladdr;
1677
1678 nladdr = (struct sockaddr_nl *)addr;
1679 nladdr->nl_pid = tswap32(nladdr->nl_pid);
1680 nladdr->nl_groups = tswap32(nladdr->nl_groups);
1681 } else if (sa_family == AF_PACKET) {
33a29b51
JT
1682 struct target_sockaddr_ll *lladdr;
1683
1684 lladdr = (struct target_sockaddr_ll *)addr;
1685 lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
1686 lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
44cf6731
MM
1687 } else if (sa_family == AF_INET6) {
1688 struct sockaddr_in6 *in6addr;
1689
1690 in6addr = (struct sockaddr_in6 *)addr;
1691 in6addr->sin6_scope_id = tswap32(in6addr->sin6_scope_id);
33a29b51 1692 }
53a5960a 1693 unlock_user(target_saddr, target_addr, 0);
579a97f7
FB
1694
1695 return 0;
7854b056
FB
1696}
1697
579a97f7
FB
1698static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1699 struct sockaddr *addr,
1700 socklen_t len)
7854b056 1701{
53a5960a
PB
1702 struct target_sockaddr *target_saddr;
1703
a1e22192
PM
1704 if (len == 0) {
1705 return 0;
1706 }
6860710c 1707 assert(addr);
a1e22192 1708
579a97f7
FB
1709 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1710 if (!target_saddr)
1711 return -TARGET_EFAULT;
53a5960a 1712 memcpy(target_saddr, addr, len);
a1e22192
PM
1713 if (len >= offsetof(struct target_sockaddr, sa_family) +
1714 sizeof(target_saddr->sa_family)) {
1715 target_saddr->sa_family = tswap16(addr->sa_family);
1716 }
a47401bc
PMD
1717 if (addr->sa_family == AF_NETLINK &&
1718 len >= sizeof(struct target_sockaddr_nl)) {
1719 struct target_sockaddr_nl *target_nl =
1720 (struct target_sockaddr_nl *)target_saddr;
6c5b5645
LV
1721 target_nl->nl_pid = tswap32(target_nl->nl_pid);
1722 target_nl->nl_groups = tswap32(target_nl->nl_groups);
a82ea939
LV
1723 } else if (addr->sa_family == AF_PACKET) {
1724 struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
1725 target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
1726 target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
ee1ac3a1
HD
1727 } else if (addr->sa_family == AF_INET6 &&
1728 len >= sizeof(struct target_sockaddr_in6)) {
1729 struct target_sockaddr_in6 *target_in6 =
1730 (struct target_sockaddr_in6 *)target_saddr;
1731 target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id);
6c5b5645 1732 }
53a5960a 1733 unlock_user(target_saddr, target_addr, len);
579a97f7
FB
1734
1735 return 0;
7854b056
FB
1736}
1737
5a4a898d
FB
1738static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1739 struct target_msghdr *target_msgh)
7854b056
FB
1740{
1741 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
5a4a898d
FB
1742 abi_long msg_controllen;
1743 abi_ulong target_cmsg_addr;
ee104587 1744 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
7854b056 1745 socklen_t space = 0;
5a4a898d 1746
cbb21eed 1747 msg_controllen = tswapal(target_msgh->msg_controllen);
5a4a898d
FB
1748 if (msg_controllen < sizeof (struct target_cmsghdr))
1749 goto the_end;
cbb21eed 1750 target_cmsg_addr = tswapal(target_msgh->msg_control);
5a4a898d 1751 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
ee104587 1752 target_cmsg_start = target_cmsg;
5a4a898d
FB
1753 if (!target_cmsg)
1754 return -TARGET_EFAULT;
7854b056
FB
1755
1756 while (cmsg && target_cmsg) {
1757 void *data = CMSG_DATA(cmsg);
1758 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1759
cbb21eed 1760 int len = tswapal(target_cmsg->cmsg_len)
ad762b99 1761 - sizeof(struct target_cmsghdr);
7854b056
FB
1762
1763 space += CMSG_SPACE(len);
1764 if (space > msgh->msg_controllen) {
1765 space -= CMSG_SPACE(len);
c2aeb258
PM
1766 /* This is a QEMU bug, since we allocated the payload
1767 * area ourselves (unlike overflow in host-to-target
1768 * conversion, which is just the guest giving us a buffer
1769 * that's too small). It can't happen for the payload types
1770 * we currently support; if it becomes an issue in future
1771 * we would need to improve our allocation strategy to
1772 * something more intelligent than "twice the size of the
1773 * target buffer we're reading from".
1774 */
39be5350
JK
1775 qemu_log_mask(LOG_UNIMP,
1776 ("Unsupported ancillary data %d/%d: "
1777 "unhandled msg size\n"),
1778 tswap32(target_cmsg->cmsg_level),
1779 tswap32(target_cmsg->cmsg_type));
7854b056
FB
1780 break;
1781 }
1782
dbf4f796
PJ
1783 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1784 cmsg->cmsg_level = SOL_SOCKET;
1785 } else {
1786 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1787 }
7854b056
FB
1788 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1789 cmsg->cmsg_len = CMSG_LEN(len);
1790
30b8b68e 1791 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
7854b056
FB
1792 int *fd = (int *)data;
1793 int *target_fd = (int *)target_data;
1794 int i, numfds = len / sizeof(int);
1795
876e23cb
PM
1796 for (i = 0; i < numfds; i++) {
1797 __get_user(fd[i], target_fd + i);
1798 }
30b8b68e
AS
1799 } else if (cmsg->cmsg_level == SOL_SOCKET
1800 && cmsg->cmsg_type == SCM_CREDENTIALS) {
1801 struct ucred *cred = (struct ucred *)data;
1802 struct target_ucred *target_cred =
1803 (struct target_ucred *)target_data;
1804
876e23cb
PM
1805 __get_user(cred->pid, &target_cred->pid);
1806 __get_user(cred->uid, &target_cred->uid);
1807 __get_user(cred->gid, &target_cred->gid);
27404b6c
HD
1808 } else if (cmsg->cmsg_level == SOL_ALG) {
1809 uint32_t *dst = (uint32_t *)data;
1810
1811 memcpy(dst, target_data, len);
669dcb60 1812 /* fix endianness of first 32-bit word */
27404b6c
HD
1813 if (len >= sizeof(uint32_t)) {
1814 *dst = tswap32(*dst);
1815 }
30b8b68e 1816 } else {
39be5350
JK
1817 qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
1818 cmsg->cmsg_level, cmsg->cmsg_type);
30b8b68e 1819 memcpy(data, target_data, len);
7854b056
FB
1820 }
1821
1822 cmsg = CMSG_NXTHDR(msgh, cmsg);
ee104587
JN
1823 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1824 target_cmsg_start);
7854b056 1825 }
5a4a898d
FB
1826 unlock_user(target_cmsg, target_cmsg_addr, 0);
1827 the_end:
7854b056 1828 msgh->msg_controllen = space;
5a4a898d 1829 return 0;
7854b056
FB
1830}
1831
5a4a898d
FB
1832static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1833 struct msghdr *msgh)
7854b056
FB
1834{
1835 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
5a4a898d
FB
1836 abi_long msg_controllen;
1837 abi_ulong target_cmsg_addr;
ee104587 1838 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
7854b056
FB
1839 socklen_t space = 0;
1840
cbb21eed 1841 msg_controllen = tswapal(target_msgh->msg_controllen);
5a4a898d
FB
1842 if (msg_controllen < sizeof (struct target_cmsghdr))
1843 goto the_end;
cbb21eed 1844 target_cmsg_addr = tswapal(target_msgh->msg_control);
5a4a898d 1845 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
ee104587 1846 target_cmsg_start = target_cmsg;
5a4a898d
FB
1847 if (!target_cmsg)
1848 return -TARGET_EFAULT;
1849
7854b056
FB
1850 while (cmsg && target_cmsg) {
1851 void *data = CMSG_DATA(cmsg);
1852 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1853
ad762b99 1854 int len = cmsg->cmsg_len - sizeof(struct cmsghdr);
c2aeb258 1855 int tgt_len, tgt_space;
7854b056 1856
c2aeb258
PM
1857 /* We never copy a half-header but may copy half-data;
1858 * this is Linux's behaviour in put_cmsg(). Note that
1859 * truncation here is a guest problem (which we report
1860 * to the guest via the CTRUNC bit), unlike truncation
1861 * in target_to_host_cmsg, which is a QEMU bug.
1862 */
7174970a 1863 if (msg_controllen < sizeof(struct target_cmsghdr)) {
c2aeb258 1864 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
7854b056
FB
1865 break;
1866 }
1867
dbf4f796
PJ
1868 if (cmsg->cmsg_level == SOL_SOCKET) {
1869 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1870 } else {
1871 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1872 }
7854b056 1873 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
7854b056 1874
c2aeb258
PM
1875 /* Payload types which need a different size of payload on
1876 * the target must adjust tgt_len here.
1877 */
309786cf 1878 tgt_len = len;
c2aeb258
PM
1879 switch (cmsg->cmsg_level) {
1880 case SOL_SOCKET:
1881 switch (cmsg->cmsg_type) {
1882 case SO_TIMESTAMP:
1883 tgt_len = sizeof(struct target_timeval);
1884 break;
1885 default:
1886 break;
1887 }
309786cf 1888 break;
c2aeb258
PM
1889 default:
1890 break;
1891 }
1892
7174970a 1893 if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) {
c2aeb258 1894 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
7174970a 1895 tgt_len = msg_controllen - sizeof(struct target_cmsghdr);
c2aeb258
PM
1896 }
1897
1898 /* We must now copy-and-convert len bytes of payload
1899 * into tgt_len bytes of destination space. Bear in mind
1900 * that in both source and destination we may be dealing
1901 * with a truncated value!
1902 */
52b65494
HD
1903 switch (cmsg->cmsg_level) {
1904 case SOL_SOCKET:
1905 switch (cmsg->cmsg_type) {
1906 case SCM_RIGHTS:
1907 {
1908 int *fd = (int *)data;
1909 int *target_fd = (int *)target_data;
c2aeb258 1910 int i, numfds = tgt_len / sizeof(int);
52b65494 1911
876e23cb
PM
1912 for (i = 0; i < numfds; i++) {
1913 __put_user(fd[i], target_fd + i);
1914 }
52b65494
HD
1915 break;
1916 }
1917 case SO_TIMESTAMP:
1918 {
1919 struct timeval *tv = (struct timeval *)data;
1920 struct target_timeval *target_tv =
1921 (struct target_timeval *)target_data;
1922
c2aeb258
PM
1923 if (len != sizeof(struct timeval) ||
1924 tgt_len != sizeof(struct target_timeval)) {
52b65494 1925 goto unimplemented;
c2aeb258 1926 }
52b65494
HD
1927
1928 /* copy struct timeval to target */
876e23cb
PM
1929 __put_user(tv->tv_sec, &target_tv->tv_sec);
1930 __put_user(tv->tv_usec, &target_tv->tv_usec);
52b65494
HD
1931 break;
1932 }
4bc29756
HD
1933 case SCM_CREDENTIALS:
1934 {
1935 struct ucred *cred = (struct ucred *)data;
1936 struct target_ucred *target_cred =
1937 (struct target_ucred *)target_data;
1938
1939 __put_user(cred->pid, &target_cred->pid);
1940 __put_user(cred->uid, &target_cred->uid);
1941 __put_user(cred->gid, &target_cred->gid);
1942 break;
1943 }
52b65494
HD
1944 default:
1945 goto unimplemented;
1946 }
1947 break;
7854b056 1948
ee1ac3a1
HD
1949 case SOL_IP:
1950 switch (cmsg->cmsg_type) {
1951 case IP_TTL:
1952 {
1953 uint32_t *v = (uint32_t *)data;
1954 uint32_t *t_int = (uint32_t *)target_data;
1955
7174970a
PM
1956 if (len != sizeof(uint32_t) ||
1957 tgt_len != sizeof(uint32_t)) {
1958 goto unimplemented;
1959 }
ee1ac3a1
HD
1960 __put_user(*v, t_int);
1961 break;
1962 }
1963 case IP_RECVERR:
1964 {
1965 struct errhdr_t {
1966 struct sock_extended_err ee;
1967 struct sockaddr_in offender;
1968 };
1969 struct errhdr_t *errh = (struct errhdr_t *)data;
1970 struct errhdr_t *target_errh =
1971 (struct errhdr_t *)target_data;
1972
7174970a
PM
1973 if (len != sizeof(struct errhdr_t) ||
1974 tgt_len != sizeof(struct errhdr_t)) {
1975 goto unimplemented;
1976 }
ee1ac3a1
HD
1977 __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
1978 __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
1979 __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
1980 __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
1981 __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
1982 __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
1983 __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
1984 host_to_target_sockaddr((unsigned long) &target_errh->offender,
1985 (void *) &errh->offender, sizeof(errh->offender));
1986 break;
1987 }
1988 default:
1989 goto unimplemented;
1990 }
1991 break;
1992
1993 case SOL_IPV6:
1994 switch (cmsg->cmsg_type) {
1995 case IPV6_HOPLIMIT:
1996 {
1997 uint32_t *v = (uint32_t *)data;
1998 uint32_t *t_int = (uint32_t *)target_data;
1999
7174970a
PM
2000 if (len != sizeof(uint32_t) ||
2001 tgt_len != sizeof(uint32_t)) {
2002 goto unimplemented;
2003 }
ee1ac3a1
HD
2004 __put_user(*v, t_int);
2005 break;
2006 }
2007 case IPV6_RECVERR:
2008 {
2009 struct errhdr6_t {
2010 struct sock_extended_err ee;
2011 struct sockaddr_in6 offender;
2012 };
2013 struct errhdr6_t *errh = (struct errhdr6_t *)data;
2014 struct errhdr6_t *target_errh =
2015 (struct errhdr6_t *)target_data;
2016
7174970a
PM
2017 if (len != sizeof(struct errhdr6_t) ||
2018 tgt_len != sizeof(struct errhdr6_t)) {
2019 goto unimplemented;
2020 }
ee1ac3a1
HD
2021 __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
2022 __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
2023 __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
2024 __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
2025 __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
2026 __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
2027 __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
2028 host_to_target_sockaddr((unsigned long) &target_errh->offender,
2029 (void *) &errh->offender, sizeof(errh->offender));
2030 break;
2031 }
2032 default:
2033 goto unimplemented;
2034 }
2035 break;
2036
52b65494
HD
2037 default:
2038 unimplemented:
39be5350
JK
2039 qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
2040 cmsg->cmsg_level, cmsg->cmsg_type);
c2aeb258
PM
2041 memcpy(target_data, data, MIN(len, tgt_len));
2042 if (tgt_len > len) {
2043 memset(target_data + len, 0, tgt_len - len);
2044 }
7854b056
FB
2045 }
2046
7174970a
PM
2047 target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len));
2048 tgt_space = TARGET_CMSG_SPACE(tgt_len);
c2aeb258
PM
2049 if (msg_controllen < tgt_space) {
2050 tgt_space = msg_controllen;
2051 }
2052 msg_controllen -= tgt_space;
2053 space += tgt_space;
7854b056 2054 cmsg = CMSG_NXTHDR(msgh, cmsg);
ee104587
JN
2055 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
2056 target_cmsg_start);
7854b056 2057 }
5a4a898d
FB
2058 unlock_user(target_cmsg, target_cmsg_addr, space);
2059 the_end:
cbb21eed 2060 target_msgh->msg_controllen = tswapal(space);
5a4a898d 2061 return 0;
7854b056
FB
2062}
2063
0da46a6e 2064/* do_setsockopt() Must return target values and target errnos. */
992f48a0 2065static abi_long do_setsockopt(int sockfd, int level, int optname,
2f619698 2066 abi_ulong optval_addr, socklen_t optlen)
7854b056 2067{
992f48a0 2068 abi_long ret;
32407103 2069 int val;
b975b83b 2070 struct ip_mreqn *ip_mreq;
6e3cb58f 2071 struct ip_mreq_source *ip_mreq_source;
3b46e624 2072
8853f86e
FB
2073 switch(level) {
2074 case SOL_TCP:
fe51b0a5
SCW
2075 case SOL_UDP:
2076 /* TCP and UDP options all take an 'int' value. */
7854b056 2077 if (optlen < sizeof(uint32_t))
0da46a6e 2078 return -TARGET_EINVAL;
3b46e624 2079
2f619698
FB
2080 if (get_user_u32(val, optval_addr))
2081 return -TARGET_EFAULT;
8853f86e
FB
2082 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2083 break;
2084 case SOL_IP:
2085 switch(optname) {
2efbe911
FB
2086 case IP_TOS:
2087 case IP_TTL:
8853f86e 2088 case IP_HDRINCL:
2efbe911
FB
2089 case IP_ROUTER_ALERT:
2090 case IP_RECVOPTS:
2091 case IP_RETOPTS:
2092 case IP_PKTINFO:
2093 case IP_MTU_DISCOVER:
2094 case IP_RECVERR:
ee1ac3a1 2095 case IP_RECVTTL:
2efbe911
FB
2096 case IP_RECVTOS:
2097#ifdef IP_FREEBIND
2098 case IP_FREEBIND:
2099#endif
2100 case IP_MULTICAST_TTL:
2101 case IP_MULTICAST_LOOP:
8853f86e
FB
2102 val = 0;
2103 if (optlen >= sizeof(uint32_t)) {
2f619698
FB
2104 if (get_user_u32(val, optval_addr))
2105 return -TARGET_EFAULT;
8853f86e 2106 } else if (optlen >= 1) {
2f619698
FB
2107 if (get_user_u8(val, optval_addr))
2108 return -TARGET_EFAULT;
8853f86e
FB
2109 }
2110 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2111 break;
b975b83b
LL
2112 case IP_ADD_MEMBERSHIP:
2113 case IP_DROP_MEMBERSHIP:
2114 if (optlen < sizeof (struct target_ip_mreq) ||
2115 optlen > sizeof (struct target_ip_mreqn))
2116 return -TARGET_EINVAL;
2117
2118 ip_mreq = (struct ip_mreqn *) alloca(optlen);
2119 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
2120 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
2121 break;
2122
6e3cb58f
LL
2123 case IP_BLOCK_SOURCE:
2124 case IP_UNBLOCK_SOURCE:
2125 case IP_ADD_SOURCE_MEMBERSHIP:
2126 case IP_DROP_SOURCE_MEMBERSHIP:
2127 if (optlen != sizeof (struct target_ip_mreq_source))
2128 return -TARGET_EINVAL;
2129
2130 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
74e43b04
PM
2131 if (!ip_mreq_source) {
2132 return -TARGET_EFAULT;
2133 }
6e3cb58f
LL
2134 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
2135 unlock_user (ip_mreq_source, optval_addr, 0);
2136 break;
2137
920394db
JH
2138 default:
2139 goto unimplemented;
2140 }
2141 break;
0d78b3b5
LV
2142 case SOL_IPV6:
2143 switch (optname) {
2144 case IPV6_MTU_DISCOVER:
2145 case IPV6_MTU:
2146 case IPV6_V6ONLY:
2147 case IPV6_RECVPKTINFO:
ee1ac3a1 2148 case IPV6_UNICAST_HOPS:
21749c4c
LV
2149 case IPV6_MULTICAST_HOPS:
2150 case IPV6_MULTICAST_LOOP:
ee1ac3a1
HD
2151 case IPV6_RECVERR:
2152 case IPV6_RECVHOPLIMIT:
2153 case IPV6_2292HOPLIMIT:
2154 case IPV6_CHECKSUM:
b9cce6d7
HD
2155 case IPV6_ADDRFORM:
2156 case IPV6_2292PKTINFO:
2157 case IPV6_RECVTCLASS:
2158 case IPV6_RECVRTHDR:
2159 case IPV6_2292RTHDR:
2160 case IPV6_RECVHOPOPTS:
2161 case IPV6_2292HOPOPTS:
2162 case IPV6_RECVDSTOPTS:
2163 case IPV6_2292DSTOPTS:
2164 case IPV6_TCLASS:
22db1213 2165 case IPV6_ADDR_PREFERENCES:
b9cce6d7
HD
2166#ifdef IPV6_RECVPATHMTU
2167 case IPV6_RECVPATHMTU:
2168#endif
2169#ifdef IPV6_TRANSPARENT
2170 case IPV6_TRANSPARENT:
2171#endif
2172#ifdef IPV6_FREEBIND
2173 case IPV6_FREEBIND:
2174#endif
2175#ifdef IPV6_RECVORIGDSTADDR
2176 case IPV6_RECVORIGDSTADDR:
2177#endif
0d78b3b5
LV
2178 val = 0;
2179 if (optlen < sizeof(uint32_t)) {
2180 return -TARGET_EINVAL;
2181 }
2182 if (get_user_u32(val, optval_addr)) {
2183 return -TARGET_EFAULT;
2184 }
2185 ret = get_errno(setsockopt(sockfd, level, optname,
2186 &val, sizeof(val)));
2187 break;
ee1ac3a1
HD
2188 case IPV6_PKTINFO:
2189 {
2190 struct in6_pktinfo pki;
2191
2192 if (optlen < sizeof(pki)) {
2193 return -TARGET_EINVAL;
2194 }
2195
2196 if (copy_from_user(&pki, optval_addr, sizeof(pki))) {
2197 return -TARGET_EFAULT;
2198 }
2199
2200 pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex);
2201
2202 ret = get_errno(setsockopt(sockfd, level, optname,
2203 &pki, sizeof(pki)));
2204 break;
2205 }
22bf4ee9
NC
2206 case IPV6_ADD_MEMBERSHIP:
2207 case IPV6_DROP_MEMBERSHIP:
2208 {
2209 struct ipv6_mreq ipv6mreq;
2210
2211 if (optlen < sizeof(ipv6mreq)) {
2212 return -TARGET_EINVAL;
2213 }
2214
2215 if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) {
2216 return -TARGET_EFAULT;
2217 }
2218
2219 ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
2220
2221 ret = get_errno(setsockopt(sockfd, level, optname,
2222 &ipv6mreq, sizeof(ipv6mreq)));
2223 break;
2224 }
ee1ac3a1
HD
2225 default:
2226 goto unimplemented;
2227 }
2228 break;
2229 case SOL_ICMPV6:
2230 switch (optname) {
2231 case ICMPV6_FILTER:
2232 {
2233 struct icmp6_filter icmp6f;
2234
2235 if (optlen > sizeof(icmp6f)) {
2236 optlen = sizeof(icmp6f);
2237 }
2238
2239 if (copy_from_user(&icmp6f, optval_addr, optlen)) {
2240 return -TARGET_EFAULT;
2241 }
2242
2243 for (val = 0; val < 8; val++) {
2244 icmp6f.data[val] = tswap32(icmp6f.data[val]);
2245 }
2246
2247 ret = get_errno(setsockopt(sockfd, level, optname,
2248 &icmp6f, optlen));
2249 break;
2250 }
0d78b3b5
LV
2251 default:
2252 goto unimplemented;
2253 }
2254 break;
920394db
JH
2255 case SOL_RAW:
2256 switch (optname) {
2257 case ICMP_FILTER:
ee1ac3a1
HD
2258 case IPV6_CHECKSUM:
2259 /* those take an u32 value */
920394db
JH
2260 if (optlen < sizeof(uint32_t)) {
2261 return -TARGET_EINVAL;
2262 }
2263
2264 if (get_user_u32(val, optval_addr)) {
2265 return -TARGET_EFAULT;
2266 }
2267 ret = get_errno(setsockopt(sockfd, level, optname,
2268 &val, sizeof(val)));
2269 break;
2270
8853f86e
FB
2271 default:
2272 goto unimplemented;
2273 }
2274 break;
f31dddd2
YS
2275#if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
2276 case SOL_ALG:
2277 switch (optname) {
2278 case ALG_SET_KEY:
2279 {
2280 char *alg_key = g_malloc(optlen);
2281
2282 if (!alg_key) {
2283 return -TARGET_ENOMEM;
2284 }
2285 if (copy_from_user(alg_key, optval_addr, optlen)) {
2286 g_free(alg_key);
2287 return -TARGET_EFAULT;
2288 }
2289 ret = get_errno(setsockopt(sockfd, level, optname,
2290 alg_key, optlen));
2291 g_free(alg_key);
2292 break;
2293 }
2294 case ALG_SET_AEAD_AUTHSIZE:
2295 {
2296 ret = get_errno(setsockopt(sockfd, level, optname,
2297 NULL, optlen));
2298 break;
2299 }
2300 default:
2301 goto unimplemented;
2302 }
2303 break;
2304#endif
3532fa74 2305 case TARGET_SOL_SOCKET:
8853f86e 2306 switch (optname) {
1b09aeb9
LV
2307 case TARGET_SO_RCVTIMEO:
2308 {
2309 struct timeval tv;
2310
2311 optname = SO_RCVTIMEO;
2312
2313set_timeout:
2314 if (optlen != sizeof(struct target_timeval)) {
2315 return -TARGET_EINVAL;
2316 }
2317
2318 if (copy_from_user_timeval(&tv, optval_addr)) {
2319 return -TARGET_EFAULT;
2320 }
2321
2322 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2323 &tv, sizeof(tv)));
2324 return ret;
2325 }
2326 case TARGET_SO_SNDTIMEO:
2327 optname = SO_SNDTIMEO;
2328 goto set_timeout;
f57d4192
LV
2329 case TARGET_SO_ATTACH_FILTER:
2330 {
2331 struct target_sock_fprog *tfprog;
2332 struct target_sock_filter *tfilter;
2333 struct sock_fprog fprog;
2334 struct sock_filter *filter;
2335 int i;
2336
2337 if (optlen != sizeof(*tfprog)) {
2338 return -TARGET_EINVAL;
2339 }
2340 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
2341 return -TARGET_EFAULT;
2342 }
2343 if (!lock_user_struct(VERIFY_READ, tfilter,
2344 tswapal(tfprog->filter), 0)) {
2345 unlock_user_struct(tfprog, optval_addr, 1);
2346 return -TARGET_EFAULT;
2347 }
2348
2349 fprog.len = tswap16(tfprog->len);
0e173b24 2350 filter = g_try_new(struct sock_filter, fprog.len);
f57d4192
LV
2351 if (filter == NULL) {
2352 unlock_user_struct(tfilter, tfprog->filter, 1);
2353 unlock_user_struct(tfprog, optval_addr, 1);
2354 return -TARGET_ENOMEM;
2355 }
2356 for (i = 0; i < fprog.len; i++) {
2357 filter[i].code = tswap16(tfilter[i].code);
2358 filter[i].jt = tfilter[i].jt;
2359 filter[i].jf = tfilter[i].jf;
2360 filter[i].k = tswap32(tfilter[i].k);
2361 }
2362 fprog.filter = filter;
2363
2364 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
2365 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
0e173b24 2366 g_free(filter);
f57d4192
LV
2367
2368 unlock_user_struct(tfilter, tfprog->filter, 1);
2369 unlock_user_struct(tfprog, optval_addr, 1);
2370 return ret;
2371 }
451aaf68
JT
2372 case TARGET_SO_BINDTODEVICE:
2373 {
2374 char *dev_ifname, *addr_ifname;
2375
2376 if (optlen > IFNAMSIZ - 1) {
2377 optlen = IFNAMSIZ - 1;
2378 }
2379 dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2380 if (!dev_ifname) {
2381 return -TARGET_EFAULT;
2382 }
2383 optname = SO_BINDTODEVICE;
2384 addr_ifname = alloca(IFNAMSIZ);
2385 memcpy(addr_ifname, dev_ifname, optlen);
2386 addr_ifname[optlen] = 0;
fad6c58a
CG
2387 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2388 addr_ifname, optlen));
451aaf68
JT
2389 unlock_user (dev_ifname, optval_addr, 0);
2390 return ret;
2391 }
83eb6e50
CMAB
2392 case TARGET_SO_LINGER:
2393 {
2394 struct linger lg;
2395 struct target_linger *tlg;
2396
2397 if (optlen != sizeof(struct target_linger)) {
2398 return -TARGET_EINVAL;
2399 }
2400 if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) {
2401 return -TARGET_EFAULT;
2402 }
2403 __get_user(lg.l_onoff, &tlg->l_onoff);
2404 __get_user(lg.l_linger, &tlg->l_linger);
2405 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER,
2406 &lg, sizeof(lg)));
2407 unlock_user_struct(tlg, optval_addr, 0);
2408 return ret;
2409 }
8853f86e 2410 /* Options with 'int' argument. */
3532fa74
FB
2411 case TARGET_SO_DEBUG:
2412 optname = SO_DEBUG;
2413 break;
2414 case TARGET_SO_REUSEADDR:
2415 optname = SO_REUSEADDR;
2416 break;
113a9dd7
YS
2417#ifdef SO_REUSEPORT
2418 case TARGET_SO_REUSEPORT:
2419 optname = SO_REUSEPORT;
2420 break;
2421#endif
3532fa74
FB
2422 case TARGET_SO_TYPE:
2423 optname = SO_TYPE;
2424 break;
2425 case TARGET_SO_ERROR:
2426 optname = SO_ERROR;
2427 break;
2428 case TARGET_SO_DONTROUTE:
2429 optname = SO_DONTROUTE;
2430 break;
2431 case TARGET_SO_BROADCAST:
2432 optname = SO_BROADCAST;
2433 break;
2434 case TARGET_SO_SNDBUF:
2435 optname = SO_SNDBUF;
2436 break;
d79b6cc4
PB
2437 case TARGET_SO_SNDBUFFORCE:
2438 optname = SO_SNDBUFFORCE;
2439 break;
3532fa74
FB
2440 case TARGET_SO_RCVBUF:
2441 optname = SO_RCVBUF;
2442 break;
d79b6cc4
PB
2443 case TARGET_SO_RCVBUFFORCE:
2444 optname = SO_RCVBUFFORCE;
2445 break;
3532fa74
FB
2446 case TARGET_SO_KEEPALIVE:
2447 optname = SO_KEEPALIVE;
2448 break;
2449 case TARGET_SO_OOBINLINE:
2450 optname = SO_OOBINLINE;
2451 break;
2452 case TARGET_SO_NO_CHECK:
2453 optname = SO_NO_CHECK;
2454 break;
2455 case TARGET_SO_PRIORITY:
2456 optname = SO_PRIORITY;
2457 break;
5e83e8e3 2458#ifdef SO_BSDCOMPAT
3532fa74
FB
2459 case TARGET_SO_BSDCOMPAT:
2460 optname = SO_BSDCOMPAT;
2461 break;
5e83e8e3 2462#endif
3532fa74
FB
2463 case TARGET_SO_PASSCRED:
2464 optname = SO_PASSCRED;
2465 break;
82d0fe6b
PB
2466 case TARGET_SO_PASSSEC:
2467 optname = SO_PASSSEC;
2468 break;
3532fa74
FB
2469 case TARGET_SO_TIMESTAMP:
2470 optname = SO_TIMESTAMP;
2471 break;
2472 case TARGET_SO_RCVLOWAT:
2473 optname = SO_RCVLOWAT;
2474 break;
8853f86e
FB
2475 default:
2476 goto unimplemented;
2477 }
3532fa74 2478 if (optlen < sizeof(uint32_t))
2f619698 2479 return -TARGET_EINVAL;
3532fa74 2480
2f619698
FB
2481 if (get_user_u32(val, optval_addr))
2482 return -TARGET_EFAULT;
3532fa74 2483 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
8853f86e 2484 break;
a2d86682
JK
2485#ifdef SOL_NETLINK
2486 case SOL_NETLINK:
2487 switch (optname) {
2488 case NETLINK_PKTINFO:
2489 case NETLINK_ADD_MEMBERSHIP:
2490 case NETLINK_DROP_MEMBERSHIP:
2491 case NETLINK_BROADCAST_ERROR:
2492 case NETLINK_NO_ENOBUFS:
2493#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2494 case NETLINK_LISTEN_ALL_NSID:
2495 case NETLINK_CAP_ACK:
2496#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2497#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2498 case NETLINK_EXT_ACK:
2499#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2500#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2501 case NETLINK_GET_STRICT_CHK:
2502#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2503 break;
2504 default:
2505 goto unimplemented;
2506 }
2507 val = 0;
2508 if (optlen < sizeof(uint32_t)) {
2509 return -TARGET_EINVAL;
2510 }
2511 if (get_user_u32(val, optval_addr)) {
2512 return -TARGET_EFAULT;
2513 }
2514 ret = get_errno(setsockopt(sockfd, SOL_NETLINK, optname, &val,
2515 sizeof(val)));
2516 break;
2517#endif /* SOL_NETLINK */
7854b056 2518 default:
8853f86e 2519 unimplemented:
39be5350
JK
2520 qemu_log_mask(LOG_UNIMP, "Unsupported setsockopt level=%d optname=%d\n",
2521 level, optname);
6fa13c17 2522 ret = -TARGET_ENOPROTOOPT;
7854b056 2523 }
8853f86e 2524 return ret;
7854b056
FB
2525}
2526
0da46a6e 2527/* do_getsockopt() Must return target values and target errnos. */
992f48a0 2528static abi_long do_getsockopt(int sockfd, int level, int optname,
2f619698 2529 abi_ulong optval_addr, abi_ulong optlen)
7854b056 2530{
992f48a0 2531 abi_long ret;
b55266b5
BS
2532 int len, val;
2533 socklen_t lv;
8853f86e
FB
2534
2535 switch(level) {
3532fa74 2536 case TARGET_SOL_SOCKET:
f3b974cd
JL
2537 level = SOL_SOCKET;
2538 switch (optname) {
2539 /* These don't just return a single integer */
f3b974cd
JL
2540 case TARGET_SO_PEERNAME:
2541 goto unimplemented;
405dc4cf
AS
2542 case TARGET_SO_RCVTIMEO: {
2543 struct timeval tv;
2544 socklen_t tvlen;
2545
2546 optname = SO_RCVTIMEO;
2547
2548get_timeout:
2549 if (get_user_u32(len, optlen)) {
2550 return -TARGET_EFAULT;
2551 }
2552 if (len < 0) {
2553 return -TARGET_EINVAL;
2554 }
2555
2556 tvlen = sizeof(tv);
2557 ret = get_errno(getsockopt(sockfd, level, optname,
2558 &tv, &tvlen));
2559 if (ret < 0) {
2560 return ret;
2561 }
2562 if (len > sizeof(struct target_timeval)) {
2563 len = sizeof(struct target_timeval);
2564 }
2565 if (copy_to_user_timeval(optval_addr, &tv)) {
2566 return -TARGET_EFAULT;
2567 }
2568 if (put_user_u32(len, optlen)) {
2569 return -TARGET_EFAULT;
2570 }
2571 break;
2572 }
2573 case TARGET_SO_SNDTIMEO:
2574 optname = SO_SNDTIMEO;
2575 goto get_timeout;
583359a6
AP
2576 case TARGET_SO_PEERCRED: {
2577 struct ucred cr;
2578 socklen_t crlen;
2579 struct target_ucred *tcr;
2580
2581 if (get_user_u32(len, optlen)) {
2582 return -TARGET_EFAULT;
2583 }
2584 if (len < 0) {
2585 return -TARGET_EINVAL;
2586 }
2587
2588 crlen = sizeof(cr);
2589 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
2590 &cr, &crlen));
2591 if (ret < 0) {
2592 return ret;
2593 }
2594 if (len > crlen) {
2595 len = crlen;
2596 }
2597 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
2598 return -TARGET_EFAULT;
2599 }
2600 __put_user(cr.pid, &tcr->pid);
2601 __put_user(cr.uid, &tcr->uid);
2602 __put_user(cr.gid, &tcr->gid);
2603 unlock_user_struct(tcr, optval_addr, 1);
2604 if (put_user_u32(len, optlen)) {
83eb6e50
CMAB
2605 return -TARGET_EFAULT;
2606 }
2607 break;
2608 }
6d485a55
LV
2609 case TARGET_SO_PEERSEC: {
2610 char *name;
2611
2612 if (get_user_u32(len, optlen)) {
2613 return -TARGET_EFAULT;
2614 }
2615 if (len < 0) {
2616 return -TARGET_EINVAL;
2617 }
2618 name = lock_user(VERIFY_WRITE, optval_addr, len, 0);
2619 if (!name) {
2620 return -TARGET_EFAULT;
2621 }
2622 lv = len;
2623 ret = get_errno(getsockopt(sockfd, level, SO_PEERSEC,
2624 name, &lv));
2625 if (put_user_u32(lv, optlen)) {
2626 ret = -TARGET_EFAULT;
2627 }
2628 unlock_user(name, optval_addr, lv);
2629 break;
2630 }
83eb6e50
CMAB
2631 case TARGET_SO_LINGER:
2632 {
2633 struct linger lg;
2634 socklen_t lglen;
2635 struct target_linger *tlg;
2636
2637 if (get_user_u32(len, optlen)) {
2638 return -TARGET_EFAULT;
2639 }
2640 if (len < 0) {
2641 return -TARGET_EINVAL;
2642 }
2643
2644 lglen = sizeof(lg);
2645 ret = get_errno(getsockopt(sockfd, level, SO_LINGER,
2646 &lg, &lglen));
2647 if (ret < 0) {
2648 return ret;
2649 }
2650 if (len > lglen) {
2651 len = lglen;
2652 }
2653 if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) {
2654 return -TARGET_EFAULT;
2655 }
2656 __put_user(lg.l_onoff, &tlg->l_onoff);
2657 __put_user(lg.l_linger, &tlg->l_linger);
2658 unlock_user_struct(tlg, optval_addr, 1);
2659 if (put_user_u32(len, optlen)) {
583359a6
AP
2660 return -TARGET_EFAULT;
2661 }
2662 break;
2663 }
f3b974cd
JL
2664 /* Options with 'int' argument. */
2665 case TARGET_SO_DEBUG:
2666 optname = SO_DEBUG;
2667 goto int_case;
2668 case TARGET_SO_REUSEADDR:
2669 optname = SO_REUSEADDR;
2670 goto int_case;
113a9dd7
YS
2671#ifdef SO_REUSEPORT
2672 case TARGET_SO_REUSEPORT:
2673 optname = SO_REUSEPORT;
2674 goto int_case;
2675#endif
f3b974cd
JL
2676 case TARGET_SO_TYPE:
2677 optname = SO_TYPE;
2678 goto int_case;
2679 case TARGET_SO_ERROR:
2680 optname = SO_ERROR;
2681 goto int_case;
2682 case TARGET_SO_DONTROUTE:
2683 optname = SO_DONTROUTE;
2684 goto int_case;
2685 case TARGET_SO_BROADCAST:
2686 optname = SO_BROADCAST;
2687 goto int_case;
2688 case TARGET_SO_SNDBUF:
2689 optname = SO_SNDBUF;
2690 goto int_case;
2691 case TARGET_SO_RCVBUF:
2692 optname = SO_RCVBUF;
2693 goto int_case;
2694 case TARGET_SO_KEEPALIVE:
2695 optname = SO_KEEPALIVE;
2696 goto int_case;
2697 case TARGET_SO_OOBINLINE:
2698 optname = SO_OOBINLINE;
2699 goto int_case;
2700 case TARGET_SO_NO_CHECK:
2701 optname = SO_NO_CHECK;
2702 goto int_case;
2703 case TARGET_SO_PRIORITY:
2704 optname = SO_PRIORITY;
2705 goto int_case;
2706#ifdef SO_BSDCOMPAT
2707 case TARGET_SO_BSDCOMPAT:
2708 optname = SO_BSDCOMPAT;
2709 goto int_case;
2710#endif
2711 case TARGET_SO_PASSCRED:
2712 optname = SO_PASSCRED;
2713 goto int_case;
2714 case TARGET_SO_TIMESTAMP:
2715 optname = SO_TIMESTAMP;
2716 goto int_case;
2717 case TARGET_SO_RCVLOWAT:
2718 optname = SO_RCVLOWAT;
2719 goto int_case;
aec1ca41
PB
2720 case TARGET_SO_ACCEPTCONN:
2721 optname = SO_ACCEPTCONN;
2722 goto int_case;
ec63e06e
JD
2723 case TARGET_SO_PROTOCOL:
2724 optname = SO_PROTOCOL;
2725 goto int_case;
2726 case TARGET_SO_DOMAIN:
2727 optname = SO_DOMAIN;
2728 goto int_case;
8853f86e 2729 default:
2efbe911
FB
2730 goto int_case;
2731 }
2732 break;
2733 case SOL_TCP:
fe51b0a5
SCW
2734 case SOL_UDP:
2735 /* TCP and UDP options all take an 'int' value. */
2efbe911 2736 int_case:
2f619698
FB
2737 if (get_user_u32(len, optlen))
2738 return -TARGET_EFAULT;
2efbe911 2739 if (len < 0)
0da46a6e 2740 return -TARGET_EINVAL;
73160d95 2741 lv = sizeof(lv);
2efbe911
FB
2742 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2743 if (ret < 0)
2744 return ret;
cb88b7c2
HD
2745 switch (optname) {
2746 case SO_TYPE:
8289d112 2747 val = host_to_target_sock_type(val);
cb88b7c2
HD
2748 break;
2749 case SO_ERROR:
2750 val = host_to_target_errno(val);
2751 break;
8289d112 2752 }
2efbe911
FB
2753 if (len > lv)
2754 len = lv;
2f619698
FB
2755 if (len == 4) {
2756 if (put_user_u32(val, optval_addr))
2757 return -TARGET_EFAULT;
2758 } else {
2759 if (put_user_u8(val, optval_addr))
2760 return -TARGET_EFAULT;
f3b974cd 2761 }
2f619698
FB
2762 if (put_user_u32(len, optlen))
2763 return -TARGET_EFAULT;
2efbe911
FB
2764 break;
2765 case SOL_IP:
2766 switch(optname) {
2767 case IP_TOS:
2768 case IP_TTL:
2769 case IP_HDRINCL:
2770 case IP_ROUTER_ALERT:
2771 case IP_RECVOPTS:
2772 case IP_RETOPTS:
2773 case IP_PKTINFO:
2774 case IP_MTU_DISCOVER:
2775 case IP_RECVERR:
2776 case IP_RECVTOS:
2777#ifdef IP_FREEBIND
2778 case IP_FREEBIND:
2779#endif
2780 case IP_MULTICAST_TTL:
2781 case IP_MULTICAST_LOOP:
2f619698
FB
2782 if (get_user_u32(len, optlen))
2783 return -TARGET_EFAULT;
8853f86e 2784 if (len < 0)
0da46a6e 2785 return -TARGET_EINVAL;
73160d95 2786 lv = sizeof(lv);
8853f86e
FB
2787 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2788 if (ret < 0)
2789 return ret;
2efbe911 2790 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2efbe911 2791 len = 1;
2f619698
FB
2792 if (put_user_u32(len, optlen)
2793 || put_user_u8(val, optval_addr))
2794 return -TARGET_EFAULT;
2efbe911 2795 } else {
2efbe911
FB
2796 if (len > sizeof(int))
2797 len = sizeof(int);
2f619698
FB
2798 if (put_user_u32(len, optlen)
2799 || put_user_u32(val, optval_addr))
2800 return -TARGET_EFAULT;
2efbe911 2801 }
8853f86e 2802 break;
2efbe911 2803 default:
c02f499e
TS
2804 ret = -TARGET_ENOPROTOOPT;
2805 break;
8853f86e
FB
2806 }
2807 break;
bd8ed485
TD
2808 case SOL_IPV6:
2809 switch (optname) {
2810 case IPV6_MTU_DISCOVER:
2811 case IPV6_MTU:
2812 case IPV6_V6ONLY:
2813 case IPV6_RECVPKTINFO:
2814 case IPV6_UNICAST_HOPS:
2815 case IPV6_MULTICAST_HOPS:
2816 case IPV6_MULTICAST_LOOP:
2817 case IPV6_RECVERR:
2818 case IPV6_RECVHOPLIMIT:
2819 case IPV6_2292HOPLIMIT:
2820 case IPV6_CHECKSUM:
b9cce6d7
HD
2821 case IPV6_ADDRFORM:
2822 case IPV6_2292PKTINFO:
2823 case IPV6_RECVTCLASS:
2824 case IPV6_RECVRTHDR:
2825 case IPV6_2292RTHDR:
2826 case IPV6_RECVHOPOPTS:
2827 case IPV6_2292HOPOPTS:
2828 case IPV6_RECVDSTOPTS:
2829 case IPV6_2292DSTOPTS:
2830 case IPV6_TCLASS:
22db1213 2831 case IPV6_ADDR_PREFERENCES:
b9cce6d7
HD
2832#ifdef IPV6_RECVPATHMTU
2833 case IPV6_RECVPATHMTU:
2834#endif
2835#ifdef IPV6_TRANSPARENT
2836 case IPV6_TRANSPARENT:
2837#endif
2838#ifdef IPV6_FREEBIND
2839 case IPV6_FREEBIND:
2840#endif
2841#ifdef IPV6_RECVORIGDSTADDR
2842 case IPV6_RECVORIGDSTADDR:
2843#endif
bd8ed485
TD
2844 if (get_user_u32(len, optlen))
2845 return -TARGET_EFAULT;
2846 if (len < 0)
2847 return -TARGET_EINVAL;
2848 lv = sizeof(lv);
2849 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2850 if (ret < 0)
2851 return ret;
2852 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2853 len = 1;
2854 if (put_user_u32(len, optlen)
2855 || put_user_u8(val, optval_addr))
2856 return -TARGET_EFAULT;
2857 } else {
2858 if (len > sizeof(int))
2859 len = sizeof(int);
2860 if (put_user_u32(len, optlen)
2861 || put_user_u32(val, optval_addr))
2862 return -TARGET_EFAULT;
2863 }
2864 break;
2865 default:
2866 ret = -TARGET_ENOPROTOOPT;
2867 break;
2868 }
2869 break;
a2d86682
JK
2870#ifdef SOL_NETLINK
2871 case SOL_NETLINK:
2872 switch (optname) {
2873 case NETLINK_PKTINFO:
2874 case NETLINK_BROADCAST_ERROR:
2875 case NETLINK_NO_ENOBUFS:
2876#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2877 case NETLINK_LISTEN_ALL_NSID:
2878 case NETLINK_CAP_ACK:
2879#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2880#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2881 case NETLINK_EXT_ACK:
2882#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2883#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2884 case NETLINK_GET_STRICT_CHK:
2885#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2886 if (get_user_u32(len, optlen)) {
2887 return -TARGET_EFAULT;
2888 }
2889 if (len != sizeof(val)) {
2890 return -TARGET_EINVAL;
2891 }
2892 lv = len;
2893 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2894 if (ret < 0) {
2895 return ret;
2896 }
2897 if (put_user_u32(lv, optlen)
2898 || put_user_u32(val, optval_addr)) {
2899 return -TARGET_EFAULT;
2900 }
2901 break;
2902#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2903 case NETLINK_LIST_MEMBERSHIPS:
2904 {
2905 uint32_t *results;
2906 int i;
2907 if (get_user_u32(len, optlen)) {
2908 return -TARGET_EFAULT;
2909 }
2910 if (len < 0) {
2911 return -TARGET_EINVAL;
2912 }
2913 results = lock_user(VERIFY_WRITE, optval_addr, len, 1);
13e340c8 2914 if (!results && len > 0) {
a2d86682
JK
2915 return -TARGET_EFAULT;
2916 }
2917 lv = len;
2918 ret = get_errno(getsockopt(sockfd, level, optname, results, &lv));
2919 if (ret < 0) {
2920 unlock_user(results, optval_addr, 0);
2921 return ret;
2922 }
669dcb60 2923 /* swap host endianness to target endianness. */
a2d86682
JK
2924 for (i = 0; i < (len / sizeof(uint32_t)); i++) {
2925 results[i] = tswap32(results[i]);
2926 }
2927 if (put_user_u32(lv, optlen)) {
2928 return -TARGET_EFAULT;
2929 }
2930 unlock_user(results, optval_addr, 0);
2931 break;
2932 }
2933#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2934 default:
2935 goto unimplemented;
2936 }
c0cb8801 2937 break;
a2d86682 2938#endif /* SOL_NETLINK */
8853f86e
FB
2939 default:
2940 unimplemented:
39be5350
JK
2941 qemu_log_mask(LOG_UNIMP,
2942 "getsockopt level=%d optname=%d not yet supported\n",
2943 level, optname);
c02f499e 2944 ret = -TARGET_EOPNOTSUPP;
8853f86e
FB
2945 break;
2946 }
2947 return ret;
7854b056
FB
2948}
2949
9ac22517
MF
2950/* Convert target low/high pair representing file offset into the host
2951 * low/high pair. This function doesn't handle offsets bigger than 64 bits
2952 * as the kernel doesn't handle them either.
2953 */
2954static void target_to_host_low_high(abi_ulong tlow,
2955 abi_ulong thigh,
2956 unsigned long *hlow,
2957 unsigned long *hhigh)
2958{
2959 uint64_t off = tlow |
2960 ((unsigned long long)thigh << TARGET_LONG_BITS / 2) <<
2961 TARGET_LONG_BITS / 2;
2962
2963 *hlow = off;
2964 *hhigh = (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2;
2965}
2966
f287b2c2 2967static struct iovec *lock_iovec(int type, abi_ulong target_addr,
dab32b32 2968 abi_ulong count, int copy)
53a5960a
PB
2969{
2970 struct target_iovec *target_vec;
f287b2c2
RH
2971 struct iovec *vec;
2972 abi_ulong total_len, max_len;
d732dcb4 2973 int i;
501bb4b0 2974 int err = 0;
29560a6c 2975 bool bad_address = false;
53a5960a 2976
f287b2c2
RH
2977 if (count == 0) {
2978 errno = 0;
2979 return NULL;
2980 }
dab32b32 2981 if (count > IOV_MAX) {
f287b2c2
RH
2982 errno = EINVAL;
2983 return NULL;
2984 }
2985
0e173b24 2986 vec = g_try_new0(struct iovec, count);
f287b2c2
RH
2987 if (vec == NULL) {
2988 errno = ENOMEM;
2989 return NULL;
2990 }
2991
2992 target_vec = lock_user(VERIFY_READ, target_addr,
2993 count * sizeof(struct target_iovec), 1);
2994 if (target_vec == NULL) {
501bb4b0 2995 err = EFAULT;
f287b2c2
RH
2996 goto fail2;
2997 }
2998
2999 /* ??? If host page size > target page size, this will result in a
3000 value larger than what we can actually support. */
3001 max_len = 0x7fffffff & TARGET_PAGE_MASK;
3002 total_len = 0;
3003
3004 for (i = 0; i < count; i++) {
3005 abi_ulong base = tswapal(target_vec[i].iov_base);
3006 abi_long len = tswapal(target_vec[i].iov_len);
3007
3008 if (len < 0) {
501bb4b0 3009 err = EINVAL;
f287b2c2
RH
3010 goto fail;
3011 } else if (len == 0) {
3012 /* Zero length pointer is ignored. */
3013 vec[i].iov_base = 0;
41df8411 3014 } else {
f287b2c2 3015 vec[i].iov_base = lock_user(type, base, len, copy);
29560a6c
TM
3016 /* If the first buffer pointer is bad, this is a fault. But
3017 * subsequent bad buffers will result in a partial write; this
3018 * is realized by filling the vector with null pointers and
3019 * zero lengths. */
f287b2c2 3020 if (!vec[i].iov_base) {
29560a6c
TM
3021 if (i == 0) {
3022 err = EFAULT;
3023 goto fail;
3024 } else {
3025 bad_address = true;
3026 }
3027 }
3028 if (bad_address) {
3029 len = 0;
f287b2c2
RH
3030 }
3031 if (len > max_len - total_len) {
3032 len = max_len - total_len;
3033 }
41df8411 3034 }
f287b2c2
RH
3035 vec[i].iov_len = len;
3036 total_len += len;
579a97f7 3037 }
f287b2c2
RH
3038
3039 unlock_user(target_vec, target_addr, 0);
3040 return vec;
3041
3042 fail:
7eff518b
CG
3043 while (--i >= 0) {
3044 if (tswapal(target_vec[i].iov_len) > 0) {
3045 unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
3046 }
3047 }
f287b2c2 3048 unlock_user(target_vec, target_addr, 0);
501bb4b0 3049 fail2:
0e173b24 3050 g_free(vec);
501bb4b0 3051 errno = err;
f287b2c2 3052 return NULL;
53a5960a
PB
3053}
3054
f287b2c2 3055static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
dab32b32 3056 abi_ulong count, int copy)
53a5960a
PB
3057{
3058 struct target_iovec *target_vec;
53a5960a
PB
3059 int i;
3060
f287b2c2
RH
3061 target_vec = lock_user(VERIFY_READ, target_addr,
3062 count * sizeof(struct target_iovec), 1);
3063 if (target_vec) {
3064 for (i = 0; i < count; i++) {
3065 abi_ulong base = tswapal(target_vec[i].iov_base);
71ec7cef 3066 abi_long len = tswapal(target_vec[i].iov_len);
f287b2c2
RH
3067 if (len < 0) {
3068 break;
3069 }
d732dcb4
AZ
3070 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
3071 }
f287b2c2 3072 unlock_user(target_vec, target_addr, 0);
53a5960a 3073 }
579a97f7 3074
0e173b24 3075 g_free(vec);
53a5960a
PB
3076}
3077
53d09b76 3078static inline int target_to_host_sock_type(int *type)
3532fa74 3079{
f651e6ae
PJ
3080 int host_type = 0;
3081 int target_type = *type;
3082
3083 switch (target_type & TARGET_SOCK_TYPE_MASK) {
3532fa74 3084 case TARGET_SOCK_DGRAM:
f651e6ae 3085 host_type = SOCK_DGRAM;
3532fa74
FB
3086 break;
3087 case TARGET_SOCK_STREAM:
f651e6ae 3088 host_type = SOCK_STREAM;
3532fa74 3089 break;
f651e6ae
PJ
3090 default:
3091 host_type = target_type & TARGET_SOCK_TYPE_MASK;
3532fa74
FB
3092 break;
3093 }
f651e6ae 3094 if (target_type & TARGET_SOCK_CLOEXEC) {
53d09b76 3095#if defined(SOCK_CLOEXEC)
f651e6ae 3096 host_type |= SOCK_CLOEXEC;
53d09b76
EI
3097#else
3098 return -TARGET_EINVAL;
3099#endif
f651e6ae
PJ
3100 }
3101 if (target_type & TARGET_SOCK_NONBLOCK) {
53d09b76 3102#if defined(SOCK_NONBLOCK)
f651e6ae 3103 host_type |= SOCK_NONBLOCK;
53d09b76
EI
3104#elif !defined(O_NONBLOCK)
3105 return -TARGET_EINVAL;
3106#endif
f651e6ae
PJ
3107 }
3108 *type = host_type;
53d09b76
EI
3109 return 0;
3110}
3111
3112/* Try to emulate socket type flags after socket creation. */
3113static int sock_flags_fixup(int fd, int target_type)
3114{
3115#if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
3116 if (target_type & TARGET_SOCK_NONBLOCK) {
3117 int flags = fcntl(fd, F_GETFL);
3118 if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
3119 close(fd);
3120 return -TARGET_EINVAL;
3121 }
3122 }
3123#endif
3124 return fd;
f651e6ae
PJ
3125}
3126
3127/* do_socket() Must return target values and target errnos. */
3128static abi_long do_socket(int domain, int type, int protocol)
3129{
53d09b76
EI
3130 int target_type = type;
3131 int ret;
3132
3133 ret = target_to_host_sock_type(&type);
3134 if (ret) {
3135 return ret;
3136 }
f651e6ae 3137
575b22b1
LV
3138 if (domain == PF_NETLINK && !(
3139#ifdef CONFIG_RTNETLINK
3140 protocol == NETLINK_ROUTE ||
3141#endif
3142 protocol == NETLINK_KOBJECT_UEVENT ||
3143 protocol == NETLINK_AUDIT)) {
71e2443e 3144 return -TARGET_EPROTONOSUPPORT;
6c5b5645 3145 }
ff626f2d
LV
3146
3147 if (domain == AF_PACKET ||
3148 (domain == AF_INET && type == SOCK_PACKET)) {
3149 protocol = tswap16(protocol);
3150 }
3151
53d09b76
EI
3152 ret = get_errno(socket(domain, type, protocol));
3153 if (ret >= 0) {
3154 ret = sock_flags_fixup(ret, target_type);
0cf22722
LV
3155 if (type == SOCK_PACKET) {
3156 /* Manage an obsolete case :
3157 * if socket type is SOCK_PACKET, bind by name
3158 */
3159 fd_trans_register(ret, &target_packet_trans);
6c5b5645
LV
3160 } else if (domain == PF_NETLINK) {
3161 switch (protocol) {
575b22b1 3162#ifdef CONFIG_RTNETLINK
6c5b5645
LV
3163 case NETLINK_ROUTE:
3164 fd_trans_register(ret, &target_netlink_route_trans);
3165 break;
575b22b1 3166#endif
b265620b
LV
3167 case NETLINK_KOBJECT_UEVENT:
3168 /* nothing to do: messages are strings */
3169 break;
5ce9bb59
LV
3170 case NETLINK_AUDIT:
3171 fd_trans_register(ret, &target_netlink_audit_trans);
3172 break;
6c5b5645
LV
3173 default:
3174 g_assert_not_reached();
3175 }
0cf22722 3176 }
53d09b76
EI
3177 }
3178 return ret;
3532fa74
FB
3179}
3180
0da46a6e 3181/* do_bind() Must return target values and target errnos. */
992f48a0
BS
3182static abi_long do_bind(int sockfd, abi_ulong target_addr,
3183 socklen_t addrlen)
3532fa74 3184{
8f7aeaf6 3185 void *addr;
917507b0 3186 abi_long ret;
8f7aeaf6 3187
38724253 3188 if ((int)addrlen < 0) {
8f7aeaf6 3189 return -TARGET_EINVAL;
38724253 3190 }
8f7aeaf6 3191
607175e0 3192 addr = alloca(addrlen+1);
3b46e624 3193
7b36f782 3194 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
917507b0
AP
3195 if (ret)
3196 return ret;
3197
3532fa74
FB
3198 return get_errno(bind(sockfd, addr, addrlen));
3199}
3200
0da46a6e 3201/* do_connect() Must return target values and target errnos. */
992f48a0
BS
3202static abi_long do_connect(int sockfd, abi_ulong target_addr,
3203 socklen_t addrlen)
3532fa74 3204{
8f7aeaf6 3205 void *addr;
917507b0 3206 abi_long ret;
8f7aeaf6 3207
38724253 3208 if ((int)addrlen < 0) {
8f7aeaf6 3209 return -TARGET_EINVAL;
38724253 3210 }
8f7aeaf6 3211
2dd08dfd 3212 addr = alloca(addrlen+1);
3b46e624 3213
7b36f782 3214 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
917507b0
AP
3215 if (ret)
3216 return ret;
3217
2a3c7619 3218 return get_errno(safe_connect(sockfd, addr, addrlen));
3532fa74
FB
3219}
3220
f19e00d7
AG
3221/* do_sendrecvmsg_locked() Must return target values and target errnos. */
3222static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
3223 int flags, int send)
3532fa74 3224{
6de645c7 3225 abi_long ret, len;
3532fa74 3226 struct msghdr msg;
dab32b32 3227 abi_ulong count;
3532fa74 3228 struct iovec *vec;
992f48a0 3229 abi_ulong target_vec;
3532fa74 3230
3532fa74
FB
3231 if (msgp->msg_name) {
3232 msg.msg_namelen = tswap32(msgp->msg_namelen);
2dd08dfd 3233 msg.msg_name = alloca(msg.msg_namelen+1);
7b36f782
LV
3234 ret = target_to_host_sockaddr(fd, msg.msg_name,
3235 tswapal(msgp->msg_name),
3236 msg.msg_namelen);
26a6fc96
PM
3237 if (ret == -TARGET_EFAULT) {
3238 /* For connected sockets msg_name and msg_namelen must
3239 * be ignored, so returning EFAULT immediately is wrong.
3240 * Instead, pass a bad msg_name to the host kernel, and
3241 * let it decide whether to return EFAULT or not.
3242 */
3243 msg.msg_name = (void *)-1;
3244 } else if (ret) {
f287b2c2 3245 goto out2;
917507b0 3246 }
3532fa74
FB
3247 } else {
3248 msg.msg_name = NULL;
3249 msg.msg_namelen = 0;
3250 }
cbb21eed 3251 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
3532fa74 3252 msg.msg_control = alloca(msg.msg_controllen);
1d3d1b23
JS
3253 memset(msg.msg_control, 0, msg.msg_controllen);
3254
3532fa74 3255 msg.msg_flags = tswap32(msgp->msg_flags);
3b46e624 3256
cbb21eed 3257 count = tswapal(msgp->msg_iovlen);
cbb21eed 3258 target_vec = tswapal(msgp->msg_iov);
97b07970
PM
3259
3260 if (count > IOV_MAX) {
3261 /* sendrcvmsg returns a different errno for this condition than
3262 * readv/writev, so we must catch it here before lock_iovec() does.
3263 */
3264 ret = -TARGET_EMSGSIZE;
3265 goto out2;
3266 }
3267
f287b2c2
RH
3268 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
3269 target_vec, count, send);
3270 if (vec == NULL) {
3271 ret = -host_to_target_errno(errno);
3f0744f9
HD
3272 /* allow sending packet without any iov, e.g. with MSG_MORE flag */
3273 if (!send || ret) {
3274 goto out2;
3275 }
f287b2c2 3276 }
3532fa74
FB
3277 msg.msg_iovlen = count;
3278 msg.msg_iov = vec;
3b46e624 3279
3532fa74 3280 if (send) {
6c5b5645 3281 if (fd_trans_target_to_host_data(fd)) {
7d61d892
LV
3282 void *host_msg;
3283
3284 host_msg = g_malloc(msg.msg_iov->iov_len);
3285 memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len);
3286 ret = fd_trans_target_to_host_data(fd)(host_msg,
6c5b5645 3287 msg.msg_iov->iov_len);
7d61d892
LV
3288 if (ret >= 0) {
3289 msg.msg_iov->iov_base = host_msg;
3290 ret = get_errno(safe_sendmsg(fd, &msg, flags));
3291 }
3292 g_free(host_msg);
6c5b5645
LV
3293 } else {
3294 ret = target_to_host_cmsg(&msg, msgp);
7d61d892
LV
3295 if (ret == 0) {
3296 ret = get_errno(safe_sendmsg(fd, &msg, flags));
3297 }
6c5b5645 3298 }
3532fa74 3299 } else {
66687530 3300 ret = get_errno(safe_recvmsg(fd, &msg, flags));
6de645c7
AZ
3301 if (!is_error(ret)) {
3302 len = ret;
6c5b5645
LV
3303 if (fd_trans_host_to_target_data(fd)) {
3304 ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
2a03d3e6 3305 MIN(msg.msg_iov->iov_len, len));
16c81dd5
IZ
3306 }
3307 if (!is_error(ret)) {
6c5b5645
LV
3308 ret = host_to_target_cmsg(msgp, &msg);
3309 }
ca619067
JH
3310 if (!is_error(ret)) {
3311 msgp->msg_namelen = tswap32(msg.msg_namelen);
24894f39 3312 msgp->msg_flags = tswap32(msg.msg_flags);
26a6fc96 3313 if (msg.msg_name != NULL && msg.msg_name != (void *)-1) {
ca619067
JH
3314 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
3315 msg.msg_name, msg.msg_namelen);
3316 if (ret) {
3317 goto out;
3318 }
3319 }
3320
6de645c7 3321 ret = len;
ca619067 3322 }
6de645c7 3323 }
3532fa74 3324 }
ca619067
JH
3325
3326out:
3f0744f9
HD
3327 if (vec) {
3328 unlock_iovec(vec, target_vec, count, !send);
3329 }
f287b2c2 3330out2:
f19e00d7
AG
3331 return ret;
3332}
3333
3334static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
3335 int flags, int send)
3336{
3337 abi_long ret;
3338 struct target_msghdr *msgp;
3339
3340 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
3341 msgp,
3342 target_msg,
3343 send ? 1 : 0)) {
3344 return -TARGET_EFAULT;
3345 }
3346 ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
579a97f7 3347 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
3532fa74
FB
3348 return ret;
3349}
3350
f19e00d7
AG
3351/* We don't rely on the C library to have sendmmsg/recvmmsg support,
3352 * so it might not have this *mmsg-specific flag either.
3353 */
3354#ifndef MSG_WAITFORONE
3355#define MSG_WAITFORONE 0x10000
3356#endif
3357
3358static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
3359 unsigned int vlen, unsigned int flags,
3360 int send)
3361{
3362 struct target_mmsghdr *mmsgp;
3363 abi_long ret = 0;
3364 int i;
3365
3366 if (vlen > UIO_MAXIOV) {
3367 vlen = UIO_MAXIOV;
3368 }
3369
3370 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
3371 if (!mmsgp) {
3372 return -TARGET_EFAULT;
3373 }
3374
3375 for (i = 0; i < vlen; i++) {
3376 ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
3377 if (is_error(ret)) {
3378 break;
3379 }
3380 mmsgp[i].msg_len = tswap32(ret);
3381 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3382 if (flags & MSG_WAITFORONE) {
3383 flags |= MSG_DONTWAIT;
3384 }
3385 }
3386
3387 unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
3388
3389 /* Return number of datagrams sent if we sent any at all;
3390 * otherwise return the error.
3391 */
3392 if (i) {
3393 return i;
3394 }
3395 return ret;
3396}
f19e00d7 3397
a94b4987
PM
3398/* do_accept4() Must return target values and target errnos. */
3399static abi_long do_accept4(int fd, abi_ulong target_addr,
3400 abi_ulong target_addrlen_addr, int flags)
1be9e1dc 3401{
cd813367 3402 socklen_t addrlen, ret_addrlen;
2f619698 3403 void *addr;
992f48a0 3404 abi_long ret;
d25295d4
PJ
3405 int host_flags;
3406
dca4c838
HD
3407 if (flags & ~(TARGET_SOCK_CLOEXEC | TARGET_SOCK_NONBLOCK)) {
3408 return -TARGET_EINVAL;
3409 }
3410
3411 host_flags = 0;
3412 if (flags & TARGET_SOCK_NONBLOCK) {
3413 host_flags |= SOCK_NONBLOCK;
3414 }
3415 if (flags & TARGET_SOCK_CLOEXEC) {
3416 host_flags |= SOCK_CLOEXEC;
3417 }
1be9e1dc 3418
a94b4987 3419 if (target_addr == 0) {
ff6dc130 3420 return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
a94b4987 3421 }
917507b0 3422
e554eb4b 3423 /* linux returns EFAULT if addrlen pointer is invalid */
2f619698 3424 if (get_user_u32(addrlen, target_addrlen_addr))
e554eb4b 3425 return -TARGET_EFAULT;
2f619698 3426
38724253 3427 if ((int)addrlen < 0) {
8f7aeaf6 3428 return -TARGET_EINVAL;
38724253 3429 }
8f7aeaf6 3430
c7169b02 3431 if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
e554eb4b 3432 return -TARGET_EFAULT;
c7169b02 3433 }
917507b0 3434
2f619698
FB
3435 addr = alloca(addrlen);
3436
cd813367
AS
3437 ret_addrlen = addrlen;
3438 ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags));
1be9e1dc 3439 if (!is_error(ret)) {
cd813367
AS
3440 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
3441 if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
2f619698 3442 ret = -TARGET_EFAULT;
cd813367 3443 }
1be9e1dc
PB
3444 }
3445 return ret;
3446}
3447
0da46a6e 3448/* do_getpeername() Must return target values and target errnos. */
992f48a0 3449static abi_long do_getpeername(int fd, abi_ulong target_addr,
2f619698 3450 abi_ulong target_addrlen_addr)
1be9e1dc 3451{
cd813367 3452 socklen_t addrlen, ret_addrlen;
2f619698 3453 void *addr;
992f48a0 3454 abi_long ret;
1be9e1dc 3455
2f619698
FB
3456 if (get_user_u32(addrlen, target_addrlen_addr))
3457 return -TARGET_EFAULT;
3458
38724253 3459 if ((int)addrlen < 0) {
8f7aeaf6 3460 return -TARGET_EINVAL;
38724253 3461 }
8f7aeaf6 3462
c7169b02 3463 if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
917507b0 3464 return -TARGET_EFAULT;
c7169b02 3465 }
917507b0 3466
2f619698
FB
3467 addr = alloca(addrlen);
3468
cd813367
AS
3469 ret_addrlen = addrlen;
3470 ret = get_errno(getpeername(fd, addr, &ret_addrlen));
1be9e1dc 3471 if (!is_error(ret)) {
cd813367
AS
3472 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
3473 if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
2f619698 3474 ret = -TARGET_EFAULT;
cd813367 3475 }
1be9e1dc
PB
3476 }
3477 return ret;
3478}
3479
0da46a6e 3480/* do_getsockname() Must return target values and target errnos. */
992f48a0 3481static abi_long do_getsockname(int fd, abi_ulong target_addr,
2f619698 3482 abi_ulong target_addrlen_addr)
1be9e1dc 3483{
cd813367 3484 socklen_t addrlen, ret_addrlen;
2f619698 3485 void *addr;
992f48a0 3486 abi_long ret;
1be9e1dc 3487
2f619698
FB
3488 if (get_user_u32(addrlen, target_addrlen_addr))
3489 return -TARGET_EFAULT;
3490
38724253 3491 if ((int)addrlen < 0) {
8f7aeaf6 3492 return -TARGET_EINVAL;
38724253 3493 }
8f7aeaf6 3494
c7169b02 3495 if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
917507b0 3496 return -TARGET_EFAULT;
c7169b02 3497 }
917507b0 3498
2f619698
FB
3499 addr = alloca(addrlen);
3500
cd813367
AS
3501 ret_addrlen = addrlen;
3502 ret = get_errno(getsockname(fd, addr, &ret_addrlen));
1be9e1dc 3503 if (!is_error(ret)) {
cd813367
AS
3504 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
3505 if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
2f619698 3506 ret = -TARGET_EFAULT;
cd813367 3507 }
1be9e1dc
PB
3508 }
3509 return ret;
3510}
3511
0da46a6e 3512/* do_socketpair() Must return target values and target errnos. */
992f48a0 3513static abi_long do_socketpair(int domain, int type, int protocol,
2f619698 3514 abi_ulong target_tab_addr)
1be9e1dc
PB
3515{
3516 int tab[2];
992f48a0 3517 abi_long ret;
1be9e1dc 3518
f651e6ae
PJ
3519 target_to_host_sock_type(&type);
3520
1be9e1dc
PB
3521 ret = get_errno(socketpair(domain, type, protocol, tab));
3522 if (!is_error(ret)) {
2f619698
FB
3523 if (put_user_s32(tab[0], target_tab_addr)
3524 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
3525 ret = -TARGET_EFAULT;
1be9e1dc
PB
3526 }
3527 return ret;
3528}
3529
0da46a6e 3530/* do_sendto() Must return target values and target errnos. */
992f48a0
BS
3531static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
3532 abi_ulong target_addr, socklen_t addrlen)
1be9e1dc
PB
3533{
3534 void *addr;
3535 void *host_msg;
7d61d892 3536 void *copy_msg = NULL;
992f48a0 3537 abi_long ret;
1be9e1dc 3538
38724253 3539 if ((int)addrlen < 0) {
8f7aeaf6 3540 return -TARGET_EINVAL;
38724253 3541 }
8f7aeaf6 3542
579a97f7
FB
3543 host_msg = lock_user(VERIFY_READ, msg, len, 1);
3544 if (!host_msg)
3545 return -TARGET_EFAULT;
6c5b5645 3546 if (fd_trans_target_to_host_data(fd)) {
7d61d892
LV
3547 copy_msg = host_msg;
3548 host_msg = g_malloc(len);
3549 memcpy(host_msg, copy_msg, len);
6c5b5645
LV
3550 ret = fd_trans_target_to_host_data(fd)(host_msg, len);
3551 if (ret < 0) {
7d61d892 3552 goto fail;
6c5b5645
LV
3553 }
3554 }
1be9e1dc 3555 if (target_addr) {
2dd08dfd 3556 addr = alloca(addrlen+1);
7b36f782 3557 ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
917507b0 3558 if (ret) {
7d61d892 3559 goto fail;
917507b0 3560 }
66687530 3561 ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
1be9e1dc 3562 } else {
66687530 3563 ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
1be9e1dc 3564 }
7d61d892
LV
3565fail:
3566 if (copy_msg) {
3567 g_free(host_msg);
3568 host_msg = copy_msg;
3569 }
1be9e1dc
PB
3570 unlock_user(host_msg, msg, 0);
3571 return ret;
3572}
3573
0da46a6e 3574/* do_recvfrom() Must return target values and target errnos. */
992f48a0
BS
3575static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
3576 abi_ulong target_addr,
3577 abi_ulong target_addrlen)
1be9e1dc 3578{
cd813367 3579 socklen_t addrlen, ret_addrlen;
1be9e1dc
PB
3580 void *addr;
3581 void *host_msg;
992f48a0 3582 abi_long ret;
1be9e1dc 3583
4a1e6bce
ZR
3584 if (!msg) {
3585 host_msg = NULL;
3586 } else {
3587 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
3588 if (!host_msg) {
3589 return -TARGET_EFAULT;
3590 }
3591 }
1be9e1dc 3592 if (target_addr) {
2f619698
FB
3593 if (get_user_u32(addrlen, target_addrlen)) {
3594 ret = -TARGET_EFAULT;
3595 goto fail;
3596 }
38724253 3597 if ((int)addrlen < 0) {
8f7aeaf6
AJ
3598 ret = -TARGET_EINVAL;
3599 goto fail;
3600 }
1be9e1dc 3601 addr = alloca(addrlen);
cd813367 3602 ret_addrlen = addrlen;
66687530 3603 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
cd813367 3604 addr, &ret_addrlen));
1be9e1dc
PB
3605 } else {
3606 addr = NULL; /* To keep compiler quiet. */
cd813367 3607 addrlen = 0; /* To keep compiler quiet. */
66687530 3608 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
1be9e1dc
PB
3609 }
3610 if (!is_error(ret)) {
c35e1f9c 3611 if (fd_trans_host_to_target_data(fd)) {
2a03d3e6
LV
3612 abi_long trans;
3613 trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len));
3614 if (is_error(trans)) {
3615 ret = trans;
3616 goto fail;
3617 }
c35e1f9c 3618 }
1be9e1dc 3619 if (target_addr) {
cd813367
AS
3620 host_to_target_sockaddr(target_addr, addr,
3621 MIN(addrlen, ret_addrlen));
3622 if (put_user_u32(ret_addrlen, target_addrlen)) {
2f619698
FB
3623 ret = -TARGET_EFAULT;
3624 goto fail;
3625 }
1be9e1dc
PB
3626 }
3627 unlock_user(host_msg, msg, len);
3628 } else {
2f619698 3629fail:
1be9e1dc
PB
3630 unlock_user(host_msg, msg, 0);
3631 }
3632 return ret;
3633}
3634
32407103 3635#ifdef TARGET_NR_socketcall
ff71a454 3636/* do_socketcall() must return target values and target errnos. */
992f48a0 3637static abi_long do_socketcall(int num, abi_ulong vptr)
31e31b8a 3638{
ff71a454
AM
3639 static const unsigned nargs[] = { /* number of arguments per operation */
3640 [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */
3641 [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */
3642 [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */
3643 [TARGET_SYS_LISTEN] = 2, /* fd, backlog */
3644 [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */
3645 [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */
3646 [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */
3647 [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */
3648 [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */
3649 [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */
3650 [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */
3651 [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */
3652 [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */
3653 [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
3654 [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
3655 [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */
3656 [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */
3657 [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */
3658 [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */
3659 [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */
62dc90c6
MT
3660 };
3661 abi_long a[6]; /* max 6 args */
ff71a454 3662 unsigned i;
62dc90c6 3663
ff71a454
AM
3664 /* check the range of the first argument num */
3665 /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
3666 if (num < 1 || num > TARGET_SYS_SENDMMSG) {
3667 return -TARGET_EINVAL;
3668 }
3669 /* ensure we have space for args */
3670 if (nargs[num] > ARRAY_SIZE(a)) {
3671 return -TARGET_EINVAL;
3672 }
3673 /* collect the arguments in a[] according to nargs[] */
3674 for (i = 0; i < nargs[num]; ++i) {
3675 if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
3676 return -TARGET_EFAULT;
31e31b8a 3677 }
62dc90c6 3678 }
ff71a454 3679 /* now when we have the args, invoke the appropriate underlying function */
62dc90c6 3680 switch (num) {
ff71a454 3681 case TARGET_SYS_SOCKET: /* domain, type, protocol */
62dc90c6 3682 return do_socket(a[0], a[1], a[2]);
ff71a454 3683 case TARGET_SYS_BIND: /* sockfd, addr, addrlen */
62dc90c6 3684 return do_bind(a[0], a[1], a[2]);
ff71a454 3685 case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */
62dc90c6 3686 return do_connect(a[0], a[1], a[2]);
ff71a454 3687 case TARGET_SYS_LISTEN: /* sockfd, backlog */
62dc90c6 3688 return get_errno(listen(a[0], a[1]));
ff71a454 3689 case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */
62dc90c6 3690 return do_accept4(a[0], a[1], a[2], 0);
ff71a454 3691 case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */
62dc90c6 3692 return do_getsockname(a[0], a[1], a[2]);
ff71a454 3693 case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */
62dc90c6 3694 return do_getpeername(a[0], a[1], a[2]);
ff71a454 3695 case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */
62dc90c6 3696 return do_socketpair(a[0], a[1], a[2], a[3]);
ff71a454 3697 case TARGET_SYS_SEND: /* sockfd, msg, len, flags */
62dc90c6 3698 return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
ff71a454 3699 case TARGET_SYS_RECV: /* sockfd, msg, len, flags */
62dc90c6 3700 return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
ff71a454 3701 case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */
62dc90c6 3702 return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
ff71a454 3703 case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */
62dc90c6 3704 return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
ff71a454 3705 case TARGET_SYS_SHUTDOWN: /* sockfd, how */
62dc90c6 3706 return get_errno(shutdown(a[0], a[1]));
ff71a454
AM
3707 case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */
3708 return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
3709 case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */
3710 return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
3711 case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */
62dc90c6 3712 return do_sendrecvmsg(a[0], a[1], a[2], 1);
ff71a454 3713 case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */
62dc90c6 3714 return do_sendrecvmsg(a[0], a[1], a[2], 0);
ff71a454
AM
3715 case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */
3716 return do_accept4(a[0], a[1], a[2], a[3]);
3717 case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */
5a53dc50 3718 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
ff71a454
AM
3719 case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */
3720 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
31e31b8a 3721 default:
39be5350 3722 qemu_log_mask(LOG_UNIMP, "Unsupported socketcall: %d\n", num);
ff71a454 3723 return -TARGET_EINVAL;
31e31b8a 3724 }
31e31b8a 3725}
32407103 3726#endif
31e31b8a 3727
005eb2ae
PM
3728#ifndef TARGET_SEMID64_DS
3729/* asm-generic version of this struct */
3730struct target_semid64_ds
3eb6b044
TS
3731{
3732 struct target_ipc_perm sem_perm;
992f48a0 3733 abi_ulong sem_otime;
005eb2ae 3734#if TARGET_ABI_BITS == 32
992f48a0 3735 abi_ulong __unused1;
03527344 3736#endif
992f48a0 3737 abi_ulong sem_ctime;
005eb2ae 3738#if TARGET_ABI_BITS == 32
992f48a0 3739 abi_ulong __unused2;
03527344 3740#endif
992f48a0
BS
3741 abi_ulong sem_nsems;
3742 abi_ulong __unused3;
3743 abi_ulong __unused4;
3eb6b044 3744};
005eb2ae 3745#endif
3eb6b044 3746
579a97f7
FB
3747static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
3748 abi_ulong target_addr)
3eb6b044
TS
3749{
3750 struct target_ipc_perm *target_ip;
005eb2ae 3751 struct target_semid64_ds *target_sd;
3eb6b044 3752
579a97f7
FB
3753 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3754 return -TARGET_EFAULT;
e8bbe36c 3755 target_ip = &(target_sd->sem_perm);
55a2b163
PJ
3756 host_ip->__key = tswap32(target_ip->__key);
3757 host_ip->uid = tswap32(target_ip->uid);
3758 host_ip->gid = tswap32(target_ip->gid);
3759 host_ip->cuid = tswap32(target_ip->cuid);
3760 host_ip->cgid = tswap32(target_ip->cgid);
3761#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3762 host_ip->mode = tswap32(target_ip->mode);
3763#else
cbb21eed 3764 host_ip->mode = tswap16(target_ip->mode);
55a2b163
PJ
3765#endif
3766#if defined(TARGET_PPC)
3767 host_ip->__seq = tswap32(target_ip->__seq);
3768#else
3769 host_ip->__seq = tswap16(target_ip->__seq);
3770#endif
3eb6b044 3771 unlock_user_struct(target_sd, target_addr, 0);
579a97f7 3772 return 0;
3eb6b044
TS
3773}
3774
579a97f7
FB
3775static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
3776 struct ipc_perm *host_ip)
3eb6b044
TS
3777{
3778 struct target_ipc_perm *target_ip;
005eb2ae 3779 struct target_semid64_ds *target_sd;
3eb6b044 3780
579a97f7
FB
3781 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3782 return -TARGET_EFAULT;
3eb6b044 3783 target_ip = &(target_sd->sem_perm);
55a2b163
PJ
3784 target_ip->__key = tswap32(host_ip->__key);
3785 target_ip->uid = tswap32(host_ip->uid);
3786 target_ip->gid = tswap32(host_ip->gid);
3787 target_ip->cuid = tswap32(host_ip->cuid);
3788 target_ip->cgid = tswap32(host_ip->cgid);
3789#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3790 target_ip->mode = tswap32(host_ip->mode);
3791#else
cbb21eed 3792 target_ip->mode = tswap16(host_ip->mode);
55a2b163
PJ
3793#endif
3794#if defined(TARGET_PPC)
3795 target_ip->__seq = tswap32(host_ip->__seq);
3796#else
3797 target_ip->__seq = tswap16(host_ip->__seq);
3798#endif
3eb6b044 3799 unlock_user_struct(target_sd, target_addr, 1);
579a97f7 3800 return 0;
3eb6b044
TS
3801}
3802
579a97f7
FB
3803static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
3804 abi_ulong target_addr)
3eb6b044 3805{
005eb2ae 3806 struct target_semid64_ds *target_sd;
3eb6b044 3807
579a97f7
FB
3808 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3809 return -TARGET_EFAULT;
e5289087
AJ
3810 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
3811 return -TARGET_EFAULT;
cbb21eed
MB
3812 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
3813 host_sd->sem_otime = tswapal(target_sd->sem_otime);
3814 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
3eb6b044 3815 unlock_user_struct(target_sd, target_addr, 0);
579a97f7 3816 return 0;
3eb6b044
TS
3817}
3818
579a97f7
FB
3819static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
3820 struct semid_ds *host_sd)
3eb6b044 3821{
005eb2ae 3822 struct target_semid64_ds *target_sd;
3eb6b044 3823
579a97f7
FB
3824 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3825 return -TARGET_EFAULT;
e5289087 3826 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
3a93113a 3827 return -TARGET_EFAULT;
cbb21eed
MB
3828 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
3829 target_sd->sem_otime = tswapal(host_sd->sem_otime);
3830 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
3eb6b044 3831 unlock_user_struct(target_sd, target_addr, 1);
579a97f7 3832 return 0;
3eb6b044
TS
3833}
3834
e5289087
AJ
3835struct target_seminfo {
3836 int semmap;
3837 int semmni;
3838 int semmns;
3839 int semmnu;
3840 int semmsl;
3841 int semopm;
3842 int semume;
3843 int semusz;
3844 int semvmx;
3845 int semaem;
3846};
3847
3848static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
3849 struct seminfo *host_seminfo)
3850{
3851 struct target_seminfo *target_seminfo;
3852 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
3853 return -TARGET_EFAULT;
3854 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
3855 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
3856 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
3857 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
3858 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
3859 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
3860 __put_user(host_seminfo->semume, &target_seminfo->semume);
3861 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
3862 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
3863 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
3864 unlock_user_struct(target_seminfo, target_addr, 1);
3865 return 0;
3866}
3867
fa294816
TS
3868union semun {
3869 int val;
3eb6b044 3870 struct semid_ds *buf;
fa294816 3871 unsigned short *array;
e5289087 3872 struct seminfo *__buf;
fa294816
TS
3873};
3874
3eb6b044
TS
3875union target_semun {
3876 int val;
e5289087
AJ
3877 abi_ulong buf;
3878 abi_ulong array;
3879 abi_ulong __buf;
3eb6b044
TS
3880};
3881
e5289087
AJ
3882static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
3883 abi_ulong target_addr)
3eb6b044 3884{
e5289087
AJ
3885 int nsems;
3886 unsigned short *array;
3887 union semun semun;
3888 struct semid_ds semid_ds;
3889 int i, ret;
3eb6b044 3890
e5289087
AJ
3891 semun.buf = &semid_ds;
3892
3893 ret = semctl(semid, 0, IPC_STAT, semun);
3894 if (ret == -1)
3895 return get_errno(ret);
3896
3897 nsems = semid_ds.sem_nsems;
3898
0e173b24 3899 *host_array = g_try_new(unsigned short, nsems);
69d4c703
PM
3900 if (!*host_array) {
3901 return -TARGET_ENOMEM;
3902 }
e5289087
AJ
3903 array = lock_user(VERIFY_READ, target_addr,
3904 nsems*sizeof(unsigned short), 1);
69d4c703 3905 if (!array) {
0e173b24 3906 g_free(*host_array);
e5289087 3907 return -TARGET_EFAULT;
69d4c703 3908 }
e5289087
AJ
3909
3910 for(i=0; i<nsems; i++) {
3911 __get_user((*host_array)[i], &array[i]);
3eb6b044 3912 }
e5289087
AJ
3913 unlock_user(array, target_addr, 0);
3914
579a97f7 3915 return 0;
3eb6b044
TS
3916}
3917
e5289087
AJ
3918static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
3919 unsigned short **host_array)
3eb6b044 3920{
e5289087
AJ
3921 int nsems;
3922 unsigned short *array;
3923 union semun semun;
3924 struct semid_ds semid_ds;
3925 int i, ret;
3eb6b044 3926
e5289087
AJ
3927 semun.buf = &semid_ds;
3928
3929 ret = semctl(semid, 0, IPC_STAT, semun);
3930 if (ret == -1)
3931 return get_errno(ret);
3932
3933 nsems = semid_ds.sem_nsems;
3934
3935 array = lock_user(VERIFY_WRITE, target_addr,
3936 nsems*sizeof(unsigned short), 0);
3937 if (!array)
3938 return -TARGET_EFAULT;
3939
3940 for(i=0; i<nsems; i++) {
3941 __put_user((*host_array)[i], &array[i]);
3eb6b044 3942 }
0e173b24 3943 g_free(*host_array);
e5289087
AJ
3944 unlock_user(array, target_addr, 1);
3945
579a97f7 3946 return 0;
3eb6b044
TS
3947}
3948
e5289087 3949static inline abi_long do_semctl(int semid, int semnum, int cmd,
d1c002b6 3950 abi_ulong target_arg)
3eb6b044 3951{
d1c002b6 3952 union target_semun target_su = { .buf = target_arg };
3eb6b044
TS
3953 union semun arg;
3954 struct semid_ds dsarg;
7b8118e8 3955 unsigned short *array = NULL;
e5289087
AJ
3956 struct seminfo seminfo;
3957 abi_long ret = -TARGET_EINVAL;
3958 abi_long err;
3959 cmd &= 0xff;
3eb6b044
TS
3960
3961 switch( cmd ) {
3962 case GETVAL:
3eb6b044 3963 case SETVAL:
5464baec
TM
3964 /* In 64 bit cross-endian situations, we will erroneously pick up
3965 * the wrong half of the union for the "val" element. To rectify
3966 * this, the entire 8-byte structure is byteswapped, followed by
3967 * a swap of the 4 byte val field. In other cases, the data is
3968 * already in proper host byte order. */
3969 if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
3970 target_su.buf = tswapal(target_su.buf);
3971 arg.val = tswap32(target_su.val);
3972 } else {
3973 arg.val = target_su.val;
3974 }
e5289087 3975 ret = get_errno(semctl(semid, semnum, cmd, arg));
3eb6b044
TS
3976 break;
3977 case GETALL:
3eb6b044 3978 case SETALL:
e5289087
AJ
3979 err = target_to_host_semarray(semid, &array, target_su.array);
3980 if (err)
3981 return err;
3982 arg.array = array;
3983 ret = get_errno(semctl(semid, semnum, cmd, arg));
3984 err = host_to_target_semarray(semid, target_su.array, &array);
3985 if (err)
3986 return err;
3eb6b044
TS
3987 break;
3988 case IPC_STAT:
3eb6b044 3989 case IPC_SET:
e5289087
AJ
3990 case SEM_STAT:
3991 err = target_to_host_semid_ds(&dsarg, target_su.buf);
3992 if (err)
3993 return err;
3994 arg.buf = &dsarg;
3995 ret = get_errno(semctl(semid, semnum, cmd, arg));
3996 err = host_to_target_semid_ds(target_su.buf, &dsarg);
3997 if (err)
3998 return err;
3999 break;
4000 case IPC_INFO:
4001 case SEM_INFO:
4002 arg.__buf = &seminfo;
4003 ret = get_errno(semctl(semid, semnum, cmd, arg));
4004 err = host_to_target_seminfo(target_su.__buf, &seminfo);
4005 if (err)
4006 return err;
4007 break;
4008 case IPC_RMID:
4009 case GETPID:
4010 case GETNCNT:
4011 case GETZCNT:
4012 ret = get_errno(semctl(semid, semnum, cmd, NULL));
3eb6b044 4013 break;
3eb6b044
TS
4014 }
4015
4016 return ret;
4017}
4018
e5289087
AJ
4019struct target_sembuf {
4020 unsigned short sem_num;
4021 short sem_op;
4022 short sem_flg;
4023};
4024
4025static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
4026 abi_ulong target_addr,
4027 unsigned nsops)
4028{
4029 struct target_sembuf *target_sembuf;
4030 int i;
4031
4032 target_sembuf = lock_user(VERIFY_READ, target_addr,
4033 nsops*sizeof(struct target_sembuf), 1);
4034 if (!target_sembuf)
4035 return -TARGET_EFAULT;
4036
4037 for(i=0; i<nsops; i++) {
4038 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
4039 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
4040 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
4041 }
4042
4043 unlock_user(target_sembuf, target_addr, 0);
4044
4045 return 0;
4046}
4047
d8c08b1e 4048#if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \
cac46eb0 4049 defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64)
d8c08b1e
MK
4050
4051/*
4052 * This macro is required to handle the s390 variants, which passes the
4053 * arguments in a different order than default.
4054 */
4055#ifdef __s390x__
4056#define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
4057 (__nsops), (__timeout), (__sops)
4058#else
4059#define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
4060 (__nsops), 0, (__sops), (__timeout)
4061#endif
4062
4063static inline abi_long do_semtimedop(int semid,
4064 abi_long ptr,
4065 unsigned nsops,
cac46eb0 4066 abi_long timeout, bool time64)
e5289087 4067{
0a7ec849 4068 struct sembuf *sops;
d8c08b1e 4069 struct timespec ts, *pts = NULL;
524fa340 4070 abi_long ret;
e5289087 4071
d8c08b1e
MK
4072 if (timeout) {
4073 pts = &ts;
cac46eb0
FB
4074 if (time64) {
4075 if (target_to_host_timespec64(pts, timeout)) {
4076 return -TARGET_EFAULT;
4077 }
4078 } else {
4079 if (target_to_host_timespec(pts, timeout)) {
4080 return -TARGET_EFAULT;
4081 }
d8c08b1e
MK
4082 }
4083 }
4084
0a7ec849
FB
4085 if (nsops > TARGET_SEMOPM) {
4086 return -TARGET_E2BIG;
4087 }
4088
4089 sops = g_new(struct sembuf, nsops);
4090
4091 if (target_to_host_sembuf(sops, ptr, nsops)) {
4092 g_free(sops);
e5289087 4093 return -TARGET_EFAULT;
0a7ec849 4094 }
e5289087 4095
524fa340
LV
4096 ret = -TARGET_ENOSYS;
4097#ifdef __NR_semtimedop
d8c08b1e 4098 ret = get_errno(safe_semtimedop(semid, sops, nsops, pts));
524fa340
LV
4099#endif
4100#ifdef __NR_ipc
4101 if (ret == -TARGET_ENOSYS) {
d8c08b1e
MK
4102 ret = get_errno(safe_ipc(IPCOP_semtimedop, semid,
4103 SEMTIMEDOP_IPC_ARGS(nsops, sops, (long)pts)));
524fa340
LV
4104 }
4105#endif
0a7ec849 4106 g_free(sops);
524fa340 4107 return ret;
e5289087 4108}
d8c08b1e 4109#endif
e5289087 4110
1bc012f6
TS
4111struct target_msqid_ds
4112{
1c54ff97
AJ
4113 struct target_ipc_perm msg_perm;
4114 abi_ulong msg_stime;
4115#if TARGET_ABI_BITS == 32
4116 abi_ulong __unused1;
4117#endif
4118 abi_ulong msg_rtime;
4119#if TARGET_ABI_BITS == 32
4120 abi_ulong __unused2;
4121#endif
4122 abi_ulong msg_ctime;
4123#if TARGET_ABI_BITS == 32
4124 abi_ulong __unused3;
4125#endif
4126 abi_ulong __msg_cbytes;
4127 abi_ulong msg_qnum;
4128 abi_ulong msg_qbytes;
4129 abi_ulong msg_lspid;
4130 abi_ulong msg_lrpid;
4131 abi_ulong __unused4;
4132 abi_ulong __unused5;
1bc012f6
TS
4133};
4134
579a97f7
FB
4135static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
4136 abi_ulong target_addr)
1bc012f6
TS
4137{
4138 struct target_msqid_ds *target_md;
4139
579a97f7
FB
4140 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
4141 return -TARGET_EFAULT;
1c54ff97
AJ
4142 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
4143 return -TARGET_EFAULT;
cbb21eed
MB
4144 host_md->msg_stime = tswapal(target_md->msg_stime);
4145 host_md->msg_rtime = tswapal(target_md->msg_rtime);
4146 host_md->msg_ctime = tswapal(target_md->msg_ctime);
4147 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
4148 host_md->msg_qnum = tswapal(target_md->msg_qnum);
4149 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
4150 host_md->msg_lspid = tswapal(target_md->msg_lspid);
4151 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
1bc012f6 4152 unlock_user_struct(target_md, target_addr, 0);
579a97f7 4153 return 0;
1bc012f6
TS
4154}
4155
579a97f7
FB
4156static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
4157 struct msqid_ds *host_md)
1bc012f6
TS
4158{
4159 struct target_msqid_ds *target_md;
4160
579a97f7
FB
4161 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
4162 return -TARGET_EFAULT;
1c54ff97
AJ
4163 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
4164 return -TARGET_EFAULT;
cbb21eed
MB
4165 target_md->msg_stime = tswapal(host_md->msg_stime);
4166 target_md->msg_rtime = tswapal(host_md->msg_rtime);
4167 target_md->msg_ctime = tswapal(host_md->msg_ctime);
4168 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
4169 target_md->msg_qnum = tswapal(host_md->msg_qnum);
4170 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
4171 target_md->msg_lspid = tswapal(host_md->msg_lspid);
4172 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
1bc012f6 4173 unlock_user_struct(target_md, target_addr, 1);
579a97f7 4174 return 0;
1bc012f6
TS
4175}
4176
1c54ff97
AJ
4177struct target_msginfo {
4178 int msgpool;
4179 int msgmap;
4180 int msgmax;
4181 int msgmnb;
4182 int msgmni;
4183 int msgssz;
4184 int msgtql;
4185 unsigned short int msgseg;
4186};
4187
4188static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
4189 struct msginfo *host_msginfo)
4190{
4191 struct target_msginfo *target_msginfo;
4192 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
4193 return -TARGET_EFAULT;
4194 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
4195 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
4196 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
4197 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
4198 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
4199 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
4200 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
4201 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
4202 unlock_user_struct(target_msginfo, target_addr, 1);
00b229ac 4203 return 0;
1c54ff97
AJ
4204}
4205
4206static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
1bc012f6
TS
4207{
4208 struct msqid_ds dsarg;
1c54ff97
AJ
4209 struct msginfo msginfo;
4210 abi_long ret = -TARGET_EINVAL;
4211
4212 cmd &= 0xff;
4213
4214 switch (cmd) {
1bc012f6
TS
4215 case IPC_STAT:
4216 case IPC_SET:
1c54ff97
AJ
4217 case MSG_STAT:
4218 if (target_to_host_msqid_ds(&dsarg,ptr))
4219 return -TARGET_EFAULT;
4220 ret = get_errno(msgctl(msgid, cmd, &dsarg));
4221 if (host_to_target_msqid_ds(ptr,&dsarg))
4222 return -TARGET_EFAULT;
4223 break;
4224 case IPC_RMID:
4225 ret = get_errno(msgctl(msgid, cmd, NULL));
4226 break;
4227 case IPC_INFO:
4228 case MSG_INFO:
4229 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
4230 if (host_to_target_msginfo(ptr, &msginfo))
4231 return -TARGET_EFAULT;
4232 break;
1bc012f6 4233 }
1c54ff97 4234
1bc012f6
TS
4235 return ret;
4236}
4237
4238struct target_msgbuf {
1c54ff97
AJ
4239 abi_long mtype;
4240 char mtext[1];
1bc012f6
TS
4241};
4242
992f48a0 4243static inline abi_long do_msgsnd(int msqid, abi_long msgp,
edcc5f9d 4244 ssize_t msgsz, int msgflg)
1bc012f6
TS
4245{
4246 struct target_msgbuf *target_mb;
4247 struct msgbuf *host_mb;
992f48a0 4248 abi_long ret = 0;
1bc012f6 4249
edcc5f9d
TM
4250 if (msgsz < 0) {
4251 return -TARGET_EINVAL;
4252 }
4253
579a97f7
FB
4254 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
4255 return -TARGET_EFAULT;
0e173b24 4256 host_mb = g_try_malloc(msgsz + sizeof(long));
29e03fcb
HZ
4257 if (!host_mb) {
4258 unlock_user_struct(target_mb, msgp, 0);
4259 return -TARGET_ENOMEM;
4260 }
cbb21eed 4261 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
1c54ff97 4262 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
524fa340
LV
4263 ret = -TARGET_ENOSYS;
4264#ifdef __NR_msgsnd
89f9fe44 4265 ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
524fa340
LV
4266#endif
4267#ifdef __NR_ipc
4268 if (ret == -TARGET_ENOSYS) {
d8c08b1e
MK
4269#ifdef __s390x__
4270 ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
4271 host_mb));
4272#else
524fa340
LV
4273 ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
4274 host_mb, 0));
d8c08b1e 4275#endif
524fa340
LV
4276 }
4277#endif
0e173b24 4278 g_free(host_mb);
1bc012f6
TS
4279 unlock_user_struct(target_mb, msgp, 0);
4280
4281 return ret;
4282}
4283
d8c08b1e
MK
4284#ifdef __NR_ipc
4285#if defined(__sparc__)
4286/* SPARC for msgrcv it does not use the kludge on final 2 arguments. */
4287#define MSGRCV_ARGS(__msgp, __msgtyp) __msgp, __msgtyp
4288#elif defined(__s390x__)
4289/* The s390 sys_ipc variant has only five parameters. */
4290#define MSGRCV_ARGS(__msgp, __msgtyp) \
4291 ((long int[]){(long int)__msgp, __msgtyp})
4292#else
4293#define MSGRCV_ARGS(__msgp, __msgtyp) \
4294 ((long int[]){(long int)__msgp, __msgtyp}), 0
4295#endif
4296#endif
4297
992f48a0 4298static inline abi_long do_msgrcv(int msqid, abi_long msgp,
99874f65 4299 ssize_t msgsz, abi_long msgtyp,
992f48a0 4300 int msgflg)
1bc012f6
TS
4301{
4302 struct target_msgbuf *target_mb;
579a97f7 4303 char *target_mtext;
1bc012f6 4304 struct msgbuf *host_mb;
992f48a0 4305 abi_long ret = 0;
1bc012f6 4306
99874f65
PM
4307 if (msgsz < 0) {
4308 return -TARGET_EINVAL;
4309 }
4310
579a97f7
FB
4311 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
4312 return -TARGET_EFAULT;
1c54ff97 4313
415d8471
PM
4314 host_mb = g_try_malloc(msgsz + sizeof(long));
4315 if (!host_mb) {
4316 ret = -TARGET_ENOMEM;
4317 goto end;
4318 }
524fa340
LV
4319 ret = -TARGET_ENOSYS;
4320#ifdef __NR_msgrcv
89f9fe44 4321 ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
524fa340
LV
4322#endif
4323#ifdef __NR_ipc
4324 if (ret == -TARGET_ENOSYS) {
4325 ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz,
d8c08b1e 4326 msgflg, MSGRCV_ARGS(host_mb, msgtyp)));
524fa340
LV
4327 }
4328#endif
1c54ff97 4329
579a97f7
FB
4330 if (ret > 0) {
4331 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
4332 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
4333 if (!target_mtext) {
4334 ret = -TARGET_EFAULT;
4335 goto end;
4336 }
1c54ff97 4337 memcpy(target_mb->mtext, host_mb->mtext, ret);
579a97f7
FB
4338 unlock_user(target_mtext, target_mtext_addr, ret);
4339 }
1c54ff97 4340
cbb21eed 4341 target_mb->mtype = tswapal(host_mb->mtype);
1bc012f6 4342
579a97f7
FB
4343end:
4344 if (target_mb)
4345 unlock_user_struct(target_mb, msgp, 1);
0d07fe47 4346 g_free(host_mb);
1bc012f6
TS
4347 return ret;
4348}
4349
88a8c984
RV
4350static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
4351 abi_ulong target_addr)
4352{
4353 struct target_shmid_ds *target_sd;
4354
4355 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
4356 return -TARGET_EFAULT;
4357 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
4358 return -TARGET_EFAULT;
4359 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
4360 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
4361 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
4362 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
4363 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
4364 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
4365 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
4366 unlock_user_struct(target_sd, target_addr, 0);
4367 return 0;
4368}
4369
4370static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
4371 struct shmid_ds *host_sd)
4372{
4373 struct target_shmid_ds *target_sd;
4374
4375 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
4376 return -TARGET_EFAULT;
4377 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
4378 return -TARGET_EFAULT;
4379 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
4380 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
4381 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
4382 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
4383 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
4384 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
4385 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
4386 unlock_user_struct(target_sd, target_addr, 1);
4387 return 0;
4388}
4389
4390struct target_shminfo {
4391 abi_ulong shmmax;
4392 abi_ulong shmmin;
4393 abi_ulong shmmni;
4394 abi_ulong shmseg;
4395 abi_ulong shmall;
4396};
4397
4398static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
4399 struct shminfo *host_shminfo)
4400{
4401 struct target_shminfo *target_shminfo;
4402 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
4403 return -TARGET_EFAULT;
4404 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
4405 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
4406 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
4407 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
4408 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
4409 unlock_user_struct(target_shminfo, target_addr, 1);
4410 return 0;
4411}
4412
4413struct target_shm_info {
4414 int used_ids;
4415 abi_ulong shm_tot;
4416 abi_ulong shm_rss;
4417 abi_ulong shm_swp;
4418 abi_ulong swap_attempts;
4419 abi_ulong swap_successes;
4420};
4421
4422static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
4423 struct shm_info *host_shm_info)
4424{
4425 struct target_shm_info *target_shm_info;
4426 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
4427 return -TARGET_EFAULT;
4428 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
4429 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
4430 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
4431 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
4432 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
4433 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
4434 unlock_user_struct(target_shm_info, target_addr, 1);
4435 return 0;
4436}
4437
4438static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
4439{
4440 struct shmid_ds dsarg;
4441 struct shminfo shminfo;
4442 struct shm_info shm_info;
4443 abi_long ret = -TARGET_EINVAL;
4444
4445 cmd &= 0xff;
4446
4447 switch(cmd) {
4448 case IPC_STAT:
4449 case IPC_SET:
4450 case SHM_STAT:
4451 if (target_to_host_shmid_ds(&dsarg, buf))
4452 return -TARGET_EFAULT;
4453 ret = get_errno(shmctl(shmid, cmd, &dsarg));
4454 if (host_to_target_shmid_ds(buf, &dsarg))
4455 return -TARGET_EFAULT;
4456 break;
4457 case IPC_INFO:
4458 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
4459 if (host_to_target_shminfo(buf, &shminfo))
4460 return -TARGET_EFAULT;
4461 break;
4462 case SHM_INFO:
4463 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
4464 if (host_to_target_shm_info(buf, &shm_info))
4465 return -TARGET_EFAULT;
4466 break;
4467 case IPC_RMID:
4468 case SHM_LOCK:
4469 case SHM_UNLOCK:
4470 ret = get_errno(shmctl(shmid, cmd, NULL));
4471 break;
4472 }
4473
4474 return ret;
4475}
4476
1c54ff97 4477#ifdef TARGET_NR_ipc
53a5960a 4478/* ??? This only works with linear mappings. */
0da46a6e 4479/* do_ipc() must return target values and target errnos. */
ee8e7614
PM
4480static abi_long do_ipc(CPUArchState *cpu_env,
4481 unsigned int call, abi_long first,
37ed0956 4482 abi_long second, abi_long third,
992f48a0 4483 abi_long ptr, abi_long fifth)
8853f86e
FB
4484{
4485 int version;
992f48a0 4486 abi_long ret = 0;
8853f86e
FB
4487
4488 version = call >> 16;
4489 call &= 0xffff;
4490
4491 switch (call) {
fa294816 4492 case IPCOP_semop:
cac46eb0 4493 ret = do_semtimedop(first, ptr, second, 0, false);
d8c08b1e
MK
4494 break;
4495 case IPCOP_semtimedop:
4496 /*
4497 * The s390 sys_ipc variant has only five parameters instead of six
4498 * (as for default variant) and the only difference is the handling of
4499 * SEMTIMEDOP where on s390 the third parameter is used as a pointer
4500 * to a struct timespec where the generic variant uses fifth parameter.
4501 */
4502#if defined(TARGET_S390X)
cac46eb0 4503 ret = do_semtimedop(first, ptr, second, third, TARGET_ABI_BITS == 64);
d8c08b1e 4504#else
cac46eb0 4505 ret = do_semtimedop(first, ptr, second, fifth, TARGET_ABI_BITS == 64);
d8c08b1e 4506#endif
fa294816
TS
4507 break;
4508
4509 case IPCOP_semget:
4510 ret = get_errno(semget(first, second, third));
4511 break;
4512
5d2fa8eb
TM
4513 case IPCOP_semctl: {
4514 /* The semun argument to semctl is passed by value, so dereference the
4515 * ptr argument. */
4516 abi_ulong atptr;
37ed0956 4517 get_user_ual(atptr, ptr);
d1c002b6 4518 ret = do_semctl(first, second, third, atptr);
fa294816 4519 break;
5d2fa8eb 4520 }
d96372ef 4521
1c54ff97
AJ
4522 case IPCOP_msgget:
4523 ret = get_errno(msgget(first, second));
4524 break;
d96372ef 4525
1c54ff97
AJ
4526 case IPCOP_msgsnd:
4527 ret = do_msgsnd(first, ptr, second, third);
4528 break;
d96372ef 4529
1c54ff97
AJ
4530 case IPCOP_msgctl:
4531 ret = do_msgctl(first, second, ptr);
4532 break;
d96372ef 4533
1c54ff97
AJ
4534 case IPCOP_msgrcv:
4535 switch (version) {
4536 case 0:
4537 {
4538 struct target_ipc_kludge {
4539 abi_long msgp;
4540 abi_long msgtyp;
4541 } *tmp;
4542
4543 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
4544 ret = -TARGET_EFAULT;
4545 break;
4546 }
d96372ef 4547
79dd77de 4548 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
d96372ef 4549
1c54ff97
AJ
4550 unlock_user_struct(tmp, ptr, 0);
4551 break;
4552 }
4553 default:
4554 ret = do_msgrcv(first, ptr, second, fifth, third);
4555 }
4556 break;
d96372ef 4557
8853f86e 4558 case IPCOP_shmat:
88a8c984
RV
4559 switch (version) {
4560 default:
5a4a898d
FB
4561 {
4562 abi_ulong raddr;
225a206c 4563 raddr = target_shmat(cpu_env, first, ptr, second);
88a8c984
RV
4564 if (is_error(raddr))
4565 return get_errno(raddr);
2f619698 4566 if (put_user_ual(raddr, third))
5a4a898d 4567 return -TARGET_EFAULT;
88a8c984
RV
4568 break;
4569 }
4570 case 1:
4571 ret = -TARGET_EINVAL;
4572 break;
5a4a898d 4573 }
8853f86e
FB
4574 break;
4575 case IPCOP_shmdt:
225a206c 4576 ret = target_shmdt(ptr);
8853f86e
FB
4577 break;
4578
4579 case IPCOP_shmget:
4580 /* IPC_* flag values are the same on all linux platforms */
4581 ret = get_errno(shmget(first, second, third));
4582 break;
4583
4584 /* IPC_* and SHM_* command values are the same on all linux platforms */
4585 case IPCOP_shmctl:
a2926784 4586 ret = do_shmctl(first, second, ptr);
8853f86e
FB
4587 break;
4588 default:
39be5350
JK
4589 qemu_log_mask(LOG_UNIMP, "Unsupported ipc call: %d (version %d)\n",
4590 call, version);
0da46a6e 4591 ret = -TARGET_ENOSYS;
8853f86e
FB
4592 break;
4593 }
4594 return ret;
4595}
32407103 4596#endif
8853f86e 4597
31e31b8a 4598/* kernel structure types definitions */
31e31b8a 4599
001faf32 4600#define STRUCT(name, ...) STRUCT_ ## name,
31e31b8a
FB
4601#define STRUCT_SPECIAL(name) STRUCT_ ## name,
4602enum {
4603#include "syscall_types.h"
8be656b8 4604STRUCT_MAX
31e31b8a
FB
4605};
4606#undef STRUCT
4607#undef STRUCT_SPECIAL
4608
001faf32 4609#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
31e31b8a
FB
4610#define STRUCT_SPECIAL(name)
4611#include "syscall_types.h"
4612#undef STRUCT
4613#undef STRUCT_SPECIAL
4614
31e31b8a
FB
4615#define MAX_STRUCT_SIZE 4096
4616
dace20dc 4617#ifdef CONFIG_FIEMAP
285da2b9
PM
4618/* So fiemap access checks don't overflow on 32 bit systems.
4619 * This is very slightly smaller than the limit imposed by
4620 * the underlying kernel.
4621 */
4622#define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
4623 / sizeof(struct fiemap_extent))
4624
4625static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
45c874eb 4626 int fd, int cmd, abi_long arg)
285da2b9
PM
4627{
4628 /* The parameter for this ioctl is a struct fiemap followed
4629 * by an array of struct fiemap_extent whose size is set
4630 * in fiemap->fm_extent_count. The array is filled in by the
4631 * ioctl.
4632 */
4633 int target_size_in, target_size_out;
4634 struct fiemap *fm;
4635 const argtype *arg_type = ie->arg_type;
4636 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
4637 void *argptr, *p;
4638 abi_long ret;
4639 int i, extent_size = thunk_type_size(extent_arg_type, 0);
4640 uint32_t outbufsz;
4641 int free_fm = 0;
4642
4643 assert(arg_type[0] == TYPE_PTR);
4644 assert(ie->access == IOC_RW);
4645 arg_type++;
4646 target_size_in = thunk_type_size(arg_type, 0);
4647 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
4648 if (!argptr) {
4649 return -TARGET_EFAULT;
4650 }
4651 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4652 unlock_user(argptr, arg, 0);
4653 fm = (struct fiemap *)buf_temp;
4654 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
4655 return -TARGET_EINVAL;
4656 }
4657
4658 outbufsz = sizeof (*fm) +
4659 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
4660
4661 if (outbufsz > MAX_STRUCT_SIZE) {
4662 /* We can't fit all the extents into the fixed size buffer.
4663 * Allocate one that is large enough and use it instead.
4664 */
0e173b24 4665 fm = g_try_malloc(outbufsz);
285da2b9
PM
4666 if (!fm) {
4667 return -TARGET_ENOMEM;
4668 }
4669 memcpy(fm, buf_temp, sizeof(struct fiemap));
4670 free_fm = 1;
4671 }
49ca6f3e 4672 ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm));
285da2b9
PM
4673 if (!is_error(ret)) {
4674 target_size_out = target_size_in;
4675 /* An extent_count of 0 means we were only counting the extents
4676 * so there are no structs to copy
4677 */
4678 if (fm->fm_extent_count != 0) {
4679 target_size_out += fm->fm_mapped_extents * extent_size;
4680 }
4681 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
4682 if (!argptr) {
4683 ret = -TARGET_EFAULT;
4684 } else {
4685 /* Convert the struct fiemap */
4686 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
4687 if (fm->fm_extent_count != 0) {
4688 p = argptr + target_size_in;
4689 /* ...and then all the struct fiemap_extents */
4690 for (i = 0; i < fm->fm_mapped_extents; i++) {
4691 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
4692 THUNK_TARGET);
4693 p += extent_size;
4694 }
4695 }
4696 unlock_user(argptr, arg, target_size_out);
4697 }
4698 }
4699 if (free_fm) {
0e173b24 4700 g_free(fm);
285da2b9
PM
4701 }
4702 return ret;
4703}
dace20dc 4704#endif
285da2b9 4705
059c2f2c 4706static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
45c874eb 4707 int fd, int cmd, abi_long arg)
059c2f2c
LV
4708{
4709 const argtype *arg_type = ie->arg_type;
4710 int target_size;
4711 void *argptr;
4712 int ret;
4713 struct ifconf *host_ifconf;
4714 uint32_t outbufsz;
4715 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
4df7b7fa 4716 const argtype ifreq_max_type[] = { MK_STRUCT(STRUCT_ifmap_ifreq) };
059c2f2c
LV
4717 int target_ifreq_size;
4718 int nb_ifreq;
4719 int free_buf = 0;
4720 int i;
4721 int target_ifc_len;
4722 abi_long target_ifc_buf;
4723 int host_ifc_len;
4724 char *host_ifc_buf;
4725
4726 assert(arg_type[0] == TYPE_PTR);
4727 assert(ie->access == IOC_RW);
4728
4729 arg_type++;
4730 target_size = thunk_type_size(arg_type, 0);
4731
4732 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4733 if (!argptr)
4734 return -TARGET_EFAULT;
4735 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4736 unlock_user(argptr, arg, 0);
4737
4738 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
059c2f2c 4739 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
4df7b7fa 4740 target_ifreq_size = thunk_type_size(ifreq_max_type, 0);
059c2f2c 4741
22e4a267
KL
4742 if (target_ifc_buf != 0) {
4743 target_ifc_len = host_ifconf->ifc_len;
4744 nb_ifreq = target_ifc_len / target_ifreq_size;
4745 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
4746
4747 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
4748 if (outbufsz > MAX_STRUCT_SIZE) {
4749 /*
4750 * We can't fit all the extents into the fixed size buffer.
4751 * Allocate one that is large enough and use it instead.
4752 */
7a5626a1 4753 host_ifconf = g_try_malloc(outbufsz);
22e4a267
KL
4754 if (!host_ifconf) {
4755 return -TARGET_ENOMEM;
4756 }
4757 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
4758 free_buf = 1;
059c2f2c 4759 }
22e4a267 4760 host_ifc_buf = (char *)host_ifconf + sizeof(*host_ifconf);
059c2f2c 4761
22e4a267
KL
4762 host_ifconf->ifc_len = host_ifc_len;
4763 } else {
4764 host_ifc_buf = NULL;
4765 }
059c2f2c
LV
4766 host_ifconf->ifc_buf = host_ifc_buf;
4767
49ca6f3e 4768 ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf));
059c2f2c
LV
4769 if (!is_error(ret)) {
4770 /* convert host ifc_len to target ifc_len */
4771
4772 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
4773 target_ifc_len = nb_ifreq * target_ifreq_size;
4774 host_ifconf->ifc_len = target_ifc_len;
4775
4776 /* restore target ifc_buf */
4777
4778 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
4779
4780 /* copy struct ifconf to target user */
4781
4782 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4783 if (!argptr)
4784 return -TARGET_EFAULT;
4785 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
4786 unlock_user(argptr, arg, target_size);
4787
22e4a267
KL
4788 if (target_ifc_buf != 0) {
4789 /* copy ifreq[] to target user */
4790 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
4791 for (i = 0; i < nb_ifreq ; i++) {
4792 thunk_convert(argptr + i * target_ifreq_size,
4793 host_ifc_buf + i * sizeof(struct ifreq),
4794 ifreq_arg_type, THUNK_TARGET);
4795 }
4796 unlock_user(argptr, target_ifc_buf, target_ifc_len);
059c2f2c 4797 }
059c2f2c
LV
4798 }
4799
4800 if (free_buf) {
7a5626a1 4801 g_free(host_ifconf);
059c2f2c
LV
4802 }
4803
4804 return ret;
4805}
4806
a133367e
CT
4807#if defined(CONFIG_USBFS)
4808#if HOST_LONG_BITS > 64
4809#error USBDEVFS thunks do not support >64 bit hosts yet.
4810#endif
4811struct live_urb {
4812 uint64_t target_urb_adr;
4813 uint64_t target_buf_adr;
4814 char *target_buf_ptr;
4815 struct usbdevfs_urb host_urb;
4816};
4817
4818static GHashTable *usbdevfs_urb_hashtable(void)
4819{
4820 static GHashTable *urb_hashtable;
4821
4822 if (!urb_hashtable) {
4823 urb_hashtable = g_hash_table_new(g_int64_hash, g_int64_equal);
4824 }
4825 return urb_hashtable;
4826}
4827
4828static void urb_hashtable_insert(struct live_urb *urb)
4829{
4830 GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
4831 g_hash_table_insert(urb_hashtable, urb, urb);
4832}
4833
4834static struct live_urb *urb_hashtable_lookup(uint64_t target_urb_adr)
4835{
4836 GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
4837 return g_hash_table_lookup(urb_hashtable, &target_urb_adr);
4838}
4839
4840static void urb_hashtable_remove(struct live_urb *urb)
4841{
4842 GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
4843 g_hash_table_remove(urb_hashtable, urb);
4844}
4845
4846static abi_long
4847do_ioctl_usbdevfs_reapurb(const IOCTLEntry *ie, uint8_t *buf_temp,
4848 int fd, int cmd, abi_long arg)
4849{
4850 const argtype usbfsurb_arg_type[] = { MK_STRUCT(STRUCT_usbdevfs_urb) };
4851 const argtype ptrvoid_arg_type[] = { TYPE_PTRVOID, 0, 0 };
4852 struct live_urb *lurb;
4853 void *argptr;
4854 uint64_t hurb;
4855 int target_size;
4856 uintptr_t target_urb_adr;
4857 abi_long ret;
4858
4859 target_size = thunk_type_size(usbfsurb_arg_type, THUNK_TARGET);
4860
4861 memset(buf_temp, 0, sizeof(uint64_t));
4862 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
4863 if (is_error(ret)) {
4864 return ret;
4865 }
4866
4867 memcpy(&hurb, buf_temp, sizeof(uint64_t));
4868 lurb = (void *)((uintptr_t)hurb - offsetof(struct live_urb, host_urb));
4869 if (!lurb->target_urb_adr) {
4870 return -TARGET_EFAULT;
4871 }
4872 urb_hashtable_remove(lurb);
4873 unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr,
4874 lurb->host_urb.buffer_length);
4875 lurb->target_buf_ptr = NULL;
4876
4877 /* restore the guest buffer pointer */
4878 lurb->host_urb.buffer = (void *)(uintptr_t)lurb->target_buf_adr;
4879
4880 /* update the guest urb struct */
4881 argptr = lock_user(VERIFY_WRITE, lurb->target_urb_adr, target_size, 0);
4882 if (!argptr) {
4883 g_free(lurb);
4884 return -TARGET_EFAULT;
4885 }
4886 thunk_convert(argptr, &lurb->host_urb, usbfsurb_arg_type, THUNK_TARGET);
4887 unlock_user(argptr, lurb->target_urb_adr, target_size);
4888
4889 target_size = thunk_type_size(ptrvoid_arg_type, THUNK_TARGET);
4890 /* write back the urb handle */
4891 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4892 if (!argptr) {
4893 g_free(lurb);
4894 return -TARGET_EFAULT;
4895 }
4896
4897 /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */
4898 target_urb_adr = lurb->target_urb_adr;
4899 thunk_convert(argptr, &target_urb_adr, ptrvoid_arg_type, THUNK_TARGET);
4900 unlock_user(argptr, arg, target_size);
4901
4902 g_free(lurb);
4903 return ret;
4904}
4905
4906static abi_long
4907do_ioctl_usbdevfs_discardurb(const IOCTLEntry *ie,
4908 uint8_t *buf_temp __attribute__((unused)),
4909 int fd, int cmd, abi_long arg)
4910{
4911 struct live_urb *lurb;
4912
4913 /* map target address back to host URB with metadata. */
4914 lurb = urb_hashtable_lookup(arg);
4915 if (!lurb) {
4916 return -TARGET_EFAULT;
4917 }
4918 return get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
4919}
4920
4921static abi_long
4922do_ioctl_usbdevfs_submiturb(const IOCTLEntry *ie, uint8_t *buf_temp,
4923 int fd, int cmd, abi_long arg)
4924{
4925 const argtype *arg_type = ie->arg_type;
4926 int target_size;
4927 abi_long ret;
4928 void *argptr;
4929 int rw_dir;
4930 struct live_urb *lurb;
4931
4932 /*
4933 * each submitted URB needs to map to a unique ID for the
4934 * kernel, and that unique ID needs to be a pointer to
4935 * host memory. hence, we need to malloc for each URB.
4936 * isochronous transfers have a variable length struct.
4937 */
4938 arg_type++;
4939 target_size = thunk_type_size(arg_type, THUNK_TARGET);
4940
4941 /* construct host copy of urb and metadata */
b21e2380 4942 lurb = g_try_new0(struct live_urb, 1);
a133367e
CT
4943 if (!lurb) {
4944 return -TARGET_ENOMEM;
4945 }
4946
4947 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4948 if (!argptr) {
4949 g_free(lurb);
4950 return -TARGET_EFAULT;
4951 }
4952 thunk_convert(&lurb->host_urb, argptr, arg_type, THUNK_HOST);
4953 unlock_user(argptr, arg, 0);
4954
4955 lurb->target_urb_adr = arg;
4956 lurb->target_buf_adr = (uintptr_t)lurb->host_urb.buffer;
4957
4958 /* buffer space used depends on endpoint type so lock the entire buffer */
4959 /* control type urbs should check the buffer contents for true direction */
4960 rw_dir = lurb->host_urb.endpoint & USB_DIR_IN ? VERIFY_WRITE : VERIFY_READ;
4961 lurb->target_buf_ptr = lock_user(rw_dir, lurb->target_buf_adr,
4962 lurb->host_urb.buffer_length, 1);
4963 if (lurb->target_buf_ptr == NULL) {
4964 g_free(lurb);
4965 return -TARGET_EFAULT;
4966 }
4967
4968 /* update buffer pointer in host copy */
4969 lurb->host_urb.buffer = lurb->target_buf_ptr;
4970
4971 ret = get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
4972 if (is_error(ret)) {
4973 unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, 0);
4974 g_free(lurb);
4975 } else {
4976 urb_hashtable_insert(lurb);
4977 }
4978
4979 return ret;
4980}
4981#endif /* CONFIG_USBFS */
4982
56e904ec 4983static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
45c874eb 4984 int cmd, abi_long arg)
56e904ec
AG
4985{
4986 void *argptr;
4987 struct dm_ioctl *host_dm;
4988 abi_long guest_data;
4989 uint32_t guest_data_size;
4990 int target_size;
4991 const argtype *arg_type = ie->arg_type;
4992 abi_long ret;
4993 void *big_buf = NULL;
4994 char *host_data;
4995
4996 arg_type++;
4997 target_size = thunk_type_size(arg_type, 0);
4998 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4999 if (!argptr) {
5000 ret = -TARGET_EFAULT;
5001 goto out;
5002 }
5003 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5004 unlock_user(argptr, arg, 0);
5005
5006 /* buf_temp is too small, so fetch things into a bigger buffer */
5007 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
5008 memcpy(big_buf, buf_temp, target_size);
5009 buf_temp = big_buf;
5010 host_dm = big_buf;
5011
5012 guest_data = arg + host_dm->data_start;
5013 if ((guest_data - arg) < 0) {
f9757b1d 5014 ret = -TARGET_EINVAL;
56e904ec
AG
5015 goto out;
5016 }
5017 guest_data_size = host_dm->data_size - host_dm->data_start;
5018 host_data = (char*)host_dm + host_dm->data_start;
5019
5020 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
3211215e
PM
5021 if (!argptr) {
5022 ret = -TARGET_EFAULT;
5023 goto out;
5024 }
5025
56e904ec
AG
5026 switch (ie->host_cmd) {
5027 case DM_REMOVE_ALL:
5028 case DM_LIST_DEVICES:
5029 case DM_DEV_CREATE:
5030 case DM_DEV_REMOVE:
5031 case DM_DEV_SUSPEND:
5032 case DM_DEV_STATUS:
5033 case DM_DEV_WAIT:
5034 case DM_TABLE_STATUS:
5035 case DM_TABLE_CLEAR:
5036 case DM_TABLE_DEPS:
5037 case DM_LIST_VERSIONS:
5038 /* no input data */
5039 break;
5040 case DM_DEV_RENAME:
5041 case DM_DEV_SET_GEOMETRY:
5042 /* data contains only strings */
5043 memcpy(host_data, argptr, guest_data_size);
5044 break;
5045 case DM_TARGET_MSG:
5046 memcpy(host_data, argptr, guest_data_size);
5047 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
5048 break;
5049 case DM_TABLE_LOAD:
5050 {
5051 void *gspec = argptr;
5052 void *cur_data = host_data;
eb2929ca
LV
5053 const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
5054 int spec_size = thunk_type_size(dm_arg_type, 0);
56e904ec
AG
5055 int i;
5056
5057 for (i = 0; i < host_dm->target_count; i++) {
5058 struct dm_target_spec *spec = cur_data;
5059 uint32_t next;
5060 int slen;
5061
eb2929ca 5062 thunk_convert(spec, gspec, dm_arg_type, THUNK_HOST);
56e904ec
AG
5063 slen = strlen((char*)gspec + spec_size) + 1;
5064 next = spec->next;
5065 spec->next = sizeof(*spec) + slen;
5066 strcpy((char*)&spec[1], gspec + spec_size);
5067 gspec += next;
5068 cur_data += spec->next;
5069 }
5070 break;
5071 }
5072 default:
5073 ret = -TARGET_EINVAL;
dec0473d 5074 unlock_user(argptr, guest_data, 0);
56e904ec
AG
5075 goto out;
5076 }
5077 unlock_user(argptr, guest_data, 0);
5078
49ca6f3e 5079 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
56e904ec
AG
5080 if (!is_error(ret)) {
5081 guest_data = arg + host_dm->data_start;
5082 guest_data_size = host_dm->data_size - host_dm->data_start;
5083 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
5084 switch (ie->host_cmd) {
5085 case DM_REMOVE_ALL:
5086 case DM_DEV_CREATE:
5087 case DM_DEV_REMOVE:
5088 case DM_DEV_RENAME:
5089 case DM_DEV_SUSPEND:
5090 case DM_DEV_STATUS:
5091 case DM_TABLE_LOAD:
5092 case DM_TABLE_CLEAR:
5093 case DM_TARGET_MSG:
5094 case DM_DEV_SET_GEOMETRY:
5095 /* no return data */
5096 break;
5097 case DM_LIST_DEVICES:
5098 {
5099 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
5100 uint32_t remaining_data = guest_data_size;
5101 void *cur_data = argptr;
eb2929ca 5102 const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
56e904ec
AG
5103 int nl_size = 12; /* can't use thunk_size due to alignment */
5104
5105 while (1) {
5106 uint32_t next = nl->next;
5107 if (next) {
5108 nl->next = nl_size + (strlen(nl->name) + 1);
5109 }
5110 if (remaining_data < nl->next) {
5111 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5112 break;
5113 }
eb2929ca 5114 thunk_convert(cur_data, nl, dm_arg_type, THUNK_TARGET);
56e904ec
AG
5115 strcpy(cur_data + nl_size, nl->name);
5116 cur_data += nl->next;
5117 remaining_data -= nl->next;
5118 if (!next) {
5119 break;
5120 }
5121 nl = (void*)nl + next;
5122 }
5123 break;
5124 }
5125 case DM_DEV_WAIT:
5126 case DM_TABLE_STATUS:
5127 {
5128 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
5129 void *cur_data = argptr;
eb2929ca
LV
5130 const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
5131 int spec_size = thunk_type_size(dm_arg_type, 0);
56e904ec
AG
5132 int i;
5133
5134 for (i = 0; i < host_dm->target_count; i++) {
5135 uint32_t next = spec->next;
5136 int slen = strlen((char*)&spec[1]) + 1;
5137 spec->next = (cur_data - argptr) + spec_size + slen;
5138 if (guest_data_size < spec->next) {
5139 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5140 break;
5141 }
eb2929ca 5142 thunk_convert(cur_data, spec, dm_arg_type, THUNK_TARGET);
56e904ec
AG
5143 strcpy(cur_data + spec_size, (char*)&spec[1]);
5144 cur_data = argptr + spec->next;
5145 spec = (void*)host_dm + host_dm->data_start + next;
5146 }
5147 break;
5148 }
5149 case DM_TABLE_DEPS:
5150 {
5151 void *hdata = (void*)host_dm + host_dm->data_start;
5152 int count = *(uint32_t*)hdata;
5153 uint64_t *hdev = hdata + 8;
5154 uint64_t *gdev = argptr + 8;
5155 int i;
5156
5157 *(uint32_t*)argptr = tswap32(count);
5158 for (i = 0; i < count; i++) {
5159 *gdev = tswap64(*hdev);
5160 gdev++;
5161 hdev++;
5162 }
5163 break;
5164 }
5165 case DM_LIST_VERSIONS:
5166 {
5167 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
5168 uint32_t remaining_data = guest_data_size;
5169 void *cur_data = argptr;
eb2929ca
LV
5170 const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
5171 int vers_size = thunk_type_size(dm_arg_type, 0);
56e904ec
AG
5172
5173 while (1) {
5174 uint32_t next = vers->next;
5175 if (next) {
5176 vers->next = vers_size + (strlen(vers->name) + 1);
5177 }
5178 if (remaining_data < vers->next) {
5179 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5180 break;
5181 }
eb2929ca 5182 thunk_convert(cur_data, vers, dm_arg_type, THUNK_TARGET);
56e904ec
AG
5183 strcpy(cur_data + vers_size, vers->name);
5184 cur_data += vers->next;
5185 remaining_data -= vers->next;
5186 if (!next) {
5187 break;
5188 }
5189 vers = (void*)vers + next;
5190 }
5191 break;
5192 }
5193 default:
dec0473d 5194 unlock_user(argptr, guest_data, 0);
56e904ec
AG
5195 ret = -TARGET_EINVAL;
5196 goto out;
5197 }
5198 unlock_user(argptr, guest_data, guest_data_size);
5199
5200 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5201 if (!argptr) {
5202 ret = -TARGET_EFAULT;
5203 goto out;
5204 }
5205 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5206 unlock_user(argptr, arg, target_size);
5207 }
5208out:
ad11ad77 5209 g_free(big_buf);
56e904ec
AG
5210 return ret;
5211}
5212
a59b5e35 5213static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
45c874eb 5214 int cmd, abi_long arg)
a59b5e35
AG
5215{
5216 void *argptr;
5217 int target_size;
5218 const argtype *arg_type = ie->arg_type;
5219 const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
5220 abi_long ret;
5221
5222 struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
5223 struct blkpg_partition host_part;
5224
5225 /* Read and convert blkpg */
5226 arg_type++;
5227 target_size = thunk_type_size(arg_type, 0);
5228 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5229 if (!argptr) {
5230 ret = -TARGET_EFAULT;
5231 goto out;
5232 }
5233 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5234 unlock_user(argptr, arg, 0);
5235
5236 switch (host_blkpg->op) {
5237 case BLKPG_ADD_PARTITION:
5238 case BLKPG_DEL_PARTITION:
5239 /* payload is struct blkpg_partition */
5240 break;
5241 default:
5242 /* Unknown opcode */
5243 ret = -TARGET_EINVAL;
5244 goto out;
5245 }
5246
5247 /* Read and convert blkpg->data */
5248 arg = (abi_long)(uintptr_t)host_blkpg->data;
5249 target_size = thunk_type_size(part_arg_type, 0);
5250 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5251 if (!argptr) {
5252 ret = -TARGET_EFAULT;
5253 goto out;
5254 }
5255 thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
5256 unlock_user(argptr, arg, 0);
5257
5258 /* Swizzle the data pointer to our local copy and call! */
5259 host_blkpg->data = &host_part;
49ca6f3e 5260 ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg));
a59b5e35
AG
5261
5262out:
5263 return ret;
5264}
5265
7ff7b666 5266static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
45c874eb 5267 int fd, int cmd, abi_long arg)
7ff7b666
LV
5268{
5269 const argtype *arg_type = ie->arg_type;
5270 const StructEntry *se;
5271 const argtype *field_types;
5272 const int *dst_offsets, *src_offsets;
5273 int target_size;
5274 void *argptr;
b78c522a
MAL
5275 abi_ulong *target_rt_dev_ptr = NULL;
5276 unsigned long *host_rt_dev_ptr = NULL;
7ff7b666
LV
5277 abi_long ret;
5278 int i;
5279
5280 assert(ie->access == IOC_W);
5281 assert(*arg_type == TYPE_PTR);
5282 arg_type++;
5283 assert(*arg_type == TYPE_STRUCT);
5284 target_size = thunk_type_size(arg_type, 0);
5285 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5286 if (!argptr) {
5287 return -TARGET_EFAULT;
5288 }
5289 arg_type++;
5290 assert(*arg_type == (int)STRUCT_rtentry);
5291 se = struct_entries + *arg_type++;
5292 assert(se->convert[0] == NULL);
5293 /* convert struct here to be able to catch rt_dev string */
5294 field_types = se->field_types;
5295 dst_offsets = se->field_offsets[THUNK_HOST];
5296 src_offsets = se->field_offsets[THUNK_TARGET];
5297 for (i = 0; i < se->nb_fields; i++) {
5298 if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
5299 assert(*field_types == TYPE_PTRVOID);
3d558330 5300 target_rt_dev_ptr = argptr + src_offsets[i];
7ff7b666
LV
5301 host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
5302 if (*target_rt_dev_ptr != 0) {
5303 *host_rt_dev_ptr = (unsigned long)lock_user_string(
5304 tswapal(*target_rt_dev_ptr));
5305 if (!*host_rt_dev_ptr) {
5306 unlock_user(argptr, arg, 0);
5307 return -TARGET_EFAULT;
5308 }
5309 } else {
5310 *host_rt_dev_ptr = 0;
5311 }
5312 field_types++;
5313 continue;
5314 }
5315 field_types = thunk_convert(buf_temp + dst_offsets[i],
5316 argptr + src_offsets[i],
5317 field_types, THUNK_HOST);
5318 }
5319 unlock_user(argptr, arg, 0);
5320
49ca6f3e 5321 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
b78c522a
MAL
5322
5323 assert(host_rt_dev_ptr != NULL);
5324 assert(target_rt_dev_ptr != NULL);
7ff7b666
LV
5325 if (*host_rt_dev_ptr != 0) {
5326 unlock_user((void *)*host_rt_dev_ptr,
5327 *target_rt_dev_ptr, 0);
5328 }
5329 return ret;
5330}
5331
ca56f5b5 5332static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
45c874eb 5333 int fd, int cmd, abi_long arg)
ca56f5b5
PB
5334{
5335 int sig = target_to_host_signal(arg);
49ca6f3e 5336 return get_errno(safe_ioctl(fd, ie->host_cmd, sig));
ca56f5b5
PB
5337}
5338
6d5d5dde
DB
5339static abi_long do_ioctl_SIOCGSTAMP(const IOCTLEntry *ie, uint8_t *buf_temp,
5340 int fd, int cmd, abi_long arg)
5341{
5342 struct timeval tv;
5343 abi_long ret;
5344
5345 ret = get_errno(safe_ioctl(fd, SIOCGSTAMP, &tv));
5346 if (is_error(ret)) {
5347 return ret;
5348 }
5349
5350 if (cmd == (int)TARGET_SIOCGSTAMP_OLD) {
5351 if (copy_to_user_timeval(arg, &tv)) {
5352 return -TARGET_EFAULT;
5353 }
5354 } else {
5355 if (copy_to_user_timeval64(arg, &tv)) {
5356 return -TARGET_EFAULT;
5357 }
5358 }
5359
5360 return ret;
5361}
5362
5363static abi_long do_ioctl_SIOCGSTAMPNS(const IOCTLEntry *ie, uint8_t *buf_temp,
5364 int fd, int cmd, abi_long arg)
5365{
5366 struct timespec ts;
5367 abi_long ret;
5368
5369 ret = get_errno(safe_ioctl(fd, SIOCGSTAMPNS, &ts));
5370 if (is_error(ret)) {
5371 return ret;
5372 }
5373
5374 if (cmd == (int)TARGET_SIOCGSTAMPNS_OLD) {
5375 if (host_to_target_timespec(arg, &ts)) {
5376 return -TARGET_EFAULT;
5377 }
5378 } else{
5379 if (host_to_target_timespec64(arg, &ts)) {
5380 return -TARGET_EFAULT;
5381 }
5382 }
5383
5384 return ret;
5385}
5386
2b74f621
AS
5387#ifdef TIOCGPTPEER
5388static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_temp,
5389 int fd, int cmd, abi_long arg)
5390{
5391 int flags = target_to_host_bitmask(arg, fcntl_flags_tbl);
5392 return get_errno(safe_ioctl(fd, ie->host_cmd, flags));
5393}
5394#endif
5395
e865b97f
CG
5396#ifdef HAVE_DRM_H
5397
5398static void unlock_drm_version(struct drm_version *host_ver,
5399 struct target_drm_version *target_ver,
5400 bool copy)
5401{
5402 unlock_user(host_ver->name, target_ver->name,
5403 copy ? host_ver->name_len : 0);
5404 unlock_user(host_ver->date, target_ver->date,
5405 copy ? host_ver->date_len : 0);
5406 unlock_user(host_ver->desc, target_ver->desc,
5407 copy ? host_ver->desc_len : 0);
5408}
5409
5410static inline abi_long target_to_host_drmversion(struct drm_version *host_ver,
5411 struct target_drm_version *target_ver)
5412{
5413 memset(host_ver, 0, sizeof(*host_ver));
5414
5415 __get_user(host_ver->name_len, &target_ver->name_len);
5416 if (host_ver->name_len) {
5417 host_ver->name = lock_user(VERIFY_WRITE, target_ver->name,
5418 target_ver->name_len, 0);
5419 if (!host_ver->name) {
5420 return -EFAULT;
5421 }
5422 }
5423
5424 __get_user(host_ver->date_len, &target_ver->date_len);
5425 if (host_ver->date_len) {
5426 host_ver->date = lock_user(VERIFY_WRITE, target_ver->date,
5427 target_ver->date_len, 0);
5428 if (!host_ver->date) {
5429 goto err;
5430 }
5431 }
5432
5433 __get_user(host_ver->desc_len, &target_ver->desc_len);
5434 if (host_ver->desc_len) {
5435 host_ver->desc = lock_user(VERIFY_WRITE, target_ver->desc,
5436 target_ver->desc_len, 0);
5437 if (!host_ver->desc) {
5438 goto err;
5439 }
5440 }
5441
5442 return 0;
5443err:
5444 unlock_drm_version(host_ver, target_ver, false);
5445 return -EFAULT;
5446}
5447
5448static inline void host_to_target_drmversion(
5449 struct target_drm_version *target_ver,
5450 struct drm_version *host_ver)
5451{
5452 __put_user(host_ver->version_major, &target_ver->version_major);
5453 __put_user(host_ver->version_minor, &target_ver->version_minor);
5454 __put_user(host_ver->version_patchlevel, &target_ver->version_patchlevel);
5455 __put_user(host_ver->name_len, &target_ver->name_len);
5456 __put_user(host_ver->date_len, &target_ver->date_len);
5457 __put_user(host_ver->desc_len, &target_ver->desc_len);
5458 unlock_drm_version(host_ver, target_ver, true);
5459}
5460
5461static abi_long do_ioctl_drm(const IOCTLEntry *ie, uint8_t *buf_temp,
5462 int fd, int cmd, abi_long arg)
5463{
5464 struct drm_version *ver;
5465 struct target_drm_version *target_ver;
5466 abi_long ret;
5467
5468 switch (ie->host_cmd) {
5469 case DRM_IOCTL_VERSION:
5470 if (!lock_user_struct(VERIFY_WRITE, target_ver, arg, 0)) {
5471 return -TARGET_EFAULT;
5472 }
5473 ver = (struct drm_version *)buf_temp;
5474 ret = target_to_host_drmversion(ver, target_ver);
5475 if (!is_error(ret)) {
5476 ret = get_errno(safe_ioctl(fd, ie->host_cmd, ver));
5477 if (is_error(ret)) {
5478 unlock_drm_version(ver, target_ver, false);
5479 } else {
5480 host_to_target_drmversion(target_ver, ver);
5481 }
5482 }
5483 unlock_user_struct(target_ver, arg, 0);
5484 return ret;
5485 }
5486 return -TARGET_ENOSYS;
5487}
5488
913b03c2
CG
5489static abi_long do_ioctl_drm_i915_getparam(const IOCTLEntry *ie,
5490 struct drm_i915_getparam *gparam,
5491 int fd, abi_long arg)
5492{
5493 abi_long ret;
5494 int value;
5495 struct target_drm_i915_getparam *target_gparam;
5496
5497 if (!lock_user_struct(VERIFY_READ, target_gparam, arg, 0)) {
5498 return -TARGET_EFAULT;
5499 }
5500
5501 __get_user(gparam->param, &target_gparam->param);
5502 gparam->value = &value;
5503 ret = get_errno(safe_ioctl(fd, ie->host_cmd, gparam));
5504 put_user_s32(value, target_gparam->value);
5505
5506 unlock_user_struct(target_gparam, arg, 0);
5507 return ret;
5508}
5509
5510static abi_long do_ioctl_drm_i915(const IOCTLEntry *ie, uint8_t *buf_temp,
5511 int fd, int cmd, abi_long arg)
5512{
5513 switch (ie->host_cmd) {
5514 case DRM_IOCTL_I915_GETPARAM:
5515 return do_ioctl_drm_i915_getparam(ie,
5516 (struct drm_i915_getparam *)buf_temp,
5517 fd, arg);
5518 default:
5519 return -TARGET_ENOSYS;
5520 }
5521}
5522
e865b97f
CG
5523#endif
5524
6addf06a
SCW
5525static abi_long do_ioctl_TUNSETTXFILTER(const IOCTLEntry *ie, uint8_t *buf_temp,
5526 int fd, int cmd, abi_long arg)
5527{
5528 struct tun_filter *filter = (struct tun_filter *)buf_temp;
5529 struct tun_filter *target_filter;
5530 char *target_addr;
5531
5532 assert(ie->access == IOC_W);
5533
5534 target_filter = lock_user(VERIFY_READ, arg, sizeof(*target_filter), 1);
5535 if (!target_filter) {
5536 return -TARGET_EFAULT;
5537 }
5538 filter->flags = tswap16(target_filter->flags);
5539 filter->count = tswap16(target_filter->count);
5540 unlock_user(target_filter, arg, 0);
5541
5542 if (filter->count) {
5543 if (offsetof(struct tun_filter, addr) + filter->count * ETH_ALEN >
5544 MAX_STRUCT_SIZE) {
5545 return -TARGET_EFAULT;
5546 }
5547
5548 target_addr = lock_user(VERIFY_READ,
5549 arg + offsetof(struct tun_filter, addr),
5550 filter->count * ETH_ALEN, 1);
5551 if (!target_addr) {
5552 return -TARGET_EFAULT;
5553 }
5554 memcpy(filter->addr, target_addr, filter->count * ETH_ALEN);
5555 unlock_user(target_addr, arg + offsetof(struct tun_filter, addr), 0);
5556 }
5557
5558 return get_errno(safe_ioctl(fd, ie->host_cmd, filter));
5559}
5560
79482e59 5561IOCTLEntry ioctl_entries[] = {
001faf32 5562#define IOCTL(cmd, access, ...) \
d2ef05bb
PM
5563 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
5564#define IOCTL_SPECIAL(cmd, access, dofn, ...) \
5565 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
8cb762fe
HD
5566#define IOCTL_IGNORE(cmd) \
5567 { TARGET_ ## cmd, 0, #cmd },
31e31b8a
FB
5568#include "ioctls.h"
5569 { 0, 0, },
5570};
5571
53a5960a 5572/* ??? Implement proper locking for ioctls. */
0da46a6e 5573/* do_ioctl() Must return target values and target errnos. */
45c874eb 5574static abi_long do_ioctl(int fd, int cmd, abi_long arg)
31e31b8a
FB
5575{
5576 const IOCTLEntry *ie;
5577 const argtype *arg_type;
992f48a0 5578 abi_long ret;
31e31b8a 5579 uint8_t buf_temp[MAX_STRUCT_SIZE];
53a5960a
PB
5580 int target_size;
5581 void *argptr;
31e31b8a
FB
5582
5583 ie = ioctl_entries;
5584 for(;;) {
5585 if (ie->target_cmd == 0) {
39be5350
JK
5586 qemu_log_mask(
5587 LOG_UNIMP, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
59d11727 5588 return -TARGET_ENOTTY;
31e31b8a
FB
5589 }
5590 if (ie->target_cmd == cmd)
5591 break;
5592 ie++;
5593 }
5594 arg_type = ie->arg_type;
d2ef05bb
PM
5595 if (ie->do_ioctl) {
5596 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
8cb762fe
HD
5597 } else if (!ie->host_cmd) {
5598 /* Some architectures define BSD ioctls in their headers
5599 that are not implemented in Linux. */
59d11727 5600 return -TARGET_ENOTTY;
d2ef05bb
PM
5601 }
5602
31e31b8a
FB
5603 switch(arg_type[0]) {
5604 case TYPE_NULL:
5605 /* no argument */
49ca6f3e 5606 ret = get_errno(safe_ioctl(fd, ie->host_cmd));
31e31b8a
FB
5607 break;
5608 case TYPE_PTRVOID:
5609 case TYPE_INT:
c858e537
FB
5610 case TYPE_LONG:
5611 case TYPE_ULONG:
49ca6f3e 5612 ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
31e31b8a
FB
5613 break;
5614 case TYPE_PTR:
5615 arg_type++;
53a5960a 5616 target_size = thunk_type_size(arg_type, 0);
31e31b8a
FB
5617 switch(ie->access) {
5618 case IOC_R:
49ca6f3e 5619 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
31e31b8a 5620 if (!is_error(ret)) {
579a97f7
FB
5621 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5622 if (!argptr)
5623 return -TARGET_EFAULT;
53a5960a
PB
5624 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5625 unlock_user(argptr, arg, target_size);
31e31b8a
FB
5626 }
5627 break;
5628 case IOC_W:
579a97f7
FB
5629 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5630 if (!argptr)
5631 return -TARGET_EFAULT;
53a5960a
PB
5632 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5633 unlock_user(argptr, arg, 0);
49ca6f3e 5634 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
31e31b8a
FB
5635 break;
5636 default:
5637 case IOC_RW:
579a97f7
FB
5638 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5639 if (!argptr)
5640 return -TARGET_EFAULT;
53a5960a
PB
5641 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5642 unlock_user(argptr, arg, 0);
49ca6f3e 5643 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
31e31b8a 5644 if (!is_error(ret)) {
579a97f7
FB
5645 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5646 if (!argptr)
5647 return -TARGET_EFAULT;
53a5960a
PB
5648 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5649 unlock_user(argptr, arg, target_size);
31e31b8a
FB
5650 }
5651 break;
5652 }
5653 break;
5654 default:
39be5350
JK
5655 qemu_log_mask(LOG_UNIMP,
5656 "Unsupported ioctl type: cmd=0x%04lx type=%d\n",
5657 (long)cmd, arg_type[0]);
59d11727 5658 ret = -TARGET_ENOTTY;
31e31b8a
FB
5659 break;
5660 }
5661 return ret;
5662}
5663
b39bc503 5664static const bitmask_transtbl iflag_tbl[] = {
31e31b8a
FB
5665 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
5666 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
5667 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
5668 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
5669 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
5670 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
5671 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
5672 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
5673 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
5674 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
5675 { TARGET_IXON, TARGET_IXON, IXON, IXON },
5676 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
5677 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
5678 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
c218b4ed 5679 { TARGET_IUTF8, TARGET_IUTF8, IUTF8, IUTF8},
31e31b8a
FB
5680};
5681
b39bc503 5682static const bitmask_transtbl oflag_tbl[] = {
31e31b8a
FB
5683 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
5684 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
5685 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
5686 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
5687 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
5688 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
5689 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
5690 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
5691 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
5692 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
5693 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
5694 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
5695 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
5696 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
5697 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
5698 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
5699 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
5700 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
5701 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
5702 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
5703 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
5704 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
5705 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
5706 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
31e31b8a
FB
5707};
5708
b39bc503 5709static const bitmask_transtbl cflag_tbl[] = {
31e31b8a
FB
5710 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
5711 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
5712 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
5713 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
5714 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
5715 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
5716 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
5717 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
5718 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
5719 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
5720 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
5721 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
5722 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
5723 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
5724 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
5725 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
5726 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
5727 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
5728 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
5729 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
5730 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
5731 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
5732 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
5733 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
5734 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
5735 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
5736 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
5737 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
5738 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
5739 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
5740 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
31e31b8a
FB
5741};
5742
b39bc503 5743static const bitmask_transtbl lflag_tbl[] = {
c218b4ed
FB
5744 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
5745 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
5746 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
5747 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
5748 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
5749 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
5750 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
5751 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
5752 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
5753 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
5754 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
5755 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
5756 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
5757 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
5758 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
5759 { TARGET_EXTPROC, TARGET_EXTPROC, EXTPROC, EXTPROC},
31e31b8a
FB
5760};
5761
5762static void target_to_host_termios (void *dst, const void *src)
5763{
5764 struct host_termios *host = dst;
5765 const struct target_termios *target = src;
3b46e624 5766
5fafdf24 5767 host->c_iflag =
31e31b8a 5768 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
5fafdf24 5769 host->c_oflag =
31e31b8a 5770 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
5fafdf24 5771 host->c_cflag =
31e31b8a 5772 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
5fafdf24 5773 host->c_lflag =
31e31b8a
FB
5774 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
5775 host->c_line = target->c_line;
3b46e624 5776
44607123 5777 memset(host->c_cc, 0, sizeof(host->c_cc));
5fafdf24
TS
5778 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
5779 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
3b46e624 5780 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
5fafdf24 5781 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
3b46e624 5782 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
5fafdf24 5783 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
3b46e624 5784 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
5fafdf24 5785 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
3b46e624 5786 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
5fafdf24
TS
5787 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
5788 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
3b46e624
TS
5789 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
5790 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
5791 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
5792 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
5793 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
5fafdf24 5794 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
31e31b8a 5795}
3b46e624 5796
31e31b8a
FB
5797static void host_to_target_termios (void *dst, const void *src)
5798{
5799 struct target_termios *target = dst;
5800 const struct host_termios *host = src;
5801
5fafdf24 5802 target->c_iflag =
31e31b8a 5803 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
5fafdf24 5804 target->c_oflag =
31e31b8a 5805 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
5fafdf24 5806 target->c_cflag =
31e31b8a 5807 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
5fafdf24 5808 target->c_lflag =
31e31b8a
FB
5809 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
5810 target->c_line = host->c_line;
3b46e624 5811
44607123 5812 memset(target->c_cc, 0, sizeof(target->c_cc));
31e31b8a
FB
5813 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
5814 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
5815 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
5816 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
5817 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
5818 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
5819 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
5820 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
5821 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
5822 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
5823 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
5824 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
5825 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
5826 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
5827 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
5828 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
5829 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
5830}
5831
8e853dc7 5832static const StructEntry struct_termios_def = {
31e31b8a
FB
5833 .convert = { host_to_target_termios, target_to_host_termios },
5834 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
5835 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
888468db 5836 .print = print_termios,
31e31b8a
FB
5837};
5838
4b840f96
RH
5839/* If the host does not provide these bits, they may be safely discarded. */
5840#ifndef MAP_SYNC
5841#define MAP_SYNC 0
5842#endif
5843#ifndef MAP_UNINITIALIZED
5844#define MAP_UNINITIALIZED 0
5845#endif
5846
180d4ef3 5847static const bitmask_transtbl mmap_flags_tbl[] = {
541e1690
HD
5848 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
5849 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS,
5850 MAP_ANONYMOUS, MAP_ANONYMOUS },
5851 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN,
5852 MAP_GROWSDOWN, MAP_GROWSDOWN },
5853 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE,
5854 MAP_DENYWRITE, MAP_DENYWRITE },
5855 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE,
5856 MAP_EXECUTABLE, MAP_EXECUTABLE },
5857 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
5858 { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE,
5859 MAP_NORESERVE, MAP_NORESERVE },
5860 { TARGET_MAP_HUGETLB, TARGET_MAP_HUGETLB, MAP_HUGETLB, MAP_HUGETLB },
5861 /* MAP_STACK had been ignored by the kernel for quite some time.
5862 Recognize it for the target insofar as we do not want to pass
5863 it through to the host. */
5864 { TARGET_MAP_STACK, TARGET_MAP_STACK, 0, 0 },
4b840f96
RH
5865 { TARGET_MAP_NONBLOCK, TARGET_MAP_NONBLOCK, MAP_NONBLOCK, MAP_NONBLOCK },
5866 { TARGET_MAP_POPULATE, TARGET_MAP_POPULATE, MAP_POPULATE, MAP_POPULATE },
5867 { TARGET_MAP_FIXED_NOREPLACE, TARGET_MAP_FIXED_NOREPLACE,
5868 MAP_FIXED_NOREPLACE, MAP_FIXED_NOREPLACE },
5869 { TARGET_MAP_UNINITIALIZED, TARGET_MAP_UNINITIALIZED,
5870 MAP_UNINITIALIZED, MAP_UNINITIALIZED },
5286db75
FB
5871};
5872
9ab8d071
RH
5873/*
5874 * Arrange for legacy / undefined architecture specific flags to be
5875 * ignored by mmap handling code.
5876 */
5877#ifndef TARGET_MAP_32BIT
5878#define TARGET_MAP_32BIT 0
5879#endif
5880#ifndef TARGET_MAP_HUGE_2MB
5881#define TARGET_MAP_HUGE_2MB 0
5882#endif
5883#ifndef TARGET_MAP_HUGE_1GB
5884#define TARGET_MAP_HUGE_1GB 0
5885#endif
5886
5887static abi_long do_mmap(abi_ulong addr, abi_ulong len, int prot,
5888 int target_flags, int fd, off_t offset)
5889{
5890 /*
5891 * The historical set of flags that all mmap types implicitly support.
5892 */
5893 enum {
5894 TARGET_LEGACY_MAP_MASK = TARGET_MAP_SHARED
5895 | TARGET_MAP_PRIVATE
5896 | TARGET_MAP_FIXED
5897 | TARGET_MAP_ANONYMOUS
5898 | TARGET_MAP_DENYWRITE
5899 | TARGET_MAP_EXECUTABLE
5900 | TARGET_MAP_UNINITIALIZED
5901 | TARGET_MAP_GROWSDOWN
5902 | TARGET_MAP_LOCKED
5903 | TARGET_MAP_NORESERVE
5904 | TARGET_MAP_POPULATE
5905 | TARGET_MAP_NONBLOCK
5906 | TARGET_MAP_STACK
5907 | TARGET_MAP_HUGETLB
5908 | TARGET_MAP_32BIT
5909 | TARGET_MAP_HUGE_2MB
5910 | TARGET_MAP_HUGE_1GB
5911 };
5912 int host_flags;
5913
5914 switch (target_flags & TARGET_MAP_TYPE) {
5915 case TARGET_MAP_PRIVATE:
5916 host_flags = MAP_PRIVATE;
5917 break;
5918 case TARGET_MAP_SHARED:
5919 host_flags = MAP_SHARED;
5920 break;
5921 case TARGET_MAP_SHARED_VALIDATE:
5922 /*
5923 * MAP_SYNC is only supported for MAP_SHARED_VALIDATE, and is
5924 * therefore omitted from mmap_flags_tbl and TARGET_LEGACY_MAP_MASK.
5925 */
5926 if (target_flags & ~(TARGET_LEGACY_MAP_MASK | TARGET_MAP_SYNC)) {
5927 return -TARGET_EOPNOTSUPP;
5928 }
5929 host_flags = MAP_SHARED_VALIDATE;
5930 if (target_flags & TARGET_MAP_SYNC) {
5931 host_flags |= MAP_SYNC;
5932 }
5933 break;
5934 default:
5935 return -TARGET_EINVAL;
5936 }
5937 host_flags |= target_to_host_bitmask(target_flags, mmap_flags_tbl);
5938
5939 return get_errno(target_mmap(addr, len, prot, host_flags, fd, offset));
5940}
5941
9d12f79d
LV
5942/*
5943 * NOTE: TARGET_ABI32 is defined for TARGET_I386 (but not for TARGET_X86_64)
5944 * TARGET_I386 is defined if TARGET_X86_64 is defined
5945 */
2ab83ea7 5946#if defined(TARGET_I386)
6dbad63e
FB
5947
5948/* NOTE: there is really one LDT for all the threads */
b1d8e52e 5949static uint8_t *ldt_table;
6dbad63e 5950
03acab66 5951static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
6dbad63e
FB
5952{
5953 int size;
53a5960a 5954 void *p;
6dbad63e
FB
5955
5956 if (!ldt_table)
5957 return 0;
5958 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
5959 if (size > bytecount)
5960 size = bytecount;
579a97f7
FB
5961 p = lock_user(VERIFY_WRITE, ptr, size, 0);
5962 if (!p)
03acab66 5963 return -TARGET_EFAULT;
579a97f7 5964 /* ??? Should this by byteswapped? */
53a5960a
PB
5965 memcpy(p, ldt_table, size);
5966 unlock_user(p, ptr, size);
6dbad63e
FB
5967 return size;
5968}
5969
5970/* XXX: add locking support */
03acab66
FB
5971static abi_long write_ldt(CPUX86State *env,
5972 abi_ulong ptr, unsigned long bytecount, int oldmode)
6dbad63e
FB
5973{
5974 struct target_modify_ldt_ldt_s ldt_info;
53a5960a 5975 struct target_modify_ldt_ldt_s *target_ldt_info;
6dbad63e 5976 int seg_32bit, contents, read_exec_only, limit_in_pages;
8d18e893 5977 int seg_not_present, useable, lm;
6dbad63e
FB
5978 uint32_t *lp, entry_1, entry_2;
5979
5980 if (bytecount != sizeof(ldt_info))
03acab66 5981 return -TARGET_EINVAL;
579a97f7 5982 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
03acab66 5983 return -TARGET_EFAULT;
53a5960a 5984 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
cbb21eed 5985 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
53a5960a
PB
5986 ldt_info.limit = tswap32(target_ldt_info->limit);
5987 ldt_info.flags = tswap32(target_ldt_info->flags);
5988 unlock_user_struct(target_ldt_info, ptr, 0);
3b46e624 5989
6dbad63e 5990 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
03acab66 5991 return -TARGET_EINVAL;
6dbad63e
FB
5992 seg_32bit = ldt_info.flags & 1;
5993 contents = (ldt_info.flags >> 1) & 3;
5994 read_exec_only = (ldt_info.flags >> 3) & 1;
5995 limit_in_pages = (ldt_info.flags >> 4) & 1;
5996 seg_not_present = (ldt_info.flags >> 5) & 1;
5997 useable = (ldt_info.flags >> 6) & 1;
8d18e893
FB
5998#ifdef TARGET_ABI32
5999 lm = 0;
6000#else
6001 lm = (ldt_info.flags >> 7) & 1;
6002#endif
6dbad63e
FB
6003 if (contents == 3) {
6004 if (oldmode)
03acab66 6005 return -TARGET_EINVAL;
6dbad63e 6006 if (seg_not_present == 0)
03acab66 6007 return -TARGET_EINVAL;
6dbad63e
FB
6008 }
6009 /* allocate the LDT */
6010 if (!ldt_table) {
e441570f
AZ
6011 env->ldt.base = target_mmap(0,
6012 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
6013 PROT_READ|PROT_WRITE,
6014 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
6015 if (env->ldt.base == -1)
03acab66 6016 return -TARGET_ENOMEM;
3e8f1628 6017 memset(g2h_untagged(env->ldt.base), 0,
e441570f 6018 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
6dbad63e 6019 env->ldt.limit = 0xffff;
3e8f1628 6020 ldt_table = g2h_untagged(env->ldt.base);
6dbad63e
FB
6021 }
6022
6023 /* NOTE: same code as Linux kernel */
6024 /* Allow LDTs to be cleared by the user. */
6025 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
6026 if (oldmode ||
6027 (contents == 0 &&
6028 read_exec_only == 1 &&
6029 seg_32bit == 0 &&
6030 limit_in_pages == 0 &&
6031 seg_not_present == 1 &&
6032 useable == 0 )) {
6033 entry_1 = 0;
6034 entry_2 = 0;
6035 goto install;
6036 }
6037 }
3b46e624 6038
6dbad63e
FB
6039 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
6040 (ldt_info.limit & 0x0ffff);
6041 entry_2 = (ldt_info.base_addr & 0xff000000) |
6042 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
6043 (ldt_info.limit & 0xf0000) |
6044 ((read_exec_only ^ 1) << 9) |
6045 (contents << 10) |
6046 ((seg_not_present ^ 1) << 15) |
6047 (seg_32bit << 22) |
6048 (limit_in_pages << 23) |
8d18e893 6049 (lm << 21) |
6dbad63e
FB
6050 0x7000;
6051 if (!oldmode)
6052 entry_2 |= (useable << 20);
14ae3ba7 6053
6dbad63e
FB
6054 /* Install the new entry ... */
6055install:
6056 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
6057 lp[0] = tswap32(entry_1);
6058 lp[1] = tswap32(entry_2);
6059 return 0;
6060}
6061
6062/* specific and weird i386 syscalls */
8fcd3692
BS
6063static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
6064 unsigned long bytecount)
6dbad63e 6065{
03acab66 6066 abi_long ret;
3b46e624 6067
6dbad63e
FB
6068 switch (func) {
6069 case 0:
6070 ret = read_ldt(ptr, bytecount);
6071 break;
6072 case 1:
6073 ret = write_ldt(env, ptr, bytecount, 1);
6074 break;
6075 case 0x11:
6076 ret = write_ldt(env, ptr, bytecount, 0);
6077 break;
03acab66
FB
6078 default:
6079 ret = -TARGET_ENOSYS;
6080 break;
6dbad63e
FB
6081 }
6082 return ret;
6083}
1b6b029e 6084
9d12f79d 6085#if defined(TARGET_ABI32)
bc22eb44 6086abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
8d18e893 6087{
3e8f1628 6088 uint64_t *gdt_table = g2h_untagged(env->gdt.base);
8d18e893
FB
6089 struct target_modify_ldt_ldt_s ldt_info;
6090 struct target_modify_ldt_ldt_s *target_ldt_info;
6091 int seg_32bit, contents, read_exec_only, limit_in_pages;
6092 int seg_not_present, useable, lm;
6093 uint32_t *lp, entry_1, entry_2;
6094 int i;
6095
6096 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
6097 if (!target_ldt_info)
6098 return -TARGET_EFAULT;
6099 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
cbb21eed 6100 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
8d18e893
FB
6101 ldt_info.limit = tswap32(target_ldt_info->limit);
6102 ldt_info.flags = tswap32(target_ldt_info->flags);
6103 if (ldt_info.entry_number == -1) {
6104 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
6105 if (gdt_table[i] == 0) {
6106 ldt_info.entry_number = i;
6107 target_ldt_info->entry_number = tswap32(i);
6108 break;
6109 }
6110 }
6111 }
6112 unlock_user_struct(target_ldt_info, ptr, 1);
6113
6114 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
6115 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
6116 return -TARGET_EINVAL;
6117 seg_32bit = ldt_info.flags & 1;
6118 contents = (ldt_info.flags >> 1) & 3;
6119 read_exec_only = (ldt_info.flags >> 3) & 1;
6120 limit_in_pages = (ldt_info.flags >> 4) & 1;
6121 seg_not_present = (ldt_info.flags >> 5) & 1;
6122 useable = (ldt_info.flags >> 6) & 1;
6123#ifdef TARGET_ABI32
6124 lm = 0;
6125#else
6126 lm = (ldt_info.flags >> 7) & 1;
6127#endif
6128
6129 if (contents == 3) {
6130 if (seg_not_present == 0)
6131 return -TARGET_EINVAL;
6132 }
6133
6134 /* NOTE: same code as Linux kernel */
6135 /* Allow LDTs to be cleared by the user. */
6136 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
6137 if ((contents == 0 &&
6138 read_exec_only == 1 &&
6139 seg_32bit == 0 &&
6140 limit_in_pages == 0 &&
6141 seg_not_present == 1 &&
6142 useable == 0 )) {
6143 entry_1 = 0;
6144 entry_2 = 0;
6145 goto install;
6146 }
6147 }
6148
6149 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
6150 (ldt_info.limit & 0x0ffff);
6151 entry_2 = (ldt_info.base_addr & 0xff000000) |
6152 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
6153 (ldt_info.limit & 0xf0000) |
6154 ((read_exec_only ^ 1) << 9) |
6155 (contents << 10) |
6156 ((seg_not_present ^ 1) << 15) |
6157 (seg_32bit << 22) |
6158 (limit_in_pages << 23) |
6159 (useable << 20) |
6160 (lm << 21) |
6161 0x7000;
6162
6163 /* Install the new entry ... */
6164install:
6165 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
6166 lp[0] = tswap32(entry_1);
6167 lp[1] = tswap32(entry_2);
6168 return 0;
6169}
6170
8fcd3692 6171static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
8d18e893
FB
6172{
6173 struct target_modify_ldt_ldt_s *target_ldt_info;
3e8f1628 6174 uint64_t *gdt_table = g2h_untagged(env->gdt.base);
8d18e893
FB
6175 uint32_t base_addr, limit, flags;
6176 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
6177 int seg_not_present, useable, lm;
6178 uint32_t *lp, entry_1, entry_2;
6179
6180 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
6181 if (!target_ldt_info)
6182 return -TARGET_EFAULT;
6183 idx = tswap32(target_ldt_info->entry_number);
6184 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
6185 idx > TARGET_GDT_ENTRY_TLS_MAX) {
6186 unlock_user_struct(target_ldt_info, ptr, 1);
6187 return -TARGET_EINVAL;
6188 }
6189 lp = (uint32_t *)(gdt_table + idx);
6190 entry_1 = tswap32(lp[0]);
6191 entry_2 = tswap32(lp[1]);
6192
6193 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
6194 contents = (entry_2 >> 10) & 3;
6195 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
6196 seg_32bit = (entry_2 >> 22) & 1;
6197 limit_in_pages = (entry_2 >> 23) & 1;
6198 useable = (entry_2 >> 20) & 1;
6199#ifdef TARGET_ABI32
6200 lm = 0;
6201#else
6202 lm = (entry_2 >> 21) & 1;
6203#endif
6204 flags = (seg_32bit << 0) | (contents << 1) |
6205 (read_exec_only << 3) | (limit_in_pages << 4) |
6206 (seg_not_present << 5) | (useable << 6) | (lm << 7);
6207 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
6208 base_addr = (entry_1 >> 16) |
6209 (entry_2 & 0xff000000) |
6210 ((entry_2 & 0xff) << 16);
cbb21eed 6211 target_ldt_info->base_addr = tswapal(base_addr);
8d18e893
FB
6212 target_ldt_info->limit = tswap32(limit);
6213 target_ldt_info->flags = tswap32(flags);
6214 unlock_user_struct(target_ldt_info, ptr, 1);
6215 return 0;
6216}
6217
9d12f79d
LV
6218abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
6219{
538fabcb 6220 return -TARGET_ENOSYS;
9d12f79d
LV
6221}
6222#else
2667e71c 6223abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
d2fd1af7 6224{
1add8698 6225 abi_long ret = 0;
d2fd1af7
FB
6226 abi_ulong val;
6227 int idx;
1add8698 6228
d2fd1af7
FB
6229 switch(code) {
6230 case TARGET_ARCH_SET_GS:
6231 case TARGET_ARCH_SET_FS:
6232 if (code == TARGET_ARCH_SET_GS)
6233 idx = R_GS;
6234 else
6235 idx = R_FS;
6236 cpu_x86_load_seg(env, idx, 0);
6237 env->segs[idx].base = addr;
6238 break;
6239 case TARGET_ARCH_GET_GS:
6240 case TARGET_ARCH_GET_FS:
6241 if (code == TARGET_ARCH_GET_GS)
6242 idx = R_GS;
6243 else
6244 idx = R_FS;
6245 val = env->segs[idx].base;
6246 if (put_user(val, addr, abi_ulong))
1add8698 6247 ret = -TARGET_EFAULT;
d2fd1af7
FB
6248 break;
6249 default:
6250 ret = -TARGET_EINVAL;
6251 break;
6252 }
1add8698 6253 return ret;
d2fd1af7 6254}
9d12f79d 6255#endif /* defined(TARGET_ABI32 */
2ab83ea7
FB
6256#endif /* defined(TARGET_I386) */
6257
87e9bf23
RH
6258/*
6259 * These constants are generic. Supply any that are missing from the host.
6260 */
6261#ifndef PR_SET_NAME
6262# define PR_SET_NAME 15
6263# define PR_GET_NAME 16
6264#endif
6265#ifndef PR_SET_FP_MODE
6266# define PR_SET_FP_MODE 45
6267# define PR_GET_FP_MODE 46
6268# define PR_FP_MODE_FR (1 << 0)
6269# define PR_FP_MODE_FRE (1 << 1)
6270#endif
6271#ifndef PR_SVE_SET_VL
6272# define PR_SVE_SET_VL 50
6273# define PR_SVE_GET_VL 51
6274# define PR_SVE_VL_LEN_MASK 0xffff
6275# define PR_SVE_VL_INHERIT (1 << 17)
6276#endif
6277#ifndef PR_PAC_RESET_KEYS
6278# define PR_PAC_RESET_KEYS 54
6279# define PR_PAC_APIAKEY (1 << 0)
6280# define PR_PAC_APIBKEY (1 << 1)
6281# define PR_PAC_APDAKEY (1 << 2)
6282# define PR_PAC_APDBKEY (1 << 3)
6283# define PR_PAC_APGAKEY (1 << 4)
6284#endif
6285#ifndef PR_SET_TAGGED_ADDR_CTRL
6286# define PR_SET_TAGGED_ADDR_CTRL 55
6287# define PR_GET_TAGGED_ADDR_CTRL 56
6288# define PR_TAGGED_ADDR_ENABLE (1UL << 0)
6289#endif
6290#ifndef PR_MTE_TCF_SHIFT
6291# define PR_MTE_TCF_SHIFT 1
6292# define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT)
6293# define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT)
6294# define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT)
6295# define PR_MTE_TCF_MASK (3UL << PR_MTE_TCF_SHIFT)
6296# define PR_MTE_TAG_SHIFT 3
6297# define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT)
6298#endif
220717a6
RH
6299#ifndef PR_SET_IO_FLUSHER
6300# define PR_SET_IO_FLUSHER 57
6301# define PR_GET_IO_FLUSHER 58
6302#endif
6303#ifndef PR_SET_SYSCALL_USER_DISPATCH
6304# define PR_SET_SYSCALL_USER_DISPATCH 59
6305#endif
24d87c18
RH
6306#ifndef PR_SME_SET_VL
6307# define PR_SME_SET_VL 63
6308# define PR_SME_GET_VL 64
6309# define PR_SME_VL_LEN_MASK 0xffff
6310# define PR_SME_VL_INHERIT (1 << 17)
6311#endif
87e9bf23
RH
6312
6313#include "target_prctl.h"
6314
6315static abi_long do_prctl_inval0(CPUArchState *env)
6316{
6317 return -TARGET_EINVAL;
6318}
6319
6320static abi_long do_prctl_inval1(CPUArchState *env, abi_long arg2)
6321{
6322 return -TARGET_EINVAL;
6323}
6324
6325#ifndef do_prctl_get_fp_mode
6326#define do_prctl_get_fp_mode do_prctl_inval0
6327#endif
6328#ifndef do_prctl_set_fp_mode
6329#define do_prctl_set_fp_mode do_prctl_inval1
6330#endif
fd72f5d0
RH
6331#ifndef do_prctl_sve_get_vl
6332#define do_prctl_sve_get_vl do_prctl_inval0
87e9bf23 6333#endif
fd72f5d0
RH
6334#ifndef do_prctl_sve_set_vl
6335#define do_prctl_sve_set_vl do_prctl_inval1
87e9bf23
RH
6336#endif
6337#ifndef do_prctl_reset_keys
6338#define do_prctl_reset_keys do_prctl_inval1
6339#endif
6340#ifndef do_prctl_set_tagged_addr_ctrl
6341#define do_prctl_set_tagged_addr_ctrl do_prctl_inval1
6342#endif
6343#ifndef do_prctl_get_tagged_addr_ctrl
6344#define do_prctl_get_tagged_addr_ctrl do_prctl_inval0
6345#endif
6e8dcacd
RH
6346#ifndef do_prctl_get_unalign
6347#define do_prctl_get_unalign do_prctl_inval1
6348#endif
6349#ifndef do_prctl_set_unalign
6350#define do_prctl_set_unalign do_prctl_inval1
6351#endif
24d87c18
RH
6352#ifndef do_prctl_sme_get_vl
6353#define do_prctl_sme_get_vl do_prctl_inval0
6354#endif
6355#ifndef do_prctl_sme_set_vl
6356#define do_prctl_sme_set_vl do_prctl_inval1
6357#endif
87e9bf23
RH
6358
6359static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2,
6360 abi_long arg3, abi_long arg4, abi_long arg5)
6361{
6362 abi_long ret;
6363
6364 switch (option) {
6365 case PR_GET_PDEATHSIG:
6366 {
6367 int deathsig;
6368 ret = get_errno(prctl(PR_GET_PDEATHSIG, &deathsig,
6369 arg3, arg4, arg5));
1edebb36
RH
6370 if (!is_error(ret) &&
6371 put_user_s32(host_to_target_signal(deathsig), arg2)) {
87e9bf23
RH
6372 return -TARGET_EFAULT;
6373 }
6374 return ret;
6375 }
f746c659
RH
6376 case PR_SET_PDEATHSIG:
6377 return get_errno(prctl(PR_SET_PDEATHSIG, target_to_host_signal(arg2),
6378 arg3, arg4, arg5));
87e9bf23
RH
6379 case PR_GET_NAME:
6380 {
6381 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
6382 if (!name) {
6383 return -TARGET_EFAULT;
6384 }
6385 ret = get_errno(prctl(PR_GET_NAME, (uintptr_t)name,
6386 arg3, arg4, arg5));
6387 unlock_user(name, arg2, 16);
6388 return ret;
6389 }
6390 case PR_SET_NAME:
6391 {
6392 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
6393 if (!name) {
6394 return -TARGET_EFAULT;
6395 }
6396 ret = get_errno(prctl(PR_SET_NAME, (uintptr_t)name,
6397 arg3, arg4, arg5));
6398 unlock_user(name, arg2, 0);
6399 return ret;
6400 }
6401 case PR_GET_FP_MODE:
6402 return do_prctl_get_fp_mode(env);
6403 case PR_SET_FP_MODE:
6404 return do_prctl_set_fp_mode(env, arg2);
6405 case PR_SVE_GET_VL:
fd72f5d0 6406 return do_prctl_sve_get_vl(env);
87e9bf23 6407 case PR_SVE_SET_VL:
fd72f5d0 6408 return do_prctl_sve_set_vl(env, arg2);
24d87c18
RH
6409 case PR_SME_GET_VL:
6410 return do_prctl_sme_get_vl(env);
6411 case PR_SME_SET_VL:
6412 return do_prctl_sme_set_vl(env, arg2);
87e9bf23
RH
6413 case PR_PAC_RESET_KEYS:
6414 if (arg3 || arg4 || arg5) {
6415 return -TARGET_EINVAL;
6416 }
6417 return do_prctl_reset_keys(env, arg2);
6418 case PR_SET_TAGGED_ADDR_CTRL:
6419 if (arg3 || arg4 || arg5) {
6420 return -TARGET_EINVAL;
6421 }
6422 return do_prctl_set_tagged_addr_ctrl(env, arg2);
6423 case PR_GET_TAGGED_ADDR_CTRL:
6424 if (arg2 || arg3 || arg4 || arg5) {
6425 return -TARGET_EINVAL;
6426 }
6427 return do_prctl_get_tagged_addr_ctrl(env);
220717a6 6428
6e8dcacd
RH
6429 case PR_GET_UNALIGN:
6430 return do_prctl_get_unalign(env, arg2);
6431 case PR_SET_UNALIGN:
6432 return do_prctl_set_unalign(env, arg2);
6433
4f4e5567
RH
6434 case PR_CAP_AMBIENT:
6435 case PR_CAPBSET_READ:
6436 case PR_CAPBSET_DROP:
220717a6
RH
6437 case PR_GET_DUMPABLE:
6438 case PR_SET_DUMPABLE:
6439 case PR_GET_KEEPCAPS:
6440 case PR_SET_KEEPCAPS:
4f4e5567
RH
6441 case PR_GET_SECUREBITS:
6442 case PR_SET_SECUREBITS:
220717a6
RH
6443 case PR_GET_TIMING:
6444 case PR_SET_TIMING:
6445 case PR_GET_TIMERSLACK:
6446 case PR_SET_TIMERSLACK:
6447 case PR_MCE_KILL:
6448 case PR_MCE_KILL_GET:
6449 case PR_GET_NO_NEW_PRIVS:
6450 case PR_SET_NO_NEW_PRIVS:
6451 case PR_GET_IO_FLUSHER:
6452 case PR_SET_IO_FLUSHER:
6453 /* Some prctl options have no pointer arguments and we can pass on. */
6454 return get_errno(prctl(option, arg2, arg3, arg4, arg5));
6455
6456 case PR_GET_CHILD_SUBREAPER:
6457 case PR_SET_CHILD_SUBREAPER:
6458 case PR_GET_SPECULATION_CTRL:
6459 case PR_SET_SPECULATION_CTRL:
6460 case PR_GET_TID_ADDRESS:
6461 /* TODO */
6462 return -TARGET_EINVAL;
6463
6464 case PR_GET_FPEXC:
6465 case PR_SET_FPEXC:
6466 /* Was used for SPE on PowerPC. */
6467 return -TARGET_EINVAL;
6468
6469 case PR_GET_ENDIAN:
6470 case PR_SET_ENDIAN:
6471 case PR_GET_FPEMU:
6472 case PR_SET_FPEMU:
6473 case PR_SET_MM:
87e9bf23
RH
6474 case PR_GET_SECCOMP:
6475 case PR_SET_SECCOMP:
220717a6
RH
6476 case PR_SET_SYSCALL_USER_DISPATCH:
6477 case PR_GET_THP_DISABLE:
6478 case PR_SET_THP_DISABLE:
6479 case PR_GET_TSC:
6480 case PR_SET_TSC:
220717a6 6481 /* Disable to prevent the target disabling stuff we need. */
87e9bf23 6482 return -TARGET_EINVAL;
220717a6 6483
87e9bf23 6484 default:
220717a6
RH
6485 qemu_log_mask(LOG_UNIMP, "Unsupported prctl: " TARGET_ABI_FMT_ld "\n",
6486 option);
6487 return -TARGET_EINVAL;
87e9bf23
RH
6488 }
6489}
6490
05098a93 6491#define NEW_STACK_SIZE 0x40000
d865bab5 6492
d865bab5
PB
6493
6494static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
6495typedef struct {
9349b4f9 6496 CPUArchState *env;
d865bab5
PB
6497 pthread_mutex_t mutex;
6498 pthread_cond_t cond;
6499 pthread_t thread;
6500 uint32_t tid;
6501 abi_ulong child_tidptr;
6502 abi_ulong parent_tidptr;
6503 sigset_t sigmask;
6504} new_thread_info;
6505
6506static void *clone_func(void *arg)
6507{
6508 new_thread_info *info = arg;
9349b4f9 6509 CPUArchState *env;
0d34282f 6510 CPUState *cpu;
edf8e2af 6511 TaskState *ts;
d865bab5 6512
70903763 6513 rcu_register_thread();
3468b59e 6514 tcg_register_thread();
d865bab5 6515 env = info->env;
29a0af61 6516 cpu = env_cpu(env);
a2247f8e 6517 thread_cpu = cpu;
0429a971 6518 ts = (TaskState *)cpu->opaque;
71ba74f6 6519 info->tid = sys_gettid();
edf8e2af 6520 task_settid(ts);
d865bab5
PB
6521 if (info->child_tidptr)
6522 put_user_u32(info->tid, info->child_tidptr);
6523 if (info->parent_tidptr)
6524 put_user_u32(info->tid, info->parent_tidptr);
5ebdd774 6525 qemu_guest_random_seed_thread_part2(cpu->random_seed);
d865bab5
PB
6526 /* Enable signals. */
6527 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
6528 /* Signal to the parent that we're ready. */
6529 pthread_mutex_lock(&info->mutex);
6530 pthread_cond_broadcast(&info->cond);
6531 pthread_mutex_unlock(&info->mutex);
a4dd3d51 6532 /* Wait until the parent has finished initializing the tls state. */
d865bab5
PB
6533 pthread_mutex_lock(&clone_lock);
6534 pthread_mutex_unlock(&clone_lock);
6535 cpu_loop(env);
6536 /* never exits */
6537 return NULL;
6538}
1b6b029e 6539
0da46a6e
TS
6540/* do_fork() Must return host values and target errnos (unlike most
6541 do_*() functions). */
9349b4f9 6542static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
d865bab5
PB
6543 abi_ulong parent_tidptr, target_ulong newtls,
6544 abi_ulong child_tidptr)
1b6b029e 6545{
29a0af61 6546 CPUState *cpu = env_cpu(env);
1b6b029e 6547 int ret;
5cd4393b 6548 TaskState *ts;
0429a971 6549 CPUState *new_cpu;
9349b4f9 6550 CPUArchState *new_env;
d865bab5 6551 sigset_t sigmask;
3b46e624 6552
5ea2fc84
PM
6553 flags &= ~CLONE_IGNORED_FLAGS;
6554
436d124b
AZ
6555 /* Emulate vfork() with fork() */
6556 if (flags & CLONE_VFORK)
6557 flags &= ~(CLONE_VFORK | CLONE_VM);
6558
1b6b029e 6559 if (flags & CLONE_VM) {
0429a971 6560 TaskState *parent_ts = (TaskState *)cpu->opaque;
d865bab5
PB
6561 new_thread_info info;
6562 pthread_attr_t attr;
24cb36a6 6563
5ea2fc84
PM
6564 if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) ||
6565 (flags & CLONE_INVALID_THREAD_FLAGS)) {
6566 return -TARGET_EINVAL;
6567 }
6568
c78d65e8 6569 ts = g_new0(TaskState, 1);
624f7979 6570 init_task_state(ts);
73a988d9
MF
6571
6572 /* Grab a mutex so that thread setup appears atomic. */
6573 pthread_mutex_lock(&clone_lock);
6574
6cc9d67c
RH
6575 /*
6576 * If this is our first additional thread, we need to ensure we
6577 * generate code for parallel execution and flush old translations.
6578 * Do this now so that the copy gets CF_PARALLEL too.
6579 */
6580 if (!(cpu->tcg_cflags & CF_PARALLEL)) {
6581 cpu->tcg_cflags |= CF_PARALLEL;
6582 tb_flush(cpu);
6583 }
6584
1b6b029e 6585 /* we create a new CPU instance. */
c5be9f08 6586 new_env = cpu_copy(env);
6e68e076 6587 /* Init regs that differ from the parent. */
608999d1 6588 cpu_clone_regs_child(new_env, newsp, flags);
07a6ecf4 6589 cpu_clone_regs_parent(env, flags);
29a0af61 6590 new_cpu = env_cpu(new_env);
0429a971 6591 new_cpu->opaque = ts;
edf8e2af
MW
6592 ts->bprm = parent_ts->bprm;
6593 ts->info = parent_ts->info;
3d3efba0 6594 ts->signal_mask = parent_ts->signal_mask;
d865bab5 6595
7cfbd386 6596 if (flags & CLONE_CHILD_CLEARTID) {
c2764719
PB
6597 ts->child_tidptr = child_tidptr;
6598 }
6599
7cfbd386 6600 if (flags & CLONE_SETTLS) {
d865bab5 6601 cpu_set_tls (new_env, newtls);
7cfbd386 6602 }
d865bab5 6603
d865bab5
PB
6604 memset(&info, 0, sizeof(info));
6605 pthread_mutex_init(&info.mutex, NULL);
6606 pthread_mutex_lock(&info.mutex);
6607 pthread_cond_init(&info.cond, NULL);
6608 info.env = new_env;
7cfbd386 6609 if (flags & CLONE_CHILD_SETTID) {
d865bab5 6610 info.child_tidptr = child_tidptr;
7cfbd386
PM
6611 }
6612 if (flags & CLONE_PARENT_SETTID) {
d865bab5 6613 info.parent_tidptr = parent_tidptr;
7cfbd386 6614 }
d865bab5
PB
6615
6616 ret = pthread_attr_init(&attr);
48e15fc2
NF
6617 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
6618 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
d865bab5
PB
6619 /* It is not safe to deliver signals until the child has finished
6620 initializing, so temporarily block all signals. */
6621 sigfillset(&sigmask);
6622 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
5ebdd774 6623 cpu->random_seed = qemu_guest_random_seed_thread_part1();
d865bab5
PB
6624
6625 ret = pthread_create(&info.thread, &attr, clone_func, &info);
c2764719 6626 /* TODO: Free new CPU state if thread creation failed. */
d865bab5
PB
6627
6628 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
6629 pthread_attr_destroy(&attr);
6630 if (ret == 0) {
6631 /* Wait for the child to initialize. */
6632 pthread_cond_wait(&info.cond, &info.mutex);
6633 ret = info.tid;
d865bab5
PB
6634 } else {
6635 ret = -1;
6636 }
6637 pthread_mutex_unlock(&info.mutex);
6638 pthread_cond_destroy(&info.cond);
6639 pthread_mutex_destroy(&info.mutex);
6640 pthread_mutex_unlock(&clone_lock);
1b6b029e
FB
6641 } else {
6642 /* if no CLONE_VM, we consider it is a fork */
5ea2fc84
PM
6643 if (flags & CLONE_INVALID_FORK_FLAGS) {
6644 return -TARGET_EINVAL;
6645 }
6646
6647 /* We can't support custom termination signals */
6648 if ((flags & CSIGNAL) != TARGET_SIGCHLD) {
93b4eff8
TB
6649 return -TARGET_EINVAL;
6650 }
7d92d34e 6651
895ce8bb
HD
6652#if !defined(__NR_pidfd_open) || !defined(TARGET_NR_pidfd_open)
6653 if (flags & CLONE_PIDFD) {
6654 return -TARGET_EINVAL;
6655 }
6656#endif
6657
6658 /* Can not allow CLONE_PIDFD with CLONE_PARENT_SETTID */
6659 if ((flags & CLONE_PIDFD) && (flags & CLONE_PARENT_SETTID)) {
6660 return -TARGET_EINVAL;
6661 }
6662
7d92d34e 6663 if (block_signals()) {
af254a27 6664 return -QEMU_ERESTARTSYS;
7d92d34e
TB
6665 }
6666
d865bab5 6667 fork_start();
1b6b029e 6668 ret = fork();
d865bab5 6669 if (ret == 0) {
2b1319c8 6670 /* Child Process. */
608999d1 6671 cpu_clone_regs_child(env, newsp, flags);
d865bab5 6672 fork_end(1);
2b1319c8
AJ
6673 /* There is a race condition here. The parent process could
6674 theoretically read the TID in the child process before the child
6675 tid is set. This would require using either ptrace
6676 (not implemented) or having *_tidptr to point at a shared memory
6677 mapping. We can't repeat the spinlock hack used above because
6678 the child process gets its own copy of the lock. */
d865bab5 6679 if (flags & CLONE_CHILD_SETTID)
71ba74f6 6680 put_user_u32(sys_gettid(), child_tidptr);
d865bab5 6681 if (flags & CLONE_PARENT_SETTID)
71ba74f6 6682 put_user_u32(sys_gettid(), parent_tidptr);
0429a971 6683 ts = (TaskState *)cpu->opaque;
d865bab5
PB
6684 if (flags & CLONE_SETTLS)
6685 cpu_set_tls (env, newtls);
c2764719
PB
6686 if (flags & CLONE_CHILD_CLEARTID)
6687 ts->child_tidptr = child_tidptr;
d865bab5 6688 } else {
07a6ecf4 6689 cpu_clone_regs_parent(env, flags);
895ce8bb
HD
6690 if (flags & CLONE_PIDFD) {
6691 int pid_fd = 0;
6692#if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
6693 int pid_child = ret;
6694 pid_fd = pidfd_open(pid_child, 0);
6695 if (pid_fd >= 0) {
6696 fcntl(pid_fd, F_SETFD, fcntl(pid_fd, F_GETFL)
6697 | FD_CLOEXEC);
6698 } else {
6699 pid_fd = 0;
6700 }
6701#endif
6702 put_user_u32(pid_fd, parent_tidptr);
6703 }
d865bab5
PB
6704 fork_end(0);
6705 }
7de0816f 6706 g_assert(!cpu_in_exclusive_context(cpu));
1b6b029e
FB
6707 }
6708 return ret;
6709}
6710
5f106811
APR
6711/* warning : doesn't handle linux specific flags... */
6712static int target_to_host_fcntl_cmd(int cmd)
6713{
4a545761
SB
6714 int ret;
6715
5f106811 6716 switch(cmd) {
4a545761
SB
6717 case TARGET_F_DUPFD:
6718 case TARGET_F_GETFD:
6719 case TARGET_F_SETFD:
6720 case TARGET_F_GETFL:
6721 case TARGET_F_SETFL:
2d92c682
AS
6722 case TARGET_F_OFD_GETLK:
6723 case TARGET_F_OFD_SETLK:
6724 case TARGET_F_OFD_SETLKW:
4a545761
SB
6725 ret = cmd;
6726 break;
6727 case TARGET_F_GETLK:
6728 ret = F_GETLK64;
6729 break;
6730 case TARGET_F_SETLK:
6731 ret = F_SETLK64;
6732 break;
6733 case TARGET_F_SETLKW:
6734 ret = F_SETLKW64;
6735 break;
6736 case TARGET_F_GETOWN:
6737 ret = F_GETOWN;
6738 break;
6739 case TARGET_F_SETOWN:
6740 ret = F_SETOWN;
6741 break;
6742 case TARGET_F_GETSIG:
6743 ret = F_GETSIG;
6744 break;
6745 case TARGET_F_SETSIG:
6746 ret = F_SETSIG;
6747 break;
5f106811 6748#if TARGET_ABI_BITS == 32
4a545761
SB
6749 case TARGET_F_GETLK64:
6750 ret = F_GETLK64;
6751 break;
6752 case TARGET_F_SETLK64:
6753 ret = F_SETLK64;
6754 break;
6755 case TARGET_F_SETLKW64:
6756 ret = F_SETLKW64;
6757 break;
5f106811 6758#endif
4a545761
SB
6759 case TARGET_F_SETLEASE:
6760 ret = F_SETLEASE;
6761 break;
6762 case TARGET_F_GETLEASE:
6763 ret = F_GETLEASE;
6764 break;
fbd5de9b 6765#ifdef F_DUPFD_CLOEXEC
4a545761
SB
6766 case TARGET_F_DUPFD_CLOEXEC:
6767 ret = F_DUPFD_CLOEXEC;
6768 break;
fbd5de9b 6769#endif
4a545761
SB
6770 case TARGET_F_NOTIFY:
6771 ret = F_NOTIFY;
6772 break;
8d5d3004 6773#ifdef F_GETOWN_EX
4a545761
SB
6774 case TARGET_F_GETOWN_EX:
6775 ret = F_GETOWN_EX;
6776 break;
8d5d3004
AS
6777#endif
6778#ifdef F_SETOWN_EX
4a545761
SB
6779 case TARGET_F_SETOWN_EX:
6780 ret = F_SETOWN_EX;
6781 break;
8d5d3004 6782#endif
ddf31aa8 6783#ifdef F_SETPIPE_SZ
4a545761
SB
6784 case TARGET_F_SETPIPE_SZ:
6785 ret = F_SETPIPE_SZ;
6786 break;
6787 case TARGET_F_GETPIPE_SZ:
6788 ret = F_GETPIPE_SZ;
6789 break;
2bb963ff
SCW
6790#endif
6791#ifdef F_ADD_SEALS
6792 case TARGET_F_ADD_SEALS:
6793 ret = F_ADD_SEALS;
6794 break;
6795 case TARGET_F_GET_SEALS:
6796 ret = F_GET_SEALS;
6797 break;
ddf31aa8 6798#endif
4a545761
SB
6799 default:
6800 ret = -TARGET_EINVAL;
6801 break;
6802 }
6803
6804#if defined(__powerpc64__)
6805 /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and
6806 * is not supported by kernel. The glibc fcntl call actually adjusts
6807 * them to 5, 6 and 7 before making the syscall(). Since we make the
6808 * syscall directly, adjust to what is supported by the kernel.
6809 */
6810 if (ret >= F_GETLK64 && ret <= F_SETLKW64) {
6811 ret -= F_GETLK64 - 5;
5f106811 6812 }
4a545761
SB
6813#endif
6814
6815 return ret;
5f106811
APR
6816}
6817
ae68ad9f
LV
6818#define FLOCK_TRANSTBL \
6819 switch (type) { \
6820 TRANSTBL_CONVERT(F_RDLCK); \
6821 TRANSTBL_CONVERT(F_WRLCK); \
6822 TRANSTBL_CONVERT(F_UNLCK); \
ae68ad9f
LV
6823 }
6824
6825static int target_to_host_flock(int type)
6826{
6827#define TRANSTBL_CONVERT(a) case TARGET_##a: return a
6828 FLOCK_TRANSTBL
6829#undef TRANSTBL_CONVERT
6830 return -TARGET_EINVAL;
6831}
6832
6833static int host_to_target_flock(int type)
6834{
6835#define TRANSTBL_CONVERT(a) case a: return TARGET_##a
6836 FLOCK_TRANSTBL
6837#undef TRANSTBL_CONVERT
6838 /* if we don't know how to convert the value coming
6839 * from the host we copy to the target field as-is
6840 */
6841 return type;
6842}
2ba7f730 6843
213d3e9e
PM
6844static inline abi_long copy_from_user_flock(struct flock64 *fl,
6845 abi_ulong target_flock_addr)
7775e9ec 6846{
53a5960a 6847 struct target_flock *target_fl;
ae68ad9f 6848 int l_type;
213d3e9e
PM
6849
6850 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6851 return -TARGET_EFAULT;
6852 }
6853
6854 __get_user(l_type, &target_fl->l_type);
ae68ad9f
LV
6855 l_type = target_to_host_flock(l_type);
6856 if (l_type < 0) {
6857 return l_type;
6858 }
6859 fl->l_type = l_type;
213d3e9e
PM
6860 __get_user(fl->l_whence, &target_fl->l_whence);
6861 __get_user(fl->l_start, &target_fl->l_start);
6862 __get_user(fl->l_len, &target_fl->l_len);
6863 __get_user(fl->l_pid, &target_fl->l_pid);
6864 unlock_user_struct(target_fl, target_flock_addr, 0);
6865 return 0;
6866}
6867
6868static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
6869 const struct flock64 *fl)
6870{
6871 struct target_flock *target_fl;
6872 short l_type;
6873
6874 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6875 return -TARGET_EFAULT;
6876 }
6877
ae68ad9f 6878 l_type = host_to_target_flock(fl->l_type);
213d3e9e
PM
6879 __put_user(l_type, &target_fl->l_type);
6880 __put_user(fl->l_whence, &target_fl->l_whence);
6881 __put_user(fl->l_start, &target_fl->l_start);
6882 __put_user(fl->l_len, &target_fl->l_len);
6883 __put_user(fl->l_pid, &target_fl->l_pid);
6884 unlock_user_struct(target_fl, target_flock_addr, 1);
6885 return 0;
6886}
6887
6888typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr);
6889typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl);
6890
6891#if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
ffe81d43
RH
6892struct target_oabi_flock64 {
6893 abi_short l_type;
6894 abi_short l_whence;
6895 abi_llong l_start;
6896 abi_llong l_len;
6897 abi_int l_pid;
6898} QEMU_PACKED;
6899
7f254c5c 6900static inline abi_long copy_from_user_oabi_flock64(struct flock64 *fl,
213d3e9e
PM
6901 abi_ulong target_flock_addr)
6902{
7f254c5c 6903 struct target_oabi_flock64 *target_fl;
ae68ad9f 6904 int l_type;
213d3e9e
PM
6905
6906 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6907 return -TARGET_EFAULT;
6908 }
6909
6910 __get_user(l_type, &target_fl->l_type);
ae68ad9f
LV
6911 l_type = target_to_host_flock(l_type);
6912 if (l_type < 0) {
6913 return l_type;
6914 }
6915 fl->l_type = l_type;
213d3e9e
PM
6916 __get_user(fl->l_whence, &target_fl->l_whence);
6917 __get_user(fl->l_start, &target_fl->l_start);
6918 __get_user(fl->l_len, &target_fl->l_len);
6919 __get_user(fl->l_pid, &target_fl->l_pid);
6920 unlock_user_struct(target_fl, target_flock_addr, 0);
6921 return 0;
6922}
6923
7f254c5c 6924static inline abi_long copy_to_user_oabi_flock64(abi_ulong target_flock_addr,
213d3e9e
PM
6925 const struct flock64 *fl)
6926{
7f254c5c 6927 struct target_oabi_flock64 *target_fl;
213d3e9e
PM
6928 short l_type;
6929
6930 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6931 return -TARGET_EFAULT;
6932 }
6933
ae68ad9f 6934 l_type = host_to_target_flock(fl->l_type);
213d3e9e
PM
6935 __put_user(l_type, &target_fl->l_type);
6936 __put_user(fl->l_whence, &target_fl->l_whence);
6937 __put_user(fl->l_start, &target_fl->l_start);
6938 __put_user(fl->l_len, &target_fl->l_len);
6939 __put_user(fl->l_pid, &target_fl->l_pid);
6940 unlock_user_struct(target_fl, target_flock_addr, 1);
6941 return 0;
6942}
6943#endif
6944
6945static inline abi_long copy_from_user_flock64(struct flock64 *fl,
6946 abi_ulong target_flock_addr)
6947{
6948 struct target_flock64 *target_fl;
ae68ad9f 6949 int l_type;
213d3e9e
PM
6950
6951 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6952 return -TARGET_EFAULT;
6953 }
6954
6955 __get_user(l_type, &target_fl->l_type);
ae68ad9f
LV
6956 l_type = target_to_host_flock(l_type);
6957 if (l_type < 0) {
6958 return l_type;
6959 }
6960 fl->l_type = l_type;
213d3e9e
PM
6961 __get_user(fl->l_whence, &target_fl->l_whence);
6962 __get_user(fl->l_start, &target_fl->l_start);
6963 __get_user(fl->l_len, &target_fl->l_len);
6964 __get_user(fl->l_pid, &target_fl->l_pid);
6965 unlock_user_struct(target_fl, target_flock_addr, 0);
6966 return 0;
6967}
6968
6969static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr,
6970 const struct flock64 *fl)
6971{
6972 struct target_flock64 *target_fl;
6973 short l_type;
6974
6975 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6976 return -TARGET_EFAULT;
6977 }
6978
ae68ad9f 6979 l_type = host_to_target_flock(fl->l_type);
213d3e9e
PM
6980 __put_user(l_type, &target_fl->l_type);
6981 __put_user(fl->l_whence, &target_fl->l_whence);
6982 __put_user(fl->l_start, &target_fl->l_start);
6983 __put_user(fl->l_len, &target_fl->l_len);
6984 __put_user(fl->l_pid, &target_fl->l_pid);
6985 unlock_user_struct(target_fl, target_flock_addr, 1);
6986 return 0;
6987}
6988
6989static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
6990{
43f238d7 6991 struct flock64 fl64;
8d5d3004
AS
6992#ifdef F_GETOWN_EX
6993 struct f_owner_ex fox;
6994 struct target_f_owner_ex *target_fox;
6995#endif
992f48a0 6996 abi_long ret;
5f106811
APR
6997 int host_cmd = target_to_host_fcntl_cmd(cmd);
6998
6999 if (host_cmd == -TARGET_EINVAL)
7000 return host_cmd;
53a5960a 7001
7775e9ec
FB
7002 switch(cmd) {
7003 case TARGET_F_GETLK:
213d3e9e
PM
7004 ret = copy_from_user_flock(&fl64, arg);
7005 if (ret) {
7006 return ret;
7007 }
435da5e7 7008 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
7775e9ec 7009 if (ret == 0) {
213d3e9e 7010 ret = copy_to_user_flock(arg, &fl64);
7775e9ec
FB
7011 }
7012 break;
3b46e624 7013
7775e9ec
FB
7014 case TARGET_F_SETLK:
7015 case TARGET_F_SETLKW:
213d3e9e
PM
7016 ret = copy_from_user_flock(&fl64, arg);
7017 if (ret) {
7018 return ret;
7019 }
435da5e7 7020 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
7775e9ec 7021 break;
3b46e624 7022
7775e9ec 7023 case TARGET_F_GETLK64:
2d92c682 7024 case TARGET_F_OFD_GETLK:
213d3e9e
PM
7025 ret = copy_from_user_flock64(&fl64, arg);
7026 if (ret) {
7027 return ret;
7028 }
435da5e7 7029 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
43f238d7 7030 if (ret == 0) {
213d3e9e 7031 ret = copy_to_user_flock64(arg, &fl64);
43f238d7 7032 }
9ee1fa2c 7033 break;
7775e9ec
FB
7034 case TARGET_F_SETLK64:
7035 case TARGET_F_SETLKW64:
2d92c682
AS
7036 case TARGET_F_OFD_SETLK:
7037 case TARGET_F_OFD_SETLKW:
213d3e9e
PM
7038 ret = copy_from_user_flock64(&fl64, arg);
7039 if (ret) {
7040 return ret;
7041 }
435da5e7 7042 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
7775e9ec
FB
7043 break;
7044
5f106811 7045 case TARGET_F_GETFL:
435da5e7 7046 ret = get_errno(safe_fcntl(fd, host_cmd, arg));
9ee1fa2c
FB
7047 if (ret >= 0) {
7048 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
e0ddf8ea
HD
7049 /* tell 32-bit guests it uses largefile on 64-bit hosts: */
7050 if (O_LARGEFILE == 0 && HOST_LONG_BITS == 64) {
7051 ret |= TARGET_O_LARGEFILE;
7052 }
9ee1fa2c 7053 }
ffa65c3b
FB
7054 break;
7055
5f106811 7056 case TARGET_F_SETFL:
435da5e7
PM
7057 ret = get_errno(safe_fcntl(fd, host_cmd,
7058 target_to_host_bitmask(arg,
7059 fcntl_flags_tbl)));
5f106811
APR
7060 break;
7061
8d5d3004
AS
7062#ifdef F_GETOWN_EX
7063 case TARGET_F_GETOWN_EX:
435da5e7 7064 ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
8d5d3004
AS
7065 if (ret >= 0) {
7066 if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
7067 return -TARGET_EFAULT;
7068 target_fox->type = tswap32(fox.type);
7069 target_fox->pid = tswap32(fox.pid);
7070 unlock_user_struct(target_fox, arg, 1);
7071 }
7072 break;
7073#endif
7074
7075#ifdef F_SETOWN_EX
7076 case TARGET_F_SETOWN_EX:
7077 if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
7078 return -TARGET_EFAULT;
7079 fox.type = tswap32(target_fox->type);
7080 fox.pid = tswap32(target_fox->pid);
7081 unlock_user_struct(target_fox, arg, 0);
435da5e7 7082 ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
8d5d3004
AS
7083 break;
7084#endif
7085
5f106811 7086 case TARGET_F_SETSIG:
a7b4c9b1
TB
7087 ret = get_errno(safe_fcntl(fd, host_cmd, target_to_host_signal(arg)));
7088 break;
7089
5f106811 7090 case TARGET_F_GETSIG:
a7b4c9b1
TB
7091 ret = host_to_target_signal(get_errno(safe_fcntl(fd, host_cmd, arg)));
7092 break;
7093
7094 case TARGET_F_SETOWN:
7095 case TARGET_F_GETOWN:
7e22e546
UH
7096 case TARGET_F_SETLEASE:
7097 case TARGET_F_GETLEASE:
7e3b92ec
PM
7098 case TARGET_F_SETPIPE_SZ:
7099 case TARGET_F_GETPIPE_SZ:
2bb963ff
SCW
7100 case TARGET_F_ADD_SEALS:
7101 case TARGET_F_GET_SEALS:
435da5e7 7102 ret = get_errno(safe_fcntl(fd, host_cmd, arg));
ffa65c3b
FB
7103 break;
7104
7775e9ec 7105 default:
435da5e7 7106 ret = get_errno(safe_fcntl(fd, cmd, arg));
7775e9ec
FB
7107 break;
7108 }
7109 return ret;
7110}
7111
67867308 7112#ifdef USE_UID16
7775e9ec 7113
67867308
FB
7114static inline int high2lowuid(int uid)
7115{
7116 if (uid > 65535)
7117 return 65534;
7118 else
7119 return uid;
7120}
7121
7122static inline int high2lowgid(int gid)
7123{
7124 if (gid > 65535)
7125 return 65534;
7126 else
7127 return gid;
7128}
7129
7130static inline int low2highuid(int uid)
7131{
7132 if ((int16_t)uid == -1)
7133 return -1;
7134 else
7135 return uid;
7136}
7137
7138static inline int low2highgid(int gid)
7139{
7140 if ((int16_t)gid == -1)
7141 return -1;
7142 else
7143 return gid;
7144}
0c866a7e
RV
7145static inline int tswapid(int id)
7146{
7147 return tswap16(id);
7148}
76ca310a
PM
7149
7150#define put_user_id(x, gaddr) put_user_u16(x, gaddr)
7151
0c866a7e
RV
7152#else /* !USE_UID16 */
7153static inline int high2lowuid(int uid)
7154{
7155 return uid;
7156}
7157static inline int high2lowgid(int gid)
7158{
7159 return gid;
7160}
7161static inline int low2highuid(int uid)
7162{
7163 return uid;
7164}
7165static inline int low2highgid(int gid)
7166{
7167 return gid;
7168}
7169static inline int tswapid(int id)
7170{
7171 return tswap32(id);
7172}
76ca310a
PM
7173
7174#define put_user_id(x, gaddr) put_user_u32(x, gaddr)
7175
67867308 7176#endif /* USE_UID16 */
1b6b029e 7177
fd6f7798
PM
7178/* We must do direct syscalls for setting UID/GID, because we want to
7179 * implement the Linux system call semantics of "change only for this thread",
7180 * not the libc/POSIX semantics of "change for all threads in process".
7181 * (See http://ewontfix.com/17/ for more details.)
7182 * We use the 32-bit version of the syscalls if present; if it is not
7183 * then either the host architecture supports 32-bit UIDs natively with
7184 * the standard syscall, or the 16-bit UID is the best we can do.
7185 */
7186#ifdef __NR_setuid32
7187#define __NR_sys_setuid __NR_setuid32
7188#else
7189#define __NR_sys_setuid __NR_setuid
7190#endif
7191#ifdef __NR_setgid32
7192#define __NR_sys_setgid __NR_setgid32
7193#else
7194#define __NR_sys_setgid __NR_setgid
7195#endif
7196#ifdef __NR_setresuid32
7197#define __NR_sys_setresuid __NR_setresuid32
7198#else
7199#define __NR_sys_setresuid __NR_setresuid
7200#endif
7201#ifdef __NR_setresgid32
7202#define __NR_sys_setresgid __NR_setresgid32
7203#else
7204#define __NR_sys_setresgid __NR_setresgid
7205#endif
7206
7207_syscall1(int, sys_setuid, uid_t, uid)
7208_syscall1(int, sys_setgid, gid_t, gid)
7209_syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
7210_syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
7211
31e31b8a
FB
7212void syscall_init(void)
7213{
2ab83ea7
FB
7214 IOCTLEntry *ie;
7215 const argtype *arg_type;
7216 int size;
7217
8be656b8
AG
7218 thunk_init(STRUCT_MAX);
7219
001faf32 7220#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
5fafdf24 7221#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
31e31b8a
FB
7222#include "syscall_types.h"
7223#undef STRUCT
7224#undef STRUCT_SPECIAL
2ab83ea7
FB
7225
7226 /* we patch the ioctl size if necessary. We rely on the fact that
7227 no ioctl has all the bits at '1' in the size field */
7228 ie = ioctl_entries;
7229 while (ie->target_cmd != 0) {
7230 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
7231 TARGET_IOC_SIZEMASK) {
7232 arg_type = ie->arg_type;
7233 if (arg_type[0] != TYPE_PTR) {
5fafdf24 7234 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
2ab83ea7
FB
7235 ie->target_cmd);
7236 exit(1);
7237 }
7238 arg_type++;
7239 size = thunk_type_size(arg_type, 0);
5fafdf24 7240 ie->target_cmd = (ie->target_cmd &
2ab83ea7
FB
7241 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
7242 (size << TARGET_IOC_SIZESHIFT);
7243 }
b92c47c1 7244
2ab83ea7 7245 /* automatic consistency check if same arch */
872ea0c0
AZ
7246#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
7247 (defined(__x86_64__) && defined(TARGET_X86_64))
7248 if (unlikely(ie->target_cmd != ie->host_cmd)) {
7249 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
7250 ie->name, ie->target_cmd, ie->host_cmd);
2ab83ea7
FB
7251 }
7252#endif
7253 ie++;
7254 }
31e31b8a 7255}
c573ff67 7256
ce4defa0 7257#ifdef TARGET_NR_truncate64
a0939b89 7258static inline abi_long target_truncate64(CPUArchState *cpu_env, const char *arg1,
992f48a0
BS
7259 abi_long arg2,
7260 abi_long arg3,
7261 abi_long arg4)
ce4defa0 7262{
8bf8e9df 7263 if (regpairs_aligned(cpu_env, TARGET_NR_truncate64)) {
ce4defa0
PB
7264 arg2 = arg3;
7265 arg3 = arg4;
48e515d4 7266 }
ce4defa0
PB
7267 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
7268}
7269#endif
7270
7271#ifdef TARGET_NR_ftruncate64
a0939b89 7272static inline abi_long target_ftruncate64(CPUArchState *cpu_env, abi_long arg1,
992f48a0
BS
7273 abi_long arg2,
7274 abi_long arg3,
7275 abi_long arg4)
ce4defa0 7276{
8bf8e9df 7277 if (regpairs_aligned(cpu_env, TARGET_NR_ftruncate64)) {
ce4defa0
PB
7278 arg2 = arg3;
7279 arg3 = arg4;
48e515d4 7280 }
ce4defa0
PB
7281 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
7282}
7283#endif
7284
859e8a89
AF
7285#if defined(TARGET_NR_timer_settime) || \
7286 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
2c86c90f 7287static inline abi_long target_to_host_itimerspec(struct itimerspec *host_its,
f4f1e10a
ECL
7288 abi_ulong target_addr)
7289{
2c86c90f
FB
7290 if (target_to_host_timespec(&host_its->it_interval, target_addr +
7291 offsetof(struct target_itimerspec,
7292 it_interval)) ||
7293 target_to_host_timespec(&host_its->it_value, target_addr +
7294 offsetof(struct target_itimerspec,
7295 it_value))) {
f4f1e10a
ECL
7296 return -TARGET_EFAULT;
7297 }
7298
f4f1e10a
ECL
7299 return 0;
7300}
859e8a89 7301#endif
f4f1e10a 7302
828cb3a1
FB
7303#if defined(TARGET_NR_timer_settime64) || \
7304 (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD))
7305static inline abi_long target_to_host_itimerspec64(struct itimerspec *host_its,
7306 abi_ulong target_addr)
7307{
7308 if (target_to_host_timespec64(&host_its->it_interval, target_addr +
7309 offsetof(struct target__kernel_itimerspec,
7310 it_interval)) ||
7311 target_to_host_timespec64(&host_its->it_value, target_addr +
7312 offsetof(struct target__kernel_itimerspec,
7313 it_value))) {
7314 return -TARGET_EFAULT;
7315 }
7316
7317 return 0;
7318}
7319#endif
7320
859e8a89
AF
7321#if ((defined(TARGET_NR_timerfd_gettime) || \
7322 defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
2c86c90f 7323 defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
f4f1e10a 7324static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
2c86c90f
FB
7325 struct itimerspec *host_its)
7326{
7327 if (host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
7328 it_interval),
7329 &host_its->it_interval) ||
7330 host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
7331 it_value),
7332 &host_its->it_value)) {
f4f1e10a
ECL
7333 return -TARGET_EFAULT;
7334 }
f4f1e10a
ECL
7335 return 0;
7336}
859e8a89 7337#endif
f4f1e10a 7338
828cb3a1
FB
7339#if ((defined(TARGET_NR_timerfd_gettime64) || \
7340 defined(TARGET_NR_timerfd_settime64)) && defined(CONFIG_TIMERFD)) || \
7341 defined(TARGET_NR_timer_gettime64) || defined(TARGET_NR_timer_settime64)
7342static inline abi_long host_to_target_itimerspec64(abi_ulong target_addr,
7343 struct itimerspec *host_its)
7344{
7345 if (host_to_target_timespec64(target_addr +
7346 offsetof(struct target__kernel_itimerspec,
7347 it_interval),
7348 &host_its->it_interval) ||
7349 host_to_target_timespec64(target_addr +
7350 offsetof(struct target__kernel_itimerspec,
7351 it_value),
7352 &host_its->it_value)) {
7353 return -TARGET_EFAULT;
7354 }
7355 return 0;
7356}
7357#endif
7358
859e8a89
AF
7359#if defined(TARGET_NR_adjtimex) || \
7360 (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME))
19f59bce
AM
7361static inline abi_long target_to_host_timex(struct timex *host_tx,
7362 abi_long target_addr)
7363{
7364 struct target_timex *target_tx;
7365
7366 if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
7367 return -TARGET_EFAULT;
7368 }
7369
7370 __get_user(host_tx->modes, &target_tx->modes);
7371 __get_user(host_tx->offset, &target_tx->offset);
7372 __get_user(host_tx->freq, &target_tx->freq);
7373 __get_user(host_tx->maxerror, &target_tx->maxerror);
7374 __get_user(host_tx->esterror, &target_tx->esterror);
7375 __get_user(host_tx->status, &target_tx->status);
7376 __get_user(host_tx->constant, &target_tx->constant);
7377 __get_user(host_tx->precision, &target_tx->precision);
7378 __get_user(host_tx->tolerance, &target_tx->tolerance);
7379 __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
7380 __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
7381 __get_user(host_tx->tick, &target_tx->tick);
7382 __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7383 __get_user(host_tx->jitter, &target_tx->jitter);
7384 __get_user(host_tx->shift, &target_tx->shift);
7385 __get_user(host_tx->stabil, &target_tx->stabil);
7386 __get_user(host_tx->jitcnt, &target_tx->jitcnt);
7387 __get_user(host_tx->calcnt, &target_tx->calcnt);
7388 __get_user(host_tx->errcnt, &target_tx->errcnt);
7389 __get_user(host_tx->stbcnt, &target_tx->stbcnt);
7390 __get_user(host_tx->tai, &target_tx->tai);
7391
7392 unlock_user_struct(target_tx, target_addr, 0);
7393 return 0;
7394}
7395
7396static inline abi_long host_to_target_timex(abi_long target_addr,
7397 struct timex *host_tx)
7398{
7399 struct target_timex *target_tx;
7400
7401 if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
7402 return -TARGET_EFAULT;
7403 }
7404
7405 __put_user(host_tx->modes, &target_tx->modes);
7406 __put_user(host_tx->offset, &target_tx->offset);
7407 __put_user(host_tx->freq, &target_tx->freq);
7408 __put_user(host_tx->maxerror, &target_tx->maxerror);
7409 __put_user(host_tx->esterror, &target_tx->esterror);
7410 __put_user(host_tx->status, &target_tx->status);
7411 __put_user(host_tx->constant, &target_tx->constant);
7412 __put_user(host_tx->precision, &target_tx->precision);
7413 __put_user(host_tx->tolerance, &target_tx->tolerance);
7414 __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
7415 __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
7416 __put_user(host_tx->tick, &target_tx->tick);
7417 __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7418 __put_user(host_tx->jitter, &target_tx->jitter);
7419 __put_user(host_tx->shift, &target_tx->shift);
7420 __put_user(host_tx->stabil, &target_tx->stabil);
7421 __put_user(host_tx->jitcnt, &target_tx->jitcnt);
7422 __put_user(host_tx->calcnt, &target_tx->calcnt);
7423 __put_user(host_tx->errcnt, &target_tx->errcnt);
7424 __put_user(host_tx->stbcnt, &target_tx->stbcnt);
7425 __put_user(host_tx->tai, &target_tx->tai);
7426
7427 unlock_user_struct(target_tx, target_addr, 1);
7428 return 0;
7429}
859e8a89 7430#endif
19f59bce 7431
6ac03b2c
FB
7432
7433#if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
7434static inline abi_long target_to_host_timex64(struct timex *host_tx,
7435 abi_long target_addr)
7436{
7437 struct target__kernel_timex *target_tx;
7438
7439 if (copy_from_user_timeval64(&host_tx->time, target_addr +
7440 offsetof(struct target__kernel_timex,
7441 time))) {
7442 return -TARGET_EFAULT;
7443 }
7444
7445 if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
7446 return -TARGET_EFAULT;
7447 }
7448
7449 __get_user(host_tx->modes, &target_tx->modes);
7450 __get_user(host_tx->offset, &target_tx->offset);
7451 __get_user(host_tx->freq, &target_tx->freq);
7452 __get_user(host_tx->maxerror, &target_tx->maxerror);
7453 __get_user(host_tx->esterror, &target_tx->esterror);
7454 __get_user(host_tx->status, &target_tx->status);
7455 __get_user(host_tx->constant, &target_tx->constant);
7456 __get_user(host_tx->precision, &target_tx->precision);
7457 __get_user(host_tx->tolerance, &target_tx->tolerance);
7458 __get_user(host_tx->tick, &target_tx->tick);
7459 __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7460 __get_user(host_tx->jitter, &target_tx->jitter);
7461 __get_user(host_tx->shift, &target_tx->shift);
7462 __get_user(host_tx->stabil, &target_tx->stabil);
7463 __get_user(host_tx->jitcnt, &target_tx->jitcnt);
7464 __get_user(host_tx->calcnt, &target_tx->calcnt);
7465 __get_user(host_tx->errcnt, &target_tx->errcnt);
7466 __get_user(host_tx->stbcnt, &target_tx->stbcnt);
7467 __get_user(host_tx->tai, &target_tx->tai);
7468
7469 unlock_user_struct(target_tx, target_addr, 0);
7470 return 0;
7471}
7472
7473static inline abi_long host_to_target_timex64(abi_long target_addr,
7474 struct timex *host_tx)
7475{
7476 struct target__kernel_timex *target_tx;
7477
7478 if (copy_to_user_timeval64(target_addr +
7479 offsetof(struct target__kernel_timex, time),
7480 &host_tx->time)) {
7481 return -TARGET_EFAULT;
7482 }
7483
7484 if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
7485 return -TARGET_EFAULT;
7486 }
7487
7488 __put_user(host_tx->modes, &target_tx->modes);
7489 __put_user(host_tx->offset, &target_tx->offset);
7490 __put_user(host_tx->freq, &target_tx->freq);
7491 __put_user(host_tx->maxerror, &target_tx->maxerror);
7492 __put_user(host_tx->esterror, &target_tx->esterror);
7493 __put_user(host_tx->status, &target_tx->status);
7494 __put_user(host_tx->constant, &target_tx->constant);
7495 __put_user(host_tx->precision, &target_tx->precision);
7496 __put_user(host_tx->tolerance, &target_tx->tolerance);
7497 __put_user(host_tx->tick, &target_tx->tick);
7498 __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7499 __put_user(host_tx->jitter, &target_tx->jitter);
7500 __put_user(host_tx->shift, &target_tx->shift);
7501 __put_user(host_tx->stabil, &target_tx->stabil);
7502 __put_user(host_tx->jitcnt, &target_tx->jitcnt);
7503 __put_user(host_tx->calcnt, &target_tx->calcnt);
7504 __put_user(host_tx->errcnt, &target_tx->errcnt);
7505 __put_user(host_tx->stbcnt, &target_tx->stbcnt);
7506 __put_user(host_tx->tai, &target_tx->tai);
7507
7508 unlock_user_struct(target_tx, target_addr, 1);
7509 return 0;
7510}
7511#endif
7512
96ff758c
MF
7513#ifndef HAVE_SIGEV_NOTIFY_THREAD_ID
7514#define sigev_notify_thread_id _sigev_un._tid
7515#endif
7516
c065976f
PM
7517static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
7518 abi_ulong target_addr)
7519{
7520 struct target_sigevent *target_sevp;
7521
7522 if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
7523 return -TARGET_EFAULT;
7524 }
7525
7526 /* This union is awkward on 64 bit systems because it has a 32 bit
7527 * integer and a pointer in it; we follow the conversion approach
7528 * used for handling sigval types in signal.c so the guest should get
7529 * the correct value back even if we did a 64 bit byteswap and it's
7530 * using the 32 bit integer.
7531 */
7532 host_sevp->sigev_value.sival_ptr =
7533 (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
7534 host_sevp->sigev_signo =
7535 target_to_host_signal(tswap32(target_sevp->sigev_signo));
7536 host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
96ff758c 7537 host_sevp->sigev_notify_thread_id = tswap32(target_sevp->_sigev_un._tid);
c065976f
PM
7538
7539 unlock_user_struct(target_sevp, target_addr, 1);
7540 return 0;
7541}
7542
6f6a4032
TM
7543#if defined(TARGET_NR_mlockall)
7544static inline int target_to_host_mlockall_arg(int arg)
7545{
7546 int result = 0;
7547
02e5d7d7 7548 if (arg & TARGET_MCL_CURRENT) {
6f6a4032
TM
7549 result |= MCL_CURRENT;
7550 }
02e5d7d7 7551 if (arg & TARGET_MCL_FUTURE) {
6f6a4032
TM
7552 result |= MCL_FUTURE;
7553 }
02e5d7d7
FB
7554#ifdef MCL_ONFAULT
7555 if (arg & TARGET_MCL_ONFAULT) {
7556 result |= MCL_ONFAULT;
7557 }
7558#endif
7559
6f6a4032
TM
7560 return result;
7561}
7562#endif
7563
fe080593
HD
7564static inline int target_to_host_msync_arg(abi_long arg)
7565{
7566 return ((arg & TARGET_MS_ASYNC) ? MS_ASYNC : 0) |
7567 ((arg & TARGET_MS_INVALIDATE) ? MS_INVALIDATE : 0) |
7568 ((arg & TARGET_MS_SYNC) ? MS_SYNC : 0) |
7569 (arg & ~(TARGET_MS_ASYNC | TARGET_MS_INVALIDATE | TARGET_MS_SYNC));
7570}
7571
4f7f8924
AR
7572#if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \
7573 defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \
7574 defined(TARGET_NR_newfstatat))
a0939b89 7575static inline abi_long host_to_target_stat64(CPUArchState *cpu_env,
6a24a778
AZ
7576 abi_ulong target_addr,
7577 struct stat *host_st)
7578{
09701199 7579#if defined(TARGET_ARM) && defined(TARGET_ABI32)
0effdc29 7580 if (cpu_env->eabi) {
6a24a778
AZ
7581 struct target_eabi_stat64 *target_st;
7582
7583 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
7584 return -TARGET_EFAULT;
7585 memset(target_st, 0, sizeof(struct target_eabi_stat64));
7586 __put_user(host_st->st_dev, &target_st->st_dev);
7587 __put_user(host_st->st_ino, &target_st->st_ino);
7588#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7589 __put_user(host_st->st_ino, &target_st->__st_ino);
7590#endif
7591 __put_user(host_st->st_mode, &target_st->st_mode);
7592 __put_user(host_st->st_nlink, &target_st->st_nlink);
7593 __put_user(host_st->st_uid, &target_st->st_uid);
7594 __put_user(host_st->st_gid, &target_st->st_gid);
7595 __put_user(host_st->st_rdev, &target_st->st_rdev);
7596 __put_user(host_st->st_size, &target_st->st_size);
7597 __put_user(host_st->st_blksize, &target_st->st_blksize);
7598 __put_user(host_st->st_blocks, &target_st->st_blocks);
7599 __put_user(host_st->st_atime, &target_st->target_st_atime);
7600 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
7601 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
febf6fad 7602#ifdef HAVE_STRUCT_STAT_ST_ATIM
5f992db6
CYT
7603 __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
7604 __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
7605 __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
7606#endif
6a24a778
AZ
7607 unlock_user_struct(target_st, target_addr, 1);
7608 } else
7609#endif
7610 {
20d155bc 7611#if defined(TARGET_HAS_STRUCT_STAT64)
6a24a778 7612 struct target_stat64 *target_st;
20d155bc
SW
7613#else
7614 struct target_stat *target_st;
9d33b76b 7615#endif
6a24a778
AZ
7616
7617 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
7618 return -TARGET_EFAULT;
9d33b76b 7619 memset(target_st, 0, sizeof(*target_st));
6a24a778
AZ
7620 __put_user(host_st->st_dev, &target_st->st_dev);
7621 __put_user(host_st->st_ino, &target_st->st_ino);
7622#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7623 __put_user(host_st->st_ino, &target_st->__st_ino);
7624#endif
7625 __put_user(host_st->st_mode, &target_st->st_mode);
7626 __put_user(host_st->st_nlink, &target_st->st_nlink);
7627 __put_user(host_st->st_uid, &target_st->st_uid);
7628 __put_user(host_st->st_gid, &target_st->st_gid);
7629 __put_user(host_st->st_rdev, &target_st->st_rdev);
7630 /* XXX: better use of kernel struct */
7631 __put_user(host_st->st_size, &target_st->st_size);
7632 __put_user(host_st->st_blksize, &target_st->st_blksize);
7633 __put_user(host_st->st_blocks, &target_st->st_blocks);
7634 __put_user(host_st->st_atime, &target_st->target_st_atime);
7635 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
7636 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
febf6fad 7637#ifdef HAVE_STRUCT_STAT_ST_ATIM
5f992db6
CYT
7638 __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
7639 __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
7640 __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
7641#endif
6a24a778
AZ
7642 unlock_user_struct(target_st, target_addr, 1);
7643 }
7644
7645 return 0;
7646}
4f7f8924 7647#endif
6a24a778 7648
efa92184
AR
7649#if defined(TARGET_NR_statx) && defined(__NR_statx)
7650static inline abi_long host_to_target_statx(struct target_statx *host_stx,
7651 abi_ulong target_addr)
7652{
7653 struct target_statx *target_stx;
7654
7655 if (!lock_user_struct(VERIFY_WRITE, target_stx, target_addr, 0)) {
7656 return -TARGET_EFAULT;
7657 }
7658 memset(target_stx, 0, sizeof(*target_stx));
7659
7660 __put_user(host_stx->stx_mask, &target_stx->stx_mask);
7661 __put_user(host_stx->stx_blksize, &target_stx->stx_blksize);
7662 __put_user(host_stx->stx_attributes, &target_stx->stx_attributes);
7663 __put_user(host_stx->stx_nlink, &target_stx->stx_nlink);
7664 __put_user(host_stx->stx_uid, &target_stx->stx_uid);
7665 __put_user(host_stx->stx_gid, &target_stx->stx_gid);
7666 __put_user(host_stx->stx_mode, &target_stx->stx_mode);
7667 __put_user(host_stx->stx_ino, &target_stx->stx_ino);
7668 __put_user(host_stx->stx_size, &target_stx->stx_size);
7669 __put_user(host_stx->stx_blocks, &target_stx->stx_blocks);
7670 __put_user(host_stx->stx_attributes_mask, &target_stx->stx_attributes_mask);
7671 __put_user(host_stx->stx_atime.tv_sec, &target_stx->stx_atime.tv_sec);
7672 __put_user(host_stx->stx_atime.tv_nsec, &target_stx->stx_atime.tv_nsec);
d1e26707
AC
7673 __put_user(host_stx->stx_btime.tv_sec, &target_stx->stx_btime.tv_sec);
7674 __put_user(host_stx->stx_btime.tv_nsec, &target_stx->stx_btime.tv_nsec);
7675 __put_user(host_stx->stx_ctime.tv_sec, &target_stx->stx_ctime.tv_sec);
7676 __put_user(host_stx->stx_ctime.tv_nsec, &target_stx->stx_ctime.tv_nsec);
7677 __put_user(host_stx->stx_mtime.tv_sec, &target_stx->stx_mtime.tv_sec);
7678 __put_user(host_stx->stx_mtime.tv_nsec, &target_stx->stx_mtime.tv_nsec);
efa92184
AR
7679 __put_user(host_stx->stx_rdev_major, &target_stx->stx_rdev_major);
7680 __put_user(host_stx->stx_rdev_minor, &target_stx->stx_rdev_minor);
7681 __put_user(host_stx->stx_dev_major, &target_stx->stx_dev_major);
7682 __put_user(host_stx->stx_dev_minor, &target_stx->stx_dev_minor);
7683
7684 unlock_user_struct(target_stx, target_addr, 1);
7685
7686 return 0;
7687}
7688#endif
7689
14690296
AF
7690static int do_sys_futex(int *uaddr, int op, int val,
7691 const struct timespec *timeout, int *uaddr2,
7692 int val3)
7693{
7694#if HOST_LONG_BITS == 64
7695#if defined(__NR_futex)
7696 /* always a 64-bit time_t, it doesn't define _time64 version */
7697 return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
7698
7699#endif
7700#else /* HOST_LONG_BITS == 64 */
7701#if defined(__NR_futex_time64)
7702 if (sizeof(timeout->tv_sec) == 8) {
7703 /* _time64 function on 32bit arch */
7704 return sys_futex_time64(uaddr, op, val, timeout, uaddr2, val3);
7705 }
7706#endif
7707#if defined(__NR_futex)
7708 /* old function on 32bit arch */
7709 return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
7710#endif
7711#endif /* HOST_LONG_BITS == 64 */
7712 g_assert_not_reached();
7713}
7714
7715static int do_safe_futex(int *uaddr, int op, int val,
7716 const struct timespec *timeout, int *uaddr2,
7717 int val3)
7718{
7719#if HOST_LONG_BITS == 64
7720#if defined(__NR_futex)
7721 /* always a 64-bit time_t, it doesn't define _time64 version */
7722 return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
7723#endif
7724#else /* HOST_LONG_BITS == 64 */
7725#if defined(__NR_futex_time64)
7726 if (sizeof(timeout->tv_sec) == 8) {
7727 /* _time64 function on 32bit arch */
7728 return get_errno(safe_futex_time64(uaddr, op, val, timeout, uaddr2,
7729 val3));
7730 }
7731#endif
7732#if defined(__NR_futex)
7733 /* old function on 32bit arch */
7734 return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
7735#endif
7736#endif /* HOST_LONG_BITS == 64 */
7737 return -TARGET_ENOSYS;
7738}
efa92184 7739
bd0c5661
PB
7740/* ??? Using host futex calls even when target atomic operations
7741 are not really atomic probably breaks things. However implementing
7742 futexes locally would make futexes shared between multiple processes
7743 tricky. However they're probably useless because guest atomic
7744 operations won't work either. */
0fbc0f8d
RH
7745#if defined(TARGET_NR_futex) || defined(TARGET_NR_futex_time64)
7746static int do_futex(CPUState *cpu, bool time64, target_ulong uaddr,
7747 int op, int val, target_ulong timeout,
7748 target_ulong uaddr2, int val3)
bd0c5661 7749{
57b9ccd4
RH
7750 struct timespec ts, *pts = NULL;
7751 void *haddr2 = NULL;
a16aae0c 7752 int base_op;
bd0c5661 7753
57b9ccd4 7754 /* We assume FUTEX_* constants are the same on both host and target. */
a29ccd63 7755#ifdef FUTEX_CMD_MASK
a16aae0c 7756 base_op = op & FUTEX_CMD_MASK;
a29ccd63 7757#else
a16aae0c 7758 base_op = op;
a29ccd63 7759#endif
a16aae0c 7760 switch (base_op) {
bd0c5661 7761 case FUTEX_WAIT:
cce246e0 7762 case FUTEX_WAIT_BITSET:
57b9ccd4
RH
7763 val = tswap32(val);
7764 break;
c72a90df
RH
7765 case FUTEX_WAIT_REQUEUE_PI:
7766 val = tswap32(val);
7767 haddr2 = g2h(cpu, uaddr2);
7768 break;
7769 case FUTEX_LOCK_PI:
7770 case FUTEX_LOCK_PI2:
7771 break;
bd0c5661 7772 case FUTEX_WAKE:
a6180f8a 7773 case FUTEX_WAKE_BITSET:
c72a90df
RH
7774 case FUTEX_TRYLOCK_PI:
7775 case FUTEX_UNLOCK_PI:
57b9ccd4
RH
7776 timeout = 0;
7777 break;
bd0c5661 7778 case FUTEX_FD:
0f946731 7779 val = target_to_host_signal(val);
57b9ccd4
RH
7780 timeout = 0;
7781 break;
bd0c5661 7782 case FUTEX_CMP_REQUEUE:
c72a90df 7783 case FUTEX_CMP_REQUEUE_PI:
57b9ccd4
RH
7784 val3 = tswap32(val3);
7785 /* fall through */
7786 case FUTEX_REQUEUE:
a16aae0c 7787 case FUTEX_WAKE_OP:
57b9ccd4
RH
7788 /*
7789 * For these, the 4th argument is not TIMEOUT, but VAL2.
7790 * But the prototype of do_safe_futex takes a pointer, so
7791 * insert casts to satisfy the compiler. We do not need
7792 * to tswap VAL2 since it's not compared to guest memory.
7793 */
7794 pts = (struct timespec *)(uintptr_t)timeout;
7795 timeout = 0;
7796 haddr2 = g2h(cpu, uaddr2);
7797 break;
bd0c5661
PB
7798 default:
7799 return -TARGET_ENOSYS;
7800 }
57b9ccd4
RH
7801 if (timeout) {
7802 pts = &ts;
7803 if (time64
7804 ? target_to_host_timespec64(pts, timeout)
7805 : target_to_host_timespec(pts, timeout)) {
7806 return -TARGET_EFAULT;
7807 }
7808 }
7809 return do_safe_futex(g2h(cpu, uaddr), op, val, pts, haddr2, val3);
bd0c5661 7810}
859e8a89 7811#endif
14690296 7812
0f0426f3
LV
7813#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7814static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
7815 abi_long handle, abi_long mount_id,
7816 abi_long flags)
7817{
7818 struct file_handle *target_fh;
7819 struct file_handle *fh;
7820 int mid = 0;
7821 abi_long ret;
7822 char *name;
7823 unsigned int size, total_size;
7824
7825 if (get_user_s32(size, handle)) {
7826 return -TARGET_EFAULT;
7827 }
7828
7829 name = lock_user_string(pathname);
7830 if (!name) {
7831 return -TARGET_EFAULT;
7832 }
7833
7834 total_size = sizeof(struct file_handle) + size;
7835 target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
7836 if (!target_fh) {
7837 unlock_user(name, pathname, 0);
7838 return -TARGET_EFAULT;
7839 }
7840
7841 fh = g_malloc0(total_size);
7842 fh->handle_bytes = size;
7843
7844 ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
7845 unlock_user(name, pathname, 0);
7846
7847 /* man name_to_handle_at(2):
7848 * Other than the use of the handle_bytes field, the caller should treat
7849 * the file_handle structure as an opaque data type
7850 */
7851
7852 memcpy(target_fh, fh, total_size);
7853 target_fh->handle_bytes = tswap32(fh->handle_bytes);
7854 target_fh->handle_type = tswap32(fh->handle_type);
7855 g_free(fh);
7856 unlock_user(target_fh, handle, total_size);
7857
7858 if (put_user_s32(mid, mount_id)) {
7859 return -TARGET_EFAULT;
7860 }
7861
7862 return ret;
7863
7864}
7865#endif
7866
7867#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7868static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
7869 abi_long flags)
7870{
7871 struct file_handle *target_fh;
7872 struct file_handle *fh;
7873 unsigned int size, total_size;
7874 abi_long ret;
7875
7876 if (get_user_s32(size, handle)) {
7877 return -TARGET_EFAULT;
7878 }
7879
7880 total_size = sizeof(struct file_handle) + size;
7881 target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
7882 if (!target_fh) {
7883 return -TARGET_EFAULT;
7884 }
7885
e9d49d51 7886 fh = g_memdup(target_fh, total_size);
0f0426f3
LV
7887 fh->handle_bytes = size;
7888 fh->handle_type = tswap32(target_fh->handle_type);
7889
7890 ret = get_errno(open_by_handle_at(mount_fd, fh,
7891 target_to_host_bitmask(flags, fcntl_flags_tbl)));
7892
7893 g_free(fh);
7894
7895 unlock_user(target_fh, handle, total_size);
7896
7897 return ret;
7898}
7899#endif
bd0c5661 7900
e36800c9
LV
7901#if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
7902
e36800c9
LV
7903static abi_long do_signalfd4(int fd, abi_long mask, int flags)
7904{
7905 int host_flags;
7906 target_sigset_t *target_mask;
7907 sigset_t host_mask;
7908 abi_long ret;
7909
78721301 7910 if (flags & ~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC)) {
e36800c9
LV
7911 return -TARGET_EINVAL;
7912 }
7913 if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
7914 return -TARGET_EFAULT;
7915 }
7916
7917 target_to_host_sigset(&host_mask, target_mask);
7918
7919 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
7920
7921 ret = get_errno(signalfd(fd, &host_mask, host_flags));
7922 if (ret >= 0) {
7923 fd_trans_register(ret, &target_signalfd_trans);
7924 }
7925
7926 unlock_user_struct(target_mask, mask, 0);
7927
7928 return ret;
7929}
7930#endif
7931
1d9d8b55
PB
7932/* Map host to target signal numbers for the wait family of syscalls.
7933 Assume all other status bits are the same. */
a05c6409 7934int host_to_target_waitstatus(int status)
1d9d8b55
PB
7935{
7936 if (WIFSIGNALED(status)) {
7937 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
7938 }
7939 if (WIFSTOPPED(status)) {
7940 return (host_to_target_signal(WSTOPSIG(status)) << 8)
7941 | (status & 0xff);
7942 }
7943 return status;
7944}
7945
a0939b89 7946static int open_self_cmdline(CPUArchState *cpu_env, int fd)
76b94245 7947{
0effdc29 7948 CPUState *cpu = env_cpu(cpu_env);
58de8b96
AS
7949 struct linux_binprm *bprm = ((TaskState *)cpu->opaque)->bprm;
7950 int i;
76b94245 7951
58de8b96
AS
7952 for (i = 0; i < bprm->argc; i++) {
7953 size_t len = strlen(bprm->argv[i]) + 1;
76b94245 7954
58de8b96 7955 if (write(fd, bprm->argv[i], len) != len) {
76b94245 7956 return -1;
76b94245
WVS
7957 }
7958 }
7959
58de8b96 7960 return 0;
76b94245
WVS
7961}
7962
7b7a3366
RH
7963struct open_self_maps_data {
7964 TaskState *ts;
7965 IntervalTreeRoot *host_maps;
7966 int fd;
7967 bool smaps;
7968};
7969
7970/*
7971 * Subroutine to output one line of /proc/self/maps,
7972 * or one region of /proc/self/smaps.
7973 */
7974
7975#ifdef TARGET_HPPA
7976# define test_stack(S, E, L) (E == L)
7977#else
7978# define test_stack(S, E, L) (S == L)
7979#endif
7980
7981static void open_self_maps_4(const struct open_self_maps_data *d,
7982 const MapInfo *mi, abi_ptr start,
7983 abi_ptr end, unsigned flags)
77ae5761 7984{
7b7a3366
RH
7985 const struct image_info *info = d->ts->info;
7986 const char *path = mi->path;
7987 uint64_t offset;
7988 int fd = d->fd;
7989 int count;
7990
7991 if (test_stack(start, end, info->stack_limit)) {
7992 path = "[stack]";
92d2a03f
RH
7993 } else if (start == info->brk) {
7994 path = "[heap]";
5d94c2ff
RH
7995 } else if (start == info->vdso) {
7996 path = "[vdso]";
7b7a3366 7997 }
77ae5761 7998
7b7a3366
RH
7999 /* Except null device (MAP_ANON), adjust offset for this fragment. */
8000 offset = mi->offset;
8001 if (mi->dev) {
8002 uintptr_t hstart = (uintptr_t)g2h_untagged(start);
8003 offset += hstart - mi->itree.start;
8004 }
8005
8006 count = dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr
8007 " %c%c%c%c %08" PRIx64 " %02x:%02x %"PRId64,
8008 start, end,
8009 (flags & PAGE_READ) ? 'r' : '-',
8010 (flags & PAGE_WRITE_ORG) ? 'w' : '-',
8011 (flags & PAGE_EXEC) ? 'x' : '-',
8012 mi->is_priv ? 'p' : 's',
8013 offset, major(mi->dev), minor(mi->dev),
8014 (uint64_t)mi->inode);
8015 if (path) {
8016 dprintf(fd, "%*s%s\n", 73 - count, "", path);
8017 } else {
8018 dprintf(fd, "\n");
8019 }
8020
8021 if (d->smaps) {
8022 unsigned long size = end - start;
8023 unsigned long page_size_kb = TARGET_PAGE_SIZE >> 10;
8024 unsigned long size_kb = size >> 10;
77ae5761 8025
7b7a3366 8026 dprintf(fd, "Size: %lu kB\n"
77ae5761
IL
8027 "KernelPageSize: %lu kB\n"
8028 "MMUPageSize: %lu kB\n"
8029 "Rss: 0 kB\n"
8030 "Pss: 0 kB\n"
8031 "Pss_Dirty: 0 kB\n"
8032 "Shared_Clean: 0 kB\n"
8033 "Shared_Dirty: 0 kB\n"
8034 "Private_Clean: 0 kB\n"
8035 "Private_Dirty: 0 kB\n"
8036 "Referenced: 0 kB\n"
6467d9eb 8037 "Anonymous: %lu kB\n"
77ae5761
IL
8038 "LazyFree: 0 kB\n"
8039 "AnonHugePages: 0 kB\n"
8040 "ShmemPmdMapped: 0 kB\n"
8041 "FilePmdMapped: 0 kB\n"
8042 "Shared_Hugetlb: 0 kB\n"
8043 "Private_Hugetlb: 0 kB\n"
8044 "Swap: 0 kB\n"
8045 "SwapPss: 0 kB\n"
8046 "Locked: 0 kB\n"
7b7a3366
RH
8047 "THPeligible: 0\n"
8048 "VmFlags:%s%s%s%s%s%s%s%s\n",
8049 size_kb, page_size_kb, page_size_kb,
6467d9eb 8050 (flags & PAGE_ANON ? size_kb : 0),
7b7a3366
RH
8051 (flags & PAGE_READ) ? " rd" : "",
8052 (flags & PAGE_WRITE_ORG) ? " wr" : "",
8053 (flags & PAGE_EXEC) ? " ex" : "",
8054 mi->is_priv ? "" : " sh",
8055 (flags & PAGE_READ) ? " mr" : "",
8056 (flags & PAGE_WRITE_ORG) ? " mw" : "",
8057 (flags & PAGE_EXEC) ? " me" : "",
8058 mi->is_priv ? "" : " ms");
8059 }
77ae5761
IL
8060}
8061
7b7a3366
RH
8062/*
8063 * Callback for walk_memory_regions, when read_self_maps() fails.
8064 * Proceed without the benefit of host /proc/self/maps cross-check.
8065 */
8066static int open_self_maps_3(void *opaque, target_ulong guest_start,
8067 target_ulong guest_end, unsigned long flags)
36c08d49 8068{
7b7a3366 8069 static const MapInfo mi = { .is_priv = true };
1a49ef2a 8070
7b7a3366
RH
8071 open_self_maps_4(opaque, &mi, guest_start, guest_end, flags);
8072 return 0;
8073}
1a49ef2a 8074
7b7a3366
RH
8075/*
8076 * Callback for walk_memory_regions, when read_self_maps() succeeds.
8077 */
8078static int open_self_maps_2(void *opaque, target_ulong guest_start,
8079 target_ulong guest_end, unsigned long flags)
8080{
8081 const struct open_self_maps_data *d = opaque;
8082 uintptr_t host_start = (uintptr_t)g2h_untagged(guest_start);
8083 uintptr_t host_last = (uintptr_t)g2h_untagged(guest_end - 1);
01ef6b9e 8084
7b7a3366
RH
8085 while (1) {
8086 IntervalTreeNode *n =
8087 interval_tree_iter_first(d->host_maps, host_start, host_start);
8088 MapInfo *mi = container_of(n, MapInfo, itree);
8089 uintptr_t this_hlast = MIN(host_last, n->last);
8090 target_ulong this_gend = h2g(this_hlast) + 1;
01ef6b9e 8091
7b7a3366 8092 open_self_maps_4(d, mi, guest_start, this_gend, flags);
01ef6b9e 8093
7b7a3366
RH
8094 if (this_hlast == host_last) {
8095 return 0;
1a49ef2a 8096 }
7b7a3366
RH
8097 host_start = this_hlast + 1;
8098 guest_start = h2g(host_start);
1a49ef2a 8099 }
7b7a3366 8100}
1a49ef2a 8101
7b7a3366
RH
8102static int open_self_maps_1(CPUArchState *env, int fd, bool smaps)
8103{
8104 struct open_self_maps_data d = {
8105 .ts = env_cpu(env)->opaque,
8106 .host_maps = read_self_maps(),
8107 .fd = fd,
8108 .smaps = smaps
8109 };
01ef6b9e 8110
7b7a3366
RH
8111 if (d.host_maps) {
8112 walk_memory_regions(&d, open_self_maps_2);
8113 free_self_maps(d.host_maps);
8114 } else {
8115 walk_memory_regions(&d, open_self_maps_3);
77ae5761 8116 }
36c08d49
AG
8117 return 0;
8118}
8119
77ae5761
IL
8120static int open_self_maps(CPUArchState *cpu_env, int fd)
8121{
8122 return open_self_maps_1(cpu_env, fd, false);
8123}
8124
8125static int open_self_smaps(CPUArchState *cpu_env, int fd)
8126{
8127 return open_self_maps_1(cpu_env, fd, true);
8128}
8129
a0939b89 8130static int open_self_stat(CPUArchState *cpu_env, int fd)
480b8e7d 8131{
0effdc29 8132 CPUState *cpu = env_cpu(cpu_env);
0429a971 8133 TaskState *ts = cpu->opaque;
7ad4d5a4 8134 g_autoptr(GString) buf = g_string_new(NULL);
480b8e7d
AG
8135 int i;
8136
8137 for (i = 0; i < 44; i++) {
7ad4d5a4
AB
8138 if (i == 0) {
8139 /* pid */
8140 g_string_printf(buf, FMT_pid " ", getpid());
8141 } else if (i == 1) {
8142 /* app name */
8143 gchar *bin = g_strrstr(ts->bprm->argv[0], "/");
8144 bin = bin ? bin + 1 : ts->bprm->argv[0];
8145 g_string_printf(buf, "(%.15s) ", bin);
25bb27c7
AS
8146 } else if (i == 2) {
8147 /* task state */
8148 g_string_assign(buf, "R "); /* we are running right now */
7aa9fe3a
AS
8149 } else if (i == 3) {
8150 /* ppid */
8151 g_string_printf(buf, FMT_pid " ", getppid());
eb33cdae
CE
8152 } else if (i == 21) {
8153 /* starttime */
8154 g_string_printf(buf, "%" PRIu64 " ", ts->start_boottime);
7ad4d5a4
AB
8155 } else if (i == 27) {
8156 /* stack bottom */
8157 g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack);
8158 } else {
8159 /* for the rest, there is MasterCard */
8160 g_string_printf(buf, "0%c", i == 43 ? '\n' : ' ');
8161 }
8162
8163 if (write(fd, buf->str, buf->len) != buf->len) {
8164 return -1;
8165 }
480b8e7d
AG
8166 }
8167
8168 return 0;
8169}
8170
a0939b89 8171static int open_self_auxv(CPUArchState *cpu_env, int fd)
257450ee 8172{
0effdc29 8173 CPUState *cpu = env_cpu(cpu_env);
0429a971 8174 TaskState *ts = cpu->opaque;
257450ee
AG
8175 abi_ulong auxv = ts->info->saved_auxv;
8176 abi_ulong len = ts->info->auxv_len;
8177 char *ptr;
8178
8179 /*
8180 * Auxiliary vector is stored in target process stack.
8181 * read in whole auxv vector and copy it to file
8182 */
8183 ptr = lock_user(VERIFY_READ, auxv, len, 0);
8184 if (ptr != NULL) {
8185 while (len > 0) {
8186 ssize_t r;
8187 r = write(fd, ptr, len);
8188 if (r <= 0) {
8189 break;
8190 }
8191 len -= r;
8192 ptr += r;
8193 }
8194 lseek(fd, 0, SEEK_SET);
8195 unlock_user(ptr, auxv, len);
8196 }
8197
8198 return 0;
8199}
8200
463d8e73
AS
8201static int is_proc_myself(const char *filename, const char *entry)
8202{
8203 if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
8204 filename += strlen("/proc/");
8205 if (!strncmp(filename, "self/", strlen("self/"))) {
8206 filename += strlen("self/");
8207 } else if (*filename >= '1' && *filename <= '9') {
8208 char myself[80];
8209 snprintf(myself, sizeof(myself), "%d/", getpid());
8210 if (!strncmp(filename, myself, strlen(myself))) {
8211 filename += strlen(myself);
8212 } else {
8213 return 0;
8214 }
8215 } else {
8216 return 0;
8217 }
8218 if (!strcmp(filename, entry)) {
8219 return 1;
8220 }
8221 }
8222 return 0;
8223}
8224
bd5ccd61
HD
8225static void excp_dump_file(FILE *logfile, CPUArchState *env,
8226 const char *fmt, int code)
8227{
8228 if (logfile) {
8229 CPUState *cs = env_cpu(env);
8230
8231 fprintf(logfile, fmt, code);
8232 fprintf(logfile, "Failing executable: %s\n", exec_path);
8233 cpu_dump_state(cs, logfile, 0);
8234 open_self_maps(env, fileno(logfile));
8235 }
8236}
8237
8238void target_exception_dump(CPUArchState *env, const char *fmt, int code)
8239{
8240 /* dump to console */
8241 excp_dump_file(stderr, env, fmt, code);
8242
8243 /* dump to log file */
8244 if (qemu_log_separate()) {
8245 FILE *logfile = qemu_log_trylock();
8246
8247 excp_dump_file(logfile, env, fmt, code);
8248 qemu_log_unlock(logfile);
8249 }
8250}
8251
121c8dd6
RH
8252#include "target_proc.h"
8253
ee3eb3a7 8254#if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN || \
121c8dd6
RH
8255 defined(HAVE_ARCH_PROC_CPUINFO) || \
8256 defined(HAVE_ARCH_PROC_HARDWARE)
de6b9933
LV
8257static int is_proc(const char *filename, const char *entry)
8258{
8259 return strcmp(filename, entry) == 0;
8260}
fff69382 8261#endif
de6b9933 8262
ee3eb3a7 8263#if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
a0939b89 8264static int open_net_route(CPUArchState *cpu_env, int fd)
de6b9933
LV
8265{
8266 FILE *fp;
8267 char *line = NULL;
8268 size_t len = 0;
8269 ssize_t read;
8270
8271 fp = fopen("/proc/net/route", "r");
8272 if (fp == NULL) {
a3ca7bb2 8273 return -1;
de6b9933
LV
8274 }
8275
8276 /* read header */
8277
8278 read = getline(&line, &len, fp);
8279 dprintf(fd, "%s", line);
8280
8281 /* read routes */
8282
8283 while ((read = getline(&line, &len, fp)) != -1) {
8284 char iface[16];
8285 uint32_t dest, gw, mask;
8286 unsigned int flags, refcnt, use, metric, mtu, window, irtt;
9d0bd0cd
PM
8287 int fields;
8288
8289 fields = sscanf(line,
8290 "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
8291 iface, &dest, &gw, &flags, &refcnt, &use, &metric,
8292 &mask, &mtu, &window, &irtt);
8293 if (fields != 11) {
8294 continue;
8295 }
de6b9933
LV
8296 dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
8297 iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
8298 metric, tswap32(mask), mtu, window, irtt);
8299 }
8300
8301 free(line);
8302 fclose(fp);
8303
8304 return 0;
8305}
8306#endif
8307
b8002058 8308int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,
35be898e 8309 int flags, mode_t mode, bool safe)
3be14d05 8310{
b8002058
HD
8311 g_autofree char *proc_name = NULL;
8312 const char *pathname;
3be14d05
AG
8313 struct fake_open {
8314 const char *filename;
a0939b89 8315 int (*fill)(CPUArchState *cpu_env, int fd);
de6b9933 8316 int (*cmp)(const char *s1, const char *s2);
3be14d05
AG
8317 };
8318 const struct fake_open *fake_open;
8319 static const struct fake_open fakes[] = {
de6b9933 8320 { "maps", open_self_maps, is_proc_myself },
77ae5761 8321 { "smaps", open_self_smaps, is_proc_myself },
de6b9933
LV
8322 { "stat", open_self_stat, is_proc_myself },
8323 { "auxv", open_self_auxv, is_proc_myself },
76b94245 8324 { "cmdline", open_self_cmdline, is_proc_myself },
ee3eb3a7 8325#if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
de6b9933 8326 { "/proc/net/route", open_net_route, is_proc },
fff69382 8327#endif
121c8dd6 8328#if defined(HAVE_ARCH_PROC_CPUINFO)
fff69382 8329 { "/proc/cpuinfo", open_cpuinfo, is_proc },
4ab6713e 8330#endif
121c8dd6 8331#if defined(HAVE_ARCH_PROC_HARDWARE)
4ab6713e 8332 { "/proc/hardware", open_hardware, is_proc },
de6b9933
LV
8333#endif
8334 { NULL, NULL, NULL }
3be14d05
AG
8335 };
8336
b8002058
HD
8337 /* if this is a file from /proc/ filesystem, expand full name */
8338 proc_name = realpath(fname, NULL);
8339 if (proc_name && strncmp(proc_name, "/proc/", 6) == 0) {
8340 pathname = proc_name;
8341 } else {
8342 pathname = fname;
8343 }
8344
aa07f5ec 8345 if (is_proc_myself(pathname, "exe")) {
35be898e
IL
8346 if (safe) {
8347 return safe_openat(dirfd, exec_path, flags, mode);
8348 } else {
8349 return openat(dirfd, exec_path, flags, mode);
8350 }
aa07f5ec
MO
8351 }
8352
3be14d05 8353 for (fake_open = fakes; fake_open->filename; fake_open++) {
de6b9933 8354 if (fake_open->cmp(pathname, fake_open->filename)) {
3be14d05
AG
8355 break;
8356 }
8357 }
8358
8359 if (fake_open->filename) {
8360 const char *tmpdir;
8361 char filename[PATH_MAX];
8362 int fd, r;
8363
5b63de6b 8364 fd = memfd_create("qemu-open", 0);
3be14d05 8365 if (fd < 0) {
5b63de6b
RM
8366 if (errno != ENOSYS) {
8367 return fd;
8368 }
8369 /* create temporary file to map stat to */
8370 tmpdir = getenv("TMPDIR");
8371 if (!tmpdir)
8372 tmpdir = "/tmp";
8373 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
8374 fd = mkstemp(filename);
8375 if (fd < 0) {
8376 return fd;
8377 }
8378 unlink(filename);
3be14d05 8379 }
3be14d05
AG
8380
8381 if ((r = fake_open->fill(cpu_env, fd))) {
a3ca7bb2 8382 int e = errno;
3be14d05 8383 close(fd);
a3ca7bb2 8384 errno = e;
3be14d05
AG
8385 return r;
8386 }
8387 lseek(fd, 0, SEEK_SET);
8388
8389 return fd;
8390 }
8391
35be898e
IL
8392 if (safe) {
8393 return safe_openat(dirfd, path(pathname), flags, mode);
8394 } else {
8395 return openat(dirfd, path(pathname), flags, mode);
8396 }
3be14d05
AG
8397}
8398
a4dab0a0
IL
8399ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz)
8400{
8401 ssize_t ret;
8402
8403 if (!pathname || !buf) {
8404 errno = EFAULT;
8405 return -1;
8406 }
8407
8408 if (!bufsiz) {
8409 /* Short circuit this for the magic exe check. */
8410 errno = EINVAL;
8411 return -1;
8412 }
8413
8414 if (is_proc_myself((const char *)pathname, "exe")) {
8415 /*
8416 * Don't worry about sign mismatch as earlier mapping
8417 * logic would have thrown a bad address error.
8418 */
8419 ret = MIN(strlen(exec_path), bufsiz);
8420 /* We cannot NUL terminate the string. */
8421 memcpy(buf, exec_path, ret);
8422 } else {
8423 ret = readlink(path(pathname), buf, bufsiz);
8424 }
8425
8426 return ret;
8427}
8428
7a8d9f3a
PB
8429static int do_execv(CPUArchState *cpu_env, int dirfd,
8430 abi_long pathname, abi_long guest_argp,
8431 abi_long guest_envp, int flags, bool is_execveat)
156e1f67
DD
8432{
8433 int ret;
8434 char **argp, **envp;
8435 int argc, envc;
8436 abi_ulong gp;
8437 abi_ulong addr;
8438 char **q;
8439 void *p;
8440
8441 argc = 0;
8442
8443 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
8444 if (get_user_ual(addr, gp)) {
8445 return -TARGET_EFAULT;
8446 }
8447 if (!addr) {
8448 break;
8449 }
8450 argc++;
8451 }
8452 envc = 0;
8453 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
8454 if (get_user_ual(addr, gp)) {
8455 return -TARGET_EFAULT;
8456 }
8457 if (!addr) {
8458 break;
8459 }
8460 envc++;
8461 }
8462
8463 argp = g_new0(char *, argc + 1);
8464 envp = g_new0(char *, envc + 1);
8465
8466 for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
8467 if (get_user_ual(addr, gp)) {
8468 goto execve_efault;
8469 }
8470 if (!addr) {
8471 break;
8472 }
8473 *q = lock_user_string(addr);
8474 if (!*q) {
8475 goto execve_efault;
8476 }
8477 }
8478 *q = NULL;
8479
8480 for (gp = guest_envp, q = envp; gp; gp += sizeof(abi_ulong), q++) {
8481 if (get_user_ual(addr, gp)) {
8482 goto execve_efault;
8483 }
8484 if (!addr) {
8485 break;
8486 }
8487 *q = lock_user_string(addr);
8488 if (!*q) {
8489 goto execve_efault;
8490 }
8491 }
8492 *q = NULL;
8493
8494 /*
8495 * Although execve() is not an interruptible syscall it is
8496 * a special case where we must use the safe_syscall wrapper:
8497 * if we allow a signal to happen before we make the host
8498 * syscall then we will 'lose' it, because at the point of
8499 * execve the process leaves QEMU's control. So we use the
8500 * safe syscall wrapper to ensure that we either take the
8501 * signal as a guest signal, or else it does not happen
8502 * before the execve completes and makes it the other
8503 * program's problem.
8504 */
8505 p = lock_user_string(pathname);
8506 if (!p) {
8507 goto execve_efault;
8508 }
8509
7a8d9f3a 8510 const char *exe = p;
156e1f67 8511 if (is_proc_myself(p, "exe")) {
7a8d9f3a 8512 exe = exec_path;
156e1f67 8513 }
7a8d9f3a
PB
8514 ret = is_execveat
8515 ? safe_execveat(dirfd, exe, argp, envp, flags)
8516 : safe_execve(exe, argp, envp);
8517 ret = get_errno(ret);
156e1f67
DD
8518
8519 unlock_user(p, pathname, 0);
8520
8521 goto execve_end;
8522
8523execve_efault:
8524 ret = -TARGET_EFAULT;
8525
8526execve_end:
8527 for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
8528 if (get_user_ual(addr, gp) || !addr) {
8529 break;
8530 }
8531 unlock_user(*q, addr, 0);
8532 }
8533 for (gp = guest_envp, q = envp; *q; gp += sizeof(abi_ulong), q++) {
8534 if (get_user_ual(addr, gp) || !addr) {
8535 break;
8536 }
8537 unlock_user(*q, addr, 0);
8538 }
8539
8540 g_free(argp);
8541 g_free(envp);
8542 return ret;
8543}
8544
aecc8861
AG
8545#define TIMER_MAGIC 0x0caf0000
8546#define TIMER_MAGIC_MASK 0xffff0000
8547
8548/* Convert QEMU provided timer ID back to internal 16bit index format */
8549static target_timer_t get_timer_id(abi_long arg)
8550{
8551 target_timer_t timerid = arg;
8552
8553 if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
8554 return -TARGET_EINVAL;
8555 }
8556
8557 timerid &= 0xffff;
8558
8559 if (timerid >= ARRAY_SIZE(g_posix_timers)) {
8560 return -TARGET_EINVAL;
8561 }
8562
8563 return timerid;
8564}
8565
2e0a8713
ST
8566static int target_to_host_cpu_mask(unsigned long *host_mask,
8567 size_t host_size,
8568 abi_ulong target_addr,
8569 size_t target_size)
8570{
8571 unsigned target_bits = sizeof(abi_ulong) * 8;
8572 unsigned host_bits = sizeof(*host_mask) * 8;
8573 abi_ulong *target_mask;
8574 unsigned i, j;
8575
8576 assert(host_size >= target_size);
8577
8578 target_mask = lock_user(VERIFY_READ, target_addr, target_size, 1);
8579 if (!target_mask) {
8580 return -TARGET_EFAULT;
8581 }
8582 memset(host_mask, 0, host_size);
8583
8584 for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
8585 unsigned bit = i * target_bits;
8586 abi_ulong val;
8587
8588 __get_user(val, &target_mask[i]);
8589 for (j = 0; j < target_bits; j++, bit++) {
8590 if (val & (1UL << j)) {
8591 host_mask[bit / host_bits] |= 1UL << (bit % host_bits);
8592 }
8593 }
8594 }
8595
8596 unlock_user(target_mask, target_addr, 0);
8597 return 0;
8598}
8599
8600static int host_to_target_cpu_mask(const unsigned long *host_mask,
8601 size_t host_size,
8602 abi_ulong target_addr,
8603 size_t target_size)
8604{
8605 unsigned target_bits = sizeof(abi_ulong) * 8;
8606 unsigned host_bits = sizeof(*host_mask) * 8;
8607 abi_ulong *target_mask;
8608 unsigned i, j;
8609
8610 assert(host_size >= target_size);
8611
8612 target_mask = lock_user(VERIFY_WRITE, target_addr, target_size, 0);
8613 if (!target_mask) {
8614 return -TARGET_EFAULT;
8615 }
8616
8617 for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
8618 unsigned bit = i * target_bits;
8619 abi_ulong val = 0;
8620
8621 for (j = 0; j < target_bits; j++, bit++) {
8622 if (host_mask[bit / host_bits] & (1UL << (bit % host_bits))) {
8623 val |= 1UL << j;
8624 }
8625 }
8626 __put_user(val, &target_mask[i]);
8627 }
8628
8629 unlock_user(target_mask, target_addr, target_size);
8630 return 0;
8631}
8632
fd08ddb9 8633#ifdef TARGET_NR_getdents
aee14c77 8634static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count)
fd08ddb9 8635{
aee14c77
RH
8636 g_autofree void *hdirp = NULL;
8637 void *tdirp;
8638 int hlen, hoff, toff;
8639 int hreclen, treclen;
8640 off64_t prev_diroff = 0;
8641
8642 hdirp = g_try_malloc(count);
8643 if (!hdirp) {
8644 return -TARGET_ENOMEM;
8645 }
fd08ddb9
RH
8646
8647#ifdef EMULATE_GETDENTS_WITH_GETDENTS
aee14c77
RH
8648 hlen = sys_getdents(dirfd, hdirp, count);
8649#else
8650 hlen = sys_getdents64(dirfd, hdirp, count);
8651#endif
fd08ddb9 8652
aee14c77
RH
8653 hlen = get_errno(hlen);
8654 if (is_error(hlen)) {
8655 return hlen;
fd08ddb9
RH
8656 }
8657
aee14c77
RH
8658 tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
8659 if (!tdirp) {
fd08ddb9
RH
8660 return -TARGET_EFAULT;
8661 }
aee14c77
RH
8662
8663 for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
8664#ifdef EMULATE_GETDENTS_WITH_GETDENTS
8665 struct linux_dirent *hde = hdirp + hoff;
fd08ddb9 8666#else
aee14c77
RH
8667 struct linux_dirent64 *hde = hdirp + hoff;
8668#endif
8669 struct target_dirent *tde = tdirp + toff;
8670 int namelen;
8671 uint8_t type;
fd08ddb9 8672
aee14c77
RH
8673 namelen = strlen(hde->d_name);
8674 hreclen = hde->d_reclen;
8675 treclen = offsetof(struct target_dirent, d_name) + namelen + 2;
8676 treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent));
fd08ddb9 8677
aee14c77 8678 if (toff + treclen > count) {
fd08ddb9 8679 /*
aee14c77
RH
8680 * If the host struct is smaller than the target struct, or
8681 * requires less alignment and thus packs into less space,
8682 * then the host can return more entries than we can pass
8683 * on to the guest.
fd08ddb9 8684 */
aee14c77
RH
8685 if (toff == 0) {
8686 toff = -TARGET_EINVAL; /* result buffer is too small */
8687 break;
8688 }
8689 /*
8690 * Return what we have, resetting the file pointer to the
8691 * location of the first record not returned.
8692 */
8693 lseek64(dirfd, prev_diroff, SEEK_SET);
8694 break;
fd08ddb9 8695 }
aee14c77
RH
8696
8697 prev_diroff = hde->d_off;
8698 tde->d_ino = tswapal(hde->d_ino);
8699 tde->d_off = tswapal(hde->d_off);
8700 tde->d_reclen = tswap16(treclen);
8701 memcpy(tde->d_name, hde->d_name, namelen + 1);
8702
8703 /*
8704 * The getdents type is in what was formerly a padding byte at the
8705 * end of the structure.
8706 */
8707#ifdef EMULATE_GETDENTS_WITH_GETDENTS
8708 type = *((uint8_t *)hde + hreclen - 1);
8709#else
8710 type = hde->d_type;
fd08ddb9 8711#endif
aee14c77
RH
8712 *((uint8_t *)tde + treclen - 1) = type;
8713 }
8714
8715 unlock_user(tdirp, arg2, toff);
8716 return toff;
fd08ddb9
RH
8717}
8718#endif /* TARGET_NR_getdents */
8719
8720#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
aee14c77 8721static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count)
fd08ddb9 8722{
aee14c77
RH
8723 g_autofree void *hdirp = NULL;
8724 void *tdirp;
8725 int hlen, hoff, toff;
8726 int hreclen, treclen;
8727 off64_t prev_diroff = 0;
8728
8729 hdirp = g_try_malloc(count);
8730 if (!hdirp) {
8731 return -TARGET_ENOMEM;
8732 }
fd08ddb9 8733
aee14c77
RH
8734 hlen = get_errno(sys_getdents64(dirfd, hdirp, count));
8735 if (is_error(hlen)) {
8736 return hlen;
8737 }
8738
8739 tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
8740 if (!tdirp) {
fd08ddb9
RH
8741 return -TARGET_EFAULT;
8742 }
aee14c77
RH
8743
8744 for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
8745 struct linux_dirent64 *hde = hdirp + hoff;
8746 struct target_dirent64 *tde = tdirp + toff;
8747 int namelen;
8748
8749 namelen = strlen(hde->d_name) + 1;
8750 hreclen = hde->d_reclen;
8751 treclen = offsetof(struct target_dirent64, d_name) + namelen;
8752 treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent64));
8753
8754 if (toff + treclen > count) {
8755 /*
8756 * If the host struct is smaller than the target struct, or
8757 * requires less alignment and thus packs into less space,
8758 * then the host can return more entries than we can pass
8759 * on to the guest.
8760 */
8761 if (toff == 0) {
8762 toff = -TARGET_EINVAL; /* result buffer is too small */
fd08ddb9
RH
8763 break;
8764 }
aee14c77
RH
8765 /*
8766 * Return what we have, resetting the file pointer to the
8767 * location of the first record not returned.
8768 */
8769 lseek64(dirfd, prev_diroff, SEEK_SET);
8770 break;
fd08ddb9 8771 }
aee14c77
RH
8772
8773 prev_diroff = hde->d_off;
8774 tde->d_ino = tswap64(hde->d_ino);
8775 tde->d_off = tswap64(hde->d_off);
8776 tde->d_reclen = tswap16(treclen);
8777 tde->d_type = hde->d_type;
8778 memcpy(tde->d_name, hde->d_name, namelen);
fd08ddb9 8779 }
aee14c77
RH
8780
8781 unlock_user(tdirp, arg2, toff);
8782 return toff;
fd08ddb9
RH
8783}
8784#endif /* TARGET_NR_getdents64 */
8785
9e1c7d98
RE
8786#if defined(TARGET_NR_riscv_hwprobe)
8787
8788#define RISCV_HWPROBE_KEY_MVENDORID 0
8789#define RISCV_HWPROBE_KEY_MARCHID 1
8790#define RISCV_HWPROBE_KEY_MIMPID 2
8791
8792#define RISCV_HWPROBE_KEY_BASE_BEHAVIOR 3
8793#define RISCV_HWPROBE_BASE_BEHAVIOR_IMA (1 << 0)
8794
8795#define RISCV_HWPROBE_KEY_IMA_EXT_0 4
8796#define RISCV_HWPROBE_IMA_FD (1 << 0)
8797#define RISCV_HWPROBE_IMA_C (1 << 1)
bb0a45e9
RE
8798#define RISCV_HWPROBE_IMA_V (1 << 2)
8799#define RISCV_HWPROBE_EXT_ZBA (1 << 3)
8800#define RISCV_HWPROBE_EXT_ZBB (1 << 4)
8801#define RISCV_HWPROBE_EXT_ZBS (1 << 5)
9e1c7d98
RE
8802
8803#define RISCV_HWPROBE_KEY_CPUPERF_0 5
8804#define RISCV_HWPROBE_MISALIGNED_UNKNOWN (0 << 0)
8805#define RISCV_HWPROBE_MISALIGNED_EMULATED (1 << 0)
8806#define RISCV_HWPROBE_MISALIGNED_SLOW (2 << 0)
8807#define RISCV_HWPROBE_MISALIGNED_FAST (3 << 0)
8808#define RISCV_HWPROBE_MISALIGNED_UNSUPPORTED (4 << 0)
8809#define RISCV_HWPROBE_MISALIGNED_MASK (7 << 0)
8810
301c65f4
PD
8811#define RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE 6
8812
9e1c7d98
RE
8813struct riscv_hwprobe {
8814 abi_llong key;
8815 abi_ullong value;
8816};
8817
8818static void risc_hwprobe_fill_pairs(CPURISCVState *env,
8819 struct riscv_hwprobe *pair,
8820 size_t pair_count)
8821{
8822 const RISCVCPUConfig *cfg = riscv_cpu_cfg(env);
8823
8824 for (; pair_count > 0; pair_count--, pair++) {
8825 abi_llong key;
8826 abi_ullong value;
8827 __put_user(0, &pair->value);
8828 __get_user(key, &pair->key);
8829 switch (key) {
8830 case RISCV_HWPROBE_KEY_MVENDORID:
8831 __put_user(cfg->mvendorid, &pair->value);
8832 break;
8833 case RISCV_HWPROBE_KEY_MARCHID:
8834 __put_user(cfg->marchid, &pair->value);
8835 break;
8836 case RISCV_HWPROBE_KEY_MIMPID:
8837 __put_user(cfg->mimpid, &pair->value);
8838 break;
8839 case RISCV_HWPROBE_KEY_BASE_BEHAVIOR:
8840 value = riscv_has_ext(env, RVI) &&
8841 riscv_has_ext(env, RVM) &&
8842 riscv_has_ext(env, RVA) ?
8843 RISCV_HWPROBE_BASE_BEHAVIOR_IMA : 0;
8844 __put_user(value, &pair->value);
8845 break;
8846 case RISCV_HWPROBE_KEY_IMA_EXT_0:
8847 value = riscv_has_ext(env, RVF) &&
8848 riscv_has_ext(env, RVD) ?
8849 RISCV_HWPROBE_IMA_FD : 0;
8850 value |= riscv_has_ext(env, RVC) ?
bb0a45e9
RE
8851 RISCV_HWPROBE_IMA_C : 0;
8852 value |= riscv_has_ext(env, RVV) ?
8853 RISCV_HWPROBE_IMA_V : 0;
8854 value |= cfg->ext_zba ?
8855 RISCV_HWPROBE_EXT_ZBA : 0;
8856 value |= cfg->ext_zbb ?
8857 RISCV_HWPROBE_EXT_ZBB : 0;
8858 value |= cfg->ext_zbs ?
8859 RISCV_HWPROBE_EXT_ZBS : 0;
9e1c7d98
RE
8860 __put_user(value, &pair->value);
8861 break;
8862 case RISCV_HWPROBE_KEY_CPUPERF_0:
8863 __put_user(RISCV_HWPROBE_MISALIGNED_FAST, &pair->value);
8864 break;
301c65f4
PD
8865 case RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE:
8866 value = cfg->ext_zicboz ? cfg->cboz_blocksize : 0;
8867 __put_user(value, &pair->value);
8868 break;
9e1c7d98
RE
8869 default:
8870 __put_user(-1, &pair->key);
8871 break;
8872 }
8873 }
8874}
8875
8876static int cpu_set_valid(abi_long arg3, abi_long arg4)
8877{
8878 int ret, i, tmp;
8879 size_t host_mask_size, target_mask_size;
8880 unsigned long *host_mask;
8881
8882 /*
8883 * cpu_set_t represent CPU masks as bit masks of type unsigned long *.
8884 * arg3 contains the cpu count.
8885 */
8886 tmp = (8 * sizeof(abi_ulong));
8887 target_mask_size = ((arg3 + tmp - 1) / tmp) * sizeof(abi_ulong);
8888 host_mask_size = (target_mask_size + (sizeof(*host_mask) - 1)) &
8889 ~(sizeof(*host_mask) - 1);
8890
8891 host_mask = alloca(host_mask_size);
8892
8893 ret = target_to_host_cpu_mask(host_mask, host_mask_size,
8894 arg4, target_mask_size);
8895 if (ret != 0) {
8896 return ret;
8897 }
8898
8899 for (i = 0 ; i < host_mask_size / sizeof(*host_mask); i++) {
8900 if (host_mask[i] != 0) {
8901 return 0;
8902 }
8903 }
8904 return -TARGET_EINVAL;
8905}
8906
8907static abi_long do_riscv_hwprobe(CPUArchState *cpu_env, abi_long arg1,
8908 abi_long arg2, abi_long arg3,
8909 abi_long arg4, abi_long arg5)
8910{
8911 int ret;
8912 struct riscv_hwprobe *host_pairs;
8913
8914 /* flags must be 0 */
8915 if (arg5 != 0) {
8916 return -TARGET_EINVAL;
8917 }
8918
8919 /* check cpu_set */
8920 if (arg3 != 0) {
8921 ret = cpu_set_valid(arg3, arg4);
8922 if (ret != 0) {
8923 return ret;
8924 }
8925 } else if (arg4 != 0) {
8926 return -TARGET_EINVAL;
8927 }
8928
8929 /* no pairs */
8930 if (arg2 == 0) {
8931 return 0;
8932 }
8933
8934 host_pairs = lock_user(VERIFY_WRITE, arg1,
8935 sizeof(*host_pairs) * (size_t)arg2, 0);
8936 if (host_pairs == NULL) {
8937 return -TARGET_EFAULT;
8938 }
8939 risc_hwprobe_fill_pairs(cpu_env, host_pairs, arg2);
8940 unlock_user(host_pairs, arg1, sizeof(*host_pairs) * (size_t)arg2);
8941 return 0;
8942}
8943#endif /* TARGET_NR_riscv_hwprobe */
8944
e10fbe8f
YT
8945#if defined(TARGET_NR_pivot_root) && defined(__NR_pivot_root)
8946_syscall2(int, pivot_root, const char *, new_root, const char *, put_old)
8947#endif
8948
7f696cdd
TW
8949#if defined(TARGET_NR_open_tree) && defined(__NR_open_tree)
8950#define __NR_sys_open_tree __NR_open_tree
8951_syscall3(int, sys_open_tree, int, __dfd, const char *, __filename,
8952 unsigned int, __flags)
8953#endif
8954
4b2d2753
TW
8955#if defined(TARGET_NR_move_mount) && defined(__NR_move_mount)
8956#define __NR_sys_move_mount __NR_move_mount
8957_syscall5(int, sys_move_mount, int, __from_dfd, const char *, __from_pathname,
8958 int, __to_dfd, const char *, __to_pathname, unsigned int, flag)
8959#endif
8960
dc1ce18b
RH
8961/* This is an internal helper for do_syscall so that it is easier
8962 * to have a single return point, so that actions, such as logging
8963 * of syscall results, can be performed.
8964 * All errnos that do_syscall() returns must be -TARGET_<errcode>.
8965 */
a0939b89 8966static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
dc1ce18b
RH
8967 abi_long arg2, abi_long arg3, abi_long arg4,
8968 abi_long arg5, abi_long arg6, abi_long arg7,
8969 abi_long arg8)
31e31b8a 8970{
29a0af61 8971 CPUState *cpu = env_cpu(cpu_env);
992f48a0 8972 abi_long ret;
4f7f8924
AR
8973#if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
8974 || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
efa92184
AR
8975 || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
8976 || defined(TARGET_NR_statx)
31e31b8a 8977 struct stat st;
4f7f8924
AR
8978#endif
8979#if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
8980 || defined(TARGET_NR_fstatfs)
56c8f68f 8981 struct statfs stfs;
4f7f8924 8982#endif
53a5960a 8983 void *p;
3b46e624 8984
31e31b8a
FB
8985 switch(num) {
8986 case TARGET_NR_exit:
9b056fcc 8987 /* In old applications this may be used to implement _exit(2).
6f9ff551 8988 However in threaded applications it is used for thread termination,
9b056fcc
AF
8989 and _exit_group is used for application termination.
8990 Do thread termination if we have more then one thread. */
a0995886
TB
8991
8992 if (block_signals()) {
af254a27 8993 return -QEMU_ERESTARTSYS;
a0995886
TB
8994 }
8995
1f81ce90 8996 pthread_mutex_lock(&clone_lock);
dd1f6349 8997
bdc44640 8998 if (CPU_NEXT(first_cpu)) {
1f81ce90 8999 TaskState *ts = cpu->opaque;
9b056fcc 9000
6490d9aa
RH
9001 if (ts->child_tidptr) {
9002 put_user_u32(0, ts->child_tidptr);
9003 do_sys_futex(g2h(cpu, ts->child_tidptr),
9004 FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
9005 }
9006
9007 object_unparent(OBJECT(cpu));
1f81ce90
AB
9008 object_unref(OBJECT(cpu));
9009 /*
9010 * At this point the CPU should be unrealized and removed
9011 * from cpu lists. We can clean-up the rest of the thread
9012 * data without the lock held.
9013 */
dd1f6349 9014
1f81ce90 9015 pthread_mutex_unlock(&clone_lock);
dd1f6349 9016
a2247f8e 9017 thread_cpu = NULL;
9b056fcc 9018 g_free(ts);
70903763 9019 rcu_unregister_thread();
9b056fcc
AF
9020 pthread_exit(NULL);
9021 }
dd1f6349 9022
1f81ce90 9023 pthread_mutex_unlock(&clone_lock);
708b6a64 9024 preexit_cleanup(cpu_env, arg1);
c2764719 9025 _exit(arg1);
72eb7ea8 9026 return 0; /* avoid warning */
31e31b8a 9027 case TARGET_NR_read:
ba584f1d
AS
9028 if (arg2 == 0 && arg3 == 0) {
9029 return get_errno(safe_read(arg1, 0, 0));
72eb7ea8 9030 } else {
38d840e6 9031 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
2852aafd 9032 return -TARGET_EFAULT;
50afd02b 9033 ret = get_errno(safe_read(arg1, p, arg3));
e36800c9 9034 if (ret >= 0 &&
5d4d3665
LV
9035 fd_trans_host_to_target_data(arg1)) {
9036 ret = fd_trans_host_to_target_data(arg1)(p, ret);
e36800c9 9037 }
38d840e6
AJ
9038 unlock_user(p, arg2, ret);
9039 }
72eb7ea8 9040 return ret;
31e31b8a 9041 case TARGET_NR_write:
58cfa6c2
TGJ
9042 if (arg2 == 0 && arg3 == 0) {
9043 return get_errno(safe_write(arg1, 0, 0));
9044 }
579a97f7 9045 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
2852aafd 9046 return -TARGET_EFAULT;
04b9bcf9
LV
9047 if (fd_trans_target_to_host_data(arg1)) {
9048 void *copy = g_malloc(arg3);
9049 memcpy(copy, p, arg3);
9050 ret = fd_trans_target_to_host_data(arg1)(copy, arg3);
9051 if (ret >= 0) {
9052 ret = get_errno(safe_write(arg1, copy, ret));
9053 }
9054 g_free(copy);
9055 } else {
9056 ret = get_errno(safe_write(arg1, p, arg3));
9057 }
53a5960a 9058 unlock_user(p, arg2, 0);
72eb7ea8
RH
9059 return ret;
9060
704eff6c 9061#ifdef TARGET_NR_open
31e31b8a 9062 case TARGET_NR_open:
2f619698 9063 if (!(p = lock_user_string(arg1)))
2852aafd 9064 return -TARGET_EFAULT;
a4dab0a0 9065 ret = get_errno(do_guest_openat(cpu_env, AT_FDCWD, p,
0b2effd7 9066 target_to_host_bitmask(arg2, fcntl_flags_tbl),
35be898e 9067 arg3, true));
e36800c9 9068 fd_trans_unregister(ret);
53a5960a 9069 unlock_user(p, arg1, 0);
72eb7ea8 9070 return ret;
704eff6c 9071#endif
82424832 9072 case TARGET_NR_openat:
579a97f7 9073 if (!(p = lock_user_string(arg2)))
2852aafd 9074 return -TARGET_EFAULT;
a4dab0a0 9075 ret = get_errno(do_guest_openat(cpu_env, arg1, p,
0b2effd7 9076 target_to_host_bitmask(arg3, fcntl_flags_tbl),
35be898e 9077 arg4, true));
e36800c9 9078 fd_trans_unregister(ret);
579a97f7 9079 unlock_user(p, arg2, 0);
72eb7ea8 9080 return ret;
0f0426f3
LV
9081#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
9082 case TARGET_NR_name_to_handle_at:
9083 ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
72eb7ea8 9084 return ret;
0f0426f3
LV
9085#endif
9086#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
9087 case TARGET_NR_open_by_handle_at:
9088 ret = do_open_by_handle_at(arg1, arg2, arg3);
e36800c9 9089 fd_trans_unregister(ret);
72eb7ea8 9090 return ret;
cc054c6f
HD
9091#endif
9092#if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
9093 case TARGET_NR_pidfd_open:
9094 return get_errno(pidfd_open(arg1, arg2));
9095#endif
9096#if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
9097 case TARGET_NR_pidfd_send_signal:
9098 {
46187d70 9099 siginfo_t uinfo, *puinfo;
cc054c6f 9100
46187d70
LV
9101 if (arg3) {
9102 p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
9103 if (!p) {
9104 return -TARGET_EFAULT;
9105 }
9106 target_to_host_siginfo(&uinfo, p);
9107 unlock_user(p, arg3, 0);
9108 puinfo = &uinfo;
9109 } else {
9110 puinfo = NULL;
cc054c6f 9111 }
cc054c6f 9112 ret = get_errno(pidfd_send_signal(arg1, target_to_host_signal(arg2),
46187d70 9113 puinfo, arg4));
cc054c6f
HD
9114 }
9115 return ret;
9116#endif
9117#if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
9118 case TARGET_NR_pidfd_getfd:
9119 return get_errno(pidfd_getfd(arg1, arg2, arg3));
0f0426f3 9120#endif
31e31b8a 9121 case TARGET_NR_close:
e36800c9 9122 fd_trans_unregister(arg1);
72eb7ea8 9123 return get_errno(close(arg1));
af804f39
HD
9124#if defined(__NR_close_range) && defined(TARGET_NR_close_range)
9125 case TARGET_NR_close_range:
9126 ret = get_errno(sys_close_range(arg1, arg2, arg3));
9127 if (ret == 0 && !(arg3 & CLOSE_RANGE_CLOEXEC)) {
9128 abi_long fd, maxfd;
9129 maxfd = MIN(arg2, target_fd_max);
9130 for (fd = arg1; fd < maxfd; fd++) {
9131 fd_trans_unregister(fd);
9132 }
9133 }
9134 return ret;
9135#endif
72eb7ea8 9136
31e31b8a 9137 case TARGET_NR_brk:
72eb7ea8 9138 return do_brk(arg1);
704eff6c 9139#ifdef TARGET_NR_fork
31e31b8a 9140 case TARGET_NR_fork:
72eb7ea8 9141 return get_errno(do_fork(cpu_env, TARGET_SIGCHLD, 0, 0, 0, 0));
704eff6c 9142#endif
e5febef5 9143#ifdef TARGET_NR_waitpid
31e31b8a
FB
9144 case TARGET_NR_waitpid:
9145 {
53a5960a 9146 int status;
4af80a37 9147 ret = get_errno(safe_wait4(arg1, &status, arg3, 0));
5379557b 9148 if (!is_error(ret) && arg2 && ret
1d9d8b55 9149 && put_user_s32(host_to_target_waitstatus(status), arg2))
2852aafd 9150 return -TARGET_EFAULT;
31e31b8a 9151 }
72eb7ea8 9152 return ret;
e5febef5 9153#endif
f0cbb613
PB
9154#ifdef TARGET_NR_waitid
9155 case TARGET_NR_waitid:
9156 {
9157 siginfo_t info;
9158 info.si_pid = 0;
4af80a37 9159 ret = get_errno(safe_waitid(arg1, arg2, &info, arg4, NULL));
f0cbb613 9160 if (!is_error(ret) && arg3 && info.si_pid != 0) {
c227f099 9161 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
2852aafd 9162 return -TARGET_EFAULT;
f0cbb613 9163 host_to_target_siginfo(p, &info);
c227f099 9164 unlock_user(p, arg3, sizeof(target_siginfo_t));
f0cbb613
PB
9165 }
9166 }
72eb7ea8 9167 return ret;
f0cbb613 9168#endif
7a3148a9 9169#ifdef TARGET_NR_creat /* not on alpha */
31e31b8a 9170 case TARGET_NR_creat:
579a97f7 9171 if (!(p = lock_user_string(arg1)))
2852aafd 9172 return -TARGET_EFAULT;
53a5960a 9173 ret = get_errno(creat(p, arg2));
e36800c9 9174 fd_trans_unregister(ret);
53a5960a 9175 unlock_user(p, arg1, 0);
72eb7ea8 9176 return ret;
7a3148a9 9177#endif
704eff6c 9178#ifdef TARGET_NR_link
31e31b8a 9179 case TARGET_NR_link:
53a5960a
PB
9180 {
9181 void * p2;
9182 p = lock_user_string(arg1);
9183 p2 = lock_user_string(arg2);
579a97f7
FB
9184 if (!p || !p2)
9185 ret = -TARGET_EFAULT;
9186 else
9187 ret = get_errno(link(p, p2));
53a5960a
PB
9188 unlock_user(p2, arg2, 0);
9189 unlock_user(p, arg1, 0);
9190 }
72eb7ea8 9191 return ret;
704eff6c 9192#endif
c0d472b1 9193#if defined(TARGET_NR_linkat)
64f0ce4c 9194 case TARGET_NR_linkat:
64f0ce4c
TS
9195 {
9196 void * p2 = NULL;
579a97f7 9197 if (!arg2 || !arg4)
2852aafd 9198 return -TARGET_EFAULT;
64f0ce4c
TS
9199 p = lock_user_string(arg2);
9200 p2 = lock_user_string(arg4);
579a97f7 9201 if (!p || !p2)
0da46a6e 9202 ret = -TARGET_EFAULT;
64f0ce4c 9203 else
c0d472b1 9204 ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
579a97f7
FB
9205 unlock_user(p, arg2, 0);
9206 unlock_user(p2, arg4, 0);
64f0ce4c 9207 }
72eb7ea8 9208 return ret;
64f0ce4c 9209#endif
704eff6c 9210#ifdef TARGET_NR_unlink
31e31b8a 9211 case TARGET_NR_unlink:
579a97f7 9212 if (!(p = lock_user_string(arg1)))
2852aafd 9213 return -TARGET_EFAULT;
53a5960a
PB
9214 ret = get_errno(unlink(p));
9215 unlock_user(p, arg1, 0);
72eb7ea8 9216 return ret;
704eff6c 9217#endif
c0d472b1 9218#if defined(TARGET_NR_unlinkat)
8170f56b 9219 case TARGET_NR_unlinkat:
579a97f7 9220 if (!(p = lock_user_string(arg2)))
2852aafd 9221 return -TARGET_EFAULT;
c0d472b1 9222 ret = get_errno(unlinkat(arg1, p, arg3));
579a97f7 9223 unlock_user(p, arg2, 0);
72eb7ea8 9224 return ret;
b7d35e65 9225#endif
55bbe4d5 9226 case TARGET_NR_execveat:
7a8d9f3a 9227 return do_execv(cpu_env, arg1, arg2, arg3, arg4, arg5, true);
31e31b8a 9228 case TARGET_NR_execve:
7a8d9f3a 9229 return do_execv(cpu_env, AT_FDCWD, arg1, arg2, arg3, 0, false);
31e31b8a 9230 case TARGET_NR_chdir:
579a97f7 9231 if (!(p = lock_user_string(arg1)))
2852aafd 9232 return -TARGET_EFAULT;
53a5960a
PB
9233 ret = get_errno(chdir(p));
9234 unlock_user(p, arg1, 0);
72eb7ea8 9235 return ret;
a315a145 9236#ifdef TARGET_NR_time
31e31b8a
FB
9237 case TARGET_NR_time:
9238 {
53a5960a
PB
9239 time_t host_time;
9240 ret = get_errno(time(&host_time));
2f619698
FB
9241 if (!is_error(ret)
9242 && arg1
9243 && put_user_sal(host_time, arg1))
2852aafd 9244 return -TARGET_EFAULT;
31e31b8a 9245 }
72eb7ea8 9246 return ret;
a315a145 9247#endif
704eff6c 9248#ifdef TARGET_NR_mknod
31e31b8a 9249 case TARGET_NR_mknod:
579a97f7 9250 if (!(p = lock_user_string(arg1)))
2852aafd 9251 return -TARGET_EFAULT;
53a5960a
PB
9252 ret = get_errno(mknod(p, arg2, arg3));
9253 unlock_user(p, arg1, 0);
72eb7ea8 9254 return ret;
704eff6c 9255#endif
c0d472b1 9256#if defined(TARGET_NR_mknodat)
75ac37a0 9257 case TARGET_NR_mknodat:
579a97f7 9258 if (!(p = lock_user_string(arg2)))
2852aafd 9259 return -TARGET_EFAULT;
c0d472b1 9260 ret = get_errno(mknodat(arg1, p, arg3, arg4));
579a97f7 9261 unlock_user(p, arg2, 0);
72eb7ea8 9262 return ret;
75ac37a0 9263#endif
704eff6c 9264#ifdef TARGET_NR_chmod
31e31b8a 9265 case TARGET_NR_chmod:
579a97f7 9266 if (!(p = lock_user_string(arg1)))
2852aafd 9267 return -TARGET_EFAULT;
53a5960a
PB
9268 ret = get_errno(chmod(p, arg2));
9269 unlock_user(p, arg1, 0);
72eb7ea8 9270 return ret;
704eff6c 9271#endif
4f7f8924 9272#ifdef TARGET_NR_lseek
31e31b8a 9273 case TARGET_NR_lseek:
72eb7ea8 9274 return get_errno(lseek(arg1, arg2, arg3));
4f7f8924 9275#endif
9231733a
RH
9276#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
9277 /* Alpha specific */
7a3148a9 9278 case TARGET_NR_getxpid:
0effdc29 9279 cpu_env->ir[IR_A4] = getppid();
72eb7ea8 9280 return get_errno(getpid());
7a3148a9 9281#endif
9231733a
RH
9282#ifdef TARGET_NR_getpid
9283 case TARGET_NR_getpid:
72eb7ea8 9284 return get_errno(getpid());
9231733a 9285#endif
31e31b8a 9286 case TARGET_NR_mount:
356d771b
PB
9287 {
9288 /* need to look at the data field */
9289 void *p2, *p3;
9290
9291 if (arg1) {
9292 p = lock_user_string(arg1);
9293 if (!p) {
2852aafd 9294 return -TARGET_EFAULT;
356d771b
PB
9295 }
9296 } else {
9297 p = NULL;
9298 }
9299
9300 p2 = lock_user_string(arg2);
9301 if (!p2) {
9302 if (arg1) {
9303 unlock_user(p, arg1, 0);
9304 }
2852aafd 9305 return -TARGET_EFAULT;
356d771b
PB
9306 }
9307
9308 if (arg3) {
9309 p3 = lock_user_string(arg3);
9310 if (!p3) {
9311 if (arg1) {
579a97f7 9312 unlock_user(p, arg1, 0);
356d771b
PB
9313 }
9314 unlock_user(p2, arg2, 0);
2852aafd 9315 return -TARGET_EFAULT;
356d771b
PB
9316 }
9317 } else {
9318 p3 = NULL;
9319 }
9320
9321 /* FIXME - arg5 should be locked, but it isn't clear how to
9322 * do that since it's not guaranteed to be a NULL-terminated
9323 * string.
9324 */
9325 if (!arg5) {
9326 ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
9327 } else {
3e8f1628 9328 ret = mount(p, p2, p3, (unsigned long)arg4, g2h(cpu, arg5));
356d771b
PB
9329 }
9330 ret = get_errno(ret);
9331
9332 if (arg1) {
9333 unlock_user(p, arg1, 0);
9334 }
9335 unlock_user(p2, arg2, 0);
9336 if (arg3) {
9337 unlock_user(p3, arg3, 0);
9338 }
9339 }
72eb7ea8 9340 return ret;
6eb9dbf6
LV
9341#if defined(TARGET_NR_umount) || defined(TARGET_NR_oldumount)
9342#if defined(TARGET_NR_umount)
31e31b8a 9343 case TARGET_NR_umount:
6eb9dbf6
LV
9344#endif
9345#if defined(TARGET_NR_oldumount)
9346 case TARGET_NR_oldumount:
9347#endif
579a97f7 9348 if (!(p = lock_user_string(arg1)))
2852aafd 9349 return -TARGET_EFAULT;
53a5960a
PB
9350 ret = get_errno(umount(p));
9351 unlock_user(p, arg1, 0);
72eb7ea8 9352 return ret;
e5febef5 9353#endif
4b2d2753
TW
9354#if defined(TARGET_NR_move_mount) && defined(__NR_move_mount)
9355 case TARGET_NR_move_mount:
9356 {
9357 void *p2, *p4;
9358
9359 if (!arg2 || !arg4) {
9360 return -TARGET_EFAULT;
9361 }
9362
9363 p2 = lock_user_string(arg2);
9364 if (!p2) {
9365 return -TARGET_EFAULT;
9366 }
9367
9368 p4 = lock_user_string(arg4);
9369 if (!p4) {
9370 unlock_user(p2, arg2, 0);
9371 return -TARGET_EFAULT;
9372 }
9373 ret = get_errno(sys_move_mount(arg1, p2, arg3, p4, arg5));
9374
9375 unlock_user(p2, arg2, 0);
9376 unlock_user(p4, arg4, 0);
9377
9378 return ret;
9379 }
9380#endif
7f696cdd
TW
9381#if defined(TARGET_NR_open_tree) && defined(__NR_open_tree)
9382 case TARGET_NR_open_tree:
9383 {
9384 void *p2;
9385 int host_flags;
9386
9387 if (!arg2) {
9388 return -TARGET_EFAULT;
9389 }
9390
9391 p2 = lock_user_string(arg2);
9392 if (!p2) {
9393 return -TARGET_EFAULT;
9394 }
9395
9396 host_flags = arg3 & ~TARGET_O_CLOEXEC;
9397 if (arg3 & TARGET_O_CLOEXEC) {
9398 host_flags |= O_CLOEXEC;
9399 }
9400
9401 ret = get_errno(sys_open_tree(arg1, p2, host_flags));
9402
9403 unlock_user(p2, arg2, 0);
9404
9405 return ret;
9406 }
9407#endif
7a3148a9 9408#ifdef TARGET_NR_stime /* not on alpha */
31e31b8a
FB
9409 case TARGET_NR_stime:
9410 {
0f1f2d45
LV
9411 struct timespec ts;
9412 ts.tv_nsec = 0;
9413 if (get_user_sal(ts.tv_sec, arg1)) {
2852aafd 9414 return -TARGET_EFAULT;
0f1f2d45
LV
9415 }
9416 return get_errno(clock_settime(CLOCK_REALTIME, &ts));
31e31b8a 9417 }
7a3148a9 9418#endif
7a3148a9 9419#ifdef TARGET_NR_alarm /* not on alpha */
31e31b8a 9420 case TARGET_NR_alarm:
72eb7ea8 9421 return alarm(arg1);
7a3148a9 9422#endif
7a3148a9 9423#ifdef TARGET_NR_pause /* not on alpha */
31e31b8a 9424 case TARGET_NR_pause:
f59ec606
TB
9425 if (!block_signals()) {
9426 sigsuspend(&((TaskState *)cpu->opaque)->signal_mask);
9427 }
72eb7ea8 9428 return -TARGET_EINTR;
7a3148a9 9429#endif
e5febef5 9430#ifdef TARGET_NR_utime
31e31b8a 9431 case TARGET_NR_utime:
ebc05488 9432 {
53a5960a
PB
9433 struct utimbuf tbuf, *host_tbuf;
9434 struct target_utimbuf *target_tbuf;
9435 if (arg2) {
579a97f7 9436 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
2852aafd 9437 return -TARGET_EFAULT;
cbb21eed
MB
9438 tbuf.actime = tswapal(target_tbuf->actime);
9439 tbuf.modtime = tswapal(target_tbuf->modtime);
53a5960a
PB
9440 unlock_user_struct(target_tbuf, arg2, 0);
9441 host_tbuf = &tbuf;
f72e8ff4 9442 } else {
53a5960a 9443 host_tbuf = NULL;
f72e8ff4 9444 }
579a97f7 9445 if (!(p = lock_user_string(arg1)))
2852aafd 9446 return -TARGET_EFAULT;
53a5960a
PB
9447 ret = get_errno(utime(p, host_tbuf));
9448 unlock_user(p, arg1, 0);
ebc05488 9449 }
72eb7ea8 9450 return ret;
e5febef5 9451#endif
704eff6c 9452#ifdef TARGET_NR_utimes
978a66ff
FB
9453 case TARGET_NR_utimes:
9454 {
978a66ff 9455 struct timeval *tvp, tv[2];
53a5960a 9456 if (arg2) {
788f5ec4
TS
9457 if (copy_from_user_timeval(&tv[0], arg2)
9458 || copy_from_user_timeval(&tv[1],
9459 arg2 + sizeof(struct target_timeval)))
2852aafd 9460 return -TARGET_EFAULT;
978a66ff
FB
9461 tvp = tv;
9462 } else {
9463 tvp = NULL;
9464 }
579a97f7 9465 if (!(p = lock_user_string(arg1)))
2852aafd 9466 return -TARGET_EFAULT;
53a5960a
PB
9467 ret = get_errno(utimes(p, tvp));
9468 unlock_user(p, arg1, 0);
978a66ff 9469 }
72eb7ea8 9470 return ret;
704eff6c 9471#endif
c0d472b1 9472#if defined(TARGET_NR_futimesat)
ac8a6556
AZ
9473 case TARGET_NR_futimesat:
9474 {
9475 struct timeval *tvp, tv[2];
9476 if (arg3) {
9477 if (copy_from_user_timeval(&tv[0], arg3)
9478 || copy_from_user_timeval(&tv[1],
9479 arg3 + sizeof(struct target_timeval)))
2852aafd 9480 return -TARGET_EFAULT;
ac8a6556
AZ
9481 tvp = tv;
9482 } else {
9483 tvp = NULL;
9484 }
2852aafd
RH
9485 if (!(p = lock_user_string(arg2))) {
9486 return -TARGET_EFAULT;
9487 }
c0d472b1 9488 ret = get_errno(futimesat(arg1, path(p), tvp));
ac8a6556
AZ
9489 unlock_user(p, arg2, 0);
9490 }
72eb7ea8 9491 return ret;
ac8a6556 9492#endif
704eff6c 9493#ifdef TARGET_NR_access
31e31b8a 9494 case TARGET_NR_access:
2852aafd
RH
9495 if (!(p = lock_user_string(arg1))) {
9496 return -TARGET_EFAULT;
9497 }
719f908e 9498 ret = get_errno(access(path(p), arg2));
53a5960a 9499 unlock_user(p, arg1, 0);
72eb7ea8 9500 return ret;
704eff6c 9501#endif
92a34c10
TS
9502#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
9503 case TARGET_NR_faccessat:
2852aafd
RH
9504 if (!(p = lock_user_string(arg2))) {
9505 return -TARGET_EFAULT;
9506 }
c0d472b1 9507 ret = get_errno(faccessat(arg1, p, arg3, 0));
579a97f7 9508 unlock_user(p, arg2, 0);
72eb7ea8 9509 return ret;
92a34c10 9510#endif
35a2c85f
WX
9511#if defined(TARGET_NR_faccessat2)
9512 case TARGET_NR_faccessat2:
9513 if (!(p = lock_user_string(arg2))) {
9514 return -TARGET_EFAULT;
9515 }
9516 ret = get_errno(faccessat(arg1, p, arg3, arg4));
9517 unlock_user(p, arg2, 0);
9518 return ret;
9519#endif
7a3148a9 9520#ifdef TARGET_NR_nice /* not on alpha */
31e31b8a 9521 case TARGET_NR_nice:
72eb7ea8 9522 return get_errno(nice(arg1));
ebc05488 9523#endif
31e31b8a 9524 case TARGET_NR_sync:
04369ff2 9525 sync();
72eb7ea8 9526 return 0;
5a03cd00
AM
9527#if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
9528 case TARGET_NR_syncfs:
72eb7ea8 9529 return get_errno(syncfs(arg1));
5a03cd00 9530#endif
31e31b8a 9531 case TARGET_NR_kill:
72eb7ea8 9532 return get_errno(safe_kill(arg1, target_to_host_signal(arg2)));
704eff6c 9533#ifdef TARGET_NR_rename
31e31b8a 9534 case TARGET_NR_rename:
53a5960a
PB
9535 {
9536 void *p2;
9537 p = lock_user_string(arg1);
9538 p2 = lock_user_string(arg2);
579a97f7
FB
9539 if (!p || !p2)
9540 ret = -TARGET_EFAULT;
9541 else
9542 ret = get_errno(rename(p, p2));
53a5960a
PB
9543 unlock_user(p2, arg2, 0);
9544 unlock_user(p, arg1, 0);
9545 }
72eb7ea8 9546 return ret;
704eff6c 9547#endif
c0d472b1 9548#if defined(TARGET_NR_renameat)
722183f6 9549 case TARGET_NR_renameat:
722183f6 9550 {
579a97f7 9551 void *p2;
722183f6
TS
9552 p = lock_user_string(arg2);
9553 p2 = lock_user_string(arg4);
579a97f7 9554 if (!p || !p2)
0da46a6e 9555 ret = -TARGET_EFAULT;
722183f6 9556 else
c0d472b1 9557 ret = get_errno(renameat(arg1, p, arg3, p2));
579a97f7
FB
9558 unlock_user(p2, arg4, 0);
9559 unlock_user(p, arg2, 0);
722183f6 9560 }
72eb7ea8 9561 return ret;
95d0307c
AS
9562#endif
9563#if defined(TARGET_NR_renameat2)
9564 case TARGET_NR_renameat2:
9565 {
9566 void *p2;
9567 p = lock_user_string(arg2);
9568 p2 = lock_user_string(arg4);
9569 if (!p || !p2) {
9570 ret = -TARGET_EFAULT;
9571 } else {
9572 ret = get_errno(sys_renameat2(arg1, p, arg3, p2, arg5));
9573 }
9574 unlock_user(p2, arg4, 0);
9575 unlock_user(p, arg2, 0);
9576 }
72eb7ea8 9577 return ret;
722183f6 9578#endif
704eff6c 9579#ifdef TARGET_NR_mkdir
31e31b8a 9580 case TARGET_NR_mkdir:
579a97f7 9581 if (!(p = lock_user_string(arg1)))
2852aafd 9582 return -TARGET_EFAULT;
53a5960a
PB
9583 ret = get_errno(mkdir(p, arg2));
9584 unlock_user(p, arg1, 0);
72eb7ea8 9585 return ret;
704eff6c 9586#endif
c0d472b1 9587#if defined(TARGET_NR_mkdirat)
4472ad0d 9588 case TARGET_NR_mkdirat:
579a97f7 9589 if (!(p = lock_user_string(arg2)))
2852aafd 9590 return -TARGET_EFAULT;
c0d472b1 9591 ret = get_errno(mkdirat(arg1, p, arg3));
579a97f7 9592 unlock_user(p, arg2, 0);
72eb7ea8 9593 return ret;
4472ad0d 9594#endif
704eff6c 9595#ifdef TARGET_NR_rmdir
31e31b8a 9596 case TARGET_NR_rmdir:
579a97f7 9597 if (!(p = lock_user_string(arg1)))
2852aafd 9598 return -TARGET_EFAULT;
53a5960a
PB
9599 ret = get_errno(rmdir(p));
9600 unlock_user(p, arg1, 0);
72eb7ea8 9601 return ret;
704eff6c 9602#endif
31e31b8a
FB
9603 case TARGET_NR_dup:
9604 ret = get_errno(dup(arg1));
e36800c9
LV
9605 if (ret >= 0) {
9606 fd_trans_dup(arg1, ret);
9607 }
72eb7ea8 9608 return ret;
704eff6c 9609#ifdef TARGET_NR_pipe
31e31b8a 9610 case TARGET_NR_pipe:
72eb7ea8 9611 return do_pipe(cpu_env, arg1, 0, 0);
704eff6c 9612#endif
099d6b0f
RV
9613#ifdef TARGET_NR_pipe2
9614 case TARGET_NR_pipe2:
72eb7ea8
RH
9615 return do_pipe(cpu_env, arg1,
9616 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
099d6b0f 9617#endif
31e31b8a 9618 case TARGET_NR_times:
32f36bce 9619 {
53a5960a 9620 struct target_tms *tmsp;
32f36bce
FB
9621 struct tms tms;
9622 ret = get_errno(times(&tms));
53a5960a 9623 if (arg1) {
579a97f7
FB
9624 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
9625 if (!tmsp)
2852aafd 9626 return -TARGET_EFAULT;
cbb21eed
MB
9627 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
9628 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
9629 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
9630 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
32f36bce 9631 }
c596ed17
FB
9632 if (!is_error(ret))
9633 ret = host_to_target_clock_t(ret);
32f36bce 9634 }
72eb7ea8 9635 return ret;
31e31b8a 9636 case TARGET_NR_acct:
38d840e6
AJ
9637 if (arg1 == 0) {
9638 ret = get_errno(acct(NULL));
9639 } else {
2852aafd
RH
9640 if (!(p = lock_user_string(arg1))) {
9641 return -TARGET_EFAULT;
9642 }
38d840e6
AJ
9643 ret = get_errno(acct(path(p)));
9644 unlock_user(p, arg1, 0);
9645 }
72eb7ea8 9646 return ret;
8070e7be 9647#ifdef TARGET_NR_umount2
31e31b8a 9648 case TARGET_NR_umount2:
579a97f7 9649 if (!(p = lock_user_string(arg1)))
2852aafd 9650 return -TARGET_EFAULT;
53a5960a
PB
9651 ret = get_errno(umount2(p, arg2));
9652 unlock_user(p, arg1, 0);
72eb7ea8 9653 return ret;
ebc05488 9654#endif
31e31b8a 9655 case TARGET_NR_ioctl:
72eb7ea8 9656 return do_ioctl(arg1, arg2, arg3);
47ae93cd 9657#ifdef TARGET_NR_fcntl
31e31b8a 9658 case TARGET_NR_fcntl:
72eb7ea8 9659 return do_fcntl(arg1, arg2, arg3);
ebc05488 9660#endif
31e31b8a 9661 case TARGET_NR_setpgid:
72eb7ea8 9662 return get_errno(setpgid(arg1, arg2));
31e31b8a 9663 case TARGET_NR_umask:
72eb7ea8 9664 return get_errno(umask(arg1));
31e31b8a 9665 case TARGET_NR_chroot:
579a97f7 9666 if (!(p = lock_user_string(arg1)))
2852aafd 9667 return -TARGET_EFAULT;
53a5960a
PB
9668 ret = get_errno(chroot(p));
9669 unlock_user(p, arg1, 0);
72eb7ea8 9670 return ret;
704eff6c 9671#ifdef TARGET_NR_dup2
31e31b8a
FB
9672 case TARGET_NR_dup2:
9673 ret = get_errno(dup2(arg1, arg2));
e36800c9
LV
9674 if (ret >= 0) {
9675 fd_trans_dup(arg1, arg2);
9676 }
72eb7ea8 9677 return ret;
704eff6c 9678#endif
d0927938
UH
9679#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
9680 case TARGET_NR_dup3:
10fa993a
PM
9681 {
9682 int host_flags;
9683
9684 if ((arg3 & ~TARGET_O_CLOEXEC) != 0) {
9685 return -EINVAL;
9686 }
9687 host_flags = target_to_host_bitmask(arg3, fcntl_flags_tbl);
9688 ret = get_errno(dup3(arg1, arg2, host_flags));
e36800c9
LV
9689 if (ret >= 0) {
9690 fd_trans_dup(arg1, arg2);
9691 }
72eb7ea8 9692 return ret;
10fa993a 9693 }
d0927938 9694#endif
7a3148a9 9695#ifdef TARGET_NR_getppid /* not on alpha */
31e31b8a 9696 case TARGET_NR_getppid:
72eb7ea8 9697 return get_errno(getppid());
7a3148a9 9698#endif
704eff6c 9699#ifdef TARGET_NR_getpgrp
31e31b8a 9700 case TARGET_NR_getpgrp:
72eb7ea8 9701 return get_errno(getpgrp());
704eff6c 9702#endif
31e31b8a 9703 case TARGET_NR_setsid:
72eb7ea8 9704 return get_errno(setsid());
e5febef5 9705#ifdef TARGET_NR_sigaction
31e31b8a 9706 case TARGET_NR_sigaction:
31e31b8a 9707 {
02d0de10 9708#if defined(TARGET_MIPS)
106ec879
FB
9709 struct target_sigaction act, oact, *pact, *old_act;
9710
9711 if (arg2) {
579a97f7 9712 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
2852aafd 9713 return -TARGET_EFAULT;
106ec879
FB
9714 act._sa_handler = old_act->_sa_handler;
9715 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
9716 act.sa_flags = old_act->sa_flags;
9717 unlock_user_struct(old_act, arg2, 0);
9718 pact = &act;
9719 } else {
9720 pact = NULL;
9721 }
9722
02fb28e8 9723 ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
106ec879
FB
9724
9725 if (!is_error(ret) && arg3) {
579a97f7 9726 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
2852aafd 9727 return -TARGET_EFAULT;
106ec879
FB
9728 old_act->_sa_handler = oact._sa_handler;
9729 old_act->sa_flags = oact.sa_flags;
9730 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
9731 old_act->sa_mask.sig[1] = 0;
9732 old_act->sa_mask.sig[2] = 0;
9733 old_act->sa_mask.sig[3] = 0;
9734 unlock_user_struct(old_act, arg3, 1);
9735 }
6049f4f8
RH
9736#else
9737 struct target_old_sigaction *old_act;
9738 struct target_sigaction act, oact, *pact;
9739 if (arg2) {
9740 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
2852aafd 9741 return -TARGET_EFAULT;
6049f4f8
RH
9742 act._sa_handler = old_act->_sa_handler;
9743 target_siginitset(&act.sa_mask, old_act->sa_mask);
9744 act.sa_flags = old_act->sa_flags;
ca192277 9745#ifdef TARGET_ARCH_HAS_SA_RESTORER
6049f4f8 9746 act.sa_restorer = old_act->sa_restorer;
ca192277 9747#endif
6049f4f8
RH
9748 unlock_user_struct(old_act, arg2, 0);
9749 pact = &act;
9750 } else {
9751 pact = NULL;
9752 }
02fb28e8 9753 ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
6049f4f8
RH
9754 if (!is_error(ret) && arg3) {
9755 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
2852aafd 9756 return -TARGET_EFAULT;
6049f4f8
RH
9757 old_act->_sa_handler = oact._sa_handler;
9758 old_act->sa_mask = oact.sa_mask.sig[0];
9759 old_act->sa_flags = oact.sa_flags;
ca192277 9760#ifdef TARGET_ARCH_HAS_SA_RESTORER
6049f4f8 9761 old_act->sa_restorer = oact.sa_restorer;
ca192277 9762#endif
6049f4f8
RH
9763 unlock_user_struct(old_act, arg3, 1);
9764 }
388bb21a 9765#endif
31e31b8a 9766 }
72eb7ea8 9767 return ret;
e5febef5 9768#endif
66fb9763 9769 case TARGET_NR_rt_sigaction:
53a5960a 9770 {
0f6f9903
RH
9771 /*
9772 * For Alpha and SPARC this is a 5 argument syscall, with
78bfef72
PM
9773 * a 'restorer' parameter which must be copied into the
9774 * sa_restorer field of the sigaction struct.
9775 * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
9776 * and arg5 is the sigsetsize.
78bfef72 9777 */
0f6f9903
RH
9778#if defined(TARGET_ALPHA)
9779 target_ulong sigsetsize = arg4;
9780 target_ulong restorer = arg5;
9781#elif defined(TARGET_SPARC)
78bfef72
PM
9782 target_ulong restorer = arg4;
9783 target_ulong sigsetsize = arg5;
9784#else
9785 target_ulong sigsetsize = arg4;
02fb28e8 9786 target_ulong restorer = 0;
78bfef72 9787#endif
fb80439b
RH
9788 struct target_sigaction *act = NULL;
9789 struct target_sigaction *oact = NULL;
53a5960a 9790
78bfef72 9791 if (sigsetsize != sizeof(target_sigset_t)) {
72eb7ea8 9792 return -TARGET_EINVAL;
c815701e 9793 }
fb80439b
RH
9794 if (arg2 && !lock_user_struct(VERIFY_READ, act, arg2, 1)) {
9795 return -TARGET_EFAULT;
78bfef72 9796 }
fb80439b
RH
9797 if (arg3 && !lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
9798 ret = -TARGET_EFAULT;
9799 } else {
9800 ret = get_errno(do_sigaction(arg1, act, oact, restorer));
9801 if (oact) {
9802 unlock_user_struct(oact, arg3, 1);
579a97f7 9803 }
fb80439b
RH
9804 }
9805 if (act) {
53a5960a 9806 unlock_user_struct(act, arg2, 0);
fb80439b 9807 }
53a5960a 9808 }
72eb7ea8 9809 return ret;
7a3148a9 9810#ifdef TARGET_NR_sgetmask /* not on alpha */
31e31b8a 9811 case TARGET_NR_sgetmask:
66fb9763
FB
9812 {
9813 sigset_t cur_set;
992f48a0 9814 abi_ulong target_set;
3d3efba0
PM
9815 ret = do_sigprocmask(0, NULL, &cur_set);
9816 if (!ret) {
9817 host_to_target_old_sigset(&target_set, &cur_set);
9818 ret = target_set;
9819 }
66fb9763 9820 }
72eb7ea8 9821 return ret;
7a3148a9
JM
9822#endif
9823#ifdef TARGET_NR_ssetmask /* not on alpha */
31e31b8a 9824 case TARGET_NR_ssetmask:
66fb9763 9825 {
a8617d8c 9826 sigset_t set, oset;
992f48a0 9827 abi_ulong target_set = arg1;
66fb9763 9828 target_to_host_old_sigset(&set, &target_set);
3d3efba0
PM
9829 ret = do_sigprocmask(SIG_SETMASK, &set, &oset);
9830 if (!ret) {
9831 host_to_target_old_sigset(&target_set, &oset);
9832 ret = target_set;
9833 }
66fb9763 9834 }
72eb7ea8 9835 return ret;
7a3148a9 9836#endif
e5febef5 9837#ifdef TARGET_NR_sigprocmask
66fb9763
FB
9838 case TARGET_NR_sigprocmask:
9839 {
a5b3b13b
RH
9840#if defined(TARGET_ALPHA)
9841 sigset_t set, oldset;
9842 abi_ulong mask;
9843 int how;
9844
9845 switch (arg1) {
9846 case TARGET_SIG_BLOCK:
9847 how = SIG_BLOCK;
9848 break;
9849 case TARGET_SIG_UNBLOCK:
9850 how = SIG_UNBLOCK;
9851 break;
9852 case TARGET_SIG_SETMASK:
9853 how = SIG_SETMASK;
9854 break;
9855 default:
259841c1 9856 return -TARGET_EINVAL;
a5b3b13b
RH
9857 }
9858 mask = arg2;
9859 target_to_host_old_sigset(&set, &mask);
9860
3d3efba0 9861 ret = do_sigprocmask(how, &set, &oldset);
a5b3b13b
RH
9862 if (!is_error(ret)) {
9863 host_to_target_old_sigset(&mask, &oldset);
9864 ret = mask;
0effdc29 9865 cpu_env->ir[IR_V0] = 0; /* force no error */
a5b3b13b
RH
9866 }
9867#else
66fb9763 9868 sigset_t set, oldset, *set_ptr;
a5b3b13b 9869 int how;
3b46e624 9870
53a5960a 9871 if (arg2) {
ebce1719
PV
9872 p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1);
9873 if (!p) {
9874 return -TARGET_EFAULT;
9875 }
9876 target_to_host_old_sigset(&set, p);
9877 unlock_user(p, arg2, 0);
9878 set_ptr = &set;
a5b3b13b 9879 switch (arg1) {
66fb9763
FB
9880 case TARGET_SIG_BLOCK:
9881 how = SIG_BLOCK;
9882 break;
9883 case TARGET_SIG_UNBLOCK:
9884 how = SIG_UNBLOCK;
9885 break;
9886 case TARGET_SIG_SETMASK:
9887 how = SIG_SETMASK;
9888 break;
9889 default:
259841c1 9890 return -TARGET_EINVAL;
66fb9763 9891 }
66fb9763
FB
9892 } else {
9893 how = 0;
9894 set_ptr = NULL;
9895 }
3d3efba0 9896 ret = do_sigprocmask(how, set_ptr, &oldset);
53a5960a 9897 if (!is_error(ret) && arg3) {
c227f099 9898 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
2852aafd 9899 return -TARGET_EFAULT;
53a5960a 9900 host_to_target_old_sigset(p, &oldset);
c227f099 9901 unlock_user(p, arg3, sizeof(target_sigset_t));
66fb9763 9902 }
a5b3b13b 9903#endif
66fb9763 9904 }
72eb7ea8 9905 return ret;
e5febef5 9906#endif
66fb9763
FB
9907 case TARGET_NR_rt_sigprocmask:
9908 {
9909 int how = arg1;
9910 sigset_t set, oldset, *set_ptr;
3b46e624 9911
c815701e 9912 if (arg4 != sizeof(target_sigset_t)) {
72eb7ea8 9913 return -TARGET_EINVAL;
c815701e
PM
9914 }
9915
53a5960a 9916 if (arg2) {
d3ced2a5
SCW
9917 p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1);
9918 if (!p) {
9919 return -TARGET_EFAULT;
9920 }
9921 target_to_host_sigset(&set, p);
9922 unlock_user(p, arg2, 0);
9923 set_ptr = &set;
66fb9763
FB
9924 switch(how) {
9925 case TARGET_SIG_BLOCK:
9926 how = SIG_BLOCK;
9927 break;
9928 case TARGET_SIG_UNBLOCK:
9929 how = SIG_UNBLOCK;
9930 break;
9931 case TARGET_SIG_SETMASK:
9932 how = SIG_SETMASK;
9933 break;
9934 default:
259841c1 9935 return -TARGET_EINVAL;
66fb9763 9936 }
66fb9763
FB
9937 } else {
9938 how = 0;
9939 set_ptr = NULL;
9940 }
3d3efba0 9941 ret = do_sigprocmask(how, set_ptr, &oldset);
53a5960a 9942 if (!is_error(ret) && arg3) {
c227f099 9943 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
2852aafd 9944 return -TARGET_EFAULT;
53a5960a 9945 host_to_target_sigset(p, &oldset);
c227f099 9946 unlock_user(p, arg3, sizeof(target_sigset_t));
66fb9763
FB
9947 }
9948 }
72eb7ea8 9949 return ret;
e5febef5 9950#ifdef TARGET_NR_sigpending
66fb9763
FB
9951 case TARGET_NR_sigpending:
9952 {
9953 sigset_t set;
9954 ret = get_errno(sigpending(&set));
9955 if (!is_error(ret)) {
c227f099 9956 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
2852aafd 9957 return -TARGET_EFAULT;
53a5960a 9958 host_to_target_old_sigset(p, &set);
c227f099 9959 unlock_user(p, arg1, sizeof(target_sigset_t));
66fb9763
FB
9960 }
9961 }
72eb7ea8 9962 return ret;
e5febef5 9963#endif
66fb9763
FB
9964 case TARGET_NR_rt_sigpending:
9965 {
9966 sigset_t set;
c815701e
PM
9967
9968 /* Yes, this check is >, not != like most. We follow the kernel's
9969 * logic and it does it like this because it implements
9970 * NR_sigpending through the same code path, and in that case
9971 * the old_sigset_t is smaller in size.
9972 */
9973 if (arg2 > sizeof(target_sigset_t)) {
72eb7ea8 9974 return -TARGET_EINVAL;
c815701e
PM
9975 }
9976
66fb9763
FB
9977 ret = get_errno(sigpending(&set));
9978 if (!is_error(ret)) {
c227f099 9979 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
2852aafd 9980 return -TARGET_EFAULT;
53a5960a 9981 host_to_target_sigset(p, &set);
c227f099 9982 unlock_user(p, arg1, sizeof(target_sigset_t));
66fb9763
FB
9983 }
9984 }
72eb7ea8 9985 return ret;
e5febef5 9986#ifdef TARGET_NR_sigsuspend
66fb9763
FB
9987 case TARGET_NR_sigsuspend:
9988 {
0a99f093
RH
9989 sigset_t *set;
9990
f43ce12b 9991#if defined(TARGET_ALPHA)
0a99f093 9992 TaskState *ts = cpu->opaque;
7fb5ef35
RH
9993 /* target_to_host_old_sigset will bswap back */
9994 abi_ulong mask = tswapal(arg1);
0a99f093
RH
9995 set = &ts->sigsuspend_mask;
9996 target_to_host_old_sigset(set, &mask);
f43ce12b 9997#else
0a99f093
RH
9998 ret = process_sigsuspend_mask(&set, arg1, sizeof(target_sigset_t));
9999 if (ret != 0) {
10000 return ret;
3d3efba0 10001 }
0a99f093
RH
10002#endif
10003 ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE));
10004 finish_sigsuspend_mask(ret);
66fb9763 10005 }
72eb7ea8 10006 return ret;
e5febef5 10007#endif
66fb9763
FB
10008 case TARGET_NR_rt_sigsuspend:
10009 {
0a99f093 10010 sigset_t *set;
c815701e 10011
0a99f093
RH
10012 ret = process_sigsuspend_mask(&set, arg1, arg2);
10013 if (ret != 0) {
10014 return ret;
3d3efba0 10015 }
0a99f093
RH
10016 ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE));
10017 finish_sigsuspend_mask(ret);
66fb9763 10018 }
72eb7ea8 10019 return ret;
859e8a89 10020#ifdef TARGET_NR_rt_sigtimedwait
66fb9763
FB
10021 case TARGET_NR_rt_sigtimedwait:
10022 {
66fb9763
FB
10023 sigset_t set;
10024 struct timespec uts, *puts;
10025 siginfo_t uinfo;
3b46e624 10026
c815701e 10027 if (arg4 != sizeof(target_sigset_t)) {
72eb7ea8 10028 return -TARGET_EINVAL;
c815701e
PM
10029 }
10030
c227f099 10031 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
2852aafd 10032 return -TARGET_EFAULT;
53a5960a
PB
10033 target_to_host_sigset(&set, p);
10034 unlock_user(p, arg1, 0);
10035 if (arg3) {
66fb9763 10036 puts = &uts;
4d213001
FB
10037 if (target_to_host_timespec(puts, arg3)) {
10038 return -TARGET_EFAULT;
10039 }
66fb9763
FB
10040 } else {
10041 puts = NULL;
10042 }
b3f82330
PM
10043 ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
10044 SIGSET_T_SIZE));
974a196d
PJ
10045 if (!is_error(ret)) {
10046 if (arg2) {
10047 p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
10048 0);
10049 if (!p) {
2852aafd 10050 return -TARGET_EFAULT;
974a196d
PJ
10051 }
10052 host_to_target_siginfo(p, &uinfo);
10053 unlock_user(p, arg2, sizeof(target_siginfo_t));
10054 }
10055 ret = host_to_target_signal(ret);
66fb9763
FB
10056 }
10057 }
72eb7ea8 10058 return ret;
ddcbde15
FB
10059#endif
10060#ifdef TARGET_NR_rt_sigtimedwait_time64
10061 case TARGET_NR_rt_sigtimedwait_time64:
10062 {
10063 sigset_t set;
10064 struct timespec uts, *puts;
10065 siginfo_t uinfo;
10066
10067 if (arg4 != sizeof(target_sigset_t)) {
10068 return -TARGET_EINVAL;
10069 }
10070
10071 p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1);
10072 if (!p) {
10073 return -TARGET_EFAULT;
10074 }
10075 target_to_host_sigset(&set, p);
10076 unlock_user(p, arg1, 0);
10077 if (arg3) {
10078 puts = &uts;
10079 if (target_to_host_timespec64(puts, arg3)) {
10080 return -TARGET_EFAULT;
10081 }
10082 } else {
10083 puts = NULL;
10084 }
10085 ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
10086 SIGSET_T_SIZE));
10087 if (!is_error(ret)) {
10088 if (arg2) {
10089 p = lock_user(VERIFY_WRITE, arg2,
10090 sizeof(target_siginfo_t), 0);
10091 if (!p) {
10092 return -TARGET_EFAULT;
10093 }
10094 host_to_target_siginfo(p, &uinfo);
10095 unlock_user(p, arg2, sizeof(target_siginfo_t));
10096 }
10097 ret = host_to_target_signal(ret);
10098 }
10099 }
10100 return ret;
859e8a89 10101#endif
66fb9763
FB
10102 case TARGET_NR_rt_sigqueueinfo:
10103 {
10104 siginfo_t uinfo;
4debae6f
PM
10105
10106 p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
10107 if (!p) {
2852aafd 10108 return -TARGET_EFAULT;
4debae6f 10109 }
53a5960a 10110 target_to_host_siginfo(&uinfo, p);
d8b6d892 10111 unlock_user(p, arg3, 0);
9b9145f0 10112 ret = get_errno(sys_rt_sigqueueinfo(arg1, target_to_host_signal(arg2), &uinfo));
66fb9763 10113 }
72eb7ea8 10114 return ret;
cf8b8bfc
MS
10115 case TARGET_NR_rt_tgsigqueueinfo:
10116 {
10117 siginfo_t uinfo;
10118
10119 p = lock_user(VERIFY_READ, arg4, sizeof(target_siginfo_t), 1);
10120 if (!p) {
2852aafd 10121 return -TARGET_EFAULT;
cf8b8bfc
MS
10122 }
10123 target_to_host_siginfo(&uinfo, p);
10124 unlock_user(p, arg4, 0);
9b9145f0 10125 ret = get_errno(sys_rt_tgsigqueueinfo(arg1, arg2, target_to_host_signal(arg3), &uinfo));
cf8b8bfc 10126 }
72eb7ea8 10127 return ret;
e5febef5 10128#ifdef TARGET_NR_sigreturn
66fb9763 10129 case TARGET_NR_sigreturn:
3d3efba0 10130 if (block_signals()) {
af254a27 10131 return -QEMU_ERESTARTSYS;
3d3efba0 10132 }
72eb7ea8 10133 return do_sigreturn(cpu_env);
e5febef5 10134#endif
66fb9763 10135 case TARGET_NR_rt_sigreturn:
3d3efba0 10136 if (block_signals()) {
af254a27 10137 return -QEMU_ERESTARTSYS;
3d3efba0 10138 }
72eb7ea8 10139 return do_rt_sigreturn(cpu_env);
31e31b8a 10140 case TARGET_NR_sethostname:
579a97f7 10141 if (!(p = lock_user_string(arg1)))
2852aafd 10142 return -TARGET_EFAULT;
53a5960a
PB
10143 ret = get_errno(sethostname(p, arg2));
10144 unlock_user(p, arg1, 0);
72eb7ea8 10145 return ret;
4f7f8924 10146#ifdef TARGET_NR_setrlimit
31e31b8a 10147 case TARGET_NR_setrlimit:
9de5e440 10148 {
e22b7015 10149 int resource = target_to_host_resource(arg1);
53a5960a 10150 struct target_rlimit *target_rlim;
9de5e440 10151 struct rlimit rlim;
579a97f7 10152 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
2852aafd 10153 return -TARGET_EFAULT;
81bbe906
TY
10154 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
10155 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
53a5960a 10156 unlock_user_struct(target_rlim, arg2, 0);
5dfa88f7
MF
10157 /*
10158 * If we just passed through resource limit settings for memory then
10159 * they would also apply to QEMU's own allocations, and QEMU will
10160 * crash or hang or die if its allocations fail. Ideally we would
10161 * track the guest allocations in QEMU and apply the limits ourselves.
10162 * For now, just tell the guest the call succeeded but don't actually
10163 * limit anything.
10164 */
10165 if (resource != RLIMIT_AS &&
10166 resource != RLIMIT_DATA &&
10167 resource != RLIMIT_STACK) {
10168 return get_errno(setrlimit(resource, &rlim));
10169 } else {
10170 return 0;
10171 }
9de5e440 10172 }
4f7f8924
AR
10173#endif
10174#ifdef TARGET_NR_getrlimit
31e31b8a 10175 case TARGET_NR_getrlimit:
9de5e440 10176 {
e22b7015 10177 int resource = target_to_host_resource(arg1);
53a5960a 10178 struct target_rlimit *target_rlim;
9de5e440 10179 struct rlimit rlim;
3b46e624 10180
9de5e440
FB
10181 ret = get_errno(getrlimit(resource, &rlim));
10182 if (!is_error(ret)) {
579a97f7 10183 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
2852aafd 10184 return -TARGET_EFAULT;
81bbe906
TY
10185 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
10186 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
53a5960a 10187 unlock_user_struct(target_rlim, arg2, 1);
9de5e440
FB
10188 }
10189 }
72eb7ea8 10190 return ret;
4f7f8924 10191#endif
31e31b8a 10192 case TARGET_NR_getrusage:
b409186b
FB
10193 {
10194 struct rusage rusage;
b409186b
FB
10195 ret = get_errno(getrusage(arg1, &rusage));
10196 if (!is_error(ret)) {
a39fb273 10197 ret = host_to_target_rusage(arg2, &rusage);
b409186b
FB
10198 }
10199 }
72eb7ea8 10200 return ret;
859e8a89 10201#if defined(TARGET_NR_gettimeofday)
31e31b8a
FB
10202 case TARGET_NR_gettimeofday:
10203 {
31e31b8a 10204 struct timeval tv;
a52f5f87
RH
10205 struct timezone tz;
10206
10207 ret = get_errno(gettimeofday(&tv, &tz));
31e31b8a 10208 if (!is_error(ret)) {
a52f5f87
RH
10209 if (arg1 && copy_to_user_timeval(arg1, &tv)) {
10210 return -TARGET_EFAULT;
10211 }
10212 if (arg2 && copy_to_user_timezone(arg2, &tz)) {
2852aafd 10213 return -TARGET_EFAULT;
a52f5f87 10214 }
31e31b8a
FB
10215 }
10216 }
72eb7ea8 10217 return ret;
859e8a89
AF
10218#endif
10219#if defined(TARGET_NR_settimeofday)
31e31b8a
FB
10220 case TARGET_NR_settimeofday:
10221 {
b67d8031 10222 struct timeval tv, *ptv = NULL;
ef4467e9
PB
10223 struct timezone tz, *ptz = NULL;
10224
b67d8031
PB
10225 if (arg1) {
10226 if (copy_from_user_timeval(&tv, arg1)) {
2852aafd 10227 return -TARGET_EFAULT;
b67d8031
PB
10228 }
10229 ptv = &tv;
10230 }
ef4467e9
PB
10231
10232 if (arg2) {
10233 if (copy_from_user_timezone(&tz, arg2)) {
2852aafd 10234 return -TARGET_EFAULT;
ef4467e9
PB
10235 }
10236 ptz = &tz;
10237 }
10238
72eb7ea8 10239 return get_errno(settimeofday(ptv, ptz));
31e31b8a 10240 }
859e8a89 10241#endif
9468a5d4 10242#if defined(TARGET_NR_select)
31e31b8a 10243 case TARGET_NR_select:
5457dc9e
LV
10244#if defined(TARGET_WANT_NI_OLD_SELECT)
10245 /* some architectures used to have old_select here
10246 * but now ENOSYS it.
10247 */
10248 ret = -TARGET_ENOSYS;
10249#elif defined(TARGET_WANT_OLD_SYS_SELECT)
10250 ret = do_old_select(arg1);
9468a5d4 10251#else
5457dc9e 10252 ret = do_select(arg1, arg2, arg3, arg4, arg5);
9468a5d4 10253#endif
72eb7ea8 10254 return ret;
9e42382f
RV
10255#endif
10256#ifdef TARGET_NR_pselect6
10257 case TARGET_NR_pselect6:
e5ce9688
FB
10258 return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, false);
10259#endif
10260#ifdef TARGET_NR_pselect6_time64
10261 case TARGET_NR_pselect6_time64:
10262 return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, true);
048f6b4d 10263#endif
704eff6c 10264#ifdef TARGET_NR_symlink
31e31b8a 10265 case TARGET_NR_symlink:
53a5960a
PB
10266 {
10267 void *p2;
10268 p = lock_user_string(arg1);
10269 p2 = lock_user_string(arg2);
579a97f7
FB
10270 if (!p || !p2)
10271 ret = -TARGET_EFAULT;
10272 else
10273 ret = get_errno(symlink(p, p2));
53a5960a
PB
10274 unlock_user(p2, arg2, 0);
10275 unlock_user(p, arg1, 0);
10276 }
72eb7ea8 10277 return ret;
704eff6c 10278#endif
c0d472b1 10279#if defined(TARGET_NR_symlinkat)
f0b6243d 10280 case TARGET_NR_symlinkat:
f0b6243d 10281 {
579a97f7 10282 void *p2;
f0b6243d
TS
10283 p = lock_user_string(arg1);
10284 p2 = lock_user_string(arg3);
579a97f7 10285 if (!p || !p2)
0da46a6e 10286 ret = -TARGET_EFAULT;
f0b6243d 10287 else
c0d472b1 10288 ret = get_errno(symlinkat(p, arg2, p2));
579a97f7
FB
10289 unlock_user(p2, arg3, 0);
10290 unlock_user(p, arg1, 0);
f0b6243d 10291 }
72eb7ea8 10292 return ret;
f0b6243d 10293#endif
704eff6c 10294#ifdef TARGET_NR_readlink
31e31b8a 10295 case TARGET_NR_readlink:
53a5960a 10296 {
463d8e73 10297 void *p2;
53a5960a 10298 p = lock_user_string(arg1);
579a97f7 10299 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
a4dab0a0 10300 ret = get_errno(do_guest_readlink(p, p2, arg3));
53a5960a
PB
10301 unlock_user(p2, arg2, ret);
10302 unlock_user(p, arg1, 0);
10303 }
72eb7ea8 10304 return ret;
704eff6c 10305#endif
c0d472b1 10306#if defined(TARGET_NR_readlinkat)
5e0ccb18 10307 case TARGET_NR_readlinkat:
5e0ccb18 10308 {
579a97f7 10309 void *p2;
5e0ccb18 10310 p = lock_user_string(arg2);
579a97f7 10311 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
463d8e73
AS
10312 if (!p || !p2) {
10313 ret = -TARGET_EFAULT;
65d4830d
JN
10314 } else if (!arg4) {
10315 /* Short circuit this for the magic exe check. */
10316 ret = -TARGET_EINVAL;
463d8e73 10317 } else if (is_proc_myself((const char *)p, "exe")) {
258bec39
HD
10318 /*
10319 * Don't worry about sign mismatch as earlier mapping
10320 * logic would have thrown a bad address error.
10321 */
10322 ret = MIN(strlen(exec_path), arg4);
10323 /* We cannot NUL terminate the string. */
10324 memcpy(p2, exec_path, ret);
463d8e73 10325 } else {
c0d472b1 10326 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
463d8e73 10327 }
579a97f7
FB
10328 unlock_user(p2, arg3, ret);
10329 unlock_user(p, arg2, 0);
5e0ccb18 10330 }
72eb7ea8 10331 return ret;
5e0ccb18 10332#endif
e5febef5 10333#ifdef TARGET_NR_swapon
31e31b8a 10334 case TARGET_NR_swapon:
579a97f7 10335 if (!(p = lock_user_string(arg1)))
2852aafd 10336 return -TARGET_EFAULT;
53a5960a
PB
10337 ret = get_errno(swapon(p, arg2));
10338 unlock_user(p, arg1, 0);
72eb7ea8 10339 return ret;
e5febef5 10340#endif
31e31b8a 10341 case TARGET_NR_reboot:
c07ecc68
LV
10342 if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
10343 /* arg4 must be ignored in all other cases */
10344 p = lock_user_string(arg4);
10345 if (!p) {
2852aafd 10346 return -TARGET_EFAULT;
c07ecc68
LV
10347 }
10348 ret = get_errno(reboot(arg1, arg2, arg3, p));
10349 unlock_user(p, arg4, 0);
10350 } else {
10351 ret = get_errno(reboot(arg1, arg2, arg3, NULL));
10352 }
72eb7ea8 10353 return ret;
e5febef5 10354#ifdef TARGET_NR_mmap
31e31b8a 10355 case TARGET_NR_mmap:
09701199
AG
10356#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
10357 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
a4c075f1
UH
10358 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
10359 || defined(TARGET_S390X)
31e31b8a 10360 {
992f48a0
BS
10361 abi_ulong *v;
10362 abi_ulong v1, v2, v3, v4, v5, v6;
579a97f7 10363 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
2852aafd 10364 return -TARGET_EFAULT;
cbb21eed
MB
10365 v1 = tswapal(v[0]);
10366 v2 = tswapal(v[1]);
10367 v3 = tswapal(v[2]);
10368 v4 = tswapal(v[3]);
10369 v5 = tswapal(v[4]);
10370 v6 = tswapal(v[5]);
53a5960a 10371 unlock_user(v, arg1, 0);
9ab8d071 10372 return do_mmap(v1, v2, v3, v4, v5, v6);
31e31b8a 10373 }
31e31b8a 10374#else
ee1bf83d 10375 /* mmap pointers are always untagged */
9ab8d071 10376 return do_mmap(arg1, arg2, arg3, arg4, arg5, arg6);
31e31b8a 10377#endif
e5febef5 10378#endif
a315a145 10379#ifdef TARGET_NR_mmap2
6fb883e8 10380 case TARGET_NR_mmap2:
bb7ec043 10381#ifndef MMAP_SHIFT
c573ff67 10382#define MMAP_SHIFT 12
c573ff67 10383#endif
9ab8d071
RH
10384 return do_mmap(arg1, arg2, arg3, arg4, arg5,
10385 (off_t)(abi_ulong)arg6 << MMAP_SHIFT);
a315a145 10386#endif
31e31b8a 10387 case TARGET_NR_munmap:
ee1bf83d 10388 arg1 = cpu_untagged_addr(cpu, arg1);
72eb7ea8 10389 return get_errno(target_munmap(arg1, arg2));
9de5e440 10390 case TARGET_NR_mprotect:
ee1bf83d 10391 arg1 = cpu_untagged_addr(cpu, arg1);
97374d38 10392 {
0429a971 10393 TaskState *ts = cpu->opaque;
97374d38
PB
10394 /* Special hack to detect libc making the stack executable. */
10395 if ((arg3 & PROT_GROWSDOWN)
10396 && arg1 >= ts->info->stack_limit
10397 && arg1 <= ts->info->start_stack) {
10398 arg3 &= ~PROT_GROWSDOWN;
10399 arg2 = arg2 + arg1 - ts->info->stack_limit;
10400 arg1 = ts->info->stack_limit;
10401 }
10402 }
72eb7ea8 10403 return get_errno(target_mprotect(arg1, arg2, arg3));
e5febef5 10404#ifdef TARGET_NR_mremap
9de5e440 10405 case TARGET_NR_mremap:
ee1bf83d
RH
10406 arg1 = cpu_untagged_addr(cpu, arg1);
10407 /* mremap new_addr (arg5) is always untagged */
72eb7ea8 10408 return get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
e5febef5 10409#endif
53a5960a 10410 /* ??? msync/mlock/munlock are broken for softmmu. */
e5febef5 10411#ifdef TARGET_NR_msync
9de5e440 10412 case TARGET_NR_msync:
fe080593
HD
10413 return get_errno(msync(g2h(cpu, arg1), arg2,
10414 target_to_host_msync_arg(arg3)));
e5febef5
TS
10415#endif
10416#ifdef TARGET_NR_mlock
9de5e440 10417 case TARGET_NR_mlock:
3e8f1628 10418 return get_errno(mlock(g2h(cpu, arg1), arg2));
e5febef5
TS
10419#endif
10420#ifdef TARGET_NR_munlock
9de5e440 10421 case TARGET_NR_munlock:
3e8f1628 10422 return get_errno(munlock(g2h(cpu, arg1), arg2));
e5febef5
TS
10423#endif
10424#ifdef TARGET_NR_mlockall
9de5e440 10425 case TARGET_NR_mlockall:
72eb7ea8 10426 return get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
e5febef5
TS
10427#endif
10428#ifdef TARGET_NR_munlockall
9de5e440 10429 case TARGET_NR_munlockall:
72eb7ea8 10430 return get_errno(munlockall());
e5febef5 10431#endif
4f7f8924 10432#ifdef TARGET_NR_truncate
31e31b8a 10433 case TARGET_NR_truncate:
579a97f7 10434 if (!(p = lock_user_string(arg1)))
2852aafd 10435 return -TARGET_EFAULT;
53a5960a
PB
10436 ret = get_errno(truncate(p, arg2));
10437 unlock_user(p, arg1, 0);
72eb7ea8 10438 return ret;
4f7f8924
AR
10439#endif
10440#ifdef TARGET_NR_ftruncate
31e31b8a 10441 case TARGET_NR_ftruncate:
72eb7ea8 10442 return get_errno(ftruncate(arg1, arg2));
4f7f8924 10443#endif
31e31b8a 10444 case TARGET_NR_fchmod:
72eb7ea8 10445 return get_errno(fchmod(arg1, arg2));
c0d472b1 10446#if defined(TARGET_NR_fchmodat)
814d7977 10447 case TARGET_NR_fchmodat:
579a97f7 10448 if (!(p = lock_user_string(arg2)))
2852aafd 10449 return -TARGET_EFAULT;
c0d472b1 10450 ret = get_errno(fchmodat(arg1, p, arg3, 0));
579a97f7 10451 unlock_user(p, arg2, 0);
72eb7ea8 10452 return ret;
814d7977 10453#endif
31e31b8a 10454 case TARGET_NR_getpriority:
95c09828
RH
10455 /* Note that negative values are valid for getpriority, so we must
10456 differentiate based on errno settings. */
10457 errno = 0;
10458 ret = getpriority(arg1, arg2);
10459 if (ret == -1 && errno != 0) {
72eb7ea8 10460 return -host_to_target_errno(errno);
95c09828
RH
10461 }
10462#ifdef TARGET_ALPHA
10463 /* Return value is the unbiased priority. Signal no error. */
0effdc29 10464 cpu_env->ir[IR_V0] = 0;
95c09828
RH
10465#else
10466 /* Return value is a biased priority to avoid negative numbers. */
10467 ret = 20 - ret;
10468#endif
72eb7ea8 10469 return ret;
31e31b8a 10470 case TARGET_NR_setpriority:
72eb7ea8 10471 return get_errno(setpriority(arg1, arg2, arg3));
4f7f8924 10472#ifdef TARGET_NR_statfs
31e31b8a 10473 case TARGET_NR_statfs:
2852aafd
RH
10474 if (!(p = lock_user_string(arg1))) {
10475 return -TARGET_EFAULT;
10476 }
53a5960a
PB
10477 ret = get_errno(statfs(path(p), &stfs));
10478 unlock_user(p, arg1, 0);
31e31b8a
FB
10479 convert_statfs:
10480 if (!is_error(ret)) {
53a5960a 10481 struct target_statfs *target_stfs;
3b46e624 10482
579a97f7 10483 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
2852aafd 10484 return -TARGET_EFAULT;
579a97f7
FB
10485 __put_user(stfs.f_type, &target_stfs->f_type);
10486 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
10487 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
10488 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
10489 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
10490 __put_user(stfs.f_files, &target_stfs->f_files);
10491 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
10492 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
10493 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
10494 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
229d3376 10495 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
d4247ec2
SL
10496#ifdef _STATFS_F_FLAGS
10497 __put_user(stfs.f_flags, &target_stfs->f_flags);
10498#else
10499 __put_user(0, &target_stfs->f_flags);
10500#endif
229d3376 10501 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
53a5960a 10502 unlock_user_struct(target_stfs, arg2, 1);
31e31b8a 10503 }
72eb7ea8 10504 return ret;
4f7f8924
AR
10505#endif
10506#ifdef TARGET_NR_fstatfs
31e31b8a 10507 case TARGET_NR_fstatfs:
56c8f68f 10508 ret = get_errno(fstatfs(arg1, &stfs));
31e31b8a 10509 goto convert_statfs;
4f7f8924 10510#endif
56c8f68f
FB
10511#ifdef TARGET_NR_statfs64
10512 case TARGET_NR_statfs64:
2852aafd
RH
10513 if (!(p = lock_user_string(arg1))) {
10514 return -TARGET_EFAULT;
10515 }
53a5960a
PB
10516 ret = get_errno(statfs(path(p), &stfs));
10517 unlock_user(p, arg1, 0);
56c8f68f
FB
10518 convert_statfs64:
10519 if (!is_error(ret)) {
53a5960a 10520 struct target_statfs64 *target_stfs;
3b46e624 10521
579a97f7 10522 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
2852aafd 10523 return -TARGET_EFAULT;
579a97f7
FB
10524 __put_user(stfs.f_type, &target_stfs->f_type);
10525 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
10526 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
10527 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
10528 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
10529 __put_user(stfs.f_files, &target_stfs->f_files);
10530 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
10531 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
10532 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
10533 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
229d3376 10534 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
b94e2b4e
F
10535#ifdef _STATFS_F_FLAGS
10536 __put_user(stfs.f_flags, &target_stfs->f_flags);
10537#else
10538 __put_user(0, &target_stfs->f_flags);
10539#endif
229d3376 10540 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
579a97f7 10541 unlock_user_struct(target_stfs, arg3, 1);
56c8f68f 10542 }
72eb7ea8 10543 return ret;
56c8f68f
FB
10544 case TARGET_NR_fstatfs64:
10545 ret = get_errno(fstatfs(arg1, &stfs));
10546 goto convert_statfs64;
10547#endif
e5febef5 10548#ifdef TARGET_NR_socketcall
31e31b8a 10549 case TARGET_NR_socketcall:
72eb7ea8 10550 return do_socketcall(arg1, arg2);
e5febef5 10551#endif
3532fa74
FB
10552#ifdef TARGET_NR_accept
10553 case TARGET_NR_accept:
72eb7ea8 10554 return do_accept4(arg1, arg2, arg3, 0);
a94b4987
PM
10555#endif
10556#ifdef TARGET_NR_accept4
10557 case TARGET_NR_accept4:
72eb7ea8 10558 return do_accept4(arg1, arg2, arg3, arg4);
3532fa74
FB
10559#endif
10560#ifdef TARGET_NR_bind
10561 case TARGET_NR_bind:
72eb7ea8 10562 return do_bind(arg1, arg2, arg3);
3532fa74
FB
10563#endif
10564#ifdef TARGET_NR_connect
10565 case TARGET_NR_connect:
72eb7ea8 10566 return do_connect(arg1, arg2, arg3);
3532fa74
FB
10567#endif
10568#ifdef TARGET_NR_getpeername
10569 case TARGET_NR_getpeername:
72eb7ea8 10570 return do_getpeername(arg1, arg2, arg3);
3532fa74
FB
10571#endif
10572#ifdef TARGET_NR_getsockname
10573 case TARGET_NR_getsockname:
72eb7ea8 10574 return do_getsockname(arg1, arg2, arg3);
3532fa74
FB
10575#endif
10576#ifdef TARGET_NR_getsockopt
10577 case TARGET_NR_getsockopt:
72eb7ea8 10578 return do_getsockopt(arg1, arg2, arg3, arg4, arg5);
3532fa74
FB
10579#endif
10580#ifdef TARGET_NR_listen
10581 case TARGET_NR_listen:
72eb7ea8 10582 return get_errno(listen(arg1, arg2));
3532fa74
FB
10583#endif
10584#ifdef TARGET_NR_recv
10585 case TARGET_NR_recv:
72eb7ea8 10586 return do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
3532fa74
FB
10587#endif
10588#ifdef TARGET_NR_recvfrom
10589 case TARGET_NR_recvfrom:
72eb7ea8 10590 return do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
3532fa74
FB
10591#endif
10592#ifdef TARGET_NR_recvmsg
10593 case TARGET_NR_recvmsg:
72eb7ea8 10594 return do_sendrecvmsg(arg1, arg2, arg3, 0);
3532fa74
FB
10595#endif
10596#ifdef TARGET_NR_send
10597 case TARGET_NR_send:
72eb7ea8 10598 return do_sendto(arg1, arg2, arg3, arg4, 0, 0);
3532fa74
FB
10599#endif
10600#ifdef TARGET_NR_sendmsg
10601 case TARGET_NR_sendmsg:
72eb7ea8 10602 return do_sendrecvmsg(arg1, arg2, arg3, 1);
3532fa74 10603#endif
f19e00d7
AG
10604#ifdef TARGET_NR_sendmmsg
10605 case TARGET_NR_sendmmsg:
72eb7ea8 10606 return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
859e8a89
AF
10607#endif
10608#ifdef TARGET_NR_recvmmsg
f19e00d7 10609 case TARGET_NR_recvmmsg:
72eb7ea8 10610 return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
f19e00d7 10611#endif
3532fa74
FB
10612#ifdef TARGET_NR_sendto
10613 case TARGET_NR_sendto:
72eb7ea8 10614 return do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
3532fa74
FB
10615#endif
10616#ifdef TARGET_NR_shutdown
10617 case TARGET_NR_shutdown:
72eb7ea8 10618 return get_errno(shutdown(arg1, arg2));
3532fa74 10619#endif
f894efd1
LV
10620#if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
10621 case TARGET_NR_getrandom:
10622 p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
10623 if (!p) {
2852aafd 10624 return -TARGET_EFAULT;
f894efd1
LV
10625 }
10626 ret = get_errno(getrandom(p, arg2, arg3));
10627 unlock_user(p, arg1, ret);
72eb7ea8 10628 return ret;
f894efd1 10629#endif
3532fa74
FB
10630#ifdef TARGET_NR_socket
10631 case TARGET_NR_socket:
72eb7ea8 10632 return do_socket(arg1, arg2, arg3);
3532fa74
FB
10633#endif
10634#ifdef TARGET_NR_socketpair
10635 case TARGET_NR_socketpair:
72eb7ea8 10636 return do_socketpair(arg1, arg2, arg3, arg4);
3532fa74
FB
10637#endif
10638#ifdef TARGET_NR_setsockopt
10639 case TARGET_NR_setsockopt:
72eb7ea8 10640 return do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
3532fa74 10641#endif
da2c8ad7 10642#if defined(TARGET_NR_syslog)
31e31b8a 10643 case TARGET_NR_syslog:
da2c8ad7
AM
10644 {
10645 int len = arg2;
7494b0f9 10646
da2c8ad7
AM
10647 switch (arg1) {
10648 case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */
10649 case TARGET_SYSLOG_ACTION_OPEN: /* Open log */
10650 case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
10651 case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */
10652 case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */
10653 case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */
10654 case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */
10655 case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */
72eb7ea8 10656 return get_errno(sys_syslog((int)arg1, NULL, (int)arg3));
da2c8ad7
AM
10657 case TARGET_SYSLOG_ACTION_READ: /* Read from log */
10658 case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */
10659 case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */
10660 {
da2c8ad7 10661 if (len < 0) {
259841c1 10662 return -TARGET_EINVAL;
da2c8ad7 10663 }
da2c8ad7 10664 if (len == 0) {
72eb7ea8 10665 return 0;
da2c8ad7
AM
10666 }
10667 p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
10668 if (!p) {
259841c1 10669 return -TARGET_EFAULT;
da2c8ad7
AM
10670 }
10671 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
10672 unlock_user(p, arg2, arg3);
10673 }
72eb7ea8 10674 return ret;
da2c8ad7 10675 default:
72eb7ea8 10676 return -TARGET_EINVAL;
da2c8ad7
AM
10677 }
10678 }
10679 break;
10680#endif
31e31b8a 10681 case TARGET_NR_setitimer:
66fb9763 10682 {
66fb9763
FB
10683 struct itimerval value, ovalue, *pvalue;
10684
53a5960a 10685 if (arg2) {
66fb9763 10686 pvalue = &value;
788f5ec4
TS
10687 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
10688 || copy_from_user_timeval(&pvalue->it_value,
10689 arg2 + sizeof(struct target_timeval)))
2852aafd 10690 return -TARGET_EFAULT;
66fb9763
FB
10691 } else {
10692 pvalue = NULL;
10693 }
10694 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
53a5960a 10695 if (!is_error(ret) && arg3) {
788f5ec4
TS
10696 if (copy_to_user_timeval(arg3,
10697 &ovalue.it_interval)
10698 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
10699 &ovalue.it_value))
2852aafd 10700 return -TARGET_EFAULT;
66fb9763
FB
10701 }
10702 }
72eb7ea8 10703 return ret;
31e31b8a 10704 case TARGET_NR_getitimer:
66fb9763 10705 {
66fb9763 10706 struct itimerval value;
3b46e624 10707
66fb9763 10708 ret = get_errno(getitimer(arg1, &value));
53a5960a 10709 if (!is_error(ret) && arg2) {
788f5ec4
TS
10710 if (copy_to_user_timeval(arg2,
10711 &value.it_interval)
10712 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
10713 &value.it_value))
2852aafd 10714 return -TARGET_EFAULT;
66fb9763
FB
10715 }
10716 }
72eb7ea8 10717 return ret;
704eff6c 10718#ifdef TARGET_NR_stat
31e31b8a 10719 case TARGET_NR_stat:
2852aafd
RH
10720 if (!(p = lock_user_string(arg1))) {
10721 return -TARGET_EFAULT;
10722 }
53a5960a
PB
10723 ret = get_errno(stat(path(p), &st));
10724 unlock_user(p, arg1, 0);
31e31b8a 10725 goto do_stat;
704eff6c
CG
10726#endif
10727#ifdef TARGET_NR_lstat
31e31b8a 10728 case TARGET_NR_lstat:
2852aafd
RH
10729 if (!(p = lock_user_string(arg1))) {
10730 return -TARGET_EFAULT;
10731 }
53a5960a
PB
10732 ret = get_errno(lstat(path(p), &st));
10733 unlock_user(p, arg1, 0);
31e31b8a 10734 goto do_stat;
704eff6c 10735#endif
4f7f8924 10736#ifdef TARGET_NR_fstat
31e31b8a
FB
10737 case TARGET_NR_fstat:
10738 {
10739 ret = get_errno(fstat(arg1, &st));
704eff6c 10740#if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
31e31b8a 10741 do_stat:
704eff6c 10742#endif
31e31b8a 10743 if (!is_error(ret)) {
53a5960a 10744 struct target_stat *target_st;
e3584658 10745
579a97f7 10746 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
2852aafd 10747 return -TARGET_EFAULT;
12727917 10748 memset(target_st, 0, sizeof(*target_st));
d2fd1af7
FB
10749 __put_user(st.st_dev, &target_st->st_dev);
10750 __put_user(st.st_ino, &target_st->st_ino);
10751 __put_user(st.st_mode, &target_st->st_mode);
10752 __put_user(st.st_uid, &target_st->st_uid);
10753 __put_user(st.st_gid, &target_st->st_gid);
10754 __put_user(st.st_nlink, &target_st->st_nlink);
10755 __put_user(st.st_rdev, &target_st->st_rdev);
10756 __put_user(st.st_size, &target_st->st_size);
10757 __put_user(st.st_blksize, &target_st->st_blksize);
10758 __put_user(st.st_blocks, &target_st->st_blocks);
10759 __put_user(st.st_atime, &target_st->target_st_atime);
10760 __put_user(st.st_mtime, &target_st->target_st_mtime);
10761 __put_user(st.st_ctime, &target_st->target_st_ctime);
febf6fad 10762#if defined(HAVE_STRUCT_STAT_ST_ATIM) && defined(TARGET_STAT_HAVE_NSEC)
5f992db6
CYT
10763 __put_user(st.st_atim.tv_nsec,
10764 &target_st->target_st_atime_nsec);
10765 __put_user(st.st_mtim.tv_nsec,
10766 &target_st->target_st_mtime_nsec);
10767 __put_user(st.st_ctim.tv_nsec,
10768 &target_st->target_st_ctime_nsec);
10769#endif
53a5960a 10770 unlock_user_struct(target_st, arg2, 1);
31e31b8a
FB
10771 }
10772 }
72eb7ea8 10773 return ret;
ebc05488 10774#endif
31e31b8a 10775 case TARGET_NR_vhangup:
72eb7ea8 10776 return get_errno(vhangup());
42ad6ae9
FB
10777#ifdef TARGET_NR_syscall
10778 case TARGET_NR_syscall:
72eb7ea8
RH
10779 return do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
10780 arg6, arg7, arg8, 0);
ebc05488 10781#endif
859e8a89 10782#if defined(TARGET_NR_wait4)
31e31b8a
FB
10783 case TARGET_NR_wait4:
10784 {
10785 int status;
992f48a0 10786 abi_long status_ptr = arg2;
31e31b8a 10787 struct rusage rusage, *rusage_ptr;
992f48a0 10788 abi_ulong target_rusage = arg4;
a39fb273 10789 abi_long rusage_err;
31e31b8a
FB
10790 if (target_rusage)
10791 rusage_ptr = &rusage;
10792 else
10793 rusage_ptr = NULL;
4af80a37 10794 ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
31e31b8a 10795 if (!is_error(ret)) {
5379557b 10796 if (status_ptr && ret) {
1d9d8b55 10797 status = host_to_target_waitstatus(status);
2f619698 10798 if (put_user_s32(status, status_ptr))
2852aafd 10799 return -TARGET_EFAULT;
31e31b8a 10800 }
a39fb273
PJ
10801 if (target_rusage) {
10802 rusage_err = host_to_target_rusage(target_rusage, &rusage);
10803 if (rusage_err) {
10804 ret = rusage_err;
10805 }
10806 }
31e31b8a
FB
10807 }
10808 }
72eb7ea8 10809 return ret;
859e8a89 10810#endif
e5febef5 10811#ifdef TARGET_NR_swapoff
31e31b8a 10812 case TARGET_NR_swapoff:
579a97f7 10813 if (!(p = lock_user_string(arg1)))
2852aafd 10814 return -TARGET_EFAULT;
53a5960a
PB
10815 ret = get_errno(swapoff(p));
10816 unlock_user(p, arg1, 0);
72eb7ea8 10817 return ret;
e5febef5 10818#endif
31e31b8a 10819 case TARGET_NR_sysinfo:
a5448a7d 10820 {
53a5960a 10821 struct target_sysinfo *target_value;
a5448a7d
FB
10822 struct sysinfo value;
10823 ret = get_errno(sysinfo(&value));
53a5960a 10824 if (!is_error(ret) && arg1)
a5448a7d 10825 {
579a97f7 10826 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
2852aafd 10827 return -TARGET_EFAULT;
a5448a7d
FB
10828 __put_user(value.uptime, &target_value->uptime);
10829 __put_user(value.loads[0], &target_value->loads[0]);
10830 __put_user(value.loads[1], &target_value->loads[1]);
10831 __put_user(value.loads[2], &target_value->loads[2]);
10832 __put_user(value.totalram, &target_value->totalram);
10833 __put_user(value.freeram, &target_value->freeram);
10834 __put_user(value.sharedram, &target_value->sharedram);
10835 __put_user(value.bufferram, &target_value->bufferram);
10836 __put_user(value.totalswap, &target_value->totalswap);
10837 __put_user(value.freeswap, &target_value->freeswap);
10838 __put_user(value.procs, &target_value->procs);
10839 __put_user(value.totalhigh, &target_value->totalhigh);
10840 __put_user(value.freehigh, &target_value->freehigh);
10841 __put_user(value.mem_unit, &target_value->mem_unit);
53a5960a 10842 unlock_user_struct(target_value, arg1, 1);
a5448a7d
FB
10843 }
10844 }
72eb7ea8 10845 return ret;
e5febef5 10846#ifdef TARGET_NR_ipc
31e31b8a 10847 case TARGET_NR_ipc:
72eb7ea8 10848 return do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
e5febef5 10849#endif
e5289087
AJ
10850#ifdef TARGET_NR_semget
10851 case TARGET_NR_semget:
72eb7ea8 10852 return get_errno(semget(arg1, arg2, arg3));
e5289087
AJ
10853#endif
10854#ifdef TARGET_NR_semop
10855 case TARGET_NR_semop:
cac46eb0 10856 return do_semtimedop(arg1, arg2, arg3, 0, false);
d8c08b1e
MK
10857#endif
10858#ifdef TARGET_NR_semtimedop
10859 case TARGET_NR_semtimedop:
cac46eb0
FB
10860 return do_semtimedop(arg1, arg2, arg3, arg4, false);
10861#endif
10862#ifdef TARGET_NR_semtimedop_time64
10863 case TARGET_NR_semtimedop_time64:
10864 return do_semtimedop(arg1, arg2, arg3, arg4, true);
e5289087
AJ
10865#endif
10866#ifdef TARGET_NR_semctl
10867 case TARGET_NR_semctl:
72eb7ea8 10868 return do_semctl(arg1, arg2, arg3, arg4);
e5289087 10869#endif
eeb438c1
AJ
10870#ifdef TARGET_NR_msgctl
10871 case TARGET_NR_msgctl:
72eb7ea8 10872 return do_msgctl(arg1, arg2, arg3);
eeb438c1
AJ
10873#endif
10874#ifdef TARGET_NR_msgget
10875 case TARGET_NR_msgget:
72eb7ea8 10876 return get_errno(msgget(arg1, arg2));
eeb438c1
AJ
10877#endif
10878#ifdef TARGET_NR_msgrcv
10879 case TARGET_NR_msgrcv:
72eb7ea8 10880 return do_msgrcv(arg1, arg2, arg3, arg4, arg5);
eeb438c1
AJ
10881#endif
10882#ifdef TARGET_NR_msgsnd
10883 case TARGET_NR_msgsnd:
72eb7ea8 10884 return do_msgsnd(arg1, arg2, arg3, arg4);
88a8c984
RV
10885#endif
10886#ifdef TARGET_NR_shmget
10887 case TARGET_NR_shmget:
72eb7ea8 10888 return get_errno(shmget(arg1, arg2, arg3));
88a8c984
RV
10889#endif
10890#ifdef TARGET_NR_shmctl
10891 case TARGET_NR_shmctl:
72eb7ea8 10892 return do_shmctl(arg1, arg2, arg3);
88a8c984
RV
10893#endif
10894#ifdef TARGET_NR_shmat
10895 case TARGET_NR_shmat:
225a206c 10896 return target_shmat(cpu_env, arg1, arg2, arg3);
88a8c984
RV
10897#endif
10898#ifdef TARGET_NR_shmdt
10899 case TARGET_NR_shmdt:
225a206c 10900 return target_shmdt(arg1);
eeb438c1 10901#endif
31e31b8a 10902 case TARGET_NR_fsync:
72eb7ea8 10903 return get_errno(fsync(arg1));
31e31b8a 10904 case TARGET_NR_clone:
4ce6243d
PM
10905 /* Linux manages to have three different orderings for its
10906 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
10907 * match the kernel's CONFIG_CLONE_* settings.
10908 * Microblaze is further special in that it uses a sixth
10909 * implicit argument to clone for the TLS pointer.
10910 */
10911#if defined(TARGET_MICROBLAZE)
a5b3bdcb 10912 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
4ce6243d
PM
10913#elif defined(TARGET_CLONE_BACKWARDS)
10914 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
10915#elif defined(TARGET_CLONE_BACKWARDS2)
a4c075f1 10916 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
0b6d3ae0 10917#else
4ce6243d 10918 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
0b6d3ae0 10919#endif
72eb7ea8 10920 return ret;
ec86b0fb
FB
10921#ifdef __NR_exit_group
10922 /* new thread calls */
10923 case TARGET_NR_exit_group:
708b6a64 10924 preexit_cleanup(cpu_env, arg1);
72eb7ea8 10925 return get_errno(exit_group(arg1));
ec86b0fb 10926#endif
31e31b8a 10927 case TARGET_NR_setdomainname:
579a97f7 10928 if (!(p = lock_user_string(arg1)))
2852aafd 10929 return -TARGET_EFAULT;
53a5960a
PB
10930 ret = get_errno(setdomainname(p, arg2));
10931 unlock_user(p, arg1, 0);
72eb7ea8 10932 return ret;
31e31b8a
FB
10933 case TARGET_NR_uname:
10934 /* no need to transcode because we use the linux syscall */
29e619b1
FB
10935 {
10936 struct new_utsname * buf;
3b46e624 10937
579a97f7 10938 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
2852aafd 10939 return -TARGET_EFAULT;
29e619b1
FB
10940 ret = get_errno(sys_uname(buf));
10941 if (!is_error(ret)) {
332c9781 10942 /* Overwrite the native machine name with whatever is being
29e619b1 10943 emulated. */
871f95c6
PMD
10944 g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env),
10945 sizeof(buf->machine));
c5937220 10946 /* Allow the user to override the reported release. */
332c9781
PM
10947 if (qemu_uname_release && *qemu_uname_release) {
10948 g_strlcpy(buf->release, qemu_uname_release,
10949 sizeof(buf->release));
10950 }
29e619b1 10951 }
53a5960a 10952 unlock_user_struct(buf, arg1, 1);
29e619b1 10953 }
72eb7ea8 10954 return ret;
6dbad63e 10955#ifdef TARGET_I386
31e31b8a 10956 case TARGET_NR_modify_ldt:
72eb7ea8 10957 return do_modify_ldt(cpu_env, arg1, arg2, arg3);
84409ddb 10958#if !defined(TARGET_X86_64)
5cd4393b 10959 case TARGET_NR_vm86:
72eb7ea8 10960 return do_vm86(cpu_env, arg1, arg2);
84409ddb 10961#endif
6dbad63e 10962#endif
859e8a89 10963#if defined(TARGET_NR_adjtimex)
31e31b8a 10964 case TARGET_NR_adjtimex:
19f59bce
AM
10965 {
10966 struct timex host_buf;
10967
10968 if (target_to_host_timex(&host_buf, arg1) != 0) {
2852aafd 10969 return -TARGET_EFAULT;
19f59bce
AM
10970 }
10971 ret = get_errno(adjtimex(&host_buf));
10972 if (!is_error(ret)) {
10973 if (host_to_target_timex(arg1, &host_buf) != 0) {
2852aafd 10974 return -TARGET_EFAULT;
19f59bce
AM
10975 }
10976 }
10977 }
72eb7ea8 10978 return ret;
859e8a89 10979#endif
38860a03
AM
10980#if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
10981 case TARGET_NR_clock_adjtime:
10982 {
aab74610 10983 struct timex htx;
38860a03 10984
aab74610 10985 if (target_to_host_timex(&htx, arg2) != 0) {
2852aafd 10986 return -TARGET_EFAULT;
38860a03 10987 }
aab74610
PM
10988 ret = get_errno(clock_adjtime(arg1, &htx));
10989 if (!is_error(ret) && host_to_target_timex(arg2, &htx)) {
10990 return -TARGET_EFAULT;
38860a03
AM
10991 }
10992 }
72eb7ea8 10993 return ret;
6ac03b2c
FB
10994#endif
10995#if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
10996 case TARGET_NR_clock_adjtime64:
10997 {
10998 struct timex htx;
10999
11000 if (target_to_host_timex64(&htx, arg2) != 0) {
11001 return -TARGET_EFAULT;
11002 }
11003 ret = get_errno(clock_adjtime(arg1, &htx));
11004 if (!is_error(ret) && host_to_target_timex64(arg2, &htx)) {
11005 return -TARGET_EFAULT;
11006 }
11007 }
11008 return ret;
38860a03 11009#endif
31e31b8a 11010 case TARGET_NR_getpgid:
72eb7ea8 11011 return get_errno(getpgid(arg1));
31e31b8a 11012 case TARGET_NR_fchdir:
72eb7ea8 11013 return get_errno(fchdir(arg1));
31e31b8a 11014 case TARGET_NR_personality:
72eb7ea8 11015 return get_errno(personality(arg1));
7a3148a9 11016#ifdef TARGET_NR__llseek /* Not on alpha */
31e31b8a
FB
11017 case TARGET_NR__llseek:
11018 {
0c1592d9 11019 int64_t res;
d35b261c 11020#if !defined(__NR_llseek)
9fea273c 11021 res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5);
0c1592d9
PM
11022 if (res == -1) {
11023 ret = get_errno(res);
11024 } else {
11025 ret = 0;
11026 }
4f2ac237 11027#else
31e31b8a 11028 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
4f2ac237 11029#endif
0c1592d9 11030 if ((ret == 0) && put_user_s64(res, arg4)) {
2852aafd 11031 return -TARGET_EFAULT;
0c1592d9 11032 }
31e31b8a 11033 }
72eb7ea8 11034 return ret;
7a3148a9 11035#endif
704eff6c 11036#ifdef TARGET_NR_getdents
31e31b8a 11037 case TARGET_NR_getdents:
fd08ddb9 11038 return do_getdents(arg1, arg2, arg3);
704eff6c 11039#endif /* TARGET_NR_getdents */
3ae43202 11040#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
dab2ed99 11041 case TARGET_NR_getdents64:
fd08ddb9 11042 return do_getdents64(arg1, arg2, arg3);
a541f297 11043#endif /* TARGET_NR_getdents64 */
9468a5d4 11044#if defined(TARGET_NR__newselect)
31e31b8a 11045 case TARGET_NR__newselect:
72eb7ea8 11046 return do_select(arg1, arg2, arg3, arg4, arg5);
e5febef5 11047#endif
e5ce9688 11048#ifdef TARGET_NR_poll
9de5e440 11049 case TARGET_NR_poll:
e5ce9688
FB
11050 return do_ppoll(arg1, arg2, arg3, arg4, arg5, false, false);
11051#endif
11052#ifdef TARGET_NR_ppoll
d8035d4c 11053 case TARGET_NR_ppoll:
e5ce9688
FB
11054 return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, false);
11055#endif
11056#ifdef TARGET_NR_ppoll_time64
11057 case TARGET_NR_ppoll_time64:
11058 return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, true);
e5febef5 11059#endif
31e31b8a 11060 case TARGET_NR_flock:
9de5e440
FB
11061 /* NOTE: the flock constant seems to be the same for every
11062 Linux platform */
72eb7ea8 11063 return get_errno(safe_flock(arg1, arg2));
31e31b8a
FB
11064 case TARGET_NR_readv:
11065 {
f287b2c2
RH
11066 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
11067 if (vec != NULL) {
918c03ed 11068 ret = get_errno(safe_readv(arg1, vec, arg3));
f287b2c2
RH
11069 unlock_iovec(vec, arg2, arg3, 1);
11070 } else {
11071 ret = -host_to_target_errno(errno);
11072 }
31e31b8a 11073 }
72eb7ea8 11074 return ret;
31e31b8a
FB
11075 case TARGET_NR_writev:
11076 {
f287b2c2
RH
11077 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
11078 if (vec != NULL) {
918c03ed 11079 ret = get_errno(safe_writev(arg1, vec, arg3));
f287b2c2
RH
11080 unlock_iovec(vec, arg2, arg3, 0);
11081 } else {
11082 ret = -host_to_target_errno(errno);
11083 }
31e31b8a 11084 }
72eb7ea8 11085 return ret;
0f26386c
DJ
11086#if defined(TARGET_NR_preadv)
11087 case TARGET_NR_preadv:
11088 {
11089 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
11090 if (vec != NULL) {
9ac22517
MF
11091 unsigned long low, high;
11092
11093 target_to_host_low_high(arg4, arg5, &low, &high);
11094 ret = get_errno(safe_preadv(arg1, vec, arg3, low, high));
0f26386c
DJ
11095 unlock_iovec(vec, arg2, arg3, 1);
11096 } else {
11097 ret = -host_to_target_errno(errno);
f8d00fba
DJ
11098 }
11099 }
72eb7ea8 11100 return ret;
f8d00fba
DJ
11101#endif
11102#if defined(TARGET_NR_pwritev)
11103 case TARGET_NR_pwritev:
11104 {
11105 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
11106 if (vec != NULL) {
9ac22517
MF
11107 unsigned long low, high;
11108
11109 target_to_host_low_high(arg4, arg5, &low, &high);
11110 ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high));
f8d00fba
DJ
11111 unlock_iovec(vec, arg2, arg3, 0);
11112 } else {
11113 ret = -host_to_target_errno(errno);
0f26386c
DJ
11114 }
11115 }
72eb7ea8 11116 return ret;
0f26386c 11117#endif
31e31b8a 11118 case TARGET_NR_getsid:
72eb7ea8 11119 return get_errno(getsid(arg1));
7a3148a9 11120#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
31e31b8a 11121 case TARGET_NR_fdatasync:
72eb7ea8 11122 return get_errno(fdatasync(arg1));
704eff6c 11123#endif
737de1d1
MF
11124 case TARGET_NR_sched_getaffinity:
11125 {
11126 unsigned int mask_size;
11127 unsigned long *mask;
11128
11129 /*
11130 * sched_getaffinity needs multiples of ulong, so need to take
11131 * care of mismatches between target ulong and host ulong sizes.
11132 */
11133 if (arg2 & (sizeof(abi_ulong) - 1)) {
72eb7ea8 11134 return -TARGET_EINVAL;
737de1d1
MF
11135 }
11136 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
11137
11138 mask = alloca(mask_size);
2e0a8713 11139 memset(mask, 0, mask_size);
737de1d1
MF
11140 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
11141
11142 if (!is_error(ret)) {
be3bd286
PM
11143 if (ret > arg2) {
11144 /* More data returned than the caller's buffer will fit.
11145 * This only happens if sizeof(abi_long) < sizeof(long)
11146 * and the caller passed us a buffer holding an odd number
11147 * of abi_longs. If the host kernel is actually using the
11148 * extra 4 bytes then fail EINVAL; otherwise we can just
11149 * ignore them and only copy the interesting part.
11150 */
11151 int numcpus = sysconf(_SC_NPROCESSORS_CONF);
11152 if (numcpus > arg2 * 8) {
72eb7ea8 11153 return -TARGET_EINVAL;
be3bd286
PM
11154 }
11155 ret = arg2;
11156 }
11157
5fdefcf8 11158 if (host_to_target_cpu_mask(mask, mask_size, arg3, ret)) {
2852aafd 11159 return -TARGET_EFAULT;
5fdefcf8 11160 }
737de1d1
MF
11161 }
11162 }
72eb7ea8 11163 return ret;
737de1d1
MF
11164 case TARGET_NR_sched_setaffinity:
11165 {
11166 unsigned int mask_size;
11167 unsigned long *mask;
11168
11169 /*
11170 * sched_setaffinity needs multiples of ulong, so need to take
11171 * care of mismatches between target ulong and host ulong sizes.
11172 */
11173 if (arg2 & (sizeof(abi_ulong) - 1)) {
72eb7ea8 11174 return -TARGET_EINVAL;
737de1d1
MF
11175 }
11176 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
737de1d1 11177 mask = alloca(mask_size);
2e0a8713
ST
11178
11179 ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2);
11180 if (ret) {
72eb7ea8 11181 return ret;
737de1d1 11182 }
737de1d1 11183
72eb7ea8 11184 return get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
737de1d1 11185 }
b827c3ed
ST
11186 case TARGET_NR_getcpu:
11187 {
9962b7c2
LV
11188 unsigned cpuid, node;
11189 ret = get_errno(sys_getcpu(arg1 ? &cpuid : NULL,
b827c3ed
ST
11190 arg2 ? &node : NULL,
11191 NULL));
11192 if (is_error(ret)) {
259841c1 11193 return ret;
b827c3ed 11194 }
9962b7c2 11195 if (arg1 && put_user_u32(cpuid, arg1)) {
2852aafd 11196 return -TARGET_EFAULT;
b827c3ed
ST
11197 }
11198 if (arg2 && put_user_u32(node, arg2)) {
2852aafd 11199 return -TARGET_EFAULT;
b827c3ed
ST
11200 }
11201 }
72eb7ea8 11202 return ret;
31e31b8a 11203 case TARGET_NR_sched_setparam:
5cd4393b 11204 {
407a119b 11205 struct target_sched_param *target_schp;
5cd4393b 11206 struct sched_param schp;
53a5960a 11207
a1d5c5b2
TM
11208 if (arg2 == 0) {
11209 return -TARGET_EINVAL;
11210 }
407a119b 11211 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1)) {
2852aafd 11212 return -TARGET_EFAULT;
407a119b 11213 }
5cd4393b 11214 schp.sched_priority = tswap32(target_schp->sched_priority);
53a5960a 11215 unlock_user_struct(target_schp, arg2, 0);
407a119b 11216 return get_errno(sys_sched_setparam(arg1, &schp));
5cd4393b 11217 }
31e31b8a 11218 case TARGET_NR_sched_getparam:
5cd4393b 11219 {
407a119b 11220 struct target_sched_param *target_schp;
5cd4393b 11221 struct sched_param schp;
a1d5c5b2
TM
11222
11223 if (arg2 == 0) {
11224 return -TARGET_EINVAL;
11225 }
407a119b 11226 ret = get_errno(sys_sched_getparam(arg1, &schp));
5cd4393b 11227 if (!is_error(ret)) {
407a119b 11228 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0)) {
2852aafd 11229 return -TARGET_EFAULT;
407a119b 11230 }
5cd4393b 11231 target_schp->sched_priority = tswap32(schp.sched_priority);
53a5960a 11232 unlock_user_struct(target_schp, arg2, 1);
5cd4393b
FB
11233 }
11234 }
72eb7ea8 11235 return ret;
31e31b8a 11236 case TARGET_NR_sched_setscheduler:
5cd4393b 11237 {
407a119b 11238 struct target_sched_param *target_schp;
5cd4393b 11239 struct sched_param schp;
a1d5c5b2
TM
11240 if (arg3 == 0) {
11241 return -TARGET_EINVAL;
11242 }
407a119b 11243 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1)) {
2852aafd 11244 return -TARGET_EFAULT;
407a119b 11245 }
5cd4393b 11246 schp.sched_priority = tswap32(target_schp->sched_priority);
53a5960a 11247 unlock_user_struct(target_schp, arg3, 0);
407a119b 11248 return get_errno(sys_sched_setscheduler(arg1, arg2, &schp));
5cd4393b 11249 }
31e31b8a 11250 case TARGET_NR_sched_getscheduler:
407a119b 11251 return get_errno(sys_sched_getscheduler(arg1));
45ad761c
TT
11252 case TARGET_NR_sched_getattr:
11253 {
11254 struct target_sched_attr *target_scha;
11255 struct sched_attr scha;
11256 if (arg2 == 0) {
11257 return -TARGET_EINVAL;
11258 }
11259 if (arg3 > sizeof(scha)) {
11260 arg3 = sizeof(scha);
11261 }
11262 ret = get_errno(sys_sched_getattr(arg1, &scha, arg3, arg4));
11263 if (!is_error(ret)) {
11264 target_scha = lock_user(VERIFY_WRITE, arg2, arg3, 0);
11265 if (!target_scha) {
11266 return -TARGET_EFAULT;
11267 }
11268 target_scha->size = tswap32(scha.size);
11269 target_scha->sched_policy = tswap32(scha.sched_policy);
11270 target_scha->sched_flags = tswap64(scha.sched_flags);
11271 target_scha->sched_nice = tswap32(scha.sched_nice);
11272 target_scha->sched_priority = tswap32(scha.sched_priority);
11273 target_scha->sched_runtime = tswap64(scha.sched_runtime);
11274 target_scha->sched_deadline = tswap64(scha.sched_deadline);
11275 target_scha->sched_period = tswap64(scha.sched_period);
11276 if (scha.size > offsetof(struct sched_attr, sched_util_min)) {
11277 target_scha->sched_util_min = tswap32(scha.sched_util_min);
11278 target_scha->sched_util_max = tswap32(scha.sched_util_max);
11279 }
11280 unlock_user(target_scha, arg2, arg3);
11281 }
11282 return ret;
11283 }
11284 case TARGET_NR_sched_setattr:
11285 {
11286 struct target_sched_attr *target_scha;
11287 struct sched_attr scha;
11288 uint32_t size;
11289 int zeroed;
11290 if (arg2 == 0) {
11291 return -TARGET_EINVAL;
11292 }
11293 if (get_user_u32(size, arg2)) {
11294 return -TARGET_EFAULT;
11295 }
11296 if (!size) {
11297 size = offsetof(struct target_sched_attr, sched_util_min);
11298 }
11299 if (size < offsetof(struct target_sched_attr, sched_util_min)) {
11300 if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
11301 return -TARGET_EFAULT;
11302 }
11303 return -TARGET_E2BIG;
11304 }
11305
11306 zeroed = check_zeroed_user(arg2, sizeof(struct target_sched_attr), size);
11307 if (zeroed < 0) {
11308 return zeroed;
11309 } else if (zeroed == 0) {
11310 if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
11311 return -TARGET_EFAULT;
11312 }
11313 return -TARGET_E2BIG;
11314 }
11315 if (size > sizeof(struct target_sched_attr)) {
11316 size = sizeof(struct target_sched_attr);
11317 }
11318
11319 target_scha = lock_user(VERIFY_READ, arg2, size, 1);
11320 if (!target_scha) {
11321 return -TARGET_EFAULT;
11322 }
11323 scha.size = size;
11324 scha.sched_policy = tswap32(target_scha->sched_policy);
11325 scha.sched_flags = tswap64(target_scha->sched_flags);
11326 scha.sched_nice = tswap32(target_scha->sched_nice);
11327 scha.sched_priority = tswap32(target_scha->sched_priority);
11328 scha.sched_runtime = tswap64(target_scha->sched_runtime);
11329 scha.sched_deadline = tswap64(target_scha->sched_deadline);
11330 scha.sched_period = tswap64(target_scha->sched_period);
11331 if (size > offsetof(struct target_sched_attr, sched_util_min)) {
11332 scha.sched_util_min = tswap32(target_scha->sched_util_min);
11333 scha.sched_util_max = tswap32(target_scha->sched_util_max);
11334 }
11335 unlock_user(target_scha, arg2, 0);
11336 return get_errno(sys_sched_setattr(arg1, &scha, arg3));
11337 }
31e31b8a 11338 case TARGET_NR_sched_yield:
72eb7ea8 11339 return get_errno(sched_yield());
31e31b8a 11340 case TARGET_NR_sched_get_priority_max:
72eb7ea8 11341 return get_errno(sched_get_priority_max(arg1));
31e31b8a 11342 case TARGET_NR_sched_get_priority_min:
72eb7ea8 11343 return get_errno(sched_get_priority_min(arg1));
859e8a89 11344#ifdef TARGET_NR_sched_rr_get_interval
31e31b8a 11345 case TARGET_NR_sched_rr_get_interval:
5cd4393b 11346 {
5cd4393b
FB
11347 struct timespec ts;
11348 ret = get_errno(sched_rr_get_interval(arg1, &ts));
11349 if (!is_error(ret)) {
d4290c40 11350 ret = host_to_target_timespec(arg2, &ts);
5cd4393b
FB
11351 }
11352 }
72eb7ea8 11353 return ret;
859e8a89 11354#endif
ddcbde15
FB
11355#ifdef TARGET_NR_sched_rr_get_interval_time64
11356 case TARGET_NR_sched_rr_get_interval_time64:
11357 {
11358 struct timespec ts;
11359 ret = get_errno(sched_rr_get_interval(arg1, &ts));
11360 if (!is_error(ret)) {
11361 ret = host_to_target_timespec64(arg2, &ts);
11362 }
11363 }
11364 return ret;
11365#endif
859e8a89 11366#if defined(TARGET_NR_nanosleep)
31e31b8a 11367 case TARGET_NR_nanosleep:
1b6b029e 11368 {
1b6b029e 11369 struct timespec req, rem;
53a5960a 11370 target_to_host_timespec(&req, arg1);
9e518226 11371 ret = get_errno(safe_nanosleep(&req, &rem));
53a5960a
PB
11372 if (is_error(ret) && arg2) {
11373 host_to_target_timespec(arg2, &rem);
1b6b029e
FB
11374 }
11375 }
72eb7ea8 11376 return ret;
859e8a89 11377#endif
31e31b8a 11378 case TARGET_NR_prctl:
87e9bf23 11379 return do_prctl(cpu_env, arg1, arg2, arg3, arg4, arg5);
39b9aae1 11380 break;
d2fd1af7
FB
11381#ifdef TARGET_NR_arch_prctl
11382 case TARGET_NR_arch_prctl:
72eb7ea8 11383 return do_arch_prctl(cpu_env, arg1, arg2);
d2fd1af7 11384#endif
f2c7ba15
AJ
11385#ifdef TARGET_NR_pread64
11386 case TARGET_NR_pread64:
8bf8e9df 11387 if (regpairs_aligned(cpu_env, num)) {
ae017a5b
AG
11388 arg4 = arg5;
11389 arg5 = arg6;
11390 }
2bd3f899
PM
11391 if (arg2 == 0 && arg3 == 0) {
11392 /* Special-case NULL buffer and zero length, which should succeed */
11393 p = 0;
11394 } else {
11395 p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
11396 if (!p) {
11397 return -TARGET_EFAULT;
11398 }
11399 }
f2c7ba15
AJ
11400 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
11401 unlock_user(p, arg2, ret);
72eb7ea8 11402 return ret;
f2c7ba15 11403 case TARGET_NR_pwrite64:
8bf8e9df 11404 if (regpairs_aligned(cpu_env, num)) {
ae017a5b
AG
11405 arg4 = arg5;
11406 arg5 = arg6;
11407 }
2bd3f899
PM
11408 if (arg2 == 0 && arg3 == 0) {
11409 /* Special-case NULL buffer and zero length, which should succeed */
11410 p = 0;
11411 } else {
11412 p = lock_user(VERIFY_READ, arg2, arg3, 1);
11413 if (!p) {
11414 return -TARGET_EFAULT;
11415 }
11416 }
f2c7ba15
AJ
11417 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
11418 unlock_user(p, arg2, 0);
72eb7ea8 11419 return ret;
67867308 11420#endif
31e31b8a 11421 case TARGET_NR_getcwd:
579a97f7 11422 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
2852aafd 11423 return -TARGET_EFAULT;
53a5960a
PB
11424 ret = get_errno(sys_getcwd1(p, arg2));
11425 unlock_user(p, arg1, ret);
72eb7ea8 11426 return ret;
31e31b8a
FB
11427 case TARGET_NR_capget:
11428 case TARGET_NR_capset:
e0eb210e
PM
11429 {
11430 struct target_user_cap_header *target_header;
11431 struct target_user_cap_data *target_data = NULL;
11432 struct __user_cap_header_struct header;
11433 struct __user_cap_data_struct data[2];
11434 struct __user_cap_data_struct *dataptr = NULL;
11435 int i, target_datalen;
11436 int data_items = 1;
11437
11438 if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
2852aafd 11439 return -TARGET_EFAULT;
e0eb210e
PM
11440 }
11441 header.version = tswap32(target_header->version);
11442 header.pid = tswap32(target_header->pid);
11443
ec864874 11444 if (header.version != _LINUX_CAPABILITY_VERSION) {
e0eb210e
PM
11445 /* Version 2 and up takes pointer to two user_data structs */
11446 data_items = 2;
11447 }
11448
11449 target_datalen = sizeof(*target_data) * data_items;
11450
11451 if (arg2) {
11452 if (num == TARGET_NR_capget) {
11453 target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
11454 } else {
11455 target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
11456 }
11457 if (!target_data) {
11458 unlock_user_struct(target_header, arg1, 0);
2852aafd 11459 return -TARGET_EFAULT;
e0eb210e
PM
11460 }
11461
11462 if (num == TARGET_NR_capset) {
11463 for (i = 0; i < data_items; i++) {
11464 data[i].effective = tswap32(target_data[i].effective);
11465 data[i].permitted = tswap32(target_data[i].permitted);
11466 data[i].inheritable = tswap32(target_data[i].inheritable);
11467 }
11468 }
11469
11470 dataptr = data;
11471 }
11472
11473 if (num == TARGET_NR_capget) {
11474 ret = get_errno(capget(&header, dataptr));
11475 } else {
11476 ret = get_errno(capset(&header, dataptr));
11477 }
11478
11479 /* The kernel always updates version for both capget and capset */
11480 target_header->version = tswap32(header.version);
11481 unlock_user_struct(target_header, arg1, 1);
11482
11483 if (arg2) {
11484 if (num == TARGET_NR_capget) {
11485 for (i = 0; i < data_items; i++) {
11486 target_data[i].effective = tswap32(data[i].effective);
11487 target_data[i].permitted = tswap32(data[i].permitted);
11488 target_data[i].inheritable = tswap32(data[i].inheritable);
11489 }
11490 unlock_user(target_data, arg2, target_datalen);
11491 } else {
11492 unlock_user(target_data, arg2, 0);
11493 }
11494 }
72eb7ea8 11495 return ret;
e0eb210e 11496 }
31e31b8a 11497 case TARGET_NR_sigaltstack:
6b208755 11498 return do_sigaltstack(arg1, arg2, cpu_env);
a8fd1aba
PM
11499
11500#ifdef CONFIG_SENDFILE
4f7f8924 11501#ifdef TARGET_NR_sendfile
a8fd1aba
PM
11502 case TARGET_NR_sendfile:
11503 {
11504 off_t *offp = NULL;
11505 off_t off;
11506 if (arg3) {
11507 ret = get_user_sal(off, arg3);
11508 if (is_error(ret)) {
72eb7ea8 11509 return ret;
a8fd1aba
PM
11510 }
11511 offp = &off;
11512 }
11513 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
11514 if (!is_error(ret) && arg3) {
11515 abi_long ret2 = put_user_sal(off, arg3);
11516 if (is_error(ret2)) {
11517 ret = ret2;
11518 }
11519 }
72eb7ea8 11520 return ret;
a8fd1aba 11521 }
4f7f8924 11522#endif
a8fd1aba
PM
11523#ifdef TARGET_NR_sendfile64
11524 case TARGET_NR_sendfile64:
11525 {
11526 off_t *offp = NULL;
11527 off_t off;
11528 if (arg3) {
11529 ret = get_user_s64(off, arg3);
11530 if (is_error(ret)) {
72eb7ea8 11531 return ret;
a8fd1aba
PM
11532 }
11533 offp = &off;
11534 }
11535 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
11536 if (!is_error(ret) && arg3) {
11537 abi_long ret2 = put_user_s64(off, arg3);
11538 if (is_error(ret2)) {
11539 ret = ret2;
11540 }
11541 }
72eb7ea8 11542 return ret;
a8fd1aba
PM
11543 }
11544#endif
ebc05488 11545#endif
048f6b4d 11546#ifdef TARGET_NR_vfork
31e31b8a 11547 case TARGET_NR_vfork:
72eb7ea8
RH
11548 return get_errno(do_fork(cpu_env,
11549 CLONE_VFORK | CLONE_VM | TARGET_SIGCHLD,
11550 0, 0, 0, 0));
048f6b4d 11551#endif
ebc05488 11552#ifdef TARGET_NR_ugetrlimit
31e31b8a 11553 case TARGET_NR_ugetrlimit:
728584be
FB
11554 {
11555 struct rlimit rlim;
e22b7015
WT
11556 int resource = target_to_host_resource(arg1);
11557 ret = get_errno(getrlimit(resource, &rlim));
728584be 11558 if (!is_error(ret)) {
53a5960a 11559 struct target_rlimit *target_rlim;
579a97f7 11560 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
2852aafd 11561 return -TARGET_EFAULT;
81bbe906
TY
11562 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
11563 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
53a5960a 11564 unlock_user_struct(target_rlim, arg2, 1);
728584be 11565 }
72eb7ea8 11566 return ret;
728584be 11567 }
ebc05488 11568#endif
a315a145 11569#ifdef TARGET_NR_truncate64
31e31b8a 11570 case TARGET_NR_truncate64:
579a97f7 11571 if (!(p = lock_user_string(arg1)))
2852aafd 11572 return -TARGET_EFAULT;
53a5960a
PB
11573 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
11574 unlock_user(p, arg1, 0);
72eb7ea8 11575 return ret;
a315a145
FB
11576#endif
11577#ifdef TARGET_NR_ftruncate64
31e31b8a 11578 case TARGET_NR_ftruncate64:
72eb7ea8 11579 return target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
a315a145
FB
11580#endif
11581#ifdef TARGET_NR_stat64
31e31b8a 11582 case TARGET_NR_stat64:
2852aafd
RH
11583 if (!(p = lock_user_string(arg1))) {
11584 return -TARGET_EFAULT;
11585 }
53a5960a
PB
11586 ret = get_errno(stat(path(p), &st));
11587 unlock_user(p, arg1, 0);
6a24a778
AZ
11588 if (!is_error(ret))
11589 ret = host_to_target_stat64(cpu_env, arg2, &st);
72eb7ea8 11590 return ret;
a315a145
FB
11591#endif
11592#ifdef TARGET_NR_lstat64
31e31b8a 11593 case TARGET_NR_lstat64:
2852aafd
RH
11594 if (!(p = lock_user_string(arg1))) {
11595 return -TARGET_EFAULT;
11596 }
53a5960a
PB
11597 ret = get_errno(lstat(path(p), &st));
11598 unlock_user(p, arg1, 0);
6a24a778
AZ
11599 if (!is_error(ret))
11600 ret = host_to_target_stat64(cpu_env, arg2, &st);
72eb7ea8 11601 return ret;
a315a145
FB
11602#endif
11603#ifdef TARGET_NR_fstat64
31e31b8a 11604 case TARGET_NR_fstat64:
6a24a778
AZ
11605 ret = get_errno(fstat(arg1, &st));
11606 if (!is_error(ret))
11607 ret = host_to_target_stat64(cpu_env, arg2, &st);
72eb7ea8 11608 return ret;
ce4defa0 11609#endif
c0d472b1 11610#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
9d33b76b 11611#ifdef TARGET_NR_fstatat64
6a24a778 11612 case TARGET_NR_fstatat64:
9d33b76b
AJ
11613#endif
11614#ifdef TARGET_NR_newfstatat
11615 case TARGET_NR_newfstatat:
11616#endif
2852aafd
RH
11617 if (!(p = lock_user_string(arg2))) {
11618 return -TARGET_EFAULT;
11619 }
c0d472b1 11620 ret = get_errno(fstatat(arg1, path(p), &st, arg4));
2852aafd 11621 unlock_user(p, arg2, 0);
6a24a778
AZ
11622 if (!is_error(ret))
11623 ret = host_to_target_stat64(cpu_env, arg3, &st);
72eb7ea8 11624 return ret;
a315a145 11625#endif
efa92184
AR
11626#if defined(TARGET_NR_statx)
11627 case TARGET_NR_statx:
11628 {
11629 struct target_statx *target_stx;
11630 int dirfd = arg1;
11631 int flags = arg3;
11632
11633 p = lock_user_string(arg2);
11634 if (p == NULL) {
11635 return -TARGET_EFAULT;
11636 }
11637#if defined(__NR_statx)
11638 {
11639 /*
11640 * It is assumed that struct statx is architecture independent.
11641 */
11642 struct target_statx host_stx;
11643 int mask = arg4;
11644
11645 ret = get_errno(sys_statx(dirfd, p, flags, mask, &host_stx));
11646 if (!is_error(ret)) {
11647 if (host_to_target_statx(&host_stx, arg5) != 0) {
11648 unlock_user(p, arg2, 0);
11649 return -TARGET_EFAULT;
11650 }
11651 }
11652
11653 if (ret != -TARGET_ENOSYS) {
11654 unlock_user(p, arg2, 0);
11655 return ret;
11656 }
11657 }
11658#endif
11659 ret = get_errno(fstatat(dirfd, path(p), &st, flags));
11660 unlock_user(p, arg2, 0);
11661
11662 if (!is_error(ret)) {
11663 if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) {
11664 return -TARGET_EFAULT;
11665 }
11666 memset(target_stx, 0, sizeof(*target_stx));
11667 __put_user(major(st.st_dev), &target_stx->stx_dev_major);
11668 __put_user(minor(st.st_dev), &target_stx->stx_dev_minor);
11669 __put_user(st.st_ino, &target_stx->stx_ino);
11670 __put_user(st.st_mode, &target_stx->stx_mode);
11671 __put_user(st.st_uid, &target_stx->stx_uid);
11672 __put_user(st.st_gid, &target_stx->stx_gid);
11673 __put_user(st.st_nlink, &target_stx->stx_nlink);
11674 __put_user(major(st.st_rdev), &target_stx->stx_rdev_major);
11675 __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor);
11676 __put_user(st.st_size, &target_stx->stx_size);
11677 __put_user(st.st_blksize, &target_stx->stx_blksize);
11678 __put_user(st.st_blocks, &target_stx->stx_blocks);
11679 __put_user(st.st_atime, &target_stx->stx_atime.tv_sec);
11680 __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec);
11681 __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec);
11682 unlock_user_struct(target_stx, arg5, 1);
11683 }
11684 }
11685 return ret;
11686#endif
704eff6c 11687#ifdef TARGET_NR_lchown
67867308 11688 case TARGET_NR_lchown:
579a97f7 11689 if (!(p = lock_user_string(arg1)))
2852aafd 11690 return -TARGET_EFAULT;
53a5960a
PB
11691 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
11692 unlock_user(p, arg1, 0);
72eb7ea8 11693 return ret;
704eff6c 11694#endif
0c866a7e 11695#ifdef TARGET_NR_getuid
67867308 11696 case TARGET_NR_getuid:
72eb7ea8 11697 return get_errno(high2lowuid(getuid()));
0c866a7e
RV
11698#endif
11699#ifdef TARGET_NR_getgid
67867308 11700 case TARGET_NR_getgid:
72eb7ea8 11701 return get_errno(high2lowgid(getgid()));
0c866a7e
RV
11702#endif
11703#ifdef TARGET_NR_geteuid
67867308 11704 case TARGET_NR_geteuid:
72eb7ea8 11705 return get_errno(high2lowuid(geteuid()));
0c866a7e
RV
11706#endif
11707#ifdef TARGET_NR_getegid
67867308 11708 case TARGET_NR_getegid:
72eb7ea8 11709 return get_errno(high2lowgid(getegid()));
0c866a7e 11710#endif
67867308 11711 case TARGET_NR_setreuid:
72eb7ea8 11712 return get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
67867308 11713 case TARGET_NR_setregid:
72eb7ea8 11714 return get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
67867308 11715 case TARGET_NR_getgroups:
725160fe 11716 { /* the same code as for TARGET_NR_getgroups32 */
67867308 11717 int gidsetsize = arg1;
0c866a7e 11718 target_id *target_grouplist;
1e35d327 11719 g_autofree gid_t *grouplist = NULL;
67867308
FB
11720 int i;
11721
8fbf89a9 11722 if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
1e35d327
MT
11723 return -TARGET_EINVAL;
11724 }
11725 if (gidsetsize > 0) {
11726 grouplist = g_try_new(gid_t, gidsetsize);
11727 if (!grouplist) {
11728 return -TARGET_ENOMEM;
11729 }
11730 }
67867308 11731 ret = get_errno(getgroups(gidsetsize, grouplist));
1e35d327
MT
11732 if (!is_error(ret) && gidsetsize > 0) {
11733 target_grouplist = lock_user(VERIFY_WRITE, arg2,
11734 gidsetsize * sizeof(target_id), 0);
11735 if (!target_grouplist) {
2852aafd 11736 return -TARGET_EFAULT;
1e35d327
MT
11737 }
11738 for (i = 0; i < ret; i++) {
0c866a7e 11739 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
1e35d327
MT
11740 }
11741 unlock_user(target_grouplist, arg2,
11742 gidsetsize * sizeof(target_id));
67867308 11743 }
1e35d327 11744 return ret;
67867308 11745 }
67867308 11746 case TARGET_NR_setgroups:
725160fe 11747 { /* the same code as for TARGET_NR_setgroups32 */
67867308 11748 int gidsetsize = arg1;
0c866a7e 11749 target_id *target_grouplist;
1e35d327 11750 g_autofree gid_t *grouplist = NULL;
67867308 11751 int i;
1e35d327
MT
11752
11753 if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
11754 return -TARGET_EINVAL;
11755 }
11756 if (gidsetsize > 0) {
11757 grouplist = g_try_new(gid_t, gidsetsize);
11758 if (!grouplist) {
11759 return -TARGET_ENOMEM;
11760 }
11761 target_grouplist = lock_user(VERIFY_READ, arg2,
11762 gidsetsize * sizeof(target_id), 1);
f2b79ce9 11763 if (!target_grouplist) {
259841c1 11764 return -TARGET_EFAULT;
f2b79ce9
DA
11765 }
11766 for (i = 0; i < gidsetsize; i++) {
11767 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
11768 }
1e35d327
MT
11769 unlock_user(target_grouplist, arg2,
11770 gidsetsize * sizeof(target_id));
579a97f7 11771 }
72eb7ea8 11772 return get_errno(setgroups(gidsetsize, grouplist));
67867308 11773 }
67867308 11774 case TARGET_NR_fchown:
72eb7ea8 11775 return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
c0d472b1 11776#if defined(TARGET_NR_fchownat)
ccfa72b7 11777 case TARGET_NR_fchownat:
579a97f7 11778 if (!(p = lock_user_string(arg2)))
2852aafd 11779 return -TARGET_EFAULT;
c0d472b1
PM
11780 ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
11781 low2highgid(arg4), arg5));
579a97f7 11782 unlock_user(p, arg2, 0);
72eb7ea8 11783 return ret;
ccfa72b7 11784#endif
67867308
FB
11785#ifdef TARGET_NR_setresuid
11786 case TARGET_NR_setresuid:
72eb7ea8
RH
11787 return get_errno(sys_setresuid(low2highuid(arg1),
11788 low2highuid(arg2),
11789 low2highuid(arg3)));
67867308
FB
11790#endif
11791#ifdef TARGET_NR_getresuid
11792 case TARGET_NR_getresuid:
11793 {
53a5960a 11794 uid_t ruid, euid, suid;
67867308
FB
11795 ret = get_errno(getresuid(&ruid, &euid, &suid));
11796 if (!is_error(ret)) {
76ca310a
PM
11797 if (put_user_id(high2lowuid(ruid), arg1)
11798 || put_user_id(high2lowuid(euid), arg2)
11799 || put_user_id(high2lowuid(suid), arg3))
2852aafd 11800 return -TARGET_EFAULT;
67867308
FB
11801 }
11802 }
72eb7ea8 11803 return ret;
67867308
FB
11804#endif
11805#ifdef TARGET_NR_getresgid
11806 case TARGET_NR_setresgid:
72eb7ea8
RH
11807 return get_errno(sys_setresgid(low2highgid(arg1),
11808 low2highgid(arg2),
11809 low2highgid(arg3)));
67867308
FB
11810#endif
11811#ifdef TARGET_NR_getresgid
11812 case TARGET_NR_getresgid:
11813 {
53a5960a 11814 gid_t rgid, egid, sgid;
67867308
FB
11815 ret = get_errno(getresgid(&rgid, &egid, &sgid));
11816 if (!is_error(ret)) {
76ca310a
PM
11817 if (put_user_id(high2lowgid(rgid), arg1)
11818 || put_user_id(high2lowgid(egid), arg2)
11819 || put_user_id(high2lowgid(sgid), arg3))
2852aafd 11820 return -TARGET_EFAULT;
67867308
FB
11821 }
11822 }
72eb7ea8 11823 return ret;
67867308 11824#endif
704eff6c 11825#ifdef TARGET_NR_chown
67867308 11826 case TARGET_NR_chown:
579a97f7 11827 if (!(p = lock_user_string(arg1)))
2852aafd 11828 return -TARGET_EFAULT;
53a5960a
PB
11829 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
11830 unlock_user(p, arg1, 0);
72eb7ea8 11831 return ret;
704eff6c 11832#endif
67867308 11833 case TARGET_NR_setuid:
72eb7ea8 11834 return get_errno(sys_setuid(low2highuid(arg1)));
67867308 11835 case TARGET_NR_setgid:
72eb7ea8 11836 return get_errno(sys_setgid(low2highgid(arg1)));
67867308 11837 case TARGET_NR_setfsuid:
72eb7ea8 11838 return get_errno(setfsuid(arg1));
67867308 11839 case TARGET_NR_setfsgid:
72eb7ea8 11840 return get_errno(setfsgid(arg1));
67867308 11841
a315a145 11842#ifdef TARGET_NR_lchown32
31e31b8a 11843 case TARGET_NR_lchown32:
579a97f7 11844 if (!(p = lock_user_string(arg1)))
2852aafd 11845 return -TARGET_EFAULT;
53a5960a
PB
11846 ret = get_errno(lchown(p, arg2, arg3));
11847 unlock_user(p, arg1, 0);
72eb7ea8 11848 return ret;
a315a145
FB
11849#endif
11850#ifdef TARGET_NR_getuid32
31e31b8a 11851 case TARGET_NR_getuid32:
72eb7ea8 11852 return get_errno(getuid());
a315a145 11853#endif
64b4d28c
AJ
11854
11855#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
11856 /* Alpha specific */
11857 case TARGET_NR_getxuid:
ba0e276d
RH
11858 {
11859 uid_t euid;
11860 euid=geteuid();
0effdc29 11861 cpu_env->ir[IR_A4]=euid;
ba0e276d 11862 }
72eb7ea8 11863 return get_errno(getuid());
64b4d28c
AJ
11864#endif
11865#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
11866 /* Alpha specific */
11867 case TARGET_NR_getxgid:
ba0e276d
RH
11868 {
11869 uid_t egid;
11870 egid=getegid();
0effdc29 11871 cpu_env->ir[IR_A4]=egid;
ba0e276d 11872 }
72eb7ea8 11873 return get_errno(getgid());
64b4d28c 11874#endif
ba0e276d
RH
11875#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
11876 /* Alpha specific */
11877 case TARGET_NR_osf_getsysinfo:
11878 ret = -TARGET_EOPNOTSUPP;
11879 switch (arg1) {
11880 case TARGET_GSI_IEEE_FP_CONTROL:
11881 {
21ba8564 11882 uint64_t fpcr = cpu_alpha_load_fpcr(cpu_env);
0effdc29 11883 uint64_t swcr = cpu_env->swcr;
21ba8564
RH
11884
11885 swcr &= ~SWCR_STATUS_MASK;
11886 swcr |= (fpcr >> 35) & SWCR_STATUS_MASK;
ba0e276d
RH
11887
11888 if (put_user_u64 (swcr, arg2))
2852aafd 11889 return -TARGET_EFAULT;
ba0e276d
RH
11890 ret = 0;
11891 }
11892 break;
11893
11894 /* case GSI_IEEE_STATE_AT_SIGNAL:
11895 -- Not implemented in linux kernel.
11896 case GSI_UACPROC:
11897 -- Retrieves current unaligned access state; not much used.
11898 case GSI_PROC_TYPE:
11899 -- Retrieves implver information; surely not used.
11900 case GSI_GET_HWRPB:
11901 -- Grabs a copy of the HWRPB; surely not used.
11902 */
11903 }
72eb7ea8 11904 return ret;
ba0e276d
RH
11905#endif
11906#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
11907 /* Alpha specific */
11908 case TARGET_NR_osf_setsysinfo:
11909 ret = -TARGET_EOPNOTSUPP;
11910 switch (arg1) {
11911 case TARGET_SSI_IEEE_FP_CONTROL:
ba0e276d 11912 {
21ba8564 11913 uint64_t swcr, fpcr;
ba0e276d 11914
6e06d515 11915 if (get_user_u64 (swcr, arg2)) {
2852aafd 11916 return -TARGET_EFAULT;
6e06d515 11917 }
ba0e276d 11918
21ba8564
RH
11919 /*
11920 * The kernel calls swcr_update_status to update the
11921 * status bits from the fpcr at every point that it
11922 * could be queried. Therefore, we store the status
11923 * bits only in FPCR.
11924 */
0effdc29 11925 cpu_env->swcr = swcr & (SWCR_TRAP_ENABLE_MASK | SWCR_MAP_MASK);
21ba8564
RH
11926
11927 fpcr = cpu_alpha_load_fpcr(cpu_env);
11928 fpcr &= ((uint64_t)FPCR_DYN_MASK << 32);
11929 fpcr |= alpha_ieee_swcr_to_fpcr(swcr);
6e06d515 11930 cpu_alpha_store_fpcr(cpu_env, fpcr);
ba0e276d 11931 ret = 0;
6e06d515
RH
11932 }
11933 break;
11934
11935 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
11936 {
21ba8564 11937 uint64_t exc, fpcr, fex;
6e06d515
RH
11938
11939 if (get_user_u64(exc, arg2)) {
2852aafd 11940 return -TARGET_EFAULT;
6e06d515 11941 }
21ba8564
RH
11942 exc &= SWCR_STATUS_MASK;
11943 fpcr = cpu_alpha_load_fpcr(cpu_env);
ba0e276d 11944
21ba8564
RH
11945 /* Old exceptions are not signaled. */
11946 fex = alpha_ieee_fpcr_to_swcr(fpcr);
11947 fex = exc & ~fex;
11948 fex >>= SWCR_STATUS_TO_EXCSUM_SHIFT;
0effdc29 11949 fex &= (cpu_env)->swcr;
6e06d515 11950
21ba8564
RH
11951 /* Update the hardware fpcr. */
11952 fpcr |= alpha_ieee_swcr_to_fpcr(exc);
6e06d515 11953 cpu_alpha_store_fpcr(cpu_env, fpcr);
6e06d515 11954
21ba8564
RH
11955 if (fex) {
11956 int si_code = TARGET_FPE_FLTUNK;
6e06d515 11957 target_siginfo_t info;
21ba8564
RH
11958
11959 if (fex & SWCR_TRAP_ENABLE_DNO) {
11960 si_code = TARGET_FPE_FLTUND;
11961 }
11962 if (fex & SWCR_TRAP_ENABLE_INE) {
11963 si_code = TARGET_FPE_FLTRES;
11964 }
11965 if (fex & SWCR_TRAP_ENABLE_UNF) {
11966 si_code = TARGET_FPE_FLTUND;
11967 }
11968 if (fex & SWCR_TRAP_ENABLE_OVF) {
11969 si_code = TARGET_FPE_FLTOVF;
11970 }
11971 if (fex & SWCR_TRAP_ENABLE_DZE) {
11972 si_code = TARGET_FPE_FLTDIV;
11973 }
11974 if (fex & SWCR_TRAP_ENABLE_INV) {
11975 si_code = TARGET_FPE_FLTINV;
11976 }
11977
6e06d515
RH
11978 info.si_signo = SIGFPE;
11979 info.si_errno = 0;
11980 info.si_code = si_code;
0effdc29
PMD
11981 info._sifields._sigfault._addr = (cpu_env)->pc;
11982 queue_signal(cpu_env, info.si_signo,
9d2803f7 11983 QEMU_SI_FAULT, &info);
ba0e276d 11984 }
21ba8564 11985 ret = 0;
ba0e276d
RH
11986 }
11987 break;
11988
11989 /* case SSI_NVPAIRS:
11990 -- Used with SSIN_UACPROC to enable unaligned accesses.
11991 case SSI_IEEE_STATE_AT_SIGNAL:
11992 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
11993 -- Not implemented in linux kernel
11994 */
11995 }
72eb7ea8 11996 return ret;
ba0e276d
RH
11997#endif
11998#ifdef TARGET_NR_osf_sigprocmask
11999 /* Alpha specific. */
12000 case TARGET_NR_osf_sigprocmask:
12001 {
12002 abi_ulong mask;
bc088ba1 12003 int how;
ba0e276d
RH
12004 sigset_t set, oldset;
12005
12006 switch(arg1) {
12007 case TARGET_SIG_BLOCK:
12008 how = SIG_BLOCK;
12009 break;
12010 case TARGET_SIG_UNBLOCK:
12011 how = SIG_UNBLOCK;
12012 break;
12013 case TARGET_SIG_SETMASK:
12014 how = SIG_SETMASK;
12015 break;
12016 default:
259841c1 12017 return -TARGET_EINVAL;
ba0e276d
RH
12018 }
12019 mask = arg2;
12020 target_to_host_old_sigset(&set, &mask);
3d3efba0
PM
12021 ret = do_sigprocmask(how, &set, &oldset);
12022 if (!ret) {
12023 host_to_target_old_sigset(&mask, &oldset);
12024 ret = mask;
12025 }
ba0e276d 12026 }
72eb7ea8 12027 return ret;
ba0e276d 12028#endif
64b4d28c 12029
a315a145 12030#ifdef TARGET_NR_getgid32
31e31b8a 12031 case TARGET_NR_getgid32:
72eb7ea8 12032 return get_errno(getgid());
a315a145
FB
12033#endif
12034#ifdef TARGET_NR_geteuid32
31e31b8a 12035 case TARGET_NR_geteuid32:
72eb7ea8 12036 return get_errno(geteuid());
a315a145
FB
12037#endif
12038#ifdef TARGET_NR_getegid32
31e31b8a 12039 case TARGET_NR_getegid32:
72eb7ea8 12040 return get_errno(getegid());
a315a145
FB
12041#endif
12042#ifdef TARGET_NR_setreuid32
31e31b8a 12043 case TARGET_NR_setreuid32:
72eb7ea8 12044 return get_errno(setreuid(arg1, arg2));
a315a145
FB
12045#endif
12046#ifdef TARGET_NR_setregid32
31e31b8a 12047 case TARGET_NR_setregid32:
72eb7ea8 12048 return get_errno(setregid(arg1, arg2));
a315a145
FB
12049#endif
12050#ifdef TARGET_NR_getgroups32
31e31b8a 12051 case TARGET_NR_getgroups32:
725160fe 12052 { /* the same code as for TARGET_NR_getgroups */
99c475ab 12053 int gidsetsize = arg1;
53a5960a 12054 uint32_t *target_grouplist;
1e35d327 12055 g_autofree gid_t *grouplist = NULL;
99c475ab
FB
12056 int i;
12057
8fbf89a9 12058 if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
1e35d327
MT
12059 return -TARGET_EINVAL;
12060 }
12061 if (gidsetsize > 0) {
12062 grouplist = g_try_new(gid_t, gidsetsize);
12063 if (!grouplist) {
12064 return -TARGET_ENOMEM;
12065 }
12066 }
99c475ab 12067 ret = get_errno(getgroups(gidsetsize, grouplist));
1e35d327
MT
12068 if (!is_error(ret) && gidsetsize > 0) {
12069 target_grouplist = lock_user(VERIFY_WRITE, arg2,
12070 gidsetsize * 4, 0);
579a97f7 12071 if (!target_grouplist) {
259841c1 12072 return -TARGET_EFAULT;
579a97f7 12073 }
1e35d327 12074 for (i = 0; i < ret; i++) {
53a5960a 12075 target_grouplist[i] = tswap32(grouplist[i]);
1e35d327 12076 }
53a5960a 12077 unlock_user(target_grouplist, arg2, gidsetsize * 4);
99c475ab 12078 }
1e35d327 12079 return ret;
99c475ab 12080 }
a315a145
FB
12081#endif
12082#ifdef TARGET_NR_setgroups32
31e31b8a 12083 case TARGET_NR_setgroups32:
725160fe 12084 { /* the same code as for TARGET_NR_setgroups */
99c475ab 12085 int gidsetsize = arg1;
53a5960a 12086 uint32_t *target_grouplist;
1e35d327 12087 g_autofree gid_t *grouplist = NULL;
99c475ab 12088 int i;
3b46e624 12089
1e35d327
MT
12090 if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
12091 return -TARGET_EINVAL;
12092 }
12093 if (gidsetsize > 0) {
12094 grouplist = g_try_new(gid_t, gidsetsize);
12095 if (!grouplist) {
12096 return -TARGET_ENOMEM;
12097 }
12098 target_grouplist = lock_user(VERIFY_READ, arg2,
12099 gidsetsize * 4, 1);
12100 if (!target_grouplist) {
12101 return -TARGET_EFAULT;
12102 }
12103 for (i = 0; i < gidsetsize; i++) {
12104 grouplist[i] = tswap32(target_grouplist[i]);
12105 }
12106 unlock_user(target_grouplist, arg2, 0);
579a97f7 12107 }
72eb7ea8 12108 return get_errno(setgroups(gidsetsize, grouplist));
99c475ab 12109 }
a315a145
FB
12110#endif
12111#ifdef TARGET_NR_fchown32
31e31b8a 12112 case TARGET_NR_fchown32:
72eb7ea8 12113 return get_errno(fchown(arg1, arg2, arg3));
a315a145
FB
12114#endif
12115#ifdef TARGET_NR_setresuid32
31e31b8a 12116 case TARGET_NR_setresuid32:
72eb7ea8 12117 return get_errno(sys_setresuid(arg1, arg2, arg3));
a315a145
FB
12118#endif
12119#ifdef TARGET_NR_getresuid32
31e31b8a 12120 case TARGET_NR_getresuid32:
b03c60f3 12121 {
53a5960a 12122 uid_t ruid, euid, suid;
b03c60f3
FB
12123 ret = get_errno(getresuid(&ruid, &euid, &suid));
12124 if (!is_error(ret)) {
2f619698
FB
12125 if (put_user_u32(ruid, arg1)
12126 || put_user_u32(euid, arg2)
12127 || put_user_u32(suid, arg3))
2852aafd 12128 return -TARGET_EFAULT;
b03c60f3
FB
12129 }
12130 }
72eb7ea8 12131 return ret;
a315a145
FB
12132#endif
12133#ifdef TARGET_NR_setresgid32
31e31b8a 12134 case TARGET_NR_setresgid32:
72eb7ea8 12135 return get_errno(sys_setresgid(arg1, arg2, arg3));
a315a145
FB
12136#endif
12137#ifdef TARGET_NR_getresgid32
31e31b8a 12138 case TARGET_NR_getresgid32:
b03c60f3 12139 {
53a5960a 12140 gid_t rgid, egid, sgid;
b03c60f3
FB
12141 ret = get_errno(getresgid(&rgid, &egid, &sgid));
12142 if (!is_error(ret)) {
2f619698
FB
12143 if (put_user_u32(rgid, arg1)
12144 || put_user_u32(egid, arg2)
12145 || put_user_u32(sgid, arg3))
2852aafd 12146 return -TARGET_EFAULT;
b03c60f3
FB
12147 }
12148 }
72eb7ea8 12149 return ret;
a315a145
FB
12150#endif
12151#ifdef TARGET_NR_chown32
31e31b8a 12152 case TARGET_NR_chown32:
579a97f7 12153 if (!(p = lock_user_string(arg1)))
2852aafd 12154 return -TARGET_EFAULT;
53a5960a
PB
12155 ret = get_errno(chown(p, arg2, arg3));
12156 unlock_user(p, arg1, 0);
72eb7ea8 12157 return ret;
a315a145
FB
12158#endif
12159#ifdef TARGET_NR_setuid32
31e31b8a 12160 case TARGET_NR_setuid32:
72eb7ea8 12161 return get_errno(sys_setuid(arg1));
a315a145
FB
12162#endif
12163#ifdef TARGET_NR_setgid32
31e31b8a 12164 case TARGET_NR_setgid32:
72eb7ea8 12165 return get_errno(sys_setgid(arg1));
a315a145
FB
12166#endif
12167#ifdef TARGET_NR_setfsuid32
31e31b8a 12168 case TARGET_NR_setfsuid32:
72eb7ea8 12169 return get_errno(setfsuid(arg1));
a315a145
FB
12170#endif
12171#ifdef TARGET_NR_setfsgid32
31e31b8a 12172 case TARGET_NR_setfsgid32:
72eb7ea8 12173 return get_errno(setfsgid(arg1));
a315a145 12174#endif
ffa65c3b 12175#ifdef TARGET_NR_mincore
31e31b8a 12176 case TARGET_NR_mincore:
04bb9ace 12177 {
f443a26c 12178 void *a = lock_user(VERIFY_NONE, arg1, arg2, 0);
98a3331a 12179 if (!a) {
259841c1 12180 return -TARGET_ENOMEM;
98a3331a 12181 }
98a3331a
FSM
12182 p = lock_user_string(arg3);
12183 if (!p) {
259841c1
RH
12184 ret = -TARGET_EFAULT;
12185 } else {
12186 ret = get_errno(mincore(a, arg2, p));
12187 unlock_user(p, arg3, ret);
98a3331a 12188 }
04bb9ace
AJ
12189 unlock_user(a, arg1, 0);
12190 }
72eb7ea8 12191 return ret;
ffa65c3b 12192#endif
408321b6
AJ
12193#ifdef TARGET_NR_arm_fadvise64_64
12194 case TARGET_NR_arm_fadvise64_64:
e0156a9d
PM
12195 /* arm_fadvise64_64 looks like fadvise64_64 but
12196 * with different argument order: fd, advice, offset, len
12197 * rather than the usual fd, offset, len, advice.
12198 * Note that offset and len are both 64-bit so appear as
12199 * pairs of 32-bit registers.
12200 */
12201 ret = posix_fadvise(arg1, target_offset64(arg3, arg4),
12202 target_offset64(arg5, arg6), arg2);
72eb7ea8 12203 return -host_to_target_errno(ret);
408321b6 12204#endif
badd3cd8 12205
eeed2291 12206#if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
badd3cd8
PM
12207
12208#ifdef TARGET_NR_fadvise64_64
12209 case TARGET_NR_fadvise64_64:
64a563dd 12210#if defined(TARGET_PPC) || defined(TARGET_XTENSA)
43046b5a
LV
12211 /* 6 args: fd, advice, offset (high, low), len (high, low) */
12212 ret = arg2;
12213 arg2 = arg3;
12214 arg3 = arg4;
12215 arg4 = arg5;
12216 arg5 = arg6;
12217 arg6 = ret;
12218#else
badd3cd8 12219 /* 6 args: fd, offset (high, low), len (high, low), advice */
8bf8e9df 12220 if (regpairs_aligned(cpu_env, num)) {
badd3cd8
PM
12221 /* offset is in (3,4), len in (5,6) and advice in 7 */
12222 arg2 = arg3;
12223 arg3 = arg4;
12224 arg4 = arg5;
12225 arg5 = arg6;
12226 arg6 = arg7;
12227 }
43046b5a 12228#endif
72eb7ea8
RH
12229 ret = posix_fadvise(arg1, target_offset64(arg2, arg3),
12230 target_offset64(arg4, arg5), arg6);
12231 return -host_to_target_errno(ret);
badd3cd8
PM
12232#endif
12233
12234#ifdef TARGET_NR_fadvise64
12235 case TARGET_NR_fadvise64:
12236 /* 5 args: fd, offset (high, low), len, advice */
8bf8e9df 12237 if (regpairs_aligned(cpu_env, num)) {
badd3cd8
PM
12238 /* offset is in (3,4), len in 5 and advice in 6 */
12239 arg2 = arg3;
12240 arg3 = arg4;
12241 arg4 = arg5;
12242 arg5 = arg6;
12243 }
72eb7ea8
RH
12244 ret = posix_fadvise(arg1, target_offset64(arg2, arg3), arg4, arg5);
12245 return -host_to_target_errno(ret);
408321b6 12246#endif
badd3cd8
PM
12247
12248#else /* not a 32-bit ABI */
e0156a9d 12249#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
408321b6
AJ
12250#ifdef TARGET_NR_fadvise64_64
12251 case TARGET_NR_fadvise64_64:
12252#endif
e72d2cc7
UH
12253#ifdef TARGET_NR_fadvise64
12254 case TARGET_NR_fadvise64:
12255#endif
12256#ifdef TARGET_S390X
12257 switch (arg4) {
12258 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
12259 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
12260 case 6: arg4 = POSIX_FADV_DONTNEED; break;
12261 case 7: arg4 = POSIX_FADV_NOREUSE; break;
12262 default: break;
12263 }
12264#endif
72eb7ea8 12265 return -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4));
408321b6 12266#endif
badd3cd8
PM
12267#endif /* end of 64-bit ABI fadvise handling */
12268
ffa65c3b 12269#ifdef TARGET_NR_madvise
31e31b8a 12270 case TARGET_NR_madvise:
892a4f6a 12271 return target_madvise(arg1, arg2, arg3);
ffa65c3b 12272#endif
bbf5f2a1 12273#ifdef TARGET_NR_fcntl64
31e31b8a 12274 case TARGET_NR_fcntl64:
77e4672d 12275 {
bbf5f2a1
AB
12276 int cmd;
12277 struct flock64 fl;
213d3e9e
PM
12278 from_flock64_fn *copyfrom = copy_from_user_flock64;
12279 to_flock64_fn *copyto = copy_to_user_flock64;
12280
ce4defa0 12281#ifdef TARGET_ARM
0effdc29 12282 if (!cpu_env->eabi) {
7f254c5c
LV
12283 copyfrom = copy_from_user_oabi_flock64;
12284 copyto = copy_to_user_oabi_flock64;
213d3e9e 12285 }
ce4defa0 12286#endif
77e4672d 12287
bbf5f2a1 12288 cmd = target_to_host_fcntl_cmd(arg2);
31b63193 12289 if (cmd == -TARGET_EINVAL) {
72eb7ea8 12290 return cmd;
31b63193 12291 }
b1e341eb 12292
60cd49d5 12293 switch(arg2) {
b1e341eb 12294 case TARGET_F_GETLK64:
213d3e9e
PM
12295 ret = copyfrom(&fl, arg3);
12296 if (ret) {
12297 break;
5813427b 12298 }
af8ab2bf 12299 ret = get_errno(safe_fcntl(arg1, cmd, &fl));
213d3e9e
PM
12300 if (ret == 0) {
12301 ret = copyto(arg3, &fl);
12302 }
77e4672d
FB
12303 break;
12304
b1e341eb
TS
12305 case TARGET_F_SETLK64:
12306 case TARGET_F_SETLKW64:
213d3e9e
PM
12307 ret = copyfrom(&fl, arg3);
12308 if (ret) {
12309 break;
ce4defa0 12310 }
435da5e7 12311 ret = get_errno(safe_fcntl(arg1, cmd, &fl));
77e4672d 12312 break;
60cd49d5 12313 default:
5f106811 12314 ret = do_fcntl(arg1, arg2, arg3);
60cd49d5
FB
12315 break;
12316 }
72eb7ea8 12317 return ret;
77e4672d 12318 }
60cd49d5 12319#endif
7d600c80
TS
12320#ifdef TARGET_NR_cacheflush
12321 case TARGET_NR_cacheflush:
12322 /* self-modifying code is handled automatically, so nothing needed */
72eb7ea8 12323 return 0;
7d600c80 12324#endif
c573ff67
FB
12325#ifdef TARGET_NR_getpagesize
12326 case TARGET_NR_getpagesize:
72eb7ea8 12327 return TARGET_PAGE_SIZE;
ebc05488 12328#endif
31e31b8a 12329 case TARGET_NR_gettid:
71ba74f6 12330 return get_errno(sys_gettid());
e5febef5 12331#ifdef TARGET_NR_readahead
31e31b8a 12332 case TARGET_NR_readahead:
eeed2291 12333#if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
8bf8e9df 12334 if (regpairs_aligned(cpu_env, num)) {
2054ac9b
AJ
12335 arg2 = arg3;
12336 arg3 = arg4;
12337 arg4 = arg5;
12338 }
77c6850f 12339 ret = get_errno(readahead(arg1, target_offset64(arg2, arg3) , arg4));
2054ac9b
AJ
12340#else
12341 ret = get_errno(readahead(arg1, arg2, arg3));
12342#endif
72eb7ea8 12343 return ret;
e5febef5 12344#endif
a790ae38 12345#ifdef CONFIG_ATTR
ebc05488 12346#ifdef TARGET_NR_setxattr
31e31b8a
FB
12347 case TARGET_NR_listxattr:
12348 case TARGET_NR_llistxattr:
fb5590f7 12349 {
77c9f177 12350 void *b = 0;
fb5590f7
PM
12351 if (arg2) {
12352 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
12353 if (!b) {
72eb7ea8 12354 return -TARGET_EFAULT;
fb5590f7
PM
12355 }
12356 }
12357 p = lock_user_string(arg1);
12358 if (p) {
12359 if (num == TARGET_NR_listxattr) {
12360 ret = get_errno(listxattr(p, b, arg3));
12361 } else {
12362 ret = get_errno(llistxattr(p, b, arg3));
12363 }
12364 } else {
12365 ret = -TARGET_EFAULT;
12366 }
12367 unlock_user(p, arg1, 0);
12368 unlock_user(b, arg2, arg3);
72eb7ea8 12369 return ret;
fb5590f7 12370 }
31e31b8a 12371 case TARGET_NR_flistxattr:
fb5590f7
PM
12372 {
12373 void *b = 0;
12374 if (arg2) {
12375 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
12376 if (!b) {
72eb7ea8 12377 return -TARGET_EFAULT;
fb5590f7
PM
12378 }
12379 }
12380 ret = get_errno(flistxattr(arg1, b, arg3));
12381 unlock_user(b, arg2, arg3);
72eb7ea8 12382 return ret;
fb5590f7 12383 }
a790ae38 12384 case TARGET_NR_setxattr:
30297b55 12385 case TARGET_NR_lsetxattr:
a790ae38 12386 {
77c9f177 12387 void *n, *v = 0;
e3c33ec6
PM
12388 if (arg3) {
12389 v = lock_user(VERIFY_READ, arg3, arg4, 1);
12390 if (!v) {
72eb7ea8 12391 return -TARGET_EFAULT;
e3c33ec6
PM
12392 }
12393 }
a790ae38
ACH
12394 p = lock_user_string(arg1);
12395 n = lock_user_string(arg2);
e3c33ec6 12396 if (p && n) {
30297b55
PM
12397 if (num == TARGET_NR_setxattr) {
12398 ret = get_errno(setxattr(p, n, v, arg4, arg5));
12399 } else {
12400 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
12401 }
a790ae38
ACH
12402 } else {
12403 ret = -TARGET_EFAULT;
12404 }
12405 unlock_user(p, arg1, 0);
12406 unlock_user(n, arg2, 0);
12407 unlock_user(v, arg3, 0);
12408 }
72eb7ea8 12409 return ret;
30297b55
PM
12410 case TARGET_NR_fsetxattr:
12411 {
12412 void *n, *v = 0;
12413 if (arg3) {
12414 v = lock_user(VERIFY_READ, arg3, arg4, 1);
12415 if (!v) {
72eb7ea8 12416 return -TARGET_EFAULT;
30297b55
PM
12417 }
12418 }
12419 n = lock_user_string(arg2);
12420 if (n) {
12421 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
12422 } else {
12423 ret = -TARGET_EFAULT;
12424 }
12425 unlock_user(n, arg2, 0);
12426 unlock_user(v, arg3, 0);
12427 }
72eb7ea8 12428 return ret;
a790ae38 12429 case TARGET_NR_getxattr:
30297b55 12430 case TARGET_NR_lgetxattr:
a790ae38 12431 {
77c9f177 12432 void *n, *v = 0;
e3c33ec6
PM
12433 if (arg3) {
12434 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
12435 if (!v) {
72eb7ea8 12436 return -TARGET_EFAULT;
e3c33ec6
PM
12437 }
12438 }
a790ae38
ACH
12439 p = lock_user_string(arg1);
12440 n = lock_user_string(arg2);
e3c33ec6 12441 if (p && n) {
30297b55
PM
12442 if (num == TARGET_NR_getxattr) {
12443 ret = get_errno(getxattr(p, n, v, arg4));
12444 } else {
12445 ret = get_errno(lgetxattr(p, n, v, arg4));
12446 }
a790ae38
ACH
12447 } else {
12448 ret = -TARGET_EFAULT;
12449 }
12450 unlock_user(p, arg1, 0);
12451 unlock_user(n, arg2, 0);
12452 unlock_user(v, arg3, arg4);
12453 }
72eb7ea8 12454 return ret;
30297b55
PM
12455 case TARGET_NR_fgetxattr:
12456 {
12457 void *n, *v = 0;
12458 if (arg3) {
12459 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
12460 if (!v) {
72eb7ea8 12461 return -TARGET_EFAULT;
30297b55
PM
12462 }
12463 }
12464 n = lock_user_string(arg2);
12465 if (n) {
12466 ret = get_errno(fgetxattr(arg1, n, v, arg4));
12467 } else {
12468 ret = -TARGET_EFAULT;
12469 }
12470 unlock_user(n, arg2, 0);
12471 unlock_user(v, arg3, arg4);
12472 }
72eb7ea8 12473 return ret;
a790ae38 12474 case TARGET_NR_removexattr:
30297b55 12475 case TARGET_NR_lremovexattr:
a790ae38 12476 {
77c9f177 12477 void *n;
a790ae38
ACH
12478 p = lock_user_string(arg1);
12479 n = lock_user_string(arg2);
12480 if (p && n) {
30297b55
PM
12481 if (num == TARGET_NR_removexattr) {
12482 ret = get_errno(removexattr(p, n));
12483 } else {
12484 ret = get_errno(lremovexattr(p, n));
12485 }
a790ae38
ACH
12486 } else {
12487 ret = -TARGET_EFAULT;
12488 }
12489 unlock_user(p, arg1, 0);
12490 unlock_user(n, arg2, 0);
12491 }
72eb7ea8 12492 return ret;
30297b55
PM
12493 case TARGET_NR_fremovexattr:
12494 {
12495 void *n;
12496 n = lock_user_string(arg2);
12497 if (n) {
12498 ret = get_errno(fremovexattr(arg1, n));
12499 } else {
12500 ret = -TARGET_EFAULT;
12501 }
12502 unlock_user(n, arg2, 0);
12503 }
72eb7ea8 12504 return ret;
ebc05488 12505#endif
a790ae38 12506#endif /* CONFIG_ATTR */
ebc05488 12507#ifdef TARGET_NR_set_thread_area
5cd4393b 12508 case TARGET_NR_set_thread_area:
8d18e893 12509#if defined(TARGET_MIPS)
0effdc29 12510 cpu_env->active_tc.CP0_UserLocal = arg1;
72eb7ea8 12511 return 0;
ef96779b
EI
12512#elif defined(TARGET_CRIS)
12513 if (arg1 & 0xff)
12514 ret = -TARGET_EINVAL;
12515 else {
0effdc29 12516 cpu_env->pregs[PR_PID] = arg1;
ef96779b
EI
12517 ret = 0;
12518 }
72eb7ea8 12519 return ret;
8d18e893 12520#elif defined(TARGET_I386) && defined(TARGET_ABI32)
72eb7ea8 12521 return do_set_thread_area(cpu_env, arg1);
1ccd9374
PM
12522#elif defined(TARGET_M68K)
12523 {
0429a971 12524 TaskState *ts = cpu->opaque;
1ccd9374 12525 ts->tp_value = arg1;
72eb7ea8 12526 return 0;
1ccd9374 12527 }
6f5b89a0 12528#else
10f45d98 12529 return -TARGET_ENOSYS;
6f5b89a0
TS
12530#endif
12531#endif
12532#ifdef TARGET_NR_get_thread_area
5cd4393b 12533 case TARGET_NR_get_thread_area:
8d18e893 12534#if defined(TARGET_I386) && defined(TARGET_ABI32)
72eb7ea8 12535 return do_get_thread_area(cpu_env, arg1);
1ccd9374
PM
12536#elif defined(TARGET_M68K)
12537 {
0429a971 12538 TaskState *ts = cpu->opaque;
72eb7ea8 12539 return ts->tp_value;
1ccd9374 12540 }
8d18e893 12541#else
10f45d98 12542 return -TARGET_ENOSYS;
48dc41eb 12543#endif
8d18e893 12544#endif
48dc41eb
FB
12545#ifdef TARGET_NR_getdomainname
12546 case TARGET_NR_getdomainname:
10f45d98 12547 return -TARGET_ENOSYS;
ebc05488 12548#endif
6f5b89a0 12549
12e3340c
MF
12550#ifdef TARGET_NR_clock_settime
12551 case TARGET_NR_clock_settime:
12552 {
12553 struct timespec ts;
12554
12555 ret = target_to_host_timespec(&ts, arg2);
12556 if (!is_error(ret)) {
12557 ret = get_errno(clock_settime(arg1, &ts));
12558 }
72eb7ea8 12559 return ret;
12e3340c
MF
12560 }
12561#endif
c6c8d102
AF
12562#ifdef TARGET_NR_clock_settime64
12563 case TARGET_NR_clock_settime64:
12564 {
12565 struct timespec ts;
12566
12567 ret = target_to_host_timespec64(&ts, arg2);
12568 if (!is_error(ret)) {
12569 ret = get_errno(clock_settime(arg1, &ts));
12570 }
12571 return ret;
12572 }
12573#endif
b5906f95
TS
12574#ifdef TARGET_NR_clock_gettime
12575 case TARGET_NR_clock_gettime:
12576 {
12577 struct timespec ts;
12578 ret = get_errno(clock_gettime(arg1, &ts));
12579 if (!is_error(ret)) {
b9f9908e 12580 ret = host_to_target_timespec(arg2, &ts);
b5906f95 12581 }
72eb7ea8 12582 return ret;
b5906f95
TS
12583 }
12584#endif
c6c8d102
AF
12585#ifdef TARGET_NR_clock_gettime64
12586 case TARGET_NR_clock_gettime64:
12587 {
12588 struct timespec ts;
12589 ret = get_errno(clock_gettime(arg1, &ts));
12590 if (!is_error(ret)) {
12591 ret = host_to_target_timespec64(arg2, &ts);
12592 }
12593 return ret;
12594 }
12595#endif
b5906f95
TS
12596#ifdef TARGET_NR_clock_getres
12597 case TARGET_NR_clock_getres:
12598 {
12599 struct timespec ts;
12600 ret = get_errno(clock_getres(arg1, &ts));
12601 if (!is_error(ret)) {
12602 host_to_target_timespec(arg2, &ts);
12603 }
72eb7ea8 12604 return ret;
b5906f95
TS
12605 }
12606#endif
828cb3a1
FB
12607#ifdef TARGET_NR_clock_getres_time64
12608 case TARGET_NR_clock_getres_time64:
12609 {
12610 struct timespec ts;
12611 ret = get_errno(clock_getres(arg1, &ts));
12612 if (!is_error(ret)) {
12613 host_to_target_timespec64(arg2, &ts);
12614 }
12615 return ret;
12616 }
12617#endif
63d7651b
PB
12618#ifdef TARGET_NR_clock_nanosleep
12619 case TARGET_NR_clock_nanosleep:
12620 {
12621 struct timespec ts;
b09d6406
FB
12622 if (target_to_host_timespec(&ts, arg3)) {
12623 return -TARGET_EFAULT;
12624 }
9e518226
PM
12625 ret = get_errno(safe_clock_nanosleep(arg1, arg2,
12626 &ts, arg4 ? &ts : NULL));
8ec68a0a
LV
12627 /*
12628 * if the call is interrupted by a signal handler, it fails
12629 * with error -TARGET_EINTR and if arg4 is not NULL and arg2 is not
12630 * TIMER_ABSTIME, it returns the remaining unslept time in arg4.
12631 */
b09d6406
FB
12632 if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
12633 host_to_target_timespec(arg4, &ts)) {
12634 return -TARGET_EFAULT;
8ec68a0a 12635 }
8fbe8fdf 12636
72eb7ea8 12637 return ret;
63d7651b
PB
12638 }
12639#endif
6ac03b2c
FB
12640#ifdef TARGET_NR_clock_nanosleep_time64
12641 case TARGET_NR_clock_nanosleep_time64:
12642 {
12643 struct timespec ts;
12644
12645 if (target_to_host_timespec64(&ts, arg3)) {
12646 return -TARGET_EFAULT;
12647 }
12648
12649 ret = get_errno(safe_clock_nanosleep(arg1, arg2,
12650 &ts, arg4 ? &ts : NULL));
12651
12652 if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
12653 host_to_target_timespec64(arg4, &ts)) {
12654 return -TARGET_EFAULT;
12655 }
12656 return ret;
12657 }
12658#endif
b5906f95 12659
9a7f682c 12660#if defined(TARGET_NR_set_tid_address)
6f5b89a0 12661 case TARGET_NR_set_tid_address:
9a7f682c
HD
12662 {
12663 TaskState *ts = cpu->opaque;
12664 ts->child_tidptr = arg1;
12665 /* do not call host set_tid_address() syscall, instead return tid() */
12666 return get_errno(sys_gettid());
12667 }
6f5b89a0
TS
12668#endif
12669
4cae1d16 12670 case TARGET_NR_tkill:
72eb7ea8 12671 return get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2)));
4cae1d16 12672
71455574 12673 case TARGET_NR_tgkill:
72eb7ea8
RH
12674 return get_errno(safe_tgkill((int)arg1, (int)arg2,
12675 target_to_host_signal(arg3)));
71455574 12676
4f2b1fe8
TS
12677#ifdef TARGET_NR_set_robust_list
12678 case TARGET_NR_set_robust_list:
e9a970a8
PM
12679 case TARGET_NR_get_robust_list:
12680 /* The ABI for supporting robust futexes has userspace pass
12681 * the kernel a pointer to a linked list which is updated by
12682 * userspace after the syscall; the list is walked by the kernel
12683 * when the thread exits. Since the linked list in QEMU guest
12684 * memory isn't a valid linked list for the host and we have
12685 * no way to reliably intercept the thread-death event, we can't
12686 * support these. Silently return ENOSYS so that guest userspace
12687 * falls back to a non-robust futex implementation (which should
12688 * be OK except in the corner case of the guest crashing while
12689 * holding a mutex that is shared with another process via
12690 * shared memory).
12691 */
10f45d98 12692 return -TARGET_ENOSYS;
4f2b1fe8
TS
12693#endif
12694
1acae9f2 12695#if defined(TARGET_NR_utimensat)
9007f0ef
TS
12696 case TARGET_NR_utimensat:
12697 {
ebc996f3
RV
12698 struct timespec *tsp, ts[2];
12699 if (!arg3) {
12700 tsp = NULL;
12701 } else {
b3a3af70
FB
12702 if (target_to_host_timespec(ts, arg3)) {
12703 return -TARGET_EFAULT;
12704 }
12705 if (target_to_host_timespec(ts + 1, arg3 +
12706 sizeof(struct target_timespec))) {
12707 return -TARGET_EFAULT;
12708 }
ebc996f3
RV
12709 tsp = ts;
12710 }
9007f0ef 12711 if (!arg2)
ebc996f3 12712 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
9007f0ef 12713 else {
579a97f7 12714 if (!(p = lock_user_string(arg2))) {
259841c1 12715 return -TARGET_EFAULT;
579a97f7 12716 }
ebc996f3 12717 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
579a97f7 12718 unlock_user(p, arg2, 0);
cac46eb0
FB
12719 }
12720 }
12721 return ret;
12722#endif
12723#ifdef TARGET_NR_utimensat_time64
12724 case TARGET_NR_utimensat_time64:
12725 {
12726 struct timespec *tsp, ts[2];
12727 if (!arg3) {
12728 tsp = NULL;
12729 } else {
12730 if (target_to_host_timespec64(ts, arg3)) {
12731 return -TARGET_EFAULT;
12732 }
12733 if (target_to_host_timespec64(ts + 1, arg3 +
12734 sizeof(struct target__kernel_timespec))) {
12735 return -TARGET_EFAULT;
12736 }
12737 tsp = ts;
12738 }
12739 if (!arg2)
12740 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
12741 else {
12742 p = lock_user_string(arg2);
12743 if (!p) {
12744 return -TARGET_EFAULT;
12745 }
12746 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
12747 unlock_user(p, arg2, 0);
9007f0ef
TS
12748 }
12749 }
72eb7ea8 12750 return ret;
9007f0ef 12751#endif
859e8a89 12752#ifdef TARGET_NR_futex
bd0c5661 12753 case TARGET_NR_futex:
0fbc0f8d 12754 return do_futex(cpu, false, arg1, arg2, arg3, arg4, arg5, arg6);
859e8a89 12755#endif
14690296
AF
12756#ifdef TARGET_NR_futex_time64
12757 case TARGET_NR_futex_time64:
0fbc0f8d 12758 return do_futex(cpu, true, arg1, arg2, arg3, arg4, arg5, arg6);
14690296 12759#endif
33f53ac5
PB
12760#ifdef CONFIG_INOTIFY
12761#if defined(TARGET_NR_inotify_init)
39b59763 12762 case TARGET_NR_inotify_init:
33f53ac5 12763 ret = get_errno(inotify_init());
b929f7e5
PMD
12764 if (ret >= 0) {
12765 fd_trans_register(ret, &target_inotify_trans);
12766 }
72eb7ea8 12767 return ret;
39b59763 12768#endif
33f53ac5 12769#if defined(TARGET_NR_inotify_init1) && defined(CONFIG_INOTIFY1)
c05c7a73 12770 case TARGET_NR_inotify_init1:
33f53ac5 12771 ret = get_errno(inotify_init1(target_to_host_bitmask(arg1,
fea243e9 12772 fcntl_flags_tbl)));
b929f7e5
PMD
12773 if (ret >= 0) {
12774 fd_trans_register(ret, &target_inotify_trans);
12775 }
72eb7ea8 12776 return ret;
c05c7a73 12777#endif
33f53ac5 12778#if defined(TARGET_NR_inotify_add_watch)
39b59763
AJ
12779 case TARGET_NR_inotify_add_watch:
12780 p = lock_user_string(arg2);
33f53ac5 12781 ret = get_errno(inotify_add_watch(arg1, path(p), arg3));
39b59763 12782 unlock_user(p, arg2, 0);
72eb7ea8 12783 return ret;
39b59763 12784#endif
33f53ac5 12785#if defined(TARGET_NR_inotify_rm_watch)
39b59763 12786 case TARGET_NR_inotify_rm_watch:
33f53ac5
PB
12787 return get_errno(inotify_rm_watch(arg1, arg2));
12788#endif
39b59763 12789#endif
9007f0ef 12790
8ec9cf89 12791#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
24e1003a
AJ
12792 case TARGET_NR_mq_open:
12793 {
c7536ab6 12794 struct mq_attr posix_mq_attr;
26400775 12795 struct mq_attr *pposix_mq_attr;
c7536ab6 12796 int host_flags;
24e1003a 12797
c7536ab6 12798 host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl);
26400775
LD
12799 pposix_mq_attr = NULL;
12800 if (arg4) {
12801 if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) {
2852aafd 12802 return -TARGET_EFAULT;
26400775
LD
12803 }
12804 pposix_mq_attr = &posix_mq_attr;
c7536ab6 12805 }
24e1003a 12806 p = lock_user_string(arg1 - 1);
c7536ab6 12807 if (!p) {
2852aafd 12808 return -TARGET_EFAULT;
b6ce1f6b 12809 }
26400775 12810 ret = get_errno(mq_open(p, host_flags, arg3, pposix_mq_attr));
24e1003a
AJ
12811 unlock_user (p, arg1, 0);
12812 }
72eb7ea8 12813 return ret;
24e1003a
AJ
12814
12815 case TARGET_NR_mq_unlink:
12816 p = lock_user_string(arg1 - 1);
3211215e 12817 if (!p) {
72eb7ea8 12818 return -TARGET_EFAULT;
3211215e 12819 }
24e1003a
AJ
12820 ret = get_errno(mq_unlink(p));
12821 unlock_user (p, arg1, 0);
72eb7ea8 12822 return ret;
24e1003a 12823
859e8a89 12824#ifdef TARGET_NR_mq_timedsend
24e1003a
AJ
12825 case TARGET_NR_mq_timedsend:
12826 {
12827 struct timespec ts;
12828
12829 p = lock_user (VERIFY_READ, arg2, arg3, 1);
12830 if (arg5 != 0) {
dcbcf5cf
FB
12831 if (target_to_host_timespec(&ts, arg5)) {
12832 return -TARGET_EFAULT;
12833 }
d40ecd66 12834 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
dcbcf5cf
FB
12835 if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
12836 return -TARGET_EFAULT;
12837 }
d40ecd66
PM
12838 } else {
12839 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
24e1003a 12840 }
24e1003a
AJ
12841 unlock_user (p, arg2, arg3);
12842 }
72eb7ea8 12843 return ret;
859e8a89 12844#endif
d107e375
FB
12845#ifdef TARGET_NR_mq_timedsend_time64
12846 case TARGET_NR_mq_timedsend_time64:
12847 {
12848 struct timespec ts;
12849
12850 p = lock_user(VERIFY_READ, arg2, arg3, 1);
12851 if (arg5 != 0) {
12852 if (target_to_host_timespec64(&ts, arg5)) {
12853 return -TARGET_EFAULT;
12854 }
12855 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
12856 if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
12857 return -TARGET_EFAULT;
12858 }
12859 } else {
12860 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
12861 }
12862 unlock_user(p, arg2, arg3);
12863 }
12864 return ret;
12865#endif
24e1003a 12866
859e8a89 12867#ifdef TARGET_NR_mq_timedreceive
24e1003a
AJ
12868 case TARGET_NR_mq_timedreceive:
12869 {
12870 struct timespec ts;
12871 unsigned int prio;
12872
12873 p = lock_user (VERIFY_READ, arg2, arg3, 1);
12874 if (arg5 != 0) {
dcbcf5cf
FB
12875 if (target_to_host_timespec(&ts, arg5)) {
12876 return -TARGET_EFAULT;
12877 }
d40ecd66
PM
12878 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
12879 &prio, &ts));
dcbcf5cf
FB
12880 if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
12881 return -TARGET_EFAULT;
12882 }
d40ecd66
PM
12883 } else {
12884 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
12885 &prio, NULL));
24e1003a 12886 }
24e1003a
AJ
12887 unlock_user (p, arg2, arg3);
12888 if (arg4 != 0)
12889 put_user_u32(prio, arg4);
12890 }
72eb7ea8 12891 return ret;
859e8a89 12892#endif
d107e375
FB
12893#ifdef TARGET_NR_mq_timedreceive_time64
12894 case TARGET_NR_mq_timedreceive_time64:
12895 {
12896 struct timespec ts;
12897 unsigned int prio;
12898
12899 p = lock_user(VERIFY_READ, arg2, arg3, 1);
12900 if (arg5 != 0) {
12901 if (target_to_host_timespec64(&ts, arg5)) {
12902 return -TARGET_EFAULT;
12903 }
12904 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
12905 &prio, &ts));
12906 if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
12907 return -TARGET_EFAULT;
12908 }
12909 } else {
12910 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
12911 &prio, NULL));
12912 }
12913 unlock_user(p, arg2, arg3);
12914 if (arg4 != 0) {
12915 put_user_u32(prio, arg4);
12916 }
12917 }
12918 return ret;
12919#endif
24e1003a
AJ
12920
12921 /* Not implemented for now... */
12922/* case TARGET_NR_mq_notify: */
12923/* break; */
12924
12925 case TARGET_NR_mq_getsetattr:
12926 {
12927 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
12928 ret = 0;
24e1003a
AJ
12929 if (arg2 != 0) {
12930 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
a23ea409
MF
12931 ret = get_errno(mq_setattr(arg1, &posix_mq_attr_in,
12932 &posix_mq_attr_out));
12933 } else if (arg3 != 0) {
12934 ret = get_errno(mq_getattr(arg1, &posix_mq_attr_out));
12935 }
12936 if (ret == 0 && arg3 != 0) {
12937 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
24e1003a 12938 }
24e1003a 12939 }
72eb7ea8 12940 return ret;
24e1003a
AJ
12941#endif
12942
3ce34dfb
VS
12943#ifdef CONFIG_SPLICE
12944#ifdef TARGET_NR_tee
12945 case TARGET_NR_tee:
12946 {
12947 ret = get_errno(tee(arg1,arg2,arg3,arg4));
12948 }
72eb7ea8 12949 return ret;
3ce34dfb
VS
12950#endif
12951#ifdef TARGET_NR_splice
12952 case TARGET_NR_splice:
12953 {
12954 loff_t loff_in, loff_out;
12955 loff_t *ploff_in = NULL, *ploff_out = NULL;
17644b36
AS
12956 if (arg2) {
12957 if (get_user_u64(loff_in, arg2)) {
2852aafd 12958 return -TARGET_EFAULT;
17644b36 12959 }
3ce34dfb
VS
12960 ploff_in = &loff_in;
12961 }
17644b36
AS
12962 if (arg4) {
12963 if (get_user_u64(loff_out, arg4)) {
2852aafd 12964 return -TARGET_EFAULT;
17644b36 12965 }
3ce34dfb
VS
12966 ploff_out = &loff_out;
12967 }
12968 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
17644b36
AS
12969 if (arg2) {
12970 if (put_user_u64(loff_in, arg2)) {
2852aafd 12971 return -TARGET_EFAULT;
17644b36
AS
12972 }
12973 }
12974 if (arg4) {
12975 if (put_user_u64(loff_out, arg4)) {
2852aafd 12976 return -TARGET_EFAULT;
17644b36
AS
12977 }
12978 }
3ce34dfb 12979 }
72eb7ea8 12980 return ret;
3ce34dfb
VS
12981#endif
12982#ifdef TARGET_NR_vmsplice
12983 case TARGET_NR_vmsplice:
12984 {
f287b2c2
RH
12985 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
12986 if (vec != NULL) {
12987 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
12988 unlock_iovec(vec, arg2, arg3, 0);
12989 } else {
12990 ret = -host_to_target_errno(errno);
12991 }
3ce34dfb 12992 }
72eb7ea8 12993 return ret;
3ce34dfb
VS
12994#endif
12995#endif /* CONFIG_SPLICE */
c2882b96
RV
12996#ifdef CONFIG_EVENTFD
12997#if defined(TARGET_NR_eventfd)
12998 case TARGET_NR_eventfd:
12999 ret = get_errno(eventfd(arg1, 0));
b929f7e5
PMD
13000 if (ret >= 0) {
13001 fd_trans_register(ret, &target_eventfd_trans);
13002 }
72eb7ea8 13003 return ret;
c2882b96
RV
13004#endif
13005#if defined(TARGET_NR_eventfd2)
13006 case TARGET_NR_eventfd2:
5947c697 13007 {
78721301 13008 int host_flags = arg2 & (~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC));
5947c697
PJ
13009 if (arg2 & TARGET_O_NONBLOCK) {
13010 host_flags |= O_NONBLOCK;
13011 }
13012 if (arg2 & TARGET_O_CLOEXEC) {
13013 host_flags |= O_CLOEXEC;
13014 }
13015 ret = get_errno(eventfd(arg1, host_flags));
b929f7e5
PMD
13016 if (ret >= 0) {
13017 fd_trans_register(ret, &target_eventfd_trans);
13018 }
72eb7ea8 13019 return ret;
5947c697 13020 }
c2882b96
RV
13021#endif
13022#endif /* CONFIG_EVENTFD */
d0927938
UH
13023#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
13024 case TARGET_NR_fallocate:
eeed2291 13025#if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
20249ae1
AG
13026 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
13027 target_offset64(arg5, arg6)));
13028#else
d0927938 13029 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
20249ae1 13030#endif
72eb7ea8 13031 return ret;
c727f47d
PM
13032#endif
13033#if defined(CONFIG_SYNC_FILE_RANGE)
13034#if defined(TARGET_NR_sync_file_range)
13035 case TARGET_NR_sync_file_range:
eeed2291 13036#if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
bfcedc57
RV
13037#if defined(TARGET_MIPS)
13038 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
13039 target_offset64(arg5, arg6), arg7));
13040#else
c727f47d
PM
13041 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
13042 target_offset64(arg4, arg5), arg6));
bfcedc57 13043#endif /* !TARGET_MIPS */
c727f47d
PM
13044#else
13045 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
13046#endif
72eb7ea8 13047 return ret;
c727f47d 13048#endif
5bcb4986
LV
13049#if defined(TARGET_NR_sync_file_range2) || \
13050 defined(TARGET_NR_arm_sync_file_range)
c727f47d
PM
13051#if defined(TARGET_NR_sync_file_range2)
13052 case TARGET_NR_sync_file_range2:
5bcb4986
LV
13053#endif
13054#if defined(TARGET_NR_arm_sync_file_range)
13055 case TARGET_NR_arm_sync_file_range:
13056#endif
c727f47d 13057 /* This is like sync_file_range but the arguments are reordered */
eeed2291 13058#if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
c727f47d
PM
13059 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
13060 target_offset64(arg5, arg6), arg2));
13061#else
13062 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
13063#endif
72eb7ea8 13064 return ret;
c727f47d 13065#endif
3b6edd16 13066#endif
e36800c9
LV
13067#if defined(TARGET_NR_signalfd4)
13068 case TARGET_NR_signalfd4:
72eb7ea8 13069 return do_signalfd4(arg1, arg2, arg4);
e36800c9
LV
13070#endif
13071#if defined(TARGET_NR_signalfd)
13072 case TARGET_NR_signalfd:
72eb7ea8 13073 return do_signalfd4(arg1, arg2, 0);
e36800c9 13074#endif
3b6edd16
PM
13075#if defined(CONFIG_EPOLL)
13076#if defined(TARGET_NR_epoll_create)
13077 case TARGET_NR_epoll_create:
72eb7ea8 13078 return get_errno(epoll_create(arg1));
3b6edd16
PM
13079#endif
13080#if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
13081 case TARGET_NR_epoll_create1:
386d3865 13082 return get_errno(epoll_create1(target_to_host_bitmask(arg1, fcntl_flags_tbl)));
3b6edd16
PM
13083#endif
13084#if defined(TARGET_NR_epoll_ctl)
13085 case TARGET_NR_epoll_ctl:
13086 {
13087 struct epoll_event ep;
13088 struct epoll_event *epp = 0;
13089 if (arg4) {
c7811022
L
13090 if (arg2 != EPOLL_CTL_DEL) {
13091 struct target_epoll_event *target_ep;
13092 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
13093 return -TARGET_EFAULT;
13094 }
13095 ep.events = tswap32(target_ep->events);
13096 /*
13097 * The epoll_data_t union is just opaque data to the kernel,
13098 * so we transfer all 64 bits across and need not worry what
13099 * actual data type it is.
13100 */
13101 ep.data.u64 = tswap64(target_ep->data.u64);
13102 unlock_user_struct(target_ep, arg4, 0);
3b6edd16 13103 }
c7811022
L
13104 /*
13105 * before kernel 2.6.9, EPOLL_CTL_DEL operation required a
13106 * non-null pointer, even though this argument is ignored.
13107 *
3b6edd16 13108 */
3b6edd16
PM
13109 epp = &ep;
13110 }
72eb7ea8 13111 return get_errno(epoll_ctl(arg1, arg2, arg3, epp));
3b6edd16
PM
13112 }
13113#endif
13114
227f0214 13115#if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
3b6edd16
PM
13116#if defined(TARGET_NR_epoll_wait)
13117 case TARGET_NR_epoll_wait:
13118#endif
227f0214 13119#if defined(TARGET_NR_epoll_pwait)
3b6edd16
PM
13120 case TARGET_NR_epoll_pwait:
13121#endif
13122 {
13123 struct target_epoll_event *target_ep;
13124 struct epoll_event *ep;
13125 int epfd = arg1;
13126 int maxevents = arg3;
13127 int timeout = arg4;
13128
2ba7fae3 13129 if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) {
72eb7ea8 13130 return -TARGET_EINVAL;
2ba7fae3
PM
13131 }
13132
3b6edd16
PM
13133 target_ep = lock_user(VERIFY_WRITE, arg2,
13134 maxevents * sizeof(struct target_epoll_event), 1);
13135 if (!target_ep) {
2852aafd 13136 return -TARGET_EFAULT;
3b6edd16
PM
13137 }
13138
04c95f4d
PM
13139 ep = g_try_new(struct epoll_event, maxevents);
13140 if (!ep) {
13141 unlock_user(target_ep, arg2, 0);
72eb7ea8 13142 return -TARGET_ENOMEM;
04c95f4d 13143 }
3b6edd16
PM
13144
13145 switch (num) {
227f0214 13146#if defined(TARGET_NR_epoll_pwait)
3b6edd16
PM
13147 case TARGET_NR_epoll_pwait:
13148 {
cd0e31a4 13149 sigset_t *set = NULL;
3b6edd16
PM
13150
13151 if (arg5) {
cd0e31a4
RH
13152 ret = process_sigsuspend_mask(&set, arg5, arg6);
13153 if (ret != 0) {
c815701e
PM
13154 break;
13155 }
3b6edd16
PM
13156 }
13157
227f0214
PM
13158 ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
13159 set, SIGSET_T_SIZE));
cd0e31a4
RH
13160
13161 if (set) {
13162 finish_sigsuspend_mask(ret);
13163 }
3b6edd16
PM
13164 break;
13165 }
13166#endif
13167#if defined(TARGET_NR_epoll_wait)
13168 case TARGET_NR_epoll_wait:
227f0214
PM
13169 ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
13170 NULL, 0));
3b6edd16
PM
13171 break;
13172#endif
13173 default:
13174 ret = -TARGET_ENOSYS;
13175 }
13176 if (!is_error(ret)) {
13177 int i;
13178 for (i = 0; i < ret; i++) {
13179 target_ep[i].events = tswap32(ep[i].events);
13180 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
13181 }
04c95f4d
PM
13182 unlock_user(target_ep, arg2,
13183 ret * sizeof(struct target_epoll_event));
13184 } else {
13185 unlock_user(target_ep, arg2, 0);
3b6edd16 13186 }
04c95f4d 13187 g_free(ep);
72eb7ea8 13188 return ret;
3b6edd16
PM
13189 }
13190#endif
163a05a8
PM
13191#endif
13192#ifdef TARGET_NR_prlimit64
13193 case TARGET_NR_prlimit64:
13194 {
13195 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
13196 struct target_rlimit64 *target_rnew, *target_rold;
13197 struct host_rlimit64 rnew, rold, *rnewp = 0;
95018018 13198 int resource = target_to_host_resource(arg2);
055d92f8
TK
13199
13200 if (arg3 && (resource != RLIMIT_AS &&
13201 resource != RLIMIT_DATA &&
13202 resource != RLIMIT_STACK)) {
163a05a8 13203 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
2852aafd 13204 return -TARGET_EFAULT;
163a05a8 13205 }
9c1da8b5
IL
13206 __get_user(rnew.rlim_cur, &target_rnew->rlim_cur);
13207 __get_user(rnew.rlim_max, &target_rnew->rlim_max);
163a05a8
PM
13208 unlock_user_struct(target_rnew, arg3, 0);
13209 rnewp = &rnew;
13210 }
13211
95018018 13212 ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
163a05a8
PM
13213 if (!is_error(ret) && arg4) {
13214 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
2852aafd 13215 return -TARGET_EFAULT;
163a05a8 13216 }
9c1da8b5
IL
13217 __put_user(rold.rlim_cur, &target_rold->rlim_cur);
13218 __put_user(rold.rlim_max, &target_rold->rlim_max);
163a05a8
PM
13219 unlock_user_struct(target_rold, arg4, 1);
13220 }
72eb7ea8 13221 return ret;
163a05a8 13222 }
3d21d29c
RH
13223#endif
13224#ifdef TARGET_NR_gethostname
13225 case TARGET_NR_gethostname:
13226 {
13227 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
13228 if (name) {
13229 ret = get_errno(gethostname(name, arg2));
13230 unlock_user(name, arg1, arg2);
13231 } else {
13232 ret = -TARGET_EFAULT;
13233 }
72eb7ea8 13234 return ret;
3d21d29c 13235 }
89aaf1a6
RV
13236#endif
13237#ifdef TARGET_NR_atomic_cmpxchg_32
13238 case TARGET_NR_atomic_cmpxchg_32:
13239 {
13240 /* should use start_exclusive from main.c */
13241 abi_ulong mem_value;
13242 if (get_user_u32(mem_value, arg6)) {
13243 target_siginfo_t info;
13244 info.si_signo = SIGSEGV;
13245 info.si_errno = 0;
13246 info.si_code = TARGET_SEGV_MAPERR;
13247 info._sifields._sigfault._addr = arg6;
0effdc29 13248 queue_signal(cpu_env, info.si_signo, QEMU_SI_FAULT, &info);
89aaf1a6
RV
13249 ret = 0xdeadbeef;
13250
13251 }
13252 if (mem_value == arg2)
13253 put_user_u32(arg1, arg6);
72eb7ea8 13254 return mem_value;
89aaf1a6
RV
13255 }
13256#endif
13257#ifdef TARGET_NR_atomic_barrier
13258 case TARGET_NR_atomic_barrier:
72eb7ea8
RH
13259 /* Like the kernel implementation and the
13260 qemu arm barrier, no-op this? */
13261 return 0;
d0927938 13262#endif
f4f1e10a
ECL
13263
13264#ifdef TARGET_NR_timer_create
13265 case TARGET_NR_timer_create:
13266 {
13267 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
13268
13269 struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
f4f1e10a
ECL
13270
13271 int clkid = arg1;
13272 int timer_index = next_free_host_timer();
13273
13274 if (timer_index < 0) {
13275 ret = -TARGET_EAGAIN;
13276 } else {
13277 timer_t *phtimer = g_posix_timers + timer_index;
13278
13279 if (arg2) {
f4f1e10a 13280 phost_sevp = &host_sevp;
c065976f
PM
13281 ret = target_to_host_sigevent(phost_sevp, arg2);
13282 if (ret != 0) {
9e59899f 13283 free_host_timer_slot(timer_index);
72eb7ea8 13284 return ret;
c065976f 13285 }
f4f1e10a
ECL
13286 }
13287
13288 ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
13289 if (ret) {
9e59899f 13290 free_host_timer_slot(timer_index);
f4f1e10a 13291 } else {
aecc8861 13292 if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
9e59899f
PM
13293 timer_delete(*phtimer);
13294 free_host_timer_slot(timer_index);
2852aafd 13295 return -TARGET_EFAULT;
f4f1e10a 13296 }
f4f1e10a
ECL
13297 }
13298 }
72eb7ea8 13299 return ret;
f4f1e10a
ECL
13300 }
13301#endif
13302
13303#ifdef TARGET_NR_timer_settime
13304 case TARGET_NR_timer_settime:
13305 {
13306 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
13307 * struct itimerspec * old_value */
aecc8861 13308 target_timer_t timerid = get_timer_id(arg1);
e52a99f7 13309
aecc8861
AG
13310 if (timerid < 0) {
13311 ret = timerid;
13312 } else if (arg3 == 0) {
f4f1e10a
ECL
13313 ret = -TARGET_EINVAL;
13314 } else {
e52a99f7 13315 timer_t htimer = g_posix_timers[timerid];
f4f1e10a
ECL
13316 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
13317
40c80b5e 13318 if (target_to_host_itimerspec(&hspec_new, arg3)) {
2852aafd 13319 return -TARGET_EFAULT;
40c80b5e 13320 }
f4f1e10a
ECL
13321 ret = get_errno(
13322 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
40c80b5e 13323 if (arg4 && host_to_target_itimerspec(arg4, &hspec_old)) {
2852aafd 13324 return -TARGET_EFAULT;
40c80b5e 13325 }
f4f1e10a 13326 }
72eb7ea8 13327 return ret;
f4f1e10a
ECL
13328 }
13329#endif
13330
828cb3a1
FB
13331#ifdef TARGET_NR_timer_settime64
13332 case TARGET_NR_timer_settime64:
13333 {
13334 target_timer_t timerid = get_timer_id(arg1);
13335
13336 if (timerid < 0) {
13337 ret = timerid;
13338 } else if (arg3 == 0) {
13339 ret = -TARGET_EINVAL;
13340 } else {
13341 timer_t htimer = g_posix_timers[timerid];
13342 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
13343
13344 if (target_to_host_itimerspec64(&hspec_new, arg3)) {
13345 return -TARGET_EFAULT;
13346 }
13347 ret = get_errno(
13348 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
13349 if (arg4 && host_to_target_itimerspec64(arg4, &hspec_old)) {
13350 return -TARGET_EFAULT;
13351 }
13352 }
13353 return ret;
13354 }
13355#endif
13356
f4f1e10a
ECL
13357#ifdef TARGET_NR_timer_gettime
13358 case TARGET_NR_timer_gettime:
13359 {
13360 /* args: timer_t timerid, struct itimerspec *curr_value */
aecc8861 13361 target_timer_t timerid = get_timer_id(arg1);
e52a99f7 13362
aecc8861
AG
13363 if (timerid < 0) {
13364 ret = timerid;
13365 } else if (!arg2) {
13366 ret = -TARGET_EFAULT;
f4f1e10a 13367 } else {
e52a99f7 13368 timer_t htimer = g_posix_timers[timerid];
f4f1e10a
ECL
13369 struct itimerspec hspec;
13370 ret = get_errno(timer_gettime(htimer, &hspec));
13371
13372 if (host_to_target_itimerspec(arg2, &hspec)) {
13373 ret = -TARGET_EFAULT;
13374 }
13375 }
72eb7ea8 13376 return ret;
f4f1e10a
ECL
13377 }
13378#endif
13379
828cb3a1
FB
13380#ifdef TARGET_NR_timer_gettime64
13381 case TARGET_NR_timer_gettime64:
13382 {
13383 /* args: timer_t timerid, struct itimerspec64 *curr_value */
13384 target_timer_t timerid = get_timer_id(arg1);
13385
13386 if (timerid < 0) {
13387 ret = timerid;
13388 } else if (!arg2) {
13389 ret = -TARGET_EFAULT;
13390 } else {
13391 timer_t htimer = g_posix_timers[timerid];
13392 struct itimerspec hspec;
13393 ret = get_errno(timer_gettime(htimer, &hspec));
13394
13395 if (host_to_target_itimerspec64(arg2, &hspec)) {
13396 ret = -TARGET_EFAULT;
13397 }
13398 }
13399 return ret;
13400 }
13401#endif
13402
f4f1e10a
ECL
13403#ifdef TARGET_NR_timer_getoverrun
13404 case TARGET_NR_timer_getoverrun:
13405 {
13406 /* args: timer_t timerid */
aecc8861 13407 target_timer_t timerid = get_timer_id(arg1);
e52a99f7 13408
aecc8861
AG
13409 if (timerid < 0) {
13410 ret = timerid;
f4f1e10a 13411 } else {
e52a99f7 13412 timer_t htimer = g_posix_timers[timerid];
f4f1e10a
ECL
13413 ret = get_errno(timer_getoverrun(htimer));
13414 }
72eb7ea8 13415 return ret;
f4f1e10a
ECL
13416 }
13417#endif
13418
13419#ifdef TARGET_NR_timer_delete
13420 case TARGET_NR_timer_delete:
13421 {
13422 /* args: timer_t timerid */
aecc8861 13423 target_timer_t timerid = get_timer_id(arg1);
e52a99f7 13424
aecc8861
AG
13425 if (timerid < 0) {
13426 ret = timerid;
f4f1e10a 13427 } else {
e52a99f7 13428 timer_t htimer = g_posix_timers[timerid];
f4f1e10a 13429 ret = get_errno(timer_delete(htimer));
9e59899f 13430 free_host_timer_slot(timerid);
f4f1e10a 13431 }
72eb7ea8 13432 return ret;
f4f1e10a
ECL
13433 }
13434#endif
13435
51834341
RV
13436#if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
13437 case TARGET_NR_timerfd_create:
d759a62b
MM
13438 ret = get_errno(timerfd_create(arg1,
13439 target_to_host_bitmask(arg2, fcntl_flags_tbl)));
13440 if (ret >= 0) {
13441 fd_trans_register(ret, &target_timerfd_trans);
13442 }
13443 return ret;
51834341
RV
13444#endif
13445
13446#if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
13447 case TARGET_NR_timerfd_gettime:
13448 {
13449 struct itimerspec its_curr;
13450
13451 ret = get_errno(timerfd_gettime(arg1, &its_curr));
13452
13453 if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
2852aafd 13454 return -TARGET_EFAULT;
51834341
RV
13455 }
13456 }
72eb7ea8 13457 return ret;
51834341
RV
13458#endif
13459
828cb3a1
FB
13460#if defined(TARGET_NR_timerfd_gettime64) && defined(CONFIG_TIMERFD)
13461 case TARGET_NR_timerfd_gettime64:
13462 {
13463 struct itimerspec its_curr;
13464
13465 ret = get_errno(timerfd_gettime(arg1, &its_curr));
13466
13467 if (arg2 && host_to_target_itimerspec64(arg2, &its_curr)) {
13468 return -TARGET_EFAULT;
13469 }
13470 }
13471 return ret;
13472#endif
13473
51834341
RV
13474#if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
13475 case TARGET_NR_timerfd_settime:
13476 {
13477 struct itimerspec its_new, its_old, *p_new;
13478
13479 if (arg3) {
13480 if (target_to_host_itimerspec(&its_new, arg3)) {
2852aafd 13481 return -TARGET_EFAULT;
51834341
RV
13482 }
13483 p_new = &its_new;
13484 } else {
13485 p_new = NULL;
13486 }
13487
13488 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
13489
13490 if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
2852aafd 13491 return -TARGET_EFAULT;
51834341
RV
13492 }
13493 }
72eb7ea8 13494 return ret;
51834341
RV
13495#endif
13496
828cb3a1
FB
13497#if defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)
13498 case TARGET_NR_timerfd_settime64:
13499 {
13500 struct itimerspec its_new, its_old, *p_new;
13501
13502 if (arg3) {
13503 if (target_to_host_itimerspec64(&its_new, arg3)) {
13504 return -TARGET_EFAULT;
13505 }
13506 p_new = &its_new;
13507 } else {
13508 p_new = NULL;
13509 }
13510
13511 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
13512
13513 if (arg4 && host_to_target_itimerspec64(arg4, &its_old)) {
13514 return -TARGET_EFAULT;
13515 }
13516 }
13517 return ret;
13518#endif
13519
ab31cda3
PB
13520#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
13521 case TARGET_NR_ioprio_get:
72eb7ea8 13522 return get_errno(ioprio_get(arg1, arg2));
ab31cda3
PB
13523#endif
13524
13525#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
13526 case TARGET_NR_ioprio_set:
72eb7ea8 13527 return get_errno(ioprio_set(arg1, arg2, arg3));
ab31cda3
PB
13528#endif
13529
9af5c906
RV
13530#if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
13531 case TARGET_NR_setns:
72eb7ea8 13532 return get_errno(setns(arg1, arg2));
9af5c906
RV
13533#endif
13534#if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
13535 case TARGET_NR_unshare:
72eb7ea8 13536 return get_errno(unshare(arg1));
9af5c906 13537#endif
2f14788c
LV
13538#if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
13539 case TARGET_NR_kcmp:
72eb7ea8 13540 return get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
2f14788c 13541#endif
fa97e38e
RH
13542#ifdef TARGET_NR_swapcontext
13543 case TARGET_NR_swapcontext:
13544 /* PowerPC specific. */
72eb7ea8 13545 return do_swapcontext(cpu_env, arg1, arg2, arg3);
fa97e38e 13546#endif
9bdfa4d2
SCW
13547#ifdef TARGET_NR_memfd_create
13548 case TARGET_NR_memfd_create:
13549 p = lock_user_string(arg1);
13550 if (!p) {
13551 return -TARGET_EFAULT;
13552 }
13553 ret = get_errno(memfd_create(p, arg2));
13554 fd_trans_unregister(ret);
13555 unlock_user(p, arg1, 0);
13556 return ret;
13557#endif
8500476f
AS
13558#if defined TARGET_NR_membarrier && defined __NR_membarrier
13559 case TARGET_NR_membarrier:
13560 return get_errno(membarrier(arg1, arg2));
13561#endif
9af5c906 13562
84946457
AS
13563#if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
13564 case TARGET_NR_copy_file_range:
13565 {
13566 loff_t inoff, outoff;
13567 loff_t *pinoff = NULL, *poutoff = NULL;
13568
13569 if (arg2) {
13570 if (get_user_u64(inoff, arg2)) {
13571 return -TARGET_EFAULT;
13572 }
13573 pinoff = &inoff;
13574 }
13575 if (arg4) {
13576 if (get_user_u64(outoff, arg4)) {
13577 return -TARGET_EFAULT;
13578 }
13579 poutoff = &outoff;
13580 }
0fa259dd 13581 /* Do not sign-extend the count parameter. */
84946457 13582 ret = get_errno(safe_copy_file_range(arg1, pinoff, arg3, poutoff,
0fa259dd 13583 (abi_ulong)arg5, arg6));
84946457
AS
13584 if (!is_error(ret) && ret > 0) {
13585 if (arg2) {
13586 if (put_user_u64(inoff, arg2)) {
13587 return -TARGET_EFAULT;
13588 }
13589 }
13590 if (arg4) {
13591 if (put_user_u64(outoff, arg4)) {
13592 return -TARGET_EFAULT;
13593 }
13594 }
13595 }
13596 }
13597 return ret;
13598#endif
13599
e10fbe8f
YT
13600#if defined(TARGET_NR_pivot_root)
13601 case TARGET_NR_pivot_root:
13602 {
13603 void *p2;
13604 p = lock_user_string(arg1); /* new_root */
13605 p2 = lock_user_string(arg2); /* put_old */
13606 if (!p || !p2) {
13607 ret = -TARGET_EFAULT;
13608 } else {
13609 ret = get_errno(pivot_root(p, p2));
13610 }
13611 unlock_user(p2, arg2, 0);
13612 unlock_user(p, arg1, 0);
13613 }
13614 return ret;
13615#endif
13616
9e1c7d98
RE
13617#if defined(TARGET_NR_riscv_hwprobe)
13618 case TARGET_NR_riscv_hwprobe:
13619 return do_riscv_hwprobe(cpu_env, arg1, arg2, arg3, arg4, arg5);
13620#endif
13621
31e31b8a 13622 default:
122f9c83 13623 qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
72eb7ea8 13624 return -TARGET_ENOSYS;
31e31b8a 13625 }
31e31b8a
FB
13626 return ret;
13627}
dc1ce18b 13628
a0939b89 13629abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1,
dc1ce18b
RH
13630 abi_long arg2, abi_long arg3, abi_long arg4,
13631 abi_long arg5, abi_long arg6, abi_long arg7,
13632 abi_long arg8)
13633{
29a0af61 13634 CPUState *cpu = env_cpu(cpu_env);
dc1ce18b
RH
13635 abi_long ret;
13636
13637#ifdef DEBUG_ERESTARTSYS
13638 /* Debug-only code for exercising the syscall-restart code paths
13639 * in the per-architecture cpu main loops: restart every syscall
13640 * the guest makes once before letting it through.
13641 */
13642 {
13643 static bool flag;
13644 flag = !flag;
13645 if (flag) {
af254a27 13646 return -QEMU_ERESTARTSYS;
dc1ce18b
RH
13647 }
13648 }
13649#endif
13650
c36f7a64
EC
13651 record_syscall_start(cpu, num, arg1,
13652 arg2, arg3, arg4, arg5, arg6, arg7, arg8);
dc1ce18b 13653
4b25a506 13654 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
e400e119 13655 print_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6);
4b25a506
JK
13656 }
13657
13658 ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
13659 arg5, arg6, arg7, arg8);
13660
13661 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
e400e119
FB
13662 print_syscall_ret(cpu_env, num, ret, arg1, arg2,
13663 arg3, arg4, arg5, arg6);
dc1ce18b
RH
13664 }
13665
c36f7a64 13666 record_syscall_return(cpu, num, ret);
dc1ce18b
RH
13667 return ret;
13668}