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