Statistics
| Branch: | Revision:

root / linux-user / syscall.c @ a062e36c

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