blob: b1dc365e6e96f4c5ffbeaa3a40ecee1d01f0e4d6 [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
ths788f5ec2007-12-09 02:37:05 +0000553static inline abi_long copy_from_user_timeval(struct timeval *tv,
554 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000555{
pbrook53a59602006-03-25 19:31:22 +0000556 struct target_timeval *target_tv;
557
ths788f5ec2007-12-09 02:37:05 +0000558 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
bellard579a97f2007-11-11 14:26:47 +0000559 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000560
561 __get_user(tv->tv_sec, &target_tv->tv_sec);
562 __get_user(tv->tv_usec, &target_tv->tv_usec);
563
564 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000565
566 return 0;
bellard31e31b82003-02-18 22:55:36 +0000567}
568
ths788f5ec2007-12-09 02:37:05 +0000569static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
570 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +0000571{
pbrook53a59602006-03-25 19:31:22 +0000572 struct target_timeval *target_tv;
573
ths788f5ec2007-12-09 02:37:05 +0000574 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
bellard579a97f2007-11-11 14:26:47 +0000575 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000576
577 __put_user(tv->tv_sec, &target_tv->tv_sec);
578 __put_user(tv->tv_usec, &target_tv->tv_usec);
579
580 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000581
582 return 0;
bellard31e31b82003-02-18 22:55:36 +0000583}
584
585
ths0da46a62007-10-20 20:23:07 +0000586/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000587static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +0000588 abi_ulong rfd_addr, abi_ulong wfd_addr,
589 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000590{
591 fd_set rfds, wfds, efds;
592 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
593 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +0000594 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +0000595
ths26edcf42007-12-09 02:25:24 +0000596 if (rfd_addr) {
597 if (copy_from_user_fdset(&rfds, rfd_addr, n))
598 return -TARGET_EFAULT;
599 rfds_ptr = &rfds;
pbrook53a59602006-03-25 19:31:22 +0000600 } else {
pbrook53a59602006-03-25 19:31:22 +0000601 rfds_ptr = NULL;
602 }
ths26edcf42007-12-09 02:25:24 +0000603 if (wfd_addr) {
604 if (copy_from_user_fdset(&wfds, wfd_addr, n))
605 return -TARGET_EFAULT;
606 wfds_ptr = &wfds;
pbrook53a59602006-03-25 19:31:22 +0000607 } else {
pbrook53a59602006-03-25 19:31:22 +0000608 wfds_ptr = NULL;
609 }
ths26edcf42007-12-09 02:25:24 +0000610 if (efd_addr) {
611 if (copy_from_user_fdset(&efds, efd_addr, n))
612 return -TARGET_EFAULT;
613 efds_ptr = &efds;
pbrook53a59602006-03-25 19:31:22 +0000614 } else {
pbrook53a59602006-03-25 19:31:22 +0000615 efds_ptr = NULL;
616 }
ths3b46e622007-09-17 08:09:54 +0000617
ths26edcf42007-12-09 02:25:24 +0000618 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +0000619 if (copy_from_user_timeval(&tv, target_tv_addr))
620 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000621 tv_ptr = &tv;
622 } else {
623 tv_ptr = NULL;
624 }
ths26edcf42007-12-09 02:25:24 +0000625
bellard31e31b82003-02-18 22:55:36 +0000626 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +0000627
ths26edcf42007-12-09 02:25:24 +0000628 if (!is_error(ret)) {
629 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
630 return -TARGET_EFAULT;
631 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
632 return -TARGET_EFAULT;
633 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
634 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000635
ths788f5ec2007-12-09 02:37:05 +0000636 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
637 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000638 }
bellard579a97f2007-11-11 14:26:47 +0000639
bellard31e31b82003-02-18 22:55:36 +0000640 return ret;
641}
642
bellard579a97f2007-11-11 14:26:47 +0000643static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
644 abi_ulong target_addr,
645 socklen_t len)
bellard7854b052003-03-29 17:22:23 +0000646{
pbrook53a59602006-03-25 19:31:22 +0000647 struct target_sockaddr *target_saddr;
648
bellard579a97f2007-11-11 14:26:47 +0000649 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
650 if (!target_saddr)
651 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +0000652 memcpy(addr, target_saddr, len);
653 addr->sa_family = tswap16(target_saddr->sa_family);
654 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000655
656 return 0;
bellard7854b052003-03-29 17:22:23 +0000657}
658
bellard579a97f2007-11-11 14:26:47 +0000659static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
660 struct sockaddr *addr,
661 socklen_t len)
bellard7854b052003-03-29 17:22:23 +0000662{
pbrook53a59602006-03-25 19:31:22 +0000663 struct target_sockaddr *target_saddr;
664
bellard579a97f2007-11-11 14:26:47 +0000665 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
666 if (!target_saddr)
667 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +0000668 memcpy(target_saddr, addr, len);
669 target_saddr->sa_family = tswap16(addr->sa_family);
670 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +0000671
672 return 0;
bellard7854b052003-03-29 17:22:23 +0000673}
674
pbrook53a59602006-03-25 19:31:22 +0000675/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +0000676static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
677 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +0000678{
679 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +0000680 abi_long msg_controllen;
681 abi_ulong target_cmsg_addr;
682 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +0000683 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +0000684
685 msg_controllen = tswapl(target_msgh->msg_controllen);
686 if (msg_controllen < sizeof (struct target_cmsghdr))
687 goto the_end;
688 target_cmsg_addr = tswapl(target_msgh->msg_control);
689 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
690 if (!target_cmsg)
691 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +0000692
693 while (cmsg && target_cmsg) {
694 void *data = CMSG_DATA(cmsg);
695 void *target_data = TARGET_CMSG_DATA(target_cmsg);
696
ths5fafdf22007-09-16 21:08:06 +0000697 int len = tswapl(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +0000698 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
699
700 space += CMSG_SPACE(len);
701 if (space > msgh->msg_controllen) {
702 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +0000703 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +0000704 break;
705 }
706
707 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
708 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
709 cmsg->cmsg_len = CMSG_LEN(len);
710
bellard3532fa72006-06-24 15:06:03 +0000711 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +0000712 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
713 memcpy(data, target_data, len);
714 } else {
715 int *fd = (int *)data;
716 int *target_fd = (int *)target_data;
717 int i, numfds = len / sizeof(int);
718
719 for (i = 0; i < numfds; i++)
720 fd[i] = tswap32(target_fd[i]);
721 }
722
723 cmsg = CMSG_NXTHDR(msgh, cmsg);
724 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
725 }
bellard5a4a8982007-11-11 17:39:18 +0000726 unlock_user(target_cmsg, target_cmsg_addr, 0);
727 the_end:
bellard7854b052003-03-29 17:22:23 +0000728 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +0000729 return 0;
bellard7854b052003-03-29 17:22:23 +0000730}
731
pbrook53a59602006-03-25 19:31:22 +0000732/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +0000733static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
734 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +0000735{
736 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +0000737 abi_long msg_controllen;
738 abi_ulong target_cmsg_addr;
739 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +0000740 socklen_t space = 0;
741
bellard5a4a8982007-11-11 17:39:18 +0000742 msg_controllen = tswapl(target_msgh->msg_controllen);
743 if (msg_controllen < sizeof (struct target_cmsghdr))
744 goto the_end;
745 target_cmsg_addr = tswapl(target_msgh->msg_control);
746 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
747 if (!target_cmsg)
748 return -TARGET_EFAULT;
749
bellard7854b052003-03-29 17:22:23 +0000750 while (cmsg && target_cmsg) {
751 void *data = CMSG_DATA(cmsg);
752 void *target_data = TARGET_CMSG_DATA(target_cmsg);
753
754 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
755
756 space += TARGET_CMSG_SPACE(len);
bellard5a4a8982007-11-11 17:39:18 +0000757 if (space > msg_controllen) {
bellard7854b052003-03-29 17:22:23 +0000758 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +0000759 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +0000760 break;
761 }
762
763 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
764 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
765 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
766
bellard3532fa72006-06-24 15:06:03 +0000767 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +0000768 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
769 memcpy(target_data, data, len);
770 } else {
771 int *fd = (int *)data;
772 int *target_fd = (int *)target_data;
773 int i, numfds = len / sizeof(int);
774
775 for (i = 0; i < numfds; i++)
776 target_fd[i] = tswap32(fd[i]);
777 }
778
779 cmsg = CMSG_NXTHDR(msgh, cmsg);
780 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
781 }
bellard5a4a8982007-11-11 17:39:18 +0000782 unlock_user(target_cmsg, target_cmsg_addr, space);
783 the_end:
784 target_msgh->msg_controllen = tswapl(space);
785 return 0;
bellard7854b052003-03-29 17:22:23 +0000786}
787
ths0da46a62007-10-20 20:23:07 +0000788/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000789static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +0000790 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +0000791{
blueswir1992f48a2007-10-14 16:27:31 +0000792 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +0000793 int val;
ths3b46e622007-09-17 08:09:54 +0000794
bellard8853f862004-02-22 14:57:26 +0000795 switch(level) {
796 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +0000797 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +0000798 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +0000799 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +0000800
bellard2f619692007-11-16 10:46:05 +0000801 if (get_user_u32(val, optval_addr))
802 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +0000803 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
804 break;
805 case SOL_IP:
806 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +0000807 case IP_TOS:
808 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +0000809 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +0000810 case IP_ROUTER_ALERT:
811 case IP_RECVOPTS:
812 case IP_RETOPTS:
813 case IP_PKTINFO:
814 case IP_MTU_DISCOVER:
815 case IP_RECVERR:
816 case IP_RECVTOS:
817#ifdef IP_FREEBIND
818 case IP_FREEBIND:
819#endif
820 case IP_MULTICAST_TTL:
821 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +0000822 val = 0;
823 if (optlen >= sizeof(uint32_t)) {
bellard2f619692007-11-16 10:46:05 +0000824 if (get_user_u32(val, optval_addr))
825 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +0000826 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +0000827 if (get_user_u8(val, optval_addr))
828 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +0000829 }
830 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
831 break;
832 default:
833 goto unimplemented;
834 }
835 break;
bellard3532fa72006-06-24 15:06:03 +0000836 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +0000837 switch (optname) {
838 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +0000839 case TARGET_SO_DEBUG:
840 optname = SO_DEBUG;
841 break;
842 case TARGET_SO_REUSEADDR:
843 optname = SO_REUSEADDR;
844 break;
845 case TARGET_SO_TYPE:
846 optname = SO_TYPE;
847 break;
848 case TARGET_SO_ERROR:
849 optname = SO_ERROR;
850 break;
851 case TARGET_SO_DONTROUTE:
852 optname = SO_DONTROUTE;
853 break;
854 case TARGET_SO_BROADCAST:
855 optname = SO_BROADCAST;
856 break;
857 case TARGET_SO_SNDBUF:
858 optname = SO_SNDBUF;
859 break;
860 case TARGET_SO_RCVBUF:
861 optname = SO_RCVBUF;
862 break;
863 case TARGET_SO_KEEPALIVE:
864 optname = SO_KEEPALIVE;
865 break;
866 case TARGET_SO_OOBINLINE:
867 optname = SO_OOBINLINE;
868 break;
869 case TARGET_SO_NO_CHECK:
870 optname = SO_NO_CHECK;
871 break;
872 case TARGET_SO_PRIORITY:
873 optname = SO_PRIORITY;
874 break;
bellard5e83e8e2005-03-01 22:32:06 +0000875#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +0000876 case TARGET_SO_BSDCOMPAT:
877 optname = SO_BSDCOMPAT;
878 break;
bellard5e83e8e2005-03-01 22:32:06 +0000879#endif
bellard3532fa72006-06-24 15:06:03 +0000880 case TARGET_SO_PASSCRED:
881 optname = SO_PASSCRED;
882 break;
883 case TARGET_SO_TIMESTAMP:
884 optname = SO_TIMESTAMP;
885 break;
886 case TARGET_SO_RCVLOWAT:
887 optname = SO_RCVLOWAT;
888 break;
889 case TARGET_SO_RCVTIMEO:
890 optname = SO_RCVTIMEO;
891 break;
892 case TARGET_SO_SNDTIMEO:
893 optname = SO_SNDTIMEO;
894 break;
bellard8853f862004-02-22 14:57:26 +0000895 break;
896 default:
897 goto unimplemented;
898 }
bellard3532fa72006-06-24 15:06:03 +0000899 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +0000900 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +0000901
bellard2f619692007-11-16 10:46:05 +0000902 if (get_user_u32(val, optval_addr))
903 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +0000904 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +0000905 break;
bellard7854b052003-03-29 17:22:23 +0000906 default:
bellard8853f862004-02-22 14:57:26 +0000907 unimplemented:
908 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
ths6fa13c12007-12-18 02:41:04 +0000909 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +0000910 }
bellard8853f862004-02-22 14:57:26 +0000911 return ret;
bellard7854b052003-03-29 17:22:23 +0000912}
913
ths0da46a62007-10-20 20:23:07 +0000914/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000915static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +0000916 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +0000917{
blueswir1992f48a2007-10-14 16:27:31 +0000918 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +0000919 int len, lv, val;
bellard8853f862004-02-22 14:57:26 +0000920
921 switch(level) {
bellard3532fa72006-06-24 15:06:03 +0000922 case TARGET_SOL_SOCKET:
923 level = SOL_SOCKET;
bellard8853f862004-02-22 14:57:26 +0000924 switch (optname) {
bellard3532fa72006-06-24 15:06:03 +0000925 case TARGET_SO_LINGER:
926 case TARGET_SO_RCVTIMEO:
927 case TARGET_SO_SNDTIMEO:
928 case TARGET_SO_PEERCRED:
929 case TARGET_SO_PEERNAME:
bellard8853f862004-02-22 14:57:26 +0000930 /* These don't just return a single integer */
931 goto unimplemented;
932 default:
bellard2efbe912005-07-23 15:10:20 +0000933 goto int_case;
934 }
935 break;
936 case SOL_TCP:
937 /* TCP options all take an 'int' value. */
938 int_case:
bellard2f619692007-11-16 10:46:05 +0000939 if (get_user_u32(len, optlen))
940 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +0000941 if (len < 0)
ths0da46a62007-10-20 20:23:07 +0000942 return -TARGET_EINVAL;
bellard2efbe912005-07-23 15:10:20 +0000943 lv = sizeof(int);
944 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
945 if (ret < 0)
946 return ret;
947 val = tswap32(val);
948 if (len > lv)
949 len = lv;
bellard2f619692007-11-16 10:46:05 +0000950 if (len == 4) {
951 if (put_user_u32(val, optval_addr))
952 return -TARGET_EFAULT;
953 } else {
954 if (put_user_u8(val, optval_addr))
955 return -TARGET_EFAULT;
956 }
957 if (put_user_u32(len, optlen))
958 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +0000959 break;
960 case SOL_IP:
961 switch(optname) {
962 case IP_TOS:
963 case IP_TTL:
964 case IP_HDRINCL:
965 case IP_ROUTER_ALERT:
966 case IP_RECVOPTS:
967 case IP_RETOPTS:
968 case IP_PKTINFO:
969 case IP_MTU_DISCOVER:
970 case IP_RECVERR:
971 case IP_RECVTOS:
972#ifdef IP_FREEBIND
973 case IP_FREEBIND:
974#endif
975 case IP_MULTICAST_TTL:
976 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +0000977 if (get_user_u32(len, optlen))
978 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +0000979 if (len < 0)
ths0da46a62007-10-20 20:23:07 +0000980 return -TARGET_EINVAL;
bellard8853f862004-02-22 14:57:26 +0000981 lv = sizeof(int);
982 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
983 if (ret < 0)
984 return ret;
bellard2efbe912005-07-23 15:10:20 +0000985 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +0000986 len = 1;
bellard2f619692007-11-16 10:46:05 +0000987 if (put_user_u32(len, optlen)
988 || put_user_u8(val, optval_addr))
989 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +0000990 } else {
bellard2efbe912005-07-23 15:10:20 +0000991 if (len > sizeof(int))
992 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +0000993 if (put_user_u32(len, optlen)
994 || put_user_u32(val, optval_addr))
995 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +0000996 }
bellard8853f862004-02-22 14:57:26 +0000997 break;
bellard2efbe912005-07-23 15:10:20 +0000998 default:
thsc02f4992007-12-18 02:39:59 +0000999 ret = -TARGET_ENOPROTOOPT;
1000 break;
bellard8853f862004-02-22 14:57:26 +00001001 }
1002 break;
1003 default:
1004 unimplemented:
1005 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1006 level, optname);
thsc02f4992007-12-18 02:39:59 +00001007 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00001008 break;
1009 }
1010 return ret;
bellard7854b052003-03-29 17:22:23 +00001011}
1012
bellard579a97f2007-11-11 14:26:47 +00001013/* FIXME
1014 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1015 * other lock functions have a return code of 0 for failure.
1016 */
1017static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1018 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001019{
1020 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001021 abi_ulong base;
bellard579a97f2007-11-11 14:26:47 +00001022 int i, j;
pbrook53a59602006-03-25 19:31:22 +00001023
bellard579a97f2007-11-11 14:26:47 +00001024 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1025 if (!target_vec)
1026 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001027 for(i = 0;i < count; i++) {
1028 base = tswapl(target_vec[i].iov_base);
1029 vec[i].iov_len = tswapl(target_vec[i].iov_len);
bellard41df8412008-02-04 22:26:57 +00001030 if (vec[i].iov_len != 0) {
1031 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
balrog7bedce82008-02-10 13:28:08 +00001032 if (!vec[i].iov_base && vec[i].iov_len)
bellard41df8412008-02-04 22:26:57 +00001033 goto fail;
1034 } else {
1035 /* zero length pointer is ignored */
1036 vec[i].iov_base = NULL;
1037 }
pbrook53a59602006-03-25 19:31:22 +00001038 }
1039 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001040 return 0;
1041 fail:
1042 /* failure - unwind locks */
1043 for (j = 0; j < i; j++) {
1044 base = tswapl(target_vec[j].iov_base);
1045 unlock_user(vec[j].iov_base, base, 0);
1046 }
1047 unlock_user (target_vec, target_addr, 0);
1048 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001049}
1050
bellard579a97f2007-11-11 14:26:47 +00001051static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1052 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001053{
1054 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001055 abi_ulong base;
pbrook53a59602006-03-25 19:31:22 +00001056 int i;
1057
bellard579a97f2007-11-11 14:26:47 +00001058 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1059 if (!target_vec)
1060 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001061 for(i = 0;i < count; i++) {
1062 base = tswapl(target_vec[i].iov_base);
1063 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1064 }
1065 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001066
1067 return 0;
pbrook53a59602006-03-25 19:31:22 +00001068}
1069
ths0da46a62007-10-20 20:23:07 +00001070/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001071static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00001072{
1073#if defined(TARGET_MIPS)
1074 switch(type) {
1075 case TARGET_SOCK_DGRAM:
1076 type = SOCK_DGRAM;
1077 break;
1078 case TARGET_SOCK_STREAM:
1079 type = SOCK_STREAM;
1080 break;
1081 case TARGET_SOCK_RAW:
1082 type = SOCK_RAW;
1083 break;
1084 case TARGET_SOCK_RDM:
1085 type = SOCK_RDM;
1086 break;
1087 case TARGET_SOCK_SEQPACKET:
1088 type = SOCK_SEQPACKET;
1089 break;
1090 case TARGET_SOCK_PACKET:
1091 type = SOCK_PACKET;
1092 break;
1093 }
1094#endif
balrog12bc92a2007-10-30 21:06:14 +00001095 if (domain == PF_NETLINK)
1096 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
bellard3532fa72006-06-24 15:06:03 +00001097 return get_errno(socket(domain, type, protocol));
1098}
1099
ths0da46a62007-10-20 20:23:07 +00001100/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001101static abi_long do_bind(int sockfd, abi_ulong target_addr,
1102 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001103{
1104 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001105
bellard3532fa72006-06-24 15:06:03 +00001106 target_to_host_sockaddr(addr, target_addr, addrlen);
1107 return get_errno(bind(sockfd, addr, addrlen));
1108}
1109
ths0da46a62007-10-20 20:23:07 +00001110/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001111static abi_long do_connect(int sockfd, abi_ulong target_addr,
1112 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001113{
1114 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001115
bellard3532fa72006-06-24 15:06:03 +00001116 target_to_host_sockaddr(addr, target_addr, addrlen);
1117 return get_errno(connect(sockfd, addr, addrlen));
1118}
1119
ths0da46a62007-10-20 20:23:07 +00001120/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001121static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1122 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001123{
blueswir1992f48a2007-10-14 16:27:31 +00001124 abi_long ret;
bellard3532fa72006-06-24 15:06:03 +00001125 struct target_msghdr *msgp;
1126 struct msghdr msg;
1127 int count;
1128 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001129 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001130
bellard579a97f2007-11-11 14:26:47 +00001131 /* FIXME */
1132 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1133 msgp,
1134 target_msg,
1135 send ? 1 : 0))
1136 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001137 if (msgp->msg_name) {
1138 msg.msg_namelen = tswap32(msgp->msg_namelen);
1139 msg.msg_name = alloca(msg.msg_namelen);
1140 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1141 msg.msg_namelen);
1142 } else {
1143 msg.msg_name = NULL;
1144 msg.msg_namelen = 0;
1145 }
1146 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1147 msg.msg_control = alloca(msg.msg_controllen);
1148 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001149
bellard3532fa72006-06-24 15:06:03 +00001150 count = tswapl(msgp->msg_iovlen);
1151 vec = alloca(count * sizeof(struct iovec));
1152 target_vec = tswapl(msgp->msg_iov);
bellard579a97f2007-11-11 14:26:47 +00001153 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
bellard3532fa72006-06-24 15:06:03 +00001154 msg.msg_iovlen = count;
1155 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001156
bellard3532fa72006-06-24 15:06:03 +00001157 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00001158 ret = target_to_host_cmsg(&msg, msgp);
1159 if (ret == 0)
1160 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00001161 } else {
1162 ret = get_errno(recvmsg(fd, &msg, flags));
1163 if (!is_error(ret))
bellard5a4a8982007-11-11 17:39:18 +00001164 ret = host_to_target_cmsg(msgp, &msg);
bellard3532fa72006-06-24 15:06:03 +00001165 }
1166 unlock_iovec(vec, target_vec, count, !send);
bellard579a97f2007-11-11 14:26:47 +00001167 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00001168 return ret;
1169}
1170
ths0da46a62007-10-20 20:23:07 +00001171/* do_accept() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001172static abi_long do_accept(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001173 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001174{
bellard2f619692007-11-16 10:46:05 +00001175 socklen_t addrlen;
1176 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001177 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001178
bellard2f619692007-11-16 10:46:05 +00001179 if (get_user_u32(addrlen, target_addrlen_addr))
1180 return -TARGET_EFAULT;
1181
1182 addr = alloca(addrlen);
1183
pbrook1be9e1d2006-11-19 15:26:04 +00001184 ret = get_errno(accept(fd, addr, &addrlen));
1185 if (!is_error(ret)) {
1186 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001187 if (put_user_u32(addrlen, target_addrlen_addr))
1188 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001189 }
1190 return ret;
1191}
1192
ths0da46a62007-10-20 20:23:07 +00001193/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001194static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001195 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001196{
bellard2f619692007-11-16 10:46:05 +00001197 socklen_t addrlen;
1198 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001199 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001200
bellard2f619692007-11-16 10:46:05 +00001201 if (get_user_u32(addrlen, target_addrlen_addr))
1202 return -TARGET_EFAULT;
1203
1204 addr = alloca(addrlen);
1205
pbrook1be9e1d2006-11-19 15:26:04 +00001206 ret = get_errno(getpeername(fd, addr, &addrlen));
1207 if (!is_error(ret)) {
1208 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001209 if (put_user_u32(addrlen, target_addrlen_addr))
1210 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001211 }
1212 return ret;
1213}
1214
ths0da46a62007-10-20 20:23:07 +00001215/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001216static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001217 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001218{
bellard2f619692007-11-16 10:46:05 +00001219 socklen_t addrlen;
1220 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001221 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001222
bellard2f619692007-11-16 10:46:05 +00001223 if (get_user_u32(addrlen, target_addrlen_addr))
1224 return -TARGET_EFAULT;
1225
1226 addr = alloca(addrlen);
1227
pbrook1be9e1d2006-11-19 15:26:04 +00001228 ret = get_errno(getsockname(fd, addr, &addrlen));
1229 if (!is_error(ret)) {
1230 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001231 if (put_user_u32(addrlen, target_addrlen_addr))
1232 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001233 }
1234 return ret;
1235}
1236
ths0da46a62007-10-20 20:23:07 +00001237/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001238static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00001239 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001240{
1241 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00001242 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001243
1244 ret = get_errno(socketpair(domain, type, protocol, tab));
1245 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00001246 if (put_user_s32(tab[0], target_tab_addr)
1247 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1248 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001249 }
1250 return ret;
1251}
1252
ths0da46a62007-10-20 20:23:07 +00001253/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001254static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1255 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001256{
1257 void *addr;
1258 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001259 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001260
bellard579a97f2007-11-11 14:26:47 +00001261 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1262 if (!host_msg)
1263 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001264 if (target_addr) {
1265 addr = alloca(addrlen);
1266 target_to_host_sockaddr(addr, target_addr, addrlen);
1267 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1268 } else {
1269 ret = get_errno(send(fd, host_msg, len, flags));
1270 }
1271 unlock_user(host_msg, msg, 0);
1272 return ret;
1273}
1274
ths0da46a62007-10-20 20:23:07 +00001275/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001276static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1277 abi_ulong target_addr,
1278 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001279{
1280 socklen_t addrlen;
1281 void *addr;
1282 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001283 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001284
bellard579a97f2007-11-11 14:26:47 +00001285 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1286 if (!host_msg)
1287 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001288 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00001289 if (get_user_u32(addrlen, target_addrlen)) {
1290 ret = -TARGET_EFAULT;
1291 goto fail;
1292 }
pbrook1be9e1d2006-11-19 15:26:04 +00001293 addr = alloca(addrlen);
1294 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1295 } else {
1296 addr = NULL; /* To keep compiler quiet. */
1297 ret = get_errno(recv(fd, host_msg, len, flags));
1298 }
1299 if (!is_error(ret)) {
1300 if (target_addr) {
1301 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001302 if (put_user_u32(addrlen, target_addrlen)) {
1303 ret = -TARGET_EFAULT;
1304 goto fail;
1305 }
pbrook1be9e1d2006-11-19 15:26:04 +00001306 }
1307 unlock_user(host_msg, msg, len);
1308 } else {
bellard2f619692007-11-16 10:46:05 +00001309fail:
pbrook1be9e1d2006-11-19 15:26:04 +00001310 unlock_user(host_msg, msg, 0);
1311 }
1312 return ret;
1313}
1314
j_mayer32407102007-09-26 23:01:49 +00001315#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00001316/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001317static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00001318{
blueswir1992f48a2007-10-14 16:27:31 +00001319 abi_long ret;
1320 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00001321
1322 switch(num) {
1323 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00001324 {
bellard2f619692007-11-16 10:46:05 +00001325 int domain, type, protocol;
1326
1327 if (get_user_s32(domain, vptr)
1328 || get_user_s32(type, vptr + n)
1329 || get_user_s32(protocol, vptr + 2 * n))
1330 return -TARGET_EFAULT;
1331
bellard3532fa72006-06-24 15:06:03 +00001332 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00001333 }
bellard31e31b82003-02-18 22:55:36 +00001334 break;
1335 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00001336 {
bellard2f619692007-11-16 10:46:05 +00001337 int sockfd;
1338 abi_ulong target_addr;
1339 socklen_t addrlen;
1340
1341 if (get_user_s32(sockfd, vptr)
1342 || get_user_ual(target_addr, vptr + n)
1343 || get_user_u32(addrlen, vptr + 2 * n))
1344 return -TARGET_EFAULT;
1345
bellard3532fa72006-06-24 15:06:03 +00001346 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001347 }
bellard31e31b82003-02-18 22:55:36 +00001348 break;
1349 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00001350 {
bellard2f619692007-11-16 10:46:05 +00001351 int sockfd;
1352 abi_ulong target_addr;
1353 socklen_t addrlen;
1354
1355 if (get_user_s32(sockfd, vptr)
1356 || get_user_ual(target_addr, vptr + n)
1357 || get_user_u32(addrlen, vptr + 2 * n))
1358 return -TARGET_EFAULT;
1359
bellard3532fa72006-06-24 15:06:03 +00001360 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001361 }
bellard31e31b82003-02-18 22:55:36 +00001362 break;
1363 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00001364 {
bellard2f619692007-11-16 10:46:05 +00001365 int sockfd, backlog;
1366
1367 if (get_user_s32(sockfd, vptr)
1368 || get_user_s32(backlog, vptr + n))
1369 return -TARGET_EFAULT;
1370
bellard7854b052003-03-29 17:22:23 +00001371 ret = get_errno(listen(sockfd, backlog));
1372 }
bellard31e31b82003-02-18 22:55:36 +00001373 break;
1374 case SOCKOP_accept:
1375 {
bellard2f619692007-11-16 10:46:05 +00001376 int sockfd;
1377 abi_ulong target_addr, target_addrlen;
1378
1379 if (get_user_s32(sockfd, vptr)
1380 || get_user_ual(target_addr, vptr + n)
1381 || get_user_u32(target_addrlen, vptr + 2 * n))
1382 return -TARGET_EFAULT;
1383
pbrook1be9e1d2006-11-19 15:26:04 +00001384 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001385 }
1386 break;
1387 case SOCKOP_getsockname:
1388 {
bellard2f619692007-11-16 10:46:05 +00001389 int sockfd;
1390 abi_ulong target_addr, target_addrlen;
1391
1392 if (get_user_s32(sockfd, vptr)
1393 || get_user_ual(target_addr, vptr + n)
1394 || get_user_u32(target_addrlen, vptr + 2 * n))
1395 return -TARGET_EFAULT;
1396
pbrook1be9e1d2006-11-19 15:26:04 +00001397 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001398 }
1399 break;
1400 case SOCKOP_getpeername:
1401 {
bellard2f619692007-11-16 10:46:05 +00001402 int sockfd;
1403 abi_ulong target_addr, target_addrlen;
1404
1405 if (get_user_s32(sockfd, vptr)
1406 || get_user_ual(target_addr, vptr + n)
1407 || get_user_u32(target_addrlen, vptr + 2 * n))
1408 return -TARGET_EFAULT;
1409
pbrook1be9e1d2006-11-19 15:26:04 +00001410 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001411 }
1412 break;
1413 case SOCKOP_socketpair:
1414 {
bellard2f619692007-11-16 10:46:05 +00001415 int domain, type, protocol;
1416 abi_ulong tab;
1417
1418 if (get_user_s32(domain, vptr)
1419 || get_user_s32(type, vptr + n)
1420 || get_user_s32(protocol, vptr + 2 * n)
1421 || get_user_ual(tab, vptr + 3 * n))
1422 return -TARGET_EFAULT;
1423
pbrook1be9e1d2006-11-19 15:26:04 +00001424 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00001425 }
1426 break;
1427 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00001428 {
bellard2f619692007-11-16 10:46:05 +00001429 int sockfd;
1430 abi_ulong msg;
1431 size_t len;
1432 int flags;
1433
1434 if (get_user_s32(sockfd, vptr)
1435 || get_user_ual(msg, vptr + n)
1436 || get_user_ual(len, vptr + 2 * n)
1437 || get_user_s32(flags, vptr + 3 * n))
1438 return -TARGET_EFAULT;
1439
pbrook1be9e1d2006-11-19 15:26:04 +00001440 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001441 }
bellard31e31b82003-02-18 22:55:36 +00001442 break;
1443 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00001444 {
bellard2f619692007-11-16 10:46:05 +00001445 int sockfd;
1446 abi_ulong msg;
1447 size_t len;
1448 int flags;
1449
1450 if (get_user_s32(sockfd, vptr)
1451 || get_user_ual(msg, vptr + n)
1452 || get_user_ual(len, vptr + 2 * n)
1453 || get_user_s32(flags, vptr + 3 * n))
1454 return -TARGET_EFAULT;
1455
pbrook1be9e1d2006-11-19 15:26:04 +00001456 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001457 }
bellard31e31b82003-02-18 22:55:36 +00001458 break;
1459 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00001460 {
bellard2f619692007-11-16 10:46:05 +00001461 int sockfd;
1462 abi_ulong msg;
1463 size_t len;
1464 int flags;
1465 abi_ulong addr;
1466 socklen_t addrlen;
1467
1468 if (get_user_s32(sockfd, vptr)
1469 || get_user_ual(msg, vptr + n)
1470 || get_user_ual(len, vptr + 2 * n)
1471 || get_user_s32(flags, vptr + 3 * n)
1472 || get_user_ual(addr, vptr + 4 * n)
1473 || get_user_u32(addrlen, vptr + 5 * n))
1474 return -TARGET_EFAULT;
1475
pbrook1be9e1d2006-11-19 15:26:04 +00001476 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001477 }
bellard31e31b82003-02-18 22:55:36 +00001478 break;
1479 case SOCKOP_recvfrom:
1480 {
bellard2f619692007-11-16 10:46:05 +00001481 int sockfd;
1482 abi_ulong msg;
1483 size_t len;
1484 int flags;
1485 abi_ulong addr;
1486 socklen_t addrlen;
1487
1488 if (get_user_s32(sockfd, vptr)
1489 || get_user_ual(msg, vptr + n)
1490 || get_user_ual(len, vptr + 2 * n)
1491 || get_user_s32(flags, vptr + 3 * n)
1492 || get_user_ual(addr, vptr + 4 * n)
1493 || get_user_u32(addrlen, vptr + 5 * n))
1494 return -TARGET_EFAULT;
1495
pbrook1be9e1d2006-11-19 15:26:04 +00001496 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00001497 }
1498 break;
1499 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00001500 {
bellard2f619692007-11-16 10:46:05 +00001501 int sockfd, how;
1502
1503 if (get_user_s32(sockfd, vptr)
1504 || get_user_s32(how, vptr + n))
1505 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001506
1507 ret = get_errno(shutdown(sockfd, how));
1508 }
bellard31e31b82003-02-18 22:55:36 +00001509 break;
1510 case SOCKOP_sendmsg:
1511 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00001512 {
1513 int fd;
blueswir1992f48a2007-10-14 16:27:31 +00001514 abi_ulong target_msg;
bellard3532fa72006-06-24 15:06:03 +00001515 int flags;
bellard1a9353d2003-03-16 20:28:50 +00001516
bellard2f619692007-11-16 10:46:05 +00001517 if (get_user_s32(fd, vptr)
1518 || get_user_ual(target_msg, vptr + n)
1519 || get_user_s32(flags, vptr + 2 * n))
1520 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001521
ths5fafdf22007-09-16 21:08:06 +00001522 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00001523 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00001524 }
1525 break;
bellard31e31b82003-02-18 22:55:36 +00001526 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00001527 {
bellard2f619692007-11-16 10:46:05 +00001528 int sockfd;
1529 int level;
1530 int optname;
1531 abi_ulong optval;
1532 socklen_t optlen;
1533
1534 if (get_user_s32(sockfd, vptr)
1535 || get_user_s32(level, vptr + n)
1536 || get_user_s32(optname, vptr + 2 * n)
1537 || get_user_ual(optval, vptr + 3 * n)
1538 || get_user_u32(optlen, vptr + 4 * n))
1539 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001540
1541 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1542 }
1543 break;
bellard31e31b82003-02-18 22:55:36 +00001544 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00001545 {
bellard2f619692007-11-16 10:46:05 +00001546 int sockfd;
1547 int level;
1548 int optname;
1549 abi_ulong optval;
1550 socklen_t optlen;
bellard7854b052003-03-29 17:22:23 +00001551
bellard2f619692007-11-16 10:46:05 +00001552 if (get_user_s32(sockfd, vptr)
1553 || get_user_s32(level, vptr + n)
1554 || get_user_s32(optname, vptr + 2 * n)
1555 || get_user_ual(optval, vptr + 3 * n)
1556 || get_user_u32(optlen, vptr + 4 * n))
1557 return -TARGET_EFAULT;
1558
1559 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
bellard7854b052003-03-29 17:22:23 +00001560 }
1561 break;
bellard31e31b82003-02-18 22:55:36 +00001562 default:
1563 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00001564 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00001565 break;
1566 }
1567 return ret;
1568}
j_mayer32407102007-09-26 23:01:49 +00001569#endif
bellard31e31b82003-02-18 22:55:36 +00001570
j_mayer32407102007-09-26 23:01:49 +00001571#ifdef TARGET_NR_ipc
bellard8853f862004-02-22 14:57:26 +00001572#define N_SHM_REGIONS 32
1573
1574static struct shm_region {
bellard5a4a8982007-11-11 17:39:18 +00001575 abi_ulong start;
1576 abi_ulong size;
bellard8853f862004-02-22 14:57:26 +00001577} shm_regions[N_SHM_REGIONS];
1578
ths3eb6b042007-06-03 14:26:27 +00001579struct target_ipc_perm
1580{
blueswir1992f48a2007-10-14 16:27:31 +00001581 abi_long __key;
1582 abi_ulong uid;
1583 abi_ulong gid;
1584 abi_ulong cuid;
1585 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00001586 unsigned short int mode;
1587 unsigned short int __pad1;
1588 unsigned short int __seq;
1589 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00001590 abi_ulong __unused1;
1591 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00001592};
1593
1594struct target_semid_ds
1595{
1596 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001597 abi_ulong sem_otime;
1598 abi_ulong __unused1;
1599 abi_ulong sem_ctime;
1600 abi_ulong __unused2;
1601 abi_ulong sem_nsems;
1602 abi_ulong __unused3;
1603 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00001604};
1605
bellard579a97f2007-11-11 14:26:47 +00001606static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
1607 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001608{
1609 struct target_ipc_perm *target_ip;
1610 struct target_semid_ds *target_sd;
1611
bellard579a97f2007-11-11 14:26:47 +00001612 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1613 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001614 target_ip=&(target_sd->sem_perm);
1615 host_ip->__key = tswapl(target_ip->__key);
1616 host_ip->uid = tswapl(target_ip->uid);
1617 host_ip->gid = tswapl(target_ip->gid);
1618 host_ip->cuid = tswapl(target_ip->cuid);
1619 host_ip->cgid = tswapl(target_ip->cgid);
1620 host_ip->mode = tswapl(target_ip->mode);
1621 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001622 return 0;
ths3eb6b042007-06-03 14:26:27 +00001623}
1624
bellard579a97f2007-11-11 14:26:47 +00001625static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
1626 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00001627{
1628 struct target_ipc_perm *target_ip;
1629 struct target_semid_ds *target_sd;
1630
bellard579a97f2007-11-11 14:26:47 +00001631 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1632 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001633 target_ip = &(target_sd->sem_perm);
1634 target_ip->__key = tswapl(host_ip->__key);
1635 target_ip->uid = tswapl(host_ip->uid);
1636 target_ip->gid = tswapl(host_ip->gid);
1637 target_ip->cuid = tswapl(host_ip->cuid);
1638 target_ip->cgid = tswapl(host_ip->cgid);
1639 target_ip->mode = tswapl(host_ip->mode);
1640 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001641 return 0;
ths3eb6b042007-06-03 14:26:27 +00001642}
1643
bellard579a97f2007-11-11 14:26:47 +00001644static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
1645 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001646{
1647 struct target_semid_ds *target_sd;
1648
bellard579a97f2007-11-11 14:26:47 +00001649 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1650 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001651 target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1652 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1653 host_sd->sem_otime = tswapl(target_sd->sem_otime);
1654 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1655 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001656 return 0;
ths3eb6b042007-06-03 14:26:27 +00001657}
1658
bellard579a97f2007-11-11 14:26:47 +00001659static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
1660 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00001661{
1662 struct target_semid_ds *target_sd;
1663
bellard579a97f2007-11-11 14:26:47 +00001664 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1665 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001666 host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1667 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1668 target_sd->sem_otime = tswapl(host_sd->sem_otime);
1669 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1670 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001671 return 0;
ths3eb6b042007-06-03 14:26:27 +00001672}
1673
thsfa294812007-02-02 22:05:00 +00001674union semun {
1675 int val;
ths3eb6b042007-06-03 14:26:27 +00001676 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00001677 unsigned short *array;
1678};
1679
ths3eb6b042007-06-03 14:26:27 +00001680union target_semun {
1681 int val;
blueswir1992f48a2007-10-14 16:27:31 +00001682 abi_long buf;
ths3eb6b042007-06-03 14:26:27 +00001683 unsigned short int *array;
1684};
1685
bellard579a97f2007-11-11 14:26:47 +00001686static inline abi_long target_to_host_semun(int cmd,
1687 union semun *host_su,
1688 abi_ulong target_addr,
1689 struct semid_ds *ds)
ths3eb6b042007-06-03 14:26:27 +00001690{
1691 union target_semun *target_su;
1692
1693 switch( cmd ) {
1694 case IPC_STAT:
1695 case IPC_SET:
bellard579a97f2007-11-11 14:26:47 +00001696 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1697 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001698 target_to_host_semid_ds(ds,target_su->buf);
1699 host_su->buf = ds;
1700 unlock_user_struct(target_su, target_addr, 0);
1701 break;
1702 case GETVAL:
1703 case SETVAL:
bellard579a97f2007-11-11 14:26:47 +00001704 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1705 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001706 host_su->val = tswapl(target_su->val);
1707 unlock_user_struct(target_su, target_addr, 0);
1708 break;
1709 case GETALL:
1710 case SETALL:
bellard579a97f2007-11-11 14:26:47 +00001711 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1712 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001713 *host_su->array = tswap16(*target_su->array);
1714 unlock_user_struct(target_su, target_addr, 0);
1715 break;
1716 default:
1717 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1718 }
bellard579a97f2007-11-11 14:26:47 +00001719 return 0;
ths3eb6b042007-06-03 14:26:27 +00001720}
1721
bellard579a97f2007-11-11 14:26:47 +00001722static inline abi_long host_to_target_semun(int cmd,
1723 abi_ulong target_addr,
1724 union semun *host_su,
1725 struct semid_ds *ds)
ths3eb6b042007-06-03 14:26:27 +00001726{
1727 union target_semun *target_su;
1728
1729 switch( cmd ) {
1730 case IPC_STAT:
1731 case IPC_SET:
bellard579a97f2007-11-11 14:26:47 +00001732 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1733 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001734 host_to_target_semid_ds(target_su->buf,ds);
1735 unlock_user_struct(target_su, target_addr, 1);
1736 break;
1737 case GETVAL:
1738 case SETVAL:
bellard579a97f2007-11-11 14:26:47 +00001739 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1740 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001741 target_su->val = tswapl(host_su->val);
1742 unlock_user_struct(target_su, target_addr, 1);
1743 break;
1744 case GETALL:
1745 case SETALL:
bellard579a97f2007-11-11 14:26:47 +00001746 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1747 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001748 *target_su->array = tswap16(*host_su->array);
1749 unlock_user_struct(target_su, target_addr, 1);
1750 break;
1751 default:
1752 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1753 }
bellard579a97f2007-11-11 14:26:47 +00001754 return 0;
ths3eb6b042007-06-03 14:26:27 +00001755}
1756
blueswir1992f48a2007-10-14 16:27:31 +00001757static inline abi_long do_semctl(int first, int second, int third,
1758 abi_long ptr)
ths3eb6b042007-06-03 14:26:27 +00001759{
1760 union semun arg;
1761 struct semid_ds dsarg;
1762 int cmd = third&0xff;
blueswir1992f48a2007-10-14 16:27:31 +00001763 abi_long ret = 0;
ths3eb6b042007-06-03 14:26:27 +00001764
1765 switch( cmd ) {
1766 case GETVAL:
1767 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1768 ret = get_errno(semctl(first, second, cmd, arg));
1769 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1770 break;
1771 case SETVAL:
1772 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1773 ret = get_errno(semctl(first, second, cmd, arg));
1774 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1775 break;
1776 case GETALL:
1777 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1778 ret = get_errno(semctl(first, second, cmd, arg));
1779 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1780 break;
1781 case SETALL:
1782 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1783 ret = get_errno(semctl(first, second, cmd, arg));
1784 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1785 break;
1786 case IPC_STAT:
1787 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1788 ret = get_errno(semctl(first, second, cmd, arg));
1789 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1790 break;
1791 case IPC_SET:
1792 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1793 ret = get_errno(semctl(first, second, cmd, arg));
1794 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1795 break;
1796 default:
1797 ret = get_errno(semctl(first, second, cmd, arg));
1798 }
1799
1800 return ret;
1801}
1802
ths1bc012f2007-06-03 14:27:49 +00001803struct target_msqid_ds
1804{
1805 struct target_ipc_perm msg_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001806 abi_ulong msg_stime;
1807 abi_ulong __unused1;
1808 abi_ulong msg_rtime;
1809 abi_ulong __unused2;
1810 abi_ulong msg_ctime;
1811 abi_ulong __unused3;
1812 abi_ulong __msg_cbytes;
1813 abi_ulong msg_qnum;
1814 abi_ulong msg_qbytes;
1815 abi_ulong msg_lspid;
1816 abi_ulong msg_lrpid;
1817 abi_ulong __unused4;
1818 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00001819};
1820
bellard579a97f2007-11-11 14:26:47 +00001821static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
1822 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00001823{
1824 struct target_msqid_ds *target_md;
1825
bellard579a97f2007-11-11 14:26:47 +00001826 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
1827 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001828 target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1829 host_md->msg_stime = tswapl(target_md->msg_stime);
1830 host_md->msg_rtime = tswapl(target_md->msg_rtime);
1831 host_md->msg_ctime = tswapl(target_md->msg_ctime);
1832 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1833 host_md->msg_qnum = tswapl(target_md->msg_qnum);
1834 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1835 host_md->msg_lspid = tswapl(target_md->msg_lspid);
1836 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1837 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001838 return 0;
ths1bc012f2007-06-03 14:27:49 +00001839}
1840
bellard579a97f2007-11-11 14:26:47 +00001841static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
1842 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00001843{
1844 struct target_msqid_ds *target_md;
1845
bellard579a97f2007-11-11 14:26:47 +00001846 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
1847 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001848 host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1849 target_md->msg_stime = tswapl(host_md->msg_stime);
1850 target_md->msg_rtime = tswapl(host_md->msg_rtime);
1851 target_md->msg_ctime = tswapl(host_md->msg_ctime);
1852 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1853 target_md->msg_qnum = tswapl(host_md->msg_qnum);
1854 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1855 target_md->msg_lspid = tswapl(host_md->msg_lspid);
1856 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1857 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001858 return 0;
ths1bc012f2007-06-03 14:27:49 +00001859}
1860
blueswir1992f48a2007-10-14 16:27:31 +00001861static inline abi_long do_msgctl(int first, int second, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00001862{
1863 struct msqid_ds dsarg;
1864 int cmd = second&0xff;
blueswir1992f48a2007-10-14 16:27:31 +00001865 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001866 switch( cmd ) {
1867 case IPC_STAT:
1868 case IPC_SET:
1869 target_to_host_msqid_ds(&dsarg,ptr);
1870 ret = get_errno(msgctl(first, cmd, &dsarg));
1871 host_to_target_msqid_ds(ptr,&dsarg);
1872 default:
1873 ret = get_errno(msgctl(first, cmd, &dsarg));
1874 }
1875 return ret;
1876}
1877
1878struct target_msgbuf {
blueswir1992f48a2007-10-14 16:27:31 +00001879 abi_ulong mtype;
ths1bc012f2007-06-03 14:27:49 +00001880 char mtext[1];
1881};
1882
blueswir1992f48a2007-10-14 16:27:31 +00001883static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1884 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001885{
1886 struct target_msgbuf *target_mb;
1887 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00001888 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001889
bellard579a97f2007-11-11 14:26:47 +00001890 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
1891 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001892 host_mb = malloc(msgsz+sizeof(long));
1893 host_mb->mtype = tswapl(target_mb->mtype);
1894 memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1895 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1896 free(host_mb);
1897 unlock_user_struct(target_mb, msgp, 0);
1898
1899 return ret;
1900}
1901
blueswir1992f48a2007-10-14 16:27:31 +00001902static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1903 unsigned int msgsz, int msgtype,
1904 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001905{
1906 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00001907 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00001908 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00001909 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001910
bellard579a97f2007-11-11 14:26:47 +00001911 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
1912 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001913 host_mb = malloc(msgsz+sizeof(long));
1914 ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
bellard579a97f2007-11-11 14:26:47 +00001915 if (ret > 0) {
1916 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
1917 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
1918 if (!target_mtext) {
1919 ret = -TARGET_EFAULT;
1920 goto end;
1921 }
ths1bc012f2007-06-03 14:27:49 +00001922 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00001923 unlock_user(target_mtext, target_mtext_addr, ret);
1924 }
ths1bc012f2007-06-03 14:27:49 +00001925 target_mb->mtype = tswapl(host_mb->mtype);
1926 free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00001927
bellard579a97f2007-11-11 14:26:47 +00001928end:
1929 if (target_mb)
1930 unlock_user_struct(target_mb, msgp, 1);
ths1bc012f2007-06-03 14:27:49 +00001931 return ret;
1932}
1933
pbrook53a59602006-03-25 19:31:22 +00001934/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00001935/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001936static abi_long do_ipc(unsigned int call, int first,
1937 int second, int third,
1938 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00001939{
1940 int version;
blueswir1992f48a2007-10-14 16:27:31 +00001941 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00001942 struct shmid_ds shm_info;
1943 int i;
1944
1945 version = call >> 16;
1946 call &= 0xffff;
1947
1948 switch (call) {
thsfa294812007-02-02 22:05:00 +00001949 case IPCOP_semop:
bellard579a97f2007-11-11 14:26:47 +00001950 ret = get_errno(semop(first,(struct sembuf *)g2h(ptr), second));
thsfa294812007-02-02 22:05:00 +00001951 break;
1952
1953 case IPCOP_semget:
1954 ret = get_errno(semget(first, second, third));
1955 break;
1956
1957 case IPCOP_semctl:
ths3eb6b042007-06-03 14:26:27 +00001958 ret = do_semctl(first, second, third, ptr);
thsfa294812007-02-02 22:05:00 +00001959 break;
1960
1961 case IPCOP_semtimedop:
j_mayer32407102007-09-26 23:01:49 +00001962 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00001963 ret = -TARGET_ENOSYS;
thsfa294812007-02-02 22:05:00 +00001964 break;
thsd96372e2007-02-02 22:05:44 +00001965
1966 case IPCOP_msgget:
1967 ret = get_errno(msgget(first, second));
1968 break;
1969
1970 case IPCOP_msgsnd:
ths1bc012f2007-06-03 14:27:49 +00001971 ret = do_msgsnd(first, ptr, second, third);
thsd96372e2007-02-02 22:05:44 +00001972 break;
1973
1974 case IPCOP_msgctl:
ths1bc012f2007-06-03 14:27:49 +00001975 ret = do_msgctl(first, second, ptr);
thsd96372e2007-02-02 22:05:44 +00001976 break;
1977
1978 case IPCOP_msgrcv:
ths1bc012f2007-06-03 14:27:49 +00001979 {
bellard579a97f2007-11-11 14:26:47 +00001980 /* XXX: this code is not correct */
ths1bc012f2007-06-03 14:27:49 +00001981 struct ipc_kludge
1982 {
1983 void *__unbounded msgp;
1984 long int msgtyp;
1985 };
thsd96372e2007-02-02 22:05:44 +00001986
bellard579a97f2007-11-11 14:26:47 +00001987 struct ipc_kludge *foo = (struct ipc_kludge *)g2h(ptr);
ths1bc012f2007-06-03 14:27:49 +00001988 struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
thsd96372e2007-02-02 22:05:44 +00001989
ths1bc012f2007-06-03 14:27:49 +00001990 ret = do_msgrcv(first, (long)msgp, second, 0, third);
thsd96372e2007-02-02 22:05:44 +00001991
ths1bc012f2007-06-03 14:27:49 +00001992 }
thsd96372e2007-02-02 22:05:44 +00001993 break;
1994
bellard8853f862004-02-22 14:57:26 +00001995 case IPCOP_shmat:
bellard5a4a8982007-11-11 17:39:18 +00001996 {
1997 abi_ulong raddr;
1998 void *host_addr;
1999 /* SHM_* flags are the same on all linux platforms */
2000 host_addr = shmat(first, (void *)g2h(ptr), second);
2001 if (host_addr == (void *)-1) {
2002 ret = get_errno((long)host_addr);
bellard8853f862004-02-22 14:57:26 +00002003 break;
bellard5a4a8982007-11-11 17:39:18 +00002004 }
2005 raddr = h2g((unsigned long)host_addr);
2006 /* find out the length of the shared memory segment */
2007
2008 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
2009 if (is_error(ret)) {
2010 /* can't get length, bail out */
2011 shmdt(host_addr);
2012 break;
2013 }
2014 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2015 PAGE_VALID | PAGE_READ |
2016 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
2017 for (i = 0; i < N_SHM_REGIONS; ++i) {
2018 if (shm_regions[i].start == 0) {
2019 shm_regions[i].start = raddr;
2020 shm_regions[i].size = shm_info.shm_segsz;
2021 break;
2022 }
2023 }
bellard2f619692007-11-16 10:46:05 +00002024 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00002025 return -TARGET_EFAULT;
2026 ret = 0;
2027 }
bellard8853f862004-02-22 14:57:26 +00002028 break;
2029 case IPCOP_shmdt:
2030 for (i = 0; i < N_SHM_REGIONS; ++i) {
2031 if (shm_regions[i].start == ptr) {
2032 shm_regions[i].start = 0;
2033 page_set_flags(ptr, shm_regions[i].size, 0);
2034 break;
2035 }
2036 }
bellard5a4a8982007-11-11 17:39:18 +00002037 ret = get_errno(shmdt((void *)g2h(ptr)));
bellard8853f862004-02-22 14:57:26 +00002038 break;
2039
2040 case IPCOP_shmget:
2041 /* IPC_* flag values are the same on all linux platforms */
2042 ret = get_errno(shmget(first, second, third));
2043 break;
2044
2045 /* IPC_* and SHM_* command values are the same on all linux platforms */
2046 case IPCOP_shmctl:
2047 switch(second) {
2048 case IPC_RMID:
2049 case SHM_LOCK:
2050 case SHM_UNLOCK:
2051 ret = get_errno(shmctl(first, second, NULL));
2052 break;
2053 default:
2054 goto unimplemented;
2055 }
2056 break;
2057 default:
2058 unimplemented:
j_mayer32407102007-09-26 23:01:49 +00002059 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00002060 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00002061 break;
2062 }
2063 return ret;
2064}
j_mayer32407102007-09-26 23:01:49 +00002065#endif
bellard8853f862004-02-22 14:57:26 +00002066
bellard31e31b82003-02-18 22:55:36 +00002067/* kernel structure types definitions */
2068#define IFNAMSIZ 16
2069
2070#define STRUCT(name, list...) STRUCT_ ## name,
2071#define STRUCT_SPECIAL(name) STRUCT_ ## name,
2072enum {
2073#include "syscall_types.h"
2074};
2075#undef STRUCT
2076#undef STRUCT_SPECIAL
2077
2078#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
2079#define STRUCT_SPECIAL(name)
2080#include "syscall_types.h"
2081#undef STRUCT
2082#undef STRUCT_SPECIAL
2083
2084typedef struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00002085 unsigned int target_cmd;
2086 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00002087 const char *name;
2088 int access;
bellard1a9353d2003-03-16 20:28:50 +00002089 const argtype arg_type[5];
bellard31e31b82003-02-18 22:55:36 +00002090} IOCTLEntry;
2091
2092#define IOC_R 0x0001
2093#define IOC_W 0x0002
2094#define IOC_RW (IOC_R | IOC_W)
2095
2096#define MAX_STRUCT_SIZE 4096
2097
bellard2ab83ea2003-06-15 19:56:46 +00002098IOCTLEntry ioctl_entries[] = {
bellard31e31b82003-02-18 22:55:36 +00002099#define IOCTL(cmd, access, types...) \
2100 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
2101#include "ioctls.h"
2102 { 0, 0, },
2103};
2104
pbrook53a59602006-03-25 19:31:22 +00002105/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00002106/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002107static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00002108{
2109 const IOCTLEntry *ie;
2110 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00002111 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00002112 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00002113 int target_size;
2114 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00002115
2116 ie = ioctl_entries;
2117 for(;;) {
2118 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00002119 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00002120 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002121 }
2122 if (ie->target_cmd == cmd)
2123 break;
2124 ie++;
2125 }
2126 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00002127#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00002128 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00002129#endif
bellard31e31b82003-02-18 22:55:36 +00002130 switch(arg_type[0]) {
2131 case TYPE_NULL:
2132 /* no argument */
2133 ret = get_errno(ioctl(fd, ie->host_cmd));
2134 break;
2135 case TYPE_PTRVOID:
2136 case TYPE_INT:
2137 /* int argment */
2138 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2139 break;
2140 case TYPE_PTR:
2141 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00002142 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00002143 switch(ie->access) {
2144 case IOC_R:
2145 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2146 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002147 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2148 if (!argptr)
2149 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002150 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2151 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00002152 }
2153 break;
2154 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00002155 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2156 if (!argptr)
2157 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002158 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2159 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002160 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2161 break;
2162 default:
2163 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00002164 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2165 if (!argptr)
2166 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002167 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2168 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002169 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2170 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002171 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2172 if (!argptr)
2173 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002174 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2175 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00002176 }
2177 break;
2178 }
2179 break;
2180 default:
j_mayer32407102007-09-26 23:01:49 +00002181 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2182 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00002183 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002184 break;
2185 }
2186 return ret;
2187}
2188
2189bitmask_transtbl iflag_tbl[] = {
2190 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2191 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2192 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2193 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2194 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2195 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2196 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2197 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2198 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2199 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2200 { TARGET_IXON, TARGET_IXON, IXON, IXON },
2201 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2202 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
2203 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
2204 { 0, 0, 0, 0 }
2205};
2206
2207bitmask_transtbl oflag_tbl[] = {
2208 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
2209 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
2210 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
2211 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
2212 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
2213 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
2214 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
2215 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
2216 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
2217 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
2218 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
2219 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
2220 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
2221 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
2222 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
2223 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
2224 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
2225 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
2226 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
2227 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
2228 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
2229 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
2230 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
2231 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
2232 { 0, 0, 0, 0 }
2233};
2234
2235bitmask_transtbl cflag_tbl[] = {
2236 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
2237 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
2238 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
2239 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
2240 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
2241 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
2242 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
2243 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
2244 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
2245 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
2246 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
2247 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
2248 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
2249 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
2250 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
2251 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
2252 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
2253 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
2254 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
2255 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
2256 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
2257 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
2258 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
2259 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
2260 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
2261 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
2262 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
2263 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
2264 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
2265 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
2266 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
2267 { 0, 0, 0, 0 }
2268};
2269
2270bitmask_transtbl lflag_tbl[] = {
2271 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2272 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2273 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2274 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2275 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2276 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2277 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2278 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2279 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2280 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2281 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2282 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2283 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2284 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2285 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
2286 { 0, 0, 0, 0 }
2287};
2288
2289static void target_to_host_termios (void *dst, const void *src)
2290{
2291 struct host_termios *host = dst;
2292 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00002293
ths5fafdf22007-09-16 21:08:06 +00002294 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002295 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002296 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002297 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002298 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002299 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002300 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002301 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2302 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00002303
ths5fafdf22007-09-16 21:08:06 +00002304 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2305 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00002306 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00002307 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00002308 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00002309 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00002310 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00002311 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00002312 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00002313 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2314 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00002315 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2316 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2317 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2318 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2319 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00002320 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00002321}
ths3b46e622007-09-17 08:09:54 +00002322
bellard31e31b82003-02-18 22:55:36 +00002323static void host_to_target_termios (void *dst, const void *src)
2324{
2325 struct target_termios *target = dst;
2326 const struct host_termios *host = src;
2327
ths5fafdf22007-09-16 21:08:06 +00002328 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002329 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002330 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002331 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002332 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002333 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002334 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002335 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2336 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00002337
bellard31e31b82003-02-18 22:55:36 +00002338 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2339 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2340 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2341 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2342 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2343 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2344 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2345 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2346 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2347 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2348 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2349 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2350 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2351 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2352 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2353 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2354 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2355}
2356
2357StructEntry struct_termios_def = {
2358 .convert = { host_to_target_termios, target_to_host_termios },
2359 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2360 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2361};
2362
bellard5286db72003-06-05 00:57:30 +00002363static bitmask_transtbl mmap_flags_tbl[] = {
2364 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2365 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2366 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2367 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2368 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2369 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2370 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2371 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2372 { 0, 0, 0, 0 }
2373};
2374
bellardffa65c32004-01-04 23:57:22 +00002375static bitmask_transtbl fcntl_flags_tbl[] = {
2376 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
2377 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
2378 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
2379 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
2380 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
2381 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
2382 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
2383 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
2384 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
2385 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
2386 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2387 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
2388 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
bellard121061d2004-02-22 11:56:01 +00002389#if defined(O_DIRECT)
bellardffa65c32004-01-04 23:57:22 +00002390 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
bellard121061d2004-02-22 11:56:01 +00002391#endif
bellardffa65c32004-01-04 23:57:22 +00002392 { 0, 0, 0, 0 }
2393};
2394
bellard2ab83ea2003-06-15 19:56:46 +00002395#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00002396
2397/* NOTE: there is really one LDT for all the threads */
2398uint8_t *ldt_table;
2399
bellard03acab62007-11-11 14:57:14 +00002400static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002401{
2402 int size;
pbrook53a59602006-03-25 19:31:22 +00002403 void *p;
bellard6dbad632003-03-16 18:05:05 +00002404
2405 if (!ldt_table)
2406 return 0;
2407 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2408 if (size > bytecount)
2409 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00002410 p = lock_user(VERIFY_WRITE, ptr, size, 0);
2411 if (!p)
bellard03acab62007-11-11 14:57:14 +00002412 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00002413 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00002414 memcpy(p, ldt_table, size);
2415 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00002416 return size;
2417}
2418
2419/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00002420static abi_long write_ldt(CPUX86State *env,
2421 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00002422{
2423 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00002424 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00002425 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00002426 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00002427 uint32_t *lp, entry_1, entry_2;
2428
2429 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00002430 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00002431 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00002432 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002433 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2434 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2435 ldt_info.limit = tswap32(target_ldt_info->limit);
2436 ldt_info.flags = tswap32(target_ldt_info->flags);
2437 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00002438
bellard6dbad632003-03-16 18:05:05 +00002439 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00002440 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002441 seg_32bit = ldt_info.flags & 1;
2442 contents = (ldt_info.flags >> 1) & 3;
2443 read_exec_only = (ldt_info.flags >> 3) & 1;
2444 limit_in_pages = (ldt_info.flags >> 4) & 1;
2445 seg_not_present = (ldt_info.flags >> 5) & 1;
2446 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00002447#ifdef TARGET_ABI32
2448 lm = 0;
2449#else
2450 lm = (ldt_info.flags >> 7) & 1;
2451#endif
bellard6dbad632003-03-16 18:05:05 +00002452 if (contents == 3) {
2453 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00002454 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002455 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00002456 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002457 }
2458 /* allocate the LDT */
2459 if (!ldt_table) {
2460 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2461 if (!ldt_table)
bellard03acab62007-11-11 14:57:14 +00002462 return -TARGET_ENOMEM;
bellard6dbad632003-03-16 18:05:05 +00002463 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellardeeeac3f2007-11-11 19:34:32 +00002464 env->ldt.base = h2g((unsigned long)ldt_table);
bellard6dbad632003-03-16 18:05:05 +00002465 env->ldt.limit = 0xffff;
2466 }
2467
2468 /* NOTE: same code as Linux kernel */
2469 /* Allow LDTs to be cleared by the user. */
2470 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2471 if (oldmode ||
2472 (contents == 0 &&
2473 read_exec_only == 1 &&
2474 seg_32bit == 0 &&
2475 limit_in_pages == 0 &&
2476 seg_not_present == 1 &&
2477 useable == 0 )) {
2478 entry_1 = 0;
2479 entry_2 = 0;
2480 goto install;
2481 }
2482 }
ths3b46e622007-09-17 08:09:54 +00002483
bellard6dbad632003-03-16 18:05:05 +00002484 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2485 (ldt_info.limit & 0x0ffff);
2486 entry_2 = (ldt_info.base_addr & 0xff000000) |
2487 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2488 (ldt_info.limit & 0xf0000) |
2489 ((read_exec_only ^ 1) << 9) |
2490 (contents << 10) |
2491 ((seg_not_present ^ 1) << 15) |
2492 (seg_32bit << 22) |
2493 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00002494 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00002495 0x7000;
2496 if (!oldmode)
2497 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00002498
bellard6dbad632003-03-16 18:05:05 +00002499 /* Install the new entry ... */
2500install:
2501 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2502 lp[0] = tswap32(entry_1);
2503 lp[1] = tswap32(entry_2);
2504 return 0;
2505}
2506
2507/* specific and weird i386 syscalls */
bellard03acab62007-11-11 14:57:14 +00002508abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
2509 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002510{
bellard03acab62007-11-11 14:57:14 +00002511 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00002512
bellard6dbad632003-03-16 18:05:05 +00002513 switch (func) {
2514 case 0:
2515 ret = read_ldt(ptr, bytecount);
2516 break;
2517 case 1:
2518 ret = write_ldt(env, ptr, bytecount, 1);
2519 break;
2520 case 0x11:
2521 ret = write_ldt(env, ptr, bytecount, 0);
2522 break;
bellard03acab62007-11-11 14:57:14 +00002523 default:
2524 ret = -TARGET_ENOSYS;
2525 break;
bellard6dbad632003-03-16 18:05:05 +00002526 }
2527 return ret;
2528}
bellard1b6b0292003-03-22 17:31:38 +00002529
bellard8d18e892007-11-14 15:18:40 +00002530abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
2531{
2532 uint64_t *gdt_table = g2h(env->gdt.base);
2533 struct target_modify_ldt_ldt_s ldt_info;
2534 struct target_modify_ldt_ldt_s *target_ldt_info;
2535 int seg_32bit, contents, read_exec_only, limit_in_pages;
2536 int seg_not_present, useable, lm;
2537 uint32_t *lp, entry_1, entry_2;
2538 int i;
2539
2540 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
2541 if (!target_ldt_info)
2542 return -TARGET_EFAULT;
2543 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2544 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2545 ldt_info.limit = tswap32(target_ldt_info->limit);
2546 ldt_info.flags = tswap32(target_ldt_info->flags);
2547 if (ldt_info.entry_number == -1) {
2548 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
2549 if (gdt_table[i] == 0) {
2550 ldt_info.entry_number = i;
2551 target_ldt_info->entry_number = tswap32(i);
2552 break;
2553 }
2554 }
2555 }
2556 unlock_user_struct(target_ldt_info, ptr, 1);
2557
2558 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
2559 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
2560 return -TARGET_EINVAL;
2561 seg_32bit = ldt_info.flags & 1;
2562 contents = (ldt_info.flags >> 1) & 3;
2563 read_exec_only = (ldt_info.flags >> 3) & 1;
2564 limit_in_pages = (ldt_info.flags >> 4) & 1;
2565 seg_not_present = (ldt_info.flags >> 5) & 1;
2566 useable = (ldt_info.flags >> 6) & 1;
2567#ifdef TARGET_ABI32
2568 lm = 0;
2569#else
2570 lm = (ldt_info.flags >> 7) & 1;
2571#endif
2572
2573 if (contents == 3) {
2574 if (seg_not_present == 0)
2575 return -TARGET_EINVAL;
2576 }
2577
2578 /* NOTE: same code as Linux kernel */
2579 /* Allow LDTs to be cleared by the user. */
2580 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2581 if ((contents == 0 &&
2582 read_exec_only == 1 &&
2583 seg_32bit == 0 &&
2584 limit_in_pages == 0 &&
2585 seg_not_present == 1 &&
2586 useable == 0 )) {
2587 entry_1 = 0;
2588 entry_2 = 0;
2589 goto install;
2590 }
2591 }
2592
2593 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2594 (ldt_info.limit & 0x0ffff);
2595 entry_2 = (ldt_info.base_addr & 0xff000000) |
2596 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2597 (ldt_info.limit & 0xf0000) |
2598 ((read_exec_only ^ 1) << 9) |
2599 (contents << 10) |
2600 ((seg_not_present ^ 1) << 15) |
2601 (seg_32bit << 22) |
2602 (limit_in_pages << 23) |
2603 (useable << 20) |
2604 (lm << 21) |
2605 0x7000;
2606
2607 /* Install the new entry ... */
2608install:
2609 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
2610 lp[0] = tswap32(entry_1);
2611 lp[1] = tswap32(entry_2);
2612 return 0;
2613}
2614
2615abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
2616{
2617 struct target_modify_ldt_ldt_s *target_ldt_info;
2618 uint64_t *gdt_table = g2h(env->gdt.base);
2619 uint32_t base_addr, limit, flags;
2620 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
2621 int seg_not_present, useable, lm;
2622 uint32_t *lp, entry_1, entry_2;
2623
2624 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
2625 if (!target_ldt_info)
2626 return -TARGET_EFAULT;
2627 idx = tswap32(target_ldt_info->entry_number);
2628 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
2629 idx > TARGET_GDT_ENTRY_TLS_MAX) {
2630 unlock_user_struct(target_ldt_info, ptr, 1);
2631 return -TARGET_EINVAL;
2632 }
2633 lp = (uint32_t *)(gdt_table + idx);
2634 entry_1 = tswap32(lp[0]);
2635 entry_2 = tswap32(lp[1]);
2636
2637 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
2638 contents = (entry_2 >> 10) & 3;
2639 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
2640 seg_32bit = (entry_2 >> 22) & 1;
2641 limit_in_pages = (entry_2 >> 23) & 1;
2642 useable = (entry_2 >> 20) & 1;
2643#ifdef TARGET_ABI32
2644 lm = 0;
2645#else
2646 lm = (entry_2 >> 21) & 1;
2647#endif
2648 flags = (seg_32bit << 0) | (contents << 1) |
2649 (read_exec_only << 3) | (limit_in_pages << 4) |
2650 (seg_not_present << 5) | (useable << 6) | (lm << 7);
2651 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
2652 base_addr = (entry_1 >> 16) |
2653 (entry_2 & 0xff000000) |
2654 ((entry_2 & 0xff) << 16);
2655 target_ldt_info->base_addr = tswapl(base_addr);
2656 target_ldt_info->limit = tswap32(limit);
2657 target_ldt_info->flags = tswap32(flags);
2658 unlock_user_struct(target_ldt_info, ptr, 1);
2659 return 0;
2660}
2661
bellardd2fd1af2007-11-14 18:08:56 +00002662#ifndef TARGET_ABI32
2663abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
2664{
2665 abi_long ret;
2666 abi_ulong val;
2667 int idx;
2668
2669 switch(code) {
2670 case TARGET_ARCH_SET_GS:
2671 case TARGET_ARCH_SET_FS:
2672 if (code == TARGET_ARCH_SET_GS)
2673 idx = R_GS;
2674 else
2675 idx = R_FS;
2676 cpu_x86_load_seg(env, idx, 0);
2677 env->segs[idx].base = addr;
2678 break;
2679 case TARGET_ARCH_GET_GS:
2680 case TARGET_ARCH_GET_FS:
2681 if (code == TARGET_ARCH_GET_GS)
2682 idx = R_GS;
2683 else
2684 idx = R_FS;
2685 val = env->segs[idx].base;
2686 if (put_user(val, addr, abi_ulong))
2687 return -TARGET_EFAULT;
2688 break;
2689 default:
2690 ret = -TARGET_EINVAL;
2691 break;
2692 }
2693 return 0;
2694}
2695#endif
2696
bellard2ab83ea2003-06-15 19:56:46 +00002697#endif /* defined(TARGET_I386) */
2698
bellard1b6b0292003-03-22 17:31:38 +00002699/* this stack is the equivalent of the kernel stack associated with a
2700 thread/process */
2701#define NEW_STACK_SIZE 8192
2702
2703static int clone_func(void *arg)
2704{
bellard2ab83ea2003-06-15 19:56:46 +00002705 CPUState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00002706 cpu_loop(env);
2707 /* never exits */
2708 return 0;
2709}
2710
ths0da46a62007-10-20 20:23:07 +00002711/* do_fork() Must return host values and target errnos (unlike most
2712 do_*() functions). */
blueswir1992f48a2007-10-14 16:27:31 +00002713int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp)
bellard1b6b0292003-03-22 17:31:38 +00002714{
2715 int ret;
bellard5cd43932003-03-29 16:54:36 +00002716 TaskState *ts;
bellard1b6b0292003-03-22 17:31:38 +00002717 uint8_t *new_stack;
bellard2ab83ea2003-06-15 19:56:46 +00002718 CPUState *new_env;
ths3b46e622007-09-17 08:09:54 +00002719
bellard1b6b0292003-03-22 17:31:38 +00002720 if (flags & CLONE_VM) {
bellard5cd43932003-03-29 16:54:36 +00002721 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2722 memset(ts, 0, sizeof(TaskState));
2723 new_stack = ts->stack;
2724 ts->used = 1;
2725 /* add in task state list */
2726 ts->next = first_task_state;
2727 first_task_state = ts;
bellard1b6b0292003-03-22 17:31:38 +00002728 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00002729 new_env = cpu_copy(env);
bellard2ab83ea2003-06-15 19:56:46 +00002730#if defined(TARGET_I386)
2731 if (!newsp)
2732 newsp = env->regs[R_ESP];
bellard1b6b0292003-03-22 17:31:38 +00002733 new_env->regs[R_ESP] = newsp;
2734 new_env->regs[R_EAX] = 0;
bellard2ab83ea2003-06-15 19:56:46 +00002735#elif defined(TARGET_ARM)
2736 if (!newsp)
2737 newsp = env->regs[13];
2738 new_env->regs[13] = newsp;
2739 new_env->regs[0] = 0;
bellardebc05482003-09-30 21:08:41 +00002740#elif defined(TARGET_SPARC)
bellard48dc41e2006-06-21 18:15:50 +00002741 if (!newsp)
2742 newsp = env->regwptr[22];
2743 new_env->regwptr[22] = newsp;
2744 new_env->regwptr[0] = 0;
2745 /* XXXXX */
bellard67867302003-11-23 17:05:30 +00002746 printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
pbrooke6e59062006-10-22 00:18:54 +00002747#elif defined(TARGET_M68K)
2748 if (!newsp)
2749 newsp = env->aregs[7];
2750 new_env->aregs[7] = newsp;
2751 new_env->dregs[0] = 0;
2752 /* ??? is this sufficient? */
bellard048f6b42005-11-26 18:47:20 +00002753#elif defined(TARGET_MIPS)
ths388bb212007-05-13 13:58:00 +00002754 if (!newsp)
thsd0dc7dc2008-02-12 21:01:26 +00002755 newsp = env->gpr[env->current_tc][29];
2756 new_env->gpr[env->current_tc][29] = newsp;
bellard67867302003-11-23 17:05:30 +00002757#elif defined(TARGET_PPC)
2758 if (!newsp)
2759 newsp = env->gpr[1];
2760 new_env->gpr[1] = newsp;
ths5fafdf22007-09-16 21:08:06 +00002761 {
bellard67867302003-11-23 17:05:30 +00002762 int i;
2763 for (i = 7; i < 32; i++)
2764 new_env->gpr[i] = 0;
2765 }
bellardfdf9b3e2006-04-27 21:07:38 +00002766#elif defined(TARGET_SH4)
2767 if (!newsp)
2768 newsp = env->gregs[15];
2769 new_env->gregs[15] = newsp;
2770 /* XXXXX */
j_mayer7a3148a2007-04-05 07:13:51 +00002771#elif defined(TARGET_ALPHA)
2772 if (!newsp)
2773 newsp = env->ir[30];
2774 new_env->ir[30] = newsp;
2775 /* ? */
2776 {
2777 int i;
2778 for (i = 7; i < 30; i++)
2779 new_env->ir[i] = 0;
2780 }
ths48733d12007-10-08 13:36:46 +00002781#elif defined(TARGET_CRIS)
2782 if (!newsp)
2783 newsp = env->regs[14];
2784 new_env->regs[14] = newsp;
bellard2ab83ea2003-06-15 19:56:46 +00002785#else
2786#error unsupported target CPU
2787#endif
bellard5cd43932003-03-29 16:54:36 +00002788 new_env->opaque = ts;
bellard27725c12003-04-29 21:08:18 +00002789#ifdef __ia64__
bellardfd4a43e2006-04-24 20:32:17 +00002790 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00002791#else
2792 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2793#endif
bellard1b6b0292003-03-22 17:31:38 +00002794 } else {
2795 /* if no CLONE_VM, we consider it is a fork */
2796 if ((flags & ~CSIGNAL) != 0)
2797 return -EINVAL;
2798 ret = fork();
2799 }
2800 return ret;
2801}
2802
blueswir1992f48a2007-10-14 16:27:31 +00002803static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00002804{
2805 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00002806 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00002807 struct flock64 fl64;
2808 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00002809 abi_long ret;
pbrook53a59602006-03-25 19:31:22 +00002810
bellard7775e9e2003-05-14 22:46:48 +00002811 switch(cmd) {
2812 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00002813 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2814 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00002815 fl.l_type = tswap16(target_fl->l_type);
2816 fl.l_whence = tswap16(target_fl->l_whence);
2817 fl.l_start = tswapl(target_fl->l_start);
2818 fl.l_len = tswapl(target_fl->l_len);
2819 fl.l_pid = tswapl(target_fl->l_pid);
2820 unlock_user_struct(target_fl, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002821 ret = get_errno(fcntl(fd, cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00002822 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00002823 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
2824 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00002825 target_fl->l_type = tswap16(fl.l_type);
2826 target_fl->l_whence = tswap16(fl.l_whence);
2827 target_fl->l_start = tswapl(fl.l_start);
2828 target_fl->l_len = tswapl(fl.l_len);
2829 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00002830 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00002831 }
2832 break;
ths3b46e622007-09-17 08:09:54 +00002833
bellard7775e9e2003-05-14 22:46:48 +00002834 case TARGET_F_SETLK:
2835 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00002836 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2837 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00002838 fl.l_type = tswap16(target_fl->l_type);
2839 fl.l_whence = tswap16(target_fl->l_whence);
2840 fl.l_start = tswapl(target_fl->l_start);
2841 fl.l_len = tswapl(target_fl->l_len);
2842 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00002843 unlock_user_struct(target_fl, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002844 ret = get_errno(fcntl(fd, cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00002845 break;
ths3b46e622007-09-17 08:09:54 +00002846
bellard7775e9e2003-05-14 22:46:48 +00002847 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00002848 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2849 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00002850 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2851 fl64.l_whence = tswap16(target_fl64->l_whence);
2852 fl64.l_start = tswapl(target_fl64->l_start);
2853 fl64.l_len = tswapl(target_fl64->l_len);
2854 fl64.l_pid = tswap16(target_fl64->l_pid);
2855 unlock_user_struct(target_fl64, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002856 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
ths43f238d2007-01-05 20:55:49 +00002857 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00002858 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
2859 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00002860 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2861 target_fl64->l_whence = tswap16(fl64.l_whence);
2862 target_fl64->l_start = tswapl(fl64.l_start);
2863 target_fl64->l_len = tswapl(fl64.l_len);
2864 target_fl64->l_pid = tswapl(fl64.l_pid);
2865 unlock_user_struct(target_fl64, arg, 1);
2866 }
bellard9ee1fa22007-11-11 15:11:19 +00002867 break;
bellard7775e9e2003-05-14 22:46:48 +00002868 case TARGET_F_SETLK64:
2869 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00002870 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2871 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00002872 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2873 fl64.l_whence = tswap16(target_fl64->l_whence);
2874 fl64.l_start = tswapl(target_fl64->l_start);
2875 fl64.l_len = tswapl(target_fl64->l_len);
2876 fl64.l_pid = tswap16(target_fl64->l_pid);
2877 unlock_user_struct(target_fl64, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002878 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00002879 break;
2880
bellardffa65c32004-01-04 23:57:22 +00002881 case F_GETFL:
bellard9ee1fa22007-11-11 15:11:19 +00002882 ret = get_errno(fcntl(fd, cmd, arg));
2883 if (ret >= 0) {
2884 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2885 }
bellardffa65c32004-01-04 23:57:22 +00002886 break;
2887
2888 case F_SETFL:
bellard9ee1fa22007-11-11 15:11:19 +00002889 ret = get_errno(fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
bellardffa65c32004-01-04 23:57:22 +00002890 break;
2891
bellard7775e9e2003-05-14 22:46:48 +00002892 default:
bellard9ee1fa22007-11-11 15:11:19 +00002893 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00002894 break;
2895 }
2896 return ret;
2897}
2898
bellard67867302003-11-23 17:05:30 +00002899#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00002900
bellard67867302003-11-23 17:05:30 +00002901static inline int high2lowuid(int uid)
2902{
2903 if (uid > 65535)
2904 return 65534;
2905 else
2906 return uid;
2907}
2908
2909static inline int high2lowgid(int gid)
2910{
2911 if (gid > 65535)
2912 return 65534;
2913 else
2914 return gid;
2915}
2916
2917static inline int low2highuid(int uid)
2918{
2919 if ((int16_t)uid == -1)
2920 return -1;
2921 else
2922 return uid;
2923}
2924
2925static inline int low2highgid(int gid)
2926{
2927 if ((int16_t)gid == -1)
2928 return -1;
2929 else
2930 return gid;
2931}
2932
2933#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00002934
bellard31e31b82003-02-18 22:55:36 +00002935void syscall_init(void)
2936{
bellard2ab83ea2003-06-15 19:56:46 +00002937 IOCTLEntry *ie;
2938 const argtype *arg_type;
2939 int size;
thsb92c47c2007-11-01 00:07:38 +00002940 int i;
bellard2ab83ea2003-06-15 19:56:46 +00002941
ths5fafdf22007-09-16 21:08:06 +00002942#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2943#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00002944#include "syscall_types.h"
2945#undef STRUCT
2946#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00002947
2948 /* we patch the ioctl size if necessary. We rely on the fact that
2949 no ioctl has all the bits at '1' in the size field */
2950 ie = ioctl_entries;
2951 while (ie->target_cmd != 0) {
2952 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2953 TARGET_IOC_SIZEMASK) {
2954 arg_type = ie->arg_type;
2955 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00002956 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002957 ie->target_cmd);
2958 exit(1);
2959 }
2960 arg_type++;
2961 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00002962 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00002963 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2964 (size << TARGET_IOC_SIZESHIFT);
2965 }
thsb92c47c2007-11-01 00:07:38 +00002966
2967 /* Build target_to_host_errno_table[] table from
2968 * host_to_target_errno_table[]. */
2969 for (i=0; i < ERRNO_TABLE_SIZE; i++)
2970 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
2971
bellard2ab83ea2003-06-15 19:56:46 +00002972 /* automatic consistency check if same arch */
bellardd2fd1af2007-11-14 18:08:56 +00002973#if defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)
bellard2ab83ea2003-06-15 19:56:46 +00002974 if (ie->target_cmd != ie->host_cmd) {
ths5fafdf22007-09-16 21:08:06 +00002975 fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002976 ie->target_cmd, ie->host_cmd);
2977 }
2978#endif
2979 ie++;
2980 }
bellard31e31b82003-02-18 22:55:36 +00002981}
bellardc573ff62004-01-04 15:51:36 +00002982
blueswir1992f48a2007-10-14 16:27:31 +00002983#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00002984static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2985{
2986#ifdef TARGET_WORDS_BIG_ENDIAN
2987 return ((uint64_t)word0 << 32) | word1;
2988#else
2989 return ((uint64_t)word1 << 32) | word0;
2990#endif
2991}
blueswir1992f48a2007-10-14 16:27:31 +00002992#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00002993static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2994{
2995 return word0;
2996}
blueswir1992f48a2007-10-14 16:27:31 +00002997#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00002998
2999#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00003000static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
3001 abi_long arg2,
3002 abi_long arg3,
3003 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00003004{
3005#ifdef TARGET_ARM
3006 if (((CPUARMState *)cpu_env)->eabi)
3007 {
3008 arg2 = arg3;
3009 arg3 = arg4;
3010 }
3011#endif
3012 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
3013}
3014#endif
3015
3016#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00003017static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
3018 abi_long arg2,
3019 abi_long arg3,
3020 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00003021{
3022#ifdef TARGET_ARM
3023 if (((CPUARMState *)cpu_env)->eabi)
3024 {
3025 arg2 = arg3;
3026 arg3 = arg4;
3027 }
3028#endif
3029 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
3030}
3031#endif
3032
bellard579a97f2007-11-11 14:26:47 +00003033static inline abi_long target_to_host_timespec(struct timespec *host_ts,
3034 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00003035{
3036 struct target_timespec *target_ts;
3037
bellard579a97f2007-11-11 14:26:47 +00003038 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
3039 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003040 host_ts->tv_sec = tswapl(target_ts->tv_sec);
3041 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
3042 unlock_user_struct(target_ts, target_addr, 0);
3043}
3044
bellard579a97f2007-11-11 14:26:47 +00003045static inline abi_long host_to_target_timespec(abi_ulong target_addr,
3046 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00003047{
3048 struct target_timespec *target_ts;
3049
bellard579a97f2007-11-11 14:26:47 +00003050 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
3051 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003052 target_ts->tv_sec = tswapl(host_ts->tv_sec);
3053 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
3054 unlock_user_struct(target_ts, target_addr, 1);
3055}
3056
ths0da46a62007-10-20 20:23:07 +00003057/* do_syscall() should always have a single exit point at the end so
3058 that actions, such as logging of syscall results, can be performed.
3059 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00003060abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
3061 abi_long arg2, abi_long arg3, abi_long arg4,
3062 abi_long arg5, abi_long arg6)
bellard31e31b82003-02-18 22:55:36 +00003063{
blueswir1992f48a2007-10-14 16:27:31 +00003064 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00003065 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00003066 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00003067 void *p;
ths3b46e622007-09-17 08:09:54 +00003068
bellard72f03902003-02-18 23:33:18 +00003069#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00003070 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00003071#endif
thsb92c47c2007-11-01 00:07:38 +00003072 if(do_strace)
3073 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
3074
bellard31e31b82003-02-18 22:55:36 +00003075 switch(num) {
3076 case TARGET_NR_exit:
bellard7d132992003-03-06 23:23:54 +00003077#ifdef HAVE_GPROF
3078 _mcleanup();
3079#endif
bellarde9009672005-04-26 20:42:36 +00003080 gdb_exit(cpu_env, arg1);
bellard1b6b0292003-03-22 17:31:38 +00003081 /* XXX: should free thread stack and CPU env */
bellard31e31b82003-02-18 22:55:36 +00003082 _exit(arg1);
3083 ret = 0; /* avoid warning */
3084 break;
3085 case TARGET_NR_read:
bellard579a97f2007-11-11 14:26:47 +00003086 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
3087 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003088 ret = get_errno(read(arg1, p, arg3));
3089 unlock_user(p, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00003090 break;
3091 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00003092 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
3093 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003094 ret = get_errno(write(arg1, p, arg3));
3095 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00003096 break;
3097 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00003098 if (!(p = lock_user_string(arg1)))
3099 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003100 ret = get_errno(open(path(p),
bellardffa65c32004-01-04 23:57:22 +00003101 target_to_host_bitmask(arg2, fcntl_flags_tbl),
3102 arg3));
pbrook53a59602006-03-25 19:31:22 +00003103 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003104 break;
ths82424832007-09-24 09:21:55 +00003105#if defined(TARGET_NR_openat) && defined(__NR_openat)
3106 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00003107 if (!(p = lock_user_string(arg2)))
3108 goto efault;
3109 ret = get_errno(sys_openat(arg1,
3110 path(p),
3111 target_to_host_bitmask(arg3, fcntl_flags_tbl),
3112 arg4));
3113 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00003114 break;
3115#endif
bellard31e31b82003-02-18 22:55:36 +00003116 case TARGET_NR_close:
3117 ret = get_errno(close(arg1));
3118 break;
3119 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00003120 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00003121 break;
3122 case TARGET_NR_fork:
bellard1b6b0292003-03-22 17:31:38 +00003123 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00003124 break;
thse5febef2007-04-01 18:31:35 +00003125#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00003126 case TARGET_NR_waitpid:
3127 {
pbrook53a59602006-03-25 19:31:22 +00003128 int status;
3129 ret = get_errno(waitpid(arg1, &status, arg3));
bellard2f619692007-11-16 10:46:05 +00003130 if (!is_error(ret) && arg2
3131 && put_user_s32(status, arg2))
3132 goto efault;
bellard31e31b82003-02-18 22:55:36 +00003133 }
3134 break;
thse5febef2007-04-01 18:31:35 +00003135#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003136#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003137 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00003138 if (!(p = lock_user_string(arg1)))
3139 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003140 ret = get_errno(creat(p, arg2));
3141 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003142 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003143#endif
bellard31e31b82003-02-18 22:55:36 +00003144 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00003145 {
3146 void * p2;
3147 p = lock_user_string(arg1);
3148 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00003149 if (!p || !p2)
3150 ret = -TARGET_EFAULT;
3151 else
3152 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00003153 unlock_user(p2, arg2, 0);
3154 unlock_user(p, arg1, 0);
3155 }
bellard31e31b82003-02-18 22:55:36 +00003156 break;
ths64f0ce42007-09-24 09:25:06 +00003157#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
3158 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00003159 {
3160 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00003161 if (!arg2 || !arg4)
3162 goto efault;
ths64f0ce42007-09-24 09:25:06 +00003163 p = lock_user_string(arg2);
3164 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00003165 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00003166 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00003167 else
3168 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00003169 unlock_user(p, arg2, 0);
3170 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00003171 }
3172 break;
3173#endif
bellard31e31b82003-02-18 22:55:36 +00003174 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00003175 if (!(p = lock_user_string(arg1)))
3176 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003177 ret = get_errno(unlink(p));
3178 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003179 break;
ths8170f562007-09-24 09:24:11 +00003180#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
3181 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00003182 if (!(p = lock_user_string(arg2)))
3183 goto efault;
3184 ret = get_errno(sys_unlinkat(arg1, p, arg3));
3185 unlock_user(p, arg2, 0);
balroged494d82007-12-11 23:23:52 +00003186 break;
balrogb7d35e62007-12-12 00:40:24 +00003187#endif
bellard31e31b82003-02-18 22:55:36 +00003188 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00003189 {
3190 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00003191 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00003192 abi_ulong gp;
3193 abi_ulong guest_argp;
3194 abi_ulong guest_envp;
3195 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00003196 char **q;
3197
bellardf7341ff2003-03-30 21:00:25 +00003198 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00003199 guest_argp = arg2;
ths982f3ab2007-12-09 23:12:55 +00003200 for (gp = guest_argp; ; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00003201 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00003202 goto efault;
ths03aa1972007-12-02 06:28:08 +00003203 if (!addr)
bellard2f619692007-11-16 10:46:05 +00003204 break;
bellard7854b052003-03-29 17:22:23 +00003205 argc++;
bellard2f619692007-11-16 10:46:05 +00003206 }
bellardf7341ff2003-03-30 21:00:25 +00003207 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00003208 guest_envp = arg3;
ths982f3ab2007-12-09 23:12:55 +00003209 for (gp = guest_envp; ; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00003210 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00003211 goto efault;
ths03aa1972007-12-02 06:28:08 +00003212 if (!addr)
bellard2f619692007-11-16 10:46:05 +00003213 break;
bellard7854b052003-03-29 17:22:23 +00003214 envc++;
bellard2f619692007-11-16 10:46:05 +00003215 }
bellard7854b052003-03-29 17:22:23 +00003216
bellardf7341ff2003-03-30 21:00:25 +00003217 argp = alloca((argc + 1) * sizeof(void *));
3218 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00003219
pbrook53a59602006-03-25 19:31:22 +00003220 for (gp = guest_argp, q = argp; ;
blueswir1992f48a2007-10-14 16:27:31 +00003221 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00003222 if (get_user_ual(addr, gp))
3223 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003224 if (!addr)
3225 break;
bellard2f619692007-11-16 10:46:05 +00003226 if (!(*q = lock_user_string(addr)))
3227 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003228 }
bellardf7341ff2003-03-30 21:00:25 +00003229 *q = NULL;
3230
pbrook53a59602006-03-25 19:31:22 +00003231 for (gp = guest_envp, q = envp; ;
blueswir1992f48a2007-10-14 16:27:31 +00003232 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00003233 if (get_user_ual(addr, gp))
3234 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003235 if (!addr)
3236 break;
bellard2f619692007-11-16 10:46:05 +00003237 if (!(*q = lock_user_string(addr)))
3238 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003239 }
bellardf7341ff2003-03-30 21:00:25 +00003240 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00003241
bellard2f619692007-11-16 10:46:05 +00003242 if (!(p = lock_user_string(arg1)))
3243 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003244 ret = get_errno(execve(p, argp, envp));
3245 unlock_user(p, arg1, 0);
3246
bellard2f619692007-11-16 10:46:05 +00003247 goto execve_end;
3248
3249 execve_efault:
3250 ret = -TARGET_EFAULT;
3251
3252 execve_end:
pbrook53a59602006-03-25 19:31:22 +00003253 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00003254 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00003255 if (get_user_ual(addr, gp)
3256 || !addr)
3257 break;
pbrook53a59602006-03-25 19:31:22 +00003258 unlock_user(*q, addr, 0);
3259 }
3260 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00003261 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00003262 if (get_user_ual(addr, gp)
3263 || !addr)
3264 break;
pbrook53a59602006-03-25 19:31:22 +00003265 unlock_user(*q, addr, 0);
3266 }
bellard7854b052003-03-29 17:22:23 +00003267 }
bellard31e31b82003-02-18 22:55:36 +00003268 break;
3269 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00003270 if (!(p = lock_user_string(arg1)))
3271 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003272 ret = get_errno(chdir(p));
3273 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003274 break;
bellarda315a142005-01-30 22:59:18 +00003275#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00003276 case TARGET_NR_time:
3277 {
pbrook53a59602006-03-25 19:31:22 +00003278 time_t host_time;
3279 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00003280 if (!is_error(ret)
3281 && arg1
3282 && put_user_sal(host_time, arg1))
3283 goto efault;
bellard31e31b82003-02-18 22:55:36 +00003284 }
3285 break;
bellarda315a142005-01-30 22:59:18 +00003286#endif
bellard31e31b82003-02-18 22:55:36 +00003287 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00003288 if (!(p = lock_user_string(arg1)))
3289 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003290 ret = get_errno(mknod(p, arg2, arg3));
3291 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003292 break;
ths75ac37a2007-09-24 09:23:05 +00003293#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
3294 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00003295 if (!(p = lock_user_string(arg2)))
3296 goto efault;
3297 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
3298 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00003299 break;
3300#endif
bellard31e31b82003-02-18 22:55:36 +00003301 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00003302 if (!(p = lock_user_string(arg1)))
3303 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003304 ret = get_errno(chmod(p, arg2));
3305 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003306 break;
bellardebc05482003-09-30 21:08:41 +00003307#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00003308 case TARGET_NR_break:
3309 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003310#endif
3311#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00003312 case TARGET_NR_oldstat:
3313 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003314#endif
bellard31e31b82003-02-18 22:55:36 +00003315 case TARGET_NR_lseek:
3316 ret = get_errno(lseek(arg1, arg2, arg3));
3317 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003318#ifdef TARGET_NR_getxpid
3319 case TARGET_NR_getxpid:
3320#else
bellard31e31b82003-02-18 22:55:36 +00003321 case TARGET_NR_getpid:
j_mayer7a3148a2007-04-05 07:13:51 +00003322#endif
bellard31e31b82003-02-18 22:55:36 +00003323 ret = get_errno(getpid());
3324 break;
3325 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00003326 {
3327 /* need to look at the data field */
3328 void *p2, *p3;
3329 p = lock_user_string(arg1);
3330 p2 = lock_user_string(arg2);
3331 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00003332 if (!p || !p2 || !p3)
3333 ret = -TARGET_EFAULT;
3334 else
3335 /* FIXME - arg5 should be locked, but it isn't clear how to
3336 * do that since it's not guaranteed to be a NULL-terminated
3337 * string.
3338 */
3339 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
3340 unlock_user(p, arg1, 0);
3341 unlock_user(p2, arg2, 0);
3342 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00003343 break;
3344 }
thse5febef2007-04-01 18:31:35 +00003345#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00003346 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00003347 if (!(p = lock_user_string(arg1)))
3348 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003349 ret = get_errno(umount(p));
3350 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003351 break;
thse5febef2007-04-01 18:31:35 +00003352#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003353#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003354 case TARGET_NR_stime:
3355 {
pbrook53a59602006-03-25 19:31:22 +00003356 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00003357 if (get_user_sal(host_time, arg1))
3358 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003359 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00003360 }
3361 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003362#endif
bellard31e31b82003-02-18 22:55:36 +00003363 case TARGET_NR_ptrace:
3364 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00003365#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003366 case TARGET_NR_alarm:
3367 ret = alarm(arg1);
3368 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003369#endif
bellardebc05482003-09-30 21:08:41 +00003370#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00003371 case TARGET_NR_oldfstat:
3372 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003373#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003374#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003375 case TARGET_NR_pause:
3376 ret = get_errno(pause());
3377 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003378#endif
thse5febef2007-04-01 18:31:35 +00003379#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00003380 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00003381 {
pbrook53a59602006-03-25 19:31:22 +00003382 struct utimbuf tbuf, *host_tbuf;
3383 struct target_utimbuf *target_tbuf;
3384 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00003385 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
3386 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003387 tbuf.actime = tswapl(target_tbuf->actime);
3388 tbuf.modtime = tswapl(target_tbuf->modtime);
3389 unlock_user_struct(target_tbuf, arg2, 0);
3390 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00003391 } else {
pbrook53a59602006-03-25 19:31:22 +00003392 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00003393 }
bellard579a97f2007-11-11 14:26:47 +00003394 if (!(p = lock_user_string(arg1)))
3395 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003396 ret = get_errno(utime(p, host_tbuf));
3397 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00003398 }
3399 break;
thse5febef2007-04-01 18:31:35 +00003400#endif
bellard978a66f2004-12-06 22:58:05 +00003401 case TARGET_NR_utimes:
3402 {
bellard978a66f2004-12-06 22:58:05 +00003403 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00003404 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00003405 if (copy_from_user_timeval(&tv[0], arg2)
3406 || copy_from_user_timeval(&tv[1],
3407 arg2 + sizeof(struct target_timeval)))
3408 goto efault;
bellard978a66f2004-12-06 22:58:05 +00003409 tvp = tv;
3410 } else {
3411 tvp = NULL;
3412 }
bellard579a97f2007-11-11 14:26:47 +00003413 if (!(p = lock_user_string(arg1)))
3414 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003415 ret = get_errno(utimes(p, tvp));
3416 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00003417 }
3418 break;
bellardebc05482003-09-30 21:08:41 +00003419#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00003420 case TARGET_NR_stty:
3421 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003422#endif
3423#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00003424 case TARGET_NR_gtty:
3425 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003426#endif
bellard31e31b82003-02-18 22:55:36 +00003427 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00003428 if (!(p = lock_user_string(arg1)))
3429 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003430 ret = get_errno(access(p, arg2));
3431 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003432 break;
ths92a34c12007-09-24 09:27:49 +00003433#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
3434 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00003435 if (!(p = lock_user_string(arg2)))
3436 goto efault;
3437 ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
3438 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00003439 break;
3440#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003441#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003442 case TARGET_NR_nice:
3443 ret = get_errno(nice(arg1));
3444 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003445#endif
bellardebc05482003-09-30 21:08:41 +00003446#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00003447 case TARGET_NR_ftime:
3448 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003449#endif
bellard31e31b82003-02-18 22:55:36 +00003450 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00003451 sync();
3452 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00003453 break;
3454 case TARGET_NR_kill:
3455 ret = get_errno(kill(arg1, arg2));
3456 break;
3457 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00003458 {
3459 void *p2;
3460 p = lock_user_string(arg1);
3461 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00003462 if (!p || !p2)
3463 ret = -TARGET_EFAULT;
3464 else
3465 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00003466 unlock_user(p2, arg2, 0);
3467 unlock_user(p, arg1, 0);
3468 }
bellard31e31b82003-02-18 22:55:36 +00003469 break;
ths722183f2007-09-24 09:24:37 +00003470#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
3471 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00003472 {
bellard579a97f2007-11-11 14:26:47 +00003473 void *p2;
ths722183f2007-09-24 09:24:37 +00003474 p = lock_user_string(arg2);
3475 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00003476 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00003477 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00003478 else
3479 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00003480 unlock_user(p2, arg4, 0);
3481 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00003482 }
3483 break;
3484#endif
bellard31e31b82003-02-18 22:55:36 +00003485 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00003486 if (!(p = lock_user_string(arg1)))
3487 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003488 ret = get_errno(mkdir(p, arg2));
3489 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003490 break;
ths4472ad02007-09-24 09:22:32 +00003491#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
3492 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00003493 if (!(p = lock_user_string(arg2)))
3494 goto efault;
3495 ret = get_errno(sys_mkdirat(arg1, p, arg3));
3496 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00003497 break;
3498#endif
bellard31e31b82003-02-18 22:55:36 +00003499 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00003500 if (!(p = lock_user_string(arg1)))
3501 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003502 ret = get_errno(rmdir(p));
3503 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003504 break;
3505 case TARGET_NR_dup:
3506 ret = get_errno(dup(arg1));
3507 break;
3508 case TARGET_NR_pipe:
3509 {
pbrook53a59602006-03-25 19:31:22 +00003510 int host_pipe[2];
3511 ret = get_errno(pipe(host_pipe));
bellard31e31b82003-02-18 22:55:36 +00003512 if (!is_error(ret)) {
thsc12ab052007-06-01 11:50:36 +00003513#if defined(TARGET_MIPS)
thsead93602007-09-06 00:18:15 +00003514 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
thsd0dc7dc2008-02-12 21:01:26 +00003515 env->gpr[env->current_tc][3] = host_pipe[1];
thsc12ab052007-06-01 11:50:36 +00003516 ret = host_pipe[0];
aurel32b5eff352008-03-11 23:30:22 +00003517#elif defined(TARGET_SH4)
3518 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
3519 ret = host_pipe[0];
thsc12ab052007-06-01 11:50:36 +00003520#else
bellard2f619692007-11-16 10:46:05 +00003521 if (put_user_s32(host_pipe[0], arg1)
3522 || put_user_s32(host_pipe[1], arg1 + sizeof(host_pipe[0])))
3523 goto efault;
thsc12ab052007-06-01 11:50:36 +00003524#endif
bellard31e31b82003-02-18 22:55:36 +00003525 }
3526 }
3527 break;
3528 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00003529 {
pbrook53a59602006-03-25 19:31:22 +00003530 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00003531 struct tms tms;
3532 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00003533 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00003534 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
3535 if (!tmsp)
3536 goto efault;
bellardc596ed12003-07-13 17:32:31 +00003537 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3538 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3539 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3540 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00003541 }
bellardc596ed12003-07-13 17:32:31 +00003542 if (!is_error(ret))
3543 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00003544 }
3545 break;
bellardebc05482003-09-30 21:08:41 +00003546#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00003547 case TARGET_NR_prof:
3548 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003549#endif
thse5febef2007-04-01 18:31:35 +00003550#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00003551 case TARGET_NR_signal:
3552 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003553#endif
bellard31e31b82003-02-18 22:55:36 +00003554 case TARGET_NR_acct:
bellard579a97f2007-11-11 14:26:47 +00003555 if (!(p = lock_user_string(arg1)))
3556 goto efault;
pbrook24836682006-04-16 14:14:53 +00003557 ret = get_errno(acct(path(p)));
3558 unlock_user(p, arg1, 0);
3559 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003560#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003561 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00003562 if (!(p = lock_user_string(arg1)))
3563 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003564 ret = get_errno(umount2(p, arg2));
3565 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003566 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003567#endif
bellardebc05482003-09-30 21:08:41 +00003568#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00003569 case TARGET_NR_lock:
3570 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003571#endif
bellard31e31b82003-02-18 22:55:36 +00003572 case TARGET_NR_ioctl:
3573 ret = do_ioctl(arg1, arg2, arg3);
3574 break;
3575 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00003576 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00003577 break;
bellardebc05482003-09-30 21:08:41 +00003578#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00003579 case TARGET_NR_mpx:
3580 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003581#endif
bellard31e31b82003-02-18 22:55:36 +00003582 case TARGET_NR_setpgid:
3583 ret = get_errno(setpgid(arg1, arg2));
3584 break;
bellardebc05482003-09-30 21:08:41 +00003585#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00003586 case TARGET_NR_ulimit:
3587 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003588#endif
3589#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00003590 case TARGET_NR_oldolduname:
3591 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003592#endif
bellard31e31b82003-02-18 22:55:36 +00003593 case TARGET_NR_umask:
3594 ret = get_errno(umask(arg1));
3595 break;
3596 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00003597 if (!(p = lock_user_string(arg1)))
3598 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003599 ret = get_errno(chroot(p));
3600 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003601 break;
3602 case TARGET_NR_ustat:
3603 goto unimplemented;
3604 case TARGET_NR_dup2:
3605 ret = get_errno(dup2(arg1, arg2));
3606 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003607#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003608 case TARGET_NR_getppid:
3609 ret = get_errno(getppid());
3610 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003611#endif
bellard31e31b82003-02-18 22:55:36 +00003612 case TARGET_NR_getpgrp:
3613 ret = get_errno(getpgrp());
3614 break;
3615 case TARGET_NR_setsid:
3616 ret = get_errno(setsid());
3617 break;
thse5febef2007-04-01 18:31:35 +00003618#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00003619 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00003620 {
ths388bb212007-05-13 13:58:00 +00003621#if !defined(TARGET_MIPS)
pbrook53a59602006-03-25 19:31:22 +00003622 struct target_old_sigaction *old_act;
bellard66fb9762003-03-23 01:06:05 +00003623 struct target_sigaction act, oact, *pact;
pbrook53a59602006-03-25 19:31:22 +00003624 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00003625 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3626 goto efault;
bellard66fb9762003-03-23 01:06:05 +00003627 act._sa_handler = old_act->_sa_handler;
3628 target_siginitset(&act.sa_mask, old_act->sa_mask);
3629 act.sa_flags = old_act->sa_flags;
3630 act.sa_restorer = old_act->sa_restorer;
pbrook53a59602006-03-25 19:31:22 +00003631 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003632 pact = &act;
3633 } else {
3634 pact = NULL;
3635 }
3636 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00003637 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003638 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3639 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003640 old_act->_sa_handler = oact._sa_handler;
3641 old_act->sa_mask = oact.sa_mask.sig[0];
3642 old_act->sa_flags = oact.sa_flags;
3643 old_act->sa_restorer = oact.sa_restorer;
3644 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00003645 }
ths388bb212007-05-13 13:58:00 +00003646#else
bellard106ec872006-06-27 21:08:10 +00003647 struct target_sigaction act, oact, *pact, *old_act;
3648
3649 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00003650 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3651 goto efault;
bellard106ec872006-06-27 21:08:10 +00003652 act._sa_handler = old_act->_sa_handler;
3653 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3654 act.sa_flags = old_act->sa_flags;
3655 unlock_user_struct(old_act, arg2, 0);
3656 pact = &act;
3657 } else {
3658 pact = NULL;
3659 }
3660
3661 ret = get_errno(do_sigaction(arg1, pact, &oact));
3662
3663 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003664 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3665 goto efault;
bellard106ec872006-06-27 21:08:10 +00003666 old_act->_sa_handler = oact._sa_handler;
3667 old_act->sa_flags = oact.sa_flags;
3668 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3669 old_act->sa_mask.sig[1] = 0;
3670 old_act->sa_mask.sig[2] = 0;
3671 old_act->sa_mask.sig[3] = 0;
3672 unlock_user_struct(old_act, arg3, 1);
3673 }
ths388bb212007-05-13 13:58:00 +00003674#endif
bellard31e31b82003-02-18 22:55:36 +00003675 }
3676 break;
thse5febef2007-04-01 18:31:35 +00003677#endif
bellard66fb9762003-03-23 01:06:05 +00003678 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00003679 {
3680 struct target_sigaction *act;
3681 struct target_sigaction *oact;
3682
bellard579a97f2007-11-11 14:26:47 +00003683 if (arg2) {
3684 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
3685 goto efault;
3686 } else
pbrook53a59602006-03-25 19:31:22 +00003687 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00003688 if (arg3) {
3689 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
3690 ret = -TARGET_EFAULT;
3691 goto rt_sigaction_fail;
3692 }
3693 } else
pbrook53a59602006-03-25 19:31:22 +00003694 oact = NULL;
3695 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00003696 rt_sigaction_fail:
3697 if (act)
pbrook53a59602006-03-25 19:31:22 +00003698 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00003699 if (oact)
pbrook53a59602006-03-25 19:31:22 +00003700 unlock_user_struct(oact, arg3, 1);
3701 }
bellard66fb9762003-03-23 01:06:05 +00003702 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003703#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003704 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00003705 {
3706 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00003707 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00003708 sigprocmask(0, NULL, &cur_set);
3709 host_to_target_old_sigset(&target_set, &cur_set);
3710 ret = target_set;
3711 }
3712 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003713#endif
3714#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003715 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00003716 {
3717 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00003718 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00003719 sigprocmask(0, NULL, &cur_set);
3720 target_to_host_old_sigset(&set, &target_set);
3721 sigorset(&set, &set, &cur_set);
3722 sigprocmask(SIG_SETMASK, &set, &oset);
3723 host_to_target_old_sigset(&target_set, &oset);
3724 ret = target_set;
3725 }
3726 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003727#endif
thse5febef2007-04-01 18:31:35 +00003728#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00003729 case TARGET_NR_sigprocmask:
3730 {
3731 int how = arg1;
3732 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003733
pbrook53a59602006-03-25 19:31:22 +00003734 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003735 switch(how) {
3736 case TARGET_SIG_BLOCK:
3737 how = SIG_BLOCK;
3738 break;
3739 case TARGET_SIG_UNBLOCK:
3740 how = SIG_UNBLOCK;
3741 break;
3742 case TARGET_SIG_SETMASK:
3743 how = SIG_SETMASK;
3744 break;
3745 default:
ths0da46a62007-10-20 20:23:07 +00003746 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00003747 goto fail;
3748 }
bellard579a97f2007-11-11 14:26:47 +00003749 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3750 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003751 target_to_host_old_sigset(&set, p);
3752 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003753 set_ptr = &set;
3754 } else {
3755 how = 0;
3756 set_ptr = NULL;
3757 }
3758 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003759 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003760 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3761 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003762 host_to_target_old_sigset(p, &oldset);
3763 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003764 }
3765 }
3766 break;
thse5febef2007-04-01 18:31:35 +00003767#endif
bellard66fb9762003-03-23 01:06:05 +00003768 case TARGET_NR_rt_sigprocmask:
3769 {
3770 int how = arg1;
3771 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003772
pbrook53a59602006-03-25 19:31:22 +00003773 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003774 switch(how) {
3775 case TARGET_SIG_BLOCK:
3776 how = SIG_BLOCK;
3777 break;
3778 case TARGET_SIG_UNBLOCK:
3779 how = SIG_UNBLOCK;
3780 break;
3781 case TARGET_SIG_SETMASK:
3782 how = SIG_SETMASK;
3783 break;
3784 default:
ths0da46a62007-10-20 20:23:07 +00003785 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00003786 goto fail;
3787 }
bellard579a97f2007-11-11 14:26:47 +00003788 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3789 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003790 target_to_host_sigset(&set, p);
3791 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003792 set_ptr = &set;
3793 } else {
3794 how = 0;
3795 set_ptr = NULL;
3796 }
3797 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003798 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003799 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3800 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003801 host_to_target_sigset(p, &oldset);
3802 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003803 }
3804 }
3805 break;
thse5febef2007-04-01 18:31:35 +00003806#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00003807 case TARGET_NR_sigpending:
3808 {
3809 sigset_t set;
3810 ret = get_errno(sigpending(&set));
3811 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003812 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3813 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003814 host_to_target_old_sigset(p, &set);
3815 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003816 }
3817 }
3818 break;
thse5febef2007-04-01 18:31:35 +00003819#endif
bellard66fb9762003-03-23 01:06:05 +00003820 case TARGET_NR_rt_sigpending:
3821 {
3822 sigset_t set;
3823 ret = get_errno(sigpending(&set));
3824 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003825 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3826 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003827 host_to_target_sigset(p, &set);
3828 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003829 }
3830 }
3831 break;
thse5febef2007-04-01 18:31:35 +00003832#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00003833 case TARGET_NR_sigsuspend:
3834 {
3835 sigset_t set;
bellard579a97f2007-11-11 14:26:47 +00003836 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3837 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003838 target_to_host_old_sigset(&set, p);
3839 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003840 ret = get_errno(sigsuspend(&set));
3841 }
3842 break;
thse5febef2007-04-01 18:31:35 +00003843#endif
bellard66fb9762003-03-23 01:06:05 +00003844 case TARGET_NR_rt_sigsuspend:
3845 {
3846 sigset_t set;
bellard579a97f2007-11-11 14:26:47 +00003847 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3848 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003849 target_to_host_sigset(&set, p);
3850 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003851 ret = get_errno(sigsuspend(&set));
3852 }
3853 break;
3854 case TARGET_NR_rt_sigtimedwait:
3855 {
bellard66fb9762003-03-23 01:06:05 +00003856 sigset_t set;
3857 struct timespec uts, *puts;
3858 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00003859
bellard579a97f2007-11-11 14:26:47 +00003860 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3861 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003862 target_to_host_sigset(&set, p);
3863 unlock_user(p, arg1, 0);
3864 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00003865 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00003866 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00003867 } else {
3868 puts = NULL;
3869 }
3870 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00003871 if (!is_error(ret) && arg2) {
bellard579a97f2007-11-11 14:26:47 +00003872 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_sigset_t), 0)))
3873 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003874 host_to_target_siginfo(p, &uinfo);
3875 unlock_user(p, arg2, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003876 }
3877 }
3878 break;
3879 case TARGET_NR_rt_sigqueueinfo:
3880 {
3881 siginfo_t uinfo;
bellard579a97f2007-11-11 14:26:47 +00003882 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
3883 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003884 target_to_host_siginfo(&uinfo, p);
3885 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003886 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3887 }
3888 break;
thse5febef2007-04-01 18:31:35 +00003889#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00003890 case TARGET_NR_sigreturn:
3891 /* NOTE: ret is eax, so not transcoding must be done */
3892 ret = do_sigreturn(cpu_env);
3893 break;
thse5febef2007-04-01 18:31:35 +00003894#endif
bellard66fb9762003-03-23 01:06:05 +00003895 case TARGET_NR_rt_sigreturn:
3896 /* NOTE: ret is eax, so not transcoding must be done */
3897 ret = do_rt_sigreturn(cpu_env);
3898 break;
bellard31e31b82003-02-18 22:55:36 +00003899 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00003900 if (!(p = lock_user_string(arg1)))
3901 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003902 ret = get_errno(sethostname(p, arg2));
3903 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003904 break;
3905 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00003906 {
3907 /* XXX: convert resource ? */
3908 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003909 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003910 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00003911 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
3912 goto efault;
bellard9de5e442003-03-23 16:49:39 +00003913 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3914 rlim.rlim_max = tswapl(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00003915 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00003916 ret = get_errno(setrlimit(resource, &rlim));
3917 }
3918 break;
bellard31e31b82003-02-18 22:55:36 +00003919 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00003920 {
3921 /* XXX: convert resource ? */
3922 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003923 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003924 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00003925
bellard9de5e442003-03-23 16:49:39 +00003926 ret = get_errno(getrlimit(resource, &rlim));
3927 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003928 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
3929 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003930 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3931 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3932 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00003933 }
3934 }
3935 break;
bellard31e31b82003-02-18 22:55:36 +00003936 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00003937 {
3938 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00003939 ret = get_errno(getrusage(arg1, &rusage));
3940 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003941 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00003942 }
3943 }
3944 break;
bellard31e31b82003-02-18 22:55:36 +00003945 case TARGET_NR_gettimeofday:
3946 {
bellard31e31b82003-02-18 22:55:36 +00003947 struct timeval tv;
3948 ret = get_errno(gettimeofday(&tv, NULL));
3949 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00003950 if (copy_to_user_timeval(arg1, &tv))
3951 goto efault;
bellard31e31b82003-02-18 22:55:36 +00003952 }
3953 }
3954 break;
3955 case TARGET_NR_settimeofday:
3956 {
bellard31e31b82003-02-18 22:55:36 +00003957 struct timeval tv;
ths788f5ec2007-12-09 02:37:05 +00003958 if (copy_from_user_timeval(&tv, arg1))
3959 goto efault;
bellard31e31b82003-02-18 22:55:36 +00003960 ret = get_errno(settimeofday(&tv, NULL));
3961 }
3962 break;
bellard048f6b42005-11-26 18:47:20 +00003963#ifdef TARGET_NR_select
bellard31e31b82003-02-18 22:55:36 +00003964 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00003965 {
pbrook53a59602006-03-25 19:31:22 +00003966 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00003967 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00003968 long nsel;
3969
bellard579a97f2007-11-11 14:26:47 +00003970 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
3971 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003972 nsel = tswapl(sel->n);
3973 inp = tswapl(sel->inp);
3974 outp = tswapl(sel->outp);
3975 exp = tswapl(sel->exp);
3976 tvp = tswapl(sel->tvp);
3977 unlock_user_struct(sel, arg1, 0);
3978 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00003979 }
3980 break;
bellard048f6b42005-11-26 18:47:20 +00003981#endif
bellard31e31b82003-02-18 22:55:36 +00003982 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00003983 {
3984 void *p2;
3985 p = lock_user_string(arg1);
3986 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00003987 if (!p || !p2)
3988 ret = -TARGET_EFAULT;
3989 else
3990 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00003991 unlock_user(p2, arg2, 0);
3992 unlock_user(p, arg1, 0);
3993 }
bellard31e31b82003-02-18 22:55:36 +00003994 break;
thsf0b62432007-09-24 09:25:40 +00003995#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
3996 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00003997 {
bellard579a97f2007-11-11 14:26:47 +00003998 void *p2;
thsf0b62432007-09-24 09:25:40 +00003999 p = lock_user_string(arg1);
4000 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00004001 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004002 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00004003 else
4004 ret = get_errno(sys_symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00004005 unlock_user(p2, arg3, 0);
4006 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00004007 }
4008 break;
4009#endif
bellardebc05482003-09-30 21:08:41 +00004010#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00004011 case TARGET_NR_oldlstat:
4012 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004013#endif
bellard31e31b82003-02-18 22:55:36 +00004014 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00004015 {
4016 void *p2;
4017 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00004018 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
4019 if (!p || !p2)
4020 ret = -TARGET_EFAULT;
4021 else
4022 ret = get_errno(readlink(path(p), p2, arg3));
pbrook53a59602006-03-25 19:31:22 +00004023 unlock_user(p2, arg2, ret);
4024 unlock_user(p, arg1, 0);
4025 }
bellard31e31b82003-02-18 22:55:36 +00004026 break;
ths5e0ccb12007-09-24 09:26:10 +00004027#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
4028 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00004029 {
bellard579a97f2007-11-11 14:26:47 +00004030 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00004031 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004032 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
4033 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004034 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00004035 else
4036 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
bellard579a97f2007-11-11 14:26:47 +00004037 unlock_user(p2, arg3, ret);
4038 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00004039 }
4040 break;
4041#endif
thse5febef2007-04-01 18:31:35 +00004042#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00004043 case TARGET_NR_uselib:
4044 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004045#endif
4046#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00004047 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00004048 if (!(p = lock_user_string(arg1)))
4049 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004050 ret = get_errno(swapon(p, arg2));
4051 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004052 break;
thse5febef2007-04-01 18:31:35 +00004053#endif
bellard31e31b82003-02-18 22:55:36 +00004054 case TARGET_NR_reboot:
4055 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004056#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00004057 case TARGET_NR_readdir:
4058 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004059#endif
4060#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00004061 case TARGET_NR_mmap:
bellardd2fd1af2007-11-14 18:08:56 +00004062#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
bellard31e31b82003-02-18 22:55:36 +00004063 {
blueswir1992f48a2007-10-14 16:27:31 +00004064 abi_ulong *v;
4065 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00004066 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
4067 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004068 v1 = tswapl(v[0]);
4069 v2 = tswapl(v[1]);
4070 v3 = tswapl(v[2]);
4071 v4 = tswapl(v[3]);
4072 v5 = tswapl(v[4]);
4073 v6 = tswapl(v[5]);
4074 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00004075 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00004076 target_to_host_bitmask(v4, mmap_flags_tbl),
4077 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00004078 }
bellard31e31b82003-02-18 22:55:36 +00004079#else
ths5fafdf22007-09-16 21:08:06 +00004080 ret = get_errno(target_mmap(arg1, arg2, arg3,
4081 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00004082 arg5,
4083 arg6));
bellard31e31b82003-02-18 22:55:36 +00004084#endif
bellard6fb883e2003-07-09 17:12:39 +00004085 break;
thse5febef2007-04-01 18:31:35 +00004086#endif
bellarda315a142005-01-30 22:59:18 +00004087#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00004088 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00004089#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00004090#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +00004091#endif
ths5fafdf22007-09-16 21:08:06 +00004092 ret = get_errno(target_mmap(arg1, arg2, arg3,
4093 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00004094 arg5,
bellardc573ff62004-01-04 15:51:36 +00004095 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00004096 break;
bellarda315a142005-01-30 22:59:18 +00004097#endif
bellard31e31b82003-02-18 22:55:36 +00004098 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00004099 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00004100 break;
bellard9de5e442003-03-23 16:49:39 +00004101 case TARGET_NR_mprotect:
bellard54936002003-05-13 00:25:15 +00004102 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00004103 break;
thse5febef2007-04-01 18:31:35 +00004104#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00004105 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00004106 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00004107 break;
thse5febef2007-04-01 18:31:35 +00004108#endif
pbrook53a59602006-03-25 19:31:22 +00004109 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00004110#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00004111 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00004112 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00004113 break;
thse5febef2007-04-01 18:31:35 +00004114#endif
4115#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00004116 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00004117 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00004118 break;
thse5febef2007-04-01 18:31:35 +00004119#endif
4120#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00004121 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00004122 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00004123 break;
thse5febef2007-04-01 18:31:35 +00004124#endif
4125#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00004126 case TARGET_NR_mlockall:
4127 ret = get_errno(mlockall(arg1));
4128 break;
thse5febef2007-04-01 18:31:35 +00004129#endif
4130#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00004131 case TARGET_NR_munlockall:
4132 ret = get_errno(munlockall());
4133 break;
thse5febef2007-04-01 18:31:35 +00004134#endif
bellard31e31b82003-02-18 22:55:36 +00004135 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00004136 if (!(p = lock_user_string(arg1)))
4137 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004138 ret = get_errno(truncate(p, arg2));
4139 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004140 break;
4141 case TARGET_NR_ftruncate:
4142 ret = get_errno(ftruncate(arg1, arg2));
4143 break;
4144 case TARGET_NR_fchmod:
4145 ret = get_errno(fchmod(arg1, arg2));
4146 break;
ths814d7972007-09-24 09:26:51 +00004147#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
4148 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00004149 if (!(p = lock_user_string(arg2)))
4150 goto efault;
4151 ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
4152 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00004153 break;
4154#endif
bellard31e31b82003-02-18 22:55:36 +00004155 case TARGET_NR_getpriority:
thsc6cda172007-10-09 03:42:34 +00004156 /* libc does special remapping of the return value of
4157 * sys_getpriority() so it's just easiest to call
4158 * sys_getpriority() directly rather than through libc. */
4159 ret = sys_getpriority(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00004160 break;
4161 case TARGET_NR_setpriority:
4162 ret = get_errno(setpriority(arg1, arg2, arg3));
4163 break;
bellardebc05482003-09-30 21:08:41 +00004164#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00004165 case TARGET_NR_profil:
4166 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004167#endif
bellard31e31b82003-02-18 22:55:36 +00004168 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00004169 if (!(p = lock_user_string(arg1)))
4170 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004171 ret = get_errno(statfs(path(p), &stfs));
4172 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004173 convert_statfs:
4174 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004175 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00004176
bellard579a97f2007-11-11 14:26:47 +00004177 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
4178 goto efault;
4179 __put_user(stfs.f_type, &target_stfs->f_type);
4180 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
4181 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
4182 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
4183 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
4184 __put_user(stfs.f_files, &target_stfs->f_files);
4185 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
4186 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
4187 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
4188 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00004189 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00004190 }
4191 break;
4192 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00004193 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00004194 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00004195#ifdef TARGET_NR_statfs64
4196 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00004197 if (!(p = lock_user_string(arg1)))
4198 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004199 ret = get_errno(statfs(path(p), &stfs));
4200 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00004201 convert_statfs64:
4202 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004203 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00004204
bellard579a97f2007-11-11 14:26:47 +00004205 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
4206 goto efault;
4207 __put_user(stfs.f_type, &target_stfs->f_type);
4208 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
4209 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
4210 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
4211 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
4212 __put_user(stfs.f_files, &target_stfs->f_files);
4213 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
4214 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
4215 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
4216 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
4217 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00004218 }
4219 break;
4220 case TARGET_NR_fstatfs64:
4221 ret = get_errno(fstatfs(arg1, &stfs));
4222 goto convert_statfs64;
4223#endif
bellardebc05482003-09-30 21:08:41 +00004224#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00004225 case TARGET_NR_ioperm:
4226 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004227#endif
thse5febef2007-04-01 18:31:35 +00004228#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00004229 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00004230 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00004231 break;
thse5febef2007-04-01 18:31:35 +00004232#endif
bellard3532fa72006-06-24 15:06:03 +00004233#ifdef TARGET_NR_accept
4234 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00004235 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00004236 break;
4237#endif
4238#ifdef TARGET_NR_bind
4239 case TARGET_NR_bind:
4240 ret = do_bind(arg1, arg2, arg3);
4241 break;
4242#endif
4243#ifdef TARGET_NR_connect
4244 case TARGET_NR_connect:
4245 ret = do_connect(arg1, arg2, arg3);
4246 break;
4247#endif
4248#ifdef TARGET_NR_getpeername
4249 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00004250 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00004251 break;
4252#endif
4253#ifdef TARGET_NR_getsockname
4254 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00004255 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00004256 break;
4257#endif
4258#ifdef TARGET_NR_getsockopt
4259 case TARGET_NR_getsockopt:
4260 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
4261 break;
4262#endif
4263#ifdef TARGET_NR_listen
4264 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00004265 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00004266 break;
4267#endif
4268#ifdef TARGET_NR_recv
4269 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00004270 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00004271 break;
4272#endif
4273#ifdef TARGET_NR_recvfrom
4274 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00004275 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00004276 break;
4277#endif
4278#ifdef TARGET_NR_recvmsg
4279 case TARGET_NR_recvmsg:
4280 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
4281 break;
4282#endif
4283#ifdef TARGET_NR_send
4284 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00004285 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00004286 break;
4287#endif
4288#ifdef TARGET_NR_sendmsg
4289 case TARGET_NR_sendmsg:
4290 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
4291 break;
4292#endif
4293#ifdef TARGET_NR_sendto
4294 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00004295 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00004296 break;
4297#endif
4298#ifdef TARGET_NR_shutdown
4299 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00004300 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00004301 break;
4302#endif
4303#ifdef TARGET_NR_socket
4304 case TARGET_NR_socket:
4305 ret = do_socket(arg1, arg2, arg3);
4306 break;
4307#endif
4308#ifdef TARGET_NR_socketpair
4309 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00004310 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00004311 break;
4312#endif
4313#ifdef TARGET_NR_setsockopt
4314 case TARGET_NR_setsockopt:
4315 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
4316 break;
4317#endif
ths7494b0f2007-02-11 18:26:53 +00004318
bellard31e31b82003-02-18 22:55:36 +00004319 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00004320 if (!(p = lock_user_string(arg2)))
4321 goto efault;
thse5574482007-02-11 20:03:13 +00004322 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
4323 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00004324 break;
4325
bellard31e31b82003-02-18 22:55:36 +00004326 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00004327 {
bellard66fb9762003-03-23 01:06:05 +00004328 struct itimerval value, ovalue, *pvalue;
4329
pbrook53a59602006-03-25 19:31:22 +00004330 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00004331 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00004332 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
4333 || copy_from_user_timeval(&pvalue->it_value,
4334 arg2 + sizeof(struct target_timeval)))
4335 goto efault;
bellard66fb9762003-03-23 01:06:05 +00004336 } else {
4337 pvalue = NULL;
4338 }
4339 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00004340 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00004341 if (copy_to_user_timeval(arg3,
4342 &ovalue.it_interval)
4343 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
4344 &ovalue.it_value))
4345 goto efault;
bellard66fb9762003-03-23 01:06:05 +00004346 }
4347 }
4348 break;
bellard31e31b82003-02-18 22:55:36 +00004349 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00004350 {
bellard66fb9762003-03-23 01:06:05 +00004351 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00004352
bellard66fb9762003-03-23 01:06:05 +00004353 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00004354 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00004355 if (copy_to_user_timeval(arg2,
4356 &value.it_interval)
4357 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
4358 &value.it_value))
4359 goto efault;
bellard66fb9762003-03-23 01:06:05 +00004360 }
4361 }
4362 break;
bellard31e31b82003-02-18 22:55:36 +00004363 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00004364 if (!(p = lock_user_string(arg1)))
4365 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004366 ret = get_errno(stat(path(p), &st));
4367 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004368 goto do_stat;
4369 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00004370 if (!(p = lock_user_string(arg1)))
4371 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004372 ret = get_errno(lstat(path(p), &st));
4373 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004374 goto do_stat;
4375 case TARGET_NR_fstat:
4376 {
4377 ret = get_errno(fstat(arg1, &st));
4378 do_stat:
4379 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004380 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00004381
bellard579a97f2007-11-11 14:26:47 +00004382 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4383 goto efault;
bellardd2fd1af2007-11-14 18:08:56 +00004384 __put_user(st.st_dev, &target_st->st_dev);
4385 __put_user(st.st_ino, &target_st->st_ino);
4386 __put_user(st.st_mode, &target_st->st_mode);
4387 __put_user(st.st_uid, &target_st->st_uid);
4388 __put_user(st.st_gid, &target_st->st_gid);
4389 __put_user(st.st_nlink, &target_st->st_nlink);
4390 __put_user(st.st_rdev, &target_st->st_rdev);
4391 __put_user(st.st_size, &target_st->st_size);
4392 __put_user(st.st_blksize, &target_st->st_blksize);
4393 __put_user(st.st_blocks, &target_st->st_blocks);
4394 __put_user(st.st_atime, &target_st->target_st_atime);
4395 __put_user(st.st_mtime, &target_st->target_st_mtime);
4396 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00004397 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00004398 }
4399 }
4400 break;
bellardebc05482003-09-30 21:08:41 +00004401#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00004402 case TARGET_NR_olduname:
4403 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004404#endif
4405#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00004406 case TARGET_NR_iopl:
4407 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004408#endif
bellard31e31b82003-02-18 22:55:36 +00004409 case TARGET_NR_vhangup:
4410 ret = get_errno(vhangup());
4411 break;
bellardebc05482003-09-30 21:08:41 +00004412#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00004413 case TARGET_NR_idle:
4414 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004415#endif
bellard42ad6ae2005-01-03 22:48:11 +00004416#ifdef TARGET_NR_syscall
4417 case TARGET_NR_syscall:
4418 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
4419 break;
4420#endif
bellard31e31b82003-02-18 22:55:36 +00004421 case TARGET_NR_wait4:
4422 {
4423 int status;
blueswir1992f48a2007-10-14 16:27:31 +00004424 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00004425 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00004426 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00004427 if (target_rusage)
4428 rusage_ptr = &rusage;
4429 else
4430 rusage_ptr = NULL;
4431 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
4432 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00004433 if (status_ptr) {
4434 if (put_user_s32(status, status_ptr))
4435 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004436 }
bellard2f619692007-11-16 10:46:05 +00004437 if (target_rusage)
4438 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00004439 }
4440 }
4441 break;
thse5febef2007-04-01 18:31:35 +00004442#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00004443 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00004444 if (!(p = lock_user_string(arg1)))
4445 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004446 ret = get_errno(swapoff(p));
4447 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004448 break;
thse5febef2007-04-01 18:31:35 +00004449#endif
bellard31e31b82003-02-18 22:55:36 +00004450 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00004451 {
pbrook53a59602006-03-25 19:31:22 +00004452 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00004453 struct sysinfo value;
4454 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00004455 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00004456 {
bellard579a97f2007-11-11 14:26:47 +00004457 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
4458 goto efault;
bellarda5448a72004-06-19 16:59:03 +00004459 __put_user(value.uptime, &target_value->uptime);
4460 __put_user(value.loads[0], &target_value->loads[0]);
4461 __put_user(value.loads[1], &target_value->loads[1]);
4462 __put_user(value.loads[2], &target_value->loads[2]);
4463 __put_user(value.totalram, &target_value->totalram);
4464 __put_user(value.freeram, &target_value->freeram);
4465 __put_user(value.sharedram, &target_value->sharedram);
4466 __put_user(value.bufferram, &target_value->bufferram);
4467 __put_user(value.totalswap, &target_value->totalswap);
4468 __put_user(value.freeswap, &target_value->freeswap);
4469 __put_user(value.procs, &target_value->procs);
4470 __put_user(value.totalhigh, &target_value->totalhigh);
4471 __put_user(value.freehigh, &target_value->freehigh);
4472 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00004473 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00004474 }
4475 }
4476 break;
thse5febef2007-04-01 18:31:35 +00004477#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00004478 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00004479 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
4480 break;
thse5febef2007-04-01 18:31:35 +00004481#endif
bellard31e31b82003-02-18 22:55:36 +00004482 case TARGET_NR_fsync:
4483 ret = get_errno(fsync(arg1));
4484 break;
bellard31e31b82003-02-18 22:55:36 +00004485 case TARGET_NR_clone:
bellard1b6b0292003-03-22 17:31:38 +00004486 ret = get_errno(do_fork(cpu_env, arg1, arg2));
4487 break;
bellardec86b0f2003-04-11 00:15:04 +00004488#ifdef __NR_exit_group
4489 /* new thread calls */
4490 case TARGET_NR_exit_group:
bellarde9009672005-04-26 20:42:36 +00004491 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00004492 ret = get_errno(exit_group(arg1));
4493 break;
4494#endif
bellard31e31b82003-02-18 22:55:36 +00004495 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00004496 if (!(p = lock_user_string(arg1)))
4497 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004498 ret = get_errno(setdomainname(p, arg2));
4499 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004500 break;
4501 case TARGET_NR_uname:
4502 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00004503 {
4504 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00004505
bellard579a97f2007-11-11 14:26:47 +00004506 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
4507 goto efault;
bellard29e619b2004-09-13 21:41:04 +00004508 ret = get_errno(sys_uname(buf));
4509 if (!is_error(ret)) {
4510 /* Overrite the native machine name with whatever is being
4511 emulated. */
4512 strcpy (buf->machine, UNAME_MACHINE);
pbrookc5937222006-05-14 11:30:38 +00004513 /* Allow the user to override the reported release. */
4514 if (qemu_uname_release && *qemu_uname_release)
4515 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00004516 }
pbrook53a59602006-03-25 19:31:22 +00004517 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00004518 }
bellard31e31b82003-02-18 22:55:36 +00004519 break;
bellard6dbad632003-03-16 18:05:05 +00004520#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00004521 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00004522 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00004523 break;
j_mayer84409dd2007-04-06 08:56:50 +00004524#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00004525 case TARGET_NR_vm86old:
4526 goto unimplemented;
4527 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00004528 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00004529 break;
4530#endif
j_mayer84409dd2007-04-06 08:56:50 +00004531#endif
bellard31e31b82003-02-18 22:55:36 +00004532 case TARGET_NR_adjtimex:
4533 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004534#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00004535 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00004536#endif
bellard31e31b82003-02-18 22:55:36 +00004537 case TARGET_NR_init_module:
4538 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00004539#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00004540 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00004541#endif
bellard31e31b82003-02-18 22:55:36 +00004542 goto unimplemented;
4543 case TARGET_NR_quotactl:
4544 goto unimplemented;
4545 case TARGET_NR_getpgid:
4546 ret = get_errno(getpgid(arg1));
4547 break;
4548 case TARGET_NR_fchdir:
4549 ret = get_errno(fchdir(arg1));
4550 break;
j_mayer84409dd2007-04-06 08:56:50 +00004551#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00004552 case TARGET_NR_bdflush:
4553 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00004554#endif
thse5febef2007-04-01 18:31:35 +00004555#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00004556 case TARGET_NR_sysfs:
4557 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004558#endif
bellard31e31b82003-02-18 22:55:36 +00004559 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00004560 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00004561 break;
thse5febef2007-04-01 18:31:35 +00004562#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00004563 case TARGET_NR_afs_syscall:
4564 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004565#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004566#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004567 case TARGET_NR__llseek:
4568 {
bellard4f2ac232004-04-26 19:44:02 +00004569#if defined (__x86_64__)
4570 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
bellard2f619692007-11-16 10:46:05 +00004571 if (put_user_s64(ret, arg4))
4572 goto efault;
bellard4f2ac232004-04-26 19:44:02 +00004573#else
bellard31e31b82003-02-18 22:55:36 +00004574 int64_t res;
4575 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard2f619692007-11-16 10:46:05 +00004576 if (put_user_s64(res, arg4))
4577 goto efault;
bellard4f2ac232004-04-26 19:44:02 +00004578#endif
bellard31e31b82003-02-18 22:55:36 +00004579 }
4580 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004581#endif
bellard31e31b82003-02-18 22:55:36 +00004582 case TARGET_NR_getdents:
blueswir1992f48a2007-10-14 16:27:31 +00004583#if TARGET_ABI_BITS != 32
pbrook53a59602006-03-25 19:31:22 +00004584 goto unimplemented;
blueswir1992f48a2007-10-14 16:27:31 +00004585#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00004586 {
pbrook53a59602006-03-25 19:31:22 +00004587 struct target_dirent *target_dirp;
bellard4add45b2003-06-05 01:52:59 +00004588 struct dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004589 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00004590
4591 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00004592 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00004593 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00004594 goto fail;
4595 }
ths3b46e622007-09-17 08:09:54 +00004596
bellard4add45b2003-06-05 01:52:59 +00004597 ret = get_errno(sys_getdents(arg1, dirp, count));
4598 if (!is_error(ret)) {
4599 struct dirent *de;
4600 struct target_dirent *tde;
4601 int len = ret;
4602 int reclen, treclen;
4603 int count1, tnamelen;
4604
4605 count1 = 0;
4606 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00004607 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4608 goto efault;
bellard4add45b2003-06-05 01:52:59 +00004609 tde = target_dirp;
4610 while (len > 0) {
4611 reclen = de->d_reclen;
blueswir1992f48a2007-10-14 16:27:31 +00004612 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
bellard4add45b2003-06-05 01:52:59 +00004613 tde->d_reclen = tswap16(treclen);
4614 tde->d_ino = tswapl(de->d_ino);
4615 tde->d_off = tswapl(de->d_off);
blueswir1992f48a2007-10-14 16:27:31 +00004616 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
bellard4add45b2003-06-05 01:52:59 +00004617 if (tnamelen > 256)
4618 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00004619 /* XXX: may not be correct */
bellard4add45b2003-06-05 01:52:59 +00004620 strncpy(tde->d_name, de->d_name, tnamelen);
4621 de = (struct dirent *)((char *)de + reclen);
4622 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00004623 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00004624 count1 += treclen;
4625 }
4626 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00004627 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00004628 }
4629 free(dirp);
4630 }
4631#else
bellard31e31b82003-02-18 22:55:36 +00004632 {
pbrook53a59602006-03-25 19:31:22 +00004633 struct dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004634 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00004635
bellard579a97f2007-11-11 14:26:47 +00004636 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4637 goto efault;
bellard72f03902003-02-18 23:33:18 +00004638 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00004639 if (!is_error(ret)) {
4640 struct dirent *de;
4641 int len = ret;
4642 int reclen;
4643 de = dirp;
4644 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004645 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00004646 if (reclen > len)
4647 break;
bellard8083a3e2003-03-24 23:12:16 +00004648 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00004649 tswapls(&de->d_ino);
4650 tswapls(&de->d_off);
4651 de = (struct dirent *)((char *)de + reclen);
4652 len -= reclen;
4653 }
4654 }
pbrook53a59602006-03-25 19:31:22 +00004655 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00004656 }
bellard4add45b2003-06-05 01:52:59 +00004657#endif
bellard31e31b82003-02-18 22:55:36 +00004658 break;
ths3ae43202007-09-16 21:39:48 +00004659#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00004660 case TARGET_NR_getdents64:
4661 {
pbrook53a59602006-03-25 19:31:22 +00004662 struct dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004663 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00004664 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4665 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00004666 ret = get_errno(sys_getdents64(arg1, dirp, count));
4667 if (!is_error(ret)) {
4668 struct dirent64 *de;
4669 int len = ret;
4670 int reclen;
4671 de = dirp;
4672 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004673 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00004674 if (reclen > len)
4675 break;
bellard8083a3e2003-03-24 23:12:16 +00004676 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00004677 tswap64s((uint64_t *)&de->d_ino);
4678 tswap64s((uint64_t *)&de->d_off);
bellarddab2ed92003-03-22 15:23:14 +00004679 de = (struct dirent64 *)((char *)de + reclen);
4680 len -= reclen;
4681 }
4682 }
pbrook53a59602006-03-25 19:31:22 +00004683 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00004684 }
4685 break;
bellarda541f292004-04-12 20:39:29 +00004686#endif /* TARGET_NR_getdents64 */
thse5febef2007-04-01 18:31:35 +00004687#ifdef TARGET_NR__newselect
bellard31e31b82003-02-18 22:55:36 +00004688 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00004689 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00004690 break;
thse5febef2007-04-01 18:31:35 +00004691#endif
4692#ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00004693 case TARGET_NR_poll:
4694 {
pbrook53a59602006-03-25 19:31:22 +00004695 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00004696 unsigned int nfds = arg2;
4697 int timeout = arg3;
4698 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00004699 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00004700
bellard579a97f2007-11-11 14:26:47 +00004701 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
4702 if (!target_pfd)
4703 goto efault;
bellard9de5e442003-03-23 16:49:39 +00004704 pfd = alloca(sizeof(struct pollfd) * nfds);
4705 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004706 pfd[i].fd = tswap32(target_pfd[i].fd);
4707 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00004708 }
4709 ret = get_errno(poll(pfd, nfds, timeout));
4710 if (!is_error(ret)) {
4711 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004712 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00004713 }
pbrook53a59602006-03-25 19:31:22 +00004714 ret += nfds * (sizeof(struct target_pollfd)
4715 - sizeof(struct pollfd));
bellard9de5e442003-03-23 16:49:39 +00004716 }
pbrook53a59602006-03-25 19:31:22 +00004717 unlock_user(target_pfd, arg1, ret);
bellard9de5e442003-03-23 16:49:39 +00004718 }
4719 break;
thse5febef2007-04-01 18:31:35 +00004720#endif
bellard31e31b82003-02-18 22:55:36 +00004721 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00004722 /* NOTE: the flock constant seems to be the same for every
4723 Linux platform */
4724 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00004725 break;
4726 case TARGET_NR_readv:
4727 {
4728 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00004729 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00004730
4731 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00004732 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
4733 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004734 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00004735 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00004736 }
4737 break;
4738 case TARGET_NR_writev:
4739 {
4740 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00004741 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00004742
4743 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00004744 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
4745 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004746 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00004747 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00004748 }
4749 break;
4750 case TARGET_NR_getsid:
4751 ret = get_errno(getsid(arg1));
4752 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004753#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00004754 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00004755 ret = get_errno(fdatasync(arg1));
4756 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004757#endif
bellard31e31b82003-02-18 22:55:36 +00004758 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00004759 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00004760 return value. */
ths0da46a62007-10-20 20:23:07 +00004761 ret = -TARGET_ENOTDIR;
4762 break;
bellard31e31b82003-02-18 22:55:36 +00004763 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00004764 {
pbrook53a59602006-03-25 19:31:22 +00004765 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004766 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00004767
bellard579a97f2007-11-11 14:26:47 +00004768 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
4769 goto efault;
bellard5cd43932003-03-29 16:54:36 +00004770 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004771 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00004772 ret = get_errno(sched_setparam(arg1, &schp));
4773 }
4774 break;
bellard31e31b82003-02-18 22:55:36 +00004775 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00004776 {
pbrook53a59602006-03-25 19:31:22 +00004777 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004778 struct sched_param schp;
4779 ret = get_errno(sched_getparam(arg1, &schp));
4780 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004781 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
4782 goto efault;
bellard5cd43932003-03-29 16:54:36 +00004783 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004784 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00004785 }
4786 }
4787 break;
bellard31e31b82003-02-18 22:55:36 +00004788 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00004789 {
pbrook53a59602006-03-25 19:31:22 +00004790 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004791 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00004792 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
4793 goto efault;
bellard5cd43932003-03-29 16:54:36 +00004794 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004795 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00004796 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
4797 }
4798 break;
bellard31e31b82003-02-18 22:55:36 +00004799 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00004800 ret = get_errno(sched_getscheduler(arg1));
4801 break;
bellard31e31b82003-02-18 22:55:36 +00004802 case TARGET_NR_sched_yield:
4803 ret = get_errno(sched_yield());
4804 break;
4805 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00004806 ret = get_errno(sched_get_priority_max(arg1));
4807 break;
bellard31e31b82003-02-18 22:55:36 +00004808 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00004809 ret = get_errno(sched_get_priority_min(arg1));
4810 break;
bellard31e31b82003-02-18 22:55:36 +00004811 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00004812 {
bellard5cd43932003-03-29 16:54:36 +00004813 struct timespec ts;
4814 ret = get_errno(sched_rr_get_interval(arg1, &ts));
4815 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004816 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00004817 }
4818 }
4819 break;
bellard31e31b82003-02-18 22:55:36 +00004820 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00004821 {
bellard1b6b0292003-03-22 17:31:38 +00004822 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00004823 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00004824 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00004825 if (is_error(ret) && arg2) {
4826 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00004827 }
4828 }
4829 break;
thse5febef2007-04-01 18:31:35 +00004830#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00004831 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00004832 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004833#endif
4834#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00004835 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00004836 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004837#endif
bellard31e31b82003-02-18 22:55:36 +00004838 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00004839 switch (arg1)
4840 {
4841 case PR_GET_PDEATHSIG:
4842 {
4843 int deathsig;
4844 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
bellard2f619692007-11-16 10:46:05 +00004845 if (!is_error(ret) && arg2
4846 && put_user_ual(deathsig, arg2))
4847 goto efault;
thse5574482007-02-11 20:03:13 +00004848 }
4849 break;
4850 default:
4851 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
4852 break;
4853 }
ths39b9aae2007-02-11 18:36:44 +00004854 break;
bellardd2fd1af2007-11-14 18:08:56 +00004855#ifdef TARGET_NR_arch_prctl
4856 case TARGET_NR_arch_prctl:
4857#if defined(TARGET_I386) && !defined(TARGET_ABI32)
4858 ret = do_arch_prctl(cpu_env, arg1, arg2);
4859 break;
4860#else
4861 goto unimplemented;
4862#endif
4863#endif
bellard67867302003-11-23 17:05:30 +00004864#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00004865 case TARGET_NR_pread:
bellard579a97f2007-11-11 14:26:47 +00004866 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4867 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004868 ret = get_errno(pread(arg1, p, arg3, arg4));
4869 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00004870 break;
bellard31e31b82003-02-18 22:55:36 +00004871 case TARGET_NR_pwrite:
bellard579a97f2007-11-11 14:26:47 +00004872 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4873 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004874 ret = get_errno(pwrite(arg1, p, arg3, arg4));
4875 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00004876 break;
bellard67867302003-11-23 17:05:30 +00004877#endif
aurel32f2c7ba12008-03-28 22:32:06 +00004878#ifdef TARGET_NR_pread64
4879 case TARGET_NR_pread64:
4880 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4881 goto efault;
4882 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
4883 unlock_user(p, arg2, ret);
4884 break;
4885 case TARGET_NR_pwrite64:
4886 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4887 goto efault;
4888 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
4889 unlock_user(p, arg2, 0);
4890 break;
4891#endif
bellard31e31b82003-02-18 22:55:36 +00004892 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00004893 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
4894 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004895 ret = get_errno(sys_getcwd1(p, arg2));
4896 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00004897 break;
4898 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00004899 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004900 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00004901 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004902 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00004903#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
4904 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
bellard579a97f2007-11-11 14:26:47 +00004905 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00004906 break;
4907#else
bellard5cd43932003-03-29 16:54:36 +00004908 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00004909#endif
bellard31e31b82003-02-18 22:55:36 +00004910 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00004911 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004912#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00004913 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00004914 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004915#endif
4916#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00004917 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00004918 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004919#endif
bellard048f6b42005-11-26 18:47:20 +00004920#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00004921 case TARGET_NR_vfork:
bellard1b6b0292003-03-22 17:31:38 +00004922 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00004923 break;
bellard048f6b42005-11-26 18:47:20 +00004924#endif
bellardebc05482003-09-30 21:08:41 +00004925#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00004926 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00004927 {
4928 struct rlimit rlim;
4929 ret = get_errno(getrlimit(arg1, &rlim));
4930 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004931 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00004932 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
4933 goto efault;
bellard728584b2003-04-29 20:43:36 +00004934 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
4935 target_rlim->rlim_max = tswapl(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00004936 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00004937 }
4938 break;
4939 }
bellardebc05482003-09-30 21:08:41 +00004940#endif
bellarda315a142005-01-30 22:59:18 +00004941#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00004942 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00004943 if (!(p = lock_user_string(arg1)))
4944 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004945 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
4946 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00004947 break;
bellarda315a142005-01-30 22:59:18 +00004948#endif
4949#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00004950 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00004951 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00004952 break;
bellarda315a142005-01-30 22:59:18 +00004953#endif
4954#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00004955 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00004956 if (!(p = lock_user_string(arg1)))
4957 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004958 ret = get_errno(stat(path(p), &st));
4959 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004960 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004961#endif
4962#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00004963 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00004964 if (!(p = lock_user_string(arg1)))
4965 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004966 ret = get_errno(lstat(path(p), &st));
4967 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004968 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004969#endif
4970#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00004971 case TARGET_NR_fstat64:
bellard60cd49d2003-03-16 22:53:56 +00004972 {
4973 ret = get_errno(fstat(arg1, &st));
4974 do_stat64:
4975 if (!is_error(ret)) {
pbrookce4defa2006-02-09 16:49:55 +00004976#ifdef TARGET_ARM
4977 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004978 struct target_eabi_stat64 *target_st;
bellard579a97f2007-11-11 14:26:47 +00004979
4980 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4981 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00004982 memset(target_st, 0, sizeof(struct target_eabi_stat64));
bellard579a97f2007-11-11 14:26:47 +00004983 __put_user(st.st_dev, &target_st->st_dev);
4984 __put_user(st.st_ino, &target_st->st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004985#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
bellard579a97f2007-11-11 14:26:47 +00004986 __put_user(st.st_ino, &target_st->__st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004987#endif
bellard579a97f2007-11-11 14:26:47 +00004988 __put_user(st.st_mode, &target_st->st_mode);
4989 __put_user(st.st_nlink, &target_st->st_nlink);
4990 __put_user(st.st_uid, &target_st->st_uid);
4991 __put_user(st.st_gid, &target_st->st_gid);
4992 __put_user(st.st_rdev, &target_st->st_rdev);
4993 __put_user(st.st_size, &target_st->st_size);
4994 __put_user(st.st_blksize, &target_st->st_blksize);
4995 __put_user(st.st_blocks, &target_st->st_blocks);
4996 __put_user(st.st_atime, &target_st->target_st_atime);
4997 __put_user(st.st_mtime, &target_st->target_st_mtime);
4998 __put_user(st.st_ctime, &target_st->target_st_ctime);
4999 unlock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00005000 } else
5001#endif
5002 {
pbrook53a59602006-03-25 19:31:22 +00005003 struct target_stat64 *target_st;
bellard579a97f2007-11-11 14:26:47 +00005004
5005 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
5006 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005007 memset(target_st, 0, sizeof(struct target_stat64));
bellard579a97f2007-11-11 14:26:47 +00005008 __put_user(st.st_dev, &target_st->st_dev);
5009 __put_user(st.st_ino, &target_st->st_ino);
pbrookce4defa2006-02-09 16:49:55 +00005010#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
bellard579a97f2007-11-11 14:26:47 +00005011 __put_user(st.st_ino, &target_st->__st_ino);
pbrookce4defa2006-02-09 16:49:55 +00005012#endif
bellard579a97f2007-11-11 14:26:47 +00005013 __put_user(st.st_mode, &target_st->st_mode);
5014 __put_user(st.st_nlink, &target_st->st_nlink);
5015 __put_user(st.st_uid, &target_st->st_uid);
5016 __put_user(st.st_gid, &target_st->st_gid);
5017 __put_user(st.st_rdev, &target_st->st_rdev);
pbrookce4defa2006-02-09 16:49:55 +00005018 /* XXX: better use of kernel struct */
bellard579a97f2007-11-11 14:26:47 +00005019 __put_user(st.st_size, &target_st->st_size);
5020 __put_user(st.st_blksize, &target_st->st_blksize);
5021 __put_user(st.st_blocks, &target_st->st_blocks);
5022 __put_user(st.st_atime, &target_st->target_st_atime);
5023 __put_user(st.st_mtime, &target_st->target_st_mtime);
5024 __put_user(st.st_ctime, &target_st->target_st_ctime);
5025 unlock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00005026 }
bellard60cd49d2003-03-16 22:53:56 +00005027 }
5028 }
5029 break;
bellarda315a142005-01-30 22:59:18 +00005030#endif
bellard67867302003-11-23 17:05:30 +00005031#ifdef USE_UID16
5032 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00005033 if (!(p = lock_user_string(arg1)))
5034 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005035 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
5036 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00005037 break;
5038 case TARGET_NR_getuid:
5039 ret = get_errno(high2lowuid(getuid()));
5040 break;
5041 case TARGET_NR_getgid:
5042 ret = get_errno(high2lowgid(getgid()));
5043 break;
5044 case TARGET_NR_geteuid:
5045 ret = get_errno(high2lowuid(geteuid()));
5046 break;
5047 case TARGET_NR_getegid:
5048 ret = get_errno(high2lowgid(getegid()));
5049 break;
5050 case TARGET_NR_setreuid:
5051 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
5052 break;
5053 case TARGET_NR_setregid:
5054 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
5055 break;
5056 case TARGET_NR_getgroups:
5057 {
5058 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00005059 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00005060 gid_t *grouplist;
5061 int i;
5062
5063 grouplist = alloca(gidsetsize * sizeof(gid_t));
5064 ret = get_errno(getgroups(gidsetsize, grouplist));
5065 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005066 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
5067 if (!target_grouplist)
5068 goto efault;
bellard67867302003-11-23 17:05:30 +00005069 for(i = 0;i < gidsetsize; i++)
5070 target_grouplist[i] = tswap16(grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00005071 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00005072 }
5073 }
5074 break;
5075 case TARGET_NR_setgroups:
5076 {
5077 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00005078 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00005079 gid_t *grouplist;
5080 int i;
5081
5082 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00005083 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
5084 if (!target_grouplist) {
5085 ret = -TARGET_EFAULT;
5086 goto fail;
5087 }
bellard67867302003-11-23 17:05:30 +00005088 for(i = 0;i < gidsetsize; i++)
5089 grouplist[i] = tswap16(target_grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00005090 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00005091 ret = get_errno(setgroups(gidsetsize, grouplist));
5092 }
5093 break;
5094 case TARGET_NR_fchown:
5095 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
5096 break;
thsccfa72b2007-09-24 09:23:34 +00005097#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
5098 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00005099 if (!(p = lock_user_string(arg2)))
5100 goto efault;
5101 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
5102 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00005103 break;
5104#endif
bellard67867302003-11-23 17:05:30 +00005105#ifdef TARGET_NR_setresuid
5106 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00005107 ret = get_errno(setresuid(low2highuid(arg1),
5108 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00005109 low2highuid(arg3)));
5110 break;
5111#endif
5112#ifdef TARGET_NR_getresuid
5113 case TARGET_NR_getresuid:
5114 {
pbrook53a59602006-03-25 19:31:22 +00005115 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00005116 ret = get_errno(getresuid(&ruid, &euid, &suid));
5117 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005118 if (put_user_u16(high2lowuid(ruid), arg1)
5119 || put_user_u16(high2lowuid(euid), arg2)
5120 || put_user_u16(high2lowuid(suid), arg3))
5121 goto efault;
bellard67867302003-11-23 17:05:30 +00005122 }
5123 }
5124 break;
5125#endif
5126#ifdef TARGET_NR_getresgid
5127 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00005128 ret = get_errno(setresgid(low2highgid(arg1),
5129 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00005130 low2highgid(arg3)));
5131 break;
5132#endif
5133#ifdef TARGET_NR_getresgid
5134 case TARGET_NR_getresgid:
5135 {
pbrook53a59602006-03-25 19:31:22 +00005136 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00005137 ret = get_errno(getresgid(&rgid, &egid, &sgid));
5138 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005139 if (put_user_u16(high2lowgid(rgid), arg1)
5140 || put_user_u16(high2lowgid(egid), arg2)
5141 || put_user_u16(high2lowgid(sgid), arg3))
5142 goto efault;
bellard67867302003-11-23 17:05:30 +00005143 }
5144 }
5145 break;
5146#endif
5147 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00005148 if (!(p = lock_user_string(arg1)))
5149 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005150 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
5151 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00005152 break;
5153 case TARGET_NR_setuid:
5154 ret = get_errno(setuid(low2highuid(arg1)));
5155 break;
5156 case TARGET_NR_setgid:
5157 ret = get_errno(setgid(low2highgid(arg1)));
5158 break;
5159 case TARGET_NR_setfsuid:
5160 ret = get_errno(setfsuid(arg1));
5161 break;
5162 case TARGET_NR_setfsgid:
5163 ret = get_errno(setfsgid(arg1));
5164 break;
5165#endif /* USE_UID16 */
5166
bellarda315a142005-01-30 22:59:18 +00005167#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00005168 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00005169 if (!(p = lock_user_string(arg1)))
5170 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005171 ret = get_errno(lchown(p, arg2, arg3));
5172 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00005173 break;
bellarda315a142005-01-30 22:59:18 +00005174#endif
5175#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00005176 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00005177 ret = get_errno(getuid());
5178 break;
bellarda315a142005-01-30 22:59:18 +00005179#endif
5180#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00005181 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00005182 ret = get_errno(getgid());
5183 break;
bellarda315a142005-01-30 22:59:18 +00005184#endif
5185#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00005186 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00005187 ret = get_errno(geteuid());
5188 break;
bellarda315a142005-01-30 22:59:18 +00005189#endif
5190#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00005191 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00005192 ret = get_errno(getegid());
5193 break;
bellarda315a142005-01-30 22:59:18 +00005194#endif
5195#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00005196 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00005197 ret = get_errno(setreuid(arg1, arg2));
5198 break;
bellarda315a142005-01-30 22:59:18 +00005199#endif
5200#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00005201 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00005202 ret = get_errno(setregid(arg1, arg2));
5203 break;
bellarda315a142005-01-30 22:59:18 +00005204#endif
5205#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00005206 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00005207 {
5208 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00005209 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00005210 gid_t *grouplist;
5211 int i;
5212
5213 grouplist = alloca(gidsetsize * sizeof(gid_t));
5214 ret = get_errno(getgroups(gidsetsize, grouplist));
5215 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005216 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
5217 if (!target_grouplist) {
5218 ret = -TARGET_EFAULT;
5219 goto fail;
5220 }
bellard99c475a2005-01-31 20:45:13 +00005221 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00005222 target_grouplist[i] = tswap32(grouplist[i]);
5223 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00005224 }
5225 }
5226 break;
bellarda315a142005-01-30 22:59:18 +00005227#endif
5228#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00005229 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00005230 {
5231 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00005232 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00005233 gid_t *grouplist;
5234 int i;
ths3b46e622007-09-17 08:09:54 +00005235
bellard99c475a2005-01-31 20:45:13 +00005236 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00005237 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
5238 if (!target_grouplist) {
5239 ret = -TARGET_EFAULT;
5240 goto fail;
5241 }
bellard99c475a2005-01-31 20:45:13 +00005242 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00005243 grouplist[i] = tswap32(target_grouplist[i]);
5244 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00005245 ret = get_errno(setgroups(gidsetsize, grouplist));
5246 }
5247 break;
bellarda315a142005-01-30 22:59:18 +00005248#endif
5249#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00005250 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00005251 ret = get_errno(fchown(arg1, arg2, arg3));
5252 break;
bellarda315a142005-01-30 22:59:18 +00005253#endif
5254#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00005255 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00005256 ret = get_errno(setresuid(arg1, arg2, arg3));
5257 break;
bellarda315a142005-01-30 22:59:18 +00005258#endif
5259#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00005260 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00005261 {
pbrook53a59602006-03-25 19:31:22 +00005262 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00005263 ret = get_errno(getresuid(&ruid, &euid, &suid));
5264 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005265 if (put_user_u32(ruid, arg1)
5266 || put_user_u32(euid, arg2)
5267 || put_user_u32(suid, arg3))
5268 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00005269 }
5270 }
5271 break;
bellarda315a142005-01-30 22:59:18 +00005272#endif
5273#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00005274 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00005275 ret = get_errno(setresgid(arg1, arg2, arg3));
5276 break;
bellarda315a142005-01-30 22:59:18 +00005277#endif
5278#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00005279 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00005280 {
pbrook53a59602006-03-25 19:31:22 +00005281 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00005282 ret = get_errno(getresgid(&rgid, &egid, &sgid));
5283 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005284 if (put_user_u32(rgid, arg1)
5285 || put_user_u32(egid, arg2)
5286 || put_user_u32(sgid, arg3))
5287 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00005288 }
5289 }
5290 break;
bellarda315a142005-01-30 22:59:18 +00005291#endif
5292#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00005293 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00005294 if (!(p = lock_user_string(arg1)))
5295 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005296 ret = get_errno(chown(p, arg2, arg3));
5297 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00005298 break;
bellarda315a142005-01-30 22:59:18 +00005299#endif
5300#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00005301 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00005302 ret = get_errno(setuid(arg1));
5303 break;
bellarda315a142005-01-30 22:59:18 +00005304#endif
5305#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00005306 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00005307 ret = get_errno(setgid(arg1));
5308 break;
bellarda315a142005-01-30 22:59:18 +00005309#endif
5310#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00005311 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00005312 ret = get_errno(setfsuid(arg1));
5313 break;
bellarda315a142005-01-30 22:59:18 +00005314#endif
5315#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00005316 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00005317 ret = get_errno(setfsgid(arg1));
5318 break;
bellarda315a142005-01-30 22:59:18 +00005319#endif
bellard67867302003-11-23 17:05:30 +00005320
bellard31e31b82003-02-18 22:55:36 +00005321 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00005322 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00005323#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00005324 case TARGET_NR_mincore:
bellardb03c60f2003-03-23 17:19:56 +00005325 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00005326#endif
5327#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00005328 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00005329 /* A straight passthrough may not be safe because qemu sometimes
5330 turns private flie-backed mappings into anonymous mappings.
5331 This will break MADV_DONTNEED.
5332 This is a hint, so ignoring and returning success is ok. */
5333 ret = get_errno(0);
5334 break;
bellardffa65c32004-01-04 23:57:22 +00005335#endif
blueswir1992f48a2007-10-14 16:27:31 +00005336#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00005337 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00005338 {
thsb1e341e2007-03-20 21:50:52 +00005339 int cmd;
bellard77e46722003-04-29 20:39:06 +00005340 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00005341 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00005342#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00005343 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00005344#endif
bellard77e46722003-04-29 20:39:06 +00005345
thsb1e341e2007-03-20 21:50:52 +00005346 switch(arg2){
5347 case TARGET_F_GETLK64:
5348 cmd = F_GETLK64;
thsa7222582007-03-22 15:08:55 +00005349 break;
thsb1e341e2007-03-20 21:50:52 +00005350 case TARGET_F_SETLK64:
5351 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00005352 break;
thsb1e341e2007-03-20 21:50:52 +00005353 case TARGET_F_SETLKW64:
5354 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00005355 break;
thsb1e341e2007-03-20 21:50:52 +00005356 default:
5357 cmd = arg2;
thsa7222582007-03-22 15:08:55 +00005358 break;
thsb1e341e2007-03-20 21:50:52 +00005359 }
5360
bellard60cd49d2003-03-16 22:53:56 +00005361 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00005362 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00005363#ifdef TARGET_ARM
5364 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00005365 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5366 goto efault;
ths58134272007-03-31 18:59:32 +00005367 fl.l_type = tswap16(target_efl->l_type);
5368 fl.l_whence = tswap16(target_efl->l_whence);
5369 fl.l_start = tswap64(target_efl->l_start);
5370 fl.l_len = tswap64(target_efl->l_len);
5371 fl.l_pid = tswapl(target_efl->l_pid);
5372 unlock_user_struct(target_efl, arg3, 0);
5373 } else
5374#endif
5375 {
bellard9ee1fa22007-11-11 15:11:19 +00005376 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5377 goto efault;
ths58134272007-03-31 18:59:32 +00005378 fl.l_type = tswap16(target_fl->l_type);
5379 fl.l_whence = tswap16(target_fl->l_whence);
5380 fl.l_start = tswap64(target_fl->l_start);
5381 fl.l_len = tswap64(target_fl->l_len);
5382 fl.l_pid = tswapl(target_fl->l_pid);
5383 unlock_user_struct(target_fl, arg3, 0);
5384 }
thsb1e341e2007-03-20 21:50:52 +00005385 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00005386 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00005387#ifdef TARGET_ARM
5388 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00005389 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
5390 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005391 target_efl->l_type = tswap16(fl.l_type);
5392 target_efl->l_whence = tswap16(fl.l_whence);
5393 target_efl->l_start = tswap64(fl.l_start);
5394 target_efl->l_len = tswap64(fl.l_len);
5395 target_efl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00005396 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00005397 } else
5398#endif
5399 {
bellard9ee1fa22007-11-11 15:11:19 +00005400 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
5401 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005402 target_fl->l_type = tswap16(fl.l_type);
5403 target_fl->l_whence = tswap16(fl.l_whence);
5404 target_fl->l_start = tswap64(fl.l_start);
5405 target_fl->l_len = tswap64(fl.l_len);
5406 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00005407 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00005408 }
bellard77e46722003-04-29 20:39:06 +00005409 }
5410 break;
5411
thsb1e341e2007-03-20 21:50:52 +00005412 case TARGET_F_SETLK64:
5413 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00005414#ifdef TARGET_ARM
5415 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00005416 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5417 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005418 fl.l_type = tswap16(target_efl->l_type);
5419 fl.l_whence = tswap16(target_efl->l_whence);
5420 fl.l_start = tswap64(target_efl->l_start);
5421 fl.l_len = tswap64(target_efl->l_len);
5422 fl.l_pid = tswapl(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00005423 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00005424 } else
5425#endif
5426 {
bellard9ee1fa22007-11-11 15:11:19 +00005427 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5428 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005429 fl.l_type = tswap16(target_fl->l_type);
5430 fl.l_whence = tswap16(target_fl->l_whence);
5431 fl.l_start = tswap64(target_fl->l_start);
5432 fl.l_len = tswap64(target_fl->l_len);
5433 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00005434 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00005435 }
thsb1e341e2007-03-20 21:50:52 +00005436 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00005437 break;
bellard60cd49d2003-03-16 22:53:56 +00005438 default:
bellard9ee1fa22007-11-11 15:11:19 +00005439 ret = do_fcntl(arg1, cmd, arg3);
bellard60cd49d2003-03-16 22:53:56 +00005440 break;
5441 }
bellard77e46722003-04-29 20:39:06 +00005442 break;
5443 }
bellard60cd49d2003-03-16 22:53:56 +00005444#endif
ths7d600c82006-12-08 01:32:58 +00005445#ifdef TARGET_NR_cacheflush
5446 case TARGET_NR_cacheflush:
5447 /* self-modifying code is handled automatically, so nothing needed */
5448 ret = 0;
5449 break;
5450#endif
bellardebc05482003-09-30 21:08:41 +00005451#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00005452 case TARGET_NR_security:
5453 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005454#endif
bellardc573ff62004-01-04 15:51:36 +00005455#ifdef TARGET_NR_getpagesize
5456 case TARGET_NR_getpagesize:
5457 ret = TARGET_PAGE_SIZE;
5458 break;
5459#endif
bellard31e31b82003-02-18 22:55:36 +00005460 case TARGET_NR_gettid:
5461 ret = get_errno(gettid());
5462 break;
thse5febef2007-04-01 18:31:35 +00005463#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00005464 case TARGET_NR_readahead:
bellard5cd43932003-03-29 16:54:36 +00005465 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005466#endif
bellardebc05482003-09-30 21:08:41 +00005467#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00005468 case TARGET_NR_setxattr:
5469 case TARGET_NR_lsetxattr:
5470 case TARGET_NR_fsetxattr:
5471 case TARGET_NR_getxattr:
5472 case TARGET_NR_lgetxattr:
5473 case TARGET_NR_fgetxattr:
5474 case TARGET_NR_listxattr:
5475 case TARGET_NR_llistxattr:
5476 case TARGET_NR_flistxattr:
5477 case TARGET_NR_removexattr:
5478 case TARGET_NR_lremovexattr:
5479 case TARGET_NR_fremovexattr:
bellard5cd43932003-03-29 16:54:36 +00005480 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00005481#endif
5482#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00005483 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00005484#if defined(TARGET_MIPS)
ths6f5b89a2007-03-02 20:48:00 +00005485 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
5486 ret = 0;
5487 break;
bellard8d18e892007-11-14 15:18:40 +00005488#elif defined(TARGET_I386) && defined(TARGET_ABI32)
5489 ret = do_set_thread_area(cpu_env, arg1);
5490 break;
ths6f5b89a2007-03-02 20:48:00 +00005491#else
5492 goto unimplemented_nowarn;
5493#endif
5494#endif
5495#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00005496 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00005497#if defined(TARGET_I386) && defined(TARGET_ABI32)
5498 ret = do_get_thread_area(cpu_env, arg1);
5499#else
bellard5cd43932003-03-29 16:54:36 +00005500 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00005501#endif
bellard8d18e892007-11-14 15:18:40 +00005502#endif
bellard48dc41e2006-06-21 18:15:50 +00005503#ifdef TARGET_NR_getdomainname
5504 case TARGET_NR_getdomainname:
5505 goto unimplemented_nowarn;
5506#endif
ths6f5b89a2007-03-02 20:48:00 +00005507
thsb5906f92007-03-19 13:32:45 +00005508#ifdef TARGET_NR_clock_gettime
5509 case TARGET_NR_clock_gettime:
5510 {
5511 struct timespec ts;
5512 ret = get_errno(clock_gettime(arg1, &ts));
5513 if (!is_error(ret)) {
5514 host_to_target_timespec(arg2, &ts);
5515 }
5516 break;
5517 }
5518#endif
5519#ifdef TARGET_NR_clock_getres
5520 case TARGET_NR_clock_getres:
5521 {
5522 struct timespec ts;
5523 ret = get_errno(clock_getres(arg1, &ts));
5524 if (!is_error(ret)) {
5525 host_to_target_timespec(arg2, &ts);
5526 }
5527 break;
5528 }
5529#endif
5530
ths6f5b89a2007-03-02 20:48:00 +00005531#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
5532 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00005533 ret = get_errno(set_tid_address((int *)g2h(arg1)));
5534 break;
ths6f5b89a2007-03-02 20:48:00 +00005535#endif
5536
ths3ae43202007-09-16 21:39:48 +00005537#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00005538 case TARGET_NR_tkill:
5539 ret = get_errno(sys_tkill((int)arg1, (int)arg2));
5540 break;
5541#endif
5542
ths3ae43202007-09-16 21:39:48 +00005543#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00005544 case TARGET_NR_tgkill:
5545 ret = get_errno(sys_tgkill((int)arg1, (int)arg2, (int)arg3));
5546 break;
5547#endif
5548
ths4f2b1fe2007-06-21 21:57:12 +00005549#ifdef TARGET_NR_set_robust_list
5550 case TARGET_NR_set_robust_list:
5551 goto unimplemented_nowarn;
5552#endif
5553
ths9007f0e2007-09-25 17:50:37 +00005554#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
5555 case TARGET_NR_utimensat:
5556 {
5557 struct timespec ts[2];
5558 target_to_host_timespec(ts, arg3);
5559 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
5560 if (!arg2)
5561 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
5562 else {
bellard579a97f2007-11-11 14:26:47 +00005563 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00005564 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00005565 goto fail;
5566 }
5567 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
5568 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00005569 }
5570 }
5571 break;
5572#endif
5573
bellard31e31b82003-02-18 22:55:36 +00005574 default:
5575 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00005576 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00005577#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 +00005578 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00005579#endif
ths0da46a62007-10-20 20:23:07 +00005580 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00005581 break;
5582 }
bellard579a97f2007-11-11 14:26:47 +00005583fail:
bellardc573ff62004-01-04 15:51:36 +00005584#ifdef DEBUG
5585 gemu_log(" = %ld\n", ret);
5586#endif
thsb92c47c2007-11-01 00:07:38 +00005587 if(do_strace)
5588 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00005589 return ret;
bellard579a97f2007-11-11 14:26:47 +00005590efault:
5591 ret = -TARGET_EFAULT;
5592 goto fail;
bellard31e31b82003-02-18 22:55:36 +00005593}