]> git.ipfire.org Git - thirdparty/qemu.git/blame - linux-user/syscall.c
user: Use get_task_state() helper
[thirdparty/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"
74781c08 28#include "exec/page-protection.h"
31e31b8a
FB
29#include <elf.h>
30#include <endian.h>
c56dc774 31#include <grp.h>
d08d3bb8
TS
32#include <sys/ipc.h>
33#include <sys/msg.h>
31e31b8a 34#include <sys/wait.h>
31e31b8a 35#include <sys/mount.h>
586b0bef
JS
36#include <sys/file.h>
37#include <sys/fsuid.h>
38#include <sys/personality.h>
39b9aae1 39#include <sys/prctl.h>
31e31b8a 40#include <sys/resource.h>
31e31b8a 41#include <sys/swap.h>
e0eb210e 42#include <linux/capability.h>
31e31b8a 43#include <sched.h>
19f59bce 44#include <sys/timex.h>
31e31b8a 45#include <sys/socket.h>
6d5d5dde 46#include <linux/sockios.h>
607175e0 47#include <sys/un.h>
31e31b8a 48#include <sys/uio.h>
0839f11c 49#include <poll.h>
32f36bce 50#include <sys/times.h>
8853f86e 51#include <sys/shm.h>
fa294816 52#include <sys/sem.h>
56c8f68f 53#include <sys/statfs.h>
ebc05488 54#include <utime.h>
a5448a7d 55#include <sys/sysinfo.h>
e36800c9 56#include <sys/signalfd.h>
72f03900 57//#include <sys/user.h>
22db1213 58#include <netinet/in.h>
8853f86e 59#include <netinet/ip.h>
7854b056 60#include <netinet/tcp.h>
fe51b0a5 61#include <netinet/udp.h>
86fcd946 62#include <linux/wireless.h>
920394db 63#include <linux/icmp.h>
ee1ac3a1 64#include <linux/icmpv6.h>
6addf06a 65#include <linux/if_tun.h>
22db1213 66#include <linux/in6.h>
ee1ac3a1 67#include <linux/errqueue.h>
d6d6d6fe 68#include <linux/random.h>
d80a1905
RV
69#ifdef CONFIG_TIMERFD
70#include <sys/timerfd.h>
71#endif
c2882b96
RV
72#ifdef CONFIG_EVENTFD
73#include <sys/eventfd.h>
74#endif
3b6edd16
PM
75#ifdef CONFIG_EPOLL
76#include <sys/epoll.h>
77#endif
a790ae38 78#ifdef CONFIG_ATTR
1de7afc9 79#include "qemu/xattr.h"
a790ae38 80#endif
a8fd1aba
PM
81#ifdef CONFIG_SENDFILE
82#include <sys/sendfile.h>
83#endif
4a9d5f89 84#ifdef HAVE_SYS_KCOV_H
bd27e675
AM
85#include <sys/kcov.h>
86#endif
31e31b8a
FB
87
88#define termios host_termios
89#define winsize host_winsize
90#define termio host_termio
04369ff2
FB
91#define sgttyb host_sgttyb /* same as target */
92#define tchars host_tchars /* same as target */
93#define ltchars host_ltchars /* same as target */
31e31b8a
FB
94
95#include <linux/termios.h>
96#include <linux/unistd.h>
31e31b8a
FB
97#include <linux/cdrom.h>
98#include <linux/hdreg.h>
99#include <linux/soundcard.h>
19b84f3c 100#include <linux/kd.h>
8fbd6b52 101#include <linux/mtio.h>
350d1779 102#include <linux/fs.h>
ab22b4dd 103#include <linux/fd.h>
dace20dc 104#if defined(CONFIG_FIEMAP)
285da2b9 105#include <linux/fiemap.h>
dace20dc 106#endif
f7680a55 107#include <linux/fb.h>
6c753a63
CT
108#if defined(CONFIG_USBFS)
109#include <linux/usbdevice_fs.h>
a133367e 110#include <linux/usb/ch9.h>
6c753a63 111#endif
f7680a55 112#include <linux/vt.h>
56e904ec 113#include <linux/dm-ioctl.h>
c07ecc68 114#include <linux/reboot.h>
7ff7b666 115#include <linux/route.h>
f57d4192 116#include <linux/filter.h>
fff8c539 117#include <linux/blkpg.h>
a82ea939 118#include <netpacket/packet.h>
6c5b5645 119#include <linux/netlink.h>
f31dddd2 120#include <linux/if_alg.h>
68365f96 121#include <linux/rtc.h>
1c4c6fcd 122#include <sound/asound.h>
48f670ec 123#ifdef HAVE_BTRFS_H
d6092e08
FB
124#include <linux/btrfs.h>
125#endif
e865b97f
CG
126#ifdef HAVE_DRM_H
127#include <libdrm/drm.h>
913b03c2 128#include <libdrm/i915_drm.h>
e865b97f 129#endif
d7e4036e 130#include "linux_loop.h"
18cb0088 131#include "uname.h"
31e31b8a 132
3ef693a0 133#include "qemu.h"
3b249d26 134#include "user-internals.h"
a44d57a3 135#include "strace.h"
2113aed6 136#include "signal-common.h"
3ad0a769 137#include "loader.h"
5423e6d3 138#include "user-mmap.h"
bbf15aaf 139#include "user/safe-syscall.h"
5ebdd774 140#include "qemu/guest-random.h"
01ef6b9e 141#include "qemu/selfmap.h"
c36f7a64 142#include "user/syscall-trace.h"
5da4063f 143#include "special-errno.h"
51977e25 144#include "qapi/error.h"
f7e6a401 145#include "fd-trans.h"
bd5ccd61 146#include "cpu_loop-common.h"
31e31b8a 147
5ea2fc84
PM
148#ifndef CLONE_IO
149#define CLONE_IO 0x80000000 /* Clone io context */
150#endif
151
152/* We can't directly call the host clone syscall, because this will
153 * badly confuse libc (breaking mutexes, for example). So we must
154 * divide clone flags into:
155 * * flag combinations that look like pthread_create()
156 * * flag combinations that look like fork()
157 * * flags we can implement within QEMU itself
158 * * flags we can't support and will return an error for
159 */
160/* For thread creation, all these flags must be present; for
161 * fork, none must be present.
162 */
163#define CLONE_THREAD_FLAGS \
164 (CLONE_VM | CLONE_FS | CLONE_FILES | \
165 CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
166
167/* These flags are ignored:
168 * CLONE_DETACHED is now ignored by the kernel;
169 * CLONE_IO is just an optimisation hint to the I/O scheduler
170 */
171#define CLONE_IGNORED_FLAGS \
172 (CLONE_DETACHED | CLONE_IO)
173
895ce8bb
HD
174#ifndef CLONE_PIDFD
175# define CLONE_PIDFD 0x00001000
176#endif
177
5ea2fc84
PM
178/* Flags for fork which we can implement within QEMU itself */
179#define CLONE_OPTIONAL_FORK_FLAGS \
895ce8bb 180 (CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_PIDFD | \
5ea2fc84
PM
181 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
182
183/* Flags for thread creation which we can implement within QEMU itself */
184#define CLONE_OPTIONAL_THREAD_FLAGS \
185 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
186 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
187
188#define CLONE_INVALID_FORK_FLAGS \
189 (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
190
191#define CLONE_INVALID_THREAD_FLAGS \
192 (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
193 CLONE_IGNORED_FLAGS))
194
195/* CLONE_VFORK is special cased early in do_fork(). The other flag bits
196 * have almost all been allocated. We cannot support any of
197 * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
198 * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
199 * The checks against the invalid thread masks above will catch these.
200 * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
201 */
30813cea 202
71a8f7fe
TB
203/* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
204 * once. This exercises the codepaths for restart.
205 */
206//#define DEBUG_ERESTARTSYS
31e31b8a 207
1a9353d2 208//#include <linux/msdos_fs.h>
540a736f
RH
209#define VFAT_IOCTL_READDIR_BOTH \
210 _IOC(_IOC_READ, 'r', 1, (sizeof(struct linux_dirent) + 256) * 2)
211#define VFAT_IOCTL_READDIR_SHORT \
212 _IOC(_IOC_READ, 'r', 2, (sizeof(struct linux_dirent) + 256) * 2)
1a9353d2 213
70a194b9
FB
214#undef _syscall0
215#undef _syscall1
216#undef _syscall2
217#undef _syscall3
218#undef _syscall4
219#undef _syscall5
83fcb515 220#undef _syscall6
70a194b9 221
83fcb515 222#define _syscall0(type,name) \
8fcd3692 223static type name (void) \
83fcb515
FB
224{ \
225 return syscall(__NR_##name); \
226}
70a194b9 227
83fcb515 228#define _syscall1(type,name,type1,arg1) \
8fcd3692 229static type name (type1 arg1) \
83fcb515
FB
230{ \
231 return syscall(__NR_##name, arg1); \
70a194b9
FB
232}
233
83fcb515 234#define _syscall2(type,name,type1,arg1,type2,arg2) \
8fcd3692 235static type name (type1 arg1,type2 arg2) \
83fcb515
FB
236{ \
237 return syscall(__NR_##name, arg1, arg2); \
70a194b9
FB
238}
239
83fcb515 240#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
8fcd3692 241static type name (type1 arg1,type2 arg2,type3 arg3) \
83fcb515
FB
242{ \
243 return syscall(__NR_##name, arg1, arg2, arg3); \
70a194b9
FB
244}
245
83fcb515 246#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
8fcd3692 247static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
83fcb515
FB
248{ \
249 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
70a194b9
FB
250}
251
83fcb515
FB
252#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
253 type5,arg5) \
8fcd3692 254static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
83fcb515
FB
255{ \
256 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
70a194b9
FB
257}
258
83fcb515
FB
259
260#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
261 type5,arg5,type6,arg6) \
8fcd3692
BS
262static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
263 type6 arg6) \
83fcb515
FB
264{ \
265 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
70a194b9 266}
83fcb515 267
70a194b9 268
31e31b8a 269#define __NR_sys_uname __NR_uname
72f03900 270#define __NR_sys_getcwd1 __NR_getcwd
72f03900 271#define __NR_sys_getdents __NR_getdents
dab2ed99 272#define __NR_sys_getdents64 __NR_getdents64
c6cda17a 273#define __NR_sys_getpriority __NR_getpriority
66fb9763 274#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
cf8b8bfc 275#define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
7494b0f9 276#define __NR_sys_syslog __NR_syslog
14690296
AF
277#if defined(__NR_futex)
278# define __NR_sys_futex __NR_futex
279#endif
280#if defined(__NR_futex_time64)
281# define __NR_sys_futex_time64 __NR_futex_time64
282#endif
efa92184 283#define __NR_sys_statx __NR_statx
31e31b8a 284
b1cef6d0 285#if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
9af9eaaa
FB
286#define __NR__llseek __NR_lseek
287#endif
288
a29e5ba2
JH
289/* Newer kernel ports have llseek() instead of _llseek() */
290#if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
291#define TARGET_NR__llseek TARGET_NR_llseek
292#endif
293
78721301
HD
294/* some platforms need to mask more bits than just TARGET_O_NONBLOCK */
295#ifndef TARGET_O_NONBLOCK_MASK
296#define TARGET_O_NONBLOCK_MASK TARGET_O_NONBLOCK
297#endif
298
71ba74f6
DB
299#define __NR_sys_gettid __NR_gettid
300_syscall0(int, sys_gettid)
2b3f64cb
PM
301
302/* For the 64-bit guest on 32-bit host case we must emulate
303 * getdents using getdents64, because otherwise the host
304 * might hand us back more dirent records than we can fit
305 * into the guest buffer after structure format conversion.
306 * Otherwise we emulate getdents with getdents if the host has it.
307 */
308#if defined(__NR_getdents) && HOST_LONG_BITS >= TARGET_ABI_BITS
309#define EMULATE_GETDENTS_WITH_GETDENTS
310#endif
311
312#if defined(TARGET_NR_getdents) && defined(EMULATE_GETDENTS_WITH_GETDENTS)
ac42f443 313_syscall3(int, sys_getdents, unsigned int, fd, struct linux_dirent *, dirp, unsigned int, count);
3307e236 314#endif
2b3f64cb
PM
315#if (defined(TARGET_NR_getdents) && \
316 !defined(EMULATE_GETDENTS_WITH_GETDENTS)) || \
3307e236 317 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
ac42f443 318_syscall3(int, sys_getdents64, unsigned int, fd, struct linux_dirent64 *, dirp, unsigned int, count);
3b3f24ad 319#endif
d35b261c 320#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
ac42f443
JQ
321_syscall5(int, _llseek, unsigned int, fd, unsigned long, hi, unsigned long, lo,
322 loff_t *, res, unsigned int, wh);
3b3f24ad 323#endif
c1a402a7 324_syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo)
cf8b8bfc
MS
325_syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig,
326 siginfo_t *, uinfo)
3b3f24ad 327_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
3b3f24ad
AJ
328#ifdef __NR_exit_group
329_syscall1(int,exit_group,int,error_code)
330#endif
af804f39
HD
331#if defined(__NR_close_range) && defined(TARGET_NR_close_range)
332#define __NR_sys_close_range __NR_close_range
333_syscall3(int,sys_close_range,int,first,int,last,int,flags)
334#ifndef CLOSE_RANGE_CLOEXEC
335#define CLOSE_RANGE_CLOEXEC (1U << 2)
336#endif
337#endif
14690296 338#if defined(__NR_futex)
3b3f24ad
AJ
339_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
340 const struct timespec *,timeout,int *,uaddr2,int,val3)
341#endif
14690296
AF
342#if defined(__NR_futex_time64)
343_syscall6(int,sys_futex_time64,int *,uaddr,int,op,int,val,
344 const struct timespec *,timeout,int *,uaddr2,int,val3)
345#endif
cc054c6f
HD
346#if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
347_syscall2(int, pidfd_open, pid_t, pid, unsigned int, flags);
348#endif
349#if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
350_syscall4(int, pidfd_send_signal, int, pidfd, int, sig, siginfo_t *, info,
351 unsigned int, flags);
352#endif
353#if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
354_syscall3(int, pidfd_getfd, int, pidfd, int, targetfd, unsigned int, flags);
355#endif
737de1d1
MF
356#define __NR_sys_sched_getaffinity __NR_sched_getaffinity
357_syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
358 unsigned long *, user_mask_ptr);
359#define __NR_sys_sched_setaffinity __NR_sched_setaffinity
360_syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
361 unsigned long *, user_mask_ptr);
45ad761c
TT
362/* sched_attr is not defined in glibc */
363struct sched_attr {
364 uint32_t size;
365 uint32_t sched_policy;
366 uint64_t sched_flags;
367 int32_t sched_nice;
368 uint32_t sched_priority;
369 uint64_t sched_runtime;
370 uint64_t sched_deadline;
371 uint64_t sched_period;
372 uint32_t sched_util_min;
373 uint32_t sched_util_max;
374};
375#define __NR_sys_sched_getattr __NR_sched_getattr
376_syscall4(int, sys_sched_getattr, pid_t, pid, struct sched_attr *, attr,
377 unsigned int, size, unsigned int, flags);
378#define __NR_sys_sched_setattr __NR_sched_setattr
379_syscall3(int, sys_sched_setattr, pid_t, pid, struct sched_attr *, attr,
380 unsigned int, flags);
407a119b
TT
381#define __NR_sys_sched_getscheduler __NR_sched_getscheduler
382_syscall1(int, sys_sched_getscheduler, pid_t, pid);
383#define __NR_sys_sched_setscheduler __NR_sched_setscheduler
384_syscall3(int, sys_sched_setscheduler, pid_t, pid, int, policy,
385 const struct sched_param *, param);
386#define __NR_sys_sched_getparam __NR_sched_getparam
387_syscall2(int, sys_sched_getparam, pid_t, pid,
388 struct sched_param *, param);
389#define __NR_sys_sched_setparam __NR_sched_setparam
390_syscall2(int, sys_sched_setparam, pid_t, pid,
391 const struct sched_param *, param);
b827c3ed
ST
392#define __NR_sys_getcpu __NR_getcpu
393_syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
0f6b4d21
AG
394_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
395 void *, arg);
e0eb210e
PM
396_syscall2(int, capget, struct __user_cap_header_struct *, header,
397 struct __user_cap_data_struct *, data);
398_syscall2(int, capset, struct __user_cap_header_struct *, header,
399 struct __user_cap_data_struct *, data);
ab31cda3
PB
400#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
401_syscall2(int, ioprio_get, int, which, int, who)
402#endif
403#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
404_syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
405#endif
f894efd1
LV
406#if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
407_syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
408#endif
3b3f24ad 409
2f14788c
LV
410#if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
411_syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
412 unsigned long, idx1, unsigned long, idx2)
413#endif
414
efa92184
AR
415/*
416 * It is assumed that struct statx is architecture independent.
417 */
418#if defined(TARGET_NR_statx) && defined(__NR_statx)
419_syscall5(int, sys_statx, int, dirfd, const char *, pathname, int, flags,
420 unsigned int, mask, struct target_statx *, statxbuf)
421#endif
8500476f
AS
422#if defined(TARGET_NR_membarrier) && defined(__NR_membarrier)
423_syscall2(int, membarrier, int, cmd, int, flags)
424#endif
efa92184 425
180d4ef3 426static const bitmask_transtbl fcntl_flags_tbl[] = {
3b3f24ad
AJ
427 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
428 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
429 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
430 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
431 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
432 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
433 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
434 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
afc8763f 435 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
3b3f24ad
AJ
436 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
437 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
438 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
439 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
3b3f24ad
AJ
440#if defined(O_DIRECT)
441 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
afc8763f
RH
442#endif
443#if defined(O_NOATIME)
444 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
445#endif
446#if defined(O_CLOEXEC)
447 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
448#endif
449#if defined(O_PATH)
450 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
5f9cee46
RV
451#endif
452#if defined(O_TMPFILE)
453 { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
afc8763f
RH
454#endif
455 /* Don't terminate the list prematurely on 64-bit host+guest. */
456#if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
457 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
3b3f24ad 458#endif
3b3f24ad
AJ
459};
460
0f6bb195 461_syscall2(int, sys_getcwd1, char *, buf, size_t, size)
3b3f24ad 462
cac46eb0 463#if defined(TARGET_NR_utimensat) || defined(TARGET_NR_utimensat_time64)
700fa58e 464#if defined(__NR_utimensat)
1acae9f2 465#define __NR_sys_utimensat __NR_utimensat
9007f0ef
TS
466_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
467 const struct timespec *,tsp,int,flags)
1acae9f2
PM
468#else
469static int sys_utimensat(int dirfd, const char *pathname,
470 const struct timespec times[2], int flags)
471{
472 errno = ENOSYS;
473 return -1;
474}
9007f0ef 475#endif
1acae9f2 476#endif /* TARGET_NR_utimensat */
3b3f24ad 477
95d0307c
AS
478#ifdef TARGET_NR_renameat2
479#if defined(__NR_renameat2)
480#define __NR_sys_renameat2 __NR_renameat2
481_syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd,
482 const char *, new, unsigned int, flags)
483#else
484static int sys_renameat2(int oldfd, const char *old,
485 int newfd, const char *new, int flags)
486{
487 if (flags == 0) {
488 return renameat(oldfd, old, newfd, new);
489 }
490 errno = ENOSYS;
491 return -1;
492}
493#endif
494#endif /* TARGET_NR_renameat2 */
495
3b3f24ad 496#ifdef CONFIG_INOTIFY
8690e420 497#include <sys/inotify.h>
3b3f24ad
AJ
498#else
499/* Userspace can usually survive runtime without inotify */
500#undef TARGET_NR_inotify_init
c05c7a73 501#undef TARGET_NR_inotify_init1
3b3f24ad
AJ
502#undef TARGET_NR_inotify_add_watch
503#undef TARGET_NR_inotify_rm_watch
504#endif /* CONFIG_INOTIFY */
505
163a05a8
PM
506#if defined(TARGET_NR_prlimit64)
507#ifndef __NR_prlimit64
508# define __NR_prlimit64 -1
509#endif
510#define __NR_sys_prlimit64 __NR_prlimit64
511/* The glibc rlimit structure may not be that used by the underlying syscall */
512struct host_rlimit64 {
513 uint64_t rlim_cur;
514 uint64_t rlim_max;
515};
516_syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
517 const struct host_rlimit64 *, new_limit,
518 struct host_rlimit64 *, old_limit)
519#endif
520
f4f1e10a
ECL
521
522#if defined(TARGET_NR_timer_create)
6f9ff551 523/* Maximum of 32 active POSIX timers allowed at any one time. */
9e59899f
PM
524#define GUEST_TIMER_MAX 32
525static timer_t g_posix_timers[GUEST_TIMER_MAX];
526static int g_posix_timer_allocated[GUEST_TIMER_MAX];
f4f1e10a
ECL
527
528static inline int next_free_host_timer(void)
529{
9e59899f
PM
530 int k;
531 for (k = 0; k < ARRAY_SIZE(g_posix_timer_allocated); k++) {
532 if (qatomic_xchg(g_posix_timer_allocated + k, 1) == 0) {
f4f1e10a
ECL
533 return k;
534 }
535 }
536 return -1;
537}
9e59899f
PM
538
539static inline void free_host_timer_slot(int id)
540{
541 qatomic_store_release(g_posix_timer_allocated + id, 0);
542}
f4f1e10a
ECL
543#endif
544
3ffe3268 545static inline int host_to_target_errno(int host_errno)
637947f1 546{
3ffe3268
PMD
547 switch (host_errno) {
548#define E(X) case X: return TARGET_##X;
549#include "errnos.c.inc"
550#undef E
551 default:
552 return host_errno;
2466119c 553 }
637947f1
TS
554}
555
3ffe3268 556static inline int target_to_host_errno(int target_errno)
b92c47c1 557{
3ffe3268
PMD
558 switch (target_errno) {
559#define E(X) case TARGET_##X: return X;
560#include "errnos.c.inc"
561#undef E
562 default:
563 return target_errno;
2466119c 564 }
b92c47c1
TS
565}
566
892a4f6a 567abi_long get_errno(abi_long ret)
31e31b8a
FB
568{
569 if (ret == -1)
637947f1 570 return -host_to_target_errno(errno);
31e31b8a
FB
571 else
572 return ret;
573}
574
7dcdaeaf 575const char *target_strerror(int err)
b92c47c1 576{
af254a27 577 if (err == QEMU_ERESTARTSYS) {
da2a34f7
PM
578 return "To be restarted";
579 }
57a0c938 580 if (err == QEMU_ESIGRETURN) {
da2a34f7
PM
581 return "Successful exit from sigreturn";
582 }
583
b92c47c1
TS
584 return strerror(target_to_host_errno(err));
585}
586
45ad761c
TT
587static int check_zeroed_user(abi_long addr, size_t ksize, size_t usize)
588{
589 int i;
590 uint8_t b;
591 if (usize <= ksize) {
592 return 1;
593 }
594 for (i = ksize; i < usize; i++) {
595 if (get_user_u8(b, addr + i)) {
596 return -TARGET_EFAULT;
597 }
598 if (b != 0) {
599 return 0;
600 }
601 }
602 return 1;
603}
604
4d330cee
TB
605#define safe_syscall0(type, name) \
606static type safe_##name(void) \
607{ \
608 return safe_syscall(__NR_##name); \
609}
610
611#define safe_syscall1(type, name, type1, arg1) \
612static type safe_##name(type1 arg1) \
613{ \
614 return safe_syscall(__NR_##name, arg1); \
615}
616
617#define safe_syscall2(type, name, type1, arg1, type2, arg2) \
618static type safe_##name(type1 arg1, type2 arg2) \
619{ \
620 return safe_syscall(__NR_##name, arg1, arg2); \
621}
622
623#define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
624static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
625{ \
626 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
627}
628
629#define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
630 type4, arg4) \
631static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
632{ \
633 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
634}
635
636#define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
637 type4, arg4, type5, arg5) \
638static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
639 type5 arg5) \
640{ \
641 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
642}
643
644#define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
645 type4, arg4, type5, arg5, type6, arg6) \
646static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
647 type5 arg5, type6 arg6) \
648{ \
649 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
650}
651
50afd02b
TB
652safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
653safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
c10a0738
TB
654safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
655 int, flags, mode_t, mode)
859e8a89 656#if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
4af80a37
TB
657safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
658 struct rusage *, rusage)
859e8a89 659#endif
4af80a37
TB
660safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
661 int, options, struct rusage *, rusage)
7a8d9f3a 662safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
55bbe4d5
DD
663safe_syscall5(int, execveat, int, dirfd, const char *, filename,
664 char **, argv, char **, envp, int, flags)
859e8a89 665#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
e5ce9688 666 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
6df9d38d
PM
667safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
668 fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
859e8a89 669#endif
e5ce9688 670#if defined(TARGET_NR_ppoll) || defined(TARGET_NR_ppoll_time64)
a6130237
PM
671safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
672 struct timespec *, tsp, const sigset_t *, sigmask,
673 size_t, sigsetsize)
859e8a89 674#endif
227f0214
PM
675safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
676 int, maxevents, int, timeout, const sigset_t *, sigmask,
677 size_t, sigsetsize)
14690296 678#if defined(__NR_futex)
d509eeb1
PM
679safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
680 const struct timespec *,timeout,int *,uaddr2,int,val3)
859e8a89 681#endif
14690296
AF
682#if defined(__NR_futex_time64)
683safe_syscall6(int,futex_time64,int *,uaddr,int,op,int,val, \
684 const struct timespec *,timeout,int *,uaddr2,int,val3)
685#endif
2fe4fba1 686safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
bef653d9
PM
687safe_syscall2(int, kill, pid_t, pid, int, sig)
688safe_syscall2(int, tkill, int, tid, int, sig)
689safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
918c03ed
PM
690safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
691safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
0f26386c
DJ
692safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
693 unsigned long, pos_l, unsigned long, pos_h)
f8d00fba
DJ
694safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
695 unsigned long, pos_l, unsigned long, pos_h)
2a3c7619
PM
696safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
697 socklen_t, addrlen)
66687530
PM
698safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
699 int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
700safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
701 int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
702safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
703safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
2a845989 704safe_syscall2(int, flock, int, fd, int, operation)
ddcbde15 705#if defined(TARGET_NR_rt_sigtimedwait) || defined(TARGET_NR_rt_sigtimedwait_time64)
b3f82330
PM
706safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
707 const struct timespec *, uts, size_t, sigsetsize)
859e8a89 708#endif
ff6dc130
PM
709safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
710 int, flags)
859e8a89 711#if defined(TARGET_NR_nanosleep)
9e518226
PM
712safe_syscall2(int, nanosleep, const struct timespec *, req,
713 struct timespec *, rem)
859e8a89 714#endif
6ac03b2c
FB
715#if defined(TARGET_NR_clock_nanosleep) || \
716 defined(TARGET_NR_clock_nanosleep_time64)
9e518226
PM
717safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
718 const struct timespec *, req, struct timespec *, rem)
719#endif
524fa340 720#ifdef __NR_ipc
d8c08b1e
MK
721#ifdef __s390x__
722safe_syscall5(int, ipc, int, call, long, first, long, second, long, third,
723 void *, ptr)
724#else
89f9fe44
PM
725safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
726 void *, ptr, long, fifth)
86e63695 727#endif
d8c08b1e 728#endif
86e63695
LV
729#ifdef __NR_msgsnd
730safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
731 int, flags)
86e63695
LV
732#endif
733#ifdef __NR_msgrcv
734safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
735 long, msgtype, int, flags)
86e63695
LV
736#endif
737#ifdef __NR_semtimedop
738safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
739 unsigned, nsops, const struct timespec *, timeout)
89f9fe44 740#endif
d107e375
FB
741#if defined(TARGET_NR_mq_timedsend) || \
742 defined(TARGET_NR_mq_timedsend_time64)
d40ecd66
PM
743safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
744 size_t, len, unsigned, prio, const struct timespec *, timeout)
859e8a89 745#endif
d107e375
FB
746#if defined(TARGET_NR_mq_timedreceive) || \
747 defined(TARGET_NR_mq_timedreceive_time64)
d40ecd66
PM
748safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
749 size_t, len, unsigned *, prio, const struct timespec *, timeout)
750#endif
84946457
AS
751#if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
752safe_syscall6(ssize_t, copy_file_range, int, infd, loff_t *, pinoff,
753 int, outfd, loff_t *, poutoff, size_t, length,
754 unsigned int, flags)
755#endif
756
49ca6f3e
PM
757/* We do ioctl like this rather than via safe_syscall3 to preserve the
758 * "third argument might be integer or pointer or not present" behaviour of
759 * the libc function.
760 */
761#define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
435da5e7
PM
762/* Similarly for fcntl. Note that callers must always:
763 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
764 * use the flock64 struct rather than unsuffixed flock
765 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
766 */
767#ifdef __NR_fcntl64
768#define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
769#else
770#define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
771#endif
50afd02b 772
8289d112
PB
773static inline int host_to_target_sock_type(int host_type)
774{
775 int target_type;
776
777 switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
778 case SOCK_DGRAM:
779 target_type = TARGET_SOCK_DGRAM;
780 break;
781 case SOCK_STREAM:
782 target_type = TARGET_SOCK_STREAM;
783 break;
784 default:
785 target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
786 break;
787 }
788
789#if defined(SOCK_CLOEXEC)
790 if (host_type & SOCK_CLOEXEC) {
791 target_type |= TARGET_SOCK_CLOEXEC;
792 }
793#endif
794
795#if defined(SOCK_NONBLOCK)
796 if (host_type & SOCK_NONBLOCK) {
797 target_type |= TARGET_SOCK_NONBLOCK;
798 }
799#endif
800
801 return target_type;
802}
803
dfe49864 804static abi_ulong target_brk, initial_target_brk;
31e31b8a 805
992f48a0 806void target_set_brk(abi_ulong new_brk)
31e31b8a 807{
d28b3c90 808 target_brk = TARGET_PAGE_ALIGN(new_brk);
dfe49864 809 initial_target_brk = target_brk;
31e31b8a
FB
810}
811
0da46a6e 812/* do_brk() must return target values and target errnos. */
86f04735 813abi_long do_brk(abi_ulong brk_val)
31e31b8a 814{
992f48a0 815 abi_long mapped_addr;
2aea137a
AO
816 abi_ulong new_brk;
817 abi_ulong old_brk;
31e31b8a 818
ee1bf83d
RH
819 /* brk pointers are always untagged */
820
dfe49864
HD
821 /* do not allow to shrink below initial brk value */
822 if (brk_val < initial_target_brk) {
cb9d5d1f 823 return target_brk;
dfe49864
HD
824 }
825
86f04735 826 new_brk = TARGET_PAGE_ALIGN(brk_val);
2aea137a 827 old_brk = TARGET_PAGE_ALIGN(target_brk);
86f04735 828
2aea137a
AO
829 /* new and old target_brk might be on the same page */
830 if (new_brk == old_brk) {
86f04735 831 target_brk = brk_val;
7ab240ad 832 return target_brk;
4d1de87c 833 }
3b46e624 834
2cf91b9a 835 /* Release heap if necessary */
2aea137a
AO
836 if (new_brk < old_brk) {
837 target_munmap(new_brk, old_brk - new_brk);
86f04735
HD
838
839 target_brk = brk_val;
840 return target_brk;
31e31b8a
FB
841 }
842
2aea137a
AO
843 mapped_addr = target_mmap(old_brk, new_brk - old_brk,
844 PROT_READ | PROT_WRITE,
845 MAP_FIXED_NOREPLACE | MAP_ANON | MAP_PRIVATE,
846 -1, 0);
70afc343 847
2aea137a 848 if (mapped_addr == old_brk) {
86f04735 849 target_brk = brk_val;
00faf08c 850 return target_brk;
00faf08c 851 }
7ab240ad 852
7dd46c02
RH
853#if defined(TARGET_ALPHA)
854 /* We (partially) emulate OSF/1 on Alpha, which requires we
855 return a proper errno, not an unchanged brk value. */
00faf08c 856 return -TARGET_ENOMEM;
7dd46c02 857#endif
00faf08c 858 /* For everything else, return the previous break. */
7ab240ad 859 return target_brk;
31e31b8a
FB
860}
861
859e8a89 862#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect) || \
e5ce9688 863 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
26edcf41
TS
864static inline abi_long copy_from_user_fdset(fd_set *fds,
865 abi_ulong target_fds_addr,
866 int n)
31e31b8a 867{
26edcf41
TS
868 int i, nw, j, k;
869 abi_ulong b, *target_fds;
870
b1b2db29 871 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
26edcf41
TS
872 if (!(target_fds = lock_user(VERIFY_READ,
873 target_fds_addr,
874 sizeof(abi_ulong) * nw,
875 1)))
876 return -TARGET_EFAULT;
877
878 FD_ZERO(fds);
879 k = 0;
880 for (i = 0; i < nw; i++) {
881 /* grab the abi_ulong */
882 __get_user(b, &target_fds[i]);
883 for (j = 0; j < TARGET_ABI_BITS; j++) {
884 /* check the bit inside the abi_ulong */
885 if ((b >> j) & 1)
886 FD_SET(k, fds);
887 k++;
31e31b8a 888 }
31e31b8a 889 }
26edcf41
TS
890
891 unlock_user(target_fds, target_fds_addr, 0);
892
893 return 0;
31e31b8a
FB
894}
895
055e0906
MF
896static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
897 abi_ulong target_fds_addr,
898 int n)
899{
900 if (target_fds_addr) {
901 if (copy_from_user_fdset(fds, target_fds_addr, n))
902 return -TARGET_EFAULT;
903 *fds_ptr = fds;
904 } else {
905 *fds_ptr = NULL;
906 }
907 return 0;
908}
909
26edcf41
TS
910static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
911 const fd_set *fds,
912 int n)
31e31b8a 913{
31e31b8a 914 int i, nw, j, k;
992f48a0 915 abi_long v;
26edcf41 916 abi_ulong *target_fds;
31e31b8a 917
b1b2db29 918 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
26edcf41
TS
919 if (!(target_fds = lock_user(VERIFY_WRITE,
920 target_fds_addr,
921 sizeof(abi_ulong) * nw,
922 0)))
923 return -TARGET_EFAULT;
924
925 k = 0;
926 for (i = 0; i < nw; i++) {
927 v = 0;
928 for (j = 0; j < TARGET_ABI_BITS; j++) {
9ab709be 929 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
26edcf41 930 k++;
31e31b8a 931 }
26edcf41 932 __put_user(v, &target_fds[i]);
31e31b8a 933 }
26edcf41
TS
934
935 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
936
937 return 0;
31e31b8a 938}
859e8a89 939#endif
31e31b8a 940
c596ed17
FB
941#if defined(__alpha__)
942#define HOST_HZ 1024
943#else
944#define HOST_HZ 100
945#endif
946
992f48a0 947static inline abi_long host_to_target_clock_t(long ticks)
c596ed17
FB
948{
949#if HOST_HZ == TARGET_HZ
950 return ticks;
951#else
952 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
953#endif
954}
955
579a97f7
FB
956static inline abi_long host_to_target_rusage(abi_ulong target_addr,
957 const struct rusage *rusage)
b409186b 958{
53a5960a
PB
959 struct target_rusage *target_rusage;
960
579a97f7
FB
961 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
962 return -TARGET_EFAULT;
cbb21eed
MB
963 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
964 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
965 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
966 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
967 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
968 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
969 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
970 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
971 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
972 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
973 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
974 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
975 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
976 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
977 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
978 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
979 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
980 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
53a5960a 981 unlock_user_struct(target_rusage, target_addr, 1);
579a97f7
FB
982
983 return 0;
b409186b
FB
984}
985
859e8a89 986#ifdef TARGET_NR_setrlimit
cbb21eed 987static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
81bbe906 988{
cbb21eed 989 abi_ulong target_rlim_swap;
95b33b2f
WT
990 rlim_t result;
991
cbb21eed
MB
992 target_rlim_swap = tswapal(target_rlim);
993 if (target_rlim_swap == TARGET_RLIM_INFINITY)
994 return RLIM_INFINITY;
995
996 result = target_rlim_swap;
997 if (target_rlim_swap != (rlim_t)result)
998 return RLIM_INFINITY;
95b33b2f
WT
999
1000 return result;
81bbe906 1001}
859e8a89 1002#endif
81bbe906 1003
859e8a89 1004#if defined(TARGET_NR_getrlimit) || defined(TARGET_NR_ugetrlimit)
cbb21eed 1005static inline abi_ulong host_to_target_rlim(rlim_t rlim)
81bbe906 1006{
cbb21eed
MB
1007 abi_ulong target_rlim_swap;
1008 abi_ulong result;
95b33b2f 1009
cbb21eed 1010 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
95b33b2f 1011 target_rlim_swap = TARGET_RLIM_INFINITY;
81bbe906 1012 else
95b33b2f 1013 target_rlim_swap = rlim;
cbb21eed 1014 result = tswapal(target_rlim_swap);
95b33b2f
WT
1015
1016 return result;
81bbe906 1017}
859e8a89 1018#endif
81bbe906 1019
e22b7015
WT
1020static inline int target_to_host_resource(int code)
1021{
1022 switch (code) {
1023 case TARGET_RLIMIT_AS:
1024 return RLIMIT_AS;
1025 case TARGET_RLIMIT_CORE:
1026 return RLIMIT_CORE;
1027 case TARGET_RLIMIT_CPU:
1028 return RLIMIT_CPU;
1029 case TARGET_RLIMIT_DATA:
1030 return RLIMIT_DATA;
1031 case TARGET_RLIMIT_FSIZE:
1032 return RLIMIT_FSIZE;
1033 case TARGET_RLIMIT_LOCKS:
1034 return RLIMIT_LOCKS;
1035 case TARGET_RLIMIT_MEMLOCK:
1036 return RLIMIT_MEMLOCK;
1037 case TARGET_RLIMIT_MSGQUEUE:
1038 return RLIMIT_MSGQUEUE;
1039 case TARGET_RLIMIT_NICE:
1040 return RLIMIT_NICE;
1041 case TARGET_RLIMIT_NOFILE:
1042 return RLIMIT_NOFILE;
1043 case TARGET_RLIMIT_NPROC:
1044 return RLIMIT_NPROC;
1045 case TARGET_RLIMIT_RSS:
1046 return RLIMIT_RSS;
1047 case TARGET_RLIMIT_RTPRIO:
1048 return RLIMIT_RTPRIO;
c3a28d71 1049#ifdef RLIMIT_RTTIME
244fd083
SB
1050 case TARGET_RLIMIT_RTTIME:
1051 return RLIMIT_RTTIME;
c3a28d71 1052#endif
e22b7015
WT
1053 case TARGET_RLIMIT_SIGPENDING:
1054 return RLIMIT_SIGPENDING;
1055 case TARGET_RLIMIT_STACK:
1056 return RLIMIT_STACK;
1057 default:
1058 return code;
1059 }
1060}
1061
788f5ec4
TS
1062static inline abi_long copy_from_user_timeval(struct timeval *tv,
1063 abi_ulong target_tv_addr)
31e31b8a 1064{
53a5960a
PB
1065 struct target_timeval *target_tv;
1066
6d5d5dde 1067 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
579a97f7 1068 return -TARGET_EFAULT;
6d5d5dde 1069 }
788f5ec4
TS
1070
1071 __get_user(tv->tv_sec, &target_tv->tv_sec);
1072 __get_user(tv->tv_usec, &target_tv->tv_usec);
1073
1074 unlock_user_struct(target_tv, target_tv_addr, 0);
579a97f7
FB
1075
1076 return 0;
31e31b8a
FB
1077}
1078
788f5ec4
TS
1079static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1080 const struct timeval *tv)
31e31b8a 1081{
53a5960a
PB
1082 struct target_timeval *target_tv;
1083
6d5d5dde
DB
1084 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
1085 return -TARGET_EFAULT;
1086 }
1087
1088 __put_user(tv->tv_sec, &target_tv->tv_sec);
1089 __put_user(tv->tv_usec, &target_tv->tv_usec);
1090
1091 unlock_user_struct(target_tv, target_tv_addr, 1);
1092
1093 return 0;
1094}
1095
6ac03b2c
FB
1096#if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
1097static inline abi_long copy_from_user_timeval64(struct timeval *tv,
1098 abi_ulong target_tv_addr)
1099{
1100 struct target__kernel_sock_timeval *target_tv;
1101
1102 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) {
1103 return -TARGET_EFAULT;
1104 }
1105
1106 __get_user(tv->tv_sec, &target_tv->tv_sec);
1107 __get_user(tv->tv_usec, &target_tv->tv_usec);
1108
1109 unlock_user_struct(target_tv, target_tv_addr, 0);
1110
1111 return 0;
1112}
1113#endif
1114
6d5d5dde 1115static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr,
6ac03b2c 1116 const struct timeval *tv)
6d5d5dde
DB
1117{
1118 struct target__kernel_sock_timeval *target_tv;
1119
1120 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) {
579a97f7 1121 return -TARGET_EFAULT;
6d5d5dde 1122 }
788f5ec4
TS
1123
1124 __put_user(tv->tv_sec, &target_tv->tv_sec);
1125 __put_user(tv->tv_usec, &target_tv->tv_usec);
1126
1127 unlock_user_struct(target_tv, target_tv_addr, 1);
579a97f7
FB
1128
1129 return 0;
31e31b8a
FB
1130}
1131
859e8a89
AF
1132#if defined(TARGET_NR_futex) || \
1133 defined(TARGET_NR_rt_sigtimedwait) || \
1134 defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6) || \
1135 defined(TARGET_NR_nanosleep) || defined(TARGET_NR_clock_settime) || \
1136 defined(TARGET_NR_utimensat) || defined(TARGET_NR_mq_timedsend) || \
d8c08b1e 1137 defined(TARGET_NR_mq_timedreceive) || defined(TARGET_NR_ipc) || \
2c86c90f
FB
1138 defined(TARGET_NR_semop) || defined(TARGET_NR_semtimedop) || \
1139 defined(TARGET_NR_timer_settime) || \
1140 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
6d5d5dde
DB
1141static inline abi_long target_to_host_timespec(struct timespec *host_ts,
1142 abi_ulong target_addr)
1143{
1144 struct target_timespec *target_ts;
1145
1146 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
1147 return -TARGET_EFAULT;
1148 }
1149 __get_user(host_ts->tv_sec, &target_ts->tv_sec);
1150 __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
1151 unlock_user_struct(target_ts, target_addr, 0);
1152 return 0;
1153}
859e8a89 1154#endif
6d5d5dde 1155
828cb3a1
FB
1156#if defined(TARGET_NR_clock_settime64) || defined(TARGET_NR_futex_time64) || \
1157 defined(TARGET_NR_timer_settime64) || \
d107e375
FB
1158 defined(TARGET_NR_mq_timedsend_time64) || \
1159 defined(TARGET_NR_mq_timedreceive_time64) || \
6ac03b2c 1160 (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)) || \
ddcbde15 1161 defined(TARGET_NR_clock_nanosleep_time64) || \
cac46eb0
FB
1162 defined(TARGET_NR_rt_sigtimedwait_time64) || \
1163 defined(TARGET_NR_utimensat) || \
1164 defined(TARGET_NR_utimensat_time64) || \
e5ce9688
FB
1165 defined(TARGET_NR_semtimedop_time64) || \
1166 defined(TARGET_NR_pselect6_time64) || defined(TARGET_NR_ppoll_time64)
c6c8d102
AF
1167static inline abi_long target_to_host_timespec64(struct timespec *host_ts,
1168 abi_ulong target_addr)
1169{
1170 struct target__kernel_timespec *target_ts;
1171
1172 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) {
1173 return -TARGET_EFAULT;
1174 }
1175 __get_user(host_ts->tv_sec, &target_ts->tv_sec);
1176 __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
00576757
LV
1177 /* in 32bit mode, this drops the padding */
1178 host_ts->tv_nsec = (long)(abi_long)host_ts->tv_nsec;
c6c8d102
AF
1179 unlock_user_struct(target_ts, target_addr, 0);
1180 return 0;
1181}
1182#endif
1183
6d5d5dde
DB
1184static inline abi_long host_to_target_timespec(abi_ulong target_addr,
1185 struct timespec *host_ts)
1186{
1187 struct target_timespec *target_ts;
1188
1189 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
1190 return -TARGET_EFAULT;
1191 }
1192 __put_user(host_ts->tv_sec, &target_ts->tv_sec);
1193 __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
1194 unlock_user_struct(target_ts, target_addr, 1);
1195 return 0;
1196}
1197
1198static inline abi_long host_to_target_timespec64(abi_ulong target_addr,
1199 struct timespec *host_ts)
1200{
1201 struct target__kernel_timespec *target_ts;
1202
1203 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) {
1204 return -TARGET_EFAULT;
1205 }
1206 __put_user(host_ts->tv_sec, &target_ts->tv_sec);
1207 __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
1208 unlock_user_struct(target_ts, target_addr, 1);
1209 return 0;
1210}
1211
a52f5f87
RH
1212#if defined(TARGET_NR_gettimeofday)
1213static inline abi_long copy_to_user_timezone(abi_ulong target_tz_addr,
1214 struct timezone *tz)
1215{
1216 struct target_timezone *target_tz;
1217
1218 if (!lock_user_struct(VERIFY_WRITE, target_tz, target_tz_addr, 1)) {
1219 return -TARGET_EFAULT;
1220 }
1221
1222 __put_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1223 __put_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1224
1225 unlock_user_struct(target_tz, target_tz_addr, 1);
1226
1227 return 0;
1228}
1229#endif
1230
859e8a89 1231#if defined(TARGET_NR_settimeofday)
ef4467e9
PB
1232static inline abi_long copy_from_user_timezone(struct timezone *tz,
1233 abi_ulong target_tz_addr)
1234{
1235 struct target_timezone *target_tz;
1236
1237 if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
1238 return -TARGET_EFAULT;
1239 }
1240
1241 __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1242 __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1243
1244 unlock_user_struct(target_tz, target_tz_addr, 0);
1245
1246 return 0;
1247}
859e8a89 1248#endif
ef4467e9 1249
8ec9cf89
NF
1250#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1251#include <mqueue.h>
1252
24e1003a
AJ
1253static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1254 abi_ulong target_mq_attr_addr)
1255{
1256 struct target_mq_attr *target_mq_attr;
1257
1258 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1259 target_mq_attr_addr, 1))
1260 return -TARGET_EFAULT;
1261
1262 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1263 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1264 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1265 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1266
1267 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1268
1269 return 0;
1270}
1271
1272static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1273 const struct mq_attr *attr)
1274{
1275 struct target_mq_attr *target_mq_attr;
1276
1277 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1278 target_mq_attr_addr, 0))
1279 return -TARGET_EFAULT;
1280
1281 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1282 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1283 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1284 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1285
1286 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1287
1288 return 0;
1289}
8ec9cf89 1290#endif
31e31b8a 1291
055e0906 1292#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
0da46a6e 1293/* do_select() must return target values and target errnos. */
992f48a0 1294static abi_long do_select(int n,
26edcf41
TS
1295 abi_ulong rfd_addr, abi_ulong wfd_addr,
1296 abi_ulong efd_addr, abi_ulong target_tv_addr)
31e31b8a
FB
1297{
1298 fd_set rfds, wfds, efds;
1299 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
6df9d38d
PM
1300 struct timeval tv;
1301 struct timespec ts, *ts_ptr;
992f48a0 1302 abi_long ret;
31e31b8a 1303
055e0906
MF
1304 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1305 if (ret) {
1306 return ret;
53a5960a 1307 }
055e0906
MF
1308 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1309 if (ret) {
1310 return ret;
53a5960a 1311 }
055e0906
MF
1312 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1313 if (ret) {
1314 return ret;
53a5960a 1315 }
3b46e624 1316
26edcf41 1317 if (target_tv_addr) {
788f5ec4
TS
1318 if (copy_from_user_timeval(&tv, target_tv_addr))
1319 return -TARGET_EFAULT;
6df9d38d
PM
1320 ts.tv_sec = tv.tv_sec;
1321 ts.tv_nsec = tv.tv_usec * 1000;
1322 ts_ptr = &ts;
31e31b8a 1323 } else {
6df9d38d 1324 ts_ptr = NULL;
31e31b8a 1325 }
26edcf41 1326
6df9d38d
PM
1327 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1328 ts_ptr, NULL));
53a5960a 1329
26edcf41
TS
1330 if (!is_error(ret)) {
1331 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1332 return -TARGET_EFAULT;
1333 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1334 return -TARGET_EFAULT;
1335 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1336 return -TARGET_EFAULT;
31e31b8a 1337
6df9d38d
PM
1338 if (target_tv_addr) {
1339 tv.tv_sec = ts.tv_sec;
1340 tv.tv_usec = ts.tv_nsec / 1000;
1341 if (copy_to_user_timeval(target_tv_addr, &tv)) {
1342 return -TARGET_EFAULT;
1343 }
1344 }
31e31b8a 1345 }
579a97f7 1346
31e31b8a
FB
1347 return ret;
1348}
5457dc9e
LV
1349
1350#if defined(TARGET_WANT_OLD_SYS_SELECT)
1351static abi_long do_old_select(abi_ulong arg1)
1352{
1353 struct target_sel_arg_struct *sel;
1354 abi_ulong inp, outp, exp, tvp;
1355 long nsel;
1356
1357 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
1358 return -TARGET_EFAULT;
1359 }
1360
1361 nsel = tswapal(sel->n);
1362 inp = tswapal(sel->inp);
1363 outp = tswapal(sel->outp);
1364 exp = tswapal(sel->exp);
1365 tvp = tswapal(sel->tvp);
1366
1367 unlock_user_struct(sel, arg1, 0);
1368
1369 return do_select(nsel, inp, outp, exp, tvp);
1370}
1371#endif
055e0906 1372#endif
31e31b8a 1373
e5ce9688
FB
1374#if defined(TARGET_NR_pselect6) || defined(TARGET_NR_pselect6_time64)
1375static abi_long do_pselect6(abi_long arg1, abi_long arg2, abi_long arg3,
1376 abi_long arg4, abi_long arg5, abi_long arg6,
1377 bool time64)
1378{
1379 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
1380 fd_set rfds, wfds, efds;
1381 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
1382 struct timespec ts, *ts_ptr;
1383 abi_long ret;
1384
1385 /*
1386 * The 6th arg is actually two args smashed together,
1387 * so we cannot use the C library.
1388 */
e5ce9688
FB
1389 struct {
1390 sigset_t *set;
1391 size_t size;
1392 } sig, *sig_ptr;
1393
1394 abi_ulong arg_sigset, arg_sigsize, *arg7;
e5ce9688
FB
1395
1396 n = arg1;
1397 rfd_addr = arg2;
1398 wfd_addr = arg3;
1399 efd_addr = arg4;
1400 ts_addr = arg5;
1401
1402 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1403 if (ret) {
1404 return ret;
1405 }
1406 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1407 if (ret) {
1408 return ret;
1409 }
1410 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1411 if (ret) {
1412 return ret;
1413 }
1414
1415 /*
1416 * This takes a timespec, and not a timeval, so we cannot
1417 * use the do_select() helper ...
1418 */
1419 if (ts_addr) {
1420 if (time64) {
1421 if (target_to_host_timespec64(&ts, ts_addr)) {
1422 return -TARGET_EFAULT;
1423 }
1424 } else {
1425 if (target_to_host_timespec(&ts, ts_addr)) {
1426 return -TARGET_EFAULT;
1427 }
1428 }
1429 ts_ptr = &ts;
1430 } else {
1431 ts_ptr = NULL;
1432 }
1433
1434 /* Extract the two packed args for the sigset */
cb226034 1435 sig_ptr = NULL;
e5ce9688 1436 if (arg6) {
e5ce9688
FB
1437 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
1438 if (!arg7) {
1439 return -TARGET_EFAULT;
1440 }
1441 arg_sigset = tswapal(arg7[0]);
1442 arg_sigsize = tswapal(arg7[1]);
1443 unlock_user(arg7, arg6, 0);
1444
1445 if (arg_sigset) {
cb226034
RH
1446 ret = process_sigsuspend_mask(&sig.set, arg_sigset, arg_sigsize);
1447 if (ret != 0) {
1448 return ret;
e5ce9688 1449 }
cb226034
RH
1450 sig_ptr = &sig;
1451 sig.size = SIGSET_T_SIZE;
e5ce9688 1452 }
e5ce9688
FB
1453 }
1454
1455 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1456 ts_ptr, sig_ptr));
1457
cb226034
RH
1458 if (sig_ptr) {
1459 finish_sigsuspend_mask(ret);
1460 }
1461
e5ce9688
FB
1462 if (!is_error(ret)) {
1463 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n)) {
1464 return -TARGET_EFAULT;
1465 }
1466 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n)) {
1467 return -TARGET_EFAULT;
1468 }
1469 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n)) {
1470 return -TARGET_EFAULT;
1471 }
1472 if (time64) {
1473 if (ts_addr && host_to_target_timespec64(ts_addr, &ts)) {
1474 return -TARGET_EFAULT;
1475 }
1476 } else {
1477 if (ts_addr && host_to_target_timespec(ts_addr, &ts)) {
1478 return -TARGET_EFAULT;
1479 }
1480 }
1481 }
1482 return ret;
1483}
1484#endif
1485
1486#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll) || \
1487 defined(TARGET_NR_ppoll_time64)
1488static abi_long do_ppoll(abi_long arg1, abi_long arg2, abi_long arg3,
1489 abi_long arg4, abi_long arg5, bool ppoll, bool time64)
1490{
1491 struct target_pollfd *target_pfd;
1492 unsigned int nfds = arg2;
1493 struct pollfd *pfd;
1494 unsigned int i;
1495 abi_long ret;
1496
1497 pfd = NULL;
1498 target_pfd = NULL;
1499 if (nfds) {
1500 if (nfds > (INT_MAX / sizeof(struct target_pollfd))) {
1501 return -TARGET_EINVAL;
1502 }
1503 target_pfd = lock_user(VERIFY_WRITE, arg1,
1504 sizeof(struct target_pollfd) * nfds, 1);
1505 if (!target_pfd) {
1506 return -TARGET_EFAULT;
1507 }
1508
1509 pfd = alloca(sizeof(struct pollfd) * nfds);
1510 for (i = 0; i < nfds; i++) {
1511 pfd[i].fd = tswap32(target_pfd[i].fd);
1512 pfd[i].events = tswap16(target_pfd[i].events);
1513 }
1514 }
1515 if (ppoll) {
1516 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
db36aa7d 1517 sigset_t *set = NULL;
e5ce9688
FB
1518
1519 if (arg3) {
1520 if (time64) {
1521 if (target_to_host_timespec64(timeout_ts, arg3)) {
1522 unlock_user(target_pfd, arg1, 0);
1523 return -TARGET_EFAULT;
1524 }
1525 } else {
1526 if (target_to_host_timespec(timeout_ts, arg3)) {
1527 unlock_user(target_pfd, arg1, 0);
1528 return -TARGET_EFAULT;
1529 }
1530 }
1531 } else {
1532 timeout_ts = NULL;
1533 }
1534
1535 if (arg4) {
db36aa7d
RH
1536 ret = process_sigsuspend_mask(&set, arg4, arg5);
1537 if (ret != 0) {
e5ce9688 1538 unlock_user(target_pfd, arg1, 0);
db36aa7d 1539 return ret;
e5ce9688 1540 }
e5ce9688
FB
1541 }
1542
1543 ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
1544 set, SIGSET_T_SIZE));
1545
db36aa7d
RH
1546 if (set) {
1547 finish_sigsuspend_mask(ret);
1548 }
e5ce9688
FB
1549 if (!is_error(ret) && arg3) {
1550 if (time64) {
1551 if (host_to_target_timespec64(arg3, timeout_ts)) {
1552 return -TARGET_EFAULT;
1553 }
1554 } else {
1555 if (host_to_target_timespec(arg3, timeout_ts)) {
1556 return -TARGET_EFAULT;
1557 }
1558 }
1559 }
e5ce9688
FB
1560 } else {
1561 struct timespec ts, *pts;
1562
1563 if (arg3 >= 0) {
1564 /* Convert ms to secs, ns */
1565 ts.tv_sec = arg3 / 1000;
1566 ts.tv_nsec = (arg3 % 1000) * 1000000LL;
1567 pts = &ts;
1568 } else {
1569 /* -ve poll() timeout means "infinite" */
1570 pts = NULL;
1571 }
1572 ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0));
1573 }
1574
1575 if (!is_error(ret)) {
1576 for (i = 0; i < nfds; i++) {
1577 target_pfd[i].revents = tswap16(pfd[i].revents);
1578 }
1579 }
1580 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
1581 return ret;
1582}
1583#endif
1584
a0939b89 1585static abi_long do_pipe(CPUArchState *cpu_env, abi_ulong pipedes,
fb41a66e 1586 int flags, int is_pipe2)
099d6b0f
RV
1587{
1588 int host_pipe[2];
1589 abi_long ret;
499d8055 1590 ret = pipe2(host_pipe, flags);
099d6b0f
RV
1591
1592 if (is_error(ret))
1593 return get_errno(ret);
fb41a66e
RH
1594
1595 /* Several targets have special calling conventions for the original
1596 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1597 if (!is_pipe2) {
1598#if defined(TARGET_ALPHA)
0effdc29 1599 cpu_env->ir[IR_A4] = host_pipe[1];
fb41a66e
RH
1600 return host_pipe[0];
1601#elif defined(TARGET_MIPS)
0effdc29 1602 cpu_env->active_tc.gpr[3] = host_pipe[1];
fb41a66e
RH
1603 return host_pipe[0];
1604#elif defined(TARGET_SH4)
0effdc29 1605 cpu_env->gregs[1] = host_pipe[1];
fb41a66e 1606 return host_pipe[0];
82f05b69 1607#elif defined(TARGET_SPARC)
0effdc29 1608 cpu_env->regwptr[1] = host_pipe[1];
82f05b69 1609 return host_pipe[0];
597c0212 1610#endif
fb41a66e
RH
1611 }
1612
099d6b0f 1613 if (put_user_s32(host_pipe[0], pipedes)
6f200f51 1614 || put_user_s32(host_pipe[1], pipedes + sizeof(abi_int)))
099d6b0f 1615 return -TARGET_EFAULT;
099d6b0f
RV
1616 return get_errno(ret);
1617}
1618
7b36f782 1619static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
579a97f7
FB
1620 abi_ulong target_addr,
1621 socklen_t len)
7854b056 1622{
607175e0
AJ
1623 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1624 sa_family_t sa_family;
53a5960a
PB
1625 struct target_sockaddr *target_saddr;
1626
7b36f782
LV
1627 if (fd_trans_target_to_host_addr(fd)) {
1628 return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
1629 }
1630
579a97f7
FB
1631 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1632 if (!target_saddr)
1633 return -TARGET_EFAULT;
607175e0
AJ
1634
1635 sa_family = tswap16(target_saddr->sa_family);
1636
1637 /* Oops. The caller might send a incomplete sun_path; sun_path
1638 * must be terminated by \0 (see the manual page), but
1639 * unfortunately it is quite common to specify sockaddr_un
1640 * length as "strlen(x->sun_path)" while it should be
1641 * "strlen(...) + 1". We'll fix that here if needed.
1642 * Linux kernel has a similar feature.
1643 */
1644
1645 if (sa_family == AF_UNIX) {
1646 if (len < unix_maxlen && len > 0) {
1647 char *cp = (char*)target_saddr;
1648
1649 if ( cp[len-1] && !cp[len] )
1650 len++;
1651 }
1652 if (len > unix_maxlen)
1653 len = unix_maxlen;
1654 }
1655
53a5960a 1656 memcpy(addr, target_saddr, len);
607175e0 1657 addr->sa_family = sa_family;
6c5b5645
LV
1658 if (sa_family == AF_NETLINK) {
1659 struct sockaddr_nl *nladdr;
1660
1661 nladdr = (struct sockaddr_nl *)addr;
1662 nladdr->nl_pid = tswap32(nladdr->nl_pid);
1663 nladdr->nl_groups = tswap32(nladdr->nl_groups);
1664 } else if (sa_family == AF_PACKET) {
33a29b51
JT
1665 struct target_sockaddr_ll *lladdr;
1666
1667 lladdr = (struct target_sockaddr_ll *)addr;
1668 lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
1669 lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
44cf6731
MM
1670 } else if (sa_family == AF_INET6) {
1671 struct sockaddr_in6 *in6addr;
1672
1673 in6addr = (struct sockaddr_in6 *)addr;
1674 in6addr->sin6_scope_id = tswap32(in6addr->sin6_scope_id);
33a29b51 1675 }
53a5960a 1676 unlock_user(target_saddr, target_addr, 0);
579a97f7
FB
1677
1678 return 0;
7854b056
FB
1679}
1680
579a97f7
FB
1681static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1682 struct sockaddr *addr,
1683 socklen_t len)
7854b056 1684{
53a5960a
PB
1685 struct target_sockaddr *target_saddr;
1686
a1e22192
PM
1687 if (len == 0) {
1688 return 0;
1689 }
6860710c 1690 assert(addr);
a1e22192 1691
579a97f7
FB
1692 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1693 if (!target_saddr)
1694 return -TARGET_EFAULT;
53a5960a 1695 memcpy(target_saddr, addr, len);
a1e22192
PM
1696 if (len >= offsetof(struct target_sockaddr, sa_family) +
1697 sizeof(target_saddr->sa_family)) {
1698 target_saddr->sa_family = tswap16(addr->sa_family);
1699 }
a47401bc
PMD
1700 if (addr->sa_family == AF_NETLINK &&
1701 len >= sizeof(struct target_sockaddr_nl)) {
1702 struct target_sockaddr_nl *target_nl =
1703 (struct target_sockaddr_nl *)target_saddr;
6c5b5645
LV
1704 target_nl->nl_pid = tswap32(target_nl->nl_pid);
1705 target_nl->nl_groups = tswap32(target_nl->nl_groups);
a82ea939
LV
1706 } else if (addr->sa_family == AF_PACKET) {
1707 struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
1708 target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
1709 target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
ee1ac3a1
HD
1710 } else if (addr->sa_family == AF_INET6 &&
1711 len >= sizeof(struct target_sockaddr_in6)) {
1712 struct target_sockaddr_in6 *target_in6 =
1713 (struct target_sockaddr_in6 *)target_saddr;
1714 target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id);
6c5b5645 1715 }
53a5960a 1716 unlock_user(target_saddr, target_addr, len);
579a97f7
FB
1717
1718 return 0;
7854b056
FB
1719}
1720
5a4a898d
FB
1721static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1722 struct target_msghdr *target_msgh)
7854b056
FB
1723{
1724 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
5a4a898d
FB
1725 abi_long msg_controllen;
1726 abi_ulong target_cmsg_addr;
ee104587 1727 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
7854b056 1728 socklen_t space = 0;
5a4a898d 1729
cbb21eed 1730 msg_controllen = tswapal(target_msgh->msg_controllen);
5a4a898d
FB
1731 if (msg_controllen < sizeof (struct target_cmsghdr))
1732 goto the_end;
cbb21eed 1733 target_cmsg_addr = tswapal(target_msgh->msg_control);
5a4a898d 1734 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
ee104587 1735 target_cmsg_start = target_cmsg;
5a4a898d
FB
1736 if (!target_cmsg)
1737 return -TARGET_EFAULT;
7854b056
FB
1738
1739 while (cmsg && target_cmsg) {
1740 void *data = CMSG_DATA(cmsg);
1741 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1742
cbb21eed 1743 int len = tswapal(target_cmsg->cmsg_len)
ad762b99 1744 - sizeof(struct target_cmsghdr);
7854b056
FB
1745
1746 space += CMSG_SPACE(len);
1747 if (space > msgh->msg_controllen) {
1748 space -= CMSG_SPACE(len);
c2aeb258
PM
1749 /* This is a QEMU bug, since we allocated the payload
1750 * area ourselves (unlike overflow in host-to-target
1751 * conversion, which is just the guest giving us a buffer
1752 * that's too small). It can't happen for the payload types
1753 * we currently support; if it becomes an issue in future
1754 * we would need to improve our allocation strategy to
1755 * something more intelligent than "twice the size of the
1756 * target buffer we're reading from".
1757 */
39be5350
JK
1758 qemu_log_mask(LOG_UNIMP,
1759 ("Unsupported ancillary data %d/%d: "
1760 "unhandled msg size\n"),
1761 tswap32(target_cmsg->cmsg_level),
1762 tswap32(target_cmsg->cmsg_type));
7854b056
FB
1763 break;
1764 }
1765
dbf4f796
PJ
1766 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1767 cmsg->cmsg_level = SOL_SOCKET;
1768 } else {
1769 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1770 }
7854b056
FB
1771 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1772 cmsg->cmsg_len = CMSG_LEN(len);
1773
30b8b68e 1774 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
7854b056
FB
1775 int *fd = (int *)data;
1776 int *target_fd = (int *)target_data;
1777 int i, numfds = len / sizeof(int);
1778
876e23cb
PM
1779 for (i = 0; i < numfds; i++) {
1780 __get_user(fd[i], target_fd + i);
1781 }
30b8b68e
AS
1782 } else if (cmsg->cmsg_level == SOL_SOCKET
1783 && cmsg->cmsg_type == SCM_CREDENTIALS) {
1784 struct ucred *cred = (struct ucred *)data;
1785 struct target_ucred *target_cred =
1786 (struct target_ucred *)target_data;
1787
876e23cb
PM
1788 __get_user(cred->pid, &target_cred->pid);
1789 __get_user(cred->uid, &target_cred->uid);
1790 __get_user(cred->gid, &target_cred->gid);
27404b6c
HD
1791 } else if (cmsg->cmsg_level == SOL_ALG) {
1792 uint32_t *dst = (uint32_t *)data;
1793
1794 memcpy(dst, target_data, len);
669dcb60 1795 /* fix endianness of first 32-bit word */
27404b6c
HD
1796 if (len >= sizeof(uint32_t)) {
1797 *dst = tswap32(*dst);
1798 }
30b8b68e 1799 } else {
39be5350
JK
1800 qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
1801 cmsg->cmsg_level, cmsg->cmsg_type);
30b8b68e 1802 memcpy(data, target_data, len);
7854b056
FB
1803 }
1804
1805 cmsg = CMSG_NXTHDR(msgh, cmsg);
ee104587
JN
1806 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1807 target_cmsg_start);
7854b056 1808 }
5a4a898d
FB
1809 unlock_user(target_cmsg, target_cmsg_addr, 0);
1810 the_end:
7854b056 1811 msgh->msg_controllen = space;
5a4a898d 1812 return 0;
7854b056
FB
1813}
1814
5a4a898d
FB
1815static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1816 struct msghdr *msgh)
7854b056
FB
1817{
1818 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
5a4a898d
FB
1819 abi_long msg_controllen;
1820 abi_ulong target_cmsg_addr;
ee104587 1821 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
7854b056
FB
1822 socklen_t space = 0;
1823
cbb21eed 1824 msg_controllen = tswapal(target_msgh->msg_controllen);
5a4a898d
FB
1825 if (msg_controllen < sizeof (struct target_cmsghdr))
1826 goto the_end;
cbb21eed 1827 target_cmsg_addr = tswapal(target_msgh->msg_control);
5a4a898d 1828 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
ee104587 1829 target_cmsg_start = target_cmsg;
5a4a898d
FB
1830 if (!target_cmsg)
1831 return -TARGET_EFAULT;
1832
7854b056
FB
1833 while (cmsg && target_cmsg) {
1834 void *data = CMSG_DATA(cmsg);
1835 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1836
ad762b99 1837 int len = cmsg->cmsg_len - sizeof(struct cmsghdr);
c2aeb258 1838 int tgt_len, tgt_space;
7854b056 1839
c2aeb258
PM
1840 /* We never copy a half-header but may copy half-data;
1841 * this is Linux's behaviour in put_cmsg(). Note that
1842 * truncation here is a guest problem (which we report
1843 * to the guest via the CTRUNC bit), unlike truncation
1844 * in target_to_host_cmsg, which is a QEMU bug.
1845 */
7174970a 1846 if (msg_controllen < sizeof(struct target_cmsghdr)) {
c2aeb258 1847 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
7854b056
FB
1848 break;
1849 }
1850
dbf4f796
PJ
1851 if (cmsg->cmsg_level == SOL_SOCKET) {
1852 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1853 } else {
1854 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1855 }
7854b056 1856 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
7854b056 1857
c2aeb258
PM
1858 /* Payload types which need a different size of payload on
1859 * the target must adjust tgt_len here.
1860 */
309786cf 1861 tgt_len = len;
c2aeb258
PM
1862 switch (cmsg->cmsg_level) {
1863 case SOL_SOCKET:
1864 switch (cmsg->cmsg_type) {
1865 case SO_TIMESTAMP:
1866 tgt_len = sizeof(struct target_timeval);
1867 break;
1868 default:
1869 break;
1870 }
309786cf 1871 break;
c2aeb258
PM
1872 default:
1873 break;
1874 }
1875
7174970a 1876 if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) {
c2aeb258 1877 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
7174970a 1878 tgt_len = msg_controllen - sizeof(struct target_cmsghdr);
c2aeb258
PM
1879 }
1880
1881 /* We must now copy-and-convert len bytes of payload
1882 * into tgt_len bytes of destination space. Bear in mind
1883 * that in both source and destination we may be dealing
1884 * with a truncated value!
1885 */
52b65494
HD
1886 switch (cmsg->cmsg_level) {
1887 case SOL_SOCKET:
1888 switch (cmsg->cmsg_type) {
1889 case SCM_RIGHTS:
1890 {
1891 int *fd = (int *)data;
1892 int *target_fd = (int *)target_data;
c2aeb258 1893 int i, numfds = tgt_len / sizeof(int);
52b65494 1894
876e23cb
PM
1895 for (i = 0; i < numfds; i++) {
1896 __put_user(fd[i], target_fd + i);
1897 }
52b65494
HD
1898 break;
1899 }
1900 case SO_TIMESTAMP:
1901 {
1902 struct timeval *tv = (struct timeval *)data;
1903 struct target_timeval *target_tv =
1904 (struct target_timeval *)target_data;
1905
c2aeb258
PM
1906 if (len != sizeof(struct timeval) ||
1907 tgt_len != sizeof(struct target_timeval)) {
52b65494 1908 goto unimplemented;
c2aeb258 1909 }
52b65494
HD
1910
1911 /* copy struct timeval to target */
876e23cb
PM
1912 __put_user(tv->tv_sec, &target_tv->tv_sec);
1913 __put_user(tv->tv_usec, &target_tv->tv_usec);
52b65494
HD
1914 break;
1915 }
4bc29756
HD
1916 case SCM_CREDENTIALS:
1917 {
1918 struct ucred *cred = (struct ucred *)data;
1919 struct target_ucred *target_cred =
1920 (struct target_ucred *)target_data;
1921
1922 __put_user(cred->pid, &target_cred->pid);
1923 __put_user(cred->uid, &target_cred->uid);
1924 __put_user(cred->gid, &target_cred->gid);
1925 break;
1926 }
52b65494
HD
1927 default:
1928 goto unimplemented;
1929 }
1930 break;
7854b056 1931
ee1ac3a1
HD
1932 case SOL_IP:
1933 switch (cmsg->cmsg_type) {
1934 case IP_TTL:
1935 {
1936 uint32_t *v = (uint32_t *)data;
1937 uint32_t *t_int = (uint32_t *)target_data;
1938
7174970a
PM
1939 if (len != sizeof(uint32_t) ||
1940 tgt_len != sizeof(uint32_t)) {
1941 goto unimplemented;
1942 }
ee1ac3a1
HD
1943 __put_user(*v, t_int);
1944 break;
1945 }
1946 case IP_RECVERR:
1947 {
1948 struct errhdr_t {
1949 struct sock_extended_err ee;
1950 struct sockaddr_in offender;
1951 };
1952 struct errhdr_t *errh = (struct errhdr_t *)data;
1953 struct errhdr_t *target_errh =
1954 (struct errhdr_t *)target_data;
1955
7174970a
PM
1956 if (len != sizeof(struct errhdr_t) ||
1957 tgt_len != sizeof(struct errhdr_t)) {
1958 goto unimplemented;
1959 }
ee1ac3a1
HD
1960 __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
1961 __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
1962 __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
1963 __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
1964 __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
1965 __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
1966 __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
1967 host_to_target_sockaddr((unsigned long) &target_errh->offender,
1968 (void *) &errh->offender, sizeof(errh->offender));
1969 break;
1970 }
1971 default:
1972 goto unimplemented;
1973 }
1974 break;
1975
1976 case SOL_IPV6:
1977 switch (cmsg->cmsg_type) {
1978 case IPV6_HOPLIMIT:
1979 {
1980 uint32_t *v = (uint32_t *)data;
1981 uint32_t *t_int = (uint32_t *)target_data;
1982
7174970a
PM
1983 if (len != sizeof(uint32_t) ||
1984 tgt_len != sizeof(uint32_t)) {
1985 goto unimplemented;
1986 }
ee1ac3a1
HD
1987 __put_user(*v, t_int);
1988 break;
1989 }
1990 case IPV6_RECVERR:
1991 {
1992 struct errhdr6_t {
1993 struct sock_extended_err ee;
1994 struct sockaddr_in6 offender;
1995 };
1996 struct errhdr6_t *errh = (struct errhdr6_t *)data;
1997 struct errhdr6_t *target_errh =
1998 (struct errhdr6_t *)target_data;
1999
7174970a
PM
2000 if (len != sizeof(struct errhdr6_t) ||
2001 tgt_len != sizeof(struct errhdr6_t)) {
2002 goto unimplemented;
2003 }
ee1ac3a1
HD
2004 __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
2005 __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
2006 __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
2007 __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
2008 __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
2009 __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
2010 __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
2011 host_to_target_sockaddr((unsigned long) &target_errh->offender,
2012 (void *) &errh->offender, sizeof(errh->offender));
2013 break;
2014 }
2015 default:
2016 goto unimplemented;
2017 }
2018 break;
2019
52b65494
HD
2020 default:
2021 unimplemented:
39be5350
JK
2022 qemu_log_mask(LOG_UNIMP, "Unsupported ancillary data: %d/%d\n",
2023 cmsg->cmsg_level, cmsg->cmsg_type);
c2aeb258
PM
2024 memcpy(target_data, data, MIN(len, tgt_len));
2025 if (tgt_len > len) {
2026 memset(target_data + len, 0, tgt_len - len);
2027 }
7854b056
FB
2028 }
2029
7174970a
PM
2030 target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len));
2031 tgt_space = TARGET_CMSG_SPACE(tgt_len);
c2aeb258
PM
2032 if (msg_controllen < tgt_space) {
2033 tgt_space = msg_controllen;
2034 }
2035 msg_controllen -= tgt_space;
2036 space += tgt_space;
7854b056 2037 cmsg = CMSG_NXTHDR(msgh, cmsg);
ee104587
JN
2038 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
2039 target_cmsg_start);
7854b056 2040 }
5a4a898d
FB
2041 unlock_user(target_cmsg, target_cmsg_addr, space);
2042 the_end:
cbb21eed 2043 target_msgh->msg_controllen = tswapal(space);
5a4a898d 2044 return 0;
7854b056
FB
2045}
2046
0da46a6e 2047/* do_setsockopt() Must return target values and target errnos. */
992f48a0 2048static abi_long do_setsockopt(int sockfd, int level, int optname,
2f619698 2049 abi_ulong optval_addr, socklen_t optlen)
7854b056 2050{
992f48a0 2051 abi_long ret;
32407103 2052 int val;
3b46e624 2053
8853f86e
FB
2054 switch(level) {
2055 case SOL_TCP:
fe51b0a5
SCW
2056 case SOL_UDP:
2057 /* TCP and UDP options all take an 'int' value. */
7854b056 2058 if (optlen < sizeof(uint32_t))
0da46a6e 2059 return -TARGET_EINVAL;
3b46e624 2060
2f619698
FB
2061 if (get_user_u32(val, optval_addr))
2062 return -TARGET_EFAULT;
8853f86e
FB
2063 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2064 break;
2065 case SOL_IP:
2066 switch(optname) {
2efbe911
FB
2067 case IP_TOS:
2068 case IP_TTL:
8853f86e 2069 case IP_HDRINCL:
2efbe911
FB
2070 case IP_ROUTER_ALERT:
2071 case IP_RECVOPTS:
2072 case IP_RETOPTS:
2073 case IP_PKTINFO:
2074 case IP_MTU_DISCOVER:
2075 case IP_RECVERR:
ee1ac3a1 2076 case IP_RECVTTL:
2efbe911
FB
2077 case IP_RECVTOS:
2078#ifdef IP_FREEBIND
2079 case IP_FREEBIND:
2080#endif
2081 case IP_MULTICAST_TTL:
2082 case IP_MULTICAST_LOOP:
8853f86e
FB
2083 val = 0;
2084 if (optlen >= sizeof(uint32_t)) {
2f619698
FB
2085 if (get_user_u32(val, optval_addr))
2086 return -TARGET_EFAULT;
8853f86e 2087 } else if (optlen >= 1) {
2f619698
FB
2088 if (get_user_u8(val, optval_addr))
2089 return -TARGET_EFAULT;
8853f86e
FB
2090 }
2091 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2092 break;
b975b83b
LL
2093 case IP_ADD_MEMBERSHIP:
2094 case IP_DROP_MEMBERSHIP:
124a1341
MT
2095 {
2096 struct ip_mreqn ip_mreq;
2097 struct target_ip_mreqn *target_smreqn;
2098
2099 QEMU_BUILD_BUG_ON(sizeof(struct ip_mreq) !=
2100 sizeof(struct target_ip_mreq));
2101
b975b83b 2102 if (optlen < sizeof (struct target_ip_mreq) ||
124a1341 2103 optlen > sizeof (struct target_ip_mreqn)) {
b975b83b 2104 return -TARGET_EINVAL;
124a1341 2105 }
b975b83b 2106
124a1341
MT
2107 target_smreqn = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2108 if (!target_smreqn) {
2109 return -TARGET_EFAULT;
2110 }
2111 ip_mreq.imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
2112 ip_mreq.imr_address.s_addr = target_smreqn->imr_address.s_addr;
2113 if (optlen == sizeof(struct target_ip_mreqn)) {
2114 ip_mreq.imr_ifindex = tswapal(target_smreqn->imr_ifindex);
2115 optlen = sizeof(struct ip_mreqn);
2116 }
2117 unlock_user(target_smreqn, optval_addr, 0);
b975b83b 2118
124a1341
MT
2119 ret = get_errno(setsockopt(sockfd, level, optname, &ip_mreq, optlen));
2120 break;
2121 }
6e3cb58f
LL
2122 case IP_BLOCK_SOURCE:
2123 case IP_UNBLOCK_SOURCE:
2124 case IP_ADD_SOURCE_MEMBERSHIP:
2125 case IP_DROP_SOURCE_MEMBERSHIP:
166bd92e
MT
2126 {
2127 struct ip_mreq_source *ip_mreq_source;
2128
6e3cb58f
LL
2129 if (optlen != sizeof (struct target_ip_mreq_source))
2130 return -TARGET_EINVAL;
2131
2132 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
74e43b04
PM
2133 if (!ip_mreq_source) {
2134 return -TARGET_EFAULT;
2135 }
6e3cb58f
LL
2136 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
2137 unlock_user (ip_mreq_source, optval_addr, 0);
2138 break;
166bd92e 2139 }
920394db
JH
2140 default:
2141 goto unimplemented;
2142 }
2143 break;
0d78b3b5
LV
2144 case SOL_IPV6:
2145 switch (optname) {
2146 case IPV6_MTU_DISCOVER:
2147 case IPV6_MTU:
2148 case IPV6_V6ONLY:
2149 case IPV6_RECVPKTINFO:
ee1ac3a1 2150 case IPV6_UNICAST_HOPS:
21749c4c
LV
2151 case IPV6_MULTICAST_HOPS:
2152 case IPV6_MULTICAST_LOOP:
ee1ac3a1
HD
2153 case IPV6_RECVERR:
2154 case IPV6_RECVHOPLIMIT:
2155 case IPV6_2292HOPLIMIT:
2156 case IPV6_CHECKSUM:
b9cce6d7
HD
2157 case IPV6_ADDRFORM:
2158 case IPV6_2292PKTINFO:
2159 case IPV6_RECVTCLASS:
2160 case IPV6_RECVRTHDR:
2161 case IPV6_2292RTHDR:
2162 case IPV6_RECVHOPOPTS:
2163 case IPV6_2292HOPOPTS:
2164 case IPV6_RECVDSTOPTS:
2165 case IPV6_2292DSTOPTS:
2166 case IPV6_TCLASS:
22db1213 2167 case IPV6_ADDR_PREFERENCES:
b9cce6d7
HD
2168#ifdef IPV6_RECVPATHMTU
2169 case IPV6_RECVPATHMTU:
2170#endif
2171#ifdef IPV6_TRANSPARENT
2172 case IPV6_TRANSPARENT:
2173#endif
2174#ifdef IPV6_FREEBIND
2175 case IPV6_FREEBIND:
2176#endif
2177#ifdef IPV6_RECVORIGDSTADDR
2178 case IPV6_RECVORIGDSTADDR:
2179#endif
0d78b3b5
LV
2180 val = 0;
2181 if (optlen < sizeof(uint32_t)) {
2182 return -TARGET_EINVAL;
2183 }
2184 if (get_user_u32(val, optval_addr)) {
2185 return -TARGET_EFAULT;
2186 }
2187 ret = get_errno(setsockopt(sockfd, level, optname,
2188 &val, sizeof(val)));
2189 break;
ee1ac3a1
HD
2190 case IPV6_PKTINFO:
2191 {
2192 struct in6_pktinfo pki;
2193
2194 if (optlen < sizeof(pki)) {
2195 return -TARGET_EINVAL;
2196 }
2197
2198 if (copy_from_user(&pki, optval_addr, sizeof(pki))) {
2199 return -TARGET_EFAULT;
2200 }
2201
2202 pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex);
2203
2204 ret = get_errno(setsockopt(sockfd, level, optname,
2205 &pki, sizeof(pki)));
2206 break;
2207 }
22bf4ee9
NC
2208 case IPV6_ADD_MEMBERSHIP:
2209 case IPV6_DROP_MEMBERSHIP:
2210 {
2211 struct ipv6_mreq ipv6mreq;
2212
2213 if (optlen < sizeof(ipv6mreq)) {
2214 return -TARGET_EINVAL;
2215 }
2216
2217 if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) {
2218 return -TARGET_EFAULT;
2219 }
2220
2221 ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
2222
2223 ret = get_errno(setsockopt(sockfd, level, optname,
2224 &ipv6mreq, sizeof(ipv6mreq)));
2225 break;
2226 }
ee1ac3a1
HD
2227 default:
2228 goto unimplemented;
2229 }
2230 break;
2231 case SOL_ICMPV6:
2232 switch (optname) {
2233 case ICMPV6_FILTER:
2234 {
2235 struct icmp6_filter icmp6f;
2236
2237 if (optlen > sizeof(icmp6f)) {
2238 optlen = sizeof(icmp6f);
2239 }
2240
2241 if (copy_from_user(&icmp6f, optval_addr, optlen)) {
2242 return -TARGET_EFAULT;
2243 }
2244
2245 for (val = 0; val < 8; val++) {
2246 icmp6f.data[val] = tswap32(icmp6f.data[val]);
2247 }
2248
2249 ret = get_errno(setsockopt(sockfd, level, optname,
2250 &icmp6f, optlen));
2251 break;
2252 }
0d78b3b5
LV
2253 default:
2254 goto unimplemented;
2255 }
2256 break;
920394db
JH
2257 case SOL_RAW:
2258 switch (optname) {
2259 case ICMP_FILTER:
ee1ac3a1
HD
2260 case IPV6_CHECKSUM:
2261 /* those take an u32 value */
920394db
JH
2262 if (optlen < sizeof(uint32_t)) {
2263 return -TARGET_EINVAL;
2264 }
2265
2266 if (get_user_u32(val, optval_addr)) {
2267 return -TARGET_EFAULT;
2268 }
2269 ret = get_errno(setsockopt(sockfd, level, optname,
2270 &val, sizeof(val)));
2271 break;
2272
8853f86e
FB
2273 default:
2274 goto unimplemented;
2275 }
2276 break;
f31dddd2
YS
2277#if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
2278 case SOL_ALG:
2279 switch (optname) {
2280 case ALG_SET_KEY:
2281 {
04f6fb89 2282 char *alg_key = lock_user(VERIFY_READ, optval_addr, optlen, 1);
f31dddd2 2283 if (!alg_key) {
f31dddd2
YS
2284 return -TARGET_EFAULT;
2285 }
2286 ret = get_errno(setsockopt(sockfd, level, optname,
2287 alg_key, optlen));
04f6fb89 2288 unlock_user(alg_key, optval_addr, optlen);
f31dddd2
YS
2289 break;
2290 }
2291 case ALG_SET_AEAD_AUTHSIZE:
2292 {
2293 ret = get_errno(setsockopt(sockfd, level, optname,
2294 NULL, optlen));
2295 break;
2296 }
2297 default:
2298 goto unimplemented;
2299 }
2300 break;
2301#endif
3532fa74 2302 case TARGET_SOL_SOCKET:
8853f86e 2303 switch (optname) {
1b09aeb9 2304 case TARGET_SO_RCVTIMEO:
88a722b6 2305 case TARGET_SO_SNDTIMEO:
1b09aeb9
LV
2306 {
2307 struct timeval tv;
2308
1b09aeb9
LV
2309 if (optlen != sizeof(struct target_timeval)) {
2310 return -TARGET_EINVAL;
2311 }
2312
2313 if (copy_from_user_timeval(&tv, optval_addr)) {
2314 return -TARGET_EFAULT;
2315 }
2316
88a722b6
MT
2317 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
2318 optname == TARGET_SO_RCVTIMEO ?
2319 SO_RCVTIMEO : SO_SNDTIMEO,
1b09aeb9
LV
2320 &tv, sizeof(tv)));
2321 return ret;
2322 }
f57d4192
LV
2323 case TARGET_SO_ATTACH_FILTER:
2324 {
2325 struct target_sock_fprog *tfprog;
2326 struct target_sock_filter *tfilter;
2327 struct sock_fprog fprog;
2328 struct sock_filter *filter;
2329 int i;
2330
2331 if (optlen != sizeof(*tfprog)) {
2332 return -TARGET_EINVAL;
2333 }
2334 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
2335 return -TARGET_EFAULT;
2336 }
2337 if (!lock_user_struct(VERIFY_READ, tfilter,
2338 tswapal(tfprog->filter), 0)) {
2339 unlock_user_struct(tfprog, optval_addr, 1);
2340 return -TARGET_EFAULT;
2341 }
2342
2343 fprog.len = tswap16(tfprog->len);
0e173b24 2344 filter = g_try_new(struct sock_filter, fprog.len);
f57d4192
LV
2345 if (filter == NULL) {
2346 unlock_user_struct(tfilter, tfprog->filter, 1);
2347 unlock_user_struct(tfprog, optval_addr, 1);
2348 return -TARGET_ENOMEM;
2349 }
2350 for (i = 0; i < fprog.len; i++) {
2351 filter[i].code = tswap16(tfilter[i].code);
2352 filter[i].jt = tfilter[i].jt;
2353 filter[i].jf = tfilter[i].jf;
2354 filter[i].k = tswap32(tfilter[i].k);
2355 }
2356 fprog.filter = filter;
2357
2358 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
2359 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
0e173b24 2360 g_free(filter);
f57d4192
LV
2361
2362 unlock_user_struct(tfilter, tfprog->filter, 1);
2363 unlock_user_struct(tfprog, optval_addr, 1);
2364 return ret;
2365 }
451aaf68
JT
2366 case TARGET_SO_BINDTODEVICE:
2367 {
2368 char *dev_ifname, *addr_ifname;
2369
2370 if (optlen > IFNAMSIZ - 1) {
2371 optlen = IFNAMSIZ - 1;
2372 }
2373 dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2374 if (!dev_ifname) {
2375 return -TARGET_EFAULT;
2376 }
2377 optname = SO_BINDTODEVICE;
2378 addr_ifname = alloca(IFNAMSIZ);
2379 memcpy(addr_ifname, dev_ifname, optlen);
2380 addr_ifname[optlen] = 0;
fad6c58a
CG
2381 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
2382 addr_ifname, optlen));
451aaf68
JT
2383 unlock_user (dev_ifname, optval_addr, 0);
2384 return ret;
2385 }
83eb6e50
CMAB
2386 case TARGET_SO_LINGER:
2387 {
2388 struct linger lg;
2389 struct target_linger *tlg;
2390
2391 if (optlen != sizeof(struct target_linger)) {
2392 return -TARGET_EINVAL;
2393 }
2394 if (!lock_user_struct(VERIFY_READ, tlg, optval_addr, 1)) {
2395 return -TARGET_EFAULT;
2396 }
2397 __get_user(lg.l_onoff, &tlg->l_onoff);
2398 __get_user(lg.l_linger, &tlg->l_linger);
2399 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, SO_LINGER,
2400 &lg, sizeof(lg)));
2401 unlock_user_struct(tlg, optval_addr, 0);
2402 return ret;
2403 }
8853f86e 2404 /* Options with 'int' argument. */
3532fa74
FB
2405 case TARGET_SO_DEBUG:
2406 optname = SO_DEBUG;
2407 break;
2408 case TARGET_SO_REUSEADDR:
2409 optname = SO_REUSEADDR;
2410 break;
113a9dd7
YS
2411#ifdef SO_REUSEPORT
2412 case TARGET_SO_REUSEPORT:
2413 optname = SO_REUSEPORT;
2414 break;
2415#endif
3532fa74
FB
2416 case TARGET_SO_TYPE:
2417 optname = SO_TYPE;
2418 break;
2419 case TARGET_SO_ERROR:
2420 optname = SO_ERROR;
2421 break;
2422 case TARGET_SO_DONTROUTE:
2423 optname = SO_DONTROUTE;
2424 break;
2425 case TARGET_SO_BROADCAST:
2426 optname = SO_BROADCAST;
2427 break;
2428 case TARGET_SO_SNDBUF:
2429 optname = SO_SNDBUF;
2430 break;
d79b6cc4
PB
2431 case TARGET_SO_SNDBUFFORCE:
2432 optname = SO_SNDBUFFORCE;
2433 break;
3532fa74
FB
2434 case TARGET_SO_RCVBUF:
2435 optname = SO_RCVBUF;
2436 break;
d79b6cc4
PB
2437 case TARGET_SO_RCVBUFFORCE:
2438 optname = SO_RCVBUFFORCE;
2439 break;
3532fa74
FB
2440 case TARGET_SO_KEEPALIVE:
2441 optname = SO_KEEPALIVE;
2442 break;
2443 case TARGET_SO_OOBINLINE:
2444 optname = SO_OOBINLINE;
2445 break;
2446 case TARGET_SO_NO_CHECK:
2447 optname = SO_NO_CHECK;
2448 break;
2449 case TARGET_SO_PRIORITY:
2450 optname = SO_PRIORITY;
2451 break;
5e83e8e3 2452#ifdef SO_BSDCOMPAT
3532fa74
FB
2453 case TARGET_SO_BSDCOMPAT:
2454 optname = SO_BSDCOMPAT;
2455 break;
5e83e8e3 2456#endif
3532fa74
FB
2457 case TARGET_SO_PASSCRED:
2458 optname = SO_PASSCRED;
2459 break;
82d0fe6b
PB
2460 case TARGET_SO_PASSSEC:
2461 optname = SO_PASSSEC;
2462 break;
3532fa74
FB
2463 case TARGET_SO_TIMESTAMP:
2464 optname = SO_TIMESTAMP;
2465 break;
2466 case TARGET_SO_RCVLOWAT:
2467 optname = SO_RCVLOWAT;
2468 break;
8853f86e
FB
2469 default:
2470 goto unimplemented;
2471 }
3532fa74 2472 if (optlen < sizeof(uint32_t))
2f619698 2473 return -TARGET_EINVAL;
3532fa74 2474
2f619698
FB
2475 if (get_user_u32(val, optval_addr))
2476 return -TARGET_EFAULT;
3532fa74 2477 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
8853f86e 2478 break;
a2d86682
JK
2479#ifdef SOL_NETLINK
2480 case SOL_NETLINK:
2481 switch (optname) {
2482 case NETLINK_PKTINFO:
2483 case NETLINK_ADD_MEMBERSHIP:
2484 case NETLINK_DROP_MEMBERSHIP:
2485 case NETLINK_BROADCAST_ERROR:
2486 case NETLINK_NO_ENOBUFS:
2487#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2488 case NETLINK_LISTEN_ALL_NSID:
2489 case NETLINK_CAP_ACK:
2490#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2491#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2492 case NETLINK_EXT_ACK:
2493#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2494#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2495 case NETLINK_GET_STRICT_CHK:
2496#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2497 break;
2498 default:
2499 goto unimplemented;
2500 }
2501 val = 0;
2502 if (optlen < sizeof(uint32_t)) {
2503 return -TARGET_EINVAL;
2504 }
2505 if (get_user_u32(val, optval_addr)) {
2506 return -TARGET_EFAULT;
2507 }
2508 ret = get_errno(setsockopt(sockfd, SOL_NETLINK, optname, &val,
2509 sizeof(val)));
2510 break;
2511#endif /* SOL_NETLINK */
7854b056 2512 default:
8853f86e 2513 unimplemented:
39be5350
JK
2514 qemu_log_mask(LOG_UNIMP, "Unsupported setsockopt level=%d optname=%d\n",
2515 level, optname);
6fa13c17 2516 ret = -TARGET_ENOPROTOOPT;
7854b056 2517 }
8853f86e 2518 return ret;
7854b056
FB
2519}
2520
0da46a6e 2521/* do_getsockopt() Must return target values and target errnos. */
992f48a0 2522static abi_long do_getsockopt(int sockfd, int level, int optname,
2f619698 2523 abi_ulong optval_addr, abi_ulong optlen)
7854b056 2524{
992f48a0 2525 abi_long ret;
b55266b5
BS
2526 int len, val;
2527 socklen_t lv;
8853f86e
FB
2528
2529 switch(level) {
3532fa74 2530 case TARGET_SOL_SOCKET:
f3b974cd
JL
2531 level = SOL_SOCKET;
2532 switch (optname) {
2533 /* These don't just return a single integer */
f3b974cd
JL
2534 case TARGET_SO_PEERNAME:
2535 goto unimplemented;
405dc4cf
AS
2536 case TARGET_SO_RCVTIMEO: {
2537 struct timeval tv;
2538 socklen_t tvlen;
2539
2540 optname = SO_RCVTIMEO;
2541
2542get_timeout:
2543 if (get_user_u32(len, optlen)) {
2544 return -TARGET_EFAULT;
2545 }
2546 if (len < 0) {
2547 return -TARGET_EINVAL;
2548 }
2549
2550 tvlen = sizeof(tv);
2551 ret = get_errno(getsockopt(sockfd, level, optname,
2552 &tv, &tvlen));
2553 if (ret < 0) {
2554 return ret;
2555 }
2556 if (len > sizeof(struct target_timeval)) {
2557 len = sizeof(struct target_timeval);
2558 }
2559 if (copy_to_user_timeval(optval_addr, &tv)) {
2560 return -TARGET_EFAULT;
2561 }
2562 if (put_user_u32(len, optlen)) {
2563 return -TARGET_EFAULT;
2564 }
2565 break;
2566 }
2567 case TARGET_SO_SNDTIMEO:
2568 optname = SO_SNDTIMEO;
2569 goto get_timeout;
583359a6
AP
2570 case TARGET_SO_PEERCRED: {
2571 struct ucred cr;
2572 socklen_t crlen;
2573 struct target_ucred *tcr;
2574
2575 if (get_user_u32(len, optlen)) {
2576 return -TARGET_EFAULT;
2577 }
2578 if (len < 0) {
2579 return -TARGET_EINVAL;
2580 }
2581
2582 crlen = sizeof(cr);
2583 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
2584 &cr, &crlen));
2585 if (ret < 0) {
2586 return ret;
2587 }
2588 if (len > crlen) {
2589 len = crlen;
2590 }
2591 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
2592 return -TARGET_EFAULT;
2593 }
2594 __put_user(cr.pid, &tcr->pid);
2595 __put_user(cr.uid, &tcr->uid);
2596 __put_user(cr.gid, &tcr->gid);
2597 unlock_user_struct(tcr, optval_addr, 1);
2598 if (put_user_u32(len, optlen)) {
83eb6e50
CMAB
2599 return -TARGET_EFAULT;
2600 }
2601 break;
2602 }
6d485a55
LV
2603 case TARGET_SO_PEERSEC: {
2604 char *name;
2605
2606 if (get_user_u32(len, optlen)) {
2607 return -TARGET_EFAULT;
2608 }
2609 if (len < 0) {
2610 return -TARGET_EINVAL;
2611 }
2612 name = lock_user(VERIFY_WRITE, optval_addr, len, 0);
2613 if (!name) {
2614 return -TARGET_EFAULT;
2615 }
2616 lv = len;
2617 ret = get_errno(getsockopt(sockfd, level, SO_PEERSEC,
2618 name, &lv));
2619 if (put_user_u32(lv, optlen)) {
2620 ret = -TARGET_EFAULT;
2621 }
2622 unlock_user(name, optval_addr, lv);
2623 break;
2624 }
83eb6e50
CMAB
2625 case TARGET_SO_LINGER:
2626 {
2627 struct linger lg;
2628 socklen_t lglen;
2629 struct target_linger *tlg;
2630
2631 if (get_user_u32(len, optlen)) {
2632 return -TARGET_EFAULT;
2633 }
2634 if (len < 0) {
2635 return -TARGET_EINVAL;
2636 }
2637
2638 lglen = sizeof(lg);
2639 ret = get_errno(getsockopt(sockfd, level, SO_LINGER,
2640 &lg, &lglen));
2641 if (ret < 0) {
2642 return ret;
2643 }
2644 if (len > lglen) {
2645 len = lglen;
2646 }
2647 if (!lock_user_struct(VERIFY_WRITE, tlg, optval_addr, 0)) {
2648 return -TARGET_EFAULT;
2649 }
2650 __put_user(lg.l_onoff, &tlg->l_onoff);
2651 __put_user(lg.l_linger, &tlg->l_linger);
2652 unlock_user_struct(tlg, optval_addr, 1);
2653 if (put_user_u32(len, optlen)) {
583359a6
AP
2654 return -TARGET_EFAULT;
2655 }
2656 break;
2657 }
f3b974cd
JL
2658 /* Options with 'int' argument. */
2659 case TARGET_SO_DEBUG:
2660 optname = SO_DEBUG;
2661 goto int_case;
2662 case TARGET_SO_REUSEADDR:
2663 optname = SO_REUSEADDR;
2664 goto int_case;
113a9dd7
YS
2665#ifdef SO_REUSEPORT
2666 case TARGET_SO_REUSEPORT:
2667 optname = SO_REUSEPORT;
2668 goto int_case;
2669#endif
f3b974cd
JL
2670 case TARGET_SO_TYPE:
2671 optname = SO_TYPE;
2672 goto int_case;
2673 case TARGET_SO_ERROR:
2674 optname = SO_ERROR;
2675 goto int_case;
2676 case TARGET_SO_DONTROUTE:
2677 optname = SO_DONTROUTE;
2678 goto int_case;
2679 case TARGET_SO_BROADCAST:
2680 optname = SO_BROADCAST;
2681 goto int_case;
2682 case TARGET_SO_SNDBUF:
2683 optname = SO_SNDBUF;
2684 goto int_case;
2685 case TARGET_SO_RCVBUF:
2686 optname = SO_RCVBUF;
2687 goto int_case;
2688 case TARGET_SO_KEEPALIVE:
2689 optname = SO_KEEPALIVE;
2690 goto int_case;
2691 case TARGET_SO_OOBINLINE:
2692 optname = SO_OOBINLINE;
2693 goto int_case;
2694 case TARGET_SO_NO_CHECK:
2695 optname = SO_NO_CHECK;
2696 goto int_case;
2697 case TARGET_SO_PRIORITY:
2698 optname = SO_PRIORITY;
2699 goto int_case;
2700#ifdef SO_BSDCOMPAT
2701 case TARGET_SO_BSDCOMPAT:
2702 optname = SO_BSDCOMPAT;
2703 goto int_case;
2704#endif
2705 case TARGET_SO_PASSCRED:
2706 optname = SO_PASSCRED;
2707 goto int_case;
2708 case TARGET_SO_TIMESTAMP:
2709 optname = SO_TIMESTAMP;
2710 goto int_case;
2711 case TARGET_SO_RCVLOWAT:
2712 optname = SO_RCVLOWAT;
2713 goto int_case;
aec1ca41
PB
2714 case TARGET_SO_ACCEPTCONN:
2715 optname = SO_ACCEPTCONN;
2716 goto int_case;
ec63e06e
JD
2717 case TARGET_SO_PROTOCOL:
2718 optname = SO_PROTOCOL;
2719 goto int_case;
2720 case TARGET_SO_DOMAIN:
2721 optname = SO_DOMAIN;
2722 goto int_case;
8853f86e 2723 default:
2efbe911
FB
2724 goto int_case;
2725 }
2726 break;
2727 case SOL_TCP:
fe51b0a5
SCW
2728 case SOL_UDP:
2729 /* TCP and UDP options all take an 'int' value. */
2efbe911 2730 int_case:
2f619698
FB
2731 if (get_user_u32(len, optlen))
2732 return -TARGET_EFAULT;
2efbe911 2733 if (len < 0)
0da46a6e 2734 return -TARGET_EINVAL;
73160d95 2735 lv = sizeof(lv);
2efbe911
FB
2736 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2737 if (ret < 0)
2738 return ret;
cb88b7c2
HD
2739 switch (optname) {
2740 case SO_TYPE:
8289d112 2741 val = host_to_target_sock_type(val);
cb88b7c2
HD
2742 break;
2743 case SO_ERROR:
2744 val = host_to_target_errno(val);
2745 break;
8289d112 2746 }
2efbe911
FB
2747 if (len > lv)
2748 len = lv;
2f619698
FB
2749 if (len == 4) {
2750 if (put_user_u32(val, optval_addr))
2751 return -TARGET_EFAULT;
2752 } else {
2753 if (put_user_u8(val, optval_addr))
2754 return -TARGET_EFAULT;
f3b974cd 2755 }
2f619698
FB
2756 if (put_user_u32(len, optlen))
2757 return -TARGET_EFAULT;
2efbe911
FB
2758 break;
2759 case SOL_IP:
2760 switch(optname) {
2761 case IP_TOS:
2762 case IP_TTL:
2763 case IP_HDRINCL:
2764 case IP_ROUTER_ALERT:
2765 case IP_RECVOPTS:
2766 case IP_RETOPTS:
2767 case IP_PKTINFO:
2768 case IP_MTU_DISCOVER:
2769 case IP_RECVERR:
2770 case IP_RECVTOS:
2771#ifdef IP_FREEBIND
2772 case IP_FREEBIND:
2773#endif
2774 case IP_MULTICAST_TTL:
2775 case IP_MULTICAST_LOOP:
2f619698
FB
2776 if (get_user_u32(len, optlen))
2777 return -TARGET_EFAULT;
8853f86e 2778 if (len < 0)
0da46a6e 2779 return -TARGET_EINVAL;
73160d95 2780 lv = sizeof(lv);
8853f86e
FB
2781 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2782 if (ret < 0)
2783 return ret;
2efbe911 2784 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2efbe911 2785 len = 1;
2f619698
FB
2786 if (put_user_u32(len, optlen)
2787 || put_user_u8(val, optval_addr))
2788 return -TARGET_EFAULT;
2efbe911 2789 } else {
2efbe911
FB
2790 if (len > sizeof(int))
2791 len = sizeof(int);
2f619698
FB
2792 if (put_user_u32(len, optlen)
2793 || put_user_u32(val, optval_addr))
2794 return -TARGET_EFAULT;
2efbe911 2795 }
8853f86e 2796 break;
2efbe911 2797 default:
c02f499e
TS
2798 ret = -TARGET_ENOPROTOOPT;
2799 break;
8853f86e
FB
2800 }
2801 break;
bd8ed485
TD
2802 case SOL_IPV6:
2803 switch (optname) {
2804 case IPV6_MTU_DISCOVER:
2805 case IPV6_MTU:
2806 case IPV6_V6ONLY:
2807 case IPV6_RECVPKTINFO:
2808 case IPV6_UNICAST_HOPS:
2809 case IPV6_MULTICAST_HOPS:
2810 case IPV6_MULTICAST_LOOP:
2811 case IPV6_RECVERR:
2812 case IPV6_RECVHOPLIMIT:
2813 case IPV6_2292HOPLIMIT:
2814 case IPV6_CHECKSUM:
b9cce6d7
HD
2815 case IPV6_ADDRFORM:
2816 case IPV6_2292PKTINFO:
2817 case IPV6_RECVTCLASS:
2818 case IPV6_RECVRTHDR:
2819 case IPV6_2292RTHDR:
2820 case IPV6_RECVHOPOPTS:
2821 case IPV6_2292HOPOPTS:
2822 case IPV6_RECVDSTOPTS:
2823 case IPV6_2292DSTOPTS:
2824 case IPV6_TCLASS:
22db1213 2825 case IPV6_ADDR_PREFERENCES:
b9cce6d7
HD
2826#ifdef IPV6_RECVPATHMTU
2827 case IPV6_RECVPATHMTU:
2828#endif
2829#ifdef IPV6_TRANSPARENT
2830 case IPV6_TRANSPARENT:
2831#endif
2832#ifdef IPV6_FREEBIND
2833 case IPV6_FREEBIND:
2834#endif
2835#ifdef IPV6_RECVORIGDSTADDR
2836 case IPV6_RECVORIGDSTADDR:
2837#endif
bd8ed485
TD
2838 if (get_user_u32(len, optlen))
2839 return -TARGET_EFAULT;
2840 if (len < 0)
2841 return -TARGET_EINVAL;
2842 lv = sizeof(lv);
2843 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2844 if (ret < 0)
2845 return ret;
2846 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2847 len = 1;
2848 if (put_user_u32(len, optlen)
2849 || put_user_u8(val, optval_addr))
2850 return -TARGET_EFAULT;
2851 } else {
2852 if (len > sizeof(int))
2853 len = sizeof(int);
2854 if (put_user_u32(len, optlen)
2855 || put_user_u32(val, optval_addr))
2856 return -TARGET_EFAULT;
2857 }
2858 break;
2859 default:
2860 ret = -TARGET_ENOPROTOOPT;
2861 break;
2862 }
2863 break;
a2d86682
JK
2864#ifdef SOL_NETLINK
2865 case SOL_NETLINK:
2866 switch (optname) {
2867 case NETLINK_PKTINFO:
2868 case NETLINK_BROADCAST_ERROR:
2869 case NETLINK_NO_ENOBUFS:
2870#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2871 case NETLINK_LISTEN_ALL_NSID:
2872 case NETLINK_CAP_ACK:
2873#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2874#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
2875 case NETLINK_EXT_ACK:
2876#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2877#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0)
2878 case NETLINK_GET_STRICT_CHK:
2879#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) */
2880 if (get_user_u32(len, optlen)) {
2881 return -TARGET_EFAULT;
2882 }
2883 if (len != sizeof(val)) {
2884 return -TARGET_EINVAL;
2885 }
2886 lv = len;
2887 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
2888 if (ret < 0) {
2889 return ret;
2890 }
2891 if (put_user_u32(lv, optlen)
2892 || put_user_u32(val, optval_addr)) {
2893 return -TARGET_EFAULT;
2894 }
2895 break;
2896#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
2897 case NETLINK_LIST_MEMBERSHIPS:
2898 {
2899 uint32_t *results;
2900 int i;
2901 if (get_user_u32(len, optlen)) {
2902 return -TARGET_EFAULT;
2903 }
2904 if (len < 0) {
2905 return -TARGET_EINVAL;
2906 }
2907 results = lock_user(VERIFY_WRITE, optval_addr, len, 1);
13e340c8 2908 if (!results && len > 0) {
a2d86682
JK
2909 return -TARGET_EFAULT;
2910 }
2911 lv = len;
2912 ret = get_errno(getsockopt(sockfd, level, optname, results, &lv));
2913 if (ret < 0) {
2914 unlock_user(results, optval_addr, 0);
2915 return ret;
2916 }
669dcb60 2917 /* swap host endianness to target endianness. */
a2d86682
JK
2918 for (i = 0; i < (len / sizeof(uint32_t)); i++) {
2919 results[i] = tswap32(results[i]);
2920 }
2921 if (put_user_u32(lv, optlen)) {
2922 return -TARGET_EFAULT;
2923 }
2924 unlock_user(results, optval_addr, 0);
2925 break;
2926 }
2927#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) */
2928 default:
2929 goto unimplemented;
2930 }
c0cb8801 2931 break;
a2d86682 2932#endif /* SOL_NETLINK */
8853f86e
FB
2933 default:
2934 unimplemented:
39be5350
JK
2935 qemu_log_mask(LOG_UNIMP,
2936 "getsockopt level=%d optname=%d not yet supported\n",
2937 level, optname);
c02f499e 2938 ret = -TARGET_EOPNOTSUPP;
8853f86e
FB
2939 break;
2940 }
2941 return ret;
7854b056
FB
2942}
2943
9ac22517
MF
2944/* Convert target low/high pair representing file offset into the host
2945 * low/high pair. This function doesn't handle offsets bigger than 64 bits
2946 * as the kernel doesn't handle them either.
2947 */
2948static void target_to_host_low_high(abi_ulong tlow,
2949 abi_ulong thigh,
2950 unsigned long *hlow,
2951 unsigned long *hhigh)
2952{
2953 uint64_t off = tlow |
2954 ((unsigned long long)thigh << TARGET_LONG_BITS / 2) <<
2955 TARGET_LONG_BITS / 2;
2956
2957 *hlow = off;
2958 *hhigh = (off >> HOST_LONG_BITS / 2) >> HOST_LONG_BITS / 2;
2959}
2960
f287b2c2 2961static struct iovec *lock_iovec(int type, abi_ulong target_addr,
dab32b32 2962 abi_ulong count, int copy)
53a5960a
PB
2963{
2964 struct target_iovec *target_vec;
f287b2c2
RH
2965 struct iovec *vec;
2966 abi_ulong total_len, max_len;
d732dcb4 2967 int i;
501bb4b0 2968 int err = 0;
29560a6c 2969 bool bad_address = false;
53a5960a 2970
f287b2c2
RH
2971 if (count == 0) {
2972 errno = 0;
2973 return NULL;
2974 }
dab32b32 2975 if (count > IOV_MAX) {
f287b2c2
RH
2976 errno = EINVAL;
2977 return NULL;
2978 }
2979
0e173b24 2980 vec = g_try_new0(struct iovec, count);
f287b2c2
RH
2981 if (vec == NULL) {
2982 errno = ENOMEM;
2983 return NULL;
2984 }
2985
2986 target_vec = lock_user(VERIFY_READ, target_addr,
2987 count * sizeof(struct target_iovec), 1);
2988 if (target_vec == NULL) {
501bb4b0 2989 err = EFAULT;
f287b2c2
RH
2990 goto fail2;
2991 }
2992
2993 /* ??? If host page size > target page size, this will result in a
2994 value larger than what we can actually support. */
2995 max_len = 0x7fffffff & TARGET_PAGE_MASK;
2996 total_len = 0;
2997
2998 for (i = 0; i < count; i++) {
2999 abi_ulong base = tswapal(target_vec[i].iov_base);
3000 abi_long len = tswapal(target_vec[i].iov_len);
3001
3002 if (len < 0) {
501bb4b0 3003 err = EINVAL;
f287b2c2
RH
3004 goto fail;
3005 } else if (len == 0) {
3006 /* Zero length pointer is ignored. */
3007 vec[i].iov_base = 0;
41df8411 3008 } else {
f287b2c2 3009 vec[i].iov_base = lock_user(type, base, len, copy);
29560a6c
TM
3010 /* If the first buffer pointer is bad, this is a fault. But
3011 * subsequent bad buffers will result in a partial write; this
3012 * is realized by filling the vector with null pointers and
3013 * zero lengths. */
f287b2c2 3014 if (!vec[i].iov_base) {
29560a6c
TM
3015 if (i == 0) {
3016 err = EFAULT;
3017 goto fail;
3018 } else {
3019 bad_address = true;
3020 }
3021 }
3022 if (bad_address) {
3023 len = 0;
f287b2c2
RH
3024 }
3025 if (len > max_len - total_len) {
3026 len = max_len - total_len;
3027 }
41df8411 3028 }
f287b2c2
RH
3029 vec[i].iov_len = len;
3030 total_len += len;
579a97f7 3031 }
f287b2c2
RH
3032
3033 unlock_user(target_vec, target_addr, 0);
3034 return vec;
3035
3036 fail:
7eff518b
CG
3037 while (--i >= 0) {
3038 if (tswapal(target_vec[i].iov_len) > 0) {
3039 unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
3040 }
3041 }
f287b2c2 3042 unlock_user(target_vec, target_addr, 0);
501bb4b0 3043 fail2:
0e173b24 3044 g_free(vec);
501bb4b0 3045 errno = err;
f287b2c2 3046 return NULL;
53a5960a
PB
3047}
3048
f287b2c2 3049static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
dab32b32 3050 abi_ulong count, int copy)
53a5960a
PB
3051{
3052 struct target_iovec *target_vec;
53a5960a
PB
3053 int i;
3054
f287b2c2
RH
3055 target_vec = lock_user(VERIFY_READ, target_addr,
3056 count * sizeof(struct target_iovec), 1);
3057 if (target_vec) {
3058 for (i = 0; i < count; i++) {
3059 abi_ulong base = tswapal(target_vec[i].iov_base);
71ec7cef 3060 abi_long len = tswapal(target_vec[i].iov_len);
f287b2c2
RH
3061 if (len < 0) {
3062 break;
3063 }
d732dcb4
AZ
3064 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
3065 }
f287b2c2 3066 unlock_user(target_vec, target_addr, 0);
53a5960a 3067 }
579a97f7 3068
0e173b24 3069 g_free(vec);
53a5960a
PB
3070}
3071
53d09b76 3072static inline int target_to_host_sock_type(int *type)
3532fa74 3073{
f651e6ae
PJ
3074 int host_type = 0;
3075 int target_type = *type;
3076
3077 switch (target_type & TARGET_SOCK_TYPE_MASK) {
3532fa74 3078 case TARGET_SOCK_DGRAM:
f651e6ae 3079 host_type = SOCK_DGRAM;
3532fa74
FB
3080 break;
3081 case TARGET_SOCK_STREAM:
f651e6ae 3082 host_type = SOCK_STREAM;
3532fa74 3083 break;
f651e6ae
PJ
3084 default:
3085 host_type = target_type & TARGET_SOCK_TYPE_MASK;
3532fa74
FB
3086 break;
3087 }
f651e6ae 3088 if (target_type & TARGET_SOCK_CLOEXEC) {
53d09b76 3089#if defined(SOCK_CLOEXEC)
f651e6ae 3090 host_type |= SOCK_CLOEXEC;
53d09b76
EI
3091#else
3092 return -TARGET_EINVAL;
3093#endif
f651e6ae
PJ
3094 }
3095 if (target_type & TARGET_SOCK_NONBLOCK) {
53d09b76 3096#if defined(SOCK_NONBLOCK)
f651e6ae 3097 host_type |= SOCK_NONBLOCK;
53d09b76
EI
3098#elif !defined(O_NONBLOCK)
3099 return -TARGET_EINVAL;
3100#endif
f651e6ae
PJ
3101 }
3102 *type = host_type;
53d09b76
EI
3103 return 0;
3104}
3105
3106/* Try to emulate socket type flags after socket creation. */
3107static int sock_flags_fixup(int fd, int target_type)
3108{
3109#if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
3110 if (target_type & TARGET_SOCK_NONBLOCK) {
3111 int flags = fcntl(fd, F_GETFL);
3112 if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
3113 close(fd);
3114 return -TARGET_EINVAL;
3115 }
3116 }
3117#endif
3118 return fd;
f651e6ae
PJ
3119}
3120
3121/* do_socket() Must return target values and target errnos. */
3122static abi_long do_socket(int domain, int type, int protocol)
3123{
53d09b76
EI
3124 int target_type = type;
3125 int ret;
3126
3127 ret = target_to_host_sock_type(&type);
3128 if (ret) {
3129 return ret;
3130 }
f651e6ae 3131
575b22b1
LV
3132 if (domain == PF_NETLINK && !(
3133#ifdef CONFIG_RTNETLINK
3134 protocol == NETLINK_ROUTE ||
3135#endif
3136 protocol == NETLINK_KOBJECT_UEVENT ||
3137 protocol == NETLINK_AUDIT)) {
71e2443e 3138 return -TARGET_EPROTONOSUPPORT;
6c5b5645 3139 }
ff626f2d
LV
3140
3141 if (domain == AF_PACKET ||
3142 (domain == AF_INET && type == SOCK_PACKET)) {
3143 protocol = tswap16(protocol);
3144 }
3145
53d09b76
EI
3146 ret = get_errno(socket(domain, type, protocol));
3147 if (ret >= 0) {
3148 ret = sock_flags_fixup(ret, target_type);
0cf22722
LV
3149 if (type == SOCK_PACKET) {
3150 /* Manage an obsolete case :
3151 * if socket type is SOCK_PACKET, bind by name
3152 */
3153 fd_trans_register(ret, &target_packet_trans);
6c5b5645
LV
3154 } else if (domain == PF_NETLINK) {
3155 switch (protocol) {
575b22b1 3156#ifdef CONFIG_RTNETLINK
6c5b5645
LV
3157 case NETLINK_ROUTE:
3158 fd_trans_register(ret, &target_netlink_route_trans);
3159 break;
575b22b1 3160#endif
b265620b
LV
3161 case NETLINK_KOBJECT_UEVENT:
3162 /* nothing to do: messages are strings */
3163 break;
5ce9bb59
LV
3164 case NETLINK_AUDIT:
3165 fd_trans_register(ret, &target_netlink_audit_trans);
3166 break;
6c5b5645
LV
3167 default:
3168 g_assert_not_reached();
3169 }
0cf22722 3170 }
53d09b76
EI
3171 }
3172 return ret;
3532fa74
FB
3173}
3174
0da46a6e 3175/* do_bind() Must return target values and target errnos. */
992f48a0
BS
3176static abi_long do_bind(int sockfd, abi_ulong target_addr,
3177 socklen_t addrlen)
3532fa74 3178{
8f7aeaf6 3179 void *addr;
917507b0 3180 abi_long ret;
8f7aeaf6 3181
38724253 3182 if ((int)addrlen < 0) {
8f7aeaf6 3183 return -TARGET_EINVAL;
38724253 3184 }
8f7aeaf6 3185
607175e0 3186 addr = alloca(addrlen+1);
3b46e624 3187
7b36f782 3188 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
917507b0
AP
3189 if (ret)
3190 return ret;
3191
3532fa74
FB
3192 return get_errno(bind(sockfd, addr, addrlen));
3193}
3194
0da46a6e 3195/* do_connect() Must return target values and target errnos. */
992f48a0
BS
3196static abi_long do_connect(int sockfd, abi_ulong target_addr,
3197 socklen_t addrlen)
3532fa74 3198{
8f7aeaf6 3199 void *addr;
917507b0 3200 abi_long ret;
8f7aeaf6 3201
38724253 3202 if ((int)addrlen < 0) {
8f7aeaf6 3203 return -TARGET_EINVAL;
38724253 3204 }
8f7aeaf6 3205
2dd08dfd 3206 addr = alloca(addrlen+1);
3b46e624 3207
7b36f782 3208 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
917507b0
AP
3209 if (ret)
3210 return ret;
3211
2a3c7619 3212 return get_errno(safe_connect(sockfd, addr, addrlen));
3532fa74
FB
3213}
3214
f19e00d7
AG
3215/* do_sendrecvmsg_locked() Must return target values and target errnos. */
3216static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
3217 int flags, int send)
3532fa74 3218{
6de645c7 3219 abi_long ret, len;
3532fa74 3220 struct msghdr msg;
dab32b32 3221 abi_ulong count;
3532fa74 3222 struct iovec *vec;
992f48a0 3223 abi_ulong target_vec;
3532fa74 3224
3532fa74
FB
3225 if (msgp->msg_name) {
3226 msg.msg_namelen = tswap32(msgp->msg_namelen);
2dd08dfd 3227 msg.msg_name = alloca(msg.msg_namelen+1);
7b36f782
LV
3228 ret = target_to_host_sockaddr(fd, msg.msg_name,
3229 tswapal(msgp->msg_name),
3230 msg.msg_namelen);
26a6fc96
PM
3231 if (ret == -TARGET_EFAULT) {
3232 /* For connected sockets msg_name and msg_namelen must
3233 * be ignored, so returning EFAULT immediately is wrong.
3234 * Instead, pass a bad msg_name to the host kernel, and
3235 * let it decide whether to return EFAULT or not.
3236 */
3237 msg.msg_name = (void *)-1;
3238 } else if (ret) {
f287b2c2 3239 goto out2;
917507b0 3240 }
3532fa74
FB
3241 } else {
3242 msg.msg_name = NULL;
3243 msg.msg_namelen = 0;
3244 }
cbb21eed 3245 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
3532fa74 3246 msg.msg_control = alloca(msg.msg_controllen);
1d3d1b23
JS
3247 memset(msg.msg_control, 0, msg.msg_controllen);
3248
3532fa74 3249 msg.msg_flags = tswap32(msgp->msg_flags);
3b46e624 3250
cbb21eed 3251 count = tswapal(msgp->msg_iovlen);
cbb21eed 3252 target_vec = tswapal(msgp->msg_iov);
97b07970
PM
3253
3254 if (count > IOV_MAX) {
3255 /* sendrcvmsg returns a different errno for this condition than
3256 * readv/writev, so we must catch it here before lock_iovec() does.
3257 */
3258 ret = -TARGET_EMSGSIZE;
3259 goto out2;
3260 }
3261
f287b2c2
RH
3262 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
3263 target_vec, count, send);
3264 if (vec == NULL) {
3265 ret = -host_to_target_errno(errno);
3f0744f9
HD
3266 /* allow sending packet without any iov, e.g. with MSG_MORE flag */
3267 if (!send || ret) {
3268 goto out2;
3269 }
f287b2c2 3270 }
3532fa74
FB
3271 msg.msg_iovlen = count;
3272 msg.msg_iov = vec;
3b46e624 3273
3532fa74 3274 if (send) {
6c5b5645 3275 if (fd_trans_target_to_host_data(fd)) {
7d61d892
LV
3276 void *host_msg;
3277
3278 host_msg = g_malloc(msg.msg_iov->iov_len);
3279 memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len);
3280 ret = fd_trans_target_to_host_data(fd)(host_msg,
6c5b5645 3281 msg.msg_iov->iov_len);
7d61d892
LV
3282 if (ret >= 0) {
3283 msg.msg_iov->iov_base = host_msg;
3284 ret = get_errno(safe_sendmsg(fd, &msg, flags));
3285 }
3286 g_free(host_msg);
6c5b5645
LV
3287 } else {
3288 ret = target_to_host_cmsg(&msg, msgp);
7d61d892
LV
3289 if (ret == 0) {
3290 ret = get_errno(safe_sendmsg(fd, &msg, flags));
3291 }
6c5b5645 3292 }
3532fa74 3293 } else {
66687530 3294 ret = get_errno(safe_recvmsg(fd, &msg, flags));
6de645c7
AZ
3295 if (!is_error(ret)) {
3296 len = ret;
6c5b5645
LV
3297 if (fd_trans_host_to_target_data(fd)) {
3298 ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
2a03d3e6 3299 MIN(msg.msg_iov->iov_len, len));
16c81dd5
IZ
3300 }
3301 if (!is_error(ret)) {
6c5b5645
LV
3302 ret = host_to_target_cmsg(msgp, &msg);
3303 }
ca619067
JH
3304 if (!is_error(ret)) {
3305 msgp->msg_namelen = tswap32(msg.msg_namelen);
24894f39 3306 msgp->msg_flags = tswap32(msg.msg_flags);
26a6fc96 3307 if (msg.msg_name != NULL && msg.msg_name != (void *)-1) {
ca619067
JH
3308 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
3309 msg.msg_name, msg.msg_namelen);
3310 if (ret) {
3311 goto out;
3312 }
3313 }
3314
6de645c7 3315 ret = len;
ca619067 3316 }
6de645c7 3317 }
3532fa74 3318 }
ca619067
JH
3319
3320out:
3f0744f9
HD
3321 if (vec) {
3322 unlock_iovec(vec, target_vec, count, !send);
3323 }
f287b2c2 3324out2:
f19e00d7
AG
3325 return ret;
3326}
3327
3328static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
3329 int flags, int send)
3330{
3331 abi_long ret;
3332 struct target_msghdr *msgp;
3333
3334 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
3335 msgp,
3336 target_msg,
3337 send ? 1 : 0)) {
3338 return -TARGET_EFAULT;
3339 }
3340 ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
579a97f7 3341 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
3532fa74
FB
3342 return ret;
3343}
3344
f19e00d7
AG
3345/* We don't rely on the C library to have sendmmsg/recvmmsg support,
3346 * so it might not have this *mmsg-specific flag either.
3347 */
3348#ifndef MSG_WAITFORONE
3349#define MSG_WAITFORONE 0x10000
3350#endif
3351
3352static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
3353 unsigned int vlen, unsigned int flags,
3354 int send)
3355{
3356 struct target_mmsghdr *mmsgp;
3357 abi_long ret = 0;
3358 int i;
3359
3360 if (vlen > UIO_MAXIOV) {
3361 vlen = UIO_MAXIOV;
3362 }
3363
3364 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
3365 if (!mmsgp) {
3366 return -TARGET_EFAULT;
3367 }
3368
3369 for (i = 0; i < vlen; i++) {
3370 ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
3371 if (is_error(ret)) {
3372 break;
3373 }
3374 mmsgp[i].msg_len = tswap32(ret);
3375 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3376 if (flags & MSG_WAITFORONE) {
3377 flags |= MSG_DONTWAIT;
3378 }
3379 }
3380
3381 unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
3382
3383 /* Return number of datagrams sent if we sent any at all;
3384 * otherwise return the error.
3385 */
3386 if (i) {
3387 return i;
3388 }
3389 return ret;
3390}
f19e00d7 3391
a94b4987
PM
3392/* do_accept4() Must return target values and target errnos. */
3393static abi_long do_accept4(int fd, abi_ulong target_addr,
3394 abi_ulong target_addrlen_addr, int flags)
1be9e1dc 3395{
cd813367 3396 socklen_t addrlen, ret_addrlen;
2f619698 3397 void *addr;
992f48a0 3398 abi_long ret;
d25295d4
PJ
3399 int host_flags;
3400
dca4c838
HD
3401 if (flags & ~(TARGET_SOCK_CLOEXEC | TARGET_SOCK_NONBLOCK)) {
3402 return -TARGET_EINVAL;
3403 }
3404
3405 host_flags = 0;
3406 if (flags & TARGET_SOCK_NONBLOCK) {
3407 host_flags |= SOCK_NONBLOCK;
3408 }
3409 if (flags & TARGET_SOCK_CLOEXEC) {
3410 host_flags |= SOCK_CLOEXEC;
3411 }
1be9e1dc 3412
a94b4987 3413 if (target_addr == 0) {
ff6dc130 3414 return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
a94b4987 3415 }
917507b0 3416
e554eb4b 3417 /* linux returns EFAULT if addrlen pointer is invalid */
2f619698 3418 if (get_user_u32(addrlen, target_addrlen_addr))
e554eb4b 3419 return -TARGET_EFAULT;
2f619698 3420
38724253 3421 if ((int)addrlen < 0) {
8f7aeaf6 3422 return -TARGET_EINVAL;
38724253 3423 }
8f7aeaf6 3424
c7169b02 3425 if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
e554eb4b 3426 return -TARGET_EFAULT;
c7169b02 3427 }
917507b0 3428
2f619698
FB
3429 addr = alloca(addrlen);
3430
cd813367
AS
3431 ret_addrlen = addrlen;
3432 ret = get_errno(safe_accept4(fd, addr, &ret_addrlen, host_flags));
1be9e1dc 3433 if (!is_error(ret)) {
cd813367
AS
3434 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
3435 if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
2f619698 3436 ret = -TARGET_EFAULT;
cd813367 3437 }
1be9e1dc
PB
3438 }
3439 return ret;
3440}
3441
0da46a6e 3442/* do_getpeername() Must return target values and target errnos. */
992f48a0 3443static abi_long do_getpeername(int fd, abi_ulong target_addr,
2f619698 3444 abi_ulong target_addrlen_addr)
1be9e1dc 3445{
cd813367 3446 socklen_t addrlen, ret_addrlen;
2f619698 3447 void *addr;
992f48a0 3448 abi_long ret;
1be9e1dc 3449
2f619698
FB
3450 if (get_user_u32(addrlen, target_addrlen_addr))
3451 return -TARGET_EFAULT;
3452
38724253 3453 if ((int)addrlen < 0) {
8f7aeaf6 3454 return -TARGET_EINVAL;
38724253 3455 }
8f7aeaf6 3456
c7169b02 3457 if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
917507b0 3458 return -TARGET_EFAULT;
c7169b02 3459 }
917507b0 3460
2f619698
FB
3461 addr = alloca(addrlen);
3462
cd813367
AS
3463 ret_addrlen = addrlen;
3464 ret = get_errno(getpeername(fd, addr, &ret_addrlen));
1be9e1dc 3465 if (!is_error(ret)) {
cd813367
AS
3466 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
3467 if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
2f619698 3468 ret = -TARGET_EFAULT;
cd813367 3469 }
1be9e1dc
PB
3470 }
3471 return ret;
3472}
3473
0da46a6e 3474/* do_getsockname() Must return target values and target errnos. */
992f48a0 3475static abi_long do_getsockname(int fd, abi_ulong target_addr,
2f619698 3476 abi_ulong target_addrlen_addr)
1be9e1dc 3477{
cd813367 3478 socklen_t addrlen, ret_addrlen;
2f619698 3479 void *addr;
992f48a0 3480 abi_long ret;
1be9e1dc 3481
2f619698
FB
3482 if (get_user_u32(addrlen, target_addrlen_addr))
3483 return -TARGET_EFAULT;
3484
38724253 3485 if ((int)addrlen < 0) {
8f7aeaf6 3486 return -TARGET_EINVAL;
38724253 3487 }
8f7aeaf6 3488
c7169b02 3489 if (!access_ok(thread_cpu, VERIFY_WRITE, target_addr, addrlen)) {
917507b0 3490 return -TARGET_EFAULT;
c7169b02 3491 }
917507b0 3492
2f619698
FB
3493 addr = alloca(addrlen);
3494
cd813367
AS
3495 ret_addrlen = addrlen;
3496 ret = get_errno(getsockname(fd, addr, &ret_addrlen));
1be9e1dc 3497 if (!is_error(ret)) {
cd813367
AS
3498 host_to_target_sockaddr(target_addr, addr, MIN(addrlen, ret_addrlen));
3499 if (put_user_u32(ret_addrlen, target_addrlen_addr)) {
2f619698 3500 ret = -TARGET_EFAULT;
cd813367 3501 }
1be9e1dc
PB
3502 }
3503 return ret;
3504}
3505
0da46a6e 3506/* do_socketpair() Must return target values and target errnos. */
992f48a0 3507static abi_long do_socketpair(int domain, int type, int protocol,
2f619698 3508 abi_ulong target_tab_addr)
1be9e1dc
PB
3509{
3510 int tab[2];
992f48a0 3511 abi_long ret;
1be9e1dc 3512
f651e6ae
PJ
3513 target_to_host_sock_type(&type);
3514
1be9e1dc
PB
3515 ret = get_errno(socketpair(domain, type, protocol, tab));
3516 if (!is_error(ret)) {
2f619698
FB
3517 if (put_user_s32(tab[0], target_tab_addr)
3518 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
3519 ret = -TARGET_EFAULT;
1be9e1dc
PB
3520 }
3521 return ret;
3522}
3523
0da46a6e 3524/* do_sendto() Must return target values and target errnos. */
992f48a0
BS
3525static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
3526 abi_ulong target_addr, socklen_t addrlen)
1be9e1dc
PB
3527{
3528 void *addr;
3529 void *host_msg;
7d61d892 3530 void *copy_msg = NULL;
992f48a0 3531 abi_long ret;
1be9e1dc 3532
38724253 3533 if ((int)addrlen < 0) {
8f7aeaf6 3534 return -TARGET_EINVAL;
38724253 3535 }
8f7aeaf6 3536
579a97f7
FB
3537 host_msg = lock_user(VERIFY_READ, msg, len, 1);
3538 if (!host_msg)
3539 return -TARGET_EFAULT;
6c5b5645 3540 if (fd_trans_target_to_host_data(fd)) {
7d61d892
LV
3541 copy_msg = host_msg;
3542 host_msg = g_malloc(len);
3543 memcpy(host_msg, copy_msg, len);
6c5b5645
LV
3544 ret = fd_trans_target_to_host_data(fd)(host_msg, len);
3545 if (ret < 0) {
7d61d892 3546 goto fail;
6c5b5645
LV
3547 }
3548 }
1be9e1dc 3549 if (target_addr) {
2dd08dfd 3550 addr = alloca(addrlen+1);
7b36f782 3551 ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
917507b0 3552 if (ret) {
7d61d892 3553 goto fail;
917507b0 3554 }
66687530 3555 ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
1be9e1dc 3556 } else {
66687530 3557 ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
1be9e1dc 3558 }
7d61d892
LV
3559fail:
3560 if (copy_msg) {
3561 g_free(host_msg);
3562 host_msg = copy_msg;
3563 }
1be9e1dc
PB
3564 unlock_user(host_msg, msg, 0);
3565 return ret;
3566}
3567
0da46a6e 3568/* do_recvfrom() Must return target values and target errnos. */
992f48a0
BS
3569static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
3570 abi_ulong target_addr,
3571 abi_ulong target_addrlen)
1be9e1dc 3572{
cd813367 3573 socklen_t addrlen, ret_addrlen;
1be9e1dc
PB
3574 void *addr;
3575 void *host_msg;
992f48a0 3576 abi_long ret;
1be9e1dc 3577
4a1e6bce
ZR
3578 if (!msg) {
3579 host_msg = NULL;
3580 } else {
3581 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
3582 if (!host_msg) {
3583 return -TARGET_EFAULT;
3584 }
3585 }
1be9e1dc 3586 if (target_addr) {
2f619698
FB
3587 if (get_user_u32(addrlen, target_addrlen)) {
3588 ret = -TARGET_EFAULT;
3589 goto fail;
3590 }
38724253 3591 if ((int)addrlen < 0) {
8f7aeaf6
AJ
3592 ret = -TARGET_EINVAL;
3593 goto fail;
3594 }
1be9e1dc 3595 addr = alloca(addrlen);
cd813367 3596 ret_addrlen = addrlen;
66687530 3597 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
cd813367 3598 addr, &ret_addrlen));
1be9e1dc
PB
3599 } else {
3600 addr = NULL; /* To keep compiler quiet. */
cd813367 3601 addrlen = 0; /* To keep compiler quiet. */
66687530 3602 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
1be9e1dc
PB
3603 }
3604 if (!is_error(ret)) {
c35e1f9c 3605 if (fd_trans_host_to_target_data(fd)) {
2a03d3e6
LV
3606 abi_long trans;
3607 trans = fd_trans_host_to_target_data(fd)(host_msg, MIN(ret, len));
3608 if (is_error(trans)) {
3609 ret = trans;
3610 goto fail;
3611 }
c35e1f9c 3612 }
1be9e1dc 3613 if (target_addr) {
cd813367
AS
3614 host_to_target_sockaddr(target_addr, addr,
3615 MIN(addrlen, ret_addrlen));
3616 if (put_user_u32(ret_addrlen, target_addrlen)) {
2f619698
FB
3617 ret = -TARGET_EFAULT;
3618 goto fail;
3619 }
1be9e1dc
PB
3620 }
3621 unlock_user(host_msg, msg, len);
3622 } else {
2f619698 3623fail:
1be9e1dc
PB
3624 unlock_user(host_msg, msg, 0);
3625 }
3626 return ret;
3627}
3628
32407103 3629#ifdef TARGET_NR_socketcall
ff71a454 3630/* do_socketcall() must return target values and target errnos. */
992f48a0 3631static abi_long do_socketcall(int num, abi_ulong vptr)
31e31b8a 3632{
ff71a454
AM
3633 static const unsigned nargs[] = { /* number of arguments per operation */
3634 [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */
3635 [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */
3636 [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */
3637 [TARGET_SYS_LISTEN] = 2, /* fd, backlog */
3638 [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */
3639 [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */
3640 [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */
3641 [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */
3642 [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */
3643 [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */
3644 [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */
3645 [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */
3646 [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */
3647 [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
3648 [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
3649 [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */
3650 [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */
3651 [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */
3652 [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */
3653 [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */
62dc90c6
MT
3654 };
3655 abi_long a[6]; /* max 6 args */
ff71a454 3656 unsigned i;
62dc90c6 3657
ff71a454
AM
3658 /* check the range of the first argument num */
3659 /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
3660 if (num < 1 || num > TARGET_SYS_SENDMMSG) {
3661 return -TARGET_EINVAL;
3662 }
3663 /* ensure we have space for args */
3664 if (nargs[num] > ARRAY_SIZE(a)) {
3665 return -TARGET_EINVAL;
3666 }
3667 /* collect the arguments in a[] according to nargs[] */
3668 for (i = 0; i < nargs[num]; ++i) {
3669 if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
3670 return -TARGET_EFAULT;
31e31b8a 3671 }
62dc90c6 3672 }
ff71a454 3673 /* now when we have the args, invoke the appropriate underlying function */
62dc90c6 3674 switch (num) {
ff71a454 3675 case TARGET_SYS_SOCKET: /* domain, type, protocol */
62dc90c6 3676 return do_socket(a[0], a[1], a[2]);
ff71a454 3677 case TARGET_SYS_BIND: /* sockfd, addr, addrlen */
62dc90c6 3678 return do_bind(a[0], a[1], a[2]);
ff71a454 3679 case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */
62dc90c6 3680 return do_connect(a[0], a[1], a[2]);
ff71a454 3681 case TARGET_SYS_LISTEN: /* sockfd, backlog */
62dc90c6 3682 return get_errno(listen(a[0], a[1]));
ff71a454 3683 case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */
62dc90c6 3684 return do_accept4(a[0], a[1], a[2], 0);
ff71a454 3685 case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */
62dc90c6 3686 return do_getsockname(a[0], a[1], a[2]);
ff71a454 3687 case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */
62dc90c6 3688 return do_getpeername(a[0], a[1], a[2]);
ff71a454 3689 case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */
62dc90c6 3690 return do_socketpair(a[0], a[1], a[2], a[3]);
ff71a454 3691 case TARGET_SYS_SEND: /* sockfd, msg, len, flags */
62dc90c6 3692 return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
ff71a454 3693 case TARGET_SYS_RECV: /* sockfd, msg, len, flags */
62dc90c6 3694 return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
ff71a454 3695 case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */
62dc90c6 3696 return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
ff71a454 3697 case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */
62dc90c6 3698 return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
ff71a454 3699 case TARGET_SYS_SHUTDOWN: /* sockfd, how */
62dc90c6 3700 return get_errno(shutdown(a[0], a[1]));
ff71a454
AM
3701 case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */
3702 return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
3703 case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */
3704 return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
3705 case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */
62dc90c6 3706 return do_sendrecvmsg(a[0], a[1], a[2], 1);
ff71a454 3707 case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */
62dc90c6 3708 return do_sendrecvmsg(a[0], a[1], a[2], 0);
ff71a454
AM
3709 case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */
3710 return do_accept4(a[0], a[1], a[2], a[3]);
3711 case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */
5a53dc50 3712 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
ff71a454
AM
3713 case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */
3714 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
31e31b8a 3715 default:
39be5350 3716 qemu_log_mask(LOG_UNIMP, "Unsupported socketcall: %d\n", num);
ff71a454 3717 return -TARGET_EINVAL;
31e31b8a 3718 }
31e31b8a 3719}
32407103 3720#endif
31e31b8a 3721
005eb2ae
PM
3722#ifndef TARGET_SEMID64_DS
3723/* asm-generic version of this struct */
3724struct target_semid64_ds
3eb6b044
TS
3725{
3726 struct target_ipc_perm sem_perm;
992f48a0 3727 abi_ulong sem_otime;
005eb2ae 3728#if TARGET_ABI_BITS == 32
992f48a0 3729 abi_ulong __unused1;
03527344 3730#endif
992f48a0 3731 abi_ulong sem_ctime;
005eb2ae 3732#if TARGET_ABI_BITS == 32
992f48a0 3733 abi_ulong __unused2;
03527344 3734#endif
992f48a0
BS
3735 abi_ulong sem_nsems;
3736 abi_ulong __unused3;
3737 abi_ulong __unused4;
3eb6b044 3738};
005eb2ae 3739#endif
3eb6b044 3740
579a97f7
FB
3741static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
3742 abi_ulong target_addr)
3eb6b044
TS
3743{
3744 struct target_ipc_perm *target_ip;
005eb2ae 3745 struct target_semid64_ds *target_sd;
3eb6b044 3746
579a97f7
FB
3747 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3748 return -TARGET_EFAULT;
e8bbe36c 3749 target_ip = &(target_sd->sem_perm);
55a2b163
PJ
3750 host_ip->__key = tswap32(target_ip->__key);
3751 host_ip->uid = tswap32(target_ip->uid);
3752 host_ip->gid = tswap32(target_ip->gid);
3753 host_ip->cuid = tswap32(target_ip->cuid);
3754 host_ip->cgid = tswap32(target_ip->cgid);
3755#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3756 host_ip->mode = tswap32(target_ip->mode);
3757#else
cbb21eed 3758 host_ip->mode = tswap16(target_ip->mode);
55a2b163
PJ
3759#endif
3760#if defined(TARGET_PPC)
3761 host_ip->__seq = tswap32(target_ip->__seq);
3762#else
3763 host_ip->__seq = tswap16(target_ip->__seq);
3764#endif
3eb6b044 3765 unlock_user_struct(target_sd, target_addr, 0);
579a97f7 3766 return 0;
3eb6b044
TS
3767}
3768
579a97f7
FB
3769static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
3770 struct ipc_perm *host_ip)
3eb6b044
TS
3771{
3772 struct target_ipc_perm *target_ip;
005eb2ae 3773 struct target_semid64_ds *target_sd;
3eb6b044 3774
579a97f7
FB
3775 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3776 return -TARGET_EFAULT;
3eb6b044 3777 target_ip = &(target_sd->sem_perm);
55a2b163
PJ
3778 target_ip->__key = tswap32(host_ip->__key);
3779 target_ip->uid = tswap32(host_ip->uid);
3780 target_ip->gid = tswap32(host_ip->gid);
3781 target_ip->cuid = tswap32(host_ip->cuid);
3782 target_ip->cgid = tswap32(host_ip->cgid);
3783#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
3784 target_ip->mode = tswap32(host_ip->mode);
3785#else
cbb21eed 3786 target_ip->mode = tswap16(host_ip->mode);
55a2b163
PJ
3787#endif
3788#if defined(TARGET_PPC)
3789 target_ip->__seq = tswap32(host_ip->__seq);
3790#else
3791 target_ip->__seq = tswap16(host_ip->__seq);
3792#endif
3eb6b044 3793 unlock_user_struct(target_sd, target_addr, 1);
579a97f7 3794 return 0;
3eb6b044
TS
3795}
3796
579a97f7
FB
3797static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
3798 abi_ulong target_addr)
3eb6b044 3799{
005eb2ae 3800 struct target_semid64_ds *target_sd;
3eb6b044 3801
579a97f7
FB
3802 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
3803 return -TARGET_EFAULT;
e5289087
AJ
3804 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
3805 return -TARGET_EFAULT;
cbb21eed
MB
3806 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
3807 host_sd->sem_otime = tswapal(target_sd->sem_otime);
3808 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
3eb6b044 3809 unlock_user_struct(target_sd, target_addr, 0);
579a97f7 3810 return 0;
3eb6b044
TS
3811}
3812
579a97f7
FB
3813static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
3814 struct semid_ds *host_sd)
3eb6b044 3815{
005eb2ae 3816 struct target_semid64_ds *target_sd;
3eb6b044 3817
579a97f7
FB
3818 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
3819 return -TARGET_EFAULT;
e5289087 3820 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
3a93113a 3821 return -TARGET_EFAULT;
cbb21eed
MB
3822 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
3823 target_sd->sem_otime = tswapal(host_sd->sem_otime);
3824 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
3eb6b044 3825 unlock_user_struct(target_sd, target_addr, 1);
579a97f7 3826 return 0;
3eb6b044
TS
3827}
3828
e5289087
AJ
3829struct target_seminfo {
3830 int semmap;
3831 int semmni;
3832 int semmns;
3833 int semmnu;
3834 int semmsl;
3835 int semopm;
3836 int semume;
3837 int semusz;
3838 int semvmx;
3839 int semaem;
3840};
3841
3842static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
3843 struct seminfo *host_seminfo)
3844{
3845 struct target_seminfo *target_seminfo;
3846 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
3847 return -TARGET_EFAULT;
3848 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
3849 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
3850 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
3851 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
3852 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
3853 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
3854 __put_user(host_seminfo->semume, &target_seminfo->semume);
3855 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
3856 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
3857 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
3858 unlock_user_struct(target_seminfo, target_addr, 1);
3859 return 0;
3860}
3861
fa294816
TS
3862union semun {
3863 int val;
3eb6b044 3864 struct semid_ds *buf;
fa294816 3865 unsigned short *array;
e5289087 3866 struct seminfo *__buf;
fa294816
TS
3867};
3868
3eb6b044
TS
3869union target_semun {
3870 int val;
e5289087
AJ
3871 abi_ulong buf;
3872 abi_ulong array;
3873 abi_ulong __buf;
3eb6b044
TS
3874};
3875
e5289087
AJ
3876static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
3877 abi_ulong target_addr)
3eb6b044 3878{
e5289087
AJ
3879 int nsems;
3880 unsigned short *array;
3881 union semun semun;
3882 struct semid_ds semid_ds;
3883 int i, ret;
3eb6b044 3884
e5289087
AJ
3885 semun.buf = &semid_ds;
3886
3887 ret = semctl(semid, 0, IPC_STAT, semun);
3888 if (ret == -1)
3889 return get_errno(ret);
3890
3891 nsems = semid_ds.sem_nsems;
3892
0e173b24 3893 *host_array = g_try_new(unsigned short, nsems);
69d4c703
PM
3894 if (!*host_array) {
3895 return -TARGET_ENOMEM;
3896 }
e5289087
AJ
3897 array = lock_user(VERIFY_READ, target_addr,
3898 nsems*sizeof(unsigned short), 1);
69d4c703 3899 if (!array) {
0e173b24 3900 g_free(*host_array);
e5289087 3901 return -TARGET_EFAULT;
69d4c703 3902 }
e5289087
AJ
3903
3904 for(i=0; i<nsems; i++) {
3905 __get_user((*host_array)[i], &array[i]);
3eb6b044 3906 }
e5289087
AJ
3907 unlock_user(array, target_addr, 0);
3908
579a97f7 3909 return 0;
3eb6b044
TS
3910}
3911
e5289087
AJ
3912static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
3913 unsigned short **host_array)
3eb6b044 3914{
e5289087
AJ
3915 int nsems;
3916 unsigned short *array;
3917 union semun semun;
3918 struct semid_ds semid_ds;
3919 int i, ret;
3eb6b044 3920
e5289087
AJ
3921 semun.buf = &semid_ds;
3922
3923 ret = semctl(semid, 0, IPC_STAT, semun);
3924 if (ret == -1)
3925 return get_errno(ret);
3926
3927 nsems = semid_ds.sem_nsems;
3928
3929 array = lock_user(VERIFY_WRITE, target_addr,
3930 nsems*sizeof(unsigned short), 0);
3931 if (!array)
3932 return -TARGET_EFAULT;
3933
3934 for(i=0; i<nsems; i++) {
3935 __put_user((*host_array)[i], &array[i]);
3eb6b044 3936 }
0e173b24 3937 g_free(*host_array);
e5289087
AJ
3938 unlock_user(array, target_addr, 1);
3939
579a97f7 3940 return 0;
3eb6b044
TS
3941}
3942
e5289087 3943static inline abi_long do_semctl(int semid, int semnum, int cmd,
d1c002b6 3944 abi_ulong target_arg)
3eb6b044 3945{
d1c002b6 3946 union target_semun target_su = { .buf = target_arg };
3eb6b044
TS
3947 union semun arg;
3948 struct semid_ds dsarg;
7b8118e8 3949 unsigned short *array = NULL;
e5289087
AJ
3950 struct seminfo seminfo;
3951 abi_long ret = -TARGET_EINVAL;
3952 abi_long err;
3953 cmd &= 0xff;
3eb6b044
TS
3954
3955 switch( cmd ) {
3956 case GETVAL:
3eb6b044 3957 case SETVAL:
5464baec
TM
3958 /* In 64 bit cross-endian situations, we will erroneously pick up
3959 * the wrong half of the union for the "val" element. To rectify
3960 * this, the entire 8-byte structure is byteswapped, followed by
3961 * a swap of the 4 byte val field. In other cases, the data is
3962 * already in proper host byte order. */
3963 if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
3964 target_su.buf = tswapal(target_su.buf);
3965 arg.val = tswap32(target_su.val);
3966 } else {
3967 arg.val = target_su.val;
3968 }
e5289087 3969 ret = get_errno(semctl(semid, semnum, cmd, arg));
3eb6b044
TS
3970 break;
3971 case GETALL:
3eb6b044 3972 case SETALL:
e5289087
AJ
3973 err = target_to_host_semarray(semid, &array, target_su.array);
3974 if (err)
3975 return err;
3976 arg.array = array;
3977 ret = get_errno(semctl(semid, semnum, cmd, arg));
3978 err = host_to_target_semarray(semid, target_su.array, &array);
3979 if (err)
3980 return err;
3eb6b044
TS
3981 break;
3982 case IPC_STAT:
3eb6b044 3983 case IPC_SET:
e5289087
AJ
3984 case SEM_STAT:
3985 err = target_to_host_semid_ds(&dsarg, target_su.buf);
3986 if (err)
3987 return err;
3988 arg.buf = &dsarg;
3989 ret = get_errno(semctl(semid, semnum, cmd, arg));
3990 err = host_to_target_semid_ds(target_su.buf, &dsarg);
3991 if (err)
3992 return err;
3993 break;
3994 case IPC_INFO:
3995 case SEM_INFO:
3996 arg.__buf = &seminfo;
3997 ret = get_errno(semctl(semid, semnum, cmd, arg));
3998 err = host_to_target_seminfo(target_su.__buf, &seminfo);
3999 if (err)
4000 return err;
4001 break;
4002 case IPC_RMID:
4003 case GETPID:
4004 case GETNCNT:
4005 case GETZCNT:
4006 ret = get_errno(semctl(semid, semnum, cmd, NULL));
3eb6b044 4007 break;
3eb6b044
TS
4008 }
4009
4010 return ret;
4011}
4012
e5289087
AJ
4013struct target_sembuf {
4014 unsigned short sem_num;
4015 short sem_op;
4016 short sem_flg;
4017};
4018
4019static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
4020 abi_ulong target_addr,
4021 unsigned nsops)
4022{
4023 struct target_sembuf *target_sembuf;
4024 int i;
4025
4026 target_sembuf = lock_user(VERIFY_READ, target_addr,
4027 nsops*sizeof(struct target_sembuf), 1);
4028 if (!target_sembuf)
4029 return -TARGET_EFAULT;
4030
4031 for(i=0; i<nsops; i++) {
4032 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
4033 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
4034 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
4035 }
4036
4037 unlock_user(target_sembuf, target_addr, 0);
4038
4039 return 0;
4040}
4041
d8c08b1e 4042#if defined(TARGET_NR_ipc) || defined(TARGET_NR_semop) || \
cac46eb0 4043 defined(TARGET_NR_semtimedop) || defined(TARGET_NR_semtimedop_time64)
d8c08b1e
MK
4044
4045/*
4046 * This macro is required to handle the s390 variants, which passes the
4047 * arguments in a different order than default.
4048 */
4049#ifdef __s390x__
4050#define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
4051 (__nsops), (__timeout), (__sops)
4052#else
4053#define SEMTIMEDOP_IPC_ARGS(__nsops, __sops, __timeout) \
4054 (__nsops), 0, (__sops), (__timeout)
4055#endif
4056
4057static inline abi_long do_semtimedop(int semid,
4058 abi_long ptr,
4059 unsigned nsops,
cac46eb0 4060 abi_long timeout, bool time64)
e5289087 4061{
0a7ec849 4062 struct sembuf *sops;
d8c08b1e 4063 struct timespec ts, *pts = NULL;
524fa340 4064 abi_long ret;
e5289087 4065
d8c08b1e
MK
4066 if (timeout) {
4067 pts = &ts;
cac46eb0
FB
4068 if (time64) {
4069 if (target_to_host_timespec64(pts, timeout)) {
4070 return -TARGET_EFAULT;
4071 }
4072 } else {
4073 if (target_to_host_timespec(pts, timeout)) {
4074 return -TARGET_EFAULT;
4075 }
d8c08b1e
MK
4076 }
4077 }
4078
0a7ec849
FB
4079 if (nsops > TARGET_SEMOPM) {
4080 return -TARGET_E2BIG;
4081 }
4082
4083 sops = g_new(struct sembuf, nsops);
4084
4085 if (target_to_host_sembuf(sops, ptr, nsops)) {
4086 g_free(sops);
e5289087 4087 return -TARGET_EFAULT;
0a7ec849 4088 }
e5289087 4089
524fa340
LV
4090 ret = -TARGET_ENOSYS;
4091#ifdef __NR_semtimedop
d8c08b1e 4092 ret = get_errno(safe_semtimedop(semid, sops, nsops, pts));
524fa340
LV
4093#endif
4094#ifdef __NR_ipc
4095 if (ret == -TARGET_ENOSYS) {
d8c08b1e
MK
4096 ret = get_errno(safe_ipc(IPCOP_semtimedop, semid,
4097 SEMTIMEDOP_IPC_ARGS(nsops, sops, (long)pts)));
524fa340
LV
4098 }
4099#endif
0a7ec849 4100 g_free(sops);
524fa340 4101 return ret;
e5289087 4102}
d8c08b1e 4103#endif
e5289087 4104
1bc012f6
TS
4105struct target_msqid_ds
4106{
1c54ff97
AJ
4107 struct target_ipc_perm msg_perm;
4108 abi_ulong msg_stime;
4109#if TARGET_ABI_BITS == 32
4110 abi_ulong __unused1;
4111#endif
4112 abi_ulong msg_rtime;
4113#if TARGET_ABI_BITS == 32
4114 abi_ulong __unused2;
4115#endif
4116 abi_ulong msg_ctime;
4117#if TARGET_ABI_BITS == 32
4118 abi_ulong __unused3;
4119#endif
4120 abi_ulong __msg_cbytes;
4121 abi_ulong msg_qnum;
4122 abi_ulong msg_qbytes;
4123 abi_ulong msg_lspid;
4124 abi_ulong msg_lrpid;
4125 abi_ulong __unused4;
4126 abi_ulong __unused5;
1bc012f6
TS
4127};
4128
579a97f7
FB
4129static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
4130 abi_ulong target_addr)
1bc012f6
TS
4131{
4132 struct target_msqid_ds *target_md;
4133
579a97f7
FB
4134 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
4135 return -TARGET_EFAULT;
1c54ff97
AJ
4136 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
4137 return -TARGET_EFAULT;
cbb21eed
MB
4138 host_md->msg_stime = tswapal(target_md->msg_stime);
4139 host_md->msg_rtime = tswapal(target_md->msg_rtime);
4140 host_md->msg_ctime = tswapal(target_md->msg_ctime);
4141 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
4142 host_md->msg_qnum = tswapal(target_md->msg_qnum);
4143 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
4144 host_md->msg_lspid = tswapal(target_md->msg_lspid);
4145 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
1bc012f6 4146 unlock_user_struct(target_md, target_addr, 0);
579a97f7 4147 return 0;
1bc012f6
TS
4148}
4149
579a97f7
FB
4150static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
4151 struct msqid_ds *host_md)
1bc012f6
TS
4152{
4153 struct target_msqid_ds *target_md;
4154
579a97f7
FB
4155 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
4156 return -TARGET_EFAULT;
1c54ff97
AJ
4157 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
4158 return -TARGET_EFAULT;
cbb21eed
MB
4159 target_md->msg_stime = tswapal(host_md->msg_stime);
4160 target_md->msg_rtime = tswapal(host_md->msg_rtime);
4161 target_md->msg_ctime = tswapal(host_md->msg_ctime);
4162 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
4163 target_md->msg_qnum = tswapal(host_md->msg_qnum);
4164 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
4165 target_md->msg_lspid = tswapal(host_md->msg_lspid);
4166 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
1bc012f6 4167 unlock_user_struct(target_md, target_addr, 1);
579a97f7 4168 return 0;
1bc012f6
TS
4169}
4170
1c54ff97
AJ
4171struct target_msginfo {
4172 int msgpool;
4173 int msgmap;
4174 int msgmax;
4175 int msgmnb;
4176 int msgmni;
4177 int msgssz;
4178 int msgtql;
4179 unsigned short int msgseg;
4180};
4181
4182static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
4183 struct msginfo *host_msginfo)
4184{
4185 struct target_msginfo *target_msginfo;
4186 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
4187 return -TARGET_EFAULT;
4188 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
4189 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
4190 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
4191 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
4192 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
4193 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
4194 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
4195 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
4196 unlock_user_struct(target_msginfo, target_addr, 1);
00b229ac 4197 return 0;
1c54ff97
AJ
4198}
4199
4200static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
1bc012f6
TS
4201{
4202 struct msqid_ds dsarg;
1c54ff97
AJ
4203 struct msginfo msginfo;
4204 abi_long ret = -TARGET_EINVAL;
4205
4206 cmd &= 0xff;
4207
4208 switch (cmd) {
1bc012f6
TS
4209 case IPC_STAT:
4210 case IPC_SET:
1c54ff97
AJ
4211 case MSG_STAT:
4212 if (target_to_host_msqid_ds(&dsarg,ptr))
4213 return -TARGET_EFAULT;
4214 ret = get_errno(msgctl(msgid, cmd, &dsarg));
4215 if (host_to_target_msqid_ds(ptr,&dsarg))
4216 return -TARGET_EFAULT;
4217 break;
4218 case IPC_RMID:
4219 ret = get_errno(msgctl(msgid, cmd, NULL));
4220 break;
4221 case IPC_INFO:
4222 case MSG_INFO:
4223 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
4224 if (host_to_target_msginfo(ptr, &msginfo))
4225 return -TARGET_EFAULT;
4226 break;
1bc012f6 4227 }
1c54ff97 4228
1bc012f6
TS
4229 return ret;
4230}
4231
4232struct target_msgbuf {
1c54ff97
AJ
4233 abi_long mtype;
4234 char mtext[1];
1bc012f6
TS
4235};
4236
992f48a0 4237static inline abi_long do_msgsnd(int msqid, abi_long msgp,
edcc5f9d 4238 ssize_t msgsz, int msgflg)
1bc012f6
TS
4239{
4240 struct target_msgbuf *target_mb;
4241 struct msgbuf *host_mb;
992f48a0 4242 abi_long ret = 0;
1bc012f6 4243
edcc5f9d
TM
4244 if (msgsz < 0) {
4245 return -TARGET_EINVAL;
4246 }
4247
579a97f7
FB
4248 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
4249 return -TARGET_EFAULT;
0e173b24 4250 host_mb = g_try_malloc(msgsz + sizeof(long));
29e03fcb
HZ
4251 if (!host_mb) {
4252 unlock_user_struct(target_mb, msgp, 0);
4253 return -TARGET_ENOMEM;
4254 }
cbb21eed 4255 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
1c54ff97 4256 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
524fa340
LV
4257 ret = -TARGET_ENOSYS;
4258#ifdef __NR_msgsnd
89f9fe44 4259 ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
524fa340
LV
4260#endif
4261#ifdef __NR_ipc
4262 if (ret == -TARGET_ENOSYS) {
d8c08b1e
MK
4263#ifdef __s390x__
4264 ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
4265 host_mb));
4266#else
524fa340
LV
4267 ret = get_errno(safe_ipc(IPCOP_msgsnd, msqid, msgsz, msgflg,
4268 host_mb, 0));
d8c08b1e 4269#endif
524fa340
LV
4270 }
4271#endif
0e173b24 4272 g_free(host_mb);
1bc012f6
TS
4273 unlock_user_struct(target_mb, msgp, 0);
4274
4275 return ret;
4276}
4277
d8c08b1e
MK
4278#ifdef __NR_ipc
4279#if defined(__sparc__)
4280/* SPARC for msgrcv it does not use the kludge on final 2 arguments. */
4281#define MSGRCV_ARGS(__msgp, __msgtyp) __msgp, __msgtyp
4282#elif defined(__s390x__)
4283/* The s390 sys_ipc variant has only five parameters. */
4284#define MSGRCV_ARGS(__msgp, __msgtyp) \
4285 ((long int[]){(long int)__msgp, __msgtyp})
4286#else
4287#define MSGRCV_ARGS(__msgp, __msgtyp) \
4288 ((long int[]){(long int)__msgp, __msgtyp}), 0
4289#endif
4290#endif
4291
992f48a0 4292static inline abi_long do_msgrcv(int msqid, abi_long msgp,
99874f65 4293 ssize_t msgsz, abi_long msgtyp,
992f48a0 4294 int msgflg)
1bc012f6
TS
4295{
4296 struct target_msgbuf *target_mb;
579a97f7 4297 char *target_mtext;
1bc012f6 4298 struct msgbuf *host_mb;
992f48a0 4299 abi_long ret = 0;
1bc012f6 4300
99874f65
PM
4301 if (msgsz < 0) {
4302 return -TARGET_EINVAL;
4303 }
4304
579a97f7
FB
4305 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
4306 return -TARGET_EFAULT;
1c54ff97 4307
415d8471
PM
4308 host_mb = g_try_malloc(msgsz + sizeof(long));
4309 if (!host_mb) {
4310 ret = -TARGET_ENOMEM;
4311 goto end;
4312 }
524fa340
LV
4313 ret = -TARGET_ENOSYS;
4314#ifdef __NR_msgrcv
89f9fe44 4315 ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
524fa340
LV
4316#endif
4317#ifdef __NR_ipc
4318 if (ret == -TARGET_ENOSYS) {
4319 ret = get_errno(safe_ipc(IPCOP_CALL(1, IPCOP_msgrcv), msqid, msgsz,
d8c08b1e 4320 msgflg, MSGRCV_ARGS(host_mb, msgtyp)));
524fa340
LV
4321 }
4322#endif
1c54ff97 4323
579a97f7
FB
4324 if (ret > 0) {
4325 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
4326 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
4327 if (!target_mtext) {
4328 ret = -TARGET_EFAULT;
4329 goto end;
4330 }
1c54ff97 4331 memcpy(target_mb->mtext, host_mb->mtext, ret);
579a97f7
FB
4332 unlock_user(target_mtext, target_mtext_addr, ret);
4333 }
1c54ff97 4334
cbb21eed 4335 target_mb->mtype = tswapal(host_mb->mtype);
1bc012f6 4336
579a97f7
FB
4337end:
4338 if (target_mb)
4339 unlock_user_struct(target_mb, msgp, 1);
0d07fe47 4340 g_free(host_mb);
1bc012f6
TS
4341 return ret;
4342}
4343
88a8c984
RV
4344static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
4345 abi_ulong target_addr)
4346{
4347 struct target_shmid_ds *target_sd;
4348
4349 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
4350 return -TARGET_EFAULT;
4351 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
4352 return -TARGET_EFAULT;
4353 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
4354 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
4355 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
4356 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
4357 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
4358 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
4359 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
4360 unlock_user_struct(target_sd, target_addr, 0);
4361 return 0;
4362}
4363
4364static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
4365 struct shmid_ds *host_sd)
4366{
4367 struct target_shmid_ds *target_sd;
4368
4369 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
4370 return -TARGET_EFAULT;
4371 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
4372 return -TARGET_EFAULT;
4373 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
4374 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
4375 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
4376 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
4377 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
4378 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
4379 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
4380 unlock_user_struct(target_sd, target_addr, 1);
4381 return 0;
4382}
4383
4384struct target_shminfo {
4385 abi_ulong shmmax;
4386 abi_ulong shmmin;
4387 abi_ulong shmmni;
4388 abi_ulong shmseg;
4389 abi_ulong shmall;
4390};
4391
4392static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
4393 struct shminfo *host_shminfo)
4394{
4395 struct target_shminfo *target_shminfo;
4396 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
4397 return -TARGET_EFAULT;
4398 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
4399 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
4400 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
4401 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
4402 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
4403 unlock_user_struct(target_shminfo, target_addr, 1);
4404 return 0;
4405}
4406
4407struct target_shm_info {
4408 int used_ids;
4409 abi_ulong shm_tot;
4410 abi_ulong shm_rss;
4411 abi_ulong shm_swp;
4412 abi_ulong swap_attempts;
4413 abi_ulong swap_successes;
4414};
4415
4416static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
4417 struct shm_info *host_shm_info)
4418{
4419 struct target_shm_info *target_shm_info;
4420 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
4421 return -TARGET_EFAULT;
4422 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
4423 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
4424 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
4425 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
4426 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
4427 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
4428 unlock_user_struct(target_shm_info, target_addr, 1);
4429 return 0;
4430}
4431
4432static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
4433{
4434 struct shmid_ds dsarg;
4435 struct shminfo shminfo;
4436 struct shm_info shm_info;
4437 abi_long ret = -TARGET_EINVAL;
4438
4439 cmd &= 0xff;
4440
4441 switch(cmd) {
4442 case IPC_STAT:
4443 case IPC_SET:
4444 case SHM_STAT:
4445 if (target_to_host_shmid_ds(&dsarg, buf))
4446 return -TARGET_EFAULT;
4447 ret = get_errno(shmctl(shmid, cmd, &dsarg));
4448 if (host_to_target_shmid_ds(buf, &dsarg))
4449 return -TARGET_EFAULT;
4450 break;
4451 case IPC_INFO:
4452 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
4453 if (host_to_target_shminfo(buf, &shminfo))
4454 return -TARGET_EFAULT;
4455 break;
4456 case SHM_INFO:
4457 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
4458 if (host_to_target_shm_info(buf, &shm_info))
4459 return -TARGET_EFAULT;
4460 break;
4461 case IPC_RMID:
4462 case SHM_LOCK:
4463 case SHM_UNLOCK:
4464 ret = get_errno(shmctl(shmid, cmd, NULL));
4465 break;
4466 }
4467
4468 return ret;
4469}
4470
1c54ff97 4471#ifdef TARGET_NR_ipc
53a5960a 4472/* ??? This only works with linear mappings. */
0da46a6e 4473/* do_ipc() must return target values and target errnos. */
ee8e7614
PM
4474static abi_long do_ipc(CPUArchState *cpu_env,
4475 unsigned int call, abi_long first,
37ed0956 4476 abi_long second, abi_long third,
992f48a0 4477 abi_long ptr, abi_long fifth)
8853f86e
FB
4478{
4479 int version;
992f48a0 4480 abi_long ret = 0;
8853f86e
FB
4481
4482 version = call >> 16;
4483 call &= 0xffff;
4484
4485 switch (call) {
fa294816 4486 case IPCOP_semop:
cac46eb0 4487 ret = do_semtimedop(first, ptr, second, 0, false);
d8c08b1e
MK
4488 break;
4489 case IPCOP_semtimedop:
4490 /*
4491 * The s390 sys_ipc variant has only five parameters instead of six
4492 * (as for default variant) and the only difference is the handling of
4493 * SEMTIMEDOP where on s390 the third parameter is used as a pointer
4494 * to a struct timespec where the generic variant uses fifth parameter.
4495 */
4496#if defined(TARGET_S390X)
cac46eb0 4497 ret = do_semtimedop(first, ptr, second, third, TARGET_ABI_BITS == 64);
d8c08b1e 4498#else
cac46eb0 4499 ret = do_semtimedop(first, ptr, second, fifth, TARGET_ABI_BITS == 64);
d8c08b1e 4500#endif
fa294816
TS
4501 break;
4502
4503 case IPCOP_semget:
4504 ret = get_errno(semget(first, second, third));
4505 break;
4506
5d2fa8eb
TM
4507 case IPCOP_semctl: {
4508 /* The semun argument to semctl is passed by value, so dereference the
4509 * ptr argument. */
4510 abi_ulong atptr;
37ed0956 4511 get_user_ual(atptr, ptr);
d1c002b6 4512 ret = do_semctl(first, second, third, atptr);
fa294816 4513 break;
5d2fa8eb 4514 }
d96372ef 4515
1c54ff97
AJ
4516 case IPCOP_msgget:
4517 ret = get_errno(msgget(first, second));
4518 break;
d96372ef 4519
1c54ff97
AJ
4520 case IPCOP_msgsnd:
4521 ret = do_msgsnd(first, ptr, second, third);
4522 break;
d96372ef 4523
1c54ff97
AJ
4524 case IPCOP_msgctl:
4525 ret = do_msgctl(first, second, ptr);
4526 break;
d96372ef 4527
1c54ff97
AJ
4528 case IPCOP_msgrcv:
4529 switch (version) {
4530 case 0:
4531 {
4532 struct target_ipc_kludge {
4533 abi_long msgp;
4534 abi_long msgtyp;
4535 } *tmp;
4536
4537 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
4538 ret = -TARGET_EFAULT;
4539 break;
4540 }
d96372ef 4541
79dd77de 4542 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
d96372ef 4543
1c54ff97
AJ
4544 unlock_user_struct(tmp, ptr, 0);
4545 break;
4546 }
4547 default:
4548 ret = do_msgrcv(first, ptr, second, fifth, third);
4549 }
4550 break;
d96372ef 4551
8853f86e 4552 case IPCOP_shmat:
88a8c984
RV
4553 switch (version) {
4554 default:
5a4a898d
FB
4555 {
4556 abi_ulong raddr;
225a206c 4557 raddr = target_shmat(cpu_env, first, ptr, second);
88a8c984
RV
4558 if (is_error(raddr))
4559 return get_errno(raddr);
2f619698 4560 if (put_user_ual(raddr, third))
5a4a898d 4561 return -TARGET_EFAULT;
88a8c984
RV
4562 break;
4563 }
4564 case 1:
4565 ret = -TARGET_EINVAL;
4566 break;
5a4a898d 4567 }
8853f86e
FB
4568 break;
4569 case IPCOP_shmdt:
225a206c 4570 ret = target_shmdt(ptr);
8853f86e
FB
4571 break;
4572
4573 case IPCOP_shmget:
4574 /* IPC_* flag values are the same on all linux platforms */
4575 ret = get_errno(shmget(first, second, third));
4576 break;
4577
4578 /* IPC_* and SHM_* command values are the same on all linux platforms */
4579 case IPCOP_shmctl:
a2926784 4580 ret = do_shmctl(first, second, ptr);
8853f86e
FB
4581 break;
4582 default:
39be5350
JK
4583 qemu_log_mask(LOG_UNIMP, "Unsupported ipc call: %d (version %d)\n",
4584 call, version);
0da46a6e 4585 ret = -TARGET_ENOSYS;
8853f86e
FB
4586 break;
4587 }
4588 return ret;
4589}
32407103 4590#endif
8853f86e 4591
31e31b8a 4592/* kernel structure types definitions */
31e31b8a 4593
001faf32 4594#define STRUCT(name, ...) STRUCT_ ## name,
31e31b8a
FB
4595#define STRUCT_SPECIAL(name) STRUCT_ ## name,
4596enum {
4597#include "syscall_types.h"
8be656b8 4598STRUCT_MAX
31e31b8a
FB
4599};
4600#undef STRUCT
4601#undef STRUCT_SPECIAL
4602
001faf32 4603#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
31e31b8a
FB
4604#define STRUCT_SPECIAL(name)
4605#include "syscall_types.h"
4606#undef STRUCT
4607#undef STRUCT_SPECIAL
4608
31e31b8a
FB
4609#define MAX_STRUCT_SIZE 4096
4610
dace20dc 4611#ifdef CONFIG_FIEMAP
285da2b9
PM
4612/* So fiemap access checks don't overflow on 32 bit systems.
4613 * This is very slightly smaller than the limit imposed by
4614 * the underlying kernel.
4615 */
4616#define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
4617 / sizeof(struct fiemap_extent))
4618
4619static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
45c874eb 4620 int fd, int cmd, abi_long arg)
285da2b9
PM
4621{
4622 /* The parameter for this ioctl is a struct fiemap followed
4623 * by an array of struct fiemap_extent whose size is set
4624 * in fiemap->fm_extent_count. The array is filled in by the
4625 * ioctl.
4626 */
4627 int target_size_in, target_size_out;
4628 struct fiemap *fm;
4629 const argtype *arg_type = ie->arg_type;
4630 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
4631 void *argptr, *p;
4632 abi_long ret;
4633 int i, extent_size = thunk_type_size(extent_arg_type, 0);
4634 uint32_t outbufsz;
4635 int free_fm = 0;
4636
4637 assert(arg_type[0] == TYPE_PTR);
4638 assert(ie->access == IOC_RW);
4639 arg_type++;
4640 target_size_in = thunk_type_size(arg_type, 0);
4641 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
4642 if (!argptr) {
4643 return -TARGET_EFAULT;
4644 }
4645 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4646 unlock_user(argptr, arg, 0);
4647 fm = (struct fiemap *)buf_temp;
4648 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
4649 return -TARGET_EINVAL;
4650 }
4651
4652 outbufsz = sizeof (*fm) +
4653 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
4654
4655 if (outbufsz > MAX_STRUCT_SIZE) {
4656 /* We can't fit all the extents into the fixed size buffer.
4657 * Allocate one that is large enough and use it instead.
4658 */
0e173b24 4659 fm = g_try_malloc(outbufsz);
285da2b9
PM
4660 if (!fm) {
4661 return -TARGET_ENOMEM;
4662 }
4663 memcpy(fm, buf_temp, sizeof(struct fiemap));
4664 free_fm = 1;
4665 }
49ca6f3e 4666 ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm));
285da2b9
PM
4667 if (!is_error(ret)) {
4668 target_size_out = target_size_in;
4669 /* An extent_count of 0 means we were only counting the extents
4670 * so there are no structs to copy
4671 */
4672 if (fm->fm_extent_count != 0) {
4673 target_size_out += fm->fm_mapped_extents * extent_size;
4674 }
4675 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
4676 if (!argptr) {
4677 ret = -TARGET_EFAULT;
4678 } else {
4679 /* Convert the struct fiemap */
4680 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
4681 if (fm->fm_extent_count != 0) {
4682 p = argptr + target_size_in;
4683 /* ...and then all the struct fiemap_extents */
4684 for (i = 0; i < fm->fm_mapped_extents; i++) {
4685 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
4686 THUNK_TARGET);
4687 p += extent_size;
4688 }
4689 }
4690 unlock_user(argptr, arg, target_size_out);
4691 }
4692 }
4693 if (free_fm) {
0e173b24 4694 g_free(fm);
285da2b9
PM
4695 }
4696 return ret;
4697}
dace20dc 4698#endif
285da2b9 4699
059c2f2c 4700static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
45c874eb 4701 int fd, int cmd, abi_long arg)
059c2f2c
LV
4702{
4703 const argtype *arg_type = ie->arg_type;
4704 int target_size;
4705 void *argptr;
4706 int ret;
4707 struct ifconf *host_ifconf;
4708 uint32_t outbufsz;
4709 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
4df7b7fa 4710 const argtype ifreq_max_type[] = { MK_STRUCT(STRUCT_ifmap_ifreq) };
059c2f2c
LV
4711 int target_ifreq_size;
4712 int nb_ifreq;
4713 int free_buf = 0;
4714 int i;
4715 int target_ifc_len;
4716 abi_long target_ifc_buf;
4717 int host_ifc_len;
4718 char *host_ifc_buf;
4719
4720 assert(arg_type[0] == TYPE_PTR);
4721 assert(ie->access == IOC_RW);
4722
4723 arg_type++;
4724 target_size = thunk_type_size(arg_type, 0);
4725
4726 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4727 if (!argptr)
4728 return -TARGET_EFAULT;
4729 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4730 unlock_user(argptr, arg, 0);
4731
4732 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
059c2f2c 4733 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
4df7b7fa 4734 target_ifreq_size = thunk_type_size(ifreq_max_type, 0);
059c2f2c 4735
22e4a267
KL
4736 if (target_ifc_buf != 0) {
4737 target_ifc_len = host_ifconf->ifc_len;
4738 nb_ifreq = target_ifc_len / target_ifreq_size;
4739 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
4740
4741 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
4742 if (outbufsz > MAX_STRUCT_SIZE) {
4743 /*
4744 * We can't fit all the extents into the fixed size buffer.
4745 * Allocate one that is large enough and use it instead.
4746 */
7a5626a1 4747 host_ifconf = g_try_malloc(outbufsz);
22e4a267
KL
4748 if (!host_ifconf) {
4749 return -TARGET_ENOMEM;
4750 }
4751 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
4752 free_buf = 1;
059c2f2c 4753 }
22e4a267 4754 host_ifc_buf = (char *)host_ifconf + sizeof(*host_ifconf);
059c2f2c 4755
22e4a267
KL
4756 host_ifconf->ifc_len = host_ifc_len;
4757 } else {
4758 host_ifc_buf = NULL;
4759 }
059c2f2c
LV
4760 host_ifconf->ifc_buf = host_ifc_buf;
4761
49ca6f3e 4762 ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf));
059c2f2c
LV
4763 if (!is_error(ret)) {
4764 /* convert host ifc_len to target ifc_len */
4765
4766 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
4767 target_ifc_len = nb_ifreq * target_ifreq_size;
4768 host_ifconf->ifc_len = target_ifc_len;
4769
4770 /* restore target ifc_buf */
4771
4772 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
4773
4774 /* copy struct ifconf to target user */
4775
4776 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4777 if (!argptr)
4778 return -TARGET_EFAULT;
4779 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
4780 unlock_user(argptr, arg, target_size);
4781
22e4a267
KL
4782 if (target_ifc_buf != 0) {
4783 /* copy ifreq[] to target user */
4784 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
4785 for (i = 0; i < nb_ifreq ; i++) {
4786 thunk_convert(argptr + i * target_ifreq_size,
4787 host_ifc_buf + i * sizeof(struct ifreq),
4788 ifreq_arg_type, THUNK_TARGET);
4789 }
4790 unlock_user(argptr, target_ifc_buf, target_ifc_len);
059c2f2c 4791 }
059c2f2c
LV
4792 }
4793
4794 if (free_buf) {
7a5626a1 4795 g_free(host_ifconf);
059c2f2c
LV
4796 }
4797
4798 return ret;
4799}
4800
a133367e
CT
4801#if defined(CONFIG_USBFS)
4802#if HOST_LONG_BITS > 64
4803#error USBDEVFS thunks do not support >64 bit hosts yet.
4804#endif
4805struct live_urb {
4806 uint64_t target_urb_adr;
4807 uint64_t target_buf_adr;
4808 char *target_buf_ptr;
4809 struct usbdevfs_urb host_urb;
4810};
4811
4812static GHashTable *usbdevfs_urb_hashtable(void)
4813{
4814 static GHashTable *urb_hashtable;
4815
4816 if (!urb_hashtable) {
4817 urb_hashtable = g_hash_table_new(g_int64_hash, g_int64_equal);
4818 }
4819 return urb_hashtable;
4820}
4821
4822static void urb_hashtable_insert(struct live_urb *urb)
4823{
4824 GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
4825 g_hash_table_insert(urb_hashtable, urb, urb);
4826}
4827
4828static struct live_urb *urb_hashtable_lookup(uint64_t target_urb_adr)
4829{
4830 GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
4831 return g_hash_table_lookup(urb_hashtable, &target_urb_adr);
4832}
4833
4834static void urb_hashtable_remove(struct live_urb *urb)
4835{
4836 GHashTable *urb_hashtable = usbdevfs_urb_hashtable();
4837 g_hash_table_remove(urb_hashtable, urb);
4838}
4839
4840static abi_long
4841do_ioctl_usbdevfs_reapurb(const IOCTLEntry *ie, uint8_t *buf_temp,
4842 int fd, int cmd, abi_long arg)
4843{
4844 const argtype usbfsurb_arg_type[] = { MK_STRUCT(STRUCT_usbdevfs_urb) };
4845 const argtype ptrvoid_arg_type[] = { TYPE_PTRVOID, 0, 0 };
4846 struct live_urb *lurb;
4847 void *argptr;
4848 uint64_t hurb;
4849 int target_size;
4850 uintptr_t target_urb_adr;
4851 abi_long ret;
4852
4853 target_size = thunk_type_size(usbfsurb_arg_type, THUNK_TARGET);
4854
4855 memset(buf_temp, 0, sizeof(uint64_t));
4856 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
4857 if (is_error(ret)) {
4858 return ret;
4859 }
4860
4861 memcpy(&hurb, buf_temp, sizeof(uint64_t));
4862 lurb = (void *)((uintptr_t)hurb - offsetof(struct live_urb, host_urb));
4863 if (!lurb->target_urb_adr) {
4864 return -TARGET_EFAULT;
4865 }
4866 urb_hashtable_remove(lurb);
4867 unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr,
4868 lurb->host_urb.buffer_length);
4869 lurb->target_buf_ptr = NULL;
4870
4871 /* restore the guest buffer pointer */
4872 lurb->host_urb.buffer = (void *)(uintptr_t)lurb->target_buf_adr;
4873
4874 /* update the guest urb struct */
4875 argptr = lock_user(VERIFY_WRITE, lurb->target_urb_adr, target_size, 0);
4876 if (!argptr) {
4877 g_free(lurb);
4878 return -TARGET_EFAULT;
4879 }
4880 thunk_convert(argptr, &lurb->host_urb, usbfsurb_arg_type, THUNK_TARGET);
4881 unlock_user(argptr, lurb->target_urb_adr, target_size);
4882
4883 target_size = thunk_type_size(ptrvoid_arg_type, THUNK_TARGET);
4884 /* write back the urb handle */
4885 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
4886 if (!argptr) {
4887 g_free(lurb);
4888 return -TARGET_EFAULT;
4889 }
4890
4891 /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */
4892 target_urb_adr = lurb->target_urb_adr;
4893 thunk_convert(argptr, &target_urb_adr, ptrvoid_arg_type, THUNK_TARGET);
4894 unlock_user(argptr, arg, target_size);
4895
4896 g_free(lurb);
4897 return ret;
4898}
4899
4900static abi_long
4901do_ioctl_usbdevfs_discardurb(const IOCTLEntry *ie,
4902 uint8_t *buf_temp __attribute__((unused)),
4903 int fd, int cmd, abi_long arg)
4904{
4905 struct live_urb *lurb;
4906
4907 /* map target address back to host URB with metadata. */
4908 lurb = urb_hashtable_lookup(arg);
4909 if (!lurb) {
4910 return -TARGET_EFAULT;
4911 }
4912 return get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
4913}
4914
4915static abi_long
4916do_ioctl_usbdevfs_submiturb(const IOCTLEntry *ie, uint8_t *buf_temp,
4917 int fd, int cmd, abi_long arg)
4918{
4919 const argtype *arg_type = ie->arg_type;
4920 int target_size;
4921 abi_long ret;
4922 void *argptr;
4923 int rw_dir;
4924 struct live_urb *lurb;
4925
4926 /*
4927 * each submitted URB needs to map to a unique ID for the
4928 * kernel, and that unique ID needs to be a pointer to
4929 * host memory. hence, we need to malloc for each URB.
4930 * isochronous transfers have a variable length struct.
4931 */
4932 arg_type++;
4933 target_size = thunk_type_size(arg_type, THUNK_TARGET);
4934
4935 /* construct host copy of urb and metadata */
b21e2380 4936 lurb = g_try_new0(struct live_urb, 1);
a133367e
CT
4937 if (!lurb) {
4938 return -TARGET_ENOMEM;
4939 }
4940
4941 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4942 if (!argptr) {
4943 g_free(lurb);
4944 return -TARGET_EFAULT;
4945 }
4946 thunk_convert(&lurb->host_urb, argptr, arg_type, THUNK_HOST);
4947 unlock_user(argptr, arg, 0);
4948
4949 lurb->target_urb_adr = arg;
4950 lurb->target_buf_adr = (uintptr_t)lurb->host_urb.buffer;
4951
4952 /* buffer space used depends on endpoint type so lock the entire buffer */
4953 /* control type urbs should check the buffer contents for true direction */
4954 rw_dir = lurb->host_urb.endpoint & USB_DIR_IN ? VERIFY_WRITE : VERIFY_READ;
4955 lurb->target_buf_ptr = lock_user(rw_dir, lurb->target_buf_adr,
4956 lurb->host_urb.buffer_length, 1);
4957 if (lurb->target_buf_ptr == NULL) {
4958 g_free(lurb);
4959 return -TARGET_EFAULT;
4960 }
4961
4962 /* update buffer pointer in host copy */
4963 lurb->host_urb.buffer = lurb->target_buf_ptr;
4964
4965 ret = get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb));
4966 if (is_error(ret)) {
4967 unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, 0);
4968 g_free(lurb);
4969 } else {
4970 urb_hashtable_insert(lurb);
4971 }
4972
4973 return ret;
4974}
4975#endif /* CONFIG_USBFS */
4976
56e904ec 4977static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
45c874eb 4978 int cmd, abi_long arg)
56e904ec
AG
4979{
4980 void *argptr;
4981 struct dm_ioctl *host_dm;
4982 abi_long guest_data;
4983 uint32_t guest_data_size;
4984 int target_size;
4985 const argtype *arg_type = ie->arg_type;
4986 abi_long ret;
4987 void *big_buf = NULL;
4988 char *host_data;
4989
4990 arg_type++;
4991 target_size = thunk_type_size(arg_type, 0);
4992 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
4993 if (!argptr) {
4994 ret = -TARGET_EFAULT;
4995 goto out;
4996 }
4997 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
4998 unlock_user(argptr, arg, 0);
4999
5000 /* buf_temp is too small, so fetch things into a bigger buffer */
5001 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
5002 memcpy(big_buf, buf_temp, target_size);
5003 buf_temp = big_buf;
5004 host_dm = big_buf;
5005
5006 guest_data = arg + host_dm->data_start;
5007 if ((guest_data - arg) < 0) {
f9757b1d 5008 ret = -TARGET_EINVAL;
56e904ec
AG
5009 goto out;
5010 }
5011 guest_data_size = host_dm->data_size - host_dm->data_start;
5012 host_data = (char*)host_dm + host_dm->data_start;
5013
5014 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
3211215e
PM
5015 if (!argptr) {
5016 ret = -TARGET_EFAULT;
5017 goto out;
5018 }
5019
56e904ec
AG
5020 switch (ie->host_cmd) {
5021 case DM_REMOVE_ALL:
5022 case DM_LIST_DEVICES:
5023 case DM_DEV_CREATE:
5024 case DM_DEV_REMOVE:
5025 case DM_DEV_SUSPEND:
5026 case DM_DEV_STATUS:
5027 case DM_DEV_WAIT:
5028 case DM_TABLE_STATUS:
5029 case DM_TABLE_CLEAR:
5030 case DM_TABLE_DEPS:
5031 case DM_LIST_VERSIONS:
5032 /* no input data */
5033 break;
5034 case DM_DEV_RENAME:
5035 case DM_DEV_SET_GEOMETRY:
5036 /* data contains only strings */
5037 memcpy(host_data, argptr, guest_data_size);
5038 break;
5039 case DM_TARGET_MSG:
5040 memcpy(host_data, argptr, guest_data_size);
5041 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
5042 break;
5043 case DM_TABLE_LOAD:
5044 {
5045 void *gspec = argptr;
5046 void *cur_data = host_data;
eb2929ca
LV
5047 const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
5048 int spec_size = thunk_type_size(dm_arg_type, 0);
56e904ec
AG
5049 int i;
5050
5051 for (i = 0; i < host_dm->target_count; i++) {
5052 struct dm_target_spec *spec = cur_data;
5053 uint32_t next;
5054 int slen;
5055
eb2929ca 5056 thunk_convert(spec, gspec, dm_arg_type, THUNK_HOST);
56e904ec
AG
5057 slen = strlen((char*)gspec + spec_size) + 1;
5058 next = spec->next;
5059 spec->next = sizeof(*spec) + slen;
5060 strcpy((char*)&spec[1], gspec + spec_size);
5061 gspec += next;
5062 cur_data += spec->next;
5063 }
5064 break;
5065 }
5066 default:
5067 ret = -TARGET_EINVAL;
dec0473d 5068 unlock_user(argptr, guest_data, 0);
56e904ec
AG
5069 goto out;
5070 }
5071 unlock_user(argptr, guest_data, 0);
5072
49ca6f3e 5073 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
56e904ec
AG
5074 if (!is_error(ret)) {
5075 guest_data = arg + host_dm->data_start;
5076 guest_data_size = host_dm->data_size - host_dm->data_start;
5077 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
5078 switch (ie->host_cmd) {
5079 case DM_REMOVE_ALL:
5080 case DM_DEV_CREATE:
5081 case DM_DEV_REMOVE:
5082 case DM_DEV_RENAME:
5083 case DM_DEV_SUSPEND:
5084 case DM_DEV_STATUS:
5085 case DM_TABLE_LOAD:
5086 case DM_TABLE_CLEAR:
5087 case DM_TARGET_MSG:
5088 case DM_DEV_SET_GEOMETRY:
5089 /* no return data */
5090 break;
5091 case DM_LIST_DEVICES:
5092 {
5093 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
5094 uint32_t remaining_data = guest_data_size;
5095 void *cur_data = argptr;
eb2929ca 5096 const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
56e904ec
AG
5097 int nl_size = 12; /* can't use thunk_size due to alignment */
5098
5099 while (1) {
5100 uint32_t next = nl->next;
5101 if (next) {
5102 nl->next = nl_size + (strlen(nl->name) + 1);
5103 }
5104 if (remaining_data < nl->next) {
5105 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5106 break;
5107 }
eb2929ca 5108 thunk_convert(cur_data, nl, dm_arg_type, THUNK_TARGET);
56e904ec
AG
5109 strcpy(cur_data + nl_size, nl->name);
5110 cur_data += nl->next;
5111 remaining_data -= nl->next;
5112 if (!next) {
5113 break;
5114 }
5115 nl = (void*)nl + next;
5116 }
5117 break;
5118 }
5119 case DM_DEV_WAIT:
5120 case DM_TABLE_STATUS:
5121 {
5122 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
5123 void *cur_data = argptr;
eb2929ca
LV
5124 const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
5125 int spec_size = thunk_type_size(dm_arg_type, 0);
56e904ec
AG
5126 int i;
5127
5128 for (i = 0; i < host_dm->target_count; i++) {
5129 uint32_t next = spec->next;
5130 int slen = strlen((char*)&spec[1]) + 1;
5131 spec->next = (cur_data - argptr) + spec_size + slen;
5132 if (guest_data_size < spec->next) {
5133 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5134 break;
5135 }
eb2929ca 5136 thunk_convert(cur_data, spec, dm_arg_type, THUNK_TARGET);
56e904ec
AG
5137 strcpy(cur_data + spec_size, (char*)&spec[1]);
5138 cur_data = argptr + spec->next;
5139 spec = (void*)host_dm + host_dm->data_start + next;
5140 }
5141 break;
5142 }
5143 case DM_TABLE_DEPS:
5144 {
5145 void *hdata = (void*)host_dm + host_dm->data_start;
5146 int count = *(uint32_t*)hdata;
5147 uint64_t *hdev = hdata + 8;
5148 uint64_t *gdev = argptr + 8;
5149 int i;
5150
5151 *(uint32_t*)argptr = tswap32(count);
5152 for (i = 0; i < count; i++) {
5153 *gdev = tswap64(*hdev);
5154 gdev++;
5155 hdev++;
5156 }
5157 break;
5158 }
5159 case DM_LIST_VERSIONS:
5160 {
5161 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
5162 uint32_t remaining_data = guest_data_size;
5163 void *cur_data = argptr;
eb2929ca
LV
5164 const argtype dm_arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
5165 int vers_size = thunk_type_size(dm_arg_type, 0);
56e904ec
AG
5166
5167 while (1) {
5168 uint32_t next = vers->next;
5169 if (next) {
5170 vers->next = vers_size + (strlen(vers->name) + 1);
5171 }
5172 if (remaining_data < vers->next) {
5173 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5174 break;
5175 }
eb2929ca 5176 thunk_convert(cur_data, vers, dm_arg_type, THUNK_TARGET);
56e904ec
AG
5177 strcpy(cur_data + vers_size, vers->name);
5178 cur_data += vers->next;
5179 remaining_data -= vers->next;
5180 if (!next) {
5181 break;
5182 }
5183 vers = (void*)vers + next;
5184 }
5185 break;
5186 }
5187 default:
dec0473d 5188 unlock_user(argptr, guest_data, 0);
56e904ec
AG
5189 ret = -TARGET_EINVAL;
5190 goto out;
5191 }
5192 unlock_user(argptr, guest_data, guest_data_size);
5193
5194 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5195 if (!argptr) {
5196 ret = -TARGET_EFAULT;
5197 goto out;
5198 }
5199 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5200 unlock_user(argptr, arg, target_size);
5201 }
5202out:
ad11ad77 5203 g_free(big_buf);
56e904ec
AG
5204 return ret;
5205}
5206
a59b5e35 5207static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
45c874eb 5208 int cmd, abi_long arg)
a59b5e35
AG
5209{
5210 void *argptr;
5211 int target_size;
5212 const argtype *arg_type = ie->arg_type;
5213 const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
5214 abi_long ret;
5215
5216 struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
5217 struct blkpg_partition host_part;
5218
5219 /* Read and convert blkpg */
5220 arg_type++;
5221 target_size = thunk_type_size(arg_type, 0);
5222 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5223 if (!argptr) {
5224 ret = -TARGET_EFAULT;
5225 goto out;
5226 }
5227 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5228 unlock_user(argptr, arg, 0);
5229
5230 switch (host_blkpg->op) {
5231 case BLKPG_ADD_PARTITION:
5232 case BLKPG_DEL_PARTITION:
5233 /* payload is struct blkpg_partition */
5234 break;
5235 default:
5236 /* Unknown opcode */
5237 ret = -TARGET_EINVAL;
5238 goto out;
5239 }
5240
5241 /* Read and convert blkpg->data */
5242 arg = (abi_long)(uintptr_t)host_blkpg->data;
5243 target_size = thunk_type_size(part_arg_type, 0);
5244 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5245 if (!argptr) {
5246 ret = -TARGET_EFAULT;
5247 goto out;
5248 }
5249 thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
5250 unlock_user(argptr, arg, 0);
5251
5252 /* Swizzle the data pointer to our local copy and call! */
5253 host_blkpg->data = &host_part;
49ca6f3e 5254 ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg));
a59b5e35
AG
5255
5256out:
5257 return ret;
5258}
5259
7ff7b666 5260static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
45c874eb 5261 int fd, int cmd, abi_long arg)
7ff7b666
LV
5262{
5263 const argtype *arg_type = ie->arg_type;
5264 const StructEntry *se;
5265 const argtype *field_types;
5266 const int *dst_offsets, *src_offsets;
5267 int target_size;
5268 void *argptr;
b78c522a
MAL
5269 abi_ulong *target_rt_dev_ptr = NULL;
5270 unsigned long *host_rt_dev_ptr = NULL;
7ff7b666
LV
5271 abi_long ret;
5272 int i;
5273
5274 assert(ie->access == IOC_W);
5275 assert(*arg_type == TYPE_PTR);
5276 arg_type++;
5277 assert(*arg_type == TYPE_STRUCT);
5278 target_size = thunk_type_size(arg_type, 0);
5279 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5280 if (!argptr) {
5281 return -TARGET_EFAULT;
5282 }
5283 arg_type++;
5284 assert(*arg_type == (int)STRUCT_rtentry);
5285 se = struct_entries + *arg_type++;
5286 assert(se->convert[0] == NULL);
5287 /* convert struct here to be able to catch rt_dev string */
5288 field_types = se->field_types;
5289 dst_offsets = se->field_offsets[THUNK_HOST];
5290 src_offsets = se->field_offsets[THUNK_TARGET];
5291 for (i = 0; i < se->nb_fields; i++) {
5292 if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
5293 assert(*field_types == TYPE_PTRVOID);
3d558330 5294 target_rt_dev_ptr = argptr + src_offsets[i];
7ff7b666
LV
5295 host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
5296 if (*target_rt_dev_ptr != 0) {
5297 *host_rt_dev_ptr = (unsigned long)lock_user_string(
5298 tswapal(*target_rt_dev_ptr));
5299 if (!*host_rt_dev_ptr) {
5300 unlock_user(argptr, arg, 0);
5301 return -TARGET_EFAULT;
5302 }
5303 } else {
5304 *host_rt_dev_ptr = 0;
5305 }
5306 field_types++;
5307 continue;
5308 }
5309 field_types = thunk_convert(buf_temp + dst_offsets[i],
5310 argptr + src_offsets[i],
5311 field_types, THUNK_HOST);
5312 }
5313 unlock_user(argptr, arg, 0);
5314
49ca6f3e 5315 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
b78c522a
MAL
5316
5317 assert(host_rt_dev_ptr != NULL);
5318 assert(target_rt_dev_ptr != NULL);
7ff7b666
LV
5319 if (*host_rt_dev_ptr != 0) {
5320 unlock_user((void *)*host_rt_dev_ptr,
5321 *target_rt_dev_ptr, 0);
5322 }
5323 return ret;
5324}
5325
ca56f5b5 5326static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
45c874eb 5327 int fd, int cmd, abi_long arg)
ca56f5b5
PB
5328{
5329 int sig = target_to_host_signal(arg);
49ca6f3e 5330 return get_errno(safe_ioctl(fd, ie->host_cmd, sig));
ca56f5b5
PB
5331}
5332
6d5d5dde
DB
5333static abi_long do_ioctl_SIOCGSTAMP(const IOCTLEntry *ie, uint8_t *buf_temp,
5334 int fd, int cmd, abi_long arg)
5335{
5336 struct timeval tv;
5337 abi_long ret;
5338
5339 ret = get_errno(safe_ioctl(fd, SIOCGSTAMP, &tv));
5340 if (is_error(ret)) {
5341 return ret;
5342 }
5343
5344 if (cmd == (int)TARGET_SIOCGSTAMP_OLD) {
5345 if (copy_to_user_timeval(arg, &tv)) {
5346 return -TARGET_EFAULT;
5347 }
5348 } else {
5349 if (copy_to_user_timeval64(arg, &tv)) {
5350 return -TARGET_EFAULT;
5351 }
5352 }
5353
5354 return ret;
5355}
5356
5357static abi_long do_ioctl_SIOCGSTAMPNS(const IOCTLEntry *ie, uint8_t *buf_temp,
5358 int fd, int cmd, abi_long arg)
5359{
5360 struct timespec ts;
5361 abi_long ret;
5362
5363 ret = get_errno(safe_ioctl(fd, SIOCGSTAMPNS, &ts));
5364 if (is_error(ret)) {
5365 return ret;
5366 }
5367
5368 if (cmd == (int)TARGET_SIOCGSTAMPNS_OLD) {
5369 if (host_to_target_timespec(arg, &ts)) {
5370 return -TARGET_EFAULT;
5371 }
5372 } else{
5373 if (host_to_target_timespec64(arg, &ts)) {
5374 return -TARGET_EFAULT;
5375 }
5376 }
5377
5378 return ret;
5379}
5380
2b74f621
AS
5381#ifdef TIOCGPTPEER
5382static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_temp,
5383 int fd, int cmd, abi_long arg)
5384{
5385 int flags = target_to_host_bitmask(arg, fcntl_flags_tbl);
5386 return get_errno(safe_ioctl(fd, ie->host_cmd, flags));
5387}
5388#endif
5389
e865b97f
CG
5390#ifdef HAVE_DRM_H
5391
5392static void unlock_drm_version(struct drm_version *host_ver,
5393 struct target_drm_version *target_ver,
5394 bool copy)
5395{
5396 unlock_user(host_ver->name, target_ver->name,
5397 copy ? host_ver->name_len : 0);
5398 unlock_user(host_ver->date, target_ver->date,
5399 copy ? host_ver->date_len : 0);
5400 unlock_user(host_ver->desc, target_ver->desc,
5401 copy ? host_ver->desc_len : 0);
5402}
5403
5404static inline abi_long target_to_host_drmversion(struct drm_version *host_ver,
5405 struct target_drm_version *target_ver)
5406{
5407 memset(host_ver, 0, sizeof(*host_ver));
5408
5409 __get_user(host_ver->name_len, &target_ver->name_len);
5410 if (host_ver->name_len) {
5411 host_ver->name = lock_user(VERIFY_WRITE, target_ver->name,
5412 target_ver->name_len, 0);
5413 if (!host_ver->name) {
5414 return -EFAULT;
5415 }
5416 }
5417
5418 __get_user(host_ver->date_len, &target_ver->date_len);
5419 if (host_ver->date_len) {
5420 host_ver->date = lock_user(VERIFY_WRITE, target_ver->date,
5421 target_ver->date_len, 0);
5422 if (!host_ver->date) {
5423 goto err;
5424 }
5425 }
5426
5427 __get_user(host_ver->desc_len, &target_ver->desc_len);
5428 if (host_ver->desc_len) {
5429 host_ver->desc = lock_user(VERIFY_WRITE, target_ver->desc,
5430 target_ver->desc_len, 0);
5431 if (!host_ver->desc) {
5432 goto err;
5433 }
5434 }
5435
5436 return 0;
5437err:
5438 unlock_drm_version(host_ver, target_ver, false);
5439 return -EFAULT;
5440}
5441
5442static inline void host_to_target_drmversion(
5443 struct target_drm_version *target_ver,
5444 struct drm_version *host_ver)
5445{
5446 __put_user(host_ver->version_major, &target_ver->version_major);
5447 __put_user(host_ver->version_minor, &target_ver->version_minor);
5448 __put_user(host_ver->version_patchlevel, &target_ver->version_patchlevel);
5449 __put_user(host_ver->name_len, &target_ver->name_len);
5450 __put_user(host_ver->date_len, &target_ver->date_len);
5451 __put_user(host_ver->desc_len, &target_ver->desc_len);
5452 unlock_drm_version(host_ver, target_ver, true);
5453}
5454
5455static abi_long do_ioctl_drm(const IOCTLEntry *ie, uint8_t *buf_temp,
5456 int fd, int cmd, abi_long arg)
5457{
5458 struct drm_version *ver;
5459 struct target_drm_version *target_ver;
5460 abi_long ret;
5461
5462 switch (ie->host_cmd) {
5463 case DRM_IOCTL_VERSION:
5464 if (!lock_user_struct(VERIFY_WRITE, target_ver, arg, 0)) {
5465 return -TARGET_EFAULT;
5466 }
5467 ver = (struct drm_version *)buf_temp;
5468 ret = target_to_host_drmversion(ver, target_ver);
5469 if (!is_error(ret)) {
5470 ret = get_errno(safe_ioctl(fd, ie->host_cmd, ver));
5471 if (is_error(ret)) {
5472 unlock_drm_version(ver, target_ver, false);
5473 } else {
5474 host_to_target_drmversion(target_ver, ver);
5475 }
5476 }
5477 unlock_user_struct(target_ver, arg, 0);
5478 return ret;
5479 }
5480 return -TARGET_ENOSYS;
5481}
5482
913b03c2
CG
5483static abi_long do_ioctl_drm_i915_getparam(const IOCTLEntry *ie,
5484 struct drm_i915_getparam *gparam,
5485 int fd, abi_long arg)
5486{
5487 abi_long ret;
5488 int value;
5489 struct target_drm_i915_getparam *target_gparam;
5490
5491 if (!lock_user_struct(VERIFY_READ, target_gparam, arg, 0)) {
5492 return -TARGET_EFAULT;
5493 }
5494
5495 __get_user(gparam->param, &target_gparam->param);
5496 gparam->value = &value;
5497 ret = get_errno(safe_ioctl(fd, ie->host_cmd, gparam));
5498 put_user_s32(value, target_gparam->value);
5499
5500 unlock_user_struct(target_gparam, arg, 0);
5501 return ret;
5502}
5503
5504static abi_long do_ioctl_drm_i915(const IOCTLEntry *ie, uint8_t *buf_temp,
5505 int fd, int cmd, abi_long arg)
5506{
5507 switch (ie->host_cmd) {
5508 case DRM_IOCTL_I915_GETPARAM:
5509 return do_ioctl_drm_i915_getparam(ie,
5510 (struct drm_i915_getparam *)buf_temp,
5511 fd, arg);
5512 default:
5513 return -TARGET_ENOSYS;
5514 }
5515}
5516
e865b97f
CG
5517#endif
5518
6addf06a
SCW
5519static abi_long do_ioctl_TUNSETTXFILTER(const IOCTLEntry *ie, uint8_t *buf_temp,
5520 int fd, int cmd, abi_long arg)
5521{
5522 struct tun_filter *filter = (struct tun_filter *)buf_temp;
5523 struct tun_filter *target_filter;
5524 char *target_addr;
5525
5526 assert(ie->access == IOC_W);
5527
5528 target_filter = lock_user(VERIFY_READ, arg, sizeof(*target_filter), 1);
5529 if (!target_filter) {
5530 return -TARGET_EFAULT;
5531 }
5532 filter->flags = tswap16(target_filter->flags);
5533 filter->count = tswap16(target_filter->count);
5534 unlock_user(target_filter, arg, 0);
5535
5536 if (filter->count) {
5537 if (offsetof(struct tun_filter, addr) + filter->count * ETH_ALEN >
5538 MAX_STRUCT_SIZE) {
5539 return -TARGET_EFAULT;
5540 }
5541
5542 target_addr = lock_user(VERIFY_READ,
5543 arg + offsetof(struct tun_filter, addr),
5544 filter->count * ETH_ALEN, 1);
5545 if (!target_addr) {
5546 return -TARGET_EFAULT;
5547 }
5548 memcpy(filter->addr, target_addr, filter->count * ETH_ALEN);
5549 unlock_user(target_addr, arg + offsetof(struct tun_filter, addr), 0);
5550 }
5551
5552 return get_errno(safe_ioctl(fd, ie->host_cmd, filter));
5553}
5554
79482e59 5555IOCTLEntry ioctl_entries[] = {
001faf32 5556#define IOCTL(cmd, access, ...) \
d2ef05bb
PM
5557 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
5558#define IOCTL_SPECIAL(cmd, access, dofn, ...) \
5559 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
8cb762fe
HD
5560#define IOCTL_IGNORE(cmd) \
5561 { TARGET_ ## cmd, 0, #cmd },
31e31b8a
FB
5562#include "ioctls.h"
5563 { 0, 0, },
5564};
5565
53a5960a 5566/* ??? Implement proper locking for ioctls. */
0da46a6e 5567/* do_ioctl() Must return target values and target errnos. */
45c874eb 5568static abi_long do_ioctl(int fd, int cmd, abi_long arg)
31e31b8a
FB
5569{
5570 const IOCTLEntry *ie;
5571 const argtype *arg_type;
992f48a0 5572 abi_long ret;
31e31b8a 5573 uint8_t buf_temp[MAX_STRUCT_SIZE];
53a5960a
PB
5574 int target_size;
5575 void *argptr;
31e31b8a
FB
5576
5577 ie = ioctl_entries;
5578 for(;;) {
5579 if (ie->target_cmd == 0) {
39be5350
JK
5580 qemu_log_mask(
5581 LOG_UNIMP, "Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
59d11727 5582 return -TARGET_ENOTTY;
31e31b8a
FB
5583 }
5584 if (ie->target_cmd == cmd)
5585 break;
5586 ie++;
5587 }
5588 arg_type = ie->arg_type;
d2ef05bb
PM
5589 if (ie->do_ioctl) {
5590 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
8cb762fe
HD
5591 } else if (!ie->host_cmd) {
5592 /* Some architectures define BSD ioctls in their headers
5593 that are not implemented in Linux. */
59d11727 5594 return -TARGET_ENOTTY;
d2ef05bb
PM
5595 }
5596
31e31b8a
FB
5597 switch(arg_type[0]) {
5598 case TYPE_NULL:
5599 /* no argument */
49ca6f3e 5600 ret = get_errno(safe_ioctl(fd, ie->host_cmd));
31e31b8a
FB
5601 break;
5602 case TYPE_PTRVOID:
5603 case TYPE_INT:
c858e537
FB
5604 case TYPE_LONG:
5605 case TYPE_ULONG:
49ca6f3e 5606 ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
31e31b8a
FB
5607 break;
5608 case TYPE_PTR:
5609 arg_type++;
53a5960a 5610 target_size = thunk_type_size(arg_type, 0);
31e31b8a
FB
5611 switch(ie->access) {
5612 case IOC_R:
49ca6f3e 5613 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
31e31b8a 5614 if (!is_error(ret)) {
579a97f7
FB
5615 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5616 if (!argptr)
5617 return -TARGET_EFAULT;
53a5960a
PB
5618 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5619 unlock_user(argptr, arg, target_size);
31e31b8a
FB
5620 }
5621 break;
5622 case IOC_W:
579a97f7
FB
5623 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5624 if (!argptr)
5625 return -TARGET_EFAULT;
53a5960a
PB
5626 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5627 unlock_user(argptr, arg, 0);
49ca6f3e 5628 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
31e31b8a
FB
5629 break;
5630 default:
5631 case IOC_RW:
579a97f7
FB
5632 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5633 if (!argptr)
5634 return -TARGET_EFAULT;
53a5960a
PB
5635 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5636 unlock_user(argptr, arg, 0);
49ca6f3e 5637 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
31e31b8a 5638 if (!is_error(ret)) {
579a97f7
FB
5639 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5640 if (!argptr)
5641 return -TARGET_EFAULT;
53a5960a
PB
5642 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5643 unlock_user(argptr, arg, target_size);
31e31b8a
FB
5644 }
5645 break;
5646 }
5647 break;
5648 default:
39be5350
JK
5649 qemu_log_mask(LOG_UNIMP,
5650 "Unsupported ioctl type: cmd=0x%04lx type=%d\n",
5651 (long)cmd, arg_type[0]);
59d11727 5652 ret = -TARGET_ENOTTY;
31e31b8a
FB
5653 break;
5654 }
5655 return ret;
5656}
5657
b39bc503 5658static const bitmask_transtbl iflag_tbl[] = {
31e31b8a
FB
5659 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
5660 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
5661 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
5662 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
5663 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
5664 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
5665 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
5666 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
5667 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
5668 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
5669 { TARGET_IXON, TARGET_IXON, IXON, IXON },
5670 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
5671 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
5672 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
c218b4ed 5673 { TARGET_IUTF8, TARGET_IUTF8, IUTF8, IUTF8},
31e31b8a
FB
5674};
5675
b39bc503 5676static const bitmask_transtbl oflag_tbl[] = {
31e31b8a
FB
5677 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
5678 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
5679 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
5680 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
5681 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
5682 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
5683 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
5684 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
5685 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
5686 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
5687 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
5688 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
5689 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
5690 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
5691 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
5692 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
5693 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
5694 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
5695 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
5696 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
5697 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
5698 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
5699 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
5700 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
31e31b8a
FB
5701};
5702
b39bc503 5703static const bitmask_transtbl cflag_tbl[] = {
31e31b8a
FB
5704 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
5705 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
5706 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
5707 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
5708 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
5709 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
5710 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
5711 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
5712 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
5713 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
5714 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
5715 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
5716 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
5717 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
5718 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
5719 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
5720 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
5721 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
5722 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
5723 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
5724 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
5725 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
5726 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
5727 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
5728 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
5729 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
5730 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
5731 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
5732 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
5733 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
5734 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
31e31b8a
FB
5735};
5736
b39bc503 5737static const bitmask_transtbl lflag_tbl[] = {
c218b4ed
FB
5738 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
5739 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
5740 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
5741 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
5742 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
5743 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
5744 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
5745 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
5746 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
5747 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
5748 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
5749 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
5750 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
5751 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
5752 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
5753 { TARGET_EXTPROC, TARGET_EXTPROC, EXTPROC, EXTPROC},
31e31b8a
FB
5754};
5755
5756static void target_to_host_termios (void *dst, const void *src)
5757{
5758 struct host_termios *host = dst;
5759 const struct target_termios *target = src;
3b46e624 5760
5fafdf24 5761 host->c_iflag =
31e31b8a 5762 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
5fafdf24 5763 host->c_oflag =
31e31b8a 5764 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
5fafdf24 5765 host->c_cflag =
31e31b8a 5766 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
5fafdf24 5767 host->c_lflag =
31e31b8a
FB
5768 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
5769 host->c_line = target->c_line;
3b46e624 5770
44607123 5771 memset(host->c_cc, 0, sizeof(host->c_cc));
5fafdf24
TS
5772 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
5773 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
3b46e624 5774 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
5fafdf24 5775 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
3b46e624 5776 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
5fafdf24 5777 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
3b46e624 5778 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
5fafdf24 5779 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
3b46e624 5780 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
5fafdf24
TS
5781 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
5782 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
3b46e624
TS
5783 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
5784 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
5785 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
5786 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
5787 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
5fafdf24 5788 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
31e31b8a 5789}
3b46e624 5790
31e31b8a
FB
5791static void host_to_target_termios (void *dst, const void *src)
5792{
5793 struct target_termios *target = dst;
5794 const struct host_termios *host = src;
5795
5fafdf24 5796 target->c_iflag =
31e31b8a 5797 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
5fafdf24 5798 target->c_oflag =
31e31b8a 5799 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
5fafdf24 5800 target->c_cflag =
31e31b8a 5801 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
5fafdf24 5802 target->c_lflag =
31e31b8a
FB
5803 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
5804 target->c_line = host->c_line;
3b46e624 5805
44607123 5806 memset(target->c_cc, 0, sizeof(target->c_cc));
31e31b8a
FB
5807 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
5808 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
5809 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
5810 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
5811 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
5812 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
5813 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
5814 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
5815 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
5816 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
5817 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
5818 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
5819 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
5820 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
5821 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
5822 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
5823 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
5824}
5825
8e853dc7 5826static const StructEntry struct_termios_def = {
31e31b8a
FB
5827 .convert = { host_to_target_termios, target_to_host_termios },
5828 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
5829 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
888468db 5830 .print = print_termios,
31e31b8a
FB
5831};
5832
4b840f96
RH
5833/* If the host does not provide these bits, they may be safely discarded. */
5834#ifndef MAP_SYNC
5835#define MAP_SYNC 0
5836#endif
5837#ifndef MAP_UNINITIALIZED
5838#define MAP_UNINITIALIZED 0
5839#endif
5840
180d4ef3 5841static const bitmask_transtbl mmap_flags_tbl[] = {
541e1690
HD
5842 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
5843 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS,
5844 MAP_ANONYMOUS, MAP_ANONYMOUS },
5845 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN,
5846 MAP_GROWSDOWN, MAP_GROWSDOWN },
5847 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE,
5848 MAP_DENYWRITE, MAP_DENYWRITE },
5849 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE,
5850 MAP_EXECUTABLE, MAP_EXECUTABLE },
5851 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
5852 { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE,
5853 MAP_NORESERVE, MAP_NORESERVE },
5854 { TARGET_MAP_HUGETLB, TARGET_MAP_HUGETLB, MAP_HUGETLB, MAP_HUGETLB },
5855 /* MAP_STACK had been ignored by the kernel for quite some time.
5856 Recognize it for the target insofar as we do not want to pass
5857 it through to the host. */
5858 { TARGET_MAP_STACK, TARGET_MAP_STACK, 0, 0 },
4b840f96
RH
5859 { TARGET_MAP_NONBLOCK, TARGET_MAP_NONBLOCK, MAP_NONBLOCK, MAP_NONBLOCK },
5860 { TARGET_MAP_POPULATE, TARGET_MAP_POPULATE, MAP_POPULATE, MAP_POPULATE },
5861 { TARGET_MAP_FIXED_NOREPLACE, TARGET_MAP_FIXED_NOREPLACE,
5862 MAP_FIXED_NOREPLACE, MAP_FIXED_NOREPLACE },
5863 { TARGET_MAP_UNINITIALIZED, TARGET_MAP_UNINITIALIZED,
5864 MAP_UNINITIALIZED, MAP_UNINITIALIZED },
5286db75
FB
5865};
5866
9ab8d071
RH
5867/*
5868 * Arrange for legacy / undefined architecture specific flags to be
5869 * ignored by mmap handling code.
5870 */
5871#ifndef TARGET_MAP_32BIT
5872#define TARGET_MAP_32BIT 0
5873#endif
5874#ifndef TARGET_MAP_HUGE_2MB
5875#define TARGET_MAP_HUGE_2MB 0
5876#endif
5877#ifndef TARGET_MAP_HUGE_1GB
5878#define TARGET_MAP_HUGE_1GB 0
5879#endif
5880
5881static abi_long do_mmap(abi_ulong addr, abi_ulong len, int prot,
5882 int target_flags, int fd, off_t offset)
5883{
5884 /*
5885 * The historical set of flags that all mmap types implicitly support.
5886 */
5887 enum {
5888 TARGET_LEGACY_MAP_MASK = TARGET_MAP_SHARED
5889 | TARGET_MAP_PRIVATE
5890 | TARGET_MAP_FIXED
5891 | TARGET_MAP_ANONYMOUS
5892 | TARGET_MAP_DENYWRITE
5893 | TARGET_MAP_EXECUTABLE
5894 | TARGET_MAP_UNINITIALIZED
5895 | TARGET_MAP_GROWSDOWN
5896 | TARGET_MAP_LOCKED
5897 | TARGET_MAP_NORESERVE
5898 | TARGET_MAP_POPULATE
5899 | TARGET_MAP_NONBLOCK
5900 | TARGET_MAP_STACK
5901 | TARGET_MAP_HUGETLB
5902 | TARGET_MAP_32BIT
5903 | TARGET_MAP_HUGE_2MB
5904 | TARGET_MAP_HUGE_1GB
5905 };
5906 int host_flags;
5907
5908 switch (target_flags & TARGET_MAP_TYPE) {
5909 case TARGET_MAP_PRIVATE:
5910 host_flags = MAP_PRIVATE;
5911 break;
5912 case TARGET_MAP_SHARED:
5913 host_flags = MAP_SHARED;
5914 break;
5915 case TARGET_MAP_SHARED_VALIDATE:
5916 /*
5917 * MAP_SYNC is only supported for MAP_SHARED_VALIDATE, and is
5918 * therefore omitted from mmap_flags_tbl and TARGET_LEGACY_MAP_MASK.
5919 */
5920 if (target_flags & ~(TARGET_LEGACY_MAP_MASK | TARGET_MAP_SYNC)) {
5921 return -TARGET_EOPNOTSUPP;
5922 }
5923 host_flags = MAP_SHARED_VALIDATE;
5924 if (target_flags & TARGET_MAP_SYNC) {
5925 host_flags |= MAP_SYNC;
5926 }
5927 break;
5928 default:
5929 return -TARGET_EINVAL;
5930 }
5931 host_flags |= target_to_host_bitmask(target_flags, mmap_flags_tbl);
5932
5933 return get_errno(target_mmap(addr, len, prot, host_flags, fd, offset));
5934}
5935
9d12f79d
LV
5936/*
5937 * NOTE: TARGET_ABI32 is defined for TARGET_I386 (but not for TARGET_X86_64)
5938 * TARGET_I386 is defined if TARGET_X86_64 is defined
5939 */
2ab83ea7 5940#if defined(TARGET_I386)
6dbad63e
FB
5941
5942/* NOTE: there is really one LDT for all the threads */
b1d8e52e 5943static uint8_t *ldt_table;
6dbad63e 5944
03acab66 5945static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
6dbad63e
FB
5946{
5947 int size;
53a5960a 5948 void *p;
6dbad63e
FB
5949
5950 if (!ldt_table)
5951 return 0;
5952 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
5953 if (size > bytecount)
5954 size = bytecount;
579a97f7
FB
5955 p = lock_user(VERIFY_WRITE, ptr, size, 0);
5956 if (!p)
03acab66 5957 return -TARGET_EFAULT;
579a97f7 5958 /* ??? Should this by byteswapped? */
53a5960a
PB
5959 memcpy(p, ldt_table, size);
5960 unlock_user(p, ptr, size);
6dbad63e
FB
5961 return size;
5962}
5963
5964/* XXX: add locking support */
03acab66
FB
5965static abi_long write_ldt(CPUX86State *env,
5966 abi_ulong ptr, unsigned long bytecount, int oldmode)
6dbad63e
FB
5967{
5968 struct target_modify_ldt_ldt_s ldt_info;
53a5960a 5969 struct target_modify_ldt_ldt_s *target_ldt_info;
6dbad63e 5970 int seg_32bit, contents, read_exec_only, limit_in_pages;
8d18e893 5971 int seg_not_present, useable, lm;
6dbad63e
FB
5972 uint32_t *lp, entry_1, entry_2;
5973
5974 if (bytecount != sizeof(ldt_info))
03acab66 5975 return -TARGET_EINVAL;
579a97f7 5976 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
03acab66 5977 return -TARGET_EFAULT;
53a5960a 5978 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
cbb21eed 5979 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
53a5960a
PB
5980 ldt_info.limit = tswap32(target_ldt_info->limit);
5981 ldt_info.flags = tswap32(target_ldt_info->flags);
5982 unlock_user_struct(target_ldt_info, ptr, 0);
3b46e624 5983
6dbad63e 5984 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
03acab66 5985 return -TARGET_EINVAL;
6dbad63e
FB
5986 seg_32bit = ldt_info.flags & 1;
5987 contents = (ldt_info.flags >> 1) & 3;
5988 read_exec_only = (ldt_info.flags >> 3) & 1;
5989 limit_in_pages = (ldt_info.flags >> 4) & 1;
5990 seg_not_present = (ldt_info.flags >> 5) & 1;
5991 useable = (ldt_info.flags >> 6) & 1;
8d18e893
FB
5992#ifdef TARGET_ABI32
5993 lm = 0;
5994#else
5995 lm = (ldt_info.flags >> 7) & 1;
5996#endif
6dbad63e
FB
5997 if (contents == 3) {
5998 if (oldmode)
03acab66 5999 return -TARGET_EINVAL;
6dbad63e 6000 if (seg_not_present == 0)
03acab66 6001 return -TARGET_EINVAL;
6dbad63e
FB
6002 }
6003 /* allocate the LDT */
6004 if (!ldt_table) {
e441570f
AZ
6005 env->ldt.base = target_mmap(0,
6006 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
6007 PROT_READ|PROT_WRITE,
6008 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
6009 if (env->ldt.base == -1)
03acab66 6010 return -TARGET_ENOMEM;
3e8f1628 6011 memset(g2h_untagged(env->ldt.base), 0,
e441570f 6012 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
6dbad63e 6013 env->ldt.limit = 0xffff;
3e8f1628 6014 ldt_table = g2h_untagged(env->ldt.base);
6dbad63e
FB
6015 }
6016
6017 /* NOTE: same code as Linux kernel */
6018 /* Allow LDTs to be cleared by the user. */
6019 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
6020 if (oldmode ||
6021 (contents == 0 &&
6022 read_exec_only == 1 &&
6023 seg_32bit == 0 &&
6024 limit_in_pages == 0 &&
6025 seg_not_present == 1 &&
6026 useable == 0 )) {
6027 entry_1 = 0;
6028 entry_2 = 0;
6029 goto install;
6030 }
6031 }
3b46e624 6032
6dbad63e
FB
6033 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
6034 (ldt_info.limit & 0x0ffff);
6035 entry_2 = (ldt_info.base_addr & 0xff000000) |
6036 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
6037 (ldt_info.limit & 0xf0000) |
6038 ((read_exec_only ^ 1) << 9) |
6039 (contents << 10) |
6040 ((seg_not_present ^ 1) << 15) |
6041 (seg_32bit << 22) |
6042 (limit_in_pages << 23) |
8d18e893 6043 (lm << 21) |
6dbad63e
FB
6044 0x7000;
6045 if (!oldmode)
6046 entry_2 |= (useable << 20);
14ae3ba7 6047
6dbad63e
FB
6048 /* Install the new entry ... */
6049install:
6050 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
6051 lp[0] = tswap32(entry_1);
6052 lp[1] = tswap32(entry_2);
6053 return 0;
6054}
6055
6056/* specific and weird i386 syscalls */
8fcd3692
BS
6057static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
6058 unsigned long bytecount)
6dbad63e 6059{
03acab66 6060 abi_long ret;
3b46e624 6061
6dbad63e
FB
6062 switch (func) {
6063 case 0:
6064 ret = read_ldt(ptr, bytecount);
6065 break;
6066 case 1:
6067 ret = write_ldt(env, ptr, bytecount, 1);
6068 break;
6069 case 0x11:
6070 ret = write_ldt(env, ptr, bytecount, 0);
6071 break;
03acab66
FB
6072 default:
6073 ret = -TARGET_ENOSYS;
6074 break;
6dbad63e
FB
6075 }
6076 return ret;
6077}
1b6b029e 6078
9d12f79d 6079#if defined(TARGET_ABI32)
bc22eb44 6080abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
8d18e893 6081{
3e8f1628 6082 uint64_t *gdt_table = g2h_untagged(env->gdt.base);
8d18e893
FB
6083 struct target_modify_ldt_ldt_s ldt_info;
6084 struct target_modify_ldt_ldt_s *target_ldt_info;
6085 int seg_32bit, contents, read_exec_only, limit_in_pages;
6086 int seg_not_present, useable, lm;
6087 uint32_t *lp, entry_1, entry_2;
6088 int i;
6089
6090 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
6091 if (!target_ldt_info)
6092 return -TARGET_EFAULT;
6093 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
cbb21eed 6094 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
8d18e893
FB
6095 ldt_info.limit = tswap32(target_ldt_info->limit);
6096 ldt_info.flags = tswap32(target_ldt_info->flags);
6097 if (ldt_info.entry_number == -1) {
6098 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
6099 if (gdt_table[i] == 0) {
6100 ldt_info.entry_number = i;
6101 target_ldt_info->entry_number = tswap32(i);
6102 break;
6103 }
6104 }
6105 }
6106 unlock_user_struct(target_ldt_info, ptr, 1);
6107
6108 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
6109 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
6110 return -TARGET_EINVAL;
6111 seg_32bit = ldt_info.flags & 1;
6112 contents = (ldt_info.flags >> 1) & 3;
6113 read_exec_only = (ldt_info.flags >> 3) & 1;
6114 limit_in_pages = (ldt_info.flags >> 4) & 1;
6115 seg_not_present = (ldt_info.flags >> 5) & 1;
6116 useable = (ldt_info.flags >> 6) & 1;
6117#ifdef TARGET_ABI32
6118 lm = 0;
6119#else
6120 lm = (ldt_info.flags >> 7) & 1;
6121#endif
6122
6123 if (contents == 3) {
6124 if (seg_not_present == 0)
6125 return -TARGET_EINVAL;
6126 }
6127
6128 /* NOTE: same code as Linux kernel */
6129 /* Allow LDTs to be cleared by the user. */
6130 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
6131 if ((contents == 0 &&
6132 read_exec_only == 1 &&
6133 seg_32bit == 0 &&
6134 limit_in_pages == 0 &&
6135 seg_not_present == 1 &&
6136 useable == 0 )) {
6137 entry_1 = 0;
6138 entry_2 = 0;
6139 goto install;
6140 }
6141 }
6142
6143 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
6144 (ldt_info.limit & 0x0ffff);
6145 entry_2 = (ldt_info.base_addr & 0xff000000) |
6146 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
6147 (ldt_info.limit & 0xf0000) |
6148 ((read_exec_only ^ 1) << 9) |
6149 (contents << 10) |
6150 ((seg_not_present ^ 1) << 15) |
6151 (seg_32bit << 22) |
6152 (limit_in_pages << 23) |
6153 (useable << 20) |
6154 (lm << 21) |
6155 0x7000;
6156
6157 /* Install the new entry ... */
6158install:
6159 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
6160 lp[0] = tswap32(entry_1);
6161 lp[1] = tswap32(entry_2);
6162 return 0;
6163}
6164
8fcd3692 6165static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
8d18e893
FB
6166{
6167 struct target_modify_ldt_ldt_s *target_ldt_info;
3e8f1628 6168 uint64_t *gdt_table = g2h_untagged(env->gdt.base);
8d18e893
FB
6169 uint32_t base_addr, limit, flags;
6170 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
6171 int seg_not_present, useable, lm;
6172 uint32_t *lp, entry_1, entry_2;
6173
6174 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
6175 if (!target_ldt_info)
6176 return -TARGET_EFAULT;
6177 idx = tswap32(target_ldt_info->entry_number);
6178 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
6179 idx > TARGET_GDT_ENTRY_TLS_MAX) {
6180 unlock_user_struct(target_ldt_info, ptr, 1);
6181 return -TARGET_EINVAL;
6182 }
6183 lp = (uint32_t *)(gdt_table + idx);
6184 entry_1 = tswap32(lp[0]);
6185 entry_2 = tswap32(lp[1]);
6186
6187 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
6188 contents = (entry_2 >> 10) & 3;
6189 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
6190 seg_32bit = (entry_2 >> 22) & 1;
6191 limit_in_pages = (entry_2 >> 23) & 1;
6192 useable = (entry_2 >> 20) & 1;
6193#ifdef TARGET_ABI32
6194 lm = 0;
6195#else
6196 lm = (entry_2 >> 21) & 1;
6197#endif
6198 flags = (seg_32bit << 0) | (contents << 1) |
6199 (read_exec_only << 3) | (limit_in_pages << 4) |
6200 (seg_not_present << 5) | (useable << 6) | (lm << 7);
6201 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
6202 base_addr = (entry_1 >> 16) |
6203 (entry_2 & 0xff000000) |
6204 ((entry_2 & 0xff) << 16);
cbb21eed 6205 target_ldt_info->base_addr = tswapal(base_addr);
8d18e893
FB
6206 target_ldt_info->limit = tswap32(limit);
6207 target_ldt_info->flags = tswap32(flags);
6208 unlock_user_struct(target_ldt_info, ptr, 1);
6209 return 0;
6210}
6211
9d12f79d
LV
6212abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
6213{
538fabcb 6214 return -TARGET_ENOSYS;
9d12f79d
LV
6215}
6216#else
2667e71c 6217abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
d2fd1af7 6218{
1add8698 6219 abi_long ret = 0;
d2fd1af7
FB
6220 abi_ulong val;
6221 int idx;
1add8698 6222
d2fd1af7
FB
6223 switch(code) {
6224 case TARGET_ARCH_SET_GS:
6225 case TARGET_ARCH_SET_FS:
6226 if (code == TARGET_ARCH_SET_GS)
6227 idx = R_GS;
6228 else
6229 idx = R_FS;
6230 cpu_x86_load_seg(env, idx, 0);
6231 env->segs[idx].base = addr;
6232 break;
6233 case TARGET_ARCH_GET_GS:
6234 case TARGET_ARCH_GET_FS:
6235 if (code == TARGET_ARCH_GET_GS)
6236 idx = R_GS;
6237 else
6238 idx = R_FS;
6239 val = env->segs[idx].base;
6240 if (put_user(val, addr, abi_ulong))
1add8698 6241 ret = -TARGET_EFAULT;
d2fd1af7
FB
6242 break;
6243 default:
6244 ret = -TARGET_EINVAL;
6245 break;
6246 }
1add8698 6247 return ret;
d2fd1af7 6248}
9d12f79d 6249#endif /* defined(TARGET_ABI32 */
2ab83ea7
FB
6250#endif /* defined(TARGET_I386) */
6251
87e9bf23
RH
6252/*
6253 * These constants are generic. Supply any that are missing from the host.
6254 */
6255#ifndef PR_SET_NAME
6256# define PR_SET_NAME 15
6257# define PR_GET_NAME 16
6258#endif
6259#ifndef PR_SET_FP_MODE
6260# define PR_SET_FP_MODE 45
6261# define PR_GET_FP_MODE 46
6262# define PR_FP_MODE_FR (1 << 0)
6263# define PR_FP_MODE_FRE (1 << 1)
6264#endif
6265#ifndef PR_SVE_SET_VL
6266# define PR_SVE_SET_VL 50
6267# define PR_SVE_GET_VL 51
6268# define PR_SVE_VL_LEN_MASK 0xffff
6269# define PR_SVE_VL_INHERIT (1 << 17)
6270#endif
6271#ifndef PR_PAC_RESET_KEYS
6272# define PR_PAC_RESET_KEYS 54
6273# define PR_PAC_APIAKEY (1 << 0)
6274# define PR_PAC_APIBKEY (1 << 1)
6275# define PR_PAC_APDAKEY (1 << 2)
6276# define PR_PAC_APDBKEY (1 << 3)
6277# define PR_PAC_APGAKEY (1 << 4)
6278#endif
6279#ifndef PR_SET_TAGGED_ADDR_CTRL
6280# define PR_SET_TAGGED_ADDR_CTRL 55
6281# define PR_GET_TAGGED_ADDR_CTRL 56
6282# define PR_TAGGED_ADDR_ENABLE (1UL << 0)
6283#endif
6284#ifndef PR_MTE_TCF_SHIFT
6285# define PR_MTE_TCF_SHIFT 1
6286# define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT)
6287# define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT)
6288# define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT)
6289# define PR_MTE_TCF_MASK (3UL << PR_MTE_TCF_SHIFT)
6290# define PR_MTE_TAG_SHIFT 3
6291# define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT)
6292#endif
220717a6
RH
6293#ifndef PR_SET_IO_FLUSHER
6294# define PR_SET_IO_FLUSHER 57
6295# define PR_GET_IO_FLUSHER 58
6296#endif
6297#ifndef PR_SET_SYSCALL_USER_DISPATCH
6298# define PR_SET_SYSCALL_USER_DISPATCH 59
6299#endif
24d87c18
RH
6300#ifndef PR_SME_SET_VL
6301# define PR_SME_SET_VL 63
6302# define PR_SME_GET_VL 64
6303# define PR_SME_VL_LEN_MASK 0xffff
6304# define PR_SME_VL_INHERIT (1 << 17)
6305#endif
87e9bf23
RH
6306
6307#include "target_prctl.h"
6308
6309static abi_long do_prctl_inval0(CPUArchState *env)
6310{
6311 return -TARGET_EINVAL;
6312}
6313
6314static abi_long do_prctl_inval1(CPUArchState *env, abi_long arg2)
6315{
6316 return -TARGET_EINVAL;
6317}
6318
6319#ifndef do_prctl_get_fp_mode
6320#define do_prctl_get_fp_mode do_prctl_inval0
6321#endif
6322#ifndef do_prctl_set_fp_mode
6323#define do_prctl_set_fp_mode do_prctl_inval1
6324#endif
fd72f5d0
RH
6325#ifndef do_prctl_sve_get_vl
6326#define do_prctl_sve_get_vl do_prctl_inval0
87e9bf23 6327#endif
fd72f5d0
RH
6328#ifndef do_prctl_sve_set_vl
6329#define do_prctl_sve_set_vl do_prctl_inval1
87e9bf23
RH
6330#endif
6331#ifndef do_prctl_reset_keys
6332#define do_prctl_reset_keys do_prctl_inval1
6333#endif
6334#ifndef do_prctl_set_tagged_addr_ctrl
6335#define do_prctl_set_tagged_addr_ctrl do_prctl_inval1
6336#endif
6337#ifndef do_prctl_get_tagged_addr_ctrl
6338#define do_prctl_get_tagged_addr_ctrl do_prctl_inval0
6339#endif
6e8dcacd
RH
6340#ifndef do_prctl_get_unalign
6341#define do_prctl_get_unalign do_prctl_inval1
6342#endif
6343#ifndef do_prctl_set_unalign
6344#define do_prctl_set_unalign do_prctl_inval1
6345#endif
24d87c18
RH
6346#ifndef do_prctl_sme_get_vl
6347#define do_prctl_sme_get_vl do_prctl_inval0
6348#endif
6349#ifndef do_prctl_sme_set_vl
6350#define do_prctl_sme_set_vl do_prctl_inval1
6351#endif
87e9bf23
RH
6352
6353static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2,
6354 abi_long arg3, abi_long arg4, abi_long arg5)
6355{
6356 abi_long ret;
6357
6358 switch (option) {
6359 case PR_GET_PDEATHSIG:
6360 {
6361 int deathsig;
6362 ret = get_errno(prctl(PR_GET_PDEATHSIG, &deathsig,
6363 arg3, arg4, arg5));
1edebb36
RH
6364 if (!is_error(ret) &&
6365 put_user_s32(host_to_target_signal(deathsig), arg2)) {
87e9bf23
RH
6366 return -TARGET_EFAULT;
6367 }
6368 return ret;
6369 }
f746c659
RH
6370 case PR_SET_PDEATHSIG:
6371 return get_errno(prctl(PR_SET_PDEATHSIG, target_to_host_signal(arg2),
6372 arg3, arg4, arg5));
87e9bf23
RH
6373 case PR_GET_NAME:
6374 {
6375 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
6376 if (!name) {
6377 return -TARGET_EFAULT;
6378 }
6379 ret = get_errno(prctl(PR_GET_NAME, (uintptr_t)name,
6380 arg3, arg4, arg5));
6381 unlock_user(name, arg2, 16);
6382 return ret;
6383 }
6384 case PR_SET_NAME:
6385 {
6386 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
6387 if (!name) {
6388 return -TARGET_EFAULT;
6389 }
6390 ret = get_errno(prctl(PR_SET_NAME, (uintptr_t)name,
6391 arg3, arg4, arg5));
6392 unlock_user(name, arg2, 0);
6393 return ret;
6394 }
6395 case PR_GET_FP_MODE:
6396 return do_prctl_get_fp_mode(env);
6397 case PR_SET_FP_MODE:
6398 return do_prctl_set_fp_mode(env, arg2);
6399 case PR_SVE_GET_VL:
fd72f5d0 6400 return do_prctl_sve_get_vl(env);
87e9bf23 6401 case PR_SVE_SET_VL:
fd72f5d0 6402 return do_prctl_sve_set_vl(env, arg2);
24d87c18
RH
6403 case PR_SME_GET_VL:
6404 return do_prctl_sme_get_vl(env);
6405 case PR_SME_SET_VL:
6406 return do_prctl_sme_set_vl(env, arg2);
87e9bf23
RH
6407 case PR_PAC_RESET_KEYS:
6408 if (arg3 || arg4 || arg5) {
6409 return -TARGET_EINVAL;
6410 }
6411 return do_prctl_reset_keys(env, arg2);
6412 case PR_SET_TAGGED_ADDR_CTRL:
6413 if (arg3 || arg4 || arg5) {
6414 return -TARGET_EINVAL;
6415 }
6416 return do_prctl_set_tagged_addr_ctrl(env, arg2);
6417 case PR_GET_TAGGED_ADDR_CTRL:
6418 if (arg2 || arg3 || arg4 || arg5) {
6419 return -TARGET_EINVAL;
6420 }
6421 return do_prctl_get_tagged_addr_ctrl(env);
220717a6 6422
6e8dcacd
RH
6423 case PR_GET_UNALIGN:
6424 return do_prctl_get_unalign(env, arg2);
6425 case PR_SET_UNALIGN:
6426 return do_prctl_set_unalign(env, arg2);
6427
4f4e5567
RH
6428 case PR_CAP_AMBIENT:
6429 case PR_CAPBSET_READ:
6430 case PR_CAPBSET_DROP:
220717a6
RH
6431 case PR_GET_DUMPABLE:
6432 case PR_SET_DUMPABLE:
6433 case PR_GET_KEEPCAPS:
6434 case PR_SET_KEEPCAPS:
4f4e5567
RH
6435 case PR_GET_SECUREBITS:
6436 case PR_SET_SECUREBITS:
220717a6
RH
6437 case PR_GET_TIMING:
6438 case PR_SET_TIMING:
6439 case PR_GET_TIMERSLACK:
6440 case PR_SET_TIMERSLACK:
6441 case PR_MCE_KILL:
6442 case PR_MCE_KILL_GET:
6443 case PR_GET_NO_NEW_PRIVS:
6444 case PR_SET_NO_NEW_PRIVS:
6445 case PR_GET_IO_FLUSHER:
6446 case PR_SET_IO_FLUSHER:
07726f52 6447 case PR_SET_CHILD_SUBREAPER:
91511bd4
RH
6448 case PR_GET_SPECULATION_CTRL:
6449 case PR_SET_SPECULATION_CTRL:
220717a6
RH
6450 /* Some prctl options have no pointer arguments and we can pass on. */
6451 return get_errno(prctl(option, arg2, arg3, arg4, arg5));
6452
6453 case PR_GET_CHILD_SUBREAPER:
07726f52
RH
6454 {
6455 int val;
6456 ret = get_errno(prctl(PR_GET_CHILD_SUBREAPER, &val,
6457 arg3, arg4, arg5));
6458 if (!is_error(ret) && put_user_s32(val, arg2)) {
6459 return -TARGET_EFAULT;
6460 }
6461 return ret;
6462 }
6463
220717a6 6464 case PR_GET_TID_ADDRESS:
8de24b15 6465 {
59272469 6466 TaskState *ts = get_task_state(env_cpu(env));
8de24b15
RH
6467 return put_user_ual(ts->child_tidptr, arg2);
6468 }
220717a6
RH
6469
6470 case PR_GET_FPEXC:
6471 case PR_SET_FPEXC:
6472 /* Was used for SPE on PowerPC. */
6473 return -TARGET_EINVAL;
6474
6475 case PR_GET_ENDIAN:
6476 case PR_SET_ENDIAN:
6477 case PR_GET_FPEMU:
6478 case PR_SET_FPEMU:
6479 case PR_SET_MM:
87e9bf23
RH
6480 case PR_GET_SECCOMP:
6481 case PR_SET_SECCOMP:
220717a6
RH
6482 case PR_SET_SYSCALL_USER_DISPATCH:
6483 case PR_GET_THP_DISABLE:
6484 case PR_SET_THP_DISABLE:
6485 case PR_GET_TSC:
6486 case PR_SET_TSC:
220717a6 6487 /* Disable to prevent the target disabling stuff we need. */
87e9bf23 6488 return -TARGET_EINVAL;
220717a6 6489
87e9bf23 6490 default:
220717a6
RH
6491 qemu_log_mask(LOG_UNIMP, "Unsupported prctl: " TARGET_ABI_FMT_ld "\n",
6492 option);
6493 return -TARGET_EINVAL;
87e9bf23
RH
6494 }
6495}
6496
05098a93 6497#define NEW_STACK_SIZE 0x40000
d865bab5 6498
d865bab5
PB
6499
6500static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
6501typedef struct {
9349b4f9 6502 CPUArchState *env;
d865bab5
PB
6503 pthread_mutex_t mutex;
6504 pthread_cond_t cond;
6505 pthread_t thread;
6506 uint32_t tid;
6507 abi_ulong child_tidptr;
6508 abi_ulong parent_tidptr;
6509 sigset_t sigmask;
6510} new_thread_info;
6511
6512static void *clone_func(void *arg)
6513{
6514 new_thread_info *info = arg;
9349b4f9 6515 CPUArchState *env;
0d34282f 6516 CPUState *cpu;
edf8e2af 6517 TaskState *ts;
d865bab5 6518
70903763 6519 rcu_register_thread();
3468b59e 6520 tcg_register_thread();
d865bab5 6521 env = info->env;
29a0af61 6522 cpu = env_cpu(env);
a2247f8e 6523 thread_cpu = cpu;
e4e5cb4a 6524 ts = get_task_state(cpu);
71ba74f6 6525 info->tid = sys_gettid();
edf8e2af 6526 task_settid(ts);
d865bab5
PB
6527 if (info->child_tidptr)
6528 put_user_u32(info->tid, info->child_tidptr);
6529 if (info->parent_tidptr)
6530 put_user_u32(info->tid, info->parent_tidptr);
5ebdd774 6531 qemu_guest_random_seed_thread_part2(cpu->random_seed);
d865bab5
PB
6532 /* Enable signals. */
6533 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
6534 /* Signal to the parent that we're ready. */
6535 pthread_mutex_lock(&info->mutex);
6536 pthread_cond_broadcast(&info->cond);
6537 pthread_mutex_unlock(&info->mutex);
a4dd3d51 6538 /* Wait until the parent has finished initializing the tls state. */
d865bab5
PB
6539 pthread_mutex_lock(&clone_lock);
6540 pthread_mutex_unlock(&clone_lock);
6541 cpu_loop(env);
6542 /* never exits */
6543 return NULL;
6544}
1b6b029e 6545
0da46a6e
TS
6546/* do_fork() Must return host values and target errnos (unlike most
6547 do_*() functions). */
9349b4f9 6548static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
d865bab5
PB
6549 abi_ulong parent_tidptr, target_ulong newtls,
6550 abi_ulong child_tidptr)
1b6b029e 6551{
29a0af61 6552 CPUState *cpu = env_cpu(env);
1b6b029e 6553 int ret;
5cd4393b 6554 TaskState *ts;
0429a971 6555 CPUState *new_cpu;
9349b4f9 6556 CPUArchState *new_env;
d865bab5 6557 sigset_t sigmask;
3b46e624 6558
5ea2fc84
PM
6559 flags &= ~CLONE_IGNORED_FLAGS;
6560
436d124b
AZ
6561 /* Emulate vfork() with fork() */
6562 if (flags & CLONE_VFORK)
6563 flags &= ~(CLONE_VFORK | CLONE_VM);
6564
1b6b029e 6565 if (flags & CLONE_VM) {
e4e5cb4a 6566 TaskState *parent_ts = get_task_state(cpu);
d865bab5
PB
6567 new_thread_info info;
6568 pthread_attr_t attr;
24cb36a6 6569
5ea2fc84
PM
6570 if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) ||
6571 (flags & CLONE_INVALID_THREAD_FLAGS)) {
6572 return -TARGET_EINVAL;
6573 }
6574
c78d65e8 6575 ts = g_new0(TaskState, 1);
624f7979 6576 init_task_state(ts);
73a988d9
MF
6577
6578 /* Grab a mutex so that thread setup appears atomic. */
6579 pthread_mutex_lock(&clone_lock);
6580
6cc9d67c
RH
6581 /*
6582 * If this is our first additional thread, we need to ensure we
6583 * generate code for parallel execution and flush old translations.
6584 * Do this now so that the copy gets CF_PARALLEL too.
6585 */
b254c342
PMD
6586 if (!tcg_cflags_has(cpu, CF_PARALLEL)) {
6587 tcg_cflags_set(cpu, CF_PARALLEL);
6cc9d67c
RH
6588 tb_flush(cpu);
6589 }
6590
1b6b029e 6591 /* we create a new CPU instance. */
c5be9f08 6592 new_env = cpu_copy(env);
6e68e076 6593 /* Init regs that differ from the parent. */
608999d1 6594 cpu_clone_regs_child(new_env, newsp, flags);
07a6ecf4 6595 cpu_clone_regs_parent(env, flags);
29a0af61 6596 new_cpu = env_cpu(new_env);
0429a971 6597 new_cpu->opaque = ts;
edf8e2af
MW
6598 ts->bprm = parent_ts->bprm;
6599 ts->info = parent_ts->info;
3d3efba0 6600 ts->signal_mask = parent_ts->signal_mask;
d865bab5 6601
7cfbd386 6602 if (flags & CLONE_CHILD_CLEARTID) {
c2764719
PB
6603 ts->child_tidptr = child_tidptr;
6604 }
6605
7cfbd386 6606 if (flags & CLONE_SETTLS) {
d865bab5 6607 cpu_set_tls (new_env, newtls);
7cfbd386 6608 }
d865bab5 6609
d865bab5
PB
6610 memset(&info, 0, sizeof(info));
6611 pthread_mutex_init(&info.mutex, NULL);
6612 pthread_mutex_lock(&info.mutex);
6613 pthread_cond_init(&info.cond, NULL);
6614 info.env = new_env;
7cfbd386 6615 if (flags & CLONE_CHILD_SETTID) {
d865bab5 6616 info.child_tidptr = child_tidptr;
7cfbd386
PM
6617 }
6618 if (flags & CLONE_PARENT_SETTID) {
d865bab5 6619 info.parent_tidptr = parent_tidptr;
7cfbd386 6620 }
d865bab5
PB
6621
6622 ret = pthread_attr_init(&attr);
48e15fc2
NF
6623 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
6624 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
d865bab5
PB
6625 /* It is not safe to deliver signals until the child has finished
6626 initializing, so temporarily block all signals. */
6627 sigfillset(&sigmask);
6628 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
5ebdd774 6629 cpu->random_seed = qemu_guest_random_seed_thread_part1();
d865bab5
PB
6630
6631 ret = pthread_create(&info.thread, &attr, clone_func, &info);
c2764719 6632 /* TODO: Free new CPU state if thread creation failed. */
d865bab5
PB
6633
6634 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
6635 pthread_attr_destroy(&attr);
6636 if (ret == 0) {
6637 /* Wait for the child to initialize. */
6638 pthread_cond_wait(&info.cond, &info.mutex);
6639 ret = info.tid;
d865bab5
PB
6640 } else {
6641 ret = -1;
6642 }
6643 pthread_mutex_unlock(&info.mutex);
6644 pthread_cond_destroy(&info.cond);
6645 pthread_mutex_destroy(&info.mutex);
6646 pthread_mutex_unlock(&clone_lock);
1b6b029e
FB
6647 } else {
6648 /* if no CLONE_VM, we consider it is a fork */
5ea2fc84
PM
6649 if (flags & CLONE_INVALID_FORK_FLAGS) {
6650 return -TARGET_EINVAL;
6651 }
6652
6653 /* We can't support custom termination signals */
6654 if ((flags & CSIGNAL) != TARGET_SIGCHLD) {
93b4eff8
TB
6655 return -TARGET_EINVAL;
6656 }
7d92d34e 6657
895ce8bb
HD
6658#if !defined(__NR_pidfd_open) || !defined(TARGET_NR_pidfd_open)
6659 if (flags & CLONE_PIDFD) {
6660 return -TARGET_EINVAL;
6661 }
6662#endif
6663
6664 /* Can not allow CLONE_PIDFD with CLONE_PARENT_SETTID */
6665 if ((flags & CLONE_PIDFD) && (flags & CLONE_PARENT_SETTID)) {
6666 return -TARGET_EINVAL;
6667 }
6668
7d92d34e 6669 if (block_signals()) {
af254a27 6670 return -QEMU_ERESTARTSYS;
7d92d34e
TB
6671 }
6672
d865bab5 6673 fork_start();
1b6b029e 6674 ret = fork();
d865bab5 6675 if (ret == 0) {
2b1319c8 6676 /* Child Process. */
608999d1 6677 cpu_clone_regs_child(env, newsp, flags);
4edc98fc 6678 fork_end(ret);
2b1319c8
AJ
6679 /* There is a race condition here. The parent process could
6680 theoretically read the TID in the child process before the child
6681 tid is set. This would require using either ptrace
6682 (not implemented) or having *_tidptr to point at a shared memory
6683 mapping. We can't repeat the spinlock hack used above because
6684 the child process gets its own copy of the lock. */
d865bab5 6685 if (flags & CLONE_CHILD_SETTID)
71ba74f6 6686 put_user_u32(sys_gettid(), child_tidptr);
d865bab5 6687 if (flags & CLONE_PARENT_SETTID)
71ba74f6 6688 put_user_u32(sys_gettid(), parent_tidptr);
e4e5cb4a 6689 ts = get_task_state(cpu);
d865bab5
PB
6690 if (flags & CLONE_SETTLS)
6691 cpu_set_tls (env, newtls);
c2764719
PB
6692 if (flags & CLONE_CHILD_CLEARTID)
6693 ts->child_tidptr = child_tidptr;
d865bab5 6694 } else {
07a6ecf4 6695 cpu_clone_regs_parent(env, flags);
895ce8bb
HD
6696 if (flags & CLONE_PIDFD) {
6697 int pid_fd = 0;
6698#if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
6699 int pid_child = ret;
6700 pid_fd = pidfd_open(pid_child, 0);
6701 if (pid_fd >= 0) {
6702 fcntl(pid_fd, F_SETFD, fcntl(pid_fd, F_GETFL)
6703 | FD_CLOEXEC);
6704 } else {
6705 pid_fd = 0;
6706 }
6707#endif
6708 put_user_u32(pid_fd, parent_tidptr);
4edc98fc
IL
6709 }
6710 fork_end(ret);
d865bab5 6711 }
7de0816f 6712 g_assert(!cpu_in_exclusive_context(cpu));
1b6b029e
FB
6713 }
6714 return ret;
6715}
6716
5f106811
APR
6717/* warning : doesn't handle linux specific flags... */
6718static int target_to_host_fcntl_cmd(int cmd)
6719{
4a545761
SB
6720 int ret;
6721
5f106811 6722 switch(cmd) {
4a545761
SB
6723 case TARGET_F_DUPFD:
6724 case TARGET_F_GETFD:
6725 case TARGET_F_SETFD:
6726 case TARGET_F_GETFL:
6727 case TARGET_F_SETFL:
2d92c682
AS
6728 case TARGET_F_OFD_GETLK:
6729 case TARGET_F_OFD_SETLK:
6730 case TARGET_F_OFD_SETLKW:
4a545761
SB
6731 ret = cmd;
6732 break;
6733 case TARGET_F_GETLK:
6734 ret = F_GETLK64;
6735 break;
6736 case TARGET_F_SETLK:
6737 ret = F_SETLK64;
6738 break;
6739 case TARGET_F_SETLKW:
6740 ret = F_SETLKW64;
6741 break;
6742 case TARGET_F_GETOWN:
6743 ret = F_GETOWN;
6744 break;
6745 case TARGET_F_SETOWN:
6746 ret = F_SETOWN;
6747 break;
6748 case TARGET_F_GETSIG:
6749 ret = F_GETSIG;
6750 break;
6751 case TARGET_F_SETSIG:
6752 ret = F_SETSIG;
6753 break;
5f106811 6754#if TARGET_ABI_BITS == 32
4a545761
SB
6755 case TARGET_F_GETLK64:
6756 ret = F_GETLK64;
6757 break;
6758 case TARGET_F_SETLK64:
6759 ret = F_SETLK64;
6760 break;
6761 case TARGET_F_SETLKW64:
6762 ret = F_SETLKW64;
6763 break;
5f106811 6764#endif
4a545761
SB
6765 case TARGET_F_SETLEASE:
6766 ret = F_SETLEASE;
6767 break;
6768 case TARGET_F_GETLEASE:
6769 ret = F_GETLEASE;
6770 break;
fbd5de9b 6771#ifdef F_DUPFD_CLOEXEC
4a545761
SB
6772 case TARGET_F_DUPFD_CLOEXEC:
6773 ret = F_DUPFD_CLOEXEC;
6774 break;
fbd5de9b 6775#endif
4a545761
SB
6776 case TARGET_F_NOTIFY:
6777 ret = F_NOTIFY;
6778 break;
8d5d3004 6779#ifdef F_GETOWN_EX
4a545761
SB
6780 case TARGET_F_GETOWN_EX:
6781 ret = F_GETOWN_EX;
6782 break;
8d5d3004
AS
6783#endif
6784#ifdef F_SETOWN_EX
4a545761
SB
6785 case TARGET_F_SETOWN_EX:
6786 ret = F_SETOWN_EX;
6787 break;
8d5d3004 6788#endif
ddf31aa8 6789#ifdef F_SETPIPE_SZ
4a545761
SB
6790 case TARGET_F_SETPIPE_SZ:
6791 ret = F_SETPIPE_SZ;
6792 break;
6793 case TARGET_F_GETPIPE_SZ:
6794 ret = F_GETPIPE_SZ;
6795 break;
2bb963ff
SCW
6796#endif
6797#ifdef F_ADD_SEALS
6798 case TARGET_F_ADD_SEALS:
6799 ret = F_ADD_SEALS;
6800 break;
6801 case TARGET_F_GET_SEALS:
6802 ret = F_GET_SEALS;
6803 break;
ddf31aa8 6804#endif
4a545761
SB
6805 default:
6806 ret = -TARGET_EINVAL;
6807 break;
6808 }
6809
6810#if defined(__powerpc64__)
6811 /* On PPC64, glibc headers has the F_*LK* defined to 12, 13 and 14 and
6812 * is not supported by kernel. The glibc fcntl call actually adjusts
6813 * them to 5, 6 and 7 before making the syscall(). Since we make the
6814 * syscall directly, adjust to what is supported by the kernel.
6815 */
6816 if (ret >= F_GETLK64 && ret <= F_SETLKW64) {
6817 ret -= F_GETLK64 - 5;
5f106811 6818 }
4a545761
SB
6819#endif
6820
6821 return ret;
5f106811
APR
6822}
6823
ae68ad9f
LV
6824#define FLOCK_TRANSTBL \
6825 switch (type) { \
6826 TRANSTBL_CONVERT(F_RDLCK); \
6827 TRANSTBL_CONVERT(F_WRLCK); \
6828 TRANSTBL_CONVERT(F_UNLCK); \
ae68ad9f
LV
6829 }
6830
6831static int target_to_host_flock(int type)
6832{
6833#define TRANSTBL_CONVERT(a) case TARGET_##a: return a
6834 FLOCK_TRANSTBL
6835#undef TRANSTBL_CONVERT
6836 return -TARGET_EINVAL;
6837}
6838
6839static int host_to_target_flock(int type)
6840{
6841#define TRANSTBL_CONVERT(a) case a: return TARGET_##a
6842 FLOCK_TRANSTBL
6843#undef TRANSTBL_CONVERT
6844 /* if we don't know how to convert the value coming
6845 * from the host we copy to the target field as-is
6846 */
6847 return type;
6848}
2ba7f730 6849
213d3e9e
PM
6850static inline abi_long copy_from_user_flock(struct flock64 *fl,
6851 abi_ulong target_flock_addr)
7775e9ec 6852{
53a5960a 6853 struct target_flock *target_fl;
ae68ad9f 6854 int l_type;
213d3e9e
PM
6855
6856 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6857 return -TARGET_EFAULT;
6858 }
6859
6860 __get_user(l_type, &target_fl->l_type);
ae68ad9f
LV
6861 l_type = target_to_host_flock(l_type);
6862 if (l_type < 0) {
6863 return l_type;
6864 }
6865 fl->l_type = l_type;
213d3e9e
PM
6866 __get_user(fl->l_whence, &target_fl->l_whence);
6867 __get_user(fl->l_start, &target_fl->l_start);
6868 __get_user(fl->l_len, &target_fl->l_len);
6869 __get_user(fl->l_pid, &target_fl->l_pid);
6870 unlock_user_struct(target_fl, target_flock_addr, 0);
6871 return 0;
6872}
6873
6874static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
6875 const struct flock64 *fl)
6876{
6877 struct target_flock *target_fl;
6878 short l_type;
6879
6880 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6881 return -TARGET_EFAULT;
6882 }
6883
ae68ad9f 6884 l_type = host_to_target_flock(fl->l_type);
213d3e9e
PM
6885 __put_user(l_type, &target_fl->l_type);
6886 __put_user(fl->l_whence, &target_fl->l_whence);
6887 __put_user(fl->l_start, &target_fl->l_start);
6888 __put_user(fl->l_len, &target_fl->l_len);
6889 __put_user(fl->l_pid, &target_fl->l_pid);
6890 unlock_user_struct(target_fl, target_flock_addr, 1);
6891 return 0;
6892}
6893
6894typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr);
6895typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl);
6896
6897#if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
ffe81d43
RH
6898struct target_oabi_flock64 {
6899 abi_short l_type;
6900 abi_short l_whence;
6901 abi_llong l_start;
6902 abi_llong l_len;
6903 abi_int l_pid;
6904} QEMU_PACKED;
6905
7f254c5c 6906static inline abi_long copy_from_user_oabi_flock64(struct flock64 *fl,
213d3e9e
PM
6907 abi_ulong target_flock_addr)
6908{
7f254c5c 6909 struct target_oabi_flock64 *target_fl;
ae68ad9f 6910 int l_type;
213d3e9e
PM
6911
6912 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6913 return -TARGET_EFAULT;
6914 }
6915
6916 __get_user(l_type, &target_fl->l_type);
ae68ad9f
LV
6917 l_type = target_to_host_flock(l_type);
6918 if (l_type < 0) {
6919 return l_type;
6920 }
6921 fl->l_type = l_type;
213d3e9e
PM
6922 __get_user(fl->l_whence, &target_fl->l_whence);
6923 __get_user(fl->l_start, &target_fl->l_start);
6924 __get_user(fl->l_len, &target_fl->l_len);
6925 __get_user(fl->l_pid, &target_fl->l_pid);
6926 unlock_user_struct(target_fl, target_flock_addr, 0);
6927 return 0;
6928}
6929
7f254c5c 6930static inline abi_long copy_to_user_oabi_flock64(abi_ulong target_flock_addr,
213d3e9e
PM
6931 const struct flock64 *fl)
6932{
7f254c5c 6933 struct target_oabi_flock64 *target_fl;
213d3e9e
PM
6934 short l_type;
6935
6936 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6937 return -TARGET_EFAULT;
6938 }
6939
ae68ad9f 6940 l_type = host_to_target_flock(fl->l_type);
213d3e9e
PM
6941 __put_user(l_type, &target_fl->l_type);
6942 __put_user(fl->l_whence, &target_fl->l_whence);
6943 __put_user(fl->l_start, &target_fl->l_start);
6944 __put_user(fl->l_len, &target_fl->l_len);
6945 __put_user(fl->l_pid, &target_fl->l_pid);
6946 unlock_user_struct(target_fl, target_flock_addr, 1);
6947 return 0;
6948}
6949#endif
6950
6951static inline abi_long copy_from_user_flock64(struct flock64 *fl,
6952 abi_ulong target_flock_addr)
6953{
6954 struct target_flock64 *target_fl;
ae68ad9f 6955 int l_type;
213d3e9e
PM
6956
6957 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6958 return -TARGET_EFAULT;
6959 }
6960
6961 __get_user(l_type, &target_fl->l_type);
ae68ad9f
LV
6962 l_type = target_to_host_flock(l_type);
6963 if (l_type < 0) {
6964 return l_type;
6965 }
6966 fl->l_type = l_type;
213d3e9e
PM
6967 __get_user(fl->l_whence, &target_fl->l_whence);
6968 __get_user(fl->l_start, &target_fl->l_start);
6969 __get_user(fl->l_len, &target_fl->l_len);
6970 __get_user(fl->l_pid, &target_fl->l_pid);
6971 unlock_user_struct(target_fl, target_flock_addr, 0);
6972 return 0;
6973}
6974
6975static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr,
6976 const struct flock64 *fl)
6977{
6978 struct target_flock64 *target_fl;
6979 short l_type;
6980
6981 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6982 return -TARGET_EFAULT;
6983 }
6984
ae68ad9f 6985 l_type = host_to_target_flock(fl->l_type);
213d3e9e
PM
6986 __put_user(l_type, &target_fl->l_type);
6987 __put_user(fl->l_whence, &target_fl->l_whence);
6988 __put_user(fl->l_start, &target_fl->l_start);
6989 __put_user(fl->l_len, &target_fl->l_len);
6990 __put_user(fl->l_pid, &target_fl->l_pid);
6991 unlock_user_struct(target_fl, target_flock_addr, 1);
6992 return 0;
6993}
6994
6995static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
6996{
43f238d7 6997 struct flock64 fl64;
8d5d3004
AS
6998#ifdef F_GETOWN_EX
6999 struct f_owner_ex fox;
7000 struct target_f_owner_ex *target_fox;
7001#endif
992f48a0 7002 abi_long ret;
5f106811
APR
7003 int host_cmd = target_to_host_fcntl_cmd(cmd);
7004
7005 if (host_cmd == -TARGET_EINVAL)
7006 return host_cmd;
53a5960a 7007
7775e9ec
FB
7008 switch(cmd) {
7009 case TARGET_F_GETLK:
213d3e9e
PM
7010 ret = copy_from_user_flock(&fl64, arg);
7011 if (ret) {
7012 return ret;
7013 }
435da5e7 7014 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
7775e9ec 7015 if (ret == 0) {
213d3e9e 7016 ret = copy_to_user_flock(arg, &fl64);
7775e9ec
FB
7017 }
7018 break;
3b46e624 7019
7775e9ec
FB
7020 case TARGET_F_SETLK:
7021 case TARGET_F_SETLKW:
213d3e9e
PM
7022 ret = copy_from_user_flock(&fl64, arg);
7023 if (ret) {
7024 return ret;
7025 }
435da5e7 7026 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
7775e9ec 7027 break;
3b46e624 7028
7775e9ec 7029 case TARGET_F_GETLK64:
2d92c682 7030 case TARGET_F_OFD_GETLK:
213d3e9e
PM
7031 ret = copy_from_user_flock64(&fl64, arg);
7032 if (ret) {
7033 return ret;
7034 }
435da5e7 7035 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
43f238d7 7036 if (ret == 0) {
213d3e9e 7037 ret = copy_to_user_flock64(arg, &fl64);
43f238d7 7038 }
9ee1fa2c 7039 break;
7775e9ec
FB
7040 case TARGET_F_SETLK64:
7041 case TARGET_F_SETLKW64:
2d92c682
AS
7042 case TARGET_F_OFD_SETLK:
7043 case TARGET_F_OFD_SETLKW:
213d3e9e
PM
7044 ret = copy_from_user_flock64(&fl64, arg);
7045 if (ret) {
7046 return ret;
7047 }
435da5e7 7048 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
7775e9ec
FB
7049 break;
7050
5f106811 7051 case TARGET_F_GETFL:
435da5e7 7052 ret = get_errno(safe_fcntl(fd, host_cmd, arg));
9ee1fa2c
FB
7053 if (ret >= 0) {
7054 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
e0ddf8ea
HD
7055 /* tell 32-bit guests it uses largefile on 64-bit hosts: */
7056 if (O_LARGEFILE == 0 && HOST_LONG_BITS == 64) {
7057 ret |= TARGET_O_LARGEFILE;
7058 }
9ee1fa2c 7059 }
ffa65c3b
FB
7060 break;
7061
5f106811 7062 case TARGET_F_SETFL:
435da5e7
PM
7063 ret = get_errno(safe_fcntl(fd, host_cmd,
7064 target_to_host_bitmask(arg,
7065 fcntl_flags_tbl)));
5f106811
APR
7066 break;
7067
8d5d3004
AS
7068#ifdef F_GETOWN_EX
7069 case TARGET_F_GETOWN_EX:
435da5e7 7070 ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
8d5d3004
AS
7071 if (ret >= 0) {
7072 if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
7073 return -TARGET_EFAULT;
7074 target_fox->type = tswap32(fox.type);
7075 target_fox->pid = tswap32(fox.pid);
7076 unlock_user_struct(target_fox, arg, 1);
7077 }
7078 break;
7079#endif
7080
7081#ifdef F_SETOWN_EX
7082 case TARGET_F_SETOWN_EX:
7083 if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
7084 return -TARGET_EFAULT;
7085 fox.type = tswap32(target_fox->type);
7086 fox.pid = tswap32(target_fox->pid);
7087 unlock_user_struct(target_fox, arg, 0);
435da5e7 7088 ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
8d5d3004
AS
7089 break;
7090#endif
7091
5f106811 7092 case TARGET_F_SETSIG:
a7b4c9b1
TB
7093 ret = get_errno(safe_fcntl(fd, host_cmd, target_to_host_signal(arg)));
7094 break;
7095
5f106811 7096 case TARGET_F_GETSIG:
a7b4c9b1
TB
7097 ret = host_to_target_signal(get_errno(safe_fcntl(fd, host_cmd, arg)));
7098 break;
7099
7100 case TARGET_F_SETOWN:
7101 case TARGET_F_GETOWN:
7e22e546
UH
7102 case TARGET_F_SETLEASE:
7103 case TARGET_F_GETLEASE:
7e3b92ec
PM
7104 case TARGET_F_SETPIPE_SZ:
7105 case TARGET_F_GETPIPE_SZ:
2bb963ff
SCW
7106 case TARGET_F_ADD_SEALS:
7107 case TARGET_F_GET_SEALS:
435da5e7 7108 ret = get_errno(safe_fcntl(fd, host_cmd, arg));
ffa65c3b
FB
7109 break;
7110
7775e9ec 7111 default:
435da5e7 7112 ret = get_errno(safe_fcntl(fd, cmd, arg));
7775e9ec
FB
7113 break;
7114 }
7115 return ret;
7116}
7117
67867308 7118#ifdef USE_UID16
7775e9ec 7119
67867308
FB
7120static inline int high2lowuid(int uid)
7121{
7122 if (uid > 65535)
7123 return 65534;
7124 else
7125 return uid;
7126}
7127
7128static inline int high2lowgid(int gid)
7129{
7130 if (gid > 65535)
7131 return 65534;
7132 else
7133 return gid;
7134}
7135
7136static inline int low2highuid(int uid)
7137{
7138 if ((int16_t)uid == -1)
7139 return -1;
7140 else
7141 return uid;
7142}
7143
7144static inline int low2highgid(int gid)
7145{
7146 if ((int16_t)gid == -1)
7147 return -1;
7148 else
7149 return gid;
7150}
0c866a7e
RV
7151static inline int tswapid(int id)
7152{
7153 return tswap16(id);
7154}
76ca310a
PM
7155
7156#define put_user_id(x, gaddr) put_user_u16(x, gaddr)
7157
0c866a7e
RV
7158#else /* !USE_UID16 */
7159static inline int high2lowuid(int uid)
7160{
7161 return uid;
7162}
7163static inline int high2lowgid(int gid)
7164{
7165 return gid;
7166}
7167static inline int low2highuid(int uid)
7168{
7169 return uid;
7170}
7171static inline int low2highgid(int gid)
7172{
7173 return gid;
7174}
7175static inline int tswapid(int id)
7176{
7177 return tswap32(id);
7178}
76ca310a
PM
7179
7180#define put_user_id(x, gaddr) put_user_u32(x, gaddr)
7181
67867308 7182#endif /* USE_UID16 */
1b6b029e 7183
fd6f7798
PM
7184/* We must do direct syscalls for setting UID/GID, because we want to
7185 * implement the Linux system call semantics of "change only for this thread",
7186 * not the libc/POSIX semantics of "change for all threads in process".
7187 * (See http://ewontfix.com/17/ for more details.)
7188 * We use the 32-bit version of the syscalls if present; if it is not
7189 * then either the host architecture supports 32-bit UIDs natively with
7190 * the standard syscall, or the 16-bit UID is the best we can do.
7191 */
7192#ifdef __NR_setuid32
7193#define __NR_sys_setuid __NR_setuid32
7194#else
7195#define __NR_sys_setuid __NR_setuid
7196#endif
7197#ifdef __NR_setgid32
7198#define __NR_sys_setgid __NR_setgid32
7199#else
7200#define __NR_sys_setgid __NR_setgid
7201#endif
7202#ifdef __NR_setresuid32
7203#define __NR_sys_setresuid __NR_setresuid32
7204#else
7205#define __NR_sys_setresuid __NR_setresuid
7206#endif
7207#ifdef __NR_setresgid32
7208#define __NR_sys_setresgid __NR_setresgid32
7209#else
7210#define __NR_sys_setresgid __NR_setresgid
7211#endif
7212
7213_syscall1(int, sys_setuid, uid_t, uid)
7214_syscall1(int, sys_setgid, gid_t, gid)
7215_syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
7216_syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
7217
31e31b8a
FB
7218void syscall_init(void)
7219{
2ab83ea7
FB
7220 IOCTLEntry *ie;
7221 const argtype *arg_type;
7222 int size;
7223
8be656b8
AG
7224 thunk_init(STRUCT_MAX);
7225
001faf32 7226#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
5fafdf24 7227#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
31e31b8a
FB
7228#include "syscall_types.h"
7229#undef STRUCT
7230#undef STRUCT_SPECIAL
2ab83ea7
FB
7231
7232 /* we patch the ioctl size if necessary. We rely on the fact that
7233 no ioctl has all the bits at '1' in the size field */
7234 ie = ioctl_entries;
7235 while (ie->target_cmd != 0) {
7236 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
7237 TARGET_IOC_SIZEMASK) {
7238 arg_type = ie->arg_type;
7239 if (arg_type[0] != TYPE_PTR) {
5fafdf24 7240 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
2ab83ea7
FB
7241 ie->target_cmd);
7242 exit(1);
7243 }
7244 arg_type++;
7245 size = thunk_type_size(arg_type, 0);
5fafdf24 7246 ie->target_cmd = (ie->target_cmd &
2ab83ea7
FB
7247 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
7248 (size << TARGET_IOC_SIZESHIFT);
7249 }
b92c47c1 7250
2ab83ea7 7251 /* automatic consistency check if same arch */
872ea0c0
AZ
7252#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
7253 (defined(__x86_64__) && defined(TARGET_X86_64))
7254 if (unlikely(ie->target_cmd != ie->host_cmd)) {
7255 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
7256 ie->name, ie->target_cmd, ie->host_cmd);
2ab83ea7
FB
7257 }
7258#endif
7259 ie++;
7260 }
31e31b8a 7261}
c573ff67 7262
ce4defa0 7263#ifdef TARGET_NR_truncate64
a0939b89 7264static inline abi_long target_truncate64(CPUArchState *cpu_env, const char *arg1,
992f48a0
BS
7265 abi_long arg2,
7266 abi_long arg3,
7267 abi_long arg4)
ce4defa0 7268{
8bf8e9df 7269 if (regpairs_aligned(cpu_env, TARGET_NR_truncate64)) {
ce4defa0
PB
7270 arg2 = arg3;
7271 arg3 = arg4;
48e515d4 7272 }
ce4defa0
PB
7273 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
7274}
7275#endif
7276
7277#ifdef TARGET_NR_ftruncate64
a0939b89 7278static inline abi_long target_ftruncate64(CPUArchState *cpu_env, abi_long arg1,
992f48a0
BS
7279 abi_long arg2,
7280 abi_long arg3,
7281 abi_long arg4)
ce4defa0 7282{
8bf8e9df 7283 if (regpairs_aligned(cpu_env, TARGET_NR_ftruncate64)) {
ce4defa0
PB
7284 arg2 = arg3;
7285 arg3 = arg4;
48e515d4 7286 }
ce4defa0
PB
7287 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
7288}
7289#endif
7290
859e8a89
AF
7291#if defined(TARGET_NR_timer_settime) || \
7292 (defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD))
2c86c90f 7293static inline abi_long target_to_host_itimerspec(struct itimerspec *host_its,
f4f1e10a
ECL
7294 abi_ulong target_addr)
7295{
2c86c90f
FB
7296 if (target_to_host_timespec(&host_its->it_interval, target_addr +
7297 offsetof(struct target_itimerspec,
7298 it_interval)) ||
7299 target_to_host_timespec(&host_its->it_value, target_addr +
7300 offsetof(struct target_itimerspec,
7301 it_value))) {
f4f1e10a
ECL
7302 return -TARGET_EFAULT;
7303 }
7304
f4f1e10a
ECL
7305 return 0;
7306}
859e8a89 7307#endif
f4f1e10a 7308
828cb3a1
FB
7309#if defined(TARGET_NR_timer_settime64) || \
7310 (defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD))
7311static inline abi_long target_to_host_itimerspec64(struct itimerspec *host_its,
7312 abi_ulong target_addr)
7313{
7314 if (target_to_host_timespec64(&host_its->it_interval, target_addr +
7315 offsetof(struct target__kernel_itimerspec,
7316 it_interval)) ||
7317 target_to_host_timespec64(&host_its->it_value, target_addr +
7318 offsetof(struct target__kernel_itimerspec,
7319 it_value))) {
7320 return -TARGET_EFAULT;
7321 }
7322
7323 return 0;
7324}
7325#endif
7326
859e8a89
AF
7327#if ((defined(TARGET_NR_timerfd_gettime) || \
7328 defined(TARGET_NR_timerfd_settime)) && defined(CONFIG_TIMERFD)) || \
2c86c90f 7329 defined(TARGET_NR_timer_gettime) || defined(TARGET_NR_timer_settime)
f4f1e10a 7330static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
2c86c90f
FB
7331 struct itimerspec *host_its)
7332{
7333 if (host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
7334 it_interval),
7335 &host_its->it_interval) ||
7336 host_to_target_timespec(target_addr + offsetof(struct target_itimerspec,
7337 it_value),
7338 &host_its->it_value)) {
f4f1e10a
ECL
7339 return -TARGET_EFAULT;
7340 }
f4f1e10a
ECL
7341 return 0;
7342}
859e8a89 7343#endif
f4f1e10a 7344
828cb3a1
FB
7345#if ((defined(TARGET_NR_timerfd_gettime64) || \
7346 defined(TARGET_NR_timerfd_settime64)) && defined(CONFIG_TIMERFD)) || \
7347 defined(TARGET_NR_timer_gettime64) || defined(TARGET_NR_timer_settime64)
7348static inline abi_long host_to_target_itimerspec64(abi_ulong target_addr,
7349 struct itimerspec *host_its)
7350{
7351 if (host_to_target_timespec64(target_addr +
7352 offsetof(struct target__kernel_itimerspec,
7353 it_interval),
7354 &host_its->it_interval) ||
7355 host_to_target_timespec64(target_addr +
7356 offsetof(struct target__kernel_itimerspec,
7357 it_value),
7358 &host_its->it_value)) {
7359 return -TARGET_EFAULT;
7360 }
7361 return 0;
7362}
7363#endif
7364
859e8a89
AF
7365#if defined(TARGET_NR_adjtimex) || \
7366 (defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME))
19f59bce
AM
7367static inline abi_long target_to_host_timex(struct timex *host_tx,
7368 abi_long target_addr)
7369{
7370 struct target_timex *target_tx;
7371
7372 if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
7373 return -TARGET_EFAULT;
7374 }
7375
7376 __get_user(host_tx->modes, &target_tx->modes);
7377 __get_user(host_tx->offset, &target_tx->offset);
7378 __get_user(host_tx->freq, &target_tx->freq);
7379 __get_user(host_tx->maxerror, &target_tx->maxerror);
7380 __get_user(host_tx->esterror, &target_tx->esterror);
7381 __get_user(host_tx->status, &target_tx->status);
7382 __get_user(host_tx->constant, &target_tx->constant);
7383 __get_user(host_tx->precision, &target_tx->precision);
7384 __get_user(host_tx->tolerance, &target_tx->tolerance);
7385 __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
7386 __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
7387 __get_user(host_tx->tick, &target_tx->tick);
7388 __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7389 __get_user(host_tx->jitter, &target_tx->jitter);
7390 __get_user(host_tx->shift, &target_tx->shift);
7391 __get_user(host_tx->stabil, &target_tx->stabil);
7392 __get_user(host_tx->jitcnt, &target_tx->jitcnt);
7393 __get_user(host_tx->calcnt, &target_tx->calcnt);
7394 __get_user(host_tx->errcnt, &target_tx->errcnt);
7395 __get_user(host_tx->stbcnt, &target_tx->stbcnt);
7396 __get_user(host_tx->tai, &target_tx->tai);
7397
7398 unlock_user_struct(target_tx, target_addr, 0);
7399 return 0;
7400}
7401
7402static inline abi_long host_to_target_timex(abi_long target_addr,
7403 struct timex *host_tx)
7404{
7405 struct target_timex *target_tx;
7406
7407 if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
7408 return -TARGET_EFAULT;
7409 }
7410
7411 __put_user(host_tx->modes, &target_tx->modes);
7412 __put_user(host_tx->offset, &target_tx->offset);
7413 __put_user(host_tx->freq, &target_tx->freq);
7414 __put_user(host_tx->maxerror, &target_tx->maxerror);
7415 __put_user(host_tx->esterror, &target_tx->esterror);
7416 __put_user(host_tx->status, &target_tx->status);
7417 __put_user(host_tx->constant, &target_tx->constant);
7418 __put_user(host_tx->precision, &target_tx->precision);
7419 __put_user(host_tx->tolerance, &target_tx->tolerance);
7420 __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
7421 __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
7422 __put_user(host_tx->tick, &target_tx->tick);
7423 __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7424 __put_user(host_tx->jitter, &target_tx->jitter);
7425 __put_user(host_tx->shift, &target_tx->shift);
7426 __put_user(host_tx->stabil, &target_tx->stabil);
7427 __put_user(host_tx->jitcnt, &target_tx->jitcnt);
7428 __put_user(host_tx->calcnt, &target_tx->calcnt);
7429 __put_user(host_tx->errcnt, &target_tx->errcnt);
7430 __put_user(host_tx->stbcnt, &target_tx->stbcnt);
7431 __put_user(host_tx->tai, &target_tx->tai);
7432
7433 unlock_user_struct(target_tx, target_addr, 1);
7434 return 0;
7435}
859e8a89 7436#endif
19f59bce 7437
6ac03b2c
FB
7438
7439#if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
7440static inline abi_long target_to_host_timex64(struct timex *host_tx,
7441 abi_long target_addr)
7442{
7443 struct target__kernel_timex *target_tx;
7444
7445 if (copy_from_user_timeval64(&host_tx->time, target_addr +
7446 offsetof(struct target__kernel_timex,
7447 time))) {
7448 return -TARGET_EFAULT;
7449 }
7450
7451 if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
7452 return -TARGET_EFAULT;
7453 }
7454
7455 __get_user(host_tx->modes, &target_tx->modes);
7456 __get_user(host_tx->offset, &target_tx->offset);
7457 __get_user(host_tx->freq, &target_tx->freq);
7458 __get_user(host_tx->maxerror, &target_tx->maxerror);
7459 __get_user(host_tx->esterror, &target_tx->esterror);
7460 __get_user(host_tx->status, &target_tx->status);
7461 __get_user(host_tx->constant, &target_tx->constant);
7462 __get_user(host_tx->precision, &target_tx->precision);
7463 __get_user(host_tx->tolerance, &target_tx->tolerance);
7464 __get_user(host_tx->tick, &target_tx->tick);
7465 __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7466 __get_user(host_tx->jitter, &target_tx->jitter);
7467 __get_user(host_tx->shift, &target_tx->shift);
7468 __get_user(host_tx->stabil, &target_tx->stabil);
7469 __get_user(host_tx->jitcnt, &target_tx->jitcnt);
7470 __get_user(host_tx->calcnt, &target_tx->calcnt);
7471 __get_user(host_tx->errcnt, &target_tx->errcnt);
7472 __get_user(host_tx->stbcnt, &target_tx->stbcnt);
7473 __get_user(host_tx->tai, &target_tx->tai);
7474
7475 unlock_user_struct(target_tx, target_addr, 0);
7476 return 0;
7477}
7478
7479static inline abi_long host_to_target_timex64(abi_long target_addr,
7480 struct timex *host_tx)
7481{
7482 struct target__kernel_timex *target_tx;
7483
7484 if (copy_to_user_timeval64(target_addr +
7485 offsetof(struct target__kernel_timex, time),
7486 &host_tx->time)) {
7487 return -TARGET_EFAULT;
7488 }
7489
7490 if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
7491 return -TARGET_EFAULT;
7492 }
7493
7494 __put_user(host_tx->modes, &target_tx->modes);
7495 __put_user(host_tx->offset, &target_tx->offset);
7496 __put_user(host_tx->freq, &target_tx->freq);
7497 __put_user(host_tx->maxerror, &target_tx->maxerror);
7498 __put_user(host_tx->esterror, &target_tx->esterror);
7499 __put_user(host_tx->status, &target_tx->status);
7500 __put_user(host_tx->constant, &target_tx->constant);
7501 __put_user(host_tx->precision, &target_tx->precision);
7502 __put_user(host_tx->tolerance, &target_tx->tolerance);
7503 __put_user(host_tx->tick, &target_tx->tick);
7504 __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7505 __put_user(host_tx->jitter, &target_tx->jitter);
7506 __put_user(host_tx->shift, &target_tx->shift);
7507 __put_user(host_tx->stabil, &target_tx->stabil);
7508 __put_user(host_tx->jitcnt, &target_tx->jitcnt);
7509 __put_user(host_tx->calcnt, &target_tx->calcnt);
7510 __put_user(host_tx->errcnt, &target_tx->errcnt);
7511 __put_user(host_tx->stbcnt, &target_tx->stbcnt);
7512 __put_user(host_tx->tai, &target_tx->tai);
7513
7514 unlock_user_struct(target_tx, target_addr, 1);
7515 return 0;
7516}
7517#endif
7518
96ff758c
MF
7519#ifndef HAVE_SIGEV_NOTIFY_THREAD_ID
7520#define sigev_notify_thread_id _sigev_un._tid
7521#endif
7522
c065976f
PM
7523static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
7524 abi_ulong target_addr)
7525{
7526 struct target_sigevent *target_sevp;
7527
7528 if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
7529 return -TARGET_EFAULT;
7530 }
7531
7532 /* This union is awkward on 64 bit systems because it has a 32 bit
7533 * integer and a pointer in it; we follow the conversion approach
7534 * used for handling sigval types in signal.c so the guest should get
7535 * the correct value back even if we did a 64 bit byteswap and it's
7536 * using the 32 bit integer.
7537 */
7538 host_sevp->sigev_value.sival_ptr =
7539 (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
7540 host_sevp->sigev_signo =
7541 target_to_host_signal(tswap32(target_sevp->sigev_signo));
7542 host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
96ff758c 7543 host_sevp->sigev_notify_thread_id = tswap32(target_sevp->_sigev_un._tid);
c065976f
PM
7544
7545 unlock_user_struct(target_sevp, target_addr, 1);
7546 return 0;
7547}
7548
6f6a4032
TM
7549#if defined(TARGET_NR_mlockall)
7550static inline int target_to_host_mlockall_arg(int arg)
7551{
7552 int result = 0;
7553
02e5d7d7 7554 if (arg & TARGET_MCL_CURRENT) {
6f6a4032
TM
7555 result |= MCL_CURRENT;
7556 }
02e5d7d7 7557 if (arg & TARGET_MCL_FUTURE) {
6f6a4032
TM
7558 result |= MCL_FUTURE;
7559 }
02e5d7d7
FB
7560#ifdef MCL_ONFAULT
7561 if (arg & TARGET_MCL_ONFAULT) {
7562 result |= MCL_ONFAULT;
7563 }
7564#endif
7565
6f6a4032
TM
7566 return result;
7567}
7568#endif
7569
fe080593
HD
7570static inline int target_to_host_msync_arg(abi_long arg)
7571{
7572 return ((arg & TARGET_MS_ASYNC) ? MS_ASYNC : 0) |
7573 ((arg & TARGET_MS_INVALIDATE) ? MS_INVALIDATE : 0) |
7574 ((arg & TARGET_MS_SYNC) ? MS_SYNC : 0) |
7575 (arg & ~(TARGET_MS_ASYNC | TARGET_MS_INVALIDATE | TARGET_MS_SYNC));
7576}
7577
4f7f8924
AR
7578#if (defined(TARGET_NR_stat64) || defined(TARGET_NR_lstat64) || \
7579 defined(TARGET_NR_fstat64) || defined(TARGET_NR_fstatat64) || \
7580 defined(TARGET_NR_newfstatat))
a0939b89 7581static inline abi_long host_to_target_stat64(CPUArchState *cpu_env,
6a24a778
AZ
7582 abi_ulong target_addr,
7583 struct stat *host_st)
7584{
09701199 7585#if defined(TARGET_ARM) && defined(TARGET_ABI32)
0effdc29 7586 if (cpu_env->eabi) {
6a24a778
AZ
7587 struct target_eabi_stat64 *target_st;
7588
7589 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
7590 return -TARGET_EFAULT;
7591 memset(target_st, 0, sizeof(struct target_eabi_stat64));
7592 __put_user(host_st->st_dev, &target_st->st_dev);
7593 __put_user(host_st->st_ino, &target_st->st_ino);
7594#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7595 __put_user(host_st->st_ino, &target_st->__st_ino);
7596#endif
7597 __put_user(host_st->st_mode, &target_st->st_mode);
7598 __put_user(host_st->st_nlink, &target_st->st_nlink);
7599 __put_user(host_st->st_uid, &target_st->st_uid);
7600 __put_user(host_st->st_gid, &target_st->st_gid);
7601 __put_user(host_st->st_rdev, &target_st->st_rdev);
7602 __put_user(host_st->st_size, &target_st->st_size);
7603 __put_user(host_st->st_blksize, &target_st->st_blksize);
7604 __put_user(host_st->st_blocks, &target_st->st_blocks);
7605 __put_user(host_st->st_atime, &target_st->target_st_atime);
7606 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
7607 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
febf6fad 7608#ifdef HAVE_STRUCT_STAT_ST_ATIM
5f992db6
CYT
7609 __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
7610 __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
7611 __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
7612#endif
6a24a778
AZ
7613 unlock_user_struct(target_st, target_addr, 1);
7614 } else
7615#endif
7616 {
20d155bc 7617#if defined(TARGET_HAS_STRUCT_STAT64)
6a24a778 7618 struct target_stat64 *target_st;
20d155bc
SW
7619#else
7620 struct target_stat *target_st;
9d33b76b 7621#endif
6a24a778
AZ
7622
7623 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
7624 return -TARGET_EFAULT;
9d33b76b 7625 memset(target_st, 0, sizeof(*target_st));
6a24a778
AZ
7626 __put_user(host_st->st_dev, &target_st->st_dev);
7627 __put_user(host_st->st_ino, &target_st->st_ino);
7628#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7629 __put_user(host_st->st_ino, &target_st->__st_ino);
7630#endif
7631 __put_user(host_st->st_mode, &target_st->st_mode);
7632 __put_user(host_st->st_nlink, &target_st->st_nlink);
7633 __put_user(host_st->st_uid, &target_st->st_uid);
7634 __put_user(host_st->st_gid, &target_st->st_gid);
7635 __put_user(host_st->st_rdev, &target_st->st_rdev);
7636 /* XXX: better use of kernel struct */
7637 __put_user(host_st->st_size, &target_st->st_size);
7638 __put_user(host_st->st_blksize, &target_st->st_blksize);
7639 __put_user(host_st->st_blocks, &target_st->st_blocks);
7640 __put_user(host_st->st_atime, &target_st->target_st_atime);
7641 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
7642 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
febf6fad 7643#ifdef HAVE_STRUCT_STAT_ST_ATIM
5f992db6
CYT
7644 __put_user(host_st->st_atim.tv_nsec, &target_st->target_st_atime_nsec);
7645 __put_user(host_st->st_mtim.tv_nsec, &target_st->target_st_mtime_nsec);
7646 __put_user(host_st->st_ctim.tv_nsec, &target_st->target_st_ctime_nsec);
7647#endif
6a24a778
AZ
7648 unlock_user_struct(target_st, target_addr, 1);
7649 }
7650
7651 return 0;
7652}
4f7f8924 7653#endif
6a24a778 7654
efa92184
AR
7655#if defined(TARGET_NR_statx) && defined(__NR_statx)
7656static inline abi_long host_to_target_statx(struct target_statx *host_stx,
7657 abi_ulong target_addr)
7658{
7659 struct target_statx *target_stx;
7660
7661 if (!lock_user_struct(VERIFY_WRITE, target_stx, target_addr, 0)) {
7662 return -TARGET_EFAULT;
7663 }
7664 memset(target_stx, 0, sizeof(*target_stx));
7665
7666 __put_user(host_stx->stx_mask, &target_stx->stx_mask);
7667 __put_user(host_stx->stx_blksize, &target_stx->stx_blksize);
7668 __put_user(host_stx->stx_attributes, &target_stx->stx_attributes);
7669 __put_user(host_stx->stx_nlink, &target_stx->stx_nlink);
7670 __put_user(host_stx->stx_uid, &target_stx->stx_uid);
7671 __put_user(host_stx->stx_gid, &target_stx->stx_gid);
7672 __put_user(host_stx->stx_mode, &target_stx->stx_mode);
7673 __put_user(host_stx->stx_ino, &target_stx->stx_ino);
7674 __put_user(host_stx->stx_size, &target_stx->stx_size);
7675 __put_user(host_stx->stx_blocks, &target_stx->stx_blocks);
7676 __put_user(host_stx->stx_attributes_mask, &target_stx->stx_attributes_mask);
7677 __put_user(host_stx->stx_atime.tv_sec, &target_stx->stx_atime.tv_sec);
7678 __put_user(host_stx->stx_atime.tv_nsec, &target_stx->stx_atime.tv_nsec);
d1e26707
AC
7679 __put_user(host_stx->stx_btime.tv_sec, &target_stx->stx_btime.tv_sec);
7680 __put_user(host_stx->stx_btime.tv_nsec, &target_stx->stx_btime.tv_nsec);
7681 __put_user(host_stx->stx_ctime.tv_sec, &target_stx->stx_ctime.tv_sec);
7682 __put_user(host_stx->stx_ctime.tv_nsec, &target_stx->stx_ctime.tv_nsec);
7683 __put_user(host_stx->stx_mtime.tv_sec, &target_stx->stx_mtime.tv_sec);
7684 __put_user(host_stx->stx_mtime.tv_nsec, &target_stx->stx_mtime.tv_nsec);
efa92184
AR
7685 __put_user(host_stx->stx_rdev_major, &target_stx->stx_rdev_major);
7686 __put_user(host_stx->stx_rdev_minor, &target_stx->stx_rdev_minor);
7687 __put_user(host_stx->stx_dev_major, &target_stx->stx_dev_major);
7688 __put_user(host_stx->stx_dev_minor, &target_stx->stx_dev_minor);
7689
7690 unlock_user_struct(target_stx, target_addr, 1);
7691
7692 return 0;
7693}
7694#endif
7695
14690296
AF
7696static int do_sys_futex(int *uaddr, int op, int val,
7697 const struct timespec *timeout, int *uaddr2,
7698 int val3)
7699{
7700#if HOST_LONG_BITS == 64
7701#if defined(__NR_futex)
7702 /* always a 64-bit time_t, it doesn't define _time64 version */
7703 return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
7704
7705#endif
7706#else /* HOST_LONG_BITS == 64 */
7707#if defined(__NR_futex_time64)
7708 if (sizeof(timeout->tv_sec) == 8) {
7709 /* _time64 function on 32bit arch */
7710 return sys_futex_time64(uaddr, op, val, timeout, uaddr2, val3);
7711 }
7712#endif
7713#if defined(__NR_futex)
7714 /* old function on 32bit arch */
7715 return sys_futex(uaddr, op, val, timeout, uaddr2, val3);
7716#endif
7717#endif /* HOST_LONG_BITS == 64 */
7718 g_assert_not_reached();
7719}
7720
7721static int do_safe_futex(int *uaddr, int op, int val,
7722 const struct timespec *timeout, int *uaddr2,
7723 int val3)
7724{
7725#if HOST_LONG_BITS == 64
7726#if defined(__NR_futex)
7727 /* always a 64-bit time_t, it doesn't define _time64 version */
7728 return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
7729#endif
7730#else /* HOST_LONG_BITS == 64 */
7731#if defined(__NR_futex_time64)
7732 if (sizeof(timeout->tv_sec) == 8) {
7733 /* _time64 function on 32bit arch */
7734 return get_errno(safe_futex_time64(uaddr, op, val, timeout, uaddr2,
7735 val3));
7736 }
7737#endif
7738#if defined(__NR_futex)
7739 /* old function on 32bit arch */
7740 return get_errno(safe_futex(uaddr, op, val, timeout, uaddr2, val3));
7741#endif
7742#endif /* HOST_LONG_BITS == 64 */
7743 return -TARGET_ENOSYS;
7744}
efa92184 7745
bd0c5661
PB
7746/* ??? Using host futex calls even when target atomic operations
7747 are not really atomic probably breaks things. However implementing
7748 futexes locally would make futexes shared between multiple processes
7749 tricky. However they're probably useless because guest atomic
7750 operations won't work either. */
0fbc0f8d
RH
7751#if defined(TARGET_NR_futex) || defined(TARGET_NR_futex_time64)
7752static int do_futex(CPUState *cpu, bool time64, target_ulong uaddr,
7753 int op, int val, target_ulong timeout,
7754 target_ulong uaddr2, int val3)
bd0c5661 7755{
57b9ccd4
RH
7756 struct timespec ts, *pts = NULL;
7757 void *haddr2 = NULL;
a16aae0c 7758 int base_op;
bd0c5661 7759
57b9ccd4 7760 /* We assume FUTEX_* constants are the same on both host and target. */
a29ccd63 7761#ifdef FUTEX_CMD_MASK
a16aae0c 7762 base_op = op & FUTEX_CMD_MASK;
a29ccd63 7763#else
a16aae0c 7764 base_op = op;
a29ccd63 7765#endif
a16aae0c 7766 switch (base_op) {
bd0c5661 7767 case FUTEX_WAIT:
cce246e0 7768 case FUTEX_WAIT_BITSET:
57b9ccd4
RH
7769 val = tswap32(val);
7770 break;
c72a90df
RH
7771 case FUTEX_WAIT_REQUEUE_PI:
7772 val = tswap32(val);
7773 haddr2 = g2h(cpu, uaddr2);
7774 break;
7775 case FUTEX_LOCK_PI:
7776 case FUTEX_LOCK_PI2:
7777 break;
bd0c5661 7778 case FUTEX_WAKE:
a6180f8a 7779 case FUTEX_WAKE_BITSET:
c72a90df
RH
7780 case FUTEX_TRYLOCK_PI:
7781 case FUTEX_UNLOCK_PI:
57b9ccd4
RH
7782 timeout = 0;
7783 break;
bd0c5661 7784 case FUTEX_FD:
0f946731 7785 val = target_to_host_signal(val);
57b9ccd4
RH
7786 timeout = 0;
7787 break;
bd0c5661 7788 case FUTEX_CMP_REQUEUE:
c72a90df 7789 case FUTEX_CMP_REQUEUE_PI:
57b9ccd4
RH
7790 val3 = tswap32(val3);
7791 /* fall through */
7792 case FUTEX_REQUEUE:
a16aae0c 7793 case FUTEX_WAKE_OP:
57b9ccd4
RH
7794 /*
7795 * For these, the 4th argument is not TIMEOUT, but VAL2.
7796 * But the prototype of do_safe_futex takes a pointer, so
7797 * insert casts to satisfy the compiler. We do not need
7798 * to tswap VAL2 since it's not compared to guest memory.
7799 */
7800 pts = (struct timespec *)(uintptr_t)timeout;
7801 timeout = 0;
7802 haddr2 = g2h(cpu, uaddr2);
7803 break;
bd0c5661
PB
7804 default:
7805 return -TARGET_ENOSYS;
7806 }
57b9ccd4
RH
7807 if (timeout) {
7808 pts = &ts;
7809 if (time64
7810 ? target_to_host_timespec64(pts, timeout)
7811 : target_to_host_timespec(pts, timeout)) {
7812 return -TARGET_EFAULT;
7813 }
7814 }
7815 return do_safe_futex(g2h(cpu, uaddr), op, val, pts, haddr2, val3);
bd0c5661 7816}
859e8a89 7817#endif
14690296 7818
0f0426f3
LV
7819#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7820static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
7821 abi_long handle, abi_long mount_id,
7822 abi_long flags)
7823{
7824 struct file_handle *target_fh;
7825 struct file_handle *fh;
7826 int mid = 0;
7827 abi_long ret;
7828 char *name;
7829 unsigned int size, total_size;
7830
7831 if (get_user_s32(size, handle)) {
7832 return -TARGET_EFAULT;
7833 }
7834
7835 name = lock_user_string(pathname);
7836 if (!name) {
7837 return -TARGET_EFAULT;
7838 }
7839
7840 total_size = sizeof(struct file_handle) + size;
7841 target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
7842 if (!target_fh) {
7843 unlock_user(name, pathname, 0);
7844 return -TARGET_EFAULT;
7845 }
7846
7847 fh = g_malloc0(total_size);
7848 fh->handle_bytes = size;
7849
7850 ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
7851 unlock_user(name, pathname, 0);
7852
7853 /* man name_to_handle_at(2):
7854 * Other than the use of the handle_bytes field, the caller should treat
7855 * the file_handle structure as an opaque data type
7856 */
7857
7858 memcpy(target_fh, fh, total_size);
7859 target_fh->handle_bytes = tswap32(fh->handle_bytes);
7860 target_fh->handle_type = tswap32(fh->handle_type);
7861 g_free(fh);
7862 unlock_user(target_fh, handle, total_size);
7863
7864 if (put_user_s32(mid, mount_id)) {
7865 return -TARGET_EFAULT;
7866 }
7867
7868 return ret;
7869
7870}
7871#endif
7872
7873#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7874static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
7875 abi_long flags)
7876{
7877 struct file_handle *target_fh;
7878 struct file_handle *fh;
7879 unsigned int size, total_size;
7880 abi_long ret;
7881
7882 if (get_user_s32(size, handle)) {
7883 return -TARGET_EFAULT;
7884 }
7885
7886 total_size = sizeof(struct file_handle) + size;
7887 target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
7888 if (!target_fh) {
7889 return -TARGET_EFAULT;
7890 }
7891
e9d49d51 7892 fh = g_memdup(target_fh, total_size);
0f0426f3
LV
7893 fh->handle_bytes = size;
7894 fh->handle_type = tswap32(target_fh->handle_type);
7895
7896 ret = get_errno(open_by_handle_at(mount_fd, fh,
7897 target_to_host_bitmask(flags, fcntl_flags_tbl)));
7898
7899 g_free(fh);
7900
7901 unlock_user(target_fh, handle, total_size);
7902
7903 return ret;
7904}
7905#endif
bd0c5661 7906
e36800c9
LV
7907#if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
7908
e36800c9
LV
7909static abi_long do_signalfd4(int fd, abi_long mask, int flags)
7910{
7911 int host_flags;
7912 target_sigset_t *target_mask;
7913 sigset_t host_mask;
7914 abi_long ret;
7915
78721301 7916 if (flags & ~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC)) {
e36800c9
LV
7917 return -TARGET_EINVAL;
7918 }
7919 if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
7920 return -TARGET_EFAULT;
7921 }
7922
7923 target_to_host_sigset(&host_mask, target_mask);
7924
7925 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
7926
7927 ret = get_errno(signalfd(fd, &host_mask, host_flags));
7928 if (ret >= 0) {
7929 fd_trans_register(ret, &target_signalfd_trans);
7930 }
7931
7932 unlock_user_struct(target_mask, mask, 0);
7933
7934 return ret;
7935}
7936#endif
7937
1d9d8b55
PB
7938/* Map host to target signal numbers for the wait family of syscalls.
7939 Assume all other status bits are the same. */
a05c6409 7940int host_to_target_waitstatus(int status)
1d9d8b55
PB
7941{
7942 if (WIFSIGNALED(status)) {
7943 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
7944 }
7945 if (WIFSTOPPED(status)) {
7946 return (host_to_target_signal(WSTOPSIG(status)) << 8)
7947 | (status & 0xff);
7948 }
7949 return status;
7950}
7951
a0939b89 7952static int open_self_cmdline(CPUArchState *cpu_env, int fd)
76b94245 7953{
0effdc29 7954 CPUState *cpu = env_cpu(cpu_env);
e4e5cb4a 7955 struct linux_binprm *bprm = get_task_state(cpu)->bprm;
58de8b96 7956 int i;
76b94245 7957
58de8b96
AS
7958 for (i = 0; i < bprm->argc; i++) {
7959 size_t len = strlen(bprm->argv[i]) + 1;
76b94245 7960
58de8b96 7961 if (write(fd, bprm->argv[i], len) != len) {
76b94245 7962 return -1;
76b94245
WVS
7963 }
7964 }
7965
58de8b96 7966 return 0;
76b94245
WVS
7967}
7968
7b7a3366
RH
7969struct open_self_maps_data {
7970 TaskState *ts;
7971 IntervalTreeRoot *host_maps;
7972 int fd;
7973 bool smaps;
7974};
7975
7976/*
7977 * Subroutine to output one line of /proc/self/maps,
7978 * or one region of /proc/self/smaps.
7979 */
7980
7981#ifdef TARGET_HPPA
7982# define test_stack(S, E, L) (E == L)
7983#else
7984# define test_stack(S, E, L) (S == L)
7985#endif
7986
7987static void open_self_maps_4(const struct open_self_maps_data *d,
7988 const MapInfo *mi, abi_ptr start,
7989 abi_ptr end, unsigned flags)
77ae5761 7990{
7b7a3366
RH
7991 const struct image_info *info = d->ts->info;
7992 const char *path = mi->path;
7993 uint64_t offset;
7994 int fd = d->fd;
7995 int count;
7996
7997 if (test_stack(start, end, info->stack_limit)) {
7998 path = "[stack]";
92d2a03f
RH
7999 } else if (start == info->brk) {
8000 path = "[heap]";
5d94c2ff
RH
8001 } else if (start == info->vdso) {
8002 path = "[vdso]";
4ef1f559
RH
8003#ifdef TARGET_X86_64
8004 } else if (start == TARGET_VSYSCALL_PAGE) {
8005 path = "[vsyscall]";
8006#endif
7b7a3366 8007 }
77ae5761 8008
7b7a3366
RH
8009 /* Except null device (MAP_ANON), adjust offset for this fragment. */
8010 offset = mi->offset;
8011 if (mi->dev) {
8012 uintptr_t hstart = (uintptr_t)g2h_untagged(start);
8013 offset += hstart - mi->itree.start;
8014 }
8015
8016 count = dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr
8017 " %c%c%c%c %08" PRIx64 " %02x:%02x %"PRId64,
8018 start, end,
8019 (flags & PAGE_READ) ? 'r' : '-',
8020 (flags & PAGE_WRITE_ORG) ? 'w' : '-',
8021 (flags & PAGE_EXEC) ? 'x' : '-',
8022 mi->is_priv ? 'p' : 's',
8023 offset, major(mi->dev), minor(mi->dev),
8024 (uint64_t)mi->inode);
8025 if (path) {
8026 dprintf(fd, "%*s%s\n", 73 - count, "", path);
8027 } else {
8028 dprintf(fd, "\n");
8029 }
8030
8031 if (d->smaps) {
8032 unsigned long size = end - start;
8033 unsigned long page_size_kb = TARGET_PAGE_SIZE >> 10;
8034 unsigned long size_kb = size >> 10;
77ae5761 8035
7b7a3366 8036 dprintf(fd, "Size: %lu kB\n"
77ae5761
IL
8037 "KernelPageSize: %lu kB\n"
8038 "MMUPageSize: %lu kB\n"
8039 "Rss: 0 kB\n"
8040 "Pss: 0 kB\n"
8041 "Pss_Dirty: 0 kB\n"
8042 "Shared_Clean: 0 kB\n"
8043 "Shared_Dirty: 0 kB\n"
8044 "Private_Clean: 0 kB\n"
8045 "Private_Dirty: 0 kB\n"
8046 "Referenced: 0 kB\n"
6467d9eb 8047 "Anonymous: %lu kB\n"
77ae5761
IL
8048 "LazyFree: 0 kB\n"
8049 "AnonHugePages: 0 kB\n"
8050 "ShmemPmdMapped: 0 kB\n"
8051 "FilePmdMapped: 0 kB\n"
8052 "Shared_Hugetlb: 0 kB\n"
8053 "Private_Hugetlb: 0 kB\n"
8054 "Swap: 0 kB\n"
8055 "SwapPss: 0 kB\n"
8056 "Locked: 0 kB\n"
7b7a3366
RH
8057 "THPeligible: 0\n"
8058 "VmFlags:%s%s%s%s%s%s%s%s\n",
8059 size_kb, page_size_kb, page_size_kb,
6467d9eb 8060 (flags & PAGE_ANON ? size_kb : 0),
7b7a3366
RH
8061 (flags & PAGE_READ) ? " rd" : "",
8062 (flags & PAGE_WRITE_ORG) ? " wr" : "",
8063 (flags & PAGE_EXEC) ? " ex" : "",
8064 mi->is_priv ? "" : " sh",
8065 (flags & PAGE_READ) ? " mr" : "",
8066 (flags & PAGE_WRITE_ORG) ? " mw" : "",
8067 (flags & PAGE_EXEC) ? " me" : "",
8068 mi->is_priv ? "" : " ms");
8069 }
77ae5761
IL
8070}
8071
7b7a3366
RH
8072/*
8073 * Callback for walk_memory_regions, when read_self_maps() fails.
8074 * Proceed without the benefit of host /proc/self/maps cross-check.
8075 */
8076static int open_self_maps_3(void *opaque, target_ulong guest_start,
8077 target_ulong guest_end, unsigned long flags)
36c08d49 8078{
7b7a3366 8079 static const MapInfo mi = { .is_priv = true };
1a49ef2a 8080
7b7a3366
RH
8081 open_self_maps_4(opaque, &mi, guest_start, guest_end, flags);
8082 return 0;
8083}
1a49ef2a 8084
7b7a3366
RH
8085/*
8086 * Callback for walk_memory_regions, when read_self_maps() succeeds.
8087 */
8088static int open_self_maps_2(void *opaque, target_ulong guest_start,
8089 target_ulong guest_end, unsigned long flags)
8090{
8091 const struct open_self_maps_data *d = opaque;
8092 uintptr_t host_start = (uintptr_t)g2h_untagged(guest_start);
8093 uintptr_t host_last = (uintptr_t)g2h_untagged(guest_end - 1);
01ef6b9e 8094
4ef1f559
RH
8095#ifdef TARGET_X86_64
8096 /*
8097 * Because of the extremely high position of the page within the guest
8098 * virtual address space, this is not backed by host memory at all.
8099 * Therefore the loop below would fail. This is the only instance
8100 * of not having host backing memory.
8101 */
8102 if (guest_start == TARGET_VSYSCALL_PAGE) {
8103 return open_self_maps_3(opaque, guest_start, guest_end, flags);
8104 }
8105#endif
8106
7b7a3366
RH
8107 while (1) {
8108 IntervalTreeNode *n =
8109 interval_tree_iter_first(d->host_maps, host_start, host_start);
8110 MapInfo *mi = container_of(n, MapInfo, itree);
8111 uintptr_t this_hlast = MIN(host_last, n->last);
8112 target_ulong this_gend = h2g(this_hlast) + 1;
01ef6b9e 8113
7b7a3366 8114 open_self_maps_4(d, mi, guest_start, this_gend, flags);
01ef6b9e 8115
7b7a3366
RH
8116 if (this_hlast == host_last) {
8117 return 0;
1a49ef2a 8118 }
7b7a3366
RH
8119 host_start = this_hlast + 1;
8120 guest_start = h2g(host_start);
1a49ef2a 8121 }
7b7a3366 8122}
1a49ef2a 8123
7b7a3366
RH
8124static int open_self_maps_1(CPUArchState *env, int fd, bool smaps)
8125{
8126 struct open_self_maps_data d = {
59272469 8127 .ts = get_task_state(env_cpu(env)),
7b7a3366
RH
8128 .host_maps = read_self_maps(),
8129 .fd = fd,
8130 .smaps = smaps
8131 };
01ef6b9e 8132
7b7a3366
RH
8133 if (d.host_maps) {
8134 walk_memory_regions(&d, open_self_maps_2);
8135 free_self_maps(d.host_maps);
8136 } else {
8137 walk_memory_regions(&d, open_self_maps_3);
77ae5761 8138 }
36c08d49
AG
8139 return 0;
8140}
8141
77ae5761
IL
8142static int open_self_maps(CPUArchState *cpu_env, int fd)
8143{
8144 return open_self_maps_1(cpu_env, fd, false);
8145}
8146
8147static int open_self_smaps(CPUArchState *cpu_env, int fd)
8148{
8149 return open_self_maps_1(cpu_env, fd, true);
8150}
8151
a0939b89 8152static int open_self_stat(CPUArchState *cpu_env, int fd)
480b8e7d 8153{
0effdc29 8154 CPUState *cpu = env_cpu(cpu_env);
e4e5cb4a 8155 TaskState *ts = get_task_state(cpu);
7ad4d5a4 8156 g_autoptr(GString) buf = g_string_new(NULL);
480b8e7d
AG
8157 int i;
8158
8159 for (i = 0; i < 44; i++) {
7ad4d5a4
AB
8160 if (i == 0) {
8161 /* pid */
8162 g_string_printf(buf, FMT_pid " ", getpid());
8163 } else if (i == 1) {
8164 /* app name */
8165 gchar *bin = g_strrstr(ts->bprm->argv[0], "/");
8166 bin = bin ? bin + 1 : ts->bprm->argv[0];
8167 g_string_printf(buf, "(%.15s) ", bin);
25bb27c7
AS
8168 } else if (i == 2) {
8169 /* task state */
8170 g_string_assign(buf, "R "); /* we are running right now */
7aa9fe3a
AS
8171 } else if (i == 3) {
8172 /* ppid */
8173 g_string_printf(buf, FMT_pid " ", getppid());
eb33cdae
CE
8174 } else if (i == 21) {
8175 /* starttime */
8176 g_string_printf(buf, "%" PRIu64 " ", ts->start_boottime);
7ad4d5a4
AB
8177 } else if (i == 27) {
8178 /* stack bottom */
8179 g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack);
8180 } else {
8181 /* for the rest, there is MasterCard */
8182 g_string_printf(buf, "0%c", i == 43 ? '\n' : ' ');
8183 }
8184
8185 if (write(fd, buf->str, buf->len) != buf->len) {
8186 return -1;
8187 }
480b8e7d
AG
8188 }
8189
8190 return 0;
8191}
8192
a0939b89 8193static int open_self_auxv(CPUArchState *cpu_env, int fd)
257450ee 8194{
0effdc29 8195 CPUState *cpu = env_cpu(cpu_env);
e4e5cb4a 8196 TaskState *ts = get_task_state(cpu);
257450ee
AG
8197 abi_ulong auxv = ts->info->saved_auxv;
8198 abi_ulong len = ts->info->auxv_len;
8199 char *ptr;
8200
8201 /*
8202 * Auxiliary vector is stored in target process stack.
8203 * read in whole auxv vector and copy it to file
8204 */
8205 ptr = lock_user(VERIFY_READ, auxv, len, 0);
8206 if (ptr != NULL) {
8207 while (len > 0) {
8208 ssize_t r;
8209 r = write(fd, ptr, len);
8210 if (r <= 0) {
8211 break;
8212 }
8213 len -= r;
8214 ptr += r;
8215 }
8216 lseek(fd, 0, SEEK_SET);
8217 unlock_user(ptr, auxv, len);
8218 }
8219
8220 return 0;
8221}
8222
463d8e73
AS
8223static int is_proc_myself(const char *filename, const char *entry)
8224{
8225 if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
8226 filename += strlen("/proc/");
8227 if (!strncmp(filename, "self/", strlen("self/"))) {
8228 filename += strlen("self/");
8229 } else if (*filename >= '1' && *filename <= '9') {
8230 char myself[80];
8231 snprintf(myself, sizeof(myself), "%d/", getpid());
8232 if (!strncmp(filename, myself, strlen(myself))) {
8233 filename += strlen(myself);
8234 } else {
8235 return 0;
8236 }
8237 } else {
8238 return 0;
8239 }
8240 if (!strcmp(filename, entry)) {
8241 return 1;
8242 }
8243 }
8244 return 0;
8245}
8246
bd5ccd61
HD
8247static void excp_dump_file(FILE *logfile, CPUArchState *env,
8248 const char *fmt, int code)
8249{
8250 if (logfile) {
8251 CPUState *cs = env_cpu(env);
8252
8253 fprintf(logfile, fmt, code);
8254 fprintf(logfile, "Failing executable: %s\n", exec_path);
8255 cpu_dump_state(cs, logfile, 0);
8256 open_self_maps(env, fileno(logfile));
8257 }
8258}
8259
8260void target_exception_dump(CPUArchState *env, const char *fmt, int code)
8261{
8262 /* dump to console */
8263 excp_dump_file(stderr, env, fmt, code);
8264
8265 /* dump to log file */
8266 if (qemu_log_separate()) {
8267 FILE *logfile = qemu_log_trylock();
8268
8269 excp_dump_file(logfile, env, fmt, code);
8270 qemu_log_unlock(logfile);
8271 }
8272}
8273
121c8dd6
RH
8274#include "target_proc.h"
8275
ee3eb3a7 8276#if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN || \
121c8dd6
RH
8277 defined(HAVE_ARCH_PROC_CPUINFO) || \
8278 defined(HAVE_ARCH_PROC_HARDWARE)
de6b9933
LV
8279static int is_proc(const char *filename, const char *entry)
8280{
8281 return strcmp(filename, entry) == 0;
8282}
fff69382 8283#endif
de6b9933 8284
ee3eb3a7 8285#if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
a0939b89 8286static int open_net_route(CPUArchState *cpu_env, int fd)
de6b9933
LV
8287{
8288 FILE *fp;
8289 char *line = NULL;
8290 size_t len = 0;
8291 ssize_t read;
8292
8293 fp = fopen("/proc/net/route", "r");
8294 if (fp == NULL) {
a3ca7bb2 8295 return -1;
de6b9933
LV
8296 }
8297
8298 /* read header */
8299
8300 read = getline(&line, &len, fp);
8301 dprintf(fd, "%s", line);
8302
8303 /* read routes */
8304
8305 while ((read = getline(&line, &len, fp)) != -1) {
8306 char iface[16];
8307 uint32_t dest, gw, mask;
8308 unsigned int flags, refcnt, use, metric, mtu, window, irtt;
9d0bd0cd
PM
8309 int fields;
8310
8311 fields = sscanf(line,
8312 "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
8313 iface, &dest, &gw, &flags, &refcnt, &use, &metric,
8314 &mask, &mtu, &window, &irtt);
8315 if (fields != 11) {
8316 continue;
8317 }
de6b9933
LV
8318 dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
8319 iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
8320 metric, tswap32(mask), mtu, window, irtt);
8321 }
8322
8323 free(line);
8324 fclose(fp);
8325
8326 return 0;
8327}
8328#endif
8329
b8002058 8330int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,
35be898e 8331 int flags, mode_t mode, bool safe)
3be14d05 8332{
b8002058
HD
8333 g_autofree char *proc_name = NULL;
8334 const char *pathname;
3be14d05
AG
8335 struct fake_open {
8336 const char *filename;
a0939b89 8337 int (*fill)(CPUArchState *cpu_env, int fd);
de6b9933 8338 int (*cmp)(const char *s1, const char *s2);
3be14d05
AG
8339 };
8340 const struct fake_open *fake_open;
8341 static const struct fake_open fakes[] = {
de6b9933 8342 { "maps", open_self_maps, is_proc_myself },
77ae5761 8343 { "smaps", open_self_smaps, is_proc_myself },
de6b9933
LV
8344 { "stat", open_self_stat, is_proc_myself },
8345 { "auxv", open_self_auxv, is_proc_myself },
76b94245 8346 { "cmdline", open_self_cmdline, is_proc_myself },
ee3eb3a7 8347#if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
de6b9933 8348 { "/proc/net/route", open_net_route, is_proc },
fff69382 8349#endif
121c8dd6 8350#if defined(HAVE_ARCH_PROC_CPUINFO)
fff69382 8351 { "/proc/cpuinfo", open_cpuinfo, is_proc },
4ab6713e 8352#endif
121c8dd6 8353#if defined(HAVE_ARCH_PROC_HARDWARE)
4ab6713e 8354 { "/proc/hardware", open_hardware, is_proc },
de6b9933
LV
8355#endif
8356 { NULL, NULL, NULL }
3be14d05
AG
8357 };
8358
b8002058
HD
8359 /* if this is a file from /proc/ filesystem, expand full name */
8360 proc_name = realpath(fname, NULL);
8361 if (proc_name && strncmp(proc_name, "/proc/", 6) == 0) {
8362 pathname = proc_name;
8363 } else {
8364 pathname = fname;
8365 }
8366
aa07f5ec 8367 if (is_proc_myself(pathname, "exe")) {
35be898e
IL
8368 if (safe) {
8369 return safe_openat(dirfd, exec_path, flags, mode);
8370 } else {
8371 return openat(dirfd, exec_path, flags, mode);
8372 }
aa07f5ec
MO
8373 }
8374
3be14d05 8375 for (fake_open = fakes; fake_open->filename; fake_open++) {
de6b9933 8376 if (fake_open->cmp(pathname, fake_open->filename)) {
3be14d05
AG
8377 break;
8378 }
8379 }
8380
8381 if (fake_open->filename) {
8382 const char *tmpdir;
8383 char filename[PATH_MAX];
8384 int fd, r;
8385
5b63de6b 8386 fd = memfd_create("qemu-open", 0);
3be14d05 8387 if (fd < 0) {
5b63de6b
RM
8388 if (errno != ENOSYS) {
8389 return fd;
8390 }
8391 /* create temporary file to map stat to */
8392 tmpdir = getenv("TMPDIR");
8393 if (!tmpdir)
8394 tmpdir = "/tmp";
8395 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
8396 fd = mkstemp(filename);
8397 if (fd < 0) {
8398 return fd;
8399 }
8400 unlink(filename);
3be14d05 8401 }
3be14d05
AG
8402
8403 if ((r = fake_open->fill(cpu_env, fd))) {
a3ca7bb2 8404 int e = errno;
3be14d05 8405 close(fd);
a3ca7bb2 8406 errno = e;
3be14d05
AG
8407 return r;
8408 }
8409 lseek(fd, 0, SEEK_SET);
8410
8411 return fd;
8412 }
8413
35be898e
IL
8414 if (safe) {
8415 return safe_openat(dirfd, path(pathname), flags, mode);
8416 } else {
8417 return openat(dirfd, path(pathname), flags, mode);
8418 }
3be14d05
AG
8419}
8420
a4dab0a0
IL
8421ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz)
8422{
8423 ssize_t ret;
8424
8425 if (!pathname || !buf) {
8426 errno = EFAULT;
8427 return -1;
8428 }
8429
8430 if (!bufsiz) {
8431 /* Short circuit this for the magic exe check. */
8432 errno = EINVAL;
8433 return -1;
8434 }
8435
8436 if (is_proc_myself((const char *)pathname, "exe")) {
8437 /*
8438 * Don't worry about sign mismatch as earlier mapping
8439 * logic would have thrown a bad address error.
8440 */
8441 ret = MIN(strlen(exec_path), bufsiz);
8442 /* We cannot NUL terminate the string. */
8443 memcpy(buf, exec_path, ret);
8444 } else {
8445 ret = readlink(path(pathname), buf, bufsiz);
8446 }
8447
8448 return ret;
8449}
8450
7a8d9f3a
PB
8451static int do_execv(CPUArchState *cpu_env, int dirfd,
8452 abi_long pathname, abi_long guest_argp,
8453 abi_long guest_envp, int flags, bool is_execveat)
156e1f67
DD
8454{
8455 int ret;
8456 char **argp, **envp;
8457 int argc, envc;
8458 abi_ulong gp;
8459 abi_ulong addr;
8460 char **q;
8461 void *p;
8462
8463 argc = 0;
8464
8465 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
8466 if (get_user_ual(addr, gp)) {
8467 return -TARGET_EFAULT;
8468 }
8469 if (!addr) {
8470 break;
8471 }
8472 argc++;
8473 }
8474 envc = 0;
8475 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
8476 if (get_user_ual(addr, gp)) {
8477 return -TARGET_EFAULT;
8478 }
8479 if (!addr) {
8480 break;
8481 }
8482 envc++;
8483 }
8484
8485 argp = g_new0(char *, argc + 1);
8486 envp = g_new0(char *, envc + 1);
8487
8488 for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
8489 if (get_user_ual(addr, gp)) {
8490 goto execve_efault;
8491 }
8492 if (!addr) {
8493 break;
8494 }
8495 *q = lock_user_string(addr);
8496 if (!*q) {
8497 goto execve_efault;
8498 }
8499 }
8500 *q = NULL;
8501
8502 for (gp = guest_envp, q = envp; gp; gp += sizeof(abi_ulong), q++) {
8503 if (get_user_ual(addr, gp)) {
8504 goto execve_efault;
8505 }
8506 if (!addr) {
8507 break;
8508 }
8509 *q = lock_user_string(addr);
8510 if (!*q) {
8511 goto execve_efault;
8512 }
8513 }
8514 *q = NULL;
8515
8516 /*
8517 * Although execve() is not an interruptible syscall it is
8518 * a special case where we must use the safe_syscall wrapper:
8519 * if we allow a signal to happen before we make the host
8520 * syscall then we will 'lose' it, because at the point of
8521 * execve the process leaves QEMU's control. So we use the
8522 * safe syscall wrapper to ensure that we either take the
8523 * signal as a guest signal, or else it does not happen
8524 * before the execve completes and makes it the other
8525 * program's problem.
8526 */
8527 p = lock_user_string(pathname);
8528 if (!p) {
8529 goto execve_efault;
8530 }
8531
7a8d9f3a 8532 const char *exe = p;
156e1f67 8533 if (is_proc_myself(p, "exe")) {
7a8d9f3a 8534 exe = exec_path;
156e1f67 8535 }
7a8d9f3a
PB
8536 ret = is_execveat
8537 ? safe_execveat(dirfd, exe, argp, envp, flags)
8538 : safe_execve(exe, argp, envp);
8539 ret = get_errno(ret);
156e1f67
DD
8540
8541 unlock_user(p, pathname, 0);
8542
8543 goto execve_end;
8544
8545execve_efault:
8546 ret = -TARGET_EFAULT;
8547
8548execve_end:
8549 for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
8550 if (get_user_ual(addr, gp) || !addr) {
8551 break;
8552 }
8553 unlock_user(*q, addr, 0);
8554 }
8555 for (gp = guest_envp, q = envp; *q; gp += sizeof(abi_ulong), q++) {
8556 if (get_user_ual(addr, gp) || !addr) {
8557 break;
8558 }
8559 unlock_user(*q, addr, 0);
8560 }
8561
8562 g_free(argp);
8563 g_free(envp);
8564 return ret;
8565}
8566
aecc8861
AG
8567#define TIMER_MAGIC 0x0caf0000
8568#define TIMER_MAGIC_MASK 0xffff0000
8569
8570/* Convert QEMU provided timer ID back to internal 16bit index format */
8571static target_timer_t get_timer_id(abi_long arg)
8572{
8573 target_timer_t timerid = arg;
8574
8575 if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
8576 return -TARGET_EINVAL;
8577 }
8578
8579 timerid &= 0xffff;
8580
8581 if (timerid >= ARRAY_SIZE(g_posix_timers)) {
8582 return -TARGET_EINVAL;
8583 }
8584
8585 return timerid;
8586}
8587
2e0a8713
ST
8588static int target_to_host_cpu_mask(unsigned long *host_mask,
8589 size_t host_size,
8590 abi_ulong target_addr,
8591 size_t target_size)
8592{
8593 unsigned target_bits = sizeof(abi_ulong) * 8;
8594 unsigned host_bits = sizeof(*host_mask) * 8;
8595 abi_ulong *target_mask;
8596 unsigned i, j;
8597
8598 assert(host_size >= target_size);
8599
8600 target_mask = lock_user(VERIFY_READ, target_addr, target_size, 1);
8601 if (!target_mask) {
8602 return -TARGET_EFAULT;
8603 }
8604 memset(host_mask, 0, host_size);
8605
8606 for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
8607 unsigned bit = i * target_bits;
8608 abi_ulong val;
8609
8610 __get_user(val, &target_mask[i]);
8611 for (j = 0; j < target_bits; j++, bit++) {
8612 if (val & (1UL << j)) {
8613 host_mask[bit / host_bits] |= 1UL << (bit % host_bits);
8614 }
8615 }
8616 }
8617
8618 unlock_user(target_mask, target_addr, 0);
8619 return 0;
8620}
8621
8622static int host_to_target_cpu_mask(const unsigned long *host_mask,
8623 size_t host_size,
8624 abi_ulong target_addr,
8625 size_t target_size)
8626{
8627 unsigned target_bits = sizeof(abi_ulong) * 8;
8628 unsigned host_bits = sizeof(*host_mask) * 8;
8629 abi_ulong *target_mask;
8630 unsigned i, j;
8631
8632 assert(host_size >= target_size);
8633
8634 target_mask = lock_user(VERIFY_WRITE, target_addr, target_size, 0);
8635 if (!target_mask) {
8636 return -TARGET_EFAULT;
8637 }
8638
8639 for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
8640 unsigned bit = i * target_bits;
8641 abi_ulong val = 0;
8642
8643 for (j = 0; j < target_bits; j++, bit++) {
8644 if (host_mask[bit / host_bits] & (1UL << (bit % host_bits))) {
8645 val |= 1UL << j;
8646 }
8647 }
8648 __put_user(val, &target_mask[i]);
8649 }
8650
8651 unlock_user(target_mask, target_addr, target_size);
8652 return 0;
8653}
8654
fd08ddb9 8655#ifdef TARGET_NR_getdents
aee14c77 8656static int do_getdents(abi_long dirfd, abi_long arg2, abi_long count)
fd08ddb9 8657{
aee14c77
RH
8658 g_autofree void *hdirp = NULL;
8659 void *tdirp;
8660 int hlen, hoff, toff;
8661 int hreclen, treclen;
8662 off64_t prev_diroff = 0;
8663
8664 hdirp = g_try_malloc(count);
8665 if (!hdirp) {
8666 return -TARGET_ENOMEM;
8667 }
fd08ddb9
RH
8668
8669#ifdef EMULATE_GETDENTS_WITH_GETDENTS
aee14c77
RH
8670 hlen = sys_getdents(dirfd, hdirp, count);
8671#else
8672 hlen = sys_getdents64(dirfd, hdirp, count);
8673#endif
fd08ddb9 8674
aee14c77
RH
8675 hlen = get_errno(hlen);
8676 if (is_error(hlen)) {
8677 return hlen;
fd08ddb9
RH
8678 }
8679
aee14c77
RH
8680 tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
8681 if (!tdirp) {
fd08ddb9
RH
8682 return -TARGET_EFAULT;
8683 }
aee14c77
RH
8684
8685 for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
8686#ifdef EMULATE_GETDENTS_WITH_GETDENTS
8687 struct linux_dirent *hde = hdirp + hoff;
fd08ddb9 8688#else
aee14c77
RH
8689 struct linux_dirent64 *hde = hdirp + hoff;
8690#endif
8691 struct target_dirent *tde = tdirp + toff;
8692 int namelen;
8693 uint8_t type;
fd08ddb9 8694
aee14c77
RH
8695 namelen = strlen(hde->d_name);
8696 hreclen = hde->d_reclen;
8697 treclen = offsetof(struct target_dirent, d_name) + namelen + 2;
8698 treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent));
fd08ddb9 8699
aee14c77 8700 if (toff + treclen > count) {
fd08ddb9 8701 /*
aee14c77
RH
8702 * If the host struct is smaller than the target struct, or
8703 * requires less alignment and thus packs into less space,
8704 * then the host can return more entries than we can pass
8705 * on to the guest.
fd08ddb9 8706 */
aee14c77
RH
8707 if (toff == 0) {
8708 toff = -TARGET_EINVAL; /* result buffer is too small */
8709 break;
8710 }
8711 /*
8712 * Return what we have, resetting the file pointer to the
8713 * location of the first record not returned.
8714 */
8715 lseek64(dirfd, prev_diroff, SEEK_SET);
8716 break;
fd08ddb9 8717 }
aee14c77
RH
8718
8719 prev_diroff = hde->d_off;
8720 tde->d_ino = tswapal(hde->d_ino);
8721 tde->d_off = tswapal(hde->d_off);
8722 tde->d_reclen = tswap16(treclen);
8723 memcpy(tde->d_name, hde->d_name, namelen + 1);
8724
8725 /*
8726 * The getdents type is in what was formerly a padding byte at the
8727 * end of the structure.
8728 */
8729#ifdef EMULATE_GETDENTS_WITH_GETDENTS
8730 type = *((uint8_t *)hde + hreclen - 1);
8731#else
8732 type = hde->d_type;
fd08ddb9 8733#endif
aee14c77
RH
8734 *((uint8_t *)tde + treclen - 1) = type;
8735 }
8736
8737 unlock_user(tdirp, arg2, toff);
8738 return toff;
fd08ddb9
RH
8739}
8740#endif /* TARGET_NR_getdents */
8741
8742#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
aee14c77 8743static int do_getdents64(abi_long dirfd, abi_long arg2, abi_long count)
fd08ddb9 8744{
aee14c77
RH
8745 g_autofree void *hdirp = NULL;
8746 void *tdirp;
8747 int hlen, hoff, toff;
8748 int hreclen, treclen;
8749 off64_t prev_diroff = 0;
8750
8751 hdirp = g_try_malloc(count);
8752 if (!hdirp) {
8753 return -TARGET_ENOMEM;
8754 }
fd08ddb9 8755
aee14c77
RH
8756 hlen = get_errno(sys_getdents64(dirfd, hdirp, count));
8757 if (is_error(hlen)) {
8758 return hlen;
8759 }
8760
8761 tdirp = lock_user(VERIFY_WRITE, arg2, count, 0);
8762 if (!tdirp) {
fd08ddb9
RH
8763 return -TARGET_EFAULT;
8764 }
aee14c77
RH
8765
8766 for (hoff = toff = 0; hoff < hlen; hoff += hreclen, toff += treclen) {
8767 struct linux_dirent64 *hde = hdirp + hoff;
8768 struct target_dirent64 *tde = tdirp + toff;
8769 int namelen;
8770
8771 namelen = strlen(hde->d_name) + 1;
8772 hreclen = hde->d_reclen;
8773 treclen = offsetof(struct target_dirent64, d_name) + namelen;
8774 treclen = QEMU_ALIGN_UP(treclen, __alignof(struct target_dirent64));
8775
8776 if (toff + treclen > count) {
8777 /*
8778 * If the host struct is smaller than the target struct, or
8779 * requires less alignment and thus packs into less space,
8780 * then the host can return more entries than we can pass
8781 * on to the guest.
8782 */
8783 if (toff == 0) {
8784 toff = -TARGET_EINVAL; /* result buffer is too small */
fd08ddb9
RH
8785 break;
8786 }
aee14c77
RH
8787 /*
8788 * Return what we have, resetting the file pointer to the
8789 * location of the first record not returned.
8790 */
8791 lseek64(dirfd, prev_diroff, SEEK_SET);
8792 break;
fd08ddb9 8793 }
aee14c77
RH
8794
8795 prev_diroff = hde->d_off;
8796 tde->d_ino = tswap64(hde->d_ino);
8797 tde->d_off = tswap64(hde->d_off);
8798 tde->d_reclen = tswap16(treclen);
8799 tde->d_type = hde->d_type;
8800 memcpy(tde->d_name, hde->d_name, namelen);
fd08ddb9 8801 }
aee14c77
RH
8802
8803 unlock_user(tdirp, arg2, toff);
8804 return toff;
fd08ddb9
RH
8805}
8806#endif /* TARGET_NR_getdents64 */
8807
9e1c7d98
RE
8808#if defined(TARGET_NR_riscv_hwprobe)
8809
8810#define RISCV_HWPROBE_KEY_MVENDORID 0
8811#define RISCV_HWPROBE_KEY_MARCHID 1
8812#define RISCV_HWPROBE_KEY_MIMPID 2
8813
8814#define RISCV_HWPROBE_KEY_BASE_BEHAVIOR 3
8815#define RISCV_HWPROBE_BASE_BEHAVIOR_IMA (1 << 0)
8816
a3432cf2
CM
8817#define RISCV_HWPROBE_KEY_IMA_EXT_0 4
8818#define RISCV_HWPROBE_IMA_FD (1 << 0)
8819#define RISCV_HWPROBE_IMA_C (1 << 1)
8820#define RISCV_HWPROBE_IMA_V (1 << 2)
8821#define RISCV_HWPROBE_EXT_ZBA (1 << 3)
8822#define RISCV_HWPROBE_EXT_ZBB (1 << 4)
8823#define RISCV_HWPROBE_EXT_ZBS (1 << 5)
8824#define RISCV_HWPROBE_EXT_ZICBOZ (1 << 6)
8825#define RISCV_HWPROBE_EXT_ZBC (1 << 7)
8826#define RISCV_HWPROBE_EXT_ZBKB (1 << 8)
8827#define RISCV_HWPROBE_EXT_ZBKC (1 << 9)
8828#define RISCV_HWPROBE_EXT_ZBKX (1 << 10)
8829#define RISCV_HWPROBE_EXT_ZKND (1 << 11)
8830#define RISCV_HWPROBE_EXT_ZKNE (1 << 12)
8831#define RISCV_HWPROBE_EXT_ZKNH (1 << 13)
8832#define RISCV_HWPROBE_EXT_ZKSED (1 << 14)
8833#define RISCV_HWPROBE_EXT_ZKSH (1 << 15)
8834#define RISCV_HWPROBE_EXT_ZKT (1 << 16)
8835#define RISCV_HWPROBE_EXT_ZVBB (1 << 17)
8836#define RISCV_HWPROBE_EXT_ZVBC (1 << 18)
8837#define RISCV_HWPROBE_EXT_ZVKB (1 << 19)
8838#define RISCV_HWPROBE_EXT_ZVKG (1 << 20)
8839#define RISCV_HWPROBE_EXT_ZVKNED (1 << 21)
8840#define RISCV_HWPROBE_EXT_ZVKNHA (1 << 22)
8841#define RISCV_HWPROBE_EXT_ZVKNHB (1 << 23)
8842#define RISCV_HWPROBE_EXT_ZVKSED (1 << 24)
8843#define RISCV_HWPROBE_EXT_ZVKSH (1 << 25)
8844#define RISCV_HWPROBE_EXT_ZVKT (1 << 26)
8845#define RISCV_HWPROBE_EXT_ZFH (1 << 27)
8846#define RISCV_HWPROBE_EXT_ZFHMIN (1 << 28)
8847#define RISCV_HWPROBE_EXT_ZIHINTNTL (1 << 29)
8848#define RISCV_HWPROBE_EXT_ZVFH (1 << 30)
8849#define RISCV_HWPROBE_EXT_ZVFHMIN (1 << 31)
8850#define RISCV_HWPROBE_EXT_ZFA (1ULL << 32)
4f1a53b3 8851#define RISCV_HWPROBE_EXT_ZTSO (1ULL << 33)
a3432cf2
CM
8852#define RISCV_HWPROBE_EXT_ZACAS (1ULL << 34)
8853#define RISCV_HWPROBE_EXT_ZICOND (1ULL << 35)
9e1c7d98
RE
8854
8855#define RISCV_HWPROBE_KEY_CPUPERF_0 5
8856#define RISCV_HWPROBE_MISALIGNED_UNKNOWN (0 << 0)
8857#define RISCV_HWPROBE_MISALIGNED_EMULATED (1 << 0)
8858#define RISCV_HWPROBE_MISALIGNED_SLOW (2 << 0)
8859#define RISCV_HWPROBE_MISALIGNED_FAST (3 << 0)
8860#define RISCV_HWPROBE_MISALIGNED_UNSUPPORTED (4 << 0)
8861#define RISCV_HWPROBE_MISALIGNED_MASK (7 << 0)
8862
301c65f4
PD
8863#define RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE 6
8864
9e1c7d98
RE
8865struct riscv_hwprobe {
8866 abi_llong key;
8867 abi_ullong value;
8868};
8869
8870static void risc_hwprobe_fill_pairs(CPURISCVState *env,
8871 struct riscv_hwprobe *pair,
8872 size_t pair_count)
8873{
8874 const RISCVCPUConfig *cfg = riscv_cpu_cfg(env);
8875
8876 for (; pair_count > 0; pair_count--, pair++) {
8877 abi_llong key;
8878 abi_ullong value;
8879 __put_user(0, &pair->value);
8880 __get_user(key, &pair->key);
8881 switch (key) {
8882 case RISCV_HWPROBE_KEY_MVENDORID:
8883 __put_user(cfg->mvendorid, &pair->value);
8884 break;
8885 case RISCV_HWPROBE_KEY_MARCHID:
8886 __put_user(cfg->marchid, &pair->value);
8887 break;
8888 case RISCV_HWPROBE_KEY_MIMPID:
8889 __put_user(cfg->mimpid, &pair->value);
8890 break;
8891 case RISCV_HWPROBE_KEY_BASE_BEHAVIOR:
8892 value = riscv_has_ext(env, RVI) &&
8893 riscv_has_ext(env, RVM) &&
8894 riscv_has_ext(env, RVA) ?
8895 RISCV_HWPROBE_BASE_BEHAVIOR_IMA : 0;
8896 __put_user(value, &pair->value);
8897 break;
8898 case RISCV_HWPROBE_KEY_IMA_EXT_0:
8899 value = riscv_has_ext(env, RVF) &&
8900 riscv_has_ext(env, RVD) ?
8901 RISCV_HWPROBE_IMA_FD : 0;
8902 value |= riscv_has_ext(env, RVC) ?
bb0a45e9
RE
8903 RISCV_HWPROBE_IMA_C : 0;
8904 value |= riscv_has_ext(env, RVV) ?
8905 RISCV_HWPROBE_IMA_V : 0;
8906 value |= cfg->ext_zba ?
8907 RISCV_HWPROBE_EXT_ZBA : 0;
8908 value |= cfg->ext_zbb ?
8909 RISCV_HWPROBE_EXT_ZBB : 0;
8910 value |= cfg->ext_zbs ?
8911 RISCV_HWPROBE_EXT_ZBS : 0;
0191131d
CM
8912 value |= cfg->ext_zicboz ?
8913 RISCV_HWPROBE_EXT_ZICBOZ : 0;
a3432cf2
CM
8914 value |= cfg->ext_zbc ?
8915 RISCV_HWPROBE_EXT_ZBC : 0;
8916 value |= cfg->ext_zbkb ?
8917 RISCV_HWPROBE_EXT_ZBKB : 0;
8918 value |= cfg->ext_zbkc ?
8919 RISCV_HWPROBE_EXT_ZBKC : 0;
8920 value |= cfg->ext_zbkx ?
8921 RISCV_HWPROBE_EXT_ZBKX : 0;
8922 value |= cfg->ext_zknd ?
8923 RISCV_HWPROBE_EXT_ZKND : 0;
8924 value |= cfg->ext_zkne ?
8925 RISCV_HWPROBE_EXT_ZKNE : 0;
8926 value |= cfg->ext_zknh ?
8927 RISCV_HWPROBE_EXT_ZKNH : 0;
8928 value |= cfg->ext_zksed ?
8929 RISCV_HWPROBE_EXT_ZKSED : 0;
8930 value |= cfg->ext_zksh ?
8931 RISCV_HWPROBE_EXT_ZKSH : 0;
8932 value |= cfg->ext_zkt ?
8933 RISCV_HWPROBE_EXT_ZKT : 0;
8934 value |= cfg->ext_zvbb ?
8935 RISCV_HWPROBE_EXT_ZVBB : 0;
8936 value |= cfg->ext_zvbc ?
8937 RISCV_HWPROBE_EXT_ZVBC : 0;
8938 value |= cfg->ext_zvkb ?
8939 RISCV_HWPROBE_EXT_ZVKB : 0;
8940 value |= cfg->ext_zvkg ?
8941 RISCV_HWPROBE_EXT_ZVKG : 0;
8942 value |= cfg->ext_zvkned ?
8943 RISCV_HWPROBE_EXT_ZVKNED : 0;
8944 value |= cfg->ext_zvknha ?
8945 RISCV_HWPROBE_EXT_ZVKNHA : 0;
8946 value |= cfg->ext_zvknhb ?
8947 RISCV_HWPROBE_EXT_ZVKNHB : 0;
8948 value |= cfg->ext_zvksed ?
8949 RISCV_HWPROBE_EXT_ZVKSED : 0;
8950 value |= cfg->ext_zvksh ?
8951 RISCV_HWPROBE_EXT_ZVKSH : 0;
8952 value |= cfg->ext_zvkt ?
8953 RISCV_HWPROBE_EXT_ZVKT : 0;
8954 value |= cfg->ext_zfh ?
8955 RISCV_HWPROBE_EXT_ZFH : 0;
8956 value |= cfg->ext_zfhmin ?
8957 RISCV_HWPROBE_EXT_ZFHMIN : 0;
8958 value |= cfg->ext_zihintntl ?
8959 RISCV_HWPROBE_EXT_ZIHINTNTL : 0;
8960 value |= cfg->ext_zvfh ?
8961 RISCV_HWPROBE_EXT_ZVFH : 0;
8962 value |= cfg->ext_zvfhmin ?
8963 RISCV_HWPROBE_EXT_ZVFHMIN : 0;
8964 value |= cfg->ext_zfa ?
8965 RISCV_HWPROBE_EXT_ZFA : 0;
4f1a53b3
CM
8966 value |= cfg->ext_ztso ?
8967 RISCV_HWPROBE_EXT_ZTSO : 0;
a3432cf2
CM
8968 value |= cfg->ext_zacas ?
8969 RISCV_HWPROBE_EXT_ZACAS : 0;
8970 value |= cfg->ext_zicond ?
8971 RISCV_HWPROBE_EXT_ZICOND : 0;
9e1c7d98
RE
8972 __put_user(value, &pair->value);
8973 break;
8974 case RISCV_HWPROBE_KEY_CPUPERF_0:
8975 __put_user(RISCV_HWPROBE_MISALIGNED_FAST, &pair->value);
8976 break;
301c65f4
PD
8977 case RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE:
8978 value = cfg->ext_zicboz ? cfg->cboz_blocksize : 0;
8979 __put_user(value, &pair->value);
8980 break;
9e1c7d98
RE
8981 default:
8982 __put_user(-1, &pair->key);
8983 break;
8984 }
8985 }
8986}
8987
8988static int cpu_set_valid(abi_long arg3, abi_long arg4)
8989{
8990 int ret, i, tmp;
8991 size_t host_mask_size, target_mask_size;
8992 unsigned long *host_mask;
8993
8994 /*
8995 * cpu_set_t represent CPU masks as bit masks of type unsigned long *.
8996 * arg3 contains the cpu count.
8997 */
8998 tmp = (8 * sizeof(abi_ulong));
8999 target_mask_size = ((arg3 + tmp - 1) / tmp) * sizeof(abi_ulong);
9000 host_mask_size = (target_mask_size + (sizeof(*host_mask) - 1)) &
9001 ~(sizeof(*host_mask) - 1);
9002
9003 host_mask = alloca(host_mask_size);
9004
9005 ret = target_to_host_cpu_mask(host_mask, host_mask_size,
9006 arg4, target_mask_size);
9007 if (ret != 0) {
9008 return ret;
9009 }
9010
9011 for (i = 0 ; i < host_mask_size / sizeof(*host_mask); i++) {
9012 if (host_mask[i] != 0) {
9013 return 0;
9014 }
9015 }
9016 return -TARGET_EINVAL;
9017}
9018
9019static abi_long do_riscv_hwprobe(CPUArchState *cpu_env, abi_long arg1,
9020 abi_long arg2, abi_long arg3,
9021 abi_long arg4, abi_long arg5)
9022{
9023 int ret;
9024 struct riscv_hwprobe *host_pairs;
9025
9026 /* flags must be 0 */
9027 if (arg5 != 0) {
9028 return -TARGET_EINVAL;
9029 }
9030
9031 /* check cpu_set */
9032 if (arg3 != 0) {
9033 ret = cpu_set_valid(arg3, arg4);
9034 if (ret != 0) {
9035 return ret;
9036 }
9037 } else if (arg4 != 0) {
9038 return -TARGET_EINVAL;
9039 }
9040
9041 /* no pairs */
9042 if (arg2 == 0) {
9043 return 0;
9044 }
9045
9046 host_pairs = lock_user(VERIFY_WRITE, arg1,
9047 sizeof(*host_pairs) * (size_t)arg2, 0);
9048 if (host_pairs == NULL) {
9049 return -TARGET_EFAULT;
9050 }
9051 risc_hwprobe_fill_pairs(cpu_env, host_pairs, arg2);
9052 unlock_user(host_pairs, arg1, sizeof(*host_pairs) * (size_t)arg2);
9053 return 0;
9054}
9055#endif /* TARGET_NR_riscv_hwprobe */
9056
e10fbe8f
YT
9057#if defined(TARGET_NR_pivot_root) && defined(__NR_pivot_root)
9058_syscall2(int, pivot_root, const char *, new_root, const char *, put_old)
9059#endif
9060
7f696cdd
TW
9061#if defined(TARGET_NR_open_tree) && defined(__NR_open_tree)
9062#define __NR_sys_open_tree __NR_open_tree
9063_syscall3(int, sys_open_tree, int, __dfd, const char *, __filename,
9064 unsigned int, __flags)
9065#endif
9066
4b2d2753
TW
9067#if defined(TARGET_NR_move_mount) && defined(__NR_move_mount)
9068#define __NR_sys_move_mount __NR_move_mount
9069_syscall5(int, sys_move_mount, int, __from_dfd, const char *, __from_pathname,
9070 int, __to_dfd, const char *, __to_pathname, unsigned int, flag)
9071#endif
9072
dc1ce18b
RH
9073/* This is an internal helper for do_syscall so that it is easier
9074 * to have a single return point, so that actions, such as logging
9075 * of syscall results, can be performed.
9076 * All errnos that do_syscall() returns must be -TARGET_<errcode>.
9077 */
a0939b89 9078static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
dc1ce18b
RH
9079 abi_long arg2, abi_long arg3, abi_long arg4,
9080 abi_long arg5, abi_long arg6, abi_long arg7,
9081 abi_long arg8)
31e31b8a 9082{
29a0af61 9083 CPUState *cpu = env_cpu(cpu_env);
992f48a0 9084 abi_long ret;
4f7f8924
AR
9085#if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
9086 || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
efa92184
AR
9087 || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
9088 || defined(TARGET_NR_statx)
31e31b8a 9089 struct stat st;
4f7f8924
AR
9090#endif
9091#if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
9092 || defined(TARGET_NR_fstatfs)
56c8f68f 9093 struct statfs stfs;
4f7f8924 9094#endif
53a5960a 9095 void *p;
3b46e624 9096
31e31b8a
FB
9097 switch(num) {
9098 case TARGET_NR_exit:
9b056fcc 9099 /* In old applications this may be used to implement _exit(2).
6f9ff551 9100 However in threaded applications it is used for thread termination,
9b056fcc
AF
9101 and _exit_group is used for application termination.
9102 Do thread termination if we have more then one thread. */
a0995886
TB
9103
9104 if (block_signals()) {
af254a27 9105 return -QEMU_ERESTARTSYS;
a0995886
TB
9106 }
9107
1f81ce90 9108 pthread_mutex_lock(&clone_lock);
dd1f6349 9109
bdc44640 9110 if (CPU_NEXT(first_cpu)) {
e4e5cb4a 9111 TaskState *ts = get_task_state(cpu);
9b056fcc 9112
6490d9aa
RH
9113 if (ts->child_tidptr) {
9114 put_user_u32(0, ts->child_tidptr);
9115 do_sys_futex(g2h(cpu, ts->child_tidptr),
9116 FUTEX_WAKE, INT_MAX, NULL, NULL, 0);
9117 }
9118
9119 object_unparent(OBJECT(cpu));
1f81ce90
AB
9120 object_unref(OBJECT(cpu));
9121 /*
9122 * At this point the CPU should be unrealized and removed
9123 * from cpu lists. We can clean-up the rest of the thread
9124 * data without the lock held.
9125 */
dd1f6349 9126
1f81ce90 9127 pthread_mutex_unlock(&clone_lock);
dd1f6349 9128
a2247f8e 9129 thread_cpu = NULL;
9b056fcc 9130 g_free(ts);
70903763 9131 rcu_unregister_thread();
9b056fcc
AF
9132 pthread_exit(NULL);
9133 }
dd1f6349 9134
1f81ce90 9135 pthread_mutex_unlock(&clone_lock);
708b6a64 9136 preexit_cleanup(cpu_env, arg1);
c2764719 9137 _exit(arg1);
72eb7ea8 9138 return 0; /* avoid warning */
31e31b8a 9139 case TARGET_NR_read:
ba584f1d
AS
9140 if (arg2 == 0 && arg3 == 0) {
9141 return get_errno(safe_read(arg1, 0, 0));
72eb7ea8 9142 } else {
38d840e6 9143 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
2852aafd 9144 return -TARGET_EFAULT;
50afd02b 9145 ret = get_errno(safe_read(arg1, p, arg3));
e36800c9 9146 if (ret >= 0 &&
5d4d3665
LV
9147 fd_trans_host_to_target_data(arg1)) {
9148 ret = fd_trans_host_to_target_data(arg1)(p, ret);
e36800c9 9149 }
38d840e6
AJ
9150 unlock_user(p, arg2, ret);
9151 }
72eb7ea8 9152 return ret;
31e31b8a 9153 case TARGET_NR_write:
58cfa6c2
TGJ
9154 if (arg2 == 0 && arg3 == 0) {
9155 return get_errno(safe_write(arg1, 0, 0));
9156 }
579a97f7 9157 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
2852aafd 9158 return -TARGET_EFAULT;
04b9bcf9
LV
9159 if (fd_trans_target_to_host_data(arg1)) {
9160 void *copy = g_malloc(arg3);
9161 memcpy(copy, p, arg3);
9162 ret = fd_trans_target_to_host_data(arg1)(copy, arg3);
9163 if (ret >= 0) {
9164 ret = get_errno(safe_write(arg1, copy, ret));
9165 }
9166 g_free(copy);
9167 } else {
9168 ret = get_errno(safe_write(arg1, p, arg3));
9169 }
53a5960a 9170 unlock_user(p, arg2, 0);
72eb7ea8
RH
9171 return ret;
9172
704eff6c 9173#ifdef TARGET_NR_open
31e31b8a 9174 case TARGET_NR_open:
2f619698 9175 if (!(p = lock_user_string(arg1)))
2852aafd 9176 return -TARGET_EFAULT;
a4dab0a0 9177 ret = get_errno(do_guest_openat(cpu_env, AT_FDCWD, p,
0b2effd7 9178 target_to_host_bitmask(arg2, fcntl_flags_tbl),
35be898e 9179 arg3, true));
e36800c9 9180 fd_trans_unregister(ret);
53a5960a 9181 unlock_user(p, arg1, 0);
72eb7ea8 9182 return ret;
704eff6c 9183#endif
82424832 9184 case TARGET_NR_openat:
579a97f7 9185 if (!(p = lock_user_string(arg2)))
2852aafd 9186 return -TARGET_EFAULT;
a4dab0a0 9187 ret = get_errno(do_guest_openat(cpu_env, arg1, p,
0b2effd7 9188 target_to_host_bitmask(arg3, fcntl_flags_tbl),
35be898e 9189 arg4, true));
e36800c9 9190 fd_trans_unregister(ret);
579a97f7 9191 unlock_user(p, arg2, 0);
72eb7ea8 9192 return ret;
0f0426f3
LV
9193#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
9194 case TARGET_NR_name_to_handle_at:
9195 ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
72eb7ea8 9196 return ret;
0f0426f3
LV
9197#endif
9198#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
9199 case TARGET_NR_open_by_handle_at:
9200 ret = do_open_by_handle_at(arg1, arg2, arg3);
e36800c9 9201 fd_trans_unregister(ret);
72eb7ea8 9202 return ret;
cc054c6f
HD
9203#endif
9204#if defined(__NR_pidfd_open) && defined(TARGET_NR_pidfd_open)
9205 case TARGET_NR_pidfd_open:
9206 return get_errno(pidfd_open(arg1, arg2));
9207#endif
9208#if defined(__NR_pidfd_send_signal) && defined(TARGET_NR_pidfd_send_signal)
9209 case TARGET_NR_pidfd_send_signal:
9210 {
46187d70 9211 siginfo_t uinfo, *puinfo;
cc054c6f 9212
46187d70
LV
9213 if (arg3) {
9214 p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
9215 if (!p) {
9216 return -TARGET_EFAULT;
9217 }
9218 target_to_host_siginfo(&uinfo, p);
9219 unlock_user(p, arg3, 0);
9220 puinfo = &uinfo;
9221 } else {
9222 puinfo = NULL;
cc054c6f 9223 }
cc054c6f 9224 ret = get_errno(pidfd_send_signal(arg1, target_to_host_signal(arg2),
46187d70 9225 puinfo, arg4));
cc054c6f
HD
9226 }
9227 return ret;
9228#endif
9229#if defined(__NR_pidfd_getfd) && defined(TARGET_NR_pidfd_getfd)
9230 case TARGET_NR_pidfd_getfd:
9231 return get_errno(pidfd_getfd(arg1, arg2, arg3));
0f0426f3 9232#endif
31e31b8a 9233 case TARGET_NR_close:
e36800c9 9234 fd_trans_unregister(arg1);
72eb7ea8 9235 return get_errno(close(arg1));
af804f39
HD
9236#if defined(__NR_close_range) && defined(TARGET_NR_close_range)
9237 case TARGET_NR_close_range:
9238 ret = get_errno(sys_close_range(arg1, arg2, arg3));
9239 if (ret == 0 && !(arg3 & CLOSE_RANGE_CLOEXEC)) {
9240 abi_long fd, maxfd;
9241 maxfd = MIN(arg2, target_fd_max);
9242 for (fd = arg1; fd < maxfd; fd++) {
9243 fd_trans_unregister(fd);
9244 }
9245 }
9246 return ret;
9247#endif
72eb7ea8 9248
31e31b8a 9249 case TARGET_NR_brk:
72eb7ea8 9250 return do_brk(arg1);
704eff6c 9251#ifdef TARGET_NR_fork
31e31b8a 9252 case TARGET_NR_fork:
72eb7ea8 9253 return get_errno(do_fork(cpu_env, TARGET_SIGCHLD, 0, 0, 0, 0));
704eff6c 9254#endif
e5febef5 9255#ifdef TARGET_NR_waitpid
31e31b8a
FB
9256 case TARGET_NR_waitpid:
9257 {
53a5960a 9258 int status;
4af80a37 9259 ret = get_errno(safe_wait4(arg1, &status, arg3, 0));
5379557b 9260 if (!is_error(ret) && arg2 && ret
1d9d8b55 9261 && put_user_s32(host_to_target_waitstatus(status), arg2))
2852aafd 9262 return -TARGET_EFAULT;
31e31b8a 9263 }
72eb7ea8 9264 return ret;
e5febef5 9265#endif
f0cbb613
PB
9266#ifdef TARGET_NR_waitid
9267 case TARGET_NR_waitid:
9268 {
f0907ff4 9269 struct rusage ru;
f0cbb613 9270 siginfo_t info;
f0907ff4
RH
9271
9272 ret = get_errno(safe_waitid(arg1, arg2, (arg3 ? &info : NULL),
9273 arg4, (arg5 ? &ru : NULL)));
9274 if (!is_error(ret)) {
9275 if (arg3) {
9276 p = lock_user(VERIFY_WRITE, arg3,
9277 sizeof(target_siginfo_t), 0);
9278 if (!p) {
9279 return -TARGET_EFAULT;
9280 }
9281 host_to_target_siginfo(p, &info);
9282 unlock_user(p, arg3, sizeof(target_siginfo_t));
9283 }
9284 if (arg5 && host_to_target_rusage(arg5, &ru)) {
2852aafd 9285 return -TARGET_EFAULT;
f0907ff4 9286 }
f0cbb613
PB
9287 }
9288 }
72eb7ea8 9289 return ret;
f0cbb613 9290#endif
7a3148a9 9291#ifdef TARGET_NR_creat /* not on alpha */
31e31b8a 9292 case TARGET_NR_creat:
579a97f7 9293 if (!(p = lock_user_string(arg1)))
2852aafd 9294 return -TARGET_EFAULT;
53a5960a 9295 ret = get_errno(creat(p, arg2));
e36800c9 9296 fd_trans_unregister(ret);
53a5960a 9297 unlock_user(p, arg1, 0);
72eb7ea8 9298 return ret;
7a3148a9 9299#endif
704eff6c 9300#ifdef TARGET_NR_link
31e31b8a 9301 case TARGET_NR_link:
53a5960a
PB
9302 {
9303 void * p2;
9304 p = lock_user_string(arg1);
9305 p2 = lock_user_string(arg2);
579a97f7
FB
9306 if (!p || !p2)
9307 ret = -TARGET_EFAULT;
9308 else
9309 ret = get_errno(link(p, p2));
53a5960a
PB
9310 unlock_user(p2, arg2, 0);
9311 unlock_user(p, arg1, 0);
9312 }
72eb7ea8 9313 return ret;
704eff6c 9314#endif
c0d472b1 9315#if defined(TARGET_NR_linkat)
64f0ce4c 9316 case TARGET_NR_linkat:
64f0ce4c
TS
9317 {
9318 void * p2 = NULL;
579a97f7 9319 if (!arg2 || !arg4)
2852aafd 9320 return -TARGET_EFAULT;
64f0ce4c
TS
9321 p = lock_user_string(arg2);
9322 p2 = lock_user_string(arg4);
579a97f7 9323 if (!p || !p2)
0da46a6e 9324 ret = -TARGET_EFAULT;
64f0ce4c 9325 else
c0d472b1 9326 ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
579a97f7
FB
9327 unlock_user(p, arg2, 0);
9328 unlock_user(p2, arg4, 0);
64f0ce4c 9329 }
72eb7ea8 9330 return ret;
64f0ce4c 9331#endif
704eff6c 9332#ifdef TARGET_NR_unlink
31e31b8a 9333 case TARGET_NR_unlink:
579a97f7 9334 if (!(p = lock_user_string(arg1)))
2852aafd 9335 return -TARGET_EFAULT;
53a5960a
PB
9336 ret = get_errno(unlink(p));
9337 unlock_user(p, arg1, 0);
72eb7ea8 9338 return ret;
704eff6c 9339#endif
c0d472b1 9340#if defined(TARGET_NR_unlinkat)
8170f56b 9341 case TARGET_NR_unlinkat:
579a97f7 9342 if (!(p = lock_user_string(arg2)))
2852aafd 9343 return -TARGET_EFAULT;
c0d472b1 9344 ret = get_errno(unlinkat(arg1, p, arg3));
579a97f7 9345 unlock_user(p, arg2, 0);
72eb7ea8 9346 return ret;
b7d35e65 9347#endif
55bbe4d5 9348 case TARGET_NR_execveat:
7a8d9f3a 9349 return do_execv(cpu_env, arg1, arg2, arg3, arg4, arg5, true);
31e31b8a 9350 case TARGET_NR_execve:
7a8d9f3a 9351 return do_execv(cpu_env, AT_FDCWD, arg1, arg2, arg3, 0, false);
31e31b8a 9352 case TARGET_NR_chdir:
579a97f7 9353 if (!(p = lock_user_string(arg1)))
2852aafd 9354 return -TARGET_EFAULT;
53a5960a
PB
9355 ret = get_errno(chdir(p));
9356 unlock_user(p, arg1, 0);
72eb7ea8 9357 return ret;
a315a145 9358#ifdef TARGET_NR_time
31e31b8a
FB
9359 case TARGET_NR_time:
9360 {
53a5960a
PB
9361 time_t host_time;
9362 ret = get_errno(time(&host_time));
2f619698
FB
9363 if (!is_error(ret)
9364 && arg1
9365 && put_user_sal(host_time, arg1))
2852aafd 9366 return -TARGET_EFAULT;
31e31b8a 9367 }
72eb7ea8 9368 return ret;
a315a145 9369#endif
704eff6c 9370#ifdef TARGET_NR_mknod
31e31b8a 9371 case TARGET_NR_mknod:
579a97f7 9372 if (!(p = lock_user_string(arg1)))
2852aafd 9373 return -TARGET_EFAULT;
53a5960a
PB
9374 ret = get_errno(mknod(p, arg2, arg3));
9375 unlock_user(p, arg1, 0);
72eb7ea8 9376 return ret;
704eff6c 9377#endif
c0d472b1 9378#if defined(TARGET_NR_mknodat)
75ac37a0 9379 case TARGET_NR_mknodat:
579a97f7 9380 if (!(p = lock_user_string(arg2)))
2852aafd 9381 return -TARGET_EFAULT;
c0d472b1 9382 ret = get_errno(mknodat(arg1, p, arg3, arg4));
579a97f7 9383 unlock_user(p, arg2, 0);
72eb7ea8 9384 return ret;
75ac37a0 9385#endif
704eff6c 9386#ifdef TARGET_NR_chmod
31e31b8a 9387 case TARGET_NR_chmod:
579a97f7 9388 if (!(p = lock_user_string(arg1)))
2852aafd 9389 return -TARGET_EFAULT;
53a5960a
PB
9390 ret = get_errno(chmod(p, arg2));
9391 unlock_user(p, arg1, 0);
72eb7ea8 9392 return ret;
704eff6c 9393#endif
4f7f8924 9394#ifdef TARGET_NR_lseek
31e31b8a 9395 case TARGET_NR_lseek:
72eb7ea8 9396 return get_errno(lseek(arg1, arg2, arg3));
4f7f8924 9397#endif
9231733a
RH
9398#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
9399 /* Alpha specific */
7a3148a9 9400 case TARGET_NR_getxpid:
0effdc29 9401 cpu_env->ir[IR_A4] = getppid();
72eb7ea8 9402 return get_errno(getpid());
7a3148a9 9403#endif
9231733a
RH
9404#ifdef TARGET_NR_getpid
9405 case TARGET_NR_getpid:
72eb7ea8 9406 return get_errno(getpid());
9231733a 9407#endif
31e31b8a 9408 case TARGET_NR_mount:
356d771b
PB
9409 {
9410 /* need to look at the data field */
9411 void *p2, *p3;
9412
9413 if (arg1) {
9414 p = lock_user_string(arg1);
9415 if (!p) {
2852aafd 9416 return -TARGET_EFAULT;
356d771b
PB
9417 }
9418 } else {
9419 p = NULL;
9420 }
9421
9422 p2 = lock_user_string(arg2);
9423 if (!p2) {
9424 if (arg1) {
9425 unlock_user(p, arg1, 0);
9426 }
2852aafd 9427 return -TARGET_EFAULT;
356d771b
PB
9428 }
9429
9430 if (arg3) {
9431 p3 = lock_user_string(arg3);
9432 if (!p3) {
9433 if (arg1) {
579a97f7 9434 unlock_user(p, arg1, 0);
356d771b
PB
9435 }
9436 unlock_user(p2, arg2, 0);
2852aafd 9437 return -TARGET_EFAULT;
356d771b
PB
9438 }
9439 } else {
9440 p3 = NULL;
9441 }
9442
9443 /* FIXME - arg5 should be locked, but it isn't clear how to
9444 * do that since it's not guaranteed to be a NULL-terminated
9445 * string.
9446 */
9447 if (!arg5) {
9448 ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
9449 } else {
3e8f1628 9450 ret = mount(p, p2, p3, (unsigned long)arg4, g2h(cpu, arg5));
356d771b
PB
9451 }
9452 ret = get_errno(ret);
9453
9454 if (arg1) {
9455 unlock_user(p, arg1, 0);
9456 }
9457 unlock_user(p2, arg2, 0);
9458 if (arg3) {
9459 unlock_user(p3, arg3, 0);
9460 }
9461 }
72eb7ea8 9462 return ret;
6eb9dbf6
LV
9463#if defined(TARGET_NR_umount) || defined(TARGET_NR_oldumount)
9464#if defined(TARGET_NR_umount)
31e31b8a 9465 case TARGET_NR_umount:
6eb9dbf6
LV
9466#endif
9467#if defined(TARGET_NR_oldumount)
9468 case TARGET_NR_oldumount:
9469#endif
579a97f7 9470 if (!(p = lock_user_string(arg1)))
2852aafd 9471 return -TARGET_EFAULT;
53a5960a
PB
9472 ret = get_errno(umount(p));
9473 unlock_user(p, arg1, 0);
72eb7ea8 9474 return ret;
e5febef5 9475#endif
4b2d2753
TW
9476#if defined(TARGET_NR_move_mount) && defined(__NR_move_mount)
9477 case TARGET_NR_move_mount:
9478 {
9479 void *p2, *p4;
9480
9481 if (!arg2 || !arg4) {
9482 return -TARGET_EFAULT;
9483 }
9484
9485 p2 = lock_user_string(arg2);
9486 if (!p2) {
9487 return -TARGET_EFAULT;
9488 }
9489
9490 p4 = lock_user_string(arg4);
9491 if (!p4) {
9492 unlock_user(p2, arg2, 0);
9493 return -TARGET_EFAULT;
9494 }
9495 ret = get_errno(sys_move_mount(arg1, p2, arg3, p4, arg5));
9496
9497 unlock_user(p2, arg2, 0);
9498 unlock_user(p4, arg4, 0);
9499
9500 return ret;
9501 }
9502#endif
7f696cdd
TW
9503#if defined(TARGET_NR_open_tree) && defined(__NR_open_tree)
9504 case TARGET_NR_open_tree:
9505 {
9506 void *p2;
9507 int host_flags;
9508
9509 if (!arg2) {
9510 return -TARGET_EFAULT;
9511 }
9512
9513 p2 = lock_user_string(arg2);
9514 if (!p2) {
9515 return -TARGET_EFAULT;
9516 }
9517
9518 host_flags = arg3 & ~TARGET_O_CLOEXEC;
9519 if (arg3 & TARGET_O_CLOEXEC) {
9520 host_flags |= O_CLOEXEC;
9521 }
9522
9523 ret = get_errno(sys_open_tree(arg1, p2, host_flags));
9524
9525 unlock_user(p2, arg2, 0);
9526
9527 return ret;
9528 }
9529#endif
7a3148a9 9530#ifdef TARGET_NR_stime /* not on alpha */
31e31b8a
FB
9531 case TARGET_NR_stime:
9532 {
0f1f2d45
LV
9533 struct timespec ts;
9534 ts.tv_nsec = 0;
9535 if (get_user_sal(ts.tv_sec, arg1)) {
2852aafd 9536 return -TARGET_EFAULT;
0f1f2d45
LV
9537 }
9538 return get_errno(clock_settime(CLOCK_REALTIME, &ts));
31e31b8a 9539 }
7a3148a9 9540#endif
7a3148a9 9541#ifdef TARGET_NR_alarm /* not on alpha */
31e31b8a 9542 case TARGET_NR_alarm:
72eb7ea8 9543 return alarm(arg1);
7a3148a9 9544#endif
7a3148a9 9545#ifdef TARGET_NR_pause /* not on alpha */
31e31b8a 9546 case TARGET_NR_pause:
f59ec606 9547 if (!block_signals()) {
e4e5cb4a 9548 sigsuspend(&get_task_state(cpu)->signal_mask);
f59ec606 9549 }
72eb7ea8 9550 return -TARGET_EINTR;
7a3148a9 9551#endif
e5febef5 9552#ifdef TARGET_NR_utime
31e31b8a 9553 case TARGET_NR_utime:
ebc05488 9554 {
53a5960a
PB
9555 struct utimbuf tbuf, *host_tbuf;
9556 struct target_utimbuf *target_tbuf;
9557 if (arg2) {
579a97f7 9558 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
2852aafd 9559 return -TARGET_EFAULT;
cbb21eed
MB
9560 tbuf.actime = tswapal(target_tbuf->actime);
9561 tbuf.modtime = tswapal(target_tbuf->modtime);
53a5960a
PB
9562 unlock_user_struct(target_tbuf, arg2, 0);
9563 host_tbuf = &tbuf;
f72e8ff4 9564 } else {
53a5960a 9565 host_tbuf = NULL;
f72e8ff4 9566 }
579a97f7 9567 if (!(p = lock_user_string(arg1)))
2852aafd 9568 return -TARGET_EFAULT;
53a5960a
PB
9569 ret = get_errno(utime(p, host_tbuf));
9570 unlock_user(p, arg1, 0);
ebc05488 9571 }
72eb7ea8 9572 return ret;
e5febef5 9573#endif
704eff6c 9574#ifdef TARGET_NR_utimes
978a66ff
FB
9575 case TARGET_NR_utimes:
9576 {
978a66ff 9577 struct timeval *tvp, tv[2];
53a5960a 9578 if (arg2) {
788f5ec4
TS
9579 if (copy_from_user_timeval(&tv[0], arg2)
9580 || copy_from_user_timeval(&tv[1],
9581 arg2 + sizeof(struct target_timeval)))
2852aafd 9582 return -TARGET_EFAULT;
978a66ff
FB
9583 tvp = tv;
9584 } else {
9585 tvp = NULL;
9586 }
579a97f7 9587 if (!(p = lock_user_string(arg1)))
2852aafd 9588 return -TARGET_EFAULT;
53a5960a
PB
9589 ret = get_errno(utimes(p, tvp));
9590 unlock_user(p, arg1, 0);
978a66ff 9591 }
72eb7ea8 9592 return ret;
704eff6c 9593#endif
c0d472b1 9594#if defined(TARGET_NR_futimesat)
ac8a6556
AZ
9595 case TARGET_NR_futimesat:
9596 {
9597 struct timeval *tvp, tv[2];
9598 if (arg3) {
9599 if (copy_from_user_timeval(&tv[0], arg3)
9600 || copy_from_user_timeval(&tv[1],
9601 arg3 + sizeof(struct target_timeval)))
2852aafd 9602 return -TARGET_EFAULT;
ac8a6556
AZ
9603 tvp = tv;
9604 } else {
9605 tvp = NULL;
9606 }
2852aafd
RH
9607 if (!(p = lock_user_string(arg2))) {
9608 return -TARGET_EFAULT;
9609 }
c0d472b1 9610 ret = get_errno(futimesat(arg1, path(p), tvp));
ac8a6556
AZ
9611 unlock_user(p, arg2, 0);
9612 }
72eb7ea8 9613 return ret;
ac8a6556 9614#endif
704eff6c 9615#ifdef TARGET_NR_access
31e31b8a 9616 case TARGET_NR_access:
2852aafd
RH
9617 if (!(p = lock_user_string(arg1))) {
9618 return -TARGET_EFAULT;
9619 }
719f908e 9620 ret = get_errno(access(path(p), arg2));
53a5960a 9621 unlock_user(p, arg1, 0);
72eb7ea8 9622 return ret;
704eff6c 9623#endif
92a34c10
TS
9624#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
9625 case TARGET_NR_faccessat:
2852aafd
RH
9626 if (!(p = lock_user_string(arg2))) {
9627 return -TARGET_EFAULT;
9628 }
c0d472b1 9629 ret = get_errno(faccessat(arg1, p, arg3, 0));
579a97f7 9630 unlock_user(p, arg2, 0);
72eb7ea8 9631 return ret;
92a34c10 9632#endif
35a2c85f
WX
9633#if defined(TARGET_NR_faccessat2)
9634 case TARGET_NR_faccessat2:
9635 if (!(p = lock_user_string(arg2))) {
9636 return -TARGET_EFAULT;
9637 }
9638 ret = get_errno(faccessat(arg1, p, arg3, arg4));
9639 unlock_user(p, arg2, 0);
9640 return ret;
9641#endif
7a3148a9 9642#ifdef TARGET_NR_nice /* not on alpha */
31e31b8a 9643 case TARGET_NR_nice:
72eb7ea8 9644 return get_errno(nice(arg1));
ebc05488 9645#endif
31e31b8a 9646 case TARGET_NR_sync:
04369ff2 9647 sync();
72eb7ea8 9648 return 0;
5a03cd00
AM
9649#if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
9650 case TARGET_NR_syncfs:
72eb7ea8 9651 return get_errno(syncfs(arg1));
5a03cd00 9652#endif
31e31b8a 9653 case TARGET_NR_kill:
72eb7ea8 9654 return get_errno(safe_kill(arg1, target_to_host_signal(arg2)));
704eff6c 9655#ifdef TARGET_NR_rename
31e31b8a 9656 case TARGET_NR_rename:
53a5960a
PB
9657 {
9658 void *p2;
9659 p = lock_user_string(arg1);
9660 p2 = lock_user_string(arg2);
579a97f7
FB
9661 if (!p || !p2)
9662 ret = -TARGET_EFAULT;
9663 else
9664 ret = get_errno(rename(p, p2));
53a5960a
PB
9665 unlock_user(p2, arg2, 0);
9666 unlock_user(p, arg1, 0);
9667 }
72eb7ea8 9668 return ret;
704eff6c 9669#endif
c0d472b1 9670#if defined(TARGET_NR_renameat)
722183f6 9671 case TARGET_NR_renameat:
722183f6 9672 {
579a97f7 9673 void *p2;
722183f6
TS
9674 p = lock_user_string(arg2);
9675 p2 = lock_user_string(arg4);
579a97f7 9676 if (!p || !p2)
0da46a6e 9677 ret = -TARGET_EFAULT;
722183f6 9678 else
c0d472b1 9679 ret = get_errno(renameat(arg1, p, arg3, p2));
579a97f7
FB
9680 unlock_user(p2, arg4, 0);
9681 unlock_user(p, arg2, 0);
722183f6 9682 }
72eb7ea8 9683 return ret;
95d0307c
AS
9684#endif
9685#if defined(TARGET_NR_renameat2)
9686 case TARGET_NR_renameat2:
9687 {
9688 void *p2;
9689 p = lock_user_string(arg2);
9690 p2 = lock_user_string(arg4);
9691 if (!p || !p2) {
9692 ret = -TARGET_EFAULT;
9693 } else {
9694 ret = get_errno(sys_renameat2(arg1, p, arg3, p2, arg5));
9695 }
9696 unlock_user(p2, arg4, 0);
9697 unlock_user(p, arg2, 0);
9698 }
72eb7ea8 9699 return ret;
722183f6 9700#endif
704eff6c 9701#ifdef TARGET_NR_mkdir
31e31b8a 9702 case TARGET_NR_mkdir:
579a97f7 9703 if (!(p = lock_user_string(arg1)))
2852aafd 9704 return -TARGET_EFAULT;
53a5960a
PB
9705 ret = get_errno(mkdir(p, arg2));
9706 unlock_user(p, arg1, 0);
72eb7ea8 9707 return ret;
704eff6c 9708#endif
c0d472b1 9709#if defined(TARGET_NR_mkdirat)
4472ad0d 9710 case TARGET_NR_mkdirat:
579a97f7 9711 if (!(p = lock_user_string(arg2)))
2852aafd 9712 return -TARGET_EFAULT;
c0d472b1 9713 ret = get_errno(mkdirat(arg1, p, arg3));
579a97f7 9714 unlock_user(p, arg2, 0);
72eb7ea8 9715 return ret;
4472ad0d 9716#endif
704eff6c 9717#ifdef TARGET_NR_rmdir
31e31b8a 9718 case TARGET_NR_rmdir:
579a97f7 9719 if (!(p = lock_user_string(arg1)))
2852aafd 9720 return -TARGET_EFAULT;
53a5960a
PB
9721 ret = get_errno(rmdir(p));
9722 unlock_user(p, arg1, 0);
72eb7ea8 9723 return ret;
704eff6c 9724#endif
31e31b8a
FB
9725 case TARGET_NR_dup:
9726 ret = get_errno(dup(arg1));
e36800c9
LV
9727 if (ret >= 0) {
9728 fd_trans_dup(arg1, ret);
9729 }
72eb7ea8 9730 return ret;
704eff6c 9731#ifdef TARGET_NR_pipe
31e31b8a 9732 case TARGET_NR_pipe:
72eb7ea8 9733 return do_pipe(cpu_env, arg1, 0, 0);
704eff6c 9734#endif
099d6b0f
RV
9735#ifdef TARGET_NR_pipe2
9736 case TARGET_NR_pipe2:
72eb7ea8
RH
9737 return do_pipe(cpu_env, arg1,
9738 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
099d6b0f 9739#endif
31e31b8a 9740 case TARGET_NR_times:
32f36bce 9741 {
53a5960a 9742 struct target_tms *tmsp;
32f36bce
FB
9743 struct tms tms;
9744 ret = get_errno(times(&tms));
53a5960a 9745 if (arg1) {
579a97f7
FB
9746 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
9747 if (!tmsp)
2852aafd 9748 return -TARGET_EFAULT;
cbb21eed
MB
9749 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
9750 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
9751 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
9752 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
32f36bce 9753 }
c596ed17
FB
9754 if (!is_error(ret))
9755 ret = host_to_target_clock_t(ret);
32f36bce 9756 }
72eb7ea8 9757 return ret;
31e31b8a 9758 case TARGET_NR_acct:
38d840e6
AJ
9759 if (arg1 == 0) {
9760 ret = get_errno(acct(NULL));
9761 } else {
2852aafd
RH
9762 if (!(p = lock_user_string(arg1))) {
9763 return -TARGET_EFAULT;
9764 }
38d840e6
AJ
9765 ret = get_errno(acct(path(p)));
9766 unlock_user(p, arg1, 0);
9767 }
72eb7ea8 9768 return ret;
8070e7be 9769#ifdef TARGET_NR_umount2
31e31b8a 9770 case TARGET_NR_umount2:
579a97f7 9771 if (!(p = lock_user_string(arg1)))
2852aafd 9772 return -TARGET_EFAULT;
53a5960a
PB
9773 ret = get_errno(umount2(p, arg2));
9774 unlock_user(p, arg1, 0);
72eb7ea8 9775 return ret;
ebc05488 9776#endif
31e31b8a 9777 case TARGET_NR_ioctl:
72eb7ea8 9778 return do_ioctl(arg1, arg2, arg3);
47ae93cd 9779#ifdef TARGET_NR_fcntl
31e31b8a 9780 case TARGET_NR_fcntl:
72eb7ea8 9781 return do_fcntl(arg1, arg2, arg3);
ebc05488 9782#endif
31e31b8a 9783 case TARGET_NR_setpgid:
72eb7ea8 9784 return get_errno(setpgid(arg1, arg2));
31e31b8a 9785 case TARGET_NR_umask:
72eb7ea8 9786 return get_errno(umask(arg1));
31e31b8a 9787 case TARGET_NR_chroot:
579a97f7 9788 if (!(p = lock_user_string(arg1)))
2852aafd 9789 return -TARGET_EFAULT;
53a5960a
PB
9790 ret = get_errno(chroot(p));
9791 unlock_user(p, arg1, 0);
72eb7ea8 9792 return ret;
704eff6c 9793#ifdef TARGET_NR_dup2
31e31b8a
FB
9794 case TARGET_NR_dup2:
9795 ret = get_errno(dup2(arg1, arg2));
e36800c9
LV
9796 if (ret >= 0) {
9797 fd_trans_dup(arg1, arg2);
9798 }
72eb7ea8 9799 return ret;
704eff6c 9800#endif
d0927938
UH
9801#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
9802 case TARGET_NR_dup3:
10fa993a
PM
9803 {
9804 int host_flags;
9805
9806 if ((arg3 & ~TARGET_O_CLOEXEC) != 0) {
9807 return -EINVAL;
9808 }
9809 host_flags = target_to_host_bitmask(arg3, fcntl_flags_tbl);
9810 ret = get_errno(dup3(arg1, arg2, host_flags));
e36800c9
LV
9811 if (ret >= 0) {
9812 fd_trans_dup(arg1, arg2);
9813 }
72eb7ea8 9814 return ret;
10fa993a 9815 }
d0927938 9816#endif
7a3148a9 9817#ifdef TARGET_NR_getppid /* not on alpha */
31e31b8a 9818 case TARGET_NR_getppid:
72eb7ea8 9819 return get_errno(getppid());
7a3148a9 9820#endif
704eff6c 9821#ifdef TARGET_NR_getpgrp
31e31b8a 9822 case TARGET_NR_getpgrp:
72eb7ea8 9823 return get_errno(getpgrp());
704eff6c 9824#endif
31e31b8a 9825 case TARGET_NR_setsid:
72eb7ea8 9826 return get_errno(setsid());
e5febef5 9827#ifdef TARGET_NR_sigaction
31e31b8a 9828 case TARGET_NR_sigaction:
31e31b8a 9829 {
02d0de10 9830#if defined(TARGET_MIPS)
106ec879
FB
9831 struct target_sigaction act, oact, *pact, *old_act;
9832
9833 if (arg2) {
579a97f7 9834 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
2852aafd 9835 return -TARGET_EFAULT;
106ec879
FB
9836 act._sa_handler = old_act->_sa_handler;
9837 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
9838 act.sa_flags = old_act->sa_flags;
9839 unlock_user_struct(old_act, arg2, 0);
9840 pact = &act;
9841 } else {
9842 pact = NULL;
9843 }
9844
02fb28e8 9845 ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
106ec879
FB
9846
9847 if (!is_error(ret) && arg3) {
579a97f7 9848 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
2852aafd 9849 return -TARGET_EFAULT;
106ec879
FB
9850 old_act->_sa_handler = oact._sa_handler;
9851 old_act->sa_flags = oact.sa_flags;
9852 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
9853 old_act->sa_mask.sig[1] = 0;
9854 old_act->sa_mask.sig[2] = 0;
9855 old_act->sa_mask.sig[3] = 0;
9856 unlock_user_struct(old_act, arg3, 1);
9857 }
6049f4f8
RH
9858#else
9859 struct target_old_sigaction *old_act;
9860 struct target_sigaction act, oact, *pact;
9861 if (arg2) {
9862 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
2852aafd 9863 return -TARGET_EFAULT;
6049f4f8
RH
9864 act._sa_handler = old_act->_sa_handler;
9865 target_siginitset(&act.sa_mask, old_act->sa_mask);
9866 act.sa_flags = old_act->sa_flags;
ca192277 9867#ifdef TARGET_ARCH_HAS_SA_RESTORER
6049f4f8 9868 act.sa_restorer = old_act->sa_restorer;
ca192277 9869#endif
6049f4f8
RH
9870 unlock_user_struct(old_act, arg2, 0);
9871 pact = &act;
9872 } else {
9873 pact = NULL;
9874 }
02fb28e8 9875 ret = get_errno(do_sigaction(arg1, pact, &oact, 0));
6049f4f8
RH
9876 if (!is_error(ret) && arg3) {
9877 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
2852aafd 9878 return -TARGET_EFAULT;
6049f4f8
RH
9879 old_act->_sa_handler = oact._sa_handler;
9880 old_act->sa_mask = oact.sa_mask.sig[0];
9881 old_act->sa_flags = oact.sa_flags;
ca192277 9882#ifdef TARGET_ARCH_HAS_SA_RESTORER
6049f4f8 9883 old_act->sa_restorer = oact.sa_restorer;
ca192277 9884#endif
6049f4f8
RH
9885 unlock_user_struct(old_act, arg3, 1);
9886 }
388bb21a 9887#endif
31e31b8a 9888 }
72eb7ea8 9889 return ret;
e5febef5 9890#endif
66fb9763 9891 case TARGET_NR_rt_sigaction:
53a5960a 9892 {
0f6f9903
RH
9893 /*
9894 * For Alpha and SPARC this is a 5 argument syscall, with
78bfef72
PM
9895 * a 'restorer' parameter which must be copied into the
9896 * sa_restorer field of the sigaction struct.
9897 * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
9898 * and arg5 is the sigsetsize.
78bfef72 9899 */
0f6f9903
RH
9900#if defined(TARGET_ALPHA)
9901 target_ulong sigsetsize = arg4;
9902 target_ulong restorer = arg5;
9903#elif defined(TARGET_SPARC)
78bfef72
PM
9904 target_ulong restorer = arg4;
9905 target_ulong sigsetsize = arg5;
9906#else
9907 target_ulong sigsetsize = arg4;
02fb28e8 9908 target_ulong restorer = 0;
78bfef72 9909#endif
fb80439b
RH
9910 struct target_sigaction *act = NULL;
9911 struct target_sigaction *oact = NULL;
53a5960a 9912
78bfef72 9913 if (sigsetsize != sizeof(target_sigset_t)) {
72eb7ea8 9914 return -TARGET_EINVAL;
c815701e 9915 }
fb80439b
RH
9916 if (arg2 && !lock_user_struct(VERIFY_READ, act, arg2, 1)) {
9917 return -TARGET_EFAULT;
78bfef72 9918 }
fb80439b
RH
9919 if (arg3 && !lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
9920 ret = -TARGET_EFAULT;
9921 } else {
9922 ret = get_errno(do_sigaction(arg1, act, oact, restorer));
9923 if (oact) {
9924 unlock_user_struct(oact, arg3, 1);
579a97f7 9925 }
fb80439b
RH
9926 }
9927 if (act) {
53a5960a 9928 unlock_user_struct(act, arg2, 0);
fb80439b 9929 }
53a5960a 9930 }
72eb7ea8 9931 return ret;
7a3148a9 9932#ifdef TARGET_NR_sgetmask /* not on alpha */
31e31b8a 9933 case TARGET_NR_sgetmask:
66fb9763
FB
9934 {
9935 sigset_t cur_set;
992f48a0 9936 abi_ulong target_set;
3d3efba0
PM
9937 ret = do_sigprocmask(0, NULL, &cur_set);
9938 if (!ret) {
9939 host_to_target_old_sigset(&target_set, &cur_set);
9940 ret = target_set;
9941 }
66fb9763 9942 }
72eb7ea8 9943 return ret;
7a3148a9
JM
9944#endif
9945#ifdef TARGET_NR_ssetmask /* not on alpha */
31e31b8a 9946 case TARGET_NR_ssetmask:
66fb9763 9947 {
a8617d8c 9948 sigset_t set, oset;
992f48a0 9949 abi_ulong target_set = arg1;
66fb9763 9950 target_to_host_old_sigset(&set, &target_set);
3d3efba0
PM
9951 ret = do_sigprocmask(SIG_SETMASK, &set, &oset);
9952 if (!ret) {
9953 host_to_target_old_sigset(&target_set, &oset);
9954 ret = target_set;
9955 }
66fb9763 9956 }
72eb7ea8 9957 return ret;
7a3148a9 9958#endif
e5febef5 9959#ifdef TARGET_NR_sigprocmask
66fb9763
FB
9960 case TARGET_NR_sigprocmask:
9961 {
a5b3b13b
RH
9962#if defined(TARGET_ALPHA)
9963 sigset_t set, oldset;
9964 abi_ulong mask;
9965 int how;
9966
9967 switch (arg1) {
9968 case TARGET_SIG_BLOCK:
9969 how = SIG_BLOCK;
9970 break;
9971 case TARGET_SIG_UNBLOCK:
9972 how = SIG_UNBLOCK;
9973 break;
9974 case TARGET_SIG_SETMASK:
9975 how = SIG_SETMASK;
9976 break;
9977 default:
259841c1 9978 return -TARGET_EINVAL;
a5b3b13b
RH
9979 }
9980 mask = arg2;
9981 target_to_host_old_sigset(&set, &mask);
9982
3d3efba0 9983 ret = do_sigprocmask(how, &set, &oldset);
a5b3b13b
RH
9984 if (!is_error(ret)) {
9985 host_to_target_old_sigset(&mask, &oldset);
9986 ret = mask;
0effdc29 9987 cpu_env->ir[IR_V0] = 0; /* force no error */
a5b3b13b
RH
9988 }
9989#else
66fb9763 9990 sigset_t set, oldset, *set_ptr;
a5b3b13b 9991 int how;
3b46e624 9992
53a5960a 9993 if (arg2) {
ebce1719
PV
9994 p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1);
9995 if (!p) {
9996 return -TARGET_EFAULT;
9997 }
9998 target_to_host_old_sigset(&set, p);
9999 unlock_user(p, arg2, 0);
10000 set_ptr = &set;
a5b3b13b 10001 switch (arg1) {
66fb9763
FB
10002 case TARGET_SIG_BLOCK:
10003 how = SIG_BLOCK;
10004 break;
10005 case TARGET_SIG_UNBLOCK:
10006 how = SIG_UNBLOCK;
10007 break;
10008 case TARGET_SIG_SETMASK:
10009 how = SIG_SETMASK;
10010 break;
10011 default:
259841c1 10012 return -TARGET_EINVAL;
66fb9763 10013 }
66fb9763
FB
10014 } else {
10015 how = 0;
10016 set_ptr = NULL;
10017 }
3d3efba0 10018 ret = do_sigprocmask(how, set_ptr, &oldset);
53a5960a 10019 if (!is_error(ret) && arg3) {
c227f099 10020 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
2852aafd 10021 return -TARGET_EFAULT;
53a5960a 10022 host_to_target_old_sigset(p, &oldset);
c227f099 10023 unlock_user(p, arg3, sizeof(target_sigset_t));
66fb9763 10024 }
a5b3b13b 10025#endif
66fb9763 10026 }
72eb7ea8 10027 return ret;
e5febef5 10028#endif
66fb9763
FB
10029 case TARGET_NR_rt_sigprocmask:
10030 {
10031 int how = arg1;
10032 sigset_t set, oldset, *set_ptr;
3b46e624 10033
c815701e 10034 if (arg4 != sizeof(target_sigset_t)) {
72eb7ea8 10035 return -TARGET_EINVAL;
c815701e
PM
10036 }
10037
53a5960a 10038 if (arg2) {
d3ced2a5
SCW
10039 p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1);
10040 if (!p) {
10041 return -TARGET_EFAULT;
10042 }
10043 target_to_host_sigset(&set, p);
10044 unlock_user(p, arg2, 0);
10045 set_ptr = &set;
66fb9763
FB
10046 switch(how) {
10047 case TARGET_SIG_BLOCK:
10048 how = SIG_BLOCK;
10049 break;
10050 case TARGET_SIG_UNBLOCK:
10051 how = SIG_UNBLOCK;
10052 break;
10053 case TARGET_SIG_SETMASK:
10054 how = SIG_SETMASK;
10055 break;
10056 default:
259841c1 10057 return -TARGET_EINVAL;
66fb9763 10058 }
66fb9763
FB
10059 } else {
10060 how = 0;
10061 set_ptr = NULL;
10062 }
3d3efba0 10063 ret = do_sigprocmask(how, set_ptr, &oldset);
53a5960a 10064 if (!is_error(ret) && arg3) {
c227f099 10065 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
2852aafd 10066 return -TARGET_EFAULT;
53a5960a 10067 host_to_target_sigset(p, &oldset);
c227f099 10068 unlock_user(p, arg3, sizeof(target_sigset_t));
66fb9763
FB
10069 }
10070 }
72eb7ea8 10071 return ret;
e5febef5 10072#ifdef TARGET_NR_sigpending
66fb9763
FB
10073 case TARGET_NR_sigpending:
10074 {
10075 sigset_t set;
10076 ret = get_errno(sigpending(&set));
10077 if (!is_error(ret)) {
c227f099 10078 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
2852aafd 10079 return -TARGET_EFAULT;
53a5960a 10080 host_to_target_old_sigset(p, &set);
c227f099 10081 unlock_user(p, arg1, sizeof(target_sigset_t));
66fb9763
FB
10082 }
10083 }
72eb7ea8 10084 return ret;
e5febef5 10085#endif
66fb9763
FB
10086 case TARGET_NR_rt_sigpending:
10087 {
10088 sigset_t set;
c815701e
PM
10089
10090 /* Yes, this check is >, not != like most. We follow the kernel's
10091 * logic and it does it like this because it implements
10092 * NR_sigpending through the same code path, and in that case
10093 * the old_sigset_t is smaller in size.
10094 */
10095 if (arg2 > sizeof(target_sigset_t)) {
72eb7ea8 10096 return -TARGET_EINVAL;
c815701e
PM
10097 }
10098
66fb9763
FB
10099 ret = get_errno(sigpending(&set));
10100 if (!is_error(ret)) {
c227f099 10101 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
2852aafd 10102 return -TARGET_EFAULT;
53a5960a 10103 host_to_target_sigset(p, &set);
c227f099 10104 unlock_user(p, arg1, sizeof(target_sigset_t));
66fb9763
FB
10105 }
10106 }
72eb7ea8 10107 return ret;
e5febef5 10108#ifdef TARGET_NR_sigsuspend
66fb9763
FB
10109 case TARGET_NR_sigsuspend:
10110 {
0a99f093
RH
10111 sigset_t *set;
10112
f43ce12b 10113#if defined(TARGET_ALPHA)
e4e5cb4a 10114 TaskState *ts = get_task_state(cpu);
7fb5ef35
RH
10115 /* target_to_host_old_sigset will bswap back */
10116 abi_ulong mask = tswapal(arg1);
0a99f093
RH
10117 set = &ts->sigsuspend_mask;
10118 target_to_host_old_sigset(set, &mask);
f43ce12b 10119#else
0a99f093
RH
10120 ret = process_sigsuspend_mask(&set, arg1, sizeof(target_sigset_t));
10121 if (ret != 0) {
10122 return ret;
3d3efba0 10123 }
0a99f093
RH
10124#endif
10125 ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE));
10126 finish_sigsuspend_mask(ret);
66fb9763 10127 }
72eb7ea8 10128 return ret;
e5febef5 10129#endif
66fb9763
FB
10130 case TARGET_NR_rt_sigsuspend:
10131 {
0a99f093 10132 sigset_t *set;
c815701e 10133
0a99f093
RH
10134 ret = process_sigsuspend_mask(&set, arg1, arg2);
10135 if (ret != 0) {
10136 return ret;
3d3efba0 10137 }
0a99f093
RH
10138 ret = get_errno(safe_rt_sigsuspend(set, SIGSET_T_SIZE));
10139 finish_sigsuspend_mask(ret);
66fb9763 10140 }
72eb7ea8 10141 return ret;
859e8a89 10142#ifdef TARGET_NR_rt_sigtimedwait
66fb9763
FB
10143 case TARGET_NR_rt_sigtimedwait:
10144 {
66fb9763
FB
10145 sigset_t set;
10146 struct timespec uts, *puts;
10147 siginfo_t uinfo;
3b46e624 10148
c815701e 10149 if (arg4 != sizeof(target_sigset_t)) {
72eb7ea8 10150 return -TARGET_EINVAL;
c815701e
PM
10151 }
10152
c227f099 10153 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
2852aafd 10154 return -TARGET_EFAULT;
53a5960a
PB
10155 target_to_host_sigset(&set, p);
10156 unlock_user(p, arg1, 0);
10157 if (arg3) {
66fb9763 10158 puts = &uts;
4d213001
FB
10159 if (target_to_host_timespec(puts, arg3)) {
10160 return -TARGET_EFAULT;
10161 }
66fb9763
FB
10162 } else {
10163 puts = NULL;
10164 }
b3f82330
PM
10165 ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
10166 SIGSET_T_SIZE));
974a196d
PJ
10167 if (!is_error(ret)) {
10168 if (arg2) {
10169 p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
10170 0);
10171 if (!p) {
2852aafd 10172 return -TARGET_EFAULT;
974a196d
PJ
10173 }
10174 host_to_target_siginfo(p, &uinfo);
10175 unlock_user(p, arg2, sizeof(target_siginfo_t));
10176 }
10177 ret = host_to_target_signal(ret);
66fb9763
FB
10178 }
10179 }
72eb7ea8 10180 return ret;
ddcbde15
FB
10181#endif
10182#ifdef TARGET_NR_rt_sigtimedwait_time64
10183 case TARGET_NR_rt_sigtimedwait_time64:
10184 {
10185 sigset_t set;
10186 struct timespec uts, *puts;
10187 siginfo_t uinfo;
10188
10189 if (arg4 != sizeof(target_sigset_t)) {
10190 return -TARGET_EINVAL;
10191 }
10192
10193 p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1);
10194 if (!p) {
10195 return -TARGET_EFAULT;
10196 }
10197 target_to_host_sigset(&set, p);
10198 unlock_user(p, arg1, 0);
10199 if (arg3) {
10200 puts = &uts;
10201 if (target_to_host_timespec64(puts, arg3)) {
10202 return -TARGET_EFAULT;
10203 }
10204 } else {
10205 puts = NULL;
10206 }
10207 ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
10208 SIGSET_T_SIZE));
10209 if (!is_error(ret)) {
10210 if (arg2) {
10211 p = lock_user(VERIFY_WRITE, arg2,
10212 sizeof(target_siginfo_t), 0);
10213 if (!p) {
10214 return -TARGET_EFAULT;
10215 }
10216 host_to_target_siginfo(p, &uinfo);
10217 unlock_user(p, arg2, sizeof(target_siginfo_t));
10218 }
10219 ret = host_to_target_signal(ret);
10220 }
10221 }
10222 return ret;
859e8a89 10223#endif
66fb9763
FB
10224 case TARGET_NR_rt_sigqueueinfo:
10225 {
10226 siginfo_t uinfo;
4debae6f
PM
10227
10228 p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
10229 if (!p) {
2852aafd 10230 return -TARGET_EFAULT;
4debae6f 10231 }
53a5960a 10232 target_to_host_siginfo(&uinfo, p);
d8b6d892 10233 unlock_user(p, arg3, 0);
9b9145f0 10234 ret = get_errno(sys_rt_sigqueueinfo(arg1, target_to_host_signal(arg2), &uinfo));
66fb9763 10235 }
72eb7ea8 10236 return ret;
cf8b8bfc
MS
10237 case TARGET_NR_rt_tgsigqueueinfo:
10238 {
10239 siginfo_t uinfo;
10240
10241 p = lock_user(VERIFY_READ, arg4, sizeof(target_siginfo_t), 1);
10242 if (!p) {
2852aafd 10243 return -TARGET_EFAULT;
cf8b8bfc
MS
10244 }
10245 target_to_host_siginfo(&uinfo, p);
10246 unlock_user(p, arg4, 0);
9b9145f0 10247 ret = get_errno(sys_rt_tgsigqueueinfo(arg1, arg2, target_to_host_signal(arg3), &uinfo));
cf8b8bfc 10248 }
72eb7ea8 10249 return ret;
e5febef5 10250#ifdef TARGET_NR_sigreturn
66fb9763 10251 case TARGET_NR_sigreturn:
3d3efba0 10252 if (block_signals()) {
af254a27 10253 return -QEMU_ERESTARTSYS;
3d3efba0 10254 }
72eb7ea8 10255 return do_sigreturn(cpu_env);
e5febef5 10256#endif
66fb9763 10257 case TARGET_NR_rt_sigreturn:
3d3efba0 10258 if (block_signals()) {
af254a27 10259 return -QEMU_ERESTARTSYS;
3d3efba0 10260 }
72eb7ea8 10261 return do_rt_sigreturn(cpu_env);
31e31b8a 10262 case TARGET_NR_sethostname:
579a97f7 10263 if (!(p = lock_user_string(arg1)))
2852aafd 10264 return -TARGET_EFAULT;
53a5960a
PB
10265 ret = get_errno(sethostname(p, arg2));
10266 unlock_user(p, arg1, 0);
72eb7ea8 10267 return ret;
4f7f8924 10268#ifdef TARGET_NR_setrlimit
31e31b8a 10269 case TARGET_NR_setrlimit:
9de5e440 10270 {
e22b7015 10271 int resource = target_to_host_resource(arg1);
53a5960a 10272 struct target_rlimit *target_rlim;
9de5e440 10273 struct rlimit rlim;
579a97f7 10274 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
2852aafd 10275 return -TARGET_EFAULT;
81bbe906
TY
10276 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
10277 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
53a5960a 10278 unlock_user_struct(target_rlim, arg2, 0);
5dfa88f7
MF
10279 /*
10280 * If we just passed through resource limit settings for memory then
10281 * they would also apply to QEMU's own allocations, and QEMU will
10282 * crash or hang or die if its allocations fail. Ideally we would
10283 * track the guest allocations in QEMU and apply the limits ourselves.
10284 * For now, just tell the guest the call succeeded but don't actually
10285 * limit anything.
10286 */
10287 if (resource != RLIMIT_AS &&
10288 resource != RLIMIT_DATA &&
10289 resource != RLIMIT_STACK) {
10290 return get_errno(setrlimit(resource, &rlim));
10291 } else {
10292 return 0;
10293 }
9de5e440 10294 }
4f7f8924
AR
10295#endif
10296#ifdef TARGET_NR_getrlimit
31e31b8a 10297 case TARGET_NR_getrlimit:
9de5e440 10298 {
e22b7015 10299 int resource = target_to_host_resource(arg1);
53a5960a 10300 struct target_rlimit *target_rlim;
9de5e440 10301 struct rlimit rlim;
3b46e624 10302
9de5e440
FB
10303 ret = get_errno(getrlimit(resource, &rlim));
10304 if (!is_error(ret)) {
579a97f7 10305 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
2852aafd 10306 return -TARGET_EFAULT;
81bbe906
TY
10307 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
10308 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
53a5960a 10309 unlock_user_struct(target_rlim, arg2, 1);
9de5e440
FB
10310 }
10311 }
72eb7ea8 10312 return ret;
4f7f8924 10313#endif
31e31b8a 10314 case TARGET_NR_getrusage:
b409186b
FB
10315 {
10316 struct rusage rusage;
b409186b
FB
10317 ret = get_errno(getrusage(arg1, &rusage));
10318 if (!is_error(ret)) {
a39fb273 10319 ret = host_to_target_rusage(arg2, &rusage);
b409186b
FB
10320 }
10321 }
72eb7ea8 10322 return ret;
859e8a89 10323#if defined(TARGET_NR_gettimeofday)
31e31b8a
FB
10324 case TARGET_NR_gettimeofday:
10325 {
31e31b8a 10326 struct timeval tv;
a52f5f87
RH
10327 struct timezone tz;
10328
10329 ret = get_errno(gettimeofday(&tv, &tz));
31e31b8a 10330 if (!is_error(ret)) {
a52f5f87
RH
10331 if (arg1 && copy_to_user_timeval(arg1, &tv)) {
10332 return -TARGET_EFAULT;
10333 }
10334 if (arg2 && copy_to_user_timezone(arg2, &tz)) {
2852aafd 10335 return -TARGET_EFAULT;
a52f5f87 10336 }
31e31b8a
FB
10337 }
10338 }
72eb7ea8 10339 return ret;
859e8a89
AF
10340#endif
10341#if defined(TARGET_NR_settimeofday)
31e31b8a
FB
10342 case TARGET_NR_settimeofday:
10343 {
b67d8031 10344 struct timeval tv, *ptv = NULL;
ef4467e9
PB
10345 struct timezone tz, *ptz = NULL;
10346
b67d8031
PB
10347 if (arg1) {
10348 if (copy_from_user_timeval(&tv, arg1)) {
2852aafd 10349 return -TARGET_EFAULT;
b67d8031
PB
10350 }
10351 ptv = &tv;
10352 }
ef4467e9
PB
10353
10354 if (arg2) {
10355 if (copy_from_user_timezone(&tz, arg2)) {
2852aafd 10356 return -TARGET_EFAULT;
ef4467e9
PB
10357 }
10358 ptz = &tz;
10359 }
10360
72eb7ea8 10361 return get_errno(settimeofday(ptv, ptz));
31e31b8a 10362 }
859e8a89 10363#endif
9468a5d4 10364#if defined(TARGET_NR_select)
31e31b8a 10365 case TARGET_NR_select:
5457dc9e
LV
10366#if defined(TARGET_WANT_NI_OLD_SELECT)
10367 /* some architectures used to have old_select here
10368 * but now ENOSYS it.
10369 */
10370 ret = -TARGET_ENOSYS;
10371#elif defined(TARGET_WANT_OLD_SYS_SELECT)
10372 ret = do_old_select(arg1);
9468a5d4 10373#else
5457dc9e 10374 ret = do_select(arg1, arg2, arg3, arg4, arg5);
9468a5d4 10375#endif
72eb7ea8 10376 return ret;
9e42382f
RV
10377#endif
10378#ifdef TARGET_NR_pselect6
10379 case TARGET_NR_pselect6:
e5ce9688
FB
10380 return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, false);
10381#endif
10382#ifdef TARGET_NR_pselect6_time64
10383 case TARGET_NR_pselect6_time64:
10384 return do_pselect6(arg1, arg2, arg3, arg4, arg5, arg6, true);
048f6b4d 10385#endif
704eff6c 10386#ifdef TARGET_NR_symlink
31e31b8a 10387 case TARGET_NR_symlink:
53a5960a
PB
10388 {
10389 void *p2;
10390 p = lock_user_string(arg1);
10391 p2 = lock_user_string(arg2);
579a97f7
FB
10392 if (!p || !p2)
10393 ret = -TARGET_EFAULT;
10394 else
10395 ret = get_errno(symlink(p, p2));
53a5960a
PB
10396 unlock_user(p2, arg2, 0);
10397 unlock_user(p, arg1, 0);
10398 }
72eb7ea8 10399 return ret;
704eff6c 10400#endif
c0d472b1 10401#if defined(TARGET_NR_symlinkat)
f0b6243d 10402 case TARGET_NR_symlinkat:
f0b6243d 10403 {
579a97f7 10404 void *p2;
f0b6243d
TS
10405 p = lock_user_string(arg1);
10406 p2 = lock_user_string(arg3);
579a97f7 10407 if (!p || !p2)
0da46a6e 10408 ret = -TARGET_EFAULT;
f0b6243d 10409 else
c0d472b1 10410 ret = get_errno(symlinkat(p, arg2, p2));
579a97f7
FB
10411 unlock_user(p2, arg3, 0);
10412 unlock_user(p, arg1, 0);
f0b6243d 10413 }
72eb7ea8 10414 return ret;
f0b6243d 10415#endif
704eff6c 10416#ifdef TARGET_NR_readlink
31e31b8a 10417 case TARGET_NR_readlink:
53a5960a 10418 {
463d8e73 10419 void *p2;
53a5960a 10420 p = lock_user_string(arg1);
579a97f7 10421 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
a4dab0a0 10422 ret = get_errno(do_guest_readlink(p, p2, arg3));
53a5960a
PB
10423 unlock_user(p2, arg2, ret);
10424 unlock_user(p, arg1, 0);
10425 }
72eb7ea8 10426 return ret;
704eff6c 10427#endif
c0d472b1 10428#if defined(TARGET_NR_readlinkat)
5e0ccb18 10429 case TARGET_NR_readlinkat:
5e0ccb18 10430 {
579a97f7 10431 void *p2;
5e0ccb18 10432 p = lock_user_string(arg2);
579a97f7 10433 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
463d8e73
AS
10434 if (!p || !p2) {
10435 ret = -TARGET_EFAULT;
65d4830d
JN
10436 } else if (!arg4) {
10437 /* Short circuit this for the magic exe check. */
10438 ret = -TARGET_EINVAL;
463d8e73 10439 } else if (is_proc_myself((const char *)p, "exe")) {
258bec39
HD
10440 /*
10441 * Don't worry about sign mismatch as earlier mapping
10442 * logic would have thrown a bad address error.
10443 */
10444 ret = MIN(strlen(exec_path), arg4);
10445 /* We cannot NUL terminate the string. */
10446 memcpy(p2, exec_path, ret);
463d8e73 10447 } else {
c0d472b1 10448 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
463d8e73 10449 }
579a97f7
FB
10450 unlock_user(p2, arg3, ret);
10451 unlock_user(p, arg2, 0);
5e0ccb18 10452 }
72eb7ea8 10453 return ret;
5e0ccb18 10454#endif
e5febef5 10455#ifdef TARGET_NR_swapon
31e31b8a 10456 case TARGET_NR_swapon:
579a97f7 10457 if (!(p = lock_user_string(arg1)))
2852aafd 10458 return -TARGET_EFAULT;
53a5960a
PB
10459 ret = get_errno(swapon(p, arg2));
10460 unlock_user(p, arg1, 0);
72eb7ea8 10461 return ret;
e5febef5 10462#endif
31e31b8a 10463 case TARGET_NR_reboot:
c07ecc68
LV
10464 if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
10465 /* arg4 must be ignored in all other cases */
10466 p = lock_user_string(arg4);
10467 if (!p) {
2852aafd 10468 return -TARGET_EFAULT;
c07ecc68
LV
10469 }
10470 ret = get_errno(reboot(arg1, arg2, arg3, p));
10471 unlock_user(p, arg4, 0);
10472 } else {
10473 ret = get_errno(reboot(arg1, arg2, arg3, NULL));
10474 }
72eb7ea8 10475 return ret;
e5febef5 10476#ifdef TARGET_NR_mmap
31e31b8a 10477 case TARGET_NR_mmap:
09701199
AG
10478#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
10479 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
a4c075f1
UH
10480 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
10481 || defined(TARGET_S390X)
31e31b8a 10482 {
992f48a0
BS
10483 abi_ulong *v;
10484 abi_ulong v1, v2, v3, v4, v5, v6;
579a97f7 10485 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
2852aafd 10486 return -TARGET_EFAULT;
cbb21eed
MB
10487 v1 = tswapal(v[0]);
10488 v2 = tswapal(v[1]);
10489 v3 = tswapal(v[2]);
10490 v4 = tswapal(v[3]);
10491 v5 = tswapal(v[4]);
10492 v6 = tswapal(v[5]);
53a5960a 10493 unlock_user(v, arg1, 0);
9ab8d071 10494 return do_mmap(v1, v2, v3, v4, v5, v6);
31e31b8a 10495 }
31e31b8a 10496#else
ee1bf83d 10497 /* mmap pointers are always untagged */
9ab8d071 10498 return do_mmap(arg1, arg2, arg3, arg4, arg5, arg6);
31e31b8a 10499#endif
e5febef5 10500#endif
a315a145 10501#ifdef TARGET_NR_mmap2
6fb883e8 10502 case TARGET_NR_mmap2:
bb7ec043 10503#ifndef MMAP_SHIFT
c573ff67 10504#define MMAP_SHIFT 12
c573ff67 10505#endif
9ab8d071
RH
10506 return do_mmap(arg1, arg2, arg3, arg4, arg5,
10507 (off_t)(abi_ulong)arg6 << MMAP_SHIFT);
a315a145 10508#endif
31e31b8a 10509 case TARGET_NR_munmap:
ee1bf83d 10510 arg1 = cpu_untagged_addr(cpu, arg1);
72eb7ea8 10511 return get_errno(target_munmap(arg1, arg2));
9de5e440 10512 case TARGET_NR_mprotect:
ee1bf83d 10513 arg1 = cpu_untagged_addr(cpu, arg1);
97374d38 10514 {
e4e5cb4a 10515 TaskState *ts = get_task_state(cpu);
97374d38
PB
10516 /* Special hack to detect libc making the stack executable. */
10517 if ((arg3 & PROT_GROWSDOWN)
10518 && arg1 >= ts->info->stack_limit
10519 && arg1 <= ts->info->start_stack) {
10520 arg3 &= ~PROT_GROWSDOWN;
10521 arg2 = arg2 + arg1 - ts->info->stack_limit;
10522 arg1 = ts->info->stack_limit;
10523 }
10524 }
72eb7ea8 10525 return get_errno(target_mprotect(arg1, arg2, arg3));
e5febef5 10526#ifdef TARGET_NR_mremap
9de5e440 10527 case TARGET_NR_mremap:
ee1bf83d
RH
10528 arg1 = cpu_untagged_addr(cpu, arg1);
10529 /* mremap new_addr (arg5) is always untagged */
72eb7ea8 10530 return get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
e5febef5 10531#endif
53a5960a 10532 /* ??? msync/mlock/munlock are broken for softmmu. */
e5febef5 10533#ifdef TARGET_NR_msync
9de5e440 10534 case TARGET_NR_msync:
fe080593
HD
10535 return get_errno(msync(g2h(cpu, arg1), arg2,
10536 target_to_host_msync_arg(arg3)));
e5febef5
TS
10537#endif
10538#ifdef TARGET_NR_mlock
9de5e440 10539 case TARGET_NR_mlock:
3e8f1628 10540 return get_errno(mlock(g2h(cpu, arg1), arg2));
e5febef5
TS
10541#endif
10542#ifdef TARGET_NR_munlock
9de5e440 10543 case TARGET_NR_munlock:
3e8f1628 10544 return get_errno(munlock(g2h(cpu, arg1), arg2));
e5febef5
TS
10545#endif
10546#ifdef TARGET_NR_mlockall
9de5e440 10547 case TARGET_NR_mlockall:
72eb7ea8 10548 return get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
e5febef5
TS
10549#endif
10550#ifdef TARGET_NR_munlockall
9de5e440 10551 case TARGET_NR_munlockall:
72eb7ea8 10552 return get_errno(munlockall());
e5febef5 10553#endif
4f7f8924 10554#ifdef TARGET_NR_truncate
31e31b8a 10555 case TARGET_NR_truncate:
579a97f7 10556 if (!(p = lock_user_string(arg1)))
2852aafd 10557 return -TARGET_EFAULT;
53a5960a
PB
10558 ret = get_errno(truncate(p, arg2));
10559 unlock_user(p, arg1, 0);
72eb7ea8 10560 return ret;
4f7f8924
AR
10561#endif
10562#ifdef TARGET_NR_ftruncate
31e31b8a 10563 case TARGET_NR_ftruncate:
72eb7ea8 10564 return get_errno(ftruncate(arg1, arg2));
4f7f8924 10565#endif
31e31b8a 10566 case TARGET_NR_fchmod:
72eb7ea8 10567 return get_errno(fchmod(arg1, arg2));
c0d472b1 10568#if defined(TARGET_NR_fchmodat)
814d7977 10569 case TARGET_NR_fchmodat:
579a97f7 10570 if (!(p = lock_user_string(arg2)))
2852aafd 10571 return -TARGET_EFAULT;
c0d472b1 10572 ret = get_errno(fchmodat(arg1, p, arg3, 0));
579a97f7 10573 unlock_user(p, arg2, 0);
72eb7ea8 10574 return ret;
814d7977 10575#endif
31e31b8a 10576 case TARGET_NR_getpriority:
95c09828
RH
10577 /* Note that negative values are valid for getpriority, so we must
10578 differentiate based on errno settings. */
10579 errno = 0;
10580 ret = getpriority(arg1, arg2);
10581 if (ret == -1 && errno != 0) {
72eb7ea8 10582 return -host_to_target_errno(errno);
95c09828
RH
10583 }
10584#ifdef TARGET_ALPHA
10585 /* Return value is the unbiased priority. Signal no error. */
0effdc29 10586 cpu_env->ir[IR_V0] = 0;
95c09828
RH
10587#else
10588 /* Return value is a biased priority to avoid negative numbers. */
10589 ret = 20 - ret;
10590#endif
72eb7ea8 10591 return ret;
31e31b8a 10592 case TARGET_NR_setpriority:
72eb7ea8 10593 return get_errno(setpriority(arg1, arg2, arg3));
4f7f8924 10594#ifdef TARGET_NR_statfs
31e31b8a 10595 case TARGET_NR_statfs:
2852aafd
RH
10596 if (!(p = lock_user_string(arg1))) {
10597 return -TARGET_EFAULT;
10598 }
53a5960a
PB
10599 ret = get_errno(statfs(path(p), &stfs));
10600 unlock_user(p, arg1, 0);
31e31b8a
FB
10601 convert_statfs:
10602 if (!is_error(ret)) {
53a5960a 10603 struct target_statfs *target_stfs;
3b46e624 10604
579a97f7 10605 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
2852aafd 10606 return -TARGET_EFAULT;
579a97f7
FB
10607 __put_user(stfs.f_type, &target_stfs->f_type);
10608 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
10609 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
10610 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
10611 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
10612 __put_user(stfs.f_files, &target_stfs->f_files);
10613 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
10614 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
10615 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
10616 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
229d3376 10617 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
d4247ec2
SL
10618#ifdef _STATFS_F_FLAGS
10619 __put_user(stfs.f_flags, &target_stfs->f_flags);
10620#else
10621 __put_user(0, &target_stfs->f_flags);
10622#endif
229d3376 10623 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
53a5960a 10624 unlock_user_struct(target_stfs, arg2, 1);
31e31b8a 10625 }
72eb7ea8 10626 return ret;
4f7f8924
AR
10627#endif
10628#ifdef TARGET_NR_fstatfs
31e31b8a 10629 case TARGET_NR_fstatfs:
56c8f68f 10630 ret = get_errno(fstatfs(arg1, &stfs));
31e31b8a 10631 goto convert_statfs;
4f7f8924 10632#endif
56c8f68f
FB
10633#ifdef TARGET_NR_statfs64
10634 case TARGET_NR_statfs64:
2852aafd
RH
10635 if (!(p = lock_user_string(arg1))) {
10636 return -TARGET_EFAULT;
10637 }
53a5960a
PB
10638 ret = get_errno(statfs(path(p), &stfs));
10639 unlock_user(p, arg1, 0);
56c8f68f
FB
10640 convert_statfs64:
10641 if (!is_error(ret)) {
53a5960a 10642 struct target_statfs64 *target_stfs;
3b46e624 10643
579a97f7 10644 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
2852aafd 10645 return -TARGET_EFAULT;
579a97f7
FB
10646 __put_user(stfs.f_type, &target_stfs->f_type);
10647 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
10648 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
10649 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
10650 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
10651 __put_user(stfs.f_files, &target_stfs->f_files);
10652 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
10653 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
10654 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
10655 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
229d3376 10656 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
b94e2b4e
F
10657#ifdef _STATFS_F_FLAGS
10658 __put_user(stfs.f_flags, &target_stfs->f_flags);
10659#else
10660 __put_user(0, &target_stfs->f_flags);
10661#endif
229d3376 10662 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
579a97f7 10663 unlock_user_struct(target_stfs, arg3, 1);
56c8f68f 10664 }
72eb7ea8 10665 return ret;
56c8f68f
FB
10666 case TARGET_NR_fstatfs64:
10667 ret = get_errno(fstatfs(arg1, &stfs));
10668 goto convert_statfs64;
10669#endif
e5febef5 10670#ifdef TARGET_NR_socketcall
31e31b8a 10671 case TARGET_NR_socketcall:
72eb7ea8 10672 return do_socketcall(arg1, arg2);
e5febef5 10673#endif
3532fa74
FB
10674#ifdef TARGET_NR_accept
10675 case TARGET_NR_accept:
72eb7ea8 10676 return do_accept4(arg1, arg2, arg3, 0);
a94b4987
PM
10677#endif
10678#ifdef TARGET_NR_accept4
10679 case TARGET_NR_accept4:
72eb7ea8 10680 return do_accept4(arg1, arg2, arg3, arg4);
3532fa74
FB
10681#endif
10682#ifdef TARGET_NR_bind
10683 case TARGET_NR_bind:
72eb7ea8 10684 return do_bind(arg1, arg2, arg3);
3532fa74
FB
10685#endif
10686#ifdef TARGET_NR_connect
10687 case TARGET_NR_connect:
72eb7ea8 10688 return do_connect(arg1, arg2, arg3);
3532fa74
FB
10689#endif
10690#ifdef TARGET_NR_getpeername
10691 case TARGET_NR_getpeername:
72eb7ea8 10692 return do_getpeername(arg1, arg2, arg3);
3532fa74
FB
10693#endif
10694#ifdef TARGET_NR_getsockname
10695 case TARGET_NR_getsockname:
72eb7ea8 10696 return do_getsockname(arg1, arg2, arg3);
3532fa74
FB
10697#endif
10698#ifdef TARGET_NR_getsockopt
10699 case TARGET_NR_getsockopt:
72eb7ea8 10700 return do_getsockopt(arg1, arg2, arg3, arg4, arg5);
3532fa74
FB
10701#endif
10702#ifdef TARGET_NR_listen
10703 case TARGET_NR_listen:
72eb7ea8 10704 return get_errno(listen(arg1, arg2));
3532fa74
FB
10705#endif
10706#ifdef TARGET_NR_recv
10707 case TARGET_NR_recv:
72eb7ea8 10708 return do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
3532fa74
FB
10709#endif
10710#ifdef TARGET_NR_recvfrom
10711 case TARGET_NR_recvfrom:
72eb7ea8 10712 return do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
3532fa74
FB
10713#endif
10714#ifdef TARGET_NR_recvmsg
10715 case TARGET_NR_recvmsg:
72eb7ea8 10716 return do_sendrecvmsg(arg1, arg2, arg3, 0);
3532fa74
FB
10717#endif
10718#ifdef TARGET_NR_send
10719 case TARGET_NR_send:
72eb7ea8 10720 return do_sendto(arg1, arg2, arg3, arg4, 0, 0);
3532fa74
FB
10721#endif
10722#ifdef TARGET_NR_sendmsg
10723 case TARGET_NR_sendmsg:
72eb7ea8 10724 return do_sendrecvmsg(arg1, arg2, arg3, 1);
3532fa74 10725#endif
f19e00d7
AG
10726#ifdef TARGET_NR_sendmmsg
10727 case TARGET_NR_sendmmsg:
72eb7ea8 10728 return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
859e8a89
AF
10729#endif
10730#ifdef TARGET_NR_recvmmsg
f19e00d7 10731 case TARGET_NR_recvmmsg:
72eb7ea8 10732 return do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
f19e00d7 10733#endif
3532fa74
FB
10734#ifdef TARGET_NR_sendto
10735 case TARGET_NR_sendto:
72eb7ea8 10736 return do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
3532fa74
FB
10737#endif
10738#ifdef TARGET_NR_shutdown
10739 case TARGET_NR_shutdown:
72eb7ea8 10740 return get_errno(shutdown(arg1, arg2));
3532fa74 10741#endif
f894efd1
LV
10742#if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
10743 case TARGET_NR_getrandom:
10744 p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
10745 if (!p) {
2852aafd 10746 return -TARGET_EFAULT;
f894efd1
LV
10747 }
10748 ret = get_errno(getrandom(p, arg2, arg3));
10749 unlock_user(p, arg1, ret);
72eb7ea8 10750 return ret;
f894efd1 10751#endif
3532fa74
FB
10752#ifdef TARGET_NR_socket
10753 case TARGET_NR_socket:
72eb7ea8 10754 return do_socket(arg1, arg2, arg3);
3532fa74
FB
10755#endif
10756#ifdef TARGET_NR_socketpair
10757 case TARGET_NR_socketpair:
72eb7ea8 10758 return do_socketpair(arg1, arg2, arg3, arg4);
3532fa74
FB
10759#endif
10760#ifdef TARGET_NR_setsockopt
10761 case TARGET_NR_setsockopt:
72eb7ea8 10762 return do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
3532fa74 10763#endif
da2c8ad7 10764#if defined(TARGET_NR_syslog)
31e31b8a 10765 case TARGET_NR_syslog:
da2c8ad7
AM
10766 {
10767 int len = arg2;
7494b0f9 10768
da2c8ad7
AM
10769 switch (arg1) {
10770 case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */
10771 case TARGET_SYSLOG_ACTION_OPEN: /* Open log */
10772 case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
10773 case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */
10774 case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */
10775 case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */
10776 case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */
10777 case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */
72eb7ea8 10778 return get_errno(sys_syslog((int)arg1, NULL, (int)arg3));
da2c8ad7
AM
10779 case TARGET_SYSLOG_ACTION_READ: /* Read from log */
10780 case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */
10781 case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */
10782 {
da2c8ad7 10783 if (len < 0) {
259841c1 10784 return -TARGET_EINVAL;
da2c8ad7 10785 }
da2c8ad7 10786 if (len == 0) {
72eb7ea8 10787 return 0;
da2c8ad7
AM
10788 }
10789 p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
10790 if (!p) {
259841c1 10791 return -TARGET_EFAULT;
da2c8ad7
AM
10792 }
10793 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
10794 unlock_user(p, arg2, arg3);
10795 }
72eb7ea8 10796 return ret;
da2c8ad7 10797 default:
72eb7ea8 10798 return -TARGET_EINVAL;
da2c8ad7
AM
10799 }
10800 }
10801 break;
10802#endif
31e31b8a 10803 case TARGET_NR_setitimer:
66fb9763 10804 {
66fb9763
FB
10805 struct itimerval value, ovalue, *pvalue;
10806
53a5960a 10807 if (arg2) {
66fb9763 10808 pvalue = &value;
788f5ec4
TS
10809 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
10810 || copy_from_user_timeval(&pvalue->it_value,
10811 arg2 + sizeof(struct target_timeval)))
2852aafd 10812 return -TARGET_EFAULT;
66fb9763
FB
10813 } else {
10814 pvalue = NULL;
10815 }
10816 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
53a5960a 10817 if (!is_error(ret) && arg3) {
788f5ec4
TS
10818 if (copy_to_user_timeval(arg3,
10819 &ovalue.it_interval)
10820 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
10821 &ovalue.it_value))
2852aafd 10822 return -TARGET_EFAULT;
66fb9763
FB
10823 }
10824 }
72eb7ea8 10825 return ret;
31e31b8a 10826 case TARGET_NR_getitimer:
66fb9763 10827 {
66fb9763 10828 struct itimerval value;
3b46e624 10829
66fb9763 10830 ret = get_errno(getitimer(arg1, &value));
53a5960a 10831 if (!is_error(ret) && arg2) {
788f5ec4
TS
10832 if (copy_to_user_timeval(arg2,
10833 &value.it_interval)
10834 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
10835 &value.it_value))
2852aafd 10836 return -TARGET_EFAULT;
66fb9763
FB
10837 }
10838 }
72eb7ea8 10839 return ret;
704eff6c 10840#ifdef TARGET_NR_stat
31e31b8a 10841 case TARGET_NR_stat:
2852aafd
RH
10842 if (!(p = lock_user_string(arg1))) {
10843 return -TARGET_EFAULT;
10844 }
53a5960a
PB
10845 ret = get_errno(stat(path(p), &st));
10846 unlock_user(p, arg1, 0);
31e31b8a 10847 goto do_stat;
704eff6c
CG
10848#endif
10849#ifdef TARGET_NR_lstat
31e31b8a 10850 case TARGET_NR_lstat:
2852aafd
RH
10851 if (!(p = lock_user_string(arg1))) {
10852 return -TARGET_EFAULT;
10853 }
53a5960a
PB
10854 ret = get_errno(lstat(path(p), &st));
10855 unlock_user(p, arg1, 0);
31e31b8a 10856 goto do_stat;
704eff6c 10857#endif
4f7f8924 10858#ifdef TARGET_NR_fstat
31e31b8a
FB
10859 case TARGET_NR_fstat:
10860 {
10861 ret = get_errno(fstat(arg1, &st));
704eff6c 10862#if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
31e31b8a 10863 do_stat:
704eff6c 10864#endif
31e31b8a 10865 if (!is_error(ret)) {
53a5960a 10866 struct target_stat *target_st;
e3584658 10867
579a97f7 10868 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
2852aafd 10869 return -TARGET_EFAULT;
12727917 10870 memset(target_st, 0, sizeof(*target_st));
d2fd1af7
FB
10871 __put_user(st.st_dev, &target_st->st_dev);
10872 __put_user(st.st_ino, &target_st->st_ino);
10873 __put_user(st.st_mode, &target_st->st_mode);
10874 __put_user(st.st_uid, &target_st->st_uid);
10875 __put_user(st.st_gid, &target_st->st_gid);
10876 __put_user(st.st_nlink, &target_st->st_nlink);
10877 __put_user(st.st_rdev, &target_st->st_rdev);
10878 __put_user(st.st_size, &target_st->st_size);
10879 __put_user(st.st_blksize, &target_st->st_blksize);
10880 __put_user(st.st_blocks, &target_st->st_blocks);
10881 __put_user(st.st_atime, &target_st->target_st_atime);
10882 __put_user(st.st_mtime, &target_st->target_st_mtime);
10883 __put_user(st.st_ctime, &target_st->target_st_ctime);
febf6fad 10884#if defined(HAVE_STRUCT_STAT_ST_ATIM) && defined(TARGET_STAT_HAVE_NSEC)
5f992db6
CYT
10885 __put_user(st.st_atim.tv_nsec,
10886 &target_st->target_st_atime_nsec);
10887 __put_user(st.st_mtim.tv_nsec,
10888 &target_st->target_st_mtime_nsec);
10889 __put_user(st.st_ctim.tv_nsec,
10890 &target_st->target_st_ctime_nsec);
10891#endif
53a5960a 10892 unlock_user_struct(target_st, arg2, 1);
31e31b8a
FB
10893 }
10894 }
72eb7ea8 10895 return ret;
ebc05488 10896#endif
31e31b8a 10897 case TARGET_NR_vhangup:
72eb7ea8 10898 return get_errno(vhangup());
42ad6ae9
FB
10899#ifdef TARGET_NR_syscall
10900 case TARGET_NR_syscall:
72eb7ea8
RH
10901 return do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
10902 arg6, arg7, arg8, 0);
ebc05488 10903#endif
859e8a89 10904#if defined(TARGET_NR_wait4)
31e31b8a
FB
10905 case TARGET_NR_wait4:
10906 {
10907 int status;
992f48a0 10908 abi_long status_ptr = arg2;
31e31b8a 10909 struct rusage rusage, *rusage_ptr;
992f48a0 10910 abi_ulong target_rusage = arg4;
a39fb273 10911 abi_long rusage_err;
31e31b8a
FB
10912 if (target_rusage)
10913 rusage_ptr = &rusage;
10914 else
10915 rusage_ptr = NULL;
4af80a37 10916 ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
31e31b8a 10917 if (!is_error(ret)) {
5379557b 10918 if (status_ptr && ret) {
1d9d8b55 10919 status = host_to_target_waitstatus(status);
2f619698 10920 if (put_user_s32(status, status_ptr))
2852aafd 10921 return -TARGET_EFAULT;
31e31b8a 10922 }
a39fb273
PJ
10923 if (target_rusage) {
10924 rusage_err = host_to_target_rusage(target_rusage, &rusage);
10925 if (rusage_err) {
10926 ret = rusage_err;
10927 }
10928 }
31e31b8a
FB
10929 }
10930 }
72eb7ea8 10931 return ret;
859e8a89 10932#endif
e5febef5 10933#ifdef TARGET_NR_swapoff
31e31b8a 10934 case TARGET_NR_swapoff:
579a97f7 10935 if (!(p = lock_user_string(arg1)))
2852aafd 10936 return -TARGET_EFAULT;
53a5960a
PB
10937 ret = get_errno(swapoff(p));
10938 unlock_user(p, arg1, 0);
72eb7ea8 10939 return ret;
e5febef5 10940#endif
31e31b8a 10941 case TARGET_NR_sysinfo:
a5448a7d 10942 {
53a5960a 10943 struct target_sysinfo *target_value;
a5448a7d
FB
10944 struct sysinfo value;
10945 ret = get_errno(sysinfo(&value));
53a5960a 10946 if (!is_error(ret) && arg1)
a5448a7d 10947 {
579a97f7 10948 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
2852aafd 10949 return -TARGET_EFAULT;
a5448a7d
FB
10950 __put_user(value.uptime, &target_value->uptime);
10951 __put_user(value.loads[0], &target_value->loads[0]);
10952 __put_user(value.loads[1], &target_value->loads[1]);
10953 __put_user(value.loads[2], &target_value->loads[2]);
10954 __put_user(value.totalram, &target_value->totalram);
10955 __put_user(value.freeram, &target_value->freeram);
10956 __put_user(value.sharedram, &target_value->sharedram);
10957 __put_user(value.bufferram, &target_value->bufferram);
10958 __put_user(value.totalswap, &target_value->totalswap);
10959 __put_user(value.freeswap, &target_value->freeswap);
10960 __put_user(value.procs, &target_value->procs);
10961 __put_user(value.totalhigh, &target_value->totalhigh);
10962 __put_user(value.freehigh, &target_value->freehigh);
10963 __put_user(value.mem_unit, &target_value->mem_unit);
53a5960a 10964 unlock_user_struct(target_value, arg1, 1);
a5448a7d
FB
10965 }
10966 }
72eb7ea8 10967 return ret;
e5febef5 10968#ifdef TARGET_NR_ipc
31e31b8a 10969 case TARGET_NR_ipc:
72eb7ea8 10970 return do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
e5febef5 10971#endif
e5289087
AJ
10972#ifdef TARGET_NR_semget
10973 case TARGET_NR_semget:
72eb7ea8 10974 return get_errno(semget(arg1, arg2, arg3));
e5289087
AJ
10975#endif
10976#ifdef TARGET_NR_semop
10977 case TARGET_NR_semop:
cac46eb0 10978 return do_semtimedop(arg1, arg2, arg3, 0, false);
d8c08b1e
MK
10979#endif
10980#ifdef TARGET_NR_semtimedop
10981 case TARGET_NR_semtimedop:
cac46eb0
FB
10982 return do_semtimedop(arg1, arg2, arg3, arg4, false);
10983#endif
10984#ifdef TARGET_NR_semtimedop_time64
10985 case TARGET_NR_semtimedop_time64:
10986 return do_semtimedop(arg1, arg2, arg3, arg4, true);
e5289087
AJ
10987#endif
10988#ifdef TARGET_NR_semctl
10989 case TARGET_NR_semctl:
72eb7ea8 10990 return do_semctl(arg1, arg2, arg3, arg4);
e5289087 10991#endif
eeb438c1
AJ
10992#ifdef TARGET_NR_msgctl
10993 case TARGET_NR_msgctl:
72eb7ea8 10994 return do_msgctl(arg1, arg2, arg3);
eeb438c1
AJ
10995#endif
10996#ifdef TARGET_NR_msgget
10997 case TARGET_NR_msgget:
72eb7ea8 10998 return get_errno(msgget(arg1, arg2));
eeb438c1
AJ
10999#endif
11000#ifdef TARGET_NR_msgrcv
11001 case TARGET_NR_msgrcv:
72eb7ea8 11002 return do_msgrcv(arg1, arg2, arg3, arg4, arg5);
eeb438c1
AJ
11003#endif
11004#ifdef TARGET_NR_msgsnd
11005 case TARGET_NR_msgsnd:
72eb7ea8 11006 return do_msgsnd(arg1, arg2, arg3, arg4);
88a8c984
RV
11007#endif
11008#ifdef TARGET_NR_shmget
11009 case TARGET_NR_shmget:
72eb7ea8 11010 return get_errno(shmget(arg1, arg2, arg3));
88a8c984
RV
11011#endif
11012#ifdef TARGET_NR_shmctl
11013 case TARGET_NR_shmctl:
72eb7ea8 11014 return do_shmctl(arg1, arg2, arg3);
88a8c984
RV
11015#endif
11016#ifdef TARGET_NR_shmat
11017 case TARGET_NR_shmat:
225a206c 11018 return target_shmat(cpu_env, arg1, arg2, arg3);
88a8c984
RV
11019#endif
11020#ifdef TARGET_NR_shmdt
11021 case TARGET_NR_shmdt:
225a206c 11022 return target_shmdt(arg1);
eeb438c1 11023#endif
31e31b8a 11024 case TARGET_NR_fsync:
72eb7ea8 11025 return get_errno(fsync(arg1));
31e31b8a 11026 case TARGET_NR_clone:
4ce6243d
PM
11027 /* Linux manages to have three different orderings for its
11028 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
11029 * match the kernel's CONFIG_CLONE_* settings.
11030 * Microblaze is further special in that it uses a sixth
11031 * implicit argument to clone for the TLS pointer.
11032 */
11033#if defined(TARGET_MICROBLAZE)
a5b3bdcb 11034 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
4ce6243d
PM
11035#elif defined(TARGET_CLONE_BACKWARDS)
11036 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
11037#elif defined(TARGET_CLONE_BACKWARDS2)
a4c075f1 11038 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
0b6d3ae0 11039#else
4ce6243d 11040 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
0b6d3ae0 11041#endif
72eb7ea8 11042 return ret;
ec86b0fb
FB
11043#ifdef __NR_exit_group
11044 /* new thread calls */
11045 case TARGET_NR_exit_group:
708b6a64 11046 preexit_cleanup(cpu_env, arg1);
72eb7ea8 11047 return get_errno(exit_group(arg1));
ec86b0fb 11048#endif
31e31b8a 11049 case TARGET_NR_setdomainname:
579a97f7 11050 if (!(p = lock_user_string(arg1)))
2852aafd 11051 return -TARGET_EFAULT;
53a5960a
PB
11052 ret = get_errno(setdomainname(p, arg2));
11053 unlock_user(p, arg1, 0);
72eb7ea8 11054 return ret;
31e31b8a
FB
11055 case TARGET_NR_uname:
11056 /* no need to transcode because we use the linux syscall */
29e619b1
FB
11057 {
11058 struct new_utsname * buf;
3b46e624 11059
579a97f7 11060 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
2852aafd 11061 return -TARGET_EFAULT;
29e619b1
FB
11062 ret = get_errno(sys_uname(buf));
11063 if (!is_error(ret)) {
332c9781 11064 /* Overwrite the native machine name with whatever is being
29e619b1 11065 emulated. */
871f95c6
PMD
11066 g_strlcpy(buf->machine, cpu_to_uname_machine(cpu_env),
11067 sizeof(buf->machine));
c5937220 11068 /* Allow the user to override the reported release. */
332c9781
PM
11069 if (qemu_uname_release && *qemu_uname_release) {
11070 g_strlcpy(buf->release, qemu_uname_release,
11071 sizeof(buf->release));
11072 }
29e619b1 11073 }
53a5960a 11074 unlock_user_struct(buf, arg1, 1);
29e619b1 11075 }
72eb7ea8 11076 return ret;
6dbad63e 11077#ifdef TARGET_I386
31e31b8a 11078 case TARGET_NR_modify_ldt:
72eb7ea8 11079 return do_modify_ldt(cpu_env, arg1, arg2, arg3);
84409ddb 11080#if !defined(TARGET_X86_64)
5cd4393b 11081 case TARGET_NR_vm86:
72eb7ea8 11082 return do_vm86(cpu_env, arg1, arg2);
84409ddb 11083#endif
6dbad63e 11084#endif
859e8a89 11085#if defined(TARGET_NR_adjtimex)
31e31b8a 11086 case TARGET_NR_adjtimex:
19f59bce
AM
11087 {
11088 struct timex host_buf;
11089
11090 if (target_to_host_timex(&host_buf, arg1) != 0) {
2852aafd 11091 return -TARGET_EFAULT;
19f59bce
AM
11092 }
11093 ret = get_errno(adjtimex(&host_buf));
11094 if (!is_error(ret)) {
11095 if (host_to_target_timex(arg1, &host_buf) != 0) {
2852aafd 11096 return -TARGET_EFAULT;
19f59bce
AM
11097 }
11098 }
11099 }
72eb7ea8 11100 return ret;
859e8a89 11101#endif
38860a03
AM
11102#if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
11103 case TARGET_NR_clock_adjtime:
11104 {
aab74610 11105 struct timex htx;
38860a03 11106
aab74610 11107 if (target_to_host_timex(&htx, arg2) != 0) {
2852aafd 11108 return -TARGET_EFAULT;
38860a03 11109 }
aab74610
PM
11110 ret = get_errno(clock_adjtime(arg1, &htx));
11111 if (!is_error(ret) && host_to_target_timex(arg2, &htx)) {
11112 return -TARGET_EFAULT;
38860a03
AM
11113 }
11114 }
72eb7ea8 11115 return ret;
6ac03b2c
FB
11116#endif
11117#if defined(TARGET_NR_clock_adjtime64) && defined(CONFIG_CLOCK_ADJTIME)
11118 case TARGET_NR_clock_adjtime64:
11119 {
11120 struct timex htx;
11121
11122 if (target_to_host_timex64(&htx, arg2) != 0) {
11123 return -TARGET_EFAULT;
11124 }
11125 ret = get_errno(clock_adjtime(arg1, &htx));
11126 if (!is_error(ret) && host_to_target_timex64(arg2, &htx)) {
11127 return -TARGET_EFAULT;
11128 }
11129 }
11130 return ret;
38860a03 11131#endif
31e31b8a 11132 case TARGET_NR_getpgid:
72eb7ea8 11133 return get_errno(getpgid(arg1));
31e31b8a 11134 case TARGET_NR_fchdir:
72eb7ea8 11135 return get_errno(fchdir(arg1));
31e31b8a 11136 case TARGET_NR_personality:
72eb7ea8 11137 return get_errno(personality(arg1));
7a3148a9 11138#ifdef TARGET_NR__llseek /* Not on alpha */
31e31b8a
FB
11139 case TARGET_NR__llseek:
11140 {
0c1592d9 11141 int64_t res;
d35b261c 11142#if !defined(__NR_llseek)
9fea273c 11143 res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5);
0c1592d9
PM
11144 if (res == -1) {
11145 ret = get_errno(res);
11146 } else {
11147 ret = 0;
11148 }
4f2ac237 11149#else
31e31b8a 11150 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
4f2ac237 11151#endif
0c1592d9 11152 if ((ret == 0) && put_user_s64(res, arg4)) {
2852aafd 11153 return -TARGET_EFAULT;
0c1592d9 11154 }
31e31b8a 11155 }
72eb7ea8 11156 return ret;
7a3148a9 11157#endif
704eff6c 11158#ifdef TARGET_NR_getdents
31e31b8a 11159 case TARGET_NR_getdents:
fd08ddb9 11160 return do_getdents(arg1, arg2, arg3);
704eff6c 11161#endif /* TARGET_NR_getdents */
3ae43202 11162#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
dab2ed99 11163 case TARGET_NR_getdents64:
fd08ddb9 11164 return do_getdents64(arg1, arg2, arg3);
a541f297 11165#endif /* TARGET_NR_getdents64 */
9468a5d4 11166#if defined(TARGET_NR__newselect)
31e31b8a 11167 case TARGET_NR__newselect:
72eb7ea8 11168 return do_select(arg1, arg2, arg3, arg4, arg5);
e5febef5 11169#endif
e5ce9688 11170#ifdef TARGET_NR_poll
9de5e440 11171 case TARGET_NR_poll:
e5ce9688
FB
11172 return do_ppoll(arg1, arg2, arg3, arg4, arg5, false, false);
11173#endif
11174#ifdef TARGET_NR_ppoll
d8035d4c 11175 case TARGET_NR_ppoll:
e5ce9688
FB
11176 return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, false);
11177#endif
11178#ifdef TARGET_NR_ppoll_time64
11179 case TARGET_NR_ppoll_time64:
11180 return do_ppoll(arg1, arg2, arg3, arg4, arg5, true, true);
e5febef5 11181#endif
31e31b8a 11182 case TARGET_NR_flock:
9de5e440
FB
11183 /* NOTE: the flock constant seems to be the same for every
11184 Linux platform */
72eb7ea8 11185 return get_errno(safe_flock(arg1, arg2));
31e31b8a
FB
11186 case TARGET_NR_readv:
11187 {
f287b2c2
RH
11188 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
11189 if (vec != NULL) {
918c03ed 11190 ret = get_errno(safe_readv(arg1, vec, arg3));
f287b2c2
RH
11191 unlock_iovec(vec, arg2, arg3, 1);
11192 } else {
11193 ret = -host_to_target_errno(errno);
11194 }
31e31b8a 11195 }
72eb7ea8 11196 return ret;
31e31b8a
FB
11197 case TARGET_NR_writev:
11198 {
f287b2c2
RH
11199 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
11200 if (vec != NULL) {
918c03ed 11201 ret = get_errno(safe_writev(arg1, vec, arg3));
f287b2c2
RH
11202 unlock_iovec(vec, arg2, arg3, 0);
11203 } else {
11204 ret = -host_to_target_errno(errno);
11205 }
31e31b8a 11206 }
72eb7ea8 11207 return ret;
0f26386c
DJ
11208#if defined(TARGET_NR_preadv)
11209 case TARGET_NR_preadv:
11210 {
11211 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
11212 if (vec != NULL) {
9ac22517
MF
11213 unsigned long low, high;
11214
11215 target_to_host_low_high(arg4, arg5, &low, &high);
11216 ret = get_errno(safe_preadv(arg1, vec, arg3, low, high));
0f26386c
DJ
11217 unlock_iovec(vec, arg2, arg3, 1);
11218 } else {
11219 ret = -host_to_target_errno(errno);
f8d00fba
DJ
11220 }
11221 }
72eb7ea8 11222 return ret;
f8d00fba
DJ
11223#endif
11224#if defined(TARGET_NR_pwritev)
11225 case TARGET_NR_pwritev:
11226 {
11227 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
11228 if (vec != NULL) {
9ac22517
MF
11229 unsigned long low, high;
11230
11231 target_to_host_low_high(arg4, arg5, &low, &high);
11232 ret = get_errno(safe_pwritev(arg1, vec, arg3, low, high));
f8d00fba
DJ
11233 unlock_iovec(vec, arg2, arg3, 0);
11234 } else {
11235 ret = -host_to_target_errno(errno);
0f26386c
DJ
11236 }
11237 }
72eb7ea8 11238 return ret;
0f26386c 11239#endif
31e31b8a 11240 case TARGET_NR_getsid:
72eb7ea8 11241 return get_errno(getsid(arg1));
7a3148a9 11242#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
31e31b8a 11243 case TARGET_NR_fdatasync:
72eb7ea8 11244 return get_errno(fdatasync(arg1));
704eff6c 11245#endif
737de1d1
MF
11246 case TARGET_NR_sched_getaffinity:
11247 {
11248 unsigned int mask_size;
11249 unsigned long *mask;
11250
11251 /*
11252 * sched_getaffinity needs multiples of ulong, so need to take
11253 * care of mismatches between target ulong and host ulong sizes.
11254 */
11255 if (arg2 & (sizeof(abi_ulong) - 1)) {
72eb7ea8 11256 return -TARGET_EINVAL;
737de1d1
MF
11257 }
11258 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
11259
11260 mask = alloca(mask_size);
2e0a8713 11261 memset(mask, 0, mask_size);
737de1d1
MF
11262 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
11263
11264 if (!is_error(ret)) {
be3bd286
PM
11265 if (ret > arg2) {
11266 /* More data returned than the caller's buffer will fit.
11267 * This only happens if sizeof(abi_long) < sizeof(long)
11268 * and the caller passed us a buffer holding an odd number
11269 * of abi_longs. If the host kernel is actually using the
11270 * extra 4 bytes then fail EINVAL; otherwise we can just
11271 * ignore them and only copy the interesting part.
11272 */
11273 int numcpus = sysconf(_SC_NPROCESSORS_CONF);
11274 if (numcpus > arg2 * 8) {
72eb7ea8 11275 return -TARGET_EINVAL;
be3bd286
PM
11276 }
11277 ret = arg2;
11278 }
11279
5fdefcf8 11280 if (host_to_target_cpu_mask(mask, mask_size, arg3, ret)) {
2852aafd 11281 return -TARGET_EFAULT;
5fdefcf8 11282 }
737de1d1
MF
11283 }
11284 }
72eb7ea8 11285 return ret;
737de1d1
MF
11286 case TARGET_NR_sched_setaffinity:
11287 {
11288 unsigned int mask_size;
11289 unsigned long *mask;
11290
11291 /*
11292 * sched_setaffinity needs multiples of ulong, so need to take
11293 * care of mismatches between target ulong and host ulong sizes.
11294 */
11295 if (arg2 & (sizeof(abi_ulong) - 1)) {
72eb7ea8 11296 return -TARGET_EINVAL;
737de1d1
MF
11297 }
11298 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
737de1d1 11299 mask = alloca(mask_size);
2e0a8713
ST
11300
11301 ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2);
11302 if (ret) {
72eb7ea8 11303 return ret;
737de1d1 11304 }
737de1d1 11305
72eb7ea8 11306 return get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
737de1d1 11307 }
b827c3ed
ST
11308 case TARGET_NR_getcpu:
11309 {
9962b7c2
LV
11310 unsigned cpuid, node;
11311 ret = get_errno(sys_getcpu(arg1 ? &cpuid : NULL,
b827c3ed
ST
11312 arg2 ? &node : NULL,
11313 NULL));
11314 if (is_error(ret)) {
259841c1 11315 return ret;
b827c3ed 11316 }
9962b7c2 11317 if (arg1 && put_user_u32(cpuid, arg1)) {
2852aafd 11318 return -TARGET_EFAULT;
b827c3ed
ST
11319 }
11320 if (arg2 && put_user_u32(node, arg2)) {
2852aafd 11321 return -TARGET_EFAULT;
b827c3ed
ST
11322 }
11323 }
72eb7ea8 11324 return ret;
31e31b8a 11325 case TARGET_NR_sched_setparam:
5cd4393b 11326 {
407a119b 11327 struct target_sched_param *target_schp;
5cd4393b 11328 struct sched_param schp;
53a5960a 11329
a1d5c5b2
TM
11330 if (arg2 == 0) {
11331 return -TARGET_EINVAL;
11332 }
407a119b 11333 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1)) {
2852aafd 11334 return -TARGET_EFAULT;
407a119b 11335 }
5cd4393b 11336 schp.sched_priority = tswap32(target_schp->sched_priority);
53a5960a 11337 unlock_user_struct(target_schp, arg2, 0);
407a119b 11338 return get_errno(sys_sched_setparam(arg1, &schp));
5cd4393b 11339 }
31e31b8a 11340 case TARGET_NR_sched_getparam:
5cd4393b 11341 {
407a119b 11342 struct target_sched_param *target_schp;
5cd4393b 11343 struct sched_param schp;
a1d5c5b2
TM
11344
11345 if (arg2 == 0) {
11346 return -TARGET_EINVAL;
11347 }
407a119b 11348 ret = get_errno(sys_sched_getparam(arg1, &schp));
5cd4393b 11349 if (!is_error(ret)) {
407a119b 11350 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0)) {
2852aafd 11351 return -TARGET_EFAULT;
407a119b 11352 }
5cd4393b 11353 target_schp->sched_priority = tswap32(schp.sched_priority);
53a5960a 11354 unlock_user_struct(target_schp, arg2, 1);
5cd4393b
FB
11355 }
11356 }
72eb7ea8 11357 return ret;
31e31b8a 11358 case TARGET_NR_sched_setscheduler:
5cd4393b 11359 {
407a119b 11360 struct target_sched_param *target_schp;
5cd4393b 11361 struct sched_param schp;
a1d5c5b2
TM
11362 if (arg3 == 0) {
11363 return -TARGET_EINVAL;
11364 }
407a119b 11365 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1)) {
2852aafd 11366 return -TARGET_EFAULT;
407a119b 11367 }
5cd4393b 11368 schp.sched_priority = tswap32(target_schp->sched_priority);
53a5960a 11369 unlock_user_struct(target_schp, arg3, 0);
407a119b 11370 return get_errno(sys_sched_setscheduler(arg1, arg2, &schp));
5cd4393b 11371 }
31e31b8a 11372 case TARGET_NR_sched_getscheduler:
407a119b 11373 return get_errno(sys_sched_getscheduler(arg1));
45ad761c
TT
11374 case TARGET_NR_sched_getattr:
11375 {
11376 struct target_sched_attr *target_scha;
11377 struct sched_attr scha;
11378 if (arg2 == 0) {
11379 return -TARGET_EINVAL;
11380 }
11381 if (arg3 > sizeof(scha)) {
11382 arg3 = sizeof(scha);
11383 }
11384 ret = get_errno(sys_sched_getattr(arg1, &scha, arg3, arg4));
11385 if (!is_error(ret)) {
11386 target_scha = lock_user(VERIFY_WRITE, arg2, arg3, 0);
11387 if (!target_scha) {
11388 return -TARGET_EFAULT;
11389 }
11390 target_scha->size = tswap32(scha.size);
11391 target_scha->sched_policy = tswap32(scha.sched_policy);
11392 target_scha->sched_flags = tswap64(scha.sched_flags);
11393 target_scha->sched_nice = tswap32(scha.sched_nice);
11394 target_scha->sched_priority = tswap32(scha.sched_priority);
11395 target_scha->sched_runtime = tswap64(scha.sched_runtime);
11396 target_scha->sched_deadline = tswap64(scha.sched_deadline);
11397 target_scha->sched_period = tswap64(scha.sched_period);
11398 if (scha.size > offsetof(struct sched_attr, sched_util_min)) {
11399 target_scha->sched_util_min = tswap32(scha.sched_util_min);
11400 target_scha->sched_util_max = tswap32(scha.sched_util_max);
11401 }
11402 unlock_user(target_scha, arg2, arg3);
11403 }
11404 return ret;
11405 }
11406 case TARGET_NR_sched_setattr:
11407 {
11408 struct target_sched_attr *target_scha;
11409 struct sched_attr scha;
11410 uint32_t size;
11411 int zeroed;
11412 if (arg2 == 0) {
11413 return -TARGET_EINVAL;
11414 }
11415 if (get_user_u32(size, arg2)) {
11416 return -TARGET_EFAULT;
11417 }
11418 if (!size) {
11419 size = offsetof(struct target_sched_attr, sched_util_min);
11420 }
11421 if (size < offsetof(struct target_sched_attr, sched_util_min)) {
11422 if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
11423 return -TARGET_EFAULT;
11424 }
11425 return -TARGET_E2BIG;
11426 }
11427
11428 zeroed = check_zeroed_user(arg2, sizeof(struct target_sched_attr), size);
11429 if (zeroed < 0) {
11430 return zeroed;
11431 } else if (zeroed == 0) {
11432 if (put_user_u32(sizeof(struct target_sched_attr), arg2)) {
11433 return -TARGET_EFAULT;
11434 }
11435 return -TARGET_E2BIG;
11436 }
11437 if (size > sizeof(struct target_sched_attr)) {
11438 size = sizeof(struct target_sched_attr);
11439 }
11440
11441 target_scha = lock_user(VERIFY_READ, arg2, size, 1);
11442 if (!target_scha) {
11443 return -TARGET_EFAULT;
11444 }
11445 scha.size = size;
11446 scha.sched_policy = tswap32(target_scha->sched_policy);
11447 scha.sched_flags = tswap64(target_scha->sched_flags);
11448 scha.sched_nice = tswap32(target_scha->sched_nice);
11449 scha.sched_priority = tswap32(target_scha->sched_priority);
11450 scha.sched_runtime = tswap64(target_scha->sched_runtime);
11451 scha.sched_deadline = tswap64(target_scha->sched_deadline);
11452 scha.sched_period = tswap64(target_scha->sched_period);
11453 if (size > offsetof(struct target_sched_attr, sched_util_min)) {
11454 scha.sched_util_min = tswap32(target_scha->sched_util_min);
11455 scha.sched_util_max = tswap32(target_scha->sched_util_max);
11456 }
11457 unlock_user(target_scha, arg2, 0);
11458 return get_errno(sys_sched_setattr(arg1, &scha, arg3));
11459 }
31e31b8a 11460 case TARGET_NR_sched_yield:
72eb7ea8 11461 return get_errno(sched_yield());
31e31b8a 11462 case TARGET_NR_sched_get_priority_max:
72eb7ea8 11463 return get_errno(sched_get_priority_max(arg1));
31e31b8a 11464 case TARGET_NR_sched_get_priority_min:
72eb7ea8 11465 return get_errno(sched_get_priority_min(arg1));
859e8a89 11466#ifdef TARGET_NR_sched_rr_get_interval
31e31b8a 11467 case TARGET_NR_sched_rr_get_interval:
5cd4393b 11468 {
5cd4393b
FB
11469 struct timespec ts;
11470 ret = get_errno(sched_rr_get_interval(arg1, &ts));
11471 if (!is_error(ret)) {
d4290c40 11472 ret = host_to_target_timespec(arg2, &ts);
5cd4393b
FB
11473 }
11474 }
72eb7ea8 11475 return ret;
859e8a89 11476#endif
ddcbde15
FB
11477#ifdef TARGET_NR_sched_rr_get_interval_time64
11478 case TARGET_NR_sched_rr_get_interval_time64:
11479 {
11480 struct timespec ts;
11481 ret = get_errno(sched_rr_get_interval(arg1, &ts));
11482 if (!is_error(ret)) {
11483 ret = host_to_target_timespec64(arg2, &ts);
11484 }
11485 }
11486 return ret;
11487#endif
859e8a89 11488#if defined(TARGET_NR_nanosleep)
31e31b8a 11489 case TARGET_NR_nanosleep:
1b6b029e 11490 {
1b6b029e 11491 struct timespec req, rem;
53a5960a 11492 target_to_host_timespec(&req, arg1);
9e518226 11493 ret = get_errno(safe_nanosleep(&req, &rem));
53a5960a
PB
11494 if (is_error(ret) && arg2) {
11495 host_to_target_timespec(arg2, &rem);
1b6b029e
FB
11496 }
11497 }
72eb7ea8 11498 return ret;
859e8a89 11499#endif
31e31b8a 11500 case TARGET_NR_prctl:
87e9bf23 11501 return do_prctl(cpu_env, arg1, arg2, arg3, arg4, arg5);
39b9aae1 11502 break;
d2fd1af7
FB
11503#ifdef TARGET_NR_arch_prctl
11504 case TARGET_NR_arch_prctl:
72eb7ea8 11505 return do_arch_prctl(cpu_env, arg1, arg2);
d2fd1af7 11506#endif
f2c7ba15
AJ
11507#ifdef TARGET_NR_pread64
11508 case TARGET_NR_pread64:
8bf8e9df 11509 if (regpairs_aligned(cpu_env, num)) {
ae017a5b
AG
11510 arg4 = arg5;
11511 arg5 = arg6;
11512 }
2bd3f899
PM
11513 if (arg2 == 0 && arg3 == 0) {
11514 /* Special-case NULL buffer and zero length, which should succeed */
11515 p = 0;
11516 } else {
11517 p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
11518 if (!p) {
11519 return -TARGET_EFAULT;
11520 }
11521 }
f2c7ba15
AJ
11522 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
11523 unlock_user(p, arg2, ret);
72eb7ea8 11524 return ret;
f2c7ba15 11525 case TARGET_NR_pwrite64:
8bf8e9df 11526 if (regpairs_aligned(cpu_env, num)) {
ae017a5b
AG
11527 arg4 = arg5;
11528 arg5 = arg6;
11529 }
2bd3f899
PM
11530 if (arg2 == 0 && arg3 == 0) {
11531 /* Special-case NULL buffer and zero length, which should succeed */
11532 p = 0;
11533 } else {
11534 p = lock_user(VERIFY_READ, arg2, arg3, 1);
11535 if (!p) {
11536 return -TARGET_EFAULT;
11537 }
11538 }
f2c7ba15
AJ
11539 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
11540 unlock_user(p, arg2, 0);
72eb7ea8 11541 return ret;
67867308 11542#endif
31e31b8a 11543 case TARGET_NR_getcwd:
579a97f7 11544 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
2852aafd 11545 return -TARGET_EFAULT;
53a5960a
PB
11546 ret = get_errno(sys_getcwd1(p, arg2));
11547 unlock_user(p, arg1, ret);
72eb7ea8 11548 return ret;
31e31b8a
FB
11549 case TARGET_NR_capget:
11550 case TARGET_NR_capset:
e0eb210e
PM
11551 {
11552 struct target_user_cap_header *target_header;
11553 struct target_user_cap_data *target_data = NULL;
11554 struct __user_cap_header_struct header;
11555 struct __user_cap_data_struct data[2];
11556 struct __user_cap_data_struct *dataptr = NULL;
11557 int i, target_datalen;
11558 int data_items = 1;
11559
11560 if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
2852aafd 11561 return -TARGET_EFAULT;
e0eb210e
PM
11562 }
11563 header.version = tswap32(target_header->version);
11564 header.pid = tswap32(target_header->pid);
11565
ec864874 11566 if (header.version != _LINUX_CAPABILITY_VERSION) {
e0eb210e
PM
11567 /* Version 2 and up takes pointer to two user_data structs */
11568 data_items = 2;
11569 }
11570
11571 target_datalen = sizeof(*target_data) * data_items;
11572
11573 if (arg2) {
11574 if (num == TARGET_NR_capget) {
11575 target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
11576 } else {
11577 target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
11578 }
11579 if (!target_data) {
11580 unlock_user_struct(target_header, arg1, 0);
2852aafd 11581 return -TARGET_EFAULT;
e0eb210e
PM
11582 }
11583
11584 if (num == TARGET_NR_capset) {
11585 for (i = 0; i < data_items; i++) {
11586 data[i].effective = tswap32(target_data[i].effective);
11587 data[i].permitted = tswap32(target_data[i].permitted);
11588 data[i].inheritable = tswap32(target_data[i].inheritable);
11589 }
11590 }
11591
11592 dataptr = data;
11593 }
11594
11595 if (num == TARGET_NR_capget) {
11596 ret = get_errno(capget(&header, dataptr));
11597 } else {
11598 ret = get_errno(capset(&header, dataptr));
11599 }
11600
11601 /* The kernel always updates version for both capget and capset */
11602 target_header->version = tswap32(header.version);
11603 unlock_user_struct(target_header, arg1, 1);
11604
11605 if (arg2) {
11606 if (num == TARGET_NR_capget) {
11607 for (i = 0; i < data_items; i++) {
11608 target_data[i].effective = tswap32(data[i].effective);
11609 target_data[i].permitted = tswap32(data[i].permitted);
11610 target_data[i].inheritable = tswap32(data[i].inheritable);
11611 }
11612 unlock_user(target_data, arg2, target_datalen);
11613 } else {
11614 unlock_user(target_data, arg2, 0);
11615 }
11616 }
72eb7ea8 11617 return ret;
e0eb210e 11618 }
31e31b8a 11619 case TARGET_NR_sigaltstack:
6b208755 11620 return do_sigaltstack(arg1, arg2, cpu_env);
a8fd1aba
PM
11621
11622#ifdef CONFIG_SENDFILE
4f7f8924 11623#ifdef TARGET_NR_sendfile
a8fd1aba
PM
11624 case TARGET_NR_sendfile:
11625 {
11626 off_t *offp = NULL;
11627 off_t off;
11628 if (arg3) {
11629 ret = get_user_sal(off, arg3);
11630 if (is_error(ret)) {
72eb7ea8 11631 return ret;
a8fd1aba
PM
11632 }
11633 offp = &off;
11634 }
11635 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
11636 if (!is_error(ret) && arg3) {
11637 abi_long ret2 = put_user_sal(off, arg3);
11638 if (is_error(ret2)) {
11639 ret = ret2;
11640 }
11641 }
72eb7ea8 11642 return ret;
a8fd1aba 11643 }
4f7f8924 11644#endif
a8fd1aba
PM
11645#ifdef TARGET_NR_sendfile64
11646 case TARGET_NR_sendfile64:
11647 {
11648 off_t *offp = NULL;
11649 off_t off;
11650 if (arg3) {
11651 ret = get_user_s64(off, arg3);
11652 if (is_error(ret)) {
72eb7ea8 11653 return ret;
a8fd1aba
PM
11654 }
11655 offp = &off;
11656 }
11657 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
11658 if (!is_error(ret) && arg3) {
11659 abi_long ret2 = put_user_s64(off, arg3);
11660 if (is_error(ret2)) {
11661 ret = ret2;
11662 }
11663 }
72eb7ea8 11664 return ret;
a8fd1aba
PM
11665 }
11666#endif
ebc05488 11667#endif
048f6b4d 11668#ifdef TARGET_NR_vfork
31e31b8a 11669 case TARGET_NR_vfork:
72eb7ea8
RH
11670 return get_errno(do_fork(cpu_env,
11671 CLONE_VFORK | CLONE_VM | TARGET_SIGCHLD,
11672 0, 0, 0, 0));
048f6b4d 11673#endif
ebc05488 11674#ifdef TARGET_NR_ugetrlimit
31e31b8a 11675 case TARGET_NR_ugetrlimit:
728584be
FB
11676 {
11677 struct rlimit rlim;
e22b7015
WT
11678 int resource = target_to_host_resource(arg1);
11679 ret = get_errno(getrlimit(resource, &rlim));
728584be 11680 if (!is_error(ret)) {
53a5960a 11681 struct target_rlimit *target_rlim;
579a97f7 11682 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
2852aafd 11683 return -TARGET_EFAULT;
81bbe906
TY
11684 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
11685 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
53a5960a 11686 unlock_user_struct(target_rlim, arg2, 1);
728584be 11687 }
72eb7ea8 11688 return ret;
728584be 11689 }
ebc05488 11690#endif
a315a145 11691#ifdef TARGET_NR_truncate64
31e31b8a 11692 case TARGET_NR_truncate64:
579a97f7 11693 if (!(p = lock_user_string(arg1)))
2852aafd 11694 return -TARGET_EFAULT;
53a5960a
PB
11695 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
11696 unlock_user(p, arg1, 0);
72eb7ea8 11697 return ret;
a315a145
FB
11698#endif
11699#ifdef TARGET_NR_ftruncate64
31e31b8a 11700 case TARGET_NR_ftruncate64:
72eb7ea8 11701 return target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
a315a145
FB
11702#endif
11703#ifdef TARGET_NR_stat64
31e31b8a 11704 case TARGET_NR_stat64:
2852aafd
RH
11705 if (!(p = lock_user_string(arg1))) {
11706 return -TARGET_EFAULT;
11707 }
53a5960a
PB
11708 ret = get_errno(stat(path(p), &st));
11709 unlock_user(p, arg1, 0);
6a24a778
AZ
11710 if (!is_error(ret))
11711 ret = host_to_target_stat64(cpu_env, arg2, &st);
72eb7ea8 11712 return ret;
a315a145
FB
11713#endif
11714#ifdef TARGET_NR_lstat64
31e31b8a 11715 case TARGET_NR_lstat64:
2852aafd
RH
11716 if (!(p = lock_user_string(arg1))) {
11717 return -TARGET_EFAULT;
11718 }
53a5960a
PB
11719 ret = get_errno(lstat(path(p), &st));
11720 unlock_user(p, arg1, 0);
6a24a778
AZ
11721 if (!is_error(ret))
11722 ret = host_to_target_stat64(cpu_env, arg2, &st);
72eb7ea8 11723 return ret;
a315a145
FB
11724#endif
11725#ifdef TARGET_NR_fstat64
31e31b8a 11726 case TARGET_NR_fstat64:
6a24a778
AZ
11727 ret = get_errno(fstat(arg1, &st));
11728 if (!is_error(ret))
11729 ret = host_to_target_stat64(cpu_env, arg2, &st);
72eb7ea8 11730 return ret;
ce4defa0 11731#endif
c0d472b1 11732#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
9d33b76b 11733#ifdef TARGET_NR_fstatat64
6a24a778 11734 case TARGET_NR_fstatat64:
9d33b76b
AJ
11735#endif
11736#ifdef TARGET_NR_newfstatat
11737 case TARGET_NR_newfstatat:
11738#endif
2852aafd
RH
11739 if (!(p = lock_user_string(arg2))) {
11740 return -TARGET_EFAULT;
11741 }
c0d472b1 11742 ret = get_errno(fstatat(arg1, path(p), &st, arg4));
2852aafd 11743 unlock_user(p, arg2, 0);
6a24a778
AZ
11744 if (!is_error(ret))
11745 ret = host_to_target_stat64(cpu_env, arg3, &st);
72eb7ea8 11746 return ret;
a315a145 11747#endif
efa92184
AR
11748#if defined(TARGET_NR_statx)
11749 case TARGET_NR_statx:
11750 {
11751 struct target_statx *target_stx;
11752 int dirfd = arg1;
11753 int flags = arg3;
11754
11755 p = lock_user_string(arg2);
11756 if (p == NULL) {
11757 return -TARGET_EFAULT;
11758 }
11759#if defined(__NR_statx)
11760 {
11761 /*
11762 * It is assumed that struct statx is architecture independent.
11763 */
11764 struct target_statx host_stx;
11765 int mask = arg4;
11766
11767 ret = get_errno(sys_statx(dirfd, p, flags, mask, &host_stx));
11768 if (!is_error(ret)) {
11769 if (host_to_target_statx(&host_stx, arg5) != 0) {
11770 unlock_user(p, arg2, 0);
11771 return -TARGET_EFAULT;
11772 }
11773 }
11774
11775 if (ret != -TARGET_ENOSYS) {
11776 unlock_user(p, arg2, 0);
11777 return ret;
11778 }
11779 }
11780#endif
11781 ret = get_errno(fstatat(dirfd, path(p), &st, flags));
11782 unlock_user(p, arg2, 0);
11783
11784 if (!is_error(ret)) {
11785 if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) {
11786 return -TARGET_EFAULT;
11787 }
11788 memset(target_stx, 0, sizeof(*target_stx));
11789 __put_user(major(st.st_dev), &target_stx->stx_dev_major);
11790 __put_user(minor(st.st_dev), &target_stx->stx_dev_minor);
11791 __put_user(st.st_ino, &target_stx->stx_ino);
11792 __put_user(st.st_mode, &target_stx->stx_mode);
11793 __put_user(st.st_uid, &target_stx->stx_uid);
11794 __put_user(st.st_gid, &target_stx->stx_gid);
11795 __put_user(st.st_nlink, &target_stx->stx_nlink);
11796 __put_user(major(st.st_rdev), &target_stx->stx_rdev_major);
11797 __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor);
11798 __put_user(st.st_size, &target_stx->stx_size);
11799 __put_user(st.st_blksize, &target_stx->stx_blksize);
11800 __put_user(st.st_blocks, &target_stx->stx_blocks);
11801 __put_user(st.st_atime, &target_stx->stx_atime.tv_sec);
11802 __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec);
11803 __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec);
11804 unlock_user_struct(target_stx, arg5, 1);
11805 }
11806 }
11807 return ret;
11808#endif
704eff6c 11809#ifdef TARGET_NR_lchown
67867308 11810 case TARGET_NR_lchown:
579a97f7 11811 if (!(p = lock_user_string(arg1)))
2852aafd 11812 return -TARGET_EFAULT;
53a5960a
PB
11813 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
11814 unlock_user(p, arg1, 0);
72eb7ea8 11815 return ret;
704eff6c 11816#endif
0c866a7e 11817#ifdef TARGET_NR_getuid
67867308 11818 case TARGET_NR_getuid:
72eb7ea8 11819 return get_errno(high2lowuid(getuid()));
0c866a7e
RV
11820#endif
11821#ifdef TARGET_NR_getgid
67867308 11822 case TARGET_NR_getgid:
72eb7ea8 11823 return get_errno(high2lowgid(getgid()));
0c866a7e
RV
11824#endif
11825#ifdef TARGET_NR_geteuid
67867308 11826 case TARGET_NR_geteuid:
72eb7ea8 11827 return get_errno(high2lowuid(geteuid()));
0c866a7e
RV
11828#endif
11829#ifdef TARGET_NR_getegid
67867308 11830 case TARGET_NR_getegid:
72eb7ea8 11831 return get_errno(high2lowgid(getegid()));
0c866a7e 11832#endif
67867308 11833 case TARGET_NR_setreuid:
72eb7ea8 11834 return get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
67867308 11835 case TARGET_NR_setregid:
72eb7ea8 11836 return get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
67867308 11837 case TARGET_NR_getgroups:
725160fe 11838 { /* the same code as for TARGET_NR_getgroups32 */
67867308 11839 int gidsetsize = arg1;
0c866a7e 11840 target_id *target_grouplist;
1e35d327 11841 g_autofree gid_t *grouplist = NULL;
67867308
FB
11842 int i;
11843
8fbf89a9 11844 if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
1e35d327
MT
11845 return -TARGET_EINVAL;
11846 }
11847 if (gidsetsize > 0) {
11848 grouplist = g_try_new(gid_t, gidsetsize);
11849 if (!grouplist) {
11850 return -TARGET_ENOMEM;
11851 }
11852 }
67867308 11853 ret = get_errno(getgroups(gidsetsize, grouplist));
1e35d327
MT
11854 if (!is_error(ret) && gidsetsize > 0) {
11855 target_grouplist = lock_user(VERIFY_WRITE, arg2,
11856 gidsetsize * sizeof(target_id), 0);
11857 if (!target_grouplist) {
2852aafd 11858 return -TARGET_EFAULT;
1e35d327
MT
11859 }
11860 for (i = 0; i < ret; i++) {
0c866a7e 11861 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
1e35d327
MT
11862 }
11863 unlock_user(target_grouplist, arg2,
11864 gidsetsize * sizeof(target_id));
67867308 11865 }
1e35d327 11866 return ret;
67867308 11867 }
67867308 11868 case TARGET_NR_setgroups:
725160fe 11869 { /* the same code as for TARGET_NR_setgroups32 */
67867308 11870 int gidsetsize = arg1;
0c866a7e 11871 target_id *target_grouplist;
1e35d327 11872 g_autofree gid_t *grouplist = NULL;
67867308 11873 int i;
1e35d327
MT
11874
11875 if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
11876 return -TARGET_EINVAL;
11877 }
11878 if (gidsetsize > 0) {
11879 grouplist = g_try_new(gid_t, gidsetsize);
11880 if (!grouplist) {
11881 return -TARGET_ENOMEM;
11882 }
11883 target_grouplist = lock_user(VERIFY_READ, arg2,
11884 gidsetsize * sizeof(target_id), 1);
f2b79ce9 11885 if (!target_grouplist) {
259841c1 11886 return -TARGET_EFAULT;
f2b79ce9
DA
11887 }
11888 for (i = 0; i < gidsetsize; i++) {
11889 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
11890 }
1e35d327
MT
11891 unlock_user(target_grouplist, arg2,
11892 gidsetsize * sizeof(target_id));
579a97f7 11893 }
72eb7ea8 11894 return get_errno(setgroups(gidsetsize, grouplist));
67867308 11895 }
67867308 11896 case TARGET_NR_fchown:
72eb7ea8 11897 return get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
c0d472b1 11898#if defined(TARGET_NR_fchownat)
ccfa72b7 11899 case TARGET_NR_fchownat:
579a97f7 11900 if (!(p = lock_user_string(arg2)))
2852aafd 11901 return -TARGET_EFAULT;
c0d472b1
PM
11902 ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
11903 low2highgid(arg4), arg5));
579a97f7 11904 unlock_user(p, arg2, 0);
72eb7ea8 11905 return ret;
ccfa72b7 11906#endif
67867308
FB
11907#ifdef TARGET_NR_setresuid
11908 case TARGET_NR_setresuid:
72eb7ea8
RH
11909 return get_errno(sys_setresuid(low2highuid(arg1),
11910 low2highuid(arg2),
11911 low2highuid(arg3)));
67867308
FB
11912#endif
11913#ifdef TARGET_NR_getresuid
11914 case TARGET_NR_getresuid:
11915 {
53a5960a 11916 uid_t ruid, euid, suid;
67867308
FB
11917 ret = get_errno(getresuid(&ruid, &euid, &suid));
11918 if (!is_error(ret)) {
76ca310a
PM
11919 if (put_user_id(high2lowuid(ruid), arg1)
11920 || put_user_id(high2lowuid(euid), arg2)
11921 || put_user_id(high2lowuid(suid), arg3))
2852aafd 11922 return -TARGET_EFAULT;
67867308
FB
11923 }
11924 }
72eb7ea8 11925 return ret;
67867308
FB
11926#endif
11927#ifdef TARGET_NR_getresgid
11928 case TARGET_NR_setresgid:
72eb7ea8
RH
11929 return get_errno(sys_setresgid(low2highgid(arg1),
11930 low2highgid(arg2),
11931 low2highgid(arg3)));
67867308
FB
11932#endif
11933#ifdef TARGET_NR_getresgid
11934 case TARGET_NR_getresgid:
11935 {
53a5960a 11936 gid_t rgid, egid, sgid;
67867308
FB
11937 ret = get_errno(getresgid(&rgid, &egid, &sgid));
11938 if (!is_error(ret)) {
76ca310a
PM
11939 if (put_user_id(high2lowgid(rgid), arg1)
11940 || put_user_id(high2lowgid(egid), arg2)
11941 || put_user_id(high2lowgid(sgid), arg3))
2852aafd 11942 return -TARGET_EFAULT;
67867308
FB
11943 }
11944 }
72eb7ea8 11945 return ret;
67867308 11946#endif
704eff6c 11947#ifdef TARGET_NR_chown
67867308 11948 case TARGET_NR_chown:
579a97f7 11949 if (!(p = lock_user_string(arg1)))
2852aafd 11950 return -TARGET_EFAULT;
53a5960a
PB
11951 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
11952 unlock_user(p, arg1, 0);
72eb7ea8 11953 return ret;
704eff6c 11954#endif
67867308 11955 case TARGET_NR_setuid:
72eb7ea8 11956 return get_errno(sys_setuid(low2highuid(arg1)));
67867308 11957 case TARGET_NR_setgid:
72eb7ea8 11958 return get_errno(sys_setgid(low2highgid(arg1)));
67867308 11959 case TARGET_NR_setfsuid:
72eb7ea8 11960 return get_errno(setfsuid(arg1));
67867308 11961 case TARGET_NR_setfsgid:
72eb7ea8 11962 return get_errno(setfsgid(arg1));
67867308 11963
a315a145 11964#ifdef TARGET_NR_lchown32
31e31b8a 11965 case TARGET_NR_lchown32:
579a97f7 11966 if (!(p = lock_user_string(arg1)))
2852aafd 11967 return -TARGET_EFAULT;
53a5960a
PB
11968 ret = get_errno(lchown(p, arg2, arg3));
11969 unlock_user(p, arg1, 0);
72eb7ea8 11970 return ret;
a315a145
FB
11971#endif
11972#ifdef TARGET_NR_getuid32
31e31b8a 11973 case TARGET_NR_getuid32:
72eb7ea8 11974 return get_errno(getuid());
a315a145 11975#endif
64b4d28c
AJ
11976
11977#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
11978 /* Alpha specific */
11979 case TARGET_NR_getxuid:
ba0e276d
RH
11980 {
11981 uid_t euid;
11982 euid=geteuid();
0effdc29 11983 cpu_env->ir[IR_A4]=euid;
ba0e276d 11984 }
72eb7ea8 11985 return get_errno(getuid());
64b4d28c
AJ
11986#endif
11987#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
11988 /* Alpha specific */
11989 case TARGET_NR_getxgid:
ba0e276d
RH
11990 {
11991 uid_t egid;
11992 egid=getegid();
0effdc29 11993 cpu_env->ir[IR_A4]=egid;
ba0e276d 11994 }
72eb7ea8 11995 return get_errno(getgid());
64b4d28c 11996#endif
ba0e276d
RH
11997#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
11998 /* Alpha specific */
11999 case TARGET_NR_osf_getsysinfo:
12000 ret = -TARGET_EOPNOTSUPP;
12001 switch (arg1) {
12002 case TARGET_GSI_IEEE_FP_CONTROL:
12003 {
21ba8564 12004 uint64_t fpcr = cpu_alpha_load_fpcr(cpu_env);
0effdc29 12005 uint64_t swcr = cpu_env->swcr;
21ba8564
RH
12006
12007 swcr &= ~SWCR_STATUS_MASK;
12008 swcr |= (fpcr >> 35) & SWCR_STATUS_MASK;
ba0e276d
RH
12009
12010 if (put_user_u64 (swcr, arg2))
2852aafd 12011 return -TARGET_EFAULT;
ba0e276d
RH
12012 ret = 0;
12013 }
12014 break;
12015
12016 /* case GSI_IEEE_STATE_AT_SIGNAL:
12017 -- Not implemented in linux kernel.
12018 case GSI_UACPROC:
12019 -- Retrieves current unaligned access state; not much used.
12020 case GSI_PROC_TYPE:
12021 -- Retrieves implver information; surely not used.
12022 case GSI_GET_HWRPB:
12023 -- Grabs a copy of the HWRPB; surely not used.
12024 */
12025 }
72eb7ea8 12026 return ret;
ba0e276d
RH
12027#endif
12028#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
12029 /* Alpha specific */
12030 case TARGET_NR_osf_setsysinfo:
12031 ret = -TARGET_EOPNOTSUPP;
12032 switch (arg1) {
12033 case TARGET_SSI_IEEE_FP_CONTROL:
ba0e276d 12034 {
21ba8564 12035 uint64_t swcr, fpcr;
ba0e276d 12036
6e06d515 12037 if (get_user_u64 (swcr, arg2)) {
2852aafd 12038 return -TARGET_EFAULT;
6e06d515 12039 }
ba0e276d 12040
21ba8564
RH
12041 /*
12042 * The kernel calls swcr_update_status to update the
12043 * status bits from the fpcr at every point that it
12044 * could be queried. Therefore, we store the status
12045 * bits only in FPCR.
12046 */
0effdc29 12047 cpu_env->swcr = swcr & (SWCR_TRAP_ENABLE_MASK | SWCR_MAP_MASK);
21ba8564
RH
12048
12049 fpcr = cpu_alpha_load_fpcr(cpu_env);
12050 fpcr &= ((uint64_t)FPCR_DYN_MASK << 32);
12051 fpcr |= alpha_ieee_swcr_to_fpcr(swcr);
6e06d515 12052 cpu_alpha_store_fpcr(cpu_env, fpcr);
ba0e276d 12053 ret = 0;
6e06d515
RH
12054 }
12055 break;
12056
12057 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
12058 {
21ba8564 12059 uint64_t exc, fpcr, fex;
6e06d515
RH
12060
12061 if (get_user_u64(exc, arg2)) {
2852aafd 12062 return -TARGET_EFAULT;
6e06d515 12063 }
21ba8564
RH
12064 exc &= SWCR_STATUS_MASK;
12065 fpcr = cpu_alpha_load_fpcr(cpu_env);
ba0e276d 12066
21ba8564
RH
12067 /* Old exceptions are not signaled. */
12068 fex = alpha_ieee_fpcr_to_swcr(fpcr);
12069 fex = exc & ~fex;
12070 fex >>= SWCR_STATUS_TO_EXCSUM_SHIFT;
0effdc29 12071 fex &= (cpu_env)->swcr;
6e06d515 12072
21ba8564
RH
12073 /* Update the hardware fpcr. */
12074 fpcr |= alpha_ieee_swcr_to_fpcr(exc);
6e06d515 12075 cpu_alpha_store_fpcr(cpu_env, fpcr);
6e06d515 12076
21ba8564
RH
12077 if (fex) {
12078 int si_code = TARGET_FPE_FLTUNK;
6e06d515 12079 target_siginfo_t info;
21ba8564
RH
12080
12081 if (fex & SWCR_TRAP_ENABLE_DNO) {
12082 si_code = TARGET_FPE_FLTUND;
12083 }
12084 if (fex & SWCR_TRAP_ENABLE_INE) {
12085 si_code = TARGET_FPE_FLTRES;
12086 }
12087 if (fex & SWCR_TRAP_ENABLE_UNF) {
12088 si_code = TARGET_FPE_FLTUND;
12089 }
12090 if (fex & SWCR_TRAP_ENABLE_OVF) {
12091 si_code = TARGET_FPE_FLTOVF;
12092 }
12093 if (fex & SWCR_TRAP_ENABLE_DZE) {
12094 si_code = TARGET_FPE_FLTDIV;
12095 }
12096 if (fex & SWCR_TRAP_ENABLE_INV) {
12097 si_code = TARGET_FPE_FLTINV;
12098 }
12099
6e06d515
RH
12100 info.si_signo = SIGFPE;
12101 info.si_errno = 0;
12102 info.si_code = si_code;
0effdc29
PMD
12103 info._sifields._sigfault._addr = (cpu_env)->pc;
12104 queue_signal(cpu_env, info.si_signo,
9d2803f7 12105 QEMU_SI_FAULT, &info);
ba0e276d 12106 }
21ba8564 12107 ret = 0;
ba0e276d
RH
12108 }
12109 break;
12110
12111 /* case SSI_NVPAIRS:
12112 -- Used with SSIN_UACPROC to enable unaligned accesses.
12113 case SSI_IEEE_STATE_AT_SIGNAL:
12114 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
12115 -- Not implemented in linux kernel
12116 */
12117 }
72eb7ea8 12118 return ret;
ba0e276d
RH
12119#endif
12120#ifdef TARGET_NR_osf_sigprocmask
12121 /* Alpha specific. */
12122 case TARGET_NR_osf_sigprocmask:
12123 {
12124 abi_ulong mask;
bc088ba1 12125 int how;
ba0e276d
RH
12126 sigset_t set, oldset;
12127
12128 switch(arg1) {
12129 case TARGET_SIG_BLOCK:
12130 how = SIG_BLOCK;
12131 break;
12132 case TARGET_SIG_UNBLOCK:
12133 how = SIG_UNBLOCK;
12134 break;
12135 case TARGET_SIG_SETMASK:
12136 how = SIG_SETMASK;
12137 break;
12138 default:
259841c1 12139 return -TARGET_EINVAL;
ba0e276d
RH
12140 }
12141 mask = arg2;
12142 target_to_host_old_sigset(&set, &mask);
3d3efba0
PM
12143 ret = do_sigprocmask(how, &set, &oldset);
12144 if (!ret) {
12145 host_to_target_old_sigset(&mask, &oldset);
12146 ret = mask;
12147 }
ba0e276d 12148 }
72eb7ea8 12149 return ret;
ba0e276d 12150#endif
64b4d28c 12151
a315a145 12152#ifdef TARGET_NR_getgid32
31e31b8a 12153 case TARGET_NR_getgid32:
72eb7ea8 12154 return get_errno(getgid());
a315a145
FB
12155#endif
12156#ifdef TARGET_NR_geteuid32
31e31b8a 12157 case TARGET_NR_geteuid32:
72eb7ea8 12158 return get_errno(geteuid());
a315a145
FB
12159#endif
12160#ifdef TARGET_NR_getegid32
31e31b8a 12161 case TARGET_NR_getegid32:
72eb7ea8 12162 return get_errno(getegid());
a315a145
FB
12163#endif
12164#ifdef TARGET_NR_setreuid32
31e31b8a 12165 case TARGET_NR_setreuid32:
72eb7ea8 12166 return get_errno(setreuid(arg1, arg2));
a315a145
FB
12167#endif
12168#ifdef TARGET_NR_setregid32
31e31b8a 12169 case TARGET_NR_setregid32:
72eb7ea8 12170 return get_errno(setregid(arg1, arg2));
a315a145
FB
12171#endif
12172#ifdef TARGET_NR_getgroups32
31e31b8a 12173 case TARGET_NR_getgroups32:
725160fe 12174 { /* the same code as for TARGET_NR_getgroups */
99c475ab 12175 int gidsetsize = arg1;
53a5960a 12176 uint32_t *target_grouplist;
1e35d327 12177 g_autofree gid_t *grouplist = NULL;
99c475ab
FB
12178 int i;
12179
8fbf89a9 12180 if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
1e35d327
MT
12181 return -TARGET_EINVAL;
12182 }
12183 if (gidsetsize > 0) {
12184 grouplist = g_try_new(gid_t, gidsetsize);
12185 if (!grouplist) {
12186 return -TARGET_ENOMEM;
12187 }
12188 }
99c475ab 12189 ret = get_errno(getgroups(gidsetsize, grouplist));
1e35d327
MT
12190 if (!is_error(ret) && gidsetsize > 0) {
12191 target_grouplist = lock_user(VERIFY_WRITE, arg2,
12192 gidsetsize * 4, 0);
579a97f7 12193 if (!target_grouplist) {
259841c1 12194 return -TARGET_EFAULT;
579a97f7 12195 }
1e35d327 12196 for (i = 0; i < ret; i++) {
53a5960a 12197 target_grouplist[i] = tswap32(grouplist[i]);
1e35d327 12198 }
53a5960a 12199 unlock_user(target_grouplist, arg2, gidsetsize * 4);
99c475ab 12200 }
1e35d327 12201 return ret;
99c475ab 12202 }
a315a145
FB
12203#endif
12204#ifdef TARGET_NR_setgroups32
31e31b8a 12205 case TARGET_NR_setgroups32:
725160fe 12206 { /* the same code as for TARGET_NR_setgroups */
99c475ab 12207 int gidsetsize = arg1;
53a5960a 12208 uint32_t *target_grouplist;
1e35d327 12209 g_autofree gid_t *grouplist = NULL;
99c475ab 12210 int i;
3b46e624 12211
1e35d327
MT
12212 if (gidsetsize > NGROUPS_MAX || gidsetsize < 0) {
12213 return -TARGET_EINVAL;
12214 }
12215 if (gidsetsize > 0) {
12216 grouplist = g_try_new(gid_t, gidsetsize);
12217 if (!grouplist) {
12218 return -TARGET_ENOMEM;
12219 }
12220 target_grouplist = lock_user(VERIFY_READ, arg2,
12221 gidsetsize * 4, 1);
12222 if (!target_grouplist) {
12223 return -TARGET_EFAULT;
12224 }
12225 for (i = 0; i < gidsetsize; i++) {
12226 grouplist[i] = tswap32(target_grouplist[i]);
12227 }
12228 unlock_user(target_grouplist, arg2, 0);
579a97f7 12229 }
72eb7ea8 12230 return get_errno(setgroups(gidsetsize, grouplist));
99c475ab 12231 }
a315a145
FB
12232#endif
12233#ifdef TARGET_NR_fchown32
31e31b8a 12234 case TARGET_NR_fchown32:
72eb7ea8 12235 return get_errno(fchown(arg1, arg2, arg3));
a315a145
FB
12236#endif
12237#ifdef TARGET_NR_setresuid32
31e31b8a 12238 case TARGET_NR_setresuid32:
72eb7ea8 12239 return get_errno(sys_setresuid(arg1, arg2, arg3));
a315a145
FB
12240#endif
12241#ifdef TARGET_NR_getresuid32
31e31b8a 12242 case TARGET_NR_getresuid32:
b03c60f3 12243 {
53a5960a 12244 uid_t ruid, euid, suid;
b03c60f3
FB
12245 ret = get_errno(getresuid(&ruid, &euid, &suid));
12246 if (!is_error(ret)) {
2f619698
FB
12247 if (put_user_u32(ruid, arg1)
12248 || put_user_u32(euid, arg2)
12249 || put_user_u32(suid, arg3))
2852aafd 12250 return -TARGET_EFAULT;
b03c60f3
FB
12251 }
12252 }
72eb7ea8 12253 return ret;
a315a145
FB
12254#endif
12255#ifdef TARGET_NR_setresgid32
31e31b8a 12256 case TARGET_NR_setresgid32:
72eb7ea8 12257 return get_errno(sys_setresgid(arg1, arg2, arg3));
a315a145
FB
12258#endif
12259#ifdef TARGET_NR_getresgid32
31e31b8a 12260 case TARGET_NR_getresgid32:
b03c60f3 12261 {
53a5960a 12262 gid_t rgid, egid, sgid;
b03c60f3
FB
12263 ret = get_errno(getresgid(&rgid, &egid, &sgid));
12264 if (!is_error(ret)) {
2f619698
FB
12265 if (put_user_u32(rgid, arg1)
12266 || put_user_u32(egid, arg2)
12267 || put_user_u32(sgid, arg3))
2852aafd 12268 return -TARGET_EFAULT;
b03c60f3
FB
12269 }
12270 }
72eb7ea8 12271 return ret;
a315a145
FB
12272#endif
12273#ifdef TARGET_NR_chown32
31e31b8a 12274 case TARGET_NR_chown32:
579a97f7 12275 if (!(p = lock_user_string(arg1)))
2852aafd 12276 return -TARGET_EFAULT;
53a5960a
PB
12277 ret = get_errno(chown(p, arg2, arg3));
12278 unlock_user(p, arg1, 0);
72eb7ea8 12279 return ret;
a315a145
FB
12280#endif
12281#ifdef TARGET_NR_setuid32
31e31b8a 12282 case TARGET_NR_setuid32:
72eb7ea8 12283 return get_errno(sys_setuid(arg1));
a315a145
FB
12284#endif
12285#ifdef TARGET_NR_setgid32
31e31b8a 12286 case TARGET_NR_setgid32:
72eb7ea8 12287 return get_errno(sys_setgid(arg1));
a315a145
FB
12288#endif
12289#ifdef TARGET_NR_setfsuid32
31e31b8a 12290 case TARGET_NR_setfsuid32:
72eb7ea8 12291 return get_errno(setfsuid(arg1));
a315a145
FB
12292#endif
12293#ifdef TARGET_NR_setfsgid32
31e31b8a 12294 case TARGET_NR_setfsgid32:
72eb7ea8 12295 return get_errno(setfsgid(arg1));
a315a145 12296#endif
ffa65c3b 12297#ifdef TARGET_NR_mincore
31e31b8a 12298 case TARGET_NR_mincore:
04bb9ace 12299 {
f443a26c 12300 void *a = lock_user(VERIFY_NONE, arg1, arg2, 0);
98a3331a 12301 if (!a) {
259841c1 12302 return -TARGET_ENOMEM;
98a3331a 12303 }
98a3331a
FSM
12304 p = lock_user_string(arg3);
12305 if (!p) {
259841c1
RH
12306 ret = -TARGET_EFAULT;
12307 } else {
12308 ret = get_errno(mincore(a, arg2, p));
12309 unlock_user(p, arg3, ret);
98a3331a 12310 }
04bb9ace
AJ
12311 unlock_user(a, arg1, 0);
12312 }
72eb7ea8 12313 return ret;
ffa65c3b 12314#endif
408321b6
AJ
12315#ifdef TARGET_NR_arm_fadvise64_64
12316 case TARGET_NR_arm_fadvise64_64:
e0156a9d
PM
12317 /* arm_fadvise64_64 looks like fadvise64_64 but
12318 * with different argument order: fd, advice, offset, len
12319 * rather than the usual fd, offset, len, advice.
12320 * Note that offset and len are both 64-bit so appear as
12321 * pairs of 32-bit registers.
12322 */
12323 ret = posix_fadvise(arg1, target_offset64(arg3, arg4),
12324 target_offset64(arg5, arg6), arg2);
72eb7ea8 12325 return -host_to_target_errno(ret);
408321b6 12326#endif
badd3cd8 12327
eeed2291 12328#if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
badd3cd8
PM
12329
12330#ifdef TARGET_NR_fadvise64_64
12331 case TARGET_NR_fadvise64_64:
64a563dd 12332#if defined(TARGET_PPC) || defined(TARGET_XTENSA)
43046b5a
LV
12333 /* 6 args: fd, advice, offset (high, low), len (high, low) */
12334 ret = arg2;
12335 arg2 = arg3;
12336 arg3 = arg4;
12337 arg4 = arg5;
12338 arg5 = arg6;
12339 arg6 = ret;
12340#else
badd3cd8 12341 /* 6 args: fd, offset (high, low), len (high, low), advice */
8bf8e9df 12342 if (regpairs_aligned(cpu_env, num)) {
badd3cd8
PM
12343 /* offset is in (3,4), len in (5,6) and advice in 7 */
12344 arg2 = arg3;
12345 arg3 = arg4;
12346 arg4 = arg5;
12347 arg5 = arg6;
12348 arg6 = arg7;
12349 }
43046b5a 12350#endif
72eb7ea8
RH
12351 ret = posix_fadvise(arg1, target_offset64(arg2, arg3),
12352 target_offset64(arg4, arg5), arg6);
12353 return -host_to_target_errno(ret);
badd3cd8
PM
12354#endif
12355
12356#ifdef TARGET_NR_fadvise64
12357 case TARGET_NR_fadvise64:
12358 /* 5 args: fd, offset (high, low), len, advice */
8bf8e9df 12359 if (regpairs_aligned(cpu_env, num)) {
badd3cd8
PM
12360 /* offset is in (3,4), len in 5 and advice in 6 */
12361 arg2 = arg3;
12362 arg3 = arg4;
12363 arg4 = arg5;
12364 arg5 = arg6;
12365 }
72eb7ea8
RH
12366 ret = posix_fadvise(arg1, target_offset64(arg2, arg3), arg4, arg5);
12367 return -host_to_target_errno(ret);
408321b6 12368#endif
badd3cd8
PM
12369
12370#else /* not a 32-bit ABI */
e0156a9d 12371#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
408321b6
AJ
12372#ifdef TARGET_NR_fadvise64_64
12373 case TARGET_NR_fadvise64_64:
12374#endif
e72d2cc7
UH
12375#ifdef TARGET_NR_fadvise64
12376 case TARGET_NR_fadvise64:
12377#endif
12378#ifdef TARGET_S390X
12379 switch (arg4) {
12380 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
12381 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
12382 case 6: arg4 = POSIX_FADV_DONTNEED; break;
12383 case 7: arg4 = POSIX_FADV_NOREUSE; break;
12384 default: break;
12385 }
12386#endif
72eb7ea8 12387 return -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4));
408321b6 12388#endif
badd3cd8
PM
12389#endif /* end of 64-bit ABI fadvise handling */
12390
ffa65c3b 12391#ifdef TARGET_NR_madvise
31e31b8a 12392 case TARGET_NR_madvise:
892a4f6a 12393 return target_madvise(arg1, arg2, arg3);
ffa65c3b 12394#endif
bbf5f2a1 12395#ifdef TARGET_NR_fcntl64
31e31b8a 12396 case TARGET_NR_fcntl64:
77e4672d 12397 {
bbf5f2a1
AB
12398 int cmd;
12399 struct flock64 fl;
213d3e9e
PM
12400 from_flock64_fn *copyfrom = copy_from_user_flock64;
12401 to_flock64_fn *copyto = copy_to_user_flock64;
12402
ce4defa0 12403#ifdef TARGET_ARM
0effdc29 12404 if (!cpu_env->eabi) {
7f254c5c
LV
12405 copyfrom = copy_from_user_oabi_flock64;
12406 copyto = copy_to_user_oabi_flock64;
213d3e9e 12407 }
ce4defa0 12408#endif
77e4672d 12409
bbf5f2a1 12410 cmd = target_to_host_fcntl_cmd(arg2);
31b63193 12411 if (cmd == -TARGET_EINVAL) {
72eb7ea8 12412 return cmd;
31b63193 12413 }
b1e341eb 12414
60cd49d5 12415 switch(arg2) {
b1e341eb 12416 case TARGET_F_GETLK64:
213d3e9e
PM
12417 ret = copyfrom(&fl, arg3);
12418 if (ret) {
12419 break;
5813427b 12420 }
af8ab2bf 12421 ret = get_errno(safe_fcntl(arg1, cmd, &fl));
213d3e9e
PM
12422 if (ret == 0) {
12423 ret = copyto(arg3, &fl);
12424 }
77e4672d
FB
12425 break;
12426
b1e341eb
TS
12427 case TARGET_F_SETLK64:
12428 case TARGET_F_SETLKW64:
213d3e9e
PM
12429 ret = copyfrom(&fl, arg3);
12430 if (ret) {
12431 break;
ce4defa0 12432 }
435da5e7 12433 ret = get_errno(safe_fcntl(arg1, cmd, &fl));
77e4672d 12434 break;
60cd49d5 12435 default:
5f106811 12436 ret = do_fcntl(arg1, arg2, arg3);
60cd49d5
FB
12437 break;
12438 }
72eb7ea8 12439 return ret;
77e4672d 12440 }
60cd49d5 12441#endif
7d600c80
TS
12442#ifdef TARGET_NR_cacheflush
12443 case TARGET_NR_cacheflush:
12444 /* self-modifying code is handled automatically, so nothing needed */
72eb7ea8 12445 return 0;
7d600c80 12446#endif
c573ff67
FB
12447#ifdef TARGET_NR_getpagesize
12448 case TARGET_NR_getpagesize:
72eb7ea8 12449 return TARGET_PAGE_SIZE;
ebc05488 12450#endif
31e31b8a 12451 case TARGET_NR_gettid:
71ba74f6 12452 return get_errno(sys_gettid());
e5febef5 12453#ifdef TARGET_NR_readahead
31e31b8a 12454 case TARGET_NR_readahead:
eeed2291 12455#if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
8bf8e9df 12456 if (regpairs_aligned(cpu_env, num)) {
2054ac9b
AJ
12457 arg2 = arg3;
12458 arg3 = arg4;
12459 arg4 = arg5;
12460 }
77c6850f 12461 ret = get_errno(readahead(arg1, target_offset64(arg2, arg3) , arg4));
2054ac9b
AJ
12462#else
12463 ret = get_errno(readahead(arg1, arg2, arg3));
12464#endif
72eb7ea8 12465 return ret;
e5febef5 12466#endif
a790ae38 12467#ifdef CONFIG_ATTR
ebc05488 12468#ifdef TARGET_NR_setxattr
31e31b8a
FB
12469 case TARGET_NR_listxattr:
12470 case TARGET_NR_llistxattr:
fb5590f7 12471 {
77c9f177 12472 void *b = 0;
fb5590f7
PM
12473 if (arg2) {
12474 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
12475 if (!b) {
72eb7ea8 12476 return -TARGET_EFAULT;
fb5590f7
PM
12477 }
12478 }
12479 p = lock_user_string(arg1);
12480 if (p) {
12481 if (num == TARGET_NR_listxattr) {
12482 ret = get_errno(listxattr(p, b, arg3));
12483 } else {
12484 ret = get_errno(llistxattr(p, b, arg3));
12485 }
12486 } else {
12487 ret = -TARGET_EFAULT;
12488 }
12489 unlock_user(p, arg1, 0);
12490 unlock_user(b, arg2, arg3);
72eb7ea8 12491 return ret;
fb5590f7 12492 }
31e31b8a 12493 case TARGET_NR_flistxattr:
fb5590f7
PM
12494 {
12495 void *b = 0;
12496 if (arg2) {
12497 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
12498 if (!b) {
72eb7ea8 12499 return -TARGET_EFAULT;
fb5590f7
PM
12500 }
12501 }
12502 ret = get_errno(flistxattr(arg1, b, arg3));
12503 unlock_user(b, arg2, arg3);
72eb7ea8 12504 return ret;
fb5590f7 12505 }
a790ae38 12506 case TARGET_NR_setxattr:
30297b55 12507 case TARGET_NR_lsetxattr:
a790ae38 12508 {
77c9f177 12509 void *n, *v = 0;
e3c33ec6
PM
12510 if (arg3) {
12511 v = lock_user(VERIFY_READ, arg3, arg4, 1);
12512 if (!v) {
72eb7ea8 12513 return -TARGET_EFAULT;
e3c33ec6
PM
12514 }
12515 }
a790ae38
ACH
12516 p = lock_user_string(arg1);
12517 n = lock_user_string(arg2);
e3c33ec6 12518 if (p && n) {
30297b55
PM
12519 if (num == TARGET_NR_setxattr) {
12520 ret = get_errno(setxattr(p, n, v, arg4, arg5));
12521 } else {
12522 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
12523 }
a790ae38
ACH
12524 } else {
12525 ret = -TARGET_EFAULT;
12526 }
12527 unlock_user(p, arg1, 0);
12528 unlock_user(n, arg2, 0);
12529 unlock_user(v, arg3, 0);
12530 }
72eb7ea8 12531 return ret;
30297b55
PM
12532 case TARGET_NR_fsetxattr:
12533 {
12534 void *n, *v = 0;
12535 if (arg3) {
12536 v = lock_user(VERIFY_READ, arg3, arg4, 1);
12537 if (!v) {
72eb7ea8 12538 return -TARGET_EFAULT;
30297b55
PM
12539 }
12540 }
12541 n = lock_user_string(arg2);
12542 if (n) {
12543 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
12544 } else {
12545 ret = -TARGET_EFAULT;
12546 }
12547 unlock_user(n, arg2, 0);
12548 unlock_user(v, arg3, 0);
12549 }
72eb7ea8 12550 return ret;
a790ae38 12551 case TARGET_NR_getxattr:
30297b55 12552 case TARGET_NR_lgetxattr:
a790ae38 12553 {
77c9f177 12554 void *n, *v = 0;
e3c33ec6
PM
12555 if (arg3) {
12556 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
12557 if (!v) {
72eb7ea8 12558 return -TARGET_EFAULT;
e3c33ec6
PM
12559 }
12560 }
a790ae38
ACH
12561 p = lock_user_string(arg1);
12562 n = lock_user_string(arg2);
e3c33ec6 12563 if (p && n) {
30297b55
PM
12564 if (num == TARGET_NR_getxattr) {
12565 ret = get_errno(getxattr(p, n, v, arg4));
12566 } else {
12567 ret = get_errno(lgetxattr(p, n, v, arg4));
12568 }
a790ae38
ACH
12569 } else {
12570 ret = -TARGET_EFAULT;
12571 }
12572 unlock_user(p, arg1, 0);
12573 unlock_user(n, arg2, 0);
12574 unlock_user(v, arg3, arg4);
12575 }
72eb7ea8 12576 return ret;
30297b55
PM
12577 case TARGET_NR_fgetxattr:
12578 {
12579 void *n, *v = 0;
12580 if (arg3) {
12581 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
12582 if (!v) {
72eb7ea8 12583 return -TARGET_EFAULT;
30297b55
PM
12584 }
12585 }
12586 n = lock_user_string(arg2);
12587 if (n) {
12588 ret = get_errno(fgetxattr(arg1, n, v, arg4));
12589 } else {
12590 ret = -TARGET_EFAULT;
12591 }
12592 unlock_user(n, arg2, 0);
12593 unlock_user(v, arg3, arg4);
12594 }
72eb7ea8 12595 return ret;
a790ae38 12596 case TARGET_NR_removexattr:
30297b55 12597 case TARGET_NR_lremovexattr:
a790ae38 12598 {
77c9f177 12599 void *n;
a790ae38
ACH
12600 p = lock_user_string(arg1);
12601 n = lock_user_string(arg2);
12602 if (p && n) {
30297b55
PM
12603 if (num == TARGET_NR_removexattr) {
12604 ret = get_errno(removexattr(p, n));
12605 } else {
12606 ret = get_errno(lremovexattr(p, n));
12607 }
a790ae38
ACH
12608 } else {
12609 ret = -TARGET_EFAULT;
12610 }
12611 unlock_user(p, arg1, 0);
12612 unlock_user(n, arg2, 0);
12613 }
72eb7ea8 12614 return ret;
30297b55
PM
12615 case TARGET_NR_fremovexattr:
12616 {
12617 void *n;
12618 n = lock_user_string(arg2);
12619 if (n) {
12620 ret = get_errno(fremovexattr(arg1, n));
12621 } else {
12622 ret = -TARGET_EFAULT;
12623 }
12624 unlock_user(n, arg2, 0);
12625 }
72eb7ea8 12626 return ret;
ebc05488 12627#endif
a790ae38 12628#endif /* CONFIG_ATTR */
ebc05488 12629#ifdef TARGET_NR_set_thread_area
5cd4393b 12630 case TARGET_NR_set_thread_area:
8d18e893 12631#if defined(TARGET_MIPS)
0effdc29 12632 cpu_env->active_tc.CP0_UserLocal = arg1;
72eb7ea8 12633 return 0;
ef96779b
EI
12634#elif defined(TARGET_CRIS)
12635 if (arg1 & 0xff)
12636 ret = -TARGET_EINVAL;
12637 else {
0effdc29 12638 cpu_env->pregs[PR_PID] = arg1;
ef96779b
EI
12639 ret = 0;
12640 }
72eb7ea8 12641 return ret;
8d18e893 12642#elif defined(TARGET_I386) && defined(TARGET_ABI32)
72eb7ea8 12643 return do_set_thread_area(cpu_env, arg1);
1ccd9374
PM
12644#elif defined(TARGET_M68K)
12645 {
e4e5cb4a 12646 TaskState *ts = get_task_state(cpu);
1ccd9374 12647 ts->tp_value = arg1;
72eb7ea8 12648 return 0;
1ccd9374 12649 }
6f5b89a0 12650#else
10f45d98 12651 return -TARGET_ENOSYS;
6f5b89a0
TS
12652#endif
12653#endif
12654#ifdef TARGET_NR_get_thread_area
5cd4393b 12655 case TARGET_NR_get_thread_area:
8d18e893 12656#if defined(TARGET_I386) && defined(TARGET_ABI32)
72eb7ea8 12657 return do_get_thread_area(cpu_env, arg1);
1ccd9374
PM
12658#elif defined(TARGET_M68K)
12659 {
e4e5cb4a 12660 TaskState *ts = get_task_state(cpu);
72eb7ea8 12661 return ts->tp_value;
1ccd9374 12662 }
8d18e893 12663#else
10f45d98 12664 return -TARGET_ENOSYS;
48dc41eb 12665#endif
8d18e893 12666#endif
48dc41eb
FB
12667#ifdef TARGET_NR_getdomainname
12668 case TARGET_NR_getdomainname:
10f45d98 12669 return -TARGET_ENOSYS;
ebc05488 12670#endif
6f5b89a0 12671
12e3340c
MF
12672#ifdef TARGET_NR_clock_settime
12673 case TARGET_NR_clock_settime:
12674 {
12675 struct timespec ts;
12676
12677 ret = target_to_host_timespec(&ts, arg2);
12678 if (!is_error(ret)) {
12679 ret = get_errno(clock_settime(arg1, &ts));
12680 }
72eb7ea8 12681 return ret;
12e3340c
MF
12682 }
12683#endif
c6c8d102
AF
12684#ifdef TARGET_NR_clock_settime64
12685 case TARGET_NR_clock_settime64:
12686 {
12687 struct timespec ts;
12688
12689 ret = target_to_host_timespec64(&ts, arg2);
12690 if (!is_error(ret)) {
12691 ret = get_errno(clock_settime(arg1, &ts));
12692 }
12693 return ret;
12694 }
12695#endif
b5906f95
TS
12696#ifdef TARGET_NR_clock_gettime
12697 case TARGET_NR_clock_gettime:
12698 {
12699 struct timespec ts;
12700 ret = get_errno(clock_gettime(arg1, &ts));
12701 if (!is_error(ret)) {
b9f9908e 12702 ret = host_to_target_timespec(arg2, &ts);
b5906f95 12703 }
72eb7ea8 12704 return ret;
b5906f95
TS
12705 }
12706#endif
c6c8d102
AF
12707#ifdef TARGET_NR_clock_gettime64
12708 case TARGET_NR_clock_gettime64:
12709 {
12710 struct timespec ts;
12711 ret = get_errno(clock_gettime(arg1, &ts));
12712 if (!is_error(ret)) {
12713 ret = host_to_target_timespec64(arg2, &ts);
12714 }
12715 return ret;
12716 }
12717#endif
b5906f95
TS
12718#ifdef TARGET_NR_clock_getres
12719 case TARGET_NR_clock_getres:
12720 {
12721 struct timespec ts;
12722 ret = get_errno(clock_getres(arg1, &ts));
12723 if (!is_error(ret)) {
12724 host_to_target_timespec(arg2, &ts);
12725 }
72eb7ea8 12726 return ret;
b5906f95
TS
12727 }
12728#endif
828cb3a1
FB
12729#ifdef TARGET_NR_clock_getres_time64
12730 case TARGET_NR_clock_getres_time64:
12731 {
12732 struct timespec ts;
12733 ret = get_errno(clock_getres(arg1, &ts));
12734 if (!is_error(ret)) {
12735 host_to_target_timespec64(arg2, &ts);
12736 }
12737 return ret;
12738 }
12739#endif
63d7651b
PB
12740#ifdef TARGET_NR_clock_nanosleep
12741 case TARGET_NR_clock_nanosleep:
12742 {
12743 struct timespec ts;
b09d6406
FB
12744 if (target_to_host_timespec(&ts, arg3)) {
12745 return -TARGET_EFAULT;
12746 }
9e518226
PM
12747 ret = get_errno(safe_clock_nanosleep(arg1, arg2,
12748 &ts, arg4 ? &ts : NULL));
8ec68a0a
LV
12749 /*
12750 * if the call is interrupted by a signal handler, it fails
12751 * with error -TARGET_EINTR and if arg4 is not NULL and arg2 is not
12752 * TIMER_ABSTIME, it returns the remaining unslept time in arg4.
12753 */
b09d6406
FB
12754 if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
12755 host_to_target_timespec(arg4, &ts)) {
12756 return -TARGET_EFAULT;
8ec68a0a 12757 }
8fbe8fdf 12758
72eb7ea8 12759 return ret;
63d7651b
PB
12760 }
12761#endif
6ac03b2c
FB
12762#ifdef TARGET_NR_clock_nanosleep_time64
12763 case TARGET_NR_clock_nanosleep_time64:
12764 {
12765 struct timespec ts;
12766
12767 if (target_to_host_timespec64(&ts, arg3)) {
12768 return -TARGET_EFAULT;
12769 }
12770
12771 ret = get_errno(safe_clock_nanosleep(arg1, arg2,
12772 &ts, arg4 ? &ts : NULL));
12773
12774 if (ret == -TARGET_EINTR && arg4 && arg2 != TIMER_ABSTIME &&
12775 host_to_target_timespec64(arg4, &ts)) {
12776 return -TARGET_EFAULT;
12777 }
12778 return ret;
12779 }
12780#endif
b5906f95 12781
9a7f682c 12782#if defined(TARGET_NR_set_tid_address)
6f5b89a0 12783 case TARGET_NR_set_tid_address:
9a7f682c 12784 {
e4e5cb4a 12785 TaskState *ts = get_task_state(cpu);
9a7f682c
HD
12786 ts->child_tidptr = arg1;
12787 /* do not call host set_tid_address() syscall, instead return tid() */
12788 return get_errno(sys_gettid());
12789 }
6f5b89a0
TS
12790#endif
12791
4cae1d16 12792 case TARGET_NR_tkill:
72eb7ea8 12793 return get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2)));
4cae1d16 12794
71455574 12795 case TARGET_NR_tgkill:
72eb7ea8
RH
12796 return get_errno(safe_tgkill((int)arg1, (int)arg2,
12797 target_to_host_signal(arg3)));
71455574 12798
4f2b1fe8
TS
12799#ifdef TARGET_NR_set_robust_list
12800 case TARGET_NR_set_robust_list:
e9a970a8
PM
12801 case TARGET_NR_get_robust_list:
12802 /* The ABI for supporting robust futexes has userspace pass
12803 * the kernel a pointer to a linked list which is updated by
12804 * userspace after the syscall; the list is walked by the kernel
12805 * when the thread exits. Since the linked list in QEMU guest
12806 * memory isn't a valid linked list for the host and we have
12807 * no way to reliably intercept the thread-death event, we can't
12808 * support these. Silently return ENOSYS so that guest userspace
12809 * falls back to a non-robust futex implementation (which should
12810 * be OK except in the corner case of the guest crashing while
12811 * holding a mutex that is shared with another process via
12812 * shared memory).
12813 */
10f45d98 12814 return -TARGET_ENOSYS;
4f2b1fe8
TS
12815#endif
12816
1acae9f2 12817#if defined(TARGET_NR_utimensat)
9007f0ef
TS
12818 case TARGET_NR_utimensat:
12819 {
ebc996f3
RV
12820 struct timespec *tsp, ts[2];
12821 if (!arg3) {
12822 tsp = NULL;
12823 } else {
b3a3af70
FB
12824 if (target_to_host_timespec(ts, arg3)) {
12825 return -TARGET_EFAULT;
12826 }
12827 if (target_to_host_timespec(ts + 1, arg3 +
12828 sizeof(struct target_timespec))) {
12829 return -TARGET_EFAULT;
12830 }
ebc996f3
RV
12831 tsp = ts;
12832 }
9007f0ef 12833 if (!arg2)
ebc996f3 12834 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
9007f0ef 12835 else {
579a97f7 12836 if (!(p = lock_user_string(arg2))) {
259841c1 12837 return -TARGET_EFAULT;
579a97f7 12838 }
ebc996f3 12839 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
579a97f7 12840 unlock_user(p, arg2, 0);
cac46eb0
FB
12841 }
12842 }
12843 return ret;
12844#endif
12845#ifdef TARGET_NR_utimensat_time64
12846 case TARGET_NR_utimensat_time64:
12847 {
12848 struct timespec *tsp, ts[2];
12849 if (!arg3) {
12850 tsp = NULL;
12851 } else {
12852 if (target_to_host_timespec64(ts, arg3)) {
12853 return -TARGET_EFAULT;
12854 }
12855 if (target_to_host_timespec64(ts + 1, arg3 +
12856 sizeof(struct target__kernel_timespec))) {
12857 return -TARGET_EFAULT;
12858 }
12859 tsp = ts;
12860 }
12861 if (!arg2)
12862 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
12863 else {
12864 p = lock_user_string(arg2);
12865 if (!p) {
12866 return -TARGET_EFAULT;
12867 }
12868 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
12869 unlock_user(p, arg2, 0);
9007f0ef
TS
12870 }
12871 }
72eb7ea8 12872 return ret;
9007f0ef 12873#endif
859e8a89 12874#ifdef TARGET_NR_futex
bd0c5661 12875 case TARGET_NR_futex:
0fbc0f8d 12876 return do_futex(cpu, false, arg1, arg2, arg3, arg4, arg5, arg6);
859e8a89 12877#endif
14690296
AF
12878#ifdef TARGET_NR_futex_time64
12879 case TARGET_NR_futex_time64:
0fbc0f8d 12880 return do_futex(cpu, true, arg1, arg2, arg3, arg4, arg5, arg6);
14690296 12881#endif
33f53ac5
PB
12882#ifdef CONFIG_INOTIFY
12883#if defined(TARGET_NR_inotify_init)
39b59763 12884 case TARGET_NR_inotify_init:
33f53ac5 12885 ret = get_errno(inotify_init());
b929f7e5
PMD
12886 if (ret >= 0) {
12887 fd_trans_register(ret, &target_inotify_trans);
12888 }
72eb7ea8 12889 return ret;
39b59763 12890#endif
33f53ac5 12891#if defined(TARGET_NR_inotify_init1) && defined(CONFIG_INOTIFY1)
c05c7a73 12892 case TARGET_NR_inotify_init1:
33f53ac5 12893 ret = get_errno(inotify_init1(target_to_host_bitmask(arg1,
fea243e9 12894 fcntl_flags_tbl)));
b929f7e5
PMD
12895 if (ret >= 0) {
12896 fd_trans_register(ret, &target_inotify_trans);
12897 }
72eb7ea8 12898 return ret;
c05c7a73 12899#endif
33f53ac5 12900#if defined(TARGET_NR_inotify_add_watch)
39b59763
AJ
12901 case TARGET_NR_inotify_add_watch:
12902 p = lock_user_string(arg2);
33f53ac5 12903 ret = get_errno(inotify_add_watch(arg1, path(p), arg3));
39b59763 12904 unlock_user(p, arg2, 0);
72eb7ea8 12905 return ret;
39b59763 12906#endif
33f53ac5 12907#if defined(TARGET_NR_inotify_rm_watch)
39b59763 12908 case TARGET_NR_inotify_rm_watch:
33f53ac5
PB
12909 return get_errno(inotify_rm_watch(arg1, arg2));
12910#endif
39b59763 12911#endif
9007f0ef 12912
8ec9cf89 12913#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
24e1003a
AJ
12914 case TARGET_NR_mq_open:
12915 {
c7536ab6 12916 struct mq_attr posix_mq_attr;
26400775 12917 struct mq_attr *pposix_mq_attr;
c7536ab6 12918 int host_flags;
24e1003a 12919
c7536ab6 12920 host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl);
26400775
LD
12921 pposix_mq_attr = NULL;
12922 if (arg4) {
12923 if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) {
2852aafd 12924 return -TARGET_EFAULT;
26400775
LD
12925 }
12926 pposix_mq_attr = &posix_mq_attr;
c7536ab6 12927 }
24e1003a 12928 p = lock_user_string(arg1 - 1);
c7536ab6 12929 if (!p) {
2852aafd 12930 return -TARGET_EFAULT;
b6ce1f6b 12931 }
26400775 12932 ret = get_errno(mq_open(p, host_flags, arg3, pposix_mq_attr));
24e1003a
AJ
12933 unlock_user (p, arg1, 0);
12934 }
72eb7ea8 12935 return ret;
24e1003a
AJ
12936
12937 case TARGET_NR_mq_unlink:
12938 p = lock_user_string(arg1 - 1);
3211215e 12939 if (!p) {
72eb7ea8 12940 return -TARGET_EFAULT;
3211215e 12941 }
24e1003a
AJ
12942 ret = get_errno(mq_unlink(p));
12943 unlock_user (p, arg1, 0);
72eb7ea8 12944 return ret;
24e1003a 12945
859e8a89 12946#ifdef TARGET_NR_mq_timedsend
24e1003a
AJ
12947 case TARGET_NR_mq_timedsend:
12948 {
12949 struct timespec ts;
12950
12951 p = lock_user (VERIFY_READ, arg2, arg3, 1);
12952 if (arg5 != 0) {
dcbcf5cf
FB
12953 if (target_to_host_timespec(&ts, arg5)) {
12954 return -TARGET_EFAULT;
12955 }
d40ecd66 12956 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
dcbcf5cf
FB
12957 if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
12958 return -TARGET_EFAULT;
12959 }
d40ecd66
PM
12960 } else {
12961 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
24e1003a 12962 }
24e1003a
AJ
12963 unlock_user (p, arg2, arg3);
12964 }
72eb7ea8 12965 return ret;
859e8a89 12966#endif
d107e375
FB
12967#ifdef TARGET_NR_mq_timedsend_time64
12968 case TARGET_NR_mq_timedsend_time64:
12969 {
12970 struct timespec ts;
12971
12972 p = lock_user(VERIFY_READ, arg2, arg3, 1);
12973 if (arg5 != 0) {
12974 if (target_to_host_timespec64(&ts, arg5)) {
12975 return -TARGET_EFAULT;
12976 }
12977 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
12978 if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
12979 return -TARGET_EFAULT;
12980 }
12981 } else {
12982 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
12983 }
12984 unlock_user(p, arg2, arg3);
12985 }
12986 return ret;
12987#endif
24e1003a 12988
859e8a89 12989#ifdef TARGET_NR_mq_timedreceive
24e1003a
AJ
12990 case TARGET_NR_mq_timedreceive:
12991 {
12992 struct timespec ts;
12993 unsigned int prio;
12994
12995 p = lock_user (VERIFY_READ, arg2, arg3, 1);
12996 if (arg5 != 0) {
dcbcf5cf
FB
12997 if (target_to_host_timespec(&ts, arg5)) {
12998 return -TARGET_EFAULT;
12999 }
d40ecd66
PM
13000 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
13001 &prio, &ts));
dcbcf5cf
FB
13002 if (!is_error(ret) && host_to_target_timespec(arg5, &ts)) {
13003 return -TARGET_EFAULT;
13004 }
d40ecd66
PM
13005 } else {
13006 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
13007 &prio, NULL));
24e1003a 13008 }
24e1003a
AJ
13009 unlock_user (p, arg2, arg3);
13010 if (arg4 != 0)
13011 put_user_u32(prio, arg4);
13012 }
72eb7ea8 13013 return ret;
859e8a89 13014#endif
d107e375
FB
13015#ifdef TARGET_NR_mq_timedreceive_time64
13016 case TARGET_NR_mq_timedreceive_time64:
13017 {
13018 struct timespec ts;
13019 unsigned int prio;
13020
13021 p = lock_user(VERIFY_READ, arg2, arg3, 1);
13022 if (arg5 != 0) {
13023 if (target_to_host_timespec64(&ts, arg5)) {
13024 return -TARGET_EFAULT;
13025 }
13026 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
13027 &prio, &ts));
13028 if (!is_error(ret) && host_to_target_timespec64(arg5, &ts)) {
13029 return -TARGET_EFAULT;
13030 }
13031 } else {
13032 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
13033 &prio, NULL));
13034 }
13035 unlock_user(p, arg2, arg3);
13036 if (arg4 != 0) {
13037 put_user_u32(prio, arg4);
13038 }
13039 }
13040 return ret;
13041#endif
24e1003a
AJ
13042
13043 /* Not implemented for now... */
13044/* case TARGET_NR_mq_notify: */
13045/* break; */
13046
13047 case TARGET_NR_mq_getsetattr:
13048 {
13049 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
13050 ret = 0;
24e1003a
AJ
13051 if (arg2 != 0) {
13052 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
a23ea409
MF
13053 ret = get_errno(mq_setattr(arg1, &posix_mq_attr_in,
13054 &posix_mq_attr_out));
13055 } else if (arg3 != 0) {
13056 ret = get_errno(mq_getattr(arg1, &posix_mq_attr_out));
13057 }
13058 if (ret == 0 && arg3 != 0) {
13059 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
24e1003a 13060 }
24e1003a 13061 }
72eb7ea8 13062 return ret;
24e1003a
AJ
13063#endif
13064
3ce34dfb
VS
13065#ifdef CONFIG_SPLICE
13066#ifdef TARGET_NR_tee
13067 case TARGET_NR_tee:
13068 {
13069 ret = get_errno(tee(arg1,arg2,arg3,arg4));
13070 }
72eb7ea8 13071 return ret;
3ce34dfb
VS
13072#endif
13073#ifdef TARGET_NR_splice
13074 case TARGET_NR_splice:
13075 {
13076 loff_t loff_in, loff_out;
13077 loff_t *ploff_in = NULL, *ploff_out = NULL;
17644b36
AS
13078 if (arg2) {
13079 if (get_user_u64(loff_in, arg2)) {
2852aafd 13080 return -TARGET_EFAULT;
17644b36 13081 }
3ce34dfb
VS
13082 ploff_in = &loff_in;
13083 }
17644b36
AS
13084 if (arg4) {
13085 if (get_user_u64(loff_out, arg4)) {
2852aafd 13086 return -TARGET_EFAULT;
17644b36 13087 }
3ce34dfb
VS
13088 ploff_out = &loff_out;
13089 }
13090 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
17644b36
AS
13091 if (arg2) {
13092 if (put_user_u64(loff_in, arg2)) {
2852aafd 13093 return -TARGET_EFAULT;
17644b36
AS
13094 }
13095 }
13096 if (arg4) {
13097 if (put_user_u64(loff_out, arg4)) {
2852aafd 13098 return -TARGET_EFAULT;
17644b36
AS
13099 }
13100 }
3ce34dfb 13101 }
72eb7ea8 13102 return ret;
3ce34dfb
VS
13103#endif
13104#ifdef TARGET_NR_vmsplice
13105 case TARGET_NR_vmsplice:
13106 {
f287b2c2
RH
13107 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
13108 if (vec != NULL) {
13109 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
13110 unlock_iovec(vec, arg2, arg3, 0);
13111 } else {
13112 ret = -host_to_target_errno(errno);
13113 }
3ce34dfb 13114 }
72eb7ea8 13115 return ret;
3ce34dfb
VS
13116#endif
13117#endif /* CONFIG_SPLICE */
c2882b96
RV
13118#ifdef CONFIG_EVENTFD
13119#if defined(TARGET_NR_eventfd)
13120 case TARGET_NR_eventfd:
13121 ret = get_errno(eventfd(arg1, 0));
b929f7e5
PMD
13122 if (ret >= 0) {
13123 fd_trans_register(ret, &target_eventfd_trans);
13124 }
72eb7ea8 13125 return ret;
c2882b96
RV
13126#endif
13127#if defined(TARGET_NR_eventfd2)
13128 case TARGET_NR_eventfd2:
5947c697 13129 {
78721301 13130 int host_flags = arg2 & (~(TARGET_O_NONBLOCK_MASK | TARGET_O_CLOEXEC));
5947c697
PJ
13131 if (arg2 & TARGET_O_NONBLOCK) {
13132 host_flags |= O_NONBLOCK;
13133 }
13134 if (arg2 & TARGET_O_CLOEXEC) {
13135 host_flags |= O_CLOEXEC;
13136 }
13137 ret = get_errno(eventfd(arg1, host_flags));
b929f7e5
PMD
13138 if (ret >= 0) {
13139 fd_trans_register(ret, &target_eventfd_trans);
13140 }
72eb7ea8 13141 return ret;
5947c697 13142 }
c2882b96
RV
13143#endif
13144#endif /* CONFIG_EVENTFD */
d0927938
UH
13145#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
13146 case TARGET_NR_fallocate:
eeed2291 13147#if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
20249ae1
AG
13148 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
13149 target_offset64(arg5, arg6)));
13150#else
d0927938 13151 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
20249ae1 13152#endif
72eb7ea8 13153 return ret;
c727f47d
PM
13154#endif
13155#if defined(CONFIG_SYNC_FILE_RANGE)
13156#if defined(TARGET_NR_sync_file_range)
13157 case TARGET_NR_sync_file_range:
eeed2291 13158#if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
bfcedc57
RV
13159#if defined(TARGET_MIPS)
13160 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
13161 target_offset64(arg5, arg6), arg7));
13162#else
c727f47d
PM
13163 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
13164 target_offset64(arg4, arg5), arg6));
bfcedc57 13165#endif /* !TARGET_MIPS */
c727f47d
PM
13166#else
13167 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
13168#endif
72eb7ea8 13169 return ret;
c727f47d 13170#endif
5bcb4986
LV
13171#if defined(TARGET_NR_sync_file_range2) || \
13172 defined(TARGET_NR_arm_sync_file_range)
c727f47d
PM
13173#if defined(TARGET_NR_sync_file_range2)
13174 case TARGET_NR_sync_file_range2:
5bcb4986
LV
13175#endif
13176#if defined(TARGET_NR_arm_sync_file_range)
13177 case TARGET_NR_arm_sync_file_range:
13178#endif
c727f47d 13179 /* This is like sync_file_range but the arguments are reordered */
eeed2291 13180#if TARGET_ABI_BITS == 32 && !defined(TARGET_ABI_MIPSN32)
c727f47d
PM
13181 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
13182 target_offset64(arg5, arg6), arg2));
13183#else
13184 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
13185#endif
72eb7ea8 13186 return ret;
c727f47d 13187#endif
3b6edd16 13188#endif
e36800c9
LV
13189#if defined(TARGET_NR_signalfd4)
13190 case TARGET_NR_signalfd4:
72eb7ea8 13191 return do_signalfd4(arg1, arg2, arg4);
e36800c9
LV
13192#endif
13193#if defined(TARGET_NR_signalfd)
13194 case TARGET_NR_signalfd:
72eb7ea8 13195 return do_signalfd4(arg1, arg2, 0);
e36800c9 13196#endif
3b6edd16
PM
13197#if defined(CONFIG_EPOLL)
13198#if defined(TARGET_NR_epoll_create)
13199 case TARGET_NR_epoll_create:
72eb7ea8 13200 return get_errno(epoll_create(arg1));
3b6edd16
PM
13201#endif
13202#if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
13203 case TARGET_NR_epoll_create1:
386d3865 13204 return get_errno(epoll_create1(target_to_host_bitmask(arg1, fcntl_flags_tbl)));
3b6edd16
PM
13205#endif
13206#if defined(TARGET_NR_epoll_ctl)
13207 case TARGET_NR_epoll_ctl:
13208 {
13209 struct epoll_event ep;
13210 struct epoll_event *epp = 0;
13211 if (arg4) {
c7811022
L
13212 if (arg2 != EPOLL_CTL_DEL) {
13213 struct target_epoll_event *target_ep;
13214 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
13215 return -TARGET_EFAULT;
13216 }
13217 ep.events = tswap32(target_ep->events);
13218 /*
13219 * The epoll_data_t union is just opaque data to the kernel,
13220 * so we transfer all 64 bits across and need not worry what
13221 * actual data type it is.
13222 */
13223 ep.data.u64 = tswap64(target_ep->data.u64);
13224 unlock_user_struct(target_ep, arg4, 0);
3b6edd16 13225 }
c7811022
L
13226 /*
13227 * before kernel 2.6.9, EPOLL_CTL_DEL operation required a
13228 * non-null pointer, even though this argument is ignored.
13229 *
3b6edd16 13230 */
3b6edd16
PM
13231 epp = &ep;
13232 }
72eb7ea8 13233 return get_errno(epoll_ctl(arg1, arg2, arg3, epp));
3b6edd16
PM
13234 }
13235#endif
13236
227f0214 13237#if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
3b6edd16
PM
13238#if defined(TARGET_NR_epoll_wait)
13239 case TARGET_NR_epoll_wait:
13240#endif
227f0214 13241#if defined(TARGET_NR_epoll_pwait)
3b6edd16
PM
13242 case TARGET_NR_epoll_pwait:
13243#endif
13244 {
13245 struct target_epoll_event *target_ep;
13246 struct epoll_event *ep;
13247 int epfd = arg1;
13248 int maxevents = arg3;
13249 int timeout = arg4;
13250
2ba7fae3 13251 if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) {
72eb7ea8 13252 return -TARGET_EINVAL;
2ba7fae3
PM
13253 }
13254
3b6edd16
PM
13255 target_ep = lock_user(VERIFY_WRITE, arg2,
13256 maxevents * sizeof(struct target_epoll_event), 1);
13257 if (!target_ep) {
2852aafd 13258 return -TARGET_EFAULT;
3b6edd16
PM
13259 }
13260
04c95f4d
PM
13261 ep = g_try_new(struct epoll_event, maxevents);
13262 if (!ep) {
13263 unlock_user(target_ep, arg2, 0);
72eb7ea8 13264 return -TARGET_ENOMEM;
04c95f4d 13265 }
3b6edd16
PM
13266
13267 switch (num) {
227f0214 13268#if defined(TARGET_NR_epoll_pwait)
3b6edd16
PM
13269 case TARGET_NR_epoll_pwait:
13270 {
cd0e31a4 13271 sigset_t *set = NULL;
3b6edd16
PM
13272
13273 if (arg5) {
cd0e31a4
RH
13274 ret = process_sigsuspend_mask(&set, arg5, arg6);
13275 if (ret != 0) {
c815701e
PM
13276 break;
13277 }
3b6edd16
PM
13278 }
13279
227f0214
PM
13280 ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
13281 set, SIGSET_T_SIZE));
cd0e31a4
RH
13282
13283 if (set) {
13284 finish_sigsuspend_mask(ret);
13285 }
3b6edd16
PM
13286 break;
13287 }
13288#endif
13289#if defined(TARGET_NR_epoll_wait)
13290 case TARGET_NR_epoll_wait:
227f0214
PM
13291 ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
13292 NULL, 0));
3b6edd16
PM
13293 break;
13294#endif
13295 default:
13296 ret = -TARGET_ENOSYS;
13297 }
13298 if (!is_error(ret)) {
13299 int i;
13300 for (i = 0; i < ret; i++) {
13301 target_ep[i].events = tswap32(ep[i].events);
13302 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
13303 }
04c95f4d
PM
13304 unlock_user(target_ep, arg2,
13305 ret * sizeof(struct target_epoll_event));
13306 } else {
13307 unlock_user(target_ep, arg2, 0);
3b6edd16 13308 }
04c95f4d 13309 g_free(ep);
72eb7ea8 13310 return ret;
3b6edd16
PM
13311 }
13312#endif
163a05a8
PM
13313#endif
13314#ifdef TARGET_NR_prlimit64
13315 case TARGET_NR_prlimit64:
13316 {
13317 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
13318 struct target_rlimit64 *target_rnew, *target_rold;
13319 struct host_rlimit64 rnew, rold, *rnewp = 0;
95018018 13320 int resource = target_to_host_resource(arg2);
055d92f8
TK
13321
13322 if (arg3 && (resource != RLIMIT_AS &&
13323 resource != RLIMIT_DATA &&
13324 resource != RLIMIT_STACK)) {
163a05a8 13325 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
2852aafd 13326 return -TARGET_EFAULT;
163a05a8 13327 }
9c1da8b5
IL
13328 __get_user(rnew.rlim_cur, &target_rnew->rlim_cur);
13329 __get_user(rnew.rlim_max, &target_rnew->rlim_max);
163a05a8
PM
13330 unlock_user_struct(target_rnew, arg3, 0);
13331 rnewp = &rnew;
13332 }
13333
95018018 13334 ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
163a05a8
PM
13335 if (!is_error(ret) && arg4) {
13336 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
2852aafd 13337 return -TARGET_EFAULT;
163a05a8 13338 }
9c1da8b5
IL
13339 __put_user(rold.rlim_cur, &target_rold->rlim_cur);
13340 __put_user(rold.rlim_max, &target_rold->rlim_max);
163a05a8
PM
13341 unlock_user_struct(target_rold, arg4, 1);
13342 }
72eb7ea8 13343 return ret;
163a05a8 13344 }
3d21d29c
RH
13345#endif
13346#ifdef TARGET_NR_gethostname
13347 case TARGET_NR_gethostname:
13348 {
13349 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
13350 if (name) {
13351 ret = get_errno(gethostname(name, arg2));
13352 unlock_user(name, arg1, arg2);
13353 } else {
13354 ret = -TARGET_EFAULT;
13355 }
72eb7ea8 13356 return ret;
3d21d29c 13357 }
89aaf1a6
RV
13358#endif
13359#ifdef TARGET_NR_atomic_cmpxchg_32
13360 case TARGET_NR_atomic_cmpxchg_32:
13361 {
13362 /* should use start_exclusive from main.c */
13363 abi_ulong mem_value;
13364 if (get_user_u32(mem_value, arg6)) {
13365 target_siginfo_t info;
13366 info.si_signo = SIGSEGV;
13367 info.si_errno = 0;
13368 info.si_code = TARGET_SEGV_MAPERR;
13369 info._sifields._sigfault._addr = arg6;
0effdc29 13370 queue_signal(cpu_env, info.si_signo, QEMU_SI_FAULT, &info);
89aaf1a6
RV
13371 ret = 0xdeadbeef;
13372
13373 }
13374 if (mem_value == arg2)
13375 put_user_u32(arg1, arg6);
72eb7ea8 13376 return mem_value;
89aaf1a6
RV
13377 }
13378#endif
13379#ifdef TARGET_NR_atomic_barrier
13380 case TARGET_NR_atomic_barrier:
72eb7ea8
RH
13381 /* Like the kernel implementation and the
13382 qemu arm barrier, no-op this? */
13383 return 0;
d0927938 13384#endif
f4f1e10a
ECL
13385
13386#ifdef TARGET_NR_timer_create
13387 case TARGET_NR_timer_create:
13388 {
13389 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
13390
13391 struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
f4f1e10a
ECL
13392
13393 int clkid = arg1;
13394 int timer_index = next_free_host_timer();
13395
13396 if (timer_index < 0) {
13397 ret = -TARGET_EAGAIN;
13398 } else {
13399 timer_t *phtimer = g_posix_timers + timer_index;
13400
13401 if (arg2) {
f4f1e10a 13402 phost_sevp = &host_sevp;
c065976f
PM
13403 ret = target_to_host_sigevent(phost_sevp, arg2);
13404 if (ret != 0) {
9e59899f 13405 free_host_timer_slot(timer_index);
72eb7ea8 13406 return ret;
c065976f 13407 }
f4f1e10a
ECL
13408 }
13409
13410 ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
13411 if (ret) {
9e59899f 13412 free_host_timer_slot(timer_index);
f4f1e10a 13413 } else {
aecc8861 13414 if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
9e59899f
PM
13415 timer_delete(*phtimer);
13416 free_host_timer_slot(timer_index);
2852aafd 13417 return -TARGET_EFAULT;
f4f1e10a 13418 }
f4f1e10a
ECL
13419 }
13420 }
72eb7ea8 13421 return ret;
f4f1e10a
ECL
13422 }
13423#endif
13424
13425#ifdef TARGET_NR_timer_settime
13426 case TARGET_NR_timer_settime:
13427 {
13428 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
13429 * struct itimerspec * old_value */
aecc8861 13430 target_timer_t timerid = get_timer_id(arg1);
e52a99f7 13431
aecc8861
AG
13432 if (timerid < 0) {
13433 ret = timerid;
13434 } else if (arg3 == 0) {
f4f1e10a
ECL
13435 ret = -TARGET_EINVAL;
13436 } else {
e52a99f7 13437 timer_t htimer = g_posix_timers[timerid];
f4f1e10a
ECL
13438 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
13439
40c80b5e 13440 if (target_to_host_itimerspec(&hspec_new, arg3)) {
2852aafd 13441 return -TARGET_EFAULT;
40c80b5e 13442 }
f4f1e10a
ECL
13443 ret = get_errno(
13444 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
40c80b5e 13445 if (arg4 && host_to_target_itimerspec(arg4, &hspec_old)) {
2852aafd 13446 return -TARGET_EFAULT;
40c80b5e 13447 }
f4f1e10a 13448 }
72eb7ea8 13449 return ret;
f4f1e10a
ECL
13450 }
13451#endif
13452
828cb3a1
FB
13453#ifdef TARGET_NR_timer_settime64
13454 case TARGET_NR_timer_settime64:
13455 {
13456 target_timer_t timerid = get_timer_id(arg1);
13457
13458 if (timerid < 0) {
13459 ret = timerid;
13460 } else if (arg3 == 0) {
13461 ret = -TARGET_EINVAL;
13462 } else {
13463 timer_t htimer = g_posix_timers[timerid];
13464 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
13465
13466 if (target_to_host_itimerspec64(&hspec_new, arg3)) {
13467 return -TARGET_EFAULT;
13468 }
13469 ret = get_errno(
13470 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
13471 if (arg4 && host_to_target_itimerspec64(arg4, &hspec_old)) {
13472 return -TARGET_EFAULT;
13473 }
13474 }
13475 return ret;
13476 }
13477#endif
13478
f4f1e10a
ECL
13479#ifdef TARGET_NR_timer_gettime
13480 case TARGET_NR_timer_gettime:
13481 {
13482 /* args: timer_t timerid, struct itimerspec *curr_value */
aecc8861 13483 target_timer_t timerid = get_timer_id(arg1);
e52a99f7 13484
aecc8861
AG
13485 if (timerid < 0) {
13486 ret = timerid;
13487 } else if (!arg2) {
13488 ret = -TARGET_EFAULT;
f4f1e10a 13489 } else {
e52a99f7 13490 timer_t htimer = g_posix_timers[timerid];
f4f1e10a
ECL
13491 struct itimerspec hspec;
13492 ret = get_errno(timer_gettime(htimer, &hspec));
13493
13494 if (host_to_target_itimerspec(arg2, &hspec)) {
13495 ret = -TARGET_EFAULT;
13496 }
13497 }
72eb7ea8 13498 return ret;
f4f1e10a
ECL
13499 }
13500#endif
13501
828cb3a1
FB
13502#ifdef TARGET_NR_timer_gettime64
13503 case TARGET_NR_timer_gettime64:
13504 {
13505 /* args: timer_t timerid, struct itimerspec64 *curr_value */
13506 target_timer_t timerid = get_timer_id(arg1);
13507
13508 if (timerid < 0) {
13509 ret = timerid;
13510 } else if (!arg2) {
13511 ret = -TARGET_EFAULT;
13512 } else {
13513 timer_t htimer = g_posix_timers[timerid];
13514 struct itimerspec hspec;
13515 ret = get_errno(timer_gettime(htimer, &hspec));
13516
13517 if (host_to_target_itimerspec64(arg2, &hspec)) {
13518 ret = -TARGET_EFAULT;
13519 }
13520 }
13521 return ret;
13522 }
13523#endif
13524
f4f1e10a
ECL
13525#ifdef TARGET_NR_timer_getoverrun
13526 case TARGET_NR_timer_getoverrun:
13527 {
13528 /* args: timer_t timerid */
aecc8861 13529 target_timer_t timerid = get_timer_id(arg1);
e52a99f7 13530
aecc8861
AG
13531 if (timerid < 0) {
13532 ret = timerid;
f4f1e10a 13533 } else {
e52a99f7 13534 timer_t htimer = g_posix_timers[timerid];
f4f1e10a
ECL
13535 ret = get_errno(timer_getoverrun(htimer));
13536 }
72eb7ea8 13537 return ret;
f4f1e10a
ECL
13538 }
13539#endif
13540
13541#ifdef TARGET_NR_timer_delete
13542 case TARGET_NR_timer_delete:
13543 {
13544 /* args: timer_t timerid */
aecc8861 13545 target_timer_t timerid = get_timer_id(arg1);
e52a99f7 13546
aecc8861
AG
13547 if (timerid < 0) {
13548 ret = timerid;
f4f1e10a 13549 } else {
e52a99f7 13550 timer_t htimer = g_posix_timers[timerid];
f4f1e10a 13551 ret = get_errno(timer_delete(htimer));
9e59899f 13552 free_host_timer_slot(timerid);
f4f1e10a 13553 }
72eb7ea8 13554 return ret;
f4f1e10a
ECL
13555 }
13556#endif
13557
51834341
RV
13558#if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
13559 case TARGET_NR_timerfd_create:
d759a62b
MM
13560 ret = get_errno(timerfd_create(arg1,
13561 target_to_host_bitmask(arg2, fcntl_flags_tbl)));
13562 if (ret >= 0) {
13563 fd_trans_register(ret, &target_timerfd_trans);
13564 }
13565 return ret;
51834341
RV
13566#endif
13567
13568#if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
13569 case TARGET_NR_timerfd_gettime:
13570 {
13571 struct itimerspec its_curr;
13572
13573 ret = get_errno(timerfd_gettime(arg1, &its_curr));
13574
13575 if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
2852aafd 13576 return -TARGET_EFAULT;
51834341
RV
13577 }
13578 }
72eb7ea8 13579 return ret;
51834341
RV
13580#endif
13581
828cb3a1
FB
13582#if defined(TARGET_NR_timerfd_gettime64) && defined(CONFIG_TIMERFD)
13583 case TARGET_NR_timerfd_gettime64:
13584 {
13585 struct itimerspec its_curr;
13586
13587 ret = get_errno(timerfd_gettime(arg1, &its_curr));
13588
13589 if (arg2 && host_to_target_itimerspec64(arg2, &its_curr)) {
13590 return -TARGET_EFAULT;
13591 }
13592 }
13593 return ret;
13594#endif
13595
51834341
RV
13596#if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
13597 case TARGET_NR_timerfd_settime:
13598 {
13599 struct itimerspec its_new, its_old, *p_new;
13600
13601 if (arg3) {
13602 if (target_to_host_itimerspec(&its_new, arg3)) {
2852aafd 13603 return -TARGET_EFAULT;
51834341
RV
13604 }
13605 p_new = &its_new;
13606 } else {
13607 p_new = NULL;
13608 }
13609
13610 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
13611
13612 if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
2852aafd 13613 return -TARGET_EFAULT;
51834341
RV
13614 }
13615 }
72eb7ea8 13616 return ret;
51834341
RV
13617#endif
13618
828cb3a1
FB
13619#if defined(TARGET_NR_timerfd_settime64) && defined(CONFIG_TIMERFD)
13620 case TARGET_NR_timerfd_settime64:
13621 {
13622 struct itimerspec its_new, its_old, *p_new;
13623
13624 if (arg3) {
13625 if (target_to_host_itimerspec64(&its_new, arg3)) {
13626 return -TARGET_EFAULT;
13627 }
13628 p_new = &its_new;
13629 } else {
13630 p_new = NULL;
13631 }
13632
13633 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
13634
13635 if (arg4 && host_to_target_itimerspec64(arg4, &its_old)) {
13636 return -TARGET_EFAULT;
13637 }
13638 }
13639 return ret;
13640#endif
13641
ab31cda3
PB
13642#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
13643 case TARGET_NR_ioprio_get:
72eb7ea8 13644 return get_errno(ioprio_get(arg1, arg2));
ab31cda3
PB
13645#endif
13646
13647#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
13648 case TARGET_NR_ioprio_set:
72eb7ea8 13649 return get_errno(ioprio_set(arg1, arg2, arg3));
ab31cda3
PB
13650#endif
13651
9af5c906
RV
13652#if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
13653 case TARGET_NR_setns:
72eb7ea8 13654 return get_errno(setns(arg1, arg2));
9af5c906
RV
13655#endif
13656#if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
13657 case TARGET_NR_unshare:
72eb7ea8 13658 return get_errno(unshare(arg1));
9af5c906 13659#endif
2f14788c
LV
13660#if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
13661 case TARGET_NR_kcmp:
72eb7ea8 13662 return get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
2f14788c 13663#endif
fa97e38e
RH
13664#ifdef TARGET_NR_swapcontext
13665 case TARGET_NR_swapcontext:
13666 /* PowerPC specific. */
72eb7ea8 13667 return do_swapcontext(cpu_env, arg1, arg2, arg3);
fa97e38e 13668#endif
9bdfa4d2
SCW
13669#ifdef TARGET_NR_memfd_create
13670 case TARGET_NR_memfd_create:
13671 p = lock_user_string(arg1);
13672 if (!p) {
13673 return -TARGET_EFAULT;
13674 }
13675 ret = get_errno(memfd_create(p, arg2));
13676 fd_trans_unregister(ret);
13677 unlock_user(p, arg1, 0);
13678 return ret;
13679#endif
8500476f
AS
13680#if defined TARGET_NR_membarrier && defined __NR_membarrier
13681 case TARGET_NR_membarrier:
13682 return get_errno(membarrier(arg1, arg2));
13683#endif
9af5c906 13684
84946457
AS
13685#if defined(TARGET_NR_copy_file_range) && defined(__NR_copy_file_range)
13686 case TARGET_NR_copy_file_range:
13687 {
13688 loff_t inoff, outoff;
13689 loff_t *pinoff = NULL, *poutoff = NULL;
13690
13691 if (arg2) {
13692 if (get_user_u64(inoff, arg2)) {
13693 return -TARGET_EFAULT;
13694 }
13695 pinoff = &inoff;
13696 }
13697 if (arg4) {
13698 if (get_user_u64(outoff, arg4)) {
13699 return -TARGET_EFAULT;
13700 }
13701 poutoff = &outoff;
13702 }
0fa259dd 13703 /* Do not sign-extend the count parameter. */
84946457 13704 ret = get_errno(safe_copy_file_range(arg1, pinoff, arg3, poutoff,
0fa259dd 13705 (abi_ulong)arg5, arg6));
84946457
AS
13706 if (!is_error(ret) && ret > 0) {
13707 if (arg2) {
13708 if (put_user_u64(inoff, arg2)) {
13709 return -TARGET_EFAULT;
13710 }
13711 }
13712 if (arg4) {
13713 if (put_user_u64(outoff, arg4)) {
13714 return -TARGET_EFAULT;
13715 }
13716 }
13717 }
13718 }
13719 return ret;
13720#endif
13721
e10fbe8f
YT
13722#if defined(TARGET_NR_pivot_root)
13723 case TARGET_NR_pivot_root:
13724 {
13725 void *p2;
13726 p = lock_user_string(arg1); /* new_root */
13727 p2 = lock_user_string(arg2); /* put_old */
13728 if (!p || !p2) {
13729 ret = -TARGET_EFAULT;
13730 } else {
13731 ret = get_errno(pivot_root(p, p2));
13732 }
13733 unlock_user(p2, arg2, 0);
13734 unlock_user(p, arg1, 0);
13735 }
13736 return ret;
13737#endif
13738
9e1c7d98
RE
13739#if defined(TARGET_NR_riscv_hwprobe)
13740 case TARGET_NR_riscv_hwprobe:
13741 return do_riscv_hwprobe(cpu_env, arg1, arg2, arg3, arg4, arg5);
13742#endif
13743
31e31b8a 13744 default:
122f9c83 13745 qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
72eb7ea8 13746 return -TARGET_ENOSYS;
31e31b8a 13747 }
31e31b8a
FB
13748 return ret;
13749}
dc1ce18b 13750
a0939b89 13751abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1,
dc1ce18b
RH
13752 abi_long arg2, abi_long arg3, abi_long arg4,
13753 abi_long arg5, abi_long arg6, abi_long arg7,
13754 abi_long arg8)
13755{
29a0af61 13756 CPUState *cpu = env_cpu(cpu_env);
dc1ce18b
RH
13757 abi_long ret;
13758
13759#ifdef DEBUG_ERESTARTSYS
13760 /* Debug-only code for exercising the syscall-restart code paths
13761 * in the per-architecture cpu main loops: restart every syscall
13762 * the guest makes once before letting it through.
13763 */
13764 {
13765 static bool flag;
13766 flag = !flag;
13767 if (flag) {
af254a27 13768 return -QEMU_ERESTARTSYS;
dc1ce18b
RH
13769 }
13770 }
13771#endif
13772
c36f7a64
EC
13773 record_syscall_start(cpu, num, arg1,
13774 arg2, arg3, arg4, arg5, arg6, arg7, arg8);
dc1ce18b 13775
4b25a506 13776 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
e400e119 13777 print_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6);
4b25a506
JK
13778 }
13779
13780 ret = do_syscall1(cpu_env, num, arg1, arg2, arg3, arg4,
13781 arg5, arg6, arg7, arg8);
13782
13783 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
e400e119
FB
13784 print_syscall_ret(cpu_env, num, ret, arg1, arg2,
13785 arg3, arg4, arg5, arg6);
dc1ce18b
RH
13786 }
13787
c36f7a64 13788 record_syscall_return(cpu, num, ret);
dc1ce18b
RH
13789 return ret;
13790}