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