blob: cf39f153500a5ad7305c3237a3d2158f1e513f58 [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
981 return get_errno(socket(domain, type, protocol));
982}
983
ths0da46a62007-10-20 20:23:07 +0000984/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000985static abi_long do_bind(int sockfd, abi_ulong target_addr,
986 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +0000987{
988 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +0000989
bellard3532fa72006-06-24 15:06:03 +0000990 target_to_host_sockaddr(addr, target_addr, addrlen);
991 return get_errno(bind(sockfd, addr, addrlen));
992}
993
ths0da46a62007-10-20 20:23:07 +0000994/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000995static abi_long do_connect(int sockfd, abi_ulong target_addr,
996 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +0000997{
998 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +0000999
bellard3532fa72006-06-24 15:06:03 +00001000 target_to_host_sockaddr(addr, target_addr, addrlen);
1001 return get_errno(connect(sockfd, addr, addrlen));
1002}
1003
ths0da46a62007-10-20 20:23:07 +00001004/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001005static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1006 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001007{
blueswir1992f48a2007-10-14 16:27:31 +00001008 abi_long ret;
bellard3532fa72006-06-24 15:06:03 +00001009 struct target_msghdr *msgp;
1010 struct msghdr msg;
1011 int count;
1012 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001013 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001014
1015 lock_user_struct(msgp, target_msg, 1);
1016 if (msgp->msg_name) {
1017 msg.msg_namelen = tswap32(msgp->msg_namelen);
1018 msg.msg_name = alloca(msg.msg_namelen);
1019 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1020 msg.msg_namelen);
1021 } else {
1022 msg.msg_name = NULL;
1023 msg.msg_namelen = 0;
1024 }
1025 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1026 msg.msg_control = alloca(msg.msg_controllen);
1027 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001028
bellard3532fa72006-06-24 15:06:03 +00001029 count = tswapl(msgp->msg_iovlen);
1030 vec = alloca(count * sizeof(struct iovec));
1031 target_vec = tswapl(msgp->msg_iov);
1032 lock_iovec(vec, target_vec, count, send);
1033 msg.msg_iovlen = count;
1034 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001035
bellard3532fa72006-06-24 15:06:03 +00001036 if (send) {
1037 target_to_host_cmsg(&msg, msgp);
1038 ret = get_errno(sendmsg(fd, &msg, flags));
1039 } else {
1040 ret = get_errno(recvmsg(fd, &msg, flags));
1041 if (!is_error(ret))
1042 host_to_target_cmsg(msgp, &msg);
1043 }
1044 unlock_iovec(vec, target_vec, count, !send);
1045 return ret;
1046}
1047
ths0da46a62007-10-20 20:23:07 +00001048/* do_accept() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001049static abi_long do_accept(int fd, abi_ulong target_addr,
1050 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001051{
1052 socklen_t addrlen = tget32(target_addrlen);
ths197ea352006-12-11 19:13:59 +00001053 void *addr = alloca(addrlen);
blueswir1992f48a2007-10-14 16:27:31 +00001054 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001055
1056 ret = get_errno(accept(fd, addr, &addrlen));
1057 if (!is_error(ret)) {
1058 host_to_target_sockaddr(target_addr, addr, addrlen);
1059 tput32(target_addrlen, addrlen);
1060 }
1061 return ret;
1062}
1063
ths0da46a62007-10-20 20:23:07 +00001064/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001065static abi_long do_getpeername(int fd, abi_ulong target_addr,
1066 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001067{
1068 socklen_t addrlen = tget32(target_addrlen);
ths01c227f2007-03-19 13:05:13 +00001069 void *addr = alloca(addrlen);
blueswir1992f48a2007-10-14 16:27:31 +00001070 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001071
1072 ret = get_errno(getpeername(fd, addr, &addrlen));
1073 if (!is_error(ret)) {
1074 host_to_target_sockaddr(target_addr, addr, addrlen);
1075 tput32(target_addrlen, addrlen);
1076 }
1077 return ret;
1078}
1079
ths0da46a62007-10-20 20:23:07 +00001080/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001081static abi_long do_getsockname(int fd, abi_ulong target_addr,
1082 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001083{
1084 socklen_t addrlen = tget32(target_addrlen);
ths01c227f2007-03-19 13:05:13 +00001085 void *addr = alloca(addrlen);
blueswir1992f48a2007-10-14 16:27:31 +00001086 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001087
1088 ret = get_errno(getsockname(fd, addr, &addrlen));
1089 if (!is_error(ret)) {
1090 host_to_target_sockaddr(target_addr, addr, addrlen);
1091 tput32(target_addrlen, addrlen);
1092 }
1093 return ret;
1094}
1095
ths0da46a62007-10-20 20:23:07 +00001096/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001097static abi_long do_socketpair(int domain, int type, int protocol,
1098 abi_ulong target_tab)
pbrook1be9e1d2006-11-19 15:26:04 +00001099{
1100 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00001101 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001102
1103 ret = get_errno(socketpair(domain, type, protocol, tab));
1104 if (!is_error(ret)) {
1105 tput32(target_tab, tab[0]);
1106 tput32(target_tab + 4, tab[1]);
1107 }
1108 return ret;
1109}
1110
ths0da46a62007-10-20 20:23:07 +00001111/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001112static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1113 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001114{
1115 void *addr;
1116 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001117 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001118
1119 host_msg = lock_user(msg, len, 1);
1120 if (target_addr) {
1121 addr = alloca(addrlen);
1122 target_to_host_sockaddr(addr, target_addr, addrlen);
1123 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1124 } else {
1125 ret = get_errno(send(fd, host_msg, len, flags));
1126 }
1127 unlock_user(host_msg, msg, 0);
1128 return ret;
1129}
1130
ths0da46a62007-10-20 20:23:07 +00001131/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001132static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1133 abi_ulong target_addr,
1134 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001135{
1136 socklen_t addrlen;
1137 void *addr;
1138 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001139 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001140
1141 host_msg = lock_user(msg, len, 0);
1142 if (target_addr) {
1143 addrlen = tget32(target_addrlen);
1144 addr = alloca(addrlen);
1145 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1146 } else {
1147 addr = NULL; /* To keep compiler quiet. */
1148 ret = get_errno(recv(fd, host_msg, len, flags));
1149 }
1150 if (!is_error(ret)) {
1151 if (target_addr) {
1152 host_to_target_sockaddr(target_addr, addr, addrlen);
1153 tput32(target_addrlen, addrlen);
1154 }
1155 unlock_user(host_msg, msg, len);
1156 } else {
1157 unlock_user(host_msg, msg, 0);
1158 }
1159 return ret;
1160}
1161
j_mayer32407102007-09-26 23:01:49 +00001162#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00001163/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001164static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00001165{
blueswir1992f48a2007-10-14 16:27:31 +00001166 abi_long ret;
1167 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00001168
1169 switch(num) {
1170 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00001171 {
pbrook53a59602006-03-25 19:31:22 +00001172 int domain = tgetl(vptr);
1173 int type = tgetl(vptr + n);
1174 int protocol = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001175 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00001176 }
bellard31e31b82003-02-18 22:55:36 +00001177 break;
1178 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00001179 {
pbrook53a59602006-03-25 19:31:22 +00001180 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001181 abi_ulong target_addr = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001182 socklen_t addrlen = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001183 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001184 }
bellard31e31b82003-02-18 22:55:36 +00001185 break;
1186 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00001187 {
pbrook53a59602006-03-25 19:31:22 +00001188 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001189 abi_ulong target_addr = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001190 socklen_t addrlen = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001191 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001192 }
bellard31e31b82003-02-18 22:55:36 +00001193 break;
1194 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00001195 {
pbrook53a59602006-03-25 19:31:22 +00001196 int sockfd = tgetl(vptr);
1197 int backlog = tgetl(vptr + n);
bellard7854b052003-03-29 17:22:23 +00001198 ret = get_errno(listen(sockfd, backlog));
1199 }
bellard31e31b82003-02-18 22:55:36 +00001200 break;
1201 case SOCKOP_accept:
1202 {
pbrook53a59602006-03-25 19:31:22 +00001203 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001204 abi_ulong target_addr = tgetl(vptr + n);
1205 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001206 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001207 }
1208 break;
1209 case SOCKOP_getsockname:
1210 {
pbrook53a59602006-03-25 19:31:22 +00001211 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001212 abi_ulong target_addr = tgetl(vptr + n);
1213 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001214 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001215 }
1216 break;
1217 case SOCKOP_getpeername:
1218 {
pbrook53a59602006-03-25 19:31:22 +00001219 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001220 abi_ulong target_addr = tgetl(vptr + n);
1221 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001222 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001223 }
1224 break;
1225 case SOCKOP_socketpair:
1226 {
pbrook53a59602006-03-25 19:31:22 +00001227 int domain = tgetl(vptr);
1228 int type = tgetl(vptr + n);
1229 int protocol = tgetl(vptr + 2 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001230 abi_ulong tab = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001231 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00001232 }
1233 break;
1234 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00001235 {
pbrook53a59602006-03-25 19:31:22 +00001236 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001237 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001238 size_t len = tgetl(vptr + 2 * n);
1239 int flags = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001240 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001241 }
bellard31e31b82003-02-18 22:55:36 +00001242 break;
1243 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00001244 {
pbrook53a59602006-03-25 19:31:22 +00001245 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001246 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001247 size_t len = tgetl(vptr + 2 * n);
1248 int flags = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001249 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001250 }
bellard31e31b82003-02-18 22:55:36 +00001251 break;
1252 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00001253 {
pbrook53a59602006-03-25 19:31:22 +00001254 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001255 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001256 size_t len = tgetl(vptr + 2 * n);
1257 int flags = tgetl(vptr + 3 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001258 abi_ulong addr = tgetl(vptr + 4 * n);
pbrook53a59602006-03-25 19:31:22 +00001259 socklen_t addrlen = tgetl(vptr + 5 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001260 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001261 }
bellard31e31b82003-02-18 22:55:36 +00001262 break;
1263 case SOCKOP_recvfrom:
1264 {
pbrook53a59602006-03-25 19:31:22 +00001265 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001266 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001267 size_t len = tgetl(vptr + 2 * n);
1268 int flags = tgetl(vptr + 3 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001269 abi_ulong addr = tgetl(vptr + 4 * n);
1270 abi_ulong addrlen = tgetl(vptr + 5 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001271 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00001272 }
1273 break;
1274 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00001275 {
pbrook53a59602006-03-25 19:31:22 +00001276 int sockfd = tgetl(vptr);
1277 int how = tgetl(vptr + n);
bellard7854b052003-03-29 17:22:23 +00001278
1279 ret = get_errno(shutdown(sockfd, how));
1280 }
bellard31e31b82003-02-18 22:55:36 +00001281 break;
1282 case SOCKOP_sendmsg:
1283 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00001284 {
1285 int fd;
blueswir1992f48a2007-10-14 16:27:31 +00001286 abi_ulong target_msg;
bellard3532fa72006-06-24 15:06:03 +00001287 int flags;
bellard1a9353d2003-03-16 20:28:50 +00001288
pbrook53a59602006-03-25 19:31:22 +00001289 fd = tgetl(vptr);
bellard3532fa72006-06-24 15:06:03 +00001290 target_msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001291 flags = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001292
ths5fafdf22007-09-16 21:08:06 +00001293 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00001294 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00001295 }
1296 break;
bellard31e31b82003-02-18 22:55:36 +00001297 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00001298 {
pbrook53a59602006-03-25 19:31:22 +00001299 int sockfd = tgetl(vptr);
1300 int level = tgetl(vptr + n);
1301 int optname = tgetl(vptr + 2 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001302 abi_ulong optval = tgetl(vptr + 3 * n);
pbrook53a59602006-03-25 19:31:22 +00001303 socklen_t optlen = tgetl(vptr + 4 * n);
bellard7854b052003-03-29 17:22:23 +00001304
1305 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1306 }
1307 break;
bellard31e31b82003-02-18 22:55:36 +00001308 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00001309 {
pbrook53a59602006-03-25 19:31:22 +00001310 int sockfd = tgetl(vptr);
1311 int level = tgetl(vptr + n);
1312 int optname = tgetl(vptr + 2 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001313 abi_ulong optval = tgetl(vptr + 3 * n);
1314 abi_ulong poptlen = tgetl(vptr + 4 * n);
bellard7854b052003-03-29 17:22:23 +00001315
bellard8853f862004-02-22 14:57:26 +00001316 ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
bellard7854b052003-03-29 17:22:23 +00001317 }
1318 break;
bellard31e31b82003-02-18 22:55:36 +00001319 default:
1320 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00001321 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00001322 break;
1323 }
1324 return ret;
1325}
j_mayer32407102007-09-26 23:01:49 +00001326#endif
bellard31e31b82003-02-18 22:55:36 +00001327
j_mayer32407102007-09-26 23:01:49 +00001328#ifdef TARGET_NR_ipc
bellard8853f862004-02-22 14:57:26 +00001329#define N_SHM_REGIONS 32
1330
1331static struct shm_region {
1332 uint32_t start;
1333 uint32_t size;
1334} shm_regions[N_SHM_REGIONS];
1335
ths3eb6b042007-06-03 14:26:27 +00001336struct target_ipc_perm
1337{
blueswir1992f48a2007-10-14 16:27:31 +00001338 abi_long __key;
1339 abi_ulong uid;
1340 abi_ulong gid;
1341 abi_ulong cuid;
1342 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00001343 unsigned short int mode;
1344 unsigned short int __pad1;
1345 unsigned short int __seq;
1346 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00001347 abi_ulong __unused1;
1348 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00001349};
1350
1351struct target_semid_ds
1352{
1353 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001354 abi_ulong sem_otime;
1355 abi_ulong __unused1;
1356 abi_ulong sem_ctime;
1357 abi_ulong __unused2;
1358 abi_ulong sem_nsems;
1359 abi_ulong __unused3;
1360 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00001361};
1362
1363static inline void target_to_host_ipc_perm(struct ipc_perm *host_ip,
blueswir1992f48a2007-10-14 16:27:31 +00001364 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001365{
1366 struct target_ipc_perm *target_ip;
1367 struct target_semid_ds *target_sd;
1368
1369 lock_user_struct(target_sd, target_addr, 1);
1370 target_ip=&(target_sd->sem_perm);
1371 host_ip->__key = tswapl(target_ip->__key);
1372 host_ip->uid = tswapl(target_ip->uid);
1373 host_ip->gid = tswapl(target_ip->gid);
1374 host_ip->cuid = tswapl(target_ip->cuid);
1375 host_ip->cgid = tswapl(target_ip->cgid);
1376 host_ip->mode = tswapl(target_ip->mode);
1377 unlock_user_struct(target_sd, target_addr, 0);
1378}
1379
blueswir1992f48a2007-10-14 16:27:31 +00001380static inline void host_to_target_ipc_perm(abi_ulong target_addr,
ths3eb6b042007-06-03 14:26:27 +00001381 struct ipc_perm *host_ip)
1382{
1383 struct target_ipc_perm *target_ip;
1384 struct target_semid_ds *target_sd;
1385
1386 lock_user_struct(target_sd, target_addr, 0);
1387 target_ip = &(target_sd->sem_perm);
1388 target_ip->__key = tswapl(host_ip->__key);
1389 target_ip->uid = tswapl(host_ip->uid);
1390 target_ip->gid = tswapl(host_ip->gid);
1391 target_ip->cuid = tswapl(host_ip->cuid);
1392 target_ip->cgid = tswapl(host_ip->cgid);
1393 target_ip->mode = tswapl(host_ip->mode);
1394 unlock_user_struct(target_sd, target_addr, 1);
1395}
1396
1397static inline void target_to_host_semid_ds(struct semid_ds *host_sd,
blueswir1992f48a2007-10-14 16:27:31 +00001398 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001399{
1400 struct target_semid_ds *target_sd;
1401
1402 lock_user_struct(target_sd, target_addr, 1);
1403 target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1404 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1405 host_sd->sem_otime = tswapl(target_sd->sem_otime);
1406 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1407 unlock_user_struct(target_sd, target_addr, 0);
1408}
1409
blueswir1992f48a2007-10-14 16:27:31 +00001410static inline void host_to_target_semid_ds(abi_ulong target_addr,
ths3eb6b042007-06-03 14:26:27 +00001411 struct semid_ds *host_sd)
1412{
1413 struct target_semid_ds *target_sd;
1414
1415 lock_user_struct(target_sd, target_addr, 0);
1416 host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1417 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1418 target_sd->sem_otime = tswapl(host_sd->sem_otime);
1419 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1420 unlock_user_struct(target_sd, target_addr, 1);
1421}
1422
thsfa294812007-02-02 22:05:00 +00001423union semun {
1424 int val;
ths3eb6b042007-06-03 14:26:27 +00001425 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00001426 unsigned short *array;
1427};
1428
ths3eb6b042007-06-03 14:26:27 +00001429union target_semun {
1430 int val;
blueswir1992f48a2007-10-14 16:27:31 +00001431 abi_long buf;
ths3eb6b042007-06-03 14:26:27 +00001432 unsigned short int *array;
1433};
1434
j_mayer32407102007-09-26 23:01:49 +00001435static inline void target_to_host_semun(int cmd,
ths3eb6b042007-06-03 14:26:27 +00001436 union semun *host_su,
blueswir1992f48a2007-10-14 16:27:31 +00001437 abi_ulong target_addr,
ths3eb6b042007-06-03 14:26:27 +00001438 struct semid_ds *ds)
1439{
1440 union target_semun *target_su;
1441
1442 switch( cmd ) {
1443 case IPC_STAT:
1444 case IPC_SET:
1445 lock_user_struct(target_su, target_addr, 1);
1446 target_to_host_semid_ds(ds,target_su->buf);
1447 host_su->buf = ds;
1448 unlock_user_struct(target_su, target_addr, 0);
1449 break;
1450 case GETVAL:
1451 case SETVAL:
1452 lock_user_struct(target_su, target_addr, 1);
1453 host_su->val = tswapl(target_su->val);
1454 unlock_user_struct(target_su, target_addr, 0);
1455 break;
1456 case GETALL:
1457 case SETALL:
1458 lock_user_struct(target_su, target_addr, 1);
1459 *host_su->array = tswap16(*target_su->array);
1460 unlock_user_struct(target_su, target_addr, 0);
1461 break;
1462 default:
1463 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1464 }
1465}
1466
j_mayer32407102007-09-26 23:01:49 +00001467static inline void host_to_target_semun(int cmd,
blueswir1992f48a2007-10-14 16:27:31 +00001468 abi_ulong target_addr,
ths3eb6b042007-06-03 14:26:27 +00001469 union semun *host_su,
1470 struct semid_ds *ds)
1471{
1472 union target_semun *target_su;
1473
1474 switch( cmd ) {
1475 case IPC_STAT:
1476 case IPC_SET:
1477 lock_user_struct(target_su, target_addr, 0);
1478 host_to_target_semid_ds(target_su->buf,ds);
1479 unlock_user_struct(target_su, target_addr, 1);
1480 break;
1481 case GETVAL:
1482 case SETVAL:
1483 lock_user_struct(target_su, target_addr, 0);
1484 target_su->val = tswapl(host_su->val);
1485 unlock_user_struct(target_su, target_addr, 1);
1486 break;
1487 case GETALL:
1488 case SETALL:
1489 lock_user_struct(target_su, target_addr, 0);
1490 *target_su->array = tswap16(*host_su->array);
1491 unlock_user_struct(target_su, target_addr, 1);
1492 break;
1493 default:
1494 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1495 }
1496}
1497
blueswir1992f48a2007-10-14 16:27:31 +00001498static inline abi_long do_semctl(int first, int second, int third,
1499 abi_long ptr)
ths3eb6b042007-06-03 14:26:27 +00001500{
1501 union semun arg;
1502 struct semid_ds dsarg;
1503 int cmd = third&0xff;
blueswir1992f48a2007-10-14 16:27:31 +00001504 abi_long ret = 0;
ths3eb6b042007-06-03 14:26:27 +00001505
1506 switch( cmd ) {
1507 case GETVAL:
1508 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1509 ret = get_errno(semctl(first, second, cmd, arg));
1510 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1511 break;
1512 case SETVAL:
1513 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1514 ret = get_errno(semctl(first, second, cmd, arg));
1515 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1516 break;
1517 case GETALL:
1518 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1519 ret = get_errno(semctl(first, second, cmd, arg));
1520 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1521 break;
1522 case SETALL:
1523 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1524 ret = get_errno(semctl(first, second, cmd, arg));
1525 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1526 break;
1527 case IPC_STAT:
1528 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1529 ret = get_errno(semctl(first, second, cmd, arg));
1530 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1531 break;
1532 case IPC_SET:
1533 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1534 ret = get_errno(semctl(first, second, cmd, arg));
1535 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1536 break;
1537 default:
1538 ret = get_errno(semctl(first, second, cmd, arg));
1539 }
1540
1541 return ret;
1542}
1543
ths1bc012f2007-06-03 14:27:49 +00001544struct target_msqid_ds
1545{
1546 struct target_ipc_perm msg_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001547 abi_ulong msg_stime;
1548 abi_ulong __unused1;
1549 abi_ulong msg_rtime;
1550 abi_ulong __unused2;
1551 abi_ulong msg_ctime;
1552 abi_ulong __unused3;
1553 abi_ulong __msg_cbytes;
1554 abi_ulong msg_qnum;
1555 abi_ulong msg_qbytes;
1556 abi_ulong msg_lspid;
1557 abi_ulong msg_lrpid;
1558 abi_ulong __unused4;
1559 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00001560};
1561
1562static inline void target_to_host_msqid_ds(struct msqid_ds *host_md,
blueswir1992f48a2007-10-14 16:27:31 +00001563 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00001564{
1565 struct target_msqid_ds *target_md;
1566
1567 lock_user_struct(target_md, target_addr, 1);
1568 target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1569 host_md->msg_stime = tswapl(target_md->msg_stime);
1570 host_md->msg_rtime = tswapl(target_md->msg_rtime);
1571 host_md->msg_ctime = tswapl(target_md->msg_ctime);
1572 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1573 host_md->msg_qnum = tswapl(target_md->msg_qnum);
1574 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1575 host_md->msg_lspid = tswapl(target_md->msg_lspid);
1576 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1577 unlock_user_struct(target_md, target_addr, 0);
1578}
1579
blueswir1992f48a2007-10-14 16:27:31 +00001580static inline void host_to_target_msqid_ds(abi_ulong target_addr,
ths1bc012f2007-06-03 14:27:49 +00001581 struct msqid_ds *host_md)
1582{
1583 struct target_msqid_ds *target_md;
1584
1585 lock_user_struct(target_md, target_addr, 0);
1586 host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1587 target_md->msg_stime = tswapl(host_md->msg_stime);
1588 target_md->msg_rtime = tswapl(host_md->msg_rtime);
1589 target_md->msg_ctime = tswapl(host_md->msg_ctime);
1590 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1591 target_md->msg_qnum = tswapl(host_md->msg_qnum);
1592 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1593 target_md->msg_lspid = tswapl(host_md->msg_lspid);
1594 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1595 unlock_user_struct(target_md, target_addr, 1);
1596}
1597
blueswir1992f48a2007-10-14 16:27:31 +00001598static inline abi_long do_msgctl(int first, int second, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00001599{
1600 struct msqid_ds dsarg;
1601 int cmd = second&0xff;
blueswir1992f48a2007-10-14 16:27:31 +00001602 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001603 switch( cmd ) {
1604 case IPC_STAT:
1605 case IPC_SET:
1606 target_to_host_msqid_ds(&dsarg,ptr);
1607 ret = get_errno(msgctl(first, cmd, &dsarg));
1608 host_to_target_msqid_ds(ptr,&dsarg);
1609 default:
1610 ret = get_errno(msgctl(first, cmd, &dsarg));
1611 }
1612 return ret;
1613}
1614
1615struct target_msgbuf {
blueswir1992f48a2007-10-14 16:27:31 +00001616 abi_ulong mtype;
ths1bc012f2007-06-03 14:27:49 +00001617 char mtext[1];
1618};
1619
blueswir1992f48a2007-10-14 16:27:31 +00001620static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1621 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001622{
1623 struct target_msgbuf *target_mb;
1624 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00001625 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001626
1627 lock_user_struct(target_mb,msgp,0);
1628 host_mb = malloc(msgsz+sizeof(long));
1629 host_mb->mtype = tswapl(target_mb->mtype);
1630 memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1631 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1632 free(host_mb);
1633 unlock_user_struct(target_mb, msgp, 0);
1634
1635 return ret;
1636}
1637
blueswir1992f48a2007-10-14 16:27:31 +00001638static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1639 unsigned int msgsz, int msgtype,
1640 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001641{
1642 struct target_msgbuf *target_mb;
1643 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00001644 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001645
1646 lock_user_struct(target_mb, msgp, 0);
1647 host_mb = malloc(msgsz+sizeof(long));
1648 ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
1649 if (ret > 0)
1650 memcpy(target_mb->mtext, host_mb->mtext, ret);
1651 target_mb->mtype = tswapl(host_mb->mtype);
1652 free(host_mb);
1653 unlock_user_struct(target_mb, msgp, 0);
1654
1655 return ret;
1656}
1657
pbrook53a59602006-03-25 19:31:22 +00001658/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00001659/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001660static abi_long do_ipc(unsigned int call, int first,
1661 int second, int third,
1662 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00001663{
1664 int version;
blueswir1992f48a2007-10-14 16:27:31 +00001665 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00001666 unsigned long raddr;
1667 struct shmid_ds shm_info;
1668 int i;
1669
1670 version = call >> 16;
1671 call &= 0xffff;
1672
1673 switch (call) {
thsfa294812007-02-02 22:05:00 +00001674 case IPCOP_semop:
1675 ret = get_errno(semop(first,(struct sembuf *) ptr, second));
1676 break;
1677
1678 case IPCOP_semget:
1679 ret = get_errno(semget(first, second, third));
1680 break;
1681
1682 case IPCOP_semctl:
ths3eb6b042007-06-03 14:26:27 +00001683 ret = do_semctl(first, second, third, ptr);
thsfa294812007-02-02 22:05:00 +00001684 break;
1685
1686 case IPCOP_semtimedop:
j_mayer32407102007-09-26 23:01:49 +00001687 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00001688 ret = -TARGET_ENOSYS;
thsfa294812007-02-02 22:05:00 +00001689 break;
thsd96372e2007-02-02 22:05:44 +00001690
1691 case IPCOP_msgget:
1692 ret = get_errno(msgget(first, second));
1693 break;
1694
1695 case IPCOP_msgsnd:
ths1bc012f2007-06-03 14:27:49 +00001696 ret = do_msgsnd(first, ptr, second, third);
thsd96372e2007-02-02 22:05:44 +00001697 break;
1698
1699 case IPCOP_msgctl:
ths1bc012f2007-06-03 14:27:49 +00001700 ret = do_msgctl(first, second, ptr);
thsd96372e2007-02-02 22:05:44 +00001701 break;
1702
1703 case IPCOP_msgrcv:
ths1bc012f2007-06-03 14:27:49 +00001704 {
1705 struct ipc_kludge
1706 {
1707 void *__unbounded msgp;
1708 long int msgtyp;
1709 };
thsd96372e2007-02-02 22:05:44 +00001710
ths1bc012f2007-06-03 14:27:49 +00001711 struct ipc_kludge *foo = (struct ipc_kludge *) ptr;
1712 struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
thsd96372e2007-02-02 22:05:44 +00001713
ths1bc012f2007-06-03 14:27:49 +00001714 ret = do_msgrcv(first, (long)msgp, second, 0, third);
thsd96372e2007-02-02 22:05:44 +00001715
ths1bc012f2007-06-03 14:27:49 +00001716 }
thsd96372e2007-02-02 22:05:44 +00001717 break;
1718
bellard8853f862004-02-22 14:57:26 +00001719 case IPCOP_shmat:
1720 /* SHM_* flags are the same on all linux platforms */
1721 ret = get_errno((long) shmat(first, (void *) ptr, second));
1722 if (is_error(ret))
1723 break;
1724 raddr = ret;
1725 /* find out the length of the shared memory segment */
ths3b46e622007-09-17 08:09:54 +00001726
bellard8853f862004-02-22 14:57:26 +00001727 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
1728 if (is_error(ret)) {
1729 /* can't get length, bail out */
1730 shmdt((void *) raddr);
1731 break;
1732 }
1733 page_set_flags(raddr, raddr + shm_info.shm_segsz,
1734 PAGE_VALID | PAGE_READ |
1735 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
1736 for (i = 0; i < N_SHM_REGIONS; ++i) {
1737 if (shm_regions[i].start == 0) {
1738 shm_regions[i].start = raddr;
1739 shm_regions[i].size = shm_info.shm_segsz;
1740 break;
1741 }
1742 }
blueswir1992f48a2007-10-14 16:27:31 +00001743 if (put_user(raddr, (abi_ulong *)third))
ths0da46a62007-10-20 20:23:07 +00001744 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001745 ret = 0;
1746 break;
1747 case IPCOP_shmdt:
1748 for (i = 0; i < N_SHM_REGIONS; ++i) {
1749 if (shm_regions[i].start == ptr) {
1750 shm_regions[i].start = 0;
1751 page_set_flags(ptr, shm_regions[i].size, 0);
1752 break;
1753 }
1754 }
1755 ret = get_errno(shmdt((void *) ptr));
1756 break;
1757
1758 case IPCOP_shmget:
1759 /* IPC_* flag values are the same on all linux platforms */
1760 ret = get_errno(shmget(first, second, third));
1761 break;
1762
1763 /* IPC_* and SHM_* command values are the same on all linux platforms */
1764 case IPCOP_shmctl:
1765 switch(second) {
1766 case IPC_RMID:
1767 case SHM_LOCK:
1768 case SHM_UNLOCK:
1769 ret = get_errno(shmctl(first, second, NULL));
1770 break;
1771 default:
1772 goto unimplemented;
1773 }
1774 break;
1775 default:
1776 unimplemented:
j_mayer32407102007-09-26 23:01:49 +00001777 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00001778 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00001779 break;
1780 }
1781 return ret;
1782}
j_mayer32407102007-09-26 23:01:49 +00001783#endif
bellard8853f862004-02-22 14:57:26 +00001784
bellard31e31b82003-02-18 22:55:36 +00001785/* kernel structure types definitions */
1786#define IFNAMSIZ 16
1787
1788#define STRUCT(name, list...) STRUCT_ ## name,
1789#define STRUCT_SPECIAL(name) STRUCT_ ## name,
1790enum {
1791#include "syscall_types.h"
1792};
1793#undef STRUCT
1794#undef STRUCT_SPECIAL
1795
1796#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
1797#define STRUCT_SPECIAL(name)
1798#include "syscall_types.h"
1799#undef STRUCT
1800#undef STRUCT_SPECIAL
1801
1802typedef struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00001803 unsigned int target_cmd;
1804 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00001805 const char *name;
1806 int access;
bellard1a9353d2003-03-16 20:28:50 +00001807 const argtype arg_type[5];
bellard31e31b82003-02-18 22:55:36 +00001808} IOCTLEntry;
1809
1810#define IOC_R 0x0001
1811#define IOC_W 0x0002
1812#define IOC_RW (IOC_R | IOC_W)
1813
1814#define MAX_STRUCT_SIZE 4096
1815
bellard2ab83ea2003-06-15 19:56:46 +00001816IOCTLEntry ioctl_entries[] = {
bellard31e31b82003-02-18 22:55:36 +00001817#define IOCTL(cmd, access, types...) \
1818 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
1819#include "ioctls.h"
1820 { 0, 0, },
1821};
1822
pbrook53a59602006-03-25 19:31:22 +00001823/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00001824/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001825static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00001826{
1827 const IOCTLEntry *ie;
1828 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00001829 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00001830 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00001831 int target_size;
1832 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00001833
1834 ie = ioctl_entries;
1835 for(;;) {
1836 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00001837 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00001838 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00001839 }
1840 if (ie->target_cmd == cmd)
1841 break;
1842 ie++;
1843 }
1844 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00001845#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00001846 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00001847#endif
bellard31e31b82003-02-18 22:55:36 +00001848 switch(arg_type[0]) {
1849 case TYPE_NULL:
1850 /* no argument */
1851 ret = get_errno(ioctl(fd, ie->host_cmd));
1852 break;
1853 case TYPE_PTRVOID:
1854 case TYPE_INT:
1855 /* int argment */
1856 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
1857 break;
1858 case TYPE_PTR:
1859 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00001860 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00001861 switch(ie->access) {
1862 case IOC_R:
1863 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1864 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00001865 argptr = lock_user(arg, target_size, 0);
1866 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1867 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00001868 }
1869 break;
1870 case IOC_W:
pbrook53a59602006-03-25 19:31:22 +00001871 argptr = lock_user(arg, target_size, 1);
1872 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1873 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00001874 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1875 break;
1876 default:
1877 case IOC_RW:
pbrook53a59602006-03-25 19:31:22 +00001878 argptr = lock_user(arg, target_size, 1);
1879 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1880 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00001881 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1882 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00001883 argptr = lock_user(arg, target_size, 0);
1884 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1885 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00001886 }
1887 break;
1888 }
1889 break;
1890 default:
j_mayer32407102007-09-26 23:01:49 +00001891 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
1892 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00001893 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00001894 break;
1895 }
1896 return ret;
1897}
1898
1899bitmask_transtbl iflag_tbl[] = {
1900 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
1901 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
1902 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
1903 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
1904 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
1905 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
1906 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
1907 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
1908 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
1909 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
1910 { TARGET_IXON, TARGET_IXON, IXON, IXON },
1911 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
1912 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
1913 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
1914 { 0, 0, 0, 0 }
1915};
1916
1917bitmask_transtbl oflag_tbl[] = {
1918 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
1919 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
1920 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
1921 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
1922 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
1923 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
1924 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
1925 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
1926 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
1927 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
1928 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
1929 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
1930 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
1931 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
1932 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
1933 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
1934 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
1935 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
1936 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
1937 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
1938 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
1939 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
1940 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
1941 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
1942 { 0, 0, 0, 0 }
1943};
1944
1945bitmask_transtbl cflag_tbl[] = {
1946 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
1947 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
1948 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
1949 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
1950 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
1951 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
1952 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
1953 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
1954 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
1955 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
1956 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
1957 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
1958 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
1959 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
1960 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
1961 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
1962 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
1963 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
1964 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
1965 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
1966 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
1967 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
1968 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
1969 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
1970 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
1971 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
1972 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
1973 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
1974 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
1975 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
1976 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
1977 { 0, 0, 0, 0 }
1978};
1979
1980bitmask_transtbl lflag_tbl[] = {
1981 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
1982 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
1983 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
1984 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
1985 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
1986 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
1987 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
1988 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
1989 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
1990 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
1991 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
1992 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
1993 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
1994 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
1995 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
1996 { 0, 0, 0, 0 }
1997};
1998
1999static void target_to_host_termios (void *dst, const void *src)
2000{
2001 struct host_termios *host = dst;
2002 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00002003
ths5fafdf22007-09-16 21:08:06 +00002004 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002005 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002006 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002007 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002008 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002009 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002010 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002011 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2012 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00002013
ths5fafdf22007-09-16 21:08:06 +00002014 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2015 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00002016 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00002017 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00002018 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00002019 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00002020 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00002021 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00002022 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00002023 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2024 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00002025 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2026 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2027 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2028 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2029 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00002030 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00002031}
ths3b46e622007-09-17 08:09:54 +00002032
bellard31e31b82003-02-18 22:55:36 +00002033static void host_to_target_termios (void *dst, const void *src)
2034{
2035 struct target_termios *target = dst;
2036 const struct host_termios *host = src;
2037
ths5fafdf22007-09-16 21:08:06 +00002038 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002039 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002040 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002041 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002042 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002043 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002044 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002045 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2046 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00002047
bellard31e31b82003-02-18 22:55:36 +00002048 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2049 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2050 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2051 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2052 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2053 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2054 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2055 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2056 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2057 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2058 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2059 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2060 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2061 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2062 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2063 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2064 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2065}
2066
2067StructEntry struct_termios_def = {
2068 .convert = { host_to_target_termios, target_to_host_termios },
2069 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2070 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2071};
2072
bellard5286db72003-06-05 00:57:30 +00002073static bitmask_transtbl mmap_flags_tbl[] = {
2074 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2075 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2076 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2077 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2078 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2079 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2080 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2081 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2082 { 0, 0, 0, 0 }
2083};
2084
bellardffa65c32004-01-04 23:57:22 +00002085static bitmask_transtbl fcntl_flags_tbl[] = {
2086 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
2087 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
2088 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
2089 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
2090 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
2091 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
2092 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
2093 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
2094 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
2095 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
2096 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2097 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
2098 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
bellard121061d2004-02-22 11:56:01 +00002099#if defined(O_DIRECT)
bellardffa65c32004-01-04 23:57:22 +00002100 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
bellard121061d2004-02-22 11:56:01 +00002101#endif
bellardffa65c32004-01-04 23:57:22 +00002102 { 0, 0, 0, 0 }
2103};
2104
bellard2ab83ea2003-06-15 19:56:46 +00002105#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00002106
2107/* NOTE: there is really one LDT for all the threads */
2108uint8_t *ldt_table;
2109
blueswir1992f48a2007-10-14 16:27:31 +00002110static int read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002111{
2112 int size;
pbrook53a59602006-03-25 19:31:22 +00002113 void *p;
bellard6dbad632003-03-16 18:05:05 +00002114
2115 if (!ldt_table)
2116 return 0;
2117 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2118 if (size > bytecount)
2119 size = bytecount;
pbrook53a59602006-03-25 19:31:22 +00002120 p = lock_user(ptr, size, 0);
2121 /* ??? Shoudl this by byteswapped? */
2122 memcpy(p, ldt_table, size);
2123 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00002124 return size;
2125}
2126
2127/* XXX: add locking support */
ths0da46a62007-10-20 20:23:07 +00002128/* write_ldt() returns host errnos */
ths5fafdf22007-09-16 21:08:06 +00002129static int write_ldt(CPUX86State *env,
blueswir1992f48a2007-10-14 16:27:31 +00002130 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00002131{
2132 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00002133 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00002134 int seg_32bit, contents, read_exec_only, limit_in_pages;
2135 int seg_not_present, useable;
2136 uint32_t *lp, entry_1, entry_2;
2137
2138 if (bytecount != sizeof(ldt_info))
2139 return -EINVAL;
pbrook53a59602006-03-25 19:31:22 +00002140 lock_user_struct(target_ldt_info, ptr, 1);
2141 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2142 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2143 ldt_info.limit = tswap32(target_ldt_info->limit);
2144 ldt_info.flags = tswap32(target_ldt_info->flags);
2145 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00002146
bellard6dbad632003-03-16 18:05:05 +00002147 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
2148 return -EINVAL;
2149 seg_32bit = ldt_info.flags & 1;
2150 contents = (ldt_info.flags >> 1) & 3;
2151 read_exec_only = (ldt_info.flags >> 3) & 1;
2152 limit_in_pages = (ldt_info.flags >> 4) & 1;
2153 seg_not_present = (ldt_info.flags >> 5) & 1;
2154 useable = (ldt_info.flags >> 6) & 1;
2155
2156 if (contents == 3) {
2157 if (oldmode)
2158 return -EINVAL;
2159 if (seg_not_present == 0)
2160 return -EINVAL;
2161 }
2162 /* allocate the LDT */
2163 if (!ldt_table) {
2164 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2165 if (!ldt_table)
2166 return -ENOMEM;
2167 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
pbrook53a59602006-03-25 19:31:22 +00002168 env->ldt.base = h2g(ldt_table);
bellard6dbad632003-03-16 18:05:05 +00002169 env->ldt.limit = 0xffff;
2170 }
2171
2172 /* NOTE: same code as Linux kernel */
2173 /* Allow LDTs to be cleared by the user. */
2174 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2175 if (oldmode ||
2176 (contents == 0 &&
2177 read_exec_only == 1 &&
2178 seg_32bit == 0 &&
2179 limit_in_pages == 0 &&
2180 seg_not_present == 1 &&
2181 useable == 0 )) {
2182 entry_1 = 0;
2183 entry_2 = 0;
2184 goto install;
2185 }
2186 }
ths3b46e622007-09-17 08:09:54 +00002187
bellard6dbad632003-03-16 18:05:05 +00002188 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2189 (ldt_info.limit & 0x0ffff);
2190 entry_2 = (ldt_info.base_addr & 0xff000000) |
2191 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2192 (ldt_info.limit & 0xf0000) |
2193 ((read_exec_only ^ 1) << 9) |
2194 (contents << 10) |
2195 ((seg_not_present ^ 1) << 15) |
2196 (seg_32bit << 22) |
2197 (limit_in_pages << 23) |
2198 0x7000;
2199 if (!oldmode)
2200 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00002201
bellard6dbad632003-03-16 18:05:05 +00002202 /* Install the new entry ... */
2203install:
2204 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2205 lp[0] = tswap32(entry_1);
2206 lp[1] = tswap32(entry_2);
2207 return 0;
2208}
2209
2210/* specific and weird i386 syscalls */
ths0da46a62007-10-20 20:23:07 +00002211/* do_modify_ldt() returns host errnos (it is inconsistent with the
2212 other do_*() functions which return target errnos). */
blueswir1992f48a2007-10-14 16:27:31 +00002213int do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002214{
2215 int ret = -ENOSYS;
ths3b46e622007-09-17 08:09:54 +00002216
bellard6dbad632003-03-16 18:05:05 +00002217 switch (func) {
2218 case 0:
2219 ret = read_ldt(ptr, bytecount);
2220 break;
2221 case 1:
2222 ret = write_ldt(env, ptr, bytecount, 1);
2223 break;
2224 case 0x11:
2225 ret = write_ldt(env, ptr, bytecount, 0);
2226 break;
2227 }
2228 return ret;
2229}
bellard1b6b0292003-03-22 17:31:38 +00002230
bellard2ab83ea2003-06-15 19:56:46 +00002231#endif /* defined(TARGET_I386) */
2232
bellard1b6b0292003-03-22 17:31:38 +00002233/* this stack is the equivalent of the kernel stack associated with a
2234 thread/process */
2235#define NEW_STACK_SIZE 8192
2236
2237static int clone_func(void *arg)
2238{
bellard2ab83ea2003-06-15 19:56:46 +00002239 CPUState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00002240 cpu_loop(env);
2241 /* never exits */
2242 return 0;
2243}
2244
ths0da46a62007-10-20 20:23:07 +00002245/* do_fork() Must return host values and target errnos (unlike most
2246 do_*() functions). */
blueswir1992f48a2007-10-14 16:27:31 +00002247int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp)
bellard1b6b0292003-03-22 17:31:38 +00002248{
2249 int ret;
bellard5cd43932003-03-29 16:54:36 +00002250 TaskState *ts;
bellard1b6b0292003-03-22 17:31:38 +00002251 uint8_t *new_stack;
bellard2ab83ea2003-06-15 19:56:46 +00002252 CPUState *new_env;
ths3b46e622007-09-17 08:09:54 +00002253
bellard1b6b0292003-03-22 17:31:38 +00002254 if (flags & CLONE_VM) {
bellard5cd43932003-03-29 16:54:36 +00002255 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2256 memset(ts, 0, sizeof(TaskState));
2257 new_stack = ts->stack;
2258 ts->used = 1;
2259 /* add in task state list */
2260 ts->next = first_task_state;
2261 first_task_state = ts;
bellard1b6b0292003-03-22 17:31:38 +00002262 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00002263 new_env = cpu_copy(env);
bellard2ab83ea2003-06-15 19:56:46 +00002264#if defined(TARGET_I386)
2265 if (!newsp)
2266 newsp = env->regs[R_ESP];
bellard1b6b0292003-03-22 17:31:38 +00002267 new_env->regs[R_ESP] = newsp;
2268 new_env->regs[R_EAX] = 0;
bellard2ab83ea2003-06-15 19:56:46 +00002269#elif defined(TARGET_ARM)
2270 if (!newsp)
2271 newsp = env->regs[13];
2272 new_env->regs[13] = newsp;
2273 new_env->regs[0] = 0;
bellardebc05482003-09-30 21:08:41 +00002274#elif defined(TARGET_SPARC)
bellard48dc41e2006-06-21 18:15:50 +00002275 if (!newsp)
2276 newsp = env->regwptr[22];
2277 new_env->regwptr[22] = newsp;
2278 new_env->regwptr[0] = 0;
2279 /* XXXXX */
bellard67867302003-11-23 17:05:30 +00002280 printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
pbrooke6e59062006-10-22 00:18:54 +00002281#elif defined(TARGET_M68K)
2282 if (!newsp)
2283 newsp = env->aregs[7];
2284 new_env->aregs[7] = newsp;
2285 new_env->dregs[0] = 0;
2286 /* ??? is this sufficient? */
bellard048f6b42005-11-26 18:47:20 +00002287#elif defined(TARGET_MIPS)
ths388bb212007-05-13 13:58:00 +00002288 if (!newsp)
thsead93602007-09-06 00:18:15 +00002289 newsp = env->gpr[29][env->current_tc];
2290 new_env->gpr[29][env->current_tc] = newsp;
bellard67867302003-11-23 17:05:30 +00002291#elif defined(TARGET_PPC)
2292 if (!newsp)
2293 newsp = env->gpr[1];
2294 new_env->gpr[1] = newsp;
ths5fafdf22007-09-16 21:08:06 +00002295 {
bellard67867302003-11-23 17:05:30 +00002296 int i;
2297 for (i = 7; i < 32; i++)
2298 new_env->gpr[i] = 0;
2299 }
bellardfdf9b3e2006-04-27 21:07:38 +00002300#elif defined(TARGET_SH4)
2301 if (!newsp)
2302 newsp = env->gregs[15];
2303 new_env->gregs[15] = newsp;
2304 /* XXXXX */
j_mayer7a3148a2007-04-05 07:13:51 +00002305#elif defined(TARGET_ALPHA)
2306 if (!newsp)
2307 newsp = env->ir[30];
2308 new_env->ir[30] = newsp;
2309 /* ? */
2310 {
2311 int i;
2312 for (i = 7; i < 30; i++)
2313 new_env->ir[i] = 0;
2314 }
ths48733d12007-10-08 13:36:46 +00002315#elif defined(TARGET_CRIS)
2316 if (!newsp)
2317 newsp = env->regs[14];
2318 new_env->regs[14] = newsp;
bellard2ab83ea2003-06-15 19:56:46 +00002319#else
2320#error unsupported target CPU
2321#endif
bellard5cd43932003-03-29 16:54:36 +00002322 new_env->opaque = ts;
bellard27725c12003-04-29 21:08:18 +00002323#ifdef __ia64__
bellardfd4a43e2006-04-24 20:32:17 +00002324 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00002325#else
2326 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2327#endif
bellard1b6b0292003-03-22 17:31:38 +00002328 } else {
2329 /* if no CLONE_VM, we consider it is a fork */
2330 if ((flags & ~CSIGNAL) != 0)
2331 return -EINVAL;
2332 ret = fork();
2333 }
2334 return ret;
2335}
2336
blueswir1992f48a2007-10-14 16:27:31 +00002337static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00002338{
2339 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00002340 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00002341 struct flock64 fl64;
2342 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00002343 abi_long ret;
pbrook53a59602006-03-25 19:31:22 +00002344
bellard7775e9e2003-05-14 22:46:48 +00002345 switch(cmd) {
2346 case TARGET_F_GETLK:
ths58134272007-03-31 18:59:32 +00002347 lock_user_struct(target_fl, arg, 1);
2348 fl.l_type = tswap16(target_fl->l_type);
2349 fl.l_whence = tswap16(target_fl->l_whence);
2350 fl.l_start = tswapl(target_fl->l_start);
2351 fl.l_len = tswapl(target_fl->l_len);
2352 fl.l_pid = tswapl(target_fl->l_pid);
2353 unlock_user_struct(target_fl, arg, 0);
bellard7775e9e2003-05-14 22:46:48 +00002354 ret = fcntl(fd, cmd, &fl);
2355 if (ret == 0) {
pbrook53a59602006-03-25 19:31:22 +00002356 lock_user_struct(target_fl, arg, 0);
bellard7775e9e2003-05-14 22:46:48 +00002357 target_fl->l_type = tswap16(fl.l_type);
2358 target_fl->l_whence = tswap16(fl.l_whence);
2359 target_fl->l_start = tswapl(fl.l_start);
2360 target_fl->l_len = tswapl(fl.l_len);
2361 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00002362 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00002363 }
2364 break;
ths3b46e622007-09-17 08:09:54 +00002365
bellard7775e9e2003-05-14 22:46:48 +00002366 case TARGET_F_SETLK:
2367 case TARGET_F_SETLKW:
pbrook53a59602006-03-25 19:31:22 +00002368 lock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00002369 fl.l_type = tswap16(target_fl->l_type);
2370 fl.l_whence = tswap16(target_fl->l_whence);
2371 fl.l_start = tswapl(target_fl->l_start);
2372 fl.l_len = tswapl(target_fl->l_len);
2373 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00002374 unlock_user_struct(target_fl, arg, 0);
bellard7775e9e2003-05-14 22:46:48 +00002375 ret = fcntl(fd, cmd, &fl);
2376 break;
ths3b46e622007-09-17 08:09:54 +00002377
bellard7775e9e2003-05-14 22:46:48 +00002378 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00002379 lock_user_struct(target_fl64, arg, 1);
2380 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2381 fl64.l_whence = tswap16(target_fl64->l_whence);
2382 fl64.l_start = tswapl(target_fl64->l_start);
2383 fl64.l_len = tswapl(target_fl64->l_len);
2384 fl64.l_pid = tswap16(target_fl64->l_pid);
2385 unlock_user_struct(target_fl64, arg, 0);
ths43f238d2007-01-05 20:55:49 +00002386 ret = fcntl(fd, cmd >> 1, &fl64);
2387 if (ret == 0) {
2388 lock_user_struct(target_fl64, arg, 0);
2389 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2390 target_fl64->l_whence = tswap16(fl64.l_whence);
2391 target_fl64->l_start = tswapl(fl64.l_start);
2392 target_fl64->l_len = tswapl(fl64.l_len);
2393 target_fl64->l_pid = tswapl(fl64.l_pid);
2394 unlock_user_struct(target_fl64, arg, 1);
2395 }
2396 break;
bellard7775e9e2003-05-14 22:46:48 +00002397 case TARGET_F_SETLK64:
2398 case TARGET_F_SETLKW64:
ths43f238d2007-01-05 20:55:49 +00002399 lock_user_struct(target_fl64, arg, 1);
2400 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2401 fl64.l_whence = tswap16(target_fl64->l_whence);
2402 fl64.l_start = tswapl(target_fl64->l_start);
2403 fl64.l_len = tswapl(target_fl64->l_len);
2404 fl64.l_pid = tswap16(target_fl64->l_pid);
2405 unlock_user_struct(target_fl64, arg, 0);
2406 ret = fcntl(fd, cmd >> 1, &fl64);
bellard7775e9e2003-05-14 22:46:48 +00002407 break;
2408
bellardffa65c32004-01-04 23:57:22 +00002409 case F_GETFL:
2410 ret = fcntl(fd, cmd, arg);
2411 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2412 break;
2413
2414 case F_SETFL:
2415 ret = fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl));
2416 break;
2417
bellard7775e9e2003-05-14 22:46:48 +00002418 default:
2419 ret = fcntl(fd, cmd, arg);
2420 break;
2421 }
2422 return ret;
2423}
2424
bellard67867302003-11-23 17:05:30 +00002425#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00002426
bellard67867302003-11-23 17:05:30 +00002427static inline int high2lowuid(int uid)
2428{
2429 if (uid > 65535)
2430 return 65534;
2431 else
2432 return uid;
2433}
2434
2435static inline int high2lowgid(int gid)
2436{
2437 if (gid > 65535)
2438 return 65534;
2439 else
2440 return gid;
2441}
2442
2443static inline int low2highuid(int uid)
2444{
2445 if ((int16_t)uid == -1)
2446 return -1;
2447 else
2448 return uid;
2449}
2450
2451static inline int low2highgid(int gid)
2452{
2453 if ((int16_t)gid == -1)
2454 return -1;
2455 else
2456 return gid;
2457}
2458
2459#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00002460
bellard31e31b82003-02-18 22:55:36 +00002461void syscall_init(void)
2462{
bellard2ab83ea2003-06-15 19:56:46 +00002463 IOCTLEntry *ie;
2464 const argtype *arg_type;
2465 int size;
2466
ths5fafdf22007-09-16 21:08:06 +00002467#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2468#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00002469#include "syscall_types.h"
2470#undef STRUCT
2471#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00002472
2473 /* we patch the ioctl size if necessary. We rely on the fact that
2474 no ioctl has all the bits at '1' in the size field */
2475 ie = ioctl_entries;
2476 while (ie->target_cmd != 0) {
2477 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2478 TARGET_IOC_SIZEMASK) {
2479 arg_type = ie->arg_type;
2480 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00002481 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002482 ie->target_cmd);
2483 exit(1);
2484 }
2485 arg_type++;
2486 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00002487 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00002488 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2489 (size << TARGET_IOC_SIZESHIFT);
2490 }
2491 /* automatic consistency check if same arch */
2492#if defined(__i386__) && defined(TARGET_I386)
2493 if (ie->target_cmd != ie->host_cmd) {
ths5fafdf22007-09-16 21:08:06 +00002494 fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002495 ie->target_cmd, ie->host_cmd);
2496 }
2497#endif
2498 ie++;
2499 }
bellard31e31b82003-02-18 22:55:36 +00002500}
bellardc573ff62004-01-04 15:51:36 +00002501
blueswir1992f48a2007-10-14 16:27:31 +00002502#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00002503static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2504{
2505#ifdef TARGET_WORDS_BIG_ENDIAN
2506 return ((uint64_t)word0 << 32) | word1;
2507#else
2508 return ((uint64_t)word1 << 32) | word0;
2509#endif
2510}
blueswir1992f48a2007-10-14 16:27:31 +00002511#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00002512static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2513{
2514 return word0;
2515}
blueswir1992f48a2007-10-14 16:27:31 +00002516#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00002517
2518#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00002519static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
2520 abi_long arg2,
2521 abi_long arg3,
2522 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00002523{
2524#ifdef TARGET_ARM
2525 if (((CPUARMState *)cpu_env)->eabi)
2526 {
2527 arg2 = arg3;
2528 arg3 = arg4;
2529 }
2530#endif
2531 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
2532}
2533#endif
2534
2535#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00002536static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
2537 abi_long arg2,
2538 abi_long arg3,
2539 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00002540{
2541#ifdef TARGET_ARM
2542 if (((CPUARMState *)cpu_env)->eabi)
2543 {
2544 arg2 = arg3;
2545 arg3 = arg4;
2546 }
2547#endif
2548 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
2549}
2550#endif
2551
pbrook53a59602006-03-25 19:31:22 +00002552static inline void target_to_host_timespec(struct timespec *host_ts,
blueswir1992f48a2007-10-14 16:27:31 +00002553 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00002554{
2555 struct target_timespec *target_ts;
2556
2557 lock_user_struct(target_ts, target_addr, 1);
2558 host_ts->tv_sec = tswapl(target_ts->tv_sec);
2559 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
2560 unlock_user_struct(target_ts, target_addr, 0);
2561}
2562
blueswir1992f48a2007-10-14 16:27:31 +00002563static inline void host_to_target_timespec(abi_ulong target_addr,
pbrook53a59602006-03-25 19:31:22 +00002564 struct timespec *host_ts)
2565{
2566 struct target_timespec *target_ts;
2567
2568 lock_user_struct(target_ts, target_addr, 0);
2569 target_ts->tv_sec = tswapl(host_ts->tv_sec);
2570 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
2571 unlock_user_struct(target_ts, target_addr, 1);
2572}
2573
ths0da46a62007-10-20 20:23:07 +00002574/* do_syscall() should always have a single exit point at the end so
2575 that actions, such as logging of syscall results, can be performed.
2576 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00002577abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
2578 abi_long arg2, abi_long arg3, abi_long arg4,
2579 abi_long arg5, abi_long arg6)
bellard31e31b82003-02-18 22:55:36 +00002580{
blueswir1992f48a2007-10-14 16:27:31 +00002581 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00002582 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00002583 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00002584 void *p;
ths3b46e622007-09-17 08:09:54 +00002585
bellard72f03902003-02-18 23:33:18 +00002586#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00002587 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00002588#endif
bellard31e31b82003-02-18 22:55:36 +00002589 switch(num) {
2590 case TARGET_NR_exit:
bellard7d132992003-03-06 23:23:54 +00002591#ifdef HAVE_GPROF
2592 _mcleanup();
2593#endif
bellarde9009672005-04-26 20:42:36 +00002594 gdb_exit(cpu_env, arg1);
bellard1b6b0292003-03-22 17:31:38 +00002595 /* XXX: should free thread stack and CPU env */
bellard31e31b82003-02-18 22:55:36 +00002596 _exit(arg1);
2597 ret = 0; /* avoid warning */
2598 break;
2599 case TARGET_NR_read:
pbrook53a59602006-03-25 19:31:22 +00002600 page_unprotect_range(arg2, arg3);
2601 p = lock_user(arg2, arg3, 0);
2602 ret = get_errno(read(arg1, p, arg3));
2603 unlock_user(p, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00002604 break;
2605 case TARGET_NR_write:
pbrook53a59602006-03-25 19:31:22 +00002606 p = lock_user(arg2, arg3, 1);
2607 ret = get_errno(write(arg1, p, arg3));
2608 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00002609 break;
2610 case TARGET_NR_open:
pbrook53a59602006-03-25 19:31:22 +00002611 p = lock_user_string(arg1);
2612 ret = get_errno(open(path(p),
bellardffa65c32004-01-04 23:57:22 +00002613 target_to_host_bitmask(arg2, fcntl_flags_tbl),
2614 arg3));
pbrook53a59602006-03-25 19:31:22 +00002615 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002616 break;
ths82424832007-09-24 09:21:55 +00002617#if defined(TARGET_NR_openat) && defined(__NR_openat)
2618 case TARGET_NR_openat:
2619 if (!arg2) {
ths0da46a62007-10-20 20:23:07 +00002620 ret = -TARGET_EFAULT;
ths82424832007-09-24 09:21:55 +00002621 goto fail;
2622 }
2623 p = lock_user_string(arg2);
2624 if (!access_ok(VERIFY_READ, p, 1))
ths0da46a62007-10-20 20:23:07 +00002625 /* Don't "goto fail" so that cleanup can happen. */
2626 ret = -TARGET_EFAULT;
ths82424832007-09-24 09:21:55 +00002627 else
2628 ret = get_errno(sys_openat(arg1,
2629 path(p),
2630 target_to_host_bitmask(arg3, fcntl_flags_tbl),
2631 arg4));
2632 if (p)
2633 unlock_user(p, arg2, 0);
2634 break;
2635#endif
bellard31e31b82003-02-18 22:55:36 +00002636 case TARGET_NR_close:
2637 ret = get_errno(close(arg1));
2638 break;
2639 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00002640 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00002641 break;
2642 case TARGET_NR_fork:
bellard1b6b0292003-03-22 17:31:38 +00002643 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00002644 break;
thse5febef2007-04-01 18:31:35 +00002645#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00002646 case TARGET_NR_waitpid:
2647 {
pbrook53a59602006-03-25 19:31:22 +00002648 int status;
2649 ret = get_errno(waitpid(arg1, &status, arg3));
2650 if (!is_error(ret) && arg2)
2651 tput32(arg2, status);
bellard31e31b82003-02-18 22:55:36 +00002652 }
2653 break;
thse5febef2007-04-01 18:31:35 +00002654#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002655#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002656 case TARGET_NR_creat:
pbrook53a59602006-03-25 19:31:22 +00002657 p = lock_user_string(arg1);
2658 ret = get_errno(creat(p, arg2));
2659 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002660 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002661#endif
bellard31e31b82003-02-18 22:55:36 +00002662 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00002663 {
2664 void * p2;
2665 p = lock_user_string(arg1);
2666 p2 = lock_user_string(arg2);
2667 ret = get_errno(link(p, p2));
2668 unlock_user(p2, arg2, 0);
2669 unlock_user(p, arg1, 0);
2670 }
bellard31e31b82003-02-18 22:55:36 +00002671 break;
ths64f0ce42007-09-24 09:25:06 +00002672#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
2673 case TARGET_NR_linkat:
2674 if (!arg2 || !arg4) {
ths0da46a62007-10-20 20:23:07 +00002675 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00002676 goto fail;
ths0da46a62007-10-20 20:23:07 +00002677 }
ths64f0ce42007-09-24 09:25:06 +00002678 {
2679 void * p2 = NULL;
2680 p = lock_user_string(arg2);
2681 p2 = lock_user_string(arg4);
2682 if (!access_ok(VERIFY_READ, p, 1)
2683 || !access_ok(VERIFY_READ, p2, 1))
ths0da46a62007-10-20 20:23:07 +00002684 /* Don't "goto fail" so that cleanup can happen. */
2685 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00002686 else
2687 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
2688 if (p2)
2689 unlock_user(p, arg2, 0);
2690 if (p)
2691 unlock_user(p2, arg4, 0);
2692 }
2693 break;
2694#endif
bellard31e31b82003-02-18 22:55:36 +00002695 case TARGET_NR_unlink:
pbrook53a59602006-03-25 19:31:22 +00002696 p = lock_user_string(arg1);
2697 ret = get_errno(unlink(p));
2698 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002699 break;
ths8170f562007-09-24 09:24:11 +00002700#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
2701 case TARGET_NR_unlinkat:
2702 if (!arg2) {
ths0da46a62007-10-20 20:23:07 +00002703 ret = -TARGET_EFAULT;
ths8170f562007-09-24 09:24:11 +00002704 goto fail;
2705 }
2706 p = lock_user_string(arg2);
2707 if (!access_ok(VERIFY_READ, p, 1))
ths0da46a62007-10-20 20:23:07 +00002708 /* Don't "goto fail" so that cleanup can happen. */
2709 ret = -TARGET_EFAULT;
ths8170f562007-09-24 09:24:11 +00002710 else
2711 ret = get_errno(sys_unlinkat(arg1, p, arg3));
2712 if (p)
2713 unlock_user(p, arg2, 0);
2714 break;
2715#endif
bellard31e31b82003-02-18 22:55:36 +00002716 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00002717 {
2718 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00002719 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00002720 abi_ulong gp;
2721 abi_ulong guest_argp;
2722 abi_ulong guest_envp;
2723 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00002724 char **q;
2725
bellardf7341ff2003-03-30 21:00:25 +00002726 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00002727 guest_argp = arg2;
2728 for (gp = guest_argp; tgetl(gp); gp++)
bellard7854b052003-03-29 17:22:23 +00002729 argc++;
bellardf7341ff2003-03-30 21:00:25 +00002730 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00002731 guest_envp = arg3;
2732 for (gp = guest_envp; tgetl(gp); gp++)
bellard7854b052003-03-29 17:22:23 +00002733 envc++;
2734
bellardf7341ff2003-03-30 21:00:25 +00002735 argp = alloca((argc + 1) * sizeof(void *));
2736 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00002737
pbrook53a59602006-03-25 19:31:22 +00002738 for (gp = guest_argp, q = argp; ;
blueswir1992f48a2007-10-14 16:27:31 +00002739 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002740 addr = tgetl(gp);
2741 if (!addr)
2742 break;
2743 *q = lock_user_string(addr);
2744 }
bellardf7341ff2003-03-30 21:00:25 +00002745 *q = NULL;
2746
pbrook53a59602006-03-25 19:31:22 +00002747 for (gp = guest_envp, q = envp; ;
blueswir1992f48a2007-10-14 16:27:31 +00002748 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002749 addr = tgetl(gp);
2750 if (!addr)
2751 break;
2752 *q = lock_user_string(addr);
2753 }
bellardf7341ff2003-03-30 21:00:25 +00002754 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00002755
pbrook53a59602006-03-25 19:31:22 +00002756 p = lock_user_string(arg1);
2757 ret = get_errno(execve(p, argp, envp));
2758 unlock_user(p, arg1, 0);
2759
2760 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00002761 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002762 addr = tgetl(gp);
2763 unlock_user(*q, addr, 0);
2764 }
2765 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00002766 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002767 addr = tgetl(gp);
2768 unlock_user(*q, addr, 0);
2769 }
bellard7854b052003-03-29 17:22:23 +00002770 }
bellard31e31b82003-02-18 22:55:36 +00002771 break;
2772 case TARGET_NR_chdir:
pbrook53a59602006-03-25 19:31:22 +00002773 p = lock_user_string(arg1);
2774 ret = get_errno(chdir(p));
2775 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002776 break;
bellarda315a142005-01-30 22:59:18 +00002777#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00002778 case TARGET_NR_time:
2779 {
pbrook53a59602006-03-25 19:31:22 +00002780 time_t host_time;
2781 ret = get_errno(time(&host_time));
2782 if (!is_error(ret) && arg1)
2783 tputl(arg1, host_time);
bellard31e31b82003-02-18 22:55:36 +00002784 }
2785 break;
bellarda315a142005-01-30 22:59:18 +00002786#endif
bellard31e31b82003-02-18 22:55:36 +00002787 case TARGET_NR_mknod:
pbrook53a59602006-03-25 19:31:22 +00002788 p = lock_user_string(arg1);
2789 ret = get_errno(mknod(p, arg2, arg3));
2790 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002791 break;
ths75ac37a2007-09-24 09:23:05 +00002792#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
2793 case TARGET_NR_mknodat:
2794 if (!arg2) {
ths0da46a62007-10-20 20:23:07 +00002795 ret = -TARGET_EFAULT;
ths75ac37a2007-09-24 09:23:05 +00002796 goto fail;
2797 }
2798 p = lock_user_string(arg2);
2799 if (!access_ok(VERIFY_READ, p, 1))
ths0da46a62007-10-20 20:23:07 +00002800 /* Don't "goto fail" so that cleanup can happen. */
2801 ret = -TARGET_EFAULT;
ths75ac37a2007-09-24 09:23:05 +00002802 else
2803 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
2804 if (p)
2805 unlock_user(p, arg2, 0);
2806 break;
2807#endif
bellard31e31b82003-02-18 22:55:36 +00002808 case TARGET_NR_chmod:
pbrook53a59602006-03-25 19:31:22 +00002809 p = lock_user_string(arg1);
2810 ret = get_errno(chmod(p, arg2));
2811 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002812 break;
bellardebc05482003-09-30 21:08:41 +00002813#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00002814 case TARGET_NR_break:
2815 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002816#endif
2817#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00002818 case TARGET_NR_oldstat:
2819 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002820#endif
bellard31e31b82003-02-18 22:55:36 +00002821 case TARGET_NR_lseek:
2822 ret = get_errno(lseek(arg1, arg2, arg3));
2823 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002824#ifdef TARGET_NR_getxpid
2825 case TARGET_NR_getxpid:
2826#else
bellard31e31b82003-02-18 22:55:36 +00002827 case TARGET_NR_getpid:
j_mayer7a3148a2007-04-05 07:13:51 +00002828#endif
bellard31e31b82003-02-18 22:55:36 +00002829 ret = get_errno(getpid());
2830 break;
2831 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00002832 {
2833 /* need to look at the data field */
2834 void *p2, *p3;
2835 p = lock_user_string(arg1);
2836 p2 = lock_user_string(arg2);
2837 p3 = lock_user_string(arg3);
2838 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, (const void *)arg5));
2839 unlock_user(p, arg1, 0);
2840 unlock_user(p2, arg2, 0);
2841 unlock_user(p3, arg3, 0);
2842 break;
2843 }
thse5febef2007-04-01 18:31:35 +00002844#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00002845 case TARGET_NR_umount:
pbrook53a59602006-03-25 19:31:22 +00002846 p = lock_user_string(arg1);
2847 ret = get_errno(umount(p));
2848 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002849 break;
thse5febef2007-04-01 18:31:35 +00002850#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002851#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002852 case TARGET_NR_stime:
2853 {
pbrook53a59602006-03-25 19:31:22 +00002854 time_t host_time;
2855 host_time = tgetl(arg1);
2856 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00002857 }
2858 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002859#endif
bellard31e31b82003-02-18 22:55:36 +00002860 case TARGET_NR_ptrace:
2861 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00002862#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002863 case TARGET_NR_alarm:
2864 ret = alarm(arg1);
2865 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002866#endif
bellardebc05482003-09-30 21:08:41 +00002867#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00002868 case TARGET_NR_oldfstat:
2869 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002870#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002871#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002872 case TARGET_NR_pause:
2873 ret = get_errno(pause());
2874 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002875#endif
thse5febef2007-04-01 18:31:35 +00002876#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00002877 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00002878 {
pbrook53a59602006-03-25 19:31:22 +00002879 struct utimbuf tbuf, *host_tbuf;
2880 struct target_utimbuf *target_tbuf;
2881 if (arg2) {
2882 lock_user_struct(target_tbuf, arg2, 1);
2883 tbuf.actime = tswapl(target_tbuf->actime);
2884 tbuf.modtime = tswapl(target_tbuf->modtime);
2885 unlock_user_struct(target_tbuf, arg2, 0);
2886 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00002887 } else {
pbrook53a59602006-03-25 19:31:22 +00002888 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00002889 }
pbrook53a59602006-03-25 19:31:22 +00002890 p = lock_user_string(arg1);
2891 ret = get_errno(utime(p, host_tbuf));
2892 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00002893 }
2894 break;
thse5febef2007-04-01 18:31:35 +00002895#endif
bellard978a66f2004-12-06 22:58:05 +00002896 case TARGET_NR_utimes:
2897 {
bellard978a66f2004-12-06 22:58:05 +00002898 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00002899 if (arg2) {
2900 target_to_host_timeval(&tv[0], arg2);
2901 target_to_host_timeval(&tv[1],
2902 arg2 + sizeof (struct target_timeval));
bellard978a66f2004-12-06 22:58:05 +00002903 tvp = tv;
2904 } else {
2905 tvp = NULL;
2906 }
pbrook53a59602006-03-25 19:31:22 +00002907 p = lock_user_string(arg1);
2908 ret = get_errno(utimes(p, tvp));
2909 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00002910 }
2911 break;
bellardebc05482003-09-30 21:08:41 +00002912#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00002913 case TARGET_NR_stty:
2914 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002915#endif
2916#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00002917 case TARGET_NR_gtty:
2918 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002919#endif
bellard31e31b82003-02-18 22:55:36 +00002920 case TARGET_NR_access:
pbrook53a59602006-03-25 19:31:22 +00002921 p = lock_user_string(arg1);
2922 ret = get_errno(access(p, arg2));
2923 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002924 break;
ths92a34c12007-09-24 09:27:49 +00002925#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
2926 case TARGET_NR_faccessat:
2927 if (!arg2) {
ths0da46a62007-10-20 20:23:07 +00002928 ret = -TARGET_EFAULT;
ths92a34c12007-09-24 09:27:49 +00002929 goto fail;
2930 }
2931 p = lock_user_string(arg2);
2932 if (!access_ok(VERIFY_READ, p, 1))
ths0da46a62007-10-20 20:23:07 +00002933 /* Don't "goto fail" so that cleanup can happen. */
2934 ret = -TARGET_EFAULT;
ths92a34c12007-09-24 09:27:49 +00002935 else
2936 ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
2937 if (p)
2938 unlock_user(p, arg2, 0);
2939 break;
2940#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002941#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002942 case TARGET_NR_nice:
2943 ret = get_errno(nice(arg1));
2944 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002945#endif
bellardebc05482003-09-30 21:08:41 +00002946#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00002947 case TARGET_NR_ftime:
2948 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002949#endif
bellard31e31b82003-02-18 22:55:36 +00002950 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00002951 sync();
2952 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00002953 break;
2954 case TARGET_NR_kill:
2955 ret = get_errno(kill(arg1, arg2));
2956 break;
2957 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00002958 {
2959 void *p2;
2960 p = lock_user_string(arg1);
2961 p2 = lock_user_string(arg2);
2962 ret = get_errno(rename(p, p2));
2963 unlock_user(p2, arg2, 0);
2964 unlock_user(p, arg1, 0);
2965 }
bellard31e31b82003-02-18 22:55:36 +00002966 break;
ths722183f2007-09-24 09:24:37 +00002967#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
2968 case TARGET_NR_renameat:
2969 if (!arg2 || !arg4) {
ths0da46a62007-10-20 20:23:07 +00002970 ret = -TARGET_EFAULT;
2971 goto fail;
ths722183f2007-09-24 09:24:37 +00002972 }
2973 {
2974 void *p2 = NULL;
2975 p = lock_user_string(arg2);
2976 p2 = lock_user_string(arg4);
2977 if (!access_ok(VERIFY_READ, p, 1)
2978 || !access_ok(VERIFY_READ, p2, 1))
ths0da46a62007-10-20 20:23:07 +00002979 /* Don't "goto fail" so that cleanup can happen. */
2980 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00002981 else
2982 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
2983 if (p2)
2984 unlock_user(p2, arg4, 0);
2985 if (p)
2986 unlock_user(p, arg2, 0);
2987 }
2988 break;
2989#endif
bellard31e31b82003-02-18 22:55:36 +00002990 case TARGET_NR_mkdir:
pbrook53a59602006-03-25 19:31:22 +00002991 p = lock_user_string(arg1);
2992 ret = get_errno(mkdir(p, arg2));
2993 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002994 break;
ths4472ad02007-09-24 09:22:32 +00002995#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
2996 case TARGET_NR_mkdirat:
2997 if (!arg2) {
ths0da46a62007-10-20 20:23:07 +00002998 ret = -TARGET_EFAULT;
ths4472ad02007-09-24 09:22:32 +00002999 goto fail;
3000 }
3001 p = lock_user_string(arg2);
3002 if (!access_ok(VERIFY_READ, p, 1))
ths0da46a62007-10-20 20:23:07 +00003003 /* Don't "goto fail" so that cleanup can happen. */
3004 ret = -TARGET_EFAULT;
ths4472ad02007-09-24 09:22:32 +00003005 else
3006 ret = get_errno(sys_mkdirat(arg1, p, arg3));
3007 if (p)
3008 unlock_user(p, arg2, 0);
3009 break;
3010#endif
bellard31e31b82003-02-18 22:55:36 +00003011 case TARGET_NR_rmdir:
pbrook53a59602006-03-25 19:31:22 +00003012 p = lock_user_string(arg1);
3013 ret = get_errno(rmdir(p));
3014 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003015 break;
3016 case TARGET_NR_dup:
3017 ret = get_errno(dup(arg1));
3018 break;
3019 case TARGET_NR_pipe:
3020 {
pbrook53a59602006-03-25 19:31:22 +00003021 int host_pipe[2];
3022 ret = get_errno(pipe(host_pipe));
bellard31e31b82003-02-18 22:55:36 +00003023 if (!is_error(ret)) {
thsc12ab052007-06-01 11:50:36 +00003024#if defined(TARGET_MIPS)
thsead93602007-09-06 00:18:15 +00003025 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
3026 env->gpr[3][env->current_tc] = host_pipe[1];
thsc12ab052007-06-01 11:50:36 +00003027 ret = host_pipe[0];
3028#else
pbrook53a59602006-03-25 19:31:22 +00003029 tput32(arg1, host_pipe[0]);
3030 tput32(arg1 + 4, host_pipe[1]);
thsc12ab052007-06-01 11:50:36 +00003031#endif
bellard31e31b82003-02-18 22:55:36 +00003032 }
3033 }
3034 break;
3035 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00003036 {
pbrook53a59602006-03-25 19:31:22 +00003037 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00003038 struct tms tms;
3039 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00003040 if (arg1) {
3041 tmsp = lock_user(arg1, sizeof(struct target_tms), 0);
bellardc596ed12003-07-13 17:32:31 +00003042 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3043 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3044 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3045 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00003046 }
bellardc596ed12003-07-13 17:32:31 +00003047 if (!is_error(ret))
3048 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00003049 }
3050 break;
bellardebc05482003-09-30 21:08:41 +00003051#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00003052 case TARGET_NR_prof:
3053 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003054#endif
thse5febef2007-04-01 18:31:35 +00003055#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00003056 case TARGET_NR_signal:
3057 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003058#endif
bellard31e31b82003-02-18 22:55:36 +00003059 case TARGET_NR_acct:
pbrook24836682006-04-16 14:14:53 +00003060 p = lock_user_string(arg1);
3061 ret = get_errno(acct(path(p)));
3062 unlock_user(p, arg1, 0);
3063 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003064#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003065 case TARGET_NR_umount2:
pbrook53a59602006-03-25 19:31:22 +00003066 p = lock_user_string(arg1);
3067 ret = get_errno(umount2(p, arg2));
3068 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003069 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003070#endif
bellardebc05482003-09-30 21:08:41 +00003071#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00003072 case TARGET_NR_lock:
3073 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003074#endif
bellard31e31b82003-02-18 22:55:36 +00003075 case TARGET_NR_ioctl:
3076 ret = do_ioctl(arg1, arg2, arg3);
3077 break;
3078 case TARGET_NR_fcntl:
bellard7775e9e2003-05-14 22:46:48 +00003079 ret = get_errno(do_fcntl(arg1, arg2, arg3));
bellard31e31b82003-02-18 22:55:36 +00003080 break;
bellardebc05482003-09-30 21:08:41 +00003081#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00003082 case TARGET_NR_mpx:
3083 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003084#endif
bellard31e31b82003-02-18 22:55:36 +00003085 case TARGET_NR_setpgid:
3086 ret = get_errno(setpgid(arg1, arg2));
3087 break;
bellardebc05482003-09-30 21:08:41 +00003088#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00003089 case TARGET_NR_ulimit:
3090 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003091#endif
3092#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00003093 case TARGET_NR_oldolduname:
3094 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003095#endif
bellard31e31b82003-02-18 22:55:36 +00003096 case TARGET_NR_umask:
3097 ret = get_errno(umask(arg1));
3098 break;
3099 case TARGET_NR_chroot:
pbrook53a59602006-03-25 19:31:22 +00003100 p = lock_user_string(arg1);
3101 ret = get_errno(chroot(p));
3102 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003103 break;
3104 case TARGET_NR_ustat:
3105 goto unimplemented;
3106 case TARGET_NR_dup2:
3107 ret = get_errno(dup2(arg1, arg2));
3108 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003109#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003110 case TARGET_NR_getppid:
3111 ret = get_errno(getppid());
3112 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003113#endif
bellard31e31b82003-02-18 22:55:36 +00003114 case TARGET_NR_getpgrp:
3115 ret = get_errno(getpgrp());
3116 break;
3117 case TARGET_NR_setsid:
3118 ret = get_errno(setsid());
3119 break;
thse5febef2007-04-01 18:31:35 +00003120#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00003121 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00003122 {
ths388bb212007-05-13 13:58:00 +00003123#if !defined(TARGET_MIPS)
pbrook53a59602006-03-25 19:31:22 +00003124 struct target_old_sigaction *old_act;
bellard66fb9762003-03-23 01:06:05 +00003125 struct target_sigaction act, oact, *pact;
pbrook53a59602006-03-25 19:31:22 +00003126 if (arg2) {
3127 lock_user_struct(old_act, arg2, 1);
bellard66fb9762003-03-23 01:06:05 +00003128 act._sa_handler = old_act->_sa_handler;
3129 target_siginitset(&act.sa_mask, old_act->sa_mask);
3130 act.sa_flags = old_act->sa_flags;
3131 act.sa_restorer = old_act->sa_restorer;
pbrook53a59602006-03-25 19:31:22 +00003132 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003133 pact = &act;
3134 } else {
3135 pact = NULL;
3136 }
3137 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00003138 if (!is_error(ret) && arg3) {
3139 lock_user_struct(old_act, arg3, 0);
3140 old_act->_sa_handler = oact._sa_handler;
3141 old_act->sa_mask = oact.sa_mask.sig[0];
3142 old_act->sa_flags = oact.sa_flags;
3143 old_act->sa_restorer = oact.sa_restorer;
3144 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00003145 }
ths388bb212007-05-13 13:58:00 +00003146#else
bellard106ec872006-06-27 21:08:10 +00003147 struct target_sigaction act, oact, *pact, *old_act;
3148
3149 if (arg2) {
3150 lock_user_struct(old_act, arg2, 1);
3151 act._sa_handler = old_act->_sa_handler;
3152 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3153 act.sa_flags = old_act->sa_flags;
3154 unlock_user_struct(old_act, arg2, 0);
3155 pact = &act;
3156 } else {
3157 pact = NULL;
3158 }
3159
3160 ret = get_errno(do_sigaction(arg1, pact, &oact));
3161
3162 if (!is_error(ret) && arg3) {
3163 lock_user_struct(old_act, arg3, 0);
3164 old_act->_sa_handler = oact._sa_handler;
3165 old_act->sa_flags = oact.sa_flags;
3166 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3167 old_act->sa_mask.sig[1] = 0;
3168 old_act->sa_mask.sig[2] = 0;
3169 old_act->sa_mask.sig[3] = 0;
3170 unlock_user_struct(old_act, arg3, 1);
3171 }
ths388bb212007-05-13 13:58:00 +00003172#endif
bellard31e31b82003-02-18 22:55:36 +00003173 }
3174 break;
thse5febef2007-04-01 18:31:35 +00003175#endif
bellard66fb9762003-03-23 01:06:05 +00003176 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00003177 {
3178 struct target_sigaction *act;
3179 struct target_sigaction *oact;
3180
3181 if (arg2)
3182 lock_user_struct(act, arg2, 1);
3183 else
3184 act = NULL;
3185 if (arg3)
3186 lock_user_struct(oact, arg3, 0);
3187 else
3188 oact = NULL;
3189 ret = get_errno(do_sigaction(arg1, act, oact));
3190 if (arg2)
3191 unlock_user_struct(act, arg2, 0);
3192 if (arg3)
3193 unlock_user_struct(oact, arg3, 1);
3194 }
bellard66fb9762003-03-23 01:06:05 +00003195 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003196#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003197 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00003198 {
3199 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00003200 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00003201 sigprocmask(0, NULL, &cur_set);
3202 host_to_target_old_sigset(&target_set, &cur_set);
3203 ret = target_set;
3204 }
3205 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003206#endif
3207#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003208 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00003209 {
3210 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00003211 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00003212 sigprocmask(0, NULL, &cur_set);
3213 target_to_host_old_sigset(&set, &target_set);
3214 sigorset(&set, &set, &cur_set);
3215 sigprocmask(SIG_SETMASK, &set, &oset);
3216 host_to_target_old_sigset(&target_set, &oset);
3217 ret = target_set;
3218 }
3219 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003220#endif
thse5febef2007-04-01 18:31:35 +00003221#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00003222 case TARGET_NR_sigprocmask:
3223 {
3224 int how = arg1;
3225 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003226
pbrook53a59602006-03-25 19:31:22 +00003227 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003228 switch(how) {
3229 case TARGET_SIG_BLOCK:
3230 how = SIG_BLOCK;
3231 break;
3232 case TARGET_SIG_UNBLOCK:
3233 how = SIG_UNBLOCK;
3234 break;
3235 case TARGET_SIG_SETMASK:
3236 how = SIG_SETMASK;
3237 break;
3238 default:
ths0da46a62007-10-20 20:23:07 +00003239 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00003240 goto fail;
3241 }
pbrook53a59602006-03-25 19:31:22 +00003242 p = lock_user(arg2, sizeof(target_sigset_t), 1);
3243 target_to_host_old_sigset(&set, p);
3244 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003245 set_ptr = &set;
3246 } else {
3247 how = 0;
3248 set_ptr = NULL;
3249 }
3250 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003251 if (!is_error(ret) && arg3) {
3252 p = lock_user(arg3, sizeof(target_sigset_t), 0);
3253 host_to_target_old_sigset(p, &oldset);
3254 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003255 }
3256 }
3257 break;
thse5febef2007-04-01 18:31:35 +00003258#endif
bellard66fb9762003-03-23 01:06:05 +00003259 case TARGET_NR_rt_sigprocmask:
3260 {
3261 int how = arg1;
3262 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003263
pbrook53a59602006-03-25 19:31:22 +00003264 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003265 switch(how) {
3266 case TARGET_SIG_BLOCK:
3267 how = SIG_BLOCK;
3268 break;
3269 case TARGET_SIG_UNBLOCK:
3270 how = SIG_UNBLOCK;
3271 break;
3272 case TARGET_SIG_SETMASK:
3273 how = SIG_SETMASK;
3274 break;
3275 default:
ths0da46a62007-10-20 20:23:07 +00003276 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00003277 goto fail;
3278 }
pbrook53a59602006-03-25 19:31:22 +00003279 p = lock_user(arg2, sizeof(target_sigset_t), 1);
3280 target_to_host_sigset(&set, p);
3281 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003282 set_ptr = &set;
3283 } else {
3284 how = 0;
3285 set_ptr = NULL;
3286 }
3287 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003288 if (!is_error(ret) && arg3) {
3289 p = lock_user(arg3, sizeof(target_sigset_t), 0);
3290 host_to_target_sigset(p, &oldset);
3291 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003292 }
3293 }
3294 break;
thse5febef2007-04-01 18:31:35 +00003295#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00003296 case TARGET_NR_sigpending:
3297 {
3298 sigset_t set;
3299 ret = get_errno(sigpending(&set));
3300 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003301 p = lock_user(arg1, sizeof(target_sigset_t), 0);
3302 host_to_target_old_sigset(p, &set);
3303 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003304 }
3305 }
3306 break;
thse5febef2007-04-01 18:31:35 +00003307#endif
bellard66fb9762003-03-23 01:06:05 +00003308 case TARGET_NR_rt_sigpending:
3309 {
3310 sigset_t set;
3311 ret = get_errno(sigpending(&set));
3312 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003313 p = lock_user(arg1, sizeof(target_sigset_t), 0);
3314 host_to_target_sigset(p, &set);
3315 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003316 }
3317 }
3318 break;
thse5febef2007-04-01 18:31:35 +00003319#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00003320 case TARGET_NR_sigsuspend:
3321 {
3322 sigset_t set;
pbrook53a59602006-03-25 19:31:22 +00003323 p = lock_user(arg1, sizeof(target_sigset_t), 1);
3324 target_to_host_old_sigset(&set, p);
3325 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003326 ret = get_errno(sigsuspend(&set));
3327 }
3328 break;
thse5febef2007-04-01 18:31:35 +00003329#endif
bellard66fb9762003-03-23 01:06:05 +00003330 case TARGET_NR_rt_sigsuspend:
3331 {
3332 sigset_t set;
pbrook53a59602006-03-25 19:31:22 +00003333 p = lock_user(arg1, sizeof(target_sigset_t), 1);
3334 target_to_host_sigset(&set, p);
3335 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003336 ret = get_errno(sigsuspend(&set));
3337 }
3338 break;
3339 case TARGET_NR_rt_sigtimedwait:
3340 {
bellard66fb9762003-03-23 01:06:05 +00003341 sigset_t set;
3342 struct timespec uts, *puts;
3343 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00003344
pbrook53a59602006-03-25 19:31:22 +00003345 p = lock_user(arg1, sizeof(target_sigset_t), 1);
3346 target_to_host_sigset(&set, p);
3347 unlock_user(p, arg1, 0);
3348 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00003349 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00003350 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00003351 } else {
3352 puts = NULL;
3353 }
3354 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00003355 if (!is_error(ret) && arg2) {
3356 p = lock_user(arg2, sizeof(target_sigset_t), 0);
3357 host_to_target_siginfo(p, &uinfo);
3358 unlock_user(p, arg2, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003359 }
3360 }
3361 break;
3362 case TARGET_NR_rt_sigqueueinfo:
3363 {
3364 siginfo_t uinfo;
pbrook53a59602006-03-25 19:31:22 +00003365 p = lock_user(arg3, sizeof(target_sigset_t), 1);
3366 target_to_host_siginfo(&uinfo, p);
3367 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003368 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3369 }
3370 break;
thse5febef2007-04-01 18:31:35 +00003371#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00003372 case TARGET_NR_sigreturn:
3373 /* NOTE: ret is eax, so not transcoding must be done */
3374 ret = do_sigreturn(cpu_env);
3375 break;
thse5febef2007-04-01 18:31:35 +00003376#endif
bellard66fb9762003-03-23 01:06:05 +00003377 case TARGET_NR_rt_sigreturn:
3378 /* NOTE: ret is eax, so not transcoding must be done */
3379 ret = do_rt_sigreturn(cpu_env);
3380 break;
bellard31e31b82003-02-18 22:55:36 +00003381 case TARGET_NR_sethostname:
pbrook53a59602006-03-25 19:31:22 +00003382 p = lock_user_string(arg1);
3383 ret = get_errno(sethostname(p, arg2));
3384 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003385 break;
3386 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00003387 {
3388 /* XXX: convert resource ? */
3389 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003390 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003391 struct rlimit rlim;
pbrook53a59602006-03-25 19:31:22 +00003392 lock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00003393 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3394 rlim.rlim_max = tswapl(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00003395 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00003396 ret = get_errno(setrlimit(resource, &rlim));
3397 }
3398 break;
bellard31e31b82003-02-18 22:55:36 +00003399 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00003400 {
3401 /* XXX: convert resource ? */
3402 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003403 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003404 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00003405
bellard9de5e442003-03-23 16:49:39 +00003406 ret = get_errno(getrlimit(resource, &rlim));
3407 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003408 lock_user_struct(target_rlim, arg2, 0);
3409 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3410 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3411 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00003412 }
3413 }
3414 break;
bellard31e31b82003-02-18 22:55:36 +00003415 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00003416 {
3417 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00003418 ret = get_errno(getrusage(arg1, &rusage));
3419 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003420 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00003421 }
3422 }
3423 break;
bellard31e31b82003-02-18 22:55:36 +00003424 case TARGET_NR_gettimeofday:
3425 {
bellard31e31b82003-02-18 22:55:36 +00003426 struct timeval tv;
3427 ret = get_errno(gettimeofday(&tv, NULL));
3428 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003429 host_to_target_timeval(arg1, &tv);
bellard31e31b82003-02-18 22:55:36 +00003430 }
3431 }
3432 break;
3433 case TARGET_NR_settimeofday:
3434 {
bellard31e31b82003-02-18 22:55:36 +00003435 struct timeval tv;
pbrook53a59602006-03-25 19:31:22 +00003436 target_to_host_timeval(&tv, arg1);
bellard31e31b82003-02-18 22:55:36 +00003437 ret = get_errno(settimeofday(&tv, NULL));
3438 }
3439 break;
bellard048f6b42005-11-26 18:47:20 +00003440#ifdef TARGET_NR_select
bellard31e31b82003-02-18 22:55:36 +00003441 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00003442 {
pbrook53a59602006-03-25 19:31:22 +00003443 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00003444 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00003445 long nsel;
3446
3447 lock_user_struct(sel, arg1, 1);
3448 nsel = tswapl(sel->n);
3449 inp = tswapl(sel->inp);
3450 outp = tswapl(sel->outp);
3451 exp = tswapl(sel->exp);
3452 tvp = tswapl(sel->tvp);
3453 unlock_user_struct(sel, arg1, 0);
3454 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00003455 }
3456 break;
bellard048f6b42005-11-26 18:47:20 +00003457#endif
bellard31e31b82003-02-18 22:55:36 +00003458 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00003459 {
3460 void *p2;
3461 p = lock_user_string(arg1);
3462 p2 = lock_user_string(arg2);
3463 ret = get_errno(symlink(p, p2));
3464 unlock_user(p2, arg2, 0);
3465 unlock_user(p, arg1, 0);
3466 }
bellard31e31b82003-02-18 22:55:36 +00003467 break;
thsf0b62432007-09-24 09:25:40 +00003468#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
3469 case TARGET_NR_symlinkat:
3470 if (!arg1 || !arg3) {
ths0da46a62007-10-20 20:23:07 +00003471 ret = -TARGET_EFAULT;
3472 goto fail;
3473 }
thsf0b62432007-09-24 09:25:40 +00003474 {
3475 void *p2 = NULL;
3476 p = lock_user_string(arg1);
3477 p2 = lock_user_string(arg3);
3478 if (!access_ok(VERIFY_READ, p, 1)
3479 || !access_ok(VERIFY_READ, p2, 1))
ths0da46a62007-10-20 20:23:07 +00003480 /* Don't "goto fail" so that cleanup can happen. */
3481 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00003482 else
3483 ret = get_errno(sys_symlinkat(p, arg2, p2));
3484 if (p2)
3485 unlock_user(p2, arg3, 0);
3486 if (p)
3487 unlock_user(p, arg1, 0);
3488 }
3489 break;
3490#endif
bellardebc05482003-09-30 21:08:41 +00003491#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00003492 case TARGET_NR_oldlstat:
3493 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003494#endif
bellard31e31b82003-02-18 22:55:36 +00003495 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00003496 {
3497 void *p2;
3498 p = lock_user_string(arg1);
3499 p2 = lock_user(arg2, arg3, 0);
3500 ret = get_errno(readlink(path(p), p2, arg3));
3501 unlock_user(p2, arg2, ret);
3502 unlock_user(p, arg1, 0);
3503 }
bellard31e31b82003-02-18 22:55:36 +00003504 break;
ths5e0ccb12007-09-24 09:26:10 +00003505#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
3506 case TARGET_NR_readlinkat:
3507 if (!arg2 || !arg3) {
ths0da46a62007-10-20 20:23:07 +00003508 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00003509 goto fail;
ths0da46a62007-10-20 20:23:07 +00003510 }
ths5e0ccb12007-09-24 09:26:10 +00003511 {
3512 void *p2 = NULL;
3513 p = lock_user_string(arg2);
3514 p2 = lock_user(arg3, arg4, 0);
3515 if (!access_ok(VERIFY_READ, p, 1)
3516 || !access_ok(VERIFY_READ, p2, 1))
ths0da46a62007-10-20 20:23:07 +00003517 /* Don't "goto fail" so that cleanup can happen. */
3518 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00003519 else
3520 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
3521 if (p2)
3522 unlock_user(p2, arg3, ret);
3523 if (p)
3524 unlock_user(p, arg2, 0);
3525 }
3526 break;
3527#endif
thse5febef2007-04-01 18:31:35 +00003528#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00003529 case TARGET_NR_uselib:
3530 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003531#endif
3532#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00003533 case TARGET_NR_swapon:
pbrook53a59602006-03-25 19:31:22 +00003534 p = lock_user_string(arg1);
3535 ret = get_errno(swapon(p, arg2));
3536 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003537 break;
thse5febef2007-04-01 18:31:35 +00003538#endif
bellard31e31b82003-02-18 22:55:36 +00003539 case TARGET_NR_reboot:
3540 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003541#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00003542 case TARGET_NR_readdir:
3543 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003544#endif
3545#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00003546 case TARGET_NR_mmap:
ths48733d12007-10-08 13:36:46 +00003547#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
bellard31e31b82003-02-18 22:55:36 +00003548 {
blueswir1992f48a2007-10-14 16:27:31 +00003549 abi_ulong *v;
3550 abi_ulong v1, v2, v3, v4, v5, v6;
3551 v = lock_user(arg1, 6 * sizeof(abi_ulong), 1);
pbrook53a59602006-03-25 19:31:22 +00003552 v1 = tswapl(v[0]);
3553 v2 = tswapl(v[1]);
3554 v3 = tswapl(v[2]);
3555 v4 = tswapl(v[3]);
3556 v5 = tswapl(v[4]);
3557 v6 = tswapl(v[5]);
3558 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00003559 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00003560 target_to_host_bitmask(v4, mmap_flags_tbl),
3561 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00003562 }
bellard31e31b82003-02-18 22:55:36 +00003563#else
ths5fafdf22007-09-16 21:08:06 +00003564 ret = get_errno(target_mmap(arg1, arg2, arg3,
3565 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00003566 arg5,
3567 arg6));
bellard31e31b82003-02-18 22:55:36 +00003568#endif
bellard6fb883e2003-07-09 17:12:39 +00003569 break;
thse5febef2007-04-01 18:31:35 +00003570#endif
bellarda315a142005-01-30 22:59:18 +00003571#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00003572 case TARGET_NR_mmap2:
ths0d3267a2006-12-11 22:04:25 +00003573#if defined(TARGET_SPARC) || defined(TARGET_MIPS)
bellardc573ff62004-01-04 15:51:36 +00003574#define MMAP_SHIFT 12
3575#else
3576#define MMAP_SHIFT TARGET_PAGE_BITS
3577#endif
ths5fafdf22007-09-16 21:08:06 +00003578 ret = get_errno(target_mmap(arg1, arg2, arg3,
3579 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00003580 arg5,
bellardc573ff62004-01-04 15:51:36 +00003581 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00003582 break;
bellarda315a142005-01-30 22:59:18 +00003583#endif
bellard31e31b82003-02-18 22:55:36 +00003584 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00003585 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00003586 break;
bellard9de5e442003-03-23 16:49:39 +00003587 case TARGET_NR_mprotect:
bellard54936002003-05-13 00:25:15 +00003588 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00003589 break;
thse5febef2007-04-01 18:31:35 +00003590#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00003591 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00003592 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00003593 break;
thse5febef2007-04-01 18:31:35 +00003594#endif
pbrook53a59602006-03-25 19:31:22 +00003595 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00003596#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00003597 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00003598 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00003599 break;
thse5febef2007-04-01 18:31:35 +00003600#endif
3601#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00003602 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00003603 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00003604 break;
thse5febef2007-04-01 18:31:35 +00003605#endif
3606#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00003607 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00003608 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00003609 break;
thse5febef2007-04-01 18:31:35 +00003610#endif
3611#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00003612 case TARGET_NR_mlockall:
3613 ret = get_errno(mlockall(arg1));
3614 break;
thse5febef2007-04-01 18:31:35 +00003615#endif
3616#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00003617 case TARGET_NR_munlockall:
3618 ret = get_errno(munlockall());
3619 break;
thse5febef2007-04-01 18:31:35 +00003620#endif
bellard31e31b82003-02-18 22:55:36 +00003621 case TARGET_NR_truncate:
pbrook53a59602006-03-25 19:31:22 +00003622 p = lock_user_string(arg1);
3623 ret = get_errno(truncate(p, arg2));
3624 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003625 break;
3626 case TARGET_NR_ftruncate:
3627 ret = get_errno(ftruncate(arg1, arg2));
3628 break;
3629 case TARGET_NR_fchmod:
3630 ret = get_errno(fchmod(arg1, arg2));
3631 break;
ths814d7972007-09-24 09:26:51 +00003632#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
3633 case TARGET_NR_fchmodat:
3634 if (!arg2) {
ths0da46a62007-10-20 20:23:07 +00003635 ret = -TARGET_EFAULT;
ths814d7972007-09-24 09:26:51 +00003636 goto fail;
3637 }
3638 p = lock_user_string(arg2);
3639 if (!access_ok(VERIFY_READ, p, 1))
ths0da46a62007-10-20 20:23:07 +00003640 /* Don't "goto fail" so that cleanup can happen. */
3641 ret = -TARGET_EFAULT;
ths814d7972007-09-24 09:26:51 +00003642 else
3643 ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
3644 if (p)
3645 unlock_user(p, arg2, 0);
3646 break;
3647#endif
bellard31e31b82003-02-18 22:55:36 +00003648 case TARGET_NR_getpriority:
thsc6cda172007-10-09 03:42:34 +00003649 /* libc does special remapping of the return value of
3650 * sys_getpriority() so it's just easiest to call
3651 * sys_getpriority() directly rather than through libc. */
3652 ret = sys_getpriority(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00003653 break;
3654 case TARGET_NR_setpriority:
3655 ret = get_errno(setpriority(arg1, arg2, arg3));
3656 break;
bellardebc05482003-09-30 21:08:41 +00003657#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00003658 case TARGET_NR_profil:
3659 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003660#endif
bellard31e31b82003-02-18 22:55:36 +00003661 case TARGET_NR_statfs:
pbrook53a59602006-03-25 19:31:22 +00003662 p = lock_user_string(arg1);
3663 ret = get_errno(statfs(path(p), &stfs));
3664 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003665 convert_statfs:
3666 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003667 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00003668
pbrook53a59602006-03-25 19:31:22 +00003669 lock_user_struct(target_stfs, arg2, 0);
3670 /* ??? put_user is probably wrong. */
bellard56c8f682005-11-28 22:28:41 +00003671 put_user(stfs.f_type, &target_stfs->f_type);
3672 put_user(stfs.f_bsize, &target_stfs->f_bsize);
3673 put_user(stfs.f_blocks, &target_stfs->f_blocks);
3674 put_user(stfs.f_bfree, &target_stfs->f_bfree);
3675 put_user(stfs.f_bavail, &target_stfs->f_bavail);
3676 put_user(stfs.f_files, &target_stfs->f_files);
3677 put_user(stfs.f_ffree, &target_stfs->f_ffree);
ths4ce6f8d2007-07-20 15:54:27 +00003678 put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3679 put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
bellard56c8f682005-11-28 22:28:41 +00003680 put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00003681 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00003682 }
3683 break;
3684 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00003685 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00003686 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00003687#ifdef TARGET_NR_statfs64
3688 case TARGET_NR_statfs64:
pbrook53a59602006-03-25 19:31:22 +00003689 p = lock_user_string(arg1);
3690 ret = get_errno(statfs(path(p), &stfs));
3691 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00003692 convert_statfs64:
3693 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003694 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00003695
pbrook53a59602006-03-25 19:31:22 +00003696 lock_user_struct(target_stfs, arg3, 0);
3697 /* ??? put_user is probably wrong. */
bellard56c8f682005-11-28 22:28:41 +00003698 put_user(stfs.f_type, &target_stfs->f_type);
3699 put_user(stfs.f_bsize, &target_stfs->f_bsize);
3700 put_user(stfs.f_blocks, &target_stfs->f_blocks);
3701 put_user(stfs.f_bfree, &target_stfs->f_bfree);
3702 put_user(stfs.f_bavail, &target_stfs->f_bavail);
3703 put_user(stfs.f_files, &target_stfs->f_files);
3704 put_user(stfs.f_ffree, &target_stfs->f_ffree);
ths4ce6f8d2007-07-20 15:54:27 +00003705 put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3706 put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
bellard56c8f682005-11-28 22:28:41 +00003707 put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00003708 unlock_user_struct(target_stfs, arg3, 0);
bellard56c8f682005-11-28 22:28:41 +00003709 }
3710 break;
3711 case TARGET_NR_fstatfs64:
3712 ret = get_errno(fstatfs(arg1, &stfs));
3713 goto convert_statfs64;
3714#endif
bellardebc05482003-09-30 21:08:41 +00003715#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00003716 case TARGET_NR_ioperm:
3717 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003718#endif
thse5febef2007-04-01 18:31:35 +00003719#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00003720 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00003721 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00003722 break;
thse5febef2007-04-01 18:31:35 +00003723#endif
bellard3532fa72006-06-24 15:06:03 +00003724#ifdef TARGET_NR_accept
3725 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00003726 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003727 break;
3728#endif
3729#ifdef TARGET_NR_bind
3730 case TARGET_NR_bind:
3731 ret = do_bind(arg1, arg2, arg3);
3732 break;
3733#endif
3734#ifdef TARGET_NR_connect
3735 case TARGET_NR_connect:
3736 ret = do_connect(arg1, arg2, arg3);
3737 break;
3738#endif
3739#ifdef TARGET_NR_getpeername
3740 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00003741 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003742 break;
3743#endif
3744#ifdef TARGET_NR_getsockname
3745 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00003746 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003747 break;
3748#endif
3749#ifdef TARGET_NR_getsockopt
3750 case TARGET_NR_getsockopt:
3751 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
3752 break;
3753#endif
3754#ifdef TARGET_NR_listen
3755 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00003756 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00003757 break;
3758#endif
3759#ifdef TARGET_NR_recv
3760 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00003761 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00003762 break;
3763#endif
3764#ifdef TARGET_NR_recvfrom
3765 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00003766 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00003767 break;
3768#endif
3769#ifdef TARGET_NR_recvmsg
3770 case TARGET_NR_recvmsg:
3771 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
3772 break;
3773#endif
3774#ifdef TARGET_NR_send
3775 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00003776 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00003777 break;
3778#endif
3779#ifdef TARGET_NR_sendmsg
3780 case TARGET_NR_sendmsg:
3781 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
3782 break;
3783#endif
3784#ifdef TARGET_NR_sendto
3785 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00003786 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00003787 break;
3788#endif
3789#ifdef TARGET_NR_shutdown
3790 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00003791 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00003792 break;
3793#endif
3794#ifdef TARGET_NR_socket
3795 case TARGET_NR_socket:
3796 ret = do_socket(arg1, arg2, arg3);
3797 break;
3798#endif
3799#ifdef TARGET_NR_socketpair
3800 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00003801 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00003802 break;
3803#endif
3804#ifdef TARGET_NR_setsockopt
3805 case TARGET_NR_setsockopt:
3806 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
3807 break;
3808#endif
ths7494b0f2007-02-11 18:26:53 +00003809
bellard31e31b82003-02-18 22:55:36 +00003810 case TARGET_NR_syslog:
thse5574482007-02-11 20:03:13 +00003811 p = lock_user_string(arg2);
3812 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
3813 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00003814 break;
3815
bellard31e31b82003-02-18 22:55:36 +00003816 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00003817 {
bellard66fb9762003-03-23 01:06:05 +00003818 struct itimerval value, ovalue, *pvalue;
3819
pbrook53a59602006-03-25 19:31:22 +00003820 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003821 pvalue = &value;
ths5fafdf22007-09-16 21:08:06 +00003822 target_to_host_timeval(&pvalue->it_interval,
pbrook53a59602006-03-25 19:31:22 +00003823 arg2);
ths5fafdf22007-09-16 21:08:06 +00003824 target_to_host_timeval(&pvalue->it_value,
pbrook53a59602006-03-25 19:31:22 +00003825 arg2 + sizeof(struct target_timeval));
bellard66fb9762003-03-23 01:06:05 +00003826 } else {
3827 pvalue = NULL;
3828 }
3829 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00003830 if (!is_error(ret) && arg3) {
3831 host_to_target_timeval(arg3,
bellard66fb9762003-03-23 01:06:05 +00003832 &ovalue.it_interval);
pbrook53a59602006-03-25 19:31:22 +00003833 host_to_target_timeval(arg3 + sizeof(struct target_timeval),
bellard66fb9762003-03-23 01:06:05 +00003834 &ovalue.it_value);
3835 }
3836 }
3837 break;
bellard31e31b82003-02-18 22:55:36 +00003838 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00003839 {
bellard66fb9762003-03-23 01:06:05 +00003840 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00003841
bellard66fb9762003-03-23 01:06:05 +00003842 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00003843 if (!is_error(ret) && arg2) {
3844 host_to_target_timeval(arg2,
bellard66fb9762003-03-23 01:06:05 +00003845 &value.it_interval);
pbrook53a59602006-03-25 19:31:22 +00003846 host_to_target_timeval(arg2 + sizeof(struct target_timeval),
bellard66fb9762003-03-23 01:06:05 +00003847 &value.it_value);
3848 }
3849 }
3850 break;
bellard31e31b82003-02-18 22:55:36 +00003851 case TARGET_NR_stat:
pbrook53a59602006-03-25 19:31:22 +00003852 p = lock_user_string(arg1);
3853 ret = get_errno(stat(path(p), &st));
3854 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003855 goto do_stat;
3856 case TARGET_NR_lstat:
pbrook53a59602006-03-25 19:31:22 +00003857 p = lock_user_string(arg1);
3858 ret = get_errno(lstat(path(p), &st));
3859 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003860 goto do_stat;
3861 case TARGET_NR_fstat:
3862 {
3863 ret = get_errno(fstat(arg1, &st));
3864 do_stat:
3865 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003866 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00003867
pbrook53a59602006-03-25 19:31:22 +00003868 lock_user_struct(target_st, arg2, 0);
blueswir1992f48a2007-10-14 16:27:31 +00003869#if defined(TARGET_MIPS) || (defined(TARGET_SPARC64) && !defined(TARGET_ABI32))
thse3584652007-06-01 11:49:38 +00003870 target_st->st_dev = tswap32(st.st_dev);
3871#else
bellard31e31b82003-02-18 22:55:36 +00003872 target_st->st_dev = tswap16(st.st_dev);
thse3584652007-06-01 11:49:38 +00003873#endif
bellard31e31b82003-02-18 22:55:36 +00003874 target_st->st_ino = tswapl(st.st_ino);
ths7d600c82006-12-08 01:32:58 +00003875#if defined(TARGET_PPC) || defined(TARGET_MIPS)
bellard67867302003-11-23 17:05:30 +00003876 target_st->st_mode = tswapl(st.st_mode); /* XXX: check this */
3877 target_st->st_uid = tswap32(st.st_uid);
3878 target_st->st_gid = tswap32(st.st_gid);
blueswir1992f48a2007-10-14 16:27:31 +00003879#elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir11b8dd642007-07-08 10:08:24 +00003880 target_st->st_mode = tswap32(st.st_mode);
3881 target_st->st_uid = tswap32(st.st_uid);
3882 target_st->st_gid = tswap32(st.st_gid);
bellard67867302003-11-23 17:05:30 +00003883#else
bellardec86b0f2003-04-11 00:15:04 +00003884 target_st->st_mode = tswap16(st.st_mode);
bellard31e31b82003-02-18 22:55:36 +00003885 target_st->st_uid = tswap16(st.st_uid);
3886 target_st->st_gid = tswap16(st.st_gid);
bellard67867302003-11-23 17:05:30 +00003887#endif
thse3584652007-06-01 11:49:38 +00003888#if defined(TARGET_MIPS)
3889 /* If this is the same on PPC, then just merge w/ the above ifdef */
3890 target_st->st_nlink = tswapl(st.st_nlink);
3891 target_st->st_rdev = tswapl(st.st_rdev);
blueswir1992f48a2007-10-14 16:27:31 +00003892#elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir11b8dd642007-07-08 10:08:24 +00003893 target_st->st_nlink = tswap32(st.st_nlink);
3894 target_st->st_rdev = tswap32(st.st_rdev);
thse3584652007-06-01 11:49:38 +00003895#else
bellard67867302003-11-23 17:05:30 +00003896 target_st->st_nlink = tswap16(st.st_nlink);
bellard31e31b82003-02-18 22:55:36 +00003897 target_st->st_rdev = tswap16(st.st_rdev);
thse3584652007-06-01 11:49:38 +00003898#endif
bellard31e31b82003-02-18 22:55:36 +00003899 target_st->st_size = tswapl(st.st_size);
3900 target_st->st_blksize = tswapl(st.st_blksize);
3901 target_st->st_blocks = tswapl(st.st_blocks);
bellard7854b052003-03-29 17:22:23 +00003902 target_st->target_st_atime = tswapl(st.st_atime);
3903 target_st->target_st_mtime = tswapl(st.st_mtime);
3904 target_st->target_st_ctime = tswapl(st.st_ctime);
pbrook53a59602006-03-25 19:31:22 +00003905 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00003906 }
3907 }
3908 break;
bellardebc05482003-09-30 21:08:41 +00003909#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00003910 case TARGET_NR_olduname:
3911 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003912#endif
3913#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00003914 case TARGET_NR_iopl:
3915 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003916#endif
bellard31e31b82003-02-18 22:55:36 +00003917 case TARGET_NR_vhangup:
3918 ret = get_errno(vhangup());
3919 break;
bellardebc05482003-09-30 21:08:41 +00003920#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00003921 case TARGET_NR_idle:
3922 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003923#endif
bellard42ad6ae2005-01-03 22:48:11 +00003924#ifdef TARGET_NR_syscall
3925 case TARGET_NR_syscall:
3926 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
3927 break;
3928#endif
bellard31e31b82003-02-18 22:55:36 +00003929 case TARGET_NR_wait4:
3930 {
3931 int status;
blueswir1992f48a2007-10-14 16:27:31 +00003932 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00003933 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00003934 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00003935 if (target_rusage)
3936 rusage_ptr = &rusage;
3937 else
3938 rusage_ptr = NULL;
3939 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
3940 if (!is_error(ret)) {
3941 if (status_ptr)
pbrook53a59602006-03-25 19:31:22 +00003942 tputl(status_ptr, status);
bellard31e31b82003-02-18 22:55:36 +00003943 if (target_rusage) {
bellardb4091862003-05-16 15:39:34 +00003944 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00003945 }
3946 }
3947 }
3948 break;
thse5febef2007-04-01 18:31:35 +00003949#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00003950 case TARGET_NR_swapoff:
pbrook53a59602006-03-25 19:31:22 +00003951 p = lock_user_string(arg1);
3952 ret = get_errno(swapoff(p));
3953 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003954 break;
thse5febef2007-04-01 18:31:35 +00003955#endif
bellard31e31b82003-02-18 22:55:36 +00003956 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00003957 {
pbrook53a59602006-03-25 19:31:22 +00003958 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00003959 struct sysinfo value;
3960 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00003961 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00003962 {
pbrook53a59602006-03-25 19:31:22 +00003963 /* ??? __put_user is probably wrong. */
3964 lock_user_struct(target_value, arg1, 0);
bellarda5448a72004-06-19 16:59:03 +00003965 __put_user(value.uptime, &target_value->uptime);
3966 __put_user(value.loads[0], &target_value->loads[0]);
3967 __put_user(value.loads[1], &target_value->loads[1]);
3968 __put_user(value.loads[2], &target_value->loads[2]);
3969 __put_user(value.totalram, &target_value->totalram);
3970 __put_user(value.freeram, &target_value->freeram);
3971 __put_user(value.sharedram, &target_value->sharedram);
3972 __put_user(value.bufferram, &target_value->bufferram);
3973 __put_user(value.totalswap, &target_value->totalswap);
3974 __put_user(value.freeswap, &target_value->freeswap);
3975 __put_user(value.procs, &target_value->procs);
3976 __put_user(value.totalhigh, &target_value->totalhigh);
3977 __put_user(value.freehigh, &target_value->freehigh);
3978 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00003979 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00003980 }
3981 }
3982 break;
thse5febef2007-04-01 18:31:35 +00003983#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00003984 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00003985 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
3986 break;
thse5febef2007-04-01 18:31:35 +00003987#endif
bellard31e31b82003-02-18 22:55:36 +00003988 case TARGET_NR_fsync:
3989 ret = get_errno(fsync(arg1));
3990 break;
bellard31e31b82003-02-18 22:55:36 +00003991 case TARGET_NR_clone:
bellard1b6b0292003-03-22 17:31:38 +00003992 ret = get_errno(do_fork(cpu_env, arg1, arg2));
3993 break;
bellardec86b0f2003-04-11 00:15:04 +00003994#ifdef __NR_exit_group
3995 /* new thread calls */
3996 case TARGET_NR_exit_group:
bellarde9009672005-04-26 20:42:36 +00003997 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00003998 ret = get_errno(exit_group(arg1));
3999 break;
4000#endif
bellard31e31b82003-02-18 22:55:36 +00004001 case TARGET_NR_setdomainname:
pbrook53a59602006-03-25 19:31:22 +00004002 p = lock_user_string(arg1);
4003 ret = get_errno(setdomainname(p, arg2));
4004 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004005 break;
4006 case TARGET_NR_uname:
4007 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00004008 {
4009 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00004010
pbrook53a59602006-03-25 19:31:22 +00004011 lock_user_struct(buf, arg1, 0);
bellard29e619b2004-09-13 21:41:04 +00004012 ret = get_errno(sys_uname(buf));
4013 if (!is_error(ret)) {
4014 /* Overrite the native machine name with whatever is being
4015 emulated. */
4016 strcpy (buf->machine, UNAME_MACHINE);
pbrookc5937222006-05-14 11:30:38 +00004017 /* Allow the user to override the reported release. */
4018 if (qemu_uname_release && *qemu_uname_release)
4019 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00004020 }
pbrook53a59602006-03-25 19:31:22 +00004021 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00004022 }
bellard31e31b82003-02-18 22:55:36 +00004023 break;
bellard6dbad632003-03-16 18:05:05 +00004024#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00004025 case TARGET_NR_modify_ldt:
pbrook53a59602006-03-25 19:31:22 +00004026 ret = get_errno(do_modify_ldt(cpu_env, arg1, arg2, arg3));
bellard5cd43932003-03-29 16:54:36 +00004027 break;
j_mayer84409dd2007-04-06 08:56:50 +00004028#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00004029 case TARGET_NR_vm86old:
4030 goto unimplemented;
4031 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00004032 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00004033 break;
4034#endif
j_mayer84409dd2007-04-06 08:56:50 +00004035#endif
bellard31e31b82003-02-18 22:55:36 +00004036 case TARGET_NR_adjtimex:
4037 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004038#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00004039 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00004040#endif
bellard31e31b82003-02-18 22:55:36 +00004041 case TARGET_NR_init_module:
4042 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00004043#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00004044 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00004045#endif
bellard31e31b82003-02-18 22:55:36 +00004046 goto unimplemented;
4047 case TARGET_NR_quotactl:
4048 goto unimplemented;
4049 case TARGET_NR_getpgid:
4050 ret = get_errno(getpgid(arg1));
4051 break;
4052 case TARGET_NR_fchdir:
4053 ret = get_errno(fchdir(arg1));
4054 break;
j_mayer84409dd2007-04-06 08:56:50 +00004055#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00004056 case TARGET_NR_bdflush:
4057 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00004058#endif
thse5febef2007-04-01 18:31:35 +00004059#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00004060 case TARGET_NR_sysfs:
4061 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004062#endif
bellard31e31b82003-02-18 22:55:36 +00004063 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00004064 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00004065 break;
thse5febef2007-04-01 18:31:35 +00004066#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00004067 case TARGET_NR_afs_syscall:
4068 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004069#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004070#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004071 case TARGET_NR__llseek:
4072 {
bellard4f2ac232004-04-26 19:44:02 +00004073#if defined (__x86_64__)
4074 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
pbrook53a59602006-03-25 19:31:22 +00004075 tput64(arg4, ret);
bellard4f2ac232004-04-26 19:44:02 +00004076#else
bellard31e31b82003-02-18 22:55:36 +00004077 int64_t res;
4078 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
pbrook53a59602006-03-25 19:31:22 +00004079 tput64(arg4, res);
bellard4f2ac232004-04-26 19:44:02 +00004080#endif
bellard31e31b82003-02-18 22:55:36 +00004081 }
4082 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004083#endif
bellard31e31b82003-02-18 22:55:36 +00004084 case TARGET_NR_getdents:
blueswir1992f48a2007-10-14 16:27:31 +00004085#if TARGET_ABI_BITS != 32
pbrook53a59602006-03-25 19:31:22 +00004086 goto unimplemented;
bellarda315a142005-01-30 22:59:18 +00004087#warning not supported
blueswir1992f48a2007-10-14 16:27:31 +00004088#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00004089 {
pbrook53a59602006-03-25 19:31:22 +00004090 struct target_dirent *target_dirp;
bellard4add45b2003-06-05 01:52:59 +00004091 struct dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004092 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00004093
4094 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00004095 if (!dirp) {
4096 ret = -TARGET_EFAULT;
4097 goto fail;
4098 }
ths3b46e622007-09-17 08:09:54 +00004099
bellard4add45b2003-06-05 01:52:59 +00004100 ret = get_errno(sys_getdents(arg1, dirp, count));
4101 if (!is_error(ret)) {
4102 struct dirent *de;
4103 struct target_dirent *tde;
4104 int len = ret;
4105 int reclen, treclen;
4106 int count1, tnamelen;
4107
4108 count1 = 0;
4109 de = dirp;
pbrook53a59602006-03-25 19:31:22 +00004110 target_dirp = lock_user(arg2, count, 0);
bellard4add45b2003-06-05 01:52:59 +00004111 tde = target_dirp;
4112 while (len > 0) {
4113 reclen = de->d_reclen;
blueswir1992f48a2007-10-14 16:27:31 +00004114 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
bellard4add45b2003-06-05 01:52:59 +00004115 tde->d_reclen = tswap16(treclen);
4116 tde->d_ino = tswapl(de->d_ino);
4117 tde->d_off = tswapl(de->d_off);
blueswir1992f48a2007-10-14 16:27:31 +00004118 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
bellard4add45b2003-06-05 01:52:59 +00004119 if (tnamelen > 256)
4120 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00004121 /* XXX: may not be correct */
bellard4add45b2003-06-05 01:52:59 +00004122 strncpy(tde->d_name, de->d_name, tnamelen);
4123 de = (struct dirent *)((char *)de + reclen);
4124 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00004125 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00004126 count1 += treclen;
4127 }
4128 ret = count1;
4129 }
pbrook53a59602006-03-25 19:31:22 +00004130 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00004131 free(dirp);
4132 }
4133#else
bellard31e31b82003-02-18 22:55:36 +00004134 {
pbrook53a59602006-03-25 19:31:22 +00004135 struct dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004136 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00004137
pbrook53a59602006-03-25 19:31:22 +00004138 dirp = lock_user(arg2, count, 0);
bellard72f03902003-02-18 23:33:18 +00004139 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00004140 if (!is_error(ret)) {
4141 struct dirent *de;
4142 int len = ret;
4143 int reclen;
4144 de = dirp;
4145 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004146 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00004147 if (reclen > len)
4148 break;
bellard8083a3e2003-03-24 23:12:16 +00004149 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00004150 tswapls(&de->d_ino);
4151 tswapls(&de->d_off);
4152 de = (struct dirent *)((char *)de + reclen);
4153 len -= reclen;
4154 }
4155 }
pbrook53a59602006-03-25 19:31:22 +00004156 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00004157 }
bellard4add45b2003-06-05 01:52:59 +00004158#endif
bellard31e31b82003-02-18 22:55:36 +00004159 break;
ths3ae43202007-09-16 21:39:48 +00004160#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00004161 case TARGET_NR_getdents64:
4162 {
pbrook53a59602006-03-25 19:31:22 +00004163 struct dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004164 abi_long count = arg3;
pbrook53a59602006-03-25 19:31:22 +00004165 dirp = lock_user(arg2, count, 0);
bellarddab2ed92003-03-22 15:23:14 +00004166 ret = get_errno(sys_getdents64(arg1, dirp, count));
4167 if (!is_error(ret)) {
4168 struct dirent64 *de;
4169 int len = ret;
4170 int reclen;
4171 de = dirp;
4172 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004173 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00004174 if (reclen > len)
4175 break;
bellard8083a3e2003-03-24 23:12:16 +00004176 de->d_reclen = tswap16(reclen);
bellarddab2ed92003-03-22 15:23:14 +00004177 tswap64s(&de->d_ino);
4178 tswap64s(&de->d_off);
4179 de = (struct dirent64 *)((char *)de + reclen);
4180 len -= reclen;
4181 }
4182 }
pbrook53a59602006-03-25 19:31:22 +00004183 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00004184 }
4185 break;
bellarda541f292004-04-12 20:39:29 +00004186#endif /* TARGET_NR_getdents64 */
thse5febef2007-04-01 18:31:35 +00004187#ifdef TARGET_NR__newselect
bellard31e31b82003-02-18 22:55:36 +00004188 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00004189 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00004190 break;
thse5febef2007-04-01 18:31:35 +00004191#endif
4192#ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00004193 case TARGET_NR_poll:
4194 {
pbrook53a59602006-03-25 19:31:22 +00004195 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00004196 unsigned int nfds = arg2;
4197 int timeout = arg3;
4198 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00004199 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00004200
pbrook53a59602006-03-25 19:31:22 +00004201 target_pfd = lock_user(arg1, sizeof(struct target_pollfd) * nfds, 1);
bellard9de5e442003-03-23 16:49:39 +00004202 pfd = alloca(sizeof(struct pollfd) * nfds);
4203 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004204 pfd[i].fd = tswap32(target_pfd[i].fd);
4205 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00004206 }
4207 ret = get_errno(poll(pfd, nfds, timeout));
4208 if (!is_error(ret)) {
4209 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004210 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00004211 }
pbrook53a59602006-03-25 19:31:22 +00004212 ret += nfds * (sizeof(struct target_pollfd)
4213 - sizeof(struct pollfd));
bellard9de5e442003-03-23 16:49:39 +00004214 }
pbrook53a59602006-03-25 19:31:22 +00004215 unlock_user(target_pfd, arg1, ret);
bellard9de5e442003-03-23 16:49:39 +00004216 }
4217 break;
thse5febef2007-04-01 18:31:35 +00004218#endif
bellard31e31b82003-02-18 22:55:36 +00004219 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00004220 /* NOTE: the flock constant seems to be the same for every
4221 Linux platform */
4222 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00004223 break;
4224 case TARGET_NR_readv:
4225 {
4226 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00004227 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00004228
4229 vec = alloca(count * sizeof(struct iovec));
pbrook53a59602006-03-25 19:31:22 +00004230 lock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00004231 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00004232 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00004233 }
4234 break;
4235 case TARGET_NR_writev:
4236 {
4237 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00004238 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00004239
4240 vec = alloca(count * sizeof(struct iovec));
pbrook53a59602006-03-25 19:31:22 +00004241 lock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00004242 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00004243 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00004244 }
4245 break;
4246 case TARGET_NR_getsid:
4247 ret = get_errno(getsid(arg1));
4248 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004249#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00004250 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00004251 ret = get_errno(fdatasync(arg1));
4252 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004253#endif
bellard31e31b82003-02-18 22:55:36 +00004254 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00004255 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00004256 return value. */
ths0da46a62007-10-20 20:23:07 +00004257 ret = -TARGET_ENOTDIR;
4258 break;
bellard31e31b82003-02-18 22:55:36 +00004259 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00004260 {
pbrook53a59602006-03-25 19:31:22 +00004261 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004262 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00004263
4264 lock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00004265 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004266 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00004267 ret = get_errno(sched_setparam(arg1, &schp));
4268 }
4269 break;
bellard31e31b82003-02-18 22:55:36 +00004270 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00004271 {
pbrook53a59602006-03-25 19:31:22 +00004272 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004273 struct sched_param schp;
4274 ret = get_errno(sched_getparam(arg1, &schp));
4275 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004276 lock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00004277 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004278 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00004279 }
4280 }
4281 break;
bellard31e31b82003-02-18 22:55:36 +00004282 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00004283 {
pbrook53a59602006-03-25 19:31:22 +00004284 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004285 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00004286 lock_user_struct(target_schp, arg3, 1);
bellard5cd43932003-03-29 16:54:36 +00004287 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004288 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00004289 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
4290 }
4291 break;
bellard31e31b82003-02-18 22:55:36 +00004292 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00004293 ret = get_errno(sched_getscheduler(arg1));
4294 break;
bellard31e31b82003-02-18 22:55:36 +00004295 case TARGET_NR_sched_yield:
4296 ret = get_errno(sched_yield());
4297 break;
4298 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00004299 ret = get_errno(sched_get_priority_max(arg1));
4300 break;
bellard31e31b82003-02-18 22:55:36 +00004301 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00004302 ret = get_errno(sched_get_priority_min(arg1));
4303 break;
bellard31e31b82003-02-18 22:55:36 +00004304 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00004305 {
bellard5cd43932003-03-29 16:54:36 +00004306 struct timespec ts;
4307 ret = get_errno(sched_rr_get_interval(arg1, &ts));
4308 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004309 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00004310 }
4311 }
4312 break;
bellard31e31b82003-02-18 22:55:36 +00004313 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00004314 {
bellard1b6b0292003-03-22 17:31:38 +00004315 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00004316 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00004317 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00004318 if (is_error(ret) && arg2) {
4319 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00004320 }
4321 }
4322 break;
thse5febef2007-04-01 18:31:35 +00004323#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00004324 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00004325 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004326#endif
4327#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00004328 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00004329 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004330#endif
bellard31e31b82003-02-18 22:55:36 +00004331 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00004332 switch (arg1)
4333 {
4334 case PR_GET_PDEATHSIG:
4335 {
4336 int deathsig;
4337 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
4338 if (!is_error(ret) && arg2)
4339 tput32(arg2, deathsig);
4340 }
4341 break;
4342 default:
4343 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
4344 break;
4345 }
ths39b9aae2007-02-11 18:36:44 +00004346 break;
bellard67867302003-11-23 17:05:30 +00004347#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00004348 case TARGET_NR_pread:
pbrook53a59602006-03-25 19:31:22 +00004349 page_unprotect_range(arg2, arg3);
4350 p = lock_user(arg2, arg3, 0);
4351 ret = get_errno(pread(arg1, p, arg3, arg4));
4352 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00004353 break;
bellard31e31b82003-02-18 22:55:36 +00004354 case TARGET_NR_pwrite:
pbrook53a59602006-03-25 19:31:22 +00004355 p = lock_user(arg2, arg3, 1);
4356 ret = get_errno(pwrite(arg1, p, arg3, arg4));
4357 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00004358 break;
bellard67867302003-11-23 17:05:30 +00004359#endif
bellard31e31b82003-02-18 22:55:36 +00004360 case TARGET_NR_getcwd:
pbrook53a59602006-03-25 19:31:22 +00004361 p = lock_user(arg1, arg2, 0);
4362 ret = get_errno(sys_getcwd1(p, arg2));
4363 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00004364 break;
4365 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00004366 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004367 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00004368 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004369 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00004370#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
4371 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
thsa04e1342007-09-27 13:57:58 +00004372 ret = do_sigaltstack((struct target_sigaltstack *)arg1,
4373 (struct target_sigaltstack *)arg2,
4374 get_sp_from_cpustate((CPUState *)cpu_env));
4375 break;
4376#else
bellard5cd43932003-03-29 16:54:36 +00004377 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00004378#endif
bellard31e31b82003-02-18 22:55:36 +00004379 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00004380 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004381#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00004382 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00004383 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004384#endif
4385#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00004386 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00004387 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004388#endif
bellard048f6b42005-11-26 18:47:20 +00004389#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00004390 case TARGET_NR_vfork:
bellard1b6b0292003-03-22 17:31:38 +00004391 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00004392 break;
bellard048f6b42005-11-26 18:47:20 +00004393#endif
bellardebc05482003-09-30 21:08:41 +00004394#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00004395 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00004396 {
4397 struct rlimit rlim;
4398 ret = get_errno(getrlimit(arg1, &rlim));
4399 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004400 struct target_rlimit *target_rlim;
4401 lock_user_struct(target_rlim, arg2, 0);
bellard728584b2003-04-29 20:43:36 +00004402 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
4403 target_rlim->rlim_max = tswapl(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00004404 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00004405 }
4406 break;
4407 }
bellardebc05482003-09-30 21:08:41 +00004408#endif
bellarda315a142005-01-30 22:59:18 +00004409#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00004410 case TARGET_NR_truncate64:
pbrook53a59602006-03-25 19:31:22 +00004411 p = lock_user_string(arg1);
4412 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
4413 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00004414 break;
bellarda315a142005-01-30 22:59:18 +00004415#endif
4416#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00004417 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00004418 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00004419 break;
bellarda315a142005-01-30 22:59:18 +00004420#endif
4421#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00004422 case TARGET_NR_stat64:
pbrook53a59602006-03-25 19:31:22 +00004423 p = lock_user_string(arg1);
4424 ret = get_errno(stat(path(p), &st));
4425 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004426 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004427#endif
4428#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00004429 case TARGET_NR_lstat64:
pbrook53a59602006-03-25 19:31:22 +00004430 p = lock_user_string(arg1);
4431 ret = get_errno(lstat(path(p), &st));
4432 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004433 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004434#endif
4435#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00004436 case TARGET_NR_fstat64:
bellard60cd49d2003-03-16 22:53:56 +00004437 {
4438 ret = get_errno(fstat(arg1, &st));
4439 do_stat64:
4440 if (!is_error(ret)) {
pbrookce4defa2006-02-09 16:49:55 +00004441#ifdef TARGET_ARM
4442 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004443 struct target_eabi_stat64 *target_st;
4444 lock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00004445 memset(target_st, 0, sizeof(struct target_eabi_stat64));
pbrook53a59602006-03-25 19:31:22 +00004446 /* put_user is probably wrong. */
pbrookce4defa2006-02-09 16:49:55 +00004447 put_user(st.st_dev, &target_st->st_dev);
4448 put_user(st.st_ino, &target_st->st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004449#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
pbrookce4defa2006-02-09 16:49:55 +00004450 put_user(st.st_ino, &target_st->__st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004451#endif
pbrookce4defa2006-02-09 16:49:55 +00004452 put_user(st.st_mode, &target_st->st_mode);
4453 put_user(st.st_nlink, &target_st->st_nlink);
4454 put_user(st.st_uid, &target_st->st_uid);
4455 put_user(st.st_gid, &target_st->st_gid);
4456 put_user(st.st_rdev, &target_st->st_rdev);
4457 /* XXX: better use of kernel struct */
4458 put_user(st.st_size, &target_st->st_size);
4459 put_user(st.st_blksize, &target_st->st_blksize);
4460 put_user(st.st_blocks, &target_st->st_blocks);
4461 put_user(st.st_atime, &target_st->target_st_atime);
4462 put_user(st.st_mtime, &target_st->target_st_mtime);
4463 put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00004464 unlock_user_struct(target_st, arg2, 0);
pbrookce4defa2006-02-09 16:49:55 +00004465 } else
4466#endif
4467 {
pbrook53a59602006-03-25 19:31:22 +00004468 struct target_stat64 *target_st;
4469 lock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00004470 memset(target_st, 0, sizeof(struct target_stat64));
pbrook53a59602006-03-25 19:31:22 +00004471 /* ??? put_user is probably wrong. */
pbrookce4defa2006-02-09 16:49:55 +00004472 put_user(st.st_dev, &target_st->st_dev);
4473 put_user(st.st_ino, &target_st->st_ino);
4474#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4475 put_user(st.st_ino, &target_st->__st_ino);
4476#endif
4477 put_user(st.st_mode, &target_st->st_mode);
4478 put_user(st.st_nlink, &target_st->st_nlink);
4479 put_user(st.st_uid, &target_st->st_uid);
4480 put_user(st.st_gid, &target_st->st_gid);
4481 put_user(st.st_rdev, &target_st->st_rdev);
4482 /* XXX: better use of kernel struct */
4483 put_user(st.st_size, &target_st->st_size);
4484 put_user(st.st_blksize, &target_st->st_blksize);
4485 put_user(st.st_blocks, &target_st->st_blocks);
4486 put_user(st.st_atime, &target_st->target_st_atime);
4487 put_user(st.st_mtime, &target_st->target_st_mtime);
4488 put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00004489 unlock_user_struct(target_st, arg2, 0);
pbrookce4defa2006-02-09 16:49:55 +00004490 }
bellard60cd49d2003-03-16 22:53:56 +00004491 }
4492 }
4493 break;
bellarda315a142005-01-30 22:59:18 +00004494#endif
bellard67867302003-11-23 17:05:30 +00004495#ifdef USE_UID16
4496 case TARGET_NR_lchown:
pbrook53a59602006-03-25 19:31:22 +00004497 p = lock_user_string(arg1);
4498 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
4499 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00004500 break;
4501 case TARGET_NR_getuid:
4502 ret = get_errno(high2lowuid(getuid()));
4503 break;
4504 case TARGET_NR_getgid:
4505 ret = get_errno(high2lowgid(getgid()));
4506 break;
4507 case TARGET_NR_geteuid:
4508 ret = get_errno(high2lowuid(geteuid()));
4509 break;
4510 case TARGET_NR_getegid:
4511 ret = get_errno(high2lowgid(getegid()));
4512 break;
4513 case TARGET_NR_setreuid:
4514 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
4515 break;
4516 case TARGET_NR_setregid:
4517 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
4518 break;
4519 case TARGET_NR_getgroups:
4520 {
4521 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004522 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00004523 gid_t *grouplist;
4524 int i;
4525
4526 grouplist = alloca(gidsetsize * sizeof(gid_t));
4527 ret = get_errno(getgroups(gidsetsize, grouplist));
4528 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004529 target_grouplist = lock_user(arg2, gidsetsize * 2, 0);
bellard67867302003-11-23 17:05:30 +00004530 for(i = 0;i < gidsetsize; i++)
4531 target_grouplist[i] = tswap16(grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00004532 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00004533 }
4534 }
4535 break;
4536 case TARGET_NR_setgroups:
4537 {
4538 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004539 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00004540 gid_t *grouplist;
4541 int i;
4542
4543 grouplist = alloca(gidsetsize * sizeof(gid_t));
pbrook53a59602006-03-25 19:31:22 +00004544 target_grouplist = lock_user(arg2, gidsetsize * 2, 1);
bellard67867302003-11-23 17:05:30 +00004545 for(i = 0;i < gidsetsize; i++)
4546 grouplist[i] = tswap16(target_grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00004547 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00004548 ret = get_errno(setgroups(gidsetsize, grouplist));
4549 }
4550 break;
4551 case TARGET_NR_fchown:
4552 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
4553 break;
thsccfa72b2007-09-24 09:23:34 +00004554#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
4555 case TARGET_NR_fchownat:
4556 if (!arg2) {
ths0da46a62007-10-20 20:23:07 +00004557 ret = -TARGET_EFAULT;
thsccfa72b2007-09-24 09:23:34 +00004558 goto fail;
4559 }
4560 p = lock_user_string(arg2);
4561 if (!access_ok(VERIFY_READ, p, 1))
ths0da46a62007-10-20 20:23:07 +00004562 /* Don't "goto fail" so that cleanup can happen. */
4563 ret = -TARGET_EFAULT;
thsccfa72b2007-09-24 09:23:34 +00004564 else
4565 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
4566 if (p)
4567 unlock_user(p, arg2, 0);
4568 break;
4569#endif
bellard67867302003-11-23 17:05:30 +00004570#ifdef TARGET_NR_setresuid
4571 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00004572 ret = get_errno(setresuid(low2highuid(arg1),
4573 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00004574 low2highuid(arg3)));
4575 break;
4576#endif
4577#ifdef TARGET_NR_getresuid
4578 case TARGET_NR_getresuid:
4579 {
pbrook53a59602006-03-25 19:31:22 +00004580 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00004581 ret = get_errno(getresuid(&ruid, &euid, &suid));
4582 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004583 tput16(arg1, tswap16(high2lowuid(ruid)));
4584 tput16(arg2, tswap16(high2lowuid(euid)));
4585 tput16(arg3, tswap16(high2lowuid(suid)));
bellard67867302003-11-23 17:05:30 +00004586 }
4587 }
4588 break;
4589#endif
4590#ifdef TARGET_NR_getresgid
4591 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00004592 ret = get_errno(setresgid(low2highgid(arg1),
4593 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00004594 low2highgid(arg3)));
4595 break;
4596#endif
4597#ifdef TARGET_NR_getresgid
4598 case TARGET_NR_getresgid:
4599 {
pbrook53a59602006-03-25 19:31:22 +00004600 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00004601 ret = get_errno(getresgid(&rgid, &egid, &sgid));
4602 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004603 tput16(arg1, tswap16(high2lowgid(rgid)));
4604 tput16(arg2, tswap16(high2lowgid(egid)));
4605 tput16(arg3, tswap16(high2lowgid(sgid)));
bellard67867302003-11-23 17:05:30 +00004606 }
4607 }
4608 break;
4609#endif
4610 case TARGET_NR_chown:
pbrook53a59602006-03-25 19:31:22 +00004611 p = lock_user_string(arg1);
4612 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
4613 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00004614 break;
4615 case TARGET_NR_setuid:
4616 ret = get_errno(setuid(low2highuid(arg1)));
4617 break;
4618 case TARGET_NR_setgid:
4619 ret = get_errno(setgid(low2highgid(arg1)));
4620 break;
4621 case TARGET_NR_setfsuid:
4622 ret = get_errno(setfsuid(arg1));
4623 break;
4624 case TARGET_NR_setfsgid:
4625 ret = get_errno(setfsgid(arg1));
4626 break;
4627#endif /* USE_UID16 */
4628
bellarda315a142005-01-30 22:59:18 +00004629#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00004630 case TARGET_NR_lchown32:
pbrook53a59602006-03-25 19:31:22 +00004631 p = lock_user_string(arg1);
4632 ret = get_errno(lchown(p, arg2, arg3));
4633 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00004634 break;
bellarda315a142005-01-30 22:59:18 +00004635#endif
4636#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00004637 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00004638 ret = get_errno(getuid());
4639 break;
bellarda315a142005-01-30 22:59:18 +00004640#endif
4641#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00004642 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00004643 ret = get_errno(getgid());
4644 break;
bellarda315a142005-01-30 22:59:18 +00004645#endif
4646#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00004647 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00004648 ret = get_errno(geteuid());
4649 break;
bellarda315a142005-01-30 22:59:18 +00004650#endif
4651#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00004652 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00004653 ret = get_errno(getegid());
4654 break;
bellarda315a142005-01-30 22:59:18 +00004655#endif
4656#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00004657 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00004658 ret = get_errno(setreuid(arg1, arg2));
4659 break;
bellarda315a142005-01-30 22:59:18 +00004660#endif
4661#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00004662 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00004663 ret = get_errno(setregid(arg1, arg2));
4664 break;
bellarda315a142005-01-30 22:59:18 +00004665#endif
4666#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00004667 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00004668 {
4669 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004670 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00004671 gid_t *grouplist;
4672 int i;
4673
4674 grouplist = alloca(gidsetsize * sizeof(gid_t));
4675 ret = get_errno(getgroups(gidsetsize, grouplist));
4676 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004677 target_grouplist = lock_user(arg2, gidsetsize * 4, 0);
bellard99c475a2005-01-31 20:45:13 +00004678 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00004679 target_grouplist[i] = tswap32(grouplist[i]);
4680 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00004681 }
4682 }
4683 break;
bellarda315a142005-01-30 22:59:18 +00004684#endif
4685#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00004686 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00004687 {
4688 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004689 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00004690 gid_t *grouplist;
4691 int i;
ths3b46e622007-09-17 08:09:54 +00004692
bellard99c475a2005-01-31 20:45:13 +00004693 grouplist = alloca(gidsetsize * sizeof(gid_t));
pbrook53a59602006-03-25 19:31:22 +00004694 target_grouplist = lock_user(arg2, gidsetsize * 4, 1);
bellard99c475a2005-01-31 20:45:13 +00004695 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00004696 grouplist[i] = tswap32(target_grouplist[i]);
4697 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00004698 ret = get_errno(setgroups(gidsetsize, grouplist));
4699 }
4700 break;
bellarda315a142005-01-30 22:59:18 +00004701#endif
4702#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00004703 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00004704 ret = get_errno(fchown(arg1, arg2, arg3));
4705 break;
bellarda315a142005-01-30 22:59:18 +00004706#endif
4707#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00004708 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00004709 ret = get_errno(setresuid(arg1, arg2, arg3));
4710 break;
bellarda315a142005-01-30 22:59:18 +00004711#endif
4712#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00004713 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00004714 {
pbrook53a59602006-03-25 19:31:22 +00004715 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00004716 ret = get_errno(getresuid(&ruid, &euid, &suid));
4717 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004718 tput32(arg1, tswap32(ruid));
4719 tput32(arg2, tswap32(euid));
4720 tput32(arg3, tswap32(suid));
bellardb03c60f2003-03-23 17:19:56 +00004721 }
4722 }
4723 break;
bellarda315a142005-01-30 22:59:18 +00004724#endif
4725#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00004726 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00004727 ret = get_errno(setresgid(arg1, arg2, arg3));
4728 break;
bellarda315a142005-01-30 22:59:18 +00004729#endif
4730#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00004731 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00004732 {
pbrook53a59602006-03-25 19:31:22 +00004733 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00004734 ret = get_errno(getresgid(&rgid, &egid, &sgid));
4735 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004736 tput32(arg1, tswap32(rgid));
4737 tput32(arg2, tswap32(egid));
4738 tput32(arg3, tswap32(sgid));
bellardb03c60f2003-03-23 17:19:56 +00004739 }
4740 }
4741 break;
bellarda315a142005-01-30 22:59:18 +00004742#endif
4743#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00004744 case TARGET_NR_chown32:
pbrook53a59602006-03-25 19:31:22 +00004745 p = lock_user_string(arg1);
4746 ret = get_errno(chown(p, arg2, arg3));
4747 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00004748 break;
bellarda315a142005-01-30 22:59:18 +00004749#endif
4750#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00004751 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00004752 ret = get_errno(setuid(arg1));
4753 break;
bellarda315a142005-01-30 22:59:18 +00004754#endif
4755#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00004756 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00004757 ret = get_errno(setgid(arg1));
4758 break;
bellarda315a142005-01-30 22:59:18 +00004759#endif
4760#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00004761 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00004762 ret = get_errno(setfsuid(arg1));
4763 break;
bellarda315a142005-01-30 22:59:18 +00004764#endif
4765#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00004766 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00004767 ret = get_errno(setfsgid(arg1));
4768 break;
bellarda315a142005-01-30 22:59:18 +00004769#endif
bellard67867302003-11-23 17:05:30 +00004770
bellard31e31b82003-02-18 22:55:36 +00004771 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00004772 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00004773#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00004774 case TARGET_NR_mincore:
bellardb03c60f2003-03-23 17:19:56 +00004775 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00004776#endif
4777#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00004778 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00004779 /* A straight passthrough may not be safe because qemu sometimes
4780 turns private flie-backed mappings into anonymous mappings.
4781 This will break MADV_DONTNEED.
4782 This is a hint, so ignoring and returning success is ok. */
4783 ret = get_errno(0);
4784 break;
bellardffa65c32004-01-04 23:57:22 +00004785#endif
blueswir1992f48a2007-10-14 16:27:31 +00004786#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00004787 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00004788 {
thsb1e341e2007-03-20 21:50:52 +00004789 int cmd;
bellard77e46722003-04-29 20:39:06 +00004790 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00004791 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00004792#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00004793 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00004794#endif
bellard77e46722003-04-29 20:39:06 +00004795
thsb1e341e2007-03-20 21:50:52 +00004796 switch(arg2){
4797 case TARGET_F_GETLK64:
4798 cmd = F_GETLK64;
thsa7222582007-03-22 15:08:55 +00004799 break;
thsb1e341e2007-03-20 21:50:52 +00004800 case TARGET_F_SETLK64:
4801 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00004802 break;
thsb1e341e2007-03-20 21:50:52 +00004803 case TARGET_F_SETLKW64:
4804 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00004805 break;
thsb1e341e2007-03-20 21:50:52 +00004806 default:
4807 cmd = arg2;
thsa7222582007-03-22 15:08:55 +00004808 break;
thsb1e341e2007-03-20 21:50:52 +00004809 }
4810
bellard60cd49d2003-03-16 22:53:56 +00004811 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00004812 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00004813#ifdef TARGET_ARM
4814 if (((CPUARMState *)cpu_env)->eabi) {
4815 lock_user_struct(target_efl, arg3, 1);
4816 fl.l_type = tswap16(target_efl->l_type);
4817 fl.l_whence = tswap16(target_efl->l_whence);
4818 fl.l_start = tswap64(target_efl->l_start);
4819 fl.l_len = tswap64(target_efl->l_len);
4820 fl.l_pid = tswapl(target_efl->l_pid);
4821 unlock_user_struct(target_efl, arg3, 0);
4822 } else
4823#endif
4824 {
4825 lock_user_struct(target_fl, arg3, 1);
4826 fl.l_type = tswap16(target_fl->l_type);
4827 fl.l_whence = tswap16(target_fl->l_whence);
4828 fl.l_start = tswap64(target_fl->l_start);
4829 fl.l_len = tswap64(target_fl->l_len);
4830 fl.l_pid = tswapl(target_fl->l_pid);
4831 unlock_user_struct(target_fl, arg3, 0);
4832 }
thsb1e341e2007-03-20 21:50:52 +00004833 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00004834 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00004835#ifdef TARGET_ARM
4836 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004837 lock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004838 target_efl->l_type = tswap16(fl.l_type);
4839 target_efl->l_whence = tswap16(fl.l_whence);
4840 target_efl->l_start = tswap64(fl.l_start);
4841 target_efl->l_len = tswap64(fl.l_len);
4842 target_efl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004843 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004844 } else
4845#endif
4846 {
pbrook53a59602006-03-25 19:31:22 +00004847 lock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004848 target_fl->l_type = tswap16(fl.l_type);
4849 target_fl->l_whence = tswap16(fl.l_whence);
4850 target_fl->l_start = tswap64(fl.l_start);
4851 target_fl->l_len = tswap64(fl.l_len);
4852 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004853 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004854 }
bellard77e46722003-04-29 20:39:06 +00004855 }
4856 break;
4857
thsb1e341e2007-03-20 21:50:52 +00004858 case TARGET_F_SETLK64:
4859 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00004860#ifdef TARGET_ARM
4861 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004862 lock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004863 fl.l_type = tswap16(target_efl->l_type);
4864 fl.l_whence = tswap16(target_efl->l_whence);
4865 fl.l_start = tswap64(target_efl->l_start);
4866 fl.l_len = tswap64(target_efl->l_len);
4867 fl.l_pid = tswapl(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004868 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004869 } else
4870#endif
4871 {
pbrook53a59602006-03-25 19:31:22 +00004872 lock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004873 fl.l_type = tswap16(target_fl->l_type);
4874 fl.l_whence = tswap16(target_fl->l_whence);
4875 fl.l_start = tswap64(target_fl->l_start);
4876 fl.l_len = tswap64(target_fl->l_len);
4877 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004878 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004879 }
thsb1e341e2007-03-20 21:50:52 +00004880 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00004881 break;
bellard60cd49d2003-03-16 22:53:56 +00004882 default:
thsb1e341e2007-03-20 21:50:52 +00004883 ret = get_errno(do_fcntl(arg1, cmd, arg3));
bellard60cd49d2003-03-16 22:53:56 +00004884 break;
4885 }
bellard77e46722003-04-29 20:39:06 +00004886 break;
4887 }
bellard60cd49d2003-03-16 22:53:56 +00004888#endif
ths7d600c82006-12-08 01:32:58 +00004889#ifdef TARGET_NR_cacheflush
4890 case TARGET_NR_cacheflush:
4891 /* self-modifying code is handled automatically, so nothing needed */
4892 ret = 0;
4893 break;
4894#endif
bellardebc05482003-09-30 21:08:41 +00004895#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00004896 case TARGET_NR_security:
4897 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004898#endif
bellardc573ff62004-01-04 15:51:36 +00004899#ifdef TARGET_NR_getpagesize
4900 case TARGET_NR_getpagesize:
4901 ret = TARGET_PAGE_SIZE;
4902 break;
4903#endif
bellard31e31b82003-02-18 22:55:36 +00004904 case TARGET_NR_gettid:
4905 ret = get_errno(gettid());
4906 break;
thse5febef2007-04-01 18:31:35 +00004907#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00004908 case TARGET_NR_readahead:
bellard5cd43932003-03-29 16:54:36 +00004909 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004910#endif
bellardebc05482003-09-30 21:08:41 +00004911#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00004912 case TARGET_NR_setxattr:
4913 case TARGET_NR_lsetxattr:
4914 case TARGET_NR_fsetxattr:
4915 case TARGET_NR_getxattr:
4916 case TARGET_NR_lgetxattr:
4917 case TARGET_NR_fgetxattr:
4918 case TARGET_NR_listxattr:
4919 case TARGET_NR_llistxattr:
4920 case TARGET_NR_flistxattr:
4921 case TARGET_NR_removexattr:
4922 case TARGET_NR_lremovexattr:
4923 case TARGET_NR_fremovexattr:
bellard5cd43932003-03-29 16:54:36 +00004924 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00004925#endif
4926#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00004927 case TARGET_NR_set_thread_area:
ths6f5b89a2007-03-02 20:48:00 +00004928#ifdef TARGET_MIPS
4929 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
4930 ret = 0;
4931 break;
4932#else
4933 goto unimplemented_nowarn;
4934#endif
4935#endif
4936#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00004937 case TARGET_NR_get_thread_area:
4938 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00004939#endif
bellard48dc41e2006-06-21 18:15:50 +00004940#ifdef TARGET_NR_getdomainname
4941 case TARGET_NR_getdomainname:
4942 goto unimplemented_nowarn;
4943#endif
ths6f5b89a2007-03-02 20:48:00 +00004944
thsb5906f92007-03-19 13:32:45 +00004945#ifdef TARGET_NR_clock_gettime
4946 case TARGET_NR_clock_gettime:
4947 {
4948 struct timespec ts;
4949 ret = get_errno(clock_gettime(arg1, &ts));
4950 if (!is_error(ret)) {
4951 host_to_target_timespec(arg2, &ts);
4952 }
4953 break;
4954 }
4955#endif
4956#ifdef TARGET_NR_clock_getres
4957 case TARGET_NR_clock_getres:
4958 {
4959 struct timespec ts;
4960 ret = get_errno(clock_getres(arg1, &ts));
4961 if (!is_error(ret)) {
4962 host_to_target_timespec(arg2, &ts);
4963 }
4964 break;
4965 }
4966#endif
4967
ths6f5b89a2007-03-02 20:48:00 +00004968#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
4969 case TARGET_NR_set_tid_address:
4970 ret = get_errno(set_tid_address((int *) arg1));
4971 break;
4972#endif
4973
ths3ae43202007-09-16 21:39:48 +00004974#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00004975 case TARGET_NR_tkill:
4976 ret = get_errno(sys_tkill((int)arg1, (int)arg2));
4977 break;
4978#endif
4979
ths3ae43202007-09-16 21:39:48 +00004980#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00004981 case TARGET_NR_tgkill:
4982 ret = get_errno(sys_tgkill((int)arg1, (int)arg2, (int)arg3));
4983 break;
4984#endif
4985
ths4f2b1fe2007-06-21 21:57:12 +00004986#ifdef TARGET_NR_set_robust_list
4987 case TARGET_NR_set_robust_list:
4988 goto unimplemented_nowarn;
4989#endif
4990
ths9007f0e2007-09-25 17:50:37 +00004991#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
4992 case TARGET_NR_utimensat:
4993 {
4994 struct timespec ts[2];
4995 target_to_host_timespec(ts, arg3);
4996 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
4997 if (!arg2)
4998 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
4999 else {
5000 p = lock_user_string(arg2);
5001 if (!access_ok(VERIFY_READ, p, 1))
ths0da46a62007-10-20 20:23:07 +00005002 /* Don't "goto fail" so that cleanup can happen. */
5003 ret = -TARGET_EFAULT;
ths9007f0e2007-09-25 17:50:37 +00005004 else
5005 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
5006 if (p)
5007 unlock_user(p, arg2, 0);
5008 }
5009 }
5010 break;
5011#endif
5012
bellard31e31b82003-02-18 22:55:36 +00005013 default:
5014 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00005015 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00005016#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 +00005017 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00005018#endif
ths0da46a62007-10-20 20:23:07 +00005019 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00005020 break;
5021 }
5022 fail:
bellardc573ff62004-01-04 15:51:36 +00005023#ifdef DEBUG
5024 gemu_log(" = %ld\n", ret);
5025#endif
bellard31e31b82003-02-18 22:55:36 +00005026 return ret;
5027}