blob: 6c8f689ae22bd4d2719da943797c7f3a67653652 [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? */
bellard5a4a8982007-11-11 17:39:18 +0000672static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
673 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +0000674{
675 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +0000676 abi_long msg_controllen;
677 abi_ulong target_cmsg_addr;
678 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +0000679 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +0000680
681 msg_controllen = tswapl(target_msgh->msg_controllen);
682 if (msg_controllen < sizeof (struct target_cmsghdr))
683 goto the_end;
684 target_cmsg_addr = tswapl(target_msgh->msg_control);
685 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
686 if (!target_cmsg)
687 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +0000688
689 while (cmsg && target_cmsg) {
690 void *data = CMSG_DATA(cmsg);
691 void *target_data = TARGET_CMSG_DATA(target_cmsg);
692
ths5fafdf22007-09-16 21:08:06 +0000693 int len = tswapl(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +0000694 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
695
696 space += CMSG_SPACE(len);
697 if (space > msgh->msg_controllen) {
698 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +0000699 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +0000700 break;
701 }
702
703 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
704 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
705 cmsg->cmsg_len = CMSG_LEN(len);
706
bellard3532fa72006-06-24 15:06:03 +0000707 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +0000708 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
709 memcpy(data, target_data, len);
710 } else {
711 int *fd = (int *)data;
712 int *target_fd = (int *)target_data;
713 int i, numfds = len / sizeof(int);
714
715 for (i = 0; i < numfds; i++)
716 fd[i] = tswap32(target_fd[i]);
717 }
718
719 cmsg = CMSG_NXTHDR(msgh, cmsg);
720 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
721 }
bellard5a4a8982007-11-11 17:39:18 +0000722 unlock_user(target_cmsg, target_cmsg_addr, 0);
723 the_end:
bellard7854b052003-03-29 17:22:23 +0000724 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +0000725 return 0;
bellard7854b052003-03-29 17:22:23 +0000726}
727
pbrook53a59602006-03-25 19:31:22 +0000728/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +0000729static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
730 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +0000731{
732 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +0000733 abi_long msg_controllen;
734 abi_ulong target_cmsg_addr;
735 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +0000736 socklen_t space = 0;
737
bellard5a4a8982007-11-11 17:39:18 +0000738 msg_controllen = tswapl(target_msgh->msg_controllen);
739 if (msg_controllen < sizeof (struct target_cmsghdr))
740 goto the_end;
741 target_cmsg_addr = tswapl(target_msgh->msg_control);
742 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
743 if (!target_cmsg)
744 return -TARGET_EFAULT;
745
bellard7854b052003-03-29 17:22:23 +0000746 while (cmsg && target_cmsg) {
747 void *data = CMSG_DATA(cmsg);
748 void *target_data = TARGET_CMSG_DATA(target_cmsg);
749
750 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
751
752 space += TARGET_CMSG_SPACE(len);
bellard5a4a8982007-11-11 17:39:18 +0000753 if (space > msg_controllen) {
bellard7854b052003-03-29 17:22:23 +0000754 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +0000755 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +0000756 break;
757 }
758
759 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
760 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
761 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
762
bellard3532fa72006-06-24 15:06:03 +0000763 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +0000764 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
765 memcpy(target_data, data, len);
766 } else {
767 int *fd = (int *)data;
768 int *target_fd = (int *)target_data;
769 int i, numfds = len / sizeof(int);
770
771 for (i = 0; i < numfds; i++)
772 target_fd[i] = tswap32(fd[i]);
773 }
774
775 cmsg = CMSG_NXTHDR(msgh, cmsg);
776 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
777 }
bellard5a4a8982007-11-11 17:39:18 +0000778 unlock_user(target_cmsg, target_cmsg_addr, space);
779 the_end:
780 target_msgh->msg_controllen = tswapl(space);
781 return 0;
bellard7854b052003-03-29 17:22:23 +0000782}
783
ths0da46a62007-10-20 20:23:07 +0000784/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000785static abi_long do_setsockopt(int sockfd, int level, int optname,
786 abi_ulong optval, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +0000787{
blueswir1992f48a2007-10-14 16:27:31 +0000788 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +0000789 int val;
ths3b46e622007-09-17 08:09:54 +0000790
bellard8853f862004-02-22 14:57:26 +0000791 switch(level) {
792 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +0000793 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +0000794 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +0000795 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +0000796
pbrook53a59602006-03-25 19:31:22 +0000797 val = tget32(optval);
bellard8853f862004-02-22 14:57:26 +0000798 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
799 break;
800 case SOL_IP:
801 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +0000802 case IP_TOS:
803 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +0000804 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +0000805 case IP_ROUTER_ALERT:
806 case IP_RECVOPTS:
807 case IP_RETOPTS:
808 case IP_PKTINFO:
809 case IP_MTU_DISCOVER:
810 case IP_RECVERR:
811 case IP_RECVTOS:
812#ifdef IP_FREEBIND
813 case IP_FREEBIND:
814#endif
815 case IP_MULTICAST_TTL:
816 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +0000817 val = 0;
818 if (optlen >= sizeof(uint32_t)) {
pbrook53a59602006-03-25 19:31:22 +0000819 val = tget32(optval);
bellard8853f862004-02-22 14:57:26 +0000820 } else if (optlen >= 1) {
pbrook53a59602006-03-25 19:31:22 +0000821 val = tget8(optval);
bellard8853f862004-02-22 14:57:26 +0000822 }
823 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
824 break;
825 default:
826 goto unimplemented;
827 }
828 break;
bellard3532fa72006-06-24 15:06:03 +0000829 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +0000830 switch (optname) {
831 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +0000832 case TARGET_SO_DEBUG:
833 optname = SO_DEBUG;
834 break;
835 case TARGET_SO_REUSEADDR:
836 optname = SO_REUSEADDR;
837 break;
838 case TARGET_SO_TYPE:
839 optname = SO_TYPE;
840 break;
841 case TARGET_SO_ERROR:
842 optname = SO_ERROR;
843 break;
844 case TARGET_SO_DONTROUTE:
845 optname = SO_DONTROUTE;
846 break;
847 case TARGET_SO_BROADCAST:
848 optname = SO_BROADCAST;
849 break;
850 case TARGET_SO_SNDBUF:
851 optname = SO_SNDBUF;
852 break;
853 case TARGET_SO_RCVBUF:
854 optname = SO_RCVBUF;
855 break;
856 case TARGET_SO_KEEPALIVE:
857 optname = SO_KEEPALIVE;
858 break;
859 case TARGET_SO_OOBINLINE:
860 optname = SO_OOBINLINE;
861 break;
862 case TARGET_SO_NO_CHECK:
863 optname = SO_NO_CHECK;
864 break;
865 case TARGET_SO_PRIORITY:
866 optname = SO_PRIORITY;
867 break;
bellard5e83e8e2005-03-01 22:32:06 +0000868#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +0000869 case TARGET_SO_BSDCOMPAT:
870 optname = SO_BSDCOMPAT;
871 break;
bellard5e83e8e2005-03-01 22:32:06 +0000872#endif
bellard3532fa72006-06-24 15:06:03 +0000873 case TARGET_SO_PASSCRED:
874 optname = SO_PASSCRED;
875 break;
876 case TARGET_SO_TIMESTAMP:
877 optname = SO_TIMESTAMP;
878 break;
879 case TARGET_SO_RCVLOWAT:
880 optname = SO_RCVLOWAT;
881 break;
882 case TARGET_SO_RCVTIMEO:
883 optname = SO_RCVTIMEO;
884 break;
885 case TARGET_SO_SNDTIMEO:
886 optname = SO_SNDTIMEO;
887 break;
bellard8853f862004-02-22 14:57:26 +0000888 break;
889 default:
890 goto unimplemented;
891 }
bellard3532fa72006-06-24 15:06:03 +0000892 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +0000893 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +0000894
895 val = tget32(optval);
896 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +0000897 break;
bellard7854b052003-03-29 17:22:23 +0000898 default:
bellard8853f862004-02-22 14:57:26 +0000899 unimplemented:
900 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
ths0da46a62007-10-20 20:23:07 +0000901 ret = -TARGET_ENOSYS;
bellard7854b052003-03-29 17:22:23 +0000902 }
bellard8853f862004-02-22 14:57:26 +0000903 return ret;
bellard7854b052003-03-29 17:22:23 +0000904}
905
ths0da46a62007-10-20 20:23:07 +0000906/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000907static abi_long do_getsockopt(int sockfd, int level, int optname,
908 abi_ulong optval, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +0000909{
blueswir1992f48a2007-10-14 16:27:31 +0000910 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +0000911 int len, lv, val;
bellard8853f862004-02-22 14:57:26 +0000912
913 switch(level) {
bellard3532fa72006-06-24 15:06:03 +0000914 case TARGET_SOL_SOCKET:
915 level = SOL_SOCKET;
bellard8853f862004-02-22 14:57:26 +0000916 switch (optname) {
bellard3532fa72006-06-24 15:06:03 +0000917 case TARGET_SO_LINGER:
918 case TARGET_SO_RCVTIMEO:
919 case TARGET_SO_SNDTIMEO:
920 case TARGET_SO_PEERCRED:
921 case TARGET_SO_PEERNAME:
bellard8853f862004-02-22 14:57:26 +0000922 /* These don't just return a single integer */
923 goto unimplemented;
924 default:
bellard2efbe912005-07-23 15:10:20 +0000925 goto int_case;
926 }
927 break;
928 case SOL_TCP:
929 /* TCP options all take an 'int' value. */
930 int_case:
pbrook53a59602006-03-25 19:31:22 +0000931 len = tget32(optlen);
bellard2efbe912005-07-23 15:10:20 +0000932 if (len < 0)
ths0da46a62007-10-20 20:23:07 +0000933 return -TARGET_EINVAL;
bellard2efbe912005-07-23 15:10:20 +0000934 lv = sizeof(int);
935 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
936 if (ret < 0)
937 return ret;
938 val = tswap32(val);
939 if (len > lv)
940 len = lv;
pbrook53a59602006-03-25 19:31:22 +0000941 if (len == 4)
942 tput32(optval, val);
943 else
944 tput8(optval, val);
945 tput32(optlen, len);
bellard2efbe912005-07-23 15:10:20 +0000946 break;
947 case SOL_IP:
948 switch(optname) {
949 case IP_TOS:
950 case IP_TTL:
951 case IP_HDRINCL:
952 case IP_ROUTER_ALERT:
953 case IP_RECVOPTS:
954 case IP_RETOPTS:
955 case IP_PKTINFO:
956 case IP_MTU_DISCOVER:
957 case IP_RECVERR:
958 case IP_RECVTOS:
959#ifdef IP_FREEBIND
960 case IP_FREEBIND:
961#endif
962 case IP_MULTICAST_TTL:
963 case IP_MULTICAST_LOOP:
pbrook53a59602006-03-25 19:31:22 +0000964 len = tget32(optlen);
bellard8853f862004-02-22 14:57:26 +0000965 if (len < 0)
ths0da46a62007-10-20 20:23:07 +0000966 return -TARGET_EINVAL;
bellard8853f862004-02-22 14:57:26 +0000967 lv = sizeof(int);
968 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
969 if (ret < 0)
970 return ret;
bellard2efbe912005-07-23 15:10:20 +0000971 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +0000972 len = 1;
pbrook53a59602006-03-25 19:31:22 +0000973 tput32(optlen, len);
974 tput8(optval, val);
bellard2efbe912005-07-23 15:10:20 +0000975 } else {
bellard2efbe912005-07-23 15:10:20 +0000976 if (len > sizeof(int))
977 len = sizeof(int);
pbrook53a59602006-03-25 19:31:22 +0000978 tput32(optlen, len);
979 tput32(optval, val);
bellard2efbe912005-07-23 15:10:20 +0000980 }
bellard8853f862004-02-22 14:57:26 +0000981 break;
bellard2efbe912005-07-23 15:10:20 +0000982 default:
983 goto unimplemented;
bellard8853f862004-02-22 14:57:26 +0000984 }
985 break;
986 default:
987 unimplemented:
988 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
989 level, optname);
ths0da46a62007-10-20 20:23:07 +0000990 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +0000991 break;
992 }
993 return ret;
bellard7854b052003-03-29 17:22:23 +0000994}
995
bellard579a97f2007-11-11 14:26:47 +0000996/* FIXME
997 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
998 * other lock functions have a return code of 0 for failure.
999 */
1000static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1001 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001002{
1003 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001004 abi_ulong base;
bellard579a97f2007-11-11 14:26:47 +00001005 int i, j;
pbrook53a59602006-03-25 19:31:22 +00001006
bellard579a97f2007-11-11 14:26:47 +00001007 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1008 if (!target_vec)
1009 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001010 for(i = 0;i < count; i++) {
1011 base = tswapl(target_vec[i].iov_base);
1012 vec[i].iov_len = tswapl(target_vec[i].iov_len);
bellard579a97f2007-11-11 14:26:47 +00001013 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
1014 if (!vec[i].iov_base)
1015 goto fail;
pbrook53a59602006-03-25 19:31:22 +00001016 }
1017 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001018 return 0;
1019 fail:
1020 /* failure - unwind locks */
1021 for (j = 0; j < i; j++) {
1022 base = tswapl(target_vec[j].iov_base);
1023 unlock_user(vec[j].iov_base, base, 0);
1024 }
1025 unlock_user (target_vec, target_addr, 0);
1026 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001027}
1028
bellard579a97f2007-11-11 14:26:47 +00001029static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1030 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001031{
1032 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001033 abi_ulong base;
pbrook53a59602006-03-25 19:31:22 +00001034 int i;
1035
bellard579a97f2007-11-11 14:26:47 +00001036 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1037 if (!target_vec)
1038 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001039 for(i = 0;i < count; i++) {
1040 base = tswapl(target_vec[i].iov_base);
1041 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1042 }
1043 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001044
1045 return 0;
pbrook53a59602006-03-25 19:31:22 +00001046}
1047
ths0da46a62007-10-20 20:23:07 +00001048/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001049static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00001050{
1051#if defined(TARGET_MIPS)
1052 switch(type) {
1053 case TARGET_SOCK_DGRAM:
1054 type = SOCK_DGRAM;
1055 break;
1056 case TARGET_SOCK_STREAM:
1057 type = SOCK_STREAM;
1058 break;
1059 case TARGET_SOCK_RAW:
1060 type = SOCK_RAW;
1061 break;
1062 case TARGET_SOCK_RDM:
1063 type = SOCK_RDM;
1064 break;
1065 case TARGET_SOCK_SEQPACKET:
1066 type = SOCK_SEQPACKET;
1067 break;
1068 case TARGET_SOCK_PACKET:
1069 type = SOCK_PACKET;
1070 break;
1071 }
1072#endif
balrog12bc92a2007-10-30 21:06:14 +00001073 if (domain == PF_NETLINK)
1074 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
bellard3532fa72006-06-24 15:06:03 +00001075 return get_errno(socket(domain, type, protocol));
1076}
1077
ths0da46a62007-10-20 20:23:07 +00001078/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001079static abi_long do_bind(int sockfd, abi_ulong target_addr,
1080 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001081{
1082 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001083
bellard3532fa72006-06-24 15:06:03 +00001084 target_to_host_sockaddr(addr, target_addr, addrlen);
1085 return get_errno(bind(sockfd, addr, addrlen));
1086}
1087
ths0da46a62007-10-20 20:23:07 +00001088/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001089static abi_long do_connect(int sockfd, abi_ulong target_addr,
1090 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001091{
1092 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001093
bellard3532fa72006-06-24 15:06:03 +00001094 target_to_host_sockaddr(addr, target_addr, addrlen);
1095 return get_errno(connect(sockfd, addr, addrlen));
1096}
1097
ths0da46a62007-10-20 20:23:07 +00001098/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001099static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1100 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001101{
blueswir1992f48a2007-10-14 16:27:31 +00001102 abi_long ret;
bellard3532fa72006-06-24 15:06:03 +00001103 struct target_msghdr *msgp;
1104 struct msghdr msg;
1105 int count;
1106 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001107 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001108
bellard579a97f2007-11-11 14:26:47 +00001109 /* FIXME */
1110 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1111 msgp,
1112 target_msg,
1113 send ? 1 : 0))
1114 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001115 if (msgp->msg_name) {
1116 msg.msg_namelen = tswap32(msgp->msg_namelen);
1117 msg.msg_name = alloca(msg.msg_namelen);
1118 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1119 msg.msg_namelen);
1120 } else {
1121 msg.msg_name = NULL;
1122 msg.msg_namelen = 0;
1123 }
1124 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1125 msg.msg_control = alloca(msg.msg_controllen);
1126 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001127
bellard3532fa72006-06-24 15:06:03 +00001128 count = tswapl(msgp->msg_iovlen);
1129 vec = alloca(count * sizeof(struct iovec));
1130 target_vec = tswapl(msgp->msg_iov);
bellard579a97f2007-11-11 14:26:47 +00001131 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
bellard3532fa72006-06-24 15:06:03 +00001132 msg.msg_iovlen = count;
1133 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001134
bellard3532fa72006-06-24 15:06:03 +00001135 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00001136 ret = target_to_host_cmsg(&msg, msgp);
1137 if (ret == 0)
1138 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00001139 } else {
1140 ret = get_errno(recvmsg(fd, &msg, flags));
1141 if (!is_error(ret))
bellard5a4a8982007-11-11 17:39:18 +00001142 ret = host_to_target_cmsg(msgp, &msg);
bellard3532fa72006-06-24 15:06:03 +00001143 }
1144 unlock_iovec(vec, target_vec, count, !send);
bellard579a97f2007-11-11 14:26:47 +00001145 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00001146 return ret;
1147}
1148
ths0da46a62007-10-20 20:23:07 +00001149/* do_accept() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001150static abi_long do_accept(int fd, abi_ulong target_addr,
1151 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001152{
1153 socklen_t addrlen = tget32(target_addrlen);
ths197ea352006-12-11 19:13:59 +00001154 void *addr = alloca(addrlen);
blueswir1992f48a2007-10-14 16:27:31 +00001155 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001156
1157 ret = get_errno(accept(fd, addr, &addrlen));
1158 if (!is_error(ret)) {
1159 host_to_target_sockaddr(target_addr, addr, addrlen);
1160 tput32(target_addrlen, addrlen);
1161 }
1162 return ret;
1163}
1164
ths0da46a62007-10-20 20:23:07 +00001165/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001166static abi_long do_getpeername(int fd, abi_ulong target_addr,
1167 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001168{
1169 socklen_t addrlen = tget32(target_addrlen);
ths01c227f2007-03-19 13:05:13 +00001170 void *addr = alloca(addrlen);
blueswir1992f48a2007-10-14 16:27:31 +00001171 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001172
1173 ret = get_errno(getpeername(fd, addr, &addrlen));
1174 if (!is_error(ret)) {
1175 host_to_target_sockaddr(target_addr, addr, addrlen);
1176 tput32(target_addrlen, addrlen);
1177 }
1178 return ret;
1179}
1180
ths0da46a62007-10-20 20:23:07 +00001181/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001182static abi_long do_getsockname(int fd, abi_ulong target_addr,
1183 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001184{
1185 socklen_t addrlen = tget32(target_addrlen);
ths01c227f2007-03-19 13:05:13 +00001186 void *addr = alloca(addrlen);
blueswir1992f48a2007-10-14 16:27:31 +00001187 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001188
1189 ret = get_errno(getsockname(fd, addr, &addrlen));
1190 if (!is_error(ret)) {
1191 host_to_target_sockaddr(target_addr, addr, addrlen);
1192 tput32(target_addrlen, addrlen);
1193 }
1194 return ret;
1195}
1196
ths0da46a62007-10-20 20:23:07 +00001197/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001198static abi_long do_socketpair(int domain, int type, int protocol,
1199 abi_ulong target_tab)
pbrook1be9e1d2006-11-19 15:26:04 +00001200{
1201 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00001202 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001203
1204 ret = get_errno(socketpair(domain, type, protocol, tab));
1205 if (!is_error(ret)) {
1206 tput32(target_tab, tab[0]);
1207 tput32(target_tab + 4, tab[1]);
1208 }
1209 return ret;
1210}
1211
ths0da46a62007-10-20 20:23:07 +00001212/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001213static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1214 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001215{
1216 void *addr;
1217 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001218 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001219
bellard579a97f2007-11-11 14:26:47 +00001220 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1221 if (!host_msg)
1222 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001223 if (target_addr) {
1224 addr = alloca(addrlen);
1225 target_to_host_sockaddr(addr, target_addr, addrlen);
1226 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1227 } else {
1228 ret = get_errno(send(fd, host_msg, len, flags));
1229 }
1230 unlock_user(host_msg, msg, 0);
1231 return ret;
1232}
1233
ths0da46a62007-10-20 20:23:07 +00001234/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001235static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1236 abi_ulong target_addr,
1237 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001238{
1239 socklen_t addrlen;
1240 void *addr;
1241 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001242 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001243
bellard579a97f2007-11-11 14:26:47 +00001244 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1245 if (!host_msg)
1246 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001247 if (target_addr) {
1248 addrlen = tget32(target_addrlen);
1249 addr = alloca(addrlen);
1250 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1251 } else {
1252 addr = NULL; /* To keep compiler quiet. */
1253 ret = get_errno(recv(fd, host_msg, len, flags));
1254 }
1255 if (!is_error(ret)) {
1256 if (target_addr) {
1257 host_to_target_sockaddr(target_addr, addr, addrlen);
1258 tput32(target_addrlen, addrlen);
1259 }
1260 unlock_user(host_msg, msg, len);
1261 } else {
1262 unlock_user(host_msg, msg, 0);
1263 }
1264 return ret;
1265}
1266
j_mayer32407102007-09-26 23:01:49 +00001267#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00001268/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001269static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00001270{
blueswir1992f48a2007-10-14 16:27:31 +00001271 abi_long ret;
1272 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00001273
1274 switch(num) {
1275 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00001276 {
pbrook53a59602006-03-25 19:31:22 +00001277 int domain = tgetl(vptr);
1278 int type = tgetl(vptr + n);
1279 int protocol = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001280 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00001281 }
bellard31e31b82003-02-18 22:55:36 +00001282 break;
1283 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00001284 {
pbrook53a59602006-03-25 19:31:22 +00001285 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001286 abi_ulong target_addr = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001287 socklen_t addrlen = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001288 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001289 }
bellard31e31b82003-02-18 22:55:36 +00001290 break;
1291 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00001292 {
pbrook53a59602006-03-25 19:31:22 +00001293 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001294 abi_ulong target_addr = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001295 socklen_t addrlen = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001296 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001297 }
bellard31e31b82003-02-18 22:55:36 +00001298 break;
1299 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00001300 {
pbrook53a59602006-03-25 19:31:22 +00001301 int sockfd = tgetl(vptr);
1302 int backlog = tgetl(vptr + n);
bellard7854b052003-03-29 17:22:23 +00001303 ret = get_errno(listen(sockfd, backlog));
1304 }
bellard31e31b82003-02-18 22:55:36 +00001305 break;
1306 case SOCKOP_accept:
1307 {
pbrook53a59602006-03-25 19:31:22 +00001308 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001309 abi_ulong target_addr = tgetl(vptr + n);
1310 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001311 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001312 }
1313 break;
1314 case SOCKOP_getsockname:
1315 {
pbrook53a59602006-03-25 19:31:22 +00001316 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001317 abi_ulong target_addr = tgetl(vptr + n);
1318 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001319 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001320 }
1321 break;
1322 case SOCKOP_getpeername:
1323 {
pbrook53a59602006-03-25 19:31:22 +00001324 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001325 abi_ulong target_addr = tgetl(vptr + n);
1326 abi_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001327 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001328 }
1329 break;
1330 case SOCKOP_socketpair:
1331 {
pbrook53a59602006-03-25 19:31:22 +00001332 int domain = tgetl(vptr);
1333 int type = tgetl(vptr + n);
1334 int protocol = tgetl(vptr + 2 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001335 abi_ulong tab = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001336 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00001337 }
1338 break;
1339 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00001340 {
pbrook53a59602006-03-25 19:31:22 +00001341 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001342 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001343 size_t len = tgetl(vptr + 2 * n);
1344 int flags = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001345 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001346 }
bellard31e31b82003-02-18 22:55:36 +00001347 break;
1348 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00001349 {
pbrook53a59602006-03-25 19:31:22 +00001350 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001351 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001352 size_t len = tgetl(vptr + 2 * n);
1353 int flags = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001354 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001355 }
bellard31e31b82003-02-18 22:55:36 +00001356 break;
1357 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00001358 {
pbrook53a59602006-03-25 19:31:22 +00001359 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001360 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001361 size_t len = tgetl(vptr + 2 * n);
1362 int flags = tgetl(vptr + 3 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001363 abi_ulong addr = tgetl(vptr + 4 * n);
pbrook53a59602006-03-25 19:31:22 +00001364 socklen_t addrlen = tgetl(vptr + 5 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001365 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001366 }
bellard31e31b82003-02-18 22:55:36 +00001367 break;
1368 case SOCKOP_recvfrom:
1369 {
pbrook53a59602006-03-25 19:31:22 +00001370 int sockfd = tgetl(vptr);
blueswir1992f48a2007-10-14 16:27:31 +00001371 abi_ulong msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001372 size_t len = tgetl(vptr + 2 * n);
1373 int flags = tgetl(vptr + 3 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001374 abi_ulong addr = tgetl(vptr + 4 * n);
1375 abi_ulong addrlen = tgetl(vptr + 5 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001376 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00001377 }
1378 break;
1379 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00001380 {
pbrook53a59602006-03-25 19:31:22 +00001381 int sockfd = tgetl(vptr);
1382 int how = tgetl(vptr + n);
bellard7854b052003-03-29 17:22:23 +00001383
1384 ret = get_errno(shutdown(sockfd, how));
1385 }
bellard31e31b82003-02-18 22:55:36 +00001386 break;
1387 case SOCKOP_sendmsg:
1388 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00001389 {
1390 int fd;
blueswir1992f48a2007-10-14 16:27:31 +00001391 abi_ulong target_msg;
bellard3532fa72006-06-24 15:06:03 +00001392 int flags;
bellard1a9353d2003-03-16 20:28:50 +00001393
pbrook53a59602006-03-25 19:31:22 +00001394 fd = tgetl(vptr);
bellard3532fa72006-06-24 15:06:03 +00001395 target_msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001396 flags = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001397
ths5fafdf22007-09-16 21:08:06 +00001398 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00001399 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00001400 }
1401 break;
bellard31e31b82003-02-18 22:55:36 +00001402 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00001403 {
pbrook53a59602006-03-25 19:31:22 +00001404 int sockfd = tgetl(vptr);
1405 int level = tgetl(vptr + n);
1406 int optname = tgetl(vptr + 2 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001407 abi_ulong optval = tgetl(vptr + 3 * n);
pbrook53a59602006-03-25 19:31:22 +00001408 socklen_t optlen = tgetl(vptr + 4 * n);
bellard7854b052003-03-29 17:22:23 +00001409
1410 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1411 }
1412 break;
bellard31e31b82003-02-18 22:55:36 +00001413 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00001414 {
pbrook53a59602006-03-25 19:31:22 +00001415 int sockfd = tgetl(vptr);
1416 int level = tgetl(vptr + n);
1417 int optname = tgetl(vptr + 2 * n);
blueswir1992f48a2007-10-14 16:27:31 +00001418 abi_ulong optval = tgetl(vptr + 3 * n);
1419 abi_ulong poptlen = tgetl(vptr + 4 * n);
bellard7854b052003-03-29 17:22:23 +00001420
bellard8853f862004-02-22 14:57:26 +00001421 ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
bellard7854b052003-03-29 17:22:23 +00001422 }
1423 break;
bellard31e31b82003-02-18 22:55:36 +00001424 default:
1425 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00001426 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00001427 break;
1428 }
1429 return ret;
1430}
j_mayer32407102007-09-26 23:01:49 +00001431#endif
bellard31e31b82003-02-18 22:55:36 +00001432
j_mayer32407102007-09-26 23:01:49 +00001433#ifdef TARGET_NR_ipc
bellard8853f862004-02-22 14:57:26 +00001434#define N_SHM_REGIONS 32
1435
1436static struct shm_region {
bellard5a4a8982007-11-11 17:39:18 +00001437 abi_ulong start;
1438 abi_ulong size;
bellard8853f862004-02-22 14:57:26 +00001439} shm_regions[N_SHM_REGIONS];
1440
ths3eb6b042007-06-03 14:26:27 +00001441struct target_ipc_perm
1442{
blueswir1992f48a2007-10-14 16:27:31 +00001443 abi_long __key;
1444 abi_ulong uid;
1445 abi_ulong gid;
1446 abi_ulong cuid;
1447 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00001448 unsigned short int mode;
1449 unsigned short int __pad1;
1450 unsigned short int __seq;
1451 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00001452 abi_ulong __unused1;
1453 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00001454};
1455
1456struct target_semid_ds
1457{
1458 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001459 abi_ulong sem_otime;
1460 abi_ulong __unused1;
1461 abi_ulong sem_ctime;
1462 abi_ulong __unused2;
1463 abi_ulong sem_nsems;
1464 abi_ulong __unused3;
1465 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00001466};
1467
bellard579a97f2007-11-11 14:26:47 +00001468static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
1469 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001470{
1471 struct target_ipc_perm *target_ip;
1472 struct target_semid_ds *target_sd;
1473
bellard579a97f2007-11-11 14:26:47 +00001474 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1475 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001476 target_ip=&(target_sd->sem_perm);
1477 host_ip->__key = tswapl(target_ip->__key);
1478 host_ip->uid = tswapl(target_ip->uid);
1479 host_ip->gid = tswapl(target_ip->gid);
1480 host_ip->cuid = tswapl(target_ip->cuid);
1481 host_ip->cgid = tswapl(target_ip->cgid);
1482 host_ip->mode = tswapl(target_ip->mode);
1483 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001484 return 0;
ths3eb6b042007-06-03 14:26:27 +00001485}
1486
bellard579a97f2007-11-11 14:26:47 +00001487static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
1488 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00001489{
1490 struct target_ipc_perm *target_ip;
1491 struct target_semid_ds *target_sd;
1492
bellard579a97f2007-11-11 14:26:47 +00001493 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1494 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001495 target_ip = &(target_sd->sem_perm);
1496 target_ip->__key = tswapl(host_ip->__key);
1497 target_ip->uid = tswapl(host_ip->uid);
1498 target_ip->gid = tswapl(host_ip->gid);
1499 target_ip->cuid = tswapl(host_ip->cuid);
1500 target_ip->cgid = tswapl(host_ip->cgid);
1501 target_ip->mode = tswapl(host_ip->mode);
1502 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001503 return 0;
ths3eb6b042007-06-03 14:26:27 +00001504}
1505
bellard579a97f2007-11-11 14:26:47 +00001506static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
1507 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001508{
1509 struct target_semid_ds *target_sd;
1510
bellard579a97f2007-11-11 14:26:47 +00001511 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1512 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001513 target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1514 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1515 host_sd->sem_otime = tswapl(target_sd->sem_otime);
1516 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1517 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001518 return 0;
ths3eb6b042007-06-03 14:26:27 +00001519}
1520
bellard579a97f2007-11-11 14:26:47 +00001521static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
1522 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00001523{
1524 struct target_semid_ds *target_sd;
1525
bellard579a97f2007-11-11 14:26:47 +00001526 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1527 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001528 host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1529 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1530 target_sd->sem_otime = tswapl(host_sd->sem_otime);
1531 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1532 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001533 return 0;
ths3eb6b042007-06-03 14:26:27 +00001534}
1535
thsfa294812007-02-02 22:05:00 +00001536union semun {
1537 int val;
ths3eb6b042007-06-03 14:26:27 +00001538 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00001539 unsigned short *array;
1540};
1541
ths3eb6b042007-06-03 14:26:27 +00001542union target_semun {
1543 int val;
blueswir1992f48a2007-10-14 16:27:31 +00001544 abi_long buf;
ths3eb6b042007-06-03 14:26:27 +00001545 unsigned short int *array;
1546};
1547
bellard579a97f2007-11-11 14:26:47 +00001548static inline abi_long target_to_host_semun(int cmd,
1549 union semun *host_su,
1550 abi_ulong target_addr,
1551 struct semid_ds *ds)
ths3eb6b042007-06-03 14:26:27 +00001552{
1553 union target_semun *target_su;
1554
1555 switch( cmd ) {
1556 case IPC_STAT:
1557 case IPC_SET:
bellard579a97f2007-11-11 14:26:47 +00001558 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1559 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001560 target_to_host_semid_ds(ds,target_su->buf);
1561 host_su->buf = ds;
1562 unlock_user_struct(target_su, target_addr, 0);
1563 break;
1564 case GETVAL:
1565 case SETVAL:
bellard579a97f2007-11-11 14:26:47 +00001566 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1567 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001568 host_su->val = tswapl(target_su->val);
1569 unlock_user_struct(target_su, target_addr, 0);
1570 break;
1571 case GETALL:
1572 case SETALL:
bellard579a97f2007-11-11 14:26:47 +00001573 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1574 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001575 *host_su->array = tswap16(*target_su->array);
1576 unlock_user_struct(target_su, target_addr, 0);
1577 break;
1578 default:
1579 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1580 }
bellard579a97f2007-11-11 14:26:47 +00001581 return 0;
ths3eb6b042007-06-03 14:26:27 +00001582}
1583
bellard579a97f2007-11-11 14:26:47 +00001584static inline abi_long host_to_target_semun(int cmd,
1585 abi_ulong target_addr,
1586 union semun *host_su,
1587 struct semid_ds *ds)
ths3eb6b042007-06-03 14:26:27 +00001588{
1589 union target_semun *target_su;
1590
1591 switch( cmd ) {
1592 case IPC_STAT:
1593 case IPC_SET:
bellard579a97f2007-11-11 14:26:47 +00001594 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1595 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001596 host_to_target_semid_ds(target_su->buf,ds);
1597 unlock_user_struct(target_su, target_addr, 1);
1598 break;
1599 case GETVAL:
1600 case SETVAL:
bellard579a97f2007-11-11 14:26:47 +00001601 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1602 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001603 target_su->val = tswapl(host_su->val);
1604 unlock_user_struct(target_su, target_addr, 1);
1605 break;
1606 case GETALL:
1607 case SETALL:
bellard579a97f2007-11-11 14:26:47 +00001608 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1609 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001610 *target_su->array = tswap16(*host_su->array);
1611 unlock_user_struct(target_su, target_addr, 1);
1612 break;
1613 default:
1614 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1615 }
bellard579a97f2007-11-11 14:26:47 +00001616 return 0;
ths3eb6b042007-06-03 14:26:27 +00001617}
1618
blueswir1992f48a2007-10-14 16:27:31 +00001619static inline abi_long do_semctl(int first, int second, int third,
1620 abi_long ptr)
ths3eb6b042007-06-03 14:26:27 +00001621{
1622 union semun arg;
1623 struct semid_ds dsarg;
1624 int cmd = third&0xff;
blueswir1992f48a2007-10-14 16:27:31 +00001625 abi_long ret = 0;
ths3eb6b042007-06-03 14:26:27 +00001626
1627 switch( cmd ) {
1628 case GETVAL:
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 case SETVAL:
1634 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1635 ret = get_errno(semctl(first, second, cmd, arg));
1636 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1637 break;
1638 case GETALL:
1639 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1640 ret = get_errno(semctl(first, second, cmd, arg));
1641 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1642 break;
1643 case SETALL:
1644 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1645 ret = get_errno(semctl(first, second, cmd, arg));
1646 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1647 break;
1648 case IPC_STAT:
1649 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1650 ret = get_errno(semctl(first, second, cmd, arg));
1651 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1652 break;
1653 case IPC_SET:
1654 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1655 ret = get_errno(semctl(first, second, cmd, arg));
1656 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1657 break;
1658 default:
1659 ret = get_errno(semctl(first, second, cmd, arg));
1660 }
1661
1662 return ret;
1663}
1664
ths1bc012f2007-06-03 14:27:49 +00001665struct target_msqid_ds
1666{
1667 struct target_ipc_perm msg_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001668 abi_ulong msg_stime;
1669 abi_ulong __unused1;
1670 abi_ulong msg_rtime;
1671 abi_ulong __unused2;
1672 abi_ulong msg_ctime;
1673 abi_ulong __unused3;
1674 abi_ulong __msg_cbytes;
1675 abi_ulong msg_qnum;
1676 abi_ulong msg_qbytes;
1677 abi_ulong msg_lspid;
1678 abi_ulong msg_lrpid;
1679 abi_ulong __unused4;
1680 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00001681};
1682
bellard579a97f2007-11-11 14:26:47 +00001683static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
1684 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00001685{
1686 struct target_msqid_ds *target_md;
1687
bellard579a97f2007-11-11 14:26:47 +00001688 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
1689 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001690 target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1691 host_md->msg_stime = tswapl(target_md->msg_stime);
1692 host_md->msg_rtime = tswapl(target_md->msg_rtime);
1693 host_md->msg_ctime = tswapl(target_md->msg_ctime);
1694 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1695 host_md->msg_qnum = tswapl(target_md->msg_qnum);
1696 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1697 host_md->msg_lspid = tswapl(target_md->msg_lspid);
1698 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1699 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001700 return 0;
ths1bc012f2007-06-03 14:27:49 +00001701}
1702
bellard579a97f2007-11-11 14:26:47 +00001703static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
1704 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00001705{
1706 struct target_msqid_ds *target_md;
1707
bellard579a97f2007-11-11 14:26:47 +00001708 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
1709 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001710 host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1711 target_md->msg_stime = tswapl(host_md->msg_stime);
1712 target_md->msg_rtime = tswapl(host_md->msg_rtime);
1713 target_md->msg_ctime = tswapl(host_md->msg_ctime);
1714 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1715 target_md->msg_qnum = tswapl(host_md->msg_qnum);
1716 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1717 target_md->msg_lspid = tswapl(host_md->msg_lspid);
1718 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1719 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001720 return 0;
ths1bc012f2007-06-03 14:27:49 +00001721}
1722
blueswir1992f48a2007-10-14 16:27:31 +00001723static inline abi_long do_msgctl(int first, int second, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00001724{
1725 struct msqid_ds dsarg;
1726 int cmd = second&0xff;
blueswir1992f48a2007-10-14 16:27:31 +00001727 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001728 switch( cmd ) {
1729 case IPC_STAT:
1730 case IPC_SET:
1731 target_to_host_msqid_ds(&dsarg,ptr);
1732 ret = get_errno(msgctl(first, cmd, &dsarg));
1733 host_to_target_msqid_ds(ptr,&dsarg);
1734 default:
1735 ret = get_errno(msgctl(first, cmd, &dsarg));
1736 }
1737 return ret;
1738}
1739
1740struct target_msgbuf {
blueswir1992f48a2007-10-14 16:27:31 +00001741 abi_ulong mtype;
ths1bc012f2007-06-03 14:27:49 +00001742 char mtext[1];
1743};
1744
blueswir1992f48a2007-10-14 16:27:31 +00001745static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1746 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001747{
1748 struct target_msgbuf *target_mb;
1749 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00001750 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001751
bellard579a97f2007-11-11 14:26:47 +00001752 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
1753 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001754 host_mb = malloc(msgsz+sizeof(long));
1755 host_mb->mtype = tswapl(target_mb->mtype);
1756 memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1757 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1758 free(host_mb);
1759 unlock_user_struct(target_mb, msgp, 0);
1760
1761 return ret;
1762}
1763
blueswir1992f48a2007-10-14 16:27:31 +00001764static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1765 unsigned int msgsz, int msgtype,
1766 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001767{
1768 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00001769 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00001770 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00001771 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001772
bellard579a97f2007-11-11 14:26:47 +00001773 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
1774 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001775 host_mb = malloc(msgsz+sizeof(long));
1776 ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
bellard579a97f2007-11-11 14:26:47 +00001777 if (ret > 0) {
1778 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
1779 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
1780 if (!target_mtext) {
1781 ret = -TARGET_EFAULT;
1782 goto end;
1783 }
ths1bc012f2007-06-03 14:27:49 +00001784 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00001785 unlock_user(target_mtext, target_mtext_addr, ret);
1786 }
ths1bc012f2007-06-03 14:27:49 +00001787 target_mb->mtype = tswapl(host_mb->mtype);
1788 free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00001789
bellard579a97f2007-11-11 14:26:47 +00001790end:
1791 if (target_mb)
1792 unlock_user_struct(target_mb, msgp, 1);
ths1bc012f2007-06-03 14:27:49 +00001793 return ret;
1794}
1795
pbrook53a59602006-03-25 19:31:22 +00001796/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00001797/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001798static abi_long do_ipc(unsigned int call, int first,
1799 int second, int third,
1800 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00001801{
1802 int version;
blueswir1992f48a2007-10-14 16:27:31 +00001803 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00001804 struct shmid_ds shm_info;
1805 int i;
1806
1807 version = call >> 16;
1808 call &= 0xffff;
1809
1810 switch (call) {
thsfa294812007-02-02 22:05:00 +00001811 case IPCOP_semop:
bellard579a97f2007-11-11 14:26:47 +00001812 ret = get_errno(semop(first,(struct sembuf *)g2h(ptr), second));
thsfa294812007-02-02 22:05:00 +00001813 break;
1814
1815 case IPCOP_semget:
1816 ret = get_errno(semget(first, second, third));
1817 break;
1818
1819 case IPCOP_semctl:
ths3eb6b042007-06-03 14:26:27 +00001820 ret = do_semctl(first, second, third, ptr);
thsfa294812007-02-02 22:05:00 +00001821 break;
1822
1823 case IPCOP_semtimedop:
j_mayer32407102007-09-26 23:01:49 +00001824 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00001825 ret = -TARGET_ENOSYS;
thsfa294812007-02-02 22:05:00 +00001826 break;
thsd96372e2007-02-02 22:05:44 +00001827
1828 case IPCOP_msgget:
1829 ret = get_errno(msgget(first, second));
1830 break;
1831
1832 case IPCOP_msgsnd:
ths1bc012f2007-06-03 14:27:49 +00001833 ret = do_msgsnd(first, ptr, second, third);
thsd96372e2007-02-02 22:05:44 +00001834 break;
1835
1836 case IPCOP_msgctl:
ths1bc012f2007-06-03 14:27:49 +00001837 ret = do_msgctl(first, second, ptr);
thsd96372e2007-02-02 22:05:44 +00001838 break;
1839
1840 case IPCOP_msgrcv:
ths1bc012f2007-06-03 14:27:49 +00001841 {
bellard579a97f2007-11-11 14:26:47 +00001842 /* XXX: this code is not correct */
ths1bc012f2007-06-03 14:27:49 +00001843 struct ipc_kludge
1844 {
1845 void *__unbounded msgp;
1846 long int msgtyp;
1847 };
thsd96372e2007-02-02 22:05:44 +00001848
bellard579a97f2007-11-11 14:26:47 +00001849 struct ipc_kludge *foo = (struct ipc_kludge *)g2h(ptr);
ths1bc012f2007-06-03 14:27:49 +00001850 struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
thsd96372e2007-02-02 22:05:44 +00001851
ths1bc012f2007-06-03 14:27:49 +00001852 ret = do_msgrcv(first, (long)msgp, second, 0, third);
thsd96372e2007-02-02 22:05:44 +00001853
ths1bc012f2007-06-03 14:27:49 +00001854 }
thsd96372e2007-02-02 22:05:44 +00001855 break;
1856
bellard8853f862004-02-22 14:57:26 +00001857 case IPCOP_shmat:
bellard5a4a8982007-11-11 17:39:18 +00001858 {
1859 abi_ulong raddr;
1860 void *host_addr;
1861 /* SHM_* flags are the same on all linux platforms */
1862 host_addr = shmat(first, (void *)g2h(ptr), second);
1863 if (host_addr == (void *)-1) {
1864 ret = get_errno((long)host_addr);
bellard8853f862004-02-22 14:57:26 +00001865 break;
bellard5a4a8982007-11-11 17:39:18 +00001866 }
1867 raddr = h2g((unsigned long)host_addr);
1868 /* find out the length of the shared memory segment */
1869
1870 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
1871 if (is_error(ret)) {
1872 /* can't get length, bail out */
1873 shmdt(host_addr);
1874 break;
1875 }
1876 page_set_flags(raddr, raddr + shm_info.shm_segsz,
1877 PAGE_VALID | PAGE_READ |
1878 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
1879 for (i = 0; i < N_SHM_REGIONS; ++i) {
1880 if (shm_regions[i].start == 0) {
1881 shm_regions[i].start = raddr;
1882 shm_regions[i].size = shm_info.shm_segsz;
1883 break;
1884 }
1885 }
1886 if (put_user(raddr, third, abi_ulong))
1887 return -TARGET_EFAULT;
1888 ret = 0;
1889 }
bellard8853f862004-02-22 14:57:26 +00001890 break;
1891 case IPCOP_shmdt:
1892 for (i = 0; i < N_SHM_REGIONS; ++i) {
1893 if (shm_regions[i].start == ptr) {
1894 shm_regions[i].start = 0;
1895 page_set_flags(ptr, shm_regions[i].size, 0);
1896 break;
1897 }
1898 }
bellard5a4a8982007-11-11 17:39:18 +00001899 ret = get_errno(shmdt((void *)g2h(ptr)));
bellard8853f862004-02-22 14:57:26 +00001900 break;
1901
1902 case IPCOP_shmget:
1903 /* IPC_* flag values are the same on all linux platforms */
1904 ret = get_errno(shmget(first, second, third));
1905 break;
1906
1907 /* IPC_* and SHM_* command values are the same on all linux platforms */
1908 case IPCOP_shmctl:
1909 switch(second) {
1910 case IPC_RMID:
1911 case SHM_LOCK:
1912 case SHM_UNLOCK:
1913 ret = get_errno(shmctl(first, second, NULL));
1914 break;
1915 default:
1916 goto unimplemented;
1917 }
1918 break;
1919 default:
1920 unimplemented:
j_mayer32407102007-09-26 23:01:49 +00001921 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00001922 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00001923 break;
1924 }
1925 return ret;
1926}
j_mayer32407102007-09-26 23:01:49 +00001927#endif
bellard8853f862004-02-22 14:57:26 +00001928
bellard31e31b82003-02-18 22:55:36 +00001929/* kernel structure types definitions */
1930#define IFNAMSIZ 16
1931
1932#define STRUCT(name, list...) STRUCT_ ## name,
1933#define STRUCT_SPECIAL(name) STRUCT_ ## name,
1934enum {
1935#include "syscall_types.h"
1936};
1937#undef STRUCT
1938#undef STRUCT_SPECIAL
1939
1940#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
1941#define STRUCT_SPECIAL(name)
1942#include "syscall_types.h"
1943#undef STRUCT
1944#undef STRUCT_SPECIAL
1945
1946typedef struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00001947 unsigned int target_cmd;
1948 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00001949 const char *name;
1950 int access;
bellard1a9353d2003-03-16 20:28:50 +00001951 const argtype arg_type[5];
bellard31e31b82003-02-18 22:55:36 +00001952} IOCTLEntry;
1953
1954#define IOC_R 0x0001
1955#define IOC_W 0x0002
1956#define IOC_RW (IOC_R | IOC_W)
1957
1958#define MAX_STRUCT_SIZE 4096
1959
bellard2ab83ea2003-06-15 19:56:46 +00001960IOCTLEntry ioctl_entries[] = {
bellard31e31b82003-02-18 22:55:36 +00001961#define IOCTL(cmd, access, types...) \
1962 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
1963#include "ioctls.h"
1964 { 0, 0, },
1965};
1966
pbrook53a59602006-03-25 19:31:22 +00001967/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00001968/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001969static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00001970{
1971 const IOCTLEntry *ie;
1972 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00001973 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00001974 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00001975 int target_size;
1976 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00001977
1978 ie = ioctl_entries;
1979 for(;;) {
1980 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00001981 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00001982 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00001983 }
1984 if (ie->target_cmd == cmd)
1985 break;
1986 ie++;
1987 }
1988 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00001989#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00001990 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00001991#endif
bellard31e31b82003-02-18 22:55:36 +00001992 switch(arg_type[0]) {
1993 case TYPE_NULL:
1994 /* no argument */
1995 ret = get_errno(ioctl(fd, ie->host_cmd));
1996 break;
1997 case TYPE_PTRVOID:
1998 case TYPE_INT:
1999 /* int argment */
2000 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2001 break;
2002 case TYPE_PTR:
2003 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00002004 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00002005 switch(ie->access) {
2006 case IOC_R:
2007 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2008 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002009 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2010 if (!argptr)
2011 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002012 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2013 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00002014 }
2015 break;
2016 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00002017 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2018 if (!argptr)
2019 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002020 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2021 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002022 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2023 break;
2024 default:
2025 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00002026 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2027 if (!argptr)
2028 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002029 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2030 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002031 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2032 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002033 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2034 if (!argptr)
2035 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002036 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2037 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00002038 }
2039 break;
2040 }
2041 break;
2042 default:
j_mayer32407102007-09-26 23:01:49 +00002043 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2044 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00002045 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002046 break;
2047 }
2048 return ret;
2049}
2050
2051bitmask_transtbl iflag_tbl[] = {
2052 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2053 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2054 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2055 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2056 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2057 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2058 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2059 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2060 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2061 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2062 { TARGET_IXON, TARGET_IXON, IXON, IXON },
2063 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2064 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
2065 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
2066 { 0, 0, 0, 0 }
2067};
2068
2069bitmask_transtbl oflag_tbl[] = {
2070 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
2071 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
2072 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
2073 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
2074 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
2075 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
2076 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
2077 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
2078 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
2079 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
2080 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
2081 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
2082 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
2083 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
2084 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
2085 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
2086 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
2087 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
2088 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
2089 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
2090 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
2091 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
2092 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
2093 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
2094 { 0, 0, 0, 0 }
2095};
2096
2097bitmask_transtbl cflag_tbl[] = {
2098 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
2099 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
2100 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
2101 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
2102 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
2103 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
2104 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
2105 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
2106 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
2107 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
2108 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
2109 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
2110 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
2111 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
2112 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
2113 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
2114 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
2115 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
2116 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
2117 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
2118 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
2119 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
2120 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
2121 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
2122 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
2123 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
2124 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
2125 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
2126 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
2127 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
2128 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
2129 { 0, 0, 0, 0 }
2130};
2131
2132bitmask_transtbl lflag_tbl[] = {
2133 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2134 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2135 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2136 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2137 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2138 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2139 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2140 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2141 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2142 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2143 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2144 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2145 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2146 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2147 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
2148 { 0, 0, 0, 0 }
2149};
2150
2151static void target_to_host_termios (void *dst, const void *src)
2152{
2153 struct host_termios *host = dst;
2154 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00002155
ths5fafdf22007-09-16 21:08:06 +00002156 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002157 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002158 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002159 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002160 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002161 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002162 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002163 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2164 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00002165
ths5fafdf22007-09-16 21:08:06 +00002166 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2167 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00002168 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00002169 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00002170 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00002171 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00002172 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00002173 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00002174 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00002175 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2176 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00002177 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2178 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2179 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2180 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2181 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00002182 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00002183}
ths3b46e622007-09-17 08:09:54 +00002184
bellard31e31b82003-02-18 22:55:36 +00002185static void host_to_target_termios (void *dst, const void *src)
2186{
2187 struct target_termios *target = dst;
2188 const struct host_termios *host = src;
2189
ths5fafdf22007-09-16 21:08:06 +00002190 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002191 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002192 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002193 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002194 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002195 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002196 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002197 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2198 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00002199
bellard31e31b82003-02-18 22:55:36 +00002200 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2201 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2202 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2203 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2204 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2205 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2206 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2207 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2208 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2209 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2210 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2211 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2212 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2213 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2214 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2215 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2216 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2217}
2218
2219StructEntry struct_termios_def = {
2220 .convert = { host_to_target_termios, target_to_host_termios },
2221 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2222 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2223};
2224
bellard5286db72003-06-05 00:57:30 +00002225static bitmask_transtbl mmap_flags_tbl[] = {
2226 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2227 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2228 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2229 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2230 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2231 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2232 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2233 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2234 { 0, 0, 0, 0 }
2235};
2236
bellardffa65c32004-01-04 23:57:22 +00002237static bitmask_transtbl fcntl_flags_tbl[] = {
2238 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
2239 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
2240 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
2241 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
2242 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
2243 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
2244 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
2245 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
2246 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
2247 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
2248 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2249 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
2250 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
bellard121061d2004-02-22 11:56:01 +00002251#if defined(O_DIRECT)
bellardffa65c32004-01-04 23:57:22 +00002252 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
bellard121061d2004-02-22 11:56:01 +00002253#endif
bellardffa65c32004-01-04 23:57:22 +00002254 { 0, 0, 0, 0 }
2255};
2256
bellard2ab83ea2003-06-15 19:56:46 +00002257#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00002258
2259/* NOTE: there is really one LDT for all the threads */
2260uint8_t *ldt_table;
2261
bellard03acab62007-11-11 14:57:14 +00002262static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002263{
2264 int size;
pbrook53a59602006-03-25 19:31:22 +00002265 void *p;
bellard6dbad632003-03-16 18:05:05 +00002266
2267 if (!ldt_table)
2268 return 0;
2269 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2270 if (size > bytecount)
2271 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00002272 p = lock_user(VERIFY_WRITE, ptr, size, 0);
2273 if (!p)
bellard03acab62007-11-11 14:57:14 +00002274 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00002275 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00002276 memcpy(p, ldt_table, size);
2277 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00002278 return size;
2279}
2280
2281/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00002282static abi_long write_ldt(CPUX86State *env,
2283 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00002284{
2285 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00002286 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00002287 int seg_32bit, contents, read_exec_only, limit_in_pages;
2288 int seg_not_present, useable;
2289 uint32_t *lp, entry_1, entry_2;
2290
2291 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00002292 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00002293 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00002294 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002295 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2296 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2297 ldt_info.limit = tswap32(target_ldt_info->limit);
2298 ldt_info.flags = tswap32(target_ldt_info->flags);
2299 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00002300
bellard6dbad632003-03-16 18:05:05 +00002301 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00002302 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002303 seg_32bit = ldt_info.flags & 1;
2304 contents = (ldt_info.flags >> 1) & 3;
2305 read_exec_only = (ldt_info.flags >> 3) & 1;
2306 limit_in_pages = (ldt_info.flags >> 4) & 1;
2307 seg_not_present = (ldt_info.flags >> 5) & 1;
2308 useable = (ldt_info.flags >> 6) & 1;
2309
2310 if (contents == 3) {
2311 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00002312 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002313 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00002314 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002315 }
2316 /* allocate the LDT */
2317 if (!ldt_table) {
2318 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2319 if (!ldt_table)
bellard03acab62007-11-11 14:57:14 +00002320 return -TARGET_ENOMEM;
bellard6dbad632003-03-16 18:05:05 +00002321 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellardeeeac3f2007-11-11 19:34:32 +00002322 env->ldt.base = h2g((unsigned long)ldt_table);
bellard6dbad632003-03-16 18:05:05 +00002323 env->ldt.limit = 0xffff;
2324 }
2325
2326 /* NOTE: same code as Linux kernel */
2327 /* Allow LDTs to be cleared by the user. */
2328 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2329 if (oldmode ||
2330 (contents == 0 &&
2331 read_exec_only == 1 &&
2332 seg_32bit == 0 &&
2333 limit_in_pages == 0 &&
2334 seg_not_present == 1 &&
2335 useable == 0 )) {
2336 entry_1 = 0;
2337 entry_2 = 0;
2338 goto install;
2339 }
2340 }
ths3b46e622007-09-17 08:09:54 +00002341
bellard6dbad632003-03-16 18:05:05 +00002342 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2343 (ldt_info.limit & 0x0ffff);
2344 entry_2 = (ldt_info.base_addr & 0xff000000) |
2345 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2346 (ldt_info.limit & 0xf0000) |
2347 ((read_exec_only ^ 1) << 9) |
2348 (contents << 10) |
2349 ((seg_not_present ^ 1) << 15) |
2350 (seg_32bit << 22) |
2351 (limit_in_pages << 23) |
2352 0x7000;
2353 if (!oldmode)
2354 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00002355
bellard6dbad632003-03-16 18:05:05 +00002356 /* Install the new entry ... */
2357install:
2358 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2359 lp[0] = tswap32(entry_1);
2360 lp[1] = tswap32(entry_2);
2361 return 0;
2362}
2363
2364/* specific and weird i386 syscalls */
bellard03acab62007-11-11 14:57:14 +00002365abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
2366 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002367{
bellard03acab62007-11-11 14:57:14 +00002368 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00002369
bellard6dbad632003-03-16 18:05:05 +00002370 switch (func) {
2371 case 0:
2372 ret = read_ldt(ptr, bytecount);
2373 break;
2374 case 1:
2375 ret = write_ldt(env, ptr, bytecount, 1);
2376 break;
2377 case 0x11:
2378 ret = write_ldt(env, ptr, bytecount, 0);
2379 break;
bellard03acab62007-11-11 14:57:14 +00002380 default:
2381 ret = -TARGET_ENOSYS;
2382 break;
bellard6dbad632003-03-16 18:05:05 +00002383 }
2384 return ret;
2385}
bellard1b6b0292003-03-22 17:31:38 +00002386
bellard2ab83ea2003-06-15 19:56:46 +00002387#endif /* defined(TARGET_I386) */
2388
bellard1b6b0292003-03-22 17:31:38 +00002389/* this stack is the equivalent of the kernel stack associated with a
2390 thread/process */
2391#define NEW_STACK_SIZE 8192
2392
2393static int clone_func(void *arg)
2394{
bellard2ab83ea2003-06-15 19:56:46 +00002395 CPUState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00002396 cpu_loop(env);
2397 /* never exits */
2398 return 0;
2399}
2400
ths0da46a62007-10-20 20:23:07 +00002401/* do_fork() Must return host values and target errnos (unlike most
2402 do_*() functions). */
blueswir1992f48a2007-10-14 16:27:31 +00002403int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp)
bellard1b6b0292003-03-22 17:31:38 +00002404{
2405 int ret;
bellard5cd43932003-03-29 16:54:36 +00002406 TaskState *ts;
bellard1b6b0292003-03-22 17:31:38 +00002407 uint8_t *new_stack;
bellard2ab83ea2003-06-15 19:56:46 +00002408 CPUState *new_env;
ths3b46e622007-09-17 08:09:54 +00002409
bellard1b6b0292003-03-22 17:31:38 +00002410 if (flags & CLONE_VM) {
bellard5cd43932003-03-29 16:54:36 +00002411 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2412 memset(ts, 0, sizeof(TaskState));
2413 new_stack = ts->stack;
2414 ts->used = 1;
2415 /* add in task state list */
2416 ts->next = first_task_state;
2417 first_task_state = ts;
bellard1b6b0292003-03-22 17:31:38 +00002418 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00002419 new_env = cpu_copy(env);
bellard2ab83ea2003-06-15 19:56:46 +00002420#if defined(TARGET_I386)
2421 if (!newsp)
2422 newsp = env->regs[R_ESP];
bellard1b6b0292003-03-22 17:31:38 +00002423 new_env->regs[R_ESP] = newsp;
2424 new_env->regs[R_EAX] = 0;
bellard2ab83ea2003-06-15 19:56:46 +00002425#elif defined(TARGET_ARM)
2426 if (!newsp)
2427 newsp = env->regs[13];
2428 new_env->regs[13] = newsp;
2429 new_env->regs[0] = 0;
bellardebc05482003-09-30 21:08:41 +00002430#elif defined(TARGET_SPARC)
bellard48dc41e2006-06-21 18:15:50 +00002431 if (!newsp)
2432 newsp = env->regwptr[22];
2433 new_env->regwptr[22] = newsp;
2434 new_env->regwptr[0] = 0;
2435 /* XXXXX */
bellard67867302003-11-23 17:05:30 +00002436 printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
pbrooke6e59062006-10-22 00:18:54 +00002437#elif defined(TARGET_M68K)
2438 if (!newsp)
2439 newsp = env->aregs[7];
2440 new_env->aregs[7] = newsp;
2441 new_env->dregs[0] = 0;
2442 /* ??? is this sufficient? */
bellard048f6b42005-11-26 18:47:20 +00002443#elif defined(TARGET_MIPS)
ths388bb212007-05-13 13:58:00 +00002444 if (!newsp)
thsead93602007-09-06 00:18:15 +00002445 newsp = env->gpr[29][env->current_tc];
2446 new_env->gpr[29][env->current_tc] = newsp;
bellard67867302003-11-23 17:05:30 +00002447#elif defined(TARGET_PPC)
2448 if (!newsp)
2449 newsp = env->gpr[1];
2450 new_env->gpr[1] = newsp;
ths5fafdf22007-09-16 21:08:06 +00002451 {
bellard67867302003-11-23 17:05:30 +00002452 int i;
2453 for (i = 7; i < 32; i++)
2454 new_env->gpr[i] = 0;
2455 }
bellardfdf9b3e2006-04-27 21:07:38 +00002456#elif defined(TARGET_SH4)
2457 if (!newsp)
2458 newsp = env->gregs[15];
2459 new_env->gregs[15] = newsp;
2460 /* XXXXX */
j_mayer7a3148a2007-04-05 07:13:51 +00002461#elif defined(TARGET_ALPHA)
2462 if (!newsp)
2463 newsp = env->ir[30];
2464 new_env->ir[30] = newsp;
2465 /* ? */
2466 {
2467 int i;
2468 for (i = 7; i < 30; i++)
2469 new_env->ir[i] = 0;
2470 }
ths48733d12007-10-08 13:36:46 +00002471#elif defined(TARGET_CRIS)
2472 if (!newsp)
2473 newsp = env->regs[14];
2474 new_env->regs[14] = newsp;
bellard2ab83ea2003-06-15 19:56:46 +00002475#else
2476#error unsupported target CPU
2477#endif
bellard5cd43932003-03-29 16:54:36 +00002478 new_env->opaque = ts;
bellard27725c12003-04-29 21:08:18 +00002479#ifdef __ia64__
bellardfd4a43e2006-04-24 20:32:17 +00002480 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00002481#else
2482 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2483#endif
bellard1b6b0292003-03-22 17:31:38 +00002484 } else {
2485 /* if no CLONE_VM, we consider it is a fork */
2486 if ((flags & ~CSIGNAL) != 0)
2487 return -EINVAL;
2488 ret = fork();
2489 }
2490 return ret;
2491}
2492
blueswir1992f48a2007-10-14 16:27:31 +00002493static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00002494{
2495 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00002496 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00002497 struct flock64 fl64;
2498 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00002499 abi_long ret;
pbrook53a59602006-03-25 19:31:22 +00002500
bellard7775e9e2003-05-14 22:46:48 +00002501 switch(cmd) {
2502 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00002503 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2504 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00002505 fl.l_type = tswap16(target_fl->l_type);
2506 fl.l_whence = tswap16(target_fl->l_whence);
2507 fl.l_start = tswapl(target_fl->l_start);
2508 fl.l_len = tswapl(target_fl->l_len);
2509 fl.l_pid = tswapl(target_fl->l_pid);
2510 unlock_user_struct(target_fl, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002511 ret = get_errno(fcntl(fd, cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00002512 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00002513 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
2514 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00002515 target_fl->l_type = tswap16(fl.l_type);
2516 target_fl->l_whence = tswap16(fl.l_whence);
2517 target_fl->l_start = tswapl(fl.l_start);
2518 target_fl->l_len = tswapl(fl.l_len);
2519 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00002520 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00002521 }
2522 break;
ths3b46e622007-09-17 08:09:54 +00002523
bellard7775e9e2003-05-14 22:46:48 +00002524 case TARGET_F_SETLK:
2525 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00002526 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2527 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00002528 fl.l_type = tswap16(target_fl->l_type);
2529 fl.l_whence = tswap16(target_fl->l_whence);
2530 fl.l_start = tswapl(target_fl->l_start);
2531 fl.l_len = tswapl(target_fl->l_len);
2532 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00002533 unlock_user_struct(target_fl, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002534 ret = get_errno(fcntl(fd, cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00002535 break;
ths3b46e622007-09-17 08:09:54 +00002536
bellard7775e9e2003-05-14 22:46:48 +00002537 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00002538 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2539 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00002540 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2541 fl64.l_whence = tswap16(target_fl64->l_whence);
2542 fl64.l_start = tswapl(target_fl64->l_start);
2543 fl64.l_len = tswapl(target_fl64->l_len);
2544 fl64.l_pid = tswap16(target_fl64->l_pid);
2545 unlock_user_struct(target_fl64, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002546 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
ths43f238d2007-01-05 20:55:49 +00002547 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00002548 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
2549 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00002550 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2551 target_fl64->l_whence = tswap16(fl64.l_whence);
2552 target_fl64->l_start = tswapl(fl64.l_start);
2553 target_fl64->l_len = tswapl(fl64.l_len);
2554 target_fl64->l_pid = tswapl(fl64.l_pid);
2555 unlock_user_struct(target_fl64, arg, 1);
2556 }
bellard9ee1fa22007-11-11 15:11:19 +00002557 break;
bellard7775e9e2003-05-14 22:46:48 +00002558 case TARGET_F_SETLK64:
2559 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00002560 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2561 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00002562 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2563 fl64.l_whence = tswap16(target_fl64->l_whence);
2564 fl64.l_start = tswapl(target_fl64->l_start);
2565 fl64.l_len = tswapl(target_fl64->l_len);
2566 fl64.l_pid = tswap16(target_fl64->l_pid);
2567 unlock_user_struct(target_fl64, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002568 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00002569 break;
2570
bellardffa65c32004-01-04 23:57:22 +00002571 case F_GETFL:
bellard9ee1fa22007-11-11 15:11:19 +00002572 ret = get_errno(fcntl(fd, cmd, arg));
2573 if (ret >= 0) {
2574 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2575 }
bellardffa65c32004-01-04 23:57:22 +00002576 break;
2577
2578 case F_SETFL:
bellard9ee1fa22007-11-11 15:11:19 +00002579 ret = get_errno(fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
bellardffa65c32004-01-04 23:57:22 +00002580 break;
2581
bellard7775e9e2003-05-14 22:46:48 +00002582 default:
bellard9ee1fa22007-11-11 15:11:19 +00002583 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00002584 break;
2585 }
2586 return ret;
2587}
2588
bellard67867302003-11-23 17:05:30 +00002589#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00002590
bellard67867302003-11-23 17:05:30 +00002591static inline int high2lowuid(int uid)
2592{
2593 if (uid > 65535)
2594 return 65534;
2595 else
2596 return uid;
2597}
2598
2599static inline int high2lowgid(int gid)
2600{
2601 if (gid > 65535)
2602 return 65534;
2603 else
2604 return gid;
2605}
2606
2607static inline int low2highuid(int uid)
2608{
2609 if ((int16_t)uid == -1)
2610 return -1;
2611 else
2612 return uid;
2613}
2614
2615static inline int low2highgid(int gid)
2616{
2617 if ((int16_t)gid == -1)
2618 return -1;
2619 else
2620 return gid;
2621}
2622
2623#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00002624
bellard31e31b82003-02-18 22:55:36 +00002625void syscall_init(void)
2626{
bellard2ab83ea2003-06-15 19:56:46 +00002627 IOCTLEntry *ie;
2628 const argtype *arg_type;
2629 int size;
thsb92c47c2007-11-01 00:07:38 +00002630 int i;
bellard2ab83ea2003-06-15 19:56:46 +00002631
ths5fafdf22007-09-16 21:08:06 +00002632#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2633#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00002634#include "syscall_types.h"
2635#undef STRUCT
2636#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00002637
2638 /* we patch the ioctl size if necessary. We rely on the fact that
2639 no ioctl has all the bits at '1' in the size field */
2640 ie = ioctl_entries;
2641 while (ie->target_cmd != 0) {
2642 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2643 TARGET_IOC_SIZEMASK) {
2644 arg_type = ie->arg_type;
2645 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00002646 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002647 ie->target_cmd);
2648 exit(1);
2649 }
2650 arg_type++;
2651 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00002652 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00002653 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2654 (size << TARGET_IOC_SIZESHIFT);
2655 }
thsb92c47c2007-11-01 00:07:38 +00002656
2657 /* Build target_to_host_errno_table[] table from
2658 * host_to_target_errno_table[]. */
2659 for (i=0; i < ERRNO_TABLE_SIZE; i++)
2660 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
2661
bellard2ab83ea2003-06-15 19:56:46 +00002662 /* automatic consistency check if same arch */
2663#if defined(__i386__) && defined(TARGET_I386)
2664 if (ie->target_cmd != ie->host_cmd) {
ths5fafdf22007-09-16 21:08:06 +00002665 fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002666 ie->target_cmd, ie->host_cmd);
2667 }
2668#endif
2669 ie++;
2670 }
bellard31e31b82003-02-18 22:55:36 +00002671}
bellardc573ff62004-01-04 15:51:36 +00002672
blueswir1992f48a2007-10-14 16:27:31 +00002673#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00002674static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2675{
2676#ifdef TARGET_WORDS_BIG_ENDIAN
2677 return ((uint64_t)word0 << 32) | word1;
2678#else
2679 return ((uint64_t)word1 << 32) | word0;
2680#endif
2681}
blueswir1992f48a2007-10-14 16:27:31 +00002682#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00002683static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2684{
2685 return word0;
2686}
blueswir1992f48a2007-10-14 16:27:31 +00002687#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00002688
2689#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00002690static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
2691 abi_long arg2,
2692 abi_long arg3,
2693 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00002694{
2695#ifdef TARGET_ARM
2696 if (((CPUARMState *)cpu_env)->eabi)
2697 {
2698 arg2 = arg3;
2699 arg3 = arg4;
2700 }
2701#endif
2702 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
2703}
2704#endif
2705
2706#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00002707static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
2708 abi_long arg2,
2709 abi_long arg3,
2710 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00002711{
2712#ifdef TARGET_ARM
2713 if (((CPUARMState *)cpu_env)->eabi)
2714 {
2715 arg2 = arg3;
2716 arg3 = arg4;
2717 }
2718#endif
2719 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
2720}
2721#endif
2722
bellard579a97f2007-11-11 14:26:47 +00002723static inline abi_long target_to_host_timespec(struct timespec *host_ts,
2724 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00002725{
2726 struct target_timespec *target_ts;
2727
bellard579a97f2007-11-11 14:26:47 +00002728 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
2729 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002730 host_ts->tv_sec = tswapl(target_ts->tv_sec);
2731 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
2732 unlock_user_struct(target_ts, target_addr, 0);
2733}
2734
bellard579a97f2007-11-11 14:26:47 +00002735static inline abi_long host_to_target_timespec(abi_ulong target_addr,
2736 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00002737{
2738 struct target_timespec *target_ts;
2739
bellard579a97f2007-11-11 14:26:47 +00002740 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
2741 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002742 target_ts->tv_sec = tswapl(host_ts->tv_sec);
2743 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
2744 unlock_user_struct(target_ts, target_addr, 1);
2745}
2746
ths0da46a62007-10-20 20:23:07 +00002747/* do_syscall() should always have a single exit point at the end so
2748 that actions, such as logging of syscall results, can be performed.
2749 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00002750abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
2751 abi_long arg2, abi_long arg3, abi_long arg4,
2752 abi_long arg5, abi_long arg6)
bellard31e31b82003-02-18 22:55:36 +00002753{
blueswir1992f48a2007-10-14 16:27:31 +00002754 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00002755 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00002756 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00002757 void *p;
ths3b46e622007-09-17 08:09:54 +00002758
bellard72f03902003-02-18 23:33:18 +00002759#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00002760 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00002761#endif
thsb92c47c2007-11-01 00:07:38 +00002762 if(do_strace)
2763 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
2764
bellard31e31b82003-02-18 22:55:36 +00002765 switch(num) {
2766 case TARGET_NR_exit:
bellard7d132992003-03-06 23:23:54 +00002767#ifdef HAVE_GPROF
2768 _mcleanup();
2769#endif
bellarde9009672005-04-26 20:42:36 +00002770 gdb_exit(cpu_env, arg1);
bellard1b6b0292003-03-22 17:31:38 +00002771 /* XXX: should free thread stack and CPU env */
bellard31e31b82003-02-18 22:55:36 +00002772 _exit(arg1);
2773 ret = 0; /* avoid warning */
2774 break;
2775 case TARGET_NR_read:
pbrook53a59602006-03-25 19:31:22 +00002776 page_unprotect_range(arg2, arg3);
bellard579a97f2007-11-11 14:26:47 +00002777 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
2778 goto efault;
pbrook53a59602006-03-25 19:31:22 +00002779 ret = get_errno(read(arg1, p, arg3));
2780 unlock_user(p, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00002781 break;
2782 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00002783 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
2784 goto efault;
pbrook53a59602006-03-25 19:31:22 +00002785 ret = get_errno(write(arg1, p, arg3));
2786 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00002787 break;
2788 case TARGET_NR_open:
bellard579a97f2007-11-11 14:26:47 +00002789 if (!(p = lock_user_string(arg1))) {
2790 return -TARGET_EFAULT;
2791 goto fail;
2792 }
pbrook53a59602006-03-25 19:31:22 +00002793 ret = get_errno(open(path(p),
bellardffa65c32004-01-04 23:57:22 +00002794 target_to_host_bitmask(arg2, fcntl_flags_tbl),
2795 arg3));
pbrook53a59602006-03-25 19:31:22 +00002796 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002797 break;
ths82424832007-09-24 09:21:55 +00002798#if defined(TARGET_NR_openat) && defined(__NR_openat)
2799 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00002800 if (!(p = lock_user_string(arg2)))
2801 goto efault;
2802 ret = get_errno(sys_openat(arg1,
2803 path(p),
2804 target_to_host_bitmask(arg3, fcntl_flags_tbl),
2805 arg4));
2806 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00002807 break;
2808#endif
bellard31e31b82003-02-18 22:55:36 +00002809 case TARGET_NR_close:
2810 ret = get_errno(close(arg1));
2811 break;
2812 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00002813 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00002814 break;
2815 case TARGET_NR_fork:
bellard1b6b0292003-03-22 17:31:38 +00002816 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00002817 break;
thse5febef2007-04-01 18:31:35 +00002818#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00002819 case TARGET_NR_waitpid:
2820 {
pbrook53a59602006-03-25 19:31:22 +00002821 int status;
2822 ret = get_errno(waitpid(arg1, &status, arg3));
2823 if (!is_error(ret) && arg2)
2824 tput32(arg2, status);
bellard31e31b82003-02-18 22:55:36 +00002825 }
2826 break;
thse5febef2007-04-01 18:31:35 +00002827#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002828#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002829 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00002830 if (!(p = lock_user_string(arg1)))
2831 goto efault;
pbrook53a59602006-03-25 19:31:22 +00002832 ret = get_errno(creat(p, arg2));
2833 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002834 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002835#endif
bellard31e31b82003-02-18 22:55:36 +00002836 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00002837 {
2838 void * p2;
2839 p = lock_user_string(arg1);
2840 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00002841 if (!p || !p2)
2842 ret = -TARGET_EFAULT;
2843 else
2844 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00002845 unlock_user(p2, arg2, 0);
2846 unlock_user(p, arg1, 0);
2847 }
bellard31e31b82003-02-18 22:55:36 +00002848 break;
ths64f0ce42007-09-24 09:25:06 +00002849#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
2850 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00002851 {
2852 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00002853 if (!arg2 || !arg4)
2854 goto efault;
ths64f0ce42007-09-24 09:25:06 +00002855 p = lock_user_string(arg2);
2856 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00002857 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00002858 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00002859 else
2860 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00002861 unlock_user(p, arg2, 0);
2862 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00002863 }
2864 break;
2865#endif
bellard31e31b82003-02-18 22:55:36 +00002866 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00002867 if (!(p = lock_user_string(arg1)))
2868 goto efault;
pbrook53a59602006-03-25 19:31:22 +00002869 ret = get_errno(unlink(p));
2870 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002871 break;
ths8170f562007-09-24 09:24:11 +00002872#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
2873 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00002874 if (!(p = lock_user_string(arg2)))
2875 goto efault;
2876 ret = get_errno(sys_unlinkat(arg1, p, arg3));
2877 unlock_user(p, arg2, 0);
ths8170f562007-09-24 09:24:11 +00002878#endif
bellard31e31b82003-02-18 22:55:36 +00002879 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00002880 {
2881 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00002882 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00002883 abi_ulong gp;
2884 abi_ulong guest_argp;
2885 abi_ulong guest_envp;
2886 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00002887 char **q;
2888
bellardf7341ff2003-03-30 21:00:25 +00002889 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00002890 guest_argp = arg2;
2891 for (gp = guest_argp; tgetl(gp); gp++)
bellard7854b052003-03-29 17:22:23 +00002892 argc++;
bellardf7341ff2003-03-30 21:00:25 +00002893 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00002894 guest_envp = arg3;
2895 for (gp = guest_envp; tgetl(gp); gp++)
bellard7854b052003-03-29 17:22:23 +00002896 envc++;
2897
bellardf7341ff2003-03-30 21:00:25 +00002898 argp = alloca((argc + 1) * sizeof(void *));
2899 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00002900
pbrook53a59602006-03-25 19:31:22 +00002901 for (gp = guest_argp, q = argp; ;
blueswir1992f48a2007-10-14 16:27:31 +00002902 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002903 addr = tgetl(gp);
2904 if (!addr)
2905 break;
bellard579a97f2007-11-11 14:26:47 +00002906 if (!(*q = lock_user_string(addr))) {
2907 ret = -TARGET_EFAULT;
2908 goto execve_fail;
2909 }
pbrook53a59602006-03-25 19:31:22 +00002910 }
bellardf7341ff2003-03-30 21:00:25 +00002911 *q = NULL;
2912
pbrook53a59602006-03-25 19:31:22 +00002913 for (gp = guest_envp, q = envp; ;
blueswir1992f48a2007-10-14 16:27:31 +00002914 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002915 addr = tgetl(gp);
2916 if (!addr)
2917 break;
bellard579a97f2007-11-11 14:26:47 +00002918 if (!(*q = lock_user_string(addr))) {
2919 ret = -TARGET_EFAULT;
2920 goto execve_fail;
2921 }
pbrook53a59602006-03-25 19:31:22 +00002922 }
bellardf7341ff2003-03-30 21:00:25 +00002923 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00002924
bellard579a97f2007-11-11 14:26:47 +00002925 if (!(p = lock_user_string(arg1))) {
2926 ret = -TARGET_EFAULT;
2927 goto execve_fail;
2928 }
pbrook53a59602006-03-25 19:31:22 +00002929 ret = get_errno(execve(p, argp, envp));
2930 unlock_user(p, arg1, 0);
2931
bellard579a97f2007-11-11 14:26:47 +00002932 execve_fail:
pbrook53a59602006-03-25 19:31:22 +00002933 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00002934 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002935 addr = tgetl(gp);
2936 unlock_user(*q, addr, 0);
2937 }
2938 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00002939 gp += sizeof(abi_ulong), q++) {
pbrook53a59602006-03-25 19:31:22 +00002940 addr = tgetl(gp);
2941 unlock_user(*q, addr, 0);
2942 }
bellard7854b052003-03-29 17:22:23 +00002943 }
bellard31e31b82003-02-18 22:55:36 +00002944 break;
2945 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00002946 if (!(p = lock_user_string(arg1)))
2947 goto efault;
pbrook53a59602006-03-25 19:31:22 +00002948 ret = get_errno(chdir(p));
2949 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002950 break;
bellarda315a142005-01-30 22:59:18 +00002951#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00002952 case TARGET_NR_time:
2953 {
pbrook53a59602006-03-25 19:31:22 +00002954 time_t host_time;
2955 ret = get_errno(time(&host_time));
2956 if (!is_error(ret) && arg1)
2957 tputl(arg1, host_time);
bellard31e31b82003-02-18 22:55:36 +00002958 }
2959 break;
bellarda315a142005-01-30 22:59:18 +00002960#endif
bellard31e31b82003-02-18 22:55:36 +00002961 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00002962 if (!(p = lock_user_string(arg1)))
2963 goto efault;
pbrook53a59602006-03-25 19:31:22 +00002964 ret = get_errno(mknod(p, arg2, arg3));
2965 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002966 break;
ths75ac37a2007-09-24 09:23:05 +00002967#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
2968 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00002969 if (!(p = lock_user_string(arg2)))
2970 goto efault;
2971 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
2972 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00002973 break;
2974#endif
bellard31e31b82003-02-18 22:55:36 +00002975 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00002976 if (!(p = lock_user_string(arg1)))
2977 goto efault;
pbrook53a59602006-03-25 19:31:22 +00002978 ret = get_errno(chmod(p, arg2));
2979 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002980 break;
bellardebc05482003-09-30 21:08:41 +00002981#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00002982 case TARGET_NR_break:
2983 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002984#endif
2985#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00002986 case TARGET_NR_oldstat:
2987 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002988#endif
bellard31e31b82003-02-18 22:55:36 +00002989 case TARGET_NR_lseek:
2990 ret = get_errno(lseek(arg1, arg2, arg3));
2991 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002992#ifdef TARGET_NR_getxpid
2993 case TARGET_NR_getxpid:
2994#else
bellard31e31b82003-02-18 22:55:36 +00002995 case TARGET_NR_getpid:
j_mayer7a3148a2007-04-05 07:13:51 +00002996#endif
bellard31e31b82003-02-18 22:55:36 +00002997 ret = get_errno(getpid());
2998 break;
2999 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00003000 {
3001 /* need to look at the data field */
3002 void *p2, *p3;
3003 p = lock_user_string(arg1);
3004 p2 = lock_user_string(arg2);
3005 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00003006 if (!p || !p2 || !p3)
3007 ret = -TARGET_EFAULT;
3008 else
3009 /* FIXME - arg5 should be locked, but it isn't clear how to
3010 * do that since it's not guaranteed to be a NULL-terminated
3011 * string.
3012 */
3013 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
3014 unlock_user(p, arg1, 0);
3015 unlock_user(p2, arg2, 0);
3016 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00003017 break;
3018 }
thse5febef2007-04-01 18:31:35 +00003019#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00003020 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00003021 if (!(p = lock_user_string(arg1)))
3022 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003023 ret = get_errno(umount(p));
3024 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003025 break;
thse5febef2007-04-01 18:31:35 +00003026#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003027#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003028 case TARGET_NR_stime:
3029 {
pbrook53a59602006-03-25 19:31:22 +00003030 time_t host_time;
3031 host_time = tgetl(arg1);
3032 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00003033 }
3034 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003035#endif
bellard31e31b82003-02-18 22:55:36 +00003036 case TARGET_NR_ptrace:
3037 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00003038#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003039 case TARGET_NR_alarm:
3040 ret = alarm(arg1);
3041 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003042#endif
bellardebc05482003-09-30 21:08:41 +00003043#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00003044 case TARGET_NR_oldfstat:
3045 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003046#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003047#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003048 case TARGET_NR_pause:
3049 ret = get_errno(pause());
3050 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003051#endif
thse5febef2007-04-01 18:31:35 +00003052#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00003053 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00003054 {
pbrook53a59602006-03-25 19:31:22 +00003055 struct utimbuf tbuf, *host_tbuf;
3056 struct target_utimbuf *target_tbuf;
3057 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00003058 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
3059 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003060 tbuf.actime = tswapl(target_tbuf->actime);
3061 tbuf.modtime = tswapl(target_tbuf->modtime);
3062 unlock_user_struct(target_tbuf, arg2, 0);
3063 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00003064 } else {
pbrook53a59602006-03-25 19:31:22 +00003065 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00003066 }
bellard579a97f2007-11-11 14:26:47 +00003067 if (!(p = lock_user_string(arg1)))
3068 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003069 ret = get_errno(utime(p, host_tbuf));
3070 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00003071 }
3072 break;
thse5febef2007-04-01 18:31:35 +00003073#endif
bellard978a66f2004-12-06 22:58:05 +00003074 case TARGET_NR_utimes:
3075 {
bellard978a66f2004-12-06 22:58:05 +00003076 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00003077 if (arg2) {
3078 target_to_host_timeval(&tv[0], arg2);
3079 target_to_host_timeval(&tv[1],
3080 arg2 + sizeof (struct target_timeval));
bellard978a66f2004-12-06 22:58:05 +00003081 tvp = tv;
3082 } else {
3083 tvp = NULL;
3084 }
bellard579a97f2007-11-11 14:26:47 +00003085 if (!(p = lock_user_string(arg1)))
3086 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003087 ret = get_errno(utimes(p, tvp));
3088 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00003089 }
3090 break;
bellardebc05482003-09-30 21:08:41 +00003091#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00003092 case TARGET_NR_stty:
3093 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003094#endif
3095#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00003096 case TARGET_NR_gtty:
3097 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003098#endif
bellard31e31b82003-02-18 22:55:36 +00003099 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00003100 if (!(p = lock_user_string(arg1)))
3101 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003102 ret = get_errno(access(p, arg2));
3103 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003104 break;
ths92a34c12007-09-24 09:27:49 +00003105#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
3106 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00003107 if (!(p = lock_user_string(arg2)))
3108 goto efault;
3109 ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
3110 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00003111 break;
3112#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003113#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003114 case TARGET_NR_nice:
3115 ret = get_errno(nice(arg1));
3116 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003117#endif
bellardebc05482003-09-30 21:08:41 +00003118#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00003119 case TARGET_NR_ftime:
3120 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003121#endif
bellard31e31b82003-02-18 22:55:36 +00003122 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00003123 sync();
3124 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00003125 break;
3126 case TARGET_NR_kill:
3127 ret = get_errno(kill(arg1, arg2));
3128 break;
3129 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00003130 {
3131 void *p2;
3132 p = lock_user_string(arg1);
3133 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00003134 if (!p || !p2)
3135 ret = -TARGET_EFAULT;
3136 else
3137 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00003138 unlock_user(p2, arg2, 0);
3139 unlock_user(p, arg1, 0);
3140 }
bellard31e31b82003-02-18 22:55:36 +00003141 break;
ths722183f2007-09-24 09:24:37 +00003142#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
3143 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00003144 {
bellard579a97f2007-11-11 14:26:47 +00003145 void *p2;
ths722183f2007-09-24 09:24:37 +00003146 p = lock_user_string(arg2);
3147 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00003148 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00003149 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00003150 else
3151 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00003152 unlock_user(p2, arg4, 0);
3153 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00003154 }
3155 break;
3156#endif
bellard31e31b82003-02-18 22:55:36 +00003157 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00003158 if (!(p = lock_user_string(arg1)))
3159 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003160 ret = get_errno(mkdir(p, arg2));
3161 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003162 break;
ths4472ad02007-09-24 09:22:32 +00003163#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
3164 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00003165 if (!(p = lock_user_string(arg2)))
3166 goto efault;
3167 ret = get_errno(sys_mkdirat(arg1, p, arg3));
3168 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00003169 break;
3170#endif
bellard31e31b82003-02-18 22:55:36 +00003171 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00003172 if (!(p = lock_user_string(arg1)))
3173 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003174 ret = get_errno(rmdir(p));
3175 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003176 break;
3177 case TARGET_NR_dup:
3178 ret = get_errno(dup(arg1));
3179 break;
3180 case TARGET_NR_pipe:
3181 {
pbrook53a59602006-03-25 19:31:22 +00003182 int host_pipe[2];
3183 ret = get_errno(pipe(host_pipe));
bellard31e31b82003-02-18 22:55:36 +00003184 if (!is_error(ret)) {
thsc12ab052007-06-01 11:50:36 +00003185#if defined(TARGET_MIPS)
thsead93602007-09-06 00:18:15 +00003186 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
3187 env->gpr[3][env->current_tc] = host_pipe[1];
thsc12ab052007-06-01 11:50:36 +00003188 ret = host_pipe[0];
3189#else
pbrook53a59602006-03-25 19:31:22 +00003190 tput32(arg1, host_pipe[0]);
3191 tput32(arg1 + 4, host_pipe[1]);
thsc12ab052007-06-01 11:50:36 +00003192#endif
bellard31e31b82003-02-18 22:55:36 +00003193 }
3194 }
3195 break;
3196 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00003197 {
pbrook53a59602006-03-25 19:31:22 +00003198 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00003199 struct tms tms;
3200 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00003201 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00003202 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
3203 if (!tmsp)
3204 goto efault;
bellardc596ed12003-07-13 17:32:31 +00003205 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3206 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3207 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3208 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00003209 }
bellardc596ed12003-07-13 17:32:31 +00003210 if (!is_error(ret))
3211 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00003212 }
3213 break;
bellardebc05482003-09-30 21:08:41 +00003214#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00003215 case TARGET_NR_prof:
3216 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003217#endif
thse5febef2007-04-01 18:31:35 +00003218#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00003219 case TARGET_NR_signal:
3220 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003221#endif
bellard31e31b82003-02-18 22:55:36 +00003222 case TARGET_NR_acct:
bellard579a97f2007-11-11 14:26:47 +00003223 if (!(p = lock_user_string(arg1)))
3224 goto efault;
pbrook24836682006-04-16 14:14:53 +00003225 ret = get_errno(acct(path(p)));
3226 unlock_user(p, arg1, 0);
3227 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003228#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003229 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00003230 if (!(p = lock_user_string(arg1)))
3231 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003232 ret = get_errno(umount2(p, arg2));
3233 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003234 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003235#endif
bellardebc05482003-09-30 21:08:41 +00003236#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00003237 case TARGET_NR_lock:
3238 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003239#endif
bellard31e31b82003-02-18 22:55:36 +00003240 case TARGET_NR_ioctl:
3241 ret = do_ioctl(arg1, arg2, arg3);
3242 break;
3243 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00003244 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00003245 break;
bellardebc05482003-09-30 21:08:41 +00003246#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00003247 case TARGET_NR_mpx:
3248 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003249#endif
bellard31e31b82003-02-18 22:55:36 +00003250 case TARGET_NR_setpgid:
3251 ret = get_errno(setpgid(arg1, arg2));
3252 break;
bellardebc05482003-09-30 21:08:41 +00003253#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00003254 case TARGET_NR_ulimit:
3255 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003256#endif
3257#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00003258 case TARGET_NR_oldolduname:
3259 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003260#endif
bellard31e31b82003-02-18 22:55:36 +00003261 case TARGET_NR_umask:
3262 ret = get_errno(umask(arg1));
3263 break;
3264 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00003265 if (!(p = lock_user_string(arg1)))
3266 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003267 ret = get_errno(chroot(p));
3268 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003269 break;
3270 case TARGET_NR_ustat:
3271 goto unimplemented;
3272 case TARGET_NR_dup2:
3273 ret = get_errno(dup2(arg1, arg2));
3274 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003275#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003276 case TARGET_NR_getppid:
3277 ret = get_errno(getppid());
3278 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003279#endif
bellard31e31b82003-02-18 22:55:36 +00003280 case TARGET_NR_getpgrp:
3281 ret = get_errno(getpgrp());
3282 break;
3283 case TARGET_NR_setsid:
3284 ret = get_errno(setsid());
3285 break;
thse5febef2007-04-01 18:31:35 +00003286#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00003287 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00003288 {
ths388bb212007-05-13 13:58:00 +00003289#if !defined(TARGET_MIPS)
pbrook53a59602006-03-25 19:31:22 +00003290 struct target_old_sigaction *old_act;
bellard66fb9762003-03-23 01:06:05 +00003291 struct target_sigaction act, oact, *pact;
pbrook53a59602006-03-25 19:31:22 +00003292 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00003293 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3294 goto efault;
bellard66fb9762003-03-23 01:06:05 +00003295 act._sa_handler = old_act->_sa_handler;
3296 target_siginitset(&act.sa_mask, old_act->sa_mask);
3297 act.sa_flags = old_act->sa_flags;
3298 act.sa_restorer = old_act->sa_restorer;
pbrook53a59602006-03-25 19:31:22 +00003299 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003300 pact = &act;
3301 } else {
3302 pact = NULL;
3303 }
3304 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00003305 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003306 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3307 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003308 old_act->_sa_handler = oact._sa_handler;
3309 old_act->sa_mask = oact.sa_mask.sig[0];
3310 old_act->sa_flags = oact.sa_flags;
3311 old_act->sa_restorer = oact.sa_restorer;
3312 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00003313 }
ths388bb212007-05-13 13:58:00 +00003314#else
bellard106ec872006-06-27 21:08:10 +00003315 struct target_sigaction act, oact, *pact, *old_act;
3316
3317 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00003318 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3319 goto efault;
bellard106ec872006-06-27 21:08:10 +00003320 act._sa_handler = old_act->_sa_handler;
3321 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3322 act.sa_flags = old_act->sa_flags;
3323 unlock_user_struct(old_act, arg2, 0);
3324 pact = &act;
3325 } else {
3326 pact = NULL;
3327 }
3328
3329 ret = get_errno(do_sigaction(arg1, pact, &oact));
3330
3331 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003332 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3333 goto efault;
bellard106ec872006-06-27 21:08:10 +00003334 old_act->_sa_handler = oact._sa_handler;
3335 old_act->sa_flags = oact.sa_flags;
3336 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3337 old_act->sa_mask.sig[1] = 0;
3338 old_act->sa_mask.sig[2] = 0;
3339 old_act->sa_mask.sig[3] = 0;
3340 unlock_user_struct(old_act, arg3, 1);
3341 }
ths388bb212007-05-13 13:58:00 +00003342#endif
bellard31e31b82003-02-18 22:55:36 +00003343 }
3344 break;
thse5febef2007-04-01 18:31:35 +00003345#endif
bellard66fb9762003-03-23 01:06:05 +00003346 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00003347 {
3348 struct target_sigaction *act;
3349 struct target_sigaction *oact;
3350
bellard579a97f2007-11-11 14:26:47 +00003351 if (arg2) {
3352 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
3353 goto efault;
3354 } else
pbrook53a59602006-03-25 19:31:22 +00003355 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00003356 if (arg3) {
3357 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
3358 ret = -TARGET_EFAULT;
3359 goto rt_sigaction_fail;
3360 }
3361 } else
pbrook53a59602006-03-25 19:31:22 +00003362 oact = NULL;
3363 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00003364 rt_sigaction_fail:
3365 if (act)
pbrook53a59602006-03-25 19:31:22 +00003366 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00003367 if (oact)
pbrook53a59602006-03-25 19:31:22 +00003368 unlock_user_struct(oact, arg3, 1);
3369 }
bellard66fb9762003-03-23 01:06:05 +00003370 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003371#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003372 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00003373 {
3374 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00003375 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00003376 sigprocmask(0, NULL, &cur_set);
3377 host_to_target_old_sigset(&target_set, &cur_set);
3378 ret = target_set;
3379 }
3380 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003381#endif
3382#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003383 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00003384 {
3385 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00003386 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00003387 sigprocmask(0, NULL, &cur_set);
3388 target_to_host_old_sigset(&set, &target_set);
3389 sigorset(&set, &set, &cur_set);
3390 sigprocmask(SIG_SETMASK, &set, &oset);
3391 host_to_target_old_sigset(&target_set, &oset);
3392 ret = target_set;
3393 }
3394 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003395#endif
thse5febef2007-04-01 18:31:35 +00003396#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00003397 case TARGET_NR_sigprocmask:
3398 {
3399 int how = arg1;
3400 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003401
pbrook53a59602006-03-25 19:31:22 +00003402 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003403 switch(how) {
3404 case TARGET_SIG_BLOCK:
3405 how = SIG_BLOCK;
3406 break;
3407 case TARGET_SIG_UNBLOCK:
3408 how = SIG_UNBLOCK;
3409 break;
3410 case TARGET_SIG_SETMASK:
3411 how = SIG_SETMASK;
3412 break;
3413 default:
ths0da46a62007-10-20 20:23:07 +00003414 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00003415 goto fail;
3416 }
bellard579a97f2007-11-11 14:26:47 +00003417 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3418 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003419 target_to_host_old_sigset(&set, p);
3420 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003421 set_ptr = &set;
3422 } else {
3423 how = 0;
3424 set_ptr = NULL;
3425 }
3426 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003427 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003428 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3429 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003430 host_to_target_old_sigset(p, &oldset);
3431 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003432 }
3433 }
3434 break;
thse5febef2007-04-01 18:31:35 +00003435#endif
bellard66fb9762003-03-23 01:06:05 +00003436 case TARGET_NR_rt_sigprocmask:
3437 {
3438 int how = arg1;
3439 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003440
pbrook53a59602006-03-25 19:31:22 +00003441 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003442 switch(how) {
3443 case TARGET_SIG_BLOCK:
3444 how = SIG_BLOCK;
3445 break;
3446 case TARGET_SIG_UNBLOCK:
3447 how = SIG_UNBLOCK;
3448 break;
3449 case TARGET_SIG_SETMASK:
3450 how = SIG_SETMASK;
3451 break;
3452 default:
ths0da46a62007-10-20 20:23:07 +00003453 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00003454 goto fail;
3455 }
bellard579a97f2007-11-11 14:26:47 +00003456 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3457 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003458 target_to_host_sigset(&set, p);
3459 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003460 set_ptr = &set;
3461 } else {
3462 how = 0;
3463 set_ptr = NULL;
3464 }
3465 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003466 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003467 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3468 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003469 host_to_target_sigset(p, &oldset);
3470 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003471 }
3472 }
3473 break;
thse5febef2007-04-01 18:31:35 +00003474#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00003475 case TARGET_NR_sigpending:
3476 {
3477 sigset_t set;
3478 ret = get_errno(sigpending(&set));
3479 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003480 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3481 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003482 host_to_target_old_sigset(p, &set);
3483 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003484 }
3485 }
3486 break;
thse5febef2007-04-01 18:31:35 +00003487#endif
bellard66fb9762003-03-23 01:06:05 +00003488 case TARGET_NR_rt_sigpending:
3489 {
3490 sigset_t set;
3491 ret = get_errno(sigpending(&set));
3492 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003493 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3494 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003495 host_to_target_sigset(p, &set);
3496 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003497 }
3498 }
3499 break;
thse5febef2007-04-01 18:31:35 +00003500#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00003501 case TARGET_NR_sigsuspend:
3502 {
3503 sigset_t set;
bellard579a97f2007-11-11 14:26:47 +00003504 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3505 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003506 target_to_host_old_sigset(&set, p);
3507 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003508 ret = get_errno(sigsuspend(&set));
3509 }
3510 break;
thse5febef2007-04-01 18:31:35 +00003511#endif
bellard66fb9762003-03-23 01:06:05 +00003512 case TARGET_NR_rt_sigsuspend:
3513 {
3514 sigset_t set;
bellard579a97f2007-11-11 14:26:47 +00003515 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3516 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003517 target_to_host_sigset(&set, p);
3518 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003519 ret = get_errno(sigsuspend(&set));
3520 }
3521 break;
3522 case TARGET_NR_rt_sigtimedwait:
3523 {
bellard66fb9762003-03-23 01:06:05 +00003524 sigset_t set;
3525 struct timespec uts, *puts;
3526 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00003527
bellard579a97f2007-11-11 14:26:47 +00003528 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3529 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003530 target_to_host_sigset(&set, p);
3531 unlock_user(p, arg1, 0);
3532 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00003533 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00003534 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00003535 } else {
3536 puts = NULL;
3537 }
3538 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00003539 if (!is_error(ret) && arg2) {
bellard579a97f2007-11-11 14:26:47 +00003540 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_sigset_t), 0)))
3541 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003542 host_to_target_siginfo(p, &uinfo);
3543 unlock_user(p, arg2, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003544 }
3545 }
3546 break;
3547 case TARGET_NR_rt_sigqueueinfo:
3548 {
3549 siginfo_t uinfo;
bellard579a97f2007-11-11 14:26:47 +00003550 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
3551 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003552 target_to_host_siginfo(&uinfo, p);
3553 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003554 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3555 }
3556 break;
thse5febef2007-04-01 18:31:35 +00003557#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00003558 case TARGET_NR_sigreturn:
3559 /* NOTE: ret is eax, so not transcoding must be done */
3560 ret = do_sigreturn(cpu_env);
3561 break;
thse5febef2007-04-01 18:31:35 +00003562#endif
bellard66fb9762003-03-23 01:06:05 +00003563 case TARGET_NR_rt_sigreturn:
3564 /* NOTE: ret is eax, so not transcoding must be done */
3565 ret = do_rt_sigreturn(cpu_env);
3566 break;
bellard31e31b82003-02-18 22:55:36 +00003567 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00003568 if (!(p = lock_user_string(arg1)))
3569 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003570 ret = get_errno(sethostname(p, arg2));
3571 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003572 break;
3573 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00003574 {
3575 /* XXX: convert resource ? */
3576 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003577 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003578 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00003579 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
3580 goto efault;
bellard9de5e442003-03-23 16:49:39 +00003581 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3582 rlim.rlim_max = tswapl(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00003583 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00003584 ret = get_errno(setrlimit(resource, &rlim));
3585 }
3586 break;
bellard31e31b82003-02-18 22:55:36 +00003587 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00003588 {
3589 /* XXX: convert resource ? */
3590 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003591 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003592 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00003593
bellard9de5e442003-03-23 16:49:39 +00003594 ret = get_errno(getrlimit(resource, &rlim));
3595 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003596 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
3597 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003598 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3599 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3600 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00003601 }
3602 }
3603 break;
bellard31e31b82003-02-18 22:55:36 +00003604 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00003605 {
3606 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00003607 ret = get_errno(getrusage(arg1, &rusage));
3608 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003609 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00003610 }
3611 }
3612 break;
bellard31e31b82003-02-18 22:55:36 +00003613 case TARGET_NR_gettimeofday:
3614 {
bellard31e31b82003-02-18 22:55:36 +00003615 struct timeval tv;
3616 ret = get_errno(gettimeofday(&tv, NULL));
3617 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003618 host_to_target_timeval(arg1, &tv);
bellard31e31b82003-02-18 22:55:36 +00003619 }
3620 }
3621 break;
3622 case TARGET_NR_settimeofday:
3623 {
bellard31e31b82003-02-18 22:55:36 +00003624 struct timeval tv;
pbrook53a59602006-03-25 19:31:22 +00003625 target_to_host_timeval(&tv, arg1);
bellard31e31b82003-02-18 22:55:36 +00003626 ret = get_errno(settimeofday(&tv, NULL));
3627 }
3628 break;
bellard048f6b42005-11-26 18:47:20 +00003629#ifdef TARGET_NR_select
bellard31e31b82003-02-18 22:55:36 +00003630 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00003631 {
pbrook53a59602006-03-25 19:31:22 +00003632 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00003633 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00003634 long nsel;
3635
bellard579a97f2007-11-11 14:26:47 +00003636 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
3637 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003638 nsel = tswapl(sel->n);
3639 inp = tswapl(sel->inp);
3640 outp = tswapl(sel->outp);
3641 exp = tswapl(sel->exp);
3642 tvp = tswapl(sel->tvp);
3643 unlock_user_struct(sel, arg1, 0);
3644 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00003645 }
3646 break;
bellard048f6b42005-11-26 18:47:20 +00003647#endif
bellard31e31b82003-02-18 22:55:36 +00003648 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00003649 {
3650 void *p2;
3651 p = lock_user_string(arg1);
3652 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00003653 if (!p || !p2)
3654 ret = -TARGET_EFAULT;
3655 else
3656 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00003657 unlock_user(p2, arg2, 0);
3658 unlock_user(p, arg1, 0);
3659 }
bellard31e31b82003-02-18 22:55:36 +00003660 break;
thsf0b62432007-09-24 09:25:40 +00003661#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
3662 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00003663 {
bellard579a97f2007-11-11 14:26:47 +00003664 void *p2;
thsf0b62432007-09-24 09:25:40 +00003665 p = lock_user_string(arg1);
3666 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00003667 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00003668 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00003669 else
3670 ret = get_errno(sys_symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00003671 unlock_user(p2, arg3, 0);
3672 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00003673 }
3674 break;
3675#endif
bellardebc05482003-09-30 21:08:41 +00003676#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00003677 case TARGET_NR_oldlstat:
3678 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003679#endif
bellard31e31b82003-02-18 22:55:36 +00003680 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00003681 {
3682 void *p2;
3683 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00003684 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
3685 if (!p || !p2)
3686 ret = -TARGET_EFAULT;
3687 else
3688 ret = get_errno(readlink(path(p), p2, arg3));
pbrook53a59602006-03-25 19:31:22 +00003689 unlock_user(p2, arg2, ret);
3690 unlock_user(p, arg1, 0);
3691 }
bellard31e31b82003-02-18 22:55:36 +00003692 break;
ths5e0ccb12007-09-24 09:26:10 +00003693#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
3694 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00003695 {
bellard579a97f2007-11-11 14:26:47 +00003696 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00003697 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00003698 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
3699 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00003700 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00003701 else
3702 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
bellard579a97f2007-11-11 14:26:47 +00003703 unlock_user(p2, arg3, ret);
3704 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00003705 }
3706 break;
3707#endif
thse5febef2007-04-01 18:31:35 +00003708#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00003709 case TARGET_NR_uselib:
3710 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003711#endif
3712#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00003713 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00003714 if (!(p = lock_user_string(arg1)))
3715 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003716 ret = get_errno(swapon(p, arg2));
3717 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003718 break;
thse5febef2007-04-01 18:31:35 +00003719#endif
bellard31e31b82003-02-18 22:55:36 +00003720 case TARGET_NR_reboot:
3721 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003722#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00003723 case TARGET_NR_readdir:
3724 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003725#endif
3726#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00003727 case TARGET_NR_mmap:
ths48733d12007-10-08 13:36:46 +00003728#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
bellard31e31b82003-02-18 22:55:36 +00003729 {
blueswir1992f48a2007-10-14 16:27:31 +00003730 abi_ulong *v;
3731 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00003732 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
3733 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003734 v1 = tswapl(v[0]);
3735 v2 = tswapl(v[1]);
3736 v3 = tswapl(v[2]);
3737 v4 = tswapl(v[3]);
3738 v5 = tswapl(v[4]);
3739 v6 = tswapl(v[5]);
3740 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00003741 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00003742 target_to_host_bitmask(v4, mmap_flags_tbl),
3743 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00003744 }
bellard31e31b82003-02-18 22:55:36 +00003745#else
ths5fafdf22007-09-16 21:08:06 +00003746 ret = get_errno(target_mmap(arg1, arg2, arg3,
3747 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00003748 arg5,
3749 arg6));
bellard31e31b82003-02-18 22:55:36 +00003750#endif
bellard6fb883e2003-07-09 17:12:39 +00003751 break;
thse5febef2007-04-01 18:31:35 +00003752#endif
bellarda315a142005-01-30 22:59:18 +00003753#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00003754 case TARGET_NR_mmap2:
ths0d3267a2006-12-11 22:04:25 +00003755#if defined(TARGET_SPARC) || defined(TARGET_MIPS)
bellardc573ff62004-01-04 15:51:36 +00003756#define MMAP_SHIFT 12
3757#else
3758#define MMAP_SHIFT TARGET_PAGE_BITS
3759#endif
ths5fafdf22007-09-16 21:08:06 +00003760 ret = get_errno(target_mmap(arg1, arg2, arg3,
3761 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00003762 arg5,
bellardc573ff62004-01-04 15:51:36 +00003763 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00003764 break;
bellarda315a142005-01-30 22:59:18 +00003765#endif
bellard31e31b82003-02-18 22:55:36 +00003766 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00003767 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00003768 break;
bellard9de5e442003-03-23 16:49:39 +00003769 case TARGET_NR_mprotect:
bellard54936002003-05-13 00:25:15 +00003770 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00003771 break;
thse5febef2007-04-01 18:31:35 +00003772#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00003773 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00003774 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00003775 break;
thse5febef2007-04-01 18:31:35 +00003776#endif
pbrook53a59602006-03-25 19:31:22 +00003777 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00003778#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00003779 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00003780 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00003781 break;
thse5febef2007-04-01 18:31:35 +00003782#endif
3783#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00003784 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00003785 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00003786 break;
thse5febef2007-04-01 18:31:35 +00003787#endif
3788#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00003789 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00003790 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00003791 break;
thse5febef2007-04-01 18:31:35 +00003792#endif
3793#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00003794 case TARGET_NR_mlockall:
3795 ret = get_errno(mlockall(arg1));
3796 break;
thse5febef2007-04-01 18:31:35 +00003797#endif
3798#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00003799 case TARGET_NR_munlockall:
3800 ret = get_errno(munlockall());
3801 break;
thse5febef2007-04-01 18:31:35 +00003802#endif
bellard31e31b82003-02-18 22:55:36 +00003803 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00003804 if (!(p = lock_user_string(arg1)))
3805 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003806 ret = get_errno(truncate(p, arg2));
3807 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003808 break;
3809 case TARGET_NR_ftruncate:
3810 ret = get_errno(ftruncate(arg1, arg2));
3811 break;
3812 case TARGET_NR_fchmod:
3813 ret = get_errno(fchmod(arg1, arg2));
3814 break;
ths814d7972007-09-24 09:26:51 +00003815#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
3816 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00003817 if (!(p = lock_user_string(arg2)))
3818 goto efault;
3819 ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
3820 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00003821 break;
3822#endif
bellard31e31b82003-02-18 22:55:36 +00003823 case TARGET_NR_getpriority:
thsc6cda172007-10-09 03:42:34 +00003824 /* libc does special remapping of the return value of
3825 * sys_getpriority() so it's just easiest to call
3826 * sys_getpriority() directly rather than through libc. */
3827 ret = sys_getpriority(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00003828 break;
3829 case TARGET_NR_setpriority:
3830 ret = get_errno(setpriority(arg1, arg2, arg3));
3831 break;
bellardebc05482003-09-30 21:08:41 +00003832#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00003833 case TARGET_NR_profil:
3834 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003835#endif
bellard31e31b82003-02-18 22:55:36 +00003836 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00003837 if (!(p = lock_user_string(arg1)))
3838 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003839 ret = get_errno(statfs(path(p), &stfs));
3840 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003841 convert_statfs:
3842 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003843 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00003844
bellard579a97f2007-11-11 14:26:47 +00003845 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
3846 goto efault;
3847 __put_user(stfs.f_type, &target_stfs->f_type);
3848 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
3849 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
3850 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
3851 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
3852 __put_user(stfs.f_files, &target_stfs->f_files);
3853 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
3854 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3855 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
3856 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00003857 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00003858 }
3859 break;
3860 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00003861 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00003862 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00003863#ifdef TARGET_NR_statfs64
3864 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00003865 if (!(p = lock_user_string(arg1)))
3866 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003867 ret = get_errno(statfs(path(p), &stfs));
3868 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00003869 convert_statfs64:
3870 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003871 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00003872
bellard579a97f2007-11-11 14:26:47 +00003873 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
3874 goto efault;
3875 __put_user(stfs.f_type, &target_stfs->f_type);
3876 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
3877 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
3878 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
3879 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
3880 __put_user(stfs.f_files, &target_stfs->f_files);
3881 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
3882 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3883 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
3884 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
3885 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00003886 }
3887 break;
3888 case TARGET_NR_fstatfs64:
3889 ret = get_errno(fstatfs(arg1, &stfs));
3890 goto convert_statfs64;
3891#endif
bellardebc05482003-09-30 21:08:41 +00003892#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00003893 case TARGET_NR_ioperm:
3894 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003895#endif
thse5febef2007-04-01 18:31:35 +00003896#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00003897 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00003898 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00003899 break;
thse5febef2007-04-01 18:31:35 +00003900#endif
bellard3532fa72006-06-24 15:06:03 +00003901#ifdef TARGET_NR_accept
3902 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00003903 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003904 break;
3905#endif
3906#ifdef TARGET_NR_bind
3907 case TARGET_NR_bind:
3908 ret = do_bind(arg1, arg2, arg3);
3909 break;
3910#endif
3911#ifdef TARGET_NR_connect
3912 case TARGET_NR_connect:
3913 ret = do_connect(arg1, arg2, arg3);
3914 break;
3915#endif
3916#ifdef TARGET_NR_getpeername
3917 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00003918 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003919 break;
3920#endif
3921#ifdef TARGET_NR_getsockname
3922 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00003923 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003924 break;
3925#endif
3926#ifdef TARGET_NR_getsockopt
3927 case TARGET_NR_getsockopt:
3928 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
3929 break;
3930#endif
3931#ifdef TARGET_NR_listen
3932 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00003933 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00003934 break;
3935#endif
3936#ifdef TARGET_NR_recv
3937 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00003938 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00003939 break;
3940#endif
3941#ifdef TARGET_NR_recvfrom
3942 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00003943 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00003944 break;
3945#endif
3946#ifdef TARGET_NR_recvmsg
3947 case TARGET_NR_recvmsg:
3948 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
3949 break;
3950#endif
3951#ifdef TARGET_NR_send
3952 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00003953 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00003954 break;
3955#endif
3956#ifdef TARGET_NR_sendmsg
3957 case TARGET_NR_sendmsg:
3958 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
3959 break;
3960#endif
3961#ifdef TARGET_NR_sendto
3962 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00003963 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00003964 break;
3965#endif
3966#ifdef TARGET_NR_shutdown
3967 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00003968 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00003969 break;
3970#endif
3971#ifdef TARGET_NR_socket
3972 case TARGET_NR_socket:
3973 ret = do_socket(arg1, arg2, arg3);
3974 break;
3975#endif
3976#ifdef TARGET_NR_socketpair
3977 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00003978 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00003979 break;
3980#endif
3981#ifdef TARGET_NR_setsockopt
3982 case TARGET_NR_setsockopt:
3983 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
3984 break;
3985#endif
ths7494b0f2007-02-11 18:26:53 +00003986
bellard31e31b82003-02-18 22:55:36 +00003987 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00003988 if (!(p = lock_user_string(arg2)))
3989 goto efault;
thse5574482007-02-11 20:03:13 +00003990 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
3991 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00003992 break;
3993
bellard31e31b82003-02-18 22:55:36 +00003994 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00003995 {
bellard66fb9762003-03-23 01:06:05 +00003996 struct itimerval value, ovalue, *pvalue;
3997
pbrook53a59602006-03-25 19:31:22 +00003998 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003999 pvalue = &value;
ths5fafdf22007-09-16 21:08:06 +00004000 target_to_host_timeval(&pvalue->it_interval,
pbrook53a59602006-03-25 19:31:22 +00004001 arg2);
ths5fafdf22007-09-16 21:08:06 +00004002 target_to_host_timeval(&pvalue->it_value,
pbrook53a59602006-03-25 19:31:22 +00004003 arg2 + sizeof(struct target_timeval));
bellard66fb9762003-03-23 01:06:05 +00004004 } else {
4005 pvalue = NULL;
4006 }
4007 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00004008 if (!is_error(ret) && arg3) {
4009 host_to_target_timeval(arg3,
bellard66fb9762003-03-23 01:06:05 +00004010 &ovalue.it_interval);
pbrook53a59602006-03-25 19:31:22 +00004011 host_to_target_timeval(arg3 + sizeof(struct target_timeval),
bellard66fb9762003-03-23 01:06:05 +00004012 &ovalue.it_value);
4013 }
4014 }
4015 break;
bellard31e31b82003-02-18 22:55:36 +00004016 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00004017 {
bellard66fb9762003-03-23 01:06:05 +00004018 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00004019
bellard66fb9762003-03-23 01:06:05 +00004020 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00004021 if (!is_error(ret) && arg2) {
4022 host_to_target_timeval(arg2,
bellard66fb9762003-03-23 01:06:05 +00004023 &value.it_interval);
pbrook53a59602006-03-25 19:31:22 +00004024 host_to_target_timeval(arg2 + sizeof(struct target_timeval),
bellard66fb9762003-03-23 01:06:05 +00004025 &value.it_value);
4026 }
4027 }
4028 break;
bellard31e31b82003-02-18 22:55:36 +00004029 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00004030 if (!(p = lock_user_string(arg1)))
4031 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004032 ret = get_errno(stat(path(p), &st));
4033 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004034 goto do_stat;
4035 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00004036 if (!(p = lock_user_string(arg1)))
4037 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004038 ret = get_errno(lstat(path(p), &st));
4039 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004040 goto do_stat;
4041 case TARGET_NR_fstat:
4042 {
4043 ret = get_errno(fstat(arg1, &st));
4044 do_stat:
4045 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004046 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00004047
bellard579a97f2007-11-11 14:26:47 +00004048 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4049 goto efault;
blueswir1992f48a2007-10-14 16:27:31 +00004050#if defined(TARGET_MIPS) || (defined(TARGET_SPARC64) && !defined(TARGET_ABI32))
thse3584652007-06-01 11:49:38 +00004051 target_st->st_dev = tswap32(st.st_dev);
4052#else
bellard31e31b82003-02-18 22:55:36 +00004053 target_st->st_dev = tswap16(st.st_dev);
thse3584652007-06-01 11:49:38 +00004054#endif
bellard31e31b82003-02-18 22:55:36 +00004055 target_st->st_ino = tswapl(st.st_ino);
ths7d600c82006-12-08 01:32:58 +00004056#if defined(TARGET_PPC) || defined(TARGET_MIPS)
bellard67867302003-11-23 17:05:30 +00004057 target_st->st_mode = tswapl(st.st_mode); /* XXX: check this */
4058 target_st->st_uid = tswap32(st.st_uid);
4059 target_st->st_gid = tswap32(st.st_gid);
blueswir1992f48a2007-10-14 16:27:31 +00004060#elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir11b8dd642007-07-08 10:08:24 +00004061 target_st->st_mode = tswap32(st.st_mode);
4062 target_st->st_uid = tswap32(st.st_uid);
4063 target_st->st_gid = tswap32(st.st_gid);
bellard67867302003-11-23 17:05:30 +00004064#else
bellardec86b0f2003-04-11 00:15:04 +00004065 target_st->st_mode = tswap16(st.st_mode);
bellard31e31b82003-02-18 22:55:36 +00004066 target_st->st_uid = tswap16(st.st_uid);
4067 target_st->st_gid = tswap16(st.st_gid);
bellard67867302003-11-23 17:05:30 +00004068#endif
thse3584652007-06-01 11:49:38 +00004069#if defined(TARGET_MIPS)
4070 /* If this is the same on PPC, then just merge w/ the above ifdef */
4071 target_st->st_nlink = tswapl(st.st_nlink);
4072 target_st->st_rdev = tswapl(st.st_rdev);
blueswir1992f48a2007-10-14 16:27:31 +00004073#elif defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir11b8dd642007-07-08 10:08:24 +00004074 target_st->st_nlink = tswap32(st.st_nlink);
4075 target_st->st_rdev = tswap32(st.st_rdev);
thse3584652007-06-01 11:49:38 +00004076#else
bellard67867302003-11-23 17:05:30 +00004077 target_st->st_nlink = tswap16(st.st_nlink);
bellard31e31b82003-02-18 22:55:36 +00004078 target_st->st_rdev = tswap16(st.st_rdev);
thse3584652007-06-01 11:49:38 +00004079#endif
bellard31e31b82003-02-18 22:55:36 +00004080 target_st->st_size = tswapl(st.st_size);
4081 target_st->st_blksize = tswapl(st.st_blksize);
4082 target_st->st_blocks = tswapl(st.st_blocks);
bellard7854b052003-03-29 17:22:23 +00004083 target_st->target_st_atime = tswapl(st.st_atime);
4084 target_st->target_st_mtime = tswapl(st.st_mtime);
4085 target_st->target_st_ctime = tswapl(st.st_ctime);
pbrook53a59602006-03-25 19:31:22 +00004086 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00004087 }
4088 }
4089 break;
bellardebc05482003-09-30 21:08:41 +00004090#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00004091 case TARGET_NR_olduname:
4092 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004093#endif
4094#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00004095 case TARGET_NR_iopl:
4096 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004097#endif
bellard31e31b82003-02-18 22:55:36 +00004098 case TARGET_NR_vhangup:
4099 ret = get_errno(vhangup());
4100 break;
bellardebc05482003-09-30 21:08:41 +00004101#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00004102 case TARGET_NR_idle:
4103 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004104#endif
bellard42ad6ae2005-01-03 22:48:11 +00004105#ifdef TARGET_NR_syscall
4106 case TARGET_NR_syscall:
4107 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
4108 break;
4109#endif
bellard31e31b82003-02-18 22:55:36 +00004110 case TARGET_NR_wait4:
4111 {
4112 int status;
blueswir1992f48a2007-10-14 16:27:31 +00004113 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00004114 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00004115 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00004116 if (target_rusage)
4117 rusage_ptr = &rusage;
4118 else
4119 rusage_ptr = NULL;
4120 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
4121 if (!is_error(ret)) {
4122 if (status_ptr)
pbrook53a59602006-03-25 19:31:22 +00004123 tputl(status_ptr, status);
bellard31e31b82003-02-18 22:55:36 +00004124 if (target_rusage) {
bellardb4091862003-05-16 15:39:34 +00004125 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00004126 }
4127 }
4128 }
4129 break;
thse5febef2007-04-01 18:31:35 +00004130#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00004131 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00004132 if (!(p = lock_user_string(arg1)))
4133 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004134 ret = get_errno(swapoff(p));
4135 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004136 break;
thse5febef2007-04-01 18:31:35 +00004137#endif
bellard31e31b82003-02-18 22:55:36 +00004138 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00004139 {
pbrook53a59602006-03-25 19:31:22 +00004140 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00004141 struct sysinfo value;
4142 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00004143 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00004144 {
bellard579a97f2007-11-11 14:26:47 +00004145 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
4146 goto efault;
bellarda5448a72004-06-19 16:59:03 +00004147 __put_user(value.uptime, &target_value->uptime);
4148 __put_user(value.loads[0], &target_value->loads[0]);
4149 __put_user(value.loads[1], &target_value->loads[1]);
4150 __put_user(value.loads[2], &target_value->loads[2]);
4151 __put_user(value.totalram, &target_value->totalram);
4152 __put_user(value.freeram, &target_value->freeram);
4153 __put_user(value.sharedram, &target_value->sharedram);
4154 __put_user(value.bufferram, &target_value->bufferram);
4155 __put_user(value.totalswap, &target_value->totalswap);
4156 __put_user(value.freeswap, &target_value->freeswap);
4157 __put_user(value.procs, &target_value->procs);
4158 __put_user(value.totalhigh, &target_value->totalhigh);
4159 __put_user(value.freehigh, &target_value->freehigh);
4160 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00004161 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00004162 }
4163 }
4164 break;
thse5febef2007-04-01 18:31:35 +00004165#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00004166 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00004167 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
4168 break;
thse5febef2007-04-01 18:31:35 +00004169#endif
bellard31e31b82003-02-18 22:55:36 +00004170 case TARGET_NR_fsync:
4171 ret = get_errno(fsync(arg1));
4172 break;
bellard31e31b82003-02-18 22:55:36 +00004173 case TARGET_NR_clone:
bellard1b6b0292003-03-22 17:31:38 +00004174 ret = get_errno(do_fork(cpu_env, arg1, arg2));
4175 break;
bellardec86b0f2003-04-11 00:15:04 +00004176#ifdef __NR_exit_group
4177 /* new thread calls */
4178 case TARGET_NR_exit_group:
bellarde9009672005-04-26 20:42:36 +00004179 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00004180 ret = get_errno(exit_group(arg1));
4181 break;
4182#endif
bellard31e31b82003-02-18 22:55:36 +00004183 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00004184 if (!(p = lock_user_string(arg1)))
4185 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004186 ret = get_errno(setdomainname(p, arg2));
4187 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004188 break;
4189 case TARGET_NR_uname:
4190 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00004191 {
4192 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00004193
bellard579a97f2007-11-11 14:26:47 +00004194 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
4195 goto efault;
bellard29e619b2004-09-13 21:41:04 +00004196 ret = get_errno(sys_uname(buf));
4197 if (!is_error(ret)) {
4198 /* Overrite the native machine name with whatever is being
4199 emulated. */
4200 strcpy (buf->machine, UNAME_MACHINE);
pbrookc5937222006-05-14 11:30:38 +00004201 /* Allow the user to override the reported release. */
4202 if (qemu_uname_release && *qemu_uname_release)
4203 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00004204 }
pbrook53a59602006-03-25 19:31:22 +00004205 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00004206 }
bellard31e31b82003-02-18 22:55:36 +00004207 break;
bellard6dbad632003-03-16 18:05:05 +00004208#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00004209 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00004210 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00004211 break;
j_mayer84409dd2007-04-06 08:56:50 +00004212#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00004213 case TARGET_NR_vm86old:
4214 goto unimplemented;
4215 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00004216 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00004217 break;
4218#endif
j_mayer84409dd2007-04-06 08:56:50 +00004219#endif
bellard31e31b82003-02-18 22:55:36 +00004220 case TARGET_NR_adjtimex:
4221 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004222#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00004223 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00004224#endif
bellard31e31b82003-02-18 22:55:36 +00004225 case TARGET_NR_init_module:
4226 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00004227#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00004228 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00004229#endif
bellard31e31b82003-02-18 22:55:36 +00004230 goto unimplemented;
4231 case TARGET_NR_quotactl:
4232 goto unimplemented;
4233 case TARGET_NR_getpgid:
4234 ret = get_errno(getpgid(arg1));
4235 break;
4236 case TARGET_NR_fchdir:
4237 ret = get_errno(fchdir(arg1));
4238 break;
j_mayer84409dd2007-04-06 08:56:50 +00004239#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00004240 case TARGET_NR_bdflush:
4241 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00004242#endif
thse5febef2007-04-01 18:31:35 +00004243#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00004244 case TARGET_NR_sysfs:
4245 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004246#endif
bellard31e31b82003-02-18 22:55:36 +00004247 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00004248 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00004249 break;
thse5febef2007-04-01 18:31:35 +00004250#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00004251 case TARGET_NR_afs_syscall:
4252 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004253#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004254#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004255 case TARGET_NR__llseek:
4256 {
bellard4f2ac232004-04-26 19:44:02 +00004257#if defined (__x86_64__)
4258 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
pbrook53a59602006-03-25 19:31:22 +00004259 tput64(arg4, ret);
bellard4f2ac232004-04-26 19:44:02 +00004260#else
bellard31e31b82003-02-18 22:55:36 +00004261 int64_t res;
4262 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
pbrook53a59602006-03-25 19:31:22 +00004263 tput64(arg4, res);
bellard4f2ac232004-04-26 19:44:02 +00004264#endif
bellard31e31b82003-02-18 22:55:36 +00004265 }
4266 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004267#endif
bellard31e31b82003-02-18 22:55:36 +00004268 case TARGET_NR_getdents:
blueswir1992f48a2007-10-14 16:27:31 +00004269#if TARGET_ABI_BITS != 32
pbrook53a59602006-03-25 19:31:22 +00004270 goto unimplemented;
blueswir1992f48a2007-10-14 16:27:31 +00004271#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00004272 {
pbrook53a59602006-03-25 19:31:22 +00004273 struct target_dirent *target_dirp;
bellard4add45b2003-06-05 01:52:59 +00004274 struct dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004275 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00004276
4277 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00004278 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00004279 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00004280 goto fail;
4281 }
ths3b46e622007-09-17 08:09:54 +00004282
bellard4add45b2003-06-05 01:52:59 +00004283 ret = get_errno(sys_getdents(arg1, dirp, count));
4284 if (!is_error(ret)) {
4285 struct dirent *de;
4286 struct target_dirent *tde;
4287 int len = ret;
4288 int reclen, treclen;
4289 int count1, tnamelen;
4290
4291 count1 = 0;
4292 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00004293 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4294 goto efault;
bellard4add45b2003-06-05 01:52:59 +00004295 tde = target_dirp;
4296 while (len > 0) {
4297 reclen = de->d_reclen;
blueswir1992f48a2007-10-14 16:27:31 +00004298 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
bellard4add45b2003-06-05 01:52:59 +00004299 tde->d_reclen = tswap16(treclen);
4300 tde->d_ino = tswapl(de->d_ino);
4301 tde->d_off = tswapl(de->d_off);
blueswir1992f48a2007-10-14 16:27:31 +00004302 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
bellard4add45b2003-06-05 01:52:59 +00004303 if (tnamelen > 256)
4304 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00004305 /* XXX: may not be correct */
bellard4add45b2003-06-05 01:52:59 +00004306 strncpy(tde->d_name, de->d_name, tnamelen);
4307 de = (struct dirent *)((char *)de + reclen);
4308 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00004309 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00004310 count1 += treclen;
4311 }
4312 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00004313 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00004314 }
4315 free(dirp);
4316 }
4317#else
bellard31e31b82003-02-18 22:55:36 +00004318 {
pbrook53a59602006-03-25 19:31:22 +00004319 struct dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004320 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00004321
bellard579a97f2007-11-11 14:26:47 +00004322 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4323 goto efault;
bellard72f03902003-02-18 23:33:18 +00004324 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00004325 if (!is_error(ret)) {
4326 struct dirent *de;
4327 int len = ret;
4328 int reclen;
4329 de = dirp;
4330 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004331 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00004332 if (reclen > len)
4333 break;
bellard8083a3e2003-03-24 23:12:16 +00004334 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00004335 tswapls(&de->d_ino);
4336 tswapls(&de->d_off);
4337 de = (struct dirent *)((char *)de + reclen);
4338 len -= reclen;
4339 }
4340 }
pbrook53a59602006-03-25 19:31:22 +00004341 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00004342 }
bellard4add45b2003-06-05 01:52:59 +00004343#endif
bellard31e31b82003-02-18 22:55:36 +00004344 break;
ths3ae43202007-09-16 21:39:48 +00004345#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00004346 case TARGET_NR_getdents64:
4347 {
pbrook53a59602006-03-25 19:31:22 +00004348 struct dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004349 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00004350 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4351 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00004352 ret = get_errno(sys_getdents64(arg1, dirp, count));
4353 if (!is_error(ret)) {
4354 struct dirent64 *de;
4355 int len = ret;
4356 int reclen;
4357 de = dirp;
4358 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004359 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00004360 if (reclen > len)
4361 break;
bellard8083a3e2003-03-24 23:12:16 +00004362 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00004363 tswap64s((uint64_t *)&de->d_ino);
4364 tswap64s((uint64_t *)&de->d_off);
bellarddab2ed92003-03-22 15:23:14 +00004365 de = (struct dirent64 *)((char *)de + reclen);
4366 len -= reclen;
4367 }
4368 }
pbrook53a59602006-03-25 19:31:22 +00004369 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00004370 }
4371 break;
bellarda541f292004-04-12 20:39:29 +00004372#endif /* TARGET_NR_getdents64 */
thse5febef2007-04-01 18:31:35 +00004373#ifdef TARGET_NR__newselect
bellard31e31b82003-02-18 22:55:36 +00004374 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00004375 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00004376 break;
thse5febef2007-04-01 18:31:35 +00004377#endif
4378#ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00004379 case TARGET_NR_poll:
4380 {
pbrook53a59602006-03-25 19:31:22 +00004381 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00004382 unsigned int nfds = arg2;
4383 int timeout = arg3;
4384 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00004385 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00004386
bellard579a97f2007-11-11 14:26:47 +00004387 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
4388 if (!target_pfd)
4389 goto efault;
bellard9de5e442003-03-23 16:49:39 +00004390 pfd = alloca(sizeof(struct pollfd) * nfds);
4391 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004392 pfd[i].fd = tswap32(target_pfd[i].fd);
4393 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00004394 }
4395 ret = get_errno(poll(pfd, nfds, timeout));
4396 if (!is_error(ret)) {
4397 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004398 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00004399 }
pbrook53a59602006-03-25 19:31:22 +00004400 ret += nfds * (sizeof(struct target_pollfd)
4401 - sizeof(struct pollfd));
bellard9de5e442003-03-23 16:49:39 +00004402 }
pbrook53a59602006-03-25 19:31:22 +00004403 unlock_user(target_pfd, arg1, ret);
bellard9de5e442003-03-23 16:49:39 +00004404 }
4405 break;
thse5febef2007-04-01 18:31:35 +00004406#endif
bellard31e31b82003-02-18 22:55:36 +00004407 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00004408 /* NOTE: the flock constant seems to be the same for every
4409 Linux platform */
4410 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00004411 break;
4412 case TARGET_NR_readv:
4413 {
4414 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00004415 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00004416
4417 vec = alloca(count * sizeof(struct iovec));
bellard579a97f2007-11-11 14:26:47 +00004418 lock_iovec(VERIFY_WRITE, vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00004419 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00004420 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00004421 }
4422 break;
4423 case TARGET_NR_writev:
4424 {
4425 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00004426 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00004427
4428 vec = alloca(count * sizeof(struct iovec));
bellard579a97f2007-11-11 14:26:47 +00004429 lock_iovec(VERIFY_READ, vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00004430 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00004431 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00004432 }
4433 break;
4434 case TARGET_NR_getsid:
4435 ret = get_errno(getsid(arg1));
4436 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004437#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00004438 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00004439 ret = get_errno(fdatasync(arg1));
4440 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004441#endif
bellard31e31b82003-02-18 22:55:36 +00004442 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00004443 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00004444 return value. */
ths0da46a62007-10-20 20:23:07 +00004445 ret = -TARGET_ENOTDIR;
4446 break;
bellard31e31b82003-02-18 22:55:36 +00004447 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00004448 {
pbrook53a59602006-03-25 19:31:22 +00004449 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004450 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00004451
bellard579a97f2007-11-11 14:26:47 +00004452 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
4453 goto efault;
bellard5cd43932003-03-29 16:54:36 +00004454 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004455 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00004456 ret = get_errno(sched_setparam(arg1, &schp));
4457 }
4458 break;
bellard31e31b82003-02-18 22:55:36 +00004459 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00004460 {
pbrook53a59602006-03-25 19:31:22 +00004461 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004462 struct sched_param schp;
4463 ret = get_errno(sched_getparam(arg1, &schp));
4464 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004465 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
4466 goto efault;
bellard5cd43932003-03-29 16:54:36 +00004467 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004468 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00004469 }
4470 }
4471 break;
bellard31e31b82003-02-18 22:55:36 +00004472 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00004473 {
pbrook53a59602006-03-25 19:31:22 +00004474 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004475 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00004476 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
4477 goto efault;
bellard5cd43932003-03-29 16:54:36 +00004478 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004479 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00004480 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
4481 }
4482 break;
bellard31e31b82003-02-18 22:55:36 +00004483 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00004484 ret = get_errno(sched_getscheduler(arg1));
4485 break;
bellard31e31b82003-02-18 22:55:36 +00004486 case TARGET_NR_sched_yield:
4487 ret = get_errno(sched_yield());
4488 break;
4489 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00004490 ret = get_errno(sched_get_priority_max(arg1));
4491 break;
bellard31e31b82003-02-18 22:55:36 +00004492 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00004493 ret = get_errno(sched_get_priority_min(arg1));
4494 break;
bellard31e31b82003-02-18 22:55:36 +00004495 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00004496 {
bellard5cd43932003-03-29 16:54:36 +00004497 struct timespec ts;
4498 ret = get_errno(sched_rr_get_interval(arg1, &ts));
4499 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004500 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00004501 }
4502 }
4503 break;
bellard31e31b82003-02-18 22:55:36 +00004504 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00004505 {
bellard1b6b0292003-03-22 17:31:38 +00004506 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00004507 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00004508 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00004509 if (is_error(ret) && arg2) {
4510 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00004511 }
4512 }
4513 break;
thse5febef2007-04-01 18:31:35 +00004514#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00004515 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00004516 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004517#endif
4518#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00004519 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00004520 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004521#endif
bellard31e31b82003-02-18 22:55:36 +00004522 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00004523 switch (arg1)
4524 {
4525 case PR_GET_PDEATHSIG:
4526 {
4527 int deathsig;
4528 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
4529 if (!is_error(ret) && arg2)
4530 tput32(arg2, deathsig);
4531 }
4532 break;
4533 default:
4534 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
4535 break;
4536 }
ths39b9aae2007-02-11 18:36:44 +00004537 break;
bellard67867302003-11-23 17:05:30 +00004538#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00004539 case TARGET_NR_pread:
pbrook53a59602006-03-25 19:31:22 +00004540 page_unprotect_range(arg2, arg3);
bellard579a97f2007-11-11 14:26:47 +00004541 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4542 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004543 ret = get_errno(pread(arg1, p, arg3, arg4));
4544 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00004545 break;
bellard31e31b82003-02-18 22:55:36 +00004546 case TARGET_NR_pwrite:
bellard579a97f2007-11-11 14:26:47 +00004547 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4548 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004549 ret = get_errno(pwrite(arg1, p, arg3, arg4));
4550 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00004551 break;
bellard67867302003-11-23 17:05:30 +00004552#endif
bellard31e31b82003-02-18 22:55:36 +00004553 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00004554 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
4555 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004556 ret = get_errno(sys_getcwd1(p, arg2));
4557 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00004558 break;
4559 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00004560 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004561 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00004562 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004563 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00004564#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
4565 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
bellard579a97f2007-11-11 14:26:47 +00004566 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00004567 break;
4568#else
bellard5cd43932003-03-29 16:54:36 +00004569 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00004570#endif
bellard31e31b82003-02-18 22:55:36 +00004571 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00004572 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004573#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00004574 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00004575 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004576#endif
4577#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00004578 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00004579 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004580#endif
bellard048f6b42005-11-26 18:47:20 +00004581#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00004582 case TARGET_NR_vfork:
bellard1b6b0292003-03-22 17:31:38 +00004583 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00004584 break;
bellard048f6b42005-11-26 18:47:20 +00004585#endif
bellardebc05482003-09-30 21:08:41 +00004586#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00004587 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00004588 {
4589 struct rlimit rlim;
4590 ret = get_errno(getrlimit(arg1, &rlim));
4591 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004592 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00004593 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
4594 goto efault;
bellard728584b2003-04-29 20:43:36 +00004595 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
4596 target_rlim->rlim_max = tswapl(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00004597 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00004598 }
4599 break;
4600 }
bellardebc05482003-09-30 21:08:41 +00004601#endif
bellarda315a142005-01-30 22:59:18 +00004602#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00004603 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00004604 if (!(p = lock_user_string(arg1)))
4605 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004606 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
4607 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00004608 break;
bellarda315a142005-01-30 22:59:18 +00004609#endif
4610#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00004611 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00004612 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00004613 break;
bellarda315a142005-01-30 22:59:18 +00004614#endif
4615#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00004616 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00004617 if (!(p = lock_user_string(arg1)))
4618 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004619 ret = get_errno(stat(path(p), &st));
4620 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004621 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004622#endif
4623#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00004624 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00004625 if (!(p = lock_user_string(arg1)))
4626 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004627 ret = get_errno(lstat(path(p), &st));
4628 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004629 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004630#endif
4631#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00004632 case TARGET_NR_fstat64:
bellard60cd49d2003-03-16 22:53:56 +00004633 {
4634 ret = get_errno(fstat(arg1, &st));
4635 do_stat64:
4636 if (!is_error(ret)) {
pbrookce4defa2006-02-09 16:49:55 +00004637#ifdef TARGET_ARM
4638 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004639 struct target_eabi_stat64 *target_st;
bellard579a97f2007-11-11 14:26:47 +00004640
4641 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4642 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00004643 memset(target_st, 0, sizeof(struct target_eabi_stat64));
bellard579a97f2007-11-11 14:26:47 +00004644 __put_user(st.st_dev, &target_st->st_dev);
4645 __put_user(st.st_ino, &target_st->st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004646#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
bellard579a97f2007-11-11 14:26:47 +00004647 __put_user(st.st_ino, &target_st->__st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004648#endif
bellard579a97f2007-11-11 14:26:47 +00004649 __put_user(st.st_mode, &target_st->st_mode);
4650 __put_user(st.st_nlink, &target_st->st_nlink);
4651 __put_user(st.st_uid, &target_st->st_uid);
4652 __put_user(st.st_gid, &target_st->st_gid);
4653 __put_user(st.st_rdev, &target_st->st_rdev);
4654 __put_user(st.st_size, &target_st->st_size);
4655 __put_user(st.st_blksize, &target_st->st_blksize);
4656 __put_user(st.st_blocks, &target_st->st_blocks);
4657 __put_user(st.st_atime, &target_st->target_st_atime);
4658 __put_user(st.st_mtime, &target_st->target_st_mtime);
4659 __put_user(st.st_ctime, &target_st->target_st_ctime);
4660 unlock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00004661 } else
4662#endif
4663 {
pbrook53a59602006-03-25 19:31:22 +00004664 struct target_stat64 *target_st;
bellard579a97f2007-11-11 14:26:47 +00004665
4666 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4667 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00004668 memset(target_st, 0, sizeof(struct target_stat64));
bellard579a97f2007-11-11 14:26:47 +00004669 __put_user(st.st_dev, &target_st->st_dev);
4670 __put_user(st.st_ino, &target_st->st_ino);
pbrookce4defa2006-02-09 16:49:55 +00004671#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
bellard579a97f2007-11-11 14:26:47 +00004672 __put_user(st.st_ino, &target_st->__st_ino);
pbrookce4defa2006-02-09 16:49:55 +00004673#endif
bellard579a97f2007-11-11 14:26:47 +00004674 __put_user(st.st_mode, &target_st->st_mode);
4675 __put_user(st.st_nlink, &target_st->st_nlink);
4676 __put_user(st.st_uid, &target_st->st_uid);
4677 __put_user(st.st_gid, &target_st->st_gid);
4678 __put_user(st.st_rdev, &target_st->st_rdev);
pbrookce4defa2006-02-09 16:49:55 +00004679 /* XXX: better use of kernel struct */
bellard579a97f2007-11-11 14:26:47 +00004680 __put_user(st.st_size, &target_st->st_size);
4681 __put_user(st.st_blksize, &target_st->st_blksize);
4682 __put_user(st.st_blocks, &target_st->st_blocks);
4683 __put_user(st.st_atime, &target_st->target_st_atime);
4684 __put_user(st.st_mtime, &target_st->target_st_mtime);
4685 __put_user(st.st_ctime, &target_st->target_st_ctime);
4686 unlock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00004687 }
bellard60cd49d2003-03-16 22:53:56 +00004688 }
4689 }
4690 break;
bellarda315a142005-01-30 22:59:18 +00004691#endif
bellard67867302003-11-23 17:05:30 +00004692#ifdef USE_UID16
4693 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00004694 if (!(p = lock_user_string(arg1)))
4695 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004696 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
4697 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00004698 break;
4699 case TARGET_NR_getuid:
4700 ret = get_errno(high2lowuid(getuid()));
4701 break;
4702 case TARGET_NR_getgid:
4703 ret = get_errno(high2lowgid(getgid()));
4704 break;
4705 case TARGET_NR_geteuid:
4706 ret = get_errno(high2lowuid(geteuid()));
4707 break;
4708 case TARGET_NR_getegid:
4709 ret = get_errno(high2lowgid(getegid()));
4710 break;
4711 case TARGET_NR_setreuid:
4712 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
4713 break;
4714 case TARGET_NR_setregid:
4715 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
4716 break;
4717 case TARGET_NR_getgroups:
4718 {
4719 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004720 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00004721 gid_t *grouplist;
4722 int i;
4723
4724 grouplist = alloca(gidsetsize * sizeof(gid_t));
4725 ret = get_errno(getgroups(gidsetsize, grouplist));
4726 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004727 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
4728 if (!target_grouplist)
4729 goto efault;
bellard67867302003-11-23 17:05:30 +00004730 for(i = 0;i < gidsetsize; i++)
4731 target_grouplist[i] = tswap16(grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00004732 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00004733 }
4734 }
4735 break;
4736 case TARGET_NR_setgroups:
4737 {
4738 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004739 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00004740 gid_t *grouplist;
4741 int i;
4742
4743 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00004744 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
4745 if (!target_grouplist) {
4746 ret = -TARGET_EFAULT;
4747 goto fail;
4748 }
bellard67867302003-11-23 17:05:30 +00004749 for(i = 0;i < gidsetsize; i++)
4750 grouplist[i] = tswap16(target_grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00004751 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00004752 ret = get_errno(setgroups(gidsetsize, grouplist));
4753 }
4754 break;
4755 case TARGET_NR_fchown:
4756 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
4757 break;
thsccfa72b2007-09-24 09:23:34 +00004758#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
4759 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00004760 if (!(p = lock_user_string(arg2)))
4761 goto efault;
4762 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
4763 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00004764 break;
4765#endif
bellard67867302003-11-23 17:05:30 +00004766#ifdef TARGET_NR_setresuid
4767 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00004768 ret = get_errno(setresuid(low2highuid(arg1),
4769 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00004770 low2highuid(arg3)));
4771 break;
4772#endif
4773#ifdef TARGET_NR_getresuid
4774 case TARGET_NR_getresuid:
4775 {
pbrook53a59602006-03-25 19:31:22 +00004776 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00004777 ret = get_errno(getresuid(&ruid, &euid, &suid));
4778 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004779 tput16(arg1, tswap16(high2lowuid(ruid)));
4780 tput16(arg2, tswap16(high2lowuid(euid)));
4781 tput16(arg3, tswap16(high2lowuid(suid)));
bellard67867302003-11-23 17:05:30 +00004782 }
4783 }
4784 break;
4785#endif
4786#ifdef TARGET_NR_getresgid
4787 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00004788 ret = get_errno(setresgid(low2highgid(arg1),
4789 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00004790 low2highgid(arg3)));
4791 break;
4792#endif
4793#ifdef TARGET_NR_getresgid
4794 case TARGET_NR_getresgid:
4795 {
pbrook53a59602006-03-25 19:31:22 +00004796 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00004797 ret = get_errno(getresgid(&rgid, &egid, &sgid));
4798 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004799 tput16(arg1, tswap16(high2lowgid(rgid)));
4800 tput16(arg2, tswap16(high2lowgid(egid)));
4801 tput16(arg3, tswap16(high2lowgid(sgid)));
bellard67867302003-11-23 17:05:30 +00004802 }
4803 }
4804 break;
4805#endif
4806 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00004807 if (!(p = lock_user_string(arg1)))
4808 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004809 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
4810 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00004811 break;
4812 case TARGET_NR_setuid:
4813 ret = get_errno(setuid(low2highuid(arg1)));
4814 break;
4815 case TARGET_NR_setgid:
4816 ret = get_errno(setgid(low2highgid(arg1)));
4817 break;
4818 case TARGET_NR_setfsuid:
4819 ret = get_errno(setfsuid(arg1));
4820 break;
4821 case TARGET_NR_setfsgid:
4822 ret = get_errno(setfsgid(arg1));
4823 break;
4824#endif /* USE_UID16 */
4825
bellarda315a142005-01-30 22:59:18 +00004826#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00004827 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00004828 if (!(p = lock_user_string(arg1)))
4829 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004830 ret = get_errno(lchown(p, arg2, arg3));
4831 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00004832 break;
bellarda315a142005-01-30 22:59:18 +00004833#endif
4834#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00004835 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00004836 ret = get_errno(getuid());
4837 break;
bellarda315a142005-01-30 22:59:18 +00004838#endif
4839#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00004840 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00004841 ret = get_errno(getgid());
4842 break;
bellarda315a142005-01-30 22:59:18 +00004843#endif
4844#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00004845 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00004846 ret = get_errno(geteuid());
4847 break;
bellarda315a142005-01-30 22:59:18 +00004848#endif
4849#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00004850 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00004851 ret = get_errno(getegid());
4852 break;
bellarda315a142005-01-30 22:59:18 +00004853#endif
4854#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00004855 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00004856 ret = get_errno(setreuid(arg1, arg2));
4857 break;
bellarda315a142005-01-30 22:59:18 +00004858#endif
4859#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00004860 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00004861 ret = get_errno(setregid(arg1, arg2));
4862 break;
bellarda315a142005-01-30 22:59:18 +00004863#endif
4864#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00004865 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00004866 {
4867 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004868 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00004869 gid_t *grouplist;
4870 int i;
4871
4872 grouplist = alloca(gidsetsize * sizeof(gid_t));
4873 ret = get_errno(getgroups(gidsetsize, grouplist));
4874 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004875 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
4876 if (!target_grouplist) {
4877 ret = -TARGET_EFAULT;
4878 goto fail;
4879 }
bellard99c475a2005-01-31 20:45:13 +00004880 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00004881 target_grouplist[i] = tswap32(grouplist[i]);
4882 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00004883 }
4884 }
4885 break;
bellarda315a142005-01-30 22:59:18 +00004886#endif
4887#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00004888 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00004889 {
4890 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004891 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00004892 gid_t *grouplist;
4893 int i;
ths3b46e622007-09-17 08:09:54 +00004894
bellard99c475a2005-01-31 20:45:13 +00004895 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00004896 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
4897 if (!target_grouplist) {
4898 ret = -TARGET_EFAULT;
4899 goto fail;
4900 }
bellard99c475a2005-01-31 20:45:13 +00004901 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00004902 grouplist[i] = tswap32(target_grouplist[i]);
4903 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00004904 ret = get_errno(setgroups(gidsetsize, grouplist));
4905 }
4906 break;
bellarda315a142005-01-30 22:59:18 +00004907#endif
4908#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00004909 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00004910 ret = get_errno(fchown(arg1, arg2, arg3));
4911 break;
bellarda315a142005-01-30 22:59:18 +00004912#endif
4913#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00004914 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00004915 ret = get_errno(setresuid(arg1, arg2, arg3));
4916 break;
bellarda315a142005-01-30 22:59:18 +00004917#endif
4918#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00004919 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00004920 {
pbrook53a59602006-03-25 19:31:22 +00004921 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00004922 ret = get_errno(getresuid(&ruid, &euid, &suid));
4923 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004924 tput32(arg1, tswap32(ruid));
4925 tput32(arg2, tswap32(euid));
4926 tput32(arg3, tswap32(suid));
bellardb03c60f2003-03-23 17:19:56 +00004927 }
4928 }
4929 break;
bellarda315a142005-01-30 22:59:18 +00004930#endif
4931#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00004932 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00004933 ret = get_errno(setresgid(arg1, arg2, arg3));
4934 break;
bellarda315a142005-01-30 22:59:18 +00004935#endif
4936#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00004937 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00004938 {
pbrook53a59602006-03-25 19:31:22 +00004939 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00004940 ret = get_errno(getresgid(&rgid, &egid, &sgid));
4941 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004942 tput32(arg1, tswap32(rgid));
4943 tput32(arg2, tswap32(egid));
4944 tput32(arg3, tswap32(sgid));
bellardb03c60f2003-03-23 17:19:56 +00004945 }
4946 }
4947 break;
bellarda315a142005-01-30 22:59:18 +00004948#endif
4949#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00004950 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00004951 if (!(p = lock_user_string(arg1)))
4952 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004953 ret = get_errno(chown(p, arg2, arg3));
4954 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00004955 break;
bellarda315a142005-01-30 22:59:18 +00004956#endif
4957#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00004958 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00004959 ret = get_errno(setuid(arg1));
4960 break;
bellarda315a142005-01-30 22:59:18 +00004961#endif
4962#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00004963 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00004964 ret = get_errno(setgid(arg1));
4965 break;
bellarda315a142005-01-30 22:59:18 +00004966#endif
4967#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00004968 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00004969 ret = get_errno(setfsuid(arg1));
4970 break;
bellarda315a142005-01-30 22:59:18 +00004971#endif
4972#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00004973 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00004974 ret = get_errno(setfsgid(arg1));
4975 break;
bellarda315a142005-01-30 22:59:18 +00004976#endif
bellard67867302003-11-23 17:05:30 +00004977
bellard31e31b82003-02-18 22:55:36 +00004978 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00004979 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00004980#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00004981 case TARGET_NR_mincore:
bellardb03c60f2003-03-23 17:19:56 +00004982 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00004983#endif
4984#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00004985 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00004986 /* A straight passthrough may not be safe because qemu sometimes
4987 turns private flie-backed mappings into anonymous mappings.
4988 This will break MADV_DONTNEED.
4989 This is a hint, so ignoring and returning success is ok. */
4990 ret = get_errno(0);
4991 break;
bellardffa65c32004-01-04 23:57:22 +00004992#endif
blueswir1992f48a2007-10-14 16:27:31 +00004993#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00004994 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00004995 {
thsb1e341e2007-03-20 21:50:52 +00004996 int cmd;
bellard77e46722003-04-29 20:39:06 +00004997 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00004998 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00004999#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00005000 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00005001#endif
bellard77e46722003-04-29 20:39:06 +00005002
thsb1e341e2007-03-20 21:50:52 +00005003 switch(arg2){
5004 case TARGET_F_GETLK64:
5005 cmd = F_GETLK64;
thsa7222582007-03-22 15:08:55 +00005006 break;
thsb1e341e2007-03-20 21:50:52 +00005007 case TARGET_F_SETLK64:
5008 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00005009 break;
thsb1e341e2007-03-20 21:50:52 +00005010 case TARGET_F_SETLKW64:
5011 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00005012 break;
thsb1e341e2007-03-20 21:50:52 +00005013 default:
5014 cmd = arg2;
thsa7222582007-03-22 15:08:55 +00005015 break;
thsb1e341e2007-03-20 21:50:52 +00005016 }
5017
bellard60cd49d2003-03-16 22:53:56 +00005018 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00005019 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00005020#ifdef TARGET_ARM
5021 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00005022 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5023 goto efault;
ths58134272007-03-31 18:59:32 +00005024 fl.l_type = tswap16(target_efl->l_type);
5025 fl.l_whence = tswap16(target_efl->l_whence);
5026 fl.l_start = tswap64(target_efl->l_start);
5027 fl.l_len = tswap64(target_efl->l_len);
5028 fl.l_pid = tswapl(target_efl->l_pid);
5029 unlock_user_struct(target_efl, arg3, 0);
5030 } else
5031#endif
5032 {
bellard9ee1fa22007-11-11 15:11:19 +00005033 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5034 goto efault;
ths58134272007-03-31 18:59:32 +00005035 fl.l_type = tswap16(target_fl->l_type);
5036 fl.l_whence = tswap16(target_fl->l_whence);
5037 fl.l_start = tswap64(target_fl->l_start);
5038 fl.l_len = tswap64(target_fl->l_len);
5039 fl.l_pid = tswapl(target_fl->l_pid);
5040 unlock_user_struct(target_fl, arg3, 0);
5041 }
thsb1e341e2007-03-20 21:50:52 +00005042 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00005043 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00005044#ifdef TARGET_ARM
5045 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00005046 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
5047 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005048 target_efl->l_type = tswap16(fl.l_type);
5049 target_efl->l_whence = tswap16(fl.l_whence);
5050 target_efl->l_start = tswap64(fl.l_start);
5051 target_efl->l_len = tswap64(fl.l_len);
5052 target_efl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00005053 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00005054 } else
5055#endif
5056 {
bellard9ee1fa22007-11-11 15:11:19 +00005057 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
5058 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005059 target_fl->l_type = tswap16(fl.l_type);
5060 target_fl->l_whence = tswap16(fl.l_whence);
5061 target_fl->l_start = tswap64(fl.l_start);
5062 target_fl->l_len = tswap64(fl.l_len);
5063 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00005064 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00005065 }
bellard77e46722003-04-29 20:39:06 +00005066 }
5067 break;
5068
thsb1e341e2007-03-20 21:50:52 +00005069 case TARGET_F_SETLK64:
5070 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00005071#ifdef TARGET_ARM
5072 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00005073 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5074 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005075 fl.l_type = tswap16(target_efl->l_type);
5076 fl.l_whence = tswap16(target_efl->l_whence);
5077 fl.l_start = tswap64(target_efl->l_start);
5078 fl.l_len = tswap64(target_efl->l_len);
5079 fl.l_pid = tswapl(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00005080 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00005081 } else
5082#endif
5083 {
bellard9ee1fa22007-11-11 15:11:19 +00005084 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5085 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005086 fl.l_type = tswap16(target_fl->l_type);
5087 fl.l_whence = tswap16(target_fl->l_whence);
5088 fl.l_start = tswap64(target_fl->l_start);
5089 fl.l_len = tswap64(target_fl->l_len);
5090 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00005091 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00005092 }
thsb1e341e2007-03-20 21:50:52 +00005093 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00005094 break;
bellard60cd49d2003-03-16 22:53:56 +00005095 default:
bellard9ee1fa22007-11-11 15:11:19 +00005096 ret = do_fcntl(arg1, cmd, arg3);
bellard60cd49d2003-03-16 22:53:56 +00005097 break;
5098 }
bellard77e46722003-04-29 20:39:06 +00005099 break;
5100 }
bellard60cd49d2003-03-16 22:53:56 +00005101#endif
ths7d600c82006-12-08 01:32:58 +00005102#ifdef TARGET_NR_cacheflush
5103 case TARGET_NR_cacheflush:
5104 /* self-modifying code is handled automatically, so nothing needed */
5105 ret = 0;
5106 break;
5107#endif
bellardebc05482003-09-30 21:08:41 +00005108#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00005109 case TARGET_NR_security:
5110 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005111#endif
bellardc573ff62004-01-04 15:51:36 +00005112#ifdef TARGET_NR_getpagesize
5113 case TARGET_NR_getpagesize:
5114 ret = TARGET_PAGE_SIZE;
5115 break;
5116#endif
bellard31e31b82003-02-18 22:55:36 +00005117 case TARGET_NR_gettid:
5118 ret = get_errno(gettid());
5119 break;
thse5febef2007-04-01 18:31:35 +00005120#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00005121 case TARGET_NR_readahead:
bellard5cd43932003-03-29 16:54:36 +00005122 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005123#endif
bellardebc05482003-09-30 21:08:41 +00005124#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00005125 case TARGET_NR_setxattr:
5126 case TARGET_NR_lsetxattr:
5127 case TARGET_NR_fsetxattr:
5128 case TARGET_NR_getxattr:
5129 case TARGET_NR_lgetxattr:
5130 case TARGET_NR_fgetxattr:
5131 case TARGET_NR_listxattr:
5132 case TARGET_NR_llistxattr:
5133 case TARGET_NR_flistxattr:
5134 case TARGET_NR_removexattr:
5135 case TARGET_NR_lremovexattr:
5136 case TARGET_NR_fremovexattr:
bellard5cd43932003-03-29 16:54:36 +00005137 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00005138#endif
5139#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00005140 case TARGET_NR_set_thread_area:
ths6f5b89a2007-03-02 20:48:00 +00005141#ifdef TARGET_MIPS
5142 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
5143 ret = 0;
5144 break;
5145#else
5146 goto unimplemented_nowarn;
5147#endif
5148#endif
5149#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00005150 case TARGET_NR_get_thread_area:
5151 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00005152#endif
bellard48dc41e2006-06-21 18:15:50 +00005153#ifdef TARGET_NR_getdomainname
5154 case TARGET_NR_getdomainname:
5155 goto unimplemented_nowarn;
5156#endif
ths6f5b89a2007-03-02 20:48:00 +00005157
thsb5906f92007-03-19 13:32:45 +00005158#ifdef TARGET_NR_clock_gettime
5159 case TARGET_NR_clock_gettime:
5160 {
5161 struct timespec ts;
5162 ret = get_errno(clock_gettime(arg1, &ts));
5163 if (!is_error(ret)) {
5164 host_to_target_timespec(arg2, &ts);
5165 }
5166 break;
5167 }
5168#endif
5169#ifdef TARGET_NR_clock_getres
5170 case TARGET_NR_clock_getres:
5171 {
5172 struct timespec ts;
5173 ret = get_errno(clock_getres(arg1, &ts));
5174 if (!is_error(ret)) {
5175 host_to_target_timespec(arg2, &ts);
5176 }
5177 break;
5178 }
5179#endif
5180
ths6f5b89a2007-03-02 20:48:00 +00005181#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
5182 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00005183 ret = get_errno(set_tid_address((int *)g2h(arg1)));
5184 break;
ths6f5b89a2007-03-02 20:48:00 +00005185#endif
5186
ths3ae43202007-09-16 21:39:48 +00005187#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00005188 case TARGET_NR_tkill:
5189 ret = get_errno(sys_tkill((int)arg1, (int)arg2));
5190 break;
5191#endif
5192
ths3ae43202007-09-16 21:39:48 +00005193#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00005194 case TARGET_NR_tgkill:
5195 ret = get_errno(sys_tgkill((int)arg1, (int)arg2, (int)arg3));
5196 break;
5197#endif
5198
ths4f2b1fe2007-06-21 21:57:12 +00005199#ifdef TARGET_NR_set_robust_list
5200 case TARGET_NR_set_robust_list:
5201 goto unimplemented_nowarn;
5202#endif
5203
ths9007f0e2007-09-25 17:50:37 +00005204#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
5205 case TARGET_NR_utimensat:
5206 {
5207 struct timespec ts[2];
5208 target_to_host_timespec(ts, arg3);
5209 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
5210 if (!arg2)
5211 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
5212 else {
bellard579a97f2007-11-11 14:26:47 +00005213 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00005214 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00005215 goto fail;
5216 }
5217 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
5218 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00005219 }
5220 }
5221 break;
5222#endif
5223
bellard31e31b82003-02-18 22:55:36 +00005224 default:
5225 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00005226 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00005227#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 +00005228 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00005229#endif
ths0da46a62007-10-20 20:23:07 +00005230 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00005231 break;
5232 }
bellard579a97f2007-11-11 14:26:47 +00005233fail:
bellardc573ff62004-01-04 15:51:36 +00005234#ifdef DEBUG
5235 gemu_log(" = %ld\n", ret);
5236#endif
thsb92c47c2007-11-01 00:07:38 +00005237 if(do_strace)
5238 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00005239 return ret;
bellard579a97f2007-11-11 14:26:47 +00005240efault:
5241 ret = -TARGET_EFAULT;
5242 goto fail;
bellard31e31b82003-02-18 22:55:36 +00005243}