blob: 6d1997e09a7732203190b5f1ba297ef2c56b9c15 [file] [log] [blame]
bellard31e31b82003-02-18 22:55:36 +00001/*
2 * Linux syscalls
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard31e31b82003-02-18 22:55:36 +00004 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20#include <stdlib.h>
21#include <stdio.h>
22#include <stdarg.h>
bellard04369ff2003-03-20 22:33:23 +000023#include <string.h>
bellard31e31b82003-02-18 22:55:36 +000024#include <elf.h>
25#include <endian.h>
26#include <errno.h>
27#include <unistd.h>
28#include <fcntl.h>
bellard7854b052003-03-29 17:22:23 +000029#include <time.h>
bellard31e31b82003-02-18 22:55:36 +000030#include <sys/types.h>
thsd08d3bb2007-03-19 13:09:22 +000031#include <sys/ipc.h>
32#include <sys/msg.h>
bellard31e31b82003-02-18 22:55:36 +000033#include <sys/wait.h>
34#include <sys/time.h>
35#include <sys/stat.h>
36#include <sys/mount.h>
ths39b9aae2007-02-11 18:36:44 +000037#include <sys/prctl.h>
bellard31e31b82003-02-18 22:55:36 +000038#include <sys/resource.h>
39#include <sys/mman.h>
40#include <sys/swap.h>
41#include <signal.h>
42#include <sched.h>
43#include <sys/socket.h>
44#include <sys/uio.h>
bellard9de5e442003-03-23 16:49:39 +000045#include <sys/poll.h>
bellard32f36bc2003-03-30 21:29:48 +000046#include <sys/times.h>
bellard8853f862004-02-22 14:57:26 +000047#include <sys/shm.h>
thsfa294812007-02-02 22:05:00 +000048#include <sys/sem.h>
bellard56c8f682005-11-28 22:28:41 +000049#include <sys/statfs.h>
bellardebc05482003-09-30 21:08:41 +000050#include <utime.h>
bellarda5448a72004-06-19 16:59:03 +000051#include <sys/sysinfo.h>
bellard72f03902003-02-18 23:33:18 +000052//#include <sys/user.h>
bellard8853f862004-02-22 14:57:26 +000053#include <netinet/ip.h>
bellard7854b052003-03-29 17:22:23 +000054#include <netinet/tcp.h>
bellard31e31b82003-02-18 22:55:36 +000055
56#define termios host_termios
57#define winsize host_winsize
58#define termio host_termio
bellard04369ff2003-03-20 22:33:23 +000059#define sgttyb host_sgttyb /* same as target */
60#define tchars host_tchars /* same as target */
61#define ltchars host_ltchars /* same as target */
bellard31e31b82003-02-18 22:55:36 +000062
63#include <linux/termios.h>
64#include <linux/unistd.h>
65#include <linux/utsname.h>
66#include <linux/cdrom.h>
67#include <linux/hdreg.h>
68#include <linux/soundcard.h>
bellarddab2ed92003-03-22 15:23:14 +000069#include <linux/dirent.h>
bellard19b84f32003-05-08 15:41:49 +000070#include <linux/kd.h>
bellard31e31b82003-02-18 22:55:36 +000071
bellard3ef693a2003-03-23 20:17:16 +000072#include "qemu.h"
bellard31e31b82003-02-18 22:55:36 +000073
bellard72f03902003-02-18 23:33:18 +000074//#define DEBUG
bellard31e31b82003-02-18 22:55:36 +000075
pbrooke6e59062006-10-22 00:18:54 +000076#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC) \
ths48733d12007-10-08 13:36:46 +000077 || defined(TARGET_M68K) || defined(TARGET_SH4) || defined(TARGET_CRIS)
bellard67867302003-11-23 17:05:30 +000078/* 16 bit uid wrappers emulation */
79#define USE_UID16
80#endif
81
bellard1a9353d2003-03-16 20:28:50 +000082//#include <linux/msdos_fs.h>
83#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct dirent [2])
84#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct dirent [2])
85
bellard70a194b2003-08-11 22:20:16 +000086
bellard70a194b2003-08-11 22:20:16 +000087#undef _syscall0
88#undef _syscall1
89#undef _syscall2
90#undef _syscall3
91#undef _syscall4
92#undef _syscall5
bellard83fcb512006-06-14 13:37:16 +000093#undef _syscall6
bellard70a194b2003-08-11 22:20:16 +000094
bellard83fcb512006-06-14 13:37:16 +000095#define _syscall0(type,name) \
96type name (void) \
97{ \
98 return syscall(__NR_##name); \
bellard70a194b2003-08-11 22:20:16 +000099}
100
bellard83fcb512006-06-14 13:37:16 +0000101#define _syscall1(type,name,type1,arg1) \
102type name (type1 arg1) \
103{ \
104 return syscall(__NR_##name, arg1); \
bellard70a194b2003-08-11 22:20:16 +0000105}
106
bellard83fcb512006-06-14 13:37:16 +0000107#define _syscall2(type,name,type1,arg1,type2,arg2) \
108type name (type1 arg1,type2 arg2) \
109{ \
110 return syscall(__NR_##name, arg1, arg2); \
bellard70a194b2003-08-11 22:20:16 +0000111}
112
bellard83fcb512006-06-14 13:37:16 +0000113#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
114type name (type1 arg1,type2 arg2,type3 arg3) \
115{ \
116 return syscall(__NR_##name, arg1, arg2, arg3); \
bellard70a194b2003-08-11 22:20:16 +0000117}
118
bellard83fcb512006-06-14 13:37:16 +0000119#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
120type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
121{ \
122 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
bellard70a194b2003-08-11 22:20:16 +0000123}
124
bellard83fcb512006-06-14 13:37:16 +0000125#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
126 type5,arg5) \
127type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
128{ \
129 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
bellard70a194b2003-08-11 22:20:16 +0000130}
bellard83fcb512006-06-14 13:37:16 +0000131
132
133#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
134 type5,arg5,type6,arg6) \
135type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \
136{ \
137 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
138}
139
bellard70a194b2003-08-11 22:20:16 +0000140
bellard31e31b82003-02-18 22:55:36 +0000141#define __NR_sys_uname __NR_uname
ths92a34c12007-09-24 09:27:49 +0000142#define __NR_sys_faccessat __NR_faccessat
ths814d7972007-09-24 09:26:51 +0000143#define __NR_sys_fchmodat __NR_fchmodat
thsccfa72b2007-09-24 09:23:34 +0000144#define __NR_sys_fchownat __NR_fchownat
bellard72f03902003-02-18 23:33:18 +0000145#define __NR_sys_getcwd1 __NR_getcwd
bellard72f03902003-02-18 23:33:18 +0000146#define __NR_sys_getdents __NR_getdents
bellarddab2ed92003-03-22 15:23:14 +0000147#define __NR_sys_getdents64 __NR_getdents64
thsc6cda172007-10-09 03:42:34 +0000148#define __NR_sys_getpriority __NR_getpriority
ths64f0ce42007-09-24 09:25:06 +0000149#define __NR_sys_linkat __NR_linkat
ths4472ad02007-09-24 09:22:32 +0000150#define __NR_sys_mkdirat __NR_mkdirat
ths75ac37a2007-09-24 09:23:05 +0000151#define __NR_sys_mknodat __NR_mknodat
ths82424832007-09-24 09:21:55 +0000152#define __NR_sys_openat __NR_openat
ths5e0ccb12007-09-24 09:26:10 +0000153#define __NR_sys_readlinkat __NR_readlinkat
ths722183f2007-09-24 09:24:37 +0000154#define __NR_sys_renameat __NR_renameat
bellard66fb9762003-03-23 01:06:05 +0000155#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
thsf0b62432007-09-24 09:25:40 +0000156#define __NR_sys_symlinkat __NR_symlinkat
ths7494b0f2007-02-11 18:26:53 +0000157#define __NR_sys_syslog __NR_syslog
ths71455572007-06-21 21:45:30 +0000158#define __NR_sys_tgkill __NR_tgkill
ths4cae1d12007-07-12 11:06:53 +0000159#define __NR_sys_tkill __NR_tkill
ths8170f562007-09-24 09:24:11 +0000160#define __NR_sys_unlinkat __NR_unlinkat
ths9007f0e2007-09-25 17:50:37 +0000161#define __NR_sys_utimensat __NR_utimensat
bellard31e31b82003-02-18 22:55:36 +0000162
bellardbc51c5c2004-03-17 23:46:04 +0000163#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
bellard9af9eaa2003-04-07 21:34:41 +0000164#define __NR__llseek __NR_lseek
165#endif
166
bellard72f03902003-02-18 23:33:18 +0000167#ifdef __NR_gettid
bellard31e31b82003-02-18 22:55:36 +0000168_syscall0(int, gettid)
bellard72f03902003-02-18 23:33:18 +0000169#else
ths0da46a62007-10-20 20:23:07 +0000170/* This is a replacement for the host gettid() and must return a host
171 errno. */
bellard72f03902003-02-18 23:33:18 +0000172static int gettid(void) {
173 return -ENOSYS;
174}
175#endif
bellard31e31b82003-02-18 22:55:36 +0000176_syscall1(int,sys_uname,struct new_utsname *,buf)
ths92a34c12007-09-24 09:27:49 +0000177#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
178_syscall4(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode,int,flags)
179#endif
ths814d7972007-09-24 09:26:51 +0000180#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
181_syscall4(int,sys_fchmodat,int,dirfd,const char *,pathname,
182 mode_t,mode,int,flags)
183#endif
thsccfa72b2007-09-24 09:23:34 +0000184#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
185_syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
186 uid_t,owner,gid_t,group,int,flags)
187#endif
bellard72f03902003-02-18 23:33:18 +0000188_syscall2(int,sys_getcwd1,char *,buf,size_t,size)
189_syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
ths3ae43202007-09-16 21:39:48 +0000190#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +0000191_syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
ths3ae43202007-09-16 21:39:48 +0000192#endif
thsc6cda172007-10-09 03:42:34 +0000193_syscall2(int, sys_getpriority, int, which, int, who);
bellard31e31b82003-02-18 22:55:36 +0000194_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
195 loff_t *, res, uint, wh);
ths64f0ce42007-09-24 09:25:06 +0000196#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
197_syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
198 int,newdirfd,const char *,newpath,int,flags)
199#endif
ths4472ad02007-09-24 09:22:32 +0000200#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
201_syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
202#endif
ths75ac37a2007-09-24 09:23:05 +0000203#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
204_syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
205 mode_t,mode,dev_t,dev)
206#endif
ths82424832007-09-24 09:21:55 +0000207#if defined(TARGET_NR_openat) && defined(__NR_openat)
208_syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
209#endif
ths5e0ccb12007-09-24 09:26:10 +0000210#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
211_syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
212 char *,buf,size_t,bufsize)
213#endif
ths722183f2007-09-24 09:24:37 +0000214#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
215_syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
216 int,newdirfd,const char *,newpath)
217#endif
bellard66fb9762003-03-23 01:06:05 +0000218_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
thsb51eaa82007-09-25 16:09:22 +0000219#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +0000220_syscall3(int,sys_symlinkat,const char *,oldpath,
221 int,newdirfd,const char *,newpath)
222#endif
ths7494b0f2007-02-11 18:26:53 +0000223_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
ths3ae43202007-09-16 21:39:48 +0000224#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +0000225_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
ths4cae1d12007-07-12 11:06:53 +0000226#endif
ths3ae43202007-09-16 21:39:48 +0000227#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +0000228_syscall2(int,sys_tkill,int,tid,int,sig)
229#endif
bellardec86b0f2003-04-11 00:15:04 +0000230#ifdef __NR_exit_group
231_syscall1(int,exit_group,int,error_code)
232#endif
ths6f5b89a2007-03-02 20:48:00 +0000233#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
234_syscall1(int,set_tid_address,int *,tidptr)
235#endif
ths8170f562007-09-24 09:24:11 +0000236#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
237_syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
238#endif
ths9007f0e2007-09-25 17:50:37 +0000239#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
240_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
241 const struct timespec *,tsp,int,flags)
242#endif
bellard66fb9762003-03-23 01:06:05 +0000243
244extern int personality(int);
bellard9de5e442003-03-23 16:49:39 +0000245extern int flock(int, int);
246extern int setfsuid(int);
247extern int setfsgid(int);
bellard5cd43932003-03-29 16:54:36 +0000248extern int setresuid(uid_t, uid_t, uid_t);
249extern int getresuid(uid_t *, uid_t *, uid_t *);
250extern int setresgid(gid_t, gid_t, gid_t);
251extern int getresgid(gid_t *, gid_t *, gid_t *);
bellard19b84f32003-05-08 15:41:49 +0000252extern int setgroups(int, gid_t *);
bellard31e31b82003-02-18 22:55:36 +0000253
ths637947f2007-06-01 12:09:19 +0000254/*
thsfe8f0962007-07-12 10:59:21 +0000255 * This list is the union of errno values overridden in asm-<arch>/errno.h
ths637947f2007-06-01 12:09:19 +0000256 * minus the errnos that are not actually generic to all archs.
257 */
258static uint16_t host_to_target_errno_table[1200] = {
259 [EIDRM] = TARGET_EIDRM,
260 [ECHRNG] = TARGET_ECHRNG,
261 [EL2NSYNC] = TARGET_EL2NSYNC,
262 [EL3HLT] = TARGET_EL3HLT,
263 [EL3RST] = TARGET_EL3RST,
264 [ELNRNG] = TARGET_ELNRNG,
265 [EUNATCH] = TARGET_EUNATCH,
266 [ENOCSI] = TARGET_ENOCSI,
267 [EL2HLT] = TARGET_EL2HLT,
268 [EDEADLK] = TARGET_EDEADLK,
269 [ENOLCK] = TARGET_ENOLCK,
270 [EBADE] = TARGET_EBADE,
271 [EBADR] = TARGET_EBADR,
272 [EXFULL] = TARGET_EXFULL,
273 [ENOANO] = TARGET_ENOANO,
274 [EBADRQC] = TARGET_EBADRQC,
275 [EBADSLT] = TARGET_EBADSLT,
276 [EBFONT] = TARGET_EBFONT,
277 [ENOSTR] = TARGET_ENOSTR,
278 [ENODATA] = TARGET_ENODATA,
279 [ETIME] = TARGET_ETIME,
280 [ENOSR] = TARGET_ENOSR,
281 [ENONET] = TARGET_ENONET,
282 [ENOPKG] = TARGET_ENOPKG,
283 [EREMOTE] = TARGET_EREMOTE,
284 [ENOLINK] = TARGET_ENOLINK,
285 [EADV] = TARGET_EADV,
286 [ESRMNT] = TARGET_ESRMNT,
287 [ECOMM] = TARGET_ECOMM,
288 [EPROTO] = TARGET_EPROTO,
289 [EDOTDOT] = TARGET_EDOTDOT,
290 [EMULTIHOP] = TARGET_EMULTIHOP,
291 [EBADMSG] = TARGET_EBADMSG,
292 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
293 [EOVERFLOW] = TARGET_EOVERFLOW,
294 [ENOTUNIQ] = TARGET_ENOTUNIQ,
295 [EBADFD] = TARGET_EBADFD,
296 [EREMCHG] = TARGET_EREMCHG,
297 [ELIBACC] = TARGET_ELIBACC,
298 [ELIBBAD] = TARGET_ELIBBAD,
299 [ELIBSCN] = TARGET_ELIBSCN,
300 [ELIBMAX] = TARGET_ELIBMAX,
301 [ELIBEXEC] = TARGET_ELIBEXEC,
302 [EILSEQ] = TARGET_EILSEQ,
303 [ENOSYS] = TARGET_ENOSYS,
304 [ELOOP] = TARGET_ELOOP,
305 [ERESTART] = TARGET_ERESTART,
306 [ESTRPIPE] = TARGET_ESTRPIPE,
307 [ENOTEMPTY] = TARGET_ENOTEMPTY,
308 [EUSERS] = TARGET_EUSERS,
309 [ENOTSOCK] = TARGET_ENOTSOCK,
310 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
311 [EMSGSIZE] = TARGET_EMSGSIZE,
312 [EPROTOTYPE] = TARGET_EPROTOTYPE,
313 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
314 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
315 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
316 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
317 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
318 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
319 [EADDRINUSE] = TARGET_EADDRINUSE,
320 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
321 [ENETDOWN] = TARGET_ENETDOWN,
322 [ENETUNREACH] = TARGET_ENETUNREACH,
323 [ENETRESET] = TARGET_ENETRESET,
324 [ECONNABORTED] = TARGET_ECONNABORTED,
325 [ECONNRESET] = TARGET_ECONNRESET,
326 [ENOBUFS] = TARGET_ENOBUFS,
327 [EISCONN] = TARGET_EISCONN,
328 [ENOTCONN] = TARGET_ENOTCONN,
329 [EUCLEAN] = TARGET_EUCLEAN,
330 [ENOTNAM] = TARGET_ENOTNAM,
331 [ENAVAIL] = TARGET_ENAVAIL,
332 [EISNAM] = TARGET_EISNAM,
333 [EREMOTEIO] = TARGET_EREMOTEIO,
334 [ESHUTDOWN] = TARGET_ESHUTDOWN,
335 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
336 [ETIMEDOUT] = TARGET_ETIMEDOUT,
337 [ECONNREFUSED] = TARGET_ECONNREFUSED,
338 [EHOSTDOWN] = TARGET_EHOSTDOWN,
339 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
340 [EALREADY] = TARGET_EALREADY,
341 [EINPROGRESS] = TARGET_EINPROGRESS,
342 [ESTALE] = TARGET_ESTALE,
343 [ECANCELED] = TARGET_ECANCELED,
344 [ENOMEDIUM] = TARGET_ENOMEDIUM,
345 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
thsb7fe5db2007-07-23 15:37:46 +0000346#ifdef ENOKEY
ths637947f2007-06-01 12:09:19 +0000347 [ENOKEY] = TARGET_ENOKEY,
thsb7fe5db2007-07-23 15:37:46 +0000348#endif
349#ifdef EKEYEXPIRED
ths637947f2007-06-01 12:09:19 +0000350 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
thsb7fe5db2007-07-23 15:37:46 +0000351#endif
352#ifdef EKEYREVOKED
ths637947f2007-06-01 12:09:19 +0000353 [EKEYREVOKED] = TARGET_EKEYREVOKED,
thsb7fe5db2007-07-23 15:37:46 +0000354#endif
355#ifdef EKEYREJECTED
ths637947f2007-06-01 12:09:19 +0000356 [EKEYREJECTED] = TARGET_EKEYREJECTED,
thsb7fe5db2007-07-23 15:37:46 +0000357#endif
358#ifdef EOWNERDEAD
ths637947f2007-06-01 12:09:19 +0000359 [EOWNERDEAD] = TARGET_EOWNERDEAD,
thsb7fe5db2007-07-23 15:37:46 +0000360#endif
361#ifdef ENOTRECOVERABLE
ths637947f2007-06-01 12:09:19 +0000362 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
thsb7fe5db2007-07-23 15:37:46 +0000363#endif
ths637947f2007-06-01 12:09:19 +0000364 };
365
366static inline int host_to_target_errno(int err)
367{
368 if(host_to_target_errno_table[err])
369 return host_to_target_errno_table[err];
370 return err;
371}
372
blueswir1992f48a2007-10-14 16:27:31 +0000373static inline abi_long get_errno(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000374{
375 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000376 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000377 else
378 return ret;
379}
380
blueswir1992f48a2007-10-14 16:27:31 +0000381static inline int is_error(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000382{
blueswir1992f48a2007-10-14 16:27:31 +0000383 return (abi_ulong)ret >= (abi_ulong)(-4096);
bellard31e31b82003-02-18 22:55:36 +0000384}
385
blueswir1992f48a2007-10-14 16:27:31 +0000386static abi_ulong target_brk;
387static abi_ulong target_original_brk;
bellard31e31b82003-02-18 22:55:36 +0000388
blueswir1992f48a2007-10-14 16:27:31 +0000389void target_set_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000390{
blueswir14c1de732007-07-07 20:45:44 +0000391 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
bellard31e31b82003-02-18 22:55:36 +0000392}
393
ths0da46a62007-10-20 20:23:07 +0000394/* do_brk() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000395abi_long do_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000396{
blueswir1992f48a2007-10-14 16:27:31 +0000397 abi_ulong brk_page;
398 abi_long mapped_addr;
bellard31e31b82003-02-18 22:55:36 +0000399 int new_alloc_size;
400
401 if (!new_brk)
pbrook53a59602006-03-25 19:31:22 +0000402 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000403 if (new_brk < target_original_brk)
ths0da46a62007-10-20 20:23:07 +0000404 return -TARGET_ENOMEM;
ths3b46e622007-09-17 08:09:54 +0000405
pbrook53a59602006-03-25 19:31:22 +0000406 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000407
408 /* If the new brk is less than this, set it and we're done... */
409 if (new_brk < brk_page) {
410 target_brk = new_brk;
pbrook53a59602006-03-25 19:31:22 +0000411 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000412 }
413
414 /* We need to allocate more memory after the brk... */
bellard54936002003-05-13 00:25:15 +0000415 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
ths5fafdf22007-09-16 21:08:06 +0000416 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000417 PROT_READ|PROT_WRITE,
418 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
bellard31e31b82003-02-18 22:55:36 +0000419 if (is_error(mapped_addr)) {
420 return mapped_addr;
421 } else {
422 target_brk = new_brk;
pbrook53a59602006-03-25 19:31:22 +0000423 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000424 }
425}
426
ths5fafdf22007-09-16 21:08:06 +0000427static inline fd_set *target_to_host_fds(fd_set *fds,
blueswir1992f48a2007-10-14 16:27:31 +0000428 abi_long *target_fds, int n)
bellard31e31b82003-02-18 22:55:36 +0000429{
bellard7854b052003-03-29 17:22:23 +0000430#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
bellard31e31b82003-02-18 22:55:36 +0000431 return (fd_set *)target_fds;
432#else
433 int i, b;
434 if (target_fds) {
435 FD_ZERO(fds);
436 for(i = 0;i < n; i++) {
blueswir1992f48a2007-10-14 16:27:31 +0000437 b = (tswapl(target_fds[i / TARGET_ABI_BITS]) >>
438 (i & (TARGET_ABI_BITS - 1))) & 1;
bellard31e31b82003-02-18 22:55:36 +0000439 if (b)
440 FD_SET(i, fds);
441 }
442 return fds;
443 } else {
444 return NULL;
445 }
446#endif
447}
448
blueswir1992f48a2007-10-14 16:27:31 +0000449static inline void host_to_target_fds(abi_long *target_fds,
bellard31e31b82003-02-18 22:55:36 +0000450 fd_set *fds, int n)
451{
bellard7854b052003-03-29 17:22:23 +0000452#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
bellard31e31b82003-02-18 22:55:36 +0000453 /* nothing to do */
454#else
455 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000456 abi_long v;
bellard31e31b82003-02-18 22:55:36 +0000457
458 if (target_fds) {
blueswir1992f48a2007-10-14 16:27:31 +0000459 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
bellard31e31b82003-02-18 22:55:36 +0000460 k = 0;
461 for(i = 0;i < nw; i++) {
462 v = 0;
blueswir1992f48a2007-10-14 16:27:31 +0000463 for(j = 0; j < TARGET_ABI_BITS; j++) {
bellard31e31b82003-02-18 22:55:36 +0000464 v |= ((FD_ISSET(k, fds) != 0) << j);
465 k++;
466 }
467 target_fds[i] = tswapl(v);
468 }
469 }
470#endif
471}
472
bellardc596ed12003-07-13 17:32:31 +0000473#if defined(__alpha__)
474#define HOST_HZ 1024
475#else
476#define HOST_HZ 100
477#endif
478
blueswir1992f48a2007-10-14 16:27:31 +0000479static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000480{
481#if HOST_HZ == TARGET_HZ
482 return ticks;
483#else
484 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
485#endif
486}
487
blueswir1992f48a2007-10-14 16:27:31 +0000488static inline void host_to_target_rusage(abi_ulong target_addr,
bellardb4091862003-05-16 15:39:34 +0000489 const struct rusage *rusage)
490{
pbrook53a59602006-03-25 19:31:22 +0000491 struct target_rusage *target_rusage;
492
493 lock_user_struct(target_rusage, target_addr, 0);
bellardb4091862003-05-16 15:39:34 +0000494 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
495 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
496 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
497 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
498 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
499 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
500 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
501 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
502 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
503 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
504 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
505 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
506 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
507 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
508 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
509 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
510 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
511 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000512 unlock_user_struct(target_rusage, target_addr, 1);
bellardb4091862003-05-16 15:39:34 +0000513}
514
pbrook53a59602006-03-25 19:31:22 +0000515static inline void target_to_host_timeval(struct timeval *tv,
blueswir1992f48a2007-10-14 16:27:31 +0000516 abi_ulong target_addr)
bellard31e31b82003-02-18 22:55:36 +0000517{
pbrook53a59602006-03-25 19:31:22 +0000518 struct target_timeval *target_tv;
519
520 lock_user_struct(target_tv, target_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000521 tv->tv_sec = tswapl(target_tv->tv_sec);
522 tv->tv_usec = tswapl(target_tv->tv_usec);
pbrook53a59602006-03-25 19:31:22 +0000523 unlock_user_struct(target_tv, target_addr, 0);
bellard31e31b82003-02-18 22:55:36 +0000524}
525
blueswir1992f48a2007-10-14 16:27:31 +0000526static inline void host_to_target_timeval(abi_ulong target_addr,
bellard5cd43932003-03-29 16:54:36 +0000527 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +0000528{
pbrook53a59602006-03-25 19:31:22 +0000529 struct target_timeval *target_tv;
530
531 lock_user_struct(target_tv, target_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000532 target_tv->tv_sec = tswapl(tv->tv_sec);
533 target_tv->tv_usec = tswapl(tv->tv_usec);
pbrook53a59602006-03-25 19:31:22 +0000534 unlock_user_struct(target_tv, target_addr, 1);
bellard31e31b82003-02-18 22:55:36 +0000535}
536
537
ths0da46a62007-10-20 20:23:07 +0000538/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000539static abi_long do_select(int n,
540 abi_ulong rfd_p, abi_ulong wfd_p,
541 abi_ulong efd_p, abi_ulong target_tv)
bellard31e31b82003-02-18 22:55:36 +0000542{
543 fd_set rfds, wfds, efds;
544 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
blueswir1992f48a2007-10-14 16:27:31 +0000545 abi_long *target_rfds, *target_wfds, *target_efds;
bellard31e31b82003-02-18 22:55:36 +0000546 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +0000547 abi_long ret;
pbrook53a59602006-03-25 19:31:22 +0000548 int ok;
bellard31e31b82003-02-18 22:55:36 +0000549
pbrook53a59602006-03-25 19:31:22 +0000550 if (rfd_p) {
blueswir1992f48a2007-10-14 16:27:31 +0000551 target_rfds = lock_user(rfd_p, sizeof(abi_long) * n, 1);
pbrook53a59602006-03-25 19:31:22 +0000552 rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
553 } else {
554 target_rfds = NULL;
555 rfds_ptr = NULL;
556 }
557 if (wfd_p) {
blueswir1992f48a2007-10-14 16:27:31 +0000558 target_wfds = lock_user(wfd_p, sizeof(abi_long) * n, 1);
pbrook53a59602006-03-25 19:31:22 +0000559 wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
560 } else {
561 target_wfds = NULL;
562 wfds_ptr = NULL;
563 }
564 if (efd_p) {
blueswir1992f48a2007-10-14 16:27:31 +0000565 target_efds = lock_user(efd_p, sizeof(abi_long) * n, 1);
pbrook53a59602006-03-25 19:31:22 +0000566 efds_ptr = target_to_host_fds(&efds, target_efds, n);
567 } else {
568 target_efds = NULL;
569 efds_ptr = NULL;
570 }
ths3b46e622007-09-17 08:09:54 +0000571
bellard31e31b82003-02-18 22:55:36 +0000572 if (target_tv) {
bellard5cd43932003-03-29 16:54:36 +0000573 target_to_host_timeval(&tv, target_tv);
bellard31e31b82003-02-18 22:55:36 +0000574 tv_ptr = &tv;
575 } else {
576 tv_ptr = NULL;
577 }
578 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +0000579 ok = !is_error(ret);
580
581 if (ok) {
bellard31e31b82003-02-18 22:55:36 +0000582 host_to_target_fds(target_rfds, rfds_ptr, n);
583 host_to_target_fds(target_wfds, wfds_ptr, n);
584 host_to_target_fds(target_efds, efds_ptr, n);
585
586 if (target_tv) {
bellard5cd43932003-03-29 16:54:36 +0000587 host_to_target_timeval(target_tv, &tv);
bellard31e31b82003-02-18 22:55:36 +0000588 }
589 }
pbrook53a59602006-03-25 19:31:22 +0000590 if (target_rfds)
blueswir1992f48a2007-10-14 16:27:31 +0000591 unlock_user(target_rfds, rfd_p, ok ? sizeof(abi_long) * n : 0);
pbrook53a59602006-03-25 19:31:22 +0000592 if (target_wfds)
blueswir1992f48a2007-10-14 16:27:31 +0000593 unlock_user(target_wfds, wfd_p, ok ? sizeof(abi_long) * n : 0);
pbrook53a59602006-03-25 19:31:22 +0000594 if (target_efds)
blueswir1992f48a2007-10-14 16:27:31 +0000595 unlock_user(target_efds, efd_p, ok ? sizeof(abi_long) * n : 0);
pbrook53a59602006-03-25 19:31:22 +0000596
bellard31e31b82003-02-18 22:55:36 +0000597 return ret;
598}
599
bellard7854b052003-03-29 17:22:23 +0000600static inline void target_to_host_sockaddr(struct sockaddr *addr,
blueswir1992f48a2007-10-14 16:27:31 +0000601 abi_ulong target_addr,
bellard7854b052003-03-29 17:22:23 +0000602 socklen_t len)
603{
pbrook53a59602006-03-25 19:31:22 +0000604 struct target_sockaddr *target_saddr;
605
606 target_saddr = lock_user(target_addr, len, 1);
607 memcpy(addr, target_saddr, len);
608 addr->sa_family = tswap16(target_saddr->sa_family);
609 unlock_user(target_saddr, target_addr, 0);
bellard7854b052003-03-29 17:22:23 +0000610}
611
blueswir1992f48a2007-10-14 16:27:31 +0000612static inline void host_to_target_sockaddr(abi_ulong target_addr,
bellard7854b052003-03-29 17:22:23 +0000613 struct sockaddr *addr,
614 socklen_t len)
615{
pbrook53a59602006-03-25 19:31:22 +0000616 struct target_sockaddr *target_saddr;
617
618 target_saddr = lock_user(target_addr, len, 0);
619 memcpy(target_saddr, addr, len);
620 target_saddr->sa_family = tswap16(addr->sa_family);
621 unlock_user(target_saddr, target_addr, len);
bellard7854b052003-03-29 17:22:23 +0000622}
623
pbrook53a59602006-03-25 19:31:22 +0000624/* ??? Should this also swap msgh->name? */
bellard7854b052003-03-29 17:22:23 +0000625static inline void target_to_host_cmsg(struct msghdr *msgh,
626 struct target_msghdr *target_msgh)
627{
628 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
629 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
630 socklen_t space = 0;
631
632 while (cmsg && target_cmsg) {
633 void *data = CMSG_DATA(cmsg);
634 void *target_data = TARGET_CMSG_DATA(target_cmsg);
635
ths5fafdf22007-09-16 21:08:06 +0000636 int len = tswapl(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +0000637 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
638
639 space += CMSG_SPACE(len);
640 if (space > msgh->msg_controllen) {
641 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +0000642 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +0000643 break;
644 }
645
646 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
647 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
648 cmsg->cmsg_len = CMSG_LEN(len);
649
bellard3532fa72006-06-24 15:06:03 +0000650 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +0000651 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
652 memcpy(data, target_data, len);
653 } else {
654 int *fd = (int *)data;
655 int *target_fd = (int *)target_data;
656 int i, numfds = len / sizeof(int);
657
658 for (i = 0; i < numfds; i++)
659 fd[i] = tswap32(target_fd[i]);
660 }
661
662 cmsg = CMSG_NXTHDR(msgh, cmsg);
663 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
664 }
665
666 msgh->msg_controllen = space;
667}
668
pbrook53a59602006-03-25 19:31:22 +0000669/* ??? Should this also swap msgh->name? */
bellard7854b052003-03-29 17:22:23 +0000670static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
671 struct msghdr *msgh)
672{
673 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
674 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
675 socklen_t space = 0;
676
677 while (cmsg && target_cmsg) {
678 void *data = CMSG_DATA(cmsg);
679 void *target_data = TARGET_CMSG_DATA(target_cmsg);
680
681 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
682
683 space += TARGET_CMSG_SPACE(len);
684 if (space > tswapl(target_msgh->msg_controllen)) {
685 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +0000686 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +0000687 break;
688 }
689
690 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
691 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
692 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
693
bellard3532fa72006-06-24 15:06:03 +0000694 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +0000695 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
696 memcpy(target_data, data, len);
697 } else {
698 int *fd = (int *)data;
699 int *target_fd = (int *)target_data;
700 int i, numfds = len / sizeof(int);
701
702 for (i = 0; i < numfds; i++)
703 target_fd[i] = tswap32(fd[i]);
704 }
705
706 cmsg = CMSG_NXTHDR(msgh, cmsg);
707 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
708 }
709
710 msgh->msg_controllen = tswapl(space);
711}
712
ths0da46a62007-10-20 20:23:07 +0000713/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000714static abi_long do_setsockopt(int sockfd, int level, int optname,
715 abi_ulong optval, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +0000716{
blueswir1992f48a2007-10-14 16:27:31 +0000717 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +0000718 int val;
ths3b46e622007-09-17 08:09:54 +0000719
bellard8853f862004-02-22 14:57:26 +0000720 switch(level) {
721 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +0000722 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +0000723 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +0000724 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +0000725
pbrook53a59602006-03-25 19:31:22 +0000726 val = tget32(optval);
bellard8853f862004-02-22 14:57:26 +0000727 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
728 break;
729 case SOL_IP:
730 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +0000731 case IP_TOS:
732 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +0000733 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +0000734 case IP_ROUTER_ALERT:
735 case IP_RECVOPTS:
736 case IP_RETOPTS:
737 case IP_PKTINFO:
738 case IP_MTU_DISCOVER:
739 case IP_RECVERR:
740 case IP_RECVTOS:
741#ifdef IP_FREEBIND
742 case IP_FREEBIND:
743#endif
744 case IP_MULTICAST_TTL:
745 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +0000746 val = 0;
747 if (optlen >= sizeof(uint32_t)) {
pbrook53a59602006-03-25 19:31:22 +0000748 val = tget32(optval);
bellard8853f862004-02-22 14:57:26 +0000749 } else if (optlen >= 1) {
pbrook53a59602006-03-25 19:31:22 +0000750 val = tget8(optval);
bellard8853f862004-02-22 14:57:26 +0000751 }
752 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
753 break;
754 default:
755 goto unimplemented;
756 }
757 break;
bellard3532fa72006-06-24 15:06:03 +0000758 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +0000759 switch (optname) {
760 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +0000761 case TARGET_SO_DEBUG:
762 optname = SO_DEBUG;
763 break;
764 case TARGET_SO_REUSEADDR:
765 optname = SO_REUSEADDR;
766 break;
767 case TARGET_SO_TYPE:
768 optname = SO_TYPE;
769 break;
770 case TARGET_SO_ERROR:
771 optname = SO_ERROR;
772 break;
773 case TARGET_SO_DONTROUTE:
774 optname = SO_DONTROUTE;
775 break;
776 case TARGET_SO_BROADCAST:
777 optname = SO_BROADCAST;
778 break;
779 case TARGET_SO_SNDBUF:
780 optname = SO_SNDBUF;
781 break;
782 case TARGET_SO_RCVBUF:
783 optname = SO_RCVBUF;
784 break;
785 case TARGET_SO_KEEPALIVE:
786 optname = SO_KEEPALIVE;
787 break;
788 case TARGET_SO_OOBINLINE:
789 optname = SO_OOBINLINE;
790 break;
791 case TARGET_SO_NO_CHECK:
792 optname = SO_NO_CHECK;
793 break;
794 case TARGET_SO_PRIORITY:
795 optname = SO_PRIORITY;
796 break;
bellard5e83e8e2005-03-01 22:32:06 +0000797#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +0000798 case TARGET_SO_BSDCOMPAT:
799 optname = SO_BSDCOMPAT;
800 break;
bellard5e83e8e2005-03-01 22:32:06 +0000801#endif
bellard3532fa72006-06-24 15:06:03 +0000802 case TARGET_SO_PASSCRED:
803 optname = SO_PASSCRED;
804 break;
805 case TARGET_SO_TIMESTAMP:
806 optname = SO_TIMESTAMP;
807 break;
808 case TARGET_SO_RCVLOWAT:
809 optname = SO_RCVLOWAT;
810 break;
811 case TARGET_SO_RCVTIMEO:
812 optname = SO_RCVTIMEO;
813 break;
814 case TARGET_SO_SNDTIMEO:
815 optname = SO_SNDTIMEO;
816 break;
bellard8853f862004-02-22 14:57:26 +0000817 break;
818 default:
819 goto unimplemented;
820 }
bellard3532fa72006-06-24 15:06:03 +0000821 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +0000822 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +0000823
824 val = tget32(optval);
825 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +0000826 break;
bellard7854b052003-03-29 17:22:23 +0000827 default:
bellard8853f862004-02-22 14:57:26 +0000828 unimplemented:
829 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
ths0da46a62007-10-20 20:23:07 +0000830 ret = -TARGET_ENOSYS;
bellard7854b052003-03-29 17:22:23 +0000831 }
bellard8853f862004-02-22 14:57:26 +0000832 return ret;
bellard7854b052003-03-29 17:22:23 +0000833}
834
ths0da46a62007-10-20 20:23:07 +0000835/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000836static abi_long do_getsockopt(int sockfd, int level, int optname,
837 abi_ulong optval, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +0000838{
blueswir1992f48a2007-10-14 16:27:31 +0000839 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +0000840 int len, lv, val;
bellard8853f862004-02-22 14:57:26 +0000841
842 switch(level) {
bellard3532fa72006-06-24 15:06:03 +0000843 case TARGET_SOL_SOCKET:
844 level = SOL_SOCKET;
bellard8853f862004-02-22 14:57:26 +0000845 switch (optname) {
bellard3532fa72006-06-24 15:06:03 +0000846 case TARGET_SO_LINGER:
847 case TARGET_SO_RCVTIMEO:
848 case TARGET_SO_SNDTIMEO:
849 case TARGET_SO_PEERCRED:
850 case TARGET_SO_PEERNAME:
bellard8853f862004-02-22 14:57:26 +0000851 /* These don't just return a single integer */
852 goto unimplemented;
853 default:
bellard2efbe912005-07-23 15:10:20 +0000854 goto int_case;
855 }
856 break;
857 case SOL_TCP:
858 /* TCP options all take an 'int' value. */
859 int_case:
pbrook53a59602006-03-25 19:31:22 +0000860 len = tget32(optlen);
bellard2efbe912005-07-23 15:10:20 +0000861 if (len < 0)
ths0da46a62007-10-20 20:23:07 +0000862 return -TARGET_EINVAL;
bellard2efbe912005-07-23 15:10:20 +0000863 lv = sizeof(int);
864 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
865 if (ret < 0)
866 return ret;
867 val = tswap32(val);
868 if (len > lv)
869 len = lv;
pbrook53a59602006-03-25 19:31:22 +0000870 if (len == 4)
871 tput32(optval, val);
872 else
873 tput8(optval, val);
874 tput32(optlen, len);
bellard2efbe912005-07-23 15:10:20 +0000875 break;
876 case SOL_IP:
877 switch(optname) {
878 case IP_TOS:
879 case IP_TTL:
880 case IP_HDRINCL:
881 case IP_ROUTER_ALERT:
882 case IP_RECVOPTS:
883 case IP_RETOPTS:
884 case IP_PKTINFO:
885 case IP_MTU_DISCOVER:
886 case IP_RECVERR:
887 case IP_RECVTOS:
888#ifdef IP_FREEBIND
889 case IP_FREEBIND:
890#endif
891 case IP_MULTICAST_TTL:
892 case IP_MULTICAST_LOOP:
pbrook53a59602006-03-25 19:31:22 +0000893 len = tget32(optlen);
bellard8853f862004-02-22 14:57:26 +0000894 if (len < 0)
ths0da46a62007-10-20 20:23:07 +0000895 return -TARGET_EINVAL;
bellard8853f862004-02-22 14:57:26 +0000896 lv = sizeof(int);
897 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
898 if (ret < 0)
899 return ret;
bellard2efbe912005-07-23 15:10:20 +0000900 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +0000901 len = 1;
pbrook53a59602006-03-25 19:31:22 +0000902 tput32(optlen, len);
903 tput8(optval, val);
bellard2efbe912005-07-23 15:10:20 +0000904 } else {
bellard2efbe912005-07-23 15:10:20 +0000905 if (len > sizeof(int))
906 len = sizeof(int);
pbrook53a59602006-03-25 19:31:22 +0000907 tput32(optlen, len);
908 tput32(optval, val);
bellard2efbe912005-07-23 15:10:20 +0000909 }
bellard8853f862004-02-22 14:57:26 +0000910 break;
bellard2efbe912005-07-23 15:10:20 +0000911 default:
912 goto unimplemented;
bellard8853f862004-02-22 14:57:26 +0000913 }
914 break;
915 default:
916 unimplemented:
917 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
918 level, optname);
ths0da46a62007-10-20 20:23:07 +0000919 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +0000920 break;
921 }
922 return ret;
bellard7854b052003-03-29 17:22:23 +0000923}
924
blueswir1992f48a2007-10-14 16:27:31 +0000925static void lock_iovec(struct iovec *vec, abi_ulong target_addr,
pbrook53a59602006-03-25 19:31:22 +0000926 int count, int copy)
927{
928 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +0000929 abi_ulong base;
pbrook53a59602006-03-25 19:31:22 +0000930 int i;
931
932 target_vec = lock_user(target_addr, count * sizeof(struct target_iovec), 1);
933 for(i = 0;i < count; i++) {
934 base = tswapl(target_vec[i].iov_base);
935 vec[i].iov_len = tswapl(target_vec[i].iov_len);
936 vec[i].iov_base = lock_user(base, vec[i].iov_len, copy);
937 }
938 unlock_user (target_vec, target_addr, 0);
939}
940
blueswir1992f48a2007-10-14 16:27:31 +0000941static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
pbrook53a59602006-03-25 19:31:22 +0000942 int count, int copy)
943{
944 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +0000945 abi_ulong base;
pbrook53a59602006-03-25 19:31:22 +0000946 int i;
947
948 target_vec = lock_user(target_addr, count * sizeof(struct target_iovec), 1);
949 for(i = 0;i < count; i++) {
950 base = tswapl(target_vec[i].iov_base);
951 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
952 }
953 unlock_user (target_vec, target_addr, 0);
954}
955
ths0da46a62007-10-20 20:23:07 +0000956/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000957static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +0000958{
959#if defined(TARGET_MIPS)
960 switch(type) {
961 case TARGET_SOCK_DGRAM:
962 type = SOCK_DGRAM;
963 break;
964 case TARGET_SOCK_STREAM:
965 type = SOCK_STREAM;
966 break;
967 case TARGET_SOCK_RAW:
968 type = SOCK_RAW;
969 break;
970 case TARGET_SOCK_RDM:
971 type = SOCK_RDM;
972 break;
973 case TARGET_SOCK_SEQPACKET:
974 type = SOCK_SEQPACKET;
975 break;
976 case TARGET_SOCK_PACKET:
977 type = SOCK_PACKET;
978 break;
979 }
980#endif
balrog12bc92a2007-10-30 21:06:14 +0000981 if (domain == PF_NETLINK)
982 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
bellard3532fa72006-06-24 15:06:03 +0000983 return get_errno(socket(domain, type, protocol));
984}
985
ths0da46a62007-10-20 20:23:07 +0000986/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000987static abi_long do_bind(int sockfd, abi_ulong target_addr,
988 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +0000989{
990 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +0000991
bellard3532fa72006-06-24 15:06:03 +0000992 target_to_host_sockaddr(addr, target_addr, addrlen);
993 return get_errno(bind(sockfd, addr, addrlen));
994}
995
ths0da46a62007-10-20 20:23:07 +0000996/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000997static abi_long do_connect(int sockfd, abi_ulong target_addr,
998 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +0000999{
1000 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001001
bellard3532fa72006-06-24 15:06:03 +00001002 target_to_host_sockaddr(addr, target_addr, addrlen);
1003 return get_errno(connect(sockfd, addr, addrlen));
1004}
1005
ths0da46a62007-10-20 20:23:07 +00001006/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001007static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1008 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001009{
blueswir1992f48a2007-10-14 16:27:31 +00001010 abi_long ret;
bellard3532fa72006-06-24 15:06:03 +00001011 struct target_msghdr *msgp;
1012 struct msghdr msg;
1013 int count;
1014 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001015 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001016
1017 lock_user_struct(msgp, target_msg, 1);
1018 if (msgp->msg_name) {
1019 msg.msg_namelen = tswap32(msgp->msg_namelen);
1020 msg.msg_name = alloca(msg.msg_namelen);
1021 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1022 msg.msg_namelen);
1023 } else {
1024 msg.msg_name = NULL;
1025 msg.msg_namelen = 0;
1026 }
1027 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1028 msg.msg_control = alloca(msg.msg_controllen);
1029 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001030
bellard3532fa72006-06-24 15:06:03 +00001031 count = tswapl(msgp->msg_iovlen);
1032 vec = alloca(count * sizeof(struct iovec));
1033 target_vec = tswapl(msgp->msg_iov);
1034 lock_iovec(vec, target_vec, count, send);
1035 msg.msg_iovlen = count;
1036 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001037
bellard3532fa72006-06-24 15:06:03 +00001038 if (send) {
1039 target_to_host_cmsg(&msg, msgp);
1040 ret = get_errno(sendmsg(fd, &msg, flags));
1041 } else {
1042 ret = get_errno(recvmsg(fd, &msg, flags));
1043 if (!is_error(ret))
1044 host_to_target_cmsg(msgp, &msg);
1045 }
1046 unlock_iovec(vec, target_vec, count, !send);
1047 return ret;
1048}
1049
ths0da46a62007-10-20 20:23:07 +00001050/* do_accept() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001051static abi_long do_accept(int fd, abi_ulong target_addr,
1052 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001053{
1054 socklen_t addrlen = tget32(target_addrlen);
ths197ea352006-12-11 19:13:59 +00001055 void *addr = alloca(addrlen);
blueswir1992f48a2007-10-14 16:27:31 +00001056 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001057
1058 ret = get_errno(accept(fd, addr, &addrlen));
1059 if (!is_error(ret)) {
1060 host_to_target_sockaddr(target_addr, addr, addrlen);
1061 tput32(target_addrlen, addrlen);
1062 }
1063 return ret;
1064}
1065
ths0da46a62007-10-20 20:23:07 +00001066/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001067static abi_long do_getpeername(int fd, abi_ulong target_addr,
1068 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001069{
1070 socklen_t addrlen = tget32(target_addrlen);
ths01c227f2007-03-19 13:05:13 +00001071 void *addr = alloca(addrlen);
blueswir1992f48a2007-10-14 16:27:31 +00001072 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001073
1074 ret = get_errno(getpeername(fd, addr, &addrlen));
1075 if (!is_error(ret)) {
1076 host_to_target_sockaddr(target_addr, addr, addrlen);
1077 tput32(target_addrlen, addrlen);
1078 }
1079 return ret;
1080}
1081
ths0da46a62007-10-20 20:23:07 +00001082/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001083static abi_long do_getsockname(int fd, abi_ulong target_addr,
1084 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001085{
1086 socklen_t addrlen = tget32(target_addrlen);
ths01c227f2007-03-19 13:05:13 +00001087 void *addr = alloca(addrlen);
blueswir1992f48a2007-10-14 16:27:31 +00001088 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001089
1090 ret = get_errno(getsockname(fd, addr, &addrlen));
1091 if (!is_error(ret)) {
1092 host_to_target_sockaddr(target_addr, addr, addrlen);
1093 tput32(target_addrlen, addrlen);
1094 }
1095 return ret;
1096}
1097
ths0da46a62007-10-20 20:23:07 +00001098/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001099static abi_long do_socketpair(int domain, int type, int protocol,
1100 abi_ulong target_tab)
pbrook1be9e1d2006-11-19 15:26:04 +00001101{
1102 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00001103 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001104
1105 ret = get_errno(socketpair(domain, type, protocol, tab));
1106 if (!is_error(ret)) {
1107 tput32(target_tab, tab[0]);
1108 tput32(target_tab + 4, tab[1]);
1109 }
1110 return ret;
1111}
1112
ths0da46a62007-10-20 20:23:07 +00001113/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001114static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1115 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001116{
1117 void *addr;
1118 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001119 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001120
1121 host_msg = lock_user(msg, len, 1);
1122 if (target_addr) {
1123 addr = alloca(addrlen);
1124 target_to_host_sockaddr(addr, target_addr, addrlen);
1125 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1126 } else {
1127 ret = get_errno(send(fd, host_msg, len, flags));
1128 }
1129 unlock_user(host_msg, msg, 0);
1130 return ret;
1131}
1132
ths0da46a62007-10-20 20:23:07 +00001133/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001134static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1135 abi_ulong target_addr,
1136 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001137{
1138 socklen_t addrlen;
1139 void *addr;
1140 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001141 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001142
1143 host_msg = lock_user(msg, len, 0);
1144 if (target_addr) {
1145 addrlen = tget32(target_addrlen);
1146 addr = alloca(addrlen);
1147 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1148 } else {
1149 addr = NULL; /* To keep compiler quiet. */
1150 ret = get_errno(recv(fd, host_msg, len, flags));
1151 }
1152 if (!is_error(ret)) {
1153 if (target_addr) {
1154 host_to_target_sockaddr(target_addr, addr, addrlen);
1155 tput32(target_addrlen, addrlen);
1156 }
1157 unlock_user(host_msg, msg, len);
1158 } else {
1159 unlock_user(host_msg, msg, 0);
1160 }
1161 return ret;
1162}
1163
j_mayer32407102007-09-26 23:01:49 +00001164#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00001165/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001166static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00001167{
blueswir1992f48a2007-10-14 16:27:31 +00001168 abi_long ret;
1169 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00001170
1171 switch(num) {
1172 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00001173 {
pbrook53a59602006-03-25 19:31:22 +00001174 int domain = tgetl(vptr);
1175 int type = tgetl(vptr + n);
1176 int protocol = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001177 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00001178 }
bellard31e31b82003-02-18 22:55:36 +00001179 break;
1180 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00001181 {
pbrook53a59602006-03-25 19:31:22 +00001182 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001183 abi_ulong target_addr = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001184 socklen_t addrlen = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001185 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001186 }
bellard31e31b82003-02-18 22:55:36 +00001187 break;
1188 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00001189 {
pbrook53a59602006-03-25 19:31:22 +00001190 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001191 abi_ulong target_addr = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001192 socklen_t addrlen = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001193 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001194 }
bellard31e31b82003-02-18 22:55:36 +00001195 break;
1196 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00001197 {
pbrook53a59602006-03-25 19:31:22 +00001198 int sockfd = tgetl(vptr);
1199 int backlog = tgetl(vptr + n);
bellard7854b052003-03-29 17:22:23 +00001200 ret = get_errno(listen(sockfd, backlog));
1201 }
bellard31e31b82003-02-18 22:55:36 +00001202 break;
1203 case SOCKOP_accept:
1204 {
pbrook53a59602006-03-25 19:31:22 +00001205 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001206 abi_ulong target_addr = tgetl(vptr + n);
1207 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001208 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001209 }
1210 break;
1211 case SOCKOP_getsockname:
1212 {
pbrook53a59602006-03-25 19:31:22 +00001213 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001214 abi_ulong target_addr = tgetl(vptr + n);
1215 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001216 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001217 }
1218 break;
1219 case SOCKOP_getpeername:
1220 {
pbrook53a59602006-03-25 19:31:22 +00001221 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001222 abi_ulong target_addr = tgetl(vptr + n);
1223 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001224 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001225 }
1226 break;
1227 case SOCKOP_socketpair:
1228 {
pbrook53a59602006-03-25 19:31:22 +00001229 int domain = tgetl(vptr);
1230 int type = tgetl(vptr + n);
1231 int protocol = tgetl(vptr + 2 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001232 abi_ulong tab = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001233 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00001234 }
1235 break;
1236 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00001237 {
pbrook53a59602006-03-25 19:31:22 +00001238 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001239 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001240 size_t len = tgetl(vptr + 2 * n);
1241 int flags = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001242 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001243 }
bellard31e31b82003-02-18 22:55:36 +00001244 break;
1245 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00001246 {
pbrook53a59602006-03-25 19:31:22 +00001247 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001248 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001249 size_t len = tgetl(vptr + 2 * n);
1250 int flags = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001251 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001252 }
bellard31e31b82003-02-18 22:55:36 +00001253 break;
1254 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00001255 {
pbrook53a59602006-03-25 19:31:22 +00001256 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001257 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001258 size_t len = tgetl(vptr + 2 * n);
1259 int flags = tgetl(vptr + 3 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001260 abi_ulong addr = tgetl(vptr + 4 * n);
pbrook53a59602006-03-25 19:31:22 +00001261 socklen_t addrlen = tgetl(vptr + 5 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001262 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001263 }
bellard31e31b82003-02-18 22:55:36 +00001264 break;
1265 case SOCKOP_recvfrom:
1266 {
pbrook53a59602006-03-25 19:31:22 +00001267 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001268 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001269 size_t len = tgetl(vptr + 2 * n);
1270 int flags = tgetl(vptr + 3 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001271 abi_ulong addr = tgetl(vptr + 4 * n);
1272 abi_ulong addrlen = tgetl(vptr + 5 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001273 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00001274 }
1275 break;
1276 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00001277 {
pbrook53a59602006-03-25 19:31:22 +00001278 int sockfd = tgetl(vptr);
1279 int how = tgetl(vptr + n);
bellard7854b052003-03-29 17:22:23 +00001280
1281 ret = get_errno(shutdown(sockfd, how));
1282 }
bellard31e31b82003-02-18 22:55:36 +00001283 break;
1284 case SOCKOP_sendmsg:
1285 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00001286 {
1287 int fd;
blueswir1992f48a2007-10-14 16:27:31 +00001288 abi_ulong target_msg;
bellard3532fa72006-06-24 15:06:03 +00001289 int flags;
bellard1a9353d2003-03-16 20:28:50 +00001290
pbrook53a59602006-03-25 19:31:22 +00001291 fd = tgetl(vptr);
bellard3532fa72006-06-24 15:06:03 +00001292 target_msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001293 flags = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001294
ths5fafdf22007-09-16 21:08:06 +00001295 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00001296 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00001297 }
1298 break;
bellard31e31b82003-02-18 22:55:36 +00001299 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00001300 {
pbrook53a59602006-03-25 19:31:22 +00001301 int sockfd = tgetl(vptr);
1302 int level = tgetl(vptr + n);
1303 int optname = tgetl(vptr + 2 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001304 abi_ulong optval = tgetl(vptr + 3 * n);
pbrook53a59602006-03-25 19:31:22 +00001305 socklen_t optlen = tgetl(vptr + 4 * n);
bellard7854b052003-03-29 17:22:23 +00001306
1307 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1308 }
1309 break;
bellard31e31b82003-02-18 22:55:36 +00001310 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00001311 {
pbrook53a59602006-03-25 19:31:22 +00001312 int sockfd = tgetl(vptr);
1313 int level = tgetl(vptr + n);
1314 int optname = tgetl(vptr + 2 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001315 abi_ulong optval = tgetl(vptr + 3 * n);
1316 abi_ulong poptlen = tgetl(vptr + 4 * n);
bellard7854b052003-03-29 17:22:23 +00001317
bellard8853f862004-02-22 14:57:26 +00001318 ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
bellard7854b052003-03-29 17:22:23 +00001319 }
1320 break;
bellard31e31b82003-02-18 22:55:36 +00001321 default:
1322 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00001323 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00001324 break;
1325 }
1326 return ret;
1327}
j_mayer32407102007-09-26 23:01:49 +00001328#endif
bellard31e31b82003-02-18 22:55:36 +00001329
j_mayer32407102007-09-26 23:01:49 +00001330#ifdef TARGET_NR_ipc
bellard8853f862004-02-22 14:57:26 +00001331#define N_SHM_REGIONS 32
1332
1333static struct shm_region {
1334 uint32_t start;
1335 uint32_t size;
1336} shm_regions[N_SHM_REGIONS];
1337
ths3eb6b042007-06-03 14:26:27 +00001338struct target_ipc_perm
1339{
blueswir1992f48a2007-10-14 16:27:31 +00001340 abi_long __key;
1341 abi_ulong uid;
1342 abi_ulong gid;
1343 abi_ulong cuid;
1344 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00001345 unsigned short int mode;
1346 unsigned short int __pad1;
1347 unsigned short int __seq;
1348 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00001349 abi_ulong __unused1;
1350 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00001351};
1352
1353struct target_semid_ds
1354{
1355 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001356 abi_ulong sem_otime;
1357 abi_ulong __unused1;
1358 abi_ulong sem_ctime;
1359 abi_ulong __unused2;
1360 abi_ulong sem_nsems;
1361 abi_ulong __unused3;
1362 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00001363};
1364
1365static inline void target_to_host_ipc_perm(struct ipc_perm *host_ip,
blueswir1992f48a2007-10-14 16:27:31 +00001366 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001367{
1368 struct target_ipc_perm *target_ip;
1369 struct target_semid_ds *target_sd;
1370
1371 lock_user_struct(target_sd, target_addr, 1);
1372 target_ip=&(target_sd->sem_perm);
1373 host_ip->__key = tswapl(target_ip->__key);
1374 host_ip->uid = tswapl(target_ip->uid);
1375 host_ip->gid = tswapl(target_ip->gid);
1376 host_ip->cuid = tswapl(target_ip->cuid);
1377 host_ip->cgid = tswapl(target_ip->cgid);
1378 host_ip->mode = tswapl(target_ip->mode);
1379 unlock_user_struct(target_sd, target_addr, 0);
1380}
1381
blueswir1992f48a2007-10-14 16:27:31 +00001382static inline void host_to_target_ipc_perm(abi_ulong target_addr,
ths3eb6b042007-06-03 14:26:27 +00001383 struct ipc_perm *host_ip)
1384{
1385 struct target_ipc_perm *target_ip;
1386 struct target_semid_ds *target_sd;
1387
1388 lock_user_struct(target_sd, target_addr, 0);
1389 target_ip = &(target_sd->sem_perm);
1390 target_ip->__key = tswapl(host_ip->__key);
1391 target_ip->uid = tswapl(host_ip->uid);
1392 target_ip->gid = tswapl(host_ip->gid);
1393 target_ip->cuid = tswapl(host_ip->cuid);
1394 target_ip->cgid = tswapl(host_ip->cgid);
1395 target_ip->mode = tswapl(host_ip->mode);
1396 unlock_user_struct(target_sd, target_addr, 1);
1397}
1398
1399static inline void target_to_host_semid_ds(struct semid_ds *host_sd,
blueswir1992f48a2007-10-14 16:27:31 +00001400 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001401{
1402 struct target_semid_ds *target_sd;
1403
1404 lock_user_struct(target_sd, target_addr, 1);
1405 target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1406 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1407 host_sd->sem_otime = tswapl(target_sd->sem_otime);
1408 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1409 unlock_user_struct(target_sd, target_addr, 0);
1410}
1411
blueswir1992f48a2007-10-14 16:27:31 +00001412static inline void host_to_target_semid_ds(abi_ulong target_addr,
ths3eb6b042007-06-03 14:26:27 +00001413 struct semid_ds *host_sd)
1414{
1415 struct target_semid_ds *target_sd;
1416
1417 lock_user_struct(target_sd, target_addr, 0);
1418 host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1419 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1420 target_sd->sem_otime = tswapl(host_sd->sem_otime);
1421 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1422 unlock_user_struct(target_sd, target_addr, 1);
1423}
1424
thsfa294812007-02-02 22:05:00 +00001425union semun {
1426 int val;
ths3eb6b042007-06-03 14:26:27 +00001427 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00001428 unsigned short *array;
1429};
1430
ths3eb6b042007-06-03 14:26:27 +00001431union target_semun {
1432 int val;
blueswir1992f48a2007-10-14 16:27:31 +00001433 abi_long buf;
ths3eb6b042007-06-03 14:26:27 +00001434 unsigned short int *array;
1435};
1436
j_mayer32407102007-09-26 23:01:49 +00001437static inline void target_to_host_semun(int cmd,
ths3eb6b042007-06-03 14:26:27 +00001438 union semun *host_su,
blueswir1992f48a2007-10-14 16:27:31 +00001439 abi_ulong target_addr,
ths3eb6b042007-06-03 14:26:27 +00001440 struct semid_ds *ds)
1441{
1442 union target_semun *target_su;
1443
1444 switch( cmd ) {
1445 case IPC_STAT:
1446 case IPC_SET:
1447 lock_user_struct(target_su, target_addr, 1);
1448 target_to_host_semid_ds(ds,target_su->buf);
1449 host_su->buf = ds;
1450 unlock_user_struct(target_su, target_addr, 0);
1451 break;
1452 case GETVAL:
1453 case SETVAL:
1454 lock_user_struct(target_su, target_addr, 1);
1455 host_su->val = tswapl(target_su->val);
1456 unlock_user_struct(target_su, target_addr, 0);
1457 break;
1458 case GETALL:
1459 case SETALL:
1460 lock_user_struct(target_su, target_addr, 1);
1461 *host_su->array = tswap16(*target_su->array);
1462 unlock_user_struct(target_su, target_addr, 0);
1463 break;
1464 default:
1465 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1466 }
1467}
1468
j_mayer32407102007-09-26 23:01:49 +00001469static inline void host_to_target_semun(int cmd,
blueswir1992f48a2007-10-14 16:27:31 +00001470 abi_ulong target_addr,
ths3eb6b042007-06-03 14:26:27 +00001471 union semun *host_su,
1472 struct semid_ds *ds)
1473{
1474 union target_semun *target_su;
1475
1476 switch( cmd ) {
1477 case IPC_STAT:
1478 case IPC_SET:
1479 lock_user_struct(target_su, target_addr, 0);
1480 host_to_target_semid_ds(target_su->buf,ds);
1481 unlock_user_struct(target_su, target_addr, 1);
1482 break;
1483 case GETVAL:
1484 case SETVAL:
1485 lock_user_struct(target_su, target_addr, 0);
1486 target_su->val = tswapl(host_su->val);
1487 unlock_user_struct(target_su, target_addr, 1);
1488 break;
1489 case GETALL:
1490 case SETALL:
1491 lock_user_struct(target_su, target_addr, 0);
1492 *target_su->array = tswap16(*host_su->array);
1493 unlock_user_struct(target_su, target_addr, 1);
1494 break;
1495 default:
1496 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1497 }
1498}
1499
blueswir1992f48a2007-10-14 16:27:31 +00001500static inline abi_long do_semctl(int first, int second, int third,
1501 abi_long ptr)
ths3eb6b042007-06-03 14:26:27 +00001502{
1503 union semun arg;
1504 struct semid_ds dsarg;
1505 int cmd = third&0xff;
blueswir1992f48a2007-10-14 16:27:31 +00001506 abi_long ret = 0;
ths3eb6b042007-06-03 14:26:27 +00001507
1508 switch( cmd ) {
1509 case GETVAL:
1510 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1511 ret = get_errno(semctl(first, second, cmd, arg));
1512 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1513 break;
1514 case SETVAL:
1515 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1516 ret = get_errno(semctl(first, second, cmd, arg));
1517 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1518 break;
1519 case GETALL:
1520 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1521 ret = get_errno(semctl(first, second, cmd, arg));
1522 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1523 break;
1524 case SETALL:
1525 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1526 ret = get_errno(semctl(first, second, cmd, arg));
1527 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1528 break;
1529 case IPC_STAT:
1530 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1531 ret = get_errno(semctl(first, second, cmd, arg));
1532 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1533 break;
1534 case IPC_SET:
1535 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1536 ret = get_errno(semctl(first, second, cmd, arg));
1537 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1538 break;
1539 default:
1540 ret = get_errno(semctl(first, second, cmd, arg));
1541 }
1542
1543 return ret;
1544}
1545
ths1bc012f2007-06-03 14:27:49 +00001546struct target_msqid_ds
1547{
1548 struct target_ipc_perm msg_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001549 abi_ulong msg_stime;
1550 abi_ulong __unused1;
1551 abi_ulong msg_rtime;
1552 abi_ulong __unused2;
1553 abi_ulong msg_ctime;
1554 abi_ulong __unused3;
1555 abi_ulong __msg_cbytes;
1556 abi_ulong msg_qnum;
1557 abi_ulong msg_qbytes;
1558 abi_ulong msg_lspid;
1559 abi_ulong msg_lrpid;
1560 abi_ulong __unused4;
1561 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00001562};
1563
1564static inline void target_to_host_msqid_ds(struct msqid_ds *host_md,
blueswir1992f48a2007-10-14 16:27:31 +00001565 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00001566{
1567 struct target_msqid_ds *target_md;
1568
1569 lock_user_struct(target_md, target_addr, 1);
1570 target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1571 host_md->msg_stime = tswapl(target_md->msg_stime);
1572 host_md->msg_rtime = tswapl(target_md->msg_rtime);
1573 host_md->msg_ctime = tswapl(target_md->msg_ctime);
1574 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1575 host_md->msg_qnum = tswapl(target_md->msg_qnum);
1576 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1577 host_md->msg_lspid = tswapl(target_md->msg_lspid);
1578 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1579 unlock_user_struct(target_md, target_addr, 0);
1580}
1581
blueswir1992f48a2007-10-14 16:27:31 +00001582static inline void host_to_target_msqid_ds(abi_ulong target_addr,
ths1bc012f2007-06-03 14:27:49 +00001583 struct msqid_ds *host_md)
1584{
1585 struct target_msqid_ds *target_md;
1586
1587 lock_user_struct(target_md, target_addr, 0);
1588 host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1589 target_md->msg_stime = tswapl(host_md->msg_stime);
1590 target_md->msg_rtime = tswapl(host_md->msg_rtime);
1591 target_md->msg_ctime = tswapl(host_md->msg_ctime);
1592 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1593 target_md->msg_qnum = tswapl(host_md->msg_qnum);
1594 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1595 target_md->msg_lspid = tswapl(host_md->msg_lspid);
1596 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1597 unlock_user_struct(target_md, target_addr, 1);
1598}
1599
blueswir1992f48a2007-10-14 16:27:31 +00001600static inline abi_long do_msgctl(int first, int second, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00001601{
1602 struct msqid_ds dsarg;
1603 int cmd = second&0xff;
blueswir1992f48a2007-10-14 16:27:31 +00001604 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001605 switch( cmd ) {
1606 case IPC_STAT:
1607 case IPC_SET:
1608 target_to_host_msqid_ds(&dsarg,ptr);
1609 ret = get_errno(msgctl(first, cmd, &dsarg));
1610 host_to_target_msqid_ds(ptr,&dsarg);
1611 default:
1612 ret = get_errno(msgctl(first, cmd, &dsarg));
1613 }
1614 return ret;
1615}
1616
1617struct target_msgbuf {
blueswir1992f48a2007-10-14 16:27:31 +00001618 abi_ulong mtype;
ths1bc012f2007-06-03 14:27:49 +00001619 char mtext[1];
1620};
1621
blueswir1992f48a2007-10-14 16:27:31 +00001622static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1623 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001624{
1625 struct target_msgbuf *target_mb;
1626 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00001627 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001628
1629 lock_user_struct(target_mb,msgp,0);
1630 host_mb = malloc(msgsz+sizeof(long));
1631 host_mb->mtype = tswapl(target_mb->mtype);
1632 memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1633 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1634 free(host_mb);
1635 unlock_user_struct(target_mb, msgp, 0);
1636
1637 return ret;
1638}
1639
blueswir1992f48a2007-10-14 16:27:31 +00001640static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1641 unsigned int msgsz, int msgtype,
1642 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001643{
1644 struct target_msgbuf *target_mb;
1645 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00001646 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001647
1648 lock_user_struct(target_mb, msgp, 0);
1649 host_mb = malloc(msgsz+sizeof(long));
1650 ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
1651 if (ret > 0)
1652 memcpy(target_mb->mtext, host_mb->mtext, ret);
1653 target_mb->mtype = tswapl(host_mb->mtype);
1654 free(host_mb);
1655 unlock_user_struct(target_mb, msgp, 0);
1656
1657 return ret;
1658}
1659
pbrook53a59602006-03-25 19:31:22 +00001660/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00001661/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001662static abi_long do_ipc(unsigned int call, int first,
1663 int second, int third,
1664 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00001665{
1666 int version;
blueswir1992f48a2007-10-14 16:27:31 +00001667 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00001668 unsigned long raddr;
1669 struct shmid_ds shm_info;
1670 int i;
1671
1672 version = call >> 16;
1673 call &= 0xffff;
1674
1675 switch (call) {
thsfa294812007-02-02 22:05:00 +00001676 case IPCOP_semop:
1677 ret = get_errno(semop(first,(struct sembuf *) ptr, second));
1678 break;
1679
1680 case IPCOP_semget:
1681 ret = get_errno(semget(first, second, third));
1682 break;
1683
1684 case IPCOP_semctl:
ths3eb6b042007-06-03 14:26:27 +00001685 ret = do_semctl(first, second, third, ptr);
thsfa294812007-02-02 22:05:00 +00001686 break;
1687
1688 case IPCOP_semtimedop:
j_mayer32407102007-09-26 23:01:49 +00001689 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00001690 ret = -TARGET_ENOSYS;
thsfa294812007-02-02 22:05:00 +00001691 break;
thsd96372e2007-02-02 22:05:44 +00001692
1693 case IPCOP_msgget:
1694 ret = get_errno(msgget(first, second));
1695 break;
1696
1697 case IPCOP_msgsnd:
ths1bc012f2007-06-03 14:27:49 +00001698 ret = do_msgsnd(first, ptr, second, third);
thsd96372e2007-02-02 22:05:44 +00001699 break;
1700
1701 case IPCOP_msgctl:
ths1bc012f2007-06-03 14:27:49 +00001702 ret = do_msgctl(first, second, ptr);
thsd96372e2007-02-02 22:05:44 +00001703 break;
1704
1705 case IPCOP_msgrcv:
ths1bc012f2007-06-03 14:27:49 +00001706 {
1707 struct ipc_kludge
1708 {
1709 void *__unbounded msgp;
1710 long int msgtyp;
1711 };
thsd96372e2007-02-02 22:05:44 +00001712
ths1bc012f2007-06-03 14:27:49 +00001713 struct ipc_kludge *foo = (struct ipc_kludge *) ptr;
1714 struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
thsd96372e2007-02-02 22:05:44 +00001715
ths1bc012f2007-06-03 14:27:49 +00001716 ret = do_msgrcv(first, (long)msgp, second, 0, third);
thsd96372e2007-02-02 22:05:44 +00001717
ths1bc012f2007-06-03 14:27:49 +00001718 }
thsd96372e2007-02-02 22:05:44 +00001719 break;
1720
bellard8853f862004-02-22 14:57:26 +00001721 case IPCOP_shmat:
1722 /* SHM_* flags are the same on all linux platforms */
1723 ret = get_errno((long) shmat(first, (void *) ptr, second));
1724 if (is_error(ret))
1725 break;
1726 raddr = ret;
1727 /* find out the length of the shared memory segment */
ths3b46e622007-09-17 08:09:54 +00001728
bellard8853f862004-02-22 14:57:26 +00001729 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
1730 if (is_error(ret)) {
1731 /* can't get length, bail out */
1732 shmdt((void *) raddr);
1733 break;
1734 }
1735 page_set_flags(raddr, raddr + shm_info.shm_segsz,
1736 PAGE_VALID | PAGE_READ |
1737 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
1738 for (i = 0; i < N_SHM_REGIONS; ++i) {
1739 if (shm_regions[i].start == 0) {
1740 shm_regions[i].start = raddr;
1741 shm_regions[i].size = shm_info.shm_segsz;
1742 break;
1743 }
1744 }
blueswir1992f48a2007-10-14 16:27:31 +00001745 if (put_user(raddr, (abi_ulong *)third))
ths0da46a62007-10-20 20:23:07 +00001746 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001747 ret = 0;
1748 break;
1749 case IPCOP_shmdt:
1750 for (i = 0; i < N_SHM_REGIONS; ++i) {
1751 if (shm_regions[i].start == ptr) {
1752 shm_regions[i].start = 0;
1753 page_set_flags(ptr, shm_regions[i].size, 0);
1754 break;
1755 }
1756 }
1757 ret = get_errno(shmdt((void *) ptr));
1758 break;
1759
1760 case IPCOP_shmget:
1761 /* IPC_* flag values are the same on all linux platforms */
1762 ret = get_errno(shmget(first, second, third));
1763 break;
1764
1765 /* IPC_* and SHM_* command values are the same on all linux platforms */
1766 case IPCOP_shmctl:
1767 switch(second) {
1768 case IPC_RMID:
1769 case SHM_LOCK:
1770 case SHM_UNLOCK:
1771 ret = get_errno(shmctl(first, second, NULL));
1772 break;
1773 default:
1774 goto unimplemented;
1775 }
1776 break;
1777 default:
1778 unimplemented:
j_mayer32407102007-09-26 23:01:49 +00001779 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00001780 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00001781 break;
1782 }
1783 return ret;
1784}
j_mayer32407102007-09-26 23:01:49 +00001785#endif
bellard8853f862004-02-22 14:57:26 +00001786
bellard31e31b82003-02-18 22:55:36 +00001787/* kernel structure types definitions */
1788#define IFNAMSIZ 16
1789
1790#define STRUCT(name, list...) STRUCT_ ## name,
1791#define STRUCT_SPECIAL(name) STRUCT_ ## name,
1792enum {
1793#include "syscall_types.h"
1794};
1795#undef STRUCT
1796#undef STRUCT_SPECIAL
1797
1798#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
1799#define STRUCT_SPECIAL(name)
1800#include "syscall_types.h"
1801#undef STRUCT
1802#undef STRUCT_SPECIAL
1803
1804typedef struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00001805 unsigned int target_cmd;
1806 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00001807 const char *name;
1808 int access;
bellard1a9353d2003-03-16 20:28:50 +00001809 const argtype arg_type[5];
bellard31e31b82003-02-18 22:55:36 +00001810} IOCTLEntry;
1811
1812#define IOC_R 0x0001
1813#define IOC_W 0x0002
1814#define IOC_RW (IOC_R | IOC_W)
1815
1816#define MAX_STRUCT_SIZE 4096
1817
bellard2ab83ea2003-06-15 19:56:46 +00001818IOCTLEntry ioctl_entries[] = {
bellard31e31b82003-02-18 22:55:36 +00001819#define IOCTL(cmd, access, types...) \
1820 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
1821#include "ioctls.h"
1822 { 0, 0, },
1823};
1824
pbrook53a59602006-03-25 19:31:22 +00001825/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00001826/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001827static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00001828{
1829 const IOCTLEntry *ie;
1830 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00001831 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00001832 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00001833 int target_size;
1834 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00001835
1836 ie = ioctl_entries;
1837 for(;;) {
1838 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00001839 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00001840 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00001841 }
1842 if (ie->target_cmd == cmd)
1843 break;
1844 ie++;
1845 }
1846 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00001847#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00001848 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00001849#endif
bellard31e31b82003-02-18 22:55:36 +00001850 switch(arg_type[0]) {
1851 case TYPE_NULL:
1852 /* no argument */
1853 ret = get_errno(ioctl(fd, ie->host_cmd));
1854 break;
1855 case TYPE_PTRVOID:
1856 case TYPE_INT:
1857 /* int argment */
1858 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
1859 break;
1860 case TYPE_PTR:
1861 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00001862 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00001863 switch(ie->access) {
1864 case IOC_R:
1865 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1866 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00001867 argptr = lock_user(arg, target_size, 0);
1868 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1869 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00001870 }
1871 break;
1872 case IOC_W:
pbrook53a59602006-03-25 19:31:22 +00001873 argptr = lock_user(arg, target_size, 1);
1874 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1875 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00001876 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1877 break;
1878 default:
1879 case IOC_RW:
pbrook53a59602006-03-25 19:31:22 +00001880 argptr = lock_user(arg, target_size, 1);
1881 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1882 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00001883 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1884 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00001885 argptr = lock_user(arg, target_size, 0);
1886 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1887 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00001888 }
1889 break;
1890 }
1891 break;
1892 default:
j_mayer32407102007-09-26 23:01:49 +00001893 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
1894 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00001895 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00001896 break;
1897 }
1898 return ret;
1899}
1900
1901bitmask_transtbl iflag_tbl[] = {
1902 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
1903 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
1904 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
1905 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
1906 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
1907 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
1908 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
1909 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
1910 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
1911 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
1912 { TARGET_IXON, TARGET_IXON, IXON, IXON },
1913 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
1914 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
1915 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
1916 { 0, 0, 0, 0 }
1917};
1918
1919bitmask_transtbl oflag_tbl[] = {
1920 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
1921 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
1922 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
1923 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
1924 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
1925 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
1926 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
1927 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
1928 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
1929 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
1930 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
1931 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
1932 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
1933 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
1934 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
1935 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
1936 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
1937 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
1938 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
1939 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
1940 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
1941 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
1942 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
1943 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
1944 { 0, 0, 0, 0 }
1945};
1946
1947bitmask_transtbl cflag_tbl[] = {
1948 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
1949 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
1950 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
1951 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
1952 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
1953 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
1954 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
1955 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
1956 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
1957 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
1958 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
1959 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
1960 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
1961 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
1962 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
1963 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
1964 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
1965 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
1966 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
1967 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
1968 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
1969 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
1970 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
1971 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
1972 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
1973 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
1974 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
1975 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
1976 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
1977 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
1978 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
1979 { 0, 0, 0, 0 }
1980};
1981
1982bitmask_transtbl lflag_tbl[] = {
1983 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
1984 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
1985 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
1986 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
1987 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
1988 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
1989 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
1990 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
1991 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
1992 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
1993 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
1994 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
1995 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
1996 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
1997 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
1998 { 0, 0, 0, 0 }
1999};
2000
2001static void target_to_host_termios (void *dst, const void *src)
2002{
2003 struct host_termios *host = dst;
2004 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00002005
ths5fafdf22007-09-16 21:08:06 +00002006 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002007 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002008 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002009 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002010 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002011 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002012 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002013 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2014 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00002015
ths5fafdf22007-09-16 21:08:06 +00002016 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2017 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00002018 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00002019 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00002020 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00002021 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00002022 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00002023 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00002024 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00002025 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2026 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00002027 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2028 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2029 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2030 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2031 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00002032 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00002033}
ths3b46e622007-09-17 08:09:54 +00002034
bellard31e31b82003-02-18 22:55:36 +00002035static void host_to_target_termios (void *dst, const void *src)
2036{
2037 struct target_termios *target = dst;
2038 const struct host_termios *host = src;
2039
ths5fafdf22007-09-16 21:08:06 +00002040 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002041 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002042 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002043 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002044 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002045 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002046 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002047 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2048 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00002049
bellard31e31b82003-02-18 22:55:36 +00002050 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2051 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2052 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2053 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2054 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2055 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2056 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2057 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2058 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2059 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2060 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2061 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2062 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2063 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2064 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2065 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2066 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2067}
2068
2069StructEntry struct_termios_def = {
2070 .convert = { host_to_target_termios, target_to_host_termios },
2071 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2072 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2073};
2074
bellard5286db72003-06-05 00:57:30 +00002075static bitmask_transtbl mmap_flags_tbl[] = {
2076 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2077 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2078 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2079 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2080 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2081 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2082 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2083 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2084 { 0, 0, 0, 0 }
2085};
2086
bellardffa65c32004-01-04 23:57:22 +00002087static bitmask_transtbl fcntl_flags_tbl[] = {
2088 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
2089 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
2090 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
2091 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
2092 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
2093 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
2094 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
2095 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
2096 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
2097 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
2098 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2099 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
2100 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
bellard121061d2004-02-22 11:56:01 +00002101#if defined(O_DIRECT)
bellardffa65c32004-01-04 23:57:22 +00002102 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
bellard121061d2004-02-22 11:56:01 +00002103#endif
bellardffa65c32004-01-04 23:57:22 +00002104 { 0, 0, 0, 0 }
2105};
2106
bellard2ab83ea2003-06-15 19:56:46 +00002107#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00002108
2109/* NOTE: there is really one LDT for all the threads */
2110uint8_t *ldt_table;
2111
blueswir1992f48a2007-10-14 16:27:31 +00002112static int read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002113{
2114 int size;
pbrook53a59602006-03-25 19:31:22 +00002115 void *p;
bellard6dbad632003-03-16 18:05:05 +00002116
2117 if (!ldt_table)
2118 return 0;
2119 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2120 if (size > bytecount)
2121 size = bytecount;
pbrook53a59602006-03-25 19:31:22 +00002122 p = lock_user(ptr, size, 0);
2123 /* ??? Shoudl this by byteswapped? */
2124 memcpy(p, ldt_table, size);
2125 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00002126 return size;
2127}
2128
2129/* XXX: add locking support */
ths0da46a62007-10-20 20:23:07 +00002130/* write_ldt() returns host errnos */
ths5fafdf22007-09-16 21:08:06 +00002131static int write_ldt(CPUX86State *env,
blueswir1992f48a2007-10-14 16:27:31 +00002132 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00002133{
2134 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00002135 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00002136 int seg_32bit, contents, read_exec_only, limit_in_pages;
2137 int seg_not_present, useable;
2138 uint32_t *lp, entry_1, entry_2;
2139
2140 if (bytecount != sizeof(ldt_info))
2141 return -EINVAL;
pbrook53a59602006-03-25 19:31:22 +00002142 lock_user_struct(target_ldt_info, ptr, 1);
2143 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2144 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2145 ldt_info.limit = tswap32(target_ldt_info->limit);
2146 ldt_info.flags = tswap32(target_ldt_info->flags);
2147 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00002148
bellard6dbad632003-03-16 18:05:05 +00002149 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
2150 return -EINVAL;
2151 seg_32bit = ldt_info.flags & 1;
2152 contents = (ldt_info.flags >> 1) & 3;
2153 read_exec_only = (ldt_info.flags >> 3) & 1;
2154 limit_in_pages = (ldt_info.flags >> 4) & 1;
2155 seg_not_present = (ldt_info.flags >> 5) & 1;
2156 useable = (ldt_info.flags >> 6) & 1;
2157
2158 if (contents == 3) {
2159 if (oldmode)
2160 return -EINVAL;
2161 if (seg_not_present == 0)
2162 return -EINVAL;
2163 }
2164 /* allocate the LDT */
2165 if (!ldt_table) {
2166 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2167 if (!ldt_table)
2168 return -ENOMEM;
2169 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
pbrook53a59602006-03-25 19:31:22 +00002170 env->ldt.base = h2g(ldt_table);
bellard6dbad632003-03-16 18:05:05 +00002171 env->ldt.limit = 0xffff;
2172 }
2173
2174 /* NOTE: same code as Linux kernel */
2175 /* Allow LDTs to be cleared by the user. */
2176 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2177 if (oldmode ||
2178 (contents == 0 &&
2179 read_exec_only == 1 &&
2180 seg_32bit == 0 &&
2181 limit_in_pages == 0 &&
2182 seg_not_present == 1 &&
2183 useable == 0 )) {
2184 entry_1 = 0;
2185 entry_2 = 0;
2186 goto install;
2187 }
2188 }
ths3b46e622007-09-17 08:09:54 +00002189
bellard6dbad632003-03-16 18:05:05 +00002190 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2191 (ldt_info.limit & 0x0ffff);
2192 entry_2 = (ldt_info.base_addr & 0xff000000) |
2193 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2194 (ldt_info.limit & 0xf0000) |
2195 ((read_exec_only ^ 1) << 9) |
2196 (contents << 10) |
2197 ((seg_not_present ^ 1) << 15) |
2198 (seg_32bit << 22) |
2199 (limit_in_pages << 23) |
2200 0x7000;
2201 if (!oldmode)
2202 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00002203
bellard6dbad632003-03-16 18:05:05 +00002204 /* Install the new entry ... */
2205install:
2206 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2207 lp[0] = tswap32(entry_1);
2208 lp[1] = tswap32(entry_2);
2209 return 0;
2210}
2211
2212/* specific and weird i386 syscalls */
ths0da46a62007-10-20 20:23:07 +00002213/* do_modify_ldt() returns host errnos (it is inconsistent with the
2214 other do_*() functions which return target errnos). */
blueswir1992f48a2007-10-14 16:27:31 +00002215int do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002216{
2217 int ret = -ENOSYS;
ths3b46e622007-09-17 08:09:54 +00002218
bellard6dbad632003-03-16 18:05:05 +00002219 switch (func) {
2220 case 0:
2221 ret = read_ldt(ptr, bytecount);
2222 break;
2223 case 1:
2224 ret = write_ldt(env, ptr, bytecount, 1);
2225 break;
2226 case 0x11:
2227 ret = write_ldt(env, ptr, bytecount, 0);
2228 break;
2229 }
2230 return ret;
2231}
bellard1b6b0292003-03-22 17:31:38 +00002232
bellard2ab83ea2003-06-15 19:56:46 +00002233#endif /* defined(TARGET_I386) */
2234
bellard1b6b0292003-03-22 17:31:38 +00002235/* this stack is the equivalent of the kernel stack associated with a
2236 thread/process */
2237#define NEW_STACK_SIZE 8192
2238
2239static int clone_func(void *arg)
2240{
bellard2ab83ea2003-06-15 19:56:46 +00002241 CPUState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00002242 cpu_loop(env);
2243 /* never exits */
2244 return 0;
2245}
2246
ths0da46a62007-10-20 20:23:07 +00002247/* do_fork() Must return host values and target errnos (unlike most
2248 do_*() functions). */
blueswir1992f48a2007-10-14 16:27:31 +00002249int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp)
bellard1b6b0292003-03-22 17:31:38 +00002250{
2251 int ret;
bellard5cd43932003-03-29 16:54:36 +00002252 TaskState *ts;
bellard1b6b0292003-03-22 17:31:38 +00002253 uint8_t *new_stack;
bellard2ab83ea2003-06-15 19:56:46 +00002254 CPUState *new_env;
ths3b46e622007-09-17 08:09:54 +00002255
bellard1b6b0292003-03-22 17:31:38 +00002256 if (flags & CLONE_VM) {
bellard5cd43932003-03-29 16:54:36 +00002257 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2258 memset(ts, 0, sizeof(TaskState));
2259 new_stack = ts->stack;
2260 ts->used = 1;
2261 /* add in task state list */
2262 ts->next = first_task_state;
2263 first_task_state = ts;
bellard1b6b0292003-03-22 17:31:38 +00002264 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00002265 new_env = cpu_copy(env);
bellard2ab83ea2003-06-15 19:56:46 +00002266#if defined(TARGET_I386)
2267 if (!newsp)
2268 newsp = env->regs[R_ESP];
bellard1b6b0292003-03-22 17:31:38 +00002269 new_env->regs[R_ESP] = newsp;
2270 new_env->regs[R_EAX] = 0;
bellard2ab83ea2003-06-15 19:56:46 +00002271#elif defined(TARGET_ARM)
2272 if (!newsp)
2273 newsp = env->regs[13];
2274 new_env->regs[13] = newsp;
2275 new_env->regs[0] = 0;
bellardebc05482003-09-30 21:08:41 +00002276#elif defined(TARGET_SPARC)
bellard48dc41e2006-06-21 18:15:50 +00002277 if (!newsp)
2278 newsp = env->regwptr[22];
2279 new_env->regwptr[22] = newsp;
2280 new_env->regwptr[0] = 0;
2281 /* XXXXX */
bellard67867302003-11-23 17:05:30 +00002282 printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
pbrooke6e59062006-10-22 00:18:54 +00002283#elif defined(TARGET_M68K)
2284 if (!newsp)
2285 newsp = env->aregs[7];
2286 new_env->aregs[7] = newsp;
2287 new_env->dregs[0] = 0;
2288 /* ??? is this sufficient? */
bellard048f6b42005-11-26 18:47:20 +00002289#elif defined(TARGET_MIPS)
ths388bb212007-05-13 13:58:00 +00002290 if (!newsp)
thsead93602007-09-06 00:18:15 +00002291 newsp = env->gpr[29][env->current_tc];
2292 new_env->gpr[29][env->current_tc] = newsp;
bellard67867302003-11-23 17:05:30 +00002293#elif defined(TARGET_PPC)
2294 if (!newsp)
2295 newsp = env->gpr[1];
2296 new_env->gpr[1] = newsp;
ths5fafdf22007-09-16 21:08:06 +00002297 {
bellard67867302003-11-23 17:05:30 +00002298 int i;
2299 for (i = 7; i < 32; i++)
2300 new_env->gpr[i] = 0;
2301 }
bellardfdf9b3e2006-04-27 21:07:38 +00002302#elif defined(TARGET_SH4)
2303 if (!newsp)
2304 newsp = env->gregs[15];
2305 new_env->gregs[15] = newsp;
2306 /* XXXXX */
j_mayer7a3148a2007-04-05 07:13:51 +00002307#elif defined(TARGET_ALPHA)
2308 if (!newsp)
2309 newsp = env->ir[30];
2310 new_env->ir[30] = newsp;
2311 /* ? */
2312 {
2313 int i;
2314 for (i = 7; i < 30; i++)
2315 new_env->ir[i] = 0;
2316 }
ths48733d12007-10-08 13:36:46 +00002317#elif defined(TARGET_CRIS)
2318 if (!newsp)
2319 newsp = env->regs[14];
2320 new_env->regs[14] = newsp;
bellard2ab83ea2003-06-15 19:56:46 +00002321#else
2322#error unsupported target CPU
2323#endif
bellard5cd43932003-03-29 16:54:36 +00002324 new_env->opaque = ts;
bellard27725c12003-04-29 21:08:18 +00002325#ifdef __ia64__
bellardfd4a43e2006-04-24 20:32:17 +00002326 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00002327#else
2328 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2329#endif
bellard1b6b0292003-03-22 17:31:38 +00002330 } else {
2331 /* if no CLONE_VM, we consider it is a fork */
2332 if ((flags & ~CSIGNAL) != 0)
2333 return -EINVAL;
2334 ret = fork();
2335 }
2336 return ret;
2337}
2338
blueswir1992f48a2007-10-14 16:27:31 +00002339static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00002340{
2341 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00002342 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00002343 struct flock64 fl64;
2344 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00002345 abi_long ret;
pbrook53a59602006-03-25 19:31:22 +00002346
bellard7775e9e2003-05-14 22:46:48 +00002347 switch(cmd) {
2348 case TARGET_F_GETLK:
ths58134272007-03-31 18:59:32 +00002349 lock_user_struct(target_fl, arg, 1);
2350 fl.l_type = tswap16(target_fl->l_type);
2351 fl.l_whence = tswap16(target_fl->l_whence);
2352 fl.l_start = tswapl(target_fl->l_start);
2353 fl.l_len = tswapl(target_fl->l_len);
2354 fl.l_pid = tswapl(target_fl->l_pid);
2355 unlock_user_struct(target_fl, arg, 0);
bellard7775e9e2003-05-14 22:46:48 +00002356 ret = fcntl(fd, cmd, &fl);
2357 if (ret == 0) {
pbrook53a59602006-03-25 19:31:22 +00002358 lock_user_struct(target_fl, arg, 0);
bellard7775e9e2003-05-14 22:46:48 +00002359 target_fl->l_type = tswap16(fl.l_type);
2360 target_fl->l_whence = tswap16(fl.l_whence);
2361 target_fl->l_start = tswapl(fl.l_start);
2362 target_fl->l_len = tswapl(fl.l_len);
2363 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00002364 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00002365 }
2366 break;
ths3b46e622007-09-17 08:09:54 +00002367
bellard7775e9e2003-05-14 22:46:48 +00002368 case TARGET_F_SETLK:
2369 case TARGET_F_SETLKW:
pbrook53a59602006-03-25 19:31:22 +00002370 lock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00002371 fl.l_type = tswap16(target_fl->l_type);
2372 fl.l_whence = tswap16(target_fl->l_whence);
2373 fl.l_start = tswapl(target_fl->l_start);
2374 fl.l_len = tswapl(target_fl->l_len);
2375 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00002376 unlock_user_struct(target_fl, arg, 0);
bellard7775e9e2003-05-14 22:46:48 +00002377 ret = fcntl(fd, cmd, &fl);
2378 break;
ths3b46e622007-09-17 08:09:54 +00002379
bellard7775e9e2003-05-14 22:46:48 +00002380 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00002381 lock_user_struct(target_fl64, arg, 1);
2382 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2383 fl64.l_whence = tswap16(target_fl64->l_whence);
2384 fl64.l_start = tswapl(target_fl64->l_start);
2385 fl64.l_len = tswapl(target_fl64->l_len);
2386 fl64.l_pid = tswap16(target_fl64->l_pid);
2387 unlock_user_struct(target_fl64, arg, 0);
ths43f238d2007-01-05 20:55:49 +00002388 ret = fcntl(fd, cmd >> 1, &fl64);
2389 if (ret == 0) {
2390 lock_user_struct(target_fl64, arg, 0);
2391 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2392 target_fl64->l_whence = tswap16(fl64.l_whence);
2393 target_fl64->l_start = tswapl(fl64.l_start);
2394 target_fl64->l_len = tswapl(fl64.l_len);
2395 target_fl64->l_pid = tswapl(fl64.l_pid);
2396 unlock_user_struct(target_fl64, arg, 1);
2397 }
2398 break;
bellard7775e9e2003-05-14 22:46:48 +00002399 case TARGET_F_SETLK64:
2400 case TARGET_F_SETLKW64:
ths43f238d2007-01-05 20:55:49 +00002401 lock_user_struct(target_fl64, arg, 1);
2402 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2403 fl64.l_whence = tswap16(target_fl64->l_whence);
2404 fl64.l_start = tswapl(target_fl64->l_start);
2405 fl64.l_len = tswapl(target_fl64->l_len);
2406 fl64.l_pid = tswap16(target_fl64->l_pid);
2407 unlock_user_struct(target_fl64, arg, 0);
2408 ret = fcntl(fd, cmd >> 1, &fl64);
bellard7775e9e2003-05-14 22:46:48 +00002409 break;
2410
bellardffa65c32004-01-04 23:57:22 +00002411 case F_GETFL:
2412 ret = fcntl(fd, cmd, arg);
2413 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2414 break;
2415
2416 case F_SETFL:
2417 ret = fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl));
2418 break;
2419
bellard7775e9e2003-05-14 22:46:48 +00002420 default:
2421 ret = fcntl(fd, cmd, arg);
2422 break;
2423 }
2424 return ret;
2425}
2426
bellard67867302003-11-23 17:05:30 +00002427#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00002428
bellard67867302003-11-23 17:05:30 +00002429static inline int high2lowuid(int uid)
2430{
2431 if (uid > 65535)
2432 return 65534;
2433 else
2434 return uid;
2435}
2436
2437static inline int high2lowgid(int gid)
2438{
2439 if (gid > 65535)
2440 return 65534;
2441 else
2442 return gid;
2443}
2444
2445static inline int low2highuid(int uid)
2446{
2447 if ((int16_t)uid == -1)
2448 return -1;
2449 else
2450 return uid;
2451}
2452
2453static inline int low2highgid(int gid)
2454{
2455 if ((int16_t)gid == -1)
2456 return -1;
2457 else
2458 return gid;
2459}
2460
2461#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00002462
bellard31e31b82003-02-18 22:55:36 +00002463void syscall_init(void)
2464{
bellard2ab83ea2003-06-15 19:56:46 +00002465 IOCTLEntry *ie;
2466 const argtype *arg_type;
2467 int size;
2468
ths5fafdf22007-09-16 21:08:06 +00002469#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2470#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00002471#include "syscall_types.h"
2472#undef STRUCT
2473#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00002474
2475 /* we patch the ioctl size if necessary. We rely on the fact that
2476 no ioctl has all the bits at '1' in the size field */
2477 ie = ioctl_entries;
2478 while (ie->target_cmd != 0) {
2479 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2480 TARGET_IOC_SIZEMASK) {
2481 arg_type = ie->arg_type;
2482 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00002483 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002484 ie->target_cmd);
2485 exit(1);
2486 }
2487 arg_type++;
2488 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00002489 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00002490 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2491 (size << TARGET_IOC_SIZESHIFT);
2492 }
2493 /* automatic consistency check if same arch */
2494#if defined(__i386__) && defined(TARGET_I386)
2495 if (ie->target_cmd != ie->host_cmd) {
ths5fafdf22007-09-16 21:08:06 +00002496 fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002497 ie->target_cmd, ie->host_cmd);
2498 }
2499#endif
2500 ie++;
2501 }
bellard31e31b82003-02-18 22:55:36 +00002502}
bellardc573ff62004-01-04 15:51:36 +00002503
blueswir1992f48a2007-10-14 16:27:31 +00002504#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00002505static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2506{
2507#ifdef TARGET_WORDS_BIG_ENDIAN
2508 return ((uint64_t)word0 << 32) | word1;
2509#else
2510 return ((uint64_t)word1 << 32) | word0;
2511#endif
2512}
blueswir1992f48a2007-10-14 16:27:31 +00002513#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00002514static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2515{
2516 return word0;
2517}
blueswir1992f48a2007-10-14 16:27:31 +00002518#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00002519
2520#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00002521static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
2522 abi_long arg2,
2523 abi_long arg3,
2524 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00002525{
2526#ifdef TARGET_ARM
2527 if (((CPUARMState *)cpu_env)->eabi)
2528 {
2529 arg2 = arg3;
2530 arg3 = arg4;
2531 }
2532#endif
2533 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
2534}
2535#endif
2536
2537#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00002538static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
2539 abi_long arg2,
2540 abi_long arg3,
2541 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00002542{
2543#ifdef TARGET_ARM
2544 if (((CPUARMState *)cpu_env)->eabi)
2545 {
2546 arg2 = arg3;
2547 arg3 = arg4;
2548 }
2549#endif
2550 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
2551}
2552#endif
2553
pbrook53a59602006-03-25 19:31:22 +00002554static inline void target_to_host_timespec(struct timespec *host_ts,
blueswir1992f48a2007-10-14 16:27:31 +00002555 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00002556{
2557 struct target_timespec *target_ts;
2558
2559 lock_user_struct(target_ts, target_addr, 1);
2560 host_ts->tv_sec = tswapl(target_ts->tv_sec);
2561 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
2562 unlock_user_struct(target_ts, target_addr, 0);
2563}
2564
blueswir1992f48a2007-10-14 16:27:31 +00002565static inline void host_to_target_timespec(abi_ulong target_addr,
pbrook53a59602006-03-25 19:31:22 +00002566 struct timespec *host_ts)
2567{
2568 struct target_timespec *target_ts;
2569
2570 lock_user_struct(target_ts, target_addr, 0);
2571 target_ts->tv_sec = tswapl(host_ts->tv_sec);
2572 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
2573 unlock_user_struct(target_ts, target_addr, 1);
2574}
2575
ths0da46a62007-10-20 20:23:07 +00002576/* do_syscall() should always have a single exit point at the end so
2577 that actions, such as logging of syscall results, can be performed.
2578 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00002579abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
2580 abi_long arg2, abi_long arg3, abi_long arg4,
2581 abi_long arg5, abi_long arg6)
bellard31e31b82003-02-18 22:55:36 +00002582{
blueswir1992f48a2007-10-14 16:27:31 +00002583 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00002584 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00002585 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00002586 void *p;
ths3b46e622007-09-17 08:09:54 +00002587
bellard72f03902003-02-18 23:33:18 +00002588#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00002589 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00002590#endif
bellard31e31b82003-02-18 22:55:36 +00002591 switch(num) {
2592 case TARGET_NR_exit:
bellard7d132992003-03-06 23:23:54 +00002593#ifdef HAVE_GPROF
2594 _mcleanup();
2595#endif
bellarde9009672005-04-26 20:42:36 +00002596 gdb_exit(cpu_env, arg1);
bellard1b6b0292003-03-22 17:31:38 +00002597 /* XXX: should free thread stack and CPU env */
bellard31e31b82003-02-18 22:55:36 +00002598 _exit(arg1);
2599 ret = 0; /* avoid warning */
2600 break;
2601 case TARGET_NR_read:
pbrook53a59602006-03-25 19:31:22 +00002602 page_unprotect_range(arg2, arg3);
2603 p = lock_user(arg2, arg3, 0);
2604 ret = get_errno(read(arg1, p, arg3));
2605 unlock_user(p, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00002606 break;
2607 case TARGET_NR_write:
pbrook53a59602006-03-25 19:31:22 +00002608 p = lock_user(arg2, arg3, 1);
2609 ret = get_errno(write(arg1, p, arg3));
2610 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00002611 break;
2612 case TARGET_NR_open:
pbrook53a59602006-03-25 19:31:22 +00002613 p = lock_user_string(arg1);
2614 ret = get_errno(open(path(p),
bellardffa65c32004-01-04 23:57:22 +00002615 target_to_host_bitmask(arg2, fcntl_flags_tbl),
2616 arg3));
pbrook53a59602006-03-25 19:31:22 +00002617 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002618 break;
ths82424832007-09-24 09:21:55 +00002619#if defined(TARGET_NR_openat) && defined(__NR_openat)
2620 case TARGET_NR_openat:
2621 if (!arg2) {
ths0da46a62007-10-20 20:23:07 +00002622 ret = -TARGET_EFAULT;
ths82424832007-09-24 09:21:55 +00002623 goto fail;
2624 }
2625 p = lock_user_string(arg2);
2626 if (!access_ok(VERIFY_READ, p, 1))
ths0da46a62007-10-20 20:23:07 +00002627 /* Don't "goto fail" so that cleanup can happen. */
2628 ret = -TARGET_EFAULT;
ths82424832007-09-24 09:21:55 +00002629 else
2630 ret = get_errno(sys_openat(arg1,
2631 path(p),
2632 target_to_host_bitmask(arg3, fcntl_flags_tbl),
2633 arg4));
2634 if (p)
2635 unlock_user(p, arg2, 0);
2636 break;
2637#endif
bellard31e31b82003-02-18 22:55:36 +00002638 case TARGET_NR_close:
2639 ret = get_errno(close(arg1));
2640 break;
2641 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00002642 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00002643 break;
2644 case TARGET_NR_fork:
bellard1b6b0292003-03-22 17:31:38 +00002645 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00002646 break;
thse5febef2007-04-01 18:31:35 +00002647#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00002648 case TARGET_NR_waitpid:
2649 {
pbrook53a59602006-03-25 19:31:22 +00002650 int status;
2651 ret = get_errno(waitpid(arg1, &status, arg3));
2652 if (!is_error(ret) && arg2)
2653 tput32(arg2, status);
bellard31e31b82003-02-18 22:55:36 +00002654 }
2655 break;
thse5febef2007-04-01 18:31:35 +00002656#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002657#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002658 case TARGET_NR_creat:
pbrook53a59602006-03-25 19:31:22 +00002659 p = lock_user_string(arg1);
2660 ret = get_errno(creat(p, arg2));
2661 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002662 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002663#endif
bellard31e31b82003-02-18 22:55:36 +00002664 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00002665 {
2666 void * p2;
2667 p = lock_user_string(arg1);
2668 p2 = lock_user_string(arg2);
2669 ret = get_errno(link(p, p2));
2670 unlock_user(p2, arg2, 0);
2671 unlock_user(p, arg1, 0);
2672 }
bellard31e31b82003-02-18 22:55:36 +00002673 break;
ths64f0ce42007-09-24 09:25:06 +00002674#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
2675 case TARGET_NR_linkat:
2676 if (!arg2 || !arg4) {
ths0da46a62007-10-20 20:23:07 +00002677 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00002678 goto fail;
ths0da46a62007-10-20 20:23:07 +00002679 }
ths64f0ce42007-09-24 09:25:06 +00002680 {
2681 void * p2 = NULL;
2682 p = lock_user_string(arg2);
2683 p2 = lock_user_string(arg4);
2684 if (!access_ok(VERIFY_READ, p, 1)
2685 || !access_ok(VERIFY_READ, p2, 1))
ths0da46a62007-10-20 20:23:07 +00002686 /* Don't "goto fail" so that cleanup can happen. */
2687 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00002688 else
2689 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
2690 if (p2)
2691 unlock_user(p, arg2, 0);
2692 if (p)
2693 unlock_user(p2, arg4, 0);
2694 }
2695 break;
2696#endif
bellard31e31b82003-02-18 22:55:36 +00002697 case TARGET_NR_unlink:
pbrook53a59602006-03-25 19:31:22 +00002698 p = lock_user_string(arg1);
2699 ret = get_errno(unlink(p));
2700 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002701 break;
ths8170f562007-09-24 09:24:11 +00002702#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
2703 case TARGET_NR_unlinkat:
2704 if (!arg2) {
ths0da46a62007-10-20 20:23:07 +00002705 ret = -TARGET_EFAULT;
ths8170f562007-09-24 09:24:11 +00002706 goto fail;
2707 }
2708 p = lock_user_string(arg2);
2709 if (!access_ok(VERIFY_READ, p, 1))
ths0da46a62007-10-20 20:23:07 +00002710 /* Don't "goto fail" so that cleanup can happen. */
2711 ret = -TARGET_EFAULT;
ths8170f562007-09-24 09:24:11 +00002712 else
2713 ret = get_errno(sys_unlinkat(arg1, p, arg3));
2714 if (p)
2715 unlock_user(p, arg2, 0);
2716 break;
2717#endif
bellard31e31b82003-02-18 22:55:36 +00002718 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00002719 {
2720 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00002721 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00002722 abi_ulong gp;
2723 abi_ulong guest_argp;
2724 abi_ulong guest_envp;
2725 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00002726 char **q;
2727
bellardf7341ff2003-03-30 21:00:25 +00002728 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00002729 guest_argp = arg2;
2730 for (gp = guest_argp; tgetl(gp); gp++)
bellard7854b052003-03-29 17:22:23 +00002731 argc++;
bellardf7341ff2003-03-30 21:00:25 +00002732 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00002733 guest_envp = arg3;
2734 for (gp = guest_envp; tgetl(gp); gp++)
bellard7854b052003-03-29 17:22:23 +00002735 envc++;
2736
bellardf7341ff2003-03-30 21:00:25 +00002737 argp = alloca((argc + 1) * sizeof(void *));
2738 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00002739
pbrook53a59602006-03-25 19:31:22 +00002740 for (gp = guest_argp, q = argp; ;
blueswir1992f48a2007-10-14 16:27:31 +00002741 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002742 addr = tgetl(gp);
2743 if (!addr)
2744 break;
2745 *q = lock_user_string(addr);
2746 }
bellardf7341ff2003-03-30 21:00:25 +00002747 *q = NULL;
2748
pbrook53a59602006-03-25 19:31:22 +00002749 for (gp = guest_envp, q = envp; ;
blueswir1992f48a2007-10-14 16:27:31 +00002750 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002751 addr = tgetl(gp);
2752 if (!addr)
2753 break;
2754 *q = lock_user_string(addr);
2755 }
bellardf7341ff2003-03-30 21:00:25 +00002756 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00002757
pbrook53a59602006-03-25 19:31:22 +00002758 p = lock_user_string(arg1);
2759 ret = get_errno(execve(p, argp, envp));
2760 unlock_user(p, arg1, 0);
2761
2762 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00002763 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002764 addr = tgetl(gp);
2765 unlock_user(*q, addr, 0);
2766 }
2767 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00002768 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002769 addr = tgetl(gp);
2770 unlock_user(*q, addr, 0);
2771 }
bellard7854b052003-03-29 17:22:23 +00002772 }
bellard31e31b82003-02-18 22:55:36 +00002773 break;
2774 case TARGET_NR_chdir:
pbrook53a59602006-03-25 19:31:22 +00002775 p = lock_user_string(arg1);
2776 ret = get_errno(chdir(p));
2777 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002778 break;
bellarda315a142005-01-30 22:59:18 +00002779#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00002780 case TARGET_NR_time:
2781 {
pbrook53a59602006-03-25 19:31:22 +00002782 time_t host_time;
2783 ret = get_errno(time(&host_time));
2784 if (!is_error(ret) && arg1)
2785 tputl(arg1, host_time);
bellard31e31b82003-02-18 22:55:36 +00002786 }
2787 break;
bellarda315a142005-01-30 22:59:18 +00002788#endif
bellard31e31b82003-02-18 22:55:36 +00002789 case TARGET_NR_mknod:
pbrook53a59602006-03-25 19:31:22 +00002790 p = lock_user_string(arg1);
2791 ret = get_errno(mknod(p, arg2, arg3));
2792 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002793 break;
ths75ac37a2007-09-24 09:23:05 +00002794#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
2795 case TARGET_NR_mknodat:
2796 if (!arg2) {
ths0da46a62007-10-20 20:23:07 +00002797 ret = -TARGET_EFAULT;
ths75ac37a2007-09-24 09:23:05 +00002798 goto fail;
2799 }
2800 p = lock_user_string(arg2);
2801 if (!access_ok(VERIFY_READ, p, 1))
ths0da46a62007-10-20 20:23:07 +00002802 /* Don't "goto fail" so that cleanup can happen. */
2803 ret = -TARGET_EFAULT;
ths75ac37a2007-09-24 09:23:05 +00002804 else
2805 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
2806 if (p)
2807 unlock_user(p, arg2, 0);
2808 break;
2809#endif
bellard31e31b82003-02-18 22:55:36 +00002810 case TARGET_NR_chmod:
pbrook53a59602006-03-25 19:31:22 +00002811 p = lock_user_string(arg1);
2812 ret = get_errno(chmod(p, arg2));
2813 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002814 break;
bellardebc05482003-09-30 21:08:41 +00002815#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00002816 case TARGET_NR_break:
2817 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002818#endif
2819#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00002820 case TARGET_NR_oldstat:
2821 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002822#endif
bellard31e31b82003-02-18 22:55:36 +00002823 case TARGET_NR_lseek:
2824 ret = get_errno(lseek(arg1, arg2, arg3));
2825 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002826#ifdef TARGET_NR_getxpid
2827 case TARGET_NR_getxpid:
2828#else
bellard31e31b82003-02-18 22:55:36 +00002829 case TARGET_NR_getpid:
j_mayer7a3148a2007-04-05 07:13:51 +00002830#endif
bellard31e31b82003-02-18 22:55:36 +00002831 ret = get_errno(getpid());
2832 break;
2833 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00002834 {
2835 /* need to look at the data field */
2836 void *p2, *p3;
2837 p = lock_user_string(arg1);
2838 p2 = lock_user_string(arg2);
2839 p3 = lock_user_string(arg3);
2840 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, (const void *)arg5));
2841 unlock_user(p, arg1, 0);
2842 unlock_user(p2, arg2, 0);
2843 unlock_user(p3, arg3, 0);
2844 break;
2845 }
thse5febef2007-04-01 18:31:35 +00002846#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00002847 case TARGET_NR_umount:
pbrook53a59602006-03-25 19:31:22 +00002848 p = lock_user_string(arg1);
2849 ret = get_errno(umount(p));
2850 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002851 break;
thse5febef2007-04-01 18:31:35 +00002852#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002853#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002854 case TARGET_NR_stime:
2855 {
pbrook53a59602006-03-25 19:31:22 +00002856 time_t host_time;
2857 host_time = tgetl(arg1);
2858 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00002859 }
2860 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002861#endif
bellard31e31b82003-02-18 22:55:36 +00002862 case TARGET_NR_ptrace:
2863 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00002864#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002865 case TARGET_NR_alarm:
2866 ret = alarm(arg1);
2867 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002868#endif
bellardebc05482003-09-30 21:08:41 +00002869#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00002870 case TARGET_NR_oldfstat:
2871 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002872#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002873#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002874 case TARGET_NR_pause:
2875 ret = get_errno(pause());
2876 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002877#endif
thse5febef2007-04-01 18:31:35 +00002878#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00002879 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00002880 {
pbrook53a59602006-03-25 19:31:22 +00002881 struct utimbuf tbuf, *host_tbuf;
2882 struct target_utimbuf *target_tbuf;
2883 if (arg2) {
2884 lock_user_struct(target_tbuf, arg2, 1);
2885 tbuf.actime = tswapl(target_tbuf->actime);
2886 tbuf.modtime = tswapl(target_tbuf->modtime);
2887 unlock_user_struct(target_tbuf, arg2, 0);
2888 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00002889 } else {
pbrook53a59602006-03-25 19:31:22 +00002890 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00002891 }
pbrook53a59602006-03-25 19:31:22 +00002892 p = lock_user_string(arg1);
2893 ret = get_errno(utime(p, host_tbuf));
2894 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00002895 }
2896 break;
thse5febef2007-04-01 18:31:35 +00002897#endif
bellard978a66f2004-12-06 22:58:05 +00002898 case TARGET_NR_utimes:
2899 {
bellard978a66f2004-12-06 22:58:05 +00002900 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00002901 if (arg2) {
2902 target_to_host_timeval(&tv[0], arg2);
2903 target_to_host_timeval(&tv[1],
2904 arg2 + sizeof (struct target_timeval));
bellard978a66f2004-12-06 22:58:05 +00002905 tvp = tv;
2906 } else {
2907 tvp = NULL;
2908 }
pbrook53a59602006-03-25 19:31:22 +00002909 p = lock_user_string(arg1);
2910 ret = get_errno(utimes(p, tvp));
2911 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00002912 }
2913 break;
bellardebc05482003-09-30 21:08:41 +00002914#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00002915 case TARGET_NR_stty:
2916 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002917#endif
2918#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00002919 case TARGET_NR_gtty:
2920 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002921#endif
bellard31e31b82003-02-18 22:55:36 +00002922 case TARGET_NR_access:
pbrook53a59602006-03-25 19:31:22 +00002923 p = lock_user_string(arg1);
2924 ret = get_errno(access(p, arg2));
2925 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002926 break;
ths92a34c12007-09-24 09:27:49 +00002927#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
2928 case TARGET_NR_faccessat:
2929 if (!arg2) {
ths0da46a62007-10-20 20:23:07 +00002930 ret = -TARGET_EFAULT;
ths92a34c12007-09-24 09:27:49 +00002931 goto fail;
2932 }
2933 p = lock_user_string(arg2);
2934 if (!access_ok(VERIFY_READ, p, 1))
ths0da46a62007-10-20 20:23:07 +00002935 /* Don't "goto fail" so that cleanup can happen. */
2936 ret = -TARGET_EFAULT;
ths92a34c12007-09-24 09:27:49 +00002937 else
2938 ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
2939 if (p)
2940 unlock_user(p, arg2, 0);
2941 break;
2942#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002943#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002944 case TARGET_NR_nice:
2945 ret = get_errno(nice(arg1));
2946 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002947#endif
bellardebc05482003-09-30 21:08:41 +00002948#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00002949 case TARGET_NR_ftime:
2950 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002951#endif
bellard31e31b82003-02-18 22:55:36 +00002952 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00002953 sync();
2954 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00002955 break;
2956 case TARGET_NR_kill:
2957 ret = get_errno(kill(arg1, arg2));
2958 break;
2959 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00002960 {
2961 void *p2;
2962 p = lock_user_string(arg1);
2963 p2 = lock_user_string(arg2);
2964 ret = get_errno(rename(p, p2));
2965 unlock_user(p2, arg2, 0);
2966 unlock_user(p, arg1, 0);
2967 }
bellard31e31b82003-02-18 22:55:36 +00002968 break;
ths722183f2007-09-24 09:24:37 +00002969#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
2970 case TARGET_NR_renameat:
2971 if (!arg2 || !arg4) {
ths0da46a62007-10-20 20:23:07 +00002972 ret = -TARGET_EFAULT;
2973 goto fail;
ths722183f2007-09-24 09:24:37 +00002974 }
2975 {
2976 void *p2 = NULL;
2977 p = lock_user_string(arg2);
2978 p2 = lock_user_string(arg4);
2979 if (!access_ok(VERIFY_READ, p, 1)
2980 || !access_ok(VERIFY_READ, p2, 1))
ths0da46a62007-10-20 20:23:07 +00002981 /* Don't "goto fail" so that cleanup can happen. */
2982 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00002983 else
2984 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
2985 if (p2)
2986 unlock_user(p2, arg4, 0);
2987 if (p)
2988 unlock_user(p, arg2, 0);
2989 }
2990 break;
2991#endif
bellard31e31b82003-02-18 22:55:36 +00002992 case TARGET_NR_mkdir:
pbrook53a59602006-03-25 19:31:22 +00002993 p = lock_user_string(arg1);
2994 ret = get_errno(mkdir(p, arg2));
2995 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002996 break;
ths4472ad02007-09-24 09:22:32 +00002997#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
2998 case TARGET_NR_mkdirat:
2999 if (!arg2) {
ths0da46a62007-10-20 20:23:07 +00003000 ret = -TARGET_EFAULT;
ths4472ad02007-09-24 09:22:32 +00003001 goto fail;
3002 }
3003 p = lock_user_string(arg2);
3004 if (!access_ok(VERIFY_READ, p, 1))
ths0da46a62007-10-20 20:23:07 +00003005 /* Don't "goto fail" so that cleanup can happen. */
3006 ret = -TARGET_EFAULT;
ths4472ad02007-09-24 09:22:32 +00003007 else
3008 ret = get_errno(sys_mkdirat(arg1, p, arg3));
3009 if (p)
3010 unlock_user(p, arg2, 0);
3011 break;
3012#endif
bellard31e31b82003-02-18 22:55:36 +00003013 case TARGET_NR_rmdir:
pbrook53a59602006-03-25 19:31:22 +00003014 p = lock_user_string(arg1);
3015 ret = get_errno(rmdir(p));
3016 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003017 break;
3018 case TARGET_NR_dup:
3019 ret = get_errno(dup(arg1));
3020 break;
3021 case TARGET_NR_pipe:
3022 {
pbrook53a59602006-03-25 19:31:22 +00003023 int host_pipe[2];
3024 ret = get_errno(pipe(host_pipe));
bellard31e31b82003-02-18 22:55:36 +00003025 if (!is_error(ret)) {
thsc12ab052007-06-01 11:50:36 +00003026#if defined(TARGET_MIPS)
thsead93602007-09-06 00:18:15 +00003027 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
3028 env->gpr[3][env->current_tc] = host_pipe[1];
thsc12ab052007-06-01 11:50:36 +00003029 ret = host_pipe[0];
3030#else
pbrook53a59602006-03-25 19:31:22 +00003031 tput32(arg1, host_pipe[0]);
3032 tput32(arg1 + 4, host_pipe[1]);
thsc12ab052007-06-01 11:50:36 +00003033#endif
bellard31e31b82003-02-18 22:55:36 +00003034 }
3035 }
3036 break;
3037 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00003038 {
pbrook53a59602006-03-25 19:31:22 +00003039 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00003040 struct tms tms;
3041 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00003042 if (arg1) {
3043 tmsp = lock_user(arg1, sizeof(struct target_tms), 0);
bellardc596ed12003-07-13 17:32:31 +00003044 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3045 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3046 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3047 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00003048 }
bellardc596ed12003-07-13 17:32:31 +00003049 if (!is_error(ret))
3050 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00003051 }
3052 break;
bellardebc05482003-09-30 21:08:41 +00003053#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00003054 case TARGET_NR_prof:
3055 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003056#endif
thse5febef2007-04-01 18:31:35 +00003057#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00003058 case TARGET_NR_signal:
3059 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003060#endif
bellard31e31b82003-02-18 22:55:36 +00003061 case TARGET_NR_acct:
pbrook24836682006-04-16 14:14:53 +00003062 p = lock_user_string(arg1);
3063 ret = get_errno(acct(path(p)));
3064 unlock_user(p, arg1, 0);
3065 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003066#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003067 case TARGET_NR_umount2:
pbrook53a59602006-03-25 19:31:22 +00003068 p = lock_user_string(arg1);
3069 ret = get_errno(umount2(p, arg2));
3070 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003071 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003072#endif
bellardebc05482003-09-30 21:08:41 +00003073#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00003074 case TARGET_NR_lock:
3075 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003076#endif
bellard31e31b82003-02-18 22:55:36 +00003077 case TARGET_NR_ioctl:
3078 ret = do_ioctl(arg1, arg2, arg3);
3079 break;
3080 case TARGET_NR_fcntl:
bellard7775e9e2003-05-14 22:46:48 +00003081 ret = get_errno(do_fcntl(arg1, arg2, arg3));
bellard31e31b82003-02-18 22:55:36 +00003082 break;
bellardebc05482003-09-30 21:08:41 +00003083#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00003084 case TARGET_NR_mpx:
3085 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003086#endif
bellard31e31b82003-02-18 22:55:36 +00003087 case TARGET_NR_setpgid:
3088 ret = get_errno(setpgid(arg1, arg2));
3089 break;
bellardebc05482003-09-30 21:08:41 +00003090#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00003091 case TARGET_NR_ulimit:
3092 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003093#endif
3094#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00003095 case TARGET_NR_oldolduname:
3096 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003097#endif
bellard31e31b82003-02-18 22:55:36 +00003098 case TARGET_NR_umask:
3099 ret = get_errno(umask(arg1));
3100 break;
3101 case TARGET_NR_chroot:
pbrook53a59602006-03-25 19:31:22 +00003102 p = lock_user_string(arg1);
3103 ret = get_errno(chroot(p));
3104 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003105 break;
3106 case TARGET_NR_ustat:
3107 goto unimplemented;
3108 case TARGET_NR_dup2:
3109 ret = get_errno(dup2(arg1, arg2));
3110 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003111#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003112 case TARGET_NR_getppid:
3113 ret = get_errno(getppid());
3114 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003115#endif
bellard31e31b82003-02-18 22:55:36 +00003116 case TARGET_NR_getpgrp:
3117 ret = get_errno(getpgrp());
3118 break;
3119 case TARGET_NR_setsid:
3120 ret = get_errno(setsid());
3121 break;
thse5febef2007-04-01 18:31:35 +00003122#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00003123 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00003124 {
ths388bb212007-05-13 13:58:00 +00003125#if !defined(TARGET_MIPS)
pbrook53a59602006-03-25 19:31:22 +00003126 struct target_old_sigaction *old_act;
bellard66fb9762003-03-23 01:06:05 +00003127 struct target_sigaction act, oact, *pact;
pbrook53a59602006-03-25 19:31:22 +00003128 if (arg2) {
3129 lock_user_struct(old_act, arg2, 1);
bellard66fb9762003-03-23 01:06:05 +00003130 act._sa_handler = old_act->_sa_handler;
3131 target_siginitset(&act.sa_mask, old_act->sa_mask);
3132 act.sa_flags = old_act->sa_flags;
3133 act.sa_restorer = old_act->sa_restorer;
pbrook53a59602006-03-25 19:31:22 +00003134 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003135 pact = &act;
3136 } else {
3137 pact = NULL;
3138 }
3139 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00003140 if (!is_error(ret) && arg3) {
3141 lock_user_struct(old_act, arg3, 0);
3142 old_act->_sa_handler = oact._sa_handler;
3143 old_act->sa_mask = oact.sa_mask.sig[0];
3144 old_act->sa_flags = oact.sa_flags;
3145 old_act->sa_restorer = oact.sa_restorer;
3146 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00003147 }
ths388bb212007-05-13 13:58:00 +00003148#else
bellard106ec872006-06-27 21:08:10 +00003149 struct target_sigaction act, oact, *pact, *old_act;
3150
3151 if (arg2) {
3152 lock_user_struct(old_act, arg2, 1);
3153 act._sa_handler = old_act->_sa_handler;
3154 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3155 act.sa_flags = old_act->sa_flags;
3156 unlock_user_struct(old_act, arg2, 0);
3157 pact = &act;
3158 } else {
3159 pact = NULL;
3160 }
3161
3162 ret = get_errno(do_sigaction(arg1, pact, &oact));
3163
3164 if (!is_error(ret) && arg3) {
3165 lock_user_struct(old_act, arg3, 0);
3166 old_act->_sa_handler = oact._sa_handler;
3167 old_act->sa_flags = oact.sa_flags;
3168 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3169 old_act->sa_mask.sig[1] = 0;
3170 old_act->sa_mask.sig[2] = 0;
3171 old_act->sa_mask.sig[3] = 0;
3172 unlock_user_struct(old_act, arg3, 1);
3173 }
ths388bb212007-05-13 13:58:00 +00003174#endif
bellard31e31b82003-02-18 22:55:36 +00003175 }
3176 break;
thse5febef2007-04-01 18:31:35 +00003177#endif
bellard66fb9762003-03-23 01:06:05 +00003178 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00003179 {
3180 struct target_sigaction *act;
3181 struct target_sigaction *oact;
3182
3183 if (arg2)
3184 lock_user_struct(act, arg2, 1);
3185 else
3186 act = NULL;
3187 if (arg3)
3188 lock_user_struct(oact, arg3, 0);
3189 else
3190 oact = NULL;
3191 ret = get_errno(do_sigaction(arg1, act, oact));
3192 if (arg2)
3193 unlock_user_struct(act, arg2, 0);
3194 if (arg3)
3195 unlock_user_struct(oact, arg3, 1);
3196 }
bellard66fb9762003-03-23 01:06:05 +00003197 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003198#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003199 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00003200 {
3201 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00003202 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00003203 sigprocmask(0, NULL, &cur_set);
3204 host_to_target_old_sigset(&target_set, &cur_set);
3205 ret = target_set;
3206 }
3207 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003208#endif
3209#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003210 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00003211 {
3212 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00003213 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00003214 sigprocmask(0, NULL, &cur_set);
3215 target_to_host_old_sigset(&set, &target_set);
3216 sigorset(&set, &set, &cur_set);
3217 sigprocmask(SIG_SETMASK, &set, &oset);
3218 host_to_target_old_sigset(&target_set, &oset);
3219 ret = target_set;
3220 }
3221 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003222#endif
thse5febef2007-04-01 18:31:35 +00003223#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00003224 case TARGET_NR_sigprocmask:
3225 {
3226 int how = arg1;
3227 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003228
pbrook53a59602006-03-25 19:31:22 +00003229 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003230 switch(how) {
3231 case TARGET_SIG_BLOCK:
3232 how = SIG_BLOCK;
3233 break;
3234 case TARGET_SIG_UNBLOCK:
3235 how = SIG_UNBLOCK;
3236 break;
3237 case TARGET_SIG_SETMASK:
3238 how = SIG_SETMASK;
3239 break;
3240 default:
ths0da46a62007-10-20 20:23:07 +00003241 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00003242 goto fail;
3243 }
pbrook53a59602006-03-25 19:31:22 +00003244 p = lock_user(arg2, sizeof(target_sigset_t), 1);
3245 target_to_host_old_sigset(&set, p);
3246 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003247 set_ptr = &set;
3248 } else {
3249 how = 0;
3250 set_ptr = NULL;
3251 }
3252 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003253 if (!is_error(ret) && arg3) {
3254 p = lock_user(arg3, sizeof(target_sigset_t), 0);
3255 host_to_target_old_sigset(p, &oldset);
3256 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003257 }
3258 }
3259 break;
thse5febef2007-04-01 18:31:35 +00003260#endif
bellard66fb9762003-03-23 01:06:05 +00003261 case TARGET_NR_rt_sigprocmask:
3262 {
3263 int how = arg1;
3264 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003265
pbrook53a59602006-03-25 19:31:22 +00003266 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003267 switch(how) {
3268 case TARGET_SIG_BLOCK:
3269 how = SIG_BLOCK;
3270 break;
3271 case TARGET_SIG_UNBLOCK:
3272 how = SIG_UNBLOCK;
3273 break;
3274 case TARGET_SIG_SETMASK:
3275 how = SIG_SETMASK;
3276 break;
3277 default:
ths0da46a62007-10-20 20:23:07 +00003278 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00003279 goto fail;
3280 }
pbrook53a59602006-03-25 19:31:22 +00003281 p = lock_user(arg2, sizeof(target_sigset_t), 1);
3282 target_to_host_sigset(&set, p);
3283 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003284 set_ptr = &set;
3285 } else {
3286 how = 0;
3287 set_ptr = NULL;
3288 }
3289 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003290 if (!is_error(ret) && arg3) {
3291 p = lock_user(arg3, sizeof(target_sigset_t), 0);
3292 host_to_target_sigset(p, &oldset);
3293 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003294 }
3295 }
3296 break;
thse5febef2007-04-01 18:31:35 +00003297#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00003298 case TARGET_NR_sigpending:
3299 {
3300 sigset_t set;
3301 ret = get_errno(sigpending(&set));
3302 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003303 p = lock_user(arg1, sizeof(target_sigset_t), 0);
3304 host_to_target_old_sigset(p, &set);
3305 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003306 }
3307 }
3308 break;
thse5febef2007-04-01 18:31:35 +00003309#endif
bellard66fb9762003-03-23 01:06:05 +00003310 case TARGET_NR_rt_sigpending:
3311 {
3312 sigset_t set;
3313 ret = get_errno(sigpending(&set));
3314 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003315 p = lock_user(arg1, sizeof(target_sigset_t), 0);
3316 host_to_target_sigset(p, &set);
3317 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003318 }
3319 }
3320 break;
thse5febef2007-04-01 18:31:35 +00003321#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00003322 case TARGET_NR_sigsuspend:
3323 {
3324 sigset_t set;
pbrook53a59602006-03-25 19:31:22 +00003325 p = lock_user(arg1, sizeof(target_sigset_t), 1);
3326 target_to_host_old_sigset(&set, p);
3327 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003328 ret = get_errno(sigsuspend(&set));
3329 }
3330 break;
thse5febef2007-04-01 18:31:35 +00003331#endif
bellard66fb9762003-03-23 01:06:05 +00003332 case TARGET_NR_rt_sigsuspend:
3333 {
3334 sigset_t set;
pbrook53a59602006-03-25 19:31:22 +00003335 p = lock_user(arg1, sizeof(target_sigset_t), 1);
3336 target_to_host_sigset(&set, p);
3337 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003338 ret = get_errno(sigsuspend(&set));
3339 }
3340 break;
3341 case TARGET_NR_rt_sigtimedwait:
3342 {
bellard66fb9762003-03-23 01:06:05 +00003343 sigset_t set;
3344 struct timespec uts, *puts;
3345 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00003346
pbrook53a59602006-03-25 19:31:22 +00003347 p = lock_user(arg1, sizeof(target_sigset_t), 1);
3348 target_to_host_sigset(&set, p);
3349 unlock_user(p, arg1, 0);
3350 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00003351 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00003352 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00003353 } else {
3354 puts = NULL;
3355 }
3356 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00003357 if (!is_error(ret) && arg2) {
3358 p = lock_user(arg2, sizeof(target_sigset_t), 0);
3359 host_to_target_siginfo(p, &uinfo);
3360 unlock_user(p, arg2, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003361 }
3362 }
3363 break;
3364 case TARGET_NR_rt_sigqueueinfo:
3365 {
3366 siginfo_t uinfo;
pbrook53a59602006-03-25 19:31:22 +00003367 p = lock_user(arg3, sizeof(target_sigset_t), 1);
3368 target_to_host_siginfo(&uinfo, p);
3369 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003370 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3371 }
3372 break;
thse5febef2007-04-01 18:31:35 +00003373#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00003374 case TARGET_NR_sigreturn:
3375 /* NOTE: ret is eax, so not transcoding must be done */
3376 ret = do_sigreturn(cpu_env);
3377 break;
thse5febef2007-04-01 18:31:35 +00003378#endif
bellard66fb9762003-03-23 01:06:05 +00003379 case TARGET_NR_rt_sigreturn:
3380 /* NOTE: ret is eax, so not transcoding must be done */
3381 ret = do_rt_sigreturn(cpu_env);
3382 break;
bellard31e31b82003-02-18 22:55:36 +00003383 case TARGET_NR_sethostname:
pbrook53a59602006-03-25 19:31:22 +00003384 p = lock_user_string(arg1);
3385 ret = get_errno(sethostname(p, arg2));
3386 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003387 break;
3388 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00003389 {
3390 /* XXX: convert resource ? */
3391 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003392 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003393 struct rlimit rlim;
pbrook53a59602006-03-25 19:31:22 +00003394 lock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00003395 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3396 rlim.rlim_max = tswapl(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00003397 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00003398 ret = get_errno(setrlimit(resource, &rlim));
3399 }
3400 break;
bellard31e31b82003-02-18 22:55:36 +00003401 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00003402 {
3403 /* XXX: convert resource ? */
3404 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003405 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003406 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00003407
bellard9de5e442003-03-23 16:49:39 +00003408 ret = get_errno(getrlimit(resource, &rlim));
3409 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003410 lock_user_struct(target_rlim, arg2, 0);
3411 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3412 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3413 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00003414 }
3415 }
3416 break;
bellard31e31b82003-02-18 22:55:36 +00003417 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00003418 {
3419 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00003420 ret = get_errno(getrusage(arg1, &rusage));
3421 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003422 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00003423 }
3424 }
3425 break;
bellard31e31b82003-02-18 22:55:36 +00003426 case TARGET_NR_gettimeofday:
3427 {
bellard31e31b82003-02-18 22:55:36 +00003428 struct timeval tv;
3429 ret = get_errno(gettimeofday(&tv, NULL));
3430 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003431 host_to_target_timeval(arg1, &tv);
bellard31e31b82003-02-18 22:55:36 +00003432 }
3433 }
3434 break;
3435 case TARGET_NR_settimeofday:
3436 {
bellard31e31b82003-02-18 22:55:36 +00003437 struct timeval tv;
pbrook53a59602006-03-25 19:31:22 +00003438 target_to_host_timeval(&tv, arg1);
bellard31e31b82003-02-18 22:55:36 +00003439 ret = get_errno(settimeofday(&tv, NULL));
3440 }
3441 break;
bellard048f6b42005-11-26 18:47:20 +00003442#ifdef TARGET_NR_select
bellard31e31b82003-02-18 22:55:36 +00003443 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00003444 {
pbrook53a59602006-03-25 19:31:22 +00003445 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00003446 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00003447 long nsel;
3448
3449 lock_user_struct(sel, arg1, 1);
3450 nsel = tswapl(sel->n);
3451 inp = tswapl(sel->inp);
3452 outp = tswapl(sel->outp);
3453 exp = tswapl(sel->exp);
3454 tvp = tswapl(sel->tvp);
3455 unlock_user_struct(sel, arg1, 0);
3456 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00003457 }
3458 break;
bellard048f6b42005-11-26 18:47:20 +00003459#endif
bellard31e31b82003-02-18 22:55:36 +00003460 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00003461 {
3462 void *p2;
3463 p = lock_user_string(arg1);
3464 p2 = lock_user_string(arg2);
3465 ret = get_errno(symlink(p, p2));
3466 unlock_user(p2, arg2, 0);
3467 unlock_user(p, arg1, 0);
3468 }
bellard31e31b82003-02-18 22:55:36 +00003469 break;
thsf0b62432007-09-24 09:25:40 +00003470#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
3471 case TARGET_NR_symlinkat:
3472 if (!arg1 || !arg3) {
ths0da46a62007-10-20 20:23:07 +00003473 ret = -TARGET_EFAULT;
3474 goto fail;
3475 }
thsf0b62432007-09-24 09:25:40 +00003476 {
3477 void *p2 = NULL;
3478 p = lock_user_string(arg1);
3479 p2 = lock_user_string(arg3);
3480 if (!access_ok(VERIFY_READ, p, 1)
3481 || !access_ok(VERIFY_READ, p2, 1))
ths0da46a62007-10-20 20:23:07 +00003482 /* Don't "goto fail" so that cleanup can happen. */
3483 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00003484 else
3485 ret = get_errno(sys_symlinkat(p, arg2, p2));
3486 if (p2)
3487 unlock_user(p2, arg3, 0);
3488 if (p)
3489 unlock_user(p, arg1, 0);
3490 }
3491 break;
3492#endif
bellardebc05482003-09-30 21:08:41 +00003493#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00003494 case TARGET_NR_oldlstat:
3495 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003496#endif
bellard31e31b82003-02-18 22:55:36 +00003497 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00003498 {
3499 void *p2;
3500 p = lock_user_string(arg1);
3501 p2 = lock_user(arg2, arg3, 0);
3502 ret = get_errno(readlink(path(p), p2, arg3));
3503 unlock_user(p2, arg2, ret);
3504 unlock_user(p, arg1, 0);
3505 }
bellard31e31b82003-02-18 22:55:36 +00003506 break;
ths5e0ccb12007-09-24 09:26:10 +00003507#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
3508 case TARGET_NR_readlinkat:
3509 if (!arg2 || !arg3) {
ths0da46a62007-10-20 20:23:07 +00003510 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00003511 goto fail;
ths0da46a62007-10-20 20:23:07 +00003512 }
ths5e0ccb12007-09-24 09:26:10 +00003513 {
3514 void *p2 = NULL;
3515 p = lock_user_string(arg2);
3516 p2 = lock_user(arg3, arg4, 0);
3517 if (!access_ok(VERIFY_READ, p, 1)
3518 || !access_ok(VERIFY_READ, p2, 1))
ths0da46a62007-10-20 20:23:07 +00003519 /* Don't "goto fail" so that cleanup can happen. */
3520 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00003521 else
3522 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
3523 if (p2)
3524 unlock_user(p2, arg3, ret);
3525 if (p)
3526 unlock_user(p, arg2, 0);
3527 }
3528 break;
3529#endif
thse5febef2007-04-01 18:31:35 +00003530#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00003531 case TARGET_NR_uselib:
3532 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003533#endif
3534#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00003535 case TARGET_NR_swapon:
pbrook53a59602006-03-25 19:31:22 +00003536 p = lock_user_string(arg1);
3537 ret = get_errno(swapon(p, arg2));
3538 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003539 break;
thse5febef2007-04-01 18:31:35 +00003540#endif
bellard31e31b82003-02-18 22:55:36 +00003541 case TARGET_NR_reboot:
3542 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003543#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00003544 case TARGET_NR_readdir:
3545 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003546#endif
3547#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00003548 case TARGET_NR_mmap:
ths48733d12007-10-08 13:36:46 +00003549#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
bellard31e31b82003-02-18 22:55:36 +00003550 {
blueswir1992f48a2007-10-14 16:27:31 +00003551 abi_ulong *v;
3552 abi_ulong v1, v2, v3, v4, v5, v6;
3553 v = lock_user(arg1, 6 * sizeof(abi_ulong), 1);
pbrook53a59602006-03-25 19:31:22 +00003554 v1 = tswapl(v[0]);
3555 v2 = tswapl(v[1]);
3556 v3 = tswapl(v[2]);
3557 v4 = tswapl(v[3]);
3558 v5 = tswapl(v[4]);
3559 v6 = tswapl(v[5]);
3560 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00003561 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00003562 target_to_host_bitmask(v4, mmap_flags_tbl),
3563 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00003564 }
bellard31e31b82003-02-18 22:55:36 +00003565#else
ths5fafdf22007-09-16 21:08:06 +00003566 ret = get_errno(target_mmap(arg1, arg2, arg3,
3567 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00003568 arg5,
3569 arg6));
bellard31e31b82003-02-18 22:55:36 +00003570#endif
bellard6fb883e2003-07-09 17:12:39 +00003571 break;
thse5febef2007-04-01 18:31:35 +00003572#endif
bellarda315a142005-01-30 22:59:18 +00003573#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00003574 case TARGET_NR_mmap2:
ths0d3267a2006-12-11 22:04:25 +00003575#if defined(TARGET_SPARC) || defined(TARGET_MIPS)
bellardc573ff62004-01-04 15:51:36 +00003576#define MMAP_SHIFT 12
3577#else
3578#define MMAP_SHIFT TARGET_PAGE_BITS
3579#endif
ths5fafdf22007-09-16 21:08:06 +00003580 ret = get_errno(target_mmap(arg1, arg2, arg3,
3581 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00003582 arg5,
bellardc573ff62004-01-04 15:51:36 +00003583 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00003584 break;
bellarda315a142005-01-30 22:59:18 +00003585#endif
bellard31e31b82003-02-18 22:55:36 +00003586 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00003587 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00003588 break;
bellard9de5e442003-03-23 16:49:39 +00003589 case TARGET_NR_mprotect:
bellard54936002003-05-13 00:25:15 +00003590 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00003591 break;
thse5febef2007-04-01 18:31:35 +00003592#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00003593 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00003594 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00003595 break;
thse5febef2007-04-01 18:31:35 +00003596#endif
pbrook53a59602006-03-25 19:31:22 +00003597 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00003598#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00003599 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00003600 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00003601 break;
thse5febef2007-04-01 18:31:35 +00003602#endif
3603#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00003604 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00003605 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00003606 break;
thse5febef2007-04-01 18:31:35 +00003607#endif
3608#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00003609 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00003610 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00003611 break;
thse5febef2007-04-01 18:31:35 +00003612#endif
3613#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00003614 case TARGET_NR_mlockall:
3615 ret = get_errno(mlockall(arg1));
3616 break;
thse5febef2007-04-01 18:31:35 +00003617#endif
3618#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00003619 case TARGET_NR_munlockall:
3620 ret = get_errno(munlockall());
3621 break;
thse5febef2007-04-01 18:31:35 +00003622#endif
bellard31e31b82003-02-18 22:55:36 +00003623 case TARGET_NR_truncate:
pbrook53a59602006-03-25 19:31:22 +00003624 p = lock_user_string(arg1);
3625 ret = get_errno(truncate(p, arg2));
3626 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003627 break;
3628 case TARGET_NR_ftruncate:
3629 ret = get_errno(ftruncate(arg1, arg2));
3630 break;
3631 case TARGET_NR_fchmod:
3632 ret = get_errno(fchmod(arg1, arg2));
3633 break;
ths814d7972007-09-24 09:26:51 +00003634#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
3635 case TARGET_NR_fchmodat:
3636 if (!arg2) {
ths0da46a62007-10-20 20:23:07 +00003637 ret = -TARGET_EFAULT;
ths814d7972007-09-24 09:26:51 +00003638 goto fail;
3639 }
3640 p = lock_user_string(arg2);
3641 if (!access_ok(VERIFY_READ, p, 1))
ths0da46a62007-10-20 20:23:07 +00003642 /* Don't "goto fail" so that cleanup can happen. */
3643 ret = -TARGET_EFAULT;
ths814d7972007-09-24 09:26:51 +00003644 else
3645 ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
3646 if (p)
3647 unlock_user(p, arg2, 0);
3648 break;
3649#endif
bellard31e31b82003-02-18 22:55:36 +00003650 case TARGET_NR_getpriority:
thsc6cda172007-10-09 03:42:34 +00003651 /* libc does special remapping of the return value of
3652 * sys_getpriority() so it's just easiest to call
3653 * sys_getpriority() directly rather than through libc. */
3654 ret = sys_getpriority(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00003655 break;
3656 case TARGET_NR_setpriority:
3657 ret = get_errno(setpriority(arg1, arg2, arg3));
3658 break;
bellardebc05482003-09-30 21:08:41 +00003659#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00003660 case TARGET_NR_profil:
3661 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003662#endif
bellard31e31b82003-02-18 22:55:36 +00003663 case TARGET_NR_statfs:
pbrook53a59602006-03-25 19:31:22 +00003664 p = lock_user_string(arg1);
3665 ret = get_errno(statfs(path(p), &stfs));
3666 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003667 convert_statfs:
3668 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003669 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00003670
pbrook53a59602006-03-25 19:31:22 +00003671 lock_user_struct(target_stfs, arg2, 0);
3672 /* ??? put_user is probably wrong. */
bellard56c8f682005-11-28 22:28:41 +00003673 put_user(stfs.f_type, &target_stfs->f_type);
3674 put_user(stfs.f_bsize, &target_stfs->f_bsize);
3675 put_user(stfs.f_blocks, &target_stfs->f_blocks);
3676 put_user(stfs.f_bfree, &target_stfs->f_bfree);
3677 put_user(stfs.f_bavail, &target_stfs->f_bavail);
3678 put_user(stfs.f_files, &target_stfs->f_files);
3679 put_user(stfs.f_ffree, &target_stfs->f_ffree);
ths4ce6f8d2007-07-20 15:54:27 +00003680 put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3681 put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
bellard56c8f682005-11-28 22:28:41 +00003682 put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00003683 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00003684 }
3685 break;
3686 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00003687 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00003688 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00003689#ifdef TARGET_NR_statfs64
3690 case TARGET_NR_statfs64:
pbrook53a59602006-03-25 19:31:22 +00003691 p = lock_user_string(arg1);
3692 ret = get_errno(statfs(path(p), &stfs));
3693 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00003694 convert_statfs64:
3695 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003696 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00003697
pbrook53a59602006-03-25 19:31:22 +00003698 lock_user_struct(target_stfs, arg3, 0);
3699 /* ??? put_user is probably wrong. */
bellard56c8f682005-11-28 22:28:41 +00003700 put_user(stfs.f_type, &target_stfs->f_type);
3701 put_user(stfs.f_bsize, &target_stfs->f_bsize);
3702 put_user(stfs.f_blocks, &target_stfs->f_blocks);
3703 put_user(stfs.f_bfree, &target_stfs->f_bfree);
3704 put_user(stfs.f_bavail, &target_stfs->f_bavail);
3705 put_user(stfs.f_files, &target_stfs->f_files);
3706 put_user(stfs.f_ffree, &target_stfs->f_ffree);
ths4ce6f8d2007-07-20 15:54:27 +00003707 put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3708 put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
bellard56c8f682005-11-28 22:28:41 +00003709 put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00003710 unlock_user_struct(target_stfs, arg3, 0);
bellard56c8f682005-11-28 22:28:41 +00003711 }
3712 break;
3713 case TARGET_NR_fstatfs64:
3714 ret = get_errno(fstatfs(arg1, &stfs));
3715 goto convert_statfs64;
3716#endif
bellardebc05482003-09-30 21:08:41 +00003717#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00003718 case TARGET_NR_ioperm:
3719 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003720#endif
thse5febef2007-04-01 18:31:35 +00003721#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00003722 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00003723 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00003724 break;
thse5febef2007-04-01 18:31:35 +00003725#endif
bellard3532fa72006-06-24 15:06:03 +00003726#ifdef TARGET_NR_accept
3727 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00003728 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003729 break;
3730#endif
3731#ifdef TARGET_NR_bind
3732 case TARGET_NR_bind:
3733 ret = do_bind(arg1, arg2, arg3);
3734 break;
3735#endif
3736#ifdef TARGET_NR_connect
3737 case TARGET_NR_connect:
3738 ret = do_connect(arg1, arg2, arg3);
3739 break;
3740#endif
3741#ifdef TARGET_NR_getpeername
3742 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00003743 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003744 break;
3745#endif
3746#ifdef TARGET_NR_getsockname
3747 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00003748 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003749 break;
3750#endif
3751#ifdef TARGET_NR_getsockopt
3752 case TARGET_NR_getsockopt:
3753 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
3754 break;
3755#endif
3756#ifdef TARGET_NR_listen
3757 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00003758 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00003759 break;
3760#endif
3761#ifdef TARGET_NR_recv
3762 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00003763 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00003764 break;
3765#endif
3766#ifdef TARGET_NR_recvfrom
3767 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00003768 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00003769 break;
3770#endif
3771#ifdef TARGET_NR_recvmsg
3772 case TARGET_NR_recvmsg:
3773 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
3774 break;
3775#endif
3776#ifdef TARGET_NR_send
3777 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00003778 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00003779 break;
3780#endif
3781#ifdef TARGET_NR_sendmsg
3782 case TARGET_NR_sendmsg:
3783 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
3784 break;
3785#endif
3786#ifdef TARGET_NR_sendto
3787 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00003788 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00003789 break;
3790#endif
3791#ifdef TARGET_NR_shutdown
3792 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00003793 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00003794 break;
3795#endif
3796#ifdef TARGET_NR_socket
3797 case TARGET_NR_socket:
3798 ret = do_socket(arg1, arg2, arg3);
3799 break;
3800#endif
3801#ifdef TARGET_NR_socketpair
3802 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00003803 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00003804 break;
3805#endif
3806#ifdef TARGET_NR_setsockopt
3807 case TARGET_NR_setsockopt:
3808 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
3809 break;
3810#endif
ths7494b0f2007-02-11 18:26:53 +00003811
bellard31e31b82003-02-18 22:55:36 +00003812 case TARGET_NR_syslog:
thse5574482007-02-11 20:03:13 +00003813 p = lock_user_string(arg2);
3814 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
3815 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00003816 break;
3817
bellard31e31b82003-02-18 22:55:36 +00003818 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00003819 {
bellard66fb9762003-03-23 01:06:05 +00003820 struct itimerval value, ovalue, *pvalue;
3821
pbrook53a59602006-03-25 19:31:22 +00003822 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003823 pvalue = &value;
ths5fafdf22007-09-16 21:08:06 +00003824 target_to_host_timeval(&pvalue->it_interval,
pbrook53a59602006-03-25 19:31:22 +00003825 arg2);
ths5fafdf22007-09-16 21:08:06 +00003826 target_to_host_timeval(&pvalue->it_value,
pbrook53a59602006-03-25 19:31:22 +00003827 arg2 + sizeof(struct target_timeval));
bellard66fb9762003-03-23 01:06:05 +00003828 } else {
3829 pvalue = NULL;
3830 }
3831 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00003832 if (!is_error(ret) && arg3) {
3833 host_to_target_timeval(arg3,
bellard66fb9762003-03-23 01:06:05 +00003834 &ovalue.it_interval);
pbrook53a59602006-03-25 19:31:22 +00003835 host_to_target_timeval(arg3 + sizeof(struct target_timeval),
bellard66fb9762003-03-23 01:06:05 +00003836 &ovalue.it_value);
3837 }
3838 }
3839 break;
bellard31e31b82003-02-18 22:55:36 +00003840 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00003841 {
bellard66fb9762003-03-23 01:06:05 +00003842 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00003843
bellard66fb9762003-03-23 01:06:05 +00003844 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00003845 if (!is_error(ret) && arg2) {
3846 host_to_target_timeval(arg2,
bellard66fb9762003-03-23 01:06:05 +00003847 &value.it_interval);
pbrook53a59602006-03-25 19:31:22 +00003848 host_to_target_timeval(arg2 + sizeof(struct target_timeval),
bellard66fb9762003-03-23 01:06:05 +00003849 &value.it_value);
3850 }
3851 }
3852 break;
bellard31e31b82003-02-18 22:55:36 +00003853 case TARGET_NR_stat:
pbrook53a59602006-03-25 19:31:22 +00003854 p = lock_user_string(arg1);
3855 ret = get_errno(stat(path(p), &st));
3856 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003857 goto do_stat;
3858 case TARGET_NR_lstat:
pbrook53a59602006-03-25 19:31:22 +00003859 p = lock_user_string(arg1);
3860 ret = get_errno(lstat(path(p), &st));
3861 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003862 goto do_stat;
3863 case TARGET_NR_fstat:
3864 {
3865 ret = get_errno(fstat(arg1, &st));
3866 do_stat:
3867 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003868 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00003869
pbrook53a59602006-03-25 19:31:22 +00003870 lock_user_struct(target_st, arg2, 0);
blueswir1992f48a2007-10-14 16:27:31 +00003871#if defined(TARGET_MIPS) || (defined(TARGET_SPARC64) && !defined(TARGET_ABI32))
thse3584652007-06-01 11:49:38 +00003872 target_st->st_dev = tswap32(st.st_dev);
3873#else
bellard31e31b82003-02-18 22:55:36 +00003874 target_st->st_dev = tswap16(st.st_dev);
thse3584652007-06-01 11:49:38 +00003875#endif
bellard31e31b82003-02-18 22:55:36 +00003876 target_st->st_ino = tswapl(st.st_ino);
ths7d600c82006-12-08 01:32:58 +00003877#if defined(TARGET_PPC) || defined(TARGET_MIPS)
bellard67867302003-11-23 17:05:30 +00003878 target_st->st_mode = tswapl(st.st_mode); /* XXX: check this */
3879 target_st->st_uid = tswap32(st.st_uid);
3880 target_st->st_gid = tswap32(st.st_gid);
blueswir1992f48a2007-10-14 16:27:31 +00003881#elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir11b8dd642007-07-08 10:08:24 +00003882 target_st->st_mode = tswap32(st.st_mode);
3883 target_st->st_uid = tswap32(st.st_uid);
3884 target_st->st_gid = tswap32(st.st_gid);
bellard67867302003-11-23 17:05:30 +00003885#else
bellardec86b0f2003-04-11 00:15:04 +00003886 target_st->st_mode = tswap16(st.st_mode);
bellard31e31b82003-02-18 22:55:36 +00003887 target_st->st_uid = tswap16(st.st_uid);
3888 target_st->st_gid = tswap16(st.st_gid);
bellard67867302003-11-23 17:05:30 +00003889#endif
thse3584652007-06-01 11:49:38 +00003890#if defined(TARGET_MIPS)
3891 /* If this is the same on PPC, then just merge w/ the above ifdef */
3892 target_st->st_nlink = tswapl(st.st_nlink);
3893 target_st->st_rdev = tswapl(st.st_rdev);
blueswir1992f48a2007-10-14 16:27:31 +00003894#elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir11b8dd642007-07-08 10:08:24 +00003895 target_st->st_nlink = tswap32(st.st_nlink);
3896 target_st->st_rdev = tswap32(st.st_rdev);
thse3584652007-06-01 11:49:38 +00003897#else
bellard67867302003-11-23 17:05:30 +00003898 target_st->st_nlink = tswap16(st.st_nlink);
bellard31e31b82003-02-18 22:55:36 +00003899 target_st->st_rdev = tswap16(st.st_rdev);
thse3584652007-06-01 11:49:38 +00003900#endif
bellard31e31b82003-02-18 22:55:36 +00003901 target_st->st_size = tswapl(st.st_size);
3902 target_st->st_blksize = tswapl(st.st_blksize);
3903 target_st->st_blocks = tswapl(st.st_blocks);
bellard7854b052003-03-29 17:22:23 +00003904 target_st->target_st_atime = tswapl(st.st_atime);
3905 target_st->target_st_mtime = tswapl(st.st_mtime);
3906 target_st->target_st_ctime = tswapl(st.st_ctime);
pbrook53a59602006-03-25 19:31:22 +00003907 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00003908 }
3909 }
3910 break;
bellardebc05482003-09-30 21:08:41 +00003911#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00003912 case TARGET_NR_olduname:
3913 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003914#endif
3915#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00003916 case TARGET_NR_iopl:
3917 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003918#endif
bellard31e31b82003-02-18 22:55:36 +00003919 case TARGET_NR_vhangup:
3920 ret = get_errno(vhangup());
3921 break;
bellardebc05482003-09-30 21:08:41 +00003922#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00003923 case TARGET_NR_idle:
3924 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003925#endif
bellard42ad6ae2005-01-03 22:48:11 +00003926#ifdef TARGET_NR_syscall
3927 case TARGET_NR_syscall:
3928 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
3929 break;
3930#endif
bellard31e31b82003-02-18 22:55:36 +00003931 case TARGET_NR_wait4:
3932 {
3933 int status;
blueswir1992f48a2007-10-14 16:27:31 +00003934 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00003935 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00003936 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00003937 if (target_rusage)
3938 rusage_ptr = &rusage;
3939 else
3940 rusage_ptr = NULL;
3941 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
3942 if (!is_error(ret)) {
3943 if (status_ptr)
pbrook53a59602006-03-25 19:31:22 +00003944 tputl(status_ptr, status);
bellard31e31b82003-02-18 22:55:36 +00003945 if (target_rusage) {
bellardb4091862003-05-16 15:39:34 +00003946 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00003947 }
3948 }
3949 }
3950 break;
thse5febef2007-04-01 18:31:35 +00003951#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00003952 case TARGET_NR_swapoff:
pbrook53a59602006-03-25 19:31:22 +00003953 p = lock_user_string(arg1);
3954 ret = get_errno(swapoff(p));
3955 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003956 break;
thse5febef2007-04-01 18:31:35 +00003957#endif
bellard31e31b82003-02-18 22:55:36 +00003958 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00003959 {
pbrook53a59602006-03-25 19:31:22 +00003960 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00003961 struct sysinfo value;
3962 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00003963 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00003964 {
pbrook53a59602006-03-25 19:31:22 +00003965 /* ??? __put_user is probably wrong. */
3966 lock_user_struct(target_value, arg1, 0);
bellarda5448a72004-06-19 16:59:03 +00003967 __put_user(value.uptime, &target_value->uptime);
3968 __put_user(value.loads[0], &target_value->loads[0]);
3969 __put_user(value.loads[1], &target_value->loads[1]);
3970 __put_user(value.loads[2], &target_value->loads[2]);
3971 __put_user(value.totalram, &target_value->totalram);
3972 __put_user(value.freeram, &target_value->freeram);
3973 __put_user(value.sharedram, &target_value->sharedram);
3974 __put_user(value.bufferram, &target_value->bufferram);
3975 __put_user(value.totalswap, &target_value->totalswap);
3976 __put_user(value.freeswap, &target_value->freeswap);
3977 __put_user(value.procs, &target_value->procs);
3978 __put_user(value.totalhigh, &target_value->totalhigh);
3979 __put_user(value.freehigh, &target_value->freehigh);
3980 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00003981 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00003982 }
3983 }
3984 break;
thse5febef2007-04-01 18:31:35 +00003985#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00003986 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00003987 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
3988 break;
thse5febef2007-04-01 18:31:35 +00003989#endif
bellard31e31b82003-02-18 22:55:36 +00003990 case TARGET_NR_fsync:
3991 ret = get_errno(fsync(arg1));
3992 break;
bellard31e31b82003-02-18 22:55:36 +00003993 case TARGET_NR_clone:
bellard1b6b0292003-03-22 17:31:38 +00003994 ret = get_errno(do_fork(cpu_env, arg1, arg2));
3995 break;
bellardec86b0f2003-04-11 00:15:04 +00003996#ifdef __NR_exit_group
3997 /* new thread calls */
3998 case TARGET_NR_exit_group:
bellarde9009672005-04-26 20:42:36 +00003999 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00004000 ret = get_errno(exit_group(arg1));
4001 break;
4002#endif
bellard31e31b82003-02-18 22:55:36 +00004003 case TARGET_NR_setdomainname:
pbrook53a59602006-03-25 19:31:22 +00004004 p = lock_user_string(arg1);
4005 ret = get_errno(setdomainname(p, arg2));
4006 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004007 break;
4008 case TARGET_NR_uname:
4009 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00004010 {
4011 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00004012
pbrook53a59602006-03-25 19:31:22 +00004013 lock_user_struct(buf, arg1, 0);
bellard29e619b2004-09-13 21:41:04 +00004014 ret = get_errno(sys_uname(buf));
4015 if (!is_error(ret)) {
4016 /* Overrite the native machine name with whatever is being
4017 emulated. */
4018 strcpy (buf->machine, UNAME_MACHINE);
pbrookc5937222006-05-14 11:30:38 +00004019 /* Allow the user to override the reported release. */
4020 if (qemu_uname_release && *qemu_uname_release)
4021 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00004022 }
pbrook53a59602006-03-25 19:31:22 +00004023 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00004024 }
bellard31e31b82003-02-18 22:55:36 +00004025 break;
bellard6dbad632003-03-16 18:05:05 +00004026#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00004027 case TARGET_NR_modify_ldt:
pbrook53a59602006-03-25 19:31:22 +00004028 ret = get_errno(do_modify_ldt(cpu_env, arg1, arg2, arg3));
bellard5cd43932003-03-29 16:54:36 +00004029 break;
j_mayer84409dd2007-04-06 08:56:50 +00004030#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00004031 case TARGET_NR_vm86old:
4032 goto unimplemented;
4033 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00004034 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00004035 break;
4036#endif
j_mayer84409dd2007-04-06 08:56:50 +00004037#endif
bellard31e31b82003-02-18 22:55:36 +00004038 case TARGET_NR_adjtimex:
4039 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004040#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00004041 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00004042#endif
bellard31e31b82003-02-18 22:55:36 +00004043 case TARGET_NR_init_module:
4044 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00004045#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00004046 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00004047#endif
bellard31e31b82003-02-18 22:55:36 +00004048 goto unimplemented;
4049 case TARGET_NR_quotactl:
4050 goto unimplemented;
4051 case TARGET_NR_getpgid:
4052 ret = get_errno(getpgid(arg1));
4053 break;
4054 case TARGET_NR_fchdir:
4055 ret = get_errno(fchdir(arg1));
4056 break;
j_mayer84409dd2007-04-06 08:56:50 +00004057#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00004058 case TARGET_NR_bdflush:
4059 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00004060#endif
thse5febef2007-04-01 18:31:35 +00004061#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00004062 case TARGET_NR_sysfs:
4063 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004064#endif
bellard31e31b82003-02-18 22:55:36 +00004065 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00004066 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00004067 break;
thse5febef2007-04-01 18:31:35 +00004068#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00004069 case TARGET_NR_afs_syscall:
4070 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004071#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004072#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004073 case TARGET_NR__llseek:
4074 {
bellard4f2ac232004-04-26 19:44:02 +00004075#if defined (__x86_64__)
4076 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
pbrook53a59602006-03-25 19:31:22 +00004077 tput64(arg4, ret);
bellard4f2ac232004-04-26 19:44:02 +00004078#else
bellard31e31b82003-02-18 22:55:36 +00004079 int64_t res;
4080 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
pbrook53a59602006-03-25 19:31:22 +00004081 tput64(arg4, res);
bellard4f2ac232004-04-26 19:44:02 +00004082#endif
bellard31e31b82003-02-18 22:55:36 +00004083 }
4084 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004085#endif
bellard31e31b82003-02-18 22:55:36 +00004086 case TARGET_NR_getdents:
blueswir1992f48a2007-10-14 16:27:31 +00004087#if TARGET_ABI_BITS != 32
pbrook53a59602006-03-25 19:31:22 +00004088 goto unimplemented;
bellarda315a142005-01-30 22:59:18 +00004089#warning not supported
blueswir1992f48a2007-10-14 16:27:31 +00004090#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00004091 {
pbrook53a59602006-03-25 19:31:22 +00004092 struct target_dirent *target_dirp;
bellard4add45b2003-06-05 01:52:59 +00004093 struct dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004094 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00004095
4096 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00004097 if (!dirp) {
4098 ret = -TARGET_EFAULT;
4099 goto fail;
4100 }
ths3b46e622007-09-17 08:09:54 +00004101
bellard4add45b2003-06-05 01:52:59 +00004102 ret = get_errno(sys_getdents(arg1, dirp, count));
4103 if (!is_error(ret)) {
4104 struct dirent *de;
4105 struct target_dirent *tde;
4106 int len = ret;
4107 int reclen, treclen;
4108 int count1, tnamelen;
4109
4110 count1 = 0;
4111 de = dirp;
pbrook53a59602006-03-25 19:31:22 +00004112 target_dirp = lock_user(arg2, count, 0);
bellard4add45b2003-06-05 01:52:59 +00004113 tde = target_dirp;
4114 while (len > 0) {
4115 reclen = de->d_reclen;
blueswir1992f48a2007-10-14 16:27:31 +00004116 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
bellard4add45b2003-06-05 01:52:59 +00004117 tde->d_reclen = tswap16(treclen);
4118 tde->d_ino = tswapl(de->d_ino);
4119 tde->d_off = tswapl(de->d_off);
blueswir1992f48a2007-10-14 16:27:31 +00004120 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
bellard4add45b2003-06-05 01:52:59 +00004121 if (tnamelen > 256)
4122 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00004123 /* XXX: may not be correct */
bellard4add45b2003-06-05 01:52:59 +00004124 strncpy(tde->d_name, de->d_name, tnamelen);
4125 de = (struct dirent *)((char *)de + reclen);
4126 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00004127 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00004128 count1 += treclen;
4129 }
4130 ret = count1;
4131 }
pbrook53a59602006-03-25 19:31:22 +00004132 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00004133 free(dirp);
4134 }
4135#else
bellard31e31b82003-02-18 22:55:36 +00004136 {
pbrook53a59602006-03-25 19:31:22 +00004137 struct dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004138 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00004139
pbrook53a59602006-03-25 19:31:22 +00004140 dirp = lock_user(arg2, count, 0);
bellard72f03902003-02-18 23:33:18 +00004141 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00004142 if (!is_error(ret)) {
4143 struct dirent *de;
4144 int len = ret;
4145 int reclen;
4146 de = dirp;
4147 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004148 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00004149 if (reclen > len)
4150 break;
bellard8083a3e2003-03-24 23:12:16 +00004151 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00004152 tswapls(&de->d_ino);
4153 tswapls(&de->d_off);
4154 de = (struct dirent *)((char *)de + reclen);
4155 len -= reclen;
4156 }
4157 }
pbrook53a59602006-03-25 19:31:22 +00004158 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00004159 }
bellard4add45b2003-06-05 01:52:59 +00004160#endif
bellard31e31b82003-02-18 22:55:36 +00004161 break;
ths3ae43202007-09-16 21:39:48 +00004162#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00004163 case TARGET_NR_getdents64:
4164 {
pbrook53a59602006-03-25 19:31:22 +00004165 struct dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004166 abi_long count = arg3;
pbrook53a59602006-03-25 19:31:22 +00004167 dirp = lock_user(arg2, count, 0);
bellarddab2ed92003-03-22 15:23:14 +00004168 ret = get_errno(sys_getdents64(arg1, dirp, count));
4169 if (!is_error(ret)) {
4170 struct dirent64 *de;
4171 int len = ret;
4172 int reclen;
4173 de = dirp;
4174 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004175 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00004176 if (reclen > len)
4177 break;
bellard8083a3e2003-03-24 23:12:16 +00004178 de->d_reclen = tswap16(reclen);
bellarddab2ed92003-03-22 15:23:14 +00004179 tswap64s(&de->d_ino);
4180 tswap64s(&de->d_off);
4181 de = (struct dirent64 *)((char *)de + reclen);
4182 len -= reclen;
4183 }
4184 }
pbrook53a59602006-03-25 19:31:22 +00004185 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00004186 }
4187 break;
bellarda541f292004-04-12 20:39:29 +00004188#endif /* TARGET_NR_getdents64 */
thse5febef2007-04-01 18:31:35 +00004189#ifdef TARGET_NR__newselect
bellard31e31b82003-02-18 22:55:36 +00004190 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00004191 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00004192 break;
thse5febef2007-04-01 18:31:35 +00004193#endif
4194#ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00004195 case TARGET_NR_poll:
4196 {
pbrook53a59602006-03-25 19:31:22 +00004197 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00004198 unsigned int nfds = arg2;
4199 int timeout = arg3;
4200 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00004201 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00004202
pbrook53a59602006-03-25 19:31:22 +00004203 target_pfd = lock_user(arg1, sizeof(struct target_pollfd) * nfds, 1);
bellard9de5e442003-03-23 16:49:39 +00004204 pfd = alloca(sizeof(struct pollfd) * nfds);
4205 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004206 pfd[i].fd = tswap32(target_pfd[i].fd);
4207 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00004208 }
4209 ret = get_errno(poll(pfd, nfds, timeout));
4210 if (!is_error(ret)) {
4211 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004212 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00004213 }
pbrook53a59602006-03-25 19:31:22 +00004214 ret += nfds * (sizeof(struct target_pollfd)
4215 - sizeof(struct pollfd));
bellard9de5e442003-03-23 16:49:39 +00004216 }
pbrook53a59602006-03-25 19:31:22 +00004217 unlock_user(target_pfd, arg1, ret);
bellard9de5e442003-03-23 16:49:39 +00004218 }
4219 break;
thse5febef2007-04-01 18:31:35 +00004220#endif
bellard31e31b82003-02-18 22:55:36 +00004221 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00004222 /* NOTE: the flock constant seems to be the same for every
4223 Linux platform */
4224 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00004225 break;
4226 case TARGET_NR_readv:
4227 {
4228 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00004229 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00004230
4231 vec = alloca(count * sizeof(struct iovec));
pbrook53a59602006-03-25 19:31:22 +00004232 lock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00004233 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00004234 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00004235 }
4236 break;
4237 case TARGET_NR_writev:
4238 {
4239 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00004240 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00004241
4242 vec = alloca(count * sizeof(struct iovec));
pbrook53a59602006-03-25 19:31:22 +00004243 lock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00004244 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00004245 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00004246 }
4247 break;
4248 case TARGET_NR_getsid:
4249 ret = get_errno(getsid(arg1));
4250 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004251#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00004252 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00004253 ret = get_errno(fdatasync(arg1));
4254 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004255#endif
bellard31e31b82003-02-18 22:55:36 +00004256 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00004257 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00004258 return value. */
ths0da46a62007-10-20 20:23:07 +00004259 ret = -TARGET_ENOTDIR;
4260 break;
bellard31e31b82003-02-18 22:55:36 +00004261 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00004262 {
pbrook53a59602006-03-25 19:31:22 +00004263 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004264 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00004265
4266 lock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00004267 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004268 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00004269 ret = get_errno(sched_setparam(arg1, &schp));
4270 }
4271 break;
bellard31e31b82003-02-18 22:55:36 +00004272 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00004273 {
pbrook53a59602006-03-25 19:31:22 +00004274 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004275 struct sched_param schp;
4276 ret = get_errno(sched_getparam(arg1, &schp));
4277 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004278 lock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00004279 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004280 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00004281 }
4282 }
4283 break;
bellard31e31b82003-02-18 22:55:36 +00004284 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00004285 {
pbrook53a59602006-03-25 19:31:22 +00004286 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004287 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00004288 lock_user_struct(target_schp, arg3, 1);
bellard5cd43932003-03-29 16:54:36 +00004289 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004290 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00004291 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
4292 }
4293 break;
bellard31e31b82003-02-18 22:55:36 +00004294 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00004295 ret = get_errno(sched_getscheduler(arg1));
4296 break;
bellard31e31b82003-02-18 22:55:36 +00004297 case TARGET_NR_sched_yield:
4298 ret = get_errno(sched_yield());
4299 break;
4300 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00004301 ret = get_errno(sched_get_priority_max(arg1));
4302 break;
bellard31e31b82003-02-18 22:55:36 +00004303 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00004304 ret = get_errno(sched_get_priority_min(arg1));
4305 break;
bellard31e31b82003-02-18 22:55:36 +00004306 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00004307 {
bellard5cd43932003-03-29 16:54:36 +00004308 struct timespec ts;
4309 ret = get_errno(sched_rr_get_interval(arg1, &ts));
4310 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004311 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00004312 }
4313 }
4314 break;
bellard31e31b82003-02-18 22:55:36 +00004315 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00004316 {
bellard1b6b0292003-03-22 17:31:38 +00004317 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00004318 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00004319 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00004320 if (is_error(ret) && arg2) {
4321 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00004322 }
4323 }
4324 break;
thse5febef2007-04-01 18:31:35 +00004325#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00004326 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00004327 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004328#endif
4329#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00004330 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00004331 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004332#endif
bellard31e31b82003-02-18 22:55:36 +00004333 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00004334 switch (arg1)
4335 {
4336 case PR_GET_PDEATHSIG:
4337 {
4338 int deathsig;
4339 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
4340 if (!is_error(ret) && arg2)
4341 tput32(arg2, deathsig);
4342 }
4343 break;
4344 default:
4345 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
4346 break;
4347 }
ths39b9aae2007-02-11 18:36:44 +00004348 break;
bellard67867302003-11-23 17:05:30 +00004349#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00004350 case TARGET_NR_pread:
pbrook53a59602006-03-25 19:31:22 +00004351 page_unprotect_range(arg2, arg3);
4352 p = lock_user(arg2, arg3, 0);
4353 ret = get_errno(pread(arg1, p, arg3, arg4));
4354 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00004355 break;
bellard31e31b82003-02-18 22:55:36 +00004356 case TARGET_NR_pwrite:
pbrook53a59602006-03-25 19:31:22 +00004357 p = lock_user(arg2, arg3, 1);
4358 ret = get_errno(pwrite(arg1, p, arg3, arg4));
4359 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00004360 break;
bellard67867302003-11-23 17:05:30 +00004361#endif
bellard31e31b82003-02-18 22:55:36 +00004362 case TARGET_NR_getcwd:
pbrook53a59602006-03-25 19:31:22 +00004363 p = lock_user(arg1, arg2, 0);
4364 ret = get_errno(sys_getcwd1(p, arg2));
4365 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00004366 break;
4367 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00004368 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004369 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00004370 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004371 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00004372#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
4373 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
thsa04e1342007-09-27 13:57:58 +00004374 ret = do_sigaltstack((struct target_sigaltstack *)arg1,
4375 (struct target_sigaltstack *)arg2,
4376 get_sp_from_cpustate((CPUState *)cpu_env));
4377 break;
4378#else
bellard5cd43932003-03-29 16:54:36 +00004379 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00004380#endif
bellard31e31b82003-02-18 22:55:36 +00004381 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00004382 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004383#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00004384 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00004385 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004386#endif
4387#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00004388 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00004389 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004390#endif
bellard048f6b42005-11-26 18:47:20 +00004391#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00004392 case TARGET_NR_vfork:
bellard1b6b0292003-03-22 17:31:38 +00004393 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00004394 break;
bellard048f6b42005-11-26 18:47:20 +00004395#endif
bellardebc05482003-09-30 21:08:41 +00004396#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00004397 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00004398 {
4399 struct rlimit rlim;
4400 ret = get_errno(getrlimit(arg1, &rlim));
4401 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004402 struct target_rlimit *target_rlim;
4403 lock_user_struct(target_rlim, arg2, 0);
bellard728584b2003-04-29 20:43:36 +00004404 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
4405 target_rlim->rlim_max = tswapl(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00004406 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00004407 }
4408 break;
4409 }
bellardebc05482003-09-30 21:08:41 +00004410#endif
bellarda315a142005-01-30 22:59:18 +00004411#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00004412 case TARGET_NR_truncate64:
pbrook53a59602006-03-25 19:31:22 +00004413 p = lock_user_string(arg1);
4414 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
4415 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00004416 break;
bellarda315a142005-01-30 22:59:18 +00004417#endif
4418#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00004419 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00004420 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00004421 break;
bellarda315a142005-01-30 22:59:18 +00004422#endif
4423#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00004424 case TARGET_NR_stat64:
pbrook53a59602006-03-25 19:31:22 +00004425 p = lock_user_string(arg1);
4426 ret = get_errno(stat(path(p), &st));
4427 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004428 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004429#endif
4430#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00004431 case TARGET_NR_lstat64:
pbrook53a59602006-03-25 19:31:22 +00004432 p = lock_user_string(arg1);
4433 ret = get_errno(lstat(path(p), &st));
4434 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004435 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004436#endif
4437#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00004438 case TARGET_NR_fstat64:
bellard60cd49d2003-03-16 22:53:56 +00004439 {
4440 ret = get_errno(fstat(arg1, &st));
4441 do_stat64:
4442 if (!is_error(ret)) {
pbrookce4defa2006-02-09 16:49:55 +00004443#ifdef TARGET_ARM
4444 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004445 struct target_eabi_stat64 *target_st;
4446 lock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00004447 memset(target_st, 0, sizeof(struct target_eabi_stat64));
pbrook53a59602006-03-25 19:31:22 +00004448 /* put_user is probably wrong. */
pbrookce4defa2006-02-09 16:49:55 +00004449 put_user(st.st_dev, &target_st->st_dev);
4450 put_user(st.st_ino, &target_st->st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004451#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
pbrookce4defa2006-02-09 16:49:55 +00004452 put_user(st.st_ino, &target_st->__st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004453#endif
pbrookce4defa2006-02-09 16:49:55 +00004454 put_user(st.st_mode, &target_st->st_mode);
4455 put_user(st.st_nlink, &target_st->st_nlink);
4456 put_user(st.st_uid, &target_st->st_uid);
4457 put_user(st.st_gid, &target_st->st_gid);
4458 put_user(st.st_rdev, &target_st->st_rdev);
4459 /* XXX: better use of kernel struct */
4460 put_user(st.st_size, &target_st->st_size);
4461 put_user(st.st_blksize, &target_st->st_blksize);
4462 put_user(st.st_blocks, &target_st->st_blocks);
4463 put_user(st.st_atime, &target_st->target_st_atime);
4464 put_user(st.st_mtime, &target_st->target_st_mtime);
4465 put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00004466 unlock_user_struct(target_st, arg2, 0);
pbrookce4defa2006-02-09 16:49:55 +00004467 } else
4468#endif
4469 {
pbrook53a59602006-03-25 19:31:22 +00004470 struct target_stat64 *target_st;
4471 lock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00004472 memset(target_st, 0, sizeof(struct target_stat64));
pbrook53a59602006-03-25 19:31:22 +00004473 /* ??? put_user is probably wrong. */
pbrookce4defa2006-02-09 16:49:55 +00004474 put_user(st.st_dev, &target_st->st_dev);
4475 put_user(st.st_ino, &target_st->st_ino);
4476#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4477 put_user(st.st_ino, &target_st->__st_ino);
4478#endif
4479 put_user(st.st_mode, &target_st->st_mode);
4480 put_user(st.st_nlink, &target_st->st_nlink);
4481 put_user(st.st_uid, &target_st->st_uid);
4482 put_user(st.st_gid, &target_st->st_gid);
4483 put_user(st.st_rdev, &target_st->st_rdev);
4484 /* XXX: better use of kernel struct */
4485 put_user(st.st_size, &target_st->st_size);
4486 put_user(st.st_blksize, &target_st->st_blksize);
4487 put_user(st.st_blocks, &target_st->st_blocks);
4488 put_user(st.st_atime, &target_st->target_st_atime);
4489 put_user(st.st_mtime, &target_st->target_st_mtime);
4490 put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00004491 unlock_user_struct(target_st, arg2, 0);
pbrookce4defa2006-02-09 16:49:55 +00004492 }
bellard60cd49d2003-03-16 22:53:56 +00004493 }
4494 }
4495 break;
bellarda315a142005-01-30 22:59:18 +00004496#endif
bellard67867302003-11-23 17:05:30 +00004497#ifdef USE_UID16
4498 case TARGET_NR_lchown:
pbrook53a59602006-03-25 19:31:22 +00004499 p = lock_user_string(arg1);
4500 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
4501 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00004502 break;
4503 case TARGET_NR_getuid:
4504 ret = get_errno(high2lowuid(getuid()));
4505 break;
4506 case TARGET_NR_getgid:
4507 ret = get_errno(high2lowgid(getgid()));
4508 break;
4509 case TARGET_NR_geteuid:
4510 ret = get_errno(high2lowuid(geteuid()));
4511 break;
4512 case TARGET_NR_getegid:
4513 ret = get_errno(high2lowgid(getegid()));
4514 break;
4515 case TARGET_NR_setreuid:
4516 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
4517 break;
4518 case TARGET_NR_setregid:
4519 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
4520 break;
4521 case TARGET_NR_getgroups:
4522 {
4523 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004524 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00004525 gid_t *grouplist;
4526 int i;
4527
4528 grouplist = alloca(gidsetsize * sizeof(gid_t));
4529 ret = get_errno(getgroups(gidsetsize, grouplist));
4530 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004531 target_grouplist = lock_user(arg2, gidsetsize * 2, 0);
bellard67867302003-11-23 17:05:30 +00004532 for(i = 0;i < gidsetsize; i++)
4533 target_grouplist[i] = tswap16(grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00004534 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00004535 }
4536 }
4537 break;
4538 case TARGET_NR_setgroups:
4539 {
4540 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004541 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00004542 gid_t *grouplist;
4543 int i;
4544
4545 grouplist = alloca(gidsetsize * sizeof(gid_t));
pbrook53a59602006-03-25 19:31:22 +00004546 target_grouplist = lock_user(arg2, gidsetsize * 2, 1);
bellard67867302003-11-23 17:05:30 +00004547 for(i = 0;i < gidsetsize; i++)
4548 grouplist[i] = tswap16(target_grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00004549 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00004550 ret = get_errno(setgroups(gidsetsize, grouplist));
4551 }
4552 break;
4553 case TARGET_NR_fchown:
4554 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
4555 break;
thsccfa72b2007-09-24 09:23:34 +00004556#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
4557 case TARGET_NR_fchownat:
4558 if (!arg2) {
ths0da46a62007-10-20 20:23:07 +00004559 ret = -TARGET_EFAULT;
thsccfa72b2007-09-24 09:23:34 +00004560 goto fail;
4561 }
4562 p = lock_user_string(arg2);
4563 if (!access_ok(VERIFY_READ, p, 1))
ths0da46a62007-10-20 20:23:07 +00004564 /* Don't "goto fail" so that cleanup can happen. */
4565 ret = -TARGET_EFAULT;
thsccfa72b2007-09-24 09:23:34 +00004566 else
4567 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
4568 if (p)
4569 unlock_user(p, arg2, 0);
4570 break;
4571#endif
bellard67867302003-11-23 17:05:30 +00004572#ifdef TARGET_NR_setresuid
4573 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00004574 ret = get_errno(setresuid(low2highuid(arg1),
4575 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00004576 low2highuid(arg3)));
4577 break;
4578#endif
4579#ifdef TARGET_NR_getresuid
4580 case TARGET_NR_getresuid:
4581 {
pbrook53a59602006-03-25 19:31:22 +00004582 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00004583 ret = get_errno(getresuid(&ruid, &euid, &suid));
4584 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004585 tput16(arg1, tswap16(high2lowuid(ruid)));
4586 tput16(arg2, tswap16(high2lowuid(euid)));
4587 tput16(arg3, tswap16(high2lowuid(suid)));
bellard67867302003-11-23 17:05:30 +00004588 }
4589 }
4590 break;
4591#endif
4592#ifdef TARGET_NR_getresgid
4593 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00004594 ret = get_errno(setresgid(low2highgid(arg1),
4595 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00004596 low2highgid(arg3)));
4597 break;
4598#endif
4599#ifdef TARGET_NR_getresgid
4600 case TARGET_NR_getresgid:
4601 {
pbrook53a59602006-03-25 19:31:22 +00004602 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00004603 ret = get_errno(getresgid(&rgid, &egid, &sgid));
4604 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004605 tput16(arg1, tswap16(high2lowgid(rgid)));
4606 tput16(arg2, tswap16(high2lowgid(egid)));
4607 tput16(arg3, tswap16(high2lowgid(sgid)));
bellard67867302003-11-23 17:05:30 +00004608 }
4609 }
4610 break;
4611#endif
4612 case TARGET_NR_chown:
pbrook53a59602006-03-25 19:31:22 +00004613 p = lock_user_string(arg1);
4614 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
4615 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00004616 break;
4617 case TARGET_NR_setuid:
4618 ret = get_errno(setuid(low2highuid(arg1)));
4619 break;
4620 case TARGET_NR_setgid:
4621 ret = get_errno(setgid(low2highgid(arg1)));
4622 break;
4623 case TARGET_NR_setfsuid:
4624 ret = get_errno(setfsuid(arg1));
4625 break;
4626 case TARGET_NR_setfsgid:
4627 ret = get_errno(setfsgid(arg1));
4628 break;
4629#endif /* USE_UID16 */
4630
bellarda315a142005-01-30 22:59:18 +00004631#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00004632 case TARGET_NR_lchown32:
pbrook53a59602006-03-25 19:31:22 +00004633 p = lock_user_string(arg1);
4634 ret = get_errno(lchown(p, arg2, arg3));
4635 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00004636 break;
bellarda315a142005-01-30 22:59:18 +00004637#endif
4638#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00004639 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00004640 ret = get_errno(getuid());
4641 break;
bellarda315a142005-01-30 22:59:18 +00004642#endif
4643#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00004644 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00004645 ret = get_errno(getgid());
4646 break;
bellarda315a142005-01-30 22:59:18 +00004647#endif
4648#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00004649 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00004650 ret = get_errno(geteuid());
4651 break;
bellarda315a142005-01-30 22:59:18 +00004652#endif
4653#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00004654 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00004655 ret = get_errno(getegid());
4656 break;
bellarda315a142005-01-30 22:59:18 +00004657#endif
4658#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00004659 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00004660 ret = get_errno(setreuid(arg1, arg2));
4661 break;
bellarda315a142005-01-30 22:59:18 +00004662#endif
4663#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00004664 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00004665 ret = get_errno(setregid(arg1, arg2));
4666 break;
bellarda315a142005-01-30 22:59:18 +00004667#endif
4668#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00004669 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00004670 {
4671 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004672 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00004673 gid_t *grouplist;
4674 int i;
4675
4676 grouplist = alloca(gidsetsize * sizeof(gid_t));
4677 ret = get_errno(getgroups(gidsetsize, grouplist));
4678 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004679 target_grouplist = lock_user(arg2, gidsetsize * 4, 0);
bellard99c475a2005-01-31 20:45:13 +00004680 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00004681 target_grouplist[i] = tswap32(grouplist[i]);
4682 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00004683 }
4684 }
4685 break;
bellarda315a142005-01-30 22:59:18 +00004686#endif
4687#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00004688 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00004689 {
4690 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004691 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00004692 gid_t *grouplist;
4693 int i;
ths3b46e622007-09-17 08:09:54 +00004694
bellard99c475a2005-01-31 20:45:13 +00004695 grouplist = alloca(gidsetsize * sizeof(gid_t));
pbrook53a59602006-03-25 19:31:22 +00004696 target_grouplist = lock_user(arg2, gidsetsize * 4, 1);
bellard99c475a2005-01-31 20:45:13 +00004697 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00004698 grouplist[i] = tswap32(target_grouplist[i]);
4699 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00004700 ret = get_errno(setgroups(gidsetsize, grouplist));
4701 }
4702 break;
bellarda315a142005-01-30 22:59:18 +00004703#endif
4704#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00004705 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00004706 ret = get_errno(fchown(arg1, arg2, arg3));
4707 break;
bellarda315a142005-01-30 22:59:18 +00004708#endif
4709#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00004710 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00004711 ret = get_errno(setresuid(arg1, arg2, arg3));
4712 break;
bellarda315a142005-01-30 22:59:18 +00004713#endif
4714#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00004715 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00004716 {
pbrook53a59602006-03-25 19:31:22 +00004717 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00004718 ret = get_errno(getresuid(&ruid, &euid, &suid));
4719 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004720 tput32(arg1, tswap32(ruid));
4721 tput32(arg2, tswap32(euid));
4722 tput32(arg3, tswap32(suid));
bellardb03c60f2003-03-23 17:19:56 +00004723 }
4724 }
4725 break;
bellarda315a142005-01-30 22:59:18 +00004726#endif
4727#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00004728 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00004729 ret = get_errno(setresgid(arg1, arg2, arg3));
4730 break;
bellarda315a142005-01-30 22:59:18 +00004731#endif
4732#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00004733 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00004734 {
pbrook53a59602006-03-25 19:31:22 +00004735 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00004736 ret = get_errno(getresgid(&rgid, &egid, &sgid));
4737 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004738 tput32(arg1, tswap32(rgid));
4739 tput32(arg2, tswap32(egid));
4740 tput32(arg3, tswap32(sgid));
bellardb03c60f2003-03-23 17:19:56 +00004741 }
4742 }
4743 break;
bellarda315a142005-01-30 22:59:18 +00004744#endif
4745#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00004746 case TARGET_NR_chown32:
pbrook53a59602006-03-25 19:31:22 +00004747 p = lock_user_string(arg1);
4748 ret = get_errno(chown(p, arg2, arg3));
4749 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00004750 break;
bellarda315a142005-01-30 22:59:18 +00004751#endif
4752#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00004753 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00004754 ret = get_errno(setuid(arg1));
4755 break;
bellarda315a142005-01-30 22:59:18 +00004756#endif
4757#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00004758 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00004759 ret = get_errno(setgid(arg1));
4760 break;
bellarda315a142005-01-30 22:59:18 +00004761#endif
4762#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00004763 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00004764 ret = get_errno(setfsuid(arg1));
4765 break;
bellarda315a142005-01-30 22:59:18 +00004766#endif
4767#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00004768 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00004769 ret = get_errno(setfsgid(arg1));
4770 break;
bellarda315a142005-01-30 22:59:18 +00004771#endif
bellard67867302003-11-23 17:05:30 +00004772
bellard31e31b82003-02-18 22:55:36 +00004773 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00004774 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00004775#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00004776 case TARGET_NR_mincore:
bellardb03c60f2003-03-23 17:19:56 +00004777 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00004778#endif
4779#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00004780 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00004781 /* A straight passthrough may not be safe because qemu sometimes
4782 turns private flie-backed mappings into anonymous mappings.
4783 This will break MADV_DONTNEED.
4784 This is a hint, so ignoring and returning success is ok. */
4785 ret = get_errno(0);
4786 break;
bellardffa65c32004-01-04 23:57:22 +00004787#endif
blueswir1992f48a2007-10-14 16:27:31 +00004788#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00004789 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00004790 {
thsb1e341e2007-03-20 21:50:52 +00004791 int cmd;
bellard77e46722003-04-29 20:39:06 +00004792 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00004793 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00004794#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00004795 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00004796#endif
bellard77e46722003-04-29 20:39:06 +00004797
thsb1e341e2007-03-20 21:50:52 +00004798 switch(arg2){
4799 case TARGET_F_GETLK64:
4800 cmd = F_GETLK64;
thsa7222582007-03-22 15:08:55 +00004801 break;
thsb1e341e2007-03-20 21:50:52 +00004802 case TARGET_F_SETLK64:
4803 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00004804 break;
thsb1e341e2007-03-20 21:50:52 +00004805 case TARGET_F_SETLKW64:
4806 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00004807 break;
thsb1e341e2007-03-20 21:50:52 +00004808 default:
4809 cmd = arg2;
thsa7222582007-03-22 15:08:55 +00004810 break;
thsb1e341e2007-03-20 21:50:52 +00004811 }
4812
bellard60cd49d2003-03-16 22:53:56 +00004813 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00004814 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00004815#ifdef TARGET_ARM
4816 if (((CPUARMState *)cpu_env)->eabi) {
4817 lock_user_struct(target_efl, arg3, 1);
4818 fl.l_type = tswap16(target_efl->l_type);
4819 fl.l_whence = tswap16(target_efl->l_whence);
4820 fl.l_start = tswap64(target_efl->l_start);
4821 fl.l_len = tswap64(target_efl->l_len);
4822 fl.l_pid = tswapl(target_efl->l_pid);
4823 unlock_user_struct(target_efl, arg3, 0);
4824 } else
4825#endif
4826 {
4827 lock_user_struct(target_fl, arg3, 1);
4828 fl.l_type = tswap16(target_fl->l_type);
4829 fl.l_whence = tswap16(target_fl->l_whence);
4830 fl.l_start = tswap64(target_fl->l_start);
4831 fl.l_len = tswap64(target_fl->l_len);
4832 fl.l_pid = tswapl(target_fl->l_pid);
4833 unlock_user_struct(target_fl, arg3, 0);
4834 }
thsb1e341e2007-03-20 21:50:52 +00004835 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00004836 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00004837#ifdef TARGET_ARM
4838 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004839 lock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004840 target_efl->l_type = tswap16(fl.l_type);
4841 target_efl->l_whence = tswap16(fl.l_whence);
4842 target_efl->l_start = tswap64(fl.l_start);
4843 target_efl->l_len = tswap64(fl.l_len);
4844 target_efl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004845 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004846 } else
4847#endif
4848 {
pbrook53a59602006-03-25 19:31:22 +00004849 lock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004850 target_fl->l_type = tswap16(fl.l_type);
4851 target_fl->l_whence = tswap16(fl.l_whence);
4852 target_fl->l_start = tswap64(fl.l_start);
4853 target_fl->l_len = tswap64(fl.l_len);
4854 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004855 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004856 }
bellard77e46722003-04-29 20:39:06 +00004857 }
4858 break;
4859
thsb1e341e2007-03-20 21:50:52 +00004860 case TARGET_F_SETLK64:
4861 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00004862#ifdef TARGET_ARM
4863 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004864 lock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004865 fl.l_type = tswap16(target_efl->l_type);
4866 fl.l_whence = tswap16(target_efl->l_whence);
4867 fl.l_start = tswap64(target_efl->l_start);
4868 fl.l_len = tswap64(target_efl->l_len);
4869 fl.l_pid = tswapl(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004870 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004871 } else
4872#endif
4873 {
pbrook53a59602006-03-25 19:31:22 +00004874 lock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004875 fl.l_type = tswap16(target_fl->l_type);
4876 fl.l_whence = tswap16(target_fl->l_whence);
4877 fl.l_start = tswap64(target_fl->l_start);
4878 fl.l_len = tswap64(target_fl->l_len);
4879 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004880 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004881 }
thsb1e341e2007-03-20 21:50:52 +00004882 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00004883 break;
bellard60cd49d2003-03-16 22:53:56 +00004884 default:
thsb1e341e2007-03-20 21:50:52 +00004885 ret = get_errno(do_fcntl(arg1, cmd, arg3));
bellard60cd49d2003-03-16 22:53:56 +00004886 break;
4887 }
bellard77e46722003-04-29 20:39:06 +00004888 break;
4889 }
bellard60cd49d2003-03-16 22:53:56 +00004890#endif
ths7d600c82006-12-08 01:32:58 +00004891#ifdef TARGET_NR_cacheflush
4892 case TARGET_NR_cacheflush:
4893 /* self-modifying code is handled automatically, so nothing needed */
4894 ret = 0;
4895 break;
4896#endif
bellardebc05482003-09-30 21:08:41 +00004897#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00004898 case TARGET_NR_security:
4899 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004900#endif
bellardc573ff62004-01-04 15:51:36 +00004901#ifdef TARGET_NR_getpagesize
4902 case TARGET_NR_getpagesize:
4903 ret = TARGET_PAGE_SIZE;
4904 break;
4905#endif
bellard31e31b82003-02-18 22:55:36 +00004906 case TARGET_NR_gettid:
4907 ret = get_errno(gettid());
4908 break;
thse5febef2007-04-01 18:31:35 +00004909#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00004910 case TARGET_NR_readahead:
bellard5cd43932003-03-29 16:54:36 +00004911 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004912#endif
bellardebc05482003-09-30 21:08:41 +00004913#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00004914 case TARGET_NR_setxattr:
4915 case TARGET_NR_lsetxattr:
4916 case TARGET_NR_fsetxattr:
4917 case TARGET_NR_getxattr:
4918 case TARGET_NR_lgetxattr:
4919 case TARGET_NR_fgetxattr:
4920 case TARGET_NR_listxattr:
4921 case TARGET_NR_llistxattr:
4922 case TARGET_NR_flistxattr:
4923 case TARGET_NR_removexattr:
4924 case TARGET_NR_lremovexattr:
4925 case TARGET_NR_fremovexattr:
bellard5cd43932003-03-29 16:54:36 +00004926 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00004927#endif
4928#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00004929 case TARGET_NR_set_thread_area:
ths6f5b89a2007-03-02 20:48:00 +00004930#ifdef TARGET_MIPS
4931 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
4932 ret = 0;
4933 break;
4934#else
4935 goto unimplemented_nowarn;
4936#endif
4937#endif
4938#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00004939 case TARGET_NR_get_thread_area:
4940 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00004941#endif
bellard48dc41e2006-06-21 18:15:50 +00004942#ifdef TARGET_NR_getdomainname
4943 case TARGET_NR_getdomainname:
4944 goto unimplemented_nowarn;
4945#endif
ths6f5b89a2007-03-02 20:48:00 +00004946
thsb5906f92007-03-19 13:32:45 +00004947#ifdef TARGET_NR_clock_gettime
4948 case TARGET_NR_clock_gettime:
4949 {
4950 struct timespec ts;
4951 ret = get_errno(clock_gettime(arg1, &ts));
4952 if (!is_error(ret)) {
4953 host_to_target_timespec(arg2, &ts);
4954 }
4955 break;
4956 }
4957#endif
4958#ifdef TARGET_NR_clock_getres
4959 case TARGET_NR_clock_getres:
4960 {
4961 struct timespec ts;
4962 ret = get_errno(clock_getres(arg1, &ts));
4963 if (!is_error(ret)) {
4964 host_to_target_timespec(arg2, &ts);
4965 }
4966 break;
4967 }
4968#endif
4969
ths6f5b89a2007-03-02 20:48:00 +00004970#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
4971 case TARGET_NR_set_tid_address:
4972 ret = get_errno(set_tid_address((int *) arg1));
4973 break;
4974#endif
4975
ths3ae43202007-09-16 21:39:48 +00004976#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00004977 case TARGET_NR_tkill:
4978 ret = get_errno(sys_tkill((int)arg1, (int)arg2));
4979 break;
4980#endif
4981
ths3ae43202007-09-16 21:39:48 +00004982#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00004983 case TARGET_NR_tgkill:
4984 ret = get_errno(sys_tgkill((int)arg1, (int)arg2, (int)arg3));
4985 break;
4986#endif
4987
ths4f2b1fe2007-06-21 21:57:12 +00004988#ifdef TARGET_NR_set_robust_list
4989 case TARGET_NR_set_robust_list:
4990 goto unimplemented_nowarn;
4991#endif
4992
ths9007f0e2007-09-25 17:50:37 +00004993#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
4994 case TARGET_NR_utimensat:
4995 {
4996 struct timespec ts[2];
4997 target_to_host_timespec(ts, arg3);
4998 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
4999 if (!arg2)
5000 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
5001 else {
5002 p = lock_user_string(arg2);
5003 if (!access_ok(VERIFY_READ, p, 1))
ths0da46a62007-10-20 20:23:07 +00005004 /* Don't "goto fail" so that cleanup can happen. */
5005 ret = -TARGET_EFAULT;
ths9007f0e2007-09-25 17:50:37 +00005006 else
5007 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
5008 if (p)
5009 unlock_user(p, arg2, 0);
5010 }
5011 }
5012 break;
5013#endif
5014
bellard31e31b82003-02-18 22:55:36 +00005015 default:
5016 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00005017 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00005018#if defined(TARGET_NR_setxattr) || defined(TARGET_NR_get_thread_area) || defined(TARGET_NR_getdomainname) || defined(TARGET_NR_set_robust_list)
bellard5cd43932003-03-29 16:54:36 +00005019 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00005020#endif
ths0da46a62007-10-20 20:23:07 +00005021 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00005022 break;
5023 }
5024 fail:
bellardc573ff62004-01-04 15:51:36 +00005025#ifdef DEBUG
5026 gemu_log(" = %ld\n", ret);
5027#endif
bellard31e31b82003-02-18 22:55:36 +00005028 return ret;
5029}