Statistics
| Branch: | Revision:

root / linux-user / syscall.c @ 0da46a6e

History | View | Annotate | Download (155 kB)

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