Don't error out on zero-length chunks in writev, as to mimic Linux (Kirill A. Shutemov).
[qemu] / linux-user / syscall.c
1 /*
2  *  Linux syscalls
3  *
4  *  Copyright (c) 2003 Fabrice Bellard
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, 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_ENOMEM;
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     if (is_error(mapped_addr)) {
439         return mapped_addr;
440     } else {
441         target_brk = new_brk;
442         return target_brk;
443     }
444 }
445
446 static inline abi_long copy_from_user_fdset(fd_set *fds,
447                                             abi_ulong target_fds_addr,
448                                             int n)
449 {
450     int i, nw, j, k;
451     abi_ulong b, *target_fds;
452
453     nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
454     if (!(target_fds = lock_user(VERIFY_READ,
455                                  target_fds_addr,
456                                  sizeof(abi_ulong) * nw,
457                                  1)))
458         return -TARGET_EFAULT;
459
460     FD_ZERO(fds);
461     k = 0;
462     for (i = 0; i < nw; i++) {
463         /* grab the abi_ulong */
464         __get_user(b, &target_fds[i]);
465         for (j = 0; j < TARGET_ABI_BITS; j++) {
466             /* check the bit inside the abi_ulong */
467             if ((b >> j) & 1)
468                 FD_SET(k, fds);
469             k++;
470         }
471     }
472
473     unlock_user(target_fds, target_fds_addr, 0);
474
475     return 0;
476 }
477
478 static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
479                                           const fd_set *fds,
480                                           int n)
481 {
482     int i, nw, j, k;
483     abi_long v;
484     abi_ulong *target_fds;
485
486     nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
487     if (!(target_fds = lock_user(VERIFY_WRITE,
488                                  target_fds_addr,
489                                  sizeof(abi_ulong) * nw,
490                                  0)))
491         return -TARGET_EFAULT;
492
493     k = 0;
494     for (i = 0; i < nw; i++) {
495         v = 0;
496         for (j = 0; j < TARGET_ABI_BITS; j++) {
497             v |= ((FD_ISSET(k, fds) != 0) << j);
498             k++;
499         }
500         __put_user(v, &target_fds[i]);
501     }
502
503     unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
504
505     return 0;
506 }
507
508 #if defined(__alpha__)
509 #define HOST_HZ 1024
510 #else
511 #define HOST_HZ 100
512 #endif
513
514 static inline abi_long host_to_target_clock_t(long ticks)
515 {
516 #if HOST_HZ == TARGET_HZ
517     return ticks;
518 #else
519     return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
520 #endif
521 }
522
523 static inline abi_long host_to_target_rusage(abi_ulong target_addr,
524                                              const struct rusage *rusage)
525 {
526     struct target_rusage *target_rusage;
527
528     if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
529         return -TARGET_EFAULT;
530     target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
531     target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
532     target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
533     target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
534     target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
535     target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
536     target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
537     target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
538     target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
539     target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
540     target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
541     target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
542     target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
543     target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
544     target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
545     target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
546     target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
547     target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
548     unlock_user_struct(target_rusage, target_addr, 1);
549
550     return 0;
551 }
552
553 static inline abi_long copy_from_user_timeval(struct timeval *tv,
554                                               abi_ulong target_tv_addr)
555 {
556     struct target_timeval *target_tv;
557
558     if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
559         return -TARGET_EFAULT;
560
561     __get_user(tv->tv_sec, &target_tv->tv_sec);
562     __get_user(tv->tv_usec, &target_tv->tv_usec);
563
564     unlock_user_struct(target_tv, target_tv_addr, 0);
565
566     return 0;
567 }
568
569 static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
570                                             const struct timeval *tv)
571 {
572     struct target_timeval *target_tv;
573
574     if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
575         return -TARGET_EFAULT;
576
577     __put_user(tv->tv_sec, &target_tv->tv_sec);
578     __put_user(tv->tv_usec, &target_tv->tv_usec);
579
580     unlock_user_struct(target_tv, target_tv_addr, 1);
581
582     return 0;
583 }
584
585
586 /* do_select() must return target values and target errnos. */
587 static abi_long do_select(int n,
588                           abi_ulong rfd_addr, abi_ulong wfd_addr,
589                           abi_ulong efd_addr, abi_ulong target_tv_addr)
590 {
591     fd_set rfds, wfds, efds;
592     fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
593     struct timeval tv, *tv_ptr;
594     abi_long ret;
595
596     if (rfd_addr) {
597         if (copy_from_user_fdset(&rfds, rfd_addr, n))
598             return -TARGET_EFAULT;
599         rfds_ptr = &rfds;
600     } else {
601         rfds_ptr = NULL;
602     }
603     if (wfd_addr) {
604         if (copy_from_user_fdset(&wfds, wfd_addr, n))
605             return -TARGET_EFAULT;
606         wfds_ptr = &wfds;
607     } else {
608         wfds_ptr = NULL;
609     }
610     if (efd_addr) {
611         if (copy_from_user_fdset(&efds, efd_addr, n))
612             return -TARGET_EFAULT;
613         efds_ptr = &efds;
614     } else {
615         efds_ptr = NULL;
616     }
617
618     if (target_tv_addr) {
619         if (copy_from_user_timeval(&tv, target_tv_addr))
620             return -TARGET_EFAULT;
621         tv_ptr = &tv;
622     } else {
623         tv_ptr = NULL;
624     }
625
626     ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
627
628     if (!is_error(ret)) {
629         if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
630             return -TARGET_EFAULT;
631         if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
632             return -TARGET_EFAULT;
633         if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
634             return -TARGET_EFAULT;
635
636         if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
637             return -TARGET_EFAULT;
638     }
639
640     return ret;
641 }
642
643 static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
644                                                abi_ulong target_addr,
645                                                socklen_t len)
646 {
647     struct target_sockaddr *target_saddr;
648
649     target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
650     if (!target_saddr)
651         return -TARGET_EFAULT;
652     memcpy(addr, target_saddr, len);
653     addr->sa_family = tswap16(target_saddr->sa_family);
654     unlock_user(target_saddr, target_addr, 0);
655
656     return 0;
657 }
658
659 static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
660                                                struct sockaddr *addr,
661                                                socklen_t len)
662 {
663     struct target_sockaddr *target_saddr;
664
665     target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
666     if (!target_saddr)
667         return -TARGET_EFAULT;
668     memcpy(target_saddr, addr, len);
669     target_saddr->sa_family = tswap16(addr->sa_family);
670     unlock_user(target_saddr, target_addr, len);
671
672     return 0;
673 }
674
675 /* ??? Should this also swap msgh->name?  */
676 static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
677                                            struct target_msghdr *target_msgh)
678 {
679     struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
680     abi_long msg_controllen;
681     abi_ulong target_cmsg_addr;
682     struct target_cmsghdr *target_cmsg;
683     socklen_t space = 0;
684     
685     msg_controllen = tswapl(target_msgh->msg_controllen);
686     if (msg_controllen < sizeof (struct target_cmsghdr)) 
687         goto the_end;
688     target_cmsg_addr = tswapl(target_msgh->msg_control);
689     target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
690     if (!target_cmsg)
691         return -TARGET_EFAULT;
692
693     while (cmsg && target_cmsg) {
694         void *data = CMSG_DATA(cmsg);
695         void *target_data = TARGET_CMSG_DATA(target_cmsg);
696
697         int len = tswapl(target_cmsg->cmsg_len)
698                   - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
699
700         space += CMSG_SPACE(len);
701         if (space > msgh->msg_controllen) {
702             space -= CMSG_SPACE(len);
703             gemu_log("Host cmsg overflow\n");
704             break;
705         }
706
707         cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
708         cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
709         cmsg->cmsg_len = CMSG_LEN(len);
710
711         if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
712             gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
713             memcpy(data, target_data, len);
714         } else {
715             int *fd = (int *)data;
716             int *target_fd = (int *)target_data;
717             int i, numfds = len / sizeof(int);
718
719             for (i = 0; i < numfds; i++)
720                 fd[i] = tswap32(target_fd[i]);
721         }
722
723         cmsg = CMSG_NXTHDR(msgh, cmsg);
724         target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
725     }
726     unlock_user(target_cmsg, target_cmsg_addr, 0);
727  the_end:
728     msgh->msg_controllen = space;
729     return 0;
730 }
731
732 /* ??? Should this also swap msgh->name?  */
733 static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
734                                            struct msghdr *msgh)
735 {
736     struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
737     abi_long msg_controllen;
738     abi_ulong target_cmsg_addr;
739     struct target_cmsghdr *target_cmsg;
740     socklen_t space = 0;
741
742     msg_controllen = tswapl(target_msgh->msg_controllen);
743     if (msg_controllen < sizeof (struct target_cmsghdr)) 
744         goto the_end;
745     target_cmsg_addr = tswapl(target_msgh->msg_control);
746     target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
747     if (!target_cmsg)
748         return -TARGET_EFAULT;
749
750     while (cmsg && target_cmsg) {
751         void *data = CMSG_DATA(cmsg);
752         void *target_data = TARGET_CMSG_DATA(target_cmsg);
753
754         int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
755
756         space += TARGET_CMSG_SPACE(len);
757         if (space > msg_controllen) {
758             space -= TARGET_CMSG_SPACE(len);
759             gemu_log("Target cmsg overflow\n");
760             break;
761         }
762
763         target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
764         target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
765         target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
766
767         if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
768             gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
769             memcpy(target_data, data, len);
770         } else {
771             int *fd = (int *)data;
772             int *target_fd = (int *)target_data;
773             int i, numfds = len / sizeof(int);
774
775             for (i = 0; i < numfds; i++)
776                 target_fd[i] = tswap32(fd[i]);
777         }
778
779         cmsg = CMSG_NXTHDR(msgh, cmsg);
780         target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
781     }
782     unlock_user(target_cmsg, target_cmsg_addr, space);
783  the_end:
784     target_msgh->msg_controllen = tswapl(space);
785     return 0;
786 }
787
788 /* do_setsockopt() Must return target values and target errnos. */
789 static abi_long do_setsockopt(int sockfd, int level, int optname,
790                               abi_ulong optval_addr, socklen_t optlen)
791 {
792     abi_long ret;
793     int val;
794
795     switch(level) {
796     case SOL_TCP:
797         /* TCP options all take an 'int' value.  */
798         if (optlen < sizeof(uint32_t))
799             return -TARGET_EINVAL;
800
801         if (get_user_u32(val, optval_addr))
802             return -TARGET_EFAULT;
803         ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
804         break;
805     case SOL_IP:
806         switch(optname) {
807         case IP_TOS:
808         case IP_TTL:
809         case IP_HDRINCL:
810         case IP_ROUTER_ALERT:
811         case IP_RECVOPTS:
812         case IP_RETOPTS:
813         case IP_PKTINFO:
814         case IP_MTU_DISCOVER:
815         case IP_RECVERR:
816         case IP_RECVTOS:
817 #ifdef IP_FREEBIND
818         case IP_FREEBIND:
819 #endif
820         case IP_MULTICAST_TTL:
821         case IP_MULTICAST_LOOP:
822             val = 0;
823             if (optlen >= sizeof(uint32_t)) {
824                 if (get_user_u32(val, optval_addr))
825                     return -TARGET_EFAULT;
826             } else if (optlen >= 1) {
827                 if (get_user_u8(val, optval_addr))
828                     return -TARGET_EFAULT;
829             }
830             ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
831             break;
832         default:
833             goto unimplemented;
834         }
835         break;
836     case TARGET_SOL_SOCKET:
837         switch (optname) {
838             /* Options with 'int' argument.  */
839         case TARGET_SO_DEBUG:
840                 optname = SO_DEBUG;
841                 break;
842         case TARGET_SO_REUSEADDR:
843                 optname = SO_REUSEADDR;
844                 break;
845         case TARGET_SO_TYPE:
846                 optname = SO_TYPE;
847                 break;
848         case TARGET_SO_ERROR:
849                 optname = SO_ERROR;
850                 break;
851         case TARGET_SO_DONTROUTE:
852                 optname = SO_DONTROUTE;
853                 break;
854         case TARGET_SO_BROADCAST:
855                 optname = SO_BROADCAST;
856                 break;
857         case TARGET_SO_SNDBUF:
858                 optname = SO_SNDBUF;
859                 break;
860         case TARGET_SO_RCVBUF:
861                 optname = SO_RCVBUF;
862                 break;
863         case TARGET_SO_KEEPALIVE:
864                 optname = SO_KEEPALIVE;
865                 break;
866         case TARGET_SO_OOBINLINE:
867                 optname = SO_OOBINLINE;
868                 break;
869         case TARGET_SO_NO_CHECK:
870                 optname = SO_NO_CHECK;
871                 break;
872         case TARGET_SO_PRIORITY:
873                 optname = SO_PRIORITY;
874                 break;
875 #ifdef SO_BSDCOMPAT
876         case TARGET_SO_BSDCOMPAT:
877                 optname = SO_BSDCOMPAT;
878                 break;
879 #endif
880         case TARGET_SO_PASSCRED:
881                 optname = SO_PASSCRED;
882                 break;
883         case TARGET_SO_TIMESTAMP:
884                 optname = SO_TIMESTAMP;
885                 break;
886         case TARGET_SO_RCVLOWAT:
887                 optname = SO_RCVLOWAT;
888                 break;
889         case TARGET_SO_RCVTIMEO:
890                 optname = SO_RCVTIMEO;
891                 break;
892         case TARGET_SO_SNDTIMEO:
893                 optname = SO_SNDTIMEO;
894                 break;
895             break;
896         default:
897             goto unimplemented;
898         }
899         if (optlen < sizeof(uint32_t))
900             return -TARGET_EINVAL;
901
902         if (get_user_u32(val, optval_addr))
903             return -TARGET_EFAULT;
904         ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
905         break;
906     default:
907     unimplemented:
908         gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
909         ret = -TARGET_ENOPROTOOPT;
910     }
911     return ret;
912 }
913
914 /* do_getsockopt() Must return target values and target errnos. */
915 static abi_long do_getsockopt(int sockfd, int level, int optname,
916                               abi_ulong optval_addr, abi_ulong optlen)
917 {
918     abi_long ret;
919     int len, lv, val;
920
921     switch(level) {
922     case TARGET_SOL_SOCKET:
923         level = SOL_SOCKET;
924         switch (optname) {
925         case TARGET_SO_LINGER:
926         case TARGET_SO_RCVTIMEO:
927         case TARGET_SO_SNDTIMEO:
928         case TARGET_SO_PEERCRED:
929         case TARGET_SO_PEERNAME:
930             /* These don't just return a single integer */
931             goto unimplemented;
932         default:
933             goto int_case;
934         }
935         break;
936     case SOL_TCP:
937         /* TCP options all take an 'int' value.  */
938     int_case:
939         if (get_user_u32(len, optlen))
940             return -TARGET_EFAULT;
941         if (len < 0)
942             return -TARGET_EINVAL;
943         lv = sizeof(int);
944         ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
945         if (ret < 0)
946             return ret;
947         val = tswap32(val);
948         if (len > lv)
949             len = lv;
950         if (len == 4) {
951             if (put_user_u32(val, optval_addr))
952                 return -TARGET_EFAULT;
953         } else {
954             if (put_user_u8(val, optval_addr))
955                 return -TARGET_EFAULT;
956         }
957         if (put_user_u32(len, optlen))
958             return -TARGET_EFAULT;
959         break;
960     case SOL_IP:
961         switch(optname) {
962         case IP_TOS:
963         case IP_TTL:
964         case IP_HDRINCL:
965         case IP_ROUTER_ALERT:
966         case IP_RECVOPTS:
967         case IP_RETOPTS:
968         case IP_PKTINFO:
969         case IP_MTU_DISCOVER:
970         case IP_RECVERR:
971         case IP_RECVTOS:
972 #ifdef IP_FREEBIND
973         case IP_FREEBIND:
974 #endif
975         case IP_MULTICAST_TTL:
976         case IP_MULTICAST_LOOP:
977             if (get_user_u32(len, optlen))
978                 return -TARGET_EFAULT;
979             if (len < 0)
980                 return -TARGET_EINVAL;
981             lv = sizeof(int);
982             ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
983             if (ret < 0)
984                 return ret;
985             if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
986                 len = 1;
987                 if (put_user_u32(len, optlen)
988                     || put_user_u8(val, optval_addr))
989                     return -TARGET_EFAULT;
990             } else {
991                 if (len > sizeof(int))
992                     len = sizeof(int);
993                 if (put_user_u32(len, optlen)
994                     || put_user_u32(val, optval_addr))
995                     return -TARGET_EFAULT;
996             }
997             break;
998         default:
999             ret = -TARGET_ENOPROTOOPT;
1000             break;
1001         }
1002         break;
1003     default:
1004     unimplemented:
1005         gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1006                  level, optname);
1007         ret = -TARGET_EOPNOTSUPP;
1008         break;
1009     }
1010     return ret;
1011 }
1012
1013 /* FIXME
1014  * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1015  * other lock functions have a return code of 0 for failure.
1016  */
1017 static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1018                            int count, int copy)
1019 {
1020     struct target_iovec *target_vec;
1021     abi_ulong base;
1022     int i, j;
1023
1024     target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1025     if (!target_vec)
1026         return -TARGET_EFAULT;
1027     for(i = 0;i < count; i++) {
1028         base = tswapl(target_vec[i].iov_base);
1029         vec[i].iov_len = tswapl(target_vec[i].iov_len);
1030         if (vec[i].iov_len != 0) {
1031             vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
1032             if (!vec[i].iov_base && vec[i].iov_len) 
1033                 goto fail;
1034         } else {
1035             /* zero length pointer is ignored */
1036             vec[i].iov_base = NULL;
1037         }
1038     }
1039     unlock_user (target_vec, target_addr, 0);
1040     return 0;
1041  fail:
1042     /* failure - unwind locks */
1043     for (j = 0; j < i; j++) {
1044         base = tswapl(target_vec[j].iov_base);
1045         unlock_user(vec[j].iov_base, base, 0);
1046     }
1047     unlock_user (target_vec, target_addr, 0);
1048     return -TARGET_EFAULT;
1049 }
1050
1051 static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1052                              int count, int copy)
1053 {
1054     struct target_iovec *target_vec;
1055     abi_ulong base;
1056     int i;
1057
1058     target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1059     if (!target_vec)
1060         return -TARGET_EFAULT;
1061     for(i = 0;i < count; i++) {
1062         base = tswapl(target_vec[i].iov_base);
1063         unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1064     }
1065     unlock_user (target_vec, target_addr, 0);
1066
1067     return 0;
1068 }
1069
1070 /* do_socket() Must return target values and target errnos. */
1071 static abi_long do_socket(int domain, int type, int protocol)
1072 {
1073 #if defined(TARGET_MIPS)
1074     switch(type) {
1075     case TARGET_SOCK_DGRAM:
1076         type = SOCK_DGRAM;
1077         break;
1078     case TARGET_SOCK_STREAM:
1079         type = SOCK_STREAM;
1080         break;
1081     case TARGET_SOCK_RAW:
1082         type = SOCK_RAW;
1083         break;
1084     case TARGET_SOCK_RDM:
1085         type = SOCK_RDM;
1086         break;
1087     case TARGET_SOCK_SEQPACKET:
1088         type = SOCK_SEQPACKET;
1089         break;
1090     case TARGET_SOCK_PACKET:
1091         type = SOCK_PACKET;
1092         break;
1093     }
1094 #endif
1095     if (domain == PF_NETLINK)
1096         return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
1097     return get_errno(socket(domain, type, protocol));
1098 }
1099
1100 /* do_bind() Must return target values and target errnos. */
1101 static abi_long do_bind(int sockfd, abi_ulong target_addr,
1102                         socklen_t addrlen)
1103 {
1104     void *addr = alloca(addrlen);
1105
1106     target_to_host_sockaddr(addr, target_addr, addrlen);
1107     return get_errno(bind(sockfd, addr, addrlen));
1108 }
1109
1110 /* do_connect() Must return target values and target errnos. */
1111 static abi_long do_connect(int sockfd, abi_ulong target_addr,
1112                            socklen_t addrlen)
1113 {
1114     void *addr = alloca(addrlen);
1115
1116     target_to_host_sockaddr(addr, target_addr, addrlen);
1117     return get_errno(connect(sockfd, addr, addrlen));
1118 }
1119
1120 /* do_sendrecvmsg() Must return target values and target errnos. */
1121 static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1122                                int flags, int send)
1123 {
1124     abi_long ret;
1125     struct target_msghdr *msgp;
1126     struct msghdr msg;
1127     int count;
1128     struct iovec *vec;
1129     abi_ulong target_vec;
1130
1131     /* FIXME */
1132     if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1133                           msgp,
1134                           target_msg,
1135                           send ? 1 : 0))
1136         return -TARGET_EFAULT;
1137     if (msgp->msg_name) {
1138         msg.msg_namelen = tswap32(msgp->msg_namelen);
1139         msg.msg_name = alloca(msg.msg_namelen);
1140         target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1141                                 msg.msg_namelen);
1142     } else {
1143         msg.msg_name = NULL;
1144         msg.msg_namelen = 0;
1145     }
1146     msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1147     msg.msg_control = alloca(msg.msg_controllen);
1148     msg.msg_flags = tswap32(msgp->msg_flags);
1149
1150     count = tswapl(msgp->msg_iovlen);
1151     vec = alloca(count * sizeof(struct iovec));
1152     target_vec = tswapl(msgp->msg_iov);
1153     lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
1154     msg.msg_iovlen = count;
1155     msg.msg_iov = vec;
1156
1157     if (send) {
1158         ret = target_to_host_cmsg(&msg, msgp);
1159         if (ret == 0)
1160             ret = get_errno(sendmsg(fd, &msg, flags));
1161     } else {
1162         ret = get_errno(recvmsg(fd, &msg, flags));
1163         if (!is_error(ret))
1164             ret = host_to_target_cmsg(msgp, &msg);
1165     }
1166     unlock_iovec(vec, target_vec, count, !send);
1167     unlock_user_struct(msgp, target_msg, send ? 0 : 1);
1168     return ret;
1169 }
1170
1171 /* do_accept() Must return target values and target errnos. */
1172 static abi_long do_accept(int fd, abi_ulong target_addr,
1173                           abi_ulong target_addrlen_addr)
1174 {
1175     socklen_t addrlen;
1176     void *addr;
1177     abi_long ret;
1178
1179     if (get_user_u32(addrlen, target_addrlen_addr))
1180         return -TARGET_EFAULT;
1181
1182     addr = alloca(addrlen);
1183
1184     ret = get_errno(accept(fd, addr, &addrlen));
1185     if (!is_error(ret)) {
1186         host_to_target_sockaddr(target_addr, addr, addrlen);
1187         if (put_user_u32(addrlen, target_addrlen_addr))
1188             ret = -TARGET_EFAULT;
1189     }
1190     return ret;
1191 }
1192
1193 /* do_getpeername() Must return target values and target errnos. */
1194 static abi_long do_getpeername(int fd, abi_ulong target_addr,
1195                                abi_ulong target_addrlen_addr)
1196 {
1197     socklen_t addrlen;
1198     void *addr;
1199     abi_long ret;
1200
1201     if (get_user_u32(addrlen, target_addrlen_addr))
1202         return -TARGET_EFAULT;
1203
1204     addr = alloca(addrlen);
1205
1206     ret = get_errno(getpeername(fd, addr, &addrlen));
1207     if (!is_error(ret)) {
1208         host_to_target_sockaddr(target_addr, addr, addrlen);
1209         if (put_user_u32(addrlen, target_addrlen_addr))
1210             ret = -TARGET_EFAULT;
1211     }
1212     return ret;
1213 }
1214
1215 /* do_getsockname() Must return target values and target errnos. */
1216 static abi_long do_getsockname(int fd, abi_ulong target_addr,
1217                                abi_ulong target_addrlen_addr)
1218 {
1219     socklen_t addrlen;
1220     void *addr;
1221     abi_long ret;
1222
1223     if (get_user_u32(addrlen, target_addrlen_addr))
1224         return -TARGET_EFAULT;
1225
1226     addr = alloca(addrlen);
1227
1228     ret = get_errno(getsockname(fd, addr, &addrlen));
1229     if (!is_error(ret)) {
1230         host_to_target_sockaddr(target_addr, addr, addrlen);
1231         if (put_user_u32(addrlen, target_addrlen_addr))
1232             ret = -TARGET_EFAULT;
1233     }
1234     return ret;
1235 }
1236
1237 /* do_socketpair() Must return target values and target errnos. */
1238 static abi_long do_socketpair(int domain, int type, int protocol,
1239                               abi_ulong target_tab_addr)
1240 {
1241     int tab[2];
1242     abi_long ret;
1243
1244     ret = get_errno(socketpair(domain, type, protocol, tab));
1245     if (!is_error(ret)) {
1246         if (put_user_s32(tab[0], target_tab_addr)
1247             || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1248             ret = -TARGET_EFAULT;
1249     }
1250     return ret;
1251 }
1252
1253 /* do_sendto() Must return target values and target errnos. */
1254 static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1255                           abi_ulong target_addr, socklen_t addrlen)
1256 {
1257     void *addr;
1258     void *host_msg;
1259     abi_long ret;
1260
1261     host_msg = lock_user(VERIFY_READ, msg, len, 1);
1262     if (!host_msg)
1263         return -TARGET_EFAULT;
1264     if (target_addr) {
1265         addr = alloca(addrlen);
1266         target_to_host_sockaddr(addr, target_addr, addrlen);
1267         ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1268     } else {
1269         ret = get_errno(send(fd, host_msg, len, flags));
1270     }
1271     unlock_user(host_msg, msg, 0);
1272     return ret;
1273 }
1274
1275 /* do_recvfrom() Must return target values and target errnos. */
1276 static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1277                             abi_ulong target_addr,
1278                             abi_ulong target_addrlen)
1279 {
1280     socklen_t addrlen;
1281     void *addr;
1282     void *host_msg;
1283     abi_long ret;
1284
1285     host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1286     if (!host_msg)
1287         return -TARGET_EFAULT;
1288     if (target_addr) {
1289         if (get_user_u32(addrlen, target_addrlen)) {
1290             ret = -TARGET_EFAULT;
1291             goto fail;
1292         }
1293         addr = alloca(addrlen);
1294         ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1295     } else {
1296         addr = NULL; /* To keep compiler quiet.  */
1297         ret = get_errno(recv(fd, host_msg, len, flags));
1298     }
1299     if (!is_error(ret)) {
1300         if (target_addr) {
1301             host_to_target_sockaddr(target_addr, addr, addrlen);
1302             if (put_user_u32(addrlen, target_addrlen)) {
1303                 ret = -TARGET_EFAULT;
1304                 goto fail;
1305             }
1306         }
1307         unlock_user(host_msg, msg, len);
1308     } else {
1309 fail:
1310         unlock_user(host_msg, msg, 0);
1311     }
1312     return ret;
1313 }
1314
1315 #ifdef TARGET_NR_socketcall
1316 /* do_socketcall() Must return target values and target errnos. */
1317 static abi_long do_socketcall(int num, abi_ulong vptr)
1318 {
1319     abi_long ret;
1320     const int n = sizeof(abi_ulong);
1321
1322     switch(num) {
1323     case SOCKOP_socket:
1324         {
1325             int domain, type, protocol;
1326
1327             if (get_user_s32(domain, vptr)
1328                 || get_user_s32(type, vptr + n)
1329                 || get_user_s32(protocol, vptr + 2 * n))
1330                 return -TARGET_EFAULT;
1331
1332             ret = do_socket(domain, type, protocol);
1333         }
1334         break;
1335     case SOCKOP_bind:
1336         {
1337             int sockfd;
1338             abi_ulong target_addr;
1339             socklen_t addrlen;
1340
1341             if (get_user_s32(sockfd, vptr)
1342                 || get_user_ual(target_addr, vptr + n)
1343                 || get_user_u32(addrlen, vptr + 2 * n))
1344                 return -TARGET_EFAULT;
1345
1346             ret = do_bind(sockfd, target_addr, addrlen);
1347         }
1348         break;
1349     case SOCKOP_connect:
1350         {
1351             int sockfd;
1352             abi_ulong target_addr;
1353             socklen_t addrlen;
1354
1355             if (get_user_s32(sockfd, vptr)
1356                 || get_user_ual(target_addr, vptr + n)
1357                 || get_user_u32(addrlen, vptr + 2 * n))
1358                 return -TARGET_EFAULT;
1359
1360             ret = do_connect(sockfd, target_addr, addrlen);
1361         }
1362         break;
1363     case SOCKOP_listen:
1364         {
1365             int sockfd, backlog;
1366
1367             if (get_user_s32(sockfd, vptr)
1368                 || get_user_s32(backlog, vptr + n))
1369                 return -TARGET_EFAULT;
1370
1371             ret = get_errno(listen(sockfd, backlog));
1372         }
1373         break;
1374     case SOCKOP_accept:
1375         {
1376             int sockfd;
1377             abi_ulong target_addr, target_addrlen;
1378
1379             if (get_user_s32(sockfd, vptr)
1380                 || get_user_ual(target_addr, vptr + n)
1381                 || get_user_u32(target_addrlen, vptr + 2 * n))
1382                 return -TARGET_EFAULT;
1383
1384             ret = do_accept(sockfd, target_addr, target_addrlen);
1385         }
1386         break;
1387     case SOCKOP_getsockname:
1388         {
1389             int sockfd;
1390             abi_ulong target_addr, target_addrlen;
1391
1392             if (get_user_s32(sockfd, vptr)
1393                 || get_user_ual(target_addr, vptr + n)
1394                 || get_user_u32(target_addrlen, vptr + 2 * n))
1395                 return -TARGET_EFAULT;
1396
1397             ret = do_getsockname(sockfd, target_addr, target_addrlen);
1398         }
1399         break;
1400     case SOCKOP_getpeername:
1401         {
1402             int sockfd;
1403             abi_ulong target_addr, target_addrlen;
1404
1405             if (get_user_s32(sockfd, vptr)
1406                 || get_user_ual(target_addr, vptr + n)
1407                 || get_user_u32(target_addrlen, vptr + 2 * n))
1408                 return -TARGET_EFAULT;
1409
1410             ret = do_getpeername(sockfd, target_addr, target_addrlen);
1411         }
1412         break;
1413     case SOCKOP_socketpair:
1414         {
1415             int domain, type, protocol;
1416             abi_ulong tab;
1417
1418             if (get_user_s32(domain, vptr)
1419                 || get_user_s32(type, vptr + n)
1420                 || get_user_s32(protocol, vptr + 2 * n)
1421                 || get_user_ual(tab, vptr + 3 * n))
1422                 return -TARGET_EFAULT;
1423
1424             ret = do_socketpair(domain, type, protocol, tab);
1425         }
1426         break;
1427     case SOCKOP_send:
1428         {
1429             int sockfd;
1430             abi_ulong msg;
1431             size_t len;
1432             int flags;
1433
1434             if (get_user_s32(sockfd, vptr)
1435                 || get_user_ual(msg, vptr + n)
1436                 || get_user_ual(len, vptr + 2 * n)
1437                 || get_user_s32(flags, vptr + 3 * n))
1438                 return -TARGET_EFAULT;
1439
1440             ret = do_sendto(sockfd, msg, len, flags, 0, 0);
1441         }
1442         break;
1443     case SOCKOP_recv:
1444         {
1445             int sockfd;
1446             abi_ulong msg;
1447             size_t len;
1448             int flags;
1449
1450             if (get_user_s32(sockfd, vptr)
1451                 || get_user_ual(msg, vptr + n)
1452                 || get_user_ual(len, vptr + 2 * n)
1453                 || get_user_s32(flags, vptr + 3 * n))
1454                 return -TARGET_EFAULT;
1455
1456             ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
1457         }
1458         break;
1459     case SOCKOP_sendto:
1460         {
1461             int sockfd;
1462             abi_ulong msg;
1463             size_t len;
1464             int flags;
1465             abi_ulong addr;
1466             socklen_t addrlen;
1467
1468             if (get_user_s32(sockfd, vptr)
1469                 || get_user_ual(msg, vptr + n)
1470                 || get_user_ual(len, vptr + 2 * n)
1471                 || get_user_s32(flags, vptr + 3 * n)
1472                 || get_user_ual(addr, vptr + 4 * n)
1473                 || get_user_u32(addrlen, vptr + 5 * n))
1474                 return -TARGET_EFAULT;
1475
1476             ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
1477         }
1478         break;
1479     case SOCKOP_recvfrom:
1480         {
1481             int sockfd;
1482             abi_ulong msg;
1483             size_t len;
1484             int flags;
1485             abi_ulong addr;
1486             socklen_t addrlen;
1487
1488             if (get_user_s32(sockfd, vptr)
1489                 || get_user_ual(msg, vptr + n)
1490                 || get_user_ual(len, vptr + 2 * n)
1491                 || get_user_s32(flags, vptr + 3 * n)
1492                 || get_user_ual(addr, vptr + 4 * n)
1493                 || get_user_u32(addrlen, vptr + 5 * n))
1494                 return -TARGET_EFAULT;
1495
1496             ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
1497         }
1498         break;
1499     case SOCKOP_shutdown:
1500         {
1501             int sockfd, how;
1502
1503             if (get_user_s32(sockfd, vptr)
1504                 || get_user_s32(how, vptr + n))
1505                 return -TARGET_EFAULT;
1506
1507             ret = get_errno(shutdown(sockfd, how));
1508         }
1509         break;
1510     case SOCKOP_sendmsg:
1511     case SOCKOP_recvmsg:
1512         {
1513             int fd;
1514             abi_ulong target_msg;
1515             int flags;
1516
1517             if (get_user_s32(fd, vptr)
1518                 || get_user_ual(target_msg, vptr + n)
1519                 || get_user_s32(flags, vptr + 2 * n))
1520                 return -TARGET_EFAULT;
1521
1522             ret = do_sendrecvmsg(fd, target_msg, flags,
1523                                  (num == SOCKOP_sendmsg));
1524         }
1525         break;
1526     case SOCKOP_setsockopt:
1527         {
1528             int sockfd;
1529             int level;
1530             int optname;
1531             abi_ulong optval;
1532             socklen_t optlen;
1533
1534             if (get_user_s32(sockfd, vptr)
1535                 || get_user_s32(level, vptr + n)
1536                 || get_user_s32(optname, vptr + 2 * n)
1537                 || get_user_ual(optval, vptr + 3 * n)
1538                 || get_user_u32(optlen, vptr + 4 * n))
1539                 return -TARGET_EFAULT;
1540
1541             ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1542         }
1543         break;
1544     case SOCKOP_getsockopt:
1545         {
1546             int sockfd;
1547             int level;
1548             int optname;
1549             abi_ulong optval;
1550             socklen_t optlen;
1551
1552             if (get_user_s32(sockfd, vptr)
1553                 || get_user_s32(level, vptr + n)
1554                 || get_user_s32(optname, vptr + 2 * n)
1555                 || get_user_ual(optval, vptr + 3 * n)
1556                 || get_user_u32(optlen, vptr + 4 * n))
1557                 return -TARGET_EFAULT;
1558
1559             ret = do_getsockopt(sockfd, level, optname, optval, optlen);
1560         }
1561         break;
1562     default:
1563         gemu_log("Unsupported socketcall: %d\n", num);
1564         ret = -TARGET_ENOSYS;
1565         break;
1566     }
1567     return ret;
1568 }
1569 #endif
1570
1571 #ifdef TARGET_NR_ipc
1572 #define N_SHM_REGIONS   32
1573
1574 static struct shm_region {
1575     abi_ulong   start;
1576     abi_ulong   size;
1577 } shm_regions[N_SHM_REGIONS];
1578
1579 struct target_ipc_perm
1580 {
1581     abi_long __key;
1582     abi_ulong uid;
1583     abi_ulong gid;
1584     abi_ulong cuid;
1585     abi_ulong cgid;
1586     unsigned short int mode;
1587     unsigned short int __pad1;
1588     unsigned short int __seq;
1589     unsigned short int __pad2;
1590     abi_ulong __unused1;
1591     abi_ulong __unused2;
1592 };
1593
1594 struct target_semid_ds
1595 {
1596   struct target_ipc_perm sem_perm;
1597   abi_ulong sem_otime;
1598   abi_ulong __unused1;
1599   abi_ulong sem_ctime;
1600   abi_ulong __unused2;
1601   abi_ulong sem_nsems;
1602   abi_ulong __unused3;
1603   abi_ulong __unused4;
1604 };
1605
1606 static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
1607                                                abi_ulong target_addr)
1608 {
1609     struct target_ipc_perm *target_ip;
1610     struct target_semid_ds *target_sd;
1611
1612     if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1613         return -TARGET_EFAULT;
1614     target_ip=&(target_sd->sem_perm);
1615     host_ip->__key = tswapl(target_ip->__key);
1616     host_ip->uid = tswapl(target_ip->uid);
1617     host_ip->gid = tswapl(target_ip->gid);
1618     host_ip->cuid = tswapl(target_ip->cuid);
1619     host_ip->cgid = tswapl(target_ip->cgid);
1620     host_ip->mode = tswapl(target_ip->mode);
1621     unlock_user_struct(target_sd, target_addr, 0);
1622     return 0;
1623 }
1624
1625 static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
1626                                                struct ipc_perm *host_ip)
1627 {
1628     struct target_ipc_perm *target_ip;
1629     struct target_semid_ds *target_sd;
1630
1631     if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1632         return -TARGET_EFAULT;
1633     target_ip = &(target_sd->sem_perm);
1634     target_ip->__key = tswapl(host_ip->__key);
1635     target_ip->uid = tswapl(host_ip->uid);
1636     target_ip->gid = tswapl(host_ip->gid);
1637     target_ip->cuid = tswapl(host_ip->cuid);
1638     target_ip->cgid = tswapl(host_ip->cgid);
1639     target_ip->mode = tswapl(host_ip->mode);
1640     unlock_user_struct(target_sd, target_addr, 1);
1641     return 0;
1642 }
1643
1644 static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
1645                                                abi_ulong target_addr)
1646 {
1647     struct target_semid_ds *target_sd;
1648
1649     if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1650         return -TARGET_EFAULT;
1651     target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1652     host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1653     host_sd->sem_otime = tswapl(target_sd->sem_otime);
1654     host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1655     unlock_user_struct(target_sd, target_addr, 0);
1656     return 0;
1657 }
1658
1659 static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
1660                                                struct semid_ds *host_sd)
1661 {
1662     struct target_semid_ds *target_sd;
1663
1664     if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1665         return -TARGET_EFAULT;
1666     host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1667     target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1668     target_sd->sem_otime = tswapl(host_sd->sem_otime);
1669     target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1670     unlock_user_struct(target_sd, target_addr, 1);
1671     return 0;
1672 }
1673
1674 union semun {
1675         int val;
1676         struct semid_ds *buf;
1677         unsigned short *array;
1678 };
1679
1680 union target_semun {
1681         int val;
1682         abi_long buf;
1683         unsigned short int *array;
1684 };
1685
1686 static inline abi_long target_to_host_semun(int cmd,
1687                                             union semun *host_su,
1688                                             abi_ulong target_addr,
1689                                             struct semid_ds *ds)
1690 {
1691     union target_semun *target_su;
1692
1693     switch( cmd ) {
1694         case IPC_STAT:
1695         case IPC_SET:
1696            if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1697                return -TARGET_EFAULT;
1698            target_to_host_semid_ds(ds,target_su->buf);
1699            host_su->buf = ds;
1700            unlock_user_struct(target_su, target_addr, 0);
1701            break;
1702         case GETVAL:
1703         case SETVAL:
1704            if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1705                return -TARGET_EFAULT;
1706            host_su->val = tswapl(target_su->val);
1707            unlock_user_struct(target_su, target_addr, 0);
1708            break;
1709         case GETALL:
1710         case SETALL:
1711            if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1712                return -TARGET_EFAULT;
1713            *host_su->array = tswap16(*target_su->array);
1714            unlock_user_struct(target_su, target_addr, 0);
1715            break;
1716         default:
1717            gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1718     }
1719     return 0;
1720 }
1721
1722 static inline abi_long host_to_target_semun(int cmd,
1723                                             abi_ulong target_addr,
1724                                             union semun *host_su,
1725                                             struct semid_ds *ds)
1726 {
1727     union target_semun *target_su;
1728
1729     switch( cmd ) {
1730         case IPC_STAT:
1731         case IPC_SET:
1732            if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1733                return -TARGET_EFAULT;
1734            host_to_target_semid_ds(target_su->buf,ds);
1735            unlock_user_struct(target_su, target_addr, 1);
1736            break;
1737         case GETVAL:
1738         case SETVAL:
1739            if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1740                return -TARGET_EFAULT;
1741            target_su->val = tswapl(host_su->val);
1742            unlock_user_struct(target_su, target_addr, 1);
1743            break;
1744         case GETALL:
1745         case SETALL:
1746            if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1747                return -TARGET_EFAULT;
1748            *target_su->array = tswap16(*host_su->array);
1749            unlock_user_struct(target_su, target_addr, 1);
1750            break;
1751         default:
1752            gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1753     }
1754     return 0;
1755 }
1756
1757 static inline abi_long do_semctl(int first, int second, int third,
1758                                  abi_long ptr)
1759 {
1760     union semun arg;
1761     struct semid_ds dsarg;
1762     int cmd = third&0xff;
1763     abi_long ret = 0;
1764
1765     switch( cmd ) {
1766         case GETVAL:
1767             target_to_host_semun(cmd,&arg,ptr,&dsarg);
1768             ret = get_errno(semctl(first, second, cmd, arg));
1769             host_to_target_semun(cmd,ptr,&arg,&dsarg);
1770             break;
1771         case SETVAL:
1772             target_to_host_semun(cmd,&arg,ptr,&dsarg);
1773             ret = get_errno(semctl(first, second, cmd, arg));
1774             host_to_target_semun(cmd,ptr,&arg,&dsarg);
1775             break;
1776         case GETALL:
1777             target_to_host_semun(cmd,&arg,ptr,&dsarg);
1778             ret = get_errno(semctl(first, second, cmd, arg));
1779             host_to_target_semun(cmd,ptr,&arg,&dsarg);
1780             break;
1781         case SETALL:
1782             target_to_host_semun(cmd,&arg,ptr,&dsarg);
1783             ret = get_errno(semctl(first, second, cmd, arg));
1784             host_to_target_semun(cmd,ptr,&arg,&dsarg);
1785             break;
1786         case IPC_STAT:
1787             target_to_host_semun(cmd,&arg,ptr,&dsarg);
1788             ret = get_errno(semctl(first, second, cmd, arg));
1789             host_to_target_semun(cmd,ptr,&arg,&dsarg);
1790             break;
1791         case IPC_SET:
1792             target_to_host_semun(cmd,&arg,ptr,&dsarg);
1793             ret = get_errno(semctl(first, second, cmd, arg));
1794             host_to_target_semun(cmd,ptr,&arg,&dsarg);
1795             break;
1796     default:
1797             ret = get_errno(semctl(first, second, cmd, arg));
1798     }
1799
1800     return ret;
1801 }
1802
1803 struct target_msqid_ds
1804 {
1805   struct target_ipc_perm msg_perm;
1806   abi_ulong msg_stime;
1807   abi_ulong __unused1;
1808   abi_ulong msg_rtime;
1809   abi_ulong __unused2;
1810   abi_ulong msg_ctime;
1811   abi_ulong __unused3;
1812   abi_ulong __msg_cbytes;
1813   abi_ulong msg_qnum;
1814   abi_ulong msg_qbytes;
1815   abi_ulong msg_lspid;
1816   abi_ulong msg_lrpid;
1817   abi_ulong __unused4;
1818   abi_ulong __unused5;
1819 };
1820
1821 static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
1822                                                abi_ulong target_addr)
1823 {
1824     struct target_msqid_ds *target_md;
1825
1826     if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
1827         return -TARGET_EFAULT;
1828     target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1829     host_md->msg_stime = tswapl(target_md->msg_stime);
1830     host_md->msg_rtime = tswapl(target_md->msg_rtime);
1831     host_md->msg_ctime = tswapl(target_md->msg_ctime);
1832     host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1833     host_md->msg_qnum = tswapl(target_md->msg_qnum);
1834     host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1835     host_md->msg_lspid = tswapl(target_md->msg_lspid);
1836     host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1837     unlock_user_struct(target_md, target_addr, 0);
1838     return 0;
1839 }
1840
1841 static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
1842                                                struct msqid_ds *host_md)
1843 {
1844     struct target_msqid_ds *target_md;
1845
1846     if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
1847         return -TARGET_EFAULT;
1848     host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1849     target_md->msg_stime = tswapl(host_md->msg_stime);
1850     target_md->msg_rtime = tswapl(host_md->msg_rtime);
1851     target_md->msg_ctime = tswapl(host_md->msg_ctime);
1852     target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1853     target_md->msg_qnum = tswapl(host_md->msg_qnum);
1854     target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1855     target_md->msg_lspid = tswapl(host_md->msg_lspid);
1856     target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1857     unlock_user_struct(target_md, target_addr, 1);
1858     return 0;
1859 }
1860
1861 static inline abi_long do_msgctl(int first, int second, abi_long ptr)
1862 {
1863     struct msqid_ds dsarg;
1864     int cmd = second&0xff;
1865     abi_long ret = 0;
1866     switch( cmd ) {
1867     case IPC_STAT:
1868     case IPC_SET:
1869         target_to_host_msqid_ds(&dsarg,ptr);
1870         ret = get_errno(msgctl(first, cmd, &dsarg));
1871         host_to_target_msqid_ds(ptr,&dsarg);
1872     default:
1873         ret = get_errno(msgctl(first, cmd, &dsarg));
1874     }
1875     return ret;
1876 }
1877
1878 struct target_msgbuf {
1879         abi_ulong mtype;
1880         char    mtext[1];
1881 };
1882
1883 static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1884                                  unsigned int msgsz, int msgflg)
1885 {
1886     struct target_msgbuf *target_mb;
1887     struct msgbuf *host_mb;
1888     abi_long ret = 0;
1889
1890     if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
1891         return -TARGET_EFAULT;
1892     host_mb = malloc(msgsz+sizeof(long));
1893     host_mb->mtype = tswapl(target_mb->mtype);
1894     memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1895     ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1896     free(host_mb);
1897     unlock_user_struct(target_mb, msgp, 0);
1898
1899     return ret;
1900 }
1901
1902 static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1903                                  unsigned int msgsz, int msgtype,
1904                                  int msgflg)
1905 {
1906     struct target_msgbuf *target_mb;
1907     char *target_mtext;
1908     struct msgbuf *host_mb;
1909     abi_long ret = 0;
1910
1911     if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
1912         return -TARGET_EFAULT;
1913     host_mb = malloc(msgsz+sizeof(long));
1914     ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
1915     if (ret > 0) {
1916         abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
1917         target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
1918         if (!target_mtext) {
1919             ret = -TARGET_EFAULT;
1920             goto end;
1921         }
1922         memcpy(target_mb->mtext, host_mb->mtext, ret);
1923         unlock_user(target_mtext, target_mtext_addr, ret);
1924     }
1925     target_mb->mtype = tswapl(host_mb->mtype);
1926     free(host_mb);
1927
1928 end:
1929     if (target_mb)
1930         unlock_user_struct(target_mb, msgp, 1);
1931     return ret;
1932 }
1933
1934 /* ??? This only works with linear mappings.  */
1935 /* do_ipc() must return target values and target errnos. */
1936 static abi_long do_ipc(unsigned int call, int first,
1937                        int second, int third,
1938                        abi_long ptr, abi_long fifth)
1939 {
1940     int version;
1941     abi_long ret = 0;
1942     struct shmid_ds shm_info;
1943     int i;
1944
1945     version = call >> 16;
1946     call &= 0xffff;
1947
1948     switch (call) {
1949     case IPCOP_semop:
1950         ret = get_errno(semop(first,(struct sembuf *)g2h(ptr), second));
1951         break;
1952
1953     case IPCOP_semget:
1954         ret = get_errno(semget(first, second, third));
1955         break;
1956
1957     case IPCOP_semctl:
1958         ret = do_semctl(first, second, third, ptr);
1959         break;
1960
1961     case IPCOP_semtimedop:
1962         gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
1963         ret = -TARGET_ENOSYS;
1964         break;
1965
1966         case IPCOP_msgget:
1967                 ret = get_errno(msgget(first, second));
1968                 break;
1969
1970         case IPCOP_msgsnd:
1971                 ret = do_msgsnd(first, ptr, second, third);
1972                 break;
1973
1974         case IPCOP_msgctl:
1975                 ret = do_msgctl(first, second, ptr);
1976                 break;
1977
1978         case IPCOP_msgrcv:
1979                 {
1980                       /* XXX: this code is not correct */
1981                       struct ipc_kludge
1982                       {
1983                               void *__unbounded msgp;
1984                               long int msgtyp;
1985                       };
1986
1987                       struct ipc_kludge *foo = (struct ipc_kludge *)g2h(ptr);
1988                       struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
1989
1990                       ret = do_msgrcv(first, (long)msgp, second, 0, third);
1991
1992                 }
1993                 break;
1994
1995     case IPCOP_shmat:
1996         {
1997             abi_ulong raddr;
1998             void *host_addr;
1999             /* SHM_* flags are the same on all linux platforms */
2000             host_addr = shmat(first, (void *)g2h(ptr), second);
2001             if (host_addr == (void *)-1) {
2002                 ret = get_errno((long)host_addr);
2003                 break;
2004             }
2005             raddr = h2g((unsigned long)host_addr);
2006             /* find out the length of the shared memory segment */
2007             
2008             ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
2009             if (is_error(ret)) {
2010                 /* can't get length, bail out */
2011                 shmdt(host_addr);
2012                 break;
2013             }
2014             page_set_flags(raddr, raddr + shm_info.shm_segsz,
2015                            PAGE_VALID | PAGE_READ |
2016                            ((second & SHM_RDONLY)? 0: PAGE_WRITE));
2017             for (i = 0; i < N_SHM_REGIONS; ++i) {
2018                 if (shm_regions[i].start == 0) {
2019                     shm_regions[i].start = raddr;
2020                     shm_regions[i].size = shm_info.shm_segsz;
2021                     break;
2022                 }
2023             }
2024             if (put_user_ual(raddr, third))
2025                 return -TARGET_EFAULT;
2026             ret = 0;
2027         }
2028         break;
2029     case IPCOP_shmdt:
2030         for (i = 0; i < N_SHM_REGIONS; ++i) {
2031             if (shm_regions[i].start == ptr) {
2032                 shm_regions[i].start = 0;
2033                 page_set_flags(ptr, shm_regions[i].size, 0);
2034                 break;
2035             }
2036         }
2037         ret = get_errno(shmdt((void *)g2h(ptr)));
2038         break;
2039
2040     case IPCOP_shmget:
2041         /* IPC_* flag values are the same on all linux platforms */
2042         ret = get_errno(shmget(first, second, third));
2043         break;
2044
2045         /* IPC_* and SHM_* command values are the same on all linux platforms */
2046     case IPCOP_shmctl:
2047         switch(second) {
2048         case IPC_RMID:
2049         case SHM_LOCK:
2050         case SHM_UNLOCK:
2051             ret = get_errno(shmctl(first, second, NULL));
2052             break;
2053         default:
2054             goto unimplemented;
2055         }
2056         break;
2057     default:
2058     unimplemented:
2059         gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
2060         ret = -TARGET_ENOSYS;
2061         break;
2062     }
2063     return ret;
2064 }
2065 #endif
2066
2067 /* kernel structure types definitions */
2068 #define IFNAMSIZ        16
2069
2070 #define STRUCT(name, list...) STRUCT_ ## name,
2071 #define STRUCT_SPECIAL(name) STRUCT_ ## name,
2072 enum {
2073 #include "syscall_types.h"
2074 };
2075 #undef STRUCT
2076 #undef STRUCT_SPECIAL
2077
2078 #define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
2079 #define STRUCT_SPECIAL(name)
2080 #include "syscall_types.h"
2081 #undef STRUCT
2082 #undef STRUCT_SPECIAL
2083
2084 typedef struct IOCTLEntry {
2085     unsigned int target_cmd;
2086     unsigned int host_cmd;
2087     const char *name;
2088     int access;
2089     const argtype arg_type[5];
2090 } IOCTLEntry;
2091
2092 #define IOC_R 0x0001
2093 #define IOC_W 0x0002
2094 #define IOC_RW (IOC_R | IOC_W)
2095
2096 #define MAX_STRUCT_SIZE 4096
2097
2098 IOCTLEntry ioctl_entries[] = {
2099 #define IOCTL(cmd, access, types...) \
2100     { TARGET_ ## cmd, cmd, #cmd, access, { types } },
2101 #include "ioctls.h"
2102     { 0, 0, },
2103 };
2104
2105 /* ??? Implement proper locking for ioctls.  */
2106 /* do_ioctl() Must return target values and target errnos. */
2107 static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
2108 {
2109     const IOCTLEntry *ie;
2110     const argtype *arg_type;
2111     abi_long ret;
2112     uint8_t buf_temp[MAX_STRUCT_SIZE];
2113     int target_size;
2114     void *argptr;
2115
2116     ie = ioctl_entries;
2117     for(;;) {
2118         if (ie->target_cmd == 0) {
2119             gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
2120             return -TARGET_ENOSYS;
2121         }
2122         if (ie->target_cmd == cmd)
2123             break;
2124         ie++;
2125     }
2126     arg_type = ie->arg_type;
2127 #if defined(DEBUG)
2128     gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
2129 #endif
2130     switch(arg_type[0]) {
2131     case TYPE_NULL:
2132         /* no argument */
2133         ret = get_errno(ioctl(fd, ie->host_cmd));
2134         break;
2135     case TYPE_PTRVOID:
2136     case TYPE_INT:
2137         /* int argment */
2138         ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2139         break;
2140     case TYPE_PTR:
2141         arg_type++;
2142         target_size = thunk_type_size(arg_type, 0);
2143         switch(ie->access) {
2144         case IOC_R:
2145             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2146             if (!is_error(ret)) {
2147                 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2148                 if (!argptr)
2149                     return -TARGET_EFAULT;
2150                 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2151                 unlock_user(argptr, arg, target_size);
2152             }
2153             break;
2154         case IOC_W:
2155             argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2156             if (!argptr)
2157                 return -TARGET_EFAULT;
2158             thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2159             unlock_user(argptr, arg, 0);
2160             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2161             break;
2162         default:
2163         case IOC_RW:
2164             argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2165             if (!argptr)
2166                 return -TARGET_EFAULT;
2167             thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2168             unlock_user(argptr, arg, 0);
2169             ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2170             if (!is_error(ret)) {
2171                 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2172                 if (!argptr)
2173                     return -TARGET_EFAULT;
2174                 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2175                 unlock_user(argptr, arg, target_size);
2176             }
2177             break;
2178         }
2179         break;
2180     default:
2181         gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2182                  (long)cmd, arg_type[0]);
2183         ret = -TARGET_ENOSYS;
2184         break;
2185     }
2186     return ret;
2187 }
2188
2189 bitmask_transtbl iflag_tbl[] = {
2190         { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2191         { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2192         { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2193         { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2194         { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2195         { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2196         { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2197         { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2198         { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2199         { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2200         { TARGET_IXON, TARGET_IXON, IXON, IXON },
2201         { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2202         { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
2203         { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
2204         { 0, 0, 0, 0 }
2205 };
2206
2207 bitmask_transtbl oflag_tbl[] = {
2208         { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
2209         { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
2210         { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
2211         { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
2212         { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
2213         { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
2214         { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
2215         { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
2216         { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
2217         { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
2218         { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
2219         { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
2220         { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
2221         { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
2222         { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
2223         { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
2224         { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
2225         { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
2226         { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
2227         { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
2228         { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
2229         { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
2230         { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
2231         { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
2232         { 0, 0, 0, 0 }
2233 };
2234
2235 bitmask_transtbl cflag_tbl[] = {
2236         { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
2237         { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
2238         { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
2239         { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
2240         { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
2241         { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
2242         { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
2243         { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
2244         { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
2245         { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
2246         { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
2247         { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
2248         { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
2249         { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
2250         { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
2251         { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
2252         { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
2253         { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
2254         { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
2255         { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
2256         { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
2257         { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
2258         { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
2259         { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
2260         { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
2261         { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
2262         { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
2263         { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
2264         { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
2265         { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
2266         { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
2267         { 0, 0, 0, 0 }
2268 };
2269
2270 bitmask_transtbl lflag_tbl[] = {
2271         { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2272         { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2273         { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2274         { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2275         { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2276         { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2277         { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2278         { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2279         { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2280         { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2281         { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2282         { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2283         { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2284         { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2285         { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
2286         { 0, 0, 0, 0 }
2287 };
2288
2289 static void target_to_host_termios (void *dst, const void *src)
2290 {
2291     struct host_termios *host = dst;
2292     const struct target_termios *target = src;
2293
2294     host->c_iflag =
2295         target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
2296     host->c_oflag =
2297         target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
2298     host->c_cflag =
2299         target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
2300     host->c_lflag =
2301         target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2302     host->c_line = target->c_line;
2303
2304     host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2305     host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
2306     host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
2307     host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
2308     host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
2309     host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
2310     host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
2311     host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
2312     host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
2313     host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2314     host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
2315     host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2316     host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2317     host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2318     host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2319     host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
2320     host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
2321 }
2322
2323 static void host_to_target_termios (void *dst, const void *src)
2324 {
2325     struct target_termios *target = dst;
2326     const struct host_termios *host = src;
2327
2328     target->c_iflag =
2329         tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
2330     target->c_oflag =
2331         tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
2332     target->c_cflag =
2333         tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
2334     target->c_lflag =
2335         tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2336     target->c_line = host->c_line;
2337
2338     target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2339     target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2340     target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2341     target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2342     target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2343     target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2344     target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2345     target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2346     target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2347     target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2348     target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2349     target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2350     target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2351     target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2352     target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2353     target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2354     target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2355 }
2356
2357 StructEntry struct_termios_def = {
2358     .convert = { host_to_target_termios, target_to_host_termios },
2359     .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2360     .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2361 };
2362
2363 static bitmask_transtbl mmap_flags_tbl[] = {
2364         { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2365         { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2366         { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2367         { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2368         { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2369         { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2370         { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2371         { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2372         { 0, 0, 0, 0 }
2373 };
2374
2375 static bitmask_transtbl fcntl_flags_tbl[] = {
2376         { TARGET_O_ACCMODE,   TARGET_O_WRONLY,    O_ACCMODE,   O_WRONLY,    },
2377         { TARGET_O_ACCMODE,   TARGET_O_RDWR,      O_ACCMODE,   O_RDWR,      },
2378         { TARGET_O_CREAT,     TARGET_O_CREAT,     O_CREAT,     O_CREAT,     },
2379         { TARGET_O_EXCL,      TARGET_O_EXCL,      O_EXCL,      O_EXCL,      },
2380         { TARGET_O_NOCTTY,    TARGET_O_NOCTTY,    O_NOCTTY,    O_NOCTTY,    },
2381         { TARGET_O_TRUNC,     TARGET_O_TRUNC,     O_TRUNC,     O_TRUNC,     },
2382         { TARGET_O_APPEND,    TARGET_O_APPEND,    O_APPEND,    O_APPEND,    },
2383         { TARGET_O_NONBLOCK,  TARGET_O_NONBLOCK,  O_NONBLOCK,  O_NONBLOCK,  },
2384         { TARGET_O_SYNC,      TARGET_O_SYNC,      O_SYNC,      O_SYNC,      },
2385         { TARGET_FASYNC,      TARGET_FASYNC,      FASYNC,      FASYNC,      },
2386         { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2387         { TARGET_O_NOFOLLOW,  TARGET_O_NOFOLLOW,  O_NOFOLLOW,  O_NOFOLLOW,  },
2388         { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
2389 #if defined(O_DIRECT)
2390         { TARGET_O_DIRECT,    TARGET_O_DIRECT,    O_DIRECT,    O_DIRECT,    },
2391 #endif
2392         { 0, 0, 0, 0 }
2393 };
2394
2395 #if defined(TARGET_I386)
2396
2397 /* NOTE: there is really one LDT for all the threads */
2398 uint8_t *ldt_table;
2399
2400 static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
2401 {
2402     int size;
2403     void *p;
2404
2405     if (!ldt_table)
2406         return 0;
2407     size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2408     if (size > bytecount)
2409         size = bytecount;
2410     p = lock_user(VERIFY_WRITE, ptr, size, 0);
2411     if (!p)
2412         return -TARGET_EFAULT;
2413     /* ??? Should this by byteswapped?  */
2414     memcpy(p, ldt_table, size);
2415     unlock_user(p, ptr, size);
2416     return size;
2417 }
2418
2419 /* XXX: add locking support */
2420 static abi_long write_ldt(CPUX86State *env,
2421                           abi_ulong ptr, unsigned long bytecount, int oldmode)
2422 {
2423     struct target_modify_ldt_ldt_s ldt_info;
2424     struct target_modify_ldt_ldt_s *target_ldt_info;
2425     int seg_32bit, contents, read_exec_only, limit_in_pages;
2426     int seg_not_present, useable, lm;
2427     uint32_t *lp, entry_1, entry_2;
2428
2429     if (bytecount != sizeof(ldt_info))
2430         return -TARGET_EINVAL;
2431     if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
2432         return -TARGET_EFAULT;
2433     ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2434     ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2435     ldt_info.limit = tswap32(target_ldt_info->limit);
2436     ldt_info.flags = tswap32(target_ldt_info->flags);
2437     unlock_user_struct(target_ldt_info, ptr, 0);
2438
2439     if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
2440         return -TARGET_EINVAL;
2441     seg_32bit = ldt_info.flags & 1;
2442     contents = (ldt_info.flags >> 1) & 3;
2443     read_exec_only = (ldt_info.flags >> 3) & 1;
2444     limit_in_pages = (ldt_info.flags >> 4) & 1;
2445     seg_not_present = (ldt_info.flags >> 5) & 1;
2446     useable = (ldt_info.flags >> 6) & 1;
2447 #ifdef TARGET_ABI32
2448     lm = 0;
2449 #else
2450     lm = (ldt_info.flags >> 7) & 1;
2451 #endif
2452     if (contents == 3) {
2453         if (oldmode)
2454             return -TARGET_EINVAL;
2455         if (seg_not_present == 0)
2456             return -TARGET_EINVAL;
2457     }
2458     /* allocate the LDT */
2459     if (!ldt_table) {
2460         ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2461         if (!ldt_table)
2462             return -TARGET_ENOMEM;
2463         memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2464         env->ldt.base = h2g((unsigned long)ldt_table);
2465         env->ldt.limit = 0xffff;
2466     }
2467
2468     /* NOTE: same code as Linux kernel */
2469     /* Allow LDTs to be cleared by the user. */
2470     if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2471         if (oldmode ||
2472             (contents == 0              &&
2473              read_exec_only == 1        &&
2474              seg_32bit == 0             &&
2475              limit_in_pages == 0        &&
2476              seg_not_present == 1       &&
2477              useable == 0 )) {
2478             entry_1 = 0;
2479             entry_2 = 0;
2480             goto install;
2481         }
2482     }
2483
2484     entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2485         (ldt_info.limit & 0x0ffff);
2486     entry_2 = (ldt_info.base_addr & 0xff000000) |
2487         ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2488         (ldt_info.limit & 0xf0000) |
2489         ((read_exec_only ^ 1) << 9) |
2490         (contents << 10) |
2491         ((seg_not_present ^ 1) << 15) |
2492         (seg_32bit << 22) |
2493         (limit_in_pages << 23) |
2494         (lm << 21) |
2495         0x7000;
2496     if (!oldmode)
2497         entry_2 |= (useable << 20);
2498
2499     /* Install the new entry ...  */
2500 install:
2501     lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2502     lp[0] = tswap32(entry_1);
2503     lp[1] = tswap32(entry_2);
2504     return 0;
2505 }
2506
2507 /* specific and weird i386 syscalls */
2508 abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr, 
2509                        unsigned long bytecount)
2510 {
2511     abi_long ret;
2512
2513     switch (func) {
2514     case 0:
2515         ret = read_ldt(ptr, bytecount);
2516         break;
2517     case 1:
2518         ret = write_ldt(env, ptr, bytecount, 1);
2519         break;
2520     case 0x11:
2521         ret = write_ldt(env, ptr, bytecount, 0);
2522         break;
2523     default:
2524         ret = -TARGET_ENOSYS;
2525         break;
2526     }
2527     return ret;
2528 }
2529
2530 abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
2531 {
2532     uint64_t *gdt_table = g2h(env->gdt.base);
2533     struct target_modify_ldt_ldt_s ldt_info;
2534     struct target_modify_ldt_ldt_s *target_ldt_info;
2535     int seg_32bit, contents, read_exec_only, limit_in_pages;
2536     int seg_not_present, useable, lm;
2537     uint32_t *lp, entry_1, entry_2;
2538     int i;
2539
2540     lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
2541     if (!target_ldt_info)
2542         return -TARGET_EFAULT;
2543     ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2544     ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2545     ldt_info.limit = tswap32(target_ldt_info->limit);
2546     ldt_info.flags = tswap32(target_ldt_info->flags);
2547     if (ldt_info.entry_number == -1) {
2548         for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
2549             if (gdt_table[i] == 0) {
2550                 ldt_info.entry_number = i;
2551                 target_ldt_info->entry_number = tswap32(i);
2552                 break;
2553             }
2554         }
2555     }
2556     unlock_user_struct(target_ldt_info, ptr, 1);
2557
2558     if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN || 
2559         ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
2560            return -TARGET_EINVAL;
2561     seg_32bit = ldt_info.flags & 1;
2562     contents = (ldt_info.flags >> 1) & 3;
2563     read_exec_only = (ldt_info.flags >> 3) & 1;
2564     limit_in_pages = (ldt_info.flags >> 4) & 1;
2565     seg_not_present = (ldt_info.flags >> 5) & 1;
2566     useable = (ldt_info.flags >> 6) & 1;
2567 #ifdef TARGET_ABI32
2568     lm = 0;
2569 #else
2570     lm = (ldt_info.flags >> 7) & 1;
2571 #endif
2572
2573     if (contents == 3) {
2574         if (seg_not_present == 0)
2575             return -TARGET_EINVAL;
2576     }
2577
2578     /* NOTE: same code as Linux kernel */
2579     /* Allow LDTs to be cleared by the user. */
2580     if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2581         if ((contents == 0             &&
2582              read_exec_only == 1       &&
2583              seg_32bit == 0            &&
2584              limit_in_pages == 0       &&
2585              seg_not_present == 1      &&
2586              useable == 0 )) {
2587             entry_1 = 0;
2588             entry_2 = 0;
2589             goto install;
2590         }
2591     }
2592
2593     entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2594         (ldt_info.limit & 0x0ffff);
2595     entry_2 = (ldt_info.base_addr & 0xff000000) |
2596         ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2597         (ldt_info.limit & 0xf0000) |
2598         ((read_exec_only ^ 1) << 9) |
2599         (contents << 10) |
2600         ((seg_not_present ^ 1) << 15) |
2601         (seg_32bit << 22) |
2602         (limit_in_pages << 23) |
2603         (useable << 20) |
2604         (lm << 21) |
2605         0x7000;
2606
2607     /* Install the new entry ...  */
2608 install:
2609     lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
2610     lp[0] = tswap32(entry_1);
2611     lp[1] = tswap32(entry_2);
2612     return 0;
2613 }
2614
2615 abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
2616 {
2617     struct target_modify_ldt_ldt_s *target_ldt_info;
2618     uint64_t *gdt_table = g2h(env->gdt.base);
2619     uint32_t base_addr, limit, flags;
2620     int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
2621     int seg_not_present, useable, lm;
2622     uint32_t *lp, entry_1, entry_2;
2623
2624     lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
2625     if (!target_ldt_info)
2626         return -TARGET_EFAULT;
2627     idx = tswap32(target_ldt_info->entry_number);
2628     if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
2629         idx > TARGET_GDT_ENTRY_TLS_MAX) {
2630         unlock_user_struct(target_ldt_info, ptr, 1);
2631         return -TARGET_EINVAL;
2632     }
2633     lp = (uint32_t *)(gdt_table + idx);
2634     entry_1 = tswap32(lp[0]);
2635     entry_2 = tswap32(lp[1]);
2636     
2637     read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
2638     contents = (entry_2 >> 10) & 3;
2639     seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
2640     seg_32bit = (entry_2 >> 22) & 1;
2641     limit_in_pages = (entry_2 >> 23) & 1;
2642     useable = (entry_2 >> 20) & 1;
2643 #ifdef TARGET_ABI32
2644     lm = 0;
2645 #else
2646     lm = (entry_2 >> 21) & 1;
2647 #endif
2648     flags = (seg_32bit << 0) | (contents << 1) |
2649         (read_exec_only << 3) | (limit_in_pages << 4) |
2650         (seg_not_present << 5) | (useable << 6) | (lm << 7);
2651     limit = (entry_1 & 0xffff) | (entry_2  & 0xf0000);
2652     base_addr = (entry_1 >> 16) | 
2653         (entry_2 & 0xff000000) | 
2654         ((entry_2 & 0xff) << 16);
2655     target_ldt_info->base_addr = tswapl(base_addr);
2656     target_ldt_info->limit = tswap32(limit);
2657     target_ldt_info->flags = tswap32(flags);
2658     unlock_user_struct(target_ldt_info, ptr, 1);
2659     return 0;
2660 }
2661
2662 #ifndef TARGET_ABI32
2663 abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
2664 {
2665     abi_long ret;
2666     abi_ulong val;
2667     int idx;
2668     
2669     switch(code) {
2670     case TARGET_ARCH_SET_GS:
2671     case TARGET_ARCH_SET_FS:
2672         if (code == TARGET_ARCH_SET_GS)
2673             idx = R_GS;
2674         else
2675             idx = R_FS;
2676         cpu_x86_load_seg(env, idx, 0);
2677         env->segs[idx].base = addr;
2678         break;
2679     case TARGET_ARCH_GET_GS:
2680     case TARGET_ARCH_GET_FS:
2681         if (code == TARGET_ARCH_GET_GS)
2682             idx = R_GS;
2683         else
2684             idx = R_FS;
2685         val = env->segs[idx].base;
2686         if (put_user(val, addr, abi_ulong))
2687             return -TARGET_EFAULT;
2688         break;
2689     default:
2690         ret = -TARGET_EINVAL;
2691         break;
2692     }
2693     return 0;
2694 }
2695 #endif
2696
2697 #endif /* defined(TARGET_I386) */
2698
2699 /* this stack is the equivalent of the kernel stack associated with a
2700    thread/process */
2701 #define NEW_STACK_SIZE 8192
2702
2703 static int clone_func(void *arg)
2704 {
2705     CPUState *env = arg;
2706     cpu_loop(env);
2707     /* never exits */
2708     return 0;
2709 }
2710
2711 /* do_fork() Must return host values and target errnos (unlike most
2712    do_*() functions). */
2713 int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp)
2714 {
2715     int ret;
2716     TaskState *ts;
2717     uint8_t *new_stack;
2718     CPUState *new_env;
2719
2720     if (flags & CLONE_VM) {
2721         ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2722         memset(ts, 0, sizeof(TaskState));
2723         new_stack = ts->stack;
2724         ts->used = 1;
2725         /* add in task state list */
2726         ts->next = first_task_state;
2727         first_task_state = ts;
2728         /* we create a new CPU instance. */
2729         new_env = cpu_copy(env);
2730 #if defined(TARGET_I386)
2731         if (!newsp)
2732             newsp = env->regs[R_ESP];
2733         new_env->regs[R_ESP] = newsp;
2734         new_env->regs[R_EAX] = 0;
2735 #elif defined(TARGET_ARM)
2736         if (!newsp)
2737             newsp = env->regs[13];
2738         new_env->regs[13] = newsp;
2739         new_env->regs[0] = 0;
2740 #elif defined(TARGET_SPARC)
2741         if (!newsp)
2742             newsp = env->regwptr[22];
2743         new_env->regwptr[22] = newsp;
2744         new_env->regwptr[0] = 0;
2745         /* XXXXX */
2746         printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
2747 #elif defined(TARGET_M68K)
2748         if (!newsp)
2749             newsp = env->aregs[7];
2750         new_env->aregs[7] = newsp;
2751         new_env->dregs[0] = 0;
2752         /* ??? is this sufficient?  */
2753 #elif defined(TARGET_MIPS)
2754         if (!newsp)
2755             newsp = env->gpr[29][env->current_tc];
2756         new_env->gpr[29][env->current_tc] = newsp;
2757 #elif defined(TARGET_PPC)
2758         if (!newsp)
2759             newsp = env->gpr[1];
2760         new_env->gpr[1] = newsp;
2761         {
2762             int i;
2763             for (i = 7; i < 32; i++)
2764                 new_env->gpr[i] = 0;
2765         }
2766 #elif defined(TARGET_SH4)
2767         if (!newsp)
2768           newsp = env->gregs[15];
2769         new_env->gregs[15] = newsp;
2770         /* XXXXX */
2771 #elif defined(TARGET_ALPHA)
2772        if (!newsp)
2773          newsp = env->ir[30];
2774        new_env->ir[30] = newsp;
2775         /* ? */
2776         {
2777             int i;
2778             for (i = 7; i < 30; i++)
2779                 new_env->ir[i] = 0;
2780         }
2781 #elif defined(TARGET_CRIS)
2782         if (!newsp)
2783           newsp = env->regs[14];
2784         new_env->regs[14] = newsp;
2785 #else
2786 #error unsupported target CPU
2787 #endif
2788         new_env->opaque = ts;
2789 #ifdef __ia64__
2790         ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2791 #else
2792         ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2793 #endif
2794     } else {
2795         /* if no CLONE_VM, we consider it is a fork */
2796         if ((flags & ~CSIGNAL) != 0)
2797             return -EINVAL;
2798         ret = fork();
2799     }
2800     return ret;
2801 }
2802
2803 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
2804 {
2805     struct flock fl;
2806     struct target_flock *target_fl;
2807     struct flock64 fl64;
2808     struct target_flock64 *target_fl64;
2809     abi_long ret;
2810
2811     switch(cmd) {
2812     case TARGET_F_GETLK:
2813         if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2814             return -TARGET_EFAULT;
2815         fl.l_type = tswap16(target_fl->l_type);
2816         fl.l_whence = tswap16(target_fl->l_whence);
2817         fl.l_start = tswapl(target_fl->l_start);
2818         fl.l_len = tswapl(target_fl->l_len);
2819         fl.l_pid = tswapl(target_fl->l_pid);
2820         unlock_user_struct(target_fl, arg, 0);
2821         ret = get_errno(fcntl(fd, cmd, &fl));
2822         if (ret == 0) {
2823             if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
2824                 return -TARGET_EFAULT;
2825             target_fl->l_type = tswap16(fl.l_type);
2826             target_fl->l_whence = tswap16(fl.l_whence);
2827             target_fl->l_start = tswapl(fl.l_start);
2828             target_fl->l_len = tswapl(fl.l_len);
2829             target_fl->l_pid = tswapl(fl.l_pid);
2830             unlock_user_struct(target_fl, arg, 1);
2831         }
2832         break;
2833
2834     case TARGET_F_SETLK:
2835     case TARGET_F_SETLKW:
2836         if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2837             return -TARGET_EFAULT;
2838         fl.l_type = tswap16(target_fl->l_type);
2839         fl.l_whence = tswap16(target_fl->l_whence);
2840         fl.l_start = tswapl(target_fl->l_start);
2841         fl.l_len = tswapl(target_fl->l_len);
2842         fl.l_pid = tswapl(target_fl->l_pid);
2843         unlock_user_struct(target_fl, arg, 0);
2844         ret = get_errno(fcntl(fd, cmd, &fl));
2845         break;
2846
2847     case TARGET_F_GETLK64:
2848         if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2849             return -TARGET_EFAULT;
2850         fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2851         fl64.l_whence = tswap16(target_fl64->l_whence);
2852         fl64.l_start = tswapl(target_fl64->l_start);
2853         fl64.l_len = tswapl(target_fl64->l_len);
2854         fl64.l_pid = tswap16(target_fl64->l_pid);
2855         unlock_user_struct(target_fl64, arg, 0);
2856         ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
2857         if (ret == 0) {
2858             if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
2859                 return -TARGET_EFAULT;
2860             target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2861             target_fl64->l_whence = tswap16(fl64.l_whence);
2862             target_fl64->l_start = tswapl(fl64.l_start);
2863             target_fl64->l_len = tswapl(fl64.l_len);
2864             target_fl64->l_pid = tswapl(fl64.l_pid);
2865             unlock_user_struct(target_fl64, arg, 1);
2866         }
2867         break;
2868     case TARGET_F_SETLK64:
2869     case TARGET_F_SETLKW64:
2870         if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2871             return -TARGET_EFAULT;
2872         fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2873         fl64.l_whence = tswap16(target_fl64->l_whence);
2874         fl64.l_start = tswapl(target_fl64->l_start);
2875         fl64.l_len = tswapl(target_fl64->l_len);
2876         fl64.l_pid = tswap16(target_fl64->l_pid);
2877         unlock_user_struct(target_fl64, arg, 0);
2878         ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
2879         break;
2880
2881     case F_GETFL:
2882         ret = get_errno(fcntl(fd, cmd, arg));
2883         if (ret >= 0) {
2884             ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2885         }
2886         break;
2887
2888     case F_SETFL:
2889         ret = get_errno(fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
2890         break;
2891
2892     default:
2893         ret = get_errno(fcntl(fd, cmd, arg));
2894         break;
2895     }
2896     return ret;
2897 }
2898
2899 #ifdef USE_UID16
2900
2901 static inline int high2lowuid(int uid)
2902 {
2903     if (uid > 65535)
2904         return 65534;
2905     else
2906         return uid;
2907 }
2908
2909 static inline int high2lowgid(int gid)
2910 {
2911     if (gid > 65535)
2912         return 65534;
2913     else
2914         return gid;
2915 }
2916
2917 static inline int low2highuid(int uid)
2918 {
2919     if ((int16_t)uid == -1)
2920         return -1;
2921     else
2922         return uid;
2923 }
2924
2925 static inline int low2highgid(int gid)
2926 {
2927     if ((int16_t)gid == -1)
2928         return -1;
2929     else
2930         return gid;
2931 }
2932
2933 #endif /* USE_UID16 */
2934
2935 void syscall_init(void)
2936 {
2937     IOCTLEntry *ie;
2938     const argtype *arg_type;
2939     int size;
2940     int i;
2941
2942 #define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2943 #define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
2944 #include "syscall_types.h"
2945 #undef STRUCT
2946 #undef STRUCT_SPECIAL
2947
2948     /* we patch the ioctl size if necessary. We rely on the fact that
2949        no ioctl has all the bits at '1' in the size field */
2950     ie = ioctl_entries;
2951     while (ie->target_cmd != 0) {
2952         if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2953             TARGET_IOC_SIZEMASK) {
2954             arg_type = ie->arg_type;
2955             if (arg_type[0] != TYPE_PTR) {
2956                 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
2957                         ie->target_cmd);
2958                 exit(1);
2959             }
2960             arg_type++;
2961             size = thunk_type_size(arg_type, 0);
2962             ie->target_cmd = (ie->target_cmd &
2963                               ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2964                 (size << TARGET_IOC_SIZESHIFT);
2965         }
2966
2967         /* Build target_to_host_errno_table[] table from
2968          * host_to_target_errno_table[]. */
2969         for (i=0; i < ERRNO_TABLE_SIZE; i++)
2970                 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
2971
2972         /* automatic consistency check if same arch */
2973 #if defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)
2974         if (ie->target_cmd != ie->host_cmd) {
2975             fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
2976                     ie->target_cmd, ie->host_cmd);
2977         }
2978 #endif
2979         ie++;
2980     }
2981 }
2982
2983 #if TARGET_ABI_BITS == 32
2984 static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2985 {
2986 #ifdef TARGET_WORDS_BIG_ENDIAN
2987     return ((uint64_t)word0 << 32) | word1;
2988 #else
2989     return ((uint64_t)word1 << 32) | word0;
2990 #endif
2991 }
2992 #else /* TARGET_ABI_BITS == 32 */
2993 static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2994 {
2995     return word0;
2996 }
2997 #endif /* TARGET_ABI_BITS != 32 */
2998
2999 #ifdef TARGET_NR_truncate64
3000 static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
3001                                          abi_long arg2,
3002                                          abi_long arg3,
3003                                          abi_long arg4)
3004 {
3005 #ifdef TARGET_ARM
3006     if (((CPUARMState *)cpu_env)->eabi)
3007       {
3008         arg2 = arg3;
3009         arg3 = arg4;
3010       }
3011 #endif
3012     return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
3013 }
3014 #endif
3015
3016 #ifdef TARGET_NR_ftruncate64
3017 static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
3018                                           abi_long arg2,
3019                                           abi_long arg3,
3020                                           abi_long arg4)
3021 {
3022 #ifdef TARGET_ARM
3023     if (((CPUARMState *)cpu_env)->eabi)
3024       {
3025         arg2 = arg3;
3026         arg3 = arg4;
3027       }
3028 #endif
3029     return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
3030 }
3031 #endif
3032
3033 static inline abi_long target_to_host_timespec(struct timespec *host_ts,
3034                                                abi_ulong target_addr)
3035 {
3036     struct target_timespec *target_ts;
3037
3038     if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
3039         return -TARGET_EFAULT;
3040     host_ts->tv_sec = tswapl(target_ts->tv_sec);
3041     host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
3042     unlock_user_struct(target_ts, target_addr, 0);
3043 }
3044
3045 static inline abi_long host_to_target_timespec(abi_ulong target_addr,
3046                                                struct timespec *host_ts)
3047 {
3048     struct target_timespec *target_ts;
3049
3050     if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
3051         return -TARGET_EFAULT;
3052     target_ts->tv_sec = tswapl(host_ts->tv_sec);
3053     target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
3054     unlock_user_struct(target_ts, target_addr, 1);
3055 }
3056
3057 /* do_syscall() should always have a single exit point at the end so
3058    that actions, such as logging of syscall results, can be performed.
3059    All errnos that do_syscall() returns must be -TARGET_<errcode>. */
3060 abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
3061                     abi_long arg2, abi_long arg3, abi_long arg4,
3062                     abi_long arg5, abi_long arg6)
3063 {
3064     abi_long ret;
3065     struct stat st;
3066     struct statfs stfs;
3067     void *p;
3068
3069 #ifdef DEBUG
3070     gemu_log("syscall %d", num);
3071 #endif
3072     if(do_strace)
3073         print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
3074
3075     switch(num) {
3076     case TARGET_NR_exit:
3077 #ifdef HAVE_GPROF
3078         _mcleanup();
3079 #endif
3080         gdb_exit(cpu_env, arg1);
3081         /* XXX: should free thread stack and CPU env */
3082         _exit(arg1);
3083         ret = 0; /* avoid warning */
3084         break;
3085     case TARGET_NR_read:
3086         if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
3087             goto efault;
3088         ret = get_errno(read(arg1, p, arg3));
3089         unlock_user(p, arg2, ret);
3090         break;
3091     case TARGET_NR_write:
3092         if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
3093             goto efault;
3094         ret = get_errno(write(arg1, p, arg3));
3095         unlock_user(p, arg2, 0);
3096         break;
3097     case TARGET_NR_open:
3098         if (!(p = lock_user_string(arg1)))
3099             goto efault;
3100         ret = get_errno(open(path(p),
3101                              target_to_host_bitmask(arg2, fcntl_flags_tbl),
3102                              arg3));
3103         unlock_user(p, arg1, 0);
3104         break;
3105 #if defined(TARGET_NR_openat) && defined(__NR_openat)
3106     case TARGET_NR_openat:
3107         if (!(p = lock_user_string(arg2)))
3108             goto efault;
3109         ret = get_errno(sys_openat(arg1,
3110                                    path(p),
3111                                    target_to_host_bitmask(arg3, fcntl_flags_tbl),
3112                                    arg4));
3113         unlock_user(p, arg2, 0);
3114         break;
3115 #endif
3116     case TARGET_NR_close:
3117         ret = get_errno(close(arg1));
3118         break;
3119     case TARGET_NR_brk:
3120         ret = do_brk(arg1);
3121         break;
3122     case TARGET_NR_fork:
3123         ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
3124         break;
3125 #ifdef TARGET_NR_waitpid
3126     case TARGET_NR_waitpid:
3127         {
3128             int status;
3129             ret = get_errno(waitpid(arg1, &status, arg3));
3130             if (!is_error(ret) && arg2
3131                 && put_user_s32(status, arg2))
3132                 goto efault;
3133         }
3134         break;
3135 #endif
3136 #ifdef TARGET_NR_creat /* not on alpha */
3137     case TARGET_NR_creat:
3138         if (!(p = lock_user_string(arg1)))
3139             goto efault;
3140         ret = get_errno(creat(p, arg2));
3141         unlock_user(p, arg1, 0);
3142         break;
3143 #endif
3144     case TARGET_NR_link:
3145         {
3146             void * p2;
3147             p = lock_user_string(arg1);
3148             p2 = lock_user_string(arg2);
3149             if (!p || !p2)
3150                 ret = -TARGET_EFAULT;
3151             else
3152                 ret = get_errno(link(p, p2));
3153             unlock_user(p2, arg2, 0);
3154             unlock_user(p, arg1, 0);
3155         }
3156         break;
3157 #if defined(TARGET_NR_linkat) && defined(__NR_linkat)
3158     case TARGET_NR_linkat:
3159         {
3160             void * p2 = NULL;
3161             if (!arg2 || !arg4)
3162                 goto efault;
3163             p  = lock_user_string(arg2);
3164             p2 = lock_user_string(arg4);
3165             if (!p || !p2)
3166                 ret = -TARGET_EFAULT;
3167             else
3168                 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
3169             unlock_user(p, arg2, 0);
3170             unlock_user(p2, arg4, 0);
3171         }
3172         break;
3173 #endif
3174     case TARGET_NR_unlink:
3175         if (!(p = lock_user_string(arg1)))
3176             goto efault;
3177         ret = get_errno(unlink(p));
3178         unlock_user(p, arg1, 0);
3179         break;
3180 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
3181     case TARGET_NR_unlinkat:
3182         if (!(p = lock_user_string(arg2)))
3183             goto efault;
3184         ret = get_errno(sys_unlinkat(arg1, p, arg3));
3185         unlock_user(p, arg2, 0);
3186         break;
3187 #endif
3188     case TARGET_NR_execve:
3189         {
3190             char **argp, **envp;
3191             int argc, envc;
3192             abi_ulong gp;
3193             abi_ulong guest_argp;
3194             abi_ulong guest_envp;
3195             abi_ulong addr;
3196             char **q;
3197
3198             argc = 0;
3199             guest_argp = arg2;
3200             for (gp = guest_argp; ; gp += sizeof(abi_ulong)) {
3201                 if (get_user_ual(addr, gp))
3202                     goto efault;
3203                 if (!addr)
3204                     break;
3205                 argc++;
3206             }
3207             envc = 0;
3208             guest_envp = arg3;
3209             for (gp = guest_envp; ; gp += sizeof(abi_ulong)) {
3210                 if (get_user_ual(addr, gp))
3211                     goto efault;
3212                 if (!addr)
3213                     break;
3214                 envc++;
3215             }
3216
3217             argp = alloca((argc + 1) * sizeof(void *));
3218             envp = alloca((envc + 1) * sizeof(void *));
3219
3220             for (gp = guest_argp, q = argp; ;
3221                   gp += sizeof(abi_ulong), q++) {
3222                 if (get_user_ual(addr, gp))
3223                     goto execve_efault;
3224                 if (!addr)
3225                     break;
3226                 if (!(*q = lock_user_string(addr)))
3227                     goto execve_efault;
3228             }
3229             *q = NULL;
3230
3231             for (gp = guest_envp, q = envp; ;
3232                   gp += sizeof(abi_ulong), q++) {
3233                 if (get_user_ual(addr, gp))
3234                     goto execve_efault;
3235                 if (!addr)
3236                     break;
3237                 if (!(*q = lock_user_string(addr)))
3238                     goto execve_efault;
3239             }
3240             *q = NULL;
3241
3242             if (!(p = lock_user_string(arg1)))
3243                 goto execve_efault;
3244             ret = get_errno(execve(p, argp, envp));
3245             unlock_user(p, arg1, 0);
3246
3247             goto execve_end;
3248
3249         execve_efault:
3250             ret = -TARGET_EFAULT;
3251
3252         execve_end:
3253             for (gp = guest_argp, q = argp; *q;
3254                   gp += sizeof(abi_ulong), q++) {
3255                 if (get_user_ual(addr, gp)
3256                     || !addr)
3257                     break;
3258                 unlock_user(*q, addr, 0);
3259             }
3260             for (gp = guest_envp, q = envp; *q;
3261                   gp += sizeof(abi_ulong), q++) {
3262                 if (get_user_ual(addr, gp)
3263                     || !addr)
3264                     break;
3265                 unlock_user(*q, addr, 0);
3266             }
3267         }
3268         break;
3269     case TARGET_NR_chdir:
3270         if (!(p = lock_user_string(arg1)))
3271             goto efault;
3272         ret = get_errno(chdir(p));
3273         unlock_user(p, arg1, 0);
3274         break;
3275 #ifdef TARGET_NR_time
3276     case TARGET_NR_time:
3277         {
3278             time_t host_time;
3279             ret = get_errno(time(&host_time));
3280             if (!is_error(ret)
3281                 && arg1
3282                 && put_user_sal(host_time, arg1))
3283                 goto efault;
3284         }
3285         break;
3286 #endif
3287     case TARGET_NR_mknod:
3288         if (!(p = lock_user_string(arg1)))
3289             goto efault;
3290         ret = get_errno(mknod(p, arg2, arg3));
3291         unlock_user(p, arg1, 0);
3292         break;
3293 #if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
3294     case TARGET_NR_mknodat:
3295         if (!(p = lock_user_string(arg2)))
3296             goto efault;
3297         ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
3298         unlock_user(p, arg2, 0);
3299         break;
3300 #endif
3301     case TARGET_NR_chmod:
3302         if (!(p = lock_user_string(arg1)))
3303             goto efault;
3304         ret = get_errno(chmod(p, arg2));
3305         unlock_user(p, arg1, 0);
3306         break;
3307 #ifdef TARGET_NR_break
3308     case TARGET_NR_break:
3309         goto unimplemented;
3310 #endif
3311 #ifdef TARGET_NR_oldstat
3312     case TARGET_NR_oldstat:
3313         goto unimplemented;
3314 #endif
3315     case TARGET_NR_lseek:
3316         ret = get_errno(lseek(arg1, arg2, arg3));
3317         break;
3318 #ifdef TARGET_NR_getxpid
3319     case TARGET_NR_getxpid:
3320 #else
3321     case TARGET_NR_getpid:
3322 #endif
3323         ret = get_errno(getpid());
3324         break;
3325     case TARGET_NR_mount:
3326                 {
3327                         /* need to look at the data field */
3328                         void *p2, *p3;
3329                         p = lock_user_string(arg1);
3330                         p2 = lock_user_string(arg2);
3331                         p3 = lock_user_string(arg3);
3332                         if (!p || !p2 || !p3)
3333                             ret = -TARGET_EFAULT;
3334                         else
3335                             /* FIXME - arg5 should be locked, but it isn't clear how to
3336                              * do that since it's not guaranteed to be a NULL-terminated
3337                              * string.
3338                              */
3339                             ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
3340                         unlock_user(p, arg1, 0);
3341                         unlock_user(p2, arg2, 0);
3342                         unlock_user(p3, arg3, 0);
3343                         break;
3344                 }
3345 #ifdef TARGET_NR_umount
3346     case TARGET_NR_umount:
3347         if (!(p = lock_user_string(arg1)))
3348             goto efault;
3349         ret = get_errno(umount(p));
3350         unlock_user(p, arg1, 0);
3351         break;
3352 #endif
3353 #ifdef TARGET_NR_stime /* not on alpha */
3354     case TARGET_NR_stime:
3355         {
3356             time_t host_time;
3357             if (get_user_sal(host_time, arg1))
3358                 goto efault;
3359             ret = get_errno(stime(&host_time));
3360         }
3361         break;
3362 #endif
3363     case TARGET_NR_ptrace:
3364         goto unimplemented;
3365 #ifdef TARGET_NR_alarm /* not on alpha */
3366     case TARGET_NR_alarm:
3367         ret = alarm(arg1);
3368         break;
3369 #endif
3370 #ifdef TARGET_NR_oldfstat
3371     case TARGET_NR_oldfstat:
3372         goto unimplemented;
3373 #endif
3374 #ifdef TARGET_NR_pause /* not on alpha */
3375     case TARGET_NR_pause:
3376         ret = get_errno(pause());
3377         break;
3378 #endif
3379 #ifdef TARGET_NR_utime
3380     case TARGET_NR_utime:
3381         {
3382             struct utimbuf tbuf, *host_tbuf;
3383             struct target_utimbuf *target_tbuf;
3384             if (arg2) {
3385                 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
3386                     goto efault;
3387                 tbuf.actime = tswapl(target_tbuf->actime);
3388                 tbuf.modtime = tswapl(target_tbuf->modtime);
3389                 unlock_user_struct(target_tbuf, arg2, 0);
3390                 host_tbuf = &tbuf;
3391             } else {
3392                 host_tbuf = NULL;
3393             }
3394             if (!(p = lock_user_string(arg1)))
3395                 goto efault;
3396             ret = get_errno(utime(p, host_tbuf));
3397             unlock_user(p, arg1, 0);
3398         }
3399         break;
3400 #endif
3401     case TARGET_NR_utimes:
3402         {
3403             struct timeval *tvp, tv[2];
3404             if (arg2) {
3405                 if (copy_from_user_timeval(&tv[0], arg2)
3406                     || copy_from_user_timeval(&tv[1],
3407                                               arg2 + sizeof(struct target_timeval)))
3408                     goto efault;
3409                 tvp = tv;
3410             } else {
3411                 tvp = NULL;
3412             }
3413             if (!(p = lock_user_string(arg1)))
3414                 goto efault;
3415             ret = get_errno(utimes(p, tvp));
3416             unlock_user(p, arg1, 0);
3417         }
3418         break;
3419 #ifdef TARGET_NR_stty
3420     case TARGET_NR_stty:
3421         goto unimplemented;
3422 #endif
3423 #ifdef TARGET_NR_gtty
3424     case TARGET_NR_gtty:
3425         goto unimplemented;
3426 #endif
3427     case TARGET_NR_access:
3428         if (!(p = lock_user_string(arg1)))
3429             goto efault;
3430         ret = get_errno(access(p, arg2));
3431         unlock_user(p, arg1, 0);
3432         break;
3433 #if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
3434     case TARGET_NR_faccessat:
3435         if (!(p = lock_user_string(arg2)))
3436             goto efault;
3437         ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
3438         unlock_user(p, arg2, 0);
3439         break;
3440 #endif
3441 #ifdef TARGET_NR_nice /* not on alpha */
3442     case TARGET_NR_nice:
3443         ret = get_errno(nice(arg1));
3444         break;
3445 #endif
3446 #ifdef TARGET_NR_ftime
3447     case TARGET_NR_ftime:
3448         goto unimplemented;
3449 #endif
3450     case TARGET_NR_sync:
3451         sync();
3452         ret = 0;
3453         break;
3454     case TARGET_NR_kill:
3455         ret = get_errno(kill(arg1, arg2));
3456         break;
3457     case TARGET_NR_rename:
3458         {
3459             void *p2;
3460             p = lock_user_string(arg1);
3461             p2 = lock_user_string(arg2);
3462             if (!p || !p2)
3463                 ret = -TARGET_EFAULT;
3464             else
3465                 ret = get_errno(rename(p, p2));
3466             unlock_user(p2, arg2, 0);
3467             unlock_user(p, arg1, 0);
3468         }
3469         break;
3470 #if defined(TARGET_NR_renameat) && defined(__NR_renameat)
3471     case TARGET_NR_renameat:
3472         {
3473             void *p2;
3474             p  = lock_user_string(arg2);
3475             p2 = lock_user_string(arg4);
3476             if (!p || !p2)
3477                 ret = -TARGET_EFAULT;
3478             else
3479                 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
3480             unlock_user(p2, arg4, 0);
3481             unlock_user(p, arg2, 0);
3482         }
3483         break;
3484 #endif
3485     case TARGET_NR_mkdir:
3486         if (!(p = lock_user_string(arg1)))
3487             goto efault;
3488         ret = get_errno(mkdir(p, arg2));
3489         unlock_user(p, arg1, 0);
3490         break;
3491 #if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
3492     case TARGET_NR_mkdirat:
3493         if (!(p = lock_user_string(arg2)))
3494             goto efault;
3495         ret = get_errno(sys_mkdirat(arg1, p, arg3));
3496         unlock_user(p, arg2, 0);
3497         break;
3498 #endif
3499     case TARGET_NR_rmdir:
3500         if (!(p = lock_user_string(arg1)))
3501             goto efault;
3502         ret = get_errno(rmdir(p));
3503         unlock_user(p, arg1, 0);
3504         break;
3505     case TARGET_NR_dup:
3506         ret = get_errno(dup(arg1));
3507         break;
3508     case TARGET_NR_pipe:
3509         {
3510             int host_pipe[2];
3511             ret = get_errno(pipe(host_pipe));
3512             if (!is_error(ret)) {
3513 #if defined(TARGET_MIPS)
3514                 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
3515                 env->gpr[3][env->current_tc] = host_pipe[1];
3516                 ret = host_pipe[0];
3517 #else
3518                 if (put_user_s32(host_pipe[0], arg1)
3519                     || put_user_s32(host_pipe[1], arg1 + sizeof(host_pipe[0])))
3520                     goto efault;
3521 #endif
3522             }
3523         }
3524         break;
3525     case TARGET_NR_times:
3526         {
3527             struct target_tms *tmsp;
3528             struct tms tms;
3529             ret = get_errno(times(&tms));
3530             if (arg1) {
3531                 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
3532                 if (!tmsp)
3533                     goto efault;
3534                 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3535                 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3536                 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3537                 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
3538             }
3539             if (!is_error(ret))
3540                 ret = host_to_target_clock_t(ret);
3541         }
3542         break;
3543 #ifdef TARGET_NR_prof
3544     case TARGET_NR_prof:
3545         goto unimplemented;
3546 #endif
3547 #ifdef TARGET_NR_signal
3548     case TARGET_NR_signal:
3549         goto unimplemented;
3550 #endif
3551     case TARGET_NR_acct:
3552         if (!(p = lock_user_string(arg1)))
3553             goto efault;
3554         ret = get_errno(acct(path(p)));
3555         unlock_user(p, arg1, 0);
3556         break;
3557 #ifdef TARGET_NR_umount2 /* not on alpha */
3558     case TARGET_NR_umount2:
3559         if (!(p = lock_user_string(arg1)))
3560             goto efault;
3561         ret = get_errno(umount2(p, arg2));
3562         unlock_user(p, arg1, 0);
3563         break;
3564 #endif
3565 #ifdef TARGET_NR_lock
3566     case TARGET_NR_lock:
3567         goto unimplemented;
3568 #endif
3569     case TARGET_NR_ioctl:
3570         ret = do_ioctl(arg1, arg2, arg3);
3571         break;
3572     case TARGET_NR_fcntl:
3573         ret = do_fcntl(arg1, arg2, arg3);
3574         break;
3575 #ifdef TARGET_NR_mpx
3576     case TARGET_NR_mpx:
3577         goto unimplemented;
3578 #endif
3579     case TARGET_NR_setpgid:
3580         ret = get_errno(setpgid(arg1, arg2));
3581         break;
3582 #ifdef TARGET_NR_ulimit
3583     case TARGET_NR_ulimit:
3584         goto unimplemented;
3585 #endif
3586 #ifdef TARGET_NR_oldolduname
3587     case TARGET_NR_oldolduname:
3588         goto unimplemented;
3589 #endif
3590     case TARGET_NR_umask:
3591         ret = get_errno(umask(arg1));
3592         break;
3593     case TARGET_NR_chroot:
3594         if (!(p = lock_user_string(arg1)))
3595             goto efault;
3596         ret = get_errno(chroot(p));
3597         unlock_user(p, arg1, 0);
3598         break;
3599     case TARGET_NR_ustat:
3600         goto unimplemented;
3601     case TARGET_NR_dup2:
3602         ret = get_errno(dup2(arg1, arg2));
3603         break;
3604 #ifdef TARGET_NR_getppid /* not on alpha */
3605     case TARGET_NR_getppid:
3606         ret = get_errno(getppid());
3607         break;
3608 #endif
3609     case TARGET_NR_getpgrp:
3610         ret = get_errno(getpgrp());
3611         break;
3612     case TARGET_NR_setsid:
3613         ret = get_errno(setsid());
3614         break;
3615 #ifdef TARGET_NR_sigaction
3616     case TARGET_NR_sigaction:
3617         {
3618 #if !defined(TARGET_MIPS)
3619             struct target_old_sigaction *old_act;
3620             struct target_sigaction act, oact, *pact;
3621             if (arg2) {
3622                 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3623                     goto efault;
3624                 act._sa_handler = old_act->_sa_handler;
3625                 target_siginitset(&act.sa_mask, old_act->sa_mask);
3626                 act.sa_flags = old_act->sa_flags;
3627                 act.sa_restorer = old_act->sa_restorer;
3628                 unlock_user_struct(old_act, arg2, 0);
3629                 pact = &act;
3630             } else {
3631                 pact = NULL;
3632             }
3633             ret = get_errno(do_sigaction(arg1, pact, &oact));
3634             if (!is_error(ret) && arg3) {
3635                 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3636                     goto efault;
3637                 old_act->_sa_handler = oact._sa_handler;
3638                 old_act->sa_mask = oact.sa_mask.sig[0];
3639                 old_act->sa_flags = oact.sa_flags;
3640                 old_act->sa_restorer = oact.sa_restorer;
3641                 unlock_user_struct(old_act, arg3, 1);
3642             }
3643 #else
3644             struct target_sigaction act, oact, *pact, *old_act;
3645
3646             if (arg2) {
3647                 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3648                     goto efault;
3649                 act._sa_handler = old_act->_sa_handler;
3650                 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3651                 act.sa_flags = old_act->sa_flags;
3652                 unlock_user_struct(old_act, arg2, 0);
3653                 pact = &act;
3654             } else {
3655                 pact = NULL;
3656             }
3657
3658             ret = get_errno(do_sigaction(arg1, pact, &oact));
3659
3660             if (!is_error(ret) && arg3) {
3661                 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3662                     goto efault;
3663                 old_act->_sa_handler = oact._sa_handler;
3664                 old_act->sa_flags = oact.sa_flags;
3665                 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3666                 old_act->sa_mask.sig[1] = 0;
3667                 old_act->sa_mask.sig[2] = 0;
3668                 old_act->sa_mask.sig[3] = 0;
3669                 unlock_user_struct(old_act, arg3, 1);
3670             }
3671 #endif
3672         }
3673         break;
3674 #endif
3675     case TARGET_NR_rt_sigaction:
3676         {
3677             struct target_sigaction *act;
3678             struct target_sigaction *oact;
3679
3680             if (arg2) {
3681                 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
3682                     goto efault;
3683             } else
3684                 act = NULL;
3685             if (arg3) {
3686                 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
3687                     ret = -TARGET_EFAULT;
3688                     goto rt_sigaction_fail;
3689                 }
3690             } else
3691                 oact = NULL;
3692             ret = get_errno(do_sigaction(arg1, act, oact));
3693         rt_sigaction_fail:
3694             if (act)
3695                 unlock_user_struct(act, arg2, 0);
3696             if (oact)
3697                 unlock_user_struct(oact, arg3, 1);
3698         }
3699         break;
3700 #ifdef TARGET_NR_sgetmask /* not on alpha */
3701     case TARGET_NR_sgetmask:
3702         {
3703             sigset_t cur_set;
3704             abi_ulong target_set;
3705             sigprocmask(0, NULL, &cur_set);
3706             host_to_target_old_sigset(&target_set, &cur_set);
3707             ret = target_set;
3708         }
3709         break;
3710 #endif
3711 #ifdef TARGET_NR_ssetmask /* not on alpha */
3712     case TARGET_NR_ssetmask:
3713         {
3714             sigset_t set, oset, cur_set;
3715             abi_ulong target_set = arg1;
3716             sigprocmask(0, NULL, &cur_set);
3717             target_to_host_old_sigset(&set, &target_set);
3718             sigorset(&set, &set, &cur_set);
3719             sigprocmask(SIG_SETMASK, &set, &oset);
3720             host_to_target_old_sigset(&target_set, &oset);
3721             ret = target_set;
3722         }
3723         break;
3724 #endif
3725 #ifdef TARGET_NR_sigprocmask
3726     case TARGET_NR_sigprocmask:
3727         {
3728             int how = arg1;
3729             sigset_t set, oldset, *set_ptr;
3730
3731             if (arg2) {
3732                 switch(how) {
3733                 case TARGET_SIG_BLOCK:
3734                     how = SIG_BLOCK;
3735                     break;
3736                 case TARGET_SIG_UNBLOCK:
3737                     how = SIG_UNBLOCK;
3738                     break;
3739                 case TARGET_SIG_SETMASK:
3740                     how = SIG_SETMASK;
3741                     break;
3742                 default:
3743                     ret = -TARGET_EINVAL;
3744                     goto fail;
3745                 }
3746                 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3747                     goto efault;
3748                 target_to_host_old_sigset(&set, p);
3749                 unlock_user(p, arg2, 0);
3750                 set_ptr = &set;
3751             } else {
3752                 how = 0;
3753                 set_ptr = NULL;
3754             }
3755             ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
3756             if (!is_error(ret) && arg3) {
3757                 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3758                     goto efault;
3759                 host_to_target_old_sigset(p, &oldset);
3760                 unlock_user(p, arg3, sizeof(target_sigset_t));
3761             }
3762         }
3763         break;
3764 #endif
3765     case TARGET_NR_rt_sigprocmask:
3766         {
3767             int how = arg1;
3768             sigset_t set, oldset, *set_ptr;
3769
3770             if (arg2) {
3771                 switch(how) {
3772                 case TARGET_SIG_BLOCK:
3773                     how = SIG_BLOCK;
3774                     break;
3775                 case TARGET_SIG_UNBLOCK:
3776                     how = SIG_UNBLOCK;
3777                     break;
3778                 case TARGET_SIG_SETMASK:
3779                     how = SIG_SETMASK;
3780                     break;
3781                 default:
3782                     ret = -TARGET_EINVAL;
3783                     goto fail;
3784                 }
3785                 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3786                     goto efault;
3787                 target_to_host_sigset(&set, p);
3788                 unlock_user(p, arg2, 0);
3789                 set_ptr = &set;
3790             } else {
3791                 how = 0;
3792                 set_ptr = NULL;
3793             }
3794             ret = get_errno(sigprocmask(how, set_ptr, &oldset));
3795             if (!is_error(ret) && arg3) {
3796                 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3797                     goto efault;
3798                 host_to_target_sigset(p, &oldset);
3799                 unlock_user(p, arg3, sizeof(target_sigset_t));
3800             }
3801         }
3802         break;
3803 #ifdef TARGET_NR_sigpending
3804     case TARGET_NR_sigpending:
3805         {
3806             sigset_t set;
3807             ret = get_errno(sigpending(&set));
3808             if (!is_error(ret)) {
3809                 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3810                     goto efault;
3811                 host_to_target_old_sigset(p, &set);
3812                 unlock_user(p, arg1, sizeof(target_sigset_t));
3813             }
3814         }
3815         break;
3816 #endif
3817     case TARGET_NR_rt_sigpending:
3818         {
3819             sigset_t set;
3820             ret = get_errno(sigpending(&set));
3821             if (!is_error(ret)) {
3822                 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3823                     goto efault;
3824                 host_to_target_sigset(p, &set);
3825                 unlock_user(p, arg1, sizeof(target_sigset_t));
3826             }
3827         }
3828         break;
3829 #ifdef TARGET_NR_sigsuspend
3830     case TARGET_NR_sigsuspend:
3831         {
3832             sigset_t set;
3833             if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3834                 goto efault;
3835             target_to_host_old_sigset(&set, p);
3836             unlock_user(p, arg1, 0);
3837             ret = get_errno(sigsuspend(&set));
3838         }
3839         break;
3840 #endif
3841     case TARGET_NR_rt_sigsuspend:
3842         {
3843             sigset_t set;
3844             if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3845                 goto efault;
3846             target_to_host_sigset(&set, p);
3847             unlock_user(p, arg1, 0);
3848             ret = get_errno(sigsuspend(&set));
3849         }
3850         break;
3851     case TARGET_NR_rt_sigtimedwait:
3852         {
3853             sigset_t set;
3854             struct timespec uts, *puts;
3855             siginfo_t uinfo;
3856
3857             if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3858                 goto efault;
3859             target_to_host_sigset(&set, p);
3860             unlock_user(p, arg1, 0);
3861             if (arg3) {
3862                 puts = &uts;
3863                 target_to_host_timespec(puts, arg3);
3864             } else {
3865                 puts = NULL;
3866             }
3867             ret = get_errno(sigtimedwait(&set, &uinfo, puts));
3868             if (!is_error(ret) && arg2) {
3869                 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_sigset_t), 0)))
3870                     goto efault;
3871                 host_to_target_siginfo(p, &uinfo);
3872                 unlock_user(p, arg2, sizeof(target_sigset_t));
3873             }
3874         }
3875         break;
3876     case TARGET_NR_rt_sigqueueinfo:
3877         {
3878             siginfo_t uinfo;
3879             if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
3880                 goto efault;
3881             target_to_host_siginfo(&uinfo, p);
3882             unlock_user(p, arg1, 0);
3883             ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3884         }
3885         break;
3886 #ifdef TARGET_NR_sigreturn
3887     case TARGET_NR_sigreturn:
3888         /* NOTE: ret is eax, so not transcoding must be done */
3889         ret = do_sigreturn(cpu_env);
3890         break;
3891 #endif
3892     case TARGET_NR_rt_sigreturn:
3893         /* NOTE: ret is eax, so not transcoding must be done */
3894         ret = do_rt_sigreturn(cpu_env);
3895         break;
3896     case TARGET_NR_sethostname:
3897         if (!(p = lock_user_string(arg1)))
3898             goto efault;
3899         ret = get_errno(sethostname(p, arg2));
3900         unlock_user(p, arg1, 0);
3901         break;
3902     case TARGET_NR_setrlimit:
3903         {
3904             /* XXX: convert resource ? */
3905             int resource = arg1;
3906             struct target_rlimit *target_rlim;
3907             struct rlimit rlim;
3908             if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
3909                 goto efault;
3910             rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3911             rlim.rlim_max = tswapl(target_rlim->rlim_max);
3912             unlock_user_struct(target_rlim, arg2, 0);
3913             ret = get_errno(setrlimit(resource, &rlim));
3914         }
3915         break;
3916     case TARGET_NR_getrlimit:
3917         {
3918             /* XXX: convert resource ? */
3919             int resource = arg1;
3920             struct target_rlimit *target_rlim;
3921             struct rlimit rlim;
3922
3923             ret = get_errno(getrlimit(resource, &rlim));
3924             if (!is_error(ret)) {
3925                 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
3926                     goto efault;
3927                 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3928                 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3929                 unlock_user_struct(target_rlim, arg2, 1);
3930             }
3931         }
3932         break;
3933     case TARGET_NR_getrusage:
3934         {
3935             struct rusage rusage;
3936             ret = get_errno(getrusage(arg1, &rusage));
3937             if (!is_error(ret)) {
3938                 host_to_target_rusage(arg2, &rusage);
3939             }
3940         }
3941         break;
3942     case TARGET_NR_gettimeofday:
3943         {
3944             struct timeval tv;
3945             ret = get_errno(gettimeofday(&tv, NULL));
3946             if (!is_error(ret)) {
3947                 if (copy_to_user_timeval(arg1, &tv))
3948                     goto efault;
3949             }
3950         }
3951         break;
3952     case TARGET_NR_settimeofday:
3953         {
3954             struct timeval tv;
3955             if (copy_from_user_timeval(&tv, arg1))
3956                 goto efault;
3957             ret = get_errno(settimeofday(&tv, NULL));
3958         }
3959         break;
3960 #ifdef TARGET_NR_select
3961     case TARGET_NR_select:
3962         {
3963             struct target_sel_arg_struct *sel;
3964             abi_ulong inp, outp, exp, tvp;
3965             long nsel;
3966
3967             if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
3968                 goto efault;
3969             nsel = tswapl(sel->n);
3970             inp = tswapl(sel->inp);
3971             outp = tswapl(sel->outp);
3972             exp = tswapl(sel->exp);
3973             tvp = tswapl(sel->tvp);
3974             unlock_user_struct(sel, arg1, 0);
3975             ret = do_select(nsel, inp, outp, exp, tvp);
3976         }
3977         break;
3978 #endif
3979     case TARGET_NR_symlink:
3980         {
3981             void *p2;
3982             p = lock_user_string(arg1);
3983             p2 = lock_user_string(arg2);
3984             if (!p || !p2)
3985                 ret = -TARGET_EFAULT;
3986             else
3987                 ret = get_errno(symlink(p, p2));
3988             unlock_user(p2, arg2, 0);
3989             unlock_user(p, arg1, 0);
3990         }
3991         break;
3992 #if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
3993     case TARGET_NR_symlinkat:
3994         {
3995             void *p2;
3996             p  = lock_user_string(arg1);
3997             p2 = lock_user_string(arg3);
3998             if (!p || !p2)
3999                 ret = -TARGET_EFAULT;
4000             else
4001                 ret = get_errno(sys_symlinkat(p, arg2, p2));
4002             unlock_user(p2, arg3, 0);
4003             unlock_user(p, arg1, 0);
4004         }
4005         break;
4006 #endif
4007 #ifdef TARGET_NR_oldlstat
4008     case TARGET_NR_oldlstat:
4009         goto unimplemented;
4010 #endif
4011     case TARGET_NR_readlink:
4012         {
4013             void *p2;
4014             p = lock_user_string(arg1);
4015             p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
4016             if (!p || !p2)
4017                 ret = -TARGET_EFAULT;
4018             else
4019                 ret = get_errno(readlink(path(p), p2, arg3));
4020             unlock_user(p2, arg2, ret);
4021             unlock_user(p, arg1, 0);
4022         }
4023         break;
4024 #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
4025     case TARGET_NR_readlinkat:
4026         {
4027             void *p2;
4028             p  = lock_user_string(arg2);
4029             p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
4030             if (!p || !p2)
4031                 ret = -TARGET_EFAULT;
4032             else
4033                 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
4034             unlock_user(p2, arg3, ret);
4035             unlock_user(p, arg2, 0);
4036         }
4037         break;
4038 #endif
4039 #ifdef TARGET_NR_uselib
4040     case TARGET_NR_uselib:
4041         goto unimplemented;
4042 #endif
4043 #ifdef TARGET_NR_swapon
4044     case TARGET_NR_swapon:
4045         if (!(p = lock_user_string(arg1)))
4046             goto efault;
4047         ret = get_errno(swapon(p, arg2));
4048         unlock_user(p, arg1, 0);
4049         break;
4050 #endif
4051     case TARGET_NR_reboot:
4052         goto unimplemented;
4053 #ifdef TARGET_NR_readdir
4054     case TARGET_NR_readdir:
4055         goto unimplemented;
4056 #endif
4057 #ifdef TARGET_NR_mmap
4058     case TARGET_NR_mmap:
4059 #if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
4060         {
4061             abi_ulong *v;
4062             abi_ulong v1, v2, v3, v4, v5, v6;
4063             if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
4064                 goto efault;
4065             v1 = tswapl(v[0]);
4066             v2 = tswapl(v[1]);
4067             v3 = tswapl(v[2]);
4068             v4 = tswapl(v[3]);
4069             v5 = tswapl(v[4]);
4070             v6 = tswapl(v[5]);
4071             unlock_user(v, arg1, 0);
4072             ret = get_errno(target_mmap(v1, v2, v3,
4073                                         target_to_host_bitmask(v4, mmap_flags_tbl),
4074                                         v5, v6));
4075         }
4076 #else
4077         ret = get_errno(target_mmap(arg1, arg2, arg3,
4078                                     target_to_host_bitmask(arg4, mmap_flags_tbl),
4079                                     arg5,
4080                                     arg6));
4081 #endif
4082         break;
4083 #endif
4084 #ifdef TARGET_NR_mmap2
4085     case TARGET_NR_mmap2:
4086 #if defined(TARGET_SPARC) || defined(TARGET_MIPS)
4087 #define MMAP_SHIFT 12
4088 #else
4089 #define MMAP_SHIFT TARGET_PAGE_BITS
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     case TARGET_NR_getcwd:
4878         if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
4879             goto efault;
4880         ret = get_errno(sys_getcwd1(p, arg2));
4881         unlock_user(p, arg1, ret);
4882         break;
4883     case TARGET_NR_capget:
4884         goto unimplemented;
4885     case TARGET_NR_capset:
4886         goto unimplemented;
4887     case TARGET_NR_sigaltstack:
4888 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
4889     defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
4890         ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
4891         break;
4892 #else
4893         goto unimplemented;
4894 #endif
4895     case TARGET_NR_sendfile:
4896         goto unimplemented;
4897 #ifdef TARGET_NR_getpmsg
4898     case TARGET_NR_getpmsg:
4899         goto unimplemented;
4900 #endif
4901 #ifdef TARGET_NR_putpmsg
4902     case TARGET_NR_putpmsg:
4903         goto unimplemented;
4904 #endif
4905 #ifdef TARGET_NR_vfork
4906     case TARGET_NR_vfork:
4907         ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
4908         break;
4909 #endif
4910 #ifdef TARGET_NR_ugetrlimit
4911     case TARGET_NR_ugetrlimit:
4912     {
4913         struct rlimit rlim;
4914         ret = get_errno(getrlimit(arg1, &rlim));
4915         if (!is_error(ret)) {
4916             struct target_rlimit *target_rlim;
4917             if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
4918                 goto efault;
4919             target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
4920             target_rlim->rlim_max = tswapl(rlim.rlim_max);
4921             unlock_user_struct(target_rlim, arg2, 1);
4922         }
4923         break;
4924     }
4925 #endif
4926 #ifdef TARGET_NR_truncate64
4927     case TARGET_NR_truncate64:
4928         if (!(p = lock_user_string(arg1)))
4929             goto efault;
4930         ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
4931         unlock_user(p, arg1, 0);
4932         break;
4933 #endif
4934 #ifdef TARGET_NR_ftruncate64
4935     case TARGET_NR_ftruncate64:
4936         ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
4937         break;
4938 #endif
4939 #ifdef TARGET_NR_stat64
4940     case TARGET_NR_stat64:
4941         if (!(p = lock_user_string(arg1)))
4942             goto efault;
4943         ret = get_errno(stat(path(p), &st));
4944         unlock_user(p, arg1, 0);
4945         goto do_stat64;
4946 #endif
4947 #ifdef TARGET_NR_lstat64
4948     case TARGET_NR_lstat64:
4949         if (!(p = lock_user_string(arg1)))
4950             goto efault;
4951         ret = get_errno(lstat(path(p), &st));
4952         unlock_user(p, arg1, 0);
4953         goto do_stat64;
4954 #endif
4955 #ifdef TARGET_NR_fstat64
4956     case TARGET_NR_fstat64:
4957         {
4958             ret = get_errno(fstat(arg1, &st));
4959         do_stat64:
4960             if (!is_error(ret)) {
4961 #ifdef TARGET_ARM
4962                 if (((CPUARMState *)cpu_env)->eabi) {
4963                     struct target_eabi_stat64 *target_st;
4964
4965                     if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4966                         goto efault;
4967                     memset(target_st, 0, sizeof(struct target_eabi_stat64));
4968                     __put_user(st.st_dev, &target_st->st_dev);
4969                     __put_user(st.st_ino, &target_st->st_ino);
4970 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4971                     __put_user(st.st_ino, &target_st->__st_ino);
4972 #endif
4973                     __put_user(st.st_mode, &target_st->st_mode);
4974                     __put_user(st.st_nlink, &target_st->st_nlink);
4975                     __put_user(st.st_uid, &target_st->st_uid);
4976                     __put_user(st.st_gid, &target_st->st_gid);
4977                     __put_user(st.st_rdev, &target_st->st_rdev);
4978                     __put_user(st.st_size, &target_st->st_size);
4979                     __put_user(st.st_blksize, &target_st->st_blksize);
4980                     __put_user(st.st_blocks, &target_st->st_blocks);
4981                     __put_user(st.st_atime, &target_st->target_st_atime);
4982                     __put_user(st.st_mtime, &target_st->target_st_mtime);
4983                     __put_user(st.st_ctime, &target_st->target_st_ctime);
4984                     unlock_user_struct(target_st, arg2, 1);
4985                 } else
4986 #endif
4987                 {
4988                     struct target_stat64 *target_st;
4989
4990                     if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4991                         goto efault;
4992                     memset(target_st, 0, sizeof(struct target_stat64));
4993                     __put_user(st.st_dev, &target_st->st_dev);
4994                     __put_user(st.st_ino, &target_st->st_ino);
4995 #ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4996                     __put_user(st.st_ino, &target_st->__st_ino);
4997 #endif
4998                     __put_user(st.st_mode, &target_st->st_mode);
4999                     __put_user(st.st_nlink, &target_st->st_nlink);
5000                     __put_user(st.st_uid, &target_st->st_uid);
5001                     __put_user(st.st_gid, &target_st->st_gid);
5002                     __put_user(st.st_rdev, &target_st->st_rdev);
5003                     /* XXX: better use of kernel struct */
5004                     __put_user(st.st_size, &target_st->st_size);
5005                     __put_user(st.st_blksize, &target_st->st_blksize);
5006                     __put_user(st.st_blocks, &target_st->st_blocks);
5007                     __put_user(st.st_atime, &target_st->target_st_atime);
5008                     __put_user(st.st_mtime, &target_st->target_st_mtime);
5009                     __put_user(st.st_ctime, &target_st->target_st_ctime);
5010                     unlock_user_struct(target_st, arg2, 1);
5011                 }
5012             }
5013         }
5014         break;
5015 #endif
5016 #ifdef USE_UID16
5017     case TARGET_NR_lchown:
5018         if (!(p = lock_user_string(arg1)))
5019             goto efault;
5020         ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
5021         unlock_user(p, arg1, 0);
5022         break;
5023     case TARGET_NR_getuid:
5024         ret = get_errno(high2lowuid(getuid()));
5025         break;
5026     case TARGET_NR_getgid:
5027         ret = get_errno(high2lowgid(getgid()));
5028         break;
5029     case TARGET_NR_geteuid:
5030         ret = get_errno(high2lowuid(geteuid()));
5031         break;
5032     case TARGET_NR_getegid:
5033         ret = get_errno(high2lowgid(getegid()));
5034         break;
5035     case TARGET_NR_setreuid:
5036         ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
5037         break;
5038     case TARGET_NR_setregid:
5039         ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
5040         break;
5041     case TARGET_NR_getgroups:
5042         {
5043             int gidsetsize = arg1;
5044             uint16_t *target_grouplist;
5045             gid_t *grouplist;
5046             int i;
5047
5048             grouplist = alloca(gidsetsize * sizeof(gid_t));
5049             ret = get_errno(getgroups(gidsetsize, grouplist));
5050             if (!is_error(ret)) {
5051                 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
5052                 if (!target_grouplist)
5053                     goto efault;
5054                 for(i = 0;i < gidsetsize; i++)
5055                     target_grouplist[i] = tswap16(grouplist[i]);
5056                 unlock_user(target_grouplist, arg2, gidsetsize * 2);
5057             }
5058         }
5059         break;
5060     case TARGET_NR_setgroups:
5061         {
5062             int gidsetsize = arg1;
5063             uint16_t *target_grouplist;
5064             gid_t *grouplist;
5065             int i;
5066
5067             grouplist = alloca(gidsetsize * sizeof(gid_t));
5068             target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
5069             if (!target_grouplist) {
5070                 ret = -TARGET_EFAULT;
5071                 goto fail;
5072             }
5073             for(i = 0;i < gidsetsize; i++)
5074                 grouplist[i] = tswap16(target_grouplist[i]);
5075             unlock_user(target_grouplist, arg2, 0);
5076             ret = get_errno(setgroups(gidsetsize, grouplist));
5077         }
5078         break;
5079     case TARGET_NR_fchown:
5080         ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
5081         break;
5082 #if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
5083     case TARGET_NR_fchownat:
5084         if (!(p = lock_user_string(arg2))) 
5085             goto efault;
5086         ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
5087         unlock_user(p, arg2, 0);
5088         break;
5089 #endif
5090 #ifdef TARGET_NR_setresuid
5091     case TARGET_NR_setresuid:
5092         ret = get_errno(setresuid(low2highuid(arg1),
5093                                   low2highuid(arg2),
5094                                   low2highuid(arg3)));
5095         break;
5096 #endif
5097 #ifdef TARGET_NR_getresuid
5098     case TARGET_NR_getresuid:
5099         {
5100             uid_t ruid, euid, suid;
5101             ret = get_errno(getresuid(&ruid, &euid, &suid));
5102             if (!is_error(ret)) {
5103                 if (put_user_u16(high2lowuid(ruid), arg1)
5104                     || put_user_u16(high2lowuid(euid), arg2)
5105                     || put_user_u16(high2lowuid(suid), arg3))
5106                     goto efault;
5107             }
5108         }
5109         break;
5110 #endif
5111 #ifdef TARGET_NR_getresgid
5112     case TARGET_NR_setresgid:
5113         ret = get_errno(setresgid(low2highgid(arg1),
5114                                   low2highgid(arg2),
5115                                   low2highgid(arg3)));
5116         break;
5117 #endif
5118 #ifdef TARGET_NR_getresgid
5119     case TARGET_NR_getresgid:
5120         {
5121             gid_t rgid, egid, sgid;
5122             ret = get_errno(getresgid(&rgid, &egid, &sgid));
5123             if (!is_error(ret)) {
5124                 if (put_user_u16(high2lowgid(rgid), arg1)
5125                     || put_user_u16(high2lowgid(egid), arg2)
5126                     || put_user_u16(high2lowgid(sgid), arg3))
5127                     goto efault;
5128             }
5129         }
5130         break;
5131 #endif
5132     case TARGET_NR_chown:
5133         if (!(p = lock_user_string(arg1)))
5134             goto efault;
5135         ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
5136         unlock_user(p, arg1, 0);
5137         break;
5138     case TARGET_NR_setuid:
5139         ret = get_errno(setuid(low2highuid(arg1)));
5140         break;
5141     case TARGET_NR_setgid:
5142         ret = get_errno(setgid(low2highgid(arg1)));
5143         break;
5144     case TARGET_NR_setfsuid:
5145         ret = get_errno(setfsuid(arg1));
5146         break;
5147     case TARGET_NR_setfsgid:
5148         ret = get_errno(setfsgid(arg1));
5149         break;
5150 #endif /* USE_UID16 */
5151
5152 #ifdef TARGET_NR_lchown32
5153     case TARGET_NR_lchown32:
5154         if (!(p = lock_user_string(arg1)))
5155             goto efault;
5156         ret = get_errno(lchown(p, arg2, arg3));
5157         unlock_user(p, arg1, 0);
5158         break;
5159 #endif
5160 #ifdef TARGET_NR_getuid32
5161     case TARGET_NR_getuid32:
5162         ret = get_errno(getuid());
5163         break;
5164 #endif
5165 #ifdef TARGET_NR_getgid32
5166     case TARGET_NR_getgid32:
5167         ret = get_errno(getgid());
5168         break;
5169 #endif
5170 #ifdef TARGET_NR_geteuid32
5171     case TARGET_NR_geteuid32:
5172         ret = get_errno(geteuid());
5173         break;
5174 #endif
5175 #ifdef TARGET_NR_getegid32
5176     case TARGET_NR_getegid32:
5177         ret = get_errno(getegid());
5178         break;
5179 #endif
5180 #ifdef TARGET_NR_setreuid32
5181     case TARGET_NR_setreuid32:
5182         ret = get_errno(setreuid(arg1, arg2));
5183         break;
5184 #endif
5185 #ifdef TARGET_NR_setregid32
5186     case TARGET_NR_setregid32:
5187         ret = get_errno(setregid(arg1, arg2));
5188         break;
5189 #endif
5190 #ifdef TARGET_NR_getgroups32
5191     case TARGET_NR_getgroups32:
5192         {
5193             int gidsetsize = arg1;
5194             uint32_t *target_grouplist;
5195             gid_t *grouplist;
5196             int i;
5197
5198             grouplist = alloca(gidsetsize * sizeof(gid_t));
5199             ret = get_errno(getgroups(gidsetsize, grouplist));
5200             if (!is_error(ret)) {
5201                 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
5202                 if (!target_grouplist) {
5203                     ret = -TARGET_EFAULT;
5204                     goto fail;
5205                 }
5206                 for(i = 0;i < gidsetsize; i++)
5207                     target_grouplist[i] = tswap32(grouplist[i]);
5208                 unlock_user(target_grouplist, arg2, gidsetsize * 4);
5209             }
5210         }
5211         break;
5212 #endif
5213 #ifdef TARGET_NR_setgroups32
5214     case TARGET_NR_setgroups32:
5215         {
5216             int gidsetsize = arg1;
5217             uint32_t *target_grouplist;
5218             gid_t *grouplist;
5219             int i;
5220
5221             grouplist = alloca(gidsetsize * sizeof(gid_t));
5222             target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
5223             if (!target_grouplist) {
5224                 ret = -TARGET_EFAULT;
5225                 goto fail;
5226             }
5227             for(i = 0;i < gidsetsize; i++)
5228                 grouplist[i] = tswap32(target_grouplist[i]);
5229             unlock_user(target_grouplist, arg2, 0);
5230             ret = get_errno(setgroups(gidsetsize, grouplist));
5231         }
5232         break;
5233 #endif
5234 #ifdef TARGET_NR_fchown32
5235     case TARGET_NR_fchown32:
5236         ret = get_errno(fchown(arg1, arg2, arg3));
5237         break;
5238 #endif
5239 #ifdef TARGET_NR_setresuid32
5240     case TARGET_NR_setresuid32:
5241         ret = get_errno(setresuid(arg1, arg2, arg3));
5242         break;
5243 #endif
5244 #ifdef TARGET_NR_getresuid32
5245     case TARGET_NR_getresuid32:
5246         {
5247             uid_t ruid, euid, suid;
5248             ret = get_errno(getresuid(&ruid, &euid, &suid));
5249             if (!is_error(ret)) {
5250                 if (put_user_u32(ruid, arg1)
5251                     || put_user_u32(euid, arg2)
5252                     || put_user_u32(suid, arg3))
5253                     goto efault;
5254             }
5255         }
5256         break;
5257 #endif
5258 #ifdef TARGET_NR_setresgid32
5259     case TARGET_NR_setresgid32:
5260         ret = get_errno(setresgid(arg1, arg2, arg3));
5261         break;
5262 #endif
5263 #ifdef TARGET_NR_getresgid32
5264     case TARGET_NR_getresgid32:
5265         {
5266             gid_t rgid, egid, sgid;
5267             ret = get_errno(getresgid(&rgid, &egid, &sgid));
5268             if (!is_error(ret)) {
5269                 if (put_user_u32(rgid, arg1)
5270                     || put_user_u32(egid, arg2)
5271                     || put_user_u32(sgid, arg3))
5272                     goto efault;
5273             }
5274         }
5275         break;
5276 #endif
5277 #ifdef TARGET_NR_chown32
5278     case TARGET_NR_chown32:
5279         if (!(p = lock_user_string(arg1)))
5280             goto efault;
5281         ret = get_errno(chown(p, arg2, arg3));
5282         unlock_user(p, arg1, 0);
5283         break;
5284 #endif
5285 #ifdef TARGET_NR_setuid32
5286     case TARGET_NR_setuid32:
5287         ret = get_errno(setuid(arg1));
5288         break;
5289 #endif
5290 #ifdef TARGET_NR_setgid32
5291     case TARGET_NR_setgid32:
5292         ret = get_errno(setgid(arg1));
5293         break;
5294 #endif
5295 #ifdef TARGET_NR_setfsuid32
5296     case TARGET_NR_setfsuid32:
5297         ret = get_errno(setfsuid(arg1));
5298         break;
5299 #endif
5300 #ifdef TARGET_NR_setfsgid32
5301     case TARGET_NR_setfsgid32:
5302         ret = get_errno(setfsgid(arg1));
5303         break;
5304 #endif
5305
5306     case TARGET_NR_pivot_root:
5307         goto unimplemented;
5308 #ifdef TARGET_NR_mincore
5309     case TARGET_NR_mincore:
5310         goto unimplemented;
5311 #endif
5312 #ifdef TARGET_NR_madvise
5313     case TARGET_NR_madvise:
5314         /* A straight passthrough may not be safe because qemu sometimes
5315            turns private flie-backed mappings into anonymous mappings.
5316            This will break MADV_DONTNEED.
5317            This is a hint, so ignoring and returning success is ok.  */
5318         ret = get_errno(0);
5319         break;
5320 #endif
5321 #if TARGET_ABI_BITS == 32
5322     case TARGET_NR_fcntl64:
5323     {
5324         int cmd;
5325         struct flock64 fl;
5326         struct target_flock64 *target_fl;
5327 #ifdef TARGET_ARM
5328         struct target_eabi_flock64 *target_efl;
5329 #endif
5330
5331         switch(arg2){
5332         case TARGET_F_GETLK64:
5333             cmd = F_GETLK64;
5334             break;
5335         case TARGET_F_SETLK64:
5336             cmd = F_SETLK64;
5337             break;
5338         case TARGET_F_SETLKW64:
5339             cmd = F_SETLK64;
5340             break;
5341         default:
5342             cmd = arg2;
5343             break;
5344         }
5345
5346         switch(arg2) {
5347         case TARGET_F_GETLK64:
5348 #ifdef TARGET_ARM
5349             if (((CPUARMState *)cpu_env)->eabi) {
5350                 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1)) 
5351                     goto efault;
5352                 fl.l_type = tswap16(target_efl->l_type);
5353                 fl.l_whence = tswap16(target_efl->l_whence);
5354                 fl.l_start = tswap64(target_efl->l_start);
5355                 fl.l_len = tswap64(target_efl->l_len);
5356                 fl.l_pid = tswapl(target_efl->l_pid);
5357                 unlock_user_struct(target_efl, arg3, 0);
5358             } else
5359 #endif
5360             {
5361                 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1)) 
5362                     goto efault;
5363                 fl.l_type = tswap16(target_fl->l_type);
5364                 fl.l_whence = tswap16(target_fl->l_whence);
5365                 fl.l_start = tswap64(target_fl->l_start);
5366                 fl.l_len = tswap64(target_fl->l_len);
5367                 fl.l_pid = tswapl(target_fl->l_pid);
5368                 unlock_user_struct(target_fl, arg3, 0);
5369             }
5370             ret = get_errno(fcntl(arg1, cmd, &fl));
5371             if (ret == 0) {
5372 #ifdef TARGET_ARM
5373                 if (((CPUARMState *)cpu_env)->eabi) {
5374                     if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0)) 
5375                         goto efault;
5376                     target_efl->l_type = tswap16(fl.l_type);
5377                     target_efl->l_whence = tswap16(fl.l_whence);
5378                     target_efl->l_start = tswap64(fl.l_start);
5379                     target_efl->l_len = tswap64(fl.l_len);
5380                     target_efl->l_pid = tswapl(fl.l_pid);
5381                     unlock_user_struct(target_efl, arg3, 1);
5382                 } else
5383 #endif
5384                 {
5385                     if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0)) 
5386                         goto efault;
5387                     target_fl->l_type = tswap16(fl.l_type);
5388                     target_fl->l_whence = tswap16(fl.l_whence);
5389                     target_fl->l_start = tswap64(fl.l_start);
5390                     target_fl->l_len = tswap64(fl.l_len);
5391                     target_fl->l_pid = tswapl(fl.l_pid);
5392                     unlock_user_struct(target_fl, arg3, 1);
5393                 }
5394             }
5395             break;
5396
5397         case TARGET_F_SETLK64:
5398         case TARGET_F_SETLKW64:
5399 #ifdef TARGET_ARM
5400             if (((CPUARMState *)cpu_env)->eabi) {
5401                 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1)) 
5402                     goto efault;
5403                 fl.l_type = tswap16(target_efl->l_type);
5404                 fl.l_whence = tswap16(target_efl->l_whence);
5405                 fl.l_start = tswap64(target_efl->l_start);
5406                 fl.l_len = tswap64(target_efl->l_len);
5407                 fl.l_pid = tswapl(target_efl->l_pid);
5408                 unlock_user_struct(target_efl, arg3, 0);
5409             } else
5410 #endif
5411             {
5412                 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1)) 
5413                     goto efault;
5414                 fl.l_type = tswap16(target_fl->l_type);
5415                 fl.l_whence = tswap16(target_fl->l_whence);
5416                 fl.l_start = tswap64(target_fl->l_start);
5417                 fl.l_len = tswap64(target_fl->l_len);
5418                 fl.l_pid = tswapl(target_fl->l_pid);
5419                 unlock_user_struct(target_fl, arg3, 0);
5420             }
5421             ret = get_errno(fcntl(arg1, cmd, &fl));
5422             break;
5423         default:
5424             ret = do_fcntl(arg1, cmd, arg3);
5425             break;
5426         }
5427         break;
5428     }
5429 #endif
5430 #ifdef TARGET_NR_cacheflush
5431     case TARGET_NR_cacheflush:
5432         /* self-modifying code is handled automatically, so nothing needed */
5433         ret = 0;
5434         break;
5435 #endif
5436 #ifdef TARGET_NR_security
5437     case TARGET_NR_security:
5438         goto unimplemented;
5439 #endif
5440 #ifdef TARGET_NR_getpagesize
5441     case TARGET_NR_getpagesize:
5442         ret = TARGET_PAGE_SIZE;
5443         break;
5444 #endif
5445     case TARGET_NR_gettid:
5446         ret = get_errno(gettid());
5447         break;
5448 #ifdef TARGET_NR_readahead
5449     case TARGET_NR_readahead:
5450         goto unimplemented;
5451 #endif
5452 #ifdef TARGET_NR_setxattr
5453     case TARGET_NR_setxattr:
5454     case TARGET_NR_lsetxattr:
5455     case TARGET_NR_fsetxattr:
5456     case TARGET_NR_getxattr:
5457     case TARGET_NR_lgetxattr:
5458     case TARGET_NR_fgetxattr:
5459     case TARGET_NR_listxattr:
5460     case TARGET_NR_llistxattr:
5461     case TARGET_NR_flistxattr:
5462     case TARGET_NR_removexattr:
5463     case TARGET_NR_lremovexattr:
5464     case TARGET_NR_fremovexattr:
5465         goto unimplemented_nowarn;
5466 #endif
5467 #ifdef TARGET_NR_set_thread_area
5468     case TARGET_NR_set_thread_area:
5469 #if defined(TARGET_MIPS)
5470       ((CPUMIPSState *) cpu_env)->tls_value = arg1;
5471       ret = 0;
5472       break;
5473 #elif defined(TARGET_I386) && defined(TARGET_ABI32)
5474       ret = do_set_thread_area(cpu_env, arg1);
5475       break;
5476 #else
5477       goto unimplemented_nowarn;
5478 #endif
5479 #endif
5480 #ifdef TARGET_NR_get_thread_area
5481     case TARGET_NR_get_thread_area:
5482 #if defined(TARGET_I386) && defined(TARGET_ABI32)
5483         ret = do_get_thread_area(cpu_env, arg1);
5484 #else
5485         goto unimplemented_nowarn;
5486 #endif
5487 #endif
5488 #ifdef TARGET_NR_getdomainname
5489     case TARGET_NR_getdomainname:
5490         goto unimplemented_nowarn;
5491 #endif
5492
5493 #ifdef TARGET_NR_clock_gettime
5494     case TARGET_NR_clock_gettime:
5495     {
5496         struct timespec ts;
5497         ret = get_errno(clock_gettime(arg1, &ts));
5498         if (!is_error(ret)) {
5499             host_to_target_timespec(arg2, &ts);
5500         }
5501         break;
5502     }
5503 #endif
5504 #ifdef TARGET_NR_clock_getres
5505     case TARGET_NR_clock_getres:
5506     {
5507         struct timespec ts;
5508         ret = get_errno(clock_getres(arg1, &ts));
5509         if (!is_error(ret)) {
5510             host_to_target_timespec(arg2, &ts);
5511         }
5512         break;
5513     }
5514 #endif
5515
5516 #if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
5517     case TARGET_NR_set_tid_address:
5518         ret = get_errno(set_tid_address((int *)g2h(arg1)));
5519         break;
5520 #endif
5521
5522 #if defined(TARGET_NR_tkill) && defined(__NR_tkill)
5523     case TARGET_NR_tkill:
5524         ret = get_errno(sys_tkill((int)arg1, (int)arg2));
5525         break;
5526 #endif
5527
5528 #if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
5529     case TARGET_NR_tgkill:
5530         ret = get_errno(sys_tgkill((int)arg1, (int)arg2, (int)arg3));
5531         break;
5532 #endif
5533
5534 #ifdef TARGET_NR_set_robust_list
5535     case TARGET_NR_set_robust_list:
5536         goto unimplemented_nowarn;
5537 #endif
5538
5539 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
5540     case TARGET_NR_utimensat:
5541         {
5542             struct timespec ts[2];
5543             target_to_host_timespec(ts, arg3);
5544             target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
5545             if (!arg2)
5546                 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
5547             else {
5548                 if (!(p = lock_user_string(arg2))) {
5549                     ret = -TARGET_EFAULT;
5550                     goto fail;
5551                 }
5552                 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
5553                 unlock_user(p, arg2, 0);
5554             }
5555         }
5556         break;
5557 #endif
5558
5559     default:
5560     unimplemented:
5561         gemu_log("qemu: Unsupported syscall: %d\n", num);
5562 #if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
5563     unimplemented_nowarn:
5564 #endif
5565         ret = -TARGET_ENOSYS;
5566         break;
5567     }
5568 fail:
5569 #ifdef DEBUG
5570     gemu_log(" = %ld\n", ret);
5571 #endif
5572     if(do_strace)
5573         print_syscall_ret(num, ret);
5574     return ret;
5575 efault:
5576     ret = -TARGET_EFAULT;
5577     goto fail;
5578 }