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