blob: 21078837dc29c476189cce7afea87030d01711f7 [file] [log] [blame]
bellard31e31b82003-02-18 22:55:36 +00001/*
2 * Linux syscalls
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard31e31b82003-02-18 22:55:36 +00004 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20#include <stdlib.h>
21#include <stdio.h>
22#include <stdarg.h>
bellard04369ff2003-03-20 22:33:23 +000023#include <string.h>
bellard31e31b82003-02-18 22:55:36 +000024#include <elf.h>
25#include <endian.h>
26#include <errno.h>
27#include <unistd.h>
28#include <fcntl.h>
bellard7854b052003-03-29 17:22:23 +000029#include <time.h>
bellard31e31b82003-02-18 22:55:36 +000030#include <sys/types.h>
thsd08d3bb2007-03-19 13:09:22 +000031#include <sys/ipc.h>
32#include <sys/msg.h>
bellard31e31b82003-02-18 22:55:36 +000033#include <sys/wait.h>
34#include <sys/time.h>
35#include <sys/stat.h>
36#include <sys/mount.h>
ths39b9aae2007-02-11 18:36:44 +000037#include <sys/prctl.h>
bellard31e31b82003-02-18 22:55:36 +000038#include <sys/resource.h>
39#include <sys/mman.h>
40#include <sys/swap.h>
41#include <signal.h>
42#include <sched.h>
43#include <sys/socket.h>
44#include <sys/uio.h>
bellard9de5e442003-03-23 16:49:39 +000045#include <sys/poll.h>
bellard32f36bc2003-03-30 21:29:48 +000046#include <sys/times.h>
bellard8853f862004-02-22 14:57:26 +000047#include <sys/shm.h>
thsfa294812007-02-02 22:05:00 +000048#include <sys/sem.h>
bellard56c8f682005-11-28 22:28:41 +000049#include <sys/statfs.h>
bellardebc05482003-09-30 21:08:41 +000050#include <utime.h>
bellarda5448a72004-06-19 16:59:03 +000051#include <sys/sysinfo.h>
bellard72f03902003-02-18 23:33:18 +000052//#include <sys/user.h>
bellard8853f862004-02-22 14:57:26 +000053#include <netinet/ip.h>
bellard7854b052003-03-29 17:22:23 +000054#include <netinet/tcp.h>
bellard31e31b82003-02-18 22:55:36 +000055
56#define termios host_termios
57#define winsize host_winsize
58#define termio host_termio
bellard04369ff2003-03-20 22:33:23 +000059#define sgttyb host_sgttyb /* same as target */
60#define tchars host_tchars /* same as target */
61#define ltchars host_ltchars /* same as target */
bellard31e31b82003-02-18 22:55:36 +000062
63#include <linux/termios.h>
64#include <linux/unistd.h>
65#include <linux/utsname.h>
66#include <linux/cdrom.h>
67#include <linux/hdreg.h>
68#include <linux/soundcard.h>
bellarddab2ed92003-03-22 15:23:14 +000069#include <linux/dirent.h>
bellard19b84f32003-05-08 15:41:49 +000070#include <linux/kd.h>
bellard31e31b82003-02-18 22:55:36 +000071
bellard3ef693a2003-03-23 20:17:16 +000072#include "qemu.h"
bellard31e31b82003-02-18 22:55:36 +000073
bellard72f03902003-02-18 23:33:18 +000074//#define DEBUG
bellard31e31b82003-02-18 22:55:36 +000075
pbrooke6e59062006-10-22 00:18:54 +000076#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC) \
ths48733d12007-10-08 13:36:46 +000077 || defined(TARGET_M68K) || defined(TARGET_SH4) || defined(TARGET_CRIS)
bellard67867302003-11-23 17:05:30 +000078/* 16 bit uid wrappers emulation */
79#define USE_UID16
80#endif
81
bellard1a9353d2003-03-16 20:28:50 +000082//#include <linux/msdos_fs.h>
83#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct dirent [2])
84#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct dirent [2])
85
bellard70a194b2003-08-11 22:20:16 +000086
bellard70a194b2003-08-11 22:20:16 +000087#undef _syscall0
88#undef _syscall1
89#undef _syscall2
90#undef _syscall3
91#undef _syscall4
92#undef _syscall5
bellard83fcb512006-06-14 13:37:16 +000093#undef _syscall6
bellard70a194b2003-08-11 22:20:16 +000094
bellard83fcb512006-06-14 13:37:16 +000095#define _syscall0(type,name) \
96type name (void) \
97{ \
98 return syscall(__NR_##name); \
bellard70a194b2003-08-11 22:20:16 +000099}
100
bellard83fcb512006-06-14 13:37:16 +0000101#define _syscall1(type,name,type1,arg1) \
102type name (type1 arg1) \
103{ \
104 return syscall(__NR_##name, arg1); \
bellard70a194b2003-08-11 22:20:16 +0000105}
106
bellard83fcb512006-06-14 13:37:16 +0000107#define _syscall2(type,name,type1,arg1,type2,arg2) \
108type name (type1 arg1,type2 arg2) \
109{ \
110 return syscall(__NR_##name, arg1, arg2); \
bellard70a194b2003-08-11 22:20:16 +0000111}
112
bellard83fcb512006-06-14 13:37:16 +0000113#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
114type name (type1 arg1,type2 arg2,type3 arg3) \
115{ \
116 return syscall(__NR_##name, arg1, arg2, arg3); \
bellard70a194b2003-08-11 22:20:16 +0000117}
118
bellard83fcb512006-06-14 13:37:16 +0000119#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
120type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
121{ \
122 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
bellard70a194b2003-08-11 22:20:16 +0000123}
124
bellard83fcb512006-06-14 13:37:16 +0000125#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
126 type5,arg5) \
127type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
128{ \
129 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
bellard70a194b2003-08-11 22:20:16 +0000130}
bellard83fcb512006-06-14 13:37:16 +0000131
132
133#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
134 type5,arg5,type6,arg6) \
135type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \
136{ \
137 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
138}
139
bellard70a194b2003-08-11 22:20:16 +0000140
bellard31e31b82003-02-18 22:55:36 +0000141#define __NR_sys_uname __NR_uname
ths92a34c12007-09-24 09:27:49 +0000142#define __NR_sys_faccessat __NR_faccessat
ths814d7972007-09-24 09:26:51 +0000143#define __NR_sys_fchmodat __NR_fchmodat
thsccfa72b2007-09-24 09:23:34 +0000144#define __NR_sys_fchownat __NR_fchownat
bellard72f03902003-02-18 23:33:18 +0000145#define __NR_sys_getcwd1 __NR_getcwd
bellard72f03902003-02-18 23:33:18 +0000146#define __NR_sys_getdents __NR_getdents
bellarddab2ed92003-03-22 15:23:14 +0000147#define __NR_sys_getdents64 __NR_getdents64
thsc6cda172007-10-09 03:42:34 +0000148#define __NR_sys_getpriority __NR_getpriority
ths64f0ce42007-09-24 09:25:06 +0000149#define __NR_sys_linkat __NR_linkat
ths4472ad02007-09-24 09:22:32 +0000150#define __NR_sys_mkdirat __NR_mkdirat
ths75ac37a2007-09-24 09:23:05 +0000151#define __NR_sys_mknodat __NR_mknodat
ths82424832007-09-24 09:21:55 +0000152#define __NR_sys_openat __NR_openat
ths5e0ccb12007-09-24 09:26:10 +0000153#define __NR_sys_readlinkat __NR_readlinkat
ths722183f2007-09-24 09:24:37 +0000154#define __NR_sys_renameat __NR_renameat
bellard66fb9762003-03-23 01:06:05 +0000155#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
thsf0b62432007-09-24 09:25:40 +0000156#define __NR_sys_symlinkat __NR_symlinkat
ths7494b0f2007-02-11 18:26:53 +0000157#define __NR_sys_syslog __NR_syslog
ths71455572007-06-21 21:45:30 +0000158#define __NR_sys_tgkill __NR_tgkill
ths4cae1d12007-07-12 11:06:53 +0000159#define __NR_sys_tkill __NR_tkill
ths8170f562007-09-24 09:24:11 +0000160#define __NR_sys_unlinkat __NR_unlinkat
ths9007f0e2007-09-25 17:50:37 +0000161#define __NR_sys_utimensat __NR_utimensat
bellard31e31b82003-02-18 22:55:36 +0000162
bellardbc51c5c2004-03-17 23:46:04 +0000163#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
bellard9af9eaa2003-04-07 21:34:41 +0000164#define __NR__llseek __NR_lseek
165#endif
166
bellard72f03902003-02-18 23:33:18 +0000167#ifdef __NR_gettid
bellard31e31b82003-02-18 22:55:36 +0000168_syscall0(int, gettid)
bellard72f03902003-02-18 23:33:18 +0000169#else
ths0da46a62007-10-20 20:23:07 +0000170/* This is a replacement for the host gettid() and must return a host
171 errno. */
bellard72f03902003-02-18 23:33:18 +0000172static int gettid(void) {
173 return -ENOSYS;
174}
175#endif
bellard31e31b82003-02-18 22:55:36 +0000176_syscall1(int,sys_uname,struct new_utsname *,buf)
ths92a34c12007-09-24 09:27:49 +0000177#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
178_syscall4(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode,int,flags)
179#endif
ths814d7972007-09-24 09:26:51 +0000180#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
181_syscall4(int,sys_fchmodat,int,dirfd,const char *,pathname,
182 mode_t,mode,int,flags)
183#endif
thsccfa72b2007-09-24 09:23:34 +0000184#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
185_syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
186 uid_t,owner,gid_t,group,int,flags)
187#endif
bellard72f03902003-02-18 23:33:18 +0000188_syscall2(int,sys_getcwd1,char *,buf,size_t,size)
189_syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
ths3ae43202007-09-16 21:39:48 +0000190#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +0000191_syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
ths3ae43202007-09-16 21:39:48 +0000192#endif
thsc6cda172007-10-09 03:42:34 +0000193_syscall2(int, sys_getpriority, int, which, int, who);
bellard31e31b82003-02-18 22:55:36 +0000194_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
195 loff_t *, res, uint, wh);
ths64f0ce42007-09-24 09:25:06 +0000196#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
197_syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
198 int,newdirfd,const char *,newpath,int,flags)
199#endif
ths4472ad02007-09-24 09:22:32 +0000200#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
201_syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
202#endif
ths75ac37a2007-09-24 09:23:05 +0000203#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
204_syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
205 mode_t,mode,dev_t,dev)
206#endif
ths82424832007-09-24 09:21:55 +0000207#if defined(TARGET_NR_openat) && defined(__NR_openat)
208_syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
209#endif
ths5e0ccb12007-09-24 09:26:10 +0000210#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
211_syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
212 char *,buf,size_t,bufsize)
213#endif
ths722183f2007-09-24 09:24:37 +0000214#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
215_syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
216 int,newdirfd,const char *,newpath)
217#endif
bellard66fb9762003-03-23 01:06:05 +0000218_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
thsb51eaa82007-09-25 16:09:22 +0000219#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +0000220_syscall3(int,sys_symlinkat,const char *,oldpath,
221 int,newdirfd,const char *,newpath)
222#endif
ths7494b0f2007-02-11 18:26:53 +0000223_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
ths3ae43202007-09-16 21:39:48 +0000224#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +0000225_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
ths4cae1d12007-07-12 11:06:53 +0000226#endif
ths3ae43202007-09-16 21:39:48 +0000227#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +0000228_syscall2(int,sys_tkill,int,tid,int,sig)
229#endif
bellardec86b0f2003-04-11 00:15:04 +0000230#ifdef __NR_exit_group
231_syscall1(int,exit_group,int,error_code)
232#endif
ths6f5b89a2007-03-02 20:48:00 +0000233#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
234_syscall1(int,set_tid_address,int *,tidptr)
235#endif
ths8170f562007-09-24 09:24:11 +0000236#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
237_syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
238#endif
ths9007f0e2007-09-25 17:50:37 +0000239#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
240_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
241 const struct timespec *,tsp,int,flags)
242#endif
bellard66fb9762003-03-23 01:06:05 +0000243
244extern int personality(int);
bellard9de5e442003-03-23 16:49:39 +0000245extern int flock(int, int);
246extern int setfsuid(int);
247extern int setfsgid(int);
bellard5cd43932003-03-29 16:54:36 +0000248extern int setresuid(uid_t, uid_t, uid_t);
249extern int getresuid(uid_t *, uid_t *, uid_t *);
250extern int setresgid(gid_t, gid_t, gid_t);
251extern int getresgid(gid_t *, gid_t *, gid_t *);
bellard19b84f32003-05-08 15:41:49 +0000252extern int setgroups(int, gid_t *);
bellard31e31b82003-02-18 22:55:36 +0000253
thsb92c47c2007-11-01 00:07:38 +0000254#define ERRNO_TABLE_SIZE 1200
255
256/* target_to_host_errno_table[] is initialized from
257 * host_to_target_errno_table[] in syscall_init(). */
258static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
259};
260
ths637947f2007-06-01 12:09:19 +0000261/*
thsfe8f0962007-07-12 10:59:21 +0000262 * This list is the union of errno values overridden in asm-<arch>/errno.h
ths637947f2007-06-01 12:09:19 +0000263 * minus the errnos that are not actually generic to all archs.
264 */
thsb92c47c2007-11-01 00:07:38 +0000265static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
ths637947f2007-06-01 12:09:19 +0000266 [EIDRM] = TARGET_EIDRM,
267 [ECHRNG] = TARGET_ECHRNG,
268 [EL2NSYNC] = TARGET_EL2NSYNC,
269 [EL3HLT] = TARGET_EL3HLT,
270 [EL3RST] = TARGET_EL3RST,
271 [ELNRNG] = TARGET_ELNRNG,
272 [EUNATCH] = TARGET_EUNATCH,
273 [ENOCSI] = TARGET_ENOCSI,
274 [EL2HLT] = TARGET_EL2HLT,
275 [EDEADLK] = TARGET_EDEADLK,
276 [ENOLCK] = TARGET_ENOLCK,
277 [EBADE] = TARGET_EBADE,
278 [EBADR] = TARGET_EBADR,
279 [EXFULL] = TARGET_EXFULL,
280 [ENOANO] = TARGET_ENOANO,
281 [EBADRQC] = TARGET_EBADRQC,
282 [EBADSLT] = TARGET_EBADSLT,
283 [EBFONT] = TARGET_EBFONT,
284 [ENOSTR] = TARGET_ENOSTR,
285 [ENODATA] = TARGET_ENODATA,
286 [ETIME] = TARGET_ETIME,
287 [ENOSR] = TARGET_ENOSR,
288 [ENONET] = TARGET_ENONET,
289 [ENOPKG] = TARGET_ENOPKG,
290 [EREMOTE] = TARGET_EREMOTE,
291 [ENOLINK] = TARGET_ENOLINK,
292 [EADV] = TARGET_EADV,
293 [ESRMNT] = TARGET_ESRMNT,
294 [ECOMM] = TARGET_ECOMM,
295 [EPROTO] = TARGET_EPROTO,
296 [EDOTDOT] = TARGET_EDOTDOT,
297 [EMULTIHOP] = TARGET_EMULTIHOP,
298 [EBADMSG] = TARGET_EBADMSG,
299 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
300 [EOVERFLOW] = TARGET_EOVERFLOW,
301 [ENOTUNIQ] = TARGET_ENOTUNIQ,
302 [EBADFD] = TARGET_EBADFD,
303 [EREMCHG] = TARGET_EREMCHG,
304 [ELIBACC] = TARGET_ELIBACC,
305 [ELIBBAD] = TARGET_ELIBBAD,
306 [ELIBSCN] = TARGET_ELIBSCN,
307 [ELIBMAX] = TARGET_ELIBMAX,
308 [ELIBEXEC] = TARGET_ELIBEXEC,
309 [EILSEQ] = TARGET_EILSEQ,
310 [ENOSYS] = TARGET_ENOSYS,
311 [ELOOP] = TARGET_ELOOP,
312 [ERESTART] = TARGET_ERESTART,
313 [ESTRPIPE] = TARGET_ESTRPIPE,
314 [ENOTEMPTY] = TARGET_ENOTEMPTY,
315 [EUSERS] = TARGET_EUSERS,
316 [ENOTSOCK] = TARGET_ENOTSOCK,
317 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
318 [EMSGSIZE] = TARGET_EMSGSIZE,
319 [EPROTOTYPE] = TARGET_EPROTOTYPE,
320 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
321 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
322 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
323 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
324 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
325 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
326 [EADDRINUSE] = TARGET_EADDRINUSE,
327 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
328 [ENETDOWN] = TARGET_ENETDOWN,
329 [ENETUNREACH] = TARGET_ENETUNREACH,
330 [ENETRESET] = TARGET_ENETRESET,
331 [ECONNABORTED] = TARGET_ECONNABORTED,
332 [ECONNRESET] = TARGET_ECONNRESET,
333 [ENOBUFS] = TARGET_ENOBUFS,
334 [EISCONN] = TARGET_EISCONN,
335 [ENOTCONN] = TARGET_ENOTCONN,
336 [EUCLEAN] = TARGET_EUCLEAN,
337 [ENOTNAM] = TARGET_ENOTNAM,
338 [ENAVAIL] = TARGET_ENAVAIL,
339 [EISNAM] = TARGET_EISNAM,
340 [EREMOTEIO] = TARGET_EREMOTEIO,
341 [ESHUTDOWN] = TARGET_ESHUTDOWN,
342 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
343 [ETIMEDOUT] = TARGET_ETIMEDOUT,
344 [ECONNREFUSED] = TARGET_ECONNREFUSED,
345 [EHOSTDOWN] = TARGET_EHOSTDOWN,
346 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
347 [EALREADY] = TARGET_EALREADY,
348 [EINPROGRESS] = TARGET_EINPROGRESS,
349 [ESTALE] = TARGET_ESTALE,
350 [ECANCELED] = TARGET_ECANCELED,
351 [ENOMEDIUM] = TARGET_ENOMEDIUM,
352 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
thsb7fe5db2007-07-23 15:37:46 +0000353#ifdef ENOKEY
ths637947f2007-06-01 12:09:19 +0000354 [ENOKEY] = TARGET_ENOKEY,
thsb7fe5db2007-07-23 15:37:46 +0000355#endif
356#ifdef EKEYEXPIRED
ths637947f2007-06-01 12:09:19 +0000357 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
thsb7fe5db2007-07-23 15:37:46 +0000358#endif
359#ifdef EKEYREVOKED
ths637947f2007-06-01 12:09:19 +0000360 [EKEYREVOKED] = TARGET_EKEYREVOKED,
thsb7fe5db2007-07-23 15:37:46 +0000361#endif
362#ifdef EKEYREJECTED
ths637947f2007-06-01 12:09:19 +0000363 [EKEYREJECTED] = TARGET_EKEYREJECTED,
thsb7fe5db2007-07-23 15:37:46 +0000364#endif
365#ifdef EOWNERDEAD
ths637947f2007-06-01 12:09:19 +0000366 [EOWNERDEAD] = TARGET_EOWNERDEAD,
thsb7fe5db2007-07-23 15:37:46 +0000367#endif
368#ifdef ENOTRECOVERABLE
ths637947f2007-06-01 12:09:19 +0000369 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
thsb7fe5db2007-07-23 15:37:46 +0000370#endif
thsb92c47c2007-11-01 00:07:38 +0000371};
ths637947f2007-06-01 12:09:19 +0000372
373static inline int host_to_target_errno(int err)
374{
375 if(host_to_target_errno_table[err])
376 return host_to_target_errno_table[err];
377 return err;
378}
379
thsb92c47c2007-11-01 00:07:38 +0000380static inline int target_to_host_errno(int err)
381{
382 if (target_to_host_errno_table[err])
383 return target_to_host_errno_table[err];
384 return err;
385}
386
blueswir1992f48a2007-10-14 16:27:31 +0000387static inline abi_long get_errno(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000388{
389 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000390 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000391 else
392 return ret;
393}
394
blueswir1992f48a2007-10-14 16:27:31 +0000395static inline int is_error(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000396{
blueswir1992f48a2007-10-14 16:27:31 +0000397 return (abi_ulong)ret >= (abi_ulong)(-4096);
bellard31e31b82003-02-18 22:55:36 +0000398}
399
thsb92c47c2007-11-01 00:07:38 +0000400char *target_strerror(int err)
401{
402 return strerror(target_to_host_errno(err));
403}
404
blueswir1992f48a2007-10-14 16:27:31 +0000405static abi_ulong target_brk;
406static abi_ulong target_original_brk;
bellard31e31b82003-02-18 22:55:36 +0000407
blueswir1992f48a2007-10-14 16:27:31 +0000408void target_set_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000409{
blueswir14c1de732007-07-07 20:45:44 +0000410 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
bellard31e31b82003-02-18 22:55:36 +0000411}
412
ths0da46a62007-10-20 20:23:07 +0000413/* do_brk() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000414abi_long do_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000415{
blueswir1992f48a2007-10-14 16:27:31 +0000416 abi_ulong brk_page;
417 abi_long mapped_addr;
bellard31e31b82003-02-18 22:55:36 +0000418 int new_alloc_size;
419
420 if (!new_brk)
pbrook53a59602006-03-25 19:31:22 +0000421 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000422 if (new_brk < target_original_brk)
ths0da46a62007-10-20 20:23:07 +0000423 return -TARGET_ENOMEM;
ths3b46e622007-09-17 08:09:54 +0000424
pbrook53a59602006-03-25 19:31:22 +0000425 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000426
427 /* If the new brk is less than this, set it and we're done... */
428 if (new_brk < brk_page) {
429 target_brk = new_brk;
pbrook53a59602006-03-25 19:31:22 +0000430 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000431 }
432
433 /* We need to allocate more memory after the brk... */
bellard54936002003-05-13 00:25:15 +0000434 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
ths5fafdf22007-09-16 21:08:06 +0000435 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000436 PROT_READ|PROT_WRITE,
437 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
bellard31e31b82003-02-18 22:55:36 +0000438 if (is_error(mapped_addr)) {
439 return mapped_addr;
440 } else {
441 target_brk = new_brk;
pbrook53a59602006-03-25 19:31:22 +0000442 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000443 }
444}
445
ths5fafdf22007-09-16 21:08:06 +0000446static inline fd_set *target_to_host_fds(fd_set *fds,
blueswir1992f48a2007-10-14 16:27:31 +0000447 abi_long *target_fds, int n)
bellard31e31b82003-02-18 22:55:36 +0000448{
bellard7854b052003-03-29 17:22:23 +0000449#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
bellard31e31b82003-02-18 22:55:36 +0000450 return (fd_set *)target_fds;
451#else
452 int i, b;
453 if (target_fds) {
454 FD_ZERO(fds);
455 for(i = 0;i < n; i++) {
blueswir1992f48a2007-10-14 16:27:31 +0000456 b = (tswapl(target_fds[i / TARGET_ABI_BITS]) >>
457 (i & (TARGET_ABI_BITS - 1))) & 1;
bellard31e31b82003-02-18 22:55:36 +0000458 if (b)
459 FD_SET(i, fds);
460 }
461 return fds;
462 } else {
463 return NULL;
464 }
465#endif
466}
467
blueswir1992f48a2007-10-14 16:27:31 +0000468static inline void host_to_target_fds(abi_long *target_fds,
bellard31e31b82003-02-18 22:55:36 +0000469 fd_set *fds, int n)
470{
bellard7854b052003-03-29 17:22:23 +0000471#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
bellard31e31b82003-02-18 22:55:36 +0000472 /* nothing to do */
473#else
474 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000475 abi_long v;
bellard31e31b82003-02-18 22:55:36 +0000476
477 if (target_fds) {
blueswir1992f48a2007-10-14 16:27:31 +0000478 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
bellard31e31b82003-02-18 22:55:36 +0000479 k = 0;
480 for(i = 0;i < nw; i++) {
481 v = 0;
blueswir1992f48a2007-10-14 16:27:31 +0000482 for(j = 0; j < TARGET_ABI_BITS; j++) {
bellard31e31b82003-02-18 22:55:36 +0000483 v |= ((FD_ISSET(k, fds) != 0) << j);
484 k++;
485 }
486 target_fds[i] = tswapl(v);
487 }
488 }
489#endif
490}
491
bellardc596ed12003-07-13 17:32:31 +0000492#if defined(__alpha__)
493#define HOST_HZ 1024
494#else
495#define HOST_HZ 100
496#endif
497
blueswir1992f48a2007-10-14 16:27:31 +0000498static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000499{
500#if HOST_HZ == TARGET_HZ
501 return ticks;
502#else
503 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
504#endif
505}
506
bellard579a97f2007-11-11 14:26:47 +0000507static inline abi_long host_to_target_rusage(abi_ulong target_addr,
508 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000509{
pbrook53a59602006-03-25 19:31:22 +0000510 struct target_rusage *target_rusage;
511
bellard579a97f2007-11-11 14:26:47 +0000512 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
513 return -TARGET_EFAULT;
bellardb4091862003-05-16 15:39:34 +0000514 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
515 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
516 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
517 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
518 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
519 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
520 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
521 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
522 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
523 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
524 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
525 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
526 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
527 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
528 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
529 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
530 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
531 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000532 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000533
534 return 0;
bellardb4091862003-05-16 15:39:34 +0000535}
536
bellard579a97f2007-11-11 14:26:47 +0000537static inline abi_long target_to_host_timeval(struct timeval *tv,
538 abi_ulong target_addr)
bellard31e31b82003-02-18 22:55:36 +0000539{
pbrook53a59602006-03-25 19:31:22 +0000540 struct target_timeval *target_tv;
541
bellard579a97f2007-11-11 14:26:47 +0000542 if (!lock_user_struct(VERIFY_READ, target_tv, target_addr, 1))
543 return -TARGET_EFAULT;
bellard66fb9762003-03-23 01:06:05 +0000544 tv->tv_sec = tswapl(target_tv->tv_sec);
545 tv->tv_usec = tswapl(target_tv->tv_usec);
pbrook53a59602006-03-25 19:31:22 +0000546 unlock_user_struct(target_tv, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000547
548 return 0;
bellard31e31b82003-02-18 22:55:36 +0000549}
550
bellard579a97f2007-11-11 14:26:47 +0000551static inline abi_long host_to_target_timeval(abi_ulong target_addr,
552 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +0000553{
pbrook53a59602006-03-25 19:31:22 +0000554 struct target_timeval *target_tv;
555
bellard579a97f2007-11-11 14:26:47 +0000556 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_addr, 0))
557 return -TARGET_EFAULT;
bellard66fb9762003-03-23 01:06:05 +0000558 target_tv->tv_sec = tswapl(tv->tv_sec);
559 target_tv->tv_usec = tswapl(tv->tv_usec);
pbrook53a59602006-03-25 19:31:22 +0000560 unlock_user_struct(target_tv, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000561
562 return 0;
bellard31e31b82003-02-18 22:55:36 +0000563}
564
565
ths0da46a62007-10-20 20:23:07 +0000566/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000567static abi_long do_select(int n,
568 abi_ulong rfd_p, abi_ulong wfd_p,
569 abi_ulong efd_p, abi_ulong target_tv)
bellard31e31b82003-02-18 22:55:36 +0000570{
571 fd_set rfds, wfds, efds;
572 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
blueswir1992f48a2007-10-14 16:27:31 +0000573 abi_long *target_rfds, *target_wfds, *target_efds;
bellard31e31b82003-02-18 22:55:36 +0000574 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +0000575 abi_long ret;
pbrook53a59602006-03-25 19:31:22 +0000576 int ok;
bellard31e31b82003-02-18 22:55:36 +0000577
pbrook53a59602006-03-25 19:31:22 +0000578 if (rfd_p) {
bellard579a97f2007-11-11 14:26:47 +0000579 target_rfds = lock_user(VERIFY_WRITE, rfd_p, sizeof(abi_long) * n, 1);
580 if (!target_rfds) {
581 ret = -TARGET_EFAULT;
582 goto end;
583 }
pbrook53a59602006-03-25 19:31:22 +0000584 rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
585 } else {
586 target_rfds = NULL;
587 rfds_ptr = NULL;
588 }
589 if (wfd_p) {
bellard579a97f2007-11-11 14:26:47 +0000590 target_wfds = lock_user(VERIFY_WRITE, wfd_p, sizeof(abi_long) * n, 1);
591 if (!target_wfds) {
592 ret = -TARGET_EFAULT;
593 goto end;
594 }
pbrook53a59602006-03-25 19:31:22 +0000595 wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
596 } else {
597 target_wfds = NULL;
598 wfds_ptr = NULL;
599 }
600 if (efd_p) {
bellard579a97f2007-11-11 14:26:47 +0000601 target_efds = lock_user(VERIFY_WRITE, efd_p, sizeof(abi_long) * n, 1);
602 if (!target_efds) {
603 ret = -TARGET_EFAULT;
604 goto end;
605 }
pbrook53a59602006-03-25 19:31:22 +0000606 efds_ptr = target_to_host_fds(&efds, target_efds, n);
607 } else {
608 target_efds = NULL;
609 efds_ptr = NULL;
610 }
ths3b46e622007-09-17 08:09:54 +0000611
bellard31e31b82003-02-18 22:55:36 +0000612 if (target_tv) {
bellard5cd43932003-03-29 16:54:36 +0000613 target_to_host_timeval(&tv, target_tv);
bellard31e31b82003-02-18 22:55:36 +0000614 tv_ptr = &tv;
615 } else {
616 tv_ptr = NULL;
617 }
618 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +0000619 ok = !is_error(ret);
620
621 if (ok) {
bellard31e31b82003-02-18 22:55:36 +0000622 host_to_target_fds(target_rfds, rfds_ptr, n);
623 host_to_target_fds(target_wfds, wfds_ptr, n);
624 host_to_target_fds(target_efds, efds_ptr, n);
625
626 if (target_tv) {
bellard5cd43932003-03-29 16:54:36 +0000627 host_to_target_timeval(target_tv, &tv);
bellard31e31b82003-02-18 22:55:36 +0000628 }
629 }
bellard579a97f2007-11-11 14:26:47 +0000630
631end:
632 unlock_user(target_rfds, rfd_p, ok ? sizeof(abi_long) * n : 0);
633 unlock_user(target_wfds, wfd_p, ok ? sizeof(abi_long) * n : 0);
634 unlock_user(target_efds, efd_p, ok ? sizeof(abi_long) * n : 0);
pbrook53a59602006-03-25 19:31:22 +0000635
bellard31e31b82003-02-18 22:55:36 +0000636 return ret;
637}
638
bellard579a97f2007-11-11 14:26:47 +0000639static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
640 abi_ulong target_addr,
641 socklen_t len)
bellard7854b052003-03-29 17:22:23 +0000642{
pbrook53a59602006-03-25 19:31:22 +0000643 struct target_sockaddr *target_saddr;
644
bellard579a97f2007-11-11 14:26:47 +0000645 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
646 if (!target_saddr)
647 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +0000648 memcpy(addr, target_saddr, len);
649 addr->sa_family = tswap16(target_saddr->sa_family);
650 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000651
652 return 0;
bellard7854b052003-03-29 17:22:23 +0000653}
654
bellard579a97f2007-11-11 14:26:47 +0000655static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
656 struct sockaddr *addr,
657 socklen_t len)
bellard7854b052003-03-29 17:22:23 +0000658{
pbrook53a59602006-03-25 19:31:22 +0000659 struct target_sockaddr *target_saddr;
660
bellard579a97f2007-11-11 14:26:47 +0000661 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
662 if (!target_saddr)
663 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +0000664 memcpy(target_saddr, addr, len);
665 target_saddr->sa_family = tswap16(addr->sa_family);
666 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +0000667
668 return 0;
bellard7854b052003-03-29 17:22:23 +0000669}
670
pbrook53a59602006-03-25 19:31:22 +0000671/* ??? Should this also swap msgh->name? */
bellard7854b052003-03-29 17:22:23 +0000672static inline void target_to_host_cmsg(struct msghdr *msgh,
673 struct target_msghdr *target_msgh)
674{
675 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
676 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
677 socklen_t space = 0;
678
679 while (cmsg && target_cmsg) {
680 void *data = CMSG_DATA(cmsg);
681 void *target_data = TARGET_CMSG_DATA(target_cmsg);
682
ths5fafdf22007-09-16 21:08:06 +0000683 int len = tswapl(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +0000684 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
685
686 space += CMSG_SPACE(len);
687 if (space > msgh->msg_controllen) {
688 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +0000689 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +0000690 break;
691 }
692
693 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
694 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
695 cmsg->cmsg_len = CMSG_LEN(len);
696
bellard3532fa72006-06-24 15:06:03 +0000697 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +0000698 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
699 memcpy(data, target_data, len);
700 } else {
701 int *fd = (int *)data;
702 int *target_fd = (int *)target_data;
703 int i, numfds = len / sizeof(int);
704
705 for (i = 0; i < numfds; i++)
706 fd[i] = tswap32(target_fd[i]);
707 }
708
709 cmsg = CMSG_NXTHDR(msgh, cmsg);
710 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
711 }
712
713 msgh->msg_controllen = space;
714}
715
pbrook53a59602006-03-25 19:31:22 +0000716/* ??? Should this also swap msgh->name? */
bellard7854b052003-03-29 17:22:23 +0000717static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
718 struct msghdr *msgh)
719{
720 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
721 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
722 socklen_t space = 0;
723
724 while (cmsg && target_cmsg) {
725 void *data = CMSG_DATA(cmsg);
726 void *target_data = TARGET_CMSG_DATA(target_cmsg);
727
728 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
729
730 space += TARGET_CMSG_SPACE(len);
731 if (space > tswapl(target_msgh->msg_controllen)) {
732 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +0000733 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +0000734 break;
735 }
736
737 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
738 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
739 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
740
bellard3532fa72006-06-24 15:06:03 +0000741 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +0000742 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
743 memcpy(target_data, data, len);
744 } else {
745 int *fd = (int *)data;
746 int *target_fd = (int *)target_data;
747 int i, numfds = len / sizeof(int);
748
749 for (i = 0; i < numfds; i++)
750 target_fd[i] = tswap32(fd[i]);
751 }
752
753 cmsg = CMSG_NXTHDR(msgh, cmsg);
754 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
755 }
756
757 msgh->msg_controllen = tswapl(space);
758}
759
ths0da46a62007-10-20 20:23:07 +0000760/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000761static abi_long do_setsockopt(int sockfd, int level, int optname,
762 abi_ulong optval, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +0000763{
blueswir1992f48a2007-10-14 16:27:31 +0000764 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +0000765 int val;
ths3b46e622007-09-17 08:09:54 +0000766
bellard8853f862004-02-22 14:57:26 +0000767 switch(level) {
768 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +0000769 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +0000770 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +0000771 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +0000772
pbrook53a59602006-03-25 19:31:22 +0000773 val = tget32(optval);
bellard8853f862004-02-22 14:57:26 +0000774 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
775 break;
776 case SOL_IP:
777 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +0000778 case IP_TOS:
779 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +0000780 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +0000781 case IP_ROUTER_ALERT:
782 case IP_RECVOPTS:
783 case IP_RETOPTS:
784 case IP_PKTINFO:
785 case IP_MTU_DISCOVER:
786 case IP_RECVERR:
787 case IP_RECVTOS:
788#ifdef IP_FREEBIND
789 case IP_FREEBIND:
790#endif
791 case IP_MULTICAST_TTL:
792 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +0000793 val = 0;
794 if (optlen >= sizeof(uint32_t)) {
pbrook53a59602006-03-25 19:31:22 +0000795 val = tget32(optval);
bellard8853f862004-02-22 14:57:26 +0000796 } else if (optlen >= 1) {
pbrook53a59602006-03-25 19:31:22 +0000797 val = tget8(optval);
bellard8853f862004-02-22 14:57:26 +0000798 }
799 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
800 break;
801 default:
802 goto unimplemented;
803 }
804 break;
bellard3532fa72006-06-24 15:06:03 +0000805 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +0000806 switch (optname) {
807 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +0000808 case TARGET_SO_DEBUG:
809 optname = SO_DEBUG;
810 break;
811 case TARGET_SO_REUSEADDR:
812 optname = SO_REUSEADDR;
813 break;
814 case TARGET_SO_TYPE:
815 optname = SO_TYPE;
816 break;
817 case TARGET_SO_ERROR:
818 optname = SO_ERROR;
819 break;
820 case TARGET_SO_DONTROUTE:
821 optname = SO_DONTROUTE;
822 break;
823 case TARGET_SO_BROADCAST:
824 optname = SO_BROADCAST;
825 break;
826 case TARGET_SO_SNDBUF:
827 optname = SO_SNDBUF;
828 break;
829 case TARGET_SO_RCVBUF:
830 optname = SO_RCVBUF;
831 break;
832 case TARGET_SO_KEEPALIVE:
833 optname = SO_KEEPALIVE;
834 break;
835 case TARGET_SO_OOBINLINE:
836 optname = SO_OOBINLINE;
837 break;
838 case TARGET_SO_NO_CHECK:
839 optname = SO_NO_CHECK;
840 break;
841 case TARGET_SO_PRIORITY:
842 optname = SO_PRIORITY;
843 break;
bellard5e83e8e2005-03-01 22:32:06 +0000844#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +0000845 case TARGET_SO_BSDCOMPAT:
846 optname = SO_BSDCOMPAT;
847 break;
bellard5e83e8e2005-03-01 22:32:06 +0000848#endif
bellard3532fa72006-06-24 15:06:03 +0000849 case TARGET_SO_PASSCRED:
850 optname = SO_PASSCRED;
851 break;
852 case TARGET_SO_TIMESTAMP:
853 optname = SO_TIMESTAMP;
854 break;
855 case TARGET_SO_RCVLOWAT:
856 optname = SO_RCVLOWAT;
857 break;
858 case TARGET_SO_RCVTIMEO:
859 optname = SO_RCVTIMEO;
860 break;
861 case TARGET_SO_SNDTIMEO:
862 optname = SO_SNDTIMEO;
863 break;
bellard8853f862004-02-22 14:57:26 +0000864 break;
865 default:
866 goto unimplemented;
867 }
bellard3532fa72006-06-24 15:06:03 +0000868 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +0000869 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +0000870
871 val = tget32(optval);
872 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +0000873 break;
bellard7854b052003-03-29 17:22:23 +0000874 default:
bellard8853f862004-02-22 14:57:26 +0000875 unimplemented:
876 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
ths0da46a62007-10-20 20:23:07 +0000877 ret = -TARGET_ENOSYS;
bellard7854b052003-03-29 17:22:23 +0000878 }
bellard8853f862004-02-22 14:57:26 +0000879 return ret;
bellard7854b052003-03-29 17:22:23 +0000880}
881
ths0da46a62007-10-20 20:23:07 +0000882/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000883static abi_long do_getsockopt(int sockfd, int level, int optname,
884 abi_ulong optval, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +0000885{
blueswir1992f48a2007-10-14 16:27:31 +0000886 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +0000887 int len, lv, val;
bellard8853f862004-02-22 14:57:26 +0000888
889 switch(level) {
bellard3532fa72006-06-24 15:06:03 +0000890 case TARGET_SOL_SOCKET:
891 level = SOL_SOCKET;
bellard8853f862004-02-22 14:57:26 +0000892 switch (optname) {
bellard3532fa72006-06-24 15:06:03 +0000893 case TARGET_SO_LINGER:
894 case TARGET_SO_RCVTIMEO:
895 case TARGET_SO_SNDTIMEO:
896 case TARGET_SO_PEERCRED:
897 case TARGET_SO_PEERNAME:
bellard8853f862004-02-22 14:57:26 +0000898 /* These don't just return a single integer */
899 goto unimplemented;
900 default:
bellard2efbe912005-07-23 15:10:20 +0000901 goto int_case;
902 }
903 break;
904 case SOL_TCP:
905 /* TCP options all take an 'int' value. */
906 int_case:
pbrook53a59602006-03-25 19:31:22 +0000907 len = tget32(optlen);
bellard2efbe912005-07-23 15:10:20 +0000908 if (len < 0)
ths0da46a62007-10-20 20:23:07 +0000909 return -TARGET_EINVAL;
bellard2efbe912005-07-23 15:10:20 +0000910 lv = sizeof(int);
911 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
912 if (ret < 0)
913 return ret;
914 val = tswap32(val);
915 if (len > lv)
916 len = lv;
pbrook53a59602006-03-25 19:31:22 +0000917 if (len == 4)
918 tput32(optval, val);
919 else
920 tput8(optval, val);
921 tput32(optlen, len);
bellard2efbe912005-07-23 15:10:20 +0000922 break;
923 case SOL_IP:
924 switch(optname) {
925 case IP_TOS:
926 case IP_TTL:
927 case IP_HDRINCL:
928 case IP_ROUTER_ALERT:
929 case IP_RECVOPTS:
930 case IP_RETOPTS:
931 case IP_PKTINFO:
932 case IP_MTU_DISCOVER:
933 case IP_RECVERR:
934 case IP_RECVTOS:
935#ifdef IP_FREEBIND
936 case IP_FREEBIND:
937#endif
938 case IP_MULTICAST_TTL:
939 case IP_MULTICAST_LOOP:
pbrook53a59602006-03-25 19:31:22 +0000940 len = tget32(optlen);
bellard8853f862004-02-22 14:57:26 +0000941 if (len < 0)
ths0da46a62007-10-20 20:23:07 +0000942 return -TARGET_EINVAL;
bellard8853f862004-02-22 14:57:26 +0000943 lv = sizeof(int);
944 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
945 if (ret < 0)
946 return ret;
bellard2efbe912005-07-23 15:10:20 +0000947 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +0000948 len = 1;
pbrook53a59602006-03-25 19:31:22 +0000949 tput32(optlen, len);
950 tput8(optval, val);
bellard2efbe912005-07-23 15:10:20 +0000951 } else {
bellard2efbe912005-07-23 15:10:20 +0000952 if (len > sizeof(int))
953 len = sizeof(int);
pbrook53a59602006-03-25 19:31:22 +0000954 tput32(optlen, len);
955 tput32(optval, val);
bellard2efbe912005-07-23 15:10:20 +0000956 }
bellard8853f862004-02-22 14:57:26 +0000957 break;
bellard2efbe912005-07-23 15:10:20 +0000958 default:
959 goto unimplemented;
bellard8853f862004-02-22 14:57:26 +0000960 }
961 break;
962 default:
963 unimplemented:
964 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
965 level, optname);
ths0da46a62007-10-20 20:23:07 +0000966 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +0000967 break;
968 }
969 return ret;
bellard7854b052003-03-29 17:22:23 +0000970}
971
bellard579a97f2007-11-11 14:26:47 +0000972/* FIXME
973 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
974 * other lock functions have a return code of 0 for failure.
975 */
976static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
977 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +0000978{
979 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +0000980 abi_ulong base;
bellard579a97f2007-11-11 14:26:47 +0000981 int i, j;
pbrook53a59602006-03-25 19:31:22 +0000982
bellard579a97f2007-11-11 14:26:47 +0000983 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
984 if (!target_vec)
985 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +0000986 for(i = 0;i < count; i++) {
987 base = tswapl(target_vec[i].iov_base);
988 vec[i].iov_len = tswapl(target_vec[i].iov_len);
bellard579a97f2007-11-11 14:26:47 +0000989 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
990 if (!vec[i].iov_base)
991 goto fail;
pbrook53a59602006-03-25 19:31:22 +0000992 }
993 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000994 return 0;
995 fail:
996 /* failure - unwind locks */
997 for (j = 0; j < i; j++) {
998 base = tswapl(target_vec[j].iov_base);
999 unlock_user(vec[j].iov_base, base, 0);
1000 }
1001 unlock_user (target_vec, target_addr, 0);
1002 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001003}
1004
bellard579a97f2007-11-11 14:26:47 +00001005static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1006 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001007{
1008 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001009 abi_ulong base;
pbrook53a59602006-03-25 19:31:22 +00001010 int i;
1011
bellard579a97f2007-11-11 14:26:47 +00001012 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1013 if (!target_vec)
1014 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001015 for(i = 0;i < count; i++) {
1016 base = tswapl(target_vec[i].iov_base);
1017 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1018 }
1019 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001020
1021 return 0;
pbrook53a59602006-03-25 19:31:22 +00001022}
1023
ths0da46a62007-10-20 20:23:07 +00001024/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001025static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00001026{
1027#if defined(TARGET_MIPS)
1028 switch(type) {
1029 case TARGET_SOCK_DGRAM:
1030 type = SOCK_DGRAM;
1031 break;
1032 case TARGET_SOCK_STREAM:
1033 type = SOCK_STREAM;
1034 break;
1035 case TARGET_SOCK_RAW:
1036 type = SOCK_RAW;
1037 break;
1038 case TARGET_SOCK_RDM:
1039 type = SOCK_RDM;
1040 break;
1041 case TARGET_SOCK_SEQPACKET:
1042 type = SOCK_SEQPACKET;
1043 break;
1044 case TARGET_SOCK_PACKET:
1045 type = SOCK_PACKET;
1046 break;
1047 }
1048#endif
balrog12bc92a2007-10-30 21:06:14 +00001049 if (domain == PF_NETLINK)
1050 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
bellard3532fa72006-06-24 15:06:03 +00001051 return get_errno(socket(domain, type, protocol));
1052}
1053
ths0da46a62007-10-20 20:23:07 +00001054/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001055static abi_long do_bind(int sockfd, abi_ulong target_addr,
1056 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001057{
1058 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001059
bellard3532fa72006-06-24 15:06:03 +00001060 target_to_host_sockaddr(addr, target_addr, addrlen);
1061 return get_errno(bind(sockfd, addr, addrlen));
1062}
1063
ths0da46a62007-10-20 20:23:07 +00001064/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001065static abi_long do_connect(int sockfd, abi_ulong target_addr,
1066 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001067{
1068 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001069
bellard3532fa72006-06-24 15:06:03 +00001070 target_to_host_sockaddr(addr, target_addr, addrlen);
1071 return get_errno(connect(sockfd, addr, addrlen));
1072}
1073
ths0da46a62007-10-20 20:23:07 +00001074/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001075static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1076 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001077{
blueswir1992f48a2007-10-14 16:27:31 +00001078 abi_long ret;
bellard3532fa72006-06-24 15:06:03 +00001079 struct target_msghdr *msgp;
1080 struct msghdr msg;
1081 int count;
1082 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001083 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001084
bellard579a97f2007-11-11 14:26:47 +00001085 /* FIXME */
1086 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1087 msgp,
1088 target_msg,
1089 send ? 1 : 0))
1090 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001091 if (msgp->msg_name) {
1092 msg.msg_namelen = tswap32(msgp->msg_namelen);
1093 msg.msg_name = alloca(msg.msg_namelen);
1094 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1095 msg.msg_namelen);
1096 } else {
1097 msg.msg_name = NULL;
1098 msg.msg_namelen = 0;
1099 }
1100 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1101 msg.msg_control = alloca(msg.msg_controllen);
1102 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001103
bellard3532fa72006-06-24 15:06:03 +00001104 count = tswapl(msgp->msg_iovlen);
1105 vec = alloca(count * sizeof(struct iovec));
1106 target_vec = tswapl(msgp->msg_iov);
bellard579a97f2007-11-11 14:26:47 +00001107 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
bellard3532fa72006-06-24 15:06:03 +00001108 msg.msg_iovlen = count;
1109 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001110
bellard3532fa72006-06-24 15:06:03 +00001111 if (send) {
1112 target_to_host_cmsg(&msg, msgp);
1113 ret = get_errno(sendmsg(fd, &msg, flags));
1114 } else {
1115 ret = get_errno(recvmsg(fd, &msg, flags));
1116 if (!is_error(ret))
1117 host_to_target_cmsg(msgp, &msg);
1118 }
1119 unlock_iovec(vec, target_vec, count, !send);
bellard579a97f2007-11-11 14:26:47 +00001120 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00001121 return ret;
1122}
1123
ths0da46a62007-10-20 20:23:07 +00001124/* do_accept() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001125static abi_long do_accept(int fd, abi_ulong target_addr,
1126 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001127{
1128 socklen_t addrlen = tget32(target_addrlen);
ths197ea352006-12-11 19:13:59 +00001129 void *addr = alloca(addrlen);
blueswir1992f48a2007-10-14 16:27:31 +00001130 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001131
1132 ret = get_errno(accept(fd, addr, &addrlen));
1133 if (!is_error(ret)) {
1134 host_to_target_sockaddr(target_addr, addr, addrlen);
1135 tput32(target_addrlen, addrlen);
1136 }
1137 return ret;
1138}
1139
ths0da46a62007-10-20 20:23:07 +00001140/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001141static abi_long do_getpeername(int fd, abi_ulong target_addr,
1142 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001143{
1144 socklen_t addrlen = tget32(target_addrlen);
ths01c227f2007-03-19 13:05:13 +00001145 void *addr = alloca(addrlen);
blueswir1992f48a2007-10-14 16:27:31 +00001146 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001147
1148 ret = get_errno(getpeername(fd, addr, &addrlen));
1149 if (!is_error(ret)) {
1150 host_to_target_sockaddr(target_addr, addr, addrlen);
1151 tput32(target_addrlen, addrlen);
1152 }
1153 return ret;
1154}
1155
ths0da46a62007-10-20 20:23:07 +00001156/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001157static abi_long do_getsockname(int fd, abi_ulong target_addr,
1158 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001159{
1160 socklen_t addrlen = tget32(target_addrlen);
ths01c227f2007-03-19 13:05:13 +00001161 void *addr = alloca(addrlen);
blueswir1992f48a2007-10-14 16:27:31 +00001162 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001163
1164 ret = get_errno(getsockname(fd, addr, &addrlen));
1165 if (!is_error(ret)) {
1166 host_to_target_sockaddr(target_addr, addr, addrlen);
1167 tput32(target_addrlen, addrlen);
1168 }
1169 return ret;
1170}
1171
ths0da46a62007-10-20 20:23:07 +00001172/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001173static abi_long do_socketpair(int domain, int type, int protocol,
1174 abi_ulong target_tab)
pbrook1be9e1d2006-11-19 15:26:04 +00001175{
1176 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00001177 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001178
1179 ret = get_errno(socketpair(domain, type, protocol, tab));
1180 if (!is_error(ret)) {
1181 tput32(target_tab, tab[0]);
1182 tput32(target_tab + 4, tab[1]);
1183 }
1184 return ret;
1185}
1186
ths0da46a62007-10-20 20:23:07 +00001187/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001188static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1189 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001190{
1191 void *addr;
1192 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001193 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001194
bellard579a97f2007-11-11 14:26:47 +00001195 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1196 if (!host_msg)
1197 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001198 if (target_addr) {
1199 addr = alloca(addrlen);
1200 target_to_host_sockaddr(addr, target_addr, addrlen);
1201 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1202 } else {
1203 ret = get_errno(send(fd, host_msg, len, flags));
1204 }
1205 unlock_user(host_msg, msg, 0);
1206 return ret;
1207}
1208
ths0da46a62007-10-20 20:23:07 +00001209/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001210static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1211 abi_ulong target_addr,
1212 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001213{
1214 socklen_t addrlen;
1215 void *addr;
1216 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001217 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001218
bellard579a97f2007-11-11 14:26:47 +00001219 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1220 if (!host_msg)
1221 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001222 if (target_addr) {
1223 addrlen = tget32(target_addrlen);
1224 addr = alloca(addrlen);
1225 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1226 } else {
1227 addr = NULL; /* To keep compiler quiet. */
1228 ret = get_errno(recv(fd, host_msg, len, flags));
1229 }
1230 if (!is_error(ret)) {
1231 if (target_addr) {
1232 host_to_target_sockaddr(target_addr, addr, addrlen);
1233 tput32(target_addrlen, addrlen);
1234 }
1235 unlock_user(host_msg, msg, len);
1236 } else {
1237 unlock_user(host_msg, msg, 0);
1238 }
1239 return ret;
1240}
1241
j_mayer32407102007-09-26 23:01:49 +00001242#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00001243/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001244static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00001245{
blueswir1992f48a2007-10-14 16:27:31 +00001246 abi_long ret;
1247 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00001248
1249 switch(num) {
1250 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00001251 {
pbrook53a59602006-03-25 19:31:22 +00001252 int domain = tgetl(vptr);
1253 int type = tgetl(vptr + n);
1254 int protocol = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001255 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00001256 }
bellard31e31b82003-02-18 22:55:36 +00001257 break;
1258 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00001259 {
pbrook53a59602006-03-25 19:31:22 +00001260 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001261 abi_ulong target_addr = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001262 socklen_t addrlen = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001263 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001264 }
bellard31e31b82003-02-18 22:55:36 +00001265 break;
1266 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00001267 {
pbrook53a59602006-03-25 19:31:22 +00001268 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001269 abi_ulong target_addr = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001270 socklen_t addrlen = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001271 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001272 }
bellard31e31b82003-02-18 22:55:36 +00001273 break;
1274 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00001275 {
pbrook53a59602006-03-25 19:31:22 +00001276 int sockfd = tgetl(vptr);
1277 int backlog = tgetl(vptr + n);
bellard7854b052003-03-29 17:22:23 +00001278 ret = get_errno(listen(sockfd, backlog));
1279 }
bellard31e31b82003-02-18 22:55:36 +00001280 break;
1281 case SOCKOP_accept:
1282 {
pbrook53a59602006-03-25 19:31:22 +00001283 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001284 abi_ulong target_addr = tgetl(vptr + n);
1285 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001286 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001287 }
1288 break;
1289 case SOCKOP_getsockname:
1290 {
pbrook53a59602006-03-25 19:31:22 +00001291 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001292 abi_ulong target_addr = tgetl(vptr + n);
1293 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001294 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001295 }
1296 break;
1297 case SOCKOP_getpeername:
1298 {
pbrook53a59602006-03-25 19:31:22 +00001299 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001300 abi_ulong target_addr = tgetl(vptr + n);
1301 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001302 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001303 }
1304 break;
1305 case SOCKOP_socketpair:
1306 {
pbrook53a59602006-03-25 19:31:22 +00001307 int domain = tgetl(vptr);
1308 int type = tgetl(vptr + n);
1309 int protocol = tgetl(vptr + 2 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001310 abi_ulong tab = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001311 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00001312 }
1313 break;
1314 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00001315 {
pbrook53a59602006-03-25 19:31:22 +00001316 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001317 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001318 size_t len = tgetl(vptr + 2 * n);
1319 int flags = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001320 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001321 }
bellard31e31b82003-02-18 22:55:36 +00001322 break;
1323 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00001324 {
pbrook53a59602006-03-25 19:31:22 +00001325 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001326 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001327 size_t len = tgetl(vptr + 2 * n);
1328 int flags = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001329 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001330 }
bellard31e31b82003-02-18 22:55:36 +00001331 break;
1332 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00001333 {
pbrook53a59602006-03-25 19:31:22 +00001334 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001335 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001336 size_t len = tgetl(vptr + 2 * n);
1337 int flags = tgetl(vptr + 3 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001338 abi_ulong addr = tgetl(vptr + 4 * n);
pbrook53a59602006-03-25 19:31:22 +00001339 socklen_t addrlen = tgetl(vptr + 5 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001340 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001341 }
bellard31e31b82003-02-18 22:55:36 +00001342 break;
1343 case SOCKOP_recvfrom:
1344 {
pbrook53a59602006-03-25 19:31:22 +00001345 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001346 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001347 size_t len = tgetl(vptr + 2 * n);
1348 int flags = tgetl(vptr + 3 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001349 abi_ulong addr = tgetl(vptr + 4 * n);
1350 abi_ulong addrlen = tgetl(vptr + 5 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001351 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00001352 }
1353 break;
1354 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00001355 {
pbrook53a59602006-03-25 19:31:22 +00001356 int sockfd = tgetl(vptr);
1357 int how = tgetl(vptr + n);
bellard7854b052003-03-29 17:22:23 +00001358
1359 ret = get_errno(shutdown(sockfd, how));
1360 }
bellard31e31b82003-02-18 22:55:36 +00001361 break;
1362 case SOCKOP_sendmsg:
1363 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00001364 {
1365 int fd;
blueswir1992f48a2007-10-14 16:27:31 +00001366 abi_ulong target_msg;
bellard3532fa72006-06-24 15:06:03 +00001367 int flags;
bellard1a9353d2003-03-16 20:28:50 +00001368
pbrook53a59602006-03-25 19:31:22 +00001369 fd = tgetl(vptr);
bellard3532fa72006-06-24 15:06:03 +00001370 target_msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001371 flags = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001372
ths5fafdf22007-09-16 21:08:06 +00001373 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00001374 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00001375 }
1376 break;
bellard31e31b82003-02-18 22:55:36 +00001377 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00001378 {
pbrook53a59602006-03-25 19:31:22 +00001379 int sockfd = tgetl(vptr);
1380 int level = tgetl(vptr + n);
1381 int optname = tgetl(vptr + 2 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001382 abi_ulong optval = tgetl(vptr + 3 * n);
pbrook53a59602006-03-25 19:31:22 +00001383 socklen_t optlen = tgetl(vptr + 4 * n);
bellard7854b052003-03-29 17:22:23 +00001384
1385 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1386 }
1387 break;
bellard31e31b82003-02-18 22:55:36 +00001388 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00001389 {
pbrook53a59602006-03-25 19:31:22 +00001390 int sockfd = tgetl(vptr);
1391 int level = tgetl(vptr + n);
1392 int optname = tgetl(vptr + 2 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001393 abi_ulong optval = tgetl(vptr + 3 * n);
1394 abi_ulong poptlen = tgetl(vptr + 4 * n);
bellard7854b052003-03-29 17:22:23 +00001395
bellard8853f862004-02-22 14:57:26 +00001396 ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
bellard7854b052003-03-29 17:22:23 +00001397 }
1398 break;
bellard31e31b82003-02-18 22:55:36 +00001399 default:
1400 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00001401 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00001402 break;
1403 }
1404 return ret;
1405}
j_mayer32407102007-09-26 23:01:49 +00001406#endif
bellard31e31b82003-02-18 22:55:36 +00001407
j_mayer32407102007-09-26 23:01:49 +00001408#ifdef TARGET_NR_ipc
bellard8853f862004-02-22 14:57:26 +00001409#define N_SHM_REGIONS 32
1410
1411static struct shm_region {
1412 uint32_t start;
1413 uint32_t size;
1414} shm_regions[N_SHM_REGIONS];
1415
ths3eb6b042007-06-03 14:26:27 +00001416struct target_ipc_perm
1417{
blueswir1992f48a2007-10-14 16:27:31 +00001418 abi_long __key;
1419 abi_ulong uid;
1420 abi_ulong gid;
1421 abi_ulong cuid;
1422 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00001423 unsigned short int mode;
1424 unsigned short int __pad1;
1425 unsigned short int __seq;
1426 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00001427 abi_ulong __unused1;
1428 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00001429};
1430
1431struct target_semid_ds
1432{
1433 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001434 abi_ulong sem_otime;
1435 abi_ulong __unused1;
1436 abi_ulong sem_ctime;
1437 abi_ulong __unused2;
1438 abi_ulong sem_nsems;
1439 abi_ulong __unused3;
1440 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00001441};
1442
bellard579a97f2007-11-11 14:26:47 +00001443static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
1444 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001445{
1446 struct target_ipc_perm *target_ip;
1447 struct target_semid_ds *target_sd;
1448
bellard579a97f2007-11-11 14:26:47 +00001449 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1450 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001451 target_ip=&(target_sd->sem_perm);
1452 host_ip->__key = tswapl(target_ip->__key);
1453 host_ip->uid = tswapl(target_ip->uid);
1454 host_ip->gid = tswapl(target_ip->gid);
1455 host_ip->cuid = tswapl(target_ip->cuid);
1456 host_ip->cgid = tswapl(target_ip->cgid);
1457 host_ip->mode = tswapl(target_ip->mode);
1458 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001459 return 0;
ths3eb6b042007-06-03 14:26:27 +00001460}
1461
bellard579a97f2007-11-11 14:26:47 +00001462static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
1463 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00001464{
1465 struct target_ipc_perm *target_ip;
1466 struct target_semid_ds *target_sd;
1467
bellard579a97f2007-11-11 14:26:47 +00001468 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1469 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001470 target_ip = &(target_sd->sem_perm);
1471 target_ip->__key = tswapl(host_ip->__key);
1472 target_ip->uid = tswapl(host_ip->uid);
1473 target_ip->gid = tswapl(host_ip->gid);
1474 target_ip->cuid = tswapl(host_ip->cuid);
1475 target_ip->cgid = tswapl(host_ip->cgid);
1476 target_ip->mode = tswapl(host_ip->mode);
1477 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001478 return 0;
ths3eb6b042007-06-03 14:26:27 +00001479}
1480
bellard579a97f2007-11-11 14:26:47 +00001481static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
1482 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001483{
1484 struct target_semid_ds *target_sd;
1485
bellard579a97f2007-11-11 14:26:47 +00001486 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1487 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001488 target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1489 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1490 host_sd->sem_otime = tswapl(target_sd->sem_otime);
1491 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1492 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001493 return 0;
ths3eb6b042007-06-03 14:26:27 +00001494}
1495
bellard579a97f2007-11-11 14:26:47 +00001496static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
1497 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00001498{
1499 struct target_semid_ds *target_sd;
1500
bellard579a97f2007-11-11 14:26:47 +00001501 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1502 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001503 host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1504 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1505 target_sd->sem_otime = tswapl(host_sd->sem_otime);
1506 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1507 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001508 return 0;
ths3eb6b042007-06-03 14:26:27 +00001509}
1510
thsfa294812007-02-02 22:05:00 +00001511union semun {
1512 int val;
ths3eb6b042007-06-03 14:26:27 +00001513 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00001514 unsigned short *array;
1515};
1516
ths3eb6b042007-06-03 14:26:27 +00001517union target_semun {
1518 int val;
blueswir1992f48a2007-10-14 16:27:31 +00001519 abi_long buf;
ths3eb6b042007-06-03 14:26:27 +00001520 unsigned short int *array;
1521};
1522
bellard579a97f2007-11-11 14:26:47 +00001523static inline abi_long target_to_host_semun(int cmd,
1524 union semun *host_su,
1525 abi_ulong target_addr,
1526 struct semid_ds *ds)
ths3eb6b042007-06-03 14:26:27 +00001527{
1528 union target_semun *target_su;
1529
1530 switch( cmd ) {
1531 case IPC_STAT:
1532 case IPC_SET:
bellard579a97f2007-11-11 14:26:47 +00001533 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1534 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001535 target_to_host_semid_ds(ds,target_su->buf);
1536 host_su->buf = ds;
1537 unlock_user_struct(target_su, target_addr, 0);
1538 break;
1539 case GETVAL:
1540 case SETVAL:
bellard579a97f2007-11-11 14:26:47 +00001541 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1542 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001543 host_su->val = tswapl(target_su->val);
1544 unlock_user_struct(target_su, target_addr, 0);
1545 break;
1546 case GETALL:
1547 case SETALL:
bellard579a97f2007-11-11 14:26:47 +00001548 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1549 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001550 *host_su->array = tswap16(*target_su->array);
1551 unlock_user_struct(target_su, target_addr, 0);
1552 break;
1553 default:
1554 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1555 }
bellard579a97f2007-11-11 14:26:47 +00001556 return 0;
ths3eb6b042007-06-03 14:26:27 +00001557}
1558
bellard579a97f2007-11-11 14:26:47 +00001559static inline abi_long host_to_target_semun(int cmd,
1560 abi_ulong target_addr,
1561 union semun *host_su,
1562 struct semid_ds *ds)
ths3eb6b042007-06-03 14:26:27 +00001563{
1564 union target_semun *target_su;
1565
1566 switch( cmd ) {
1567 case IPC_STAT:
1568 case IPC_SET:
bellard579a97f2007-11-11 14:26:47 +00001569 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1570 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001571 host_to_target_semid_ds(target_su->buf,ds);
1572 unlock_user_struct(target_su, target_addr, 1);
1573 break;
1574 case GETVAL:
1575 case SETVAL:
bellard579a97f2007-11-11 14:26:47 +00001576 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1577 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001578 target_su->val = tswapl(host_su->val);
1579 unlock_user_struct(target_su, target_addr, 1);
1580 break;
1581 case GETALL:
1582 case SETALL:
bellard579a97f2007-11-11 14:26:47 +00001583 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1584 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001585 *target_su->array = tswap16(*host_su->array);
1586 unlock_user_struct(target_su, target_addr, 1);
1587 break;
1588 default:
1589 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1590 }
bellard579a97f2007-11-11 14:26:47 +00001591 return 0;
ths3eb6b042007-06-03 14:26:27 +00001592}
1593
blueswir1992f48a2007-10-14 16:27:31 +00001594static inline abi_long do_semctl(int first, int second, int third,
1595 abi_long ptr)
ths3eb6b042007-06-03 14:26:27 +00001596{
1597 union semun arg;
1598 struct semid_ds dsarg;
1599 int cmd = third&0xff;
blueswir1992f48a2007-10-14 16:27:31 +00001600 abi_long ret = 0;
ths3eb6b042007-06-03 14:26:27 +00001601
1602 switch( cmd ) {
1603 case GETVAL:
1604 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1605 ret = get_errno(semctl(first, second, cmd, arg));
1606 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1607 break;
1608 case SETVAL:
1609 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1610 ret = get_errno(semctl(first, second, cmd, arg));
1611 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1612 break;
1613 case GETALL:
1614 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1615 ret = get_errno(semctl(first, second, cmd, arg));
1616 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1617 break;
1618 case SETALL:
1619 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1620 ret = get_errno(semctl(first, second, cmd, arg));
1621 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1622 break;
1623 case IPC_STAT:
1624 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1625 ret = get_errno(semctl(first, second, cmd, arg));
1626 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1627 break;
1628 case IPC_SET:
1629 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1630 ret = get_errno(semctl(first, second, cmd, arg));
1631 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1632 break;
1633 default:
1634 ret = get_errno(semctl(first, second, cmd, arg));
1635 }
1636
1637 return ret;
1638}
1639
ths1bc012f2007-06-03 14:27:49 +00001640struct target_msqid_ds
1641{
1642 struct target_ipc_perm msg_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001643 abi_ulong msg_stime;
1644 abi_ulong __unused1;
1645 abi_ulong msg_rtime;
1646 abi_ulong __unused2;
1647 abi_ulong msg_ctime;
1648 abi_ulong __unused3;
1649 abi_ulong __msg_cbytes;
1650 abi_ulong msg_qnum;
1651 abi_ulong msg_qbytes;
1652 abi_ulong msg_lspid;
1653 abi_ulong msg_lrpid;
1654 abi_ulong __unused4;
1655 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00001656};
1657
bellard579a97f2007-11-11 14:26:47 +00001658static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
1659 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00001660{
1661 struct target_msqid_ds *target_md;
1662
bellard579a97f2007-11-11 14:26:47 +00001663 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
1664 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001665 target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1666 host_md->msg_stime = tswapl(target_md->msg_stime);
1667 host_md->msg_rtime = tswapl(target_md->msg_rtime);
1668 host_md->msg_ctime = tswapl(target_md->msg_ctime);
1669 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1670 host_md->msg_qnum = tswapl(target_md->msg_qnum);
1671 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1672 host_md->msg_lspid = tswapl(target_md->msg_lspid);
1673 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1674 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001675 return 0;
ths1bc012f2007-06-03 14:27:49 +00001676}
1677
bellard579a97f2007-11-11 14:26:47 +00001678static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
1679 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00001680{
1681 struct target_msqid_ds *target_md;
1682
bellard579a97f2007-11-11 14:26:47 +00001683 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
1684 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001685 host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1686 target_md->msg_stime = tswapl(host_md->msg_stime);
1687 target_md->msg_rtime = tswapl(host_md->msg_rtime);
1688 target_md->msg_ctime = tswapl(host_md->msg_ctime);
1689 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1690 target_md->msg_qnum = tswapl(host_md->msg_qnum);
1691 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1692 target_md->msg_lspid = tswapl(host_md->msg_lspid);
1693 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1694 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001695 return 0;
ths1bc012f2007-06-03 14:27:49 +00001696}
1697
blueswir1992f48a2007-10-14 16:27:31 +00001698static inline abi_long do_msgctl(int first, int second, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00001699{
1700 struct msqid_ds dsarg;
1701 int cmd = second&0xff;
blueswir1992f48a2007-10-14 16:27:31 +00001702 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001703 switch( cmd ) {
1704 case IPC_STAT:
1705 case IPC_SET:
1706 target_to_host_msqid_ds(&dsarg,ptr);
1707 ret = get_errno(msgctl(first, cmd, &dsarg));
1708 host_to_target_msqid_ds(ptr,&dsarg);
1709 default:
1710 ret = get_errno(msgctl(first, cmd, &dsarg));
1711 }
1712 return ret;
1713}
1714
1715struct target_msgbuf {
blueswir1992f48a2007-10-14 16:27:31 +00001716 abi_ulong mtype;
ths1bc012f2007-06-03 14:27:49 +00001717 char mtext[1];
1718};
1719
blueswir1992f48a2007-10-14 16:27:31 +00001720static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1721 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001722{
1723 struct target_msgbuf *target_mb;
1724 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00001725 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001726
bellard579a97f2007-11-11 14:26:47 +00001727 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
1728 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001729 host_mb = malloc(msgsz+sizeof(long));
1730 host_mb->mtype = tswapl(target_mb->mtype);
1731 memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1732 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1733 free(host_mb);
1734 unlock_user_struct(target_mb, msgp, 0);
1735
1736 return ret;
1737}
1738
blueswir1992f48a2007-10-14 16:27:31 +00001739static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1740 unsigned int msgsz, int msgtype,
1741 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001742{
1743 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00001744 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00001745 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00001746 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001747
bellard579a97f2007-11-11 14:26:47 +00001748 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
1749 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001750 host_mb = malloc(msgsz+sizeof(long));
1751 ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
bellard579a97f2007-11-11 14:26:47 +00001752 if (ret > 0) {
1753 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
1754 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
1755 if (!target_mtext) {
1756 ret = -TARGET_EFAULT;
1757 goto end;
1758 }
ths1bc012f2007-06-03 14:27:49 +00001759 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00001760 unlock_user(target_mtext, target_mtext_addr, ret);
1761 }
ths1bc012f2007-06-03 14:27:49 +00001762 target_mb->mtype = tswapl(host_mb->mtype);
1763 free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00001764
bellard579a97f2007-11-11 14:26:47 +00001765end:
1766 if (target_mb)
1767 unlock_user_struct(target_mb, msgp, 1);
ths1bc012f2007-06-03 14:27:49 +00001768 return ret;
1769}
1770
pbrook53a59602006-03-25 19:31:22 +00001771/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00001772/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001773static abi_long do_ipc(unsigned int call, int first,
1774 int second, int third,
1775 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00001776{
1777 int version;
blueswir1992f48a2007-10-14 16:27:31 +00001778 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00001779 unsigned long raddr;
1780 struct shmid_ds shm_info;
1781 int i;
1782
1783 version = call >> 16;
1784 call &= 0xffff;
1785
1786 switch (call) {
thsfa294812007-02-02 22:05:00 +00001787 case IPCOP_semop:
bellard579a97f2007-11-11 14:26:47 +00001788 ret = get_errno(semop(first,(struct sembuf *)g2h(ptr), second));
thsfa294812007-02-02 22:05:00 +00001789 break;
1790
1791 case IPCOP_semget:
1792 ret = get_errno(semget(first, second, third));
1793 break;
1794
1795 case IPCOP_semctl:
ths3eb6b042007-06-03 14:26:27 +00001796 ret = do_semctl(first, second, third, ptr);
thsfa294812007-02-02 22:05:00 +00001797 break;
1798
1799 case IPCOP_semtimedop:
j_mayer32407102007-09-26 23:01:49 +00001800 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00001801 ret = -TARGET_ENOSYS;
thsfa294812007-02-02 22:05:00 +00001802 break;
thsd96372e2007-02-02 22:05:44 +00001803
1804 case IPCOP_msgget:
1805 ret = get_errno(msgget(first, second));
1806 break;
1807
1808 case IPCOP_msgsnd:
ths1bc012f2007-06-03 14:27:49 +00001809 ret = do_msgsnd(first, ptr, second, third);
thsd96372e2007-02-02 22:05:44 +00001810 break;
1811
1812 case IPCOP_msgctl:
ths1bc012f2007-06-03 14:27:49 +00001813 ret = do_msgctl(first, second, ptr);
thsd96372e2007-02-02 22:05:44 +00001814 break;
1815
1816 case IPCOP_msgrcv:
ths1bc012f2007-06-03 14:27:49 +00001817 {
bellard579a97f2007-11-11 14:26:47 +00001818 /* XXX: this code is not correct */
ths1bc012f2007-06-03 14:27:49 +00001819 struct ipc_kludge
1820 {
1821 void *__unbounded msgp;
1822 long int msgtyp;
1823 };
thsd96372e2007-02-02 22:05:44 +00001824
bellard579a97f2007-11-11 14:26:47 +00001825 struct ipc_kludge *foo = (struct ipc_kludge *)g2h(ptr);
ths1bc012f2007-06-03 14:27:49 +00001826 struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
thsd96372e2007-02-02 22:05:44 +00001827
ths1bc012f2007-06-03 14:27:49 +00001828 ret = do_msgrcv(first, (long)msgp, second, 0, third);
thsd96372e2007-02-02 22:05:44 +00001829
ths1bc012f2007-06-03 14:27:49 +00001830 }
thsd96372e2007-02-02 22:05:44 +00001831 break;
1832
bellard8853f862004-02-22 14:57:26 +00001833 case IPCOP_shmat:
1834 /* SHM_* flags are the same on all linux platforms */
1835 ret = get_errno((long) shmat(first, (void *) ptr, second));
1836 if (is_error(ret))
1837 break;
1838 raddr = ret;
1839 /* find out the length of the shared memory segment */
ths3b46e622007-09-17 08:09:54 +00001840
bellard8853f862004-02-22 14:57:26 +00001841 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
1842 if (is_error(ret)) {
1843 /* can't get length, bail out */
1844 shmdt((void *) raddr);
1845 break;
1846 }
1847 page_set_flags(raddr, raddr + shm_info.shm_segsz,
1848 PAGE_VALID | PAGE_READ |
1849 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
1850 for (i = 0; i < N_SHM_REGIONS; ++i) {
1851 if (shm_regions[i].start == 0) {
1852 shm_regions[i].start = raddr;
1853 shm_regions[i].size = shm_info.shm_segsz;
1854 break;
1855 }
1856 }
bellard579a97f2007-11-11 14:26:47 +00001857 if (put_user(raddr, third, abi_ulong))
ths0da46a62007-10-20 20:23:07 +00001858 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001859 ret = 0;
1860 break;
1861 case IPCOP_shmdt:
1862 for (i = 0; i < N_SHM_REGIONS; ++i) {
1863 if (shm_regions[i].start == ptr) {
1864 shm_regions[i].start = 0;
1865 page_set_flags(ptr, shm_regions[i].size, 0);
1866 break;
1867 }
1868 }
1869 ret = get_errno(shmdt((void *) ptr));
1870 break;
1871
1872 case IPCOP_shmget:
1873 /* IPC_* flag values are the same on all linux platforms */
1874 ret = get_errno(shmget(first, second, third));
1875 break;
1876
1877 /* IPC_* and SHM_* command values are the same on all linux platforms */
1878 case IPCOP_shmctl:
1879 switch(second) {
1880 case IPC_RMID:
1881 case SHM_LOCK:
1882 case SHM_UNLOCK:
1883 ret = get_errno(shmctl(first, second, NULL));
1884 break;
1885 default:
1886 goto unimplemented;
1887 }
1888 break;
1889 default:
1890 unimplemented:
j_mayer32407102007-09-26 23:01:49 +00001891 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00001892 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00001893 break;
1894 }
1895 return ret;
1896}
j_mayer32407102007-09-26 23:01:49 +00001897#endif
bellard8853f862004-02-22 14:57:26 +00001898
bellard31e31b82003-02-18 22:55:36 +00001899/* kernel structure types definitions */
1900#define IFNAMSIZ 16
1901
1902#define STRUCT(name, list...) STRUCT_ ## name,
1903#define STRUCT_SPECIAL(name) STRUCT_ ## name,
1904enum {
1905#include "syscall_types.h"
1906};
1907#undef STRUCT
1908#undef STRUCT_SPECIAL
1909
1910#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
1911#define STRUCT_SPECIAL(name)
1912#include "syscall_types.h"
1913#undef STRUCT
1914#undef STRUCT_SPECIAL
1915
1916typedef struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00001917 unsigned int target_cmd;
1918 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00001919 const char *name;
1920 int access;
bellard1a9353d2003-03-16 20:28:50 +00001921 const argtype arg_type[5];
bellard31e31b82003-02-18 22:55:36 +00001922} IOCTLEntry;
1923
1924#define IOC_R 0x0001
1925#define IOC_W 0x0002
1926#define IOC_RW (IOC_R | IOC_W)
1927
1928#define MAX_STRUCT_SIZE 4096
1929
bellard2ab83ea2003-06-15 19:56:46 +00001930IOCTLEntry ioctl_entries[] = {
bellard31e31b82003-02-18 22:55:36 +00001931#define IOCTL(cmd, access, types...) \
1932 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
1933#include "ioctls.h"
1934 { 0, 0, },
1935};
1936
pbrook53a59602006-03-25 19:31:22 +00001937/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00001938/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001939static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00001940{
1941 const IOCTLEntry *ie;
1942 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00001943 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00001944 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00001945 int target_size;
1946 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00001947
1948 ie = ioctl_entries;
1949 for(;;) {
1950 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00001951 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00001952 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00001953 }
1954 if (ie->target_cmd == cmd)
1955 break;
1956 ie++;
1957 }
1958 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00001959#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00001960 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00001961#endif
bellard31e31b82003-02-18 22:55:36 +00001962 switch(arg_type[0]) {
1963 case TYPE_NULL:
1964 /* no argument */
1965 ret = get_errno(ioctl(fd, ie->host_cmd));
1966 break;
1967 case TYPE_PTRVOID:
1968 case TYPE_INT:
1969 /* int argment */
1970 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
1971 break;
1972 case TYPE_PTR:
1973 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00001974 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00001975 switch(ie->access) {
1976 case IOC_R:
1977 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1978 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00001979 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
1980 if (!argptr)
1981 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001982 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1983 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00001984 }
1985 break;
1986 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00001987 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
1988 if (!argptr)
1989 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001990 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1991 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00001992 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1993 break;
1994 default:
1995 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00001996 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
1997 if (!argptr)
1998 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001999 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2000 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002001 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2002 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002003 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2004 if (!argptr)
2005 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002006 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2007 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00002008 }
2009 break;
2010 }
2011 break;
2012 default:
j_mayer32407102007-09-26 23:01:49 +00002013 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2014 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00002015 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002016 break;
2017 }
2018 return ret;
2019}
2020
2021bitmask_transtbl iflag_tbl[] = {
2022 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2023 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2024 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2025 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2026 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2027 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2028 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2029 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2030 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2031 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2032 { TARGET_IXON, TARGET_IXON, IXON, IXON },
2033 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2034 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
2035 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
2036 { 0, 0, 0, 0 }
2037};
2038
2039bitmask_transtbl oflag_tbl[] = {
2040 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
2041 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
2042 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
2043 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
2044 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
2045 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
2046 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
2047 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
2048 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
2049 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
2050 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
2051 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
2052 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
2053 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
2054 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
2055 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
2056 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
2057 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
2058 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
2059 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
2060 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
2061 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
2062 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
2063 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
2064 { 0, 0, 0, 0 }
2065};
2066
2067bitmask_transtbl cflag_tbl[] = {
2068 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
2069 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
2070 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
2071 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
2072 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
2073 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
2074 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
2075 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
2076 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
2077 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
2078 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
2079 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
2080 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
2081 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
2082 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
2083 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
2084 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
2085 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
2086 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
2087 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
2088 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
2089 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
2090 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
2091 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
2092 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
2093 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
2094 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
2095 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
2096 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
2097 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
2098 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
2099 { 0, 0, 0, 0 }
2100};
2101
2102bitmask_transtbl lflag_tbl[] = {
2103 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2104 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2105 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2106 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2107 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2108 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2109 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2110 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2111 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2112 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2113 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2114 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2115 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2116 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2117 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
2118 { 0, 0, 0, 0 }
2119};
2120
2121static void target_to_host_termios (void *dst, const void *src)
2122{
2123 struct host_termios *host = dst;
2124 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00002125
ths5fafdf22007-09-16 21:08:06 +00002126 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002127 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002128 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002129 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002130 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002131 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002132 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002133 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2134 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00002135
ths5fafdf22007-09-16 21:08:06 +00002136 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2137 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00002138 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00002139 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00002140 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00002141 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00002142 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00002143 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00002144 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00002145 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2146 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00002147 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2148 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2149 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2150 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2151 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00002152 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00002153}
ths3b46e622007-09-17 08:09:54 +00002154
bellard31e31b82003-02-18 22:55:36 +00002155static void host_to_target_termios (void *dst, const void *src)
2156{
2157 struct target_termios *target = dst;
2158 const struct host_termios *host = src;
2159
ths5fafdf22007-09-16 21:08:06 +00002160 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002161 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002162 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002163 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002164 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002165 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002166 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002167 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2168 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00002169
bellard31e31b82003-02-18 22:55:36 +00002170 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2171 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2172 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2173 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2174 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2175 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2176 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2177 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2178 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2179 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2180 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2181 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2182 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2183 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2184 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2185 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2186 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2187}
2188
2189StructEntry struct_termios_def = {
2190 .convert = { host_to_target_termios, target_to_host_termios },
2191 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2192 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2193};
2194
bellard5286db72003-06-05 00:57:30 +00002195static bitmask_transtbl mmap_flags_tbl[] = {
2196 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2197 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2198 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2199 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2200 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2201 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2202 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2203 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2204 { 0, 0, 0, 0 }
2205};
2206
bellardffa65c32004-01-04 23:57:22 +00002207static bitmask_transtbl fcntl_flags_tbl[] = {
2208 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
2209 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
2210 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
2211 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
2212 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
2213 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
2214 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
2215 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
2216 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
2217 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
2218 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2219 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
2220 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
bellard121061d2004-02-22 11:56:01 +00002221#if defined(O_DIRECT)
bellardffa65c32004-01-04 23:57:22 +00002222 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
bellard121061d2004-02-22 11:56:01 +00002223#endif
bellardffa65c32004-01-04 23:57:22 +00002224 { 0, 0, 0, 0 }
2225};
2226
bellard2ab83ea2003-06-15 19:56:46 +00002227#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00002228
2229/* NOTE: there is really one LDT for all the threads */
2230uint8_t *ldt_table;
2231
bellard03acab62007-11-11 14:57:14 +00002232static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002233{
2234 int size;
pbrook53a59602006-03-25 19:31:22 +00002235 void *p;
bellard6dbad632003-03-16 18:05:05 +00002236
2237 if (!ldt_table)
2238 return 0;
2239 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2240 if (size > bytecount)
2241 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00002242 p = lock_user(VERIFY_WRITE, ptr, size, 0);
2243 if (!p)
bellard03acab62007-11-11 14:57:14 +00002244 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00002245 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00002246 memcpy(p, ldt_table, size);
2247 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00002248 return size;
2249}
2250
2251/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00002252static abi_long write_ldt(CPUX86State *env,
2253 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00002254{
2255 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00002256 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00002257 int seg_32bit, contents, read_exec_only, limit_in_pages;
2258 int seg_not_present, useable;
2259 uint32_t *lp, entry_1, entry_2;
2260
2261 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00002262 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00002263 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00002264 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002265 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2266 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2267 ldt_info.limit = tswap32(target_ldt_info->limit);
2268 ldt_info.flags = tswap32(target_ldt_info->flags);
2269 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00002270
bellard6dbad632003-03-16 18:05:05 +00002271 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00002272 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002273 seg_32bit = ldt_info.flags & 1;
2274 contents = (ldt_info.flags >> 1) & 3;
2275 read_exec_only = (ldt_info.flags >> 3) & 1;
2276 limit_in_pages = (ldt_info.flags >> 4) & 1;
2277 seg_not_present = (ldt_info.flags >> 5) & 1;
2278 useable = (ldt_info.flags >> 6) & 1;
2279
2280 if (contents == 3) {
2281 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00002282 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002283 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00002284 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002285 }
2286 /* allocate the LDT */
2287 if (!ldt_table) {
2288 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2289 if (!ldt_table)
bellard03acab62007-11-11 14:57:14 +00002290 return -TARGET_ENOMEM;
bellard6dbad632003-03-16 18:05:05 +00002291 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
pbrook53a59602006-03-25 19:31:22 +00002292 env->ldt.base = h2g(ldt_table);
bellard6dbad632003-03-16 18:05:05 +00002293 env->ldt.limit = 0xffff;
2294 }
2295
2296 /* NOTE: same code as Linux kernel */
2297 /* Allow LDTs to be cleared by the user. */
2298 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2299 if (oldmode ||
2300 (contents == 0 &&
2301 read_exec_only == 1 &&
2302 seg_32bit == 0 &&
2303 limit_in_pages == 0 &&
2304 seg_not_present == 1 &&
2305 useable == 0 )) {
2306 entry_1 = 0;
2307 entry_2 = 0;
2308 goto install;
2309 }
2310 }
ths3b46e622007-09-17 08:09:54 +00002311
bellard6dbad632003-03-16 18:05:05 +00002312 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2313 (ldt_info.limit & 0x0ffff);
2314 entry_2 = (ldt_info.base_addr & 0xff000000) |
2315 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2316 (ldt_info.limit & 0xf0000) |
2317 ((read_exec_only ^ 1) << 9) |
2318 (contents << 10) |
2319 ((seg_not_present ^ 1) << 15) |
2320 (seg_32bit << 22) |
2321 (limit_in_pages << 23) |
2322 0x7000;
2323 if (!oldmode)
2324 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00002325
bellard6dbad632003-03-16 18:05:05 +00002326 /* Install the new entry ... */
2327install:
2328 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2329 lp[0] = tswap32(entry_1);
2330 lp[1] = tswap32(entry_2);
2331 return 0;
2332}
2333
2334/* specific and weird i386 syscalls */
bellard03acab62007-11-11 14:57:14 +00002335abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
2336 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002337{
bellard03acab62007-11-11 14:57:14 +00002338 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00002339
bellard6dbad632003-03-16 18:05:05 +00002340 switch (func) {
2341 case 0:
2342 ret = read_ldt(ptr, bytecount);
2343 break;
2344 case 1:
2345 ret = write_ldt(env, ptr, bytecount, 1);
2346 break;
2347 case 0x11:
2348 ret = write_ldt(env, ptr, bytecount, 0);
2349 break;
bellard03acab62007-11-11 14:57:14 +00002350 default:
2351 ret = -TARGET_ENOSYS;
2352 break;
bellard6dbad632003-03-16 18:05:05 +00002353 }
2354 return ret;
2355}
bellard1b6b0292003-03-22 17:31:38 +00002356
bellard2ab83ea2003-06-15 19:56:46 +00002357#endif /* defined(TARGET_I386) */
2358
bellard1b6b0292003-03-22 17:31:38 +00002359/* this stack is the equivalent of the kernel stack associated with a
2360 thread/process */
2361#define NEW_STACK_SIZE 8192
2362
2363static int clone_func(void *arg)
2364{
bellard2ab83ea2003-06-15 19:56:46 +00002365 CPUState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00002366 cpu_loop(env);
2367 /* never exits */
2368 return 0;
2369}
2370
ths0da46a62007-10-20 20:23:07 +00002371/* do_fork() Must return host values and target errnos (unlike most
2372 do_*() functions). */
blueswir1992f48a2007-10-14 16:27:31 +00002373int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp)
bellard1b6b0292003-03-22 17:31:38 +00002374{
2375 int ret;
bellard5cd43932003-03-29 16:54:36 +00002376 TaskState *ts;
bellard1b6b0292003-03-22 17:31:38 +00002377 uint8_t *new_stack;
bellard2ab83ea2003-06-15 19:56:46 +00002378 CPUState *new_env;
ths3b46e622007-09-17 08:09:54 +00002379
bellard1b6b0292003-03-22 17:31:38 +00002380 if (flags & CLONE_VM) {
bellard5cd43932003-03-29 16:54:36 +00002381 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2382 memset(ts, 0, sizeof(TaskState));
2383 new_stack = ts->stack;
2384 ts->used = 1;
2385 /* add in task state list */
2386 ts->next = first_task_state;
2387 first_task_state = ts;
bellard1b6b0292003-03-22 17:31:38 +00002388 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00002389 new_env = cpu_copy(env);
bellard2ab83ea2003-06-15 19:56:46 +00002390#if defined(TARGET_I386)
2391 if (!newsp)
2392 newsp = env->regs[R_ESP];
bellard1b6b0292003-03-22 17:31:38 +00002393 new_env->regs[R_ESP] = newsp;
2394 new_env->regs[R_EAX] = 0;
bellard2ab83ea2003-06-15 19:56:46 +00002395#elif defined(TARGET_ARM)
2396 if (!newsp)
2397 newsp = env->regs[13];
2398 new_env->regs[13] = newsp;
2399 new_env->regs[0] = 0;
bellardebc05482003-09-30 21:08:41 +00002400#elif defined(TARGET_SPARC)
bellard48dc41e2006-06-21 18:15:50 +00002401 if (!newsp)
2402 newsp = env->regwptr[22];
2403 new_env->regwptr[22] = newsp;
2404 new_env->regwptr[0] = 0;
2405 /* XXXXX */
bellard67867302003-11-23 17:05:30 +00002406 printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
pbrooke6e59062006-10-22 00:18:54 +00002407#elif defined(TARGET_M68K)
2408 if (!newsp)
2409 newsp = env->aregs[7];
2410 new_env->aregs[7] = newsp;
2411 new_env->dregs[0] = 0;
2412 /* ??? is this sufficient? */
bellard048f6b42005-11-26 18:47:20 +00002413#elif defined(TARGET_MIPS)
ths388bb212007-05-13 13:58:00 +00002414 if (!newsp)
thsead93602007-09-06 00:18:15 +00002415 newsp = env->gpr[29][env->current_tc];
2416 new_env->gpr[29][env->current_tc] = newsp;
bellard67867302003-11-23 17:05:30 +00002417#elif defined(TARGET_PPC)
2418 if (!newsp)
2419 newsp = env->gpr[1];
2420 new_env->gpr[1] = newsp;
ths5fafdf22007-09-16 21:08:06 +00002421 {
bellard67867302003-11-23 17:05:30 +00002422 int i;
2423 for (i = 7; i < 32; i++)
2424 new_env->gpr[i] = 0;
2425 }
bellardfdf9b3e2006-04-27 21:07:38 +00002426#elif defined(TARGET_SH4)
2427 if (!newsp)
2428 newsp = env->gregs[15];
2429 new_env->gregs[15] = newsp;
2430 /* XXXXX */
j_mayer7a3148a2007-04-05 07:13:51 +00002431#elif defined(TARGET_ALPHA)
2432 if (!newsp)
2433 newsp = env->ir[30];
2434 new_env->ir[30] = newsp;
2435 /* ? */
2436 {
2437 int i;
2438 for (i = 7; i < 30; i++)
2439 new_env->ir[i] = 0;
2440 }
ths48733d12007-10-08 13:36:46 +00002441#elif defined(TARGET_CRIS)
2442 if (!newsp)
2443 newsp = env->regs[14];
2444 new_env->regs[14] = newsp;
bellard2ab83ea2003-06-15 19:56:46 +00002445#else
2446#error unsupported target CPU
2447#endif
bellard5cd43932003-03-29 16:54:36 +00002448 new_env->opaque = ts;
bellard27725c12003-04-29 21:08:18 +00002449#ifdef __ia64__
bellardfd4a43e2006-04-24 20:32:17 +00002450 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00002451#else
2452 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2453#endif
bellard1b6b0292003-03-22 17:31:38 +00002454 } else {
2455 /* if no CLONE_VM, we consider it is a fork */
2456 if ((flags & ~CSIGNAL) != 0)
2457 return -EINVAL;
2458 ret = fork();
2459 }
2460 return ret;
2461}
2462
blueswir1992f48a2007-10-14 16:27:31 +00002463static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00002464{
2465 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00002466 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00002467 struct flock64 fl64;
2468 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00002469 abi_long ret;
pbrook53a59602006-03-25 19:31:22 +00002470
bellard7775e9e2003-05-14 22:46:48 +00002471 switch(cmd) {
2472 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00002473 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2474 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00002475 fl.l_type = tswap16(target_fl->l_type);
2476 fl.l_whence = tswap16(target_fl->l_whence);
2477 fl.l_start = tswapl(target_fl->l_start);
2478 fl.l_len = tswapl(target_fl->l_len);
2479 fl.l_pid = tswapl(target_fl->l_pid);
2480 unlock_user_struct(target_fl, arg, 0);
bellard7775e9e2003-05-14 22:46:48 +00002481 ret = fcntl(fd, cmd, &fl);
2482 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00002483 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
2484 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00002485 target_fl->l_type = tswap16(fl.l_type);
2486 target_fl->l_whence = tswap16(fl.l_whence);
2487 target_fl->l_start = tswapl(fl.l_start);
2488 target_fl->l_len = tswapl(fl.l_len);
2489 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00002490 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00002491 }
2492 break;
ths3b46e622007-09-17 08:09:54 +00002493
bellard7775e9e2003-05-14 22:46:48 +00002494 case TARGET_F_SETLK:
2495 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00002496 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2497 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00002498 fl.l_type = tswap16(target_fl->l_type);
2499 fl.l_whence = tswap16(target_fl->l_whence);
2500 fl.l_start = tswapl(target_fl->l_start);
2501 fl.l_len = tswapl(target_fl->l_len);
2502 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00002503 unlock_user_struct(target_fl, arg, 0);
bellard7775e9e2003-05-14 22:46:48 +00002504 ret = fcntl(fd, cmd, &fl);
2505 break;
ths3b46e622007-09-17 08:09:54 +00002506
bellard7775e9e2003-05-14 22:46:48 +00002507 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00002508 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2509 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00002510 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2511 fl64.l_whence = tswap16(target_fl64->l_whence);
2512 fl64.l_start = tswapl(target_fl64->l_start);
2513 fl64.l_len = tswapl(target_fl64->l_len);
2514 fl64.l_pid = tswap16(target_fl64->l_pid);
2515 unlock_user_struct(target_fl64, arg, 0);
ths43f238d2007-01-05 20:55:49 +00002516 ret = fcntl(fd, cmd >> 1, &fl64);
2517 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00002518 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
2519 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00002520 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2521 target_fl64->l_whence = tswap16(fl64.l_whence);
2522 target_fl64->l_start = tswapl(fl64.l_start);
2523 target_fl64->l_len = tswapl(fl64.l_len);
2524 target_fl64->l_pid = tswapl(fl64.l_pid);
2525 unlock_user_struct(target_fl64, arg, 1);
2526 }
2527 break;
bellard7775e9e2003-05-14 22:46:48 +00002528 case TARGET_F_SETLK64:
2529 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00002530 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2531 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00002532 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2533 fl64.l_whence = tswap16(target_fl64->l_whence);
2534 fl64.l_start = tswapl(target_fl64->l_start);
2535 fl64.l_len = tswapl(target_fl64->l_len);
2536 fl64.l_pid = tswap16(target_fl64->l_pid);
2537 unlock_user_struct(target_fl64, arg, 0);
bellard579a97f2007-11-11 14:26:47 +00002538 ret = fcntl(fd, cmd >> 1, &fl64);
bellard7775e9e2003-05-14 22:46:48 +00002539 break;
2540
bellardffa65c32004-01-04 23:57:22 +00002541 case F_GETFL:
2542 ret = fcntl(fd, cmd, arg);
2543 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2544 break;
2545
2546 case F_SETFL:
2547 ret = fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl));
2548 break;
2549
bellard7775e9e2003-05-14 22:46:48 +00002550 default:
2551 ret = fcntl(fd, cmd, arg);
2552 break;
2553 }
2554 return ret;
2555}
2556
bellard67867302003-11-23 17:05:30 +00002557#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00002558
bellard67867302003-11-23 17:05:30 +00002559static inline int high2lowuid(int uid)
2560{
2561 if (uid > 65535)
2562 return 65534;
2563 else
2564 return uid;
2565}
2566
2567static inline int high2lowgid(int gid)
2568{
2569 if (gid > 65535)
2570 return 65534;
2571 else
2572 return gid;
2573}
2574
2575static inline int low2highuid(int uid)
2576{
2577 if ((int16_t)uid == -1)
2578 return -1;
2579 else
2580 return uid;
2581}
2582
2583static inline int low2highgid(int gid)
2584{
2585 if ((int16_t)gid == -1)
2586 return -1;
2587 else
2588 return gid;
2589}
2590
2591#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00002592
bellard31e31b82003-02-18 22:55:36 +00002593void syscall_init(void)
2594{
bellard2ab83ea2003-06-15 19:56:46 +00002595 IOCTLEntry *ie;
2596 const argtype *arg_type;
2597 int size;
thsb92c47c2007-11-01 00:07:38 +00002598 int i;
bellard2ab83ea2003-06-15 19:56:46 +00002599
ths5fafdf22007-09-16 21:08:06 +00002600#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2601#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00002602#include "syscall_types.h"
2603#undef STRUCT
2604#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00002605
2606 /* we patch the ioctl size if necessary. We rely on the fact that
2607 no ioctl has all the bits at '1' in the size field */
2608 ie = ioctl_entries;
2609 while (ie->target_cmd != 0) {
2610 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2611 TARGET_IOC_SIZEMASK) {
2612 arg_type = ie->arg_type;
2613 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00002614 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002615 ie->target_cmd);
2616 exit(1);
2617 }
2618 arg_type++;
2619 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00002620 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00002621 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2622 (size << TARGET_IOC_SIZESHIFT);
2623 }
thsb92c47c2007-11-01 00:07:38 +00002624
2625 /* Build target_to_host_errno_table[] table from
2626 * host_to_target_errno_table[]. */
2627 for (i=0; i < ERRNO_TABLE_SIZE; i++)
2628 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
2629
bellard2ab83ea2003-06-15 19:56:46 +00002630 /* automatic consistency check if same arch */
2631#if defined(__i386__) && defined(TARGET_I386)
2632 if (ie->target_cmd != ie->host_cmd) {
ths5fafdf22007-09-16 21:08:06 +00002633 fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002634 ie->target_cmd, ie->host_cmd);
2635 }
2636#endif
2637 ie++;
2638 }
bellard31e31b82003-02-18 22:55:36 +00002639}
bellardc573ff62004-01-04 15:51:36 +00002640
blueswir1992f48a2007-10-14 16:27:31 +00002641#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00002642static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2643{
2644#ifdef TARGET_WORDS_BIG_ENDIAN
2645 return ((uint64_t)word0 << 32) | word1;
2646#else
2647 return ((uint64_t)word1 << 32) | word0;
2648#endif
2649}
blueswir1992f48a2007-10-14 16:27:31 +00002650#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00002651static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2652{
2653 return word0;
2654}
blueswir1992f48a2007-10-14 16:27:31 +00002655#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00002656
2657#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00002658static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
2659 abi_long arg2,
2660 abi_long arg3,
2661 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00002662{
2663#ifdef TARGET_ARM
2664 if (((CPUARMState *)cpu_env)->eabi)
2665 {
2666 arg2 = arg3;
2667 arg3 = arg4;
2668 }
2669#endif
2670 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
2671}
2672#endif
2673
2674#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00002675static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
2676 abi_long arg2,
2677 abi_long arg3,
2678 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00002679{
2680#ifdef TARGET_ARM
2681 if (((CPUARMState *)cpu_env)->eabi)
2682 {
2683 arg2 = arg3;
2684 arg3 = arg4;
2685 }
2686#endif
2687 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
2688}
2689#endif
2690
bellard579a97f2007-11-11 14:26:47 +00002691static inline abi_long target_to_host_timespec(struct timespec *host_ts,
2692 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00002693{
2694 struct target_timespec *target_ts;
2695
bellard579a97f2007-11-11 14:26:47 +00002696 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
2697 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002698 host_ts->tv_sec = tswapl(target_ts->tv_sec);
2699 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
2700 unlock_user_struct(target_ts, target_addr, 0);
2701}
2702
bellard579a97f2007-11-11 14:26:47 +00002703static inline abi_long host_to_target_timespec(abi_ulong target_addr,
2704 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00002705{
2706 struct target_timespec *target_ts;
2707
bellard579a97f2007-11-11 14:26:47 +00002708 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
2709 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002710 target_ts->tv_sec = tswapl(host_ts->tv_sec);
2711 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
2712 unlock_user_struct(target_ts, target_addr, 1);
2713}
2714
ths0da46a62007-10-20 20:23:07 +00002715/* do_syscall() should always have a single exit point at the end so
2716 that actions, such as logging of syscall results, can be performed.
2717 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00002718abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
2719 abi_long arg2, abi_long arg3, abi_long arg4,
2720 abi_long arg5, abi_long arg6)
bellard31e31b82003-02-18 22:55:36 +00002721{
blueswir1992f48a2007-10-14 16:27:31 +00002722 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00002723 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00002724 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00002725 void *p;
ths3b46e622007-09-17 08:09:54 +00002726
bellard72f03902003-02-18 23:33:18 +00002727#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00002728 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00002729#endif
thsb92c47c2007-11-01 00:07:38 +00002730 if(do_strace)
2731 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
2732
bellard31e31b82003-02-18 22:55:36 +00002733 switch(num) {
2734 case TARGET_NR_exit:
bellard7d132992003-03-06 23:23:54 +00002735#ifdef HAVE_GPROF
2736 _mcleanup();
2737#endif
bellarde9009672005-04-26 20:42:36 +00002738 gdb_exit(cpu_env, arg1);
bellard1b6b0292003-03-22 17:31:38 +00002739 /* XXX: should free thread stack and CPU env */
bellard31e31b82003-02-18 22:55:36 +00002740 _exit(arg1);
2741 ret = 0; /* avoid warning */
2742 break;
2743 case TARGET_NR_read:
pbrook53a59602006-03-25 19:31:22 +00002744 page_unprotect_range(arg2, arg3);
bellard579a97f2007-11-11 14:26:47 +00002745 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
2746 goto efault;
pbrook53a59602006-03-25 19:31:22 +00002747 ret = get_errno(read(arg1, p, arg3));
2748 unlock_user(p, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00002749 break;
2750 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00002751 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
2752 goto efault;
pbrook53a59602006-03-25 19:31:22 +00002753 ret = get_errno(write(arg1, p, arg3));
2754 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00002755 break;
2756 case TARGET_NR_open:
bellard579a97f2007-11-11 14:26:47 +00002757 if (!(p = lock_user_string(arg1))) {
2758 return -TARGET_EFAULT;
2759 goto fail;
2760 }
pbrook53a59602006-03-25 19:31:22 +00002761 ret = get_errno(open(path(p),
bellardffa65c32004-01-04 23:57:22 +00002762 target_to_host_bitmask(arg2, fcntl_flags_tbl),
2763 arg3));
pbrook53a59602006-03-25 19:31:22 +00002764 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002765 break;
ths82424832007-09-24 09:21:55 +00002766#if defined(TARGET_NR_openat) && defined(__NR_openat)
2767 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00002768 if (!(p = lock_user_string(arg2)))
2769 goto efault;
2770 ret = get_errno(sys_openat(arg1,
2771 path(p),
2772 target_to_host_bitmask(arg3, fcntl_flags_tbl),
2773 arg4));
2774 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00002775 break;
2776#endif
bellard31e31b82003-02-18 22:55:36 +00002777 case TARGET_NR_close:
2778 ret = get_errno(close(arg1));
2779 break;
2780 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00002781 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00002782 break;
2783 case TARGET_NR_fork:
bellard1b6b0292003-03-22 17:31:38 +00002784 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00002785 break;
thse5febef2007-04-01 18:31:35 +00002786#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00002787 case TARGET_NR_waitpid:
2788 {
pbrook53a59602006-03-25 19:31:22 +00002789 int status;
2790 ret = get_errno(waitpid(arg1, &status, arg3));
2791 if (!is_error(ret) && arg2)
2792 tput32(arg2, status);
bellard31e31b82003-02-18 22:55:36 +00002793 }
2794 break;
thse5febef2007-04-01 18:31:35 +00002795#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002796#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002797 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00002798 if (!(p = lock_user_string(arg1)))
2799 goto efault;
pbrook53a59602006-03-25 19:31:22 +00002800 ret = get_errno(creat(p, arg2));
2801 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002802 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002803#endif
bellard31e31b82003-02-18 22:55:36 +00002804 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00002805 {
2806 void * p2;
2807 p = lock_user_string(arg1);
2808 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00002809 if (!p || !p2)
2810 ret = -TARGET_EFAULT;
2811 else
2812 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00002813 unlock_user(p2, arg2, 0);
2814 unlock_user(p, arg1, 0);
2815 }
bellard31e31b82003-02-18 22:55:36 +00002816 break;
ths64f0ce42007-09-24 09:25:06 +00002817#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
2818 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00002819 {
2820 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00002821 if (!arg2 || !arg4)
2822 goto efault;
ths64f0ce42007-09-24 09:25:06 +00002823 p = lock_user_string(arg2);
2824 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00002825 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00002826 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00002827 else
2828 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00002829 unlock_user(p, arg2, 0);
2830 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00002831 }
2832 break;
2833#endif
bellard31e31b82003-02-18 22:55:36 +00002834 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00002835 if (!(p = lock_user_string(arg1)))
2836 goto efault;
pbrook53a59602006-03-25 19:31:22 +00002837 ret = get_errno(unlink(p));
2838 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002839 break;
ths8170f562007-09-24 09:24:11 +00002840#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
2841 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00002842 if (!(p = lock_user_string(arg2)))
2843 goto efault;
2844 ret = get_errno(sys_unlinkat(arg1, p, arg3));
2845 unlock_user(p, arg2, 0);
ths8170f562007-09-24 09:24:11 +00002846#endif
bellard31e31b82003-02-18 22:55:36 +00002847 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00002848 {
2849 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00002850 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00002851 abi_ulong gp;
2852 abi_ulong guest_argp;
2853 abi_ulong guest_envp;
2854 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00002855 char **q;
2856
bellardf7341ff2003-03-30 21:00:25 +00002857 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00002858 guest_argp = arg2;
2859 for (gp = guest_argp; tgetl(gp); gp++)
bellard7854b052003-03-29 17:22:23 +00002860 argc++;
bellardf7341ff2003-03-30 21:00:25 +00002861 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00002862 guest_envp = arg3;
2863 for (gp = guest_envp; tgetl(gp); gp++)
bellard7854b052003-03-29 17:22:23 +00002864 envc++;
2865
bellardf7341ff2003-03-30 21:00:25 +00002866 argp = alloca((argc + 1) * sizeof(void *));
2867 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00002868
pbrook53a59602006-03-25 19:31:22 +00002869 for (gp = guest_argp, q = argp; ;
blueswir1992f48a2007-10-14 16:27:31 +00002870 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002871 addr = tgetl(gp);
2872 if (!addr)
2873 break;
bellard579a97f2007-11-11 14:26:47 +00002874 if (!(*q = lock_user_string(addr))) {
2875 ret = -TARGET_EFAULT;
2876 goto execve_fail;
2877 }
pbrook53a59602006-03-25 19:31:22 +00002878 }
bellardf7341ff2003-03-30 21:00:25 +00002879 *q = NULL;
2880
pbrook53a59602006-03-25 19:31:22 +00002881 for (gp = guest_envp, q = envp; ;
blueswir1992f48a2007-10-14 16:27:31 +00002882 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002883 addr = tgetl(gp);
2884 if (!addr)
2885 break;
bellard579a97f2007-11-11 14:26:47 +00002886 if (!(*q = lock_user_string(addr))) {
2887 ret = -TARGET_EFAULT;
2888 goto execve_fail;
2889 }
pbrook53a59602006-03-25 19:31:22 +00002890 }
bellardf7341ff2003-03-30 21:00:25 +00002891 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00002892
bellard579a97f2007-11-11 14:26:47 +00002893 if (!(p = lock_user_string(arg1))) {
2894 ret = -TARGET_EFAULT;
2895 goto execve_fail;
2896 }
pbrook53a59602006-03-25 19:31:22 +00002897 ret = get_errno(execve(p, argp, envp));
2898 unlock_user(p, arg1, 0);
2899
bellard579a97f2007-11-11 14:26:47 +00002900 execve_fail:
pbrook53a59602006-03-25 19:31:22 +00002901 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00002902 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002903 addr = tgetl(gp);
2904 unlock_user(*q, addr, 0);
2905 }
2906 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00002907 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002908 addr = tgetl(gp);
2909 unlock_user(*q, addr, 0);
2910 }
bellard7854b052003-03-29 17:22:23 +00002911 }
bellard31e31b82003-02-18 22:55:36 +00002912 break;
2913 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00002914 if (!(p = lock_user_string(arg1)))
2915 goto efault;
pbrook53a59602006-03-25 19:31:22 +00002916 ret = get_errno(chdir(p));
2917 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002918 break;
bellarda315a142005-01-30 22:59:18 +00002919#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00002920 case TARGET_NR_time:
2921 {
pbrook53a59602006-03-25 19:31:22 +00002922 time_t host_time;
2923 ret = get_errno(time(&host_time));
2924 if (!is_error(ret) && arg1)
2925 tputl(arg1, host_time);
bellard31e31b82003-02-18 22:55:36 +00002926 }
2927 break;
bellarda315a142005-01-30 22:59:18 +00002928#endif
bellard31e31b82003-02-18 22:55:36 +00002929 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00002930 if (!(p = lock_user_string(arg1)))
2931 goto efault;
pbrook53a59602006-03-25 19:31:22 +00002932 ret = get_errno(mknod(p, arg2, arg3));
2933 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002934 break;
ths75ac37a2007-09-24 09:23:05 +00002935#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
2936 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00002937 if (!(p = lock_user_string(arg2)))
2938 goto efault;
2939 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
2940 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00002941 break;
2942#endif
bellard31e31b82003-02-18 22:55:36 +00002943 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00002944 if (!(p = lock_user_string(arg1)))
2945 goto efault;
pbrook53a59602006-03-25 19:31:22 +00002946 ret = get_errno(chmod(p, arg2));
2947 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002948 break;
bellardebc05482003-09-30 21:08:41 +00002949#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00002950 case TARGET_NR_break:
2951 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002952#endif
2953#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00002954 case TARGET_NR_oldstat:
2955 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002956#endif
bellard31e31b82003-02-18 22:55:36 +00002957 case TARGET_NR_lseek:
2958 ret = get_errno(lseek(arg1, arg2, arg3));
2959 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002960#ifdef TARGET_NR_getxpid
2961 case TARGET_NR_getxpid:
2962#else
bellard31e31b82003-02-18 22:55:36 +00002963 case TARGET_NR_getpid:
j_mayer7a3148a2007-04-05 07:13:51 +00002964#endif
bellard31e31b82003-02-18 22:55:36 +00002965 ret = get_errno(getpid());
2966 break;
2967 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00002968 {
2969 /* need to look at the data field */
2970 void *p2, *p3;
2971 p = lock_user_string(arg1);
2972 p2 = lock_user_string(arg2);
2973 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00002974 if (!p || !p2 || !p3)
2975 ret = -TARGET_EFAULT;
2976 else
2977 /* FIXME - arg5 should be locked, but it isn't clear how to
2978 * do that since it's not guaranteed to be a NULL-terminated
2979 * string.
2980 */
2981 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
2982 unlock_user(p, arg1, 0);
2983 unlock_user(p2, arg2, 0);
2984 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00002985 break;
2986 }
thse5febef2007-04-01 18:31:35 +00002987#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00002988 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00002989 if (!(p = lock_user_string(arg1)))
2990 goto efault;
pbrook53a59602006-03-25 19:31:22 +00002991 ret = get_errno(umount(p));
2992 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002993 break;
thse5febef2007-04-01 18:31:35 +00002994#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002995#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002996 case TARGET_NR_stime:
2997 {
pbrook53a59602006-03-25 19:31:22 +00002998 time_t host_time;
2999 host_time = tgetl(arg1);
3000 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00003001 }
3002 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003003#endif
bellard31e31b82003-02-18 22:55:36 +00003004 case TARGET_NR_ptrace:
3005 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00003006#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003007 case TARGET_NR_alarm:
3008 ret = alarm(arg1);
3009 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003010#endif
bellardebc05482003-09-30 21:08:41 +00003011#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00003012 case TARGET_NR_oldfstat:
3013 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003014#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003015#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003016 case TARGET_NR_pause:
3017 ret = get_errno(pause());
3018 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003019#endif
thse5febef2007-04-01 18:31:35 +00003020#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00003021 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00003022 {
pbrook53a59602006-03-25 19:31:22 +00003023 struct utimbuf tbuf, *host_tbuf;
3024 struct target_utimbuf *target_tbuf;
3025 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00003026 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
3027 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003028 tbuf.actime = tswapl(target_tbuf->actime);
3029 tbuf.modtime = tswapl(target_tbuf->modtime);
3030 unlock_user_struct(target_tbuf, arg2, 0);
3031 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00003032 } else {
pbrook53a59602006-03-25 19:31:22 +00003033 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00003034 }
bellard579a97f2007-11-11 14:26:47 +00003035 if (!(p = lock_user_string(arg1)))
3036 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003037 ret = get_errno(utime(p, host_tbuf));
3038 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00003039 }
3040 break;
thse5febef2007-04-01 18:31:35 +00003041#endif
bellard978a66f2004-12-06 22:58:05 +00003042 case TARGET_NR_utimes:
3043 {
bellard978a66f2004-12-06 22:58:05 +00003044 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00003045 if (arg2) {
3046 target_to_host_timeval(&tv[0], arg2);
3047 target_to_host_timeval(&tv[1],
3048 arg2 + sizeof (struct target_timeval));
bellard978a66f2004-12-06 22:58:05 +00003049 tvp = tv;
3050 } else {
3051 tvp = NULL;
3052 }
bellard579a97f2007-11-11 14:26:47 +00003053 if (!(p = lock_user_string(arg1)))
3054 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003055 ret = get_errno(utimes(p, tvp));
3056 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00003057 }
3058 break;
bellardebc05482003-09-30 21:08:41 +00003059#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00003060 case TARGET_NR_stty:
3061 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003062#endif
3063#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00003064 case TARGET_NR_gtty:
3065 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003066#endif
bellard31e31b82003-02-18 22:55:36 +00003067 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00003068 if (!(p = lock_user_string(arg1)))
3069 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003070 ret = get_errno(access(p, arg2));
3071 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003072 break;
ths92a34c12007-09-24 09:27:49 +00003073#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
3074 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00003075 if (!(p = lock_user_string(arg2)))
3076 goto efault;
3077 ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
3078 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00003079 break;
3080#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003081#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003082 case TARGET_NR_nice:
3083 ret = get_errno(nice(arg1));
3084 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003085#endif
bellardebc05482003-09-30 21:08:41 +00003086#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00003087 case TARGET_NR_ftime:
3088 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003089#endif
bellard31e31b82003-02-18 22:55:36 +00003090 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00003091 sync();
3092 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00003093 break;
3094 case TARGET_NR_kill:
3095 ret = get_errno(kill(arg1, arg2));
3096 break;
3097 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00003098 {
3099 void *p2;
3100 p = lock_user_string(arg1);
3101 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00003102 if (!p || !p2)
3103 ret = -TARGET_EFAULT;
3104 else
3105 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00003106 unlock_user(p2, arg2, 0);
3107 unlock_user(p, arg1, 0);
3108 }
bellard31e31b82003-02-18 22:55:36 +00003109 break;
ths722183f2007-09-24 09:24:37 +00003110#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
3111 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00003112 {
bellard579a97f2007-11-11 14:26:47 +00003113 void *p2;
ths722183f2007-09-24 09:24:37 +00003114 p = lock_user_string(arg2);
3115 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00003116 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00003117 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00003118 else
3119 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00003120 unlock_user(p2, arg4, 0);
3121 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00003122 }
3123 break;
3124#endif
bellard31e31b82003-02-18 22:55:36 +00003125 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00003126 if (!(p = lock_user_string(arg1)))
3127 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003128 ret = get_errno(mkdir(p, arg2));
3129 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003130 break;
ths4472ad02007-09-24 09:22:32 +00003131#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
3132 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00003133 if (!(p = lock_user_string(arg2)))
3134 goto efault;
3135 ret = get_errno(sys_mkdirat(arg1, p, arg3));
3136 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00003137 break;
3138#endif
bellard31e31b82003-02-18 22:55:36 +00003139 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00003140 if (!(p = lock_user_string(arg1)))
3141 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003142 ret = get_errno(rmdir(p));
3143 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003144 break;
3145 case TARGET_NR_dup:
3146 ret = get_errno(dup(arg1));
3147 break;
3148 case TARGET_NR_pipe:
3149 {
pbrook53a59602006-03-25 19:31:22 +00003150 int host_pipe[2];
3151 ret = get_errno(pipe(host_pipe));
bellard31e31b82003-02-18 22:55:36 +00003152 if (!is_error(ret)) {
thsc12ab052007-06-01 11:50:36 +00003153#if defined(TARGET_MIPS)
thsead93602007-09-06 00:18:15 +00003154 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
3155 env->gpr[3][env->current_tc] = host_pipe[1];
thsc12ab052007-06-01 11:50:36 +00003156 ret = host_pipe[0];
3157#else
pbrook53a59602006-03-25 19:31:22 +00003158 tput32(arg1, host_pipe[0]);
3159 tput32(arg1 + 4, host_pipe[1]);
thsc12ab052007-06-01 11:50:36 +00003160#endif
bellard31e31b82003-02-18 22:55:36 +00003161 }
3162 }
3163 break;
3164 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00003165 {
pbrook53a59602006-03-25 19:31:22 +00003166 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00003167 struct tms tms;
3168 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00003169 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00003170 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
3171 if (!tmsp)
3172 goto efault;
bellardc596ed12003-07-13 17:32:31 +00003173 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3174 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3175 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3176 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00003177 }
bellardc596ed12003-07-13 17:32:31 +00003178 if (!is_error(ret))
3179 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00003180 }
3181 break;
bellardebc05482003-09-30 21:08:41 +00003182#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00003183 case TARGET_NR_prof:
3184 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003185#endif
thse5febef2007-04-01 18:31:35 +00003186#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00003187 case TARGET_NR_signal:
3188 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003189#endif
bellard31e31b82003-02-18 22:55:36 +00003190 case TARGET_NR_acct:
bellard579a97f2007-11-11 14:26:47 +00003191 if (!(p = lock_user_string(arg1)))
3192 goto efault;
pbrook24836682006-04-16 14:14:53 +00003193 ret = get_errno(acct(path(p)));
3194 unlock_user(p, arg1, 0);
3195 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003196#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003197 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00003198 if (!(p = lock_user_string(arg1)))
3199 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003200 ret = get_errno(umount2(p, arg2));
3201 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003202 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003203#endif
bellardebc05482003-09-30 21:08:41 +00003204#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00003205 case TARGET_NR_lock:
3206 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003207#endif
bellard31e31b82003-02-18 22:55:36 +00003208 case TARGET_NR_ioctl:
3209 ret = do_ioctl(arg1, arg2, arg3);
3210 break;
3211 case TARGET_NR_fcntl:
bellard7775e9e2003-05-14 22:46:48 +00003212 ret = get_errno(do_fcntl(arg1, arg2, arg3));
bellard31e31b82003-02-18 22:55:36 +00003213 break;
bellardebc05482003-09-30 21:08:41 +00003214#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00003215 case TARGET_NR_mpx:
3216 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003217#endif
bellard31e31b82003-02-18 22:55:36 +00003218 case TARGET_NR_setpgid:
3219 ret = get_errno(setpgid(arg1, arg2));
3220 break;
bellardebc05482003-09-30 21:08:41 +00003221#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00003222 case TARGET_NR_ulimit:
3223 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003224#endif
3225#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00003226 case TARGET_NR_oldolduname:
3227 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003228#endif
bellard31e31b82003-02-18 22:55:36 +00003229 case TARGET_NR_umask:
3230 ret = get_errno(umask(arg1));
3231 break;
3232 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00003233 if (!(p = lock_user_string(arg1)))
3234 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003235 ret = get_errno(chroot(p));
3236 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003237 break;
3238 case TARGET_NR_ustat:
3239 goto unimplemented;
3240 case TARGET_NR_dup2:
3241 ret = get_errno(dup2(arg1, arg2));
3242 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003243#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003244 case TARGET_NR_getppid:
3245 ret = get_errno(getppid());
3246 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003247#endif
bellard31e31b82003-02-18 22:55:36 +00003248 case TARGET_NR_getpgrp:
3249 ret = get_errno(getpgrp());
3250 break;
3251 case TARGET_NR_setsid:
3252 ret = get_errno(setsid());
3253 break;
thse5febef2007-04-01 18:31:35 +00003254#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00003255 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00003256 {
ths388bb212007-05-13 13:58:00 +00003257#if !defined(TARGET_MIPS)
pbrook53a59602006-03-25 19:31:22 +00003258 struct target_old_sigaction *old_act;
bellard66fb9762003-03-23 01:06:05 +00003259 struct target_sigaction act, oact, *pact;
pbrook53a59602006-03-25 19:31:22 +00003260 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00003261 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3262 goto efault;
bellard66fb9762003-03-23 01:06:05 +00003263 act._sa_handler = old_act->_sa_handler;
3264 target_siginitset(&act.sa_mask, old_act->sa_mask);
3265 act.sa_flags = old_act->sa_flags;
3266 act.sa_restorer = old_act->sa_restorer;
pbrook53a59602006-03-25 19:31:22 +00003267 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003268 pact = &act;
3269 } else {
3270 pact = NULL;
3271 }
3272 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00003273 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003274 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3275 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003276 old_act->_sa_handler = oact._sa_handler;
3277 old_act->sa_mask = oact.sa_mask.sig[0];
3278 old_act->sa_flags = oact.sa_flags;
3279 old_act->sa_restorer = oact.sa_restorer;
3280 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00003281 }
ths388bb212007-05-13 13:58:00 +00003282#else
bellard106ec872006-06-27 21:08:10 +00003283 struct target_sigaction act, oact, *pact, *old_act;
3284
3285 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00003286 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3287 goto efault;
bellard106ec872006-06-27 21:08:10 +00003288 act._sa_handler = old_act->_sa_handler;
3289 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3290 act.sa_flags = old_act->sa_flags;
3291 unlock_user_struct(old_act, arg2, 0);
3292 pact = &act;
3293 } else {
3294 pact = NULL;
3295 }
3296
3297 ret = get_errno(do_sigaction(arg1, pact, &oact));
3298
3299 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003300 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3301 goto efault;
bellard106ec872006-06-27 21:08:10 +00003302 old_act->_sa_handler = oact._sa_handler;
3303 old_act->sa_flags = oact.sa_flags;
3304 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3305 old_act->sa_mask.sig[1] = 0;
3306 old_act->sa_mask.sig[2] = 0;
3307 old_act->sa_mask.sig[3] = 0;
3308 unlock_user_struct(old_act, arg3, 1);
3309 }
ths388bb212007-05-13 13:58:00 +00003310#endif
bellard31e31b82003-02-18 22:55:36 +00003311 }
3312 break;
thse5febef2007-04-01 18:31:35 +00003313#endif
bellard66fb9762003-03-23 01:06:05 +00003314 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00003315 {
3316 struct target_sigaction *act;
3317 struct target_sigaction *oact;
3318
bellard579a97f2007-11-11 14:26:47 +00003319 if (arg2) {
3320 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
3321 goto efault;
3322 } else
pbrook53a59602006-03-25 19:31:22 +00003323 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00003324 if (arg3) {
3325 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
3326 ret = -TARGET_EFAULT;
3327 goto rt_sigaction_fail;
3328 }
3329 } else
pbrook53a59602006-03-25 19:31:22 +00003330 oact = NULL;
3331 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00003332 rt_sigaction_fail:
3333 if (act)
pbrook53a59602006-03-25 19:31:22 +00003334 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00003335 if (oact)
pbrook53a59602006-03-25 19:31:22 +00003336 unlock_user_struct(oact, arg3, 1);
3337 }
bellard66fb9762003-03-23 01:06:05 +00003338 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003339#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003340 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00003341 {
3342 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00003343 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00003344 sigprocmask(0, NULL, &cur_set);
3345 host_to_target_old_sigset(&target_set, &cur_set);
3346 ret = target_set;
3347 }
3348 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003349#endif
3350#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003351 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00003352 {
3353 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00003354 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00003355 sigprocmask(0, NULL, &cur_set);
3356 target_to_host_old_sigset(&set, &target_set);
3357 sigorset(&set, &set, &cur_set);
3358 sigprocmask(SIG_SETMASK, &set, &oset);
3359 host_to_target_old_sigset(&target_set, &oset);
3360 ret = target_set;
3361 }
3362 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003363#endif
thse5febef2007-04-01 18:31:35 +00003364#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00003365 case TARGET_NR_sigprocmask:
3366 {
3367 int how = arg1;
3368 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003369
pbrook53a59602006-03-25 19:31:22 +00003370 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003371 switch(how) {
3372 case TARGET_SIG_BLOCK:
3373 how = SIG_BLOCK;
3374 break;
3375 case TARGET_SIG_UNBLOCK:
3376 how = SIG_UNBLOCK;
3377 break;
3378 case TARGET_SIG_SETMASK:
3379 how = SIG_SETMASK;
3380 break;
3381 default:
ths0da46a62007-10-20 20:23:07 +00003382 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00003383 goto fail;
3384 }
bellard579a97f2007-11-11 14:26:47 +00003385 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3386 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003387 target_to_host_old_sigset(&set, p);
3388 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003389 set_ptr = &set;
3390 } else {
3391 how = 0;
3392 set_ptr = NULL;
3393 }
3394 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003395 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003396 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3397 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003398 host_to_target_old_sigset(p, &oldset);
3399 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003400 }
3401 }
3402 break;
thse5febef2007-04-01 18:31:35 +00003403#endif
bellard66fb9762003-03-23 01:06:05 +00003404 case TARGET_NR_rt_sigprocmask:
3405 {
3406 int how = arg1;
3407 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003408
pbrook53a59602006-03-25 19:31:22 +00003409 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003410 switch(how) {
3411 case TARGET_SIG_BLOCK:
3412 how = SIG_BLOCK;
3413 break;
3414 case TARGET_SIG_UNBLOCK:
3415 how = SIG_UNBLOCK;
3416 break;
3417 case TARGET_SIG_SETMASK:
3418 how = SIG_SETMASK;
3419 break;
3420 default:
ths0da46a62007-10-20 20:23:07 +00003421 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00003422 goto fail;
3423 }
bellard579a97f2007-11-11 14:26:47 +00003424 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3425 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003426 target_to_host_sigset(&set, p);
3427 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003428 set_ptr = &set;
3429 } else {
3430 how = 0;
3431 set_ptr = NULL;
3432 }
3433 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003434 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003435 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3436 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003437 host_to_target_sigset(p, &oldset);
3438 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003439 }
3440 }
3441 break;
thse5febef2007-04-01 18:31:35 +00003442#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00003443 case TARGET_NR_sigpending:
3444 {
3445 sigset_t set;
3446 ret = get_errno(sigpending(&set));
3447 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003448 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3449 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003450 host_to_target_old_sigset(p, &set);
3451 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003452 }
3453 }
3454 break;
thse5febef2007-04-01 18:31:35 +00003455#endif
bellard66fb9762003-03-23 01:06:05 +00003456 case TARGET_NR_rt_sigpending:
3457 {
3458 sigset_t set;
3459 ret = get_errno(sigpending(&set));
3460 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003461 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3462 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003463 host_to_target_sigset(p, &set);
3464 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003465 }
3466 }
3467 break;
thse5febef2007-04-01 18:31:35 +00003468#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00003469 case TARGET_NR_sigsuspend:
3470 {
3471 sigset_t set;
bellard579a97f2007-11-11 14:26:47 +00003472 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3473 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003474 target_to_host_old_sigset(&set, p);
3475 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003476 ret = get_errno(sigsuspend(&set));
3477 }
3478 break;
thse5febef2007-04-01 18:31:35 +00003479#endif
bellard66fb9762003-03-23 01:06:05 +00003480 case TARGET_NR_rt_sigsuspend:
3481 {
3482 sigset_t set;
bellard579a97f2007-11-11 14:26:47 +00003483 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3484 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003485 target_to_host_sigset(&set, p);
3486 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003487 ret = get_errno(sigsuspend(&set));
3488 }
3489 break;
3490 case TARGET_NR_rt_sigtimedwait:
3491 {
bellard66fb9762003-03-23 01:06:05 +00003492 sigset_t set;
3493 struct timespec uts, *puts;
3494 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00003495
bellard579a97f2007-11-11 14:26:47 +00003496 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3497 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003498 target_to_host_sigset(&set, p);
3499 unlock_user(p, arg1, 0);
3500 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00003501 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00003502 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00003503 } else {
3504 puts = NULL;
3505 }
3506 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00003507 if (!is_error(ret) && arg2) {
bellard579a97f2007-11-11 14:26:47 +00003508 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_sigset_t), 0)))
3509 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003510 host_to_target_siginfo(p, &uinfo);
3511 unlock_user(p, arg2, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003512 }
3513 }
3514 break;
3515 case TARGET_NR_rt_sigqueueinfo:
3516 {
3517 siginfo_t uinfo;
bellard579a97f2007-11-11 14:26:47 +00003518 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
3519 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003520 target_to_host_siginfo(&uinfo, p);
3521 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003522 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3523 }
3524 break;
thse5febef2007-04-01 18:31:35 +00003525#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00003526 case TARGET_NR_sigreturn:
3527 /* NOTE: ret is eax, so not transcoding must be done */
3528 ret = do_sigreturn(cpu_env);
3529 break;
thse5febef2007-04-01 18:31:35 +00003530#endif
bellard66fb9762003-03-23 01:06:05 +00003531 case TARGET_NR_rt_sigreturn:
3532 /* NOTE: ret is eax, so not transcoding must be done */
3533 ret = do_rt_sigreturn(cpu_env);
3534 break;
bellard31e31b82003-02-18 22:55:36 +00003535 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00003536 if (!(p = lock_user_string(arg1)))
3537 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003538 ret = get_errno(sethostname(p, arg2));
3539 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003540 break;
3541 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00003542 {
3543 /* XXX: convert resource ? */
3544 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003545 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003546 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00003547 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
3548 goto efault;
bellard9de5e442003-03-23 16:49:39 +00003549 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3550 rlim.rlim_max = tswapl(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00003551 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00003552 ret = get_errno(setrlimit(resource, &rlim));
3553 }
3554 break;
bellard31e31b82003-02-18 22:55:36 +00003555 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00003556 {
3557 /* XXX: convert resource ? */
3558 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003559 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003560 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00003561
bellard9de5e442003-03-23 16:49:39 +00003562 ret = get_errno(getrlimit(resource, &rlim));
3563 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003564 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
3565 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003566 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3567 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3568 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00003569 }
3570 }
3571 break;
bellard31e31b82003-02-18 22:55:36 +00003572 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00003573 {
3574 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00003575 ret = get_errno(getrusage(arg1, &rusage));
3576 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003577 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00003578 }
3579 }
3580 break;
bellard31e31b82003-02-18 22:55:36 +00003581 case TARGET_NR_gettimeofday:
3582 {
bellard31e31b82003-02-18 22:55:36 +00003583 struct timeval tv;
3584 ret = get_errno(gettimeofday(&tv, NULL));
3585 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003586 host_to_target_timeval(arg1, &tv);
bellard31e31b82003-02-18 22:55:36 +00003587 }
3588 }
3589 break;
3590 case TARGET_NR_settimeofday:
3591 {
bellard31e31b82003-02-18 22:55:36 +00003592 struct timeval tv;
pbrook53a59602006-03-25 19:31:22 +00003593 target_to_host_timeval(&tv, arg1);
bellard31e31b82003-02-18 22:55:36 +00003594 ret = get_errno(settimeofday(&tv, NULL));
3595 }
3596 break;
bellard048f6b42005-11-26 18:47:20 +00003597#ifdef TARGET_NR_select
bellard31e31b82003-02-18 22:55:36 +00003598 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00003599 {
pbrook53a59602006-03-25 19:31:22 +00003600 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00003601 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00003602 long nsel;
3603
bellard579a97f2007-11-11 14:26:47 +00003604 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
3605 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003606 nsel = tswapl(sel->n);
3607 inp = tswapl(sel->inp);
3608 outp = tswapl(sel->outp);
3609 exp = tswapl(sel->exp);
3610 tvp = tswapl(sel->tvp);
3611 unlock_user_struct(sel, arg1, 0);
3612 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00003613 }
3614 break;
bellard048f6b42005-11-26 18:47:20 +00003615#endif
bellard31e31b82003-02-18 22:55:36 +00003616 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00003617 {
3618 void *p2;
3619 p = lock_user_string(arg1);
3620 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00003621 if (!p || !p2)
3622 ret = -TARGET_EFAULT;
3623 else
3624 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00003625 unlock_user(p2, arg2, 0);
3626 unlock_user(p, arg1, 0);
3627 }
bellard31e31b82003-02-18 22:55:36 +00003628 break;
thsf0b62432007-09-24 09:25:40 +00003629#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
3630 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00003631 {
bellard579a97f2007-11-11 14:26:47 +00003632 void *p2;
thsf0b62432007-09-24 09:25:40 +00003633 p = lock_user_string(arg1);
3634 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00003635 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00003636 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00003637 else
3638 ret = get_errno(sys_symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00003639 unlock_user(p2, arg3, 0);
3640 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00003641 }
3642 break;
3643#endif
bellardebc05482003-09-30 21:08:41 +00003644#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00003645 case TARGET_NR_oldlstat:
3646 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003647#endif
bellard31e31b82003-02-18 22:55:36 +00003648 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00003649 {
3650 void *p2;
3651 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00003652 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
3653 if (!p || !p2)
3654 ret = -TARGET_EFAULT;
3655 else
3656 ret = get_errno(readlink(path(p), p2, arg3));
pbrook53a59602006-03-25 19:31:22 +00003657 unlock_user(p2, arg2, ret);
3658 unlock_user(p, arg1, 0);
3659 }
bellard31e31b82003-02-18 22:55:36 +00003660 break;
ths5e0ccb12007-09-24 09:26:10 +00003661#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
3662 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00003663 {
bellard579a97f2007-11-11 14:26:47 +00003664 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00003665 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00003666 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
3667 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00003668 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00003669 else
3670 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
bellard579a97f2007-11-11 14:26:47 +00003671 unlock_user(p2, arg3, ret);
3672 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00003673 }
3674 break;
3675#endif
thse5febef2007-04-01 18:31:35 +00003676#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00003677 case TARGET_NR_uselib:
3678 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003679#endif
3680#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00003681 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00003682 if (!(p = lock_user_string(arg1)))
3683 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003684 ret = get_errno(swapon(p, arg2));
3685 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003686 break;
thse5febef2007-04-01 18:31:35 +00003687#endif
bellard31e31b82003-02-18 22:55:36 +00003688 case TARGET_NR_reboot:
3689 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003690#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00003691 case TARGET_NR_readdir:
3692 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003693#endif
3694#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00003695 case TARGET_NR_mmap:
ths48733d12007-10-08 13:36:46 +00003696#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
bellard31e31b82003-02-18 22:55:36 +00003697 {
blueswir1992f48a2007-10-14 16:27:31 +00003698 abi_ulong *v;
3699 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00003700 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
3701 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003702 v1 = tswapl(v[0]);
3703 v2 = tswapl(v[1]);
3704 v3 = tswapl(v[2]);
3705 v4 = tswapl(v[3]);
3706 v5 = tswapl(v[4]);
3707 v6 = tswapl(v[5]);
3708 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00003709 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00003710 target_to_host_bitmask(v4, mmap_flags_tbl),
3711 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00003712 }
bellard31e31b82003-02-18 22:55:36 +00003713#else
ths5fafdf22007-09-16 21:08:06 +00003714 ret = get_errno(target_mmap(arg1, arg2, arg3,
3715 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00003716 arg5,
3717 arg6));
bellard31e31b82003-02-18 22:55:36 +00003718#endif
bellard6fb883e2003-07-09 17:12:39 +00003719 break;
thse5febef2007-04-01 18:31:35 +00003720#endif
bellarda315a142005-01-30 22:59:18 +00003721#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00003722 case TARGET_NR_mmap2:
ths0d3267a2006-12-11 22:04:25 +00003723#if defined(TARGET_SPARC) || defined(TARGET_MIPS)
bellardc573ff62004-01-04 15:51:36 +00003724#define MMAP_SHIFT 12
3725#else
3726#define MMAP_SHIFT TARGET_PAGE_BITS
3727#endif
ths5fafdf22007-09-16 21:08:06 +00003728 ret = get_errno(target_mmap(arg1, arg2, arg3,
3729 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00003730 arg5,
bellardc573ff62004-01-04 15:51:36 +00003731 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00003732 break;
bellarda315a142005-01-30 22:59:18 +00003733#endif
bellard31e31b82003-02-18 22:55:36 +00003734 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00003735 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00003736 break;
bellard9de5e442003-03-23 16:49:39 +00003737 case TARGET_NR_mprotect:
bellard54936002003-05-13 00:25:15 +00003738 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00003739 break;
thse5febef2007-04-01 18:31:35 +00003740#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00003741 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00003742 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00003743 break;
thse5febef2007-04-01 18:31:35 +00003744#endif
pbrook53a59602006-03-25 19:31:22 +00003745 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00003746#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00003747 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00003748 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00003749 break;
thse5febef2007-04-01 18:31:35 +00003750#endif
3751#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00003752 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00003753 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00003754 break;
thse5febef2007-04-01 18:31:35 +00003755#endif
3756#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00003757 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00003758 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00003759 break;
thse5febef2007-04-01 18:31:35 +00003760#endif
3761#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00003762 case TARGET_NR_mlockall:
3763 ret = get_errno(mlockall(arg1));
3764 break;
thse5febef2007-04-01 18:31:35 +00003765#endif
3766#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00003767 case TARGET_NR_munlockall:
3768 ret = get_errno(munlockall());
3769 break;
thse5febef2007-04-01 18:31:35 +00003770#endif
bellard31e31b82003-02-18 22:55:36 +00003771 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00003772 if (!(p = lock_user_string(arg1)))
3773 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003774 ret = get_errno(truncate(p, arg2));
3775 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003776 break;
3777 case TARGET_NR_ftruncate:
3778 ret = get_errno(ftruncate(arg1, arg2));
3779 break;
3780 case TARGET_NR_fchmod:
3781 ret = get_errno(fchmod(arg1, arg2));
3782 break;
ths814d7972007-09-24 09:26:51 +00003783#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
3784 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00003785 if (!(p = lock_user_string(arg2)))
3786 goto efault;
3787 ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
3788 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00003789 break;
3790#endif
bellard31e31b82003-02-18 22:55:36 +00003791 case TARGET_NR_getpriority:
thsc6cda172007-10-09 03:42:34 +00003792 /* libc does special remapping of the return value of
3793 * sys_getpriority() so it's just easiest to call
3794 * sys_getpriority() directly rather than through libc. */
3795 ret = sys_getpriority(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00003796 break;
3797 case TARGET_NR_setpriority:
3798 ret = get_errno(setpriority(arg1, arg2, arg3));
3799 break;
bellardebc05482003-09-30 21:08:41 +00003800#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00003801 case TARGET_NR_profil:
3802 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003803#endif
bellard31e31b82003-02-18 22:55:36 +00003804 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00003805 if (!(p = lock_user_string(arg1)))
3806 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003807 ret = get_errno(statfs(path(p), &stfs));
3808 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003809 convert_statfs:
3810 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003811 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00003812
bellard579a97f2007-11-11 14:26:47 +00003813 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
3814 goto efault;
3815 __put_user(stfs.f_type, &target_stfs->f_type);
3816 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
3817 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
3818 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
3819 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
3820 __put_user(stfs.f_files, &target_stfs->f_files);
3821 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
3822 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3823 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
3824 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00003825 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00003826 }
3827 break;
3828 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00003829 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00003830 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00003831#ifdef TARGET_NR_statfs64
3832 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00003833 if (!(p = lock_user_string(arg1)))
3834 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003835 ret = get_errno(statfs(path(p), &stfs));
3836 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00003837 convert_statfs64:
3838 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003839 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00003840
bellard579a97f2007-11-11 14:26:47 +00003841 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
3842 goto efault;
3843 __put_user(stfs.f_type, &target_stfs->f_type);
3844 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
3845 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
3846 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
3847 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
3848 __put_user(stfs.f_files, &target_stfs->f_files);
3849 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
3850 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3851 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
3852 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
3853 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00003854 }
3855 break;
3856 case TARGET_NR_fstatfs64:
3857 ret = get_errno(fstatfs(arg1, &stfs));
3858 goto convert_statfs64;
3859#endif
bellardebc05482003-09-30 21:08:41 +00003860#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00003861 case TARGET_NR_ioperm:
3862 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003863#endif
thse5febef2007-04-01 18:31:35 +00003864#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00003865 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00003866 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00003867 break;
thse5febef2007-04-01 18:31:35 +00003868#endif
bellard3532fa72006-06-24 15:06:03 +00003869#ifdef TARGET_NR_accept
3870 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00003871 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003872 break;
3873#endif
3874#ifdef TARGET_NR_bind
3875 case TARGET_NR_bind:
3876 ret = do_bind(arg1, arg2, arg3);
3877 break;
3878#endif
3879#ifdef TARGET_NR_connect
3880 case TARGET_NR_connect:
3881 ret = do_connect(arg1, arg2, arg3);
3882 break;
3883#endif
3884#ifdef TARGET_NR_getpeername
3885 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00003886 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003887 break;
3888#endif
3889#ifdef TARGET_NR_getsockname
3890 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00003891 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003892 break;
3893#endif
3894#ifdef TARGET_NR_getsockopt
3895 case TARGET_NR_getsockopt:
3896 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
3897 break;
3898#endif
3899#ifdef TARGET_NR_listen
3900 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00003901 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00003902 break;
3903#endif
3904#ifdef TARGET_NR_recv
3905 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00003906 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00003907 break;
3908#endif
3909#ifdef TARGET_NR_recvfrom
3910 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00003911 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00003912 break;
3913#endif
3914#ifdef TARGET_NR_recvmsg
3915 case TARGET_NR_recvmsg:
3916 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
3917 break;
3918#endif
3919#ifdef TARGET_NR_send
3920 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00003921 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00003922 break;
3923#endif
3924#ifdef TARGET_NR_sendmsg
3925 case TARGET_NR_sendmsg:
3926 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
3927 break;
3928#endif
3929#ifdef TARGET_NR_sendto
3930 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00003931 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00003932 break;
3933#endif
3934#ifdef TARGET_NR_shutdown
3935 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00003936 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00003937 break;
3938#endif
3939#ifdef TARGET_NR_socket
3940 case TARGET_NR_socket:
3941 ret = do_socket(arg1, arg2, arg3);
3942 break;
3943#endif
3944#ifdef TARGET_NR_socketpair
3945 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00003946 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00003947 break;
3948#endif
3949#ifdef TARGET_NR_setsockopt
3950 case TARGET_NR_setsockopt:
3951 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
3952 break;
3953#endif
ths7494b0f2007-02-11 18:26:53 +00003954
bellard31e31b82003-02-18 22:55:36 +00003955 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00003956 if (!(p = lock_user_string(arg2)))
3957 goto efault;
thse5574482007-02-11 20:03:13 +00003958 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
3959 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00003960 break;
3961
bellard31e31b82003-02-18 22:55:36 +00003962 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00003963 {
bellard66fb9762003-03-23 01:06:05 +00003964 struct itimerval value, ovalue, *pvalue;
3965
pbrook53a59602006-03-25 19:31:22 +00003966 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003967 pvalue = &value;
ths5fafdf22007-09-16 21:08:06 +00003968 target_to_host_timeval(&pvalue->it_interval,
pbrook53a59602006-03-25 19:31:22 +00003969 arg2);
ths5fafdf22007-09-16 21:08:06 +00003970 target_to_host_timeval(&pvalue->it_value,
pbrook53a59602006-03-25 19:31:22 +00003971 arg2 + sizeof(struct target_timeval));
bellard66fb9762003-03-23 01:06:05 +00003972 } else {
3973 pvalue = NULL;
3974 }
3975 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00003976 if (!is_error(ret) && arg3) {
3977 host_to_target_timeval(arg3,
bellard66fb9762003-03-23 01:06:05 +00003978 &ovalue.it_interval);
pbrook53a59602006-03-25 19:31:22 +00003979 host_to_target_timeval(arg3 + sizeof(struct target_timeval),
bellard66fb9762003-03-23 01:06:05 +00003980 &ovalue.it_value);
3981 }
3982 }
3983 break;
bellard31e31b82003-02-18 22:55:36 +00003984 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00003985 {
bellard66fb9762003-03-23 01:06:05 +00003986 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00003987
bellard66fb9762003-03-23 01:06:05 +00003988 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00003989 if (!is_error(ret) && arg2) {
3990 host_to_target_timeval(arg2,
bellard66fb9762003-03-23 01:06:05 +00003991 &value.it_interval);
pbrook53a59602006-03-25 19:31:22 +00003992 host_to_target_timeval(arg2 + sizeof(struct target_timeval),
bellard66fb9762003-03-23 01:06:05 +00003993 &value.it_value);
3994 }
3995 }
3996 break;
bellard31e31b82003-02-18 22:55:36 +00003997 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00003998 if (!(p = lock_user_string(arg1)))
3999 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004000 ret = get_errno(stat(path(p), &st));
4001 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004002 goto do_stat;
4003 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00004004 if (!(p = lock_user_string(arg1)))
4005 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004006 ret = get_errno(lstat(path(p), &st));
4007 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004008 goto do_stat;
4009 case TARGET_NR_fstat:
4010 {
4011 ret = get_errno(fstat(arg1, &st));
4012 do_stat:
4013 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004014 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00004015
bellard579a97f2007-11-11 14:26:47 +00004016 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4017 goto efault;
blueswir1992f48a2007-10-14 16:27:31 +00004018#if defined(TARGET_MIPS) || (defined(TARGET_SPARC64) && !defined(TARGET_ABI32))
thse3584652007-06-01 11:49:38 +00004019 target_st->st_dev = tswap32(st.st_dev);
4020#else
bellard31e31b82003-02-18 22:55:36 +00004021 target_st->st_dev = tswap16(st.st_dev);
thse3584652007-06-01 11:49:38 +00004022#endif
bellard31e31b82003-02-18 22:55:36 +00004023 target_st->st_ino = tswapl(st.st_ino);
ths7d600c82006-12-08 01:32:58 +00004024#if defined(TARGET_PPC) || defined(TARGET_MIPS)
bellard67867302003-11-23 17:05:30 +00004025 target_st->st_mode = tswapl(st.st_mode); /* XXX: check this */
4026 target_st->st_uid = tswap32(st.st_uid);
4027 target_st->st_gid = tswap32(st.st_gid);
blueswir1992f48a2007-10-14 16:27:31 +00004028#elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir11b8dd642007-07-08 10:08:24 +00004029 target_st->st_mode = tswap32(st.st_mode);
4030 target_st->st_uid = tswap32(st.st_uid);
4031 target_st->st_gid = tswap32(st.st_gid);
bellard67867302003-11-23 17:05:30 +00004032#else
bellardec86b0f2003-04-11 00:15:04 +00004033 target_st->st_mode = tswap16(st.st_mode);
bellard31e31b82003-02-18 22:55:36 +00004034 target_st->st_uid = tswap16(st.st_uid);
4035 target_st->st_gid = tswap16(st.st_gid);
bellard67867302003-11-23 17:05:30 +00004036#endif
thse3584652007-06-01 11:49:38 +00004037#if defined(TARGET_MIPS)
4038 /* If this is the same on PPC, then just merge w/ the above ifdef */
4039 target_st->st_nlink = tswapl(st.st_nlink);
4040 target_st->st_rdev = tswapl(st.st_rdev);
blueswir1992f48a2007-10-14 16:27:31 +00004041#elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir11b8dd642007-07-08 10:08:24 +00004042 target_st->st_nlink = tswap32(st.st_nlink);
4043 target_st->st_rdev = tswap32(st.st_rdev);
thse3584652007-06-01 11:49:38 +00004044#else
bellard67867302003-11-23 17:05:30 +00004045 target_st->st_nlink = tswap16(st.st_nlink);
bellard31e31b82003-02-18 22:55:36 +00004046 target_st->st_rdev = tswap16(st.st_rdev);
thse3584652007-06-01 11:49:38 +00004047#endif
bellard31e31b82003-02-18 22:55:36 +00004048 target_st->st_size = tswapl(st.st_size);
4049 target_st->st_blksize = tswapl(st.st_blksize);
4050 target_st->st_blocks = tswapl(st.st_blocks);
bellard7854b052003-03-29 17:22:23 +00004051 target_st->target_st_atime = tswapl(st.st_atime);
4052 target_st->target_st_mtime = tswapl(st.st_mtime);
4053 target_st->target_st_ctime = tswapl(st.st_ctime);
pbrook53a59602006-03-25 19:31:22 +00004054 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00004055 }
4056 }
4057 break;
bellardebc05482003-09-30 21:08:41 +00004058#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00004059 case TARGET_NR_olduname:
4060 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004061#endif
4062#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00004063 case TARGET_NR_iopl:
4064 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004065#endif
bellard31e31b82003-02-18 22:55:36 +00004066 case TARGET_NR_vhangup:
4067 ret = get_errno(vhangup());
4068 break;
bellardebc05482003-09-30 21:08:41 +00004069#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00004070 case TARGET_NR_idle:
4071 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004072#endif
bellard42ad6ae2005-01-03 22:48:11 +00004073#ifdef TARGET_NR_syscall
4074 case TARGET_NR_syscall:
4075 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
4076 break;
4077#endif
bellard31e31b82003-02-18 22:55:36 +00004078 case TARGET_NR_wait4:
4079 {
4080 int status;
blueswir1992f48a2007-10-14 16:27:31 +00004081 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00004082 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00004083 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00004084 if (target_rusage)
4085 rusage_ptr = &rusage;
4086 else
4087 rusage_ptr = NULL;
4088 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
4089 if (!is_error(ret)) {
4090 if (status_ptr)
pbrook53a59602006-03-25 19:31:22 +00004091 tputl(status_ptr, status);
bellard31e31b82003-02-18 22:55:36 +00004092 if (target_rusage) {
bellardb4091862003-05-16 15:39:34 +00004093 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00004094 }
4095 }
4096 }
4097 break;
thse5febef2007-04-01 18:31:35 +00004098#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00004099 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00004100 if (!(p = lock_user_string(arg1)))
4101 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004102 ret = get_errno(swapoff(p));
4103 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004104 break;
thse5febef2007-04-01 18:31:35 +00004105#endif
bellard31e31b82003-02-18 22:55:36 +00004106 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00004107 {
pbrook53a59602006-03-25 19:31:22 +00004108 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00004109 struct sysinfo value;
4110 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00004111 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00004112 {
bellard579a97f2007-11-11 14:26:47 +00004113 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
4114 goto efault;
bellarda5448a72004-06-19 16:59:03 +00004115 __put_user(value.uptime, &target_value->uptime);
4116 __put_user(value.loads[0], &target_value->loads[0]);
4117 __put_user(value.loads[1], &target_value->loads[1]);
4118 __put_user(value.loads[2], &target_value->loads[2]);
4119 __put_user(value.totalram, &target_value->totalram);
4120 __put_user(value.freeram, &target_value->freeram);
4121 __put_user(value.sharedram, &target_value->sharedram);
4122 __put_user(value.bufferram, &target_value->bufferram);
4123 __put_user(value.totalswap, &target_value->totalswap);
4124 __put_user(value.freeswap, &target_value->freeswap);
4125 __put_user(value.procs, &target_value->procs);
4126 __put_user(value.totalhigh, &target_value->totalhigh);
4127 __put_user(value.freehigh, &target_value->freehigh);
4128 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00004129 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00004130 }
4131 }
4132 break;
thse5febef2007-04-01 18:31:35 +00004133#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00004134 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00004135 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
4136 break;
thse5febef2007-04-01 18:31:35 +00004137#endif
bellard31e31b82003-02-18 22:55:36 +00004138 case TARGET_NR_fsync:
4139 ret = get_errno(fsync(arg1));
4140 break;
bellard31e31b82003-02-18 22:55:36 +00004141 case TARGET_NR_clone:
bellard1b6b0292003-03-22 17:31:38 +00004142 ret = get_errno(do_fork(cpu_env, arg1, arg2));
4143 break;
bellardec86b0f2003-04-11 00:15:04 +00004144#ifdef __NR_exit_group
4145 /* new thread calls */
4146 case TARGET_NR_exit_group:
bellarde9009672005-04-26 20:42:36 +00004147 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00004148 ret = get_errno(exit_group(arg1));
4149 break;
4150#endif
bellard31e31b82003-02-18 22:55:36 +00004151 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00004152 if (!(p = lock_user_string(arg1)))
4153 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004154 ret = get_errno(setdomainname(p, arg2));
4155 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004156 break;
4157 case TARGET_NR_uname:
4158 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00004159 {
4160 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00004161
bellard579a97f2007-11-11 14:26:47 +00004162 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
4163 goto efault;
bellard29e619b2004-09-13 21:41:04 +00004164 ret = get_errno(sys_uname(buf));
4165 if (!is_error(ret)) {
4166 /* Overrite the native machine name with whatever is being
4167 emulated. */
4168 strcpy (buf->machine, UNAME_MACHINE);
pbrookc5937222006-05-14 11:30:38 +00004169 /* Allow the user to override the reported release. */
4170 if (qemu_uname_release && *qemu_uname_release)
4171 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00004172 }
pbrook53a59602006-03-25 19:31:22 +00004173 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00004174 }
bellard31e31b82003-02-18 22:55:36 +00004175 break;
bellard6dbad632003-03-16 18:05:05 +00004176#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00004177 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00004178 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00004179 break;
j_mayer84409dd2007-04-06 08:56:50 +00004180#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00004181 case TARGET_NR_vm86old:
4182 goto unimplemented;
4183 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00004184 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00004185 break;
4186#endif
j_mayer84409dd2007-04-06 08:56:50 +00004187#endif
bellard31e31b82003-02-18 22:55:36 +00004188 case TARGET_NR_adjtimex:
4189 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004190#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00004191 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00004192#endif
bellard31e31b82003-02-18 22:55:36 +00004193 case TARGET_NR_init_module:
4194 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00004195#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00004196 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00004197#endif
bellard31e31b82003-02-18 22:55:36 +00004198 goto unimplemented;
4199 case TARGET_NR_quotactl:
4200 goto unimplemented;
4201 case TARGET_NR_getpgid:
4202 ret = get_errno(getpgid(arg1));
4203 break;
4204 case TARGET_NR_fchdir:
4205 ret = get_errno(fchdir(arg1));
4206 break;
j_mayer84409dd2007-04-06 08:56:50 +00004207#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00004208 case TARGET_NR_bdflush:
4209 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00004210#endif
thse5febef2007-04-01 18:31:35 +00004211#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00004212 case TARGET_NR_sysfs:
4213 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004214#endif
bellard31e31b82003-02-18 22:55:36 +00004215 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00004216 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00004217 break;
thse5febef2007-04-01 18:31:35 +00004218#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00004219 case TARGET_NR_afs_syscall:
4220 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004221#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004222#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004223 case TARGET_NR__llseek:
4224 {
bellard4f2ac232004-04-26 19:44:02 +00004225#if defined (__x86_64__)
4226 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
pbrook53a59602006-03-25 19:31:22 +00004227 tput64(arg4, ret);
bellard4f2ac232004-04-26 19:44:02 +00004228#else
bellard31e31b82003-02-18 22:55:36 +00004229 int64_t res;
4230 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
pbrook53a59602006-03-25 19:31:22 +00004231 tput64(arg4, res);
bellard4f2ac232004-04-26 19:44:02 +00004232#endif
bellard31e31b82003-02-18 22:55:36 +00004233 }
4234 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004235#endif
bellard31e31b82003-02-18 22:55:36 +00004236 case TARGET_NR_getdents:
blueswir1992f48a2007-10-14 16:27:31 +00004237#if TARGET_ABI_BITS != 32
pbrook53a59602006-03-25 19:31:22 +00004238 goto unimplemented;
bellarda315a142005-01-30 22:59:18 +00004239#warning not supported
blueswir1992f48a2007-10-14 16:27:31 +00004240#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00004241 {
pbrook53a59602006-03-25 19:31:22 +00004242 struct target_dirent *target_dirp;
bellard4add45b2003-06-05 01:52:59 +00004243 struct dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004244 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00004245
4246 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00004247 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00004248 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00004249 goto fail;
4250 }
ths3b46e622007-09-17 08:09:54 +00004251
bellard4add45b2003-06-05 01:52:59 +00004252 ret = get_errno(sys_getdents(arg1, dirp, count));
4253 if (!is_error(ret)) {
4254 struct dirent *de;
4255 struct target_dirent *tde;
4256 int len = ret;
4257 int reclen, treclen;
4258 int count1, tnamelen;
4259
4260 count1 = 0;
4261 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00004262 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4263 goto efault;
bellard4add45b2003-06-05 01:52:59 +00004264 tde = target_dirp;
4265 while (len > 0) {
4266 reclen = de->d_reclen;
blueswir1992f48a2007-10-14 16:27:31 +00004267 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
bellard4add45b2003-06-05 01:52:59 +00004268 tde->d_reclen = tswap16(treclen);
4269 tde->d_ino = tswapl(de->d_ino);
4270 tde->d_off = tswapl(de->d_off);
blueswir1992f48a2007-10-14 16:27:31 +00004271 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
bellard4add45b2003-06-05 01:52:59 +00004272 if (tnamelen > 256)
4273 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00004274 /* XXX: may not be correct */
bellard4add45b2003-06-05 01:52:59 +00004275 strncpy(tde->d_name, de->d_name, tnamelen);
4276 de = (struct dirent *)((char *)de + reclen);
4277 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00004278 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00004279 count1 += treclen;
4280 }
4281 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00004282 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00004283 }
4284 free(dirp);
4285 }
4286#else
bellard31e31b82003-02-18 22:55:36 +00004287 {
pbrook53a59602006-03-25 19:31:22 +00004288 struct dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004289 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00004290
bellard579a97f2007-11-11 14:26:47 +00004291 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4292 goto efault;
bellard72f03902003-02-18 23:33:18 +00004293 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00004294 if (!is_error(ret)) {
4295 struct dirent *de;
4296 int len = ret;
4297 int reclen;
4298 de = dirp;
4299 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004300 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00004301 if (reclen > len)
4302 break;
bellard8083a3e2003-03-24 23:12:16 +00004303 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00004304 tswapls(&de->d_ino);
4305 tswapls(&de->d_off);
4306 de = (struct dirent *)((char *)de + reclen);
4307 len -= reclen;
4308 }
4309 }
pbrook53a59602006-03-25 19:31:22 +00004310 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00004311 }
bellard4add45b2003-06-05 01:52:59 +00004312#endif
bellard31e31b82003-02-18 22:55:36 +00004313 break;
ths3ae43202007-09-16 21:39:48 +00004314#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00004315 case TARGET_NR_getdents64:
4316 {
pbrook53a59602006-03-25 19:31:22 +00004317 struct dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004318 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00004319 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4320 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00004321 ret = get_errno(sys_getdents64(arg1, dirp, count));
4322 if (!is_error(ret)) {
4323 struct dirent64 *de;
4324 int len = ret;
4325 int reclen;
4326 de = dirp;
4327 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004328 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00004329 if (reclen > len)
4330 break;
bellard8083a3e2003-03-24 23:12:16 +00004331 de->d_reclen = tswap16(reclen);
bellarddab2ed92003-03-22 15:23:14 +00004332 tswap64s(&de->d_ino);
4333 tswap64s(&de->d_off);
4334 de = (struct dirent64 *)((char *)de + reclen);
4335 len -= reclen;
4336 }
4337 }
pbrook53a59602006-03-25 19:31:22 +00004338 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00004339 }
4340 break;
bellarda541f292004-04-12 20:39:29 +00004341#endif /* TARGET_NR_getdents64 */
thse5febef2007-04-01 18:31:35 +00004342#ifdef TARGET_NR__newselect
bellard31e31b82003-02-18 22:55:36 +00004343 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00004344 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00004345 break;
thse5febef2007-04-01 18:31:35 +00004346#endif
4347#ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00004348 case TARGET_NR_poll:
4349 {
pbrook53a59602006-03-25 19:31:22 +00004350 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00004351 unsigned int nfds = arg2;
4352 int timeout = arg3;
4353 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00004354 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00004355
bellard579a97f2007-11-11 14:26:47 +00004356 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
4357 if (!target_pfd)
4358 goto efault;
bellard9de5e442003-03-23 16:49:39 +00004359 pfd = alloca(sizeof(struct pollfd) * nfds);
4360 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004361 pfd[i].fd = tswap32(target_pfd[i].fd);
4362 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00004363 }
4364 ret = get_errno(poll(pfd, nfds, timeout));
4365 if (!is_error(ret)) {
4366 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004367 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00004368 }
pbrook53a59602006-03-25 19:31:22 +00004369 ret += nfds * (sizeof(struct target_pollfd)
4370 - sizeof(struct pollfd));
bellard9de5e442003-03-23 16:49:39 +00004371 }
pbrook53a59602006-03-25 19:31:22 +00004372 unlock_user(target_pfd, arg1, ret);
bellard9de5e442003-03-23 16:49:39 +00004373 }
4374 break;
thse5febef2007-04-01 18:31:35 +00004375#endif
bellard31e31b82003-02-18 22:55:36 +00004376 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00004377 /* NOTE: the flock constant seems to be the same for every
4378 Linux platform */
4379 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00004380 break;
4381 case TARGET_NR_readv:
4382 {
4383 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00004384 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00004385
4386 vec = alloca(count * sizeof(struct iovec));
bellard579a97f2007-11-11 14:26:47 +00004387 lock_iovec(VERIFY_WRITE, vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00004388 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00004389 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00004390 }
4391 break;
4392 case TARGET_NR_writev:
4393 {
4394 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00004395 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00004396
4397 vec = alloca(count * sizeof(struct iovec));
bellard579a97f2007-11-11 14:26:47 +00004398 lock_iovec(VERIFY_READ, vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00004399 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00004400 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00004401 }
4402 break;
4403 case TARGET_NR_getsid:
4404 ret = get_errno(getsid(arg1));
4405 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004406#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00004407 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00004408 ret = get_errno(fdatasync(arg1));
4409 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004410#endif
bellard31e31b82003-02-18 22:55:36 +00004411 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00004412 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00004413 return value. */
ths0da46a62007-10-20 20:23:07 +00004414 ret = -TARGET_ENOTDIR;
4415 break;
bellard31e31b82003-02-18 22:55:36 +00004416 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00004417 {
pbrook53a59602006-03-25 19:31:22 +00004418 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004419 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00004420
bellard579a97f2007-11-11 14:26:47 +00004421 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
4422 goto efault;
bellard5cd43932003-03-29 16:54:36 +00004423 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004424 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00004425 ret = get_errno(sched_setparam(arg1, &schp));
4426 }
4427 break;
bellard31e31b82003-02-18 22:55:36 +00004428 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00004429 {
pbrook53a59602006-03-25 19:31:22 +00004430 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004431 struct sched_param schp;
4432 ret = get_errno(sched_getparam(arg1, &schp));
4433 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004434 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
4435 goto efault;
bellard5cd43932003-03-29 16:54:36 +00004436 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004437 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00004438 }
4439 }
4440 break;
bellard31e31b82003-02-18 22:55:36 +00004441 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00004442 {
pbrook53a59602006-03-25 19:31:22 +00004443 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004444 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00004445 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
4446 goto efault;
bellard5cd43932003-03-29 16:54:36 +00004447 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004448 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00004449 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
4450 }
4451 break;
bellard31e31b82003-02-18 22:55:36 +00004452 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00004453 ret = get_errno(sched_getscheduler(arg1));
4454 break;
bellard31e31b82003-02-18 22:55:36 +00004455 case TARGET_NR_sched_yield:
4456 ret = get_errno(sched_yield());
4457 break;
4458 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00004459 ret = get_errno(sched_get_priority_max(arg1));
4460 break;
bellard31e31b82003-02-18 22:55:36 +00004461 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00004462 ret = get_errno(sched_get_priority_min(arg1));
4463 break;
bellard31e31b82003-02-18 22:55:36 +00004464 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00004465 {
bellard5cd43932003-03-29 16:54:36 +00004466 struct timespec ts;
4467 ret = get_errno(sched_rr_get_interval(arg1, &ts));
4468 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004469 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00004470 }
4471 }
4472 break;
bellard31e31b82003-02-18 22:55:36 +00004473 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00004474 {
bellard1b6b0292003-03-22 17:31:38 +00004475 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00004476 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00004477 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00004478 if (is_error(ret) && arg2) {
4479 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00004480 }
4481 }
4482 break;
thse5febef2007-04-01 18:31:35 +00004483#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00004484 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00004485 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004486#endif
4487#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00004488 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00004489 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004490#endif
bellard31e31b82003-02-18 22:55:36 +00004491 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00004492 switch (arg1)
4493 {
4494 case PR_GET_PDEATHSIG:
4495 {
4496 int deathsig;
4497 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
4498 if (!is_error(ret) && arg2)
4499 tput32(arg2, deathsig);
4500 }
4501 break;
4502 default:
4503 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
4504 break;
4505 }
ths39b9aae2007-02-11 18:36:44 +00004506 break;
bellard67867302003-11-23 17:05:30 +00004507#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00004508 case TARGET_NR_pread:
pbrook53a59602006-03-25 19:31:22 +00004509 page_unprotect_range(arg2, arg3);
bellard579a97f2007-11-11 14:26:47 +00004510 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4511 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004512 ret = get_errno(pread(arg1, p, arg3, arg4));
4513 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00004514 break;
bellard31e31b82003-02-18 22:55:36 +00004515 case TARGET_NR_pwrite:
bellard579a97f2007-11-11 14:26:47 +00004516 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4517 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004518 ret = get_errno(pwrite(arg1, p, arg3, arg4));
4519 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00004520 break;
bellard67867302003-11-23 17:05:30 +00004521#endif
bellard31e31b82003-02-18 22:55:36 +00004522 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00004523 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
4524 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004525 ret = get_errno(sys_getcwd1(p, arg2));
4526 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00004527 break;
4528 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00004529 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004530 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00004531 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004532 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00004533#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
4534 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
bellard579a97f2007-11-11 14:26:47 +00004535 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00004536 break;
4537#else
bellard5cd43932003-03-29 16:54:36 +00004538 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00004539#endif
bellard31e31b82003-02-18 22:55:36 +00004540 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00004541 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004542#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00004543 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00004544 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004545#endif
4546#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00004547 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00004548 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004549#endif
bellard048f6b42005-11-26 18:47:20 +00004550#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00004551 case TARGET_NR_vfork:
bellard1b6b0292003-03-22 17:31:38 +00004552 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00004553 break;
bellard048f6b42005-11-26 18:47:20 +00004554#endif
bellardebc05482003-09-30 21:08:41 +00004555#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00004556 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00004557 {
4558 struct rlimit rlim;
4559 ret = get_errno(getrlimit(arg1, &rlim));
4560 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004561 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00004562 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
4563 goto efault;
bellard728584b2003-04-29 20:43:36 +00004564 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
4565 target_rlim->rlim_max = tswapl(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00004566 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00004567 }
4568 break;
4569 }
bellardebc05482003-09-30 21:08:41 +00004570#endif
bellarda315a142005-01-30 22:59:18 +00004571#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00004572 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00004573 if (!(p = lock_user_string(arg1)))
4574 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004575 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
4576 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00004577 break;
bellarda315a142005-01-30 22:59:18 +00004578#endif
4579#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00004580 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00004581 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00004582 break;
bellarda315a142005-01-30 22:59:18 +00004583#endif
4584#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00004585 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00004586 if (!(p = lock_user_string(arg1)))
4587 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004588 ret = get_errno(stat(path(p), &st));
4589 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004590 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004591#endif
4592#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00004593 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00004594 if (!(p = lock_user_string(arg1)))
4595 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004596 ret = get_errno(lstat(path(p), &st));
4597 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004598 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004599#endif
4600#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00004601 case TARGET_NR_fstat64:
bellard60cd49d2003-03-16 22:53:56 +00004602 {
4603 ret = get_errno(fstat(arg1, &st));
4604 do_stat64:
4605 if (!is_error(ret)) {
pbrookce4defa2006-02-09 16:49:55 +00004606#ifdef TARGET_ARM
4607 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004608 struct target_eabi_stat64 *target_st;
bellard579a97f2007-11-11 14:26:47 +00004609
4610 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4611 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00004612 memset(target_st, 0, sizeof(struct target_eabi_stat64));
bellard579a97f2007-11-11 14:26:47 +00004613 __put_user(st.st_dev, &target_st->st_dev);
4614 __put_user(st.st_ino, &target_st->st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004615#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
bellard579a97f2007-11-11 14:26:47 +00004616 __put_user(st.st_ino, &target_st->__st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004617#endif
bellard579a97f2007-11-11 14:26:47 +00004618 __put_user(st.st_mode, &target_st->st_mode);
4619 __put_user(st.st_nlink, &target_st->st_nlink);
4620 __put_user(st.st_uid, &target_st->st_uid);
4621 __put_user(st.st_gid, &target_st->st_gid);
4622 __put_user(st.st_rdev, &target_st->st_rdev);
4623 __put_user(st.st_size, &target_st->st_size);
4624 __put_user(st.st_blksize, &target_st->st_blksize);
4625 __put_user(st.st_blocks, &target_st->st_blocks);
4626 __put_user(st.st_atime, &target_st->target_st_atime);
4627 __put_user(st.st_mtime, &target_st->target_st_mtime);
4628 __put_user(st.st_ctime, &target_st->target_st_ctime);
4629 unlock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00004630 } else
4631#endif
4632 {
pbrook53a59602006-03-25 19:31:22 +00004633 struct target_stat64 *target_st;
bellard579a97f2007-11-11 14:26:47 +00004634
4635 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4636 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00004637 memset(target_st, 0, sizeof(struct target_stat64));
bellard579a97f2007-11-11 14:26:47 +00004638 __put_user(st.st_dev, &target_st->st_dev);
4639 __put_user(st.st_ino, &target_st->st_ino);
pbrookce4defa2006-02-09 16:49:55 +00004640#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
bellard579a97f2007-11-11 14:26:47 +00004641 __put_user(st.st_ino, &target_st->__st_ino);
pbrookce4defa2006-02-09 16:49:55 +00004642#endif
bellard579a97f2007-11-11 14:26:47 +00004643 __put_user(st.st_mode, &target_st->st_mode);
4644 __put_user(st.st_nlink, &target_st->st_nlink);
4645 __put_user(st.st_uid, &target_st->st_uid);
4646 __put_user(st.st_gid, &target_st->st_gid);
4647 __put_user(st.st_rdev, &target_st->st_rdev);
pbrookce4defa2006-02-09 16:49:55 +00004648 /* XXX: better use of kernel struct */
bellard579a97f2007-11-11 14:26:47 +00004649 __put_user(st.st_size, &target_st->st_size);
4650 __put_user(st.st_blksize, &target_st->st_blksize);
4651 __put_user(st.st_blocks, &target_st->st_blocks);
4652 __put_user(st.st_atime, &target_st->target_st_atime);
4653 __put_user(st.st_mtime, &target_st->target_st_mtime);
4654 __put_user(st.st_ctime, &target_st->target_st_ctime);
4655 unlock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00004656 }
bellard60cd49d2003-03-16 22:53:56 +00004657 }
4658 }
4659 break;
bellarda315a142005-01-30 22:59:18 +00004660#endif
bellard67867302003-11-23 17:05:30 +00004661#ifdef USE_UID16
4662 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00004663 if (!(p = lock_user_string(arg1)))
4664 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004665 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
4666 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00004667 break;
4668 case TARGET_NR_getuid:
4669 ret = get_errno(high2lowuid(getuid()));
4670 break;
4671 case TARGET_NR_getgid:
4672 ret = get_errno(high2lowgid(getgid()));
4673 break;
4674 case TARGET_NR_geteuid:
4675 ret = get_errno(high2lowuid(geteuid()));
4676 break;
4677 case TARGET_NR_getegid:
4678 ret = get_errno(high2lowgid(getegid()));
4679 break;
4680 case TARGET_NR_setreuid:
4681 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
4682 break;
4683 case TARGET_NR_setregid:
4684 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
4685 break;
4686 case TARGET_NR_getgroups:
4687 {
4688 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004689 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00004690 gid_t *grouplist;
4691 int i;
4692
4693 grouplist = alloca(gidsetsize * sizeof(gid_t));
4694 ret = get_errno(getgroups(gidsetsize, grouplist));
4695 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004696 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
4697 if (!target_grouplist)
4698 goto efault;
bellard67867302003-11-23 17:05:30 +00004699 for(i = 0;i < gidsetsize; i++)
4700 target_grouplist[i] = tswap16(grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00004701 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00004702 }
4703 }
4704 break;
4705 case TARGET_NR_setgroups:
4706 {
4707 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004708 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00004709 gid_t *grouplist;
4710 int i;
4711
4712 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00004713 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
4714 if (!target_grouplist) {
4715 ret = -TARGET_EFAULT;
4716 goto fail;
4717 }
bellard67867302003-11-23 17:05:30 +00004718 for(i = 0;i < gidsetsize; i++)
4719 grouplist[i] = tswap16(target_grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00004720 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00004721 ret = get_errno(setgroups(gidsetsize, grouplist));
4722 }
4723 break;
4724 case TARGET_NR_fchown:
4725 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
4726 break;
thsccfa72b2007-09-24 09:23:34 +00004727#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
4728 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00004729 if (!(p = lock_user_string(arg2)))
4730 goto efault;
4731 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
4732 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00004733 break;
4734#endif
bellard67867302003-11-23 17:05:30 +00004735#ifdef TARGET_NR_setresuid
4736 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00004737 ret = get_errno(setresuid(low2highuid(arg1),
4738 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00004739 low2highuid(arg3)));
4740 break;
4741#endif
4742#ifdef TARGET_NR_getresuid
4743 case TARGET_NR_getresuid:
4744 {
pbrook53a59602006-03-25 19:31:22 +00004745 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00004746 ret = get_errno(getresuid(&ruid, &euid, &suid));
4747 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004748 tput16(arg1, tswap16(high2lowuid(ruid)));
4749 tput16(arg2, tswap16(high2lowuid(euid)));
4750 tput16(arg3, tswap16(high2lowuid(suid)));
bellard67867302003-11-23 17:05:30 +00004751 }
4752 }
4753 break;
4754#endif
4755#ifdef TARGET_NR_getresgid
4756 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00004757 ret = get_errno(setresgid(low2highgid(arg1),
4758 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00004759 low2highgid(arg3)));
4760 break;
4761#endif
4762#ifdef TARGET_NR_getresgid
4763 case TARGET_NR_getresgid:
4764 {
pbrook53a59602006-03-25 19:31:22 +00004765 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00004766 ret = get_errno(getresgid(&rgid, &egid, &sgid));
4767 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004768 tput16(arg1, tswap16(high2lowgid(rgid)));
4769 tput16(arg2, tswap16(high2lowgid(egid)));
4770 tput16(arg3, tswap16(high2lowgid(sgid)));
bellard67867302003-11-23 17:05:30 +00004771 }
4772 }
4773 break;
4774#endif
4775 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00004776 if (!(p = lock_user_string(arg1)))
4777 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004778 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
4779 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00004780 break;
4781 case TARGET_NR_setuid:
4782 ret = get_errno(setuid(low2highuid(arg1)));
4783 break;
4784 case TARGET_NR_setgid:
4785 ret = get_errno(setgid(low2highgid(arg1)));
4786 break;
4787 case TARGET_NR_setfsuid:
4788 ret = get_errno(setfsuid(arg1));
4789 break;
4790 case TARGET_NR_setfsgid:
4791 ret = get_errno(setfsgid(arg1));
4792 break;
4793#endif /* USE_UID16 */
4794
bellarda315a142005-01-30 22:59:18 +00004795#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00004796 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00004797 if (!(p = lock_user_string(arg1)))
4798 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004799 ret = get_errno(lchown(p, arg2, arg3));
4800 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00004801 break;
bellarda315a142005-01-30 22:59:18 +00004802#endif
4803#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00004804 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00004805 ret = get_errno(getuid());
4806 break;
bellarda315a142005-01-30 22:59:18 +00004807#endif
4808#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00004809 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00004810 ret = get_errno(getgid());
4811 break;
bellarda315a142005-01-30 22:59:18 +00004812#endif
4813#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00004814 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00004815 ret = get_errno(geteuid());
4816 break;
bellarda315a142005-01-30 22:59:18 +00004817#endif
4818#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00004819 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00004820 ret = get_errno(getegid());
4821 break;
bellarda315a142005-01-30 22:59:18 +00004822#endif
4823#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00004824 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00004825 ret = get_errno(setreuid(arg1, arg2));
4826 break;
bellarda315a142005-01-30 22:59:18 +00004827#endif
4828#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00004829 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00004830 ret = get_errno(setregid(arg1, arg2));
4831 break;
bellarda315a142005-01-30 22:59:18 +00004832#endif
4833#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00004834 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00004835 {
4836 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004837 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00004838 gid_t *grouplist;
4839 int i;
4840
4841 grouplist = alloca(gidsetsize * sizeof(gid_t));
4842 ret = get_errno(getgroups(gidsetsize, grouplist));
4843 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004844 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
4845 if (!target_grouplist) {
4846 ret = -TARGET_EFAULT;
4847 goto fail;
4848 }
bellard99c475a2005-01-31 20:45:13 +00004849 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00004850 target_grouplist[i] = tswap32(grouplist[i]);
4851 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00004852 }
4853 }
4854 break;
bellarda315a142005-01-30 22:59:18 +00004855#endif
4856#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00004857 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00004858 {
4859 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004860 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00004861 gid_t *grouplist;
4862 int i;
ths3b46e622007-09-17 08:09:54 +00004863
bellard99c475a2005-01-31 20:45:13 +00004864 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00004865 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
4866 if (!target_grouplist) {
4867 ret = -TARGET_EFAULT;
4868 goto fail;
4869 }
bellard99c475a2005-01-31 20:45:13 +00004870 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00004871 grouplist[i] = tswap32(target_grouplist[i]);
4872 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00004873 ret = get_errno(setgroups(gidsetsize, grouplist));
4874 }
4875 break;
bellarda315a142005-01-30 22:59:18 +00004876#endif
4877#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00004878 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00004879 ret = get_errno(fchown(arg1, arg2, arg3));
4880 break;
bellarda315a142005-01-30 22:59:18 +00004881#endif
4882#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00004883 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00004884 ret = get_errno(setresuid(arg1, arg2, arg3));
4885 break;
bellarda315a142005-01-30 22:59:18 +00004886#endif
4887#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00004888 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00004889 {
pbrook53a59602006-03-25 19:31:22 +00004890 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00004891 ret = get_errno(getresuid(&ruid, &euid, &suid));
4892 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004893 tput32(arg1, tswap32(ruid));
4894 tput32(arg2, tswap32(euid));
4895 tput32(arg3, tswap32(suid));
bellardb03c60f2003-03-23 17:19:56 +00004896 }
4897 }
4898 break;
bellarda315a142005-01-30 22:59:18 +00004899#endif
4900#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00004901 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00004902 ret = get_errno(setresgid(arg1, arg2, arg3));
4903 break;
bellarda315a142005-01-30 22:59:18 +00004904#endif
4905#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00004906 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00004907 {
pbrook53a59602006-03-25 19:31:22 +00004908 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00004909 ret = get_errno(getresgid(&rgid, &egid, &sgid));
4910 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004911 tput32(arg1, tswap32(rgid));
4912 tput32(arg2, tswap32(egid));
4913 tput32(arg3, tswap32(sgid));
bellardb03c60f2003-03-23 17:19:56 +00004914 }
4915 }
4916 break;
bellarda315a142005-01-30 22:59:18 +00004917#endif
4918#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00004919 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00004920 if (!(p = lock_user_string(arg1)))
4921 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004922 ret = get_errno(chown(p, arg2, arg3));
4923 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00004924 break;
bellarda315a142005-01-30 22:59:18 +00004925#endif
4926#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00004927 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00004928 ret = get_errno(setuid(arg1));
4929 break;
bellarda315a142005-01-30 22:59:18 +00004930#endif
4931#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00004932 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00004933 ret = get_errno(setgid(arg1));
4934 break;
bellarda315a142005-01-30 22:59:18 +00004935#endif
4936#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00004937 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00004938 ret = get_errno(setfsuid(arg1));
4939 break;
bellarda315a142005-01-30 22:59:18 +00004940#endif
4941#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00004942 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00004943 ret = get_errno(setfsgid(arg1));
4944 break;
bellarda315a142005-01-30 22:59:18 +00004945#endif
bellard67867302003-11-23 17:05:30 +00004946
bellard31e31b82003-02-18 22:55:36 +00004947 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00004948 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00004949#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00004950 case TARGET_NR_mincore:
bellardb03c60f2003-03-23 17:19:56 +00004951 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00004952#endif
4953#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00004954 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00004955 /* A straight passthrough may not be safe because qemu sometimes
4956 turns private flie-backed mappings into anonymous mappings.
4957 This will break MADV_DONTNEED.
4958 This is a hint, so ignoring and returning success is ok. */
4959 ret = get_errno(0);
4960 break;
bellardffa65c32004-01-04 23:57:22 +00004961#endif
blueswir1992f48a2007-10-14 16:27:31 +00004962#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00004963 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00004964 {
thsb1e341e2007-03-20 21:50:52 +00004965 int cmd;
bellard77e46722003-04-29 20:39:06 +00004966 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00004967 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00004968#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00004969 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00004970#endif
bellard77e46722003-04-29 20:39:06 +00004971
thsb1e341e2007-03-20 21:50:52 +00004972 switch(arg2){
4973 case TARGET_F_GETLK64:
4974 cmd = F_GETLK64;
thsa7222582007-03-22 15:08:55 +00004975 break;
thsb1e341e2007-03-20 21:50:52 +00004976 case TARGET_F_SETLK64:
4977 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00004978 break;
thsb1e341e2007-03-20 21:50:52 +00004979 case TARGET_F_SETLKW64:
4980 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00004981 break;
thsb1e341e2007-03-20 21:50:52 +00004982 default:
4983 cmd = arg2;
thsa7222582007-03-22 15:08:55 +00004984 break;
thsb1e341e2007-03-20 21:50:52 +00004985 }
4986
bellard60cd49d2003-03-16 22:53:56 +00004987 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00004988 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00004989#ifdef TARGET_ARM
4990 if (((CPUARMState *)cpu_env)->eabi) {
bellard579a97f2007-11-11 14:26:47 +00004991 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1)) {
4992 ret = -TARGET_EFAULT;
4993 goto fail;
4994 }
ths58134272007-03-31 18:59:32 +00004995 fl.l_type = tswap16(target_efl->l_type);
4996 fl.l_whence = tswap16(target_efl->l_whence);
4997 fl.l_start = tswap64(target_efl->l_start);
4998 fl.l_len = tswap64(target_efl->l_len);
4999 fl.l_pid = tswapl(target_efl->l_pid);
5000 unlock_user_struct(target_efl, arg3, 0);
5001 } else
5002#endif
5003 {
bellard579a97f2007-11-11 14:26:47 +00005004 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1)) {
5005 ret = -TARGET_EFAULT;
5006 goto fail;
5007 }
ths58134272007-03-31 18:59:32 +00005008 fl.l_type = tswap16(target_fl->l_type);
5009 fl.l_whence = tswap16(target_fl->l_whence);
5010 fl.l_start = tswap64(target_fl->l_start);
5011 fl.l_len = tswap64(target_fl->l_len);
5012 fl.l_pid = tswapl(target_fl->l_pid);
5013 unlock_user_struct(target_fl, arg3, 0);
5014 }
thsb1e341e2007-03-20 21:50:52 +00005015 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00005016 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00005017#ifdef TARGET_ARM
5018 if (((CPUARMState *)cpu_env)->eabi) {
bellard579a97f2007-11-11 14:26:47 +00005019 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0)) {
5020 ret = -TARGET_EFAULT;
5021 goto fail;
5022 }
pbrookce4defa2006-02-09 16:49:55 +00005023 target_efl->l_type = tswap16(fl.l_type);
5024 target_efl->l_whence = tswap16(fl.l_whence);
5025 target_efl->l_start = tswap64(fl.l_start);
5026 target_efl->l_len = tswap64(fl.l_len);
5027 target_efl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00005028 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00005029 } else
5030#endif
5031 {
bellard579a97f2007-11-11 14:26:47 +00005032 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0)) {
5033 ret = -TARGET_EFAULT;
5034 goto fail;
5035 }
pbrookce4defa2006-02-09 16:49:55 +00005036 target_fl->l_type = tswap16(fl.l_type);
5037 target_fl->l_whence = tswap16(fl.l_whence);
5038 target_fl->l_start = tswap64(fl.l_start);
5039 target_fl->l_len = tswap64(fl.l_len);
5040 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00005041 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00005042 }
bellard77e46722003-04-29 20:39:06 +00005043 }
5044 break;
5045
thsb1e341e2007-03-20 21:50:52 +00005046 case TARGET_F_SETLK64:
5047 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00005048#ifdef TARGET_ARM
5049 if (((CPUARMState *)cpu_env)->eabi) {
bellard579a97f2007-11-11 14:26:47 +00005050 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1)) {
5051 ret = -TARGET_EFAULT;
5052 goto fail;
5053 }
pbrookce4defa2006-02-09 16:49:55 +00005054 fl.l_type = tswap16(target_efl->l_type);
5055 fl.l_whence = tswap16(target_efl->l_whence);
5056 fl.l_start = tswap64(target_efl->l_start);
5057 fl.l_len = tswap64(target_efl->l_len);
5058 fl.l_pid = tswapl(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00005059 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00005060 } else
5061#endif
5062 {
bellard579a97f2007-11-11 14:26:47 +00005063 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1)) {
5064 ret = -TARGET_EFAULT;
5065 goto fail;
5066 }
pbrookce4defa2006-02-09 16:49:55 +00005067 fl.l_type = tswap16(target_fl->l_type);
5068 fl.l_whence = tswap16(target_fl->l_whence);
5069 fl.l_start = tswap64(target_fl->l_start);
5070 fl.l_len = tswap64(target_fl->l_len);
5071 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00005072 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00005073 }
thsb1e341e2007-03-20 21:50:52 +00005074 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00005075 break;
bellard60cd49d2003-03-16 22:53:56 +00005076 default:
thsb1e341e2007-03-20 21:50:52 +00005077 ret = get_errno(do_fcntl(arg1, cmd, arg3));
bellard60cd49d2003-03-16 22:53:56 +00005078 break;
5079 }
bellard77e46722003-04-29 20:39:06 +00005080 break;
5081 }
bellard60cd49d2003-03-16 22:53:56 +00005082#endif
ths7d600c82006-12-08 01:32:58 +00005083#ifdef TARGET_NR_cacheflush
5084 case TARGET_NR_cacheflush:
5085 /* self-modifying code is handled automatically, so nothing needed */
5086 ret = 0;
5087 break;
5088#endif
bellardebc05482003-09-30 21:08:41 +00005089#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00005090 case TARGET_NR_security:
5091 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005092#endif
bellardc573ff62004-01-04 15:51:36 +00005093#ifdef TARGET_NR_getpagesize
5094 case TARGET_NR_getpagesize:
5095 ret = TARGET_PAGE_SIZE;
5096 break;
5097#endif
bellard31e31b82003-02-18 22:55:36 +00005098 case TARGET_NR_gettid:
5099 ret = get_errno(gettid());
5100 break;
thse5febef2007-04-01 18:31:35 +00005101#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00005102 case TARGET_NR_readahead:
bellard5cd43932003-03-29 16:54:36 +00005103 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005104#endif
bellardebc05482003-09-30 21:08:41 +00005105#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00005106 case TARGET_NR_setxattr:
5107 case TARGET_NR_lsetxattr:
5108 case TARGET_NR_fsetxattr:
5109 case TARGET_NR_getxattr:
5110 case TARGET_NR_lgetxattr:
5111 case TARGET_NR_fgetxattr:
5112 case TARGET_NR_listxattr:
5113 case TARGET_NR_llistxattr:
5114 case TARGET_NR_flistxattr:
5115 case TARGET_NR_removexattr:
5116 case TARGET_NR_lremovexattr:
5117 case TARGET_NR_fremovexattr:
bellard5cd43932003-03-29 16:54:36 +00005118 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00005119#endif
5120#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00005121 case TARGET_NR_set_thread_area:
ths6f5b89a2007-03-02 20:48:00 +00005122#ifdef TARGET_MIPS
5123 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
5124 ret = 0;
5125 break;
5126#else
5127 goto unimplemented_nowarn;
5128#endif
5129#endif
5130#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00005131 case TARGET_NR_get_thread_area:
5132 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00005133#endif
bellard48dc41e2006-06-21 18:15:50 +00005134#ifdef TARGET_NR_getdomainname
5135 case TARGET_NR_getdomainname:
5136 goto unimplemented_nowarn;
5137#endif
ths6f5b89a2007-03-02 20:48:00 +00005138
thsb5906f92007-03-19 13:32:45 +00005139#ifdef TARGET_NR_clock_gettime
5140 case TARGET_NR_clock_gettime:
5141 {
5142 struct timespec ts;
5143 ret = get_errno(clock_gettime(arg1, &ts));
5144 if (!is_error(ret)) {
5145 host_to_target_timespec(arg2, &ts);
5146 }
5147 break;
5148 }
5149#endif
5150#ifdef TARGET_NR_clock_getres
5151 case TARGET_NR_clock_getres:
5152 {
5153 struct timespec ts;
5154 ret = get_errno(clock_getres(arg1, &ts));
5155 if (!is_error(ret)) {
5156 host_to_target_timespec(arg2, &ts);
5157 }
5158 break;
5159 }
5160#endif
5161
ths6f5b89a2007-03-02 20:48:00 +00005162#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
5163 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00005164 ret = get_errno(set_tid_address((int *)g2h(arg1)));
5165 break;
ths6f5b89a2007-03-02 20:48:00 +00005166#endif
5167
ths3ae43202007-09-16 21:39:48 +00005168#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00005169 case TARGET_NR_tkill:
5170 ret = get_errno(sys_tkill((int)arg1, (int)arg2));
5171 break;
5172#endif
5173
ths3ae43202007-09-16 21:39:48 +00005174#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00005175 case TARGET_NR_tgkill:
5176 ret = get_errno(sys_tgkill((int)arg1, (int)arg2, (int)arg3));
5177 break;
5178#endif
5179
ths4f2b1fe2007-06-21 21:57:12 +00005180#ifdef TARGET_NR_set_robust_list
5181 case TARGET_NR_set_robust_list:
5182 goto unimplemented_nowarn;
5183#endif
5184
ths9007f0e2007-09-25 17:50:37 +00005185#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
5186 case TARGET_NR_utimensat:
5187 {
5188 struct timespec ts[2];
5189 target_to_host_timespec(ts, arg3);
5190 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
5191 if (!arg2)
5192 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
5193 else {
bellard579a97f2007-11-11 14:26:47 +00005194 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00005195 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00005196 goto fail;
5197 }
5198 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
5199 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00005200 }
5201 }
5202 break;
5203#endif
5204
bellard31e31b82003-02-18 22:55:36 +00005205 default:
5206 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00005207 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00005208#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 +00005209 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00005210#endif
ths0da46a62007-10-20 20:23:07 +00005211 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00005212 break;
5213 }
bellard579a97f2007-11-11 14:26:47 +00005214fail:
bellardc573ff62004-01-04 15:51:36 +00005215#ifdef DEBUG
5216 gemu_log(" = %ld\n", ret);
5217#endif
thsb92c47c2007-11-01 00:07:38 +00005218 if(do_strace)
5219 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00005220 return ret;
bellard579a97f2007-11-11 14:26:47 +00005221efault:
5222 ret = -TARGET_EFAULT;
5223 goto fail;
bellard31e31b82003-02-18 22:55:36 +00005224}