blob: af5b9d9225fcb1e982037c7fd2aec2e0da75c87f [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) \
ths56e07942007-04-26 23:49:45 +000077 || defined(TARGET_M68K) || defined(TARGET_SH4)
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
ths64f0ce42007-09-24 09:25:06 +0000148#define __NR_sys_linkat __NR_linkat
ths4472ad02007-09-24 09:22:32 +0000149#define __NR_sys_mkdirat __NR_mkdirat
ths75ac37a2007-09-24 09:23:05 +0000150#define __NR_sys_mknodat __NR_mknodat
ths82424832007-09-24 09:21:55 +0000151#define __NR_sys_openat __NR_openat
ths5e0ccb12007-09-24 09:26:10 +0000152#define __NR_sys_readlinkat __NR_readlinkat
ths722183f2007-09-24 09:24:37 +0000153#define __NR_sys_renameat __NR_renameat
bellard66fb9762003-03-23 01:06:05 +0000154#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
thsf0b62432007-09-24 09:25:40 +0000155#define __NR_sys_symlinkat __NR_symlinkat
ths7494b0f2007-02-11 18:26:53 +0000156#define __NR_sys_syslog __NR_syslog
ths71455572007-06-21 21:45:30 +0000157#define __NR_sys_tgkill __NR_tgkill
ths4cae1d12007-07-12 11:06:53 +0000158#define __NR_sys_tkill __NR_tkill
ths8170f562007-09-24 09:24:11 +0000159#define __NR_sys_unlinkat __NR_unlinkat
ths9007f0e2007-09-25 17:50:37 +0000160#define __NR_sys_utimensat __NR_utimensat
bellard31e31b82003-02-18 22:55:36 +0000161
bellardbc51c5c2004-03-17 23:46:04 +0000162#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
bellard9af9eaa2003-04-07 21:34:41 +0000163#define __NR__llseek __NR_lseek
164#endif
165
bellard72f03902003-02-18 23:33:18 +0000166#ifdef __NR_gettid
bellard31e31b82003-02-18 22:55:36 +0000167_syscall0(int, gettid)
bellard72f03902003-02-18 23:33:18 +0000168#else
169static int gettid(void) {
170 return -ENOSYS;
171}
172#endif
bellard31e31b82003-02-18 22:55:36 +0000173_syscall1(int,sys_uname,struct new_utsname *,buf)
ths92a34c12007-09-24 09:27:49 +0000174#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
175_syscall4(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode,int,flags)
176#endif
ths814d7972007-09-24 09:26:51 +0000177#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
178_syscall4(int,sys_fchmodat,int,dirfd,const char *,pathname,
179 mode_t,mode,int,flags)
180#endif
thsccfa72b2007-09-24 09:23:34 +0000181#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
182_syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
183 uid_t,owner,gid_t,group,int,flags)
184#endif
bellard72f03902003-02-18 23:33:18 +0000185_syscall2(int,sys_getcwd1,char *,buf,size_t,size)
186_syscall3(int, sys_getdents, uint, fd, struct dirent *, dirp, uint, count);
ths3ae43202007-09-16 21:39:48 +0000187#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +0000188_syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);
ths3ae43202007-09-16 21:39:48 +0000189#endif
bellard31e31b82003-02-18 22:55:36 +0000190_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
191 loff_t *, res, uint, wh);
ths64f0ce42007-09-24 09:25:06 +0000192#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
193_syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
194 int,newdirfd,const char *,newpath,int,flags)
195#endif
ths4472ad02007-09-24 09:22:32 +0000196#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
197_syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
198#endif
ths75ac37a2007-09-24 09:23:05 +0000199#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
200_syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
201 mode_t,mode,dev_t,dev)
202#endif
ths82424832007-09-24 09:21:55 +0000203#if defined(TARGET_NR_openat) && defined(__NR_openat)
204_syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
205#endif
ths5e0ccb12007-09-24 09:26:10 +0000206#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
207_syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
208 char *,buf,size_t,bufsize)
209#endif
ths722183f2007-09-24 09:24:37 +0000210#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
211_syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
212 int,newdirfd,const char *,newpath)
213#endif
bellard66fb9762003-03-23 01:06:05 +0000214_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
thsb51eaa82007-09-25 16:09:22 +0000215#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +0000216_syscall3(int,sys_symlinkat,const char *,oldpath,
217 int,newdirfd,const char *,newpath)
218#endif
ths7494b0f2007-02-11 18:26:53 +0000219_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
ths3ae43202007-09-16 21:39:48 +0000220#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +0000221_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
ths4cae1d12007-07-12 11:06:53 +0000222#endif
ths3ae43202007-09-16 21:39:48 +0000223#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +0000224_syscall2(int,sys_tkill,int,tid,int,sig)
225#endif
bellardec86b0f2003-04-11 00:15:04 +0000226#ifdef __NR_exit_group
227_syscall1(int,exit_group,int,error_code)
228#endif
ths6f5b89a2007-03-02 20:48:00 +0000229#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
230_syscall1(int,set_tid_address,int *,tidptr)
231#endif
ths8170f562007-09-24 09:24:11 +0000232#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
233_syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
234#endif
ths9007f0e2007-09-25 17:50:37 +0000235#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
236_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
237 const struct timespec *,tsp,int,flags)
238#endif
bellard66fb9762003-03-23 01:06:05 +0000239
240extern int personality(int);
bellard9de5e442003-03-23 16:49:39 +0000241extern int flock(int, int);
242extern int setfsuid(int);
243extern int setfsgid(int);
bellard5cd43932003-03-29 16:54:36 +0000244extern int setresuid(uid_t, uid_t, uid_t);
245extern int getresuid(uid_t *, uid_t *, uid_t *);
246extern int setresgid(gid_t, gid_t, gid_t);
247extern int getresgid(gid_t *, gid_t *, gid_t *);
bellard19b84f32003-05-08 15:41:49 +0000248extern int setgroups(int, gid_t *);
bellard31e31b82003-02-18 22:55:36 +0000249
ths637947f2007-06-01 12:09:19 +0000250/*
thsfe8f0962007-07-12 10:59:21 +0000251 * This list is the union of errno values overridden in asm-<arch>/errno.h
ths637947f2007-06-01 12:09:19 +0000252 * minus the errnos that are not actually generic to all archs.
253 */
254static uint16_t host_to_target_errno_table[1200] = {
255 [EIDRM] = TARGET_EIDRM,
256 [ECHRNG] = TARGET_ECHRNG,
257 [EL2NSYNC] = TARGET_EL2NSYNC,
258 [EL3HLT] = TARGET_EL3HLT,
259 [EL3RST] = TARGET_EL3RST,
260 [ELNRNG] = TARGET_ELNRNG,
261 [EUNATCH] = TARGET_EUNATCH,
262 [ENOCSI] = TARGET_ENOCSI,
263 [EL2HLT] = TARGET_EL2HLT,
264 [EDEADLK] = TARGET_EDEADLK,
265 [ENOLCK] = TARGET_ENOLCK,
266 [EBADE] = TARGET_EBADE,
267 [EBADR] = TARGET_EBADR,
268 [EXFULL] = TARGET_EXFULL,
269 [ENOANO] = TARGET_ENOANO,
270 [EBADRQC] = TARGET_EBADRQC,
271 [EBADSLT] = TARGET_EBADSLT,
272 [EBFONT] = TARGET_EBFONT,
273 [ENOSTR] = TARGET_ENOSTR,
274 [ENODATA] = TARGET_ENODATA,
275 [ETIME] = TARGET_ETIME,
276 [ENOSR] = TARGET_ENOSR,
277 [ENONET] = TARGET_ENONET,
278 [ENOPKG] = TARGET_ENOPKG,
279 [EREMOTE] = TARGET_EREMOTE,
280 [ENOLINK] = TARGET_ENOLINK,
281 [EADV] = TARGET_EADV,
282 [ESRMNT] = TARGET_ESRMNT,
283 [ECOMM] = TARGET_ECOMM,
284 [EPROTO] = TARGET_EPROTO,
285 [EDOTDOT] = TARGET_EDOTDOT,
286 [EMULTIHOP] = TARGET_EMULTIHOP,
287 [EBADMSG] = TARGET_EBADMSG,
288 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
289 [EOVERFLOW] = TARGET_EOVERFLOW,
290 [ENOTUNIQ] = TARGET_ENOTUNIQ,
291 [EBADFD] = TARGET_EBADFD,
292 [EREMCHG] = TARGET_EREMCHG,
293 [ELIBACC] = TARGET_ELIBACC,
294 [ELIBBAD] = TARGET_ELIBBAD,
295 [ELIBSCN] = TARGET_ELIBSCN,
296 [ELIBMAX] = TARGET_ELIBMAX,
297 [ELIBEXEC] = TARGET_ELIBEXEC,
298 [EILSEQ] = TARGET_EILSEQ,
299 [ENOSYS] = TARGET_ENOSYS,
300 [ELOOP] = TARGET_ELOOP,
301 [ERESTART] = TARGET_ERESTART,
302 [ESTRPIPE] = TARGET_ESTRPIPE,
303 [ENOTEMPTY] = TARGET_ENOTEMPTY,
304 [EUSERS] = TARGET_EUSERS,
305 [ENOTSOCK] = TARGET_ENOTSOCK,
306 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
307 [EMSGSIZE] = TARGET_EMSGSIZE,
308 [EPROTOTYPE] = TARGET_EPROTOTYPE,
309 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
310 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
311 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
312 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
313 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
314 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
315 [EADDRINUSE] = TARGET_EADDRINUSE,
316 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
317 [ENETDOWN] = TARGET_ENETDOWN,
318 [ENETUNREACH] = TARGET_ENETUNREACH,
319 [ENETRESET] = TARGET_ENETRESET,
320 [ECONNABORTED] = TARGET_ECONNABORTED,
321 [ECONNRESET] = TARGET_ECONNRESET,
322 [ENOBUFS] = TARGET_ENOBUFS,
323 [EISCONN] = TARGET_EISCONN,
324 [ENOTCONN] = TARGET_ENOTCONN,
325 [EUCLEAN] = TARGET_EUCLEAN,
326 [ENOTNAM] = TARGET_ENOTNAM,
327 [ENAVAIL] = TARGET_ENAVAIL,
328 [EISNAM] = TARGET_EISNAM,
329 [EREMOTEIO] = TARGET_EREMOTEIO,
330 [ESHUTDOWN] = TARGET_ESHUTDOWN,
331 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
332 [ETIMEDOUT] = TARGET_ETIMEDOUT,
333 [ECONNREFUSED] = TARGET_ECONNREFUSED,
334 [EHOSTDOWN] = TARGET_EHOSTDOWN,
335 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
336 [EALREADY] = TARGET_EALREADY,
337 [EINPROGRESS] = TARGET_EINPROGRESS,
338 [ESTALE] = TARGET_ESTALE,
339 [ECANCELED] = TARGET_ECANCELED,
340 [ENOMEDIUM] = TARGET_ENOMEDIUM,
341 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
thsb7fe5db2007-07-23 15:37:46 +0000342#ifdef ENOKEY
ths637947f2007-06-01 12:09:19 +0000343 [ENOKEY] = TARGET_ENOKEY,
thsb7fe5db2007-07-23 15:37:46 +0000344#endif
345#ifdef EKEYEXPIRED
ths637947f2007-06-01 12:09:19 +0000346 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
thsb7fe5db2007-07-23 15:37:46 +0000347#endif
348#ifdef EKEYREVOKED
ths637947f2007-06-01 12:09:19 +0000349 [EKEYREVOKED] = TARGET_EKEYREVOKED,
thsb7fe5db2007-07-23 15:37:46 +0000350#endif
351#ifdef EKEYREJECTED
ths637947f2007-06-01 12:09:19 +0000352 [EKEYREJECTED] = TARGET_EKEYREJECTED,
thsb7fe5db2007-07-23 15:37:46 +0000353#endif
354#ifdef EOWNERDEAD
ths637947f2007-06-01 12:09:19 +0000355 [EOWNERDEAD] = TARGET_EOWNERDEAD,
thsb7fe5db2007-07-23 15:37:46 +0000356#endif
357#ifdef ENOTRECOVERABLE
ths637947f2007-06-01 12:09:19 +0000358 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
thsb7fe5db2007-07-23 15:37:46 +0000359#endif
ths637947f2007-06-01 12:09:19 +0000360 };
361
362static inline int host_to_target_errno(int err)
363{
364 if(host_to_target_errno_table[err])
365 return host_to_target_errno_table[err];
366 return err;
367}
368
j_mayer32407102007-09-26 23:01:49 +0000369static inline target_long get_errno(target_long ret)
bellard31e31b82003-02-18 22:55:36 +0000370{
371 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000372 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000373 else
374 return ret;
375}
376
j_mayer32407102007-09-26 23:01:49 +0000377static inline int is_error(target_long ret)
bellard31e31b82003-02-18 22:55:36 +0000378{
j_mayer32407102007-09-26 23:01:49 +0000379 return (target_ulong)ret >= (target_ulong)(-4096);
bellard31e31b82003-02-18 22:55:36 +0000380}
381
pbrook53a59602006-03-25 19:31:22 +0000382static target_ulong target_brk;
383static target_ulong target_original_brk;
bellard31e31b82003-02-18 22:55:36 +0000384
pbrook53a59602006-03-25 19:31:22 +0000385void target_set_brk(target_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000386{
blueswir14c1de732007-07-07 20:45:44 +0000387 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
bellard31e31b82003-02-18 22:55:36 +0000388}
389
j_mayer32407102007-09-26 23:01:49 +0000390target_long do_brk(target_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000391{
pbrook53a59602006-03-25 19:31:22 +0000392 target_ulong brk_page;
j_mayer32407102007-09-26 23:01:49 +0000393 target_long mapped_addr;
bellard31e31b82003-02-18 22:55:36 +0000394 int new_alloc_size;
395
396 if (!new_brk)
pbrook53a59602006-03-25 19:31:22 +0000397 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000398 if (new_brk < target_original_brk)
399 return -ENOMEM;
ths3b46e622007-09-17 08:09:54 +0000400
pbrook53a59602006-03-25 19:31:22 +0000401 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000402
403 /* If the new brk is less than this, set it and we're done... */
404 if (new_brk < brk_page) {
405 target_brk = new_brk;
pbrook53a59602006-03-25 19:31:22 +0000406 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000407 }
408
409 /* We need to allocate more memory after the brk... */
bellard54936002003-05-13 00:25:15 +0000410 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
ths5fafdf22007-09-16 21:08:06 +0000411 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000412 PROT_READ|PROT_WRITE,
413 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
bellard31e31b82003-02-18 22:55:36 +0000414 if (is_error(mapped_addr)) {
415 return mapped_addr;
416 } else {
417 target_brk = new_brk;
pbrook53a59602006-03-25 19:31:22 +0000418 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000419 }
420}
421
ths5fafdf22007-09-16 21:08:06 +0000422static inline fd_set *target_to_host_fds(fd_set *fds,
bellard31e31b82003-02-18 22:55:36 +0000423 target_long *target_fds, int n)
424{
bellard7854b052003-03-29 17:22:23 +0000425#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
bellard31e31b82003-02-18 22:55:36 +0000426 return (fd_set *)target_fds;
427#else
428 int i, b;
429 if (target_fds) {
430 FD_ZERO(fds);
431 for(i = 0;i < n; i++) {
432 b = (tswapl(target_fds[i / TARGET_LONG_BITS]) >>
433 (i & (TARGET_LONG_BITS - 1))) & 1;
434 if (b)
435 FD_SET(i, fds);
436 }
437 return fds;
438 } else {
439 return NULL;
440 }
441#endif
442}
443
ths5fafdf22007-09-16 21:08:06 +0000444static inline void host_to_target_fds(target_long *target_fds,
bellard31e31b82003-02-18 22:55:36 +0000445 fd_set *fds, int n)
446{
bellard7854b052003-03-29 17:22:23 +0000447#if !defined(BSWAP_NEEDED) && !defined(WORDS_BIGENDIAN)
bellard31e31b82003-02-18 22:55:36 +0000448 /* nothing to do */
449#else
450 int i, nw, j, k;
451 target_long v;
452
453 if (target_fds) {
bellard8853f862004-02-22 14:57:26 +0000454 nw = (n + TARGET_LONG_BITS - 1) / TARGET_LONG_BITS;
bellard31e31b82003-02-18 22:55:36 +0000455 k = 0;
456 for(i = 0;i < nw; i++) {
457 v = 0;
458 for(j = 0; j < TARGET_LONG_BITS; j++) {
459 v |= ((FD_ISSET(k, fds) != 0) << j);
460 k++;
461 }
462 target_fds[i] = tswapl(v);
463 }
464 }
465#endif
466}
467
bellardc596ed12003-07-13 17:32:31 +0000468#if defined(__alpha__)
469#define HOST_HZ 1024
470#else
471#define HOST_HZ 100
472#endif
473
j_mayer32407102007-09-26 23:01:49 +0000474static inline target_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000475{
476#if HOST_HZ == TARGET_HZ
477 return ticks;
478#else
479 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
480#endif
481}
482
pbrook53a59602006-03-25 19:31:22 +0000483static inline void host_to_target_rusage(target_ulong target_addr,
bellardb4091862003-05-16 15:39:34 +0000484 const struct rusage *rusage)
485{
pbrook53a59602006-03-25 19:31:22 +0000486 struct target_rusage *target_rusage;
487
488 lock_user_struct(target_rusage, target_addr, 0);
bellardb4091862003-05-16 15:39:34 +0000489 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
490 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
491 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
492 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
493 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
494 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
495 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
496 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
497 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
498 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
499 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
500 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
501 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
502 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
503 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
504 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
505 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
506 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000507 unlock_user_struct(target_rusage, target_addr, 1);
bellardb4091862003-05-16 15:39:34 +0000508}
509
pbrook53a59602006-03-25 19:31:22 +0000510static inline void target_to_host_timeval(struct timeval *tv,
511 target_ulong target_addr)
bellard31e31b82003-02-18 22:55:36 +0000512{
pbrook53a59602006-03-25 19:31:22 +0000513 struct target_timeval *target_tv;
514
515 lock_user_struct(target_tv, target_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000516 tv->tv_sec = tswapl(target_tv->tv_sec);
517 tv->tv_usec = tswapl(target_tv->tv_usec);
pbrook53a59602006-03-25 19:31:22 +0000518 unlock_user_struct(target_tv, target_addr, 0);
bellard31e31b82003-02-18 22:55:36 +0000519}
520
pbrook53a59602006-03-25 19:31:22 +0000521static inline void host_to_target_timeval(target_ulong target_addr,
bellard5cd43932003-03-29 16:54:36 +0000522 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +0000523{
pbrook53a59602006-03-25 19:31:22 +0000524 struct target_timeval *target_tv;
525
526 lock_user_struct(target_tv, target_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000527 target_tv->tv_sec = tswapl(tv->tv_sec);
528 target_tv->tv_usec = tswapl(tv->tv_usec);
pbrook53a59602006-03-25 19:31:22 +0000529 unlock_user_struct(target_tv, target_addr, 1);
bellard31e31b82003-02-18 22:55:36 +0000530}
531
532
j_mayer32407102007-09-26 23:01:49 +0000533static target_long do_select(int n,
534 target_ulong rfd_p, target_ulong wfd_p,
535 target_ulong efd_p, target_ulong target_tv)
bellard31e31b82003-02-18 22:55:36 +0000536{
537 fd_set rfds, wfds, efds;
538 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
pbrook53a59602006-03-25 19:31:22 +0000539 target_long *target_rfds, *target_wfds, *target_efds;
bellard31e31b82003-02-18 22:55:36 +0000540 struct timeval tv, *tv_ptr;
j_mayer32407102007-09-26 23:01:49 +0000541 target_long ret;
pbrook53a59602006-03-25 19:31:22 +0000542 int ok;
bellard31e31b82003-02-18 22:55:36 +0000543
pbrook53a59602006-03-25 19:31:22 +0000544 if (rfd_p) {
545 target_rfds = lock_user(rfd_p, sizeof(target_long) * n, 1);
546 rfds_ptr = target_to_host_fds(&rfds, target_rfds, n);
547 } else {
548 target_rfds = NULL;
549 rfds_ptr = NULL;
550 }
551 if (wfd_p) {
552 target_wfds = lock_user(wfd_p, sizeof(target_long) * n, 1);
553 wfds_ptr = target_to_host_fds(&wfds, target_wfds, n);
554 } else {
555 target_wfds = NULL;
556 wfds_ptr = NULL;
557 }
558 if (efd_p) {
559 target_efds = lock_user(efd_p, sizeof(target_long) * n, 1);
560 efds_ptr = target_to_host_fds(&efds, target_efds, n);
561 } else {
562 target_efds = NULL;
563 efds_ptr = NULL;
564 }
ths3b46e622007-09-17 08:09:54 +0000565
bellard31e31b82003-02-18 22:55:36 +0000566 if (target_tv) {
bellard5cd43932003-03-29 16:54:36 +0000567 target_to_host_timeval(&tv, target_tv);
bellard31e31b82003-02-18 22:55:36 +0000568 tv_ptr = &tv;
569 } else {
570 tv_ptr = NULL;
571 }
572 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +0000573 ok = !is_error(ret);
574
575 if (ok) {
bellard31e31b82003-02-18 22:55:36 +0000576 host_to_target_fds(target_rfds, rfds_ptr, n);
577 host_to_target_fds(target_wfds, wfds_ptr, n);
578 host_to_target_fds(target_efds, efds_ptr, n);
579
580 if (target_tv) {
bellard5cd43932003-03-29 16:54:36 +0000581 host_to_target_timeval(target_tv, &tv);
bellard31e31b82003-02-18 22:55:36 +0000582 }
583 }
pbrook53a59602006-03-25 19:31:22 +0000584 if (target_rfds)
585 unlock_user(target_rfds, rfd_p, ok ? sizeof(target_long) * n : 0);
586 if (target_wfds)
587 unlock_user(target_wfds, wfd_p, ok ? sizeof(target_long) * n : 0);
588 if (target_efds)
589 unlock_user(target_efds, efd_p, ok ? sizeof(target_long) * n : 0);
590
bellard31e31b82003-02-18 22:55:36 +0000591 return ret;
592}
593
bellard7854b052003-03-29 17:22:23 +0000594static inline void target_to_host_sockaddr(struct sockaddr *addr,
pbrook53a59602006-03-25 19:31:22 +0000595 target_ulong target_addr,
bellard7854b052003-03-29 17:22:23 +0000596 socklen_t len)
597{
pbrook53a59602006-03-25 19:31:22 +0000598 struct target_sockaddr *target_saddr;
599
600 target_saddr = lock_user(target_addr, len, 1);
601 memcpy(addr, target_saddr, len);
602 addr->sa_family = tswap16(target_saddr->sa_family);
603 unlock_user(target_saddr, target_addr, 0);
bellard7854b052003-03-29 17:22:23 +0000604}
605
pbrook53a59602006-03-25 19:31:22 +0000606static inline void host_to_target_sockaddr(target_ulong target_addr,
bellard7854b052003-03-29 17:22:23 +0000607 struct sockaddr *addr,
608 socklen_t len)
609{
pbrook53a59602006-03-25 19:31:22 +0000610 struct target_sockaddr *target_saddr;
611
612 target_saddr = lock_user(target_addr, len, 0);
613 memcpy(target_saddr, addr, len);
614 target_saddr->sa_family = tswap16(addr->sa_family);
615 unlock_user(target_saddr, target_addr, len);
bellard7854b052003-03-29 17:22:23 +0000616}
617
pbrook53a59602006-03-25 19:31:22 +0000618/* ??? Should this also swap msgh->name? */
bellard7854b052003-03-29 17:22:23 +0000619static inline void target_to_host_cmsg(struct msghdr *msgh,
620 struct target_msghdr *target_msgh)
621{
622 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
623 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
624 socklen_t space = 0;
625
626 while (cmsg && target_cmsg) {
627 void *data = CMSG_DATA(cmsg);
628 void *target_data = TARGET_CMSG_DATA(target_cmsg);
629
ths5fafdf22007-09-16 21:08:06 +0000630 int len = tswapl(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +0000631 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
632
633 space += CMSG_SPACE(len);
634 if (space > msgh->msg_controllen) {
635 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +0000636 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +0000637 break;
638 }
639
640 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
641 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
642 cmsg->cmsg_len = CMSG_LEN(len);
643
bellard3532fa72006-06-24 15:06:03 +0000644 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +0000645 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
646 memcpy(data, target_data, len);
647 } else {
648 int *fd = (int *)data;
649 int *target_fd = (int *)target_data;
650 int i, numfds = len / sizeof(int);
651
652 for (i = 0; i < numfds; i++)
653 fd[i] = tswap32(target_fd[i]);
654 }
655
656 cmsg = CMSG_NXTHDR(msgh, cmsg);
657 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
658 }
659
660 msgh->msg_controllen = space;
661}
662
pbrook53a59602006-03-25 19:31:22 +0000663/* ??? Should this also swap msgh->name? */
bellard7854b052003-03-29 17:22:23 +0000664static inline void host_to_target_cmsg(struct target_msghdr *target_msgh,
665 struct msghdr *msgh)
666{
667 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
668 struct target_cmsghdr *target_cmsg = TARGET_CMSG_FIRSTHDR(target_msgh);
669 socklen_t space = 0;
670
671 while (cmsg && target_cmsg) {
672 void *data = CMSG_DATA(cmsg);
673 void *target_data = TARGET_CMSG_DATA(target_cmsg);
674
675 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
676
677 space += TARGET_CMSG_SPACE(len);
678 if (space > tswapl(target_msgh->msg_controllen)) {
679 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +0000680 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +0000681 break;
682 }
683
684 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
685 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
686 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
687
bellard3532fa72006-06-24 15:06:03 +0000688 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +0000689 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
690 memcpy(target_data, data, len);
691 } else {
692 int *fd = (int *)data;
693 int *target_fd = (int *)target_data;
694 int i, numfds = len / sizeof(int);
695
696 for (i = 0; i < numfds; i++)
697 target_fd[i] = tswap32(fd[i]);
698 }
699
700 cmsg = CMSG_NXTHDR(msgh, cmsg);
701 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
702 }
703
704 msgh->msg_controllen = tswapl(space);
705}
706
j_mayer32407102007-09-26 23:01:49 +0000707static target_long do_setsockopt(int sockfd, int level, int optname,
708 target_ulong optval, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +0000709{
j_mayer32407102007-09-26 23:01:49 +0000710 target_long ret;
711 int val;
ths3b46e622007-09-17 08:09:54 +0000712
bellard8853f862004-02-22 14:57:26 +0000713 switch(level) {
714 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +0000715 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +0000716 if (optlen < sizeof(uint32_t))
717 return -EINVAL;
ths3b46e622007-09-17 08:09:54 +0000718
pbrook53a59602006-03-25 19:31:22 +0000719 val = tget32(optval);
bellard8853f862004-02-22 14:57:26 +0000720 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
721 break;
722 case SOL_IP:
723 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +0000724 case IP_TOS:
725 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +0000726 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +0000727 case IP_ROUTER_ALERT:
728 case IP_RECVOPTS:
729 case IP_RETOPTS:
730 case IP_PKTINFO:
731 case IP_MTU_DISCOVER:
732 case IP_RECVERR:
733 case IP_RECVTOS:
734#ifdef IP_FREEBIND
735 case IP_FREEBIND:
736#endif
737 case IP_MULTICAST_TTL:
738 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +0000739 val = 0;
740 if (optlen >= sizeof(uint32_t)) {
pbrook53a59602006-03-25 19:31:22 +0000741 val = tget32(optval);
bellard8853f862004-02-22 14:57:26 +0000742 } else if (optlen >= 1) {
pbrook53a59602006-03-25 19:31:22 +0000743 val = tget8(optval);
bellard8853f862004-02-22 14:57:26 +0000744 }
745 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
746 break;
747 default:
748 goto unimplemented;
749 }
750 break;
bellard3532fa72006-06-24 15:06:03 +0000751 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +0000752 switch (optname) {
753 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +0000754 case TARGET_SO_DEBUG:
755 optname = SO_DEBUG;
756 break;
757 case TARGET_SO_REUSEADDR:
758 optname = SO_REUSEADDR;
759 break;
760 case TARGET_SO_TYPE:
761 optname = SO_TYPE;
762 break;
763 case TARGET_SO_ERROR:
764 optname = SO_ERROR;
765 break;
766 case TARGET_SO_DONTROUTE:
767 optname = SO_DONTROUTE;
768 break;
769 case TARGET_SO_BROADCAST:
770 optname = SO_BROADCAST;
771 break;
772 case TARGET_SO_SNDBUF:
773 optname = SO_SNDBUF;
774 break;
775 case TARGET_SO_RCVBUF:
776 optname = SO_RCVBUF;
777 break;
778 case TARGET_SO_KEEPALIVE:
779 optname = SO_KEEPALIVE;
780 break;
781 case TARGET_SO_OOBINLINE:
782 optname = SO_OOBINLINE;
783 break;
784 case TARGET_SO_NO_CHECK:
785 optname = SO_NO_CHECK;
786 break;
787 case TARGET_SO_PRIORITY:
788 optname = SO_PRIORITY;
789 break;
bellard5e83e8e2005-03-01 22:32:06 +0000790#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +0000791 case TARGET_SO_BSDCOMPAT:
792 optname = SO_BSDCOMPAT;
793 break;
bellard5e83e8e2005-03-01 22:32:06 +0000794#endif
bellard3532fa72006-06-24 15:06:03 +0000795 case TARGET_SO_PASSCRED:
796 optname = SO_PASSCRED;
797 break;
798 case TARGET_SO_TIMESTAMP:
799 optname = SO_TIMESTAMP;
800 break;
801 case TARGET_SO_RCVLOWAT:
802 optname = SO_RCVLOWAT;
803 break;
804 case TARGET_SO_RCVTIMEO:
805 optname = SO_RCVTIMEO;
806 break;
807 case TARGET_SO_SNDTIMEO:
808 optname = SO_SNDTIMEO;
809 break;
bellard8853f862004-02-22 14:57:26 +0000810 break;
811 default:
812 goto unimplemented;
813 }
bellard3532fa72006-06-24 15:06:03 +0000814 if (optlen < sizeof(uint32_t))
815 return -EINVAL;
816
817 val = tget32(optval);
818 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +0000819 break;
bellard7854b052003-03-29 17:22:23 +0000820 default:
bellard8853f862004-02-22 14:57:26 +0000821 unimplemented:
822 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
823 ret = -ENOSYS;
bellard7854b052003-03-29 17:22:23 +0000824 }
bellard8853f862004-02-22 14:57:26 +0000825 return ret;
bellard7854b052003-03-29 17:22:23 +0000826}
827
j_mayer32407102007-09-26 23:01:49 +0000828static target_long do_getsockopt(int sockfd, int level, int optname,
829 target_ulong optval, target_ulong optlen)
bellard7854b052003-03-29 17:22:23 +0000830{
j_mayer32407102007-09-26 23:01:49 +0000831 target_long ret;
832 int len, lv, val;
bellard8853f862004-02-22 14:57:26 +0000833
834 switch(level) {
bellard3532fa72006-06-24 15:06:03 +0000835 case TARGET_SOL_SOCKET:
836 level = SOL_SOCKET;
bellard8853f862004-02-22 14:57:26 +0000837 switch (optname) {
bellard3532fa72006-06-24 15:06:03 +0000838 case TARGET_SO_LINGER:
839 case TARGET_SO_RCVTIMEO:
840 case TARGET_SO_SNDTIMEO:
841 case TARGET_SO_PEERCRED:
842 case TARGET_SO_PEERNAME:
bellard8853f862004-02-22 14:57:26 +0000843 /* These don't just return a single integer */
844 goto unimplemented;
845 default:
bellard2efbe912005-07-23 15:10:20 +0000846 goto int_case;
847 }
848 break;
849 case SOL_TCP:
850 /* TCP options all take an 'int' value. */
851 int_case:
pbrook53a59602006-03-25 19:31:22 +0000852 len = tget32(optlen);
bellard2efbe912005-07-23 15:10:20 +0000853 if (len < 0)
854 return -EINVAL;
855 lv = sizeof(int);
856 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
857 if (ret < 0)
858 return ret;
859 val = tswap32(val);
860 if (len > lv)
861 len = lv;
pbrook53a59602006-03-25 19:31:22 +0000862 if (len == 4)
863 tput32(optval, val);
864 else
865 tput8(optval, val);
866 tput32(optlen, len);
bellard2efbe912005-07-23 15:10:20 +0000867 break;
868 case SOL_IP:
869 switch(optname) {
870 case IP_TOS:
871 case IP_TTL:
872 case IP_HDRINCL:
873 case IP_ROUTER_ALERT:
874 case IP_RECVOPTS:
875 case IP_RETOPTS:
876 case IP_PKTINFO:
877 case IP_MTU_DISCOVER:
878 case IP_RECVERR:
879 case IP_RECVTOS:
880#ifdef IP_FREEBIND
881 case IP_FREEBIND:
882#endif
883 case IP_MULTICAST_TTL:
884 case IP_MULTICAST_LOOP:
pbrook53a59602006-03-25 19:31:22 +0000885 len = tget32(optlen);
bellard8853f862004-02-22 14:57:26 +0000886 if (len < 0)
887 return -EINVAL;
888 lv = sizeof(int);
889 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
890 if (ret < 0)
891 return ret;
bellard2efbe912005-07-23 15:10:20 +0000892 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +0000893 len = 1;
pbrook53a59602006-03-25 19:31:22 +0000894 tput32(optlen, len);
895 tput8(optval, val);
bellard2efbe912005-07-23 15:10:20 +0000896 } else {
bellard2efbe912005-07-23 15:10:20 +0000897 if (len > sizeof(int))
898 len = sizeof(int);
pbrook53a59602006-03-25 19:31:22 +0000899 tput32(optlen, len);
900 tput32(optval, val);
bellard2efbe912005-07-23 15:10:20 +0000901 }
bellard8853f862004-02-22 14:57:26 +0000902 break;
bellard2efbe912005-07-23 15:10:20 +0000903 default:
904 goto unimplemented;
bellard8853f862004-02-22 14:57:26 +0000905 }
906 break;
907 default:
908 unimplemented:
909 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
910 level, optname);
911 ret = -ENOSYS;
912 break;
913 }
914 return ret;
bellard7854b052003-03-29 17:22:23 +0000915}
916
pbrook53a59602006-03-25 19:31:22 +0000917static void lock_iovec(struct iovec *vec, target_ulong target_addr,
918 int count, int copy)
919{
920 struct target_iovec *target_vec;
921 target_ulong base;
922 int i;
923
924 target_vec = lock_user(target_addr, count * sizeof(struct target_iovec), 1);
925 for(i = 0;i < count; i++) {
926 base = tswapl(target_vec[i].iov_base);
927 vec[i].iov_len = tswapl(target_vec[i].iov_len);
928 vec[i].iov_base = lock_user(base, vec[i].iov_len, copy);
929 }
930 unlock_user (target_vec, target_addr, 0);
931}
932
933static void unlock_iovec(struct iovec *vec, target_ulong target_addr,
934 int count, int copy)
935{
936 struct target_iovec *target_vec;
937 target_ulong base;
938 int i;
939
940 target_vec = lock_user(target_addr, count * sizeof(struct target_iovec), 1);
941 for(i = 0;i < count; i++) {
942 base = tswapl(target_vec[i].iov_base);
943 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
944 }
945 unlock_user (target_vec, target_addr, 0);
946}
947
j_mayer32407102007-09-26 23:01:49 +0000948static target_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +0000949{
950#if defined(TARGET_MIPS)
951 switch(type) {
952 case TARGET_SOCK_DGRAM:
953 type = SOCK_DGRAM;
954 break;
955 case TARGET_SOCK_STREAM:
956 type = SOCK_STREAM;
957 break;
958 case TARGET_SOCK_RAW:
959 type = SOCK_RAW;
960 break;
961 case TARGET_SOCK_RDM:
962 type = SOCK_RDM;
963 break;
964 case TARGET_SOCK_SEQPACKET:
965 type = SOCK_SEQPACKET;
966 break;
967 case TARGET_SOCK_PACKET:
968 type = SOCK_PACKET;
969 break;
970 }
971#endif
972 return get_errno(socket(domain, type, protocol));
973}
974
j_mayer32407102007-09-26 23:01:49 +0000975static target_long do_bind(int sockfd, target_ulong target_addr,
976 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +0000977{
978 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +0000979
bellard3532fa72006-06-24 15:06:03 +0000980 target_to_host_sockaddr(addr, target_addr, addrlen);
981 return get_errno(bind(sockfd, addr, addrlen));
982}
983
j_mayer32407102007-09-26 23:01:49 +0000984static target_long do_connect(int sockfd, target_ulong target_addr,
985 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +0000986{
987 void *addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +0000988
bellard3532fa72006-06-24 15:06:03 +0000989 target_to_host_sockaddr(addr, target_addr, addrlen);
990 return get_errno(connect(sockfd, addr, addrlen));
991}
992
j_mayer32407102007-09-26 23:01:49 +0000993static target_long do_sendrecvmsg(int fd, target_ulong target_msg,
994 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +0000995{
j_mayer32407102007-09-26 23:01:49 +0000996 target_long ret;
bellard3532fa72006-06-24 15:06:03 +0000997 struct target_msghdr *msgp;
998 struct msghdr msg;
999 int count;
1000 struct iovec *vec;
1001 target_ulong target_vec;
1002
1003 lock_user_struct(msgp, target_msg, 1);
1004 if (msgp->msg_name) {
1005 msg.msg_namelen = tswap32(msgp->msg_namelen);
1006 msg.msg_name = alloca(msg.msg_namelen);
1007 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1008 msg.msg_namelen);
1009 } else {
1010 msg.msg_name = NULL;
1011 msg.msg_namelen = 0;
1012 }
1013 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1014 msg.msg_control = alloca(msg.msg_controllen);
1015 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001016
bellard3532fa72006-06-24 15:06:03 +00001017 count = tswapl(msgp->msg_iovlen);
1018 vec = alloca(count * sizeof(struct iovec));
1019 target_vec = tswapl(msgp->msg_iov);
1020 lock_iovec(vec, target_vec, count, send);
1021 msg.msg_iovlen = count;
1022 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001023
bellard3532fa72006-06-24 15:06:03 +00001024 if (send) {
1025 target_to_host_cmsg(&msg, msgp);
1026 ret = get_errno(sendmsg(fd, &msg, flags));
1027 } else {
1028 ret = get_errno(recvmsg(fd, &msg, flags));
1029 if (!is_error(ret))
1030 host_to_target_cmsg(msgp, &msg);
1031 }
1032 unlock_iovec(vec, target_vec, count, !send);
1033 return ret;
1034}
1035
j_mayer32407102007-09-26 23:01:49 +00001036static target_long do_accept(int fd, target_ulong target_addr,
1037 target_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001038{
1039 socklen_t addrlen = tget32(target_addrlen);
ths197ea352006-12-11 19:13:59 +00001040 void *addr = alloca(addrlen);
j_mayer32407102007-09-26 23:01:49 +00001041 target_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001042
1043 ret = get_errno(accept(fd, addr, &addrlen));
1044 if (!is_error(ret)) {
1045 host_to_target_sockaddr(target_addr, addr, addrlen);
1046 tput32(target_addrlen, addrlen);
1047 }
1048 return ret;
1049}
1050
j_mayer32407102007-09-26 23:01:49 +00001051static target_long do_getpeername(int fd, target_ulong target_addr,
1052 target_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001053{
1054 socklen_t addrlen = tget32(target_addrlen);
ths01c227f2007-03-19 13:05:13 +00001055 void *addr = alloca(addrlen);
j_mayer32407102007-09-26 23:01:49 +00001056 target_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001057
1058 ret = get_errno(getpeername(fd, addr, &addrlen));
1059 if (!is_error(ret)) {
1060 host_to_target_sockaddr(target_addr, addr, addrlen);
1061 tput32(target_addrlen, addrlen);
1062 }
1063 return ret;
1064}
1065
j_mayer32407102007-09-26 23:01:49 +00001066static target_long do_getsockname(int fd, target_ulong target_addr,
1067 target_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001068{
1069 socklen_t addrlen = tget32(target_addrlen);
ths01c227f2007-03-19 13:05:13 +00001070 void *addr = alloca(addrlen);
j_mayer32407102007-09-26 23:01:49 +00001071 target_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001072
1073 ret = get_errno(getsockname(fd, addr, &addrlen));
1074 if (!is_error(ret)) {
1075 host_to_target_sockaddr(target_addr, addr, addrlen);
1076 tput32(target_addrlen, addrlen);
1077 }
1078 return ret;
1079}
1080
j_mayer32407102007-09-26 23:01:49 +00001081static target_long do_socketpair(int domain, int type, int protocol,
1082 target_ulong target_tab)
pbrook1be9e1d2006-11-19 15:26:04 +00001083{
1084 int tab[2];
j_mayer32407102007-09-26 23:01:49 +00001085 target_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001086
1087 ret = get_errno(socketpair(domain, type, protocol, tab));
1088 if (!is_error(ret)) {
1089 tput32(target_tab, tab[0]);
1090 tput32(target_tab + 4, tab[1]);
1091 }
1092 return ret;
1093}
1094
j_mayer32407102007-09-26 23:01:49 +00001095static target_long do_sendto(int fd, target_ulong msg, size_t len, int flags,
1096 target_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001097{
1098 void *addr;
1099 void *host_msg;
j_mayer32407102007-09-26 23:01:49 +00001100 target_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001101
1102 host_msg = lock_user(msg, len, 1);
1103 if (target_addr) {
1104 addr = alloca(addrlen);
1105 target_to_host_sockaddr(addr, target_addr, addrlen);
1106 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1107 } else {
1108 ret = get_errno(send(fd, host_msg, len, flags));
1109 }
1110 unlock_user(host_msg, msg, 0);
1111 return ret;
1112}
1113
j_mayer32407102007-09-26 23:01:49 +00001114static target_long do_recvfrom(int fd, target_ulong msg, size_t len, int flags,
1115 target_ulong target_addr,
1116 target_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001117{
1118 socklen_t addrlen;
1119 void *addr;
1120 void *host_msg;
j_mayer32407102007-09-26 23:01:49 +00001121 target_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001122
1123 host_msg = lock_user(msg, len, 0);
1124 if (target_addr) {
1125 addrlen = tget32(target_addrlen);
1126 addr = alloca(addrlen);
1127 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1128 } else {
1129 addr = NULL; /* To keep compiler quiet. */
1130 ret = get_errno(recv(fd, host_msg, len, flags));
1131 }
1132 if (!is_error(ret)) {
1133 if (target_addr) {
1134 host_to_target_sockaddr(target_addr, addr, addrlen);
1135 tput32(target_addrlen, addrlen);
1136 }
1137 unlock_user(host_msg, msg, len);
1138 } else {
1139 unlock_user(host_msg, msg, 0);
1140 }
1141 return ret;
1142}
1143
j_mayer32407102007-09-26 23:01:49 +00001144#ifdef TARGET_NR_socketcall
1145static target_long do_socketcall(int num, target_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00001146{
j_mayer32407102007-09-26 23:01:49 +00001147 target_long ret;
pbrook53a59602006-03-25 19:31:22 +00001148 const int n = sizeof(target_ulong);
bellard31e31b82003-02-18 22:55:36 +00001149
1150 switch(num) {
1151 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00001152 {
pbrook53a59602006-03-25 19:31:22 +00001153 int domain = tgetl(vptr);
1154 int type = tgetl(vptr + n);
1155 int protocol = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001156 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00001157 }
bellard31e31b82003-02-18 22:55:36 +00001158 break;
1159 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00001160 {
pbrook53a59602006-03-25 19:31:22 +00001161 int sockfd = tgetl(vptr);
1162 target_ulong target_addr = tgetl(vptr + n);
1163 socklen_t addrlen = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001164 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001165 }
bellard31e31b82003-02-18 22:55:36 +00001166 break;
1167 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00001168 {
pbrook53a59602006-03-25 19:31:22 +00001169 int sockfd = tgetl(vptr);
1170 target_ulong target_addr = tgetl(vptr + n);
1171 socklen_t addrlen = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001172 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001173 }
bellard31e31b82003-02-18 22:55:36 +00001174 break;
1175 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00001176 {
pbrook53a59602006-03-25 19:31:22 +00001177 int sockfd = tgetl(vptr);
1178 int backlog = tgetl(vptr + n);
bellard7854b052003-03-29 17:22:23 +00001179 ret = get_errno(listen(sockfd, backlog));
1180 }
bellard31e31b82003-02-18 22:55:36 +00001181 break;
1182 case SOCKOP_accept:
1183 {
pbrook53a59602006-03-25 19:31:22 +00001184 int sockfd = tgetl(vptr);
1185 target_ulong target_addr = tgetl(vptr + n);
1186 target_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001187 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001188 }
1189 break;
1190 case SOCKOP_getsockname:
1191 {
pbrook53a59602006-03-25 19:31:22 +00001192 int sockfd = tgetl(vptr);
1193 target_ulong target_addr = tgetl(vptr + n);
1194 target_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001195 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001196 }
1197 break;
1198 case SOCKOP_getpeername:
1199 {
pbrook53a59602006-03-25 19:31:22 +00001200 int sockfd = tgetl(vptr);
1201 target_ulong target_addr = tgetl(vptr + n);
1202 target_ulong target_addrlen = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001203 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001204 }
1205 break;
1206 case SOCKOP_socketpair:
1207 {
pbrook53a59602006-03-25 19:31:22 +00001208 int domain = tgetl(vptr);
1209 int type = tgetl(vptr + n);
1210 int protocol = tgetl(vptr + 2 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001211 target_ulong tab = tgetl(vptr + 3 * n);
1212 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00001213 }
1214 break;
1215 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00001216 {
pbrook53a59602006-03-25 19:31:22 +00001217 int sockfd = tgetl(vptr);
1218 target_ulong msg = tgetl(vptr + n);
1219 size_t len = tgetl(vptr + 2 * n);
1220 int flags = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001221 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001222 }
bellard31e31b82003-02-18 22:55:36 +00001223 break;
1224 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00001225 {
pbrook53a59602006-03-25 19:31:22 +00001226 int sockfd = tgetl(vptr);
1227 target_ulong msg = tgetl(vptr + n);
1228 size_t len = tgetl(vptr + 2 * n);
1229 int flags = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001230 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001231 }
bellard31e31b82003-02-18 22:55:36 +00001232 break;
1233 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00001234 {
pbrook53a59602006-03-25 19:31:22 +00001235 int sockfd = tgetl(vptr);
1236 target_ulong msg = tgetl(vptr + n);
1237 size_t len = tgetl(vptr + 2 * n);
1238 int flags = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001239 target_ulong addr = tgetl(vptr + 4 * n);
pbrook53a59602006-03-25 19:31:22 +00001240 socklen_t addrlen = tgetl(vptr + 5 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001241 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001242 }
bellard31e31b82003-02-18 22:55:36 +00001243 break;
1244 case SOCKOP_recvfrom:
1245 {
pbrook53a59602006-03-25 19:31:22 +00001246 int sockfd = tgetl(vptr);
1247 target_ulong msg = tgetl(vptr + n);
1248 size_t len = tgetl(vptr + 2 * n);
1249 int flags = tgetl(vptr + 3 * n);
pbrook1be9e1d2006-11-19 15:26:04 +00001250 target_ulong addr = tgetl(vptr + 4 * n);
1251 target_ulong addrlen = tgetl(vptr + 5 * n);
1252 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00001253 }
1254 break;
1255 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00001256 {
pbrook53a59602006-03-25 19:31:22 +00001257 int sockfd = tgetl(vptr);
1258 int how = tgetl(vptr + n);
bellard7854b052003-03-29 17:22:23 +00001259
1260 ret = get_errno(shutdown(sockfd, how));
1261 }
bellard31e31b82003-02-18 22:55:36 +00001262 break;
1263 case SOCKOP_sendmsg:
1264 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00001265 {
1266 int fd;
pbrook53a59602006-03-25 19:31:22 +00001267 target_ulong target_msg;
bellard3532fa72006-06-24 15:06:03 +00001268 int flags;
bellard1a9353d2003-03-16 20:28:50 +00001269
pbrook53a59602006-03-25 19:31:22 +00001270 fd = tgetl(vptr);
bellard3532fa72006-06-24 15:06:03 +00001271 target_msg = tgetl(vptr + n);
pbrook53a59602006-03-25 19:31:22 +00001272 flags = tgetl(vptr + 2 * n);
bellard3532fa72006-06-24 15:06:03 +00001273
ths5fafdf22007-09-16 21:08:06 +00001274 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00001275 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00001276 }
1277 break;
bellard31e31b82003-02-18 22:55:36 +00001278 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00001279 {
pbrook53a59602006-03-25 19:31:22 +00001280 int sockfd = tgetl(vptr);
1281 int level = tgetl(vptr + n);
1282 int optname = tgetl(vptr + 2 * n);
1283 target_ulong optval = tgetl(vptr + 3 * n);
1284 socklen_t optlen = tgetl(vptr + 4 * n);
bellard7854b052003-03-29 17:22:23 +00001285
1286 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1287 }
1288 break;
bellard31e31b82003-02-18 22:55:36 +00001289 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00001290 {
pbrook53a59602006-03-25 19:31:22 +00001291 int sockfd = tgetl(vptr);
1292 int level = tgetl(vptr + n);
1293 int optname = tgetl(vptr + 2 * n);
1294 target_ulong optval = tgetl(vptr + 3 * n);
1295 target_ulong poptlen = tgetl(vptr + 4 * n);
bellard7854b052003-03-29 17:22:23 +00001296
bellard8853f862004-02-22 14:57:26 +00001297 ret = do_getsockopt(sockfd, level, optname, optval, poptlen);
bellard7854b052003-03-29 17:22:23 +00001298 }
1299 break;
bellard31e31b82003-02-18 22:55:36 +00001300 default:
1301 gemu_log("Unsupported socketcall: %d\n", num);
1302 ret = -ENOSYS;
1303 break;
1304 }
1305 return ret;
1306}
j_mayer32407102007-09-26 23:01:49 +00001307#endif
bellard31e31b82003-02-18 22:55:36 +00001308
j_mayer32407102007-09-26 23:01:49 +00001309#ifdef TARGET_NR_ipc
bellard8853f862004-02-22 14:57:26 +00001310#define N_SHM_REGIONS 32
1311
1312static struct shm_region {
1313 uint32_t start;
1314 uint32_t size;
1315} shm_regions[N_SHM_REGIONS];
1316
ths3eb6b042007-06-03 14:26:27 +00001317struct target_ipc_perm
1318{
1319 target_long __key;
1320 target_ulong uid;
1321 target_ulong gid;
1322 target_ulong cuid;
1323 target_ulong cgid;
1324 unsigned short int mode;
1325 unsigned short int __pad1;
1326 unsigned short int __seq;
1327 unsigned short int __pad2;
1328 target_ulong __unused1;
1329 target_ulong __unused2;
1330};
1331
1332struct target_semid_ds
1333{
1334 struct target_ipc_perm sem_perm;
1335 target_ulong sem_otime;
1336 target_ulong __unused1;
1337 target_ulong sem_ctime;
1338 target_ulong __unused2;
1339 target_ulong sem_nsems;
1340 target_ulong __unused3;
1341 target_ulong __unused4;
1342};
1343
1344static inline void target_to_host_ipc_perm(struct ipc_perm *host_ip,
1345 target_ulong target_addr)
1346{
1347 struct target_ipc_perm *target_ip;
1348 struct target_semid_ds *target_sd;
1349
1350 lock_user_struct(target_sd, target_addr, 1);
1351 target_ip=&(target_sd->sem_perm);
1352 host_ip->__key = tswapl(target_ip->__key);
1353 host_ip->uid = tswapl(target_ip->uid);
1354 host_ip->gid = tswapl(target_ip->gid);
1355 host_ip->cuid = tswapl(target_ip->cuid);
1356 host_ip->cgid = tswapl(target_ip->cgid);
1357 host_ip->mode = tswapl(target_ip->mode);
1358 unlock_user_struct(target_sd, target_addr, 0);
1359}
1360
1361static inline void host_to_target_ipc_perm(target_ulong target_addr,
1362 struct ipc_perm *host_ip)
1363{
1364 struct target_ipc_perm *target_ip;
1365 struct target_semid_ds *target_sd;
1366
1367 lock_user_struct(target_sd, target_addr, 0);
1368 target_ip = &(target_sd->sem_perm);
1369 target_ip->__key = tswapl(host_ip->__key);
1370 target_ip->uid = tswapl(host_ip->uid);
1371 target_ip->gid = tswapl(host_ip->gid);
1372 target_ip->cuid = tswapl(host_ip->cuid);
1373 target_ip->cgid = tswapl(host_ip->cgid);
1374 target_ip->mode = tswapl(host_ip->mode);
1375 unlock_user_struct(target_sd, target_addr, 1);
1376}
1377
1378static inline void target_to_host_semid_ds(struct semid_ds *host_sd,
1379 target_ulong target_addr)
1380{
1381 struct target_semid_ds *target_sd;
1382
1383 lock_user_struct(target_sd, target_addr, 1);
1384 target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr);
1385 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
1386 host_sd->sem_otime = tswapl(target_sd->sem_otime);
1387 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
1388 unlock_user_struct(target_sd, target_addr, 0);
1389}
1390
1391static inline void host_to_target_semid_ds(target_ulong target_addr,
1392 struct semid_ds *host_sd)
1393{
1394 struct target_semid_ds *target_sd;
1395
1396 lock_user_struct(target_sd, target_addr, 0);
1397 host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm));
1398 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
1399 target_sd->sem_otime = tswapl(host_sd->sem_otime);
1400 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
1401 unlock_user_struct(target_sd, target_addr, 1);
1402}
1403
thsfa294812007-02-02 22:05:00 +00001404union semun {
1405 int val;
ths3eb6b042007-06-03 14:26:27 +00001406 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00001407 unsigned short *array;
1408};
1409
ths3eb6b042007-06-03 14:26:27 +00001410union target_semun {
1411 int val;
1412 target_long buf;
1413 unsigned short int *array;
1414};
1415
j_mayer32407102007-09-26 23:01:49 +00001416static inline void target_to_host_semun(int cmd,
ths3eb6b042007-06-03 14:26:27 +00001417 union semun *host_su,
1418 target_ulong target_addr,
1419 struct semid_ds *ds)
1420{
1421 union target_semun *target_su;
1422
1423 switch( cmd ) {
1424 case IPC_STAT:
1425 case IPC_SET:
1426 lock_user_struct(target_su, target_addr, 1);
1427 target_to_host_semid_ds(ds,target_su->buf);
1428 host_su->buf = ds;
1429 unlock_user_struct(target_su, target_addr, 0);
1430 break;
1431 case GETVAL:
1432 case SETVAL:
1433 lock_user_struct(target_su, target_addr, 1);
1434 host_su->val = tswapl(target_su->val);
1435 unlock_user_struct(target_su, target_addr, 0);
1436 break;
1437 case GETALL:
1438 case SETALL:
1439 lock_user_struct(target_su, target_addr, 1);
1440 *host_su->array = tswap16(*target_su->array);
1441 unlock_user_struct(target_su, target_addr, 0);
1442 break;
1443 default:
1444 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1445 }
1446}
1447
j_mayer32407102007-09-26 23:01:49 +00001448static inline void host_to_target_semun(int cmd,
ths3eb6b042007-06-03 14:26:27 +00001449 target_ulong target_addr,
1450 union semun *host_su,
1451 struct semid_ds *ds)
1452{
1453 union target_semun *target_su;
1454
1455 switch( cmd ) {
1456 case IPC_STAT:
1457 case IPC_SET:
1458 lock_user_struct(target_su, target_addr, 0);
1459 host_to_target_semid_ds(target_su->buf,ds);
1460 unlock_user_struct(target_su, target_addr, 1);
1461 break;
1462 case GETVAL:
1463 case SETVAL:
1464 lock_user_struct(target_su, target_addr, 0);
1465 target_su->val = tswapl(host_su->val);
1466 unlock_user_struct(target_su, target_addr, 1);
1467 break;
1468 case GETALL:
1469 case SETALL:
1470 lock_user_struct(target_su, target_addr, 0);
1471 *target_su->array = tswap16(*host_su->array);
1472 unlock_user_struct(target_su, target_addr, 1);
1473 break;
1474 default:
1475 gemu_log("semun operation not fully supported: %d\n", (int)cmd);
1476 }
1477}
1478
j_mayer32407102007-09-26 23:01:49 +00001479static inline target_long do_semctl(int first, int second, int third,
1480 target_long ptr)
ths3eb6b042007-06-03 14:26:27 +00001481{
1482 union semun arg;
1483 struct semid_ds dsarg;
1484 int cmd = third&0xff;
j_mayer32407102007-09-26 23:01:49 +00001485 target_long ret = 0;
ths3eb6b042007-06-03 14:26:27 +00001486
1487 switch( cmd ) {
1488 case GETVAL:
1489 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1490 ret = get_errno(semctl(first, second, cmd, arg));
1491 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1492 break;
1493 case SETVAL:
1494 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1495 ret = get_errno(semctl(first, second, cmd, arg));
1496 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1497 break;
1498 case GETALL:
1499 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1500 ret = get_errno(semctl(first, second, cmd, arg));
1501 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1502 break;
1503 case SETALL:
1504 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1505 ret = get_errno(semctl(first, second, cmd, arg));
1506 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1507 break;
1508 case IPC_STAT:
1509 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1510 ret = get_errno(semctl(first, second, cmd, arg));
1511 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1512 break;
1513 case IPC_SET:
1514 target_to_host_semun(cmd,&arg,ptr,&dsarg);
1515 ret = get_errno(semctl(first, second, cmd, arg));
1516 host_to_target_semun(cmd,ptr,&arg,&dsarg);
1517 break;
1518 default:
1519 ret = get_errno(semctl(first, second, cmd, arg));
1520 }
1521
1522 return ret;
1523}
1524
ths1bc012f2007-06-03 14:27:49 +00001525struct target_msqid_ds
1526{
1527 struct target_ipc_perm msg_perm;
1528 target_ulong msg_stime;
1529 target_ulong __unused1;
1530 target_ulong msg_rtime;
1531 target_ulong __unused2;
1532 target_ulong msg_ctime;
1533 target_ulong __unused3;
1534 target_ulong __msg_cbytes;
1535 target_ulong msg_qnum;
1536 target_ulong msg_qbytes;
1537 target_ulong msg_lspid;
1538 target_ulong msg_lrpid;
1539 target_ulong __unused4;
1540 target_ulong __unused5;
1541};
1542
1543static inline void target_to_host_msqid_ds(struct msqid_ds *host_md,
j_mayer32407102007-09-26 23:01:49 +00001544 target_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00001545{
1546 struct target_msqid_ds *target_md;
1547
1548 lock_user_struct(target_md, target_addr, 1);
1549 target_to_host_ipc_perm(&(host_md->msg_perm),target_addr);
1550 host_md->msg_stime = tswapl(target_md->msg_stime);
1551 host_md->msg_rtime = tswapl(target_md->msg_rtime);
1552 host_md->msg_ctime = tswapl(target_md->msg_ctime);
1553 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
1554 host_md->msg_qnum = tswapl(target_md->msg_qnum);
1555 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
1556 host_md->msg_lspid = tswapl(target_md->msg_lspid);
1557 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
1558 unlock_user_struct(target_md, target_addr, 0);
1559}
1560
1561static inline void host_to_target_msqid_ds(target_ulong target_addr,
1562 struct msqid_ds *host_md)
1563{
1564 struct target_msqid_ds *target_md;
1565
1566 lock_user_struct(target_md, target_addr, 0);
1567 host_to_target_ipc_perm(target_addr,&(host_md->msg_perm));
1568 target_md->msg_stime = tswapl(host_md->msg_stime);
1569 target_md->msg_rtime = tswapl(host_md->msg_rtime);
1570 target_md->msg_ctime = tswapl(host_md->msg_ctime);
1571 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
1572 target_md->msg_qnum = tswapl(host_md->msg_qnum);
1573 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
1574 target_md->msg_lspid = tswapl(host_md->msg_lspid);
1575 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
1576 unlock_user_struct(target_md, target_addr, 1);
1577}
1578
j_mayer32407102007-09-26 23:01:49 +00001579static inline target_long do_msgctl(int first, int second, target_long ptr)
ths1bc012f2007-06-03 14:27:49 +00001580{
1581 struct msqid_ds dsarg;
1582 int cmd = second&0xff;
j_mayer32407102007-09-26 23:01:49 +00001583 target_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001584 switch( cmd ) {
1585 case IPC_STAT:
1586 case IPC_SET:
1587 target_to_host_msqid_ds(&dsarg,ptr);
1588 ret = get_errno(msgctl(first, cmd, &dsarg));
1589 host_to_target_msqid_ds(ptr,&dsarg);
1590 default:
1591 ret = get_errno(msgctl(first, cmd, &dsarg));
1592 }
1593 return ret;
1594}
1595
1596struct target_msgbuf {
1597 target_ulong mtype;
1598 char mtext[1];
1599};
1600
j_mayer32407102007-09-26 23:01:49 +00001601static inline target_long do_msgsnd(int msqid, target_long msgp,
1602 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001603{
1604 struct target_msgbuf *target_mb;
1605 struct msgbuf *host_mb;
j_mayer32407102007-09-26 23:01:49 +00001606 target_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001607
1608 lock_user_struct(target_mb,msgp,0);
1609 host_mb = malloc(msgsz+sizeof(long));
1610 host_mb->mtype = tswapl(target_mb->mtype);
1611 memcpy(host_mb->mtext,target_mb->mtext,msgsz);
1612 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
1613 free(host_mb);
1614 unlock_user_struct(target_mb, msgp, 0);
1615
1616 return ret;
1617}
1618
j_mayer32407102007-09-26 23:01:49 +00001619static inline target_long do_msgrcv(int msqid, target_long msgp,
1620 unsigned int msgsz, int msgtype,
1621 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00001622{
1623 struct target_msgbuf *target_mb;
1624 struct msgbuf *host_mb;
j_mayer32407102007-09-26 23:01:49 +00001625 target_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00001626
1627 lock_user_struct(target_mb, msgp, 0);
1628 host_mb = malloc(msgsz+sizeof(long));
1629 ret = get_errno(msgrcv(msqid, host_mb, msgsz, 1, msgflg));
1630 if (ret > 0)
1631 memcpy(target_mb->mtext, host_mb->mtext, ret);
1632 target_mb->mtype = tswapl(host_mb->mtype);
1633 free(host_mb);
1634 unlock_user_struct(target_mb, msgp, 0);
1635
1636 return ret;
1637}
1638
pbrook53a59602006-03-25 19:31:22 +00001639/* ??? This only works with linear mappings. */
j_mayer32407102007-09-26 23:01:49 +00001640static target_long do_ipc(unsigned int call, int first,
1641 int second, int third,
1642 target_long ptr, target_long fifth)
bellard8853f862004-02-22 14:57:26 +00001643{
1644 int version;
j_mayer32407102007-09-26 23:01:49 +00001645 target_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00001646 unsigned long raddr;
1647 struct shmid_ds shm_info;
1648 int i;
1649
1650 version = call >> 16;
1651 call &= 0xffff;
1652
1653 switch (call) {
thsfa294812007-02-02 22:05:00 +00001654 case IPCOP_semop:
1655 ret = get_errno(semop(first,(struct sembuf *) ptr, second));
1656 break;
1657
1658 case IPCOP_semget:
1659 ret = get_errno(semget(first, second, third));
1660 break;
1661
1662 case IPCOP_semctl:
ths3eb6b042007-06-03 14:26:27 +00001663 ret = do_semctl(first, second, third, ptr);
thsfa294812007-02-02 22:05:00 +00001664 break;
1665
1666 case IPCOP_semtimedop:
j_mayer32407102007-09-26 23:01:49 +00001667 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
thsfa294812007-02-02 22:05:00 +00001668 ret = -ENOSYS;
1669 break;
thsd96372e2007-02-02 22:05:44 +00001670
1671 case IPCOP_msgget:
1672 ret = get_errno(msgget(first, second));
1673 break;
1674
1675 case IPCOP_msgsnd:
ths1bc012f2007-06-03 14:27:49 +00001676 ret = do_msgsnd(first, ptr, second, third);
thsd96372e2007-02-02 22:05:44 +00001677 break;
1678
1679 case IPCOP_msgctl:
ths1bc012f2007-06-03 14:27:49 +00001680 ret = do_msgctl(first, second, ptr);
thsd96372e2007-02-02 22:05:44 +00001681 break;
1682
1683 case IPCOP_msgrcv:
ths1bc012f2007-06-03 14:27:49 +00001684 {
1685 struct ipc_kludge
1686 {
1687 void *__unbounded msgp;
1688 long int msgtyp;
1689 };
thsd96372e2007-02-02 22:05:44 +00001690
ths1bc012f2007-06-03 14:27:49 +00001691 struct ipc_kludge *foo = (struct ipc_kludge *) ptr;
1692 struct msgbuf *msgp = (struct msgbuf *) foo->msgp;
thsd96372e2007-02-02 22:05:44 +00001693
ths1bc012f2007-06-03 14:27:49 +00001694 ret = do_msgrcv(first, (long)msgp, second, 0, third);
thsd96372e2007-02-02 22:05:44 +00001695
ths1bc012f2007-06-03 14:27:49 +00001696 }
thsd96372e2007-02-02 22:05:44 +00001697 break;
1698
bellard8853f862004-02-22 14:57:26 +00001699 case IPCOP_shmat:
1700 /* SHM_* flags are the same on all linux platforms */
1701 ret = get_errno((long) shmat(first, (void *) ptr, second));
1702 if (is_error(ret))
1703 break;
1704 raddr = ret;
1705 /* find out the length of the shared memory segment */
ths3b46e622007-09-17 08:09:54 +00001706
bellard8853f862004-02-22 14:57:26 +00001707 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
1708 if (is_error(ret)) {
1709 /* can't get length, bail out */
1710 shmdt((void *) raddr);
1711 break;
1712 }
1713 page_set_flags(raddr, raddr + shm_info.shm_segsz,
1714 PAGE_VALID | PAGE_READ |
1715 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
1716 for (i = 0; i < N_SHM_REGIONS; ++i) {
1717 if (shm_regions[i].start == 0) {
1718 shm_regions[i].start = raddr;
1719 shm_regions[i].size = shm_info.shm_segsz;
1720 break;
1721 }
1722 }
j_mayer32407102007-09-26 23:01:49 +00001723 if (put_user(raddr, (target_ulong *)third))
bellard8853f862004-02-22 14:57:26 +00001724 return -EFAULT;
1725 ret = 0;
1726 break;
1727 case IPCOP_shmdt:
1728 for (i = 0; i < N_SHM_REGIONS; ++i) {
1729 if (shm_regions[i].start == ptr) {
1730 shm_regions[i].start = 0;
1731 page_set_flags(ptr, shm_regions[i].size, 0);
1732 break;
1733 }
1734 }
1735 ret = get_errno(shmdt((void *) ptr));
1736 break;
1737
1738 case IPCOP_shmget:
1739 /* IPC_* flag values are the same on all linux platforms */
1740 ret = get_errno(shmget(first, second, third));
1741 break;
1742
1743 /* IPC_* and SHM_* command values are the same on all linux platforms */
1744 case IPCOP_shmctl:
1745 switch(second) {
1746 case IPC_RMID:
1747 case SHM_LOCK:
1748 case SHM_UNLOCK:
1749 ret = get_errno(shmctl(first, second, NULL));
1750 break;
1751 default:
1752 goto unimplemented;
1753 }
1754 break;
1755 default:
1756 unimplemented:
j_mayer32407102007-09-26 23:01:49 +00001757 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
bellard8853f862004-02-22 14:57:26 +00001758 ret = -ENOSYS;
1759 break;
1760 }
1761 return ret;
1762}
j_mayer32407102007-09-26 23:01:49 +00001763#endif
bellard8853f862004-02-22 14:57:26 +00001764
bellard31e31b82003-02-18 22:55:36 +00001765/* kernel structure types definitions */
1766#define IFNAMSIZ 16
1767
1768#define STRUCT(name, list...) STRUCT_ ## name,
1769#define STRUCT_SPECIAL(name) STRUCT_ ## name,
1770enum {
1771#include "syscall_types.h"
1772};
1773#undef STRUCT
1774#undef STRUCT_SPECIAL
1775
1776#define STRUCT(name, list...) const argtype struct_ ## name ## _def[] = { list, TYPE_NULL };
1777#define STRUCT_SPECIAL(name)
1778#include "syscall_types.h"
1779#undef STRUCT
1780#undef STRUCT_SPECIAL
1781
1782typedef struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00001783 unsigned int target_cmd;
1784 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00001785 const char *name;
1786 int access;
bellard1a9353d2003-03-16 20:28:50 +00001787 const argtype arg_type[5];
bellard31e31b82003-02-18 22:55:36 +00001788} IOCTLEntry;
1789
1790#define IOC_R 0x0001
1791#define IOC_W 0x0002
1792#define IOC_RW (IOC_R | IOC_W)
1793
1794#define MAX_STRUCT_SIZE 4096
1795
bellard2ab83ea2003-06-15 19:56:46 +00001796IOCTLEntry ioctl_entries[] = {
bellard31e31b82003-02-18 22:55:36 +00001797#define IOCTL(cmd, access, types...) \
1798 { TARGET_ ## cmd, cmd, #cmd, access, { types } },
1799#include "ioctls.h"
1800 { 0, 0, },
1801};
1802
pbrook53a59602006-03-25 19:31:22 +00001803/* ??? Implement proper locking for ioctls. */
j_mayer32407102007-09-26 23:01:49 +00001804static target_long do_ioctl(int fd, target_long cmd, target_long arg)
bellard31e31b82003-02-18 22:55:36 +00001805{
1806 const IOCTLEntry *ie;
1807 const argtype *arg_type;
j_mayer32407102007-09-26 23:01:49 +00001808 target_long ret;
bellard31e31b82003-02-18 22:55:36 +00001809 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00001810 int target_size;
1811 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00001812
1813 ie = ioctl_entries;
1814 for(;;) {
1815 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00001816 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
bellard31e31b82003-02-18 22:55:36 +00001817 return -ENOSYS;
1818 }
1819 if (ie->target_cmd == cmd)
1820 break;
1821 ie++;
1822 }
1823 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00001824#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00001825 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00001826#endif
bellard31e31b82003-02-18 22:55:36 +00001827 switch(arg_type[0]) {
1828 case TYPE_NULL:
1829 /* no argument */
1830 ret = get_errno(ioctl(fd, ie->host_cmd));
1831 break;
1832 case TYPE_PTRVOID:
1833 case TYPE_INT:
1834 /* int argment */
1835 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
1836 break;
1837 case TYPE_PTR:
1838 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00001839 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00001840 switch(ie->access) {
1841 case IOC_R:
1842 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1843 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00001844 argptr = lock_user(arg, target_size, 0);
1845 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1846 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00001847 }
1848 break;
1849 case IOC_W:
pbrook53a59602006-03-25 19:31:22 +00001850 argptr = lock_user(arg, target_size, 1);
1851 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1852 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00001853 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1854 break;
1855 default:
1856 case IOC_RW:
pbrook53a59602006-03-25 19:31:22 +00001857 argptr = lock_user(arg, target_size, 1);
1858 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
1859 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00001860 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
1861 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00001862 argptr = lock_user(arg, target_size, 0);
1863 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
1864 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00001865 }
1866 break;
1867 }
1868 break;
1869 default:
j_mayer32407102007-09-26 23:01:49 +00001870 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
1871 (long)cmd, arg_type[0]);
bellard31e31b82003-02-18 22:55:36 +00001872 ret = -ENOSYS;
1873 break;
1874 }
1875 return ret;
1876}
1877
1878bitmask_transtbl iflag_tbl[] = {
1879 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
1880 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
1881 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
1882 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
1883 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
1884 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
1885 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
1886 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
1887 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
1888 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
1889 { TARGET_IXON, TARGET_IXON, IXON, IXON },
1890 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
1891 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
1892 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
1893 { 0, 0, 0, 0 }
1894};
1895
1896bitmask_transtbl oflag_tbl[] = {
1897 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
1898 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
1899 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
1900 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
1901 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
1902 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
1903 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
1904 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
1905 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
1906 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
1907 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
1908 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
1909 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
1910 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
1911 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
1912 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
1913 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
1914 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
1915 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
1916 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
1917 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
1918 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
1919 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
1920 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
1921 { 0, 0, 0, 0 }
1922};
1923
1924bitmask_transtbl cflag_tbl[] = {
1925 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
1926 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
1927 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
1928 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
1929 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
1930 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
1931 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
1932 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
1933 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
1934 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
1935 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
1936 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
1937 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
1938 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
1939 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
1940 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
1941 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
1942 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
1943 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
1944 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
1945 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
1946 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
1947 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
1948 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
1949 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
1950 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
1951 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
1952 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
1953 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
1954 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
1955 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
1956 { 0, 0, 0, 0 }
1957};
1958
1959bitmask_transtbl lflag_tbl[] = {
1960 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
1961 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
1962 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
1963 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
1964 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
1965 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
1966 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
1967 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
1968 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
1969 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
1970 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
1971 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
1972 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
1973 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
1974 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
1975 { 0, 0, 0, 0 }
1976};
1977
1978static void target_to_host_termios (void *dst, const void *src)
1979{
1980 struct host_termios *host = dst;
1981 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00001982
ths5fafdf22007-09-16 21:08:06 +00001983 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00001984 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00001985 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00001986 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00001987 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00001988 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00001989 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00001990 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
1991 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00001992
ths5fafdf22007-09-16 21:08:06 +00001993 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
1994 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00001995 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00001996 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00001997 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00001998 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00001999 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00002000 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00002001 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00002002 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2003 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00002004 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2005 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2006 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2007 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2008 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00002009 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00002010}
ths3b46e622007-09-17 08:09:54 +00002011
bellard31e31b82003-02-18 22:55:36 +00002012static void host_to_target_termios (void *dst, const void *src)
2013{
2014 struct target_termios *target = dst;
2015 const struct host_termios *host = src;
2016
ths5fafdf22007-09-16 21:08:06 +00002017 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002018 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002019 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002020 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002021 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002022 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002023 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002024 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2025 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00002026
bellard31e31b82003-02-18 22:55:36 +00002027 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2028 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2029 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2030 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2031 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2032 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2033 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2034 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2035 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2036 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2037 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2038 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2039 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2040 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2041 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2042 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2043 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2044}
2045
2046StructEntry struct_termios_def = {
2047 .convert = { host_to_target_termios, target_to_host_termios },
2048 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2049 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2050};
2051
bellard5286db72003-06-05 00:57:30 +00002052static bitmask_transtbl mmap_flags_tbl[] = {
2053 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2054 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2055 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2056 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2057 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2058 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2059 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2060 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2061 { 0, 0, 0, 0 }
2062};
2063
bellardffa65c32004-01-04 23:57:22 +00002064static bitmask_transtbl fcntl_flags_tbl[] = {
2065 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
2066 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
2067 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
2068 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
2069 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
2070 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
2071 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
2072 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
2073 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
2074 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
2075 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
2076 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
2077 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
bellard121061d2004-02-22 11:56:01 +00002078#if defined(O_DIRECT)
bellardffa65c32004-01-04 23:57:22 +00002079 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
bellard121061d2004-02-22 11:56:01 +00002080#endif
bellardffa65c32004-01-04 23:57:22 +00002081 { 0, 0, 0, 0 }
2082};
2083
bellard2ab83ea2003-06-15 19:56:46 +00002084#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00002085
2086/* NOTE: there is really one LDT for all the threads */
2087uint8_t *ldt_table;
2088
pbrook53a59602006-03-25 19:31:22 +00002089static int read_ldt(target_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002090{
2091 int size;
pbrook53a59602006-03-25 19:31:22 +00002092 void *p;
bellard6dbad632003-03-16 18:05:05 +00002093
2094 if (!ldt_table)
2095 return 0;
2096 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2097 if (size > bytecount)
2098 size = bytecount;
pbrook53a59602006-03-25 19:31:22 +00002099 p = lock_user(ptr, size, 0);
2100 /* ??? Shoudl this by byteswapped? */
2101 memcpy(p, ldt_table, size);
2102 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00002103 return size;
2104}
2105
2106/* XXX: add locking support */
ths5fafdf22007-09-16 21:08:06 +00002107static int write_ldt(CPUX86State *env,
pbrook53a59602006-03-25 19:31:22 +00002108 target_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00002109{
2110 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00002111 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00002112 int seg_32bit, contents, read_exec_only, limit_in_pages;
2113 int seg_not_present, useable;
2114 uint32_t *lp, entry_1, entry_2;
2115
2116 if (bytecount != sizeof(ldt_info))
2117 return -EINVAL;
pbrook53a59602006-03-25 19:31:22 +00002118 lock_user_struct(target_ldt_info, ptr, 1);
2119 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2120 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2121 ldt_info.limit = tswap32(target_ldt_info->limit);
2122 ldt_info.flags = tswap32(target_ldt_info->flags);
2123 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00002124
bellard6dbad632003-03-16 18:05:05 +00002125 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
2126 return -EINVAL;
2127 seg_32bit = ldt_info.flags & 1;
2128 contents = (ldt_info.flags >> 1) & 3;
2129 read_exec_only = (ldt_info.flags >> 3) & 1;
2130 limit_in_pages = (ldt_info.flags >> 4) & 1;
2131 seg_not_present = (ldt_info.flags >> 5) & 1;
2132 useable = (ldt_info.flags >> 6) & 1;
2133
2134 if (contents == 3) {
2135 if (oldmode)
2136 return -EINVAL;
2137 if (seg_not_present == 0)
2138 return -EINVAL;
2139 }
2140 /* allocate the LDT */
2141 if (!ldt_table) {
2142 ldt_table = malloc(TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
2143 if (!ldt_table)
2144 return -ENOMEM;
2145 memset(ldt_table, 0, TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
pbrook53a59602006-03-25 19:31:22 +00002146 env->ldt.base = h2g(ldt_table);
bellard6dbad632003-03-16 18:05:05 +00002147 env->ldt.limit = 0xffff;
2148 }
2149
2150 /* NOTE: same code as Linux kernel */
2151 /* Allow LDTs to be cleared by the user. */
2152 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2153 if (oldmode ||
2154 (contents == 0 &&
2155 read_exec_only == 1 &&
2156 seg_32bit == 0 &&
2157 limit_in_pages == 0 &&
2158 seg_not_present == 1 &&
2159 useable == 0 )) {
2160 entry_1 = 0;
2161 entry_2 = 0;
2162 goto install;
2163 }
2164 }
ths3b46e622007-09-17 08:09:54 +00002165
bellard6dbad632003-03-16 18:05:05 +00002166 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2167 (ldt_info.limit & 0x0ffff);
2168 entry_2 = (ldt_info.base_addr & 0xff000000) |
2169 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2170 (ldt_info.limit & 0xf0000) |
2171 ((read_exec_only ^ 1) << 9) |
2172 (contents << 10) |
2173 ((seg_not_present ^ 1) << 15) |
2174 (seg_32bit << 22) |
2175 (limit_in_pages << 23) |
2176 0x7000;
2177 if (!oldmode)
2178 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00002179
bellard6dbad632003-03-16 18:05:05 +00002180 /* Install the new entry ... */
2181install:
2182 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2183 lp[0] = tswap32(entry_1);
2184 lp[1] = tswap32(entry_2);
2185 return 0;
2186}
2187
2188/* specific and weird i386 syscalls */
pbrook53a59602006-03-25 19:31:22 +00002189int do_modify_ldt(CPUX86State *env, int func, target_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002190{
2191 int ret = -ENOSYS;
ths3b46e622007-09-17 08:09:54 +00002192
bellard6dbad632003-03-16 18:05:05 +00002193 switch (func) {
2194 case 0:
2195 ret = read_ldt(ptr, bytecount);
2196 break;
2197 case 1:
2198 ret = write_ldt(env, ptr, bytecount, 1);
2199 break;
2200 case 0x11:
2201 ret = write_ldt(env, ptr, bytecount, 0);
2202 break;
2203 }
2204 return ret;
2205}
bellard1b6b0292003-03-22 17:31:38 +00002206
bellard2ab83ea2003-06-15 19:56:46 +00002207#endif /* defined(TARGET_I386) */
2208
bellard1b6b0292003-03-22 17:31:38 +00002209/* this stack is the equivalent of the kernel stack associated with a
2210 thread/process */
2211#define NEW_STACK_SIZE 8192
2212
2213static int clone_func(void *arg)
2214{
bellard2ab83ea2003-06-15 19:56:46 +00002215 CPUState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00002216 cpu_loop(env);
2217 /* never exits */
2218 return 0;
2219}
2220
j_mayer32407102007-09-26 23:01:49 +00002221int do_fork(CPUState *env, unsigned int flags, target_ulong newsp)
bellard1b6b0292003-03-22 17:31:38 +00002222{
2223 int ret;
bellard5cd43932003-03-29 16:54:36 +00002224 TaskState *ts;
bellard1b6b0292003-03-22 17:31:38 +00002225 uint8_t *new_stack;
bellard2ab83ea2003-06-15 19:56:46 +00002226 CPUState *new_env;
ths3b46e622007-09-17 08:09:54 +00002227
bellard1b6b0292003-03-22 17:31:38 +00002228 if (flags & CLONE_VM) {
bellard5cd43932003-03-29 16:54:36 +00002229 ts = malloc(sizeof(TaskState) + NEW_STACK_SIZE);
2230 memset(ts, 0, sizeof(TaskState));
2231 new_stack = ts->stack;
2232 ts->used = 1;
2233 /* add in task state list */
2234 ts->next = first_task_state;
2235 first_task_state = ts;
bellard1b6b0292003-03-22 17:31:38 +00002236 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00002237 new_env = cpu_copy(env);
bellard2ab83ea2003-06-15 19:56:46 +00002238#if defined(TARGET_I386)
2239 if (!newsp)
2240 newsp = env->regs[R_ESP];
bellard1b6b0292003-03-22 17:31:38 +00002241 new_env->regs[R_ESP] = newsp;
2242 new_env->regs[R_EAX] = 0;
bellard2ab83ea2003-06-15 19:56:46 +00002243#elif defined(TARGET_ARM)
2244 if (!newsp)
2245 newsp = env->regs[13];
2246 new_env->regs[13] = newsp;
2247 new_env->regs[0] = 0;
bellardebc05482003-09-30 21:08:41 +00002248#elif defined(TARGET_SPARC)
bellard48dc41e2006-06-21 18:15:50 +00002249 if (!newsp)
2250 newsp = env->regwptr[22];
2251 new_env->regwptr[22] = newsp;
2252 new_env->regwptr[0] = 0;
2253 /* XXXXX */
bellard67867302003-11-23 17:05:30 +00002254 printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
pbrooke6e59062006-10-22 00:18:54 +00002255#elif defined(TARGET_M68K)
2256 if (!newsp)
2257 newsp = env->aregs[7];
2258 new_env->aregs[7] = newsp;
2259 new_env->dregs[0] = 0;
2260 /* ??? is this sufficient? */
bellard048f6b42005-11-26 18:47:20 +00002261#elif defined(TARGET_MIPS)
ths388bb212007-05-13 13:58:00 +00002262 if (!newsp)
thsead93602007-09-06 00:18:15 +00002263 newsp = env->gpr[29][env->current_tc];
2264 new_env->gpr[29][env->current_tc] = newsp;
bellard67867302003-11-23 17:05:30 +00002265#elif defined(TARGET_PPC)
2266 if (!newsp)
2267 newsp = env->gpr[1];
2268 new_env->gpr[1] = newsp;
ths5fafdf22007-09-16 21:08:06 +00002269 {
bellard67867302003-11-23 17:05:30 +00002270 int i;
2271 for (i = 7; i < 32; i++)
2272 new_env->gpr[i] = 0;
2273 }
bellardfdf9b3e2006-04-27 21:07:38 +00002274#elif defined(TARGET_SH4)
2275 if (!newsp)
2276 newsp = env->gregs[15];
2277 new_env->gregs[15] = newsp;
2278 /* XXXXX */
j_mayer7a3148a2007-04-05 07:13:51 +00002279#elif defined(TARGET_ALPHA)
2280 if (!newsp)
2281 newsp = env->ir[30];
2282 new_env->ir[30] = newsp;
2283 /* ? */
2284 {
2285 int i;
2286 for (i = 7; i < 30; i++)
2287 new_env->ir[i] = 0;
2288 }
bellard2ab83ea2003-06-15 19:56:46 +00002289#else
2290#error unsupported target CPU
2291#endif
bellard5cd43932003-03-29 16:54:36 +00002292 new_env->opaque = ts;
bellard27725c12003-04-29 21:08:18 +00002293#ifdef __ia64__
bellardfd4a43e2006-04-24 20:32:17 +00002294 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00002295#else
2296 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
2297#endif
bellard1b6b0292003-03-22 17:31:38 +00002298 } else {
2299 /* if no CLONE_VM, we consider it is a fork */
2300 if ((flags & ~CSIGNAL) != 0)
2301 return -EINVAL;
2302 ret = fork();
2303 }
2304 return ret;
2305}
2306
j_mayer32407102007-09-26 23:01:49 +00002307static target_long do_fcntl(int fd, int cmd, target_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00002308{
2309 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00002310 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00002311 struct flock64 fl64;
2312 struct target_flock64 *target_fl64;
j_mayer32407102007-09-26 23:01:49 +00002313 target_long ret;
pbrook53a59602006-03-25 19:31:22 +00002314
bellard7775e9e2003-05-14 22:46:48 +00002315 switch(cmd) {
2316 case TARGET_F_GETLK:
ths58134272007-03-31 18:59:32 +00002317 lock_user_struct(target_fl, arg, 1);
2318 fl.l_type = tswap16(target_fl->l_type);
2319 fl.l_whence = tswap16(target_fl->l_whence);
2320 fl.l_start = tswapl(target_fl->l_start);
2321 fl.l_len = tswapl(target_fl->l_len);
2322 fl.l_pid = tswapl(target_fl->l_pid);
2323 unlock_user_struct(target_fl, arg, 0);
bellard7775e9e2003-05-14 22:46:48 +00002324 ret = fcntl(fd, cmd, &fl);
2325 if (ret == 0) {
pbrook53a59602006-03-25 19:31:22 +00002326 lock_user_struct(target_fl, arg, 0);
bellard7775e9e2003-05-14 22:46:48 +00002327 target_fl->l_type = tswap16(fl.l_type);
2328 target_fl->l_whence = tswap16(fl.l_whence);
2329 target_fl->l_start = tswapl(fl.l_start);
2330 target_fl->l_len = tswapl(fl.l_len);
2331 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00002332 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00002333 }
2334 break;
ths3b46e622007-09-17 08:09:54 +00002335
bellard7775e9e2003-05-14 22:46:48 +00002336 case TARGET_F_SETLK:
2337 case TARGET_F_SETLKW:
pbrook53a59602006-03-25 19:31:22 +00002338 lock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00002339 fl.l_type = tswap16(target_fl->l_type);
2340 fl.l_whence = tswap16(target_fl->l_whence);
2341 fl.l_start = tswapl(target_fl->l_start);
2342 fl.l_len = tswapl(target_fl->l_len);
2343 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00002344 unlock_user_struct(target_fl, arg, 0);
bellard7775e9e2003-05-14 22:46:48 +00002345 ret = fcntl(fd, cmd, &fl);
2346 break;
ths3b46e622007-09-17 08:09:54 +00002347
bellard7775e9e2003-05-14 22:46:48 +00002348 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00002349 lock_user_struct(target_fl64, arg, 1);
2350 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2351 fl64.l_whence = tswap16(target_fl64->l_whence);
2352 fl64.l_start = tswapl(target_fl64->l_start);
2353 fl64.l_len = tswapl(target_fl64->l_len);
2354 fl64.l_pid = tswap16(target_fl64->l_pid);
2355 unlock_user_struct(target_fl64, arg, 0);
ths43f238d2007-01-05 20:55:49 +00002356 ret = fcntl(fd, cmd >> 1, &fl64);
2357 if (ret == 0) {
2358 lock_user_struct(target_fl64, arg, 0);
2359 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
2360 target_fl64->l_whence = tswap16(fl64.l_whence);
2361 target_fl64->l_start = tswapl(fl64.l_start);
2362 target_fl64->l_len = tswapl(fl64.l_len);
2363 target_fl64->l_pid = tswapl(fl64.l_pid);
2364 unlock_user_struct(target_fl64, arg, 1);
2365 }
2366 break;
bellard7775e9e2003-05-14 22:46:48 +00002367 case TARGET_F_SETLK64:
2368 case TARGET_F_SETLKW64:
ths43f238d2007-01-05 20:55:49 +00002369 lock_user_struct(target_fl64, arg, 1);
2370 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
2371 fl64.l_whence = tswap16(target_fl64->l_whence);
2372 fl64.l_start = tswapl(target_fl64->l_start);
2373 fl64.l_len = tswapl(target_fl64->l_len);
2374 fl64.l_pid = tswap16(target_fl64->l_pid);
2375 unlock_user_struct(target_fl64, arg, 0);
2376 ret = fcntl(fd, cmd >> 1, &fl64);
bellard7775e9e2003-05-14 22:46:48 +00002377 break;
2378
bellardffa65c32004-01-04 23:57:22 +00002379 case F_GETFL:
2380 ret = fcntl(fd, cmd, arg);
2381 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
2382 break;
2383
2384 case F_SETFL:
2385 ret = fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl));
2386 break;
2387
bellard7775e9e2003-05-14 22:46:48 +00002388 default:
2389 ret = fcntl(fd, cmd, arg);
2390 break;
2391 }
2392 return ret;
2393}
2394
bellard67867302003-11-23 17:05:30 +00002395#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00002396
bellard67867302003-11-23 17:05:30 +00002397static inline int high2lowuid(int uid)
2398{
2399 if (uid > 65535)
2400 return 65534;
2401 else
2402 return uid;
2403}
2404
2405static inline int high2lowgid(int gid)
2406{
2407 if (gid > 65535)
2408 return 65534;
2409 else
2410 return gid;
2411}
2412
2413static inline int low2highuid(int uid)
2414{
2415 if ((int16_t)uid == -1)
2416 return -1;
2417 else
2418 return uid;
2419}
2420
2421static inline int low2highgid(int gid)
2422{
2423 if ((int16_t)gid == -1)
2424 return -1;
2425 else
2426 return gid;
2427}
2428
2429#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00002430
bellard31e31b82003-02-18 22:55:36 +00002431void syscall_init(void)
2432{
bellard2ab83ea2003-06-15 19:56:46 +00002433 IOCTLEntry *ie;
2434 const argtype *arg_type;
2435 int size;
2436
ths5fafdf22007-09-16 21:08:06 +00002437#define STRUCT(name, list...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
2438#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00002439#include "syscall_types.h"
2440#undef STRUCT
2441#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00002442
2443 /* we patch the ioctl size if necessary. We rely on the fact that
2444 no ioctl has all the bits at '1' in the size field */
2445 ie = ioctl_entries;
2446 while (ie->target_cmd != 0) {
2447 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
2448 TARGET_IOC_SIZEMASK) {
2449 arg_type = ie->arg_type;
2450 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00002451 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002452 ie->target_cmd);
2453 exit(1);
2454 }
2455 arg_type++;
2456 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00002457 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00002458 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
2459 (size << TARGET_IOC_SIZESHIFT);
2460 }
2461 /* automatic consistency check if same arch */
2462#if defined(__i386__) && defined(TARGET_I386)
2463 if (ie->target_cmd != ie->host_cmd) {
ths5fafdf22007-09-16 21:08:06 +00002464 fprintf(stderr, "ERROR: ioctl: target=0x%x host=0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00002465 ie->target_cmd, ie->host_cmd);
2466 }
2467#endif
2468 ie++;
2469 }
bellard31e31b82003-02-18 22:55:36 +00002470}
bellardc573ff62004-01-04 15:51:36 +00002471
j_mayer32407102007-09-26 23:01:49 +00002472#if TARGET_LONG_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00002473static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
2474{
2475#ifdef TARGET_WORDS_BIG_ENDIAN
2476 return ((uint64_t)word0 << 32) | word1;
2477#else
2478 return ((uint64_t)word1 << 32) | word0;
2479#endif
2480}
j_mayer32407102007-09-26 23:01:49 +00002481#else /* TARGET_LONG_BITS == 32 */
2482static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
2483{
2484 return word0;
2485}
2486#endif /* TARGET_LONG_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00002487
2488#ifdef TARGET_NR_truncate64
j_mayer32407102007-09-26 23:01:49 +00002489static inline target_long target_truncate64(void *cpu_env, const char *arg1,
2490 target_long arg2,
2491 target_long arg3,
2492 target_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00002493{
2494#ifdef TARGET_ARM
2495 if (((CPUARMState *)cpu_env)->eabi)
2496 {
2497 arg2 = arg3;
2498 arg3 = arg4;
2499 }
2500#endif
2501 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
2502}
2503#endif
2504
2505#ifdef TARGET_NR_ftruncate64
j_mayer32407102007-09-26 23:01:49 +00002506static inline target_long target_ftruncate64(void *cpu_env, target_long arg1,
2507 target_long arg2,
2508 target_long arg3,
2509 target_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00002510{
2511#ifdef TARGET_ARM
2512 if (((CPUARMState *)cpu_env)->eabi)
2513 {
2514 arg2 = arg3;
2515 arg3 = arg4;
2516 }
2517#endif
2518 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
2519}
2520#endif
2521
pbrook53a59602006-03-25 19:31:22 +00002522static inline void target_to_host_timespec(struct timespec *host_ts,
2523 target_ulong target_addr)
2524{
2525 struct target_timespec *target_ts;
2526
2527 lock_user_struct(target_ts, target_addr, 1);
2528 host_ts->tv_sec = tswapl(target_ts->tv_sec);
2529 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
2530 unlock_user_struct(target_ts, target_addr, 0);
2531}
2532
2533static inline void host_to_target_timespec(target_ulong target_addr,
2534 struct timespec *host_ts)
2535{
2536 struct target_timespec *target_ts;
2537
2538 lock_user_struct(target_ts, target_addr, 0);
2539 target_ts->tv_sec = tswapl(host_ts->tv_sec);
2540 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
2541 unlock_user_struct(target_ts, target_addr, 1);
2542}
2543
j_mayer32407102007-09-26 23:01:49 +00002544target_long do_syscall(void *cpu_env, int num, target_long arg1,
2545 target_long arg2, target_long arg3, target_long arg4,
2546 target_long arg5, target_long arg6)
bellard31e31b82003-02-18 22:55:36 +00002547{
j_mayer32407102007-09-26 23:01:49 +00002548 target_long ret;
bellard31e31b82003-02-18 22:55:36 +00002549 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00002550 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00002551 void *p;
ths3b46e622007-09-17 08:09:54 +00002552
bellard72f03902003-02-18 23:33:18 +00002553#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00002554 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00002555#endif
bellard31e31b82003-02-18 22:55:36 +00002556 switch(num) {
2557 case TARGET_NR_exit:
bellard7d132992003-03-06 23:23:54 +00002558#ifdef HAVE_GPROF
2559 _mcleanup();
2560#endif
bellarde9009672005-04-26 20:42:36 +00002561 gdb_exit(cpu_env, arg1);
bellard1b6b0292003-03-22 17:31:38 +00002562 /* XXX: should free thread stack and CPU env */
bellard31e31b82003-02-18 22:55:36 +00002563 _exit(arg1);
2564 ret = 0; /* avoid warning */
2565 break;
2566 case TARGET_NR_read:
pbrook53a59602006-03-25 19:31:22 +00002567 page_unprotect_range(arg2, arg3);
2568 p = lock_user(arg2, arg3, 0);
2569 ret = get_errno(read(arg1, p, arg3));
2570 unlock_user(p, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00002571 break;
2572 case TARGET_NR_write:
pbrook53a59602006-03-25 19:31:22 +00002573 p = lock_user(arg2, arg3, 1);
2574 ret = get_errno(write(arg1, p, arg3));
2575 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00002576 break;
2577 case TARGET_NR_open:
pbrook53a59602006-03-25 19:31:22 +00002578 p = lock_user_string(arg1);
2579 ret = get_errno(open(path(p),
bellardffa65c32004-01-04 23:57:22 +00002580 target_to_host_bitmask(arg2, fcntl_flags_tbl),
2581 arg3));
pbrook53a59602006-03-25 19:31:22 +00002582 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002583 break;
ths82424832007-09-24 09:21:55 +00002584#if defined(TARGET_NR_openat) && defined(__NR_openat)
2585 case TARGET_NR_openat:
2586 if (!arg2) {
2587 ret = -EFAULT;
2588 goto fail;
2589 }
2590 p = lock_user_string(arg2);
2591 if (!access_ok(VERIFY_READ, p, 1))
2592 ret = -EFAULT;
2593 else
2594 ret = get_errno(sys_openat(arg1,
2595 path(p),
2596 target_to_host_bitmask(arg3, fcntl_flags_tbl),
2597 arg4));
2598 if (p)
2599 unlock_user(p, arg2, 0);
2600 break;
2601#endif
bellard31e31b82003-02-18 22:55:36 +00002602 case TARGET_NR_close:
2603 ret = get_errno(close(arg1));
2604 break;
2605 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00002606 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00002607 break;
2608 case TARGET_NR_fork:
bellard1b6b0292003-03-22 17:31:38 +00002609 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00002610 break;
thse5febef2007-04-01 18:31:35 +00002611#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00002612 case TARGET_NR_waitpid:
2613 {
pbrook53a59602006-03-25 19:31:22 +00002614 int status;
2615 ret = get_errno(waitpid(arg1, &status, arg3));
2616 if (!is_error(ret) && arg2)
2617 tput32(arg2, status);
bellard31e31b82003-02-18 22:55:36 +00002618 }
2619 break;
thse5febef2007-04-01 18:31:35 +00002620#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002621#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002622 case TARGET_NR_creat:
pbrook53a59602006-03-25 19:31:22 +00002623 p = lock_user_string(arg1);
2624 ret = get_errno(creat(p, arg2));
2625 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002626 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002627#endif
bellard31e31b82003-02-18 22:55:36 +00002628 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00002629 {
2630 void * p2;
2631 p = lock_user_string(arg1);
2632 p2 = lock_user_string(arg2);
2633 ret = get_errno(link(p, p2));
2634 unlock_user(p2, arg2, 0);
2635 unlock_user(p, arg1, 0);
2636 }
bellard31e31b82003-02-18 22:55:36 +00002637 break;
ths64f0ce42007-09-24 09:25:06 +00002638#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
2639 case TARGET_NR_linkat:
2640 if (!arg2 || !arg4) {
2641 ret = -EFAULT;
2642 goto fail;
2643 }
2644 {
2645 void * p2 = NULL;
2646 p = lock_user_string(arg2);
2647 p2 = lock_user_string(arg4);
2648 if (!access_ok(VERIFY_READ, p, 1)
2649 || !access_ok(VERIFY_READ, p2, 1))
2650 ret = -EFAULT;
2651 else
2652 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
2653 if (p2)
2654 unlock_user(p, arg2, 0);
2655 if (p)
2656 unlock_user(p2, arg4, 0);
2657 }
2658 break;
2659#endif
bellard31e31b82003-02-18 22:55:36 +00002660 case TARGET_NR_unlink:
pbrook53a59602006-03-25 19:31:22 +00002661 p = lock_user_string(arg1);
2662 ret = get_errno(unlink(p));
2663 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002664 break;
ths8170f562007-09-24 09:24:11 +00002665#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
2666 case TARGET_NR_unlinkat:
2667 if (!arg2) {
2668 ret = -EFAULT;
2669 goto fail;
2670 }
2671 p = lock_user_string(arg2);
2672 if (!access_ok(VERIFY_READ, p, 1))
2673 ret = -EFAULT;
2674 else
2675 ret = get_errno(sys_unlinkat(arg1, p, arg3));
2676 if (p)
2677 unlock_user(p, arg2, 0);
2678 break;
2679#endif
bellard31e31b82003-02-18 22:55:36 +00002680 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00002681 {
2682 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00002683 int argc, envc;
pbrook53a59602006-03-25 19:31:22 +00002684 target_ulong gp;
2685 target_ulong guest_argp;
2686 target_ulong guest_envp;
2687 target_ulong addr;
bellard7854b052003-03-29 17:22:23 +00002688 char **q;
2689
bellardf7341ff2003-03-30 21:00:25 +00002690 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00002691 guest_argp = arg2;
2692 for (gp = guest_argp; tgetl(gp); gp++)
bellard7854b052003-03-29 17:22:23 +00002693 argc++;
bellardf7341ff2003-03-30 21:00:25 +00002694 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00002695 guest_envp = arg3;
2696 for (gp = guest_envp; tgetl(gp); gp++)
bellard7854b052003-03-29 17:22:23 +00002697 envc++;
2698
bellardf7341ff2003-03-30 21:00:25 +00002699 argp = alloca((argc + 1) * sizeof(void *));
2700 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00002701
pbrook53a59602006-03-25 19:31:22 +00002702 for (gp = guest_argp, q = argp; ;
2703 gp += sizeof(target_ulong), q++) {
2704 addr = tgetl(gp);
2705 if (!addr)
2706 break;
2707 *q = lock_user_string(addr);
2708 }
bellardf7341ff2003-03-30 21:00:25 +00002709 *q = NULL;
2710
pbrook53a59602006-03-25 19:31:22 +00002711 for (gp = guest_envp, q = envp; ;
2712 gp += sizeof(target_ulong), q++) {
2713 addr = tgetl(gp);
2714 if (!addr)
2715 break;
2716 *q = lock_user_string(addr);
2717 }
bellardf7341ff2003-03-30 21:00:25 +00002718 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00002719
pbrook53a59602006-03-25 19:31:22 +00002720 p = lock_user_string(arg1);
2721 ret = get_errno(execve(p, argp, envp));
2722 unlock_user(p, arg1, 0);
2723
2724 for (gp = guest_argp, q = argp; *q;
2725 gp += sizeof(target_ulong), q++) {
2726 addr = tgetl(gp);
2727 unlock_user(*q, addr, 0);
2728 }
2729 for (gp = guest_envp, q = envp; *q;
2730 gp += sizeof(target_ulong), q++) {
2731 addr = tgetl(gp);
2732 unlock_user(*q, addr, 0);
2733 }
bellard7854b052003-03-29 17:22:23 +00002734 }
bellard31e31b82003-02-18 22:55:36 +00002735 break;
2736 case TARGET_NR_chdir:
pbrook53a59602006-03-25 19:31:22 +00002737 p = lock_user_string(arg1);
2738 ret = get_errno(chdir(p));
2739 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002740 break;
bellarda315a142005-01-30 22:59:18 +00002741#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00002742 case TARGET_NR_time:
2743 {
pbrook53a59602006-03-25 19:31:22 +00002744 time_t host_time;
2745 ret = get_errno(time(&host_time));
2746 if (!is_error(ret) && arg1)
2747 tputl(arg1, host_time);
bellard31e31b82003-02-18 22:55:36 +00002748 }
2749 break;
bellarda315a142005-01-30 22:59:18 +00002750#endif
bellard31e31b82003-02-18 22:55:36 +00002751 case TARGET_NR_mknod:
pbrook53a59602006-03-25 19:31:22 +00002752 p = lock_user_string(arg1);
2753 ret = get_errno(mknod(p, arg2, arg3));
2754 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002755 break;
ths75ac37a2007-09-24 09:23:05 +00002756#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
2757 case TARGET_NR_mknodat:
2758 if (!arg2) {
2759 ret = -EFAULT;
2760 goto fail;
2761 }
2762 p = lock_user_string(arg2);
2763 if (!access_ok(VERIFY_READ, p, 1))
2764 ret = -EFAULT;
2765 else
2766 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
2767 if (p)
2768 unlock_user(p, arg2, 0);
2769 break;
2770#endif
bellard31e31b82003-02-18 22:55:36 +00002771 case TARGET_NR_chmod:
pbrook53a59602006-03-25 19:31:22 +00002772 p = lock_user_string(arg1);
2773 ret = get_errno(chmod(p, arg2));
2774 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002775 break;
bellardebc05482003-09-30 21:08:41 +00002776#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00002777 case TARGET_NR_break:
2778 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002779#endif
2780#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00002781 case TARGET_NR_oldstat:
2782 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002783#endif
bellard31e31b82003-02-18 22:55:36 +00002784 case TARGET_NR_lseek:
2785 ret = get_errno(lseek(arg1, arg2, arg3));
2786 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002787#ifdef TARGET_NR_getxpid
2788 case TARGET_NR_getxpid:
2789#else
bellard31e31b82003-02-18 22:55:36 +00002790 case TARGET_NR_getpid:
j_mayer7a3148a2007-04-05 07:13:51 +00002791#endif
bellard31e31b82003-02-18 22:55:36 +00002792 ret = get_errno(getpid());
2793 break;
2794 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00002795 {
2796 /* need to look at the data field */
2797 void *p2, *p3;
2798 p = lock_user_string(arg1);
2799 p2 = lock_user_string(arg2);
2800 p3 = lock_user_string(arg3);
2801 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, (const void *)arg5));
2802 unlock_user(p, arg1, 0);
2803 unlock_user(p2, arg2, 0);
2804 unlock_user(p3, arg3, 0);
2805 break;
2806 }
thse5febef2007-04-01 18:31:35 +00002807#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00002808 case TARGET_NR_umount:
pbrook53a59602006-03-25 19:31:22 +00002809 p = lock_user_string(arg1);
2810 ret = get_errno(umount(p));
2811 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002812 break;
thse5febef2007-04-01 18:31:35 +00002813#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002814#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002815 case TARGET_NR_stime:
2816 {
pbrook53a59602006-03-25 19:31:22 +00002817 time_t host_time;
2818 host_time = tgetl(arg1);
2819 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00002820 }
2821 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002822#endif
bellard31e31b82003-02-18 22:55:36 +00002823 case TARGET_NR_ptrace:
2824 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00002825#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002826 case TARGET_NR_alarm:
2827 ret = alarm(arg1);
2828 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002829#endif
bellardebc05482003-09-30 21:08:41 +00002830#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00002831 case TARGET_NR_oldfstat:
2832 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002833#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002834#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002835 case TARGET_NR_pause:
2836 ret = get_errno(pause());
2837 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002838#endif
thse5febef2007-04-01 18:31:35 +00002839#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00002840 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00002841 {
pbrook53a59602006-03-25 19:31:22 +00002842 struct utimbuf tbuf, *host_tbuf;
2843 struct target_utimbuf *target_tbuf;
2844 if (arg2) {
2845 lock_user_struct(target_tbuf, arg2, 1);
2846 tbuf.actime = tswapl(target_tbuf->actime);
2847 tbuf.modtime = tswapl(target_tbuf->modtime);
2848 unlock_user_struct(target_tbuf, arg2, 0);
2849 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00002850 } else {
pbrook53a59602006-03-25 19:31:22 +00002851 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00002852 }
pbrook53a59602006-03-25 19:31:22 +00002853 p = lock_user_string(arg1);
2854 ret = get_errno(utime(p, host_tbuf));
2855 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00002856 }
2857 break;
thse5febef2007-04-01 18:31:35 +00002858#endif
bellard978a66f2004-12-06 22:58:05 +00002859 case TARGET_NR_utimes:
2860 {
bellard978a66f2004-12-06 22:58:05 +00002861 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00002862 if (arg2) {
2863 target_to_host_timeval(&tv[0], arg2);
2864 target_to_host_timeval(&tv[1],
2865 arg2 + sizeof (struct target_timeval));
bellard978a66f2004-12-06 22:58:05 +00002866 tvp = tv;
2867 } else {
2868 tvp = NULL;
2869 }
pbrook53a59602006-03-25 19:31:22 +00002870 p = lock_user_string(arg1);
2871 ret = get_errno(utimes(p, tvp));
2872 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00002873 }
2874 break;
bellardebc05482003-09-30 21:08:41 +00002875#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00002876 case TARGET_NR_stty:
2877 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002878#endif
2879#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00002880 case TARGET_NR_gtty:
2881 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002882#endif
bellard31e31b82003-02-18 22:55:36 +00002883 case TARGET_NR_access:
pbrook53a59602006-03-25 19:31:22 +00002884 p = lock_user_string(arg1);
2885 ret = get_errno(access(p, arg2));
2886 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002887 break;
ths92a34c12007-09-24 09:27:49 +00002888#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
2889 case TARGET_NR_faccessat:
2890 if (!arg2) {
2891 ret = -EFAULT;
2892 goto fail;
2893 }
2894 p = lock_user_string(arg2);
2895 if (!access_ok(VERIFY_READ, p, 1))
2896 ret = -EFAULT;
2897 else
2898 ret = get_errno(sys_faccessat(arg1, p, arg3, arg4));
2899 if (p)
2900 unlock_user(p, arg2, 0);
2901 break;
2902#endif
j_mayer7a3148a2007-04-05 07:13:51 +00002903#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00002904 case TARGET_NR_nice:
2905 ret = get_errno(nice(arg1));
2906 break;
j_mayer7a3148a2007-04-05 07:13:51 +00002907#endif
bellardebc05482003-09-30 21:08:41 +00002908#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00002909 case TARGET_NR_ftime:
2910 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00002911#endif
bellard31e31b82003-02-18 22:55:36 +00002912 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00002913 sync();
2914 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00002915 break;
2916 case TARGET_NR_kill:
2917 ret = get_errno(kill(arg1, arg2));
2918 break;
2919 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00002920 {
2921 void *p2;
2922 p = lock_user_string(arg1);
2923 p2 = lock_user_string(arg2);
2924 ret = get_errno(rename(p, p2));
2925 unlock_user(p2, arg2, 0);
2926 unlock_user(p, arg1, 0);
2927 }
bellard31e31b82003-02-18 22:55:36 +00002928 break;
ths722183f2007-09-24 09:24:37 +00002929#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
2930 case TARGET_NR_renameat:
2931 if (!arg2 || !arg4) {
2932 ret = -EFAULT;
2933 goto fail;
2934 }
2935 {
2936 void *p2 = NULL;
2937 p = lock_user_string(arg2);
2938 p2 = lock_user_string(arg4);
2939 if (!access_ok(VERIFY_READ, p, 1)
2940 || !access_ok(VERIFY_READ, p2, 1))
2941 ret = -EFAULT;
2942 else
2943 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
2944 if (p2)
2945 unlock_user(p2, arg4, 0);
2946 if (p)
2947 unlock_user(p, arg2, 0);
2948 }
2949 break;
2950#endif
bellard31e31b82003-02-18 22:55:36 +00002951 case TARGET_NR_mkdir:
pbrook53a59602006-03-25 19:31:22 +00002952 p = lock_user_string(arg1);
2953 ret = get_errno(mkdir(p, arg2));
2954 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002955 break;
ths4472ad02007-09-24 09:22:32 +00002956#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
2957 case TARGET_NR_mkdirat:
2958 if (!arg2) {
2959 ret = -EFAULT;
2960 goto fail;
2961 }
2962 p = lock_user_string(arg2);
2963 if (!access_ok(VERIFY_READ, p, 1))
2964 ret = -EFAULT;
2965 else
2966 ret = get_errno(sys_mkdirat(arg1, p, arg3));
2967 if (p)
2968 unlock_user(p, arg2, 0);
2969 break;
2970#endif
bellard31e31b82003-02-18 22:55:36 +00002971 case TARGET_NR_rmdir:
pbrook53a59602006-03-25 19:31:22 +00002972 p = lock_user_string(arg1);
2973 ret = get_errno(rmdir(p));
2974 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00002975 break;
2976 case TARGET_NR_dup:
2977 ret = get_errno(dup(arg1));
2978 break;
2979 case TARGET_NR_pipe:
2980 {
pbrook53a59602006-03-25 19:31:22 +00002981 int host_pipe[2];
2982 ret = get_errno(pipe(host_pipe));
bellard31e31b82003-02-18 22:55:36 +00002983 if (!is_error(ret)) {
thsc12ab052007-06-01 11:50:36 +00002984#if defined(TARGET_MIPS)
thsead93602007-09-06 00:18:15 +00002985 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
2986 env->gpr[3][env->current_tc] = host_pipe[1];
thsc12ab052007-06-01 11:50:36 +00002987 ret = host_pipe[0];
2988#else
pbrook53a59602006-03-25 19:31:22 +00002989 tput32(arg1, host_pipe[0]);
2990 tput32(arg1 + 4, host_pipe[1]);
thsc12ab052007-06-01 11:50:36 +00002991#endif
bellard31e31b82003-02-18 22:55:36 +00002992 }
2993 }
2994 break;
2995 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00002996 {
pbrook53a59602006-03-25 19:31:22 +00002997 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00002998 struct tms tms;
2999 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00003000 if (arg1) {
3001 tmsp = lock_user(arg1, sizeof(struct target_tms), 0);
bellardc596ed12003-07-13 17:32:31 +00003002 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
3003 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
3004 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
3005 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00003006 }
bellardc596ed12003-07-13 17:32:31 +00003007 if (!is_error(ret))
3008 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00003009 }
3010 break;
bellardebc05482003-09-30 21:08:41 +00003011#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00003012 case TARGET_NR_prof:
3013 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003014#endif
thse5febef2007-04-01 18:31:35 +00003015#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00003016 case TARGET_NR_signal:
3017 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003018#endif
bellard31e31b82003-02-18 22:55:36 +00003019 case TARGET_NR_acct:
pbrook24836682006-04-16 14:14:53 +00003020 p = lock_user_string(arg1);
3021 ret = get_errno(acct(path(p)));
3022 unlock_user(p, arg1, 0);
3023 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003024#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003025 case TARGET_NR_umount2:
pbrook53a59602006-03-25 19:31:22 +00003026 p = lock_user_string(arg1);
3027 ret = get_errno(umount2(p, arg2));
3028 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003029 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003030#endif
bellardebc05482003-09-30 21:08:41 +00003031#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00003032 case TARGET_NR_lock:
3033 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003034#endif
bellard31e31b82003-02-18 22:55:36 +00003035 case TARGET_NR_ioctl:
3036 ret = do_ioctl(arg1, arg2, arg3);
3037 break;
3038 case TARGET_NR_fcntl:
bellard7775e9e2003-05-14 22:46:48 +00003039 ret = get_errno(do_fcntl(arg1, arg2, arg3));
bellard31e31b82003-02-18 22:55:36 +00003040 break;
bellardebc05482003-09-30 21:08:41 +00003041#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00003042 case TARGET_NR_mpx:
3043 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003044#endif
bellard31e31b82003-02-18 22:55:36 +00003045 case TARGET_NR_setpgid:
3046 ret = get_errno(setpgid(arg1, arg2));
3047 break;
bellardebc05482003-09-30 21:08:41 +00003048#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00003049 case TARGET_NR_ulimit:
3050 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003051#endif
3052#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00003053 case TARGET_NR_oldolduname:
3054 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003055#endif
bellard31e31b82003-02-18 22:55:36 +00003056 case TARGET_NR_umask:
3057 ret = get_errno(umask(arg1));
3058 break;
3059 case TARGET_NR_chroot:
pbrook53a59602006-03-25 19:31:22 +00003060 p = lock_user_string(arg1);
3061 ret = get_errno(chroot(p));
3062 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003063 break;
3064 case TARGET_NR_ustat:
3065 goto unimplemented;
3066 case TARGET_NR_dup2:
3067 ret = get_errno(dup2(arg1, arg2));
3068 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003069#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003070 case TARGET_NR_getppid:
3071 ret = get_errno(getppid());
3072 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003073#endif
bellard31e31b82003-02-18 22:55:36 +00003074 case TARGET_NR_getpgrp:
3075 ret = get_errno(getpgrp());
3076 break;
3077 case TARGET_NR_setsid:
3078 ret = get_errno(setsid());
3079 break;
thse5febef2007-04-01 18:31:35 +00003080#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00003081 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00003082 {
ths388bb212007-05-13 13:58:00 +00003083#if !defined(TARGET_MIPS)
pbrook53a59602006-03-25 19:31:22 +00003084 struct target_old_sigaction *old_act;
bellard66fb9762003-03-23 01:06:05 +00003085 struct target_sigaction act, oact, *pact;
pbrook53a59602006-03-25 19:31:22 +00003086 if (arg2) {
3087 lock_user_struct(old_act, arg2, 1);
bellard66fb9762003-03-23 01:06:05 +00003088 act._sa_handler = old_act->_sa_handler;
3089 target_siginitset(&act.sa_mask, old_act->sa_mask);
3090 act.sa_flags = old_act->sa_flags;
3091 act.sa_restorer = old_act->sa_restorer;
pbrook53a59602006-03-25 19:31:22 +00003092 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003093 pact = &act;
3094 } else {
3095 pact = NULL;
3096 }
3097 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00003098 if (!is_error(ret) && arg3) {
3099 lock_user_struct(old_act, arg3, 0);
3100 old_act->_sa_handler = oact._sa_handler;
3101 old_act->sa_mask = oact.sa_mask.sig[0];
3102 old_act->sa_flags = oact.sa_flags;
3103 old_act->sa_restorer = oact.sa_restorer;
3104 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00003105 }
ths388bb212007-05-13 13:58:00 +00003106#else
bellard106ec872006-06-27 21:08:10 +00003107 struct target_sigaction act, oact, *pact, *old_act;
3108
3109 if (arg2) {
3110 lock_user_struct(old_act, arg2, 1);
3111 act._sa_handler = old_act->_sa_handler;
3112 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
3113 act.sa_flags = old_act->sa_flags;
3114 unlock_user_struct(old_act, arg2, 0);
3115 pact = &act;
3116 } else {
3117 pact = NULL;
3118 }
3119
3120 ret = get_errno(do_sigaction(arg1, pact, &oact));
3121
3122 if (!is_error(ret) && arg3) {
3123 lock_user_struct(old_act, arg3, 0);
3124 old_act->_sa_handler = oact._sa_handler;
3125 old_act->sa_flags = oact.sa_flags;
3126 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
3127 old_act->sa_mask.sig[1] = 0;
3128 old_act->sa_mask.sig[2] = 0;
3129 old_act->sa_mask.sig[3] = 0;
3130 unlock_user_struct(old_act, arg3, 1);
3131 }
ths388bb212007-05-13 13:58:00 +00003132#endif
bellard31e31b82003-02-18 22:55:36 +00003133 }
3134 break;
thse5febef2007-04-01 18:31:35 +00003135#endif
bellard66fb9762003-03-23 01:06:05 +00003136 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00003137 {
3138 struct target_sigaction *act;
3139 struct target_sigaction *oact;
3140
3141 if (arg2)
3142 lock_user_struct(act, arg2, 1);
3143 else
3144 act = NULL;
3145 if (arg3)
3146 lock_user_struct(oact, arg3, 0);
3147 else
3148 oact = NULL;
3149 ret = get_errno(do_sigaction(arg1, act, oact));
3150 if (arg2)
3151 unlock_user_struct(act, arg2, 0);
3152 if (arg3)
3153 unlock_user_struct(oact, arg3, 1);
3154 }
bellard66fb9762003-03-23 01:06:05 +00003155 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003156#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003157 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00003158 {
3159 sigset_t cur_set;
3160 target_ulong target_set;
3161 sigprocmask(0, NULL, &cur_set);
3162 host_to_target_old_sigset(&target_set, &cur_set);
3163 ret = target_set;
3164 }
3165 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003166#endif
3167#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003168 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00003169 {
3170 sigset_t set, oset, cur_set;
3171 target_ulong target_set = arg1;
3172 sigprocmask(0, NULL, &cur_set);
3173 target_to_host_old_sigset(&set, &target_set);
3174 sigorset(&set, &set, &cur_set);
3175 sigprocmask(SIG_SETMASK, &set, &oset);
3176 host_to_target_old_sigset(&target_set, &oset);
3177 ret = target_set;
3178 }
3179 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003180#endif
thse5febef2007-04-01 18:31:35 +00003181#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00003182 case TARGET_NR_sigprocmask:
3183 {
3184 int how = arg1;
3185 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003186
pbrook53a59602006-03-25 19:31:22 +00003187 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003188 switch(how) {
3189 case TARGET_SIG_BLOCK:
3190 how = SIG_BLOCK;
3191 break;
3192 case TARGET_SIG_UNBLOCK:
3193 how = SIG_UNBLOCK;
3194 break;
3195 case TARGET_SIG_SETMASK:
3196 how = SIG_SETMASK;
3197 break;
3198 default:
3199 ret = -EINVAL;
3200 goto fail;
3201 }
pbrook53a59602006-03-25 19:31:22 +00003202 p = lock_user(arg2, sizeof(target_sigset_t), 1);
3203 target_to_host_old_sigset(&set, p);
3204 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003205 set_ptr = &set;
3206 } else {
3207 how = 0;
3208 set_ptr = NULL;
3209 }
3210 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003211 if (!is_error(ret) && arg3) {
3212 p = lock_user(arg3, sizeof(target_sigset_t), 0);
3213 host_to_target_old_sigset(p, &oldset);
3214 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003215 }
3216 }
3217 break;
thse5febef2007-04-01 18:31:35 +00003218#endif
bellard66fb9762003-03-23 01:06:05 +00003219 case TARGET_NR_rt_sigprocmask:
3220 {
3221 int how = arg1;
3222 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00003223
pbrook53a59602006-03-25 19:31:22 +00003224 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003225 switch(how) {
3226 case TARGET_SIG_BLOCK:
3227 how = SIG_BLOCK;
3228 break;
3229 case TARGET_SIG_UNBLOCK:
3230 how = SIG_UNBLOCK;
3231 break;
3232 case TARGET_SIG_SETMASK:
3233 how = SIG_SETMASK;
3234 break;
3235 default:
3236 ret = -EINVAL;
3237 goto fail;
3238 }
pbrook53a59602006-03-25 19:31:22 +00003239 p = lock_user(arg2, sizeof(target_sigset_t), 1);
3240 target_to_host_sigset(&set, p);
3241 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00003242 set_ptr = &set;
3243 } else {
3244 how = 0;
3245 set_ptr = NULL;
3246 }
3247 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00003248 if (!is_error(ret) && arg3) {
3249 p = lock_user(arg3, sizeof(target_sigset_t), 0);
3250 host_to_target_sigset(p, &oldset);
3251 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003252 }
3253 }
3254 break;
thse5febef2007-04-01 18:31:35 +00003255#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00003256 case TARGET_NR_sigpending:
3257 {
3258 sigset_t set;
3259 ret = get_errno(sigpending(&set));
3260 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003261 p = lock_user(arg1, sizeof(target_sigset_t), 0);
3262 host_to_target_old_sigset(p, &set);
3263 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003264 }
3265 }
3266 break;
thse5febef2007-04-01 18:31:35 +00003267#endif
bellard66fb9762003-03-23 01:06:05 +00003268 case TARGET_NR_rt_sigpending:
3269 {
3270 sigset_t set;
3271 ret = get_errno(sigpending(&set));
3272 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003273 p = lock_user(arg1, sizeof(target_sigset_t), 0);
3274 host_to_target_sigset(p, &set);
3275 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003276 }
3277 }
3278 break;
thse5febef2007-04-01 18:31:35 +00003279#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00003280 case TARGET_NR_sigsuspend:
3281 {
3282 sigset_t set;
pbrook53a59602006-03-25 19:31:22 +00003283 p = lock_user(arg1, sizeof(target_sigset_t), 1);
3284 target_to_host_old_sigset(&set, p);
3285 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003286 ret = get_errno(sigsuspend(&set));
3287 }
3288 break;
thse5febef2007-04-01 18:31:35 +00003289#endif
bellard66fb9762003-03-23 01:06:05 +00003290 case TARGET_NR_rt_sigsuspend:
3291 {
3292 sigset_t set;
pbrook53a59602006-03-25 19:31:22 +00003293 p = lock_user(arg1, sizeof(target_sigset_t), 1);
3294 target_to_host_sigset(&set, p);
3295 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003296 ret = get_errno(sigsuspend(&set));
3297 }
3298 break;
3299 case TARGET_NR_rt_sigtimedwait:
3300 {
bellard66fb9762003-03-23 01:06:05 +00003301 sigset_t set;
3302 struct timespec uts, *puts;
3303 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00003304
pbrook53a59602006-03-25 19:31:22 +00003305 p = lock_user(arg1, sizeof(target_sigset_t), 1);
3306 target_to_host_sigset(&set, p);
3307 unlock_user(p, arg1, 0);
3308 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00003309 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00003310 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00003311 } else {
3312 puts = NULL;
3313 }
3314 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00003315 if (!is_error(ret) && arg2) {
3316 p = lock_user(arg2, sizeof(target_sigset_t), 0);
3317 host_to_target_siginfo(p, &uinfo);
3318 unlock_user(p, arg2, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00003319 }
3320 }
3321 break;
3322 case TARGET_NR_rt_sigqueueinfo:
3323 {
3324 siginfo_t uinfo;
pbrook53a59602006-03-25 19:31:22 +00003325 p = lock_user(arg3, sizeof(target_sigset_t), 1);
3326 target_to_host_siginfo(&uinfo, p);
3327 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00003328 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
3329 }
3330 break;
thse5febef2007-04-01 18:31:35 +00003331#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00003332 case TARGET_NR_sigreturn:
3333 /* NOTE: ret is eax, so not transcoding must be done */
3334 ret = do_sigreturn(cpu_env);
3335 break;
thse5febef2007-04-01 18:31:35 +00003336#endif
bellard66fb9762003-03-23 01:06:05 +00003337 case TARGET_NR_rt_sigreturn:
3338 /* NOTE: ret is eax, so not transcoding must be done */
3339 ret = do_rt_sigreturn(cpu_env);
3340 break;
bellard31e31b82003-02-18 22:55:36 +00003341 case TARGET_NR_sethostname:
pbrook53a59602006-03-25 19:31:22 +00003342 p = lock_user_string(arg1);
3343 ret = get_errno(sethostname(p, arg2));
3344 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003345 break;
3346 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00003347 {
3348 /* XXX: convert resource ? */
3349 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003350 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003351 struct rlimit rlim;
pbrook53a59602006-03-25 19:31:22 +00003352 lock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00003353 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3354 rlim.rlim_max = tswapl(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00003355 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00003356 ret = get_errno(setrlimit(resource, &rlim));
3357 }
3358 break;
bellard31e31b82003-02-18 22:55:36 +00003359 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00003360 {
3361 /* XXX: convert resource ? */
3362 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00003363 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00003364 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00003365
bellard9de5e442003-03-23 16:49:39 +00003366 ret = get_errno(getrlimit(resource, &rlim));
3367 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003368 lock_user_struct(target_rlim, arg2, 0);
3369 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
3370 rlim.rlim_max = tswapl(target_rlim->rlim_max);
3371 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00003372 }
3373 }
3374 break;
bellard31e31b82003-02-18 22:55:36 +00003375 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00003376 {
3377 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00003378 ret = get_errno(getrusage(arg1, &rusage));
3379 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003380 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00003381 }
3382 }
3383 break;
bellard31e31b82003-02-18 22:55:36 +00003384 case TARGET_NR_gettimeofday:
3385 {
bellard31e31b82003-02-18 22:55:36 +00003386 struct timeval tv;
3387 ret = get_errno(gettimeofday(&tv, NULL));
3388 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003389 host_to_target_timeval(arg1, &tv);
bellard31e31b82003-02-18 22:55:36 +00003390 }
3391 }
3392 break;
3393 case TARGET_NR_settimeofday:
3394 {
bellard31e31b82003-02-18 22:55:36 +00003395 struct timeval tv;
pbrook53a59602006-03-25 19:31:22 +00003396 target_to_host_timeval(&tv, arg1);
bellard31e31b82003-02-18 22:55:36 +00003397 ret = get_errno(settimeofday(&tv, NULL));
3398 }
3399 break;
bellard048f6b42005-11-26 18:47:20 +00003400#ifdef TARGET_NR_select
bellard31e31b82003-02-18 22:55:36 +00003401 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00003402 {
pbrook53a59602006-03-25 19:31:22 +00003403 struct target_sel_arg_struct *sel;
3404 target_ulong inp, outp, exp, tvp;
3405 long nsel;
3406
3407 lock_user_struct(sel, arg1, 1);
3408 nsel = tswapl(sel->n);
3409 inp = tswapl(sel->inp);
3410 outp = tswapl(sel->outp);
3411 exp = tswapl(sel->exp);
3412 tvp = tswapl(sel->tvp);
3413 unlock_user_struct(sel, arg1, 0);
3414 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00003415 }
3416 break;
bellard048f6b42005-11-26 18:47:20 +00003417#endif
bellard31e31b82003-02-18 22:55:36 +00003418 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00003419 {
3420 void *p2;
3421 p = lock_user_string(arg1);
3422 p2 = lock_user_string(arg2);
3423 ret = get_errno(symlink(p, p2));
3424 unlock_user(p2, arg2, 0);
3425 unlock_user(p, arg1, 0);
3426 }
bellard31e31b82003-02-18 22:55:36 +00003427 break;
thsf0b62432007-09-24 09:25:40 +00003428#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
3429 case TARGET_NR_symlinkat:
3430 if (!arg1 || !arg3) {
3431 ret = -EFAULT;
3432 goto fail;
3433 }
3434 {
3435 void *p2 = NULL;
3436 p = lock_user_string(arg1);
3437 p2 = lock_user_string(arg3);
3438 if (!access_ok(VERIFY_READ, p, 1)
3439 || !access_ok(VERIFY_READ, p2, 1))
3440 ret = -EFAULT;
3441 else
3442 ret = get_errno(sys_symlinkat(p, arg2, p2));
3443 if (p2)
3444 unlock_user(p2, arg3, 0);
3445 if (p)
3446 unlock_user(p, arg1, 0);
3447 }
3448 break;
3449#endif
bellardebc05482003-09-30 21:08:41 +00003450#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00003451 case TARGET_NR_oldlstat:
3452 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003453#endif
bellard31e31b82003-02-18 22:55:36 +00003454 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00003455 {
3456 void *p2;
3457 p = lock_user_string(arg1);
3458 p2 = lock_user(arg2, arg3, 0);
3459 ret = get_errno(readlink(path(p), p2, arg3));
3460 unlock_user(p2, arg2, ret);
3461 unlock_user(p, arg1, 0);
3462 }
bellard31e31b82003-02-18 22:55:36 +00003463 break;
ths5e0ccb12007-09-24 09:26:10 +00003464#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
3465 case TARGET_NR_readlinkat:
3466 if (!arg2 || !arg3) {
3467 ret = -EFAULT;
3468 goto fail;
3469 }
3470 {
3471 void *p2 = NULL;
3472 p = lock_user_string(arg2);
3473 p2 = lock_user(arg3, arg4, 0);
3474 if (!access_ok(VERIFY_READ, p, 1)
3475 || !access_ok(VERIFY_READ, p2, 1))
3476 ret = -EFAULT;
3477 else
3478 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
3479 if (p2)
3480 unlock_user(p2, arg3, ret);
3481 if (p)
3482 unlock_user(p, arg2, 0);
3483 }
3484 break;
3485#endif
thse5febef2007-04-01 18:31:35 +00003486#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00003487 case TARGET_NR_uselib:
3488 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003489#endif
3490#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00003491 case TARGET_NR_swapon:
pbrook53a59602006-03-25 19:31:22 +00003492 p = lock_user_string(arg1);
3493 ret = get_errno(swapon(p, arg2));
3494 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003495 break;
thse5febef2007-04-01 18:31:35 +00003496#endif
bellard31e31b82003-02-18 22:55:36 +00003497 case TARGET_NR_reboot:
3498 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003499#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00003500 case TARGET_NR_readdir:
3501 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003502#endif
3503#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00003504 case TARGET_NR_mmap:
pbrooke6e59062006-10-22 00:18:54 +00003505#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_M68K)
bellard31e31b82003-02-18 22:55:36 +00003506 {
pbrook53a59602006-03-25 19:31:22 +00003507 target_ulong *v;
3508 target_ulong v1, v2, v3, v4, v5, v6;
3509 v = lock_user(arg1, 6 * sizeof(target_ulong), 1);
3510 v1 = tswapl(v[0]);
3511 v2 = tswapl(v[1]);
3512 v3 = tswapl(v[2]);
3513 v4 = tswapl(v[3]);
3514 v5 = tswapl(v[4]);
3515 v6 = tswapl(v[5]);
3516 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00003517 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00003518 target_to_host_bitmask(v4, mmap_flags_tbl),
3519 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00003520 }
bellard31e31b82003-02-18 22:55:36 +00003521#else
ths5fafdf22007-09-16 21:08:06 +00003522 ret = get_errno(target_mmap(arg1, arg2, arg3,
3523 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00003524 arg5,
3525 arg6));
bellard31e31b82003-02-18 22:55:36 +00003526#endif
bellard6fb883e2003-07-09 17:12:39 +00003527 break;
thse5febef2007-04-01 18:31:35 +00003528#endif
bellarda315a142005-01-30 22:59:18 +00003529#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00003530 case TARGET_NR_mmap2:
ths0d3267a2006-12-11 22:04:25 +00003531#if defined(TARGET_SPARC) || defined(TARGET_MIPS)
bellardc573ff62004-01-04 15:51:36 +00003532#define MMAP_SHIFT 12
3533#else
3534#define MMAP_SHIFT TARGET_PAGE_BITS
3535#endif
ths5fafdf22007-09-16 21:08:06 +00003536 ret = get_errno(target_mmap(arg1, arg2, arg3,
3537 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00003538 arg5,
bellardc573ff62004-01-04 15:51:36 +00003539 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00003540 break;
bellarda315a142005-01-30 22:59:18 +00003541#endif
bellard31e31b82003-02-18 22:55:36 +00003542 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00003543 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00003544 break;
bellard9de5e442003-03-23 16:49:39 +00003545 case TARGET_NR_mprotect:
bellard54936002003-05-13 00:25:15 +00003546 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00003547 break;
thse5febef2007-04-01 18:31:35 +00003548#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00003549 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00003550 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00003551 break;
thse5febef2007-04-01 18:31:35 +00003552#endif
pbrook53a59602006-03-25 19:31:22 +00003553 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00003554#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00003555 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00003556 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00003557 break;
thse5febef2007-04-01 18:31:35 +00003558#endif
3559#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00003560 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00003561 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00003562 break;
thse5febef2007-04-01 18:31:35 +00003563#endif
3564#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00003565 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00003566 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00003567 break;
thse5febef2007-04-01 18:31:35 +00003568#endif
3569#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00003570 case TARGET_NR_mlockall:
3571 ret = get_errno(mlockall(arg1));
3572 break;
thse5febef2007-04-01 18:31:35 +00003573#endif
3574#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00003575 case TARGET_NR_munlockall:
3576 ret = get_errno(munlockall());
3577 break;
thse5febef2007-04-01 18:31:35 +00003578#endif
bellard31e31b82003-02-18 22:55:36 +00003579 case TARGET_NR_truncate:
pbrook53a59602006-03-25 19:31:22 +00003580 p = lock_user_string(arg1);
3581 ret = get_errno(truncate(p, arg2));
3582 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003583 break;
3584 case TARGET_NR_ftruncate:
3585 ret = get_errno(ftruncate(arg1, arg2));
3586 break;
3587 case TARGET_NR_fchmod:
3588 ret = get_errno(fchmod(arg1, arg2));
3589 break;
ths814d7972007-09-24 09:26:51 +00003590#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
3591 case TARGET_NR_fchmodat:
3592 if (!arg2) {
3593 ret = -EFAULT;
3594 goto fail;
3595 }
3596 p = lock_user_string(arg2);
3597 if (!access_ok(VERIFY_READ, p, 1))
3598 ret = -EFAULT;
3599 else
3600 ret = get_errno(sys_fchmodat(arg1, p, arg3, arg4));
3601 if (p)
3602 unlock_user(p, arg2, 0);
3603 break;
3604#endif
bellard31e31b82003-02-18 22:55:36 +00003605 case TARGET_NR_getpriority:
3606 ret = get_errno(getpriority(arg1, arg2));
3607 break;
3608 case TARGET_NR_setpriority:
3609 ret = get_errno(setpriority(arg1, arg2, arg3));
3610 break;
bellardebc05482003-09-30 21:08:41 +00003611#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00003612 case TARGET_NR_profil:
3613 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003614#endif
bellard31e31b82003-02-18 22:55:36 +00003615 case TARGET_NR_statfs:
pbrook53a59602006-03-25 19:31:22 +00003616 p = lock_user_string(arg1);
3617 ret = get_errno(statfs(path(p), &stfs));
3618 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003619 convert_statfs:
3620 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003621 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00003622
pbrook53a59602006-03-25 19:31:22 +00003623 lock_user_struct(target_stfs, arg2, 0);
3624 /* ??? put_user is probably wrong. */
bellard56c8f682005-11-28 22:28:41 +00003625 put_user(stfs.f_type, &target_stfs->f_type);
3626 put_user(stfs.f_bsize, &target_stfs->f_bsize);
3627 put_user(stfs.f_blocks, &target_stfs->f_blocks);
3628 put_user(stfs.f_bfree, &target_stfs->f_bfree);
3629 put_user(stfs.f_bavail, &target_stfs->f_bavail);
3630 put_user(stfs.f_files, &target_stfs->f_files);
3631 put_user(stfs.f_ffree, &target_stfs->f_ffree);
ths4ce6f8d2007-07-20 15:54:27 +00003632 put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3633 put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
bellard56c8f682005-11-28 22:28:41 +00003634 put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00003635 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00003636 }
3637 break;
3638 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00003639 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00003640 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00003641#ifdef TARGET_NR_statfs64
3642 case TARGET_NR_statfs64:
pbrook53a59602006-03-25 19:31:22 +00003643 p = lock_user_string(arg1);
3644 ret = get_errno(statfs(path(p), &stfs));
3645 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00003646 convert_statfs64:
3647 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003648 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00003649
pbrook53a59602006-03-25 19:31:22 +00003650 lock_user_struct(target_stfs, arg3, 0);
3651 /* ??? put_user is probably wrong. */
bellard56c8f682005-11-28 22:28:41 +00003652 put_user(stfs.f_type, &target_stfs->f_type);
3653 put_user(stfs.f_bsize, &target_stfs->f_bsize);
3654 put_user(stfs.f_blocks, &target_stfs->f_blocks);
3655 put_user(stfs.f_bfree, &target_stfs->f_bfree);
3656 put_user(stfs.f_bavail, &target_stfs->f_bavail);
3657 put_user(stfs.f_files, &target_stfs->f_files);
3658 put_user(stfs.f_ffree, &target_stfs->f_ffree);
ths4ce6f8d2007-07-20 15:54:27 +00003659 put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
3660 put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
bellard56c8f682005-11-28 22:28:41 +00003661 put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00003662 unlock_user_struct(target_stfs, arg3, 0);
bellard56c8f682005-11-28 22:28:41 +00003663 }
3664 break;
3665 case TARGET_NR_fstatfs64:
3666 ret = get_errno(fstatfs(arg1, &stfs));
3667 goto convert_statfs64;
3668#endif
bellardebc05482003-09-30 21:08:41 +00003669#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00003670 case TARGET_NR_ioperm:
3671 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003672#endif
thse5febef2007-04-01 18:31:35 +00003673#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00003674 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00003675 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00003676 break;
thse5febef2007-04-01 18:31:35 +00003677#endif
bellard3532fa72006-06-24 15:06:03 +00003678#ifdef TARGET_NR_accept
3679 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00003680 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003681 break;
3682#endif
3683#ifdef TARGET_NR_bind
3684 case TARGET_NR_bind:
3685 ret = do_bind(arg1, arg2, arg3);
3686 break;
3687#endif
3688#ifdef TARGET_NR_connect
3689 case TARGET_NR_connect:
3690 ret = do_connect(arg1, arg2, arg3);
3691 break;
3692#endif
3693#ifdef TARGET_NR_getpeername
3694 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00003695 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003696 break;
3697#endif
3698#ifdef TARGET_NR_getsockname
3699 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00003700 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00003701 break;
3702#endif
3703#ifdef TARGET_NR_getsockopt
3704 case TARGET_NR_getsockopt:
3705 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
3706 break;
3707#endif
3708#ifdef TARGET_NR_listen
3709 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00003710 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00003711 break;
3712#endif
3713#ifdef TARGET_NR_recv
3714 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00003715 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00003716 break;
3717#endif
3718#ifdef TARGET_NR_recvfrom
3719 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00003720 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00003721 break;
3722#endif
3723#ifdef TARGET_NR_recvmsg
3724 case TARGET_NR_recvmsg:
3725 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
3726 break;
3727#endif
3728#ifdef TARGET_NR_send
3729 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00003730 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00003731 break;
3732#endif
3733#ifdef TARGET_NR_sendmsg
3734 case TARGET_NR_sendmsg:
3735 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
3736 break;
3737#endif
3738#ifdef TARGET_NR_sendto
3739 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00003740 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00003741 break;
3742#endif
3743#ifdef TARGET_NR_shutdown
3744 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00003745 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00003746 break;
3747#endif
3748#ifdef TARGET_NR_socket
3749 case TARGET_NR_socket:
3750 ret = do_socket(arg1, arg2, arg3);
3751 break;
3752#endif
3753#ifdef TARGET_NR_socketpair
3754 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00003755 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00003756 break;
3757#endif
3758#ifdef TARGET_NR_setsockopt
3759 case TARGET_NR_setsockopt:
3760 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
3761 break;
3762#endif
ths7494b0f2007-02-11 18:26:53 +00003763
bellard31e31b82003-02-18 22:55:36 +00003764 case TARGET_NR_syslog:
thse5574482007-02-11 20:03:13 +00003765 p = lock_user_string(arg2);
3766 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
3767 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00003768 break;
3769
bellard31e31b82003-02-18 22:55:36 +00003770 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00003771 {
bellard66fb9762003-03-23 01:06:05 +00003772 struct itimerval value, ovalue, *pvalue;
3773
pbrook53a59602006-03-25 19:31:22 +00003774 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00003775 pvalue = &value;
ths5fafdf22007-09-16 21:08:06 +00003776 target_to_host_timeval(&pvalue->it_interval,
pbrook53a59602006-03-25 19:31:22 +00003777 arg2);
ths5fafdf22007-09-16 21:08:06 +00003778 target_to_host_timeval(&pvalue->it_value,
pbrook53a59602006-03-25 19:31:22 +00003779 arg2 + sizeof(struct target_timeval));
bellard66fb9762003-03-23 01:06:05 +00003780 } else {
3781 pvalue = NULL;
3782 }
3783 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00003784 if (!is_error(ret) && arg3) {
3785 host_to_target_timeval(arg3,
bellard66fb9762003-03-23 01:06:05 +00003786 &ovalue.it_interval);
pbrook53a59602006-03-25 19:31:22 +00003787 host_to_target_timeval(arg3 + sizeof(struct target_timeval),
bellard66fb9762003-03-23 01:06:05 +00003788 &ovalue.it_value);
3789 }
3790 }
3791 break;
bellard31e31b82003-02-18 22:55:36 +00003792 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00003793 {
bellard66fb9762003-03-23 01:06:05 +00003794 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00003795
bellard66fb9762003-03-23 01:06:05 +00003796 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00003797 if (!is_error(ret) && arg2) {
3798 host_to_target_timeval(arg2,
bellard66fb9762003-03-23 01:06:05 +00003799 &value.it_interval);
pbrook53a59602006-03-25 19:31:22 +00003800 host_to_target_timeval(arg2 + sizeof(struct target_timeval),
bellard66fb9762003-03-23 01:06:05 +00003801 &value.it_value);
3802 }
3803 }
3804 break;
bellard31e31b82003-02-18 22:55:36 +00003805 case TARGET_NR_stat:
pbrook53a59602006-03-25 19:31:22 +00003806 p = lock_user_string(arg1);
3807 ret = get_errno(stat(path(p), &st));
3808 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003809 goto do_stat;
3810 case TARGET_NR_lstat:
pbrook53a59602006-03-25 19:31:22 +00003811 p = lock_user_string(arg1);
3812 ret = get_errno(lstat(path(p), &st));
3813 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003814 goto do_stat;
3815 case TARGET_NR_fstat:
3816 {
3817 ret = get_errno(fstat(arg1, &st));
3818 do_stat:
3819 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00003820 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00003821
pbrook53a59602006-03-25 19:31:22 +00003822 lock_user_struct(target_st, arg2, 0);
blueswir11b8dd642007-07-08 10:08:24 +00003823#if defined(TARGET_MIPS) || defined(TARGET_SPARC64)
thse3584652007-06-01 11:49:38 +00003824 target_st->st_dev = tswap32(st.st_dev);
3825#else
bellard31e31b82003-02-18 22:55:36 +00003826 target_st->st_dev = tswap16(st.st_dev);
thse3584652007-06-01 11:49:38 +00003827#endif
bellard31e31b82003-02-18 22:55:36 +00003828 target_st->st_ino = tswapl(st.st_ino);
ths7d600c82006-12-08 01:32:58 +00003829#if defined(TARGET_PPC) || defined(TARGET_MIPS)
bellard67867302003-11-23 17:05:30 +00003830 target_st->st_mode = tswapl(st.st_mode); /* XXX: check this */
3831 target_st->st_uid = tswap32(st.st_uid);
3832 target_st->st_gid = tswap32(st.st_gid);
blueswir11b8dd642007-07-08 10:08:24 +00003833#elif defined(TARGET_SPARC64)
3834 target_st->st_mode = tswap32(st.st_mode);
3835 target_st->st_uid = tswap32(st.st_uid);
3836 target_st->st_gid = tswap32(st.st_gid);
bellard67867302003-11-23 17:05:30 +00003837#else
bellardec86b0f2003-04-11 00:15:04 +00003838 target_st->st_mode = tswap16(st.st_mode);
bellard31e31b82003-02-18 22:55:36 +00003839 target_st->st_uid = tswap16(st.st_uid);
3840 target_st->st_gid = tswap16(st.st_gid);
bellard67867302003-11-23 17:05:30 +00003841#endif
thse3584652007-06-01 11:49:38 +00003842#if defined(TARGET_MIPS)
3843 /* If this is the same on PPC, then just merge w/ the above ifdef */
3844 target_st->st_nlink = tswapl(st.st_nlink);
3845 target_st->st_rdev = tswapl(st.st_rdev);
blueswir11b8dd642007-07-08 10:08:24 +00003846#elif defined(TARGET_SPARC64)
3847 target_st->st_nlink = tswap32(st.st_nlink);
3848 target_st->st_rdev = tswap32(st.st_rdev);
thse3584652007-06-01 11:49:38 +00003849#else
bellard67867302003-11-23 17:05:30 +00003850 target_st->st_nlink = tswap16(st.st_nlink);
bellard31e31b82003-02-18 22:55:36 +00003851 target_st->st_rdev = tswap16(st.st_rdev);
thse3584652007-06-01 11:49:38 +00003852#endif
bellard31e31b82003-02-18 22:55:36 +00003853 target_st->st_size = tswapl(st.st_size);
3854 target_st->st_blksize = tswapl(st.st_blksize);
3855 target_st->st_blocks = tswapl(st.st_blocks);
bellard7854b052003-03-29 17:22:23 +00003856 target_st->target_st_atime = tswapl(st.st_atime);
3857 target_st->target_st_mtime = tswapl(st.st_mtime);
3858 target_st->target_st_ctime = tswapl(st.st_ctime);
pbrook53a59602006-03-25 19:31:22 +00003859 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00003860 }
3861 }
3862 break;
bellardebc05482003-09-30 21:08:41 +00003863#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00003864 case TARGET_NR_olduname:
3865 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003866#endif
3867#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00003868 case TARGET_NR_iopl:
3869 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003870#endif
bellard31e31b82003-02-18 22:55:36 +00003871 case TARGET_NR_vhangup:
3872 ret = get_errno(vhangup());
3873 break;
bellardebc05482003-09-30 21:08:41 +00003874#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00003875 case TARGET_NR_idle:
3876 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00003877#endif
bellard42ad6ae2005-01-03 22:48:11 +00003878#ifdef TARGET_NR_syscall
3879 case TARGET_NR_syscall:
3880 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
3881 break;
3882#endif
bellard31e31b82003-02-18 22:55:36 +00003883 case TARGET_NR_wait4:
3884 {
3885 int status;
pbrook53a59602006-03-25 19:31:22 +00003886 target_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00003887 struct rusage rusage, *rusage_ptr;
pbrook53a59602006-03-25 19:31:22 +00003888 target_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00003889 if (target_rusage)
3890 rusage_ptr = &rusage;
3891 else
3892 rusage_ptr = NULL;
3893 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
3894 if (!is_error(ret)) {
3895 if (status_ptr)
pbrook53a59602006-03-25 19:31:22 +00003896 tputl(status_ptr, status);
bellard31e31b82003-02-18 22:55:36 +00003897 if (target_rusage) {
bellardb4091862003-05-16 15:39:34 +00003898 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00003899 }
3900 }
3901 }
3902 break;
thse5febef2007-04-01 18:31:35 +00003903#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00003904 case TARGET_NR_swapoff:
pbrook53a59602006-03-25 19:31:22 +00003905 p = lock_user_string(arg1);
3906 ret = get_errno(swapoff(p));
3907 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003908 break;
thse5febef2007-04-01 18:31:35 +00003909#endif
bellard31e31b82003-02-18 22:55:36 +00003910 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00003911 {
pbrook53a59602006-03-25 19:31:22 +00003912 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00003913 struct sysinfo value;
3914 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00003915 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00003916 {
pbrook53a59602006-03-25 19:31:22 +00003917 /* ??? __put_user is probably wrong. */
3918 lock_user_struct(target_value, arg1, 0);
bellarda5448a72004-06-19 16:59:03 +00003919 __put_user(value.uptime, &target_value->uptime);
3920 __put_user(value.loads[0], &target_value->loads[0]);
3921 __put_user(value.loads[1], &target_value->loads[1]);
3922 __put_user(value.loads[2], &target_value->loads[2]);
3923 __put_user(value.totalram, &target_value->totalram);
3924 __put_user(value.freeram, &target_value->freeram);
3925 __put_user(value.sharedram, &target_value->sharedram);
3926 __put_user(value.bufferram, &target_value->bufferram);
3927 __put_user(value.totalswap, &target_value->totalswap);
3928 __put_user(value.freeswap, &target_value->freeswap);
3929 __put_user(value.procs, &target_value->procs);
3930 __put_user(value.totalhigh, &target_value->totalhigh);
3931 __put_user(value.freehigh, &target_value->freehigh);
3932 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00003933 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00003934 }
3935 }
3936 break;
thse5febef2007-04-01 18:31:35 +00003937#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00003938 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00003939 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
3940 break;
thse5febef2007-04-01 18:31:35 +00003941#endif
bellard31e31b82003-02-18 22:55:36 +00003942 case TARGET_NR_fsync:
3943 ret = get_errno(fsync(arg1));
3944 break;
bellard31e31b82003-02-18 22:55:36 +00003945 case TARGET_NR_clone:
bellard1b6b0292003-03-22 17:31:38 +00003946 ret = get_errno(do_fork(cpu_env, arg1, arg2));
3947 break;
bellardec86b0f2003-04-11 00:15:04 +00003948#ifdef __NR_exit_group
3949 /* new thread calls */
3950 case TARGET_NR_exit_group:
bellarde9009672005-04-26 20:42:36 +00003951 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00003952 ret = get_errno(exit_group(arg1));
3953 break;
3954#endif
bellard31e31b82003-02-18 22:55:36 +00003955 case TARGET_NR_setdomainname:
pbrook53a59602006-03-25 19:31:22 +00003956 p = lock_user_string(arg1);
3957 ret = get_errno(setdomainname(p, arg2));
3958 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003959 break;
3960 case TARGET_NR_uname:
3961 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00003962 {
3963 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00003964
pbrook53a59602006-03-25 19:31:22 +00003965 lock_user_struct(buf, arg1, 0);
bellard29e619b2004-09-13 21:41:04 +00003966 ret = get_errno(sys_uname(buf));
3967 if (!is_error(ret)) {
3968 /* Overrite the native machine name with whatever is being
3969 emulated. */
3970 strcpy (buf->machine, UNAME_MACHINE);
pbrookc5937222006-05-14 11:30:38 +00003971 /* Allow the user to override the reported release. */
3972 if (qemu_uname_release && *qemu_uname_release)
3973 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00003974 }
pbrook53a59602006-03-25 19:31:22 +00003975 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00003976 }
bellard31e31b82003-02-18 22:55:36 +00003977 break;
bellard6dbad632003-03-16 18:05:05 +00003978#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00003979 case TARGET_NR_modify_ldt:
pbrook53a59602006-03-25 19:31:22 +00003980 ret = get_errno(do_modify_ldt(cpu_env, arg1, arg2, arg3));
bellard5cd43932003-03-29 16:54:36 +00003981 break;
j_mayer84409dd2007-04-06 08:56:50 +00003982#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00003983 case TARGET_NR_vm86old:
3984 goto unimplemented;
3985 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00003986 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00003987 break;
3988#endif
j_mayer84409dd2007-04-06 08:56:50 +00003989#endif
bellard31e31b82003-02-18 22:55:36 +00003990 case TARGET_NR_adjtimex:
3991 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00003992#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00003993 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00003994#endif
bellard31e31b82003-02-18 22:55:36 +00003995 case TARGET_NR_init_module:
3996 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00003997#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00003998 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00003999#endif
bellard31e31b82003-02-18 22:55:36 +00004000 goto unimplemented;
4001 case TARGET_NR_quotactl:
4002 goto unimplemented;
4003 case TARGET_NR_getpgid:
4004 ret = get_errno(getpgid(arg1));
4005 break;
4006 case TARGET_NR_fchdir:
4007 ret = get_errno(fchdir(arg1));
4008 break;
j_mayer84409dd2007-04-06 08:56:50 +00004009#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00004010 case TARGET_NR_bdflush:
4011 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00004012#endif
thse5febef2007-04-01 18:31:35 +00004013#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00004014 case TARGET_NR_sysfs:
4015 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004016#endif
bellard31e31b82003-02-18 22:55:36 +00004017 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00004018 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00004019 break;
thse5febef2007-04-01 18:31:35 +00004020#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00004021 case TARGET_NR_afs_syscall:
4022 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004023#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004024#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004025 case TARGET_NR__llseek:
4026 {
bellard4f2ac232004-04-26 19:44:02 +00004027#if defined (__x86_64__)
4028 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
pbrook53a59602006-03-25 19:31:22 +00004029 tput64(arg4, ret);
bellard4f2ac232004-04-26 19:44:02 +00004030#else
bellard31e31b82003-02-18 22:55:36 +00004031 int64_t res;
4032 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
pbrook53a59602006-03-25 19:31:22 +00004033 tput64(arg4, res);
bellard4f2ac232004-04-26 19:44:02 +00004034#endif
bellard31e31b82003-02-18 22:55:36 +00004035 }
4036 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004037#endif
bellard31e31b82003-02-18 22:55:36 +00004038 case TARGET_NR_getdents:
4039#if TARGET_LONG_SIZE != 4
pbrook53a59602006-03-25 19:31:22 +00004040 goto unimplemented;
bellarda315a142005-01-30 22:59:18 +00004041#warning not supported
bellard4add45b2003-06-05 01:52:59 +00004042#elif TARGET_LONG_SIZE == 4 && HOST_LONG_SIZE == 8
4043 {
pbrook53a59602006-03-25 19:31:22 +00004044 struct target_dirent *target_dirp;
bellard4add45b2003-06-05 01:52:59 +00004045 struct dirent *dirp;
j_mayer32407102007-09-26 23:01:49 +00004046 target_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00004047
4048 dirp = malloc(count);
4049 if (!dirp)
4050 return -ENOMEM;
ths3b46e622007-09-17 08:09:54 +00004051
bellard4add45b2003-06-05 01:52:59 +00004052 ret = get_errno(sys_getdents(arg1, dirp, count));
4053 if (!is_error(ret)) {
4054 struct dirent *de;
4055 struct target_dirent *tde;
4056 int len = ret;
4057 int reclen, treclen;
4058 int count1, tnamelen;
4059
4060 count1 = 0;
4061 de = dirp;
pbrook53a59602006-03-25 19:31:22 +00004062 target_dirp = lock_user(arg2, count, 0);
bellard4add45b2003-06-05 01:52:59 +00004063 tde = target_dirp;
4064 while (len > 0) {
4065 reclen = de->d_reclen;
4066 treclen = reclen - (2 * (sizeof(long) - sizeof(target_long)));
4067 tde->d_reclen = tswap16(treclen);
4068 tde->d_ino = tswapl(de->d_ino);
4069 tde->d_off = tswapl(de->d_off);
4070 tnamelen = treclen - (2 * sizeof(target_long) + 2);
4071 if (tnamelen > 256)
4072 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00004073 /* XXX: may not be correct */
bellard4add45b2003-06-05 01:52:59 +00004074 strncpy(tde->d_name, de->d_name, tnamelen);
4075 de = (struct dirent *)((char *)de + reclen);
4076 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00004077 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00004078 count1 += treclen;
4079 }
4080 ret = count1;
4081 }
pbrook53a59602006-03-25 19:31:22 +00004082 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00004083 free(dirp);
4084 }
4085#else
bellard31e31b82003-02-18 22:55:36 +00004086 {
pbrook53a59602006-03-25 19:31:22 +00004087 struct dirent *dirp;
j_mayer32407102007-09-26 23:01:49 +00004088 target_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00004089
pbrook53a59602006-03-25 19:31:22 +00004090 dirp = lock_user(arg2, count, 0);
bellard72f03902003-02-18 23:33:18 +00004091 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00004092 if (!is_error(ret)) {
4093 struct dirent *de;
4094 int len = ret;
4095 int reclen;
4096 de = dirp;
4097 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004098 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00004099 if (reclen > len)
4100 break;
bellard8083a3e2003-03-24 23:12:16 +00004101 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00004102 tswapls(&de->d_ino);
4103 tswapls(&de->d_off);
4104 de = (struct dirent *)((char *)de + reclen);
4105 len -= reclen;
4106 }
4107 }
pbrook53a59602006-03-25 19:31:22 +00004108 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00004109 }
bellard4add45b2003-06-05 01:52:59 +00004110#endif
bellard31e31b82003-02-18 22:55:36 +00004111 break;
ths3ae43202007-09-16 21:39:48 +00004112#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00004113 case TARGET_NR_getdents64:
4114 {
pbrook53a59602006-03-25 19:31:22 +00004115 struct dirent64 *dirp;
j_mayer32407102007-09-26 23:01:49 +00004116 target_long count = arg3;
pbrook53a59602006-03-25 19:31:22 +00004117 dirp = lock_user(arg2, count, 0);
bellarddab2ed92003-03-22 15:23:14 +00004118 ret = get_errno(sys_getdents64(arg1, dirp, count));
4119 if (!is_error(ret)) {
4120 struct dirent64 *de;
4121 int len = ret;
4122 int reclen;
4123 de = dirp;
4124 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00004125 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00004126 if (reclen > len)
4127 break;
bellard8083a3e2003-03-24 23:12:16 +00004128 de->d_reclen = tswap16(reclen);
bellarddab2ed92003-03-22 15:23:14 +00004129 tswap64s(&de->d_ino);
4130 tswap64s(&de->d_off);
4131 de = (struct dirent64 *)((char *)de + reclen);
4132 len -= reclen;
4133 }
4134 }
pbrook53a59602006-03-25 19:31:22 +00004135 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00004136 }
4137 break;
bellarda541f292004-04-12 20:39:29 +00004138#endif /* TARGET_NR_getdents64 */
thse5febef2007-04-01 18:31:35 +00004139#ifdef TARGET_NR__newselect
bellard31e31b82003-02-18 22:55:36 +00004140 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00004141 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00004142 break;
thse5febef2007-04-01 18:31:35 +00004143#endif
4144#ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00004145 case TARGET_NR_poll:
4146 {
pbrook53a59602006-03-25 19:31:22 +00004147 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00004148 unsigned int nfds = arg2;
4149 int timeout = arg3;
4150 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00004151 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00004152
pbrook53a59602006-03-25 19:31:22 +00004153 target_pfd = lock_user(arg1, sizeof(struct target_pollfd) * nfds, 1);
bellard9de5e442003-03-23 16:49:39 +00004154 pfd = alloca(sizeof(struct pollfd) * nfds);
4155 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004156 pfd[i].fd = tswap32(target_pfd[i].fd);
4157 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00004158 }
4159 ret = get_errno(poll(pfd, nfds, timeout));
4160 if (!is_error(ret)) {
4161 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00004162 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00004163 }
pbrook53a59602006-03-25 19:31:22 +00004164 ret += nfds * (sizeof(struct target_pollfd)
4165 - sizeof(struct pollfd));
bellard9de5e442003-03-23 16:49:39 +00004166 }
pbrook53a59602006-03-25 19:31:22 +00004167 unlock_user(target_pfd, arg1, ret);
bellard9de5e442003-03-23 16:49:39 +00004168 }
4169 break;
thse5febef2007-04-01 18:31:35 +00004170#endif
bellard31e31b82003-02-18 22:55:36 +00004171 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00004172 /* NOTE: the flock constant seems to be the same for every
4173 Linux platform */
4174 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00004175 break;
4176 case TARGET_NR_readv:
4177 {
4178 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00004179 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00004180
4181 vec = alloca(count * sizeof(struct iovec));
pbrook53a59602006-03-25 19:31:22 +00004182 lock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00004183 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00004184 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00004185 }
4186 break;
4187 case TARGET_NR_writev:
4188 {
4189 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00004190 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00004191
4192 vec = alloca(count * sizeof(struct iovec));
pbrook53a59602006-03-25 19:31:22 +00004193 lock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00004194 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00004195 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00004196 }
4197 break;
4198 case TARGET_NR_getsid:
4199 ret = get_errno(getsid(arg1));
4200 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004201#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00004202 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00004203 ret = get_errno(fdatasync(arg1));
4204 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004205#endif
bellard31e31b82003-02-18 22:55:36 +00004206 case TARGET_NR__sysctl:
bellard29e619b2004-09-13 21:41:04 +00004207 /* We don't implement this, but ENODIR is always a safe
4208 return value. */
4209 return -ENOTDIR;
bellard31e31b82003-02-18 22:55:36 +00004210 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00004211 {
pbrook53a59602006-03-25 19:31:22 +00004212 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004213 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00004214
4215 lock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00004216 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004217 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00004218 ret = get_errno(sched_setparam(arg1, &schp));
4219 }
4220 break;
bellard31e31b82003-02-18 22:55:36 +00004221 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00004222 {
pbrook53a59602006-03-25 19:31:22 +00004223 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004224 struct sched_param schp;
4225 ret = get_errno(sched_getparam(arg1, &schp));
4226 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004227 lock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00004228 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004229 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00004230 }
4231 }
4232 break;
bellard31e31b82003-02-18 22:55:36 +00004233 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00004234 {
pbrook53a59602006-03-25 19:31:22 +00004235 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00004236 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00004237 lock_user_struct(target_schp, arg3, 1);
bellard5cd43932003-03-29 16:54:36 +00004238 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00004239 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00004240 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
4241 }
4242 break;
bellard31e31b82003-02-18 22:55:36 +00004243 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00004244 ret = get_errno(sched_getscheduler(arg1));
4245 break;
bellard31e31b82003-02-18 22:55:36 +00004246 case TARGET_NR_sched_yield:
4247 ret = get_errno(sched_yield());
4248 break;
4249 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00004250 ret = get_errno(sched_get_priority_max(arg1));
4251 break;
bellard31e31b82003-02-18 22:55:36 +00004252 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00004253 ret = get_errno(sched_get_priority_min(arg1));
4254 break;
bellard31e31b82003-02-18 22:55:36 +00004255 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00004256 {
bellard5cd43932003-03-29 16:54:36 +00004257 struct timespec ts;
4258 ret = get_errno(sched_rr_get_interval(arg1, &ts));
4259 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004260 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00004261 }
4262 }
4263 break;
bellard31e31b82003-02-18 22:55:36 +00004264 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00004265 {
bellard1b6b0292003-03-22 17:31:38 +00004266 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00004267 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00004268 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00004269 if (is_error(ret) && arg2) {
4270 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00004271 }
4272 }
4273 break;
thse5febef2007-04-01 18:31:35 +00004274#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00004275 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00004276 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004277#endif
4278#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00004279 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00004280 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004281#endif
bellard31e31b82003-02-18 22:55:36 +00004282 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00004283 switch (arg1)
4284 {
4285 case PR_GET_PDEATHSIG:
4286 {
4287 int deathsig;
4288 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
4289 if (!is_error(ret) && arg2)
4290 tput32(arg2, deathsig);
4291 }
4292 break;
4293 default:
4294 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
4295 break;
4296 }
ths39b9aae2007-02-11 18:36:44 +00004297 break;
bellard67867302003-11-23 17:05:30 +00004298#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00004299 case TARGET_NR_pread:
pbrook53a59602006-03-25 19:31:22 +00004300 page_unprotect_range(arg2, arg3);
4301 p = lock_user(arg2, arg3, 0);
4302 ret = get_errno(pread(arg1, p, arg3, arg4));
4303 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00004304 break;
bellard31e31b82003-02-18 22:55:36 +00004305 case TARGET_NR_pwrite:
pbrook53a59602006-03-25 19:31:22 +00004306 p = lock_user(arg2, arg3, 1);
4307 ret = get_errno(pwrite(arg1, p, arg3, arg4));
4308 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00004309 break;
bellard67867302003-11-23 17:05:30 +00004310#endif
bellard31e31b82003-02-18 22:55:36 +00004311 case TARGET_NR_getcwd:
pbrook53a59602006-03-25 19:31:22 +00004312 p = lock_user(arg1, arg2, 0);
4313 ret = get_errno(sys_getcwd1(p, arg2));
4314 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00004315 break;
4316 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00004317 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004318 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00004319 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00004320 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00004321#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
4322 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
thsa04e1342007-09-27 13:57:58 +00004323 ret = do_sigaltstack((struct target_sigaltstack *)arg1,
4324 (struct target_sigaltstack *)arg2,
4325 get_sp_from_cpustate((CPUState *)cpu_env));
4326 break;
4327#else
bellard5cd43932003-03-29 16:54:36 +00004328 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00004329#endif
bellard31e31b82003-02-18 22:55:36 +00004330 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00004331 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004332#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00004333 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00004334 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004335#endif
4336#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00004337 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00004338 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004339#endif
bellard048f6b42005-11-26 18:47:20 +00004340#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00004341 case TARGET_NR_vfork:
bellard1b6b0292003-03-22 17:31:38 +00004342 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD, 0));
bellard31e31b82003-02-18 22:55:36 +00004343 break;
bellard048f6b42005-11-26 18:47:20 +00004344#endif
bellardebc05482003-09-30 21:08:41 +00004345#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00004346 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00004347 {
4348 struct rlimit rlim;
4349 ret = get_errno(getrlimit(arg1, &rlim));
4350 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004351 struct target_rlimit *target_rlim;
4352 lock_user_struct(target_rlim, arg2, 0);
bellard728584b2003-04-29 20:43:36 +00004353 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
4354 target_rlim->rlim_max = tswapl(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00004355 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00004356 }
4357 break;
4358 }
bellardebc05482003-09-30 21:08:41 +00004359#endif
bellarda315a142005-01-30 22:59:18 +00004360#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00004361 case TARGET_NR_truncate64:
pbrook53a59602006-03-25 19:31:22 +00004362 p = lock_user_string(arg1);
4363 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
4364 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00004365 break;
bellarda315a142005-01-30 22:59:18 +00004366#endif
4367#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00004368 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00004369 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00004370 break;
bellarda315a142005-01-30 22:59:18 +00004371#endif
4372#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00004373 case TARGET_NR_stat64:
pbrook53a59602006-03-25 19:31:22 +00004374 p = lock_user_string(arg1);
4375 ret = get_errno(stat(path(p), &st));
4376 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004377 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004378#endif
4379#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00004380 case TARGET_NR_lstat64:
pbrook53a59602006-03-25 19:31:22 +00004381 p = lock_user_string(arg1);
4382 ret = get_errno(lstat(path(p), &st));
4383 unlock_user(p, arg1, 0);
bellard60cd49d2003-03-16 22:53:56 +00004384 goto do_stat64;
bellarda315a142005-01-30 22:59:18 +00004385#endif
4386#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00004387 case TARGET_NR_fstat64:
bellard60cd49d2003-03-16 22:53:56 +00004388 {
4389 ret = get_errno(fstat(arg1, &st));
4390 do_stat64:
4391 if (!is_error(ret)) {
pbrookce4defa2006-02-09 16:49:55 +00004392#ifdef TARGET_ARM
4393 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004394 struct target_eabi_stat64 *target_st;
4395 lock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00004396 memset(target_st, 0, sizeof(struct target_eabi_stat64));
pbrook53a59602006-03-25 19:31:22 +00004397 /* put_user is probably wrong. */
pbrookce4defa2006-02-09 16:49:55 +00004398 put_user(st.st_dev, &target_st->st_dev);
4399 put_user(st.st_ino, &target_st->st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004400#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
pbrookce4defa2006-02-09 16:49:55 +00004401 put_user(st.st_ino, &target_st->__st_ino);
bellardec86b0f2003-04-11 00:15:04 +00004402#endif
pbrookce4defa2006-02-09 16:49:55 +00004403 put_user(st.st_mode, &target_st->st_mode);
4404 put_user(st.st_nlink, &target_st->st_nlink);
4405 put_user(st.st_uid, &target_st->st_uid);
4406 put_user(st.st_gid, &target_st->st_gid);
4407 put_user(st.st_rdev, &target_st->st_rdev);
4408 /* XXX: better use of kernel struct */
4409 put_user(st.st_size, &target_st->st_size);
4410 put_user(st.st_blksize, &target_st->st_blksize);
4411 put_user(st.st_blocks, &target_st->st_blocks);
4412 put_user(st.st_atime, &target_st->target_st_atime);
4413 put_user(st.st_mtime, &target_st->target_st_mtime);
4414 put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00004415 unlock_user_struct(target_st, arg2, 0);
pbrookce4defa2006-02-09 16:49:55 +00004416 } else
4417#endif
4418 {
pbrook53a59602006-03-25 19:31:22 +00004419 struct target_stat64 *target_st;
4420 lock_user_struct(target_st, arg2, 1);
pbrookce4defa2006-02-09 16:49:55 +00004421 memset(target_st, 0, sizeof(struct target_stat64));
pbrook53a59602006-03-25 19:31:22 +00004422 /* ??? put_user is probably wrong. */
pbrookce4defa2006-02-09 16:49:55 +00004423 put_user(st.st_dev, &target_st->st_dev);
4424 put_user(st.st_ino, &target_st->st_ino);
4425#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
4426 put_user(st.st_ino, &target_st->__st_ino);
4427#endif
4428 put_user(st.st_mode, &target_st->st_mode);
4429 put_user(st.st_nlink, &target_st->st_nlink);
4430 put_user(st.st_uid, &target_st->st_uid);
4431 put_user(st.st_gid, &target_st->st_gid);
4432 put_user(st.st_rdev, &target_st->st_rdev);
4433 /* XXX: better use of kernel struct */
4434 put_user(st.st_size, &target_st->st_size);
4435 put_user(st.st_blksize, &target_st->st_blksize);
4436 put_user(st.st_blocks, &target_st->st_blocks);
4437 put_user(st.st_atime, &target_st->target_st_atime);
4438 put_user(st.st_mtime, &target_st->target_st_mtime);
4439 put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00004440 unlock_user_struct(target_st, arg2, 0);
pbrookce4defa2006-02-09 16:49:55 +00004441 }
bellard60cd49d2003-03-16 22:53:56 +00004442 }
4443 }
4444 break;
bellarda315a142005-01-30 22:59:18 +00004445#endif
bellard67867302003-11-23 17:05:30 +00004446#ifdef USE_UID16
4447 case TARGET_NR_lchown:
pbrook53a59602006-03-25 19:31:22 +00004448 p = lock_user_string(arg1);
4449 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
4450 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00004451 break;
4452 case TARGET_NR_getuid:
4453 ret = get_errno(high2lowuid(getuid()));
4454 break;
4455 case TARGET_NR_getgid:
4456 ret = get_errno(high2lowgid(getgid()));
4457 break;
4458 case TARGET_NR_geteuid:
4459 ret = get_errno(high2lowuid(geteuid()));
4460 break;
4461 case TARGET_NR_getegid:
4462 ret = get_errno(high2lowgid(getegid()));
4463 break;
4464 case TARGET_NR_setreuid:
4465 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
4466 break;
4467 case TARGET_NR_setregid:
4468 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
4469 break;
4470 case TARGET_NR_getgroups:
4471 {
4472 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004473 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00004474 gid_t *grouplist;
4475 int i;
4476
4477 grouplist = alloca(gidsetsize * sizeof(gid_t));
4478 ret = get_errno(getgroups(gidsetsize, grouplist));
4479 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004480 target_grouplist = lock_user(arg2, gidsetsize * 2, 0);
bellard67867302003-11-23 17:05:30 +00004481 for(i = 0;i < gidsetsize; i++)
4482 target_grouplist[i] = tswap16(grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00004483 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00004484 }
4485 }
4486 break;
4487 case TARGET_NR_setgroups:
4488 {
4489 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004490 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00004491 gid_t *grouplist;
4492 int i;
4493
4494 grouplist = alloca(gidsetsize * sizeof(gid_t));
pbrook53a59602006-03-25 19:31:22 +00004495 target_grouplist = lock_user(arg2, gidsetsize * 2, 1);
bellard67867302003-11-23 17:05:30 +00004496 for(i = 0;i < gidsetsize; i++)
4497 grouplist[i] = tswap16(target_grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00004498 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00004499 ret = get_errno(setgroups(gidsetsize, grouplist));
4500 }
4501 break;
4502 case TARGET_NR_fchown:
4503 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
4504 break;
thsccfa72b2007-09-24 09:23:34 +00004505#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
4506 case TARGET_NR_fchownat:
4507 if (!arg2) {
4508 ret = -EFAULT;
4509 goto fail;
4510 }
4511 p = lock_user_string(arg2);
4512 if (!access_ok(VERIFY_READ, p, 1))
4513 ret = -EFAULT;
4514 else
4515 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
4516 if (p)
4517 unlock_user(p, arg2, 0);
4518 break;
4519#endif
bellard67867302003-11-23 17:05:30 +00004520#ifdef TARGET_NR_setresuid
4521 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00004522 ret = get_errno(setresuid(low2highuid(arg1),
4523 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00004524 low2highuid(arg3)));
4525 break;
4526#endif
4527#ifdef TARGET_NR_getresuid
4528 case TARGET_NR_getresuid:
4529 {
pbrook53a59602006-03-25 19:31:22 +00004530 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00004531 ret = get_errno(getresuid(&ruid, &euid, &suid));
4532 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004533 tput16(arg1, tswap16(high2lowuid(ruid)));
4534 tput16(arg2, tswap16(high2lowuid(euid)));
4535 tput16(arg3, tswap16(high2lowuid(suid)));
bellard67867302003-11-23 17:05:30 +00004536 }
4537 }
4538 break;
4539#endif
4540#ifdef TARGET_NR_getresgid
4541 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00004542 ret = get_errno(setresgid(low2highgid(arg1),
4543 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00004544 low2highgid(arg3)));
4545 break;
4546#endif
4547#ifdef TARGET_NR_getresgid
4548 case TARGET_NR_getresgid:
4549 {
pbrook53a59602006-03-25 19:31:22 +00004550 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00004551 ret = get_errno(getresgid(&rgid, &egid, &sgid));
4552 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004553 tput16(arg1, tswap16(high2lowgid(rgid)));
4554 tput16(arg2, tswap16(high2lowgid(egid)));
4555 tput16(arg3, tswap16(high2lowgid(sgid)));
bellard67867302003-11-23 17:05:30 +00004556 }
4557 }
4558 break;
4559#endif
4560 case TARGET_NR_chown:
pbrook53a59602006-03-25 19:31:22 +00004561 p = lock_user_string(arg1);
4562 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
4563 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00004564 break;
4565 case TARGET_NR_setuid:
4566 ret = get_errno(setuid(low2highuid(arg1)));
4567 break;
4568 case TARGET_NR_setgid:
4569 ret = get_errno(setgid(low2highgid(arg1)));
4570 break;
4571 case TARGET_NR_setfsuid:
4572 ret = get_errno(setfsuid(arg1));
4573 break;
4574 case TARGET_NR_setfsgid:
4575 ret = get_errno(setfsgid(arg1));
4576 break;
4577#endif /* USE_UID16 */
4578
bellarda315a142005-01-30 22:59:18 +00004579#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00004580 case TARGET_NR_lchown32:
pbrook53a59602006-03-25 19:31:22 +00004581 p = lock_user_string(arg1);
4582 ret = get_errno(lchown(p, arg2, arg3));
4583 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00004584 break;
bellarda315a142005-01-30 22:59:18 +00004585#endif
4586#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00004587 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00004588 ret = get_errno(getuid());
4589 break;
bellarda315a142005-01-30 22:59:18 +00004590#endif
4591#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00004592 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00004593 ret = get_errno(getgid());
4594 break;
bellarda315a142005-01-30 22:59:18 +00004595#endif
4596#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00004597 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00004598 ret = get_errno(geteuid());
4599 break;
bellarda315a142005-01-30 22:59:18 +00004600#endif
4601#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00004602 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00004603 ret = get_errno(getegid());
4604 break;
bellarda315a142005-01-30 22:59:18 +00004605#endif
4606#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00004607 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00004608 ret = get_errno(setreuid(arg1, arg2));
4609 break;
bellarda315a142005-01-30 22:59:18 +00004610#endif
4611#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00004612 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00004613 ret = get_errno(setregid(arg1, arg2));
4614 break;
bellarda315a142005-01-30 22:59:18 +00004615#endif
4616#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00004617 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00004618 {
4619 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004620 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00004621 gid_t *grouplist;
4622 int i;
4623
4624 grouplist = alloca(gidsetsize * sizeof(gid_t));
4625 ret = get_errno(getgroups(gidsetsize, grouplist));
4626 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004627 target_grouplist = lock_user(arg2, gidsetsize * 4, 0);
bellard99c475a2005-01-31 20:45:13 +00004628 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00004629 target_grouplist[i] = tswap32(grouplist[i]);
4630 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00004631 }
4632 }
4633 break;
bellarda315a142005-01-30 22:59:18 +00004634#endif
4635#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00004636 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00004637 {
4638 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00004639 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00004640 gid_t *grouplist;
4641 int i;
ths3b46e622007-09-17 08:09:54 +00004642
bellard99c475a2005-01-31 20:45:13 +00004643 grouplist = alloca(gidsetsize * sizeof(gid_t));
pbrook53a59602006-03-25 19:31:22 +00004644 target_grouplist = lock_user(arg2, gidsetsize * 4, 1);
bellard99c475a2005-01-31 20:45:13 +00004645 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00004646 grouplist[i] = tswap32(target_grouplist[i]);
4647 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00004648 ret = get_errno(setgroups(gidsetsize, grouplist));
4649 }
4650 break;
bellarda315a142005-01-30 22:59:18 +00004651#endif
4652#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00004653 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00004654 ret = get_errno(fchown(arg1, arg2, arg3));
4655 break;
bellarda315a142005-01-30 22:59:18 +00004656#endif
4657#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00004658 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00004659 ret = get_errno(setresuid(arg1, arg2, arg3));
4660 break;
bellarda315a142005-01-30 22:59:18 +00004661#endif
4662#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00004663 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00004664 {
pbrook53a59602006-03-25 19:31:22 +00004665 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00004666 ret = get_errno(getresuid(&ruid, &euid, &suid));
4667 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004668 tput32(arg1, tswap32(ruid));
4669 tput32(arg2, tswap32(euid));
4670 tput32(arg3, tswap32(suid));
bellardb03c60f2003-03-23 17:19:56 +00004671 }
4672 }
4673 break;
bellarda315a142005-01-30 22:59:18 +00004674#endif
4675#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00004676 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00004677 ret = get_errno(setresgid(arg1, arg2, arg3));
4678 break;
bellarda315a142005-01-30 22:59:18 +00004679#endif
4680#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00004681 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00004682 {
pbrook53a59602006-03-25 19:31:22 +00004683 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00004684 ret = get_errno(getresgid(&rgid, &egid, &sgid));
4685 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004686 tput32(arg1, tswap32(rgid));
4687 tput32(arg2, tswap32(egid));
4688 tput32(arg3, tswap32(sgid));
bellardb03c60f2003-03-23 17:19:56 +00004689 }
4690 }
4691 break;
bellarda315a142005-01-30 22:59:18 +00004692#endif
4693#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00004694 case TARGET_NR_chown32:
pbrook53a59602006-03-25 19:31:22 +00004695 p = lock_user_string(arg1);
4696 ret = get_errno(chown(p, arg2, arg3));
4697 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00004698 break;
bellarda315a142005-01-30 22:59:18 +00004699#endif
4700#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00004701 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00004702 ret = get_errno(setuid(arg1));
4703 break;
bellarda315a142005-01-30 22:59:18 +00004704#endif
4705#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00004706 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00004707 ret = get_errno(setgid(arg1));
4708 break;
bellarda315a142005-01-30 22:59:18 +00004709#endif
4710#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00004711 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00004712 ret = get_errno(setfsuid(arg1));
4713 break;
bellarda315a142005-01-30 22:59:18 +00004714#endif
4715#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00004716 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00004717 ret = get_errno(setfsgid(arg1));
4718 break;
bellarda315a142005-01-30 22:59:18 +00004719#endif
bellard67867302003-11-23 17:05:30 +00004720
bellard31e31b82003-02-18 22:55:36 +00004721 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00004722 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00004723#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00004724 case TARGET_NR_mincore:
bellardb03c60f2003-03-23 17:19:56 +00004725 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00004726#endif
4727#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00004728 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00004729 /* A straight passthrough may not be safe because qemu sometimes
4730 turns private flie-backed mappings into anonymous mappings.
4731 This will break MADV_DONTNEED.
4732 This is a hint, so ignoring and returning success is ok. */
4733 ret = get_errno(0);
4734 break;
bellardffa65c32004-01-04 23:57:22 +00004735#endif
bellard60cd49d2003-03-16 22:53:56 +00004736#if TARGET_LONG_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00004737 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00004738 {
thsb1e341e2007-03-20 21:50:52 +00004739 int cmd;
bellard77e46722003-04-29 20:39:06 +00004740 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00004741 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00004742#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00004743 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00004744#endif
bellard77e46722003-04-29 20:39:06 +00004745
thsb1e341e2007-03-20 21:50:52 +00004746 switch(arg2){
4747 case TARGET_F_GETLK64:
4748 cmd = F_GETLK64;
thsa7222582007-03-22 15:08:55 +00004749 break;
thsb1e341e2007-03-20 21:50:52 +00004750 case TARGET_F_SETLK64:
4751 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00004752 break;
thsb1e341e2007-03-20 21:50:52 +00004753 case TARGET_F_SETLKW64:
4754 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00004755 break;
thsb1e341e2007-03-20 21:50:52 +00004756 default:
4757 cmd = arg2;
thsa7222582007-03-22 15:08:55 +00004758 break;
thsb1e341e2007-03-20 21:50:52 +00004759 }
4760
bellard60cd49d2003-03-16 22:53:56 +00004761 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00004762 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00004763#ifdef TARGET_ARM
4764 if (((CPUARMState *)cpu_env)->eabi) {
4765 lock_user_struct(target_efl, arg3, 1);
4766 fl.l_type = tswap16(target_efl->l_type);
4767 fl.l_whence = tswap16(target_efl->l_whence);
4768 fl.l_start = tswap64(target_efl->l_start);
4769 fl.l_len = tswap64(target_efl->l_len);
4770 fl.l_pid = tswapl(target_efl->l_pid);
4771 unlock_user_struct(target_efl, arg3, 0);
4772 } else
4773#endif
4774 {
4775 lock_user_struct(target_fl, arg3, 1);
4776 fl.l_type = tswap16(target_fl->l_type);
4777 fl.l_whence = tswap16(target_fl->l_whence);
4778 fl.l_start = tswap64(target_fl->l_start);
4779 fl.l_len = tswap64(target_fl->l_len);
4780 fl.l_pid = tswapl(target_fl->l_pid);
4781 unlock_user_struct(target_fl, arg3, 0);
4782 }
thsb1e341e2007-03-20 21:50:52 +00004783 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00004784 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00004785#ifdef TARGET_ARM
4786 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004787 lock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004788 target_efl->l_type = tswap16(fl.l_type);
4789 target_efl->l_whence = tswap16(fl.l_whence);
4790 target_efl->l_start = tswap64(fl.l_start);
4791 target_efl->l_len = tswap64(fl.l_len);
4792 target_efl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004793 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004794 } else
4795#endif
4796 {
pbrook53a59602006-03-25 19:31:22 +00004797 lock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004798 target_fl->l_type = tswap16(fl.l_type);
4799 target_fl->l_whence = tswap16(fl.l_whence);
4800 target_fl->l_start = tswap64(fl.l_start);
4801 target_fl->l_len = tswap64(fl.l_len);
4802 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00004803 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004804 }
bellard77e46722003-04-29 20:39:06 +00004805 }
4806 break;
4807
thsb1e341e2007-03-20 21:50:52 +00004808 case TARGET_F_SETLK64:
4809 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00004810#ifdef TARGET_ARM
4811 if (((CPUARMState *)cpu_env)->eabi) {
pbrook53a59602006-03-25 19:31:22 +00004812 lock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004813 fl.l_type = tswap16(target_efl->l_type);
4814 fl.l_whence = tswap16(target_efl->l_whence);
4815 fl.l_start = tswap64(target_efl->l_start);
4816 fl.l_len = tswap64(target_efl->l_len);
4817 fl.l_pid = tswapl(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004818 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004819 } else
4820#endif
4821 {
pbrook53a59602006-03-25 19:31:22 +00004822 lock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00004823 fl.l_type = tswap16(target_fl->l_type);
4824 fl.l_whence = tswap16(target_fl->l_whence);
4825 fl.l_start = tswap64(target_fl->l_start);
4826 fl.l_len = tswap64(target_fl->l_len);
4827 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00004828 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00004829 }
thsb1e341e2007-03-20 21:50:52 +00004830 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00004831 break;
bellard60cd49d2003-03-16 22:53:56 +00004832 default:
thsb1e341e2007-03-20 21:50:52 +00004833 ret = get_errno(do_fcntl(arg1, cmd, arg3));
bellard60cd49d2003-03-16 22:53:56 +00004834 break;
4835 }
bellard77e46722003-04-29 20:39:06 +00004836 break;
4837 }
bellard60cd49d2003-03-16 22:53:56 +00004838#endif
ths7d600c82006-12-08 01:32:58 +00004839#ifdef TARGET_NR_cacheflush
4840 case TARGET_NR_cacheflush:
4841 /* self-modifying code is handled automatically, so nothing needed */
4842 ret = 0;
4843 break;
4844#endif
bellardebc05482003-09-30 21:08:41 +00004845#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00004846 case TARGET_NR_security:
4847 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004848#endif
bellardc573ff62004-01-04 15:51:36 +00004849#ifdef TARGET_NR_getpagesize
4850 case TARGET_NR_getpagesize:
4851 ret = TARGET_PAGE_SIZE;
4852 break;
4853#endif
bellard31e31b82003-02-18 22:55:36 +00004854 case TARGET_NR_gettid:
4855 ret = get_errno(gettid());
4856 break;
thse5febef2007-04-01 18:31:35 +00004857#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00004858 case TARGET_NR_readahead:
bellard5cd43932003-03-29 16:54:36 +00004859 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004860#endif
bellardebc05482003-09-30 21:08:41 +00004861#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00004862 case TARGET_NR_setxattr:
4863 case TARGET_NR_lsetxattr:
4864 case TARGET_NR_fsetxattr:
4865 case TARGET_NR_getxattr:
4866 case TARGET_NR_lgetxattr:
4867 case TARGET_NR_fgetxattr:
4868 case TARGET_NR_listxattr:
4869 case TARGET_NR_llistxattr:
4870 case TARGET_NR_flistxattr:
4871 case TARGET_NR_removexattr:
4872 case TARGET_NR_lremovexattr:
4873 case TARGET_NR_fremovexattr:
bellard5cd43932003-03-29 16:54:36 +00004874 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00004875#endif
4876#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00004877 case TARGET_NR_set_thread_area:
ths6f5b89a2007-03-02 20:48:00 +00004878#ifdef TARGET_MIPS
4879 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
4880 ret = 0;
4881 break;
4882#else
4883 goto unimplemented_nowarn;
4884#endif
4885#endif
4886#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00004887 case TARGET_NR_get_thread_area:
4888 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00004889#endif
bellard48dc41e2006-06-21 18:15:50 +00004890#ifdef TARGET_NR_getdomainname
4891 case TARGET_NR_getdomainname:
4892 goto unimplemented_nowarn;
4893#endif
ths6f5b89a2007-03-02 20:48:00 +00004894
thsb5906f92007-03-19 13:32:45 +00004895#ifdef TARGET_NR_clock_gettime
4896 case TARGET_NR_clock_gettime:
4897 {
4898 struct timespec ts;
4899 ret = get_errno(clock_gettime(arg1, &ts));
4900 if (!is_error(ret)) {
4901 host_to_target_timespec(arg2, &ts);
4902 }
4903 break;
4904 }
4905#endif
4906#ifdef TARGET_NR_clock_getres
4907 case TARGET_NR_clock_getres:
4908 {
4909 struct timespec ts;
4910 ret = get_errno(clock_getres(arg1, &ts));
4911 if (!is_error(ret)) {
4912 host_to_target_timespec(arg2, &ts);
4913 }
4914 break;
4915 }
4916#endif
4917
ths6f5b89a2007-03-02 20:48:00 +00004918#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
4919 case TARGET_NR_set_tid_address:
4920 ret = get_errno(set_tid_address((int *) arg1));
4921 break;
4922#endif
4923
ths3ae43202007-09-16 21:39:48 +00004924#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00004925 case TARGET_NR_tkill:
4926 ret = get_errno(sys_tkill((int)arg1, (int)arg2));
4927 break;
4928#endif
4929
ths3ae43202007-09-16 21:39:48 +00004930#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00004931 case TARGET_NR_tgkill:
4932 ret = get_errno(sys_tgkill((int)arg1, (int)arg2, (int)arg3));
4933 break;
4934#endif
4935
ths4f2b1fe2007-06-21 21:57:12 +00004936#ifdef TARGET_NR_set_robust_list
4937 case TARGET_NR_set_robust_list:
4938 goto unimplemented_nowarn;
4939#endif
4940
ths9007f0e2007-09-25 17:50:37 +00004941#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
4942 case TARGET_NR_utimensat:
4943 {
4944 struct timespec ts[2];
4945 target_to_host_timespec(ts, arg3);
4946 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
4947 if (!arg2)
4948 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
4949 else {
4950 p = lock_user_string(arg2);
4951 if (!access_ok(VERIFY_READ, p, 1))
4952 ret = -EFAULT;
4953 else
4954 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
4955 if (p)
4956 unlock_user(p, arg2, 0);
4957 }
4958 }
4959 break;
4960#endif
4961
bellard31e31b82003-02-18 22:55:36 +00004962 default:
4963 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00004964 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00004965#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 +00004966 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00004967#endif
bellard31e31b82003-02-18 22:55:36 +00004968 ret = -ENOSYS;
4969 break;
4970 }
4971 fail:
bellardc573ff62004-01-04 15:51:36 +00004972#ifdef DEBUG
4973 gemu_log(" = %ld\n", ret);
4974#endif
bellard31e31b82003-02-18 22:55:36 +00004975 return ret;
4976}