xref: /openbmc/qemu/linux-user/syscall.c (revision 7ab240ad)
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, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
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 <sys/types.h>
31 #include <sys/ipc.h>
32 #include <sys/msg.h>
33 #include <sys/wait.h>
34 #include <sys/time.h>
35 #include <sys/stat.h>
36 #include <sys/mount.h>
37 #include <sys/prctl.h>
38 #include <sys/resource.h>
39 #include <sys/mman.h>
40 #include <sys/swap.h>
41 #include <signal.h>
42 #include <sched.h>
43 #include <sys/socket.h>
44 #include <sys/uio.h>
45 #include <sys/poll.h>
46 #include <sys/times.h>
47 #include <sys/shm.h>
48 #include <sys/sem.h>
49 #include <sys/statfs.h>
50 #include <utime.h>
51 #include <sys/sysinfo.h>
52 //#include <sys/user.h>
53 #include <netinet/ip.h>
54 #include <netinet/tcp.h>
55 
56 #define termios host_termios
57 #define winsize host_winsize
58 #define termio host_termio
59 #define sgttyb host_sgttyb /* same as target */
60 #define tchars host_tchars /* same as target */
61 #define ltchars host_ltchars /* same as target */
62 
63 #include <linux/termios.h>
64 #include <linux/unistd.h>
65 #include <linux/utsname.h>
66 #include <linux/cdrom.h>
67 #include <linux/hdreg.h>
68 #include <linux/soundcard.h>
69 #include <linux/dirent.h>
70 #include <linux/kd.h>
71 
72 #include "qemu.h"
73 
74 //#define DEBUG
75 
76 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC) \
77     || defined(TARGET_M68K) || defined(TARGET_SH4) || defined(TARGET_CRIS)
78 /* 16 bit uid wrappers emulation */
79 #define USE_UID16
80 #endif
81 
82 //#include <linux/msdos_fs.h>
83 #define	VFAT_IOCTL_READDIR_BOTH		_IOR('r', 1, struct dirent [2])
84 #define	VFAT_IOCTL_READDIR_SHORT	_IOR('r', 2, struct dirent [2])
85 
86 
87 #undef _syscall0
88 #undef _syscall1
89 #undef _syscall2
90 #undef _syscall3
91 #undef _syscall4
92 #undef _syscall5
93 #undef _syscall6
94 
95 #define _syscall0(type,name)		\
96 type name (void)			\
97 {					\
98 	return syscall(__NR_##name);	\
99 }
100 
101 #define _syscall1(type,name,type1,arg1)		\
102 type name (type1 arg1)				\
103 {						\
104 	return syscall(__NR_##name, arg1);	\
105 }
106 
107 #define _syscall2(type,name,type1,arg1,type2,arg2)	\
108 type name (type1 arg1,type2 arg2)			\
109 {							\
110 	return syscall(__NR_##name, arg1, arg2);	\
111 }
112 
113 #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3)	\
114 type name (type1 arg1,type2 arg2,type3 arg3)			\
115 {								\
116 	return syscall(__NR_##name, arg1, arg2, arg3);		\
117 }
118 
119 #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4)	\
120 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4)				\
121 {										\
122 	return syscall(__NR_##name, arg1, arg2, arg3, arg4);			\
123 }
124 
125 #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,	\
126 		  type5,arg5)							\
127 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5)		\
128 {										\
129 	return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5);		\
130 }
131 
132 
133 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,	\
134 		  type5,arg5,type6,arg6)					\
135 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6)	\
136 {										\
137 	return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6);	\
138 }
139 
140 
141 #define __NR_sys_uname __NR_uname
142 #define __NR_sys_faccessat __NR_faccessat
143 #define __NR_sys_fchmodat __NR_fchmodat
144 #define __NR_sys_fchownat __NR_fchownat
145 #define __NR_sys_getcwd1 __NR_getcwd
146 #define __NR_sys_getdents __NR_getdents
147 #define __NR_sys_getdents64 __NR_getdents64
148 #define __NR_sys_getpriority __NR_getpriority
149 #define __NR_sys_linkat __NR_linkat
150 #define __NR_sys_mkdirat __NR_mkdirat
151 #define __NR_sys_mknodat __NR_mknodat
152 #define __NR_sys_openat __NR_openat
153 #define __NR_sys_readlinkat __NR_readlinkat
154 #define __NR_sys_renameat __NR_renameat
155 #define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
156 #define __NR_sys_symlinkat __NR_symlinkat
157 #define __NR_sys_syslog __NR_syslog
158 #define __NR_sys_tgkill __NR_tgkill
159 #define __NR_sys_tkill __NR_tkill
160 #define __NR_sys_unlinkat __NR_unlinkat
161 #define __NR_sys_utimensat __NR_utimensat
162 
163 #if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
164 #define __NR__llseek __NR_lseek
165 #endif
166 
167 #ifdef __NR_gettid
168 _syscall0(int, gettid)
169 #else
170 /* This is a replacement for the host gettid() and must return a host
171    errno. */
172 static int gettid(void) {
173     return -ENOSYS;
174 }
175 #endif
176 _syscall1(int,sys_uname,struct new_utsname *,buf)
177 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
178 _syscall4(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode,int,flags)
179 #endif
180 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
181 _syscall4(int,sys_fchmodat,int,dirfd,const char *,pathname,
182           mode_t,mode,int,flags)
183 #endif
184 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
185 _syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
186           uid_t,owner,gid_t,group,int,flags)
187 #endif
188 _syscall2(int,sys_getcwd1,char *,buf,size_t,size)
189 _syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
190 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
191 _syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
192 #endif
193 _syscall2(int, sys_getpriority, int, which, int, who);
194 _syscall5(int, _llseek,  uint,  fd, ulong, hi, ulong, lo,
195           loff_t *, res, uint, wh);
196 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
197 _syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
198 	  int,newdirfd,const char *,newpath,int,flags)
199 #endif
200 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
201 _syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
202 #endif
203 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
204 _syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
205           mode_t,mode,dev_t,dev)
206 #endif
207 #if defined(TARGET_NR_openat) && defined(__NR_openat)
208 _syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
209 #endif
210 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
211 _syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
212           char *,buf,size_t,bufsize)
213 #endif
214 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
215 _syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
216           int,newdirfd,const char *,newpath)
217 #endif
218 _syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
219 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
220 _syscall3(int,sys_symlinkat,const char *,oldpath,
221           int,newdirfd,const char *,newpath)
222 #endif
223 _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
224 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
225 _syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
226 #endif
227 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
228 _syscall2(int,sys_tkill,int,tid,int,sig)
229 #endif
230 #ifdef __NR_exit_group
231 _syscall1(int,exit_group,int,error_code)
232 #endif
233 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
234 _syscall1(int,set_tid_address,int *,tidptr)
235 #endif
236 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
237 _syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
238 #endif
239 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
240 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
241           const struct timespec *,tsp,int,flags)
242 #endif
243 
244 extern int personality(int);
245 extern int flock(int, int);
246 extern int setfsuid(int);
247 extern int setfsgid(int);
248 extern int setresuid(uid_t, uid_t, uid_t);
249 extern int getresuid(uid_t *, uid_t *, uid_t *);
250 extern int setresgid(gid_t, gid_t, gid_t);
251 extern int getresgid(gid_t *, gid_t *, gid_t *);
252 extern int setgroups(int, gid_t *);
253 
254 #define ERRNO_TABLE_SIZE 1200
255 
256 /* target_to_host_errno_table[] is initialized from
257  * host_to_target_errno_table[] in syscall_init(). */
258 static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
259 };
260 
261 /*
262  * This list is the union of errno values overridden in asm-<arch>/errno.h
263  * minus the errnos that are not actually generic to all archs.
264  */
265 static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
266     [EIDRM]		= TARGET_EIDRM,
267     [ECHRNG]		= TARGET_ECHRNG,
268     [EL2NSYNC]		= TARGET_EL2NSYNC,
269     [EL3HLT]		= TARGET_EL3HLT,
270     [EL3RST]		= TARGET_EL3RST,
271     [ELNRNG]		= TARGET_ELNRNG,
272     [EUNATCH]		= TARGET_EUNATCH,
273     [ENOCSI]		= TARGET_ENOCSI,
274     [EL2HLT]		= TARGET_EL2HLT,
275     [EDEADLK]		= TARGET_EDEADLK,
276     [ENOLCK]		= TARGET_ENOLCK,
277     [EBADE]		= TARGET_EBADE,
278     [EBADR]		= TARGET_EBADR,
279     [EXFULL]		= TARGET_EXFULL,
280     [ENOANO]		= TARGET_ENOANO,
281     [EBADRQC]		= TARGET_EBADRQC,
282     [EBADSLT]		= TARGET_EBADSLT,
283     [EBFONT]		= TARGET_EBFONT,
284     [ENOSTR]		= TARGET_ENOSTR,
285     [ENODATA]		= TARGET_ENODATA,
286     [ETIME]		= TARGET_ETIME,
287     [ENOSR]		= TARGET_ENOSR,
288     [ENONET]		= TARGET_ENONET,
289     [ENOPKG]		= TARGET_ENOPKG,
290     [EREMOTE]		= TARGET_EREMOTE,
291     [ENOLINK]		= TARGET_ENOLINK,
292     [EADV]		= TARGET_EADV,
293     [ESRMNT]		= TARGET_ESRMNT,
294     [ECOMM]		= TARGET_ECOMM,
295     [EPROTO]		= TARGET_EPROTO,
296     [EDOTDOT]		= TARGET_EDOTDOT,
297     [EMULTIHOP]		= TARGET_EMULTIHOP,
298     [EBADMSG]		= TARGET_EBADMSG,
299     [ENAMETOOLONG]	= TARGET_ENAMETOOLONG,
300     [EOVERFLOW]		= TARGET_EOVERFLOW,
301     [ENOTUNIQ]		= TARGET_ENOTUNIQ,
302     [EBADFD]		= TARGET_EBADFD,
303     [EREMCHG]		= TARGET_EREMCHG,
304     [ELIBACC]		= TARGET_ELIBACC,
305     [ELIBBAD]		= TARGET_ELIBBAD,
306     [ELIBSCN]		= TARGET_ELIBSCN,
307     [ELIBMAX]		= TARGET_ELIBMAX,
308     [ELIBEXEC]		= TARGET_ELIBEXEC,
309     [EILSEQ]		= TARGET_EILSEQ,
310     [ENOSYS]		= TARGET_ENOSYS,
311     [ELOOP]		= TARGET_ELOOP,
312     [ERESTART]		= TARGET_ERESTART,
313     [ESTRPIPE]		= TARGET_ESTRPIPE,
314     [ENOTEMPTY]		= TARGET_ENOTEMPTY,
315     [EUSERS]		= TARGET_EUSERS,
316     [ENOTSOCK]		= TARGET_ENOTSOCK,
317     [EDESTADDRREQ]	= TARGET_EDESTADDRREQ,
318     [EMSGSIZE]		= TARGET_EMSGSIZE,
319     [EPROTOTYPE]	= TARGET_EPROTOTYPE,
320     [ENOPROTOOPT]	= TARGET_ENOPROTOOPT,
321     [EPROTONOSUPPORT]	= TARGET_EPROTONOSUPPORT,
322     [ESOCKTNOSUPPORT]	= TARGET_ESOCKTNOSUPPORT,
323     [EOPNOTSUPP]	= TARGET_EOPNOTSUPP,
324     [EPFNOSUPPORT]	= TARGET_EPFNOSUPPORT,
325     [EAFNOSUPPORT]	= TARGET_EAFNOSUPPORT,
326     [EADDRINUSE]	= TARGET_EADDRINUSE,
327     [EADDRNOTAVAIL]	= TARGET_EADDRNOTAVAIL,
328     [ENETDOWN]		= TARGET_ENETDOWN,
329     [ENETUNREACH]	= TARGET_ENETUNREACH,
330     [ENETRESET]		= TARGET_ENETRESET,
331     [ECONNABORTED]	= TARGET_ECONNABORTED,
332     [ECONNRESET]	= TARGET_ECONNRESET,
333     [ENOBUFS]		= TARGET_ENOBUFS,
334     [EISCONN]		= TARGET_EISCONN,
335     [ENOTCONN]		= TARGET_ENOTCONN,
336     [EUCLEAN]		= TARGET_EUCLEAN,
337     [ENOTNAM]		= TARGET_ENOTNAM,
338     [ENAVAIL]		= TARGET_ENAVAIL,
339     [EISNAM]		= TARGET_EISNAM,
340     [EREMOTEIO]		= TARGET_EREMOTEIO,
341     [ESHUTDOWN]		= TARGET_ESHUTDOWN,
342     [ETOOMANYREFS]	= TARGET_ETOOMANYREFS,
343     [ETIMEDOUT]		= TARGET_ETIMEDOUT,
344     [ECONNREFUSED]	= TARGET_ECONNREFUSED,
345     [EHOSTDOWN]		= TARGET_EHOSTDOWN,
346     [EHOSTUNREACH]	= TARGET_EHOSTUNREACH,
347     [EALREADY]		= TARGET_EALREADY,
348     [EINPROGRESS]	= TARGET_EINPROGRESS,
349     [ESTALE]		= TARGET_ESTALE,
350     [ECANCELED]		= TARGET_ECANCELED,
351     [ENOMEDIUM]		= TARGET_ENOMEDIUM,
352     [EMEDIUMTYPE]	= TARGET_EMEDIUMTYPE,
353 #ifdef ENOKEY
354     [ENOKEY]		= TARGET_ENOKEY,
355 #endif
356 #ifdef EKEYEXPIRED
357     [EKEYEXPIRED]	= TARGET_EKEYEXPIRED,
358 #endif
359 #ifdef EKEYREVOKED
360     [EKEYREVOKED]	= TARGET_EKEYREVOKED,
361 #endif
362 #ifdef EKEYREJECTED
363     [EKEYREJECTED]	= TARGET_EKEYREJECTED,
364 #endif
365 #ifdef EOWNERDEAD
366     [EOWNERDEAD]	= TARGET_EOWNERDEAD,
367 #endif
368 #ifdef ENOTRECOVERABLE
369     [ENOTRECOVERABLE]	= TARGET_ENOTRECOVERABLE,
370 #endif
371 };
372 
373 static inline int host_to_target_errno(int err)
374 {
375     if(host_to_target_errno_table[err])
376         return host_to_target_errno_table[err];
377     return err;
378 }
379 
380 static inline int target_to_host_errno(int err)
381 {
382     if (target_to_host_errno_table[err])
383         return target_to_host_errno_table[err];
384     return err;
385 }
386 
387 static inline abi_long get_errno(abi_long ret)
388 {
389     if (ret == -1)
390         return -host_to_target_errno(errno);
391     else
392         return ret;
393 }
394 
395 static inline int is_error(abi_long ret)
396 {
397     return (abi_ulong)ret >= (abi_ulong)(-4096);
398 }
399 
400 char *target_strerror(int err)
401 {
402     return strerror(target_to_host_errno(err));
403 }
404 
405 static abi_ulong target_brk;
406 static abi_ulong target_original_brk;
407 
408 void target_set_brk(abi_ulong new_brk)
409 {
410     target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
411 }
412 
413 /* do_brk() must return target values and target errnos. */
414 abi_long do_brk(abi_ulong new_brk)
415 {
416     abi_ulong brk_page;
417     abi_long mapped_addr;
418     int	new_alloc_size;
419 
420     if (!new_brk)
421         return target_brk;
422     if (new_brk < target_original_brk)
423         return target_brk;
424 
425     brk_page = HOST_PAGE_ALIGN(target_brk);
426 
427     /* If the new brk is less than this, set it and we're done... */
428     if (new_brk < brk_page) {
429 	target_brk = new_brk;
430     	return target_brk;
431     }
432 
433     /* We need to allocate more memory after the brk... */
434     new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
435     mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
436                                         PROT_READ|PROT_WRITE,
437                                         MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
438 
439     if (!is_error(mapped_addr))
440 	target_brk = new_brk;
441 
442     return target_brk;
443 }
444 
445 static inline abi_long copy_from_user_fdset(fd_set *fds,
446                                             abi_ulong target_fds_addr,
447                                             int n)
448 {
449     int i, nw, j, k;
450     abi_ulong b, *target_fds;
451 
452     nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
453     if (!(target_fds = lock_user(VERIFY_READ,
454                                  target_fds_addr,
455                                  sizeof(abi_ulong) * nw,
456                                  1)))
457         return -TARGET_EFAULT;
458 
459     FD_ZERO(fds);
460     k = 0;
461     for (i = 0; i < nw; i++) {
462         /* grab the abi_ulong */
463         __get_user(b, &target_fds[i]);
464         for (j = 0; j < TARGET_ABI_BITS; j++) {
465             /* check the bit inside the abi_ulong */
466             if ((b >> j) & 1)
467                 FD_SET(k, fds);
468             k++;
469         }
470     }
471 
472     unlock_user(target_fds, target_fds_addr, 0);
473 
474     return 0;
475 }
476 
477 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
478                                           const fd_set *fds,
479                                           int n)
480 {
481     int i, nw, j, k;
482     abi_long v;
483     abi_ulong *target_fds;
484 
485     nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
486     if (!(target_fds = lock_user(VERIFY_WRITE,
487                                  target_fds_addr,
488                                  sizeof(abi_ulong) * nw,
489                                  0)))
490         return -TARGET_EFAULT;
491 
492     k = 0;
493     for (i = 0; i < nw; i++) {
494         v = 0;
495         for (j = 0; j < TARGET_ABI_BITS; j++) {
496             v |= ((FD_ISSET(k, fds) != 0) << j);
497             k++;
498         }
499         __put_user(v, &target_fds[i]);
500     }
501 
502     unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
503 
504     return 0;
505 }
506 
507 #if defined(__alpha__)
508 #define HOST_HZ 1024
509 #else
510 #define HOST_HZ 100
511 #endif
512 
513 static inline abi_long host_to_target_clock_t(long ticks)
514 {
515 #if HOST_HZ == TARGET_HZ
516     return ticks;
517 #else
518     return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
519 #endif
520 }
521 
522 static inline abi_long host_to_target_rusage(abi_ulong target_addr,
523                                              const struct rusage *rusage)
524 {
525     struct target_rusage *target_rusage;
526 
527     if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
528         return -TARGET_EFAULT;
529     target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
530     target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
531     target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
532     target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
533     target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
534     target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
535     target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
536     target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
537     target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
538     target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
539     target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
540     target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
541     target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
542     target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
543     target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
544     target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
545     target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
546     target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
547     unlock_user_struct(target_rusage, target_addr, 1);
548 
549     return 0;
550 }
551 
552 static inline abi_long copy_from_user_timeval(struct timeval *tv,
553                                               abi_ulong target_tv_addr)
554 {
555     struct target_timeval *target_tv;
556 
557     if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
558         return -TARGET_EFAULT;
559 
560     __get_user(tv->tv_sec, &target_tv->tv_sec);
561     __get_user(tv->tv_usec, &target_tv->tv_usec);
562 
563     unlock_user_struct(target_tv, target_tv_addr, 0);
564 
565     return 0;
566 }
567 
568 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
569                                             const struct timeval *tv)
570 {
571     struct target_timeval *target_tv;
572 
573     if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
574         return -TARGET_EFAULT;
575 
576     __put_user(tv->tv_sec, &target_tv->tv_sec);
577     __put_user(tv->tv_usec, &target_tv->tv_usec);
578 
579     unlock_user_struct(target_tv, target_tv_addr, 1);
580 
581     return 0;
582 }
583 
584 
585 /* do_select() must return target values and target errnos. */
586 static abi_long do_select(int n,
587                           abi_ulong rfd_addr, abi_ulong wfd_addr,
588                           abi_ulong efd_addr, abi_ulong target_tv_addr)
589 {
590     fd_set rfds, wfds, efds;
591     fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
592     struct timeval tv, *tv_ptr;
593     abi_long ret;
594 
595     if (rfd_addr) {
596         if (copy_from_user_fdset(&rfds, rfd_addr, n))
597             return -TARGET_EFAULT;
598         rfds_ptr = &rfds;
599     } else {
600         rfds_ptr = NULL;
601     }
602     if (wfd_addr) {
603         if (copy_from_user_fdset(&wfds, wfd_addr, n))
604             return -TARGET_EFAULT;
605         wfds_ptr = &wfds;
606     } else {
607         wfds_ptr = NULL;
608     }
609     if (efd_addr) {
610         if (copy_from_user_fdset(&efds, efd_addr, n))
611             return -TARGET_EFAULT;
612         efds_ptr = &efds;
613     } else {
614         efds_ptr = NULL;
615     }
616 
617     if (target_tv_addr) {
618         if (copy_from_user_timeval(&tv, target_tv_addr))
619             return -TARGET_EFAULT;
620         tv_ptr = &tv;
621     } else {
622         tv_ptr = NULL;
623     }
624 
625     ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
626 
627     if (!is_error(ret)) {
628         if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
629             return -TARGET_EFAULT;
630         if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
631             return -TARGET_EFAULT;
632         if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
633             return -TARGET_EFAULT;
634 
635         if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
636             return -TARGET_EFAULT;
637     }
638 
639     return ret;
640 }
641 
642 static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
643                                                abi_ulong target_addr,
644                                                socklen_t len)
645 {
646     struct target_sockaddr *target_saddr;
647 
648     target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
649     if (!target_saddr)
650         return -TARGET_EFAULT;
651     memcpy(addr, target_saddr, len);
652     addr->sa_family = tswap16(target_saddr->sa_family);
653     unlock_user(target_saddr, target_addr, 0);
654 
655     return 0;
656 }
657 
658 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
659                                                struct sockaddr *addr,
660                                                socklen_t len)
661 {
662     struct target_sockaddr *target_saddr;
663 
664     target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
665     if (!target_saddr)
666         return -TARGET_EFAULT;
667     memcpy(target_saddr, addr, len);
668     target_saddr->sa_family = tswap16(addr->sa_family);
669     unlock_user(target_saddr, target_addr, len);
670 
671     return 0;
672 }
673 
674 /* ??? Should this also swap msgh->name?  */
675 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
676                                            struct target_msghdr *target_msgh)
677 {
678     struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
679     abi_long msg_controllen;
680     abi_ulong target_cmsg_addr;
681     struct target_cmsghdr *target_cmsg;
682     socklen_t space = 0;
683 
684     msg_controllen = tswapl(target_msgh->msg_controllen);
685     if (msg_controllen < sizeof (struct target_cmsghdr))
686         goto the_end;
687     target_cmsg_addr = tswapl(target_msgh->msg_control);
688     target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
689     if (!target_cmsg)
690         return -TARGET_EFAULT;
691 
692     while (cmsg && target_cmsg) {
693         void *data = CMSG_DATA(cmsg);
694         void *target_data = TARGET_CMSG_DATA(target_cmsg);
695 
696         int len = tswapl(target_cmsg->cmsg_len)
697                   - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
698 
699         space += CMSG_SPACE(len);
700         if (space > msgh->msg_controllen) {
701             space -= CMSG_SPACE(len);
702             gemu_log("Host cmsg overflow\n");
703             break;
704         }
705 
706         cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
707         cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
708         cmsg->cmsg_len = CMSG_LEN(len);
709 
710         if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
711             gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
712             memcpy(data, target_data, len);
713         } else {
714             int *fd = (int *)data;
715             int *target_fd = (int *)target_data;
716             int i, numfds = len / sizeof(int);
717 
718             for (i = 0; i < numfds; i++)
719                 fd[i] = tswap32(target_fd[i]);
720         }
721 
722         cmsg = CMSG_NXTHDR(msgh, cmsg);
723         target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
724     }
725     unlock_user(target_cmsg, target_cmsg_addr, 0);
726  the_end:
727     msgh->msg_controllen = space;
728     return 0;
729 }
730 
731 /* ??? Should this also swap msgh->name?  */
732 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
733                                            struct msghdr *msgh)
734 {
735     struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
736     abi_long msg_controllen;
737     abi_ulong target_cmsg_addr;
738     struct target_cmsghdr *target_cmsg;
739     socklen_t space = 0;
740 
741     msg_controllen = tswapl(target_msgh->msg_controllen);
742     if (msg_controllen < sizeof (struct target_cmsghdr))
743         goto the_end;
744     target_cmsg_addr = tswapl(target_msgh->msg_control);
745     target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
746     if (!target_cmsg)
747         return -TARGET_EFAULT;
748 
749     while (cmsg && target_cmsg) {
750         void *data = CMSG_DATA(cmsg);
751         void *target_data = TARGET_CMSG_DATA(target_cmsg);
752 
753         int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
754 
755         space += TARGET_CMSG_SPACE(len);
756         if (space > msg_controllen) {
757             space -= TARGET_CMSG_SPACE(len);
758             gemu_log("Target cmsg overflow\n");
759             break;
760         }
761 
762         target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
763         target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
764         target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
765 
766         if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
767             gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
768             memcpy(target_data, data, len);
769         } else {
770             int *fd = (int *)data;
771             int *target_fd = (int *)target_data;
772             int i, numfds = len / sizeof(int);
773 
774             for (i = 0; i < numfds; i++)
775                 target_fd[i] = tswap32(fd[i]);
776         }
777 
778         cmsg = CMSG_NXTHDR(msgh, cmsg);
779         target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
780     }
781     unlock_user(target_cmsg, target_cmsg_addr, space);
782  the_end:
783     target_msgh->msg_controllen = tswapl(space);
784     return 0;
785 }
786 
787 /* do_setsockopt() Must return target values and target errnos. */
788 static abi_long do_setsockopt(int sockfd, int level, int optname,
789                               abi_ulong optval_addr, socklen_t optlen)
790 {
791     abi_long ret;
792     int val;
793 
794     switch(level) {
795     case SOL_TCP:
796         /* TCP options all take an 'int' value.  */
797         if (optlen < sizeof(uint32_t))
798             return -TARGET_EINVAL;
799 
800         if (get_user_u32(val, optval_addr))
801             return -TARGET_EFAULT;
802         ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
803         break;
804     case SOL_IP:
805         switch(optname) {
806         case IP_TOS:
807         case IP_TTL:
808         case IP_HDRINCL:
809         case IP_ROUTER_ALERT:
810         case IP_RECVOPTS:
811         case IP_RETOPTS:
812         case IP_PKTINFO:
813         case IP_MTU_DISCOVER:
814         case IP_RECVERR:
815         case IP_RECVTOS:
816 #ifdef IP_FREEBIND
817         case IP_FREEBIND:
818 #endif
819         case IP_MULTICAST_TTL:
820         case IP_MULTICAST_LOOP:
821             val = 0;
822             if (optlen >= sizeof(uint32_t)) {
823                 if (get_user_u32(val, optval_addr))
824                     return -TARGET_EFAULT;
825             } else if (optlen >= 1) {
826                 if (get_user_u8(val, optval_addr))
827                     return -TARGET_EFAULT;
828             }
829             ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
830             break;
831         default:
832             goto unimplemented;
833         }
834         break;
835     case TARGET_SOL_SOCKET:
836         switch (optname) {
837             /* Options with 'int' argument.  */
838         case TARGET_SO_DEBUG:
839 		optname = SO_DEBUG;
840 		break;
841         case TARGET_SO_REUSEADDR:
842 		optname = SO_REUSEADDR;
843 		break;
844         case TARGET_SO_TYPE:
845 		optname = SO_TYPE;
846 		break;
847         case TARGET_SO_ERROR:
848 		optname = SO_ERROR;
849 		break;
850         case TARGET_SO_DONTROUTE:
851 		optname = SO_DONTROUTE;
852 		break;
853         case TARGET_SO_BROADCAST:
854 		optname = SO_BROADCAST;
855 		break;
856         case TARGET_SO_SNDBUF:
857 		optname = SO_SNDBUF;
858 		break;
859         case TARGET_SO_RCVBUF:
860 		optname = SO_RCVBUF;
861 		break;
862         case TARGET_SO_KEEPALIVE:
863 		optname = SO_KEEPALIVE;
864 		break;
865         case TARGET_SO_OOBINLINE:
866 		optname = SO_OOBINLINE;
867 		break;
868         case TARGET_SO_NO_CHECK:
869 		optname = SO_NO_CHECK;
870 		break;
871         case TARGET_SO_PRIORITY:
872 		optname = SO_PRIORITY;
873 		break;
874 #ifdef SO_BSDCOMPAT
875         case TARGET_SO_BSDCOMPAT:
876 		optname = SO_BSDCOMPAT;
877 		break;
878 #endif
879         case TARGET_SO_PASSCRED:
880 		optname = SO_PASSCRED;
881 		break;
882         case TARGET_SO_TIMESTAMP:
883 		optname = SO_TIMESTAMP;
884 		break;
885         case TARGET_SO_RCVLOWAT:
886 		optname = SO_RCVLOWAT;
887 		break;
888         case TARGET_SO_RCVTIMEO:
889 		optname = SO_RCVTIMEO;
890 		break;
891         case TARGET_SO_SNDTIMEO:
892 		optname = SO_SNDTIMEO;
893 		break;
894             break;
895         default:
896             goto unimplemented;
897         }
898 	if (optlen < sizeof(uint32_t))
899             return -TARGET_EINVAL;
900 
901 	if (get_user_u32(val, optval_addr))
902             return -TARGET_EFAULT;
903 	ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
904         break;
905     default:
906     unimplemented:
907         gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
908         ret = -TARGET_ENOPROTOOPT;
909     }
910     return ret;
911 }
912 
913 /* do_getsockopt() Must return target values and target errnos. */
914 static abi_long do_getsockopt(int sockfd, int level, int optname,
915                               abi_ulong optval_addr, abi_ulong optlen)
916 {
917     abi_long ret;
918     int len, lv, val;
919 
920     switch(level) {
921     case TARGET_SOL_SOCKET:
922     	level = SOL_SOCKET;
923 	switch (optname) {
924 	case TARGET_SO_LINGER:
925 	case TARGET_SO_RCVTIMEO:
926 	case TARGET_SO_SNDTIMEO:
927 	case TARGET_SO_PEERCRED:
928 	case TARGET_SO_PEERNAME:
929 	    /* These don't just return a single integer */
930 	    goto unimplemented;
931         default:
932             goto int_case;
933         }
934         break;
935     case SOL_TCP:
936         /* TCP options all take an 'int' value.  */
937     int_case:
938         if (get_user_u32(len, optlen))
939             return -TARGET_EFAULT;
940         if (len < 0)
941             return -TARGET_EINVAL;
942         lv = sizeof(int);
943         ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
944         if (ret < 0)
945             return ret;
946         val = tswap32(val);
947         if (len > lv)
948             len = lv;
949         if (len == 4) {
950             if (put_user_u32(val, optval_addr))
951                 return -TARGET_EFAULT;
952         } else {
953             if (put_user_u8(val, optval_addr))
954                 return -TARGET_EFAULT;
955 	}
956         if (put_user_u32(len, optlen))
957             return -TARGET_EFAULT;
958         break;
959     case SOL_IP:
960         switch(optname) {
961         case IP_TOS:
962         case IP_TTL:
963         case IP_HDRINCL:
964         case IP_ROUTER_ALERT:
965         case IP_RECVOPTS:
966         case IP_RETOPTS:
967         case IP_PKTINFO:
968         case IP_MTU_DISCOVER:
969         case IP_RECVERR:
970         case IP_RECVTOS:
971 #ifdef IP_FREEBIND
972         case IP_FREEBIND:
973 #endif
974         case IP_MULTICAST_TTL:
975         case IP_MULTICAST_LOOP:
976             if (get_user_u32(len, optlen))
977                 return -TARGET_EFAULT;
978             if (len < 0)
979                 return -TARGET_EINVAL;
980             lv = sizeof(int);
981             ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
982             if (ret < 0)
983                 return ret;
984             if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
985                 len = 1;
986                 if (put_user_u32(len, optlen)
987                     || put_user_u8(val, optval_addr))
988                     return -TARGET_EFAULT;
989             } else {
990                 if (len > sizeof(int))
991                     len = sizeof(int);
992                 if (put_user_u32(len, optlen)
993                     || put_user_u32(val, optval_addr))
994                     return -TARGET_EFAULT;
995             }
996             break;
997         default:
998             ret = -TARGET_ENOPROTOOPT;
999             break;
1000         }
1001         break;
1002     default:
1003     unimplemented:
1004         gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1005                  level, optname);
1006         ret = -TARGET_EOPNOTSUPP;
1007         break;
1008     }
1009     return ret;
1010 }
1011 
1012 /* FIXME
1013  * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1014  * other lock functions have a return code of 0 for failure.
1015  */
1016 static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1017                            int count, int copy)
1018 {
1019     struct target_iovec *target_vec;
1020     abi_ulong base;
1021     int i, j;
1022 
1023     target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1024     if (!target_vec)
1025         return -TARGET_EFAULT;
1026     for(i = 0;i < count; i++) {
1027         base = tswapl(target_vec[i].iov_base);
1028         vec[i].iov_len = tswapl(target_vec[i].iov_len);
1029         if (vec[i].iov_len != 0) {
1030             vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
1031             if (!vec[i].iov_base && vec[i].iov_len)
1032                 goto fail;
1033         } else {
1034             /* zero length pointer is ignored */
1035             vec[i].iov_base = NULL;
1036         }
1037     }
1038     unlock_user (target_vec, target_addr, 0);
1039     return 0;
1040  fail:
1041     /* failure - unwind locks */
1042     for (j = 0; j < i; j++) {
1043         base = tswapl(target_vec[j].iov_base);
1044         unlock_user(vec[j].iov_base, base, 0);
1045     }
1046     unlock_user (target_vec, target_addr, 0);
1047     return -TARGET_EFAULT;
1048 }
1049 
1050 static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1051                              int count, int copy)
1052 {
1053     struct target_iovec *target_vec;
1054     abi_ulong base;
1055     int i;
1056 
1057     target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1058     if (!target_vec)
1059         return -TARGET_EFAULT;
1060     for(i = 0;i < count; i++) {
1061         base = tswapl(target_vec[i].iov_base);
1062         unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1063     }
1064     unlock_user (target_vec, target_addr, 0);
1065 
1066     return 0;
1067 }
1068 
1069 /* do_socket() Must return target values and target errnos. */
1070 static abi_long do_socket(int domain, int type, int protocol)
1071 {
1072 #if defined(TARGET_MIPS)
1073     switch(type) {
1074     case TARGET_SOCK_DGRAM:
1075         type = SOCK_DGRAM;
1076         break;
1077     case TARGET_SOCK_STREAM:
1078         type = SOCK_STREAM;
1079         break;
1080     case TARGET_SOCK_RAW:
1081         type = SOCK_RAW;
1082         break;
1083     case TARGET_SOCK_RDM:
1084         type = SOCK_RDM;
1085         break;
1086     case TARGET_SOCK_SEQPACKET:
1087         type = SOCK_SEQPACKET;
1088         break;
1089     case TARGET_SOCK_PACKET:
1090         type = SOCK_PACKET;
1091         break;
1092     }
1093 #endif
1094     if (domain == PF_NETLINK)
1095         return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
1096     return get_errno(socket(domain, type, protocol));
1097 }
1098 
1099 /* do_bind() Must return target values and target errnos. */
1100 static abi_long do_bind(int sockfd, abi_ulong target_addr,
1101                         socklen_t addrlen)
1102 {
1103     void *addr = alloca(addrlen);
1104 
1105     target_to_host_sockaddr(addr, target_addr, addrlen);
1106     return get_errno(bind(sockfd, addr, addrlen));
1107 }
1108 
1109 /* do_connect() Must return target values and target errnos. */
1110 static abi_long do_connect(int sockfd, abi_ulong target_addr,
1111                            socklen_t addrlen)
1112 {
1113     void *addr = alloca(addrlen);
1114 
1115     target_to_host_sockaddr(addr, target_addr, addrlen);
1116     return get_errno(connect(sockfd, addr, addrlen));
1117 }
1118 
1119 /* do_sendrecvmsg() Must return target values and target errnos. */
1120 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1121                                int flags, int send)
1122 {
1123     abi_long ret;
1124     struct target_msghdr *msgp;
1125     struct msghdr msg;
1126     int count;
1127     struct iovec *vec;
1128     abi_ulong target_vec;
1129 
1130     /* FIXME */
1131     if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1132                           msgp,
1133                           target_msg,
1134                           send ? 1 : 0))
1135         return -TARGET_EFAULT;
1136     if (msgp->msg_name) {
1137         msg.msg_namelen = tswap32(msgp->msg_namelen);
1138         msg.msg_name = alloca(msg.msg_namelen);
1139         target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1140                                 msg.msg_namelen);
1141     } else {
1142         msg.msg_name = NULL;
1143         msg.msg_namelen = 0;
1144     }
1145     msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1146     msg.msg_control = alloca(msg.msg_controllen);
1147     msg.msg_flags = tswap32(msgp->msg_flags);
1148 
1149     count = tswapl(msgp->msg_iovlen);
1150     vec = alloca(count * sizeof(struct iovec));
1151     target_vec = tswapl(msgp->msg_iov);
1152     lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
1153     msg.msg_iovlen = count;
1154     msg.msg_iov = vec;
1155 
1156     if (send) {
1157         ret = target_to_host_cmsg(&msg, msgp);
1158         if (ret == 0)
1159             ret = get_errno(sendmsg(fd, &msg, flags));
1160     } else {
1161         ret = get_errno(recvmsg(fd, &msg, flags));
1162         if (!is_error(ret))
1163             ret = host_to_target_cmsg(msgp, &msg);
1164     }
1165     unlock_iovec(vec, target_vec, count, !send);
1166     unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1167     return ret;
1168 }
1169 
1170 /* do_accept() Must return target values and target errnos. */
1171 static abi_long do_accept(int fd, abi_ulong target_addr,
1172                           abi_ulong target_addrlen_addr)
1173 {
1174     socklen_t addrlen;
1175     void *addr;
1176     abi_long ret;
1177 
1178     if (get_user_u32(addrlen, target_addrlen_addr))
1179         return -TARGET_EFAULT;
1180 
1181     addr = alloca(addrlen);
1182 
1183     ret = get_errno(accept(fd, addr, &addrlen));
1184     if (!is_error(ret)) {
1185         host_to_target_sockaddr(target_addr, addr, addrlen);
1186         if (put_user_u32(addrlen, target_addrlen_addr))
1187             ret = -TARGET_EFAULT;
1188     }
1189     return ret;
1190 }
1191 
1192 /* do_getpeername() Must return target values and target errnos. */
1193 static abi_long do_getpeername(int fd, abi_ulong target_addr,
1194                                abi_ulong target_addrlen_addr)
1195 {
1196     socklen_t addrlen;
1197     void *addr;
1198     abi_long ret;
1199 
1200     if (get_user_u32(addrlen, target_addrlen_addr))
1201         return -TARGET_EFAULT;
1202 
1203     addr = alloca(addrlen);
1204 
1205     ret = get_errno(getpeername(fd, addr, &addrlen));
1206     if (!is_error(ret)) {
1207         host_to_target_sockaddr(target_addr, addr, addrlen);
1208         if (put_user_u32(addrlen, target_addrlen_addr))
1209             ret = -TARGET_EFAULT;
1210     }
1211     return ret;
1212 }
1213 
1214 /* do_getsockname() Must return target values and target errnos. */
1215 static abi_long do_getsockname(int fd, abi_ulong target_addr,
1216                                abi_ulong target_addrlen_addr)
1217 {
1218     socklen_t addrlen;
1219     void *addr;
1220     abi_long ret;
1221 
1222     if (get_user_u32(addrlen, target_addrlen_addr))
1223         return -TARGET_EFAULT;
1224 
1225     addr = alloca(addrlen);
1226 
1227     ret = get_errno(getsockname(fd, addr, &addrlen));
1228     if (!is_error(ret)) {
1229         host_to_target_sockaddr(target_addr, addr, addrlen);
1230         if (put_user_u32(addrlen, target_addrlen_addr))
1231             ret = -TARGET_EFAULT;
1232     }
1233     return ret;
1234 }
1235 
1236 /* do_socketpair() Must return target values and target errnos. */
1237 static abi_long do_socketpair(int domain, int type, int protocol,
1238                               abi_ulong target_tab_addr)
1239 {
1240     int tab[2];
1241     abi_long ret;
1242 
1243     ret = get_errno(socketpair(domain, type, protocol, tab));
1244     if (!is_error(ret)) {
1245         if (put_user_s32(tab[0], target_tab_addr)
1246             || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1247             ret = -TARGET_EFAULT;
1248     }
1249     return ret;
1250 }
1251 
1252 /* do_sendto() Must return target values and target errnos. */
1253 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1254                           abi_ulong target_addr, socklen_t addrlen)
1255 {
1256     void *addr;
1257     void *host_msg;
1258     abi_long ret;
1259 
1260     host_msg = lock_user(VERIFY_READ, msg, len, 1);
1261     if (!host_msg)
1262         return -TARGET_EFAULT;
1263     if (target_addr) {
1264         addr = alloca(addrlen);
1265         target_to_host_sockaddr(addr, target_addr, addrlen);
1266         ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1267     } else {
1268         ret = get_errno(send(fd, host_msg, len, flags));
1269     }
1270     unlock_user(host_msg, msg, 0);
1271     return ret;
1272 }
1273 
1274 /* do_recvfrom() Must return target values and target errnos. */
1275 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1276                             abi_ulong target_addr,
1277                             abi_ulong target_addrlen)
1278 {
1279     socklen_t addrlen;
1280     void *addr;
1281     void *host_msg;
1282     abi_long ret;
1283 
1284     host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1285     if (!host_msg)
1286         return -TARGET_EFAULT;
1287     if (target_addr) {
1288         if (get_user_u32(addrlen, target_addrlen)) {
1289             ret = -TARGET_EFAULT;
1290             goto fail;
1291         }
1292         addr = alloca(addrlen);
1293         ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1294     } else {
1295         addr = NULL; /* To keep compiler quiet.  */
1296         ret = get_errno(recv(fd, host_msg, len, flags));
1297     }
1298     if (!is_error(ret)) {
1299         if (target_addr) {
1300             host_to_target_sockaddr(target_addr, addr, addrlen);
1301             if (put_user_u32(addrlen, target_addrlen)) {
1302                 ret = -TARGET_EFAULT;
1303                 goto fail;
1304             }
1305         }
1306         unlock_user(host_msg, msg, len);
1307     } else {
1308 fail:
1309         unlock_user(host_msg, msg, 0);
1310     }
1311     return ret;
1312 }
1313 
1314 #ifdef TARGET_NR_socketcall
1315 /* do_socketcall() Must return target values and target errnos. */
1316 static abi_long do_socketcall(int num, abi_ulong vptr)
1317 {
1318     abi_long ret;
1319     const int n = sizeof(abi_ulong);
1320 
1321     switch(num) {
1322     case SOCKOP_socket:
1323 	{
1324             int domain, type, protocol;
1325 
1326             if (get_user_s32(domain, vptr)
1327                 || get_user_s32(type, vptr + n)
1328                 || get_user_s32(protocol, vptr + 2 * n))
1329                 return -TARGET_EFAULT;
1330 
1331             ret = do_socket(domain, type, protocol);
1332 	}
1333         break;
1334     case SOCKOP_bind:
1335 	{
1336             int sockfd;
1337             abi_ulong target_addr;
1338             socklen_t addrlen;
1339 
1340             if (get_user_s32(sockfd, vptr)
1341                 || get_user_ual(target_addr, vptr + n)
1342                 || get_user_u32(addrlen, vptr + 2 * n))
1343                 return -TARGET_EFAULT;
1344 
1345             ret = do_bind(sockfd, target_addr, addrlen);
1346         }
1347         break;
1348     case SOCKOP_connect:
1349         {
1350             int sockfd;
1351             abi_ulong target_addr;
1352             socklen_t addrlen;
1353 
1354             if (get_user_s32(sockfd, vptr)
1355                 || get_user_ual(target_addr, vptr + n)
1356                 || get_user_u32(addrlen, vptr + 2 * n))
1357                 return -TARGET_EFAULT;
1358 
1359             ret = do_connect(sockfd, target_addr, addrlen);
1360         }
1361         break;
1362     case SOCKOP_listen:
1363         {
1364             int sockfd, backlog;
1365 
1366             if (get_user_s32(sockfd, vptr)
1367                 || get_user_s32(backlog, vptr + n))
1368                 return -TARGET_EFAULT;
1369 
1370             ret = get_errno(listen(sockfd, backlog));
1371         }
1372         break;
1373     case SOCKOP_accept:
1374         {
1375             int sockfd;
1376             abi_ulong target_addr, target_addrlen;
1377 
1378             if (get_user_s32(sockfd, vptr)
1379                 || get_user_ual(target_addr, vptr + n)
1380                 || get_user_u32(target_addrlen, vptr + 2 * n))
1381                 return -TARGET_EFAULT;
1382 
1383             ret = do_accept(sockfd, target_addr, target_addrlen);
1384         }
1385         break;
1386     case SOCKOP_getsockname:
1387         {
1388             int sockfd;
1389             abi_ulong target_addr, target_addrlen;
1390 
1391             if (get_user_s32(sockfd, vptr)
1392                 || get_user_ual(target_addr, vptr + n)
1393                 || get_user_u32(target_addrlen, vptr + 2 * n))
1394                 return -TARGET_EFAULT;
1395 
1396             ret = do_getsockname(sockfd, target_addr, target_addrlen);
1397         }
1398         break;
1399     case SOCKOP_getpeername:
1400         {
1401             int sockfd;
1402             abi_ulong target_addr, target_addrlen;
1403 
1404             if (get_user_s32(sockfd, vptr)
1405                 || get_user_ual(target_addr, vptr + n)
1406                 || get_user_u32(target_addrlen, vptr + 2 * n))
1407                 return -TARGET_EFAULT;
1408 
1409             ret = do_getpeername(sockfd, target_addr, target_addrlen);
1410         }
1411         break;
1412     case SOCKOP_socketpair:
1413         {
1414             int domain, type, protocol;
1415             abi_ulong tab;
1416 
1417             if (get_user_s32(domain, vptr)
1418                 || get_user_s32(type, vptr + n)
1419                 || get_user_s32(protocol, vptr + 2 * n)
1420                 || get_user_ual(tab, vptr + 3 * n))
1421                 return -TARGET_EFAULT;
1422 
1423             ret = do_socketpair(domain, type, protocol, tab);
1424         }
1425         break;
1426     case SOCKOP_send:
1427         {
1428             int sockfd;
1429             abi_ulong msg;
1430             size_t len;
1431             int flags;
1432 
1433             if (get_user_s32(sockfd, vptr)
1434                 || get_user_ual(msg, vptr + n)
1435                 || get_user_ual(len, vptr + 2 * n)
1436                 || get_user_s32(flags, vptr + 3 * n))
1437                 return -TARGET_EFAULT;
1438 
1439             ret = do_sendto(sockfd, msg, len, flags, 0, 0);
1440         }
1441         break;
1442     case SOCKOP_recv:
1443         {
1444             int sockfd;
1445             abi_ulong msg;
1446             size_t len;
1447             int flags;
1448 
1449             if (get_user_s32(sockfd, vptr)
1450                 || get_user_ual(msg, vptr + n)
1451                 || get_user_ual(len, vptr + 2 * n)
1452                 || get_user_s32(flags, vptr + 3 * n))
1453                 return -TARGET_EFAULT;
1454 
1455             ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
1456         }
1457         break;
1458     case SOCKOP_sendto:
1459         {
1460             int sockfd;
1461             abi_ulong msg;
1462             size_t len;
1463             int flags;
1464             abi_ulong addr;
1465             socklen_t addrlen;
1466 
1467             if (get_user_s32(sockfd, vptr)
1468                 || get_user_ual(msg, vptr + n)
1469                 || get_user_ual(len, vptr + 2 * n)
1470                 || get_user_s32(flags, vptr + 3 * n)
1471                 || get_user_ual(addr, vptr + 4 * n)
1472                 || get_user_u32(addrlen, vptr + 5 * n))
1473                 return -TARGET_EFAULT;
1474 
1475             ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
1476         }
1477         break;
1478     case SOCKOP_recvfrom:
1479         {
1480             int sockfd;
1481             abi_ulong msg;
1482             size_t len;
1483             int flags;
1484             abi_ulong addr;
1485             socklen_t addrlen;
1486 
1487             if (get_user_s32(sockfd, vptr)
1488                 || get_user_ual(msg, vptr + n)
1489                 || get_user_ual(len, vptr + 2 * n)
1490                 || get_user_s32(flags, vptr + 3 * n)
1491                 || get_user_ual(addr, vptr + 4 * n)
1492                 || get_user_u32(addrlen, vptr + 5 * n))
1493                 return -TARGET_EFAULT;
1494 
1495             ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
1496         }
1497         break;
1498     case SOCKOP_shutdown:
1499         {
1500             int sockfd, how;
1501 
1502             if (get_user_s32(sockfd, vptr)
1503                 || get_user_s32(how, vptr + n))
1504                 return -TARGET_EFAULT;
1505 
1506             ret = get_errno(shutdown(sockfd, how));
1507         }
1508         break;
1509     case SOCKOP_sendmsg:
1510     case SOCKOP_recvmsg:
1511         {
1512             int fd;
1513             abi_ulong target_msg;
1514             int flags;
1515 
1516             if (get_user_s32(fd, vptr)
1517                 || get_user_ual(target_msg, vptr + n)
1518                 || get_user_s32(flags, vptr + 2 * n))
1519                 return -TARGET_EFAULT;
1520 
1521             ret = do_sendrecvmsg(fd, target_msg, flags,
1522                                  (num == SOCKOP_sendmsg));
1523         }
1524         break;
1525     case SOCKOP_setsockopt:
1526         {
1527             int sockfd;
1528             int level;
1529             int optname;
1530             abi_ulong optval;
1531             socklen_t optlen;
1532 
1533             if (get_user_s32(sockfd, vptr)
1534                 || get_user_s32(level, vptr + n)
1535                 || get_user_s32(optname, vptr + 2 * n)
1536                 || get_user_ual(optval, vptr + 3 * n)
1537                 || get_user_u32(optlen, vptr + 4 * n))
1538                 return -TARGET_EFAULT;
1539 
1540             ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1541         }
1542         break;
1543     case SOCKOP_getsockopt:
1544         {
1545             int sockfd;
1546             int level;
1547             int optname;
1548             abi_ulong optval;
1549             socklen_t optlen;
1550 
1551             if (get_user_s32(sockfd, vptr)
1552                 || get_user_s32(level, vptr + n)
1553                 || get_user_s32(optname, vptr + 2 * n)
1554                 || get_user_ual(optval, vptr + 3 * n)
1555                 || get_user_u32(optlen, vptr + 4 * n))
1556                 return -TARGET_EFAULT;
1557 
1558             ret = do_getsockopt(sockfd, level, optname, optval, optlen);
1559         }
1560         break;
1561     default:
1562         gemu_log("Unsupported socketcall: %d\n", num);
1563         ret = -TARGET_ENOSYS;
1564         break;
1565     }
1566     return ret;
1567 }
1568 #endif
1569 
1570 #ifdef TARGET_NR_ipc
1571 #define N_SHM_REGIONS	32
1572 
1573 static struct shm_region {
1574     abi_ulong	start;
1575     abi_ulong	size;
1576 } shm_regions[N_SHM_REGIONS];
1577 
1578 struct target_ipc_perm
1579 {
1580     abi_long __key;
1581     abi_ulong uid;
1582     abi_ulong gid;
1583     abi_ulong cuid;
1584     abi_ulong cgid;
1585     unsigned short int mode;
1586     unsigned short int __pad1;
1587     unsigned short int __seq;
1588     unsigned short int __pad2;
1589     abi_ulong __unused1;
1590     abi_ulong __unused2;
1591 };
1592 
1593 struct target_semid_ds
1594 {
1595   struct target_ipc_perm sem_perm;
1596   abi_ulong sem_otime;
1597   abi_ulong __unused1;
1598   abi_ulong sem_ctime;
1599   abi_ulong __unused2;
1600   abi_ulong sem_nsems;
1601   abi_ulong __unused3;
1602   abi_ulong __unused4;
1603 };
1604 
1605 static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
1606                                                abi_ulong target_addr)
1607 {
1608     struct target_ipc_perm *target_ip;
1609     struct target_semid_ds *target_sd;
1610 
1611     if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1612         return -TARGET_EFAULT;
1613     target_ip=&(target_sd->sem_perm);
1614     host_ip->__key = tswapl(target_ip->__key);
1615     host_ip->uid = tswapl(target_ip->uid);
1616     host_ip->gid = tswapl(target_ip->gid);
1617     host_ip->cuid = tswapl(target_ip->cuid);
1618     host_ip->cgid = tswapl(target_ip->cgid);
1619     host_ip->mode = tswapl(target_ip->mode);
1620     unlock_user_struct(target_sd, target_addr, 0);
1621     return 0;
1622 }
1623 
1624 static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
1625                                                struct ipc_perm *host_ip)
1626 {
1627     struct target_ipc_perm *target_ip;
1628     struct target_semid_ds *target_sd;
1629 
1630     if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1631         return -TARGET_EFAULT;
1632     target_ip = &(target_sd->sem_perm);
1633     target_ip->__key = tswapl(host_ip->__key);
1634     target_ip->uid = tswapl(host_ip->uid);
1635     target_ip->gid = tswapl(host_ip->gid);
1636     target_ip->cuid = tswapl(host_ip->cuid);
1637     target_ip->cgid = tswapl(host_ip->cgid);
1638     target_ip->mode = tswapl(host_ip->mode);
1639     unlock_user_struct(target_sd, target_addr, 1);
1640     return 0;
1641 }
1642 
1643 static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
1644                                                abi_ulong target_addr)
1645 {
1646     struct target_semid_ds *target_sd;
1647 
1648     if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1649         return -TARGET_EFAULT;
1650     target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1651     host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1652     host_sd->sem_otime = tswapl(target_sd->sem_otime);
1653     host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1654     unlock_user_struct(target_sd, target_addr, 0);
1655     return 0;
1656 }
1657 
1658 static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
1659                                                struct semid_ds *host_sd)
1660 {
1661     struct target_semid_ds *target_sd;
1662 
1663     if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1664         return -TARGET_EFAULT;
1665     host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1666     target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1667     target_sd->sem_otime = tswapl(host_sd->sem_otime);
1668     target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1669     unlock_user_struct(target_sd, target_addr, 1);
1670     return 0;
1671 }
1672 
1673 union semun {
1674 	int val;
1675 	struct semid_ds *buf;
1676 	unsigned short *array;
1677 };
1678 
1679 union target_semun {
1680 	int val;
1681 	abi_long buf;
1682 	unsigned short int *array;
1683 };
1684 
1685 static inline abi_long target_to_host_semun(int cmd,
1686                                             union semun *host_su,
1687                                             abi_ulong target_addr,
1688                                             struct semid_ds *ds)
1689 {
1690     union target_semun *target_su;
1691 
1692     switch( cmd ) {
1693 	case IPC_STAT:
1694 	case IPC_SET:
1695            if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1696                return -TARGET_EFAULT;
1697 	   target_to_host_semid_ds(ds,target_su->buf);
1698 	   host_su->buf = ds;
1699            unlock_user_struct(target_su, target_addr, 0);
1700 	   break;
1701 	case GETVAL:
1702 	case SETVAL:
1703            if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1704                return -TARGET_EFAULT;
1705 	   host_su->val = tswapl(target_su->val);
1706            unlock_user_struct(target_su, target_addr, 0);
1707 	   break;
1708 	case GETALL:
1709 	case SETALL:
1710            if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1711                return -TARGET_EFAULT;
1712 	   *host_su->array = tswap16(*target_su->array);
1713            unlock_user_struct(target_su, target_addr, 0);
1714 	   break;
1715 	default:
1716            gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1717     }
1718     return 0;
1719 }
1720 
1721 static inline abi_long host_to_target_semun(int cmd,
1722                                             abi_ulong target_addr,
1723                                             union semun *host_su,
1724                                             struct semid_ds *ds)
1725 {
1726     union target_semun *target_su;
1727 
1728     switch( cmd ) {
1729 	case IPC_STAT:
1730 	case IPC_SET:
1731            if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1732                return -TARGET_EFAULT;
1733 	   host_to_target_semid_ds(target_su->buf,ds);
1734            unlock_user_struct(target_su, target_addr, 1);
1735 	   break;
1736 	case GETVAL:
1737 	case SETVAL:
1738            if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1739                return -TARGET_EFAULT;
1740 	   target_su->val = tswapl(host_su->val);
1741            unlock_user_struct(target_su, target_addr, 1);
1742 	   break;
1743 	case GETALL:
1744 	case SETALL:
1745            if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1746                return -TARGET_EFAULT;
1747 	   *target_su->array = tswap16(*host_su->array);
1748            unlock_user_struct(target_su, target_addr, 1);
1749 	   break;
1750         default:
1751            gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1752     }
1753     return 0;
1754 }
1755 
1756 static inline abi_long do_semctl(int first, int second, int third,
1757                                  abi_long ptr)
1758 {
1759     union semun arg;
1760     struct semid_ds dsarg;
1761     int cmd = third&0xff;
1762     abi_long ret = 0;
1763 
1764     switch( cmd ) {
1765 	case GETVAL:
1766             target_to_host_semun(cmd,&arg,ptr,&dsarg);
1767             ret = get_errno(semctl(first, second, cmd, arg));
1768             host_to_target_semun(cmd,ptr,&arg,&dsarg);
1769             break;
1770 	case SETVAL:
1771             target_to_host_semun(cmd,&arg,ptr,&dsarg);
1772             ret = get_errno(semctl(first, second, cmd, arg));
1773             host_to_target_semun(cmd,ptr,&arg,&dsarg);
1774             break;
1775 	case GETALL:
1776             target_to_host_semun(cmd,&arg,ptr,&dsarg);
1777             ret = get_errno(semctl(first, second, cmd, arg));
1778             host_to_target_semun(cmd,ptr,&arg,&dsarg);
1779             break;
1780 	case SETALL:
1781             target_to_host_semun(cmd,&arg,ptr,&dsarg);
1782             ret = get_errno(semctl(first, second, cmd, arg));
1783             host_to_target_semun(cmd,ptr,&arg,&dsarg);
1784             break;
1785 	case IPC_STAT:
1786             target_to_host_semun(cmd,&arg,ptr,&dsarg);
1787             ret = get_errno(semctl(first, second, cmd, arg));
1788             host_to_target_semun(cmd,ptr,&arg,&dsarg);
1789             break;
1790 	case IPC_SET:
1791             target_to_host_semun(cmd,&arg,ptr,&dsarg);
1792             ret = get_errno(semctl(first, second, cmd, arg));
1793             host_to_target_semun(cmd,ptr,&arg,&dsarg);
1794             break;
1795     default:
1796             ret = get_errno(semctl(first, second, cmd, arg));
1797     }
1798 
1799     return ret;
1800 }
1801 
1802 struct target_msqid_ds
1803 {
1804   struct target_ipc_perm msg_perm;
1805   abi_ulong msg_stime;
1806   abi_ulong __unused1;
1807   abi_ulong msg_rtime;
1808   abi_ulong __unused2;
1809   abi_ulong msg_ctime;
1810   abi_ulong __unused3;
1811   abi_ulong __msg_cbytes;
1812   abi_ulong msg_qnum;
1813   abi_ulong msg_qbytes;
1814   abi_ulong msg_lspid;
1815   abi_ulong msg_lrpid;
1816   abi_ulong __unused4;
1817   abi_ulong __unused5;
1818 };
1819 
1820 static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
1821                                                abi_ulong target_addr)
1822 {
1823     struct target_msqid_ds *target_md;
1824 
1825     if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
1826         return -TARGET_EFAULT;
1827     target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1828     host_md->msg_stime = tswapl(target_md->msg_stime);
1829     host_md->msg_rtime = tswapl(target_md->msg_rtime);
1830     host_md->msg_ctime = tswapl(target_md->msg_ctime);
1831     host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1832     host_md->msg_qnum = tswapl(target_md->msg_qnum);
1833     host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1834     host_md->msg_lspid = tswapl(target_md->msg_lspid);
1835     host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1836     unlock_user_struct(target_md, target_addr, 0);
1837     return 0;
1838 }
1839 
1840 static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
1841                                                struct msqid_ds *host_md)
1842 {
1843     struct target_msqid_ds *target_md;
1844 
1845     if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
1846         return -TARGET_EFAULT;
1847     host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1848     target_md->msg_stime = tswapl(host_md->msg_stime);
1849     target_md->msg_rtime = tswapl(host_md->msg_rtime);
1850     target_md->msg_ctime = tswapl(host_md->msg_ctime);
1851     target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1852     target_md->msg_qnum = tswapl(host_md->msg_qnum);
1853     target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1854     target_md->msg_lspid = tswapl(host_md->msg_lspid);
1855     target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1856     unlock_user_struct(target_md, target_addr, 1);
1857     return 0;
1858 }
1859 
1860 static inline abi_long do_msgctl(int first, int second, abi_long ptr)
1861 {
1862     struct msqid_ds dsarg;
1863     int cmd = second&0xff;
1864     abi_long ret = 0;
1865     switch( cmd ) {
1866     case IPC_STAT:
1867     case IPC_SET:
1868         target_to_host_msqid_ds(&dsarg,ptr);
1869         ret = get_errno(msgctl(first, cmd, &dsarg));
1870         host_to_target_msqid_ds(ptr,&dsarg);
1871     default:
1872         ret = get_errno(msgctl(first, cmd, &dsarg));
1873     }
1874     return ret;
1875 }
1876 
1877 struct target_msgbuf {
1878 	abi_ulong mtype;
1879 	char	mtext[1];
1880 };
1881 
1882 static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1883                                  unsigned int msgsz, int msgflg)
1884 {
1885     struct target_msgbuf *target_mb;
1886     struct msgbuf *host_mb;
1887     abi_long ret = 0;
1888 
1889     if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
1890         return -TARGET_EFAULT;
1891     host_mb = malloc(msgsz+sizeof(long));
1892     host_mb->mtype = tswapl(target_mb->mtype);
1893     memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1894     ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1895     free(host_mb);
1896     unlock_user_struct(target_mb, msgp, 0);
1897 
1898     return ret;
1899 }
1900 
1901 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1902                                  unsigned int msgsz, int msgtype,
1903                                  int msgflg)
1904 {
1905     struct target_msgbuf *target_mb;
1906     char *target_mtext;
1907     struct msgbuf *host_mb;
1908     abi_long ret = 0;
1909 
1910     if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
1911         return -TARGET_EFAULT;
1912     host_mb = malloc(msgsz+sizeof(long));
1913     ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
1914     if (ret > 0) {
1915         abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
1916         target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
1917         if (!target_mtext) {
1918             ret = -TARGET_EFAULT;
1919             goto end;
1920         }
1921     	memcpy(target_mb->mtext, host_mb->mtext, ret);
1922         unlock_user(target_mtext, target_mtext_addr, ret);
1923     }
1924     target_mb->mtype = tswapl(host_mb->mtype);
1925     free(host_mb);
1926 
1927 end:
1928     if (target_mb)
1929         unlock_user_struct(target_mb, msgp, 1);
1930     return ret;
1931 }
1932 
1933 /* ??? This only works with linear mappings.  */
1934 /* do_ipc() must return target values and target errnos. */
1935 static abi_long do_ipc(unsigned int call, int first,
1936                        int second, int third,
1937                        abi_long ptr, abi_long fifth)
1938 {
1939     int version;
1940     abi_long ret = 0;
1941     struct shmid_ds shm_info;
1942     int i;
1943 
1944     version = call >> 16;
1945     call &= 0xffff;
1946 
1947     switch (call) {
1948     case IPCOP_semop:
1949         ret = get_errno(semop(first,(struct sembuf *)g2h(ptr), second));
1950         break;
1951 
1952     case IPCOP_semget:
1953         ret = get_errno(semget(first, second, third));
1954         break;
1955 
1956     case IPCOP_semctl:
1957         ret = do_semctl(first, second, third, ptr);
1958         break;
1959 
1960     case IPCOP_semtimedop:
1961         gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
1962         ret = -TARGET_ENOSYS;
1963         break;
1964 
1965 	case IPCOP_msgget:
1966 		ret = get_errno(msgget(first, second));
1967 		break;
1968 
1969 	case IPCOP_msgsnd:
1970 		ret = do_msgsnd(first, ptr, second, third);
1971 		break;
1972 
1973 	case IPCOP_msgctl:
1974         	ret = do_msgctl(first, second, ptr);
1975 		break;
1976 
1977 	case IPCOP_msgrcv:
1978                 {
1979                       /* XXX: this code is not correct */
1980                       struct ipc_kludge
1981                       {
1982                               void *__unbounded msgp;
1983                               long int msgtyp;
1984                       };
1985 
1986                       struct ipc_kludge *foo = (struct ipc_kludge *)g2h(ptr);
1987                       struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
1988 
1989                       ret = do_msgrcv(first, (long)msgp, second, 0, third);
1990 
1991                 }
1992 		break;
1993 
1994     case IPCOP_shmat:
1995         {
1996             abi_ulong raddr;
1997             void *host_addr;
1998             /* SHM_* flags are the same on all linux platforms */
1999             host_addr = shmat(first, (void *)g2h(ptr), second);
2000             if (host_addr == (void *)-1) {
2001                 ret = get_errno((long)host_addr);
2002                 break;
2003             }
2004             raddr = h2g((unsigned long)host_addr);
2005             /* find out the length of the shared memory segment */
2006 
2007             ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
2008             if (is_error(ret)) {
2009                 /* can't get length, bail out */
2010                 shmdt(host_addr);
2011                 break;
2012             }
2013             page_set_flags(raddr, raddr + shm_info.shm_segsz,
2014                            PAGE_VALID | PAGE_READ |
2015                            ((second & SHM_RDONLY)? 0: PAGE_WRITE));
2016             for (i = 0; i < N_SHM_REGIONS; ++i) {
2017                 if (shm_regions[i].start == 0) {
2018                     shm_regions[i].start = raddr;
2019                     shm_regions[i].size = shm_info.shm_segsz;
2020                     break;
2021                 }
2022             }
2023             if (put_user_ual(raddr, third))
2024                 return -TARGET_EFAULT;
2025             ret = 0;
2026         }
2027 	break;
2028     case IPCOP_shmdt:
2029 	for (i = 0; i < N_SHM_REGIONS; ++i) {
2030 	    if (shm_regions[i].start == ptr) {
2031 		shm_regions[i].start = 0;
2032 		page_set_flags(ptr, shm_regions[i].size, 0);
2033 		break;
2034 	    }
2035 	}
2036 	ret = get_errno(shmdt((void *)g2h(ptr)));
2037 	break;
2038 
2039     case IPCOP_shmget:
2040 	/* IPC_* flag values are the same on all linux platforms */
2041 	ret = get_errno(shmget(first, second, third));
2042 	break;
2043 
2044 	/* IPC_* and SHM_* command values are the same on all linux platforms */
2045     case IPCOP_shmctl:
2046         switch(second) {
2047         case IPC_RMID:
2048         case SHM_LOCK:
2049         case SHM_UNLOCK:
2050             ret = get_errno(shmctl(first, second, NULL));
2051             break;
2052         default:
2053             goto unimplemented;
2054         }
2055         break;
2056     default:
2057     unimplemented:
2058 	gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
2059 	ret = -TARGET_ENOSYS;
2060 	break;
2061     }
2062     return ret;
2063 }
2064 #endif
2065 
2066 /* kernel structure types definitions */
2067 #define IFNAMSIZ        16
2068 
2069 #define STRUCT(name, list...) STRUCT_ ## name,
2070 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
2071 enum {
2072 #include "syscall_types.h"
2073 };
2074 #undef STRUCT
2075 #undef STRUCT_SPECIAL
2076 
2077 #define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
2078 #define STRUCT_SPECIAL(name)
2079 #include "syscall_types.h"
2080 #undef STRUCT
2081 #undef STRUCT_SPECIAL
2082 
2083 typedef struct IOCTLEntry {
2084     unsigned int target_cmd;
2085     unsigned int host_cmd;
2086     const char *name;
2087     int access;
2088     const argtype arg_type[5];
2089 } IOCTLEntry;
2090 
2091 #define IOC_R 0x0001
2092 #define IOC_W 0x0002
2093 #define IOC_RW (IOC_R | IOC_W)
2094 
2095 #define MAX_STRUCT_SIZE 4096
2096 
2097 IOCTLEntry ioctl_entries[] = {
2098 #define IOCTL(cmd, access, types...) \
2099     { TARGET_ ## cmd, cmd, #cmd, access, { types } },
2100 #include "ioctls.h"
2101     { 0, 0, },
2102 };
2103 
2104 /* ??? Implement proper locking for ioctls.  */
2105 /* do_ioctl() Must return target values and target errnos. */
2106 static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
2107 {
2108     const IOCTLEntry *ie;
2109     const argtype *arg_type;
2110     abi_long ret;
2111     uint8_t buf_temp[MAX_STRUCT_SIZE];
2112     int target_size;
2113     void *argptr;
2114 
2115     ie = ioctl_entries;
2116     for(;;) {
2117         if (ie->target_cmd == 0) {
2118             gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
2119             return -TARGET_ENOSYS;
2120         }
2121         if (ie->target_cmd == cmd)
2122             break;
2123         ie++;
2124     }
2125     arg_type = ie->arg_type;
2126 #if defined(DEBUG)
2127     gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
2128 #endif
2129     switch(arg_type[0]) {
2130     case TYPE_NULL:
2131         /* no argument */
2132         ret = get_errno(ioctl(fd, ie->host_cmd));
2133         break;
2134     case TYPE_PTRVOID:
2135     case TYPE_INT:
2136         /* int argment */
2137         ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2138         break;
2139     case TYPE_PTR:
2140         arg_type++;
2141         target_size = thunk_type_size(arg_type, 0);
2142         switch(ie->access) {
2143         case IOC_R:
2144             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2145             if (!is_error(ret)) {
2146                 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2147                 if (!argptr)
2148                     return -TARGET_EFAULT;
2149                 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2150                 unlock_user(argptr, arg, target_size);
2151             }
2152             break;
2153         case IOC_W:
2154             argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2155             if (!argptr)
2156                 return -TARGET_EFAULT;
2157             thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2158             unlock_user(argptr, arg, 0);
2159             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2160             break;
2161         default:
2162         case IOC_RW:
2163             argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2164             if (!argptr)
2165                 return -TARGET_EFAULT;
2166             thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2167             unlock_user(argptr, arg, 0);
2168             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2169             if (!is_error(ret)) {
2170                 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2171                 if (!argptr)
2172                     return -TARGET_EFAULT;
2173                 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2174                 unlock_user(argptr, arg, target_size);
2175             }
2176             break;
2177         }
2178         break;
2179     default:
2180         gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2181                  (long)cmd, arg_type[0]);
2182         ret = -TARGET_ENOSYS;
2183         break;
2184     }
2185     return ret;
2186 }
2187 
2188 bitmask_transtbl iflag_tbl[] = {
2189         { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2190         { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2191         { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2192         { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2193         { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2194         { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2195         { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2196         { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2197         { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2198         { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2199         { TARGET_IXON, TARGET_IXON, IXON, IXON },
2200         { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2201         { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
2202         { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
2203         { 0, 0, 0, 0 }
2204 };
2205 
2206 bitmask_transtbl oflag_tbl[] = {
2207 	{ TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
2208 	{ TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
2209 	{ TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
2210 	{ TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
2211 	{ TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
2212 	{ TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
2213 	{ TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
2214 	{ TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
2215 	{ TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
2216 	{ TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
2217 	{ TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
2218 	{ TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
2219 	{ TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
2220 	{ TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
2221 	{ TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
2222 	{ TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
2223 	{ TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
2224 	{ TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
2225 	{ TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
2226 	{ TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
2227 	{ TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
2228 	{ TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
2229 	{ TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
2230 	{ TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
2231 	{ 0, 0, 0, 0 }
2232 };
2233 
2234 bitmask_transtbl cflag_tbl[] = {
2235 	{ TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
2236 	{ TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
2237 	{ TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
2238 	{ TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
2239 	{ TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
2240 	{ TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
2241 	{ TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
2242 	{ TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
2243 	{ TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
2244 	{ TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
2245 	{ TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
2246 	{ TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
2247 	{ TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
2248 	{ TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
2249 	{ TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
2250 	{ TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
2251 	{ TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
2252 	{ TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
2253 	{ TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
2254 	{ TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
2255 	{ TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
2256 	{ TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
2257 	{ TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
2258 	{ TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
2259 	{ TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
2260 	{ TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
2261 	{ TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
2262 	{ TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
2263 	{ TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
2264 	{ TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
2265 	{ TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
2266 	{ 0, 0, 0, 0 }
2267 };
2268 
2269 bitmask_transtbl lflag_tbl[] = {
2270 	{ TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2271 	{ TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2272 	{ TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2273 	{ TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2274 	{ TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2275 	{ TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2276 	{ TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2277 	{ TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2278 	{ TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2279 	{ TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2280 	{ TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2281 	{ TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2282 	{ TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2283 	{ TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2284 	{ TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
2285 	{ 0, 0, 0, 0 }
2286 };
2287 
2288 static void target_to_host_termios (void *dst, const void *src)
2289 {
2290     struct host_termios *host = dst;
2291     const struct target_termios *target = src;
2292 
2293     host->c_iflag =
2294         target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
2295     host->c_oflag =
2296         target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
2297     host->c_cflag =
2298         target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
2299     host->c_lflag =
2300         target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2301     host->c_line = target->c_line;
2302 
2303     host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2304     host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
2305     host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
2306     host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
2307     host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
2308     host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
2309     host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
2310     host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
2311     host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
2312     host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2313     host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
2314     host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2315     host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2316     host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2317     host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2318     host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
2319     host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
2320 }
2321 
2322 static void host_to_target_termios (void *dst, const void *src)
2323 {
2324     struct target_termios *target = dst;
2325     const struct host_termios *host = src;
2326 
2327     target->c_iflag =
2328         tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
2329     target->c_oflag =
2330         tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
2331     target->c_cflag =
2332         tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
2333     target->c_lflag =
2334         tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2335     target->c_line = host->c_line;
2336 
2337     target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2338     target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2339     target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2340     target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2341     target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2342     target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2343     target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2344     target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2345     target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2346     target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2347     target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2348     target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2349     target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2350     target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2351     target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2352     target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2353     target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2354 }
2355 
2356 StructEntry struct_termios_def = {
2357     .convert = { host_to_target_termios, target_to_host_termios },
2358     .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2359     .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2360 };
2361 
2362 static bitmask_transtbl mmap_flags_tbl[] = {
2363 	{ TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2364 	{ TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2365 	{ TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2366 	{ TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2367 	{ TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2368 	{ TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2369 	{ TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2370 	{ TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2371 	{ 0, 0, 0, 0 }
2372 };
2373 
2374 static bitmask_transtbl fcntl_flags_tbl[] = {
2375 	{ TARGET_O_ACCMODE,   TARGET_O_WRONLY,    O_ACCMODE,   O_WRONLY,    },
2376 	{ TARGET_O_ACCMODE,   TARGET_O_RDWR,      O_ACCMODE,   O_RDWR,      },
2377 	{ TARGET_O_CREAT,     TARGET_O_CREAT,     O_CREAT,     O_CREAT,     },
2378 	{ TARGET_O_EXCL,      TARGET_O_EXCL,      O_EXCL,      O_EXCL,      },
2379 	{ TARGET_O_NOCTTY,    TARGET_O_NOCTTY,    O_NOCTTY,    O_NOCTTY,    },
2380 	{ TARGET_O_TRUNC,     TARGET_O_TRUNC,     O_TRUNC,     O_TRUNC,     },
2381 	{ TARGET_O_APPEND,    TARGET_O_APPEND,    O_APPEND,    O_APPEND,    },
2382 	{ TARGET_O_NONBLOCK,  TARGET_O_NONBLOCK,  O_NONBLOCK,  O_NONBLOCK,  },
2383 	{ TARGET_O_SYNC,      TARGET_O_SYNC,      O_SYNC,      O_SYNC,      },
2384 	{ TARGET_FASYNC,      TARGET_FASYNC,      FASYNC,      FASYNC,      },
2385 	{ TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2386 	{ TARGET_O_NOFOLLOW,  TARGET_O_NOFOLLOW,  O_NOFOLLOW,  O_NOFOLLOW,  },
2387 	{ TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
2388 #if defined(O_DIRECT)
2389 	{ TARGET_O_DIRECT,    TARGET_O_DIRECT,    O_DIRECT,    O_DIRECT,    },
2390 #endif
2391 	{ 0, 0, 0, 0 }
2392 };
2393 
2394 #if defined(TARGET_I386)
2395 
2396 /* NOTE: there is really one LDT for all the threads */
2397 uint8_t *ldt_table;
2398 
2399 static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
2400 {
2401     int size;
2402     void *p;
2403 
2404     if (!ldt_table)
2405         return 0;
2406     size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2407     if (size > bytecount)
2408         size = bytecount;
2409     p = lock_user(VERIFY_WRITE, ptr, size, 0);
2410     if (!p)
2411         return -TARGET_EFAULT;
2412     /* ??? Should this by byteswapped?  */
2413     memcpy(p, ldt_table, size);
2414     unlock_user(p, ptr, size);
2415     return size;
2416 }
2417 
2418 /* XXX: add locking support */
2419 static abi_long write_ldt(CPUX86State *env,
2420                           abi_ulong ptr, unsigned long bytecount, int oldmode)
2421 {
2422     struct target_modify_ldt_ldt_s ldt_info;
2423     struct target_modify_ldt_ldt_s *target_ldt_info;
2424     int seg_32bit, contents, read_exec_only, limit_in_pages;
2425     int seg_not_present, useable, lm;
2426     uint32_t *lp, entry_1, entry_2;
2427 
2428     if (bytecount != sizeof(ldt_info))
2429         return -TARGET_EINVAL;
2430     if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
2431         return -TARGET_EFAULT;
2432     ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2433     ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2434     ldt_info.limit = tswap32(target_ldt_info->limit);
2435     ldt_info.flags = tswap32(target_ldt_info->flags);
2436     unlock_user_struct(target_ldt_info, ptr, 0);
2437 
2438     if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
2439         return -TARGET_EINVAL;
2440     seg_32bit = ldt_info.flags & 1;
2441     contents = (ldt_info.flags >> 1) & 3;
2442     read_exec_only = (ldt_info.flags >> 3) & 1;
2443     limit_in_pages = (ldt_info.flags >> 4) & 1;
2444     seg_not_present = (ldt_info.flags >> 5) & 1;
2445     useable = (ldt_info.flags >> 6) & 1;
2446 #ifdef TARGET_ABI32
2447     lm = 0;
2448 #else
2449     lm = (ldt_info.flags >> 7) & 1;
2450 #endif
2451     if (contents == 3) {
2452         if (oldmode)
2453             return -TARGET_EINVAL;
2454         if (seg_not_present == 0)
2455             return -TARGET_EINVAL;
2456     }
2457     /* allocate the LDT */
2458     if (!ldt_table) {
2459         ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2460         if (!ldt_table)
2461             return -TARGET_ENOMEM;
2462         memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2463         env->ldt.base = h2g((unsigned long)ldt_table);
2464         env->ldt.limit = 0xffff;
2465     }
2466 
2467     /* NOTE: same code as Linux kernel */
2468     /* Allow LDTs to be cleared by the user. */
2469     if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2470         if (oldmode ||
2471             (contents == 0		&&
2472              read_exec_only == 1	&&
2473              seg_32bit == 0		&&
2474              limit_in_pages == 0	&&
2475              seg_not_present == 1	&&
2476              useable == 0 )) {
2477             entry_1 = 0;
2478             entry_2 = 0;
2479             goto install;
2480         }
2481     }
2482 
2483     entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2484         (ldt_info.limit & 0x0ffff);
2485     entry_2 = (ldt_info.base_addr & 0xff000000) |
2486         ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2487         (ldt_info.limit & 0xf0000) |
2488         ((read_exec_only ^ 1) << 9) |
2489         (contents << 10) |
2490         ((seg_not_present ^ 1) << 15) |
2491         (seg_32bit << 22) |
2492         (limit_in_pages << 23) |
2493         (lm << 21) |
2494         0x7000;
2495     if (!oldmode)
2496         entry_2 |= (useable << 20);
2497 
2498     /* Install the new entry ...  */
2499 install:
2500     lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2501     lp[0] = tswap32(entry_1);
2502     lp[1] = tswap32(entry_2);
2503     return 0;
2504 }
2505 
2506 /* specific and weird i386 syscalls */
2507 abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
2508                        unsigned long bytecount)
2509 {
2510     abi_long ret;
2511 
2512     switch (func) {
2513     case 0:
2514         ret = read_ldt(ptr, bytecount);
2515         break;
2516     case 1:
2517         ret = write_ldt(env, ptr, bytecount, 1);
2518         break;
2519     case 0x11:
2520         ret = write_ldt(env, ptr, bytecount, 0);
2521         break;
2522     default:
2523         ret = -TARGET_ENOSYS;
2524         break;
2525     }
2526     return ret;
2527 }
2528 
2529 abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
2530 {
2531     uint64_t *gdt_table = g2h(env->gdt.base);
2532     struct target_modify_ldt_ldt_s ldt_info;
2533     struct target_modify_ldt_ldt_s *target_ldt_info;
2534     int seg_32bit, contents, read_exec_only, limit_in_pages;
2535     int seg_not_present, useable, lm;
2536     uint32_t *lp, entry_1, entry_2;
2537     int i;
2538 
2539     lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
2540     if (!target_ldt_info)
2541         return -TARGET_EFAULT;
2542     ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2543     ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2544     ldt_info.limit = tswap32(target_ldt_info->limit);
2545     ldt_info.flags = tswap32(target_ldt_info->flags);
2546     if (ldt_info.entry_number == -1) {
2547         for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
2548             if (gdt_table[i] == 0) {
2549                 ldt_info.entry_number = i;
2550                 target_ldt_info->entry_number = tswap32(i);
2551                 break;
2552             }
2553         }
2554     }
2555     unlock_user_struct(target_ldt_info, ptr, 1);
2556 
2557     if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
2558         ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
2559            return -TARGET_EINVAL;
2560     seg_32bit = ldt_info.flags & 1;
2561     contents = (ldt_info.flags >> 1) & 3;
2562     read_exec_only = (ldt_info.flags >> 3) & 1;
2563     limit_in_pages = (ldt_info.flags >> 4) & 1;
2564     seg_not_present = (ldt_info.flags >> 5) & 1;
2565     useable = (ldt_info.flags >> 6) & 1;
2566 #ifdef TARGET_ABI32
2567     lm = 0;
2568 #else
2569     lm = (ldt_info.flags >> 7) & 1;
2570 #endif
2571 
2572     if (contents == 3) {
2573         if (seg_not_present == 0)
2574             return -TARGET_EINVAL;
2575     }
2576 
2577     /* NOTE: same code as Linux kernel */
2578     /* Allow LDTs to be cleared by the user. */
2579     if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2580         if ((contents == 0             &&
2581              read_exec_only == 1       &&
2582              seg_32bit == 0            &&
2583              limit_in_pages == 0       &&
2584              seg_not_present == 1      &&
2585              useable == 0 )) {
2586             entry_1 = 0;
2587             entry_2 = 0;
2588             goto install;
2589         }
2590     }
2591 
2592     entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2593         (ldt_info.limit & 0x0ffff);
2594     entry_2 = (ldt_info.base_addr & 0xff000000) |
2595         ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2596         (ldt_info.limit & 0xf0000) |
2597         ((read_exec_only ^ 1) << 9) |
2598         (contents << 10) |
2599         ((seg_not_present ^ 1) << 15) |
2600         (seg_32bit << 22) |
2601         (limit_in_pages << 23) |
2602         (useable << 20) |
2603         (lm << 21) |
2604         0x7000;
2605 
2606     /* Install the new entry ...  */
2607 install:
2608     lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
2609     lp[0] = tswap32(entry_1);
2610     lp[1] = tswap32(entry_2);
2611     return 0;
2612 }
2613 
2614 abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
2615 {
2616     struct target_modify_ldt_ldt_s *target_ldt_info;
2617     uint64_t *gdt_table = g2h(env->gdt.base);
2618     uint32_t base_addr, limit, flags;
2619     int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
2620     int seg_not_present, useable, lm;
2621     uint32_t *lp, entry_1, entry_2;
2622 
2623     lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
2624     if (!target_ldt_info)
2625         return -TARGET_EFAULT;
2626     idx = tswap32(target_ldt_info->entry_number);
2627     if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
2628         idx > TARGET_GDT_ENTRY_TLS_MAX) {
2629         unlock_user_struct(target_ldt_info, ptr, 1);
2630         return -TARGET_EINVAL;
2631     }
2632     lp = (uint32_t *)(gdt_table + idx);
2633     entry_1 = tswap32(lp[0]);
2634     entry_2 = tswap32(lp[1]);
2635 
2636     read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
2637     contents = (entry_2 >> 10) & 3;
2638     seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
2639     seg_32bit = (entry_2 >> 22) & 1;
2640     limit_in_pages = (entry_2 >> 23) & 1;
2641     useable = (entry_2 >> 20) & 1;
2642 #ifdef TARGET_ABI32
2643     lm = 0;
2644 #else
2645     lm = (entry_2 >> 21) & 1;
2646 #endif
2647     flags = (seg_32bit << 0) | (contents << 1) |
2648         (read_exec_only << 3) | (limit_in_pages << 4) |
2649         (seg_not_present << 5) | (useable << 6) | (lm << 7);
2650     limit = (entry_1 & 0xffff) | (entry_2  & 0xf0000);
2651     base_addr = (entry_1 >> 16) |
2652         (entry_2 & 0xff000000) |
2653         ((entry_2 & 0xff) << 16);
2654     target_ldt_info->base_addr = tswapl(base_addr);
2655     target_ldt_info->limit = tswap32(limit);
2656     target_ldt_info->flags = tswap32(flags);
2657     unlock_user_struct(target_ldt_info, ptr, 1);
2658     return 0;
2659 }
2660 
2661 #ifndef TARGET_ABI32
2662 abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
2663 {
2664     abi_long ret;
2665     abi_ulong val;
2666     int idx;
2667 
2668     switch(code) {
2669     case TARGET_ARCH_SET_GS:
2670     case TARGET_ARCH_SET_FS:
2671         if (code == TARGET_ARCH_SET_GS)
2672             idx = R_GS;
2673         else
2674             idx = R_FS;
2675         cpu_x86_load_seg(env, idx, 0);
2676         env->segs[idx].base = addr;
2677         break;
2678     case TARGET_ARCH_GET_GS:
2679     case TARGET_ARCH_GET_FS:
2680         if (code == TARGET_ARCH_GET_GS)
2681             idx = R_GS;
2682         else
2683             idx = R_FS;
2684         val = env->segs[idx].base;
2685         if (put_user(val, addr, abi_ulong))
2686             return -TARGET_EFAULT;
2687         break;
2688     default:
2689         ret = -TARGET_EINVAL;
2690         break;
2691     }
2692     return 0;
2693 }
2694 #endif
2695 
2696 #endif /* defined(TARGET_I386) */
2697 
2698 /* this stack is the equivalent of the kernel stack associated with a
2699    thread/process */
2700 #define NEW_STACK_SIZE 8192
2701 
2702 static int clone_func(void *arg)
2703 {
2704     CPUState *env = arg;
2705     cpu_loop(env);
2706     /* never exits */
2707     return 0;
2708 }
2709 
2710 /* do_fork() Must return host values and target errnos (unlike most
2711    do_*() functions). */
2712 int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp)
2713 {
2714     int ret;
2715     TaskState *ts;
2716     uint8_t *new_stack;
2717     CPUState *new_env;
2718 
2719     if (flags & CLONE_VM) {
2720         ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2721         memset(ts, 0, sizeof(TaskState));
2722         new_stack = ts->stack;
2723         ts->used = 1;
2724         /* add in task state list */
2725         ts->next = first_task_state;
2726         first_task_state = ts;
2727         /* we create a new CPU instance. */
2728         new_env = cpu_copy(env);
2729 #if defined(TARGET_I386)
2730         if (!newsp)
2731             newsp = env->regs[R_ESP];
2732         new_env->regs[R_ESP] = newsp;
2733         new_env->regs[R_EAX] = 0;
2734 #elif defined(TARGET_ARM)
2735         if (!newsp)
2736             newsp = env->regs[13];
2737         new_env->regs[13] = newsp;
2738         new_env->regs[0] = 0;
2739 #elif defined(TARGET_SPARC)
2740         if (!newsp)
2741             newsp = env->regwptr[22];
2742         new_env->regwptr[22] = newsp;
2743         new_env->regwptr[0] = 0;
2744 	/* XXXXX */
2745         printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
2746 #elif defined(TARGET_M68K)
2747         if (!newsp)
2748             newsp = env->aregs[7];
2749         new_env->aregs[7] = newsp;
2750         new_env->dregs[0] = 0;
2751         /* ??? is this sufficient?  */
2752 #elif defined(TARGET_MIPS)
2753         if (!newsp)
2754             newsp = env->gpr[env->current_tc][29];
2755         new_env->gpr[env->current_tc][29] = newsp;
2756 #elif defined(TARGET_PPC)
2757         if (!newsp)
2758             newsp = env->gpr[1];
2759         new_env->gpr[1] = newsp;
2760         {
2761             int i;
2762             for (i = 7; i < 32; i++)
2763                 new_env->gpr[i] = 0;
2764         }
2765 #elif defined(TARGET_SH4)
2766 	if (!newsp)
2767 	  newsp = env->gregs[15];
2768 	new_env->gregs[15] = newsp;
2769 	/* XXXXX */
2770 #elif defined(TARGET_ALPHA)
2771        if (!newsp)
2772          newsp = env->ir[30];
2773        new_env->ir[30] = newsp;
2774         /* ? */
2775         {
2776             int i;
2777             for (i = 7; i < 30; i++)
2778                 new_env->ir[i] = 0;
2779         }
2780 #elif defined(TARGET_CRIS)
2781 	if (!newsp)
2782 	  newsp = env->regs[14];
2783 	new_env->regs[14] = newsp;
2784 #else
2785 #error unsupported target CPU
2786 #endif
2787         new_env->opaque = ts;
2788 #ifdef __ia64__
2789         ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2790 #else
2791 	ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2792 #endif
2793     } else {
2794         /* if no CLONE_VM, we consider it is a fork */
2795         if ((flags & ~CSIGNAL) != 0)
2796             return -EINVAL;
2797         ret = fork();
2798     }
2799     return ret;
2800 }
2801 
2802 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
2803 {
2804     struct flock fl;
2805     struct target_flock *target_fl;
2806     struct flock64 fl64;
2807     struct target_flock64 *target_fl64;
2808     abi_long ret;
2809 
2810     switch(cmd) {
2811     case TARGET_F_GETLK:
2812         if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2813             return -TARGET_EFAULT;
2814         fl.l_type = tswap16(target_fl->l_type);
2815         fl.l_whence = tswap16(target_fl->l_whence);
2816         fl.l_start = tswapl(target_fl->l_start);
2817         fl.l_len = tswapl(target_fl->l_len);
2818         fl.l_pid = tswapl(target_fl->l_pid);
2819         unlock_user_struct(target_fl, arg, 0);
2820         ret = get_errno(fcntl(fd, cmd, &fl));
2821         if (ret == 0) {
2822             if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
2823                 return -TARGET_EFAULT;
2824             target_fl->l_type = tswap16(fl.l_type);
2825             target_fl->l_whence = tswap16(fl.l_whence);
2826             target_fl->l_start = tswapl(fl.l_start);
2827             target_fl->l_len = tswapl(fl.l_len);
2828             target_fl->l_pid = tswapl(fl.l_pid);
2829             unlock_user_struct(target_fl, arg, 1);
2830         }
2831         break;
2832 
2833     case TARGET_F_SETLK:
2834     case TARGET_F_SETLKW:
2835         if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2836             return -TARGET_EFAULT;
2837         fl.l_type = tswap16(target_fl->l_type);
2838         fl.l_whence = tswap16(target_fl->l_whence);
2839         fl.l_start = tswapl(target_fl->l_start);
2840         fl.l_len = tswapl(target_fl->l_len);
2841         fl.l_pid = tswapl(target_fl->l_pid);
2842         unlock_user_struct(target_fl, arg, 0);
2843         ret = get_errno(fcntl(fd, cmd, &fl));
2844         break;
2845 
2846     case TARGET_F_GETLK64:
2847         if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2848             return -TARGET_EFAULT;
2849         fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2850         fl64.l_whence = tswap16(target_fl64->l_whence);
2851         fl64.l_start = tswapl(target_fl64->l_start);
2852         fl64.l_len = tswapl(target_fl64->l_len);
2853         fl64.l_pid = tswap16(target_fl64->l_pid);
2854         unlock_user_struct(target_fl64, arg, 0);
2855         ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
2856         if (ret == 0) {
2857             if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
2858                 return -TARGET_EFAULT;
2859             target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2860             target_fl64->l_whence = tswap16(fl64.l_whence);
2861             target_fl64->l_start = tswapl(fl64.l_start);
2862             target_fl64->l_len = tswapl(fl64.l_len);
2863             target_fl64->l_pid = tswapl(fl64.l_pid);
2864             unlock_user_struct(target_fl64, arg, 1);
2865         }
2866         break;
2867     case TARGET_F_SETLK64:
2868     case TARGET_F_SETLKW64:
2869         if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2870             return -TARGET_EFAULT;
2871         fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2872         fl64.l_whence = tswap16(target_fl64->l_whence);
2873         fl64.l_start = tswapl(target_fl64->l_start);
2874         fl64.l_len = tswapl(target_fl64->l_len);
2875         fl64.l_pid = tswap16(target_fl64->l_pid);
2876         unlock_user_struct(target_fl64, arg, 0);
2877         ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
2878         break;
2879 
2880     case F_GETFL:
2881         ret = get_errno(fcntl(fd, cmd, arg));
2882         if (ret >= 0) {
2883             ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2884         }
2885         break;
2886 
2887     case F_SETFL:
2888         ret = get_errno(fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
2889         break;
2890 
2891     default:
2892         ret = get_errno(fcntl(fd, cmd, arg));
2893         break;
2894     }
2895     return ret;
2896 }
2897 
2898 #ifdef USE_UID16
2899 
2900 static inline int high2lowuid(int uid)
2901 {
2902     if (uid > 65535)
2903         return 65534;
2904     else
2905         return uid;
2906 }
2907 
2908 static inline int high2lowgid(int gid)
2909 {
2910     if (gid > 65535)
2911         return 65534;
2912     else
2913         return gid;
2914 }
2915 
2916 static inline int low2highuid(int uid)
2917 {
2918     if ((int16_t)uid == -1)
2919         return -1;
2920     else
2921         return uid;
2922 }
2923 
2924 static inline int low2highgid(int gid)
2925 {
2926     if ((int16_t)gid == -1)
2927         return -1;
2928     else
2929         return gid;
2930 }
2931 
2932 #endif /* USE_UID16 */
2933 
2934 void syscall_init(void)
2935 {
2936     IOCTLEntry *ie;
2937     const argtype *arg_type;
2938     int size;
2939     int i;
2940 
2941 #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2942 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
2943 #include "syscall_types.h"
2944 #undef STRUCT
2945 #undef STRUCT_SPECIAL
2946 
2947     /* we patch the ioctl size if necessary. We rely on the fact that
2948        no ioctl has all the bits at '1' in the size field */
2949     ie = ioctl_entries;
2950     while (ie->target_cmd != 0) {
2951         if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2952             TARGET_IOC_SIZEMASK) {
2953             arg_type = ie->arg_type;
2954             if (arg_type[0] != TYPE_PTR) {
2955                 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
2956                         ie->target_cmd);
2957                 exit(1);
2958             }
2959             arg_type++;
2960             size = thunk_type_size(arg_type, 0);
2961             ie->target_cmd = (ie->target_cmd &
2962                               ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2963                 (size << TARGET_IOC_SIZESHIFT);
2964         }
2965 
2966         /* Build target_to_host_errno_table[] table from
2967          * host_to_target_errno_table[]. */
2968         for (i=0; i < ERRNO_TABLE_SIZE; i++)
2969                 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
2970 
2971         /* automatic consistency check if same arch */
2972 #if defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)
2973         if (ie->target_cmd != ie->host_cmd) {
2974             fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
2975                     ie->target_cmd, ie->host_cmd);
2976         }
2977 #endif
2978         ie++;
2979     }
2980 }
2981 
2982 #if TARGET_ABI_BITS == 32
2983 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2984 {
2985 #ifdef TARGET_WORDS_BIG_ENDIAN
2986     return ((uint64_t)word0 << 32) | word1;
2987 #else
2988     return ((uint64_t)word1 << 32) | word0;
2989 #endif
2990 }
2991 #else /* TARGET_ABI_BITS == 32 */
2992 static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2993 {
2994     return word0;
2995 }
2996 #endif /* TARGET_ABI_BITS != 32 */
2997 
2998 #ifdef TARGET_NR_truncate64
2999 static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
3000                                          abi_long arg2,
3001                                          abi_long arg3,
3002                                          abi_long arg4)
3003 {
3004 #ifdef TARGET_ARM
3005     if (((CPUARMState *)cpu_env)->eabi)
3006       {
3007         arg2 = arg3;
3008         arg3 = arg4;
3009       }
3010 #endif
3011     return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
3012 }
3013 #endif
3014 
3015 #ifdef TARGET_NR_ftruncate64
3016 static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
3017                                           abi_long arg2,
3018                                           abi_long arg3,
3019                                           abi_long arg4)
3020 {
3021 #ifdef TARGET_ARM
3022     if (((CPUARMState *)cpu_env)->eabi)
3023       {
3024         arg2 = arg3;
3025         arg3 = arg4;
3026       }
3027 #endif
3028     return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
3029 }
3030 #endif
3031 
3032 static inline abi_long target_to_host_timespec(struct timespec *host_ts,
3033                                                abi_ulong target_addr)
3034 {
3035     struct target_timespec *target_ts;
3036 
3037     if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
3038         return -TARGET_EFAULT;
3039     host_ts->tv_sec = tswapl(target_ts->tv_sec);
3040     host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
3041     unlock_user_struct(target_ts, target_addr, 0);
3042 }
3043 
3044 static inline abi_long host_to_target_timespec(abi_ulong target_addr,
3045                                                struct timespec *host_ts)
3046 {
3047     struct target_timespec *target_ts;
3048 
3049     if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
3050         return -TARGET_EFAULT;
3051     target_ts->tv_sec = tswapl(host_ts->tv_sec);
3052     target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
3053     unlock_user_struct(target_ts, target_addr, 1);
3054 }
3055 
3056 /* do_syscall() should always have a single exit point at the end so
3057    that actions, such as logging of syscall results, can be performed.
3058    All errnos that do_syscall() returns must be -TARGET_<errcode>. */
3059 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
3060                     abi_long arg2, abi_long arg3, abi_long arg4,
3061                     abi_long arg5, abi_long arg6)
3062 {
3063     abi_long ret;
3064     struct stat st;
3065     struct statfs stfs;
3066     void *p;
3067 
3068 #ifdef DEBUG
3069     gemu_log("syscall %d", num);
3070 #endif
3071     if(do_strace)
3072         print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
3073 
3074     switch(num) {
3075     case TARGET_NR_exit:
3076 #ifdef HAVE_GPROF
3077         _mcleanup();
3078 #endif
3079         gdb_exit(cpu_env, arg1);
3080         /* XXX: should free thread stack and CPU env */
3081         _exit(arg1);
3082         ret = 0; /* avoid warning */
3083         break;
3084     case TARGET_NR_read:
3085         if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
3086             goto efault;
3087         ret = get_errno(read(arg1, p, arg3));
3088         unlock_user(p, arg2, ret);
3089         break;
3090     case TARGET_NR_write:
3091         if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
3092             goto efault;
3093         ret = get_errno(write(arg1, p, arg3));
3094         unlock_user(p, arg2, 0);
3095         break;
3096     case TARGET_NR_open:
3097         if (!(p = lock_user_string(arg1)))
3098             goto efault;
3099         ret = get_errno(open(path(p),
3100                              target_to_host_bitmask(arg2, fcntl_flags_tbl),
3101                              arg3));
3102         unlock_user(p, arg1, 0);
3103         break;
3104 #if defined(TARGET_NR_openat) && defined(__NR_openat)
3105     case TARGET_NR_openat:
3106         if (!(p = lock_user_string(arg2)))
3107             goto efault;
3108         ret = get_errno(sys_openat(arg1,
3109                                    path(p),
3110                                    target_to_host_bitmask(arg3, fcntl_flags_tbl),
3111                                    arg4));
3112         unlock_user(p, arg2, 0);
3113         break;
3114 #endif
3115     case TARGET_NR_close:
3116         ret = get_errno(close(arg1));
3117         break;
3118     case TARGET_NR_brk:
3119         ret = do_brk(arg1);
3120         break;
3121     case TARGET_NR_fork:
3122         ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
3123         break;
3124 #ifdef TARGET_NR_waitpid
3125     case TARGET_NR_waitpid:
3126         {
3127             int status;
3128             ret = get_errno(waitpid(arg1, &status, arg3));
3129             if (!is_error(ret) && arg2
3130                 && put_user_s32(status, arg2))
3131                 goto efault;
3132         }
3133         break;
3134 #endif
3135 #ifdef TARGET_NR_creat /* not on alpha */
3136     case TARGET_NR_creat:
3137         if (!(p = lock_user_string(arg1)))
3138             goto efault;
3139         ret = get_errno(creat(p, arg2));
3140         unlock_user(p, arg1, 0);
3141         break;
3142 #endif
3143     case TARGET_NR_link:
3144         {
3145             void * p2;
3146             p = lock_user_string(arg1);
3147             p2 = lock_user_string(arg2);
3148             if (!p || !p2)
3149                 ret = -TARGET_EFAULT;
3150             else
3151                 ret = get_errno(link(p, p2));
3152             unlock_user(p2, arg2, 0);
3153             unlock_user(p, arg1, 0);
3154         }
3155         break;
3156 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
3157     case TARGET_NR_linkat:
3158         {
3159             void * p2 = NULL;
3160             if (!arg2 || !arg4)
3161                 goto efault;
3162             p  = lock_user_string(arg2);
3163             p2 = lock_user_string(arg4);
3164             if (!p || !p2)
3165                 ret = -TARGET_EFAULT;
3166             else
3167                 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
3168             unlock_user(p, arg2, 0);
3169             unlock_user(p2, arg4, 0);
3170         }
3171         break;
3172 #endif
3173     case TARGET_NR_unlink:
3174         if (!(p = lock_user_string(arg1)))
3175             goto efault;
3176         ret = get_errno(unlink(p));
3177         unlock_user(p, arg1, 0);
3178         break;
3179 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
3180     case TARGET_NR_unlinkat:
3181         if (!(p = lock_user_string(arg2)))
3182             goto efault;
3183         ret = get_errno(sys_unlinkat(arg1, p, arg3));
3184         unlock_user(p, arg2, 0);
3185         break;
3186 #endif
3187     case TARGET_NR_execve:
3188         {
3189             char **argp, **envp;
3190             int argc, envc;
3191             abi_ulong gp;
3192             abi_ulong guest_argp;
3193             abi_ulong guest_envp;
3194             abi_ulong addr;
3195             char **q;
3196 
3197             argc = 0;
3198             guest_argp = arg2;
3199             for (gp = guest_argp; ; gp += sizeof(abi_ulong)) {
3200                 if (get_user_ual(addr, gp))
3201                     goto efault;
3202                 if (!addr)
3203                     break;
3204                 argc++;
3205             }
3206             envc = 0;
3207             guest_envp = arg3;
3208             for (gp = guest_envp; ; gp += sizeof(abi_ulong)) {
3209                 if (get_user_ual(addr, gp))
3210                     goto efault;
3211                 if (!addr)
3212                     break;
3213                 envc++;
3214             }
3215 
3216             argp = alloca((argc + 1) * sizeof(void *));
3217             envp = alloca((envc + 1) * sizeof(void *));
3218 
3219             for (gp = guest_argp, q = argp; ;
3220                   gp += sizeof(abi_ulong), q++) {
3221                 if (get_user_ual(addr, gp))
3222                     goto execve_efault;
3223                 if (!addr)
3224                     break;
3225                 if (!(*q = lock_user_string(addr)))
3226                     goto execve_efault;
3227             }
3228             *q = NULL;
3229 
3230             for (gp = guest_envp, q = envp; ;
3231                   gp += sizeof(abi_ulong), q++) {
3232                 if (get_user_ual(addr, gp))
3233                     goto execve_efault;
3234                 if (!addr)
3235                     break;
3236                 if (!(*q = lock_user_string(addr)))
3237                     goto execve_efault;
3238             }
3239             *q = NULL;
3240 
3241             if (!(p = lock_user_string(arg1)))
3242                 goto execve_efault;
3243             ret = get_errno(execve(p, argp, envp));
3244             unlock_user(p, arg1, 0);
3245 
3246             goto execve_end;
3247 
3248         execve_efault:
3249             ret = -TARGET_EFAULT;
3250 
3251         execve_end:
3252             for (gp = guest_argp, q = argp; *q;
3253                   gp += sizeof(abi_ulong), q++) {
3254                 if (get_user_ual(addr, gp)
3255                     || !addr)
3256                     break;
3257                 unlock_user(*q, addr, 0);
3258             }
3259             for (gp = guest_envp, q = envp; *q;
3260                   gp += sizeof(abi_ulong), q++) {
3261                 if (get_user_ual(addr, gp)
3262                     || !addr)
3263                     break;
3264                 unlock_user(*q, addr, 0);
3265             }
3266         }
3267         break;
3268     case TARGET_NR_chdir:
3269         if (!(p = lock_user_string(arg1)))
3270             goto efault;
3271         ret = get_errno(chdir(p));
3272         unlock_user(p, arg1, 0);
3273         break;
3274 #ifdef TARGET_NR_time
3275     case TARGET_NR_time:
3276         {
3277             time_t host_time;
3278             ret = get_errno(time(&host_time));
3279             if (!is_error(ret)
3280                 && arg1
3281                 && put_user_sal(host_time, arg1))
3282                 goto efault;
3283         }
3284         break;
3285 #endif
3286     case TARGET_NR_mknod:
3287         if (!(p = lock_user_string(arg1)))
3288             goto efault;
3289         ret = get_errno(mknod(p, arg2, arg3));
3290         unlock_user(p, arg1, 0);
3291         break;
3292 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
3293     case TARGET_NR_mknodat:
3294         if (!(p = lock_user_string(arg2)))
3295             goto efault;
3296         ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
3297         unlock_user(p, arg2, 0);
3298         break;
3299 #endif
3300     case TARGET_NR_chmod:
3301         if (!(p = lock_user_string(arg1)))
3302             goto efault;
3303         ret = get_errno(chmod(p, arg2));
3304         unlock_user(p, arg1, 0);
3305         break;
3306 #ifdef TARGET_NR_break
3307     case TARGET_NR_break:
3308         goto unimplemented;
3309 #endif
3310 #ifdef TARGET_NR_oldstat
3311     case TARGET_NR_oldstat:
3312         goto unimplemented;
3313 #endif
3314     case TARGET_NR_lseek:
3315         ret = get_errno(lseek(arg1, arg2, arg3));
3316         break;
3317 #ifdef TARGET_NR_getxpid
3318     case TARGET_NR_getxpid:
3319 #else
3320     case TARGET_NR_getpid:
3321 #endif
3322         ret = get_errno(getpid());
3323         break;
3324     case TARGET_NR_mount:
3325 		{
3326 			/* need to look at the data field */
3327 			void *p2, *p3;
3328 			p = lock_user_string(arg1);
3329 			p2 = lock_user_string(arg2);
3330 			p3 = lock_user_string(arg3);
3331                         if (!p || !p2 || !p3)
3332                             ret = -TARGET_EFAULT;
3333                         else
3334                             /* FIXME - arg5 should be locked, but it isn't clear how to
3335                              * do that since it's not guaranteed to be a NULL-terminated
3336                              * string.
3337                              */
3338                             ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
3339                         unlock_user(p, arg1, 0);
3340                         unlock_user(p2, arg2, 0);
3341                         unlock_user(p3, arg3, 0);
3342 			break;
3343 		}
3344 #ifdef TARGET_NR_umount
3345     case TARGET_NR_umount:
3346         if (!(p = lock_user_string(arg1)))
3347             goto efault;
3348         ret = get_errno(umount(p));
3349         unlock_user(p, arg1, 0);
3350         break;
3351 #endif
3352 #ifdef TARGET_NR_stime /* not on alpha */
3353     case TARGET_NR_stime:
3354         {
3355             time_t host_time;
3356             if (get_user_sal(host_time, arg1))
3357                 goto efault;
3358             ret = get_errno(stime(&host_time));
3359         }
3360         break;
3361 #endif
3362     case TARGET_NR_ptrace:
3363         goto unimplemented;
3364 #ifdef TARGET_NR_alarm /* not on alpha */
3365     case TARGET_NR_alarm:
3366         ret = alarm(arg1);
3367         break;
3368 #endif
3369 #ifdef TARGET_NR_oldfstat
3370     case TARGET_NR_oldfstat:
3371         goto unimplemented;
3372 #endif
3373 #ifdef TARGET_NR_pause /* not on alpha */
3374     case TARGET_NR_pause:
3375         ret = get_errno(pause());
3376         break;
3377 #endif
3378 #ifdef TARGET_NR_utime
3379     case TARGET_NR_utime:
3380         {
3381             struct utimbuf tbuf, *host_tbuf;
3382             struct target_utimbuf *target_tbuf;
3383             if (arg2) {
3384                 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
3385                     goto efault;
3386                 tbuf.actime = tswapl(target_tbuf->actime);
3387                 tbuf.modtime = tswapl(target_tbuf->modtime);
3388                 unlock_user_struct(target_tbuf, arg2, 0);
3389                 host_tbuf = &tbuf;
3390             } else {
3391                 host_tbuf = NULL;
3392             }
3393             if (!(p = lock_user_string(arg1)))
3394                 goto efault;
3395             ret = get_errno(utime(p, host_tbuf));
3396             unlock_user(p, arg1, 0);
3397         }
3398         break;
3399 #endif
3400     case TARGET_NR_utimes:
3401         {
3402             struct timeval *tvp, tv[2];
3403             if (arg2) {
3404                 if (copy_from_user_timeval(&tv[0], arg2)
3405                     || copy_from_user_timeval(&tv[1],
3406                                               arg2 + sizeof(struct target_timeval)))
3407                     goto efault;
3408                 tvp = tv;
3409             } else {
3410                 tvp = NULL;
3411             }
3412             if (!(p = lock_user_string(arg1)))
3413                 goto efault;
3414             ret = get_errno(utimes(p, tvp));
3415             unlock_user(p, arg1, 0);
3416         }
3417         break;
3418 #ifdef TARGET_NR_stty
3419     case TARGET_NR_stty:
3420         goto unimplemented;
3421 #endif
3422 #ifdef TARGET_NR_gtty
3423     case TARGET_NR_gtty:
3424         goto unimplemented;
3425 #endif
3426     case TARGET_NR_access:
3427         if (!(p = lock_user_string(arg1)))
3428             goto efault;
3429         ret = get_errno(access(p, arg2));
3430         unlock_user(p, arg1, 0);
3431         break;
3432 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
3433     case TARGET_NR_faccessat:
3434         if (!(p = lock_user_string(arg2)))
3435             goto efault;
3436         ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
3437         unlock_user(p, arg2, 0);
3438         break;
3439 #endif
3440 #ifdef TARGET_NR_nice /* not on alpha */
3441     case TARGET_NR_nice:
3442         ret = get_errno(nice(arg1));
3443         break;
3444 #endif
3445 #ifdef TARGET_NR_ftime
3446     case TARGET_NR_ftime:
3447         goto unimplemented;
3448 #endif
3449     case TARGET_NR_sync:
3450         sync();
3451         ret = 0;
3452         break;
3453     case TARGET_NR_kill:
3454         ret = get_errno(kill(arg1, arg2));
3455         break;
3456     case TARGET_NR_rename:
3457         {
3458             void *p2;
3459             p = lock_user_string(arg1);
3460             p2 = lock_user_string(arg2);
3461             if (!p || !p2)
3462                 ret = -TARGET_EFAULT;
3463             else
3464                 ret = get_errno(rename(p, p2));
3465             unlock_user(p2, arg2, 0);
3466             unlock_user(p, arg1, 0);
3467         }
3468         break;
3469 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
3470     case TARGET_NR_renameat:
3471         {
3472             void *p2;
3473             p  = lock_user_string(arg2);
3474             p2 = lock_user_string(arg4);
3475             if (!p || !p2)
3476                 ret = -TARGET_EFAULT;
3477             else
3478                 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
3479             unlock_user(p2, arg4, 0);
3480             unlock_user(p, arg2, 0);
3481         }
3482         break;
3483 #endif
3484     case TARGET_NR_mkdir:
3485         if (!(p = lock_user_string(arg1)))
3486             goto efault;
3487         ret = get_errno(mkdir(p, arg2));
3488         unlock_user(p, arg1, 0);
3489         break;
3490 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
3491     case TARGET_NR_mkdirat:
3492         if (!(p = lock_user_string(arg2)))
3493             goto efault;
3494         ret = get_errno(sys_mkdirat(arg1, p, arg3));
3495         unlock_user(p, arg2, 0);
3496         break;
3497 #endif
3498     case TARGET_NR_rmdir:
3499         if (!(p = lock_user_string(arg1)))
3500             goto efault;
3501         ret = get_errno(rmdir(p));
3502         unlock_user(p, arg1, 0);
3503         break;
3504     case TARGET_NR_dup:
3505         ret = get_errno(dup(arg1));
3506         break;
3507     case TARGET_NR_pipe:
3508         {
3509             int host_pipe[2];
3510             ret = get_errno(pipe(host_pipe));
3511             if (!is_error(ret)) {
3512 #if defined(TARGET_MIPS)
3513                 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
3514 		env->gpr[env->current_tc][3] = host_pipe[1];
3515 		ret = host_pipe[0];
3516 #elif defined(TARGET_SH4)
3517 		((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
3518 		ret = host_pipe[0];
3519 #else
3520                 if (put_user_s32(host_pipe[0], arg1)
3521                     || put_user_s32(host_pipe[1], arg1 + sizeof(host_pipe[0])))
3522                     goto efault;
3523 #endif
3524             }
3525         }
3526         break;
3527     case TARGET_NR_times:
3528         {
3529             struct target_tms *tmsp;
3530             struct tms tms;
3531             ret = get_errno(times(&tms));
3532             if (arg1) {
3533                 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
3534                 if (!tmsp)
3535                     goto efault;
3536                 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3537                 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3538                 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3539                 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
3540             }
3541             if (!is_error(ret))
3542                 ret = host_to_target_clock_t(ret);
3543         }
3544         break;
3545 #ifdef TARGET_NR_prof
3546     case TARGET_NR_prof:
3547         goto unimplemented;
3548 #endif
3549 #ifdef TARGET_NR_signal
3550     case TARGET_NR_signal:
3551         goto unimplemented;
3552 #endif
3553     case TARGET_NR_acct:
3554         if (!(p = lock_user_string(arg1)))
3555             goto efault;
3556         ret = get_errno(acct(path(p)));
3557         unlock_user(p, arg1, 0);
3558         break;
3559 #ifdef TARGET_NR_umount2 /* not on alpha */
3560     case TARGET_NR_umount2:
3561         if (!(p = lock_user_string(arg1)))
3562             goto efault;
3563         ret = get_errno(umount2(p, arg2));
3564         unlock_user(p, arg1, 0);
3565         break;
3566 #endif
3567 #ifdef TARGET_NR_lock
3568     case TARGET_NR_lock:
3569         goto unimplemented;
3570 #endif
3571     case TARGET_NR_ioctl:
3572         ret = do_ioctl(arg1, arg2, arg3);
3573         break;
3574     case TARGET_NR_fcntl:
3575         ret = do_fcntl(arg1, arg2, arg3);
3576         break;
3577 #ifdef TARGET_NR_mpx
3578     case TARGET_NR_mpx:
3579         goto unimplemented;
3580 #endif
3581     case TARGET_NR_setpgid:
3582         ret = get_errno(setpgid(arg1, arg2));
3583         break;
3584 #ifdef TARGET_NR_ulimit
3585     case TARGET_NR_ulimit:
3586         goto unimplemented;
3587 #endif
3588 #ifdef TARGET_NR_oldolduname
3589     case TARGET_NR_oldolduname:
3590         goto unimplemented;
3591 #endif
3592     case TARGET_NR_umask:
3593         ret = get_errno(umask(arg1));
3594         break;
3595     case TARGET_NR_chroot:
3596         if (!(p = lock_user_string(arg1)))
3597             goto efault;
3598         ret = get_errno(chroot(p));
3599         unlock_user(p, arg1, 0);
3600         break;
3601     case TARGET_NR_ustat:
3602         goto unimplemented;
3603     case TARGET_NR_dup2:
3604         ret = get_errno(dup2(arg1, arg2));
3605         break;
3606 #ifdef TARGET_NR_getppid /* not on alpha */
3607     case TARGET_NR_getppid:
3608         ret = get_errno(getppid());
3609         break;
3610 #endif
3611     case TARGET_NR_getpgrp:
3612         ret = get_errno(getpgrp());
3613         break;
3614     case TARGET_NR_setsid:
3615         ret = get_errno(setsid());
3616         break;
3617 #ifdef TARGET_NR_sigaction
3618     case TARGET_NR_sigaction:
3619         {
3620 #if !defined(TARGET_MIPS)
3621             struct target_old_sigaction *old_act;
3622             struct target_sigaction act, oact, *pact;
3623             if (arg2) {
3624                 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3625                     goto efault;
3626                 act._sa_handler = old_act->_sa_handler;
3627                 target_siginitset(&act.sa_mask, old_act->sa_mask);
3628                 act.sa_flags = old_act->sa_flags;
3629                 act.sa_restorer = old_act->sa_restorer;
3630                 unlock_user_struct(old_act, arg2, 0);
3631                 pact = &act;
3632             } else {
3633                 pact = NULL;
3634             }
3635             ret = get_errno(do_sigaction(arg1, pact, &oact));
3636             if (!is_error(ret) && arg3) {
3637                 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3638                     goto efault;
3639                 old_act->_sa_handler = oact._sa_handler;
3640                 old_act->sa_mask = oact.sa_mask.sig[0];
3641                 old_act->sa_flags = oact.sa_flags;
3642                 old_act->sa_restorer = oact.sa_restorer;
3643                 unlock_user_struct(old_act, arg3, 1);
3644             }
3645 #else
3646 	    struct target_sigaction act, oact, *pact, *old_act;
3647 
3648 	    if (arg2) {
3649                 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3650                     goto efault;
3651 		act._sa_handler = old_act->_sa_handler;
3652 		target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3653 		act.sa_flags = old_act->sa_flags;
3654 		unlock_user_struct(old_act, arg2, 0);
3655 		pact = &act;
3656 	    } else {
3657 		pact = NULL;
3658 	    }
3659 
3660 	    ret = get_errno(do_sigaction(arg1, pact, &oact));
3661 
3662 	    if (!is_error(ret) && arg3) {
3663                 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3664                     goto efault;
3665 		old_act->_sa_handler = oact._sa_handler;
3666 		old_act->sa_flags = oact.sa_flags;
3667 		old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3668 		old_act->sa_mask.sig[1] = 0;
3669 		old_act->sa_mask.sig[2] = 0;
3670 		old_act->sa_mask.sig[3] = 0;
3671 		unlock_user_struct(old_act, arg3, 1);
3672 	    }
3673 #endif
3674         }
3675         break;
3676 #endif
3677     case TARGET_NR_rt_sigaction:
3678         {
3679             struct target_sigaction *act;
3680             struct target_sigaction *oact;
3681 
3682             if (arg2) {
3683                 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
3684                     goto efault;
3685             } else
3686                 act = NULL;
3687             if (arg3) {
3688                 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
3689                     ret = -TARGET_EFAULT;
3690                     goto rt_sigaction_fail;
3691                 }
3692             } else
3693                 oact = NULL;
3694             ret = get_errno(do_sigaction(arg1, act, oact));
3695 	rt_sigaction_fail:
3696             if (act)
3697                 unlock_user_struct(act, arg2, 0);
3698             if (oact)
3699                 unlock_user_struct(oact, arg3, 1);
3700         }
3701         break;
3702 #ifdef TARGET_NR_sgetmask /* not on alpha */
3703     case TARGET_NR_sgetmask:
3704         {
3705             sigset_t cur_set;
3706             abi_ulong target_set;
3707             sigprocmask(0, NULL, &cur_set);
3708             host_to_target_old_sigset(&target_set, &cur_set);
3709             ret = target_set;
3710         }
3711         break;
3712 #endif
3713 #ifdef TARGET_NR_ssetmask /* not on alpha */
3714     case TARGET_NR_ssetmask:
3715         {
3716             sigset_t set, oset, cur_set;
3717             abi_ulong target_set = arg1;
3718             sigprocmask(0, NULL, &cur_set);
3719             target_to_host_old_sigset(&set, &target_set);
3720             sigorset(&set, &set, &cur_set);
3721             sigprocmask(SIG_SETMASK, &set, &oset);
3722             host_to_target_old_sigset(&target_set, &oset);
3723             ret = target_set;
3724         }
3725         break;
3726 #endif
3727 #ifdef TARGET_NR_sigprocmask
3728     case TARGET_NR_sigprocmask:
3729         {
3730             int how = arg1;
3731             sigset_t set, oldset, *set_ptr;
3732 
3733             if (arg2) {
3734                 switch(how) {
3735                 case TARGET_SIG_BLOCK:
3736                     how = SIG_BLOCK;
3737                     break;
3738                 case TARGET_SIG_UNBLOCK:
3739                     how = SIG_UNBLOCK;
3740                     break;
3741                 case TARGET_SIG_SETMASK:
3742                     how = SIG_SETMASK;
3743                     break;
3744                 default:
3745                     ret = -TARGET_EINVAL;
3746                     goto fail;
3747                 }
3748                 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3749                     goto efault;
3750                 target_to_host_old_sigset(&set, p);
3751                 unlock_user(p, arg2, 0);
3752                 set_ptr = &set;
3753             } else {
3754                 how = 0;
3755                 set_ptr = NULL;
3756             }
3757             ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
3758             if (!is_error(ret) && arg3) {
3759                 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3760                     goto efault;
3761                 host_to_target_old_sigset(p, &oldset);
3762                 unlock_user(p, arg3, sizeof(target_sigset_t));
3763             }
3764         }
3765         break;
3766 #endif
3767     case TARGET_NR_rt_sigprocmask:
3768         {
3769             int how = arg1;
3770             sigset_t set, oldset, *set_ptr;
3771 
3772             if (arg2) {
3773                 switch(how) {
3774                 case TARGET_SIG_BLOCK:
3775                     how = SIG_BLOCK;
3776                     break;
3777                 case TARGET_SIG_UNBLOCK:
3778                     how = SIG_UNBLOCK;
3779                     break;
3780                 case TARGET_SIG_SETMASK:
3781                     how = SIG_SETMASK;
3782                     break;
3783                 default:
3784                     ret = -TARGET_EINVAL;
3785                     goto fail;
3786                 }
3787                 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3788                     goto efault;
3789                 target_to_host_sigset(&set, p);
3790                 unlock_user(p, arg2, 0);
3791                 set_ptr = &set;
3792             } else {
3793                 how = 0;
3794                 set_ptr = NULL;
3795             }
3796             ret = get_errno(sigprocmask(how, set_ptr, &oldset));
3797             if (!is_error(ret) && arg3) {
3798                 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3799                     goto efault;
3800                 host_to_target_sigset(p, &oldset);
3801                 unlock_user(p, arg3, sizeof(target_sigset_t));
3802             }
3803         }
3804         break;
3805 #ifdef TARGET_NR_sigpending
3806     case TARGET_NR_sigpending:
3807         {
3808             sigset_t set;
3809             ret = get_errno(sigpending(&set));
3810             if (!is_error(ret)) {
3811                 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3812                     goto efault;
3813                 host_to_target_old_sigset(p, &set);
3814                 unlock_user(p, arg1, sizeof(target_sigset_t));
3815             }
3816         }
3817         break;
3818 #endif
3819     case TARGET_NR_rt_sigpending:
3820         {
3821             sigset_t set;
3822             ret = get_errno(sigpending(&set));
3823             if (!is_error(ret)) {
3824                 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3825                     goto efault;
3826                 host_to_target_sigset(p, &set);
3827                 unlock_user(p, arg1, sizeof(target_sigset_t));
3828             }
3829         }
3830         break;
3831 #ifdef TARGET_NR_sigsuspend
3832     case TARGET_NR_sigsuspend:
3833         {
3834             sigset_t set;
3835             if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3836                 goto efault;
3837             target_to_host_old_sigset(&set, p);
3838             unlock_user(p, arg1, 0);
3839             ret = get_errno(sigsuspend(&set));
3840         }
3841         break;
3842 #endif
3843     case TARGET_NR_rt_sigsuspend:
3844         {
3845             sigset_t set;
3846             if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3847                 goto efault;
3848             target_to_host_sigset(&set, p);
3849             unlock_user(p, arg1, 0);
3850             ret = get_errno(sigsuspend(&set));
3851         }
3852         break;
3853     case TARGET_NR_rt_sigtimedwait:
3854         {
3855             sigset_t set;
3856             struct timespec uts, *puts;
3857             siginfo_t uinfo;
3858 
3859             if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3860                 goto efault;
3861             target_to_host_sigset(&set, p);
3862             unlock_user(p, arg1, 0);
3863             if (arg3) {
3864                 puts = &uts;
3865                 target_to_host_timespec(puts, arg3);
3866             } else {
3867                 puts = NULL;
3868             }
3869             ret = get_errno(sigtimedwait(&set, &uinfo, puts));
3870             if (!is_error(ret) && arg2) {
3871                 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_sigset_t), 0)))
3872                     goto efault;
3873                 host_to_target_siginfo(p, &uinfo);
3874                 unlock_user(p, arg2, sizeof(target_sigset_t));
3875             }
3876         }
3877         break;
3878     case TARGET_NR_rt_sigqueueinfo:
3879         {
3880             siginfo_t uinfo;
3881             if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
3882                 goto efault;
3883             target_to_host_siginfo(&uinfo, p);
3884             unlock_user(p, arg1, 0);
3885             ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3886         }
3887         break;
3888 #ifdef TARGET_NR_sigreturn
3889     case TARGET_NR_sigreturn:
3890         /* NOTE: ret is eax, so not transcoding must be done */
3891         ret = do_sigreturn(cpu_env);
3892         break;
3893 #endif
3894     case TARGET_NR_rt_sigreturn:
3895         /* NOTE: ret is eax, so not transcoding must be done */
3896         ret = do_rt_sigreturn(cpu_env);
3897         break;
3898     case TARGET_NR_sethostname:
3899         if (!(p = lock_user_string(arg1)))
3900             goto efault;
3901         ret = get_errno(sethostname(p, arg2));
3902         unlock_user(p, arg1, 0);
3903         break;
3904     case TARGET_NR_setrlimit:
3905         {
3906             /* XXX: convert resource ? */
3907             int resource = arg1;
3908             struct target_rlimit *target_rlim;
3909             struct rlimit rlim;
3910             if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
3911                 goto efault;
3912             rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3913             rlim.rlim_max = tswapl(target_rlim->rlim_max);
3914             unlock_user_struct(target_rlim, arg2, 0);
3915             ret = get_errno(setrlimit(resource, &rlim));
3916         }
3917         break;
3918     case TARGET_NR_getrlimit:
3919         {
3920             /* XXX: convert resource ? */
3921             int resource = arg1;
3922             struct target_rlimit *target_rlim;
3923             struct rlimit rlim;
3924 
3925             ret = get_errno(getrlimit(resource, &rlim));
3926             if (!is_error(ret)) {
3927                 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
3928                     goto efault;
3929                 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3930                 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3931                 unlock_user_struct(target_rlim, arg2, 1);
3932             }
3933         }
3934         break;
3935     case TARGET_NR_getrusage:
3936         {
3937             struct rusage rusage;
3938             ret = get_errno(getrusage(arg1, &rusage));
3939             if (!is_error(ret)) {
3940                 host_to_target_rusage(arg2, &rusage);
3941             }
3942         }
3943         break;
3944     case TARGET_NR_gettimeofday:
3945         {
3946             struct timeval tv;
3947             ret = get_errno(gettimeofday(&tv, NULL));
3948             if (!is_error(ret)) {
3949                 if (copy_to_user_timeval(arg1, &tv))
3950                     goto efault;
3951             }
3952         }
3953         break;
3954     case TARGET_NR_settimeofday:
3955         {
3956             struct timeval tv;
3957             if (copy_from_user_timeval(&tv, arg1))
3958                 goto efault;
3959             ret = get_errno(settimeofday(&tv, NULL));
3960         }
3961         break;
3962 #ifdef TARGET_NR_select
3963     case TARGET_NR_select:
3964         {
3965             struct target_sel_arg_struct *sel;
3966             abi_ulong inp, outp, exp, tvp;
3967             long nsel;
3968 
3969             if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
3970                 goto efault;
3971             nsel = tswapl(sel->n);
3972             inp = tswapl(sel->inp);
3973             outp = tswapl(sel->outp);
3974             exp = tswapl(sel->exp);
3975             tvp = tswapl(sel->tvp);
3976             unlock_user_struct(sel, arg1, 0);
3977             ret = do_select(nsel, inp, outp, exp, tvp);
3978         }
3979         break;
3980 #endif
3981     case TARGET_NR_symlink:
3982         {
3983             void *p2;
3984             p = lock_user_string(arg1);
3985             p2 = lock_user_string(arg2);
3986             if (!p || !p2)
3987                 ret = -TARGET_EFAULT;
3988             else
3989                 ret = get_errno(symlink(p, p2));
3990             unlock_user(p2, arg2, 0);
3991             unlock_user(p, arg1, 0);
3992         }
3993         break;
3994 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
3995     case TARGET_NR_symlinkat:
3996         {
3997             void *p2;
3998             p  = lock_user_string(arg1);
3999             p2 = lock_user_string(arg3);
4000             if (!p || !p2)
4001                 ret = -TARGET_EFAULT;
4002             else
4003                 ret = get_errno(sys_symlinkat(p, arg2, p2));
4004             unlock_user(p2, arg3, 0);
4005             unlock_user(p, arg1, 0);
4006         }
4007         break;
4008 #endif
4009 #ifdef TARGET_NR_oldlstat
4010     case TARGET_NR_oldlstat:
4011         goto unimplemented;
4012 #endif
4013     case TARGET_NR_readlink:
4014         {
4015             void *p2;
4016             p = lock_user_string(arg1);
4017             p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
4018             if (!p || !p2)
4019                 ret = -TARGET_EFAULT;
4020             else
4021                 ret = get_errno(readlink(path(p), p2, arg3));
4022             unlock_user(p2, arg2, ret);
4023             unlock_user(p, arg1, 0);
4024         }
4025         break;
4026 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
4027     case TARGET_NR_readlinkat:
4028         {
4029             void *p2;
4030             p  = lock_user_string(arg2);
4031             p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
4032             if (!p || !p2)
4033         	ret = -TARGET_EFAULT;
4034             else
4035                 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
4036             unlock_user(p2, arg3, ret);
4037             unlock_user(p, arg2, 0);
4038         }
4039         break;
4040 #endif
4041 #ifdef TARGET_NR_uselib
4042     case TARGET_NR_uselib:
4043         goto unimplemented;
4044 #endif
4045 #ifdef TARGET_NR_swapon
4046     case TARGET_NR_swapon:
4047         if (!(p = lock_user_string(arg1)))
4048             goto efault;
4049         ret = get_errno(swapon(p, arg2));
4050         unlock_user(p, arg1, 0);
4051         break;
4052 #endif
4053     case TARGET_NR_reboot:
4054         goto unimplemented;
4055 #ifdef TARGET_NR_readdir
4056     case TARGET_NR_readdir:
4057         goto unimplemented;
4058 #endif
4059 #ifdef TARGET_NR_mmap
4060     case TARGET_NR_mmap:
4061 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
4062         {
4063             abi_ulong *v;
4064             abi_ulong v1, v2, v3, v4, v5, v6;
4065             if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
4066                 goto efault;
4067             v1 = tswapl(v[0]);
4068             v2 = tswapl(v[1]);
4069             v3 = tswapl(v[2]);
4070             v4 = tswapl(v[3]);
4071             v5 = tswapl(v[4]);
4072             v6 = tswapl(v[5]);
4073             unlock_user(v, arg1, 0);
4074             ret = get_errno(target_mmap(v1, v2, v3,
4075                                         target_to_host_bitmask(v4, mmap_flags_tbl),
4076                                         v5, v6));
4077         }
4078 #else
4079         ret = get_errno(target_mmap(arg1, arg2, arg3,
4080                                     target_to_host_bitmask(arg4, mmap_flags_tbl),
4081                                     arg5,
4082                                     arg6));
4083 #endif
4084         break;
4085 #endif
4086 #ifdef TARGET_NR_mmap2
4087     case TARGET_NR_mmap2:
4088 #ifndef MMAP_SHIFT
4089 #define MMAP_SHIFT 12
4090 #endif
4091         ret = get_errno(target_mmap(arg1, arg2, arg3,
4092                                     target_to_host_bitmask(arg4, mmap_flags_tbl),
4093                                     arg5,
4094                                     arg6 << MMAP_SHIFT));
4095         break;
4096 #endif
4097     case TARGET_NR_munmap:
4098         ret = get_errno(target_munmap(arg1, arg2));
4099         break;
4100     case TARGET_NR_mprotect:
4101         ret = get_errno(target_mprotect(arg1, arg2, arg3));
4102         break;
4103 #ifdef TARGET_NR_mremap
4104     case TARGET_NR_mremap:
4105         ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
4106         break;
4107 #endif
4108         /* ??? msync/mlock/munlock are broken for softmmu.  */
4109 #ifdef TARGET_NR_msync
4110     case TARGET_NR_msync:
4111         ret = get_errno(msync(g2h(arg1), arg2, arg3));
4112         break;
4113 #endif
4114 #ifdef TARGET_NR_mlock
4115     case TARGET_NR_mlock:
4116         ret = get_errno(mlock(g2h(arg1), arg2));
4117         break;
4118 #endif
4119 #ifdef TARGET_NR_munlock
4120     case TARGET_NR_munlock:
4121         ret = get_errno(munlock(g2h(arg1), arg2));
4122         break;
4123 #endif
4124 #ifdef TARGET_NR_mlockall
4125     case TARGET_NR_mlockall:
4126         ret = get_errno(mlockall(arg1));
4127         break;
4128 #endif
4129 #ifdef TARGET_NR_munlockall
4130     case TARGET_NR_munlockall:
4131         ret = get_errno(munlockall());
4132         break;
4133 #endif
4134     case TARGET_NR_truncate:
4135         if (!(p = lock_user_string(arg1)))
4136             goto efault;
4137         ret = get_errno(truncate(p, arg2));
4138         unlock_user(p, arg1, 0);
4139         break;
4140     case TARGET_NR_ftruncate:
4141         ret = get_errno(ftruncate(arg1, arg2));
4142         break;
4143     case TARGET_NR_fchmod:
4144         ret = get_errno(fchmod(arg1, arg2));
4145         break;
4146 #if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
4147     case TARGET_NR_fchmodat:
4148         if (!(p = lock_user_string(arg2)))
4149             goto efault;
4150         ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
4151         unlock_user(p, arg2, 0);
4152         break;
4153 #endif
4154     case TARGET_NR_getpriority:
4155         /* libc does special remapping of the return value of
4156          * sys_getpriority() so it's just easiest to call
4157          * sys_getpriority() directly rather than through libc. */
4158         ret = sys_getpriority(arg1, arg2);
4159         break;
4160     case TARGET_NR_setpriority:
4161         ret = get_errno(setpriority(arg1, arg2, arg3));
4162         break;
4163 #ifdef TARGET_NR_profil
4164     case TARGET_NR_profil:
4165         goto unimplemented;
4166 #endif
4167     case TARGET_NR_statfs:
4168         if (!(p = lock_user_string(arg1)))
4169             goto efault;
4170         ret = get_errno(statfs(path(p), &stfs));
4171         unlock_user(p, arg1, 0);
4172     convert_statfs:
4173         if (!is_error(ret)) {
4174             struct target_statfs *target_stfs;
4175 
4176             if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
4177                 goto efault;
4178             __put_user(stfs.f_type, &target_stfs->f_type);
4179             __put_user(stfs.f_bsize, &target_stfs->f_bsize);
4180             __put_user(stfs.f_blocks, &target_stfs->f_blocks);
4181             __put_user(stfs.f_bfree, &target_stfs->f_bfree);
4182             __put_user(stfs.f_bavail, &target_stfs->f_bavail);
4183             __put_user(stfs.f_files, &target_stfs->f_files);
4184             __put_user(stfs.f_ffree, &target_stfs->f_ffree);
4185             __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
4186             __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
4187             __put_user(stfs.f_namelen, &target_stfs->f_namelen);
4188             unlock_user_struct(target_stfs, arg2, 1);
4189         }
4190         break;
4191     case TARGET_NR_fstatfs:
4192         ret = get_errno(fstatfs(arg1, &stfs));
4193         goto convert_statfs;
4194 #ifdef TARGET_NR_statfs64
4195     case TARGET_NR_statfs64:
4196         if (!(p = lock_user_string(arg1)))
4197             goto efault;
4198         ret = get_errno(statfs(path(p), &stfs));
4199         unlock_user(p, arg1, 0);
4200     convert_statfs64:
4201         if (!is_error(ret)) {
4202             struct target_statfs64 *target_stfs;
4203 
4204             if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
4205                 goto efault;
4206             __put_user(stfs.f_type, &target_stfs->f_type);
4207             __put_user(stfs.f_bsize, &target_stfs->f_bsize);
4208             __put_user(stfs.f_blocks, &target_stfs->f_blocks);
4209             __put_user(stfs.f_bfree, &target_stfs->f_bfree);
4210             __put_user(stfs.f_bavail, &target_stfs->f_bavail);
4211             __put_user(stfs.f_files, &target_stfs->f_files);
4212             __put_user(stfs.f_ffree, &target_stfs->f_ffree);
4213             __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
4214             __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
4215             __put_user(stfs.f_namelen, &target_stfs->f_namelen);
4216             unlock_user_struct(target_stfs, arg3, 1);
4217         }
4218         break;
4219     case TARGET_NR_fstatfs64:
4220         ret = get_errno(fstatfs(arg1, &stfs));
4221         goto convert_statfs64;
4222 #endif
4223 #ifdef TARGET_NR_ioperm
4224     case TARGET_NR_ioperm:
4225         goto unimplemented;
4226 #endif
4227 #ifdef TARGET_NR_socketcall
4228     case TARGET_NR_socketcall:
4229         ret = do_socketcall(arg1, arg2);
4230         break;
4231 #endif
4232 #ifdef TARGET_NR_accept
4233     case TARGET_NR_accept:
4234         ret = do_accept(arg1, arg2, arg3);
4235         break;
4236 #endif
4237 #ifdef TARGET_NR_bind
4238     case TARGET_NR_bind:
4239         ret = do_bind(arg1, arg2, arg3);
4240         break;
4241 #endif
4242 #ifdef TARGET_NR_connect
4243     case TARGET_NR_connect:
4244         ret = do_connect(arg1, arg2, arg3);
4245         break;
4246 #endif
4247 #ifdef TARGET_NR_getpeername
4248     case TARGET_NR_getpeername:
4249         ret = do_getpeername(arg1, arg2, arg3);
4250         break;
4251 #endif
4252 #ifdef TARGET_NR_getsockname
4253     case TARGET_NR_getsockname:
4254         ret = do_getsockname(arg1, arg2, arg3);
4255         break;
4256 #endif
4257 #ifdef TARGET_NR_getsockopt
4258     case TARGET_NR_getsockopt:
4259         ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
4260         break;
4261 #endif
4262 #ifdef TARGET_NR_listen
4263     case TARGET_NR_listen:
4264         ret = get_errno(listen(arg1, arg2));
4265         break;
4266 #endif
4267 #ifdef TARGET_NR_recv
4268     case TARGET_NR_recv:
4269         ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
4270         break;
4271 #endif
4272 #ifdef TARGET_NR_recvfrom
4273     case TARGET_NR_recvfrom:
4274         ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
4275         break;
4276 #endif
4277 #ifdef TARGET_NR_recvmsg
4278     case TARGET_NR_recvmsg:
4279         ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
4280         break;
4281 #endif
4282 #ifdef TARGET_NR_send
4283     case TARGET_NR_send:
4284         ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
4285         break;
4286 #endif
4287 #ifdef TARGET_NR_sendmsg
4288     case TARGET_NR_sendmsg:
4289         ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
4290         break;
4291 #endif
4292 #ifdef TARGET_NR_sendto
4293     case TARGET_NR_sendto:
4294         ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
4295         break;
4296 #endif
4297 #ifdef TARGET_NR_shutdown
4298     case TARGET_NR_shutdown:
4299         ret = get_errno(shutdown(arg1, arg2));
4300         break;
4301 #endif
4302 #ifdef TARGET_NR_socket
4303     case TARGET_NR_socket:
4304         ret = do_socket(arg1, arg2, arg3);
4305         break;
4306 #endif
4307 #ifdef TARGET_NR_socketpair
4308     case TARGET_NR_socketpair:
4309         ret = do_socketpair(arg1, arg2, arg3, arg4);
4310         break;
4311 #endif
4312 #ifdef TARGET_NR_setsockopt
4313     case TARGET_NR_setsockopt:
4314         ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
4315         break;
4316 #endif
4317 
4318     case TARGET_NR_syslog:
4319         if (!(p = lock_user_string(arg2)))
4320             goto efault;
4321         ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
4322         unlock_user(p, arg2, 0);
4323         break;
4324 
4325     case TARGET_NR_setitimer:
4326         {
4327             struct itimerval value, ovalue, *pvalue;
4328 
4329             if (arg2) {
4330                 pvalue = &value;
4331                 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
4332                     || copy_from_user_timeval(&pvalue->it_value,
4333                                               arg2 + sizeof(struct target_timeval)))
4334                     goto efault;
4335             } else {
4336                 pvalue = NULL;
4337             }
4338             ret = get_errno(setitimer(arg1, pvalue, &ovalue));
4339             if (!is_error(ret) && arg3) {
4340                 if (copy_to_user_timeval(arg3,
4341                                          &ovalue.it_interval)
4342                     || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
4343                                             &ovalue.it_value))
4344                     goto efault;
4345             }
4346         }
4347         break;
4348     case TARGET_NR_getitimer:
4349         {
4350             struct itimerval value;
4351 
4352             ret = get_errno(getitimer(arg1, &value));
4353             if (!is_error(ret) && arg2) {
4354                 if (copy_to_user_timeval(arg2,
4355                                          &value.it_interval)
4356                     || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
4357                                             &value.it_value))
4358                     goto efault;
4359             }
4360         }
4361         break;
4362     case TARGET_NR_stat:
4363         if (!(p = lock_user_string(arg1)))
4364             goto efault;
4365         ret = get_errno(stat(path(p), &st));
4366         unlock_user(p, arg1, 0);
4367         goto do_stat;
4368     case TARGET_NR_lstat:
4369         if (!(p = lock_user_string(arg1)))
4370             goto efault;
4371         ret = get_errno(lstat(path(p), &st));
4372         unlock_user(p, arg1, 0);
4373         goto do_stat;
4374     case TARGET_NR_fstat:
4375         {
4376             ret = get_errno(fstat(arg1, &st));
4377         do_stat:
4378             if (!is_error(ret)) {
4379                 struct target_stat *target_st;
4380 
4381                 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4382                     goto efault;
4383                 __put_user(st.st_dev, &target_st->st_dev);
4384                 __put_user(st.st_ino, &target_st->st_ino);
4385                 __put_user(st.st_mode, &target_st->st_mode);
4386                 __put_user(st.st_uid, &target_st->st_uid);
4387                 __put_user(st.st_gid, &target_st->st_gid);
4388                 __put_user(st.st_nlink, &target_st->st_nlink);
4389                 __put_user(st.st_rdev, &target_st->st_rdev);
4390                 __put_user(st.st_size, &target_st->st_size);
4391                 __put_user(st.st_blksize, &target_st->st_blksize);
4392                 __put_user(st.st_blocks, &target_st->st_blocks);
4393                 __put_user(st.st_atime, &target_st->target_st_atime);
4394                 __put_user(st.st_mtime, &target_st->target_st_mtime);
4395                 __put_user(st.st_ctime, &target_st->target_st_ctime);
4396                 unlock_user_struct(target_st, arg2, 1);
4397             }
4398         }
4399         break;
4400 #ifdef TARGET_NR_olduname
4401     case TARGET_NR_olduname:
4402         goto unimplemented;
4403 #endif
4404 #ifdef TARGET_NR_iopl
4405     case TARGET_NR_iopl:
4406         goto unimplemented;
4407 #endif
4408     case TARGET_NR_vhangup:
4409         ret = get_errno(vhangup());
4410         break;
4411 #ifdef TARGET_NR_idle
4412     case TARGET_NR_idle:
4413         goto unimplemented;
4414 #endif
4415 #ifdef TARGET_NR_syscall
4416     case TARGET_NR_syscall:
4417     	ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
4418     	break;
4419 #endif
4420     case TARGET_NR_wait4:
4421         {
4422             int status;
4423             abi_long status_ptr = arg2;
4424             struct rusage rusage, *rusage_ptr;
4425             abi_ulong target_rusage = arg4;
4426             if (target_rusage)
4427                 rusage_ptr = &rusage;
4428             else
4429                 rusage_ptr = NULL;
4430             ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
4431             if (!is_error(ret)) {
4432                 if (status_ptr) {
4433                     if (put_user_s32(status, status_ptr))
4434                         goto efault;
4435                 }
4436                 if (target_rusage)
4437                     host_to_target_rusage(target_rusage, &rusage);
4438             }
4439         }
4440         break;
4441 #ifdef TARGET_NR_swapoff
4442     case TARGET_NR_swapoff:
4443         if (!(p = lock_user_string(arg1)))
4444             goto efault;
4445         ret = get_errno(swapoff(p));
4446         unlock_user(p, arg1, 0);
4447         break;
4448 #endif
4449     case TARGET_NR_sysinfo:
4450         {
4451             struct target_sysinfo *target_value;
4452             struct sysinfo value;
4453             ret = get_errno(sysinfo(&value));
4454             if (!is_error(ret) && arg1)
4455             {
4456                 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
4457                     goto efault;
4458                 __put_user(value.uptime, &target_value->uptime);
4459                 __put_user(value.loads[0], &target_value->loads[0]);
4460                 __put_user(value.loads[1], &target_value->loads[1]);
4461                 __put_user(value.loads[2], &target_value->loads[2]);
4462                 __put_user(value.totalram, &target_value->totalram);
4463                 __put_user(value.freeram, &target_value->freeram);
4464                 __put_user(value.sharedram, &target_value->sharedram);
4465                 __put_user(value.bufferram, &target_value->bufferram);
4466                 __put_user(value.totalswap, &target_value->totalswap);
4467                 __put_user(value.freeswap, &target_value->freeswap);
4468                 __put_user(value.procs, &target_value->procs);
4469                 __put_user(value.totalhigh, &target_value->totalhigh);
4470                 __put_user(value.freehigh, &target_value->freehigh);
4471                 __put_user(value.mem_unit, &target_value->mem_unit);
4472                 unlock_user_struct(target_value, arg1, 1);
4473             }
4474         }
4475         break;
4476 #ifdef TARGET_NR_ipc
4477     case TARGET_NR_ipc:
4478 	ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
4479 	break;
4480 #endif
4481     case TARGET_NR_fsync:
4482         ret = get_errno(fsync(arg1));
4483         break;
4484     case TARGET_NR_clone:
4485         ret = get_errno(do_fork(cpu_env, arg1, arg2));
4486         break;
4487 #ifdef __NR_exit_group
4488         /* new thread calls */
4489     case TARGET_NR_exit_group:
4490         gdb_exit(cpu_env, arg1);
4491         ret = get_errno(exit_group(arg1));
4492         break;
4493 #endif
4494     case TARGET_NR_setdomainname:
4495         if (!(p = lock_user_string(arg1)))
4496             goto efault;
4497         ret = get_errno(setdomainname(p, arg2));
4498         unlock_user(p, arg1, 0);
4499         break;
4500     case TARGET_NR_uname:
4501         /* no need to transcode because we use the linux syscall */
4502         {
4503             struct new_utsname * buf;
4504 
4505             if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
4506                 goto efault;
4507             ret = get_errno(sys_uname(buf));
4508             if (!is_error(ret)) {
4509                 /* Overrite the native machine name with whatever is being
4510                    emulated. */
4511                 strcpy (buf->machine, UNAME_MACHINE);
4512                 /* Allow the user to override the reported release.  */
4513                 if (qemu_uname_release && *qemu_uname_release)
4514                   strcpy (buf->release, qemu_uname_release);
4515             }
4516             unlock_user_struct(buf, arg1, 1);
4517         }
4518         break;
4519 #ifdef TARGET_I386
4520     case TARGET_NR_modify_ldt:
4521         ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
4522         break;
4523 #if !defined(TARGET_X86_64)
4524     case TARGET_NR_vm86old:
4525         goto unimplemented;
4526     case TARGET_NR_vm86:
4527         ret = do_vm86(cpu_env, arg1, arg2);
4528         break;
4529 #endif
4530 #endif
4531     case TARGET_NR_adjtimex:
4532         goto unimplemented;
4533 #ifdef TARGET_NR_create_module
4534     case TARGET_NR_create_module:
4535 #endif
4536     case TARGET_NR_init_module:
4537     case TARGET_NR_delete_module:
4538 #ifdef TARGET_NR_get_kernel_syms
4539     case TARGET_NR_get_kernel_syms:
4540 #endif
4541         goto unimplemented;
4542     case TARGET_NR_quotactl:
4543         goto unimplemented;
4544     case TARGET_NR_getpgid:
4545         ret = get_errno(getpgid(arg1));
4546         break;
4547     case TARGET_NR_fchdir:
4548         ret = get_errno(fchdir(arg1));
4549         break;
4550 #ifdef TARGET_NR_bdflush /* not on x86_64 */
4551     case TARGET_NR_bdflush:
4552         goto unimplemented;
4553 #endif
4554 #ifdef TARGET_NR_sysfs
4555     case TARGET_NR_sysfs:
4556         goto unimplemented;
4557 #endif
4558     case TARGET_NR_personality:
4559         ret = get_errno(personality(arg1));
4560         break;
4561 #ifdef TARGET_NR_afs_syscall
4562     case TARGET_NR_afs_syscall:
4563         goto unimplemented;
4564 #endif
4565 #ifdef TARGET_NR__llseek /* Not on alpha */
4566     case TARGET_NR__llseek:
4567         {
4568 #if defined (__x86_64__)
4569             ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
4570             if (put_user_s64(ret, arg4))
4571                 goto efault;
4572 #else
4573             int64_t res;
4574             ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
4575             if (put_user_s64(res, arg4))
4576                 goto efault;
4577 #endif
4578         }
4579         break;
4580 #endif
4581     case TARGET_NR_getdents:
4582 #if TARGET_ABI_BITS != 32
4583         goto unimplemented;
4584 #elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
4585         {
4586             struct target_dirent *target_dirp;
4587             struct dirent *dirp;
4588             abi_long count = arg3;
4589 
4590 	    dirp = malloc(count);
4591 	    if (!dirp) {
4592                 ret = -TARGET_ENOMEM;
4593                 goto fail;
4594             }
4595 
4596             ret = get_errno(sys_getdents(arg1, dirp, count));
4597             if (!is_error(ret)) {
4598                 struct dirent *de;
4599 		struct target_dirent *tde;
4600                 int len = ret;
4601                 int reclen, treclen;
4602 		int count1, tnamelen;
4603 
4604 		count1 = 0;
4605                 de = dirp;
4606                 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4607                     goto efault;
4608 		tde = target_dirp;
4609                 while (len > 0) {
4610                     reclen = de->d_reclen;
4611 		    treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
4612                     tde->d_reclen = tswap16(treclen);
4613                     tde->d_ino = tswapl(de->d_ino);
4614                     tde->d_off = tswapl(de->d_off);
4615 		    tnamelen = treclen - (2 * sizeof(abi_long) + 2);
4616 		    if (tnamelen > 256)
4617                         tnamelen = 256;
4618                     /* XXX: may not be correct */
4619 		    strncpy(tde->d_name, de->d_name, tnamelen);
4620                     de = (struct dirent *)((char *)de + reclen);
4621                     len -= reclen;
4622                     tde = (struct target_dirent *)((char *)tde + treclen);
4623 		    count1 += treclen;
4624                 }
4625 		ret = count1;
4626                 unlock_user(target_dirp, arg2, ret);
4627             }
4628 	    free(dirp);
4629         }
4630 #else
4631         {
4632             struct dirent *dirp;
4633             abi_long count = arg3;
4634 
4635             if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4636                 goto efault;
4637             ret = get_errno(sys_getdents(arg1, dirp, count));
4638             if (!is_error(ret)) {
4639                 struct dirent *de;
4640                 int len = ret;
4641                 int reclen;
4642                 de = dirp;
4643                 while (len > 0) {
4644                     reclen = de->d_reclen;
4645                     if (reclen > len)
4646                         break;
4647                     de->d_reclen = tswap16(reclen);
4648                     tswapls(&de->d_ino);
4649                     tswapls(&de->d_off);
4650                     de = (struct dirent *)((char *)de + reclen);
4651                     len -= reclen;
4652                 }
4653             }
4654             unlock_user(dirp, arg2, ret);
4655         }
4656 #endif
4657         break;
4658 #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
4659     case TARGET_NR_getdents64:
4660         {
4661             struct dirent64 *dirp;
4662             abi_long count = arg3;
4663             if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4664                 goto efault;
4665             ret = get_errno(sys_getdents64(arg1, dirp, count));
4666             if (!is_error(ret)) {
4667                 struct dirent64 *de;
4668                 int len = ret;
4669                 int reclen;
4670                 de = dirp;
4671                 while (len > 0) {
4672                     reclen = de->d_reclen;
4673                     if (reclen > len)
4674                         break;
4675                     de->d_reclen = tswap16(reclen);
4676                     tswap64s((uint64_t *)&de->d_ino);
4677                     tswap64s((uint64_t *)&de->d_off);
4678                     de = (struct dirent64 *)((char *)de + reclen);
4679                     len -= reclen;
4680                 }
4681             }
4682             unlock_user(dirp, arg2, ret);
4683         }
4684         break;
4685 #endif /* TARGET_NR_getdents64 */
4686 #ifdef TARGET_NR__newselect
4687     case TARGET_NR__newselect:
4688         ret = do_select(arg1, arg2, arg3, arg4, arg5);
4689         break;
4690 #endif
4691 #ifdef TARGET_NR_poll
4692     case TARGET_NR_poll:
4693         {
4694             struct target_pollfd *target_pfd;
4695             unsigned int nfds = arg2;
4696             int timeout = arg3;
4697             struct pollfd *pfd;
4698             unsigned int i;
4699 
4700             target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
4701             if (!target_pfd)
4702                 goto efault;
4703             pfd = alloca(sizeof(struct pollfd) * nfds);
4704             for(i = 0; i < nfds; i++) {
4705                 pfd[i].fd = tswap32(target_pfd[i].fd);
4706                 pfd[i].events = tswap16(target_pfd[i].events);
4707             }
4708             ret = get_errno(poll(pfd, nfds, timeout));
4709             if (!is_error(ret)) {
4710                 for(i = 0; i < nfds; i++) {
4711                     target_pfd[i].revents = tswap16(pfd[i].revents);
4712                 }
4713                 ret += nfds * (sizeof(struct target_pollfd)
4714                                - sizeof(struct pollfd));
4715             }
4716             unlock_user(target_pfd, arg1, ret);
4717         }
4718         break;
4719 #endif
4720     case TARGET_NR_flock:
4721         /* NOTE: the flock constant seems to be the same for every
4722            Linux platform */
4723         ret = get_errno(flock(arg1, arg2));
4724         break;
4725     case TARGET_NR_readv:
4726         {
4727             int count = arg3;
4728             struct iovec *vec;
4729 
4730             vec = alloca(count * sizeof(struct iovec));
4731             if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
4732                 goto efault;
4733             ret = get_errno(readv(arg1, vec, count));
4734             unlock_iovec(vec, arg2, count, 1);
4735         }
4736         break;
4737     case TARGET_NR_writev:
4738         {
4739             int count = arg3;
4740             struct iovec *vec;
4741 
4742             vec = alloca(count * sizeof(struct iovec));
4743             if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
4744                 goto efault;
4745             ret = get_errno(writev(arg1, vec, count));
4746             unlock_iovec(vec, arg2, count, 0);
4747         }
4748         break;
4749     case TARGET_NR_getsid:
4750         ret = get_errno(getsid(arg1));
4751         break;
4752 #if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
4753     case TARGET_NR_fdatasync:
4754         ret = get_errno(fdatasync(arg1));
4755         break;
4756 #endif
4757     case TARGET_NR__sysctl:
4758         /* We don't implement this, but ENOTDIR is always a safe
4759            return value. */
4760         ret = -TARGET_ENOTDIR;
4761         break;
4762     case TARGET_NR_sched_setparam:
4763         {
4764             struct sched_param *target_schp;
4765             struct sched_param schp;
4766 
4767             if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
4768                 goto efault;
4769             schp.sched_priority = tswap32(target_schp->sched_priority);
4770             unlock_user_struct(target_schp, arg2, 0);
4771             ret = get_errno(sched_setparam(arg1, &schp));
4772         }
4773         break;
4774     case TARGET_NR_sched_getparam:
4775         {
4776             struct sched_param *target_schp;
4777             struct sched_param schp;
4778             ret = get_errno(sched_getparam(arg1, &schp));
4779             if (!is_error(ret)) {
4780                 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
4781                     goto efault;
4782                 target_schp->sched_priority = tswap32(schp.sched_priority);
4783                 unlock_user_struct(target_schp, arg2, 1);
4784             }
4785         }
4786         break;
4787     case TARGET_NR_sched_setscheduler:
4788         {
4789             struct sched_param *target_schp;
4790             struct sched_param schp;
4791             if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
4792                 goto efault;
4793             schp.sched_priority = tswap32(target_schp->sched_priority);
4794             unlock_user_struct(target_schp, arg3, 0);
4795             ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
4796         }
4797         break;
4798     case TARGET_NR_sched_getscheduler:
4799         ret = get_errno(sched_getscheduler(arg1));
4800         break;
4801     case TARGET_NR_sched_yield:
4802         ret = get_errno(sched_yield());
4803         break;
4804     case TARGET_NR_sched_get_priority_max:
4805         ret = get_errno(sched_get_priority_max(arg1));
4806         break;
4807     case TARGET_NR_sched_get_priority_min:
4808         ret = get_errno(sched_get_priority_min(arg1));
4809         break;
4810     case TARGET_NR_sched_rr_get_interval:
4811         {
4812             struct timespec ts;
4813             ret = get_errno(sched_rr_get_interval(arg1, &ts));
4814             if (!is_error(ret)) {
4815                 host_to_target_timespec(arg2, &ts);
4816             }
4817         }
4818         break;
4819     case TARGET_NR_nanosleep:
4820         {
4821             struct timespec req, rem;
4822             target_to_host_timespec(&req, arg1);
4823             ret = get_errno(nanosleep(&req, &rem));
4824             if (is_error(ret) && arg2) {
4825                 host_to_target_timespec(arg2, &rem);
4826             }
4827         }
4828         break;
4829 #ifdef TARGET_NR_query_module
4830     case TARGET_NR_query_module:
4831         goto unimplemented;
4832 #endif
4833 #ifdef TARGET_NR_nfsservctl
4834     case TARGET_NR_nfsservctl:
4835         goto unimplemented;
4836 #endif
4837     case TARGET_NR_prctl:
4838         switch (arg1)
4839             {
4840             case PR_GET_PDEATHSIG:
4841                 {
4842                     int deathsig;
4843                     ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
4844                     if (!is_error(ret) && arg2
4845                         && put_user_ual(deathsig, arg2))
4846                         goto efault;
4847                 }
4848                 break;
4849             default:
4850                 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
4851                 break;
4852             }
4853         break;
4854 #ifdef TARGET_NR_arch_prctl
4855     case TARGET_NR_arch_prctl:
4856 #if defined(TARGET_I386) && !defined(TARGET_ABI32)
4857         ret = do_arch_prctl(cpu_env, arg1, arg2);
4858         break;
4859 #else
4860         goto unimplemented;
4861 #endif
4862 #endif
4863 #ifdef TARGET_NR_pread
4864     case TARGET_NR_pread:
4865         if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4866             goto efault;
4867         ret = get_errno(pread(arg1, p, arg3, arg4));
4868         unlock_user(p, arg2, ret);
4869         break;
4870     case TARGET_NR_pwrite:
4871         if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4872             goto efault;
4873         ret = get_errno(pwrite(arg1, p, arg3, arg4));
4874         unlock_user(p, arg2, 0);
4875         break;
4876 #endif
4877 #ifdef TARGET_NR_pread64
4878     case TARGET_NR_pread64:
4879         if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4880             goto efault;
4881         ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
4882         unlock_user(p, arg2, ret);
4883         break;
4884     case TARGET_NR_pwrite64:
4885         if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4886             goto efault;
4887         ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
4888         unlock_user(p, arg2, 0);
4889         break;
4890 #endif
4891     case TARGET_NR_getcwd:
4892         if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
4893             goto efault;
4894         ret = get_errno(sys_getcwd1(p, arg2));
4895         unlock_user(p, arg1, ret);
4896         break;
4897     case TARGET_NR_capget:
4898         goto unimplemented;
4899     case TARGET_NR_capset:
4900         goto unimplemented;
4901     case TARGET_NR_sigaltstack:
4902 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
4903     defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
4904         ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
4905         break;
4906 #else
4907         goto unimplemented;
4908 #endif
4909     case TARGET_NR_sendfile:
4910         goto unimplemented;
4911 #ifdef TARGET_NR_getpmsg
4912     case TARGET_NR_getpmsg:
4913         goto unimplemented;
4914 #endif
4915 #ifdef TARGET_NR_putpmsg
4916     case TARGET_NR_putpmsg:
4917         goto unimplemented;
4918 #endif
4919 #ifdef TARGET_NR_vfork
4920     case TARGET_NR_vfork:
4921         ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
4922         break;
4923 #endif
4924 #ifdef TARGET_NR_ugetrlimit
4925     case TARGET_NR_ugetrlimit:
4926     {
4927 	struct rlimit rlim;
4928 	ret = get_errno(getrlimit(arg1, &rlim));
4929 	if (!is_error(ret)) {
4930 	    struct target_rlimit *target_rlim;
4931             if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
4932                 goto efault;
4933 	    target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
4934 	    target_rlim->rlim_max = tswapl(rlim.rlim_max);
4935             unlock_user_struct(target_rlim, arg2, 1);
4936 	}
4937 	break;
4938     }
4939 #endif
4940 #ifdef TARGET_NR_truncate64
4941     case TARGET_NR_truncate64:
4942         if (!(p = lock_user_string(arg1)))
4943             goto efault;
4944 	ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
4945         unlock_user(p, arg1, 0);
4946 	break;
4947 #endif
4948 #ifdef TARGET_NR_ftruncate64
4949     case TARGET_NR_ftruncate64:
4950 	ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
4951 	break;
4952 #endif
4953 #ifdef TARGET_NR_stat64
4954     case TARGET_NR_stat64:
4955         if (!(p = lock_user_string(arg1)))
4956             goto efault;
4957         ret = get_errno(stat(path(p), &st));
4958         unlock_user(p, arg1, 0);
4959         goto do_stat64;
4960 #endif
4961 #ifdef TARGET_NR_lstat64
4962     case TARGET_NR_lstat64:
4963         if (!(p = lock_user_string(arg1)))
4964             goto efault;
4965         ret = get_errno(lstat(path(p), &st));
4966         unlock_user(p, arg1, 0);
4967         goto do_stat64;
4968 #endif
4969 #ifdef TARGET_NR_fstat64
4970     case TARGET_NR_fstat64:
4971         {
4972             ret = get_errno(fstat(arg1, &st));
4973         do_stat64:
4974             if (!is_error(ret)) {
4975 #ifdef TARGET_ARM
4976                 if (((CPUARMState *)cpu_env)->eabi) {
4977                     struct target_eabi_stat64 *target_st;
4978 
4979                     if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4980                         goto efault;
4981                     memset(target_st, 0, sizeof(struct target_eabi_stat64));
4982                     __put_user(st.st_dev, &target_st->st_dev);
4983                     __put_user(st.st_ino, &target_st->st_ino);
4984 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4985                     __put_user(st.st_ino, &target_st->__st_ino);
4986 #endif
4987                     __put_user(st.st_mode, &target_st->st_mode);
4988                     __put_user(st.st_nlink, &target_st->st_nlink);
4989                     __put_user(st.st_uid, &target_st->st_uid);
4990                     __put_user(st.st_gid, &target_st->st_gid);
4991                     __put_user(st.st_rdev, &target_st->st_rdev);
4992                     __put_user(st.st_size, &target_st->st_size);
4993                     __put_user(st.st_blksize, &target_st->st_blksize);
4994                     __put_user(st.st_blocks, &target_st->st_blocks);
4995                     __put_user(st.st_atime, &target_st->target_st_atime);
4996                     __put_user(st.st_mtime, &target_st->target_st_mtime);
4997                     __put_user(st.st_ctime, &target_st->target_st_ctime);
4998                     unlock_user_struct(target_st, arg2, 1);
4999                 } else
5000 #endif
5001                 {
5002                     struct target_stat64 *target_st;
5003 
5004                     if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
5005                         goto efault;
5006                     memset(target_st, 0, sizeof(struct target_stat64));
5007                     __put_user(st.st_dev, &target_st->st_dev);
5008                     __put_user(st.st_ino, &target_st->st_ino);
5009 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
5010                     __put_user(st.st_ino, &target_st->__st_ino);
5011 #endif
5012                     __put_user(st.st_mode, &target_st->st_mode);
5013                     __put_user(st.st_nlink, &target_st->st_nlink);
5014                     __put_user(st.st_uid, &target_st->st_uid);
5015                     __put_user(st.st_gid, &target_st->st_gid);
5016                     __put_user(st.st_rdev, &target_st->st_rdev);
5017                     /* XXX: better use of kernel struct */
5018                     __put_user(st.st_size, &target_st->st_size);
5019                     __put_user(st.st_blksize, &target_st->st_blksize);
5020                     __put_user(st.st_blocks, &target_st->st_blocks);
5021                     __put_user(st.st_atime, &target_st->target_st_atime);
5022                     __put_user(st.st_mtime, &target_st->target_st_mtime);
5023                     __put_user(st.st_ctime, &target_st->target_st_ctime);
5024                     unlock_user_struct(target_st, arg2, 1);
5025                 }
5026             }
5027         }
5028         break;
5029 #endif
5030 #ifdef USE_UID16
5031     case TARGET_NR_lchown:
5032         if (!(p = lock_user_string(arg1)))
5033             goto efault;
5034         ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
5035         unlock_user(p, arg1, 0);
5036         break;
5037     case TARGET_NR_getuid:
5038         ret = get_errno(high2lowuid(getuid()));
5039         break;
5040     case TARGET_NR_getgid:
5041         ret = get_errno(high2lowgid(getgid()));
5042         break;
5043     case TARGET_NR_geteuid:
5044         ret = get_errno(high2lowuid(geteuid()));
5045         break;
5046     case TARGET_NR_getegid:
5047         ret = get_errno(high2lowgid(getegid()));
5048         break;
5049     case TARGET_NR_setreuid:
5050         ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
5051         break;
5052     case TARGET_NR_setregid:
5053         ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
5054         break;
5055     case TARGET_NR_getgroups:
5056         {
5057             int gidsetsize = arg1;
5058             uint16_t *target_grouplist;
5059             gid_t *grouplist;
5060             int i;
5061 
5062             grouplist = alloca(gidsetsize * sizeof(gid_t));
5063             ret = get_errno(getgroups(gidsetsize, grouplist));
5064             if (!is_error(ret)) {
5065                 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
5066                 if (!target_grouplist)
5067                     goto efault;
5068                 for(i = 0;i < gidsetsize; i++)
5069                     target_grouplist[i] = tswap16(grouplist[i]);
5070                 unlock_user(target_grouplist, arg2, gidsetsize * 2);
5071             }
5072         }
5073         break;
5074     case TARGET_NR_setgroups:
5075         {
5076             int gidsetsize = arg1;
5077             uint16_t *target_grouplist;
5078             gid_t *grouplist;
5079             int i;
5080 
5081             grouplist = alloca(gidsetsize * sizeof(gid_t));
5082             target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
5083             if (!target_grouplist) {
5084                 ret = -TARGET_EFAULT;
5085                 goto fail;
5086             }
5087             for(i = 0;i < gidsetsize; i++)
5088                 grouplist[i] = tswap16(target_grouplist[i]);
5089             unlock_user(target_grouplist, arg2, 0);
5090             ret = get_errno(setgroups(gidsetsize, grouplist));
5091         }
5092         break;
5093     case TARGET_NR_fchown:
5094         ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
5095         break;
5096 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
5097     case TARGET_NR_fchownat:
5098         if (!(p = lock_user_string(arg2)))
5099             goto efault;
5100         ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
5101         unlock_user(p, arg2, 0);
5102         break;
5103 #endif
5104 #ifdef TARGET_NR_setresuid
5105     case TARGET_NR_setresuid:
5106         ret = get_errno(setresuid(low2highuid(arg1),
5107                                   low2highuid(arg2),
5108                                   low2highuid(arg3)));
5109         break;
5110 #endif
5111 #ifdef TARGET_NR_getresuid
5112     case TARGET_NR_getresuid:
5113         {
5114             uid_t ruid, euid, suid;
5115             ret = get_errno(getresuid(&ruid, &euid, &suid));
5116             if (!is_error(ret)) {
5117                 if (put_user_u16(high2lowuid(ruid), arg1)
5118                     || put_user_u16(high2lowuid(euid), arg2)
5119                     || put_user_u16(high2lowuid(suid), arg3))
5120                     goto efault;
5121             }
5122         }
5123         break;
5124 #endif
5125 #ifdef TARGET_NR_getresgid
5126     case TARGET_NR_setresgid:
5127         ret = get_errno(setresgid(low2highgid(arg1),
5128                                   low2highgid(arg2),
5129                                   low2highgid(arg3)));
5130         break;
5131 #endif
5132 #ifdef TARGET_NR_getresgid
5133     case TARGET_NR_getresgid:
5134         {
5135             gid_t rgid, egid, sgid;
5136             ret = get_errno(getresgid(&rgid, &egid, &sgid));
5137             if (!is_error(ret)) {
5138                 if (put_user_u16(high2lowgid(rgid), arg1)
5139                     || put_user_u16(high2lowgid(egid), arg2)
5140                     || put_user_u16(high2lowgid(sgid), arg3))
5141                     goto efault;
5142             }
5143         }
5144         break;
5145 #endif
5146     case TARGET_NR_chown:
5147         if (!(p = lock_user_string(arg1)))
5148             goto efault;
5149         ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
5150         unlock_user(p, arg1, 0);
5151         break;
5152     case TARGET_NR_setuid:
5153         ret = get_errno(setuid(low2highuid(arg1)));
5154         break;
5155     case TARGET_NR_setgid:
5156         ret = get_errno(setgid(low2highgid(arg1)));
5157         break;
5158     case TARGET_NR_setfsuid:
5159         ret = get_errno(setfsuid(arg1));
5160         break;
5161     case TARGET_NR_setfsgid:
5162         ret = get_errno(setfsgid(arg1));
5163         break;
5164 #endif /* USE_UID16 */
5165 
5166 #ifdef TARGET_NR_lchown32
5167     case TARGET_NR_lchown32:
5168         if (!(p = lock_user_string(arg1)))
5169             goto efault;
5170         ret = get_errno(lchown(p, arg2, arg3));
5171         unlock_user(p, arg1, 0);
5172         break;
5173 #endif
5174 #ifdef TARGET_NR_getuid32
5175     case TARGET_NR_getuid32:
5176         ret = get_errno(getuid());
5177         break;
5178 #endif
5179 #ifdef TARGET_NR_getgid32
5180     case TARGET_NR_getgid32:
5181         ret = get_errno(getgid());
5182         break;
5183 #endif
5184 #ifdef TARGET_NR_geteuid32
5185     case TARGET_NR_geteuid32:
5186         ret = get_errno(geteuid());
5187         break;
5188 #endif
5189 #ifdef TARGET_NR_getegid32
5190     case TARGET_NR_getegid32:
5191         ret = get_errno(getegid());
5192         break;
5193 #endif
5194 #ifdef TARGET_NR_setreuid32
5195     case TARGET_NR_setreuid32:
5196         ret = get_errno(setreuid(arg1, arg2));
5197         break;
5198 #endif
5199 #ifdef TARGET_NR_setregid32
5200     case TARGET_NR_setregid32:
5201         ret = get_errno(setregid(arg1, arg2));
5202         break;
5203 #endif
5204 #ifdef TARGET_NR_getgroups32
5205     case TARGET_NR_getgroups32:
5206         {
5207             int gidsetsize = arg1;
5208             uint32_t *target_grouplist;
5209             gid_t *grouplist;
5210             int i;
5211 
5212             grouplist = alloca(gidsetsize * sizeof(gid_t));
5213             ret = get_errno(getgroups(gidsetsize, grouplist));
5214             if (!is_error(ret)) {
5215                 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
5216                 if (!target_grouplist) {
5217                     ret = -TARGET_EFAULT;
5218                     goto fail;
5219                 }
5220                 for(i = 0;i < gidsetsize; i++)
5221                     target_grouplist[i] = tswap32(grouplist[i]);
5222                 unlock_user(target_grouplist, arg2, gidsetsize * 4);
5223             }
5224         }
5225         break;
5226 #endif
5227 #ifdef TARGET_NR_setgroups32
5228     case TARGET_NR_setgroups32:
5229         {
5230             int gidsetsize = arg1;
5231             uint32_t *target_grouplist;
5232             gid_t *grouplist;
5233             int i;
5234 
5235             grouplist = alloca(gidsetsize * sizeof(gid_t));
5236             target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
5237             if (!target_grouplist) {
5238                 ret = -TARGET_EFAULT;
5239                 goto fail;
5240             }
5241             for(i = 0;i < gidsetsize; i++)
5242                 grouplist[i] = tswap32(target_grouplist[i]);
5243             unlock_user(target_grouplist, arg2, 0);
5244             ret = get_errno(setgroups(gidsetsize, grouplist));
5245         }
5246         break;
5247 #endif
5248 #ifdef TARGET_NR_fchown32
5249     case TARGET_NR_fchown32:
5250         ret = get_errno(fchown(arg1, arg2, arg3));
5251         break;
5252 #endif
5253 #ifdef TARGET_NR_setresuid32
5254     case TARGET_NR_setresuid32:
5255         ret = get_errno(setresuid(arg1, arg2, arg3));
5256         break;
5257 #endif
5258 #ifdef TARGET_NR_getresuid32
5259     case TARGET_NR_getresuid32:
5260         {
5261             uid_t ruid, euid, suid;
5262             ret = get_errno(getresuid(&ruid, &euid, &suid));
5263             if (!is_error(ret)) {
5264                 if (put_user_u32(ruid, arg1)
5265                     || put_user_u32(euid, arg2)
5266                     || put_user_u32(suid, arg3))
5267                     goto efault;
5268             }
5269         }
5270         break;
5271 #endif
5272 #ifdef TARGET_NR_setresgid32
5273     case TARGET_NR_setresgid32:
5274         ret = get_errno(setresgid(arg1, arg2, arg3));
5275         break;
5276 #endif
5277 #ifdef TARGET_NR_getresgid32
5278     case TARGET_NR_getresgid32:
5279         {
5280             gid_t rgid, egid, sgid;
5281             ret = get_errno(getresgid(&rgid, &egid, &sgid));
5282             if (!is_error(ret)) {
5283                 if (put_user_u32(rgid, arg1)
5284                     || put_user_u32(egid, arg2)
5285                     || put_user_u32(sgid, arg3))
5286                     goto efault;
5287             }
5288         }
5289         break;
5290 #endif
5291 #ifdef TARGET_NR_chown32
5292     case TARGET_NR_chown32:
5293         if (!(p = lock_user_string(arg1)))
5294             goto efault;
5295         ret = get_errno(chown(p, arg2, arg3));
5296         unlock_user(p, arg1, 0);
5297         break;
5298 #endif
5299 #ifdef TARGET_NR_setuid32
5300     case TARGET_NR_setuid32:
5301         ret = get_errno(setuid(arg1));
5302         break;
5303 #endif
5304 #ifdef TARGET_NR_setgid32
5305     case TARGET_NR_setgid32:
5306         ret = get_errno(setgid(arg1));
5307         break;
5308 #endif
5309 #ifdef TARGET_NR_setfsuid32
5310     case TARGET_NR_setfsuid32:
5311         ret = get_errno(setfsuid(arg1));
5312         break;
5313 #endif
5314 #ifdef TARGET_NR_setfsgid32
5315     case TARGET_NR_setfsgid32:
5316         ret = get_errno(setfsgid(arg1));
5317         break;
5318 #endif
5319 
5320     case TARGET_NR_pivot_root:
5321         goto unimplemented;
5322 #ifdef TARGET_NR_mincore
5323     case TARGET_NR_mincore:
5324         goto unimplemented;
5325 #endif
5326 #ifdef TARGET_NR_madvise
5327     case TARGET_NR_madvise:
5328         /* A straight passthrough may not be safe because qemu sometimes
5329            turns private flie-backed mappings into anonymous mappings.
5330            This will break MADV_DONTNEED.
5331            This is a hint, so ignoring and returning success is ok.  */
5332         ret = get_errno(0);
5333         break;
5334 #endif
5335 #if TARGET_ABI_BITS == 32
5336     case TARGET_NR_fcntl64:
5337     {
5338 	int cmd;
5339 	struct flock64 fl;
5340 	struct target_flock64 *target_fl;
5341 #ifdef TARGET_ARM
5342 	struct target_eabi_flock64 *target_efl;
5343 #endif
5344 
5345         switch(arg2){
5346         case TARGET_F_GETLK64:
5347             cmd = F_GETLK64;
5348             break;
5349         case TARGET_F_SETLK64:
5350             cmd = F_SETLK64;
5351             break;
5352         case TARGET_F_SETLKW64:
5353             cmd = F_SETLK64;
5354             break;
5355         default:
5356             cmd = arg2;
5357             break;
5358         }
5359 
5360         switch(arg2) {
5361         case TARGET_F_GETLK64:
5362 #ifdef TARGET_ARM
5363             if (((CPUARMState *)cpu_env)->eabi) {
5364                 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5365                     goto efault;
5366                 fl.l_type = tswap16(target_efl->l_type);
5367                 fl.l_whence = tswap16(target_efl->l_whence);
5368                 fl.l_start = tswap64(target_efl->l_start);
5369                 fl.l_len = tswap64(target_efl->l_len);
5370                 fl.l_pid = tswapl(target_efl->l_pid);
5371                 unlock_user_struct(target_efl, arg3, 0);
5372             } else
5373 #endif
5374             {
5375                 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5376                     goto efault;
5377                 fl.l_type = tswap16(target_fl->l_type);
5378                 fl.l_whence = tswap16(target_fl->l_whence);
5379                 fl.l_start = tswap64(target_fl->l_start);
5380                 fl.l_len = tswap64(target_fl->l_len);
5381                 fl.l_pid = tswapl(target_fl->l_pid);
5382                 unlock_user_struct(target_fl, arg3, 0);
5383             }
5384             ret = get_errno(fcntl(arg1, cmd, &fl));
5385 	    if (ret == 0) {
5386 #ifdef TARGET_ARM
5387                 if (((CPUARMState *)cpu_env)->eabi) {
5388                     if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
5389                         goto efault;
5390                     target_efl->l_type = tswap16(fl.l_type);
5391                     target_efl->l_whence = tswap16(fl.l_whence);
5392                     target_efl->l_start = tswap64(fl.l_start);
5393                     target_efl->l_len = tswap64(fl.l_len);
5394                     target_efl->l_pid = tswapl(fl.l_pid);
5395                     unlock_user_struct(target_efl, arg3, 1);
5396                 } else
5397 #endif
5398                 {
5399                     if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
5400                         goto efault;
5401                     target_fl->l_type = tswap16(fl.l_type);
5402                     target_fl->l_whence = tswap16(fl.l_whence);
5403                     target_fl->l_start = tswap64(fl.l_start);
5404                     target_fl->l_len = tswap64(fl.l_len);
5405                     target_fl->l_pid = tswapl(fl.l_pid);
5406                     unlock_user_struct(target_fl, arg3, 1);
5407                 }
5408 	    }
5409 	    break;
5410 
5411         case TARGET_F_SETLK64:
5412         case TARGET_F_SETLKW64:
5413 #ifdef TARGET_ARM
5414             if (((CPUARMState *)cpu_env)->eabi) {
5415                 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5416                     goto efault;
5417                 fl.l_type = tswap16(target_efl->l_type);
5418                 fl.l_whence = tswap16(target_efl->l_whence);
5419                 fl.l_start = tswap64(target_efl->l_start);
5420                 fl.l_len = tswap64(target_efl->l_len);
5421                 fl.l_pid = tswapl(target_efl->l_pid);
5422                 unlock_user_struct(target_efl, arg3, 0);
5423             } else
5424 #endif
5425             {
5426                 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5427                     goto efault;
5428                 fl.l_type = tswap16(target_fl->l_type);
5429                 fl.l_whence = tswap16(target_fl->l_whence);
5430                 fl.l_start = tswap64(target_fl->l_start);
5431                 fl.l_len = tswap64(target_fl->l_len);
5432                 fl.l_pid = tswapl(target_fl->l_pid);
5433                 unlock_user_struct(target_fl, arg3, 0);
5434             }
5435             ret = get_errno(fcntl(arg1, cmd, &fl));
5436 	    break;
5437         default:
5438             ret = do_fcntl(arg1, cmd, arg3);
5439             break;
5440         }
5441 	break;
5442     }
5443 #endif
5444 #ifdef TARGET_NR_cacheflush
5445     case TARGET_NR_cacheflush:
5446         /* self-modifying code is handled automatically, so nothing needed */
5447         ret = 0;
5448         break;
5449 #endif
5450 #ifdef TARGET_NR_security
5451     case TARGET_NR_security:
5452         goto unimplemented;
5453 #endif
5454 #ifdef TARGET_NR_getpagesize
5455     case TARGET_NR_getpagesize:
5456         ret = TARGET_PAGE_SIZE;
5457         break;
5458 #endif
5459     case TARGET_NR_gettid:
5460         ret = get_errno(gettid());
5461         break;
5462 #ifdef TARGET_NR_readahead
5463     case TARGET_NR_readahead:
5464         goto unimplemented;
5465 #endif
5466 #ifdef TARGET_NR_setxattr
5467     case TARGET_NR_setxattr:
5468     case TARGET_NR_lsetxattr:
5469     case TARGET_NR_fsetxattr:
5470     case TARGET_NR_getxattr:
5471     case TARGET_NR_lgetxattr:
5472     case TARGET_NR_fgetxattr:
5473     case TARGET_NR_listxattr:
5474     case TARGET_NR_llistxattr:
5475     case TARGET_NR_flistxattr:
5476     case TARGET_NR_removexattr:
5477     case TARGET_NR_lremovexattr:
5478     case TARGET_NR_fremovexattr:
5479         goto unimplemented_nowarn;
5480 #endif
5481 #ifdef TARGET_NR_set_thread_area
5482     case TARGET_NR_set_thread_area:
5483 #if defined(TARGET_MIPS)
5484       ((CPUMIPSState *) cpu_env)->tls_value = arg1;
5485       ret = 0;
5486       break;
5487 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
5488       ret = do_set_thread_area(cpu_env, arg1);
5489       break;
5490 #else
5491       goto unimplemented_nowarn;
5492 #endif
5493 #endif
5494 #ifdef TARGET_NR_get_thread_area
5495     case TARGET_NR_get_thread_area:
5496 #if defined(TARGET_I386) && defined(TARGET_ABI32)
5497         ret = do_get_thread_area(cpu_env, arg1);
5498 #else
5499         goto unimplemented_nowarn;
5500 #endif
5501 #endif
5502 #ifdef TARGET_NR_getdomainname
5503     case TARGET_NR_getdomainname:
5504         goto unimplemented_nowarn;
5505 #endif
5506 
5507 #ifdef TARGET_NR_clock_gettime
5508     case TARGET_NR_clock_gettime:
5509     {
5510         struct timespec ts;
5511         ret = get_errno(clock_gettime(arg1, &ts));
5512         if (!is_error(ret)) {
5513             host_to_target_timespec(arg2, &ts);
5514         }
5515         break;
5516     }
5517 #endif
5518 #ifdef TARGET_NR_clock_getres
5519     case TARGET_NR_clock_getres:
5520     {
5521         struct timespec ts;
5522         ret = get_errno(clock_getres(arg1, &ts));
5523         if (!is_error(ret)) {
5524             host_to_target_timespec(arg2, &ts);
5525         }
5526         break;
5527     }
5528 #endif
5529 
5530 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
5531     case TARGET_NR_set_tid_address:
5532         ret = get_errno(set_tid_address((int *)g2h(arg1)));
5533         break;
5534 #endif
5535 
5536 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
5537     case TARGET_NR_tkill:
5538         ret = get_errno(sys_tkill((int)arg1, (int)arg2));
5539         break;
5540 #endif
5541 
5542 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
5543     case TARGET_NR_tgkill:
5544 	ret = get_errno(sys_tgkill((int)arg1, (int)arg2, (int)arg3));
5545 	break;
5546 #endif
5547 
5548 #ifdef TARGET_NR_set_robust_list
5549     case TARGET_NR_set_robust_list:
5550 	goto unimplemented_nowarn;
5551 #endif
5552 
5553 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
5554     case TARGET_NR_utimensat:
5555         {
5556             struct timespec ts[2];
5557             target_to_host_timespec(ts, arg3);
5558             target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
5559             if (!arg2)
5560                 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
5561             else {
5562                 if (!(p = lock_user_string(arg2))) {
5563                     ret = -TARGET_EFAULT;
5564                     goto fail;
5565                 }
5566                 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
5567                 unlock_user(p, arg2, 0);
5568             }
5569         }
5570 	break;
5571 #endif
5572 
5573     default:
5574     unimplemented:
5575         gemu_log("qemu: Unsupported syscall: %d\n", num);
5576 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
5577     unimplemented_nowarn:
5578 #endif
5579         ret = -TARGET_ENOSYS;
5580         break;
5581     }
5582 fail:
5583 #ifdef DEBUG
5584     gemu_log(" = %ld\n", ret);
5585 #endif
5586     if(do_strace)
5587         print_syscall_ret(num, ret);
5588     return ret;
5589 efault:
5590     ret = -TARGET_EFAULT;
5591     goto fail;
5592 }
5593