blob: c3b09c0a6d4bfb98669d9074ec529cdc9261c71f [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
ths26edcf42007-12-09 02:25:24 +0000446static inline abi_long copy_from_user_fdset(fd_set *fds,
447 abi_ulong target_fds_addr,
448 int n)
bellard31e31b82003-02-18 22:55:36 +0000449{
ths26edcf42007-12-09 02:25:24 +0000450 int i, nw, j, k;
451 abi_ulong b, *target_fds;
452
453 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
454 if (!(target_fds = lock_user(VERIFY_READ,
455 target_fds_addr,
456 sizeof(abi_ulong) * nw,
457 1)))
458 return -TARGET_EFAULT;
459
460 FD_ZERO(fds);
461 k = 0;
462 for (i = 0; i < nw; i++) {
463 /* grab the abi_ulong */
464 __get_user(b, &target_fds[i]);
465 for (j = 0; j < TARGET_ABI_BITS; j++) {
466 /* check the bit inside the abi_ulong */
467 if ((b >> j) & 1)
468 FD_SET(k, fds);
469 k++;
bellard31e31b82003-02-18 22:55:36 +0000470 }
bellard31e31b82003-02-18 22:55:36 +0000471 }
ths26edcf42007-12-09 02:25:24 +0000472
473 unlock_user(target_fds, target_fds_addr, 0);
474
475 return 0;
bellard31e31b82003-02-18 22:55:36 +0000476}
477
ths26edcf42007-12-09 02:25:24 +0000478static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
479 const fd_set *fds,
480 int n)
bellard31e31b82003-02-18 22:55:36 +0000481{
bellard31e31b82003-02-18 22:55:36 +0000482 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000483 abi_long v;
ths26edcf42007-12-09 02:25:24 +0000484 abi_ulong *target_fds;
bellard31e31b82003-02-18 22:55:36 +0000485
ths26edcf42007-12-09 02:25:24 +0000486 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
487 if (!(target_fds = lock_user(VERIFY_WRITE,
488 target_fds_addr,
489 sizeof(abi_ulong) * nw,
490 0)))
491 return -TARGET_EFAULT;
492
493 k = 0;
494 for (i = 0; i < nw; i++) {
495 v = 0;
496 for (j = 0; j < TARGET_ABI_BITS; j++) {
497 v |= ((FD_ISSET(k, fds) != 0) << j);
498 k++;
bellard31e31b82003-02-18 22:55:36 +0000499 }
ths26edcf42007-12-09 02:25:24 +0000500 __put_user(v, &target_fds[i]);
bellard31e31b82003-02-18 22:55:36 +0000501 }
ths26edcf42007-12-09 02:25:24 +0000502
503 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
504
505 return 0;
bellard31e31b82003-02-18 22:55:36 +0000506}
507
bellardc596ed12003-07-13 17:32:31 +0000508#if defined(__alpha__)
509#define HOST_HZ 1024
510#else
511#define HOST_HZ 100
512#endif
513
blueswir1992f48a2007-10-14 16:27:31 +0000514static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000515{
516#if HOST_HZ == TARGET_HZ
517 return ticks;
518#else
519 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
520#endif
521}
522
bellard579a97f2007-11-11 14:26:47 +0000523static inline abi_long host_to_target_rusage(abi_ulong target_addr,
524 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000525{
pbrook53a59602006-03-25 19:31:22 +0000526 struct target_rusage *target_rusage;
527
bellard579a97f2007-11-11 14:26:47 +0000528 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
529 return -TARGET_EFAULT;
bellardb4091862003-05-16 15:39:34 +0000530 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
531 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
532 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
533 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
534 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
535 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
536 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
537 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
538 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
539 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
540 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
541 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
542 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
543 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
544 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
545 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
546 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
547 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000548 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000549
550 return 0;
bellardb4091862003-05-16 15:39:34 +0000551}
552
bellard579a97f2007-11-11 14:26:47 +0000553static inline abi_long target_to_host_timeval(struct timeval *tv,
554 abi_ulong target_addr)
bellard31e31b82003-02-18 22:55:36 +0000555{
pbrook53a59602006-03-25 19:31:22 +0000556 struct target_timeval *target_tv;
557
bellard579a97f2007-11-11 14:26:47 +0000558 if (!lock_user_struct(VERIFY_READ, target_tv, target_addr, 1))
559 return -TARGET_EFAULT;
bellard66fb9762003-03-23 01:06:05 +0000560 tv->tv_sec = tswapl(target_tv->tv_sec);
561 tv->tv_usec = tswapl(target_tv->tv_usec);
pbrook53a59602006-03-25 19:31:22 +0000562 unlock_user_struct(target_tv, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000563
564 return 0;
bellard31e31b82003-02-18 22:55:36 +0000565}
566
bellard579a97f2007-11-11 14:26:47 +0000567static inline abi_long host_to_target_timeval(abi_ulong target_addr,
568 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +0000569{
pbrook53a59602006-03-25 19:31:22 +0000570 struct target_timeval *target_tv;
571
bellard579a97f2007-11-11 14:26:47 +0000572 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_addr, 0))
573 return -TARGET_EFAULT;
bellard66fb9762003-03-23 01:06:05 +0000574 target_tv->tv_sec = tswapl(tv->tv_sec);
575 target_tv->tv_usec = tswapl(tv->tv_usec);
pbrook53a59602006-03-25 19:31:22 +0000576 unlock_user_struct(target_tv, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000577
578 return 0;
bellard31e31b82003-02-18 22:55:36 +0000579}
580
581
ths0da46a62007-10-20 20:23:07 +0000582/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000583static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +0000584 abi_ulong rfd_addr, abi_ulong wfd_addr,
585 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000586{
587 fd_set rfds, wfds, efds;
588 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
589 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +0000590 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +0000591
ths26edcf42007-12-09 02:25:24 +0000592 if (rfd_addr) {
593 if (copy_from_user_fdset(&rfds, rfd_addr, n))
594 return -TARGET_EFAULT;
595 rfds_ptr = &rfds;
pbrook53a59602006-03-25 19:31:22 +0000596 } else {
pbrook53a59602006-03-25 19:31:22 +0000597 rfds_ptr = NULL;
598 }
ths26edcf42007-12-09 02:25:24 +0000599 if (wfd_addr) {
600 if (copy_from_user_fdset(&wfds, wfd_addr, n))
601 return -TARGET_EFAULT;
602 wfds_ptr = &wfds;
pbrook53a59602006-03-25 19:31:22 +0000603 } else {
pbrook53a59602006-03-25 19:31:22 +0000604 wfds_ptr = NULL;
605 }
ths26edcf42007-12-09 02:25:24 +0000606 if (efd_addr) {
607 if (copy_from_user_fdset(&efds, efd_addr, n))
608 return -TARGET_EFAULT;
609 efds_ptr = &efds;
pbrook53a59602006-03-25 19:31:22 +0000610 } else {
pbrook53a59602006-03-25 19:31:22 +0000611 efds_ptr = NULL;
612 }
ths3b46e622007-09-17 08:09:54 +0000613
ths26edcf42007-12-09 02:25:24 +0000614 if (target_tv_addr) {
615 target_to_host_timeval(&tv, target_tv_addr);
bellard31e31b82003-02-18 22:55:36 +0000616 tv_ptr = &tv;
617 } else {
618 tv_ptr = NULL;
619 }
ths26edcf42007-12-09 02:25:24 +0000620
bellard31e31b82003-02-18 22:55:36 +0000621 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +0000622
ths26edcf42007-12-09 02:25:24 +0000623 if (!is_error(ret)) {
624 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
625 return -TARGET_EFAULT;
626 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
627 return -TARGET_EFAULT;
628 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
629 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000630
ths26edcf42007-12-09 02:25:24 +0000631 if (target_tv_addr)
632 host_to_target_timeval(target_tv_addr, &tv);
bellard31e31b82003-02-18 22:55:36 +0000633 }
bellard579a97f2007-11-11 14:26:47 +0000634
bellard31e31b82003-02-18 22:55:36 +0000635 return ret;
636}
637
bellard579a97f2007-11-11 14:26:47 +0000638static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
639 abi_ulong target_addr,
640 socklen_t len)
bellard7854b052003-03-29 17:22:23 +0000641{
pbrook53a59602006-03-25 19:31:22 +0000642 struct target_sockaddr *target_saddr;
643
bellard579a97f2007-11-11 14:26:47 +0000644 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
645 if (!target_saddr)
646 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +0000647 memcpy(addr, target_saddr, len);
648 addr->sa_family = tswap16(target_saddr->sa_family);
649 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000650
651 return 0;
bellard7854b052003-03-29 17:22:23 +0000652}
653
bellard579a97f2007-11-11 14:26:47 +0000654static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
655 struct sockaddr *addr,
656 socklen_t len)
bellard7854b052003-03-29 17:22:23 +0000657{
pbrook53a59602006-03-25 19:31:22 +0000658 struct target_sockaddr *target_saddr;
659
bellard579a97f2007-11-11 14:26:47 +0000660 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
661 if (!target_saddr)
662 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +0000663 memcpy(target_saddr, addr, len);
664 target_saddr->sa_family = tswap16(addr->sa_family);
665 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +0000666
667 return 0;
bellard7854b052003-03-29 17:22:23 +0000668}
669
pbrook53a59602006-03-25 19:31:22 +0000670/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +0000671static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
672 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +0000673{
674 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +0000675 abi_long msg_controllen;
676 abi_ulong target_cmsg_addr;
677 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +0000678 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +0000679
680 msg_controllen = tswapl(target_msgh->msg_controllen);
681 if (msg_controllen < sizeof (struct target_cmsghdr))
682 goto the_end;
683 target_cmsg_addr = tswapl(target_msgh->msg_control);
684 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
685 if (!target_cmsg)
686 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +0000687
688 while (cmsg && target_cmsg) {
689 void *data = CMSG_DATA(cmsg);
690 void *target_data = TARGET_CMSG_DATA(target_cmsg);
691
ths5fafdf22007-09-16 21:08:06 +0000692 int len = tswapl(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +0000693 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
694
695 space += CMSG_SPACE(len);
696 if (space > msgh->msg_controllen) {
697 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +0000698 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +0000699 break;
700 }
701
702 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
703 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
704 cmsg->cmsg_len = CMSG_LEN(len);
705
bellard3532fa72006-06-24 15:06:03 +0000706 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +0000707 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
708 memcpy(data, target_data, len);
709 } else {
710 int *fd = (int *)data;
711 int *target_fd = (int *)target_data;
712 int i, numfds = len / sizeof(int);
713
714 for (i = 0; i < numfds; i++)
715 fd[i] = tswap32(target_fd[i]);
716 }
717
718 cmsg = CMSG_NXTHDR(msgh, cmsg);
719 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
720 }
bellard5a4a8982007-11-11 17:39:18 +0000721 unlock_user(target_cmsg, target_cmsg_addr, 0);
722 the_end:
bellard7854b052003-03-29 17:22:23 +0000723 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +0000724 return 0;
bellard7854b052003-03-29 17:22:23 +0000725}
726
pbrook53a59602006-03-25 19:31:22 +0000727/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +0000728static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
729 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +0000730{
731 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +0000732 abi_long msg_controllen;
733 abi_ulong target_cmsg_addr;
734 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +0000735 socklen_t space = 0;
736
bellard5a4a8982007-11-11 17:39:18 +0000737 msg_controllen = tswapl(target_msgh->msg_controllen);
738 if (msg_controllen < sizeof (struct target_cmsghdr))
739 goto the_end;
740 target_cmsg_addr = tswapl(target_msgh->msg_control);
741 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
742 if (!target_cmsg)
743 return -TARGET_EFAULT;
744
bellard7854b052003-03-29 17:22:23 +0000745 while (cmsg && target_cmsg) {
746 void *data = CMSG_DATA(cmsg);
747 void *target_data = TARGET_CMSG_DATA(target_cmsg);
748
749 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
750
751 space += TARGET_CMSG_SPACE(len);
bellard5a4a8982007-11-11 17:39:18 +0000752 if (space > msg_controllen) {
bellard7854b052003-03-29 17:22:23 +0000753 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +0000754 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +0000755 break;
756 }
757
758 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
759 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
760 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
761
bellard3532fa72006-06-24 15:06:03 +0000762 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +0000763 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
764 memcpy(target_data, data, len);
765 } else {
766 int *fd = (int *)data;
767 int *target_fd = (int *)target_data;
768 int i, numfds = len / sizeof(int);
769
770 for (i = 0; i < numfds; i++)
771 target_fd[i] = tswap32(fd[i]);
772 }
773
774 cmsg = CMSG_NXTHDR(msgh, cmsg);
775 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
776 }
bellard5a4a8982007-11-11 17:39:18 +0000777 unlock_user(target_cmsg, target_cmsg_addr, space);
778 the_end:
779 target_msgh->msg_controllen = tswapl(space);
780 return 0;
bellard7854b052003-03-29 17:22:23 +0000781}
782
ths0da46a62007-10-20 20:23:07 +0000783/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000784static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +0000785 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +0000786{
blueswir1992f48a2007-10-14 16:27:31 +0000787 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +0000788 int val;
ths3b46e622007-09-17 08:09:54 +0000789
bellard8853f862004-02-22 14:57:26 +0000790 switch(level) {
791 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +0000792 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +0000793 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +0000794 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +0000795
bellard2f619692007-11-16 10:46:05 +0000796 if (get_user_u32(val, optval_addr))
797 return -TARGET_EFAULT;
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)) {
bellard2f619692007-11-16 10:46:05 +0000819 if (get_user_u32(val, optval_addr))
820 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +0000821 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +0000822 if (get_user_u8(val, optval_addr))
823 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +0000824 }
825 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
826 break;
827 default:
828 goto unimplemented;
829 }
830 break;
bellard3532fa72006-06-24 15:06:03 +0000831 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +0000832 switch (optname) {
833 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +0000834 case TARGET_SO_DEBUG:
835 optname = SO_DEBUG;
836 break;
837 case TARGET_SO_REUSEADDR:
838 optname = SO_REUSEADDR;
839 break;
840 case TARGET_SO_TYPE:
841 optname = SO_TYPE;
842 break;
843 case TARGET_SO_ERROR:
844 optname = SO_ERROR;
845 break;
846 case TARGET_SO_DONTROUTE:
847 optname = SO_DONTROUTE;
848 break;
849 case TARGET_SO_BROADCAST:
850 optname = SO_BROADCAST;
851 break;
852 case TARGET_SO_SNDBUF:
853 optname = SO_SNDBUF;
854 break;
855 case TARGET_SO_RCVBUF:
856 optname = SO_RCVBUF;
857 break;
858 case TARGET_SO_KEEPALIVE:
859 optname = SO_KEEPALIVE;
860 break;
861 case TARGET_SO_OOBINLINE:
862 optname = SO_OOBINLINE;
863 break;
864 case TARGET_SO_NO_CHECK:
865 optname = SO_NO_CHECK;
866 break;
867 case TARGET_SO_PRIORITY:
868 optname = SO_PRIORITY;
869 break;
bellard5e83e8e2005-03-01 22:32:06 +0000870#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +0000871 case TARGET_SO_BSDCOMPAT:
872 optname = SO_BSDCOMPAT;
873 break;
bellard5e83e8e2005-03-01 22:32:06 +0000874#endif
bellard3532fa72006-06-24 15:06:03 +0000875 case TARGET_SO_PASSCRED:
876 optname = SO_PASSCRED;
877 break;
878 case TARGET_SO_TIMESTAMP:
879 optname = SO_TIMESTAMP;
880 break;
881 case TARGET_SO_RCVLOWAT:
882 optname = SO_RCVLOWAT;
883 break;
884 case TARGET_SO_RCVTIMEO:
885 optname = SO_RCVTIMEO;
886 break;
887 case TARGET_SO_SNDTIMEO:
888 optname = SO_SNDTIMEO;
889 break;
bellard8853f862004-02-22 14:57:26 +0000890 break;
891 default:
892 goto unimplemented;
893 }
bellard3532fa72006-06-24 15:06:03 +0000894 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +0000895 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +0000896
bellard2f619692007-11-16 10:46:05 +0000897 if (get_user_u32(val, optval_addr))
898 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +0000899 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +0000900 break;
bellard7854b052003-03-29 17:22:23 +0000901 default:
bellard8853f862004-02-22 14:57:26 +0000902 unimplemented:
903 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
ths0da46a62007-10-20 20:23:07 +0000904 ret = -TARGET_ENOSYS;
bellard7854b052003-03-29 17:22:23 +0000905 }
bellard8853f862004-02-22 14:57:26 +0000906 return ret;
bellard7854b052003-03-29 17:22:23 +0000907}
908
ths0da46a62007-10-20 20:23:07 +0000909/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000910static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +0000911 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +0000912{
blueswir1992f48a2007-10-14 16:27:31 +0000913 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +0000914 int len, lv, val;
bellard8853f862004-02-22 14:57:26 +0000915
916 switch(level) {
bellard3532fa72006-06-24 15:06:03 +0000917 case TARGET_SOL_SOCKET:
918 level = SOL_SOCKET;
bellard8853f862004-02-22 14:57:26 +0000919 switch (optname) {
bellard3532fa72006-06-24 15:06:03 +0000920 case TARGET_SO_LINGER:
921 case TARGET_SO_RCVTIMEO:
922 case TARGET_SO_SNDTIMEO:
923 case TARGET_SO_PEERCRED:
924 case TARGET_SO_PEERNAME:
bellard8853f862004-02-22 14:57:26 +0000925 /* These don't just return a single integer */
926 goto unimplemented;
927 default:
bellard2efbe912005-07-23 15:10:20 +0000928 goto int_case;
929 }
930 break;
931 case SOL_TCP:
932 /* TCP options all take an 'int' value. */
933 int_case:
bellard2f619692007-11-16 10:46:05 +0000934 if (get_user_u32(len, optlen))
935 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +0000936 if (len < 0)
ths0da46a62007-10-20 20:23:07 +0000937 return -TARGET_EINVAL;
bellard2efbe912005-07-23 15:10:20 +0000938 lv = sizeof(int);
939 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
940 if (ret < 0)
941 return ret;
942 val = tswap32(val);
943 if (len > lv)
944 len = lv;
bellard2f619692007-11-16 10:46:05 +0000945 if (len == 4) {
946 if (put_user_u32(val, optval_addr))
947 return -TARGET_EFAULT;
948 } else {
949 if (put_user_u8(val, optval_addr))
950 return -TARGET_EFAULT;
951 }
952 if (put_user_u32(len, optlen))
953 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +0000954 break;
955 case SOL_IP:
956 switch(optname) {
957 case IP_TOS:
958 case IP_TTL:
959 case IP_HDRINCL:
960 case IP_ROUTER_ALERT:
961 case IP_RECVOPTS:
962 case IP_RETOPTS:
963 case IP_PKTINFO:
964 case IP_MTU_DISCOVER:
965 case IP_RECVERR:
966 case IP_RECVTOS:
967#ifdef IP_FREEBIND
968 case IP_FREEBIND:
969#endif
970 case IP_MULTICAST_TTL:
971 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +0000972 if (get_user_u32(len, optlen))
973 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +0000974 if (len < 0)
ths0da46a62007-10-20 20:23:07 +0000975 return -TARGET_EINVAL;
bellard8853f862004-02-22 14:57:26 +0000976 lv = sizeof(int);
977 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
978 if (ret < 0)
979 return ret;
bellard2efbe912005-07-23 15:10:20 +0000980 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +0000981 len = 1;
bellard2f619692007-11-16 10:46:05 +0000982 if (put_user_u32(len, optlen)
983 || put_user_u8(val, optval_addr))
984 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +0000985 } else {
bellard2efbe912005-07-23 15:10:20 +0000986 if (len > sizeof(int))
987 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +0000988 if (put_user_u32(len, optlen)
989 || put_user_u32(val, optval_addr))
990 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +0000991 }
bellard8853f862004-02-22 14:57:26 +0000992 break;
bellard2efbe912005-07-23 15:10:20 +0000993 default:
994 goto unimplemented;
bellard8853f862004-02-22 14:57:26 +0000995 }
996 break;
997 default:
998 unimplemented:
999 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1000 level, optname);
ths0da46a62007-10-20 20:23:07 +00001001 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00001002 break;
1003 }
1004 return ret;
bellard7854b052003-03-29 17:22:23 +00001005}
1006
bellard579a97f2007-11-11 14:26:47 +00001007/* FIXME
1008 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1009 * other lock functions have a return code of 0 for failure.
1010 */
1011static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1012 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001013{
1014 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001015 abi_ulong base;
bellard579a97f2007-11-11 14:26:47 +00001016 int i, j;
pbrook53a59602006-03-25 19:31:22 +00001017
bellard579a97f2007-11-11 14:26:47 +00001018 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1019 if (!target_vec)
1020 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001021 for(i = 0;i < count; i++) {
1022 base = tswapl(target_vec[i].iov_base);
1023 vec[i].iov_len = tswapl(target_vec[i].iov_len);
bellard579a97f2007-11-11 14:26:47 +00001024 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
1025 if (!vec[i].iov_base)
1026 goto fail;
pbrook53a59602006-03-25 19:31:22 +00001027 }
1028 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001029 return 0;
1030 fail:
1031 /* failure - unwind locks */
1032 for (j = 0; j < i; j++) {
1033 base = tswapl(target_vec[j].iov_base);
1034 unlock_user(vec[j].iov_base, base, 0);
1035 }
1036 unlock_user (target_vec, target_addr, 0);
1037 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001038}
1039
bellard579a97f2007-11-11 14:26:47 +00001040static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1041 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001042{
1043 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001044 abi_ulong base;
pbrook53a59602006-03-25 19:31:22 +00001045 int i;
1046
bellard579a97f2007-11-11 14:26:47 +00001047 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1048 if (!target_vec)
1049 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001050 for(i = 0;i < count; i++) {
1051 base = tswapl(target_vec[i].iov_base);
1052 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1053 }
1054 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001055
1056 return 0;
pbrook53a59602006-03-25 19:31:22 +00001057}
1058
ths0da46a62007-10-20 20:23:07 +00001059/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001060static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00001061{
1062#if defined(TARGET_MIPS)
1063 switch(type) {
1064 case TARGET_SOCK_DGRAM:
1065 type = SOCK_DGRAM;
1066 break;
1067 case TARGET_SOCK_STREAM:
1068 type = SOCK_STREAM;
1069 break;
1070 case TARGET_SOCK_RAW:
1071 type = SOCK_RAW;
1072 break;
1073 case TARGET_SOCK_RDM:
1074 type = SOCK_RDM;
1075 break;
1076 case TARGET_SOCK_SEQPACKET:
1077 type = SOCK_SEQPACKET;
1078 break;
1079 case TARGET_SOCK_PACKET:
1080 type = SOCK_PACKET;
1081 break;
1082 }
1083#endif
balrog12bc92a2007-10-30 21:06:14 +00001084 if (domain == PF_NETLINK)
1085 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
bellard3532fa72006-06-24 15:06:03 +00001086 return get_errno(socket(domain, type, protocol));
1087}
1088
ths0da46a62007-10-20 20:23:07 +00001089/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001090static abi_long do_bind(int sockfd, abi_ulong target_addr,
1091 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001092{
1093 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001094
bellard3532fa72006-06-24 15:06:03 +00001095 target_to_host_sockaddr(addr, target_addr, addrlen);
1096 return get_errno(bind(sockfd, addr, addrlen));
1097}
1098
ths0da46a62007-10-20 20:23:07 +00001099/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001100static abi_long do_connect(int sockfd, abi_ulong target_addr,
1101 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001102{
1103 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001104
bellard3532fa72006-06-24 15:06:03 +00001105 target_to_host_sockaddr(addr, target_addr, addrlen);
1106 return get_errno(connect(sockfd, addr, addrlen));
1107}
1108
ths0da46a62007-10-20 20:23:07 +00001109/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001110static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1111 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001112{
blueswir1992f48a2007-10-14 16:27:31 +00001113 abi_long ret;
bellard3532fa72006-06-24 15:06:03 +00001114 struct target_msghdr *msgp;
1115 struct msghdr msg;
1116 int count;
1117 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001118 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001119
bellard579a97f2007-11-11 14:26:47 +00001120 /* FIXME */
1121 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1122 msgp,
1123 target_msg,
1124 send ? 1 : 0))
1125 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001126 if (msgp->msg_name) {
1127 msg.msg_namelen = tswap32(msgp->msg_namelen);
1128 msg.msg_name = alloca(msg.msg_namelen);
1129 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1130 msg.msg_namelen);
1131 } else {
1132 msg.msg_name = NULL;
1133 msg.msg_namelen = 0;
1134 }
1135 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1136 msg.msg_control = alloca(msg.msg_controllen);
1137 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001138
bellard3532fa72006-06-24 15:06:03 +00001139 count = tswapl(msgp->msg_iovlen);
1140 vec = alloca(count * sizeof(struct iovec));
1141 target_vec = tswapl(msgp->msg_iov);
bellard579a97f2007-11-11 14:26:47 +00001142 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
bellard3532fa72006-06-24 15:06:03 +00001143 msg.msg_iovlen = count;
1144 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001145
bellard3532fa72006-06-24 15:06:03 +00001146 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00001147 ret = target_to_host_cmsg(&msg, msgp);
1148 if (ret == 0)
1149 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00001150 } else {
1151 ret = get_errno(recvmsg(fd, &msg, flags));
1152 if (!is_error(ret))
bellard5a4a8982007-11-11 17:39:18 +00001153 ret = host_to_target_cmsg(msgp, &msg);
bellard3532fa72006-06-24 15:06:03 +00001154 }
1155 unlock_iovec(vec, target_vec, count, !send);
bellard579a97f2007-11-11 14:26:47 +00001156 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00001157 return ret;
1158}
1159
ths0da46a62007-10-20 20:23:07 +00001160/* do_accept() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001161static abi_long do_accept(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001162 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001163{
bellard2f619692007-11-16 10:46:05 +00001164 socklen_t addrlen;
1165 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001166 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001167
bellard2f619692007-11-16 10:46:05 +00001168 if (get_user_u32(addrlen, target_addrlen_addr))
1169 return -TARGET_EFAULT;
1170
1171 addr = alloca(addrlen);
1172
pbrook1be9e1d2006-11-19 15:26:04 +00001173 ret = get_errno(accept(fd, addr, &addrlen));
1174 if (!is_error(ret)) {
1175 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001176 if (put_user_u32(addrlen, target_addrlen_addr))
1177 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001178 }
1179 return ret;
1180}
1181
ths0da46a62007-10-20 20:23:07 +00001182/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001183static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001184 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001185{
bellard2f619692007-11-16 10:46:05 +00001186 socklen_t addrlen;
1187 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001188 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001189
bellard2f619692007-11-16 10:46:05 +00001190 if (get_user_u32(addrlen, target_addrlen_addr))
1191 return -TARGET_EFAULT;
1192
1193 addr = alloca(addrlen);
1194
pbrook1be9e1d2006-11-19 15:26:04 +00001195 ret = get_errno(getpeername(fd, addr, &addrlen));
1196 if (!is_error(ret)) {
1197 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001198 if (put_user_u32(addrlen, target_addrlen_addr))
1199 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001200 }
1201 return ret;
1202}
1203
ths0da46a62007-10-20 20:23:07 +00001204/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001205static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001206 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001207{
bellard2f619692007-11-16 10:46:05 +00001208 socklen_t addrlen;
1209 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001210 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001211
bellard2f619692007-11-16 10:46:05 +00001212 if (get_user_u32(addrlen, target_addrlen_addr))
1213 return -TARGET_EFAULT;
1214
1215 addr = alloca(addrlen);
1216
pbrook1be9e1d2006-11-19 15:26:04 +00001217 ret = get_errno(getsockname(fd, addr, &addrlen));
1218 if (!is_error(ret)) {
1219 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001220 if (put_user_u32(addrlen, target_addrlen_addr))
1221 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001222 }
1223 return ret;
1224}
1225
ths0da46a62007-10-20 20:23:07 +00001226/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001227static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00001228 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001229{
1230 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00001231 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001232
1233 ret = get_errno(socketpair(domain, type, protocol, tab));
1234 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00001235 if (put_user_s32(tab[0], target_tab_addr)
1236 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1237 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001238 }
1239 return ret;
1240}
1241
ths0da46a62007-10-20 20:23:07 +00001242/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001243static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1244 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001245{
1246 void *addr;
1247 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001248 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001249
bellard579a97f2007-11-11 14:26:47 +00001250 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1251 if (!host_msg)
1252 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001253 if (target_addr) {
1254 addr = alloca(addrlen);
1255 target_to_host_sockaddr(addr, target_addr, addrlen);
1256 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1257 } else {
1258 ret = get_errno(send(fd, host_msg, len, flags));
1259 }
1260 unlock_user(host_msg, msg, 0);
1261 return ret;
1262}
1263
ths0da46a62007-10-20 20:23:07 +00001264/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001265static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1266 abi_ulong target_addr,
1267 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001268{
1269 socklen_t addrlen;
1270 void *addr;
1271 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001272 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001273
bellard579a97f2007-11-11 14:26:47 +00001274 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1275 if (!host_msg)
1276 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001277 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00001278 if (get_user_u32(addrlen, target_addrlen)) {
1279 ret = -TARGET_EFAULT;
1280 goto fail;
1281 }
pbrook1be9e1d2006-11-19 15:26:04 +00001282 addr = alloca(addrlen);
1283 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1284 } else {
1285 addr = NULL; /* To keep compiler quiet. */
1286 ret = get_errno(recv(fd, host_msg, len, flags));
1287 }
1288 if (!is_error(ret)) {
1289 if (target_addr) {
1290 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001291 if (put_user_u32(addrlen, target_addrlen)) {
1292 ret = -TARGET_EFAULT;
1293 goto fail;
1294 }
pbrook1be9e1d2006-11-19 15:26:04 +00001295 }
1296 unlock_user(host_msg, msg, len);
1297 } else {
bellard2f619692007-11-16 10:46:05 +00001298fail:
pbrook1be9e1d2006-11-19 15:26:04 +00001299 unlock_user(host_msg, msg, 0);
1300 }
1301 return ret;
1302}
1303
j_mayer32407102007-09-26 23:01:49 +00001304#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00001305/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001306static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00001307{
blueswir1992f48a2007-10-14 16:27:31 +00001308 abi_long ret;
1309 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00001310
1311 switch(num) {
1312 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00001313 {
bellard2f619692007-11-16 10:46:05 +00001314 int domain, type, protocol;
1315
1316 if (get_user_s32(domain, vptr)
1317 || get_user_s32(type, vptr + n)
1318 || get_user_s32(protocol, vptr + 2 * n))
1319 return -TARGET_EFAULT;
1320
bellard3532fa72006-06-24 15:06:03 +00001321 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00001322 }
bellard31e31b82003-02-18 22:55:36 +00001323 break;
1324 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00001325 {
bellard2f619692007-11-16 10:46:05 +00001326 int sockfd;
1327 abi_ulong target_addr;
1328 socklen_t addrlen;
1329
1330 if (get_user_s32(sockfd, vptr)
1331 || get_user_ual(target_addr, vptr + n)
1332 || get_user_u32(addrlen, vptr + 2 * n))
1333 return -TARGET_EFAULT;
1334
bellard3532fa72006-06-24 15:06:03 +00001335 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001336 }
bellard31e31b82003-02-18 22:55:36 +00001337 break;
1338 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00001339 {
bellard2f619692007-11-16 10:46:05 +00001340 int sockfd;
1341 abi_ulong target_addr;
1342 socklen_t addrlen;
1343
1344 if (get_user_s32(sockfd, vptr)
1345 || get_user_ual(target_addr, vptr + n)
1346 || get_user_u32(addrlen, vptr + 2 * n))
1347 return -TARGET_EFAULT;
1348
bellard3532fa72006-06-24 15:06:03 +00001349 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001350 }
bellard31e31b82003-02-18 22:55:36 +00001351 break;
1352 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00001353 {
bellard2f619692007-11-16 10:46:05 +00001354 int sockfd, backlog;
1355
1356 if (get_user_s32(sockfd, vptr)
1357 || get_user_s32(backlog, vptr + n))
1358 return -TARGET_EFAULT;
1359
bellard7854b052003-03-29 17:22:23 +00001360 ret = get_errno(listen(sockfd, backlog));
1361 }
bellard31e31b82003-02-18 22:55:36 +00001362 break;
1363 case SOCKOP_accept:
1364 {
bellard2f619692007-11-16 10:46:05 +00001365 int sockfd;
1366 abi_ulong target_addr, target_addrlen;
1367
1368 if (get_user_s32(sockfd, vptr)
1369 || get_user_ual(target_addr, vptr + n)
1370 || get_user_u32(target_addrlen, vptr + 2 * n))
1371 return -TARGET_EFAULT;
1372
pbrook1be9e1d2006-11-19 15:26:04 +00001373 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001374 }
1375 break;
1376 case SOCKOP_getsockname:
1377 {
bellard2f619692007-11-16 10:46:05 +00001378 int sockfd;
1379 abi_ulong target_addr, target_addrlen;
1380
1381 if (get_user_s32(sockfd, vptr)
1382 || get_user_ual(target_addr, vptr + n)
1383 || get_user_u32(target_addrlen, vptr + 2 * n))
1384 return -TARGET_EFAULT;
1385
pbrook1be9e1d2006-11-19 15:26:04 +00001386 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001387 }
1388 break;
1389 case SOCKOP_getpeername:
1390 {
bellard2f619692007-11-16 10:46:05 +00001391 int sockfd;
1392 abi_ulong target_addr, target_addrlen;
1393
1394 if (get_user_s32(sockfd, vptr)
1395 || get_user_ual(target_addr, vptr + n)
1396 || get_user_u32(target_addrlen, vptr + 2 * n))
1397 return -TARGET_EFAULT;
1398
pbrook1be9e1d2006-11-19 15:26:04 +00001399 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001400 }
1401 break;
1402 case SOCKOP_socketpair:
1403 {
bellard2f619692007-11-16 10:46:05 +00001404 int domain, type, protocol;
1405 abi_ulong tab;
1406
1407 if (get_user_s32(domain, vptr)
1408 || get_user_s32(type, vptr + n)
1409 || get_user_s32(protocol, vptr + 2 * n)
1410 || get_user_ual(tab, vptr + 3 * n))
1411 return -TARGET_EFAULT;
1412
pbrook1be9e1d2006-11-19 15:26:04 +00001413 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00001414 }
1415 break;
1416 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00001417 {
bellard2f619692007-11-16 10:46:05 +00001418 int sockfd;
1419 abi_ulong msg;
1420 size_t len;
1421 int flags;
1422
1423 if (get_user_s32(sockfd, vptr)
1424 || get_user_ual(msg, vptr + n)
1425 || get_user_ual(len, vptr + 2 * n)
1426 || get_user_s32(flags, vptr + 3 * n))
1427 return -TARGET_EFAULT;
1428
pbrook1be9e1d2006-11-19 15:26:04 +00001429 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001430 }
bellard31e31b82003-02-18 22:55:36 +00001431 break;
1432 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00001433 {
bellard2f619692007-11-16 10:46:05 +00001434 int sockfd;
1435 abi_ulong msg;
1436 size_t len;
1437 int flags;
1438
1439 if (get_user_s32(sockfd, vptr)
1440 || get_user_ual(msg, vptr + n)
1441 || get_user_ual(len, vptr + 2 * n)
1442 || get_user_s32(flags, vptr + 3 * n))
1443 return -TARGET_EFAULT;
1444
pbrook1be9e1d2006-11-19 15:26:04 +00001445 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001446 }
bellard31e31b82003-02-18 22:55:36 +00001447 break;
1448 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00001449 {
bellard2f619692007-11-16 10:46:05 +00001450 int sockfd;
1451 abi_ulong msg;
1452 size_t len;
1453 int flags;
1454 abi_ulong addr;
1455 socklen_t addrlen;
1456
1457 if (get_user_s32(sockfd, vptr)
1458 || get_user_ual(msg, vptr + n)
1459 || get_user_ual(len, vptr + 2 * n)
1460 || get_user_s32(flags, vptr + 3 * n)
1461 || get_user_ual(addr, vptr + 4 * n)
1462 || get_user_u32(addrlen, vptr + 5 * n))
1463 return -TARGET_EFAULT;
1464
pbrook1be9e1d2006-11-19 15:26:04 +00001465 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001466 }
bellard31e31b82003-02-18 22:55:36 +00001467 break;
1468 case SOCKOP_recvfrom:
1469 {
bellard2f619692007-11-16 10:46:05 +00001470 int sockfd;
1471 abi_ulong msg;
1472 size_t len;
1473 int flags;
1474 abi_ulong addr;
1475 socklen_t addrlen;
1476
1477 if (get_user_s32(sockfd, vptr)
1478 || get_user_ual(msg, vptr + n)
1479 || get_user_ual(len, vptr + 2 * n)
1480 || get_user_s32(flags, vptr + 3 * n)
1481 || get_user_ual(addr, vptr + 4 * n)
1482 || get_user_u32(addrlen, vptr + 5 * n))
1483 return -TARGET_EFAULT;
1484
pbrook1be9e1d2006-11-19 15:26:04 +00001485 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00001486 }
1487 break;
1488 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00001489 {
bellard2f619692007-11-16 10:46:05 +00001490 int sockfd, how;
1491
1492 if (get_user_s32(sockfd, vptr)
1493 || get_user_s32(how, vptr + n))
1494 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001495
1496 ret = get_errno(shutdown(sockfd, how));
1497 }
bellard31e31b82003-02-18 22:55:36 +00001498 break;
1499 case SOCKOP_sendmsg:
1500 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00001501 {
1502 int fd;
blueswir1992f48a2007-10-14 16:27:31 +00001503 abi_ulong target_msg;
bellard3532fa72006-06-24 15:06:03 +00001504 int flags;
bellard1a9353d2003-03-16 20:28:50 +00001505
bellard2f619692007-11-16 10:46:05 +00001506 if (get_user_s32(fd, vptr)
1507 || get_user_ual(target_msg, vptr + n)
1508 || get_user_s32(flags, vptr + 2 * n))
1509 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001510
ths5fafdf22007-09-16 21:08:06 +00001511 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00001512 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00001513 }
1514 break;
bellard31e31b82003-02-18 22:55:36 +00001515 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00001516 {
bellard2f619692007-11-16 10:46:05 +00001517 int sockfd;
1518 int level;
1519 int optname;
1520 abi_ulong optval;
1521 socklen_t optlen;
1522
1523 if (get_user_s32(sockfd, vptr)
1524 || get_user_s32(level, vptr + n)
1525 || get_user_s32(optname, vptr + 2 * n)
1526 || get_user_ual(optval, vptr + 3 * n)
1527 || get_user_u32(optlen, vptr + 4 * n))
1528 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001529
1530 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1531 }
1532 break;
bellard31e31b82003-02-18 22:55:36 +00001533 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00001534 {
bellard2f619692007-11-16 10:46:05 +00001535 int sockfd;
1536 int level;
1537 int optname;
1538 abi_ulong optval;
1539 socklen_t optlen;
bellard7854b052003-03-29 17:22:23 +00001540
bellard2f619692007-11-16 10:46:05 +00001541 if (get_user_s32(sockfd, vptr)
1542 || get_user_s32(level, vptr + n)
1543 || get_user_s32(optname, vptr + 2 * n)
1544 || get_user_ual(optval, vptr + 3 * n)
1545 || get_user_u32(optlen, vptr + 4 * n))
1546 return -TARGET_EFAULT;
1547
1548 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
bellard7854b052003-03-29 17:22:23 +00001549 }
1550 break;
bellard31e31b82003-02-18 22:55:36 +00001551 default:
1552 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00001553 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00001554 break;
1555 }
1556 return ret;
1557}
j_mayer32407102007-09-26 23:01:49 +00001558#endif
bellard31e31b82003-02-18 22:55:36 +00001559
j_mayer32407102007-09-26 23:01:49 +00001560#ifdef TARGET_NR_ipc
bellard8853f862004-02-22 14:57:26 +00001561#define N_SHM_REGIONS 32
1562
1563static struct shm_region {
bellard5a4a8982007-11-11 17:39:18 +00001564 abi_ulong start;
1565 abi_ulong size;
bellard8853f862004-02-22 14:57:26 +00001566} shm_regions[N_SHM_REGIONS];
1567
ths3eb6b042007-06-03 14:26:27 +00001568struct target_ipc_perm
1569{
blueswir1992f48a2007-10-14 16:27:31 +00001570 abi_long __key;
1571 abi_ulong uid;
1572 abi_ulong gid;
1573 abi_ulong cuid;
1574 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00001575 unsigned short int mode;
1576 unsigned short int __pad1;
1577 unsigned short int __seq;
1578 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00001579 abi_ulong __unused1;
1580 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00001581};
1582
1583struct target_semid_ds
1584{
1585 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001586 abi_ulong sem_otime;
1587 abi_ulong __unused1;
1588 abi_ulong sem_ctime;
1589 abi_ulong __unused2;
1590 abi_ulong sem_nsems;
1591 abi_ulong __unused3;
1592 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00001593};
1594
bellard579a97f2007-11-11 14:26:47 +00001595static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
1596 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001597{
1598 struct target_ipc_perm *target_ip;
1599 struct target_semid_ds *target_sd;
1600
bellard579a97f2007-11-11 14:26:47 +00001601 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1602 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001603 target_ip=&(target_sd->sem_perm);
1604 host_ip->__key = tswapl(target_ip->__key);
1605 host_ip->uid = tswapl(target_ip->uid);
1606 host_ip->gid = tswapl(target_ip->gid);
1607 host_ip->cuid = tswapl(target_ip->cuid);
1608 host_ip->cgid = tswapl(target_ip->cgid);
1609 host_ip->mode = tswapl(target_ip->mode);
1610 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001611 return 0;
ths3eb6b042007-06-03 14:26:27 +00001612}
1613
bellard579a97f2007-11-11 14:26:47 +00001614static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
1615 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00001616{
1617 struct target_ipc_perm *target_ip;
1618 struct target_semid_ds *target_sd;
1619
bellard579a97f2007-11-11 14:26:47 +00001620 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1621 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001622 target_ip = &(target_sd->sem_perm);
1623 target_ip->__key = tswapl(host_ip->__key);
1624 target_ip->uid = tswapl(host_ip->uid);
1625 target_ip->gid = tswapl(host_ip->gid);
1626 target_ip->cuid = tswapl(host_ip->cuid);
1627 target_ip->cgid = tswapl(host_ip->cgid);
1628 target_ip->mode = tswapl(host_ip->mode);
1629 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001630 return 0;
ths3eb6b042007-06-03 14:26:27 +00001631}
1632
bellard579a97f2007-11-11 14:26:47 +00001633static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
1634 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001635{
1636 struct target_semid_ds *target_sd;
1637
bellard579a97f2007-11-11 14:26:47 +00001638 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1639 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001640 target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1641 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1642 host_sd->sem_otime = tswapl(target_sd->sem_otime);
1643 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1644 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001645 return 0;
ths3eb6b042007-06-03 14:26:27 +00001646}
1647
bellard579a97f2007-11-11 14:26:47 +00001648static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
1649 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00001650{
1651 struct target_semid_ds *target_sd;
1652
bellard579a97f2007-11-11 14:26:47 +00001653 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1654 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001655 host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1656 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1657 target_sd->sem_otime = tswapl(host_sd->sem_otime);
1658 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1659 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001660 return 0;
ths3eb6b042007-06-03 14:26:27 +00001661}
1662
thsfa294812007-02-02 22:05:00 +00001663union semun {
1664 int val;
ths3eb6b042007-06-03 14:26:27 +00001665 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00001666 unsigned short *array;
1667};
1668
ths3eb6b042007-06-03 14:26:27 +00001669union target_semun {
1670 int val;
blueswir1992f48a2007-10-14 16:27:31 +00001671 abi_long buf;
ths3eb6b042007-06-03 14:26:27 +00001672 unsigned short int *array;
1673};
1674
bellard579a97f2007-11-11 14:26:47 +00001675static inline abi_long target_to_host_semun(int cmd,
1676 union semun *host_su,
1677 abi_ulong target_addr,
1678 struct semid_ds *ds)
ths3eb6b042007-06-03 14:26:27 +00001679{
1680 union target_semun *target_su;
1681
1682 switch( cmd ) {
1683 case IPC_STAT:
1684 case IPC_SET:
bellard579a97f2007-11-11 14:26:47 +00001685 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1686 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001687 target_to_host_semid_ds(ds,target_su->buf);
1688 host_su->buf = ds;
1689 unlock_user_struct(target_su, target_addr, 0);
1690 break;
1691 case GETVAL:
1692 case SETVAL:
bellard579a97f2007-11-11 14:26:47 +00001693 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1694 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001695 host_su->val = tswapl(target_su->val);
1696 unlock_user_struct(target_su, target_addr, 0);
1697 break;
1698 case GETALL:
1699 case SETALL:
bellard579a97f2007-11-11 14:26:47 +00001700 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1701 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001702 *host_su->array = tswap16(*target_su->array);
1703 unlock_user_struct(target_su, target_addr, 0);
1704 break;
1705 default:
1706 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1707 }
bellard579a97f2007-11-11 14:26:47 +00001708 return 0;
ths3eb6b042007-06-03 14:26:27 +00001709}
1710
bellard579a97f2007-11-11 14:26:47 +00001711static inline abi_long host_to_target_semun(int cmd,
1712 abi_ulong target_addr,
1713 union semun *host_su,
1714 struct semid_ds *ds)
ths3eb6b042007-06-03 14:26:27 +00001715{
1716 union target_semun *target_su;
1717
1718 switch( cmd ) {
1719 case IPC_STAT:
1720 case IPC_SET:
bellard579a97f2007-11-11 14:26:47 +00001721 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1722 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001723 host_to_target_semid_ds(target_su->buf,ds);
1724 unlock_user_struct(target_su, target_addr, 1);
1725 break;
1726 case GETVAL:
1727 case SETVAL:
bellard579a97f2007-11-11 14:26:47 +00001728 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1729 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001730 target_su->val = tswapl(host_su->val);
1731 unlock_user_struct(target_su, target_addr, 1);
1732 break;
1733 case GETALL:
1734 case SETALL:
bellard579a97f2007-11-11 14:26:47 +00001735 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1736 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001737 *target_su->array = tswap16(*host_su->array);
1738 unlock_user_struct(target_su, target_addr, 1);
1739 break;
1740 default:
1741 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1742 }
bellard579a97f2007-11-11 14:26:47 +00001743 return 0;
ths3eb6b042007-06-03 14:26:27 +00001744}
1745
blueswir1992f48a2007-10-14 16:27:31 +00001746static inline abi_long do_semctl(int first, int second, int third,
1747 abi_long ptr)
ths3eb6b042007-06-03 14:26:27 +00001748{
1749 union semun arg;
1750 struct semid_ds dsarg;
1751 int cmd = third&0xff;
blueswir1992f48a2007-10-14 16:27:31 +00001752 abi_long ret = 0;
ths3eb6b042007-06-03 14:26:27 +00001753
1754 switch( cmd ) {
1755 case GETVAL:
1756 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1757 ret = get_errno(semctl(first, second, cmd, arg));
1758 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1759 break;
1760 case SETVAL:
1761 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1762 ret = get_errno(semctl(first, second, cmd, arg));
1763 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1764 break;
1765 case GETALL:
1766 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1767 ret = get_errno(semctl(first, second, cmd, arg));
1768 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1769 break;
1770 case SETALL:
1771 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1772 ret = get_errno(semctl(first, second, cmd, arg));
1773 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1774 break;
1775 case IPC_STAT:
1776 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1777 ret = get_errno(semctl(first, second, cmd, arg));
1778 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1779 break;
1780 case IPC_SET:
1781 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1782 ret = get_errno(semctl(first, second, cmd, arg));
1783 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1784 break;
1785 default:
1786 ret = get_errno(semctl(first, second, cmd, arg));
1787 }
1788
1789 return ret;
1790}
1791
ths1bc012f2007-06-03 14:27:49 +00001792struct target_msqid_ds
1793{
1794 struct target_ipc_perm msg_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001795 abi_ulong msg_stime;
1796 abi_ulong __unused1;
1797 abi_ulong msg_rtime;
1798 abi_ulong __unused2;
1799 abi_ulong msg_ctime;
1800 abi_ulong __unused3;
1801 abi_ulong __msg_cbytes;
1802 abi_ulong msg_qnum;
1803 abi_ulong msg_qbytes;
1804 abi_ulong msg_lspid;
1805 abi_ulong msg_lrpid;
1806 abi_ulong __unused4;
1807 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00001808};
1809
bellard579a97f2007-11-11 14:26:47 +00001810static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
1811 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00001812{
1813 struct target_msqid_ds *target_md;
1814
bellard579a97f2007-11-11 14:26:47 +00001815 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
1816 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001817 target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1818 host_md->msg_stime = tswapl(target_md->msg_stime);
1819 host_md->msg_rtime = tswapl(target_md->msg_rtime);
1820 host_md->msg_ctime = tswapl(target_md->msg_ctime);
1821 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1822 host_md->msg_qnum = tswapl(target_md->msg_qnum);
1823 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1824 host_md->msg_lspid = tswapl(target_md->msg_lspid);
1825 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1826 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001827 return 0;
ths1bc012f2007-06-03 14:27:49 +00001828}
1829
bellard579a97f2007-11-11 14:26:47 +00001830static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
1831 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00001832{
1833 struct target_msqid_ds *target_md;
1834
bellard579a97f2007-11-11 14:26:47 +00001835 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
1836 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001837 host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1838 target_md->msg_stime = tswapl(host_md->msg_stime);
1839 target_md->msg_rtime = tswapl(host_md->msg_rtime);
1840 target_md->msg_ctime = tswapl(host_md->msg_ctime);
1841 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1842 target_md->msg_qnum = tswapl(host_md->msg_qnum);
1843 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1844 target_md->msg_lspid = tswapl(host_md->msg_lspid);
1845 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1846 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001847 return 0;
ths1bc012f2007-06-03 14:27:49 +00001848}
1849
blueswir1992f48a2007-10-14 16:27:31 +00001850static inline abi_long do_msgctl(int first, int second, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00001851{
1852 struct msqid_ds dsarg;
1853 int cmd = second&0xff;
blueswir1992f48a2007-10-14 16:27:31 +00001854 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001855 switch( cmd ) {
1856 case IPC_STAT:
1857 case IPC_SET:
1858 target_to_host_msqid_ds(&dsarg,ptr);
1859 ret = get_errno(msgctl(first, cmd, &dsarg));
1860 host_to_target_msqid_ds(ptr,&dsarg);
1861 default:
1862 ret = get_errno(msgctl(first, cmd, &dsarg));
1863 }
1864 return ret;
1865}
1866
1867struct target_msgbuf {
blueswir1992f48a2007-10-14 16:27:31 +00001868 abi_ulong mtype;
ths1bc012f2007-06-03 14:27:49 +00001869 char mtext[1];
1870};
1871
blueswir1992f48a2007-10-14 16:27:31 +00001872static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1873 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001874{
1875 struct target_msgbuf *target_mb;
1876 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00001877 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001878
bellard579a97f2007-11-11 14:26:47 +00001879 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
1880 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001881 host_mb = malloc(msgsz+sizeof(long));
1882 host_mb->mtype = tswapl(target_mb->mtype);
1883 memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1884 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1885 free(host_mb);
1886 unlock_user_struct(target_mb, msgp, 0);
1887
1888 return ret;
1889}
1890
blueswir1992f48a2007-10-14 16:27:31 +00001891static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1892 unsigned int msgsz, int msgtype,
1893 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001894{
1895 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00001896 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00001897 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00001898 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001899
bellard579a97f2007-11-11 14:26:47 +00001900 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
1901 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001902 host_mb = malloc(msgsz+sizeof(long));
1903 ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
bellard579a97f2007-11-11 14:26:47 +00001904 if (ret > 0) {
1905 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
1906 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
1907 if (!target_mtext) {
1908 ret = -TARGET_EFAULT;
1909 goto end;
1910 }
ths1bc012f2007-06-03 14:27:49 +00001911 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00001912 unlock_user(target_mtext, target_mtext_addr, ret);
1913 }
ths1bc012f2007-06-03 14:27:49 +00001914 target_mb->mtype = tswapl(host_mb->mtype);
1915 free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00001916
bellard579a97f2007-11-11 14:26:47 +00001917end:
1918 if (target_mb)
1919 unlock_user_struct(target_mb, msgp, 1);
ths1bc012f2007-06-03 14:27:49 +00001920 return ret;
1921}
1922
pbrook53a59602006-03-25 19:31:22 +00001923/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00001924/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001925static abi_long do_ipc(unsigned int call, int first,
1926 int second, int third,
1927 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00001928{
1929 int version;
blueswir1992f48a2007-10-14 16:27:31 +00001930 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00001931 struct shmid_ds shm_info;
1932 int i;
1933
1934 version = call >> 16;
1935 call &= 0xffff;
1936
1937 switch (call) {
thsfa294812007-02-02 22:05:00 +00001938 case IPCOP_semop:
bellard579a97f2007-11-11 14:26:47 +00001939 ret = get_errno(semop(first,(struct sembuf *)g2h(ptr), second));
thsfa294812007-02-02 22:05:00 +00001940 break;
1941
1942 case IPCOP_semget:
1943 ret = get_errno(semget(first, second, third));
1944 break;
1945
1946 case IPCOP_semctl:
ths3eb6b042007-06-03 14:26:27 +00001947 ret = do_semctl(first, second, third, ptr);
thsfa294812007-02-02 22:05:00 +00001948 break;
1949
1950 case IPCOP_semtimedop:
j_mayer32407102007-09-26 23:01:49 +00001951 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00001952 ret = -TARGET_ENOSYS;
thsfa294812007-02-02 22:05:00 +00001953 break;
thsd96372e2007-02-02 22:05:44 +00001954
1955 case IPCOP_msgget:
1956 ret = get_errno(msgget(first, second));
1957 break;
1958
1959 case IPCOP_msgsnd:
ths1bc012f2007-06-03 14:27:49 +00001960 ret = do_msgsnd(first, ptr, second, third);
thsd96372e2007-02-02 22:05:44 +00001961 break;
1962
1963 case IPCOP_msgctl:
ths1bc012f2007-06-03 14:27:49 +00001964 ret = do_msgctl(first, second, ptr);
thsd96372e2007-02-02 22:05:44 +00001965 break;
1966
1967 case IPCOP_msgrcv:
ths1bc012f2007-06-03 14:27:49 +00001968 {
bellard579a97f2007-11-11 14:26:47 +00001969 /* XXX: this code is not correct */
ths1bc012f2007-06-03 14:27:49 +00001970 struct ipc_kludge
1971 {
1972 void *__unbounded msgp;
1973 long int msgtyp;
1974 };
thsd96372e2007-02-02 22:05:44 +00001975
bellard579a97f2007-11-11 14:26:47 +00001976 struct ipc_kludge *foo = (struct ipc_kludge *)g2h(ptr);
ths1bc012f2007-06-03 14:27:49 +00001977 struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
thsd96372e2007-02-02 22:05:44 +00001978
ths1bc012f2007-06-03 14:27:49 +00001979 ret = do_msgrcv(first, (long)msgp, second, 0, third);
thsd96372e2007-02-02 22:05:44 +00001980
ths1bc012f2007-06-03 14:27:49 +00001981 }
thsd96372e2007-02-02 22:05:44 +00001982 break;
1983
bellard8853f862004-02-22 14:57:26 +00001984 case IPCOP_shmat:
bellard5a4a8982007-11-11 17:39:18 +00001985 {
1986 abi_ulong raddr;
1987 void *host_addr;
1988 /* SHM_* flags are the same on all linux platforms */
1989 host_addr = shmat(first, (void *)g2h(ptr), second);
1990 if (host_addr == (void *)-1) {
1991 ret = get_errno((long)host_addr);
bellard8853f862004-02-22 14:57:26 +00001992 break;
bellard5a4a8982007-11-11 17:39:18 +00001993 }
1994 raddr = h2g((unsigned long)host_addr);
1995 /* find out the length of the shared memory segment */
1996
1997 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
1998 if (is_error(ret)) {
1999 /* can't get length, bail out */
2000 shmdt(host_addr);
2001 break;
2002 }
2003 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2004 PAGE_VALID | PAGE_READ |
2005 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
2006 for (i = 0; i < N_SHM_REGIONS; ++i) {
2007 if (shm_regions[i].start == 0) {
2008 shm_regions[i].start = raddr;
2009 shm_regions[i].size = shm_info.shm_segsz;
2010 break;
2011 }
2012 }
bellard2f619692007-11-16 10:46:05 +00002013 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00002014 return -TARGET_EFAULT;
2015 ret = 0;
2016 }
bellard8853f862004-02-22 14:57:26 +00002017 break;
2018 case IPCOP_shmdt:
2019 for (i = 0; i < N_SHM_REGIONS; ++i) {
2020 if (shm_regions[i].start == ptr) {
2021 shm_regions[i].start = 0;
2022 page_set_flags(ptr, shm_regions[i].size, 0);
2023 break;
2024 }
2025 }
bellard5a4a8982007-11-11 17:39:18 +00002026 ret = get_errno(shmdt((void *)g2h(ptr)));
bellard8853f862004-02-22 14:57:26 +00002027 break;
2028
2029 case IPCOP_shmget:
2030 /* IPC_* flag values are the same on all linux platforms */
2031 ret = get_errno(shmget(first, second, third));
2032 break;
2033
2034 /* IPC_* and SHM_* command values are the same on all linux platforms */
2035 case IPCOP_shmctl:
2036 switch(second) {
2037 case IPC_RMID:
2038 case SHM_LOCK:
2039 case SHM_UNLOCK:
2040 ret = get_errno(shmctl(first, second, NULL));
2041 break;
2042 default:
2043 goto unimplemented;
2044 }
2045 break;
2046 default:
2047 unimplemented:
j_mayer32407102007-09-26 23:01:49 +00002048 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00002049 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00002050 break;
2051 }
2052 return ret;
2053}
j_mayer32407102007-09-26 23:01:49 +00002054#endif
bellard8853f862004-02-22 14:57:26 +00002055
bellard31e31b82003-02-18 22:55:36 +00002056/* kernel structure types definitions */
2057#define IFNAMSIZ 16
2058
2059#define STRUCT(name, list...) STRUCT_ ## name,
2060#define STRUCT_SPECIAL(name) STRUCT_ ## name,
2061enum {
2062#include "syscall_types.h"
2063};
2064#undef STRUCT
2065#undef STRUCT_SPECIAL
2066
2067#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
2068#define STRUCT_SPECIAL(name)
2069#include "syscall_types.h"
2070#undef STRUCT
2071#undef STRUCT_SPECIAL
2072
2073typedef struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00002074 unsigned int target_cmd;
2075 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00002076 const char *name;
2077 int access;
bellard1a9353d2003-03-16 20:28:50 +00002078 const argtype arg_type[5];
bellard31e31b82003-02-18 22:55:36 +00002079} IOCTLEntry;
2080
2081#define IOC_R 0x0001
2082#define IOC_W 0x0002
2083#define IOC_RW (IOC_R | IOC_W)
2084
2085#define MAX_STRUCT_SIZE 4096
2086
bellard2ab83ea2003-06-15 19:56:46 +00002087IOCTLEntry ioctl_entries[] = {
bellard31e31b82003-02-18 22:55:36 +00002088#define IOCTL(cmd, access, types...) \
2089 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
2090#include "ioctls.h"
2091 { 0, 0, },
2092};
2093
pbrook53a59602006-03-25 19:31:22 +00002094/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00002095/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002096static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00002097{
2098 const IOCTLEntry *ie;
2099 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00002100 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00002101 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00002102 int target_size;
2103 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00002104
2105 ie = ioctl_entries;
2106 for(;;) {
2107 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00002108 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00002109 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002110 }
2111 if (ie->target_cmd == cmd)
2112 break;
2113 ie++;
2114 }
2115 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00002116#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00002117 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00002118#endif
bellard31e31b82003-02-18 22:55:36 +00002119 switch(arg_type[0]) {
2120 case TYPE_NULL:
2121 /* no argument */
2122 ret = get_errno(ioctl(fd, ie->host_cmd));
2123 break;
2124 case TYPE_PTRVOID:
2125 case TYPE_INT:
2126 /* int argment */
2127 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2128 break;
2129 case TYPE_PTR:
2130 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00002131 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00002132 switch(ie->access) {
2133 case IOC_R:
2134 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2135 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002136 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2137 if (!argptr)
2138 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002139 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2140 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00002141 }
2142 break;
2143 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00002144 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2145 if (!argptr)
2146 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002147 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2148 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002149 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2150 break;
2151 default:
2152 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00002153 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2154 if (!argptr)
2155 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002156 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2157 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002158 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2159 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002160 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2161 if (!argptr)
2162 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002163 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2164 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00002165 }
2166 break;
2167 }
2168 break;
2169 default:
j_mayer32407102007-09-26 23:01:49 +00002170 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2171 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00002172 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002173 break;
2174 }
2175 return ret;
2176}
2177
2178bitmask_transtbl iflag_tbl[] = {
2179 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2180 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2181 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2182 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2183 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2184 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2185 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2186 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2187 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2188 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2189 { TARGET_IXON, TARGET_IXON, IXON, IXON },
2190 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2191 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
2192 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
2193 { 0, 0, 0, 0 }
2194};
2195
2196bitmask_transtbl oflag_tbl[] = {
2197 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
2198 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
2199 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
2200 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
2201 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
2202 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
2203 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
2204 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
2205 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
2206 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
2207 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
2208 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
2209 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
2210 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
2211 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
2212 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
2213 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
2214 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
2215 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
2216 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
2217 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
2218 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
2219 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
2220 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
2221 { 0, 0, 0, 0 }
2222};
2223
2224bitmask_transtbl cflag_tbl[] = {
2225 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
2226 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
2227 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
2228 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
2229 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
2230 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
2231 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
2232 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
2233 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
2234 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
2235 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
2236 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
2237 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
2238 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
2239 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
2240 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
2241 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
2242 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
2243 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
2244 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
2245 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
2246 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
2247 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
2248 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
2249 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
2250 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
2251 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
2252 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
2253 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
2254 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
2255 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
2256 { 0, 0, 0, 0 }
2257};
2258
2259bitmask_transtbl lflag_tbl[] = {
2260 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2261 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2262 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2263 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2264 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2265 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2266 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2267 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2268 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2269 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2270 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2271 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2272 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2273 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2274 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
2275 { 0, 0, 0, 0 }
2276};
2277
2278static void target_to_host_termios (void *dst, const void *src)
2279{
2280 struct host_termios *host = dst;
2281 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00002282
ths5fafdf22007-09-16 21:08:06 +00002283 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002284 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002285 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002286 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002287 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002288 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002289 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002290 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2291 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00002292
ths5fafdf22007-09-16 21:08:06 +00002293 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2294 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00002295 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00002296 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00002297 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00002298 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00002299 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00002300 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00002301 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00002302 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2303 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00002304 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2305 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2306 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2307 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2308 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00002309 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00002310}
ths3b46e622007-09-17 08:09:54 +00002311
bellard31e31b82003-02-18 22:55:36 +00002312static void host_to_target_termios (void *dst, const void *src)
2313{
2314 struct target_termios *target = dst;
2315 const struct host_termios *host = src;
2316
ths5fafdf22007-09-16 21:08:06 +00002317 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002318 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002319 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002320 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002321 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002322 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002323 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002324 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2325 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00002326
bellard31e31b82003-02-18 22:55:36 +00002327 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2328 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2329 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2330 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2331 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2332 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2333 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2334 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2335 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2336 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2337 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2338 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2339 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2340 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2341 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2342 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2343 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2344}
2345
2346StructEntry struct_termios_def = {
2347 .convert = { host_to_target_termios, target_to_host_termios },
2348 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2349 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2350};
2351
bellard5286db72003-06-05 00:57:30 +00002352static bitmask_transtbl mmap_flags_tbl[] = {
2353 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2354 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2355 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2356 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2357 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2358 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2359 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2360 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2361 { 0, 0, 0, 0 }
2362};
2363
bellardffa65c32004-01-04 23:57:22 +00002364static bitmask_transtbl fcntl_flags_tbl[] = {
2365 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
2366 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
2367 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
2368 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
2369 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
2370 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
2371 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
2372 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
2373 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
2374 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
2375 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2376 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
2377 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
bellard121061d2004-02-22 11:56:01 +00002378#if defined(O_DIRECT)
bellardffa65c32004-01-04 23:57:22 +00002379 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
bellard121061d2004-02-22 11:56:01 +00002380#endif
bellardffa65c32004-01-04 23:57:22 +00002381 { 0, 0, 0, 0 }
2382};
2383
bellard2ab83ea2003-06-15 19:56:46 +00002384#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00002385
2386/* NOTE: there is really one LDT for all the threads */
2387uint8_t *ldt_table;
2388
bellard03acab62007-11-11 14:57:14 +00002389static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002390{
2391 int size;
pbrook53a59602006-03-25 19:31:22 +00002392 void *p;
bellard6dbad632003-03-16 18:05:05 +00002393
2394 if (!ldt_table)
2395 return 0;
2396 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2397 if (size > bytecount)
2398 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00002399 p = lock_user(VERIFY_WRITE, ptr, size, 0);
2400 if (!p)
bellard03acab62007-11-11 14:57:14 +00002401 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00002402 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00002403 memcpy(p, ldt_table, size);
2404 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00002405 return size;
2406}
2407
2408/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00002409static abi_long write_ldt(CPUX86State *env,
2410 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00002411{
2412 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00002413 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00002414 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00002415 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00002416 uint32_t *lp, entry_1, entry_2;
2417
2418 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00002419 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00002420 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00002421 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002422 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2423 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2424 ldt_info.limit = tswap32(target_ldt_info->limit);
2425 ldt_info.flags = tswap32(target_ldt_info->flags);
2426 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00002427
bellard6dbad632003-03-16 18:05:05 +00002428 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00002429 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002430 seg_32bit = ldt_info.flags & 1;
2431 contents = (ldt_info.flags >> 1) & 3;
2432 read_exec_only = (ldt_info.flags >> 3) & 1;
2433 limit_in_pages = (ldt_info.flags >> 4) & 1;
2434 seg_not_present = (ldt_info.flags >> 5) & 1;
2435 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00002436#ifdef TARGET_ABI32
2437 lm = 0;
2438#else
2439 lm = (ldt_info.flags >> 7) & 1;
2440#endif
bellard6dbad632003-03-16 18:05:05 +00002441 if (contents == 3) {
2442 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00002443 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002444 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00002445 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002446 }
2447 /* allocate the LDT */
2448 if (!ldt_table) {
2449 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2450 if (!ldt_table)
bellard03acab62007-11-11 14:57:14 +00002451 return -TARGET_ENOMEM;
bellard6dbad632003-03-16 18:05:05 +00002452 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellardeeeac3f2007-11-11 19:34:32 +00002453 env->ldt.base = h2g((unsigned long)ldt_table);
bellard6dbad632003-03-16 18:05:05 +00002454 env->ldt.limit = 0xffff;
2455 }
2456
2457 /* NOTE: same code as Linux kernel */
2458 /* Allow LDTs to be cleared by the user. */
2459 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2460 if (oldmode ||
2461 (contents == 0 &&
2462 read_exec_only == 1 &&
2463 seg_32bit == 0 &&
2464 limit_in_pages == 0 &&
2465 seg_not_present == 1 &&
2466 useable == 0 )) {
2467 entry_1 = 0;
2468 entry_2 = 0;
2469 goto install;
2470 }
2471 }
ths3b46e622007-09-17 08:09:54 +00002472
bellard6dbad632003-03-16 18:05:05 +00002473 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2474 (ldt_info.limit & 0x0ffff);
2475 entry_2 = (ldt_info.base_addr & 0xff000000) |
2476 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2477 (ldt_info.limit & 0xf0000) |
2478 ((read_exec_only ^ 1) << 9) |
2479 (contents << 10) |
2480 ((seg_not_present ^ 1) << 15) |
2481 (seg_32bit << 22) |
2482 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00002483 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00002484 0x7000;
2485 if (!oldmode)
2486 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00002487
bellard6dbad632003-03-16 18:05:05 +00002488 /* Install the new entry ... */
2489install:
2490 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2491 lp[0] = tswap32(entry_1);
2492 lp[1] = tswap32(entry_2);
2493 return 0;
2494}
2495
2496/* specific and weird i386 syscalls */
bellard03acab62007-11-11 14:57:14 +00002497abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
2498 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002499{
bellard03acab62007-11-11 14:57:14 +00002500 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00002501
bellard6dbad632003-03-16 18:05:05 +00002502 switch (func) {
2503 case 0:
2504 ret = read_ldt(ptr, bytecount);
2505 break;
2506 case 1:
2507 ret = write_ldt(env, ptr, bytecount, 1);
2508 break;
2509 case 0x11:
2510 ret = write_ldt(env, ptr, bytecount, 0);
2511 break;
bellard03acab62007-11-11 14:57:14 +00002512 default:
2513 ret = -TARGET_ENOSYS;
2514 break;
bellard6dbad632003-03-16 18:05:05 +00002515 }
2516 return ret;
2517}
bellard1b6b0292003-03-22 17:31:38 +00002518
bellard8d18e892007-11-14 15:18:40 +00002519abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
2520{
2521 uint64_t *gdt_table = g2h(env->gdt.base);
2522 struct target_modify_ldt_ldt_s ldt_info;
2523 struct target_modify_ldt_ldt_s *target_ldt_info;
2524 int seg_32bit, contents, read_exec_only, limit_in_pages;
2525 int seg_not_present, useable, lm;
2526 uint32_t *lp, entry_1, entry_2;
2527 int i;
2528
2529 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
2530 if (!target_ldt_info)
2531 return -TARGET_EFAULT;
2532 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2533 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2534 ldt_info.limit = tswap32(target_ldt_info->limit);
2535 ldt_info.flags = tswap32(target_ldt_info->flags);
2536 if (ldt_info.entry_number == -1) {
2537 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
2538 if (gdt_table[i] == 0) {
2539 ldt_info.entry_number = i;
2540 target_ldt_info->entry_number = tswap32(i);
2541 break;
2542 }
2543 }
2544 }
2545 unlock_user_struct(target_ldt_info, ptr, 1);
2546
2547 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
2548 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
2549 return -TARGET_EINVAL;
2550 seg_32bit = ldt_info.flags & 1;
2551 contents = (ldt_info.flags >> 1) & 3;
2552 read_exec_only = (ldt_info.flags >> 3) & 1;
2553 limit_in_pages = (ldt_info.flags >> 4) & 1;
2554 seg_not_present = (ldt_info.flags >> 5) & 1;
2555 useable = (ldt_info.flags >> 6) & 1;
2556#ifdef TARGET_ABI32
2557 lm = 0;
2558#else
2559 lm = (ldt_info.flags >> 7) & 1;
2560#endif
2561
2562 if (contents == 3) {
2563 if (seg_not_present == 0)
2564 return -TARGET_EINVAL;
2565 }
2566
2567 /* NOTE: same code as Linux kernel */
2568 /* Allow LDTs to be cleared by the user. */
2569 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2570 if ((contents == 0 &&
2571 read_exec_only == 1 &&
2572 seg_32bit == 0 &&
2573 limit_in_pages == 0 &&
2574 seg_not_present == 1 &&
2575 useable == 0 )) {
2576 entry_1 = 0;
2577 entry_2 = 0;
2578 goto install;
2579 }
2580 }
2581
2582 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2583 (ldt_info.limit & 0x0ffff);
2584 entry_2 = (ldt_info.base_addr & 0xff000000) |
2585 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2586 (ldt_info.limit & 0xf0000) |
2587 ((read_exec_only ^ 1) << 9) |
2588 (contents << 10) |
2589 ((seg_not_present ^ 1) << 15) |
2590 (seg_32bit << 22) |
2591 (limit_in_pages << 23) |
2592 (useable << 20) |
2593 (lm << 21) |
2594 0x7000;
2595
2596 /* Install the new entry ... */
2597install:
2598 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
2599 lp[0] = tswap32(entry_1);
2600 lp[1] = tswap32(entry_2);
2601 return 0;
2602}
2603
2604abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
2605{
2606 struct target_modify_ldt_ldt_s *target_ldt_info;
2607 uint64_t *gdt_table = g2h(env->gdt.base);
2608 uint32_t base_addr, limit, flags;
2609 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
2610 int seg_not_present, useable, lm;
2611 uint32_t *lp, entry_1, entry_2;
2612
2613 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
2614 if (!target_ldt_info)
2615 return -TARGET_EFAULT;
2616 idx = tswap32(target_ldt_info->entry_number);
2617 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
2618 idx > TARGET_GDT_ENTRY_TLS_MAX) {
2619 unlock_user_struct(target_ldt_info, ptr, 1);
2620 return -TARGET_EINVAL;
2621 }
2622 lp = (uint32_t *)(gdt_table + idx);
2623 entry_1 = tswap32(lp[0]);
2624 entry_2 = tswap32(lp[1]);
2625
2626 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
2627 contents = (entry_2 >> 10) & 3;
2628 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
2629 seg_32bit = (entry_2 >> 22) & 1;
2630 limit_in_pages = (entry_2 >> 23) & 1;
2631 useable = (entry_2 >> 20) & 1;
2632#ifdef TARGET_ABI32
2633 lm = 0;
2634#else
2635 lm = (entry_2 >> 21) & 1;
2636#endif
2637 flags = (seg_32bit << 0) | (contents << 1) |
2638 (read_exec_only << 3) | (limit_in_pages << 4) |
2639 (seg_not_present << 5) | (useable << 6) | (lm << 7);
2640 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
2641 base_addr = (entry_1 >> 16) |
2642 (entry_2 & 0xff000000) |
2643 ((entry_2 & 0xff) << 16);
2644 target_ldt_info->base_addr = tswapl(base_addr);
2645 target_ldt_info->limit = tswap32(limit);
2646 target_ldt_info->flags = tswap32(flags);
2647 unlock_user_struct(target_ldt_info, ptr, 1);
2648 return 0;
2649}
2650
bellardd2fd1af2007-11-14 18:08:56 +00002651#ifndef TARGET_ABI32
2652abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
2653{
2654 abi_long ret;
2655 abi_ulong val;
2656 int idx;
2657
2658 switch(code) {
2659 case TARGET_ARCH_SET_GS:
2660 case TARGET_ARCH_SET_FS:
2661 if (code == TARGET_ARCH_SET_GS)
2662 idx = R_GS;
2663 else
2664 idx = R_FS;
2665 cpu_x86_load_seg(env, idx, 0);
2666 env->segs[idx].base = addr;
2667 break;
2668 case TARGET_ARCH_GET_GS:
2669 case TARGET_ARCH_GET_FS:
2670 if (code == TARGET_ARCH_GET_GS)
2671 idx = R_GS;
2672 else
2673 idx = R_FS;
2674 val = env->segs[idx].base;
2675 if (put_user(val, addr, abi_ulong))
2676 return -TARGET_EFAULT;
2677 break;
2678 default:
2679 ret = -TARGET_EINVAL;
2680 break;
2681 }
2682 return 0;
2683}
2684#endif
2685
bellard2ab83ea2003-06-15 19:56:46 +00002686#endif /* defined(TARGET_I386) */
2687
bellard1b6b0292003-03-22 17:31:38 +00002688/* this stack is the equivalent of the kernel stack associated with a
2689 thread/process */
2690#define NEW_STACK_SIZE 8192
2691
2692static int clone_func(void *arg)
2693{
bellard2ab83ea2003-06-15 19:56:46 +00002694 CPUState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00002695 cpu_loop(env);
2696 /* never exits */
2697 return 0;
2698}
2699
ths0da46a62007-10-20 20:23:07 +00002700/* do_fork() Must return host values and target errnos (unlike most
2701 do_*() functions). */
blueswir1992f48a2007-10-14 16:27:31 +00002702int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp)
bellard1b6b0292003-03-22 17:31:38 +00002703{
2704 int ret;
bellard5cd43932003-03-29 16:54:36 +00002705 TaskState *ts;
bellard1b6b0292003-03-22 17:31:38 +00002706 uint8_t *new_stack;
bellard2ab83ea2003-06-15 19:56:46 +00002707 CPUState *new_env;
ths3b46e622007-09-17 08:09:54 +00002708
bellard1b6b0292003-03-22 17:31:38 +00002709 if (flags & CLONE_VM) {
bellard5cd43932003-03-29 16:54:36 +00002710 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2711 memset(ts, 0, sizeof(TaskState));
2712 new_stack = ts->stack;
2713 ts->used = 1;
2714 /* add in task state list */
2715 ts->next = first_task_state;
2716 first_task_state = ts;
bellard1b6b0292003-03-22 17:31:38 +00002717 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00002718 new_env = cpu_copy(env);
bellard2ab83ea2003-06-15 19:56:46 +00002719#if defined(TARGET_I386)
2720 if (!newsp)
2721 newsp = env->regs[R_ESP];
bellard1b6b0292003-03-22 17:31:38 +00002722 new_env->regs[R_ESP] = newsp;
2723 new_env->regs[R_EAX] = 0;
bellard2ab83ea2003-06-15 19:56:46 +00002724#elif defined(TARGET_ARM)
2725 if (!newsp)
2726 newsp = env->regs[13];
2727 new_env->regs[13] = newsp;
2728 new_env->regs[0] = 0;
bellardebc05482003-09-30 21:08:41 +00002729#elif defined(TARGET_SPARC)
bellard48dc41e2006-06-21 18:15:50 +00002730 if (!newsp)
2731 newsp = env->regwptr[22];
2732 new_env->regwptr[22] = newsp;
2733 new_env->regwptr[0] = 0;
2734 /* XXXXX */
bellard67867302003-11-23 17:05:30 +00002735 printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
pbrooke6e59062006-10-22 00:18:54 +00002736#elif defined(TARGET_M68K)
2737 if (!newsp)
2738 newsp = env->aregs[7];
2739 new_env->aregs[7] = newsp;
2740 new_env->dregs[0] = 0;
2741 /* ??? is this sufficient? */
bellard048f6b42005-11-26 18:47:20 +00002742#elif defined(TARGET_MIPS)
ths388bb212007-05-13 13:58:00 +00002743 if (!newsp)
thsead93602007-09-06 00:18:15 +00002744 newsp = env->gpr[29][env->current_tc];
2745 new_env->gpr[29][env->current_tc] = newsp;
bellard67867302003-11-23 17:05:30 +00002746#elif defined(TARGET_PPC)
2747 if (!newsp)
2748 newsp = env->gpr[1];
2749 new_env->gpr[1] = newsp;
ths5fafdf22007-09-16 21:08:06 +00002750 {
bellard67867302003-11-23 17:05:30 +00002751 int i;
2752 for (i = 7; i < 32; i++)
2753 new_env->gpr[i] = 0;
2754 }
bellardfdf9b3e2006-04-27 21:07:38 +00002755#elif defined(TARGET_SH4)
2756 if (!newsp)
2757 newsp = env->gregs[15];
2758 new_env->gregs[15] = newsp;
2759 /* XXXXX */
j_mayer7a3148a2007-04-05 07:13:51 +00002760#elif defined(TARGET_ALPHA)
2761 if (!newsp)
2762 newsp = env->ir[30];
2763 new_env->ir[30] = newsp;
2764 /* ? */
2765 {
2766 int i;
2767 for (i = 7; i < 30; i++)
2768 new_env->ir[i] = 0;
2769 }
ths48733d12007-10-08 13:36:46 +00002770#elif defined(TARGET_CRIS)
2771 if (!newsp)
2772 newsp = env->regs[14];
2773 new_env->regs[14] = newsp;
bellard2ab83ea2003-06-15 19:56:46 +00002774#else
2775#error unsupported target CPU
2776#endif
bellard5cd43932003-03-29 16:54:36 +00002777 new_env->opaque = ts;
bellard27725c12003-04-29 21:08:18 +00002778#ifdef __ia64__
bellardfd4a43e2006-04-24 20:32:17 +00002779 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00002780#else
2781 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2782#endif
bellard1b6b0292003-03-22 17:31:38 +00002783 } else {
2784 /* if no CLONE_VM, we consider it is a fork */
2785 if ((flags & ~CSIGNAL) != 0)
2786 return -EINVAL;
2787 ret = fork();
2788 }
2789 return ret;
2790}
2791
blueswir1992f48a2007-10-14 16:27:31 +00002792static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00002793{
2794 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00002795 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00002796 struct flock64 fl64;
2797 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00002798 abi_long ret;
pbrook53a59602006-03-25 19:31:22 +00002799
bellard7775e9e2003-05-14 22:46:48 +00002800 switch(cmd) {
2801 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00002802 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2803 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00002804 fl.l_type = tswap16(target_fl->l_type);
2805 fl.l_whence = tswap16(target_fl->l_whence);
2806 fl.l_start = tswapl(target_fl->l_start);
2807 fl.l_len = tswapl(target_fl->l_len);
2808 fl.l_pid = tswapl(target_fl->l_pid);
2809 unlock_user_struct(target_fl, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002810 ret = get_errno(fcntl(fd, cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00002811 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00002812 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
2813 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00002814 target_fl->l_type = tswap16(fl.l_type);
2815 target_fl->l_whence = tswap16(fl.l_whence);
2816 target_fl->l_start = tswapl(fl.l_start);
2817 target_fl->l_len = tswapl(fl.l_len);
2818 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00002819 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00002820 }
2821 break;
ths3b46e622007-09-17 08:09:54 +00002822
bellard7775e9e2003-05-14 22:46:48 +00002823 case TARGET_F_SETLK:
2824 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00002825 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2826 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00002827 fl.l_type = tswap16(target_fl->l_type);
2828 fl.l_whence = tswap16(target_fl->l_whence);
2829 fl.l_start = tswapl(target_fl->l_start);
2830 fl.l_len = tswapl(target_fl->l_len);
2831 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00002832 unlock_user_struct(target_fl, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002833 ret = get_errno(fcntl(fd, cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00002834 break;
ths3b46e622007-09-17 08:09:54 +00002835
bellard7775e9e2003-05-14 22:46:48 +00002836 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00002837 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2838 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00002839 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2840 fl64.l_whence = tswap16(target_fl64->l_whence);
2841 fl64.l_start = tswapl(target_fl64->l_start);
2842 fl64.l_len = tswapl(target_fl64->l_len);
2843 fl64.l_pid = tswap16(target_fl64->l_pid);
2844 unlock_user_struct(target_fl64, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002845 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
ths43f238d2007-01-05 20:55:49 +00002846 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00002847 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
2848 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00002849 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2850 target_fl64->l_whence = tswap16(fl64.l_whence);
2851 target_fl64->l_start = tswapl(fl64.l_start);
2852 target_fl64->l_len = tswapl(fl64.l_len);
2853 target_fl64->l_pid = tswapl(fl64.l_pid);
2854 unlock_user_struct(target_fl64, arg, 1);
2855 }
bellard9ee1fa22007-11-11 15:11:19 +00002856 break;
bellard7775e9e2003-05-14 22:46:48 +00002857 case TARGET_F_SETLK64:
2858 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00002859 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2860 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00002861 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2862 fl64.l_whence = tswap16(target_fl64->l_whence);
2863 fl64.l_start = tswapl(target_fl64->l_start);
2864 fl64.l_len = tswapl(target_fl64->l_len);
2865 fl64.l_pid = tswap16(target_fl64->l_pid);
2866 unlock_user_struct(target_fl64, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002867 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00002868 break;
2869
bellardffa65c32004-01-04 23:57:22 +00002870 case F_GETFL:
bellard9ee1fa22007-11-11 15:11:19 +00002871 ret = get_errno(fcntl(fd, cmd, arg));
2872 if (ret >= 0) {
2873 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2874 }
bellardffa65c32004-01-04 23:57:22 +00002875 break;
2876
2877 case F_SETFL:
bellard9ee1fa22007-11-11 15:11:19 +00002878 ret = get_errno(fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
bellardffa65c32004-01-04 23:57:22 +00002879 break;
2880
bellard7775e9e2003-05-14 22:46:48 +00002881 default:
bellard9ee1fa22007-11-11 15:11:19 +00002882 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00002883 break;
2884 }
2885 return ret;
2886}
2887
bellard67867302003-11-23 17:05:30 +00002888#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00002889
bellard67867302003-11-23 17:05:30 +00002890static inline int high2lowuid(int uid)
2891{
2892 if (uid > 65535)
2893 return 65534;
2894 else
2895 return uid;
2896}
2897
2898static inline int high2lowgid(int gid)
2899{
2900 if (gid > 65535)
2901 return 65534;
2902 else
2903 return gid;
2904}
2905
2906static inline int low2highuid(int uid)
2907{
2908 if ((int16_t)uid == -1)
2909 return -1;
2910 else
2911 return uid;
2912}
2913
2914static inline int low2highgid(int gid)
2915{
2916 if ((int16_t)gid == -1)
2917 return -1;
2918 else
2919 return gid;
2920}
2921
2922#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00002923
bellard31e31b82003-02-18 22:55:36 +00002924void syscall_init(void)
2925{
bellard2ab83ea2003-06-15 19:56:46 +00002926 IOCTLEntry *ie;
2927 const argtype *arg_type;
2928 int size;
thsb92c47c2007-11-01 00:07:38 +00002929 int i;
bellard2ab83ea2003-06-15 19:56:46 +00002930
ths5fafdf22007-09-16 21:08:06 +00002931#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2932#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00002933#include "syscall_types.h"
2934#undef STRUCT
2935#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00002936
2937 /* we patch the ioctl size if necessary. We rely on the fact that
2938 no ioctl has all the bits at '1' in the size field */
2939 ie = ioctl_entries;
2940 while (ie->target_cmd != 0) {
2941 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2942 TARGET_IOC_SIZEMASK) {
2943 arg_type = ie->arg_type;
2944 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00002945 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002946 ie->target_cmd);
2947 exit(1);
2948 }
2949 arg_type++;
2950 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00002951 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00002952 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2953 (size << TARGET_IOC_SIZESHIFT);
2954 }
thsb92c47c2007-11-01 00:07:38 +00002955
2956 /* Build target_to_host_errno_table[] table from
2957 * host_to_target_errno_table[]. */
2958 for (i=0; i < ERRNO_TABLE_SIZE; i++)
2959 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
2960
bellard2ab83ea2003-06-15 19:56:46 +00002961 /* automatic consistency check if same arch */
bellardd2fd1af2007-11-14 18:08:56 +00002962#if defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)
bellard2ab83ea2003-06-15 19:56:46 +00002963 if (ie->target_cmd != ie->host_cmd) {
ths5fafdf22007-09-16 21:08:06 +00002964 fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002965 ie->target_cmd, ie->host_cmd);
2966 }
2967#endif
2968 ie++;
2969 }
bellard31e31b82003-02-18 22:55:36 +00002970}
bellardc573ff62004-01-04 15:51:36 +00002971
blueswir1992f48a2007-10-14 16:27:31 +00002972#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00002973static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2974{
2975#ifdef TARGET_WORDS_BIG_ENDIAN
2976 return ((uint64_t)word0 << 32) | word1;
2977#else
2978 return ((uint64_t)word1 << 32) | word0;
2979#endif
2980}
blueswir1992f48a2007-10-14 16:27:31 +00002981#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00002982static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2983{
2984 return word0;
2985}
blueswir1992f48a2007-10-14 16:27:31 +00002986#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00002987
2988#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00002989static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
2990 abi_long arg2,
2991 abi_long arg3,
2992 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00002993{
2994#ifdef TARGET_ARM
2995 if (((CPUARMState *)cpu_env)->eabi)
2996 {
2997 arg2 = arg3;
2998 arg3 = arg4;
2999 }
3000#endif
3001 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
3002}
3003#endif
3004
3005#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00003006static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
3007 abi_long arg2,
3008 abi_long arg3,
3009 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00003010{
3011#ifdef TARGET_ARM
3012 if (((CPUARMState *)cpu_env)->eabi)
3013 {
3014 arg2 = arg3;
3015 arg3 = arg4;
3016 }
3017#endif
3018 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
3019}
3020#endif
3021
bellard579a97f2007-11-11 14:26:47 +00003022static inline abi_long target_to_host_timespec(struct timespec *host_ts,
3023 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00003024{
3025 struct target_timespec *target_ts;
3026
bellard579a97f2007-11-11 14:26:47 +00003027 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
3028 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003029 host_ts->tv_sec = tswapl(target_ts->tv_sec);
3030 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
3031 unlock_user_struct(target_ts, target_addr, 0);
3032}
3033
bellard579a97f2007-11-11 14:26:47 +00003034static inline abi_long host_to_target_timespec(abi_ulong target_addr,
3035 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00003036{
3037 struct target_timespec *target_ts;
3038
bellard579a97f2007-11-11 14:26:47 +00003039 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
3040 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003041 target_ts->tv_sec = tswapl(host_ts->tv_sec);
3042 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
3043 unlock_user_struct(target_ts, target_addr, 1);
3044}
3045
ths0da46a62007-10-20 20:23:07 +00003046/* do_syscall() should always have a single exit point at the end so
3047 that actions, such as logging of syscall results, can be performed.
3048 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00003049abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
3050 abi_long arg2, abi_long arg3, abi_long arg4,
3051 abi_long arg5, abi_long arg6)
bellard31e31b82003-02-18 22:55:36 +00003052{
blueswir1992f48a2007-10-14 16:27:31 +00003053 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00003054 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00003055 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00003056 void *p;
ths3b46e622007-09-17 08:09:54 +00003057
bellard72f03902003-02-18 23:33:18 +00003058#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00003059 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00003060#endif
thsb92c47c2007-11-01 00:07:38 +00003061 if(do_strace)
3062 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
3063
bellard31e31b82003-02-18 22:55:36 +00003064 switch(num) {
3065 case TARGET_NR_exit:
bellard7d132992003-03-06 23:23:54 +00003066#ifdef HAVE_GPROF
3067 _mcleanup();
3068#endif
bellarde9009672005-04-26 20:42:36 +00003069 gdb_exit(cpu_env, arg1);
bellard1b6b0292003-03-22 17:31:38 +00003070 /* XXX: should free thread stack and CPU env */
bellard31e31b82003-02-18 22:55:36 +00003071 _exit(arg1);
3072 ret = 0; /* avoid warning */
3073 break;
3074 case TARGET_NR_read:
bellard579a97f2007-11-11 14:26:47 +00003075 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
3076 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003077 ret = get_errno(read(arg1, p, arg3));
3078 unlock_user(p, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00003079 break;
3080 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00003081 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
3082 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003083 ret = get_errno(write(arg1, p, arg3));
3084 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00003085 break;
3086 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00003087 if (!(p = lock_user_string(arg1)))
3088 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003089 ret = get_errno(open(path(p),
bellardffa65c32004-01-04 23:57:22 +00003090 target_to_host_bitmask(arg2, fcntl_flags_tbl),
3091 arg3));
pbrook53a59602006-03-25 19:31:22 +00003092 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003093 break;
ths82424832007-09-24 09:21:55 +00003094#if defined(TARGET_NR_openat) && defined(__NR_openat)
3095 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00003096 if (!(p = lock_user_string(arg2)))
3097 goto efault;
3098 ret = get_errno(sys_openat(arg1,
3099 path(p),
3100 target_to_host_bitmask(arg3, fcntl_flags_tbl),
3101 arg4));
3102 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00003103 break;
3104#endif
bellard31e31b82003-02-18 22:55:36 +00003105 case TARGET_NR_close:
3106 ret = get_errno(close(arg1));
3107 break;
3108 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00003109 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00003110 break;
3111 case TARGET_NR_fork:
bellard1b6b0292003-03-22 17:31:38 +00003112 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00003113 break;
thse5febef2007-04-01 18:31:35 +00003114#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00003115 case TARGET_NR_waitpid:
3116 {
pbrook53a59602006-03-25 19:31:22 +00003117 int status;
3118 ret = get_errno(waitpid(arg1, &status, arg3));
bellard2f619692007-11-16 10:46:05 +00003119 if (!is_error(ret) && arg2
3120 && put_user_s32(status, arg2))
3121 goto efault;
bellard31e31b82003-02-18 22:55:36 +00003122 }
3123 break;
thse5febef2007-04-01 18:31:35 +00003124#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003125#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003126 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00003127 if (!(p = lock_user_string(arg1)))
3128 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003129 ret = get_errno(creat(p, arg2));
3130 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003131 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003132#endif
bellard31e31b82003-02-18 22:55:36 +00003133 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00003134 {
3135 void * p2;
3136 p = lock_user_string(arg1);
3137 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00003138 if (!p || !p2)
3139 ret = -TARGET_EFAULT;
3140 else
3141 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00003142 unlock_user(p2, arg2, 0);
3143 unlock_user(p, arg1, 0);
3144 }
bellard31e31b82003-02-18 22:55:36 +00003145 break;
ths64f0ce42007-09-24 09:25:06 +00003146#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
3147 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00003148 {
3149 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00003150 if (!arg2 || !arg4)
3151 goto efault;
ths64f0ce42007-09-24 09:25:06 +00003152 p = lock_user_string(arg2);
3153 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00003154 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00003155 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00003156 else
3157 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00003158 unlock_user(p, arg2, 0);
3159 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00003160 }
3161 break;
3162#endif
bellard31e31b82003-02-18 22:55:36 +00003163 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00003164 if (!(p = lock_user_string(arg1)))
3165 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003166 ret = get_errno(unlink(p));
3167 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003168 break;
ths8170f562007-09-24 09:24:11 +00003169#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
3170 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00003171 if (!(p = lock_user_string(arg2)))
3172 goto efault;
3173 ret = get_errno(sys_unlinkat(arg1, p, arg3));
3174 unlock_user(p, arg2, 0);
ths8170f562007-09-24 09:24:11 +00003175#endif
bellard31e31b82003-02-18 22:55:36 +00003176 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00003177 {
3178 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00003179 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00003180 abi_ulong gp;
3181 abi_ulong guest_argp;
3182 abi_ulong guest_envp;
3183 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00003184 char **q;
3185
bellardf7341ff2003-03-30 21:00:25 +00003186 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00003187 guest_argp = arg2;
bellard2f619692007-11-16 10:46:05 +00003188 for (gp = guest_argp; ; gp++) {
ths03aa1972007-12-02 06:28:08 +00003189 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00003190 goto efault;
ths03aa1972007-12-02 06:28:08 +00003191 if (!addr)
bellard2f619692007-11-16 10:46:05 +00003192 break;
bellard7854b052003-03-29 17:22:23 +00003193 argc++;
bellard2f619692007-11-16 10:46:05 +00003194 }
bellardf7341ff2003-03-30 21:00:25 +00003195 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00003196 guest_envp = arg3;
bellard2f619692007-11-16 10:46:05 +00003197 for (gp = guest_envp; ; gp++) {
ths03aa1972007-12-02 06:28:08 +00003198 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00003199 goto efault;
ths03aa1972007-12-02 06:28:08 +00003200 if (!addr)
bellard2f619692007-11-16 10:46:05 +00003201 break;
bellard7854b052003-03-29 17:22:23 +00003202 envc++;
bellard2f619692007-11-16 10:46:05 +00003203 }
bellard7854b052003-03-29 17:22:23 +00003204
bellardf7341ff2003-03-30 21:00:25 +00003205 argp = alloca((argc + 1) * sizeof(void *));
3206 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00003207
pbrook53a59602006-03-25 19:31:22 +00003208 for (gp = guest_argp, q = argp; ;
blueswir1992f48a2007-10-14 16:27:31 +00003209 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00003210 if (get_user_ual(addr, gp))
3211 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003212 if (!addr)
3213 break;
bellard2f619692007-11-16 10:46:05 +00003214 if (!(*q = lock_user_string(addr)))
3215 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003216 }
bellardf7341ff2003-03-30 21:00:25 +00003217 *q = NULL;
3218
pbrook53a59602006-03-25 19:31:22 +00003219 for (gp = guest_envp, q = envp; ;
blueswir1992f48a2007-10-14 16:27:31 +00003220 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00003221 if (get_user_ual(addr, gp))
3222 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003223 if (!addr)
3224 break;
bellard2f619692007-11-16 10:46:05 +00003225 if (!(*q = lock_user_string(addr)))
3226 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003227 }
bellardf7341ff2003-03-30 21:00:25 +00003228 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00003229
bellard2f619692007-11-16 10:46:05 +00003230 if (!(p = lock_user_string(arg1)))
3231 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003232 ret = get_errno(execve(p, argp, envp));
3233 unlock_user(p, arg1, 0);
3234
bellard2f619692007-11-16 10:46:05 +00003235 goto execve_end;
3236
3237 execve_efault:
3238 ret = -TARGET_EFAULT;
3239
3240 execve_end:
pbrook53a59602006-03-25 19:31:22 +00003241 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00003242 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00003243 if (get_user_ual(addr, gp)
3244 || !addr)
3245 break;
pbrook53a59602006-03-25 19:31:22 +00003246 unlock_user(*q, addr, 0);
3247 }
3248 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00003249 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00003250 if (get_user_ual(addr, gp)
3251 || !addr)
3252 break;
pbrook53a59602006-03-25 19:31:22 +00003253 unlock_user(*q, addr, 0);
3254 }
bellard7854b052003-03-29 17:22:23 +00003255 }
bellard31e31b82003-02-18 22:55:36 +00003256 break;
3257 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00003258 if (!(p = lock_user_string(arg1)))
3259 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003260 ret = get_errno(chdir(p));
3261 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003262 break;
bellarda315a142005-01-30 22:59:18 +00003263#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00003264 case TARGET_NR_time:
3265 {
pbrook53a59602006-03-25 19:31:22 +00003266 time_t host_time;
3267 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00003268 if (!is_error(ret)
3269 && arg1
3270 && put_user_sal(host_time, arg1))
3271 goto efault;
bellard31e31b82003-02-18 22:55:36 +00003272 }
3273 break;
bellarda315a142005-01-30 22:59:18 +00003274#endif
bellard31e31b82003-02-18 22:55:36 +00003275 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00003276 if (!(p = lock_user_string(arg1)))
3277 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003278 ret = get_errno(mknod(p, arg2, arg3));
3279 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003280 break;
ths75ac37a2007-09-24 09:23:05 +00003281#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
3282 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00003283 if (!(p = lock_user_string(arg2)))
3284 goto efault;
3285 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
3286 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00003287 break;
3288#endif
bellard31e31b82003-02-18 22:55:36 +00003289 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00003290 if (!(p = lock_user_string(arg1)))
3291 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003292 ret = get_errno(chmod(p, arg2));
3293 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003294 break;
bellardebc05482003-09-30 21:08:41 +00003295#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00003296 case TARGET_NR_break:
3297 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003298#endif
3299#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00003300 case TARGET_NR_oldstat:
3301 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003302#endif
bellard31e31b82003-02-18 22:55:36 +00003303 case TARGET_NR_lseek:
3304 ret = get_errno(lseek(arg1, arg2, arg3));
3305 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003306#ifdef TARGET_NR_getxpid
3307 case TARGET_NR_getxpid:
3308#else
bellard31e31b82003-02-18 22:55:36 +00003309 case TARGET_NR_getpid:
j_mayer7a3148a2007-04-05 07:13:51 +00003310#endif
bellard31e31b82003-02-18 22:55:36 +00003311 ret = get_errno(getpid());
3312 break;
3313 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00003314 {
3315 /* need to look at the data field */
3316 void *p2, *p3;
3317 p = lock_user_string(arg1);
3318 p2 = lock_user_string(arg2);
3319 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00003320 if (!p || !p2 || !p3)
3321 ret = -TARGET_EFAULT;
3322 else
3323 /* FIXME - arg5 should be locked, but it isn't clear how to
3324 * do that since it's not guaranteed to be a NULL-terminated
3325 * string.
3326 */
3327 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
3328 unlock_user(p, arg1, 0);
3329 unlock_user(p2, arg2, 0);
3330 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00003331 break;
3332 }
thse5febef2007-04-01 18:31:35 +00003333#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00003334 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00003335 if (!(p = lock_user_string(arg1)))
3336 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003337 ret = get_errno(umount(p));
3338 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003339 break;
thse5febef2007-04-01 18:31:35 +00003340#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003341#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003342 case TARGET_NR_stime:
3343 {
pbrook53a59602006-03-25 19:31:22 +00003344 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00003345 if (get_user_sal(host_time, arg1))
3346 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003347 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00003348 }
3349 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003350#endif
bellard31e31b82003-02-18 22:55:36 +00003351 case TARGET_NR_ptrace:
3352 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00003353#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003354 case TARGET_NR_alarm:
3355 ret = alarm(arg1);
3356 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003357#endif
bellardebc05482003-09-30 21:08:41 +00003358#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00003359 case TARGET_NR_oldfstat:
3360 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003361#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003362#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003363 case TARGET_NR_pause:
3364 ret = get_errno(pause());
3365 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003366#endif
thse5febef2007-04-01 18:31:35 +00003367#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00003368 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00003369 {
pbrook53a59602006-03-25 19:31:22 +00003370 struct utimbuf tbuf, *host_tbuf;
3371 struct target_utimbuf *target_tbuf;
3372 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00003373 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
3374 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003375 tbuf.actime = tswapl(target_tbuf->actime);
3376 tbuf.modtime = tswapl(target_tbuf->modtime);
3377 unlock_user_struct(target_tbuf, arg2, 0);
3378 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00003379 } else {
pbrook53a59602006-03-25 19:31:22 +00003380 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00003381 }
bellard579a97f2007-11-11 14:26:47 +00003382 if (!(p = lock_user_string(arg1)))
3383 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003384 ret = get_errno(utime(p, host_tbuf));
3385 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00003386 }
3387 break;
thse5febef2007-04-01 18:31:35 +00003388#endif
bellard978a66f2004-12-06 22:58:05 +00003389 case TARGET_NR_utimes:
3390 {
bellard978a66f2004-12-06 22:58:05 +00003391 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00003392 if (arg2) {
3393 target_to_host_timeval(&tv[0], arg2);
3394 target_to_host_timeval(&tv[1],
3395 arg2 + sizeof (struct target_timeval));
bellard978a66f2004-12-06 22:58:05 +00003396 tvp = tv;
3397 } else {
3398 tvp = NULL;
3399 }
bellard579a97f2007-11-11 14:26:47 +00003400 if (!(p = lock_user_string(arg1)))
3401 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003402 ret = get_errno(utimes(p, tvp));
3403 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00003404 }
3405 break;
bellardebc05482003-09-30 21:08:41 +00003406#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00003407 case TARGET_NR_stty:
3408 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003409#endif
3410#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00003411 case TARGET_NR_gtty:
3412 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003413#endif
bellard31e31b82003-02-18 22:55:36 +00003414 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00003415 if (!(p = lock_user_string(arg1)))
3416 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003417 ret = get_errno(access(p, arg2));
3418 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003419 break;
ths92a34c12007-09-24 09:27:49 +00003420#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
3421 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00003422 if (!(p = lock_user_string(arg2)))
3423 goto efault;
3424 ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
3425 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00003426 break;
3427#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003428#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003429 case TARGET_NR_nice:
3430 ret = get_errno(nice(arg1));
3431 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003432#endif
bellardebc05482003-09-30 21:08:41 +00003433#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00003434 case TARGET_NR_ftime:
3435 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003436#endif
bellard31e31b82003-02-18 22:55:36 +00003437 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00003438 sync();
3439 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00003440 break;
3441 case TARGET_NR_kill:
3442 ret = get_errno(kill(arg1, arg2));
3443 break;
3444 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00003445 {
3446 void *p2;
3447 p = lock_user_string(arg1);
3448 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00003449 if (!p || !p2)
3450 ret = -TARGET_EFAULT;
3451 else
3452 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00003453 unlock_user(p2, arg2, 0);
3454 unlock_user(p, arg1, 0);
3455 }
bellard31e31b82003-02-18 22:55:36 +00003456 break;
ths722183f2007-09-24 09:24:37 +00003457#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
3458 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00003459 {
bellard579a97f2007-11-11 14:26:47 +00003460 void *p2;
ths722183f2007-09-24 09:24:37 +00003461 p = lock_user_string(arg2);
3462 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00003463 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00003464 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00003465 else
3466 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00003467 unlock_user(p2, arg4, 0);
3468 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00003469 }
3470 break;
3471#endif
bellard31e31b82003-02-18 22:55:36 +00003472 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00003473 if (!(p = lock_user_string(arg1)))
3474 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003475 ret = get_errno(mkdir(p, arg2));
3476 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003477 break;
ths4472ad02007-09-24 09:22:32 +00003478#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
3479 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00003480 if (!(p = lock_user_string(arg2)))
3481 goto efault;
3482 ret = get_errno(sys_mkdirat(arg1, p, arg3));
3483 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00003484 break;
3485#endif
bellard31e31b82003-02-18 22:55:36 +00003486 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00003487 if (!(p = lock_user_string(arg1)))
3488 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003489 ret = get_errno(rmdir(p));
3490 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003491 break;
3492 case TARGET_NR_dup:
3493 ret = get_errno(dup(arg1));
3494 break;
3495 case TARGET_NR_pipe:
3496 {
pbrook53a59602006-03-25 19:31:22 +00003497 int host_pipe[2];
3498 ret = get_errno(pipe(host_pipe));
bellard31e31b82003-02-18 22:55:36 +00003499 if (!is_error(ret)) {
thsc12ab052007-06-01 11:50:36 +00003500#if defined(TARGET_MIPS)
thsead93602007-09-06 00:18:15 +00003501 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
3502 env->gpr[3][env->current_tc] = host_pipe[1];
thsc12ab052007-06-01 11:50:36 +00003503 ret = host_pipe[0];
3504#else
bellard2f619692007-11-16 10:46:05 +00003505 if (put_user_s32(host_pipe[0], arg1)
3506 || put_user_s32(host_pipe[1], arg1 + sizeof(host_pipe[0])))
3507 goto efault;
thsc12ab052007-06-01 11:50:36 +00003508#endif
bellard31e31b82003-02-18 22:55:36 +00003509 }
3510 }
3511 break;
3512 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00003513 {
pbrook53a59602006-03-25 19:31:22 +00003514 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00003515 struct tms tms;
3516 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00003517 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00003518 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
3519 if (!tmsp)
3520 goto efault;
bellardc596ed12003-07-13 17:32:31 +00003521 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3522 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3523 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3524 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00003525 }
bellardc596ed12003-07-13 17:32:31 +00003526 if (!is_error(ret))
3527 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00003528 }
3529 break;
bellardebc05482003-09-30 21:08:41 +00003530#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00003531 case TARGET_NR_prof:
3532 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003533#endif
thse5febef2007-04-01 18:31:35 +00003534#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00003535 case TARGET_NR_signal:
3536 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003537#endif
bellard31e31b82003-02-18 22:55:36 +00003538 case TARGET_NR_acct:
bellard579a97f2007-11-11 14:26:47 +00003539 if (!(p = lock_user_string(arg1)))
3540 goto efault;
pbrook24836682006-04-16 14:14:53 +00003541 ret = get_errno(acct(path(p)));
3542 unlock_user(p, arg1, 0);
3543 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003544#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003545 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00003546 if (!(p = lock_user_string(arg1)))
3547 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003548 ret = get_errno(umount2(p, arg2));
3549 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003550 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003551#endif
bellardebc05482003-09-30 21:08:41 +00003552#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00003553 case TARGET_NR_lock:
3554 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003555#endif
bellard31e31b82003-02-18 22:55:36 +00003556 case TARGET_NR_ioctl:
3557 ret = do_ioctl(arg1, arg2, arg3);
3558 break;
3559 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00003560 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00003561 break;
bellardebc05482003-09-30 21:08:41 +00003562#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00003563 case TARGET_NR_mpx:
3564 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003565#endif
bellard31e31b82003-02-18 22:55:36 +00003566 case TARGET_NR_setpgid:
3567 ret = get_errno(setpgid(arg1, arg2));
3568 break;
bellardebc05482003-09-30 21:08:41 +00003569#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00003570 case TARGET_NR_ulimit:
3571 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003572#endif
3573#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00003574 case TARGET_NR_oldolduname:
3575 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003576#endif
bellard31e31b82003-02-18 22:55:36 +00003577 case TARGET_NR_umask:
3578 ret = get_errno(umask(arg1));
3579 break;
3580 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00003581 if (!(p = lock_user_string(arg1)))
3582 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003583 ret = get_errno(chroot(p));
3584 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003585 break;
3586 case TARGET_NR_ustat:
3587 goto unimplemented;
3588 case TARGET_NR_dup2:
3589 ret = get_errno(dup2(arg1, arg2));
3590 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003591#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003592 case TARGET_NR_getppid:
3593 ret = get_errno(getppid());
3594 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003595#endif
bellard31e31b82003-02-18 22:55:36 +00003596 case TARGET_NR_getpgrp:
3597 ret = get_errno(getpgrp());
3598 break;
3599 case TARGET_NR_setsid:
3600 ret = get_errno(setsid());
3601 break;
thse5febef2007-04-01 18:31:35 +00003602#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00003603 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00003604 {
ths388bb212007-05-13 13:58:00 +00003605#if !defined(TARGET_MIPS)
pbrook53a59602006-03-25 19:31:22 +00003606 struct target_old_sigaction *old_act;
bellard66fb9762003-03-23 01:06:05 +00003607 struct target_sigaction act, oact, *pact;
pbrook53a59602006-03-25 19:31:22 +00003608 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00003609 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3610 goto efault;
bellard66fb9762003-03-23 01:06:05 +00003611 act._sa_handler = old_act->_sa_handler;
3612 target_siginitset(&act.sa_mask, old_act->sa_mask);
3613 act.sa_flags = old_act->sa_flags;
3614 act.sa_restorer = old_act->sa_restorer;
pbrook53a59602006-03-25 19:31:22 +00003615 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003616 pact = &act;
3617 } else {
3618 pact = NULL;
3619 }
3620 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00003621 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003622 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3623 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003624 old_act->_sa_handler = oact._sa_handler;
3625 old_act->sa_mask = oact.sa_mask.sig[0];
3626 old_act->sa_flags = oact.sa_flags;
3627 old_act->sa_restorer = oact.sa_restorer;
3628 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00003629 }
ths388bb212007-05-13 13:58:00 +00003630#else
bellard106ec872006-06-27 21:08:10 +00003631 struct target_sigaction act, oact, *pact, *old_act;
3632
3633 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00003634 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3635 goto efault;
bellard106ec872006-06-27 21:08:10 +00003636 act._sa_handler = old_act->_sa_handler;
3637 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3638 act.sa_flags = old_act->sa_flags;
3639 unlock_user_struct(old_act, arg2, 0);
3640 pact = &act;
3641 } else {
3642 pact = NULL;
3643 }
3644
3645 ret = get_errno(do_sigaction(arg1, pact, &oact));
3646
3647 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003648 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3649 goto efault;
bellard106ec872006-06-27 21:08:10 +00003650 old_act->_sa_handler = oact._sa_handler;
3651 old_act->sa_flags = oact.sa_flags;
3652 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3653 old_act->sa_mask.sig[1] = 0;
3654 old_act->sa_mask.sig[2] = 0;
3655 old_act->sa_mask.sig[3] = 0;
3656 unlock_user_struct(old_act, arg3, 1);
3657 }
ths388bb212007-05-13 13:58:00 +00003658#endif
bellard31e31b82003-02-18 22:55:36 +00003659 }
3660 break;
thse5febef2007-04-01 18:31:35 +00003661#endif
bellard66fb9762003-03-23 01:06:05 +00003662 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00003663 {
3664 struct target_sigaction *act;
3665 struct target_sigaction *oact;
3666
bellard579a97f2007-11-11 14:26:47 +00003667 if (arg2) {
3668 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
3669 goto efault;
3670 } else
pbrook53a59602006-03-25 19:31:22 +00003671 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00003672 if (arg3) {
3673 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
3674 ret = -TARGET_EFAULT;
3675 goto rt_sigaction_fail;
3676 }
3677 } else
pbrook53a59602006-03-25 19:31:22 +00003678 oact = NULL;
3679 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00003680 rt_sigaction_fail:
3681 if (act)
pbrook53a59602006-03-25 19:31:22 +00003682 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00003683 if (oact)
pbrook53a59602006-03-25 19:31:22 +00003684 unlock_user_struct(oact, arg3, 1);
3685 }
bellard66fb9762003-03-23 01:06:05 +00003686 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003687#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003688 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00003689 {
3690 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00003691 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00003692 sigprocmask(0, NULL, &cur_set);
3693 host_to_target_old_sigset(&target_set, &cur_set);
3694 ret = target_set;
3695 }
3696 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003697#endif
3698#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003699 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00003700 {
3701 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00003702 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00003703 sigprocmask(0, NULL, &cur_set);
3704 target_to_host_old_sigset(&set, &target_set);
3705 sigorset(&set, &set, &cur_set);
3706 sigprocmask(SIG_SETMASK, &set, &oset);
3707 host_to_target_old_sigset(&target_set, &oset);
3708 ret = target_set;
3709 }
3710 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003711#endif
thse5febef2007-04-01 18:31:35 +00003712#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00003713 case TARGET_NR_sigprocmask:
3714 {
3715 int how = arg1;
3716 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003717
pbrook53a59602006-03-25 19:31:22 +00003718 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003719 switch(how) {
3720 case TARGET_SIG_BLOCK:
3721 how = SIG_BLOCK;
3722 break;
3723 case TARGET_SIG_UNBLOCK:
3724 how = SIG_UNBLOCK;
3725 break;
3726 case TARGET_SIG_SETMASK:
3727 how = SIG_SETMASK;
3728 break;
3729 default:
ths0da46a62007-10-20 20:23:07 +00003730 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00003731 goto fail;
3732 }
bellard579a97f2007-11-11 14:26:47 +00003733 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3734 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003735 target_to_host_old_sigset(&set, p);
3736 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003737 set_ptr = &set;
3738 } else {
3739 how = 0;
3740 set_ptr = NULL;
3741 }
3742 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003743 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003744 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3745 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003746 host_to_target_old_sigset(p, &oldset);
3747 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003748 }
3749 }
3750 break;
thse5febef2007-04-01 18:31:35 +00003751#endif
bellard66fb9762003-03-23 01:06:05 +00003752 case TARGET_NR_rt_sigprocmask:
3753 {
3754 int how = arg1;
3755 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003756
pbrook53a59602006-03-25 19:31:22 +00003757 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003758 switch(how) {
3759 case TARGET_SIG_BLOCK:
3760 how = SIG_BLOCK;
3761 break;
3762 case TARGET_SIG_UNBLOCK:
3763 how = SIG_UNBLOCK;
3764 break;
3765 case TARGET_SIG_SETMASK:
3766 how = SIG_SETMASK;
3767 break;
3768 default:
ths0da46a62007-10-20 20:23:07 +00003769 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00003770 goto fail;
3771 }
bellard579a97f2007-11-11 14:26:47 +00003772 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3773 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003774 target_to_host_sigset(&set, p);
3775 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003776 set_ptr = &set;
3777 } else {
3778 how = 0;
3779 set_ptr = NULL;
3780 }
3781 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003782 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003783 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3784 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003785 host_to_target_sigset(p, &oldset);
3786 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003787 }
3788 }
3789 break;
thse5febef2007-04-01 18:31:35 +00003790#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00003791 case TARGET_NR_sigpending:
3792 {
3793 sigset_t set;
3794 ret = get_errno(sigpending(&set));
3795 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003796 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3797 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003798 host_to_target_old_sigset(p, &set);
3799 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003800 }
3801 }
3802 break;
thse5febef2007-04-01 18:31:35 +00003803#endif
bellard66fb9762003-03-23 01:06:05 +00003804 case TARGET_NR_rt_sigpending:
3805 {
3806 sigset_t set;
3807 ret = get_errno(sigpending(&set));
3808 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003809 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3810 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003811 host_to_target_sigset(p, &set);
3812 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003813 }
3814 }
3815 break;
thse5febef2007-04-01 18:31:35 +00003816#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00003817 case TARGET_NR_sigsuspend:
3818 {
3819 sigset_t set;
bellard579a97f2007-11-11 14:26:47 +00003820 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3821 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003822 target_to_host_old_sigset(&set, p);
3823 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003824 ret = get_errno(sigsuspend(&set));
3825 }
3826 break;
thse5febef2007-04-01 18:31:35 +00003827#endif
bellard66fb9762003-03-23 01:06:05 +00003828 case TARGET_NR_rt_sigsuspend:
3829 {
3830 sigset_t set;
bellard579a97f2007-11-11 14:26:47 +00003831 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3832 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003833 target_to_host_sigset(&set, p);
3834 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003835 ret = get_errno(sigsuspend(&set));
3836 }
3837 break;
3838 case TARGET_NR_rt_sigtimedwait:
3839 {
bellard66fb9762003-03-23 01:06:05 +00003840 sigset_t set;
3841 struct timespec uts, *puts;
3842 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00003843
bellard579a97f2007-11-11 14:26:47 +00003844 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3845 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003846 target_to_host_sigset(&set, p);
3847 unlock_user(p, arg1, 0);
3848 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00003849 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00003850 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00003851 } else {
3852 puts = NULL;
3853 }
3854 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00003855 if (!is_error(ret) && arg2) {
bellard579a97f2007-11-11 14:26:47 +00003856 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_sigset_t), 0)))
3857 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003858 host_to_target_siginfo(p, &uinfo);
3859 unlock_user(p, arg2, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003860 }
3861 }
3862 break;
3863 case TARGET_NR_rt_sigqueueinfo:
3864 {
3865 siginfo_t uinfo;
bellard579a97f2007-11-11 14:26:47 +00003866 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
3867 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003868 target_to_host_siginfo(&uinfo, p);
3869 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003870 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3871 }
3872 break;
thse5febef2007-04-01 18:31:35 +00003873#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00003874 case TARGET_NR_sigreturn:
3875 /* NOTE: ret is eax, so not transcoding must be done */
3876 ret = do_sigreturn(cpu_env);
3877 break;
thse5febef2007-04-01 18:31:35 +00003878#endif
bellard66fb9762003-03-23 01:06:05 +00003879 case TARGET_NR_rt_sigreturn:
3880 /* NOTE: ret is eax, so not transcoding must be done */
3881 ret = do_rt_sigreturn(cpu_env);
3882 break;
bellard31e31b82003-02-18 22:55:36 +00003883 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00003884 if (!(p = lock_user_string(arg1)))
3885 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003886 ret = get_errno(sethostname(p, arg2));
3887 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003888 break;
3889 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00003890 {
3891 /* XXX: convert resource ? */
3892 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003893 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003894 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00003895 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
3896 goto efault;
bellard9de5e442003-03-23 16:49:39 +00003897 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3898 rlim.rlim_max = tswapl(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00003899 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00003900 ret = get_errno(setrlimit(resource, &rlim));
3901 }
3902 break;
bellard31e31b82003-02-18 22:55:36 +00003903 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00003904 {
3905 /* XXX: convert resource ? */
3906 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003907 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003908 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00003909
bellard9de5e442003-03-23 16:49:39 +00003910 ret = get_errno(getrlimit(resource, &rlim));
3911 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003912 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
3913 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003914 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3915 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3916 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00003917 }
3918 }
3919 break;
bellard31e31b82003-02-18 22:55:36 +00003920 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00003921 {
3922 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00003923 ret = get_errno(getrusage(arg1, &rusage));
3924 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003925 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00003926 }
3927 }
3928 break;
bellard31e31b82003-02-18 22:55:36 +00003929 case TARGET_NR_gettimeofday:
3930 {
bellard31e31b82003-02-18 22:55:36 +00003931 struct timeval tv;
3932 ret = get_errno(gettimeofday(&tv, NULL));
3933 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003934 host_to_target_timeval(arg1, &tv);
bellard31e31b82003-02-18 22:55:36 +00003935 }
3936 }
3937 break;
3938 case TARGET_NR_settimeofday:
3939 {
bellard31e31b82003-02-18 22:55:36 +00003940 struct timeval tv;
pbrook53a59602006-03-25 19:31:22 +00003941 target_to_host_timeval(&tv, arg1);
bellard31e31b82003-02-18 22:55:36 +00003942 ret = get_errno(settimeofday(&tv, NULL));
3943 }
3944 break;
bellard048f6b42005-11-26 18:47:20 +00003945#ifdef TARGET_NR_select
bellard31e31b82003-02-18 22:55:36 +00003946 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00003947 {
pbrook53a59602006-03-25 19:31:22 +00003948 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00003949 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00003950 long nsel;
3951
bellard579a97f2007-11-11 14:26:47 +00003952 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
3953 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003954 nsel = tswapl(sel->n);
3955 inp = tswapl(sel->inp);
3956 outp = tswapl(sel->outp);
3957 exp = tswapl(sel->exp);
3958 tvp = tswapl(sel->tvp);
3959 unlock_user_struct(sel, arg1, 0);
3960 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00003961 }
3962 break;
bellard048f6b42005-11-26 18:47:20 +00003963#endif
bellard31e31b82003-02-18 22:55:36 +00003964 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00003965 {
3966 void *p2;
3967 p = lock_user_string(arg1);
3968 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00003969 if (!p || !p2)
3970 ret = -TARGET_EFAULT;
3971 else
3972 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00003973 unlock_user(p2, arg2, 0);
3974 unlock_user(p, arg1, 0);
3975 }
bellard31e31b82003-02-18 22:55:36 +00003976 break;
thsf0b62432007-09-24 09:25:40 +00003977#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
3978 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00003979 {
bellard579a97f2007-11-11 14:26:47 +00003980 void *p2;
thsf0b62432007-09-24 09:25:40 +00003981 p = lock_user_string(arg1);
3982 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00003983 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00003984 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00003985 else
3986 ret = get_errno(sys_symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00003987 unlock_user(p2, arg3, 0);
3988 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00003989 }
3990 break;
3991#endif
bellardebc05482003-09-30 21:08:41 +00003992#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00003993 case TARGET_NR_oldlstat:
3994 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003995#endif
bellard31e31b82003-02-18 22:55:36 +00003996 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00003997 {
3998 void *p2;
3999 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00004000 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
4001 if (!p || !p2)
4002 ret = -TARGET_EFAULT;
4003 else
4004 ret = get_errno(readlink(path(p), p2, arg3));
pbrook53a59602006-03-25 19:31:22 +00004005 unlock_user(p2, arg2, ret);
4006 unlock_user(p, arg1, 0);
4007 }
bellard31e31b82003-02-18 22:55:36 +00004008 break;
ths5e0ccb12007-09-24 09:26:10 +00004009#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
4010 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00004011 {
bellard579a97f2007-11-11 14:26:47 +00004012 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00004013 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004014 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
4015 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004016 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00004017 else
4018 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
bellard579a97f2007-11-11 14:26:47 +00004019 unlock_user(p2, arg3, ret);
4020 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00004021 }
4022 break;
4023#endif
thse5febef2007-04-01 18:31:35 +00004024#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00004025 case TARGET_NR_uselib:
4026 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004027#endif
4028#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00004029 case TARGET_NR_swapon:
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(swapon(p, arg2));
4033 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004034 break;
thse5febef2007-04-01 18:31:35 +00004035#endif
bellard31e31b82003-02-18 22:55:36 +00004036 case TARGET_NR_reboot:
4037 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004038#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00004039 case TARGET_NR_readdir:
4040 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004041#endif
4042#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00004043 case TARGET_NR_mmap:
bellardd2fd1af2007-11-14 18:08:56 +00004044#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
bellard31e31b82003-02-18 22:55:36 +00004045 {
blueswir1992f48a2007-10-14 16:27:31 +00004046 abi_ulong *v;
4047 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00004048 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
4049 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004050 v1 = tswapl(v[0]);
4051 v2 = tswapl(v[1]);
4052 v3 = tswapl(v[2]);
4053 v4 = tswapl(v[3]);
4054 v5 = tswapl(v[4]);
4055 v6 = tswapl(v[5]);
4056 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00004057 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00004058 target_to_host_bitmask(v4, mmap_flags_tbl),
4059 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00004060 }
bellard31e31b82003-02-18 22:55:36 +00004061#else
ths5fafdf22007-09-16 21:08:06 +00004062 ret = get_errno(target_mmap(arg1, arg2, arg3,
4063 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00004064 arg5,
4065 arg6));
bellard31e31b82003-02-18 22:55:36 +00004066#endif
bellard6fb883e2003-07-09 17:12:39 +00004067 break;
thse5febef2007-04-01 18:31:35 +00004068#endif
bellarda315a142005-01-30 22:59:18 +00004069#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00004070 case TARGET_NR_mmap2:
ths0d3267a2006-12-11 22:04:25 +00004071#if defined(TARGET_SPARC) || defined(TARGET_MIPS)
bellardc573ff62004-01-04 15:51:36 +00004072#define MMAP_SHIFT 12
4073#else
4074#define MMAP_SHIFT TARGET_PAGE_BITS
4075#endif
ths5fafdf22007-09-16 21:08:06 +00004076 ret = get_errno(target_mmap(arg1, arg2, arg3,
4077 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00004078 arg5,
bellardc573ff62004-01-04 15:51:36 +00004079 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00004080 break;
bellarda315a142005-01-30 22:59:18 +00004081#endif
bellard31e31b82003-02-18 22:55:36 +00004082 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00004083 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00004084 break;
bellard9de5e442003-03-23 16:49:39 +00004085 case TARGET_NR_mprotect:
bellard54936002003-05-13 00:25:15 +00004086 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00004087 break;
thse5febef2007-04-01 18:31:35 +00004088#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00004089 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00004090 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00004091 break;
thse5febef2007-04-01 18:31:35 +00004092#endif
pbrook53a59602006-03-25 19:31:22 +00004093 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00004094#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00004095 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00004096 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00004097 break;
thse5febef2007-04-01 18:31:35 +00004098#endif
4099#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00004100 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00004101 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00004102 break;
thse5febef2007-04-01 18:31:35 +00004103#endif
4104#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00004105 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00004106 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00004107 break;
thse5febef2007-04-01 18:31:35 +00004108#endif
4109#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00004110 case TARGET_NR_mlockall:
4111 ret = get_errno(mlockall(arg1));
4112 break;
thse5febef2007-04-01 18:31:35 +00004113#endif
4114#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00004115 case TARGET_NR_munlockall:
4116 ret = get_errno(munlockall());
4117 break;
thse5febef2007-04-01 18:31:35 +00004118#endif
bellard31e31b82003-02-18 22:55:36 +00004119 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00004120 if (!(p = lock_user_string(arg1)))
4121 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004122 ret = get_errno(truncate(p, arg2));
4123 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004124 break;
4125 case TARGET_NR_ftruncate:
4126 ret = get_errno(ftruncate(arg1, arg2));
4127 break;
4128 case TARGET_NR_fchmod:
4129 ret = get_errno(fchmod(arg1, arg2));
4130 break;
ths814d7972007-09-24 09:26:51 +00004131#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
4132 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00004133 if (!(p = lock_user_string(arg2)))
4134 goto efault;
4135 ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
4136 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00004137 break;
4138#endif
bellard31e31b82003-02-18 22:55:36 +00004139 case TARGET_NR_getpriority:
thsc6cda172007-10-09 03:42:34 +00004140 /* libc does special remapping of the return value of
4141 * sys_getpriority() so it's just easiest to call
4142 * sys_getpriority() directly rather than through libc. */
4143 ret = sys_getpriority(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00004144 break;
4145 case TARGET_NR_setpriority:
4146 ret = get_errno(setpriority(arg1, arg2, arg3));
4147 break;
bellardebc05482003-09-30 21:08:41 +00004148#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00004149 case TARGET_NR_profil:
4150 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004151#endif
bellard31e31b82003-02-18 22:55:36 +00004152 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00004153 if (!(p = lock_user_string(arg1)))
4154 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004155 ret = get_errno(statfs(path(p), &stfs));
4156 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004157 convert_statfs:
4158 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004159 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00004160
bellard579a97f2007-11-11 14:26:47 +00004161 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
4162 goto efault;
4163 __put_user(stfs.f_type, &target_stfs->f_type);
4164 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
4165 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
4166 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
4167 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
4168 __put_user(stfs.f_files, &target_stfs->f_files);
4169 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
4170 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
4171 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
4172 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00004173 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00004174 }
4175 break;
4176 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00004177 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00004178 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00004179#ifdef TARGET_NR_statfs64
4180 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00004181 if (!(p = lock_user_string(arg1)))
4182 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004183 ret = get_errno(statfs(path(p), &stfs));
4184 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00004185 convert_statfs64:
4186 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004187 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00004188
bellard579a97f2007-11-11 14:26:47 +00004189 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
4190 goto efault;
4191 __put_user(stfs.f_type, &target_stfs->f_type);
4192 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
4193 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
4194 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
4195 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
4196 __put_user(stfs.f_files, &target_stfs->f_files);
4197 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
4198 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
4199 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
4200 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
4201 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00004202 }
4203 break;
4204 case TARGET_NR_fstatfs64:
4205 ret = get_errno(fstatfs(arg1, &stfs));
4206 goto convert_statfs64;
4207#endif
bellardebc05482003-09-30 21:08:41 +00004208#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00004209 case TARGET_NR_ioperm:
4210 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004211#endif
thse5febef2007-04-01 18:31:35 +00004212#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00004213 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00004214 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00004215 break;
thse5febef2007-04-01 18:31:35 +00004216#endif
bellard3532fa72006-06-24 15:06:03 +00004217#ifdef TARGET_NR_accept
4218 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00004219 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00004220 break;
4221#endif
4222#ifdef TARGET_NR_bind
4223 case TARGET_NR_bind:
4224 ret = do_bind(arg1, arg2, arg3);
4225 break;
4226#endif
4227#ifdef TARGET_NR_connect
4228 case TARGET_NR_connect:
4229 ret = do_connect(arg1, arg2, arg3);
4230 break;
4231#endif
4232#ifdef TARGET_NR_getpeername
4233 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00004234 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00004235 break;
4236#endif
4237#ifdef TARGET_NR_getsockname
4238 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00004239 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00004240 break;
4241#endif
4242#ifdef TARGET_NR_getsockopt
4243 case TARGET_NR_getsockopt:
4244 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
4245 break;
4246#endif
4247#ifdef TARGET_NR_listen
4248 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00004249 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00004250 break;
4251#endif
4252#ifdef TARGET_NR_recv
4253 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00004254 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00004255 break;
4256#endif
4257#ifdef TARGET_NR_recvfrom
4258 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00004259 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00004260 break;
4261#endif
4262#ifdef TARGET_NR_recvmsg
4263 case TARGET_NR_recvmsg:
4264 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
4265 break;
4266#endif
4267#ifdef TARGET_NR_send
4268 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00004269 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00004270 break;
4271#endif
4272#ifdef TARGET_NR_sendmsg
4273 case TARGET_NR_sendmsg:
4274 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
4275 break;
4276#endif
4277#ifdef TARGET_NR_sendto
4278 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00004279 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00004280 break;
4281#endif
4282#ifdef TARGET_NR_shutdown
4283 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00004284 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00004285 break;
4286#endif
4287#ifdef TARGET_NR_socket
4288 case TARGET_NR_socket:
4289 ret = do_socket(arg1, arg2, arg3);
4290 break;
4291#endif
4292#ifdef TARGET_NR_socketpair
4293 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00004294 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00004295 break;
4296#endif
4297#ifdef TARGET_NR_setsockopt
4298 case TARGET_NR_setsockopt:
4299 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
4300 break;
4301#endif
ths7494b0f2007-02-11 18:26:53 +00004302
bellard31e31b82003-02-18 22:55:36 +00004303 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00004304 if (!(p = lock_user_string(arg2)))
4305 goto efault;
thse5574482007-02-11 20:03:13 +00004306 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
4307 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00004308 break;
4309
bellard31e31b82003-02-18 22:55:36 +00004310 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00004311 {
bellard66fb9762003-03-23 01:06:05 +00004312 struct itimerval value, ovalue, *pvalue;
4313
pbrook53a59602006-03-25 19:31:22 +00004314 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00004315 pvalue = &value;
ths5fafdf22007-09-16 21:08:06 +00004316 target_to_host_timeval(&pvalue->it_interval,
pbrook53a59602006-03-25 19:31:22 +00004317 arg2);
ths5fafdf22007-09-16 21:08:06 +00004318 target_to_host_timeval(&pvalue->it_value,
pbrook53a59602006-03-25 19:31:22 +00004319 arg2 + sizeof(struct target_timeval));
bellard66fb9762003-03-23 01:06:05 +00004320 } else {
4321 pvalue = NULL;
4322 }
4323 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00004324 if (!is_error(ret) && arg3) {
4325 host_to_target_timeval(arg3,
bellard66fb9762003-03-23 01:06:05 +00004326 &ovalue.it_interval);
pbrook53a59602006-03-25 19:31:22 +00004327 host_to_target_timeval(arg3 + sizeof(struct target_timeval),
bellard66fb9762003-03-23 01:06:05 +00004328 &ovalue.it_value);
4329 }
4330 }
4331 break;
bellard31e31b82003-02-18 22:55:36 +00004332 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00004333 {
bellard66fb9762003-03-23 01:06:05 +00004334 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00004335
bellard66fb9762003-03-23 01:06:05 +00004336 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00004337 if (!is_error(ret) && arg2) {
4338 host_to_target_timeval(arg2,
bellard66fb9762003-03-23 01:06:05 +00004339 &value.it_interval);
pbrook53a59602006-03-25 19:31:22 +00004340 host_to_target_timeval(arg2 + sizeof(struct target_timeval),
bellard66fb9762003-03-23 01:06:05 +00004341 &value.it_value);
4342 }
4343 }
4344 break;
bellard31e31b82003-02-18 22:55:36 +00004345 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00004346 if (!(p = lock_user_string(arg1)))
4347 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004348 ret = get_errno(stat(path(p), &st));
4349 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004350 goto do_stat;
4351 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00004352 if (!(p = lock_user_string(arg1)))
4353 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004354 ret = get_errno(lstat(path(p), &st));
4355 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004356 goto do_stat;
4357 case TARGET_NR_fstat:
4358 {
4359 ret = get_errno(fstat(arg1, &st));
4360 do_stat:
4361 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004362 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00004363
bellard579a97f2007-11-11 14:26:47 +00004364 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4365 goto efault;
bellardd2fd1af2007-11-14 18:08:56 +00004366 __put_user(st.st_dev, &target_st->st_dev);
4367 __put_user(st.st_ino, &target_st->st_ino);
4368 __put_user(st.st_mode, &target_st->st_mode);
4369 __put_user(st.st_uid, &target_st->st_uid);
4370 __put_user(st.st_gid, &target_st->st_gid);
4371 __put_user(st.st_nlink, &target_st->st_nlink);
4372 __put_user(st.st_rdev, &target_st->st_rdev);
4373 __put_user(st.st_size, &target_st->st_size);
4374 __put_user(st.st_blksize, &target_st->st_blksize);
4375 __put_user(st.st_blocks, &target_st->st_blocks);
4376 __put_user(st.st_atime, &target_st->target_st_atime);
4377 __put_user(st.st_mtime, &target_st->target_st_mtime);
4378 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00004379 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00004380 }
4381 }
4382 break;
bellardebc05482003-09-30 21:08:41 +00004383#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00004384 case TARGET_NR_olduname:
4385 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004386#endif
4387#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00004388 case TARGET_NR_iopl:
4389 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004390#endif
bellard31e31b82003-02-18 22:55:36 +00004391 case TARGET_NR_vhangup:
4392 ret = get_errno(vhangup());
4393 break;
bellardebc05482003-09-30 21:08:41 +00004394#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00004395 case TARGET_NR_idle:
4396 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004397#endif
bellard42ad6ae2005-01-03 22:48:11 +00004398#ifdef TARGET_NR_syscall
4399 case TARGET_NR_syscall:
4400 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
4401 break;
4402#endif
bellard31e31b82003-02-18 22:55:36 +00004403 case TARGET_NR_wait4:
4404 {
4405 int status;
blueswir1992f48a2007-10-14 16:27:31 +00004406 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00004407 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00004408 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00004409 if (target_rusage)
4410 rusage_ptr = &rusage;
4411 else
4412 rusage_ptr = NULL;
4413 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
4414 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00004415 if (status_ptr) {
4416 if (put_user_s32(status, status_ptr))
4417 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004418 }
bellard2f619692007-11-16 10:46:05 +00004419 if (target_rusage)
4420 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00004421 }
4422 }
4423 break;
thse5febef2007-04-01 18:31:35 +00004424#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00004425 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00004426 if (!(p = lock_user_string(arg1)))
4427 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004428 ret = get_errno(swapoff(p));
4429 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004430 break;
thse5febef2007-04-01 18:31:35 +00004431#endif
bellard31e31b82003-02-18 22:55:36 +00004432 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00004433 {
pbrook53a59602006-03-25 19:31:22 +00004434 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00004435 struct sysinfo value;
4436 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00004437 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00004438 {
bellard579a97f2007-11-11 14:26:47 +00004439 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
4440 goto efault;
bellarda5448a72004-06-19 16:59:03 +00004441 __put_user(value.uptime, &target_value->uptime);
4442 __put_user(value.loads[0], &target_value->loads[0]);
4443 __put_user(value.loads[1], &target_value->loads[1]);
4444 __put_user(value.loads[2], &target_value->loads[2]);
4445 __put_user(value.totalram, &target_value->totalram);
4446 __put_user(value.freeram, &target_value->freeram);
4447 __put_user(value.sharedram, &target_value->sharedram);
4448 __put_user(value.bufferram, &target_value->bufferram);
4449 __put_user(value.totalswap, &target_value->totalswap);
4450 __put_user(value.freeswap, &target_value->freeswap);
4451 __put_user(value.procs, &target_value->procs);
4452 __put_user(value.totalhigh, &target_value->totalhigh);
4453 __put_user(value.freehigh, &target_value->freehigh);
4454 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00004455 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00004456 }
4457 }
4458 break;
thse5febef2007-04-01 18:31:35 +00004459#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00004460 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00004461 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
4462 break;
thse5febef2007-04-01 18:31:35 +00004463#endif
bellard31e31b82003-02-18 22:55:36 +00004464 case TARGET_NR_fsync:
4465 ret = get_errno(fsync(arg1));
4466 break;
bellard31e31b82003-02-18 22:55:36 +00004467 case TARGET_NR_clone:
bellard1b6b0292003-03-22 17:31:38 +00004468 ret = get_errno(do_fork(cpu_env, arg1, arg2));
4469 break;
bellardec86b0f2003-04-11 00:15:04 +00004470#ifdef __NR_exit_group
4471 /* new thread calls */
4472 case TARGET_NR_exit_group:
bellarde9009672005-04-26 20:42:36 +00004473 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00004474 ret = get_errno(exit_group(arg1));
4475 break;
4476#endif
bellard31e31b82003-02-18 22:55:36 +00004477 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00004478 if (!(p = lock_user_string(arg1)))
4479 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004480 ret = get_errno(setdomainname(p, arg2));
4481 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004482 break;
4483 case TARGET_NR_uname:
4484 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00004485 {
4486 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00004487
bellard579a97f2007-11-11 14:26:47 +00004488 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
4489 goto efault;
bellard29e619b2004-09-13 21:41:04 +00004490 ret = get_errno(sys_uname(buf));
4491 if (!is_error(ret)) {
4492 /* Overrite the native machine name with whatever is being
4493 emulated. */
4494 strcpy (buf->machine, UNAME_MACHINE);
pbrookc5937222006-05-14 11:30:38 +00004495 /* Allow the user to override the reported release. */
4496 if (qemu_uname_release && *qemu_uname_release)
4497 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00004498 }
pbrook53a59602006-03-25 19:31:22 +00004499 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00004500 }
bellard31e31b82003-02-18 22:55:36 +00004501 break;
bellard6dbad632003-03-16 18:05:05 +00004502#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00004503 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00004504 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00004505 break;
j_mayer84409dd2007-04-06 08:56:50 +00004506#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00004507 case TARGET_NR_vm86old:
4508 goto unimplemented;
4509 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00004510 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00004511 break;
4512#endif
j_mayer84409dd2007-04-06 08:56:50 +00004513#endif
bellard31e31b82003-02-18 22:55:36 +00004514 case TARGET_NR_adjtimex:
4515 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004516#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00004517 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00004518#endif
bellard31e31b82003-02-18 22:55:36 +00004519 case TARGET_NR_init_module:
4520 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00004521#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00004522 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00004523#endif
bellard31e31b82003-02-18 22:55:36 +00004524 goto unimplemented;
4525 case TARGET_NR_quotactl:
4526 goto unimplemented;
4527 case TARGET_NR_getpgid:
4528 ret = get_errno(getpgid(arg1));
4529 break;
4530 case TARGET_NR_fchdir:
4531 ret = get_errno(fchdir(arg1));
4532 break;
j_mayer84409dd2007-04-06 08:56:50 +00004533#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00004534 case TARGET_NR_bdflush:
4535 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00004536#endif
thse5febef2007-04-01 18:31:35 +00004537#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00004538 case TARGET_NR_sysfs:
4539 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004540#endif
bellard31e31b82003-02-18 22:55:36 +00004541 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00004542 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00004543 break;
thse5febef2007-04-01 18:31:35 +00004544#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00004545 case TARGET_NR_afs_syscall:
4546 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004547#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004548#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004549 case TARGET_NR__llseek:
4550 {
bellard4f2ac232004-04-26 19:44:02 +00004551#if defined (__x86_64__)
4552 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
bellard2f619692007-11-16 10:46:05 +00004553 if (put_user_s64(ret, arg4))
4554 goto efault;
bellard4f2ac232004-04-26 19:44:02 +00004555#else
bellard31e31b82003-02-18 22:55:36 +00004556 int64_t res;
4557 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard2f619692007-11-16 10:46:05 +00004558 if (put_user_s64(res, arg4))
4559 goto efault;
bellard4f2ac232004-04-26 19:44:02 +00004560#endif
bellard31e31b82003-02-18 22:55:36 +00004561 }
4562 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004563#endif
bellard31e31b82003-02-18 22:55:36 +00004564 case TARGET_NR_getdents:
blueswir1992f48a2007-10-14 16:27:31 +00004565#if TARGET_ABI_BITS != 32
pbrook53a59602006-03-25 19:31:22 +00004566 goto unimplemented;
blueswir1992f48a2007-10-14 16:27:31 +00004567#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00004568 {
pbrook53a59602006-03-25 19:31:22 +00004569 struct target_dirent *target_dirp;
bellard4add45b2003-06-05 01:52:59 +00004570 struct dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004571 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00004572
4573 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00004574 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00004575 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00004576 goto fail;
4577 }
ths3b46e622007-09-17 08:09:54 +00004578
bellard4add45b2003-06-05 01:52:59 +00004579 ret = get_errno(sys_getdents(arg1, dirp, count));
4580 if (!is_error(ret)) {
4581 struct dirent *de;
4582 struct target_dirent *tde;
4583 int len = ret;
4584 int reclen, treclen;
4585 int count1, tnamelen;
4586
4587 count1 = 0;
4588 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00004589 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4590 goto efault;
bellard4add45b2003-06-05 01:52:59 +00004591 tde = target_dirp;
4592 while (len > 0) {
4593 reclen = de->d_reclen;
blueswir1992f48a2007-10-14 16:27:31 +00004594 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
bellard4add45b2003-06-05 01:52:59 +00004595 tde->d_reclen = tswap16(treclen);
4596 tde->d_ino = tswapl(de->d_ino);
4597 tde->d_off = tswapl(de->d_off);
blueswir1992f48a2007-10-14 16:27:31 +00004598 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
bellard4add45b2003-06-05 01:52:59 +00004599 if (tnamelen > 256)
4600 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00004601 /* XXX: may not be correct */
bellard4add45b2003-06-05 01:52:59 +00004602 strncpy(tde->d_name, de->d_name, tnamelen);
4603 de = (struct dirent *)((char *)de + reclen);
4604 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00004605 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00004606 count1 += treclen;
4607 }
4608 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00004609 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00004610 }
4611 free(dirp);
4612 }
4613#else
bellard31e31b82003-02-18 22:55:36 +00004614 {
pbrook53a59602006-03-25 19:31:22 +00004615 struct dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004616 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00004617
bellard579a97f2007-11-11 14:26:47 +00004618 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4619 goto efault;
bellard72f03902003-02-18 23:33:18 +00004620 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00004621 if (!is_error(ret)) {
4622 struct dirent *de;
4623 int len = ret;
4624 int reclen;
4625 de = dirp;
4626 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004627 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00004628 if (reclen > len)
4629 break;
bellard8083a3e2003-03-24 23:12:16 +00004630 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00004631 tswapls(&de->d_ino);
4632 tswapls(&de->d_off);
4633 de = (struct dirent *)((char *)de + reclen);
4634 len -= reclen;
4635 }
4636 }
pbrook53a59602006-03-25 19:31:22 +00004637 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00004638 }
bellard4add45b2003-06-05 01:52:59 +00004639#endif
bellard31e31b82003-02-18 22:55:36 +00004640 break;
ths3ae43202007-09-16 21:39:48 +00004641#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00004642 case TARGET_NR_getdents64:
4643 {
pbrook53a59602006-03-25 19:31:22 +00004644 struct dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004645 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00004646 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4647 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00004648 ret = get_errno(sys_getdents64(arg1, dirp, count));
4649 if (!is_error(ret)) {
4650 struct dirent64 *de;
4651 int len = ret;
4652 int reclen;
4653 de = dirp;
4654 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004655 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00004656 if (reclen > len)
4657 break;
bellard8083a3e2003-03-24 23:12:16 +00004658 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00004659 tswap64s((uint64_t *)&de->d_ino);
4660 tswap64s((uint64_t *)&de->d_off);
bellarddab2ed92003-03-22 15:23:14 +00004661 de = (struct dirent64 *)((char *)de + reclen);
4662 len -= reclen;
4663 }
4664 }
pbrook53a59602006-03-25 19:31:22 +00004665 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00004666 }
4667 break;
bellarda541f292004-04-12 20:39:29 +00004668#endif /* TARGET_NR_getdents64 */
thse5febef2007-04-01 18:31:35 +00004669#ifdef TARGET_NR__newselect
bellard31e31b82003-02-18 22:55:36 +00004670 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00004671 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00004672 break;
thse5febef2007-04-01 18:31:35 +00004673#endif
4674#ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00004675 case TARGET_NR_poll:
4676 {
pbrook53a59602006-03-25 19:31:22 +00004677 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00004678 unsigned int nfds = arg2;
4679 int timeout = arg3;
4680 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00004681 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00004682
bellard579a97f2007-11-11 14:26:47 +00004683 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
4684 if (!target_pfd)
4685 goto efault;
bellard9de5e442003-03-23 16:49:39 +00004686 pfd = alloca(sizeof(struct pollfd) * nfds);
4687 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004688 pfd[i].fd = tswap32(target_pfd[i].fd);
4689 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00004690 }
4691 ret = get_errno(poll(pfd, nfds, timeout));
4692 if (!is_error(ret)) {
4693 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004694 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00004695 }
pbrook53a59602006-03-25 19:31:22 +00004696 ret += nfds * (sizeof(struct target_pollfd)
4697 - sizeof(struct pollfd));
bellard9de5e442003-03-23 16:49:39 +00004698 }
pbrook53a59602006-03-25 19:31:22 +00004699 unlock_user(target_pfd, arg1, ret);
bellard9de5e442003-03-23 16:49:39 +00004700 }
4701 break;
thse5febef2007-04-01 18:31:35 +00004702#endif
bellard31e31b82003-02-18 22:55:36 +00004703 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00004704 /* NOTE: the flock constant seems to be the same for every
4705 Linux platform */
4706 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00004707 break;
4708 case TARGET_NR_readv:
4709 {
4710 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00004711 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00004712
4713 vec = alloca(count * sizeof(struct iovec));
bellard579a97f2007-11-11 14:26:47 +00004714 lock_iovec(VERIFY_WRITE, vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00004715 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00004716 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00004717 }
4718 break;
4719 case TARGET_NR_writev:
4720 {
4721 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00004722 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00004723
4724 vec = alloca(count * sizeof(struct iovec));
bellard579a97f2007-11-11 14:26:47 +00004725 lock_iovec(VERIFY_READ, vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00004726 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00004727 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00004728 }
4729 break;
4730 case TARGET_NR_getsid:
4731 ret = get_errno(getsid(arg1));
4732 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004733#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00004734 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00004735 ret = get_errno(fdatasync(arg1));
4736 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004737#endif
bellard31e31b82003-02-18 22:55:36 +00004738 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00004739 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00004740 return value. */
ths0da46a62007-10-20 20:23:07 +00004741 ret = -TARGET_ENOTDIR;
4742 break;
bellard31e31b82003-02-18 22:55:36 +00004743 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00004744 {
pbrook53a59602006-03-25 19:31:22 +00004745 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004746 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00004747
bellard579a97f2007-11-11 14:26:47 +00004748 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
4749 goto efault;
bellard5cd43932003-03-29 16:54:36 +00004750 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004751 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00004752 ret = get_errno(sched_setparam(arg1, &schp));
4753 }
4754 break;
bellard31e31b82003-02-18 22:55:36 +00004755 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00004756 {
pbrook53a59602006-03-25 19:31:22 +00004757 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004758 struct sched_param schp;
4759 ret = get_errno(sched_getparam(arg1, &schp));
4760 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004761 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
4762 goto efault;
bellard5cd43932003-03-29 16:54:36 +00004763 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004764 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00004765 }
4766 }
4767 break;
bellard31e31b82003-02-18 22:55:36 +00004768 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00004769 {
pbrook53a59602006-03-25 19:31:22 +00004770 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004771 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00004772 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
4773 goto efault;
bellard5cd43932003-03-29 16:54:36 +00004774 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004775 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00004776 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
4777 }
4778 break;
bellard31e31b82003-02-18 22:55:36 +00004779 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00004780 ret = get_errno(sched_getscheduler(arg1));
4781 break;
bellard31e31b82003-02-18 22:55:36 +00004782 case TARGET_NR_sched_yield:
4783 ret = get_errno(sched_yield());
4784 break;
4785 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00004786 ret = get_errno(sched_get_priority_max(arg1));
4787 break;
bellard31e31b82003-02-18 22:55:36 +00004788 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00004789 ret = get_errno(sched_get_priority_min(arg1));
4790 break;
bellard31e31b82003-02-18 22:55:36 +00004791 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00004792 {
bellard5cd43932003-03-29 16:54:36 +00004793 struct timespec ts;
4794 ret = get_errno(sched_rr_get_interval(arg1, &ts));
4795 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004796 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00004797 }
4798 }
4799 break;
bellard31e31b82003-02-18 22:55:36 +00004800 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00004801 {
bellard1b6b0292003-03-22 17:31:38 +00004802 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00004803 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00004804 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00004805 if (is_error(ret) && arg2) {
4806 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00004807 }
4808 }
4809 break;
thse5febef2007-04-01 18:31:35 +00004810#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00004811 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00004812 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004813#endif
4814#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00004815 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00004816 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004817#endif
bellard31e31b82003-02-18 22:55:36 +00004818 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00004819 switch (arg1)
4820 {
4821 case PR_GET_PDEATHSIG:
4822 {
4823 int deathsig;
4824 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
bellard2f619692007-11-16 10:46:05 +00004825 if (!is_error(ret) && arg2
4826 && put_user_ual(deathsig, arg2))
4827 goto efault;
thse5574482007-02-11 20:03:13 +00004828 }
4829 break;
4830 default:
4831 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
4832 break;
4833 }
ths39b9aae2007-02-11 18:36:44 +00004834 break;
bellardd2fd1af2007-11-14 18:08:56 +00004835#ifdef TARGET_NR_arch_prctl
4836 case TARGET_NR_arch_prctl:
4837#if defined(TARGET_I386) && !defined(TARGET_ABI32)
4838 ret = do_arch_prctl(cpu_env, arg1, arg2);
4839 break;
4840#else
4841 goto unimplemented;
4842#endif
4843#endif
bellard67867302003-11-23 17:05:30 +00004844#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00004845 case TARGET_NR_pread:
bellard579a97f2007-11-11 14:26:47 +00004846 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4847 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004848 ret = get_errno(pread(arg1, p, arg3, arg4));
4849 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00004850 break;
bellard31e31b82003-02-18 22:55:36 +00004851 case TARGET_NR_pwrite:
bellard579a97f2007-11-11 14:26:47 +00004852 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4853 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004854 ret = get_errno(pwrite(arg1, p, arg3, arg4));
4855 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00004856 break;
bellard67867302003-11-23 17:05:30 +00004857#endif
bellard31e31b82003-02-18 22:55:36 +00004858 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00004859 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
4860 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004861 ret = get_errno(sys_getcwd1(p, arg2));
4862 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00004863 break;
4864 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00004865 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004866 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00004867 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004868 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00004869#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
4870 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
bellard579a97f2007-11-11 14:26:47 +00004871 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00004872 break;
4873#else
bellard5cd43932003-03-29 16:54:36 +00004874 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00004875#endif
bellard31e31b82003-02-18 22:55:36 +00004876 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00004877 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004878#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00004879 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00004880 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004881#endif
4882#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00004883 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00004884 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004885#endif
bellard048f6b42005-11-26 18:47:20 +00004886#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00004887 case TARGET_NR_vfork:
bellard1b6b0292003-03-22 17:31:38 +00004888 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00004889 break;
bellard048f6b42005-11-26 18:47:20 +00004890#endif
bellardebc05482003-09-30 21:08:41 +00004891#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00004892 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00004893 {
4894 struct rlimit rlim;
4895 ret = get_errno(getrlimit(arg1, &rlim));
4896 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004897 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00004898 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
4899 goto efault;
bellard728584b2003-04-29 20:43:36 +00004900 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
4901 target_rlim->rlim_max = tswapl(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00004902 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00004903 }
4904 break;
4905 }
bellardebc05482003-09-30 21:08:41 +00004906#endif
bellarda315a142005-01-30 22:59:18 +00004907#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00004908 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00004909 if (!(p = lock_user_string(arg1)))
4910 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004911 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
4912 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00004913 break;
bellarda315a142005-01-30 22:59:18 +00004914#endif
4915#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00004916 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00004917 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00004918 break;
bellarda315a142005-01-30 22:59:18 +00004919#endif
4920#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00004921 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00004922 if (!(p = lock_user_string(arg1)))
4923 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004924 ret = get_errno(stat(path(p), &st));
4925 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004926 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004927#endif
4928#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00004929 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00004930 if (!(p = lock_user_string(arg1)))
4931 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004932 ret = get_errno(lstat(path(p), &st));
4933 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004934 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004935#endif
4936#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00004937 case TARGET_NR_fstat64:
bellard60cd49d2003-03-16 22:53:56 +00004938 {
4939 ret = get_errno(fstat(arg1, &st));
4940 do_stat64:
4941 if (!is_error(ret)) {
pbrookce4defa2006-02-09 16:49:55 +00004942#ifdef TARGET_ARM
4943 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004944 struct target_eabi_stat64 *target_st;
bellard579a97f2007-11-11 14:26:47 +00004945
4946 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4947 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00004948 memset(target_st, 0, sizeof(struct target_eabi_stat64));
bellard579a97f2007-11-11 14:26:47 +00004949 __put_user(st.st_dev, &target_st->st_dev);
4950 __put_user(st.st_ino, &target_st->st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004951#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
bellard579a97f2007-11-11 14:26:47 +00004952 __put_user(st.st_ino, &target_st->__st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004953#endif
bellard579a97f2007-11-11 14:26:47 +00004954 __put_user(st.st_mode, &target_st->st_mode);
4955 __put_user(st.st_nlink, &target_st->st_nlink);
4956 __put_user(st.st_uid, &target_st->st_uid);
4957 __put_user(st.st_gid, &target_st->st_gid);
4958 __put_user(st.st_rdev, &target_st->st_rdev);
4959 __put_user(st.st_size, &target_st->st_size);
4960 __put_user(st.st_blksize, &target_st->st_blksize);
4961 __put_user(st.st_blocks, &target_st->st_blocks);
4962 __put_user(st.st_atime, &target_st->target_st_atime);
4963 __put_user(st.st_mtime, &target_st->target_st_mtime);
4964 __put_user(st.st_ctime, &target_st->target_st_ctime);
4965 unlock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00004966 } else
4967#endif
4968 {
pbrook53a59602006-03-25 19:31:22 +00004969 struct target_stat64 *target_st;
bellard579a97f2007-11-11 14:26:47 +00004970
4971 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4972 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00004973 memset(target_st, 0, sizeof(struct target_stat64));
bellard579a97f2007-11-11 14:26:47 +00004974 __put_user(st.st_dev, &target_st->st_dev);
4975 __put_user(st.st_ino, &target_st->st_ino);
pbrookce4defa2006-02-09 16:49:55 +00004976#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
bellard579a97f2007-11-11 14:26:47 +00004977 __put_user(st.st_ino, &target_st->__st_ino);
pbrookce4defa2006-02-09 16:49:55 +00004978#endif
bellard579a97f2007-11-11 14:26:47 +00004979 __put_user(st.st_mode, &target_st->st_mode);
4980 __put_user(st.st_nlink, &target_st->st_nlink);
4981 __put_user(st.st_uid, &target_st->st_uid);
4982 __put_user(st.st_gid, &target_st->st_gid);
4983 __put_user(st.st_rdev, &target_st->st_rdev);
pbrookce4defa2006-02-09 16:49:55 +00004984 /* XXX: better use of kernel struct */
bellard579a97f2007-11-11 14:26:47 +00004985 __put_user(st.st_size, &target_st->st_size);
4986 __put_user(st.st_blksize, &target_st->st_blksize);
4987 __put_user(st.st_blocks, &target_st->st_blocks);
4988 __put_user(st.st_atime, &target_st->target_st_atime);
4989 __put_user(st.st_mtime, &target_st->target_st_mtime);
4990 __put_user(st.st_ctime, &target_st->target_st_ctime);
4991 unlock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00004992 }
bellard60cd49d2003-03-16 22:53:56 +00004993 }
4994 }
4995 break;
bellarda315a142005-01-30 22:59:18 +00004996#endif
bellard67867302003-11-23 17:05:30 +00004997#ifdef USE_UID16
4998 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00004999 if (!(p = lock_user_string(arg1)))
5000 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005001 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
5002 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00005003 break;
5004 case TARGET_NR_getuid:
5005 ret = get_errno(high2lowuid(getuid()));
5006 break;
5007 case TARGET_NR_getgid:
5008 ret = get_errno(high2lowgid(getgid()));
5009 break;
5010 case TARGET_NR_geteuid:
5011 ret = get_errno(high2lowuid(geteuid()));
5012 break;
5013 case TARGET_NR_getegid:
5014 ret = get_errno(high2lowgid(getegid()));
5015 break;
5016 case TARGET_NR_setreuid:
5017 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
5018 break;
5019 case TARGET_NR_setregid:
5020 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
5021 break;
5022 case TARGET_NR_getgroups:
5023 {
5024 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00005025 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00005026 gid_t *grouplist;
5027 int i;
5028
5029 grouplist = alloca(gidsetsize * sizeof(gid_t));
5030 ret = get_errno(getgroups(gidsetsize, grouplist));
5031 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005032 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
5033 if (!target_grouplist)
5034 goto efault;
bellard67867302003-11-23 17:05:30 +00005035 for(i = 0;i < gidsetsize; i++)
5036 target_grouplist[i] = tswap16(grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00005037 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00005038 }
5039 }
5040 break;
5041 case TARGET_NR_setgroups:
5042 {
5043 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00005044 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00005045 gid_t *grouplist;
5046 int i;
5047
5048 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00005049 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
5050 if (!target_grouplist) {
5051 ret = -TARGET_EFAULT;
5052 goto fail;
5053 }
bellard67867302003-11-23 17:05:30 +00005054 for(i = 0;i < gidsetsize; i++)
5055 grouplist[i] = tswap16(target_grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00005056 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00005057 ret = get_errno(setgroups(gidsetsize, grouplist));
5058 }
5059 break;
5060 case TARGET_NR_fchown:
5061 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
5062 break;
thsccfa72b2007-09-24 09:23:34 +00005063#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
5064 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00005065 if (!(p = lock_user_string(arg2)))
5066 goto efault;
5067 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
5068 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00005069 break;
5070#endif
bellard67867302003-11-23 17:05:30 +00005071#ifdef TARGET_NR_setresuid
5072 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00005073 ret = get_errno(setresuid(low2highuid(arg1),
5074 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00005075 low2highuid(arg3)));
5076 break;
5077#endif
5078#ifdef TARGET_NR_getresuid
5079 case TARGET_NR_getresuid:
5080 {
pbrook53a59602006-03-25 19:31:22 +00005081 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00005082 ret = get_errno(getresuid(&ruid, &euid, &suid));
5083 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005084 if (put_user_u16(high2lowuid(ruid), arg1)
5085 || put_user_u16(high2lowuid(euid), arg2)
5086 || put_user_u16(high2lowuid(suid), arg3))
5087 goto efault;
bellard67867302003-11-23 17:05:30 +00005088 }
5089 }
5090 break;
5091#endif
5092#ifdef TARGET_NR_getresgid
5093 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00005094 ret = get_errno(setresgid(low2highgid(arg1),
5095 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00005096 low2highgid(arg3)));
5097 break;
5098#endif
5099#ifdef TARGET_NR_getresgid
5100 case TARGET_NR_getresgid:
5101 {
pbrook53a59602006-03-25 19:31:22 +00005102 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00005103 ret = get_errno(getresgid(&rgid, &egid, &sgid));
5104 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005105 if (put_user_u16(high2lowgid(rgid), arg1)
5106 || put_user_u16(high2lowgid(egid), arg2)
5107 || put_user_u16(high2lowgid(sgid), arg3))
5108 goto efault;
bellard67867302003-11-23 17:05:30 +00005109 }
5110 }
5111 break;
5112#endif
5113 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00005114 if (!(p = lock_user_string(arg1)))
5115 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005116 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
5117 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00005118 break;
5119 case TARGET_NR_setuid:
5120 ret = get_errno(setuid(low2highuid(arg1)));
5121 break;
5122 case TARGET_NR_setgid:
5123 ret = get_errno(setgid(low2highgid(arg1)));
5124 break;
5125 case TARGET_NR_setfsuid:
5126 ret = get_errno(setfsuid(arg1));
5127 break;
5128 case TARGET_NR_setfsgid:
5129 ret = get_errno(setfsgid(arg1));
5130 break;
5131#endif /* USE_UID16 */
5132
bellarda315a142005-01-30 22:59:18 +00005133#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00005134 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00005135 if (!(p = lock_user_string(arg1)))
5136 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005137 ret = get_errno(lchown(p, arg2, arg3));
5138 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00005139 break;
bellarda315a142005-01-30 22:59:18 +00005140#endif
5141#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00005142 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00005143 ret = get_errno(getuid());
5144 break;
bellarda315a142005-01-30 22:59:18 +00005145#endif
5146#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00005147 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00005148 ret = get_errno(getgid());
5149 break;
bellarda315a142005-01-30 22:59:18 +00005150#endif
5151#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00005152 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00005153 ret = get_errno(geteuid());
5154 break;
bellarda315a142005-01-30 22:59:18 +00005155#endif
5156#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00005157 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00005158 ret = get_errno(getegid());
5159 break;
bellarda315a142005-01-30 22:59:18 +00005160#endif
5161#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00005162 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00005163 ret = get_errno(setreuid(arg1, arg2));
5164 break;
bellarda315a142005-01-30 22:59:18 +00005165#endif
5166#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00005167 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00005168 ret = get_errno(setregid(arg1, arg2));
5169 break;
bellarda315a142005-01-30 22:59:18 +00005170#endif
5171#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00005172 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00005173 {
5174 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00005175 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00005176 gid_t *grouplist;
5177 int i;
5178
5179 grouplist = alloca(gidsetsize * sizeof(gid_t));
5180 ret = get_errno(getgroups(gidsetsize, grouplist));
5181 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005182 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
5183 if (!target_grouplist) {
5184 ret = -TARGET_EFAULT;
5185 goto fail;
5186 }
bellard99c475a2005-01-31 20:45:13 +00005187 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00005188 target_grouplist[i] = tswap32(grouplist[i]);
5189 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00005190 }
5191 }
5192 break;
bellarda315a142005-01-30 22:59:18 +00005193#endif
5194#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00005195 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00005196 {
5197 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00005198 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00005199 gid_t *grouplist;
5200 int i;
ths3b46e622007-09-17 08:09:54 +00005201
bellard99c475a2005-01-31 20:45:13 +00005202 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00005203 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
5204 if (!target_grouplist) {
5205 ret = -TARGET_EFAULT;
5206 goto fail;
5207 }
bellard99c475a2005-01-31 20:45:13 +00005208 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00005209 grouplist[i] = tswap32(target_grouplist[i]);
5210 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00005211 ret = get_errno(setgroups(gidsetsize, grouplist));
5212 }
5213 break;
bellarda315a142005-01-30 22:59:18 +00005214#endif
5215#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00005216 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00005217 ret = get_errno(fchown(arg1, arg2, arg3));
5218 break;
bellarda315a142005-01-30 22:59:18 +00005219#endif
5220#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00005221 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00005222 ret = get_errno(setresuid(arg1, arg2, arg3));
5223 break;
bellarda315a142005-01-30 22:59:18 +00005224#endif
5225#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00005226 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00005227 {
pbrook53a59602006-03-25 19:31:22 +00005228 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00005229 ret = get_errno(getresuid(&ruid, &euid, &suid));
5230 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005231 if (put_user_u32(ruid, arg1)
5232 || put_user_u32(euid, arg2)
5233 || put_user_u32(suid, arg3))
5234 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00005235 }
5236 }
5237 break;
bellarda315a142005-01-30 22:59:18 +00005238#endif
5239#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00005240 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00005241 ret = get_errno(setresgid(arg1, arg2, arg3));
5242 break;
bellarda315a142005-01-30 22:59:18 +00005243#endif
5244#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00005245 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00005246 {
pbrook53a59602006-03-25 19:31:22 +00005247 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00005248 ret = get_errno(getresgid(&rgid, &egid, &sgid));
5249 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005250 if (put_user_u32(rgid, arg1)
5251 || put_user_u32(egid, arg2)
5252 || put_user_u32(sgid, arg3))
5253 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00005254 }
5255 }
5256 break;
bellarda315a142005-01-30 22:59:18 +00005257#endif
5258#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00005259 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00005260 if (!(p = lock_user_string(arg1)))
5261 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005262 ret = get_errno(chown(p, arg2, arg3));
5263 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00005264 break;
bellarda315a142005-01-30 22:59:18 +00005265#endif
5266#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00005267 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00005268 ret = get_errno(setuid(arg1));
5269 break;
bellarda315a142005-01-30 22:59:18 +00005270#endif
5271#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00005272 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00005273 ret = get_errno(setgid(arg1));
5274 break;
bellarda315a142005-01-30 22:59:18 +00005275#endif
5276#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00005277 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00005278 ret = get_errno(setfsuid(arg1));
5279 break;
bellarda315a142005-01-30 22:59:18 +00005280#endif
5281#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00005282 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00005283 ret = get_errno(setfsgid(arg1));
5284 break;
bellarda315a142005-01-30 22:59:18 +00005285#endif
bellard67867302003-11-23 17:05:30 +00005286
bellard31e31b82003-02-18 22:55:36 +00005287 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00005288 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00005289#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00005290 case TARGET_NR_mincore:
bellardb03c60f2003-03-23 17:19:56 +00005291 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00005292#endif
5293#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00005294 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00005295 /* A straight passthrough may not be safe because qemu sometimes
5296 turns private flie-backed mappings into anonymous mappings.
5297 This will break MADV_DONTNEED.
5298 This is a hint, so ignoring and returning success is ok. */
5299 ret = get_errno(0);
5300 break;
bellardffa65c32004-01-04 23:57:22 +00005301#endif
blueswir1992f48a2007-10-14 16:27:31 +00005302#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00005303 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00005304 {
thsb1e341e2007-03-20 21:50:52 +00005305 int cmd;
bellard77e46722003-04-29 20:39:06 +00005306 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00005307 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00005308#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00005309 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00005310#endif
bellard77e46722003-04-29 20:39:06 +00005311
thsb1e341e2007-03-20 21:50:52 +00005312 switch(arg2){
5313 case TARGET_F_GETLK64:
5314 cmd = F_GETLK64;
thsa7222582007-03-22 15:08:55 +00005315 break;
thsb1e341e2007-03-20 21:50:52 +00005316 case TARGET_F_SETLK64:
5317 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00005318 break;
thsb1e341e2007-03-20 21:50:52 +00005319 case TARGET_F_SETLKW64:
5320 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00005321 break;
thsb1e341e2007-03-20 21:50:52 +00005322 default:
5323 cmd = arg2;
thsa7222582007-03-22 15:08:55 +00005324 break;
thsb1e341e2007-03-20 21:50:52 +00005325 }
5326
bellard60cd49d2003-03-16 22:53:56 +00005327 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00005328 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00005329#ifdef TARGET_ARM
5330 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00005331 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5332 goto efault;
ths58134272007-03-31 18:59:32 +00005333 fl.l_type = tswap16(target_efl->l_type);
5334 fl.l_whence = tswap16(target_efl->l_whence);
5335 fl.l_start = tswap64(target_efl->l_start);
5336 fl.l_len = tswap64(target_efl->l_len);
5337 fl.l_pid = tswapl(target_efl->l_pid);
5338 unlock_user_struct(target_efl, arg3, 0);
5339 } else
5340#endif
5341 {
bellard9ee1fa22007-11-11 15:11:19 +00005342 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5343 goto efault;
ths58134272007-03-31 18:59:32 +00005344 fl.l_type = tswap16(target_fl->l_type);
5345 fl.l_whence = tswap16(target_fl->l_whence);
5346 fl.l_start = tswap64(target_fl->l_start);
5347 fl.l_len = tswap64(target_fl->l_len);
5348 fl.l_pid = tswapl(target_fl->l_pid);
5349 unlock_user_struct(target_fl, arg3, 0);
5350 }
thsb1e341e2007-03-20 21:50:52 +00005351 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00005352 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00005353#ifdef TARGET_ARM
5354 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00005355 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
5356 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005357 target_efl->l_type = tswap16(fl.l_type);
5358 target_efl->l_whence = tswap16(fl.l_whence);
5359 target_efl->l_start = tswap64(fl.l_start);
5360 target_efl->l_len = tswap64(fl.l_len);
5361 target_efl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00005362 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00005363 } else
5364#endif
5365 {
bellard9ee1fa22007-11-11 15:11:19 +00005366 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
5367 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005368 target_fl->l_type = tswap16(fl.l_type);
5369 target_fl->l_whence = tswap16(fl.l_whence);
5370 target_fl->l_start = tswap64(fl.l_start);
5371 target_fl->l_len = tswap64(fl.l_len);
5372 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00005373 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00005374 }
bellard77e46722003-04-29 20:39:06 +00005375 }
5376 break;
5377
thsb1e341e2007-03-20 21:50:52 +00005378 case TARGET_F_SETLK64:
5379 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00005380#ifdef TARGET_ARM
5381 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00005382 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5383 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005384 fl.l_type = tswap16(target_efl->l_type);
5385 fl.l_whence = tswap16(target_efl->l_whence);
5386 fl.l_start = tswap64(target_efl->l_start);
5387 fl.l_len = tswap64(target_efl->l_len);
5388 fl.l_pid = tswapl(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00005389 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00005390 } else
5391#endif
5392 {
bellard9ee1fa22007-11-11 15:11:19 +00005393 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5394 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005395 fl.l_type = tswap16(target_fl->l_type);
5396 fl.l_whence = tswap16(target_fl->l_whence);
5397 fl.l_start = tswap64(target_fl->l_start);
5398 fl.l_len = tswap64(target_fl->l_len);
5399 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00005400 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00005401 }
thsb1e341e2007-03-20 21:50:52 +00005402 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00005403 break;
bellard60cd49d2003-03-16 22:53:56 +00005404 default:
bellard9ee1fa22007-11-11 15:11:19 +00005405 ret = do_fcntl(arg1, cmd, arg3);
bellard60cd49d2003-03-16 22:53:56 +00005406 break;
5407 }
bellard77e46722003-04-29 20:39:06 +00005408 break;
5409 }
bellard60cd49d2003-03-16 22:53:56 +00005410#endif
ths7d600c82006-12-08 01:32:58 +00005411#ifdef TARGET_NR_cacheflush
5412 case TARGET_NR_cacheflush:
5413 /* self-modifying code is handled automatically, so nothing needed */
5414 ret = 0;
5415 break;
5416#endif
bellardebc05482003-09-30 21:08:41 +00005417#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00005418 case TARGET_NR_security:
5419 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005420#endif
bellardc573ff62004-01-04 15:51:36 +00005421#ifdef TARGET_NR_getpagesize
5422 case TARGET_NR_getpagesize:
5423 ret = TARGET_PAGE_SIZE;
5424 break;
5425#endif
bellard31e31b82003-02-18 22:55:36 +00005426 case TARGET_NR_gettid:
5427 ret = get_errno(gettid());
5428 break;
thse5febef2007-04-01 18:31:35 +00005429#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00005430 case TARGET_NR_readahead:
bellard5cd43932003-03-29 16:54:36 +00005431 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005432#endif
bellardebc05482003-09-30 21:08:41 +00005433#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00005434 case TARGET_NR_setxattr:
5435 case TARGET_NR_lsetxattr:
5436 case TARGET_NR_fsetxattr:
5437 case TARGET_NR_getxattr:
5438 case TARGET_NR_lgetxattr:
5439 case TARGET_NR_fgetxattr:
5440 case TARGET_NR_listxattr:
5441 case TARGET_NR_llistxattr:
5442 case TARGET_NR_flistxattr:
5443 case TARGET_NR_removexattr:
5444 case TARGET_NR_lremovexattr:
5445 case TARGET_NR_fremovexattr:
bellard5cd43932003-03-29 16:54:36 +00005446 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00005447#endif
5448#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00005449 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00005450#if defined(TARGET_MIPS)
ths6f5b89a2007-03-02 20:48:00 +00005451 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
5452 ret = 0;
5453 break;
bellard8d18e892007-11-14 15:18:40 +00005454#elif defined(TARGET_I386) && defined(TARGET_ABI32)
5455 ret = do_set_thread_area(cpu_env, arg1);
5456 break;
ths6f5b89a2007-03-02 20:48:00 +00005457#else
5458 goto unimplemented_nowarn;
5459#endif
5460#endif
5461#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00005462 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00005463#if defined(TARGET_I386) && defined(TARGET_ABI32)
5464 ret = do_get_thread_area(cpu_env, arg1);
5465#else
bellard5cd43932003-03-29 16:54:36 +00005466 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00005467#endif
bellard8d18e892007-11-14 15:18:40 +00005468#endif
bellard48dc41e2006-06-21 18:15:50 +00005469#ifdef TARGET_NR_getdomainname
5470 case TARGET_NR_getdomainname:
5471 goto unimplemented_nowarn;
5472#endif
ths6f5b89a2007-03-02 20:48:00 +00005473
thsb5906f92007-03-19 13:32:45 +00005474#ifdef TARGET_NR_clock_gettime
5475 case TARGET_NR_clock_gettime:
5476 {
5477 struct timespec ts;
5478 ret = get_errno(clock_gettime(arg1, &ts));
5479 if (!is_error(ret)) {
5480 host_to_target_timespec(arg2, &ts);
5481 }
5482 break;
5483 }
5484#endif
5485#ifdef TARGET_NR_clock_getres
5486 case TARGET_NR_clock_getres:
5487 {
5488 struct timespec ts;
5489 ret = get_errno(clock_getres(arg1, &ts));
5490 if (!is_error(ret)) {
5491 host_to_target_timespec(arg2, &ts);
5492 }
5493 break;
5494 }
5495#endif
5496
ths6f5b89a2007-03-02 20:48:00 +00005497#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
5498 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00005499 ret = get_errno(set_tid_address((int *)g2h(arg1)));
5500 break;
ths6f5b89a2007-03-02 20:48:00 +00005501#endif
5502
ths3ae43202007-09-16 21:39:48 +00005503#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00005504 case TARGET_NR_tkill:
5505 ret = get_errno(sys_tkill((int)arg1, (int)arg2));
5506 break;
5507#endif
5508
ths3ae43202007-09-16 21:39:48 +00005509#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00005510 case TARGET_NR_tgkill:
5511 ret = get_errno(sys_tgkill((int)arg1, (int)arg2, (int)arg3));
5512 break;
5513#endif
5514
ths4f2b1fe2007-06-21 21:57:12 +00005515#ifdef TARGET_NR_set_robust_list
5516 case TARGET_NR_set_robust_list:
5517 goto unimplemented_nowarn;
5518#endif
5519
ths9007f0e2007-09-25 17:50:37 +00005520#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
5521 case TARGET_NR_utimensat:
5522 {
5523 struct timespec ts[2];
5524 target_to_host_timespec(ts, arg3);
5525 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
5526 if (!arg2)
5527 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
5528 else {
bellard579a97f2007-11-11 14:26:47 +00005529 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00005530 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00005531 goto fail;
5532 }
5533 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
5534 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00005535 }
5536 }
5537 break;
5538#endif
5539
bellard31e31b82003-02-18 22:55:36 +00005540 default:
5541 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00005542 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00005543#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 +00005544 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00005545#endif
ths0da46a62007-10-20 20:23:07 +00005546 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00005547 break;
5548 }
bellard579a97f2007-11-11 14:26:47 +00005549fail:
bellardc573ff62004-01-04 15:51:36 +00005550#ifdef DEBUG
5551 gemu_log(" = %ld\n", ret);
5552#endif
thsb92c47c2007-11-01 00:07:38 +00005553 if(do_strace)
5554 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00005555 return ret;
bellard579a97f2007-11-11 14:26:47 +00005556efault:
5557 ret = -TARGET_EFAULT;
5558 goto fail;
bellard31e31b82003-02-18 22:55:36 +00005559}