xref: /openbmc/qemu/linux-user/syscall.c (revision 285da2b9)
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 <stdlib.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <string.h>
24 #include <elf.h>
25 #include <endian.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <time.h>
30 #include <limits.h>
31 #include <sys/types.h>
32 #include <sys/ipc.h>
33 #include <sys/msg.h>
34 #include <sys/wait.h>
35 #include <sys/time.h>
36 #include <sys/stat.h>
37 #include <sys/mount.h>
38 #include <sys/prctl.h>
39 #include <sys/resource.h>
40 #include <sys/mman.h>
41 #include <sys/swap.h>
42 #include <signal.h>
43 #include <sched.h>
44 #ifdef __ia64__
45 int __clone2(int (*fn)(void *), void *child_stack_base,
46              size_t stack_size, int flags, void *arg, ...);
47 #endif
48 #include <sys/socket.h>
49 #include <sys/un.h>
50 #include <sys/uio.h>
51 #include <sys/poll.h>
52 #include <sys/times.h>
53 #include <sys/shm.h>
54 #include <sys/sem.h>
55 #include <sys/statfs.h>
56 #include <utime.h>
57 #include <sys/sysinfo.h>
58 #include <sys/utsname.h>
59 //#include <sys/user.h>
60 #include <netinet/ip.h>
61 #include <netinet/tcp.h>
62 #include <qemu-common.h>
63 #ifdef TARGET_GPROF
64 #include <sys/gmon.h>
65 #endif
66 #ifdef CONFIG_EVENTFD
67 #include <sys/eventfd.h>
68 #endif
69 
70 #define termios host_termios
71 #define winsize host_winsize
72 #define termio host_termio
73 #define sgttyb host_sgttyb /* same as target */
74 #define tchars host_tchars /* same as target */
75 #define ltchars host_ltchars /* same as target */
76 
77 #include <linux/termios.h>
78 #include <linux/unistd.h>
79 #include <linux/utsname.h>
80 #include <linux/cdrom.h>
81 #include <linux/hdreg.h>
82 #include <linux/soundcard.h>
83 #include <linux/kd.h>
84 #include <linux/mtio.h>
85 #include <linux/fs.h>
86 #include <linux/fiemap.h>
87 #include <linux/fb.h>
88 #include <linux/vt.h>
89 #include "linux_loop.h"
90 #include "cpu-uname.h"
91 
92 #include "qemu.h"
93 #include "qemu-common.h"
94 
95 #if defined(CONFIG_USE_NPTL)
96 #define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
97     CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
98 #else
99 /* XXX: Hardcode the above values.  */
100 #define CLONE_NPTL_FLAGS2 0
101 #endif
102 
103 //#define DEBUG
104 
105 //#include <linux/msdos_fs.h>
106 #define	VFAT_IOCTL_READDIR_BOTH		_IOR('r', 1, struct linux_dirent [2])
107 #define	VFAT_IOCTL_READDIR_SHORT	_IOR('r', 2, struct linux_dirent [2])
108 
109 
110 #undef _syscall0
111 #undef _syscall1
112 #undef _syscall2
113 #undef _syscall3
114 #undef _syscall4
115 #undef _syscall5
116 #undef _syscall6
117 
118 #define _syscall0(type,name)		\
119 static type name (void)			\
120 {					\
121 	return syscall(__NR_##name);	\
122 }
123 
124 #define _syscall1(type,name,type1,arg1)		\
125 static type name (type1 arg1)			\
126 {						\
127 	return syscall(__NR_##name, arg1);	\
128 }
129 
130 #define _syscall2(type,name,type1,arg1,type2,arg2)	\
131 static type name (type1 arg1,type2 arg2)		\
132 {							\
133 	return syscall(__NR_##name, arg1, arg2);	\
134 }
135 
136 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3)	\
137 static type name (type1 arg1,type2 arg2,type3 arg3)		\
138 {								\
139 	return syscall(__NR_##name, arg1, arg2, arg3);		\
140 }
141 
142 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4)	\
143 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4)			\
144 {										\
145 	return syscall(__NR_##name, arg1, arg2, arg3, arg4);			\
146 }
147 
148 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,	\
149 		  type5,arg5)							\
150 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5)	\
151 {										\
152 	return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5);		\
153 }
154 
155 
156 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,	\
157 		  type5,arg5,type6,arg6)					\
158 static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,	\
159                   type6 arg6)							\
160 {										\
161 	return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6);	\
162 }
163 
164 
165 #define __NR_sys_uname __NR_uname
166 #define __NR_sys_faccessat __NR_faccessat
167 #define __NR_sys_fchmodat __NR_fchmodat
168 #define __NR_sys_fchownat __NR_fchownat
169 #define __NR_sys_fstatat64 __NR_fstatat64
170 #define __NR_sys_futimesat __NR_futimesat
171 #define __NR_sys_getcwd1 __NR_getcwd
172 #define __NR_sys_getdents __NR_getdents
173 #define __NR_sys_getdents64 __NR_getdents64
174 #define __NR_sys_getpriority __NR_getpriority
175 #define __NR_sys_linkat __NR_linkat
176 #define __NR_sys_mkdirat __NR_mkdirat
177 #define __NR_sys_mknodat __NR_mknodat
178 #define __NR_sys_newfstatat __NR_newfstatat
179 #define __NR_sys_openat __NR_openat
180 #define __NR_sys_readlinkat __NR_readlinkat
181 #define __NR_sys_renameat __NR_renameat
182 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
183 #define __NR_sys_symlinkat __NR_symlinkat
184 #define __NR_sys_syslog __NR_syslog
185 #define __NR_sys_tgkill __NR_tgkill
186 #define __NR_sys_tkill __NR_tkill
187 #define __NR_sys_unlinkat __NR_unlinkat
188 #define __NR_sys_utimensat __NR_utimensat
189 #define __NR_sys_futex __NR_futex
190 #define __NR_sys_inotify_init __NR_inotify_init
191 #define __NR_sys_inotify_add_watch __NR_inotify_add_watch
192 #define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
193 
194 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
195 #define __NR__llseek __NR_lseek
196 #endif
197 
198 #ifdef __NR_gettid
199 _syscall0(int, gettid)
200 #else
201 /* This is a replacement for the host gettid() and must return a host
202    errno. */
203 static int gettid(void) {
204     return -ENOSYS;
205 }
206 #endif
207 _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
208 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
209 _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
210 #endif
211 _syscall2(int, sys_getpriority, int, which, int, who);
212 #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
213 _syscall5(int, _llseek,  uint,  fd, ulong, hi, ulong, lo,
214           loff_t *, res, uint, wh);
215 #endif
216 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
217 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
218 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
219 _syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
220 #endif
221 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
222 _syscall2(int,sys_tkill,int,tid,int,sig)
223 #endif
224 #ifdef __NR_exit_group
225 _syscall1(int,exit_group,int,error_code)
226 #endif
227 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
228 _syscall1(int,set_tid_address,int *,tidptr)
229 #endif
230 #if defined(CONFIG_USE_NPTL)
231 #if defined(TARGET_NR_futex) && defined(__NR_futex)
232 _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
233           const struct timespec *,timeout,int *,uaddr2,int,val3)
234 #endif
235 #endif
236 
237 static bitmask_transtbl fcntl_flags_tbl[] = {
238   { TARGET_O_ACCMODE,   TARGET_O_WRONLY,    O_ACCMODE,   O_WRONLY,    },
239   { TARGET_O_ACCMODE,   TARGET_O_RDWR,      O_ACCMODE,   O_RDWR,      },
240   { TARGET_O_CREAT,     TARGET_O_CREAT,     O_CREAT,     O_CREAT,     },
241   { TARGET_O_EXCL,      TARGET_O_EXCL,      O_EXCL,      O_EXCL,      },
242   { TARGET_O_NOCTTY,    TARGET_O_NOCTTY,    O_NOCTTY,    O_NOCTTY,    },
243   { TARGET_O_TRUNC,     TARGET_O_TRUNC,     O_TRUNC,     O_TRUNC,     },
244   { TARGET_O_APPEND,    TARGET_O_APPEND,    O_APPEND,    O_APPEND,    },
245   { TARGET_O_NONBLOCK,  TARGET_O_NONBLOCK,  O_NONBLOCK,  O_NONBLOCK,  },
246   { TARGET_O_SYNC,      TARGET_O_SYNC,      O_SYNC,      O_SYNC,      },
247   { TARGET_FASYNC,      TARGET_FASYNC,      FASYNC,      FASYNC,      },
248   { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
249   { TARGET_O_NOFOLLOW,  TARGET_O_NOFOLLOW,  O_NOFOLLOW,  O_NOFOLLOW,  },
250   { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
251 #if defined(O_DIRECT)
252   { TARGET_O_DIRECT,    TARGET_O_DIRECT,    O_DIRECT,    O_DIRECT,    },
253 #endif
254   { 0, 0, 0, 0 }
255 };
256 
257 #define COPY_UTSNAME_FIELD(dest, src) \
258   do { \
259       /* __NEW_UTS_LEN doesn't include terminating null */ \
260       (void) strncpy((dest), (src), __NEW_UTS_LEN); \
261       (dest)[__NEW_UTS_LEN] = '\0'; \
262   } while (0)
263 
264 static int sys_uname(struct new_utsname *buf)
265 {
266   struct utsname uts_buf;
267 
268   if (uname(&uts_buf) < 0)
269       return (-1);
270 
271   /*
272    * Just in case these have some differences, we
273    * translate utsname to new_utsname (which is the
274    * struct linux kernel uses).
275    */
276 
277   bzero(buf, sizeof (*buf));
278   COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
279   COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
280   COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
281   COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
282   COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
283 #ifdef _GNU_SOURCE
284   COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
285 #endif
286   return (0);
287 
288 #undef COPY_UTSNAME_FIELD
289 }
290 
291 static int sys_getcwd1(char *buf, size_t size)
292 {
293   if (getcwd(buf, size) == NULL) {
294       /* getcwd() sets errno */
295       return (-1);
296   }
297   return strlen(buf)+1;
298 }
299 
300 #ifdef CONFIG_ATFILE
301 /*
302  * Host system seems to have atfile syscall stubs available.  We
303  * now enable them one by one as specified by target syscall_nr.h.
304  */
305 
306 #ifdef TARGET_NR_faccessat
307 static int sys_faccessat(int dirfd, const char *pathname, int mode)
308 {
309   return (faccessat(dirfd, pathname, mode, 0));
310 }
311 #endif
312 #ifdef TARGET_NR_fchmodat
313 static int sys_fchmodat(int dirfd, const char *pathname, mode_t mode)
314 {
315   return (fchmodat(dirfd, pathname, mode, 0));
316 }
317 #endif
318 #if defined(TARGET_NR_fchownat) && defined(USE_UID16)
319 static int sys_fchownat(int dirfd, const char *pathname, uid_t owner,
320     gid_t group, int flags)
321 {
322   return (fchownat(dirfd, pathname, owner, group, flags));
323 }
324 #endif
325 #ifdef __NR_fstatat64
326 static int sys_fstatat64(int dirfd, const char *pathname, struct stat *buf,
327     int flags)
328 {
329   return (fstatat(dirfd, pathname, buf, flags));
330 }
331 #endif
332 #ifdef __NR_newfstatat
333 static int sys_newfstatat(int dirfd, const char *pathname, struct stat *buf,
334     int flags)
335 {
336   return (fstatat(dirfd, pathname, buf, flags));
337 }
338 #endif
339 #ifdef TARGET_NR_futimesat
340 static int sys_futimesat(int dirfd, const char *pathname,
341     const struct timeval times[2])
342 {
343   return (futimesat(dirfd, pathname, times));
344 }
345 #endif
346 #ifdef TARGET_NR_linkat
347 static int sys_linkat(int olddirfd, const char *oldpath,
348     int newdirfd, const char *newpath, int flags)
349 {
350   return (linkat(olddirfd, oldpath, newdirfd, newpath, flags));
351 }
352 #endif
353 #ifdef TARGET_NR_mkdirat
354 static int sys_mkdirat(int dirfd, const char *pathname, mode_t mode)
355 {
356   return (mkdirat(dirfd, pathname, mode));
357 }
358 #endif
359 #ifdef TARGET_NR_mknodat
360 static int sys_mknodat(int dirfd, const char *pathname, mode_t mode,
361     dev_t dev)
362 {
363   return (mknodat(dirfd, pathname, mode, dev));
364 }
365 #endif
366 #ifdef TARGET_NR_openat
367 static int sys_openat(int dirfd, const char *pathname, int flags, ...)
368 {
369   /*
370    * open(2) has extra parameter 'mode' when called with
371    * flag O_CREAT.
372    */
373   if ((flags & O_CREAT) != 0) {
374       va_list ap;
375       mode_t mode;
376 
377       /*
378        * Get the 'mode' parameter and translate it to
379        * host bits.
380        */
381       va_start(ap, flags);
382       mode = va_arg(ap, mode_t);
383       mode = target_to_host_bitmask(mode, fcntl_flags_tbl);
384       va_end(ap);
385 
386       return (openat(dirfd, pathname, flags, mode));
387   }
388   return (openat(dirfd, pathname, flags));
389 }
390 #endif
391 #ifdef TARGET_NR_readlinkat
392 static int sys_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
393 {
394   return (readlinkat(dirfd, pathname, buf, bufsiz));
395 }
396 #endif
397 #ifdef TARGET_NR_renameat
398 static int sys_renameat(int olddirfd, const char *oldpath,
399     int newdirfd, const char *newpath)
400 {
401   return (renameat(olddirfd, oldpath, newdirfd, newpath));
402 }
403 #endif
404 #ifdef TARGET_NR_symlinkat
405 static int sys_symlinkat(const char *oldpath, int newdirfd, const char *newpath)
406 {
407   return (symlinkat(oldpath, newdirfd, newpath));
408 }
409 #endif
410 #ifdef TARGET_NR_unlinkat
411 static int sys_unlinkat(int dirfd, const char *pathname, int flags)
412 {
413   return (unlinkat(dirfd, pathname, flags));
414 }
415 #endif
416 #else /* !CONFIG_ATFILE */
417 
418 /*
419  * Try direct syscalls instead
420  */
421 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
422 _syscall3(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode)
423 #endif
424 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
425 _syscall3(int,sys_fchmodat,int,dirfd,const char *,pathname, mode_t,mode)
426 #endif
427 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat) && defined(USE_UID16)
428 _syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
429           uid_t,owner,gid_t,group,int,flags)
430 #endif
431 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
432         defined(__NR_fstatat64)
433 _syscall4(int,sys_fstatat64,int,dirfd,const char *,pathname,
434           struct stat *,buf,int,flags)
435 #endif
436 #if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
437 _syscall3(int,sys_futimesat,int,dirfd,const char *,pathname,
438          const struct timeval *,times)
439 #endif
440 #if (defined(TARGET_NR_newfstatat) || defined(TARGET_NR_fstatat64) ) && \
441         defined(__NR_newfstatat)
442 _syscall4(int,sys_newfstatat,int,dirfd,const char *,pathname,
443           struct stat *,buf,int,flags)
444 #endif
445 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
446 _syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
447       int,newdirfd,const char *,newpath,int,flags)
448 #endif
449 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
450 _syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
451 #endif
452 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
453 _syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
454           mode_t,mode,dev_t,dev)
455 #endif
456 #if defined(TARGET_NR_openat) && defined(__NR_openat)
457 _syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
458 #endif
459 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
460 _syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
461           char *,buf,size_t,bufsize)
462 #endif
463 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
464 _syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
465           int,newdirfd,const char *,newpath)
466 #endif
467 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
468 _syscall3(int,sys_symlinkat,const char *,oldpath,
469           int,newdirfd,const char *,newpath)
470 #endif
471 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
472 _syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
473 #endif
474 
475 #endif /* CONFIG_ATFILE */
476 
477 #ifdef CONFIG_UTIMENSAT
478 static int sys_utimensat(int dirfd, const char *pathname,
479     const struct timespec times[2], int flags)
480 {
481     if (pathname == NULL)
482         return futimens(dirfd, times);
483     else
484         return utimensat(dirfd, pathname, times, flags);
485 }
486 #else
487 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
488 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
489           const struct timespec *,tsp,int,flags)
490 #endif
491 #endif /* CONFIG_UTIMENSAT  */
492 
493 #ifdef CONFIG_INOTIFY
494 #include <sys/inotify.h>
495 
496 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
497 static int sys_inotify_init(void)
498 {
499   return (inotify_init());
500 }
501 #endif
502 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
503 static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
504 {
505   return (inotify_add_watch(fd, pathname, mask));
506 }
507 #endif
508 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
509 static int sys_inotify_rm_watch(int fd, int32_t wd)
510 {
511   return (inotify_rm_watch(fd, wd));
512 }
513 #endif
514 #ifdef CONFIG_INOTIFY1
515 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
516 static int sys_inotify_init1(int flags)
517 {
518   return (inotify_init1(flags));
519 }
520 #endif
521 #endif
522 #else
523 /* Userspace can usually survive runtime without inotify */
524 #undef TARGET_NR_inotify_init
525 #undef TARGET_NR_inotify_init1
526 #undef TARGET_NR_inotify_add_watch
527 #undef TARGET_NR_inotify_rm_watch
528 #endif /* CONFIG_INOTIFY  */
529 
530 
531 extern int personality(int);
532 extern int flock(int, int);
533 extern int setfsuid(int);
534 extern int setfsgid(int);
535 extern int setgroups(int, gid_t *);
536 
537 #define ERRNO_TABLE_SIZE 1200
538 
539 /* target_to_host_errno_table[] is initialized from
540  * host_to_target_errno_table[] in syscall_init(). */
541 static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
542 };
543 
544 /*
545  * This list is the union of errno values overridden in asm-<arch>/errno.h
546  * minus the errnos that are not actually generic to all archs.
547  */
548 static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
549     [EIDRM]		= TARGET_EIDRM,
550     [ECHRNG]		= TARGET_ECHRNG,
551     [EL2NSYNC]		= TARGET_EL2NSYNC,
552     [EL3HLT]		= TARGET_EL3HLT,
553     [EL3RST]		= TARGET_EL3RST,
554     [ELNRNG]		= TARGET_ELNRNG,
555     [EUNATCH]		= TARGET_EUNATCH,
556     [ENOCSI]		= TARGET_ENOCSI,
557     [EL2HLT]		= TARGET_EL2HLT,
558     [EDEADLK]		= TARGET_EDEADLK,
559     [ENOLCK]		= TARGET_ENOLCK,
560     [EBADE]		= TARGET_EBADE,
561     [EBADR]		= TARGET_EBADR,
562     [EXFULL]		= TARGET_EXFULL,
563     [ENOANO]		= TARGET_ENOANO,
564     [EBADRQC]		= TARGET_EBADRQC,
565     [EBADSLT]		= TARGET_EBADSLT,
566     [EBFONT]		= TARGET_EBFONT,
567     [ENOSTR]		= TARGET_ENOSTR,
568     [ENODATA]		= TARGET_ENODATA,
569     [ETIME]		= TARGET_ETIME,
570     [ENOSR]		= TARGET_ENOSR,
571     [ENONET]		= TARGET_ENONET,
572     [ENOPKG]		= TARGET_ENOPKG,
573     [EREMOTE]		= TARGET_EREMOTE,
574     [ENOLINK]		= TARGET_ENOLINK,
575     [EADV]		= TARGET_EADV,
576     [ESRMNT]		= TARGET_ESRMNT,
577     [ECOMM]		= TARGET_ECOMM,
578     [EPROTO]		= TARGET_EPROTO,
579     [EDOTDOT]		= TARGET_EDOTDOT,
580     [EMULTIHOP]		= TARGET_EMULTIHOP,
581     [EBADMSG]		= TARGET_EBADMSG,
582     [ENAMETOOLONG]	= TARGET_ENAMETOOLONG,
583     [EOVERFLOW]		= TARGET_EOVERFLOW,
584     [ENOTUNIQ]		= TARGET_ENOTUNIQ,
585     [EBADFD]		= TARGET_EBADFD,
586     [EREMCHG]		= TARGET_EREMCHG,
587     [ELIBACC]		= TARGET_ELIBACC,
588     [ELIBBAD]		= TARGET_ELIBBAD,
589     [ELIBSCN]		= TARGET_ELIBSCN,
590     [ELIBMAX]		= TARGET_ELIBMAX,
591     [ELIBEXEC]		= TARGET_ELIBEXEC,
592     [EILSEQ]		= TARGET_EILSEQ,
593     [ENOSYS]		= TARGET_ENOSYS,
594     [ELOOP]		= TARGET_ELOOP,
595     [ERESTART]		= TARGET_ERESTART,
596     [ESTRPIPE]		= TARGET_ESTRPIPE,
597     [ENOTEMPTY]		= TARGET_ENOTEMPTY,
598     [EUSERS]		= TARGET_EUSERS,
599     [ENOTSOCK]		= TARGET_ENOTSOCK,
600     [EDESTADDRREQ]	= TARGET_EDESTADDRREQ,
601     [EMSGSIZE]		= TARGET_EMSGSIZE,
602     [EPROTOTYPE]	= TARGET_EPROTOTYPE,
603     [ENOPROTOOPT]	= TARGET_ENOPROTOOPT,
604     [EPROTONOSUPPORT]	= TARGET_EPROTONOSUPPORT,
605     [ESOCKTNOSUPPORT]	= TARGET_ESOCKTNOSUPPORT,
606     [EOPNOTSUPP]	= TARGET_EOPNOTSUPP,
607     [EPFNOSUPPORT]	= TARGET_EPFNOSUPPORT,
608     [EAFNOSUPPORT]	= TARGET_EAFNOSUPPORT,
609     [EADDRINUSE]	= TARGET_EADDRINUSE,
610     [EADDRNOTAVAIL]	= TARGET_EADDRNOTAVAIL,
611     [ENETDOWN]		= TARGET_ENETDOWN,
612     [ENETUNREACH]	= TARGET_ENETUNREACH,
613     [ENETRESET]		= TARGET_ENETRESET,
614     [ECONNABORTED]	= TARGET_ECONNABORTED,
615     [ECONNRESET]	= TARGET_ECONNRESET,
616     [ENOBUFS]		= TARGET_ENOBUFS,
617     [EISCONN]		= TARGET_EISCONN,
618     [ENOTCONN]		= TARGET_ENOTCONN,
619     [EUCLEAN]		= TARGET_EUCLEAN,
620     [ENOTNAM]		= TARGET_ENOTNAM,
621     [ENAVAIL]		= TARGET_ENAVAIL,
622     [EISNAM]		= TARGET_EISNAM,
623     [EREMOTEIO]		= TARGET_EREMOTEIO,
624     [ESHUTDOWN]		= TARGET_ESHUTDOWN,
625     [ETOOMANYREFS]	= TARGET_ETOOMANYREFS,
626     [ETIMEDOUT]		= TARGET_ETIMEDOUT,
627     [ECONNREFUSED]	= TARGET_ECONNREFUSED,
628     [EHOSTDOWN]		= TARGET_EHOSTDOWN,
629     [EHOSTUNREACH]	= TARGET_EHOSTUNREACH,
630     [EALREADY]		= TARGET_EALREADY,
631     [EINPROGRESS]	= TARGET_EINPROGRESS,
632     [ESTALE]		= TARGET_ESTALE,
633     [ECANCELED]		= TARGET_ECANCELED,
634     [ENOMEDIUM]		= TARGET_ENOMEDIUM,
635     [EMEDIUMTYPE]	= TARGET_EMEDIUMTYPE,
636 #ifdef ENOKEY
637     [ENOKEY]		= TARGET_ENOKEY,
638 #endif
639 #ifdef EKEYEXPIRED
640     [EKEYEXPIRED]	= TARGET_EKEYEXPIRED,
641 #endif
642 #ifdef EKEYREVOKED
643     [EKEYREVOKED]	= TARGET_EKEYREVOKED,
644 #endif
645 #ifdef EKEYREJECTED
646     [EKEYREJECTED]	= TARGET_EKEYREJECTED,
647 #endif
648 #ifdef EOWNERDEAD
649     [EOWNERDEAD]	= TARGET_EOWNERDEAD,
650 #endif
651 #ifdef ENOTRECOVERABLE
652     [ENOTRECOVERABLE]	= TARGET_ENOTRECOVERABLE,
653 #endif
654 };
655 
656 static inline int host_to_target_errno(int err)
657 {
658     if(host_to_target_errno_table[err])
659         return host_to_target_errno_table[err];
660     return err;
661 }
662 
663 static inline int target_to_host_errno(int err)
664 {
665     if (target_to_host_errno_table[err])
666         return target_to_host_errno_table[err];
667     return err;
668 }
669 
670 static inline abi_long get_errno(abi_long ret)
671 {
672     if (ret == -1)
673         return -host_to_target_errno(errno);
674     else
675         return ret;
676 }
677 
678 static inline int is_error(abi_long ret)
679 {
680     return (abi_ulong)ret >= (abi_ulong)(-4096);
681 }
682 
683 char *target_strerror(int err)
684 {
685     return strerror(target_to_host_errno(err));
686 }
687 
688 static abi_ulong target_brk;
689 static abi_ulong target_original_brk;
690 
691 void target_set_brk(abi_ulong new_brk)
692 {
693     target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
694 }
695 
696 /* do_brk() must return target values and target errnos. */
697 abi_long do_brk(abi_ulong new_brk)
698 {
699     abi_ulong brk_page;
700     abi_long mapped_addr;
701     int	new_alloc_size;
702 
703     if (!new_brk)
704         return target_brk;
705     if (new_brk < target_original_brk)
706         return target_brk;
707 
708     brk_page = HOST_PAGE_ALIGN(target_brk);
709 
710     /* If the new brk is less than this, set it and we're done... */
711     if (new_brk < brk_page) {
712 	target_brk = new_brk;
713     	return target_brk;
714     }
715 
716     /* We need to allocate more memory after the brk... */
717     new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
718     mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
719                                         PROT_READ|PROT_WRITE,
720                                         MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
721 
722 #if defined(TARGET_ALPHA)
723     /* We (partially) emulate OSF/1 on Alpha, which requires we
724        return a proper errno, not an unchanged brk value.  */
725     if (is_error(mapped_addr)) {
726         return -TARGET_ENOMEM;
727     }
728 #endif
729 
730     if (!is_error(mapped_addr)) {
731 	target_brk = new_brk;
732     }
733     return target_brk;
734 }
735 
736 static inline abi_long copy_from_user_fdset(fd_set *fds,
737                                             abi_ulong target_fds_addr,
738                                             int n)
739 {
740     int i, nw, j, k;
741     abi_ulong b, *target_fds;
742 
743     nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
744     if (!(target_fds = lock_user(VERIFY_READ,
745                                  target_fds_addr,
746                                  sizeof(abi_ulong) * nw,
747                                  1)))
748         return -TARGET_EFAULT;
749 
750     FD_ZERO(fds);
751     k = 0;
752     for (i = 0; i < nw; i++) {
753         /* grab the abi_ulong */
754         __get_user(b, &target_fds[i]);
755         for (j = 0; j < TARGET_ABI_BITS; j++) {
756             /* check the bit inside the abi_ulong */
757             if ((b >> j) & 1)
758                 FD_SET(k, fds);
759             k++;
760         }
761     }
762 
763     unlock_user(target_fds, target_fds_addr, 0);
764 
765     return 0;
766 }
767 
768 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
769                                           const fd_set *fds,
770                                           int n)
771 {
772     int i, nw, j, k;
773     abi_long v;
774     abi_ulong *target_fds;
775 
776     nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
777     if (!(target_fds = lock_user(VERIFY_WRITE,
778                                  target_fds_addr,
779                                  sizeof(abi_ulong) * nw,
780                                  0)))
781         return -TARGET_EFAULT;
782 
783     k = 0;
784     for (i = 0; i < nw; i++) {
785         v = 0;
786         for (j = 0; j < TARGET_ABI_BITS; j++) {
787             v |= ((FD_ISSET(k, fds) != 0) << j);
788             k++;
789         }
790         __put_user(v, &target_fds[i]);
791     }
792 
793     unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
794 
795     return 0;
796 }
797 
798 #if defined(__alpha__)
799 #define HOST_HZ 1024
800 #else
801 #define HOST_HZ 100
802 #endif
803 
804 static inline abi_long host_to_target_clock_t(long ticks)
805 {
806 #if HOST_HZ == TARGET_HZ
807     return ticks;
808 #else
809     return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
810 #endif
811 }
812 
813 static inline abi_long host_to_target_rusage(abi_ulong target_addr,
814                                              const struct rusage *rusage)
815 {
816     struct target_rusage *target_rusage;
817 
818     if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
819         return -TARGET_EFAULT;
820     target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
821     target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
822     target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
823     target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
824     target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
825     target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
826     target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
827     target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
828     target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
829     target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
830     target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
831     target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
832     target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
833     target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
834     target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
835     target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
836     target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
837     target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
838     unlock_user_struct(target_rusage, target_addr, 1);
839 
840     return 0;
841 }
842 
843 static inline rlim_t target_to_host_rlim(target_ulong target_rlim)
844 {
845     if (target_rlim == TARGET_RLIM_INFINITY)
846         return RLIM_INFINITY;
847     else
848         return tswapl(target_rlim);
849 }
850 
851 static inline target_ulong host_to_target_rlim(rlim_t rlim)
852 {
853     if (rlim == RLIM_INFINITY || rlim != (target_long)rlim)
854         return TARGET_RLIM_INFINITY;
855     else
856         return tswapl(rlim);
857 }
858 
859 static inline abi_long copy_from_user_timeval(struct timeval *tv,
860                                               abi_ulong target_tv_addr)
861 {
862     struct target_timeval *target_tv;
863 
864     if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
865         return -TARGET_EFAULT;
866 
867     __get_user(tv->tv_sec, &target_tv->tv_sec);
868     __get_user(tv->tv_usec, &target_tv->tv_usec);
869 
870     unlock_user_struct(target_tv, target_tv_addr, 0);
871 
872     return 0;
873 }
874 
875 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
876                                             const struct timeval *tv)
877 {
878     struct target_timeval *target_tv;
879 
880     if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
881         return -TARGET_EFAULT;
882 
883     __put_user(tv->tv_sec, &target_tv->tv_sec);
884     __put_user(tv->tv_usec, &target_tv->tv_usec);
885 
886     unlock_user_struct(target_tv, target_tv_addr, 1);
887 
888     return 0;
889 }
890 
891 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
892 #include <mqueue.h>
893 
894 static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
895                                               abi_ulong target_mq_attr_addr)
896 {
897     struct target_mq_attr *target_mq_attr;
898 
899     if (!lock_user_struct(VERIFY_READ, target_mq_attr,
900                           target_mq_attr_addr, 1))
901         return -TARGET_EFAULT;
902 
903     __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
904     __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
905     __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
906     __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
907 
908     unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
909 
910     return 0;
911 }
912 
913 static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
914                                             const struct mq_attr *attr)
915 {
916     struct target_mq_attr *target_mq_attr;
917 
918     if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
919                           target_mq_attr_addr, 0))
920         return -TARGET_EFAULT;
921 
922     __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
923     __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
924     __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
925     __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
926 
927     unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
928 
929     return 0;
930 }
931 #endif
932 
933 /* do_select() must return target values and target errnos. */
934 static abi_long do_select(int n,
935                           abi_ulong rfd_addr, abi_ulong wfd_addr,
936                           abi_ulong efd_addr, abi_ulong target_tv_addr)
937 {
938     fd_set rfds, wfds, efds;
939     fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
940     struct timeval tv, *tv_ptr;
941     abi_long ret;
942 
943     if (rfd_addr) {
944         if (copy_from_user_fdset(&rfds, rfd_addr, n))
945             return -TARGET_EFAULT;
946         rfds_ptr = &rfds;
947     } else {
948         rfds_ptr = NULL;
949     }
950     if (wfd_addr) {
951         if (copy_from_user_fdset(&wfds, wfd_addr, n))
952             return -TARGET_EFAULT;
953         wfds_ptr = &wfds;
954     } else {
955         wfds_ptr = NULL;
956     }
957     if (efd_addr) {
958         if (copy_from_user_fdset(&efds, efd_addr, n))
959             return -TARGET_EFAULT;
960         efds_ptr = &efds;
961     } else {
962         efds_ptr = NULL;
963     }
964 
965     if (target_tv_addr) {
966         if (copy_from_user_timeval(&tv, target_tv_addr))
967             return -TARGET_EFAULT;
968         tv_ptr = &tv;
969     } else {
970         tv_ptr = NULL;
971     }
972 
973     ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
974 
975     if (!is_error(ret)) {
976         if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
977             return -TARGET_EFAULT;
978         if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
979             return -TARGET_EFAULT;
980         if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
981             return -TARGET_EFAULT;
982 
983         if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
984             return -TARGET_EFAULT;
985     }
986 
987     return ret;
988 }
989 
990 static abi_long do_pipe2(int host_pipe[], int flags)
991 {
992 #ifdef CONFIG_PIPE2
993     return pipe2(host_pipe, flags);
994 #else
995     return -ENOSYS;
996 #endif
997 }
998 
999 static abi_long do_pipe(void *cpu_env, abi_ulong pipedes,
1000                         int flags, int is_pipe2)
1001 {
1002     int host_pipe[2];
1003     abi_long ret;
1004     ret = flags ? do_pipe2(host_pipe, flags) : pipe(host_pipe);
1005 
1006     if (is_error(ret))
1007         return get_errno(ret);
1008 
1009     /* Several targets have special calling conventions for the original
1010        pipe syscall, but didn't replicate this into the pipe2 syscall.  */
1011     if (!is_pipe2) {
1012 #if defined(TARGET_ALPHA)
1013         ((CPUAlphaState *)cpu_env)->ir[IR_A4] = host_pipe[1];
1014         return host_pipe[0];
1015 #elif defined(TARGET_MIPS)
1016         ((CPUMIPSState*)cpu_env)->active_tc.gpr[3] = host_pipe[1];
1017         return host_pipe[0];
1018 #elif defined(TARGET_SH4)
1019         ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
1020         return host_pipe[0];
1021 #endif
1022     }
1023 
1024     if (put_user_s32(host_pipe[0], pipedes)
1025         || put_user_s32(host_pipe[1], pipedes + sizeof(host_pipe[0])))
1026         return -TARGET_EFAULT;
1027     return get_errno(ret);
1028 }
1029 
1030 static inline abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn,
1031                                               abi_ulong target_addr,
1032                                               socklen_t len)
1033 {
1034     struct target_ip_mreqn *target_smreqn;
1035 
1036     target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
1037     if (!target_smreqn)
1038         return -TARGET_EFAULT;
1039     mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
1040     mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
1041     if (len == sizeof(struct target_ip_mreqn))
1042         mreqn->imr_ifindex = tswapl(target_smreqn->imr_ifindex);
1043     unlock_user(target_smreqn, target_addr, 0);
1044 
1045     return 0;
1046 }
1047 
1048 static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
1049                                                abi_ulong target_addr,
1050                                                socklen_t len)
1051 {
1052     const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
1053     sa_family_t sa_family;
1054     struct target_sockaddr *target_saddr;
1055 
1056     target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
1057     if (!target_saddr)
1058         return -TARGET_EFAULT;
1059 
1060     sa_family = tswap16(target_saddr->sa_family);
1061 
1062     /* Oops. The caller might send a incomplete sun_path; sun_path
1063      * must be terminated by \0 (see the manual page), but
1064      * unfortunately it is quite common to specify sockaddr_un
1065      * length as "strlen(x->sun_path)" while it should be
1066      * "strlen(...) + 1". We'll fix that here if needed.
1067      * Linux kernel has a similar feature.
1068      */
1069 
1070     if (sa_family == AF_UNIX) {
1071         if (len < unix_maxlen && len > 0) {
1072             char *cp = (char*)target_saddr;
1073 
1074             if ( cp[len-1] && !cp[len] )
1075                 len++;
1076         }
1077         if (len > unix_maxlen)
1078             len = unix_maxlen;
1079     }
1080 
1081     memcpy(addr, target_saddr, len);
1082     addr->sa_family = sa_family;
1083     unlock_user(target_saddr, target_addr, 0);
1084 
1085     return 0;
1086 }
1087 
1088 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
1089                                                struct sockaddr *addr,
1090                                                socklen_t len)
1091 {
1092     struct target_sockaddr *target_saddr;
1093 
1094     target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
1095     if (!target_saddr)
1096         return -TARGET_EFAULT;
1097     memcpy(target_saddr, addr, len);
1098     target_saddr->sa_family = tswap16(addr->sa_family);
1099     unlock_user(target_saddr, target_addr, len);
1100 
1101     return 0;
1102 }
1103 
1104 /* ??? Should this also swap msgh->name?  */
1105 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1106                                            struct target_msghdr *target_msgh)
1107 {
1108     struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1109     abi_long msg_controllen;
1110     abi_ulong target_cmsg_addr;
1111     struct target_cmsghdr *target_cmsg;
1112     socklen_t space = 0;
1113 
1114     msg_controllen = tswapl(target_msgh->msg_controllen);
1115     if (msg_controllen < sizeof (struct target_cmsghdr))
1116         goto the_end;
1117     target_cmsg_addr = tswapl(target_msgh->msg_control);
1118     target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1119     if (!target_cmsg)
1120         return -TARGET_EFAULT;
1121 
1122     while (cmsg && target_cmsg) {
1123         void *data = CMSG_DATA(cmsg);
1124         void *target_data = TARGET_CMSG_DATA(target_cmsg);
1125 
1126         int len = tswapl(target_cmsg->cmsg_len)
1127                   - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1128 
1129         space += CMSG_SPACE(len);
1130         if (space > msgh->msg_controllen) {
1131             space -= CMSG_SPACE(len);
1132             gemu_log("Host cmsg overflow\n");
1133             break;
1134         }
1135 
1136         cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1137         cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1138         cmsg->cmsg_len = CMSG_LEN(len);
1139 
1140         if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
1141             gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1142             memcpy(data, target_data, len);
1143         } else {
1144             int *fd = (int *)data;
1145             int *target_fd = (int *)target_data;
1146             int i, numfds = len / sizeof(int);
1147 
1148             for (i = 0; i < numfds; i++)
1149                 fd[i] = tswap32(target_fd[i]);
1150         }
1151 
1152         cmsg = CMSG_NXTHDR(msgh, cmsg);
1153         target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1154     }
1155     unlock_user(target_cmsg, target_cmsg_addr, 0);
1156  the_end:
1157     msgh->msg_controllen = space;
1158     return 0;
1159 }
1160 
1161 /* ??? Should this also swap msgh->name?  */
1162 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1163                                            struct msghdr *msgh)
1164 {
1165     struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
1166     abi_long msg_controllen;
1167     abi_ulong target_cmsg_addr;
1168     struct target_cmsghdr *target_cmsg;
1169     socklen_t space = 0;
1170 
1171     msg_controllen = tswapl(target_msgh->msg_controllen);
1172     if (msg_controllen < sizeof (struct target_cmsghdr))
1173         goto the_end;
1174     target_cmsg_addr = tswapl(target_msgh->msg_control);
1175     target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1176     if (!target_cmsg)
1177         return -TARGET_EFAULT;
1178 
1179     while (cmsg && target_cmsg) {
1180         void *data = CMSG_DATA(cmsg);
1181         void *target_data = TARGET_CMSG_DATA(target_cmsg);
1182 
1183         int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1184 
1185         space += TARGET_CMSG_SPACE(len);
1186         if (space > msg_controllen) {
1187             space -= TARGET_CMSG_SPACE(len);
1188             gemu_log("Target cmsg overflow\n");
1189             break;
1190         }
1191 
1192         target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1193         target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1194         target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
1195 
1196         if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
1197             gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1198             memcpy(target_data, data, len);
1199         } else {
1200             int *fd = (int *)data;
1201             int *target_fd = (int *)target_data;
1202             int i, numfds = len / sizeof(int);
1203 
1204             for (i = 0; i < numfds; i++)
1205                 target_fd[i] = tswap32(fd[i]);
1206         }
1207 
1208         cmsg = CMSG_NXTHDR(msgh, cmsg);
1209         target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1210     }
1211     unlock_user(target_cmsg, target_cmsg_addr, space);
1212  the_end:
1213     target_msgh->msg_controllen = tswapl(space);
1214     return 0;
1215 }
1216 
1217 /* do_setsockopt() Must return target values and target errnos. */
1218 static abi_long do_setsockopt(int sockfd, int level, int optname,
1219                               abi_ulong optval_addr, socklen_t optlen)
1220 {
1221     abi_long ret;
1222     int val;
1223     struct ip_mreqn *ip_mreq;
1224     struct ip_mreq_source *ip_mreq_source;
1225 
1226     switch(level) {
1227     case SOL_TCP:
1228         /* TCP options all take an 'int' value.  */
1229         if (optlen < sizeof(uint32_t))
1230             return -TARGET_EINVAL;
1231 
1232         if (get_user_u32(val, optval_addr))
1233             return -TARGET_EFAULT;
1234         ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1235         break;
1236     case SOL_IP:
1237         switch(optname) {
1238         case IP_TOS:
1239         case IP_TTL:
1240         case IP_HDRINCL:
1241         case IP_ROUTER_ALERT:
1242         case IP_RECVOPTS:
1243         case IP_RETOPTS:
1244         case IP_PKTINFO:
1245         case IP_MTU_DISCOVER:
1246         case IP_RECVERR:
1247         case IP_RECVTOS:
1248 #ifdef IP_FREEBIND
1249         case IP_FREEBIND:
1250 #endif
1251         case IP_MULTICAST_TTL:
1252         case IP_MULTICAST_LOOP:
1253             val = 0;
1254             if (optlen >= sizeof(uint32_t)) {
1255                 if (get_user_u32(val, optval_addr))
1256                     return -TARGET_EFAULT;
1257             } else if (optlen >= 1) {
1258                 if (get_user_u8(val, optval_addr))
1259                     return -TARGET_EFAULT;
1260             }
1261             ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1262             break;
1263         case IP_ADD_MEMBERSHIP:
1264         case IP_DROP_MEMBERSHIP:
1265             if (optlen < sizeof (struct target_ip_mreq) ||
1266                 optlen > sizeof (struct target_ip_mreqn))
1267                 return -TARGET_EINVAL;
1268 
1269             ip_mreq = (struct ip_mreqn *) alloca(optlen);
1270             target_to_host_ip_mreq(ip_mreq, optval_addr, optlen);
1271             ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq, optlen));
1272             break;
1273 
1274         case IP_BLOCK_SOURCE:
1275         case IP_UNBLOCK_SOURCE:
1276         case IP_ADD_SOURCE_MEMBERSHIP:
1277         case IP_DROP_SOURCE_MEMBERSHIP:
1278             if (optlen != sizeof (struct target_ip_mreq_source))
1279                 return -TARGET_EINVAL;
1280 
1281             ip_mreq_source = lock_user(VERIFY_READ, optval_addr, optlen, 1);
1282             ret = get_errno(setsockopt(sockfd, level, optname, ip_mreq_source, optlen));
1283             unlock_user (ip_mreq_source, optval_addr, 0);
1284             break;
1285 
1286         default:
1287             goto unimplemented;
1288         }
1289         break;
1290     case TARGET_SOL_SOCKET:
1291         switch (optname) {
1292             /* Options with 'int' argument.  */
1293         case TARGET_SO_DEBUG:
1294 		optname = SO_DEBUG;
1295 		break;
1296         case TARGET_SO_REUSEADDR:
1297 		optname = SO_REUSEADDR;
1298 		break;
1299         case TARGET_SO_TYPE:
1300 		optname = SO_TYPE;
1301 		break;
1302         case TARGET_SO_ERROR:
1303 		optname = SO_ERROR;
1304 		break;
1305         case TARGET_SO_DONTROUTE:
1306 		optname = SO_DONTROUTE;
1307 		break;
1308         case TARGET_SO_BROADCAST:
1309 		optname = SO_BROADCAST;
1310 		break;
1311         case TARGET_SO_SNDBUF:
1312 		optname = SO_SNDBUF;
1313 		break;
1314         case TARGET_SO_RCVBUF:
1315 		optname = SO_RCVBUF;
1316 		break;
1317         case TARGET_SO_KEEPALIVE:
1318 		optname = SO_KEEPALIVE;
1319 		break;
1320         case TARGET_SO_OOBINLINE:
1321 		optname = SO_OOBINLINE;
1322 		break;
1323         case TARGET_SO_NO_CHECK:
1324 		optname = SO_NO_CHECK;
1325 		break;
1326         case TARGET_SO_PRIORITY:
1327 		optname = SO_PRIORITY;
1328 		break;
1329 #ifdef SO_BSDCOMPAT
1330         case TARGET_SO_BSDCOMPAT:
1331 		optname = SO_BSDCOMPAT;
1332 		break;
1333 #endif
1334         case TARGET_SO_PASSCRED:
1335 		optname = SO_PASSCRED;
1336 		break;
1337         case TARGET_SO_TIMESTAMP:
1338 		optname = SO_TIMESTAMP;
1339 		break;
1340         case TARGET_SO_RCVLOWAT:
1341 		optname = SO_RCVLOWAT;
1342 		break;
1343         case TARGET_SO_RCVTIMEO:
1344 		optname = SO_RCVTIMEO;
1345 		break;
1346         case TARGET_SO_SNDTIMEO:
1347 		optname = SO_SNDTIMEO;
1348 		break;
1349             break;
1350         default:
1351             goto unimplemented;
1352         }
1353 	if (optlen < sizeof(uint32_t))
1354             return -TARGET_EINVAL;
1355 
1356 	if (get_user_u32(val, optval_addr))
1357             return -TARGET_EFAULT;
1358 	ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
1359         break;
1360     default:
1361     unimplemented:
1362         gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
1363         ret = -TARGET_ENOPROTOOPT;
1364     }
1365     return ret;
1366 }
1367 
1368 /* do_getsockopt() Must return target values and target errnos. */
1369 static abi_long do_getsockopt(int sockfd, int level, int optname,
1370                               abi_ulong optval_addr, abi_ulong optlen)
1371 {
1372     abi_long ret;
1373     int len, val;
1374     socklen_t lv;
1375 
1376     switch(level) {
1377     case TARGET_SOL_SOCKET:
1378         level = SOL_SOCKET;
1379         switch (optname) {
1380         /* These don't just return a single integer */
1381         case TARGET_SO_LINGER:
1382         case TARGET_SO_RCVTIMEO:
1383         case TARGET_SO_SNDTIMEO:
1384         case TARGET_SO_PEERCRED:
1385         case TARGET_SO_PEERNAME:
1386             goto unimplemented;
1387         /* Options with 'int' argument.  */
1388         case TARGET_SO_DEBUG:
1389             optname = SO_DEBUG;
1390             goto int_case;
1391         case TARGET_SO_REUSEADDR:
1392             optname = SO_REUSEADDR;
1393             goto int_case;
1394         case TARGET_SO_TYPE:
1395             optname = SO_TYPE;
1396             goto int_case;
1397         case TARGET_SO_ERROR:
1398             optname = SO_ERROR;
1399             goto int_case;
1400         case TARGET_SO_DONTROUTE:
1401             optname = SO_DONTROUTE;
1402             goto int_case;
1403         case TARGET_SO_BROADCAST:
1404             optname = SO_BROADCAST;
1405             goto int_case;
1406         case TARGET_SO_SNDBUF:
1407             optname = SO_SNDBUF;
1408             goto int_case;
1409         case TARGET_SO_RCVBUF:
1410             optname = SO_RCVBUF;
1411             goto int_case;
1412         case TARGET_SO_KEEPALIVE:
1413             optname = SO_KEEPALIVE;
1414             goto int_case;
1415         case TARGET_SO_OOBINLINE:
1416             optname = SO_OOBINLINE;
1417             goto int_case;
1418         case TARGET_SO_NO_CHECK:
1419             optname = SO_NO_CHECK;
1420             goto int_case;
1421         case TARGET_SO_PRIORITY:
1422             optname = SO_PRIORITY;
1423             goto int_case;
1424 #ifdef SO_BSDCOMPAT
1425         case TARGET_SO_BSDCOMPAT:
1426             optname = SO_BSDCOMPAT;
1427             goto int_case;
1428 #endif
1429         case TARGET_SO_PASSCRED:
1430             optname = SO_PASSCRED;
1431             goto int_case;
1432         case TARGET_SO_TIMESTAMP:
1433             optname = SO_TIMESTAMP;
1434             goto int_case;
1435         case TARGET_SO_RCVLOWAT:
1436             optname = SO_RCVLOWAT;
1437             goto int_case;
1438         default:
1439             goto int_case;
1440         }
1441         break;
1442     case SOL_TCP:
1443         /* TCP options all take an 'int' value.  */
1444     int_case:
1445         if (get_user_u32(len, optlen))
1446             return -TARGET_EFAULT;
1447         if (len < 0)
1448             return -TARGET_EINVAL;
1449         lv = sizeof(int);
1450         ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1451         if (ret < 0)
1452             return ret;
1453         if (len > lv)
1454             len = lv;
1455         if (len == 4) {
1456             if (put_user_u32(val, optval_addr))
1457                 return -TARGET_EFAULT;
1458         } else {
1459             if (put_user_u8(val, optval_addr))
1460                 return -TARGET_EFAULT;
1461         }
1462         if (put_user_u32(len, optlen))
1463             return -TARGET_EFAULT;
1464         break;
1465     case SOL_IP:
1466         switch(optname) {
1467         case IP_TOS:
1468         case IP_TTL:
1469         case IP_HDRINCL:
1470         case IP_ROUTER_ALERT:
1471         case IP_RECVOPTS:
1472         case IP_RETOPTS:
1473         case IP_PKTINFO:
1474         case IP_MTU_DISCOVER:
1475         case IP_RECVERR:
1476         case IP_RECVTOS:
1477 #ifdef IP_FREEBIND
1478         case IP_FREEBIND:
1479 #endif
1480         case IP_MULTICAST_TTL:
1481         case IP_MULTICAST_LOOP:
1482             if (get_user_u32(len, optlen))
1483                 return -TARGET_EFAULT;
1484             if (len < 0)
1485                 return -TARGET_EINVAL;
1486             lv = sizeof(int);
1487             ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1488             if (ret < 0)
1489                 return ret;
1490             if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
1491                 len = 1;
1492                 if (put_user_u32(len, optlen)
1493                     || put_user_u8(val, optval_addr))
1494                     return -TARGET_EFAULT;
1495             } else {
1496                 if (len > sizeof(int))
1497                     len = sizeof(int);
1498                 if (put_user_u32(len, optlen)
1499                     || put_user_u32(val, optval_addr))
1500                     return -TARGET_EFAULT;
1501             }
1502             break;
1503         default:
1504             ret = -TARGET_ENOPROTOOPT;
1505             break;
1506         }
1507         break;
1508     default:
1509     unimplemented:
1510         gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1511                  level, optname);
1512         ret = -TARGET_EOPNOTSUPP;
1513         break;
1514     }
1515     return ret;
1516 }
1517 
1518 /* FIXME
1519  * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1520  * other lock functions have a return code of 0 for failure.
1521  */
1522 static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1523                            int count, int copy)
1524 {
1525     struct target_iovec *target_vec;
1526     abi_ulong base;
1527     int i;
1528 
1529     target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1530     if (!target_vec)
1531         return -TARGET_EFAULT;
1532     for(i = 0;i < count; i++) {
1533         base = tswapl(target_vec[i].iov_base);
1534         vec[i].iov_len = tswapl(target_vec[i].iov_len);
1535         if (vec[i].iov_len != 0) {
1536             vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
1537             /* Don't check lock_user return value. We must call writev even
1538                if a element has invalid base address. */
1539         } else {
1540             /* zero length pointer is ignored */
1541             vec[i].iov_base = NULL;
1542         }
1543     }
1544     unlock_user (target_vec, target_addr, 0);
1545     return 0;
1546 }
1547 
1548 static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1549                              int count, int copy)
1550 {
1551     struct target_iovec *target_vec;
1552     abi_ulong base;
1553     int i;
1554 
1555     target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1556     if (!target_vec)
1557         return -TARGET_EFAULT;
1558     for(i = 0;i < count; i++) {
1559         if (target_vec[i].iov_base) {
1560             base = tswapl(target_vec[i].iov_base);
1561             unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1562         }
1563     }
1564     unlock_user (target_vec, target_addr, 0);
1565 
1566     return 0;
1567 }
1568 
1569 /* do_socket() Must return target values and target errnos. */
1570 static abi_long do_socket(int domain, int type, int protocol)
1571 {
1572 #if defined(TARGET_MIPS)
1573     switch(type) {
1574     case TARGET_SOCK_DGRAM:
1575         type = SOCK_DGRAM;
1576         break;
1577     case TARGET_SOCK_STREAM:
1578         type = SOCK_STREAM;
1579         break;
1580     case TARGET_SOCK_RAW:
1581         type = SOCK_RAW;
1582         break;
1583     case TARGET_SOCK_RDM:
1584         type = SOCK_RDM;
1585         break;
1586     case TARGET_SOCK_SEQPACKET:
1587         type = SOCK_SEQPACKET;
1588         break;
1589     case TARGET_SOCK_PACKET:
1590         type = SOCK_PACKET;
1591         break;
1592     }
1593 #endif
1594     if (domain == PF_NETLINK)
1595         return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
1596     return get_errno(socket(domain, type, protocol));
1597 }
1598 
1599 /* do_bind() Must return target values and target errnos. */
1600 static abi_long do_bind(int sockfd, abi_ulong target_addr,
1601                         socklen_t addrlen)
1602 {
1603     void *addr;
1604     abi_long ret;
1605 
1606     if ((int)addrlen < 0) {
1607         return -TARGET_EINVAL;
1608     }
1609 
1610     addr = alloca(addrlen+1);
1611 
1612     ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1613     if (ret)
1614         return ret;
1615 
1616     return get_errno(bind(sockfd, addr, addrlen));
1617 }
1618 
1619 /* do_connect() Must return target values and target errnos. */
1620 static abi_long do_connect(int sockfd, abi_ulong target_addr,
1621                            socklen_t addrlen)
1622 {
1623     void *addr;
1624     abi_long ret;
1625 
1626     if ((int)addrlen < 0) {
1627         return -TARGET_EINVAL;
1628     }
1629 
1630     addr = alloca(addrlen);
1631 
1632     ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1633     if (ret)
1634         return ret;
1635 
1636     return get_errno(connect(sockfd, addr, addrlen));
1637 }
1638 
1639 /* do_sendrecvmsg() Must return target values and target errnos. */
1640 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1641                                int flags, int send)
1642 {
1643     abi_long ret, len;
1644     struct target_msghdr *msgp;
1645     struct msghdr msg;
1646     int count;
1647     struct iovec *vec;
1648     abi_ulong target_vec;
1649 
1650     /* FIXME */
1651     if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1652                           msgp,
1653                           target_msg,
1654                           send ? 1 : 0))
1655         return -TARGET_EFAULT;
1656     if (msgp->msg_name) {
1657         msg.msg_namelen = tswap32(msgp->msg_namelen);
1658         msg.msg_name = alloca(msg.msg_namelen);
1659         ret = target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1660                                 msg.msg_namelen);
1661         if (ret) {
1662             unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1663             return ret;
1664         }
1665     } else {
1666         msg.msg_name = NULL;
1667         msg.msg_namelen = 0;
1668     }
1669     msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1670     msg.msg_control = alloca(msg.msg_controllen);
1671     msg.msg_flags = tswap32(msgp->msg_flags);
1672 
1673     count = tswapl(msgp->msg_iovlen);
1674     vec = alloca(count * sizeof(struct iovec));
1675     target_vec = tswapl(msgp->msg_iov);
1676     lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
1677     msg.msg_iovlen = count;
1678     msg.msg_iov = vec;
1679 
1680     if (send) {
1681         ret = target_to_host_cmsg(&msg, msgp);
1682         if (ret == 0)
1683             ret = get_errno(sendmsg(fd, &msg, flags));
1684     } else {
1685         ret = get_errno(recvmsg(fd, &msg, flags));
1686         if (!is_error(ret)) {
1687             len = ret;
1688             ret = host_to_target_cmsg(msgp, &msg);
1689             if (!is_error(ret))
1690                 ret = len;
1691         }
1692     }
1693     unlock_iovec(vec, target_vec, count, !send);
1694     unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1695     return ret;
1696 }
1697 
1698 /* do_accept() Must return target values and target errnos. */
1699 static abi_long do_accept(int fd, abi_ulong target_addr,
1700                           abi_ulong target_addrlen_addr)
1701 {
1702     socklen_t addrlen;
1703     void *addr;
1704     abi_long ret;
1705 
1706     if (target_addr == 0)
1707        return get_errno(accept(fd, NULL, NULL));
1708 
1709     /* linux returns EINVAL if addrlen pointer is invalid */
1710     if (get_user_u32(addrlen, target_addrlen_addr))
1711         return -TARGET_EINVAL;
1712 
1713     if ((int)addrlen < 0) {
1714         return -TARGET_EINVAL;
1715     }
1716 
1717     if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1718         return -TARGET_EINVAL;
1719 
1720     addr = alloca(addrlen);
1721 
1722     ret = get_errno(accept(fd, addr, &addrlen));
1723     if (!is_error(ret)) {
1724         host_to_target_sockaddr(target_addr, addr, addrlen);
1725         if (put_user_u32(addrlen, target_addrlen_addr))
1726             ret = -TARGET_EFAULT;
1727     }
1728     return ret;
1729 }
1730 
1731 /* do_getpeername() Must return target values and target errnos. */
1732 static abi_long do_getpeername(int fd, abi_ulong target_addr,
1733                                abi_ulong target_addrlen_addr)
1734 {
1735     socklen_t addrlen;
1736     void *addr;
1737     abi_long ret;
1738 
1739     if (get_user_u32(addrlen, target_addrlen_addr))
1740         return -TARGET_EFAULT;
1741 
1742     if ((int)addrlen < 0) {
1743         return -TARGET_EINVAL;
1744     }
1745 
1746     if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1747         return -TARGET_EFAULT;
1748 
1749     addr = alloca(addrlen);
1750 
1751     ret = get_errno(getpeername(fd, addr, &addrlen));
1752     if (!is_error(ret)) {
1753         host_to_target_sockaddr(target_addr, addr, addrlen);
1754         if (put_user_u32(addrlen, target_addrlen_addr))
1755             ret = -TARGET_EFAULT;
1756     }
1757     return ret;
1758 }
1759 
1760 /* do_getsockname() Must return target values and target errnos. */
1761 static abi_long do_getsockname(int fd, abi_ulong target_addr,
1762                                abi_ulong target_addrlen_addr)
1763 {
1764     socklen_t addrlen;
1765     void *addr;
1766     abi_long ret;
1767 
1768     if (get_user_u32(addrlen, target_addrlen_addr))
1769         return -TARGET_EFAULT;
1770 
1771     if ((int)addrlen < 0) {
1772         return -TARGET_EINVAL;
1773     }
1774 
1775     if (!access_ok(VERIFY_WRITE, target_addr, addrlen))
1776         return -TARGET_EFAULT;
1777 
1778     addr = alloca(addrlen);
1779 
1780     ret = get_errno(getsockname(fd, addr, &addrlen));
1781     if (!is_error(ret)) {
1782         host_to_target_sockaddr(target_addr, addr, addrlen);
1783         if (put_user_u32(addrlen, target_addrlen_addr))
1784             ret = -TARGET_EFAULT;
1785     }
1786     return ret;
1787 }
1788 
1789 /* do_socketpair() Must return target values and target errnos. */
1790 static abi_long do_socketpair(int domain, int type, int protocol,
1791                               abi_ulong target_tab_addr)
1792 {
1793     int tab[2];
1794     abi_long ret;
1795 
1796     ret = get_errno(socketpair(domain, type, protocol, tab));
1797     if (!is_error(ret)) {
1798         if (put_user_s32(tab[0], target_tab_addr)
1799             || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1800             ret = -TARGET_EFAULT;
1801     }
1802     return ret;
1803 }
1804 
1805 /* do_sendto() Must return target values and target errnos. */
1806 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1807                           abi_ulong target_addr, socklen_t addrlen)
1808 {
1809     void *addr;
1810     void *host_msg;
1811     abi_long ret;
1812 
1813     if ((int)addrlen < 0) {
1814         return -TARGET_EINVAL;
1815     }
1816 
1817     host_msg = lock_user(VERIFY_READ, msg, len, 1);
1818     if (!host_msg)
1819         return -TARGET_EFAULT;
1820     if (target_addr) {
1821         addr = alloca(addrlen);
1822         ret = target_to_host_sockaddr(addr, target_addr, addrlen);
1823         if (ret) {
1824             unlock_user(host_msg, msg, 0);
1825             return ret;
1826         }
1827         ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1828     } else {
1829         ret = get_errno(send(fd, host_msg, len, flags));
1830     }
1831     unlock_user(host_msg, msg, 0);
1832     return ret;
1833 }
1834 
1835 /* do_recvfrom() Must return target values and target errnos. */
1836 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1837                             abi_ulong target_addr,
1838                             abi_ulong target_addrlen)
1839 {
1840     socklen_t addrlen;
1841     void *addr;
1842     void *host_msg;
1843     abi_long ret;
1844 
1845     host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1846     if (!host_msg)
1847         return -TARGET_EFAULT;
1848     if (target_addr) {
1849         if (get_user_u32(addrlen, target_addrlen)) {
1850             ret = -TARGET_EFAULT;
1851             goto fail;
1852         }
1853         if ((int)addrlen < 0) {
1854             ret = -TARGET_EINVAL;
1855             goto fail;
1856         }
1857         addr = alloca(addrlen);
1858         ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1859     } else {
1860         addr = NULL; /* To keep compiler quiet.  */
1861         ret = get_errno(recv(fd, host_msg, len, flags));
1862     }
1863     if (!is_error(ret)) {
1864         if (target_addr) {
1865             host_to_target_sockaddr(target_addr, addr, addrlen);
1866             if (put_user_u32(addrlen, target_addrlen)) {
1867                 ret = -TARGET_EFAULT;
1868                 goto fail;
1869             }
1870         }
1871         unlock_user(host_msg, msg, len);
1872     } else {
1873 fail:
1874         unlock_user(host_msg, msg, 0);
1875     }
1876     return ret;
1877 }
1878 
1879 #ifdef TARGET_NR_socketcall
1880 /* do_socketcall() Must return target values and target errnos. */
1881 static abi_long do_socketcall(int num, abi_ulong vptr)
1882 {
1883     abi_long ret;
1884     const int n = sizeof(abi_ulong);
1885 
1886     switch(num) {
1887     case SOCKOP_socket:
1888 	{
1889             abi_ulong domain, type, protocol;
1890 
1891             if (get_user_ual(domain, vptr)
1892                 || get_user_ual(type, vptr + n)
1893                 || get_user_ual(protocol, vptr + 2 * n))
1894                 return -TARGET_EFAULT;
1895 
1896             ret = do_socket(domain, type, protocol);
1897 	}
1898         break;
1899     case SOCKOP_bind:
1900 	{
1901             abi_ulong sockfd;
1902             abi_ulong target_addr;
1903             socklen_t addrlen;
1904 
1905             if (get_user_ual(sockfd, vptr)
1906                 || get_user_ual(target_addr, vptr + n)
1907                 || get_user_ual(addrlen, vptr + 2 * n))
1908                 return -TARGET_EFAULT;
1909 
1910             ret = do_bind(sockfd, target_addr, addrlen);
1911         }
1912         break;
1913     case SOCKOP_connect:
1914         {
1915             abi_ulong sockfd;
1916             abi_ulong target_addr;
1917             socklen_t addrlen;
1918 
1919             if (get_user_ual(sockfd, vptr)
1920                 || get_user_ual(target_addr, vptr + n)
1921                 || get_user_ual(addrlen, vptr + 2 * n))
1922                 return -TARGET_EFAULT;
1923 
1924             ret = do_connect(sockfd, target_addr, addrlen);
1925         }
1926         break;
1927     case SOCKOP_listen:
1928         {
1929             abi_ulong sockfd, backlog;
1930 
1931             if (get_user_ual(sockfd, vptr)
1932                 || get_user_ual(backlog, vptr + n))
1933                 return -TARGET_EFAULT;
1934 
1935             ret = get_errno(listen(sockfd, backlog));
1936         }
1937         break;
1938     case SOCKOP_accept:
1939         {
1940             abi_ulong sockfd;
1941             abi_ulong target_addr, target_addrlen;
1942 
1943             if (get_user_ual(sockfd, vptr)
1944                 || get_user_ual(target_addr, vptr + n)
1945                 || get_user_ual(target_addrlen, vptr + 2 * n))
1946                 return -TARGET_EFAULT;
1947 
1948             ret = do_accept(sockfd, target_addr, target_addrlen);
1949         }
1950         break;
1951     case SOCKOP_getsockname:
1952         {
1953             abi_ulong sockfd;
1954             abi_ulong target_addr, target_addrlen;
1955 
1956             if (get_user_ual(sockfd, vptr)
1957                 || get_user_ual(target_addr, vptr + n)
1958                 || get_user_ual(target_addrlen, vptr + 2 * n))
1959                 return -TARGET_EFAULT;
1960 
1961             ret = do_getsockname(sockfd, target_addr, target_addrlen);
1962         }
1963         break;
1964     case SOCKOP_getpeername:
1965         {
1966             abi_ulong sockfd;
1967             abi_ulong target_addr, target_addrlen;
1968 
1969             if (get_user_ual(sockfd, vptr)
1970                 || get_user_ual(target_addr, vptr + n)
1971                 || get_user_ual(target_addrlen, vptr + 2 * n))
1972                 return -TARGET_EFAULT;
1973 
1974             ret = do_getpeername(sockfd, target_addr, target_addrlen);
1975         }
1976         break;
1977     case SOCKOP_socketpair:
1978         {
1979             abi_ulong domain, type, protocol;
1980             abi_ulong tab;
1981 
1982             if (get_user_ual(domain, vptr)
1983                 || get_user_ual(type, vptr + n)
1984                 || get_user_ual(protocol, vptr + 2 * n)
1985                 || get_user_ual(tab, vptr + 3 * n))
1986                 return -TARGET_EFAULT;
1987 
1988             ret = do_socketpair(domain, type, protocol, tab);
1989         }
1990         break;
1991     case SOCKOP_send:
1992         {
1993             abi_ulong sockfd;
1994             abi_ulong msg;
1995             size_t len;
1996             abi_ulong flags;
1997 
1998             if (get_user_ual(sockfd, vptr)
1999                 || get_user_ual(msg, vptr + n)
2000                 || get_user_ual(len, vptr + 2 * n)
2001                 || get_user_ual(flags, vptr + 3 * n))
2002                 return -TARGET_EFAULT;
2003 
2004             ret = do_sendto(sockfd, msg, len, flags, 0, 0);
2005         }
2006         break;
2007     case SOCKOP_recv:
2008         {
2009             abi_ulong sockfd;
2010             abi_ulong msg;
2011             size_t len;
2012             abi_ulong flags;
2013 
2014             if (get_user_ual(sockfd, vptr)
2015                 || get_user_ual(msg, vptr + n)
2016                 || get_user_ual(len, vptr + 2 * n)
2017                 || get_user_ual(flags, vptr + 3 * n))
2018                 return -TARGET_EFAULT;
2019 
2020             ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
2021         }
2022         break;
2023     case SOCKOP_sendto:
2024         {
2025             abi_ulong sockfd;
2026             abi_ulong msg;
2027             size_t len;
2028             abi_ulong flags;
2029             abi_ulong addr;
2030             socklen_t addrlen;
2031 
2032             if (get_user_ual(sockfd, vptr)
2033                 || get_user_ual(msg, vptr + n)
2034                 || get_user_ual(len, vptr + 2 * n)
2035                 || get_user_ual(flags, vptr + 3 * n)
2036                 || get_user_ual(addr, vptr + 4 * n)
2037                 || get_user_ual(addrlen, vptr + 5 * n))
2038                 return -TARGET_EFAULT;
2039 
2040             ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
2041         }
2042         break;
2043     case SOCKOP_recvfrom:
2044         {
2045             abi_ulong sockfd;
2046             abi_ulong msg;
2047             size_t len;
2048             abi_ulong flags;
2049             abi_ulong addr;
2050             socklen_t addrlen;
2051 
2052             if (get_user_ual(sockfd, vptr)
2053                 || get_user_ual(msg, vptr + n)
2054                 || get_user_ual(len, vptr + 2 * n)
2055                 || get_user_ual(flags, vptr + 3 * n)
2056                 || get_user_ual(addr, vptr + 4 * n)
2057                 || get_user_ual(addrlen, vptr + 5 * n))
2058                 return -TARGET_EFAULT;
2059 
2060             ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
2061         }
2062         break;
2063     case SOCKOP_shutdown:
2064         {
2065             abi_ulong sockfd, how;
2066 
2067             if (get_user_ual(sockfd, vptr)
2068                 || get_user_ual(how, vptr + n))
2069                 return -TARGET_EFAULT;
2070 
2071             ret = get_errno(shutdown(sockfd, how));
2072         }
2073         break;
2074     case SOCKOP_sendmsg:
2075     case SOCKOP_recvmsg:
2076         {
2077             abi_ulong fd;
2078             abi_ulong target_msg;
2079             abi_ulong flags;
2080 
2081             if (get_user_ual(fd, vptr)
2082                 || get_user_ual(target_msg, vptr + n)
2083                 || get_user_ual(flags, vptr + 2 * n))
2084                 return -TARGET_EFAULT;
2085 
2086             ret = do_sendrecvmsg(fd, target_msg, flags,
2087                                  (num == SOCKOP_sendmsg));
2088         }
2089         break;
2090     case SOCKOP_setsockopt:
2091         {
2092             abi_ulong sockfd;
2093             abi_ulong level;
2094             abi_ulong optname;
2095             abi_ulong optval;
2096             socklen_t optlen;
2097 
2098             if (get_user_ual(sockfd, vptr)
2099                 || get_user_ual(level, vptr + n)
2100                 || get_user_ual(optname, vptr + 2 * n)
2101                 || get_user_ual(optval, vptr + 3 * n)
2102                 || get_user_ual(optlen, vptr + 4 * n))
2103                 return -TARGET_EFAULT;
2104 
2105             ret = do_setsockopt(sockfd, level, optname, optval, optlen);
2106         }
2107         break;
2108     case SOCKOP_getsockopt:
2109         {
2110             abi_ulong sockfd;
2111             abi_ulong level;
2112             abi_ulong optname;
2113             abi_ulong optval;
2114             socklen_t optlen;
2115 
2116             if (get_user_ual(sockfd, vptr)
2117                 || get_user_ual(level, vptr + n)
2118                 || get_user_ual(optname, vptr + 2 * n)
2119                 || get_user_ual(optval, vptr + 3 * n)
2120                 || get_user_ual(optlen, vptr + 4 * n))
2121                 return -TARGET_EFAULT;
2122 
2123             ret = do_getsockopt(sockfd, level, optname, optval, optlen);
2124         }
2125         break;
2126     default:
2127         gemu_log("Unsupported socketcall: %d\n", num);
2128         ret = -TARGET_ENOSYS;
2129         break;
2130     }
2131     return ret;
2132 }
2133 #endif
2134 
2135 #define N_SHM_REGIONS	32
2136 
2137 static struct shm_region {
2138     abi_ulong	start;
2139     abi_ulong	size;
2140 } shm_regions[N_SHM_REGIONS];
2141 
2142 struct target_ipc_perm
2143 {
2144     abi_long __key;
2145     abi_ulong uid;
2146     abi_ulong gid;
2147     abi_ulong cuid;
2148     abi_ulong cgid;
2149     unsigned short int mode;
2150     unsigned short int __pad1;
2151     unsigned short int __seq;
2152     unsigned short int __pad2;
2153     abi_ulong __unused1;
2154     abi_ulong __unused2;
2155 };
2156 
2157 struct target_semid_ds
2158 {
2159   struct target_ipc_perm sem_perm;
2160   abi_ulong sem_otime;
2161   abi_ulong __unused1;
2162   abi_ulong sem_ctime;
2163   abi_ulong __unused2;
2164   abi_ulong sem_nsems;
2165   abi_ulong __unused3;
2166   abi_ulong __unused4;
2167 };
2168 
2169 static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
2170                                                abi_ulong target_addr)
2171 {
2172     struct target_ipc_perm *target_ip;
2173     struct target_semid_ds *target_sd;
2174 
2175     if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2176         return -TARGET_EFAULT;
2177     target_ip = &(target_sd->sem_perm);
2178     host_ip->__key = tswapl(target_ip->__key);
2179     host_ip->uid = tswapl(target_ip->uid);
2180     host_ip->gid = tswapl(target_ip->gid);
2181     host_ip->cuid = tswapl(target_ip->cuid);
2182     host_ip->cgid = tswapl(target_ip->cgid);
2183     host_ip->mode = tswapl(target_ip->mode);
2184     unlock_user_struct(target_sd, target_addr, 0);
2185     return 0;
2186 }
2187 
2188 static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
2189                                                struct ipc_perm *host_ip)
2190 {
2191     struct target_ipc_perm *target_ip;
2192     struct target_semid_ds *target_sd;
2193 
2194     if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2195         return -TARGET_EFAULT;
2196     target_ip = &(target_sd->sem_perm);
2197     target_ip->__key = tswapl(host_ip->__key);
2198     target_ip->uid = tswapl(host_ip->uid);
2199     target_ip->gid = tswapl(host_ip->gid);
2200     target_ip->cuid = tswapl(host_ip->cuid);
2201     target_ip->cgid = tswapl(host_ip->cgid);
2202     target_ip->mode = tswapl(host_ip->mode);
2203     unlock_user_struct(target_sd, target_addr, 1);
2204     return 0;
2205 }
2206 
2207 static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2208                                                abi_ulong target_addr)
2209 {
2210     struct target_semid_ds *target_sd;
2211 
2212     if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2213         return -TARGET_EFAULT;
2214     if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2215         return -TARGET_EFAULT;
2216     host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
2217     host_sd->sem_otime = tswapl(target_sd->sem_otime);
2218     host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
2219     unlock_user_struct(target_sd, target_addr, 0);
2220     return 0;
2221 }
2222 
2223 static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2224                                                struct semid_ds *host_sd)
2225 {
2226     struct target_semid_ds *target_sd;
2227 
2228     if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2229         return -TARGET_EFAULT;
2230     if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
2231         return -TARGET_EFAULT;;
2232     target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
2233     target_sd->sem_otime = tswapl(host_sd->sem_otime);
2234     target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
2235     unlock_user_struct(target_sd, target_addr, 1);
2236     return 0;
2237 }
2238 
2239 struct target_seminfo {
2240     int semmap;
2241     int semmni;
2242     int semmns;
2243     int semmnu;
2244     int semmsl;
2245     int semopm;
2246     int semume;
2247     int semusz;
2248     int semvmx;
2249     int semaem;
2250 };
2251 
2252 static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2253                                               struct seminfo *host_seminfo)
2254 {
2255     struct target_seminfo *target_seminfo;
2256     if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2257         return -TARGET_EFAULT;
2258     __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2259     __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2260     __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2261     __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2262     __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2263     __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2264     __put_user(host_seminfo->semume, &target_seminfo->semume);
2265     __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2266     __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2267     __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2268     unlock_user_struct(target_seminfo, target_addr, 1);
2269     return 0;
2270 }
2271 
2272 union semun {
2273 	int val;
2274 	struct semid_ds *buf;
2275 	unsigned short *array;
2276 	struct seminfo *__buf;
2277 };
2278 
2279 union target_semun {
2280 	int val;
2281 	abi_ulong buf;
2282 	abi_ulong array;
2283 	abi_ulong __buf;
2284 };
2285 
2286 static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2287                                                abi_ulong target_addr)
2288 {
2289     int nsems;
2290     unsigned short *array;
2291     union semun semun;
2292     struct semid_ds semid_ds;
2293     int i, ret;
2294 
2295     semun.buf = &semid_ds;
2296 
2297     ret = semctl(semid, 0, IPC_STAT, semun);
2298     if (ret == -1)
2299         return get_errno(ret);
2300 
2301     nsems = semid_ds.sem_nsems;
2302 
2303     *host_array = malloc(nsems*sizeof(unsigned short));
2304     array = lock_user(VERIFY_READ, target_addr,
2305                       nsems*sizeof(unsigned short), 1);
2306     if (!array)
2307         return -TARGET_EFAULT;
2308 
2309     for(i=0; i<nsems; i++) {
2310         __get_user((*host_array)[i], &array[i]);
2311     }
2312     unlock_user(array, target_addr, 0);
2313 
2314     return 0;
2315 }
2316 
2317 static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2318                                                unsigned short **host_array)
2319 {
2320     int nsems;
2321     unsigned short *array;
2322     union semun semun;
2323     struct semid_ds semid_ds;
2324     int i, ret;
2325 
2326     semun.buf = &semid_ds;
2327 
2328     ret = semctl(semid, 0, IPC_STAT, semun);
2329     if (ret == -1)
2330         return get_errno(ret);
2331 
2332     nsems = semid_ds.sem_nsems;
2333 
2334     array = lock_user(VERIFY_WRITE, target_addr,
2335                       nsems*sizeof(unsigned short), 0);
2336     if (!array)
2337         return -TARGET_EFAULT;
2338 
2339     for(i=0; i<nsems; i++) {
2340         __put_user((*host_array)[i], &array[i]);
2341     }
2342     free(*host_array);
2343     unlock_user(array, target_addr, 1);
2344 
2345     return 0;
2346 }
2347 
2348 static inline abi_long do_semctl(int semid, int semnum, int cmd,
2349                                  union target_semun target_su)
2350 {
2351     union semun arg;
2352     struct semid_ds dsarg;
2353     unsigned short *array = NULL;
2354     struct seminfo seminfo;
2355     abi_long ret = -TARGET_EINVAL;
2356     abi_long err;
2357     cmd &= 0xff;
2358 
2359     switch( cmd ) {
2360 	case GETVAL:
2361 	case SETVAL:
2362             arg.val = tswapl(target_su.val);
2363             ret = get_errno(semctl(semid, semnum, cmd, arg));
2364             target_su.val = tswapl(arg.val);
2365             break;
2366 	case GETALL:
2367 	case SETALL:
2368             err = target_to_host_semarray(semid, &array, target_su.array);
2369             if (err)
2370                 return err;
2371             arg.array = array;
2372             ret = get_errno(semctl(semid, semnum, cmd, arg));
2373             err = host_to_target_semarray(semid, target_su.array, &array);
2374             if (err)
2375                 return err;
2376             break;
2377 	case IPC_STAT:
2378 	case IPC_SET:
2379 	case SEM_STAT:
2380             err = target_to_host_semid_ds(&dsarg, target_su.buf);
2381             if (err)
2382                 return err;
2383             arg.buf = &dsarg;
2384             ret = get_errno(semctl(semid, semnum, cmd, arg));
2385             err = host_to_target_semid_ds(target_su.buf, &dsarg);
2386             if (err)
2387                 return err;
2388             break;
2389 	case IPC_INFO:
2390 	case SEM_INFO:
2391             arg.__buf = &seminfo;
2392             ret = get_errno(semctl(semid, semnum, cmd, arg));
2393             err = host_to_target_seminfo(target_su.__buf, &seminfo);
2394             if (err)
2395                 return err;
2396             break;
2397 	case IPC_RMID:
2398 	case GETPID:
2399 	case GETNCNT:
2400 	case GETZCNT:
2401             ret = get_errno(semctl(semid, semnum, cmd, NULL));
2402             break;
2403     }
2404 
2405     return ret;
2406 }
2407 
2408 struct target_sembuf {
2409     unsigned short sem_num;
2410     short sem_op;
2411     short sem_flg;
2412 };
2413 
2414 static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2415                                              abi_ulong target_addr,
2416                                              unsigned nsops)
2417 {
2418     struct target_sembuf *target_sembuf;
2419     int i;
2420 
2421     target_sembuf = lock_user(VERIFY_READ, target_addr,
2422                               nsops*sizeof(struct target_sembuf), 1);
2423     if (!target_sembuf)
2424         return -TARGET_EFAULT;
2425 
2426     for(i=0; i<nsops; i++) {
2427         __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2428         __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2429         __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2430     }
2431 
2432     unlock_user(target_sembuf, target_addr, 0);
2433 
2434     return 0;
2435 }
2436 
2437 static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2438 {
2439     struct sembuf sops[nsops];
2440 
2441     if (target_to_host_sembuf(sops, ptr, nsops))
2442         return -TARGET_EFAULT;
2443 
2444     return semop(semid, sops, nsops);
2445 }
2446 
2447 struct target_msqid_ds
2448 {
2449     struct target_ipc_perm msg_perm;
2450     abi_ulong msg_stime;
2451 #if TARGET_ABI_BITS == 32
2452     abi_ulong __unused1;
2453 #endif
2454     abi_ulong msg_rtime;
2455 #if TARGET_ABI_BITS == 32
2456     abi_ulong __unused2;
2457 #endif
2458     abi_ulong msg_ctime;
2459 #if TARGET_ABI_BITS == 32
2460     abi_ulong __unused3;
2461 #endif
2462     abi_ulong __msg_cbytes;
2463     abi_ulong msg_qnum;
2464     abi_ulong msg_qbytes;
2465     abi_ulong msg_lspid;
2466     abi_ulong msg_lrpid;
2467     abi_ulong __unused4;
2468     abi_ulong __unused5;
2469 };
2470 
2471 static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2472                                                abi_ulong target_addr)
2473 {
2474     struct target_msqid_ds *target_md;
2475 
2476     if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2477         return -TARGET_EFAULT;
2478     if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2479         return -TARGET_EFAULT;
2480     host_md->msg_stime = tswapl(target_md->msg_stime);
2481     host_md->msg_rtime = tswapl(target_md->msg_rtime);
2482     host_md->msg_ctime = tswapl(target_md->msg_ctime);
2483     host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
2484     host_md->msg_qnum = tswapl(target_md->msg_qnum);
2485     host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
2486     host_md->msg_lspid = tswapl(target_md->msg_lspid);
2487     host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
2488     unlock_user_struct(target_md, target_addr, 0);
2489     return 0;
2490 }
2491 
2492 static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2493                                                struct msqid_ds *host_md)
2494 {
2495     struct target_msqid_ds *target_md;
2496 
2497     if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2498         return -TARGET_EFAULT;
2499     if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2500         return -TARGET_EFAULT;
2501     target_md->msg_stime = tswapl(host_md->msg_stime);
2502     target_md->msg_rtime = tswapl(host_md->msg_rtime);
2503     target_md->msg_ctime = tswapl(host_md->msg_ctime);
2504     target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
2505     target_md->msg_qnum = tswapl(host_md->msg_qnum);
2506     target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
2507     target_md->msg_lspid = tswapl(host_md->msg_lspid);
2508     target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
2509     unlock_user_struct(target_md, target_addr, 1);
2510     return 0;
2511 }
2512 
2513 struct target_msginfo {
2514     int msgpool;
2515     int msgmap;
2516     int msgmax;
2517     int msgmnb;
2518     int msgmni;
2519     int msgssz;
2520     int msgtql;
2521     unsigned short int msgseg;
2522 };
2523 
2524 static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2525                                               struct msginfo *host_msginfo)
2526 {
2527     struct target_msginfo *target_msginfo;
2528     if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2529         return -TARGET_EFAULT;
2530     __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2531     __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2532     __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2533     __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2534     __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2535     __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2536     __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2537     __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2538     unlock_user_struct(target_msginfo, target_addr, 1);
2539     return 0;
2540 }
2541 
2542 static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
2543 {
2544     struct msqid_ds dsarg;
2545     struct msginfo msginfo;
2546     abi_long ret = -TARGET_EINVAL;
2547 
2548     cmd &= 0xff;
2549 
2550     switch (cmd) {
2551     case IPC_STAT:
2552     case IPC_SET:
2553     case MSG_STAT:
2554         if (target_to_host_msqid_ds(&dsarg,ptr))
2555             return -TARGET_EFAULT;
2556         ret = get_errno(msgctl(msgid, cmd, &dsarg));
2557         if (host_to_target_msqid_ds(ptr,&dsarg))
2558             return -TARGET_EFAULT;
2559         break;
2560     case IPC_RMID:
2561         ret = get_errno(msgctl(msgid, cmd, NULL));
2562         break;
2563     case IPC_INFO:
2564     case MSG_INFO:
2565         ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2566         if (host_to_target_msginfo(ptr, &msginfo))
2567             return -TARGET_EFAULT;
2568         break;
2569     }
2570 
2571     return ret;
2572 }
2573 
2574 struct target_msgbuf {
2575     abi_long mtype;
2576     char	mtext[1];
2577 };
2578 
2579 static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2580                                  unsigned int msgsz, int msgflg)
2581 {
2582     struct target_msgbuf *target_mb;
2583     struct msgbuf *host_mb;
2584     abi_long ret = 0;
2585 
2586     if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2587         return -TARGET_EFAULT;
2588     host_mb = malloc(msgsz+sizeof(long));
2589     host_mb->mtype = (abi_long) tswapl(target_mb->mtype);
2590     memcpy(host_mb->mtext, target_mb->mtext, msgsz);
2591     ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2592     free(host_mb);
2593     unlock_user_struct(target_mb, msgp, 0);
2594 
2595     return ret;
2596 }
2597 
2598 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
2599                                  unsigned int msgsz, abi_long msgtyp,
2600                                  int msgflg)
2601 {
2602     struct target_msgbuf *target_mb;
2603     char *target_mtext;
2604     struct msgbuf *host_mb;
2605     abi_long ret = 0;
2606 
2607     if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2608         return -TARGET_EFAULT;
2609 
2610     host_mb = malloc(msgsz+sizeof(long));
2611     ret = get_errno(msgrcv(msqid, host_mb, msgsz, tswapl(msgtyp), msgflg));
2612 
2613     if (ret > 0) {
2614         abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2615         target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2616         if (!target_mtext) {
2617             ret = -TARGET_EFAULT;
2618             goto end;
2619         }
2620         memcpy(target_mb->mtext, host_mb->mtext, ret);
2621         unlock_user(target_mtext, target_mtext_addr, ret);
2622     }
2623 
2624     target_mb->mtype = tswapl(host_mb->mtype);
2625     free(host_mb);
2626 
2627 end:
2628     if (target_mb)
2629         unlock_user_struct(target_mb, msgp, 1);
2630     return ret;
2631 }
2632 
2633 struct target_shmid_ds
2634 {
2635     struct target_ipc_perm shm_perm;
2636     abi_ulong shm_segsz;
2637     abi_ulong shm_atime;
2638 #if TARGET_ABI_BITS == 32
2639     abi_ulong __unused1;
2640 #endif
2641     abi_ulong shm_dtime;
2642 #if TARGET_ABI_BITS == 32
2643     abi_ulong __unused2;
2644 #endif
2645     abi_ulong shm_ctime;
2646 #if TARGET_ABI_BITS == 32
2647     abi_ulong __unused3;
2648 #endif
2649     int shm_cpid;
2650     int shm_lpid;
2651     abi_ulong shm_nattch;
2652     unsigned long int __unused4;
2653     unsigned long int __unused5;
2654 };
2655 
2656 static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
2657                                                abi_ulong target_addr)
2658 {
2659     struct target_shmid_ds *target_sd;
2660 
2661     if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2662         return -TARGET_EFAULT;
2663     if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
2664         return -TARGET_EFAULT;
2665     __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2666     __get_user(host_sd->shm_atime, &target_sd->shm_atime);
2667     __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2668     __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2669     __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2670     __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2671     __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2672     unlock_user_struct(target_sd, target_addr, 0);
2673     return 0;
2674 }
2675 
2676 static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
2677                                                struct shmid_ds *host_sd)
2678 {
2679     struct target_shmid_ds *target_sd;
2680 
2681     if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2682         return -TARGET_EFAULT;
2683     if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
2684         return -TARGET_EFAULT;
2685     __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
2686     __put_user(host_sd->shm_atime, &target_sd->shm_atime);
2687     __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
2688     __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
2689     __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
2690     __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
2691     __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
2692     unlock_user_struct(target_sd, target_addr, 1);
2693     return 0;
2694 }
2695 
2696 struct  target_shminfo {
2697     abi_ulong shmmax;
2698     abi_ulong shmmin;
2699     abi_ulong shmmni;
2700     abi_ulong shmseg;
2701     abi_ulong shmall;
2702 };
2703 
2704 static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
2705                                               struct shminfo *host_shminfo)
2706 {
2707     struct target_shminfo *target_shminfo;
2708     if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
2709         return -TARGET_EFAULT;
2710     __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
2711     __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
2712     __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
2713     __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
2714     __put_user(host_shminfo->shmall, &target_shminfo->shmall);
2715     unlock_user_struct(target_shminfo, target_addr, 1);
2716     return 0;
2717 }
2718 
2719 struct target_shm_info {
2720     int used_ids;
2721     abi_ulong shm_tot;
2722     abi_ulong shm_rss;
2723     abi_ulong shm_swp;
2724     abi_ulong swap_attempts;
2725     abi_ulong swap_successes;
2726 };
2727 
2728 static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
2729                                                struct shm_info *host_shm_info)
2730 {
2731     struct target_shm_info *target_shm_info;
2732     if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
2733         return -TARGET_EFAULT;
2734     __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
2735     __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
2736     __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
2737     __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
2738     __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
2739     __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
2740     unlock_user_struct(target_shm_info, target_addr, 1);
2741     return 0;
2742 }
2743 
2744 static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
2745 {
2746     struct shmid_ds dsarg;
2747     struct shminfo shminfo;
2748     struct shm_info shm_info;
2749     abi_long ret = -TARGET_EINVAL;
2750 
2751     cmd &= 0xff;
2752 
2753     switch(cmd) {
2754     case IPC_STAT:
2755     case IPC_SET:
2756     case SHM_STAT:
2757         if (target_to_host_shmid_ds(&dsarg, buf))
2758             return -TARGET_EFAULT;
2759         ret = get_errno(shmctl(shmid, cmd, &dsarg));
2760         if (host_to_target_shmid_ds(buf, &dsarg))
2761             return -TARGET_EFAULT;
2762         break;
2763     case IPC_INFO:
2764         ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
2765         if (host_to_target_shminfo(buf, &shminfo))
2766             return -TARGET_EFAULT;
2767         break;
2768     case SHM_INFO:
2769         ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
2770         if (host_to_target_shm_info(buf, &shm_info))
2771             return -TARGET_EFAULT;
2772         break;
2773     case IPC_RMID:
2774     case SHM_LOCK:
2775     case SHM_UNLOCK:
2776         ret = get_errno(shmctl(shmid, cmd, NULL));
2777         break;
2778     }
2779 
2780     return ret;
2781 }
2782 
2783 static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
2784 {
2785     abi_long raddr;
2786     void *host_raddr;
2787     struct shmid_ds shm_info;
2788     int i,ret;
2789 
2790     /* find out the length of the shared memory segment */
2791     ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
2792     if (is_error(ret)) {
2793         /* can't get length, bail out */
2794         return ret;
2795     }
2796 
2797     mmap_lock();
2798 
2799     if (shmaddr)
2800         host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
2801     else {
2802         abi_ulong mmap_start;
2803 
2804         mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
2805 
2806         if (mmap_start == -1) {
2807             errno = ENOMEM;
2808             host_raddr = (void *)-1;
2809         } else
2810             host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
2811     }
2812 
2813     if (host_raddr == (void *)-1) {
2814         mmap_unlock();
2815         return get_errno((long)host_raddr);
2816     }
2817     raddr=h2g((unsigned long)host_raddr);
2818 
2819     page_set_flags(raddr, raddr + shm_info.shm_segsz,
2820                    PAGE_VALID | PAGE_READ |
2821                    ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
2822 
2823     for (i = 0; i < N_SHM_REGIONS; i++) {
2824         if (shm_regions[i].start == 0) {
2825             shm_regions[i].start = raddr;
2826             shm_regions[i].size = shm_info.shm_segsz;
2827             break;
2828         }
2829     }
2830 
2831     mmap_unlock();
2832     return raddr;
2833 
2834 }
2835 
2836 static inline abi_long do_shmdt(abi_ulong shmaddr)
2837 {
2838     int i;
2839 
2840     for (i = 0; i < N_SHM_REGIONS; ++i) {
2841         if (shm_regions[i].start == shmaddr) {
2842             shm_regions[i].start = 0;
2843             page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0);
2844             break;
2845         }
2846     }
2847 
2848     return get_errno(shmdt(g2h(shmaddr)));
2849 }
2850 
2851 #ifdef TARGET_NR_ipc
2852 /* ??? This only works with linear mappings.  */
2853 /* do_ipc() must return target values and target errnos. */
2854 static abi_long do_ipc(unsigned int call, int first,
2855                        int second, int third,
2856                        abi_long ptr, abi_long fifth)
2857 {
2858     int version;
2859     abi_long ret = 0;
2860 
2861     version = call >> 16;
2862     call &= 0xffff;
2863 
2864     switch (call) {
2865     case IPCOP_semop:
2866         ret = do_semop(first, ptr, second);
2867         break;
2868 
2869     case IPCOP_semget:
2870         ret = get_errno(semget(first, second, third));
2871         break;
2872 
2873     case IPCOP_semctl:
2874         ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
2875         break;
2876 
2877     case IPCOP_msgget:
2878         ret = get_errno(msgget(first, second));
2879         break;
2880 
2881     case IPCOP_msgsnd:
2882         ret = do_msgsnd(first, ptr, second, third);
2883         break;
2884 
2885     case IPCOP_msgctl:
2886         ret = do_msgctl(first, second, ptr);
2887         break;
2888 
2889     case IPCOP_msgrcv:
2890         switch (version) {
2891         case 0:
2892             {
2893                 struct target_ipc_kludge {
2894                     abi_long msgp;
2895                     abi_long msgtyp;
2896                 } *tmp;
2897 
2898                 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
2899                     ret = -TARGET_EFAULT;
2900                     break;
2901                 }
2902 
2903                 ret = do_msgrcv(first, tmp->msgp, second, tmp->msgtyp, third);
2904 
2905                 unlock_user_struct(tmp, ptr, 0);
2906                 break;
2907             }
2908         default:
2909             ret = do_msgrcv(first, ptr, second, fifth, third);
2910         }
2911         break;
2912 
2913     case IPCOP_shmat:
2914         switch (version) {
2915         default:
2916         {
2917             abi_ulong raddr;
2918             raddr = do_shmat(first, ptr, second);
2919             if (is_error(raddr))
2920                 return get_errno(raddr);
2921             if (put_user_ual(raddr, third))
2922                 return -TARGET_EFAULT;
2923             break;
2924         }
2925         case 1:
2926             ret = -TARGET_EINVAL;
2927             break;
2928         }
2929 	break;
2930     case IPCOP_shmdt:
2931         ret = do_shmdt(ptr);
2932 	break;
2933 
2934     case IPCOP_shmget:
2935 	/* IPC_* flag values are the same on all linux platforms */
2936 	ret = get_errno(shmget(first, second, third));
2937 	break;
2938 
2939 	/* IPC_* and SHM_* command values are the same on all linux platforms */
2940     case IPCOP_shmctl:
2941         ret = do_shmctl(first, second, third);
2942         break;
2943     default:
2944 	gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
2945 	ret = -TARGET_ENOSYS;
2946 	break;
2947     }
2948     return ret;
2949 }
2950 #endif
2951 
2952 /* kernel structure types definitions */
2953 #define IFNAMSIZ        16
2954 
2955 #define STRUCT(name, ...) STRUCT_ ## name,
2956 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
2957 enum {
2958 #include "syscall_types.h"
2959 };
2960 #undef STRUCT
2961 #undef STRUCT_SPECIAL
2962 
2963 #define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = {  __VA_ARGS__, TYPE_NULL };
2964 #define STRUCT_SPECIAL(name)
2965 #include "syscall_types.h"
2966 #undef STRUCT
2967 #undef STRUCT_SPECIAL
2968 
2969 typedef struct IOCTLEntry IOCTLEntry;
2970 
2971 typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,
2972                              int fd, abi_long cmd, abi_long arg);
2973 
2974 struct IOCTLEntry {
2975     unsigned int target_cmd;
2976     unsigned int host_cmd;
2977     const char *name;
2978     int access;
2979     do_ioctl_fn *do_ioctl;
2980     const argtype arg_type[5];
2981 };
2982 
2983 #define IOC_R 0x0001
2984 #define IOC_W 0x0002
2985 #define IOC_RW (IOC_R | IOC_W)
2986 
2987 #define MAX_STRUCT_SIZE 4096
2988 
2989 /* So fiemap access checks don't overflow on 32 bit systems.
2990  * This is very slightly smaller than the limit imposed by
2991  * the underlying kernel.
2992  */
2993 #define FIEMAP_MAX_EXTENTS ((UINT_MAX - sizeof(struct fiemap))  \
2994                             / sizeof(struct fiemap_extent))
2995 
2996 static abi_long do_ioctl_fs_ioc_fiemap(const IOCTLEntry *ie, uint8_t *buf_temp,
2997                                        int fd, abi_long cmd, abi_long arg)
2998 {
2999     /* The parameter for this ioctl is a struct fiemap followed
3000      * by an array of struct fiemap_extent whose size is set
3001      * in fiemap->fm_extent_count. The array is filled in by the
3002      * ioctl.
3003      */
3004     int target_size_in, target_size_out;
3005     struct fiemap *fm;
3006     const argtype *arg_type = ie->arg_type;
3007     const argtype extent_arg_type[] = { MK_STRUCT(STRUCT_fiemap_extent) };
3008     void *argptr, *p;
3009     abi_long ret;
3010     int i, extent_size = thunk_type_size(extent_arg_type, 0);
3011     uint32_t outbufsz;
3012     int free_fm = 0;
3013 
3014     assert(arg_type[0] == TYPE_PTR);
3015     assert(ie->access == IOC_RW);
3016     arg_type++;
3017     target_size_in = thunk_type_size(arg_type, 0);
3018     argptr = lock_user(VERIFY_READ, arg, target_size_in, 1);
3019     if (!argptr) {
3020         return -TARGET_EFAULT;
3021     }
3022     thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3023     unlock_user(argptr, arg, 0);
3024     fm = (struct fiemap *)buf_temp;
3025     if (fm->fm_extent_count > FIEMAP_MAX_EXTENTS) {
3026         return -TARGET_EINVAL;
3027     }
3028 
3029     outbufsz = sizeof (*fm) +
3030         (sizeof(struct fiemap_extent) * fm->fm_extent_count);
3031 
3032     if (outbufsz > MAX_STRUCT_SIZE) {
3033         /* We can't fit all the extents into the fixed size buffer.
3034          * Allocate one that is large enough and use it instead.
3035          */
3036         fm = malloc(outbufsz);
3037         if (!fm) {
3038             return -TARGET_ENOMEM;
3039         }
3040         memcpy(fm, buf_temp, sizeof(struct fiemap));
3041         free_fm = 1;
3042     }
3043     ret = get_errno(ioctl(fd, ie->host_cmd, fm));
3044     if (!is_error(ret)) {
3045         target_size_out = target_size_in;
3046         /* An extent_count of 0 means we were only counting the extents
3047          * so there are no structs to copy
3048          */
3049         if (fm->fm_extent_count != 0) {
3050             target_size_out += fm->fm_mapped_extents * extent_size;
3051         }
3052         argptr = lock_user(VERIFY_WRITE, arg, target_size_out, 0);
3053         if (!argptr) {
3054             ret = -TARGET_EFAULT;
3055         } else {
3056             /* Convert the struct fiemap */
3057             thunk_convert(argptr, fm, arg_type, THUNK_TARGET);
3058             if (fm->fm_extent_count != 0) {
3059                 p = argptr + target_size_in;
3060                 /* ...and then all the struct fiemap_extents */
3061                 for (i = 0; i < fm->fm_mapped_extents; i++) {
3062                     thunk_convert(p, &fm->fm_extents[i], extent_arg_type,
3063                                   THUNK_TARGET);
3064                     p += extent_size;
3065                 }
3066             }
3067             unlock_user(argptr, arg, target_size_out);
3068         }
3069     }
3070     if (free_fm) {
3071         free(fm);
3072     }
3073     return ret;
3074 }
3075 
3076 static IOCTLEntry ioctl_entries[] = {
3077 #define IOCTL(cmd, access, ...) \
3078     { TARGET_ ## cmd, cmd, #cmd, access, 0, {  __VA_ARGS__ } },
3079 #define IOCTL_SPECIAL(cmd, access, dofn, ...)                      \
3080     { TARGET_ ## cmd, cmd, #cmd, access, dofn, {  __VA_ARGS__ } },
3081 #include "ioctls.h"
3082     { 0, 0, },
3083 };
3084 
3085 /* ??? Implement proper locking for ioctls.  */
3086 /* do_ioctl() Must return target values and target errnos. */
3087 static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
3088 {
3089     const IOCTLEntry *ie;
3090     const argtype *arg_type;
3091     abi_long ret;
3092     uint8_t buf_temp[MAX_STRUCT_SIZE];
3093     int target_size;
3094     void *argptr;
3095 
3096     ie = ioctl_entries;
3097     for(;;) {
3098         if (ie->target_cmd == 0) {
3099             gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
3100             return -TARGET_ENOSYS;
3101         }
3102         if (ie->target_cmd == cmd)
3103             break;
3104         ie++;
3105     }
3106     arg_type = ie->arg_type;
3107 #if defined(DEBUG)
3108     gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
3109 #endif
3110     if (ie->do_ioctl) {
3111         return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
3112     }
3113 
3114     switch(arg_type[0]) {
3115     case TYPE_NULL:
3116         /* no argument */
3117         ret = get_errno(ioctl(fd, ie->host_cmd));
3118         break;
3119     case TYPE_PTRVOID:
3120     case TYPE_INT:
3121         /* int argment */
3122         ret = get_errno(ioctl(fd, ie->host_cmd, arg));
3123         break;
3124     case TYPE_PTR:
3125         arg_type++;
3126         target_size = thunk_type_size(arg_type, 0);
3127         switch(ie->access) {
3128         case IOC_R:
3129             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3130             if (!is_error(ret)) {
3131                 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3132                 if (!argptr)
3133                     return -TARGET_EFAULT;
3134                 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3135                 unlock_user(argptr, arg, target_size);
3136             }
3137             break;
3138         case IOC_W:
3139             argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3140             if (!argptr)
3141                 return -TARGET_EFAULT;
3142             thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3143             unlock_user(argptr, arg, 0);
3144             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3145             break;
3146         default:
3147         case IOC_RW:
3148             argptr = lock_user(VERIFY_READ, arg, target_size, 1);
3149             if (!argptr)
3150                 return -TARGET_EFAULT;
3151             thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
3152             unlock_user(argptr, arg, 0);
3153             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
3154             if (!is_error(ret)) {
3155                 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
3156                 if (!argptr)
3157                     return -TARGET_EFAULT;
3158                 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
3159                 unlock_user(argptr, arg, target_size);
3160             }
3161             break;
3162         }
3163         break;
3164     default:
3165         gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
3166                  (long)cmd, arg_type[0]);
3167         ret = -TARGET_ENOSYS;
3168         break;
3169     }
3170     return ret;
3171 }
3172 
3173 static const bitmask_transtbl iflag_tbl[] = {
3174         { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
3175         { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
3176         { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
3177         { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
3178         { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
3179         { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
3180         { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
3181         { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
3182         { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
3183         { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
3184         { TARGET_IXON, TARGET_IXON, IXON, IXON },
3185         { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
3186         { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
3187         { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
3188         { 0, 0, 0, 0 }
3189 };
3190 
3191 static const bitmask_transtbl oflag_tbl[] = {
3192 	{ TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
3193 	{ TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
3194 	{ TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
3195 	{ TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
3196 	{ TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
3197 	{ TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
3198 	{ TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
3199 	{ TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
3200 	{ TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
3201 	{ TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
3202 	{ TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
3203 	{ TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
3204 	{ TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
3205 	{ TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
3206 	{ TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
3207 	{ TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
3208 	{ TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
3209 	{ TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
3210 	{ TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
3211 	{ TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
3212 	{ TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
3213 	{ TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
3214 	{ TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
3215 	{ TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
3216 	{ 0, 0, 0, 0 }
3217 };
3218 
3219 static const bitmask_transtbl cflag_tbl[] = {
3220 	{ TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
3221 	{ TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
3222 	{ TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
3223 	{ TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
3224 	{ TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
3225 	{ TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
3226 	{ TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
3227 	{ TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
3228 	{ TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
3229 	{ TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
3230 	{ TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
3231 	{ TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
3232 	{ TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
3233 	{ TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
3234 	{ TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
3235 	{ TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
3236 	{ TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
3237 	{ TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
3238 	{ TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
3239 	{ TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
3240 	{ TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
3241 	{ TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
3242 	{ TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
3243 	{ TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
3244 	{ TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
3245 	{ TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
3246 	{ TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
3247 	{ TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
3248 	{ TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
3249 	{ TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
3250 	{ TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
3251 	{ 0, 0, 0, 0 }
3252 };
3253 
3254 static const bitmask_transtbl lflag_tbl[] = {
3255 	{ TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
3256 	{ TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
3257 	{ TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
3258 	{ TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
3259 	{ TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
3260 	{ TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
3261 	{ TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
3262 	{ TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
3263 	{ TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
3264 	{ TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
3265 	{ TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
3266 	{ TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
3267 	{ TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
3268 	{ TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
3269 	{ TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
3270 	{ 0, 0, 0, 0 }
3271 };
3272 
3273 static void target_to_host_termios (void *dst, const void *src)
3274 {
3275     struct host_termios *host = dst;
3276     const struct target_termios *target = src;
3277 
3278     host->c_iflag =
3279         target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
3280     host->c_oflag =
3281         target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
3282     host->c_cflag =
3283         target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
3284     host->c_lflag =
3285         target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
3286     host->c_line = target->c_line;
3287 
3288     memset(host->c_cc, 0, sizeof(host->c_cc));
3289     host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
3290     host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
3291     host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
3292     host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
3293     host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
3294     host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
3295     host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
3296     host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
3297     host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
3298     host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
3299     host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
3300     host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
3301     host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
3302     host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
3303     host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
3304     host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
3305     host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
3306 }
3307 
3308 static void host_to_target_termios (void *dst, const void *src)
3309 {
3310     struct target_termios *target = dst;
3311     const struct host_termios *host = src;
3312 
3313     target->c_iflag =
3314         tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
3315     target->c_oflag =
3316         tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
3317     target->c_cflag =
3318         tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
3319     target->c_lflag =
3320         tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
3321     target->c_line = host->c_line;
3322 
3323     memset(target->c_cc, 0, sizeof(target->c_cc));
3324     target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
3325     target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
3326     target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
3327     target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
3328     target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
3329     target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
3330     target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
3331     target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
3332     target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
3333     target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
3334     target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
3335     target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
3336     target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
3337     target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
3338     target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
3339     target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
3340     target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
3341 }
3342 
3343 static const StructEntry struct_termios_def = {
3344     .convert = { host_to_target_termios, target_to_host_termios },
3345     .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
3346     .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
3347 };
3348 
3349 static bitmask_transtbl mmap_flags_tbl[] = {
3350 	{ TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
3351 	{ TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
3352 	{ TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
3353 	{ TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
3354 	{ TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
3355 	{ TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
3356 	{ TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
3357 	{ TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
3358 	{ 0, 0, 0, 0 }
3359 };
3360 
3361 #if defined(TARGET_I386)
3362 
3363 /* NOTE: there is really one LDT for all the threads */
3364 static uint8_t *ldt_table;
3365 
3366 static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
3367 {
3368     int size;
3369     void *p;
3370 
3371     if (!ldt_table)
3372         return 0;
3373     size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
3374     if (size > bytecount)
3375         size = bytecount;
3376     p = lock_user(VERIFY_WRITE, ptr, size, 0);
3377     if (!p)
3378         return -TARGET_EFAULT;
3379     /* ??? Should this by byteswapped?  */
3380     memcpy(p, ldt_table, size);
3381     unlock_user(p, ptr, size);
3382     return size;
3383 }
3384 
3385 /* XXX: add locking support */
3386 static abi_long write_ldt(CPUX86State *env,
3387                           abi_ulong ptr, unsigned long bytecount, int oldmode)
3388 {
3389     struct target_modify_ldt_ldt_s ldt_info;
3390     struct target_modify_ldt_ldt_s *target_ldt_info;
3391     int seg_32bit, contents, read_exec_only, limit_in_pages;
3392     int seg_not_present, useable, lm;
3393     uint32_t *lp, entry_1, entry_2;
3394 
3395     if (bytecount != sizeof(ldt_info))
3396         return -TARGET_EINVAL;
3397     if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
3398         return -TARGET_EFAULT;
3399     ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3400     ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3401     ldt_info.limit = tswap32(target_ldt_info->limit);
3402     ldt_info.flags = tswap32(target_ldt_info->flags);
3403     unlock_user_struct(target_ldt_info, ptr, 0);
3404 
3405     if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
3406         return -TARGET_EINVAL;
3407     seg_32bit = ldt_info.flags & 1;
3408     contents = (ldt_info.flags >> 1) & 3;
3409     read_exec_only = (ldt_info.flags >> 3) & 1;
3410     limit_in_pages = (ldt_info.flags >> 4) & 1;
3411     seg_not_present = (ldt_info.flags >> 5) & 1;
3412     useable = (ldt_info.flags >> 6) & 1;
3413 #ifdef TARGET_ABI32
3414     lm = 0;
3415 #else
3416     lm = (ldt_info.flags >> 7) & 1;
3417 #endif
3418     if (contents == 3) {
3419         if (oldmode)
3420             return -TARGET_EINVAL;
3421         if (seg_not_present == 0)
3422             return -TARGET_EINVAL;
3423     }
3424     /* allocate the LDT */
3425     if (!ldt_table) {
3426         env->ldt.base = target_mmap(0,
3427                                     TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
3428                                     PROT_READ|PROT_WRITE,
3429                                     MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
3430         if (env->ldt.base == -1)
3431             return -TARGET_ENOMEM;
3432         memset(g2h(env->ldt.base), 0,
3433                TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
3434         env->ldt.limit = 0xffff;
3435         ldt_table = g2h(env->ldt.base);
3436     }
3437 
3438     /* NOTE: same code as Linux kernel */
3439     /* Allow LDTs to be cleared by the user. */
3440     if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3441         if (oldmode ||
3442             (contents == 0		&&
3443              read_exec_only == 1	&&
3444              seg_32bit == 0		&&
3445              limit_in_pages == 0	&&
3446              seg_not_present == 1	&&
3447              useable == 0 )) {
3448             entry_1 = 0;
3449             entry_2 = 0;
3450             goto install;
3451         }
3452     }
3453 
3454     entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3455         (ldt_info.limit & 0x0ffff);
3456     entry_2 = (ldt_info.base_addr & 0xff000000) |
3457         ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3458         (ldt_info.limit & 0xf0000) |
3459         ((read_exec_only ^ 1) << 9) |
3460         (contents << 10) |
3461         ((seg_not_present ^ 1) << 15) |
3462         (seg_32bit << 22) |
3463         (limit_in_pages << 23) |
3464         (lm << 21) |
3465         0x7000;
3466     if (!oldmode)
3467         entry_2 |= (useable << 20);
3468 
3469     /* Install the new entry ...  */
3470 install:
3471     lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
3472     lp[0] = tswap32(entry_1);
3473     lp[1] = tswap32(entry_2);
3474     return 0;
3475 }
3476 
3477 /* specific and weird i386 syscalls */
3478 static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
3479                               unsigned long bytecount)
3480 {
3481     abi_long ret;
3482 
3483     switch (func) {
3484     case 0:
3485         ret = read_ldt(ptr, bytecount);
3486         break;
3487     case 1:
3488         ret = write_ldt(env, ptr, bytecount, 1);
3489         break;
3490     case 0x11:
3491         ret = write_ldt(env, ptr, bytecount, 0);
3492         break;
3493     default:
3494         ret = -TARGET_ENOSYS;
3495         break;
3496     }
3497     return ret;
3498 }
3499 
3500 #if defined(TARGET_I386) && defined(TARGET_ABI32)
3501 static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
3502 {
3503     uint64_t *gdt_table = g2h(env->gdt.base);
3504     struct target_modify_ldt_ldt_s ldt_info;
3505     struct target_modify_ldt_ldt_s *target_ldt_info;
3506     int seg_32bit, contents, read_exec_only, limit_in_pages;
3507     int seg_not_present, useable, lm;
3508     uint32_t *lp, entry_1, entry_2;
3509     int i;
3510 
3511     lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3512     if (!target_ldt_info)
3513         return -TARGET_EFAULT;
3514     ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3515     ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3516     ldt_info.limit = tswap32(target_ldt_info->limit);
3517     ldt_info.flags = tswap32(target_ldt_info->flags);
3518     if (ldt_info.entry_number == -1) {
3519         for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
3520             if (gdt_table[i] == 0) {
3521                 ldt_info.entry_number = i;
3522                 target_ldt_info->entry_number = tswap32(i);
3523                 break;
3524             }
3525         }
3526     }
3527     unlock_user_struct(target_ldt_info, ptr, 1);
3528 
3529     if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
3530         ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
3531            return -TARGET_EINVAL;
3532     seg_32bit = ldt_info.flags & 1;
3533     contents = (ldt_info.flags >> 1) & 3;
3534     read_exec_only = (ldt_info.flags >> 3) & 1;
3535     limit_in_pages = (ldt_info.flags >> 4) & 1;
3536     seg_not_present = (ldt_info.flags >> 5) & 1;
3537     useable = (ldt_info.flags >> 6) & 1;
3538 #ifdef TARGET_ABI32
3539     lm = 0;
3540 #else
3541     lm = (ldt_info.flags >> 7) & 1;
3542 #endif
3543 
3544     if (contents == 3) {
3545         if (seg_not_present == 0)
3546             return -TARGET_EINVAL;
3547     }
3548 
3549     /* NOTE: same code as Linux kernel */
3550     /* Allow LDTs to be cleared by the user. */
3551     if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3552         if ((contents == 0             &&
3553              read_exec_only == 1       &&
3554              seg_32bit == 0            &&
3555              limit_in_pages == 0       &&
3556              seg_not_present == 1      &&
3557              useable == 0 )) {
3558             entry_1 = 0;
3559             entry_2 = 0;
3560             goto install;
3561         }
3562     }
3563 
3564     entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3565         (ldt_info.limit & 0x0ffff);
3566     entry_2 = (ldt_info.base_addr & 0xff000000) |
3567         ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3568         (ldt_info.limit & 0xf0000) |
3569         ((read_exec_only ^ 1) << 9) |
3570         (contents << 10) |
3571         ((seg_not_present ^ 1) << 15) |
3572         (seg_32bit << 22) |
3573         (limit_in_pages << 23) |
3574         (useable << 20) |
3575         (lm << 21) |
3576         0x7000;
3577 
3578     /* Install the new entry ...  */
3579 install:
3580     lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
3581     lp[0] = tswap32(entry_1);
3582     lp[1] = tswap32(entry_2);
3583     return 0;
3584 }
3585 
3586 static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
3587 {
3588     struct target_modify_ldt_ldt_s *target_ldt_info;
3589     uint64_t *gdt_table = g2h(env->gdt.base);
3590     uint32_t base_addr, limit, flags;
3591     int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
3592     int seg_not_present, useable, lm;
3593     uint32_t *lp, entry_1, entry_2;
3594 
3595     lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3596     if (!target_ldt_info)
3597         return -TARGET_EFAULT;
3598     idx = tswap32(target_ldt_info->entry_number);
3599     if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
3600         idx > TARGET_GDT_ENTRY_TLS_MAX) {
3601         unlock_user_struct(target_ldt_info, ptr, 1);
3602         return -TARGET_EINVAL;
3603     }
3604     lp = (uint32_t *)(gdt_table + idx);
3605     entry_1 = tswap32(lp[0]);
3606     entry_2 = tswap32(lp[1]);
3607 
3608     read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
3609     contents = (entry_2 >> 10) & 3;
3610     seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
3611     seg_32bit = (entry_2 >> 22) & 1;
3612     limit_in_pages = (entry_2 >> 23) & 1;
3613     useable = (entry_2 >> 20) & 1;
3614 #ifdef TARGET_ABI32
3615     lm = 0;
3616 #else
3617     lm = (entry_2 >> 21) & 1;
3618 #endif
3619     flags = (seg_32bit << 0) | (contents << 1) |
3620         (read_exec_only << 3) | (limit_in_pages << 4) |
3621         (seg_not_present << 5) | (useable << 6) | (lm << 7);
3622     limit = (entry_1 & 0xffff) | (entry_2  & 0xf0000);
3623     base_addr = (entry_1 >> 16) |
3624         (entry_2 & 0xff000000) |
3625         ((entry_2 & 0xff) << 16);
3626     target_ldt_info->base_addr = tswapl(base_addr);
3627     target_ldt_info->limit = tswap32(limit);
3628     target_ldt_info->flags = tswap32(flags);
3629     unlock_user_struct(target_ldt_info, ptr, 1);
3630     return 0;
3631 }
3632 #endif /* TARGET_I386 && TARGET_ABI32 */
3633 
3634 #ifndef TARGET_ABI32
3635 static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
3636 {
3637     abi_long ret;
3638     abi_ulong val;
3639     int idx;
3640 
3641     switch(code) {
3642     case TARGET_ARCH_SET_GS:
3643     case TARGET_ARCH_SET_FS:
3644         if (code == TARGET_ARCH_SET_GS)
3645             idx = R_GS;
3646         else
3647             idx = R_FS;
3648         cpu_x86_load_seg(env, idx, 0);
3649         env->segs[idx].base = addr;
3650         break;
3651     case TARGET_ARCH_GET_GS:
3652     case TARGET_ARCH_GET_FS:
3653         if (code == TARGET_ARCH_GET_GS)
3654             idx = R_GS;
3655         else
3656             idx = R_FS;
3657         val = env->segs[idx].base;
3658         if (put_user(val, addr, abi_ulong))
3659             return -TARGET_EFAULT;
3660         break;
3661     default:
3662         ret = -TARGET_EINVAL;
3663         break;
3664     }
3665     return 0;
3666 }
3667 #endif
3668 
3669 #endif /* defined(TARGET_I386) */
3670 
3671 #if defined(CONFIG_USE_NPTL)
3672 
3673 #define NEW_STACK_SIZE PTHREAD_STACK_MIN
3674 
3675 static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
3676 typedef struct {
3677     CPUState *env;
3678     pthread_mutex_t mutex;
3679     pthread_cond_t cond;
3680     pthread_t thread;
3681     uint32_t tid;
3682     abi_ulong child_tidptr;
3683     abi_ulong parent_tidptr;
3684     sigset_t sigmask;
3685 } new_thread_info;
3686 
3687 static void *clone_func(void *arg)
3688 {
3689     new_thread_info *info = arg;
3690     CPUState *env;
3691     TaskState *ts;
3692 
3693     env = info->env;
3694     thread_env = env;
3695     ts = (TaskState *)thread_env->opaque;
3696     info->tid = gettid();
3697     env->host_tid = info->tid;
3698     task_settid(ts);
3699     if (info->child_tidptr)
3700         put_user_u32(info->tid, info->child_tidptr);
3701     if (info->parent_tidptr)
3702         put_user_u32(info->tid, info->parent_tidptr);
3703     /* Enable signals.  */
3704     sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
3705     /* Signal to the parent that we're ready.  */
3706     pthread_mutex_lock(&info->mutex);
3707     pthread_cond_broadcast(&info->cond);
3708     pthread_mutex_unlock(&info->mutex);
3709     /* Wait until the parent has finshed initializing the tls state.  */
3710     pthread_mutex_lock(&clone_lock);
3711     pthread_mutex_unlock(&clone_lock);
3712     cpu_loop(env);
3713     /* never exits */
3714     return NULL;
3715 }
3716 #else
3717 /* this stack is the equivalent of the kernel stack associated with a
3718    thread/process */
3719 #define NEW_STACK_SIZE 8192
3720 
3721 static int clone_func(void *arg)
3722 {
3723     CPUState *env = arg;
3724     cpu_loop(env);
3725     /* never exits */
3726     return 0;
3727 }
3728 #endif
3729 
3730 /* do_fork() Must return host values and target errnos (unlike most
3731    do_*() functions). */
3732 static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,
3733                    abi_ulong parent_tidptr, target_ulong newtls,
3734                    abi_ulong child_tidptr)
3735 {
3736     int ret;
3737     TaskState *ts;
3738     CPUState *new_env;
3739 #if defined(CONFIG_USE_NPTL)
3740     unsigned int nptl_flags;
3741     sigset_t sigmask;
3742 #else
3743     uint8_t *new_stack;
3744 #endif
3745 
3746     /* Emulate vfork() with fork() */
3747     if (flags & CLONE_VFORK)
3748         flags &= ~(CLONE_VFORK | CLONE_VM);
3749 
3750     if (flags & CLONE_VM) {
3751         TaskState *parent_ts = (TaskState *)env->opaque;
3752 #if defined(CONFIG_USE_NPTL)
3753         new_thread_info info;
3754         pthread_attr_t attr;
3755 #endif
3756         ts = qemu_mallocz(sizeof(TaskState));
3757         init_task_state(ts);
3758         /* we create a new CPU instance. */
3759         new_env = cpu_copy(env);
3760 #if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC)
3761         cpu_reset(new_env);
3762 #endif
3763         /* Init regs that differ from the parent.  */
3764         cpu_clone_regs(new_env, newsp);
3765         new_env->opaque = ts;
3766         ts->bprm = parent_ts->bprm;
3767         ts->info = parent_ts->info;
3768 #if defined(CONFIG_USE_NPTL)
3769         nptl_flags = flags;
3770         flags &= ~CLONE_NPTL_FLAGS2;
3771 
3772         if (nptl_flags & CLONE_CHILD_CLEARTID) {
3773             ts->child_tidptr = child_tidptr;
3774         }
3775 
3776         if (nptl_flags & CLONE_SETTLS)
3777             cpu_set_tls (new_env, newtls);
3778 
3779         /* Grab a mutex so that thread setup appears atomic.  */
3780         pthread_mutex_lock(&clone_lock);
3781 
3782         memset(&info, 0, sizeof(info));
3783         pthread_mutex_init(&info.mutex, NULL);
3784         pthread_mutex_lock(&info.mutex);
3785         pthread_cond_init(&info.cond, NULL);
3786         info.env = new_env;
3787         if (nptl_flags & CLONE_CHILD_SETTID)
3788             info.child_tidptr = child_tidptr;
3789         if (nptl_flags & CLONE_PARENT_SETTID)
3790             info.parent_tidptr = parent_tidptr;
3791 
3792         ret = pthread_attr_init(&attr);
3793         ret = pthread_attr_setstacksize(&attr, NEW_STACK_SIZE);
3794         ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
3795         /* It is not safe to deliver signals until the child has finished
3796            initializing, so temporarily block all signals.  */
3797         sigfillset(&sigmask);
3798         sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
3799 
3800         ret = pthread_create(&info.thread, &attr, clone_func, &info);
3801         /* TODO: Free new CPU state if thread creation failed.  */
3802 
3803         sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
3804         pthread_attr_destroy(&attr);
3805         if (ret == 0) {
3806             /* Wait for the child to initialize.  */
3807             pthread_cond_wait(&info.cond, &info.mutex);
3808             ret = info.tid;
3809             if (flags & CLONE_PARENT_SETTID)
3810                 put_user_u32(ret, parent_tidptr);
3811         } else {
3812             ret = -1;
3813         }
3814         pthread_mutex_unlock(&info.mutex);
3815         pthread_cond_destroy(&info.cond);
3816         pthread_mutex_destroy(&info.mutex);
3817         pthread_mutex_unlock(&clone_lock);
3818 #else
3819         if (flags & CLONE_NPTL_FLAGS2)
3820             return -EINVAL;
3821         /* This is probably going to die very quickly, but do it anyway.  */
3822         new_stack = qemu_mallocz (NEW_STACK_SIZE);
3823 #ifdef __ia64__
3824         ret = __clone2(clone_func, new_stack, NEW_STACK_SIZE, flags, new_env);
3825 #else
3826 	ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
3827 #endif
3828 #endif
3829     } else {
3830         /* if no CLONE_VM, we consider it is a fork */
3831         if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
3832             return -EINVAL;
3833         fork_start();
3834         ret = fork();
3835         if (ret == 0) {
3836             /* Child Process.  */
3837             cpu_clone_regs(env, newsp);
3838             fork_end(1);
3839 #if defined(CONFIG_USE_NPTL)
3840             /* There is a race condition here.  The parent process could
3841                theoretically read the TID in the child process before the child
3842                tid is set.  This would require using either ptrace
3843                (not implemented) or having *_tidptr to point at a shared memory
3844                mapping.  We can't repeat the spinlock hack used above because
3845                the child process gets its own copy of the lock.  */
3846             if (flags & CLONE_CHILD_SETTID)
3847                 put_user_u32(gettid(), child_tidptr);
3848             if (flags & CLONE_PARENT_SETTID)
3849                 put_user_u32(gettid(), parent_tidptr);
3850             ts = (TaskState *)env->opaque;
3851             if (flags & CLONE_SETTLS)
3852                 cpu_set_tls (env, newtls);
3853             if (flags & CLONE_CHILD_CLEARTID)
3854                 ts->child_tidptr = child_tidptr;
3855 #endif
3856         } else {
3857             fork_end(0);
3858         }
3859     }
3860     return ret;
3861 }
3862 
3863 /* warning : doesn't handle linux specific flags... */
3864 static int target_to_host_fcntl_cmd(int cmd)
3865 {
3866     switch(cmd) {
3867 	case TARGET_F_DUPFD:
3868 	case TARGET_F_GETFD:
3869 	case TARGET_F_SETFD:
3870 	case TARGET_F_GETFL:
3871 	case TARGET_F_SETFL:
3872             return cmd;
3873         case TARGET_F_GETLK:
3874 	    return F_GETLK;
3875 	case TARGET_F_SETLK:
3876 	    return F_SETLK;
3877 	case TARGET_F_SETLKW:
3878 	    return F_SETLKW;
3879 	case TARGET_F_GETOWN:
3880 	    return F_GETOWN;
3881 	case TARGET_F_SETOWN:
3882 	    return F_SETOWN;
3883 	case TARGET_F_GETSIG:
3884 	    return F_GETSIG;
3885 	case TARGET_F_SETSIG:
3886 	    return F_SETSIG;
3887 #if TARGET_ABI_BITS == 32
3888         case TARGET_F_GETLK64:
3889 	    return F_GETLK64;
3890 	case TARGET_F_SETLK64:
3891 	    return F_SETLK64;
3892 	case TARGET_F_SETLKW64:
3893 	    return F_SETLKW64;
3894 #endif
3895         case TARGET_F_SETLEASE:
3896             return F_SETLEASE;
3897         case TARGET_F_GETLEASE:
3898             return F_GETLEASE;
3899 #ifdef F_DUPFD_CLOEXEC
3900         case TARGET_F_DUPFD_CLOEXEC:
3901             return F_DUPFD_CLOEXEC;
3902 #endif
3903         case TARGET_F_NOTIFY:
3904             return F_NOTIFY;
3905 	default:
3906             return -TARGET_EINVAL;
3907     }
3908     return -TARGET_EINVAL;
3909 }
3910 
3911 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
3912 {
3913     struct flock fl;
3914     struct target_flock *target_fl;
3915     struct flock64 fl64;
3916     struct target_flock64 *target_fl64;
3917     abi_long ret;
3918     int host_cmd = target_to_host_fcntl_cmd(cmd);
3919 
3920     if (host_cmd == -TARGET_EINVAL)
3921 	    return host_cmd;
3922 
3923     switch(cmd) {
3924     case TARGET_F_GETLK:
3925         if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3926             return -TARGET_EFAULT;
3927         fl.l_type = tswap16(target_fl->l_type);
3928         fl.l_whence = tswap16(target_fl->l_whence);
3929         fl.l_start = tswapl(target_fl->l_start);
3930         fl.l_len = tswapl(target_fl->l_len);
3931         fl.l_pid = tswap32(target_fl->l_pid);
3932         unlock_user_struct(target_fl, arg, 0);
3933         ret = get_errno(fcntl(fd, host_cmd, &fl));
3934         if (ret == 0) {
3935             if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
3936                 return -TARGET_EFAULT;
3937             target_fl->l_type = tswap16(fl.l_type);
3938             target_fl->l_whence = tswap16(fl.l_whence);
3939             target_fl->l_start = tswapl(fl.l_start);
3940             target_fl->l_len = tswapl(fl.l_len);
3941             target_fl->l_pid = tswap32(fl.l_pid);
3942             unlock_user_struct(target_fl, arg, 1);
3943         }
3944         break;
3945 
3946     case TARGET_F_SETLK:
3947     case TARGET_F_SETLKW:
3948         if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3949             return -TARGET_EFAULT;
3950         fl.l_type = tswap16(target_fl->l_type);
3951         fl.l_whence = tswap16(target_fl->l_whence);
3952         fl.l_start = tswapl(target_fl->l_start);
3953         fl.l_len = tswapl(target_fl->l_len);
3954         fl.l_pid = tswap32(target_fl->l_pid);
3955         unlock_user_struct(target_fl, arg, 0);
3956         ret = get_errno(fcntl(fd, host_cmd, &fl));
3957         break;
3958 
3959     case TARGET_F_GETLK64:
3960         if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3961             return -TARGET_EFAULT;
3962         fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3963         fl64.l_whence = tswap16(target_fl64->l_whence);
3964         fl64.l_start = tswapl(target_fl64->l_start);
3965         fl64.l_len = tswapl(target_fl64->l_len);
3966         fl64.l_pid = tswap32(target_fl64->l_pid);
3967         unlock_user_struct(target_fl64, arg, 0);
3968         ret = get_errno(fcntl(fd, host_cmd, &fl64));
3969         if (ret == 0) {
3970             if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
3971                 return -TARGET_EFAULT;
3972             target_fl64->l_type = tswap16(fl64.l_type) >> 1;
3973             target_fl64->l_whence = tswap16(fl64.l_whence);
3974             target_fl64->l_start = tswapl(fl64.l_start);
3975             target_fl64->l_len = tswapl(fl64.l_len);
3976             target_fl64->l_pid = tswap32(fl64.l_pid);
3977             unlock_user_struct(target_fl64, arg, 1);
3978         }
3979         break;
3980     case TARGET_F_SETLK64:
3981     case TARGET_F_SETLKW64:
3982         if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3983             return -TARGET_EFAULT;
3984         fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3985         fl64.l_whence = tswap16(target_fl64->l_whence);
3986         fl64.l_start = tswapl(target_fl64->l_start);
3987         fl64.l_len = tswapl(target_fl64->l_len);
3988         fl64.l_pid = tswap32(target_fl64->l_pid);
3989         unlock_user_struct(target_fl64, arg, 0);
3990         ret = get_errno(fcntl(fd, host_cmd, &fl64));
3991         break;
3992 
3993     case TARGET_F_GETFL:
3994         ret = get_errno(fcntl(fd, host_cmd, arg));
3995         if (ret >= 0) {
3996             ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
3997         }
3998         break;
3999 
4000     case TARGET_F_SETFL:
4001         ret = get_errno(fcntl(fd, host_cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
4002         break;
4003 
4004     case TARGET_F_SETOWN:
4005     case TARGET_F_GETOWN:
4006     case TARGET_F_SETSIG:
4007     case TARGET_F_GETSIG:
4008     case TARGET_F_SETLEASE:
4009     case TARGET_F_GETLEASE:
4010         ret = get_errno(fcntl(fd, host_cmd, arg));
4011         break;
4012 
4013     default:
4014         ret = get_errno(fcntl(fd, cmd, arg));
4015         break;
4016     }
4017     return ret;
4018 }
4019 
4020 #ifdef USE_UID16
4021 
4022 static inline int high2lowuid(int uid)
4023 {
4024     if (uid > 65535)
4025         return 65534;
4026     else
4027         return uid;
4028 }
4029 
4030 static inline int high2lowgid(int gid)
4031 {
4032     if (gid > 65535)
4033         return 65534;
4034     else
4035         return gid;
4036 }
4037 
4038 static inline int low2highuid(int uid)
4039 {
4040     if ((int16_t)uid == -1)
4041         return -1;
4042     else
4043         return uid;
4044 }
4045 
4046 static inline int low2highgid(int gid)
4047 {
4048     if ((int16_t)gid == -1)
4049         return -1;
4050     else
4051         return gid;
4052 }
4053 
4054 #endif /* USE_UID16 */
4055 
4056 void syscall_init(void)
4057 {
4058     IOCTLEntry *ie;
4059     const argtype *arg_type;
4060     int size;
4061     int i;
4062 
4063 #define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
4064 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
4065 #include "syscall_types.h"
4066 #undef STRUCT
4067 #undef STRUCT_SPECIAL
4068 
4069     /* we patch the ioctl size if necessary. We rely on the fact that
4070        no ioctl has all the bits at '1' in the size field */
4071     ie = ioctl_entries;
4072     while (ie->target_cmd != 0) {
4073         if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
4074             TARGET_IOC_SIZEMASK) {
4075             arg_type = ie->arg_type;
4076             if (arg_type[0] != TYPE_PTR) {
4077                 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
4078                         ie->target_cmd);
4079                 exit(1);
4080             }
4081             arg_type++;
4082             size = thunk_type_size(arg_type, 0);
4083             ie->target_cmd = (ie->target_cmd &
4084                               ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
4085                 (size << TARGET_IOC_SIZESHIFT);
4086         }
4087 
4088         /* Build target_to_host_errno_table[] table from
4089          * host_to_target_errno_table[]. */
4090         for (i=0; i < ERRNO_TABLE_SIZE; i++)
4091                 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
4092 
4093         /* automatic consistency check if same arch */
4094 #if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
4095     (defined(__x86_64__) && defined(TARGET_X86_64))
4096         if (unlikely(ie->target_cmd != ie->host_cmd)) {
4097             fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
4098                     ie->name, ie->target_cmd, ie->host_cmd);
4099         }
4100 #endif
4101         ie++;
4102     }
4103 }
4104 
4105 #if TARGET_ABI_BITS == 32
4106 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
4107 {
4108 #ifdef TARGET_WORDS_BIGENDIAN
4109     return ((uint64_t)word0 << 32) | word1;
4110 #else
4111     return ((uint64_t)word1 << 32) | word0;
4112 #endif
4113 }
4114 #else /* TARGET_ABI_BITS == 32 */
4115 static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
4116 {
4117     return word0;
4118 }
4119 #endif /* TARGET_ABI_BITS != 32 */
4120 
4121 #ifdef TARGET_NR_truncate64
4122 static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
4123                                          abi_long arg2,
4124                                          abi_long arg3,
4125                                          abi_long arg4)
4126 {
4127 #ifdef TARGET_ARM
4128     if (((CPUARMState *)cpu_env)->eabi)
4129       {
4130         arg2 = arg3;
4131         arg3 = arg4;
4132       }
4133 #endif
4134     return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
4135 }
4136 #endif
4137 
4138 #ifdef TARGET_NR_ftruncate64
4139 static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
4140                                           abi_long arg2,
4141                                           abi_long arg3,
4142                                           abi_long arg4)
4143 {
4144 #ifdef TARGET_ARM
4145     if (((CPUARMState *)cpu_env)->eabi)
4146       {
4147         arg2 = arg3;
4148         arg3 = arg4;
4149       }
4150 #endif
4151     return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
4152 }
4153 #endif
4154 
4155 static inline abi_long target_to_host_timespec(struct timespec *host_ts,
4156                                                abi_ulong target_addr)
4157 {
4158     struct target_timespec *target_ts;
4159 
4160     if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
4161         return -TARGET_EFAULT;
4162     host_ts->tv_sec = tswapl(target_ts->tv_sec);
4163     host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
4164     unlock_user_struct(target_ts, target_addr, 0);
4165     return 0;
4166 }
4167 
4168 static inline abi_long host_to_target_timespec(abi_ulong target_addr,
4169                                                struct timespec *host_ts)
4170 {
4171     struct target_timespec *target_ts;
4172 
4173     if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
4174         return -TARGET_EFAULT;
4175     target_ts->tv_sec = tswapl(host_ts->tv_sec);
4176     target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
4177     unlock_user_struct(target_ts, target_addr, 1);
4178     return 0;
4179 }
4180 
4181 #if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
4182 static inline abi_long host_to_target_stat64(void *cpu_env,
4183                                              abi_ulong target_addr,
4184                                              struct stat *host_st)
4185 {
4186 #ifdef TARGET_ARM
4187     if (((CPUARMState *)cpu_env)->eabi) {
4188         struct target_eabi_stat64 *target_st;
4189 
4190         if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4191             return -TARGET_EFAULT;
4192         memset(target_st, 0, sizeof(struct target_eabi_stat64));
4193         __put_user(host_st->st_dev, &target_st->st_dev);
4194         __put_user(host_st->st_ino, &target_st->st_ino);
4195 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4196         __put_user(host_st->st_ino, &target_st->__st_ino);
4197 #endif
4198         __put_user(host_st->st_mode, &target_st->st_mode);
4199         __put_user(host_st->st_nlink, &target_st->st_nlink);
4200         __put_user(host_st->st_uid, &target_st->st_uid);
4201         __put_user(host_st->st_gid, &target_st->st_gid);
4202         __put_user(host_st->st_rdev, &target_st->st_rdev);
4203         __put_user(host_st->st_size, &target_st->st_size);
4204         __put_user(host_st->st_blksize, &target_st->st_blksize);
4205         __put_user(host_st->st_blocks, &target_st->st_blocks);
4206         __put_user(host_st->st_atime, &target_st->target_st_atime);
4207         __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4208         __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4209         unlock_user_struct(target_st, target_addr, 1);
4210     } else
4211 #endif
4212     {
4213 #if TARGET_ABI_BITS == 64 && !defined(TARGET_ALPHA)
4214         struct target_stat *target_st;
4215 #else
4216         struct target_stat64 *target_st;
4217 #endif
4218 
4219         if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
4220             return -TARGET_EFAULT;
4221         memset(target_st, 0, sizeof(*target_st));
4222         __put_user(host_st->st_dev, &target_st->st_dev);
4223         __put_user(host_st->st_ino, &target_st->st_ino);
4224 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4225         __put_user(host_st->st_ino, &target_st->__st_ino);
4226 #endif
4227         __put_user(host_st->st_mode, &target_st->st_mode);
4228         __put_user(host_st->st_nlink, &target_st->st_nlink);
4229         __put_user(host_st->st_uid, &target_st->st_uid);
4230         __put_user(host_st->st_gid, &target_st->st_gid);
4231         __put_user(host_st->st_rdev, &target_st->st_rdev);
4232         /* XXX: better use of kernel struct */
4233         __put_user(host_st->st_size, &target_st->st_size);
4234         __put_user(host_st->st_blksize, &target_st->st_blksize);
4235         __put_user(host_st->st_blocks, &target_st->st_blocks);
4236         __put_user(host_st->st_atime, &target_st->target_st_atime);
4237         __put_user(host_st->st_mtime, &target_st->target_st_mtime);
4238         __put_user(host_st->st_ctime, &target_st->target_st_ctime);
4239         unlock_user_struct(target_st, target_addr, 1);
4240     }
4241 
4242     return 0;
4243 }
4244 #endif
4245 
4246 #if defined(CONFIG_USE_NPTL)
4247 /* ??? Using host futex calls even when target atomic operations
4248    are not really atomic probably breaks things.  However implementing
4249    futexes locally would make futexes shared between multiple processes
4250    tricky.  However they're probably useless because guest atomic
4251    operations won't work either.  */
4252 static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
4253                     target_ulong uaddr2, int val3)
4254 {
4255     struct timespec ts, *pts;
4256     int base_op;
4257 
4258     /* ??? We assume FUTEX_* constants are the same on both host
4259        and target.  */
4260 #ifdef FUTEX_CMD_MASK
4261     base_op = op & FUTEX_CMD_MASK;
4262 #else
4263     base_op = op;
4264 #endif
4265     switch (base_op) {
4266     case FUTEX_WAIT:
4267         if (timeout) {
4268             pts = &ts;
4269             target_to_host_timespec(pts, timeout);
4270         } else {
4271             pts = NULL;
4272         }
4273         return get_errno(sys_futex(g2h(uaddr), op, tswap32(val),
4274                          pts, NULL, 0));
4275     case FUTEX_WAKE:
4276         return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
4277     case FUTEX_FD:
4278         return get_errno(sys_futex(g2h(uaddr), op, val, NULL, NULL, 0));
4279     case FUTEX_REQUEUE:
4280     case FUTEX_CMP_REQUEUE:
4281     case FUTEX_WAKE_OP:
4282         /* For FUTEX_REQUEUE, FUTEX_CMP_REQUEUE, and FUTEX_WAKE_OP, the
4283            TIMEOUT parameter is interpreted as a uint32_t by the kernel.
4284            But the prototype takes a `struct timespec *'; insert casts
4285            to satisfy the compiler.  We do not need to tswap TIMEOUT
4286            since it's not compared to guest memory.  */
4287         pts = (struct timespec *)(uintptr_t) timeout;
4288         return get_errno(sys_futex(g2h(uaddr), op, val, pts,
4289                                    g2h(uaddr2),
4290                                    (base_op == FUTEX_CMP_REQUEUE
4291                                     ? tswap32(val3)
4292                                     : val3)));
4293     default:
4294         return -TARGET_ENOSYS;
4295     }
4296 }
4297 #endif
4298 
4299 /* Map host to target signal numbers for the wait family of syscalls.
4300    Assume all other status bits are the same.  */
4301 static int host_to_target_waitstatus(int status)
4302 {
4303     if (WIFSIGNALED(status)) {
4304         return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
4305     }
4306     if (WIFSTOPPED(status)) {
4307         return (host_to_target_signal(WSTOPSIG(status)) << 8)
4308                | (status & 0xff);
4309     }
4310     return status;
4311 }
4312 
4313 int get_osversion(void)
4314 {
4315     static int osversion;
4316     struct new_utsname buf;
4317     const char *s;
4318     int i, n, tmp;
4319     if (osversion)
4320         return osversion;
4321     if (qemu_uname_release && *qemu_uname_release) {
4322         s = qemu_uname_release;
4323     } else {
4324         if (sys_uname(&buf))
4325             return 0;
4326         s = buf.release;
4327     }
4328     tmp = 0;
4329     for (i = 0; i < 3; i++) {
4330         n = 0;
4331         while (*s >= '0' && *s <= '9') {
4332             n *= 10;
4333             n += *s - '0';
4334             s++;
4335         }
4336         tmp = (tmp << 8) + n;
4337         if (*s == '.')
4338             s++;
4339     }
4340     osversion = tmp;
4341     return osversion;
4342 }
4343 
4344 /* do_syscall() should always have a single exit point at the end so
4345    that actions, such as logging of syscall results, can be performed.
4346    All errnos that do_syscall() returns must be -TARGET_<errcode>. */
4347 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
4348                     abi_long arg2, abi_long arg3, abi_long arg4,
4349                     abi_long arg5, abi_long arg6)
4350 {
4351     abi_long ret;
4352     struct stat st;
4353     struct statfs stfs;
4354     void *p;
4355 
4356 #ifdef DEBUG
4357     gemu_log("syscall %d", num);
4358 #endif
4359     if(do_strace)
4360         print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
4361 
4362     switch(num) {
4363     case TARGET_NR_exit:
4364 #ifdef CONFIG_USE_NPTL
4365       /* In old applications this may be used to implement _exit(2).
4366          However in threaded applictions it is used for thread termination,
4367          and _exit_group is used for application termination.
4368          Do thread termination if we have more then one thread.  */
4369       /* FIXME: This probably breaks if a signal arrives.  We should probably
4370          be disabling signals.  */
4371       if (first_cpu->next_cpu) {
4372           TaskState *ts;
4373           CPUState **lastp;
4374           CPUState *p;
4375 
4376           cpu_list_lock();
4377           lastp = &first_cpu;
4378           p = first_cpu;
4379           while (p && p != (CPUState *)cpu_env) {
4380               lastp = &p->next_cpu;
4381               p = p->next_cpu;
4382           }
4383           /* If we didn't find the CPU for this thread then something is
4384              horribly wrong.  */
4385           if (!p)
4386               abort();
4387           /* Remove the CPU from the list.  */
4388           *lastp = p->next_cpu;
4389           cpu_list_unlock();
4390           ts = ((CPUState *)cpu_env)->opaque;
4391           if (ts->child_tidptr) {
4392               put_user_u32(0, ts->child_tidptr);
4393               sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
4394                         NULL, NULL, 0);
4395           }
4396           thread_env = NULL;
4397           qemu_free(cpu_env);
4398           qemu_free(ts);
4399           pthread_exit(NULL);
4400       }
4401 #endif
4402 #ifdef TARGET_GPROF
4403         _mcleanup();
4404 #endif
4405         gdb_exit(cpu_env, arg1);
4406         _exit(arg1);
4407         ret = 0; /* avoid warning */
4408         break;
4409     case TARGET_NR_read:
4410         if (arg3 == 0)
4411             ret = 0;
4412         else {
4413             if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4414                 goto efault;
4415             ret = get_errno(read(arg1, p, arg3));
4416             unlock_user(p, arg2, ret);
4417         }
4418         break;
4419     case TARGET_NR_write:
4420         if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4421             goto efault;
4422         ret = get_errno(write(arg1, p, arg3));
4423         unlock_user(p, arg2, 0);
4424         break;
4425     case TARGET_NR_open:
4426         if (!(p = lock_user_string(arg1)))
4427             goto efault;
4428         ret = get_errno(open(path(p),
4429                              target_to_host_bitmask(arg2, fcntl_flags_tbl),
4430                              arg3));
4431         unlock_user(p, arg1, 0);
4432         break;
4433 #if defined(TARGET_NR_openat) && defined(__NR_openat)
4434     case TARGET_NR_openat:
4435         if (!(p = lock_user_string(arg2)))
4436             goto efault;
4437         ret = get_errno(sys_openat(arg1,
4438                                    path(p),
4439                                    target_to_host_bitmask(arg3, fcntl_flags_tbl),
4440                                    arg4));
4441         unlock_user(p, arg2, 0);
4442         break;
4443 #endif
4444     case TARGET_NR_close:
4445         ret = get_errno(close(arg1));
4446         break;
4447     case TARGET_NR_brk:
4448         ret = do_brk(arg1);
4449         break;
4450     case TARGET_NR_fork:
4451         ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
4452         break;
4453 #ifdef TARGET_NR_waitpid
4454     case TARGET_NR_waitpid:
4455         {
4456             int status;
4457             ret = get_errno(waitpid(arg1, &status, arg3));
4458             if (!is_error(ret) && arg2
4459                 && put_user_s32(host_to_target_waitstatus(status), arg2))
4460                 goto efault;
4461         }
4462         break;
4463 #endif
4464 #ifdef TARGET_NR_waitid
4465     case TARGET_NR_waitid:
4466         {
4467             siginfo_t info;
4468             info.si_pid = 0;
4469             ret = get_errno(waitid(arg1, arg2, &info, arg4));
4470             if (!is_error(ret) && arg3 && info.si_pid != 0) {
4471                 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
4472                     goto efault;
4473                 host_to_target_siginfo(p, &info);
4474                 unlock_user(p, arg3, sizeof(target_siginfo_t));
4475             }
4476         }
4477         break;
4478 #endif
4479 #ifdef TARGET_NR_creat /* not on alpha */
4480     case TARGET_NR_creat:
4481         if (!(p = lock_user_string(arg1)))
4482             goto efault;
4483         ret = get_errno(creat(p, arg2));
4484         unlock_user(p, arg1, 0);
4485         break;
4486 #endif
4487     case TARGET_NR_link:
4488         {
4489             void * p2;
4490             p = lock_user_string(arg1);
4491             p2 = lock_user_string(arg2);
4492             if (!p || !p2)
4493                 ret = -TARGET_EFAULT;
4494             else
4495                 ret = get_errno(link(p, p2));
4496             unlock_user(p2, arg2, 0);
4497             unlock_user(p, arg1, 0);
4498         }
4499         break;
4500 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
4501     case TARGET_NR_linkat:
4502         {
4503             void * p2 = NULL;
4504             if (!arg2 || !arg4)
4505                 goto efault;
4506             p  = lock_user_string(arg2);
4507             p2 = lock_user_string(arg4);
4508             if (!p || !p2)
4509                 ret = -TARGET_EFAULT;
4510             else
4511                 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
4512             unlock_user(p, arg2, 0);
4513             unlock_user(p2, arg4, 0);
4514         }
4515         break;
4516 #endif
4517     case TARGET_NR_unlink:
4518         if (!(p = lock_user_string(arg1)))
4519             goto efault;
4520         ret = get_errno(unlink(p));
4521         unlock_user(p, arg1, 0);
4522         break;
4523 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
4524     case TARGET_NR_unlinkat:
4525         if (!(p = lock_user_string(arg2)))
4526             goto efault;
4527         ret = get_errno(sys_unlinkat(arg1, p, arg3));
4528         unlock_user(p, arg2, 0);
4529         break;
4530 #endif
4531     case TARGET_NR_execve:
4532         {
4533             char **argp, **envp;
4534             int argc, envc;
4535             abi_ulong gp;
4536             abi_ulong guest_argp;
4537             abi_ulong guest_envp;
4538             abi_ulong addr;
4539             char **q;
4540 
4541             argc = 0;
4542             guest_argp = arg2;
4543             for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
4544                 if (get_user_ual(addr, gp))
4545                     goto efault;
4546                 if (!addr)
4547                     break;
4548                 argc++;
4549             }
4550             envc = 0;
4551             guest_envp = arg3;
4552             for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
4553                 if (get_user_ual(addr, gp))
4554                     goto efault;
4555                 if (!addr)
4556                     break;
4557                 envc++;
4558             }
4559 
4560             argp = alloca((argc + 1) * sizeof(void *));
4561             envp = alloca((envc + 1) * sizeof(void *));
4562 
4563             for (gp = guest_argp, q = argp; gp;
4564                   gp += sizeof(abi_ulong), q++) {
4565                 if (get_user_ual(addr, gp))
4566                     goto execve_efault;
4567                 if (!addr)
4568                     break;
4569                 if (!(*q = lock_user_string(addr)))
4570                     goto execve_efault;
4571             }
4572             *q = NULL;
4573 
4574             for (gp = guest_envp, q = envp; gp;
4575                   gp += sizeof(abi_ulong), q++) {
4576                 if (get_user_ual(addr, gp))
4577                     goto execve_efault;
4578                 if (!addr)
4579                     break;
4580                 if (!(*q = lock_user_string(addr)))
4581                     goto execve_efault;
4582             }
4583             *q = NULL;
4584 
4585             if (!(p = lock_user_string(arg1)))
4586                 goto execve_efault;
4587             ret = get_errno(execve(p, argp, envp));
4588             unlock_user(p, arg1, 0);
4589 
4590             goto execve_end;
4591 
4592         execve_efault:
4593             ret = -TARGET_EFAULT;
4594 
4595         execve_end:
4596             for (gp = guest_argp, q = argp; *q;
4597                   gp += sizeof(abi_ulong), q++) {
4598                 if (get_user_ual(addr, gp)
4599                     || !addr)
4600                     break;
4601                 unlock_user(*q, addr, 0);
4602             }
4603             for (gp = guest_envp, q = envp; *q;
4604                   gp += sizeof(abi_ulong), q++) {
4605                 if (get_user_ual(addr, gp)
4606                     || !addr)
4607                     break;
4608                 unlock_user(*q, addr, 0);
4609             }
4610         }
4611         break;
4612     case TARGET_NR_chdir:
4613         if (!(p = lock_user_string(arg1)))
4614             goto efault;
4615         ret = get_errno(chdir(p));
4616         unlock_user(p, arg1, 0);
4617         break;
4618 #ifdef TARGET_NR_time
4619     case TARGET_NR_time:
4620         {
4621             time_t host_time;
4622             ret = get_errno(time(&host_time));
4623             if (!is_error(ret)
4624                 && arg1
4625                 && put_user_sal(host_time, arg1))
4626                 goto efault;
4627         }
4628         break;
4629 #endif
4630     case TARGET_NR_mknod:
4631         if (!(p = lock_user_string(arg1)))
4632             goto efault;
4633         ret = get_errno(mknod(p, arg2, arg3));
4634         unlock_user(p, arg1, 0);
4635         break;
4636 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
4637     case TARGET_NR_mknodat:
4638         if (!(p = lock_user_string(arg2)))
4639             goto efault;
4640         ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
4641         unlock_user(p, arg2, 0);
4642         break;
4643 #endif
4644     case TARGET_NR_chmod:
4645         if (!(p = lock_user_string(arg1)))
4646             goto efault;
4647         ret = get_errno(chmod(p, arg2));
4648         unlock_user(p, arg1, 0);
4649         break;
4650 #ifdef TARGET_NR_break
4651     case TARGET_NR_break:
4652         goto unimplemented;
4653 #endif
4654 #ifdef TARGET_NR_oldstat
4655     case TARGET_NR_oldstat:
4656         goto unimplemented;
4657 #endif
4658     case TARGET_NR_lseek:
4659         ret = get_errno(lseek(arg1, arg2, arg3));
4660         break;
4661 #if defined(TARGET_NR_getxpid) && defined(TARGET_ALPHA)
4662     /* Alpha specific */
4663     case TARGET_NR_getxpid:
4664         ((CPUAlphaState *)cpu_env)->ir[IR_A4] = getppid();
4665         ret = get_errno(getpid());
4666         break;
4667 #endif
4668 #ifdef TARGET_NR_getpid
4669     case TARGET_NR_getpid:
4670         ret = get_errno(getpid());
4671         break;
4672 #endif
4673     case TARGET_NR_mount:
4674 		{
4675 			/* need to look at the data field */
4676 			void *p2, *p3;
4677 			p = lock_user_string(arg1);
4678 			p2 = lock_user_string(arg2);
4679 			p3 = lock_user_string(arg3);
4680                         if (!p || !p2 || !p3)
4681                             ret = -TARGET_EFAULT;
4682                         else {
4683                             /* FIXME - arg5 should be locked, but it isn't clear how to
4684                              * do that since it's not guaranteed to be a NULL-terminated
4685                              * string.
4686                              */
4687                             if ( ! arg5 )
4688                                 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, NULL));
4689                             else
4690                                 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
4691                         }
4692                         unlock_user(p, arg1, 0);
4693                         unlock_user(p2, arg2, 0);
4694                         unlock_user(p3, arg3, 0);
4695 			break;
4696 		}
4697 #ifdef TARGET_NR_umount
4698     case TARGET_NR_umount:
4699         if (!(p = lock_user_string(arg1)))
4700             goto efault;
4701         ret = get_errno(umount(p));
4702         unlock_user(p, arg1, 0);
4703         break;
4704 #endif
4705 #ifdef TARGET_NR_stime /* not on alpha */
4706     case TARGET_NR_stime:
4707         {
4708             time_t host_time;
4709             if (get_user_sal(host_time, arg1))
4710                 goto efault;
4711             ret = get_errno(stime(&host_time));
4712         }
4713         break;
4714 #endif
4715     case TARGET_NR_ptrace:
4716         goto unimplemented;
4717 #ifdef TARGET_NR_alarm /* not on alpha */
4718     case TARGET_NR_alarm:
4719         ret = alarm(arg1);
4720         break;
4721 #endif
4722 #ifdef TARGET_NR_oldfstat
4723     case TARGET_NR_oldfstat:
4724         goto unimplemented;
4725 #endif
4726 #ifdef TARGET_NR_pause /* not on alpha */
4727     case TARGET_NR_pause:
4728         ret = get_errno(pause());
4729         break;
4730 #endif
4731 #ifdef TARGET_NR_utime
4732     case TARGET_NR_utime:
4733         {
4734             struct utimbuf tbuf, *host_tbuf;
4735             struct target_utimbuf *target_tbuf;
4736             if (arg2) {
4737                 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
4738                     goto efault;
4739                 tbuf.actime = tswapl(target_tbuf->actime);
4740                 tbuf.modtime = tswapl(target_tbuf->modtime);
4741                 unlock_user_struct(target_tbuf, arg2, 0);
4742                 host_tbuf = &tbuf;
4743             } else {
4744                 host_tbuf = NULL;
4745             }
4746             if (!(p = lock_user_string(arg1)))
4747                 goto efault;
4748             ret = get_errno(utime(p, host_tbuf));
4749             unlock_user(p, arg1, 0);
4750         }
4751         break;
4752 #endif
4753     case TARGET_NR_utimes:
4754         {
4755             struct timeval *tvp, tv[2];
4756             if (arg2) {
4757                 if (copy_from_user_timeval(&tv[0], arg2)
4758                     || copy_from_user_timeval(&tv[1],
4759                                               arg2 + sizeof(struct target_timeval)))
4760                     goto efault;
4761                 tvp = tv;
4762             } else {
4763                 tvp = NULL;
4764             }
4765             if (!(p = lock_user_string(arg1)))
4766                 goto efault;
4767             ret = get_errno(utimes(p, tvp));
4768             unlock_user(p, arg1, 0);
4769         }
4770         break;
4771 #if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
4772     case TARGET_NR_futimesat:
4773         {
4774             struct timeval *tvp, tv[2];
4775             if (arg3) {
4776                 if (copy_from_user_timeval(&tv[0], arg3)
4777                     || copy_from_user_timeval(&tv[1],
4778                                               arg3 + sizeof(struct target_timeval)))
4779                     goto efault;
4780                 tvp = tv;
4781             } else {
4782                 tvp = NULL;
4783             }
4784             if (!(p = lock_user_string(arg2)))
4785                 goto efault;
4786             ret = get_errno(sys_futimesat(arg1, path(p), tvp));
4787             unlock_user(p, arg2, 0);
4788         }
4789         break;
4790 #endif
4791 #ifdef TARGET_NR_stty
4792     case TARGET_NR_stty:
4793         goto unimplemented;
4794 #endif
4795 #ifdef TARGET_NR_gtty
4796     case TARGET_NR_gtty:
4797         goto unimplemented;
4798 #endif
4799     case TARGET_NR_access:
4800         if (!(p = lock_user_string(arg1)))
4801             goto efault;
4802         ret = get_errno(access(path(p), arg2));
4803         unlock_user(p, arg1, 0);
4804         break;
4805 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
4806     case TARGET_NR_faccessat:
4807         if (!(p = lock_user_string(arg2)))
4808             goto efault;
4809         ret = get_errno(sys_faccessat(arg1, p, arg3));
4810         unlock_user(p, arg2, 0);
4811         break;
4812 #endif
4813 #ifdef TARGET_NR_nice /* not on alpha */
4814     case TARGET_NR_nice:
4815         ret = get_errno(nice(arg1));
4816         break;
4817 #endif
4818 #ifdef TARGET_NR_ftime
4819     case TARGET_NR_ftime:
4820         goto unimplemented;
4821 #endif
4822     case TARGET_NR_sync:
4823         sync();
4824         ret = 0;
4825         break;
4826     case TARGET_NR_kill:
4827         ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
4828         break;
4829     case TARGET_NR_rename:
4830         {
4831             void *p2;
4832             p = lock_user_string(arg1);
4833             p2 = lock_user_string(arg2);
4834             if (!p || !p2)
4835                 ret = -TARGET_EFAULT;
4836             else
4837                 ret = get_errno(rename(p, p2));
4838             unlock_user(p2, arg2, 0);
4839             unlock_user(p, arg1, 0);
4840         }
4841         break;
4842 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
4843     case TARGET_NR_renameat:
4844         {
4845             void *p2;
4846             p  = lock_user_string(arg2);
4847             p2 = lock_user_string(arg4);
4848             if (!p || !p2)
4849                 ret = -TARGET_EFAULT;
4850             else
4851                 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
4852             unlock_user(p2, arg4, 0);
4853             unlock_user(p, arg2, 0);
4854         }
4855         break;
4856 #endif
4857     case TARGET_NR_mkdir:
4858         if (!(p = lock_user_string(arg1)))
4859             goto efault;
4860         ret = get_errno(mkdir(p, arg2));
4861         unlock_user(p, arg1, 0);
4862         break;
4863 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
4864     case TARGET_NR_mkdirat:
4865         if (!(p = lock_user_string(arg2)))
4866             goto efault;
4867         ret = get_errno(sys_mkdirat(arg1, p, arg3));
4868         unlock_user(p, arg2, 0);
4869         break;
4870 #endif
4871     case TARGET_NR_rmdir:
4872         if (!(p = lock_user_string(arg1)))
4873             goto efault;
4874         ret = get_errno(rmdir(p));
4875         unlock_user(p, arg1, 0);
4876         break;
4877     case TARGET_NR_dup:
4878         ret = get_errno(dup(arg1));
4879         break;
4880     case TARGET_NR_pipe:
4881         ret = do_pipe(cpu_env, arg1, 0, 0);
4882         break;
4883 #ifdef TARGET_NR_pipe2
4884     case TARGET_NR_pipe2:
4885         ret = do_pipe(cpu_env, arg1, arg2, 1);
4886         break;
4887 #endif
4888     case TARGET_NR_times:
4889         {
4890             struct target_tms *tmsp;
4891             struct tms tms;
4892             ret = get_errno(times(&tms));
4893             if (arg1) {
4894                 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
4895                 if (!tmsp)
4896                     goto efault;
4897                 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
4898                 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
4899                 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
4900                 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
4901             }
4902             if (!is_error(ret))
4903                 ret = host_to_target_clock_t(ret);
4904         }
4905         break;
4906 #ifdef TARGET_NR_prof
4907     case TARGET_NR_prof:
4908         goto unimplemented;
4909 #endif
4910 #ifdef TARGET_NR_signal
4911     case TARGET_NR_signal:
4912         goto unimplemented;
4913 #endif
4914     case TARGET_NR_acct:
4915         if (arg1 == 0) {
4916             ret = get_errno(acct(NULL));
4917         } else {
4918             if (!(p = lock_user_string(arg1)))
4919                 goto efault;
4920             ret = get_errno(acct(path(p)));
4921             unlock_user(p, arg1, 0);
4922         }
4923         break;
4924 #ifdef TARGET_NR_umount2 /* not on alpha */
4925     case TARGET_NR_umount2:
4926         if (!(p = lock_user_string(arg1)))
4927             goto efault;
4928         ret = get_errno(umount2(p, arg2));
4929         unlock_user(p, arg1, 0);
4930         break;
4931 #endif
4932 #ifdef TARGET_NR_lock
4933     case TARGET_NR_lock:
4934         goto unimplemented;
4935 #endif
4936     case TARGET_NR_ioctl:
4937         ret = do_ioctl(arg1, arg2, arg3);
4938         break;
4939     case TARGET_NR_fcntl:
4940         ret = do_fcntl(arg1, arg2, arg3);
4941         break;
4942 #ifdef TARGET_NR_mpx
4943     case TARGET_NR_mpx:
4944         goto unimplemented;
4945 #endif
4946     case TARGET_NR_setpgid:
4947         ret = get_errno(setpgid(arg1, arg2));
4948         break;
4949 #ifdef TARGET_NR_ulimit
4950     case TARGET_NR_ulimit:
4951         goto unimplemented;
4952 #endif
4953 #ifdef TARGET_NR_oldolduname
4954     case TARGET_NR_oldolduname:
4955         goto unimplemented;
4956 #endif
4957     case TARGET_NR_umask:
4958         ret = get_errno(umask(arg1));
4959         break;
4960     case TARGET_NR_chroot:
4961         if (!(p = lock_user_string(arg1)))
4962             goto efault;
4963         ret = get_errno(chroot(p));
4964         unlock_user(p, arg1, 0);
4965         break;
4966     case TARGET_NR_ustat:
4967         goto unimplemented;
4968     case TARGET_NR_dup2:
4969         ret = get_errno(dup2(arg1, arg2));
4970         break;
4971 #if defined(CONFIG_DUP3) && defined(TARGET_NR_dup3)
4972     case TARGET_NR_dup3:
4973         ret = get_errno(dup3(arg1, arg2, arg3));
4974         break;
4975 #endif
4976 #ifdef TARGET_NR_getppid /* not on alpha */
4977     case TARGET_NR_getppid:
4978         ret = get_errno(getppid());
4979         break;
4980 #endif
4981     case TARGET_NR_getpgrp:
4982         ret = get_errno(getpgrp());
4983         break;
4984     case TARGET_NR_setsid:
4985         ret = get_errno(setsid());
4986         break;
4987 #ifdef TARGET_NR_sigaction
4988     case TARGET_NR_sigaction:
4989         {
4990 #if defined(TARGET_ALPHA)
4991             struct target_sigaction act, oact, *pact = 0;
4992             struct target_old_sigaction *old_act;
4993             if (arg2) {
4994                 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4995                     goto efault;
4996                 act._sa_handler = old_act->_sa_handler;
4997                 target_siginitset(&act.sa_mask, old_act->sa_mask);
4998                 act.sa_flags = old_act->sa_flags;
4999                 act.sa_restorer = 0;
5000                 unlock_user_struct(old_act, arg2, 0);
5001                 pact = &act;
5002             }
5003             ret = get_errno(do_sigaction(arg1, pact, &oact));
5004             if (!is_error(ret) && arg3) {
5005                 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5006                     goto efault;
5007                 old_act->_sa_handler = oact._sa_handler;
5008                 old_act->sa_mask = oact.sa_mask.sig[0];
5009                 old_act->sa_flags = oact.sa_flags;
5010                 unlock_user_struct(old_act, arg3, 1);
5011             }
5012 #elif defined(TARGET_MIPS)
5013 	    struct target_sigaction act, oact, *pact, *old_act;
5014 
5015 	    if (arg2) {
5016                 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5017                     goto efault;
5018 		act._sa_handler = old_act->_sa_handler;
5019 		target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
5020 		act.sa_flags = old_act->sa_flags;
5021 		unlock_user_struct(old_act, arg2, 0);
5022 		pact = &act;
5023 	    } else {
5024 		pact = NULL;
5025 	    }
5026 
5027 	    ret = get_errno(do_sigaction(arg1, pact, &oact));
5028 
5029 	    if (!is_error(ret) && arg3) {
5030                 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5031                     goto efault;
5032 		old_act->_sa_handler = oact._sa_handler;
5033 		old_act->sa_flags = oact.sa_flags;
5034 		old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
5035 		old_act->sa_mask.sig[1] = 0;
5036 		old_act->sa_mask.sig[2] = 0;
5037 		old_act->sa_mask.sig[3] = 0;
5038 		unlock_user_struct(old_act, arg3, 1);
5039 	    }
5040 #else
5041             struct target_old_sigaction *old_act;
5042             struct target_sigaction act, oact, *pact;
5043             if (arg2) {
5044                 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
5045                     goto efault;
5046                 act._sa_handler = old_act->_sa_handler;
5047                 target_siginitset(&act.sa_mask, old_act->sa_mask);
5048                 act.sa_flags = old_act->sa_flags;
5049                 act.sa_restorer = old_act->sa_restorer;
5050                 unlock_user_struct(old_act, arg2, 0);
5051                 pact = &act;
5052             } else {
5053                 pact = NULL;
5054             }
5055             ret = get_errno(do_sigaction(arg1, pact, &oact));
5056             if (!is_error(ret) && arg3) {
5057                 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
5058                     goto efault;
5059                 old_act->_sa_handler = oact._sa_handler;
5060                 old_act->sa_mask = oact.sa_mask.sig[0];
5061                 old_act->sa_flags = oact.sa_flags;
5062                 old_act->sa_restorer = oact.sa_restorer;
5063                 unlock_user_struct(old_act, arg3, 1);
5064             }
5065 #endif
5066         }
5067         break;
5068 #endif
5069     case TARGET_NR_rt_sigaction:
5070         {
5071 #if defined(TARGET_ALPHA)
5072             struct target_sigaction act, oact, *pact = 0;
5073             struct target_rt_sigaction *rt_act;
5074             /* ??? arg4 == sizeof(sigset_t).  */
5075             if (arg2) {
5076                 if (!lock_user_struct(VERIFY_READ, rt_act, arg2, 1))
5077                     goto efault;
5078                 act._sa_handler = rt_act->_sa_handler;
5079                 act.sa_mask = rt_act->sa_mask;
5080                 act.sa_flags = rt_act->sa_flags;
5081                 act.sa_restorer = arg5;
5082                 unlock_user_struct(rt_act, arg2, 0);
5083                 pact = &act;
5084             }
5085             ret = get_errno(do_sigaction(arg1, pact, &oact));
5086             if (!is_error(ret) && arg3) {
5087                 if (!lock_user_struct(VERIFY_WRITE, rt_act, arg3, 0))
5088                     goto efault;
5089                 rt_act->_sa_handler = oact._sa_handler;
5090                 rt_act->sa_mask = oact.sa_mask;
5091                 rt_act->sa_flags = oact.sa_flags;
5092                 unlock_user_struct(rt_act, arg3, 1);
5093             }
5094 #else
5095             struct target_sigaction *act;
5096             struct target_sigaction *oact;
5097 
5098             if (arg2) {
5099                 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
5100                     goto efault;
5101             } else
5102                 act = NULL;
5103             if (arg3) {
5104                 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
5105                     ret = -TARGET_EFAULT;
5106                     goto rt_sigaction_fail;
5107                 }
5108             } else
5109                 oact = NULL;
5110             ret = get_errno(do_sigaction(arg1, act, oact));
5111 	rt_sigaction_fail:
5112             if (act)
5113                 unlock_user_struct(act, arg2, 0);
5114             if (oact)
5115                 unlock_user_struct(oact, arg3, 1);
5116 #endif
5117         }
5118         break;
5119 #ifdef TARGET_NR_sgetmask /* not on alpha */
5120     case TARGET_NR_sgetmask:
5121         {
5122             sigset_t cur_set;
5123             abi_ulong target_set;
5124             sigprocmask(0, NULL, &cur_set);
5125             host_to_target_old_sigset(&target_set, &cur_set);
5126             ret = target_set;
5127         }
5128         break;
5129 #endif
5130 #ifdef TARGET_NR_ssetmask /* not on alpha */
5131     case TARGET_NR_ssetmask:
5132         {
5133             sigset_t set, oset, cur_set;
5134             abi_ulong target_set = arg1;
5135             sigprocmask(0, NULL, &cur_set);
5136             target_to_host_old_sigset(&set, &target_set);
5137             sigorset(&set, &set, &cur_set);
5138             sigprocmask(SIG_SETMASK, &set, &oset);
5139             host_to_target_old_sigset(&target_set, &oset);
5140             ret = target_set;
5141         }
5142         break;
5143 #endif
5144 #ifdef TARGET_NR_sigprocmask
5145     case TARGET_NR_sigprocmask:
5146         {
5147 #if defined(TARGET_ALPHA)
5148             sigset_t set, oldset;
5149             abi_ulong mask;
5150             int how;
5151 
5152             switch (arg1) {
5153             case TARGET_SIG_BLOCK:
5154                 how = SIG_BLOCK;
5155                 break;
5156             case TARGET_SIG_UNBLOCK:
5157                 how = SIG_UNBLOCK;
5158                 break;
5159             case TARGET_SIG_SETMASK:
5160                 how = SIG_SETMASK;
5161                 break;
5162             default:
5163                 ret = -TARGET_EINVAL;
5164                 goto fail;
5165             }
5166             mask = arg2;
5167             target_to_host_old_sigset(&set, &mask);
5168 
5169             ret = get_errno(sigprocmask(how, &set, &oldset));
5170 
5171             if (!is_error(ret)) {
5172                 host_to_target_old_sigset(&mask, &oldset);
5173                 ret = mask;
5174                 ((CPUAlphaState *)cpu_env)->[IR_V0] = 0; /* force no error */
5175             }
5176 #else
5177             sigset_t set, oldset, *set_ptr;
5178             int how;
5179 
5180             if (arg2) {
5181                 switch (arg1) {
5182                 case TARGET_SIG_BLOCK:
5183                     how = SIG_BLOCK;
5184                     break;
5185                 case TARGET_SIG_UNBLOCK:
5186                     how = SIG_UNBLOCK;
5187                     break;
5188                 case TARGET_SIG_SETMASK:
5189                     how = SIG_SETMASK;
5190                     break;
5191                 default:
5192                     ret = -TARGET_EINVAL;
5193                     goto fail;
5194                 }
5195                 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
5196                     goto efault;
5197                 target_to_host_old_sigset(&set, p);
5198                 unlock_user(p, arg2, 0);
5199                 set_ptr = &set;
5200             } else {
5201                 how = 0;
5202                 set_ptr = NULL;
5203             }
5204             ret = get_errno(sigprocmask(how, set_ptr, &oldset));
5205             if (!is_error(ret) && arg3) {
5206                 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
5207                     goto efault;
5208                 host_to_target_old_sigset(p, &oldset);
5209                 unlock_user(p, arg3, sizeof(target_sigset_t));
5210             }
5211 #endif
5212         }
5213         break;
5214 #endif
5215     case TARGET_NR_rt_sigprocmask:
5216         {
5217             int how = arg1;
5218             sigset_t set, oldset, *set_ptr;
5219 
5220             if (arg2) {
5221                 switch(how) {
5222                 case TARGET_SIG_BLOCK:
5223                     how = SIG_BLOCK;
5224                     break;
5225                 case TARGET_SIG_UNBLOCK:
5226                     how = SIG_UNBLOCK;
5227                     break;
5228                 case TARGET_SIG_SETMASK:
5229                     how = SIG_SETMASK;
5230                     break;
5231                 default:
5232                     ret = -TARGET_EINVAL;
5233                     goto fail;
5234                 }
5235                 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
5236                     goto efault;
5237                 target_to_host_sigset(&set, p);
5238                 unlock_user(p, arg2, 0);
5239                 set_ptr = &set;
5240             } else {
5241                 how = 0;
5242                 set_ptr = NULL;
5243             }
5244             ret = get_errno(sigprocmask(how, set_ptr, &oldset));
5245             if (!is_error(ret) && arg3) {
5246                 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
5247                     goto efault;
5248                 host_to_target_sigset(p, &oldset);
5249                 unlock_user(p, arg3, sizeof(target_sigset_t));
5250             }
5251         }
5252         break;
5253 #ifdef TARGET_NR_sigpending
5254     case TARGET_NR_sigpending:
5255         {
5256             sigset_t set;
5257             ret = get_errno(sigpending(&set));
5258             if (!is_error(ret)) {
5259                 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
5260                     goto efault;
5261                 host_to_target_old_sigset(p, &set);
5262                 unlock_user(p, arg1, sizeof(target_sigset_t));
5263             }
5264         }
5265         break;
5266 #endif
5267     case TARGET_NR_rt_sigpending:
5268         {
5269             sigset_t set;
5270             ret = get_errno(sigpending(&set));
5271             if (!is_error(ret)) {
5272                 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
5273                     goto efault;
5274                 host_to_target_sigset(p, &set);
5275                 unlock_user(p, arg1, sizeof(target_sigset_t));
5276             }
5277         }
5278         break;
5279 #ifdef TARGET_NR_sigsuspend
5280     case TARGET_NR_sigsuspend:
5281         {
5282             sigset_t set;
5283 #if defined(TARGET_ALPHA)
5284             abi_ulong mask = arg1;
5285             target_to_host_old_sigset(&set, &mask);
5286 #else
5287             if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
5288                 goto efault;
5289             target_to_host_old_sigset(&set, p);
5290             unlock_user(p, arg1, 0);
5291 #endif
5292             ret = get_errno(sigsuspend(&set));
5293         }
5294         break;
5295 #endif
5296     case TARGET_NR_rt_sigsuspend:
5297         {
5298             sigset_t set;
5299             if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
5300                 goto efault;
5301             target_to_host_sigset(&set, p);
5302             unlock_user(p, arg1, 0);
5303             ret = get_errno(sigsuspend(&set));
5304         }
5305         break;
5306     case TARGET_NR_rt_sigtimedwait:
5307         {
5308             sigset_t set;
5309             struct timespec uts, *puts;
5310             siginfo_t uinfo;
5311 
5312             if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
5313                 goto efault;
5314             target_to_host_sigset(&set, p);
5315             unlock_user(p, arg1, 0);
5316             if (arg3) {
5317                 puts = &uts;
5318                 target_to_host_timespec(puts, arg3);
5319             } else {
5320                 puts = NULL;
5321             }
5322             ret = get_errno(sigtimedwait(&set, &uinfo, puts));
5323             if (!is_error(ret) && arg2) {
5324                 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
5325                     goto efault;
5326                 host_to_target_siginfo(p, &uinfo);
5327                 unlock_user(p, arg2, sizeof(target_siginfo_t));
5328             }
5329         }
5330         break;
5331     case TARGET_NR_rt_sigqueueinfo:
5332         {
5333             siginfo_t uinfo;
5334             if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
5335                 goto efault;
5336             target_to_host_siginfo(&uinfo, p);
5337             unlock_user(p, arg1, 0);
5338             ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
5339         }
5340         break;
5341 #ifdef TARGET_NR_sigreturn
5342     case TARGET_NR_sigreturn:
5343         /* NOTE: ret is eax, so not transcoding must be done */
5344         ret = do_sigreturn(cpu_env);
5345         break;
5346 #endif
5347     case TARGET_NR_rt_sigreturn:
5348         /* NOTE: ret is eax, so not transcoding must be done */
5349         ret = do_rt_sigreturn(cpu_env);
5350         break;
5351     case TARGET_NR_sethostname:
5352         if (!(p = lock_user_string(arg1)))
5353             goto efault;
5354         ret = get_errno(sethostname(p, arg2));
5355         unlock_user(p, arg1, 0);
5356         break;
5357     case TARGET_NR_setrlimit:
5358         {
5359             int resource = arg1;
5360             struct target_rlimit *target_rlim;
5361             struct rlimit rlim;
5362             if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
5363                 goto efault;
5364             rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
5365             rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
5366             unlock_user_struct(target_rlim, arg2, 0);
5367             ret = get_errno(setrlimit(resource, &rlim));
5368         }
5369         break;
5370     case TARGET_NR_getrlimit:
5371         {
5372             int resource = arg1;
5373             struct target_rlimit *target_rlim;
5374             struct rlimit rlim;
5375 
5376             ret = get_errno(getrlimit(resource, &rlim));
5377             if (!is_error(ret)) {
5378                 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
5379                     goto efault;
5380                 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
5381                 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
5382                 unlock_user_struct(target_rlim, arg2, 1);
5383             }
5384         }
5385         break;
5386     case TARGET_NR_getrusage:
5387         {
5388             struct rusage rusage;
5389             ret = get_errno(getrusage(arg1, &rusage));
5390             if (!is_error(ret)) {
5391                 host_to_target_rusage(arg2, &rusage);
5392             }
5393         }
5394         break;
5395     case TARGET_NR_gettimeofday:
5396         {
5397             struct timeval tv;
5398             ret = get_errno(gettimeofday(&tv, NULL));
5399             if (!is_error(ret)) {
5400                 if (copy_to_user_timeval(arg1, &tv))
5401                     goto efault;
5402             }
5403         }
5404         break;
5405     case TARGET_NR_settimeofday:
5406         {
5407             struct timeval tv;
5408             if (copy_from_user_timeval(&tv, arg1))
5409                 goto efault;
5410             ret = get_errno(settimeofday(&tv, NULL));
5411         }
5412         break;
5413 #ifdef TARGET_NR_select
5414     case TARGET_NR_select:
5415         {
5416             struct target_sel_arg_struct *sel;
5417             abi_ulong inp, outp, exp, tvp;
5418             long nsel;
5419 
5420             if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
5421                 goto efault;
5422             nsel = tswapl(sel->n);
5423             inp = tswapl(sel->inp);
5424             outp = tswapl(sel->outp);
5425             exp = tswapl(sel->exp);
5426             tvp = tswapl(sel->tvp);
5427             unlock_user_struct(sel, arg1, 0);
5428             ret = do_select(nsel, inp, outp, exp, tvp);
5429         }
5430         break;
5431 #endif
5432 #ifdef TARGET_NR_pselect6
5433     case TARGET_NR_pselect6:
5434 	    goto unimplemented_nowarn;
5435 #endif
5436     case TARGET_NR_symlink:
5437         {
5438             void *p2;
5439             p = lock_user_string(arg1);
5440             p2 = lock_user_string(arg2);
5441             if (!p || !p2)
5442                 ret = -TARGET_EFAULT;
5443             else
5444                 ret = get_errno(symlink(p, p2));
5445             unlock_user(p2, arg2, 0);
5446             unlock_user(p, arg1, 0);
5447         }
5448         break;
5449 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
5450     case TARGET_NR_symlinkat:
5451         {
5452             void *p2;
5453             p  = lock_user_string(arg1);
5454             p2 = lock_user_string(arg3);
5455             if (!p || !p2)
5456                 ret = -TARGET_EFAULT;
5457             else
5458                 ret = get_errno(sys_symlinkat(p, arg2, p2));
5459             unlock_user(p2, arg3, 0);
5460             unlock_user(p, arg1, 0);
5461         }
5462         break;
5463 #endif
5464 #ifdef TARGET_NR_oldlstat
5465     case TARGET_NR_oldlstat:
5466         goto unimplemented;
5467 #endif
5468     case TARGET_NR_readlink:
5469         {
5470             void *p2, *temp;
5471             p = lock_user_string(arg1);
5472             p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
5473             if (!p || !p2)
5474                 ret = -TARGET_EFAULT;
5475             else {
5476                 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
5477                     char real[PATH_MAX];
5478                     temp = realpath(exec_path,real);
5479                     ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
5480                     snprintf((char *)p2, arg3, "%s", real);
5481                     }
5482                 else
5483                     ret = get_errno(readlink(path(p), p2, arg3));
5484             }
5485             unlock_user(p2, arg2, ret);
5486             unlock_user(p, arg1, 0);
5487         }
5488         break;
5489 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
5490     case TARGET_NR_readlinkat:
5491         {
5492             void *p2;
5493             p  = lock_user_string(arg2);
5494             p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
5495             if (!p || !p2)
5496         	ret = -TARGET_EFAULT;
5497             else
5498                 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
5499             unlock_user(p2, arg3, ret);
5500             unlock_user(p, arg2, 0);
5501         }
5502         break;
5503 #endif
5504 #ifdef TARGET_NR_uselib
5505     case TARGET_NR_uselib:
5506         goto unimplemented;
5507 #endif
5508 #ifdef TARGET_NR_swapon
5509     case TARGET_NR_swapon:
5510         if (!(p = lock_user_string(arg1)))
5511             goto efault;
5512         ret = get_errno(swapon(p, arg2));
5513         unlock_user(p, arg1, 0);
5514         break;
5515 #endif
5516     case TARGET_NR_reboot:
5517         goto unimplemented;
5518 #ifdef TARGET_NR_readdir
5519     case TARGET_NR_readdir:
5520         goto unimplemented;
5521 #endif
5522 #ifdef TARGET_NR_mmap
5523     case TARGET_NR_mmap:
5524 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE)
5525         {
5526             abi_ulong *v;
5527             abi_ulong v1, v2, v3, v4, v5, v6;
5528             if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
5529                 goto efault;
5530             v1 = tswapl(v[0]);
5531             v2 = tswapl(v[1]);
5532             v3 = tswapl(v[2]);
5533             v4 = tswapl(v[3]);
5534             v5 = tswapl(v[4]);
5535             v6 = tswapl(v[5]);
5536             unlock_user(v, arg1, 0);
5537             ret = get_errno(target_mmap(v1, v2, v3,
5538                                         target_to_host_bitmask(v4, mmap_flags_tbl),
5539                                         v5, v6));
5540         }
5541 #else
5542         ret = get_errno(target_mmap(arg1, arg2, arg3,
5543                                     target_to_host_bitmask(arg4, mmap_flags_tbl),
5544                                     arg5,
5545                                     arg6));
5546 #endif
5547         break;
5548 #endif
5549 #ifdef TARGET_NR_mmap2
5550     case TARGET_NR_mmap2:
5551 #ifndef MMAP_SHIFT
5552 #define MMAP_SHIFT 12
5553 #endif
5554         ret = get_errno(target_mmap(arg1, arg2, arg3,
5555                                     target_to_host_bitmask(arg4, mmap_flags_tbl),
5556                                     arg5,
5557                                     arg6 << MMAP_SHIFT));
5558         break;
5559 #endif
5560     case TARGET_NR_munmap:
5561         ret = get_errno(target_munmap(arg1, arg2));
5562         break;
5563     case TARGET_NR_mprotect:
5564         {
5565             TaskState *ts = ((CPUState *)cpu_env)->opaque;
5566             /* Special hack to detect libc making the stack executable.  */
5567             if ((arg3 & PROT_GROWSDOWN)
5568                 && arg1 >= ts->info->stack_limit
5569                 && arg1 <= ts->info->start_stack) {
5570                 arg3 &= ~PROT_GROWSDOWN;
5571                 arg2 = arg2 + arg1 - ts->info->stack_limit;
5572                 arg1 = ts->info->stack_limit;
5573             }
5574         }
5575         ret = get_errno(target_mprotect(arg1, arg2, arg3));
5576         break;
5577 #ifdef TARGET_NR_mremap
5578     case TARGET_NR_mremap:
5579         ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
5580         break;
5581 #endif
5582         /* ??? msync/mlock/munlock are broken for softmmu.  */
5583 #ifdef TARGET_NR_msync
5584     case TARGET_NR_msync:
5585         ret = get_errno(msync(g2h(arg1), arg2, arg3));
5586         break;
5587 #endif
5588 #ifdef TARGET_NR_mlock
5589     case TARGET_NR_mlock:
5590         ret = get_errno(mlock(g2h(arg1), arg2));
5591         break;
5592 #endif
5593 #ifdef TARGET_NR_munlock
5594     case TARGET_NR_munlock:
5595         ret = get_errno(munlock(g2h(arg1), arg2));
5596         break;
5597 #endif
5598 #ifdef TARGET_NR_mlockall
5599     case TARGET_NR_mlockall:
5600         ret = get_errno(mlockall(arg1));
5601         break;
5602 #endif
5603 #ifdef TARGET_NR_munlockall
5604     case TARGET_NR_munlockall:
5605         ret = get_errno(munlockall());
5606         break;
5607 #endif
5608     case TARGET_NR_truncate:
5609         if (!(p = lock_user_string(arg1)))
5610             goto efault;
5611         ret = get_errno(truncate(p, arg2));
5612         unlock_user(p, arg1, 0);
5613         break;
5614     case TARGET_NR_ftruncate:
5615         ret = get_errno(ftruncate(arg1, arg2));
5616         break;
5617     case TARGET_NR_fchmod:
5618         ret = get_errno(fchmod(arg1, arg2));
5619         break;
5620 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
5621     case TARGET_NR_fchmodat:
5622         if (!(p = lock_user_string(arg2)))
5623             goto efault;
5624         ret = get_errno(sys_fchmodat(arg1, p, arg3));
5625         unlock_user(p, arg2, 0);
5626         break;
5627 #endif
5628     case TARGET_NR_getpriority:
5629         /* libc does special remapping of the return value of
5630          * sys_getpriority() so it's just easiest to call
5631          * sys_getpriority() directly rather than through libc. */
5632         ret = get_errno(sys_getpriority(arg1, arg2));
5633         break;
5634     case TARGET_NR_setpriority:
5635         ret = get_errno(setpriority(arg1, arg2, arg3));
5636         break;
5637 #ifdef TARGET_NR_profil
5638     case TARGET_NR_profil:
5639         goto unimplemented;
5640 #endif
5641     case TARGET_NR_statfs:
5642         if (!(p = lock_user_string(arg1)))
5643             goto efault;
5644         ret = get_errno(statfs(path(p), &stfs));
5645         unlock_user(p, arg1, 0);
5646     convert_statfs:
5647         if (!is_error(ret)) {
5648             struct target_statfs *target_stfs;
5649 
5650             if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
5651                 goto efault;
5652             __put_user(stfs.f_type, &target_stfs->f_type);
5653             __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5654             __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5655             __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5656             __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5657             __put_user(stfs.f_files, &target_stfs->f_files);
5658             __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5659             __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5660             __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5661             __put_user(stfs.f_namelen, &target_stfs->f_namelen);
5662             unlock_user_struct(target_stfs, arg2, 1);
5663         }
5664         break;
5665     case TARGET_NR_fstatfs:
5666         ret = get_errno(fstatfs(arg1, &stfs));
5667         goto convert_statfs;
5668 #ifdef TARGET_NR_statfs64
5669     case TARGET_NR_statfs64:
5670         if (!(p = lock_user_string(arg1)))
5671             goto efault;
5672         ret = get_errno(statfs(path(p), &stfs));
5673         unlock_user(p, arg1, 0);
5674     convert_statfs64:
5675         if (!is_error(ret)) {
5676             struct target_statfs64 *target_stfs;
5677 
5678             if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
5679                 goto efault;
5680             __put_user(stfs.f_type, &target_stfs->f_type);
5681             __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5682             __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5683             __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5684             __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5685             __put_user(stfs.f_files, &target_stfs->f_files);
5686             __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5687             __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5688             __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5689             __put_user(stfs.f_namelen, &target_stfs->f_namelen);
5690             unlock_user_struct(target_stfs, arg3, 1);
5691         }
5692         break;
5693     case TARGET_NR_fstatfs64:
5694         ret = get_errno(fstatfs(arg1, &stfs));
5695         goto convert_statfs64;
5696 #endif
5697 #ifdef TARGET_NR_ioperm
5698     case TARGET_NR_ioperm:
5699         goto unimplemented;
5700 #endif
5701 #ifdef TARGET_NR_socketcall
5702     case TARGET_NR_socketcall:
5703         ret = do_socketcall(arg1, arg2);
5704         break;
5705 #endif
5706 #ifdef TARGET_NR_accept
5707     case TARGET_NR_accept:
5708         ret = do_accept(arg1, arg2, arg3);
5709         break;
5710 #endif
5711 #ifdef TARGET_NR_bind
5712     case TARGET_NR_bind:
5713         ret = do_bind(arg1, arg2, arg3);
5714         break;
5715 #endif
5716 #ifdef TARGET_NR_connect
5717     case TARGET_NR_connect:
5718         ret = do_connect(arg1, arg2, arg3);
5719         break;
5720 #endif
5721 #ifdef TARGET_NR_getpeername
5722     case TARGET_NR_getpeername:
5723         ret = do_getpeername(arg1, arg2, arg3);
5724         break;
5725 #endif
5726 #ifdef TARGET_NR_getsockname
5727     case TARGET_NR_getsockname:
5728         ret = do_getsockname(arg1, arg2, arg3);
5729         break;
5730 #endif
5731 #ifdef TARGET_NR_getsockopt
5732     case TARGET_NR_getsockopt:
5733         ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
5734         break;
5735 #endif
5736 #ifdef TARGET_NR_listen
5737     case TARGET_NR_listen:
5738         ret = get_errno(listen(arg1, arg2));
5739         break;
5740 #endif
5741 #ifdef TARGET_NR_recv
5742     case TARGET_NR_recv:
5743         ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
5744         break;
5745 #endif
5746 #ifdef TARGET_NR_recvfrom
5747     case TARGET_NR_recvfrom:
5748         ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
5749         break;
5750 #endif
5751 #ifdef TARGET_NR_recvmsg
5752     case TARGET_NR_recvmsg:
5753         ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
5754         break;
5755 #endif
5756 #ifdef TARGET_NR_send
5757     case TARGET_NR_send:
5758         ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
5759         break;
5760 #endif
5761 #ifdef TARGET_NR_sendmsg
5762     case TARGET_NR_sendmsg:
5763         ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
5764         break;
5765 #endif
5766 #ifdef TARGET_NR_sendto
5767     case TARGET_NR_sendto:
5768         ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
5769         break;
5770 #endif
5771 #ifdef TARGET_NR_shutdown
5772     case TARGET_NR_shutdown:
5773         ret = get_errno(shutdown(arg1, arg2));
5774         break;
5775 #endif
5776 #ifdef TARGET_NR_socket
5777     case TARGET_NR_socket:
5778         ret = do_socket(arg1, arg2, arg3);
5779         break;
5780 #endif
5781 #ifdef TARGET_NR_socketpair
5782     case TARGET_NR_socketpair:
5783         ret = do_socketpair(arg1, arg2, arg3, arg4);
5784         break;
5785 #endif
5786 #ifdef TARGET_NR_setsockopt
5787     case TARGET_NR_setsockopt:
5788         ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
5789         break;
5790 #endif
5791 
5792     case TARGET_NR_syslog:
5793         if (!(p = lock_user_string(arg2)))
5794             goto efault;
5795         ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
5796         unlock_user(p, arg2, 0);
5797         break;
5798 
5799     case TARGET_NR_setitimer:
5800         {
5801             struct itimerval value, ovalue, *pvalue;
5802 
5803             if (arg2) {
5804                 pvalue = &value;
5805                 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
5806                     || copy_from_user_timeval(&pvalue->it_value,
5807                                               arg2 + sizeof(struct target_timeval)))
5808                     goto efault;
5809             } else {
5810                 pvalue = NULL;
5811             }
5812             ret = get_errno(setitimer(arg1, pvalue, &ovalue));
5813             if (!is_error(ret) && arg3) {
5814                 if (copy_to_user_timeval(arg3,
5815                                          &ovalue.it_interval)
5816                     || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
5817                                             &ovalue.it_value))
5818                     goto efault;
5819             }
5820         }
5821         break;
5822     case TARGET_NR_getitimer:
5823         {
5824             struct itimerval value;
5825 
5826             ret = get_errno(getitimer(arg1, &value));
5827             if (!is_error(ret) && arg2) {
5828                 if (copy_to_user_timeval(arg2,
5829                                          &value.it_interval)
5830                     || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
5831                                             &value.it_value))
5832                     goto efault;
5833             }
5834         }
5835         break;
5836     case TARGET_NR_stat:
5837         if (!(p = lock_user_string(arg1)))
5838             goto efault;
5839         ret = get_errno(stat(path(p), &st));
5840         unlock_user(p, arg1, 0);
5841         goto do_stat;
5842     case TARGET_NR_lstat:
5843         if (!(p = lock_user_string(arg1)))
5844             goto efault;
5845         ret = get_errno(lstat(path(p), &st));
5846         unlock_user(p, arg1, 0);
5847         goto do_stat;
5848     case TARGET_NR_fstat:
5849         {
5850             ret = get_errno(fstat(arg1, &st));
5851         do_stat:
5852             if (!is_error(ret)) {
5853                 struct target_stat *target_st;
5854 
5855                 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
5856                     goto efault;
5857                 memset(target_st, 0, sizeof(*target_st));
5858                 __put_user(st.st_dev, &target_st->st_dev);
5859                 __put_user(st.st_ino, &target_st->st_ino);
5860                 __put_user(st.st_mode, &target_st->st_mode);
5861                 __put_user(st.st_uid, &target_st->st_uid);
5862                 __put_user(st.st_gid, &target_st->st_gid);
5863                 __put_user(st.st_nlink, &target_st->st_nlink);
5864                 __put_user(st.st_rdev, &target_st->st_rdev);
5865                 __put_user(st.st_size, &target_st->st_size);
5866                 __put_user(st.st_blksize, &target_st->st_blksize);
5867                 __put_user(st.st_blocks, &target_st->st_blocks);
5868                 __put_user(st.st_atime, &target_st->target_st_atime);
5869                 __put_user(st.st_mtime, &target_st->target_st_mtime);
5870                 __put_user(st.st_ctime, &target_st->target_st_ctime);
5871                 unlock_user_struct(target_st, arg2, 1);
5872             }
5873         }
5874         break;
5875 #ifdef TARGET_NR_olduname
5876     case TARGET_NR_olduname:
5877         goto unimplemented;
5878 #endif
5879 #ifdef TARGET_NR_iopl
5880     case TARGET_NR_iopl:
5881         goto unimplemented;
5882 #endif
5883     case TARGET_NR_vhangup:
5884         ret = get_errno(vhangup());
5885         break;
5886 #ifdef TARGET_NR_idle
5887     case TARGET_NR_idle:
5888         goto unimplemented;
5889 #endif
5890 #ifdef TARGET_NR_syscall
5891     case TARGET_NR_syscall:
5892     	ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
5893     	break;
5894 #endif
5895     case TARGET_NR_wait4:
5896         {
5897             int status;
5898             abi_long status_ptr = arg2;
5899             struct rusage rusage, *rusage_ptr;
5900             abi_ulong target_rusage = arg4;
5901             if (target_rusage)
5902                 rusage_ptr = &rusage;
5903             else
5904                 rusage_ptr = NULL;
5905             ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
5906             if (!is_error(ret)) {
5907                 if (status_ptr) {
5908                     status = host_to_target_waitstatus(status);
5909                     if (put_user_s32(status, status_ptr))
5910                         goto efault;
5911                 }
5912                 if (target_rusage)
5913                     host_to_target_rusage(target_rusage, &rusage);
5914             }
5915         }
5916         break;
5917 #ifdef TARGET_NR_swapoff
5918     case TARGET_NR_swapoff:
5919         if (!(p = lock_user_string(arg1)))
5920             goto efault;
5921         ret = get_errno(swapoff(p));
5922         unlock_user(p, arg1, 0);
5923         break;
5924 #endif
5925     case TARGET_NR_sysinfo:
5926         {
5927             struct target_sysinfo *target_value;
5928             struct sysinfo value;
5929             ret = get_errno(sysinfo(&value));
5930             if (!is_error(ret) && arg1)
5931             {
5932                 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
5933                     goto efault;
5934                 __put_user(value.uptime, &target_value->uptime);
5935                 __put_user(value.loads[0], &target_value->loads[0]);
5936                 __put_user(value.loads[1], &target_value->loads[1]);
5937                 __put_user(value.loads[2], &target_value->loads[2]);
5938                 __put_user(value.totalram, &target_value->totalram);
5939                 __put_user(value.freeram, &target_value->freeram);
5940                 __put_user(value.sharedram, &target_value->sharedram);
5941                 __put_user(value.bufferram, &target_value->bufferram);
5942                 __put_user(value.totalswap, &target_value->totalswap);
5943                 __put_user(value.freeswap, &target_value->freeswap);
5944                 __put_user(value.procs, &target_value->procs);
5945                 __put_user(value.totalhigh, &target_value->totalhigh);
5946                 __put_user(value.freehigh, &target_value->freehigh);
5947                 __put_user(value.mem_unit, &target_value->mem_unit);
5948                 unlock_user_struct(target_value, arg1, 1);
5949             }
5950         }
5951         break;
5952 #ifdef TARGET_NR_ipc
5953     case TARGET_NR_ipc:
5954 	ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
5955 	break;
5956 #endif
5957 #ifdef TARGET_NR_semget
5958     case TARGET_NR_semget:
5959         ret = get_errno(semget(arg1, arg2, arg3));
5960         break;
5961 #endif
5962 #ifdef TARGET_NR_semop
5963     case TARGET_NR_semop:
5964         ret = get_errno(do_semop(arg1, arg2, arg3));
5965         break;
5966 #endif
5967 #ifdef TARGET_NR_semctl
5968     case TARGET_NR_semctl:
5969         ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
5970         break;
5971 #endif
5972 #ifdef TARGET_NR_msgctl
5973     case TARGET_NR_msgctl:
5974         ret = do_msgctl(arg1, arg2, arg3);
5975         break;
5976 #endif
5977 #ifdef TARGET_NR_msgget
5978     case TARGET_NR_msgget:
5979         ret = get_errno(msgget(arg1, arg2));
5980         break;
5981 #endif
5982 #ifdef TARGET_NR_msgrcv
5983     case TARGET_NR_msgrcv:
5984         ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
5985         break;
5986 #endif
5987 #ifdef TARGET_NR_msgsnd
5988     case TARGET_NR_msgsnd:
5989         ret = do_msgsnd(arg1, arg2, arg3, arg4);
5990         break;
5991 #endif
5992 #ifdef TARGET_NR_shmget
5993     case TARGET_NR_shmget:
5994         ret = get_errno(shmget(arg1, arg2, arg3));
5995         break;
5996 #endif
5997 #ifdef TARGET_NR_shmctl
5998     case TARGET_NR_shmctl:
5999         ret = do_shmctl(arg1, arg2, arg3);
6000         break;
6001 #endif
6002 #ifdef TARGET_NR_shmat
6003     case TARGET_NR_shmat:
6004         ret = do_shmat(arg1, arg2, arg3);
6005         break;
6006 #endif
6007 #ifdef TARGET_NR_shmdt
6008     case TARGET_NR_shmdt:
6009         ret = do_shmdt(arg1);
6010         break;
6011 #endif
6012     case TARGET_NR_fsync:
6013         ret = get_errno(fsync(arg1));
6014         break;
6015     case TARGET_NR_clone:
6016 #if defined(TARGET_SH4) || defined(TARGET_ALPHA)
6017         ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
6018 #elif defined(TARGET_CRIS)
6019         ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
6020 #else
6021         ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
6022 #endif
6023         break;
6024 #ifdef __NR_exit_group
6025         /* new thread calls */
6026     case TARGET_NR_exit_group:
6027 #ifdef TARGET_GPROF
6028         _mcleanup();
6029 #endif
6030         gdb_exit(cpu_env, arg1);
6031         ret = get_errno(exit_group(arg1));
6032         break;
6033 #endif
6034     case TARGET_NR_setdomainname:
6035         if (!(p = lock_user_string(arg1)))
6036             goto efault;
6037         ret = get_errno(setdomainname(p, arg2));
6038         unlock_user(p, arg1, 0);
6039         break;
6040     case TARGET_NR_uname:
6041         /* no need to transcode because we use the linux syscall */
6042         {
6043             struct new_utsname * buf;
6044 
6045             if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
6046                 goto efault;
6047             ret = get_errno(sys_uname(buf));
6048             if (!is_error(ret)) {
6049                 /* Overrite the native machine name with whatever is being
6050                    emulated. */
6051                 strcpy (buf->machine, cpu_to_uname_machine(cpu_env));
6052                 /* Allow the user to override the reported release.  */
6053                 if (qemu_uname_release && *qemu_uname_release)
6054                   strcpy (buf->release, qemu_uname_release);
6055             }
6056             unlock_user_struct(buf, arg1, 1);
6057         }
6058         break;
6059 #ifdef TARGET_I386
6060     case TARGET_NR_modify_ldt:
6061         ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
6062         break;
6063 #if !defined(TARGET_X86_64)
6064     case TARGET_NR_vm86old:
6065         goto unimplemented;
6066     case TARGET_NR_vm86:
6067         ret = do_vm86(cpu_env, arg1, arg2);
6068         break;
6069 #endif
6070 #endif
6071     case TARGET_NR_adjtimex:
6072         goto unimplemented;
6073 #ifdef TARGET_NR_create_module
6074     case TARGET_NR_create_module:
6075 #endif
6076     case TARGET_NR_init_module:
6077     case TARGET_NR_delete_module:
6078 #ifdef TARGET_NR_get_kernel_syms
6079     case TARGET_NR_get_kernel_syms:
6080 #endif
6081         goto unimplemented;
6082     case TARGET_NR_quotactl:
6083         goto unimplemented;
6084     case TARGET_NR_getpgid:
6085         ret = get_errno(getpgid(arg1));
6086         break;
6087     case TARGET_NR_fchdir:
6088         ret = get_errno(fchdir(arg1));
6089         break;
6090 #ifdef TARGET_NR_bdflush /* not on x86_64 */
6091     case TARGET_NR_bdflush:
6092         goto unimplemented;
6093 #endif
6094 #ifdef TARGET_NR_sysfs
6095     case TARGET_NR_sysfs:
6096         goto unimplemented;
6097 #endif
6098     case TARGET_NR_personality:
6099         ret = get_errno(personality(arg1));
6100         break;
6101 #ifdef TARGET_NR_afs_syscall
6102     case TARGET_NR_afs_syscall:
6103         goto unimplemented;
6104 #endif
6105 #ifdef TARGET_NR__llseek /* Not on alpha */
6106     case TARGET_NR__llseek:
6107         {
6108 #if !defined(__NR_llseek)
6109             ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
6110             if (put_user_s64(ret, arg4))
6111                 goto efault;
6112 #else
6113             int64_t res;
6114             ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
6115             if (put_user_s64(res, arg4))
6116                 goto efault;
6117 #endif
6118         }
6119         break;
6120 #endif
6121     case TARGET_NR_getdents:
6122 #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
6123         {
6124             struct target_dirent *target_dirp;
6125             struct linux_dirent *dirp;
6126             abi_long count = arg3;
6127 
6128 	    dirp = malloc(count);
6129 	    if (!dirp) {
6130                 ret = -TARGET_ENOMEM;
6131                 goto fail;
6132             }
6133 
6134             ret = get_errno(sys_getdents(arg1, dirp, count));
6135             if (!is_error(ret)) {
6136                 struct linux_dirent *de;
6137 		struct target_dirent *tde;
6138                 int len = ret;
6139                 int reclen, treclen;
6140 		int count1, tnamelen;
6141 
6142 		count1 = 0;
6143                 de = dirp;
6144                 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6145                     goto efault;
6146 		tde = target_dirp;
6147                 while (len > 0) {
6148                     reclen = de->d_reclen;
6149 		    treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
6150                     tde->d_reclen = tswap16(treclen);
6151                     tde->d_ino = tswapl(de->d_ino);
6152                     tde->d_off = tswapl(de->d_off);
6153 		    tnamelen = treclen - (2 * sizeof(abi_long) + 2);
6154 		    if (tnamelen > 256)
6155                         tnamelen = 256;
6156                     /* XXX: may not be correct */
6157                     pstrcpy(tde->d_name, tnamelen, de->d_name);
6158                     de = (struct linux_dirent *)((char *)de + reclen);
6159                     len -= reclen;
6160                     tde = (struct target_dirent *)((char *)tde + treclen);
6161 		    count1 += treclen;
6162                 }
6163 		ret = count1;
6164                 unlock_user(target_dirp, arg2, ret);
6165             }
6166 	    free(dirp);
6167         }
6168 #else
6169         {
6170             struct linux_dirent *dirp;
6171             abi_long count = arg3;
6172 
6173             if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6174                 goto efault;
6175             ret = get_errno(sys_getdents(arg1, dirp, count));
6176             if (!is_error(ret)) {
6177                 struct linux_dirent *de;
6178                 int len = ret;
6179                 int reclen;
6180                 de = dirp;
6181                 while (len > 0) {
6182                     reclen = de->d_reclen;
6183                     if (reclen > len)
6184                         break;
6185                     de->d_reclen = tswap16(reclen);
6186                     tswapls(&de->d_ino);
6187                     tswapls(&de->d_off);
6188                     de = (struct linux_dirent *)((char *)de + reclen);
6189                     len -= reclen;
6190                 }
6191             }
6192             unlock_user(dirp, arg2, ret);
6193         }
6194 #endif
6195         break;
6196 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
6197     case TARGET_NR_getdents64:
6198         {
6199             struct linux_dirent64 *dirp;
6200             abi_long count = arg3;
6201             if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
6202                 goto efault;
6203             ret = get_errno(sys_getdents64(arg1, dirp, count));
6204             if (!is_error(ret)) {
6205                 struct linux_dirent64 *de;
6206                 int len = ret;
6207                 int reclen;
6208                 de = dirp;
6209                 while (len > 0) {
6210                     reclen = de->d_reclen;
6211                     if (reclen > len)
6212                         break;
6213                     de->d_reclen = tswap16(reclen);
6214                     tswap64s((uint64_t *)&de->d_ino);
6215                     tswap64s((uint64_t *)&de->d_off);
6216                     de = (struct linux_dirent64 *)((char *)de + reclen);
6217                     len -= reclen;
6218                 }
6219             }
6220             unlock_user(dirp, arg2, ret);
6221         }
6222         break;
6223 #endif /* TARGET_NR_getdents64 */
6224 #ifdef TARGET_NR__newselect
6225     case TARGET_NR__newselect:
6226         ret = do_select(arg1, arg2, arg3, arg4, arg5);
6227         break;
6228 #endif
6229 #ifdef TARGET_NR_poll
6230     case TARGET_NR_poll:
6231         {
6232             struct target_pollfd *target_pfd;
6233             unsigned int nfds = arg2;
6234             int timeout = arg3;
6235             struct pollfd *pfd;
6236             unsigned int i;
6237 
6238             target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
6239             if (!target_pfd)
6240                 goto efault;
6241             pfd = alloca(sizeof(struct pollfd) * nfds);
6242             for(i = 0; i < nfds; i++) {
6243                 pfd[i].fd = tswap32(target_pfd[i].fd);
6244                 pfd[i].events = tswap16(target_pfd[i].events);
6245             }
6246             ret = get_errno(poll(pfd, nfds, timeout));
6247             if (!is_error(ret)) {
6248                 for(i = 0; i < nfds; i++) {
6249                     target_pfd[i].revents = tswap16(pfd[i].revents);
6250                 }
6251                 ret += nfds * (sizeof(struct target_pollfd)
6252                                - sizeof(struct pollfd));
6253             }
6254             unlock_user(target_pfd, arg1, ret);
6255         }
6256         break;
6257 #endif
6258     case TARGET_NR_flock:
6259         /* NOTE: the flock constant seems to be the same for every
6260            Linux platform */
6261         ret = get_errno(flock(arg1, arg2));
6262         break;
6263     case TARGET_NR_readv:
6264         {
6265             int count = arg3;
6266             struct iovec *vec;
6267 
6268             vec = alloca(count * sizeof(struct iovec));
6269             if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
6270                 goto efault;
6271             ret = get_errno(readv(arg1, vec, count));
6272             unlock_iovec(vec, arg2, count, 1);
6273         }
6274         break;
6275     case TARGET_NR_writev:
6276         {
6277             int count = arg3;
6278             struct iovec *vec;
6279 
6280             vec = alloca(count * sizeof(struct iovec));
6281             if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
6282                 goto efault;
6283             ret = get_errno(writev(arg1, vec, count));
6284             unlock_iovec(vec, arg2, count, 0);
6285         }
6286         break;
6287     case TARGET_NR_getsid:
6288         ret = get_errno(getsid(arg1));
6289         break;
6290 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
6291     case TARGET_NR_fdatasync:
6292         ret = get_errno(fdatasync(arg1));
6293         break;
6294 #endif
6295     case TARGET_NR__sysctl:
6296         /* We don't implement this, but ENOTDIR is always a safe
6297            return value. */
6298         ret = -TARGET_ENOTDIR;
6299         break;
6300     case TARGET_NR_sched_setparam:
6301         {
6302             struct sched_param *target_schp;
6303             struct sched_param schp;
6304 
6305             if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
6306                 goto efault;
6307             schp.sched_priority = tswap32(target_schp->sched_priority);
6308             unlock_user_struct(target_schp, arg2, 0);
6309             ret = get_errno(sched_setparam(arg1, &schp));
6310         }
6311         break;
6312     case TARGET_NR_sched_getparam:
6313         {
6314             struct sched_param *target_schp;
6315             struct sched_param schp;
6316             ret = get_errno(sched_getparam(arg1, &schp));
6317             if (!is_error(ret)) {
6318                 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
6319                     goto efault;
6320                 target_schp->sched_priority = tswap32(schp.sched_priority);
6321                 unlock_user_struct(target_schp, arg2, 1);
6322             }
6323         }
6324         break;
6325     case TARGET_NR_sched_setscheduler:
6326         {
6327             struct sched_param *target_schp;
6328             struct sched_param schp;
6329             if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
6330                 goto efault;
6331             schp.sched_priority = tswap32(target_schp->sched_priority);
6332             unlock_user_struct(target_schp, arg3, 0);
6333             ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
6334         }
6335         break;
6336     case TARGET_NR_sched_getscheduler:
6337         ret = get_errno(sched_getscheduler(arg1));
6338         break;
6339     case TARGET_NR_sched_yield:
6340         ret = get_errno(sched_yield());
6341         break;
6342     case TARGET_NR_sched_get_priority_max:
6343         ret = get_errno(sched_get_priority_max(arg1));
6344         break;
6345     case TARGET_NR_sched_get_priority_min:
6346         ret = get_errno(sched_get_priority_min(arg1));
6347         break;
6348     case TARGET_NR_sched_rr_get_interval:
6349         {
6350             struct timespec ts;
6351             ret = get_errno(sched_rr_get_interval(arg1, &ts));
6352             if (!is_error(ret)) {
6353                 host_to_target_timespec(arg2, &ts);
6354             }
6355         }
6356         break;
6357     case TARGET_NR_nanosleep:
6358         {
6359             struct timespec req, rem;
6360             target_to_host_timespec(&req, arg1);
6361             ret = get_errno(nanosleep(&req, &rem));
6362             if (is_error(ret) && arg2) {
6363                 host_to_target_timespec(arg2, &rem);
6364             }
6365         }
6366         break;
6367 #ifdef TARGET_NR_query_module
6368     case TARGET_NR_query_module:
6369         goto unimplemented;
6370 #endif
6371 #ifdef TARGET_NR_nfsservctl
6372     case TARGET_NR_nfsservctl:
6373         goto unimplemented;
6374 #endif
6375     case TARGET_NR_prctl:
6376         switch (arg1)
6377             {
6378             case PR_GET_PDEATHSIG:
6379                 {
6380                     int deathsig;
6381                     ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
6382                     if (!is_error(ret) && arg2
6383                         && put_user_ual(deathsig, arg2))
6384                         goto efault;
6385                 }
6386                 break;
6387             default:
6388                 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
6389                 break;
6390             }
6391         break;
6392 #ifdef TARGET_NR_arch_prctl
6393     case TARGET_NR_arch_prctl:
6394 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
6395         ret = do_arch_prctl(cpu_env, arg1, arg2);
6396         break;
6397 #else
6398         goto unimplemented;
6399 #endif
6400 #endif
6401 #ifdef TARGET_NR_pread
6402     case TARGET_NR_pread:
6403 #ifdef TARGET_ARM
6404         if (((CPUARMState *)cpu_env)->eabi)
6405             arg4 = arg5;
6406 #endif
6407         if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6408             goto efault;
6409         ret = get_errno(pread(arg1, p, arg3, arg4));
6410         unlock_user(p, arg2, ret);
6411         break;
6412     case TARGET_NR_pwrite:
6413 #ifdef TARGET_ARM
6414         if (((CPUARMState *)cpu_env)->eabi)
6415             arg4 = arg5;
6416 #endif
6417         if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6418             goto efault;
6419         ret = get_errno(pwrite(arg1, p, arg3, arg4));
6420         unlock_user(p, arg2, 0);
6421         break;
6422 #endif
6423 #ifdef TARGET_NR_pread64
6424     case TARGET_NR_pread64:
6425         if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
6426             goto efault;
6427         ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
6428         unlock_user(p, arg2, ret);
6429         break;
6430     case TARGET_NR_pwrite64:
6431         if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
6432             goto efault;
6433         ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
6434         unlock_user(p, arg2, 0);
6435         break;
6436 #endif
6437     case TARGET_NR_getcwd:
6438         if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
6439             goto efault;
6440         ret = get_errno(sys_getcwd1(p, arg2));
6441         unlock_user(p, arg1, ret);
6442         break;
6443     case TARGET_NR_capget:
6444         goto unimplemented;
6445     case TARGET_NR_capset:
6446         goto unimplemented;
6447     case TARGET_NR_sigaltstack:
6448 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
6449     defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
6450     defined(TARGET_M68K)
6451         ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
6452         break;
6453 #else
6454         goto unimplemented;
6455 #endif
6456     case TARGET_NR_sendfile:
6457         goto unimplemented;
6458 #ifdef TARGET_NR_getpmsg
6459     case TARGET_NR_getpmsg:
6460         goto unimplemented;
6461 #endif
6462 #ifdef TARGET_NR_putpmsg
6463     case TARGET_NR_putpmsg:
6464         goto unimplemented;
6465 #endif
6466 #ifdef TARGET_NR_vfork
6467     case TARGET_NR_vfork:
6468         ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
6469                         0, 0, 0, 0));
6470         break;
6471 #endif
6472 #ifdef TARGET_NR_ugetrlimit
6473     case TARGET_NR_ugetrlimit:
6474     {
6475 	struct rlimit rlim;
6476 	ret = get_errno(getrlimit(arg1, &rlim));
6477 	if (!is_error(ret)) {
6478 	    struct target_rlimit *target_rlim;
6479             if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
6480                 goto efault;
6481 	    target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
6482 	    target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
6483             unlock_user_struct(target_rlim, arg2, 1);
6484 	}
6485 	break;
6486     }
6487 #endif
6488 #ifdef TARGET_NR_truncate64
6489     case TARGET_NR_truncate64:
6490         if (!(p = lock_user_string(arg1)))
6491             goto efault;
6492 	ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
6493         unlock_user(p, arg1, 0);
6494 	break;
6495 #endif
6496 #ifdef TARGET_NR_ftruncate64
6497     case TARGET_NR_ftruncate64:
6498 	ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
6499 	break;
6500 #endif
6501 #ifdef TARGET_NR_stat64
6502     case TARGET_NR_stat64:
6503         if (!(p = lock_user_string(arg1)))
6504             goto efault;
6505         ret = get_errno(stat(path(p), &st));
6506         unlock_user(p, arg1, 0);
6507         if (!is_error(ret))
6508             ret = host_to_target_stat64(cpu_env, arg2, &st);
6509         break;
6510 #endif
6511 #ifdef TARGET_NR_lstat64
6512     case TARGET_NR_lstat64:
6513         if (!(p = lock_user_string(arg1)))
6514             goto efault;
6515         ret = get_errno(lstat(path(p), &st));
6516         unlock_user(p, arg1, 0);
6517         if (!is_error(ret))
6518             ret = host_to_target_stat64(cpu_env, arg2, &st);
6519         break;
6520 #endif
6521 #ifdef TARGET_NR_fstat64
6522     case TARGET_NR_fstat64:
6523         ret = get_errno(fstat(arg1, &st));
6524         if (!is_error(ret))
6525             ret = host_to_target_stat64(cpu_env, arg2, &st);
6526         break;
6527 #endif
6528 #if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
6529         (defined(__NR_fstatat64) || defined(__NR_newfstatat))
6530 #ifdef TARGET_NR_fstatat64
6531     case TARGET_NR_fstatat64:
6532 #endif
6533 #ifdef TARGET_NR_newfstatat
6534     case TARGET_NR_newfstatat:
6535 #endif
6536         if (!(p = lock_user_string(arg2)))
6537             goto efault;
6538 #ifdef __NR_fstatat64
6539         ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
6540 #else
6541         ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
6542 #endif
6543         if (!is_error(ret))
6544             ret = host_to_target_stat64(cpu_env, arg3, &st);
6545         break;
6546 #endif
6547 #ifdef USE_UID16
6548     case TARGET_NR_lchown:
6549         if (!(p = lock_user_string(arg1)))
6550             goto efault;
6551         ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
6552         unlock_user(p, arg1, 0);
6553         break;
6554     case TARGET_NR_getuid:
6555         ret = get_errno(high2lowuid(getuid()));
6556         break;
6557     case TARGET_NR_getgid:
6558         ret = get_errno(high2lowgid(getgid()));
6559         break;
6560     case TARGET_NR_geteuid:
6561         ret = get_errno(high2lowuid(geteuid()));
6562         break;
6563     case TARGET_NR_getegid:
6564         ret = get_errno(high2lowgid(getegid()));
6565         break;
6566     case TARGET_NR_setreuid:
6567         ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
6568         break;
6569     case TARGET_NR_setregid:
6570         ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
6571         break;
6572     case TARGET_NR_getgroups:
6573         {
6574             int gidsetsize = arg1;
6575             uint16_t *target_grouplist;
6576             gid_t *grouplist;
6577             int i;
6578 
6579             grouplist = alloca(gidsetsize * sizeof(gid_t));
6580             ret = get_errno(getgroups(gidsetsize, grouplist));
6581             if (gidsetsize == 0)
6582                 break;
6583             if (!is_error(ret)) {
6584                 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
6585                 if (!target_grouplist)
6586                     goto efault;
6587                 for(i = 0;i < ret; i++)
6588                     target_grouplist[i] = tswap16(grouplist[i]);
6589                 unlock_user(target_grouplist, arg2, gidsetsize * 2);
6590             }
6591         }
6592         break;
6593     case TARGET_NR_setgroups:
6594         {
6595             int gidsetsize = arg1;
6596             uint16_t *target_grouplist;
6597             gid_t *grouplist;
6598             int i;
6599 
6600             grouplist = alloca(gidsetsize * sizeof(gid_t));
6601             target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
6602             if (!target_grouplist) {
6603                 ret = -TARGET_EFAULT;
6604                 goto fail;
6605             }
6606             for(i = 0;i < gidsetsize; i++)
6607                 grouplist[i] = tswap16(target_grouplist[i]);
6608             unlock_user(target_grouplist, arg2, 0);
6609             ret = get_errno(setgroups(gidsetsize, grouplist));
6610         }
6611         break;
6612     case TARGET_NR_fchown:
6613         ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
6614         break;
6615 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
6616     case TARGET_NR_fchownat:
6617         if (!(p = lock_user_string(arg2)))
6618             goto efault;
6619         ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
6620         unlock_user(p, arg2, 0);
6621         break;
6622 #endif
6623 #ifdef TARGET_NR_setresuid
6624     case TARGET_NR_setresuid:
6625         ret = get_errno(setresuid(low2highuid(arg1),
6626                                   low2highuid(arg2),
6627                                   low2highuid(arg3)));
6628         break;
6629 #endif
6630 #ifdef TARGET_NR_getresuid
6631     case TARGET_NR_getresuid:
6632         {
6633             uid_t ruid, euid, suid;
6634             ret = get_errno(getresuid(&ruid, &euid, &suid));
6635             if (!is_error(ret)) {
6636                 if (put_user_u16(high2lowuid(ruid), arg1)
6637                     || put_user_u16(high2lowuid(euid), arg2)
6638                     || put_user_u16(high2lowuid(suid), arg3))
6639                     goto efault;
6640             }
6641         }
6642         break;
6643 #endif
6644 #ifdef TARGET_NR_getresgid
6645     case TARGET_NR_setresgid:
6646         ret = get_errno(setresgid(low2highgid(arg1),
6647                                   low2highgid(arg2),
6648                                   low2highgid(arg3)));
6649         break;
6650 #endif
6651 #ifdef TARGET_NR_getresgid
6652     case TARGET_NR_getresgid:
6653         {
6654             gid_t rgid, egid, sgid;
6655             ret = get_errno(getresgid(&rgid, &egid, &sgid));
6656             if (!is_error(ret)) {
6657                 if (put_user_u16(high2lowgid(rgid), arg1)
6658                     || put_user_u16(high2lowgid(egid), arg2)
6659                     || put_user_u16(high2lowgid(sgid), arg3))
6660                     goto efault;
6661             }
6662         }
6663         break;
6664 #endif
6665     case TARGET_NR_chown:
6666         if (!(p = lock_user_string(arg1)))
6667             goto efault;
6668         ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
6669         unlock_user(p, arg1, 0);
6670         break;
6671     case TARGET_NR_setuid:
6672         ret = get_errno(setuid(low2highuid(arg1)));
6673         break;
6674     case TARGET_NR_setgid:
6675         ret = get_errno(setgid(low2highgid(arg1)));
6676         break;
6677     case TARGET_NR_setfsuid:
6678         ret = get_errno(setfsuid(arg1));
6679         break;
6680     case TARGET_NR_setfsgid:
6681         ret = get_errno(setfsgid(arg1));
6682         break;
6683 #endif /* USE_UID16 */
6684 
6685 #ifdef TARGET_NR_lchown32
6686     case TARGET_NR_lchown32:
6687         if (!(p = lock_user_string(arg1)))
6688             goto efault;
6689         ret = get_errno(lchown(p, arg2, arg3));
6690         unlock_user(p, arg1, 0);
6691         break;
6692 #endif
6693 #ifdef TARGET_NR_getuid32
6694     case TARGET_NR_getuid32:
6695         ret = get_errno(getuid());
6696         break;
6697 #endif
6698 
6699 #if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
6700    /* Alpha specific */
6701     case TARGET_NR_getxuid:
6702          {
6703             uid_t euid;
6704             euid=geteuid();
6705             ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
6706          }
6707         ret = get_errno(getuid());
6708         break;
6709 #endif
6710 #if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
6711    /* Alpha specific */
6712     case TARGET_NR_getxgid:
6713          {
6714             uid_t egid;
6715             egid=getegid();
6716             ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
6717          }
6718         ret = get_errno(getgid());
6719         break;
6720 #endif
6721 #if defined(TARGET_NR_osf_getsysinfo) && defined(TARGET_ALPHA)
6722     /* Alpha specific */
6723     case TARGET_NR_osf_getsysinfo:
6724         ret = -TARGET_EOPNOTSUPP;
6725         switch (arg1) {
6726           case TARGET_GSI_IEEE_FP_CONTROL:
6727             {
6728                 uint64_t swcr, fpcr = cpu_alpha_load_fpcr (cpu_env);
6729 
6730                 /* Copied from linux ieee_fpcr_to_swcr.  */
6731                 swcr = (fpcr >> 35) & SWCR_STATUS_MASK;
6732                 swcr |= (fpcr >> 36) & SWCR_MAP_DMZ;
6733                 swcr |= (~fpcr >> 48) & (SWCR_TRAP_ENABLE_INV
6734                                         | SWCR_TRAP_ENABLE_DZE
6735                                         | SWCR_TRAP_ENABLE_OVF);
6736                 swcr |= (~fpcr >> 57) & (SWCR_TRAP_ENABLE_UNF
6737                                         | SWCR_TRAP_ENABLE_INE);
6738                 swcr |= (fpcr >> 47) & SWCR_MAP_UMZ;
6739                 swcr |= (~fpcr >> 41) & SWCR_TRAP_ENABLE_DNO;
6740 
6741                 if (put_user_u64 (swcr, arg2))
6742                         goto efault;
6743                 ret = 0;
6744             }
6745             break;
6746 
6747           /* case GSI_IEEE_STATE_AT_SIGNAL:
6748              -- Not implemented in linux kernel.
6749              case GSI_UACPROC:
6750              -- Retrieves current unaligned access state; not much used.
6751              case GSI_PROC_TYPE:
6752              -- Retrieves implver information; surely not used.
6753              case GSI_GET_HWRPB:
6754              -- Grabs a copy of the HWRPB; surely not used.
6755           */
6756         }
6757         break;
6758 #endif
6759 #if defined(TARGET_NR_osf_setsysinfo) && defined(TARGET_ALPHA)
6760     /* Alpha specific */
6761     case TARGET_NR_osf_setsysinfo:
6762         ret = -TARGET_EOPNOTSUPP;
6763         switch (arg1) {
6764           case TARGET_SSI_IEEE_FP_CONTROL:
6765           case TARGET_SSI_IEEE_RAISE_EXCEPTION:
6766             {
6767                 uint64_t swcr, fpcr, orig_fpcr;
6768 
6769                 if (get_user_u64 (swcr, arg2))
6770                     goto efault;
6771                 orig_fpcr = cpu_alpha_load_fpcr (cpu_env);
6772                 fpcr = orig_fpcr & FPCR_DYN_MASK;
6773 
6774                 /* Copied from linux ieee_swcr_to_fpcr.  */
6775                 fpcr |= (swcr & SWCR_STATUS_MASK) << 35;
6776                 fpcr |= (swcr & SWCR_MAP_DMZ) << 36;
6777                 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_INV
6778                                   | SWCR_TRAP_ENABLE_DZE
6779                                   | SWCR_TRAP_ENABLE_OVF)) << 48;
6780                 fpcr |= (~swcr & (SWCR_TRAP_ENABLE_UNF
6781                                   | SWCR_TRAP_ENABLE_INE)) << 57;
6782                 fpcr |= (swcr & SWCR_MAP_UMZ ? FPCR_UNDZ | FPCR_UNFD : 0);
6783                 fpcr |= (~swcr & SWCR_TRAP_ENABLE_DNO) << 41;
6784 
6785                 cpu_alpha_store_fpcr (cpu_env, fpcr);
6786                 ret = 0;
6787 
6788                 if (arg1 == TARGET_SSI_IEEE_RAISE_EXCEPTION) {
6789                     /* Old exceptions are not signaled.  */
6790                     fpcr &= ~(orig_fpcr & FPCR_STATUS_MASK);
6791 
6792                     /* If any exceptions set by this call, and are unmasked,
6793                        send a signal.  */
6794                     /* ??? FIXME */
6795                 }
6796             }
6797             break;
6798 
6799           /* case SSI_NVPAIRS:
6800              -- Used with SSIN_UACPROC to enable unaligned accesses.
6801              case SSI_IEEE_STATE_AT_SIGNAL:
6802              case SSI_IEEE_IGNORE_STATE_AT_SIGNAL:
6803              -- Not implemented in linux kernel
6804           */
6805         }
6806         break;
6807 #endif
6808 #ifdef TARGET_NR_osf_sigprocmask
6809     /* Alpha specific.  */
6810     case TARGET_NR_osf_sigprocmask:
6811         {
6812             abi_ulong mask;
6813             int how = arg1;
6814             sigset_t set, oldset;
6815 
6816             switch(arg1) {
6817             case TARGET_SIG_BLOCK:
6818                 how = SIG_BLOCK;
6819                 break;
6820             case TARGET_SIG_UNBLOCK:
6821                 how = SIG_UNBLOCK;
6822                 break;
6823             case TARGET_SIG_SETMASK:
6824                 how = SIG_SETMASK;
6825                 break;
6826             default:
6827                 ret = -TARGET_EINVAL;
6828                 goto fail;
6829             }
6830             mask = arg2;
6831             target_to_host_old_sigset(&set, &mask);
6832             sigprocmask(arg1, &set, &oldset);
6833             host_to_target_old_sigset(&mask, &oldset);
6834             ret = mask;
6835         }
6836         break;
6837 #endif
6838 
6839 #ifdef TARGET_NR_getgid32
6840     case TARGET_NR_getgid32:
6841         ret = get_errno(getgid());
6842         break;
6843 #endif
6844 #ifdef TARGET_NR_geteuid32
6845     case TARGET_NR_geteuid32:
6846         ret = get_errno(geteuid());
6847         break;
6848 #endif
6849 #ifdef TARGET_NR_getegid32
6850     case TARGET_NR_getegid32:
6851         ret = get_errno(getegid());
6852         break;
6853 #endif
6854 #ifdef TARGET_NR_setreuid32
6855     case TARGET_NR_setreuid32:
6856         ret = get_errno(setreuid(arg1, arg2));
6857         break;
6858 #endif
6859 #ifdef TARGET_NR_setregid32
6860     case TARGET_NR_setregid32:
6861         ret = get_errno(setregid(arg1, arg2));
6862         break;
6863 #endif
6864 #ifdef TARGET_NR_getgroups32
6865     case TARGET_NR_getgroups32:
6866         {
6867             int gidsetsize = arg1;
6868             uint32_t *target_grouplist;
6869             gid_t *grouplist;
6870             int i;
6871 
6872             grouplist = alloca(gidsetsize * sizeof(gid_t));
6873             ret = get_errno(getgroups(gidsetsize, grouplist));
6874             if (gidsetsize == 0)
6875                 break;
6876             if (!is_error(ret)) {
6877                 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
6878                 if (!target_grouplist) {
6879                     ret = -TARGET_EFAULT;
6880                     goto fail;
6881                 }
6882                 for(i = 0;i < ret; i++)
6883                     target_grouplist[i] = tswap32(grouplist[i]);
6884                 unlock_user(target_grouplist, arg2, gidsetsize * 4);
6885             }
6886         }
6887         break;
6888 #endif
6889 #ifdef TARGET_NR_setgroups32
6890     case TARGET_NR_setgroups32:
6891         {
6892             int gidsetsize = arg1;
6893             uint32_t *target_grouplist;
6894             gid_t *grouplist;
6895             int i;
6896 
6897             grouplist = alloca(gidsetsize * sizeof(gid_t));
6898             target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
6899             if (!target_grouplist) {
6900                 ret = -TARGET_EFAULT;
6901                 goto fail;
6902             }
6903             for(i = 0;i < gidsetsize; i++)
6904                 grouplist[i] = tswap32(target_grouplist[i]);
6905             unlock_user(target_grouplist, arg2, 0);
6906             ret = get_errno(setgroups(gidsetsize, grouplist));
6907         }
6908         break;
6909 #endif
6910 #ifdef TARGET_NR_fchown32
6911     case TARGET_NR_fchown32:
6912         ret = get_errno(fchown(arg1, arg2, arg3));
6913         break;
6914 #endif
6915 #ifdef TARGET_NR_setresuid32
6916     case TARGET_NR_setresuid32:
6917         ret = get_errno(setresuid(arg1, arg2, arg3));
6918         break;
6919 #endif
6920 #ifdef TARGET_NR_getresuid32
6921     case TARGET_NR_getresuid32:
6922         {
6923             uid_t ruid, euid, suid;
6924             ret = get_errno(getresuid(&ruid, &euid, &suid));
6925             if (!is_error(ret)) {
6926                 if (put_user_u32(ruid, arg1)
6927                     || put_user_u32(euid, arg2)
6928                     || put_user_u32(suid, arg3))
6929                     goto efault;
6930             }
6931         }
6932         break;
6933 #endif
6934 #ifdef TARGET_NR_setresgid32
6935     case TARGET_NR_setresgid32:
6936         ret = get_errno(setresgid(arg1, arg2, arg3));
6937         break;
6938 #endif
6939 #ifdef TARGET_NR_getresgid32
6940     case TARGET_NR_getresgid32:
6941         {
6942             gid_t rgid, egid, sgid;
6943             ret = get_errno(getresgid(&rgid, &egid, &sgid));
6944             if (!is_error(ret)) {
6945                 if (put_user_u32(rgid, arg1)
6946                     || put_user_u32(egid, arg2)
6947                     || put_user_u32(sgid, arg3))
6948                     goto efault;
6949             }
6950         }
6951         break;
6952 #endif
6953 #ifdef TARGET_NR_chown32
6954     case TARGET_NR_chown32:
6955         if (!(p = lock_user_string(arg1)))
6956             goto efault;
6957         ret = get_errno(chown(p, arg2, arg3));
6958         unlock_user(p, arg1, 0);
6959         break;
6960 #endif
6961 #ifdef TARGET_NR_setuid32
6962     case TARGET_NR_setuid32:
6963         ret = get_errno(setuid(arg1));
6964         break;
6965 #endif
6966 #ifdef TARGET_NR_setgid32
6967     case TARGET_NR_setgid32:
6968         ret = get_errno(setgid(arg1));
6969         break;
6970 #endif
6971 #ifdef TARGET_NR_setfsuid32
6972     case TARGET_NR_setfsuid32:
6973         ret = get_errno(setfsuid(arg1));
6974         break;
6975 #endif
6976 #ifdef TARGET_NR_setfsgid32
6977     case TARGET_NR_setfsgid32:
6978         ret = get_errno(setfsgid(arg1));
6979         break;
6980 #endif
6981 
6982     case TARGET_NR_pivot_root:
6983         goto unimplemented;
6984 #ifdef TARGET_NR_mincore
6985     case TARGET_NR_mincore:
6986         {
6987             void *a;
6988             ret = -TARGET_EFAULT;
6989             if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
6990                 goto efault;
6991             if (!(p = lock_user_string(arg3)))
6992                 goto mincore_fail;
6993             ret = get_errno(mincore(a, arg2, p));
6994             unlock_user(p, arg3, ret);
6995             mincore_fail:
6996             unlock_user(a, arg1, 0);
6997         }
6998         break;
6999 #endif
7000 #ifdef TARGET_NR_arm_fadvise64_64
7001     case TARGET_NR_arm_fadvise64_64:
7002 	{
7003 		/*
7004 		 * arm_fadvise64_64 looks like fadvise64_64 but
7005 		 * with different argument order
7006 		 */
7007 		abi_long temp;
7008 		temp = arg3;
7009 		arg3 = arg4;
7010 		arg4 = temp;
7011 	}
7012 #endif
7013 #if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64) || defined(TARGET_NR_fadvise64)
7014 #ifdef TARGET_NR_fadvise64_64
7015     case TARGET_NR_fadvise64_64:
7016 #endif
7017 #ifdef TARGET_NR_fadvise64
7018     case TARGET_NR_fadvise64:
7019 #endif
7020 #ifdef TARGET_S390X
7021         switch (arg4) {
7022         case 4: arg4 = POSIX_FADV_NOREUSE + 1; break; /* make sure it's an invalid value */
7023         case 5: arg4 = POSIX_FADV_NOREUSE + 2; break; /* ditto */
7024         case 6: arg4 = POSIX_FADV_DONTNEED; break;
7025         case 7: arg4 = POSIX_FADV_NOREUSE; break;
7026         default: break;
7027         }
7028 #endif
7029         ret = -posix_fadvise(arg1, arg2, arg3, arg4);
7030 	break;
7031 #endif
7032 #ifdef TARGET_NR_madvise
7033     case TARGET_NR_madvise:
7034         /* A straight passthrough may not be safe because qemu sometimes
7035            turns private flie-backed mappings into anonymous mappings.
7036            This will break MADV_DONTNEED.
7037            This is a hint, so ignoring and returning success is ok.  */
7038         ret = get_errno(0);
7039         break;
7040 #endif
7041 #if TARGET_ABI_BITS == 32
7042     case TARGET_NR_fcntl64:
7043     {
7044 	int cmd;
7045 	struct flock64 fl;
7046 	struct target_flock64 *target_fl;
7047 #ifdef TARGET_ARM
7048 	struct target_eabi_flock64 *target_efl;
7049 #endif
7050 
7051 	cmd = target_to_host_fcntl_cmd(arg2);
7052 	if (cmd == -TARGET_EINVAL)
7053 		return cmd;
7054 
7055         switch(arg2) {
7056         case TARGET_F_GETLK64:
7057 #ifdef TARGET_ARM
7058             if (((CPUARMState *)cpu_env)->eabi) {
7059                 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
7060                     goto efault;
7061                 fl.l_type = tswap16(target_efl->l_type);
7062                 fl.l_whence = tswap16(target_efl->l_whence);
7063                 fl.l_start = tswap64(target_efl->l_start);
7064                 fl.l_len = tswap64(target_efl->l_len);
7065                 fl.l_pid = tswap32(target_efl->l_pid);
7066                 unlock_user_struct(target_efl, arg3, 0);
7067             } else
7068 #endif
7069             {
7070                 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
7071                     goto efault;
7072                 fl.l_type = tswap16(target_fl->l_type);
7073                 fl.l_whence = tswap16(target_fl->l_whence);
7074                 fl.l_start = tswap64(target_fl->l_start);
7075                 fl.l_len = tswap64(target_fl->l_len);
7076                 fl.l_pid = tswap32(target_fl->l_pid);
7077                 unlock_user_struct(target_fl, arg3, 0);
7078             }
7079             ret = get_errno(fcntl(arg1, cmd, &fl));
7080 	    if (ret == 0) {
7081 #ifdef TARGET_ARM
7082                 if (((CPUARMState *)cpu_env)->eabi) {
7083                     if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
7084                         goto efault;
7085                     target_efl->l_type = tswap16(fl.l_type);
7086                     target_efl->l_whence = tswap16(fl.l_whence);
7087                     target_efl->l_start = tswap64(fl.l_start);
7088                     target_efl->l_len = tswap64(fl.l_len);
7089                     target_efl->l_pid = tswap32(fl.l_pid);
7090                     unlock_user_struct(target_efl, arg3, 1);
7091                 } else
7092 #endif
7093                 {
7094                     if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
7095                         goto efault;
7096                     target_fl->l_type = tswap16(fl.l_type);
7097                     target_fl->l_whence = tswap16(fl.l_whence);
7098                     target_fl->l_start = tswap64(fl.l_start);
7099                     target_fl->l_len = tswap64(fl.l_len);
7100                     target_fl->l_pid = tswap32(fl.l_pid);
7101                     unlock_user_struct(target_fl, arg3, 1);
7102                 }
7103 	    }
7104 	    break;
7105 
7106         case TARGET_F_SETLK64:
7107         case TARGET_F_SETLKW64:
7108 #ifdef TARGET_ARM
7109             if (((CPUARMState *)cpu_env)->eabi) {
7110                 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
7111                     goto efault;
7112                 fl.l_type = tswap16(target_efl->l_type);
7113                 fl.l_whence = tswap16(target_efl->l_whence);
7114                 fl.l_start = tswap64(target_efl->l_start);
7115                 fl.l_len = tswap64(target_efl->l_len);
7116                 fl.l_pid = tswap32(target_efl->l_pid);
7117                 unlock_user_struct(target_efl, arg3, 0);
7118             } else
7119 #endif
7120             {
7121                 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
7122                     goto efault;
7123                 fl.l_type = tswap16(target_fl->l_type);
7124                 fl.l_whence = tswap16(target_fl->l_whence);
7125                 fl.l_start = tswap64(target_fl->l_start);
7126                 fl.l_len = tswap64(target_fl->l_len);
7127                 fl.l_pid = tswap32(target_fl->l_pid);
7128                 unlock_user_struct(target_fl, arg3, 0);
7129             }
7130             ret = get_errno(fcntl(arg1, cmd, &fl));
7131 	    break;
7132         default:
7133             ret = do_fcntl(arg1, arg2, arg3);
7134             break;
7135         }
7136 	break;
7137     }
7138 #endif
7139 #ifdef TARGET_NR_cacheflush
7140     case TARGET_NR_cacheflush:
7141         /* self-modifying code is handled automatically, so nothing needed */
7142         ret = 0;
7143         break;
7144 #endif
7145 #ifdef TARGET_NR_security
7146     case TARGET_NR_security:
7147         goto unimplemented;
7148 #endif
7149 #ifdef TARGET_NR_getpagesize
7150     case TARGET_NR_getpagesize:
7151         ret = TARGET_PAGE_SIZE;
7152         break;
7153 #endif
7154     case TARGET_NR_gettid:
7155         ret = get_errno(gettid());
7156         break;
7157 #ifdef TARGET_NR_readahead
7158     case TARGET_NR_readahead:
7159 #if TARGET_ABI_BITS == 32
7160 #ifdef TARGET_ARM
7161         if (((CPUARMState *)cpu_env)->eabi)
7162         {
7163             arg2 = arg3;
7164             arg3 = arg4;
7165             arg4 = arg5;
7166         }
7167 #endif
7168         ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
7169 #else
7170         ret = get_errno(readahead(arg1, arg2, arg3));
7171 #endif
7172         break;
7173 #endif
7174 #ifdef TARGET_NR_setxattr
7175     case TARGET_NR_setxattr:
7176     case TARGET_NR_lsetxattr:
7177     case TARGET_NR_fsetxattr:
7178     case TARGET_NR_getxattr:
7179     case TARGET_NR_lgetxattr:
7180     case TARGET_NR_fgetxattr:
7181     case TARGET_NR_listxattr:
7182     case TARGET_NR_llistxattr:
7183     case TARGET_NR_flistxattr:
7184     case TARGET_NR_removexattr:
7185     case TARGET_NR_lremovexattr:
7186     case TARGET_NR_fremovexattr:
7187         ret = -TARGET_EOPNOTSUPP;
7188         break;
7189 #endif
7190 #ifdef TARGET_NR_set_thread_area
7191     case TARGET_NR_set_thread_area:
7192 #if defined(TARGET_MIPS)
7193       ((CPUMIPSState *) cpu_env)->tls_value = arg1;
7194       ret = 0;
7195       break;
7196 #elif defined(TARGET_CRIS)
7197       if (arg1 & 0xff)
7198           ret = -TARGET_EINVAL;
7199       else {
7200           ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
7201           ret = 0;
7202       }
7203       break;
7204 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
7205       ret = do_set_thread_area(cpu_env, arg1);
7206       break;
7207 #else
7208       goto unimplemented_nowarn;
7209 #endif
7210 #endif
7211 #ifdef TARGET_NR_get_thread_area
7212     case TARGET_NR_get_thread_area:
7213 #if defined(TARGET_I386) && defined(TARGET_ABI32)
7214         ret = do_get_thread_area(cpu_env, arg1);
7215 #else
7216         goto unimplemented_nowarn;
7217 #endif
7218 #endif
7219 #ifdef TARGET_NR_getdomainname
7220     case TARGET_NR_getdomainname:
7221         goto unimplemented_nowarn;
7222 #endif
7223 
7224 #ifdef TARGET_NR_clock_gettime
7225     case TARGET_NR_clock_gettime:
7226     {
7227         struct timespec ts;
7228         ret = get_errno(clock_gettime(arg1, &ts));
7229         if (!is_error(ret)) {
7230             host_to_target_timespec(arg2, &ts);
7231         }
7232         break;
7233     }
7234 #endif
7235 #ifdef TARGET_NR_clock_getres
7236     case TARGET_NR_clock_getres:
7237     {
7238         struct timespec ts;
7239         ret = get_errno(clock_getres(arg1, &ts));
7240         if (!is_error(ret)) {
7241             host_to_target_timespec(arg2, &ts);
7242         }
7243         break;
7244     }
7245 #endif
7246 #ifdef TARGET_NR_clock_nanosleep
7247     case TARGET_NR_clock_nanosleep:
7248     {
7249         struct timespec ts;
7250         target_to_host_timespec(&ts, arg3);
7251         ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
7252         if (arg4)
7253             host_to_target_timespec(arg4, &ts);
7254         break;
7255     }
7256 #endif
7257 
7258 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
7259     case TARGET_NR_set_tid_address:
7260         ret = get_errno(set_tid_address((int *)g2h(arg1)));
7261         break;
7262 #endif
7263 
7264 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
7265     case TARGET_NR_tkill:
7266         ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
7267         break;
7268 #endif
7269 
7270 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
7271     case TARGET_NR_tgkill:
7272 	ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
7273                         target_to_host_signal(arg3)));
7274 	break;
7275 #endif
7276 
7277 #ifdef TARGET_NR_set_robust_list
7278     case TARGET_NR_set_robust_list:
7279 	goto unimplemented_nowarn;
7280 #endif
7281 
7282 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
7283     case TARGET_NR_utimensat:
7284         {
7285             struct timespec *tsp, ts[2];
7286             if (!arg3) {
7287                 tsp = NULL;
7288             } else {
7289                 target_to_host_timespec(ts, arg3);
7290                 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
7291                 tsp = ts;
7292             }
7293             if (!arg2)
7294                 ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
7295             else {
7296                 if (!(p = lock_user_string(arg2))) {
7297                     ret = -TARGET_EFAULT;
7298                     goto fail;
7299                 }
7300                 ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
7301                 unlock_user(p, arg2, 0);
7302             }
7303         }
7304 	break;
7305 #endif
7306 #if defined(CONFIG_USE_NPTL)
7307     case TARGET_NR_futex:
7308         ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
7309         break;
7310 #endif
7311 #if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
7312     case TARGET_NR_inotify_init:
7313         ret = get_errno(sys_inotify_init());
7314         break;
7315 #endif
7316 #ifdef CONFIG_INOTIFY1
7317 #if defined(TARGET_NR_inotify_init1) && defined(__NR_inotify_init1)
7318     case TARGET_NR_inotify_init1:
7319         ret = get_errno(sys_inotify_init1(arg1));
7320         break;
7321 #endif
7322 #endif
7323 #if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
7324     case TARGET_NR_inotify_add_watch:
7325         p = lock_user_string(arg2);
7326         ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
7327         unlock_user(p, arg2, 0);
7328         break;
7329 #endif
7330 #if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
7331     case TARGET_NR_inotify_rm_watch:
7332         ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
7333         break;
7334 #endif
7335 
7336 #if defined(TARGET_NR_mq_open) && defined(__NR_mq_open)
7337     case TARGET_NR_mq_open:
7338         {
7339             struct mq_attr posix_mq_attr;
7340 
7341             p = lock_user_string(arg1 - 1);
7342             if (arg4 != 0)
7343                 copy_from_user_mq_attr (&posix_mq_attr, arg4);
7344             ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
7345             unlock_user (p, arg1, 0);
7346         }
7347         break;
7348 
7349     case TARGET_NR_mq_unlink:
7350         p = lock_user_string(arg1 - 1);
7351         ret = get_errno(mq_unlink(p));
7352         unlock_user (p, arg1, 0);
7353         break;
7354 
7355     case TARGET_NR_mq_timedsend:
7356         {
7357             struct timespec ts;
7358 
7359             p = lock_user (VERIFY_READ, arg2, arg3, 1);
7360             if (arg5 != 0) {
7361                 target_to_host_timespec(&ts, arg5);
7362                 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
7363                 host_to_target_timespec(arg5, &ts);
7364             }
7365             else
7366                 ret = get_errno(mq_send(arg1, p, arg3, arg4));
7367             unlock_user (p, arg2, arg3);
7368         }
7369         break;
7370 
7371     case TARGET_NR_mq_timedreceive:
7372         {
7373             struct timespec ts;
7374             unsigned int prio;
7375 
7376             p = lock_user (VERIFY_READ, arg2, arg3, 1);
7377             if (arg5 != 0) {
7378                 target_to_host_timespec(&ts, arg5);
7379                 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
7380                 host_to_target_timespec(arg5, &ts);
7381             }
7382             else
7383                 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
7384             unlock_user (p, arg2, arg3);
7385             if (arg4 != 0)
7386                 put_user_u32(prio, arg4);
7387         }
7388         break;
7389 
7390     /* Not implemented for now... */
7391 /*     case TARGET_NR_mq_notify: */
7392 /*         break; */
7393 
7394     case TARGET_NR_mq_getsetattr:
7395         {
7396             struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
7397             ret = 0;
7398             if (arg3 != 0) {
7399                 ret = mq_getattr(arg1, &posix_mq_attr_out);
7400                 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
7401             }
7402             if (arg2 != 0) {
7403                 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
7404                 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
7405             }
7406 
7407         }
7408         break;
7409 #endif
7410 
7411 #ifdef CONFIG_SPLICE
7412 #ifdef TARGET_NR_tee
7413     case TARGET_NR_tee:
7414         {
7415             ret = get_errno(tee(arg1,arg2,arg3,arg4));
7416         }
7417         break;
7418 #endif
7419 #ifdef TARGET_NR_splice
7420     case TARGET_NR_splice:
7421         {
7422             loff_t loff_in, loff_out;
7423             loff_t *ploff_in = NULL, *ploff_out = NULL;
7424             if(arg2) {
7425                 get_user_u64(loff_in, arg2);
7426                 ploff_in = &loff_in;
7427             }
7428             if(arg4) {
7429                 get_user_u64(loff_out, arg2);
7430                 ploff_out = &loff_out;
7431             }
7432             ret = get_errno(splice(arg1, ploff_in, arg3, ploff_out, arg5, arg6));
7433         }
7434         break;
7435 #endif
7436 #ifdef TARGET_NR_vmsplice
7437 	case TARGET_NR_vmsplice:
7438         {
7439             int count = arg3;
7440             struct iovec *vec;
7441 
7442             vec = alloca(count * sizeof(struct iovec));
7443             if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
7444                 goto efault;
7445             ret = get_errno(vmsplice(arg1, vec, count, arg4));
7446             unlock_iovec(vec, arg2, count, 0);
7447         }
7448         break;
7449 #endif
7450 #endif /* CONFIG_SPLICE */
7451 #ifdef CONFIG_EVENTFD
7452 #if defined(TARGET_NR_eventfd)
7453     case TARGET_NR_eventfd:
7454         ret = get_errno(eventfd(arg1, 0));
7455         break;
7456 #endif
7457 #if defined(TARGET_NR_eventfd2)
7458     case TARGET_NR_eventfd2:
7459         ret = get_errno(eventfd(arg1, arg2));
7460         break;
7461 #endif
7462 #endif /* CONFIG_EVENTFD  */
7463 #if defined(CONFIG_FALLOCATE) && defined(TARGET_NR_fallocate)
7464     case TARGET_NR_fallocate:
7465         ret = get_errno(fallocate(arg1, arg2, arg3, arg4));
7466         break;
7467 #endif
7468 #if defined(CONFIG_SYNC_FILE_RANGE)
7469 #if defined(TARGET_NR_sync_file_range)
7470     case TARGET_NR_sync_file_range:
7471 #if TARGET_ABI_BITS == 32
7472         ret = get_errno(sync_file_range(arg1, target_offset64(arg2, arg3),
7473                                         target_offset64(arg4, arg5), arg6));
7474 #else
7475         ret = get_errno(sync_file_range(arg1, arg2, arg3, arg4));
7476 #endif
7477         break;
7478 #endif
7479 #if defined(TARGET_NR_sync_file_range2)
7480     case TARGET_NR_sync_file_range2:
7481         /* This is like sync_file_range but the arguments are reordered */
7482 #if TARGET_ABI_BITS == 32
7483         ret = get_errno(sync_file_range(arg1, target_offset64(arg3, arg4),
7484                                         target_offset64(arg5, arg6), arg2));
7485 #else
7486         ret = get_errno(sync_file_range(arg1, arg3, arg4, arg2));
7487 #endif
7488         break;
7489 #endif
7490 #endif
7491     default:
7492     unimplemented:
7493         gemu_log("qemu: Unsupported syscall: %d\n", num);
7494 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
7495     unimplemented_nowarn:
7496 #endif
7497         ret = -TARGET_ENOSYS;
7498         break;
7499     }
7500 fail:
7501 #ifdef DEBUG
7502     gemu_log(" = " TARGET_ABI_FMT_ld "\n", ret);
7503 #endif
7504     if(do_strace)
7505         print_syscall_ret(num, ret);
7506     return ret;
7507 efault:
7508     ret = -TARGET_EFAULT;
7509     goto fail;
7510 }
7511