blob: 590c72fa4aaff8c02feea6921b71d32a60b71c7e [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);
ths0da46a62007-10-20 20:23:07 +0000909 ret = -TARGET_ENOSYS;
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:
999 goto unimplemented;
bellard8853f862004-02-22 14:57:26 +00001000 }
1001 break;
1002 default:
1003 unimplemented:
1004 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1005 level, optname);
ths0da46a62007-10-20 20:23:07 +00001006 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00001007 break;
1008 }
1009 return ret;
bellard7854b052003-03-29 17:22:23 +00001010}
1011
bellard579a97f2007-11-11 14:26:47 +00001012/* FIXME
1013 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1014 * other lock functions have a return code of 0 for failure.
1015 */
1016static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1017 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001018{
1019 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001020 abi_ulong base;
bellard579a97f2007-11-11 14:26:47 +00001021 int i, j;
pbrook53a59602006-03-25 19:31:22 +00001022
bellard579a97f2007-11-11 14:26:47 +00001023 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1024 if (!target_vec)
1025 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001026 for(i = 0;i < count; i++) {
1027 base = tswapl(target_vec[i].iov_base);
1028 vec[i].iov_len = tswapl(target_vec[i].iov_len);
bellard579a97f2007-11-11 14:26:47 +00001029 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
1030 if (!vec[i].iov_base)
1031 goto fail;
pbrook53a59602006-03-25 19:31:22 +00001032 }
1033 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001034 return 0;
1035 fail:
1036 /* failure - unwind locks */
1037 for (j = 0; j < i; j++) {
1038 base = tswapl(target_vec[j].iov_base);
1039 unlock_user(vec[j].iov_base, base, 0);
1040 }
1041 unlock_user (target_vec, target_addr, 0);
1042 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001043}
1044
bellard579a97f2007-11-11 14:26:47 +00001045static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1046 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001047{
1048 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001049 abi_ulong base;
pbrook53a59602006-03-25 19:31:22 +00001050 int i;
1051
bellard579a97f2007-11-11 14:26:47 +00001052 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1053 if (!target_vec)
1054 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001055 for(i = 0;i < count; i++) {
1056 base = tswapl(target_vec[i].iov_base);
1057 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1058 }
1059 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001060
1061 return 0;
pbrook53a59602006-03-25 19:31:22 +00001062}
1063
ths0da46a62007-10-20 20:23:07 +00001064/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001065static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00001066{
1067#if defined(TARGET_MIPS)
1068 switch(type) {
1069 case TARGET_SOCK_DGRAM:
1070 type = SOCK_DGRAM;
1071 break;
1072 case TARGET_SOCK_STREAM:
1073 type = SOCK_STREAM;
1074 break;
1075 case TARGET_SOCK_RAW:
1076 type = SOCK_RAW;
1077 break;
1078 case TARGET_SOCK_RDM:
1079 type = SOCK_RDM;
1080 break;
1081 case TARGET_SOCK_SEQPACKET:
1082 type = SOCK_SEQPACKET;
1083 break;
1084 case TARGET_SOCK_PACKET:
1085 type = SOCK_PACKET;
1086 break;
1087 }
1088#endif
balrog12bc92a2007-10-30 21:06:14 +00001089 if (domain == PF_NETLINK)
1090 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
bellard3532fa72006-06-24 15:06:03 +00001091 return get_errno(socket(domain, type, protocol));
1092}
1093
ths0da46a62007-10-20 20:23:07 +00001094/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001095static abi_long do_bind(int sockfd, abi_ulong target_addr,
1096 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001097{
1098 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001099
bellard3532fa72006-06-24 15:06:03 +00001100 target_to_host_sockaddr(addr, target_addr, addrlen);
1101 return get_errno(bind(sockfd, addr, addrlen));
1102}
1103
ths0da46a62007-10-20 20:23:07 +00001104/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001105static abi_long do_connect(int sockfd, abi_ulong target_addr,
1106 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001107{
1108 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001109
bellard3532fa72006-06-24 15:06:03 +00001110 target_to_host_sockaddr(addr, target_addr, addrlen);
1111 return get_errno(connect(sockfd, addr, addrlen));
1112}
1113
ths0da46a62007-10-20 20:23:07 +00001114/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001115static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1116 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001117{
blueswir1992f48a2007-10-14 16:27:31 +00001118 abi_long ret;
bellard3532fa72006-06-24 15:06:03 +00001119 struct target_msghdr *msgp;
1120 struct msghdr msg;
1121 int count;
1122 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001123 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001124
bellard579a97f2007-11-11 14:26:47 +00001125 /* FIXME */
1126 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1127 msgp,
1128 target_msg,
1129 send ? 1 : 0))
1130 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001131 if (msgp->msg_name) {
1132 msg.msg_namelen = tswap32(msgp->msg_namelen);
1133 msg.msg_name = alloca(msg.msg_namelen);
1134 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1135 msg.msg_namelen);
1136 } else {
1137 msg.msg_name = NULL;
1138 msg.msg_namelen = 0;
1139 }
1140 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1141 msg.msg_control = alloca(msg.msg_controllen);
1142 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001143
bellard3532fa72006-06-24 15:06:03 +00001144 count = tswapl(msgp->msg_iovlen);
1145 vec = alloca(count * sizeof(struct iovec));
1146 target_vec = tswapl(msgp->msg_iov);
bellard579a97f2007-11-11 14:26:47 +00001147 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
bellard3532fa72006-06-24 15:06:03 +00001148 msg.msg_iovlen = count;
1149 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001150
bellard3532fa72006-06-24 15:06:03 +00001151 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00001152 ret = target_to_host_cmsg(&msg, msgp);
1153 if (ret == 0)
1154 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00001155 } else {
1156 ret = get_errno(recvmsg(fd, &msg, flags));
1157 if (!is_error(ret))
bellard5a4a8982007-11-11 17:39:18 +00001158 ret = host_to_target_cmsg(msgp, &msg);
bellard3532fa72006-06-24 15:06:03 +00001159 }
1160 unlock_iovec(vec, target_vec, count, !send);
bellard579a97f2007-11-11 14:26:47 +00001161 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00001162 return ret;
1163}
1164
ths0da46a62007-10-20 20:23:07 +00001165/* do_accept() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001166static abi_long do_accept(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001167 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001168{
bellard2f619692007-11-16 10:46:05 +00001169 socklen_t addrlen;
1170 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001171 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001172
bellard2f619692007-11-16 10:46:05 +00001173 if (get_user_u32(addrlen, target_addrlen_addr))
1174 return -TARGET_EFAULT;
1175
1176 addr = alloca(addrlen);
1177
pbrook1be9e1d2006-11-19 15:26:04 +00001178 ret = get_errno(accept(fd, addr, &addrlen));
1179 if (!is_error(ret)) {
1180 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001181 if (put_user_u32(addrlen, target_addrlen_addr))
1182 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001183 }
1184 return ret;
1185}
1186
ths0da46a62007-10-20 20:23:07 +00001187/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001188static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001189 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001190{
bellard2f619692007-11-16 10:46:05 +00001191 socklen_t addrlen;
1192 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001193 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001194
bellard2f619692007-11-16 10:46:05 +00001195 if (get_user_u32(addrlen, target_addrlen_addr))
1196 return -TARGET_EFAULT;
1197
1198 addr = alloca(addrlen);
1199
pbrook1be9e1d2006-11-19 15:26:04 +00001200 ret = get_errno(getpeername(fd, addr, &addrlen));
1201 if (!is_error(ret)) {
1202 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001203 if (put_user_u32(addrlen, target_addrlen_addr))
1204 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001205 }
1206 return ret;
1207}
1208
ths0da46a62007-10-20 20:23:07 +00001209/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001210static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001211 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001212{
bellard2f619692007-11-16 10:46:05 +00001213 socklen_t addrlen;
1214 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001215 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001216
bellard2f619692007-11-16 10:46:05 +00001217 if (get_user_u32(addrlen, target_addrlen_addr))
1218 return -TARGET_EFAULT;
1219
1220 addr = alloca(addrlen);
1221
pbrook1be9e1d2006-11-19 15:26:04 +00001222 ret = get_errno(getsockname(fd, addr, &addrlen));
1223 if (!is_error(ret)) {
1224 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001225 if (put_user_u32(addrlen, target_addrlen_addr))
1226 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001227 }
1228 return ret;
1229}
1230
ths0da46a62007-10-20 20:23:07 +00001231/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001232static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00001233 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001234{
1235 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00001236 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001237
1238 ret = get_errno(socketpair(domain, type, protocol, tab));
1239 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00001240 if (put_user_s32(tab[0], target_tab_addr)
1241 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1242 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001243 }
1244 return ret;
1245}
1246
ths0da46a62007-10-20 20:23:07 +00001247/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001248static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1249 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001250{
1251 void *addr;
1252 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001253 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001254
bellard579a97f2007-11-11 14:26:47 +00001255 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1256 if (!host_msg)
1257 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001258 if (target_addr) {
1259 addr = alloca(addrlen);
1260 target_to_host_sockaddr(addr, target_addr, addrlen);
1261 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1262 } else {
1263 ret = get_errno(send(fd, host_msg, len, flags));
1264 }
1265 unlock_user(host_msg, msg, 0);
1266 return ret;
1267}
1268
ths0da46a62007-10-20 20:23:07 +00001269/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001270static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1271 abi_ulong target_addr,
1272 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001273{
1274 socklen_t addrlen;
1275 void *addr;
1276 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001277 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001278
bellard579a97f2007-11-11 14:26:47 +00001279 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1280 if (!host_msg)
1281 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001282 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00001283 if (get_user_u32(addrlen, target_addrlen)) {
1284 ret = -TARGET_EFAULT;
1285 goto fail;
1286 }
pbrook1be9e1d2006-11-19 15:26:04 +00001287 addr = alloca(addrlen);
1288 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1289 } else {
1290 addr = NULL; /* To keep compiler quiet. */
1291 ret = get_errno(recv(fd, host_msg, len, flags));
1292 }
1293 if (!is_error(ret)) {
1294 if (target_addr) {
1295 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001296 if (put_user_u32(addrlen, target_addrlen)) {
1297 ret = -TARGET_EFAULT;
1298 goto fail;
1299 }
pbrook1be9e1d2006-11-19 15:26:04 +00001300 }
1301 unlock_user(host_msg, msg, len);
1302 } else {
bellard2f619692007-11-16 10:46:05 +00001303fail:
pbrook1be9e1d2006-11-19 15:26:04 +00001304 unlock_user(host_msg, msg, 0);
1305 }
1306 return ret;
1307}
1308
j_mayer32407102007-09-26 23:01:49 +00001309#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00001310/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001311static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00001312{
blueswir1992f48a2007-10-14 16:27:31 +00001313 abi_long ret;
1314 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00001315
1316 switch(num) {
1317 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00001318 {
bellard2f619692007-11-16 10:46:05 +00001319 int domain, type, protocol;
1320
1321 if (get_user_s32(domain, vptr)
1322 || get_user_s32(type, vptr + n)
1323 || get_user_s32(protocol, vptr + 2 * n))
1324 return -TARGET_EFAULT;
1325
bellard3532fa72006-06-24 15:06:03 +00001326 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00001327 }
bellard31e31b82003-02-18 22:55:36 +00001328 break;
1329 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00001330 {
bellard2f619692007-11-16 10:46:05 +00001331 int sockfd;
1332 abi_ulong target_addr;
1333 socklen_t addrlen;
1334
1335 if (get_user_s32(sockfd, vptr)
1336 || get_user_ual(target_addr, vptr + n)
1337 || get_user_u32(addrlen, vptr + 2 * n))
1338 return -TARGET_EFAULT;
1339
bellard3532fa72006-06-24 15:06:03 +00001340 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001341 }
bellard31e31b82003-02-18 22:55:36 +00001342 break;
1343 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00001344 {
bellard2f619692007-11-16 10:46:05 +00001345 int sockfd;
1346 abi_ulong target_addr;
1347 socklen_t addrlen;
1348
1349 if (get_user_s32(sockfd, vptr)
1350 || get_user_ual(target_addr, vptr + n)
1351 || get_user_u32(addrlen, vptr + 2 * n))
1352 return -TARGET_EFAULT;
1353
bellard3532fa72006-06-24 15:06:03 +00001354 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001355 }
bellard31e31b82003-02-18 22:55:36 +00001356 break;
1357 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00001358 {
bellard2f619692007-11-16 10:46:05 +00001359 int sockfd, backlog;
1360
1361 if (get_user_s32(sockfd, vptr)
1362 || get_user_s32(backlog, vptr + n))
1363 return -TARGET_EFAULT;
1364
bellard7854b052003-03-29 17:22:23 +00001365 ret = get_errno(listen(sockfd, backlog));
1366 }
bellard31e31b82003-02-18 22:55:36 +00001367 break;
1368 case SOCKOP_accept:
1369 {
bellard2f619692007-11-16 10:46:05 +00001370 int sockfd;
1371 abi_ulong target_addr, target_addrlen;
1372
1373 if (get_user_s32(sockfd, vptr)
1374 || get_user_ual(target_addr, vptr + n)
1375 || get_user_u32(target_addrlen, vptr + 2 * n))
1376 return -TARGET_EFAULT;
1377
pbrook1be9e1d2006-11-19 15:26:04 +00001378 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001379 }
1380 break;
1381 case SOCKOP_getsockname:
1382 {
bellard2f619692007-11-16 10:46:05 +00001383 int sockfd;
1384 abi_ulong target_addr, target_addrlen;
1385
1386 if (get_user_s32(sockfd, vptr)
1387 || get_user_ual(target_addr, vptr + n)
1388 || get_user_u32(target_addrlen, vptr + 2 * n))
1389 return -TARGET_EFAULT;
1390
pbrook1be9e1d2006-11-19 15:26:04 +00001391 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001392 }
1393 break;
1394 case SOCKOP_getpeername:
1395 {
bellard2f619692007-11-16 10:46:05 +00001396 int sockfd;
1397 abi_ulong target_addr, target_addrlen;
1398
1399 if (get_user_s32(sockfd, vptr)
1400 || get_user_ual(target_addr, vptr + n)
1401 || get_user_u32(target_addrlen, vptr + 2 * n))
1402 return -TARGET_EFAULT;
1403
pbrook1be9e1d2006-11-19 15:26:04 +00001404 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001405 }
1406 break;
1407 case SOCKOP_socketpair:
1408 {
bellard2f619692007-11-16 10:46:05 +00001409 int domain, type, protocol;
1410 abi_ulong tab;
1411
1412 if (get_user_s32(domain, vptr)
1413 || get_user_s32(type, vptr + n)
1414 || get_user_s32(protocol, vptr + 2 * n)
1415 || get_user_ual(tab, vptr + 3 * n))
1416 return -TARGET_EFAULT;
1417
pbrook1be9e1d2006-11-19 15:26:04 +00001418 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00001419 }
1420 break;
1421 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00001422 {
bellard2f619692007-11-16 10:46:05 +00001423 int sockfd;
1424 abi_ulong msg;
1425 size_t len;
1426 int flags;
1427
1428 if (get_user_s32(sockfd, vptr)
1429 || get_user_ual(msg, vptr + n)
1430 || get_user_ual(len, vptr + 2 * n)
1431 || get_user_s32(flags, vptr + 3 * n))
1432 return -TARGET_EFAULT;
1433
pbrook1be9e1d2006-11-19 15:26:04 +00001434 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001435 }
bellard31e31b82003-02-18 22:55:36 +00001436 break;
1437 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00001438 {
bellard2f619692007-11-16 10:46:05 +00001439 int sockfd;
1440 abi_ulong msg;
1441 size_t len;
1442 int flags;
1443
1444 if (get_user_s32(sockfd, vptr)
1445 || get_user_ual(msg, vptr + n)
1446 || get_user_ual(len, vptr + 2 * n)
1447 || get_user_s32(flags, vptr + 3 * n))
1448 return -TARGET_EFAULT;
1449
pbrook1be9e1d2006-11-19 15:26:04 +00001450 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001451 }
bellard31e31b82003-02-18 22:55:36 +00001452 break;
1453 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00001454 {
bellard2f619692007-11-16 10:46:05 +00001455 int sockfd;
1456 abi_ulong msg;
1457 size_t len;
1458 int flags;
1459 abi_ulong addr;
1460 socklen_t addrlen;
1461
1462 if (get_user_s32(sockfd, vptr)
1463 || get_user_ual(msg, vptr + n)
1464 || get_user_ual(len, vptr + 2 * n)
1465 || get_user_s32(flags, vptr + 3 * n)
1466 || get_user_ual(addr, vptr + 4 * n)
1467 || get_user_u32(addrlen, vptr + 5 * n))
1468 return -TARGET_EFAULT;
1469
pbrook1be9e1d2006-11-19 15:26:04 +00001470 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001471 }
bellard31e31b82003-02-18 22:55:36 +00001472 break;
1473 case SOCKOP_recvfrom:
1474 {
bellard2f619692007-11-16 10:46:05 +00001475 int sockfd;
1476 abi_ulong msg;
1477 size_t len;
1478 int flags;
1479 abi_ulong addr;
1480 socklen_t addrlen;
1481
1482 if (get_user_s32(sockfd, vptr)
1483 || get_user_ual(msg, vptr + n)
1484 || get_user_ual(len, vptr + 2 * n)
1485 || get_user_s32(flags, vptr + 3 * n)
1486 || get_user_ual(addr, vptr + 4 * n)
1487 || get_user_u32(addrlen, vptr + 5 * n))
1488 return -TARGET_EFAULT;
1489
pbrook1be9e1d2006-11-19 15:26:04 +00001490 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00001491 }
1492 break;
1493 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00001494 {
bellard2f619692007-11-16 10:46:05 +00001495 int sockfd, how;
1496
1497 if (get_user_s32(sockfd, vptr)
1498 || get_user_s32(how, vptr + n))
1499 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001500
1501 ret = get_errno(shutdown(sockfd, how));
1502 }
bellard31e31b82003-02-18 22:55:36 +00001503 break;
1504 case SOCKOP_sendmsg:
1505 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00001506 {
1507 int fd;
blueswir1992f48a2007-10-14 16:27:31 +00001508 abi_ulong target_msg;
bellard3532fa72006-06-24 15:06:03 +00001509 int flags;
bellard1a9353d2003-03-16 20:28:50 +00001510
bellard2f619692007-11-16 10:46:05 +00001511 if (get_user_s32(fd, vptr)
1512 || get_user_ual(target_msg, vptr + n)
1513 || get_user_s32(flags, vptr + 2 * n))
1514 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001515
ths5fafdf22007-09-16 21:08:06 +00001516 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00001517 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00001518 }
1519 break;
bellard31e31b82003-02-18 22:55:36 +00001520 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00001521 {
bellard2f619692007-11-16 10:46:05 +00001522 int sockfd;
1523 int level;
1524 int optname;
1525 abi_ulong optval;
1526 socklen_t optlen;
1527
1528 if (get_user_s32(sockfd, vptr)
1529 || get_user_s32(level, vptr + n)
1530 || get_user_s32(optname, vptr + 2 * n)
1531 || get_user_ual(optval, vptr + 3 * n)
1532 || get_user_u32(optlen, vptr + 4 * n))
1533 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001534
1535 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1536 }
1537 break;
bellard31e31b82003-02-18 22:55:36 +00001538 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00001539 {
bellard2f619692007-11-16 10:46:05 +00001540 int sockfd;
1541 int level;
1542 int optname;
1543 abi_ulong optval;
1544 socklen_t optlen;
bellard7854b052003-03-29 17:22:23 +00001545
bellard2f619692007-11-16 10:46:05 +00001546 if (get_user_s32(sockfd, vptr)
1547 || get_user_s32(level, vptr + n)
1548 || get_user_s32(optname, vptr + 2 * n)
1549 || get_user_ual(optval, vptr + 3 * n)
1550 || get_user_u32(optlen, vptr + 4 * n))
1551 return -TARGET_EFAULT;
1552
1553 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
bellard7854b052003-03-29 17:22:23 +00001554 }
1555 break;
bellard31e31b82003-02-18 22:55:36 +00001556 default:
1557 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00001558 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00001559 break;
1560 }
1561 return ret;
1562}
j_mayer32407102007-09-26 23:01:49 +00001563#endif
bellard31e31b82003-02-18 22:55:36 +00001564
j_mayer32407102007-09-26 23:01:49 +00001565#ifdef TARGET_NR_ipc
bellard8853f862004-02-22 14:57:26 +00001566#define N_SHM_REGIONS 32
1567
1568static struct shm_region {
bellard5a4a8982007-11-11 17:39:18 +00001569 abi_ulong start;
1570 abi_ulong size;
bellard8853f862004-02-22 14:57:26 +00001571} shm_regions[N_SHM_REGIONS];
1572
ths3eb6b042007-06-03 14:26:27 +00001573struct target_ipc_perm
1574{
blueswir1992f48a2007-10-14 16:27:31 +00001575 abi_long __key;
1576 abi_ulong uid;
1577 abi_ulong gid;
1578 abi_ulong cuid;
1579 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00001580 unsigned short int mode;
1581 unsigned short int __pad1;
1582 unsigned short int __seq;
1583 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00001584 abi_ulong __unused1;
1585 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00001586};
1587
1588struct target_semid_ds
1589{
1590 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001591 abi_ulong sem_otime;
1592 abi_ulong __unused1;
1593 abi_ulong sem_ctime;
1594 abi_ulong __unused2;
1595 abi_ulong sem_nsems;
1596 abi_ulong __unused3;
1597 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00001598};
1599
bellard579a97f2007-11-11 14:26:47 +00001600static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
1601 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001602{
1603 struct target_ipc_perm *target_ip;
1604 struct target_semid_ds *target_sd;
1605
bellard579a97f2007-11-11 14:26:47 +00001606 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1607 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001608 target_ip=&(target_sd->sem_perm);
1609 host_ip->__key = tswapl(target_ip->__key);
1610 host_ip->uid = tswapl(target_ip->uid);
1611 host_ip->gid = tswapl(target_ip->gid);
1612 host_ip->cuid = tswapl(target_ip->cuid);
1613 host_ip->cgid = tswapl(target_ip->cgid);
1614 host_ip->mode = tswapl(target_ip->mode);
1615 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001616 return 0;
ths3eb6b042007-06-03 14:26:27 +00001617}
1618
bellard579a97f2007-11-11 14:26:47 +00001619static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
1620 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00001621{
1622 struct target_ipc_perm *target_ip;
1623 struct target_semid_ds *target_sd;
1624
bellard579a97f2007-11-11 14:26:47 +00001625 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1626 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001627 target_ip = &(target_sd->sem_perm);
1628 target_ip->__key = tswapl(host_ip->__key);
1629 target_ip->uid = tswapl(host_ip->uid);
1630 target_ip->gid = tswapl(host_ip->gid);
1631 target_ip->cuid = tswapl(host_ip->cuid);
1632 target_ip->cgid = tswapl(host_ip->cgid);
1633 target_ip->mode = tswapl(host_ip->mode);
1634 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001635 return 0;
ths3eb6b042007-06-03 14:26:27 +00001636}
1637
bellard579a97f2007-11-11 14:26:47 +00001638static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
1639 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001640{
1641 struct target_semid_ds *target_sd;
1642
bellard579a97f2007-11-11 14:26:47 +00001643 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1644 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001645 target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1646 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1647 host_sd->sem_otime = tswapl(target_sd->sem_otime);
1648 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1649 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001650 return 0;
ths3eb6b042007-06-03 14:26:27 +00001651}
1652
bellard579a97f2007-11-11 14:26:47 +00001653static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
1654 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00001655{
1656 struct target_semid_ds *target_sd;
1657
bellard579a97f2007-11-11 14:26:47 +00001658 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1659 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001660 host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1661 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1662 target_sd->sem_otime = tswapl(host_sd->sem_otime);
1663 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1664 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001665 return 0;
ths3eb6b042007-06-03 14:26:27 +00001666}
1667
thsfa294812007-02-02 22:05:00 +00001668union semun {
1669 int val;
ths3eb6b042007-06-03 14:26:27 +00001670 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00001671 unsigned short *array;
1672};
1673
ths3eb6b042007-06-03 14:26:27 +00001674union target_semun {
1675 int val;
blueswir1992f48a2007-10-14 16:27:31 +00001676 abi_long buf;
ths3eb6b042007-06-03 14:26:27 +00001677 unsigned short int *array;
1678};
1679
bellard579a97f2007-11-11 14:26:47 +00001680static inline abi_long target_to_host_semun(int cmd,
1681 union semun *host_su,
1682 abi_ulong target_addr,
1683 struct semid_ds *ds)
ths3eb6b042007-06-03 14:26:27 +00001684{
1685 union target_semun *target_su;
1686
1687 switch( cmd ) {
1688 case IPC_STAT:
1689 case IPC_SET:
bellard579a97f2007-11-11 14:26:47 +00001690 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1691 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001692 target_to_host_semid_ds(ds,target_su->buf);
1693 host_su->buf = ds;
1694 unlock_user_struct(target_su, target_addr, 0);
1695 break;
1696 case GETVAL:
1697 case SETVAL:
bellard579a97f2007-11-11 14:26:47 +00001698 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1699 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001700 host_su->val = tswapl(target_su->val);
1701 unlock_user_struct(target_su, target_addr, 0);
1702 break;
1703 case GETALL:
1704 case SETALL:
bellard579a97f2007-11-11 14:26:47 +00001705 if (!lock_user_struct(VERIFY_READ, target_su, target_addr, 1))
1706 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001707 *host_su->array = tswap16(*target_su->array);
1708 unlock_user_struct(target_su, target_addr, 0);
1709 break;
1710 default:
1711 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1712 }
bellard579a97f2007-11-11 14:26:47 +00001713 return 0;
ths3eb6b042007-06-03 14:26:27 +00001714}
1715
bellard579a97f2007-11-11 14:26:47 +00001716static inline abi_long host_to_target_semun(int cmd,
1717 abi_ulong target_addr,
1718 union semun *host_su,
1719 struct semid_ds *ds)
ths3eb6b042007-06-03 14:26:27 +00001720{
1721 union target_semun *target_su;
1722
1723 switch( cmd ) {
1724 case IPC_STAT:
1725 case IPC_SET:
bellard579a97f2007-11-11 14:26:47 +00001726 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1727 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001728 host_to_target_semid_ds(target_su->buf,ds);
1729 unlock_user_struct(target_su, target_addr, 1);
1730 break;
1731 case GETVAL:
1732 case SETVAL:
bellard579a97f2007-11-11 14:26:47 +00001733 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1734 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001735 target_su->val = tswapl(host_su->val);
1736 unlock_user_struct(target_su, target_addr, 1);
1737 break;
1738 case GETALL:
1739 case SETALL:
bellard579a97f2007-11-11 14:26:47 +00001740 if (lock_user_struct(VERIFY_WRITE, target_su, target_addr, 0))
1741 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001742 *target_su->array = tswap16(*host_su->array);
1743 unlock_user_struct(target_su, target_addr, 1);
1744 break;
1745 default:
1746 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1747 }
bellard579a97f2007-11-11 14:26:47 +00001748 return 0;
ths3eb6b042007-06-03 14:26:27 +00001749}
1750
blueswir1992f48a2007-10-14 16:27:31 +00001751static inline abi_long do_semctl(int first, int second, int third,
1752 abi_long ptr)
ths3eb6b042007-06-03 14:26:27 +00001753{
1754 union semun arg;
1755 struct semid_ds dsarg;
1756 int cmd = third&0xff;
blueswir1992f48a2007-10-14 16:27:31 +00001757 abi_long ret = 0;
ths3eb6b042007-06-03 14:26:27 +00001758
1759 switch( cmd ) {
1760 case GETVAL:
1761 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1762 ret = get_errno(semctl(first, second, cmd, arg));
1763 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1764 break;
1765 case SETVAL:
1766 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1767 ret = get_errno(semctl(first, second, cmd, arg));
1768 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1769 break;
1770 case GETALL:
1771 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1772 ret = get_errno(semctl(first, second, cmd, arg));
1773 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1774 break;
1775 case SETALL:
1776 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1777 ret = get_errno(semctl(first, second, cmd, arg));
1778 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1779 break;
1780 case IPC_STAT:
1781 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1782 ret = get_errno(semctl(first, second, cmd, arg));
1783 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1784 break;
1785 case IPC_SET:
1786 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1787 ret = get_errno(semctl(first, second, cmd, arg));
1788 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1789 break;
1790 default:
1791 ret = get_errno(semctl(first, second, cmd, arg));
1792 }
1793
1794 return ret;
1795}
1796
ths1bc012f2007-06-03 14:27:49 +00001797struct target_msqid_ds
1798{
1799 struct target_ipc_perm msg_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001800 abi_ulong msg_stime;
1801 abi_ulong __unused1;
1802 abi_ulong msg_rtime;
1803 abi_ulong __unused2;
1804 abi_ulong msg_ctime;
1805 abi_ulong __unused3;
1806 abi_ulong __msg_cbytes;
1807 abi_ulong msg_qnum;
1808 abi_ulong msg_qbytes;
1809 abi_ulong msg_lspid;
1810 abi_ulong msg_lrpid;
1811 abi_ulong __unused4;
1812 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00001813};
1814
bellard579a97f2007-11-11 14:26:47 +00001815static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
1816 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00001817{
1818 struct target_msqid_ds *target_md;
1819
bellard579a97f2007-11-11 14:26:47 +00001820 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
1821 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001822 target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1823 host_md->msg_stime = tswapl(target_md->msg_stime);
1824 host_md->msg_rtime = tswapl(target_md->msg_rtime);
1825 host_md->msg_ctime = tswapl(target_md->msg_ctime);
1826 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1827 host_md->msg_qnum = tswapl(target_md->msg_qnum);
1828 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1829 host_md->msg_lspid = tswapl(target_md->msg_lspid);
1830 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1831 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001832 return 0;
ths1bc012f2007-06-03 14:27:49 +00001833}
1834
bellard579a97f2007-11-11 14:26:47 +00001835static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
1836 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00001837{
1838 struct target_msqid_ds *target_md;
1839
bellard579a97f2007-11-11 14:26:47 +00001840 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
1841 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001842 host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1843 target_md->msg_stime = tswapl(host_md->msg_stime);
1844 target_md->msg_rtime = tswapl(host_md->msg_rtime);
1845 target_md->msg_ctime = tswapl(host_md->msg_ctime);
1846 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1847 target_md->msg_qnum = tswapl(host_md->msg_qnum);
1848 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1849 target_md->msg_lspid = tswapl(host_md->msg_lspid);
1850 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1851 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001852 return 0;
ths1bc012f2007-06-03 14:27:49 +00001853}
1854
blueswir1992f48a2007-10-14 16:27:31 +00001855static inline abi_long do_msgctl(int first, int second, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00001856{
1857 struct msqid_ds dsarg;
1858 int cmd = second&0xff;
blueswir1992f48a2007-10-14 16:27:31 +00001859 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001860 switch( cmd ) {
1861 case IPC_STAT:
1862 case IPC_SET:
1863 target_to_host_msqid_ds(&dsarg,ptr);
1864 ret = get_errno(msgctl(first, cmd, &dsarg));
1865 host_to_target_msqid_ds(ptr,&dsarg);
1866 default:
1867 ret = get_errno(msgctl(first, cmd, &dsarg));
1868 }
1869 return ret;
1870}
1871
1872struct target_msgbuf {
blueswir1992f48a2007-10-14 16:27:31 +00001873 abi_ulong mtype;
ths1bc012f2007-06-03 14:27:49 +00001874 char mtext[1];
1875};
1876
blueswir1992f48a2007-10-14 16:27:31 +00001877static inline abi_long do_msgsnd(int msqid, abi_long msgp,
1878 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001879{
1880 struct target_msgbuf *target_mb;
1881 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00001882 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001883
bellard579a97f2007-11-11 14:26:47 +00001884 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
1885 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001886 host_mb = malloc(msgsz+sizeof(long));
1887 host_mb->mtype = tswapl(target_mb->mtype);
1888 memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1889 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1890 free(host_mb);
1891 unlock_user_struct(target_mb, msgp, 0);
1892
1893 return ret;
1894}
1895
blueswir1992f48a2007-10-14 16:27:31 +00001896static inline abi_long do_msgrcv(int msqid, abi_long msgp,
1897 unsigned int msgsz, int msgtype,
1898 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001899{
1900 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00001901 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00001902 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00001903 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001904
bellard579a97f2007-11-11 14:26:47 +00001905 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
1906 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00001907 host_mb = malloc(msgsz+sizeof(long));
1908 ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
bellard579a97f2007-11-11 14:26:47 +00001909 if (ret > 0) {
1910 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
1911 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
1912 if (!target_mtext) {
1913 ret = -TARGET_EFAULT;
1914 goto end;
1915 }
ths1bc012f2007-06-03 14:27:49 +00001916 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00001917 unlock_user(target_mtext, target_mtext_addr, ret);
1918 }
ths1bc012f2007-06-03 14:27:49 +00001919 target_mb->mtype = tswapl(host_mb->mtype);
1920 free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00001921
bellard579a97f2007-11-11 14:26:47 +00001922end:
1923 if (target_mb)
1924 unlock_user_struct(target_mb, msgp, 1);
ths1bc012f2007-06-03 14:27:49 +00001925 return ret;
1926}
1927
pbrook53a59602006-03-25 19:31:22 +00001928/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00001929/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001930static abi_long do_ipc(unsigned int call, int first,
1931 int second, int third,
1932 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00001933{
1934 int version;
blueswir1992f48a2007-10-14 16:27:31 +00001935 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00001936 struct shmid_ds shm_info;
1937 int i;
1938
1939 version = call >> 16;
1940 call &= 0xffff;
1941
1942 switch (call) {
thsfa294812007-02-02 22:05:00 +00001943 case IPCOP_semop:
bellard579a97f2007-11-11 14:26:47 +00001944 ret = get_errno(semop(first,(struct sembuf *)g2h(ptr), second));
thsfa294812007-02-02 22:05:00 +00001945 break;
1946
1947 case IPCOP_semget:
1948 ret = get_errno(semget(first, second, third));
1949 break;
1950
1951 case IPCOP_semctl:
ths3eb6b042007-06-03 14:26:27 +00001952 ret = do_semctl(first, second, third, ptr);
thsfa294812007-02-02 22:05:00 +00001953 break;
1954
1955 case IPCOP_semtimedop:
j_mayer32407102007-09-26 23:01:49 +00001956 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00001957 ret = -TARGET_ENOSYS;
thsfa294812007-02-02 22:05:00 +00001958 break;
thsd96372e2007-02-02 22:05:44 +00001959
1960 case IPCOP_msgget:
1961 ret = get_errno(msgget(first, second));
1962 break;
1963
1964 case IPCOP_msgsnd:
ths1bc012f2007-06-03 14:27:49 +00001965 ret = do_msgsnd(first, ptr, second, third);
thsd96372e2007-02-02 22:05:44 +00001966 break;
1967
1968 case IPCOP_msgctl:
ths1bc012f2007-06-03 14:27:49 +00001969 ret = do_msgctl(first, second, ptr);
thsd96372e2007-02-02 22:05:44 +00001970 break;
1971
1972 case IPCOP_msgrcv:
ths1bc012f2007-06-03 14:27:49 +00001973 {
bellard579a97f2007-11-11 14:26:47 +00001974 /* XXX: this code is not correct */
ths1bc012f2007-06-03 14:27:49 +00001975 struct ipc_kludge
1976 {
1977 void *__unbounded msgp;
1978 long int msgtyp;
1979 };
thsd96372e2007-02-02 22:05:44 +00001980
bellard579a97f2007-11-11 14:26:47 +00001981 struct ipc_kludge *foo = (struct ipc_kludge *)g2h(ptr);
ths1bc012f2007-06-03 14:27:49 +00001982 struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
thsd96372e2007-02-02 22:05:44 +00001983
ths1bc012f2007-06-03 14:27:49 +00001984 ret = do_msgrcv(first, (long)msgp, second, 0, third);
thsd96372e2007-02-02 22:05:44 +00001985
ths1bc012f2007-06-03 14:27:49 +00001986 }
thsd96372e2007-02-02 22:05:44 +00001987 break;
1988
bellard8853f862004-02-22 14:57:26 +00001989 case IPCOP_shmat:
bellard5a4a8982007-11-11 17:39:18 +00001990 {
1991 abi_ulong raddr;
1992 void *host_addr;
1993 /* SHM_* flags are the same on all linux platforms */
1994 host_addr = shmat(first, (void *)g2h(ptr), second);
1995 if (host_addr == (void *)-1) {
1996 ret = get_errno((long)host_addr);
bellard8853f862004-02-22 14:57:26 +00001997 break;
bellard5a4a8982007-11-11 17:39:18 +00001998 }
1999 raddr = h2g((unsigned long)host_addr);
2000 /* find out the length of the shared memory segment */
2001
2002 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
2003 if (is_error(ret)) {
2004 /* can't get length, bail out */
2005 shmdt(host_addr);
2006 break;
2007 }
2008 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2009 PAGE_VALID | PAGE_READ |
2010 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
2011 for (i = 0; i < N_SHM_REGIONS; ++i) {
2012 if (shm_regions[i].start == 0) {
2013 shm_regions[i].start = raddr;
2014 shm_regions[i].size = shm_info.shm_segsz;
2015 break;
2016 }
2017 }
bellard2f619692007-11-16 10:46:05 +00002018 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00002019 return -TARGET_EFAULT;
2020 ret = 0;
2021 }
bellard8853f862004-02-22 14:57:26 +00002022 break;
2023 case IPCOP_shmdt:
2024 for (i = 0; i < N_SHM_REGIONS; ++i) {
2025 if (shm_regions[i].start == ptr) {
2026 shm_regions[i].start = 0;
2027 page_set_flags(ptr, shm_regions[i].size, 0);
2028 break;
2029 }
2030 }
bellard5a4a8982007-11-11 17:39:18 +00002031 ret = get_errno(shmdt((void *)g2h(ptr)));
bellard8853f862004-02-22 14:57:26 +00002032 break;
2033
2034 case IPCOP_shmget:
2035 /* IPC_* flag values are the same on all linux platforms */
2036 ret = get_errno(shmget(first, second, third));
2037 break;
2038
2039 /* IPC_* and SHM_* command values are the same on all linux platforms */
2040 case IPCOP_shmctl:
2041 switch(second) {
2042 case IPC_RMID:
2043 case SHM_LOCK:
2044 case SHM_UNLOCK:
2045 ret = get_errno(shmctl(first, second, NULL));
2046 break;
2047 default:
2048 goto unimplemented;
2049 }
2050 break;
2051 default:
2052 unimplemented:
j_mayer32407102007-09-26 23:01:49 +00002053 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00002054 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00002055 break;
2056 }
2057 return ret;
2058}
j_mayer32407102007-09-26 23:01:49 +00002059#endif
bellard8853f862004-02-22 14:57:26 +00002060
bellard31e31b82003-02-18 22:55:36 +00002061/* kernel structure types definitions */
2062#define IFNAMSIZ 16
2063
2064#define STRUCT(name, list...) STRUCT_ ## name,
2065#define STRUCT_SPECIAL(name) STRUCT_ ## name,
2066enum {
2067#include "syscall_types.h"
2068};
2069#undef STRUCT
2070#undef STRUCT_SPECIAL
2071
2072#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
2073#define STRUCT_SPECIAL(name)
2074#include "syscall_types.h"
2075#undef STRUCT
2076#undef STRUCT_SPECIAL
2077
2078typedef struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00002079 unsigned int target_cmd;
2080 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00002081 const char *name;
2082 int access;
bellard1a9353d2003-03-16 20:28:50 +00002083 const argtype arg_type[5];
bellard31e31b82003-02-18 22:55:36 +00002084} IOCTLEntry;
2085
2086#define IOC_R 0x0001
2087#define IOC_W 0x0002
2088#define IOC_RW (IOC_R | IOC_W)
2089
2090#define MAX_STRUCT_SIZE 4096
2091
bellard2ab83ea2003-06-15 19:56:46 +00002092IOCTLEntry ioctl_entries[] = {
bellard31e31b82003-02-18 22:55:36 +00002093#define IOCTL(cmd, access, types...) \
2094 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
2095#include "ioctls.h"
2096 { 0, 0, },
2097};
2098
pbrook53a59602006-03-25 19:31:22 +00002099/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00002100/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002101static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00002102{
2103 const IOCTLEntry *ie;
2104 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00002105 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00002106 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00002107 int target_size;
2108 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00002109
2110 ie = ioctl_entries;
2111 for(;;) {
2112 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00002113 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00002114 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002115 }
2116 if (ie->target_cmd == cmd)
2117 break;
2118 ie++;
2119 }
2120 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00002121#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00002122 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00002123#endif
bellard31e31b82003-02-18 22:55:36 +00002124 switch(arg_type[0]) {
2125 case TYPE_NULL:
2126 /* no argument */
2127 ret = get_errno(ioctl(fd, ie->host_cmd));
2128 break;
2129 case TYPE_PTRVOID:
2130 case TYPE_INT:
2131 /* int argment */
2132 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2133 break;
2134 case TYPE_PTR:
2135 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00002136 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00002137 switch(ie->access) {
2138 case IOC_R:
2139 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2140 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002141 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2142 if (!argptr)
2143 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002144 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2145 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00002146 }
2147 break;
2148 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00002149 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2150 if (!argptr)
2151 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002152 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2153 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002154 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2155 break;
2156 default:
2157 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00002158 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2159 if (!argptr)
2160 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002161 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2162 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002163 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2164 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002165 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2166 if (!argptr)
2167 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002168 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2169 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00002170 }
2171 break;
2172 }
2173 break;
2174 default:
j_mayer32407102007-09-26 23:01:49 +00002175 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2176 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00002177 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002178 break;
2179 }
2180 return ret;
2181}
2182
2183bitmask_transtbl iflag_tbl[] = {
2184 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2185 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2186 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2187 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2188 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2189 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2190 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2191 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2192 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2193 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2194 { TARGET_IXON, TARGET_IXON, IXON, IXON },
2195 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2196 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
2197 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
2198 { 0, 0, 0, 0 }
2199};
2200
2201bitmask_transtbl oflag_tbl[] = {
2202 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
2203 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
2204 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
2205 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
2206 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
2207 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
2208 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
2209 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
2210 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
2211 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
2212 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
2213 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
2214 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
2215 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
2216 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
2217 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
2218 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
2219 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
2220 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
2221 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
2222 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
2223 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
2224 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
2225 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
2226 { 0, 0, 0, 0 }
2227};
2228
2229bitmask_transtbl cflag_tbl[] = {
2230 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
2231 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
2232 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
2233 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
2234 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
2235 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
2236 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
2237 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
2238 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
2239 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
2240 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
2241 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
2242 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
2243 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
2244 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
2245 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
2246 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
2247 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
2248 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
2249 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
2250 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
2251 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
2252 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
2253 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
2254 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
2255 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
2256 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
2257 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
2258 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
2259 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
2260 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
2261 { 0, 0, 0, 0 }
2262};
2263
2264bitmask_transtbl lflag_tbl[] = {
2265 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2266 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2267 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2268 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2269 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2270 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2271 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2272 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2273 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2274 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2275 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2276 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2277 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2278 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2279 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
2280 { 0, 0, 0, 0 }
2281};
2282
2283static void target_to_host_termios (void *dst, const void *src)
2284{
2285 struct host_termios *host = dst;
2286 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00002287
ths5fafdf22007-09-16 21:08:06 +00002288 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002289 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002290 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002291 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002292 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002293 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002294 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002295 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2296 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00002297
ths5fafdf22007-09-16 21:08:06 +00002298 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2299 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00002300 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00002301 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00002302 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00002303 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00002304 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00002305 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00002306 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00002307 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2308 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00002309 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2310 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2311 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2312 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2313 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00002314 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00002315}
ths3b46e622007-09-17 08:09:54 +00002316
bellard31e31b82003-02-18 22:55:36 +00002317static void host_to_target_termios (void *dst, const void *src)
2318{
2319 struct target_termios *target = dst;
2320 const struct host_termios *host = src;
2321
ths5fafdf22007-09-16 21:08:06 +00002322 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002323 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002324 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002325 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002326 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002327 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002328 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002329 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2330 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00002331
bellard31e31b82003-02-18 22:55:36 +00002332 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2333 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2334 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2335 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2336 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2337 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2338 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2339 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2340 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2341 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2342 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2343 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2344 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2345 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2346 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2347 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2348 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2349}
2350
2351StructEntry struct_termios_def = {
2352 .convert = { host_to_target_termios, target_to_host_termios },
2353 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2354 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2355};
2356
bellard5286db72003-06-05 00:57:30 +00002357static bitmask_transtbl mmap_flags_tbl[] = {
2358 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2359 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2360 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2361 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2362 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2363 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2364 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2365 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2366 { 0, 0, 0, 0 }
2367};
2368
bellardffa65c32004-01-04 23:57:22 +00002369static bitmask_transtbl fcntl_flags_tbl[] = {
2370 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
2371 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
2372 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
2373 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
2374 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
2375 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
2376 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
2377 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
2378 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
2379 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
2380 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2381 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
2382 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
bellard121061d2004-02-22 11:56:01 +00002383#if defined(O_DIRECT)
bellardffa65c32004-01-04 23:57:22 +00002384 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
bellard121061d2004-02-22 11:56:01 +00002385#endif
bellardffa65c32004-01-04 23:57:22 +00002386 { 0, 0, 0, 0 }
2387};
2388
bellard2ab83ea2003-06-15 19:56:46 +00002389#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00002390
2391/* NOTE: there is really one LDT for all the threads */
2392uint8_t *ldt_table;
2393
bellard03acab62007-11-11 14:57:14 +00002394static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002395{
2396 int size;
pbrook53a59602006-03-25 19:31:22 +00002397 void *p;
bellard6dbad632003-03-16 18:05:05 +00002398
2399 if (!ldt_table)
2400 return 0;
2401 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2402 if (size > bytecount)
2403 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00002404 p = lock_user(VERIFY_WRITE, ptr, size, 0);
2405 if (!p)
bellard03acab62007-11-11 14:57:14 +00002406 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00002407 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00002408 memcpy(p, ldt_table, size);
2409 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00002410 return size;
2411}
2412
2413/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00002414static abi_long write_ldt(CPUX86State *env,
2415 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00002416{
2417 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00002418 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00002419 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00002420 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00002421 uint32_t *lp, entry_1, entry_2;
2422
2423 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00002424 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00002425 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00002426 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002427 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2428 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2429 ldt_info.limit = tswap32(target_ldt_info->limit);
2430 ldt_info.flags = tswap32(target_ldt_info->flags);
2431 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00002432
bellard6dbad632003-03-16 18:05:05 +00002433 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00002434 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002435 seg_32bit = ldt_info.flags & 1;
2436 contents = (ldt_info.flags >> 1) & 3;
2437 read_exec_only = (ldt_info.flags >> 3) & 1;
2438 limit_in_pages = (ldt_info.flags >> 4) & 1;
2439 seg_not_present = (ldt_info.flags >> 5) & 1;
2440 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00002441#ifdef TARGET_ABI32
2442 lm = 0;
2443#else
2444 lm = (ldt_info.flags >> 7) & 1;
2445#endif
bellard6dbad632003-03-16 18:05:05 +00002446 if (contents == 3) {
2447 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00002448 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002449 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00002450 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002451 }
2452 /* allocate the LDT */
2453 if (!ldt_table) {
2454 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2455 if (!ldt_table)
bellard03acab62007-11-11 14:57:14 +00002456 return -TARGET_ENOMEM;
bellard6dbad632003-03-16 18:05:05 +00002457 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellardeeeac3f2007-11-11 19:34:32 +00002458 env->ldt.base = h2g((unsigned long)ldt_table);
bellard6dbad632003-03-16 18:05:05 +00002459 env->ldt.limit = 0xffff;
2460 }
2461
2462 /* NOTE: same code as Linux kernel */
2463 /* Allow LDTs to be cleared by the user. */
2464 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2465 if (oldmode ||
2466 (contents == 0 &&
2467 read_exec_only == 1 &&
2468 seg_32bit == 0 &&
2469 limit_in_pages == 0 &&
2470 seg_not_present == 1 &&
2471 useable == 0 )) {
2472 entry_1 = 0;
2473 entry_2 = 0;
2474 goto install;
2475 }
2476 }
ths3b46e622007-09-17 08:09:54 +00002477
bellard6dbad632003-03-16 18:05:05 +00002478 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2479 (ldt_info.limit & 0x0ffff);
2480 entry_2 = (ldt_info.base_addr & 0xff000000) |
2481 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2482 (ldt_info.limit & 0xf0000) |
2483 ((read_exec_only ^ 1) << 9) |
2484 (contents << 10) |
2485 ((seg_not_present ^ 1) << 15) |
2486 (seg_32bit << 22) |
2487 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00002488 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00002489 0x7000;
2490 if (!oldmode)
2491 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00002492
bellard6dbad632003-03-16 18:05:05 +00002493 /* Install the new entry ... */
2494install:
2495 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2496 lp[0] = tswap32(entry_1);
2497 lp[1] = tswap32(entry_2);
2498 return 0;
2499}
2500
2501/* specific and weird i386 syscalls */
bellard03acab62007-11-11 14:57:14 +00002502abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
2503 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002504{
bellard03acab62007-11-11 14:57:14 +00002505 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00002506
bellard6dbad632003-03-16 18:05:05 +00002507 switch (func) {
2508 case 0:
2509 ret = read_ldt(ptr, bytecount);
2510 break;
2511 case 1:
2512 ret = write_ldt(env, ptr, bytecount, 1);
2513 break;
2514 case 0x11:
2515 ret = write_ldt(env, ptr, bytecount, 0);
2516 break;
bellard03acab62007-11-11 14:57:14 +00002517 default:
2518 ret = -TARGET_ENOSYS;
2519 break;
bellard6dbad632003-03-16 18:05:05 +00002520 }
2521 return ret;
2522}
bellard1b6b0292003-03-22 17:31:38 +00002523
bellard8d18e892007-11-14 15:18:40 +00002524abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
2525{
2526 uint64_t *gdt_table = g2h(env->gdt.base);
2527 struct target_modify_ldt_ldt_s ldt_info;
2528 struct target_modify_ldt_ldt_s *target_ldt_info;
2529 int seg_32bit, contents, read_exec_only, limit_in_pages;
2530 int seg_not_present, useable, lm;
2531 uint32_t *lp, entry_1, entry_2;
2532 int i;
2533
2534 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
2535 if (!target_ldt_info)
2536 return -TARGET_EFAULT;
2537 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2538 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2539 ldt_info.limit = tswap32(target_ldt_info->limit);
2540 ldt_info.flags = tswap32(target_ldt_info->flags);
2541 if (ldt_info.entry_number == -1) {
2542 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
2543 if (gdt_table[i] == 0) {
2544 ldt_info.entry_number = i;
2545 target_ldt_info->entry_number = tswap32(i);
2546 break;
2547 }
2548 }
2549 }
2550 unlock_user_struct(target_ldt_info, ptr, 1);
2551
2552 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
2553 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
2554 return -TARGET_EINVAL;
2555 seg_32bit = ldt_info.flags & 1;
2556 contents = (ldt_info.flags >> 1) & 3;
2557 read_exec_only = (ldt_info.flags >> 3) & 1;
2558 limit_in_pages = (ldt_info.flags >> 4) & 1;
2559 seg_not_present = (ldt_info.flags >> 5) & 1;
2560 useable = (ldt_info.flags >> 6) & 1;
2561#ifdef TARGET_ABI32
2562 lm = 0;
2563#else
2564 lm = (ldt_info.flags >> 7) & 1;
2565#endif
2566
2567 if (contents == 3) {
2568 if (seg_not_present == 0)
2569 return -TARGET_EINVAL;
2570 }
2571
2572 /* NOTE: same code as Linux kernel */
2573 /* Allow LDTs to be cleared by the user. */
2574 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2575 if ((contents == 0 &&
2576 read_exec_only == 1 &&
2577 seg_32bit == 0 &&
2578 limit_in_pages == 0 &&
2579 seg_not_present == 1 &&
2580 useable == 0 )) {
2581 entry_1 = 0;
2582 entry_2 = 0;
2583 goto install;
2584 }
2585 }
2586
2587 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2588 (ldt_info.limit & 0x0ffff);
2589 entry_2 = (ldt_info.base_addr & 0xff000000) |
2590 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2591 (ldt_info.limit & 0xf0000) |
2592 ((read_exec_only ^ 1) << 9) |
2593 (contents << 10) |
2594 ((seg_not_present ^ 1) << 15) |
2595 (seg_32bit << 22) |
2596 (limit_in_pages << 23) |
2597 (useable << 20) |
2598 (lm << 21) |
2599 0x7000;
2600
2601 /* Install the new entry ... */
2602install:
2603 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
2604 lp[0] = tswap32(entry_1);
2605 lp[1] = tswap32(entry_2);
2606 return 0;
2607}
2608
2609abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
2610{
2611 struct target_modify_ldt_ldt_s *target_ldt_info;
2612 uint64_t *gdt_table = g2h(env->gdt.base);
2613 uint32_t base_addr, limit, flags;
2614 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
2615 int seg_not_present, useable, lm;
2616 uint32_t *lp, entry_1, entry_2;
2617
2618 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
2619 if (!target_ldt_info)
2620 return -TARGET_EFAULT;
2621 idx = tswap32(target_ldt_info->entry_number);
2622 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
2623 idx > TARGET_GDT_ENTRY_TLS_MAX) {
2624 unlock_user_struct(target_ldt_info, ptr, 1);
2625 return -TARGET_EINVAL;
2626 }
2627 lp = (uint32_t *)(gdt_table + idx);
2628 entry_1 = tswap32(lp[0]);
2629 entry_2 = tswap32(lp[1]);
2630
2631 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
2632 contents = (entry_2 >> 10) & 3;
2633 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
2634 seg_32bit = (entry_2 >> 22) & 1;
2635 limit_in_pages = (entry_2 >> 23) & 1;
2636 useable = (entry_2 >> 20) & 1;
2637#ifdef TARGET_ABI32
2638 lm = 0;
2639#else
2640 lm = (entry_2 >> 21) & 1;
2641#endif
2642 flags = (seg_32bit << 0) | (contents << 1) |
2643 (read_exec_only << 3) | (limit_in_pages << 4) |
2644 (seg_not_present << 5) | (useable << 6) | (lm << 7);
2645 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
2646 base_addr = (entry_1 >> 16) |
2647 (entry_2 & 0xff000000) |
2648 ((entry_2 & 0xff) << 16);
2649 target_ldt_info->base_addr = tswapl(base_addr);
2650 target_ldt_info->limit = tswap32(limit);
2651 target_ldt_info->flags = tswap32(flags);
2652 unlock_user_struct(target_ldt_info, ptr, 1);
2653 return 0;
2654}
2655
bellardd2fd1af2007-11-14 18:08:56 +00002656#ifndef TARGET_ABI32
2657abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
2658{
2659 abi_long ret;
2660 abi_ulong val;
2661 int idx;
2662
2663 switch(code) {
2664 case TARGET_ARCH_SET_GS:
2665 case TARGET_ARCH_SET_FS:
2666 if (code == TARGET_ARCH_SET_GS)
2667 idx = R_GS;
2668 else
2669 idx = R_FS;
2670 cpu_x86_load_seg(env, idx, 0);
2671 env->segs[idx].base = addr;
2672 break;
2673 case TARGET_ARCH_GET_GS:
2674 case TARGET_ARCH_GET_FS:
2675 if (code == TARGET_ARCH_GET_GS)
2676 idx = R_GS;
2677 else
2678 idx = R_FS;
2679 val = env->segs[idx].base;
2680 if (put_user(val, addr, abi_ulong))
2681 return -TARGET_EFAULT;
2682 break;
2683 default:
2684 ret = -TARGET_EINVAL;
2685 break;
2686 }
2687 return 0;
2688}
2689#endif
2690
bellard2ab83ea2003-06-15 19:56:46 +00002691#endif /* defined(TARGET_I386) */
2692
bellard1b6b0292003-03-22 17:31:38 +00002693/* this stack is the equivalent of the kernel stack associated with a
2694 thread/process */
2695#define NEW_STACK_SIZE 8192
2696
2697static int clone_func(void *arg)
2698{
bellard2ab83ea2003-06-15 19:56:46 +00002699 CPUState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00002700 cpu_loop(env);
2701 /* never exits */
2702 return 0;
2703}
2704
ths0da46a62007-10-20 20:23:07 +00002705/* do_fork() Must return host values and target errnos (unlike most
2706 do_*() functions). */
blueswir1992f48a2007-10-14 16:27:31 +00002707int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp)
bellard1b6b0292003-03-22 17:31:38 +00002708{
2709 int ret;
bellard5cd43932003-03-29 16:54:36 +00002710 TaskState *ts;
bellard1b6b0292003-03-22 17:31:38 +00002711 uint8_t *new_stack;
bellard2ab83ea2003-06-15 19:56:46 +00002712 CPUState *new_env;
ths3b46e622007-09-17 08:09:54 +00002713
bellard1b6b0292003-03-22 17:31:38 +00002714 if (flags & CLONE_VM) {
bellard5cd43932003-03-29 16:54:36 +00002715 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2716 memset(ts, 0, sizeof(TaskState));
2717 new_stack = ts->stack;
2718 ts->used = 1;
2719 /* add in task state list */
2720 ts->next = first_task_state;
2721 first_task_state = ts;
bellard1b6b0292003-03-22 17:31:38 +00002722 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00002723 new_env = cpu_copy(env);
bellard2ab83ea2003-06-15 19:56:46 +00002724#if defined(TARGET_I386)
2725 if (!newsp)
2726 newsp = env->regs[R_ESP];
bellard1b6b0292003-03-22 17:31:38 +00002727 new_env->regs[R_ESP] = newsp;
2728 new_env->regs[R_EAX] = 0;
bellard2ab83ea2003-06-15 19:56:46 +00002729#elif defined(TARGET_ARM)
2730 if (!newsp)
2731 newsp = env->regs[13];
2732 new_env->regs[13] = newsp;
2733 new_env->regs[0] = 0;
bellardebc05482003-09-30 21:08:41 +00002734#elif defined(TARGET_SPARC)
bellard48dc41e2006-06-21 18:15:50 +00002735 if (!newsp)
2736 newsp = env->regwptr[22];
2737 new_env->regwptr[22] = newsp;
2738 new_env->regwptr[0] = 0;
2739 /* XXXXX */
bellard67867302003-11-23 17:05:30 +00002740 printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
pbrooke6e59062006-10-22 00:18:54 +00002741#elif defined(TARGET_M68K)
2742 if (!newsp)
2743 newsp = env->aregs[7];
2744 new_env->aregs[7] = newsp;
2745 new_env->dregs[0] = 0;
2746 /* ??? is this sufficient? */
bellard048f6b42005-11-26 18:47:20 +00002747#elif defined(TARGET_MIPS)
ths388bb212007-05-13 13:58:00 +00002748 if (!newsp)
thsead93602007-09-06 00:18:15 +00002749 newsp = env->gpr[29][env->current_tc];
2750 new_env->gpr[29][env->current_tc] = newsp;
bellard67867302003-11-23 17:05:30 +00002751#elif defined(TARGET_PPC)
2752 if (!newsp)
2753 newsp = env->gpr[1];
2754 new_env->gpr[1] = newsp;
ths5fafdf22007-09-16 21:08:06 +00002755 {
bellard67867302003-11-23 17:05:30 +00002756 int i;
2757 for (i = 7; i < 32; i++)
2758 new_env->gpr[i] = 0;
2759 }
bellardfdf9b3e2006-04-27 21:07:38 +00002760#elif defined(TARGET_SH4)
2761 if (!newsp)
2762 newsp = env->gregs[15];
2763 new_env->gregs[15] = newsp;
2764 /* XXXXX */
j_mayer7a3148a2007-04-05 07:13:51 +00002765#elif defined(TARGET_ALPHA)
2766 if (!newsp)
2767 newsp = env->ir[30];
2768 new_env->ir[30] = newsp;
2769 /* ? */
2770 {
2771 int i;
2772 for (i = 7; i < 30; i++)
2773 new_env->ir[i] = 0;
2774 }
ths48733d12007-10-08 13:36:46 +00002775#elif defined(TARGET_CRIS)
2776 if (!newsp)
2777 newsp = env->regs[14];
2778 new_env->regs[14] = newsp;
bellard2ab83ea2003-06-15 19:56:46 +00002779#else
2780#error unsupported target CPU
2781#endif
bellard5cd43932003-03-29 16:54:36 +00002782 new_env->opaque = ts;
bellard27725c12003-04-29 21:08:18 +00002783#ifdef __ia64__
bellardfd4a43e2006-04-24 20:32:17 +00002784 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00002785#else
2786 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2787#endif
bellard1b6b0292003-03-22 17:31:38 +00002788 } else {
2789 /* if no CLONE_VM, we consider it is a fork */
2790 if ((flags & ~CSIGNAL) != 0)
2791 return -EINVAL;
2792 ret = fork();
2793 }
2794 return ret;
2795}
2796
blueswir1992f48a2007-10-14 16:27:31 +00002797static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00002798{
2799 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00002800 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00002801 struct flock64 fl64;
2802 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00002803 abi_long ret;
pbrook53a59602006-03-25 19:31:22 +00002804
bellard7775e9e2003-05-14 22:46:48 +00002805 switch(cmd) {
2806 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00002807 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2808 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00002809 fl.l_type = tswap16(target_fl->l_type);
2810 fl.l_whence = tswap16(target_fl->l_whence);
2811 fl.l_start = tswapl(target_fl->l_start);
2812 fl.l_len = tswapl(target_fl->l_len);
2813 fl.l_pid = tswapl(target_fl->l_pid);
2814 unlock_user_struct(target_fl, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002815 ret = get_errno(fcntl(fd, cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00002816 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00002817 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
2818 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00002819 target_fl->l_type = tswap16(fl.l_type);
2820 target_fl->l_whence = tswap16(fl.l_whence);
2821 target_fl->l_start = tswapl(fl.l_start);
2822 target_fl->l_len = tswapl(fl.l_len);
2823 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00002824 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00002825 }
2826 break;
ths3b46e622007-09-17 08:09:54 +00002827
bellard7775e9e2003-05-14 22:46:48 +00002828 case TARGET_F_SETLK:
2829 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00002830 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
2831 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00002832 fl.l_type = tswap16(target_fl->l_type);
2833 fl.l_whence = tswap16(target_fl->l_whence);
2834 fl.l_start = tswapl(target_fl->l_start);
2835 fl.l_len = tswapl(target_fl->l_len);
2836 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00002837 unlock_user_struct(target_fl, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002838 ret = get_errno(fcntl(fd, cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00002839 break;
ths3b46e622007-09-17 08:09:54 +00002840
bellard7775e9e2003-05-14 22:46:48 +00002841 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00002842 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2843 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00002844 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2845 fl64.l_whence = tswap16(target_fl64->l_whence);
2846 fl64.l_start = tswapl(target_fl64->l_start);
2847 fl64.l_len = tswapl(target_fl64->l_len);
2848 fl64.l_pid = tswap16(target_fl64->l_pid);
2849 unlock_user_struct(target_fl64, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002850 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
ths43f238d2007-01-05 20:55:49 +00002851 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00002852 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
2853 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00002854 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2855 target_fl64->l_whence = tswap16(fl64.l_whence);
2856 target_fl64->l_start = tswapl(fl64.l_start);
2857 target_fl64->l_len = tswapl(fl64.l_len);
2858 target_fl64->l_pid = tswapl(fl64.l_pid);
2859 unlock_user_struct(target_fl64, arg, 1);
2860 }
bellard9ee1fa22007-11-11 15:11:19 +00002861 break;
bellard7775e9e2003-05-14 22:46:48 +00002862 case TARGET_F_SETLK64:
2863 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00002864 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
2865 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00002866 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2867 fl64.l_whence = tswap16(target_fl64->l_whence);
2868 fl64.l_start = tswapl(target_fl64->l_start);
2869 fl64.l_len = tswapl(target_fl64->l_len);
2870 fl64.l_pid = tswap16(target_fl64->l_pid);
2871 unlock_user_struct(target_fl64, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00002872 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00002873 break;
2874
bellardffa65c32004-01-04 23:57:22 +00002875 case F_GETFL:
bellard9ee1fa22007-11-11 15:11:19 +00002876 ret = get_errno(fcntl(fd, cmd, arg));
2877 if (ret >= 0) {
2878 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2879 }
bellardffa65c32004-01-04 23:57:22 +00002880 break;
2881
2882 case F_SETFL:
bellard9ee1fa22007-11-11 15:11:19 +00002883 ret = get_errno(fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
bellardffa65c32004-01-04 23:57:22 +00002884 break;
2885
bellard7775e9e2003-05-14 22:46:48 +00002886 default:
bellard9ee1fa22007-11-11 15:11:19 +00002887 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00002888 break;
2889 }
2890 return ret;
2891}
2892
bellard67867302003-11-23 17:05:30 +00002893#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00002894
bellard67867302003-11-23 17:05:30 +00002895static inline int high2lowuid(int uid)
2896{
2897 if (uid > 65535)
2898 return 65534;
2899 else
2900 return uid;
2901}
2902
2903static inline int high2lowgid(int gid)
2904{
2905 if (gid > 65535)
2906 return 65534;
2907 else
2908 return gid;
2909}
2910
2911static inline int low2highuid(int uid)
2912{
2913 if ((int16_t)uid == -1)
2914 return -1;
2915 else
2916 return uid;
2917}
2918
2919static inline int low2highgid(int gid)
2920{
2921 if ((int16_t)gid == -1)
2922 return -1;
2923 else
2924 return gid;
2925}
2926
2927#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00002928
bellard31e31b82003-02-18 22:55:36 +00002929void syscall_init(void)
2930{
bellard2ab83ea2003-06-15 19:56:46 +00002931 IOCTLEntry *ie;
2932 const argtype *arg_type;
2933 int size;
thsb92c47c2007-11-01 00:07:38 +00002934 int i;
bellard2ab83ea2003-06-15 19:56:46 +00002935
ths5fafdf22007-09-16 21:08:06 +00002936#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2937#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00002938#include "syscall_types.h"
2939#undef STRUCT
2940#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00002941
2942 /* we patch the ioctl size if necessary. We rely on the fact that
2943 no ioctl has all the bits at '1' in the size field */
2944 ie = ioctl_entries;
2945 while (ie->target_cmd != 0) {
2946 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2947 TARGET_IOC_SIZEMASK) {
2948 arg_type = ie->arg_type;
2949 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00002950 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002951 ie->target_cmd);
2952 exit(1);
2953 }
2954 arg_type++;
2955 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00002956 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00002957 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2958 (size << TARGET_IOC_SIZESHIFT);
2959 }
thsb92c47c2007-11-01 00:07:38 +00002960
2961 /* Build target_to_host_errno_table[] table from
2962 * host_to_target_errno_table[]. */
2963 for (i=0; i < ERRNO_TABLE_SIZE; i++)
2964 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
2965
bellard2ab83ea2003-06-15 19:56:46 +00002966 /* automatic consistency check if same arch */
bellardd2fd1af2007-11-14 18:08:56 +00002967#if defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)
bellard2ab83ea2003-06-15 19:56:46 +00002968 if (ie->target_cmd != ie->host_cmd) {
ths5fafdf22007-09-16 21:08:06 +00002969 fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002970 ie->target_cmd, ie->host_cmd);
2971 }
2972#endif
2973 ie++;
2974 }
bellard31e31b82003-02-18 22:55:36 +00002975}
bellardc573ff62004-01-04 15:51:36 +00002976
blueswir1992f48a2007-10-14 16:27:31 +00002977#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00002978static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2979{
2980#ifdef TARGET_WORDS_BIG_ENDIAN
2981 return ((uint64_t)word0 << 32) | word1;
2982#else
2983 return ((uint64_t)word1 << 32) | word0;
2984#endif
2985}
blueswir1992f48a2007-10-14 16:27:31 +00002986#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00002987static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2988{
2989 return word0;
2990}
blueswir1992f48a2007-10-14 16:27:31 +00002991#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00002992
2993#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00002994static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
2995 abi_long arg2,
2996 abi_long arg3,
2997 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00002998{
2999#ifdef TARGET_ARM
3000 if (((CPUARMState *)cpu_env)->eabi)
3001 {
3002 arg2 = arg3;
3003 arg3 = arg4;
3004 }
3005#endif
3006 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
3007}
3008#endif
3009
3010#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00003011static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
3012 abi_long arg2,
3013 abi_long arg3,
3014 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00003015{
3016#ifdef TARGET_ARM
3017 if (((CPUARMState *)cpu_env)->eabi)
3018 {
3019 arg2 = arg3;
3020 arg3 = arg4;
3021 }
3022#endif
3023 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
3024}
3025#endif
3026
bellard579a97f2007-11-11 14:26:47 +00003027static inline abi_long target_to_host_timespec(struct timespec *host_ts,
3028 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00003029{
3030 struct target_timespec *target_ts;
3031
bellard579a97f2007-11-11 14:26:47 +00003032 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
3033 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003034 host_ts->tv_sec = tswapl(target_ts->tv_sec);
3035 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
3036 unlock_user_struct(target_ts, target_addr, 0);
3037}
3038
bellard579a97f2007-11-11 14:26:47 +00003039static inline abi_long host_to_target_timespec(abi_ulong target_addr,
3040 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00003041{
3042 struct target_timespec *target_ts;
3043
bellard579a97f2007-11-11 14:26:47 +00003044 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
3045 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003046 target_ts->tv_sec = tswapl(host_ts->tv_sec);
3047 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
3048 unlock_user_struct(target_ts, target_addr, 1);
3049}
3050
ths0da46a62007-10-20 20:23:07 +00003051/* do_syscall() should always have a single exit point at the end so
3052 that actions, such as logging of syscall results, can be performed.
3053 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00003054abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
3055 abi_long arg2, abi_long arg3, abi_long arg4,
3056 abi_long arg5, abi_long arg6)
bellard31e31b82003-02-18 22:55:36 +00003057{
blueswir1992f48a2007-10-14 16:27:31 +00003058 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00003059 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00003060 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00003061 void *p;
ths3b46e622007-09-17 08:09:54 +00003062
bellard72f03902003-02-18 23:33:18 +00003063#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00003064 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00003065#endif
thsb92c47c2007-11-01 00:07:38 +00003066 if(do_strace)
3067 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
3068
bellard31e31b82003-02-18 22:55:36 +00003069 switch(num) {
3070 case TARGET_NR_exit:
bellard7d132992003-03-06 23:23:54 +00003071#ifdef HAVE_GPROF
3072 _mcleanup();
3073#endif
bellarde9009672005-04-26 20:42:36 +00003074 gdb_exit(cpu_env, arg1);
bellard1b6b0292003-03-22 17:31:38 +00003075 /* XXX: should free thread stack and CPU env */
bellard31e31b82003-02-18 22:55:36 +00003076 _exit(arg1);
3077 ret = 0; /* avoid warning */
3078 break;
3079 case TARGET_NR_read:
bellard579a97f2007-11-11 14:26:47 +00003080 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
3081 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003082 ret = get_errno(read(arg1, p, arg3));
3083 unlock_user(p, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00003084 break;
3085 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00003086 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
3087 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003088 ret = get_errno(write(arg1, p, arg3));
3089 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00003090 break;
3091 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00003092 if (!(p = lock_user_string(arg1)))
3093 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003094 ret = get_errno(open(path(p),
bellardffa65c32004-01-04 23:57:22 +00003095 target_to_host_bitmask(arg2, fcntl_flags_tbl),
3096 arg3));
pbrook53a59602006-03-25 19:31:22 +00003097 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003098 break;
ths82424832007-09-24 09:21:55 +00003099#if defined(TARGET_NR_openat) && defined(__NR_openat)
3100 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00003101 if (!(p = lock_user_string(arg2)))
3102 goto efault;
3103 ret = get_errno(sys_openat(arg1,
3104 path(p),
3105 target_to_host_bitmask(arg3, fcntl_flags_tbl),
3106 arg4));
3107 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00003108 break;
3109#endif
bellard31e31b82003-02-18 22:55:36 +00003110 case TARGET_NR_close:
3111 ret = get_errno(close(arg1));
3112 break;
3113 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00003114 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00003115 break;
3116 case TARGET_NR_fork:
bellard1b6b0292003-03-22 17:31:38 +00003117 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00003118 break;
thse5febef2007-04-01 18:31:35 +00003119#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00003120 case TARGET_NR_waitpid:
3121 {
pbrook53a59602006-03-25 19:31:22 +00003122 int status;
3123 ret = get_errno(waitpid(arg1, &status, arg3));
bellard2f619692007-11-16 10:46:05 +00003124 if (!is_error(ret) && arg2
3125 && put_user_s32(status, arg2))
3126 goto efault;
bellard31e31b82003-02-18 22:55:36 +00003127 }
3128 break;
thse5febef2007-04-01 18:31:35 +00003129#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003130#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003131 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00003132 if (!(p = lock_user_string(arg1)))
3133 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003134 ret = get_errno(creat(p, arg2));
3135 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003136 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003137#endif
bellard31e31b82003-02-18 22:55:36 +00003138 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00003139 {
3140 void * p2;
3141 p = lock_user_string(arg1);
3142 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00003143 if (!p || !p2)
3144 ret = -TARGET_EFAULT;
3145 else
3146 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00003147 unlock_user(p2, arg2, 0);
3148 unlock_user(p, arg1, 0);
3149 }
bellard31e31b82003-02-18 22:55:36 +00003150 break;
ths64f0ce42007-09-24 09:25:06 +00003151#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
3152 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00003153 {
3154 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00003155 if (!arg2 || !arg4)
3156 goto efault;
ths64f0ce42007-09-24 09:25:06 +00003157 p = lock_user_string(arg2);
3158 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00003159 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00003160 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00003161 else
3162 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00003163 unlock_user(p, arg2, 0);
3164 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00003165 }
3166 break;
3167#endif
bellard31e31b82003-02-18 22:55:36 +00003168 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00003169 if (!(p = lock_user_string(arg1)))
3170 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003171 ret = get_errno(unlink(p));
3172 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003173 break;
ths8170f562007-09-24 09:24:11 +00003174#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
3175 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00003176 if (!(p = lock_user_string(arg2)))
3177 goto efault;
3178 ret = get_errno(sys_unlinkat(arg1, p, arg3));
3179 unlock_user(p, arg2, 0);
ths8170f562007-09-24 09:24:11 +00003180#endif
bellard31e31b82003-02-18 22:55:36 +00003181 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00003182 {
3183 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00003184 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00003185 abi_ulong gp;
3186 abi_ulong guest_argp;
3187 abi_ulong guest_envp;
3188 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00003189 char **q;
3190
bellardf7341ff2003-03-30 21:00:25 +00003191 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00003192 guest_argp = arg2;
bellard2f619692007-11-16 10:46:05 +00003193 for (gp = guest_argp; ; gp++) {
ths03aa1972007-12-02 06:28:08 +00003194 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00003195 goto efault;
ths03aa1972007-12-02 06:28:08 +00003196 if (!addr)
bellard2f619692007-11-16 10:46:05 +00003197 break;
bellard7854b052003-03-29 17:22:23 +00003198 argc++;
bellard2f619692007-11-16 10:46:05 +00003199 }
bellardf7341ff2003-03-30 21:00:25 +00003200 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00003201 guest_envp = arg3;
bellard2f619692007-11-16 10:46:05 +00003202 for (gp = guest_envp; ; gp++) {
ths03aa1972007-12-02 06:28:08 +00003203 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00003204 goto efault;
ths03aa1972007-12-02 06:28:08 +00003205 if (!addr)
bellard2f619692007-11-16 10:46:05 +00003206 break;
bellard7854b052003-03-29 17:22:23 +00003207 envc++;
bellard2f619692007-11-16 10:46:05 +00003208 }
bellard7854b052003-03-29 17:22:23 +00003209
bellardf7341ff2003-03-30 21:00:25 +00003210 argp = alloca((argc + 1) * sizeof(void *));
3211 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00003212
pbrook53a59602006-03-25 19:31:22 +00003213 for (gp = guest_argp, q = argp; ;
blueswir1992f48a2007-10-14 16:27:31 +00003214 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00003215 if (get_user_ual(addr, gp))
3216 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003217 if (!addr)
3218 break;
bellard2f619692007-11-16 10:46:05 +00003219 if (!(*q = lock_user_string(addr)))
3220 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003221 }
bellardf7341ff2003-03-30 21:00:25 +00003222 *q = NULL;
3223
pbrook53a59602006-03-25 19:31:22 +00003224 for (gp = guest_envp, q = envp; ;
blueswir1992f48a2007-10-14 16:27:31 +00003225 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00003226 if (get_user_ual(addr, gp))
3227 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003228 if (!addr)
3229 break;
bellard2f619692007-11-16 10:46:05 +00003230 if (!(*q = lock_user_string(addr)))
3231 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003232 }
bellardf7341ff2003-03-30 21:00:25 +00003233 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00003234
bellard2f619692007-11-16 10:46:05 +00003235 if (!(p = lock_user_string(arg1)))
3236 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003237 ret = get_errno(execve(p, argp, envp));
3238 unlock_user(p, arg1, 0);
3239
bellard2f619692007-11-16 10:46:05 +00003240 goto execve_end;
3241
3242 execve_efault:
3243 ret = -TARGET_EFAULT;
3244
3245 execve_end:
pbrook53a59602006-03-25 19:31:22 +00003246 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00003247 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00003248 if (get_user_ual(addr, gp)
3249 || !addr)
3250 break;
pbrook53a59602006-03-25 19:31:22 +00003251 unlock_user(*q, addr, 0);
3252 }
3253 for (gp = guest_envp, q = envp; *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 }
bellard7854b052003-03-29 17:22:23 +00003260 }
bellard31e31b82003-02-18 22:55:36 +00003261 break;
3262 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00003263 if (!(p = lock_user_string(arg1)))
3264 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003265 ret = get_errno(chdir(p));
3266 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003267 break;
bellarda315a142005-01-30 22:59:18 +00003268#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00003269 case TARGET_NR_time:
3270 {
pbrook53a59602006-03-25 19:31:22 +00003271 time_t host_time;
3272 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00003273 if (!is_error(ret)
3274 && arg1
3275 && put_user_sal(host_time, arg1))
3276 goto efault;
bellard31e31b82003-02-18 22:55:36 +00003277 }
3278 break;
bellarda315a142005-01-30 22:59:18 +00003279#endif
bellard31e31b82003-02-18 22:55:36 +00003280 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00003281 if (!(p = lock_user_string(arg1)))
3282 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003283 ret = get_errno(mknod(p, arg2, arg3));
3284 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003285 break;
ths75ac37a2007-09-24 09:23:05 +00003286#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
3287 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00003288 if (!(p = lock_user_string(arg2)))
3289 goto efault;
3290 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
3291 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00003292 break;
3293#endif
bellard31e31b82003-02-18 22:55:36 +00003294 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00003295 if (!(p = lock_user_string(arg1)))
3296 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003297 ret = get_errno(chmod(p, arg2));
3298 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003299 break;
bellardebc05482003-09-30 21:08:41 +00003300#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00003301 case TARGET_NR_break:
3302 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003303#endif
3304#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00003305 case TARGET_NR_oldstat:
3306 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003307#endif
bellard31e31b82003-02-18 22:55:36 +00003308 case TARGET_NR_lseek:
3309 ret = get_errno(lseek(arg1, arg2, arg3));
3310 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003311#ifdef TARGET_NR_getxpid
3312 case TARGET_NR_getxpid:
3313#else
bellard31e31b82003-02-18 22:55:36 +00003314 case TARGET_NR_getpid:
j_mayer7a3148a2007-04-05 07:13:51 +00003315#endif
bellard31e31b82003-02-18 22:55:36 +00003316 ret = get_errno(getpid());
3317 break;
3318 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00003319 {
3320 /* need to look at the data field */
3321 void *p2, *p3;
3322 p = lock_user_string(arg1);
3323 p2 = lock_user_string(arg2);
3324 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00003325 if (!p || !p2 || !p3)
3326 ret = -TARGET_EFAULT;
3327 else
3328 /* FIXME - arg5 should be locked, but it isn't clear how to
3329 * do that since it's not guaranteed to be a NULL-terminated
3330 * string.
3331 */
3332 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
3333 unlock_user(p, arg1, 0);
3334 unlock_user(p2, arg2, 0);
3335 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00003336 break;
3337 }
thse5febef2007-04-01 18:31:35 +00003338#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00003339 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00003340 if (!(p = lock_user_string(arg1)))
3341 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003342 ret = get_errno(umount(p));
3343 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003344 break;
thse5febef2007-04-01 18:31:35 +00003345#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003346#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003347 case TARGET_NR_stime:
3348 {
pbrook53a59602006-03-25 19:31:22 +00003349 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00003350 if (get_user_sal(host_time, arg1))
3351 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003352 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00003353 }
3354 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003355#endif
bellard31e31b82003-02-18 22:55:36 +00003356 case TARGET_NR_ptrace:
3357 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00003358#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003359 case TARGET_NR_alarm:
3360 ret = alarm(arg1);
3361 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003362#endif
bellardebc05482003-09-30 21:08:41 +00003363#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00003364 case TARGET_NR_oldfstat:
3365 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003366#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003367#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003368 case TARGET_NR_pause:
3369 ret = get_errno(pause());
3370 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003371#endif
thse5febef2007-04-01 18:31:35 +00003372#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00003373 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00003374 {
pbrook53a59602006-03-25 19:31:22 +00003375 struct utimbuf tbuf, *host_tbuf;
3376 struct target_utimbuf *target_tbuf;
3377 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00003378 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
3379 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003380 tbuf.actime = tswapl(target_tbuf->actime);
3381 tbuf.modtime = tswapl(target_tbuf->modtime);
3382 unlock_user_struct(target_tbuf, arg2, 0);
3383 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00003384 } else {
pbrook53a59602006-03-25 19:31:22 +00003385 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00003386 }
bellard579a97f2007-11-11 14:26:47 +00003387 if (!(p = lock_user_string(arg1)))
3388 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003389 ret = get_errno(utime(p, host_tbuf));
3390 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00003391 }
3392 break;
thse5febef2007-04-01 18:31:35 +00003393#endif
bellard978a66f2004-12-06 22:58:05 +00003394 case TARGET_NR_utimes:
3395 {
bellard978a66f2004-12-06 22:58:05 +00003396 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00003397 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00003398 if (copy_from_user_timeval(&tv[0], arg2)
3399 || copy_from_user_timeval(&tv[1],
3400 arg2 + sizeof(struct target_timeval)))
3401 goto efault;
bellard978a66f2004-12-06 22:58:05 +00003402 tvp = tv;
3403 } else {
3404 tvp = NULL;
3405 }
bellard579a97f2007-11-11 14:26:47 +00003406 if (!(p = lock_user_string(arg1)))
3407 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003408 ret = get_errno(utimes(p, tvp));
3409 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00003410 }
3411 break;
bellardebc05482003-09-30 21:08:41 +00003412#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00003413 case TARGET_NR_stty:
3414 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003415#endif
3416#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00003417 case TARGET_NR_gtty:
3418 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003419#endif
bellard31e31b82003-02-18 22:55:36 +00003420 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00003421 if (!(p = lock_user_string(arg1)))
3422 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003423 ret = get_errno(access(p, arg2));
3424 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003425 break;
ths92a34c12007-09-24 09:27:49 +00003426#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
3427 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00003428 if (!(p = lock_user_string(arg2)))
3429 goto efault;
3430 ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
3431 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00003432 break;
3433#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003434#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003435 case TARGET_NR_nice:
3436 ret = get_errno(nice(arg1));
3437 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003438#endif
bellardebc05482003-09-30 21:08:41 +00003439#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00003440 case TARGET_NR_ftime:
3441 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003442#endif
bellard31e31b82003-02-18 22:55:36 +00003443 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00003444 sync();
3445 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00003446 break;
3447 case TARGET_NR_kill:
3448 ret = get_errno(kill(arg1, arg2));
3449 break;
3450 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00003451 {
3452 void *p2;
3453 p = lock_user_string(arg1);
3454 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00003455 if (!p || !p2)
3456 ret = -TARGET_EFAULT;
3457 else
3458 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00003459 unlock_user(p2, arg2, 0);
3460 unlock_user(p, arg1, 0);
3461 }
bellard31e31b82003-02-18 22:55:36 +00003462 break;
ths722183f2007-09-24 09:24:37 +00003463#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
3464 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00003465 {
bellard579a97f2007-11-11 14:26:47 +00003466 void *p2;
ths722183f2007-09-24 09:24:37 +00003467 p = lock_user_string(arg2);
3468 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00003469 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00003470 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00003471 else
3472 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00003473 unlock_user(p2, arg4, 0);
3474 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00003475 }
3476 break;
3477#endif
bellard31e31b82003-02-18 22:55:36 +00003478 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00003479 if (!(p = lock_user_string(arg1)))
3480 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003481 ret = get_errno(mkdir(p, arg2));
3482 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003483 break;
ths4472ad02007-09-24 09:22:32 +00003484#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
3485 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00003486 if (!(p = lock_user_string(arg2)))
3487 goto efault;
3488 ret = get_errno(sys_mkdirat(arg1, p, arg3));
3489 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00003490 break;
3491#endif
bellard31e31b82003-02-18 22:55:36 +00003492 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00003493 if (!(p = lock_user_string(arg1)))
3494 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003495 ret = get_errno(rmdir(p));
3496 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003497 break;
3498 case TARGET_NR_dup:
3499 ret = get_errno(dup(arg1));
3500 break;
3501 case TARGET_NR_pipe:
3502 {
pbrook53a59602006-03-25 19:31:22 +00003503 int host_pipe[2];
3504 ret = get_errno(pipe(host_pipe));
bellard31e31b82003-02-18 22:55:36 +00003505 if (!is_error(ret)) {
thsc12ab052007-06-01 11:50:36 +00003506#if defined(TARGET_MIPS)
thsead93602007-09-06 00:18:15 +00003507 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
3508 env->gpr[3][env->current_tc] = host_pipe[1];
thsc12ab052007-06-01 11:50:36 +00003509 ret = host_pipe[0];
3510#else
bellard2f619692007-11-16 10:46:05 +00003511 if (put_user_s32(host_pipe[0], arg1)
3512 || put_user_s32(host_pipe[1], arg1 + sizeof(host_pipe[0])))
3513 goto efault;
thsc12ab052007-06-01 11:50:36 +00003514#endif
bellard31e31b82003-02-18 22:55:36 +00003515 }
3516 }
3517 break;
3518 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00003519 {
pbrook53a59602006-03-25 19:31:22 +00003520 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00003521 struct tms tms;
3522 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00003523 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00003524 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
3525 if (!tmsp)
3526 goto efault;
bellardc596ed12003-07-13 17:32:31 +00003527 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3528 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3529 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3530 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00003531 }
bellardc596ed12003-07-13 17:32:31 +00003532 if (!is_error(ret))
3533 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00003534 }
3535 break;
bellardebc05482003-09-30 21:08:41 +00003536#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00003537 case TARGET_NR_prof:
3538 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003539#endif
thse5febef2007-04-01 18:31:35 +00003540#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00003541 case TARGET_NR_signal:
3542 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003543#endif
bellard31e31b82003-02-18 22:55:36 +00003544 case TARGET_NR_acct:
bellard579a97f2007-11-11 14:26:47 +00003545 if (!(p = lock_user_string(arg1)))
3546 goto efault;
pbrook24836682006-04-16 14:14:53 +00003547 ret = get_errno(acct(path(p)));
3548 unlock_user(p, arg1, 0);
3549 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003550#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003551 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00003552 if (!(p = lock_user_string(arg1)))
3553 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003554 ret = get_errno(umount2(p, arg2));
3555 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003556 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003557#endif
bellardebc05482003-09-30 21:08:41 +00003558#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00003559 case TARGET_NR_lock:
3560 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003561#endif
bellard31e31b82003-02-18 22:55:36 +00003562 case TARGET_NR_ioctl:
3563 ret = do_ioctl(arg1, arg2, arg3);
3564 break;
3565 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00003566 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00003567 break;
bellardebc05482003-09-30 21:08:41 +00003568#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00003569 case TARGET_NR_mpx:
3570 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003571#endif
bellard31e31b82003-02-18 22:55:36 +00003572 case TARGET_NR_setpgid:
3573 ret = get_errno(setpgid(arg1, arg2));
3574 break;
bellardebc05482003-09-30 21:08:41 +00003575#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00003576 case TARGET_NR_ulimit:
3577 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003578#endif
3579#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00003580 case TARGET_NR_oldolduname:
3581 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003582#endif
bellard31e31b82003-02-18 22:55:36 +00003583 case TARGET_NR_umask:
3584 ret = get_errno(umask(arg1));
3585 break;
3586 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00003587 if (!(p = lock_user_string(arg1)))
3588 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003589 ret = get_errno(chroot(p));
3590 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003591 break;
3592 case TARGET_NR_ustat:
3593 goto unimplemented;
3594 case TARGET_NR_dup2:
3595 ret = get_errno(dup2(arg1, arg2));
3596 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003597#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003598 case TARGET_NR_getppid:
3599 ret = get_errno(getppid());
3600 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003601#endif
bellard31e31b82003-02-18 22:55:36 +00003602 case TARGET_NR_getpgrp:
3603 ret = get_errno(getpgrp());
3604 break;
3605 case TARGET_NR_setsid:
3606 ret = get_errno(setsid());
3607 break;
thse5febef2007-04-01 18:31:35 +00003608#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00003609 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00003610 {
ths388bb212007-05-13 13:58:00 +00003611#if !defined(TARGET_MIPS)
pbrook53a59602006-03-25 19:31:22 +00003612 struct target_old_sigaction *old_act;
bellard66fb9762003-03-23 01:06:05 +00003613 struct target_sigaction act, oact, *pact;
pbrook53a59602006-03-25 19:31:22 +00003614 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00003615 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3616 goto efault;
bellard66fb9762003-03-23 01:06:05 +00003617 act._sa_handler = old_act->_sa_handler;
3618 target_siginitset(&act.sa_mask, old_act->sa_mask);
3619 act.sa_flags = old_act->sa_flags;
3620 act.sa_restorer = old_act->sa_restorer;
pbrook53a59602006-03-25 19:31:22 +00003621 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003622 pact = &act;
3623 } else {
3624 pact = NULL;
3625 }
3626 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00003627 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003628 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3629 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003630 old_act->_sa_handler = oact._sa_handler;
3631 old_act->sa_mask = oact.sa_mask.sig[0];
3632 old_act->sa_flags = oact.sa_flags;
3633 old_act->sa_restorer = oact.sa_restorer;
3634 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00003635 }
ths388bb212007-05-13 13:58:00 +00003636#else
bellard106ec872006-06-27 21:08:10 +00003637 struct target_sigaction act, oact, *pact, *old_act;
3638
3639 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00003640 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
3641 goto efault;
bellard106ec872006-06-27 21:08:10 +00003642 act._sa_handler = old_act->_sa_handler;
3643 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3644 act.sa_flags = old_act->sa_flags;
3645 unlock_user_struct(old_act, arg2, 0);
3646 pact = &act;
3647 } else {
3648 pact = NULL;
3649 }
3650
3651 ret = get_errno(do_sigaction(arg1, pact, &oact));
3652
3653 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003654 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
3655 goto efault;
bellard106ec872006-06-27 21:08:10 +00003656 old_act->_sa_handler = oact._sa_handler;
3657 old_act->sa_flags = oact.sa_flags;
3658 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3659 old_act->sa_mask.sig[1] = 0;
3660 old_act->sa_mask.sig[2] = 0;
3661 old_act->sa_mask.sig[3] = 0;
3662 unlock_user_struct(old_act, arg3, 1);
3663 }
ths388bb212007-05-13 13:58:00 +00003664#endif
bellard31e31b82003-02-18 22:55:36 +00003665 }
3666 break;
thse5febef2007-04-01 18:31:35 +00003667#endif
bellard66fb9762003-03-23 01:06:05 +00003668 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00003669 {
3670 struct target_sigaction *act;
3671 struct target_sigaction *oact;
3672
bellard579a97f2007-11-11 14:26:47 +00003673 if (arg2) {
3674 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
3675 goto efault;
3676 } else
pbrook53a59602006-03-25 19:31:22 +00003677 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00003678 if (arg3) {
3679 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
3680 ret = -TARGET_EFAULT;
3681 goto rt_sigaction_fail;
3682 }
3683 } else
pbrook53a59602006-03-25 19:31:22 +00003684 oact = NULL;
3685 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00003686 rt_sigaction_fail:
3687 if (act)
pbrook53a59602006-03-25 19:31:22 +00003688 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00003689 if (oact)
pbrook53a59602006-03-25 19:31:22 +00003690 unlock_user_struct(oact, arg3, 1);
3691 }
bellard66fb9762003-03-23 01:06:05 +00003692 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003693#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003694 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00003695 {
3696 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00003697 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00003698 sigprocmask(0, NULL, &cur_set);
3699 host_to_target_old_sigset(&target_set, &cur_set);
3700 ret = target_set;
3701 }
3702 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003703#endif
3704#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003705 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00003706 {
3707 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00003708 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00003709 sigprocmask(0, NULL, &cur_set);
3710 target_to_host_old_sigset(&set, &target_set);
3711 sigorset(&set, &set, &cur_set);
3712 sigprocmask(SIG_SETMASK, &set, &oset);
3713 host_to_target_old_sigset(&target_set, &oset);
3714 ret = target_set;
3715 }
3716 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003717#endif
thse5febef2007-04-01 18:31:35 +00003718#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00003719 case TARGET_NR_sigprocmask:
3720 {
3721 int how = arg1;
3722 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003723
pbrook53a59602006-03-25 19:31:22 +00003724 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003725 switch(how) {
3726 case TARGET_SIG_BLOCK:
3727 how = SIG_BLOCK;
3728 break;
3729 case TARGET_SIG_UNBLOCK:
3730 how = SIG_UNBLOCK;
3731 break;
3732 case TARGET_SIG_SETMASK:
3733 how = SIG_SETMASK;
3734 break;
3735 default:
ths0da46a62007-10-20 20:23:07 +00003736 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00003737 goto fail;
3738 }
bellard579a97f2007-11-11 14:26:47 +00003739 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3740 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003741 target_to_host_old_sigset(&set, p);
3742 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003743 set_ptr = &set;
3744 } else {
3745 how = 0;
3746 set_ptr = NULL;
3747 }
3748 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003749 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003750 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3751 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003752 host_to_target_old_sigset(p, &oldset);
3753 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003754 }
3755 }
3756 break;
thse5febef2007-04-01 18:31:35 +00003757#endif
bellard66fb9762003-03-23 01:06:05 +00003758 case TARGET_NR_rt_sigprocmask:
3759 {
3760 int how = arg1;
3761 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003762
pbrook53a59602006-03-25 19:31:22 +00003763 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003764 switch(how) {
3765 case TARGET_SIG_BLOCK:
3766 how = SIG_BLOCK;
3767 break;
3768 case TARGET_SIG_UNBLOCK:
3769 how = SIG_UNBLOCK;
3770 break;
3771 case TARGET_SIG_SETMASK:
3772 how = SIG_SETMASK;
3773 break;
3774 default:
ths0da46a62007-10-20 20:23:07 +00003775 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00003776 goto fail;
3777 }
bellard579a97f2007-11-11 14:26:47 +00003778 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
3779 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003780 target_to_host_sigset(&set, p);
3781 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003782 set_ptr = &set;
3783 } else {
3784 how = 0;
3785 set_ptr = NULL;
3786 }
3787 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003788 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00003789 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
3790 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003791 host_to_target_sigset(p, &oldset);
3792 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003793 }
3794 }
3795 break;
thse5febef2007-04-01 18:31:35 +00003796#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00003797 case TARGET_NR_sigpending:
3798 {
3799 sigset_t set;
3800 ret = get_errno(sigpending(&set));
3801 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003802 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3803 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003804 host_to_target_old_sigset(p, &set);
3805 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003806 }
3807 }
3808 break;
thse5febef2007-04-01 18:31:35 +00003809#endif
bellard66fb9762003-03-23 01:06:05 +00003810 case TARGET_NR_rt_sigpending:
3811 {
3812 sigset_t set;
3813 ret = get_errno(sigpending(&set));
3814 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003815 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
3816 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003817 host_to_target_sigset(p, &set);
3818 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003819 }
3820 }
3821 break;
thse5febef2007-04-01 18:31:35 +00003822#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00003823 case TARGET_NR_sigsuspend:
3824 {
3825 sigset_t set;
bellard579a97f2007-11-11 14:26:47 +00003826 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3827 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003828 target_to_host_old_sigset(&set, p);
3829 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003830 ret = get_errno(sigsuspend(&set));
3831 }
3832 break;
thse5febef2007-04-01 18:31:35 +00003833#endif
bellard66fb9762003-03-23 01:06:05 +00003834 case TARGET_NR_rt_sigsuspend:
3835 {
3836 sigset_t set;
bellard579a97f2007-11-11 14:26:47 +00003837 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3838 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003839 target_to_host_sigset(&set, p);
3840 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003841 ret = get_errno(sigsuspend(&set));
3842 }
3843 break;
3844 case TARGET_NR_rt_sigtimedwait:
3845 {
bellard66fb9762003-03-23 01:06:05 +00003846 sigset_t set;
3847 struct timespec uts, *puts;
3848 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00003849
bellard579a97f2007-11-11 14:26:47 +00003850 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
3851 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003852 target_to_host_sigset(&set, p);
3853 unlock_user(p, arg1, 0);
3854 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00003855 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00003856 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00003857 } else {
3858 puts = NULL;
3859 }
3860 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00003861 if (!is_error(ret) && arg2) {
bellard579a97f2007-11-11 14:26:47 +00003862 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_sigset_t), 0)))
3863 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003864 host_to_target_siginfo(p, &uinfo);
3865 unlock_user(p, arg2, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003866 }
3867 }
3868 break;
3869 case TARGET_NR_rt_sigqueueinfo:
3870 {
3871 siginfo_t uinfo;
bellard579a97f2007-11-11 14:26:47 +00003872 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
3873 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003874 target_to_host_siginfo(&uinfo, p);
3875 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003876 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3877 }
3878 break;
thse5febef2007-04-01 18:31:35 +00003879#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00003880 case TARGET_NR_sigreturn:
3881 /* NOTE: ret is eax, so not transcoding must be done */
3882 ret = do_sigreturn(cpu_env);
3883 break;
thse5febef2007-04-01 18:31:35 +00003884#endif
bellard66fb9762003-03-23 01:06:05 +00003885 case TARGET_NR_rt_sigreturn:
3886 /* NOTE: ret is eax, so not transcoding must be done */
3887 ret = do_rt_sigreturn(cpu_env);
3888 break;
bellard31e31b82003-02-18 22:55:36 +00003889 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00003890 if (!(p = lock_user_string(arg1)))
3891 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003892 ret = get_errno(sethostname(p, arg2));
3893 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003894 break;
3895 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00003896 {
3897 /* XXX: convert resource ? */
3898 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003899 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003900 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00003901 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
3902 goto efault;
bellard9de5e442003-03-23 16:49:39 +00003903 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3904 rlim.rlim_max = tswapl(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00003905 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00003906 ret = get_errno(setrlimit(resource, &rlim));
3907 }
3908 break;
bellard31e31b82003-02-18 22:55:36 +00003909 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00003910 {
3911 /* XXX: convert resource ? */
3912 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003913 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003914 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00003915
bellard9de5e442003-03-23 16:49:39 +00003916 ret = get_errno(getrlimit(resource, &rlim));
3917 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00003918 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
3919 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003920 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3921 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3922 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00003923 }
3924 }
3925 break;
bellard31e31b82003-02-18 22:55:36 +00003926 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00003927 {
3928 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00003929 ret = get_errno(getrusage(arg1, &rusage));
3930 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003931 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00003932 }
3933 }
3934 break;
bellard31e31b82003-02-18 22:55:36 +00003935 case TARGET_NR_gettimeofday:
3936 {
bellard31e31b82003-02-18 22:55:36 +00003937 struct timeval tv;
3938 ret = get_errno(gettimeofday(&tv, NULL));
3939 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00003940 if (copy_to_user_timeval(arg1, &tv))
3941 goto efault;
bellard31e31b82003-02-18 22:55:36 +00003942 }
3943 }
3944 break;
3945 case TARGET_NR_settimeofday:
3946 {
bellard31e31b82003-02-18 22:55:36 +00003947 struct timeval tv;
ths788f5ec2007-12-09 02:37:05 +00003948 if (copy_from_user_timeval(&tv, arg1))
3949 goto efault;
bellard31e31b82003-02-18 22:55:36 +00003950 ret = get_errno(settimeofday(&tv, NULL));
3951 }
3952 break;
bellard048f6b42005-11-26 18:47:20 +00003953#ifdef TARGET_NR_select
bellard31e31b82003-02-18 22:55:36 +00003954 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00003955 {
pbrook53a59602006-03-25 19:31:22 +00003956 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00003957 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00003958 long nsel;
3959
bellard579a97f2007-11-11 14:26:47 +00003960 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
3961 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003962 nsel = tswapl(sel->n);
3963 inp = tswapl(sel->inp);
3964 outp = tswapl(sel->outp);
3965 exp = tswapl(sel->exp);
3966 tvp = tswapl(sel->tvp);
3967 unlock_user_struct(sel, arg1, 0);
3968 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00003969 }
3970 break;
bellard048f6b42005-11-26 18:47:20 +00003971#endif
bellard31e31b82003-02-18 22:55:36 +00003972 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00003973 {
3974 void *p2;
3975 p = lock_user_string(arg1);
3976 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00003977 if (!p || !p2)
3978 ret = -TARGET_EFAULT;
3979 else
3980 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00003981 unlock_user(p2, arg2, 0);
3982 unlock_user(p, arg1, 0);
3983 }
bellard31e31b82003-02-18 22:55:36 +00003984 break;
thsf0b62432007-09-24 09:25:40 +00003985#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
3986 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00003987 {
bellard579a97f2007-11-11 14:26:47 +00003988 void *p2;
thsf0b62432007-09-24 09:25:40 +00003989 p = lock_user_string(arg1);
3990 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00003991 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00003992 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00003993 else
3994 ret = get_errno(sys_symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00003995 unlock_user(p2, arg3, 0);
3996 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00003997 }
3998 break;
3999#endif
bellardebc05482003-09-30 21:08:41 +00004000#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00004001 case TARGET_NR_oldlstat:
4002 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004003#endif
bellard31e31b82003-02-18 22:55:36 +00004004 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00004005 {
4006 void *p2;
4007 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00004008 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
4009 if (!p || !p2)
4010 ret = -TARGET_EFAULT;
4011 else
4012 ret = get_errno(readlink(path(p), p2, arg3));
pbrook53a59602006-03-25 19:31:22 +00004013 unlock_user(p2, arg2, ret);
4014 unlock_user(p, arg1, 0);
4015 }
bellard31e31b82003-02-18 22:55:36 +00004016 break;
ths5e0ccb12007-09-24 09:26:10 +00004017#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
4018 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00004019 {
bellard579a97f2007-11-11 14:26:47 +00004020 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00004021 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004022 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
4023 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004024 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00004025 else
4026 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
bellard579a97f2007-11-11 14:26:47 +00004027 unlock_user(p2, arg3, ret);
4028 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00004029 }
4030 break;
4031#endif
thse5febef2007-04-01 18:31:35 +00004032#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00004033 case TARGET_NR_uselib:
4034 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004035#endif
4036#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00004037 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00004038 if (!(p = lock_user_string(arg1)))
4039 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004040 ret = get_errno(swapon(p, arg2));
4041 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004042 break;
thse5febef2007-04-01 18:31:35 +00004043#endif
bellard31e31b82003-02-18 22:55:36 +00004044 case TARGET_NR_reboot:
4045 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004046#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00004047 case TARGET_NR_readdir:
4048 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004049#endif
4050#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00004051 case TARGET_NR_mmap:
bellardd2fd1af2007-11-14 18:08:56 +00004052#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS)
bellard31e31b82003-02-18 22:55:36 +00004053 {
blueswir1992f48a2007-10-14 16:27:31 +00004054 abi_ulong *v;
4055 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00004056 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
4057 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004058 v1 = tswapl(v[0]);
4059 v2 = tswapl(v[1]);
4060 v3 = tswapl(v[2]);
4061 v4 = tswapl(v[3]);
4062 v5 = tswapl(v[4]);
4063 v6 = tswapl(v[5]);
4064 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00004065 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00004066 target_to_host_bitmask(v4, mmap_flags_tbl),
4067 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00004068 }
bellard31e31b82003-02-18 22:55:36 +00004069#else
ths5fafdf22007-09-16 21:08:06 +00004070 ret = get_errno(target_mmap(arg1, arg2, arg3,
4071 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00004072 arg5,
4073 arg6));
bellard31e31b82003-02-18 22:55:36 +00004074#endif
bellard6fb883e2003-07-09 17:12:39 +00004075 break;
thse5febef2007-04-01 18:31:35 +00004076#endif
bellarda315a142005-01-30 22:59:18 +00004077#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00004078 case TARGET_NR_mmap2:
ths0d3267a2006-12-11 22:04:25 +00004079#if defined(TARGET_SPARC) || defined(TARGET_MIPS)
bellardc573ff62004-01-04 15:51:36 +00004080#define MMAP_SHIFT 12
4081#else
4082#define MMAP_SHIFT TARGET_PAGE_BITS
4083#endif
ths5fafdf22007-09-16 21:08:06 +00004084 ret = get_errno(target_mmap(arg1, arg2, arg3,
4085 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00004086 arg5,
bellardc573ff62004-01-04 15:51:36 +00004087 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00004088 break;
bellarda315a142005-01-30 22:59:18 +00004089#endif
bellard31e31b82003-02-18 22:55:36 +00004090 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00004091 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00004092 break;
bellard9de5e442003-03-23 16:49:39 +00004093 case TARGET_NR_mprotect:
bellard54936002003-05-13 00:25:15 +00004094 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00004095 break;
thse5febef2007-04-01 18:31:35 +00004096#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00004097 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00004098 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00004099 break;
thse5febef2007-04-01 18:31:35 +00004100#endif
pbrook53a59602006-03-25 19:31:22 +00004101 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00004102#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00004103 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00004104 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00004105 break;
thse5febef2007-04-01 18:31:35 +00004106#endif
4107#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00004108 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00004109 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00004110 break;
thse5febef2007-04-01 18:31:35 +00004111#endif
4112#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00004113 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00004114 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00004115 break;
thse5febef2007-04-01 18:31:35 +00004116#endif
4117#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00004118 case TARGET_NR_mlockall:
4119 ret = get_errno(mlockall(arg1));
4120 break;
thse5febef2007-04-01 18:31:35 +00004121#endif
4122#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00004123 case TARGET_NR_munlockall:
4124 ret = get_errno(munlockall());
4125 break;
thse5febef2007-04-01 18:31:35 +00004126#endif
bellard31e31b82003-02-18 22:55:36 +00004127 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00004128 if (!(p = lock_user_string(arg1)))
4129 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004130 ret = get_errno(truncate(p, arg2));
4131 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004132 break;
4133 case TARGET_NR_ftruncate:
4134 ret = get_errno(ftruncate(arg1, arg2));
4135 break;
4136 case TARGET_NR_fchmod:
4137 ret = get_errno(fchmod(arg1, arg2));
4138 break;
ths814d7972007-09-24 09:26:51 +00004139#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
4140 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00004141 if (!(p = lock_user_string(arg2)))
4142 goto efault;
4143 ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
4144 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00004145 break;
4146#endif
bellard31e31b82003-02-18 22:55:36 +00004147 case TARGET_NR_getpriority:
thsc6cda172007-10-09 03:42:34 +00004148 /* libc does special remapping of the return value of
4149 * sys_getpriority() so it's just easiest to call
4150 * sys_getpriority() directly rather than through libc. */
4151 ret = sys_getpriority(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00004152 break;
4153 case TARGET_NR_setpriority:
4154 ret = get_errno(setpriority(arg1, arg2, arg3));
4155 break;
bellardebc05482003-09-30 21:08:41 +00004156#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00004157 case TARGET_NR_profil:
4158 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004159#endif
bellard31e31b82003-02-18 22:55:36 +00004160 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00004161 if (!(p = lock_user_string(arg1)))
4162 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004163 ret = get_errno(statfs(path(p), &stfs));
4164 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004165 convert_statfs:
4166 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004167 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00004168
bellard579a97f2007-11-11 14:26:47 +00004169 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
4170 goto efault;
4171 __put_user(stfs.f_type, &target_stfs->f_type);
4172 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
4173 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
4174 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
4175 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
4176 __put_user(stfs.f_files, &target_stfs->f_files);
4177 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
4178 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
4179 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
4180 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00004181 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00004182 }
4183 break;
4184 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00004185 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00004186 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00004187#ifdef TARGET_NR_statfs64
4188 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00004189 if (!(p = lock_user_string(arg1)))
4190 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004191 ret = get_errno(statfs(path(p), &stfs));
4192 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00004193 convert_statfs64:
4194 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004195 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00004196
bellard579a97f2007-11-11 14:26:47 +00004197 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
4198 goto efault;
4199 __put_user(stfs.f_type, &target_stfs->f_type);
4200 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
4201 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
4202 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
4203 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
4204 __put_user(stfs.f_files, &target_stfs->f_files);
4205 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
4206 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
4207 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
4208 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
4209 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00004210 }
4211 break;
4212 case TARGET_NR_fstatfs64:
4213 ret = get_errno(fstatfs(arg1, &stfs));
4214 goto convert_statfs64;
4215#endif
bellardebc05482003-09-30 21:08:41 +00004216#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00004217 case TARGET_NR_ioperm:
4218 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004219#endif
thse5febef2007-04-01 18:31:35 +00004220#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00004221 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00004222 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00004223 break;
thse5febef2007-04-01 18:31:35 +00004224#endif
bellard3532fa72006-06-24 15:06:03 +00004225#ifdef TARGET_NR_accept
4226 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00004227 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00004228 break;
4229#endif
4230#ifdef TARGET_NR_bind
4231 case TARGET_NR_bind:
4232 ret = do_bind(arg1, arg2, arg3);
4233 break;
4234#endif
4235#ifdef TARGET_NR_connect
4236 case TARGET_NR_connect:
4237 ret = do_connect(arg1, arg2, arg3);
4238 break;
4239#endif
4240#ifdef TARGET_NR_getpeername
4241 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00004242 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00004243 break;
4244#endif
4245#ifdef TARGET_NR_getsockname
4246 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00004247 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00004248 break;
4249#endif
4250#ifdef TARGET_NR_getsockopt
4251 case TARGET_NR_getsockopt:
4252 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
4253 break;
4254#endif
4255#ifdef TARGET_NR_listen
4256 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00004257 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00004258 break;
4259#endif
4260#ifdef TARGET_NR_recv
4261 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00004262 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00004263 break;
4264#endif
4265#ifdef TARGET_NR_recvfrom
4266 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00004267 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00004268 break;
4269#endif
4270#ifdef TARGET_NR_recvmsg
4271 case TARGET_NR_recvmsg:
4272 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
4273 break;
4274#endif
4275#ifdef TARGET_NR_send
4276 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00004277 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00004278 break;
4279#endif
4280#ifdef TARGET_NR_sendmsg
4281 case TARGET_NR_sendmsg:
4282 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
4283 break;
4284#endif
4285#ifdef TARGET_NR_sendto
4286 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00004287 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00004288 break;
4289#endif
4290#ifdef TARGET_NR_shutdown
4291 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00004292 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00004293 break;
4294#endif
4295#ifdef TARGET_NR_socket
4296 case TARGET_NR_socket:
4297 ret = do_socket(arg1, arg2, arg3);
4298 break;
4299#endif
4300#ifdef TARGET_NR_socketpair
4301 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00004302 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00004303 break;
4304#endif
4305#ifdef TARGET_NR_setsockopt
4306 case TARGET_NR_setsockopt:
4307 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
4308 break;
4309#endif
ths7494b0f2007-02-11 18:26:53 +00004310
bellard31e31b82003-02-18 22:55:36 +00004311 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00004312 if (!(p = lock_user_string(arg2)))
4313 goto efault;
thse5574482007-02-11 20:03:13 +00004314 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
4315 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00004316 break;
4317
bellard31e31b82003-02-18 22:55:36 +00004318 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00004319 {
bellard66fb9762003-03-23 01:06:05 +00004320 struct itimerval value, ovalue, *pvalue;
4321
pbrook53a59602006-03-25 19:31:22 +00004322 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00004323 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00004324 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
4325 || copy_from_user_timeval(&pvalue->it_value,
4326 arg2 + sizeof(struct target_timeval)))
4327 goto efault;
bellard66fb9762003-03-23 01:06:05 +00004328 } else {
4329 pvalue = NULL;
4330 }
4331 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00004332 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00004333 if (copy_to_user_timeval(arg3,
4334 &ovalue.it_interval)
4335 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
4336 &ovalue.it_value))
4337 goto efault;
bellard66fb9762003-03-23 01:06:05 +00004338 }
4339 }
4340 break;
bellard31e31b82003-02-18 22:55:36 +00004341 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00004342 {
bellard66fb9762003-03-23 01:06:05 +00004343 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00004344
bellard66fb9762003-03-23 01:06:05 +00004345 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00004346 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00004347 if (copy_to_user_timeval(arg2,
4348 &value.it_interval)
4349 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
4350 &value.it_value))
4351 goto efault;
bellard66fb9762003-03-23 01:06:05 +00004352 }
4353 }
4354 break;
bellard31e31b82003-02-18 22:55:36 +00004355 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00004356 if (!(p = lock_user_string(arg1)))
4357 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004358 ret = get_errno(stat(path(p), &st));
4359 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004360 goto do_stat;
4361 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00004362 if (!(p = lock_user_string(arg1)))
4363 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004364 ret = get_errno(lstat(path(p), &st));
4365 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004366 goto do_stat;
4367 case TARGET_NR_fstat:
4368 {
4369 ret = get_errno(fstat(arg1, &st));
4370 do_stat:
4371 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004372 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00004373
bellard579a97f2007-11-11 14:26:47 +00004374 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4375 goto efault;
bellardd2fd1af2007-11-14 18:08:56 +00004376 __put_user(st.st_dev, &target_st->st_dev);
4377 __put_user(st.st_ino, &target_st->st_ino);
4378 __put_user(st.st_mode, &target_st->st_mode);
4379 __put_user(st.st_uid, &target_st->st_uid);
4380 __put_user(st.st_gid, &target_st->st_gid);
4381 __put_user(st.st_nlink, &target_st->st_nlink);
4382 __put_user(st.st_rdev, &target_st->st_rdev);
4383 __put_user(st.st_size, &target_st->st_size);
4384 __put_user(st.st_blksize, &target_st->st_blksize);
4385 __put_user(st.st_blocks, &target_st->st_blocks);
4386 __put_user(st.st_atime, &target_st->target_st_atime);
4387 __put_user(st.st_mtime, &target_st->target_st_mtime);
4388 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00004389 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00004390 }
4391 }
4392 break;
bellardebc05482003-09-30 21:08:41 +00004393#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00004394 case TARGET_NR_olduname:
4395 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004396#endif
4397#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00004398 case TARGET_NR_iopl:
4399 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004400#endif
bellard31e31b82003-02-18 22:55:36 +00004401 case TARGET_NR_vhangup:
4402 ret = get_errno(vhangup());
4403 break;
bellardebc05482003-09-30 21:08:41 +00004404#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00004405 case TARGET_NR_idle:
4406 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004407#endif
bellard42ad6ae2005-01-03 22:48:11 +00004408#ifdef TARGET_NR_syscall
4409 case TARGET_NR_syscall:
4410 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
4411 break;
4412#endif
bellard31e31b82003-02-18 22:55:36 +00004413 case TARGET_NR_wait4:
4414 {
4415 int status;
blueswir1992f48a2007-10-14 16:27:31 +00004416 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00004417 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00004418 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00004419 if (target_rusage)
4420 rusage_ptr = &rusage;
4421 else
4422 rusage_ptr = NULL;
4423 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
4424 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00004425 if (status_ptr) {
4426 if (put_user_s32(status, status_ptr))
4427 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004428 }
bellard2f619692007-11-16 10:46:05 +00004429 if (target_rusage)
4430 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00004431 }
4432 }
4433 break;
thse5febef2007-04-01 18:31:35 +00004434#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00004435 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00004436 if (!(p = lock_user_string(arg1)))
4437 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004438 ret = get_errno(swapoff(p));
4439 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004440 break;
thse5febef2007-04-01 18:31:35 +00004441#endif
bellard31e31b82003-02-18 22:55:36 +00004442 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00004443 {
pbrook53a59602006-03-25 19:31:22 +00004444 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00004445 struct sysinfo value;
4446 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00004447 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00004448 {
bellard579a97f2007-11-11 14:26:47 +00004449 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
4450 goto efault;
bellarda5448a72004-06-19 16:59:03 +00004451 __put_user(value.uptime, &target_value->uptime);
4452 __put_user(value.loads[0], &target_value->loads[0]);
4453 __put_user(value.loads[1], &target_value->loads[1]);
4454 __put_user(value.loads[2], &target_value->loads[2]);
4455 __put_user(value.totalram, &target_value->totalram);
4456 __put_user(value.freeram, &target_value->freeram);
4457 __put_user(value.sharedram, &target_value->sharedram);
4458 __put_user(value.bufferram, &target_value->bufferram);
4459 __put_user(value.totalswap, &target_value->totalswap);
4460 __put_user(value.freeswap, &target_value->freeswap);
4461 __put_user(value.procs, &target_value->procs);
4462 __put_user(value.totalhigh, &target_value->totalhigh);
4463 __put_user(value.freehigh, &target_value->freehigh);
4464 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00004465 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00004466 }
4467 }
4468 break;
thse5febef2007-04-01 18:31:35 +00004469#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00004470 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00004471 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
4472 break;
thse5febef2007-04-01 18:31:35 +00004473#endif
bellard31e31b82003-02-18 22:55:36 +00004474 case TARGET_NR_fsync:
4475 ret = get_errno(fsync(arg1));
4476 break;
bellard31e31b82003-02-18 22:55:36 +00004477 case TARGET_NR_clone:
bellard1b6b0292003-03-22 17:31:38 +00004478 ret = get_errno(do_fork(cpu_env, arg1, arg2));
4479 break;
bellardec86b0f2003-04-11 00:15:04 +00004480#ifdef __NR_exit_group
4481 /* new thread calls */
4482 case TARGET_NR_exit_group:
bellarde9009672005-04-26 20:42:36 +00004483 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00004484 ret = get_errno(exit_group(arg1));
4485 break;
4486#endif
bellard31e31b82003-02-18 22:55:36 +00004487 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00004488 if (!(p = lock_user_string(arg1)))
4489 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004490 ret = get_errno(setdomainname(p, arg2));
4491 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004492 break;
4493 case TARGET_NR_uname:
4494 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00004495 {
4496 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00004497
bellard579a97f2007-11-11 14:26:47 +00004498 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
4499 goto efault;
bellard29e619b2004-09-13 21:41:04 +00004500 ret = get_errno(sys_uname(buf));
4501 if (!is_error(ret)) {
4502 /* Overrite the native machine name with whatever is being
4503 emulated. */
4504 strcpy (buf->machine, UNAME_MACHINE);
pbrookc5937222006-05-14 11:30:38 +00004505 /* Allow the user to override the reported release. */
4506 if (qemu_uname_release && *qemu_uname_release)
4507 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00004508 }
pbrook53a59602006-03-25 19:31:22 +00004509 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00004510 }
bellard31e31b82003-02-18 22:55:36 +00004511 break;
bellard6dbad632003-03-16 18:05:05 +00004512#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00004513 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00004514 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00004515 break;
j_mayer84409dd2007-04-06 08:56:50 +00004516#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00004517 case TARGET_NR_vm86old:
4518 goto unimplemented;
4519 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00004520 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00004521 break;
4522#endif
j_mayer84409dd2007-04-06 08:56:50 +00004523#endif
bellard31e31b82003-02-18 22:55:36 +00004524 case TARGET_NR_adjtimex:
4525 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004526#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00004527 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00004528#endif
bellard31e31b82003-02-18 22:55:36 +00004529 case TARGET_NR_init_module:
4530 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00004531#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00004532 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00004533#endif
bellard31e31b82003-02-18 22:55:36 +00004534 goto unimplemented;
4535 case TARGET_NR_quotactl:
4536 goto unimplemented;
4537 case TARGET_NR_getpgid:
4538 ret = get_errno(getpgid(arg1));
4539 break;
4540 case TARGET_NR_fchdir:
4541 ret = get_errno(fchdir(arg1));
4542 break;
j_mayer84409dd2007-04-06 08:56:50 +00004543#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00004544 case TARGET_NR_bdflush:
4545 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00004546#endif
thse5febef2007-04-01 18:31:35 +00004547#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00004548 case TARGET_NR_sysfs:
4549 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004550#endif
bellard31e31b82003-02-18 22:55:36 +00004551 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00004552 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00004553 break;
thse5febef2007-04-01 18:31:35 +00004554#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00004555 case TARGET_NR_afs_syscall:
4556 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004557#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004558#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004559 case TARGET_NR__llseek:
4560 {
bellard4f2ac232004-04-26 19:44:02 +00004561#if defined (__x86_64__)
4562 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
bellard2f619692007-11-16 10:46:05 +00004563 if (put_user_s64(ret, arg4))
4564 goto efault;
bellard4f2ac232004-04-26 19:44:02 +00004565#else
bellard31e31b82003-02-18 22:55:36 +00004566 int64_t res;
4567 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard2f619692007-11-16 10:46:05 +00004568 if (put_user_s64(res, arg4))
4569 goto efault;
bellard4f2ac232004-04-26 19:44:02 +00004570#endif
bellard31e31b82003-02-18 22:55:36 +00004571 }
4572 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004573#endif
bellard31e31b82003-02-18 22:55:36 +00004574 case TARGET_NR_getdents:
blueswir1992f48a2007-10-14 16:27:31 +00004575#if TARGET_ABI_BITS != 32
pbrook53a59602006-03-25 19:31:22 +00004576 goto unimplemented;
blueswir1992f48a2007-10-14 16:27:31 +00004577#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00004578 {
pbrook53a59602006-03-25 19:31:22 +00004579 struct target_dirent *target_dirp;
bellard4add45b2003-06-05 01:52:59 +00004580 struct dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004581 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00004582
4583 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00004584 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00004585 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00004586 goto fail;
4587 }
ths3b46e622007-09-17 08:09:54 +00004588
bellard4add45b2003-06-05 01:52:59 +00004589 ret = get_errno(sys_getdents(arg1, dirp, count));
4590 if (!is_error(ret)) {
4591 struct dirent *de;
4592 struct target_dirent *tde;
4593 int len = ret;
4594 int reclen, treclen;
4595 int count1, tnamelen;
4596
4597 count1 = 0;
4598 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00004599 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4600 goto efault;
bellard4add45b2003-06-05 01:52:59 +00004601 tde = target_dirp;
4602 while (len > 0) {
4603 reclen = de->d_reclen;
blueswir1992f48a2007-10-14 16:27:31 +00004604 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
bellard4add45b2003-06-05 01:52:59 +00004605 tde->d_reclen = tswap16(treclen);
4606 tde->d_ino = tswapl(de->d_ino);
4607 tde->d_off = tswapl(de->d_off);
blueswir1992f48a2007-10-14 16:27:31 +00004608 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
bellard4add45b2003-06-05 01:52:59 +00004609 if (tnamelen > 256)
4610 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00004611 /* XXX: may not be correct */
bellard4add45b2003-06-05 01:52:59 +00004612 strncpy(tde->d_name, de->d_name, tnamelen);
4613 de = (struct dirent *)((char *)de + reclen);
4614 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00004615 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00004616 count1 += treclen;
4617 }
4618 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00004619 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00004620 }
4621 free(dirp);
4622 }
4623#else
bellard31e31b82003-02-18 22:55:36 +00004624 {
pbrook53a59602006-03-25 19:31:22 +00004625 struct dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004626 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00004627
bellard579a97f2007-11-11 14:26:47 +00004628 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4629 goto efault;
bellard72f03902003-02-18 23:33:18 +00004630 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00004631 if (!is_error(ret)) {
4632 struct dirent *de;
4633 int len = ret;
4634 int reclen;
4635 de = dirp;
4636 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004637 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00004638 if (reclen > len)
4639 break;
bellard8083a3e2003-03-24 23:12:16 +00004640 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00004641 tswapls(&de->d_ino);
4642 tswapls(&de->d_off);
4643 de = (struct dirent *)((char *)de + reclen);
4644 len -= reclen;
4645 }
4646 }
pbrook53a59602006-03-25 19:31:22 +00004647 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00004648 }
bellard4add45b2003-06-05 01:52:59 +00004649#endif
bellard31e31b82003-02-18 22:55:36 +00004650 break;
ths3ae43202007-09-16 21:39:48 +00004651#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00004652 case TARGET_NR_getdents64:
4653 {
pbrook53a59602006-03-25 19:31:22 +00004654 struct dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00004655 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00004656 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
4657 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00004658 ret = get_errno(sys_getdents64(arg1, dirp, count));
4659 if (!is_error(ret)) {
4660 struct dirent64 *de;
4661 int len = ret;
4662 int reclen;
4663 de = dirp;
4664 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004665 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00004666 if (reclen > len)
4667 break;
bellard8083a3e2003-03-24 23:12:16 +00004668 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00004669 tswap64s((uint64_t *)&de->d_ino);
4670 tswap64s((uint64_t *)&de->d_off);
bellarddab2ed92003-03-22 15:23:14 +00004671 de = (struct dirent64 *)((char *)de + reclen);
4672 len -= reclen;
4673 }
4674 }
pbrook53a59602006-03-25 19:31:22 +00004675 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00004676 }
4677 break;
bellarda541f292004-04-12 20:39:29 +00004678#endif /* TARGET_NR_getdents64 */
thse5febef2007-04-01 18:31:35 +00004679#ifdef TARGET_NR__newselect
bellard31e31b82003-02-18 22:55:36 +00004680 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00004681 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00004682 break;
thse5febef2007-04-01 18:31:35 +00004683#endif
4684#ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00004685 case TARGET_NR_poll:
4686 {
pbrook53a59602006-03-25 19:31:22 +00004687 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00004688 unsigned int nfds = arg2;
4689 int timeout = arg3;
4690 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00004691 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00004692
bellard579a97f2007-11-11 14:26:47 +00004693 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
4694 if (!target_pfd)
4695 goto efault;
bellard9de5e442003-03-23 16:49:39 +00004696 pfd = alloca(sizeof(struct pollfd) * nfds);
4697 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004698 pfd[i].fd = tswap32(target_pfd[i].fd);
4699 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00004700 }
4701 ret = get_errno(poll(pfd, nfds, timeout));
4702 if (!is_error(ret)) {
4703 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004704 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00004705 }
pbrook53a59602006-03-25 19:31:22 +00004706 ret += nfds * (sizeof(struct target_pollfd)
4707 - sizeof(struct pollfd));
bellard9de5e442003-03-23 16:49:39 +00004708 }
pbrook53a59602006-03-25 19:31:22 +00004709 unlock_user(target_pfd, arg1, ret);
bellard9de5e442003-03-23 16:49:39 +00004710 }
4711 break;
thse5febef2007-04-01 18:31:35 +00004712#endif
bellard31e31b82003-02-18 22:55:36 +00004713 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00004714 /* NOTE: the flock constant seems to be the same for every
4715 Linux platform */
4716 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00004717 break;
4718 case TARGET_NR_readv:
4719 {
4720 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00004721 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00004722
4723 vec = alloca(count * sizeof(struct iovec));
bellard579a97f2007-11-11 14:26:47 +00004724 lock_iovec(VERIFY_WRITE, vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00004725 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00004726 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00004727 }
4728 break;
4729 case TARGET_NR_writev:
4730 {
4731 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00004732 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00004733
4734 vec = alloca(count * sizeof(struct iovec));
bellard579a97f2007-11-11 14:26:47 +00004735 lock_iovec(VERIFY_READ, vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00004736 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00004737 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00004738 }
4739 break;
4740 case TARGET_NR_getsid:
4741 ret = get_errno(getsid(arg1));
4742 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004743#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00004744 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00004745 ret = get_errno(fdatasync(arg1));
4746 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004747#endif
bellard31e31b82003-02-18 22:55:36 +00004748 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00004749 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00004750 return value. */
ths0da46a62007-10-20 20:23:07 +00004751 ret = -TARGET_ENOTDIR;
4752 break;
bellard31e31b82003-02-18 22:55:36 +00004753 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00004754 {
pbrook53a59602006-03-25 19:31:22 +00004755 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004756 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00004757
bellard579a97f2007-11-11 14:26:47 +00004758 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
4759 goto efault;
bellard5cd43932003-03-29 16:54:36 +00004760 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004761 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00004762 ret = get_errno(sched_setparam(arg1, &schp));
4763 }
4764 break;
bellard31e31b82003-02-18 22:55:36 +00004765 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00004766 {
pbrook53a59602006-03-25 19:31:22 +00004767 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004768 struct sched_param schp;
4769 ret = get_errno(sched_getparam(arg1, &schp));
4770 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004771 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
4772 goto efault;
bellard5cd43932003-03-29 16:54:36 +00004773 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004774 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00004775 }
4776 }
4777 break;
bellard31e31b82003-02-18 22:55:36 +00004778 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00004779 {
pbrook53a59602006-03-25 19:31:22 +00004780 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004781 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00004782 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
4783 goto efault;
bellard5cd43932003-03-29 16:54:36 +00004784 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004785 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00004786 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
4787 }
4788 break;
bellard31e31b82003-02-18 22:55:36 +00004789 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00004790 ret = get_errno(sched_getscheduler(arg1));
4791 break;
bellard31e31b82003-02-18 22:55:36 +00004792 case TARGET_NR_sched_yield:
4793 ret = get_errno(sched_yield());
4794 break;
4795 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00004796 ret = get_errno(sched_get_priority_max(arg1));
4797 break;
bellard31e31b82003-02-18 22:55:36 +00004798 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00004799 ret = get_errno(sched_get_priority_min(arg1));
4800 break;
bellard31e31b82003-02-18 22:55:36 +00004801 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00004802 {
bellard5cd43932003-03-29 16:54:36 +00004803 struct timespec ts;
4804 ret = get_errno(sched_rr_get_interval(arg1, &ts));
4805 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004806 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00004807 }
4808 }
4809 break;
bellard31e31b82003-02-18 22:55:36 +00004810 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00004811 {
bellard1b6b0292003-03-22 17:31:38 +00004812 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00004813 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00004814 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00004815 if (is_error(ret) && arg2) {
4816 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00004817 }
4818 }
4819 break;
thse5febef2007-04-01 18:31:35 +00004820#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00004821 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00004822 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004823#endif
4824#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00004825 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00004826 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004827#endif
bellard31e31b82003-02-18 22:55:36 +00004828 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00004829 switch (arg1)
4830 {
4831 case PR_GET_PDEATHSIG:
4832 {
4833 int deathsig;
4834 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
bellard2f619692007-11-16 10:46:05 +00004835 if (!is_error(ret) && arg2
4836 && put_user_ual(deathsig, arg2))
4837 goto efault;
thse5574482007-02-11 20:03:13 +00004838 }
4839 break;
4840 default:
4841 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
4842 break;
4843 }
ths39b9aae2007-02-11 18:36:44 +00004844 break;
bellardd2fd1af2007-11-14 18:08:56 +00004845#ifdef TARGET_NR_arch_prctl
4846 case TARGET_NR_arch_prctl:
4847#if defined(TARGET_I386) && !defined(TARGET_ABI32)
4848 ret = do_arch_prctl(cpu_env, arg1, arg2);
4849 break;
4850#else
4851 goto unimplemented;
4852#endif
4853#endif
bellard67867302003-11-23 17:05:30 +00004854#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00004855 case TARGET_NR_pread:
bellard579a97f2007-11-11 14:26:47 +00004856 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
4857 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004858 ret = get_errno(pread(arg1, p, arg3, arg4));
4859 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00004860 break;
bellard31e31b82003-02-18 22:55:36 +00004861 case TARGET_NR_pwrite:
bellard579a97f2007-11-11 14:26:47 +00004862 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
4863 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004864 ret = get_errno(pwrite(arg1, p, arg3, arg4));
4865 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00004866 break;
bellard67867302003-11-23 17:05:30 +00004867#endif
bellard31e31b82003-02-18 22:55:36 +00004868 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00004869 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
4870 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004871 ret = get_errno(sys_getcwd1(p, arg2));
4872 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00004873 break;
4874 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00004875 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004876 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00004877 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004878 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00004879#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
4880 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
bellard579a97f2007-11-11 14:26:47 +00004881 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00004882 break;
4883#else
bellard5cd43932003-03-29 16:54:36 +00004884 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00004885#endif
bellard31e31b82003-02-18 22:55:36 +00004886 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00004887 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004888#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00004889 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00004890 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004891#endif
4892#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00004893 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00004894 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004895#endif
bellard048f6b42005-11-26 18:47:20 +00004896#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00004897 case TARGET_NR_vfork:
bellard1b6b0292003-03-22 17:31:38 +00004898 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00004899 break;
bellard048f6b42005-11-26 18:47:20 +00004900#endif
bellardebc05482003-09-30 21:08:41 +00004901#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00004902 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00004903 {
4904 struct rlimit rlim;
4905 ret = get_errno(getrlimit(arg1, &rlim));
4906 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004907 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00004908 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
4909 goto efault;
bellard728584b2003-04-29 20:43:36 +00004910 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
4911 target_rlim->rlim_max = tswapl(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00004912 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00004913 }
4914 break;
4915 }
bellardebc05482003-09-30 21:08:41 +00004916#endif
bellarda315a142005-01-30 22:59:18 +00004917#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00004918 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00004919 if (!(p = lock_user_string(arg1)))
4920 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004921 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
4922 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00004923 break;
bellarda315a142005-01-30 22:59:18 +00004924#endif
4925#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00004926 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00004927 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00004928 break;
bellarda315a142005-01-30 22:59:18 +00004929#endif
4930#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00004931 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00004932 if (!(p = lock_user_string(arg1)))
4933 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004934 ret = get_errno(stat(path(p), &st));
4935 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004936 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004937#endif
4938#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00004939 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00004940 if (!(p = lock_user_string(arg1)))
4941 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004942 ret = get_errno(lstat(path(p), &st));
4943 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004944 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004945#endif
4946#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00004947 case TARGET_NR_fstat64:
bellard60cd49d2003-03-16 22:53:56 +00004948 {
4949 ret = get_errno(fstat(arg1, &st));
4950 do_stat64:
4951 if (!is_error(ret)) {
pbrookce4defa2006-02-09 16:49:55 +00004952#ifdef TARGET_ARM
4953 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004954 struct target_eabi_stat64 *target_st;
bellard579a97f2007-11-11 14:26:47 +00004955
4956 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4957 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00004958 memset(target_st, 0, sizeof(struct target_eabi_stat64));
bellard579a97f2007-11-11 14:26:47 +00004959 __put_user(st.st_dev, &target_st->st_dev);
4960 __put_user(st.st_ino, &target_st->st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004961#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
bellard579a97f2007-11-11 14:26:47 +00004962 __put_user(st.st_ino, &target_st->__st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004963#endif
bellard579a97f2007-11-11 14:26:47 +00004964 __put_user(st.st_mode, &target_st->st_mode);
4965 __put_user(st.st_nlink, &target_st->st_nlink);
4966 __put_user(st.st_uid, &target_st->st_uid);
4967 __put_user(st.st_gid, &target_st->st_gid);
4968 __put_user(st.st_rdev, &target_st->st_rdev);
4969 __put_user(st.st_size, &target_st->st_size);
4970 __put_user(st.st_blksize, &target_st->st_blksize);
4971 __put_user(st.st_blocks, &target_st->st_blocks);
4972 __put_user(st.st_atime, &target_st->target_st_atime);
4973 __put_user(st.st_mtime, &target_st->target_st_mtime);
4974 __put_user(st.st_ctime, &target_st->target_st_ctime);
4975 unlock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00004976 } else
4977#endif
4978 {
pbrook53a59602006-03-25 19:31:22 +00004979 struct target_stat64 *target_st;
bellard579a97f2007-11-11 14:26:47 +00004980
4981 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
4982 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00004983 memset(target_st, 0, sizeof(struct target_stat64));
bellard579a97f2007-11-11 14:26:47 +00004984 __put_user(st.st_dev, &target_st->st_dev);
4985 __put_user(st.st_ino, &target_st->st_ino);
pbrookce4defa2006-02-09 16:49:55 +00004986#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
bellard579a97f2007-11-11 14:26:47 +00004987 __put_user(st.st_ino, &target_st->__st_ino);
pbrookce4defa2006-02-09 16:49:55 +00004988#endif
bellard579a97f2007-11-11 14:26:47 +00004989 __put_user(st.st_mode, &target_st->st_mode);
4990 __put_user(st.st_nlink, &target_st->st_nlink);
4991 __put_user(st.st_uid, &target_st->st_uid);
4992 __put_user(st.st_gid, &target_st->st_gid);
4993 __put_user(st.st_rdev, &target_st->st_rdev);
pbrookce4defa2006-02-09 16:49:55 +00004994 /* XXX: better use of kernel struct */
bellard579a97f2007-11-11 14:26:47 +00004995 __put_user(st.st_size, &target_st->st_size);
4996 __put_user(st.st_blksize, &target_st->st_blksize);
4997 __put_user(st.st_blocks, &target_st->st_blocks);
4998 __put_user(st.st_atime, &target_st->target_st_atime);
4999 __put_user(st.st_mtime, &target_st->target_st_mtime);
5000 __put_user(st.st_ctime, &target_st->target_st_ctime);
5001 unlock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00005002 }
bellard60cd49d2003-03-16 22:53:56 +00005003 }
5004 }
5005 break;
bellarda315a142005-01-30 22:59:18 +00005006#endif
bellard67867302003-11-23 17:05:30 +00005007#ifdef USE_UID16
5008 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00005009 if (!(p = lock_user_string(arg1)))
5010 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005011 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
5012 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00005013 break;
5014 case TARGET_NR_getuid:
5015 ret = get_errno(high2lowuid(getuid()));
5016 break;
5017 case TARGET_NR_getgid:
5018 ret = get_errno(high2lowgid(getgid()));
5019 break;
5020 case TARGET_NR_geteuid:
5021 ret = get_errno(high2lowuid(geteuid()));
5022 break;
5023 case TARGET_NR_getegid:
5024 ret = get_errno(high2lowgid(getegid()));
5025 break;
5026 case TARGET_NR_setreuid:
5027 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
5028 break;
5029 case TARGET_NR_setregid:
5030 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
5031 break;
5032 case TARGET_NR_getgroups:
5033 {
5034 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00005035 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00005036 gid_t *grouplist;
5037 int i;
5038
5039 grouplist = alloca(gidsetsize * sizeof(gid_t));
5040 ret = get_errno(getgroups(gidsetsize, grouplist));
5041 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005042 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
5043 if (!target_grouplist)
5044 goto efault;
bellard67867302003-11-23 17:05:30 +00005045 for(i = 0;i < gidsetsize; i++)
5046 target_grouplist[i] = tswap16(grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00005047 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00005048 }
5049 }
5050 break;
5051 case TARGET_NR_setgroups:
5052 {
5053 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00005054 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00005055 gid_t *grouplist;
5056 int i;
5057
5058 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00005059 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
5060 if (!target_grouplist) {
5061 ret = -TARGET_EFAULT;
5062 goto fail;
5063 }
bellard67867302003-11-23 17:05:30 +00005064 for(i = 0;i < gidsetsize; i++)
5065 grouplist[i] = tswap16(target_grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00005066 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00005067 ret = get_errno(setgroups(gidsetsize, grouplist));
5068 }
5069 break;
5070 case TARGET_NR_fchown:
5071 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
5072 break;
thsccfa72b2007-09-24 09:23:34 +00005073#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
5074 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00005075 if (!(p = lock_user_string(arg2)))
5076 goto efault;
5077 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
5078 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00005079 break;
5080#endif
bellard67867302003-11-23 17:05:30 +00005081#ifdef TARGET_NR_setresuid
5082 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00005083 ret = get_errno(setresuid(low2highuid(arg1),
5084 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00005085 low2highuid(arg3)));
5086 break;
5087#endif
5088#ifdef TARGET_NR_getresuid
5089 case TARGET_NR_getresuid:
5090 {
pbrook53a59602006-03-25 19:31:22 +00005091 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00005092 ret = get_errno(getresuid(&ruid, &euid, &suid));
5093 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005094 if (put_user_u16(high2lowuid(ruid), arg1)
5095 || put_user_u16(high2lowuid(euid), arg2)
5096 || put_user_u16(high2lowuid(suid), arg3))
5097 goto efault;
bellard67867302003-11-23 17:05:30 +00005098 }
5099 }
5100 break;
5101#endif
5102#ifdef TARGET_NR_getresgid
5103 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00005104 ret = get_errno(setresgid(low2highgid(arg1),
5105 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00005106 low2highgid(arg3)));
5107 break;
5108#endif
5109#ifdef TARGET_NR_getresgid
5110 case TARGET_NR_getresgid:
5111 {
pbrook53a59602006-03-25 19:31:22 +00005112 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00005113 ret = get_errno(getresgid(&rgid, &egid, &sgid));
5114 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005115 if (put_user_u16(high2lowgid(rgid), arg1)
5116 || put_user_u16(high2lowgid(egid), arg2)
5117 || put_user_u16(high2lowgid(sgid), arg3))
5118 goto efault;
bellard67867302003-11-23 17:05:30 +00005119 }
5120 }
5121 break;
5122#endif
5123 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00005124 if (!(p = lock_user_string(arg1)))
5125 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005126 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
5127 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00005128 break;
5129 case TARGET_NR_setuid:
5130 ret = get_errno(setuid(low2highuid(arg1)));
5131 break;
5132 case TARGET_NR_setgid:
5133 ret = get_errno(setgid(low2highgid(arg1)));
5134 break;
5135 case TARGET_NR_setfsuid:
5136 ret = get_errno(setfsuid(arg1));
5137 break;
5138 case TARGET_NR_setfsgid:
5139 ret = get_errno(setfsgid(arg1));
5140 break;
5141#endif /* USE_UID16 */
5142
bellarda315a142005-01-30 22:59:18 +00005143#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00005144 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00005145 if (!(p = lock_user_string(arg1)))
5146 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005147 ret = get_errno(lchown(p, arg2, arg3));
5148 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00005149 break;
bellarda315a142005-01-30 22:59:18 +00005150#endif
5151#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00005152 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00005153 ret = get_errno(getuid());
5154 break;
bellarda315a142005-01-30 22:59:18 +00005155#endif
5156#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00005157 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00005158 ret = get_errno(getgid());
5159 break;
bellarda315a142005-01-30 22:59:18 +00005160#endif
5161#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00005162 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00005163 ret = get_errno(geteuid());
5164 break;
bellarda315a142005-01-30 22:59:18 +00005165#endif
5166#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00005167 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00005168 ret = get_errno(getegid());
5169 break;
bellarda315a142005-01-30 22:59:18 +00005170#endif
5171#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00005172 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00005173 ret = get_errno(setreuid(arg1, arg2));
5174 break;
bellarda315a142005-01-30 22:59:18 +00005175#endif
5176#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00005177 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00005178 ret = get_errno(setregid(arg1, arg2));
5179 break;
bellarda315a142005-01-30 22:59:18 +00005180#endif
5181#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00005182 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00005183 {
5184 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00005185 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00005186 gid_t *grouplist;
5187 int i;
5188
5189 grouplist = alloca(gidsetsize * sizeof(gid_t));
5190 ret = get_errno(getgroups(gidsetsize, grouplist));
5191 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005192 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
5193 if (!target_grouplist) {
5194 ret = -TARGET_EFAULT;
5195 goto fail;
5196 }
bellard99c475a2005-01-31 20:45:13 +00005197 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00005198 target_grouplist[i] = tswap32(grouplist[i]);
5199 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00005200 }
5201 }
5202 break;
bellarda315a142005-01-30 22:59:18 +00005203#endif
5204#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00005205 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00005206 {
5207 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00005208 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00005209 gid_t *grouplist;
5210 int i;
ths3b46e622007-09-17 08:09:54 +00005211
bellard99c475a2005-01-31 20:45:13 +00005212 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00005213 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
5214 if (!target_grouplist) {
5215 ret = -TARGET_EFAULT;
5216 goto fail;
5217 }
bellard99c475a2005-01-31 20:45:13 +00005218 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00005219 grouplist[i] = tswap32(target_grouplist[i]);
5220 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00005221 ret = get_errno(setgroups(gidsetsize, grouplist));
5222 }
5223 break;
bellarda315a142005-01-30 22:59:18 +00005224#endif
5225#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00005226 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00005227 ret = get_errno(fchown(arg1, arg2, arg3));
5228 break;
bellarda315a142005-01-30 22:59:18 +00005229#endif
5230#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00005231 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00005232 ret = get_errno(setresuid(arg1, arg2, arg3));
5233 break;
bellarda315a142005-01-30 22:59:18 +00005234#endif
5235#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00005236 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00005237 {
pbrook53a59602006-03-25 19:31:22 +00005238 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00005239 ret = get_errno(getresuid(&ruid, &euid, &suid));
5240 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005241 if (put_user_u32(ruid, arg1)
5242 || put_user_u32(euid, arg2)
5243 || put_user_u32(suid, arg3))
5244 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00005245 }
5246 }
5247 break;
bellarda315a142005-01-30 22:59:18 +00005248#endif
5249#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00005250 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00005251 ret = get_errno(setresgid(arg1, arg2, arg3));
5252 break;
bellarda315a142005-01-30 22:59:18 +00005253#endif
5254#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00005255 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00005256 {
pbrook53a59602006-03-25 19:31:22 +00005257 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00005258 ret = get_errno(getresgid(&rgid, &egid, &sgid));
5259 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005260 if (put_user_u32(rgid, arg1)
5261 || put_user_u32(egid, arg2)
5262 || put_user_u32(sgid, arg3))
5263 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00005264 }
5265 }
5266 break;
bellarda315a142005-01-30 22:59:18 +00005267#endif
5268#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00005269 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00005270 if (!(p = lock_user_string(arg1)))
5271 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005272 ret = get_errno(chown(p, arg2, arg3));
5273 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00005274 break;
bellarda315a142005-01-30 22:59:18 +00005275#endif
5276#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00005277 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00005278 ret = get_errno(setuid(arg1));
5279 break;
bellarda315a142005-01-30 22:59:18 +00005280#endif
5281#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00005282 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00005283 ret = get_errno(setgid(arg1));
5284 break;
bellarda315a142005-01-30 22:59:18 +00005285#endif
5286#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00005287 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00005288 ret = get_errno(setfsuid(arg1));
5289 break;
bellarda315a142005-01-30 22:59:18 +00005290#endif
5291#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00005292 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00005293 ret = get_errno(setfsgid(arg1));
5294 break;
bellarda315a142005-01-30 22:59:18 +00005295#endif
bellard67867302003-11-23 17:05:30 +00005296
bellard31e31b82003-02-18 22:55:36 +00005297 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00005298 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00005299#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00005300 case TARGET_NR_mincore:
bellardb03c60f2003-03-23 17:19:56 +00005301 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00005302#endif
5303#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00005304 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00005305 /* A straight passthrough may not be safe because qemu sometimes
5306 turns private flie-backed mappings into anonymous mappings.
5307 This will break MADV_DONTNEED.
5308 This is a hint, so ignoring and returning success is ok. */
5309 ret = get_errno(0);
5310 break;
bellardffa65c32004-01-04 23:57:22 +00005311#endif
blueswir1992f48a2007-10-14 16:27:31 +00005312#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00005313 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00005314 {
thsb1e341e2007-03-20 21:50:52 +00005315 int cmd;
bellard77e46722003-04-29 20:39:06 +00005316 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00005317 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00005318#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00005319 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00005320#endif
bellard77e46722003-04-29 20:39:06 +00005321
thsb1e341e2007-03-20 21:50:52 +00005322 switch(arg2){
5323 case TARGET_F_GETLK64:
5324 cmd = F_GETLK64;
thsa7222582007-03-22 15:08:55 +00005325 break;
thsb1e341e2007-03-20 21:50:52 +00005326 case TARGET_F_SETLK64:
5327 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00005328 break;
thsb1e341e2007-03-20 21:50:52 +00005329 case TARGET_F_SETLKW64:
5330 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00005331 break;
thsb1e341e2007-03-20 21:50:52 +00005332 default:
5333 cmd = arg2;
thsa7222582007-03-22 15:08:55 +00005334 break;
thsb1e341e2007-03-20 21:50:52 +00005335 }
5336
bellard60cd49d2003-03-16 22:53:56 +00005337 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00005338 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00005339#ifdef TARGET_ARM
5340 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00005341 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5342 goto efault;
ths58134272007-03-31 18:59:32 +00005343 fl.l_type = tswap16(target_efl->l_type);
5344 fl.l_whence = tswap16(target_efl->l_whence);
5345 fl.l_start = tswap64(target_efl->l_start);
5346 fl.l_len = tswap64(target_efl->l_len);
5347 fl.l_pid = tswapl(target_efl->l_pid);
5348 unlock_user_struct(target_efl, arg3, 0);
5349 } else
5350#endif
5351 {
bellard9ee1fa22007-11-11 15:11:19 +00005352 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5353 goto efault;
ths58134272007-03-31 18:59:32 +00005354 fl.l_type = tswap16(target_fl->l_type);
5355 fl.l_whence = tswap16(target_fl->l_whence);
5356 fl.l_start = tswap64(target_fl->l_start);
5357 fl.l_len = tswap64(target_fl->l_len);
5358 fl.l_pid = tswapl(target_fl->l_pid);
5359 unlock_user_struct(target_fl, arg3, 0);
5360 }
thsb1e341e2007-03-20 21:50:52 +00005361 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00005362 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00005363#ifdef TARGET_ARM
5364 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00005365 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
5366 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005367 target_efl->l_type = tswap16(fl.l_type);
5368 target_efl->l_whence = tswap16(fl.l_whence);
5369 target_efl->l_start = tswap64(fl.l_start);
5370 target_efl->l_len = tswap64(fl.l_len);
5371 target_efl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00005372 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00005373 } else
5374#endif
5375 {
bellard9ee1fa22007-11-11 15:11:19 +00005376 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
5377 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005378 target_fl->l_type = tswap16(fl.l_type);
5379 target_fl->l_whence = tswap16(fl.l_whence);
5380 target_fl->l_start = tswap64(fl.l_start);
5381 target_fl->l_len = tswap64(fl.l_len);
5382 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00005383 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00005384 }
bellard77e46722003-04-29 20:39:06 +00005385 }
5386 break;
5387
thsb1e341e2007-03-20 21:50:52 +00005388 case TARGET_F_SETLK64:
5389 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00005390#ifdef TARGET_ARM
5391 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00005392 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
5393 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005394 fl.l_type = tswap16(target_efl->l_type);
5395 fl.l_whence = tswap16(target_efl->l_whence);
5396 fl.l_start = tswap64(target_efl->l_start);
5397 fl.l_len = tswap64(target_efl->l_len);
5398 fl.l_pid = tswapl(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00005399 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00005400 } else
5401#endif
5402 {
bellard9ee1fa22007-11-11 15:11:19 +00005403 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
5404 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00005405 fl.l_type = tswap16(target_fl->l_type);
5406 fl.l_whence = tswap16(target_fl->l_whence);
5407 fl.l_start = tswap64(target_fl->l_start);
5408 fl.l_len = tswap64(target_fl->l_len);
5409 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00005410 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00005411 }
thsb1e341e2007-03-20 21:50:52 +00005412 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00005413 break;
bellard60cd49d2003-03-16 22:53:56 +00005414 default:
bellard9ee1fa22007-11-11 15:11:19 +00005415 ret = do_fcntl(arg1, cmd, arg3);
bellard60cd49d2003-03-16 22:53:56 +00005416 break;
5417 }
bellard77e46722003-04-29 20:39:06 +00005418 break;
5419 }
bellard60cd49d2003-03-16 22:53:56 +00005420#endif
ths7d600c82006-12-08 01:32:58 +00005421#ifdef TARGET_NR_cacheflush
5422 case TARGET_NR_cacheflush:
5423 /* self-modifying code is handled automatically, so nothing needed */
5424 ret = 0;
5425 break;
5426#endif
bellardebc05482003-09-30 21:08:41 +00005427#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00005428 case TARGET_NR_security:
5429 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005430#endif
bellardc573ff62004-01-04 15:51:36 +00005431#ifdef TARGET_NR_getpagesize
5432 case TARGET_NR_getpagesize:
5433 ret = TARGET_PAGE_SIZE;
5434 break;
5435#endif
bellard31e31b82003-02-18 22:55:36 +00005436 case TARGET_NR_gettid:
5437 ret = get_errno(gettid());
5438 break;
thse5febef2007-04-01 18:31:35 +00005439#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00005440 case TARGET_NR_readahead:
bellard5cd43932003-03-29 16:54:36 +00005441 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005442#endif
bellardebc05482003-09-30 21:08:41 +00005443#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00005444 case TARGET_NR_setxattr:
5445 case TARGET_NR_lsetxattr:
5446 case TARGET_NR_fsetxattr:
5447 case TARGET_NR_getxattr:
5448 case TARGET_NR_lgetxattr:
5449 case TARGET_NR_fgetxattr:
5450 case TARGET_NR_listxattr:
5451 case TARGET_NR_llistxattr:
5452 case TARGET_NR_flistxattr:
5453 case TARGET_NR_removexattr:
5454 case TARGET_NR_lremovexattr:
5455 case TARGET_NR_fremovexattr:
bellard5cd43932003-03-29 16:54:36 +00005456 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00005457#endif
5458#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00005459 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00005460#if defined(TARGET_MIPS)
ths6f5b89a2007-03-02 20:48:00 +00005461 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
5462 ret = 0;
5463 break;
bellard8d18e892007-11-14 15:18:40 +00005464#elif defined(TARGET_I386) && defined(TARGET_ABI32)
5465 ret = do_set_thread_area(cpu_env, arg1);
5466 break;
ths6f5b89a2007-03-02 20:48:00 +00005467#else
5468 goto unimplemented_nowarn;
5469#endif
5470#endif
5471#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00005472 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00005473#if defined(TARGET_I386) && defined(TARGET_ABI32)
5474 ret = do_get_thread_area(cpu_env, arg1);
5475#else
bellard5cd43932003-03-29 16:54:36 +00005476 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00005477#endif
bellard8d18e892007-11-14 15:18:40 +00005478#endif
bellard48dc41e2006-06-21 18:15:50 +00005479#ifdef TARGET_NR_getdomainname
5480 case TARGET_NR_getdomainname:
5481 goto unimplemented_nowarn;
5482#endif
ths6f5b89a2007-03-02 20:48:00 +00005483
thsb5906f92007-03-19 13:32:45 +00005484#ifdef TARGET_NR_clock_gettime
5485 case TARGET_NR_clock_gettime:
5486 {
5487 struct timespec ts;
5488 ret = get_errno(clock_gettime(arg1, &ts));
5489 if (!is_error(ret)) {
5490 host_to_target_timespec(arg2, &ts);
5491 }
5492 break;
5493 }
5494#endif
5495#ifdef TARGET_NR_clock_getres
5496 case TARGET_NR_clock_getres:
5497 {
5498 struct timespec ts;
5499 ret = get_errno(clock_getres(arg1, &ts));
5500 if (!is_error(ret)) {
5501 host_to_target_timespec(arg2, &ts);
5502 }
5503 break;
5504 }
5505#endif
5506
ths6f5b89a2007-03-02 20:48:00 +00005507#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
5508 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00005509 ret = get_errno(set_tid_address((int *)g2h(arg1)));
5510 break;
ths6f5b89a2007-03-02 20:48:00 +00005511#endif
5512
ths3ae43202007-09-16 21:39:48 +00005513#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00005514 case TARGET_NR_tkill:
5515 ret = get_errno(sys_tkill((int)arg1, (int)arg2));
5516 break;
5517#endif
5518
ths3ae43202007-09-16 21:39:48 +00005519#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00005520 case TARGET_NR_tgkill:
5521 ret = get_errno(sys_tgkill((int)arg1, (int)arg2, (int)arg3));
5522 break;
5523#endif
5524
ths4f2b1fe2007-06-21 21:57:12 +00005525#ifdef TARGET_NR_set_robust_list
5526 case TARGET_NR_set_robust_list:
5527 goto unimplemented_nowarn;
5528#endif
5529
ths9007f0e2007-09-25 17:50:37 +00005530#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
5531 case TARGET_NR_utimensat:
5532 {
5533 struct timespec ts[2];
5534 target_to_host_timespec(ts, arg3);
5535 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
5536 if (!arg2)
5537 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
5538 else {
bellard579a97f2007-11-11 14:26:47 +00005539 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00005540 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00005541 goto fail;
5542 }
5543 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
5544 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00005545 }
5546 }
5547 break;
5548#endif
5549
bellard31e31b82003-02-18 22:55:36 +00005550 default:
5551 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00005552 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00005553#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 +00005554 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00005555#endif
ths0da46a62007-10-20 20:23:07 +00005556 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00005557 break;
5558 }
bellard579a97f2007-11-11 14:26:47 +00005559fail:
bellardc573ff62004-01-04 15:51:36 +00005560#ifdef DEBUG
5561 gemu_log(" = %ld\n", ret);
5562#endif
thsb92c47c2007-11-01 00:07:38 +00005563 if(do_strace)
5564 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00005565 return ret;
bellard579a97f2007-11-11 14:26:47 +00005566efault:
5567 ret = -TARGET_EFAULT;
5568 goto fail;
bellard31e31b82003-02-18 22:55:36 +00005569}