blob: e4ea31349c641ce634c9ebe5951044cce5b7f7a8 [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
170static int gettid(void) {
171 return -ENOSYS;
172}
173#endif
bellard31e31b82003-02-18 22:55:36 +0000174_syscall1(int,sys_uname,struct new_utsname *,buf)
ths92a34c12007-09-24 09:27:49 +0000175#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
176_syscall4(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode,int,flags)
177#endif
ths814d7972007-09-24 09:26:51 +0000178#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
179_syscall4(int,sys_fchmodat,int,dirfd,const char *,pathname,
180 mode_t,mode,int,flags)
181#endif
thsccfa72b2007-09-24 09:23:34 +0000182#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
183_syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
184 uid_t,owner,gid_t,group,int,flags)
185#endif
bellard72f03902003-02-18 23:33:18 +0000186_syscall2(int,sys_getcwd1,char *,buf,size_t,size)
187_syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
ths3ae43202007-09-16 21:39:48 +0000188#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +0000189_syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
ths3ae43202007-09-16 21:39:48 +0000190#endif
thsc6cda172007-10-09 03:42:34 +0000191_syscall2(int, sys_getpriority, int, which, int, who);
bellard31e31b82003-02-18 22:55:36 +0000192_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
193 loff_t *, res, uint, wh);
ths64f0ce42007-09-24 09:25:06 +0000194#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
195_syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
196 int,newdirfd,const char *,newpath,int,flags)
197#endif
ths4472ad02007-09-24 09:22:32 +0000198#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
199_syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
200#endif
ths75ac37a2007-09-24 09:23:05 +0000201#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
202_syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
203 mode_t,mode,dev_t,dev)
204#endif
ths82424832007-09-24 09:21:55 +0000205#if defined(TARGET_NR_openat) && defined(__NR_openat)
206_syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
207#endif
ths5e0ccb12007-09-24 09:26:10 +0000208#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
209_syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
210 char *,buf,size_t,bufsize)
211#endif
ths722183f2007-09-24 09:24:37 +0000212#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
213_syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
214 int,newdirfd,const char *,newpath)
215#endif
bellard66fb9762003-03-23 01:06:05 +0000216_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
thsb51eaa82007-09-25 16:09:22 +0000217#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +0000218_syscall3(int,sys_symlinkat,const char *,oldpath,
219 int,newdirfd,const char *,newpath)
220#endif
ths7494b0f2007-02-11 18:26:53 +0000221_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
ths3ae43202007-09-16 21:39:48 +0000222#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +0000223_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
ths4cae1d12007-07-12 11:06:53 +0000224#endif
ths3ae43202007-09-16 21:39:48 +0000225#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +0000226_syscall2(int,sys_tkill,int,tid,int,sig)
227#endif
bellardec86b0f2003-04-11 00:15:04 +0000228#ifdef __NR_exit_group
229_syscall1(int,exit_group,int,error_code)
230#endif
ths6f5b89a2007-03-02 20:48:00 +0000231#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
232_syscall1(int,set_tid_address,int *,tidptr)
233#endif
ths8170f562007-09-24 09:24:11 +0000234#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
235_syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
236#endif
ths9007f0e2007-09-25 17:50:37 +0000237#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
238_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
239 const struct timespec *,tsp,int,flags)
240#endif
bellard66fb9762003-03-23 01:06:05 +0000241
242extern int personality(int);
bellard9de5e442003-03-23 16:49:39 +0000243extern int flock(int, int);
244extern int setfsuid(int);
245extern int setfsgid(int);
bellard5cd43932003-03-29 16:54:36 +0000246extern int setresuid(uid_t, uid_t, uid_t);
247extern int getresuid(uid_t *, uid_t *, uid_t *);
248extern int setresgid(gid_t, gid_t, gid_t);
249extern int getresgid(gid_t *, gid_t *, gid_t *);
bellard19b84f32003-05-08 15:41:49 +0000250extern int setgroups(int, gid_t *);
bellard31e31b82003-02-18 22:55:36 +0000251
ths637947f2007-06-01 12:09:19 +0000252/*
thsfe8f0962007-07-12 10:59:21 +0000253 * This list is the union of errno values overridden in asm-<arch>/errno.h
ths637947f2007-06-01 12:09:19 +0000254 * minus the errnos that are not actually generic to all archs.
255 */
256static uint16_t host_to_target_errno_table[1200] = {
257 [EIDRM] = TARGET_EIDRM,
258 [ECHRNG] = TARGET_ECHRNG,
259 [EL2NSYNC] = TARGET_EL2NSYNC,
260 [EL3HLT] = TARGET_EL3HLT,
261 [EL3RST] = TARGET_EL3RST,
262 [ELNRNG] = TARGET_ELNRNG,
263 [EUNATCH] = TARGET_EUNATCH,
264 [ENOCSI] = TARGET_ENOCSI,
265 [EL2HLT] = TARGET_EL2HLT,
266 [EDEADLK] = TARGET_EDEADLK,
267 [ENOLCK] = TARGET_ENOLCK,
268 [EBADE] = TARGET_EBADE,
269 [EBADR] = TARGET_EBADR,
270 [EXFULL] = TARGET_EXFULL,
271 [ENOANO] = TARGET_ENOANO,
272 [EBADRQC] = TARGET_EBADRQC,
273 [EBADSLT] = TARGET_EBADSLT,
274 [EBFONT] = TARGET_EBFONT,
275 [ENOSTR] = TARGET_ENOSTR,
276 [ENODATA] = TARGET_ENODATA,
277 [ETIME] = TARGET_ETIME,
278 [ENOSR] = TARGET_ENOSR,
279 [ENONET] = TARGET_ENONET,
280 [ENOPKG] = TARGET_ENOPKG,
281 [EREMOTE] = TARGET_EREMOTE,
282 [ENOLINK] = TARGET_ENOLINK,
283 [EADV] = TARGET_EADV,
284 [ESRMNT] = TARGET_ESRMNT,
285 [ECOMM] = TARGET_ECOMM,
286 [EPROTO] = TARGET_EPROTO,
287 [EDOTDOT] = TARGET_EDOTDOT,
288 [EMULTIHOP] = TARGET_EMULTIHOP,
289 [EBADMSG] = TARGET_EBADMSG,
290 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
291 [EOVERFLOW] = TARGET_EOVERFLOW,
292 [ENOTUNIQ] = TARGET_ENOTUNIQ,
293 [EBADFD] = TARGET_EBADFD,
294 [EREMCHG] = TARGET_EREMCHG,
295 [ELIBACC] = TARGET_ELIBACC,
296 [ELIBBAD] = TARGET_ELIBBAD,
297 [ELIBSCN] = TARGET_ELIBSCN,
298 [ELIBMAX] = TARGET_ELIBMAX,
299 [ELIBEXEC] = TARGET_ELIBEXEC,
300 [EILSEQ] = TARGET_EILSEQ,
301 [ENOSYS] = TARGET_ENOSYS,
302 [ELOOP] = TARGET_ELOOP,
303 [ERESTART] = TARGET_ERESTART,
304 [ESTRPIPE] = TARGET_ESTRPIPE,
305 [ENOTEMPTY] = TARGET_ENOTEMPTY,
306 [EUSERS] = TARGET_EUSERS,
307 [ENOTSOCK] = TARGET_ENOTSOCK,
308 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
309 [EMSGSIZE] = TARGET_EMSGSIZE,
310 [EPROTOTYPE] = TARGET_EPROTOTYPE,
311 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
312 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
313 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
314 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
315 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
316 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
317 [EADDRINUSE] = TARGET_EADDRINUSE,
318 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
319 [ENETDOWN] = TARGET_ENETDOWN,
320 [ENETUNREACH] = TARGET_ENETUNREACH,
321 [ENETRESET] = TARGET_ENETRESET,
322 [ECONNABORTED] = TARGET_ECONNABORTED,
323 [ECONNRESET] = TARGET_ECONNRESET,
324 [ENOBUFS] = TARGET_ENOBUFS,
325 [EISCONN] = TARGET_EISCONN,
326 [ENOTCONN] = TARGET_ENOTCONN,
327 [EUCLEAN] = TARGET_EUCLEAN,
328 [ENOTNAM] = TARGET_ENOTNAM,
329 [ENAVAIL] = TARGET_ENAVAIL,
330 [EISNAM] = TARGET_EISNAM,
331 [EREMOTEIO] = TARGET_EREMOTEIO,
332 [ESHUTDOWN] = TARGET_ESHUTDOWN,
333 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
334 [ETIMEDOUT] = TARGET_ETIMEDOUT,
335 [ECONNREFUSED] = TARGET_ECONNREFUSED,
336 [EHOSTDOWN] = TARGET_EHOSTDOWN,
337 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
338 [EALREADY] = TARGET_EALREADY,
339 [EINPROGRESS] = TARGET_EINPROGRESS,
340 [ESTALE] = TARGET_ESTALE,
341 [ECANCELED] = TARGET_ECANCELED,
342 [ENOMEDIUM] = TARGET_ENOMEDIUM,
343 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
thsb7fe5db2007-07-23 15:37:46 +0000344#ifdef ENOKEY
ths637947f2007-06-01 12:09:19 +0000345 [ENOKEY] = TARGET_ENOKEY,
thsb7fe5db2007-07-23 15:37:46 +0000346#endif
347#ifdef EKEYEXPIRED
ths637947f2007-06-01 12:09:19 +0000348 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
thsb7fe5db2007-07-23 15:37:46 +0000349#endif
350#ifdef EKEYREVOKED
ths637947f2007-06-01 12:09:19 +0000351 [EKEYREVOKED] = TARGET_EKEYREVOKED,
thsb7fe5db2007-07-23 15:37:46 +0000352#endif
353#ifdef EKEYREJECTED
ths637947f2007-06-01 12:09:19 +0000354 [EKEYREJECTED] = TARGET_EKEYREJECTED,
thsb7fe5db2007-07-23 15:37:46 +0000355#endif
356#ifdef EOWNERDEAD
ths637947f2007-06-01 12:09:19 +0000357 [EOWNERDEAD] = TARGET_EOWNERDEAD,
thsb7fe5db2007-07-23 15:37:46 +0000358#endif
359#ifdef ENOTRECOVERABLE
ths637947f2007-06-01 12:09:19 +0000360 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
thsb7fe5db2007-07-23 15:37:46 +0000361#endif
ths637947f2007-06-01 12:09:19 +0000362 };
363
364static inline int host_to_target_errno(int err)
365{
366 if(host_to_target_errno_table[err])
367 return host_to_target_errno_table[err];
368 return err;
369}
370
blueswir1992f48a2007-10-14 16:27:31 +0000371static inline abi_long get_errno(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000372{
373 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000374 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000375 else
376 return ret;
377}
378
blueswir1992f48a2007-10-14 16:27:31 +0000379static inline int is_error(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000380{
blueswir1992f48a2007-10-14 16:27:31 +0000381 return (abi_ulong)ret >= (abi_ulong)(-4096);
bellard31e31b82003-02-18 22:55:36 +0000382}
383
blueswir1992f48a2007-10-14 16:27:31 +0000384static abi_ulong target_brk;
385static abi_ulong target_original_brk;
bellard31e31b82003-02-18 22:55:36 +0000386
blueswir1992f48a2007-10-14 16:27:31 +0000387void target_set_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000388{
blueswir14c1de732007-07-07 20:45:44 +0000389 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
bellard31e31b82003-02-18 22:55:36 +0000390}
391
blueswir1992f48a2007-10-14 16:27:31 +0000392abi_long do_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000393{
blueswir1992f48a2007-10-14 16:27:31 +0000394 abi_ulong brk_page;
395 abi_long mapped_addr;
bellard31e31b82003-02-18 22:55:36 +0000396 int new_alloc_size;
397
398 if (!new_brk)
pbrook53a59602006-03-25 19:31:22 +0000399 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000400 if (new_brk < target_original_brk)
401 return -ENOMEM;
ths3b46e622007-09-17 08:09:54 +0000402
pbrook53a59602006-03-25 19:31:22 +0000403 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000404
405 /* If the new brk is less than this, set it and we're done... */
406 if (new_brk < brk_page) {
407 target_brk = new_brk;
pbrook53a59602006-03-25 19:31:22 +0000408 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000409 }
410
411 /* We need to allocate more memory after the brk... */
bellard54936002003-05-13 00:25:15 +0000412 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
ths5fafdf22007-09-16 21:08:06 +0000413 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000414 PROT_READ|PROT_WRITE,
415 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
bellard31e31b82003-02-18 22:55:36 +0000416 if (is_error(mapped_addr)) {
417 return mapped_addr;
418 } else {
419 target_brk = new_brk;
pbrook53a59602006-03-25 19:31:22 +0000420 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000421 }
422}
423
ths5fafdf22007-09-16 21:08:06 +0000424static inline fd_set *target_to_host_fds(fd_set *fds,
blueswir1992f48a2007-10-14 16:27:31 +0000425 abi_long *target_fds, int n)
bellard31e31b82003-02-18 22:55:36 +0000426{
bellard7854b052003-03-29 17:22:23 +0000427#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
bellard31e31b82003-02-18 22:55:36 +0000428 return (fd_set *)target_fds;
429#else
430 int i, b;
431 if (target_fds) {
432 FD_ZERO(fds);
433 for(i = 0;i < n; i++) {
blueswir1992f48a2007-10-14 16:27:31 +0000434 b = (tswapl(target_fds[i / TARGET_ABI_BITS]) >>
435 (i & (TARGET_ABI_BITS - 1))) & 1;
bellard31e31b82003-02-18 22:55:36 +0000436 if (b)
437 FD_SET(i, fds);
438 }
439 return fds;
440 } else {
441 return NULL;
442 }
443#endif
444}
445
blueswir1992f48a2007-10-14 16:27:31 +0000446static inline void host_to_target_fds(abi_long *target_fds,
bellard31e31b82003-02-18 22:55:36 +0000447 fd_set *fds, int n)
448{
bellard7854b052003-03-29 17:22:23 +0000449#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
bellard31e31b82003-02-18 22:55:36 +0000450 /* nothing to do */
451#else
452 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000453 abi_long v;
bellard31e31b82003-02-18 22:55:36 +0000454
455 if (target_fds) {
blueswir1992f48a2007-10-14 16:27:31 +0000456 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
bellard31e31b82003-02-18 22:55:36 +0000457 k = 0;
458 for(i = 0;i < nw; i++) {
459 v = 0;
blueswir1992f48a2007-10-14 16:27:31 +0000460 for(j = 0; j < TARGET_ABI_BITS; j++) {
bellard31e31b82003-02-18 22:55:36 +0000461 v |= ((FD_ISSET(k, fds) != 0) << j);
462 k++;
463 }
464 target_fds[i] = tswapl(v);
465 }
466 }
467#endif
468}
469
bellardc596ed12003-07-13 17:32:31 +0000470#if defined(__alpha__)
471#define HOST_HZ 1024
472#else
473#define HOST_HZ 100
474#endif
475
blueswir1992f48a2007-10-14 16:27:31 +0000476static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000477{
478#if HOST_HZ == TARGET_HZ
479 return ticks;
480#else
481 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
482#endif
483}
484
blueswir1992f48a2007-10-14 16:27:31 +0000485static inline void host_to_target_rusage(abi_ulong target_addr,
bellardb4091862003-05-16 15:39:34 +0000486 const struct rusage *rusage)
487{
pbrook53a59602006-03-25 19:31:22 +0000488 struct target_rusage *target_rusage;
489
490 lock_user_struct(target_rusage, target_addr, 0);
bellardb4091862003-05-16 15:39:34 +0000491 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
492 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
493 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
494 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
495 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
496 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
497 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
498 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
499 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
500 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
501 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
502 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
503 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
504 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
505 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
506 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
507 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
508 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000509 unlock_user_struct(target_rusage, target_addr, 1);
bellardb4091862003-05-16 15:39:34 +0000510}
511
pbrook53a59602006-03-25 19:31:22 +0000512static inline void target_to_host_timeval(struct timeval *tv,
blueswir1992f48a2007-10-14 16:27:31 +0000513 abi_ulong target_addr)
bellard31e31b82003-02-18 22:55:36 +0000514{
pbrook53a59602006-03-25 19:31:22 +0000515 struct target_timeval *target_tv;
516
517 lock_user_struct(target_tv, target_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000518 tv->tv_sec = tswapl(target_tv->tv_sec);
519 tv->tv_usec = tswapl(target_tv->tv_usec);
pbrook53a59602006-03-25 19:31:22 +0000520 unlock_user_struct(target_tv, target_addr, 0);
bellard31e31b82003-02-18 22:55:36 +0000521}
522
blueswir1992f48a2007-10-14 16:27:31 +0000523static inline void host_to_target_timeval(abi_ulong target_addr,
bellard5cd43932003-03-29 16:54:36 +0000524 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +0000525{
pbrook53a59602006-03-25 19:31:22 +0000526 struct target_timeval *target_tv;
527
528 lock_user_struct(target_tv, target_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000529 target_tv->tv_sec = tswapl(tv->tv_sec);
530 target_tv->tv_usec = tswapl(tv->tv_usec);
pbrook53a59602006-03-25 19:31:22 +0000531 unlock_user_struct(target_tv, target_addr, 1);
bellard31e31b82003-02-18 22:55:36 +0000532}
533
534
blueswir1992f48a2007-10-14 16:27:31 +0000535static abi_long do_select(int n,
536 abi_ulong rfd_p, abi_ulong wfd_p,
537 abi_ulong efd_p, abi_ulong target_tv)
bellard31e31b82003-02-18 22:55:36 +0000538{
539 fd_set rfds, wfds, efds;
540 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
blueswir1992f48a2007-10-14 16:27:31 +0000541 abi_long *target_rfds, *target_wfds, *target_efds;
bellard31e31b82003-02-18 22:55:36 +0000542 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +0000543 abi_long ret;
pbrook53a59602006-03-25 19:31:22 +0000544 int ok;
bellard31e31b82003-02-18 22:55:36 +0000545
pbrook53a59602006-03-25 19:31:22 +0000546 if (rfd_p) {
blueswir1992f48a2007-10-14 16:27:31 +0000547 target_rfds = lock_user(rfd_p, sizeof(abi_long) * n, 1);
pbrook53a59602006-03-25 19:31:22 +0000548 rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
549 } else {
550 target_rfds = NULL;
551 rfds_ptr = NULL;
552 }
553 if (wfd_p) {
blueswir1992f48a2007-10-14 16:27:31 +0000554 target_wfds = lock_user(wfd_p, sizeof(abi_long) * n, 1);
pbrook53a59602006-03-25 19:31:22 +0000555 wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
556 } else {
557 target_wfds = NULL;
558 wfds_ptr = NULL;
559 }
560 if (efd_p) {
blueswir1992f48a2007-10-14 16:27:31 +0000561 target_efds = lock_user(efd_p, sizeof(abi_long) * n, 1);
pbrook53a59602006-03-25 19:31:22 +0000562 efds_ptr = target_to_host_fds(&efds, target_efds, n);
563 } else {
564 target_efds = NULL;
565 efds_ptr = NULL;
566 }
ths3b46e622007-09-17 08:09:54 +0000567
bellard31e31b82003-02-18 22:55:36 +0000568 if (target_tv) {
bellard5cd43932003-03-29 16:54:36 +0000569 target_to_host_timeval(&tv, target_tv);
bellard31e31b82003-02-18 22:55:36 +0000570 tv_ptr = &tv;
571 } else {
572 tv_ptr = NULL;
573 }
574 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +0000575 ok = !is_error(ret);
576
577 if (ok) {
bellard31e31b82003-02-18 22:55:36 +0000578 host_to_target_fds(target_rfds, rfds_ptr, n);
579 host_to_target_fds(target_wfds, wfds_ptr, n);
580 host_to_target_fds(target_efds, efds_ptr, n);
581
582 if (target_tv) {
bellard5cd43932003-03-29 16:54:36 +0000583 host_to_target_timeval(target_tv, &tv);
bellard31e31b82003-02-18 22:55:36 +0000584 }
585 }
pbrook53a59602006-03-25 19:31:22 +0000586 if (target_rfds)
blueswir1992f48a2007-10-14 16:27:31 +0000587 unlock_user(target_rfds, rfd_p, ok ? sizeof(abi_long) * n : 0);
pbrook53a59602006-03-25 19:31:22 +0000588 if (target_wfds)
blueswir1992f48a2007-10-14 16:27:31 +0000589 unlock_user(target_wfds, wfd_p, ok ? sizeof(abi_long) * n : 0);
pbrook53a59602006-03-25 19:31:22 +0000590 if (target_efds)
blueswir1992f48a2007-10-14 16:27:31 +0000591 unlock_user(target_efds, efd_p, ok ? sizeof(abi_long) * n : 0);
pbrook53a59602006-03-25 19:31:22 +0000592
bellard31e31b82003-02-18 22:55:36 +0000593 return ret;
594}
595
bellard7854b052003-03-29 17:22:23 +0000596static inline void target_to_host_sockaddr(struct sockaddr *addr,
blueswir1992f48a2007-10-14 16:27:31 +0000597 abi_ulong target_addr,
bellard7854b052003-03-29 17:22:23 +0000598 socklen_t len)
599{
pbrook53a59602006-03-25 19:31:22 +0000600 struct target_sockaddr *target_saddr;
601
602 target_saddr = lock_user(target_addr, len, 1);
603 memcpy(addr, target_saddr, len);
604 addr->sa_family = tswap16(target_saddr->sa_family);
605 unlock_user(target_saddr, target_addr, 0);
bellard7854b052003-03-29 17:22:23 +0000606}
607
blueswir1992f48a2007-10-14 16:27:31 +0000608static inline void host_to_target_sockaddr(abi_ulong target_addr,
bellard7854b052003-03-29 17:22:23 +0000609 struct sockaddr *addr,
610 socklen_t len)
611{
pbrook53a59602006-03-25 19:31:22 +0000612 struct target_sockaddr *target_saddr;
613
614 target_saddr = lock_user(target_addr, len, 0);
615 memcpy(target_saddr, addr, len);
616 target_saddr->sa_family = tswap16(addr->sa_family);
617 unlock_user(target_saddr, target_addr, len);
bellard7854b052003-03-29 17:22:23 +0000618}
619
pbrook53a59602006-03-25 19:31:22 +0000620/* ??? Should this also swap msgh->name? */
bellard7854b052003-03-29 17:22:23 +0000621static inline void target_to_host_cmsg(struct msghdr *msgh,
622 struct target_msghdr *target_msgh)
623{
624 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
625 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
626 socklen_t space = 0;
627
628 while (cmsg && target_cmsg) {
629 void *data = CMSG_DATA(cmsg);
630 void *target_data = TARGET_CMSG_DATA(target_cmsg);
631
ths5fafdf22007-09-16 21:08:06 +0000632 int len = tswapl(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +0000633 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
634
635 space += CMSG_SPACE(len);
636 if (space > msgh->msg_controllen) {
637 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +0000638 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +0000639 break;
640 }
641
642 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
643 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
644 cmsg->cmsg_len = CMSG_LEN(len);
645
bellard3532fa72006-06-24 15:06:03 +0000646 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +0000647 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
648 memcpy(data, target_data, len);
649 } else {
650 int *fd = (int *)data;
651 int *target_fd = (int *)target_data;
652 int i, numfds = len / sizeof(int);
653
654 for (i = 0; i < numfds; i++)
655 fd[i] = tswap32(target_fd[i]);
656 }
657
658 cmsg = CMSG_NXTHDR(msgh, cmsg);
659 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
660 }
661
662 msgh->msg_controllen = space;
663}
664
pbrook53a59602006-03-25 19:31:22 +0000665/* ??? Should this also swap msgh->name? */
bellard7854b052003-03-29 17:22:23 +0000666static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
667 struct msghdr *msgh)
668{
669 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
670 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
671 socklen_t space = 0;
672
673 while (cmsg && target_cmsg) {
674 void *data = CMSG_DATA(cmsg);
675 void *target_data = TARGET_CMSG_DATA(target_cmsg);
676
677 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
678
679 space += TARGET_CMSG_SPACE(len);
680 if (space > tswapl(target_msgh->msg_controllen)) {
681 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +0000682 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +0000683 break;
684 }
685
686 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
687 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
688 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
689
bellard3532fa72006-06-24 15:06:03 +0000690 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +0000691 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
692 memcpy(target_data, data, len);
693 } else {
694 int *fd = (int *)data;
695 int *target_fd = (int *)target_data;
696 int i, numfds = len / sizeof(int);
697
698 for (i = 0; i < numfds; i++)
699 target_fd[i] = tswap32(fd[i]);
700 }
701
702 cmsg = CMSG_NXTHDR(msgh, cmsg);
703 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
704 }
705
706 msgh->msg_controllen = tswapl(space);
707}
708
blueswir1992f48a2007-10-14 16:27:31 +0000709static abi_long do_setsockopt(int sockfd, int level, int optname,
710 abi_ulong optval, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +0000711{
blueswir1992f48a2007-10-14 16:27:31 +0000712 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +0000713 int val;
ths3b46e622007-09-17 08:09:54 +0000714
bellard8853f862004-02-22 14:57:26 +0000715 switch(level) {
716 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +0000717 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +0000718 if (optlen < sizeof(uint32_t))
719 return -EINVAL;
ths3b46e622007-09-17 08:09:54 +0000720
pbrook53a59602006-03-25 19:31:22 +0000721 val = tget32(optval);
bellard8853f862004-02-22 14:57:26 +0000722 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
723 break;
724 case SOL_IP:
725 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +0000726 case IP_TOS:
727 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +0000728 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +0000729 case IP_ROUTER_ALERT:
730 case IP_RECVOPTS:
731 case IP_RETOPTS:
732 case IP_PKTINFO:
733 case IP_MTU_DISCOVER:
734 case IP_RECVERR:
735 case IP_RECVTOS:
736#ifdef IP_FREEBIND
737 case IP_FREEBIND:
738#endif
739 case IP_MULTICAST_TTL:
740 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +0000741 val = 0;
742 if (optlen >= sizeof(uint32_t)) {
pbrook53a59602006-03-25 19:31:22 +0000743 val = tget32(optval);
bellard8853f862004-02-22 14:57:26 +0000744 } else if (optlen >= 1) {
pbrook53a59602006-03-25 19:31:22 +0000745 val = tget8(optval);
bellard8853f862004-02-22 14:57:26 +0000746 }
747 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
748 break;
749 default:
750 goto unimplemented;
751 }
752 break;
bellard3532fa72006-06-24 15:06:03 +0000753 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +0000754 switch (optname) {
755 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +0000756 case TARGET_SO_DEBUG:
757 optname = SO_DEBUG;
758 break;
759 case TARGET_SO_REUSEADDR:
760 optname = SO_REUSEADDR;
761 break;
762 case TARGET_SO_TYPE:
763 optname = SO_TYPE;
764 break;
765 case TARGET_SO_ERROR:
766 optname = SO_ERROR;
767 break;
768 case TARGET_SO_DONTROUTE:
769 optname = SO_DONTROUTE;
770 break;
771 case TARGET_SO_BROADCAST:
772 optname = SO_BROADCAST;
773 break;
774 case TARGET_SO_SNDBUF:
775 optname = SO_SNDBUF;
776 break;
777 case TARGET_SO_RCVBUF:
778 optname = SO_RCVBUF;
779 break;
780 case TARGET_SO_KEEPALIVE:
781 optname = SO_KEEPALIVE;
782 break;
783 case TARGET_SO_OOBINLINE:
784 optname = SO_OOBINLINE;
785 break;
786 case TARGET_SO_NO_CHECK:
787 optname = SO_NO_CHECK;
788 break;
789 case TARGET_SO_PRIORITY:
790 optname = SO_PRIORITY;
791 break;
bellard5e83e8e2005-03-01 22:32:06 +0000792#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +0000793 case TARGET_SO_BSDCOMPAT:
794 optname = SO_BSDCOMPAT;
795 break;
bellard5e83e8e2005-03-01 22:32:06 +0000796#endif
bellard3532fa72006-06-24 15:06:03 +0000797 case TARGET_SO_PASSCRED:
798 optname = SO_PASSCRED;
799 break;
800 case TARGET_SO_TIMESTAMP:
801 optname = SO_TIMESTAMP;
802 break;
803 case TARGET_SO_RCVLOWAT:
804 optname = SO_RCVLOWAT;
805 break;
806 case TARGET_SO_RCVTIMEO:
807 optname = SO_RCVTIMEO;
808 break;
809 case TARGET_SO_SNDTIMEO:
810 optname = SO_SNDTIMEO;
811 break;
bellard8853f862004-02-22 14:57:26 +0000812 break;
813 default:
814 goto unimplemented;
815 }
bellard3532fa72006-06-24 15:06:03 +0000816 if (optlen < sizeof(uint32_t))
817 return -EINVAL;
818
819 val = tget32(optval);
820 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +0000821 break;
bellard7854b052003-03-29 17:22:23 +0000822 default:
bellard8853f862004-02-22 14:57:26 +0000823 unimplemented:
824 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
825 ret = -ENOSYS;
bellard7854b052003-03-29 17:22:23 +0000826 }
bellard8853f862004-02-22 14:57:26 +0000827 return ret;
bellard7854b052003-03-29 17:22:23 +0000828}
829
blueswir1992f48a2007-10-14 16:27:31 +0000830static abi_long do_getsockopt(int sockfd, int level, int optname,
831 abi_ulong optval, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +0000832{
blueswir1992f48a2007-10-14 16:27:31 +0000833 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +0000834 int len, lv, val;
bellard8853f862004-02-22 14:57:26 +0000835
836 switch(level) {
bellard3532fa72006-06-24 15:06:03 +0000837 case TARGET_SOL_SOCKET:
838 level = SOL_SOCKET;
bellard8853f862004-02-22 14:57:26 +0000839 switch (optname) {
bellard3532fa72006-06-24 15:06:03 +0000840 case TARGET_SO_LINGER:
841 case TARGET_SO_RCVTIMEO:
842 case TARGET_SO_SNDTIMEO:
843 case TARGET_SO_PEERCRED:
844 case TARGET_SO_PEERNAME:
bellard8853f862004-02-22 14:57:26 +0000845 /* These don't just return a single integer */
846 goto unimplemented;
847 default:
bellard2efbe912005-07-23 15:10:20 +0000848 goto int_case;
849 }
850 break;
851 case SOL_TCP:
852 /* TCP options all take an 'int' value. */
853 int_case:
pbrook53a59602006-03-25 19:31:22 +0000854 len = tget32(optlen);
bellard2efbe912005-07-23 15:10:20 +0000855 if (len < 0)
856 return -EINVAL;
857 lv = sizeof(int);
858 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
859 if (ret < 0)
860 return ret;
861 val = tswap32(val);
862 if (len > lv)
863 len = lv;
pbrook53a59602006-03-25 19:31:22 +0000864 if (len == 4)
865 tput32(optval, val);
866 else
867 tput8(optval, val);
868 tput32(optlen, len);
bellard2efbe912005-07-23 15:10:20 +0000869 break;
870 case SOL_IP:
871 switch(optname) {
872 case IP_TOS:
873 case IP_TTL:
874 case IP_HDRINCL:
875 case IP_ROUTER_ALERT:
876 case IP_RECVOPTS:
877 case IP_RETOPTS:
878 case IP_PKTINFO:
879 case IP_MTU_DISCOVER:
880 case IP_RECVERR:
881 case IP_RECVTOS:
882#ifdef IP_FREEBIND
883 case IP_FREEBIND:
884#endif
885 case IP_MULTICAST_TTL:
886 case IP_MULTICAST_LOOP:
pbrook53a59602006-03-25 19:31:22 +0000887 len = tget32(optlen);
bellard8853f862004-02-22 14:57:26 +0000888 if (len < 0)
889 return -EINVAL;
890 lv = sizeof(int);
891 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
892 if (ret < 0)
893 return ret;
bellard2efbe912005-07-23 15:10:20 +0000894 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +0000895 len = 1;
pbrook53a59602006-03-25 19:31:22 +0000896 tput32(optlen, len);
897 tput8(optval, val);
bellard2efbe912005-07-23 15:10:20 +0000898 } else {
bellard2efbe912005-07-23 15:10:20 +0000899 if (len > sizeof(int))
900 len = sizeof(int);
pbrook53a59602006-03-25 19:31:22 +0000901 tput32(optlen, len);
902 tput32(optval, val);
bellard2efbe912005-07-23 15:10:20 +0000903 }
bellard8853f862004-02-22 14:57:26 +0000904 break;
bellard2efbe912005-07-23 15:10:20 +0000905 default:
906 goto unimplemented;
bellard8853f862004-02-22 14:57:26 +0000907 }
908 break;
909 default:
910 unimplemented:
911 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
912 level, optname);
913 ret = -ENOSYS;
914 break;
915 }
916 return ret;
bellard7854b052003-03-29 17:22:23 +0000917}
918
blueswir1992f48a2007-10-14 16:27:31 +0000919static void lock_iovec(struct iovec *vec, abi_ulong target_addr,
pbrook53a59602006-03-25 19:31:22 +0000920 int count, int copy)
921{
922 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +0000923 abi_ulong base;
pbrook53a59602006-03-25 19:31:22 +0000924 int i;
925
926 target_vec = lock_user(target_addr, count * sizeof(struct target_iovec), 1);
927 for(i = 0;i < count; i++) {
928 base = tswapl(target_vec[i].iov_base);
929 vec[i].iov_len = tswapl(target_vec[i].iov_len);
930 vec[i].iov_base = lock_user(base, vec[i].iov_len, copy);
931 }
932 unlock_user (target_vec, target_addr, 0);
933}
934
blueswir1992f48a2007-10-14 16:27:31 +0000935static void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
pbrook53a59602006-03-25 19:31:22 +0000936 int count, int copy)
937{
938 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +0000939 abi_ulong base;
pbrook53a59602006-03-25 19:31:22 +0000940 int i;
941
942 target_vec = lock_user(target_addr, count * sizeof(struct target_iovec), 1);
943 for(i = 0;i < count; i++) {
944 base = tswapl(target_vec[i].iov_base);
945 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
946 }
947 unlock_user (target_vec, target_addr, 0);
948}
949
blueswir1992f48a2007-10-14 16:27:31 +0000950static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +0000951{
952#if defined(TARGET_MIPS)
953 switch(type) {
954 case TARGET_SOCK_DGRAM:
955 type = SOCK_DGRAM;
956 break;
957 case TARGET_SOCK_STREAM:
958 type = SOCK_STREAM;
959 break;
960 case TARGET_SOCK_RAW:
961 type = SOCK_RAW;
962 break;
963 case TARGET_SOCK_RDM:
964 type = SOCK_RDM;
965 break;
966 case TARGET_SOCK_SEQPACKET:
967 type = SOCK_SEQPACKET;
968 break;
969 case TARGET_SOCK_PACKET:
970 type = SOCK_PACKET;
971 break;
972 }
973#endif
974 return get_errno(socket(domain, type, protocol));
975}
976
blueswir1992f48a2007-10-14 16:27:31 +0000977static abi_long do_bind(int sockfd, abi_ulong target_addr,
978 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +0000979{
980 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +0000981
bellard3532fa72006-06-24 15:06:03 +0000982 target_to_host_sockaddr(addr, target_addr, addrlen);
983 return get_errno(bind(sockfd, addr, addrlen));
984}
985
blueswir1992f48a2007-10-14 16:27:31 +0000986static abi_long do_connect(int sockfd, abi_ulong target_addr,
987 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +0000988{
989 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +0000990
bellard3532fa72006-06-24 15:06:03 +0000991 target_to_host_sockaddr(addr, target_addr, addrlen);
992 return get_errno(connect(sockfd, addr, addrlen));
993}
994
blueswir1992f48a2007-10-14 16:27:31 +0000995static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
996 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +0000997{
blueswir1992f48a2007-10-14 16:27:31 +0000998 abi_long ret;
bellard3532fa72006-06-24 15:06:03 +0000999 struct target_msghdr *msgp;
1000 struct msghdr msg;
1001 int count;
1002 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001003 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001004
1005 lock_user_struct(msgp, target_msg, 1);
1006 if (msgp->msg_name) {
1007 msg.msg_namelen = tswap32(msgp->msg_namelen);
1008 msg.msg_name = alloca(msg.msg_namelen);
1009 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1010 msg.msg_namelen);
1011 } else {
1012 msg.msg_name = NULL;
1013 msg.msg_namelen = 0;
1014 }
1015 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1016 msg.msg_control = alloca(msg.msg_controllen);
1017 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001018
bellard3532fa72006-06-24 15:06:03 +00001019 count = tswapl(msgp->msg_iovlen);
1020 vec = alloca(count * sizeof(struct iovec));
1021 target_vec = tswapl(msgp->msg_iov);
1022 lock_iovec(vec, target_vec, count, send);
1023 msg.msg_iovlen = count;
1024 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001025
bellard3532fa72006-06-24 15:06:03 +00001026 if (send) {
1027 target_to_host_cmsg(&msg, msgp);
1028 ret = get_errno(sendmsg(fd, &msg, flags));
1029 } else {
1030 ret = get_errno(recvmsg(fd, &msg, flags));
1031 if (!is_error(ret))
1032 host_to_target_cmsg(msgp, &msg);
1033 }
1034 unlock_iovec(vec, target_vec, count, !send);
1035 return ret;
1036}
1037
blueswir1992f48a2007-10-14 16:27:31 +00001038static abi_long do_accept(int fd, abi_ulong target_addr,
1039 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001040{
1041 socklen_t addrlen = tget32(target_addrlen);
ths197ea352006-12-11 19:13:59 +00001042 void *addr = alloca(addrlen);
blueswir1992f48a2007-10-14 16:27:31 +00001043 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001044
1045 ret = get_errno(accept(fd, addr, &addrlen));
1046 if (!is_error(ret)) {
1047 host_to_target_sockaddr(target_addr, addr, addrlen);
1048 tput32(target_addrlen, addrlen);
1049 }
1050 return ret;
1051}
1052
blueswir1992f48a2007-10-14 16:27:31 +00001053static abi_long do_getpeername(int fd, abi_ulong target_addr,
1054 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001055{
1056 socklen_t addrlen = tget32(target_addrlen);
ths01c227f2007-03-19 13:05:13 +00001057 void *addr = alloca(addrlen);
blueswir1992f48a2007-10-14 16:27:31 +00001058 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001059
1060 ret = get_errno(getpeername(fd, addr, &addrlen));
1061 if (!is_error(ret)) {
1062 host_to_target_sockaddr(target_addr, addr, addrlen);
1063 tput32(target_addrlen, addrlen);
1064 }
1065 return ret;
1066}
1067
blueswir1992f48a2007-10-14 16:27:31 +00001068static abi_long do_getsockname(int fd, abi_ulong target_addr,
1069 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001070{
1071 socklen_t addrlen = tget32(target_addrlen);
ths01c227f2007-03-19 13:05:13 +00001072 void *addr = alloca(addrlen);
blueswir1992f48a2007-10-14 16:27:31 +00001073 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001074
1075 ret = get_errno(getsockname(fd, addr, &addrlen));
1076 if (!is_error(ret)) {
1077 host_to_target_sockaddr(target_addr, addr, addrlen);
1078 tput32(target_addrlen, addrlen);
1079 }
1080 return ret;
1081}
1082
blueswir1992f48a2007-10-14 16:27:31 +00001083static abi_long do_socketpair(int domain, int type, int protocol,
1084 abi_ulong target_tab)
pbrook1be9e1d2006-11-19 15:26:04 +00001085{
1086 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00001087 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001088
1089 ret = get_errno(socketpair(domain, type, protocol, tab));
1090 if (!is_error(ret)) {
1091 tput32(target_tab, tab[0]);
1092 tput32(target_tab + 4, tab[1]);
1093 }
1094 return ret;
1095}
1096
blueswir1992f48a2007-10-14 16:27:31 +00001097static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1098 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001099{
1100 void *addr;
1101 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001102 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001103
1104 host_msg = lock_user(msg, len, 1);
1105 if (target_addr) {
1106 addr = alloca(addrlen);
1107 target_to_host_sockaddr(addr, target_addr, addrlen);
1108 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1109 } else {
1110 ret = get_errno(send(fd, host_msg, len, flags));
1111 }
1112 unlock_user(host_msg, msg, 0);
1113 return ret;
1114}
1115
blueswir1992f48a2007-10-14 16:27:31 +00001116static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1117 abi_ulong target_addr,
1118 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001119{
1120 socklen_t addrlen;
1121 void *addr;
1122 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001123 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001124
1125 host_msg = lock_user(msg, len, 0);
1126 if (target_addr) {
1127 addrlen = tget32(target_addrlen);
1128 addr = alloca(addrlen);
1129 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1130 } else {
1131 addr = NULL; /* To keep compiler quiet. */
1132 ret = get_errno(recv(fd, host_msg, len, flags));
1133 }
1134 if (!is_error(ret)) {
1135 if (target_addr) {
1136 host_to_target_sockaddr(target_addr, addr, addrlen);
1137 tput32(target_addrlen, addrlen);
1138 }
1139 unlock_user(host_msg, msg, len);
1140 } else {
1141 unlock_user(host_msg, msg, 0);
1142 }
1143 return ret;
1144}
1145
j_mayer32407102007-09-26 23:01:49 +00001146#ifdef TARGET_NR_socketcall
blueswir1992f48a2007-10-14 16:27:31 +00001147static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00001148{
blueswir1992f48a2007-10-14 16:27:31 +00001149 abi_long ret;
1150 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00001151
1152 switch(num) {
1153 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00001154 {
pbrook53a59602006-03-25 19:31:22 +00001155 int domain = tgetl(vptr);
1156 int type = tgetl(vptr + n);
1157 int protocol = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001158 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00001159 }
bellard31e31b82003-02-18 22:55:36 +00001160 break;
1161 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00001162 {
pbrook53a59602006-03-25 19:31:22 +00001163 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001164 abi_ulong target_addr = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001165 socklen_t addrlen = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001166 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001167 }
bellard31e31b82003-02-18 22:55:36 +00001168 break;
1169 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00001170 {
pbrook53a59602006-03-25 19:31:22 +00001171 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001172 abi_ulong target_addr = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001173 socklen_t addrlen = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001174 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001175 }
bellard31e31b82003-02-18 22:55:36 +00001176 break;
1177 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00001178 {
pbrook53a59602006-03-25 19:31:22 +00001179 int sockfd = tgetl(vptr);
1180 int backlog = tgetl(vptr + n);
bellard7854b052003-03-29 17:22:23 +00001181 ret = get_errno(listen(sockfd, backlog));
1182 }
bellard31e31b82003-02-18 22:55:36 +00001183 break;
1184 case SOCKOP_accept:
1185 {
pbrook53a59602006-03-25 19:31:22 +00001186 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001187 abi_ulong target_addr = tgetl(vptr + n);
1188 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001189 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001190 }
1191 break;
1192 case SOCKOP_getsockname:
1193 {
pbrook53a59602006-03-25 19:31:22 +00001194 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001195 abi_ulong target_addr = tgetl(vptr + n);
1196 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001197 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001198 }
1199 break;
1200 case SOCKOP_getpeername:
1201 {
pbrook53a59602006-03-25 19:31:22 +00001202 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001203 abi_ulong target_addr = tgetl(vptr + n);
1204 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001205 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001206 }
1207 break;
1208 case SOCKOP_socketpair:
1209 {
pbrook53a59602006-03-25 19:31:22 +00001210 int domain = tgetl(vptr);
1211 int type = tgetl(vptr + n);
1212 int protocol = tgetl(vptr + 2 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001213 abi_ulong tab = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001214 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00001215 }
1216 break;
1217 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00001218 {
pbrook53a59602006-03-25 19:31:22 +00001219 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001220 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001221 size_t len = tgetl(vptr + 2 * n);
1222 int flags = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001223 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001224 }
bellard31e31b82003-02-18 22:55:36 +00001225 break;
1226 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00001227 {
pbrook53a59602006-03-25 19:31:22 +00001228 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001229 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001230 size_t len = tgetl(vptr + 2 * n);
1231 int flags = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001232 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001233 }
bellard31e31b82003-02-18 22:55:36 +00001234 break;
1235 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00001236 {
pbrook53a59602006-03-25 19:31:22 +00001237 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001238 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001239 size_t len = tgetl(vptr + 2 * n);
1240 int flags = tgetl(vptr + 3 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001241 abi_ulong addr = tgetl(vptr + 4 * n);
pbrook53a59602006-03-25 19:31:22 +00001242 socklen_t addrlen = tgetl(vptr + 5 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001243 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001244 }
bellard31e31b82003-02-18 22:55:36 +00001245 break;
1246 case SOCKOP_recvfrom:
1247 {
pbrook53a59602006-03-25 19:31:22 +00001248 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001249 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001250 size_t len = tgetl(vptr + 2 * n);
1251 int flags = tgetl(vptr + 3 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001252 abi_ulong addr = tgetl(vptr + 4 * n);
1253 abi_ulong addrlen = tgetl(vptr + 5 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001254 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00001255 }
1256 break;
1257 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00001258 {
pbrook53a59602006-03-25 19:31:22 +00001259 int sockfd = tgetl(vptr);
1260 int how = tgetl(vptr + n);
bellard7854b052003-03-29 17:22:23 +00001261
1262 ret = get_errno(shutdown(sockfd, how));
1263 }
bellard31e31b82003-02-18 22:55:36 +00001264 break;
1265 case SOCKOP_sendmsg:
1266 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00001267 {
1268 int fd;
blueswir1992f48a2007-10-14 16:27:31 +00001269 abi_ulong target_msg;
bellard3532fa72006-06-24 15:06:03 +00001270 int flags;
bellard1a9353d2003-03-16 20:28:50 +00001271
pbrook53a59602006-03-25 19:31:22 +00001272 fd = tgetl(vptr);
bellard3532fa72006-06-24 15:06:03 +00001273 target_msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001274 flags = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001275
ths5fafdf22007-09-16 21:08:06 +00001276 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00001277 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00001278 }
1279 break;
bellard31e31b82003-02-18 22:55:36 +00001280 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00001281 {
pbrook53a59602006-03-25 19:31:22 +00001282 int sockfd = tgetl(vptr);
1283 int level = tgetl(vptr + n);
1284 int optname = tgetl(vptr + 2 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001285 abi_ulong optval = tgetl(vptr + 3 * n);
pbrook53a59602006-03-25 19:31:22 +00001286 socklen_t optlen = tgetl(vptr + 4 * n);
bellard7854b052003-03-29 17:22:23 +00001287
1288 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1289 }
1290 break;
bellard31e31b82003-02-18 22:55:36 +00001291 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00001292 {
pbrook53a59602006-03-25 19:31:22 +00001293 int sockfd = tgetl(vptr);
1294 int level = tgetl(vptr + n);
1295 int optname = tgetl(vptr + 2 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001296 abi_ulong optval = tgetl(vptr + 3 * n);
1297 abi_ulong poptlen = tgetl(vptr + 4 * n);
bellard7854b052003-03-29 17:22:23 +00001298
bellard8853f862004-02-22 14:57:26 +00001299 ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
bellard7854b052003-03-29 17:22:23 +00001300 }
1301 break;
bellard31e31b82003-02-18 22:55:36 +00001302 default:
1303 gemu_log("Unsupported socketcall: %d\n", num);
1304 ret = -ENOSYS;
1305 break;
1306 }
1307 return ret;
1308}
j_mayer32407102007-09-26 23:01:49 +00001309#endif
bellard31e31b82003-02-18 22:55:36 +00001310
j_mayer32407102007-09-26 23:01:49 +00001311#ifdef TARGET_NR_ipc
bellard8853f862004-02-22 14:57:26 +00001312#define N_SHM_REGIONS 32
1313
1314static struct shm_region {
1315 uint32_t start;
1316 uint32_t size;
1317} shm_regions[N_SHM_REGIONS];
1318
ths3eb6b042007-06-03 14:26:27 +00001319struct target_ipc_perm
1320{
blueswir1992f48a2007-10-14 16:27:31 +00001321 abi_long __key;
1322 abi_ulong uid;
1323 abi_ulong gid;
1324 abi_ulong cuid;
1325 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00001326 unsigned short int mode;
1327 unsigned short int __pad1;
1328 unsigned short int __seq;
1329 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00001330 abi_ulong __unused1;
1331 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00001332};
1333
1334struct target_semid_ds
1335{
1336 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001337 abi_ulong sem_otime;
1338 abi_ulong __unused1;
1339 abi_ulong sem_ctime;
1340 abi_ulong __unused2;
1341 abi_ulong sem_nsems;
1342 abi_ulong __unused3;
1343 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00001344};
1345
1346static inline void target_to_host_ipc_perm(struct ipc_perm *host_ip,
blueswir1992f48a2007-10-14 16:27:31 +00001347 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001348{
1349 struct target_ipc_perm *target_ip;
1350 struct target_semid_ds *target_sd;
1351
1352 lock_user_struct(target_sd, target_addr, 1);
1353 target_ip=&(target_sd->sem_perm);
1354 host_ip->__key = tswapl(target_ip->__key);
1355 host_ip->uid = tswapl(target_ip->uid);
1356 host_ip->gid = tswapl(target_ip->gid);
1357 host_ip->cuid = tswapl(target_ip->cuid);
1358 host_ip->cgid = tswapl(target_ip->cgid);
1359 host_ip->mode = tswapl(target_ip->mode);
1360 unlock_user_struct(target_sd, target_addr, 0);
1361}
1362
blueswir1992f48a2007-10-14 16:27:31 +00001363static inline void host_to_target_ipc_perm(abi_ulong target_addr,
ths3eb6b042007-06-03 14:26:27 +00001364 struct ipc_perm *host_ip)
1365{
1366 struct target_ipc_perm *target_ip;
1367 struct target_semid_ds *target_sd;
1368
1369 lock_user_struct(target_sd, target_addr, 0);
1370 target_ip = &(target_sd->sem_perm);
1371 target_ip->__key = tswapl(host_ip->__key);
1372 target_ip->uid = tswapl(host_ip->uid);
1373 target_ip->gid = tswapl(host_ip->gid);
1374 target_ip->cuid = tswapl(host_ip->cuid);
1375 target_ip->cgid = tswapl(host_ip->cgid);
1376 target_ip->mode = tswapl(host_ip->mode);
1377 unlock_user_struct(target_sd, target_addr, 1);
1378}
1379
1380static inline void target_to_host_semid_ds(struct semid_ds *host_sd,
blueswir1992f48a2007-10-14 16:27:31 +00001381 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001382{
1383 struct target_semid_ds *target_sd;
1384
1385 lock_user_struct(target_sd, target_addr, 1);
1386 target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1387 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1388 host_sd->sem_otime = tswapl(target_sd->sem_otime);
1389 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1390 unlock_user_struct(target_sd, target_addr, 0);
1391}
1392
blueswir1992f48a2007-10-14 16:27:31 +00001393static inline void host_to_target_semid_ds(abi_ulong target_addr,
ths3eb6b042007-06-03 14:26:27 +00001394 struct semid_ds *host_sd)
1395{
1396 struct target_semid_ds *target_sd;
1397
1398 lock_user_struct(target_sd, target_addr, 0);
1399 host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1400 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1401 target_sd->sem_otime = tswapl(host_sd->sem_otime);
1402 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1403 unlock_user_struct(target_sd, target_addr, 1);
1404}
1405
thsfa294812007-02-02 22:05:00 +00001406union semun {
1407 int val;
ths3eb6b042007-06-03 14:26:27 +00001408 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00001409 unsigned short *array;
1410};
1411
ths3eb6b042007-06-03 14:26:27 +00001412union target_semun {
1413 int val;
blueswir1992f48a2007-10-14 16:27:31 +00001414 abi_long buf;
ths3eb6b042007-06-03 14:26:27 +00001415 unsigned short int *array;
1416};
1417
j_mayer32407102007-09-26 23:01:49 +00001418static inline void target_to_host_semun(int cmd,
ths3eb6b042007-06-03 14:26:27 +00001419 union semun *host_su,
blueswir1992f48a2007-10-14 16:27:31 +00001420 abi_ulong target_addr,
ths3eb6b042007-06-03 14:26:27 +00001421 struct semid_ds *ds)
1422{
1423 union target_semun *target_su;
1424
1425 switch( cmd ) {
1426 case IPC_STAT:
1427 case IPC_SET:
1428 lock_user_struct(target_su, target_addr, 1);
1429 target_to_host_semid_ds(ds,target_su->buf);
1430 host_su->buf = ds;
1431 unlock_user_struct(target_su, target_addr, 0);
1432 break;
1433 case GETVAL:
1434 case SETVAL:
1435 lock_user_struct(target_su, target_addr, 1);
1436 host_su->val = tswapl(target_su->val);
1437 unlock_user_struct(target_su, target_addr, 0);
1438 break;
1439 case GETALL:
1440 case SETALL:
1441 lock_user_struct(target_su, target_addr, 1);
1442 *host_su->array = tswap16(*target_su->array);
1443 unlock_user_struct(target_su, target_addr, 0);
1444 break;
1445 default:
1446 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1447 }
1448}
1449
j_mayer32407102007-09-26 23:01:49 +00001450static inline void host_to_target_semun(int cmd,
blueswir1992f48a2007-10-14 16:27:31 +00001451 abi_ulong target_addr,
ths3eb6b042007-06-03 14:26:27 +00001452 union semun *host_su,
1453 struct semid_ds *ds)
1454{
1455 union target_semun *target_su;
1456
1457 switch( cmd ) {
1458 case IPC_STAT:
1459 case IPC_SET:
1460 lock_user_struct(target_su, target_addr, 0);
1461 host_to_target_semid_ds(target_su->buf,ds);
1462 unlock_user_struct(target_su, target_addr, 1);
1463 break;
1464 case GETVAL:
1465 case SETVAL:
1466 lock_user_struct(target_su, target_addr, 0);
1467 target_su->val = tswapl(host_su->val);
1468 unlock_user_struct(target_su, target_addr, 1);
1469 break;
1470 case GETALL:
1471 case SETALL:
1472 lock_user_struct(target_su, target_addr, 0);
1473 *target_su->array = tswap16(*host_su->array);
1474 unlock_user_struct(target_su, target_addr, 1);
1475 break;
1476 default:
1477 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1478 }
1479}
1480
blueswir1992f48a2007-10-14 16:27:31 +00001481static inline abi_long do_semctl(int first, int second, int third,
1482 abi_long ptr)
ths3eb6b042007-06-03 14:26:27 +00001483{
1484 union semun arg;
1485 struct semid_ds dsarg;
1486 int cmd = third&0xff;
blueswir1992f48a2007-10-14 16:27:31 +00001487 abi_long ret = 0;
ths3eb6b042007-06-03 14:26:27 +00001488
1489 switch( cmd ) {
1490 case GETVAL:
1491 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1492 ret = get_errno(semctl(first, second, cmd, arg));
1493 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1494 break;
1495 case SETVAL:
1496 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1497 ret = get_errno(semctl(first, second, cmd, arg));
1498 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1499 break;
1500 case GETALL:
1501 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1502 ret = get_errno(semctl(first, second, cmd, arg));
1503 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1504 break;
1505 case SETALL:
1506 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1507 ret = get_errno(semctl(first, second, cmd, arg));
1508 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1509 break;
1510 case IPC_STAT:
1511 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1512 ret = get_errno(semctl(first, second, cmd, arg));
1513 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1514 break;
1515 case IPC_SET:
1516 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1517 ret = get_errno(semctl(first, second, cmd, arg));
1518 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1519 break;
1520 default:
1521 ret = get_errno(semctl(first, second, cmd, arg));
1522 }
1523
1524 return ret;
1525}
1526
ths1bc012f2007-06-03 14:27:49 +00001527struct target_msqid_ds
1528{
1529 struct target_ipc_perm msg_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001530 abi_ulong msg_stime;
1531 abi_ulong __unused1;
1532 abi_ulong msg_rtime;
1533 abi_ulong __unused2;
1534 abi_ulong msg_ctime;
1535 abi_ulong __unused3;
1536 abi_ulong __msg_cbytes;
1537 abi_ulong msg_qnum;
1538 abi_ulong msg_qbytes;
1539 abi_ulong msg_lspid;
1540 abi_ulong msg_lrpid;
1541 abi_ulong __unused4;
1542 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00001543};
1544
1545static inline void target_to_host_msqid_ds(struct msqid_ds *host_md,
blueswir1992f48a2007-10-14 16:27:31 +00001546 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00001547{
1548 struct target_msqid_ds *target_md;
1549
1550 lock_user_struct(target_md, target_addr, 1);
1551 target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1552 host_md->msg_stime = tswapl(target_md->msg_stime);
1553 host_md->msg_rtime = tswapl(target_md->msg_rtime);
1554 host_md->msg_ctime = tswapl(target_md->msg_ctime);
1555 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1556 host_md->msg_qnum = tswapl(target_md->msg_qnum);
1557 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1558 host_md->msg_lspid = tswapl(target_md->msg_lspid);
1559 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1560 unlock_user_struct(target_md, target_addr, 0);
1561}
1562
blueswir1992f48a2007-10-14 16:27:31 +00001563static inline void host_to_target_msqid_ds(abi_ulong target_addr,
ths1bc012f2007-06-03 14:27:49 +00001564 struct msqid_ds *host_md)
1565{
1566 struct target_msqid_ds *target_md;
1567
1568 lock_user_struct(target_md, target_addr, 0);
1569 host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1570 target_md->msg_stime = tswapl(host_md->msg_stime);
1571 target_md->msg_rtime = tswapl(host_md->msg_rtime);
1572 target_md->msg_ctime = tswapl(host_md->msg_ctime);
1573 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1574 target_md->msg_qnum = tswapl(host_md->msg_qnum);
1575 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1576 target_md->msg_lspid = tswapl(host_md->msg_lspid);
1577 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1578 unlock_user_struct(target_md, target_addr, 1);
1579}
1580
blueswir1992f48a2007-10-14 16:27:31 +00001581static inline abi_long do_msgctl(int first, int second, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00001582{
1583 struct msqid_ds dsarg;
1584 int cmd = second&0xff;
blueswir1992f48a2007-10-14 16:27:31 +00001585 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001586 switch( cmd ) {
1587 case IPC_STAT:
1588 case IPC_SET:
1589 target_to_host_msqid_ds(&dsarg,ptr);
1590 ret = get_errno(msgctl(first, cmd, &dsarg));
1591 host_to_target_msqid_ds(ptr,&dsarg);
1592 default:
1593 ret = get_errno(msgctl(first, cmd, &dsarg));
1594 }
1595 return ret;
1596}
1597
1598struct target_msgbuf {
blueswir1992f48a2007-10-14 16:27:31 +00001599 abi_ulong mtype;
ths1bc012f2007-06-03 14:27:49 +00001600 char mtext[1];
1601};
1602
blueswir1992f48a2007-10-14 16:27:31 +00001603static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1604 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001605{
1606 struct target_msgbuf *target_mb;
1607 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00001608 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001609
1610 lock_user_struct(target_mb,msgp,0);
1611 host_mb = malloc(msgsz+sizeof(long));
1612 host_mb->mtype = tswapl(target_mb->mtype);
1613 memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1614 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1615 free(host_mb);
1616 unlock_user_struct(target_mb, msgp, 0);
1617
1618 return ret;
1619}
1620
blueswir1992f48a2007-10-14 16:27:31 +00001621static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1622 unsigned int msgsz, int msgtype,
1623 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001624{
1625 struct target_msgbuf *target_mb;
1626 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00001627 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001628
1629 lock_user_struct(target_mb, msgp, 0);
1630 host_mb = malloc(msgsz+sizeof(long));
1631 ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
1632 if (ret > 0)
1633 memcpy(target_mb->mtext, host_mb->mtext, ret);
1634 target_mb->mtype = tswapl(host_mb->mtype);
1635 free(host_mb);
1636 unlock_user_struct(target_mb, msgp, 0);
1637
1638 return ret;
1639}
1640
pbrook53a59602006-03-25 19:31:22 +00001641/* ??? This only works with linear mappings. */
blueswir1992f48a2007-10-14 16:27:31 +00001642static abi_long do_ipc(unsigned int call, int first,
1643 int second, int third,
1644 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00001645{
1646 int version;
blueswir1992f48a2007-10-14 16:27:31 +00001647 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00001648 unsigned long raddr;
1649 struct shmid_ds shm_info;
1650 int i;
1651
1652 version = call >> 16;
1653 call &= 0xffff;
1654
1655 switch (call) {
thsfa294812007-02-02 22:05:00 +00001656 case IPCOP_semop:
1657 ret = get_errno(semop(first,(struct sembuf *) ptr, second));
1658 break;
1659
1660 case IPCOP_semget:
1661 ret = get_errno(semget(first, second, third));
1662 break;
1663
1664 case IPCOP_semctl:
ths3eb6b042007-06-03 14:26:27 +00001665 ret = do_semctl(first, second, third, ptr);
thsfa294812007-02-02 22:05:00 +00001666 break;
1667
1668 case IPCOP_semtimedop:
j_mayer32407102007-09-26 23:01:49 +00001669 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
thsfa294812007-02-02 22:05:00 +00001670 ret = -ENOSYS;
1671 break;
thsd96372e2007-02-02 22:05:44 +00001672
1673 case IPCOP_msgget:
1674 ret = get_errno(msgget(first, second));
1675 break;
1676
1677 case IPCOP_msgsnd:
ths1bc012f2007-06-03 14:27:49 +00001678 ret = do_msgsnd(first, ptr, second, third);
thsd96372e2007-02-02 22:05:44 +00001679 break;
1680
1681 case IPCOP_msgctl:
ths1bc012f2007-06-03 14:27:49 +00001682 ret = do_msgctl(first, second, ptr);
thsd96372e2007-02-02 22:05:44 +00001683 break;
1684
1685 case IPCOP_msgrcv:
ths1bc012f2007-06-03 14:27:49 +00001686 {
1687 struct ipc_kludge
1688 {
1689 void *__unbounded msgp;
1690 long int msgtyp;
1691 };
thsd96372e2007-02-02 22:05:44 +00001692
ths1bc012f2007-06-03 14:27:49 +00001693 struct ipc_kludge *foo = (struct ipc_kludge *) ptr;
1694 struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
thsd96372e2007-02-02 22:05:44 +00001695
ths1bc012f2007-06-03 14:27:49 +00001696 ret = do_msgrcv(first, (long)msgp, second, 0, third);
thsd96372e2007-02-02 22:05:44 +00001697
ths1bc012f2007-06-03 14:27:49 +00001698 }
thsd96372e2007-02-02 22:05:44 +00001699 break;
1700
bellard8853f862004-02-22 14:57:26 +00001701 case IPCOP_shmat:
1702 /* SHM_* flags are the same on all linux platforms */
1703 ret = get_errno((long) shmat(first, (void *) ptr, second));
1704 if (is_error(ret))
1705 break;
1706 raddr = ret;
1707 /* find out the length of the shared memory segment */
ths3b46e622007-09-17 08:09:54 +00001708
bellard8853f862004-02-22 14:57:26 +00001709 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
1710 if (is_error(ret)) {
1711 /* can't get length, bail out */
1712 shmdt((void *) raddr);
1713 break;
1714 }
1715 page_set_flags(raddr, raddr + shm_info.shm_segsz,
1716 PAGE_VALID | PAGE_READ |
1717 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
1718 for (i = 0; i < N_SHM_REGIONS; ++i) {
1719 if (shm_regions[i].start == 0) {
1720 shm_regions[i].start = raddr;
1721 shm_regions[i].size = shm_info.shm_segsz;
1722 break;
1723 }
1724 }
blueswir1992f48a2007-10-14 16:27:31 +00001725 if (put_user(raddr, (abi_ulong *)third))
bellard8853f862004-02-22 14:57:26 +00001726 return -EFAULT;
1727 ret = 0;
1728 break;
1729 case IPCOP_shmdt:
1730 for (i = 0; i < N_SHM_REGIONS; ++i) {
1731 if (shm_regions[i].start == ptr) {
1732 shm_regions[i].start = 0;
1733 page_set_flags(ptr, shm_regions[i].size, 0);
1734 break;
1735 }
1736 }
1737 ret = get_errno(shmdt((void *) ptr));
1738 break;
1739
1740 case IPCOP_shmget:
1741 /* IPC_* flag values are the same on all linux platforms */
1742 ret = get_errno(shmget(first, second, third));
1743 break;
1744
1745 /* IPC_* and SHM_* command values are the same on all linux platforms */
1746 case IPCOP_shmctl:
1747 switch(second) {
1748 case IPC_RMID:
1749 case SHM_LOCK:
1750 case SHM_UNLOCK:
1751 ret = get_errno(shmctl(first, second, NULL));
1752 break;
1753 default:
1754 goto unimplemented;
1755 }
1756 break;
1757 default:
1758 unimplemented:
j_mayer32407102007-09-26 23:01:49 +00001759 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
bellard8853f862004-02-22 14:57:26 +00001760 ret = -ENOSYS;
1761 break;
1762 }
1763 return ret;
1764}
j_mayer32407102007-09-26 23:01:49 +00001765#endif
bellard8853f862004-02-22 14:57:26 +00001766
bellard31e31b82003-02-18 22:55:36 +00001767/* kernel structure types definitions */
1768#define IFNAMSIZ 16
1769
1770#define STRUCT(name, list...) STRUCT_ ## name,
1771#define STRUCT_SPECIAL(name) STRUCT_ ## name,
1772enum {
1773#include "syscall_types.h"
1774};
1775#undef STRUCT
1776#undef STRUCT_SPECIAL
1777
1778#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
1779#define STRUCT_SPECIAL(name)
1780#include "syscall_types.h"
1781#undef STRUCT
1782#undef STRUCT_SPECIAL
1783
1784typedef struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00001785 unsigned int target_cmd;
1786 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00001787 const char *name;
1788 int access;
bellard1a9353d2003-03-16 20:28:50 +00001789 const argtype arg_type[5];
bellard31e31b82003-02-18 22:55:36 +00001790} IOCTLEntry;
1791
1792#define IOC_R 0x0001
1793#define IOC_W 0x0002
1794#define IOC_RW (IOC_R | IOC_W)
1795
1796#define MAX_STRUCT_SIZE 4096
1797
bellard2ab83ea2003-06-15 19:56:46 +00001798IOCTLEntry ioctl_entries[] = {
bellard31e31b82003-02-18 22:55:36 +00001799#define IOCTL(cmd, access, types...) \
1800 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
1801#include "ioctls.h"
1802 { 0, 0, },
1803};
1804
pbrook53a59602006-03-25 19:31:22 +00001805/* ??? Implement proper locking for ioctls. */
blueswir1992f48a2007-10-14 16:27:31 +00001806static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00001807{
1808 const IOCTLEntry *ie;
1809 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00001810 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00001811 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00001812 int target_size;
1813 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00001814
1815 ie = ioctl_entries;
1816 for(;;) {
1817 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00001818 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
bellard31e31b82003-02-18 22:55:36 +00001819 return -ENOSYS;
1820 }
1821 if (ie->target_cmd == cmd)
1822 break;
1823 ie++;
1824 }
1825 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00001826#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00001827 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00001828#endif
bellard31e31b82003-02-18 22:55:36 +00001829 switch(arg_type[0]) {
1830 case TYPE_NULL:
1831 /* no argument */
1832 ret = get_errno(ioctl(fd, ie->host_cmd));
1833 break;
1834 case TYPE_PTRVOID:
1835 case TYPE_INT:
1836 /* int argment */
1837 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
1838 break;
1839 case TYPE_PTR:
1840 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00001841 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00001842 switch(ie->access) {
1843 case IOC_R:
1844 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1845 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00001846 argptr = lock_user(arg, target_size, 0);
1847 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1848 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00001849 }
1850 break;
1851 case IOC_W:
pbrook53a59602006-03-25 19:31:22 +00001852 argptr = lock_user(arg, target_size, 1);
1853 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1854 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00001855 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1856 break;
1857 default:
1858 case IOC_RW:
pbrook53a59602006-03-25 19:31:22 +00001859 argptr = lock_user(arg, target_size, 1);
1860 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1861 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00001862 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1863 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00001864 argptr = lock_user(arg, target_size, 0);
1865 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1866 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00001867 }
1868 break;
1869 }
1870 break;
1871 default:
j_mayer32407102007-09-26 23:01:49 +00001872 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
1873 (long)cmd, arg_type[0]);
bellard31e31b82003-02-18 22:55:36 +00001874 ret = -ENOSYS;
1875 break;
1876 }
1877 return ret;
1878}
1879
1880bitmask_transtbl iflag_tbl[] = {
1881 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
1882 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
1883 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
1884 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
1885 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
1886 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
1887 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
1888 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
1889 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
1890 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
1891 { TARGET_IXON, TARGET_IXON, IXON, IXON },
1892 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
1893 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
1894 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
1895 { 0, 0, 0, 0 }
1896};
1897
1898bitmask_transtbl oflag_tbl[] = {
1899 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
1900 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
1901 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
1902 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
1903 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
1904 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
1905 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
1906 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
1907 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
1908 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
1909 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
1910 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
1911 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
1912 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
1913 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
1914 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
1915 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
1916 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
1917 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
1918 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
1919 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
1920 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
1921 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
1922 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
1923 { 0, 0, 0, 0 }
1924};
1925
1926bitmask_transtbl cflag_tbl[] = {
1927 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
1928 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
1929 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
1930 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
1931 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
1932 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
1933 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
1934 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
1935 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
1936 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
1937 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
1938 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
1939 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
1940 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
1941 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
1942 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
1943 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
1944 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
1945 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
1946 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
1947 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
1948 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
1949 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
1950 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
1951 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
1952 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
1953 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
1954 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
1955 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
1956 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
1957 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
1958 { 0, 0, 0, 0 }
1959};
1960
1961bitmask_transtbl lflag_tbl[] = {
1962 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
1963 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
1964 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
1965 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
1966 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
1967 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
1968 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
1969 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
1970 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
1971 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
1972 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
1973 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
1974 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
1975 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
1976 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
1977 { 0, 0, 0, 0 }
1978};
1979
1980static void target_to_host_termios (void *dst, const void *src)
1981{
1982 struct host_termios *host = dst;
1983 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00001984
ths5fafdf22007-09-16 21:08:06 +00001985 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00001986 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00001987 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00001988 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00001989 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00001990 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00001991 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00001992 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
1993 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00001994
ths5fafdf22007-09-16 21:08:06 +00001995 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
1996 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00001997 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00001998 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00001999 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00002000 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00002001 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00002002 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00002003 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00002004 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2005 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00002006 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2007 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2008 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2009 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2010 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00002011 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00002012}
ths3b46e622007-09-17 08:09:54 +00002013
bellard31e31b82003-02-18 22:55:36 +00002014static void host_to_target_termios (void *dst, const void *src)
2015{
2016 struct target_termios *target = dst;
2017 const struct host_termios *host = src;
2018
ths5fafdf22007-09-16 21:08:06 +00002019 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002020 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002021 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002022 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002023 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002024 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002025 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002026 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2027 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00002028
bellard31e31b82003-02-18 22:55:36 +00002029 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2030 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2031 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2032 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2033 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2034 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2035 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2036 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2037 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2038 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2039 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2040 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2041 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2042 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2043 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2044 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2045 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2046}
2047
2048StructEntry struct_termios_def = {
2049 .convert = { host_to_target_termios, target_to_host_termios },
2050 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2051 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2052};
2053
bellard5286db72003-06-05 00:57:30 +00002054static bitmask_transtbl mmap_flags_tbl[] = {
2055 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2056 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2057 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2058 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2059 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2060 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2061 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2062 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2063 { 0, 0, 0, 0 }
2064};
2065
bellardffa65c32004-01-04 23:57:22 +00002066static bitmask_transtbl fcntl_flags_tbl[] = {
2067 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
2068 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
2069 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
2070 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
2071 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
2072 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
2073 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
2074 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
2075 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
2076 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
2077 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2078 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
2079 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
bellard121061d2004-02-22 11:56:01 +00002080#if defined(O_DIRECT)
bellardffa65c32004-01-04 23:57:22 +00002081 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
bellard121061d2004-02-22 11:56:01 +00002082#endif
bellardffa65c32004-01-04 23:57:22 +00002083 { 0, 0, 0, 0 }
2084};
2085
bellard2ab83ea2003-06-15 19:56:46 +00002086#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00002087
2088/* NOTE: there is really one LDT for all the threads */
2089uint8_t *ldt_table;
2090
blueswir1992f48a2007-10-14 16:27:31 +00002091static int read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002092{
2093 int size;
pbrook53a59602006-03-25 19:31:22 +00002094 void *p;
bellard6dbad632003-03-16 18:05:05 +00002095
2096 if (!ldt_table)
2097 return 0;
2098 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2099 if (size > bytecount)
2100 size = bytecount;
pbrook53a59602006-03-25 19:31:22 +00002101 p = lock_user(ptr, size, 0);
2102 /* ??? Shoudl this by byteswapped? */
2103 memcpy(p, ldt_table, size);
2104 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00002105 return size;
2106}
2107
2108/* XXX: add locking support */
ths5fafdf22007-09-16 21:08:06 +00002109static int write_ldt(CPUX86State *env,
blueswir1992f48a2007-10-14 16:27:31 +00002110 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00002111{
2112 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00002113 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00002114 int seg_32bit, contents, read_exec_only, limit_in_pages;
2115 int seg_not_present, useable;
2116 uint32_t *lp, entry_1, entry_2;
2117
2118 if (bytecount != sizeof(ldt_info))
2119 return -EINVAL;
pbrook53a59602006-03-25 19:31:22 +00002120 lock_user_struct(target_ldt_info, ptr, 1);
2121 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2122 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2123 ldt_info.limit = tswap32(target_ldt_info->limit);
2124 ldt_info.flags = tswap32(target_ldt_info->flags);
2125 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00002126
bellard6dbad632003-03-16 18:05:05 +00002127 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
2128 return -EINVAL;
2129 seg_32bit = ldt_info.flags & 1;
2130 contents = (ldt_info.flags >> 1) & 3;
2131 read_exec_only = (ldt_info.flags >> 3) & 1;
2132 limit_in_pages = (ldt_info.flags >> 4) & 1;
2133 seg_not_present = (ldt_info.flags >> 5) & 1;
2134 useable = (ldt_info.flags >> 6) & 1;
2135
2136 if (contents == 3) {
2137 if (oldmode)
2138 return -EINVAL;
2139 if (seg_not_present == 0)
2140 return -EINVAL;
2141 }
2142 /* allocate the LDT */
2143 if (!ldt_table) {
2144 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2145 if (!ldt_table)
2146 return -ENOMEM;
2147 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
pbrook53a59602006-03-25 19:31:22 +00002148 env->ldt.base = h2g(ldt_table);
bellard6dbad632003-03-16 18:05:05 +00002149 env->ldt.limit = 0xffff;
2150 }
2151
2152 /* NOTE: same code as Linux kernel */
2153 /* Allow LDTs to be cleared by the user. */
2154 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2155 if (oldmode ||
2156 (contents == 0 &&
2157 read_exec_only == 1 &&
2158 seg_32bit == 0 &&
2159 limit_in_pages == 0 &&
2160 seg_not_present == 1 &&
2161 useable == 0 )) {
2162 entry_1 = 0;
2163 entry_2 = 0;
2164 goto install;
2165 }
2166 }
ths3b46e622007-09-17 08:09:54 +00002167
bellard6dbad632003-03-16 18:05:05 +00002168 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2169 (ldt_info.limit & 0x0ffff);
2170 entry_2 = (ldt_info.base_addr & 0xff000000) |
2171 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2172 (ldt_info.limit & 0xf0000) |
2173 ((read_exec_only ^ 1) << 9) |
2174 (contents << 10) |
2175 ((seg_not_present ^ 1) << 15) |
2176 (seg_32bit << 22) |
2177 (limit_in_pages << 23) |
2178 0x7000;
2179 if (!oldmode)
2180 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00002181
bellard6dbad632003-03-16 18:05:05 +00002182 /* Install the new entry ... */
2183install:
2184 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2185 lp[0] = tswap32(entry_1);
2186 lp[1] = tswap32(entry_2);
2187 return 0;
2188}
2189
2190/* specific and weird i386 syscalls */
blueswir1992f48a2007-10-14 16:27:31 +00002191int do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002192{
2193 int ret = -ENOSYS;
ths3b46e622007-09-17 08:09:54 +00002194
bellard6dbad632003-03-16 18:05:05 +00002195 switch (func) {
2196 case 0:
2197 ret = read_ldt(ptr, bytecount);
2198 break;
2199 case 1:
2200 ret = write_ldt(env, ptr, bytecount, 1);
2201 break;
2202 case 0x11:
2203 ret = write_ldt(env, ptr, bytecount, 0);
2204 break;
2205 }
2206 return ret;
2207}
bellard1b6b0292003-03-22 17:31:38 +00002208
bellard2ab83ea2003-06-15 19:56:46 +00002209#endif /* defined(TARGET_I386) */
2210
bellard1b6b0292003-03-22 17:31:38 +00002211/* this stack is the equivalent of the kernel stack associated with a
2212 thread/process */
2213#define NEW_STACK_SIZE 8192
2214
2215static int clone_func(void *arg)
2216{
bellard2ab83ea2003-06-15 19:56:46 +00002217 CPUState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00002218 cpu_loop(env);
2219 /* never exits */
2220 return 0;
2221}
2222
blueswir1992f48a2007-10-14 16:27:31 +00002223int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp)
bellard1b6b0292003-03-22 17:31:38 +00002224{
2225 int ret;
bellard5cd43932003-03-29 16:54:36 +00002226 TaskState *ts;
bellard1b6b0292003-03-22 17:31:38 +00002227 uint8_t *new_stack;
bellard2ab83ea2003-06-15 19:56:46 +00002228 CPUState *new_env;
ths3b46e622007-09-17 08:09:54 +00002229
bellard1b6b0292003-03-22 17:31:38 +00002230 if (flags & CLONE_VM) {
bellard5cd43932003-03-29 16:54:36 +00002231 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2232 memset(ts, 0, sizeof(TaskState));
2233 new_stack = ts->stack;
2234 ts->used = 1;
2235 /* add in task state list */
2236 ts->next = first_task_state;
2237 first_task_state = ts;
bellard1b6b0292003-03-22 17:31:38 +00002238 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00002239 new_env = cpu_copy(env);
bellard2ab83ea2003-06-15 19:56:46 +00002240#if defined(TARGET_I386)
2241 if (!newsp)
2242 newsp = env->regs[R_ESP];
bellard1b6b0292003-03-22 17:31:38 +00002243 new_env->regs[R_ESP] = newsp;
2244 new_env->regs[R_EAX] = 0;
bellard2ab83ea2003-06-15 19:56:46 +00002245#elif defined(TARGET_ARM)
2246 if (!newsp)
2247 newsp = env->regs[13];
2248 new_env->regs[13] = newsp;
2249 new_env->regs[0] = 0;
bellardebc05482003-09-30 21:08:41 +00002250#elif defined(TARGET_SPARC)
bellard48dc41e2006-06-21 18:15:50 +00002251 if (!newsp)
2252 newsp = env->regwptr[22];
2253 new_env->regwptr[22] = newsp;
2254 new_env->regwptr[0] = 0;
2255 /* XXXXX */
bellard67867302003-11-23 17:05:30 +00002256 printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
pbrooke6e59062006-10-22 00:18:54 +00002257#elif defined(TARGET_M68K)
2258 if (!newsp)
2259 newsp = env->aregs[7];
2260 new_env->aregs[7] = newsp;
2261 new_env->dregs[0] = 0;
2262 /* ??? is this sufficient? */
bellard048f6b42005-11-26 18:47:20 +00002263#elif defined(TARGET_MIPS)
ths388bb212007-05-13 13:58:00 +00002264 if (!newsp)
thsead93602007-09-06 00:18:15 +00002265 newsp = env->gpr[29][env->current_tc];
2266 new_env->gpr[29][env->current_tc] = newsp;
bellard67867302003-11-23 17:05:30 +00002267#elif defined(TARGET_PPC)
2268 if (!newsp)
2269 newsp = env->gpr[1];
2270 new_env->gpr[1] = newsp;
ths5fafdf22007-09-16 21:08:06 +00002271 {
bellard67867302003-11-23 17:05:30 +00002272 int i;
2273 for (i = 7; i < 32; i++)
2274 new_env->gpr[i] = 0;
2275 }
bellardfdf9b3e2006-04-27 21:07:38 +00002276#elif defined(TARGET_SH4)
2277 if (!newsp)
2278 newsp = env->gregs[15];
2279 new_env->gregs[15] = newsp;
2280 /* XXXXX */
j_mayer7a3148a2007-04-05 07:13:51 +00002281#elif defined(TARGET_ALPHA)
2282 if (!newsp)
2283 newsp = env->ir[30];
2284 new_env->ir[30] = newsp;
2285 /* ? */
2286 {
2287 int i;
2288 for (i = 7; i < 30; i++)
2289 new_env->ir[i] = 0;
2290 }
ths48733d12007-10-08 13:36:46 +00002291#elif defined(TARGET_CRIS)
2292 if (!newsp)
2293 newsp = env->regs[14];
2294 new_env->regs[14] = newsp;
bellard2ab83ea2003-06-15 19:56:46 +00002295#else
2296#error unsupported target CPU
2297#endif
bellard5cd43932003-03-29 16:54:36 +00002298 new_env->opaque = ts;
bellard27725c12003-04-29 21:08:18 +00002299#ifdef __ia64__
bellardfd4a43e2006-04-24 20:32:17 +00002300 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00002301#else
2302 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2303#endif
bellard1b6b0292003-03-22 17:31:38 +00002304 } else {
2305 /* if no CLONE_VM, we consider it is a fork */
2306 if ((flags & ~CSIGNAL) != 0)
2307 return -EINVAL;
2308 ret = fork();
2309 }
2310 return ret;
2311}
2312
blueswir1992f48a2007-10-14 16:27:31 +00002313static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00002314{
2315 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00002316 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00002317 struct flock64 fl64;
2318 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00002319 abi_long ret;
pbrook53a59602006-03-25 19:31:22 +00002320
bellard7775e9e2003-05-14 22:46:48 +00002321 switch(cmd) {
2322 case TARGET_F_GETLK:
ths58134272007-03-31 18:59:32 +00002323 lock_user_struct(target_fl, arg, 1);
2324 fl.l_type = tswap16(target_fl->l_type);
2325 fl.l_whence = tswap16(target_fl->l_whence);
2326 fl.l_start = tswapl(target_fl->l_start);
2327 fl.l_len = tswapl(target_fl->l_len);
2328 fl.l_pid = tswapl(target_fl->l_pid);
2329 unlock_user_struct(target_fl, arg, 0);
bellard7775e9e2003-05-14 22:46:48 +00002330 ret = fcntl(fd, cmd, &fl);
2331 if (ret == 0) {
pbrook53a59602006-03-25 19:31:22 +00002332 lock_user_struct(target_fl, arg, 0);
bellard7775e9e2003-05-14 22:46:48 +00002333 target_fl->l_type = tswap16(fl.l_type);
2334 target_fl->l_whence = tswap16(fl.l_whence);
2335 target_fl->l_start = tswapl(fl.l_start);
2336 target_fl->l_len = tswapl(fl.l_len);
2337 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00002338 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00002339 }
2340 break;
ths3b46e622007-09-17 08:09:54 +00002341
bellard7775e9e2003-05-14 22:46:48 +00002342 case TARGET_F_SETLK:
2343 case TARGET_F_SETLKW:
pbrook53a59602006-03-25 19:31:22 +00002344 lock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00002345 fl.l_type = tswap16(target_fl->l_type);
2346 fl.l_whence = tswap16(target_fl->l_whence);
2347 fl.l_start = tswapl(target_fl->l_start);
2348 fl.l_len = tswapl(target_fl->l_len);
2349 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00002350 unlock_user_struct(target_fl, arg, 0);
bellard7775e9e2003-05-14 22:46:48 +00002351 ret = fcntl(fd, cmd, &fl);
2352 break;
ths3b46e622007-09-17 08:09:54 +00002353
bellard7775e9e2003-05-14 22:46:48 +00002354 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00002355 lock_user_struct(target_fl64, arg, 1);
2356 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2357 fl64.l_whence = tswap16(target_fl64->l_whence);
2358 fl64.l_start = tswapl(target_fl64->l_start);
2359 fl64.l_len = tswapl(target_fl64->l_len);
2360 fl64.l_pid = tswap16(target_fl64->l_pid);
2361 unlock_user_struct(target_fl64, arg, 0);
ths43f238d2007-01-05 20:55:49 +00002362 ret = fcntl(fd, cmd >> 1, &fl64);
2363 if (ret == 0) {
2364 lock_user_struct(target_fl64, arg, 0);
2365 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2366 target_fl64->l_whence = tswap16(fl64.l_whence);
2367 target_fl64->l_start = tswapl(fl64.l_start);
2368 target_fl64->l_len = tswapl(fl64.l_len);
2369 target_fl64->l_pid = tswapl(fl64.l_pid);
2370 unlock_user_struct(target_fl64, arg, 1);
2371 }
2372 break;
bellard7775e9e2003-05-14 22:46:48 +00002373 case TARGET_F_SETLK64:
2374 case TARGET_F_SETLKW64:
ths43f238d2007-01-05 20:55:49 +00002375 lock_user_struct(target_fl64, arg, 1);
2376 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2377 fl64.l_whence = tswap16(target_fl64->l_whence);
2378 fl64.l_start = tswapl(target_fl64->l_start);
2379 fl64.l_len = tswapl(target_fl64->l_len);
2380 fl64.l_pid = tswap16(target_fl64->l_pid);
2381 unlock_user_struct(target_fl64, arg, 0);
2382 ret = fcntl(fd, cmd >> 1, &fl64);
bellard7775e9e2003-05-14 22:46:48 +00002383 break;
2384
bellardffa65c32004-01-04 23:57:22 +00002385 case F_GETFL:
2386 ret = fcntl(fd, cmd, arg);
2387 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2388 break;
2389
2390 case F_SETFL:
2391 ret = fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl));
2392 break;
2393
bellard7775e9e2003-05-14 22:46:48 +00002394 default:
2395 ret = fcntl(fd, cmd, arg);
2396 break;
2397 }
2398 return ret;
2399}
2400
bellard67867302003-11-23 17:05:30 +00002401#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00002402
bellard67867302003-11-23 17:05:30 +00002403static inline int high2lowuid(int uid)
2404{
2405 if (uid > 65535)
2406 return 65534;
2407 else
2408 return uid;
2409}
2410
2411static inline int high2lowgid(int gid)
2412{
2413 if (gid > 65535)
2414 return 65534;
2415 else
2416 return gid;
2417}
2418
2419static inline int low2highuid(int uid)
2420{
2421 if ((int16_t)uid == -1)
2422 return -1;
2423 else
2424 return uid;
2425}
2426
2427static inline int low2highgid(int gid)
2428{
2429 if ((int16_t)gid == -1)
2430 return -1;
2431 else
2432 return gid;
2433}
2434
2435#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00002436
bellard31e31b82003-02-18 22:55:36 +00002437void syscall_init(void)
2438{
bellard2ab83ea2003-06-15 19:56:46 +00002439 IOCTLEntry *ie;
2440 const argtype *arg_type;
2441 int size;
2442
ths5fafdf22007-09-16 21:08:06 +00002443#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2444#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00002445#include "syscall_types.h"
2446#undef STRUCT
2447#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00002448
2449 /* we patch the ioctl size if necessary. We rely on the fact that
2450 no ioctl has all the bits at '1' in the size field */
2451 ie = ioctl_entries;
2452 while (ie->target_cmd != 0) {
2453 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2454 TARGET_IOC_SIZEMASK) {
2455 arg_type = ie->arg_type;
2456 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00002457 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002458 ie->target_cmd);
2459 exit(1);
2460 }
2461 arg_type++;
2462 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00002463 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00002464 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2465 (size << TARGET_IOC_SIZESHIFT);
2466 }
2467 /* automatic consistency check if same arch */
2468#if defined(__i386__) && defined(TARGET_I386)
2469 if (ie->target_cmd != ie->host_cmd) {
ths5fafdf22007-09-16 21:08:06 +00002470 fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002471 ie->target_cmd, ie->host_cmd);
2472 }
2473#endif
2474 ie++;
2475 }
bellard31e31b82003-02-18 22:55:36 +00002476}
bellardc573ff62004-01-04 15:51:36 +00002477
blueswir1992f48a2007-10-14 16:27:31 +00002478#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00002479static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2480{
2481#ifdef TARGET_WORDS_BIG_ENDIAN
2482 return ((uint64_t)word0 << 32) | word1;
2483#else
2484 return ((uint64_t)word1 << 32) | word0;
2485#endif
2486}
blueswir1992f48a2007-10-14 16:27:31 +00002487#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00002488static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2489{
2490 return word0;
2491}
blueswir1992f48a2007-10-14 16:27:31 +00002492#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00002493
2494#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00002495static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
2496 abi_long arg2,
2497 abi_long arg3,
2498 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00002499{
2500#ifdef TARGET_ARM
2501 if (((CPUARMState *)cpu_env)->eabi)
2502 {
2503 arg2 = arg3;
2504 arg3 = arg4;
2505 }
2506#endif
2507 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
2508}
2509#endif
2510
2511#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00002512static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
2513 abi_long arg2,
2514 abi_long arg3,
2515 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00002516{
2517#ifdef TARGET_ARM
2518 if (((CPUARMState *)cpu_env)->eabi)
2519 {
2520 arg2 = arg3;
2521 arg3 = arg4;
2522 }
2523#endif
2524 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
2525}
2526#endif
2527
pbrook53a59602006-03-25 19:31:22 +00002528static inline void target_to_host_timespec(struct timespec *host_ts,
blueswir1992f48a2007-10-14 16:27:31 +00002529 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00002530{
2531 struct target_timespec *target_ts;
2532
2533 lock_user_struct(target_ts, target_addr, 1);
2534 host_ts->tv_sec = tswapl(target_ts->tv_sec);
2535 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
2536 unlock_user_struct(target_ts, target_addr, 0);
2537}
2538
blueswir1992f48a2007-10-14 16:27:31 +00002539static inline void host_to_target_timespec(abi_ulong target_addr,
pbrook53a59602006-03-25 19:31:22 +00002540 struct timespec *host_ts)
2541{
2542 struct target_timespec *target_ts;
2543
2544 lock_user_struct(target_ts, target_addr, 0);
2545 target_ts->tv_sec = tswapl(host_ts->tv_sec);
2546 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
2547 unlock_user_struct(target_ts, target_addr, 1);
2548}
2549
blueswir1992f48a2007-10-14 16:27:31 +00002550abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
2551 abi_long arg2, abi_long arg3, abi_long arg4,
2552 abi_long arg5, abi_long arg6)
bellard31e31b82003-02-18 22:55:36 +00002553{
blueswir1992f48a2007-10-14 16:27:31 +00002554 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00002555 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00002556 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00002557 void *p;
ths3b46e622007-09-17 08:09:54 +00002558
bellard72f03902003-02-18 23:33:18 +00002559#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00002560 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00002561#endif
bellard31e31b82003-02-18 22:55:36 +00002562 switch(num) {
2563 case TARGET_NR_exit:
bellard7d132992003-03-06 23:23:54 +00002564#ifdef HAVE_GPROF
2565 _mcleanup();
2566#endif
bellarde9009672005-04-26 20:42:36 +00002567 gdb_exit(cpu_env, arg1);
bellard1b6b0292003-03-22 17:31:38 +00002568 /* XXX: should free thread stack and CPU env */
bellard31e31b82003-02-18 22:55:36 +00002569 _exit(arg1);
2570 ret = 0; /* avoid warning */
2571 break;
2572 case TARGET_NR_read:
pbrook53a59602006-03-25 19:31:22 +00002573 page_unprotect_range(arg2, arg3);
2574 p = lock_user(arg2, arg3, 0);
2575 ret = get_errno(read(arg1, p, arg3));
2576 unlock_user(p, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00002577 break;
2578 case TARGET_NR_write:
pbrook53a59602006-03-25 19:31:22 +00002579 p = lock_user(arg2, arg3, 1);
2580 ret = get_errno(write(arg1, p, arg3));
2581 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00002582 break;
2583 case TARGET_NR_open:
pbrook53a59602006-03-25 19:31:22 +00002584 p = lock_user_string(arg1);
2585 ret = get_errno(open(path(p),
bellardffa65c32004-01-04 23:57:22 +00002586 target_to_host_bitmask(arg2, fcntl_flags_tbl),
2587 arg3));
pbrook53a59602006-03-25 19:31:22 +00002588 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002589 break;
ths82424832007-09-24 09:21:55 +00002590#if defined(TARGET_NR_openat) && defined(__NR_openat)
2591 case TARGET_NR_openat:
2592 if (!arg2) {
2593 ret = -EFAULT;
2594 goto fail;
2595 }
2596 p = lock_user_string(arg2);
2597 if (!access_ok(VERIFY_READ, p, 1))
2598 ret = -EFAULT;
2599 else
2600 ret = get_errno(sys_openat(arg1,
2601 path(p),
2602 target_to_host_bitmask(arg3, fcntl_flags_tbl),
2603 arg4));
2604 if (p)
2605 unlock_user(p, arg2, 0);
2606 break;
2607#endif
bellard31e31b82003-02-18 22:55:36 +00002608 case TARGET_NR_close:
2609 ret = get_errno(close(arg1));
2610 break;
2611 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00002612 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00002613 break;
2614 case TARGET_NR_fork:
bellard1b6b0292003-03-22 17:31:38 +00002615 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00002616 break;
thse5febef2007-04-01 18:31:35 +00002617#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00002618 case TARGET_NR_waitpid:
2619 {
pbrook53a59602006-03-25 19:31:22 +00002620 int status;
2621 ret = get_errno(waitpid(arg1, &status, arg3));
2622 if (!is_error(ret) && arg2)
2623 tput32(arg2, status);
bellard31e31b82003-02-18 22:55:36 +00002624 }
2625 break;
thse5febef2007-04-01 18:31:35 +00002626#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002627#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002628 case TARGET_NR_creat:
pbrook53a59602006-03-25 19:31:22 +00002629 p = lock_user_string(arg1);
2630 ret = get_errno(creat(p, arg2));
2631 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002632 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002633#endif
bellard31e31b82003-02-18 22:55:36 +00002634 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00002635 {
2636 void * p2;
2637 p = lock_user_string(arg1);
2638 p2 = lock_user_string(arg2);
2639 ret = get_errno(link(p, p2));
2640 unlock_user(p2, arg2, 0);
2641 unlock_user(p, arg1, 0);
2642 }
bellard31e31b82003-02-18 22:55:36 +00002643 break;
ths64f0ce42007-09-24 09:25:06 +00002644#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
2645 case TARGET_NR_linkat:
2646 if (!arg2 || !arg4) {
2647 ret = -EFAULT;
2648 goto fail;
2649 }
2650 {
2651 void * p2 = NULL;
2652 p = lock_user_string(arg2);
2653 p2 = lock_user_string(arg4);
2654 if (!access_ok(VERIFY_READ, p, 1)
2655 || !access_ok(VERIFY_READ, p2, 1))
2656 ret = -EFAULT;
2657 else
2658 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
2659 if (p2)
2660 unlock_user(p, arg2, 0);
2661 if (p)
2662 unlock_user(p2, arg4, 0);
2663 }
2664 break;
2665#endif
bellard31e31b82003-02-18 22:55:36 +00002666 case TARGET_NR_unlink:
pbrook53a59602006-03-25 19:31:22 +00002667 p = lock_user_string(arg1);
2668 ret = get_errno(unlink(p));
2669 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002670 break;
ths8170f562007-09-24 09:24:11 +00002671#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
2672 case TARGET_NR_unlinkat:
2673 if (!arg2) {
2674 ret = -EFAULT;
2675 goto fail;
2676 }
2677 p = lock_user_string(arg2);
2678 if (!access_ok(VERIFY_READ, p, 1))
2679 ret = -EFAULT;
2680 else
2681 ret = get_errno(sys_unlinkat(arg1, p, arg3));
2682 if (p)
2683 unlock_user(p, arg2, 0);
2684 break;
2685#endif
bellard31e31b82003-02-18 22:55:36 +00002686 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00002687 {
2688 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00002689 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00002690 abi_ulong gp;
2691 abi_ulong guest_argp;
2692 abi_ulong guest_envp;
2693 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00002694 char **q;
2695
bellardf7341ff2003-03-30 21:00:25 +00002696 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00002697 guest_argp = arg2;
2698 for (gp = guest_argp; tgetl(gp); gp++)
bellard7854b052003-03-29 17:22:23 +00002699 argc++;
bellardf7341ff2003-03-30 21:00:25 +00002700 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00002701 guest_envp = arg3;
2702 for (gp = guest_envp; tgetl(gp); gp++)
bellard7854b052003-03-29 17:22:23 +00002703 envc++;
2704
bellardf7341ff2003-03-30 21:00:25 +00002705 argp = alloca((argc + 1) * sizeof(void *));
2706 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00002707
pbrook53a59602006-03-25 19:31:22 +00002708 for (gp = guest_argp, q = argp; ;
blueswir1992f48a2007-10-14 16:27:31 +00002709 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002710 addr = tgetl(gp);
2711 if (!addr)
2712 break;
2713 *q = lock_user_string(addr);
2714 }
bellardf7341ff2003-03-30 21:00:25 +00002715 *q = NULL;
2716
pbrook53a59602006-03-25 19:31:22 +00002717 for (gp = guest_envp, q = envp; ;
blueswir1992f48a2007-10-14 16:27:31 +00002718 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002719 addr = tgetl(gp);
2720 if (!addr)
2721 break;
2722 *q = lock_user_string(addr);
2723 }
bellardf7341ff2003-03-30 21:00:25 +00002724 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00002725
pbrook53a59602006-03-25 19:31:22 +00002726 p = lock_user_string(arg1);
2727 ret = get_errno(execve(p, argp, envp));
2728 unlock_user(p, arg1, 0);
2729
2730 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00002731 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002732 addr = tgetl(gp);
2733 unlock_user(*q, addr, 0);
2734 }
2735 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00002736 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002737 addr = tgetl(gp);
2738 unlock_user(*q, addr, 0);
2739 }
bellard7854b052003-03-29 17:22:23 +00002740 }
bellard31e31b82003-02-18 22:55:36 +00002741 break;
2742 case TARGET_NR_chdir:
pbrook53a59602006-03-25 19:31:22 +00002743 p = lock_user_string(arg1);
2744 ret = get_errno(chdir(p));
2745 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002746 break;
bellarda315a142005-01-30 22:59:18 +00002747#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00002748 case TARGET_NR_time:
2749 {
pbrook53a59602006-03-25 19:31:22 +00002750 time_t host_time;
2751 ret = get_errno(time(&host_time));
2752 if (!is_error(ret) && arg1)
2753 tputl(arg1, host_time);
bellard31e31b82003-02-18 22:55:36 +00002754 }
2755 break;
bellarda315a142005-01-30 22:59:18 +00002756#endif
bellard31e31b82003-02-18 22:55:36 +00002757 case TARGET_NR_mknod:
pbrook53a59602006-03-25 19:31:22 +00002758 p = lock_user_string(arg1);
2759 ret = get_errno(mknod(p, arg2, arg3));
2760 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002761 break;
ths75ac37a2007-09-24 09:23:05 +00002762#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
2763 case TARGET_NR_mknodat:
2764 if (!arg2) {
2765 ret = -EFAULT;
2766 goto fail;
2767 }
2768 p = lock_user_string(arg2);
2769 if (!access_ok(VERIFY_READ, p, 1))
2770 ret = -EFAULT;
2771 else
2772 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
2773 if (p)
2774 unlock_user(p, arg2, 0);
2775 break;
2776#endif
bellard31e31b82003-02-18 22:55:36 +00002777 case TARGET_NR_chmod:
pbrook53a59602006-03-25 19:31:22 +00002778 p = lock_user_string(arg1);
2779 ret = get_errno(chmod(p, arg2));
2780 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002781 break;
bellardebc05482003-09-30 21:08:41 +00002782#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00002783 case TARGET_NR_break:
2784 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002785#endif
2786#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00002787 case TARGET_NR_oldstat:
2788 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002789#endif
bellard31e31b82003-02-18 22:55:36 +00002790 case TARGET_NR_lseek:
2791 ret = get_errno(lseek(arg1, arg2, arg3));
2792 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002793#ifdef TARGET_NR_getxpid
2794 case TARGET_NR_getxpid:
2795#else
bellard31e31b82003-02-18 22:55:36 +00002796 case TARGET_NR_getpid:
j_mayer7a3148a2007-04-05 07:13:51 +00002797#endif
bellard31e31b82003-02-18 22:55:36 +00002798 ret = get_errno(getpid());
2799 break;
2800 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00002801 {
2802 /* need to look at the data field */
2803 void *p2, *p3;
2804 p = lock_user_string(arg1);
2805 p2 = lock_user_string(arg2);
2806 p3 = lock_user_string(arg3);
2807 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, (const void *)arg5));
2808 unlock_user(p, arg1, 0);
2809 unlock_user(p2, arg2, 0);
2810 unlock_user(p3, arg3, 0);
2811 break;
2812 }
thse5febef2007-04-01 18:31:35 +00002813#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00002814 case TARGET_NR_umount:
pbrook53a59602006-03-25 19:31:22 +00002815 p = lock_user_string(arg1);
2816 ret = get_errno(umount(p));
2817 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002818 break;
thse5febef2007-04-01 18:31:35 +00002819#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002820#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002821 case TARGET_NR_stime:
2822 {
pbrook53a59602006-03-25 19:31:22 +00002823 time_t host_time;
2824 host_time = tgetl(arg1);
2825 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00002826 }
2827 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002828#endif
bellard31e31b82003-02-18 22:55:36 +00002829 case TARGET_NR_ptrace:
2830 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00002831#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002832 case TARGET_NR_alarm:
2833 ret = alarm(arg1);
2834 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002835#endif
bellardebc05482003-09-30 21:08:41 +00002836#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00002837 case TARGET_NR_oldfstat:
2838 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002839#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002840#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002841 case TARGET_NR_pause:
2842 ret = get_errno(pause());
2843 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002844#endif
thse5febef2007-04-01 18:31:35 +00002845#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00002846 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00002847 {
pbrook53a59602006-03-25 19:31:22 +00002848 struct utimbuf tbuf, *host_tbuf;
2849 struct target_utimbuf *target_tbuf;
2850 if (arg2) {
2851 lock_user_struct(target_tbuf, arg2, 1);
2852 tbuf.actime = tswapl(target_tbuf->actime);
2853 tbuf.modtime = tswapl(target_tbuf->modtime);
2854 unlock_user_struct(target_tbuf, arg2, 0);
2855 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00002856 } else {
pbrook53a59602006-03-25 19:31:22 +00002857 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00002858 }
pbrook53a59602006-03-25 19:31:22 +00002859 p = lock_user_string(arg1);
2860 ret = get_errno(utime(p, host_tbuf));
2861 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00002862 }
2863 break;
thse5febef2007-04-01 18:31:35 +00002864#endif
bellard978a66f2004-12-06 22:58:05 +00002865 case TARGET_NR_utimes:
2866 {
bellard978a66f2004-12-06 22:58:05 +00002867 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00002868 if (arg2) {
2869 target_to_host_timeval(&tv[0], arg2);
2870 target_to_host_timeval(&tv[1],
2871 arg2 + sizeof (struct target_timeval));
bellard978a66f2004-12-06 22:58:05 +00002872 tvp = tv;
2873 } else {
2874 tvp = NULL;
2875 }
pbrook53a59602006-03-25 19:31:22 +00002876 p = lock_user_string(arg1);
2877 ret = get_errno(utimes(p, tvp));
2878 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00002879 }
2880 break;
bellardebc05482003-09-30 21:08:41 +00002881#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00002882 case TARGET_NR_stty:
2883 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002884#endif
2885#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00002886 case TARGET_NR_gtty:
2887 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002888#endif
bellard31e31b82003-02-18 22:55:36 +00002889 case TARGET_NR_access:
pbrook53a59602006-03-25 19:31:22 +00002890 p = lock_user_string(arg1);
2891 ret = get_errno(access(p, arg2));
2892 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002893 break;
ths92a34c12007-09-24 09:27:49 +00002894#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
2895 case TARGET_NR_faccessat:
2896 if (!arg2) {
2897 ret = -EFAULT;
2898 goto fail;
2899 }
2900 p = lock_user_string(arg2);
2901 if (!access_ok(VERIFY_READ, p, 1))
2902 ret = -EFAULT;
2903 else
2904 ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
2905 if (p)
2906 unlock_user(p, arg2, 0);
2907 break;
2908#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002909#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002910 case TARGET_NR_nice:
2911 ret = get_errno(nice(arg1));
2912 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002913#endif
bellardebc05482003-09-30 21:08:41 +00002914#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00002915 case TARGET_NR_ftime:
2916 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002917#endif
bellard31e31b82003-02-18 22:55:36 +00002918 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00002919 sync();
2920 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00002921 break;
2922 case TARGET_NR_kill:
2923 ret = get_errno(kill(arg1, arg2));
2924 break;
2925 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00002926 {
2927 void *p2;
2928 p = lock_user_string(arg1);
2929 p2 = lock_user_string(arg2);
2930 ret = get_errno(rename(p, p2));
2931 unlock_user(p2, arg2, 0);
2932 unlock_user(p, arg1, 0);
2933 }
bellard31e31b82003-02-18 22:55:36 +00002934 break;
ths722183f2007-09-24 09:24:37 +00002935#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
2936 case TARGET_NR_renameat:
2937 if (!arg2 || !arg4) {
2938 ret = -EFAULT;
2939 goto fail;
2940 }
2941 {
2942 void *p2 = NULL;
2943 p = lock_user_string(arg2);
2944 p2 = lock_user_string(arg4);
2945 if (!access_ok(VERIFY_READ, p, 1)
2946 || !access_ok(VERIFY_READ, p2, 1))
2947 ret = -EFAULT;
2948 else
2949 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
2950 if (p2)
2951 unlock_user(p2, arg4, 0);
2952 if (p)
2953 unlock_user(p, arg2, 0);
2954 }
2955 break;
2956#endif
bellard31e31b82003-02-18 22:55:36 +00002957 case TARGET_NR_mkdir:
pbrook53a59602006-03-25 19:31:22 +00002958 p = lock_user_string(arg1);
2959 ret = get_errno(mkdir(p, arg2));
2960 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002961 break;
ths4472ad02007-09-24 09:22:32 +00002962#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
2963 case TARGET_NR_mkdirat:
2964 if (!arg2) {
2965 ret = -EFAULT;
2966 goto fail;
2967 }
2968 p = lock_user_string(arg2);
2969 if (!access_ok(VERIFY_READ, p, 1))
2970 ret = -EFAULT;
2971 else
2972 ret = get_errno(sys_mkdirat(arg1, p, arg3));
2973 if (p)
2974 unlock_user(p, arg2, 0);
2975 break;
2976#endif
bellard31e31b82003-02-18 22:55:36 +00002977 case TARGET_NR_rmdir:
pbrook53a59602006-03-25 19:31:22 +00002978 p = lock_user_string(arg1);
2979 ret = get_errno(rmdir(p));
2980 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002981 break;
2982 case TARGET_NR_dup:
2983 ret = get_errno(dup(arg1));
2984 break;
2985 case TARGET_NR_pipe:
2986 {
pbrook53a59602006-03-25 19:31:22 +00002987 int host_pipe[2];
2988 ret = get_errno(pipe(host_pipe));
bellard31e31b82003-02-18 22:55:36 +00002989 if (!is_error(ret)) {
thsc12ab052007-06-01 11:50:36 +00002990#if defined(TARGET_MIPS)
thsead93602007-09-06 00:18:15 +00002991 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
2992 env->gpr[3][env->current_tc] = host_pipe[1];
thsc12ab052007-06-01 11:50:36 +00002993 ret = host_pipe[0];
2994#else
pbrook53a59602006-03-25 19:31:22 +00002995 tput32(arg1, host_pipe[0]);
2996 tput32(arg1 + 4, host_pipe[1]);
thsc12ab052007-06-01 11:50:36 +00002997#endif
bellard31e31b82003-02-18 22:55:36 +00002998 }
2999 }
3000 break;
3001 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00003002 {
pbrook53a59602006-03-25 19:31:22 +00003003 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00003004 struct tms tms;
3005 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00003006 if (arg1) {
3007 tmsp = lock_user(arg1, sizeof(struct target_tms), 0);
bellardc596ed12003-07-13 17:32:31 +00003008 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3009 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3010 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3011 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00003012 }
bellardc596ed12003-07-13 17:32:31 +00003013 if (!is_error(ret))
3014 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00003015 }
3016 break;
bellardebc05482003-09-30 21:08:41 +00003017#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00003018 case TARGET_NR_prof:
3019 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003020#endif
thse5febef2007-04-01 18:31:35 +00003021#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00003022 case TARGET_NR_signal:
3023 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003024#endif
bellard31e31b82003-02-18 22:55:36 +00003025 case TARGET_NR_acct:
pbrook24836682006-04-16 14:14:53 +00003026 p = lock_user_string(arg1);
3027 ret = get_errno(acct(path(p)));
3028 unlock_user(p, arg1, 0);
3029 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003030#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003031 case TARGET_NR_umount2:
pbrook53a59602006-03-25 19:31:22 +00003032 p = lock_user_string(arg1);
3033 ret = get_errno(umount2(p, arg2));
3034 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003035 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003036#endif
bellardebc05482003-09-30 21:08:41 +00003037#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00003038 case TARGET_NR_lock:
3039 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003040#endif
bellard31e31b82003-02-18 22:55:36 +00003041 case TARGET_NR_ioctl:
3042 ret = do_ioctl(arg1, arg2, arg3);
3043 break;
3044 case TARGET_NR_fcntl:
bellard7775e9e2003-05-14 22:46:48 +00003045 ret = get_errno(do_fcntl(arg1, arg2, arg3));
bellard31e31b82003-02-18 22:55:36 +00003046 break;
bellardebc05482003-09-30 21:08:41 +00003047#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00003048 case TARGET_NR_mpx:
3049 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003050#endif
bellard31e31b82003-02-18 22:55:36 +00003051 case TARGET_NR_setpgid:
3052 ret = get_errno(setpgid(arg1, arg2));
3053 break;
bellardebc05482003-09-30 21:08:41 +00003054#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00003055 case TARGET_NR_ulimit:
3056 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003057#endif
3058#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00003059 case TARGET_NR_oldolduname:
3060 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003061#endif
bellard31e31b82003-02-18 22:55:36 +00003062 case TARGET_NR_umask:
3063 ret = get_errno(umask(arg1));
3064 break;
3065 case TARGET_NR_chroot:
pbrook53a59602006-03-25 19:31:22 +00003066 p = lock_user_string(arg1);
3067 ret = get_errno(chroot(p));
3068 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003069 break;
3070 case TARGET_NR_ustat:
3071 goto unimplemented;
3072 case TARGET_NR_dup2:
3073 ret = get_errno(dup2(arg1, arg2));
3074 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003075#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003076 case TARGET_NR_getppid:
3077 ret = get_errno(getppid());
3078 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003079#endif
bellard31e31b82003-02-18 22:55:36 +00003080 case TARGET_NR_getpgrp:
3081 ret = get_errno(getpgrp());
3082 break;
3083 case TARGET_NR_setsid:
3084 ret = get_errno(setsid());
3085 break;
thse5febef2007-04-01 18:31:35 +00003086#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00003087 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00003088 {
ths388bb212007-05-13 13:58:00 +00003089#if !defined(TARGET_MIPS)
pbrook53a59602006-03-25 19:31:22 +00003090 struct target_old_sigaction *old_act;
bellard66fb9762003-03-23 01:06:05 +00003091 struct target_sigaction act, oact, *pact;
pbrook53a59602006-03-25 19:31:22 +00003092 if (arg2) {
3093 lock_user_struct(old_act, arg2, 1);
bellard66fb9762003-03-23 01:06:05 +00003094 act._sa_handler = old_act->_sa_handler;
3095 target_siginitset(&act.sa_mask, old_act->sa_mask);
3096 act.sa_flags = old_act->sa_flags;
3097 act.sa_restorer = old_act->sa_restorer;
pbrook53a59602006-03-25 19:31:22 +00003098 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003099 pact = &act;
3100 } else {
3101 pact = NULL;
3102 }
3103 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00003104 if (!is_error(ret) && arg3) {
3105 lock_user_struct(old_act, arg3, 0);
3106 old_act->_sa_handler = oact._sa_handler;
3107 old_act->sa_mask = oact.sa_mask.sig[0];
3108 old_act->sa_flags = oact.sa_flags;
3109 old_act->sa_restorer = oact.sa_restorer;
3110 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00003111 }
ths388bb212007-05-13 13:58:00 +00003112#else
bellard106ec872006-06-27 21:08:10 +00003113 struct target_sigaction act, oact, *pact, *old_act;
3114
3115 if (arg2) {
3116 lock_user_struct(old_act, arg2, 1);
3117 act._sa_handler = old_act->_sa_handler;
3118 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3119 act.sa_flags = old_act->sa_flags;
3120 unlock_user_struct(old_act, arg2, 0);
3121 pact = &act;
3122 } else {
3123 pact = NULL;
3124 }
3125
3126 ret = get_errno(do_sigaction(arg1, pact, &oact));
3127
3128 if (!is_error(ret) && arg3) {
3129 lock_user_struct(old_act, arg3, 0);
3130 old_act->_sa_handler = oact._sa_handler;
3131 old_act->sa_flags = oact.sa_flags;
3132 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3133 old_act->sa_mask.sig[1] = 0;
3134 old_act->sa_mask.sig[2] = 0;
3135 old_act->sa_mask.sig[3] = 0;
3136 unlock_user_struct(old_act, arg3, 1);
3137 }
ths388bb212007-05-13 13:58:00 +00003138#endif
bellard31e31b82003-02-18 22:55:36 +00003139 }
3140 break;
thse5febef2007-04-01 18:31:35 +00003141#endif
bellard66fb9762003-03-23 01:06:05 +00003142 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00003143 {
3144 struct target_sigaction *act;
3145 struct target_sigaction *oact;
3146
3147 if (arg2)
3148 lock_user_struct(act, arg2, 1);
3149 else
3150 act = NULL;
3151 if (arg3)
3152 lock_user_struct(oact, arg3, 0);
3153 else
3154 oact = NULL;
3155 ret = get_errno(do_sigaction(arg1, act, oact));
3156 if (arg2)
3157 unlock_user_struct(act, arg2, 0);
3158 if (arg3)
3159 unlock_user_struct(oact, arg3, 1);
3160 }
bellard66fb9762003-03-23 01:06:05 +00003161 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003162#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003163 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00003164 {
3165 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00003166 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00003167 sigprocmask(0, NULL, &cur_set);
3168 host_to_target_old_sigset(&target_set, &cur_set);
3169 ret = target_set;
3170 }
3171 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003172#endif
3173#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003174 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00003175 {
3176 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00003177 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00003178 sigprocmask(0, NULL, &cur_set);
3179 target_to_host_old_sigset(&set, &target_set);
3180 sigorset(&set, &set, &cur_set);
3181 sigprocmask(SIG_SETMASK, &set, &oset);
3182 host_to_target_old_sigset(&target_set, &oset);
3183 ret = target_set;
3184 }
3185 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003186#endif
thse5febef2007-04-01 18:31:35 +00003187#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00003188 case TARGET_NR_sigprocmask:
3189 {
3190 int how = arg1;
3191 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003192
pbrook53a59602006-03-25 19:31:22 +00003193 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003194 switch(how) {
3195 case TARGET_SIG_BLOCK:
3196 how = SIG_BLOCK;
3197 break;
3198 case TARGET_SIG_UNBLOCK:
3199 how = SIG_UNBLOCK;
3200 break;
3201 case TARGET_SIG_SETMASK:
3202 how = SIG_SETMASK;
3203 break;
3204 default:
3205 ret = -EINVAL;
3206 goto fail;
3207 }
pbrook53a59602006-03-25 19:31:22 +00003208 p = lock_user(arg2, sizeof(target_sigset_t), 1);
3209 target_to_host_old_sigset(&set, p);
3210 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003211 set_ptr = &set;
3212 } else {
3213 how = 0;
3214 set_ptr = NULL;
3215 }
3216 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003217 if (!is_error(ret) && arg3) {
3218 p = lock_user(arg3, sizeof(target_sigset_t), 0);
3219 host_to_target_old_sigset(p, &oldset);
3220 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003221 }
3222 }
3223 break;
thse5febef2007-04-01 18:31:35 +00003224#endif
bellard66fb9762003-03-23 01:06:05 +00003225 case TARGET_NR_rt_sigprocmask:
3226 {
3227 int how = arg1;
3228 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003229
pbrook53a59602006-03-25 19:31:22 +00003230 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003231 switch(how) {
3232 case TARGET_SIG_BLOCK:
3233 how = SIG_BLOCK;
3234 break;
3235 case TARGET_SIG_UNBLOCK:
3236 how = SIG_UNBLOCK;
3237 break;
3238 case TARGET_SIG_SETMASK:
3239 how = SIG_SETMASK;
3240 break;
3241 default:
3242 ret = -EINVAL;
3243 goto fail;
3244 }
pbrook53a59602006-03-25 19:31:22 +00003245 p = lock_user(arg2, sizeof(target_sigset_t), 1);
3246 target_to_host_sigset(&set, p);
3247 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003248 set_ptr = &set;
3249 } else {
3250 how = 0;
3251 set_ptr = NULL;
3252 }
3253 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003254 if (!is_error(ret) && arg3) {
3255 p = lock_user(arg3, sizeof(target_sigset_t), 0);
3256 host_to_target_sigset(p, &oldset);
3257 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003258 }
3259 }
3260 break;
thse5febef2007-04-01 18:31:35 +00003261#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00003262 case TARGET_NR_sigpending:
3263 {
3264 sigset_t set;
3265 ret = get_errno(sigpending(&set));
3266 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003267 p = lock_user(arg1, sizeof(target_sigset_t), 0);
3268 host_to_target_old_sigset(p, &set);
3269 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003270 }
3271 }
3272 break;
thse5febef2007-04-01 18:31:35 +00003273#endif
bellard66fb9762003-03-23 01:06:05 +00003274 case TARGET_NR_rt_sigpending:
3275 {
3276 sigset_t set;
3277 ret = get_errno(sigpending(&set));
3278 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003279 p = lock_user(arg1, sizeof(target_sigset_t), 0);
3280 host_to_target_sigset(p, &set);
3281 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003282 }
3283 }
3284 break;
thse5febef2007-04-01 18:31:35 +00003285#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00003286 case TARGET_NR_sigsuspend:
3287 {
3288 sigset_t set;
pbrook53a59602006-03-25 19:31:22 +00003289 p = lock_user(arg1, sizeof(target_sigset_t), 1);
3290 target_to_host_old_sigset(&set, p);
3291 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003292 ret = get_errno(sigsuspend(&set));
3293 }
3294 break;
thse5febef2007-04-01 18:31:35 +00003295#endif
bellard66fb9762003-03-23 01:06:05 +00003296 case TARGET_NR_rt_sigsuspend:
3297 {
3298 sigset_t set;
pbrook53a59602006-03-25 19:31:22 +00003299 p = lock_user(arg1, sizeof(target_sigset_t), 1);
3300 target_to_host_sigset(&set, p);
3301 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003302 ret = get_errno(sigsuspend(&set));
3303 }
3304 break;
3305 case TARGET_NR_rt_sigtimedwait:
3306 {
bellard66fb9762003-03-23 01:06:05 +00003307 sigset_t set;
3308 struct timespec uts, *puts;
3309 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00003310
pbrook53a59602006-03-25 19:31:22 +00003311 p = lock_user(arg1, sizeof(target_sigset_t), 1);
3312 target_to_host_sigset(&set, p);
3313 unlock_user(p, arg1, 0);
3314 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00003315 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00003316 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00003317 } else {
3318 puts = NULL;
3319 }
3320 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00003321 if (!is_error(ret) && arg2) {
3322 p = lock_user(arg2, sizeof(target_sigset_t), 0);
3323 host_to_target_siginfo(p, &uinfo);
3324 unlock_user(p, arg2, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003325 }
3326 }
3327 break;
3328 case TARGET_NR_rt_sigqueueinfo:
3329 {
3330 siginfo_t uinfo;
pbrook53a59602006-03-25 19:31:22 +00003331 p = lock_user(arg3, sizeof(target_sigset_t), 1);
3332 target_to_host_siginfo(&uinfo, p);
3333 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003334 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3335 }
3336 break;
thse5febef2007-04-01 18:31:35 +00003337#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00003338 case TARGET_NR_sigreturn:
3339 /* NOTE: ret is eax, so not transcoding must be done */
3340 ret = do_sigreturn(cpu_env);
3341 break;
thse5febef2007-04-01 18:31:35 +00003342#endif
bellard66fb9762003-03-23 01:06:05 +00003343 case TARGET_NR_rt_sigreturn:
3344 /* NOTE: ret is eax, so not transcoding must be done */
3345 ret = do_rt_sigreturn(cpu_env);
3346 break;
bellard31e31b82003-02-18 22:55:36 +00003347 case TARGET_NR_sethostname:
pbrook53a59602006-03-25 19:31:22 +00003348 p = lock_user_string(arg1);
3349 ret = get_errno(sethostname(p, arg2));
3350 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003351 break;
3352 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00003353 {
3354 /* XXX: convert resource ? */
3355 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003356 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003357 struct rlimit rlim;
pbrook53a59602006-03-25 19:31:22 +00003358 lock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00003359 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3360 rlim.rlim_max = tswapl(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00003361 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00003362 ret = get_errno(setrlimit(resource, &rlim));
3363 }
3364 break;
bellard31e31b82003-02-18 22:55:36 +00003365 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00003366 {
3367 /* XXX: convert resource ? */
3368 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003369 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003370 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00003371
bellard9de5e442003-03-23 16:49:39 +00003372 ret = get_errno(getrlimit(resource, &rlim));
3373 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003374 lock_user_struct(target_rlim, arg2, 0);
3375 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3376 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3377 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00003378 }
3379 }
3380 break;
bellard31e31b82003-02-18 22:55:36 +00003381 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00003382 {
3383 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00003384 ret = get_errno(getrusage(arg1, &rusage));
3385 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003386 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00003387 }
3388 }
3389 break;
bellard31e31b82003-02-18 22:55:36 +00003390 case TARGET_NR_gettimeofday:
3391 {
bellard31e31b82003-02-18 22:55:36 +00003392 struct timeval tv;
3393 ret = get_errno(gettimeofday(&tv, NULL));
3394 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003395 host_to_target_timeval(arg1, &tv);
bellard31e31b82003-02-18 22:55:36 +00003396 }
3397 }
3398 break;
3399 case TARGET_NR_settimeofday:
3400 {
bellard31e31b82003-02-18 22:55:36 +00003401 struct timeval tv;
pbrook53a59602006-03-25 19:31:22 +00003402 target_to_host_timeval(&tv, arg1);
bellard31e31b82003-02-18 22:55:36 +00003403 ret = get_errno(settimeofday(&tv, NULL));
3404 }
3405 break;
bellard048f6b42005-11-26 18:47:20 +00003406#ifdef TARGET_NR_select
bellard31e31b82003-02-18 22:55:36 +00003407 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00003408 {
pbrook53a59602006-03-25 19:31:22 +00003409 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00003410 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00003411 long nsel;
3412
3413 lock_user_struct(sel, arg1, 1);
3414 nsel = tswapl(sel->n);
3415 inp = tswapl(sel->inp);
3416 outp = tswapl(sel->outp);
3417 exp = tswapl(sel->exp);
3418 tvp = tswapl(sel->tvp);
3419 unlock_user_struct(sel, arg1, 0);
3420 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00003421 }
3422 break;
bellard048f6b42005-11-26 18:47:20 +00003423#endif
bellard31e31b82003-02-18 22:55:36 +00003424 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00003425 {
3426 void *p2;
3427 p = lock_user_string(arg1);
3428 p2 = lock_user_string(arg2);
3429 ret = get_errno(symlink(p, p2));
3430 unlock_user(p2, arg2, 0);
3431 unlock_user(p, arg1, 0);
3432 }
bellard31e31b82003-02-18 22:55:36 +00003433 break;
thsf0b62432007-09-24 09:25:40 +00003434#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
3435 case TARGET_NR_symlinkat:
3436 if (!arg1 || !arg3) {
3437 ret = -EFAULT;
3438 goto fail;
3439 }
3440 {
3441 void *p2 = NULL;
3442 p = lock_user_string(arg1);
3443 p2 = lock_user_string(arg3);
3444 if (!access_ok(VERIFY_READ, p, 1)
3445 || !access_ok(VERIFY_READ, p2, 1))
3446 ret = -EFAULT;
3447 else
3448 ret = get_errno(sys_symlinkat(p, arg2, p2));
3449 if (p2)
3450 unlock_user(p2, arg3, 0);
3451 if (p)
3452 unlock_user(p, arg1, 0);
3453 }
3454 break;
3455#endif
bellardebc05482003-09-30 21:08:41 +00003456#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00003457 case TARGET_NR_oldlstat:
3458 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003459#endif
bellard31e31b82003-02-18 22:55:36 +00003460 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00003461 {
3462 void *p2;
3463 p = lock_user_string(arg1);
3464 p2 = lock_user(arg2, arg3, 0);
3465 ret = get_errno(readlink(path(p), p2, arg3));
3466 unlock_user(p2, arg2, ret);
3467 unlock_user(p, arg1, 0);
3468 }
bellard31e31b82003-02-18 22:55:36 +00003469 break;
ths5e0ccb12007-09-24 09:26:10 +00003470#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
3471 case TARGET_NR_readlinkat:
3472 if (!arg2 || !arg3) {
3473 ret = -EFAULT;
3474 goto fail;
3475 }
3476 {
3477 void *p2 = NULL;
3478 p = lock_user_string(arg2);
3479 p2 = lock_user(arg3, arg4, 0);
3480 if (!access_ok(VERIFY_READ, p, 1)
3481 || !access_ok(VERIFY_READ, p2, 1))
3482 ret = -EFAULT;
3483 else
3484 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
3485 if (p2)
3486 unlock_user(p2, arg3, ret);
3487 if (p)
3488 unlock_user(p, arg2, 0);
3489 }
3490 break;
3491#endif
thse5febef2007-04-01 18:31:35 +00003492#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00003493 case TARGET_NR_uselib:
3494 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003495#endif
3496#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00003497 case TARGET_NR_swapon:
pbrook53a59602006-03-25 19:31:22 +00003498 p = lock_user_string(arg1);
3499 ret = get_errno(swapon(p, arg2));
3500 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003501 break;
thse5febef2007-04-01 18:31:35 +00003502#endif
bellard31e31b82003-02-18 22:55:36 +00003503 case TARGET_NR_reboot:
3504 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003505#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00003506 case TARGET_NR_readdir:
3507 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003508#endif
3509#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00003510 case TARGET_NR_mmap:
ths48733d12007-10-08 13:36:46 +00003511#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
bellard31e31b82003-02-18 22:55:36 +00003512 {
blueswir1992f48a2007-10-14 16:27:31 +00003513 abi_ulong *v;
3514 abi_ulong v1, v2, v3, v4, v5, v6;
3515 v = lock_user(arg1, 6 * sizeof(abi_ulong), 1);
pbrook53a59602006-03-25 19:31:22 +00003516 v1 = tswapl(v[0]);
3517 v2 = tswapl(v[1]);
3518 v3 = tswapl(v[2]);
3519 v4 = tswapl(v[3]);
3520 v5 = tswapl(v[4]);
3521 v6 = tswapl(v[5]);
3522 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00003523 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00003524 target_to_host_bitmask(v4, mmap_flags_tbl),
3525 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00003526 }
bellard31e31b82003-02-18 22:55:36 +00003527#else
ths5fafdf22007-09-16 21:08:06 +00003528 ret = get_errno(target_mmap(arg1, arg2, arg3,
3529 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00003530 arg5,
3531 arg6));
bellard31e31b82003-02-18 22:55:36 +00003532#endif
bellard6fb883e2003-07-09 17:12:39 +00003533 break;
thse5febef2007-04-01 18:31:35 +00003534#endif
bellarda315a142005-01-30 22:59:18 +00003535#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00003536 case TARGET_NR_mmap2:
ths0d3267a2006-12-11 22:04:25 +00003537#if defined(TARGET_SPARC) || defined(TARGET_MIPS)
bellardc573ff62004-01-04 15:51:36 +00003538#define MMAP_SHIFT 12
3539#else
3540#define MMAP_SHIFT TARGET_PAGE_BITS
3541#endif
ths5fafdf22007-09-16 21:08:06 +00003542 ret = get_errno(target_mmap(arg1, arg2, arg3,
3543 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00003544 arg5,
bellardc573ff62004-01-04 15:51:36 +00003545 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00003546 break;
bellarda315a142005-01-30 22:59:18 +00003547#endif
bellard31e31b82003-02-18 22:55:36 +00003548 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00003549 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00003550 break;
bellard9de5e442003-03-23 16:49:39 +00003551 case TARGET_NR_mprotect:
bellard54936002003-05-13 00:25:15 +00003552 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00003553 break;
thse5febef2007-04-01 18:31:35 +00003554#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00003555 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00003556 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00003557 break;
thse5febef2007-04-01 18:31:35 +00003558#endif
pbrook53a59602006-03-25 19:31:22 +00003559 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00003560#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00003561 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00003562 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00003563 break;
thse5febef2007-04-01 18:31:35 +00003564#endif
3565#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00003566 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00003567 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00003568 break;
thse5febef2007-04-01 18:31:35 +00003569#endif
3570#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00003571 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00003572 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00003573 break;
thse5febef2007-04-01 18:31:35 +00003574#endif
3575#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00003576 case TARGET_NR_mlockall:
3577 ret = get_errno(mlockall(arg1));
3578 break;
thse5febef2007-04-01 18:31:35 +00003579#endif
3580#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00003581 case TARGET_NR_munlockall:
3582 ret = get_errno(munlockall());
3583 break;
thse5febef2007-04-01 18:31:35 +00003584#endif
bellard31e31b82003-02-18 22:55:36 +00003585 case TARGET_NR_truncate:
pbrook53a59602006-03-25 19:31:22 +00003586 p = lock_user_string(arg1);
3587 ret = get_errno(truncate(p, arg2));
3588 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003589 break;
3590 case TARGET_NR_ftruncate:
3591 ret = get_errno(ftruncate(arg1, arg2));
3592 break;
3593 case TARGET_NR_fchmod:
3594 ret = get_errno(fchmod(arg1, arg2));
3595 break;
ths814d7972007-09-24 09:26:51 +00003596#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
3597 case TARGET_NR_fchmodat:
3598 if (!arg2) {
3599 ret = -EFAULT;
3600 goto fail;
3601 }
3602 p = lock_user_string(arg2);
3603 if (!access_ok(VERIFY_READ, p, 1))
3604 ret = -EFAULT;
3605 else
3606 ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
3607 if (p)
3608 unlock_user(p, arg2, 0);
3609 break;
3610#endif
bellard31e31b82003-02-18 22:55:36 +00003611 case TARGET_NR_getpriority:
thsc6cda172007-10-09 03:42:34 +00003612 /* libc does special remapping of the return value of
3613 * sys_getpriority() so it's just easiest to call
3614 * sys_getpriority() directly rather than through libc. */
3615 ret = sys_getpriority(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00003616 break;
3617 case TARGET_NR_setpriority:
3618 ret = get_errno(setpriority(arg1, arg2, arg3));
3619 break;
bellardebc05482003-09-30 21:08:41 +00003620#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00003621 case TARGET_NR_profil:
3622 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003623#endif
bellard31e31b82003-02-18 22:55:36 +00003624 case TARGET_NR_statfs:
pbrook53a59602006-03-25 19:31:22 +00003625 p = lock_user_string(arg1);
3626 ret = get_errno(statfs(path(p), &stfs));
3627 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003628 convert_statfs:
3629 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003630 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00003631
pbrook53a59602006-03-25 19:31:22 +00003632 lock_user_struct(target_stfs, arg2, 0);
3633 /* ??? put_user is probably wrong. */
bellard56c8f682005-11-28 22:28:41 +00003634 put_user(stfs.f_type, &target_stfs->f_type);
3635 put_user(stfs.f_bsize, &target_stfs->f_bsize);
3636 put_user(stfs.f_blocks, &target_stfs->f_blocks);
3637 put_user(stfs.f_bfree, &target_stfs->f_bfree);
3638 put_user(stfs.f_bavail, &target_stfs->f_bavail);
3639 put_user(stfs.f_files, &target_stfs->f_files);
3640 put_user(stfs.f_ffree, &target_stfs->f_ffree);
ths4ce6f8d2007-07-20 15:54:27 +00003641 put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3642 put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
bellard56c8f682005-11-28 22:28:41 +00003643 put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00003644 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00003645 }
3646 break;
3647 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00003648 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00003649 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00003650#ifdef TARGET_NR_statfs64
3651 case TARGET_NR_statfs64:
pbrook53a59602006-03-25 19:31:22 +00003652 p = lock_user_string(arg1);
3653 ret = get_errno(statfs(path(p), &stfs));
3654 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00003655 convert_statfs64:
3656 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003657 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00003658
pbrook53a59602006-03-25 19:31:22 +00003659 lock_user_struct(target_stfs, arg3, 0);
3660 /* ??? put_user is probably wrong. */
bellard56c8f682005-11-28 22:28:41 +00003661 put_user(stfs.f_type, &target_stfs->f_type);
3662 put_user(stfs.f_bsize, &target_stfs->f_bsize);
3663 put_user(stfs.f_blocks, &target_stfs->f_blocks);
3664 put_user(stfs.f_bfree, &target_stfs->f_bfree);
3665 put_user(stfs.f_bavail, &target_stfs->f_bavail);
3666 put_user(stfs.f_files, &target_stfs->f_files);
3667 put_user(stfs.f_ffree, &target_stfs->f_ffree);
ths4ce6f8d2007-07-20 15:54:27 +00003668 put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3669 put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
bellard56c8f682005-11-28 22:28:41 +00003670 put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00003671 unlock_user_struct(target_stfs, arg3, 0);
bellard56c8f682005-11-28 22:28:41 +00003672 }
3673 break;
3674 case TARGET_NR_fstatfs64:
3675 ret = get_errno(fstatfs(arg1, &stfs));
3676 goto convert_statfs64;
3677#endif
bellardebc05482003-09-30 21:08:41 +00003678#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00003679 case TARGET_NR_ioperm:
3680 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003681#endif
thse5febef2007-04-01 18:31:35 +00003682#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00003683 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00003684 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00003685 break;
thse5febef2007-04-01 18:31:35 +00003686#endif
bellard3532fa72006-06-24 15:06:03 +00003687#ifdef TARGET_NR_accept
3688 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00003689 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003690 break;
3691#endif
3692#ifdef TARGET_NR_bind
3693 case TARGET_NR_bind:
3694 ret = do_bind(arg1, arg2, arg3);
3695 break;
3696#endif
3697#ifdef TARGET_NR_connect
3698 case TARGET_NR_connect:
3699 ret = do_connect(arg1, arg2, arg3);
3700 break;
3701#endif
3702#ifdef TARGET_NR_getpeername
3703 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00003704 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003705 break;
3706#endif
3707#ifdef TARGET_NR_getsockname
3708 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00003709 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003710 break;
3711#endif
3712#ifdef TARGET_NR_getsockopt
3713 case TARGET_NR_getsockopt:
3714 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
3715 break;
3716#endif
3717#ifdef TARGET_NR_listen
3718 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00003719 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00003720 break;
3721#endif
3722#ifdef TARGET_NR_recv
3723 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00003724 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00003725 break;
3726#endif
3727#ifdef TARGET_NR_recvfrom
3728 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00003729 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00003730 break;
3731#endif
3732#ifdef TARGET_NR_recvmsg
3733 case TARGET_NR_recvmsg:
3734 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
3735 break;
3736#endif
3737#ifdef TARGET_NR_send
3738 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00003739 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00003740 break;
3741#endif
3742#ifdef TARGET_NR_sendmsg
3743 case TARGET_NR_sendmsg:
3744 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
3745 break;
3746#endif
3747#ifdef TARGET_NR_sendto
3748 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00003749 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00003750 break;
3751#endif
3752#ifdef TARGET_NR_shutdown
3753 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00003754 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00003755 break;
3756#endif
3757#ifdef TARGET_NR_socket
3758 case TARGET_NR_socket:
3759 ret = do_socket(arg1, arg2, arg3);
3760 break;
3761#endif
3762#ifdef TARGET_NR_socketpair
3763 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00003764 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00003765 break;
3766#endif
3767#ifdef TARGET_NR_setsockopt
3768 case TARGET_NR_setsockopt:
3769 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
3770 break;
3771#endif
ths7494b0f2007-02-11 18:26:53 +00003772
bellard31e31b82003-02-18 22:55:36 +00003773 case TARGET_NR_syslog:
thse5574482007-02-11 20:03:13 +00003774 p = lock_user_string(arg2);
3775 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
3776 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00003777 break;
3778
bellard31e31b82003-02-18 22:55:36 +00003779 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00003780 {
bellard66fb9762003-03-23 01:06:05 +00003781 struct itimerval value, ovalue, *pvalue;
3782
pbrook53a59602006-03-25 19:31:22 +00003783 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003784 pvalue = &value;
ths5fafdf22007-09-16 21:08:06 +00003785 target_to_host_timeval(&pvalue->it_interval,
pbrook53a59602006-03-25 19:31:22 +00003786 arg2);
ths5fafdf22007-09-16 21:08:06 +00003787 target_to_host_timeval(&pvalue->it_value,
pbrook53a59602006-03-25 19:31:22 +00003788 arg2 + sizeof(struct target_timeval));
bellard66fb9762003-03-23 01:06:05 +00003789 } else {
3790 pvalue = NULL;
3791 }
3792 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00003793 if (!is_error(ret) && arg3) {
3794 host_to_target_timeval(arg3,
bellard66fb9762003-03-23 01:06:05 +00003795 &ovalue.it_interval);
pbrook53a59602006-03-25 19:31:22 +00003796 host_to_target_timeval(arg3 + sizeof(struct target_timeval),
bellard66fb9762003-03-23 01:06:05 +00003797 &ovalue.it_value);
3798 }
3799 }
3800 break;
bellard31e31b82003-02-18 22:55:36 +00003801 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00003802 {
bellard66fb9762003-03-23 01:06:05 +00003803 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00003804
bellard66fb9762003-03-23 01:06:05 +00003805 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00003806 if (!is_error(ret) && arg2) {
3807 host_to_target_timeval(arg2,
bellard66fb9762003-03-23 01:06:05 +00003808 &value.it_interval);
pbrook53a59602006-03-25 19:31:22 +00003809 host_to_target_timeval(arg2 + sizeof(struct target_timeval),
bellard66fb9762003-03-23 01:06:05 +00003810 &value.it_value);
3811 }
3812 }
3813 break;
bellard31e31b82003-02-18 22:55:36 +00003814 case TARGET_NR_stat:
pbrook53a59602006-03-25 19:31:22 +00003815 p = lock_user_string(arg1);
3816 ret = get_errno(stat(path(p), &st));
3817 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003818 goto do_stat;
3819 case TARGET_NR_lstat:
pbrook53a59602006-03-25 19:31:22 +00003820 p = lock_user_string(arg1);
3821 ret = get_errno(lstat(path(p), &st));
3822 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003823 goto do_stat;
3824 case TARGET_NR_fstat:
3825 {
3826 ret = get_errno(fstat(arg1, &st));
3827 do_stat:
3828 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003829 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00003830
pbrook53a59602006-03-25 19:31:22 +00003831 lock_user_struct(target_st, arg2, 0);
blueswir1992f48a2007-10-14 16:27:31 +00003832#if defined(TARGET_MIPS) || (defined(TARGET_SPARC64) && !defined(TARGET_ABI32))
thse3584652007-06-01 11:49:38 +00003833 target_st->st_dev = tswap32(st.st_dev);
3834#else
bellard31e31b82003-02-18 22:55:36 +00003835 target_st->st_dev = tswap16(st.st_dev);
thse3584652007-06-01 11:49:38 +00003836#endif
bellard31e31b82003-02-18 22:55:36 +00003837 target_st->st_ino = tswapl(st.st_ino);
ths7d600c82006-12-08 01:32:58 +00003838#if defined(TARGET_PPC) || defined(TARGET_MIPS)
bellard67867302003-11-23 17:05:30 +00003839 target_st->st_mode = tswapl(st.st_mode); /* XXX: check this */
3840 target_st->st_uid = tswap32(st.st_uid);
3841 target_st->st_gid = tswap32(st.st_gid);
blueswir1992f48a2007-10-14 16:27:31 +00003842#elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir11b8dd642007-07-08 10:08:24 +00003843 target_st->st_mode = tswap32(st.st_mode);
3844 target_st->st_uid = tswap32(st.st_uid);
3845 target_st->st_gid = tswap32(st.st_gid);
bellard67867302003-11-23 17:05:30 +00003846#else
bellardec86b0f2003-04-11 00:15:04 +00003847 target_st->st_mode = tswap16(st.st_mode);
bellard31e31b82003-02-18 22:55:36 +00003848 target_st->st_uid = tswap16(st.st_uid);
3849 target_st->st_gid = tswap16(st.st_gid);
bellard67867302003-11-23 17:05:30 +00003850#endif
thse3584652007-06-01 11:49:38 +00003851#if defined(TARGET_MIPS)
3852 /* If this is the same on PPC, then just merge w/ the above ifdef */
3853 target_st->st_nlink = tswapl(st.st_nlink);
3854 target_st->st_rdev = tswapl(st.st_rdev);
blueswir1992f48a2007-10-14 16:27:31 +00003855#elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir11b8dd642007-07-08 10:08:24 +00003856 target_st->st_nlink = tswap32(st.st_nlink);
3857 target_st->st_rdev = tswap32(st.st_rdev);
thse3584652007-06-01 11:49:38 +00003858#else
bellard67867302003-11-23 17:05:30 +00003859 target_st->st_nlink = tswap16(st.st_nlink);
bellard31e31b82003-02-18 22:55:36 +00003860 target_st->st_rdev = tswap16(st.st_rdev);
thse3584652007-06-01 11:49:38 +00003861#endif
bellard31e31b82003-02-18 22:55:36 +00003862 target_st->st_size = tswapl(st.st_size);
3863 target_st->st_blksize = tswapl(st.st_blksize);
3864 target_st->st_blocks = tswapl(st.st_blocks);
bellard7854b052003-03-29 17:22:23 +00003865 target_st->target_st_atime = tswapl(st.st_atime);
3866 target_st->target_st_mtime = tswapl(st.st_mtime);
3867 target_st->target_st_ctime = tswapl(st.st_ctime);
pbrook53a59602006-03-25 19:31:22 +00003868 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00003869 }
3870 }
3871 break;
bellardebc05482003-09-30 21:08:41 +00003872#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00003873 case TARGET_NR_olduname:
3874 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003875#endif
3876#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00003877 case TARGET_NR_iopl:
3878 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003879#endif
bellard31e31b82003-02-18 22:55:36 +00003880 case TARGET_NR_vhangup:
3881 ret = get_errno(vhangup());
3882 break;
bellardebc05482003-09-30 21:08:41 +00003883#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00003884 case TARGET_NR_idle:
3885 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003886#endif
bellard42ad6ae2005-01-03 22:48:11 +00003887#ifdef TARGET_NR_syscall
3888 case TARGET_NR_syscall:
3889 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
3890 break;
3891#endif
bellard31e31b82003-02-18 22:55:36 +00003892 case TARGET_NR_wait4:
3893 {
3894 int status;
blueswir1992f48a2007-10-14 16:27:31 +00003895 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00003896 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00003897 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00003898 if (target_rusage)
3899 rusage_ptr = &rusage;
3900 else
3901 rusage_ptr = NULL;
3902 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
3903 if (!is_error(ret)) {
3904 if (status_ptr)
pbrook53a59602006-03-25 19:31:22 +00003905 tputl(status_ptr, status);
bellard31e31b82003-02-18 22:55:36 +00003906 if (target_rusage) {
bellardb4091862003-05-16 15:39:34 +00003907 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00003908 }
3909 }
3910 }
3911 break;
thse5febef2007-04-01 18:31:35 +00003912#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00003913 case TARGET_NR_swapoff:
pbrook53a59602006-03-25 19:31:22 +00003914 p = lock_user_string(arg1);
3915 ret = get_errno(swapoff(p));
3916 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003917 break;
thse5febef2007-04-01 18:31:35 +00003918#endif
bellard31e31b82003-02-18 22:55:36 +00003919 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00003920 {
pbrook53a59602006-03-25 19:31:22 +00003921 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00003922 struct sysinfo value;
3923 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00003924 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00003925 {
pbrook53a59602006-03-25 19:31:22 +00003926 /* ??? __put_user is probably wrong. */
3927 lock_user_struct(target_value, arg1, 0);
bellarda5448a72004-06-19 16:59:03 +00003928 __put_user(value.uptime, &target_value->uptime);
3929 __put_user(value.loads[0], &target_value->loads[0]);
3930 __put_user(value.loads[1], &target_value->loads[1]);
3931 __put_user(value.loads[2], &target_value->loads[2]);
3932 __put_user(value.totalram, &target_value->totalram);
3933 __put_user(value.freeram, &target_value->freeram);
3934 __put_user(value.sharedram, &target_value->sharedram);
3935 __put_user(value.bufferram, &target_value->bufferram);
3936 __put_user(value.totalswap, &target_value->totalswap);
3937 __put_user(value.freeswap, &target_value->freeswap);
3938 __put_user(value.procs, &target_value->procs);
3939 __put_user(value.totalhigh, &target_value->totalhigh);
3940 __put_user(value.freehigh, &target_value->freehigh);
3941 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00003942 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00003943 }
3944 }
3945 break;
thse5febef2007-04-01 18:31:35 +00003946#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00003947 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00003948 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
3949 break;
thse5febef2007-04-01 18:31:35 +00003950#endif
bellard31e31b82003-02-18 22:55:36 +00003951 case TARGET_NR_fsync:
3952 ret = get_errno(fsync(arg1));
3953 break;
bellard31e31b82003-02-18 22:55:36 +00003954 case TARGET_NR_clone:
bellard1b6b0292003-03-22 17:31:38 +00003955 ret = get_errno(do_fork(cpu_env, arg1, arg2));
3956 break;
bellardec86b0f2003-04-11 00:15:04 +00003957#ifdef __NR_exit_group
3958 /* new thread calls */
3959 case TARGET_NR_exit_group:
bellarde9009672005-04-26 20:42:36 +00003960 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00003961 ret = get_errno(exit_group(arg1));
3962 break;
3963#endif
bellard31e31b82003-02-18 22:55:36 +00003964 case TARGET_NR_setdomainname:
pbrook53a59602006-03-25 19:31:22 +00003965 p = lock_user_string(arg1);
3966 ret = get_errno(setdomainname(p, arg2));
3967 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003968 break;
3969 case TARGET_NR_uname:
3970 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00003971 {
3972 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00003973
pbrook53a59602006-03-25 19:31:22 +00003974 lock_user_struct(buf, arg1, 0);
bellard29e619b2004-09-13 21:41:04 +00003975 ret = get_errno(sys_uname(buf));
3976 if (!is_error(ret)) {
3977 /* Overrite the native machine name with whatever is being
3978 emulated. */
3979 strcpy (buf->machine, UNAME_MACHINE);
pbrookc5937222006-05-14 11:30:38 +00003980 /* Allow the user to override the reported release. */
3981 if (qemu_uname_release && *qemu_uname_release)
3982 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00003983 }
pbrook53a59602006-03-25 19:31:22 +00003984 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00003985 }
bellard31e31b82003-02-18 22:55:36 +00003986 break;
bellard6dbad632003-03-16 18:05:05 +00003987#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00003988 case TARGET_NR_modify_ldt:
pbrook53a59602006-03-25 19:31:22 +00003989 ret = get_errno(do_modify_ldt(cpu_env, arg1, arg2, arg3));
bellard5cd43932003-03-29 16:54:36 +00003990 break;
j_mayer84409dd2007-04-06 08:56:50 +00003991#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00003992 case TARGET_NR_vm86old:
3993 goto unimplemented;
3994 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00003995 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00003996 break;
3997#endif
j_mayer84409dd2007-04-06 08:56:50 +00003998#endif
bellard31e31b82003-02-18 22:55:36 +00003999 case TARGET_NR_adjtimex:
4000 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004001#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00004002 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00004003#endif
bellard31e31b82003-02-18 22:55:36 +00004004 case TARGET_NR_init_module:
4005 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00004006#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00004007 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00004008#endif
bellard31e31b82003-02-18 22:55:36 +00004009 goto unimplemented;
4010 case TARGET_NR_quotactl:
4011 goto unimplemented;
4012 case TARGET_NR_getpgid:
4013 ret = get_errno(getpgid(arg1));
4014 break;
4015 case TARGET_NR_fchdir:
4016 ret = get_errno(fchdir(arg1));
4017 break;
j_mayer84409dd2007-04-06 08:56:50 +00004018#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00004019 case TARGET_NR_bdflush:
4020 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00004021#endif
thse5febef2007-04-01 18:31:35 +00004022#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00004023 case TARGET_NR_sysfs:
4024 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004025#endif
bellard31e31b82003-02-18 22:55:36 +00004026 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00004027 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00004028 break;
thse5febef2007-04-01 18:31:35 +00004029#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00004030 case TARGET_NR_afs_syscall:
4031 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004032#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004033#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004034 case TARGET_NR__llseek:
4035 {
bellard4f2ac232004-04-26 19:44:02 +00004036#if defined (__x86_64__)
4037 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
pbrook53a59602006-03-25 19:31:22 +00004038 tput64(arg4, ret);
bellard4f2ac232004-04-26 19:44:02 +00004039#else
bellard31e31b82003-02-18 22:55:36 +00004040 int64_t res;
4041 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
pbrook53a59602006-03-25 19:31:22 +00004042 tput64(arg4, res);
bellard4f2ac232004-04-26 19:44:02 +00004043#endif
bellard31e31b82003-02-18 22:55:36 +00004044 }
4045 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004046#endif
bellard31e31b82003-02-18 22:55:36 +00004047 case TARGET_NR_getdents:
blueswir1992f48a2007-10-14 16:27:31 +00004048#if TARGET_ABI_BITS != 32
pbrook53a59602006-03-25 19:31:22 +00004049 goto unimplemented;
bellarda315a142005-01-30 22:59:18 +00004050#warning not supported
blueswir1992f48a2007-10-14 16:27:31 +00004051#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00004052 {
pbrook53a59602006-03-25 19:31:22 +00004053 struct target_dirent *target_dirp;
bellard4add45b2003-06-05 01:52:59 +00004054 struct dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004055 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00004056
4057 dirp = malloc(count);
4058 if (!dirp)
4059 return -ENOMEM;
ths3b46e622007-09-17 08:09:54 +00004060
bellard4add45b2003-06-05 01:52:59 +00004061 ret = get_errno(sys_getdents(arg1, dirp, count));
4062 if (!is_error(ret)) {
4063 struct dirent *de;
4064 struct target_dirent *tde;
4065 int len = ret;
4066 int reclen, treclen;
4067 int count1, tnamelen;
4068
4069 count1 = 0;
4070 de = dirp;
pbrook53a59602006-03-25 19:31:22 +00004071 target_dirp = lock_user(arg2, count, 0);
bellard4add45b2003-06-05 01:52:59 +00004072 tde = target_dirp;
4073 while (len > 0) {
4074 reclen = de->d_reclen;
blueswir1992f48a2007-10-14 16:27:31 +00004075 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
bellard4add45b2003-06-05 01:52:59 +00004076 tde->d_reclen = tswap16(treclen);
4077 tde->d_ino = tswapl(de->d_ino);
4078 tde->d_off = tswapl(de->d_off);
blueswir1992f48a2007-10-14 16:27:31 +00004079 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
bellard4add45b2003-06-05 01:52:59 +00004080 if (tnamelen > 256)
4081 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00004082 /* XXX: may not be correct */
bellard4add45b2003-06-05 01:52:59 +00004083 strncpy(tde->d_name, de->d_name, tnamelen);
4084 de = (struct dirent *)((char *)de + reclen);
4085 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00004086 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00004087 count1 += treclen;
4088 }
4089 ret = count1;
4090 }
pbrook53a59602006-03-25 19:31:22 +00004091 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00004092 free(dirp);
4093 }
4094#else
bellard31e31b82003-02-18 22:55:36 +00004095 {
pbrook53a59602006-03-25 19:31:22 +00004096 struct dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004097 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00004098
pbrook53a59602006-03-25 19:31:22 +00004099 dirp = lock_user(arg2, count, 0);
bellard72f03902003-02-18 23:33:18 +00004100 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00004101 if (!is_error(ret)) {
4102 struct dirent *de;
4103 int len = ret;
4104 int reclen;
4105 de = dirp;
4106 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004107 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00004108 if (reclen > len)
4109 break;
bellard8083a3e2003-03-24 23:12:16 +00004110 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00004111 tswapls(&de->d_ino);
4112 tswapls(&de->d_off);
4113 de = (struct dirent *)((char *)de + reclen);
4114 len -= reclen;
4115 }
4116 }
pbrook53a59602006-03-25 19:31:22 +00004117 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00004118 }
bellard4add45b2003-06-05 01:52:59 +00004119#endif
bellard31e31b82003-02-18 22:55:36 +00004120 break;
ths3ae43202007-09-16 21:39:48 +00004121#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00004122 case TARGET_NR_getdents64:
4123 {
pbrook53a59602006-03-25 19:31:22 +00004124 struct dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004125 abi_long count = arg3;
pbrook53a59602006-03-25 19:31:22 +00004126 dirp = lock_user(arg2, count, 0);
bellarddab2ed92003-03-22 15:23:14 +00004127 ret = get_errno(sys_getdents64(arg1, dirp, count));
4128 if (!is_error(ret)) {
4129 struct dirent64 *de;
4130 int len = ret;
4131 int reclen;
4132 de = dirp;
4133 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004134 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00004135 if (reclen > len)
4136 break;
bellard8083a3e2003-03-24 23:12:16 +00004137 de->d_reclen = tswap16(reclen);
bellarddab2ed92003-03-22 15:23:14 +00004138 tswap64s(&de->d_ino);
4139 tswap64s(&de->d_off);
4140 de = (struct dirent64 *)((char *)de + reclen);
4141 len -= reclen;
4142 }
4143 }
pbrook53a59602006-03-25 19:31:22 +00004144 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00004145 }
4146 break;
bellarda541f292004-04-12 20:39:29 +00004147#endif /* TARGET_NR_getdents64 */
thse5febef2007-04-01 18:31:35 +00004148#ifdef TARGET_NR__newselect
bellard31e31b82003-02-18 22:55:36 +00004149 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00004150 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00004151 break;
thse5febef2007-04-01 18:31:35 +00004152#endif
4153#ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00004154 case TARGET_NR_poll:
4155 {
pbrook53a59602006-03-25 19:31:22 +00004156 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00004157 unsigned int nfds = arg2;
4158 int timeout = arg3;
4159 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00004160 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00004161
pbrook53a59602006-03-25 19:31:22 +00004162 target_pfd = lock_user(arg1, sizeof(struct target_pollfd) * nfds, 1);
bellard9de5e442003-03-23 16:49:39 +00004163 pfd = alloca(sizeof(struct pollfd) * nfds);
4164 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004165 pfd[i].fd = tswap32(target_pfd[i].fd);
4166 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00004167 }
4168 ret = get_errno(poll(pfd, nfds, timeout));
4169 if (!is_error(ret)) {
4170 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004171 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00004172 }
pbrook53a59602006-03-25 19:31:22 +00004173 ret += nfds * (sizeof(struct target_pollfd)
4174 - sizeof(struct pollfd));
bellard9de5e442003-03-23 16:49:39 +00004175 }
pbrook53a59602006-03-25 19:31:22 +00004176 unlock_user(target_pfd, arg1, ret);
bellard9de5e442003-03-23 16:49:39 +00004177 }
4178 break;
thse5febef2007-04-01 18:31:35 +00004179#endif
bellard31e31b82003-02-18 22:55:36 +00004180 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00004181 /* NOTE: the flock constant seems to be the same for every
4182 Linux platform */
4183 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00004184 break;
4185 case TARGET_NR_readv:
4186 {
4187 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00004188 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00004189
4190 vec = alloca(count * sizeof(struct iovec));
pbrook53a59602006-03-25 19:31:22 +00004191 lock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00004192 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00004193 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00004194 }
4195 break;
4196 case TARGET_NR_writev:
4197 {
4198 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00004199 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00004200
4201 vec = alloca(count * sizeof(struct iovec));
pbrook53a59602006-03-25 19:31:22 +00004202 lock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00004203 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00004204 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00004205 }
4206 break;
4207 case TARGET_NR_getsid:
4208 ret = get_errno(getsid(arg1));
4209 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004210#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00004211 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00004212 ret = get_errno(fdatasync(arg1));
4213 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004214#endif
bellard31e31b82003-02-18 22:55:36 +00004215 case TARGET_NR__sysctl:
bellard29e619b2004-09-13 21:41:04 +00004216 /* We don't implement this, but ENODIR is always a safe
4217 return value. */
4218 return -ENOTDIR;
bellard31e31b82003-02-18 22:55:36 +00004219 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00004220 {
pbrook53a59602006-03-25 19:31:22 +00004221 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004222 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00004223
4224 lock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00004225 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004226 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00004227 ret = get_errno(sched_setparam(arg1, &schp));
4228 }
4229 break;
bellard31e31b82003-02-18 22:55:36 +00004230 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00004231 {
pbrook53a59602006-03-25 19:31:22 +00004232 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004233 struct sched_param schp;
4234 ret = get_errno(sched_getparam(arg1, &schp));
4235 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004236 lock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00004237 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004238 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00004239 }
4240 }
4241 break;
bellard31e31b82003-02-18 22:55:36 +00004242 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00004243 {
pbrook53a59602006-03-25 19:31:22 +00004244 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004245 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00004246 lock_user_struct(target_schp, arg3, 1);
bellard5cd43932003-03-29 16:54:36 +00004247 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004248 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00004249 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
4250 }
4251 break;
bellard31e31b82003-02-18 22:55:36 +00004252 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00004253 ret = get_errno(sched_getscheduler(arg1));
4254 break;
bellard31e31b82003-02-18 22:55:36 +00004255 case TARGET_NR_sched_yield:
4256 ret = get_errno(sched_yield());
4257 break;
4258 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00004259 ret = get_errno(sched_get_priority_max(arg1));
4260 break;
bellard31e31b82003-02-18 22:55:36 +00004261 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00004262 ret = get_errno(sched_get_priority_min(arg1));
4263 break;
bellard31e31b82003-02-18 22:55:36 +00004264 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00004265 {
bellard5cd43932003-03-29 16:54:36 +00004266 struct timespec ts;
4267 ret = get_errno(sched_rr_get_interval(arg1, &ts));
4268 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004269 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00004270 }
4271 }
4272 break;
bellard31e31b82003-02-18 22:55:36 +00004273 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00004274 {
bellard1b6b0292003-03-22 17:31:38 +00004275 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00004276 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00004277 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00004278 if (is_error(ret) && arg2) {
4279 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00004280 }
4281 }
4282 break;
thse5febef2007-04-01 18:31:35 +00004283#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00004284 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00004285 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004286#endif
4287#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00004288 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00004289 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004290#endif
bellard31e31b82003-02-18 22:55:36 +00004291 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00004292 switch (arg1)
4293 {
4294 case PR_GET_PDEATHSIG:
4295 {
4296 int deathsig;
4297 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
4298 if (!is_error(ret) && arg2)
4299 tput32(arg2, deathsig);
4300 }
4301 break;
4302 default:
4303 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
4304 break;
4305 }
ths39b9aae2007-02-11 18:36:44 +00004306 break;
bellard67867302003-11-23 17:05:30 +00004307#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00004308 case TARGET_NR_pread:
pbrook53a59602006-03-25 19:31:22 +00004309 page_unprotect_range(arg2, arg3);
4310 p = lock_user(arg2, arg3, 0);
4311 ret = get_errno(pread(arg1, p, arg3, arg4));
4312 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00004313 break;
bellard31e31b82003-02-18 22:55:36 +00004314 case TARGET_NR_pwrite:
pbrook53a59602006-03-25 19:31:22 +00004315 p = lock_user(arg2, arg3, 1);
4316 ret = get_errno(pwrite(arg1, p, arg3, arg4));
4317 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00004318 break;
bellard67867302003-11-23 17:05:30 +00004319#endif
bellard31e31b82003-02-18 22:55:36 +00004320 case TARGET_NR_getcwd:
pbrook53a59602006-03-25 19:31:22 +00004321 p = lock_user(arg1, arg2, 0);
4322 ret = get_errno(sys_getcwd1(p, arg2));
4323 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00004324 break;
4325 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00004326 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004327 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00004328 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004329 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00004330#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
4331 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
thsa04e1342007-09-27 13:57:58 +00004332 ret = do_sigaltstack((struct target_sigaltstack *)arg1,
4333 (struct target_sigaltstack *)arg2,
4334 get_sp_from_cpustate((CPUState *)cpu_env));
4335 break;
4336#else
bellard5cd43932003-03-29 16:54:36 +00004337 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00004338#endif
bellard31e31b82003-02-18 22:55:36 +00004339 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00004340 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004341#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00004342 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00004343 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004344#endif
4345#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00004346 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00004347 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004348#endif
bellard048f6b42005-11-26 18:47:20 +00004349#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00004350 case TARGET_NR_vfork:
bellard1b6b0292003-03-22 17:31:38 +00004351 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00004352 break;
bellard048f6b42005-11-26 18:47:20 +00004353#endif
bellardebc05482003-09-30 21:08:41 +00004354#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00004355 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00004356 {
4357 struct rlimit rlim;
4358 ret = get_errno(getrlimit(arg1, &rlim));
4359 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004360 struct target_rlimit *target_rlim;
4361 lock_user_struct(target_rlim, arg2, 0);
bellard728584b2003-04-29 20:43:36 +00004362 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
4363 target_rlim->rlim_max = tswapl(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00004364 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00004365 }
4366 break;
4367 }
bellardebc05482003-09-30 21:08:41 +00004368#endif
bellarda315a142005-01-30 22:59:18 +00004369#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00004370 case TARGET_NR_truncate64:
pbrook53a59602006-03-25 19:31:22 +00004371 p = lock_user_string(arg1);
4372 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
4373 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00004374 break;
bellarda315a142005-01-30 22:59:18 +00004375#endif
4376#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00004377 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00004378 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00004379 break;
bellarda315a142005-01-30 22:59:18 +00004380#endif
4381#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00004382 case TARGET_NR_stat64:
pbrook53a59602006-03-25 19:31:22 +00004383 p = lock_user_string(arg1);
4384 ret = get_errno(stat(path(p), &st));
4385 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004386 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004387#endif
4388#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00004389 case TARGET_NR_lstat64:
pbrook53a59602006-03-25 19:31:22 +00004390 p = lock_user_string(arg1);
4391 ret = get_errno(lstat(path(p), &st));
4392 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004393 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004394#endif
4395#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00004396 case TARGET_NR_fstat64:
bellard60cd49d2003-03-16 22:53:56 +00004397 {
4398 ret = get_errno(fstat(arg1, &st));
4399 do_stat64:
4400 if (!is_error(ret)) {
pbrookce4defa2006-02-09 16:49:55 +00004401#ifdef TARGET_ARM
4402 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004403 struct target_eabi_stat64 *target_st;
4404 lock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00004405 memset(target_st, 0, sizeof(struct target_eabi_stat64));
pbrook53a59602006-03-25 19:31:22 +00004406 /* put_user is probably wrong. */
pbrookce4defa2006-02-09 16:49:55 +00004407 put_user(st.st_dev, &target_st->st_dev);
4408 put_user(st.st_ino, &target_st->st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004409#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
pbrookce4defa2006-02-09 16:49:55 +00004410 put_user(st.st_ino, &target_st->__st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004411#endif
pbrookce4defa2006-02-09 16:49:55 +00004412 put_user(st.st_mode, &target_st->st_mode);
4413 put_user(st.st_nlink, &target_st->st_nlink);
4414 put_user(st.st_uid, &target_st->st_uid);
4415 put_user(st.st_gid, &target_st->st_gid);
4416 put_user(st.st_rdev, &target_st->st_rdev);
4417 /* XXX: better use of kernel struct */
4418 put_user(st.st_size, &target_st->st_size);
4419 put_user(st.st_blksize, &target_st->st_blksize);
4420 put_user(st.st_blocks, &target_st->st_blocks);
4421 put_user(st.st_atime, &target_st->target_st_atime);
4422 put_user(st.st_mtime, &target_st->target_st_mtime);
4423 put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00004424 unlock_user_struct(target_st, arg2, 0);
pbrookce4defa2006-02-09 16:49:55 +00004425 } else
4426#endif
4427 {
pbrook53a59602006-03-25 19:31:22 +00004428 struct target_stat64 *target_st;
4429 lock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00004430 memset(target_st, 0, sizeof(struct target_stat64));
pbrook53a59602006-03-25 19:31:22 +00004431 /* ??? put_user is probably wrong. */
pbrookce4defa2006-02-09 16:49:55 +00004432 put_user(st.st_dev, &target_st->st_dev);
4433 put_user(st.st_ino, &target_st->st_ino);
4434#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4435 put_user(st.st_ino, &target_st->__st_ino);
4436#endif
4437 put_user(st.st_mode, &target_st->st_mode);
4438 put_user(st.st_nlink, &target_st->st_nlink);
4439 put_user(st.st_uid, &target_st->st_uid);
4440 put_user(st.st_gid, &target_st->st_gid);
4441 put_user(st.st_rdev, &target_st->st_rdev);
4442 /* XXX: better use of kernel struct */
4443 put_user(st.st_size, &target_st->st_size);
4444 put_user(st.st_blksize, &target_st->st_blksize);
4445 put_user(st.st_blocks, &target_st->st_blocks);
4446 put_user(st.st_atime, &target_st->target_st_atime);
4447 put_user(st.st_mtime, &target_st->target_st_mtime);
4448 put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00004449 unlock_user_struct(target_st, arg2, 0);
pbrookce4defa2006-02-09 16:49:55 +00004450 }
bellard60cd49d2003-03-16 22:53:56 +00004451 }
4452 }
4453 break;
bellarda315a142005-01-30 22:59:18 +00004454#endif
bellard67867302003-11-23 17:05:30 +00004455#ifdef USE_UID16
4456 case TARGET_NR_lchown:
pbrook53a59602006-03-25 19:31:22 +00004457 p = lock_user_string(arg1);
4458 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
4459 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00004460 break;
4461 case TARGET_NR_getuid:
4462 ret = get_errno(high2lowuid(getuid()));
4463 break;
4464 case TARGET_NR_getgid:
4465 ret = get_errno(high2lowgid(getgid()));
4466 break;
4467 case TARGET_NR_geteuid:
4468 ret = get_errno(high2lowuid(geteuid()));
4469 break;
4470 case TARGET_NR_getegid:
4471 ret = get_errno(high2lowgid(getegid()));
4472 break;
4473 case TARGET_NR_setreuid:
4474 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
4475 break;
4476 case TARGET_NR_setregid:
4477 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
4478 break;
4479 case TARGET_NR_getgroups:
4480 {
4481 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004482 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00004483 gid_t *grouplist;
4484 int i;
4485
4486 grouplist = alloca(gidsetsize * sizeof(gid_t));
4487 ret = get_errno(getgroups(gidsetsize, grouplist));
4488 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004489 target_grouplist = lock_user(arg2, gidsetsize * 2, 0);
bellard67867302003-11-23 17:05:30 +00004490 for(i = 0;i < gidsetsize; i++)
4491 target_grouplist[i] = tswap16(grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00004492 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00004493 }
4494 }
4495 break;
4496 case TARGET_NR_setgroups:
4497 {
4498 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004499 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00004500 gid_t *grouplist;
4501 int i;
4502
4503 grouplist = alloca(gidsetsize * sizeof(gid_t));
pbrook53a59602006-03-25 19:31:22 +00004504 target_grouplist = lock_user(arg2, gidsetsize * 2, 1);
bellard67867302003-11-23 17:05:30 +00004505 for(i = 0;i < gidsetsize; i++)
4506 grouplist[i] = tswap16(target_grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00004507 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00004508 ret = get_errno(setgroups(gidsetsize, grouplist));
4509 }
4510 break;
4511 case TARGET_NR_fchown:
4512 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
4513 break;
thsccfa72b2007-09-24 09:23:34 +00004514#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
4515 case TARGET_NR_fchownat:
4516 if (!arg2) {
4517 ret = -EFAULT;
4518 goto fail;
4519 }
4520 p = lock_user_string(arg2);
4521 if (!access_ok(VERIFY_READ, p, 1))
4522 ret = -EFAULT;
4523 else
4524 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
4525 if (p)
4526 unlock_user(p, arg2, 0);
4527 break;
4528#endif
bellard67867302003-11-23 17:05:30 +00004529#ifdef TARGET_NR_setresuid
4530 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00004531 ret = get_errno(setresuid(low2highuid(arg1),
4532 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00004533 low2highuid(arg3)));
4534 break;
4535#endif
4536#ifdef TARGET_NR_getresuid
4537 case TARGET_NR_getresuid:
4538 {
pbrook53a59602006-03-25 19:31:22 +00004539 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00004540 ret = get_errno(getresuid(&ruid, &euid, &suid));
4541 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004542 tput16(arg1, tswap16(high2lowuid(ruid)));
4543 tput16(arg2, tswap16(high2lowuid(euid)));
4544 tput16(arg3, tswap16(high2lowuid(suid)));
bellard67867302003-11-23 17:05:30 +00004545 }
4546 }
4547 break;
4548#endif
4549#ifdef TARGET_NR_getresgid
4550 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00004551 ret = get_errno(setresgid(low2highgid(arg1),
4552 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00004553 low2highgid(arg3)));
4554 break;
4555#endif
4556#ifdef TARGET_NR_getresgid
4557 case TARGET_NR_getresgid:
4558 {
pbrook53a59602006-03-25 19:31:22 +00004559 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00004560 ret = get_errno(getresgid(&rgid, &egid, &sgid));
4561 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004562 tput16(arg1, tswap16(high2lowgid(rgid)));
4563 tput16(arg2, tswap16(high2lowgid(egid)));
4564 tput16(arg3, tswap16(high2lowgid(sgid)));
bellard67867302003-11-23 17:05:30 +00004565 }
4566 }
4567 break;
4568#endif
4569 case TARGET_NR_chown:
pbrook53a59602006-03-25 19:31:22 +00004570 p = lock_user_string(arg1);
4571 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
4572 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00004573 break;
4574 case TARGET_NR_setuid:
4575 ret = get_errno(setuid(low2highuid(arg1)));
4576 break;
4577 case TARGET_NR_setgid:
4578 ret = get_errno(setgid(low2highgid(arg1)));
4579 break;
4580 case TARGET_NR_setfsuid:
4581 ret = get_errno(setfsuid(arg1));
4582 break;
4583 case TARGET_NR_setfsgid:
4584 ret = get_errno(setfsgid(arg1));
4585 break;
4586#endif /* USE_UID16 */
4587
bellarda315a142005-01-30 22:59:18 +00004588#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00004589 case TARGET_NR_lchown32:
pbrook53a59602006-03-25 19:31:22 +00004590 p = lock_user_string(arg1);
4591 ret = get_errno(lchown(p, arg2, arg3));
4592 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00004593 break;
bellarda315a142005-01-30 22:59:18 +00004594#endif
4595#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00004596 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00004597 ret = get_errno(getuid());
4598 break;
bellarda315a142005-01-30 22:59:18 +00004599#endif
4600#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00004601 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00004602 ret = get_errno(getgid());
4603 break;
bellarda315a142005-01-30 22:59:18 +00004604#endif
4605#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00004606 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00004607 ret = get_errno(geteuid());
4608 break;
bellarda315a142005-01-30 22:59:18 +00004609#endif
4610#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00004611 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00004612 ret = get_errno(getegid());
4613 break;
bellarda315a142005-01-30 22:59:18 +00004614#endif
4615#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00004616 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00004617 ret = get_errno(setreuid(arg1, arg2));
4618 break;
bellarda315a142005-01-30 22:59:18 +00004619#endif
4620#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00004621 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00004622 ret = get_errno(setregid(arg1, arg2));
4623 break;
bellarda315a142005-01-30 22:59:18 +00004624#endif
4625#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00004626 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00004627 {
4628 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004629 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00004630 gid_t *grouplist;
4631 int i;
4632
4633 grouplist = alloca(gidsetsize * sizeof(gid_t));
4634 ret = get_errno(getgroups(gidsetsize, grouplist));
4635 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004636 target_grouplist = lock_user(arg2, gidsetsize * 4, 0);
bellard99c475a2005-01-31 20:45:13 +00004637 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00004638 target_grouplist[i] = tswap32(grouplist[i]);
4639 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00004640 }
4641 }
4642 break;
bellarda315a142005-01-30 22:59:18 +00004643#endif
4644#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00004645 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00004646 {
4647 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004648 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00004649 gid_t *grouplist;
4650 int i;
ths3b46e622007-09-17 08:09:54 +00004651
bellard99c475a2005-01-31 20:45:13 +00004652 grouplist = alloca(gidsetsize * sizeof(gid_t));
pbrook53a59602006-03-25 19:31:22 +00004653 target_grouplist = lock_user(arg2, gidsetsize * 4, 1);
bellard99c475a2005-01-31 20:45:13 +00004654 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00004655 grouplist[i] = tswap32(target_grouplist[i]);
4656 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00004657 ret = get_errno(setgroups(gidsetsize, grouplist));
4658 }
4659 break;
bellarda315a142005-01-30 22:59:18 +00004660#endif
4661#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00004662 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00004663 ret = get_errno(fchown(arg1, arg2, arg3));
4664 break;
bellarda315a142005-01-30 22:59:18 +00004665#endif
4666#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00004667 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00004668 ret = get_errno(setresuid(arg1, arg2, arg3));
4669 break;
bellarda315a142005-01-30 22:59:18 +00004670#endif
4671#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00004672 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00004673 {
pbrook53a59602006-03-25 19:31:22 +00004674 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00004675 ret = get_errno(getresuid(&ruid, &euid, &suid));
4676 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004677 tput32(arg1, tswap32(ruid));
4678 tput32(arg2, tswap32(euid));
4679 tput32(arg3, tswap32(suid));
bellardb03c60f2003-03-23 17:19:56 +00004680 }
4681 }
4682 break;
bellarda315a142005-01-30 22:59:18 +00004683#endif
4684#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00004685 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00004686 ret = get_errno(setresgid(arg1, arg2, arg3));
4687 break;
bellarda315a142005-01-30 22:59:18 +00004688#endif
4689#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00004690 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00004691 {
pbrook53a59602006-03-25 19:31:22 +00004692 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00004693 ret = get_errno(getresgid(&rgid, &egid, &sgid));
4694 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004695 tput32(arg1, tswap32(rgid));
4696 tput32(arg2, tswap32(egid));
4697 tput32(arg3, tswap32(sgid));
bellardb03c60f2003-03-23 17:19:56 +00004698 }
4699 }
4700 break;
bellarda315a142005-01-30 22:59:18 +00004701#endif
4702#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00004703 case TARGET_NR_chown32:
pbrook53a59602006-03-25 19:31:22 +00004704 p = lock_user_string(arg1);
4705 ret = get_errno(chown(p, arg2, arg3));
4706 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00004707 break;
bellarda315a142005-01-30 22:59:18 +00004708#endif
4709#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00004710 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00004711 ret = get_errno(setuid(arg1));
4712 break;
bellarda315a142005-01-30 22:59:18 +00004713#endif
4714#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00004715 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00004716 ret = get_errno(setgid(arg1));
4717 break;
bellarda315a142005-01-30 22:59:18 +00004718#endif
4719#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00004720 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00004721 ret = get_errno(setfsuid(arg1));
4722 break;
bellarda315a142005-01-30 22:59:18 +00004723#endif
4724#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00004725 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00004726 ret = get_errno(setfsgid(arg1));
4727 break;
bellarda315a142005-01-30 22:59:18 +00004728#endif
bellard67867302003-11-23 17:05:30 +00004729
bellard31e31b82003-02-18 22:55:36 +00004730 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00004731 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00004732#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00004733 case TARGET_NR_mincore:
bellardb03c60f2003-03-23 17:19:56 +00004734 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00004735#endif
4736#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00004737 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00004738 /* A straight passthrough may not be safe because qemu sometimes
4739 turns private flie-backed mappings into anonymous mappings.
4740 This will break MADV_DONTNEED.
4741 This is a hint, so ignoring and returning success is ok. */
4742 ret = get_errno(0);
4743 break;
bellardffa65c32004-01-04 23:57:22 +00004744#endif
blueswir1992f48a2007-10-14 16:27:31 +00004745#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00004746 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00004747 {
thsb1e341e2007-03-20 21:50:52 +00004748 int cmd;
bellard77e46722003-04-29 20:39:06 +00004749 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00004750 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00004751#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00004752 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00004753#endif
bellard77e46722003-04-29 20:39:06 +00004754
thsb1e341e2007-03-20 21:50:52 +00004755 switch(arg2){
4756 case TARGET_F_GETLK64:
4757 cmd = F_GETLK64;
thsa7222582007-03-22 15:08:55 +00004758 break;
thsb1e341e2007-03-20 21:50:52 +00004759 case TARGET_F_SETLK64:
4760 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00004761 break;
thsb1e341e2007-03-20 21:50:52 +00004762 case TARGET_F_SETLKW64:
4763 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00004764 break;
thsb1e341e2007-03-20 21:50:52 +00004765 default:
4766 cmd = arg2;
thsa7222582007-03-22 15:08:55 +00004767 break;
thsb1e341e2007-03-20 21:50:52 +00004768 }
4769
bellard60cd49d2003-03-16 22:53:56 +00004770 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00004771 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00004772#ifdef TARGET_ARM
4773 if (((CPUARMState *)cpu_env)->eabi) {
4774 lock_user_struct(target_efl, arg3, 1);
4775 fl.l_type = tswap16(target_efl->l_type);
4776 fl.l_whence = tswap16(target_efl->l_whence);
4777 fl.l_start = tswap64(target_efl->l_start);
4778 fl.l_len = tswap64(target_efl->l_len);
4779 fl.l_pid = tswapl(target_efl->l_pid);
4780 unlock_user_struct(target_efl, arg3, 0);
4781 } else
4782#endif
4783 {
4784 lock_user_struct(target_fl, arg3, 1);
4785 fl.l_type = tswap16(target_fl->l_type);
4786 fl.l_whence = tswap16(target_fl->l_whence);
4787 fl.l_start = tswap64(target_fl->l_start);
4788 fl.l_len = tswap64(target_fl->l_len);
4789 fl.l_pid = tswapl(target_fl->l_pid);
4790 unlock_user_struct(target_fl, arg3, 0);
4791 }
thsb1e341e2007-03-20 21:50:52 +00004792 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00004793 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00004794#ifdef TARGET_ARM
4795 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004796 lock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004797 target_efl->l_type = tswap16(fl.l_type);
4798 target_efl->l_whence = tswap16(fl.l_whence);
4799 target_efl->l_start = tswap64(fl.l_start);
4800 target_efl->l_len = tswap64(fl.l_len);
4801 target_efl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004802 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004803 } else
4804#endif
4805 {
pbrook53a59602006-03-25 19:31:22 +00004806 lock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004807 target_fl->l_type = tswap16(fl.l_type);
4808 target_fl->l_whence = tswap16(fl.l_whence);
4809 target_fl->l_start = tswap64(fl.l_start);
4810 target_fl->l_len = tswap64(fl.l_len);
4811 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004812 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004813 }
bellard77e46722003-04-29 20:39:06 +00004814 }
4815 break;
4816
thsb1e341e2007-03-20 21:50:52 +00004817 case TARGET_F_SETLK64:
4818 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00004819#ifdef TARGET_ARM
4820 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004821 lock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004822 fl.l_type = tswap16(target_efl->l_type);
4823 fl.l_whence = tswap16(target_efl->l_whence);
4824 fl.l_start = tswap64(target_efl->l_start);
4825 fl.l_len = tswap64(target_efl->l_len);
4826 fl.l_pid = tswapl(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004827 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004828 } else
4829#endif
4830 {
pbrook53a59602006-03-25 19:31:22 +00004831 lock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004832 fl.l_type = tswap16(target_fl->l_type);
4833 fl.l_whence = tswap16(target_fl->l_whence);
4834 fl.l_start = tswap64(target_fl->l_start);
4835 fl.l_len = tswap64(target_fl->l_len);
4836 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004837 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004838 }
thsb1e341e2007-03-20 21:50:52 +00004839 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00004840 break;
bellard60cd49d2003-03-16 22:53:56 +00004841 default:
thsb1e341e2007-03-20 21:50:52 +00004842 ret = get_errno(do_fcntl(arg1, cmd, arg3));
bellard60cd49d2003-03-16 22:53:56 +00004843 break;
4844 }
bellard77e46722003-04-29 20:39:06 +00004845 break;
4846 }
bellard60cd49d2003-03-16 22:53:56 +00004847#endif
ths7d600c82006-12-08 01:32:58 +00004848#ifdef TARGET_NR_cacheflush
4849 case TARGET_NR_cacheflush:
4850 /* self-modifying code is handled automatically, so nothing needed */
4851 ret = 0;
4852 break;
4853#endif
bellardebc05482003-09-30 21:08:41 +00004854#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00004855 case TARGET_NR_security:
4856 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004857#endif
bellardc573ff62004-01-04 15:51:36 +00004858#ifdef TARGET_NR_getpagesize
4859 case TARGET_NR_getpagesize:
4860 ret = TARGET_PAGE_SIZE;
4861 break;
4862#endif
bellard31e31b82003-02-18 22:55:36 +00004863 case TARGET_NR_gettid:
4864 ret = get_errno(gettid());
4865 break;
thse5febef2007-04-01 18:31:35 +00004866#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00004867 case TARGET_NR_readahead:
bellard5cd43932003-03-29 16:54:36 +00004868 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004869#endif
bellardebc05482003-09-30 21:08:41 +00004870#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00004871 case TARGET_NR_setxattr:
4872 case TARGET_NR_lsetxattr:
4873 case TARGET_NR_fsetxattr:
4874 case TARGET_NR_getxattr:
4875 case TARGET_NR_lgetxattr:
4876 case TARGET_NR_fgetxattr:
4877 case TARGET_NR_listxattr:
4878 case TARGET_NR_llistxattr:
4879 case TARGET_NR_flistxattr:
4880 case TARGET_NR_removexattr:
4881 case TARGET_NR_lremovexattr:
4882 case TARGET_NR_fremovexattr:
bellard5cd43932003-03-29 16:54:36 +00004883 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00004884#endif
4885#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00004886 case TARGET_NR_set_thread_area:
ths6f5b89a2007-03-02 20:48:00 +00004887#ifdef TARGET_MIPS
4888 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
4889 ret = 0;
4890 break;
4891#else
4892 goto unimplemented_nowarn;
4893#endif
4894#endif
4895#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00004896 case TARGET_NR_get_thread_area:
4897 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00004898#endif
bellard48dc41e2006-06-21 18:15:50 +00004899#ifdef TARGET_NR_getdomainname
4900 case TARGET_NR_getdomainname:
4901 goto unimplemented_nowarn;
4902#endif
ths6f5b89a2007-03-02 20:48:00 +00004903
thsb5906f92007-03-19 13:32:45 +00004904#ifdef TARGET_NR_clock_gettime
4905 case TARGET_NR_clock_gettime:
4906 {
4907 struct timespec ts;
4908 ret = get_errno(clock_gettime(arg1, &ts));
4909 if (!is_error(ret)) {
4910 host_to_target_timespec(arg2, &ts);
4911 }
4912 break;
4913 }
4914#endif
4915#ifdef TARGET_NR_clock_getres
4916 case TARGET_NR_clock_getres:
4917 {
4918 struct timespec ts;
4919 ret = get_errno(clock_getres(arg1, &ts));
4920 if (!is_error(ret)) {
4921 host_to_target_timespec(arg2, &ts);
4922 }
4923 break;
4924 }
4925#endif
4926
ths6f5b89a2007-03-02 20:48:00 +00004927#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
4928 case TARGET_NR_set_tid_address:
4929 ret = get_errno(set_tid_address((int *) arg1));
4930 break;
4931#endif
4932
ths3ae43202007-09-16 21:39:48 +00004933#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00004934 case TARGET_NR_tkill:
4935 ret = get_errno(sys_tkill((int)arg1, (int)arg2));
4936 break;
4937#endif
4938
ths3ae43202007-09-16 21:39:48 +00004939#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00004940 case TARGET_NR_tgkill:
4941 ret = get_errno(sys_tgkill((int)arg1, (int)arg2, (int)arg3));
4942 break;
4943#endif
4944
ths4f2b1fe2007-06-21 21:57:12 +00004945#ifdef TARGET_NR_set_robust_list
4946 case TARGET_NR_set_robust_list:
4947 goto unimplemented_nowarn;
4948#endif
4949
ths9007f0e2007-09-25 17:50:37 +00004950#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
4951 case TARGET_NR_utimensat:
4952 {
4953 struct timespec ts[2];
4954 target_to_host_timespec(ts, arg3);
4955 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
4956 if (!arg2)
4957 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
4958 else {
4959 p = lock_user_string(arg2);
4960 if (!access_ok(VERIFY_READ, p, 1))
4961 ret = -EFAULT;
4962 else
4963 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
4964 if (p)
4965 unlock_user(p, arg2, 0);
4966 }
4967 }
4968 break;
4969#endif
4970
bellard31e31b82003-02-18 22:55:36 +00004971 default:
4972 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00004973 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00004974#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 +00004975 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00004976#endif
bellard31e31b82003-02-18 22:55:36 +00004977 ret = -ENOSYS;
4978 break;
4979 }
4980 fail:
bellardc573ff62004-01-04 15:51:36 +00004981#ifdef DEBUG
4982 gemu_log(" = %ld\n", ret);
4983#endif
bellard31e31b82003-02-18 22:55:36 +00004984 return ret;
4985}