]> git.proxmox.com Git - mirror_qemu.git/blame - linux-user/syscall.c
linux-user: Support f_flags in statfs when available.
[mirror_qemu.git] / linux-user / syscall.c
CommitLineData
31e31b8a
FB
1/*
2 * Linux syscalls
5fafdf24 3 *
31e31b8a
FB
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
8167ee88 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
31e31b8a 18 */
d5b3a9b6 19#define _ATFILE_SOURCE
d39594e9 20#include "qemu/osdep.h"
f348b6d1
VB
21#include "qemu/cutils.h"
22#include "qemu/path.h"
31e31b8a
FB
23#include <elf.h>
24#include <endian.h>
c56dc774 25#include <grp.h>
d08d3bb8
TS
26#include <sys/ipc.h>
27#include <sys/msg.h>
31e31b8a 28#include <sys/wait.h>
31e31b8a 29#include <sys/mount.h>
586b0bef
JS
30#include <sys/file.h>
31#include <sys/fsuid.h>
32#include <sys/personality.h>
39b9aae1 33#include <sys/prctl.h>
31e31b8a 34#include <sys/resource.h>
31e31b8a 35#include <sys/swap.h>
e0eb210e 36#include <linux/capability.h>
31e31b8a 37#include <sched.h>
19f59bce 38#include <sys/timex.h>
31e31b8a 39#include <sys/socket.h>
607175e0 40#include <sys/un.h>
31e31b8a 41#include <sys/uio.h>
0839f11c 42#include <poll.h>
32f36bce 43#include <sys/times.h>
8853f86e 44#include <sys/shm.h>
fa294816 45#include <sys/sem.h>
56c8f68f 46#include <sys/statfs.h>
ebc05488 47#include <utime.h>
a5448a7d 48#include <sys/sysinfo.h>
e36800c9 49#include <sys/signalfd.h>
72f03900 50//#include <sys/user.h>
8853f86e 51#include <netinet/ip.h>
7854b056 52#include <netinet/tcp.h>
86fcd946 53#include <linux/wireless.h>
920394db 54#include <linux/icmp.h>
ee1ac3a1
HD
55#include <linux/icmpv6.h>
56#include <linux/errqueue.h>
d6d6d6fe 57#include <linux/random.h>
5a61cb60 58#include "qemu-common.h"
d80a1905
RV
59#ifdef CONFIG_TIMERFD
60#include <sys/timerfd.h>
61#endif
9788c9ca 62#ifdef TARGET_GPROF
6d946cda
AJ
63#include <sys/gmon.h>
64#endif
c2882b96
RV
65#ifdef CONFIG_EVENTFD
66#include <sys/eventfd.h>
67#endif
3b6edd16
PM
68#ifdef CONFIG_EPOLL
69#include <sys/epoll.h>
70#endif
a790ae38 71#ifdef CONFIG_ATTR
1de7afc9 72#include "qemu/xattr.h"
a790ae38 73#endif
a8fd1aba
PM
74#ifdef CONFIG_SENDFILE
75#include <sys/sendfile.h>
76#endif
31e31b8a
FB
77
78#define termios host_termios
79#define winsize host_winsize
80#define termio host_termio
04369ff2
FB
81#define sgttyb host_sgttyb /* same as target */
82#define tchars host_tchars /* same as target */
83#define ltchars host_ltchars /* same as target */
31e31b8a
FB
84
85#include <linux/termios.h>
86#include <linux/unistd.h>
31e31b8a
FB
87#include <linux/cdrom.h>
88#include <linux/hdreg.h>
89#include <linux/soundcard.h>
19b84f3c 90#include <linux/kd.h>
8fbd6b52 91#include <linux/mtio.h>
350d1779 92#include <linux/fs.h>
dace20dc 93#if defined(CONFIG_FIEMAP)
285da2b9 94#include <linux/fiemap.h>
dace20dc 95#endif
f7680a55
UH
96#include <linux/fb.h>
97#include <linux/vt.h>
56e904ec 98#include <linux/dm-ioctl.h>
c07ecc68 99#include <linux/reboot.h>
7ff7b666 100#include <linux/route.h>
f57d4192 101#include <linux/filter.h>
fff8c539 102#include <linux/blkpg.h>
a82ea939 103#include <netpacket/packet.h>
6c5b5645 104#include <linux/netlink.h>
575b22b1 105#ifdef CONFIG_RTNETLINK
6c5b5645 106#include <linux/rtnetlink.h>
c5dff280 107#include <linux/if_bridge.h>
575b22b1 108#endif
5ce9bb59 109#include <linux/audit.h>
d7e4036e 110#include "linux_loop.h"
18cb0088 111#include "uname.h"
31e31b8a 112
3ef693a0 113#include "qemu.h"
31e31b8a 114
5ea2fc84
PM
115#ifndef CLONE_IO
116#define CLONE_IO 0x80000000 /* Clone io context */
117#endif
118
119/* We can't directly call the host clone syscall, because this will
120 * badly confuse libc (breaking mutexes, for example). So we must
121 * divide clone flags into:
122 * * flag combinations that look like pthread_create()
123 * * flag combinations that look like fork()
124 * * flags we can implement within QEMU itself
125 * * flags we can't support and will return an error for
126 */
127/* For thread creation, all these flags must be present; for
128 * fork, none must be present.
129 */
130#define CLONE_THREAD_FLAGS \
131 (CLONE_VM | CLONE_FS | CLONE_FILES | \
132 CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM)
133
134/* These flags are ignored:
135 * CLONE_DETACHED is now ignored by the kernel;
136 * CLONE_IO is just an optimisation hint to the I/O scheduler
137 */
138#define CLONE_IGNORED_FLAGS \
139 (CLONE_DETACHED | CLONE_IO)
140
141/* Flags for fork which we can implement within QEMU itself */
142#define CLONE_OPTIONAL_FORK_FLAGS \
143 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
144 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID)
145
146/* Flags for thread creation which we can implement within QEMU itself */
147#define CLONE_OPTIONAL_THREAD_FLAGS \
148 (CLONE_SETTLS | CLONE_PARENT_SETTID | \
149 CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | CLONE_PARENT)
150
151#define CLONE_INVALID_FORK_FLAGS \
152 (~(CSIGNAL | CLONE_OPTIONAL_FORK_FLAGS | CLONE_IGNORED_FLAGS))
153
154#define CLONE_INVALID_THREAD_FLAGS \
155 (~(CSIGNAL | CLONE_THREAD_FLAGS | CLONE_OPTIONAL_THREAD_FLAGS | \
156 CLONE_IGNORED_FLAGS))
157
158/* CLONE_VFORK is special cased early in do_fork(). The other flag bits
159 * have almost all been allocated. We cannot support any of
160 * CLONE_NEWNS, CLONE_NEWCGROUP, CLONE_NEWUTS, CLONE_NEWIPC,
161 * CLONE_NEWUSER, CLONE_NEWPID, CLONE_NEWNET, CLONE_PTRACE, CLONE_UNTRACED.
162 * The checks against the invalid thread masks above will catch these.
163 * (The one remaining unallocated bit is 0x1000 which used to be CLONE_PID.)
164 */
30813cea 165
72f03900 166//#define DEBUG
71a8f7fe
TB
167/* Define DEBUG_ERESTARTSYS to force every syscall to be restarted
168 * once. This exercises the codepaths for restart.
169 */
170//#define DEBUG_ERESTARTSYS
31e31b8a 171
1a9353d2 172//#include <linux/msdos_fs.h>
6556a833
AJ
173#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
174#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
1a9353d2 175
70a194b9
FB
176#undef _syscall0
177#undef _syscall1
178#undef _syscall2
179#undef _syscall3
180#undef _syscall4
181#undef _syscall5
83fcb515 182#undef _syscall6
70a194b9 183
83fcb515 184#define _syscall0(type,name) \
8fcd3692 185static type name (void) \
83fcb515
FB
186{ \
187 return syscall(__NR_##name); \
188}
70a194b9 189
83fcb515 190#define _syscall1(type,name,type1,arg1) \
8fcd3692 191static type name (type1 arg1) \
83fcb515
FB
192{ \
193 return syscall(__NR_##name, arg1); \
70a194b9
FB
194}
195
83fcb515 196#define _syscall2(type,name,type1,arg1,type2,arg2) \
8fcd3692 197static type name (type1 arg1,type2 arg2) \
83fcb515
FB
198{ \
199 return syscall(__NR_##name, arg1, arg2); \
70a194b9
FB
200}
201
83fcb515 202#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
8fcd3692 203static type name (type1 arg1,type2 arg2,type3 arg3) \
83fcb515
FB
204{ \
205 return syscall(__NR_##name, arg1, arg2, arg3); \
70a194b9
FB
206}
207
83fcb515 208#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
8fcd3692 209static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
83fcb515
FB
210{ \
211 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
70a194b9
FB
212}
213
83fcb515
FB
214#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
215 type5,arg5) \
8fcd3692 216static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
83fcb515
FB
217{ \
218 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
70a194b9
FB
219}
220
83fcb515
FB
221
222#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
223 type5,arg5,type6,arg6) \
8fcd3692
BS
224static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
225 type6 arg6) \
83fcb515
FB
226{ \
227 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
70a194b9 228}
83fcb515 229
70a194b9 230
31e31b8a 231#define __NR_sys_uname __NR_uname
72f03900 232#define __NR_sys_getcwd1 __NR_getcwd
72f03900 233#define __NR_sys_getdents __NR_getdents
dab2ed99 234#define __NR_sys_getdents64 __NR_getdents64
c6cda17a 235#define __NR_sys_getpriority __NR_getpriority
66fb9763 236#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
cf8b8bfc 237#define __NR_sys_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
7494b0f9 238#define __NR_sys_syslog __NR_syslog
bd0c5661 239#define __NR_sys_futex __NR_futex
39b59763
AJ
240#define __NR_sys_inotify_init __NR_inotify_init
241#define __NR_sys_inotify_add_watch __NR_inotify_add_watch
242#define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
31e31b8a 243
b1cef6d0 244#if defined(__alpha__) || defined(__x86_64__) || defined(__s390x__)
9af9eaaa
FB
245#define __NR__llseek __NR_lseek
246#endif
247
a29e5ba2
JH
248/* Newer kernel ports have llseek() instead of _llseek() */
249#if defined(TARGET_NR_llseek) && !defined(TARGET_NR__llseek)
250#define TARGET_NR__llseek TARGET_NR_llseek
251#endif
252
72f03900 253#ifdef __NR_gettid
31e31b8a 254_syscall0(int, gettid)
72f03900 255#else
0da46a6e
TS
256/* This is a replacement for the host gettid() and must return a host
257 errno. */
72f03900
FB
258static int gettid(void) {
259 return -ENOSYS;
260}
261#endif
704eff6c 262#if defined(TARGET_NR_getdents) && defined(__NR_getdents)
3b3f24ad 263_syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
3307e236
PM
264#endif
265#if !defined(__NR_getdents) || \
266 (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
3b3f24ad
AJ
267_syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
268#endif
d35b261c 269#if defined(TARGET_NR__llseek) && defined(__NR_llseek)
3b3f24ad
AJ
270_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
271 loff_t *, res, uint, wh);
272#endif
c1a402a7 273_syscall3(int, sys_rt_sigqueueinfo, pid_t, pid, int, sig, siginfo_t *, uinfo)
cf8b8bfc
MS
274_syscall4(int, sys_rt_tgsigqueueinfo, pid_t, pid, pid_t, tid, int, sig,
275 siginfo_t *, uinfo)
3b3f24ad 276_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
3b3f24ad
AJ
277#ifdef __NR_exit_group
278_syscall1(int,exit_group,int,error_code)
279#endif
280#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
281_syscall1(int,set_tid_address,int *,tidptr)
282#endif
3b3f24ad
AJ
283#if defined(TARGET_NR_futex) && defined(__NR_futex)
284_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
285 const struct timespec *,timeout,int *,uaddr2,int,val3)
286#endif
737de1d1
MF
287#define __NR_sys_sched_getaffinity __NR_sched_getaffinity
288_syscall3(int, sys_sched_getaffinity, pid_t, pid, unsigned int, len,
289 unsigned long *, user_mask_ptr);
290#define __NR_sys_sched_setaffinity __NR_sched_setaffinity
291_syscall3(int, sys_sched_setaffinity, pid_t, pid, unsigned int, len,
292 unsigned long *, user_mask_ptr);
b827c3ed
ST
293#define __NR_sys_getcpu __NR_getcpu
294_syscall3(int, sys_getcpu, unsigned *, cpu, unsigned *, node, void *, tcache);
0f6b4d21
AG
295_syscall4(int, reboot, int, magic1, int, magic2, unsigned int, cmd,
296 void *, arg);
e0eb210e
PM
297_syscall2(int, capget, struct __user_cap_header_struct *, header,
298 struct __user_cap_data_struct *, data);
299_syscall2(int, capset, struct __user_cap_header_struct *, header,
300 struct __user_cap_data_struct *, data);
ab31cda3
PB
301#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
302_syscall2(int, ioprio_get, int, which, int, who)
303#endif
304#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
305_syscall3(int, ioprio_set, int, which, int, who, int, ioprio)
306#endif
f894efd1
LV
307#if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
308_syscall3(int, getrandom, void *, buf, size_t, buflen, unsigned int, flags)
309#endif
3b3f24ad 310
2f14788c
LV
311#if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
312_syscall5(int, kcmp, pid_t, pid1, pid_t, pid2, int, type,
313 unsigned long, idx1, unsigned long, idx2)
314#endif
315
3b3f24ad
AJ
316static bitmask_transtbl fcntl_flags_tbl[] = {
317 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
318 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
319 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
320 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
321 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
322 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
323 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
324 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
afc8763f 325 { TARGET_O_SYNC, TARGET_O_DSYNC, O_SYNC, O_DSYNC, },
3b3f24ad
AJ
326 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
327 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
328 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
329 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
3b3f24ad
AJ
330#if defined(O_DIRECT)
331 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
afc8763f
RH
332#endif
333#if defined(O_NOATIME)
334 { TARGET_O_NOATIME, TARGET_O_NOATIME, O_NOATIME, O_NOATIME },
335#endif
336#if defined(O_CLOEXEC)
337 { TARGET_O_CLOEXEC, TARGET_O_CLOEXEC, O_CLOEXEC, O_CLOEXEC },
338#endif
339#if defined(O_PATH)
340 { TARGET_O_PATH, TARGET_O_PATH, O_PATH, O_PATH },
5f9cee46
RV
341#endif
342#if defined(O_TMPFILE)
343 { TARGET_O_TMPFILE, TARGET_O_TMPFILE, O_TMPFILE, O_TMPFILE },
afc8763f
RH
344#endif
345 /* Don't terminate the list prematurely on 64-bit host+guest. */
346#if TARGET_O_LARGEFILE != 0 || O_LARGEFILE != 0
347 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
3b3f24ad
AJ
348#endif
349 { 0, 0, 0, 0 }
350};
351
60c6b790
MP
352enum {
353 QEMU_IFLA_BR_UNSPEC,
354 QEMU_IFLA_BR_FORWARD_DELAY,
355 QEMU_IFLA_BR_HELLO_TIME,
356 QEMU_IFLA_BR_MAX_AGE,
357 QEMU_IFLA_BR_AGEING_TIME,
358 QEMU_IFLA_BR_STP_STATE,
359 QEMU_IFLA_BR_PRIORITY,
360 QEMU_IFLA_BR_VLAN_FILTERING,
361 QEMU_IFLA_BR_VLAN_PROTOCOL,
362 QEMU_IFLA_BR_GROUP_FWD_MASK,
363 QEMU_IFLA_BR_ROOT_ID,
364 QEMU_IFLA_BR_BRIDGE_ID,
365 QEMU_IFLA_BR_ROOT_PORT,
366 QEMU_IFLA_BR_ROOT_PATH_COST,
367 QEMU_IFLA_BR_TOPOLOGY_CHANGE,
368 QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
369 QEMU_IFLA_BR_HELLO_TIMER,
370 QEMU_IFLA_BR_TCN_TIMER,
371 QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER,
372 QEMU_IFLA_BR_GC_TIMER,
373 QEMU_IFLA_BR_GROUP_ADDR,
374 QEMU_IFLA_BR_FDB_FLUSH,
375 QEMU_IFLA_BR_MCAST_ROUTER,
376 QEMU_IFLA_BR_MCAST_SNOOPING,
377 QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR,
378 QEMU_IFLA_BR_MCAST_QUERIER,
379 QEMU_IFLA_BR_MCAST_HASH_ELASTICITY,
380 QEMU_IFLA_BR_MCAST_HASH_MAX,
381 QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT,
382 QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT,
383 QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL,
384 QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL,
385 QEMU_IFLA_BR_MCAST_QUERIER_INTVL,
386 QEMU_IFLA_BR_MCAST_QUERY_INTVL,
387 QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL,
388 QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL,
389 QEMU_IFLA_BR_NF_CALL_IPTABLES,
390 QEMU_IFLA_BR_NF_CALL_IP6TABLES,
391 QEMU_IFLA_BR_NF_CALL_ARPTABLES,
392 QEMU_IFLA_BR_VLAN_DEFAULT_PVID,
393 QEMU_IFLA_BR_PAD,
394 QEMU_IFLA_BR_VLAN_STATS_ENABLED,
395 QEMU_IFLA_BR_MCAST_STATS_ENABLED,
396 QEMU___IFLA_BR_MAX,
397};
398
399enum {
400 QEMU_IFLA_UNSPEC,
401 QEMU_IFLA_ADDRESS,
402 QEMU_IFLA_BROADCAST,
403 QEMU_IFLA_IFNAME,
404 QEMU_IFLA_MTU,
405 QEMU_IFLA_LINK,
406 QEMU_IFLA_QDISC,
407 QEMU_IFLA_STATS,
408 QEMU_IFLA_COST,
409 QEMU_IFLA_PRIORITY,
410 QEMU_IFLA_MASTER,
411 QEMU_IFLA_WIRELESS,
412 QEMU_IFLA_PROTINFO,
413 QEMU_IFLA_TXQLEN,
414 QEMU_IFLA_MAP,
415 QEMU_IFLA_WEIGHT,
416 QEMU_IFLA_OPERSTATE,
417 QEMU_IFLA_LINKMODE,
418 QEMU_IFLA_LINKINFO,
419 QEMU_IFLA_NET_NS_PID,
420 QEMU_IFLA_IFALIAS,
421 QEMU_IFLA_NUM_VF,
422 QEMU_IFLA_VFINFO_LIST,
423 QEMU_IFLA_STATS64,
424 QEMU_IFLA_VF_PORTS,
425 QEMU_IFLA_PORT_SELF,
426 QEMU_IFLA_AF_SPEC,
427 QEMU_IFLA_GROUP,
428 QEMU_IFLA_NET_NS_FD,
429 QEMU_IFLA_EXT_MASK,
430 QEMU_IFLA_PROMISCUITY,
431 QEMU_IFLA_NUM_TX_QUEUES,
432 QEMU_IFLA_NUM_RX_QUEUES,
433 QEMU_IFLA_CARRIER,
434 QEMU_IFLA_PHYS_PORT_ID,
435 QEMU_IFLA_CARRIER_CHANGES,
436 QEMU_IFLA_PHYS_SWITCH_ID,
437 QEMU_IFLA_LINK_NETNSID,
438 QEMU_IFLA_PHYS_PORT_NAME,
439 QEMU_IFLA_PROTO_DOWN,
440 QEMU_IFLA_GSO_MAX_SEGS,
441 QEMU_IFLA_GSO_MAX_SIZE,
442 QEMU_IFLA_PAD,
443 QEMU_IFLA_XDP,
444 QEMU___IFLA_MAX
445};
446
447enum {
448 QEMU_IFLA_BRPORT_UNSPEC,
449 QEMU_IFLA_BRPORT_STATE,
450 QEMU_IFLA_BRPORT_PRIORITY,
451 QEMU_IFLA_BRPORT_COST,
452 QEMU_IFLA_BRPORT_MODE,
453 QEMU_IFLA_BRPORT_GUARD,
454 QEMU_IFLA_BRPORT_PROTECT,
455 QEMU_IFLA_BRPORT_FAST_LEAVE,
456 QEMU_IFLA_BRPORT_LEARNING,
457 QEMU_IFLA_BRPORT_UNICAST_FLOOD,
458 QEMU_IFLA_BRPORT_PROXYARP,
459 QEMU_IFLA_BRPORT_LEARNING_SYNC,
460 QEMU_IFLA_BRPORT_PROXYARP_WIFI,
461 QEMU_IFLA_BRPORT_ROOT_ID,
462 QEMU_IFLA_BRPORT_BRIDGE_ID,
463 QEMU_IFLA_BRPORT_DESIGNATED_PORT,
464 QEMU_IFLA_BRPORT_DESIGNATED_COST,
465 QEMU_IFLA_BRPORT_ID,
466 QEMU_IFLA_BRPORT_NO,
467 QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
468 QEMU_IFLA_BRPORT_CONFIG_PENDING,
469 QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER,
470 QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER,
471 QEMU_IFLA_BRPORT_HOLD_TIMER,
472 QEMU_IFLA_BRPORT_FLUSH,
473 QEMU_IFLA_BRPORT_MULTICAST_ROUTER,
474 QEMU_IFLA_BRPORT_PAD,
475 QEMU___IFLA_BRPORT_MAX
476};
477
478enum {
479 QEMU_IFLA_INFO_UNSPEC,
480 QEMU_IFLA_INFO_KIND,
481 QEMU_IFLA_INFO_DATA,
482 QEMU_IFLA_INFO_XSTATS,
483 QEMU_IFLA_INFO_SLAVE_KIND,
484 QEMU_IFLA_INFO_SLAVE_DATA,
485 QEMU___IFLA_INFO_MAX,
486};
487
488enum {
489 QEMU_IFLA_INET_UNSPEC,
490 QEMU_IFLA_INET_CONF,
491 QEMU___IFLA_INET_MAX,
492};
493
494enum {
495 QEMU_IFLA_INET6_UNSPEC,
496 QEMU_IFLA_INET6_FLAGS,
497 QEMU_IFLA_INET6_CONF,
498 QEMU_IFLA_INET6_STATS,
499 QEMU_IFLA_INET6_MCAST,
500 QEMU_IFLA_INET6_CACHEINFO,
501 QEMU_IFLA_INET6_ICMP6STATS,
502 QEMU_IFLA_INET6_TOKEN,
503 QEMU_IFLA_INET6_ADDR_GEN_MODE,
504 QEMU___IFLA_INET6_MAX
505};
506
5d4d3665 507typedef abi_long (*TargetFdDataFunc)(void *, size_t);
7b36f782 508typedef abi_long (*TargetFdAddrFunc)(void *, abi_ulong, socklen_t);
e36800c9 509typedef struct TargetFdTrans {
5d4d3665
LV
510 TargetFdDataFunc host_to_target_data;
511 TargetFdDataFunc target_to_host_data;
7b36f782 512 TargetFdAddrFunc target_to_host_addr;
e36800c9
LV
513} TargetFdTrans;
514
515static TargetFdTrans **target_fd_trans;
516
517static unsigned int target_fd_max;
518
6c5b5645
LV
519static TargetFdDataFunc fd_trans_target_to_host_data(int fd)
520{
521 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
522 return target_fd_trans[fd]->target_to_host_data;
523 }
524 return NULL;
525}
526
5d4d3665 527static TargetFdDataFunc fd_trans_host_to_target_data(int fd)
e36800c9 528{
861d72cd 529 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
5d4d3665 530 return target_fd_trans[fd]->host_to_target_data;
e36800c9
LV
531 }
532 return NULL;
533}
534
7b36f782
LV
535static TargetFdAddrFunc fd_trans_target_to_host_addr(int fd)
536{
861d72cd 537 if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
7b36f782
LV
538 return target_fd_trans[fd]->target_to_host_addr;
539 }
540 return NULL;
541}
542
e36800c9
LV
543static void fd_trans_register(int fd, TargetFdTrans *trans)
544{
545 unsigned int oldmax;
546
547 if (fd >= target_fd_max) {
548 oldmax = target_fd_max;
549 target_fd_max = ((fd >> 6) + 1) << 6; /* by slice of 64 entries */
5089c7ce
LV
550 target_fd_trans = g_renew(TargetFdTrans *,
551 target_fd_trans, target_fd_max);
e36800c9
LV
552 memset((void *)(target_fd_trans + oldmax), 0,
553 (target_fd_max - oldmax) * sizeof(TargetFdTrans *));
554 }
555 target_fd_trans[fd] = trans;
556}
557
558static void fd_trans_unregister(int fd)
559{
560 if (fd >= 0 && fd < target_fd_max) {
561 target_fd_trans[fd] = NULL;
562 }
563}
564
565static void fd_trans_dup(int oldfd, int newfd)
566{
567 fd_trans_unregister(newfd);
568 if (oldfd < target_fd_max && target_fd_trans[oldfd]) {
569 fd_trans_register(newfd, target_fd_trans[oldfd]);
570 }
571}
572
3b3f24ad
AJ
573static int sys_getcwd1(char *buf, size_t size)
574{
575 if (getcwd(buf, size) == NULL) {
576 /* getcwd() sets errno */
577 return (-1);
578 }
aaf4ad39 579 return strlen(buf)+1;
3b3f24ad
AJ
580}
581
1acae9f2 582#ifdef TARGET_NR_utimensat
700fa58e 583#if defined(__NR_utimensat)
1acae9f2 584#define __NR_sys_utimensat __NR_utimensat
9007f0ef
TS
585_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
586 const struct timespec *,tsp,int,flags)
1acae9f2
PM
587#else
588static int sys_utimensat(int dirfd, const char *pathname,
589 const struct timespec times[2], int flags)
590{
591 errno = ENOSYS;
592 return -1;
593}
9007f0ef 594#endif
1acae9f2 595#endif /* TARGET_NR_utimensat */
3b3f24ad 596
95d0307c
AS
597#ifdef TARGET_NR_renameat2
598#if defined(__NR_renameat2)
599#define __NR_sys_renameat2 __NR_renameat2
600_syscall5(int, sys_renameat2, int, oldfd, const char *, old, int, newfd,
601 const char *, new, unsigned int, flags)
602#else
603static int sys_renameat2(int oldfd, const char *old,
604 int newfd, const char *new, int flags)
605{
606 if (flags == 0) {
607 return renameat(oldfd, old, newfd, new);
608 }
609 errno = ENOSYS;
610 return -1;
611}
612#endif
613#endif /* TARGET_NR_renameat2 */
614
3b3f24ad 615#ifdef CONFIG_INOTIFY
8690e420 616#include <sys/inotify.h>
3b3f24ad 617
39b59763 618#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
3b3f24ad
AJ
619static int sys_inotify_init(void)
620{
621 return (inotify_init());
622}
39b59763
AJ
623#endif
624#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
3b3f24ad
AJ
625static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
626{
627 return (inotify_add_watch(fd, pathname, mask));
628}
39b59763
AJ
629#endif
630#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
3b3f24ad
AJ
631static int sys_inotify_rm_watch(int fd, int32_t wd)
632{
8690e420 633 return (inotify_rm_watch(fd, wd));
3b3f24ad 634}
bd0c5661 635#endif
c05c7a73
RV
636#ifdef CONFIG_INOTIFY1
637#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
638static int sys_inotify_init1(int flags)
639{
640 return (inotify_init1(flags));
641}
642#endif
643#endif
3b3f24ad
AJ
644#else
645/* Userspace can usually survive runtime without inotify */
646#undef TARGET_NR_inotify_init
c05c7a73 647#undef TARGET_NR_inotify_init1
3b3f24ad
AJ
648#undef TARGET_NR_inotify_add_watch
649#undef TARGET_NR_inotify_rm_watch
650#endif /* CONFIG_INOTIFY */
651
163a05a8
PM
652#if defined(TARGET_NR_prlimit64)
653#ifndef __NR_prlimit64
654# define __NR_prlimit64 -1
655#endif
656#define __NR_sys_prlimit64 __NR_prlimit64
657/* The glibc rlimit structure may not be that used by the underlying syscall */
658struct host_rlimit64 {
659 uint64_t rlim_cur;
660 uint64_t rlim_max;
661};
662_syscall4(int, sys_prlimit64, pid_t, pid, int, resource,
663 const struct host_rlimit64 *, new_limit,
664 struct host_rlimit64 *, old_limit)
665#endif
666
f4f1e10a
ECL
667
668#if defined(TARGET_NR_timer_create)
669/* Maxiumum of 32 active POSIX timers allowed at any one time. */
670static timer_t g_posix_timers[32] = { 0, } ;
671
672static inline int next_free_host_timer(void)
673{
674 int k ;
675 /* FIXME: Does finding the next free slot require a lock? */
676 for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) {
677 if (g_posix_timers[k] == 0) {
678 g_posix_timers[k] = (timer_t) 1;
679 return k;
680 }
681 }
682 return -1;
683}
684#endif
685
48e515d4 686/* ARM EABI and MIPS expect 64bit types aligned even on pairs or registers */
4a1def4e 687#ifdef TARGET_ARM
8bf8e9df
JC
688static inline int regpairs_aligned(void *cpu_env, int num)
689{
48e515d4
RV
690 return ((((CPUARMState *)cpu_env)->eabi) == 1) ;
691}
d7779acb 692#elif defined(TARGET_MIPS) && (TARGET_ABI_BITS == 32)
8bf8e9df 693static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
4a1def4e
AG
694#elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
695/* SysV AVI for PPC32 expects 64bit parameters to be passed on odd/even pairs
696 * of registers which translates to the same as ARM/MIPS, because we start with
697 * r3 as arg1 */
8bf8e9df
JC
698static inline int regpairs_aligned(void *cpu_env, int num) { return 1; }
699#elif defined(TARGET_SH4)
700/* SH4 doesn't align register pairs, except for p{read,write}64 */
701static inline int regpairs_aligned(void *cpu_env, int num)
702{
703 switch (num) {
704 case TARGET_NR_pread64:
705 case TARGET_NR_pwrite64:
706 return 1;
707
708 default:
709 return 0;
710 }
711}
48e515d4 712#else
8bf8e9df 713static inline int regpairs_aligned(void *cpu_env, int num) { return 0; }
48e515d4
RV
714#endif
715
b92c47c1
TS
716#define ERRNO_TABLE_SIZE 1200
717
718/* target_to_host_errno_table[] is initialized from
719 * host_to_target_errno_table[] in syscall_init(). */
720static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
721};
722
637947f1 723/*
fe8f096b 724 * This list is the union of errno values overridden in asm-<arch>/errno.h
637947f1
TS
725 * minus the errnos that are not actually generic to all archs.
726 */
b92c47c1 727static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
08703b9f 728 [EAGAIN] = TARGET_EAGAIN,
637947f1
TS
729 [EIDRM] = TARGET_EIDRM,
730 [ECHRNG] = TARGET_ECHRNG,
731 [EL2NSYNC] = TARGET_EL2NSYNC,
732 [EL3HLT] = TARGET_EL3HLT,
733 [EL3RST] = TARGET_EL3RST,
734 [ELNRNG] = TARGET_ELNRNG,
735 [EUNATCH] = TARGET_EUNATCH,
736 [ENOCSI] = TARGET_ENOCSI,
737 [EL2HLT] = TARGET_EL2HLT,
738 [EDEADLK] = TARGET_EDEADLK,
739 [ENOLCK] = TARGET_ENOLCK,
740 [EBADE] = TARGET_EBADE,
741 [EBADR] = TARGET_EBADR,
742 [EXFULL] = TARGET_EXFULL,
743 [ENOANO] = TARGET_ENOANO,
744 [EBADRQC] = TARGET_EBADRQC,
745 [EBADSLT] = TARGET_EBADSLT,
746 [EBFONT] = TARGET_EBFONT,
747 [ENOSTR] = TARGET_ENOSTR,
748 [ENODATA] = TARGET_ENODATA,
749 [ETIME] = TARGET_ETIME,
750 [ENOSR] = TARGET_ENOSR,
751 [ENONET] = TARGET_ENONET,
752 [ENOPKG] = TARGET_ENOPKG,
753 [EREMOTE] = TARGET_EREMOTE,
754 [ENOLINK] = TARGET_ENOLINK,
755 [EADV] = TARGET_EADV,
756 [ESRMNT] = TARGET_ESRMNT,
757 [ECOMM] = TARGET_ECOMM,
758 [EPROTO] = TARGET_EPROTO,
759 [EDOTDOT] = TARGET_EDOTDOT,
760 [EMULTIHOP] = TARGET_EMULTIHOP,
761 [EBADMSG] = TARGET_EBADMSG,
762 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
763 [EOVERFLOW] = TARGET_EOVERFLOW,
764 [ENOTUNIQ] = TARGET_ENOTUNIQ,
765 [EBADFD] = TARGET_EBADFD,
766 [EREMCHG] = TARGET_EREMCHG,
767 [ELIBACC] = TARGET_ELIBACC,
768 [ELIBBAD] = TARGET_ELIBBAD,
769 [ELIBSCN] = TARGET_ELIBSCN,
770 [ELIBMAX] = TARGET_ELIBMAX,
771 [ELIBEXEC] = TARGET_ELIBEXEC,
772 [EILSEQ] = TARGET_EILSEQ,
773 [ENOSYS] = TARGET_ENOSYS,
774 [ELOOP] = TARGET_ELOOP,
775 [ERESTART] = TARGET_ERESTART,
776 [ESTRPIPE] = TARGET_ESTRPIPE,
777 [ENOTEMPTY] = TARGET_ENOTEMPTY,
778 [EUSERS] = TARGET_EUSERS,
779 [ENOTSOCK] = TARGET_ENOTSOCK,
780 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
781 [EMSGSIZE] = TARGET_EMSGSIZE,
782 [EPROTOTYPE] = TARGET_EPROTOTYPE,
783 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
784 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
785 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
786 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
787 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
788 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
789 [EADDRINUSE] = TARGET_EADDRINUSE,
790 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
791 [ENETDOWN] = TARGET_ENETDOWN,
792 [ENETUNREACH] = TARGET_ENETUNREACH,
793 [ENETRESET] = TARGET_ENETRESET,
794 [ECONNABORTED] = TARGET_ECONNABORTED,
795 [ECONNRESET] = TARGET_ECONNRESET,
796 [ENOBUFS] = TARGET_ENOBUFS,
797 [EISCONN] = TARGET_EISCONN,
798 [ENOTCONN] = TARGET_ENOTCONN,
799 [EUCLEAN] = TARGET_EUCLEAN,
800 [ENOTNAM] = TARGET_ENOTNAM,
801 [ENAVAIL] = TARGET_ENAVAIL,
802 [EISNAM] = TARGET_EISNAM,
803 [EREMOTEIO] = TARGET_EREMOTEIO,
0444a3b7 804 [EDQUOT] = TARGET_EDQUOT,
637947f1
TS
805 [ESHUTDOWN] = TARGET_ESHUTDOWN,
806 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
807 [ETIMEDOUT] = TARGET_ETIMEDOUT,
808 [ECONNREFUSED] = TARGET_ECONNREFUSED,
809 [EHOSTDOWN] = TARGET_EHOSTDOWN,
810 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
811 [EALREADY] = TARGET_EALREADY,
812 [EINPROGRESS] = TARGET_EINPROGRESS,
813 [ESTALE] = TARGET_ESTALE,
814 [ECANCELED] = TARGET_ECANCELED,
815 [ENOMEDIUM] = TARGET_ENOMEDIUM,
816 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
b7fe5db7 817#ifdef ENOKEY
637947f1 818 [ENOKEY] = TARGET_ENOKEY,
b7fe5db7
TS
819#endif
820#ifdef EKEYEXPIRED
637947f1 821 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
b7fe5db7
TS
822#endif
823#ifdef EKEYREVOKED
637947f1 824 [EKEYREVOKED] = TARGET_EKEYREVOKED,
b7fe5db7
TS
825#endif
826#ifdef EKEYREJECTED
637947f1 827 [EKEYREJECTED] = TARGET_EKEYREJECTED,
b7fe5db7
TS
828#endif
829#ifdef EOWNERDEAD
637947f1 830 [EOWNERDEAD] = TARGET_EOWNERDEAD,
b7fe5db7
TS
831#endif
832#ifdef ENOTRECOVERABLE
637947f1 833 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
b7fe5db7 834#endif
da39db63
AM
835#ifdef ENOMSG
836 [ENOMSG] = TARGET_ENOMSG,
837#endif
fe8ed7d5
RH
838#ifdef ERKFILL
839 [ERFKILL] = TARGET_ERFKILL,
840#endif
841#ifdef EHWPOISON
842 [EHWPOISON] = TARGET_EHWPOISON,
843#endif
b92c47c1 844};
637947f1
TS
845
846static inline int host_to_target_errno(int err)
847{
2466119c
TB
848 if (err >= 0 && err < ERRNO_TABLE_SIZE &&
849 host_to_target_errno_table[err]) {
637947f1 850 return host_to_target_errno_table[err];
2466119c 851 }
637947f1
TS
852 return err;
853}
854
b92c47c1
TS
855static inline int target_to_host_errno(int err)
856{
2466119c
TB
857 if (err >= 0 && err < ERRNO_TABLE_SIZE &&
858 target_to_host_errno_table[err]) {
b92c47c1 859 return target_to_host_errno_table[err];
2466119c 860 }
b92c47c1
TS
861 return err;
862}
863
992f48a0 864static inline abi_long get_errno(abi_long ret)
31e31b8a
FB
865{
866 if (ret == -1)
637947f1 867 return -host_to_target_errno(errno);
31e31b8a
FB
868 else
869 return ret;
870}
871
992f48a0 872static inline int is_error(abi_long ret)
31e31b8a 873{
992f48a0 874 return (abi_ulong)ret >= (abi_ulong)(-4096);
31e31b8a
FB
875}
876
7dcdaeaf 877const char *target_strerror(int err)
b92c47c1 878{
da2a34f7
PM
879 if (err == TARGET_ERESTARTSYS) {
880 return "To be restarted";
881 }
882 if (err == TARGET_QEMU_ESIGRETURN) {
883 return "Successful exit from sigreturn";
884 }
885
962b289e
AG
886 if ((err >= ERRNO_TABLE_SIZE) || (err < 0)) {
887 return NULL;
888 }
b92c47c1
TS
889 return strerror(target_to_host_errno(err));
890}
891
4d330cee
TB
892#define safe_syscall0(type, name) \
893static type safe_##name(void) \
894{ \
895 return safe_syscall(__NR_##name); \
896}
897
898#define safe_syscall1(type, name, type1, arg1) \
899static type safe_##name(type1 arg1) \
900{ \
901 return safe_syscall(__NR_##name, arg1); \
902}
903
904#define safe_syscall2(type, name, type1, arg1, type2, arg2) \
905static type safe_##name(type1 arg1, type2 arg2) \
906{ \
907 return safe_syscall(__NR_##name, arg1, arg2); \
908}
909
910#define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
911static type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
912{ \
913 return safe_syscall(__NR_##name, arg1, arg2, arg3); \
914}
915
916#define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
917 type4, arg4) \
918static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
919{ \
920 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4); \
921}
922
923#define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
924 type4, arg4, type5, arg5) \
925static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
926 type5 arg5) \
927{ \
928 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
929}
930
931#define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
932 type4, arg4, type5, arg5, type6, arg6) \
933static type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
934 type5 arg5, type6 arg6) \
935{ \
936 return safe_syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
937}
938
50afd02b
TB
939safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
940safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
c10a0738
TB
941safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
942 int, flags, mode_t, mode)
4af80a37
TB
943safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
944 struct rusage *, rusage)
945safe_syscall5(int, waitid, idtype_t, idtype, id_t, id, siginfo_t *, infop, \
946 int, options, struct rusage *, rusage)
ffdcbe22 947safe_syscall3(int, execve, const char *, filename, char **, argv, char **, envp)
6df9d38d
PM
948safe_syscall6(int, pselect6, int, nfds, fd_set *, readfds, fd_set *, writefds, \
949 fd_set *, exceptfds, struct timespec *, timeout, void *, sig)
a6130237
PM
950safe_syscall5(int, ppoll, struct pollfd *, ufds, unsigned int, nfds,
951 struct timespec *, tsp, const sigset_t *, sigmask,
952 size_t, sigsetsize)
227f0214
PM
953safe_syscall6(int, epoll_pwait, int, epfd, struct epoll_event *, events,
954 int, maxevents, int, timeout, const sigset_t *, sigmask,
955 size_t, sigsetsize)
d509eeb1
PM
956safe_syscall6(int,futex,int *,uaddr,int,op,int,val, \
957 const struct timespec *,timeout,int *,uaddr2,int,val3)
2fe4fba1 958safe_syscall2(int, rt_sigsuspend, sigset_t *, newset, size_t, sigsetsize)
bef653d9
PM
959safe_syscall2(int, kill, pid_t, pid, int, sig)
960safe_syscall2(int, tkill, int, tid, int, sig)
961safe_syscall3(int, tgkill, int, tgid, int, pid, int, sig)
918c03ed
PM
962safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt)
963safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt)
0f26386c
DJ
964safe_syscall5(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
965 unsigned long, pos_l, unsigned long, pos_h)
f8d00fba
DJ
966safe_syscall5(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
967 unsigned long, pos_l, unsigned long, pos_h)
2a3c7619
PM
968safe_syscall3(int, connect, int, fd, const struct sockaddr *, addr,
969 socklen_t, addrlen)
66687530
PM
970safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len,
971 int, flags, const struct sockaddr *, addr, socklen_t, addrlen)
972safe_syscall6(ssize_t, recvfrom, int, fd, void *, buf, size_t, len,
973 int, flags, struct sockaddr *, addr, socklen_t *, addrlen)
974safe_syscall3(ssize_t, sendmsg, int, fd, const struct msghdr *, msg, int, flags)
975safe_syscall3(ssize_t, recvmsg, int, fd, struct msghdr *, msg, int, flags)
2a845989 976safe_syscall2(int, flock, int, fd, int, operation)
b3f82330
PM
977safe_syscall4(int, rt_sigtimedwait, const sigset_t *, these, siginfo_t *, uinfo,
978 const struct timespec *, uts, size_t, sigsetsize)
ff6dc130
PM
979safe_syscall4(int, accept4, int, fd, struct sockaddr *, addr, socklen_t *, len,
980 int, flags)
9e518226
PM
981safe_syscall2(int, nanosleep, const struct timespec *, req,
982 struct timespec *, rem)
983#ifdef TARGET_NR_clock_nanosleep
984safe_syscall4(int, clock_nanosleep, const clockid_t, clock, int, flags,
985 const struct timespec *, req, struct timespec *, rem)
986#endif
89f9fe44
PM
987#ifdef __NR_msgsnd
988safe_syscall4(int, msgsnd, int, msgid, const void *, msgp, size_t, sz,
989 int, flags)
990safe_syscall5(int, msgrcv, int, msgid, void *, msgp, size_t, sz,
991 long, msgtype, int, flags)
ffb7ee79
PM
992safe_syscall4(int, semtimedop, int, semid, struct sembuf *, tsops,
993 unsigned, nsops, const struct timespec *, timeout)
89f9fe44
PM
994#else
995/* This host kernel architecture uses a single ipc syscall; fake up
996 * wrappers for the sub-operations to hide this implementation detail.
997 * Annoyingly we can't include linux/ipc.h to get the constant definitions
998 * for the call parameter because some structs in there conflict with the
999 * sys/ipc.h ones. So we just define them here, and rely on them being
1000 * the same for all host architectures.
1001 */
ffb7ee79 1002#define Q_SEMTIMEDOP 4
89f9fe44
PM
1003#define Q_MSGSND 11
1004#define Q_MSGRCV 12
1005#define Q_IPCCALL(VERSION, OP) ((VERSION) << 16 | (OP))
1006
1007safe_syscall6(int, ipc, int, call, long, first, long, second, long, third,
1008 void *, ptr, long, fifth)
1009static int safe_msgsnd(int msgid, const void *msgp, size_t sz, int flags)
1010{
1011 return safe_ipc(Q_IPCCALL(0, Q_MSGSND), msgid, sz, flags, (void *)msgp, 0);
1012}
1013static int safe_msgrcv(int msgid, void *msgp, size_t sz, long type, int flags)
1014{
1015 return safe_ipc(Q_IPCCALL(1, Q_MSGRCV), msgid, sz, flags, msgp, type);
1016}
ffb7ee79
PM
1017static int safe_semtimedop(int semid, struct sembuf *tsops, unsigned nsops,
1018 const struct timespec *timeout)
1019{
1020 return safe_ipc(Q_IPCCALL(0, Q_SEMTIMEDOP), semid, nsops, 0, tsops,
1021 (long)timeout);
1022}
89f9fe44 1023#endif
d40ecd66
PM
1024#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1025safe_syscall5(int, mq_timedsend, int, mqdes, const char *, msg_ptr,
1026 size_t, len, unsigned, prio, const struct timespec *, timeout)
1027safe_syscall5(int, mq_timedreceive, int, mqdes, char *, msg_ptr,
1028 size_t, len, unsigned *, prio, const struct timespec *, timeout)
1029#endif
49ca6f3e
PM
1030/* We do ioctl like this rather than via safe_syscall3 to preserve the
1031 * "third argument might be integer or pointer or not present" behaviour of
1032 * the libc function.
1033 */
1034#define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
435da5e7
PM
1035/* Similarly for fcntl. Note that callers must always:
1036 * pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
1037 * use the flock64 struct rather than unsuffixed flock
1038 * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
1039 */
1040#ifdef __NR_fcntl64
1041#define safe_fcntl(...) safe_syscall(__NR_fcntl64, __VA_ARGS__)
1042#else
1043#define safe_fcntl(...) safe_syscall(__NR_fcntl, __VA_ARGS__)
1044#endif
50afd02b 1045
8289d112
PB
1046static inline int host_to_target_sock_type(int host_type)
1047{
1048 int target_type;
1049
1050 switch (host_type & 0xf /* SOCK_TYPE_MASK */) {
1051 case SOCK_DGRAM:
1052 target_type = TARGET_SOCK_DGRAM;
1053 break;
1054 case SOCK_STREAM:
1055 target_type = TARGET_SOCK_STREAM;
1056 break;
1057 default:
1058 target_type = host_type & 0xf /* SOCK_TYPE_MASK */;
1059 break;
1060 }
1061
1062#if defined(SOCK_CLOEXEC)
1063 if (host_type & SOCK_CLOEXEC) {
1064 target_type |= TARGET_SOCK_CLOEXEC;
1065 }
1066#endif
1067
1068#if defined(SOCK_NONBLOCK)
1069 if (host_type & SOCK_NONBLOCK) {
1070 target_type |= TARGET_SOCK_NONBLOCK;
1071 }
1072#endif
1073
1074 return target_type;
1075}
1076
992f48a0
BS
1077static abi_ulong target_brk;
1078static abi_ulong target_original_brk;
4d1de87c 1079static abi_ulong brk_page;
31e31b8a 1080
992f48a0 1081void target_set_brk(abi_ulong new_brk)
31e31b8a 1082{
4c1de73d 1083 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
4d1de87c 1084 brk_page = HOST_PAGE_ALIGN(target_brk);
31e31b8a
FB
1085}
1086
4d1de87c
CV
1087//#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0)
1088#define DEBUGF_BRK(message, args...)
1089
0da46a6e 1090/* do_brk() must return target values and target errnos. */
992f48a0 1091abi_long do_brk(abi_ulong new_brk)
31e31b8a 1092{
992f48a0 1093 abi_long mapped_addr;
ef4330c2 1094 abi_ulong new_alloc_size;
31e31b8a 1095
3a0c6c4a 1096 DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk);
4d1de87c
CV
1097
1098 if (!new_brk) {
3a0c6c4a 1099 DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk);
53a5960a 1100 return target_brk;
4d1de87c
CV
1101 }
1102 if (new_brk < target_original_brk) {
3a0c6c4a
PB
1103 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n",
1104 target_brk);
7ab240ad 1105 return target_brk;
4d1de87c 1106 }
3b46e624 1107
4d1de87c
CV
1108 /* If the new brk is less than the highest page reserved to the
1109 * target heap allocation, set it and we're almost done... */
1110 if (new_brk <= brk_page) {
1111 /* Heap contents are initialized to zero, as for anonymous
1112 * mapped pages. */
1113 if (new_brk > target_brk) {
1114 memset(g2h(target_brk), 0, new_brk - target_brk);
1115 }
31e31b8a 1116 target_brk = new_brk;
3a0c6c4a 1117 DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk);
53a5960a 1118 return target_brk;
31e31b8a
FB
1119 }
1120
00faf08c
PM
1121 /* We need to allocate more memory after the brk... Note that
1122 * we don't use MAP_FIXED because that will map over the top of
1123 * any existing mapping (like the one with the host libc or qemu
1124 * itself); instead we treat "mapped but at wrong address" as
1125 * a failure and unmap again.
1126 */
4d1de87c 1127 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page);
5fafdf24 1128 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
54936004 1129 PROT_READ|PROT_WRITE,
00faf08c
PM
1130 MAP_ANON|MAP_PRIVATE, 0, 0));
1131
1132 if (mapped_addr == brk_page) {
70afc343
CV
1133 /* Heap contents are initialized to zero, as for anonymous
1134 * mapped pages. Technically the new pages are already
1135 * initialized to zero since they *are* anonymous mapped
1136 * pages, however we have to take care with the contents that
1137 * come from the remaining part of the previous page: it may
1138 * contains garbage data due to a previous heap usage (grown
1139 * then shrunken). */
1140 memset(g2h(target_brk), 0, brk_page - target_brk);
1141
00faf08c 1142 target_brk = new_brk;
4d1de87c 1143 brk_page = HOST_PAGE_ALIGN(target_brk);
3a0c6c4a
PB
1144 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n",
1145 target_brk);
00faf08c
PM
1146 return target_brk;
1147 } else if (mapped_addr != -1) {
1148 /* Mapped but at wrong address, meaning there wasn't actually
1149 * enough space for this brk.
1150 */
1151 target_munmap(mapped_addr, new_alloc_size);
1152 mapped_addr = -1;
3a0c6c4a 1153 DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk);
4d1de87c
CV
1154 }
1155 else {
3a0c6c4a 1156 DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk);
00faf08c 1157 }
7ab240ad 1158
7dd46c02
RH
1159#if defined(TARGET_ALPHA)
1160 /* We (partially) emulate OSF/1 on Alpha, which requires we
1161 return a proper errno, not an unchanged brk value. */
00faf08c 1162 return -TARGET_ENOMEM;
7dd46c02 1163#endif
00faf08c 1164 /* For everything else, return the previous break. */
7ab240ad 1165 return target_brk;
31e31b8a
FB
1166}
1167
26edcf41
TS
1168static inline abi_long copy_from_user_fdset(fd_set *fds,
1169 abi_ulong target_fds_addr,
1170 int n)
31e31b8a 1171{
26edcf41
TS
1172 int i, nw, j, k;
1173 abi_ulong b, *target_fds;
1174
b1b2db29 1175 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
26edcf41
TS
1176 if (!(target_fds = lock_user(VERIFY_READ,
1177 target_fds_addr,
1178 sizeof(abi_ulong) * nw,
1179 1)))
1180 return -TARGET_EFAULT;
1181
1182 FD_ZERO(fds);
1183 k = 0;
1184 for (i = 0; i < nw; i++) {
1185 /* grab the abi_ulong */
1186 __get_user(b, &target_fds[i]);
1187 for (j = 0; j < TARGET_ABI_BITS; j++) {
1188 /* check the bit inside the abi_ulong */
1189 if ((b >> j) & 1)
1190 FD_SET(k, fds);
1191 k++;
31e31b8a 1192 }
31e31b8a 1193 }
26edcf41
TS
1194
1195 unlock_user(target_fds, target_fds_addr, 0);
1196
1197 return 0;
31e31b8a
FB
1198}
1199
055e0906
MF
1200static inline abi_ulong copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
1201 abi_ulong target_fds_addr,
1202 int n)
1203{
1204 if (target_fds_addr) {
1205 if (copy_from_user_fdset(fds, target_fds_addr, n))
1206 return -TARGET_EFAULT;
1207 *fds_ptr = fds;
1208 } else {
1209 *fds_ptr = NULL;
1210 }
1211 return 0;
1212}
1213
26edcf41
TS
1214static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
1215 const fd_set *fds,
1216 int n)
31e31b8a 1217{
31e31b8a 1218 int i, nw, j, k;
992f48a0 1219 abi_long v;
26edcf41 1220 abi_ulong *target_fds;
31e31b8a 1221
b1b2db29 1222 nw = DIV_ROUND_UP(n, TARGET_ABI_BITS);
26edcf41
TS
1223 if (!(target_fds = lock_user(VERIFY_WRITE,
1224 target_fds_addr,
1225 sizeof(abi_ulong) * nw,
1226 0)))
1227 return -TARGET_EFAULT;
1228
1229 k = 0;
1230 for (i = 0; i < nw; i++) {
1231 v = 0;
1232 for (j = 0; j < TARGET_ABI_BITS; j++) {
9ab709be 1233 v |= ((abi_ulong)(FD_ISSET(k, fds) != 0) << j);
26edcf41 1234 k++;
31e31b8a 1235 }
26edcf41 1236 __put_user(v, &target_fds[i]);
31e31b8a 1237 }
26edcf41
TS
1238
1239 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
1240
1241 return 0;
31e31b8a
FB
1242}
1243
c596ed17
FB
1244#if defined(__alpha__)
1245#define HOST_HZ 1024
1246#else
1247#define HOST_HZ 100
1248#endif
1249
992f48a0 1250static inline abi_long host_to_target_clock_t(long ticks)
c596ed17
FB
1251{
1252#if HOST_HZ == TARGET_HZ
1253 return ticks;
1254#else
1255 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
1256#endif
1257}
1258
579a97f7
FB
1259static inline abi_long host_to_target_rusage(abi_ulong target_addr,
1260 const struct rusage *rusage)
b409186b 1261{
53a5960a
PB
1262 struct target_rusage *target_rusage;
1263
579a97f7
FB
1264 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
1265 return -TARGET_EFAULT;
cbb21eed
MB
1266 target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
1267 target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
1268 target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
1269 target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
1270 target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
1271 target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
1272 target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
1273 target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
1274 target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
1275 target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
1276 target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
1277 target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
1278 target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
1279 target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
1280 target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
1281 target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
1282 target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
1283 target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
53a5960a 1284 unlock_user_struct(target_rusage, target_addr, 1);
579a97f7
FB
1285
1286 return 0;
b409186b
FB
1287}
1288
cbb21eed 1289static inline rlim_t target_to_host_rlim(abi_ulong target_rlim)
81bbe906 1290{
cbb21eed 1291 abi_ulong target_rlim_swap;
95b33b2f
WT
1292 rlim_t result;
1293
cbb21eed
MB
1294 target_rlim_swap = tswapal(target_rlim);
1295 if (target_rlim_swap == TARGET_RLIM_INFINITY)
1296 return RLIM_INFINITY;
1297
1298 result = target_rlim_swap;
1299 if (target_rlim_swap != (rlim_t)result)
1300 return RLIM_INFINITY;
95b33b2f
WT
1301
1302 return result;
81bbe906
TY
1303}
1304
cbb21eed 1305static inline abi_ulong host_to_target_rlim(rlim_t rlim)
81bbe906 1306{
cbb21eed
MB
1307 abi_ulong target_rlim_swap;
1308 abi_ulong result;
95b33b2f 1309
cbb21eed 1310 if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
95b33b2f 1311 target_rlim_swap = TARGET_RLIM_INFINITY;
81bbe906 1312 else
95b33b2f 1313 target_rlim_swap = rlim;
cbb21eed 1314 result = tswapal(target_rlim_swap);
95b33b2f
WT
1315
1316 return result;
81bbe906
TY
1317}
1318
e22b7015
WT
1319static inline int target_to_host_resource(int code)
1320{
1321 switch (code) {
1322 case TARGET_RLIMIT_AS:
1323 return RLIMIT_AS;
1324 case TARGET_RLIMIT_CORE:
1325 return RLIMIT_CORE;
1326 case TARGET_RLIMIT_CPU:
1327 return RLIMIT_CPU;
1328 case TARGET_RLIMIT_DATA:
1329 return RLIMIT_DATA;
1330 case TARGET_RLIMIT_FSIZE:
1331 return RLIMIT_FSIZE;
1332 case TARGET_RLIMIT_LOCKS:
1333 return RLIMIT_LOCKS;
1334 case TARGET_RLIMIT_MEMLOCK:
1335 return RLIMIT_MEMLOCK;
1336 case TARGET_RLIMIT_MSGQUEUE:
1337 return RLIMIT_MSGQUEUE;
1338 case TARGET_RLIMIT_NICE:
1339 return RLIMIT_NICE;
1340 case TARGET_RLIMIT_NOFILE:
1341 return RLIMIT_NOFILE;
1342 case TARGET_RLIMIT_NPROC:
1343 return RLIMIT_NPROC;
1344 case TARGET_RLIMIT_RSS:
1345 return RLIMIT_RSS;
1346 case TARGET_RLIMIT_RTPRIO:
1347 return RLIMIT_RTPRIO;
1348 case TARGET_RLIMIT_SIGPENDING:
1349 return RLIMIT_SIGPENDING;
1350 case TARGET_RLIMIT_STACK:
1351 return RLIMIT_STACK;
1352 default:
1353 return code;
1354 }
1355}
1356
788f5ec4
TS
1357static inline abi_long copy_from_user_timeval(struct timeval *tv,
1358 abi_ulong target_tv_addr)
31e31b8a 1359{
53a5960a
PB
1360 struct target_timeval *target_tv;
1361
788f5ec4 1362 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
579a97f7 1363 return -TARGET_EFAULT;
788f5ec4
TS
1364
1365 __get_user(tv->tv_sec, &target_tv->tv_sec);
1366 __get_user(tv->tv_usec, &target_tv->tv_usec);
1367
1368 unlock_user_struct(target_tv, target_tv_addr, 0);
579a97f7
FB
1369
1370 return 0;
31e31b8a
FB
1371}
1372
788f5ec4
TS
1373static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
1374 const struct timeval *tv)
31e31b8a 1375{
53a5960a
PB
1376 struct target_timeval *target_tv;
1377
788f5ec4 1378 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
579a97f7 1379 return -TARGET_EFAULT;
788f5ec4
TS
1380
1381 __put_user(tv->tv_sec, &target_tv->tv_sec);
1382 __put_user(tv->tv_usec, &target_tv->tv_usec);
1383
1384 unlock_user_struct(target_tv, target_tv_addr, 1);
579a97f7
FB
1385
1386 return 0;
31e31b8a
FB
1387}
1388
ef4467e9
PB
1389static inline abi_long copy_from_user_timezone(struct timezone *tz,
1390 abi_ulong target_tz_addr)
1391{
1392 struct target_timezone *target_tz;
1393
1394 if (!lock_user_struct(VERIFY_READ, target_tz, target_tz_addr, 1)) {
1395 return -TARGET_EFAULT;
1396 }
1397
1398 __get_user(tz->tz_minuteswest, &target_tz->tz_minuteswest);
1399 __get_user(tz->tz_dsttime, &target_tz->tz_dsttime);
1400
1401 unlock_user_struct(target_tz, target_tz_addr, 0);
1402
1403 return 0;
1404}
1405
8ec9cf89
NF
1406#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
1407#include <mqueue.h>
1408
24e1003a
AJ
1409static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
1410 abi_ulong target_mq_attr_addr)
1411{
1412 struct target_mq_attr *target_mq_attr;
1413
1414 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
1415 target_mq_attr_addr, 1))
1416 return -TARGET_EFAULT;
1417
1418 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
1419 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1420 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1421 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1422
1423 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
1424
1425 return 0;
1426}
1427
1428static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
1429 const struct mq_attr *attr)
1430{
1431 struct target_mq_attr *target_mq_attr;
1432
1433 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
1434 target_mq_attr_addr, 0))
1435 return -TARGET_EFAULT;
1436
1437 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
1438 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
1439 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
1440 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
1441
1442 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
1443
1444 return 0;
1445}
8ec9cf89 1446#endif
31e31b8a 1447
055e0906 1448#if defined(TARGET_NR_select) || defined(TARGET_NR__newselect)
0da46a6e 1449/* do_select() must return target values and target errnos. */
992f48a0 1450static abi_long do_select(int n,
26edcf41
TS
1451 abi_ulong rfd_addr, abi_ulong wfd_addr,
1452 abi_ulong efd_addr, abi_ulong target_tv_addr)
31e31b8a
FB
1453{
1454 fd_set rfds, wfds, efds;
1455 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
6df9d38d
PM
1456 struct timeval tv;
1457 struct timespec ts, *ts_ptr;
992f48a0 1458 abi_long ret;
31e31b8a 1459
055e0906
MF
1460 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
1461 if (ret) {
1462 return ret;
53a5960a 1463 }
055e0906
MF
1464 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
1465 if (ret) {
1466 return ret;
53a5960a 1467 }
055e0906
MF
1468 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
1469 if (ret) {
1470 return ret;
53a5960a 1471 }
3b46e624 1472
26edcf41 1473 if (target_tv_addr) {
788f5ec4
TS
1474 if (copy_from_user_timeval(&tv, target_tv_addr))
1475 return -TARGET_EFAULT;
6df9d38d
PM
1476 ts.tv_sec = tv.tv_sec;
1477 ts.tv_nsec = tv.tv_usec * 1000;
1478 ts_ptr = &ts;
31e31b8a 1479 } else {
6df9d38d 1480 ts_ptr = NULL;
31e31b8a 1481 }
26edcf41 1482
6df9d38d
PM
1483 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
1484 ts_ptr, NULL));
53a5960a 1485
26edcf41
TS
1486 if (!is_error(ret)) {
1487 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
1488 return -TARGET_EFAULT;
1489 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
1490 return -TARGET_EFAULT;
1491 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
1492 return -TARGET_EFAULT;
31e31b8a 1493
6df9d38d
PM
1494 if (target_tv_addr) {
1495 tv.tv_sec = ts.tv_sec;
1496 tv.tv_usec = ts.tv_nsec / 1000;
1497 if (copy_to_user_timeval(target_tv_addr, &tv)) {
1498 return -TARGET_EFAULT;
1499 }
1500 }
31e31b8a 1501 }
579a97f7 1502
31e31b8a
FB
1503 return ret;
1504}
5457dc9e
LV
1505
1506#if defined(TARGET_WANT_OLD_SYS_SELECT)
1507static abi_long do_old_select(abi_ulong arg1)
1508{
1509 struct target_sel_arg_struct *sel;
1510 abi_ulong inp, outp, exp, tvp;
1511 long nsel;
1512
1513 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1)) {
1514 return -TARGET_EFAULT;
1515 }
1516
1517 nsel = tswapal(sel->n);
1518 inp = tswapal(sel->inp);
1519 outp = tswapal(sel->outp);
1520 exp = tswapal(sel->exp);
1521 tvp = tswapal(sel->tvp);
1522
1523 unlock_user_struct(sel, arg1, 0);
1524
1525 return do_select(nsel, inp, outp, exp, tvp);
1526}
1527#endif
055e0906 1528#endif
31e31b8a 1529
099d6b0f
RV
1530static abi_long do_pipe2(int host_pipe[], int flags)
1531{
1532#ifdef CONFIG_PIPE2
1533 return pipe2(host_pipe, flags);
1534#else
1535 return -ENOSYS;
1536#endif
1537}
1538
fb41a66e
RH
1539static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1540 int flags, int is_pipe2)
099d6b0f
RV
1541{
1542 int host_pipe[2];
1543 abi_long ret;
1544 ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1545
1546 if (is_error(ret))
1547 return get_errno(ret);
fb41a66e
RH
1548
1549 /* Several targets have special calling conventions for the original
1550 pipe syscall, but didn't replicate this into the pipe2 syscall. */
1551 if (!is_pipe2) {
1552#if defined(TARGET_ALPHA)
1553 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1554 return host_pipe[0];
1555#elif defined(TARGET_MIPS)
1556 ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1557 return host_pipe[0];
1558#elif defined(TARGET_SH4)
597c0212 1559 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
fb41a66e 1560 return host_pipe[0];
82f05b69
PM
1561#elif defined(TARGET_SPARC)
1562 ((CPUSPARCState*)cpu_env)->regwptr[1] = host_pipe[1];
1563 return host_pipe[0];
597c0212 1564#endif
fb41a66e
RH
1565 }
1566
099d6b0f
RV
1567 if (put_user_s32(host_pipe[0], pipedes)
1568 || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1569 return -TARGET_EFAULT;
099d6b0f
RV
1570 return get_errno(ret);
1571}
1572
b975b83b
LL
1573static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1574 abi_ulong target_addr,
1575 socklen_t len)
1576{
1577 struct target_ip_mreqn *target_smreqn;
1578
1579 target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1580 if (!target_smreqn)
1581 return -TARGET_EFAULT;
1582 mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1583 mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1584 if (len == sizeof(struct target_ip_mreqn))
cbb21eed 1585 mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
b975b83b
LL
1586 unlock_user(target_smreqn, target_addr, 0);
1587
1588 return 0;
1589}
1590
7b36f782 1591static inline abi_long target_to_host_sockaddr(int fd, struct sockaddr *addr,
579a97f7
FB
1592 abi_ulong target_addr,
1593 socklen_t len)
7854b056 1594{
607175e0
AJ
1595 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1596 sa_family_t sa_family;
53a5960a
PB
1597 struct target_sockaddr *target_saddr;
1598
7b36f782
LV
1599 if (fd_trans_target_to_host_addr(fd)) {
1600 return fd_trans_target_to_host_addr(fd)(addr, target_addr, len);
1601 }
1602
579a97f7
FB
1603 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1604 if (!target_saddr)
1605 return -TARGET_EFAULT;
607175e0
AJ
1606
1607 sa_family = tswap16(target_saddr->sa_family);
1608
1609 /* Oops. The caller might send a incomplete sun_path; sun_path
1610 * must be terminated by \0 (see the manual page), but
1611 * unfortunately it is quite common to specify sockaddr_un
1612 * length as "strlen(x->sun_path)" while it should be
1613 * "strlen(...) + 1". We'll fix that here if needed.
1614 * Linux kernel has a similar feature.
1615 */
1616
1617 if (sa_family == AF_UNIX) {
1618 if (len < unix_maxlen && len > 0) {
1619 char *cp = (char*)target_saddr;
1620
1621 if ( cp[len-1] && !cp[len] )
1622 len++;
1623 }
1624 if (len > unix_maxlen)
1625 len = unix_maxlen;
1626 }
1627
53a5960a 1628 memcpy(addr, target_saddr, len);
607175e0 1629 addr->sa_family = sa_family;
6c5b5645
LV
1630 if (sa_family == AF_NETLINK) {
1631 struct sockaddr_nl *nladdr;
1632
1633 nladdr = (struct sockaddr_nl *)addr;
1634 nladdr->nl_pid = tswap32(nladdr->nl_pid);
1635 nladdr->nl_groups = tswap32(nladdr->nl_groups);
1636 } else if (sa_family == AF_PACKET) {
33a29b51
JT
1637 struct target_sockaddr_ll *lladdr;
1638
1639 lladdr = (struct target_sockaddr_ll *)addr;
1640 lladdr->sll_ifindex = tswap32(lladdr->sll_ifindex);
1641 lladdr->sll_hatype = tswap16(lladdr->sll_hatype);
1642 }
53a5960a 1643 unlock_user(target_saddr, target_addr, 0);
579a97f7
FB
1644
1645 return 0;
7854b056
FB
1646}
1647
579a97f7
FB
1648static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1649 struct sockaddr *addr,
1650 socklen_t len)
7854b056 1651{
53a5960a
PB
1652 struct target_sockaddr *target_saddr;
1653
a1e22192
PM
1654 if (len == 0) {
1655 return 0;
1656 }
6860710c 1657 assert(addr);
a1e22192 1658
579a97f7
FB
1659 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1660 if (!target_saddr)
1661 return -TARGET_EFAULT;
53a5960a 1662 memcpy(target_saddr, addr, len);
a1e22192
PM
1663 if (len >= offsetof(struct target_sockaddr, sa_family) +
1664 sizeof(target_saddr->sa_family)) {
1665 target_saddr->sa_family = tswap16(addr->sa_family);
1666 }
1667 if (addr->sa_family == AF_NETLINK && len >= sizeof(struct sockaddr_nl)) {
6c5b5645
LV
1668 struct sockaddr_nl *target_nl = (struct sockaddr_nl *)target_saddr;
1669 target_nl->nl_pid = tswap32(target_nl->nl_pid);
1670 target_nl->nl_groups = tswap32(target_nl->nl_groups);
a82ea939
LV
1671 } else if (addr->sa_family == AF_PACKET) {
1672 struct sockaddr_ll *target_ll = (struct sockaddr_ll *)target_saddr;
1673 target_ll->sll_ifindex = tswap32(target_ll->sll_ifindex);
1674 target_ll->sll_hatype = tswap16(target_ll->sll_hatype);
ee1ac3a1
HD
1675 } else if (addr->sa_family == AF_INET6 &&
1676 len >= sizeof(struct target_sockaddr_in6)) {
1677 struct target_sockaddr_in6 *target_in6 =
1678 (struct target_sockaddr_in6 *)target_saddr;
1679 target_in6->sin6_scope_id = tswap16(target_in6->sin6_scope_id);
6c5b5645 1680 }
53a5960a 1681 unlock_user(target_saddr, target_addr, len);
579a97f7
FB
1682
1683 return 0;
7854b056
FB
1684}
1685
5a4a898d
FB
1686static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1687 struct target_msghdr *target_msgh)
7854b056
FB
1688{
1689 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
5a4a898d
FB
1690 abi_long msg_controllen;
1691 abi_ulong target_cmsg_addr;
ee104587 1692 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
7854b056 1693 socklen_t space = 0;
5a4a898d 1694
cbb21eed 1695 msg_controllen = tswapal(target_msgh->msg_controllen);
5a4a898d
FB
1696 if (msg_controllen < sizeof (struct target_cmsghdr))
1697 goto the_end;
cbb21eed 1698 target_cmsg_addr = tswapal(target_msgh->msg_control);
5a4a898d 1699 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
ee104587 1700 target_cmsg_start = target_cmsg;
5a4a898d
FB
1701 if (!target_cmsg)
1702 return -TARGET_EFAULT;
7854b056
FB
1703
1704 while (cmsg && target_cmsg) {
1705 void *data = CMSG_DATA(cmsg);
1706 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1707
cbb21eed 1708 int len = tswapal(target_cmsg->cmsg_len)
ad762b99 1709 - sizeof(struct target_cmsghdr);
7854b056
FB
1710
1711 space += CMSG_SPACE(len);
1712 if (space > msgh->msg_controllen) {
1713 space -= CMSG_SPACE(len);
c2aeb258
PM
1714 /* This is a QEMU bug, since we allocated the payload
1715 * area ourselves (unlike overflow in host-to-target
1716 * conversion, which is just the guest giving us a buffer
1717 * that's too small). It can't happen for the payload types
1718 * we currently support; if it becomes an issue in future
1719 * we would need to improve our allocation strategy to
1720 * something more intelligent than "twice the size of the
1721 * target buffer we're reading from".
1722 */
31febb71 1723 gemu_log("Host cmsg overflow\n");
7854b056
FB
1724 break;
1725 }
1726
dbf4f796
PJ
1727 if (tswap32(target_cmsg->cmsg_level) == TARGET_SOL_SOCKET) {
1728 cmsg->cmsg_level = SOL_SOCKET;
1729 } else {
1730 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1731 }
7854b056
FB
1732 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1733 cmsg->cmsg_len = CMSG_LEN(len);
1734
30b8b68e 1735 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
7854b056
FB
1736 int *fd = (int *)data;
1737 int *target_fd = (int *)target_data;
1738 int i, numfds = len / sizeof(int);
1739
876e23cb
PM
1740 for (i = 0; i < numfds; i++) {
1741 __get_user(fd[i], target_fd + i);
1742 }
30b8b68e
AS
1743 } else if (cmsg->cmsg_level == SOL_SOCKET
1744 && cmsg->cmsg_type == SCM_CREDENTIALS) {
1745 struct ucred *cred = (struct ucred *)data;
1746 struct target_ucred *target_cred =
1747 (struct target_ucred *)target_data;
1748
876e23cb
PM
1749 __get_user(cred->pid, &target_cred->pid);
1750 __get_user(cred->uid, &target_cred->uid);
1751 __get_user(cred->gid, &target_cred->gid);
30b8b68e
AS
1752 } else {
1753 gemu_log("Unsupported ancillary data: %d/%d\n",
1754 cmsg->cmsg_level, cmsg->cmsg_type);
1755 memcpy(data, target_data, len);
7854b056
FB
1756 }
1757
1758 cmsg = CMSG_NXTHDR(msgh, cmsg);
ee104587
JN
1759 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1760 target_cmsg_start);
7854b056 1761 }
5a4a898d
FB
1762 unlock_user(target_cmsg, target_cmsg_addr, 0);
1763 the_end:
7854b056 1764 msgh->msg_controllen = space;
5a4a898d 1765 return 0;
7854b056
FB
1766}
1767
5a4a898d
FB
1768static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1769 struct msghdr *msgh)
7854b056
FB
1770{
1771 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
5a4a898d
FB
1772 abi_long msg_controllen;
1773 abi_ulong target_cmsg_addr;
ee104587 1774 struct target_cmsghdr *target_cmsg, *target_cmsg_start;
7854b056
FB
1775 socklen_t space = 0;
1776
cbb21eed 1777 msg_controllen = tswapal(target_msgh->msg_controllen);
5a4a898d
FB
1778 if (msg_controllen < sizeof (struct target_cmsghdr))
1779 goto the_end;
cbb21eed 1780 target_cmsg_addr = tswapal(target_msgh->msg_control);
5a4a898d 1781 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
ee104587 1782 target_cmsg_start = target_cmsg;
5a4a898d
FB
1783 if (!target_cmsg)
1784 return -TARGET_EFAULT;
1785
7854b056
FB
1786 while (cmsg && target_cmsg) {
1787 void *data = CMSG_DATA(cmsg);
1788 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1789
ad762b99 1790 int len = cmsg->cmsg_len - sizeof(struct cmsghdr);
c2aeb258 1791 int tgt_len, tgt_space;
7854b056 1792
c2aeb258
PM
1793 /* We never copy a half-header but may copy half-data;
1794 * this is Linux's behaviour in put_cmsg(). Note that
1795 * truncation here is a guest problem (which we report
1796 * to the guest via the CTRUNC bit), unlike truncation
1797 * in target_to_host_cmsg, which is a QEMU bug.
1798 */
7174970a 1799 if (msg_controllen < sizeof(struct target_cmsghdr)) {
c2aeb258 1800 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
7854b056
FB
1801 break;
1802 }
1803
dbf4f796
PJ
1804 if (cmsg->cmsg_level == SOL_SOCKET) {
1805 target_cmsg->cmsg_level = tswap32(TARGET_SOL_SOCKET);
1806 } else {
1807 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1808 }
7854b056 1809 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
7854b056 1810
c2aeb258
PM
1811 /* Payload types which need a different size of payload on
1812 * the target must adjust tgt_len here.
1813 */
1814 switch (cmsg->cmsg_level) {
1815 case SOL_SOCKET:
1816 switch (cmsg->cmsg_type) {
1817 case SO_TIMESTAMP:
1818 tgt_len = sizeof(struct target_timeval);
1819 break;
1820 default:
1821 break;
1822 }
1823 default:
7174970a 1824 tgt_len = len;
c2aeb258
PM
1825 break;
1826 }
1827
7174970a 1828 if (msg_controllen < TARGET_CMSG_LEN(tgt_len)) {
c2aeb258 1829 target_msgh->msg_flags |= tswap32(MSG_CTRUNC);
7174970a 1830 tgt_len = msg_controllen - sizeof(struct target_cmsghdr);
c2aeb258
PM
1831 }
1832
1833 /* We must now copy-and-convert len bytes of payload
1834 * into tgt_len bytes of destination space. Bear in mind
1835 * that in both source and destination we may be dealing
1836 * with a truncated value!
1837 */
52b65494
HD
1838 switch (cmsg->cmsg_level) {
1839 case SOL_SOCKET:
1840 switch (cmsg->cmsg_type) {
1841 case SCM_RIGHTS:
1842 {
1843 int *fd = (int *)data;
1844 int *target_fd = (int *)target_data;
c2aeb258 1845 int i, numfds = tgt_len / sizeof(int);
52b65494 1846
876e23cb
PM
1847 for (i = 0; i < numfds; i++) {
1848 __put_user(fd[i], target_fd + i);
1849 }
52b65494
HD
1850 break;
1851 }
1852 case SO_TIMESTAMP:
1853 {
1854 struct timeval *tv = (struct timeval *)data;
1855 struct target_timeval *target_tv =
1856 (struct target_timeval *)target_data;
1857
c2aeb258
PM
1858 if (len != sizeof(struct timeval) ||
1859 tgt_len != sizeof(struct target_timeval)) {
52b65494 1860 goto unimplemented;
c2aeb258 1861 }
52b65494
HD
1862
1863 /* copy struct timeval to target */
876e23cb
PM
1864 __put_user(tv->tv_sec, &target_tv->tv_sec);
1865 __put_user(tv->tv_usec, &target_tv->tv_usec);
52b65494
HD
1866 break;
1867 }
4bc29756
HD
1868 case SCM_CREDENTIALS:
1869 {
1870 struct ucred *cred = (struct ucred *)data;
1871 struct target_ucred *target_cred =
1872 (struct target_ucred *)target_data;
1873
1874 __put_user(cred->pid, &target_cred->pid);
1875 __put_user(cred->uid, &target_cred->uid);
1876 __put_user(cred->gid, &target_cred->gid);
1877 break;
1878 }
52b65494
HD
1879 default:
1880 goto unimplemented;
1881 }
1882 break;
7854b056 1883
ee1ac3a1
HD
1884 case SOL_IP:
1885 switch (cmsg->cmsg_type) {
1886 case IP_TTL:
1887 {
1888 uint32_t *v = (uint32_t *)data;
1889 uint32_t *t_int = (uint32_t *)target_data;
1890
7174970a
PM
1891 if (len != sizeof(uint32_t) ||
1892 tgt_len != sizeof(uint32_t)) {
1893 goto unimplemented;
1894 }
ee1ac3a1
HD
1895 __put_user(*v, t_int);
1896 break;
1897 }
1898 case IP_RECVERR:
1899 {
1900 struct errhdr_t {
1901 struct sock_extended_err ee;
1902 struct sockaddr_in offender;
1903 };
1904 struct errhdr_t *errh = (struct errhdr_t *)data;
1905 struct errhdr_t *target_errh =
1906 (struct errhdr_t *)target_data;
1907
7174970a
PM
1908 if (len != sizeof(struct errhdr_t) ||
1909 tgt_len != sizeof(struct errhdr_t)) {
1910 goto unimplemented;
1911 }
ee1ac3a1
HD
1912 __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
1913 __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
1914 __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
1915 __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
1916 __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
1917 __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
1918 __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
1919 host_to_target_sockaddr((unsigned long) &target_errh->offender,
1920 (void *) &errh->offender, sizeof(errh->offender));
1921 break;
1922 }
1923 default:
1924 goto unimplemented;
1925 }
1926 break;
1927
1928 case SOL_IPV6:
1929 switch (cmsg->cmsg_type) {
1930 case IPV6_HOPLIMIT:
1931 {
1932 uint32_t *v = (uint32_t *)data;
1933 uint32_t *t_int = (uint32_t *)target_data;
1934
7174970a
PM
1935 if (len != sizeof(uint32_t) ||
1936 tgt_len != sizeof(uint32_t)) {
1937 goto unimplemented;
1938 }
ee1ac3a1
HD
1939 __put_user(*v, t_int);
1940 break;
1941 }
1942 case IPV6_RECVERR:
1943 {
1944 struct errhdr6_t {
1945 struct sock_extended_err ee;
1946 struct sockaddr_in6 offender;
1947 };
1948 struct errhdr6_t *errh = (struct errhdr6_t *)data;
1949 struct errhdr6_t *target_errh =
1950 (struct errhdr6_t *)target_data;
1951
7174970a
PM
1952 if (len != sizeof(struct errhdr6_t) ||
1953 tgt_len != sizeof(struct errhdr6_t)) {
1954 goto unimplemented;
1955 }
ee1ac3a1
HD
1956 __put_user(errh->ee.ee_errno, &target_errh->ee.ee_errno);
1957 __put_user(errh->ee.ee_origin, &target_errh->ee.ee_origin);
1958 __put_user(errh->ee.ee_type, &target_errh->ee.ee_type);
1959 __put_user(errh->ee.ee_code, &target_errh->ee.ee_code);
1960 __put_user(errh->ee.ee_pad, &target_errh->ee.ee_pad);
1961 __put_user(errh->ee.ee_info, &target_errh->ee.ee_info);
1962 __put_user(errh->ee.ee_data, &target_errh->ee.ee_data);
1963 host_to_target_sockaddr((unsigned long) &target_errh->offender,
1964 (void *) &errh->offender, sizeof(errh->offender));
1965 break;
1966 }
1967 default:
1968 goto unimplemented;
1969 }
1970 break;
1971
52b65494
HD
1972 default:
1973 unimplemented:
aebf5bc7
JH
1974 gemu_log("Unsupported ancillary data: %d/%d\n",
1975 cmsg->cmsg_level, cmsg->cmsg_type);
c2aeb258
PM
1976 memcpy(target_data, data, MIN(len, tgt_len));
1977 if (tgt_len > len) {
1978 memset(target_data + len, 0, tgt_len - len);
1979 }
7854b056
FB
1980 }
1981
7174970a
PM
1982 target_cmsg->cmsg_len = tswapal(TARGET_CMSG_LEN(tgt_len));
1983 tgt_space = TARGET_CMSG_SPACE(tgt_len);
c2aeb258
PM
1984 if (msg_controllen < tgt_space) {
1985 tgt_space = msg_controllen;
1986 }
1987 msg_controllen -= tgt_space;
1988 space += tgt_space;
7854b056 1989 cmsg = CMSG_NXTHDR(msgh, cmsg);
ee104587
JN
1990 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg,
1991 target_cmsg_start);
7854b056 1992 }
5a4a898d
FB
1993 unlock_user(target_cmsg, target_cmsg_addr, space);
1994 the_end:
cbb21eed 1995 target_msgh->msg_controllen = tswapal(space);
5a4a898d 1996 return 0;
7854b056
FB
1997}
1998
6c5b5645
LV
1999static void tswap_nlmsghdr(struct nlmsghdr *nlh)
2000{
2001 nlh->nlmsg_len = tswap32(nlh->nlmsg_len);
2002 nlh->nlmsg_type = tswap16(nlh->nlmsg_type);
2003 nlh->nlmsg_flags = tswap16(nlh->nlmsg_flags);
2004 nlh->nlmsg_seq = tswap32(nlh->nlmsg_seq);
2005 nlh->nlmsg_pid = tswap32(nlh->nlmsg_pid);
2006}
2007
2008static abi_long host_to_target_for_each_nlmsg(struct nlmsghdr *nlh,
2009 size_t len,
2010 abi_long (*host_to_target_nlmsg)
2011 (struct nlmsghdr *))
2012{
2013 uint32_t nlmsg_len;
2014 abi_long ret;
2015
2016 while (len > sizeof(struct nlmsghdr)) {
2017
2018 nlmsg_len = nlh->nlmsg_len;
2019 if (nlmsg_len < sizeof(struct nlmsghdr) ||
2020 nlmsg_len > len) {
2021 break;
2022 }
2023
2024 switch (nlh->nlmsg_type) {
2025 case NLMSG_DONE:
2026 tswap_nlmsghdr(nlh);
2027 return 0;
2028 case NLMSG_NOOP:
2029 break;
2030 case NLMSG_ERROR:
2031 {
2032 struct nlmsgerr *e = NLMSG_DATA(nlh);
2033 e->error = tswap32(e->error);
2034 tswap_nlmsghdr(&e->msg);
2035 tswap_nlmsghdr(nlh);
2036 return 0;
2037 }
2038 default:
2039 ret = host_to_target_nlmsg(nlh);
2040 if (ret < 0) {
2041 tswap_nlmsghdr(nlh);
2042 return ret;
2043 }
2044 break;
2045 }
2046 tswap_nlmsghdr(nlh);
2047 len -= NLMSG_ALIGN(nlmsg_len);
2048 nlh = (struct nlmsghdr *)(((char*)nlh) + NLMSG_ALIGN(nlmsg_len));
2049 }
2050 return 0;
2051}
2052
2053static abi_long target_to_host_for_each_nlmsg(struct nlmsghdr *nlh,
2054 size_t len,
2055 abi_long (*target_to_host_nlmsg)
2056 (struct nlmsghdr *))
2057{
2058 int ret;
2059
2060 while (len > sizeof(struct nlmsghdr)) {
2061 if (tswap32(nlh->nlmsg_len) < sizeof(struct nlmsghdr) ||
2062 tswap32(nlh->nlmsg_len) > len) {
2063 break;
2064 }
2065 tswap_nlmsghdr(nlh);
2066 switch (nlh->nlmsg_type) {
2067 case NLMSG_DONE:
2068 return 0;
2069 case NLMSG_NOOP:
2070 break;
2071 case NLMSG_ERROR:
2072 {
2073 struct nlmsgerr *e = NLMSG_DATA(nlh);
2074 e->error = tswap32(e->error);
2075 tswap_nlmsghdr(&e->msg);
84f34b00 2076 return 0;
6c5b5645
LV
2077 }
2078 default:
2079 ret = target_to_host_nlmsg(nlh);
2080 if (ret < 0) {
2081 return ret;
2082 }
2083 }
2084 len -= NLMSG_ALIGN(nlh->nlmsg_len);
2085 nlh = (struct nlmsghdr *)(((char *)nlh) + NLMSG_ALIGN(nlh->nlmsg_len));
2086 }
2087 return 0;
2088}
2089
575b22b1 2090#ifdef CONFIG_RTNETLINK
c5dff280
LV
2091static abi_long host_to_target_for_each_nlattr(struct nlattr *nlattr,
2092 size_t len, void *context,
2093 abi_long (*host_to_target_nlattr)
2094 (struct nlattr *,
2095 void *context))
2096{
2097 unsigned short nla_len;
2098 abi_long ret;
2099
2100 while (len > sizeof(struct nlattr)) {
2101 nla_len = nlattr->nla_len;
2102 if (nla_len < sizeof(struct nlattr) ||
2103 nla_len > len) {
2104 break;
2105 }
2106 ret = host_to_target_nlattr(nlattr, context);
2107 nlattr->nla_len = tswap16(nlattr->nla_len);
2108 nlattr->nla_type = tswap16(nlattr->nla_type);
2109 if (ret < 0) {
2110 return ret;
2111 }
2112 len -= NLA_ALIGN(nla_len);
2113 nlattr = (struct nlattr *)(((char *)nlattr) + NLA_ALIGN(nla_len));
2114 }
2115 return 0;
2116}
2117
6c5b5645
LV
2118static abi_long host_to_target_for_each_rtattr(struct rtattr *rtattr,
2119 size_t len,
2120 abi_long (*host_to_target_rtattr)
2121 (struct rtattr *))
2122{
2123 unsigned short rta_len;
2124 abi_long ret;
2125
2126 while (len > sizeof(struct rtattr)) {
2127 rta_len = rtattr->rta_len;
2128 if (rta_len < sizeof(struct rtattr) ||
2129 rta_len > len) {
2130 break;
2131 }
2132 ret = host_to_target_rtattr(rtattr);
2133 rtattr->rta_len = tswap16(rtattr->rta_len);
2134 rtattr->rta_type = tswap16(rtattr->rta_type);
2135 if (ret < 0) {
2136 return ret;
2137 }
2138 len -= RTA_ALIGN(rta_len);
2139 rtattr = (struct rtattr *)(((char *)rtattr) + RTA_ALIGN(rta_len));
2140 }
2141 return 0;
2142}
2143
c5dff280
LV
2144#define NLA_DATA(nla) ((void *)((char *)(nla)) + NLA_HDRLEN)
2145
2146static abi_long host_to_target_data_bridge_nlattr(struct nlattr *nlattr,
2147 void *context)
2148{
2149 uint16_t *u16;
2150 uint32_t *u32;
2151 uint64_t *u64;
2152
2153 switch (nlattr->nla_type) {
2154 /* no data */
60c6b790 2155 case QEMU_IFLA_BR_FDB_FLUSH:
c5dff280
LV
2156 break;
2157 /* binary */
60c6b790 2158 case QEMU_IFLA_BR_GROUP_ADDR:
c5dff280
LV
2159 break;
2160 /* uint8_t */
60c6b790
MP
2161 case QEMU_IFLA_BR_VLAN_FILTERING:
2162 case QEMU_IFLA_BR_TOPOLOGY_CHANGE:
2163 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_DETECTED:
2164 case QEMU_IFLA_BR_MCAST_ROUTER:
2165 case QEMU_IFLA_BR_MCAST_SNOOPING:
2166 case QEMU_IFLA_BR_MCAST_QUERY_USE_IFADDR:
2167 case QEMU_IFLA_BR_MCAST_QUERIER:
2168 case QEMU_IFLA_BR_NF_CALL_IPTABLES:
2169 case QEMU_IFLA_BR_NF_CALL_IP6TABLES:
2170 case QEMU_IFLA_BR_NF_CALL_ARPTABLES:
c5dff280
LV
2171 break;
2172 /* uint16_t */
60c6b790
MP
2173 case QEMU_IFLA_BR_PRIORITY:
2174 case QEMU_IFLA_BR_VLAN_PROTOCOL:
2175 case QEMU_IFLA_BR_GROUP_FWD_MASK:
2176 case QEMU_IFLA_BR_ROOT_PORT:
2177 case QEMU_IFLA_BR_VLAN_DEFAULT_PVID:
c5dff280
LV
2178 u16 = NLA_DATA(nlattr);
2179 *u16 = tswap16(*u16);
2180 break;
2181 /* uint32_t */
60c6b790
MP
2182 case QEMU_IFLA_BR_FORWARD_DELAY:
2183 case QEMU_IFLA_BR_HELLO_TIME:
2184 case QEMU_IFLA_BR_MAX_AGE:
2185 case QEMU_IFLA_BR_AGEING_TIME:
2186 case QEMU_IFLA_BR_STP_STATE:
2187 case QEMU_IFLA_BR_ROOT_PATH_COST:
2188 case QEMU_IFLA_BR_MCAST_HASH_ELASTICITY:
2189 case QEMU_IFLA_BR_MCAST_HASH_MAX:
2190 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_CNT:
2191 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_CNT:
c5dff280
LV
2192 u32 = NLA_DATA(nlattr);
2193 *u32 = tswap32(*u32);
2194 break;
2195 /* uint64_t */
60c6b790
MP
2196 case QEMU_IFLA_BR_HELLO_TIMER:
2197 case QEMU_IFLA_BR_TCN_TIMER:
2198 case QEMU_IFLA_BR_GC_TIMER:
2199 case QEMU_IFLA_BR_TOPOLOGY_CHANGE_TIMER:
2200 case QEMU_IFLA_BR_MCAST_LAST_MEMBER_INTVL:
2201 case QEMU_IFLA_BR_MCAST_MEMBERSHIP_INTVL:
2202 case QEMU_IFLA_BR_MCAST_QUERIER_INTVL:
2203 case QEMU_IFLA_BR_MCAST_QUERY_INTVL:
2204 case QEMU_IFLA_BR_MCAST_QUERY_RESPONSE_INTVL:
2205 case QEMU_IFLA_BR_MCAST_STARTUP_QUERY_INTVL:
c5dff280
LV
2206 u64 = NLA_DATA(nlattr);
2207 *u64 = tswap64(*u64);
2208 break;
2209 /* ifla_bridge_id: uin8_t[] */
60c6b790
MP
2210 case QEMU_IFLA_BR_ROOT_ID:
2211 case QEMU_IFLA_BR_BRIDGE_ID:
c5dff280
LV
2212 break;
2213 default:
60c6b790 2214 gemu_log("Unknown QEMU_IFLA_BR type %d\n", nlattr->nla_type);
c5dff280
LV
2215 break;
2216 }
2217 return 0;
2218}
2219
2220static abi_long host_to_target_slave_data_bridge_nlattr(struct nlattr *nlattr,
2221 void *context)
2222{
2223 uint16_t *u16;
2224 uint32_t *u32;
2225 uint64_t *u64;
2226
2227 switch (nlattr->nla_type) {
2228 /* uint8_t */
60c6b790
MP
2229 case QEMU_IFLA_BRPORT_STATE:
2230 case QEMU_IFLA_BRPORT_MODE:
2231 case QEMU_IFLA_BRPORT_GUARD:
2232 case QEMU_IFLA_BRPORT_PROTECT:
2233 case QEMU_IFLA_BRPORT_FAST_LEAVE:
2234 case QEMU_IFLA_BRPORT_LEARNING:
2235 case QEMU_IFLA_BRPORT_UNICAST_FLOOD:
2236 case QEMU_IFLA_BRPORT_PROXYARP:
2237 case QEMU_IFLA_BRPORT_LEARNING_SYNC:
2238 case QEMU_IFLA_BRPORT_PROXYARP_WIFI:
2239 case QEMU_IFLA_BRPORT_TOPOLOGY_CHANGE_ACK:
2240 case QEMU_IFLA_BRPORT_CONFIG_PENDING:
2241 case QEMU_IFLA_BRPORT_MULTICAST_ROUTER:
c5dff280
LV
2242 break;
2243 /* uint16_t */
60c6b790
MP
2244 case QEMU_IFLA_BRPORT_PRIORITY:
2245 case QEMU_IFLA_BRPORT_DESIGNATED_PORT:
2246 case QEMU_IFLA_BRPORT_DESIGNATED_COST:
2247 case QEMU_IFLA_BRPORT_ID:
2248 case QEMU_IFLA_BRPORT_NO:
c5dff280
LV
2249 u16 = NLA_DATA(nlattr);
2250 *u16 = tswap16(*u16);
2251 break;
2252 /* uin32_t */
60c6b790 2253 case QEMU_IFLA_BRPORT_COST:
c5dff280
LV
2254 u32 = NLA_DATA(nlattr);
2255 *u32 = tswap32(*u32);
2256 break;
2257 /* uint64_t */
60c6b790
MP
2258 case QEMU_IFLA_BRPORT_MESSAGE_AGE_TIMER:
2259 case QEMU_IFLA_BRPORT_FORWARD_DELAY_TIMER:
2260 case QEMU_IFLA_BRPORT_HOLD_TIMER:
c5dff280
LV
2261 u64 = NLA_DATA(nlattr);
2262 *u64 = tswap64(*u64);
2263 break;
2264 /* ifla_bridge_id: uint8_t[] */
60c6b790
MP
2265 case QEMU_IFLA_BRPORT_ROOT_ID:
2266 case QEMU_IFLA_BRPORT_BRIDGE_ID:
c5dff280
LV
2267 break;
2268 default:
60c6b790 2269 gemu_log("Unknown QEMU_IFLA_BRPORT type %d\n", nlattr->nla_type);
c5dff280
LV
2270 break;
2271 }
2272 return 0;
2273}
2274
2275struct linkinfo_context {
2276 int len;
2277 char *name;
2278 int slave_len;
2279 char *slave_name;
2280};
2281
2282static abi_long host_to_target_data_linkinfo_nlattr(struct nlattr *nlattr,
2283 void *context)
2284{
2285 struct linkinfo_context *li_context = context;
2286
2287 switch (nlattr->nla_type) {
2288 /* string */
60c6b790 2289 case QEMU_IFLA_INFO_KIND:
c5dff280
LV
2290 li_context->name = NLA_DATA(nlattr);
2291 li_context->len = nlattr->nla_len - NLA_HDRLEN;
2292 break;
60c6b790 2293 case QEMU_IFLA_INFO_SLAVE_KIND:
c5dff280
LV
2294 li_context->slave_name = NLA_DATA(nlattr);
2295 li_context->slave_len = nlattr->nla_len - NLA_HDRLEN;
2296 break;
2297 /* stats */
60c6b790 2298 case QEMU_IFLA_INFO_XSTATS:
c5dff280
LV
2299 /* FIXME: only used by CAN */
2300 break;
2301 /* nested */
60c6b790 2302 case QEMU_IFLA_INFO_DATA:
c5dff280
LV
2303 if (strncmp(li_context->name, "bridge",
2304 li_context->len) == 0) {
2305 return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
2306 nlattr->nla_len,
2307 NULL,
2308 host_to_target_data_bridge_nlattr);
2309 } else {
60c6b790 2310 gemu_log("Unknown QEMU_IFLA_INFO_KIND %s\n", li_context->name);
c5dff280
LV
2311 }
2312 break;
60c6b790 2313 case QEMU_IFLA_INFO_SLAVE_DATA:
c5dff280
LV
2314 if (strncmp(li_context->slave_name, "bridge",
2315 li_context->slave_len) == 0) {
2316 return host_to_target_for_each_nlattr(NLA_DATA(nlattr),
2317 nlattr->nla_len,
2318 NULL,
2319 host_to_target_slave_data_bridge_nlattr);
2320 } else {
60c6b790 2321 gemu_log("Unknown QEMU_IFLA_INFO_SLAVE_KIND %s\n",
c5dff280
LV
2322 li_context->slave_name);
2323 }
2324 break;
2325 default:
60c6b790 2326 gemu_log("Unknown host QEMU_IFLA_INFO type: %d\n", nlattr->nla_type);
c5dff280
LV
2327 break;
2328 }
2329
2330 return 0;
2331}
2332
2333static abi_long host_to_target_data_inet_nlattr(struct nlattr *nlattr,
2334 void *context)
2335{
2336 uint32_t *u32;
2337 int i;
2338
2339 switch (nlattr->nla_type) {
60c6b790 2340 case QEMU_IFLA_INET_CONF:
c5dff280
LV
2341 u32 = NLA_DATA(nlattr);
2342 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u32);
2343 i++) {
2344 u32[i] = tswap32(u32[i]);
2345 }
2346 break;
2347 default:
2348 gemu_log("Unknown host AF_INET type: %d\n", nlattr->nla_type);
2349 }
2350 return 0;
2351}
2352
2353static abi_long host_to_target_data_inet6_nlattr(struct nlattr *nlattr,
2354 void *context)
2355{
2356 uint32_t *u32;
2357 uint64_t *u64;
2358 struct ifla_cacheinfo *ci;
2359 int i;
2360
2361 switch (nlattr->nla_type) {
2362 /* binaries */
60c6b790 2363 case QEMU_IFLA_INET6_TOKEN:
c5dff280
LV
2364 break;
2365 /* uint8_t */
60c6b790 2366 case QEMU_IFLA_INET6_ADDR_GEN_MODE:
c5dff280
LV
2367 break;
2368 /* uint32_t */
60c6b790 2369 case QEMU_IFLA_INET6_FLAGS:
c5dff280
LV
2370 u32 = NLA_DATA(nlattr);
2371 *u32 = tswap32(*u32);
2372 break;
2373 /* uint32_t[] */
60c6b790 2374 case QEMU_IFLA_INET6_CONF:
c5dff280
LV
2375 u32 = NLA_DATA(nlattr);
2376 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u32);
2377 i++) {
2378 u32[i] = tswap32(u32[i]);
2379 }
2380 break;
2381 /* ifla_cacheinfo */
60c6b790 2382 case QEMU_IFLA_INET6_CACHEINFO:
c5dff280
LV
2383 ci = NLA_DATA(nlattr);
2384 ci->max_reasm_len = tswap32(ci->max_reasm_len);
2385 ci->tstamp = tswap32(ci->tstamp);
2386 ci->reachable_time = tswap32(ci->reachable_time);
2387 ci->retrans_time = tswap32(ci->retrans_time);
2388 break;
2389 /* uint64_t[] */
60c6b790
MP
2390 case QEMU_IFLA_INET6_STATS:
2391 case QEMU_IFLA_INET6_ICMP6STATS:
c5dff280
LV
2392 u64 = NLA_DATA(nlattr);
2393 for (i = 0; i < (nlattr->nla_len - NLA_HDRLEN) / sizeof(*u64);
2394 i++) {
2395 u64[i] = tswap64(u64[i]);
2396 }
2397 break;
2398 default:
2399 gemu_log("Unknown host AF_INET6 type: %d\n", nlattr->nla_type);
2400 }
2401 return 0;
2402}
2403
2404static abi_long host_to_target_data_spec_nlattr(struct nlattr *nlattr,
2405 void *context)
2406{
2407 switch (nlattr->nla_type) {
2408 case AF_INET:
2409 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), nlattr->nla_len,
2410 NULL,
2411 host_to_target_data_inet_nlattr);
2412 case AF_INET6:
2413 return host_to_target_for_each_nlattr(NLA_DATA(nlattr), nlattr->nla_len,
2414 NULL,
2415 host_to_target_data_inet6_nlattr);
2416 default:
2417 gemu_log("Unknown host AF_SPEC type: %d\n", nlattr->nla_type);
2418 break;
2419 }
2420 return 0;
2421}
2422
6c5b5645
LV
2423static abi_long host_to_target_data_link_rtattr(struct rtattr *rtattr)
2424{
2425 uint32_t *u32;
2426 struct rtnl_link_stats *st;
2427 struct rtnl_link_stats64 *st64;
2428 struct rtnl_link_ifmap *map;
c5dff280 2429 struct linkinfo_context li_context;
6c5b5645
LV
2430
2431 switch (rtattr->rta_type) {
2432 /* binary stream */
60c6b790
MP
2433 case QEMU_IFLA_ADDRESS:
2434 case QEMU_IFLA_BROADCAST:
6c5b5645 2435 /* string */
60c6b790
MP
2436 case QEMU_IFLA_IFNAME:
2437 case QEMU_IFLA_QDISC:
6c5b5645
LV
2438 break;
2439 /* uin8_t */
60c6b790
MP
2440 case QEMU_IFLA_OPERSTATE:
2441 case QEMU_IFLA_LINKMODE:
2442 case QEMU_IFLA_CARRIER:
2443 case QEMU_IFLA_PROTO_DOWN:
6c5b5645
LV
2444 break;
2445 /* uint32_t */
60c6b790
MP
2446 case QEMU_IFLA_MTU:
2447 case QEMU_IFLA_LINK:
2448 case QEMU_IFLA_WEIGHT:
2449 case QEMU_IFLA_TXQLEN:
2450 case QEMU_IFLA_CARRIER_CHANGES:
2451 case QEMU_IFLA_NUM_RX_QUEUES:
2452 case QEMU_IFLA_NUM_TX_QUEUES:
2453 case QEMU_IFLA_PROMISCUITY:
2454 case QEMU_IFLA_EXT_MASK:
2455 case QEMU_IFLA_LINK_NETNSID:
2456 case QEMU_IFLA_GROUP:
2457 case QEMU_IFLA_MASTER:
2458 case QEMU_IFLA_NUM_VF:
a1488b86
LV
2459 case QEMU_IFLA_GSO_MAX_SEGS:
2460 case QEMU_IFLA_GSO_MAX_SIZE:
6c5b5645
LV
2461 u32 = RTA_DATA(rtattr);
2462 *u32 = tswap32(*u32);
2463 break;
2464 /* struct rtnl_link_stats */
60c6b790 2465 case QEMU_IFLA_STATS:
6c5b5645
LV
2466 st = RTA_DATA(rtattr);
2467 st->rx_packets = tswap32(st->rx_packets);
2468 st->tx_packets = tswap32(st->tx_packets);
2469 st->rx_bytes = tswap32(st->rx_bytes);
2470 st->tx_bytes = tswap32(st->tx_bytes);
2471 st->rx_errors = tswap32(st->rx_errors);
2472 st->tx_errors = tswap32(st->tx_errors);
2473 st->rx_dropped = tswap32(st->rx_dropped);
2474 st->tx_dropped = tswap32(st->tx_dropped);
2475 st->multicast = tswap32(st->multicast);
2476 st->collisions = tswap32(st->collisions);
2477
2478 /* detailed rx_errors: */
2479 st->rx_length_errors = tswap32(st->rx_length_errors);
2480 st->rx_over_errors = tswap32(st->rx_over_errors);
2481 st->rx_crc_errors = tswap32(st->rx_crc_errors);
2482 st->rx_frame_errors = tswap32(st->rx_frame_errors);
2483 st->rx_fifo_errors = tswap32(st->rx_fifo_errors);
2484 st->rx_missed_errors = tswap32(st->rx_missed_errors);
2485
2486 /* detailed tx_errors */
2487 st->tx_aborted_errors = tswap32(st->tx_aborted_errors);
2488 st->tx_carrier_errors = tswap32(st->tx_carrier_errors);
2489 st->tx_fifo_errors = tswap32(st->tx_fifo_errors);
2490 st->tx_heartbeat_errors = tswap32(st->tx_heartbeat_errors);
2491 st->tx_window_errors = tswap32(st->tx_window_errors);
2492
2493 /* for cslip etc */
2494 st->rx_compressed = tswap32(st->rx_compressed);
2495 st->tx_compressed = tswap32(st->tx_compressed);
2496 break;
2497 /* struct rtnl_link_stats64 */
60c6b790 2498 case QEMU_IFLA_STATS64:
6c5b5645
LV
2499 st64 = RTA_DATA(rtattr);
2500 st64->rx_packets = tswap64(st64->rx_packets);
2501 st64->tx_packets = tswap64(st64->tx_packets);
2502 st64->rx_bytes = tswap64(st64->rx_bytes);
2503 st64->tx_bytes = tswap64(st64->tx_bytes);
2504 st64->rx_errors = tswap64(st64->rx_errors);
2505 st64->tx_errors = tswap64(st64->tx_errors);
2506 st64->rx_dropped = tswap64(st64->rx_dropped);
2507 st64->tx_dropped = tswap64(st64->tx_dropped);
2508 st64->multicast = tswap64(st64->multicast);
2509 st64->collisions = tswap64(st64->collisions);
2510
2511 /* detailed rx_errors: */
2512 st64->rx_length_errors = tswap64(st64->rx_length_errors);
2513 st64->rx_over_errors = tswap64(st64->rx_over_errors);
2514 st64->rx_crc_errors = tswap64(st64->rx_crc_errors);
2515 st64->rx_frame_errors = tswap64(st64->rx_frame_errors);
2516 st64->rx_fifo_errors = tswap64(st64->rx_fifo_errors);
2517 st64->rx_missed_errors = tswap64(st64->rx_missed_errors);
2518
2519 /* detailed tx_errors */
2520 st64->tx_aborted_errors = tswap64(st64->tx_aborted_errors);
2521 st64->tx_carrier_errors = tswap64(st64->tx_carrier_errors);
2522 st64->tx_fifo_errors = tswap64(st64->tx_fifo_errors);
2523 st64->tx_heartbeat_errors = tswap64(st64->tx_heartbeat_errors);
2524 st64->tx_window_errors = tswap64(st64->tx_window_errors);
2525
2526 /* for cslip etc */
2527 st64->rx_compressed = tswap64(st64->rx_compressed);
2528 st64->tx_compressed = tswap64(st64->tx_compressed);
2529 break;
2530 /* struct rtnl_link_ifmap */
60c6b790 2531 case QEMU_IFLA_MAP:
6c5b5645
LV
2532 map = RTA_DATA(rtattr);
2533 map->mem_start = tswap64(map->mem_start);
2534 map->mem_end = tswap64(map->mem_end);
2535 map->base_addr = tswap64(map->base_addr);
2536 map->irq = tswap16(map->irq);
2537 break;
2538 /* nested */
60c6b790 2539 case QEMU_IFLA_LINKINFO:
c5dff280
LV
2540 memset(&li_context, 0, sizeof(li_context));
2541 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
2542 &li_context,
2543 host_to_target_data_linkinfo_nlattr);
60c6b790 2544 case QEMU_IFLA_AF_SPEC:
c5dff280
LV
2545 return host_to_target_for_each_nlattr(RTA_DATA(rtattr), rtattr->rta_len,
2546 NULL,
2547 host_to_target_data_spec_nlattr);
6c5b5645 2548 default:
60c6b790 2549 gemu_log("Unknown host QEMU_IFLA type: %d\n", rtattr->rta_type);
6c5b5645
LV
2550 break;
2551 }
2552 return 0;
2553}
2554
2555static abi_long host_to_target_data_addr_rtattr(struct rtattr *rtattr)
2556{
2557 uint32_t *u32;
2558 struct ifa_cacheinfo *ci;
2559
2560 switch (rtattr->rta_type) {
2561 /* binary: depends on family type */
2562 case IFA_ADDRESS:
2563 case IFA_LOCAL:
2564 break;
2565 /* string */
2566 case IFA_LABEL:
2567 break;
2568 /* u32 */
2569 case IFA_FLAGS:
2570 case IFA_BROADCAST:
2571 u32 = RTA_DATA(rtattr);
2572 *u32 = tswap32(*u32);
2573 break;
2574 /* struct ifa_cacheinfo */
2575 case IFA_CACHEINFO:
2576 ci = RTA_DATA(rtattr);
2577 ci->ifa_prefered = tswap32(ci->ifa_prefered);
2578 ci->ifa_valid = tswap32(ci->ifa_valid);
2579 ci->cstamp = tswap32(ci->cstamp);
2580 ci->tstamp = tswap32(ci->tstamp);
2581 break;
2582 default:
2583 gemu_log("Unknown host IFA type: %d\n", rtattr->rta_type);
2584 break;
2585 }
2586 return 0;
2587}
2588
2589static abi_long host_to_target_data_route_rtattr(struct rtattr *rtattr)
2590{
2591 uint32_t *u32;
2592 switch (rtattr->rta_type) {
2593 /* binary: depends on family type */
2594 case RTA_GATEWAY:
2595 case RTA_DST:
2596 case RTA_PREFSRC:
2597 break;
2598 /* u32 */
2599 case RTA_PRIORITY:
2600 case RTA_TABLE:
2601 case RTA_OIF:
2602 u32 = RTA_DATA(rtattr);
2603 *u32 = tswap32(*u32);
2604 break;
2605 default:
2606 gemu_log("Unknown host RTA type: %d\n", rtattr->rta_type);
2607 break;
2608 }
2609 return 0;
2610}
2611
2612static abi_long host_to_target_link_rtattr(struct rtattr *rtattr,
2613 uint32_t rtattr_len)
2614{
2615 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
2616 host_to_target_data_link_rtattr);
2617}
2618
2619static abi_long host_to_target_addr_rtattr(struct rtattr *rtattr,
2620 uint32_t rtattr_len)
2621{
2622 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
2623 host_to_target_data_addr_rtattr);
2624}
2625
2626static abi_long host_to_target_route_rtattr(struct rtattr *rtattr,
2627 uint32_t rtattr_len)
2628{
2629 return host_to_target_for_each_rtattr(rtattr, rtattr_len,
2630 host_to_target_data_route_rtattr);
2631}
2632
2633static abi_long host_to_target_data_route(struct nlmsghdr *nlh)
2634{
2635 uint32_t nlmsg_len;
2636 struct ifinfomsg *ifi;
2637 struct ifaddrmsg *ifa;
2638 struct rtmsg *rtm;
2639
2640 nlmsg_len = nlh->nlmsg_len;
2641 switch (nlh->nlmsg_type) {
2642 case RTM_NEWLINK:
2643 case RTM_DELLINK:
2644 case RTM_GETLINK:
b9403979
LV
2645 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) {
2646 ifi = NLMSG_DATA(nlh);
2647 ifi->ifi_type = tswap16(ifi->ifi_type);
2648 ifi->ifi_index = tswap32(ifi->ifi_index);
2649 ifi->ifi_flags = tswap32(ifi->ifi_flags);
2650 ifi->ifi_change = tswap32(ifi->ifi_change);
2651 host_to_target_link_rtattr(IFLA_RTA(ifi),
2652 nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
2653 }
6c5b5645
LV
2654 break;
2655 case RTM_NEWADDR:
2656 case RTM_DELADDR:
2657 case RTM_GETADDR:
b9403979
LV
2658 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) {
2659 ifa = NLMSG_DATA(nlh);
2660 ifa->ifa_index = tswap32(ifa->ifa_index);
2661 host_to_target_addr_rtattr(IFA_RTA(ifa),
2662 nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
2663 }
6c5b5645
LV
2664 break;
2665 case RTM_NEWROUTE:
2666 case RTM_DELROUTE:
2667 case RTM_GETROUTE:
b9403979
LV
2668 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) {
2669 rtm = NLMSG_DATA(nlh);
2670 rtm->rtm_flags = tswap32(rtm->rtm_flags);
2671 host_to_target_route_rtattr(RTM_RTA(rtm),
2672 nlmsg_len - NLMSG_LENGTH(sizeof(*rtm)));
2673 }
6c5b5645
LV
2674 break;
2675 default:
2676 return -TARGET_EINVAL;
2677 }
2678 return 0;
2679}
2680
2681static inline abi_long host_to_target_nlmsg_route(struct nlmsghdr *nlh,
2682 size_t len)
2683{
2684 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_route);
2685}
2686
2687static abi_long target_to_host_for_each_rtattr(struct rtattr *rtattr,
2688 size_t len,
2689 abi_long (*target_to_host_rtattr)
2690 (struct rtattr *))
2691{
2692 abi_long ret;
2693
2694 while (len >= sizeof(struct rtattr)) {
2695 if (tswap16(rtattr->rta_len) < sizeof(struct rtattr) ||
2696 tswap16(rtattr->rta_len) > len) {
2697 break;
2698 }
2699 rtattr->rta_len = tswap16(rtattr->rta_len);
2700 rtattr->rta_type = tswap16(rtattr->rta_type);
2701 ret = target_to_host_rtattr(rtattr);
2702 if (ret < 0) {
2703 return ret;
2704 }
2705 len -= RTA_ALIGN(rtattr->rta_len);
2706 rtattr = (struct rtattr *)(((char *)rtattr) +
2707 RTA_ALIGN(rtattr->rta_len));
2708 }
2709 return 0;
2710}
2711
2712static abi_long target_to_host_data_link_rtattr(struct rtattr *rtattr)
2713{
2714 switch (rtattr->rta_type) {
2715 default:
60c6b790 2716 gemu_log("Unknown target QEMU_IFLA type: %d\n", rtattr->rta_type);
6c5b5645
LV
2717 break;
2718 }
2719 return 0;
2720}
2721
2722static abi_long target_to_host_data_addr_rtattr(struct rtattr *rtattr)
2723{
2724 switch (rtattr->rta_type) {
2725 /* binary: depends on family type */
2726 case IFA_LOCAL:
2727 case IFA_ADDRESS:
2728 break;
2729 default:
2730 gemu_log("Unknown target IFA type: %d\n", rtattr->rta_type);
2731 break;
2732 }
2733 return 0;
2734}
2735
2736static abi_long target_to_host_data_route_rtattr(struct rtattr *rtattr)
2737{
2738 uint32_t *u32;
2739 switch (rtattr->rta_type) {
2740 /* binary: depends on family type */
2741 case RTA_DST:
2742 case RTA_SRC:
2743 case RTA_GATEWAY:
2744 break;
2745 /* u32 */
434f286b 2746 case RTA_PRIORITY:
6c5b5645
LV
2747 case RTA_OIF:
2748 u32 = RTA_DATA(rtattr);
2749 *u32 = tswap32(*u32);
2750 break;
2751 default:
2752 gemu_log("Unknown target RTA type: %d\n", rtattr->rta_type);
2753 break;
2754 }
2755 return 0;
2756}
2757
2758static void target_to_host_link_rtattr(struct rtattr *rtattr,
2759 uint32_t rtattr_len)
2760{
2761 target_to_host_for_each_rtattr(rtattr, rtattr_len,
2762 target_to_host_data_link_rtattr);
2763}
2764
2765static void target_to_host_addr_rtattr(struct rtattr *rtattr,
2766 uint32_t rtattr_len)
2767{
2768 target_to_host_for_each_rtattr(rtattr, rtattr_len,
2769 target_to_host_data_addr_rtattr);
2770}
2771
2772static void target_to_host_route_rtattr(struct rtattr *rtattr,
2773 uint32_t rtattr_len)
2774{
2775 target_to_host_for_each_rtattr(rtattr, rtattr_len,
2776 target_to_host_data_route_rtattr);
2777}
2778
2779static abi_long target_to_host_data_route(struct nlmsghdr *nlh)
2780{
2781 struct ifinfomsg *ifi;
2782 struct ifaddrmsg *ifa;
2783 struct rtmsg *rtm;
2784
2785 switch (nlh->nlmsg_type) {
2786 case RTM_GETLINK:
2787 break;
2788 case RTM_NEWLINK:
2789 case RTM_DELLINK:
b9403979
LV
2790 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifi))) {
2791 ifi = NLMSG_DATA(nlh);
2792 ifi->ifi_type = tswap16(ifi->ifi_type);
2793 ifi->ifi_index = tswap32(ifi->ifi_index);
2794 ifi->ifi_flags = tswap32(ifi->ifi_flags);
2795 ifi->ifi_change = tswap32(ifi->ifi_change);
2796 target_to_host_link_rtattr(IFLA_RTA(ifi), nlh->nlmsg_len -
2797 NLMSG_LENGTH(sizeof(*ifi)));
2798 }
6c5b5645
LV
2799 break;
2800 case RTM_GETADDR:
2801 case RTM_NEWADDR:
2802 case RTM_DELADDR:
b9403979
LV
2803 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*ifa))) {
2804 ifa = NLMSG_DATA(nlh);
2805 ifa->ifa_index = tswap32(ifa->ifa_index);
2806 target_to_host_addr_rtattr(IFA_RTA(ifa), nlh->nlmsg_len -
2807 NLMSG_LENGTH(sizeof(*ifa)));
2808 }
6c5b5645
LV
2809 break;
2810 case RTM_GETROUTE:
2811 break;
2812 case RTM_NEWROUTE:
2813 case RTM_DELROUTE:
b9403979
LV
2814 if (nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(*rtm))) {
2815 rtm = NLMSG_DATA(nlh);
2816 rtm->rtm_flags = tswap32(rtm->rtm_flags);
2817 target_to_host_route_rtattr(RTM_RTA(rtm), nlh->nlmsg_len -
2818 NLMSG_LENGTH(sizeof(*rtm)));
2819 }
6c5b5645
LV
2820 break;
2821 default:
2822 return -TARGET_EOPNOTSUPP;
2823 }
2824 return 0;
2825}
2826
2827static abi_long target_to_host_nlmsg_route(struct nlmsghdr *nlh, size_t len)
2828{
2829 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_route);
2830}
575b22b1 2831#endif /* CONFIG_RTNETLINK */
6c5b5645 2832
5ce9bb59
LV
2833static abi_long host_to_target_data_audit(struct nlmsghdr *nlh)
2834{
2835 switch (nlh->nlmsg_type) {
2836 default:
2837 gemu_log("Unknown host audit message type %d\n",
2838 nlh->nlmsg_type);
2839 return -TARGET_EINVAL;
2840 }
2841 return 0;
2842}
2843
2844static inline abi_long host_to_target_nlmsg_audit(struct nlmsghdr *nlh,
2845 size_t len)
2846{
2847 return host_to_target_for_each_nlmsg(nlh, len, host_to_target_data_audit);
2848}
2849
2850static abi_long target_to_host_data_audit(struct nlmsghdr *nlh)
2851{
2852 switch (nlh->nlmsg_type) {
2853 case AUDIT_USER:
2854 case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
2855 case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
2856 break;
2857 default:
2858 gemu_log("Unknown target audit message type %d\n",
2859 nlh->nlmsg_type);
2860 return -TARGET_EINVAL;
2861 }
2862
2863 return 0;
2864}
2865
2866static abi_long target_to_host_nlmsg_audit(struct nlmsghdr *nlh, size_t len)
2867{
2868 return target_to_host_for_each_nlmsg(nlh, len, target_to_host_data_audit);
2869}
2870
0da46a6e 2871/* do_setsockopt() Must return target values and target errnos. */
992f48a0 2872static abi_long do_setsockopt(int sockfd, int level, int optname,
2f619698 2873 abi_ulong optval_addr, socklen_t optlen)
7854b056 2874{
992f48a0 2875 abi_long ret;
32407103 2876 int val;
b975b83b 2877 struct ip_mreqn *ip_mreq;
6e3cb58f 2878 struct ip_mreq_source *ip_mreq_source;
3b46e624 2879
8853f86e
FB
2880 switch(level) {
2881 case SOL_TCP:
7854b056 2882 /* TCP options all take an 'int' value. */
7854b056 2883 if (optlen < sizeof(uint32_t))
0da46a6e 2884 return -TARGET_EINVAL;
3b46e624 2885
2f619698
FB
2886 if (get_user_u32(val, optval_addr))
2887 return -TARGET_EFAULT;
8853f86e
FB
2888 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2889 break;
2890 case SOL_IP:
2891 switch(optname) {
2efbe911
FB
2892 case IP_TOS:
2893 case IP_TTL:
8853f86e 2894 case IP_HDRINCL:
2efbe911
FB
2895 case IP_ROUTER_ALERT:
2896 case IP_RECVOPTS:
2897 case IP_RETOPTS:
2898 case IP_PKTINFO:
2899 case IP_MTU_DISCOVER:
2900 case IP_RECVERR:
ee1ac3a1 2901 case IP_RECVTTL:
2efbe911
FB
2902 case IP_RECVTOS:
2903#ifdef IP_FREEBIND
2904 case IP_FREEBIND:
2905#endif
2906 case IP_MULTICAST_TTL:
2907 case IP_MULTICAST_LOOP:
8853f86e
FB
2908 val = 0;
2909 if (optlen >= sizeof(uint32_t)) {
2f619698
FB
2910 if (get_user_u32(val, optval_addr))
2911 return -TARGET_EFAULT;
8853f86e 2912 } else if (optlen >= 1) {
2f619698
FB
2913 if (get_user_u8(val, optval_addr))
2914 return -TARGET_EFAULT;
8853f86e
FB
2915 }
2916 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
2917 break;
b975b83b
LL
2918 case IP_ADD_MEMBERSHIP:
2919 case IP_DROP_MEMBERSHIP:
2920 if (optlen < sizeof (struct target_ip_mreq) ||
2921 optlen > sizeof (struct target_ip_mreqn))
2922 return -TARGET_EINVAL;
2923
2924 ip_mreq = (struct ip_mreqn *) alloca(optlen);
2925 target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
2926 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
2927 break;
2928
6e3cb58f
LL
2929 case IP_BLOCK_SOURCE:
2930 case IP_UNBLOCK_SOURCE:
2931 case IP_ADD_SOURCE_MEMBERSHIP:
2932 case IP_DROP_SOURCE_MEMBERSHIP:
2933 if (optlen != sizeof (struct target_ip_mreq_source))
2934 return -TARGET_EINVAL;
2935
2936 ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
2937 ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
2938 unlock_user (ip_mreq_source, optval_addr, 0);
2939 break;
2940
920394db
JH
2941 default:
2942 goto unimplemented;
2943 }
2944 break;
0d78b3b5
LV
2945 case SOL_IPV6:
2946 switch (optname) {
2947 case IPV6_MTU_DISCOVER:
2948 case IPV6_MTU:
2949 case IPV6_V6ONLY:
2950 case IPV6_RECVPKTINFO:
ee1ac3a1
HD
2951 case IPV6_UNICAST_HOPS:
2952 case IPV6_RECVERR:
2953 case IPV6_RECVHOPLIMIT:
2954 case IPV6_2292HOPLIMIT:
2955 case IPV6_CHECKSUM:
0d78b3b5
LV
2956 val = 0;
2957 if (optlen < sizeof(uint32_t)) {
2958 return -TARGET_EINVAL;
2959 }
2960 if (get_user_u32(val, optval_addr)) {
2961 return -TARGET_EFAULT;
2962 }
2963 ret = get_errno(setsockopt(sockfd, level, optname,
2964 &val, sizeof(val)));
2965 break;
ee1ac3a1
HD
2966 case IPV6_PKTINFO:
2967 {
2968 struct in6_pktinfo pki;
2969
2970 if (optlen < sizeof(pki)) {
2971 return -TARGET_EINVAL;
2972 }
2973
2974 if (copy_from_user(&pki, optval_addr, sizeof(pki))) {
2975 return -TARGET_EFAULT;
2976 }
2977
2978 pki.ipi6_ifindex = tswap32(pki.ipi6_ifindex);
2979
2980 ret = get_errno(setsockopt(sockfd, level, optname,
2981 &pki, sizeof(pki)));
2982 break;
2983 }
2984 default:
2985 goto unimplemented;
2986 }
2987 break;
2988 case SOL_ICMPV6:
2989 switch (optname) {
2990 case ICMPV6_FILTER:
2991 {
2992 struct icmp6_filter icmp6f;
2993
2994 if (optlen > sizeof(icmp6f)) {
2995 optlen = sizeof(icmp6f);
2996 }
2997
2998 if (copy_from_user(&icmp6f, optval_addr, optlen)) {
2999 return -TARGET_EFAULT;
3000 }
3001
3002 for (val = 0; val < 8; val++) {
3003 icmp6f.data[val] = tswap32(icmp6f.data[val]);
3004 }
3005
3006 ret = get_errno(setsockopt(sockfd, level, optname,
3007 &icmp6f, optlen));
3008 break;
3009 }
0d78b3b5
LV
3010 default:
3011 goto unimplemented;
3012 }
3013 break;
920394db
JH
3014 case SOL_RAW:
3015 switch (optname) {
3016 case ICMP_FILTER:
ee1ac3a1
HD
3017 case IPV6_CHECKSUM:
3018 /* those take an u32 value */
920394db
JH
3019 if (optlen < sizeof(uint32_t)) {
3020 return -TARGET_EINVAL;
3021 }
3022
3023 if (get_user_u32(val, optval_addr)) {
3024 return -TARGET_EFAULT;
3025 }
3026 ret = get_errno(setsockopt(sockfd, level, optname,
3027 &val, sizeof(val)));
3028 break;
3029
8853f86e
FB
3030 default:
3031 goto unimplemented;
3032 }
3033 break;
3532fa74 3034 case TARGET_SOL_SOCKET:
8853f86e 3035 switch (optname) {
1b09aeb9
LV
3036 case TARGET_SO_RCVTIMEO:
3037 {
3038 struct timeval tv;
3039
3040 optname = SO_RCVTIMEO;
3041
3042set_timeout:
3043 if (optlen != sizeof(struct target_timeval)) {
3044 return -TARGET_EINVAL;
3045 }
3046
3047 if (copy_from_user_timeval(&tv, optval_addr)) {
3048 return -TARGET_EFAULT;
3049 }
3050
3051 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
3052 &tv, sizeof(tv)));
3053 return ret;
3054 }
3055 case TARGET_SO_SNDTIMEO:
3056 optname = SO_SNDTIMEO;
3057 goto set_timeout;
f57d4192
LV
3058 case TARGET_SO_ATTACH_FILTER:
3059 {
3060 struct target_sock_fprog *tfprog;
3061 struct target_sock_filter *tfilter;
3062 struct sock_fprog fprog;
3063 struct sock_filter *filter;
3064 int i;
3065
3066 if (optlen != sizeof(*tfprog)) {
3067 return -TARGET_EINVAL;
3068 }
3069 if (!lock_user_struct(VERIFY_READ, tfprog, optval_addr, 0)) {
3070 return -TARGET_EFAULT;
3071 }
3072 if (!lock_user_struct(VERIFY_READ, tfilter,
3073 tswapal(tfprog->filter), 0)) {
3074 unlock_user_struct(tfprog, optval_addr, 1);
3075 return -TARGET_EFAULT;
3076 }
3077
3078 fprog.len = tswap16(tfprog->len);
0e173b24 3079 filter = g_try_new(struct sock_filter, fprog.len);
f57d4192
LV
3080 if (filter == NULL) {
3081 unlock_user_struct(tfilter, tfprog->filter, 1);
3082 unlock_user_struct(tfprog, optval_addr, 1);
3083 return -TARGET_ENOMEM;
3084 }
3085 for (i = 0; i < fprog.len; i++) {
3086 filter[i].code = tswap16(tfilter[i].code);
3087 filter[i].jt = tfilter[i].jt;
3088 filter[i].jf = tfilter[i].jf;
3089 filter[i].k = tswap32(tfilter[i].k);
3090 }
3091 fprog.filter = filter;
3092
3093 ret = get_errno(setsockopt(sockfd, SOL_SOCKET,
3094 SO_ATTACH_FILTER, &fprog, sizeof(fprog)));
0e173b24 3095 g_free(filter);
f57d4192
LV
3096
3097 unlock_user_struct(tfilter, tfprog->filter, 1);
3098 unlock_user_struct(tfprog, optval_addr, 1);
3099 return ret;
3100 }
451aaf68
JT
3101 case TARGET_SO_BINDTODEVICE:
3102 {
3103 char *dev_ifname, *addr_ifname;
3104
3105 if (optlen > IFNAMSIZ - 1) {
3106 optlen = IFNAMSIZ - 1;
3107 }
3108 dev_ifname = lock_user(VERIFY_READ, optval_addr, optlen, 1);
3109 if (!dev_ifname) {
3110 return -TARGET_EFAULT;
3111 }
3112 optname = SO_BINDTODEVICE;
3113 addr_ifname = alloca(IFNAMSIZ);
3114 memcpy(addr_ifname, dev_ifname, optlen);
3115 addr_ifname[optlen] = 0;
fad6c58a
CG
3116 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname,
3117 addr_ifname, optlen));
451aaf68
JT
3118 unlock_user (dev_ifname, optval_addr, 0);
3119 return ret;
3120 }
8853f86e 3121 /* Options with 'int' argument. */
3532fa74
FB
3122 case TARGET_SO_DEBUG:
3123 optname = SO_DEBUG;
3124 break;
3125 case TARGET_SO_REUSEADDR:
3126 optname = SO_REUSEADDR;
3127 break;
3128 case TARGET_SO_TYPE:
3129 optname = SO_TYPE;
3130 break;
3131 case TARGET_SO_ERROR:
3132 optname = SO_ERROR;
3133 break;
3134 case TARGET_SO_DONTROUTE:
3135 optname = SO_DONTROUTE;
3136 break;
3137 case TARGET_SO_BROADCAST:
3138 optname = SO_BROADCAST;
3139 break;
3140 case TARGET_SO_SNDBUF:
3141 optname = SO_SNDBUF;
3142 break;
d79b6cc4
PB
3143 case TARGET_SO_SNDBUFFORCE:
3144 optname = SO_SNDBUFFORCE;
3145 break;
3532fa74
FB
3146 case TARGET_SO_RCVBUF:
3147 optname = SO_RCVBUF;
3148 break;
d79b6cc4
PB
3149 case TARGET_SO_RCVBUFFORCE:
3150 optname = SO_RCVBUFFORCE;
3151 break;
3532fa74
FB
3152 case TARGET_SO_KEEPALIVE:
3153 optname = SO_KEEPALIVE;
3154 break;
3155 case TARGET_SO_OOBINLINE:
3156 optname = SO_OOBINLINE;
3157 break;
3158 case TARGET_SO_NO_CHECK:
3159 optname = SO_NO_CHECK;
3160 break;
3161 case TARGET_SO_PRIORITY:
3162 optname = SO_PRIORITY;
3163 break;
5e83e8e3 3164#ifdef SO_BSDCOMPAT
3532fa74
FB
3165 case TARGET_SO_BSDCOMPAT:
3166 optname = SO_BSDCOMPAT;
3167 break;
5e83e8e3 3168#endif
3532fa74
FB
3169 case TARGET_SO_PASSCRED:
3170 optname = SO_PASSCRED;
3171 break;
82d0fe6b
PB
3172 case TARGET_SO_PASSSEC:
3173 optname = SO_PASSSEC;
3174 break;
3532fa74
FB
3175 case TARGET_SO_TIMESTAMP:
3176 optname = SO_TIMESTAMP;
3177 break;
3178 case TARGET_SO_RCVLOWAT:
3179 optname = SO_RCVLOWAT;
3180 break;
8853f86e
FB
3181 default:
3182 goto unimplemented;
3183 }
3532fa74 3184 if (optlen < sizeof(uint32_t))
2f619698 3185 return -TARGET_EINVAL;
3532fa74 3186
2f619698
FB
3187 if (get_user_u32(val, optval_addr))
3188 return -TARGET_EFAULT;
3532fa74 3189 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
8853f86e 3190 break;
7854b056 3191 default:
8853f86e 3192 unimplemented:
b2bedb21 3193 gemu_log("Unsupported setsockopt level=%d optname=%d\n", level, optname);
6fa13c17 3194 ret = -TARGET_ENOPROTOOPT;
7854b056 3195 }
8853f86e 3196 return ret;
7854b056
FB
3197}
3198
0da46a6e 3199/* do_getsockopt() Must return target values and target errnos. */
992f48a0 3200static abi_long do_getsockopt(int sockfd, int level, int optname,
2f619698 3201 abi_ulong optval_addr, abi_ulong optlen)
7854b056 3202{
992f48a0 3203 abi_long ret;
b55266b5
BS
3204 int len, val;
3205 socklen_t lv;
8853f86e
FB
3206
3207 switch(level) {
3532fa74 3208 case TARGET_SOL_SOCKET:
f3b974cd
JL
3209 level = SOL_SOCKET;
3210 switch (optname) {
3211 /* These don't just return a single integer */
3212 case TARGET_SO_LINGER:
3213 case TARGET_SO_RCVTIMEO:
3214 case TARGET_SO_SNDTIMEO:
f3b974cd
JL
3215 case TARGET_SO_PEERNAME:
3216 goto unimplemented;
583359a6
AP
3217 case TARGET_SO_PEERCRED: {
3218 struct ucred cr;
3219 socklen_t crlen;
3220 struct target_ucred *tcr;
3221
3222 if (get_user_u32(len, optlen)) {
3223 return -TARGET_EFAULT;
3224 }
3225 if (len < 0) {
3226 return -TARGET_EINVAL;
3227 }
3228
3229 crlen = sizeof(cr);
3230 ret = get_errno(getsockopt(sockfd, level, SO_PEERCRED,
3231 &cr, &crlen));
3232 if (ret < 0) {
3233 return ret;
3234 }
3235 if (len > crlen) {
3236 len = crlen;
3237 }
3238 if (!lock_user_struct(VERIFY_WRITE, tcr, optval_addr, 0)) {
3239 return -TARGET_EFAULT;
3240 }
3241 __put_user(cr.pid, &tcr->pid);
3242 __put_user(cr.uid, &tcr->uid);
3243 __put_user(cr.gid, &tcr->gid);
3244 unlock_user_struct(tcr, optval_addr, 1);
3245 if (put_user_u32(len, optlen)) {
3246 return -TARGET_EFAULT;
3247 }
3248 break;
3249 }
f3b974cd
JL
3250 /* Options with 'int' argument. */
3251 case TARGET_SO_DEBUG:
3252 optname = SO_DEBUG;
3253 goto int_case;
3254 case TARGET_SO_REUSEADDR:
3255 optname = SO_REUSEADDR;
3256 goto int_case;
3257 case TARGET_SO_TYPE:
3258 optname = SO_TYPE;
3259 goto int_case;
3260 case TARGET_SO_ERROR:
3261 optname = SO_ERROR;
3262 goto int_case;
3263 case TARGET_SO_DONTROUTE:
3264 optname = SO_DONTROUTE;
3265 goto int_case;
3266 case TARGET_SO_BROADCAST:
3267 optname = SO_BROADCAST;
3268 goto int_case;
3269 case TARGET_SO_SNDBUF:
3270 optname = SO_SNDBUF;
3271 goto int_case;
3272 case TARGET_SO_RCVBUF:
3273 optname = SO_RCVBUF;
3274 goto int_case;
3275 case TARGET_SO_KEEPALIVE:
3276 optname = SO_KEEPALIVE;
3277 goto int_case;
3278 case TARGET_SO_OOBINLINE:
3279 optname = SO_OOBINLINE;
3280 goto int_case;
3281 case TARGET_SO_NO_CHECK:
3282 optname = SO_NO_CHECK;
3283 goto int_case;
3284 case TARGET_SO_PRIORITY:
3285 optname = SO_PRIORITY;
3286 goto int_case;
3287#ifdef SO_BSDCOMPAT
3288 case TARGET_SO_BSDCOMPAT:
3289 optname = SO_BSDCOMPAT;
3290 goto int_case;
3291#endif
3292 case TARGET_SO_PASSCRED:
3293 optname = SO_PASSCRED;
3294 goto int_case;
3295 case TARGET_SO_TIMESTAMP:
3296 optname = SO_TIMESTAMP;
3297 goto int_case;
3298 case TARGET_SO_RCVLOWAT:
3299 optname = SO_RCVLOWAT;
3300 goto int_case;
aec1ca41
PB
3301 case TARGET_SO_ACCEPTCONN:
3302 optname = SO_ACCEPTCONN;
3303 goto int_case;
8853f86e 3304 default:
2efbe911
FB
3305 goto int_case;
3306 }
3307 break;
3308 case SOL_TCP:
3309 /* TCP options all take an 'int' value. */
3310 int_case:
2f619698
FB
3311 if (get_user_u32(len, optlen))
3312 return -TARGET_EFAULT;
2efbe911 3313 if (len < 0)
0da46a6e 3314 return -TARGET_EINVAL;
73160d95 3315 lv = sizeof(lv);
2efbe911
FB
3316 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
3317 if (ret < 0)
3318 return ret;
8289d112
PB
3319 if (optname == SO_TYPE) {
3320 val = host_to_target_sock_type(val);
3321 }
2efbe911
FB
3322 if (len > lv)
3323 len = lv;
2f619698
FB
3324 if (len == 4) {
3325 if (put_user_u32(val, optval_addr))
3326 return -TARGET_EFAULT;
3327 } else {
3328 if (put_user_u8(val, optval_addr))
3329 return -TARGET_EFAULT;
f3b974cd 3330 }
2f619698
FB
3331 if (put_user_u32(len, optlen))
3332 return -TARGET_EFAULT;
2efbe911
FB
3333 break;
3334 case SOL_IP:
3335 switch(optname) {
3336 case IP_TOS:
3337 case IP_TTL:
3338 case IP_HDRINCL:
3339 case IP_ROUTER_ALERT:
3340 case IP_RECVOPTS:
3341 case IP_RETOPTS:
3342 case IP_PKTINFO:
3343 case IP_MTU_DISCOVER:
3344 case IP_RECVERR:
3345 case IP_RECVTOS:
3346#ifdef IP_FREEBIND
3347 case IP_FREEBIND:
3348#endif
3349 case IP_MULTICAST_TTL:
3350 case IP_MULTICAST_LOOP:
2f619698
FB
3351 if (get_user_u32(len, optlen))
3352 return -TARGET_EFAULT;
8853f86e 3353 if (len < 0)
0da46a6e 3354 return -TARGET_EINVAL;
73160d95 3355 lv = sizeof(lv);
8853f86e
FB
3356 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
3357 if (ret < 0)
3358 return ret;
2efbe911 3359 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
2efbe911 3360 len = 1;
2f619698
FB
3361 if (put_user_u32(len, optlen)
3362 || put_user_u8(val, optval_addr))
3363 return -TARGET_EFAULT;
2efbe911 3364 } else {
2efbe911
FB
3365 if (len > sizeof(int))
3366 len = sizeof(int);
2f619698
FB
3367 if (put_user_u32(len, optlen)
3368 || put_user_u32(val, optval_addr))
3369 return -TARGET_EFAULT;
2efbe911 3370 }
8853f86e 3371 break;
2efbe911 3372 default:
c02f499e
TS
3373 ret = -TARGET_ENOPROTOOPT;
3374 break;
8853f86e
FB
3375 }
3376 break;
3377 default:
3378 unimplemented:
3379 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
3380 level, optname);
c02f499e 3381 ret = -TARGET_EOPNOTSUPP;
8853f86e
FB
3382 break;
3383 }
3384 return ret;
7854b056
FB
3385}
3386
f287b2c2 3387static struct iovec *lock_iovec(int type, abi_ulong target_addr,
dab32b32 3388 abi_ulong count, int copy)
53a5960a
PB
3389{
3390 struct target_iovec *target_vec;
f287b2c2
RH
3391 struct iovec *vec;
3392 abi_ulong total_len, max_len;
d732dcb4 3393 int i;
501bb4b0 3394 int err = 0;
29560a6c 3395 bool bad_address = false;
53a5960a 3396
f287b2c2
RH
3397 if (count == 0) {
3398 errno = 0;
3399 return NULL;
3400 }
dab32b32 3401 if (count > IOV_MAX) {
f287b2c2
RH
3402 errno = EINVAL;
3403 return NULL;
3404 }
3405
0e173b24 3406 vec = g_try_new0(struct iovec, count);
f287b2c2
RH
3407 if (vec == NULL) {
3408 errno = ENOMEM;
3409 return NULL;
3410 }
3411
3412 target_vec = lock_user(VERIFY_READ, target_addr,
3413 count * sizeof(struct target_iovec), 1);
3414 if (target_vec == NULL) {
501bb4b0 3415 err = EFAULT;
f287b2c2
RH
3416 goto fail2;
3417 }
3418
3419 /* ??? If host page size > target page size, this will result in a
3420 value larger than what we can actually support. */
3421 max_len = 0x7fffffff & TARGET_PAGE_MASK;
3422 total_len = 0;
3423
3424 for (i = 0; i < count; i++) {
3425 abi_ulong base = tswapal(target_vec[i].iov_base);
3426 abi_long len = tswapal(target_vec[i].iov_len);
3427
3428 if (len < 0) {
501bb4b0 3429 err = EINVAL;
f287b2c2
RH
3430 goto fail;
3431 } else if (len == 0) {
3432 /* Zero length pointer is ignored. */
3433 vec[i].iov_base = 0;
41df8411 3434 } else {
f287b2c2 3435 vec[i].iov_base = lock_user(type, base, len, copy);
29560a6c
TM
3436 /* If the first buffer pointer is bad, this is a fault. But
3437 * subsequent bad buffers will result in a partial write; this
3438 * is realized by filling the vector with null pointers and
3439 * zero lengths. */
f287b2c2 3440 if (!vec[i].iov_base) {
29560a6c
TM
3441 if (i == 0) {
3442 err = EFAULT;
3443 goto fail;
3444 } else {
3445 bad_address = true;
3446 }
3447 }
3448 if (bad_address) {
3449 len = 0;
f287b2c2
RH
3450 }
3451 if (len > max_len - total_len) {
3452 len = max_len - total_len;
3453 }
41df8411 3454 }
f287b2c2
RH
3455 vec[i].iov_len = len;
3456 total_len += len;
579a97f7 3457 }
f287b2c2
RH
3458
3459 unlock_user(target_vec, target_addr, 0);
3460 return vec;
3461
3462 fail:
7eff518b
CG
3463 while (--i >= 0) {
3464 if (tswapal(target_vec[i].iov_len) > 0) {
3465 unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
3466 }
3467 }
f287b2c2 3468 unlock_user(target_vec, target_addr, 0);
501bb4b0 3469 fail2:
0e173b24 3470 g_free(vec);
501bb4b0 3471 errno = err;
f287b2c2 3472 return NULL;
53a5960a
PB
3473}
3474
f287b2c2 3475static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
dab32b32 3476 abi_ulong count, int copy)
53a5960a
PB
3477{
3478 struct target_iovec *target_vec;
53a5960a
PB
3479 int i;
3480
f287b2c2
RH
3481 target_vec = lock_user(VERIFY_READ, target_addr,
3482 count * sizeof(struct target_iovec), 1);
3483 if (target_vec) {
3484 for (i = 0; i < count; i++) {
3485 abi_ulong base = tswapal(target_vec[i].iov_base);
71ec7cef 3486 abi_long len = tswapal(target_vec[i].iov_len);
f287b2c2
RH
3487 if (len < 0) {
3488 break;
3489 }
d732dcb4
AZ
3490 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
3491 }
f287b2c2 3492 unlock_user(target_vec, target_addr, 0);
53a5960a 3493 }
579a97f7 3494
0e173b24 3495 g_free(vec);
53a5960a
PB
3496}
3497
53d09b76 3498static inline int target_to_host_sock_type(int *type)
3532fa74 3499{
f651e6ae
PJ
3500 int host_type = 0;
3501 int target_type = *type;
3502
3503 switch (target_type & TARGET_SOCK_TYPE_MASK) {
3532fa74 3504 case TARGET_SOCK_DGRAM:
f651e6ae 3505 host_type = SOCK_DGRAM;
3532fa74
FB
3506 break;
3507 case TARGET_SOCK_STREAM:
f651e6ae 3508 host_type = SOCK_STREAM;
3532fa74 3509 break;
f651e6ae
PJ
3510 default:
3511 host_type = target_type & TARGET_SOCK_TYPE_MASK;
3532fa74
FB
3512 break;
3513 }
f651e6ae 3514 if (target_type & TARGET_SOCK_CLOEXEC) {
53d09b76 3515#if defined(SOCK_CLOEXEC)
f651e6ae 3516 host_type |= SOCK_CLOEXEC;
53d09b76
EI
3517#else
3518 return -TARGET_EINVAL;
3519#endif
f651e6ae
PJ
3520 }
3521 if (target_type & TARGET_SOCK_NONBLOCK) {
53d09b76 3522#if defined(SOCK_NONBLOCK)
f651e6ae 3523 host_type |= SOCK_NONBLOCK;
53d09b76
EI
3524#elif !defined(O_NONBLOCK)
3525 return -TARGET_EINVAL;
3526#endif
f651e6ae
PJ
3527 }
3528 *type = host_type;
53d09b76
EI
3529 return 0;
3530}
3531
3532/* Try to emulate socket type flags after socket creation. */
3533static int sock_flags_fixup(int fd, int target_type)
3534{
3535#if !defined(SOCK_NONBLOCK) && defined(O_NONBLOCK)
3536 if (target_type & TARGET_SOCK_NONBLOCK) {
3537 int flags = fcntl(fd, F_GETFL);
3538 if (fcntl(fd, F_SETFL, O_NONBLOCK | flags) == -1) {
3539 close(fd);
3540 return -TARGET_EINVAL;
3541 }
3542 }
3543#endif
3544 return fd;
f651e6ae
PJ
3545}
3546
0cf22722
LV
3547static abi_long packet_target_to_host_sockaddr(void *host_addr,
3548 abi_ulong target_addr,
3549 socklen_t len)
3550{
3551 struct sockaddr *addr = host_addr;
3552 struct target_sockaddr *target_saddr;
3553
3554 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
3555 if (!target_saddr) {
3556 return -TARGET_EFAULT;
3557 }
3558
3559 memcpy(addr, target_saddr, len);
3560 addr->sa_family = tswap16(target_saddr->sa_family);
3561 /* spkt_protocol is big-endian */
3562
3563 unlock_user(target_saddr, target_addr, 0);
3564 return 0;
3565}
3566
3567static TargetFdTrans target_packet_trans = {
3568 .target_to_host_addr = packet_target_to_host_sockaddr,
3569};
3570
575b22b1 3571#ifdef CONFIG_RTNETLINK
6c5b5645
LV
3572static abi_long netlink_route_target_to_host(void *buf, size_t len)
3573{
ef759f6f
LV
3574 abi_long ret;
3575
3576 ret = target_to_host_nlmsg_route(buf, len);
3577 if (ret < 0) {
3578 return ret;
3579 }
3580
3581 return len;
6c5b5645
LV
3582}
3583
3584static abi_long netlink_route_host_to_target(void *buf, size_t len)
3585{
ef759f6f
LV
3586 abi_long ret;
3587
3588 ret = host_to_target_nlmsg_route(buf, len);
3589 if (ret < 0) {
3590 return ret;
3591 }
3592
3593 return len;
6c5b5645
LV
3594}
3595
3596static TargetFdTrans target_netlink_route_trans = {
3597 .target_to_host_data = netlink_route_target_to_host,
3598 .host_to_target_data = netlink_route_host_to_target,
3599};
575b22b1 3600#endif /* CONFIG_RTNETLINK */
6c5b5645 3601
5ce9bb59
LV
3602static abi_long netlink_audit_target_to_host(void *buf, size_t len)
3603{
ef759f6f
LV
3604 abi_long ret;
3605
3606 ret = target_to_host_nlmsg_audit(buf, len);
3607 if (ret < 0) {
3608 return ret;
3609 }
3610
3611 return len;
5ce9bb59
LV
3612}
3613
3614static abi_long netlink_audit_host_to_target(void *buf, size_t len)
3615{
ef759f6f
LV
3616 abi_long ret;
3617
3618 ret = host_to_target_nlmsg_audit(buf, len);
3619 if (ret < 0) {
3620 return ret;
3621 }
3622
3623 return len;
5ce9bb59
LV
3624}
3625
3626static TargetFdTrans target_netlink_audit_trans = {
3627 .target_to_host_data = netlink_audit_target_to_host,
3628 .host_to_target_data = netlink_audit_host_to_target,
3629};
3630
f651e6ae
PJ
3631/* do_socket() Must return target values and target errnos. */
3632static abi_long do_socket(int domain, int type, int protocol)
3633{
53d09b76
EI
3634 int target_type = type;
3635 int ret;
3636
3637 ret = target_to_host_sock_type(&type);
3638 if (ret) {
3639 return ret;
3640 }
f651e6ae 3641
575b22b1
LV
3642 if (domain == PF_NETLINK && !(
3643#ifdef CONFIG_RTNETLINK
3644 protocol == NETLINK_ROUTE ||
3645#endif
3646 protocol == NETLINK_KOBJECT_UEVENT ||
3647 protocol == NETLINK_AUDIT)) {
6c5b5645
LV
3648 return -EPFNOSUPPORT;
3649 }
ff626f2d
LV
3650
3651 if (domain == AF_PACKET ||
3652 (domain == AF_INET && type == SOCK_PACKET)) {
3653 protocol = tswap16(protocol);
3654 }
3655
53d09b76
EI
3656 ret = get_errno(socket(domain, type, protocol));
3657 if (ret >= 0) {
3658 ret = sock_flags_fixup(ret, target_type);
0cf22722
LV
3659 if (type == SOCK_PACKET) {
3660 /* Manage an obsolete case :
3661 * if socket type is SOCK_PACKET, bind by name
3662 */
3663 fd_trans_register(ret, &target_packet_trans);
6c5b5645
LV
3664 } else if (domain == PF_NETLINK) {
3665 switch (protocol) {
575b22b1 3666#ifdef CONFIG_RTNETLINK
6c5b5645
LV
3667 case NETLINK_ROUTE:
3668 fd_trans_register(ret, &target_netlink_route_trans);
3669 break;
575b22b1 3670#endif
b265620b
LV
3671 case NETLINK_KOBJECT_UEVENT:
3672 /* nothing to do: messages are strings */
3673 break;
5ce9bb59
LV
3674 case NETLINK_AUDIT:
3675 fd_trans_register(ret, &target_netlink_audit_trans);
3676 break;
6c5b5645
LV
3677 default:
3678 g_assert_not_reached();
3679 }
0cf22722 3680 }
53d09b76
EI
3681 }
3682 return ret;
3532fa74
FB
3683}
3684
0da46a6e 3685/* do_bind() Must return target values and target errnos. */
992f48a0
BS
3686static abi_long do_bind(int sockfd, abi_ulong target_addr,
3687 socklen_t addrlen)
3532fa74 3688{
8f7aeaf6 3689 void *addr;
917507b0 3690 abi_long ret;
8f7aeaf6 3691
38724253 3692 if ((int)addrlen < 0) {
8f7aeaf6 3693 return -TARGET_EINVAL;
38724253 3694 }
8f7aeaf6 3695
607175e0 3696 addr = alloca(addrlen+1);
3b46e624 3697
7b36f782 3698 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
917507b0
AP
3699 if (ret)
3700 return ret;
3701
3532fa74
FB
3702 return get_errno(bind(sockfd, addr, addrlen));
3703}
3704
0da46a6e 3705/* do_connect() Must return target values and target errnos. */
992f48a0
BS
3706static abi_long do_connect(int sockfd, abi_ulong target_addr,
3707 socklen_t addrlen)
3532fa74 3708{
8f7aeaf6 3709 void *addr;
917507b0 3710 abi_long ret;
8f7aeaf6 3711
38724253 3712 if ((int)addrlen < 0) {
8f7aeaf6 3713 return -TARGET_EINVAL;
38724253 3714 }
8f7aeaf6 3715
2dd08dfd 3716 addr = alloca(addrlen+1);
3b46e624 3717
7b36f782 3718 ret = target_to_host_sockaddr(sockfd, addr, target_addr, addrlen);
917507b0
AP
3719 if (ret)
3720 return ret;
3721
2a3c7619 3722 return get_errno(safe_connect(sockfd, addr, addrlen));
3532fa74
FB
3723}
3724
f19e00d7
AG
3725/* do_sendrecvmsg_locked() Must return target values and target errnos. */
3726static abi_long do_sendrecvmsg_locked(int fd, struct target_msghdr *msgp,
3727 int flags, int send)
3532fa74 3728{
6de645c7 3729 abi_long ret, len;
3532fa74 3730 struct msghdr msg;
dab32b32 3731 abi_ulong count;
3532fa74 3732 struct iovec *vec;
992f48a0 3733 abi_ulong target_vec;
3532fa74 3734
3532fa74
FB
3735 if (msgp->msg_name) {
3736 msg.msg_namelen = tswap32(msgp->msg_namelen);
2dd08dfd 3737 msg.msg_name = alloca(msg.msg_namelen+1);
7b36f782
LV
3738 ret = target_to_host_sockaddr(fd, msg.msg_name,
3739 tswapal(msgp->msg_name),
3740 msg.msg_namelen);
26a6fc96
PM
3741 if (ret == -TARGET_EFAULT) {
3742 /* For connected sockets msg_name and msg_namelen must
3743 * be ignored, so returning EFAULT immediately is wrong.
3744 * Instead, pass a bad msg_name to the host kernel, and
3745 * let it decide whether to return EFAULT or not.
3746 */
3747 msg.msg_name = (void *)-1;
3748 } else if (ret) {
f287b2c2 3749 goto out2;
917507b0 3750 }
3532fa74
FB
3751 } else {
3752 msg.msg_name = NULL;
3753 msg.msg_namelen = 0;
3754 }
cbb21eed 3755 msg.msg_controllen = 2 * tswapal(msgp->msg_controllen);
3532fa74
FB
3756 msg.msg_control = alloca(msg.msg_controllen);
3757 msg.msg_flags = tswap32(msgp->msg_flags);
3b46e624 3758
cbb21eed 3759 count = tswapal(msgp->msg_iovlen);
cbb21eed 3760 target_vec = tswapal(msgp->msg_iov);
97b07970
PM
3761
3762 if (count > IOV_MAX) {
3763 /* sendrcvmsg returns a different errno for this condition than
3764 * readv/writev, so we must catch it here before lock_iovec() does.
3765 */
3766 ret = -TARGET_EMSGSIZE;
3767 goto out2;
3768 }
3769
f287b2c2
RH
3770 vec = lock_iovec(send ? VERIFY_READ : VERIFY_WRITE,
3771 target_vec, count, send);
3772 if (vec == NULL) {
3773 ret = -host_to_target_errno(errno);
3774 goto out2;
3775 }
3532fa74
FB
3776 msg.msg_iovlen = count;
3777 msg.msg_iov = vec;
3b46e624 3778
3532fa74 3779 if (send) {
6c5b5645 3780 if (fd_trans_target_to_host_data(fd)) {
7d61d892
LV
3781 void *host_msg;
3782
3783 host_msg = g_malloc(msg.msg_iov->iov_len);
3784 memcpy(host_msg, msg.msg_iov->iov_base, msg.msg_iov->iov_len);
3785 ret = fd_trans_target_to_host_data(fd)(host_msg,
6c5b5645 3786 msg.msg_iov->iov_len);
7d61d892
LV
3787 if (ret >= 0) {
3788 msg.msg_iov->iov_base = host_msg;
3789 ret = get_errno(safe_sendmsg(fd, &msg, flags));
3790 }
3791 g_free(host_msg);
6c5b5645
LV
3792 } else {
3793 ret = target_to_host_cmsg(&msg, msgp);
7d61d892
LV
3794 if (ret == 0) {
3795 ret = get_errno(safe_sendmsg(fd, &msg, flags));
3796 }
6c5b5645 3797 }
3532fa74 3798 } else {
66687530 3799 ret = get_errno(safe_recvmsg(fd, &msg, flags));
6de645c7
AZ
3800 if (!is_error(ret)) {
3801 len = ret;
6c5b5645
LV
3802 if (fd_trans_host_to_target_data(fd)) {
3803 ret = fd_trans_host_to_target_data(fd)(msg.msg_iov->iov_base,
48dc0f2c 3804 len);
6c5b5645
LV
3805 } else {
3806 ret = host_to_target_cmsg(msgp, &msg);
3807 }
ca619067
JH
3808 if (!is_error(ret)) {
3809 msgp->msg_namelen = tswap32(msg.msg_namelen);
26a6fc96 3810 if (msg.msg_name != NULL && msg.msg_name != (void *)-1) {
ca619067
JH
3811 ret = host_to_target_sockaddr(tswapal(msgp->msg_name),
3812 msg.msg_name, msg.msg_namelen);
3813 if (ret) {
3814 goto out;
3815 }
3816 }
3817
6de645c7 3818 ret = len;
ca619067 3819 }
6de645c7 3820 }
3532fa74 3821 }
ca619067
JH
3822
3823out:
3532fa74 3824 unlock_iovec(vec, target_vec, count, !send);
f287b2c2 3825out2:
f19e00d7
AG
3826 return ret;
3827}
3828
3829static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
3830 int flags, int send)
3831{
3832 abi_long ret;
3833 struct target_msghdr *msgp;
3834
3835 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
3836 msgp,
3837 target_msg,
3838 send ? 1 : 0)) {
3839 return -TARGET_EFAULT;
3840 }
3841 ret = do_sendrecvmsg_locked(fd, msgp, flags, send);
579a97f7 3842 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
3532fa74
FB
3843 return ret;
3844}
3845
f19e00d7
AG
3846/* We don't rely on the C library to have sendmmsg/recvmmsg support,
3847 * so it might not have this *mmsg-specific flag either.
3848 */
3849#ifndef MSG_WAITFORONE
3850#define MSG_WAITFORONE 0x10000
3851#endif
3852
3853static abi_long do_sendrecvmmsg(int fd, abi_ulong target_msgvec,
3854 unsigned int vlen, unsigned int flags,
3855 int send)
3856{
3857 struct target_mmsghdr *mmsgp;
3858 abi_long ret = 0;
3859 int i;
3860
3861 if (vlen > UIO_MAXIOV) {
3862 vlen = UIO_MAXIOV;
3863 }
3864
3865 mmsgp = lock_user(VERIFY_WRITE, target_msgvec, sizeof(*mmsgp) * vlen, 1);
3866 if (!mmsgp) {
3867 return -TARGET_EFAULT;
3868 }
3869
3870 for (i = 0; i < vlen; i++) {
3871 ret = do_sendrecvmsg_locked(fd, &mmsgp[i].msg_hdr, flags, send);
3872 if (is_error(ret)) {
3873 break;
3874 }
3875 mmsgp[i].msg_len = tswap32(ret);
3876 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3877 if (flags & MSG_WAITFORONE) {
3878 flags |= MSG_DONTWAIT;
3879 }
3880 }
3881
3882 unlock_user(mmsgp, target_msgvec, sizeof(*mmsgp) * i);
3883
3884 /* Return number of datagrams sent if we sent any at all;
3885 * otherwise return the error.
3886 */
3887 if (i) {
3888 return i;
3889 }
3890 return ret;
3891}
f19e00d7 3892
a94b4987
PM
3893/* do_accept4() Must return target values and target errnos. */
3894static abi_long do_accept4(int fd, abi_ulong target_addr,
3895 abi_ulong target_addrlen_addr, int flags)
1be9e1dc 3896{
2f619698
FB
3897 socklen_t addrlen;
3898 void *addr;
992f48a0 3899 abi_long ret;
d25295d4
PJ
3900 int host_flags;
3901
3902 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
1be9e1dc 3903
a94b4987 3904 if (target_addr == 0) {
ff6dc130 3905 return get_errno(safe_accept4(fd, NULL, NULL, host_flags));
a94b4987 3906 }
917507b0
AP
3907
3908 /* linux returns EINVAL if addrlen pointer is invalid */
2f619698 3909 if (get_user_u32(addrlen, target_addrlen_addr))
917507b0 3910 return -TARGET_EINVAL;
2f619698 3911
38724253 3912 if ((int)addrlen < 0) {
8f7aeaf6 3913 return -TARGET_EINVAL;
38724253 3914 }
8f7aeaf6 3915
917507b0
AP
3916 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
3917 return -TARGET_EINVAL;
3918
2f619698
FB
3919 addr = alloca(addrlen);
3920
ff6dc130 3921 ret = get_errno(safe_accept4(fd, addr, &addrlen, host_flags));
1be9e1dc
PB
3922 if (!is_error(ret)) {
3923 host_to_target_sockaddr(target_addr, addr, addrlen);
2f619698
FB
3924 if (put_user_u32(addrlen, target_addrlen_addr))
3925 ret = -TARGET_EFAULT;
1be9e1dc
PB
3926 }
3927 return ret;
3928}
3929
0da46a6e 3930/* do_getpeername() Must return target values and target errnos. */
992f48a0 3931static abi_long do_getpeername(int fd, abi_ulong target_addr,
2f619698 3932 abi_ulong target_addrlen_addr)
1be9e1dc 3933{
2f619698
FB
3934 socklen_t addrlen;
3935 void *addr;
992f48a0 3936 abi_long ret;
1be9e1dc 3937
2f619698
FB
3938 if (get_user_u32(addrlen, target_addrlen_addr))
3939 return -TARGET_EFAULT;
3940
38724253 3941 if ((int)addrlen < 0) {
8f7aeaf6 3942 return -TARGET_EINVAL;
38724253 3943 }
8f7aeaf6 3944
917507b0
AP
3945 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
3946 return -TARGET_EFAULT;
3947
2f619698
FB
3948 addr = alloca(addrlen);
3949
1be9e1dc
PB
3950 ret = get_errno(getpeername(fd, addr, &addrlen));
3951 if (!is_error(ret)) {
3952 host_to_target_sockaddr(target_addr, addr, addrlen);
2f619698
FB
3953 if (put_user_u32(addrlen, target_addrlen_addr))
3954 ret = -TARGET_EFAULT;
1be9e1dc
PB
3955 }
3956 return ret;
3957}
3958
0da46a6e 3959/* do_getsockname() Must return target values and target errnos. */
992f48a0 3960static abi_long do_getsockname(int fd, abi_ulong target_addr,
2f619698 3961 abi_ulong target_addrlen_addr)
1be9e1dc 3962{
2f619698
FB
3963 socklen_t addrlen;
3964 void *addr;
992f48a0 3965 abi_long ret;
1be9e1dc 3966
2f619698
FB
3967 if (get_user_u32(addrlen, target_addrlen_addr))
3968 return -TARGET_EFAULT;
3969
38724253 3970 if ((int)addrlen < 0) {
8f7aeaf6 3971 return -TARGET_EINVAL;
38724253 3972 }
8f7aeaf6 3973
917507b0
AP
3974 if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
3975 return -TARGET_EFAULT;
3976
2f619698
FB
3977 addr = alloca(addrlen);
3978
1be9e1dc
PB
3979 ret = get_errno(getsockname(fd, addr, &addrlen));
3980 if (!is_error(ret)) {
3981 host_to_target_sockaddr(target_addr, addr, addrlen);
2f619698
FB
3982 if (put_user_u32(addrlen, target_addrlen_addr))
3983 ret = -TARGET_EFAULT;
1be9e1dc
PB
3984 }
3985 return ret;
3986}
3987
0da46a6e 3988/* do_socketpair() Must return target values and target errnos. */
992f48a0 3989static abi_long do_socketpair(int domain, int type, int protocol,
2f619698 3990 abi_ulong target_tab_addr)
1be9e1dc
PB
3991{
3992 int tab[2];
992f48a0 3993 abi_long ret;
1be9e1dc 3994
f651e6ae
PJ
3995 target_to_host_sock_type(&type);
3996
1be9e1dc
PB
3997 ret = get_errno(socketpair(domain, type, protocol, tab));
3998 if (!is_error(ret)) {
2f619698
FB
3999 if (put_user_s32(tab[0], target_tab_addr)
4000 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
4001 ret = -TARGET_EFAULT;
1be9e1dc
PB
4002 }
4003 return ret;
4004}
4005
0da46a6e 4006/* do_sendto() Must return target values and target errnos. */
992f48a0
BS
4007static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
4008 abi_ulong target_addr, socklen_t addrlen)
1be9e1dc
PB
4009{
4010 void *addr;
4011 void *host_msg;
7d61d892 4012 void *copy_msg = NULL;
992f48a0 4013 abi_long ret;
1be9e1dc 4014
38724253 4015 if ((int)addrlen < 0) {
8f7aeaf6 4016 return -TARGET_EINVAL;
38724253 4017 }
8f7aeaf6 4018
579a97f7
FB
4019 host_msg = lock_user(VERIFY_READ, msg, len, 1);
4020 if (!host_msg)
4021 return -TARGET_EFAULT;
6c5b5645 4022 if (fd_trans_target_to_host_data(fd)) {
7d61d892
LV
4023 copy_msg = host_msg;
4024 host_msg = g_malloc(len);
4025 memcpy(host_msg, copy_msg, len);
6c5b5645
LV
4026 ret = fd_trans_target_to_host_data(fd)(host_msg, len);
4027 if (ret < 0) {
7d61d892 4028 goto fail;
6c5b5645
LV
4029 }
4030 }
1be9e1dc 4031 if (target_addr) {
2dd08dfd 4032 addr = alloca(addrlen+1);
7b36f782 4033 ret = target_to_host_sockaddr(fd, addr, target_addr, addrlen);
917507b0 4034 if (ret) {
7d61d892 4035 goto fail;
917507b0 4036 }
66687530 4037 ret = get_errno(safe_sendto(fd, host_msg, len, flags, addr, addrlen));
1be9e1dc 4038 } else {
66687530 4039 ret = get_errno(safe_sendto(fd, host_msg, len, flags, NULL, 0));
1be9e1dc 4040 }
7d61d892
LV
4041fail:
4042 if (copy_msg) {
4043 g_free(host_msg);
4044 host_msg = copy_msg;
4045 }
1be9e1dc
PB
4046 unlock_user(host_msg, msg, 0);
4047 return ret;
4048}
4049
0da46a6e 4050/* do_recvfrom() Must return target values and target errnos. */
992f48a0
BS
4051static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
4052 abi_ulong target_addr,
4053 abi_ulong target_addrlen)
1be9e1dc
PB
4054{
4055 socklen_t addrlen;
4056 void *addr;
4057 void *host_msg;
992f48a0 4058 abi_long ret;
1be9e1dc 4059
579a97f7
FB
4060 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
4061 if (!host_msg)
4062 return -TARGET_EFAULT;
1be9e1dc 4063 if (target_addr) {
2f619698
FB
4064 if (get_user_u32(addrlen, target_addrlen)) {
4065 ret = -TARGET_EFAULT;
4066 goto fail;
4067 }
38724253 4068 if ((int)addrlen < 0) {
8f7aeaf6
AJ
4069 ret = -TARGET_EINVAL;
4070 goto fail;
4071 }
1be9e1dc 4072 addr = alloca(addrlen);
66687530
PM
4073 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags,
4074 addr, &addrlen));
1be9e1dc
PB
4075 } else {
4076 addr = NULL; /* To keep compiler quiet. */
66687530 4077 ret = get_errno(safe_recvfrom(fd, host_msg, len, flags, NULL, 0));
1be9e1dc
PB
4078 }
4079 if (!is_error(ret)) {
c35e1f9c
LV
4080 if (fd_trans_host_to_target_data(fd)) {
4081 ret = fd_trans_host_to_target_data(fd)(host_msg, ret);
4082 }
1be9e1dc
PB
4083 if (target_addr) {
4084 host_to_target_sockaddr(target_addr, addr, addrlen);
2f619698
FB
4085 if (put_user_u32(addrlen, target_addrlen)) {
4086 ret = -TARGET_EFAULT;
4087 goto fail;
4088 }
1be9e1dc
PB
4089 }
4090 unlock_user(host_msg, msg, len);
4091 } else {
2f619698 4092fail:
1be9e1dc
PB
4093 unlock_user(host_msg, msg, 0);
4094 }
4095 return ret;
4096}
4097
32407103 4098#ifdef TARGET_NR_socketcall
ff71a454 4099/* do_socketcall() must return target values and target errnos. */
992f48a0 4100static abi_long do_socketcall(int num, abi_ulong vptr)
31e31b8a 4101{
ff71a454
AM
4102 static const unsigned nargs[] = { /* number of arguments per operation */
4103 [TARGET_SYS_SOCKET] = 3, /* domain, type, protocol */
4104 [TARGET_SYS_BIND] = 3, /* fd, addr, addrlen */
4105 [TARGET_SYS_CONNECT] = 3, /* fd, addr, addrlen */
4106 [TARGET_SYS_LISTEN] = 2, /* fd, backlog */
4107 [TARGET_SYS_ACCEPT] = 3, /* fd, addr, addrlen */
4108 [TARGET_SYS_GETSOCKNAME] = 3, /* fd, addr, addrlen */
4109 [TARGET_SYS_GETPEERNAME] = 3, /* fd, addr, addrlen */
4110 [TARGET_SYS_SOCKETPAIR] = 4, /* domain, type, protocol, tab */
4111 [TARGET_SYS_SEND] = 4, /* fd, msg, len, flags */
4112 [TARGET_SYS_RECV] = 4, /* fd, msg, len, flags */
4113 [TARGET_SYS_SENDTO] = 6, /* fd, msg, len, flags, addr, addrlen */
4114 [TARGET_SYS_RECVFROM] = 6, /* fd, msg, len, flags, addr, addrlen */
4115 [TARGET_SYS_SHUTDOWN] = 2, /* fd, how */
4116 [TARGET_SYS_SETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
4117 [TARGET_SYS_GETSOCKOPT] = 5, /* fd, level, optname, optval, optlen */
4118 [TARGET_SYS_SENDMSG] = 3, /* fd, msg, flags */
4119 [TARGET_SYS_RECVMSG] = 3, /* fd, msg, flags */
4120 [TARGET_SYS_ACCEPT4] = 4, /* fd, addr, addrlen, flags */
4121 [TARGET_SYS_RECVMMSG] = 4, /* fd, msgvec, vlen, flags */
4122 [TARGET_SYS_SENDMMSG] = 4, /* fd, msgvec, vlen, flags */
62dc90c6
MT
4123 };
4124 abi_long a[6]; /* max 6 args */
ff71a454 4125 unsigned i;
62dc90c6 4126
ff71a454
AM
4127 /* check the range of the first argument num */
4128 /* (TARGET_SYS_SENDMMSG is the highest among TARGET_SYS_xxx) */
4129 if (num < 1 || num > TARGET_SYS_SENDMMSG) {
4130 return -TARGET_EINVAL;
4131 }
4132 /* ensure we have space for args */
4133 if (nargs[num] > ARRAY_SIZE(a)) {
4134 return -TARGET_EINVAL;
4135 }
4136 /* collect the arguments in a[] according to nargs[] */
4137 for (i = 0; i < nargs[num]; ++i) {
4138 if (get_user_ual(a[i], vptr + i * sizeof(abi_long)) != 0) {
4139 return -TARGET_EFAULT;
31e31b8a 4140 }
62dc90c6 4141 }
ff71a454 4142 /* now when we have the args, invoke the appropriate underlying function */
62dc90c6 4143 switch (num) {
ff71a454 4144 case TARGET_SYS_SOCKET: /* domain, type, protocol */
62dc90c6 4145 return do_socket(a[0], a[1], a[2]);
ff71a454 4146 case TARGET_SYS_BIND: /* sockfd, addr, addrlen */
62dc90c6 4147 return do_bind(a[0], a[1], a[2]);
ff71a454 4148 case TARGET_SYS_CONNECT: /* sockfd, addr, addrlen */
62dc90c6 4149 return do_connect(a[0], a[1], a[2]);
ff71a454 4150 case TARGET_SYS_LISTEN: /* sockfd, backlog */
62dc90c6 4151 return get_errno(listen(a[0], a[1]));
ff71a454 4152 case TARGET_SYS_ACCEPT: /* sockfd, addr, addrlen */
62dc90c6 4153 return do_accept4(a[0], a[1], a[2], 0);
ff71a454 4154 case TARGET_SYS_GETSOCKNAME: /* sockfd, addr, addrlen */
62dc90c6 4155 return do_getsockname(a[0], a[1], a[2]);
ff71a454 4156 case TARGET_SYS_GETPEERNAME: /* sockfd, addr, addrlen */
62dc90c6 4157 return do_getpeername(a[0], a[1], a[2]);
ff71a454 4158 case TARGET_SYS_SOCKETPAIR: /* domain, type, protocol, tab */
62dc90c6 4159 return do_socketpair(a[0], a[1], a[2], a[3]);
ff71a454 4160 case TARGET_SYS_SEND: /* sockfd, msg, len, flags */
62dc90c6 4161 return do_sendto(a[0], a[1], a[2], a[3], 0, 0);
ff71a454 4162 case TARGET_SYS_RECV: /* sockfd, msg, len, flags */
62dc90c6 4163 return do_recvfrom(a[0], a[1], a[2], a[3], 0, 0);
ff71a454 4164 case TARGET_SYS_SENDTO: /* sockfd, msg, len, flags, addr, addrlen */
62dc90c6 4165 return do_sendto(a[0], a[1], a[2], a[3], a[4], a[5]);
ff71a454 4166 case TARGET_SYS_RECVFROM: /* sockfd, msg, len, flags, addr, addrlen */
62dc90c6 4167 return do_recvfrom(a[0], a[1], a[2], a[3], a[4], a[5]);
ff71a454 4168 case TARGET_SYS_SHUTDOWN: /* sockfd, how */
62dc90c6 4169 return get_errno(shutdown(a[0], a[1]));
ff71a454
AM
4170 case TARGET_SYS_SETSOCKOPT: /* sockfd, level, optname, optval, optlen */
4171 return do_setsockopt(a[0], a[1], a[2], a[3], a[4]);
4172 case TARGET_SYS_GETSOCKOPT: /* sockfd, level, optname, optval, optlen */
4173 return do_getsockopt(a[0], a[1], a[2], a[3], a[4]);
4174 case TARGET_SYS_SENDMSG: /* sockfd, msg, flags */
62dc90c6 4175 return do_sendrecvmsg(a[0], a[1], a[2], 1);
ff71a454 4176 case TARGET_SYS_RECVMSG: /* sockfd, msg, flags */
62dc90c6 4177 return do_sendrecvmsg(a[0], a[1], a[2], 0);
ff71a454
AM
4178 case TARGET_SYS_ACCEPT4: /* sockfd, addr, addrlen, flags */
4179 return do_accept4(a[0], a[1], a[2], a[3]);
4180 case TARGET_SYS_RECVMMSG: /* sockfd, msgvec, vlen, flags */
5a53dc50 4181 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 0);
ff71a454
AM
4182 case TARGET_SYS_SENDMMSG: /* sockfd, msgvec, vlen, flags */
4183 return do_sendrecvmmsg(a[0], a[1], a[2], a[3], 1);
31e31b8a
FB
4184 default:
4185 gemu_log("Unsupported socketcall: %d\n", num);
ff71a454 4186 return -TARGET_EINVAL;
31e31b8a 4187 }
31e31b8a 4188}
32407103 4189#endif
31e31b8a 4190
8853f86e
FB
4191#define N_SHM_REGIONS 32
4192
4193static struct shm_region {
b6e17875
PM
4194 abi_ulong start;
4195 abi_ulong size;
4196 bool in_use;
8853f86e
FB
4197} shm_regions[N_SHM_REGIONS];
4198
005eb2ae
PM
4199#ifndef TARGET_SEMID64_DS
4200/* asm-generic version of this struct */
4201struct target_semid64_ds
3eb6b044
TS
4202{
4203 struct target_ipc_perm sem_perm;
992f48a0 4204 abi_ulong sem_otime;
005eb2ae 4205#if TARGET_ABI_BITS == 32
992f48a0 4206 abi_ulong __unused1;
03527344 4207#endif
992f48a0 4208 abi_ulong sem_ctime;
005eb2ae 4209#if TARGET_ABI_BITS == 32
992f48a0 4210 abi_ulong __unused2;
03527344 4211#endif
992f48a0
BS
4212 abi_ulong sem_nsems;
4213 abi_ulong __unused3;
4214 abi_ulong __unused4;
3eb6b044 4215};
005eb2ae 4216#endif
3eb6b044 4217
579a97f7
FB
4218static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
4219 abi_ulong target_addr)
3eb6b044
TS
4220{
4221 struct target_ipc_perm *target_ip;
005eb2ae 4222 struct target_semid64_ds *target_sd;
3eb6b044 4223
579a97f7
FB
4224 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
4225 return -TARGET_EFAULT;
e8bbe36c 4226 target_ip = &(target_sd->sem_perm);
55a2b163
PJ
4227 host_ip->__key = tswap32(target_ip->__key);
4228 host_ip->uid = tswap32(target_ip->uid);
4229 host_ip->gid = tswap32(target_ip->gid);
4230 host_ip->cuid = tswap32(target_ip->cuid);
4231 host_ip->cgid = tswap32(target_ip->cgid);
4232#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
4233 host_ip->mode = tswap32(target_ip->mode);
4234#else
cbb21eed 4235 host_ip->mode = tswap16(target_ip->mode);
55a2b163
PJ
4236#endif
4237#if defined(TARGET_PPC)
4238 host_ip->__seq = tswap32(target_ip->__seq);
4239#else
4240 host_ip->__seq = tswap16(target_ip->__seq);
4241#endif
3eb6b044 4242 unlock_user_struct(target_sd, target_addr, 0);
579a97f7 4243 return 0;
3eb6b044
TS
4244}
4245
579a97f7
FB
4246static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
4247 struct ipc_perm *host_ip)
3eb6b044
TS
4248{
4249 struct target_ipc_perm *target_ip;
005eb2ae 4250 struct target_semid64_ds *target_sd;
3eb6b044 4251
579a97f7
FB
4252 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
4253 return -TARGET_EFAULT;
3eb6b044 4254 target_ip = &(target_sd->sem_perm);
55a2b163
PJ
4255 target_ip->__key = tswap32(host_ip->__key);
4256 target_ip->uid = tswap32(host_ip->uid);
4257 target_ip->gid = tswap32(host_ip->gid);
4258 target_ip->cuid = tswap32(host_ip->cuid);
4259 target_ip->cgid = tswap32(host_ip->cgid);
4260#if defined(TARGET_ALPHA) || defined(TARGET_MIPS) || defined(TARGET_PPC)
4261 target_ip->mode = tswap32(host_ip->mode);
4262#else
cbb21eed 4263 target_ip->mode = tswap16(host_ip->mode);
55a2b163
PJ
4264#endif
4265#if defined(TARGET_PPC)
4266 target_ip->__seq = tswap32(host_ip->__seq);
4267#else
4268 target_ip->__seq = tswap16(host_ip->__seq);
4269#endif
3eb6b044 4270 unlock_user_struct(target_sd, target_addr, 1);
579a97f7 4271 return 0;
3eb6b044
TS
4272}
4273
579a97f7
FB
4274static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
4275 abi_ulong target_addr)
3eb6b044 4276{
005eb2ae 4277 struct target_semid64_ds *target_sd;
3eb6b044 4278
579a97f7
FB
4279 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
4280 return -TARGET_EFAULT;
e5289087
AJ
4281 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
4282 return -TARGET_EFAULT;
cbb21eed
MB
4283 host_sd->sem_nsems = tswapal(target_sd->sem_nsems);
4284 host_sd->sem_otime = tswapal(target_sd->sem_otime);
4285 host_sd->sem_ctime = tswapal(target_sd->sem_ctime);
3eb6b044 4286 unlock_user_struct(target_sd, target_addr, 0);
579a97f7 4287 return 0;
3eb6b044
TS
4288}
4289
579a97f7
FB
4290static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
4291 struct semid_ds *host_sd)
3eb6b044 4292{
005eb2ae 4293 struct target_semid64_ds *target_sd;
3eb6b044 4294
579a97f7
FB
4295 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
4296 return -TARGET_EFAULT;
e5289087 4297 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
3a93113a 4298 return -TARGET_EFAULT;
cbb21eed
MB
4299 target_sd->sem_nsems = tswapal(host_sd->sem_nsems);
4300 target_sd->sem_otime = tswapal(host_sd->sem_otime);
4301 target_sd->sem_ctime = tswapal(host_sd->sem_ctime);
3eb6b044 4302 unlock_user_struct(target_sd, target_addr, 1);
579a97f7 4303 return 0;
3eb6b044
TS
4304}
4305
e5289087
AJ
4306struct target_seminfo {
4307 int semmap;
4308 int semmni;
4309 int semmns;
4310 int semmnu;
4311 int semmsl;
4312 int semopm;
4313 int semume;
4314 int semusz;
4315 int semvmx;
4316 int semaem;
4317};
4318
4319static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
4320 struct seminfo *host_seminfo)
4321{
4322 struct target_seminfo *target_seminfo;
4323 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
4324 return -TARGET_EFAULT;
4325 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
4326 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
4327 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
4328 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
4329 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
4330 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
4331 __put_user(host_seminfo->semume, &target_seminfo->semume);
4332 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
4333 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
4334 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
4335 unlock_user_struct(target_seminfo, target_addr, 1);
4336 return 0;
4337}
4338
fa294816
TS
4339union semun {
4340 int val;
3eb6b044 4341 struct semid_ds *buf;
fa294816 4342 unsigned short *array;
e5289087 4343 struct seminfo *__buf;
fa294816
TS
4344};
4345
3eb6b044
TS
4346union target_semun {
4347 int val;
e5289087
AJ
4348 abi_ulong buf;
4349 abi_ulong array;
4350 abi_ulong __buf;
3eb6b044
TS
4351};
4352
e5289087
AJ
4353static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
4354 abi_ulong target_addr)
3eb6b044 4355{
e5289087
AJ
4356 int nsems;
4357 unsigned short *array;
4358 union semun semun;
4359 struct semid_ds semid_ds;
4360 int i, ret;
3eb6b044 4361
e5289087
AJ
4362 semun.buf = &semid_ds;
4363
4364 ret = semctl(semid, 0, IPC_STAT, semun);
4365 if (ret == -1)
4366 return get_errno(ret);
4367
4368 nsems = semid_ds.sem_nsems;
4369
0e173b24 4370 *host_array = g_try_new(unsigned short, nsems);
69d4c703
PM
4371 if (!*host_array) {
4372 return -TARGET_ENOMEM;
4373 }
e5289087
AJ
4374 array = lock_user(VERIFY_READ, target_addr,
4375 nsems*sizeof(unsigned short), 1);
69d4c703 4376 if (!array) {
0e173b24 4377 g_free(*host_array);
e5289087 4378 return -TARGET_EFAULT;
69d4c703 4379 }
e5289087
AJ
4380
4381 for(i=0; i<nsems; i++) {
4382 __get_user((*host_array)[i], &array[i]);
3eb6b044 4383 }
e5289087
AJ
4384 unlock_user(array, target_addr, 0);
4385
579a97f7 4386 return 0;
3eb6b044
TS
4387}
4388
e5289087
AJ
4389static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
4390 unsigned short **host_array)
3eb6b044 4391{
e5289087
AJ
4392 int nsems;
4393 unsigned short *array;
4394 union semun semun;
4395 struct semid_ds semid_ds;
4396 int i, ret;
3eb6b044 4397
e5289087
AJ
4398 semun.buf = &semid_ds;
4399
4400 ret = semctl(semid, 0, IPC_STAT, semun);
4401 if (ret == -1)
4402 return get_errno(ret);
4403
4404 nsems = semid_ds.sem_nsems;
4405
4406 array = lock_user(VERIFY_WRITE, target_addr,
4407 nsems*sizeof(unsigned short), 0);
4408 if (!array)
4409 return -TARGET_EFAULT;
4410
4411 for(i=0; i<nsems; i++) {
4412 __put_user((*host_array)[i], &array[i]);
3eb6b044 4413 }
0e173b24 4414 g_free(*host_array);
e5289087
AJ
4415 unlock_user(array, target_addr, 1);
4416
579a97f7 4417 return 0;
3eb6b044
TS
4418}
4419
e5289087 4420static inline abi_long do_semctl(int semid, int semnum, int cmd,
d1c002b6 4421 abi_ulong target_arg)
3eb6b044 4422{
d1c002b6 4423 union target_semun target_su = { .buf = target_arg };
3eb6b044
TS
4424 union semun arg;
4425 struct semid_ds dsarg;
7b8118e8 4426 unsigned short *array = NULL;
e5289087
AJ
4427 struct seminfo seminfo;
4428 abi_long ret = -TARGET_EINVAL;
4429 abi_long err;
4430 cmd &= 0xff;
3eb6b044
TS
4431
4432 switch( cmd ) {
4433 case GETVAL:
3eb6b044 4434 case SETVAL:
5464baec
TM
4435 /* In 64 bit cross-endian situations, we will erroneously pick up
4436 * the wrong half of the union for the "val" element. To rectify
4437 * this, the entire 8-byte structure is byteswapped, followed by
4438 * a swap of the 4 byte val field. In other cases, the data is
4439 * already in proper host byte order. */
4440 if (sizeof(target_su.val) != (sizeof(target_su.buf))) {
4441 target_su.buf = tswapal(target_su.buf);
4442 arg.val = tswap32(target_su.val);
4443 } else {
4444 arg.val = target_su.val;
4445 }
e5289087 4446 ret = get_errno(semctl(semid, semnum, cmd, arg));
3eb6b044
TS
4447 break;
4448 case GETALL:
3eb6b044 4449 case SETALL:
e5289087
AJ
4450 err = target_to_host_semarray(semid, &array, target_su.array);
4451 if (err)
4452 return err;
4453 arg.array = array;
4454 ret = get_errno(semctl(semid, semnum, cmd, arg));
4455 err = host_to_target_semarray(semid, target_su.array, &array);
4456 if (err)
4457 return err;
3eb6b044
TS
4458 break;
4459 case IPC_STAT:
3eb6b044 4460 case IPC_SET:
e5289087
AJ
4461 case SEM_STAT:
4462 err = target_to_host_semid_ds(&dsarg, target_su.buf);
4463 if (err)
4464 return err;
4465 arg.buf = &dsarg;
4466 ret = get_errno(semctl(semid, semnum, cmd, arg));
4467 err = host_to_target_semid_ds(target_su.buf, &dsarg);
4468 if (err)
4469 return err;
4470 break;
4471 case IPC_INFO:
4472 case SEM_INFO:
4473 arg.__buf = &seminfo;
4474 ret = get_errno(semctl(semid, semnum, cmd, arg));
4475 err = host_to_target_seminfo(target_su.__buf, &seminfo);
4476 if (err)
4477 return err;
4478 break;
4479 case IPC_RMID:
4480 case GETPID:
4481 case GETNCNT:
4482 case GETZCNT:
4483 ret = get_errno(semctl(semid, semnum, cmd, NULL));
3eb6b044 4484 break;
3eb6b044
TS
4485 }
4486
4487 return ret;
4488}
4489
e5289087
AJ
4490struct target_sembuf {
4491 unsigned short sem_num;
4492 short sem_op;
4493 short sem_flg;
4494};
4495
4496static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
4497 abi_ulong target_addr,
4498 unsigned nsops)
4499{
4500 struct target_sembuf *target_sembuf;
4501 int i;
4502
4503 target_sembuf = lock_user(VERIFY_READ, target_addr,
4504 nsops*sizeof(struct target_sembuf), 1);
4505 if (!target_sembuf)
4506 return -TARGET_EFAULT;
4507
4508 for(i=0; i<nsops; i++) {
4509 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
4510 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
4511 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
4512 }
4513
4514 unlock_user(target_sembuf, target_addr, 0);
4515
4516 return 0;
4517}
4518
4519static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
4520{
4521 struct sembuf sops[nsops];
4522
4523 if (target_to_host_sembuf(sops, ptr, nsops))
4524 return -TARGET_EFAULT;
4525
ffb7ee79 4526 return get_errno(safe_semtimedop(semid, sops, nsops, NULL));
e5289087
AJ
4527}
4528
1bc012f6
TS
4529struct target_msqid_ds
4530{
1c54ff97
AJ
4531 struct target_ipc_perm msg_perm;
4532 abi_ulong msg_stime;
4533#if TARGET_ABI_BITS == 32
4534 abi_ulong __unused1;
4535#endif
4536 abi_ulong msg_rtime;
4537#if TARGET_ABI_BITS == 32
4538 abi_ulong __unused2;
4539#endif
4540 abi_ulong msg_ctime;
4541#if TARGET_ABI_BITS == 32
4542 abi_ulong __unused3;
4543#endif
4544 abi_ulong __msg_cbytes;
4545 abi_ulong msg_qnum;
4546 abi_ulong msg_qbytes;
4547 abi_ulong msg_lspid;
4548 abi_ulong msg_lrpid;
4549 abi_ulong __unused4;
4550 abi_ulong __unused5;
1bc012f6
TS
4551};
4552
579a97f7
FB
4553static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
4554 abi_ulong target_addr)
1bc012f6
TS
4555{
4556 struct target_msqid_ds *target_md;
4557
579a97f7
FB
4558 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
4559 return -TARGET_EFAULT;
1c54ff97
AJ
4560 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
4561 return -TARGET_EFAULT;
cbb21eed
MB
4562 host_md->msg_stime = tswapal(target_md->msg_stime);
4563 host_md->msg_rtime = tswapal(target_md->msg_rtime);
4564 host_md->msg_ctime = tswapal(target_md->msg_ctime);
4565 host_md->__msg_cbytes = tswapal(target_md->__msg_cbytes);
4566 host_md->msg_qnum = tswapal(target_md->msg_qnum);
4567 host_md->msg_qbytes = tswapal(target_md->msg_qbytes);
4568 host_md->msg_lspid = tswapal(target_md->msg_lspid);
4569 host_md->msg_lrpid = tswapal(target_md->msg_lrpid);
1bc012f6 4570 unlock_user_struct(target_md, target_addr, 0);
579a97f7 4571 return 0;
1bc012f6
TS
4572}
4573
579a97f7
FB
4574static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
4575 struct msqid_ds *host_md)
1bc012f6
TS
4576{
4577 struct target_msqid_ds *target_md;
4578
579a97f7
FB
4579 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
4580 return -TARGET_EFAULT;
1c54ff97
AJ
4581 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
4582 return -TARGET_EFAULT;
cbb21eed
MB
4583 target_md->msg_stime = tswapal(host_md->msg_stime);
4584 target_md->msg_rtime = tswapal(host_md->msg_rtime);
4585 target_md->msg_ctime = tswapal(host_md->msg_ctime);
4586 target_md->__msg_cbytes = tswapal(host_md->__msg_cbytes);
4587 target_md->msg_qnum = tswapal(host_md->msg_qnum);
4588 target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
4589 target_md->msg_lspid = tswapal(host_md->msg_lspid);
4590 target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
1bc012f6 4591 unlock_user_struct(target_md, target_addr, 1);
579a97f7 4592 return 0;
1bc012f6
TS
4593}
4594
1c54ff97
AJ
4595struct target_msginfo {
4596 int msgpool;
4597 int msgmap;
4598 int msgmax;
4599 int msgmnb;
4600 int msgmni;
4601 int msgssz;
4602 int msgtql;
4603 unsigned short int msgseg;
4604};
4605
4606static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
4607 struct msginfo *host_msginfo)
4608{
4609 struct target_msginfo *target_msginfo;
4610 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
4611 return -TARGET_EFAULT;
4612 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
4613 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
4614 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
4615 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
4616 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
4617 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
4618 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
4619 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
4620 unlock_user_struct(target_msginfo, target_addr, 1);
00b229ac 4621 return 0;
1c54ff97
AJ
4622}
4623
4624static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
1bc012f6
TS
4625{
4626 struct msqid_ds dsarg;
1c54ff97
AJ
4627 struct msginfo msginfo;
4628 abi_long ret = -TARGET_EINVAL;
4629
4630 cmd &= 0xff;
4631
4632 switch (cmd) {
1bc012f6
TS
4633 case IPC_STAT:
4634 case IPC_SET:
1c54ff97
AJ
4635 case MSG_STAT:
4636 if (target_to_host_msqid_ds(&dsarg,ptr))
4637 return -TARGET_EFAULT;
4638 ret = get_errno(msgctl(msgid, cmd, &dsarg));
4639 if (host_to_target_msqid_ds(ptr,&dsarg))
4640 return -TARGET_EFAULT;
4641 break;
4642 case IPC_RMID:
4643 ret = get_errno(msgctl(msgid, cmd, NULL));
4644 break;
4645 case IPC_INFO:
4646 case MSG_INFO:
4647 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
4648 if (host_to_target_msginfo(ptr, &msginfo))
4649 return -TARGET_EFAULT;
4650 break;
1bc012f6 4651 }
1c54ff97 4652
1bc012f6
TS
4653 return ret;
4654}
4655
4656struct target_msgbuf {
1c54ff97
AJ
4657 abi_long mtype;
4658 char mtext[1];
1bc012f6
TS
4659};
4660
992f48a0 4661static inline abi_long do_msgsnd(int msqid, abi_long msgp,
edcc5f9d 4662 ssize_t msgsz, int msgflg)
1bc012f6
TS
4663{
4664 struct target_msgbuf *target_mb;
4665 struct msgbuf *host_mb;
992f48a0 4666 abi_long ret = 0;
1bc012f6 4667
edcc5f9d
TM
4668 if (msgsz < 0) {
4669 return -TARGET_EINVAL;
4670 }
4671
579a97f7
FB
4672 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
4673 return -TARGET_EFAULT;
0e173b24 4674 host_mb = g_try_malloc(msgsz + sizeof(long));
29e03fcb
HZ
4675 if (!host_mb) {
4676 unlock_user_struct(target_mb, msgp, 0);
4677 return -TARGET_ENOMEM;
4678 }
cbb21eed 4679 host_mb->mtype = (abi_long) tswapal(target_mb->mtype);
1c54ff97 4680 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
89f9fe44 4681 ret = get_errno(safe_msgsnd(msqid, host_mb, msgsz, msgflg));
0e173b24 4682 g_free(host_mb);
1bc012f6
TS
4683 unlock_user_struct(target_mb, msgp, 0);
4684
4685 return ret;
4686}
4687
992f48a0 4688static inline abi_long do_msgrcv(int msqid, abi_long msgp,
99874f65 4689 ssize_t msgsz, abi_long msgtyp,
992f48a0 4690 int msgflg)
1bc012f6
TS
4691{
4692 struct target_msgbuf *target_mb;
579a97f7 4693 char *target_mtext;
1bc012f6 4694 struct msgbuf *host_mb;
992f48a0 4695 abi_long ret = 0;
1bc012f6 4696
99874f65
PM
4697 if (msgsz < 0) {
4698 return -TARGET_EINVAL;
4699 }
4700
579a97f7
FB
4701 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
4702 return -TARGET_EFAULT;
1c54ff97 4703
415d8471
PM
4704 host_mb = g_try_malloc(msgsz + sizeof(long));
4705 if (!host_mb) {
4706 ret = -TARGET_ENOMEM;
4707 goto end;
4708 }
89f9fe44 4709 ret = get_errno(safe_msgrcv(msqid, host_mb, msgsz, msgtyp, msgflg));
1c54ff97 4710
579a97f7
FB
4711 if (ret > 0) {
4712 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
4713 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
4714 if (!target_mtext) {
4715 ret = -TARGET_EFAULT;
4716 goto end;
4717 }
1c54ff97 4718 memcpy(target_mb->mtext, host_mb->mtext, ret);
579a97f7
FB
4719 unlock_user(target_mtext, target_mtext_addr, ret);
4720 }
1c54ff97 4721
cbb21eed 4722 target_mb->mtype = tswapal(host_mb->mtype);
1bc012f6 4723
579a97f7
FB
4724end:
4725 if (target_mb)
4726 unlock_user_struct(target_mb, msgp, 1);
0d07fe47 4727 g_free(host_mb);
1bc012f6
TS
4728 return ret;
4729}
4730
88a8c984
RV
4731static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
4732 abi_ulong target_addr)
4733{
4734 struct target_shmid_ds *target_sd;
4735
4736 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
4737 return -TARGET_EFAULT;
4738 if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
4739 return -TARGET_EFAULT;
4740 __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
4741 __get_user(host_sd->shm_atime, &target_sd->shm_atime);
4742 __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
4743 __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
4744 __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
4745 __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
4746 __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
4747 unlock_user_struct(target_sd, target_addr, 0);
4748 return 0;
4749}
4750
4751static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
4752 struct shmid_ds *host_sd)
4753{
4754 struct target_shmid_ds *target_sd;
4755
4756 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
4757 return -TARGET_EFAULT;
4758 if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
4759 return -TARGET_EFAULT;
4760 __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
4761 __put_user(host_sd->shm_atime, &target_sd->shm_atime);
4762 __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
4763 __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
4764 __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
4765 __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
4766 __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
4767 unlock_user_struct(target_sd, target_addr, 1);
4768 return 0;
4769}
4770
4771struct target_shminfo {
4772 abi_ulong shmmax;
4773 abi_ulong shmmin;
4774 abi_ulong shmmni;
4775 abi_ulong shmseg;
4776 abi_ulong shmall;
4777};
4778
4779static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
4780 struct shminfo *host_shminfo)
4781{
4782 struct target_shminfo *target_shminfo;
4783 if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
4784 return -TARGET_EFAULT;
4785 __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
4786 __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
4787 __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
4788 __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
4789 __put_user(host_shminfo->shmall, &target_shminfo->shmall);
4790 unlock_user_struct(target_shminfo, target_addr, 1);
4791 return 0;
4792}
4793
4794struct target_shm_info {
4795 int used_ids;
4796 abi_ulong shm_tot;
4797 abi_ulong shm_rss;
4798 abi_ulong shm_swp;
4799 abi_ulong swap_attempts;
4800 abi_ulong swap_successes;
4801};
4802
4803static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
4804 struct shm_info *host_shm_info)
4805{
4806 struct target_shm_info *target_shm_info;
4807 if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
4808 return -TARGET_EFAULT;
4809 __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
4810 __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
4811 __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
4812 __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
4813 __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
4814 __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
4815 unlock_user_struct(target_shm_info, target_addr, 1);
4816 return 0;
4817}
4818
4819static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
4820{
4821 struct shmid_ds dsarg;
4822 struct shminfo shminfo;
4823 struct shm_info shm_info;
4824 abi_long ret = -TARGET_EINVAL;
4825
4826 cmd &= 0xff;
4827
4828 switch(cmd) {
4829 case IPC_STAT:
4830 case IPC_SET:
4831 case SHM_STAT:
4832 if (target_to_host_shmid_ds(&dsarg, buf))
4833 return -TARGET_EFAULT;
4834 ret = get_errno(shmctl(shmid, cmd, &dsarg));
4835 if (host_to_target_shmid_ds(buf, &dsarg))
4836 return -TARGET_EFAULT;
4837 break;
4838 case IPC_INFO:
4839 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
4840 if (host_to_target_shminfo(buf, &shminfo))
4841 return -TARGET_EFAULT;
4842 break;
4843 case SHM_INFO:
4844 ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
4845 if (host_to_target_shm_info(buf, &shm_info))
4846 return -TARGET_EFAULT;
4847 break;
4848 case IPC_RMID:
4849 case SHM_LOCK:
4850 case SHM_UNLOCK:
4851 ret = get_errno(shmctl(shmid, cmd, NULL));
4852 break;
4853 }
4854
4855 return ret;
4856}
4857
ee8e7614
PM
4858#ifndef TARGET_FORCE_SHMLBA
4859/* For most architectures, SHMLBA is the same as the page size;
4860 * some architectures have larger values, in which case they should
4861 * define TARGET_FORCE_SHMLBA and provide a target_shmlba() function.
4862 * This corresponds to the kernel arch code defining __ARCH_FORCE_SHMLBA
4863 * and defining its own value for SHMLBA.
4864 *
4865 * The kernel also permits SHMLBA to be set by the architecture to a
4866 * value larger than the page size without setting __ARCH_FORCE_SHMLBA;
4867 * this means that addresses are rounded to the large size if
4868 * SHM_RND is set but addresses not aligned to that size are not rejected
4869 * as long as they are at least page-aligned. Since the only architecture
4870 * which uses this is ia64 this code doesn't provide for that oddity.
4871 */
4872static inline abi_ulong target_shmlba(CPUArchState *cpu_env)
4873{
4874 return TARGET_PAGE_SIZE;
4875}
4876#endif
4877
4878static inline abi_ulong do_shmat(CPUArchState *cpu_env,
4879 int shmid, abi_ulong shmaddr, int shmflg)
88a8c984
RV
4880{
4881 abi_long raddr;
4882 void *host_raddr;
4883 struct shmid_ds shm_info;
4884 int i,ret;
ee8e7614 4885 abi_ulong shmlba;
88a8c984
RV
4886
4887 /* find out the length of the shared memory segment */
4888 ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
4889 if (is_error(ret)) {
4890 /* can't get length, bail out */
4891 return ret;
4892 }
4893
ee8e7614
PM
4894 shmlba = target_shmlba(cpu_env);
4895
4896 if (shmaddr & (shmlba - 1)) {
4897 if (shmflg & SHM_RND) {
4898 shmaddr &= ~(shmlba - 1);
4899 } else {
4900 return -TARGET_EINVAL;
4901 }
4902 }
4903
88a8c984
RV
4904 mmap_lock();
4905
4906 if (shmaddr)
4907 host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
4908 else {
4909 abi_ulong mmap_start;
4910
4911 mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
4912
4913 if (mmap_start == -1) {
4914 errno = ENOMEM;
4915 host_raddr = (void *)-1;
4916 } else
4917 host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
4918 }
4919
4920 if (host_raddr == (void *)-1) {
4921 mmap_unlock();
4922 return get_errno((long)host_raddr);
4923 }
4924 raddr=h2g((unsigned long)host_raddr);
4925
4926 page_set_flags(raddr, raddr + shm_info.shm_segsz,
4927 PAGE_VALID | PAGE_READ |
4928 ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
4929
4930 for (i = 0; i < N_SHM_REGIONS; i++) {
b6e17875
PM
4931 if (!shm_regions[i].in_use) {
4932 shm_regions[i].in_use = true;
88a8c984
RV
4933 shm_regions[i].start = raddr;
4934 shm_regions[i].size = shm_info.shm_segsz;
4935 break;
4936 }
4937 }
4938
4939 mmap_unlock();
4940 return raddr;
4941
4942}
4943
4944static inline abi_long do_shmdt(abi_ulong shmaddr)
4945{
4946 int i;
4947
4948 for (i = 0; i < N_SHM_REGIONS; ++i) {
b6e17875
PM
4949 if (shm_regions[i].in_use && shm_regions[i].start == shmaddr) {
4950 shm_regions[i].in_use = false;
e00ac249 4951 page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
88a8c984
RV
4952 break;
4953 }
4954 }
4955
4956 return get_errno(shmdt(g2h(shmaddr)));
4957}
4958
1c54ff97 4959#ifdef TARGET_NR_ipc
53a5960a 4960/* ??? This only works with linear mappings. */
0da46a6e 4961/* do_ipc() must return target values and target errnos. */
ee8e7614
PM
4962static abi_long do_ipc(CPUArchState *cpu_env,
4963 unsigned int call, abi_long first,
37ed0956 4964 abi_long second, abi_long third,
992f48a0 4965 abi_long ptr, abi_long fifth)
8853f86e
FB
4966{
4967 int version;
992f48a0 4968 abi_long ret = 0;
8853f86e
FB
4969
4970 version = call >> 16;
4971 call &= 0xffff;
4972
4973 switch (call) {
fa294816 4974 case IPCOP_semop:
e5289087 4975 ret = do_semop(first, ptr, second);
fa294816
TS
4976 break;
4977
4978 case IPCOP_semget:
4979 ret = get_errno(semget(first, second, third));
4980 break;
4981
5d2fa8eb
TM
4982 case IPCOP_semctl: {
4983 /* The semun argument to semctl is passed by value, so dereference the
4984 * ptr argument. */
4985 abi_ulong atptr;
37ed0956 4986 get_user_ual(atptr, ptr);
d1c002b6 4987 ret = do_semctl(first, second, third, atptr);
fa294816 4988 break;
5d2fa8eb 4989 }
d96372ef 4990
1c54ff97
AJ
4991 case IPCOP_msgget:
4992 ret = get_errno(msgget(first, second));
4993 break;
d96372ef 4994
1c54ff97
AJ
4995 case IPCOP_msgsnd:
4996 ret = do_msgsnd(first, ptr, second, third);
4997 break;
d96372ef 4998
1c54ff97
AJ
4999 case IPCOP_msgctl:
5000 ret = do_msgctl(first, second, ptr);
5001 break;
d96372ef 5002
1c54ff97
AJ
5003 case IPCOP_msgrcv:
5004 switch (version) {
5005 case 0:
5006 {
5007 struct target_ipc_kludge {
5008 abi_long msgp;
5009 abi_long msgtyp;
5010 } *tmp;
5011
5012 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
5013 ret = -TARGET_EFAULT;
5014 break;
5015 }
d96372ef 5016
79dd77de 5017 ret = do_msgrcv(first, tswapal(tmp->msgp), second, tswapal(tmp->msgtyp), third);
d96372ef 5018
1c54ff97
AJ
5019 unlock_user_struct(tmp, ptr, 0);
5020 break;
5021 }
5022 default:
5023 ret = do_msgrcv(first, ptr, second, fifth, third);
5024 }
5025 break;
d96372ef 5026
8853f86e 5027 case IPCOP_shmat:
88a8c984
RV
5028 switch (version) {
5029 default:
5a4a898d
FB
5030 {
5031 abi_ulong raddr;
ee8e7614 5032 raddr = do_shmat(cpu_env, first, ptr, second);
88a8c984
RV
5033 if (is_error(raddr))
5034 return get_errno(raddr);
2f619698 5035 if (put_user_ual(raddr, third))
5a4a898d 5036 return -TARGET_EFAULT;
88a8c984
RV
5037 break;
5038 }
5039 case 1:
5040 ret = -TARGET_EINVAL;
5041 break;
5a4a898d 5042 }
8853f86e
FB
5043 break;
5044 case IPCOP_shmdt:
88a8c984 5045 ret = do_shmdt(ptr);
8853f86e
FB
5046 break;
5047
5048 case IPCOP_shmget:
5049 /* IPC_* flag values are the same on all linux platforms */
5050 ret = get_errno(shmget(first, second, third));
5051 break;
5052
5053 /* IPC_* and SHM_* command values are the same on all linux platforms */
5054 case IPCOP_shmctl:
a2926784 5055 ret = do_shmctl(first, second, ptr);
8853f86e
FB
5056 break;
5057 default:
32407103 5058 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
0da46a6e 5059 ret = -TARGET_ENOSYS;
8853f86e
FB
5060 break;
5061 }
5062 return ret;
5063}
32407103 5064#endif
8853f86e 5065
31e31b8a 5066/* kernel structure types definitions */
31e31b8a 5067
001faf32 5068#define STRUCT(name, ...) STRUCT_ ## name,
31e31b8a
FB
5069#define STRUCT_SPECIAL(name) STRUCT_ ## name,
5070enum {
5071#include "syscall_types.h"
8be656b8 5072STRUCT_MAX
31e31b8a
FB
5073};
5074#undef STRUCT
5075#undef STRUCT_SPECIAL
5076
001faf32 5077#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
31e31b8a
FB
5078#define STRUCT_SPECIAL(name)
5079#include "syscall_types.h"
5080#undef STRUCT
5081#undef STRUCT_SPECIAL
5082
d2ef05bb
PM
5083typedef struct IOCTLEntry IOCTLEntry;
5084
5085typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
45c874eb 5086 int fd, int cmd, abi_long arg);
d2ef05bb
PM
5087
5088struct IOCTLEntry {
9c6bf9c7 5089 int target_cmd;
2ab83ea7 5090 unsigned int host_cmd;
31e31b8a
FB
5091 const char *name;
5092 int access;
d2ef05bb 5093 do_ioctl_fn *do_ioctl;
1a9353d2 5094 const argtype arg_type[5];
d2ef05bb 5095};
31e31b8a
FB
5096
5097#define IOC_R 0x0001
5098#define IOC_W 0x0002
5099#define IOC_RW (IOC_R | IOC_W)
5100
5101#define MAX_STRUCT_SIZE 4096
5102
dace20dc 5103#ifdef CONFIG_FIEMAP
285da2b9
PM
5104/* So fiemap access checks don't overflow on 32 bit systems.
5105 * This is very slightly smaller than the limit imposed by
5106 * the underlying kernel.
5107 */
5108#define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap)) \
5109 / sizeof(struct fiemap_extent))
5110
5111static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
45c874eb 5112 int fd, int cmd, abi_long arg)
285da2b9
PM
5113{
5114 /* The parameter for this ioctl is a struct fiemap followed
5115 * by an array of struct fiemap_extent whose size is set
5116 * in fiemap->fm_extent_count. The array is filled in by the
5117 * ioctl.
5118 */
5119 int target_size_in, target_size_out;
5120 struct fiemap *fm;
5121 const argtype *arg_type = ie->arg_type;
5122 const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
5123 void *argptr, *p;
5124 abi_long ret;
5125 int i, extent_size = thunk_type_size(extent_arg_type, 0);
5126 uint32_t outbufsz;
5127 int free_fm = 0;
5128
5129 assert(arg_type[0] == TYPE_PTR);
5130 assert(ie->access == IOC_RW);
5131 arg_type++;
5132 target_size_in = thunk_type_size(arg_type, 0);
5133 argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
5134 if (!argptr) {
5135 return -TARGET_EFAULT;
5136 }
5137 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5138 unlock_user(argptr, arg, 0);
5139 fm = (struct fiemap *)buf_temp;
5140 if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
5141 return -TARGET_EINVAL;
5142 }
5143
5144 outbufsz = sizeof (*fm) +
5145 (sizeof(struct fiemap_extent) * fm->fm_extent_count);
5146
5147 if (outbufsz > MAX_STRUCT_SIZE) {
5148 /* We can't fit all the extents into the fixed size buffer.
5149 * Allocate one that is large enough and use it instead.
5150 */
0e173b24 5151 fm = g_try_malloc(outbufsz);
285da2b9
PM
5152 if (!fm) {
5153 return -TARGET_ENOMEM;
5154 }
5155 memcpy(fm, buf_temp, sizeof(struct fiemap));
5156 free_fm = 1;
5157 }
49ca6f3e 5158 ret = get_errno(safe_ioctl(fd, ie->host_cmd, fm));
285da2b9
PM
5159 if (!is_error(ret)) {
5160 target_size_out = target_size_in;
5161 /* An extent_count of 0 means we were only counting the extents
5162 * so there are no structs to copy
5163 */
5164 if (fm->fm_extent_count != 0) {
5165 target_size_out += fm->fm_mapped_extents * extent_size;
5166 }
5167 argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
5168 if (!argptr) {
5169 ret = -TARGET_EFAULT;
5170 } else {
5171 /* Convert the struct fiemap */
5172 thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
5173 if (fm->fm_extent_count != 0) {
5174 p = argptr + target_size_in;
5175 /* ...and then all the struct fiemap_extents */
5176 for (i = 0; i < fm->fm_mapped_extents; i++) {
5177 thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
5178 THUNK_TARGET);
5179 p += extent_size;
5180 }
5181 }
5182 unlock_user(argptr, arg, target_size_out);
5183 }
5184 }
5185 if (free_fm) {
0e173b24 5186 g_free(fm);
285da2b9
PM
5187 }
5188 return ret;
5189}
dace20dc 5190#endif
285da2b9 5191
059c2f2c 5192static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp,
45c874eb 5193 int fd, int cmd, abi_long arg)
059c2f2c
LV
5194{
5195 const argtype *arg_type = ie->arg_type;
5196 int target_size;
5197 void *argptr;
5198 int ret;
5199 struct ifconf *host_ifconf;
5200 uint32_t outbufsz;
5201 const argtype ifreq_arg_type[] = { MK_STRUCT(STRUCT_sockaddr_ifreq) };
5202 int target_ifreq_size;
5203 int nb_ifreq;
5204 int free_buf = 0;
5205 int i;
5206 int target_ifc_len;
5207 abi_long target_ifc_buf;
5208 int host_ifc_len;
5209 char *host_ifc_buf;
5210
5211 assert(arg_type[0] == TYPE_PTR);
5212 assert(ie->access == IOC_RW);
5213
5214 arg_type++;
5215 target_size = thunk_type_size(arg_type, 0);
5216
5217 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5218 if (!argptr)
5219 return -TARGET_EFAULT;
5220 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5221 unlock_user(argptr, arg, 0);
5222
5223 host_ifconf = (struct ifconf *)(unsigned long)buf_temp;
5224 target_ifc_len = host_ifconf->ifc_len;
5225 target_ifc_buf = (abi_long)(unsigned long)host_ifconf->ifc_buf;
5226
5227 target_ifreq_size = thunk_type_size(ifreq_arg_type, 0);
5228 nb_ifreq = target_ifc_len / target_ifreq_size;
5229 host_ifc_len = nb_ifreq * sizeof(struct ifreq);
5230
5231 outbufsz = sizeof(*host_ifconf) + host_ifc_len;
5232 if (outbufsz > MAX_STRUCT_SIZE) {
5233 /* We can't fit all the extents into the fixed size buffer.
5234 * Allocate one that is large enough and use it instead.
5235 */
5236 host_ifconf = malloc(outbufsz);
5237 if (!host_ifconf) {
5238 return -TARGET_ENOMEM;
5239 }
5240 memcpy(host_ifconf, buf_temp, sizeof(*host_ifconf));
5241 free_buf = 1;
5242 }
5243 host_ifc_buf = (char*)host_ifconf + sizeof(*host_ifconf);
5244
5245 host_ifconf->ifc_len = host_ifc_len;
5246 host_ifconf->ifc_buf = host_ifc_buf;
5247
49ca6f3e 5248 ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_ifconf));
059c2f2c
LV
5249 if (!is_error(ret)) {
5250 /* convert host ifc_len to target ifc_len */
5251
5252 nb_ifreq = host_ifconf->ifc_len / sizeof(struct ifreq);
5253 target_ifc_len = nb_ifreq * target_ifreq_size;
5254 host_ifconf->ifc_len = target_ifc_len;
5255
5256 /* restore target ifc_buf */
5257
5258 host_ifconf->ifc_buf = (char *)(unsigned long)target_ifc_buf;
5259
5260 /* copy struct ifconf to target user */
5261
5262 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5263 if (!argptr)
5264 return -TARGET_EFAULT;
5265 thunk_convert(argptr, host_ifconf, arg_type, THUNK_TARGET);
5266 unlock_user(argptr, arg, target_size);
5267
5268 /* copy ifreq[] to target user */
5269
5270 argptr = lock_user(VERIFY_WRITE, target_ifc_buf, target_ifc_len, 0);
5271 for (i = 0; i < nb_ifreq ; i++) {
5272 thunk_convert(argptr + i * target_ifreq_size,
5273 host_ifc_buf + i * sizeof(struct ifreq),
5274 ifreq_arg_type, THUNK_TARGET);
5275 }
5276 unlock_user(argptr, target_ifc_buf, target_ifc_len);
5277 }
5278
5279 if (free_buf) {
5280 free(host_ifconf);
5281 }
5282
5283 return ret;
5284}
5285
56e904ec 5286static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
45c874eb 5287 int cmd, abi_long arg)
56e904ec
AG
5288{
5289 void *argptr;
5290 struct dm_ioctl *host_dm;
5291 abi_long guest_data;
5292 uint32_t guest_data_size;
5293 int target_size;
5294 const argtype *arg_type = ie->arg_type;
5295 abi_long ret;
5296 void *big_buf = NULL;
5297 char *host_data;
5298
5299 arg_type++;
5300 target_size = thunk_type_size(arg_type, 0);
5301 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5302 if (!argptr) {
5303 ret = -TARGET_EFAULT;
5304 goto out;
5305 }
5306 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5307 unlock_user(argptr, arg, 0);
5308
5309 /* buf_temp is too small, so fetch things into a bigger buffer */
5310 big_buf = g_malloc0(((struct dm_ioctl*)buf_temp)->data_size * 2);
5311 memcpy(big_buf, buf_temp, target_size);
5312 buf_temp = big_buf;
5313 host_dm = big_buf;
5314
5315 guest_data = arg + host_dm->data_start;
5316 if ((guest_data - arg) < 0) {
f9757b1d 5317 ret = -TARGET_EINVAL;
56e904ec
AG
5318 goto out;
5319 }
5320 guest_data_size = host_dm->data_size - host_dm->data_start;
5321 host_data = (char*)host_dm + host_dm->data_start;
5322
5323 argptr = lock_user(VERIFY_READ, guest_data, guest_data_size, 1);
3211215e
PM
5324 if (!argptr) {
5325 ret = -TARGET_EFAULT;
5326 goto out;
5327 }
5328
56e904ec
AG
5329 switch (ie->host_cmd) {
5330 case DM_REMOVE_ALL:
5331 case DM_LIST_DEVICES:
5332 case DM_DEV_CREATE:
5333 case DM_DEV_REMOVE:
5334 case DM_DEV_SUSPEND:
5335 case DM_DEV_STATUS:
5336 case DM_DEV_WAIT:
5337 case DM_TABLE_STATUS:
5338 case DM_TABLE_CLEAR:
5339 case DM_TABLE_DEPS:
5340 case DM_LIST_VERSIONS:
5341 /* no input data */
5342 break;
5343 case DM_DEV_RENAME:
5344 case DM_DEV_SET_GEOMETRY:
5345 /* data contains only strings */
5346 memcpy(host_data, argptr, guest_data_size);
5347 break;
5348 case DM_TARGET_MSG:
5349 memcpy(host_data, argptr, guest_data_size);
5350 *(uint64_t*)host_data = tswap64(*(uint64_t*)argptr);
5351 break;
5352 case DM_TABLE_LOAD:
5353 {
5354 void *gspec = argptr;
5355 void *cur_data = host_data;
5356 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
5357 int spec_size = thunk_type_size(arg_type, 0);
5358 int i;
5359
5360 for (i = 0; i < host_dm->target_count; i++) {
5361 struct dm_target_spec *spec = cur_data;
5362 uint32_t next;
5363 int slen;
5364
5365 thunk_convert(spec, gspec, arg_type, THUNK_HOST);
5366 slen = strlen((char*)gspec + spec_size) + 1;
5367 next = spec->next;
5368 spec->next = sizeof(*spec) + slen;
5369 strcpy((char*)&spec[1], gspec + spec_size);
5370 gspec += next;
5371 cur_data += spec->next;
5372 }
5373 break;
5374 }
5375 default:
5376 ret = -TARGET_EINVAL;
dec0473d 5377 unlock_user(argptr, guest_data, 0);
56e904ec
AG
5378 goto out;
5379 }
5380 unlock_user(argptr, guest_data, 0);
5381
49ca6f3e 5382 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
56e904ec
AG
5383 if (!is_error(ret)) {
5384 guest_data = arg + host_dm->data_start;
5385 guest_data_size = host_dm->data_size - host_dm->data_start;
5386 argptr = lock_user(VERIFY_WRITE, guest_data, guest_data_size, 0);
5387 switch (ie->host_cmd) {
5388 case DM_REMOVE_ALL:
5389 case DM_DEV_CREATE:
5390 case DM_DEV_REMOVE:
5391 case DM_DEV_RENAME:
5392 case DM_DEV_SUSPEND:
5393 case DM_DEV_STATUS:
5394 case DM_TABLE_LOAD:
5395 case DM_TABLE_CLEAR:
5396 case DM_TARGET_MSG:
5397 case DM_DEV_SET_GEOMETRY:
5398 /* no return data */
5399 break;
5400 case DM_LIST_DEVICES:
5401 {
5402 struct dm_name_list *nl = (void*)host_dm + host_dm->data_start;
5403 uint32_t remaining_data = guest_data_size;
5404 void *cur_data = argptr;
5405 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_name_list) };
5406 int nl_size = 12; /* can't use thunk_size due to alignment */
5407
5408 while (1) {
5409 uint32_t next = nl->next;
5410 if (next) {
5411 nl->next = nl_size + (strlen(nl->name) + 1);
5412 }
5413 if (remaining_data < nl->next) {
5414 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5415 break;
5416 }
5417 thunk_convert(cur_data, nl, arg_type, THUNK_TARGET);
5418 strcpy(cur_data + nl_size, nl->name);
5419 cur_data += nl->next;
5420 remaining_data -= nl->next;
5421 if (!next) {
5422 break;
5423 }
5424 nl = (void*)nl + next;
5425 }
5426 break;
5427 }
5428 case DM_DEV_WAIT:
5429 case DM_TABLE_STATUS:
5430 {
5431 struct dm_target_spec *spec = (void*)host_dm + host_dm->data_start;
5432 void *cur_data = argptr;
5433 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_spec) };
5434 int spec_size = thunk_type_size(arg_type, 0);
5435 int i;
5436
5437 for (i = 0; i < host_dm->target_count; i++) {
5438 uint32_t next = spec->next;
5439 int slen = strlen((char*)&spec[1]) + 1;
5440 spec->next = (cur_data - argptr) + spec_size + slen;
5441 if (guest_data_size < spec->next) {
5442 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5443 break;
5444 }
5445 thunk_convert(cur_data, spec, arg_type, THUNK_TARGET);
5446 strcpy(cur_data + spec_size, (char*)&spec[1]);
5447 cur_data = argptr + spec->next;
5448 spec = (void*)host_dm + host_dm->data_start + next;
5449 }
5450 break;
5451 }
5452 case DM_TABLE_DEPS:
5453 {
5454 void *hdata = (void*)host_dm + host_dm->data_start;
5455 int count = *(uint32_t*)hdata;
5456 uint64_t *hdev = hdata + 8;
5457 uint64_t *gdev = argptr + 8;
5458 int i;
5459
5460 *(uint32_t*)argptr = tswap32(count);
5461 for (i = 0; i < count; i++) {
5462 *gdev = tswap64(*hdev);
5463 gdev++;
5464 hdev++;
5465 }
5466 break;
5467 }
5468 case DM_LIST_VERSIONS:
5469 {
5470 struct dm_target_versions *vers = (void*)host_dm + host_dm->data_start;
5471 uint32_t remaining_data = guest_data_size;
5472 void *cur_data = argptr;
5473 const argtype arg_type[] = { MK_STRUCT(STRUCT_dm_target_versions) };
5474 int vers_size = thunk_type_size(arg_type, 0);
5475
5476 while (1) {
5477 uint32_t next = vers->next;
5478 if (next) {
5479 vers->next = vers_size + (strlen(vers->name) + 1);
5480 }
5481 if (remaining_data < vers->next) {
5482 host_dm->flags |= DM_BUFFER_FULL_FLAG;
5483 break;
5484 }
5485 thunk_convert(cur_data, vers, arg_type, THUNK_TARGET);
5486 strcpy(cur_data + vers_size, vers->name);
5487 cur_data += vers->next;
5488 remaining_data -= vers->next;
5489 if (!next) {
5490 break;
5491 }
5492 vers = (void*)vers + next;
5493 }
5494 break;
5495 }
5496 default:
dec0473d 5497 unlock_user(argptr, guest_data, 0);
56e904ec
AG
5498 ret = -TARGET_EINVAL;
5499 goto out;
5500 }
5501 unlock_user(argptr, guest_data, guest_data_size);
5502
5503 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5504 if (!argptr) {
5505 ret = -TARGET_EFAULT;
5506 goto out;
5507 }
5508 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5509 unlock_user(argptr, arg, target_size);
5510 }
5511out:
ad11ad77 5512 g_free(big_buf);
56e904ec
AG
5513 return ret;
5514}
5515
a59b5e35 5516static abi_long do_ioctl_blkpg(const IOCTLEntry *ie, uint8_t *buf_temp, int fd,
45c874eb 5517 int cmd, abi_long arg)
a59b5e35
AG
5518{
5519 void *argptr;
5520 int target_size;
5521 const argtype *arg_type = ie->arg_type;
5522 const argtype part_arg_type[] = { MK_STRUCT(STRUCT_blkpg_partition) };
5523 abi_long ret;
5524
5525 struct blkpg_ioctl_arg *host_blkpg = (void*)buf_temp;
5526 struct blkpg_partition host_part;
5527
5528 /* Read and convert blkpg */
5529 arg_type++;
5530 target_size = thunk_type_size(arg_type, 0);
5531 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5532 if (!argptr) {
5533 ret = -TARGET_EFAULT;
5534 goto out;
5535 }
5536 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5537 unlock_user(argptr, arg, 0);
5538
5539 switch (host_blkpg->op) {
5540 case BLKPG_ADD_PARTITION:
5541 case BLKPG_DEL_PARTITION:
5542 /* payload is struct blkpg_partition */
5543 break;
5544 default:
5545 /* Unknown opcode */
5546 ret = -TARGET_EINVAL;
5547 goto out;
5548 }
5549
5550 /* Read and convert blkpg->data */
5551 arg = (abi_long)(uintptr_t)host_blkpg->data;
5552 target_size = thunk_type_size(part_arg_type, 0);
5553 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5554 if (!argptr) {
5555 ret = -TARGET_EFAULT;
5556 goto out;
5557 }
5558 thunk_convert(&host_part, argptr, part_arg_type, THUNK_HOST);
5559 unlock_user(argptr, arg, 0);
5560
5561 /* Swizzle the data pointer to our local copy and call! */
5562 host_blkpg->data = &host_part;
49ca6f3e 5563 ret = get_errno(safe_ioctl(fd, ie->host_cmd, host_blkpg));
a59b5e35
AG
5564
5565out:
5566 return ret;
5567}
5568
7ff7b666 5569static abi_long do_ioctl_rt(const IOCTLEntry *ie, uint8_t *buf_temp,
45c874eb 5570 int fd, int cmd, abi_long arg)
7ff7b666
LV
5571{
5572 const argtype *arg_type = ie->arg_type;
5573 const StructEntry *se;
5574 const argtype *field_types;
5575 const int *dst_offsets, *src_offsets;
5576 int target_size;
5577 void *argptr;
5578 abi_ulong *target_rt_dev_ptr;
5579 unsigned long *host_rt_dev_ptr;
5580 abi_long ret;
5581 int i;
5582
5583 assert(ie->access == IOC_W);
5584 assert(*arg_type == TYPE_PTR);
5585 arg_type++;
5586 assert(*arg_type == TYPE_STRUCT);
5587 target_size = thunk_type_size(arg_type, 0);
5588 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5589 if (!argptr) {
5590 return -TARGET_EFAULT;
5591 }
5592 arg_type++;
5593 assert(*arg_type == (int)STRUCT_rtentry);
5594 se = struct_entries + *arg_type++;
5595 assert(se->convert[0] == NULL);
5596 /* convert struct here to be able to catch rt_dev string */
5597 field_types = se->field_types;
5598 dst_offsets = se->field_offsets[THUNK_HOST];
5599 src_offsets = se->field_offsets[THUNK_TARGET];
5600 for (i = 0; i < se->nb_fields; i++) {
5601 if (dst_offsets[i] == offsetof(struct rtentry, rt_dev)) {
5602 assert(*field_types == TYPE_PTRVOID);
5603 target_rt_dev_ptr = (abi_ulong *)(argptr + src_offsets[i]);
5604 host_rt_dev_ptr = (unsigned long *)(buf_temp + dst_offsets[i]);
5605 if (*target_rt_dev_ptr != 0) {
5606 *host_rt_dev_ptr = (unsigned long)lock_user_string(
5607 tswapal(*target_rt_dev_ptr));
5608 if (!*host_rt_dev_ptr) {
5609 unlock_user(argptr, arg, 0);
5610 return -TARGET_EFAULT;
5611 }
5612 } else {
5613 *host_rt_dev_ptr = 0;
5614 }
5615 field_types++;
5616 continue;
5617 }
5618 field_types = thunk_convert(buf_temp + dst_offsets[i],
5619 argptr + src_offsets[i],
5620 field_types, THUNK_HOST);
5621 }
5622 unlock_user(argptr, arg, 0);
5623
49ca6f3e 5624 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
7ff7b666
LV
5625 if (*host_rt_dev_ptr != 0) {
5626 unlock_user((void *)*host_rt_dev_ptr,
5627 *target_rt_dev_ptr, 0);
5628 }
5629 return ret;
5630}
5631
ca56f5b5 5632static abi_long do_ioctl_kdsigaccept(const IOCTLEntry *ie, uint8_t *buf_temp,
45c874eb 5633 int fd, int cmd, abi_long arg)
ca56f5b5
PB
5634{
5635 int sig = target_to_host_signal(arg);
49ca6f3e 5636 return get_errno(safe_ioctl(fd, ie->host_cmd, sig));
ca56f5b5
PB
5637}
5638
2b74f621
AS
5639#ifdef TIOCGPTPEER
5640static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_temp,
5641 int fd, int cmd, abi_long arg)
5642{
5643 int flags = target_to_host_bitmask(arg, fcntl_flags_tbl);
5644 return get_errno(safe_ioctl(fd, ie->host_cmd, flags));
5645}
5646#endif
5647
9f106a75 5648static IOCTLEntry ioctl_entries[] = {
001faf32 5649#define IOCTL(cmd, access, ...) \
d2ef05bb
PM
5650 { TARGET_ ## cmd, cmd, #cmd, access, 0, { __VA_ARGS__ } },
5651#define IOCTL_SPECIAL(cmd, access, dofn, ...) \
5652 { TARGET_ ## cmd, cmd, #cmd, access, dofn, { __VA_ARGS__ } },
8cb762fe
HD
5653#define IOCTL_IGNORE(cmd) \
5654 { TARGET_ ## cmd, 0, #cmd },
31e31b8a
FB
5655#include "ioctls.h"
5656 { 0, 0, },
5657};
5658
53a5960a 5659/* ??? Implement proper locking for ioctls. */
0da46a6e 5660/* do_ioctl() Must return target values and target errnos. */
45c874eb 5661static abi_long do_ioctl(int fd, int cmd, abi_long arg)
31e31b8a
FB
5662{
5663 const IOCTLEntry *ie;
5664 const argtype *arg_type;
992f48a0 5665 abi_long ret;
31e31b8a 5666 uint8_t buf_temp[MAX_STRUCT_SIZE];
53a5960a
PB
5667 int target_size;
5668 void *argptr;
31e31b8a
FB
5669
5670 ie = ioctl_entries;
5671 for(;;) {
5672 if (ie->target_cmd == 0) {
32407103 5673 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
0da46a6e 5674 return -TARGET_ENOSYS;
31e31b8a
FB
5675 }
5676 if (ie->target_cmd == cmd)
5677 break;
5678 ie++;
5679 }
5680 arg_type = ie->arg_type;
9de5e440 5681#if defined(DEBUG)
32407103 5682 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
72f03900 5683#endif
d2ef05bb
PM
5684 if (ie->do_ioctl) {
5685 return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
8cb762fe
HD
5686 } else if (!ie->host_cmd) {
5687 /* Some architectures define BSD ioctls in their headers
5688 that are not implemented in Linux. */
5689 return -TARGET_ENOSYS;
d2ef05bb
PM
5690 }
5691
31e31b8a
FB
5692 switch(arg_type[0]) {
5693 case TYPE_NULL:
5694 /* no argument */
49ca6f3e 5695 ret = get_errno(safe_ioctl(fd, ie->host_cmd));
31e31b8a
FB
5696 break;
5697 case TYPE_PTRVOID:
5698 case TYPE_INT:
49ca6f3e 5699 ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
31e31b8a
FB
5700 break;
5701 case TYPE_PTR:
5702 arg_type++;
53a5960a 5703 target_size = thunk_type_size(arg_type, 0);
31e31b8a
FB
5704 switch(ie->access) {
5705 case IOC_R:
49ca6f3e 5706 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
31e31b8a 5707 if (!is_error(ret)) {
579a97f7
FB
5708 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5709 if (!argptr)
5710 return -TARGET_EFAULT;
53a5960a
PB
5711 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5712 unlock_user(argptr, arg, target_size);
31e31b8a
FB
5713 }
5714 break;
5715 case IOC_W:
579a97f7
FB
5716 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5717 if (!argptr)
5718 return -TARGET_EFAULT;
53a5960a
PB
5719 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5720 unlock_user(argptr, arg, 0);
49ca6f3e 5721 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
31e31b8a
FB
5722 break;
5723 default:
5724 case IOC_RW:
579a97f7
FB
5725 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
5726 if (!argptr)
5727 return -TARGET_EFAULT;
53a5960a
PB
5728 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
5729 unlock_user(argptr, arg, 0);
49ca6f3e 5730 ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
31e31b8a 5731 if (!is_error(ret)) {
579a97f7
FB
5732 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
5733 if (!argptr)
5734 return -TARGET_EFAULT;
53a5960a
PB
5735 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
5736 unlock_user(argptr, arg, target_size);
31e31b8a
FB
5737 }
5738 break;
5739 }
5740 break;
5741 default:
32407103
JM
5742 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
5743 (long)cmd, arg_type[0]);
0da46a6e 5744 ret = -TARGET_ENOSYS;
31e31b8a
FB
5745 break;
5746 }
5747 return ret;
5748}
5749
b39bc503 5750static const bitmask_transtbl iflag_tbl[] = {
31e31b8a
FB
5751 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
5752 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
5753 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
5754 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
5755 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
5756 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
5757 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
5758 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
5759 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
5760 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
5761 { TARGET_IXON, TARGET_IXON, IXON, IXON },
5762 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
5763 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
5764 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
5765 { 0, 0, 0, 0 }
5766};
5767
b39bc503 5768static const bitmask_transtbl oflag_tbl[] = {
31e31b8a
FB
5769 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
5770 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
5771 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
5772 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
5773 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
5774 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
5775 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
5776 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
5777 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
5778 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
5779 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
5780 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
5781 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
5782 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
5783 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
5784 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
5785 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
5786 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
5787 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
5788 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
5789 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
5790 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
5791 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
5792 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
5793 { 0, 0, 0, 0 }
5794};
5795
b39bc503 5796static const bitmask_transtbl cflag_tbl[] = {
31e31b8a
FB
5797 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
5798 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
5799 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
5800 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
5801 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
5802 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
5803 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
5804 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
5805 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
5806 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
5807 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
5808 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
5809 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
5810 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
5811 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
5812 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
5813 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
5814 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
5815 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
5816 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
5817 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
5818 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
5819 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
5820 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
5821 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
5822 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
5823 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
5824 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
5825 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
5826 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
5827 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
5828 { 0, 0, 0, 0 }
5829};
5830
b39bc503 5831static const bitmask_transtbl lflag_tbl[] = {
31e31b8a
FB
5832 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
5833 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
5834 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
5835 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
5836 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
5837 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
5838 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
5839 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
5840 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
5841 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
5842 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
5843 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
5844 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
5845 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
5846 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
5847 { 0, 0, 0, 0 }
5848};
5849
5850static void target_to_host_termios (void *dst, const void *src)
5851{
5852 struct host_termios *host = dst;
5853 const struct target_termios *target = src;
3b46e624 5854
5fafdf24 5855 host->c_iflag =
31e31b8a 5856 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
5fafdf24 5857 host->c_oflag =
31e31b8a 5858 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
5fafdf24 5859 host->c_cflag =
31e31b8a 5860 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
5fafdf24 5861 host->c_lflag =
31e31b8a
FB
5862 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
5863 host->c_line = target->c_line;
3b46e624 5864
44607123 5865 memset(host->c_cc, 0, sizeof(host->c_cc));
5fafdf24
TS
5866 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
5867 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
3b46e624 5868 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
5fafdf24 5869 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
3b46e624 5870 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
5fafdf24 5871 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
3b46e624 5872 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
5fafdf24 5873 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
3b46e624 5874 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
5fafdf24
TS
5875 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
5876 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
3b46e624
TS
5877 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
5878 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
5879 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
5880 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
5881 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
5fafdf24 5882 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
31e31b8a 5883}
3b46e624 5884
31e31b8a
FB
5885static void host_to_target_termios (void *dst, const void *src)
5886{
5887 struct target_termios *target = dst;
5888 const struct host_termios *host = src;
5889
5fafdf24 5890 target->c_iflag =
31e31b8a 5891 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
5fafdf24 5892 target->c_oflag =
31e31b8a 5893 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
5fafdf24 5894 target->c_cflag =
31e31b8a 5895 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
5fafdf24 5896 target->c_lflag =
31e31b8a
FB
5897 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
5898 target->c_line = host->c_line;
3b46e624 5899
44607123 5900 memset(target->c_cc, 0, sizeof(target->c_cc));
31e31b8a
FB
5901 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
5902 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
5903 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
5904 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
5905 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
5906 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
5907 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
5908 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
5909 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
5910 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
5911 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
5912 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
5913 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
5914 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
5915 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
5916 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
5917 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
5918}
5919
8e853dc7 5920static const StructEntry struct_termios_def = {
31e31b8a
FB
5921 .convert = { host_to_target_termios, target_to_host_termios },
5922 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
5923 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
5924};
5925
5286db75 5926static bitmask_transtbl mmap_flags_tbl[] = {
541e1690
HD
5927 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
5928 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
5929 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
5930 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS,
5931 MAP_ANONYMOUS, MAP_ANONYMOUS },
5932 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN,
5933 MAP_GROWSDOWN, MAP_GROWSDOWN },
5934 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE,
5935 MAP_DENYWRITE, MAP_DENYWRITE },
5936 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE,
5937 MAP_EXECUTABLE, MAP_EXECUTABLE },
5938 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
5939 { TARGET_MAP_NORESERVE, TARGET_MAP_NORESERVE,
5940 MAP_NORESERVE, MAP_NORESERVE },
5941 { TARGET_MAP_HUGETLB, TARGET_MAP_HUGETLB, MAP_HUGETLB, MAP_HUGETLB },
5942 /* MAP_STACK had been ignored by the kernel for quite some time.
5943 Recognize it for the target insofar as we do not want to pass
5944 it through to the host. */
5945 { TARGET_MAP_STACK, TARGET_MAP_STACK, 0, 0 },
5946 { 0, 0, 0, 0 }
5286db75
FB
5947};
5948
2ab83ea7 5949#if defined(TARGET_I386)
6dbad63e
FB
5950
5951/* NOTE: there is really one LDT for all the threads */
b1d8e52e 5952static uint8_t *ldt_table;
6dbad63e 5953
03acab66 5954static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
6dbad63e
FB
5955{
5956 int size;
53a5960a 5957 void *p;
6dbad63e
FB
5958
5959 if (!ldt_table)
5960 return 0;
5961 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
5962 if (size > bytecount)
5963 size = bytecount;
579a97f7
FB
5964 p = lock_user(VERIFY_WRITE, ptr, size, 0);
5965 if (!p)
03acab66 5966 return -TARGET_EFAULT;
579a97f7 5967 /* ??? Should this by byteswapped? */
53a5960a
PB
5968 memcpy(p, ldt_table, size);
5969 unlock_user(p, ptr, size);
6dbad63e
FB
5970 return size;
5971}
5972
5973/* XXX: add locking support */
03acab66
FB
5974static abi_long write_ldt(CPUX86State *env,
5975 abi_ulong ptr, unsigned long bytecount, int oldmode)
6dbad63e
FB
5976{
5977 struct target_modify_ldt_ldt_s ldt_info;
53a5960a 5978 struct target_modify_ldt_ldt_s *target_ldt_info;
6dbad63e 5979 int seg_32bit, contents, read_exec_only, limit_in_pages;
8d18e893 5980 int seg_not_present, useable, lm;
6dbad63e
FB
5981 uint32_t *lp, entry_1, entry_2;
5982
5983 if (bytecount != sizeof(ldt_info))
03acab66 5984 return -TARGET_EINVAL;
579a97f7 5985 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
03acab66 5986 return -TARGET_EFAULT;
53a5960a 5987 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
cbb21eed 5988 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
53a5960a
PB
5989 ldt_info.limit = tswap32(target_ldt_info->limit);
5990 ldt_info.flags = tswap32(target_ldt_info->flags);
5991 unlock_user_struct(target_ldt_info, ptr, 0);
3b46e624 5992
6dbad63e 5993 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
03acab66 5994 return -TARGET_EINVAL;
6dbad63e
FB
5995 seg_32bit = ldt_info.flags & 1;
5996 contents = (ldt_info.flags >> 1) & 3;
5997 read_exec_only = (ldt_info.flags >> 3) & 1;
5998 limit_in_pages = (ldt_info.flags >> 4) & 1;
5999 seg_not_present = (ldt_info.flags >> 5) & 1;
6000 useable = (ldt_info.flags >> 6) & 1;
8d18e893
FB
6001#ifdef TARGET_ABI32
6002 lm = 0;
6003#else
6004 lm = (ldt_info.flags >> 7) & 1;
6005#endif
6dbad63e
FB
6006 if (contents == 3) {
6007 if (oldmode)
03acab66 6008 return -TARGET_EINVAL;
6dbad63e 6009 if (seg_not_present == 0)
03acab66 6010 return -TARGET_EINVAL;
6dbad63e
FB
6011 }
6012 /* allocate the LDT */
6013 if (!ldt_table) {
e441570f
AZ
6014 env->ldt.base = target_mmap(0,
6015 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
6016 PROT_READ|PROT_WRITE,
6017 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
6018 if (env->ldt.base == -1)
03acab66 6019 return -TARGET_ENOMEM;
e441570f
AZ
6020 memset(g2h(env->ldt.base), 0,
6021 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
6dbad63e 6022 env->ldt.limit = 0xffff;
e441570f 6023 ldt_table = g2h(env->ldt.base);
6dbad63e
FB
6024 }
6025
6026 /* NOTE: same code as Linux kernel */
6027 /* Allow LDTs to be cleared by the user. */
6028 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
6029 if (oldmode ||
6030 (contents == 0 &&
6031 read_exec_only == 1 &&
6032 seg_32bit == 0 &&
6033 limit_in_pages == 0 &&
6034 seg_not_present == 1 &&
6035 useable == 0 )) {
6036 entry_1 = 0;
6037 entry_2 = 0;
6038 goto install;
6039 }
6040 }
3b46e624 6041
6dbad63e
FB
6042 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
6043 (ldt_info.limit & 0x0ffff);
6044 entry_2 = (ldt_info.base_addr & 0xff000000) |
6045 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
6046 (ldt_info.limit & 0xf0000) |
6047 ((read_exec_only ^ 1) << 9) |
6048 (contents << 10) |
6049 ((seg_not_present ^ 1) << 15) |
6050 (seg_32bit << 22) |
6051 (limit_in_pages << 23) |
8d18e893 6052 (lm << 21) |
6dbad63e
FB
6053 0x7000;
6054 if (!oldmode)
6055 entry_2 |= (useable << 20);
14ae3ba7 6056
6dbad63e
FB
6057 /* Install the new entry ... */
6058install:
6059 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
6060 lp[0] = tswap32(entry_1);
6061 lp[1] = tswap32(entry_2);
6062 return 0;
6063}
6064
6065/* specific and weird i386 syscalls */
8fcd3692
BS
6066static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
6067 unsigned long bytecount)
6dbad63e 6068{
03acab66 6069 abi_long ret;
3b46e624 6070
6dbad63e
FB
6071 switch (func) {
6072 case 0:
6073 ret = read_ldt(ptr, bytecount);
6074 break;
6075 case 1:
6076 ret = write_ldt(env, ptr, bytecount, 1);
6077 break;
6078 case 0x11:
6079 ret = write_ldt(env, ptr, bytecount, 0);
6080 break;
03acab66
FB
6081 default:
6082 ret = -TARGET_ENOSYS;
6083 break;
6dbad63e
FB
6084 }
6085 return ret;
6086}
1b6b029e 6087
4583f589 6088#if defined(TARGET_I386) && defined(TARGET_ABI32)
bc22eb44 6089abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
8d18e893
FB
6090{
6091 uint64_t *gdt_table = g2h(env->gdt.base);
6092 struct target_modify_ldt_ldt_s ldt_info;
6093 struct target_modify_ldt_ldt_s *target_ldt_info;
6094 int seg_32bit, contents, read_exec_only, limit_in_pages;
6095 int seg_not_present, useable, lm;
6096 uint32_t *lp, entry_1, entry_2;
6097 int i;
6098
6099 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
6100 if (!target_ldt_info)
6101 return -TARGET_EFAULT;
6102 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
cbb21eed 6103 ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
8d18e893
FB
6104 ldt_info.limit = tswap32(target_ldt_info->limit);
6105 ldt_info.flags = tswap32(target_ldt_info->flags);
6106 if (ldt_info.entry_number == -1) {
6107 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
6108 if (gdt_table[i] == 0) {
6109 ldt_info.entry_number = i;
6110 target_ldt_info->entry_number = tswap32(i);
6111 break;
6112 }
6113 }
6114 }
6115 unlock_user_struct(target_ldt_info, ptr, 1);
6116
6117 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
6118 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
6119 return -TARGET_EINVAL;
6120 seg_32bit = ldt_info.flags & 1;
6121 contents = (ldt_info.flags >> 1) & 3;
6122 read_exec_only = (ldt_info.flags >> 3) & 1;
6123 limit_in_pages = (ldt_info.flags >> 4) & 1;
6124 seg_not_present = (ldt_info.flags >> 5) & 1;
6125 useable = (ldt_info.flags >> 6) & 1;
6126#ifdef TARGET_ABI32
6127 lm = 0;
6128#else
6129 lm = (ldt_info.flags >> 7) & 1;
6130#endif
6131
6132 if (contents == 3) {
6133 if (seg_not_present == 0)
6134 return -TARGET_EINVAL;
6135 }
6136
6137 /* NOTE: same code as Linux kernel */
6138 /* Allow LDTs to be cleared by the user. */
6139 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
6140 if ((contents == 0 &&
6141 read_exec_only == 1 &&
6142 seg_32bit == 0 &&
6143 limit_in_pages == 0 &&
6144 seg_not_present == 1 &&
6145 useable == 0 )) {
6146 entry_1 = 0;
6147 entry_2 = 0;
6148 goto install;
6149 }
6150 }
6151
6152 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
6153 (ldt_info.limit & 0x0ffff);
6154 entry_2 = (ldt_info.base_addr & 0xff000000) |
6155 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
6156 (ldt_info.limit & 0xf0000) |
6157 ((read_exec_only ^ 1) << 9) |
6158 (contents << 10) |
6159 ((seg_not_present ^ 1) << 15) |
6160 (seg_32bit << 22) |
6161 (limit_in_pages << 23) |
6162 (useable << 20) |
6163 (lm << 21) |
6164 0x7000;
6165
6166 /* Install the new entry ... */
6167install:
6168 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
6169 lp[0] = tswap32(entry_1);
6170 lp[1] = tswap32(entry_2);
6171 return 0;
6172}
6173
8fcd3692 6174static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
8d18e893
FB
6175{
6176 struct target_modify_ldt_ldt_s *target_ldt_info;
6177 uint64_t *gdt_table = g2h(env->gdt.base);
6178 uint32_t base_addr, limit, flags;
6179 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
6180 int seg_not_present, useable, lm;
6181 uint32_t *lp, entry_1, entry_2;
6182
6183 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
6184 if (!target_ldt_info)
6185 return -TARGET_EFAULT;
6186 idx = tswap32(target_ldt_info->entry_number);
6187 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
6188 idx > TARGET_GDT_ENTRY_TLS_MAX) {
6189 unlock_user_struct(target_ldt_info, ptr, 1);
6190 return -TARGET_EINVAL;
6191 }
6192 lp = (uint32_t *)(gdt_table + idx);
6193 entry_1 = tswap32(lp[0]);
6194 entry_2 = tswap32(lp[1]);
6195
6196 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
6197 contents = (entry_2 >> 10) & 3;
6198 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
6199 seg_32bit = (entry_2 >> 22) & 1;
6200 limit_in_pages = (entry_2 >> 23) & 1;
6201 useable = (entry_2 >> 20) & 1;
6202#ifdef TARGET_ABI32
6203 lm = 0;
6204#else
6205 lm = (entry_2 >> 21) & 1;
6206#endif
6207 flags = (seg_32bit << 0) | (contents << 1) |
6208 (read_exec_only << 3) | (limit_in_pages << 4) |
6209 (seg_not_present << 5) | (useable << 6) | (lm << 7);
6210 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
6211 base_addr = (entry_1 >> 16) |
6212 (entry_2 & 0xff000000) |
6213 ((entry_2 & 0xff) << 16);
cbb21eed 6214 target_ldt_info->base_addr = tswapal(base_addr);
8d18e893
FB
6215 target_ldt_info->limit = tswap32(limit);
6216 target_ldt_info->flags = tswap32(flags);
6217 unlock_user_struct(target_ldt_info, ptr, 1);
6218 return 0;
6219}
4583f589 6220#endif /* TARGET_I386 && TARGET_ABI32 */
8d18e893 6221
d2fd1af7 6222#ifndef TARGET_ABI32
2667e71c 6223abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
d2fd1af7 6224{
1add8698 6225 abi_long ret = 0;
d2fd1af7
FB
6226 abi_ulong val;
6227 int idx;
1add8698 6228
d2fd1af7
FB
6229 switch(code) {
6230 case TARGET_ARCH_SET_GS:
6231 case TARGET_ARCH_SET_FS:
6232 if (code == TARGET_ARCH_SET_GS)
6233 idx = R_GS;
6234 else
6235 idx = R_FS;
6236 cpu_x86_load_seg(env, idx, 0);
6237 env->segs[idx].base = addr;
6238 break;
6239 case TARGET_ARCH_GET_GS:
6240 case TARGET_ARCH_GET_FS:
6241 if (code == TARGET_ARCH_GET_GS)
6242 idx = R_GS;
6243 else
6244 idx = R_FS;
6245 val = env->segs[idx].base;
6246 if (put_user(val, addr, abi_ulong))
1add8698 6247 ret = -TARGET_EFAULT;
d2fd1af7
FB
6248 break;
6249 default:
6250 ret = -TARGET_EINVAL;
6251 break;
6252 }
1add8698 6253 return ret;
d2fd1af7
FB
6254}
6255#endif
6256
2ab83ea7
FB
6257#endif /* defined(TARGET_I386) */
6258
05098a93 6259#define NEW_STACK_SIZE 0x40000
d865bab5 6260
d865bab5
PB
6261
6262static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
6263typedef struct {
9349b4f9 6264 CPUArchState *env;
d865bab5
PB
6265 pthread_mutex_t mutex;
6266 pthread_cond_t cond;
6267 pthread_t thread;
6268 uint32_t tid;
6269 abi_ulong child_tidptr;
6270 abi_ulong parent_tidptr;
6271 sigset_t sigmask;
6272} new_thread_info;
6273
6274static void *clone_func(void *arg)
6275{
6276 new_thread_info *info = arg;
9349b4f9 6277 CPUArchState *env;
0d34282f 6278 CPUState *cpu;
edf8e2af 6279 TaskState *ts;
d865bab5 6280
70903763 6281 rcu_register_thread();
3468b59e 6282 tcg_register_thread();
d865bab5 6283 env = info->env;
0d34282f 6284 cpu = ENV_GET_CPU(env);
a2247f8e 6285 thread_cpu = cpu;
0429a971 6286 ts = (TaskState *)cpu->opaque;
d865bab5 6287 info->tid = gettid();
edf8e2af 6288 task_settid(ts);
d865bab5
PB
6289 if (info->child_tidptr)
6290 put_user_u32(info->tid, info->child_tidptr);
6291 if (info->parent_tidptr)
6292 put_user_u32(info->tid, info->parent_tidptr);
6293 /* Enable signals. */
6294 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
6295 /* Signal to the parent that we're ready. */
6296 pthread_mutex_lock(&info->mutex);
6297 pthread_cond_broadcast(&info->cond);
6298 pthread_mutex_unlock(&info->mutex);
a4dd3d51 6299 /* Wait until the parent has finished initializing the tls state. */
d865bab5
PB
6300 pthread_mutex_lock(&clone_lock);
6301 pthread_mutex_unlock(&clone_lock);
6302 cpu_loop(env);
6303 /* never exits */
6304 return NULL;
6305}
1b6b029e 6306
0da46a6e
TS
6307/* do_fork() Must return host values and target errnos (unlike most
6308 do_*() functions). */
9349b4f9 6309static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
d865bab5
PB
6310 abi_ulong parent_tidptr, target_ulong newtls,
6311 abi_ulong child_tidptr)
1b6b029e 6312{
0429a971 6313 CPUState *cpu = ENV_GET_CPU(env);
1b6b029e 6314 int ret;
5cd4393b 6315 TaskState *ts;
0429a971 6316 CPUState *new_cpu;
9349b4f9 6317 CPUArchState *new_env;
d865bab5 6318 sigset_t sigmask;
3b46e624 6319
5ea2fc84
PM
6320 flags &= ~CLONE_IGNORED_FLAGS;
6321
436d124b
AZ
6322 /* Emulate vfork() with fork() */
6323 if (flags & CLONE_VFORK)
6324 flags &= ~(CLONE_VFORK | CLONE_VM);
6325
1b6b029e 6326 if (flags & CLONE_VM) {
0429a971 6327 TaskState *parent_ts = (TaskState *)cpu->opaque;
d865bab5
PB
6328 new_thread_info info;
6329 pthread_attr_t attr;
24cb36a6 6330
5ea2fc84
PM
6331 if (((flags & CLONE_THREAD_FLAGS) != CLONE_THREAD_FLAGS) ||
6332 (flags & CLONE_INVALID_THREAD_FLAGS)) {
6333 return -TARGET_EINVAL;
6334 }
6335
c78d65e8 6336 ts = g_new0(TaskState, 1);
624f7979 6337 init_task_state(ts);
1b6b029e 6338 /* we create a new CPU instance. */
c5be9f08 6339 new_env = cpu_copy(env);
6e68e076
PB
6340 /* Init regs that differ from the parent. */
6341 cpu_clone_regs(new_env, newsp);
0429a971
AF
6342 new_cpu = ENV_GET_CPU(new_env);
6343 new_cpu->opaque = ts;
edf8e2af
MW
6344 ts->bprm = parent_ts->bprm;
6345 ts->info = parent_ts->info;
3d3efba0 6346 ts->signal_mask = parent_ts->signal_mask;
d865bab5 6347
7cfbd386 6348 if (flags & CLONE_CHILD_CLEARTID) {
c2764719
PB
6349 ts->child_tidptr = child_tidptr;
6350 }
6351
7cfbd386 6352 if (flags & CLONE_SETTLS) {
d865bab5 6353 cpu_set_tls (new_env, newtls);
7cfbd386 6354 }
d865bab5
PB
6355
6356 /* Grab a mutex so that thread setup appears atomic. */
6357 pthread_mutex_lock(&clone_lock);
6358
6359 memset(&info, 0, sizeof(info));
6360 pthread_mutex_init(&info.mutex, NULL);
6361 pthread_mutex_lock(&info.mutex);
6362 pthread_cond_init(&info.cond, NULL);
6363 info.env = new_env;
7cfbd386 6364 if (flags & CLONE_CHILD_SETTID) {
d865bab5 6365 info.child_tidptr = child_tidptr;
7cfbd386
PM
6366 }
6367 if (flags & CLONE_PARENT_SETTID) {
d865bab5 6368 info.parent_tidptr = parent_tidptr;
7cfbd386 6369 }
d865bab5
PB
6370
6371 ret = pthread_attr_init(&attr);
48e15fc2
NF
6372 ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
6373 ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
d865bab5
PB
6374 /* It is not safe to deliver signals until the child has finished
6375 initializing, so temporarily block all signals. */
6376 sigfillset(&sigmask);
6377 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
6378
b67cb68b
AB
6379 /* If this is our first additional thread, we need to ensure we
6380 * generate code for parallel execution and flush old translations.
6381 */
6382 if (!parallel_cpus) {
6383 parallel_cpus = true;
6384 tb_flush(cpu);
6385 }
6386
d865bab5 6387 ret = pthread_create(&info.thread, &attr, clone_func, &info);
c2764719 6388 /* TODO: Free new CPU state if thread creation failed. */
d865bab5
PB
6389
6390 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
6391 pthread_attr_destroy(&attr);
6392 if (ret == 0) {
6393 /* Wait for the child to initialize. */
6394 pthread_cond_wait(&info.cond, &info.mutex);
6395 ret = info.tid;
d865bab5
PB
6396 } else {
6397 ret = -1;
6398 }
6399 pthread_mutex_unlock(&info.mutex);
6400 pthread_cond_destroy(&info.cond);
6401 pthread_mutex_destroy(&info.mutex);
6402 pthread_mutex_unlock(&clone_lock);
1b6b029e
FB
6403 } else {
6404 /* if no CLONE_VM, we consider it is a fork */
5ea2fc84
PM
6405 if (flags & CLONE_INVALID_FORK_FLAGS) {
6406 return -TARGET_EINVAL;
6407 }
6408
6409 /* We can't support custom termination signals */
6410 if ((flags & CSIGNAL) != TARGET_SIGCHLD) {
93b4eff8
TB
6411 return -TARGET_EINVAL;
6412 }
7d92d34e
TB
6413
6414 if (block_signals()) {
6415 return -TARGET_ERESTARTSYS;
6416 }
6417
d865bab5 6418 fork_start();
1b6b029e 6419 ret = fork();
d865bab5 6420 if (ret == 0) {
2b1319c8 6421 /* Child Process. */
d865bab5
PB
6422 cpu_clone_regs(env, newsp);
6423 fork_end(1);
2b1319c8
AJ
6424 /* There is a race condition here. The parent process could
6425 theoretically read the TID in the child process before the child
6426 tid is set. This would require using either ptrace
6427 (not implemented) or having *_tidptr to point at a shared memory
6428 mapping. We can't repeat the spinlock hack used above because
6429 the child process gets its own copy of the lock. */
d865bab5
PB
6430 if (flags & CLONE_CHILD_SETTID)
6431 put_user_u32(gettid(), child_tidptr);
6432 if (flags & CLONE_PARENT_SETTID)
6433 put_user_u32(gettid(), parent_tidptr);
0429a971 6434 ts = (TaskState *)cpu->opaque;
d865bab5
PB
6435 if (flags & CLONE_SETTLS)
6436 cpu_set_tls (env, newtls);
c2764719
PB
6437 if (flags & CLONE_CHILD_CLEARTID)
6438 ts->child_tidptr = child_tidptr;
d865bab5
PB
6439 } else {
6440 fork_end(0);
6441 }
1b6b029e
FB
6442 }
6443 return ret;
6444}
6445
5f106811
APR
6446/* warning : doesn't handle linux specific flags... */
6447static int target_to_host_fcntl_cmd(int cmd)
6448{
6449 switch(cmd) {
6450 case TARGET_F_DUPFD:
6451 case TARGET_F_GETFD:
6452 case TARGET_F_SETFD:
6453 case TARGET_F_GETFL:
6454 case TARGET_F_SETFL:
6455 return cmd;
6456 case TARGET_F_GETLK:
213d3e9e
PM
6457 return F_GETLK64;
6458 case TARGET_F_SETLK:
6459 return F_SETLK64;
6460 case TARGET_F_SETLKW:
6461 return F_SETLKW64;
5f106811
APR
6462 case TARGET_F_GETOWN:
6463 return F_GETOWN;
6464 case TARGET_F_SETOWN:
6465 return F_SETOWN;
6466 case TARGET_F_GETSIG:
6467 return F_GETSIG;
6468 case TARGET_F_SETSIG:
6469 return F_SETSIG;
6470#if TARGET_ABI_BITS == 32
6471 case TARGET_F_GETLK64:
6472 return F_GETLK64;
6473 case TARGET_F_SETLK64:
6474 return F_SETLK64;
6475 case TARGET_F_SETLKW64:
6476 return F_SETLKW64;
6477#endif
7e22e546
UH
6478 case TARGET_F_SETLEASE:
6479 return F_SETLEASE;
6480 case TARGET_F_GETLEASE:
6481 return F_GETLEASE;
fbd5de9b 6482#ifdef F_DUPFD_CLOEXEC
7e22e546
UH
6483 case TARGET_F_DUPFD_CLOEXEC:
6484 return F_DUPFD_CLOEXEC;
fbd5de9b 6485#endif
7e22e546
UH
6486 case TARGET_F_NOTIFY:
6487 return F_NOTIFY;
8d5d3004
AS
6488#ifdef F_GETOWN_EX
6489 case TARGET_F_GETOWN_EX:
6490 return F_GETOWN_EX;
6491#endif
6492#ifdef F_SETOWN_EX
6493 case TARGET_F_SETOWN_EX:
6494 return F_SETOWN_EX;
6495#endif
ddf31aa8 6496#ifdef F_SETPIPE_SZ
7e3b92ec
PM
6497 case TARGET_F_SETPIPE_SZ:
6498 return F_SETPIPE_SZ;
6499 case TARGET_F_GETPIPE_SZ:
6500 return F_GETPIPE_SZ;
ddf31aa8 6501#endif
5f106811
APR
6502 default:
6503 return -TARGET_EINVAL;
6504 }
6505 return -TARGET_EINVAL;
6506}
6507
2ba7f730
LV
6508#define TRANSTBL_CONVERT(a) { -1, TARGET_##a, -1, a }
6509static const bitmask_transtbl flock_tbl[] = {
6510 TRANSTBL_CONVERT(F_RDLCK),
6511 TRANSTBL_CONVERT(F_WRLCK),
6512 TRANSTBL_CONVERT(F_UNLCK),
6513 TRANSTBL_CONVERT(F_EXLCK),
6514 TRANSTBL_CONVERT(F_SHLCK),
6515 { 0, 0, 0, 0 }
6516};
6517
213d3e9e
PM
6518static inline abi_long copy_from_user_flock(struct flock64 *fl,
6519 abi_ulong target_flock_addr)
7775e9ec 6520{
53a5960a 6521 struct target_flock *target_fl;
213d3e9e
PM
6522 short l_type;
6523
6524 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6525 return -TARGET_EFAULT;
6526 }
6527
6528 __get_user(l_type, &target_fl->l_type);
6529 fl->l_type = target_to_host_bitmask(l_type, flock_tbl);
6530 __get_user(fl->l_whence, &target_fl->l_whence);
6531 __get_user(fl->l_start, &target_fl->l_start);
6532 __get_user(fl->l_len, &target_fl->l_len);
6533 __get_user(fl->l_pid, &target_fl->l_pid);
6534 unlock_user_struct(target_fl, target_flock_addr, 0);
6535 return 0;
6536}
6537
6538static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
6539 const struct flock64 *fl)
6540{
6541 struct target_flock *target_fl;
6542 short l_type;
6543
6544 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6545 return -TARGET_EFAULT;
6546 }
6547
6548 l_type = host_to_target_bitmask(fl->l_type, flock_tbl);
6549 __put_user(l_type, &target_fl->l_type);
6550 __put_user(fl->l_whence, &target_fl->l_whence);
6551 __put_user(fl->l_start, &target_fl->l_start);
6552 __put_user(fl->l_len, &target_fl->l_len);
6553 __put_user(fl->l_pid, &target_fl->l_pid);
6554 unlock_user_struct(target_fl, target_flock_addr, 1);
6555 return 0;
6556}
6557
6558typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr);
6559typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl);
6560
6561#if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
6562static inline abi_long copy_from_user_eabi_flock64(struct flock64 *fl,
6563 abi_ulong target_flock_addr)
6564{
6565 struct target_eabi_flock64 *target_fl;
6566 short l_type;
6567
6568 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6569 return -TARGET_EFAULT;
6570 }
6571
6572 __get_user(l_type, &target_fl->l_type);
6573 fl->l_type = target_to_host_bitmask(l_type, flock_tbl);
6574 __get_user(fl->l_whence, &target_fl->l_whence);
6575 __get_user(fl->l_start, &target_fl->l_start);
6576 __get_user(fl->l_len, &target_fl->l_len);
6577 __get_user(fl->l_pid, &target_fl->l_pid);
6578 unlock_user_struct(target_fl, target_flock_addr, 0);
6579 return 0;
6580}
6581
6582static inline abi_long copy_to_user_eabi_flock64(abi_ulong target_flock_addr,
6583 const struct flock64 *fl)
6584{
6585 struct target_eabi_flock64 *target_fl;
6586 short l_type;
6587
6588 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6589 return -TARGET_EFAULT;
6590 }
6591
6592 l_type = host_to_target_bitmask(fl->l_type, flock_tbl);
6593 __put_user(l_type, &target_fl->l_type);
6594 __put_user(fl->l_whence, &target_fl->l_whence);
6595 __put_user(fl->l_start, &target_fl->l_start);
6596 __put_user(fl->l_len, &target_fl->l_len);
6597 __put_user(fl->l_pid, &target_fl->l_pid);
6598 unlock_user_struct(target_fl, target_flock_addr, 1);
6599 return 0;
6600}
6601#endif
6602
6603static inline abi_long copy_from_user_flock64(struct flock64 *fl,
6604 abi_ulong target_flock_addr)
6605{
6606 struct target_flock64 *target_fl;
6607 short l_type;
6608
6609 if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
6610 return -TARGET_EFAULT;
6611 }
6612
6613 __get_user(l_type, &target_fl->l_type);
6614 fl->l_type = target_to_host_bitmask(l_type, flock_tbl);
6615 __get_user(fl->l_whence, &target_fl->l_whence);
6616 __get_user(fl->l_start, &target_fl->l_start);
6617 __get_user(fl->l_len, &target_fl->l_len);
6618 __get_user(fl->l_pid, &target_fl->l_pid);
6619 unlock_user_struct(target_fl, target_flock_addr, 0);
6620 return 0;
6621}
6622
6623static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr,
6624 const struct flock64 *fl)
6625{
6626 struct target_flock64 *target_fl;
6627 short l_type;
6628
6629 if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
6630 return -TARGET_EFAULT;
6631 }
6632
6633 l_type = host_to_target_bitmask(fl->l_type, flock_tbl);
6634 __put_user(l_type, &target_fl->l_type);
6635 __put_user(fl->l_whence, &target_fl->l_whence);
6636 __put_user(fl->l_start, &target_fl->l_start);
6637 __put_user(fl->l_len, &target_fl->l_len);
6638 __put_user(fl->l_pid, &target_fl->l_pid);
6639 unlock_user_struct(target_fl, target_flock_addr, 1);
6640 return 0;
6641}
6642
6643static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
6644{
43f238d7 6645 struct flock64 fl64;
8d5d3004
AS
6646#ifdef F_GETOWN_EX
6647 struct f_owner_ex fox;
6648 struct target_f_owner_ex *target_fox;
6649#endif
992f48a0 6650 abi_long ret;
5f106811
APR
6651 int host_cmd = target_to_host_fcntl_cmd(cmd);
6652
6653 if (host_cmd == -TARGET_EINVAL)
6654 return host_cmd;
53a5960a 6655
7775e9ec
FB
6656 switch(cmd) {
6657 case TARGET_F_GETLK:
213d3e9e
PM
6658 ret = copy_from_user_flock(&fl64, arg);
6659 if (ret) {
6660 return ret;
6661 }
435da5e7 6662 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
7775e9ec 6663 if (ret == 0) {
213d3e9e 6664 ret = copy_to_user_flock(arg, &fl64);
7775e9ec
FB
6665 }
6666 break;
3b46e624 6667
7775e9ec
FB
6668 case TARGET_F_SETLK:
6669 case TARGET_F_SETLKW:
213d3e9e
PM
6670 ret = copy_from_user_flock(&fl64, arg);
6671 if (ret) {
6672 return ret;
6673 }
435da5e7 6674 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
7775e9ec 6675 break;
3b46e624 6676
7775e9ec 6677 case TARGET_F_GETLK64:
213d3e9e
PM
6678 ret = copy_from_user_flock64(&fl64, arg);
6679 if (ret) {
6680 return ret;
6681 }
435da5e7 6682 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
43f238d7 6683 if (ret == 0) {
213d3e9e 6684 ret = copy_to_user_flock64(arg, &fl64);
43f238d7 6685 }
9ee1fa2c 6686 break;
7775e9ec
FB
6687 case TARGET_F_SETLK64:
6688 case TARGET_F_SETLKW64:
213d3e9e
PM
6689 ret = copy_from_user_flock64(&fl64, arg);
6690 if (ret) {
6691 return ret;
6692 }
435da5e7 6693 ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
7775e9ec
FB
6694 break;
6695
5f106811 6696 case TARGET_F_GETFL:
435da5e7 6697 ret = get_errno(safe_fcntl(fd, host_cmd, arg));
9ee1fa2c
FB
6698 if (ret >= 0) {
6699 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
6700 }
ffa65c3b
FB
6701 break;
6702
5f106811 6703 case TARGET_F_SETFL:
435da5e7
PM
6704 ret = get_errno(safe_fcntl(fd, host_cmd,
6705 target_to_host_bitmask(arg,
6706 fcntl_flags_tbl)));
5f106811
APR
6707 break;
6708
8d5d3004
AS
6709#ifdef F_GETOWN_EX
6710 case TARGET_F_GETOWN_EX:
435da5e7 6711 ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
8d5d3004
AS
6712 if (ret >= 0) {
6713 if (!lock_user_struct(VERIFY_WRITE, target_fox, arg, 0))
6714 return -TARGET_EFAULT;
6715 target_fox->type = tswap32(fox.type);
6716 target_fox->pid = tswap32(fox.pid);
6717 unlock_user_struct(target_fox, arg, 1);
6718 }
6719 break;
6720#endif
6721
6722#ifdef F_SETOWN_EX
6723 case TARGET_F_SETOWN_EX:
6724 if (!lock_user_struct(VERIFY_READ, target_fox, arg, 1))
6725 return -TARGET_EFAULT;
6726 fox.type = tswap32(target_fox->type);
6727 fox.pid = tswap32(target_fox->pid);
6728 unlock_user_struct(target_fox, arg, 0);
435da5e7 6729 ret = get_errno(safe_fcntl(fd, host_cmd, &fox));
8d5d3004
AS
6730 break;
6731#endif
6732
5f106811
APR
6733 case TARGET_F_SETOWN:
6734 case TARGET_F_GETOWN:
6735 case TARGET_F_SETSIG:
6736 case TARGET_F_GETSIG:
7e22e546
UH
6737 case TARGET_F_SETLEASE:
6738 case TARGET_F_GETLEASE:
7e3b92ec
PM
6739 case TARGET_F_SETPIPE_SZ:
6740 case TARGET_F_GETPIPE_SZ:
435da5e7 6741 ret = get_errno(safe_fcntl(fd, host_cmd, arg));
ffa65c3b
FB
6742 break;
6743
7775e9ec 6744 default:
435da5e7 6745 ret = get_errno(safe_fcntl(fd, cmd, arg));
7775e9ec
FB
6746 break;
6747 }
6748 return ret;
6749}
6750
67867308 6751#ifdef USE_UID16
7775e9ec 6752
67867308
FB
6753static inline int high2lowuid(int uid)
6754{
6755 if (uid > 65535)
6756 return 65534;
6757 else
6758 return uid;
6759}
6760
6761static inline int high2lowgid(int gid)
6762{
6763 if (gid > 65535)
6764 return 65534;
6765 else
6766 return gid;
6767}
6768
6769static inline int low2highuid(int uid)
6770{
6771 if ((int16_t)uid == -1)
6772 return -1;
6773 else
6774 return uid;
6775}
6776
6777static inline int low2highgid(int gid)
6778{
6779 if ((int16_t)gid == -1)
6780 return -1;
6781 else
6782 return gid;
6783}
0c866a7e
RV
6784static inline int tswapid(int id)
6785{
6786 return tswap16(id);
6787}
76ca310a
PM
6788
6789#define put_user_id(x, gaddr) put_user_u16(x, gaddr)
6790
0c866a7e
RV
6791#else /* !USE_UID16 */
6792static inline int high2lowuid(int uid)
6793{
6794 return uid;
6795}
6796static inline int high2lowgid(int gid)
6797{
6798 return gid;
6799}
6800static inline int low2highuid(int uid)
6801{
6802 return uid;
6803}
6804static inline int low2highgid(int gid)
6805{
6806 return gid;
6807}
6808static inline int tswapid(int id)
6809{
6810 return tswap32(id);
6811}
76ca310a
PM
6812
6813#define put_user_id(x, gaddr) put_user_u32(x, gaddr)
6814
67867308 6815#endif /* USE_UID16 */
1b6b029e 6816
fd6f7798
PM
6817/* We must do direct syscalls for setting UID/GID, because we want to
6818 * implement the Linux system call semantics of "change only for this thread",
6819 * not the libc/POSIX semantics of "change for all threads in process".
6820 * (See http://ewontfix.com/17/ for more details.)
6821 * We use the 32-bit version of the syscalls if present; if it is not
6822 * then either the host architecture supports 32-bit UIDs natively with
6823 * the standard syscall, or the 16-bit UID is the best we can do.
6824 */
6825#ifdef __NR_setuid32
6826#define __NR_sys_setuid __NR_setuid32
6827#else
6828#define __NR_sys_setuid __NR_setuid
6829#endif
6830#ifdef __NR_setgid32
6831#define __NR_sys_setgid __NR_setgid32
6832#else
6833#define __NR_sys_setgid __NR_setgid
6834#endif
6835#ifdef __NR_setresuid32
6836#define __NR_sys_setresuid __NR_setresuid32
6837#else
6838#define __NR_sys_setresuid __NR_setresuid
6839#endif
6840#ifdef __NR_setresgid32
6841#define __NR_sys_setresgid __NR_setresgid32
6842#else
6843#define __NR_sys_setresgid __NR_setresgid
6844#endif
6845
6846_syscall1(int, sys_setuid, uid_t, uid)
6847_syscall1(int, sys_setgid, gid_t, gid)
6848_syscall3(int, sys_setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
6849_syscall3(int, sys_setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
6850
31e31b8a
FB
6851void syscall_init(void)
6852{
2ab83ea7
FB
6853 IOCTLEntry *ie;
6854 const argtype *arg_type;
6855 int size;
b92c47c1 6856 int i;
2ab83ea7 6857
8be656b8
AG
6858 thunk_init(STRUCT_MAX);
6859
001faf32 6860#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
5fafdf24 6861#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
31e31b8a
FB
6862#include "syscall_types.h"
6863#undef STRUCT
6864#undef STRUCT_SPECIAL
2ab83ea7 6865
dd6e957a
PM
6866 /* Build target_to_host_errno_table[] table from
6867 * host_to_target_errno_table[]. */
6868 for (i = 0; i < ERRNO_TABLE_SIZE; i++) {
6869 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
6870 }
6871
2ab83ea7
FB
6872 /* we patch the ioctl size if necessary. We rely on the fact that
6873 no ioctl has all the bits at '1' in the size field */
6874 ie = ioctl_entries;
6875 while (ie->target_cmd != 0) {
6876 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
6877 TARGET_IOC_SIZEMASK) {
6878 arg_type = ie->arg_type;
6879 if (arg_type[0] != TYPE_PTR) {
5fafdf24 6880 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
2ab83ea7
FB
6881 ie->target_cmd);
6882 exit(1);
6883 }
6884 arg_type++;
6885 size = thunk_type_size(arg_type, 0);
5fafdf24 6886 ie->target_cmd = (ie->target_cmd &
2ab83ea7
FB
6887 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
6888 (size << TARGET_IOC_SIZESHIFT);
6889 }
b92c47c1 6890
2ab83ea7 6891 /* automatic consistency check if same arch */
872ea0c0
AZ
6892#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
6893 (defined(__x86_64__) && defined(TARGET_X86_64))
6894 if (unlikely(ie->target_cmd != ie->host_cmd)) {
6895 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
6896 ie->name, ie->target_cmd, ie->host_cmd);
2ab83ea7
FB
6897 }
6898#endif
6899 ie++;
6900 }
31e31b8a 6901}
c573ff67 6902
992f48a0 6903#if TARGET_ABI_BITS == 32
ce4defa0
PB
6904static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
6905{
af325d36 6906#ifdef TARGET_WORDS_BIGENDIAN
ce4defa0
PB
6907 return ((uint64_t)word0 << 32) | word1;
6908#else
6909 return ((uint64_t)word1 << 32) | word0;
6910#endif
6911}
992f48a0 6912#else /* TARGET_ABI_BITS == 32 */
32407103
JM
6913static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
6914{
6915 return word0;
6916}
992f48a0 6917#endif /* TARGET_ABI_BITS != 32 */
ce4defa0
PB
6918
6919#ifdef TARGET_NR_truncate64
992f48a0
BS
6920static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
6921 abi_long arg2,
6922 abi_long arg3,
6923 abi_long arg4)
ce4defa0 6924{
8bf8e9df 6925 if (regpairs_aligned(cpu_env, TARGET_NR_truncate64)) {
ce4defa0
PB
6926 arg2 = arg3;
6927 arg3 = arg4;
48e515d4 6928 }
ce4defa0
PB
6929 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
6930}
6931#endif
6932
6933#ifdef TARGET_NR_ftruncate64
992f48a0
BS
6934static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
6935 abi_long arg2,
6936 abi_long arg3,
6937 abi_long arg4)
ce4defa0 6938{
8bf8e9df 6939 if (regpairs_aligned(cpu_env, TARGET_NR_ftruncate64)) {
ce4defa0
PB
6940 arg2 = arg3;
6941 arg3 = arg4;
48e515d4 6942 }
ce4defa0
PB
6943 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
6944}
6945#endif
6946
579a97f7
FB
6947static inline abi_long target_to_host_timespec(struct timespec *host_ts,
6948 abi_ulong target_addr)
53a5960a
PB
6949{
6950 struct target_timespec *target_ts;
6951
579a97f7
FB
6952 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
6953 return -TARGET_EFAULT;
c7e35da3
PM
6954 __get_user(host_ts->tv_sec, &target_ts->tv_sec);
6955 __get_user(host_ts->tv_nsec, &target_ts->tv_nsec);
53a5960a 6956 unlock_user_struct(target_ts, target_addr, 0);
b255bfa8 6957 return 0;
53a5960a
PB
6958}
6959
579a97f7
FB
6960static inline abi_long host_to_target_timespec(abi_ulong target_addr,
6961 struct timespec *host_ts)
53a5960a
PB
6962{
6963 struct target_timespec *target_ts;
6964
579a97f7
FB
6965 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
6966 return -TARGET_EFAULT;
c7e35da3
PM
6967 __put_user(host_ts->tv_sec, &target_ts->tv_sec);
6968 __put_user(host_ts->tv_nsec, &target_ts->tv_nsec);
53a5960a 6969 unlock_user_struct(target_ts, target_addr, 1);
b255bfa8 6970 return 0;
53a5960a
PB
6971}
6972
f4f1e10a
ECL
6973static inline abi_long target_to_host_itimerspec(struct itimerspec *host_itspec,
6974 abi_ulong target_addr)
6975{
6976 struct target_itimerspec *target_itspec;
6977
6978 if (!lock_user_struct(VERIFY_READ, target_itspec, target_addr, 1)) {
6979 return -TARGET_EFAULT;
6980 }
6981
6982 host_itspec->it_interval.tv_sec =
6983 tswapal(target_itspec->it_interval.tv_sec);
6984 host_itspec->it_interval.tv_nsec =
6985 tswapal(target_itspec->it_interval.tv_nsec);
6986 host_itspec->it_value.tv_sec = tswapal(target_itspec->it_value.tv_sec);
6987 host_itspec->it_value.tv_nsec = tswapal(target_itspec->it_value.tv_nsec);
6988
6989 unlock_user_struct(target_itspec, target_addr, 1);
6990 return 0;
6991}
6992
6993static inline abi_long host_to_target_itimerspec(abi_ulong target_addr,
6994 struct itimerspec *host_its)
6995{
6996 struct target_itimerspec *target_itspec;
6997
6998 if (!lock_user_struct(VERIFY_WRITE, target_itspec, target_addr, 0)) {
6999 return -TARGET_EFAULT;
7000 }
7001
7002 target_itspec->it_interval.tv_sec = tswapal(host_its->it_interval.tv_sec);
7003 target_itspec->it_interval.tv_nsec = tswapal(host_its->it_interval.tv_nsec);
7004
7005 target_itspec->it_value.tv_sec = tswapal(host_its->it_value.tv_sec);
7006 target_itspec->it_value.tv_nsec = tswapal(host_its->it_value.tv_nsec);
7007
7008 unlock_user_struct(target_itspec, target_addr, 0);
7009 return 0;
7010}
7011
19f59bce
AM
7012static inline abi_long target_to_host_timex(struct timex *host_tx,
7013 abi_long target_addr)
7014{
7015 struct target_timex *target_tx;
7016
7017 if (!lock_user_struct(VERIFY_READ, target_tx, target_addr, 1)) {
7018 return -TARGET_EFAULT;
7019 }
7020
7021 __get_user(host_tx->modes, &target_tx->modes);
7022 __get_user(host_tx->offset, &target_tx->offset);
7023 __get_user(host_tx->freq, &target_tx->freq);
7024 __get_user(host_tx->maxerror, &target_tx->maxerror);
7025 __get_user(host_tx->esterror, &target_tx->esterror);
7026 __get_user(host_tx->status, &target_tx->status);
7027 __get_user(host_tx->constant, &target_tx->constant);
7028 __get_user(host_tx->precision, &target_tx->precision);
7029 __get_user(host_tx->tolerance, &target_tx->tolerance);
7030 __get_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
7031 __get_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
7032 __get_user(host_tx->tick, &target_tx->tick);
7033 __get_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7034 __get_user(host_tx->jitter, &target_tx->jitter);
7035 __get_user(host_tx->shift, &target_tx->shift);
7036 __get_user(host_tx->stabil, &target_tx->stabil);
7037 __get_user(host_tx->jitcnt, &target_tx->jitcnt);
7038 __get_user(host_tx->calcnt, &target_tx->calcnt);
7039 __get_user(host_tx->errcnt, &target_tx->errcnt);
7040 __get_user(host_tx->stbcnt, &target_tx->stbcnt);
7041 __get_user(host_tx->tai, &target_tx->tai);
7042
7043 unlock_user_struct(target_tx, target_addr, 0);
7044 return 0;
7045}
7046
7047static inline abi_long host_to_target_timex(abi_long target_addr,
7048 struct timex *host_tx)
7049{
7050 struct target_timex *target_tx;
7051
7052 if (!lock_user_struct(VERIFY_WRITE, target_tx, target_addr, 0)) {
7053 return -TARGET_EFAULT;
7054 }
7055
7056 __put_user(host_tx->modes, &target_tx->modes);
7057 __put_user(host_tx->offset, &target_tx->offset);
7058 __put_user(host_tx->freq, &target_tx->freq);
7059 __put_user(host_tx->maxerror, &target_tx->maxerror);
7060 __put_user(host_tx->esterror, &target_tx->esterror);
7061 __put_user(host_tx->status, &target_tx->status);
7062 __put_user(host_tx->constant, &target_tx->constant);
7063 __put_user(host_tx->precision, &target_tx->precision);
7064 __put_user(host_tx->tolerance, &target_tx->tolerance);
7065 __put_user(host_tx->time.tv_sec, &target_tx->time.tv_sec);
7066 __put_user(host_tx->time.tv_usec, &target_tx->time.tv_usec);
7067 __put_user(host_tx->tick, &target_tx->tick);
7068 __put_user(host_tx->ppsfreq, &target_tx->ppsfreq);
7069 __put_user(host_tx->jitter, &target_tx->jitter);
7070 __put_user(host_tx->shift, &target_tx->shift);
7071 __put_user(host_tx->stabil, &target_tx->stabil);
7072 __put_user(host_tx->jitcnt, &target_tx->jitcnt);
7073 __put_user(host_tx->calcnt, &target_tx->calcnt);
7074 __put_user(host_tx->errcnt, &target_tx->errcnt);
7075 __put_user(host_tx->stbcnt, &target_tx->stbcnt);
7076 __put_user(host_tx->tai, &target_tx->tai);
7077
7078 unlock_user_struct(target_tx, target_addr, 1);
7079 return 0;
7080}
7081
7082
c065976f
PM
7083static inline abi_long target_to_host_sigevent(struct sigevent *host_sevp,
7084 abi_ulong target_addr)
7085{
7086 struct target_sigevent *target_sevp;
7087
7088 if (!lock_user_struct(VERIFY_READ, target_sevp, target_addr, 1)) {
7089 return -TARGET_EFAULT;
7090 }
7091
7092 /* This union is awkward on 64 bit systems because it has a 32 bit
7093 * integer and a pointer in it; we follow the conversion approach
7094 * used for handling sigval types in signal.c so the guest should get
7095 * the correct value back even if we did a 64 bit byteswap and it's
7096 * using the 32 bit integer.
7097 */
7098 host_sevp->sigev_value.sival_ptr =
7099 (void *)(uintptr_t)tswapal(target_sevp->sigev_value.sival_ptr);
7100 host_sevp->sigev_signo =
7101 target_to_host_signal(tswap32(target_sevp->sigev_signo));
7102 host_sevp->sigev_notify = tswap32(target_sevp->sigev_notify);
7103 host_sevp->_sigev_un._tid = tswap32(target_sevp->_sigev_un._tid);
7104
7105 unlock_user_struct(target_sevp, target_addr, 1);
7106 return 0;
7107}
7108
6f6a4032
TM
7109#if defined(TARGET_NR_mlockall)
7110static inline int target_to_host_mlockall_arg(int arg)
7111{
7112 int result = 0;
7113
7114 if (arg & TARGET_MLOCKALL_MCL_CURRENT) {
7115 result |= MCL_CURRENT;
7116 }
7117 if (arg & TARGET_MLOCKALL_MCL_FUTURE) {
7118 result |= MCL_FUTURE;
7119 }
7120 return result;
7121}
7122#endif
7123
6a24a778
AZ
7124static inline abi_long host_to_target_stat64(void *cpu_env,
7125 abi_ulong target_addr,
7126 struct stat *host_st)
7127{
09701199 7128#if defined(TARGET_ARM) && defined(TARGET_ABI32)
6a24a778
AZ
7129 if (((CPUARMState *)cpu_env)->eabi) {
7130 struct target_eabi_stat64 *target_st;
7131
7132 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
7133 return -TARGET_EFAULT;
7134 memset(target_st, 0, sizeof(struct target_eabi_stat64));
7135 __put_user(host_st->st_dev, &target_st->st_dev);
7136 __put_user(host_st->st_ino, &target_st->st_ino);
7137#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7138 __put_user(host_st->st_ino, &target_st->__st_ino);
7139#endif
7140 __put_user(host_st->st_mode, &target_st->st_mode);
7141 __put_user(host_st->st_nlink, &target_st->st_nlink);
7142 __put_user(host_st->st_uid, &target_st->st_uid);
7143 __put_user(host_st->st_gid, &target_st->st_gid);
7144 __put_user(host_st->st_rdev, &target_st->st_rdev);
7145 __put_user(host_st->st_size, &target_st->st_size);
7146 __put_user(host_st->st_blksize, &target_st->st_blksize);
7147 __put_user(host_st->st_blocks, &target_st->st_blocks);
7148 __put_user(host_st->st_atime, &target_st->target_st_atime);
7149 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
7150 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
7151 unlock_user_struct(target_st, target_addr, 1);
7152 } else
7153#endif
7154 {
20d155bc 7155#if defined(TARGET_HAS_STRUCT_STAT64)
6a24a778 7156 struct target_stat64 *target_st;
20d155bc
SW
7157#else
7158 struct target_stat *target_st;
9d33b76b 7159#endif
6a24a778
AZ
7160
7161 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
7162 return -TARGET_EFAULT;
9d33b76b 7163 memset(target_st, 0, sizeof(*target_st));
6a24a778
AZ
7164 __put_user(host_st->st_dev, &target_st->st_dev);
7165 __put_user(host_st->st_ino, &target_st->st_ino);
7166#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
7167 __put_user(host_st->st_ino, &target_st->__st_ino);
7168#endif
7169 __put_user(host_st->st_mode, &target_st->st_mode);
7170 __put_user(host_st->st_nlink, &target_st->st_nlink);
7171 __put_user(host_st->st_uid, &target_st->st_uid);
7172 __put_user(host_st->st_gid, &target_st->st_gid);
7173 __put_user(host_st->st_rdev, &target_st->st_rdev);
7174 /* XXX: better use of kernel struct */
7175 __put_user(host_st->st_size, &target_st->st_size);
7176 __put_user(host_st->st_blksize, &target_st->st_blksize);
7177 __put_user(host_st->st_blocks, &target_st->st_blocks);
7178 __put_user(host_st->st_atime, &target_st->target_st_atime);
7179 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
7180 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
7181 unlock_user_struct(target_st, target_addr, 1);
7182 }
7183
7184 return 0;
7185}
6a24a778 7186
bd0c5661
PB
7187/* ??? Using host futex calls even when target atomic operations
7188 are not really atomic probably breaks things. However implementing
7189 futexes locally would make futexes shared between multiple processes
7190 tricky. However they're probably useless because guest atomic
7191 operations won't work either. */
8fcd3692
BS
7192static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
7193 target_ulong uaddr2, int val3)
bd0c5661
PB
7194{
7195 struct timespec ts, *pts;
a16aae0c 7196 int base_op;
bd0c5661
PB
7197
7198 /* ??? We assume FUTEX_* constants are the same on both host
7199 and target. */
a29ccd63 7200#ifdef FUTEX_CMD_MASK
a16aae0c 7201 base_op = op & FUTEX_CMD_MASK;
a29ccd63 7202#else
a16aae0c 7203 base_op = op;
a29ccd63 7204#endif
a16aae0c 7205 switch (base_op) {
bd0c5661 7206 case FUTEX_WAIT:
cce246e0 7207 case FUTEX_WAIT_BITSET:
bd0c5661
PB
7208 if (timeout) {
7209 pts = &ts;
7210 target_to_host_timespec(pts, timeout);
7211 } else {
7212 pts = NULL;
7213 }
d509eeb1 7214 return get_errno(safe_futex(g2h(uaddr), op, tswap32(val),
cce246e0 7215 pts, NULL, val3));
bd0c5661 7216 case FUTEX_WAKE:
d509eeb1 7217 return get_errno(safe_futex(g2h(uaddr), op, val, NULL, NULL, 0));
bd0c5661 7218 case FUTEX_FD:
d509eeb1 7219 return get_errno(safe_futex(g2h(uaddr), op, val, NULL, NULL, 0));
bd0c5661 7220 case FUTEX_REQUEUE:
bd0c5661 7221 case FUTEX_CMP_REQUEUE:
a16aae0c
NF
7222 case FUTEX_WAKE_OP:
7223 /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
7224 TIMEOUT parameter is interpreted as a uint32_t by the kernel.
7225 But the prototype takes a `struct timespec *'; insert casts
7226 to satisfy the compiler. We do not need to tswap TIMEOUT
7227 since it's not compared to guest memory. */
7228 pts = (struct timespec *)(uintptr_t) timeout;
d509eeb1
PM
7229 return get_errno(safe_futex(g2h(uaddr), op, val, pts,
7230 g2h(uaddr2),
7231 (base_op == FUTEX_CMP_REQUEUE
7232 ? tswap32(val3)
7233 : val3)));
bd0c5661
PB
7234 default:
7235 return -TARGET_ENOSYS;
7236 }
7237}
0f0426f3
LV
7238#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7239static abi_long do_name_to_handle_at(abi_long dirfd, abi_long pathname,
7240 abi_long handle, abi_long mount_id,
7241 abi_long flags)
7242{
7243 struct file_handle *target_fh;
7244 struct file_handle *fh;
7245 int mid = 0;
7246 abi_long ret;
7247 char *name;
7248 unsigned int size, total_size;
7249
7250 if (get_user_s32(size, handle)) {
7251 return -TARGET_EFAULT;
7252 }
7253
7254 name = lock_user_string(pathname);
7255 if (!name) {
7256 return -TARGET_EFAULT;
7257 }
7258
7259 total_size = sizeof(struct file_handle) + size;
7260 target_fh = lock_user(VERIFY_WRITE, handle, total_size, 0);
7261 if (!target_fh) {
7262 unlock_user(name, pathname, 0);
7263 return -TARGET_EFAULT;
7264 }
7265
7266 fh = g_malloc0(total_size);
7267 fh->handle_bytes = size;
7268
7269 ret = get_errno(name_to_handle_at(dirfd, path(name), fh, &mid, flags));
7270 unlock_user(name, pathname, 0);
7271
7272 /* man name_to_handle_at(2):
7273 * Other than the use of the handle_bytes field, the caller should treat
7274 * the file_handle structure as an opaque data type
7275 */
7276
7277 memcpy(target_fh, fh, total_size);
7278 target_fh->handle_bytes = tswap32(fh->handle_bytes);
7279 target_fh->handle_type = tswap32(fh->handle_type);
7280 g_free(fh);
7281 unlock_user(target_fh, handle, total_size);
7282
7283 if (put_user_s32(mid, mount_id)) {
7284 return -TARGET_EFAULT;
7285 }
7286
7287 return ret;
7288
7289}
7290#endif
7291
7292#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7293static abi_long do_open_by_handle_at(abi_long mount_fd, abi_long handle,
7294 abi_long flags)
7295{
7296 struct file_handle *target_fh;
7297 struct file_handle *fh;
7298 unsigned int size, total_size;
7299 abi_long ret;
7300
7301 if (get_user_s32(size, handle)) {
7302 return -TARGET_EFAULT;
7303 }
7304
7305 total_size = sizeof(struct file_handle) + size;
7306 target_fh = lock_user(VERIFY_READ, handle, total_size, 1);
7307 if (!target_fh) {
7308 return -TARGET_EFAULT;
7309 }
7310
e9d49d51 7311 fh = g_memdup(target_fh, total_size);
0f0426f3
LV
7312 fh->handle_bytes = size;
7313 fh->handle_type = tswap32(target_fh->handle_type);
7314
7315 ret = get_errno(open_by_handle_at(mount_fd, fh,
7316 target_to_host_bitmask(flags, fcntl_flags_tbl)));
7317
7318 g_free(fh);
7319
7320 unlock_user(target_fh, handle, total_size);
7321
7322 return ret;
7323}
7324#endif
bd0c5661 7325
e36800c9
LV
7326#if defined(TARGET_NR_signalfd) || defined(TARGET_NR_signalfd4)
7327
7328/* signalfd siginfo conversion */
7329
7330static void
7331host_to_target_signalfd_siginfo(struct signalfd_siginfo *tinfo,
7332 const struct signalfd_siginfo *info)
7333{
7334 int sig = host_to_target_signal(info->ssi_signo);
7335
7336 /* linux/signalfd.h defines a ssi_addr_lsb
7337 * not defined in sys/signalfd.h but used by some kernels
7338 */
7339
7340#ifdef BUS_MCEERR_AO
7341 if (tinfo->ssi_signo == SIGBUS &&
7342 (tinfo->ssi_code == BUS_MCEERR_AR ||
7343 tinfo->ssi_code == BUS_MCEERR_AO)) {
7344 uint16_t *ssi_addr_lsb = (uint16_t *)(&info->ssi_addr + 1);
7345 uint16_t *tssi_addr_lsb = (uint16_t *)(&tinfo->ssi_addr + 1);
7346 *tssi_addr_lsb = tswap16(*ssi_addr_lsb);
7347 }
7348#endif
7349
7350 tinfo->ssi_signo = tswap32(sig);
7351 tinfo->ssi_errno = tswap32(tinfo->ssi_errno);
7352 tinfo->ssi_code = tswap32(info->ssi_code);
7353 tinfo->ssi_pid = tswap32(info->ssi_pid);
7354 tinfo->ssi_uid = tswap32(info->ssi_uid);
7355 tinfo->ssi_fd = tswap32(info->ssi_fd);
7356 tinfo->ssi_tid = tswap32(info->ssi_tid);
7357 tinfo->ssi_band = tswap32(info->ssi_band);
7358 tinfo->ssi_overrun = tswap32(info->ssi_overrun);
7359 tinfo->ssi_trapno = tswap32(info->ssi_trapno);
7360 tinfo->ssi_status = tswap32(info->ssi_status);
7361 tinfo->ssi_int = tswap32(info->ssi_int);
7362 tinfo->ssi_ptr = tswap64(info->ssi_ptr);
7363 tinfo->ssi_utime = tswap64(info->ssi_utime);
7364 tinfo->ssi_stime = tswap64(info->ssi_stime);
7365 tinfo->ssi_addr = tswap64(info->ssi_addr);
7366}
7367
5d4d3665 7368static abi_long host_to_target_data_signalfd(void *buf, size_t len)
e36800c9
LV
7369{
7370 int i;
7371
7372 for (i = 0; i < len; i += sizeof(struct signalfd_siginfo)) {
7373 host_to_target_signalfd_siginfo(buf + i, buf + i);
7374 }
7375
7376 return len;
7377}
7378
7379static TargetFdTrans target_signalfd_trans = {
5d4d3665 7380 .host_to_target_data = host_to_target_data_signalfd,
e36800c9
LV
7381};
7382
7383static abi_long do_signalfd4(int fd, abi_long mask, int flags)
7384{
7385 int host_flags;
7386 target_sigset_t *target_mask;
7387 sigset_t host_mask;
7388 abi_long ret;
7389
7390 if (flags & ~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC)) {
7391 return -TARGET_EINVAL;
7392 }
7393 if (!lock_user_struct(VERIFY_READ, target_mask, mask, 1)) {
7394 return -TARGET_EFAULT;
7395 }
7396
7397 target_to_host_sigset(&host_mask, target_mask);
7398
7399 host_flags = target_to_host_bitmask(flags, fcntl_flags_tbl);
7400
7401 ret = get_errno(signalfd(fd, &host_mask, host_flags));
7402 if (ret >= 0) {
7403 fd_trans_register(ret, &target_signalfd_trans);
7404 }
7405
7406 unlock_user_struct(target_mask, mask, 0);
7407
7408 return ret;
7409}
7410#endif
7411
1d9d8b55
PB
7412/* Map host to target signal numbers for the wait family of syscalls.
7413 Assume all other status bits are the same. */
a05c6409 7414int host_to_target_waitstatus(int status)
1d9d8b55
PB
7415{
7416 if (WIFSIGNALED(status)) {
7417 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
7418 }
7419 if (WIFSTOPPED(status)) {
7420 return (host_to_target_signal(WSTOPSIG(status)) << 8)
7421 | (status & 0xff);
7422 }
7423 return status;
7424}
7425
76b94245
WVS
7426static int open_self_cmdline(void *cpu_env, int fd)
7427{
58de8b96
AS
7428 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
7429 struct linux_binprm *bprm = ((TaskState *)cpu->opaque)->bprm;
7430 int i;
76b94245 7431
58de8b96
AS
7432 for (i = 0; i < bprm->argc; i++) {
7433 size_t len = strlen(bprm->argv[i]) + 1;
76b94245 7434
58de8b96 7435 if (write(fd, bprm->argv[i], len) != len) {
76b94245 7436 return -1;
76b94245
WVS
7437 }
7438 }
7439
58de8b96 7440 return 0;
76b94245
WVS
7441}
7442
36c08d49
AG
7443static int open_self_maps(void *cpu_env, int fd)
7444{
0429a971
AF
7445 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
7446 TaskState *ts = cpu->opaque;
1a49ef2a
AG
7447 FILE *fp;
7448 char *line = NULL;
7449 size_t len = 0;
7450 ssize_t read;
7451
7452 fp = fopen("/proc/self/maps", "r");
7453 if (fp == NULL) {
a3ca7bb2 7454 return -1;
1a49ef2a 7455 }
36c08d49 7456
1a49ef2a
AG
7457 while ((read = getline(&line, &len, fp)) != -1) {
7458 int fields, dev_maj, dev_min, inode;
7459 uint64_t min, max, offset;
7460 char flag_r, flag_w, flag_x, flag_p;
7461 char path[512] = "";
7462 fields = sscanf(line, "%"PRIx64"-%"PRIx64" %c%c%c%c %"PRIx64" %x:%x %d"
7463 " %512s", &min, &max, &flag_r, &flag_w, &flag_x,
7464 &flag_p, &offset, &dev_maj, &dev_min, &inode, path);
7465
7466 if ((fields < 10) || (fields > 11)) {
7467 continue;
7468 }
d67f4aaa
MI
7469 if (h2g_valid(min)) {
7470 int flags = page_get_flags(h2g(min));
7471 max = h2g_valid(max - 1) ? max : (uintptr_t)g2h(GUEST_ADDR_MAX);
7472 if (page_check_range(h2g(min), max - min, flags) == -1) {
7473 continue;
7474 }
7475 if (h2g(min) == ts->info->stack_limit) {
7476 pstrcpy(path, sizeof(path), " [stack]");
7477 }
1a49ef2a 7478 dprintf(fd, TARGET_ABI_FMT_lx "-" TARGET_ABI_FMT_lx
e24fed4e 7479 " %c%c%c%c %08" PRIx64 " %02x:%02x %d %s%s\n",
d67f4aaa 7480 h2g(min), h2g(max - 1) + 1, flag_r, flag_w,
1a49ef2a 7481 flag_x, flag_p, offset, dev_maj, dev_min, inode,
e24fed4e 7482 path[0] ? " " : "", path);
1a49ef2a
AG
7483 }
7484 }
7485
7486 free(line);
7487 fclose(fp);
7488
36c08d49
AG
7489 return 0;
7490}
7491
480b8e7d
AG
7492static int open_self_stat(void *cpu_env, int fd)
7493{
0429a971
AF
7494 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
7495 TaskState *ts = cpu->opaque;
480b8e7d
AG
7496 abi_ulong start_stack = ts->info->start_stack;
7497 int i;
7498
7499 for (i = 0; i < 44; i++) {
7500 char buf[128];
7501 int len;
7502 uint64_t val = 0;
7503
e0e65bee
FE
7504 if (i == 0) {
7505 /* pid */
7506 val = getpid();
7507 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
7508 } else if (i == 1) {
7509 /* app name */
7510 snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
7511 } else if (i == 27) {
7512 /* stack bottom */
7513 val = start_stack;
7514 snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
7515 } else {
7516 /* for the rest, there is MasterCard */
7517 snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
480b8e7d 7518 }
e0e65bee 7519
480b8e7d
AG
7520 len = strlen(buf);
7521 if (write(fd, buf, len) != len) {
7522 return -1;
7523 }
7524 }
7525
7526 return 0;
7527}
7528
257450ee
AG
7529static int open_self_auxv(void *cpu_env, int fd)
7530{
0429a971
AF
7531 CPUState *cpu = ENV_GET_CPU((CPUArchState *)cpu_env);
7532 TaskState *ts = cpu->opaque;
257450ee
AG
7533 abi_ulong auxv = ts->info->saved_auxv;
7534 abi_ulong len = ts->info->auxv_len;
7535 char *ptr;
7536
7537 /*
7538 * Auxiliary vector is stored in target process stack.
7539 * read in whole auxv vector and copy it to file
7540 */
7541 ptr = lock_user(VERIFY_READ, auxv, len, 0);
7542 if (ptr != NULL) {
7543 while (len > 0) {
7544 ssize_t r;
7545 r = write(fd, ptr, len);
7546 if (r <= 0) {
7547 break;
7548 }
7549 len -= r;
7550 ptr += r;
7551 }
7552 lseek(fd, 0, SEEK_SET);
7553 unlock_user(ptr, auxv, len);
7554 }
7555
7556 return 0;
7557}
7558
463d8e73
AS
7559static int is_proc_myself(const char *filename, const char *entry)
7560{
7561 if (!strncmp(filename, "/proc/", strlen("/proc/"))) {
7562 filename += strlen("/proc/");
7563 if (!strncmp(filename, "self/", strlen("self/"))) {
7564 filename += strlen("self/");
7565 } else if (*filename >= '1' && *filename <= '9') {
7566 char myself[80];
7567 snprintf(myself, sizeof(myself), "%d/", getpid());
7568 if (!strncmp(filename, myself, strlen(myself))) {
7569 filename += strlen(myself);
7570 } else {
7571 return 0;
7572 }
7573 } else {
7574 return 0;
7575 }
7576 if (!strcmp(filename, entry)) {
7577 return 1;
7578 }
7579 }
7580 return 0;
7581}
7582
de6b9933
LV
7583#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
7584static int is_proc(const char *filename, const char *entry)
7585{
7586 return strcmp(filename, entry) == 0;
7587}
7588
7589static int open_net_route(void *cpu_env, int fd)
7590{
7591 FILE *fp;
7592 char *line = NULL;
7593 size_t len = 0;
7594 ssize_t read;
7595
7596 fp = fopen("/proc/net/route", "r");
7597 if (fp == NULL) {
a3ca7bb2 7598 return -1;
de6b9933
LV
7599 }
7600
7601 /* read header */
7602
7603 read = getline(&line, &len, fp);
7604 dprintf(fd, "%s", line);
7605
7606 /* read routes */
7607
7608 while ((read = getline(&line, &len, fp)) != -1) {
7609 char iface[16];
7610 uint32_t dest, gw, mask;
7611 unsigned int flags, refcnt, use, metric, mtu, window, irtt;
7612 sscanf(line, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
7613 iface, &dest, &gw, &flags, &refcnt, &use, &metric,
7614 &mask, &mtu, &window, &irtt);
7615 dprintf(fd, "%s\t%08x\t%08x\t%04x\t%d\t%d\t%d\t%08x\t%d\t%u\t%u\n",
7616 iface, tswap32(dest), tswap32(gw), flags, refcnt, use,
7617 metric, tswap32(mask), mtu, window, irtt);
7618 }
7619
7620 free(line);
7621 fclose(fp);
7622
7623 return 0;
7624}
7625#endif
7626
0b2effd7 7627static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags, mode_t mode)
3be14d05
AG
7628{
7629 struct fake_open {
7630 const char *filename;
7631 int (*fill)(void *cpu_env, int fd);
de6b9933 7632 int (*cmp)(const char *s1, const char *s2);
3be14d05
AG
7633 };
7634 const struct fake_open *fake_open;
7635 static const struct fake_open fakes[] = {
de6b9933
LV
7636 { "maps", open_self_maps, is_proc_myself },
7637 { "stat", open_self_stat, is_proc_myself },
7638 { "auxv", open_self_auxv, is_proc_myself },
76b94245 7639 { "cmdline", open_self_cmdline, is_proc_myself },
de6b9933
LV
7640#if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
7641 { "/proc/net/route", open_net_route, is_proc },
7642#endif
7643 { NULL, NULL, NULL }
3be14d05
AG
7644 };
7645
aa07f5ec
MO
7646 if (is_proc_myself(pathname, "exe")) {
7647 int execfd = qemu_getauxval(AT_EXECFD);
c10a0738 7648 return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode);
aa07f5ec
MO
7649 }
7650
3be14d05 7651 for (fake_open = fakes; fake_open->filename; fake_open++) {
de6b9933 7652 if (fake_open->cmp(pathname, fake_open->filename)) {
3be14d05
AG
7653 break;
7654 }
7655 }
7656
7657 if (fake_open->filename) {
7658 const char *tmpdir;
7659 char filename[PATH_MAX];
7660 int fd, r;
7661
7662 /* create temporary file to map stat to */
7663 tmpdir = getenv("TMPDIR");
7664 if (!tmpdir)
7665 tmpdir = "/tmp";
7666 snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
7667 fd = mkstemp(filename);
7668 if (fd < 0) {
7669 return fd;
7670 }
7671 unlink(filename);
7672
7673 if ((r = fake_open->fill(cpu_env, fd))) {
a3ca7bb2 7674 int e = errno;
3be14d05 7675 close(fd);
a3ca7bb2 7676 errno = e;
3be14d05
AG
7677 return r;
7678 }
7679 lseek(fd, 0, SEEK_SET);
7680
7681 return fd;
7682 }
7683
c10a0738 7684 return safe_openat(dirfd, path(pathname), flags, mode);
3be14d05
AG
7685}
7686
aecc8861
AG
7687#define TIMER_MAGIC 0x0caf0000
7688#define TIMER_MAGIC_MASK 0xffff0000
7689
7690/* Convert QEMU provided timer ID back to internal 16bit index format */
7691static target_timer_t get_timer_id(abi_long arg)
7692{
7693 target_timer_t timerid = arg;
7694
7695 if ((timerid & TIMER_MAGIC_MASK) != TIMER_MAGIC) {
7696 return -TARGET_EINVAL;
7697 }
7698
7699 timerid &= 0xffff;
7700
7701 if (timerid >= ARRAY_SIZE(g_posix_timers)) {
7702 return -TARGET_EINVAL;
7703 }
7704
7705 return timerid;
7706}
7707
562a20b4
LV
7708static abi_long swap_data_eventfd(void *buf, size_t len)
7709{
7710 uint64_t *counter = buf;
7711 int i;
7712
7713 if (len < sizeof(uint64_t)) {
7714 return -EINVAL;
7715 }
7716
7717 for (i = 0; i < len; i += sizeof(uint64_t)) {
7718 *counter = tswap64(*counter);
7719 counter++;
7720 }
7721
7722 return len;
7723}
7724
7725static TargetFdTrans target_eventfd_trans = {
7726 .host_to_target_data = swap_data_eventfd,
7727 .target_to_host_data = swap_data_eventfd,
7728};
7729
c4e316cf
LV
7730#if (defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)) || \
7731 (defined(CONFIG_INOTIFY1) && defined(TARGET_NR_inotify_init1) && \
7732 defined(__NR_inotify_init1))
7733static abi_long host_to_target_data_inotify(void *buf, size_t len)
7734{
7735 struct inotify_event *ev;
7736 int i;
7737 uint32_t name_len;
7738
7739 for (i = 0; i < len; i += sizeof(struct inotify_event) + name_len) {
7740 ev = (struct inotify_event *)((char *)buf + i);
7741 name_len = ev->len;
7742
7743 ev->wd = tswap32(ev->wd);
7744 ev->mask = tswap32(ev->mask);
7745 ev->cookie = tswap32(ev->cookie);
7746 ev->len = tswap32(name_len);
7747 }
7748
7749 return len;
7750}
7751
7752static TargetFdTrans target_inotify_trans = {
7753 .host_to_target_data = host_to_target_data_inotify,
7754};
7755#endif
7756
2e0a8713
ST
7757static int target_to_host_cpu_mask(unsigned long *host_mask,
7758 size_t host_size,
7759 abi_ulong target_addr,
7760 size_t target_size)
7761{
7762 unsigned target_bits = sizeof(abi_ulong) * 8;
7763 unsigned host_bits = sizeof(*host_mask) * 8;
7764 abi_ulong *target_mask;
7765 unsigned i, j;
7766
7767 assert(host_size >= target_size);
7768
7769 target_mask = lock_user(VERIFY_READ, target_addr, target_size, 1);
7770 if (!target_mask) {
7771 return -TARGET_EFAULT;
7772 }
7773 memset(host_mask, 0, host_size);
7774
7775 for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
7776 unsigned bit = i * target_bits;
7777 abi_ulong val;
7778
7779 __get_user(val, &target_mask[i]);
7780 for (j = 0; j < target_bits; j++, bit++) {
7781 if (val & (1UL << j)) {
7782 host_mask[bit / host_bits] |= 1UL << (bit % host_bits);
7783 }
7784 }
7785 }
7786
7787 unlock_user(target_mask, target_addr, 0);
7788 return 0;
7789}
7790
7791static int host_to_target_cpu_mask(const unsigned long *host_mask,
7792 size_t host_size,
7793 abi_ulong target_addr,
7794 size_t target_size)
7795{
7796 unsigned target_bits = sizeof(abi_ulong) * 8;
7797 unsigned host_bits = sizeof(*host_mask) * 8;
7798 abi_ulong *target_mask;
7799 unsigned i, j;
7800
7801 assert(host_size >= target_size);
7802
7803 target_mask = lock_user(VERIFY_WRITE, target_addr, target_size, 0);
7804 if (!target_mask) {
7805 return -TARGET_EFAULT;
7806 }
7807
7808 for (i = 0 ; i < target_size / sizeof(abi_ulong); i++) {
7809 unsigned bit = i * target_bits;
7810 abi_ulong val = 0;
7811
7812 for (j = 0; j < target_bits; j++, bit++) {
7813 if (host_mask[bit / host_bits] & (1UL << (bit % host_bits))) {
7814 val |= 1UL << j;
7815 }
7816 }
7817 __put_user(val, &target_mask[i]);
7818 }
7819
7820 unlock_user(target_mask, target_addr, target_size);
7821 return 0;
7822}
7823
0da46a6e
TS
7824/* do_syscall() should always have a single exit point at the end so
7825 that actions, such as logging of syscall results, can be performed.
7826 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
992f48a0
BS
7827abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
7828 abi_long arg2, abi_long arg3, abi_long arg4,
5945cfcb
PM
7829 abi_long arg5, abi_long arg6, abi_long arg7,
7830 abi_long arg8)
31e31b8a 7831{
182735ef 7832 CPUState *cpu = ENV_GET_CPU(cpu_env);
992f48a0 7833 abi_long ret;
31e31b8a 7834 struct stat st;
56c8f68f 7835 struct statfs stfs;
53a5960a 7836 void *p;
3b46e624 7837
71a8f7fe
TB
7838#if defined(DEBUG_ERESTARTSYS)
7839 /* Debug-only code for exercising the syscall-restart code paths
7840 * in the per-architecture cpu main loops: restart every syscall
7841 * the guest makes once before letting it through.
7842 */
7843 {
7844 static int flag;
7845
7846 flag = !flag;
7847 if (flag) {
7848 return -TARGET_ERESTARTSYS;
7849 }
7850 }
7851#endif
7852
72f03900 7853#ifdef DEBUG
c573ff67 7854 gemu_log("syscall %d", num);
72f03900 7855#endif
9c15e700 7856 trace_guest_user_syscall(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
b92c47c1
TS
7857 if(do_strace)
7858 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
7859
31e31b8a
FB
7860 switch(num) {
7861 case TARGET_NR_exit:
9b056fcc
AF
7862 /* In old applications this may be used to implement _exit(2).
7863 However in threaded applictions it is used for thread termination,
7864 and _exit_group is used for application termination.
7865 Do thread termination if we have more then one thread. */
a0995886
TB
7866
7867 if (block_signals()) {
7868 ret = -TARGET_ERESTARTSYS;
7869 break;
7870 }
7871
dd1f6349
AB
7872 cpu_list_lock();
7873
bdc44640 7874 if (CPU_NEXT(first_cpu)) {
9b056fcc 7875 TaskState *ts;
9b056fcc 7876
9b056fcc 7877 /* Remove the CPU from the list. */
bdc44640 7878 QTAILQ_REMOVE(&cpus, cpu, node);
dd1f6349 7879
9b056fcc 7880 cpu_list_unlock();
dd1f6349 7881
0429a971 7882 ts = cpu->opaque;
9b056fcc
AF
7883 if (ts->child_tidptr) {
7884 put_user_u32(0, ts->child_tidptr);
7885 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
7886 NULL, NULL, 0);
7887 }
a2247f8e 7888 thread_cpu = NULL;
0429a971 7889 object_unref(OBJECT(cpu));
9b056fcc 7890 g_free(ts);
70903763 7891 rcu_unregister_thread();
9b056fcc
AF
7892 pthread_exit(NULL);
7893 }
dd1f6349
AB
7894
7895 cpu_list_unlock();
9788c9ca 7896#ifdef TARGET_GPROF
7d13299d
FB
7897 _mcleanup();
7898#endif
e9009676 7899 gdb_exit(cpu_env, arg1);
c2764719 7900 _exit(arg1);
31e31b8a
FB
7901 ret = 0; /* avoid warning */
7902 break;
7903 case TARGET_NR_read:
38d840e6
AJ
7904 if (arg3 == 0)
7905 ret = 0;
7906 else {
7907 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
7908 goto efault;
50afd02b 7909 ret = get_errno(safe_read(arg1, p, arg3));
e36800c9 7910 if (ret >= 0 &&
5d4d3665
LV
7911 fd_trans_host_to_target_data(arg1)) {
7912 ret = fd_trans_host_to_target_data(arg1)(p, ret);
e36800c9 7913 }
38d840e6
AJ
7914 unlock_user(p, arg2, ret);
7915 }
31e31b8a
FB
7916 break;
7917 case TARGET_NR_write:
579a97f7
FB
7918 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
7919 goto efault;
04b9bcf9
LV
7920 if (fd_trans_target_to_host_data(arg1)) {
7921 void *copy = g_malloc(arg3);
7922 memcpy(copy, p, arg3);
7923 ret = fd_trans_target_to_host_data(arg1)(copy, arg3);
7924 if (ret >= 0) {
7925 ret = get_errno(safe_write(arg1, copy, ret));
7926 }
7927 g_free(copy);
7928 } else {
7929 ret = get_errno(safe_write(arg1, p, arg3));
7930 }
53a5960a 7931 unlock_user(p, arg2, 0);
31e31b8a 7932 break;
704eff6c 7933#ifdef TARGET_NR_open
31e31b8a 7934 case TARGET_NR_open:
2f619698
FB
7935 if (!(p = lock_user_string(arg1)))
7936 goto efault;
0b2effd7
RV
7937 ret = get_errno(do_openat(cpu_env, AT_FDCWD, p,
7938 target_to_host_bitmask(arg2, fcntl_flags_tbl),
7939 arg3));
e36800c9 7940 fd_trans_unregister(ret);
53a5960a 7941 unlock_user(p, arg1, 0);
31e31b8a 7942 break;
704eff6c 7943#endif
82424832 7944 case TARGET_NR_openat:
579a97f7
FB
7945 if (!(p = lock_user_string(arg2)))
7946 goto efault;
0b2effd7
RV
7947 ret = get_errno(do_openat(cpu_env, arg1, p,
7948 target_to_host_bitmask(arg3, fcntl_flags_tbl),
7949 arg4));
e36800c9 7950 fd_trans_unregister(ret);
579a97f7 7951 unlock_user(p, arg2, 0);
82424832 7952 break;
0f0426f3
LV
7953#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7954 case TARGET_NR_name_to_handle_at:
7955 ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
7956 break;
7957#endif
7958#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
7959 case TARGET_NR_open_by_handle_at:
7960 ret = do_open_by_handle_at(arg1, arg2, arg3);
e36800c9 7961 fd_trans_unregister(ret);
0f0426f3
LV
7962 break;
7963#endif
31e31b8a 7964 case TARGET_NR_close:
e36800c9 7965 fd_trans_unregister(arg1);
31e31b8a
FB
7966 ret = get_errno(close(arg1));
7967 break;
7968 case TARGET_NR_brk:
53a5960a 7969 ret = do_brk(arg1);
31e31b8a 7970 break;
704eff6c 7971#ifdef TARGET_NR_fork
31e31b8a 7972 case TARGET_NR_fork:
7eddb5dd 7973 ret = get_errno(do_fork(cpu_env, TARGET_SIGCHLD, 0, 0, 0, 0));
31e31b8a 7974 break;
704eff6c 7975#endif
e5febef5 7976#ifdef TARGET_NR_waitpid
31e31b8a
FB
7977 case TARGET_NR_waitpid:
7978 {
53a5960a 7979 int status;
4af80a37 7980 ret = get_errno(safe_wait4(arg1, &status, arg3, 0));
5379557b 7981 if (!is_error(ret) && arg2 && ret
1d9d8b55 7982 && put_user_s32(host_to_target_waitstatus(status), arg2))
2f619698 7983 goto efault;
31e31b8a
FB
7984 }
7985 break;
e5febef5 7986#endif
f0cbb613
PB
7987#ifdef TARGET_NR_waitid
7988 case TARGET_NR_waitid:
7989 {
7990 siginfo_t info;
7991 info.si_pid = 0;
4af80a37 7992 ret = get_errno(safe_waitid(arg1, arg2, &info, arg4, NULL));
f0cbb613 7993 if (!is_error(ret) && arg3 && info.si_pid != 0) {
c227f099 7994 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
f0cbb613
PB
7995 goto efault;
7996 host_to_target_siginfo(p, &info);
c227f099 7997 unlock_user(p, arg3, sizeof(target_siginfo_t));
f0cbb613
PB
7998 }
7999 }
8000 break;
8001#endif
7a3148a9 8002#ifdef TARGET_NR_creat /* not on alpha */
31e31b8a 8003 case TARGET_NR_creat:
579a97f7
FB
8004 if (!(p = lock_user_string(arg1)))
8005 goto efault;
53a5960a 8006 ret = get_errno(creat(p, arg2));
e36800c9 8007 fd_trans_unregister(ret);
53a5960a 8008 unlock_user(p, arg1, 0);
31e31b8a 8009 break;
7a3148a9 8010#endif
704eff6c 8011#ifdef TARGET_NR_link
31e31b8a 8012 case TARGET_NR_link:
53a5960a
PB
8013 {
8014 void * p2;
8015 p = lock_user_string(arg1);
8016 p2 = lock_user_string(arg2);
579a97f7
FB
8017 if (!p || !p2)
8018 ret = -TARGET_EFAULT;
8019 else
8020 ret = get_errno(link(p, p2));
53a5960a
PB
8021 unlock_user(p2, arg2, 0);
8022 unlock_user(p, arg1, 0);
8023 }
31e31b8a 8024 break;
704eff6c 8025#endif
c0d472b1 8026#if defined(TARGET_NR_linkat)
64f0ce4c 8027 case TARGET_NR_linkat:
64f0ce4c
TS
8028 {
8029 void * p2 = NULL;
579a97f7
FB
8030 if (!arg2 || !arg4)
8031 goto efault;
64f0ce4c
TS
8032 p = lock_user_string(arg2);
8033 p2 = lock_user_string(arg4);
579a97f7 8034 if (!p || !p2)
0da46a6e 8035 ret = -TARGET_EFAULT;
64f0ce4c 8036 else
c0d472b1 8037 ret = get_errno(linkat(arg1, p, arg3, p2, arg5));
579a97f7
FB
8038 unlock_user(p, arg2, 0);
8039 unlock_user(p2, arg4, 0);
64f0ce4c
TS
8040 }
8041 break;
8042#endif
704eff6c 8043#ifdef TARGET_NR_unlink
31e31b8a 8044 case TARGET_NR_unlink:
579a97f7
FB
8045 if (!(p = lock_user_string(arg1)))
8046 goto efault;
53a5960a
PB
8047 ret = get_errno(unlink(p));
8048 unlock_user(p, arg1, 0);
31e31b8a 8049 break;
704eff6c 8050#endif
c0d472b1 8051#if defined(TARGET_NR_unlinkat)
8170f56b 8052 case TARGET_NR_unlinkat:
579a97f7
FB
8053 if (!(p = lock_user_string(arg2)))
8054 goto efault;
c0d472b1 8055 ret = get_errno(unlinkat(arg1, p, arg3));
579a97f7 8056 unlock_user(p, arg2, 0);
ed494d87 8057 break;
b7d35e65 8058#endif
31e31b8a 8059 case TARGET_NR_execve:
7854b056
FB
8060 {
8061 char **argp, **envp;
f7341ff4 8062 int argc, envc;
992f48a0
BS
8063 abi_ulong gp;
8064 abi_ulong guest_argp;
8065 abi_ulong guest_envp;
8066 abi_ulong addr;
7854b056 8067 char **q;
a6f79cc9 8068 int total_size = 0;
7854b056 8069
f7341ff4 8070 argc = 0;
53a5960a 8071 guest_argp = arg2;
da94d263 8072 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
03aa1976 8073 if (get_user_ual(addr, gp))
2f619698 8074 goto efault;
03aa1976 8075 if (!addr)
2f619698 8076 break;
7854b056 8077 argc++;
2f619698 8078 }
f7341ff4 8079 envc = 0;
53a5960a 8080 guest_envp = arg3;
da94d263 8081 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
03aa1976 8082 if (get_user_ual(addr, gp))
2f619698 8083 goto efault;
03aa1976 8084 if (!addr)
2f619698 8085 break;
7854b056 8086 envc++;
2f619698 8087 }
7854b056 8088
b936cb50
PP
8089 argp = g_new0(char *, argc + 1);
8090 envp = g_new0(char *, envc + 1);
7854b056 8091
da94d263 8092 for (gp = guest_argp, q = argp; gp;
992f48a0 8093 gp += sizeof(abi_ulong), q++) {
2f619698
FB
8094 if (get_user_ual(addr, gp))
8095 goto execve_efault;
53a5960a
PB
8096 if (!addr)
8097 break;
2f619698
FB
8098 if (!(*q = lock_user_string(addr)))
8099 goto execve_efault;
a6f79cc9 8100 total_size += strlen(*q) + 1;
53a5960a 8101 }
f7341ff4
FB
8102 *q = NULL;
8103
da94d263 8104 for (gp = guest_envp, q = envp; gp;
992f48a0 8105 gp += sizeof(abi_ulong), q++) {
2f619698
FB
8106 if (get_user_ual(addr, gp))
8107 goto execve_efault;
53a5960a
PB
8108 if (!addr)
8109 break;
2f619698
FB
8110 if (!(*q = lock_user_string(addr)))
8111 goto execve_efault;
a6f79cc9 8112 total_size += strlen(*q) + 1;
53a5960a 8113 }
f7341ff4 8114 *q = NULL;
7854b056 8115
2f619698
FB
8116 if (!(p = lock_user_string(arg1)))
8117 goto execve_efault;
ffdcbe22
TB
8118 /* Although execve() is not an interruptible syscall it is
8119 * a special case where we must use the safe_syscall wrapper:
8120 * if we allow a signal to happen before we make the host
8121 * syscall then we will 'lose' it, because at the point of
8122 * execve the process leaves QEMU's control. So we use the
8123 * safe syscall wrapper to ensure that we either take the
8124 * signal as a guest signal, or else it does not happen
8125 * before the execve completes and makes it the other
8126 * program's problem.
8127 */
8128 ret = get_errno(safe_execve(p, argp, envp));
53a5960a
PB
8129 unlock_user(p, arg1, 0);
8130
2f619698
FB
8131 goto execve_end;
8132
8133 execve_efault:
8134 ret = -TARGET_EFAULT;
8135
8136 execve_end:
53a5960a 8137 for (gp = guest_argp, q = argp; *q;
992f48a0 8138 gp += sizeof(abi_ulong), q++) {
2f619698
FB
8139 if (get_user_ual(addr, gp)
8140 || !addr)
8141 break;
53a5960a
PB
8142 unlock_user(*q, addr, 0);
8143 }
8144 for (gp = guest_envp, q = envp; *q;
992f48a0 8145 gp += sizeof(abi_ulong), q++) {
2f619698
FB
8146 if (get_user_ual(addr, gp)
8147 || !addr)
8148 break;
53a5960a
PB
8149 unlock_user(*q, addr, 0);
8150 }
b936cb50
PP
8151
8152 g_free(argp);
8153 g_free(envp);
7854b056 8154 }
31e31b8a
FB
8155 break;
8156 case TARGET_NR_chdir:
579a97f7
FB
8157 if (!(p = lock_user_string(arg1)))
8158 goto efault;
53a5960a
PB
8159 ret = get_errno(chdir(p));
8160 unlock_user(p, arg1, 0);
31e31b8a 8161 break;
a315a145 8162#ifdef TARGET_NR_time
31e31b8a
FB
8163 case TARGET_NR_time:
8164 {
53a5960a
PB
8165 time_t host_time;
8166 ret = get_errno(time(&host_time));
2f619698
FB
8167 if (!is_error(ret)
8168 && arg1
8169 && put_user_sal(host_time, arg1))
8170 goto efault;
31e31b8a
FB
8171 }
8172 break;
a315a145 8173#endif
704eff6c 8174#ifdef TARGET_NR_mknod
31e31b8a 8175 case TARGET_NR_mknod:
579a97f7
FB
8176 if (!(p = lock_user_string(arg1)))
8177 goto efault;
53a5960a
PB
8178 ret = get_errno(mknod(p, arg2, arg3));
8179 unlock_user(p, arg1, 0);
31e31b8a 8180 break;
704eff6c 8181#endif
c0d472b1 8182#if defined(TARGET_NR_mknodat)
75ac37a0 8183 case TARGET_NR_mknodat:
579a97f7
FB
8184 if (!(p = lock_user_string(arg2)))
8185 goto efault;
c0d472b1 8186 ret = get_errno(mknodat(arg1, p, arg3, arg4));
579a97f7 8187 unlock_user(p, arg2, 0);
75ac37a0
TS
8188 break;
8189#endif
704eff6c 8190#ifdef TARGET_NR_chmod
31e31b8a 8191 case TARGET_NR_chmod:
579a97f7
FB
8192 if (!(p = lock_user_string(arg1)))
8193 goto efault;
53a5960a
PB
8194 ret = get_errno(chmod(p, arg2));
8195 unlock_user(p, arg1, 0);
31e31b8a 8196 break;
704eff6c 8197#endif
ebc05488 8198#ifdef TARGET_NR_break
31e31b8a
FB
8199 case TARGET_NR_break:
8200 goto unimplemented;
ebc05488
FB
8201#endif
8202#ifdef TARGET_NR_oldstat
31e31b8a
FB
8203 case TARGET_NR_oldstat:
8204 goto unimplemented;
ebc05488 8205#endif
31e31b8a
FB
8206 case TARGET_NR_lseek:
8207 ret = get_errno(lseek(arg1, arg2, arg3));
8208 break;
9231733a
RH
8209#if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
8210 /* Alpha specific */
7a3148a9 8211 case TARGET_NR_getxpid:
9231733a
RH
8212 ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
8213 ret = get_errno(getpid());
8214 break;
7a3148a9 8215#endif
9231733a
RH
8216#ifdef TARGET_NR_getpid
8217 case TARGET_NR_getpid:
31e31b8a
FB
8218 ret = get_errno(getpid());
8219 break;
9231733a 8220#endif
31e31b8a 8221 case TARGET_NR_mount:
356d771b
PB
8222 {
8223 /* need to look at the data field */
8224 void *p2, *p3;
8225
8226 if (arg1) {
8227 p = lock_user_string(arg1);
8228 if (!p) {
8229 goto efault;
8230 }
8231 } else {
8232 p = NULL;
8233 }
8234
8235 p2 = lock_user_string(arg2);
8236 if (!p2) {
8237 if (arg1) {
8238 unlock_user(p, arg1, 0);
8239 }
8240 goto efault;
8241 }
8242
8243 if (arg3) {
8244 p3 = lock_user_string(arg3);
8245 if (!p3) {
8246 if (arg1) {
579a97f7 8247 unlock_user(p, arg1, 0);
356d771b
PB
8248 }
8249 unlock_user(p2, arg2, 0);
8250 goto efault;
8251 }
8252 } else {
8253 p3 = NULL;
8254 }
8255
8256 /* FIXME - arg5 should be locked, but it isn't clear how to
8257 * do that since it's not guaranteed to be a NULL-terminated
8258 * string.
8259 */
8260 if (!arg5) {
8261 ret = mount(p, p2, p3, (unsigned long)arg4, NULL);
8262 } else {
8263 ret = mount(p, p2, p3, (unsigned long)arg4, g2h(arg5));
8264 }
8265 ret = get_errno(ret);
8266
8267 if (arg1) {
8268 unlock_user(p, arg1, 0);
8269 }
8270 unlock_user(p2, arg2, 0);
8271 if (arg3) {
8272 unlock_user(p3, arg3, 0);
8273 }
8274 }
8275 break;
e5febef5 8276#ifdef TARGET_NR_umount
31e31b8a 8277 case TARGET_NR_umount:
579a97f7
FB
8278 if (!(p = lock_user_string(arg1)))
8279 goto efault;
53a5960a
PB
8280 ret = get_errno(umount(p));
8281 unlock_user(p, arg1, 0);
31e31b8a 8282 break;
e5febef5 8283#endif
7a3148a9 8284#ifdef TARGET_NR_stime /* not on alpha */
31e31b8a
FB
8285 case TARGET_NR_stime:
8286 {
53a5960a 8287 time_t host_time;
2f619698
FB
8288 if (get_user_sal(host_time, arg1))
8289 goto efault;
53a5960a 8290 ret = get_errno(stime(&host_time));
31e31b8a
FB
8291 }
8292 break;
7a3148a9 8293#endif
31e31b8a
FB
8294 case TARGET_NR_ptrace:
8295 goto unimplemented;
7a3148a9 8296#ifdef TARGET_NR_alarm /* not on alpha */
31e31b8a
FB
8297 case TARGET_NR_alarm:
8298 ret = alarm(arg1);
8299 break;
7a3148a9 8300#endif
ebc05488 8301#ifdef TARGET_NR_oldfstat
31e31b8a
FB
8302 case TARGET_NR_oldfstat:
8303 goto unimplemented;
ebc05488 8304#endif
7a3148a9 8305#ifdef TARGET_NR_pause /* not on alpha */
31e31b8a 8306 case TARGET_NR_pause:
f59ec606
TB
8307 if (!block_signals()) {
8308 sigsuspend(&((TaskState *)cpu->opaque)->signal_mask);
8309 }
8310 ret = -TARGET_EINTR;
31e31b8a 8311 break;
7a3148a9 8312#endif
e5febef5 8313#ifdef TARGET_NR_utime
31e31b8a 8314 case TARGET_NR_utime:
ebc05488 8315 {
53a5960a
PB
8316 struct utimbuf tbuf, *host_tbuf;
8317 struct target_utimbuf *target_tbuf;
8318 if (arg2) {
579a97f7
FB
8319 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
8320 goto efault;
cbb21eed
MB
8321 tbuf.actime = tswapal(target_tbuf->actime);
8322 tbuf.modtime = tswapal(target_tbuf->modtime);
53a5960a
PB
8323 unlock_user_struct(target_tbuf, arg2, 0);
8324 host_tbuf = &tbuf;
f72e8ff4 8325 } else {
53a5960a 8326 host_tbuf = NULL;
f72e8ff4 8327 }
579a97f7
FB
8328 if (!(p = lock_user_string(arg1)))
8329 goto efault;
53a5960a
PB
8330 ret = get_errno(utime(p, host_tbuf));
8331 unlock_user(p, arg1, 0);
ebc05488
FB
8332 }
8333 break;
e5febef5 8334#endif
704eff6c 8335#ifdef TARGET_NR_utimes
978a66ff
FB
8336 case TARGET_NR_utimes:
8337 {
978a66ff 8338 struct timeval *tvp, tv[2];
53a5960a 8339 if (arg2) {
788f5ec4
TS
8340 if (copy_from_user_timeval(&tv[0], arg2)
8341 || copy_from_user_timeval(&tv[1],
8342 arg2 + sizeof(struct target_timeval)))
8343 goto efault;
978a66ff
FB
8344 tvp = tv;
8345 } else {
8346 tvp = NULL;
8347 }
579a97f7
FB
8348 if (!(p = lock_user_string(arg1)))
8349 goto efault;
53a5960a
PB
8350 ret = get_errno(utimes(p, tvp));
8351 unlock_user(p, arg1, 0);
978a66ff
FB
8352 }
8353 break;
704eff6c 8354#endif
c0d472b1 8355#if defined(TARGET_NR_futimesat)
ac8a6556
AZ
8356 case TARGET_NR_futimesat:
8357 {
8358 struct timeval *tvp, tv[2];
8359 if (arg3) {
8360 if (copy_from_user_timeval(&tv[0], arg3)
8361 || copy_from_user_timeval(&tv[1],
8362 arg3 + sizeof(struct target_timeval)))
8363 goto efault;
8364 tvp = tv;
8365 } else {
8366 tvp = NULL;
8367 }
8368 if (!(p = lock_user_string(arg2)))
8369 goto efault;
c0d472b1 8370 ret = get_errno(futimesat(arg1, path(p), tvp));
ac8a6556
AZ
8371 unlock_user(p, arg2, 0);
8372 }
8373 break;
8374#endif
ebc05488 8375#ifdef TARGET_NR_stty
31e31b8a
FB
8376 case TARGET_NR_stty:
8377 goto unimplemented;
ebc05488
FB
8378#endif
8379#ifdef TARGET_NR_gtty
31e31b8a
FB
8380 case TARGET_NR_gtty:
8381 goto unimplemented;
ebc05488 8382#endif
704eff6c 8383#ifdef TARGET_NR_access
31e31b8a 8384 case TARGET_NR_access:
579a97f7
FB
8385 if (!(p = lock_user_string(arg1)))
8386 goto efault;
719f908e 8387 ret = get_errno(access(path(p), arg2));
53a5960a 8388 unlock_user(p, arg1, 0);
31e31b8a 8389 break;
704eff6c 8390#endif
92a34c10
TS
8391#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
8392 case TARGET_NR_faccessat:
579a97f7
FB
8393 if (!(p = lock_user_string(arg2)))
8394 goto efault;
c0d472b1 8395 ret = get_errno(faccessat(arg1, p, arg3, 0));
579a97f7 8396 unlock_user(p, arg2, 0);
92a34c10
TS
8397 break;
8398#endif
7a3148a9 8399#ifdef TARGET_NR_nice /* not on alpha */
31e31b8a
FB
8400 case TARGET_NR_nice:
8401 ret = get_errno(nice(arg1));
8402 break;
7a3148a9 8403#endif
ebc05488 8404#ifdef TARGET_NR_ftime
31e31b8a
FB
8405 case TARGET_NR_ftime:
8406 goto unimplemented;
ebc05488 8407#endif
31e31b8a 8408 case TARGET_NR_sync:
04369ff2
FB
8409 sync();
8410 ret = 0;
31e31b8a 8411 break;
5a03cd00
AM
8412#if defined(TARGET_NR_syncfs) && defined(CONFIG_SYNCFS)
8413 case TARGET_NR_syncfs:
8414 ret = get_errno(syncfs(arg1));
8415 break;
8416#endif
31e31b8a 8417 case TARGET_NR_kill:
bef653d9 8418 ret = get_errno(safe_kill(arg1, target_to_host_signal(arg2)));
31e31b8a 8419 break;
704eff6c 8420#ifdef TARGET_NR_rename
31e31b8a 8421 case TARGET_NR_rename:
53a5960a
PB
8422 {
8423 void *p2;
8424 p = lock_user_string(arg1);
8425 p2 = lock_user_string(arg2);
579a97f7
FB
8426 if (!p || !p2)
8427 ret = -TARGET_EFAULT;
8428 else
8429 ret = get_errno(rename(p, p2));
53a5960a
PB
8430 unlock_user(p2, arg2, 0);
8431 unlock_user(p, arg1, 0);
8432 }
31e31b8a 8433 break;
704eff6c 8434#endif
c0d472b1 8435#if defined(TARGET_NR_renameat)
722183f6 8436 case TARGET_NR_renameat:
722183f6 8437 {
579a97f7 8438 void *p2;
722183f6
TS
8439 p = lock_user_string(arg2);
8440 p2 = lock_user_string(arg4);
579a97f7 8441 if (!p || !p2)
0da46a6e 8442 ret = -TARGET_EFAULT;
722183f6 8443 else
c0d472b1 8444 ret = get_errno(renameat(arg1, p, arg3, p2));
579a97f7
FB
8445 unlock_user(p2, arg4, 0);
8446 unlock_user(p, arg2, 0);
722183f6 8447 }
95d0307c
AS
8448 break;
8449#endif
8450#if defined(TARGET_NR_renameat2)
8451 case TARGET_NR_renameat2:
8452 {
8453 void *p2;
8454 p = lock_user_string(arg2);
8455 p2 = lock_user_string(arg4);
8456 if (!p || !p2) {
8457 ret = -TARGET_EFAULT;
8458 } else {
8459 ret = get_errno(sys_renameat2(arg1, p, arg3, p2, arg5));
8460 }
8461 unlock_user(p2, arg4, 0);
8462 unlock_user(p, arg2, 0);
8463 }
722183f6
TS
8464 break;
8465#endif
704eff6c 8466#ifdef TARGET_NR_mkdir
31e31b8a 8467 case TARGET_NR_mkdir:
579a97f7
FB
8468 if (!(p = lock_user_string(arg1)))
8469 goto efault;
53a5960a
PB
8470 ret = get_errno(mkdir(p, arg2));
8471 unlock_user(p, arg1, 0);
31e31b8a 8472 break;
704eff6c 8473#endif
c0d472b1 8474#if defined(TARGET_NR_mkdirat)
4472ad0d 8475 case TARGET_NR_mkdirat:
579a97f7
FB
8476 if (!(p = lock_user_string(arg2)))
8477 goto efault;
c0d472b1 8478 ret = get_errno(mkdirat(arg1, p, arg3));
579a97f7 8479 unlock_user(p, arg2, 0);
4472ad0d
TS
8480 break;
8481#endif
704eff6c 8482#ifdef TARGET_NR_rmdir
31e31b8a 8483 case TARGET_NR_rmdir:
579a97f7
FB
8484 if (!(p = lock_user_string(arg1)))
8485 goto efault;
53a5960a
PB
8486 ret = get_errno(rmdir(p));
8487 unlock_user(p, arg1, 0);
31e31b8a 8488 break;
704eff6c 8489#endif
31e31b8a
FB
8490 case TARGET_NR_dup:
8491 ret = get_errno(dup(arg1));
e36800c9
LV
8492 if (ret >= 0) {
8493 fd_trans_dup(arg1, ret);
8494 }
31e31b8a 8495 break;
704eff6c 8496#ifdef TARGET_NR_pipe
31e31b8a 8497 case TARGET_NR_pipe:
fb41a66e 8498 ret = do_pipe(cpu_env, arg1, 0, 0);
099d6b0f 8499 break;
704eff6c 8500#endif
099d6b0f
RV
8501#ifdef TARGET_NR_pipe2
8502 case TARGET_NR_pipe2:
e7ea6cbe
RH
8503 ret = do_pipe(cpu_env, arg1,
8504 target_to_host_bitmask(arg2, fcntl_flags_tbl), 1);
31e31b8a 8505 break;
099d6b0f 8506#endif
31e31b8a 8507 case TARGET_NR_times:
32f36bce 8508 {
53a5960a 8509 struct target_tms *tmsp;
32f36bce
FB
8510 struct tms tms;
8511 ret = get_errno(times(&tms));
53a5960a 8512 if (arg1) {
579a97f7
FB
8513 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
8514 if (!tmsp)
8515 goto efault;
cbb21eed
MB
8516 tmsp->tms_utime = tswapal(host_to_target_clock_t(tms.tms_utime));
8517 tmsp->tms_stime = tswapal(host_to_target_clock_t(tms.tms_stime));
8518 tmsp->tms_cutime = tswapal(host_to_target_clock_t(tms.tms_cutime));
8519 tmsp->tms_cstime = tswapal(host_to_target_clock_t(tms.tms_cstime));
32f36bce 8520 }
c596ed17
FB
8521 if (!is_error(ret))
8522 ret = host_to_target_clock_t(ret);
32f36bce
FB
8523 }
8524 break;
ebc05488 8525#ifdef TARGET_NR_prof
31e31b8a
FB
8526 case TARGET_NR_prof:
8527 goto unimplemented;
ebc05488 8528#endif
e5febef5 8529#ifdef TARGET_NR_signal
31e31b8a
FB
8530 case TARGET_NR_signal:
8531 goto unimplemented;
e5febef5 8532#endif
31e31b8a 8533 case TARGET_NR_acct:
38d840e6
AJ
8534 if (arg1 == 0) {
8535 ret = get_errno(acct(NULL));
8536 } else {
8537 if (!(p = lock_user_string(arg1)))
8538 goto efault;
8539 ret = get_errno(acct(path(p)));
8540 unlock_user(p, arg1, 0);
8541 }
24836689 8542 break;
8070e7be 8543#ifdef TARGET_NR_umount2
31e31b8a 8544 case TARGET_NR_umount2:
579a97f7
FB
8545 if (!(p = lock_user_string(arg1)))
8546 goto efault;
53a5960a
PB
8547 ret = get_errno(umount2(p, arg2));
8548 unlock_user(p, arg1, 0);
31e31b8a 8549 break;
7a3148a9 8550#endif
ebc05488 8551#ifdef TARGET_NR_lock
31e31b8a
FB
8552 case TARGET_NR_lock:
8553 goto unimplemented;
ebc05488 8554#endif
31e31b8a
FB
8555 case TARGET_NR_ioctl:
8556 ret = do_ioctl(arg1, arg2, arg3);
8557 break;
47ae93cd 8558#ifdef TARGET_NR_fcntl
31e31b8a 8559 case TARGET_NR_fcntl:
9ee1fa2c 8560 ret = do_fcntl(arg1, arg2, arg3);
31e31b8a 8561 break;
47ae93cd 8562#endif
ebc05488 8563#ifdef TARGET_NR_mpx
31e31b8a
FB
8564 case TARGET_NR_mpx:
8565 goto unimplemented;
ebc05488 8566#endif
31e31b8a
FB
8567 case TARGET_NR_setpgid:
8568 ret = get_errno(setpgid(arg1, arg2));
8569 break;
ebc05488 8570#ifdef TARGET_NR_ulimit
31e31b8a
FB
8571 case TARGET_NR_ulimit:
8572 goto unimplemented;
ebc05488
FB
8573#endif
8574#ifdef TARGET_NR_oldolduname
31e31b8a
FB
8575 case TARGET_NR_oldolduname:
8576 goto unimplemented;
ebc05488 8577#endif
31e31b8a
FB
8578 case TARGET_NR_umask:
8579 ret = get_errno(umask(arg1));
8580 break;
8581 case TARGET_NR_chroot:
579a97f7
FB
8582 if (!(p = lock_user_string(arg1)))
8583 goto efault;
53a5960a
PB
8584 ret = get_errno(chroot(p));
8585 unlock_user(p, arg1, 0);
31e31b8a 8586 break;
704eff6c 8587#ifdef TARGET_NR_ustat
31e31b8a
FB
8588 case TARGET_NR_ustat:
8589 goto unimplemented;
704eff6c
CG
8590#endif
8591#ifdef TARGET_NR_dup2
31e31b8a
FB
8592 case TARGET_NR_dup2:
8593 ret = get_errno(dup2(arg1, arg2));
e36800c9
LV
8594 if (ret >= 0) {
8595 fd_trans_dup(arg1, arg2);
8596 }
31e31b8a 8597 break;
704eff6c 8598#endif
d0927938
UH
8599#if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
8600 case TARGET_NR_dup3:
10fa993a
PM
8601 {
8602 int host_flags;
8603
8604 if ((arg3 & ~TARGET_O_CLOEXEC) != 0) {
8605 return -EINVAL;
8606 }
8607 host_flags = target_to_host_bitmask(arg3, fcntl_flags_tbl);
8608 ret = get_errno(dup3(arg1, arg2, host_flags));
e36800c9
LV
8609 if (ret >= 0) {
8610 fd_trans_dup(arg1, arg2);
8611 }
d0927938 8612 break;
10fa993a 8613 }
d0927938 8614#endif
7a3148a9 8615#ifdef TARGET_NR_getppid /* not on alpha */
31e31b8a
FB
8616 case TARGET_NR_getppid:
8617 ret = get_errno(getppid());
8618 break;
7a3148a9 8619#endif
704eff6c 8620#ifdef TARGET_NR_getpgrp
31e31b8a
FB
8621 case TARGET_NR_getpgrp:
8622 ret = get_errno(getpgrp());
8623 break;
704eff6c 8624#endif
31e31b8a
FB
8625 case TARGET_NR_setsid:
8626 ret = get_errno(setsid());
8627 break;
e5febef5 8628#ifdef TARGET_NR_sigaction
31e31b8a 8629 case TARGET_NR_sigaction:
31e31b8a 8630 {
6049f4f8
RH
8631#if defined(TARGET_ALPHA)
8632 struct target_sigaction act, oact, *pact = 0;
53a5960a 8633 struct target_old_sigaction *old_act;
53a5960a 8634 if (arg2) {
579a97f7
FB
8635 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
8636 goto efault;
66fb9763
FB
8637 act._sa_handler = old_act->_sa_handler;
8638 target_siginitset(&act.sa_mask, old_act->sa_mask);
8639 act.sa_flags = old_act->sa_flags;
6049f4f8 8640 act.sa_restorer = 0;
53a5960a 8641 unlock_user_struct(old_act, arg2, 0);
66fb9763 8642 pact = &act;
66fb9763
FB
8643 }
8644 ret = get_errno(do_sigaction(arg1, pact, &oact));
53a5960a 8645 if (!is_error(ret) && arg3) {
579a97f7
FB
8646 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
8647 goto efault;
53a5960a
PB
8648 old_act->_sa_handler = oact._sa_handler;
8649 old_act->sa_mask = oact.sa_mask.sig[0];
8650 old_act->sa_flags = oact.sa_flags;
53a5960a 8651 unlock_user_struct(old_act, arg3, 1);
66fb9763 8652 }
6049f4f8 8653#elif defined(TARGET_MIPS)
106ec879
FB
8654 struct target_sigaction act, oact, *pact, *old_act;
8655
8656 if (arg2) {
579a97f7
FB
8657 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
8658 goto efault;
106ec879
FB
8659 act._sa_handler = old_act->_sa_handler;
8660 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
8661 act.sa_flags = old_act->sa_flags;
8662 unlock_user_struct(old_act, arg2, 0);
8663 pact = &act;
8664 } else {
8665 pact = NULL;
8666 }
8667
8668 ret = get_errno(do_sigaction(arg1, pact, &oact));
8669
8670 if (!is_error(ret) && arg3) {
579a97f7
FB
8671 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
8672 goto efault;
106ec879
FB
8673 old_act->_sa_handler = oact._sa_handler;
8674 old_act->sa_flags = oact.sa_flags;
8675 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
8676 old_act->sa_mask.sig[1] = 0;
8677 old_act->sa_mask.sig[2] = 0;
8678 old_act->sa_mask.sig[3] = 0;
8679 unlock_user_struct(old_act, arg3, 1);
8680 }
6049f4f8
RH
8681#else
8682 struct target_old_sigaction *old_act;
8683 struct target_sigaction act, oact, *pact;
8684 if (arg2) {
8685 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
8686 goto efault;
8687 act._sa_handler = old_act->_sa_handler;
8688 target_siginitset(&act.sa_mask, old_act->sa_mask);
8689 act.sa_flags = old_act->sa_flags;
8690 act.sa_restorer = old_act->sa_restorer;
8691 unlock_user_struct(old_act, arg2, 0);
8692 pact = &act;
8693 } else {
8694 pact = NULL;
8695 }
8696 ret = get_errno(do_sigaction(arg1, pact, &oact));
8697 if (!is_error(ret) && arg3) {
8698 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
8699 goto efault;
8700 old_act->_sa_handler = oact._sa_handler;
8701 old_act->sa_mask = oact.sa_mask.sig[0];
8702 old_act->sa_flags = oact.sa_flags;
8703 old_act->sa_restorer = oact.sa_restorer;
8704 unlock_user_struct(old_act, arg3, 1);
8705 }
388bb21a 8706#endif
31e31b8a
FB
8707 }
8708 break;
e5febef5 8709#endif
66fb9763 8710 case TARGET_NR_rt_sigaction:
53a5960a 8711 {
6049f4f8 8712#if defined(TARGET_ALPHA)
78bfef72
PM
8713 /* For Alpha and SPARC this is a 5 argument syscall, with
8714 * a 'restorer' parameter which must be copied into the
8715 * sa_restorer field of the sigaction struct.
8716 * For Alpha that 'restorer' is arg5; for SPARC it is arg4,
8717 * and arg5 is the sigsetsize.
8718 * Alpha also has a separate rt_sigaction struct that it uses
8719 * here; SPARC uses the usual sigaction struct.
8720 */
6049f4f8 8721 struct target_rt_sigaction *rt_act;
78bfef72 8722 struct target_sigaction act, oact, *pact = 0;
c815701e
PM
8723
8724 if (arg4 != sizeof(target_sigset_t)) {
8725 ret = -TARGET_EINVAL;
8726 break;
8727 }
6049f4f8
RH
8728 if (arg2) {
8729 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
8730 goto efault;
8731 act._sa_handler = rt_act->_sa_handler;
8732 act.sa_mask = rt_act->sa_mask;
8733 act.sa_flags = rt_act->sa_flags;
8734 act.sa_restorer = arg5;
8735 unlock_user_struct(rt_act, arg2, 0);
8736 pact = &act;
8737 }
8738 ret = get_errno(do_sigaction(arg1, pact, &oact));
8739 if (!is_error(ret) && arg3) {
8740 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
8741 goto efault;
8742 rt_act->_sa_handler = oact._sa_handler;
8743 rt_act->sa_mask = oact.sa_mask;
8744 rt_act->sa_flags = oact.sa_flags;
8745 unlock_user_struct(rt_act, arg3, 1);
8746 }
8747#else
78bfef72
PM
8748#ifdef TARGET_SPARC
8749 target_ulong restorer = arg4;
8750 target_ulong sigsetsize = arg5;
8751#else
8752 target_ulong sigsetsize = arg4;
8753#endif
53a5960a
PB
8754 struct target_sigaction *act;
8755 struct target_sigaction *oact;
8756
78bfef72 8757 if (sigsetsize != sizeof(target_sigset_t)) {
c815701e
PM
8758 ret = -TARGET_EINVAL;
8759 break;
8760 }
579a97f7 8761 if (arg2) {
78bfef72 8762 if (!lock_user_struct(VERIFY_READ, act, arg2, 1)) {
579a97f7 8763 goto efault;
78bfef72
PM
8764 }
8765#ifdef TARGET_SPARC
8766 act->sa_restorer = restorer;
8767#endif
8768 } else {
53a5960a 8769 act = NULL;
78bfef72 8770 }
579a97f7
FB
8771 if (arg3) {
8772 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
8773 ret = -TARGET_EFAULT;
8774 goto rt_sigaction_fail;
8775 }
8776 } else
53a5960a
PB
8777 oact = NULL;
8778 ret = get_errno(do_sigaction(arg1, act, oact));
579a97f7
FB
8779 rt_sigaction_fail:
8780 if (act)
53a5960a 8781 unlock_user_struct(act, arg2, 0);
579a97f7 8782 if (oact)
53a5960a 8783 unlock_user_struct(oact, arg3, 1);
6049f4f8 8784#endif
53a5960a 8785 }
66fb9763 8786 break;
7a3148a9 8787#ifdef TARGET_NR_sgetmask /* not on alpha */
31e31b8a 8788 case TARGET_NR_sgetmask:
66fb9763
FB
8789 {
8790 sigset_t cur_set;
992f48a0 8791 abi_ulong target_set;
3d3efba0
PM
8792 ret = do_sigprocmask(0, NULL, &cur_set);
8793 if (!ret) {
8794 host_to_target_old_sigset(&target_set, &cur_set);
8795 ret = target_set;
8796 }
66fb9763
FB
8797 }
8798 break;
7a3148a9
JM
8799#endif
8800#ifdef TARGET_NR_ssetmask /* not on alpha */
31e31b8a 8801 case TARGET_NR_ssetmask:
66fb9763 8802 {
a8617d8c 8803 sigset_t set, oset;
992f48a0 8804 abi_ulong target_set = arg1;
66fb9763 8805 target_to_host_old_sigset(&set, &target_set);
3d3efba0
PM
8806 ret = do_sigprocmask(SIG_SETMASK, &set, &oset);
8807 if (!ret) {
8808 host_to_target_old_sigset(&target_set, &oset);
8809 ret = target_set;
8810 }
66fb9763
FB
8811 }
8812 break;
7a3148a9 8813#endif
e5febef5 8814#ifdef TARGET_NR_sigprocmask
66fb9763
FB
8815 case TARGET_NR_sigprocmask:
8816 {
a5b3b13b
RH
8817#if defined(TARGET_ALPHA)
8818 sigset_t set, oldset;
8819 abi_ulong mask;
8820 int how;
8821
8822 switch (arg1) {
8823 case TARGET_SIG_BLOCK:
8824 how = SIG_BLOCK;
8825 break;
8826 case TARGET_SIG_UNBLOCK:
8827 how = SIG_UNBLOCK;
8828 break;
8829 case TARGET_SIG_SETMASK:
8830 how = SIG_SETMASK;
8831 break;
8832 default:
8833 ret = -TARGET_EINVAL;
8834 goto fail;
8835 }
8836 mask = arg2;
8837 target_to_host_old_sigset(&set, &mask);
8838
3d3efba0 8839 ret = do_sigprocmask(how, &set, &oldset);
a5b3b13b
RH
8840 if (!is_error(ret)) {
8841 host_to_target_old_sigset(&mask, &oldset);
8842 ret = mask;
0229f5a3 8843 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0; /* force no error */
a5b3b13b
RH
8844 }
8845#else
66fb9763 8846 sigset_t set, oldset, *set_ptr;
a5b3b13b 8847 int how;
3b46e624 8848
53a5960a 8849 if (arg2) {
a5b3b13b 8850 switch (arg1) {
66fb9763
FB
8851 case TARGET_SIG_BLOCK:
8852 how = SIG_BLOCK;
8853 break;
8854 case TARGET_SIG_UNBLOCK:
8855 how = SIG_UNBLOCK;
8856 break;
8857 case TARGET_SIG_SETMASK:
8858 how = SIG_SETMASK;
8859 break;
8860 default:
0da46a6e 8861 ret = -TARGET_EINVAL;
66fb9763
FB
8862 goto fail;
8863 }
c227f099 8864 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
579a97f7 8865 goto efault;
53a5960a
PB
8866 target_to_host_old_sigset(&set, p);
8867 unlock_user(p, arg2, 0);
66fb9763
FB
8868 set_ptr = &set;
8869 } else {
8870 how = 0;
8871 set_ptr = NULL;
8872 }
3d3efba0 8873 ret = do_sigprocmask(how, set_ptr, &oldset);
53a5960a 8874 if (!is_error(ret) && arg3) {
c227f099 8875 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
579a97f7 8876 goto efault;
53a5960a 8877 host_to_target_old_sigset(p, &oldset);
c227f099 8878 unlock_user(p, arg3, sizeof(target_sigset_t));
66fb9763 8879 }
a5b3b13b 8880#endif
66fb9763
FB
8881 }
8882 break;
e5febef5 8883#endif
66fb9763
FB
8884 case TARGET_NR_rt_sigprocmask:
8885 {
8886 int how = arg1;
8887 sigset_t set, oldset, *set_ptr;
3b46e624 8888
c815701e
PM
8889 if (arg4 != sizeof(target_sigset_t)) {
8890 ret = -TARGET_EINVAL;
8891 break;
8892 }
8893
53a5960a 8894 if (arg2) {
66fb9763
FB
8895 switch(how) {
8896 case TARGET_SIG_BLOCK:
8897 how = SIG_BLOCK;
8898 break;
8899 case TARGET_SIG_UNBLOCK:
8900 how = SIG_UNBLOCK;
8901 break;
8902 case TARGET_SIG_SETMASK:
8903 how = SIG_SETMASK;
8904 break;
8905 default:
0da46a6e 8906 ret = -TARGET_EINVAL;
66fb9763
FB
8907 goto fail;
8908 }
c227f099 8909 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
579a97f7 8910 goto efault;
53a5960a
PB
8911 target_to_host_sigset(&set, p);
8912 unlock_user(p, arg2, 0);
66fb9763
FB
8913 set_ptr = &set;
8914 } else {
8915 how = 0;
8916 set_ptr = NULL;
8917 }
3d3efba0 8918 ret = do_sigprocmask(how, set_ptr, &oldset);
53a5960a 8919 if (!is_error(ret) && arg3) {
c227f099 8920 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
579a97f7 8921 goto efault;
53a5960a 8922 host_to_target_sigset(p, &oldset);
c227f099 8923 unlock_user(p, arg3, sizeof(target_sigset_t));
66fb9763
FB
8924 }
8925 }
8926 break;
e5febef5 8927#ifdef TARGET_NR_sigpending
66fb9763
FB
8928 case TARGET_NR_sigpending:
8929 {
8930 sigset_t set;
8931 ret = get_errno(sigpending(&set));
8932 if (!is_error(ret)) {
c227f099 8933 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
579a97f7 8934 goto efault;
53a5960a 8935 host_to_target_old_sigset(p, &set);
c227f099 8936 unlock_user(p, arg1, sizeof(target_sigset_t));
66fb9763
FB
8937 }
8938 }
8939 break;
e5febef5 8940#endif
66fb9763
FB
8941 case TARGET_NR_rt_sigpending:
8942 {
8943 sigset_t set;
c815701e
PM
8944
8945 /* Yes, this check is >, not != like most. We follow the kernel's
8946 * logic and it does it like this because it implements
8947 * NR_sigpending through the same code path, and in that case
8948 * the old_sigset_t is smaller in size.
8949 */
8950 if (arg2 > sizeof(target_sigset_t)) {
8951 ret = -TARGET_EINVAL;
8952 break;
8953 }
8954
66fb9763
FB
8955 ret = get_errno(sigpending(&set));
8956 if (!is_error(ret)) {
c227f099 8957 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
579a97f7 8958 goto efault;
53a5960a 8959 host_to_target_sigset(p, &set);
c227f099 8960 unlock_user(p, arg1, sizeof(target_sigset_t));
66fb9763
FB
8961 }
8962 }
8963 break;
e5febef5 8964#ifdef TARGET_NR_sigsuspend
66fb9763
FB
8965 case TARGET_NR_sigsuspend:
8966 {
3d3efba0 8967 TaskState *ts = cpu->opaque;
f43ce12b
RH
8968#if defined(TARGET_ALPHA)
8969 abi_ulong mask = arg1;
3d3efba0 8970 target_to_host_old_sigset(&ts->sigsuspend_mask, &mask);
f43ce12b 8971#else
c227f099 8972 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
579a97f7 8973 goto efault;
3d3efba0 8974 target_to_host_old_sigset(&ts->sigsuspend_mask, p);
53a5960a 8975 unlock_user(p, arg1, 0);
f43ce12b 8976#endif
3d3efba0
PM
8977 ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask,
8978 SIGSET_T_SIZE));
8979 if (ret != -TARGET_ERESTARTSYS) {
8980 ts->in_sigsuspend = 1;
8981 }
66fb9763
FB
8982 }
8983 break;
e5febef5 8984#endif
66fb9763
FB
8985 case TARGET_NR_rt_sigsuspend:
8986 {
3d3efba0 8987 TaskState *ts = cpu->opaque;
c815701e
PM
8988
8989 if (arg2 != sizeof(target_sigset_t)) {
8990 ret = -TARGET_EINVAL;
8991 break;
8992 }
c227f099 8993 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
579a97f7 8994 goto efault;
3d3efba0 8995 target_to_host_sigset(&ts->sigsuspend_mask, p);
53a5960a 8996 unlock_user(p, arg1, 0);
3d3efba0
PM
8997 ret = get_errno(safe_rt_sigsuspend(&ts->sigsuspend_mask,
8998 SIGSET_T_SIZE));
8999 if (ret != -TARGET_ERESTARTSYS) {
9000 ts->in_sigsuspend = 1;
9001 }
66fb9763
FB
9002 }
9003 break;
9004 case TARGET_NR_rt_sigtimedwait:
9005 {
66fb9763
FB
9006 sigset_t set;
9007 struct timespec uts, *puts;
9008 siginfo_t uinfo;
3b46e624 9009
c815701e
PM
9010 if (arg4 != sizeof(target_sigset_t)) {
9011 ret = -TARGET_EINVAL;
9012 break;
9013 }
9014
c227f099 9015 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
579a97f7 9016 goto efault;
53a5960a
PB
9017 target_to_host_sigset(&set, p);
9018 unlock_user(p, arg1, 0);
9019 if (arg3) {
66fb9763 9020 puts = &uts;
53a5960a 9021 target_to_host_timespec(puts, arg3);
66fb9763
FB
9022 } else {
9023 puts = NULL;
9024 }
b3f82330
PM
9025 ret = get_errno(safe_rt_sigtimedwait(&set, &uinfo, puts,
9026 SIGSET_T_SIZE));
974a196d
PJ
9027 if (!is_error(ret)) {
9028 if (arg2) {
9029 p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t),
9030 0);
9031 if (!p) {
9032 goto efault;
9033 }
9034 host_to_target_siginfo(p, &uinfo);
9035 unlock_user(p, arg2, sizeof(target_siginfo_t));
9036 }
9037 ret = host_to_target_signal(ret);
66fb9763
FB
9038 }
9039 }
9040 break;
9041 case TARGET_NR_rt_sigqueueinfo:
9042 {
9043 siginfo_t uinfo;
4debae6f
PM
9044
9045 p = lock_user(VERIFY_READ, arg3, sizeof(target_siginfo_t), 1);
9046 if (!p) {
579a97f7 9047 goto efault;
4debae6f 9048 }
53a5960a 9049 target_to_host_siginfo(&uinfo, p);
d8b6d892 9050 unlock_user(p, arg3, 0);
66fb9763
FB
9051 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
9052 }
9053 break;
cf8b8bfc
MS
9054 case TARGET_NR_rt_tgsigqueueinfo:
9055 {
9056 siginfo_t uinfo;
9057
9058 p = lock_user(VERIFY_READ, arg4, sizeof(target_siginfo_t), 1);
9059 if (!p) {
9060 goto efault;
9061 }
9062 target_to_host_siginfo(&uinfo, p);
9063 unlock_user(p, arg4, 0);
9064 ret = get_errno(sys_rt_tgsigqueueinfo(arg1, arg2, arg3, &uinfo));
9065 }
9066 break;
e5febef5 9067#ifdef TARGET_NR_sigreturn
66fb9763 9068 case TARGET_NR_sigreturn:
3d3efba0
PM
9069 if (block_signals()) {
9070 ret = -TARGET_ERESTARTSYS;
9071 } else {
9072 ret = do_sigreturn(cpu_env);
9073 }
66fb9763 9074 break;
e5febef5 9075#endif
66fb9763 9076 case TARGET_NR_rt_sigreturn:
3d3efba0
PM
9077 if (block_signals()) {
9078 ret = -TARGET_ERESTARTSYS;
9079 } else {
9080 ret = do_rt_sigreturn(cpu_env);
9081 }
66fb9763 9082 break;
31e31b8a 9083 case TARGET_NR_sethostname:
579a97f7
FB
9084 if (!(p = lock_user_string(arg1)))
9085 goto efault;
53a5960a
PB
9086 ret = get_errno(sethostname(p, arg2));
9087 unlock_user(p, arg1, 0);
31e31b8a
FB
9088 break;
9089 case TARGET_NR_setrlimit:
9de5e440 9090 {
e22b7015 9091 int resource = target_to_host_resource(arg1);
53a5960a 9092 struct target_rlimit *target_rlim;
9de5e440 9093 struct rlimit rlim;
579a97f7
FB
9094 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
9095 goto efault;
81bbe906
TY
9096 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
9097 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
53a5960a 9098 unlock_user_struct(target_rlim, arg2, 0);
9de5e440
FB
9099 ret = get_errno(setrlimit(resource, &rlim));
9100 }
9101 break;
31e31b8a 9102 case TARGET_NR_getrlimit:
9de5e440 9103 {
e22b7015 9104 int resource = target_to_host_resource(arg1);
53a5960a 9105 struct target_rlimit *target_rlim;
9de5e440 9106 struct rlimit rlim;
3b46e624 9107
9de5e440
FB
9108 ret = get_errno(getrlimit(resource, &rlim));
9109 if (!is_error(ret)) {
579a97f7
FB
9110 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
9111 goto efault;
81bbe906
TY
9112 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
9113 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
53a5960a 9114 unlock_user_struct(target_rlim, arg2, 1);
9de5e440
FB
9115 }
9116 }
9117 break;
31e31b8a 9118 case TARGET_NR_getrusage:
b409186b
FB
9119 {
9120 struct rusage rusage;
b409186b
FB
9121 ret = get_errno(getrusage(arg1, &rusage));
9122 if (!is_error(ret)) {
a39fb273 9123 ret = host_to_target_rusage(arg2, &rusage);
b409186b
FB
9124 }
9125 }
9126 break;
31e31b8a
FB
9127 case TARGET_NR_gettimeofday:
9128 {
31e31b8a
FB
9129 struct timeval tv;
9130 ret = get_errno(gettimeofday(&tv, NULL));
9131 if (!is_error(ret)) {
788f5ec4
TS
9132 if (copy_to_user_timeval(arg1, &tv))
9133 goto efault;
31e31b8a
FB
9134 }
9135 }
9136 break;
9137 case TARGET_NR_settimeofday:
9138 {
b67d8031 9139 struct timeval tv, *ptv = NULL;
ef4467e9
PB
9140 struct timezone tz, *ptz = NULL;
9141
b67d8031
PB
9142 if (arg1) {
9143 if (copy_from_user_timeval(&tv, arg1)) {
9144 goto efault;
9145 }
9146 ptv = &tv;
9147 }
ef4467e9
PB
9148
9149 if (arg2) {
9150 if (copy_from_user_timezone(&tz, arg2)) {
9151 goto efault;
9152 }
9153 ptz = &tz;
9154 }
9155
b67d8031 9156 ret = get_errno(settimeofday(ptv, ptz));
31e31b8a
FB
9157 }
9158 break;
9468a5d4 9159#if defined(TARGET_NR_select)
31e31b8a 9160 case TARGET_NR_select:
5457dc9e
LV
9161#if defined(TARGET_WANT_NI_OLD_SELECT)
9162 /* some architectures used to have old_select here
9163 * but now ENOSYS it.
9164 */
9165 ret = -TARGET_ENOSYS;
9166#elif defined(TARGET_WANT_OLD_SYS_SELECT)
9167 ret = do_old_select(arg1);
9468a5d4 9168#else
5457dc9e 9169 ret = do_select(arg1, arg2, arg3, arg4, arg5);
9468a5d4 9170#endif
f2674e31 9171 break;
9e42382f
RV
9172#endif
9173#ifdef TARGET_NR_pselect6
9174 case TARGET_NR_pselect6:
055e0906
MF
9175 {
9176 abi_long rfd_addr, wfd_addr, efd_addr, n, ts_addr;
9177 fd_set rfds, wfds, efds;
9178 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
9179 struct timespec ts, *ts_ptr;
9180
9181 /*
9182 * The 6th arg is actually two args smashed together,
9183 * so we cannot use the C library.
9184 */
9185 sigset_t set;
9186 struct {
9187 sigset_t *set;
9188 size_t size;
9189 } sig, *sig_ptr;
9190
9191 abi_ulong arg_sigset, arg_sigsize, *arg7;
9192 target_sigset_t *target_sigset;
9193
9194 n = arg1;
9195 rfd_addr = arg2;
9196 wfd_addr = arg3;
9197 efd_addr = arg4;
9198 ts_addr = arg5;
9199
9200 ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
9201 if (ret) {
9202 goto fail;
9203 }
9204 ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
9205 if (ret) {
9206 goto fail;
9207 }
9208 ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
9209 if (ret) {
9210 goto fail;
9211 }
9212
9213 /*
9214 * This takes a timespec, and not a timeval, so we cannot
9215 * use the do_select() helper ...
9216 */
9217 if (ts_addr) {
9218 if (target_to_host_timespec(&ts, ts_addr)) {
9219 goto efault;
9220 }
9221 ts_ptr = &ts;
9222 } else {
9223 ts_ptr = NULL;
9224 }
9225
9226 /* Extract the two packed args for the sigset */
9227 if (arg6) {
9228 sig_ptr = &sig;
b28a1f33 9229 sig.size = SIGSET_T_SIZE;
055e0906
MF
9230
9231 arg7 = lock_user(VERIFY_READ, arg6, sizeof(*arg7) * 2, 1);
9232 if (!arg7) {
9233 goto efault;
9234 }
cbb21eed
MB
9235 arg_sigset = tswapal(arg7[0]);
9236 arg_sigsize = tswapal(arg7[1]);
055e0906
MF
9237 unlock_user(arg7, arg6, 0);
9238
9239 if (arg_sigset) {
9240 sig.set = &set;
8f04eeb3
PM
9241 if (arg_sigsize != sizeof(*target_sigset)) {
9242 /* Like the kernel, we enforce correct size sigsets */
9243 ret = -TARGET_EINVAL;
9244 goto fail;
9245 }
055e0906
MF
9246 target_sigset = lock_user(VERIFY_READ, arg_sigset,
9247 sizeof(*target_sigset), 1);
9248 if (!target_sigset) {
9249 goto efault;
9250 }
9251 target_to_host_sigset(&set, target_sigset);
9252 unlock_user(target_sigset, arg_sigset, 0);
9253 } else {
9254 sig.set = NULL;
9255 }
9256 } else {
9257 sig_ptr = NULL;
9258 }
9259
6df9d38d
PM
9260 ret = get_errno(safe_pselect6(n, rfds_ptr, wfds_ptr, efds_ptr,
9261 ts_ptr, sig_ptr));
055e0906
MF
9262
9263 if (!is_error(ret)) {
9264 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
9265 goto efault;
9266 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
9267 goto efault;
9268 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
9269 goto efault;
9270
9271 if (ts_addr && host_to_target_timespec(ts_addr, &ts))
9272 goto efault;
9273 }
9274 }
9275 break;
048f6b4d 9276#endif
704eff6c 9277#ifdef TARGET_NR_symlink
31e31b8a 9278 case TARGET_NR_symlink:
53a5960a
PB
9279 {
9280 void *p2;
9281 p = lock_user_string(arg1);
9282 p2 = lock_user_string(arg2);
579a97f7
FB
9283 if (!p || !p2)
9284 ret = -TARGET_EFAULT;
9285 else
9286 ret = get_errno(symlink(p, p2));
53a5960a
PB
9287 unlock_user(p2, arg2, 0);
9288 unlock_user(p, arg1, 0);
9289 }
31e31b8a 9290 break;
704eff6c 9291#endif
c0d472b1 9292#if defined(TARGET_NR_symlinkat)
f0b6243d 9293 case TARGET_NR_symlinkat:
f0b6243d 9294 {
579a97f7 9295 void *p2;
f0b6243d
TS
9296 p = lock_user_string(arg1);
9297 p2 = lock_user_string(arg3);
579a97f7 9298 if (!p || !p2)
0da46a6e 9299 ret = -TARGET_EFAULT;
f0b6243d 9300 else
c0d472b1 9301 ret = get_errno(symlinkat(p, arg2, p2));
579a97f7
FB
9302 unlock_user(p2, arg3, 0);
9303 unlock_user(p, arg1, 0);
f0b6243d
TS
9304 }
9305 break;
9306#endif
ebc05488 9307#ifdef TARGET_NR_oldlstat
31e31b8a
FB
9308 case TARGET_NR_oldlstat:
9309 goto unimplemented;
ebc05488 9310#endif
704eff6c 9311#ifdef TARGET_NR_readlink
31e31b8a 9312 case TARGET_NR_readlink:
53a5960a 9313 {
463d8e73 9314 void *p2;
53a5960a 9315 p = lock_user_string(arg1);
579a97f7 9316 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
463d8e73 9317 if (!p || !p2) {
579a97f7 9318 ret = -TARGET_EFAULT;
f17f4989
MF
9319 } else if (!arg3) {
9320 /* Short circuit this for the magic exe check. */
9321 ret = -TARGET_EINVAL;
463d8e73
AS
9322 } else if (is_proc_myself((const char *)p, "exe")) {
9323 char real[PATH_MAX], *temp;
9324 temp = realpath(exec_path, real);
f17f4989
MF
9325 /* Return value is # of bytes that we wrote to the buffer. */
9326 if (temp == NULL) {
9327 ret = get_errno(-1);
9328 } else {
9329 /* Don't worry about sign mismatch as earlier mapping
9330 * logic would have thrown a bad address error. */
9331 ret = MIN(strlen(real), arg3);
9332 /* We cannot NUL terminate the string. */
9333 memcpy(p2, real, ret);
9334 }
463d8e73
AS
9335 } else {
9336 ret = get_errno(readlink(path(p), p2, arg3));
d088d664 9337 }
53a5960a
PB
9338 unlock_user(p2, arg2, ret);
9339 unlock_user(p, arg1, 0);
9340 }
31e31b8a 9341 break;
704eff6c 9342#endif
c0d472b1 9343#if defined(TARGET_NR_readlinkat)
5e0ccb18 9344 case TARGET_NR_readlinkat:
5e0ccb18 9345 {
579a97f7 9346 void *p2;
5e0ccb18 9347 p = lock_user_string(arg2);
579a97f7 9348 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
463d8e73
AS
9349 if (!p || !p2) {
9350 ret = -TARGET_EFAULT;
9351 } else if (is_proc_myself((const char *)p, "exe")) {
9352 char real[PATH_MAX], *temp;
9353 temp = realpath(exec_path, real);
9354 ret = temp == NULL ? get_errno(-1) : strlen(real) ;
9355 snprintf((char *)p2, arg4, "%s", real);
9356 } else {
c0d472b1 9357 ret = get_errno(readlinkat(arg1, path(p), p2, arg4));
463d8e73 9358 }
579a97f7
FB
9359 unlock_user(p2, arg3, ret);
9360 unlock_user(p, arg2, 0);
5e0ccb18
TS
9361 }
9362 break;
9363#endif
e5febef5 9364#ifdef TARGET_NR_uselib
31e31b8a
FB
9365 case TARGET_NR_uselib:
9366 goto unimplemented;
e5febef5
TS
9367#endif
9368#ifdef TARGET_NR_swapon
31e31b8a 9369 case TARGET_NR_swapon:
579a97f7
FB
9370 if (!(p = lock_user_string(arg1)))
9371 goto efault;
53a5960a
PB
9372 ret = get_errno(swapon(p, arg2));
9373 unlock_user(p, arg1, 0);
31e31b8a 9374 break;
e5febef5 9375#endif
31e31b8a 9376 case TARGET_NR_reboot:
c07ecc68
LV
9377 if (arg3 == LINUX_REBOOT_CMD_RESTART2) {
9378 /* arg4 must be ignored in all other cases */
9379 p = lock_user_string(arg4);
9380 if (!p) {
9381 goto efault;
9382 }
9383 ret = get_errno(reboot(arg1, arg2, arg3, p));
9384 unlock_user(p, arg4, 0);
9385 } else {
9386 ret = get_errno(reboot(arg1, arg2, arg3, NULL));
9387 }
0f6b4d21 9388 break;
e5febef5 9389#ifdef TARGET_NR_readdir
31e31b8a
FB
9390 case TARGET_NR_readdir:
9391 goto unimplemented;
e5febef5
TS
9392#endif
9393#ifdef TARGET_NR_mmap
31e31b8a 9394 case TARGET_NR_mmap:
09701199
AG
9395#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || \
9396 (defined(TARGET_ARM) && defined(TARGET_ABI32)) || \
a4c075f1
UH
9397 defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE) \
9398 || defined(TARGET_S390X)
31e31b8a 9399 {
992f48a0
BS
9400 abi_ulong *v;
9401 abi_ulong v1, v2, v3, v4, v5, v6;
579a97f7
FB
9402 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
9403 goto efault;
cbb21eed
MB
9404 v1 = tswapal(v[0]);
9405 v2 = tswapal(v[1]);
9406 v3 = tswapal(v[2]);
9407 v4 = tswapal(v[3]);
9408 v5 = tswapal(v[4]);
9409 v6 = tswapal(v[5]);
53a5960a 9410 unlock_user(v, arg1, 0);
5fafdf24 9411 ret = get_errno(target_mmap(v1, v2, v3,
5286db75
FB
9412 target_to_host_bitmask(v4, mmap_flags_tbl),
9413 v5, v6));
31e31b8a 9414 }
31e31b8a 9415#else
5fafdf24
TS
9416 ret = get_errno(target_mmap(arg1, arg2, arg3,
9417 target_to_host_bitmask(arg4, mmap_flags_tbl),
6fb883e8
FB
9418 arg5,
9419 arg6));
31e31b8a 9420#endif
6fb883e8 9421 break;
e5febef5 9422#endif
a315a145 9423#ifdef TARGET_NR_mmap2
6fb883e8 9424 case TARGET_NR_mmap2:
bb7ec043 9425#ifndef MMAP_SHIFT
c573ff67 9426#define MMAP_SHIFT 12
c573ff67 9427#endif
5fafdf24
TS
9428 ret = get_errno(target_mmap(arg1, arg2, arg3,
9429 target_to_host_bitmask(arg4, mmap_flags_tbl),
5286db75 9430 arg5,
c573ff67 9431 arg6 << MMAP_SHIFT));
31e31b8a 9432 break;
a315a145 9433#endif
31e31b8a 9434 case TARGET_NR_munmap:
54936004 9435 ret = get_errno(target_munmap(arg1, arg2));
31e31b8a 9436 break;
9de5e440 9437 case TARGET_NR_mprotect:
97374d38 9438 {
0429a971 9439 TaskState *ts = cpu->opaque;
97374d38
PB
9440 /* Special hack to detect libc making the stack executable. */
9441 if ((arg3 & PROT_GROWSDOWN)
9442 && arg1 >= ts->info->stack_limit
9443 && arg1 <= ts->info->start_stack) {
9444 arg3 &= ~PROT_GROWSDOWN;
9445 arg2 = arg2 + arg1 - ts->info->stack_limit;
9446 arg1 = ts->info->stack_limit;
9447 }
9448 }
54936004 9449 ret = get_errno(target_mprotect(arg1, arg2, arg3));
9de5e440 9450 break;
e5febef5 9451#ifdef TARGET_NR_mremap
9de5e440 9452 case TARGET_NR_mremap:
54936004 9453 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
9de5e440 9454 break;
e5febef5 9455#endif
53a5960a 9456 /* ??? msync/mlock/munlock are broken for softmmu. */
e5febef5 9457#ifdef TARGET_NR_msync
9de5e440 9458 case TARGET_NR_msync:
53a5960a 9459 ret = get_errno(msync(g2h(arg1), arg2, arg3));
9de5e440 9460 break;
e5febef5
TS
9461#endif
9462#ifdef TARGET_NR_mlock
9de5e440 9463 case TARGET_NR_mlock:
53a5960a 9464 ret = get_errno(mlock(g2h(arg1), arg2));
9de5e440 9465 break;
e5febef5
TS
9466#endif
9467#ifdef TARGET_NR_munlock
9de5e440 9468 case TARGET_NR_munlock:
53a5960a 9469 ret = get_errno(munlock(g2h(arg1), arg2));
9de5e440 9470 break;
e5febef5
TS
9471#endif
9472#ifdef TARGET_NR_mlockall
9de5e440 9473 case TARGET_NR_mlockall:
6f6a4032 9474 ret = get_errno(mlockall(target_to_host_mlockall_arg(arg1)));
9de5e440 9475 break;
e5febef5
TS
9476#endif
9477#ifdef TARGET_NR_munlockall
9de5e440
FB
9478 case TARGET_NR_munlockall:
9479 ret = get_errno(munlockall());
9480 break;
e5febef5 9481#endif
31e31b8a 9482 case TARGET_NR_truncate:
579a97f7
FB
9483 if (!(p = lock_user_string(arg1)))
9484 goto efault;
53a5960a
PB
9485 ret = get_errno(truncate(p, arg2));
9486 unlock_user(p, arg1, 0);
31e31b8a
FB
9487 break;
9488 case TARGET_NR_ftruncate:
9489 ret = get_errno(ftruncate(arg1, arg2));
9490 break;
9491 case TARGET_NR_fchmod:
9492 ret = get_errno(fchmod(arg1, arg2));
9493 break;
c0d472b1 9494#if defined(TARGET_NR_fchmodat)
814d7977 9495 case TARGET_NR_fchmodat:
579a97f7
FB
9496 if (!(p = lock_user_string(arg2)))
9497 goto efault;
c0d472b1 9498 ret = get_errno(fchmodat(arg1, p, arg3, 0));
579a97f7 9499 unlock_user(p, arg2, 0);
814d7977
TS
9500 break;
9501#endif
31e31b8a 9502 case TARGET_NR_getpriority:
95c09828
RH
9503 /* Note that negative values are valid for getpriority, so we must
9504 differentiate based on errno settings. */
9505 errno = 0;
9506 ret = getpriority(arg1, arg2);
9507 if (ret == -1 && errno != 0) {
9508 ret = -host_to_target_errno(errno);
9509 break;
9510 }
9511#ifdef TARGET_ALPHA
9512 /* Return value is the unbiased priority. Signal no error. */
9513 ((CPUAlphaState *)cpu_env)->ir[IR_V0] = 0;
9514#else
9515 /* Return value is a biased priority to avoid negative numbers. */
9516 ret = 20 - ret;
9517#endif
31e31b8a
FB
9518 break;
9519 case TARGET_NR_setpriority:
9520 ret = get_errno(setpriority(arg1, arg2, arg3));
9521 break;
ebc05488 9522#ifdef TARGET_NR_profil
31e31b8a
FB
9523 case TARGET_NR_profil:
9524 goto unimplemented;
ebc05488 9525#endif
31e31b8a 9526 case TARGET_NR_statfs:
579a97f7
FB
9527 if (!(p = lock_user_string(arg1)))
9528 goto efault;
53a5960a
PB
9529 ret = get_errno(statfs(path(p), &stfs));
9530 unlock_user(p, arg1, 0);
31e31b8a
FB
9531 convert_statfs:
9532 if (!is_error(ret)) {
53a5960a 9533 struct target_statfs *target_stfs;
3b46e624 9534
579a97f7
FB
9535 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
9536 goto efault;
9537 __put_user(stfs.f_type, &target_stfs->f_type);
9538 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
9539 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
9540 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
9541 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
9542 __put_user(stfs.f_files, &target_stfs->f_files);
9543 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
9544 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
9545 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
9546 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
229d3376 9547 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
d4247ec2
SL
9548#ifdef _STATFS_F_FLAGS
9549 __put_user(stfs.f_flags, &target_stfs->f_flags);
9550#else
9551 __put_user(0, &target_stfs->f_flags);
9552#endif
229d3376 9553 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
53a5960a 9554 unlock_user_struct(target_stfs, arg2, 1);
31e31b8a
FB
9555 }
9556 break;
9557 case TARGET_NR_fstatfs:
56c8f68f 9558 ret = get_errno(fstatfs(arg1, &stfs));
31e31b8a 9559 goto convert_statfs;
56c8f68f
FB
9560#ifdef TARGET_NR_statfs64
9561 case TARGET_NR_statfs64:
579a97f7
FB
9562 if (!(p = lock_user_string(arg1)))
9563 goto efault;
53a5960a
PB
9564 ret = get_errno(statfs(path(p), &stfs));
9565 unlock_user(p, arg1, 0);
56c8f68f
FB
9566 convert_statfs64:
9567 if (!is_error(ret)) {
53a5960a 9568 struct target_statfs64 *target_stfs;
3b46e624 9569
579a97f7
FB
9570 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
9571 goto efault;
9572 __put_user(stfs.f_type, &target_stfs->f_type);
9573 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
9574 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
9575 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
9576 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
9577 __put_user(stfs.f_files, &target_stfs->f_files);
9578 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
9579 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
9580 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
9581 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
229d3376
AG
9582 __put_user(stfs.f_frsize, &target_stfs->f_frsize);
9583 memset(target_stfs->f_spare, 0, sizeof(target_stfs->f_spare));
579a97f7 9584 unlock_user_struct(target_stfs, arg3, 1);
56c8f68f
FB
9585 }
9586 break;
9587 case TARGET_NR_fstatfs64:
9588 ret = get_errno(fstatfs(arg1, &stfs));
9589 goto convert_statfs64;
9590#endif
ebc05488 9591#ifdef TARGET_NR_ioperm
31e31b8a
FB
9592 case TARGET_NR_ioperm:
9593 goto unimplemented;
ebc05488 9594#endif
e5febef5 9595#ifdef TARGET_NR_socketcall
31e31b8a 9596 case TARGET_NR_socketcall:
53a5960a 9597 ret = do_socketcall(arg1, arg2);
31e31b8a 9598 break;
e5febef5 9599#endif
3532fa74
FB
9600#ifdef TARGET_NR_accept
9601 case TARGET_NR_accept:
a94b4987
PM
9602 ret = do_accept4(arg1, arg2, arg3, 0);
9603 break;
9604#endif
9605#ifdef TARGET_NR_accept4
9606 case TARGET_NR_accept4:
a94b4987 9607 ret = do_accept4(arg1, arg2, arg3, arg4);
3532fa74
FB
9608 break;
9609#endif
9610#ifdef TARGET_NR_bind
9611 case TARGET_NR_bind:
9612 ret = do_bind(arg1, arg2, arg3);
9613 break;
9614#endif
9615#ifdef TARGET_NR_connect
9616 case TARGET_NR_connect:
9617 ret = do_connect(arg1, arg2, arg3);
9618 break;
9619#endif
9620#ifdef TARGET_NR_getpeername
9621 case TARGET_NR_getpeername:
1be9e1dc 9622 ret = do_getpeername(arg1, arg2, arg3);
3532fa74
FB
9623 break;
9624#endif
9625#ifdef TARGET_NR_getsockname
9626 case TARGET_NR_getsockname:
1be9e1dc 9627 ret = do_getsockname(arg1, arg2, arg3);
3532fa74
FB
9628 break;
9629#endif
9630#ifdef TARGET_NR_getsockopt
9631 case TARGET_NR_getsockopt:
9632 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
9633 break;
9634#endif
9635#ifdef TARGET_NR_listen
9636 case TARGET_NR_listen:
1be9e1dc 9637 ret = get_errno(listen(arg1, arg2));
3532fa74
FB
9638 break;
9639#endif
9640#ifdef TARGET_NR_recv
9641 case TARGET_NR_recv:
214201bd 9642 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
3532fa74
FB
9643 break;
9644#endif
9645#ifdef TARGET_NR_recvfrom
9646 case TARGET_NR_recvfrom:
214201bd 9647 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
3532fa74
FB
9648 break;
9649#endif
9650#ifdef TARGET_NR_recvmsg
9651 case TARGET_NR_recvmsg:
9652 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
9653 break;
9654#endif
9655#ifdef TARGET_NR_send
9656 case TARGET_NR_send:
1be9e1dc 9657 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
3532fa74
FB
9658 break;
9659#endif
9660#ifdef TARGET_NR_sendmsg
9661 case TARGET_NR_sendmsg:
9662 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
9663 break;
9664#endif
f19e00d7
AG
9665#ifdef TARGET_NR_sendmmsg
9666 case TARGET_NR_sendmmsg:
9667 ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 1);
9668 break;
9669 case TARGET_NR_recvmmsg:
9670 ret = do_sendrecvmmsg(arg1, arg2, arg3, arg4, 0);
9671 break;
9672#endif
3532fa74
FB
9673#ifdef TARGET_NR_sendto
9674 case TARGET_NR_sendto:
1be9e1dc 9675 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
3532fa74
FB
9676 break;
9677#endif
9678#ifdef TARGET_NR_shutdown
9679 case TARGET_NR_shutdown:
1be9e1dc 9680 ret = get_errno(shutdown(arg1, arg2));
3532fa74
FB
9681 break;
9682#endif
f894efd1
LV
9683#if defined(TARGET_NR_getrandom) && defined(__NR_getrandom)
9684 case TARGET_NR_getrandom:
9685 p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
9686 if (!p) {
9687 goto efault;
9688 }
9689 ret = get_errno(getrandom(p, arg2, arg3));
9690 unlock_user(p, arg1, ret);
9691 break;
9692#endif
3532fa74
FB
9693#ifdef TARGET_NR_socket
9694 case TARGET_NR_socket:
9695 ret = do_socket(arg1, arg2, arg3);
9696 break;
9697#endif
9698#ifdef TARGET_NR_socketpair
9699 case TARGET_NR_socketpair:
1be9e1dc 9700 ret = do_socketpair(arg1, arg2, arg3, arg4);
3532fa74
FB
9701 break;
9702#endif
9703#ifdef TARGET_NR_setsockopt
9704 case TARGET_NR_setsockopt:
9705 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
9706 break;
9707#endif
da2c8ad7 9708#if defined(TARGET_NR_syslog)
31e31b8a 9709 case TARGET_NR_syslog:
da2c8ad7
AM
9710 {
9711 int len = arg2;
7494b0f9 9712
da2c8ad7
AM
9713 switch (arg1) {
9714 case TARGET_SYSLOG_ACTION_CLOSE: /* Close log */
9715 case TARGET_SYSLOG_ACTION_OPEN: /* Open log */
9716 case TARGET_SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
9717 case TARGET_SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging */
9718 case TARGET_SYSLOG_ACTION_CONSOLE_ON: /* Enable logging */
9719 case TARGET_SYSLOG_ACTION_CONSOLE_LEVEL: /* Set messages level */
9720 case TARGET_SYSLOG_ACTION_SIZE_UNREAD: /* Number of chars */
9721 case TARGET_SYSLOG_ACTION_SIZE_BUFFER: /* Size of the buffer */
9722 {
9723 ret = get_errno(sys_syslog((int)arg1, NULL, (int)arg3));
9724 }
9725 break;
9726 case TARGET_SYSLOG_ACTION_READ: /* Read from log */
9727 case TARGET_SYSLOG_ACTION_READ_CLEAR: /* Read/clear msgs */
9728 case TARGET_SYSLOG_ACTION_READ_ALL: /* Read last messages */
9729 {
9730 ret = -TARGET_EINVAL;
9731 if (len < 0) {
9732 goto fail;
9733 }
9734 ret = 0;
9735 if (len == 0) {
9736 break;
9737 }
9738 p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
9739 if (!p) {
9740 ret = -TARGET_EFAULT;
9741 goto fail;
9742 }
9743 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
9744 unlock_user(p, arg2, arg3);
9745 }
9746 break;
9747 default:
9748 ret = -EINVAL;
9749 break;
9750 }
9751 }
9752 break;
9753#endif
31e31b8a 9754 case TARGET_NR_setitimer:
66fb9763 9755 {
66fb9763
FB
9756 struct itimerval value, ovalue, *pvalue;
9757
53a5960a 9758 if (arg2) {
66fb9763 9759 pvalue = &value;
788f5ec4
TS
9760 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
9761 || copy_from_user_timeval(&pvalue->it_value,
9762 arg2 + sizeof(struct target_timeval)))
9763 goto efault;
66fb9763
FB
9764 } else {
9765 pvalue = NULL;
9766 }
9767 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
53a5960a 9768 if (!is_error(ret) && arg3) {
788f5ec4
TS
9769 if (copy_to_user_timeval(arg3,
9770 &ovalue.it_interval)
9771 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
9772 &ovalue.it_value))
9773 goto efault;
66fb9763
FB
9774 }
9775 }
9776 break;
31e31b8a 9777 case TARGET_NR_getitimer:
66fb9763 9778 {
66fb9763 9779 struct itimerval value;
3b46e624 9780
66fb9763 9781 ret = get_errno(getitimer(arg1, &value));
53a5960a 9782 if (!is_error(ret) && arg2) {
788f5ec4
TS
9783 if (copy_to_user_timeval(arg2,
9784 &value.it_interval)
9785 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
9786 &value.it_value))
9787 goto efault;
66fb9763
FB
9788 }
9789 }
9790 break;
704eff6c 9791#ifdef TARGET_NR_stat
31e31b8a 9792 case TARGET_NR_stat:
579a97f7
FB
9793 if (!(p = lock_user_string(arg1)))
9794 goto efault;
53a5960a
PB
9795 ret = get_errno(stat(path(p), &st));
9796 unlock_user(p, arg1, 0);
31e31b8a 9797 goto do_stat;
704eff6c
CG
9798#endif
9799#ifdef TARGET_NR_lstat
31e31b8a 9800 case TARGET_NR_lstat:
579a97f7
FB
9801 if (!(p = lock_user_string(arg1)))
9802 goto efault;
53a5960a
PB
9803 ret = get_errno(lstat(path(p), &st));
9804 unlock_user(p, arg1, 0);
31e31b8a 9805 goto do_stat;
704eff6c 9806#endif
31e31b8a
FB
9807 case TARGET_NR_fstat:
9808 {
9809 ret = get_errno(fstat(arg1, &st));
704eff6c 9810#if defined(TARGET_NR_stat) || defined(TARGET_NR_lstat)
31e31b8a 9811 do_stat:
704eff6c 9812#endif
31e31b8a 9813 if (!is_error(ret)) {
53a5960a 9814 struct target_stat *target_st;
e3584658 9815
579a97f7
FB
9816 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
9817 goto efault;
12727917 9818 memset(target_st, 0, sizeof(*target_st));
d2fd1af7
FB
9819 __put_user(st.st_dev, &target_st->st_dev);
9820 __put_user(st.st_ino, &target_st->st_ino);
9821 __put_user(st.st_mode, &target_st->st_mode);
9822 __put_user(st.st_uid, &target_st->st_uid);
9823 __put_user(st.st_gid, &target_st->st_gid);
9824 __put_user(st.st_nlink, &target_st->st_nlink);
9825 __put_user(st.st_rdev, &target_st->st_rdev);
9826 __put_user(st.st_size, &target_st->st_size);
9827 __put_user(st.st_blksize, &target_st->st_blksize);
9828 __put_user(st.st_blocks, &target_st->st_blocks);
9829 __put_user(st.st_atime, &target_st->target_st_atime);
9830 __put_user(st.st_mtime, &target_st->target_st_mtime);
9831 __put_user(st.st_ctime, &target_st->target_st_ctime);
53a5960a 9832 unlock_user_struct(target_st, arg2, 1);
31e31b8a
FB
9833 }
9834 }
9835 break;
ebc05488 9836#ifdef TARGET_NR_olduname
31e31b8a
FB
9837 case TARGET_NR_olduname:
9838 goto unimplemented;
ebc05488
FB
9839#endif
9840#ifdef TARGET_NR_iopl
31e31b8a
FB
9841 case TARGET_NR_iopl:
9842 goto unimplemented;
ebc05488 9843#endif
31e31b8a
FB
9844 case TARGET_NR_vhangup:
9845 ret = get_errno(vhangup());
9846 break;
ebc05488 9847#ifdef TARGET_NR_idle
31e31b8a
FB
9848 case TARGET_NR_idle:
9849 goto unimplemented;
42ad6ae9
FB
9850#endif
9851#ifdef TARGET_NR_syscall
9852 case TARGET_NR_syscall:
5945cfcb
PM
9853 ret = do_syscall(cpu_env, arg1 & 0xffff, arg2, arg3, arg4, arg5,
9854 arg6, arg7, arg8, 0);
9855 break;
ebc05488 9856#endif
31e31b8a
FB
9857 case TARGET_NR_wait4:
9858 {
9859 int status;
992f48a0 9860 abi_long status_ptr = arg2;
31e31b8a 9861 struct rusage rusage, *rusage_ptr;
992f48a0 9862 abi_ulong target_rusage = arg4;
a39fb273 9863 abi_long rusage_err;
31e31b8a
FB
9864 if (target_rusage)
9865 rusage_ptr = &rusage;
9866 else
9867 rusage_ptr = NULL;
4af80a37 9868 ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
31e31b8a 9869 if (!is_error(ret)) {
5379557b 9870 if (status_ptr && ret) {
1d9d8b55 9871 status = host_to_target_waitstatus(status);
2f619698
FB
9872 if (put_user_s32(status, status_ptr))
9873 goto efault;
31e31b8a 9874 }
a39fb273
PJ
9875 if (target_rusage) {
9876 rusage_err = host_to_target_rusage(target_rusage, &rusage);
9877 if (rusage_err) {
9878 ret = rusage_err;
9879 }
9880 }
31e31b8a
FB
9881 }
9882 }
9883 break;
e5febef5 9884#ifdef TARGET_NR_swapoff
31e31b8a 9885 case TARGET_NR_swapoff:
579a97f7
FB
9886 if (!(p = lock_user_string(arg1)))
9887 goto efault;
53a5960a
PB
9888 ret = get_errno(swapoff(p));
9889 unlock_user(p, arg1, 0);
31e31b8a 9890 break;
e5febef5 9891#endif
31e31b8a 9892 case TARGET_NR_sysinfo:
a5448a7d 9893 {
53a5960a 9894 struct target_sysinfo *target_value;
a5448a7d
FB
9895 struct sysinfo value;
9896 ret = get_errno(sysinfo(&value));
53a5960a 9897 if (!is_error(ret) && arg1)
a5448a7d 9898 {
579a97f7
FB
9899 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
9900 goto efault;
a5448a7d
FB
9901 __put_user(value.uptime, &target_value->uptime);
9902 __put_user(value.loads[0], &target_value->loads[0]);
9903 __put_user(value.loads[1], &target_value->loads[1]);
9904 __put_user(value.loads[2], &target_value->loads[2]);
9905 __put_user(value.totalram, &target_value->totalram);
9906 __put_user(value.freeram, &target_value->freeram);
9907 __put_user(value.sharedram, &target_value->sharedram);
9908 __put_user(value.bufferram, &target_value->bufferram);
9909 __put_user(value.totalswap, &target_value->totalswap);
9910 __put_user(value.freeswap, &target_value->freeswap);
9911 __put_user(value.procs, &target_value->procs);
9912 __put_user(value.totalhigh, &target_value->totalhigh);
9913 __put_user(value.freehigh, &target_value->freehigh);
9914 __put_user(value.mem_unit, &target_value->mem_unit);
53a5960a 9915 unlock_user_struct(target_value, arg1, 1);
a5448a7d
FB
9916 }
9917 }
9918 break;
e5febef5 9919#ifdef TARGET_NR_ipc
31e31b8a 9920 case TARGET_NR_ipc:
ee8e7614
PM
9921 ret = do_ipc(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
9922 break;
e5febef5 9923#endif
e5289087
AJ
9924#ifdef TARGET_NR_semget
9925 case TARGET_NR_semget:
9926 ret = get_errno(semget(arg1, arg2, arg3));
9927 break;
9928#endif
9929#ifdef TARGET_NR_semop
9930 case TARGET_NR_semop:
c7128c9f 9931 ret = do_semop(arg1, arg2, arg3);
e5289087
AJ
9932 break;
9933#endif
9934#ifdef TARGET_NR_semctl
9935 case TARGET_NR_semctl:
d1c002b6 9936 ret = do_semctl(arg1, arg2, arg3, arg4);
e5289087
AJ
9937 break;
9938#endif
eeb438c1
AJ
9939#ifdef TARGET_NR_msgctl
9940 case TARGET_NR_msgctl:
9941 ret = do_msgctl(arg1, arg2, arg3);
9942 break;
9943#endif
9944#ifdef TARGET_NR_msgget
9945 case TARGET_NR_msgget:
9946 ret = get_errno(msgget(arg1, arg2));
9947 break;
9948#endif
9949#ifdef TARGET_NR_msgrcv
9950 case TARGET_NR_msgrcv:
9951 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
9952 break;
9953#endif
9954#ifdef TARGET_NR_msgsnd
9955 case TARGET_NR_msgsnd:
9956 ret = do_msgsnd(arg1, arg2, arg3, arg4);
9957 break;
88a8c984
RV
9958#endif
9959#ifdef TARGET_NR_shmget
9960 case TARGET_NR_shmget:
9961 ret = get_errno(shmget(arg1, arg2, arg3));
9962 break;
9963#endif
9964#ifdef TARGET_NR_shmctl
9965 case TARGET_NR_shmctl:
9966 ret = do_shmctl(arg1, arg2, arg3);
9967 break;
9968#endif
9969#ifdef TARGET_NR_shmat
9970 case TARGET_NR_shmat:
ee8e7614 9971 ret = do_shmat(cpu_env, arg1, arg2, arg3);
88a8c984
RV
9972 break;
9973#endif
9974#ifdef TARGET_NR_shmdt
9975 case TARGET_NR_shmdt:
9976 ret = do_shmdt(arg1);
9977 break;
eeb438c1 9978#endif
31e31b8a
FB
9979 case TARGET_NR_fsync:
9980 ret = get_errno(fsync(arg1));
9981 break;
31e31b8a 9982 case TARGET_NR_clone:
4ce6243d
PM
9983 /* Linux manages to have three different orderings for its
9984 * arguments to clone(); the BACKWARDS and BACKWARDS2 defines
9985 * match the kernel's CONFIG_CLONE_* settings.
9986 * Microblaze is further special in that it uses a sixth
9987 * implicit argument to clone for the TLS pointer.
9988 */
9989#if defined(TARGET_MICROBLAZE)
a5b3bdcb 9990 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg4, arg6, arg5));
4ce6243d
PM
9991#elif defined(TARGET_CLONE_BACKWARDS)
9992 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
9993#elif defined(TARGET_CLONE_BACKWARDS2)
a4c075f1 9994 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg5, arg4));
0b6d3ae0 9995#else
4ce6243d 9996 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
0b6d3ae0 9997#endif
1b6b029e 9998 break;
ec86b0fb
FB
9999#ifdef __NR_exit_group
10000 /* new thread calls */
10001 case TARGET_NR_exit_group:
9788c9ca 10002#ifdef TARGET_GPROF
6d946cda
AJ
10003 _mcleanup();
10004#endif
e9009676 10005 gdb_exit(cpu_env, arg1);
ec86b0fb
FB
10006 ret = get_errno(exit_group(arg1));
10007 break;
10008#endif
31e31b8a 10009 case TARGET_NR_setdomainname:
579a97f7
FB
10010 if (!(p = lock_user_string(arg1)))
10011 goto efault;
53a5960a
PB
10012 ret = get_errno(setdomainname(p, arg2));
10013 unlock_user(p, arg1, 0);
31e31b8a
FB
10014 break;
10015 case TARGET_NR_uname:
10016 /* no need to transcode because we use the linux syscall */
29e619b1
FB
10017 {
10018 struct new_utsname * buf;
3b46e624 10019
579a97f7
FB
10020 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
10021 goto efault;
29e619b1
FB
10022 ret = get_errno(sys_uname(buf));
10023 if (!is_error(ret)) {
332c9781 10024 /* Overwrite the native machine name with whatever is being
29e619b1 10025 emulated. */
da79030f 10026 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
c5937220 10027 /* Allow the user to override the reported release. */
332c9781
PM
10028 if (qemu_uname_release && *qemu_uname_release) {
10029 g_strlcpy(buf->release, qemu_uname_release,
10030 sizeof(buf->release));
10031 }
29e619b1 10032 }
53a5960a 10033 unlock_user_struct(buf, arg1, 1);
29e619b1 10034 }
31e31b8a 10035 break;
6dbad63e 10036#ifdef TARGET_I386
31e31b8a 10037 case TARGET_NR_modify_ldt:
03acab66 10038 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
5cd4393b 10039 break;
84409ddb 10040#if !defined(TARGET_X86_64)
5cd4393b
FB
10041 case TARGET_NR_vm86old:
10042 goto unimplemented;
10043 case TARGET_NR_vm86:
53a5960a 10044 ret = do_vm86(cpu_env, arg1, arg2);
6dbad63e 10045 break;
84409ddb 10046#endif
6dbad63e 10047#endif
31e31b8a 10048 case TARGET_NR_adjtimex:
19f59bce
AM
10049 {
10050 struct timex host_buf;
10051
10052 if (target_to_host_timex(&host_buf, arg1) != 0) {
10053 goto efault;
10054 }
10055 ret = get_errno(adjtimex(&host_buf));
10056 if (!is_error(ret)) {
10057 if (host_to_target_timex(arg1, &host_buf) != 0) {
10058 goto efault;
10059 }
10060 }
10061 }
10062 break;
38860a03
AM
10063#if defined(TARGET_NR_clock_adjtime) && defined(CONFIG_CLOCK_ADJTIME)
10064 case TARGET_NR_clock_adjtime:
10065 {
10066 struct timex htx, *phtx = &htx;
10067
10068 if (target_to_host_timex(phtx, arg2) != 0) {
10069 goto efault;
10070 }
10071 ret = get_errno(clock_adjtime(arg1, phtx));
10072 if (!is_error(ret) && phtx) {
10073 if (host_to_target_timex(arg2, phtx) != 0) {
10074 goto efault;
10075 }
10076 }
10077 }
10078 break;
10079#endif
e5febef5 10080#ifdef TARGET_NR_create_module
31e31b8a 10081 case TARGET_NR_create_module:
e5febef5 10082#endif
31e31b8a
FB
10083 case TARGET_NR_init_module:
10084 case TARGET_NR_delete_module:
e5febef5 10085#ifdef TARGET_NR_get_kernel_syms
31e31b8a 10086 case TARGET_NR_get_kernel_syms:
e5febef5 10087#endif
31e31b8a
FB
10088 goto unimplemented;
10089 case TARGET_NR_quotactl:
10090 goto unimplemented;
10091 case TARGET_NR_getpgid:
10092 ret = get_errno(getpgid(arg1));
10093 break;
10094 case TARGET_NR_fchdir:
10095 ret = get_errno(fchdir(arg1));
10096 break;
84409ddb 10097#ifdef TARGET_NR_bdflush /* not on x86_64 */
31e31b8a
FB
10098 case TARGET_NR_bdflush:
10099 goto unimplemented;
84409ddb 10100#endif
e5febef5 10101#ifdef TARGET_NR_sysfs
31e31b8a
FB
10102 case TARGET_NR_sysfs:
10103 goto unimplemented;
e5febef5 10104#endif
31e31b8a 10105 case TARGET_NR_personality:
1b6b029e 10106 ret = get_errno(personality(arg1));
31e31b8a 10107 break;
e5febef5 10108#ifdef TARGET_NR_afs_syscall
31e31b8a
FB
10109 case TARGET_NR_afs_syscall:
10110 goto unimplemented;
e5febef5 10111#endif
7a3148a9 10112#ifdef TARGET_NR__llseek /* Not on alpha */
31e31b8a
FB
10113 case TARGET_NR__llseek:
10114 {
0c1592d9 10115 int64_t res;
d35b261c 10116#if !defined(__NR_llseek)
9fea273c 10117 res = lseek(arg1, ((uint64_t)arg2 << 32) | (abi_ulong)arg3, arg5);
0c1592d9
PM
10118 if (res == -1) {
10119 ret = get_errno(res);
10120 } else {
10121 ret = 0;
10122 }
4f2ac237 10123#else
31e31b8a 10124 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
4f2ac237 10125#endif
0c1592d9
PM
10126 if ((ret == 0) && put_user_s64(res, arg4)) {
10127 goto efault;
10128 }
31e31b8a
FB
10129 }
10130 break;
7a3148a9 10131#endif
704eff6c 10132#ifdef TARGET_NR_getdents
31e31b8a 10133 case TARGET_NR_getdents:
3307e236 10134#ifdef __NR_getdents
d83c8733 10135#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
4add45b4 10136 {
53a5960a 10137 struct target_dirent *target_dirp;
6556a833 10138 struct linux_dirent *dirp;
992f48a0 10139 abi_long count = arg3;
4add45b4 10140
0e173b24
HK
10141 dirp = g_try_malloc(count);
10142 if (!dirp) {
579a97f7 10143 ret = -TARGET_ENOMEM;
0da46a6e
TS
10144 goto fail;
10145 }
3b46e624 10146
4add45b4
FB
10147 ret = get_errno(sys_getdents(arg1, dirp, count));
10148 if (!is_error(ret)) {
6556a833 10149 struct linux_dirent *de;
4add45b4
FB
10150 struct target_dirent *tde;
10151 int len = ret;
10152 int reclen, treclen;
10153 int count1, tnamelen;
10154
10155 count1 = 0;
10156 de = dirp;
579a97f7
FB
10157 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
10158 goto efault;
4add45b4
FB
10159 tde = target_dirp;
10160 while (len > 0) {
10161 reclen = de->d_reclen;
333858b7
DL
10162 tnamelen = reclen - offsetof(struct linux_dirent, d_name);
10163 assert(tnamelen >= 0);
10164 treclen = tnamelen + offsetof(struct target_dirent, d_name);
10165 assert(count1 + treclen <= count);
4add45b4 10166 tde->d_reclen = tswap16(treclen);
cbb21eed
MB
10167 tde->d_ino = tswapal(de->d_ino);
10168 tde->d_off = tswapal(de->d_off);
333858b7 10169 memcpy(tde->d_name, de->d_name, tnamelen);
6556a833 10170 de = (struct linux_dirent *)((char *)de + reclen);
4add45b4 10171 len -= reclen;
1c5bf3bf 10172 tde = (struct target_dirent *)((char *)tde + treclen);
4add45b4
FB
10173 count1 += treclen;
10174 }
10175 ret = count1;
579a97f7 10176 unlock_user(target_dirp, arg2, ret);
4add45b4 10177 }
0e173b24 10178 g_free(dirp);
4add45b4
FB
10179 }
10180#else
31e31b8a 10181 {
6556a833 10182 struct linux_dirent *dirp;
992f48a0 10183 abi_long count = arg3;
dab2ed99 10184
579a97f7
FB
10185 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
10186 goto efault;
72f03900 10187 ret = get_errno(sys_getdents(arg1, dirp, count));
31e31b8a 10188 if (!is_error(ret)) {
6556a833 10189 struct linux_dirent *de;
31e31b8a
FB
10190 int len = ret;
10191 int reclen;
10192 de = dirp;
10193 while (len > 0) {
8083a3e5 10194 reclen = de->d_reclen;
31e31b8a
FB
10195 if (reclen > len)
10196 break;
8083a3e5 10197 de->d_reclen = tswap16(reclen);
31e31b8a
FB
10198 tswapls(&de->d_ino);
10199 tswapls(&de->d_off);
6556a833 10200 de = (struct linux_dirent *)((char *)de + reclen);
31e31b8a
FB
10201 len -= reclen;
10202 }
10203 }
53a5960a 10204 unlock_user(dirp, arg2, ret);
3307e236
PM
10205 }
10206#endif
10207#else
10208 /* Implement getdents in terms of getdents64 */
10209 {
10210 struct linux_dirent64 *dirp;
10211 abi_long count = arg3;
10212
10213 dirp = lock_user(VERIFY_WRITE, arg2, count, 0);
10214 if (!dirp) {
10215 goto efault;
10216 }
10217 ret = get_errno(sys_getdents64(arg1, dirp, count));
10218 if (!is_error(ret)) {
10219 /* Convert the dirent64 structs to target dirent. We do this
10220 * in-place, since we can guarantee that a target_dirent is no
10221 * larger than a dirent64; however this means we have to be
10222 * careful to read everything before writing in the new format.
10223 */
10224 struct linux_dirent64 *de;
10225 struct target_dirent *tde;
10226 int len = ret;
10227 int tlen = 0;
10228
10229 de = dirp;
10230 tde = (struct target_dirent *)dirp;
10231 while (len > 0) {
10232 int namelen, treclen;
10233 int reclen = de->d_reclen;
10234 uint64_t ino = de->d_ino;
10235 int64_t off = de->d_off;
10236 uint8_t type = de->d_type;
10237
10238 namelen = strlen(de->d_name);
10239 treclen = offsetof(struct target_dirent, d_name)
10240 + namelen + 2;
10241 treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long));
10242
10243 memmove(tde->d_name, de->d_name, namelen + 1);
10244 tde->d_ino = tswapal(ino);
10245 tde->d_off = tswapal(off);
10246 tde->d_reclen = tswap16(treclen);
10247 /* The target_dirent type is in what was formerly a padding
10248 * byte at the end of the structure:
10249 */
10250 *(((char *)tde) + treclen - 1) = type;
10251
10252 de = (struct linux_dirent64 *)((char *)de + reclen);
10253 tde = (struct target_dirent *)((char *)tde + treclen);
10254 len -= reclen;
10255 tlen += treclen;
10256 }
10257 ret = tlen;
10258 }
10259 unlock_user(dirp, arg2, ret);
31e31b8a 10260 }
4add45b4 10261#endif
31e31b8a 10262 break;
704eff6c 10263#endif /* TARGET_NR_getdents */
3ae43202 10264#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
dab2ed99
FB
10265 case TARGET_NR_getdents64:
10266 {
6556a833 10267 struct linux_dirent64 *dirp;
992f48a0 10268 abi_long count = arg3;
579a97f7
FB
10269 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
10270 goto efault;
dab2ed99
FB
10271 ret = get_errno(sys_getdents64(arg1, dirp, count));
10272 if (!is_error(ret)) {
6556a833 10273 struct linux_dirent64 *de;
dab2ed99
FB
10274 int len = ret;
10275 int reclen;
10276 de = dirp;
10277 while (len > 0) {
8083a3e5 10278 reclen = de->d_reclen;
dab2ed99
FB
10279 if (reclen > len)
10280 break;
8083a3e5 10281 de->d_reclen = tswap16(reclen);
8582a53a
FB
10282 tswap64s((uint64_t *)&de->d_ino);
10283 tswap64s((uint64_t *)&de->d_off);
6556a833 10284 de = (struct linux_dirent64 *)((char *)de + reclen);
dab2ed99
FB
10285 len -= reclen;
10286 }
10287 }
53a5960a 10288 unlock_user(dirp, arg2, ret);
dab2ed99
FB
10289 }
10290 break;
a541f297 10291#endif /* TARGET_NR_getdents64 */
9468a5d4 10292#if defined(TARGET_NR__newselect)
31e31b8a 10293 case TARGET_NR__newselect:
53a5960a 10294 ret = do_select(arg1, arg2, arg3, arg4, arg5);
31e31b8a 10295 break;
e5febef5 10296#endif
d8035d4c
MF
10297#if defined(TARGET_NR_poll) || defined(TARGET_NR_ppoll)
10298# ifdef TARGET_NR_poll
9de5e440 10299 case TARGET_NR_poll:
d8035d4c
MF
10300# endif
10301# ifdef TARGET_NR_ppoll
10302 case TARGET_NR_ppoll:
10303# endif
9de5e440 10304 {
53a5960a 10305 struct target_pollfd *target_pfd;
9de5e440 10306 unsigned int nfds = arg2;
9de5e440 10307 struct pollfd *pfd;
7854b056 10308 unsigned int i;
9de5e440 10309
3e24bb3f
LV
10310 pfd = NULL;
10311 target_pfd = NULL;
10312 if (nfds) {
ce9c139d
PM
10313 if (nfds > (INT_MAX / sizeof(struct target_pollfd))) {
10314 ret = -TARGET_EINVAL;
10315 break;
10316 }
10317
3e24bb3f
LV
10318 target_pfd = lock_user(VERIFY_WRITE, arg1,
10319 sizeof(struct target_pollfd) * nfds, 1);
10320 if (!target_pfd) {
10321 goto efault;
10322 }
d8035d4c 10323
3e24bb3f
LV
10324 pfd = alloca(sizeof(struct pollfd) * nfds);
10325 for (i = 0; i < nfds; i++) {
10326 pfd[i].fd = tswap32(target_pfd[i].fd);
10327 pfd[i].events = tswap16(target_pfd[i].events);
10328 }
9de5e440 10329 }
d8035d4c 10330
a6130237 10331 switch (num) {
d8035d4c 10332# ifdef TARGET_NR_ppoll
a6130237
PM
10333 case TARGET_NR_ppoll:
10334 {
d8035d4c
MF
10335 struct timespec _timeout_ts, *timeout_ts = &_timeout_ts;
10336 target_sigset_t *target_set;
10337 sigset_t _set, *set = &_set;
10338
10339 if (arg3) {
10340 if (target_to_host_timespec(timeout_ts, arg3)) {
10341 unlock_user(target_pfd, arg1, 0);
10342 goto efault;
10343 }
10344 } else {
10345 timeout_ts = NULL;
10346 }
10347
10348 if (arg4) {
c815701e
PM
10349 if (arg5 != sizeof(target_sigset_t)) {
10350 unlock_user(target_pfd, arg1, 0);
10351 ret = -TARGET_EINVAL;
10352 break;
10353 }
10354
d8035d4c
MF
10355 target_set = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
10356 if (!target_set) {
10357 unlock_user(target_pfd, arg1, 0);
10358 goto efault;
10359 }
10360 target_to_host_sigset(set, target_set);
10361 } else {
10362 set = NULL;
10363 }
10364
a6130237
PM
10365 ret = get_errno(safe_ppoll(pfd, nfds, timeout_ts,
10366 set, SIGSET_T_SIZE));
d8035d4c
MF
10367
10368 if (!is_error(ret) && arg3) {
10369 host_to_target_timespec(arg3, timeout_ts);
10370 }
10371 if (arg4) {
10372 unlock_user(target_set, arg4, 0);
10373 }
a6130237
PM
10374 break;
10375 }
d8035d4c 10376# endif
a6130237
PM
10377# ifdef TARGET_NR_poll
10378 case TARGET_NR_poll:
10379 {
10380 struct timespec ts, *pts;
10381
10382 if (arg3 >= 0) {
10383 /* Convert ms to secs, ns */
10384 ts.tv_sec = arg3 / 1000;
10385 ts.tv_nsec = (arg3 % 1000) * 1000000LL;
10386 pts = &ts;
10387 } else {
10388 /* -ve poll() timeout means "infinite" */
10389 pts = NULL;
10390 }
10391 ret = get_errno(safe_ppoll(pfd, nfds, pts, NULL, 0));
10392 break;
10393 }
d8035d4c 10394# endif
a6130237
PM
10395 default:
10396 g_assert_not_reached();
10397 }
d8035d4c 10398
9de5e440
FB
10399 if (!is_error(ret)) {
10400 for(i = 0; i < nfds; i++) {
5cd4393b 10401 target_pfd[i].revents = tswap16(pfd[i].revents);
9de5e440
FB
10402 }
10403 }
30cb4cde 10404 unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
9de5e440
FB
10405 }
10406 break;
e5febef5 10407#endif
31e31b8a 10408 case TARGET_NR_flock:
9de5e440
FB
10409 /* NOTE: the flock constant seems to be the same for every
10410 Linux platform */
2a845989 10411 ret = get_errno(safe_flock(arg1, arg2));
31e31b8a
FB
10412 break;
10413 case TARGET_NR_readv:
10414 {
f287b2c2
RH
10415 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
10416 if (vec != NULL) {
918c03ed 10417 ret = get_errno(safe_readv(arg1, vec, arg3));
f287b2c2
RH
10418 unlock_iovec(vec, arg2, arg3, 1);
10419 } else {
10420 ret = -host_to_target_errno(errno);
10421 }
31e31b8a
FB
10422 }
10423 break;
10424 case TARGET_NR_writev:
10425 {
f287b2c2
RH
10426 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
10427 if (vec != NULL) {
918c03ed 10428 ret = get_errno(safe_writev(arg1, vec, arg3));
f287b2c2
RH
10429 unlock_iovec(vec, arg2, arg3, 0);
10430 } else {
10431 ret = -host_to_target_errno(errno);
10432 }
31e31b8a
FB
10433 }
10434 break;
0f26386c
DJ
10435#if defined(TARGET_NR_preadv)
10436 case TARGET_NR_preadv:
10437 {
10438 struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
10439 if (vec != NULL) {
10440 ret = get_errno(safe_preadv(arg1, vec, arg3, arg4, arg5));
10441 unlock_iovec(vec, arg2, arg3, 1);
10442 } else {
10443 ret = -host_to_target_errno(errno);
f8d00fba
DJ
10444 }
10445 }
10446 break;
10447#endif
10448#if defined(TARGET_NR_pwritev)
10449 case TARGET_NR_pwritev:
10450 {
10451 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
10452 if (vec != NULL) {
10453 ret = get_errno(safe_pwritev(arg1, vec, arg3, arg4, arg5));
10454 unlock_iovec(vec, arg2, arg3, 0);
10455 } else {
10456 ret = -host_to_target_errno(errno);
0f26386c
DJ
10457 }
10458 }
10459 break;
10460#endif
31e31b8a
FB
10461 case TARGET_NR_getsid:
10462 ret = get_errno(getsid(arg1));
10463 break;
7a3148a9 10464#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
31e31b8a 10465 case TARGET_NR_fdatasync:
5cd4393b
FB
10466 ret = get_errno(fdatasync(arg1));
10467 break;
7a3148a9 10468#endif
704eff6c 10469#ifdef TARGET_NR__sysctl
31e31b8a 10470 case TARGET_NR__sysctl:
0da46a6e 10471 /* We don't implement this, but ENOTDIR is always a safe
29e619b1 10472 return value. */
0da46a6e
TS
10473 ret = -TARGET_ENOTDIR;
10474 break;
704eff6c 10475#endif
737de1d1
MF
10476 case TARGET_NR_sched_getaffinity:
10477 {
10478 unsigned int mask_size;
10479 unsigned long *mask;
10480
10481 /*
10482 * sched_getaffinity needs multiples of ulong, so need to take
10483 * care of mismatches between target ulong and host ulong sizes.
10484 */
10485 if (arg2 & (sizeof(abi_ulong) - 1)) {
10486 ret = -TARGET_EINVAL;
10487 break;
10488 }
10489 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
10490
10491 mask = alloca(mask_size);
2e0a8713 10492 memset(mask, 0, mask_size);
737de1d1
MF
10493 ret = get_errno(sys_sched_getaffinity(arg1, mask_size, mask));
10494
10495 if (!is_error(ret)) {
be3bd286
PM
10496 if (ret > arg2) {
10497 /* More data returned than the caller's buffer will fit.
10498 * This only happens if sizeof(abi_long) < sizeof(long)
10499 * and the caller passed us a buffer holding an odd number
10500 * of abi_longs. If the host kernel is actually using the
10501 * extra 4 bytes then fail EINVAL; otherwise we can just
10502 * ignore them and only copy the interesting part.
10503 */
10504 int numcpus = sysconf(_SC_NPROCESSORS_CONF);
10505 if (numcpus > arg2 * 8) {
10506 ret = -TARGET_EINVAL;
10507 break;
10508 }
10509 ret = arg2;
10510 }
10511
5fdefcf8
ST
10512 if (host_to_target_cpu_mask(mask, mask_size, arg3, ret)) {
10513 goto efault;
10514 }
737de1d1
MF
10515 }
10516 }
10517 break;
10518 case TARGET_NR_sched_setaffinity:
10519 {
10520 unsigned int mask_size;
10521 unsigned long *mask;
10522
10523 /*
10524 * sched_setaffinity needs multiples of ulong, so need to take
10525 * care of mismatches between target ulong and host ulong sizes.
10526 */
10527 if (arg2 & (sizeof(abi_ulong) - 1)) {
10528 ret = -TARGET_EINVAL;
10529 break;
10530 }
10531 mask_size = (arg2 + (sizeof(*mask) - 1)) & ~(sizeof(*mask) - 1);
737de1d1 10532 mask = alloca(mask_size);
2e0a8713
ST
10533
10534 ret = target_to_host_cpu_mask(mask, mask_size, arg3, arg2);
10535 if (ret) {
10536 break;
737de1d1 10537 }
737de1d1
MF
10538
10539 ret = get_errno(sys_sched_setaffinity(arg1, mask_size, mask));
10540 }
10541 break;
b827c3ed
ST
10542 case TARGET_NR_getcpu:
10543 {
10544 unsigned cpu, node;
10545 ret = get_errno(sys_getcpu(arg1 ? &cpu : NULL,
10546 arg2 ? &node : NULL,
10547 NULL));
10548 if (is_error(ret)) {
10549 goto fail;
10550 }
10551 if (arg1 && put_user_u32(cpu, arg1)) {
10552 goto efault;
10553 }
10554 if (arg2 && put_user_u32(node, arg2)) {
10555 goto efault;
10556 }
10557 }
10558 break;
31e31b8a 10559 case TARGET_NR_sched_setparam:
5cd4393b 10560 {
53a5960a 10561 struct sched_param *target_schp;
5cd4393b 10562 struct sched_param schp;
53a5960a 10563
a1d5c5b2
TM
10564 if (arg2 == 0) {
10565 return -TARGET_EINVAL;
10566 }
579a97f7
FB
10567 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
10568 goto efault;
5cd4393b 10569 schp.sched_priority = tswap32(target_schp->sched_priority);
53a5960a 10570 unlock_user_struct(target_schp, arg2, 0);
5cd4393b
FB
10571 ret = get_errno(sched_setparam(arg1, &schp));
10572 }
10573 break;
31e31b8a 10574 case TARGET_NR_sched_getparam:
5cd4393b 10575 {
53a5960a 10576 struct sched_param *target_schp;
5cd4393b 10577 struct sched_param schp;
a1d5c5b2
TM
10578
10579 if (arg2 == 0) {
10580 return -TARGET_EINVAL;
10581 }
5cd4393b
FB
10582 ret = get_errno(sched_getparam(arg1, &schp));
10583 if (!is_error(ret)) {
579a97f7
FB
10584 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
10585 goto efault;
5cd4393b 10586 target_schp->sched_priority = tswap32(schp.sched_priority);
53a5960a 10587 unlock_user_struct(target_schp, arg2, 1);
5cd4393b
FB
10588 }
10589 }
10590 break;
31e31b8a 10591 case TARGET_NR_sched_setscheduler:
5cd4393b 10592 {
53a5960a 10593 struct sched_param *target_schp;
5cd4393b 10594 struct sched_param schp;
a1d5c5b2
TM
10595 if (arg3 == 0) {
10596 return -TARGET_EINVAL;
10597 }
579a97f7
FB
10598 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
10599 goto efault;
5cd4393b 10600 schp.sched_priority = tswap32(target_schp->sched_priority);
53a5960a 10601 unlock_user_struct(target_schp, arg3, 0);
5cd4393b
FB
10602 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
10603 }
10604 break;
31e31b8a 10605 case TARGET_NR_sched_getscheduler:
5cd4393b
FB
10606 ret = get_errno(sched_getscheduler(arg1));
10607 break;
31e31b8a
FB
10608 case TARGET_NR_sched_yield:
10609 ret = get_errno(sched_yield());
10610 break;
10611 case TARGET_NR_sched_get_priority_max:
5cd4393b
FB
10612 ret = get_errno(sched_get_priority_max(arg1));
10613 break;
31e31b8a 10614 case TARGET_NR_sched_get_priority_min:
5cd4393b
FB
10615 ret = get_errno(sched_get_priority_min(arg1));
10616 break;
31e31b8a 10617 case TARGET_NR_sched_rr_get_interval:
5cd4393b 10618 {
5cd4393b
FB
10619 struct timespec ts;
10620 ret = get_errno(sched_rr_get_interval(arg1, &ts));
10621 if (!is_error(ret)) {
d4290c40 10622 ret = host_to_target_timespec(arg2, &ts);
5cd4393b
FB
10623 }
10624 }
10625 break;
31e31b8a 10626 case TARGET_NR_nanosleep:
1b6b029e 10627 {
1b6b029e 10628 struct timespec req, rem;
53a5960a 10629 target_to_host_timespec(&req, arg1);
9e518226 10630 ret = get_errno(safe_nanosleep(&req, &rem));
53a5960a
PB
10631 if (is_error(ret) && arg2) {
10632 host_to_target_timespec(arg2, &rem);
1b6b029e
FB
10633 }
10634 }
10635 break;
e5febef5 10636#ifdef TARGET_NR_query_module
31e31b8a 10637 case TARGET_NR_query_module:
5cd4393b 10638 goto unimplemented;
e5febef5
TS
10639#endif
10640#ifdef TARGET_NR_nfsservctl
31e31b8a 10641 case TARGET_NR_nfsservctl:
5cd4393b 10642 goto unimplemented;
e5febef5 10643#endif
31e31b8a 10644 case TARGET_NR_prctl:
1e6722f8
PM
10645 switch (arg1) {
10646 case PR_GET_PDEATHSIG:
10647 {
10648 int deathsig;
10649 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
10650 if (!is_error(ret) && arg2
10651 && put_user_ual(deathsig, arg2)) {
10652 goto efault;
e5574487 10653 }
1e6722f8
PM
10654 break;
10655 }
db9526b1
PM
10656#ifdef PR_GET_NAME
10657 case PR_GET_NAME:
10658 {
10659 void *name = lock_user(VERIFY_WRITE, arg2, 16, 1);
10660 if (!name) {
10661 goto efault;
10662 }
10663 ret = get_errno(prctl(arg1, (unsigned long)name,
10664 arg3, arg4, arg5));
10665 unlock_user(name, arg2, 16);
10666 break;
10667 }
10668 case PR_SET_NAME:
10669 {
10670 void *name = lock_user(VERIFY_READ, arg2, 16, 1);
10671 if (!name) {
10672 goto efault;
10673 }
10674 ret = get_errno(prctl(arg1, (unsigned long)name,
10675 arg3, arg4, arg5));
10676 unlock_user(name, arg2, 0);
10677 break;
10678 }
10679#endif
a8b154a6
JC
10680 case PR_GET_SECCOMP:
10681 case PR_SET_SECCOMP:
10682 /* Disable seccomp to prevent the target disabling syscalls we
10683 * need. */
10684 ret = -TARGET_EINVAL;
10685 break;
1e6722f8
PM
10686 default:
10687 /* Most prctl options have no pointer arguments */
10688 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
10689 break;
10690 }
39b9aae1 10691 break;
d2fd1af7
FB
10692#ifdef TARGET_NR_arch_prctl
10693 case TARGET_NR_arch_prctl:
10694#if defined(TARGET_I386) && !defined(TARGET_ABI32)
10695 ret = do_arch_prctl(cpu_env, arg1, arg2);
10696 break;
10697#else
10698 goto unimplemented;
10699#endif
10700#endif
f2c7ba15
AJ
10701#ifdef TARGET_NR_pread64
10702 case TARGET_NR_pread64:
8bf8e9df 10703 if (regpairs_aligned(cpu_env, num)) {
ae017a5b
AG
10704 arg4 = arg5;
10705 arg5 = arg6;
10706 }
f2c7ba15
AJ
10707 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
10708 goto efault;
10709 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
10710 unlock_user(p, arg2, ret);
10711 break;
10712 case TARGET_NR_pwrite64:
8bf8e9df 10713 if (regpairs_aligned(cpu_env, num)) {
ae017a5b
AG
10714 arg4 = arg5;
10715 arg5 = arg6;
10716 }
f2c7ba15
AJ
10717 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
10718 goto efault;
10719 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
10720 unlock_user(p, arg2, 0);
10721 break;
67867308 10722#endif
31e31b8a 10723 case TARGET_NR_getcwd:
579a97f7
FB
10724 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
10725 goto efault;
53a5960a
PB
10726 ret = get_errno(sys_getcwd1(p, arg2));
10727 unlock_user(p, arg1, ret);
31e31b8a
FB
10728 break;
10729 case TARGET_NR_capget:
10730 case TARGET_NR_capset:
e0eb210e
PM
10731 {
10732 struct target_user_cap_header *target_header;
10733 struct target_user_cap_data *target_data = NULL;
10734 struct __user_cap_header_struct header;
10735 struct __user_cap_data_struct data[2];
10736 struct __user_cap_data_struct *dataptr = NULL;
10737 int i, target_datalen;
10738 int data_items = 1;
10739
10740 if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
10741 goto efault;
10742 }
10743 header.version = tswap32(target_header->version);
10744 header.pid = tswap32(target_header->pid);
10745
ec864874 10746 if (header.version != _LINUX_CAPABILITY_VERSION) {
e0eb210e
PM
10747 /* Version 2 and up takes pointer to two user_data structs */
10748 data_items = 2;
10749 }
10750
10751 target_datalen = sizeof(*target_data) * data_items;
10752
10753 if (arg2) {
10754 if (num == TARGET_NR_capget) {
10755 target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
10756 } else {
10757 target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
10758 }
10759 if (!target_data) {
10760 unlock_user_struct(target_header, arg1, 0);
10761 goto efault;
10762 }
10763
10764 if (num == TARGET_NR_capset) {
10765 for (i = 0; i < data_items; i++) {
10766 data[i].effective = tswap32(target_data[i].effective);
10767 data[i].permitted = tswap32(target_data[i].permitted);
10768 data[i].inheritable = tswap32(target_data[i].inheritable);
10769 }
10770 }
10771
10772 dataptr = data;
10773 }
10774
10775 if (num == TARGET_NR_capget) {
10776 ret = get_errno(capget(&header, dataptr));
10777 } else {
10778 ret = get_errno(capset(&header, dataptr));
10779 }
10780
10781 /* The kernel always updates version for both capget and capset */
10782 target_header->version = tswap32(header.version);
10783 unlock_user_struct(target_header, arg1, 1);
10784
10785 if (arg2) {
10786 if (num == TARGET_NR_capget) {
10787 for (i = 0; i < data_items; i++) {
10788 target_data[i].effective = tswap32(data[i].effective);
10789 target_data[i].permitted = tswap32(data[i].permitted);
10790 target_data[i].inheritable = tswap32(data[i].inheritable);
10791 }
10792 unlock_user(target_data, arg2, target_datalen);
10793 } else {
10794 unlock_user(target_data, arg2, 0);
10795 }
10796 }
10797 break;
10798 }
31e31b8a 10799 case TARGET_NR_sigaltstack:
9349b4f9 10800 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUArchState *)cpu_env));
a04e134a 10801 break;
a8fd1aba
PM
10802
10803#ifdef CONFIG_SENDFILE
10804 case TARGET_NR_sendfile:
10805 {
10806 off_t *offp = NULL;
10807 off_t off;
10808 if (arg3) {
10809 ret = get_user_sal(off, arg3);
10810 if (is_error(ret)) {
10811 break;
10812 }
10813 offp = &off;
10814 }
10815 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
10816 if (!is_error(ret) && arg3) {
10817 abi_long ret2 = put_user_sal(off, arg3);
10818 if (is_error(ret2)) {
10819 ret = ret2;
10820 }
10821 }
10822 break;
10823 }
10824#ifdef TARGET_NR_sendfile64
10825 case TARGET_NR_sendfile64:
10826 {
10827 off_t *offp = NULL;
10828 off_t off;
10829 if (arg3) {
10830 ret = get_user_s64(off, arg3);
10831 if (is_error(ret)) {
10832 break;
10833 }
10834 offp = &off;
10835 }
10836 ret = get_errno(sendfile(arg1, arg2, offp, arg4));
10837 if (!is_error(ret) && arg3) {
10838 abi_long ret2 = put_user_s64(off, arg3);
10839 if (is_error(ret2)) {
10840 ret = ret2;
10841 }
10842 }
10843 break;
10844 }
10845#endif
10846#else
31e31b8a 10847 case TARGET_NR_sendfile:
7edd2cf1 10848#ifdef TARGET_NR_sendfile64
a8fd1aba
PM
10849 case TARGET_NR_sendfile64:
10850#endif
5cd4393b 10851 goto unimplemented;
a8fd1aba
PM
10852#endif
10853
ebc05488 10854#ifdef TARGET_NR_getpmsg
31e31b8a 10855 case TARGET_NR_getpmsg:
5cd4393b 10856 goto unimplemented;
ebc05488
FB
10857#endif
10858#ifdef TARGET_NR_putpmsg
31e31b8a 10859 case TARGET_NR_putpmsg:
5cd4393b 10860 goto unimplemented;
ebc05488 10861#endif
048f6b4d 10862#ifdef TARGET_NR_vfork
31e31b8a 10863 case TARGET_NR_vfork:
7eddb5dd
LV
10864 ret = get_errno(do_fork(cpu_env,
10865 CLONE_VFORK | CLONE_VM | TARGET_SIGCHLD,
d865bab5 10866 0, 0, 0, 0));
31e31b8a 10867 break;
048f6b4d 10868#endif
ebc05488 10869#ifdef TARGET_NR_ugetrlimit
31e31b8a 10870 case TARGET_NR_ugetrlimit:
728584be
FB
10871 {
10872 struct rlimit rlim;
e22b7015
WT
10873 int resource = target_to_host_resource(arg1);
10874 ret = get_errno(getrlimit(resource, &rlim));
728584be 10875 if (!is_error(ret)) {
53a5960a 10876 struct target_rlimit *target_rlim;
579a97f7
FB
10877 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
10878 goto efault;
81bbe906
TY
10879 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
10880 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
53a5960a 10881 unlock_user_struct(target_rlim, arg2, 1);
728584be
FB
10882 }
10883 break;
10884 }
ebc05488 10885#endif
a315a145 10886#ifdef TARGET_NR_truncate64
31e31b8a 10887 case TARGET_NR_truncate64:
579a97f7
FB
10888 if (!(p = lock_user_string(arg1)))
10889 goto efault;
53a5960a
PB
10890 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
10891 unlock_user(p, arg1, 0);
667f38b1 10892 break;
a315a145
FB
10893#endif
10894#ifdef TARGET_NR_ftruncate64
31e31b8a 10895 case TARGET_NR_ftruncate64:
ce4defa0 10896 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
667f38b1 10897 break;
a315a145
FB
10898#endif
10899#ifdef TARGET_NR_stat64
31e31b8a 10900 case TARGET_NR_stat64:
579a97f7
FB
10901 if (!(p = lock_user_string(arg1)))
10902 goto efault;
53a5960a
PB
10903 ret = get_errno(stat(path(p), &st));
10904 unlock_user(p, arg1, 0);
6a24a778
AZ
10905 if (!is_error(ret))
10906 ret = host_to_target_stat64(cpu_env, arg2, &st);
10907 break;
a315a145
FB
10908#endif
10909#ifdef TARGET_NR_lstat64
31e31b8a 10910 case TARGET_NR_lstat64:
579a97f7
FB
10911 if (!(p = lock_user_string(arg1)))
10912 goto efault;
53a5960a
PB
10913 ret = get_errno(lstat(path(p), &st));
10914 unlock_user(p, arg1, 0);
6a24a778
AZ
10915 if (!is_error(ret))
10916 ret = host_to_target_stat64(cpu_env, arg2, &st);
10917 break;
a315a145
FB
10918#endif
10919#ifdef TARGET_NR_fstat64
31e31b8a 10920 case TARGET_NR_fstat64:
6a24a778
AZ
10921 ret = get_errno(fstat(arg1, &st));
10922 if (!is_error(ret))
10923 ret = host_to_target_stat64(cpu_env, arg2, &st);
10924 break;
ce4defa0 10925#endif
c0d472b1 10926#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat))
9d33b76b 10927#ifdef TARGET_NR_fstatat64
6a24a778 10928 case TARGET_NR_fstatat64:
9d33b76b
AJ
10929#endif
10930#ifdef TARGET_NR_newfstatat
10931 case TARGET_NR_newfstatat:
10932#endif
6a24a778
AZ
10933 if (!(p = lock_user_string(arg2)))
10934 goto efault;
c0d472b1 10935 ret = get_errno(fstatat(arg1, path(p), &st, arg4));
6a24a778
AZ
10936 if (!is_error(ret))
10937 ret = host_to_target_stat64(cpu_env, arg3, &st);
60cd49d5 10938 break;
a315a145 10939#endif
704eff6c 10940#ifdef TARGET_NR_lchown
67867308 10941 case TARGET_NR_lchown:
579a97f7
FB
10942 if (!(p = lock_user_string(arg1)))
10943 goto efault;
53a5960a
PB
10944 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
10945 unlock_user(p, arg1, 0);
67867308 10946 break;
704eff6c 10947#endif
0c866a7e 10948#ifdef TARGET_NR_getuid
67867308
FB
10949 case TARGET_NR_getuid:
10950 ret = get_errno(high2lowuid(getuid()));
10951 break;
0c866a7e
RV
10952#endif
10953#ifdef TARGET_NR_getgid
67867308
FB
10954 case TARGET_NR_getgid:
10955 ret = get_errno(high2lowgid(getgid()));
10956 break;
0c866a7e
RV
10957#endif
10958#ifdef TARGET_NR_geteuid
67867308
FB
10959 case TARGET_NR_geteuid:
10960 ret = get_errno(high2lowuid(geteuid()));
10961 break;
0c866a7e
RV
10962#endif
10963#ifdef TARGET_NR_getegid
67867308
FB
10964 case TARGET_NR_getegid:
10965 ret = get_errno(high2lowgid(getegid()));
10966 break;
0c866a7e 10967#endif
67867308
FB
10968 case TARGET_NR_setreuid:
10969 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
10970 break;
10971 case TARGET_NR_setregid:
10972 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
10973 break;
10974 case TARGET_NR_getgroups:
10975 {
10976 int gidsetsize = arg1;
0c866a7e 10977 target_id *target_grouplist;
67867308
FB
10978 gid_t *grouplist;
10979 int i;
10980
10981 grouplist = alloca(gidsetsize * sizeof(gid_t));
10982 ret = get_errno(getgroups(gidsetsize, grouplist));
cb3bc233
AZ
10983 if (gidsetsize == 0)
10984 break;
67867308 10985 if (!is_error(ret)) {
03903ffc 10986 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * sizeof(target_id), 0);
579a97f7
FB
10987 if (!target_grouplist)
10988 goto efault;
a2155fcc 10989 for(i = 0;i < ret; i++)
0c866a7e 10990 target_grouplist[i] = tswapid(high2lowgid(grouplist[i]));
03903ffc 10991 unlock_user(target_grouplist, arg2, gidsetsize * sizeof(target_id));
67867308
FB
10992 }
10993 }
10994 break;
10995 case TARGET_NR_setgroups:
10996 {
10997 int gidsetsize = arg1;
0c866a7e 10998 target_id *target_grouplist;
f2b79ce9 10999 gid_t *grouplist = NULL;
67867308 11000 int i;
f2b79ce9
DA
11001 if (gidsetsize) {
11002 grouplist = alloca(gidsetsize * sizeof(gid_t));
03903ffc 11003 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * sizeof(target_id), 1);
f2b79ce9
DA
11004 if (!target_grouplist) {
11005 ret = -TARGET_EFAULT;
11006 goto fail;
11007 }
11008 for (i = 0; i < gidsetsize; i++) {
11009 grouplist[i] = low2highgid(tswapid(target_grouplist[i]));
11010 }
11011 unlock_user(target_grouplist, arg2, 0);
579a97f7 11012 }
67867308
FB
11013 ret = get_errno(setgroups(gidsetsize, grouplist));
11014 }
11015 break;
11016 case TARGET_NR_fchown:
11017 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
11018 break;
c0d472b1 11019#if defined(TARGET_NR_fchownat)
ccfa72b7 11020 case TARGET_NR_fchownat:
579a97f7
FB
11021 if (!(p = lock_user_string(arg2)))
11022 goto efault;
c0d472b1
PM
11023 ret = get_errno(fchownat(arg1, p, low2highuid(arg3),
11024 low2highgid(arg4), arg5));
579a97f7 11025 unlock_user(p, arg2, 0);
ccfa72b7
TS
11026 break;
11027#endif
67867308
FB
11028#ifdef TARGET_NR_setresuid
11029 case TARGET_NR_setresuid:
fd6f7798
PM
11030 ret = get_errno(sys_setresuid(low2highuid(arg1),
11031 low2highuid(arg2),
11032 low2highuid(arg3)));
67867308
FB
11033 break;
11034#endif
11035#ifdef TARGET_NR_getresuid
11036 case TARGET_NR_getresuid:
11037 {
53a5960a 11038 uid_t ruid, euid, suid;
67867308
FB
11039 ret = get_errno(getresuid(&ruid, &euid, &suid));
11040 if (!is_error(ret)) {
76ca310a
PM
11041 if (put_user_id(high2lowuid(ruid), arg1)
11042 || put_user_id(high2lowuid(euid), arg2)
11043 || put_user_id(high2lowuid(suid), arg3))
2f619698 11044 goto efault;
67867308
FB
11045 }
11046 }
11047 break;
11048#endif
11049#ifdef TARGET_NR_getresgid
11050 case TARGET_NR_setresgid:
fd6f7798
PM
11051 ret = get_errno(sys_setresgid(low2highgid(arg1),
11052 low2highgid(arg2),
11053 low2highgid(arg3)));
67867308
FB
11054 break;
11055#endif
11056#ifdef TARGET_NR_getresgid
11057 case TARGET_NR_getresgid:
11058 {
53a5960a 11059 gid_t rgid, egid, sgid;
67867308
FB
11060 ret = get_errno(getresgid(&rgid, &egid, &sgid));
11061 if (!is_error(ret)) {
76ca310a
PM
11062 if (put_user_id(high2lowgid(rgid), arg1)
11063 || put_user_id(high2lowgid(egid), arg2)
11064 || put_user_id(high2lowgid(sgid), arg3))
2f619698 11065 goto efault;
67867308
FB
11066 }
11067 }
11068 break;
11069#endif
704eff6c 11070#ifdef TARGET_NR_chown
67867308 11071 case TARGET_NR_chown:
579a97f7
FB
11072 if (!(p = lock_user_string(arg1)))
11073 goto efault;
53a5960a
PB
11074 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
11075 unlock_user(p, arg1, 0);
67867308 11076 break;
704eff6c 11077#endif
67867308 11078 case TARGET_NR_setuid:
fd6f7798 11079 ret = get_errno(sys_setuid(low2highuid(arg1)));
67867308
FB
11080 break;
11081 case TARGET_NR_setgid:
fd6f7798 11082 ret = get_errno(sys_setgid(low2highgid(arg1)));
67867308
FB
11083 break;
11084 case TARGET_NR_setfsuid:
11085 ret = get_errno(setfsuid(arg1));
11086 break;
11087 case TARGET_NR_setfsgid:
11088 ret = get_errno(setfsgid(arg1));
11089 break;
67867308 11090
a315a145 11091#ifdef TARGET_NR_lchown32
31e31b8a 11092 case TARGET_NR_lchown32:
579a97f7
FB
11093 if (!(p = lock_user_string(arg1)))
11094 goto efault;
53a5960a
PB
11095 ret = get_errno(lchown(p, arg2, arg3));
11096 unlock_user(p, arg1, 0);
b03c60f3 11097 break;
a315a145
FB
11098#endif
11099#ifdef TARGET_NR_getuid32
31e31b8a 11100 case TARGET_NR_getuid32:
b03c60f3
FB
11101 ret = get_errno(getuid());
11102 break;
a315a145 11103#endif
64b4d28c
AJ
11104
11105#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
11106 /* Alpha specific */
11107 case TARGET_NR_getxuid:
ba0e276d
RH
11108 {
11109 uid_t euid;
11110 euid=geteuid();
11111 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
11112 }
64b4d28c
AJ
11113 ret = get_errno(getuid());
11114 break;
11115#endif
11116#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
11117 /* Alpha specific */
11118 case TARGET_NR_getxgid:
ba0e276d
RH
11119 {
11120 uid_t egid;
11121 egid=getegid();
11122 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
11123 }
64b4d28c
AJ
11124 ret = get_errno(getgid());
11125 break;
11126#endif
ba0e276d
RH
11127#if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
11128 /* Alpha specific */
11129 case TARGET_NR_osf_getsysinfo:
11130 ret = -TARGET_EOPNOTSUPP;
11131 switch (arg1) {
11132 case TARGET_GSI_IEEE_FP_CONTROL:
11133 {
11134 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
11135
11136 /* Copied from linux ieee_fpcr_to_swcr. */
11137 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
11138 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
11139 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
11140 | SWCR_TRAP_ENABLE_DZE
11141 | SWCR_TRAP_ENABLE_OVF);
11142 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
11143 | SWCR_TRAP_ENABLE_INE);
11144 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
11145 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
11146
11147 if (put_user_u64 (swcr, arg2))
11148 goto efault;
11149 ret = 0;
11150 }
11151 break;
11152
11153 /* case GSI_IEEE_STATE_AT_SIGNAL:
11154 -- Not implemented in linux kernel.
11155 case GSI_UACPROC:
11156 -- Retrieves current unaligned access state; not much used.
11157 case GSI_PROC_TYPE:
11158 -- Retrieves implver information; surely not used.
11159 case GSI_GET_HWRPB:
11160 -- Grabs a copy of the HWRPB; surely not used.
11161 */
11162 }
11163 break;
11164#endif
11165#if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
11166 /* Alpha specific */
11167 case TARGET_NR_osf_setsysinfo:
11168 ret = -TARGET_EOPNOTSUPP;
11169 switch (arg1) {
11170 case TARGET_SSI_IEEE_FP_CONTROL:
ba0e276d
RH
11171 {
11172 uint64_t swcr, fpcr, orig_fpcr;
11173
6e06d515 11174 if (get_user_u64 (swcr, arg2)) {
ba0e276d 11175 goto efault;
6e06d515
RH
11176 }
11177 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
ba0e276d
RH
11178 fpcr = orig_fpcr & FPCR_DYN_MASK;
11179
11180 /* Copied from linux ieee_swcr_to_fpcr. */
11181 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
11182 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
11183 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
11184 | SWCR_TRAP_ENABLE_DZE
11185 | SWCR_TRAP_ENABLE_OVF)) << 48;
11186 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
11187 | SWCR_TRAP_ENABLE_INE)) << 57;
11188 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
11189 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
11190
6e06d515 11191 cpu_alpha_store_fpcr(cpu_env, fpcr);
ba0e276d 11192 ret = 0;
6e06d515
RH
11193 }
11194 break;
11195
11196 case TARGET_SSI_IEEE_RAISE_EXCEPTION:
11197 {
11198 uint64_t exc, fpcr, orig_fpcr;
11199 int si_code;
11200
11201 if (get_user_u64(exc, arg2)) {
11202 goto efault;
11203 }
ba0e276d 11204
6e06d515 11205 orig_fpcr = cpu_alpha_load_fpcr(cpu_env);
ba0e276d 11206
6e06d515
RH
11207 /* We only add to the exception status here. */
11208 fpcr = orig_fpcr | ((exc & SWCR_STATUS_MASK) << 35);
11209
11210 cpu_alpha_store_fpcr(cpu_env, fpcr);
11211 ret = 0;
11212
11213 /* Old exceptions are not signaled. */
11214 fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
11215
11216 /* If any exceptions set by this call,
11217 and are unmasked, send a signal. */
11218 si_code = 0;
11219 if ((fpcr & (FPCR_INE | FPCR_INED)) == FPCR_INE) {
11220 si_code = TARGET_FPE_FLTRES;
11221 }
11222 if ((fpcr & (FPCR_UNF | FPCR_UNFD)) == FPCR_UNF) {
11223 si_code = TARGET_FPE_FLTUND;
11224 }
11225 if ((fpcr & (FPCR_OVF | FPCR_OVFD)) == FPCR_OVF) {
11226 si_code = TARGET_FPE_FLTOVF;
11227 }
11228 if ((fpcr & (FPCR_DZE | FPCR_DZED)) == FPCR_DZE) {
11229 si_code = TARGET_FPE_FLTDIV;
11230 }
11231 if ((fpcr & (FPCR_INV | FPCR_INVD)) == FPCR_INV) {
11232 si_code = TARGET_FPE_FLTINV;
11233 }
11234 if (si_code != 0) {
11235 target_siginfo_t info;
11236 info.si_signo = SIGFPE;
11237 info.si_errno = 0;
11238 info.si_code = si_code;
11239 info._sifields._sigfault._addr
11240 = ((CPUArchState *)cpu_env)->pc;
9d2803f7
PM
11241 queue_signal((CPUArchState *)cpu_env, info.si_signo,
11242 QEMU_SI_FAULT, &info);
ba0e276d
RH
11243 }
11244 }
11245 break;
11246
11247 /* case SSI_NVPAIRS:
11248 -- Used with SSIN_UACPROC to enable unaligned accesses.
11249 case SSI_IEEE_STATE_AT_SIGNAL:
11250 case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
11251 -- Not implemented in linux kernel
11252 */
11253 }
11254 break;
11255#endif
11256#ifdef TARGET_NR_osf_sigprocmask
11257 /* Alpha specific. */
11258 case TARGET_NR_osf_sigprocmask:
11259 {
11260 abi_ulong mask;
bc088ba1 11261 int how;
ba0e276d
RH
11262 sigset_t set, oldset;
11263
11264 switch(arg1) {
11265 case TARGET_SIG_BLOCK:
11266 how = SIG_BLOCK;
11267 break;
11268 case TARGET_SIG_UNBLOCK:
11269 how = SIG_UNBLOCK;
11270 break;
11271 case TARGET_SIG_SETMASK:
11272 how = SIG_SETMASK;
11273 break;
11274 default:
11275 ret = -TARGET_EINVAL;
11276 goto fail;
11277 }
11278 mask = arg2;
11279 target_to_host_old_sigset(&set, &mask);
3d3efba0
PM
11280 ret = do_sigprocmask(how, &set, &oldset);
11281 if (!ret) {
11282 host_to_target_old_sigset(&mask, &oldset);
11283 ret = mask;
11284 }
ba0e276d
RH
11285 }
11286 break;
11287#endif
64b4d28c 11288
a315a145 11289#ifdef TARGET_NR_getgid32
31e31b8a 11290 case TARGET_NR_getgid32:
b03c60f3
FB
11291 ret = get_errno(getgid());
11292 break;
a315a145
FB
11293#endif
11294#ifdef TARGET_NR_geteuid32
31e31b8a 11295 case TARGET_NR_geteuid32:
b03c60f3
FB
11296 ret = get_errno(geteuid());
11297 break;
a315a145
FB
11298#endif
11299#ifdef TARGET_NR_getegid32
31e31b8a 11300 case TARGET_NR_getegid32:
b03c60f3
FB
11301 ret = get_errno(getegid());
11302 break;
a315a145
FB
11303#endif
11304#ifdef TARGET_NR_setreuid32
31e31b8a 11305 case TARGET_NR_setreuid32:
b03c60f3
FB
11306 ret = get_errno(setreuid(arg1, arg2));
11307 break;
a315a145
FB
11308#endif
11309#ifdef TARGET_NR_setregid32
31e31b8a 11310 case TARGET_NR_setregid32:
b03c60f3
FB
11311 ret = get_errno(setregid(arg1, arg2));
11312 break;
a315a145
FB
11313#endif
11314#ifdef TARGET_NR_getgroups32
31e31b8a 11315 case TARGET_NR_getgroups32:
99c475ab
FB
11316 {
11317 int gidsetsize = arg1;
53a5960a 11318 uint32_t *target_grouplist;
99c475ab
FB
11319 gid_t *grouplist;
11320 int i;
11321
11322 grouplist = alloca(gidsetsize * sizeof(gid_t));
11323 ret = get_errno(getgroups(gidsetsize, grouplist));
cb3bc233
AZ
11324 if (gidsetsize == 0)
11325 break;
99c475ab 11326 if (!is_error(ret)) {
579a97f7
FB
11327 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
11328 if (!target_grouplist) {
11329 ret = -TARGET_EFAULT;
11330 goto fail;
11331 }
a2155fcc 11332 for(i = 0;i < ret; i++)
53a5960a
PB
11333 target_grouplist[i] = tswap32(grouplist[i]);
11334 unlock_user(target_grouplist, arg2, gidsetsize * 4);
99c475ab
FB
11335 }
11336 }
11337 break;
a315a145
FB
11338#endif
11339#ifdef TARGET_NR_setgroups32
31e31b8a 11340 case TARGET_NR_setgroups32:
99c475ab
FB
11341 {
11342 int gidsetsize = arg1;
53a5960a 11343 uint32_t *target_grouplist;
99c475ab
FB
11344 gid_t *grouplist;
11345 int i;
3b46e624 11346
99c475ab 11347 grouplist = alloca(gidsetsize * sizeof(gid_t));
579a97f7
FB
11348 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
11349 if (!target_grouplist) {
11350 ret = -TARGET_EFAULT;
11351 goto fail;
11352 }
99c475ab 11353 for(i = 0;i < gidsetsize; i++)
53a5960a
PB
11354 grouplist[i] = tswap32(target_grouplist[i]);
11355 unlock_user(target_grouplist, arg2, 0);
99c475ab
FB
11356 ret = get_errno(setgroups(gidsetsize, grouplist));
11357 }
11358 break;
a315a145
FB
11359#endif
11360#ifdef TARGET_NR_fchown32
31e31b8a 11361 case TARGET_NR_fchown32:
b03c60f3
FB
11362 ret = get_errno(fchown(arg1, arg2, arg3));
11363 break;
a315a145
FB
11364#endif
11365#ifdef TARGET_NR_setresuid32
31e31b8a 11366 case TARGET_NR_setresuid32:
fd6f7798 11367 ret = get_errno(sys_setresuid(arg1, arg2, arg3));
b03c60f3 11368 break;
a315a145
FB
11369#endif
11370#ifdef TARGET_NR_getresuid32
31e31b8a 11371 case TARGET_NR_getresuid32:
b03c60f3 11372 {
53a5960a 11373 uid_t ruid, euid, suid;
b03c60f3
FB
11374 ret = get_errno(getresuid(&ruid, &euid, &suid));
11375 if (!is_error(ret)) {
2f619698
FB
11376 if (put_user_u32(ruid, arg1)
11377 || put_user_u32(euid, arg2)
11378 || put_user_u32(suid, arg3))
11379 goto efault;
b03c60f3
FB
11380 }
11381 }
11382 break;
a315a145
FB
11383#endif
11384#ifdef TARGET_NR_setresgid32
31e31b8a 11385 case TARGET_NR_setresgid32:
fd6f7798 11386 ret = get_errno(sys_setresgid(arg1, arg2, arg3));
b03c60f3 11387 break;
a315a145
FB
11388#endif
11389#ifdef TARGET_NR_getresgid32
31e31b8a 11390 case TARGET_NR_getresgid32:
b03c60f3 11391 {
53a5960a 11392 gid_t rgid, egid, sgid;
b03c60f3
FB
11393 ret = get_errno(getresgid(&rgid, &egid, &sgid));
11394 if (!is_error(ret)) {
2f619698
FB
11395 if (put_user_u32(rgid, arg1)
11396 || put_user_u32(egid, arg2)
11397 || put_user_u32(sgid, arg3))
11398 goto efault;
b03c60f3
FB
11399 }
11400 }
11401 break;
a315a145
FB
11402#endif
11403#ifdef TARGET_NR_chown32
31e31b8a 11404 case TARGET_NR_chown32:
579a97f7
FB
11405 if (!(p = lock_user_string(arg1)))
11406 goto efault;
53a5960a
PB
11407 ret = get_errno(chown(p, arg2, arg3));
11408 unlock_user(p, arg1, 0);
b03c60f3 11409 break;
a315a145
FB
11410#endif
11411#ifdef TARGET_NR_setuid32
31e31b8a 11412 case TARGET_NR_setuid32:
fd6f7798 11413 ret = get_errno(sys_setuid(arg1));
b03c60f3 11414 break;
a315a145
FB
11415#endif
11416#ifdef TARGET_NR_setgid32
31e31b8a 11417 case TARGET_NR_setgid32:
fd6f7798 11418 ret = get_errno(sys_setgid(arg1));
b03c60f3 11419 break;
a315a145
FB
11420#endif
11421#ifdef TARGET_NR_setfsuid32
31e31b8a 11422 case TARGET_NR_setfsuid32:
b03c60f3
FB
11423 ret = get_errno(setfsuid(arg1));
11424 break;
a315a145
FB
11425#endif
11426#ifdef TARGET_NR_setfsgid32
31e31b8a 11427 case TARGET_NR_setfsgid32:
b03c60f3
FB
11428 ret = get_errno(setfsgid(arg1));
11429 break;
a315a145 11430#endif
67867308 11431
31e31b8a 11432 case TARGET_NR_pivot_root:
b03c60f3 11433 goto unimplemented;
ffa65c3b 11434#ifdef TARGET_NR_mincore
31e31b8a 11435 case TARGET_NR_mincore:
04bb9ace
AJ
11436 {
11437 void *a;
98a3331a
FSM
11438 ret = -TARGET_ENOMEM;
11439 a = lock_user(VERIFY_READ, arg1, arg2, 0);
11440 if (!a) {
11441 goto fail;
11442 }
04bb9ace 11443 ret = -TARGET_EFAULT;
98a3331a
FSM
11444 p = lock_user_string(arg3);
11445 if (!p) {
04bb9ace 11446 goto mincore_fail;
98a3331a 11447 }
04bb9ace
AJ
11448 ret = get_errno(mincore(a, arg2, p));
11449 unlock_user(p, arg3, ret);
11450 mincore_fail:
11451 unlock_user(a, arg1, 0);
11452 }
11453 break;
ffa65c3b 11454#endif
408321b6
AJ
11455#ifdef TARGET_NR_arm_fadvise64_64
11456 case TARGET_NR_arm_fadvise64_64:
e0156a9d
PM
11457 /* arm_fadvise64_64 looks like fadvise64_64 but
11458 * with different argument order: fd, advice, offset, len
11459 * rather than the usual fd, offset, len, advice.
11460 * Note that offset and len are both 64-bit so appear as
11461 * pairs of 32-bit registers.
11462 */
11463 ret = posix_fadvise(arg1, target_offset64(arg3, arg4),
11464 target_offset64(arg5, arg6), arg2);
11465 ret = -host_to_target_errno(ret);
11466 break;
408321b6 11467#endif
badd3cd8
PM
11468
11469#if TARGET_ABI_BITS == 32
11470
11471#ifdef TARGET_NR_fadvise64_64
11472 case TARGET_NR_fadvise64_64:
43046b5a
LV
11473#if defined(TARGET_PPC)
11474 /* 6 args: fd, advice, offset (high, low), len (high, low) */
11475 ret = arg2;
11476 arg2 = arg3;
11477 arg3 = arg4;
11478 arg4 = arg5;
11479 arg5 = arg6;
11480 arg6 = ret;
11481#else
badd3cd8 11482 /* 6 args: fd, offset (high, low), len (high, low), advice */
8bf8e9df 11483 if (regpairs_aligned(cpu_env, num)) {
badd3cd8
PM
11484 /* offset is in (3,4), len in (5,6) and advice in 7 */
11485 arg2 = arg3;
11486 arg3 = arg4;
11487 arg4 = arg5;
11488 arg5 = arg6;
11489 arg6 = arg7;
11490 }
43046b5a 11491#endif
badd3cd8
PM
11492 ret = -host_to_target_errno(posix_fadvise(arg1,
11493 target_offset64(arg2, arg3),
11494 target_offset64(arg4, arg5),
11495 arg6));
11496 break;
11497#endif
11498
11499#ifdef TARGET_NR_fadvise64
11500 case TARGET_NR_fadvise64:
11501 /* 5 args: fd, offset (high, low), len, advice */
8bf8e9df 11502 if (regpairs_aligned(cpu_env, num)) {
badd3cd8
PM
11503 /* offset is in (3,4), len in 5 and advice in 6 */
11504 arg2 = arg3;
11505 arg3 = arg4;
11506 arg4 = arg5;
11507 arg5 = arg6;
11508 }
11509 ret = -host_to_target_errno(posix_fadvise(arg1,
11510 target_offset64(arg2, arg3),
11511 arg4, arg5));
11512 break;
408321b6 11513#endif
badd3cd8
PM
11514
11515#else /* not a 32-bit ABI */
e0156a9d 11516#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_fadvise64)
408321b6
AJ
11517#ifdef TARGET_NR_fadvise64_64
11518 case TARGET_NR_fadvise64_64:
11519#endif
e72d2cc7
UH
11520#ifdef TARGET_NR_fadvise64
11521 case TARGET_NR_fadvise64:
11522#endif
11523#ifdef TARGET_S390X
11524 switch (arg4) {
11525 case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
11526 case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
11527 case 6: arg4 = POSIX_FADV_DONTNEED; break;
11528 case 7: arg4 = POSIX_FADV_NOREUSE; break;
11529 default: break;
11530 }
11531#endif
977d8241
PM
11532 ret = -host_to_target_errno(posix_fadvise(arg1, arg2, arg3, arg4));
11533 break;
408321b6 11534#endif
badd3cd8
PM
11535#endif /* end of 64-bit ABI fadvise handling */
11536
ffa65c3b 11537#ifdef TARGET_NR_madvise
31e31b8a 11538 case TARGET_NR_madvise:
24836689 11539 /* A straight passthrough may not be safe because qemu sometimes
d2d6b857 11540 turns private file-backed mappings into anonymous mappings.
24836689
PB
11541 This will break MADV_DONTNEED.
11542 This is a hint, so ignoring and returning success is ok. */
11543 ret = get_errno(0);
11544 break;
ffa65c3b 11545#endif
992f48a0 11546#if TARGET_ABI_BITS == 32
31e31b8a 11547 case TARGET_NR_fcntl64:
77e4672d 11548 {
b1e341eb 11549 int cmd;
77e4672d 11550 struct flock64 fl;
213d3e9e
PM
11551 from_flock64_fn *copyfrom = copy_from_user_flock64;
11552 to_flock64_fn *copyto = copy_to_user_flock64;
11553
ce4defa0 11554#ifdef TARGET_ARM
213d3e9e
PM
11555 if (((CPUARMState *)cpu_env)->eabi) {
11556 copyfrom = copy_from_user_eabi_flock64;
11557 copyto = copy_to_user_eabi_flock64;
11558 }
ce4defa0 11559#endif
77e4672d 11560
5f106811 11561 cmd = target_to_host_fcntl_cmd(arg2);
31b63193
PM
11562 if (cmd == -TARGET_EINVAL) {
11563 ret = cmd;
11564 break;
11565 }
b1e341eb 11566
60cd49d5 11567 switch(arg2) {
b1e341eb 11568 case TARGET_F_GETLK64:
213d3e9e
PM
11569 ret = copyfrom(&fl, arg3);
11570 if (ret) {
11571 break;
5813427b 11572 }
b1e341eb 11573 ret = get_errno(fcntl(arg1, cmd, &fl));
213d3e9e
PM
11574 if (ret == 0) {
11575 ret = copyto(arg3, &fl);
11576 }
77e4672d
FB
11577 break;
11578
b1e341eb
TS
11579 case TARGET_F_SETLK64:
11580 case TARGET_F_SETLKW64:
213d3e9e
PM
11581 ret = copyfrom(&fl, arg3);
11582 if (ret) {
11583 break;
ce4defa0 11584 }
435da5e7 11585 ret = get_errno(safe_fcntl(arg1, cmd, &fl));
77e4672d 11586 break;
60cd49d5 11587 default:
5f106811 11588 ret = do_fcntl(arg1, arg2, arg3);
60cd49d5
FB
11589 break;
11590 }
77e4672d
FB
11591 break;
11592 }
60cd49d5 11593#endif
7d600c80
TS
11594#ifdef TARGET_NR_cacheflush
11595 case TARGET_NR_cacheflush:
11596 /* self-modifying code is handled automatically, so nothing needed */
11597 ret = 0;
11598 break;
11599#endif
ebc05488 11600#ifdef TARGET_NR_security
31e31b8a
FB
11601 case TARGET_NR_security:
11602 goto unimplemented;
c573ff67
FB
11603#endif
11604#ifdef TARGET_NR_getpagesize
11605 case TARGET_NR_getpagesize:
11606 ret = TARGET_PAGE_SIZE;
11607 break;
ebc05488 11608#endif
31e31b8a
FB
11609 case TARGET_NR_gettid:
11610 ret = get_errno(gettid());
11611 break;
e5febef5 11612#ifdef TARGET_NR_readahead
31e31b8a 11613 case TARGET_NR_readahead:
2054ac9b 11614#if TARGET_ABI_BITS == 32
8bf8e9df 11615 if (regpairs_aligned(cpu_env, num)) {
2054ac9b
AJ
11616 arg2 = arg3;
11617 arg3 = arg4;
11618 arg4 = arg5;
11619 }
77c6850f 11620 ret = get_errno(readahead(arg1, target_offset64(arg2, arg3) , arg4));
2054ac9b
AJ
11621#else
11622 ret = get_errno(readahead(arg1, arg2, arg3));
11623#endif
11624 break;
e5febef5 11625#endif
a790ae38 11626#ifdef CONFIG_ATTR
ebc05488 11627#ifdef TARGET_NR_setxattr
31e31b8a
FB
11628 case TARGET_NR_listxattr:
11629 case TARGET_NR_llistxattr:
fb5590f7
PM
11630 {
11631 void *p, *b = 0;
11632 if (arg2) {
11633 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
11634 if (!b) {
11635 ret = -TARGET_EFAULT;
11636 break;
11637 }
11638 }
11639 p = lock_user_string(arg1);
11640 if (p) {
11641 if (num == TARGET_NR_listxattr) {
11642 ret = get_errno(listxattr(p, b, arg3));
11643 } else {
11644 ret = get_errno(llistxattr(p, b, arg3));
11645 }
11646 } else {
11647 ret = -TARGET_EFAULT;
11648 }
11649 unlock_user(p, arg1, 0);
11650 unlock_user(b, arg2, arg3);
11651 break;
11652 }
31e31b8a 11653 case TARGET_NR_flistxattr:
fb5590f7
PM
11654 {
11655 void *b = 0;
11656 if (arg2) {
11657 b = lock_user(VERIFY_WRITE, arg2, arg3, 0);
11658 if (!b) {
11659 ret = -TARGET_EFAULT;
11660 break;
11661 }
11662 }
11663 ret = get_errno(flistxattr(arg1, b, arg3));
11664 unlock_user(b, arg2, arg3);
6f932f91 11665 break;
fb5590f7 11666 }
a790ae38 11667 case TARGET_NR_setxattr:
30297b55 11668 case TARGET_NR_lsetxattr:
a790ae38 11669 {
e3c33ec6
PM
11670 void *p, *n, *v = 0;
11671 if (arg3) {
11672 v = lock_user(VERIFY_READ, arg3, arg4, 1);
11673 if (!v) {
11674 ret = -TARGET_EFAULT;
11675 break;
11676 }
11677 }
a790ae38
ACH
11678 p = lock_user_string(arg1);
11679 n = lock_user_string(arg2);
e3c33ec6 11680 if (p && n) {
30297b55
PM
11681 if (num == TARGET_NR_setxattr) {
11682 ret = get_errno(setxattr(p, n, v, arg4, arg5));
11683 } else {
11684 ret = get_errno(lsetxattr(p, n, v, arg4, arg5));
11685 }
a790ae38
ACH
11686 } else {
11687 ret = -TARGET_EFAULT;
11688 }
11689 unlock_user(p, arg1, 0);
11690 unlock_user(n, arg2, 0);
11691 unlock_user(v, arg3, 0);
11692 }
11693 break;
30297b55
PM
11694 case TARGET_NR_fsetxattr:
11695 {
11696 void *n, *v = 0;
11697 if (arg3) {
11698 v = lock_user(VERIFY_READ, arg3, arg4, 1);
11699 if (!v) {
11700 ret = -TARGET_EFAULT;
11701 break;
11702 }
11703 }
11704 n = lock_user_string(arg2);
11705 if (n) {
11706 ret = get_errno(fsetxattr(arg1, n, v, arg4, arg5));
11707 } else {
11708 ret = -TARGET_EFAULT;
11709 }
11710 unlock_user(n, arg2, 0);
11711 unlock_user(v, arg3, 0);
11712 }
11713 break;
a790ae38 11714 case TARGET_NR_getxattr:
30297b55 11715 case TARGET_NR_lgetxattr:
a790ae38 11716 {
e3c33ec6
PM
11717 void *p, *n, *v = 0;
11718 if (arg3) {
11719 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
11720 if (!v) {
11721 ret = -TARGET_EFAULT;
11722 break;
11723 }
11724 }
a790ae38
ACH
11725 p = lock_user_string(arg1);
11726 n = lock_user_string(arg2);
e3c33ec6 11727 if (p && n) {
30297b55
PM
11728 if (num == TARGET_NR_getxattr) {
11729 ret = get_errno(getxattr(p, n, v, arg4));
11730 } else {
11731 ret = get_errno(lgetxattr(p, n, v, arg4));
11732 }
a790ae38
ACH
11733 } else {
11734 ret = -TARGET_EFAULT;
11735 }
11736 unlock_user(p, arg1, 0);
11737 unlock_user(n, arg2, 0);
11738 unlock_user(v, arg3, arg4);
11739 }
11740 break;
30297b55
PM
11741 case TARGET_NR_fgetxattr:
11742 {
11743 void *n, *v = 0;
11744 if (arg3) {
11745 v = lock_user(VERIFY_WRITE, arg3, arg4, 0);
11746 if (!v) {
11747 ret = -TARGET_EFAULT;
11748 break;
11749 }
11750 }
11751 n = lock_user_string(arg2);
11752 if (n) {
11753 ret = get_errno(fgetxattr(arg1, n, v, arg4));
11754 } else {
11755 ret = -TARGET_EFAULT;
11756 }
11757 unlock_user(n, arg2, 0);
11758 unlock_user(v, arg3, arg4);
11759 }
11760 break;
a790ae38 11761 case TARGET_NR_removexattr:
30297b55 11762 case TARGET_NR_lremovexattr:
a790ae38
ACH
11763 {
11764 void *p, *n;
11765 p = lock_user_string(arg1);
11766 n = lock_user_string(arg2);
11767 if (p && n) {
30297b55
PM
11768 if (num == TARGET_NR_removexattr) {
11769 ret = get_errno(removexattr(p, n));
11770 } else {
11771 ret = get_errno(lremovexattr(p, n));
11772 }
a790ae38
ACH
11773 } else {
11774 ret = -TARGET_EFAULT;
11775 }
11776 unlock_user(p, arg1, 0);
11777 unlock_user(n, arg2, 0);
11778 }
11779 break;
30297b55
PM
11780 case TARGET_NR_fremovexattr:
11781 {
11782 void *n;
11783 n = lock_user_string(arg2);
11784 if (n) {
11785 ret = get_errno(fremovexattr(arg1, n));
11786 } else {
11787 ret = -TARGET_EFAULT;
11788 }
11789 unlock_user(n, arg2, 0);
11790 }
11791 break;
ebc05488 11792#endif
a790ae38 11793#endif /* CONFIG_ATTR */
ebc05488 11794#ifdef TARGET_NR_set_thread_area
5cd4393b 11795 case TARGET_NR_set_thread_area:
8d18e893 11796#if defined(TARGET_MIPS)
d279279e 11797 ((CPUMIPSState *) cpu_env)->active_tc.CP0_UserLocal = arg1;
6f5b89a0
TS
11798 ret = 0;
11799 break;
ef96779b
EI
11800#elif defined(TARGET_CRIS)
11801 if (arg1 & 0xff)
11802 ret = -TARGET_EINVAL;
11803 else {
11804 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
11805 ret = 0;
11806 }
11807 break;
8d18e893
FB
11808#elif defined(TARGET_I386) && defined(TARGET_ABI32)
11809 ret = do_set_thread_area(cpu_env, arg1);
11810 break;
1ccd9374
PM
11811#elif defined(TARGET_M68K)
11812 {
0429a971 11813 TaskState *ts = cpu->opaque;
1ccd9374 11814 ts->tp_value = arg1;
95c1eb19 11815 ret = 0;
1ccd9374
PM
11816 break;
11817 }
6f5b89a0
TS
11818#else
11819 goto unimplemented_nowarn;
11820#endif
11821#endif
11822#ifdef TARGET_NR_get_thread_area
5cd4393b 11823 case TARGET_NR_get_thread_area:
8d18e893
FB
11824#if defined(TARGET_I386) && defined(TARGET_ABI32)
11825 ret = do_get_thread_area(cpu_env, arg1);
d312bbe1 11826 break;
1ccd9374
PM
11827#elif defined(TARGET_M68K)
11828 {
0429a971 11829 TaskState *ts = cpu->opaque;
1ccd9374
PM
11830 ret = ts->tp_value;
11831 break;
11832 }
8d18e893 11833#else
5cd4393b 11834 goto unimplemented_nowarn;
48dc41eb 11835#endif
8d18e893 11836#endif
48dc41eb
FB
11837#ifdef TARGET_NR_getdomainname
11838 case TARGET_NR_getdomainname:
11839 goto unimplemented_nowarn;
ebc05488 11840#endif
6f5b89a0 11841
b5906f95
TS
11842#ifdef TARGET_NR_clock_gettime
11843 case TARGET_NR_clock_gettime:
11844 {
11845 struct timespec ts;
11846 ret = get_errno(clock_gettime(arg1, &ts));
11847 if (!is_error(ret)) {
11848 host_to_target_timespec(arg2, &ts);
11849 }
11850 break;
11851 }
11852#endif
11853#ifdef TARGET_NR_clock_getres
11854 case TARGET_NR_clock_getres:
11855 {
11856 struct timespec ts;
11857 ret = get_errno(clock_getres(arg1, &ts));
11858 if (!is_error(ret)) {
11859 host_to_target_timespec(arg2, &ts);
11860 }
11861 break;
11862 }
11863#endif
63d7651b
PB
11864#ifdef TARGET_NR_clock_nanosleep
11865 case TARGET_NR_clock_nanosleep:
11866 {
11867 struct timespec ts;
11868 target_to_host_timespec(&ts, arg3);
9e518226
PM
11869 ret = get_errno(safe_clock_nanosleep(arg1, arg2,
11870 &ts, arg4 ? &ts : NULL));
63d7651b
PB
11871 if (arg4)
11872 host_to_target_timespec(arg4, &ts);
8fbe8fdf
TM
11873
11874#if defined(TARGET_PPC)
11875 /* clock_nanosleep is odd in that it returns positive errno values.
11876 * On PPC, CR0 bit 3 should be set in such a situation. */
9e518226 11877 if (ret && ret != -TARGET_ERESTARTSYS) {
8fbe8fdf
TM
11878 ((CPUPPCState *)cpu_env)->crf[0] |= 1;
11879 }
11880#endif
63d7651b
PB
11881 break;
11882 }
11883#endif
b5906f95 11884
6f5b89a0
TS
11885#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
11886 case TARGET_NR_set_tid_address:
579a97f7
FB
11887 ret = get_errno(set_tid_address((int *)g2h(arg1)));
11888 break;
6f5b89a0
TS
11889#endif
11890
4cae1d16 11891 case TARGET_NR_tkill:
bef653d9 11892 ret = get_errno(safe_tkill((int)arg1, target_to_host_signal(arg2)));
4cae1d16 11893 break;
4cae1d16 11894
71455574 11895 case TARGET_NR_tgkill:
bef653d9 11896 ret = get_errno(safe_tgkill((int)arg1, (int)arg2,
4cb05961 11897 target_to_host_signal(arg3)));
bef653d9 11898 break;
71455574 11899
4f2b1fe8
TS
11900#ifdef TARGET_NR_set_robust_list
11901 case TARGET_NR_set_robust_list:
e9a970a8
PM
11902 case TARGET_NR_get_robust_list:
11903 /* The ABI for supporting robust futexes has userspace pass
11904 * the kernel a pointer to a linked list which is updated by
11905 * userspace after the syscall; the list is walked by the kernel
11906 * when the thread exits. Since the linked list in QEMU guest
11907 * memory isn't a valid linked list for the host and we have
11908 * no way to reliably intercept the thread-death event, we can't
11909 * support these. Silently return ENOSYS so that guest userspace
11910 * falls back to a non-robust futex implementation (which should
11911 * be OK except in the corner case of the guest crashing while
11912 * holding a mutex that is shared with another process via
11913 * shared memory).
11914 */
11915 goto unimplemented_nowarn;
4f2b1fe8
TS
11916#endif
11917
1acae9f2 11918#if defined(TARGET_NR_utimensat)
9007f0ef
TS
11919 case TARGET_NR_utimensat:
11920 {
ebc996f3
RV
11921 struct timespec *tsp, ts[2];
11922 if (!arg3) {
11923 tsp = NULL;
11924 } else {
11925 target_to_host_timespec(ts, arg3);
11926 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
11927 tsp = ts;
11928 }
9007f0ef 11929 if (!arg2)
ebc996f3 11930 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
9007f0ef 11931 else {
579a97f7 11932 if (!(p = lock_user_string(arg2))) {
0da46a6e 11933 ret = -TARGET_EFAULT;
579a97f7
FB
11934 goto fail;
11935 }
ebc996f3 11936 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
579a97f7 11937 unlock_user(p, arg2, 0);
9007f0ef
TS
11938 }
11939 }
11940 break;
11941#endif
bd0c5661
PB
11942 case TARGET_NR_futex:
11943 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
11944 break;
dbfe4c36 11945#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
39b59763
AJ
11946 case TARGET_NR_inotify_init:
11947 ret = get_errno(sys_inotify_init());
b929f7e5
PMD
11948 if (ret >= 0) {
11949 fd_trans_register(ret, &target_inotify_trans);
11950 }
39b59763
AJ
11951 break;
11952#endif
a1606b0b 11953#ifdef CONFIG_INOTIFY1
c05c7a73
RV
11954#if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
11955 case TARGET_NR_inotify_init1:
fea243e9
LD
11956 ret = get_errno(sys_inotify_init1(target_to_host_bitmask(arg1,
11957 fcntl_flags_tbl)));
b929f7e5
PMD
11958 if (ret >= 0) {
11959 fd_trans_register(ret, &target_inotify_trans);
11960 }
c05c7a73
RV
11961 break;
11962#endif
a1606b0b 11963#endif
dbfe4c36 11964#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
39b59763
AJ
11965 case TARGET_NR_inotify_add_watch:
11966 p = lock_user_string(arg2);
11967 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
11968 unlock_user(p, arg2, 0);
11969 break;
11970#endif
dbfe4c36 11971#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
39b59763
AJ
11972 case TARGET_NR_inotify_rm_watch:
11973 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
11974 break;
11975#endif
9007f0ef 11976
8ec9cf89 11977#if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
24e1003a
AJ
11978 case TARGET_NR_mq_open:
11979 {
c7536ab6 11980 struct mq_attr posix_mq_attr;
26400775 11981 struct mq_attr *pposix_mq_attr;
c7536ab6 11982 int host_flags;
24e1003a 11983
c7536ab6 11984 host_flags = target_to_host_bitmask(arg2, fcntl_flags_tbl);
26400775
LD
11985 pposix_mq_attr = NULL;
11986 if (arg4) {
11987 if (copy_from_user_mq_attr(&posix_mq_attr, arg4) != 0) {
11988 goto efault;
11989 }
11990 pposix_mq_attr = &posix_mq_attr;
c7536ab6 11991 }
24e1003a 11992 p = lock_user_string(arg1 - 1);
c7536ab6
AM
11993 if (!p) {
11994 goto efault;
b6ce1f6b 11995 }
26400775 11996 ret = get_errno(mq_open(p, host_flags, arg3, pposix_mq_attr));
24e1003a
AJ
11997 unlock_user (p, arg1, 0);
11998 }
11999 break;
12000
12001 case TARGET_NR_mq_unlink:
12002 p = lock_user_string(arg1 - 1);
3211215e
PM
12003 if (!p) {
12004 ret = -TARGET_EFAULT;
12005 break;
12006 }
24e1003a
AJ
12007 ret = get_errno(mq_unlink(p));
12008 unlock_user (p, arg1, 0);
12009 break;
12010
12011 case TARGET_NR_mq_timedsend:
12012 {
12013 struct timespec ts;
12014
12015 p = lock_user (VERIFY_READ, arg2, arg3, 1);
12016 if (arg5 != 0) {
12017 target_to_host_timespec(&ts, arg5);
d40ecd66 12018 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, &ts));
24e1003a 12019 host_to_target_timespec(arg5, &ts);
d40ecd66
PM
12020 } else {
12021 ret = get_errno(safe_mq_timedsend(arg1, p, arg3, arg4, NULL));
24e1003a 12022 }
24e1003a
AJ
12023 unlock_user (p, arg2, arg3);
12024 }
12025 break;
12026
12027 case TARGET_NR_mq_timedreceive:
12028 {
12029 struct timespec ts;
12030 unsigned int prio;
12031
12032 p = lock_user (VERIFY_READ, arg2, arg3, 1);
12033 if (arg5 != 0) {
12034 target_to_host_timespec(&ts, arg5);
d40ecd66
PM
12035 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
12036 &prio, &ts));
24e1003a 12037 host_to_target_timespec(arg5, &ts);
d40ecd66
PM
12038 } else {
12039 ret = get_errno(safe_mq_timedreceive(arg1, p, arg3,
12040 &prio, NULL));
24e1003a 12041 }
24e1003a
AJ
12042 unlock_user (p, arg2, arg3);
12043 if (arg4 != 0)
12044 put_user_u32(prio, arg4);
12045 }
12046 break;
12047
12048 /* Not implemented for now... */
12049/* case TARGET_NR_mq_notify: */
12050/* break; */
12051
12052 case TARGET_NR_mq_getsetattr:
12053 {
12054 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
12055 ret = 0;
12056 if (arg3 != 0) {
12057 ret = mq_getattr(arg1, &posix_mq_attr_out);
12058 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
12059 }
12060 if (arg2 != 0) {
12061 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
12062 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
12063 }
12064
12065 }
12066 break;
12067#endif
12068
3ce34dfb
VS
12069#ifdef CONFIG_SPLICE
12070#ifdef TARGET_NR_tee
12071 case TARGET_NR_tee:
12072 {
12073 ret = get_errno(tee(arg1,arg2,arg3,arg4));
12074 }
12075 break;
12076#endif
12077#ifdef TARGET_NR_splice
12078 case TARGET_NR_splice:
12079 {
12080 loff_t loff_in, loff_out;
12081 loff_t *ploff_in = NULL, *ploff_out = NULL;
17644b36
AS
12082 if (arg2) {
12083 if (get_user_u64(loff_in, arg2)) {
12084 goto efault;
12085 }
3ce34dfb
VS
12086 ploff_in = &loff_in;
12087 }
17644b36
AS
12088 if (arg4) {
12089 if (get_user_u64(loff_out, arg4)) {
12090 goto efault;
12091 }
3ce34dfb
VS
12092 ploff_out = &loff_out;
12093 }
12094 ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
17644b36
AS
12095 if (arg2) {
12096 if (put_user_u64(loff_in, arg2)) {
12097 goto efault;
12098 }
12099 }
12100 if (arg4) {
12101 if (put_user_u64(loff_out, arg4)) {
12102 goto efault;
12103 }
12104 }
3ce34dfb
VS
12105 }
12106 break;
12107#endif
12108#ifdef TARGET_NR_vmsplice
12109 case TARGET_NR_vmsplice:
12110 {
f287b2c2
RH
12111 struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
12112 if (vec != NULL) {
12113 ret = get_errno(vmsplice(arg1, vec, arg3, arg4));
12114 unlock_iovec(vec, arg2, arg3, 0);
12115 } else {
12116 ret = -host_to_target_errno(errno);
12117 }
3ce34dfb
VS
12118 }
12119 break;
12120#endif
12121#endif /* CONFIG_SPLICE */
c2882b96
RV
12122#ifdef CONFIG_EVENTFD
12123#if defined(TARGET_NR_eventfd)
12124 case TARGET_NR_eventfd:
12125 ret = get_errno(eventfd(arg1, 0));
b929f7e5
PMD
12126 if (ret >= 0) {
12127 fd_trans_register(ret, &target_eventfd_trans);
12128 }
c2882b96
RV
12129 break;
12130#endif
12131#if defined(TARGET_NR_eventfd2)
12132 case TARGET_NR_eventfd2:
5947c697
PJ
12133 {
12134 int host_flags = arg2 & (~(TARGET_O_NONBLOCK | TARGET_O_CLOEXEC));
12135 if (arg2 & TARGET_O_NONBLOCK) {
12136 host_flags |= O_NONBLOCK;
12137 }
12138 if (arg2 & TARGET_O_CLOEXEC) {
12139 host_flags |= O_CLOEXEC;
12140 }
12141 ret = get_errno(eventfd(arg1, host_flags));
b929f7e5
PMD
12142 if (ret >= 0) {
12143 fd_trans_register(ret, &target_eventfd_trans);
12144 }
c2882b96 12145 break;
5947c697 12146 }
c2882b96
RV
12147#endif
12148#endif /* CONFIG_EVENTFD */
d0927938
UH
12149#if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
12150 case TARGET_NR_fallocate:
20249ae1
AG
12151#if TARGET_ABI_BITS == 32
12152 ret = get_errno(fallocate(arg1, arg2, target_offset64(arg3, arg4),
12153 target_offset64(arg5, arg6)));
12154#else
d0927938 12155 ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
20249ae1 12156#endif
d0927938 12157 break;
c727f47d
PM
12158#endif
12159#if defined(CONFIG_SYNC_FILE_RANGE)
12160#if defined(TARGET_NR_sync_file_range)
12161 case TARGET_NR_sync_file_range:
12162#if TARGET_ABI_BITS == 32
bfcedc57
RV
12163#if defined(TARGET_MIPS)
12164 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
12165 target_offset64(arg5, arg6), arg7));
12166#else
c727f47d
PM
12167 ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
12168 target_offset64(arg4, arg5), arg6));
bfcedc57 12169#endif /* !TARGET_MIPS */
c727f47d
PM
12170#else
12171 ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
12172#endif
12173 break;
12174#endif
12175#if defined(TARGET_NR_sync_file_range2)
12176 case TARGET_NR_sync_file_range2:
12177 /* This is like sync_file_range but the arguments are reordered */
12178#if TARGET_ABI_BITS == 32
12179 ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
12180 target_offset64(arg5, arg6), arg2));
12181#else
12182 ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
12183#endif
12184 break;
12185#endif
3b6edd16 12186#endif
e36800c9
LV
12187#if defined(TARGET_NR_signalfd4)
12188 case TARGET_NR_signalfd4:
12189 ret = do_signalfd4(arg1, arg2, arg4);
12190 break;
12191#endif
12192#if defined(TARGET_NR_signalfd)
12193 case TARGET_NR_signalfd:
12194 ret = do_signalfd4(arg1, arg2, 0);
12195 break;
12196#endif
3b6edd16
PM
12197#if defined(CONFIG_EPOLL)
12198#if defined(TARGET_NR_epoll_create)
12199 case TARGET_NR_epoll_create:
12200 ret = get_errno(epoll_create(arg1));
12201 break;
12202#endif
12203#if defined(TARGET_NR_epoll_create1) && defined(CONFIG_EPOLL_CREATE1)
12204 case TARGET_NR_epoll_create1:
12205 ret = get_errno(epoll_create1(arg1));
12206 break;
12207#endif
12208#if defined(TARGET_NR_epoll_ctl)
12209 case TARGET_NR_epoll_ctl:
12210 {
12211 struct epoll_event ep;
12212 struct epoll_event *epp = 0;
12213 if (arg4) {
12214 struct target_epoll_event *target_ep;
12215 if (!lock_user_struct(VERIFY_READ, target_ep, arg4, 1)) {
12216 goto efault;
12217 }
12218 ep.events = tswap32(target_ep->events);
12219 /* The epoll_data_t union is just opaque data to the kernel,
12220 * so we transfer all 64 bits across and need not worry what
12221 * actual data type it is.
12222 */
12223 ep.data.u64 = tswap64(target_ep->data.u64);
12224 unlock_user_struct(target_ep, arg4, 0);
12225 epp = &ep;
12226 }
12227 ret = get_errno(epoll_ctl(arg1, arg2, arg3, epp));
12228 break;
12229 }
12230#endif
12231
227f0214 12232#if defined(TARGET_NR_epoll_wait) || defined(TARGET_NR_epoll_pwait)
3b6edd16
PM
12233#if defined(TARGET_NR_epoll_wait)
12234 case TARGET_NR_epoll_wait:
12235#endif
227f0214 12236#if defined(TARGET_NR_epoll_pwait)
3b6edd16
PM
12237 case TARGET_NR_epoll_pwait:
12238#endif
12239 {
12240 struct target_epoll_event *target_ep;
12241 struct epoll_event *ep;
12242 int epfd = arg1;
12243 int maxevents = arg3;
12244 int timeout = arg4;
12245
2ba7fae3
PM
12246 if (maxevents <= 0 || maxevents > TARGET_EP_MAX_EVENTS) {
12247 ret = -TARGET_EINVAL;
12248 break;
12249 }
12250
3b6edd16
PM
12251 target_ep = lock_user(VERIFY_WRITE, arg2,
12252 maxevents * sizeof(struct target_epoll_event), 1);
12253 if (!target_ep) {
12254 goto efault;
12255 }
12256
04c95f4d
PM
12257 ep = g_try_new(struct epoll_event, maxevents);
12258 if (!ep) {
12259 unlock_user(target_ep, arg2, 0);
12260 ret = -TARGET_ENOMEM;
12261 break;
12262 }
3b6edd16
PM
12263
12264 switch (num) {
227f0214 12265#if defined(TARGET_NR_epoll_pwait)
3b6edd16
PM
12266 case TARGET_NR_epoll_pwait:
12267 {
12268 target_sigset_t *target_set;
12269 sigset_t _set, *set = &_set;
12270
12271 if (arg5) {
c815701e
PM
12272 if (arg6 != sizeof(target_sigset_t)) {
12273 ret = -TARGET_EINVAL;
12274 break;
12275 }
12276
3b6edd16
PM
12277 target_set = lock_user(VERIFY_READ, arg5,
12278 sizeof(target_sigset_t), 1);
12279 if (!target_set) {
04c95f4d
PM
12280 ret = -TARGET_EFAULT;
12281 break;
3b6edd16
PM
12282 }
12283 target_to_host_sigset(set, target_set);
12284 unlock_user(target_set, arg5, 0);
12285 } else {
12286 set = NULL;
12287 }
12288
227f0214
PM
12289 ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
12290 set, SIGSET_T_SIZE));
3b6edd16
PM
12291 break;
12292 }
12293#endif
12294#if defined(TARGET_NR_epoll_wait)
12295 case TARGET_NR_epoll_wait:
227f0214
PM
12296 ret = get_errno(safe_epoll_pwait(epfd, ep, maxevents, timeout,
12297 NULL, 0));
3b6edd16
PM
12298 break;
12299#endif
12300 default:
12301 ret = -TARGET_ENOSYS;
12302 }
12303 if (!is_error(ret)) {
12304 int i;
12305 for (i = 0; i < ret; i++) {
12306 target_ep[i].events = tswap32(ep[i].events);
12307 target_ep[i].data.u64 = tswap64(ep[i].data.u64);
12308 }
04c95f4d
PM
12309 unlock_user(target_ep, arg2,
12310 ret * sizeof(struct target_epoll_event));
12311 } else {
12312 unlock_user(target_ep, arg2, 0);
3b6edd16 12313 }
04c95f4d 12314 g_free(ep);
3b6edd16
PM
12315 break;
12316 }
12317#endif
163a05a8
PM
12318#endif
12319#ifdef TARGET_NR_prlimit64
12320 case TARGET_NR_prlimit64:
12321 {
12322 /* args: pid, resource number, ptr to new rlimit, ptr to old rlimit */
12323 struct target_rlimit64 *target_rnew, *target_rold;
12324 struct host_rlimit64 rnew, rold, *rnewp = 0;
95018018 12325 int resource = target_to_host_resource(arg2);
163a05a8
PM
12326 if (arg3) {
12327 if (!lock_user_struct(VERIFY_READ, target_rnew, arg3, 1)) {
12328 goto efault;
12329 }
12330 rnew.rlim_cur = tswap64(target_rnew->rlim_cur);
12331 rnew.rlim_max = tswap64(target_rnew->rlim_max);
12332 unlock_user_struct(target_rnew, arg3, 0);
12333 rnewp = &rnew;
12334 }
12335
95018018 12336 ret = get_errno(sys_prlimit64(arg1, resource, rnewp, arg4 ? &rold : 0));
163a05a8
PM
12337 if (!is_error(ret) && arg4) {
12338 if (!lock_user_struct(VERIFY_WRITE, target_rold, arg4, 1)) {
12339 goto efault;
12340 }
12341 target_rold->rlim_cur = tswap64(rold.rlim_cur);
12342 target_rold->rlim_max = tswap64(rold.rlim_max);
12343 unlock_user_struct(target_rold, arg4, 1);
12344 }
12345 break;
12346 }
3d21d29c
RH
12347#endif
12348#ifdef TARGET_NR_gethostname
12349 case TARGET_NR_gethostname:
12350 {
12351 char *name = lock_user(VERIFY_WRITE, arg1, arg2, 0);
12352 if (name) {
12353 ret = get_errno(gethostname(name, arg2));
12354 unlock_user(name, arg1, arg2);
12355 } else {
12356 ret = -TARGET_EFAULT;
12357 }
12358 break;
12359 }
89aaf1a6
RV
12360#endif
12361#ifdef TARGET_NR_atomic_cmpxchg_32
12362 case TARGET_NR_atomic_cmpxchg_32:
12363 {
12364 /* should use start_exclusive from main.c */
12365 abi_ulong mem_value;
12366 if (get_user_u32(mem_value, arg6)) {
12367 target_siginfo_t info;
12368 info.si_signo = SIGSEGV;
12369 info.si_errno = 0;
12370 info.si_code = TARGET_SEGV_MAPERR;
12371 info._sifields._sigfault._addr = arg6;
9d2803f7
PM
12372 queue_signal((CPUArchState *)cpu_env, info.si_signo,
12373 QEMU_SI_FAULT, &info);
89aaf1a6
RV
12374 ret = 0xdeadbeef;
12375
12376 }
12377 if (mem_value == arg2)
12378 put_user_u32(arg1, arg6);
12379 ret = mem_value;
12380 break;
12381 }
12382#endif
12383#ifdef TARGET_NR_atomic_barrier
12384 case TARGET_NR_atomic_barrier:
12385 {
12386 /* Like the kernel implementation and the qemu arm barrier, no-op this? */
3b899ea7 12387 ret = 0;
89aaf1a6
RV
12388 break;
12389 }
d0927938 12390#endif
f4f1e10a
ECL
12391
12392#ifdef TARGET_NR_timer_create
12393 case TARGET_NR_timer_create:
12394 {
12395 /* args: clockid_t clockid, struct sigevent *sevp, timer_t *timerid */
12396
12397 struct sigevent host_sevp = { {0}, }, *phost_sevp = NULL;
f4f1e10a
ECL
12398
12399 int clkid = arg1;
12400 int timer_index = next_free_host_timer();
12401
12402 if (timer_index < 0) {
12403 ret = -TARGET_EAGAIN;
12404 } else {
12405 timer_t *phtimer = g_posix_timers + timer_index;
12406
12407 if (arg2) {
f4f1e10a 12408 phost_sevp = &host_sevp;
c065976f
PM
12409 ret = target_to_host_sigevent(phost_sevp, arg2);
12410 if (ret != 0) {
12411 break;
12412 }
f4f1e10a
ECL
12413 }
12414
12415 ret = get_errno(timer_create(clkid, phost_sevp, phtimer));
12416 if (ret) {
12417 phtimer = NULL;
12418 } else {
aecc8861 12419 if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer_t)) {
f4f1e10a
ECL
12420 goto efault;
12421 }
f4f1e10a
ECL
12422 }
12423 }
12424 break;
12425 }
12426#endif
12427
12428#ifdef TARGET_NR_timer_settime
12429 case TARGET_NR_timer_settime:
12430 {
12431 /* args: timer_t timerid, int flags, const struct itimerspec *new_value,
12432 * struct itimerspec * old_value */
aecc8861 12433 target_timer_t timerid = get_timer_id(arg1);
e52a99f7 12434
aecc8861
AG
12435 if (timerid < 0) {
12436 ret = timerid;
12437 } else if (arg3 == 0) {
f4f1e10a
ECL
12438 ret = -TARGET_EINVAL;
12439 } else {
e52a99f7 12440 timer_t htimer = g_posix_timers[timerid];
f4f1e10a
ECL
12441 struct itimerspec hspec_new = {{0},}, hspec_old = {{0},};
12442
40c80b5e
MAL
12443 if (target_to_host_itimerspec(&hspec_new, arg3)) {
12444 goto efault;
12445 }
f4f1e10a
ECL
12446 ret = get_errno(
12447 timer_settime(htimer, arg2, &hspec_new, &hspec_old));
40c80b5e
MAL
12448 if (arg4 && host_to_target_itimerspec(arg4, &hspec_old)) {
12449 goto efault;
12450 }
f4f1e10a
ECL
12451 }
12452 break;
12453 }
12454#endif
12455
12456#ifdef TARGET_NR_timer_gettime
12457 case TARGET_NR_timer_gettime:
12458 {
12459 /* args: timer_t timerid, struct itimerspec *curr_value */
aecc8861 12460 target_timer_t timerid = get_timer_id(arg1);
e52a99f7 12461
aecc8861
AG
12462 if (timerid < 0) {
12463 ret = timerid;
12464 } else if (!arg2) {
12465 ret = -TARGET_EFAULT;
f4f1e10a 12466 } else {
e52a99f7 12467 timer_t htimer = g_posix_timers[timerid];
f4f1e10a
ECL
12468 struct itimerspec hspec;
12469 ret = get_errno(timer_gettime(htimer, &hspec));
12470
12471 if (host_to_target_itimerspec(arg2, &hspec)) {
12472 ret = -TARGET_EFAULT;
12473 }
12474 }
12475 break;
12476 }
12477#endif
12478
12479#ifdef TARGET_NR_timer_getoverrun
12480 case TARGET_NR_timer_getoverrun:
12481 {
12482 /* args: timer_t timerid */
aecc8861 12483 target_timer_t timerid = get_timer_id(arg1);
e52a99f7 12484
aecc8861
AG
12485 if (timerid < 0) {
12486 ret = timerid;
f4f1e10a 12487 } else {
e52a99f7 12488 timer_t htimer = g_posix_timers[timerid];
f4f1e10a
ECL
12489 ret = get_errno(timer_getoverrun(htimer));
12490 }
e36800c9 12491 fd_trans_unregister(ret);
f4f1e10a
ECL
12492 break;
12493 }
12494#endif
12495
12496#ifdef TARGET_NR_timer_delete
12497 case TARGET_NR_timer_delete:
12498 {
12499 /* args: timer_t timerid */
aecc8861 12500 target_timer_t timerid = get_timer_id(arg1);
e52a99f7 12501
aecc8861
AG
12502 if (timerid < 0) {
12503 ret = timerid;
f4f1e10a 12504 } else {
e52a99f7 12505 timer_t htimer = g_posix_timers[timerid];
f4f1e10a 12506 ret = get_errno(timer_delete(htimer));
e52a99f7 12507 g_posix_timers[timerid] = 0;
f4f1e10a
ECL
12508 }
12509 break;
12510 }
12511#endif
12512
51834341
RV
12513#if defined(TARGET_NR_timerfd_create) && defined(CONFIG_TIMERFD)
12514 case TARGET_NR_timerfd_create:
12515 ret = get_errno(timerfd_create(arg1,
12516 target_to_host_bitmask(arg2, fcntl_flags_tbl)));
12517 break;
12518#endif
12519
12520#if defined(TARGET_NR_timerfd_gettime) && defined(CONFIG_TIMERFD)
12521 case TARGET_NR_timerfd_gettime:
12522 {
12523 struct itimerspec its_curr;
12524
12525 ret = get_errno(timerfd_gettime(arg1, &its_curr));
12526
12527 if (arg2 && host_to_target_itimerspec(arg2, &its_curr)) {
12528 goto efault;
12529 }
12530 }
12531 break;
12532#endif
12533
12534#if defined(TARGET_NR_timerfd_settime) && defined(CONFIG_TIMERFD)
12535 case TARGET_NR_timerfd_settime:
12536 {
12537 struct itimerspec its_new, its_old, *p_new;
12538
12539 if (arg3) {
12540 if (target_to_host_itimerspec(&its_new, arg3)) {
12541 goto efault;
12542 }
12543 p_new = &its_new;
12544 } else {
12545 p_new = NULL;
12546 }
12547
12548 ret = get_errno(timerfd_settime(arg1, arg2, p_new, &its_old));
12549
12550 if (arg4 && host_to_target_itimerspec(arg4, &its_old)) {
12551 goto efault;
12552 }
12553 }
12554 break;
12555#endif
12556
ab31cda3
PB
12557#if defined(TARGET_NR_ioprio_get) && defined(__NR_ioprio_get)
12558 case TARGET_NR_ioprio_get:
12559 ret = get_errno(ioprio_get(arg1, arg2));
12560 break;
12561#endif
12562
12563#if defined(TARGET_NR_ioprio_set) && defined(__NR_ioprio_set)
12564 case TARGET_NR_ioprio_set:
12565 ret = get_errno(ioprio_set(arg1, arg2, arg3));
12566 break;
12567#endif
12568
9af5c906
RV
12569#if defined(TARGET_NR_setns) && defined(CONFIG_SETNS)
12570 case TARGET_NR_setns:
12571 ret = get_errno(setns(arg1, arg2));
12572 break;
12573#endif
12574#if defined(TARGET_NR_unshare) && defined(CONFIG_SETNS)
12575 case TARGET_NR_unshare:
12576 ret = get_errno(unshare(arg1));
12577 break;
12578#endif
2f14788c
LV
12579#if defined(TARGET_NR_kcmp) && defined(__NR_kcmp)
12580 case TARGET_NR_kcmp:
12581 ret = get_errno(kcmp(arg1, arg2, arg3, arg4, arg5));
12582 break;
12583#endif
9af5c906 12584
31e31b8a
FB
12585 default:
12586 unimplemented:
5cd4393b 12587 gemu_log("qemu: Unsupported syscall: %d\n", num);
4f2b1fe8 12588#if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
5cd4393b 12589 unimplemented_nowarn:
80a9d035 12590#endif
0da46a6e 12591 ret = -TARGET_ENOSYS;
31e31b8a
FB
12592 break;
12593 }
579a97f7 12594fail:
c573ff67 12595#ifdef DEBUG
0bf9e31a 12596 gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
c573ff67 12597#endif
b92c47c1
TS
12598 if(do_strace)
12599 print_syscall_ret(num, ret);
9c15e700 12600 trace_guest_user_syscall_ret(cpu, num, ret);
31e31b8a 12601 return ret;
579a97f7
FB
12602efault:
12603 ret = -TARGET_EFAULT;
12604 goto fail;
31e31b8a 12605}