blob: 59c91f8da9af98bc50f5b7eb209f331d5544d579 [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
blueswir1530e7612009-01-05 18:11:53 +000018 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 * MA 02110-1301, USA.
bellard31e31b82003-02-18 22:55:36 +000020 */
21#include <stdlib.h>
22#include <stdio.h>
23#include <stdarg.h>
bellard04369ff2003-03-20 22:33:23 +000024#include <string.h>
bellard31e31b82003-02-18 22:55:36 +000025#include <elf.h>
26#include <endian.h>
27#include <errno.h>
28#include <unistd.h>
29#include <fcntl.h>
bellard7854b052003-03-29 17:22:23 +000030#include <time.h>
pbrook82e671d2008-06-09 12:10:22 +000031#include <limits.h>
aurel3224e10032009-04-15 16:11:43 +000032#include <mqueue.h>
bellard31e31b82003-02-18 22:55:36 +000033#include <sys/types.h>
thsd08d3bb2007-03-19 13:09:22 +000034#include <sys/ipc.h>
35#include <sys/msg.h>
bellard31e31b82003-02-18 22:55:36 +000036#include <sys/wait.h>
37#include <sys/time.h>
38#include <sys/stat.h>
39#include <sys/mount.h>
ths39b9aae2007-02-11 18:36:44 +000040#include <sys/prctl.h>
bellard31e31b82003-02-18 22:55:36 +000041#include <sys/resource.h>
42#include <sys/mman.h>
43#include <sys/swap.h>
44#include <signal.h>
45#include <sched.h>
46#include <sys/socket.h>
aurel32607175e2009-04-15 16:11:59 +000047#include <sys/un.h>
bellard31e31b82003-02-18 22:55:36 +000048#include <sys/uio.h>
bellard9de5e442003-03-23 16:49:39 +000049#include <sys/poll.h>
bellard32f36bc2003-03-30 21:29:48 +000050#include <sys/times.h>
bellard8853f862004-02-22 14:57:26 +000051#include <sys/shm.h>
thsfa294812007-02-02 22:05:00 +000052#include <sys/sem.h>
bellard56c8f682005-11-28 22:28:41 +000053#include <sys/statfs.h>
bellardebc05482003-09-30 21:08:41 +000054#include <utime.h>
bellarda5448a72004-06-19 16:59:03 +000055#include <sys/sysinfo.h>
aurel323b3f24a2009-04-15 16:12:13 +000056#include <sys/utsname.h>
bellard72f03902003-02-18 23:33:18 +000057//#include <sys/user.h>
bellard8853f862004-02-22 14:57:26 +000058#include <netinet/ip.h>
bellard7854b052003-03-29 17:22:23 +000059#include <netinet/tcp.h>
aurel320b6d3ae2008-09-15 07:43:43 +000060#include <qemu-common.h>
aurel326d946cd2008-11-06 16:15:18 +000061#ifdef HAVE_GPROF
62#include <sys/gmon.h>
63#endif
bellard31e31b82003-02-18 22:55:36 +000064
65#define termios host_termios
66#define winsize host_winsize
67#define termio host_termio
bellard04369ff2003-03-20 22:33:23 +000068#define sgttyb host_sgttyb /* same as target */
69#define tchars host_tchars /* same as target */
70#define ltchars host_ltchars /* same as target */
bellard31e31b82003-02-18 22:55:36 +000071
72#include <linux/termios.h>
73#include <linux/unistd.h>
74#include <linux/utsname.h>
75#include <linux/cdrom.h>
76#include <linux/hdreg.h>
77#include <linux/soundcard.h>
bellard19b84f32003-05-08 15:41:49 +000078#include <linux/kd.h>
balrog8fbd6b52008-09-20 03:03:09 +000079#include <linux/mtio.h>
pbrookd7e40362008-05-23 16:06:43 +000080#include "linux_loop.h"
bellard31e31b82003-02-18 22:55:36 +000081
bellard3ef693a2003-03-23 20:17:16 +000082#include "qemu.h"
balrog526ccb72008-07-16 12:13:52 +000083#include "qemu-common.h"
bellard31e31b82003-02-18 22:55:36 +000084
pbrook30813ce2008-06-02 15:45:44 +000085#if defined(USE_NPTL)
86#include <linux/futex.h>
pbrookd865bab2008-06-07 22:12:17 +000087#define CLONE_NPTL_FLAGS2 (CLONE_SETTLS | \
88 CLONE_PARENT_SETTID | CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
89#else
90/* XXX: Hardcode the above values. */
91#define CLONE_NPTL_FLAGS2 0
pbrook30813ce2008-06-02 15:45:44 +000092#endif
93
bellard72f03902003-02-18 23:33:18 +000094//#define DEBUG
bellard31e31b82003-02-18 22:55:36 +000095
pbrooke6e59062006-10-22 00:18:54 +000096#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC) \
ths48733d12007-10-08 13:36:46 +000097 || defined(TARGET_M68K) || defined(TARGET_SH4) || defined(TARGET_CRIS)
bellard67867302003-11-23 17:05:30 +000098/* 16 bit uid wrappers emulation */
99#define USE_UID16
100#endif
101
bellard1a9353d2003-03-16 20:28:50 +0000102//#include <linux/msdos_fs.h>
aurel326556a832008-10-13 21:08:17 +0000103#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct linux_dirent [2])
104#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct linux_dirent [2])
bellard1a9353d2003-03-16 20:28:50 +0000105
bellard70a194b2003-08-11 22:20:16 +0000106
bellard70a194b2003-08-11 22:20:16 +0000107#undef _syscall0
108#undef _syscall1
109#undef _syscall2
110#undef _syscall3
111#undef _syscall4
112#undef _syscall5
bellard83fcb512006-06-14 13:37:16 +0000113#undef _syscall6
bellard70a194b2003-08-11 22:20:16 +0000114
bellard83fcb512006-06-14 13:37:16 +0000115#define _syscall0(type,name) \
blueswir18fcd3692008-08-17 20:26:25 +0000116static type name (void) \
bellard83fcb512006-06-14 13:37:16 +0000117{ \
118 return syscall(__NR_##name); \
bellard70a194b2003-08-11 22:20:16 +0000119}
120
bellard83fcb512006-06-14 13:37:16 +0000121#define _syscall1(type,name,type1,arg1) \
blueswir18fcd3692008-08-17 20:26:25 +0000122static type name (type1 arg1) \
bellard83fcb512006-06-14 13:37:16 +0000123{ \
124 return syscall(__NR_##name, arg1); \
bellard70a194b2003-08-11 22:20:16 +0000125}
126
bellard83fcb512006-06-14 13:37:16 +0000127#define _syscall2(type,name,type1,arg1,type2,arg2) \
blueswir18fcd3692008-08-17 20:26:25 +0000128static type name (type1 arg1,type2 arg2) \
bellard83fcb512006-06-14 13:37:16 +0000129{ \
130 return syscall(__NR_##name, arg1, arg2); \
bellard70a194b2003-08-11 22:20:16 +0000131}
132
bellard83fcb512006-06-14 13:37:16 +0000133#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
blueswir18fcd3692008-08-17 20:26:25 +0000134static type name (type1 arg1,type2 arg2,type3 arg3) \
bellard83fcb512006-06-14 13:37:16 +0000135{ \
136 return syscall(__NR_##name, arg1, arg2, arg3); \
bellard70a194b2003-08-11 22:20:16 +0000137}
138
bellard83fcb512006-06-14 13:37:16 +0000139#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
blueswir18fcd3692008-08-17 20:26:25 +0000140static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
bellard83fcb512006-06-14 13:37:16 +0000141{ \
142 return syscall(__NR_##name, arg1, arg2, arg3, arg4); \
bellard70a194b2003-08-11 22:20:16 +0000143}
144
bellard83fcb512006-06-14 13:37:16 +0000145#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
146 type5,arg5) \
blueswir18fcd3692008-08-17 20:26:25 +0000147static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
bellard83fcb512006-06-14 13:37:16 +0000148{ \
149 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \
bellard70a194b2003-08-11 22:20:16 +0000150}
bellard83fcb512006-06-14 13:37:16 +0000151
152
153#define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
154 type5,arg5,type6,arg6) \
blueswir18fcd3692008-08-17 20:26:25 +0000155static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \
156 type6 arg6) \
bellard83fcb512006-06-14 13:37:16 +0000157{ \
158 return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
159}
160
bellard70a194b2003-08-11 22:20:16 +0000161
bellard31e31b82003-02-18 22:55:36 +0000162#define __NR_sys_uname __NR_uname
ths92a34c12007-09-24 09:27:49 +0000163#define __NR_sys_faccessat __NR_faccessat
ths814d7972007-09-24 09:26:51 +0000164#define __NR_sys_fchmodat __NR_fchmodat
thsccfa72b2007-09-24 09:23:34 +0000165#define __NR_sys_fchownat __NR_fchownat
balrog6a24a772008-09-20 02:23:36 +0000166#define __NR_sys_fstatat64 __NR_fstatat64
balrogac8a6552008-09-20 02:25:39 +0000167#define __NR_sys_futimesat __NR_futimesat
bellard72f03902003-02-18 23:33:18 +0000168#define __NR_sys_getcwd1 __NR_getcwd
bellard72f03902003-02-18 23:33:18 +0000169#define __NR_sys_getdents __NR_getdents
bellarddab2ed92003-03-22 15:23:14 +0000170#define __NR_sys_getdents64 __NR_getdents64
thsc6cda172007-10-09 03:42:34 +0000171#define __NR_sys_getpriority __NR_getpriority
ths64f0ce42007-09-24 09:25:06 +0000172#define __NR_sys_linkat __NR_linkat
ths4472ad02007-09-24 09:22:32 +0000173#define __NR_sys_mkdirat __NR_mkdirat
ths75ac37a2007-09-24 09:23:05 +0000174#define __NR_sys_mknodat __NR_mknodat
aurel329d33b762009-04-08 23:07:05 +0000175#define __NR_sys_newfstatat __NR_newfstatat
ths82424832007-09-24 09:21:55 +0000176#define __NR_sys_openat __NR_openat
ths5e0ccb12007-09-24 09:26:10 +0000177#define __NR_sys_readlinkat __NR_readlinkat
ths722183f2007-09-24 09:24:37 +0000178#define __NR_sys_renameat __NR_renameat
bellard66fb9762003-03-23 01:06:05 +0000179#define __NR_sys_rt_sigqueueinfo __NR_rt_sigqueueinfo
thsf0b62432007-09-24 09:25:40 +0000180#define __NR_sys_symlinkat __NR_symlinkat
ths7494b0f2007-02-11 18:26:53 +0000181#define __NR_sys_syslog __NR_syslog
ths71455572007-06-21 21:45:30 +0000182#define __NR_sys_tgkill __NR_tgkill
ths4cae1d12007-07-12 11:06:53 +0000183#define __NR_sys_tkill __NR_tkill
ths8170f562007-09-24 09:24:11 +0000184#define __NR_sys_unlinkat __NR_unlinkat
ths9007f0e2007-09-25 17:50:37 +0000185#define __NR_sys_utimensat __NR_utimensat
pbrookbd0c5662008-05-29 14:34:11 +0000186#define __NR_sys_futex __NR_futex
aurel3239b59762008-10-01 21:46:50 +0000187#define __NR_sys_inotify_init __NR_inotify_init
188#define __NR_sys_inotify_add_watch __NR_inotify_add_watch
189#define __NR_sys_inotify_rm_watch __NR_inotify_rm_watch
bellard31e31b82003-02-18 22:55:36 +0000190
bellardbc51c5c2004-03-17 23:46:04 +0000191#if defined(__alpha__) || defined (__ia64__) || defined(__x86_64__)
bellard9af9eaa2003-04-07 21:34:41 +0000192#define __NR__llseek __NR_lseek
193#endif
194
bellard72f03902003-02-18 23:33:18 +0000195#ifdef __NR_gettid
bellard31e31b82003-02-18 22:55:36 +0000196_syscall0(int, gettid)
bellard72f03902003-02-18 23:33:18 +0000197#else
ths0da46a62007-10-20 20:23:07 +0000198/* This is a replacement for the host gettid() and must return a host
199 errno. */
bellard72f03902003-02-18 23:33:18 +0000200static int gettid(void) {
201 return -ENOSYS;
202}
203#endif
aurel323b3f24a2009-04-15 16:12:13 +0000204#if TARGET_ABI_BITS == 32
205_syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
206#endif
207#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
208_syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
209#endif
210_syscall2(int, sys_getpriority, int, which, int, who);
Paul Brook909b69c2009-05-19 15:58:52 +0100211#if defined(TARGET_NR__llseek) && !defined (__x86_64__)
aurel323b3f24a2009-04-15 16:12:13 +0000212_syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo,
213 loff_t *, res, uint, wh);
214#endif
215_syscall3(int,sys_rt_sigqueueinfo,int,pid,int,sig,siginfo_t *,uinfo)
216_syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
217#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
218_syscall3(int,sys_tgkill,int,tgid,int,pid,int,sig)
219#endif
220#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
221_syscall2(int,sys_tkill,int,tid,int,sig)
222#endif
223#ifdef __NR_exit_group
224_syscall1(int,exit_group,int,error_code)
225#endif
226#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
227_syscall1(int,set_tid_address,int *,tidptr)
228#endif
229#if defined(USE_NPTL)
230#if defined(TARGET_NR_futex) && defined(__NR_futex)
231_syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
232 const struct timespec *,timeout,int *,uaddr2,int,val3)
233#endif
234#endif
235
236static bitmask_transtbl fcntl_flags_tbl[] = {
237 { TARGET_O_ACCMODE, TARGET_O_WRONLY, O_ACCMODE, O_WRONLY, },
238 { TARGET_O_ACCMODE, TARGET_O_RDWR, O_ACCMODE, O_RDWR, },
239 { TARGET_O_CREAT, TARGET_O_CREAT, O_CREAT, O_CREAT, },
240 { TARGET_O_EXCL, TARGET_O_EXCL, O_EXCL, O_EXCL, },
241 { TARGET_O_NOCTTY, TARGET_O_NOCTTY, O_NOCTTY, O_NOCTTY, },
242 { TARGET_O_TRUNC, TARGET_O_TRUNC, O_TRUNC, O_TRUNC, },
243 { TARGET_O_APPEND, TARGET_O_APPEND, O_APPEND, O_APPEND, },
244 { TARGET_O_NONBLOCK, TARGET_O_NONBLOCK, O_NONBLOCK, O_NONBLOCK, },
245 { TARGET_O_SYNC, TARGET_O_SYNC, O_SYNC, O_SYNC, },
246 { TARGET_FASYNC, TARGET_FASYNC, FASYNC, FASYNC, },
247 { TARGET_O_DIRECTORY, TARGET_O_DIRECTORY, O_DIRECTORY, O_DIRECTORY, },
248 { TARGET_O_NOFOLLOW, TARGET_O_NOFOLLOW, O_NOFOLLOW, O_NOFOLLOW, },
249 { TARGET_O_LARGEFILE, TARGET_O_LARGEFILE, O_LARGEFILE, O_LARGEFILE, },
250#if defined(O_DIRECT)
251 { TARGET_O_DIRECT, TARGET_O_DIRECT, O_DIRECT, O_DIRECT, },
252#endif
253 { 0, 0, 0, 0 }
254};
255
256#define COPY_UTSNAME_FIELD(dest, src) \
257 do { \
258 /* __NEW_UTS_LEN doesn't include terminating null */ \
259 (void) strncpy((dest), (src), __NEW_UTS_LEN); \
260 (dest)[__NEW_UTS_LEN] = '\0'; \
261 } while (0)
262
263static int sys_uname(struct new_utsname *buf)
264{
265 struct utsname uts_buf;
266
267 if (uname(&uts_buf) < 0)
268 return (-1);
269
270 /*
271 * Just in case these have some differences, we
272 * translate utsname to new_utsname (which is the
273 * struct linux kernel uses).
274 */
275
276 bzero(buf, sizeof (*buf));
277 COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
278 COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
279 COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
280 COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
281 COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
282#ifdef _GNU_SOURCE
283 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
284#endif
285 return (0);
286
287#undef COPY_UTSNAME_FIELD
288}
289
290static int sys_getcwd1(char *buf, size_t size)
291{
292 if (getcwd(buf, size) == NULL) {
293 /* getcwd() sets errno */
294 return (-1);
295 }
aurel32aaf4ad32009-04-16 14:17:14 +0000296 return strlen(buf)+1;
aurel323b3f24a2009-04-15 16:12:13 +0000297}
298
299#ifdef CONFIG_ATFILE
300/*
301 * Host system seems to have atfile syscall stubs available. We
302 * now enable them one by one as specified by target syscall_nr.h.
303 */
304
305#ifdef TARGET_NR_faccessat
aurel32465c9f02009-04-19 08:52:17 +0000306static int sys_faccessat(int dirfd, const char *pathname, int mode)
aurel323b3f24a2009-04-15 16:12:13 +0000307{
aurel32465c9f02009-04-19 08:52:17 +0000308 return (faccessat(dirfd, pathname, mode, 0));
aurel323b3f24a2009-04-15 16:12:13 +0000309}
310#endif
311#ifdef TARGET_NR_fchmodat
aurel32465c9f02009-04-19 08:52:17 +0000312static int sys_fchmodat(int dirfd, const char *pathname, mode_t mode)
aurel323b3f24a2009-04-15 16:12:13 +0000313{
aurel32465c9f02009-04-19 08:52:17 +0000314 return (fchmodat(dirfd, pathname, mode, 0));
aurel323b3f24a2009-04-15 16:12:13 +0000315}
316#endif
aurel32fda33742009-04-15 17:12:01 +0000317#if defined(TARGET_NR_fchownat) && defined(USE_UID16)
aurel323b3f24a2009-04-15 16:12:13 +0000318static int sys_fchownat(int dirfd, const char *pathname, uid_t owner,
319 gid_t group, int flags)
320{
321 return (fchownat(dirfd, pathname, owner, group, flags));
322}
323#endif
324#ifdef __NR_fstatat64
325static int sys_fstatat64(int dirfd, const char *pathname, struct stat *buf,
326 int flags)
327{
328 return (fstatat(dirfd, pathname, buf, flags));
329}
330#endif
331#ifdef __NR_newfstatat
332static int sys_newfstatat(int dirfd, const char *pathname, struct stat *buf,
333 int flags)
334{
335 return (fstatat(dirfd, pathname, buf, flags));
336}
337#endif
338#ifdef TARGET_NR_futimesat
339static int sys_futimesat(int dirfd, const char *pathname,
340 const struct timeval times[2])
341{
342 return (futimesat(dirfd, pathname, times));
343}
344#endif
345#ifdef TARGET_NR_linkat
346static int sys_linkat(int olddirfd, const char *oldpath,
347 int newdirfd, const char *newpath, int flags)
348{
349 return (linkat(olddirfd, oldpath, newdirfd, newpath, flags));
350}
351#endif
352#ifdef TARGET_NR_mkdirat
353static int sys_mkdirat(int dirfd, const char *pathname, mode_t mode)
354{
355 return (mkdirat(dirfd, pathname, mode));
356}
357#endif
358#ifdef TARGET_NR_mknodat
359static int sys_mknodat(int dirfd, const char *pathname, mode_t mode,
360 dev_t dev)
361{
362 return (mknodat(dirfd, pathname, mode, dev));
363}
364#endif
365#ifdef TARGET_NR_openat
366static int sys_openat(int dirfd, const char *pathname, int flags, ...)
367{
368 /*
369 * open(2) has extra parameter 'mode' when called with
370 * flag O_CREAT.
371 */
372 if ((flags & O_CREAT) != 0) {
373 va_list ap;
374 mode_t mode;
375
376 /*
377 * Get the 'mode' parameter and translate it to
378 * host bits.
379 */
380 va_start(ap, flags);
381 mode = va_arg(ap, mode_t);
382 mode = target_to_host_bitmask(mode, fcntl_flags_tbl);
383 va_end(ap);
384
385 return (openat(dirfd, pathname, flags, mode));
386 }
387 return (openat(dirfd, pathname, flags));
388}
389#endif
390#ifdef TARGET_NR_readlinkat
391static int sys_readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz)
392{
393 return (readlinkat(dirfd, pathname, buf, bufsiz));
394}
395#endif
396#ifdef TARGET_NR_renameat
397static int sys_renameat(int olddirfd, const char *oldpath,
398 int newdirfd, const char *newpath)
399{
400 return (renameat(olddirfd, oldpath, newdirfd, newpath));
401}
402#endif
403#ifdef TARGET_NR_symlinkat
404static int sys_symlinkat(const char *oldpath, int newdirfd, const char *newpath)
405{
406 return (symlinkat(oldpath, newdirfd, newpath));
407}
408#endif
409#ifdef TARGET_NR_unlinkat
410static int sys_unlinkat(int dirfd, const char *pathname, int flags)
411{
412 return (unlinkat(dirfd, pathname, flags));
413}
414#endif
415#ifdef TARGET_NR_utimensat
416static int sys_utimensat(int dirfd, const char *pathname,
417 const struct timespec times[2], int flags)
418{
419 return (utimensat(dirfd, pathname, times, flags));
420}
421#endif
422#else /* !CONFIG_ATFILE */
423
424/*
425 * Try direct syscalls instead
426 */
ths92a34c12007-09-24 09:27:49 +0000427#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
aurel32465c9f02009-04-19 08:52:17 +0000428_syscall3(int,sys_faccessat,int,dirfd,const char *,pathname,int,mode)
ths92a34c12007-09-24 09:27:49 +0000429#endif
ths814d7972007-09-24 09:26:51 +0000430#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
aurel32465c9f02009-04-19 08:52:17 +0000431_syscall3(int,sys_fchmodat,int,dirfd,const char *,pathname, mode_t,mode)
ths814d7972007-09-24 09:26:51 +0000432#endif
blueswir14583f582008-08-24 10:35:55 +0000433#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat) && defined(USE_UID16)
thsccfa72b2007-09-24 09:23:34 +0000434_syscall5(int,sys_fchownat,int,dirfd,const char *,pathname,
435 uid_t,owner,gid_t,group,int,flags)
436#endif
aurel329d33b762009-04-08 23:07:05 +0000437#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
438 defined(__NR_fstatat64)
balrog6a24a772008-09-20 02:23:36 +0000439_syscall4(int,sys_fstatat64,int,dirfd,const char *,pathname,
440 struct stat *,buf,int,flags)
441#endif
balrogac8a6552008-09-20 02:25:39 +0000442#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
443_syscall3(int,sys_futimesat,int,dirfd,const char *,pathname,
444 const struct timeval *,times)
445#endif
aurel323b3f24a2009-04-15 16:12:13 +0000446#if (defined(TARGET_NR_newfstatat) || defined(TARGET_NR_fstatat64) ) && \
447 defined(__NR_newfstatat)
448_syscall4(int,sys_newfstatat,int,dirfd,const char *,pathname,
449 struct stat *,buf,int,flags)
blueswir18fcd3692008-08-17 20:26:25 +0000450#endif
ths64f0ce42007-09-24 09:25:06 +0000451#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
452_syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath,
aurel323b3f24a2009-04-15 16:12:13 +0000453 int,newdirfd,const char *,newpath,int,flags)
ths64f0ce42007-09-24 09:25:06 +0000454#endif
ths4472ad02007-09-24 09:22:32 +0000455#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
456_syscall3(int,sys_mkdirat,int,dirfd,const char *,pathname,mode_t,mode)
457#endif
ths75ac37a2007-09-24 09:23:05 +0000458#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
459_syscall4(int,sys_mknodat,int,dirfd,const char *,pathname,
460 mode_t,mode,dev_t,dev)
461#endif
ths82424832007-09-24 09:21:55 +0000462#if defined(TARGET_NR_openat) && defined(__NR_openat)
463_syscall4(int,sys_openat,int,dirfd,const char *,pathname,int,flags,mode_t,mode)
464#endif
ths5e0ccb12007-09-24 09:26:10 +0000465#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
466_syscall4(int,sys_readlinkat,int,dirfd,const char *,pathname,
467 char *,buf,size_t,bufsize)
468#endif
ths722183f2007-09-24 09:24:37 +0000469#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
470_syscall4(int,sys_renameat,int,olddirfd,const char *,oldpath,
471 int,newdirfd,const char *,newpath)
472#endif
thsb51eaa82007-09-25 16:09:22 +0000473#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
thsf0b62432007-09-24 09:25:40 +0000474_syscall3(int,sys_symlinkat,const char *,oldpath,
475 int,newdirfd,const char *,newpath)
476#endif
ths8170f562007-09-24 09:24:11 +0000477#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
478_syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
479#endif
ths9007f0e2007-09-25 17:50:37 +0000480#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
481_syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
482 const struct timespec *,tsp,int,flags)
483#endif
aurel323b3f24a2009-04-15 16:12:13 +0000484
485#endif /* CONFIG_ATFILE */
486
487#ifdef CONFIG_INOTIFY
aurel328690e422009-04-17 13:50:32 +0000488#include <sys/inotify.h>
aurel323b3f24a2009-04-15 16:12:13 +0000489
aurel3239b59762008-10-01 21:46:50 +0000490#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel323b3f24a2009-04-15 16:12:13 +0000491static int sys_inotify_init(void)
492{
493 return (inotify_init());
494}
aurel3239b59762008-10-01 21:46:50 +0000495#endif
496#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000497static int sys_inotify_add_watch(int fd,const char *pathname, int32_t mask)
498{
499 return (inotify_add_watch(fd, pathname, mask));
500}
aurel3239b59762008-10-01 21:46:50 +0000501#endif
502#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel323b3f24a2009-04-15 16:12:13 +0000503static int sys_inotify_rm_watch(int fd, int32_t wd)
504{
aurel328690e422009-04-17 13:50:32 +0000505 return (inotify_rm_watch(fd, wd));
aurel323b3f24a2009-04-15 16:12:13 +0000506}
aurel3239b59762008-10-01 21:46:50 +0000507#endif
aurel323b3f24a2009-04-15 16:12:13 +0000508#else
509/* Userspace can usually survive runtime without inotify */
510#undef TARGET_NR_inotify_init
511#undef TARGET_NR_inotify_add_watch
512#undef TARGET_NR_inotify_rm_watch
513#endif /* CONFIG_INOTIFY */
514
bellard66fb9762003-03-23 01:06:05 +0000515
516extern int personality(int);
bellard9de5e442003-03-23 16:49:39 +0000517extern int flock(int, int);
518extern int setfsuid(int);
519extern int setfsgid(int);
bellard19b84f32003-05-08 15:41:49 +0000520extern int setgroups(int, gid_t *);
bellard31e31b82003-02-18 22:55:36 +0000521
thsb92c47c2007-11-01 00:07:38 +0000522#define ERRNO_TABLE_SIZE 1200
523
524/* target_to_host_errno_table[] is initialized from
525 * host_to_target_errno_table[] in syscall_init(). */
526static uint16_t target_to_host_errno_table[ERRNO_TABLE_SIZE] = {
527};
528
ths637947f2007-06-01 12:09:19 +0000529/*
thsfe8f0962007-07-12 10:59:21 +0000530 * This list is the union of errno values overridden in asm-<arch>/errno.h
ths637947f2007-06-01 12:09:19 +0000531 * minus the errnos that are not actually generic to all archs.
532 */
thsb92c47c2007-11-01 00:07:38 +0000533static uint16_t host_to_target_errno_table[ERRNO_TABLE_SIZE] = {
ths637947f2007-06-01 12:09:19 +0000534 [EIDRM] = TARGET_EIDRM,
535 [ECHRNG] = TARGET_ECHRNG,
536 [EL2NSYNC] = TARGET_EL2NSYNC,
537 [EL3HLT] = TARGET_EL3HLT,
538 [EL3RST] = TARGET_EL3RST,
539 [ELNRNG] = TARGET_ELNRNG,
540 [EUNATCH] = TARGET_EUNATCH,
541 [ENOCSI] = TARGET_ENOCSI,
542 [EL2HLT] = TARGET_EL2HLT,
543 [EDEADLK] = TARGET_EDEADLK,
544 [ENOLCK] = TARGET_ENOLCK,
545 [EBADE] = TARGET_EBADE,
546 [EBADR] = TARGET_EBADR,
547 [EXFULL] = TARGET_EXFULL,
548 [ENOANO] = TARGET_ENOANO,
549 [EBADRQC] = TARGET_EBADRQC,
550 [EBADSLT] = TARGET_EBADSLT,
551 [EBFONT] = TARGET_EBFONT,
552 [ENOSTR] = TARGET_ENOSTR,
553 [ENODATA] = TARGET_ENODATA,
554 [ETIME] = TARGET_ETIME,
555 [ENOSR] = TARGET_ENOSR,
556 [ENONET] = TARGET_ENONET,
557 [ENOPKG] = TARGET_ENOPKG,
558 [EREMOTE] = TARGET_EREMOTE,
559 [ENOLINK] = TARGET_ENOLINK,
560 [EADV] = TARGET_EADV,
561 [ESRMNT] = TARGET_ESRMNT,
562 [ECOMM] = TARGET_ECOMM,
563 [EPROTO] = TARGET_EPROTO,
564 [EDOTDOT] = TARGET_EDOTDOT,
565 [EMULTIHOP] = TARGET_EMULTIHOP,
566 [EBADMSG] = TARGET_EBADMSG,
567 [ENAMETOOLONG] = TARGET_ENAMETOOLONG,
568 [EOVERFLOW] = TARGET_EOVERFLOW,
569 [ENOTUNIQ] = TARGET_ENOTUNIQ,
570 [EBADFD] = TARGET_EBADFD,
571 [EREMCHG] = TARGET_EREMCHG,
572 [ELIBACC] = TARGET_ELIBACC,
573 [ELIBBAD] = TARGET_ELIBBAD,
574 [ELIBSCN] = TARGET_ELIBSCN,
575 [ELIBMAX] = TARGET_ELIBMAX,
576 [ELIBEXEC] = TARGET_ELIBEXEC,
577 [EILSEQ] = TARGET_EILSEQ,
578 [ENOSYS] = TARGET_ENOSYS,
579 [ELOOP] = TARGET_ELOOP,
580 [ERESTART] = TARGET_ERESTART,
581 [ESTRPIPE] = TARGET_ESTRPIPE,
582 [ENOTEMPTY] = TARGET_ENOTEMPTY,
583 [EUSERS] = TARGET_EUSERS,
584 [ENOTSOCK] = TARGET_ENOTSOCK,
585 [EDESTADDRREQ] = TARGET_EDESTADDRREQ,
586 [EMSGSIZE] = TARGET_EMSGSIZE,
587 [EPROTOTYPE] = TARGET_EPROTOTYPE,
588 [ENOPROTOOPT] = TARGET_ENOPROTOOPT,
589 [EPROTONOSUPPORT] = TARGET_EPROTONOSUPPORT,
590 [ESOCKTNOSUPPORT] = TARGET_ESOCKTNOSUPPORT,
591 [EOPNOTSUPP] = TARGET_EOPNOTSUPP,
592 [EPFNOSUPPORT] = TARGET_EPFNOSUPPORT,
593 [EAFNOSUPPORT] = TARGET_EAFNOSUPPORT,
594 [EADDRINUSE] = TARGET_EADDRINUSE,
595 [EADDRNOTAVAIL] = TARGET_EADDRNOTAVAIL,
596 [ENETDOWN] = TARGET_ENETDOWN,
597 [ENETUNREACH] = TARGET_ENETUNREACH,
598 [ENETRESET] = TARGET_ENETRESET,
599 [ECONNABORTED] = TARGET_ECONNABORTED,
600 [ECONNRESET] = TARGET_ECONNRESET,
601 [ENOBUFS] = TARGET_ENOBUFS,
602 [EISCONN] = TARGET_EISCONN,
603 [ENOTCONN] = TARGET_ENOTCONN,
604 [EUCLEAN] = TARGET_EUCLEAN,
605 [ENOTNAM] = TARGET_ENOTNAM,
606 [ENAVAIL] = TARGET_ENAVAIL,
607 [EISNAM] = TARGET_EISNAM,
608 [EREMOTEIO] = TARGET_EREMOTEIO,
609 [ESHUTDOWN] = TARGET_ESHUTDOWN,
610 [ETOOMANYREFS] = TARGET_ETOOMANYREFS,
611 [ETIMEDOUT] = TARGET_ETIMEDOUT,
612 [ECONNREFUSED] = TARGET_ECONNREFUSED,
613 [EHOSTDOWN] = TARGET_EHOSTDOWN,
614 [EHOSTUNREACH] = TARGET_EHOSTUNREACH,
615 [EALREADY] = TARGET_EALREADY,
616 [EINPROGRESS] = TARGET_EINPROGRESS,
617 [ESTALE] = TARGET_ESTALE,
618 [ECANCELED] = TARGET_ECANCELED,
619 [ENOMEDIUM] = TARGET_ENOMEDIUM,
620 [EMEDIUMTYPE] = TARGET_EMEDIUMTYPE,
thsb7fe5db2007-07-23 15:37:46 +0000621#ifdef ENOKEY
ths637947f2007-06-01 12:09:19 +0000622 [ENOKEY] = TARGET_ENOKEY,
thsb7fe5db2007-07-23 15:37:46 +0000623#endif
624#ifdef EKEYEXPIRED
ths637947f2007-06-01 12:09:19 +0000625 [EKEYEXPIRED] = TARGET_EKEYEXPIRED,
thsb7fe5db2007-07-23 15:37:46 +0000626#endif
627#ifdef EKEYREVOKED
ths637947f2007-06-01 12:09:19 +0000628 [EKEYREVOKED] = TARGET_EKEYREVOKED,
thsb7fe5db2007-07-23 15:37:46 +0000629#endif
630#ifdef EKEYREJECTED
ths637947f2007-06-01 12:09:19 +0000631 [EKEYREJECTED] = TARGET_EKEYREJECTED,
thsb7fe5db2007-07-23 15:37:46 +0000632#endif
633#ifdef EOWNERDEAD
ths637947f2007-06-01 12:09:19 +0000634 [EOWNERDEAD] = TARGET_EOWNERDEAD,
thsb7fe5db2007-07-23 15:37:46 +0000635#endif
636#ifdef ENOTRECOVERABLE
ths637947f2007-06-01 12:09:19 +0000637 [ENOTRECOVERABLE] = TARGET_ENOTRECOVERABLE,
thsb7fe5db2007-07-23 15:37:46 +0000638#endif
thsb92c47c2007-11-01 00:07:38 +0000639};
ths637947f2007-06-01 12:09:19 +0000640
641static inline int host_to_target_errno(int err)
642{
643 if(host_to_target_errno_table[err])
644 return host_to_target_errno_table[err];
645 return err;
646}
647
thsb92c47c2007-11-01 00:07:38 +0000648static inline int target_to_host_errno(int err)
649{
650 if (target_to_host_errno_table[err])
651 return target_to_host_errno_table[err];
652 return err;
653}
654
blueswir1992f48a2007-10-14 16:27:31 +0000655static inline abi_long get_errno(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000656{
657 if (ret == -1)
ths637947f2007-06-01 12:09:19 +0000658 return -host_to_target_errno(errno);
bellard31e31b82003-02-18 22:55:36 +0000659 else
660 return ret;
661}
662
blueswir1992f48a2007-10-14 16:27:31 +0000663static inline int is_error(abi_long ret)
bellard31e31b82003-02-18 22:55:36 +0000664{
blueswir1992f48a2007-10-14 16:27:31 +0000665 return (abi_ulong)ret >= (abi_ulong)(-4096);
bellard31e31b82003-02-18 22:55:36 +0000666}
667
thsb92c47c2007-11-01 00:07:38 +0000668char *target_strerror(int err)
669{
670 return strerror(target_to_host_errno(err));
671}
672
blueswir1992f48a2007-10-14 16:27:31 +0000673static abi_ulong target_brk;
674static abi_ulong target_original_brk;
bellard31e31b82003-02-18 22:55:36 +0000675
blueswir1992f48a2007-10-14 16:27:31 +0000676void target_set_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000677{
blueswir14c1de732007-07-07 20:45:44 +0000678 target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
bellard31e31b82003-02-18 22:55:36 +0000679}
680
ths0da46a62007-10-20 20:23:07 +0000681/* do_brk() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000682abi_long do_brk(abi_ulong new_brk)
bellard31e31b82003-02-18 22:55:36 +0000683{
blueswir1992f48a2007-10-14 16:27:31 +0000684 abi_ulong brk_page;
685 abi_long mapped_addr;
bellard31e31b82003-02-18 22:55:36 +0000686 int new_alloc_size;
687
688 if (!new_brk)
pbrook53a59602006-03-25 19:31:22 +0000689 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000690 if (new_brk < target_original_brk)
balrog7ab240a2008-04-26 12:17:34 +0000691 return target_brk;
ths3b46e622007-09-17 08:09:54 +0000692
pbrook53a59602006-03-25 19:31:22 +0000693 brk_page = HOST_PAGE_ALIGN(target_brk);
bellard31e31b82003-02-18 22:55:36 +0000694
695 /* If the new brk is less than this, set it and we're done... */
696 if (new_brk < brk_page) {
697 target_brk = new_brk;
pbrook53a59602006-03-25 19:31:22 +0000698 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000699 }
700
701 /* We need to allocate more memory after the brk... */
bellard54936002003-05-13 00:25:15 +0000702 new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
ths5fafdf22007-09-16 21:08:06 +0000703 mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
bellard54936002003-05-13 00:25:15 +0000704 PROT_READ|PROT_WRITE,
705 MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
balrog7ab240a2008-04-26 12:17:34 +0000706
707 if (!is_error(mapped_addr))
bellard31e31b82003-02-18 22:55:36 +0000708 target_brk = new_brk;
balrog7ab240a2008-04-26 12:17:34 +0000709
710 return target_brk;
bellard31e31b82003-02-18 22:55:36 +0000711}
712
ths26edcf42007-12-09 02:25:24 +0000713static inline abi_long copy_from_user_fdset(fd_set *fds,
714 abi_ulong target_fds_addr,
715 int n)
bellard31e31b82003-02-18 22:55:36 +0000716{
ths26edcf42007-12-09 02:25:24 +0000717 int i, nw, j, k;
718 abi_ulong b, *target_fds;
719
720 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
721 if (!(target_fds = lock_user(VERIFY_READ,
722 target_fds_addr,
723 sizeof(abi_ulong) * nw,
724 1)))
725 return -TARGET_EFAULT;
726
727 FD_ZERO(fds);
728 k = 0;
729 for (i = 0; i < nw; i++) {
730 /* grab the abi_ulong */
731 __get_user(b, &target_fds[i]);
732 for (j = 0; j < TARGET_ABI_BITS; j++) {
733 /* check the bit inside the abi_ulong */
734 if ((b >> j) & 1)
735 FD_SET(k, fds);
736 k++;
bellard31e31b82003-02-18 22:55:36 +0000737 }
bellard31e31b82003-02-18 22:55:36 +0000738 }
ths26edcf42007-12-09 02:25:24 +0000739
740 unlock_user(target_fds, target_fds_addr, 0);
741
742 return 0;
bellard31e31b82003-02-18 22:55:36 +0000743}
744
ths26edcf42007-12-09 02:25:24 +0000745static inline abi_long copy_to_user_fdset(abi_ulong target_fds_addr,
746 const fd_set *fds,
747 int n)
bellard31e31b82003-02-18 22:55:36 +0000748{
bellard31e31b82003-02-18 22:55:36 +0000749 int i, nw, j, k;
blueswir1992f48a2007-10-14 16:27:31 +0000750 abi_long v;
ths26edcf42007-12-09 02:25:24 +0000751 abi_ulong *target_fds;
bellard31e31b82003-02-18 22:55:36 +0000752
ths26edcf42007-12-09 02:25:24 +0000753 nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
754 if (!(target_fds = lock_user(VERIFY_WRITE,
755 target_fds_addr,
756 sizeof(abi_ulong) * nw,
757 0)))
758 return -TARGET_EFAULT;
759
760 k = 0;
761 for (i = 0; i < nw; i++) {
762 v = 0;
763 for (j = 0; j < TARGET_ABI_BITS; j++) {
764 v |= ((FD_ISSET(k, fds) != 0) << j);
765 k++;
bellard31e31b82003-02-18 22:55:36 +0000766 }
ths26edcf42007-12-09 02:25:24 +0000767 __put_user(v, &target_fds[i]);
bellard31e31b82003-02-18 22:55:36 +0000768 }
ths26edcf42007-12-09 02:25:24 +0000769
770 unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
771
772 return 0;
bellard31e31b82003-02-18 22:55:36 +0000773}
774
bellardc596ed12003-07-13 17:32:31 +0000775#if defined(__alpha__)
776#define HOST_HZ 1024
777#else
778#define HOST_HZ 100
779#endif
780
blueswir1992f48a2007-10-14 16:27:31 +0000781static inline abi_long host_to_target_clock_t(long ticks)
bellardc596ed12003-07-13 17:32:31 +0000782{
783#if HOST_HZ == TARGET_HZ
784 return ticks;
785#else
786 return ((int64_t)ticks * TARGET_HZ) / HOST_HZ;
787#endif
788}
789
bellard579a97f2007-11-11 14:26:47 +0000790static inline abi_long host_to_target_rusage(abi_ulong target_addr,
791 const struct rusage *rusage)
bellardb4091862003-05-16 15:39:34 +0000792{
pbrook53a59602006-03-25 19:31:22 +0000793 struct target_rusage *target_rusage;
794
bellard579a97f2007-11-11 14:26:47 +0000795 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
796 return -TARGET_EFAULT;
bellardb4091862003-05-16 15:39:34 +0000797 target_rusage->ru_utime.tv_sec = tswapl(rusage->ru_utime.tv_sec);
798 target_rusage->ru_utime.tv_usec = tswapl(rusage->ru_utime.tv_usec);
799 target_rusage->ru_stime.tv_sec = tswapl(rusage->ru_stime.tv_sec);
800 target_rusage->ru_stime.tv_usec = tswapl(rusage->ru_stime.tv_usec);
801 target_rusage->ru_maxrss = tswapl(rusage->ru_maxrss);
802 target_rusage->ru_ixrss = tswapl(rusage->ru_ixrss);
803 target_rusage->ru_idrss = tswapl(rusage->ru_idrss);
804 target_rusage->ru_isrss = tswapl(rusage->ru_isrss);
805 target_rusage->ru_minflt = tswapl(rusage->ru_minflt);
806 target_rusage->ru_majflt = tswapl(rusage->ru_majflt);
807 target_rusage->ru_nswap = tswapl(rusage->ru_nswap);
808 target_rusage->ru_inblock = tswapl(rusage->ru_inblock);
809 target_rusage->ru_oublock = tswapl(rusage->ru_oublock);
810 target_rusage->ru_msgsnd = tswapl(rusage->ru_msgsnd);
811 target_rusage->ru_msgrcv = tswapl(rusage->ru_msgrcv);
812 target_rusage->ru_nsignals = tswapl(rusage->ru_nsignals);
813 target_rusage->ru_nvcsw = tswapl(rusage->ru_nvcsw);
814 target_rusage->ru_nivcsw = tswapl(rusage->ru_nivcsw);
pbrook53a59602006-03-25 19:31:22 +0000815 unlock_user_struct(target_rusage, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000816
817 return 0;
bellardb4091862003-05-16 15:39:34 +0000818}
819
ths788f5ec2007-12-09 02:37:05 +0000820static inline abi_long copy_from_user_timeval(struct timeval *tv,
821 abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000822{
pbrook53a59602006-03-25 19:31:22 +0000823 struct target_timeval *target_tv;
824
ths788f5ec2007-12-09 02:37:05 +0000825 if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1))
bellard579a97f2007-11-11 14:26:47 +0000826 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000827
828 __get_user(tv->tv_sec, &target_tv->tv_sec);
829 __get_user(tv->tv_usec, &target_tv->tv_usec);
830
831 unlock_user_struct(target_tv, target_tv_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000832
833 return 0;
bellard31e31b82003-02-18 22:55:36 +0000834}
835
ths788f5ec2007-12-09 02:37:05 +0000836static inline abi_long copy_to_user_timeval(abi_ulong target_tv_addr,
837 const struct timeval *tv)
bellard31e31b82003-02-18 22:55:36 +0000838{
pbrook53a59602006-03-25 19:31:22 +0000839 struct target_timeval *target_tv;
840
ths788f5ec2007-12-09 02:37:05 +0000841 if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
bellard579a97f2007-11-11 14:26:47 +0000842 return -TARGET_EFAULT;
ths788f5ec2007-12-09 02:37:05 +0000843
844 __put_user(tv->tv_sec, &target_tv->tv_sec);
845 __put_user(tv->tv_usec, &target_tv->tv_usec);
846
847 unlock_user_struct(target_tv, target_tv_addr, 1);
bellard579a97f2007-11-11 14:26:47 +0000848
849 return 0;
bellard31e31b82003-02-18 22:55:36 +0000850}
851
aurel3224e10032009-04-15 16:11:43 +0000852static inline abi_long copy_from_user_mq_attr(struct mq_attr *attr,
853 abi_ulong target_mq_attr_addr)
854{
855 struct target_mq_attr *target_mq_attr;
856
857 if (!lock_user_struct(VERIFY_READ, target_mq_attr,
858 target_mq_attr_addr, 1))
859 return -TARGET_EFAULT;
860
861 __get_user(attr->mq_flags, &target_mq_attr->mq_flags);
862 __get_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
863 __get_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
864 __get_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
865
866 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 0);
867
868 return 0;
869}
870
871static inline abi_long copy_to_user_mq_attr(abi_ulong target_mq_attr_addr,
872 const struct mq_attr *attr)
873{
874 struct target_mq_attr *target_mq_attr;
875
876 if (!lock_user_struct(VERIFY_WRITE, target_mq_attr,
877 target_mq_attr_addr, 0))
878 return -TARGET_EFAULT;
879
880 __put_user(attr->mq_flags, &target_mq_attr->mq_flags);
881 __put_user(attr->mq_maxmsg, &target_mq_attr->mq_maxmsg);
882 __put_user(attr->mq_msgsize, &target_mq_attr->mq_msgsize);
883 __put_user(attr->mq_curmsgs, &target_mq_attr->mq_curmsgs);
884
885 unlock_user_struct(target_mq_attr, target_mq_attr_addr, 1);
886
887 return 0;
888}
bellard31e31b82003-02-18 22:55:36 +0000889
ths0da46a62007-10-20 20:23:07 +0000890/* do_select() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +0000891static abi_long do_select(int n,
ths26edcf42007-12-09 02:25:24 +0000892 abi_ulong rfd_addr, abi_ulong wfd_addr,
893 abi_ulong efd_addr, abi_ulong target_tv_addr)
bellard31e31b82003-02-18 22:55:36 +0000894{
895 fd_set rfds, wfds, efds;
896 fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
897 struct timeval tv, *tv_ptr;
blueswir1992f48a2007-10-14 16:27:31 +0000898 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +0000899
ths26edcf42007-12-09 02:25:24 +0000900 if (rfd_addr) {
901 if (copy_from_user_fdset(&rfds, rfd_addr, n))
902 return -TARGET_EFAULT;
903 rfds_ptr = &rfds;
pbrook53a59602006-03-25 19:31:22 +0000904 } else {
pbrook53a59602006-03-25 19:31:22 +0000905 rfds_ptr = NULL;
906 }
ths26edcf42007-12-09 02:25:24 +0000907 if (wfd_addr) {
908 if (copy_from_user_fdset(&wfds, wfd_addr, n))
909 return -TARGET_EFAULT;
910 wfds_ptr = &wfds;
pbrook53a59602006-03-25 19:31:22 +0000911 } else {
pbrook53a59602006-03-25 19:31:22 +0000912 wfds_ptr = NULL;
913 }
ths26edcf42007-12-09 02:25:24 +0000914 if (efd_addr) {
915 if (copy_from_user_fdset(&efds, efd_addr, n))
916 return -TARGET_EFAULT;
917 efds_ptr = &efds;
pbrook53a59602006-03-25 19:31:22 +0000918 } else {
pbrook53a59602006-03-25 19:31:22 +0000919 efds_ptr = NULL;
920 }
ths3b46e622007-09-17 08:09:54 +0000921
ths26edcf42007-12-09 02:25:24 +0000922 if (target_tv_addr) {
ths788f5ec2007-12-09 02:37:05 +0000923 if (copy_from_user_timeval(&tv, target_tv_addr))
924 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000925 tv_ptr = &tv;
926 } else {
927 tv_ptr = NULL;
928 }
ths26edcf42007-12-09 02:25:24 +0000929
bellard31e31b82003-02-18 22:55:36 +0000930 ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
pbrook53a59602006-03-25 19:31:22 +0000931
ths26edcf42007-12-09 02:25:24 +0000932 if (!is_error(ret)) {
933 if (rfd_addr && copy_to_user_fdset(rfd_addr, &rfds, n))
934 return -TARGET_EFAULT;
935 if (wfd_addr && copy_to_user_fdset(wfd_addr, &wfds, n))
936 return -TARGET_EFAULT;
937 if (efd_addr && copy_to_user_fdset(efd_addr, &efds, n))
938 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000939
ths788f5ec2007-12-09 02:37:05 +0000940 if (target_tv_addr && copy_to_user_timeval(target_tv_addr, &tv))
941 return -TARGET_EFAULT;
bellard31e31b82003-02-18 22:55:36 +0000942 }
bellard579a97f2007-11-11 14:26:47 +0000943
bellard31e31b82003-02-18 22:55:36 +0000944 return ret;
945}
946
bellard579a97f2007-11-11 14:26:47 +0000947static inline abi_long target_to_host_sockaddr(struct sockaddr *addr,
948 abi_ulong target_addr,
949 socklen_t len)
bellard7854b052003-03-29 17:22:23 +0000950{
aurel32607175e2009-04-15 16:11:59 +0000951 const socklen_t unix_maxlen = sizeof (struct sockaddr_un);
952 sa_family_t sa_family;
pbrook53a59602006-03-25 19:31:22 +0000953 struct target_sockaddr *target_saddr;
954
bellard579a97f2007-11-11 14:26:47 +0000955 target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
956 if (!target_saddr)
957 return -TARGET_EFAULT;
aurel32607175e2009-04-15 16:11:59 +0000958
959 sa_family = tswap16(target_saddr->sa_family);
960
961 /* Oops. The caller might send a incomplete sun_path; sun_path
962 * must be terminated by \0 (see the manual page), but
963 * unfortunately it is quite common to specify sockaddr_un
964 * length as "strlen(x->sun_path)" while it should be
965 * "strlen(...) + 1". We'll fix that here if needed.
966 * Linux kernel has a similar feature.
967 */
968
969 if (sa_family == AF_UNIX) {
970 if (len < unix_maxlen && len > 0) {
971 char *cp = (char*)target_saddr;
972
973 if ( cp[len-1] && !cp[len] )
974 len++;
975 }
976 if (len > unix_maxlen)
977 len = unix_maxlen;
978 }
979
pbrook53a59602006-03-25 19:31:22 +0000980 memcpy(addr, target_saddr, len);
aurel32607175e2009-04-15 16:11:59 +0000981 addr->sa_family = sa_family;
pbrook53a59602006-03-25 19:31:22 +0000982 unlock_user(target_saddr, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +0000983
984 return 0;
bellard7854b052003-03-29 17:22:23 +0000985}
986
bellard579a97f2007-11-11 14:26:47 +0000987static inline abi_long host_to_target_sockaddr(abi_ulong target_addr,
988 struct sockaddr *addr,
989 socklen_t len)
bellard7854b052003-03-29 17:22:23 +0000990{
pbrook53a59602006-03-25 19:31:22 +0000991 struct target_sockaddr *target_saddr;
992
bellard579a97f2007-11-11 14:26:47 +0000993 target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
994 if (!target_saddr)
995 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +0000996 memcpy(target_saddr, addr, len);
997 target_saddr->sa_family = tswap16(addr->sa_family);
998 unlock_user(target_saddr, target_addr, len);
bellard579a97f2007-11-11 14:26:47 +0000999
1000 return 0;
bellard7854b052003-03-29 17:22:23 +00001001}
1002
pbrook53a59602006-03-25 19:31:22 +00001003/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +00001004static inline abi_long target_to_host_cmsg(struct msghdr *msgh,
1005 struct target_msghdr *target_msgh)
bellard7854b052003-03-29 17:22:23 +00001006{
1007 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001008 abi_long msg_controllen;
1009 abi_ulong target_cmsg_addr;
1010 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001011 socklen_t space = 0;
bellard5a4a8982007-11-11 17:39:18 +00001012
1013 msg_controllen = tswapl(target_msgh->msg_controllen);
1014 if (msg_controllen < sizeof (struct target_cmsghdr))
1015 goto the_end;
1016 target_cmsg_addr = tswapl(target_msgh->msg_control);
1017 target_cmsg = lock_user(VERIFY_READ, target_cmsg_addr, msg_controllen, 1);
1018 if (!target_cmsg)
1019 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001020
1021 while (cmsg && target_cmsg) {
1022 void *data = CMSG_DATA(cmsg);
1023 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1024
ths5fafdf22007-09-16 21:08:06 +00001025 int len = tswapl(target_cmsg->cmsg_len)
bellard7854b052003-03-29 17:22:23 +00001026 - TARGET_CMSG_ALIGN(sizeof (struct target_cmsghdr));
1027
1028 space += CMSG_SPACE(len);
1029 if (space > msgh->msg_controllen) {
1030 space -= CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001031 gemu_log("Host cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001032 break;
1033 }
1034
1035 cmsg->cmsg_level = tswap32(target_cmsg->cmsg_level);
1036 cmsg->cmsg_type = tswap32(target_cmsg->cmsg_type);
1037 cmsg->cmsg_len = CMSG_LEN(len);
1038
bellard3532fa72006-06-24 15:06:03 +00001039 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001040 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1041 memcpy(data, target_data, len);
1042 } else {
1043 int *fd = (int *)data;
1044 int *target_fd = (int *)target_data;
1045 int i, numfds = len / sizeof(int);
1046
1047 for (i = 0; i < numfds; i++)
1048 fd[i] = tswap32(target_fd[i]);
1049 }
1050
1051 cmsg = CMSG_NXTHDR(msgh, cmsg);
1052 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1053 }
bellard5a4a8982007-11-11 17:39:18 +00001054 unlock_user(target_cmsg, target_cmsg_addr, 0);
1055 the_end:
bellard7854b052003-03-29 17:22:23 +00001056 msgh->msg_controllen = space;
bellard5a4a8982007-11-11 17:39:18 +00001057 return 0;
bellard7854b052003-03-29 17:22:23 +00001058}
1059
pbrook53a59602006-03-25 19:31:22 +00001060/* ??? Should this also swap msgh->name? */
bellard5a4a8982007-11-11 17:39:18 +00001061static inline abi_long host_to_target_cmsg(struct target_msghdr *target_msgh,
1062 struct msghdr *msgh)
bellard7854b052003-03-29 17:22:23 +00001063{
1064 struct cmsghdr *cmsg = CMSG_FIRSTHDR(msgh);
bellard5a4a8982007-11-11 17:39:18 +00001065 abi_long msg_controllen;
1066 abi_ulong target_cmsg_addr;
1067 struct target_cmsghdr *target_cmsg;
bellard7854b052003-03-29 17:22:23 +00001068 socklen_t space = 0;
1069
bellard5a4a8982007-11-11 17:39:18 +00001070 msg_controllen = tswapl(target_msgh->msg_controllen);
1071 if (msg_controllen < sizeof (struct target_cmsghdr))
1072 goto the_end;
1073 target_cmsg_addr = tswapl(target_msgh->msg_control);
1074 target_cmsg = lock_user(VERIFY_WRITE, target_cmsg_addr, msg_controllen, 0);
1075 if (!target_cmsg)
1076 return -TARGET_EFAULT;
1077
bellard7854b052003-03-29 17:22:23 +00001078 while (cmsg && target_cmsg) {
1079 void *data = CMSG_DATA(cmsg);
1080 void *target_data = TARGET_CMSG_DATA(target_cmsg);
1081
1082 int len = cmsg->cmsg_len - CMSG_ALIGN(sizeof (struct cmsghdr));
1083
1084 space += TARGET_CMSG_SPACE(len);
bellard5a4a8982007-11-11 17:39:18 +00001085 if (space > msg_controllen) {
bellard7854b052003-03-29 17:22:23 +00001086 space -= TARGET_CMSG_SPACE(len);
bellard31febb72005-12-18 20:03:27 +00001087 gemu_log("Target cmsg overflow\n");
bellard7854b052003-03-29 17:22:23 +00001088 break;
1089 }
1090
1091 target_cmsg->cmsg_level = tswap32(cmsg->cmsg_level);
1092 target_cmsg->cmsg_type = tswap32(cmsg->cmsg_type);
1093 target_cmsg->cmsg_len = tswapl(TARGET_CMSG_LEN(len));
1094
bellard3532fa72006-06-24 15:06:03 +00001095 if (cmsg->cmsg_level != TARGET_SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
bellard7854b052003-03-29 17:22:23 +00001096 gemu_log("Unsupported ancillary data: %d/%d\n", cmsg->cmsg_level, cmsg->cmsg_type);
1097 memcpy(target_data, data, len);
1098 } else {
1099 int *fd = (int *)data;
1100 int *target_fd = (int *)target_data;
1101 int i, numfds = len / sizeof(int);
1102
1103 for (i = 0; i < numfds; i++)
1104 target_fd[i] = tswap32(fd[i]);
1105 }
1106
1107 cmsg = CMSG_NXTHDR(msgh, cmsg);
1108 target_cmsg = TARGET_CMSG_NXTHDR(target_msgh, target_cmsg);
1109 }
bellard5a4a8982007-11-11 17:39:18 +00001110 unlock_user(target_cmsg, target_cmsg_addr, space);
1111 the_end:
1112 target_msgh->msg_controllen = tswapl(space);
1113 return 0;
bellard7854b052003-03-29 17:22:23 +00001114}
1115
ths0da46a62007-10-20 20:23:07 +00001116/* do_setsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001117static abi_long do_setsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001118 abi_ulong optval_addr, socklen_t optlen)
bellard7854b052003-03-29 17:22:23 +00001119{
blueswir1992f48a2007-10-14 16:27:31 +00001120 abi_long ret;
j_mayer32407102007-09-26 23:01:49 +00001121 int val;
ths3b46e622007-09-17 08:09:54 +00001122
bellard8853f862004-02-22 14:57:26 +00001123 switch(level) {
1124 case SOL_TCP:
bellard7854b052003-03-29 17:22:23 +00001125 /* TCP options all take an 'int' value. */
bellard7854b052003-03-29 17:22:23 +00001126 if (optlen < sizeof(uint32_t))
ths0da46a62007-10-20 20:23:07 +00001127 return -TARGET_EINVAL;
ths3b46e622007-09-17 08:09:54 +00001128
bellard2f619692007-11-16 10:46:05 +00001129 if (get_user_u32(val, optval_addr))
1130 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001131 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1132 break;
1133 case SOL_IP:
1134 switch(optname) {
bellard2efbe912005-07-23 15:10:20 +00001135 case IP_TOS:
1136 case IP_TTL:
bellard8853f862004-02-22 14:57:26 +00001137 case IP_HDRINCL:
bellard2efbe912005-07-23 15:10:20 +00001138 case IP_ROUTER_ALERT:
1139 case IP_RECVOPTS:
1140 case IP_RETOPTS:
1141 case IP_PKTINFO:
1142 case IP_MTU_DISCOVER:
1143 case IP_RECVERR:
1144 case IP_RECVTOS:
1145#ifdef IP_FREEBIND
1146 case IP_FREEBIND:
1147#endif
1148 case IP_MULTICAST_TTL:
1149 case IP_MULTICAST_LOOP:
bellard8853f862004-02-22 14:57:26 +00001150 val = 0;
1151 if (optlen >= sizeof(uint32_t)) {
bellard2f619692007-11-16 10:46:05 +00001152 if (get_user_u32(val, optval_addr))
1153 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001154 } else if (optlen >= 1) {
bellard2f619692007-11-16 10:46:05 +00001155 if (get_user_u8(val, optval_addr))
1156 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001157 }
1158 ret = get_errno(setsockopt(sockfd, level, optname, &val, sizeof(val)));
1159 break;
1160 default:
1161 goto unimplemented;
1162 }
1163 break;
bellard3532fa72006-06-24 15:06:03 +00001164 case TARGET_SOL_SOCKET:
bellard8853f862004-02-22 14:57:26 +00001165 switch (optname) {
1166 /* Options with 'int' argument. */
bellard3532fa72006-06-24 15:06:03 +00001167 case TARGET_SO_DEBUG:
1168 optname = SO_DEBUG;
1169 break;
1170 case TARGET_SO_REUSEADDR:
1171 optname = SO_REUSEADDR;
1172 break;
1173 case TARGET_SO_TYPE:
1174 optname = SO_TYPE;
1175 break;
1176 case TARGET_SO_ERROR:
1177 optname = SO_ERROR;
1178 break;
1179 case TARGET_SO_DONTROUTE:
1180 optname = SO_DONTROUTE;
1181 break;
1182 case TARGET_SO_BROADCAST:
1183 optname = SO_BROADCAST;
1184 break;
1185 case TARGET_SO_SNDBUF:
1186 optname = SO_SNDBUF;
1187 break;
1188 case TARGET_SO_RCVBUF:
1189 optname = SO_RCVBUF;
1190 break;
1191 case TARGET_SO_KEEPALIVE:
1192 optname = SO_KEEPALIVE;
1193 break;
1194 case TARGET_SO_OOBINLINE:
1195 optname = SO_OOBINLINE;
1196 break;
1197 case TARGET_SO_NO_CHECK:
1198 optname = SO_NO_CHECK;
1199 break;
1200 case TARGET_SO_PRIORITY:
1201 optname = SO_PRIORITY;
1202 break;
bellard5e83e8e2005-03-01 22:32:06 +00001203#ifdef SO_BSDCOMPAT
bellard3532fa72006-06-24 15:06:03 +00001204 case TARGET_SO_BSDCOMPAT:
1205 optname = SO_BSDCOMPAT;
1206 break;
bellard5e83e8e2005-03-01 22:32:06 +00001207#endif
bellard3532fa72006-06-24 15:06:03 +00001208 case TARGET_SO_PASSCRED:
1209 optname = SO_PASSCRED;
1210 break;
1211 case TARGET_SO_TIMESTAMP:
1212 optname = SO_TIMESTAMP;
1213 break;
1214 case TARGET_SO_RCVLOWAT:
1215 optname = SO_RCVLOWAT;
1216 break;
1217 case TARGET_SO_RCVTIMEO:
1218 optname = SO_RCVTIMEO;
1219 break;
1220 case TARGET_SO_SNDTIMEO:
1221 optname = SO_SNDTIMEO;
1222 break;
bellard8853f862004-02-22 14:57:26 +00001223 break;
1224 default:
1225 goto unimplemented;
1226 }
bellard3532fa72006-06-24 15:06:03 +00001227 if (optlen < sizeof(uint32_t))
bellard2f619692007-11-16 10:46:05 +00001228 return -TARGET_EINVAL;
bellard3532fa72006-06-24 15:06:03 +00001229
bellard2f619692007-11-16 10:46:05 +00001230 if (get_user_u32(val, optval_addr))
1231 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001232 ret = get_errno(setsockopt(sockfd, SOL_SOCKET, optname, &val, sizeof(val)));
bellard8853f862004-02-22 14:57:26 +00001233 break;
bellard7854b052003-03-29 17:22:23 +00001234 default:
bellard8853f862004-02-22 14:57:26 +00001235 unimplemented:
1236 gemu_log("Unsupported setsockopt level=%d optname=%d \n", level, optname);
ths6fa13c12007-12-18 02:41:04 +00001237 ret = -TARGET_ENOPROTOOPT;
bellard7854b052003-03-29 17:22:23 +00001238 }
bellard8853f862004-02-22 14:57:26 +00001239 return ret;
bellard7854b052003-03-29 17:22:23 +00001240}
1241
ths0da46a62007-10-20 20:23:07 +00001242/* do_getsockopt() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001243static abi_long do_getsockopt(int sockfd, int level, int optname,
bellard2f619692007-11-16 10:46:05 +00001244 abi_ulong optval_addr, abi_ulong optlen)
bellard7854b052003-03-29 17:22:23 +00001245{
blueswir1992f48a2007-10-14 16:27:31 +00001246 abi_long ret;
blueswir1b55266b2008-09-20 08:07:15 +00001247 int len, val;
1248 socklen_t lv;
bellard8853f862004-02-22 14:57:26 +00001249
1250 switch(level) {
bellard3532fa72006-06-24 15:06:03 +00001251 case TARGET_SOL_SOCKET:
1252 level = SOL_SOCKET;
bellard8853f862004-02-22 14:57:26 +00001253 switch (optname) {
bellard3532fa72006-06-24 15:06:03 +00001254 case TARGET_SO_LINGER:
1255 case TARGET_SO_RCVTIMEO:
1256 case TARGET_SO_SNDTIMEO:
1257 case TARGET_SO_PEERCRED:
1258 case TARGET_SO_PEERNAME:
bellard8853f862004-02-22 14:57:26 +00001259 /* These don't just return a single integer */
1260 goto unimplemented;
1261 default:
bellard2efbe912005-07-23 15:10:20 +00001262 goto int_case;
1263 }
1264 break;
1265 case SOL_TCP:
1266 /* TCP options all take an 'int' value. */
1267 int_case:
bellard2f619692007-11-16 10:46:05 +00001268 if (get_user_u32(len, optlen))
1269 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001270 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001271 return -TARGET_EINVAL;
bellard2efbe912005-07-23 15:10:20 +00001272 lv = sizeof(int);
1273 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1274 if (ret < 0)
1275 return ret;
1276 val = tswap32(val);
1277 if (len > lv)
1278 len = lv;
bellard2f619692007-11-16 10:46:05 +00001279 if (len == 4) {
1280 if (put_user_u32(val, optval_addr))
1281 return -TARGET_EFAULT;
1282 } else {
1283 if (put_user_u8(val, optval_addr))
1284 return -TARGET_EFAULT;
1285 }
1286 if (put_user_u32(len, optlen))
1287 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001288 break;
1289 case SOL_IP:
1290 switch(optname) {
1291 case IP_TOS:
1292 case IP_TTL:
1293 case IP_HDRINCL:
1294 case IP_ROUTER_ALERT:
1295 case IP_RECVOPTS:
1296 case IP_RETOPTS:
1297 case IP_PKTINFO:
1298 case IP_MTU_DISCOVER:
1299 case IP_RECVERR:
1300 case IP_RECVTOS:
1301#ifdef IP_FREEBIND
1302 case IP_FREEBIND:
1303#endif
1304 case IP_MULTICAST_TTL:
1305 case IP_MULTICAST_LOOP:
bellard2f619692007-11-16 10:46:05 +00001306 if (get_user_u32(len, optlen))
1307 return -TARGET_EFAULT;
bellard8853f862004-02-22 14:57:26 +00001308 if (len < 0)
ths0da46a62007-10-20 20:23:07 +00001309 return -TARGET_EINVAL;
bellard8853f862004-02-22 14:57:26 +00001310 lv = sizeof(int);
1311 ret = get_errno(getsockopt(sockfd, level, optname, &val, &lv));
1312 if (ret < 0)
1313 return ret;
bellard2efbe912005-07-23 15:10:20 +00001314 if (len < sizeof(int) && len > 0 && val >= 0 && val < 255) {
bellard2efbe912005-07-23 15:10:20 +00001315 len = 1;
bellard2f619692007-11-16 10:46:05 +00001316 if (put_user_u32(len, optlen)
1317 || put_user_u8(val, optval_addr))
1318 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001319 } else {
bellard2efbe912005-07-23 15:10:20 +00001320 if (len > sizeof(int))
1321 len = sizeof(int);
bellard2f619692007-11-16 10:46:05 +00001322 if (put_user_u32(len, optlen)
1323 || put_user_u32(val, optval_addr))
1324 return -TARGET_EFAULT;
bellard2efbe912005-07-23 15:10:20 +00001325 }
bellard8853f862004-02-22 14:57:26 +00001326 break;
bellard2efbe912005-07-23 15:10:20 +00001327 default:
thsc02f4992007-12-18 02:39:59 +00001328 ret = -TARGET_ENOPROTOOPT;
1329 break;
bellard8853f862004-02-22 14:57:26 +00001330 }
1331 break;
1332 default:
1333 unimplemented:
1334 gemu_log("getsockopt level=%d optname=%d not yet supported\n",
1335 level, optname);
thsc02f4992007-12-18 02:39:59 +00001336 ret = -TARGET_EOPNOTSUPP;
bellard8853f862004-02-22 14:57:26 +00001337 break;
1338 }
1339 return ret;
bellard7854b052003-03-29 17:22:23 +00001340}
1341
bellard579a97f2007-11-11 14:26:47 +00001342/* FIXME
1343 * lock_iovec()/unlock_iovec() have a return code of 0 for success where
1344 * other lock functions have a return code of 0 for failure.
1345 */
1346static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
1347 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001348{
1349 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001350 abi_ulong base;
balrogd732dcb2008-10-28 10:21:03 +00001351 int i;
pbrook53a59602006-03-25 19:31:22 +00001352
bellard579a97f2007-11-11 14:26:47 +00001353 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1354 if (!target_vec)
1355 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001356 for(i = 0;i < count; i++) {
1357 base = tswapl(target_vec[i].iov_base);
1358 vec[i].iov_len = tswapl(target_vec[i].iov_len);
bellard41df8412008-02-04 22:26:57 +00001359 if (vec[i].iov_len != 0) {
1360 vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
balrogd732dcb2008-10-28 10:21:03 +00001361 /* Don't check lock_user return value. We must call writev even
1362 if a element has invalid base address. */
bellard41df8412008-02-04 22:26:57 +00001363 } else {
1364 /* zero length pointer is ignored */
1365 vec[i].iov_base = NULL;
1366 }
pbrook53a59602006-03-25 19:31:22 +00001367 }
1368 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001369 return 0;
pbrook53a59602006-03-25 19:31:22 +00001370}
1371
bellard579a97f2007-11-11 14:26:47 +00001372static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
1373 int count, int copy)
pbrook53a59602006-03-25 19:31:22 +00001374{
1375 struct target_iovec *target_vec;
blueswir1992f48a2007-10-14 16:27:31 +00001376 abi_ulong base;
pbrook53a59602006-03-25 19:31:22 +00001377 int i;
1378
bellard579a97f2007-11-11 14:26:47 +00001379 target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
1380 if (!target_vec)
1381 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00001382 for(i = 0;i < count; i++) {
balrogd732dcb2008-10-28 10:21:03 +00001383 if (target_vec[i].iov_base) {
1384 base = tswapl(target_vec[i].iov_base);
1385 unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
1386 }
pbrook53a59602006-03-25 19:31:22 +00001387 }
1388 unlock_user (target_vec, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001389
1390 return 0;
pbrook53a59602006-03-25 19:31:22 +00001391}
1392
ths0da46a62007-10-20 20:23:07 +00001393/* do_socket() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001394static abi_long do_socket(int domain, int type, int protocol)
bellard3532fa72006-06-24 15:06:03 +00001395{
1396#if defined(TARGET_MIPS)
1397 switch(type) {
1398 case TARGET_SOCK_DGRAM:
1399 type = SOCK_DGRAM;
1400 break;
1401 case TARGET_SOCK_STREAM:
1402 type = SOCK_STREAM;
1403 break;
1404 case TARGET_SOCK_RAW:
1405 type = SOCK_RAW;
1406 break;
1407 case TARGET_SOCK_RDM:
1408 type = SOCK_RDM;
1409 break;
1410 case TARGET_SOCK_SEQPACKET:
1411 type = SOCK_SEQPACKET;
1412 break;
1413 case TARGET_SOCK_PACKET:
1414 type = SOCK_PACKET;
1415 break;
1416 }
1417#endif
balrog12bc92a2007-10-30 21:06:14 +00001418 if (domain == PF_NETLINK)
1419 return -EAFNOSUPPORT; /* do not NETLINK socket connections possible */
bellard3532fa72006-06-24 15:06:03 +00001420 return get_errno(socket(domain, type, protocol));
1421}
1422
ths0da46a62007-10-20 20:23:07 +00001423/* do_bind() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001424static abi_long do_bind(int sockfd, abi_ulong target_addr,
1425 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001426{
aurel328f7aeaf2009-01-30 19:47:57 +00001427 void *addr;
1428
aurel32be09ac42009-04-15 16:12:06 +00001429 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001430 return -TARGET_EINVAL;
1431
aurel32607175e2009-04-15 16:11:59 +00001432 addr = alloca(addrlen+1);
ths3b46e622007-09-17 08:09:54 +00001433
bellard3532fa72006-06-24 15:06:03 +00001434 target_to_host_sockaddr(addr, target_addr, addrlen);
1435 return get_errno(bind(sockfd, addr, addrlen));
1436}
1437
ths0da46a62007-10-20 20:23:07 +00001438/* do_connect() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001439static abi_long do_connect(int sockfd, abi_ulong target_addr,
1440 socklen_t addrlen)
bellard3532fa72006-06-24 15:06:03 +00001441{
aurel328f7aeaf2009-01-30 19:47:57 +00001442 void *addr;
1443
aurel32be09ac42009-04-15 16:12:06 +00001444 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001445 return -TARGET_EINVAL;
1446
1447 addr = alloca(addrlen);
ths3b46e622007-09-17 08:09:54 +00001448
bellard3532fa72006-06-24 15:06:03 +00001449 target_to_host_sockaddr(addr, target_addr, addrlen);
1450 return get_errno(connect(sockfd, addr, addrlen));
1451}
1452
ths0da46a62007-10-20 20:23:07 +00001453/* do_sendrecvmsg() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001454static abi_long do_sendrecvmsg(int fd, abi_ulong target_msg,
1455 int flags, int send)
bellard3532fa72006-06-24 15:06:03 +00001456{
balrog6de645c2008-10-28 10:26:29 +00001457 abi_long ret, len;
bellard3532fa72006-06-24 15:06:03 +00001458 struct target_msghdr *msgp;
1459 struct msghdr msg;
1460 int count;
1461 struct iovec *vec;
blueswir1992f48a2007-10-14 16:27:31 +00001462 abi_ulong target_vec;
bellard3532fa72006-06-24 15:06:03 +00001463
bellard579a97f2007-11-11 14:26:47 +00001464 /* FIXME */
1465 if (!lock_user_struct(send ? VERIFY_READ : VERIFY_WRITE,
1466 msgp,
1467 target_msg,
1468 send ? 1 : 0))
1469 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001470 if (msgp->msg_name) {
1471 msg.msg_namelen = tswap32(msgp->msg_namelen);
1472 msg.msg_name = alloca(msg.msg_namelen);
1473 target_to_host_sockaddr(msg.msg_name, tswapl(msgp->msg_name),
1474 msg.msg_namelen);
1475 } else {
1476 msg.msg_name = NULL;
1477 msg.msg_namelen = 0;
1478 }
1479 msg.msg_controllen = 2 * tswapl(msgp->msg_controllen);
1480 msg.msg_control = alloca(msg.msg_controllen);
1481 msg.msg_flags = tswap32(msgp->msg_flags);
ths3b46e622007-09-17 08:09:54 +00001482
bellard3532fa72006-06-24 15:06:03 +00001483 count = tswapl(msgp->msg_iovlen);
1484 vec = alloca(count * sizeof(struct iovec));
1485 target_vec = tswapl(msgp->msg_iov);
bellard579a97f2007-11-11 14:26:47 +00001486 lock_iovec(send ? VERIFY_READ : VERIFY_WRITE, vec, target_vec, count, send);
bellard3532fa72006-06-24 15:06:03 +00001487 msg.msg_iovlen = count;
1488 msg.msg_iov = vec;
ths3b46e622007-09-17 08:09:54 +00001489
bellard3532fa72006-06-24 15:06:03 +00001490 if (send) {
bellard5a4a8982007-11-11 17:39:18 +00001491 ret = target_to_host_cmsg(&msg, msgp);
1492 if (ret == 0)
1493 ret = get_errno(sendmsg(fd, &msg, flags));
bellard3532fa72006-06-24 15:06:03 +00001494 } else {
1495 ret = get_errno(recvmsg(fd, &msg, flags));
balrog6de645c2008-10-28 10:26:29 +00001496 if (!is_error(ret)) {
1497 len = ret;
bellard5a4a8982007-11-11 17:39:18 +00001498 ret = host_to_target_cmsg(msgp, &msg);
balrog6de645c2008-10-28 10:26:29 +00001499 if (!is_error(ret))
1500 ret = len;
1501 }
bellard3532fa72006-06-24 15:06:03 +00001502 }
1503 unlock_iovec(vec, target_vec, count, !send);
bellard579a97f2007-11-11 14:26:47 +00001504 unlock_user_struct(msgp, target_msg, send ? 0 : 1);
bellard3532fa72006-06-24 15:06:03 +00001505 return ret;
1506}
1507
ths0da46a62007-10-20 20:23:07 +00001508/* do_accept() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001509static abi_long do_accept(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001510 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001511{
bellard2f619692007-11-16 10:46:05 +00001512 socklen_t addrlen;
1513 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001514 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001515
bellard2f619692007-11-16 10:46:05 +00001516 if (get_user_u32(addrlen, target_addrlen_addr))
1517 return -TARGET_EFAULT;
1518
aurel32be09ac42009-04-15 16:12:06 +00001519 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001520 return -TARGET_EINVAL;
1521
bellard2f619692007-11-16 10:46:05 +00001522 addr = alloca(addrlen);
1523
pbrook1be9e1d2006-11-19 15:26:04 +00001524 ret = get_errno(accept(fd, addr, &addrlen));
1525 if (!is_error(ret)) {
1526 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001527 if (put_user_u32(addrlen, target_addrlen_addr))
1528 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001529 }
1530 return ret;
1531}
1532
ths0da46a62007-10-20 20:23:07 +00001533/* do_getpeername() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001534static abi_long do_getpeername(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001535 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001536{
bellard2f619692007-11-16 10:46:05 +00001537 socklen_t addrlen;
1538 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001539 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001540
bellard2f619692007-11-16 10:46:05 +00001541 if (get_user_u32(addrlen, target_addrlen_addr))
1542 return -TARGET_EFAULT;
1543
aurel32be09ac42009-04-15 16:12:06 +00001544 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001545 return -TARGET_EINVAL;
1546
bellard2f619692007-11-16 10:46:05 +00001547 addr = alloca(addrlen);
1548
pbrook1be9e1d2006-11-19 15:26:04 +00001549 ret = get_errno(getpeername(fd, addr, &addrlen));
1550 if (!is_error(ret)) {
1551 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001552 if (put_user_u32(addrlen, target_addrlen_addr))
1553 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001554 }
1555 return ret;
1556}
1557
ths0da46a62007-10-20 20:23:07 +00001558/* do_getsockname() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001559static abi_long do_getsockname(int fd, abi_ulong target_addr,
bellard2f619692007-11-16 10:46:05 +00001560 abi_ulong target_addrlen_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001561{
bellard2f619692007-11-16 10:46:05 +00001562 socklen_t addrlen;
1563 void *addr;
blueswir1992f48a2007-10-14 16:27:31 +00001564 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001565
aurel328fea3602009-01-30 19:47:47 +00001566 if (target_addr == 0)
1567 return get_errno(accept(fd, NULL, NULL));
1568
bellard2f619692007-11-16 10:46:05 +00001569 if (get_user_u32(addrlen, target_addrlen_addr))
1570 return -TARGET_EFAULT;
1571
aurel32be09ac42009-04-15 16:12:06 +00001572 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001573 return -TARGET_EINVAL;
1574
bellard2f619692007-11-16 10:46:05 +00001575 addr = alloca(addrlen);
1576
pbrook1be9e1d2006-11-19 15:26:04 +00001577 ret = get_errno(getsockname(fd, addr, &addrlen));
1578 if (!is_error(ret)) {
1579 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001580 if (put_user_u32(addrlen, target_addrlen_addr))
1581 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001582 }
1583 return ret;
1584}
1585
ths0da46a62007-10-20 20:23:07 +00001586/* do_socketpair() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001587static abi_long do_socketpair(int domain, int type, int protocol,
bellard2f619692007-11-16 10:46:05 +00001588 abi_ulong target_tab_addr)
pbrook1be9e1d2006-11-19 15:26:04 +00001589{
1590 int tab[2];
blueswir1992f48a2007-10-14 16:27:31 +00001591 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001592
1593 ret = get_errno(socketpair(domain, type, protocol, tab));
1594 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00001595 if (put_user_s32(tab[0], target_tab_addr)
1596 || put_user_s32(tab[1], target_tab_addr + sizeof(tab[0])))
1597 ret = -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001598 }
1599 return ret;
1600}
1601
ths0da46a62007-10-20 20:23:07 +00001602/* do_sendto() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001603static abi_long do_sendto(int fd, abi_ulong msg, size_t len, int flags,
1604 abi_ulong target_addr, socklen_t addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001605{
1606 void *addr;
1607 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001608 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001609
aurel32be09ac42009-04-15 16:12:06 +00001610 if (addrlen < 0)
aurel328f7aeaf2009-01-30 19:47:57 +00001611 return -TARGET_EINVAL;
1612
bellard579a97f2007-11-11 14:26:47 +00001613 host_msg = lock_user(VERIFY_READ, msg, len, 1);
1614 if (!host_msg)
1615 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001616 if (target_addr) {
1617 addr = alloca(addrlen);
1618 target_to_host_sockaddr(addr, target_addr, addrlen);
1619 ret = get_errno(sendto(fd, host_msg, len, flags, addr, addrlen));
1620 } else {
1621 ret = get_errno(send(fd, host_msg, len, flags));
1622 }
1623 unlock_user(host_msg, msg, 0);
1624 return ret;
1625}
1626
ths0da46a62007-10-20 20:23:07 +00001627/* do_recvfrom() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001628static abi_long do_recvfrom(int fd, abi_ulong msg, size_t len, int flags,
1629 abi_ulong target_addr,
1630 abi_ulong target_addrlen)
pbrook1be9e1d2006-11-19 15:26:04 +00001631{
1632 socklen_t addrlen;
1633 void *addr;
1634 void *host_msg;
blueswir1992f48a2007-10-14 16:27:31 +00001635 abi_long ret;
pbrook1be9e1d2006-11-19 15:26:04 +00001636
bellard579a97f2007-11-11 14:26:47 +00001637 host_msg = lock_user(VERIFY_WRITE, msg, len, 0);
1638 if (!host_msg)
1639 return -TARGET_EFAULT;
pbrook1be9e1d2006-11-19 15:26:04 +00001640 if (target_addr) {
bellard2f619692007-11-16 10:46:05 +00001641 if (get_user_u32(addrlen, target_addrlen)) {
1642 ret = -TARGET_EFAULT;
1643 goto fail;
1644 }
aurel32be09ac42009-04-15 16:12:06 +00001645 if (addrlen < 0) {
aurel328f7aeaf2009-01-30 19:47:57 +00001646 ret = -TARGET_EINVAL;
1647 goto fail;
1648 }
pbrook1be9e1d2006-11-19 15:26:04 +00001649 addr = alloca(addrlen);
1650 ret = get_errno(recvfrom(fd, host_msg, len, flags, addr, &addrlen));
1651 } else {
1652 addr = NULL; /* To keep compiler quiet. */
1653 ret = get_errno(recv(fd, host_msg, len, flags));
1654 }
1655 if (!is_error(ret)) {
1656 if (target_addr) {
1657 host_to_target_sockaddr(target_addr, addr, addrlen);
bellard2f619692007-11-16 10:46:05 +00001658 if (put_user_u32(addrlen, target_addrlen)) {
1659 ret = -TARGET_EFAULT;
1660 goto fail;
1661 }
pbrook1be9e1d2006-11-19 15:26:04 +00001662 }
1663 unlock_user(host_msg, msg, len);
1664 } else {
bellard2f619692007-11-16 10:46:05 +00001665fail:
pbrook1be9e1d2006-11-19 15:26:04 +00001666 unlock_user(host_msg, msg, 0);
1667 }
1668 return ret;
1669}
1670
j_mayer32407102007-09-26 23:01:49 +00001671#ifdef TARGET_NR_socketcall
ths0da46a62007-10-20 20:23:07 +00001672/* do_socketcall() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00001673static abi_long do_socketcall(int num, abi_ulong vptr)
bellard31e31b82003-02-18 22:55:36 +00001674{
blueswir1992f48a2007-10-14 16:27:31 +00001675 abi_long ret;
1676 const int n = sizeof(abi_ulong);
bellard31e31b82003-02-18 22:55:36 +00001677
1678 switch(num) {
1679 case SOCKOP_socket:
bellard7854b052003-03-29 17:22:23 +00001680 {
bellard2f619692007-11-16 10:46:05 +00001681 int domain, type, protocol;
1682
1683 if (get_user_s32(domain, vptr)
1684 || get_user_s32(type, vptr + n)
1685 || get_user_s32(protocol, vptr + 2 * n))
1686 return -TARGET_EFAULT;
1687
bellard3532fa72006-06-24 15:06:03 +00001688 ret = do_socket(domain, type, protocol);
bellard7854b052003-03-29 17:22:23 +00001689 }
bellard31e31b82003-02-18 22:55:36 +00001690 break;
1691 case SOCKOP_bind:
bellard7854b052003-03-29 17:22:23 +00001692 {
bellard2f619692007-11-16 10:46:05 +00001693 int sockfd;
1694 abi_ulong target_addr;
1695 socklen_t addrlen;
1696
1697 if (get_user_s32(sockfd, vptr)
1698 || get_user_ual(target_addr, vptr + n)
1699 || get_user_u32(addrlen, vptr + 2 * n))
1700 return -TARGET_EFAULT;
1701
bellard3532fa72006-06-24 15:06:03 +00001702 ret = do_bind(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001703 }
bellard31e31b82003-02-18 22:55:36 +00001704 break;
1705 case SOCKOP_connect:
bellard7854b052003-03-29 17:22:23 +00001706 {
bellard2f619692007-11-16 10:46:05 +00001707 int sockfd;
1708 abi_ulong target_addr;
1709 socklen_t addrlen;
1710
1711 if (get_user_s32(sockfd, vptr)
1712 || get_user_ual(target_addr, vptr + n)
1713 || get_user_u32(addrlen, vptr + 2 * n))
1714 return -TARGET_EFAULT;
1715
bellard3532fa72006-06-24 15:06:03 +00001716 ret = do_connect(sockfd, target_addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001717 }
bellard31e31b82003-02-18 22:55:36 +00001718 break;
1719 case SOCKOP_listen:
bellard7854b052003-03-29 17:22:23 +00001720 {
bellard2f619692007-11-16 10:46:05 +00001721 int sockfd, backlog;
1722
1723 if (get_user_s32(sockfd, vptr)
1724 || get_user_s32(backlog, vptr + n))
1725 return -TARGET_EFAULT;
1726
bellard7854b052003-03-29 17:22:23 +00001727 ret = get_errno(listen(sockfd, backlog));
1728 }
bellard31e31b82003-02-18 22:55:36 +00001729 break;
1730 case SOCKOP_accept:
1731 {
bellard2f619692007-11-16 10:46:05 +00001732 int sockfd;
1733 abi_ulong target_addr, target_addrlen;
1734
1735 if (get_user_s32(sockfd, vptr)
1736 || get_user_ual(target_addr, vptr + n)
1737 || get_user_u32(target_addrlen, vptr + 2 * n))
1738 return -TARGET_EFAULT;
1739
pbrook1be9e1d2006-11-19 15:26:04 +00001740 ret = do_accept(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001741 }
1742 break;
1743 case SOCKOP_getsockname:
1744 {
bellard2f619692007-11-16 10:46:05 +00001745 int sockfd;
1746 abi_ulong target_addr, target_addrlen;
1747
1748 if (get_user_s32(sockfd, vptr)
1749 || get_user_ual(target_addr, vptr + n)
1750 || get_user_u32(target_addrlen, vptr + 2 * n))
1751 return -TARGET_EFAULT;
1752
pbrook1be9e1d2006-11-19 15:26:04 +00001753 ret = do_getsockname(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001754 }
1755 break;
1756 case SOCKOP_getpeername:
1757 {
bellard2f619692007-11-16 10:46:05 +00001758 int sockfd;
1759 abi_ulong target_addr, target_addrlen;
1760
1761 if (get_user_s32(sockfd, vptr)
1762 || get_user_ual(target_addr, vptr + n)
1763 || get_user_u32(target_addrlen, vptr + 2 * n))
1764 return -TARGET_EFAULT;
1765
pbrook1be9e1d2006-11-19 15:26:04 +00001766 ret = do_getpeername(sockfd, target_addr, target_addrlen);
bellard31e31b82003-02-18 22:55:36 +00001767 }
1768 break;
1769 case SOCKOP_socketpair:
1770 {
bellard2f619692007-11-16 10:46:05 +00001771 int domain, type, protocol;
1772 abi_ulong tab;
1773
1774 if (get_user_s32(domain, vptr)
1775 || get_user_s32(type, vptr + n)
1776 || get_user_s32(protocol, vptr + 2 * n)
1777 || get_user_ual(tab, vptr + 3 * n))
1778 return -TARGET_EFAULT;
1779
pbrook1be9e1d2006-11-19 15:26:04 +00001780 ret = do_socketpair(domain, type, protocol, tab);
bellard31e31b82003-02-18 22:55:36 +00001781 }
1782 break;
1783 case SOCKOP_send:
bellard7854b052003-03-29 17:22:23 +00001784 {
bellard2f619692007-11-16 10:46:05 +00001785 int sockfd;
1786 abi_ulong msg;
1787 size_t len;
1788 int flags;
1789
1790 if (get_user_s32(sockfd, vptr)
1791 || get_user_ual(msg, vptr + n)
1792 || get_user_ual(len, vptr + 2 * n)
1793 || get_user_s32(flags, vptr + 3 * n))
1794 return -TARGET_EFAULT;
1795
pbrook1be9e1d2006-11-19 15:26:04 +00001796 ret = do_sendto(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001797 }
bellard31e31b82003-02-18 22:55:36 +00001798 break;
1799 case SOCKOP_recv:
bellard7854b052003-03-29 17:22:23 +00001800 {
bellard2f619692007-11-16 10:46:05 +00001801 int sockfd;
1802 abi_ulong msg;
1803 size_t len;
1804 int flags;
1805
1806 if (get_user_s32(sockfd, vptr)
1807 || get_user_ual(msg, vptr + n)
1808 || get_user_ual(len, vptr + 2 * n)
1809 || get_user_s32(flags, vptr + 3 * n))
1810 return -TARGET_EFAULT;
1811
pbrook1be9e1d2006-11-19 15:26:04 +00001812 ret = do_recvfrom(sockfd, msg, len, flags, 0, 0);
bellard7854b052003-03-29 17:22:23 +00001813 }
bellard31e31b82003-02-18 22:55:36 +00001814 break;
1815 case SOCKOP_sendto:
bellard7854b052003-03-29 17:22:23 +00001816 {
bellard2f619692007-11-16 10:46:05 +00001817 int sockfd;
1818 abi_ulong msg;
1819 size_t len;
1820 int flags;
1821 abi_ulong addr;
1822 socklen_t addrlen;
1823
1824 if (get_user_s32(sockfd, vptr)
1825 || get_user_ual(msg, vptr + n)
1826 || get_user_ual(len, vptr + 2 * n)
1827 || get_user_s32(flags, vptr + 3 * n)
1828 || get_user_ual(addr, vptr + 4 * n)
1829 || get_user_u32(addrlen, vptr + 5 * n))
1830 return -TARGET_EFAULT;
1831
pbrook1be9e1d2006-11-19 15:26:04 +00001832 ret = do_sendto(sockfd, msg, len, flags, addr, addrlen);
bellard7854b052003-03-29 17:22:23 +00001833 }
bellard31e31b82003-02-18 22:55:36 +00001834 break;
1835 case SOCKOP_recvfrom:
1836 {
bellard2f619692007-11-16 10:46:05 +00001837 int sockfd;
1838 abi_ulong msg;
1839 size_t len;
1840 int flags;
1841 abi_ulong addr;
1842 socklen_t addrlen;
1843
1844 if (get_user_s32(sockfd, vptr)
1845 || get_user_ual(msg, vptr + n)
1846 || get_user_ual(len, vptr + 2 * n)
1847 || get_user_s32(flags, vptr + 3 * n)
1848 || get_user_ual(addr, vptr + 4 * n)
1849 || get_user_u32(addrlen, vptr + 5 * n))
1850 return -TARGET_EFAULT;
1851
pbrook1be9e1d2006-11-19 15:26:04 +00001852 ret = do_recvfrom(sockfd, msg, len, flags, addr, addrlen);
bellard31e31b82003-02-18 22:55:36 +00001853 }
1854 break;
1855 case SOCKOP_shutdown:
bellard7854b052003-03-29 17:22:23 +00001856 {
bellard2f619692007-11-16 10:46:05 +00001857 int sockfd, how;
1858
1859 if (get_user_s32(sockfd, vptr)
1860 || get_user_s32(how, vptr + n))
1861 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001862
1863 ret = get_errno(shutdown(sockfd, how));
1864 }
bellard31e31b82003-02-18 22:55:36 +00001865 break;
1866 case SOCKOP_sendmsg:
1867 case SOCKOP_recvmsg:
bellard1a9353d2003-03-16 20:28:50 +00001868 {
1869 int fd;
blueswir1992f48a2007-10-14 16:27:31 +00001870 abi_ulong target_msg;
bellard3532fa72006-06-24 15:06:03 +00001871 int flags;
bellard1a9353d2003-03-16 20:28:50 +00001872
bellard2f619692007-11-16 10:46:05 +00001873 if (get_user_s32(fd, vptr)
1874 || get_user_ual(target_msg, vptr + n)
1875 || get_user_s32(flags, vptr + 2 * n))
1876 return -TARGET_EFAULT;
bellard3532fa72006-06-24 15:06:03 +00001877
ths5fafdf22007-09-16 21:08:06 +00001878 ret = do_sendrecvmsg(fd, target_msg, flags,
bellard3532fa72006-06-24 15:06:03 +00001879 (num == SOCKOP_sendmsg));
bellard1a9353d2003-03-16 20:28:50 +00001880 }
1881 break;
bellard31e31b82003-02-18 22:55:36 +00001882 case SOCKOP_setsockopt:
bellard7854b052003-03-29 17:22:23 +00001883 {
bellard2f619692007-11-16 10:46:05 +00001884 int sockfd;
1885 int level;
1886 int optname;
1887 abi_ulong optval;
1888 socklen_t optlen;
1889
1890 if (get_user_s32(sockfd, vptr)
1891 || get_user_s32(level, vptr + n)
1892 || get_user_s32(optname, vptr + 2 * n)
1893 || get_user_ual(optval, vptr + 3 * n)
1894 || get_user_u32(optlen, vptr + 4 * n))
1895 return -TARGET_EFAULT;
bellard7854b052003-03-29 17:22:23 +00001896
1897 ret = do_setsockopt(sockfd, level, optname, optval, optlen);
1898 }
1899 break;
bellard31e31b82003-02-18 22:55:36 +00001900 case SOCKOP_getsockopt:
bellard7854b052003-03-29 17:22:23 +00001901 {
bellard2f619692007-11-16 10:46:05 +00001902 int sockfd;
1903 int level;
1904 int optname;
1905 abi_ulong optval;
1906 socklen_t optlen;
bellard7854b052003-03-29 17:22:23 +00001907
bellard2f619692007-11-16 10:46:05 +00001908 if (get_user_s32(sockfd, vptr)
1909 || get_user_s32(level, vptr + n)
1910 || get_user_s32(optname, vptr + 2 * n)
1911 || get_user_ual(optval, vptr + 3 * n)
1912 || get_user_u32(optlen, vptr + 4 * n))
1913 return -TARGET_EFAULT;
1914
1915 ret = do_getsockopt(sockfd, level, optname, optval, optlen);
bellard7854b052003-03-29 17:22:23 +00001916 }
1917 break;
bellard31e31b82003-02-18 22:55:36 +00001918 default:
1919 gemu_log("Unsupported socketcall: %d\n", num);
ths0da46a62007-10-20 20:23:07 +00001920 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00001921 break;
1922 }
1923 return ret;
1924}
j_mayer32407102007-09-26 23:01:49 +00001925#endif
bellard31e31b82003-02-18 22:55:36 +00001926
blueswir13f911a52008-12-13 11:37:02 +00001927#ifdef TARGET_NR_ipc
bellard8853f862004-02-22 14:57:26 +00001928#define N_SHM_REGIONS 32
1929
1930static struct shm_region {
bellard5a4a8982007-11-11 17:39:18 +00001931 abi_ulong start;
1932 abi_ulong size;
bellard8853f862004-02-22 14:57:26 +00001933} shm_regions[N_SHM_REGIONS];
blueswir13f911a52008-12-13 11:37:02 +00001934#endif
bellard8853f862004-02-22 14:57:26 +00001935
ths3eb6b042007-06-03 14:26:27 +00001936struct target_ipc_perm
1937{
blueswir1992f48a2007-10-14 16:27:31 +00001938 abi_long __key;
1939 abi_ulong uid;
1940 abi_ulong gid;
1941 abi_ulong cuid;
1942 abi_ulong cgid;
ths3eb6b042007-06-03 14:26:27 +00001943 unsigned short int mode;
1944 unsigned short int __pad1;
1945 unsigned short int __seq;
1946 unsigned short int __pad2;
blueswir1992f48a2007-10-14 16:27:31 +00001947 abi_ulong __unused1;
1948 abi_ulong __unused2;
ths3eb6b042007-06-03 14:26:27 +00001949};
1950
1951struct target_semid_ds
1952{
1953 struct target_ipc_perm sem_perm;
blueswir1992f48a2007-10-14 16:27:31 +00001954 abi_ulong sem_otime;
1955 abi_ulong __unused1;
1956 abi_ulong sem_ctime;
1957 abi_ulong __unused2;
1958 abi_ulong sem_nsems;
1959 abi_ulong __unused3;
1960 abi_ulong __unused4;
ths3eb6b042007-06-03 14:26:27 +00001961};
1962
bellard579a97f2007-11-11 14:26:47 +00001963static inline abi_long target_to_host_ipc_perm(struct ipc_perm *host_ip,
1964 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00001965{
1966 struct target_ipc_perm *target_ip;
1967 struct target_semid_ds *target_sd;
1968
bellard579a97f2007-11-11 14:26:47 +00001969 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
1970 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001971 target_ip=&(target_sd->sem_perm);
1972 host_ip->__key = tswapl(target_ip->__key);
1973 host_ip->uid = tswapl(target_ip->uid);
1974 host_ip->gid = tswapl(target_ip->gid);
1975 host_ip->cuid = tswapl(target_ip->cuid);
1976 host_ip->cgid = tswapl(target_ip->cgid);
1977 host_ip->mode = tswapl(target_ip->mode);
1978 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00001979 return 0;
ths3eb6b042007-06-03 14:26:27 +00001980}
1981
bellard579a97f2007-11-11 14:26:47 +00001982static inline abi_long host_to_target_ipc_perm(abi_ulong target_addr,
1983 struct ipc_perm *host_ip)
ths3eb6b042007-06-03 14:26:27 +00001984{
1985 struct target_ipc_perm *target_ip;
1986 struct target_semid_ds *target_sd;
1987
bellard579a97f2007-11-11 14:26:47 +00001988 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
1989 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00001990 target_ip = &(target_sd->sem_perm);
1991 target_ip->__key = tswapl(host_ip->__key);
1992 target_ip->uid = tswapl(host_ip->uid);
1993 target_ip->gid = tswapl(host_ip->gid);
1994 target_ip->cuid = tswapl(host_ip->cuid);
1995 target_ip->cgid = tswapl(host_ip->cgid);
1996 target_ip->mode = tswapl(host_ip->mode);
1997 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00001998 return 0;
ths3eb6b042007-06-03 14:26:27 +00001999}
2000
bellard579a97f2007-11-11 14:26:47 +00002001static inline abi_long target_to_host_semid_ds(struct semid_ds *host_sd,
2002 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002003{
2004 struct target_semid_ds *target_sd;
2005
bellard579a97f2007-11-11 14:26:47 +00002006 if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
2007 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002008 if (target_to_host_ipc_perm(&(host_sd->sem_perm),target_addr))
2009 return -TARGET_EFAULT;
ths3eb6b042007-06-03 14:26:27 +00002010 host_sd->sem_nsems = tswapl(target_sd->sem_nsems);
2011 host_sd->sem_otime = tswapl(target_sd->sem_otime);
2012 host_sd->sem_ctime = tswapl(target_sd->sem_ctime);
2013 unlock_user_struct(target_sd, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002014 return 0;
ths3eb6b042007-06-03 14:26:27 +00002015}
2016
bellard579a97f2007-11-11 14:26:47 +00002017static inline abi_long host_to_target_semid_ds(abi_ulong target_addr,
2018 struct semid_ds *host_sd)
ths3eb6b042007-06-03 14:26:27 +00002019{
2020 struct target_semid_ds *target_sd;
2021
bellard579a97f2007-11-11 14:26:47 +00002022 if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
2023 return -TARGET_EFAULT;
aurel32e5289082009-04-18 16:16:12 +00002024 if (host_to_target_ipc_perm(target_addr,&(host_sd->sem_perm)))
2025 return -TARGET_EFAULT;;
ths3eb6b042007-06-03 14:26:27 +00002026 target_sd->sem_nsems = tswapl(host_sd->sem_nsems);
2027 target_sd->sem_otime = tswapl(host_sd->sem_otime);
2028 target_sd->sem_ctime = tswapl(host_sd->sem_ctime);
2029 unlock_user_struct(target_sd, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002030 return 0;
ths3eb6b042007-06-03 14:26:27 +00002031}
2032
aurel32e5289082009-04-18 16:16:12 +00002033struct target_seminfo {
2034 int semmap;
2035 int semmni;
2036 int semmns;
2037 int semmnu;
2038 int semmsl;
2039 int semopm;
2040 int semume;
2041 int semusz;
2042 int semvmx;
2043 int semaem;
2044};
2045
2046static inline abi_long host_to_target_seminfo(abi_ulong target_addr,
2047 struct seminfo *host_seminfo)
2048{
2049 struct target_seminfo *target_seminfo;
2050 if (!lock_user_struct(VERIFY_WRITE, target_seminfo, target_addr, 0))
2051 return -TARGET_EFAULT;
2052 __put_user(host_seminfo->semmap, &target_seminfo->semmap);
2053 __put_user(host_seminfo->semmni, &target_seminfo->semmni);
2054 __put_user(host_seminfo->semmns, &target_seminfo->semmns);
2055 __put_user(host_seminfo->semmnu, &target_seminfo->semmnu);
2056 __put_user(host_seminfo->semmsl, &target_seminfo->semmsl);
2057 __put_user(host_seminfo->semopm, &target_seminfo->semopm);
2058 __put_user(host_seminfo->semume, &target_seminfo->semume);
2059 __put_user(host_seminfo->semusz, &target_seminfo->semusz);
2060 __put_user(host_seminfo->semvmx, &target_seminfo->semvmx);
2061 __put_user(host_seminfo->semaem, &target_seminfo->semaem);
2062 unlock_user_struct(target_seminfo, target_addr, 1);
2063 return 0;
2064}
2065
thsfa294812007-02-02 22:05:00 +00002066union semun {
2067 int val;
ths3eb6b042007-06-03 14:26:27 +00002068 struct semid_ds *buf;
thsfa294812007-02-02 22:05:00 +00002069 unsigned short *array;
aurel32e5289082009-04-18 16:16:12 +00002070 struct seminfo *__buf;
thsfa294812007-02-02 22:05:00 +00002071};
2072
ths3eb6b042007-06-03 14:26:27 +00002073union target_semun {
2074 int val;
aurel32e5289082009-04-18 16:16:12 +00002075 abi_ulong buf;
2076 abi_ulong array;
2077 abi_ulong __buf;
ths3eb6b042007-06-03 14:26:27 +00002078};
2079
aurel32e5289082009-04-18 16:16:12 +00002080static inline abi_long target_to_host_semarray(int semid, unsigned short **host_array,
2081 abi_ulong target_addr)
ths3eb6b042007-06-03 14:26:27 +00002082{
aurel32e5289082009-04-18 16:16:12 +00002083 int nsems;
2084 unsigned short *array;
2085 union semun semun;
2086 struct semid_ds semid_ds;
2087 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002088
aurel32e5289082009-04-18 16:16:12 +00002089 semun.buf = &semid_ds;
2090
2091 ret = semctl(semid, 0, IPC_STAT, semun);
2092 if (ret == -1)
2093 return get_errno(ret);
2094
2095 nsems = semid_ds.sem_nsems;
2096
2097 *host_array = malloc(nsems*sizeof(unsigned short));
2098 array = lock_user(VERIFY_READ, target_addr,
2099 nsems*sizeof(unsigned short), 1);
2100 if (!array)
2101 return -TARGET_EFAULT;
2102
2103 for(i=0; i<nsems; i++) {
2104 __get_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002105 }
aurel32e5289082009-04-18 16:16:12 +00002106 unlock_user(array, target_addr, 0);
2107
bellard579a97f2007-11-11 14:26:47 +00002108 return 0;
ths3eb6b042007-06-03 14:26:27 +00002109}
2110
aurel32e5289082009-04-18 16:16:12 +00002111static inline abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
2112 unsigned short **host_array)
ths3eb6b042007-06-03 14:26:27 +00002113{
aurel32e5289082009-04-18 16:16:12 +00002114 int nsems;
2115 unsigned short *array;
2116 union semun semun;
2117 struct semid_ds semid_ds;
2118 int i, ret;
ths3eb6b042007-06-03 14:26:27 +00002119
aurel32e5289082009-04-18 16:16:12 +00002120 semun.buf = &semid_ds;
2121
2122 ret = semctl(semid, 0, IPC_STAT, semun);
2123 if (ret == -1)
2124 return get_errno(ret);
2125
2126 nsems = semid_ds.sem_nsems;
2127
2128 array = lock_user(VERIFY_WRITE, target_addr,
2129 nsems*sizeof(unsigned short), 0);
2130 if (!array)
2131 return -TARGET_EFAULT;
2132
2133 for(i=0; i<nsems; i++) {
2134 __put_user((*host_array)[i], &array[i]);
ths3eb6b042007-06-03 14:26:27 +00002135 }
aurel32e5289082009-04-18 16:16:12 +00002136 free(*host_array);
2137 unlock_user(array, target_addr, 1);
2138
bellard579a97f2007-11-11 14:26:47 +00002139 return 0;
ths3eb6b042007-06-03 14:26:27 +00002140}
2141
aurel32e5289082009-04-18 16:16:12 +00002142static inline abi_long do_semctl(int semid, int semnum, int cmd,
2143 union target_semun target_su)
ths3eb6b042007-06-03 14:26:27 +00002144{
2145 union semun arg;
2146 struct semid_ds dsarg;
aurel32e5289082009-04-18 16:16:12 +00002147 unsigned short *array;
2148 struct seminfo seminfo;
2149 abi_long ret = -TARGET_EINVAL;
2150 abi_long err;
2151 cmd &= 0xff;
ths3eb6b042007-06-03 14:26:27 +00002152
2153 switch( cmd ) {
2154 case GETVAL:
ths3eb6b042007-06-03 14:26:27 +00002155 case SETVAL:
aurel32e5289082009-04-18 16:16:12 +00002156 arg.val = tswapl(target_su.val);
2157 ret = get_errno(semctl(semid, semnum, cmd, arg));
2158 target_su.val = tswapl(arg.val);
ths3eb6b042007-06-03 14:26:27 +00002159 break;
2160 case GETALL:
ths3eb6b042007-06-03 14:26:27 +00002161 case SETALL:
aurel32e5289082009-04-18 16:16:12 +00002162 err = target_to_host_semarray(semid, &array, target_su.array);
2163 if (err)
2164 return err;
2165 arg.array = array;
2166 ret = get_errno(semctl(semid, semnum, cmd, arg));
2167 err = host_to_target_semarray(semid, target_su.array, &array);
2168 if (err)
2169 return err;
ths3eb6b042007-06-03 14:26:27 +00002170 break;
2171 case IPC_STAT:
ths3eb6b042007-06-03 14:26:27 +00002172 case IPC_SET:
aurel32e5289082009-04-18 16:16:12 +00002173 case SEM_STAT:
2174 err = target_to_host_semid_ds(&dsarg, target_su.buf);
2175 if (err)
2176 return err;
2177 arg.buf = &dsarg;
2178 ret = get_errno(semctl(semid, semnum, cmd, arg));
2179 err = host_to_target_semid_ds(target_su.buf, &dsarg);
2180 if (err)
2181 return err;
ths3eb6b042007-06-03 14:26:27 +00002182 break;
aurel32e5289082009-04-18 16:16:12 +00002183 case IPC_INFO:
2184 case SEM_INFO:
2185 arg.__buf = &seminfo;
2186 ret = get_errno(semctl(semid, semnum, cmd, arg));
2187 err = host_to_target_seminfo(target_su.__buf, &seminfo);
2188 if (err)
2189 return err;
2190 break;
2191 case IPC_RMID:
2192 case GETPID:
2193 case GETNCNT:
2194 case GETZCNT:
2195 ret = get_errno(semctl(semid, semnum, cmd, NULL));
2196 break;
ths3eb6b042007-06-03 14:26:27 +00002197 }
2198
2199 return ret;
2200}
2201
aurel32e5289082009-04-18 16:16:12 +00002202struct target_sembuf {
2203 unsigned short sem_num;
2204 short sem_op;
2205 short sem_flg;
2206};
2207
2208static inline abi_long target_to_host_sembuf(struct sembuf *host_sembuf,
2209 abi_ulong target_addr,
2210 unsigned nsops)
2211{
2212 struct target_sembuf *target_sembuf;
2213 int i;
2214
2215 target_sembuf = lock_user(VERIFY_READ, target_addr,
2216 nsops*sizeof(struct target_sembuf), 1);
2217 if (!target_sembuf)
2218 return -TARGET_EFAULT;
2219
2220 for(i=0; i<nsops; i++) {
2221 __get_user(host_sembuf[i].sem_num, &target_sembuf[i].sem_num);
2222 __get_user(host_sembuf[i].sem_op, &target_sembuf[i].sem_op);
2223 __get_user(host_sembuf[i].sem_flg, &target_sembuf[i].sem_flg);
2224 }
2225
2226 unlock_user(target_sembuf, target_addr, 0);
2227
2228 return 0;
2229}
2230
2231static inline abi_long do_semop(int semid, abi_long ptr, unsigned nsops)
2232{
2233 struct sembuf sops[nsops];
2234
2235 if (target_to_host_sembuf(sops, ptr, nsops))
2236 return -TARGET_EFAULT;
2237
2238 return semop(semid, sops, nsops);
2239}
2240
ths1bc012f2007-06-03 14:27:49 +00002241struct target_msqid_ds
2242{
aurel321c54ff92008-10-13 21:08:44 +00002243 struct target_ipc_perm msg_perm;
2244 abi_ulong msg_stime;
2245#if TARGET_ABI_BITS == 32
2246 abi_ulong __unused1;
2247#endif
2248 abi_ulong msg_rtime;
2249#if TARGET_ABI_BITS == 32
2250 abi_ulong __unused2;
2251#endif
2252 abi_ulong msg_ctime;
2253#if TARGET_ABI_BITS == 32
2254 abi_ulong __unused3;
2255#endif
2256 abi_ulong __msg_cbytes;
2257 abi_ulong msg_qnum;
2258 abi_ulong msg_qbytes;
2259 abi_ulong msg_lspid;
2260 abi_ulong msg_lrpid;
2261 abi_ulong __unused4;
2262 abi_ulong __unused5;
ths1bc012f2007-06-03 14:27:49 +00002263};
2264
bellard579a97f2007-11-11 14:26:47 +00002265static inline abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
2266 abi_ulong target_addr)
ths1bc012f2007-06-03 14:27:49 +00002267{
2268 struct target_msqid_ds *target_md;
2269
bellard579a97f2007-11-11 14:26:47 +00002270 if (!lock_user_struct(VERIFY_READ, target_md, target_addr, 1))
2271 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002272 if (target_to_host_ipc_perm(&(host_md->msg_perm),target_addr))
2273 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002274 host_md->msg_stime = tswapl(target_md->msg_stime);
2275 host_md->msg_rtime = tswapl(target_md->msg_rtime);
2276 host_md->msg_ctime = tswapl(target_md->msg_ctime);
2277 host_md->__msg_cbytes = tswapl(target_md->__msg_cbytes);
2278 host_md->msg_qnum = tswapl(target_md->msg_qnum);
2279 host_md->msg_qbytes = tswapl(target_md->msg_qbytes);
2280 host_md->msg_lspid = tswapl(target_md->msg_lspid);
2281 host_md->msg_lrpid = tswapl(target_md->msg_lrpid);
2282 unlock_user_struct(target_md, target_addr, 0);
bellard579a97f2007-11-11 14:26:47 +00002283 return 0;
ths1bc012f2007-06-03 14:27:49 +00002284}
2285
bellard579a97f2007-11-11 14:26:47 +00002286static inline abi_long host_to_target_msqid_ds(abi_ulong target_addr,
2287 struct msqid_ds *host_md)
ths1bc012f2007-06-03 14:27:49 +00002288{
2289 struct target_msqid_ds *target_md;
2290
bellard579a97f2007-11-11 14:26:47 +00002291 if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0))
2292 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002293 if (host_to_target_ipc_perm(target_addr,&(host_md->msg_perm)))
2294 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002295 target_md->msg_stime = tswapl(host_md->msg_stime);
2296 target_md->msg_rtime = tswapl(host_md->msg_rtime);
2297 target_md->msg_ctime = tswapl(host_md->msg_ctime);
2298 target_md->__msg_cbytes = tswapl(host_md->__msg_cbytes);
2299 target_md->msg_qnum = tswapl(host_md->msg_qnum);
2300 target_md->msg_qbytes = tswapl(host_md->msg_qbytes);
2301 target_md->msg_lspid = tswapl(host_md->msg_lspid);
2302 target_md->msg_lrpid = tswapl(host_md->msg_lrpid);
2303 unlock_user_struct(target_md, target_addr, 1);
bellard579a97f2007-11-11 14:26:47 +00002304 return 0;
ths1bc012f2007-06-03 14:27:49 +00002305}
2306
aurel321c54ff92008-10-13 21:08:44 +00002307struct target_msginfo {
2308 int msgpool;
2309 int msgmap;
2310 int msgmax;
2311 int msgmnb;
2312 int msgmni;
2313 int msgssz;
2314 int msgtql;
2315 unsigned short int msgseg;
2316};
2317
2318static inline abi_long host_to_target_msginfo(abi_ulong target_addr,
2319 struct msginfo *host_msginfo)
2320{
2321 struct target_msginfo *target_msginfo;
2322 if (!lock_user_struct(VERIFY_WRITE, target_msginfo, target_addr, 0))
2323 return -TARGET_EFAULT;
2324 __put_user(host_msginfo->msgpool, &target_msginfo->msgpool);
2325 __put_user(host_msginfo->msgmap, &target_msginfo->msgmap);
2326 __put_user(host_msginfo->msgmax, &target_msginfo->msgmax);
2327 __put_user(host_msginfo->msgmnb, &target_msginfo->msgmnb);
2328 __put_user(host_msginfo->msgmni, &target_msginfo->msgmni);
2329 __put_user(host_msginfo->msgssz, &target_msginfo->msgssz);
2330 __put_user(host_msginfo->msgtql, &target_msginfo->msgtql);
2331 __put_user(host_msginfo->msgseg, &target_msginfo->msgseg);
2332 unlock_user_struct(target_msginfo, target_addr, 1);
aurel3200b229a2008-10-24 13:12:52 +00002333 return 0;
aurel321c54ff92008-10-13 21:08:44 +00002334}
2335
2336static inline abi_long do_msgctl(int msgid, int cmd, abi_long ptr)
ths1bc012f2007-06-03 14:27:49 +00002337{
2338 struct msqid_ds dsarg;
aurel321c54ff92008-10-13 21:08:44 +00002339 struct msginfo msginfo;
2340 abi_long ret = -TARGET_EINVAL;
2341
2342 cmd &= 0xff;
2343
2344 switch (cmd) {
ths1bc012f2007-06-03 14:27:49 +00002345 case IPC_STAT:
2346 case IPC_SET:
aurel321c54ff92008-10-13 21:08:44 +00002347 case MSG_STAT:
2348 if (target_to_host_msqid_ds(&dsarg,ptr))
2349 return -TARGET_EFAULT;
2350 ret = get_errno(msgctl(msgid, cmd, &dsarg));
2351 if (host_to_target_msqid_ds(ptr,&dsarg))
2352 return -TARGET_EFAULT;
2353 break;
2354 case IPC_RMID:
2355 ret = get_errno(msgctl(msgid, cmd, NULL));
2356 break;
2357 case IPC_INFO:
2358 case MSG_INFO:
2359 ret = get_errno(msgctl(msgid, cmd, (struct msqid_ds *)&msginfo));
2360 if (host_to_target_msginfo(ptr, &msginfo))
2361 return -TARGET_EFAULT;
2362 break;
ths1bc012f2007-06-03 14:27:49 +00002363 }
aurel321c54ff92008-10-13 21:08:44 +00002364
ths1bc012f2007-06-03 14:27:49 +00002365 return ret;
2366}
2367
2368struct target_msgbuf {
aurel321c54ff92008-10-13 21:08:44 +00002369 abi_long mtype;
2370 char mtext[1];
ths1bc012f2007-06-03 14:27:49 +00002371};
2372
blueswir1992f48a2007-10-14 16:27:31 +00002373static inline abi_long do_msgsnd(int msqid, abi_long msgp,
2374 unsigned int msgsz, int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002375{
2376 struct target_msgbuf *target_mb;
2377 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002378 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002379
bellard579a97f2007-11-11 14:26:47 +00002380 if (!lock_user_struct(VERIFY_READ, target_mb, msgp, 0))
2381 return -TARGET_EFAULT;
ths1bc012f2007-06-03 14:27:49 +00002382 host_mb = malloc(msgsz+sizeof(long));
aurel321c54ff92008-10-13 21:08:44 +00002383 host_mb->mtype = (abi_long) tswapl(target_mb->mtype);
2384 memcpy(host_mb->mtext, target_mb->mtext, msgsz);
ths1bc012f2007-06-03 14:27:49 +00002385 ret = get_errno(msgsnd(msqid, host_mb, msgsz, msgflg));
2386 free(host_mb);
2387 unlock_user_struct(target_mb, msgp, 0);
2388
2389 return ret;
2390}
2391
blueswir1992f48a2007-10-14 16:27:31 +00002392static inline abi_long do_msgrcv(int msqid, abi_long msgp,
aurel321c54ff92008-10-13 21:08:44 +00002393 unsigned int msgsz, abi_long msgtyp,
blueswir1992f48a2007-10-14 16:27:31 +00002394 int msgflg)
ths1bc012f2007-06-03 14:27:49 +00002395{
2396 struct target_msgbuf *target_mb;
bellard579a97f2007-11-11 14:26:47 +00002397 char *target_mtext;
ths1bc012f2007-06-03 14:27:49 +00002398 struct msgbuf *host_mb;
blueswir1992f48a2007-10-14 16:27:31 +00002399 abi_long ret = 0;
ths1bc012f2007-06-03 14:27:49 +00002400
bellard579a97f2007-11-11 14:26:47 +00002401 if (!lock_user_struct(VERIFY_WRITE, target_mb, msgp, 0))
2402 return -TARGET_EFAULT;
aurel321c54ff92008-10-13 21:08:44 +00002403
ths1bc012f2007-06-03 14:27:49 +00002404 host_mb = malloc(msgsz+sizeof(long));
aurel321c54ff92008-10-13 21:08:44 +00002405 ret = get_errno(msgrcv(msqid, host_mb, msgsz, tswapl(msgtyp), msgflg));
2406
bellard579a97f2007-11-11 14:26:47 +00002407 if (ret > 0) {
2408 abi_ulong target_mtext_addr = msgp + sizeof(abi_ulong);
2409 target_mtext = lock_user(VERIFY_WRITE, target_mtext_addr, ret, 0);
2410 if (!target_mtext) {
2411 ret = -TARGET_EFAULT;
2412 goto end;
2413 }
aurel321c54ff92008-10-13 21:08:44 +00002414 memcpy(target_mb->mtext, host_mb->mtext, ret);
bellard579a97f2007-11-11 14:26:47 +00002415 unlock_user(target_mtext, target_mtext_addr, ret);
2416 }
aurel321c54ff92008-10-13 21:08:44 +00002417
ths1bc012f2007-06-03 14:27:49 +00002418 target_mb->mtype = tswapl(host_mb->mtype);
2419 free(host_mb);
ths1bc012f2007-06-03 14:27:49 +00002420
bellard579a97f2007-11-11 14:26:47 +00002421end:
2422 if (target_mb)
2423 unlock_user_struct(target_mb, msgp, 1);
ths1bc012f2007-06-03 14:27:49 +00002424 return ret;
2425}
2426
aurel321c54ff92008-10-13 21:08:44 +00002427#ifdef TARGET_NR_ipc
pbrook53a59602006-03-25 19:31:22 +00002428/* ??? This only works with linear mappings. */
ths0da46a62007-10-20 20:23:07 +00002429/* do_ipc() must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002430static abi_long do_ipc(unsigned int call, int first,
2431 int second, int third,
2432 abi_long ptr, abi_long fifth)
bellard8853f862004-02-22 14:57:26 +00002433{
2434 int version;
blueswir1992f48a2007-10-14 16:27:31 +00002435 abi_long ret = 0;
bellard8853f862004-02-22 14:57:26 +00002436 struct shmid_ds shm_info;
2437 int i;
2438
2439 version = call >> 16;
2440 call &= 0xffff;
2441
2442 switch (call) {
thsfa294812007-02-02 22:05:00 +00002443 case IPCOP_semop:
aurel32e5289082009-04-18 16:16:12 +00002444 ret = do_semop(first, ptr, second);
thsfa294812007-02-02 22:05:00 +00002445 break;
2446
2447 case IPCOP_semget:
2448 ret = get_errno(semget(first, second, third));
2449 break;
2450
2451 case IPCOP_semctl:
aurel32e5289082009-04-18 16:16:12 +00002452 ret = do_semctl(first, second, third, (union target_semun)(abi_ulong) ptr);
thsfa294812007-02-02 22:05:00 +00002453 break;
thsd96372e2007-02-02 22:05:44 +00002454
aurel321c54ff92008-10-13 21:08:44 +00002455 case IPCOP_msgget:
2456 ret = get_errno(msgget(first, second));
2457 break;
thsd96372e2007-02-02 22:05:44 +00002458
aurel321c54ff92008-10-13 21:08:44 +00002459 case IPCOP_msgsnd:
2460 ret = do_msgsnd(first, ptr, second, third);
2461 break;
thsd96372e2007-02-02 22:05:44 +00002462
aurel321c54ff92008-10-13 21:08:44 +00002463 case IPCOP_msgctl:
2464 ret = do_msgctl(first, second, ptr);
2465 break;
thsd96372e2007-02-02 22:05:44 +00002466
aurel321c54ff92008-10-13 21:08:44 +00002467 case IPCOP_msgrcv:
2468 switch (version) {
2469 case 0:
2470 {
2471 struct target_ipc_kludge {
2472 abi_long msgp;
2473 abi_long msgtyp;
2474 } *tmp;
thsd96372e2007-02-02 22:05:44 +00002475
aurel321c54ff92008-10-13 21:08:44 +00002476 if (!lock_user_struct(VERIFY_READ, tmp, ptr, 1)) {
2477 ret = -TARGET_EFAULT;
2478 break;
ths1bc012f2007-06-03 14:27:49 +00002479 }
aurel321c54ff92008-10-13 21:08:44 +00002480
2481 ret = do_msgrcv(first, tmp->msgp, second, tmp->msgtyp, third);
2482
2483 unlock_user_struct(tmp, ptr, 0);
2484 break;
2485 }
2486 default:
2487 ret = do_msgrcv(first, ptr, second, fifth, third);
2488 }
2489 break;
thsd96372e2007-02-02 22:05:44 +00002490
bellard8853f862004-02-22 14:57:26 +00002491 case IPCOP_shmat:
bellard5a4a8982007-11-11 17:39:18 +00002492 {
2493 abi_ulong raddr;
2494 void *host_addr;
2495 /* SHM_* flags are the same on all linux platforms */
2496 host_addr = shmat(first, (void *)g2h(ptr), second);
2497 if (host_addr == (void *)-1) {
2498 ret = get_errno((long)host_addr);
bellard8853f862004-02-22 14:57:26 +00002499 break;
bellard5a4a8982007-11-11 17:39:18 +00002500 }
2501 raddr = h2g((unsigned long)host_addr);
2502 /* find out the length of the shared memory segment */
2503
2504 ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
2505 if (is_error(ret)) {
2506 /* can't get length, bail out */
2507 shmdt(host_addr);
2508 break;
2509 }
2510 page_set_flags(raddr, raddr + shm_info.shm_segsz,
2511 PAGE_VALID | PAGE_READ |
2512 ((second & SHM_RDONLY)? 0: PAGE_WRITE));
2513 for (i = 0; i < N_SHM_REGIONS; ++i) {
2514 if (shm_regions[i].start == 0) {
2515 shm_regions[i].start = raddr;
2516 shm_regions[i].size = shm_info.shm_segsz;
2517 break;
2518 }
2519 }
bellard2f619692007-11-16 10:46:05 +00002520 if (put_user_ual(raddr, third))
bellard5a4a8982007-11-11 17:39:18 +00002521 return -TARGET_EFAULT;
2522 ret = 0;
2523 }
bellard8853f862004-02-22 14:57:26 +00002524 break;
2525 case IPCOP_shmdt:
2526 for (i = 0; i < N_SHM_REGIONS; ++i) {
2527 if (shm_regions[i].start == ptr) {
2528 shm_regions[i].start = 0;
2529 page_set_flags(ptr, shm_regions[i].size, 0);
2530 break;
2531 }
2532 }
bellard5a4a8982007-11-11 17:39:18 +00002533 ret = get_errno(shmdt((void *)g2h(ptr)));
bellard8853f862004-02-22 14:57:26 +00002534 break;
2535
2536 case IPCOP_shmget:
2537 /* IPC_* flag values are the same on all linux platforms */
2538 ret = get_errno(shmget(first, second, third));
2539 break;
2540
2541 /* IPC_* and SHM_* command values are the same on all linux platforms */
2542 case IPCOP_shmctl:
2543 switch(second) {
2544 case IPC_RMID:
2545 case SHM_LOCK:
2546 case SHM_UNLOCK:
2547 ret = get_errno(shmctl(first, second, NULL));
2548 break;
2549 default:
2550 goto unimplemented;
2551 }
2552 break;
2553 default:
2554 unimplemented:
j_mayer32407102007-09-26 23:01:49 +00002555 gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
ths0da46a62007-10-20 20:23:07 +00002556 ret = -TARGET_ENOSYS;
bellard8853f862004-02-22 14:57:26 +00002557 break;
2558 }
2559 return ret;
2560}
j_mayer32407102007-09-26 23:01:49 +00002561#endif
bellard8853f862004-02-22 14:57:26 +00002562
bellard31e31b82003-02-18 22:55:36 +00002563/* kernel structure types definitions */
2564#define IFNAMSIZ 16
2565
Blue Swirl001faf32009-05-13 17:53:17 +00002566#define STRUCT(name, ...) STRUCT_ ## name,
bellard31e31b82003-02-18 22:55:36 +00002567#define STRUCT_SPECIAL(name) STRUCT_ ## name,
2568enum {
2569#include "syscall_types.h"
2570};
2571#undef STRUCT
2572#undef STRUCT_SPECIAL
2573
Blue Swirl001faf32009-05-13 17:53:17 +00002574#define STRUCT(name, ...) static const argtype struct_ ## name ## _def[] = { __VA_ARGS__, TYPE_NULL };
bellard31e31b82003-02-18 22:55:36 +00002575#define STRUCT_SPECIAL(name)
2576#include "syscall_types.h"
2577#undef STRUCT
2578#undef STRUCT_SPECIAL
2579
2580typedef struct IOCTLEntry {
bellard2ab83ea2003-06-15 19:56:46 +00002581 unsigned int target_cmd;
2582 unsigned int host_cmd;
bellard31e31b82003-02-18 22:55:36 +00002583 const char *name;
2584 int access;
bellard1a9353d2003-03-16 20:28:50 +00002585 const argtype arg_type[5];
bellard31e31b82003-02-18 22:55:36 +00002586} IOCTLEntry;
2587
2588#define IOC_R 0x0001
2589#define IOC_W 0x0002
2590#define IOC_RW (IOC_R | IOC_W)
2591
2592#define MAX_STRUCT_SIZE 4096
2593
blueswir19f106a72008-10-05 10:52:52 +00002594static IOCTLEntry ioctl_entries[] = {
Blue Swirl001faf32009-05-13 17:53:17 +00002595#define IOCTL(cmd, access, ...) \
2596 { TARGET_ ## cmd, cmd, #cmd, access, { __VA_ARGS__ } },
bellard31e31b82003-02-18 22:55:36 +00002597#include "ioctls.h"
2598 { 0, 0, },
2599};
2600
pbrook53a59602006-03-25 19:31:22 +00002601/* ??? Implement proper locking for ioctls. */
ths0da46a62007-10-20 20:23:07 +00002602/* do_ioctl() Must return target values and target errnos. */
blueswir1992f48a2007-10-14 16:27:31 +00002603static abi_long do_ioctl(int fd, abi_long cmd, abi_long arg)
bellard31e31b82003-02-18 22:55:36 +00002604{
2605 const IOCTLEntry *ie;
2606 const argtype *arg_type;
blueswir1992f48a2007-10-14 16:27:31 +00002607 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00002608 uint8_t buf_temp[MAX_STRUCT_SIZE];
pbrook53a59602006-03-25 19:31:22 +00002609 int target_size;
2610 void *argptr;
bellard31e31b82003-02-18 22:55:36 +00002611
2612 ie = ioctl_entries;
2613 for(;;) {
2614 if (ie->target_cmd == 0) {
j_mayer32407102007-09-26 23:01:49 +00002615 gemu_log("Unsupported ioctl: cmd=0x%04lx\n", (long)cmd);
ths0da46a62007-10-20 20:23:07 +00002616 return -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002617 }
2618 if (ie->target_cmd == cmd)
2619 break;
2620 ie++;
2621 }
2622 arg_type = ie->arg_type;
bellard9de5e442003-03-23 16:49:39 +00002623#if defined(DEBUG)
j_mayer32407102007-09-26 23:01:49 +00002624 gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
bellard72f03902003-02-18 23:33:18 +00002625#endif
bellard31e31b82003-02-18 22:55:36 +00002626 switch(arg_type[0]) {
2627 case TYPE_NULL:
2628 /* no argument */
2629 ret = get_errno(ioctl(fd, ie->host_cmd));
2630 break;
2631 case TYPE_PTRVOID:
2632 case TYPE_INT:
2633 /* int argment */
2634 ret = get_errno(ioctl(fd, ie->host_cmd, arg));
2635 break;
2636 case TYPE_PTR:
2637 arg_type++;
pbrook53a59602006-03-25 19:31:22 +00002638 target_size = thunk_type_size(arg_type, 0);
bellard31e31b82003-02-18 22:55:36 +00002639 switch(ie->access) {
2640 case IOC_R:
2641 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2642 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002643 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2644 if (!argptr)
2645 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002646 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2647 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00002648 }
2649 break;
2650 case IOC_W:
bellard579a97f2007-11-11 14:26:47 +00002651 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2652 if (!argptr)
2653 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002654 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2655 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002656 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2657 break;
2658 default:
2659 case IOC_RW:
bellard579a97f2007-11-11 14:26:47 +00002660 argptr = lock_user(VERIFY_READ, arg, target_size, 1);
2661 if (!argptr)
2662 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002663 thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
2664 unlock_user(argptr, arg, 0);
bellard31e31b82003-02-18 22:55:36 +00002665 ret = get_errno(ioctl(fd, ie->host_cmd, buf_temp));
2666 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00002667 argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
2668 if (!argptr)
2669 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002670 thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
2671 unlock_user(argptr, arg, target_size);
bellard31e31b82003-02-18 22:55:36 +00002672 }
2673 break;
2674 }
2675 break;
2676 default:
j_mayer32407102007-09-26 23:01:49 +00002677 gemu_log("Unsupported ioctl type: cmd=0x%04lx type=%d\n",
2678 (long)cmd, arg_type[0]);
ths0da46a62007-10-20 20:23:07 +00002679 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00002680 break;
2681 }
2682 return ret;
2683}
2684
blueswir1b39bc502008-10-05 10:51:10 +00002685static const bitmask_transtbl iflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00002686 { TARGET_IGNBRK, TARGET_IGNBRK, IGNBRK, IGNBRK },
2687 { TARGET_BRKINT, TARGET_BRKINT, BRKINT, BRKINT },
2688 { TARGET_IGNPAR, TARGET_IGNPAR, IGNPAR, IGNPAR },
2689 { TARGET_PARMRK, TARGET_PARMRK, PARMRK, PARMRK },
2690 { TARGET_INPCK, TARGET_INPCK, INPCK, INPCK },
2691 { TARGET_ISTRIP, TARGET_ISTRIP, ISTRIP, ISTRIP },
2692 { TARGET_INLCR, TARGET_INLCR, INLCR, INLCR },
2693 { TARGET_IGNCR, TARGET_IGNCR, IGNCR, IGNCR },
2694 { TARGET_ICRNL, TARGET_ICRNL, ICRNL, ICRNL },
2695 { TARGET_IUCLC, TARGET_IUCLC, IUCLC, IUCLC },
2696 { TARGET_IXON, TARGET_IXON, IXON, IXON },
2697 { TARGET_IXANY, TARGET_IXANY, IXANY, IXANY },
2698 { TARGET_IXOFF, TARGET_IXOFF, IXOFF, IXOFF },
2699 { TARGET_IMAXBEL, TARGET_IMAXBEL, IMAXBEL, IMAXBEL },
2700 { 0, 0, 0, 0 }
2701};
2702
blueswir1b39bc502008-10-05 10:51:10 +00002703static const bitmask_transtbl oflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00002704 { TARGET_OPOST, TARGET_OPOST, OPOST, OPOST },
2705 { TARGET_OLCUC, TARGET_OLCUC, OLCUC, OLCUC },
2706 { TARGET_ONLCR, TARGET_ONLCR, ONLCR, ONLCR },
2707 { TARGET_OCRNL, TARGET_OCRNL, OCRNL, OCRNL },
2708 { TARGET_ONOCR, TARGET_ONOCR, ONOCR, ONOCR },
2709 { TARGET_ONLRET, TARGET_ONLRET, ONLRET, ONLRET },
2710 { TARGET_OFILL, TARGET_OFILL, OFILL, OFILL },
2711 { TARGET_OFDEL, TARGET_OFDEL, OFDEL, OFDEL },
2712 { TARGET_NLDLY, TARGET_NL0, NLDLY, NL0 },
2713 { TARGET_NLDLY, TARGET_NL1, NLDLY, NL1 },
2714 { TARGET_CRDLY, TARGET_CR0, CRDLY, CR0 },
2715 { TARGET_CRDLY, TARGET_CR1, CRDLY, CR1 },
2716 { TARGET_CRDLY, TARGET_CR2, CRDLY, CR2 },
2717 { TARGET_CRDLY, TARGET_CR3, CRDLY, CR3 },
2718 { TARGET_TABDLY, TARGET_TAB0, TABDLY, TAB0 },
2719 { TARGET_TABDLY, TARGET_TAB1, TABDLY, TAB1 },
2720 { TARGET_TABDLY, TARGET_TAB2, TABDLY, TAB2 },
2721 { TARGET_TABDLY, TARGET_TAB3, TABDLY, TAB3 },
2722 { TARGET_BSDLY, TARGET_BS0, BSDLY, BS0 },
2723 { TARGET_BSDLY, TARGET_BS1, BSDLY, BS1 },
2724 { TARGET_VTDLY, TARGET_VT0, VTDLY, VT0 },
2725 { TARGET_VTDLY, TARGET_VT1, VTDLY, VT1 },
2726 { TARGET_FFDLY, TARGET_FF0, FFDLY, FF0 },
2727 { TARGET_FFDLY, TARGET_FF1, FFDLY, FF1 },
2728 { 0, 0, 0, 0 }
2729};
2730
blueswir1b39bc502008-10-05 10:51:10 +00002731static const bitmask_transtbl cflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00002732 { TARGET_CBAUD, TARGET_B0, CBAUD, B0 },
2733 { TARGET_CBAUD, TARGET_B50, CBAUD, B50 },
2734 { TARGET_CBAUD, TARGET_B75, CBAUD, B75 },
2735 { TARGET_CBAUD, TARGET_B110, CBAUD, B110 },
2736 { TARGET_CBAUD, TARGET_B134, CBAUD, B134 },
2737 { TARGET_CBAUD, TARGET_B150, CBAUD, B150 },
2738 { TARGET_CBAUD, TARGET_B200, CBAUD, B200 },
2739 { TARGET_CBAUD, TARGET_B300, CBAUD, B300 },
2740 { TARGET_CBAUD, TARGET_B600, CBAUD, B600 },
2741 { TARGET_CBAUD, TARGET_B1200, CBAUD, B1200 },
2742 { TARGET_CBAUD, TARGET_B1800, CBAUD, B1800 },
2743 { TARGET_CBAUD, TARGET_B2400, CBAUD, B2400 },
2744 { TARGET_CBAUD, TARGET_B4800, CBAUD, B4800 },
2745 { TARGET_CBAUD, TARGET_B9600, CBAUD, B9600 },
2746 { TARGET_CBAUD, TARGET_B19200, CBAUD, B19200 },
2747 { TARGET_CBAUD, TARGET_B38400, CBAUD, B38400 },
2748 { TARGET_CBAUD, TARGET_B57600, CBAUD, B57600 },
2749 { TARGET_CBAUD, TARGET_B115200, CBAUD, B115200 },
2750 { TARGET_CBAUD, TARGET_B230400, CBAUD, B230400 },
2751 { TARGET_CBAUD, TARGET_B460800, CBAUD, B460800 },
2752 { TARGET_CSIZE, TARGET_CS5, CSIZE, CS5 },
2753 { TARGET_CSIZE, TARGET_CS6, CSIZE, CS6 },
2754 { TARGET_CSIZE, TARGET_CS7, CSIZE, CS7 },
2755 { TARGET_CSIZE, TARGET_CS8, CSIZE, CS8 },
2756 { TARGET_CSTOPB, TARGET_CSTOPB, CSTOPB, CSTOPB },
2757 { TARGET_CREAD, TARGET_CREAD, CREAD, CREAD },
2758 { TARGET_PARENB, TARGET_PARENB, PARENB, PARENB },
2759 { TARGET_PARODD, TARGET_PARODD, PARODD, PARODD },
2760 { TARGET_HUPCL, TARGET_HUPCL, HUPCL, HUPCL },
2761 { TARGET_CLOCAL, TARGET_CLOCAL, CLOCAL, CLOCAL },
2762 { TARGET_CRTSCTS, TARGET_CRTSCTS, CRTSCTS, CRTSCTS },
2763 { 0, 0, 0, 0 }
2764};
2765
blueswir1b39bc502008-10-05 10:51:10 +00002766static const bitmask_transtbl lflag_tbl[] = {
bellard31e31b82003-02-18 22:55:36 +00002767 { TARGET_ISIG, TARGET_ISIG, ISIG, ISIG },
2768 { TARGET_ICANON, TARGET_ICANON, ICANON, ICANON },
2769 { TARGET_XCASE, TARGET_XCASE, XCASE, XCASE },
2770 { TARGET_ECHO, TARGET_ECHO, ECHO, ECHO },
2771 { TARGET_ECHOE, TARGET_ECHOE, ECHOE, ECHOE },
2772 { TARGET_ECHOK, TARGET_ECHOK, ECHOK, ECHOK },
2773 { TARGET_ECHONL, TARGET_ECHONL, ECHONL, ECHONL },
2774 { TARGET_NOFLSH, TARGET_NOFLSH, NOFLSH, NOFLSH },
2775 { TARGET_TOSTOP, TARGET_TOSTOP, TOSTOP, TOSTOP },
2776 { TARGET_ECHOCTL, TARGET_ECHOCTL, ECHOCTL, ECHOCTL },
2777 { TARGET_ECHOPRT, TARGET_ECHOPRT, ECHOPRT, ECHOPRT },
2778 { TARGET_ECHOKE, TARGET_ECHOKE, ECHOKE, ECHOKE },
2779 { TARGET_FLUSHO, TARGET_FLUSHO, FLUSHO, FLUSHO },
2780 { TARGET_PENDIN, TARGET_PENDIN, PENDIN, PENDIN },
2781 { TARGET_IEXTEN, TARGET_IEXTEN, IEXTEN, IEXTEN },
2782 { 0, 0, 0, 0 }
2783};
2784
2785static void target_to_host_termios (void *dst, const void *src)
2786{
2787 struct host_termios *host = dst;
2788 const struct target_termios *target = src;
ths3b46e622007-09-17 08:09:54 +00002789
ths5fafdf22007-09-16 21:08:06 +00002790 host->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002791 target_to_host_bitmask(tswap32(target->c_iflag), iflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002792 host->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002793 target_to_host_bitmask(tswap32(target->c_oflag), oflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002794 host->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002795 target_to_host_bitmask(tswap32(target->c_cflag), cflag_tbl);
ths5fafdf22007-09-16 21:08:06 +00002796 host->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002797 target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
2798 host->c_line = target->c_line;
ths3b46e622007-09-17 08:09:54 +00002799
ths5fafdf22007-09-16 21:08:06 +00002800 host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
2801 host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
ths3b46e622007-09-17 08:09:54 +00002802 host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
ths5fafdf22007-09-16 21:08:06 +00002803 host->c_cc[VKILL] = target->c_cc[TARGET_VKILL];
ths3b46e622007-09-17 08:09:54 +00002804 host->c_cc[VEOF] = target->c_cc[TARGET_VEOF];
ths5fafdf22007-09-16 21:08:06 +00002805 host->c_cc[VTIME] = target->c_cc[TARGET_VTIME];
ths3b46e622007-09-17 08:09:54 +00002806 host->c_cc[VMIN] = target->c_cc[TARGET_VMIN];
ths5fafdf22007-09-16 21:08:06 +00002807 host->c_cc[VSWTC] = target->c_cc[TARGET_VSWTC];
ths3b46e622007-09-17 08:09:54 +00002808 host->c_cc[VSTART] = target->c_cc[TARGET_VSTART];
ths5fafdf22007-09-16 21:08:06 +00002809 host->c_cc[VSTOP] = target->c_cc[TARGET_VSTOP];
2810 host->c_cc[VSUSP] = target->c_cc[TARGET_VSUSP];
ths3b46e622007-09-17 08:09:54 +00002811 host->c_cc[VEOL] = target->c_cc[TARGET_VEOL];
2812 host->c_cc[VREPRINT] = target->c_cc[TARGET_VREPRINT];
2813 host->c_cc[VDISCARD] = target->c_cc[TARGET_VDISCARD];
2814 host->c_cc[VWERASE] = target->c_cc[TARGET_VWERASE];
2815 host->c_cc[VLNEXT] = target->c_cc[TARGET_VLNEXT];
ths5fafdf22007-09-16 21:08:06 +00002816 host->c_cc[VEOL2] = target->c_cc[TARGET_VEOL2];
bellard31e31b82003-02-18 22:55:36 +00002817}
ths3b46e622007-09-17 08:09:54 +00002818
bellard31e31b82003-02-18 22:55:36 +00002819static void host_to_target_termios (void *dst, const void *src)
2820{
2821 struct target_termios *target = dst;
2822 const struct host_termios *host = src;
2823
ths5fafdf22007-09-16 21:08:06 +00002824 target->c_iflag =
bellard31e31b82003-02-18 22:55:36 +00002825 tswap32(host_to_target_bitmask(host->c_iflag, iflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002826 target->c_oflag =
bellard31e31b82003-02-18 22:55:36 +00002827 tswap32(host_to_target_bitmask(host->c_oflag, oflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002828 target->c_cflag =
bellard31e31b82003-02-18 22:55:36 +00002829 tswap32(host_to_target_bitmask(host->c_cflag, cflag_tbl));
ths5fafdf22007-09-16 21:08:06 +00002830 target->c_lflag =
bellard31e31b82003-02-18 22:55:36 +00002831 tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
2832 target->c_line = host->c_line;
ths3b46e622007-09-17 08:09:54 +00002833
bellard31e31b82003-02-18 22:55:36 +00002834 target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
2835 target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
2836 target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
2837 target->c_cc[TARGET_VKILL] = host->c_cc[VKILL];
2838 target->c_cc[TARGET_VEOF] = host->c_cc[VEOF];
2839 target->c_cc[TARGET_VTIME] = host->c_cc[VTIME];
2840 target->c_cc[TARGET_VMIN] = host->c_cc[VMIN];
2841 target->c_cc[TARGET_VSWTC] = host->c_cc[VSWTC];
2842 target->c_cc[TARGET_VSTART] = host->c_cc[VSTART];
2843 target->c_cc[TARGET_VSTOP] = host->c_cc[VSTOP];
2844 target->c_cc[TARGET_VSUSP] = host->c_cc[VSUSP];
2845 target->c_cc[TARGET_VEOL] = host->c_cc[VEOL];
2846 target->c_cc[TARGET_VREPRINT] = host->c_cc[VREPRINT];
2847 target->c_cc[TARGET_VDISCARD] = host->c_cc[VDISCARD];
2848 target->c_cc[TARGET_VWERASE] = host->c_cc[VWERASE];
2849 target->c_cc[TARGET_VLNEXT] = host->c_cc[VLNEXT];
2850 target->c_cc[TARGET_VEOL2] = host->c_cc[VEOL2];
2851}
2852
blueswir18e853dc2008-10-05 10:49:32 +00002853static const StructEntry struct_termios_def = {
bellard31e31b82003-02-18 22:55:36 +00002854 .convert = { host_to_target_termios, target_to_host_termios },
2855 .size = { sizeof(struct target_termios), sizeof(struct host_termios) },
2856 .align = { __alignof__(struct target_termios), __alignof__(struct host_termios) },
2857};
2858
bellard5286db72003-06-05 00:57:30 +00002859static bitmask_transtbl mmap_flags_tbl[] = {
2860 { TARGET_MAP_SHARED, TARGET_MAP_SHARED, MAP_SHARED, MAP_SHARED },
2861 { TARGET_MAP_PRIVATE, TARGET_MAP_PRIVATE, MAP_PRIVATE, MAP_PRIVATE },
2862 { TARGET_MAP_FIXED, TARGET_MAP_FIXED, MAP_FIXED, MAP_FIXED },
2863 { TARGET_MAP_ANONYMOUS, TARGET_MAP_ANONYMOUS, MAP_ANONYMOUS, MAP_ANONYMOUS },
2864 { TARGET_MAP_GROWSDOWN, TARGET_MAP_GROWSDOWN, MAP_GROWSDOWN, MAP_GROWSDOWN },
2865 { TARGET_MAP_DENYWRITE, TARGET_MAP_DENYWRITE, MAP_DENYWRITE, MAP_DENYWRITE },
2866 { TARGET_MAP_EXECUTABLE, TARGET_MAP_EXECUTABLE, MAP_EXECUTABLE, MAP_EXECUTABLE },
2867 { TARGET_MAP_LOCKED, TARGET_MAP_LOCKED, MAP_LOCKED, MAP_LOCKED },
2868 { 0, 0, 0, 0 }
2869};
2870
bellard2ab83ea2003-06-15 19:56:46 +00002871#if defined(TARGET_I386)
bellard6dbad632003-03-16 18:05:05 +00002872
2873/* NOTE: there is really one LDT for all the threads */
blueswir1b1d8e522008-10-26 13:43:07 +00002874static uint8_t *ldt_table;
bellard6dbad632003-03-16 18:05:05 +00002875
bellard03acab62007-11-11 14:57:14 +00002876static abi_long read_ldt(abi_ulong ptr, unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002877{
2878 int size;
pbrook53a59602006-03-25 19:31:22 +00002879 void *p;
bellard6dbad632003-03-16 18:05:05 +00002880
2881 if (!ldt_table)
2882 return 0;
2883 size = TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE;
2884 if (size > bytecount)
2885 size = bytecount;
bellard579a97f2007-11-11 14:26:47 +00002886 p = lock_user(VERIFY_WRITE, ptr, size, 0);
2887 if (!p)
bellard03acab62007-11-11 14:57:14 +00002888 return -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00002889 /* ??? Should this by byteswapped? */
pbrook53a59602006-03-25 19:31:22 +00002890 memcpy(p, ldt_table, size);
2891 unlock_user(p, ptr, size);
bellard6dbad632003-03-16 18:05:05 +00002892 return size;
2893}
2894
2895/* XXX: add locking support */
bellard03acab62007-11-11 14:57:14 +00002896static abi_long write_ldt(CPUX86State *env,
2897 abi_ulong ptr, unsigned long bytecount, int oldmode)
bellard6dbad632003-03-16 18:05:05 +00002898{
2899 struct target_modify_ldt_ldt_s ldt_info;
pbrook53a59602006-03-25 19:31:22 +00002900 struct target_modify_ldt_ldt_s *target_ldt_info;
bellard6dbad632003-03-16 18:05:05 +00002901 int seg_32bit, contents, read_exec_only, limit_in_pages;
bellard8d18e892007-11-14 15:18:40 +00002902 int seg_not_present, useable, lm;
bellard6dbad632003-03-16 18:05:05 +00002903 uint32_t *lp, entry_1, entry_2;
2904
2905 if (bytecount != sizeof(ldt_info))
bellard03acab62007-11-11 14:57:14 +00002906 return -TARGET_EINVAL;
bellard579a97f2007-11-11 14:26:47 +00002907 if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1))
bellard03acab62007-11-11 14:57:14 +00002908 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00002909 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
2910 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
2911 ldt_info.limit = tswap32(target_ldt_info->limit);
2912 ldt_info.flags = tswap32(target_ldt_info->flags);
2913 unlock_user_struct(target_ldt_info, ptr, 0);
ths3b46e622007-09-17 08:09:54 +00002914
bellard6dbad632003-03-16 18:05:05 +00002915 if (ldt_info.entry_number >= TARGET_LDT_ENTRIES)
bellard03acab62007-11-11 14:57:14 +00002916 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002917 seg_32bit = ldt_info.flags & 1;
2918 contents = (ldt_info.flags >> 1) & 3;
2919 read_exec_only = (ldt_info.flags >> 3) & 1;
2920 limit_in_pages = (ldt_info.flags >> 4) & 1;
2921 seg_not_present = (ldt_info.flags >> 5) & 1;
2922 useable = (ldt_info.flags >> 6) & 1;
bellard8d18e892007-11-14 15:18:40 +00002923#ifdef TARGET_ABI32
2924 lm = 0;
2925#else
2926 lm = (ldt_info.flags >> 7) & 1;
2927#endif
bellard6dbad632003-03-16 18:05:05 +00002928 if (contents == 3) {
2929 if (oldmode)
bellard03acab62007-11-11 14:57:14 +00002930 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002931 if (seg_not_present == 0)
bellard03acab62007-11-11 14:57:14 +00002932 return -TARGET_EINVAL;
bellard6dbad632003-03-16 18:05:05 +00002933 }
2934 /* allocate the LDT */
2935 if (!ldt_table) {
balroge4415702008-11-10 02:55:33 +00002936 env->ldt.base = target_mmap(0,
2937 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE,
2938 PROT_READ|PROT_WRITE,
2939 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
2940 if (env->ldt.base == -1)
bellard03acab62007-11-11 14:57:14 +00002941 return -TARGET_ENOMEM;
balroge4415702008-11-10 02:55:33 +00002942 memset(g2h(env->ldt.base), 0,
2943 TARGET_LDT_ENTRIES * TARGET_LDT_ENTRY_SIZE);
bellard6dbad632003-03-16 18:05:05 +00002944 env->ldt.limit = 0xffff;
balroge4415702008-11-10 02:55:33 +00002945 ldt_table = g2h(env->ldt.base);
bellard6dbad632003-03-16 18:05:05 +00002946 }
2947
2948 /* NOTE: same code as Linux kernel */
2949 /* Allow LDTs to be cleared by the user. */
2950 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
2951 if (oldmode ||
2952 (contents == 0 &&
2953 read_exec_only == 1 &&
2954 seg_32bit == 0 &&
2955 limit_in_pages == 0 &&
2956 seg_not_present == 1 &&
2957 useable == 0 )) {
2958 entry_1 = 0;
2959 entry_2 = 0;
2960 goto install;
2961 }
2962 }
ths3b46e622007-09-17 08:09:54 +00002963
bellard6dbad632003-03-16 18:05:05 +00002964 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
2965 (ldt_info.limit & 0x0ffff);
2966 entry_2 = (ldt_info.base_addr & 0xff000000) |
2967 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
2968 (ldt_info.limit & 0xf0000) |
2969 ((read_exec_only ^ 1) << 9) |
2970 (contents << 10) |
2971 ((seg_not_present ^ 1) << 15) |
2972 (seg_32bit << 22) |
2973 (limit_in_pages << 23) |
bellard8d18e892007-11-14 15:18:40 +00002974 (lm << 21) |
bellard6dbad632003-03-16 18:05:05 +00002975 0x7000;
2976 if (!oldmode)
2977 entry_2 |= (useable << 20);
bellard14ae3ba2003-05-27 23:25:06 +00002978
bellard6dbad632003-03-16 18:05:05 +00002979 /* Install the new entry ... */
2980install:
2981 lp = (uint32_t *)(ldt_table + (ldt_info.entry_number << 3));
2982 lp[0] = tswap32(entry_1);
2983 lp[1] = tswap32(entry_2);
2984 return 0;
2985}
2986
2987/* specific and weird i386 syscalls */
blueswir18fcd3692008-08-17 20:26:25 +00002988static abi_long do_modify_ldt(CPUX86State *env, int func, abi_ulong ptr,
2989 unsigned long bytecount)
bellard6dbad632003-03-16 18:05:05 +00002990{
bellard03acab62007-11-11 14:57:14 +00002991 abi_long ret;
ths3b46e622007-09-17 08:09:54 +00002992
bellard6dbad632003-03-16 18:05:05 +00002993 switch (func) {
2994 case 0:
2995 ret = read_ldt(ptr, bytecount);
2996 break;
2997 case 1:
2998 ret = write_ldt(env, ptr, bytecount, 1);
2999 break;
3000 case 0x11:
3001 ret = write_ldt(env, ptr, bytecount, 0);
3002 break;
bellard03acab62007-11-11 14:57:14 +00003003 default:
3004 ret = -TARGET_ENOSYS;
3005 break;
bellard6dbad632003-03-16 18:05:05 +00003006 }
3007 return ret;
3008}
bellard1b6b0292003-03-22 17:31:38 +00003009
blueswir14583f582008-08-24 10:35:55 +00003010#if defined(TARGET_I386) && defined(TARGET_ABI32)
blueswir18fcd3692008-08-17 20:26:25 +00003011static abi_long do_set_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00003012{
3013 uint64_t *gdt_table = g2h(env->gdt.base);
3014 struct target_modify_ldt_ldt_s ldt_info;
3015 struct target_modify_ldt_ldt_s *target_ldt_info;
3016 int seg_32bit, contents, read_exec_only, limit_in_pages;
3017 int seg_not_present, useable, lm;
3018 uint32_t *lp, entry_1, entry_2;
3019 int i;
3020
3021 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3022 if (!target_ldt_info)
3023 return -TARGET_EFAULT;
3024 ldt_info.entry_number = tswap32(target_ldt_info->entry_number);
3025 ldt_info.base_addr = tswapl(target_ldt_info->base_addr);
3026 ldt_info.limit = tswap32(target_ldt_info->limit);
3027 ldt_info.flags = tswap32(target_ldt_info->flags);
3028 if (ldt_info.entry_number == -1) {
3029 for (i=TARGET_GDT_ENTRY_TLS_MIN; i<=TARGET_GDT_ENTRY_TLS_MAX; i++) {
3030 if (gdt_table[i] == 0) {
3031 ldt_info.entry_number = i;
3032 target_ldt_info->entry_number = tswap32(i);
3033 break;
3034 }
3035 }
3036 }
3037 unlock_user_struct(target_ldt_info, ptr, 1);
3038
3039 if (ldt_info.entry_number < TARGET_GDT_ENTRY_TLS_MIN ||
3040 ldt_info.entry_number > TARGET_GDT_ENTRY_TLS_MAX)
3041 return -TARGET_EINVAL;
3042 seg_32bit = ldt_info.flags & 1;
3043 contents = (ldt_info.flags >> 1) & 3;
3044 read_exec_only = (ldt_info.flags >> 3) & 1;
3045 limit_in_pages = (ldt_info.flags >> 4) & 1;
3046 seg_not_present = (ldt_info.flags >> 5) & 1;
3047 useable = (ldt_info.flags >> 6) & 1;
3048#ifdef TARGET_ABI32
3049 lm = 0;
3050#else
3051 lm = (ldt_info.flags >> 7) & 1;
3052#endif
3053
3054 if (contents == 3) {
3055 if (seg_not_present == 0)
3056 return -TARGET_EINVAL;
3057 }
3058
3059 /* NOTE: same code as Linux kernel */
3060 /* Allow LDTs to be cleared by the user. */
3061 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
3062 if ((contents == 0 &&
3063 read_exec_only == 1 &&
3064 seg_32bit == 0 &&
3065 limit_in_pages == 0 &&
3066 seg_not_present == 1 &&
3067 useable == 0 )) {
3068 entry_1 = 0;
3069 entry_2 = 0;
3070 goto install;
3071 }
3072 }
3073
3074 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
3075 (ldt_info.limit & 0x0ffff);
3076 entry_2 = (ldt_info.base_addr & 0xff000000) |
3077 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
3078 (ldt_info.limit & 0xf0000) |
3079 ((read_exec_only ^ 1) << 9) |
3080 (contents << 10) |
3081 ((seg_not_present ^ 1) << 15) |
3082 (seg_32bit << 22) |
3083 (limit_in_pages << 23) |
3084 (useable << 20) |
3085 (lm << 21) |
3086 0x7000;
3087
3088 /* Install the new entry ... */
3089install:
3090 lp = (uint32_t *)(gdt_table + ldt_info.entry_number);
3091 lp[0] = tswap32(entry_1);
3092 lp[1] = tswap32(entry_2);
3093 return 0;
3094}
3095
blueswir18fcd3692008-08-17 20:26:25 +00003096static abi_long do_get_thread_area(CPUX86State *env, abi_ulong ptr)
bellard8d18e892007-11-14 15:18:40 +00003097{
3098 struct target_modify_ldt_ldt_s *target_ldt_info;
3099 uint64_t *gdt_table = g2h(env->gdt.base);
3100 uint32_t base_addr, limit, flags;
3101 int seg_32bit, contents, read_exec_only, limit_in_pages, idx;
3102 int seg_not_present, useable, lm;
3103 uint32_t *lp, entry_1, entry_2;
3104
3105 lock_user_struct(VERIFY_WRITE, target_ldt_info, ptr, 1);
3106 if (!target_ldt_info)
3107 return -TARGET_EFAULT;
3108 idx = tswap32(target_ldt_info->entry_number);
3109 if (idx < TARGET_GDT_ENTRY_TLS_MIN ||
3110 idx > TARGET_GDT_ENTRY_TLS_MAX) {
3111 unlock_user_struct(target_ldt_info, ptr, 1);
3112 return -TARGET_EINVAL;
3113 }
3114 lp = (uint32_t *)(gdt_table + idx);
3115 entry_1 = tswap32(lp[0]);
3116 entry_2 = tswap32(lp[1]);
3117
3118 read_exec_only = ((entry_2 >> 9) & 1) ^ 1;
3119 contents = (entry_2 >> 10) & 3;
3120 seg_not_present = ((entry_2 >> 15) & 1) ^ 1;
3121 seg_32bit = (entry_2 >> 22) & 1;
3122 limit_in_pages = (entry_2 >> 23) & 1;
3123 useable = (entry_2 >> 20) & 1;
3124#ifdef TARGET_ABI32
3125 lm = 0;
3126#else
3127 lm = (entry_2 >> 21) & 1;
3128#endif
3129 flags = (seg_32bit << 0) | (contents << 1) |
3130 (read_exec_only << 3) | (limit_in_pages << 4) |
3131 (seg_not_present << 5) | (useable << 6) | (lm << 7);
3132 limit = (entry_1 & 0xffff) | (entry_2 & 0xf0000);
3133 base_addr = (entry_1 >> 16) |
3134 (entry_2 & 0xff000000) |
3135 ((entry_2 & 0xff) << 16);
3136 target_ldt_info->base_addr = tswapl(base_addr);
3137 target_ldt_info->limit = tswap32(limit);
3138 target_ldt_info->flags = tswap32(flags);
3139 unlock_user_struct(target_ldt_info, ptr, 1);
3140 return 0;
3141}
blueswir14583f582008-08-24 10:35:55 +00003142#endif /* TARGET_I386 && TARGET_ABI32 */
bellard8d18e892007-11-14 15:18:40 +00003143
bellardd2fd1af2007-11-14 18:08:56 +00003144#ifndef TARGET_ABI32
blueswir18fcd3692008-08-17 20:26:25 +00003145static abi_long do_arch_prctl(CPUX86State *env, int code, abi_ulong addr)
bellardd2fd1af2007-11-14 18:08:56 +00003146{
3147 abi_long ret;
3148 abi_ulong val;
3149 int idx;
3150
3151 switch(code) {
3152 case TARGET_ARCH_SET_GS:
3153 case TARGET_ARCH_SET_FS:
3154 if (code == TARGET_ARCH_SET_GS)
3155 idx = R_GS;
3156 else
3157 idx = R_FS;
3158 cpu_x86_load_seg(env, idx, 0);
3159 env->segs[idx].base = addr;
3160 break;
3161 case TARGET_ARCH_GET_GS:
3162 case TARGET_ARCH_GET_FS:
3163 if (code == TARGET_ARCH_GET_GS)
3164 idx = R_GS;
3165 else
3166 idx = R_FS;
3167 val = env->segs[idx].base;
3168 if (put_user(val, addr, abi_ulong))
3169 return -TARGET_EFAULT;
3170 break;
3171 default:
3172 ret = -TARGET_EINVAL;
3173 break;
3174 }
3175 return 0;
3176}
3177#endif
3178
bellard2ab83ea2003-06-15 19:56:46 +00003179#endif /* defined(TARGET_I386) */
3180
pbrookd865bab2008-06-07 22:12:17 +00003181#if defined(USE_NPTL)
3182
3183#define NEW_STACK_SIZE PTHREAD_STACK_MIN
3184
3185static pthread_mutex_t clone_lock = PTHREAD_MUTEX_INITIALIZER;
3186typedef struct {
3187 CPUState *env;
3188 pthread_mutex_t mutex;
3189 pthread_cond_t cond;
3190 pthread_t thread;
3191 uint32_t tid;
3192 abi_ulong child_tidptr;
3193 abi_ulong parent_tidptr;
3194 sigset_t sigmask;
3195} new_thread_info;
3196
3197static void *clone_func(void *arg)
3198{
3199 new_thread_info *info = arg;
3200 CPUState *env;
3201
3202 env = info->env;
3203 thread_env = env;
3204 info->tid = gettid();
Nathan Froyd1e9fa732009-06-03 11:33:08 -07003205 env->host_tid = info->tid;
pbrookd865bab2008-06-07 22:12:17 +00003206 if (info->child_tidptr)
3207 put_user_u32(info->tid, info->child_tidptr);
3208 if (info->parent_tidptr)
3209 put_user_u32(info->tid, info->parent_tidptr);
3210 /* Enable signals. */
3211 sigprocmask(SIG_SETMASK, &info->sigmask, NULL);
3212 /* Signal to the parent that we're ready. */
3213 pthread_mutex_lock(&info->mutex);
3214 pthread_cond_broadcast(&info->cond);
3215 pthread_mutex_unlock(&info->mutex);
3216 /* Wait until the parent has finshed initializing the tls state. */
3217 pthread_mutex_lock(&clone_lock);
3218 pthread_mutex_unlock(&clone_lock);
3219 cpu_loop(env);
3220 /* never exits */
3221 return NULL;
3222}
3223#else
bellard1b6b0292003-03-22 17:31:38 +00003224/* this stack is the equivalent of the kernel stack associated with a
3225 thread/process */
3226#define NEW_STACK_SIZE 8192
3227
3228static int clone_func(void *arg)
3229{
bellard2ab83ea2003-06-15 19:56:46 +00003230 CPUState *env = arg;
bellard1b6b0292003-03-22 17:31:38 +00003231 cpu_loop(env);
3232 /* never exits */
3233 return 0;
3234}
pbrookd865bab2008-06-07 22:12:17 +00003235#endif
bellard1b6b0292003-03-22 17:31:38 +00003236
ths0da46a62007-10-20 20:23:07 +00003237/* do_fork() Must return host values and target errnos (unlike most
3238 do_*() functions). */
pbrookd865bab2008-06-07 22:12:17 +00003239static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,
3240 abi_ulong parent_tidptr, target_ulong newtls,
3241 abi_ulong child_tidptr)
bellard1b6b0292003-03-22 17:31:38 +00003242{
3243 int ret;
bellard5cd43932003-03-29 16:54:36 +00003244 TaskState *ts;
bellard1b6b0292003-03-22 17:31:38 +00003245 uint8_t *new_stack;
bellard2ab83ea2003-06-15 19:56:46 +00003246 CPUState *new_env;
pbrookd865bab2008-06-07 22:12:17 +00003247#if defined(USE_NPTL)
3248 unsigned int nptl_flags;
3249 sigset_t sigmask;
3250#endif
ths3b46e622007-09-17 08:09:54 +00003251
balrog436d1242008-09-21 02:39:45 +00003252 /* Emulate vfork() with fork() */
3253 if (flags & CLONE_VFORK)
3254 flags &= ~(CLONE_VFORK | CLONE_VM);
3255
bellard1b6b0292003-03-22 17:31:38 +00003256 if (flags & CLONE_VM) {
pbrookbd0c5662008-05-29 14:34:11 +00003257#if defined(USE_NPTL)
pbrookd865bab2008-06-07 22:12:17 +00003258 new_thread_info info;
3259 pthread_attr_t attr;
pbrookbd0c5662008-05-29 14:34:11 +00003260#endif
pbrookc3a92832008-06-09 14:02:50 +00003261 ts = qemu_mallocz(sizeof(TaskState) + NEW_STACK_SIZE);
pbrook624f7972008-05-31 16:11:38 +00003262 init_task_state(ts);
bellard5cd43932003-03-29 16:54:36 +00003263 new_stack = ts->stack;
bellard1b6b0292003-03-22 17:31:38 +00003264 /* we create a new CPU instance. */
thsc5be9f02007-02-28 20:20:53 +00003265 new_env = cpu_copy(env);
pbrook6e68e072008-05-30 17:22:15 +00003266 /* Init regs that differ from the parent. */
3267 cpu_clone_regs(new_env, newsp);
bellard5cd43932003-03-29 16:54:36 +00003268 new_env->opaque = ts;
pbrookd865bab2008-06-07 22:12:17 +00003269#if defined(USE_NPTL)
3270 nptl_flags = flags;
3271 flags &= ~CLONE_NPTL_FLAGS2;
3272
pbrookc2764712009-03-07 15:24:59 +00003273 if (nptl_flags & CLONE_CHILD_CLEARTID) {
3274 ts->child_tidptr = child_tidptr;
3275 }
3276
pbrookd865bab2008-06-07 22:12:17 +00003277 if (nptl_flags & CLONE_SETTLS)
3278 cpu_set_tls (new_env, newtls);
3279
3280 /* Grab a mutex so that thread setup appears atomic. */
3281 pthread_mutex_lock(&clone_lock);
3282
3283 memset(&info, 0, sizeof(info));
3284 pthread_mutex_init(&info.mutex, NULL);
3285 pthread_mutex_lock(&info.mutex);
3286 pthread_cond_init(&info.cond, NULL);
3287 info.env = new_env;
3288 if (nptl_flags & CLONE_CHILD_SETTID)
3289 info.child_tidptr = child_tidptr;
3290 if (nptl_flags & CLONE_PARENT_SETTID)
3291 info.parent_tidptr = parent_tidptr;
3292
3293 ret = pthread_attr_init(&attr);
3294 ret = pthread_attr_setstack(&attr, new_stack, NEW_STACK_SIZE);
3295 /* It is not safe to deliver signals until the child has finished
3296 initializing, so temporarily block all signals. */
3297 sigfillset(&sigmask);
3298 sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
3299
3300 ret = pthread_create(&info.thread, &attr, clone_func, &info);
pbrookc2764712009-03-07 15:24:59 +00003301 /* TODO: Free new CPU state if thread creation failed. */
pbrookd865bab2008-06-07 22:12:17 +00003302
3303 sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
3304 pthread_attr_destroy(&attr);
3305 if (ret == 0) {
3306 /* Wait for the child to initialize. */
3307 pthread_cond_wait(&info.cond, &info.mutex);
3308 ret = info.tid;
3309 if (flags & CLONE_PARENT_SETTID)
3310 put_user_u32(ret, parent_tidptr);
3311 } else {
3312 ret = -1;
3313 }
3314 pthread_mutex_unlock(&info.mutex);
3315 pthread_cond_destroy(&info.cond);
3316 pthread_mutex_destroy(&info.mutex);
3317 pthread_mutex_unlock(&clone_lock);
3318#else
3319 if (flags & CLONE_NPTL_FLAGS2)
3320 return -EINVAL;
3321 /* This is probably going to die very quickly, but do it anyway. */
bellard27725c12003-04-29 21:08:18 +00003322#ifdef __ia64__
bellardfd4a43e2006-04-24 20:32:17 +00003323 ret = __clone2(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
bellard27725c12003-04-29 21:08:18 +00003324#else
3325 ret = clone(clone_func, new_stack + NEW_STACK_SIZE, flags, new_env);
3326#endif
pbrookd865bab2008-06-07 22:12:17 +00003327#endif
bellard1b6b0292003-03-22 17:31:38 +00003328 } else {
3329 /* if no CLONE_VM, we consider it is a fork */
pbrookd865bab2008-06-07 22:12:17 +00003330 if ((flags & ~(CSIGNAL | CLONE_NPTL_FLAGS2)) != 0)
bellard1b6b0292003-03-22 17:31:38 +00003331 return -EINVAL;
pbrookd865bab2008-06-07 22:12:17 +00003332 fork_start();
bellard1b6b0292003-03-22 17:31:38 +00003333 ret = fork();
pbrookd865bab2008-06-07 22:12:17 +00003334 if (ret == 0) {
aurel322b1319c2008-12-18 22:44:04 +00003335 /* Child Process. */
pbrookd865bab2008-06-07 22:12:17 +00003336 cpu_clone_regs(env, newsp);
3337 fork_end(1);
aurel322b1319c2008-12-18 22:44:04 +00003338#if defined(USE_NPTL)
3339 /* There is a race condition here. The parent process could
3340 theoretically read the TID in the child process before the child
3341 tid is set. This would require using either ptrace
3342 (not implemented) or having *_tidptr to point at a shared memory
3343 mapping. We can't repeat the spinlock hack used above because
3344 the child process gets its own copy of the lock. */
pbrookd865bab2008-06-07 22:12:17 +00003345 if (flags & CLONE_CHILD_SETTID)
3346 put_user_u32(gettid(), child_tidptr);
3347 if (flags & CLONE_PARENT_SETTID)
3348 put_user_u32(gettid(), parent_tidptr);
3349 ts = (TaskState *)env->opaque;
3350 if (flags & CLONE_SETTLS)
3351 cpu_set_tls (env, newtls);
pbrookc2764712009-03-07 15:24:59 +00003352 if (flags & CLONE_CHILD_CLEARTID)
3353 ts->child_tidptr = child_tidptr;
aurel322b1319c2008-12-18 22:44:04 +00003354#endif
pbrookd865bab2008-06-07 22:12:17 +00003355 } else {
3356 fork_end(0);
3357 }
bellard1b6b0292003-03-22 17:31:38 +00003358 }
3359 return ret;
3360}
3361
blueswir1992f48a2007-10-14 16:27:31 +00003362static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
bellard7775e9e2003-05-14 22:46:48 +00003363{
3364 struct flock fl;
pbrook53a59602006-03-25 19:31:22 +00003365 struct target_flock *target_fl;
ths43f238d2007-01-05 20:55:49 +00003366 struct flock64 fl64;
3367 struct target_flock64 *target_fl64;
blueswir1992f48a2007-10-14 16:27:31 +00003368 abi_long ret;
pbrook53a59602006-03-25 19:31:22 +00003369
bellard7775e9e2003-05-14 22:46:48 +00003370 switch(cmd) {
3371 case TARGET_F_GETLK:
bellard579a97f2007-11-11 14:26:47 +00003372 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3373 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00003374 fl.l_type = tswap16(target_fl->l_type);
3375 fl.l_whence = tswap16(target_fl->l_whence);
3376 fl.l_start = tswapl(target_fl->l_start);
3377 fl.l_len = tswapl(target_fl->l_len);
3378 fl.l_pid = tswapl(target_fl->l_pid);
3379 unlock_user_struct(target_fl, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00003380 ret = get_errno(fcntl(fd, cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00003381 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00003382 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg, 0))
3383 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00003384 target_fl->l_type = tswap16(fl.l_type);
3385 target_fl->l_whence = tswap16(fl.l_whence);
3386 target_fl->l_start = tswapl(fl.l_start);
3387 target_fl->l_len = tswapl(fl.l_len);
3388 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00003389 unlock_user_struct(target_fl, arg, 1);
bellard7775e9e2003-05-14 22:46:48 +00003390 }
3391 break;
ths3b46e622007-09-17 08:09:54 +00003392
bellard7775e9e2003-05-14 22:46:48 +00003393 case TARGET_F_SETLK:
3394 case TARGET_F_SETLKW:
bellard579a97f2007-11-11 14:26:47 +00003395 if (!lock_user_struct(VERIFY_READ, target_fl, arg, 1))
3396 return -TARGET_EFAULT;
bellard7775e9e2003-05-14 22:46:48 +00003397 fl.l_type = tswap16(target_fl->l_type);
3398 fl.l_whence = tswap16(target_fl->l_whence);
3399 fl.l_start = tswapl(target_fl->l_start);
3400 fl.l_len = tswapl(target_fl->l_len);
3401 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00003402 unlock_user_struct(target_fl, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00003403 ret = get_errno(fcntl(fd, cmd, &fl));
bellard7775e9e2003-05-14 22:46:48 +00003404 break;
ths3b46e622007-09-17 08:09:54 +00003405
bellard7775e9e2003-05-14 22:46:48 +00003406 case TARGET_F_GETLK64:
bellard579a97f2007-11-11 14:26:47 +00003407 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3408 return -TARGET_EFAULT;
ths58134272007-03-31 18:59:32 +00003409 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3410 fl64.l_whence = tswap16(target_fl64->l_whence);
3411 fl64.l_start = tswapl(target_fl64->l_start);
3412 fl64.l_len = tswapl(target_fl64->l_len);
3413 fl64.l_pid = tswap16(target_fl64->l_pid);
3414 unlock_user_struct(target_fl64, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00003415 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
ths43f238d2007-01-05 20:55:49 +00003416 if (ret == 0) {
bellard579a97f2007-11-11 14:26:47 +00003417 if (!lock_user_struct(VERIFY_WRITE, target_fl64, arg, 0))
3418 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00003419 target_fl64->l_type = tswap16(fl64.l_type) >> 1;
3420 target_fl64->l_whence = tswap16(fl64.l_whence);
3421 target_fl64->l_start = tswapl(fl64.l_start);
3422 target_fl64->l_len = tswapl(fl64.l_len);
3423 target_fl64->l_pid = tswapl(fl64.l_pid);
3424 unlock_user_struct(target_fl64, arg, 1);
3425 }
bellard9ee1fa22007-11-11 15:11:19 +00003426 break;
bellard7775e9e2003-05-14 22:46:48 +00003427 case TARGET_F_SETLK64:
3428 case TARGET_F_SETLKW64:
bellard579a97f2007-11-11 14:26:47 +00003429 if (!lock_user_struct(VERIFY_READ, target_fl64, arg, 1))
3430 return -TARGET_EFAULT;
ths43f238d2007-01-05 20:55:49 +00003431 fl64.l_type = tswap16(target_fl64->l_type) >> 1;
3432 fl64.l_whence = tswap16(target_fl64->l_whence);
3433 fl64.l_start = tswapl(target_fl64->l_start);
3434 fl64.l_len = tswapl(target_fl64->l_len);
3435 fl64.l_pid = tswap16(target_fl64->l_pid);
3436 unlock_user_struct(target_fl64, arg, 0);
bellard9ee1fa22007-11-11 15:11:19 +00003437 ret = get_errno(fcntl(fd, cmd >> 1, &fl64));
bellard7775e9e2003-05-14 22:46:48 +00003438 break;
3439
bellardffa65c32004-01-04 23:57:22 +00003440 case F_GETFL:
bellard9ee1fa22007-11-11 15:11:19 +00003441 ret = get_errno(fcntl(fd, cmd, arg));
3442 if (ret >= 0) {
3443 ret = host_to_target_bitmask(ret, fcntl_flags_tbl);
3444 }
bellardffa65c32004-01-04 23:57:22 +00003445 break;
3446
3447 case F_SETFL:
bellard9ee1fa22007-11-11 15:11:19 +00003448 ret = get_errno(fcntl(fd, cmd, target_to_host_bitmask(arg, fcntl_flags_tbl)));
bellardffa65c32004-01-04 23:57:22 +00003449 break;
3450
bellard7775e9e2003-05-14 22:46:48 +00003451 default:
bellard9ee1fa22007-11-11 15:11:19 +00003452 ret = get_errno(fcntl(fd, cmd, arg));
bellard7775e9e2003-05-14 22:46:48 +00003453 break;
3454 }
3455 return ret;
3456}
3457
bellard67867302003-11-23 17:05:30 +00003458#ifdef USE_UID16
bellard7775e9e2003-05-14 22:46:48 +00003459
bellard67867302003-11-23 17:05:30 +00003460static inline int high2lowuid(int uid)
3461{
3462 if (uid > 65535)
3463 return 65534;
3464 else
3465 return uid;
3466}
3467
3468static inline int high2lowgid(int gid)
3469{
3470 if (gid > 65535)
3471 return 65534;
3472 else
3473 return gid;
3474}
3475
3476static inline int low2highuid(int uid)
3477{
3478 if ((int16_t)uid == -1)
3479 return -1;
3480 else
3481 return uid;
3482}
3483
3484static inline int low2highgid(int gid)
3485{
3486 if ((int16_t)gid == -1)
3487 return -1;
3488 else
3489 return gid;
3490}
3491
3492#endif /* USE_UID16 */
bellard1b6b0292003-03-22 17:31:38 +00003493
bellard31e31b82003-02-18 22:55:36 +00003494void syscall_init(void)
3495{
bellard2ab83ea2003-06-15 19:56:46 +00003496 IOCTLEntry *ie;
3497 const argtype *arg_type;
3498 int size;
thsb92c47c2007-11-01 00:07:38 +00003499 int i;
bellard2ab83ea2003-06-15 19:56:46 +00003500
Blue Swirl001faf32009-05-13 17:53:17 +00003501#define STRUCT(name, ...) thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
ths5fafdf22007-09-16 21:08:06 +00003502#define STRUCT_SPECIAL(name) thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
bellard31e31b82003-02-18 22:55:36 +00003503#include "syscall_types.h"
3504#undef STRUCT
3505#undef STRUCT_SPECIAL
bellard2ab83ea2003-06-15 19:56:46 +00003506
3507 /* we patch the ioctl size if necessary. We rely on the fact that
3508 no ioctl has all the bits at '1' in the size field */
3509 ie = ioctl_entries;
3510 while (ie->target_cmd != 0) {
3511 if (((ie->target_cmd >> TARGET_IOC_SIZESHIFT) & TARGET_IOC_SIZEMASK) ==
3512 TARGET_IOC_SIZEMASK) {
3513 arg_type = ie->arg_type;
3514 if (arg_type[0] != TYPE_PTR) {
ths5fafdf22007-09-16 21:08:06 +00003515 fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
bellard2ab83ea2003-06-15 19:56:46 +00003516 ie->target_cmd);
3517 exit(1);
3518 }
3519 arg_type++;
3520 size = thunk_type_size(arg_type, 0);
ths5fafdf22007-09-16 21:08:06 +00003521 ie->target_cmd = (ie->target_cmd &
bellard2ab83ea2003-06-15 19:56:46 +00003522 ~(TARGET_IOC_SIZEMASK << TARGET_IOC_SIZESHIFT)) |
3523 (size << TARGET_IOC_SIZESHIFT);
3524 }
thsb92c47c2007-11-01 00:07:38 +00003525
3526 /* Build target_to_host_errno_table[] table from
3527 * host_to_target_errno_table[]. */
3528 for (i=0; i < ERRNO_TABLE_SIZE; i++)
3529 target_to_host_errno_table[host_to_target_errno_table[i]] = i;
3530
bellard2ab83ea2003-06-15 19:56:46 +00003531 /* automatic consistency check if same arch */
balrog872ea0c2008-09-21 02:31:19 +00003532#if (defined(__i386__) && defined(TARGET_I386) && defined(TARGET_ABI32)) || \
3533 (defined(__x86_64__) && defined(TARGET_X86_64))
3534 if (unlikely(ie->target_cmd != ie->host_cmd)) {
3535 fprintf(stderr, "ERROR: ioctl(%s): target=0x%x host=0x%x\n",
3536 ie->name, ie->target_cmd, ie->host_cmd);
bellard2ab83ea2003-06-15 19:56:46 +00003537 }
3538#endif
3539 ie++;
3540 }
bellard31e31b82003-02-18 22:55:36 +00003541}
bellardc573ff62004-01-04 15:51:36 +00003542
blueswir1992f48a2007-10-14 16:27:31 +00003543#if TARGET_ABI_BITS == 32
pbrookce4defa2006-02-09 16:49:55 +00003544static inline uint64_t target_offset64(uint32_t word0, uint32_t word1)
3545{
thsaf325d32008-06-10 15:29:15 +00003546#ifdef TARGET_WORDS_BIGENDIAN
pbrookce4defa2006-02-09 16:49:55 +00003547 return ((uint64_t)word0 << 32) | word1;
3548#else
3549 return ((uint64_t)word1 << 32) | word0;
3550#endif
3551}
blueswir1992f48a2007-10-14 16:27:31 +00003552#else /* TARGET_ABI_BITS == 32 */
j_mayer32407102007-09-26 23:01:49 +00003553static inline uint64_t target_offset64(uint64_t word0, uint64_t word1)
3554{
3555 return word0;
3556}
blueswir1992f48a2007-10-14 16:27:31 +00003557#endif /* TARGET_ABI_BITS != 32 */
pbrookce4defa2006-02-09 16:49:55 +00003558
3559#ifdef TARGET_NR_truncate64
blueswir1992f48a2007-10-14 16:27:31 +00003560static inline abi_long target_truncate64(void *cpu_env, const char *arg1,
3561 abi_long arg2,
3562 abi_long arg3,
3563 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00003564{
3565#ifdef TARGET_ARM
3566 if (((CPUARMState *)cpu_env)->eabi)
3567 {
3568 arg2 = arg3;
3569 arg3 = arg4;
3570 }
3571#endif
3572 return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
3573}
3574#endif
3575
3576#ifdef TARGET_NR_ftruncate64
blueswir1992f48a2007-10-14 16:27:31 +00003577static inline abi_long target_ftruncate64(void *cpu_env, abi_long arg1,
3578 abi_long arg2,
3579 abi_long arg3,
3580 abi_long arg4)
pbrookce4defa2006-02-09 16:49:55 +00003581{
3582#ifdef TARGET_ARM
3583 if (((CPUARMState *)cpu_env)->eabi)
3584 {
3585 arg2 = arg3;
3586 arg3 = arg4;
3587 }
3588#endif
3589 return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
3590}
3591#endif
3592
bellard579a97f2007-11-11 14:26:47 +00003593static inline abi_long target_to_host_timespec(struct timespec *host_ts,
3594 abi_ulong target_addr)
pbrook53a59602006-03-25 19:31:22 +00003595{
3596 struct target_timespec *target_ts;
3597
bellard579a97f2007-11-11 14:26:47 +00003598 if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1))
3599 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003600 host_ts->tv_sec = tswapl(target_ts->tv_sec);
3601 host_ts->tv_nsec = tswapl(target_ts->tv_nsec);
3602 unlock_user_struct(target_ts, target_addr, 0);
bellardb255bfa2008-05-10 21:51:02 +00003603 return 0;
pbrook53a59602006-03-25 19:31:22 +00003604}
3605
bellard579a97f2007-11-11 14:26:47 +00003606static inline abi_long host_to_target_timespec(abi_ulong target_addr,
3607 struct timespec *host_ts)
pbrook53a59602006-03-25 19:31:22 +00003608{
3609 struct target_timespec *target_ts;
3610
bellard579a97f2007-11-11 14:26:47 +00003611 if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0))
3612 return -TARGET_EFAULT;
pbrook53a59602006-03-25 19:31:22 +00003613 target_ts->tv_sec = tswapl(host_ts->tv_sec);
3614 target_ts->tv_nsec = tswapl(host_ts->tv_nsec);
3615 unlock_user_struct(target_ts, target_addr, 1);
bellardb255bfa2008-05-10 21:51:02 +00003616 return 0;
pbrook53a59602006-03-25 19:31:22 +00003617}
3618
aurel329d33b762009-04-08 23:07:05 +00003619#if defined(TARGET_NR_stat64) || defined(TARGET_NR_newfstatat)
balrog6a24a772008-09-20 02:23:36 +00003620static inline abi_long host_to_target_stat64(void *cpu_env,
3621 abi_ulong target_addr,
3622 struct stat *host_st)
3623{
3624#ifdef TARGET_ARM
3625 if (((CPUARMState *)cpu_env)->eabi) {
3626 struct target_eabi_stat64 *target_st;
3627
3628 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
3629 return -TARGET_EFAULT;
3630 memset(target_st, 0, sizeof(struct target_eabi_stat64));
3631 __put_user(host_st->st_dev, &target_st->st_dev);
3632 __put_user(host_st->st_ino, &target_st->st_ino);
3633#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
3634 __put_user(host_st->st_ino, &target_st->__st_ino);
3635#endif
3636 __put_user(host_st->st_mode, &target_st->st_mode);
3637 __put_user(host_st->st_nlink, &target_st->st_nlink);
3638 __put_user(host_st->st_uid, &target_st->st_uid);
3639 __put_user(host_st->st_gid, &target_st->st_gid);
3640 __put_user(host_st->st_rdev, &target_st->st_rdev);
3641 __put_user(host_st->st_size, &target_st->st_size);
3642 __put_user(host_st->st_blksize, &target_st->st_blksize);
3643 __put_user(host_st->st_blocks, &target_st->st_blocks);
3644 __put_user(host_st->st_atime, &target_st->target_st_atime);
3645 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
3646 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
3647 unlock_user_struct(target_st, target_addr, 1);
3648 } else
3649#endif
3650 {
aurel329d33b762009-04-08 23:07:05 +00003651#if TARGET_LONG_BITS == 64
3652 struct target_stat *target_st;
3653#else
balrog6a24a772008-09-20 02:23:36 +00003654 struct target_stat64 *target_st;
aurel329d33b762009-04-08 23:07:05 +00003655#endif
balrog6a24a772008-09-20 02:23:36 +00003656
3657 if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0))
3658 return -TARGET_EFAULT;
aurel329d33b762009-04-08 23:07:05 +00003659 memset(target_st, 0, sizeof(*target_st));
balrog6a24a772008-09-20 02:23:36 +00003660 __put_user(host_st->st_dev, &target_st->st_dev);
3661 __put_user(host_st->st_ino, &target_st->st_ino);
3662#ifdef TARGET_STAT64_HAS_BROKEN_ST_INO
3663 __put_user(host_st->st_ino, &target_st->__st_ino);
3664#endif
3665 __put_user(host_st->st_mode, &target_st->st_mode);
3666 __put_user(host_st->st_nlink, &target_st->st_nlink);
3667 __put_user(host_st->st_uid, &target_st->st_uid);
3668 __put_user(host_st->st_gid, &target_st->st_gid);
3669 __put_user(host_st->st_rdev, &target_st->st_rdev);
3670 /* XXX: better use of kernel struct */
3671 __put_user(host_st->st_size, &target_st->st_size);
3672 __put_user(host_st->st_blksize, &target_st->st_blksize);
3673 __put_user(host_st->st_blocks, &target_st->st_blocks);
3674 __put_user(host_st->st_atime, &target_st->target_st_atime);
3675 __put_user(host_st->st_mtime, &target_st->target_st_mtime);
3676 __put_user(host_st->st_ctime, &target_st->target_st_ctime);
3677 unlock_user_struct(target_st, target_addr, 1);
3678 }
3679
3680 return 0;
3681}
3682#endif
3683
pbrookbd0c5662008-05-29 14:34:11 +00003684#if defined(USE_NPTL)
3685/* ??? Using host futex calls even when target atomic operations
3686 are not really atomic probably breaks things. However implementing
3687 futexes locally would make futexes shared between multiple processes
3688 tricky. However they're probably useless because guest atomic
3689 operations won't work either. */
blueswir18fcd3692008-08-17 20:26:25 +00003690static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
3691 target_ulong uaddr2, int val3)
pbrookbd0c5662008-05-29 14:34:11 +00003692{
3693 struct timespec ts, *pts;
3694
3695 /* ??? We assume FUTEX_* constants are the same on both host
3696 and target. */
3697 switch (op) {
3698 case FUTEX_WAIT:
3699 if (timeout) {
3700 pts = &ts;
3701 target_to_host_timespec(pts, timeout);
3702 } else {
3703 pts = NULL;
3704 }
3705 return get_errno(sys_futex(g2h(uaddr), FUTEX_WAIT, tswap32(val),
3706 pts, NULL, 0));
3707 case FUTEX_WAKE:
3708 return get_errno(sys_futex(g2h(uaddr), FUTEX_WAKE, val, NULL, NULL, 0));
3709 case FUTEX_FD:
3710 return get_errno(sys_futex(g2h(uaddr), FUTEX_FD, val, NULL, NULL, 0));
3711 case FUTEX_REQUEUE:
3712 return get_errno(sys_futex(g2h(uaddr), FUTEX_REQUEUE, val,
3713 NULL, g2h(uaddr2), 0));
3714 case FUTEX_CMP_REQUEUE:
3715 return get_errno(sys_futex(g2h(uaddr), FUTEX_CMP_REQUEUE, val,
3716 NULL, g2h(uaddr2), tswap32(val3)));
3717 default:
3718 return -TARGET_ENOSYS;
3719 }
3720}
3721#endif
3722
pbrook1d9d8b52009-04-16 15:17:02 +00003723/* Map host to target signal numbers for the wait family of syscalls.
3724 Assume all other status bits are the same. */
3725static int host_to_target_waitstatus(int status)
3726{
3727 if (WIFSIGNALED(status)) {
3728 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
3729 }
3730 if (WIFSTOPPED(status)) {
3731 return (host_to_target_signal(WSTOPSIG(status)) << 8)
3732 | (status & 0xff);
3733 }
3734 return status;
3735}
3736
pbrooka745ec62008-05-06 15:36:17 +00003737int get_osversion(void)
3738{
3739 static int osversion;
3740 struct new_utsname buf;
3741 const char *s;
3742 int i, n, tmp;
3743 if (osversion)
3744 return osversion;
3745 if (qemu_uname_release && *qemu_uname_release) {
3746 s = qemu_uname_release;
3747 } else {
3748 if (sys_uname(&buf))
3749 return 0;
3750 s = buf.release;
3751 }
3752 tmp = 0;
3753 for (i = 0; i < 3; i++) {
3754 n = 0;
3755 while (*s >= '0' && *s <= '9') {
3756 n *= 10;
3757 n += *s - '0';
3758 s++;
3759 }
3760 tmp = (tmp << 8) + n;
3761 if (*s == '.')
3762 s++;
3763 }
3764 osversion = tmp;
3765 return osversion;
3766}
3767
ths0da46a62007-10-20 20:23:07 +00003768/* do_syscall() should always have a single exit point at the end so
3769 that actions, such as logging of syscall results, can be performed.
3770 All errnos that do_syscall() returns must be -TARGET_<errcode>. */
blueswir1992f48a2007-10-14 16:27:31 +00003771abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
3772 abi_long arg2, abi_long arg3, abi_long arg4,
3773 abi_long arg5, abi_long arg6)
bellard31e31b82003-02-18 22:55:36 +00003774{
blueswir1992f48a2007-10-14 16:27:31 +00003775 abi_long ret;
bellard31e31b82003-02-18 22:55:36 +00003776 struct stat st;
bellard56c8f682005-11-28 22:28:41 +00003777 struct statfs stfs;
pbrook53a59602006-03-25 19:31:22 +00003778 void *p;
ths3b46e622007-09-17 08:09:54 +00003779
bellard72f03902003-02-18 23:33:18 +00003780#ifdef DEBUG
bellardc573ff62004-01-04 15:51:36 +00003781 gemu_log("syscall %d", num);
bellard72f03902003-02-18 23:33:18 +00003782#endif
thsb92c47c2007-11-01 00:07:38 +00003783 if(do_strace)
3784 print_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
3785
bellard31e31b82003-02-18 22:55:36 +00003786 switch(num) {
3787 case TARGET_NR_exit:
pbrookc2764712009-03-07 15:24:59 +00003788#ifdef USE_NPTL
3789 /* In old applications this may be used to implement _exit(2).
3790 However in threaded applictions it is used for thread termination,
3791 and _exit_group is used for application termination.
3792 Do thread termination if we have more then one thread. */
3793 /* FIXME: This probably breaks if a signal arrives. We should probably
3794 be disabling signals. */
3795 if (first_cpu->next_cpu) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07003796 TaskState *ts;
pbrookc2764712009-03-07 15:24:59 +00003797 CPUState **lastp;
3798 CPUState *p;
3799
3800 cpu_list_lock();
3801 lastp = &first_cpu;
3802 p = first_cpu;
3803 while (p && p != (CPUState *)cpu_env) {
3804 lastp = &p->next_cpu;
3805 p = p->next_cpu;
3806 }
3807 /* If we didn't find the CPU for this thread then something is
3808 horribly wrong. */
3809 if (!p)
3810 abort();
3811 /* Remove the CPU from the list. */
3812 *lastp = p->next_cpu;
3813 cpu_list_unlock();
Nathan Froyd1e9fa732009-06-03 11:33:08 -07003814 ts = ((CPUState *)cpu_env)->opaque;
pbrookc2764712009-03-07 15:24:59 +00003815 if (ts->child_tidptr) {
3816 put_user_u32(0, ts->child_tidptr);
3817 sys_futex(g2h(ts->child_tidptr), FUTEX_WAKE, INT_MAX,
3818 NULL, NULL, 0);
3819 }
3820 /* TODO: Free CPU state. */
3821 pthread_exit(NULL);
3822 }
3823#endif
bellard7d132992003-03-06 23:23:54 +00003824#ifdef HAVE_GPROF
3825 _mcleanup();
3826#endif
bellarde9009672005-04-26 20:42:36 +00003827 gdb_exit(cpu_env, arg1);
pbrookc2764712009-03-07 15:24:59 +00003828 _exit(arg1);
bellard31e31b82003-02-18 22:55:36 +00003829 ret = 0; /* avoid warning */
3830 break;
3831 case TARGET_NR_read:
aurel3238d840e2009-01-30 19:48:17 +00003832 if (arg3 == 0)
3833 ret = 0;
3834 else {
3835 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
3836 goto efault;
3837 ret = get_errno(read(arg1, p, arg3));
3838 unlock_user(p, arg2, ret);
3839 }
bellard31e31b82003-02-18 22:55:36 +00003840 break;
3841 case TARGET_NR_write:
bellard579a97f2007-11-11 14:26:47 +00003842 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
3843 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003844 ret = get_errno(write(arg1, p, arg3));
3845 unlock_user(p, arg2, 0);
bellard31e31b82003-02-18 22:55:36 +00003846 break;
3847 case TARGET_NR_open:
bellard2f619692007-11-16 10:46:05 +00003848 if (!(p = lock_user_string(arg1)))
3849 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003850 ret = get_errno(open(path(p),
bellardffa65c32004-01-04 23:57:22 +00003851 target_to_host_bitmask(arg2, fcntl_flags_tbl),
3852 arg3));
pbrook53a59602006-03-25 19:31:22 +00003853 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003854 break;
ths82424832007-09-24 09:21:55 +00003855#if defined(TARGET_NR_openat) && defined(__NR_openat)
3856 case TARGET_NR_openat:
bellard579a97f2007-11-11 14:26:47 +00003857 if (!(p = lock_user_string(arg2)))
3858 goto efault;
3859 ret = get_errno(sys_openat(arg1,
3860 path(p),
3861 target_to_host_bitmask(arg3, fcntl_flags_tbl),
3862 arg4));
3863 unlock_user(p, arg2, 0);
ths82424832007-09-24 09:21:55 +00003864 break;
3865#endif
bellard31e31b82003-02-18 22:55:36 +00003866 case TARGET_NR_close:
3867 ret = get_errno(close(arg1));
3868 break;
3869 case TARGET_NR_brk:
pbrook53a59602006-03-25 19:31:22 +00003870 ret = do_brk(arg1);
bellard31e31b82003-02-18 22:55:36 +00003871 break;
3872 case TARGET_NR_fork:
pbrookd865bab2008-06-07 22:12:17 +00003873 ret = get_errno(do_fork(cpu_env, SIGCHLD, 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00003874 break;
thse5febef2007-04-01 18:31:35 +00003875#ifdef TARGET_NR_waitpid
bellard31e31b82003-02-18 22:55:36 +00003876 case TARGET_NR_waitpid:
3877 {
pbrook53a59602006-03-25 19:31:22 +00003878 int status;
3879 ret = get_errno(waitpid(arg1, &status, arg3));
bellard2f619692007-11-16 10:46:05 +00003880 if (!is_error(ret) && arg2
pbrook1d9d8b52009-04-16 15:17:02 +00003881 && put_user_s32(host_to_target_waitstatus(status), arg2))
bellard2f619692007-11-16 10:46:05 +00003882 goto efault;
bellard31e31b82003-02-18 22:55:36 +00003883 }
3884 break;
thse5febef2007-04-01 18:31:35 +00003885#endif
pbrookf0cbb612008-05-30 18:20:05 +00003886#ifdef TARGET_NR_waitid
3887 case TARGET_NR_waitid:
3888 {
3889 siginfo_t info;
3890 info.si_pid = 0;
3891 ret = get_errno(waitid(arg1, arg2, &info, arg4));
3892 if (!is_error(ret) && arg3 && info.si_pid != 0) {
3893 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_siginfo_t), 0)))
3894 goto efault;
3895 host_to_target_siginfo(p, &info);
3896 unlock_user(p, arg3, sizeof(target_siginfo_t));
3897 }
3898 }
3899 break;
3900#endif
j_mayer7a3148a2007-04-05 07:13:51 +00003901#ifdef TARGET_NR_creat /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00003902 case TARGET_NR_creat:
bellard579a97f2007-11-11 14:26:47 +00003903 if (!(p = lock_user_string(arg1)))
3904 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003905 ret = get_errno(creat(p, arg2));
3906 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003907 break;
j_mayer7a3148a2007-04-05 07:13:51 +00003908#endif
bellard31e31b82003-02-18 22:55:36 +00003909 case TARGET_NR_link:
pbrook53a59602006-03-25 19:31:22 +00003910 {
3911 void * p2;
3912 p = lock_user_string(arg1);
3913 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00003914 if (!p || !p2)
3915 ret = -TARGET_EFAULT;
3916 else
3917 ret = get_errno(link(p, p2));
pbrook53a59602006-03-25 19:31:22 +00003918 unlock_user(p2, arg2, 0);
3919 unlock_user(p, arg1, 0);
3920 }
bellard31e31b82003-02-18 22:55:36 +00003921 break;
ths64f0ce42007-09-24 09:25:06 +00003922#if defined(TARGET_NR_linkat) && defined(__NR_linkat)
3923 case TARGET_NR_linkat:
ths64f0ce42007-09-24 09:25:06 +00003924 {
3925 void * p2 = NULL;
bellard579a97f2007-11-11 14:26:47 +00003926 if (!arg2 || !arg4)
3927 goto efault;
ths64f0ce42007-09-24 09:25:06 +00003928 p = lock_user_string(arg2);
3929 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00003930 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00003931 ret = -TARGET_EFAULT;
ths64f0ce42007-09-24 09:25:06 +00003932 else
3933 ret = get_errno(sys_linkat(arg1, p, arg3, p2, arg5));
bellard579a97f2007-11-11 14:26:47 +00003934 unlock_user(p, arg2, 0);
3935 unlock_user(p2, arg4, 0);
ths64f0ce42007-09-24 09:25:06 +00003936 }
3937 break;
3938#endif
bellard31e31b82003-02-18 22:55:36 +00003939 case TARGET_NR_unlink:
bellard579a97f2007-11-11 14:26:47 +00003940 if (!(p = lock_user_string(arg1)))
3941 goto efault;
pbrook53a59602006-03-25 19:31:22 +00003942 ret = get_errno(unlink(p));
3943 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00003944 break;
ths8170f562007-09-24 09:24:11 +00003945#if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
3946 case TARGET_NR_unlinkat:
bellard579a97f2007-11-11 14:26:47 +00003947 if (!(p = lock_user_string(arg2)))
3948 goto efault;
3949 ret = get_errno(sys_unlinkat(arg1, p, arg3));
3950 unlock_user(p, arg2, 0);
balroged494d82007-12-11 23:23:52 +00003951 break;
balrogb7d35e62007-12-12 00:40:24 +00003952#endif
bellard31e31b82003-02-18 22:55:36 +00003953 case TARGET_NR_execve:
bellard7854b052003-03-29 17:22:23 +00003954 {
3955 char **argp, **envp;
bellardf7341ff2003-03-30 21:00:25 +00003956 int argc, envc;
blueswir1992f48a2007-10-14 16:27:31 +00003957 abi_ulong gp;
3958 abi_ulong guest_argp;
3959 abi_ulong guest_envp;
3960 abi_ulong addr;
bellard7854b052003-03-29 17:22:23 +00003961 char **q;
3962
bellardf7341ff2003-03-30 21:00:25 +00003963 argc = 0;
pbrook53a59602006-03-25 19:31:22 +00003964 guest_argp = arg2;
pbrookda94d262008-05-30 18:24:00 +00003965 for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00003966 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00003967 goto efault;
ths03aa1972007-12-02 06:28:08 +00003968 if (!addr)
bellard2f619692007-11-16 10:46:05 +00003969 break;
bellard7854b052003-03-29 17:22:23 +00003970 argc++;
bellard2f619692007-11-16 10:46:05 +00003971 }
bellardf7341ff2003-03-30 21:00:25 +00003972 envc = 0;
pbrook53a59602006-03-25 19:31:22 +00003973 guest_envp = arg3;
pbrookda94d262008-05-30 18:24:00 +00003974 for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
ths03aa1972007-12-02 06:28:08 +00003975 if (get_user_ual(addr, gp))
bellard2f619692007-11-16 10:46:05 +00003976 goto efault;
ths03aa1972007-12-02 06:28:08 +00003977 if (!addr)
bellard2f619692007-11-16 10:46:05 +00003978 break;
bellard7854b052003-03-29 17:22:23 +00003979 envc++;
bellard2f619692007-11-16 10:46:05 +00003980 }
bellard7854b052003-03-29 17:22:23 +00003981
bellardf7341ff2003-03-30 21:00:25 +00003982 argp = alloca((argc + 1) * sizeof(void *));
3983 envp = alloca((envc + 1) * sizeof(void *));
bellard7854b052003-03-29 17:22:23 +00003984
pbrookda94d262008-05-30 18:24:00 +00003985 for (gp = guest_argp, q = argp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00003986 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00003987 if (get_user_ual(addr, gp))
3988 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003989 if (!addr)
3990 break;
bellard2f619692007-11-16 10:46:05 +00003991 if (!(*q = lock_user_string(addr)))
3992 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00003993 }
bellardf7341ff2003-03-30 21:00:25 +00003994 *q = NULL;
3995
pbrookda94d262008-05-30 18:24:00 +00003996 for (gp = guest_envp, q = envp; gp;
blueswir1992f48a2007-10-14 16:27:31 +00003997 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00003998 if (get_user_ual(addr, gp))
3999 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004000 if (!addr)
4001 break;
bellard2f619692007-11-16 10:46:05 +00004002 if (!(*q = lock_user_string(addr)))
4003 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004004 }
bellardf7341ff2003-03-30 21:00:25 +00004005 *q = NULL;
bellard7854b052003-03-29 17:22:23 +00004006
bellard2f619692007-11-16 10:46:05 +00004007 if (!(p = lock_user_string(arg1)))
4008 goto execve_efault;
pbrook53a59602006-03-25 19:31:22 +00004009 ret = get_errno(execve(p, argp, envp));
4010 unlock_user(p, arg1, 0);
4011
bellard2f619692007-11-16 10:46:05 +00004012 goto execve_end;
4013
4014 execve_efault:
4015 ret = -TARGET_EFAULT;
4016
4017 execve_end:
pbrook53a59602006-03-25 19:31:22 +00004018 for (gp = guest_argp, q = argp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00004019 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004020 if (get_user_ual(addr, gp)
4021 || !addr)
4022 break;
pbrook53a59602006-03-25 19:31:22 +00004023 unlock_user(*q, addr, 0);
4024 }
4025 for (gp = guest_envp, q = envp; *q;
blueswir1992f48a2007-10-14 16:27:31 +00004026 gp += sizeof(abi_ulong), q++) {
bellard2f619692007-11-16 10:46:05 +00004027 if (get_user_ual(addr, gp)
4028 || !addr)
4029 break;
pbrook53a59602006-03-25 19:31:22 +00004030 unlock_user(*q, addr, 0);
4031 }
bellard7854b052003-03-29 17:22:23 +00004032 }
bellard31e31b82003-02-18 22:55:36 +00004033 break;
4034 case TARGET_NR_chdir:
bellard579a97f2007-11-11 14:26:47 +00004035 if (!(p = lock_user_string(arg1)))
4036 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004037 ret = get_errno(chdir(p));
4038 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004039 break;
bellarda315a142005-01-30 22:59:18 +00004040#ifdef TARGET_NR_time
bellard31e31b82003-02-18 22:55:36 +00004041 case TARGET_NR_time:
4042 {
pbrook53a59602006-03-25 19:31:22 +00004043 time_t host_time;
4044 ret = get_errno(time(&host_time));
bellard2f619692007-11-16 10:46:05 +00004045 if (!is_error(ret)
4046 && arg1
4047 && put_user_sal(host_time, arg1))
4048 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004049 }
4050 break;
bellarda315a142005-01-30 22:59:18 +00004051#endif
bellard31e31b82003-02-18 22:55:36 +00004052 case TARGET_NR_mknod:
bellard579a97f2007-11-11 14:26:47 +00004053 if (!(p = lock_user_string(arg1)))
4054 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004055 ret = get_errno(mknod(p, arg2, arg3));
4056 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004057 break;
ths75ac37a2007-09-24 09:23:05 +00004058#if defined(TARGET_NR_mknodat) && defined(__NR_mknodat)
4059 case TARGET_NR_mknodat:
bellard579a97f2007-11-11 14:26:47 +00004060 if (!(p = lock_user_string(arg2)))
4061 goto efault;
4062 ret = get_errno(sys_mknodat(arg1, p, arg3, arg4));
4063 unlock_user(p, arg2, 0);
ths75ac37a2007-09-24 09:23:05 +00004064 break;
4065#endif
bellard31e31b82003-02-18 22:55:36 +00004066 case TARGET_NR_chmod:
bellard579a97f2007-11-11 14:26:47 +00004067 if (!(p = lock_user_string(arg1)))
4068 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004069 ret = get_errno(chmod(p, arg2));
4070 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004071 break;
bellardebc05482003-09-30 21:08:41 +00004072#ifdef TARGET_NR_break
bellard31e31b82003-02-18 22:55:36 +00004073 case TARGET_NR_break:
4074 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004075#endif
4076#ifdef TARGET_NR_oldstat
bellard31e31b82003-02-18 22:55:36 +00004077 case TARGET_NR_oldstat:
4078 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004079#endif
bellard31e31b82003-02-18 22:55:36 +00004080 case TARGET_NR_lseek:
4081 ret = get_errno(lseek(arg1, arg2, arg3));
4082 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004083#ifdef TARGET_NR_getxpid
4084 case TARGET_NR_getxpid:
4085#else
bellard31e31b82003-02-18 22:55:36 +00004086 case TARGET_NR_getpid:
j_mayer7a3148a2007-04-05 07:13:51 +00004087#endif
bellard31e31b82003-02-18 22:55:36 +00004088 ret = get_errno(getpid());
4089 break;
4090 case TARGET_NR_mount:
ths80265912007-02-17 22:25:41 +00004091 {
4092 /* need to look at the data field */
4093 void *p2, *p3;
4094 p = lock_user_string(arg1);
4095 p2 = lock_user_string(arg2);
4096 p3 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00004097 if (!p || !p2 || !p3)
4098 ret = -TARGET_EFAULT;
4099 else
4100 /* FIXME - arg5 should be locked, but it isn't clear how to
4101 * do that since it's not guaranteed to be a NULL-terminated
4102 * string.
4103 */
4104 ret = get_errno(mount(p, p2, p3, (unsigned long)arg4, g2h(arg5)));
4105 unlock_user(p, arg1, 0);
4106 unlock_user(p2, arg2, 0);
4107 unlock_user(p3, arg3, 0);
ths80265912007-02-17 22:25:41 +00004108 break;
4109 }
thse5febef2007-04-01 18:31:35 +00004110#ifdef TARGET_NR_umount
bellard31e31b82003-02-18 22:55:36 +00004111 case TARGET_NR_umount:
bellard579a97f2007-11-11 14:26:47 +00004112 if (!(p = lock_user_string(arg1)))
4113 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004114 ret = get_errno(umount(p));
4115 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004116 break;
thse5febef2007-04-01 18:31:35 +00004117#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004118#ifdef TARGET_NR_stime /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004119 case TARGET_NR_stime:
4120 {
pbrook53a59602006-03-25 19:31:22 +00004121 time_t host_time;
bellard2f619692007-11-16 10:46:05 +00004122 if (get_user_sal(host_time, arg1))
4123 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004124 ret = get_errno(stime(&host_time));
bellard31e31b82003-02-18 22:55:36 +00004125 }
4126 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004127#endif
bellard31e31b82003-02-18 22:55:36 +00004128 case TARGET_NR_ptrace:
4129 goto unimplemented;
j_mayer7a3148a2007-04-05 07:13:51 +00004130#ifdef TARGET_NR_alarm /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004131 case TARGET_NR_alarm:
4132 ret = alarm(arg1);
4133 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004134#endif
bellardebc05482003-09-30 21:08:41 +00004135#ifdef TARGET_NR_oldfstat
bellard31e31b82003-02-18 22:55:36 +00004136 case TARGET_NR_oldfstat:
4137 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004138#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004139#ifdef TARGET_NR_pause /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004140 case TARGET_NR_pause:
4141 ret = get_errno(pause());
4142 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004143#endif
thse5febef2007-04-01 18:31:35 +00004144#ifdef TARGET_NR_utime
bellard31e31b82003-02-18 22:55:36 +00004145 case TARGET_NR_utime:
bellardebc05482003-09-30 21:08:41 +00004146 {
pbrook53a59602006-03-25 19:31:22 +00004147 struct utimbuf tbuf, *host_tbuf;
4148 struct target_utimbuf *target_tbuf;
4149 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00004150 if (!lock_user_struct(VERIFY_READ, target_tbuf, arg2, 1))
4151 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004152 tbuf.actime = tswapl(target_tbuf->actime);
4153 tbuf.modtime = tswapl(target_tbuf->modtime);
4154 unlock_user_struct(target_tbuf, arg2, 0);
4155 host_tbuf = &tbuf;
bellardf72e8ff2004-05-03 19:23:07 +00004156 } else {
pbrook53a59602006-03-25 19:31:22 +00004157 host_tbuf = NULL;
bellardf72e8ff2004-05-03 19:23:07 +00004158 }
bellard579a97f2007-11-11 14:26:47 +00004159 if (!(p = lock_user_string(arg1)))
4160 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004161 ret = get_errno(utime(p, host_tbuf));
4162 unlock_user(p, arg1, 0);
bellardebc05482003-09-30 21:08:41 +00004163 }
4164 break;
thse5febef2007-04-01 18:31:35 +00004165#endif
bellard978a66f2004-12-06 22:58:05 +00004166 case TARGET_NR_utimes:
4167 {
bellard978a66f2004-12-06 22:58:05 +00004168 struct timeval *tvp, tv[2];
pbrook53a59602006-03-25 19:31:22 +00004169 if (arg2) {
ths788f5ec2007-12-09 02:37:05 +00004170 if (copy_from_user_timeval(&tv[0], arg2)
4171 || copy_from_user_timeval(&tv[1],
4172 arg2 + sizeof(struct target_timeval)))
4173 goto efault;
bellard978a66f2004-12-06 22:58:05 +00004174 tvp = tv;
4175 } else {
4176 tvp = NULL;
4177 }
bellard579a97f2007-11-11 14:26:47 +00004178 if (!(p = lock_user_string(arg1)))
4179 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004180 ret = get_errno(utimes(p, tvp));
4181 unlock_user(p, arg1, 0);
bellard978a66f2004-12-06 22:58:05 +00004182 }
4183 break;
balrogac8a6552008-09-20 02:25:39 +00004184#if defined(TARGET_NR_futimesat) && defined(__NR_futimesat)
4185 case TARGET_NR_futimesat:
4186 {
4187 struct timeval *tvp, tv[2];
4188 if (arg3) {
4189 if (copy_from_user_timeval(&tv[0], arg3)
4190 || copy_from_user_timeval(&tv[1],
4191 arg3 + sizeof(struct target_timeval)))
4192 goto efault;
4193 tvp = tv;
4194 } else {
4195 tvp = NULL;
4196 }
4197 if (!(p = lock_user_string(arg2)))
4198 goto efault;
4199 ret = get_errno(sys_futimesat(arg1, path(p), tvp));
4200 unlock_user(p, arg2, 0);
4201 }
4202 break;
4203#endif
bellardebc05482003-09-30 21:08:41 +00004204#ifdef TARGET_NR_stty
bellard31e31b82003-02-18 22:55:36 +00004205 case TARGET_NR_stty:
4206 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004207#endif
4208#ifdef TARGET_NR_gtty
bellard31e31b82003-02-18 22:55:36 +00004209 case TARGET_NR_gtty:
4210 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004211#endif
bellard31e31b82003-02-18 22:55:36 +00004212 case TARGET_NR_access:
bellard579a97f2007-11-11 14:26:47 +00004213 if (!(p = lock_user_string(arg1)))
4214 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004215 ret = get_errno(access(p, arg2));
4216 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004217 break;
ths92a34c12007-09-24 09:27:49 +00004218#if defined(TARGET_NR_faccessat) && defined(__NR_faccessat)
4219 case TARGET_NR_faccessat:
bellard579a97f2007-11-11 14:26:47 +00004220 if (!(p = lock_user_string(arg2)))
4221 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00004222 ret = get_errno(sys_faccessat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00004223 unlock_user(p, arg2, 0);
ths92a34c12007-09-24 09:27:49 +00004224 break;
4225#endif
j_mayer7a3148a2007-04-05 07:13:51 +00004226#ifdef TARGET_NR_nice /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004227 case TARGET_NR_nice:
4228 ret = get_errno(nice(arg1));
4229 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004230#endif
bellardebc05482003-09-30 21:08:41 +00004231#ifdef TARGET_NR_ftime
bellard31e31b82003-02-18 22:55:36 +00004232 case TARGET_NR_ftime:
4233 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004234#endif
bellard31e31b82003-02-18 22:55:36 +00004235 case TARGET_NR_sync:
bellard04369ff2003-03-20 22:33:23 +00004236 sync();
4237 ret = 0;
bellard31e31b82003-02-18 22:55:36 +00004238 break;
4239 case TARGET_NR_kill:
pbrook4cb05962008-05-30 18:05:19 +00004240 ret = get_errno(kill(arg1, target_to_host_signal(arg2)));
bellard31e31b82003-02-18 22:55:36 +00004241 break;
4242 case TARGET_NR_rename:
pbrook53a59602006-03-25 19:31:22 +00004243 {
4244 void *p2;
4245 p = lock_user_string(arg1);
4246 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004247 if (!p || !p2)
4248 ret = -TARGET_EFAULT;
4249 else
4250 ret = get_errno(rename(p, p2));
pbrook53a59602006-03-25 19:31:22 +00004251 unlock_user(p2, arg2, 0);
4252 unlock_user(p, arg1, 0);
4253 }
bellard31e31b82003-02-18 22:55:36 +00004254 break;
ths722183f2007-09-24 09:24:37 +00004255#if defined(TARGET_NR_renameat) && defined(__NR_renameat)
4256 case TARGET_NR_renameat:
ths722183f2007-09-24 09:24:37 +00004257 {
bellard579a97f2007-11-11 14:26:47 +00004258 void *p2;
ths722183f2007-09-24 09:24:37 +00004259 p = lock_user_string(arg2);
4260 p2 = lock_user_string(arg4);
bellard579a97f2007-11-11 14:26:47 +00004261 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004262 ret = -TARGET_EFAULT;
ths722183f2007-09-24 09:24:37 +00004263 else
4264 ret = get_errno(sys_renameat(arg1, p, arg3, p2));
bellard579a97f2007-11-11 14:26:47 +00004265 unlock_user(p2, arg4, 0);
4266 unlock_user(p, arg2, 0);
ths722183f2007-09-24 09:24:37 +00004267 }
4268 break;
4269#endif
bellard31e31b82003-02-18 22:55:36 +00004270 case TARGET_NR_mkdir:
bellard579a97f2007-11-11 14:26:47 +00004271 if (!(p = lock_user_string(arg1)))
4272 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004273 ret = get_errno(mkdir(p, arg2));
4274 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004275 break;
ths4472ad02007-09-24 09:22:32 +00004276#if defined(TARGET_NR_mkdirat) && defined(__NR_mkdirat)
4277 case TARGET_NR_mkdirat:
bellard579a97f2007-11-11 14:26:47 +00004278 if (!(p = lock_user_string(arg2)))
4279 goto efault;
4280 ret = get_errno(sys_mkdirat(arg1, p, arg3));
4281 unlock_user(p, arg2, 0);
ths4472ad02007-09-24 09:22:32 +00004282 break;
4283#endif
bellard31e31b82003-02-18 22:55:36 +00004284 case TARGET_NR_rmdir:
bellard579a97f2007-11-11 14:26:47 +00004285 if (!(p = lock_user_string(arg1)))
4286 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004287 ret = get_errno(rmdir(p));
4288 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004289 break;
4290 case TARGET_NR_dup:
4291 ret = get_errno(dup(arg1));
4292 break;
4293 case TARGET_NR_pipe:
4294 {
pbrook53a59602006-03-25 19:31:22 +00004295 int host_pipe[2];
4296 ret = get_errno(pipe(host_pipe));
bellard31e31b82003-02-18 22:55:36 +00004297 if (!is_error(ret)) {
thsc12ab052007-06-01 11:50:36 +00004298#if defined(TARGET_MIPS)
thsead93602007-09-06 00:18:15 +00004299 CPUMIPSState *env = (CPUMIPSState*)cpu_env;
thsb5dc7732008-06-27 10:02:35 +00004300 env->active_tc.gpr[3] = host_pipe[1];
thsc12ab052007-06-01 11:50:36 +00004301 ret = host_pipe[0];
aurel32b5eff352008-03-11 23:30:22 +00004302#elif defined(TARGET_SH4)
4303 ((CPUSH4State*)cpu_env)->gregs[1] = host_pipe[1];
4304 ret = host_pipe[0];
thsc12ab052007-06-01 11:50:36 +00004305#else
bellard2f619692007-11-16 10:46:05 +00004306 if (put_user_s32(host_pipe[0], arg1)
4307 || put_user_s32(host_pipe[1], arg1 + sizeof(host_pipe[0])))
4308 goto efault;
thsc12ab052007-06-01 11:50:36 +00004309#endif
bellard31e31b82003-02-18 22:55:36 +00004310 }
4311 }
4312 break;
4313 case TARGET_NR_times:
bellard32f36bc2003-03-30 21:29:48 +00004314 {
pbrook53a59602006-03-25 19:31:22 +00004315 struct target_tms *tmsp;
bellard32f36bc2003-03-30 21:29:48 +00004316 struct tms tms;
4317 ret = get_errno(times(&tms));
pbrook53a59602006-03-25 19:31:22 +00004318 if (arg1) {
bellard579a97f2007-11-11 14:26:47 +00004319 tmsp = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_tms), 0);
4320 if (!tmsp)
4321 goto efault;
bellardc596ed12003-07-13 17:32:31 +00004322 tmsp->tms_utime = tswapl(host_to_target_clock_t(tms.tms_utime));
4323 tmsp->tms_stime = tswapl(host_to_target_clock_t(tms.tms_stime));
4324 tmsp->tms_cutime = tswapl(host_to_target_clock_t(tms.tms_cutime));
4325 tmsp->tms_cstime = tswapl(host_to_target_clock_t(tms.tms_cstime));
bellard32f36bc2003-03-30 21:29:48 +00004326 }
bellardc596ed12003-07-13 17:32:31 +00004327 if (!is_error(ret))
4328 ret = host_to_target_clock_t(ret);
bellard32f36bc2003-03-30 21:29:48 +00004329 }
4330 break;
bellardebc05482003-09-30 21:08:41 +00004331#ifdef TARGET_NR_prof
bellard31e31b82003-02-18 22:55:36 +00004332 case TARGET_NR_prof:
4333 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004334#endif
thse5febef2007-04-01 18:31:35 +00004335#ifdef TARGET_NR_signal
bellard31e31b82003-02-18 22:55:36 +00004336 case TARGET_NR_signal:
4337 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004338#endif
bellard31e31b82003-02-18 22:55:36 +00004339 case TARGET_NR_acct:
aurel3238d840e2009-01-30 19:48:17 +00004340 if (arg1 == 0) {
4341 ret = get_errno(acct(NULL));
4342 } else {
4343 if (!(p = lock_user_string(arg1)))
4344 goto efault;
4345 ret = get_errno(acct(path(p)));
4346 unlock_user(p, arg1, 0);
4347 }
pbrook24836682006-04-16 14:14:53 +00004348 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004349#ifdef TARGET_NR_umount2 /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004350 case TARGET_NR_umount2:
bellard579a97f2007-11-11 14:26:47 +00004351 if (!(p = lock_user_string(arg1)))
4352 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004353 ret = get_errno(umount2(p, arg2));
4354 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004355 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004356#endif
bellardebc05482003-09-30 21:08:41 +00004357#ifdef TARGET_NR_lock
bellard31e31b82003-02-18 22:55:36 +00004358 case TARGET_NR_lock:
4359 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004360#endif
bellard31e31b82003-02-18 22:55:36 +00004361 case TARGET_NR_ioctl:
4362 ret = do_ioctl(arg1, arg2, arg3);
4363 break;
4364 case TARGET_NR_fcntl:
bellard9ee1fa22007-11-11 15:11:19 +00004365 ret = do_fcntl(arg1, arg2, arg3);
bellard31e31b82003-02-18 22:55:36 +00004366 break;
bellardebc05482003-09-30 21:08:41 +00004367#ifdef TARGET_NR_mpx
bellard31e31b82003-02-18 22:55:36 +00004368 case TARGET_NR_mpx:
4369 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004370#endif
bellard31e31b82003-02-18 22:55:36 +00004371 case TARGET_NR_setpgid:
4372 ret = get_errno(setpgid(arg1, arg2));
4373 break;
bellardebc05482003-09-30 21:08:41 +00004374#ifdef TARGET_NR_ulimit
bellard31e31b82003-02-18 22:55:36 +00004375 case TARGET_NR_ulimit:
4376 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004377#endif
4378#ifdef TARGET_NR_oldolduname
bellard31e31b82003-02-18 22:55:36 +00004379 case TARGET_NR_oldolduname:
4380 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004381#endif
bellard31e31b82003-02-18 22:55:36 +00004382 case TARGET_NR_umask:
4383 ret = get_errno(umask(arg1));
4384 break;
4385 case TARGET_NR_chroot:
bellard579a97f2007-11-11 14:26:47 +00004386 if (!(p = lock_user_string(arg1)))
4387 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004388 ret = get_errno(chroot(p));
4389 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004390 break;
4391 case TARGET_NR_ustat:
4392 goto unimplemented;
4393 case TARGET_NR_dup2:
4394 ret = get_errno(dup2(arg1, arg2));
4395 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004396#ifdef TARGET_NR_getppid /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004397 case TARGET_NR_getppid:
4398 ret = get_errno(getppid());
4399 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004400#endif
bellard31e31b82003-02-18 22:55:36 +00004401 case TARGET_NR_getpgrp:
4402 ret = get_errno(getpgrp());
4403 break;
4404 case TARGET_NR_setsid:
4405 ret = get_errno(setsid());
4406 break;
thse5febef2007-04-01 18:31:35 +00004407#ifdef TARGET_NR_sigaction
bellard31e31b82003-02-18 22:55:36 +00004408 case TARGET_NR_sigaction:
bellard31e31b82003-02-18 22:55:36 +00004409 {
ths388bb212007-05-13 13:58:00 +00004410#if !defined(TARGET_MIPS)
pbrook53a59602006-03-25 19:31:22 +00004411 struct target_old_sigaction *old_act;
bellard66fb9762003-03-23 01:06:05 +00004412 struct target_sigaction act, oact, *pact;
pbrook53a59602006-03-25 19:31:22 +00004413 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00004414 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4415 goto efault;
bellard66fb9762003-03-23 01:06:05 +00004416 act._sa_handler = old_act->_sa_handler;
4417 target_siginitset(&act.sa_mask, old_act->sa_mask);
4418 act.sa_flags = old_act->sa_flags;
4419 act.sa_restorer = old_act->sa_restorer;
pbrook53a59602006-03-25 19:31:22 +00004420 unlock_user_struct(old_act, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00004421 pact = &act;
4422 } else {
4423 pact = NULL;
4424 }
4425 ret = get_errno(do_sigaction(arg1, pact, &oact));
pbrook53a59602006-03-25 19:31:22 +00004426 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00004427 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4428 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004429 old_act->_sa_handler = oact._sa_handler;
4430 old_act->sa_mask = oact.sa_mask.sig[0];
4431 old_act->sa_flags = oact.sa_flags;
4432 old_act->sa_restorer = oact.sa_restorer;
4433 unlock_user_struct(old_act, arg3, 1);
bellard66fb9762003-03-23 01:06:05 +00004434 }
ths388bb212007-05-13 13:58:00 +00004435#else
bellard106ec872006-06-27 21:08:10 +00004436 struct target_sigaction act, oact, *pact, *old_act;
4437
4438 if (arg2) {
bellard579a97f2007-11-11 14:26:47 +00004439 if (!lock_user_struct(VERIFY_READ, old_act, arg2, 1))
4440 goto efault;
bellard106ec872006-06-27 21:08:10 +00004441 act._sa_handler = old_act->_sa_handler;
4442 target_siginitset(&act.sa_mask, old_act->sa_mask.sig[0]);
4443 act.sa_flags = old_act->sa_flags;
4444 unlock_user_struct(old_act, arg2, 0);
4445 pact = &act;
4446 } else {
4447 pact = NULL;
4448 }
4449
4450 ret = get_errno(do_sigaction(arg1, pact, &oact));
4451
4452 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00004453 if (!lock_user_struct(VERIFY_WRITE, old_act, arg3, 0))
4454 goto efault;
bellard106ec872006-06-27 21:08:10 +00004455 old_act->_sa_handler = oact._sa_handler;
4456 old_act->sa_flags = oact.sa_flags;
4457 old_act->sa_mask.sig[0] = oact.sa_mask.sig[0];
4458 old_act->sa_mask.sig[1] = 0;
4459 old_act->sa_mask.sig[2] = 0;
4460 old_act->sa_mask.sig[3] = 0;
4461 unlock_user_struct(old_act, arg3, 1);
4462 }
ths388bb212007-05-13 13:58:00 +00004463#endif
bellard31e31b82003-02-18 22:55:36 +00004464 }
4465 break;
thse5febef2007-04-01 18:31:35 +00004466#endif
bellard66fb9762003-03-23 01:06:05 +00004467 case TARGET_NR_rt_sigaction:
pbrook53a59602006-03-25 19:31:22 +00004468 {
4469 struct target_sigaction *act;
4470 struct target_sigaction *oact;
4471
bellard579a97f2007-11-11 14:26:47 +00004472 if (arg2) {
4473 if (!lock_user_struct(VERIFY_READ, act, arg2, 1))
4474 goto efault;
4475 } else
pbrook53a59602006-03-25 19:31:22 +00004476 act = NULL;
bellard579a97f2007-11-11 14:26:47 +00004477 if (arg3) {
4478 if (!lock_user_struct(VERIFY_WRITE, oact, arg3, 0)) {
4479 ret = -TARGET_EFAULT;
4480 goto rt_sigaction_fail;
4481 }
4482 } else
pbrook53a59602006-03-25 19:31:22 +00004483 oact = NULL;
4484 ret = get_errno(do_sigaction(arg1, act, oact));
bellard579a97f2007-11-11 14:26:47 +00004485 rt_sigaction_fail:
4486 if (act)
pbrook53a59602006-03-25 19:31:22 +00004487 unlock_user_struct(act, arg2, 0);
bellard579a97f2007-11-11 14:26:47 +00004488 if (oact)
pbrook53a59602006-03-25 19:31:22 +00004489 unlock_user_struct(oact, arg3, 1);
4490 }
bellard66fb9762003-03-23 01:06:05 +00004491 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004492#ifdef TARGET_NR_sgetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004493 case TARGET_NR_sgetmask:
bellard66fb9762003-03-23 01:06:05 +00004494 {
4495 sigset_t cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00004496 abi_ulong target_set;
bellard66fb9762003-03-23 01:06:05 +00004497 sigprocmask(0, NULL, &cur_set);
4498 host_to_target_old_sigset(&target_set, &cur_set);
4499 ret = target_set;
4500 }
4501 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004502#endif
4503#ifdef TARGET_NR_ssetmask /* not on alpha */
bellard31e31b82003-02-18 22:55:36 +00004504 case TARGET_NR_ssetmask:
bellard66fb9762003-03-23 01:06:05 +00004505 {
4506 sigset_t set, oset, cur_set;
blueswir1992f48a2007-10-14 16:27:31 +00004507 abi_ulong target_set = arg1;
bellard66fb9762003-03-23 01:06:05 +00004508 sigprocmask(0, NULL, &cur_set);
4509 target_to_host_old_sigset(&set, &target_set);
4510 sigorset(&set, &set, &cur_set);
4511 sigprocmask(SIG_SETMASK, &set, &oset);
4512 host_to_target_old_sigset(&target_set, &oset);
4513 ret = target_set;
4514 }
4515 break;
j_mayer7a3148a2007-04-05 07:13:51 +00004516#endif
thse5febef2007-04-01 18:31:35 +00004517#ifdef TARGET_NR_sigprocmask
bellard66fb9762003-03-23 01:06:05 +00004518 case TARGET_NR_sigprocmask:
4519 {
4520 int how = arg1;
4521 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00004522
pbrook53a59602006-03-25 19:31:22 +00004523 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00004524 switch(how) {
4525 case TARGET_SIG_BLOCK:
4526 how = SIG_BLOCK;
4527 break;
4528 case TARGET_SIG_UNBLOCK:
4529 how = SIG_UNBLOCK;
4530 break;
4531 case TARGET_SIG_SETMASK:
4532 how = SIG_SETMASK;
4533 break;
4534 default:
ths0da46a62007-10-20 20:23:07 +00004535 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00004536 goto fail;
4537 }
bellard579a97f2007-11-11 14:26:47 +00004538 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
4539 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004540 target_to_host_old_sigset(&set, p);
4541 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00004542 set_ptr = &set;
4543 } else {
4544 how = 0;
4545 set_ptr = NULL;
4546 }
4547 ret = get_errno(sigprocmask(arg1, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00004548 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00004549 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
4550 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004551 host_to_target_old_sigset(p, &oldset);
4552 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00004553 }
4554 }
4555 break;
thse5febef2007-04-01 18:31:35 +00004556#endif
bellard66fb9762003-03-23 01:06:05 +00004557 case TARGET_NR_rt_sigprocmask:
4558 {
4559 int how = arg1;
4560 sigset_t set, oldset, *set_ptr;
ths3b46e622007-09-17 08:09:54 +00004561
pbrook53a59602006-03-25 19:31:22 +00004562 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00004563 switch(how) {
4564 case TARGET_SIG_BLOCK:
4565 how = SIG_BLOCK;
4566 break;
4567 case TARGET_SIG_UNBLOCK:
4568 how = SIG_UNBLOCK;
4569 break;
4570 case TARGET_SIG_SETMASK:
4571 how = SIG_SETMASK;
4572 break;
4573 default:
ths0da46a62007-10-20 20:23:07 +00004574 ret = -TARGET_EINVAL;
bellard66fb9762003-03-23 01:06:05 +00004575 goto fail;
4576 }
bellard579a97f2007-11-11 14:26:47 +00004577 if (!(p = lock_user(VERIFY_READ, arg2, sizeof(target_sigset_t), 1)))
4578 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004579 target_to_host_sigset(&set, p);
4580 unlock_user(p, arg2, 0);
bellard66fb9762003-03-23 01:06:05 +00004581 set_ptr = &set;
4582 } else {
4583 how = 0;
4584 set_ptr = NULL;
4585 }
4586 ret = get_errno(sigprocmask(how, set_ptr, &oldset));
pbrook53a59602006-03-25 19:31:22 +00004587 if (!is_error(ret) && arg3) {
bellard579a97f2007-11-11 14:26:47 +00004588 if (!(p = lock_user(VERIFY_WRITE, arg3, sizeof(target_sigset_t), 0)))
4589 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004590 host_to_target_sigset(p, &oldset);
4591 unlock_user(p, arg3, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00004592 }
4593 }
4594 break;
thse5febef2007-04-01 18:31:35 +00004595#ifdef TARGET_NR_sigpending
bellard66fb9762003-03-23 01:06:05 +00004596 case TARGET_NR_sigpending:
4597 {
4598 sigset_t set;
4599 ret = get_errno(sigpending(&set));
4600 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004601 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
4602 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004603 host_to_target_old_sigset(p, &set);
4604 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00004605 }
4606 }
4607 break;
thse5febef2007-04-01 18:31:35 +00004608#endif
bellard66fb9762003-03-23 01:06:05 +00004609 case TARGET_NR_rt_sigpending:
4610 {
4611 sigset_t set;
4612 ret = get_errno(sigpending(&set));
4613 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004614 if (!(p = lock_user(VERIFY_WRITE, arg1, sizeof(target_sigset_t), 0)))
4615 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004616 host_to_target_sigset(p, &set);
4617 unlock_user(p, arg1, sizeof(target_sigset_t));
bellard66fb9762003-03-23 01:06:05 +00004618 }
4619 }
4620 break;
thse5febef2007-04-01 18:31:35 +00004621#ifdef TARGET_NR_sigsuspend
bellard66fb9762003-03-23 01:06:05 +00004622 case TARGET_NR_sigsuspend:
4623 {
4624 sigset_t set;
bellard579a97f2007-11-11 14:26:47 +00004625 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4626 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004627 target_to_host_old_sigset(&set, p);
4628 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00004629 ret = get_errno(sigsuspend(&set));
4630 }
4631 break;
thse5febef2007-04-01 18:31:35 +00004632#endif
bellard66fb9762003-03-23 01:06:05 +00004633 case TARGET_NR_rt_sigsuspend:
4634 {
4635 sigset_t set;
bellard579a97f2007-11-11 14:26:47 +00004636 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4637 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004638 target_to_host_sigset(&set, p);
4639 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00004640 ret = get_errno(sigsuspend(&set));
4641 }
4642 break;
4643 case TARGET_NR_rt_sigtimedwait:
4644 {
bellard66fb9762003-03-23 01:06:05 +00004645 sigset_t set;
4646 struct timespec uts, *puts;
4647 siginfo_t uinfo;
ths3b46e622007-09-17 08:09:54 +00004648
bellard579a97f2007-11-11 14:26:47 +00004649 if (!(p = lock_user(VERIFY_READ, arg1, sizeof(target_sigset_t), 1)))
4650 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004651 target_to_host_sigset(&set, p);
4652 unlock_user(p, arg1, 0);
4653 if (arg3) {
bellard66fb9762003-03-23 01:06:05 +00004654 puts = &uts;
pbrook53a59602006-03-25 19:31:22 +00004655 target_to_host_timespec(puts, arg3);
bellard66fb9762003-03-23 01:06:05 +00004656 } else {
4657 puts = NULL;
4658 }
4659 ret = get_errno(sigtimedwait(&set, &uinfo, puts));
pbrook53a59602006-03-25 19:31:22 +00004660 if (!is_error(ret) && arg2) {
pbrooke1e3f302008-05-30 21:53:38 +00004661 if (!(p = lock_user(VERIFY_WRITE, arg2, sizeof(target_siginfo_t), 0)))
bellard579a97f2007-11-11 14:26:47 +00004662 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004663 host_to_target_siginfo(p, &uinfo);
pbrooke1e3f302008-05-30 21:53:38 +00004664 unlock_user(p, arg2, sizeof(target_siginfo_t));
bellard66fb9762003-03-23 01:06:05 +00004665 }
4666 }
4667 break;
4668 case TARGET_NR_rt_sigqueueinfo:
4669 {
4670 siginfo_t uinfo;
bellard579a97f2007-11-11 14:26:47 +00004671 if (!(p = lock_user(VERIFY_READ, arg3, sizeof(target_sigset_t), 1)))
4672 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004673 target_to_host_siginfo(&uinfo, p);
4674 unlock_user(p, arg1, 0);
bellard66fb9762003-03-23 01:06:05 +00004675 ret = get_errno(sys_rt_sigqueueinfo(arg1, arg2, &uinfo));
4676 }
4677 break;
thse5febef2007-04-01 18:31:35 +00004678#ifdef TARGET_NR_sigreturn
bellard66fb9762003-03-23 01:06:05 +00004679 case TARGET_NR_sigreturn:
4680 /* NOTE: ret is eax, so not transcoding must be done */
4681 ret = do_sigreturn(cpu_env);
4682 break;
thse5febef2007-04-01 18:31:35 +00004683#endif
bellard66fb9762003-03-23 01:06:05 +00004684 case TARGET_NR_rt_sigreturn:
4685 /* NOTE: ret is eax, so not transcoding must be done */
4686 ret = do_rt_sigreturn(cpu_env);
4687 break;
bellard31e31b82003-02-18 22:55:36 +00004688 case TARGET_NR_sethostname:
bellard579a97f2007-11-11 14:26:47 +00004689 if (!(p = lock_user_string(arg1)))
4690 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004691 ret = get_errno(sethostname(p, arg2));
4692 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004693 break;
4694 case TARGET_NR_setrlimit:
bellard9de5e442003-03-23 16:49:39 +00004695 {
4696 /* XXX: convert resource ? */
4697 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00004698 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00004699 struct rlimit rlim;
bellard579a97f2007-11-11 14:26:47 +00004700 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1))
4701 goto efault;
bellard9de5e442003-03-23 16:49:39 +00004702 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
4703 rlim.rlim_max = tswapl(target_rlim->rlim_max);
pbrook53a59602006-03-25 19:31:22 +00004704 unlock_user_struct(target_rlim, arg2, 0);
bellard9de5e442003-03-23 16:49:39 +00004705 ret = get_errno(setrlimit(resource, &rlim));
4706 }
4707 break;
bellard31e31b82003-02-18 22:55:36 +00004708 case TARGET_NR_getrlimit:
bellard9de5e442003-03-23 16:49:39 +00004709 {
4710 /* XXX: convert resource ? */
4711 int resource = arg1;
pbrook53a59602006-03-25 19:31:22 +00004712 struct target_rlimit *target_rlim;
bellard9de5e442003-03-23 16:49:39 +00004713 struct rlimit rlim;
ths3b46e622007-09-17 08:09:54 +00004714
bellard9de5e442003-03-23 16:49:39 +00004715 ret = get_errno(getrlimit(resource, &rlim));
4716 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00004717 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
4718 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004719 rlim.rlim_cur = tswapl(target_rlim->rlim_cur);
4720 rlim.rlim_max = tswapl(target_rlim->rlim_max);
4721 unlock_user_struct(target_rlim, arg2, 1);
bellard9de5e442003-03-23 16:49:39 +00004722 }
4723 }
4724 break;
bellard31e31b82003-02-18 22:55:36 +00004725 case TARGET_NR_getrusage:
bellardb4091862003-05-16 15:39:34 +00004726 {
4727 struct rusage rusage;
bellardb4091862003-05-16 15:39:34 +00004728 ret = get_errno(getrusage(arg1, &rusage));
4729 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004730 host_to_target_rusage(arg2, &rusage);
bellardb4091862003-05-16 15:39:34 +00004731 }
4732 }
4733 break;
bellard31e31b82003-02-18 22:55:36 +00004734 case TARGET_NR_gettimeofday:
4735 {
bellard31e31b82003-02-18 22:55:36 +00004736 struct timeval tv;
4737 ret = get_errno(gettimeofday(&tv, NULL));
4738 if (!is_error(ret)) {
ths788f5ec2007-12-09 02:37:05 +00004739 if (copy_to_user_timeval(arg1, &tv))
4740 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004741 }
4742 }
4743 break;
4744 case TARGET_NR_settimeofday:
4745 {
bellard31e31b82003-02-18 22:55:36 +00004746 struct timeval tv;
ths788f5ec2007-12-09 02:37:05 +00004747 if (copy_from_user_timeval(&tv, arg1))
4748 goto efault;
bellard31e31b82003-02-18 22:55:36 +00004749 ret = get_errno(settimeofday(&tv, NULL));
4750 }
4751 break;
bellard048f6b42005-11-26 18:47:20 +00004752#ifdef TARGET_NR_select
bellard31e31b82003-02-18 22:55:36 +00004753 case TARGET_NR_select:
bellardf2674e32003-07-09 12:26:09 +00004754 {
pbrook53a59602006-03-25 19:31:22 +00004755 struct target_sel_arg_struct *sel;
blueswir1992f48a2007-10-14 16:27:31 +00004756 abi_ulong inp, outp, exp, tvp;
pbrook53a59602006-03-25 19:31:22 +00004757 long nsel;
4758
bellard579a97f2007-11-11 14:26:47 +00004759 if (!lock_user_struct(VERIFY_READ, sel, arg1, 1))
4760 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004761 nsel = tswapl(sel->n);
4762 inp = tswapl(sel->inp);
4763 outp = tswapl(sel->outp);
4764 exp = tswapl(sel->exp);
4765 tvp = tswapl(sel->tvp);
4766 unlock_user_struct(sel, arg1, 0);
4767 ret = do_select(nsel, inp, outp, exp, tvp);
bellardf2674e32003-07-09 12:26:09 +00004768 }
4769 break;
bellard048f6b42005-11-26 18:47:20 +00004770#endif
bellard31e31b82003-02-18 22:55:36 +00004771 case TARGET_NR_symlink:
pbrook53a59602006-03-25 19:31:22 +00004772 {
4773 void *p2;
4774 p = lock_user_string(arg1);
4775 p2 = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004776 if (!p || !p2)
4777 ret = -TARGET_EFAULT;
4778 else
4779 ret = get_errno(symlink(p, p2));
pbrook53a59602006-03-25 19:31:22 +00004780 unlock_user(p2, arg2, 0);
4781 unlock_user(p, arg1, 0);
4782 }
bellard31e31b82003-02-18 22:55:36 +00004783 break;
thsf0b62432007-09-24 09:25:40 +00004784#if defined(TARGET_NR_symlinkat) && defined(__NR_symlinkat)
4785 case TARGET_NR_symlinkat:
thsf0b62432007-09-24 09:25:40 +00004786 {
bellard579a97f2007-11-11 14:26:47 +00004787 void *p2;
thsf0b62432007-09-24 09:25:40 +00004788 p = lock_user_string(arg1);
4789 p2 = lock_user_string(arg3);
bellard579a97f2007-11-11 14:26:47 +00004790 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004791 ret = -TARGET_EFAULT;
thsf0b62432007-09-24 09:25:40 +00004792 else
4793 ret = get_errno(sys_symlinkat(p, arg2, p2));
bellard579a97f2007-11-11 14:26:47 +00004794 unlock_user(p2, arg3, 0);
4795 unlock_user(p, arg1, 0);
thsf0b62432007-09-24 09:25:40 +00004796 }
4797 break;
4798#endif
bellardebc05482003-09-30 21:08:41 +00004799#ifdef TARGET_NR_oldlstat
bellard31e31b82003-02-18 22:55:36 +00004800 case TARGET_NR_oldlstat:
4801 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004802#endif
bellard31e31b82003-02-18 22:55:36 +00004803 case TARGET_NR_readlink:
pbrook53a59602006-03-25 19:31:22 +00004804 {
aurel32d088d662009-01-30 20:09:01 +00004805 void *p2, *temp;
pbrook53a59602006-03-25 19:31:22 +00004806 p = lock_user_string(arg1);
bellard579a97f2007-11-11 14:26:47 +00004807 p2 = lock_user(VERIFY_WRITE, arg2, arg3, 0);
4808 if (!p || !p2)
4809 ret = -TARGET_EFAULT;
aurel32d088d662009-01-30 20:09:01 +00004810 else {
4811 if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) {
4812 char real[PATH_MAX];
4813 temp = realpath(exec_path,real);
4814 ret = (temp==NULL) ? get_errno(-1) : strlen(real) ;
4815 snprintf((char *)p2, arg3, "%s", real);
4816 }
4817 else
4818 ret = get_errno(readlink(path(p), p2, arg3));
aurel32d088d662009-01-30 20:09:01 +00004819 }
pbrook53a59602006-03-25 19:31:22 +00004820 unlock_user(p2, arg2, ret);
4821 unlock_user(p, arg1, 0);
4822 }
bellard31e31b82003-02-18 22:55:36 +00004823 break;
ths5e0ccb12007-09-24 09:26:10 +00004824#if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat)
4825 case TARGET_NR_readlinkat:
ths5e0ccb12007-09-24 09:26:10 +00004826 {
bellard579a97f2007-11-11 14:26:47 +00004827 void *p2;
ths5e0ccb12007-09-24 09:26:10 +00004828 p = lock_user_string(arg2);
bellard579a97f2007-11-11 14:26:47 +00004829 p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0);
4830 if (!p || !p2)
ths0da46a62007-10-20 20:23:07 +00004831 ret = -TARGET_EFAULT;
ths5e0ccb12007-09-24 09:26:10 +00004832 else
4833 ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4));
bellard579a97f2007-11-11 14:26:47 +00004834 unlock_user(p2, arg3, ret);
4835 unlock_user(p, arg2, 0);
ths5e0ccb12007-09-24 09:26:10 +00004836 }
4837 break;
4838#endif
thse5febef2007-04-01 18:31:35 +00004839#ifdef TARGET_NR_uselib
bellard31e31b82003-02-18 22:55:36 +00004840 case TARGET_NR_uselib:
4841 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004842#endif
4843#ifdef TARGET_NR_swapon
bellard31e31b82003-02-18 22:55:36 +00004844 case TARGET_NR_swapon:
bellard579a97f2007-11-11 14:26:47 +00004845 if (!(p = lock_user_string(arg1)))
4846 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004847 ret = get_errno(swapon(p, arg2));
4848 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004849 break;
thse5febef2007-04-01 18:31:35 +00004850#endif
bellard31e31b82003-02-18 22:55:36 +00004851 case TARGET_NR_reboot:
4852 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004853#ifdef TARGET_NR_readdir
bellard31e31b82003-02-18 22:55:36 +00004854 case TARGET_NR_readdir:
4855 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00004856#endif
4857#ifdef TARGET_NR_mmap
bellard31e31b82003-02-18 22:55:36 +00004858 case TARGET_NR_mmap:
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02004859#if (defined(TARGET_I386) && defined(TARGET_ABI32)) || defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_CRIS) || defined(TARGET_MICROBLAZE)
bellard31e31b82003-02-18 22:55:36 +00004860 {
blueswir1992f48a2007-10-14 16:27:31 +00004861 abi_ulong *v;
4862 abi_ulong v1, v2, v3, v4, v5, v6;
bellard579a97f2007-11-11 14:26:47 +00004863 if (!(v = lock_user(VERIFY_READ, arg1, 6 * sizeof(abi_ulong), 1)))
4864 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004865 v1 = tswapl(v[0]);
4866 v2 = tswapl(v[1]);
4867 v3 = tswapl(v[2]);
4868 v4 = tswapl(v[3]);
4869 v5 = tswapl(v[4]);
4870 v6 = tswapl(v[5]);
4871 unlock_user(v, arg1, 0);
ths5fafdf22007-09-16 21:08:06 +00004872 ret = get_errno(target_mmap(v1, v2, v3,
bellard5286db72003-06-05 00:57:30 +00004873 target_to_host_bitmask(v4, mmap_flags_tbl),
4874 v5, v6));
bellard31e31b82003-02-18 22:55:36 +00004875 }
bellard31e31b82003-02-18 22:55:36 +00004876#else
ths5fafdf22007-09-16 21:08:06 +00004877 ret = get_errno(target_mmap(arg1, arg2, arg3,
4878 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard6fb883e2003-07-09 17:12:39 +00004879 arg5,
4880 arg6));
bellard31e31b82003-02-18 22:55:36 +00004881#endif
bellard6fb883e2003-07-09 17:12:39 +00004882 break;
thse5febef2007-04-01 18:31:35 +00004883#endif
bellarda315a142005-01-30 22:59:18 +00004884#ifdef TARGET_NR_mmap2
bellard6fb883e2003-07-09 17:12:39 +00004885 case TARGET_NR_mmap2:
pbrookbb7ec042008-03-25 22:28:25 +00004886#ifndef MMAP_SHIFT
bellardc573ff62004-01-04 15:51:36 +00004887#define MMAP_SHIFT 12
bellardc573ff62004-01-04 15:51:36 +00004888#endif
ths5fafdf22007-09-16 21:08:06 +00004889 ret = get_errno(target_mmap(arg1, arg2, arg3,
4890 target_to_host_bitmask(arg4, mmap_flags_tbl),
bellard5286db72003-06-05 00:57:30 +00004891 arg5,
bellardc573ff62004-01-04 15:51:36 +00004892 arg6 << MMAP_SHIFT));
bellard31e31b82003-02-18 22:55:36 +00004893 break;
bellarda315a142005-01-30 22:59:18 +00004894#endif
bellard31e31b82003-02-18 22:55:36 +00004895 case TARGET_NR_munmap:
bellard54936002003-05-13 00:25:15 +00004896 ret = get_errno(target_munmap(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00004897 break;
bellard9de5e442003-03-23 16:49:39 +00004898 case TARGET_NR_mprotect:
bellard54936002003-05-13 00:25:15 +00004899 ret = get_errno(target_mprotect(arg1, arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00004900 break;
thse5febef2007-04-01 18:31:35 +00004901#ifdef TARGET_NR_mremap
bellard9de5e442003-03-23 16:49:39 +00004902 case TARGET_NR_mremap:
bellard54936002003-05-13 00:25:15 +00004903 ret = get_errno(target_mremap(arg1, arg2, arg3, arg4, arg5));
bellard9de5e442003-03-23 16:49:39 +00004904 break;
thse5febef2007-04-01 18:31:35 +00004905#endif
pbrook53a59602006-03-25 19:31:22 +00004906 /* ??? msync/mlock/munlock are broken for softmmu. */
thse5febef2007-04-01 18:31:35 +00004907#ifdef TARGET_NR_msync
bellard9de5e442003-03-23 16:49:39 +00004908 case TARGET_NR_msync:
pbrook53a59602006-03-25 19:31:22 +00004909 ret = get_errno(msync(g2h(arg1), arg2, arg3));
bellard9de5e442003-03-23 16:49:39 +00004910 break;
thse5febef2007-04-01 18:31:35 +00004911#endif
4912#ifdef TARGET_NR_mlock
bellard9de5e442003-03-23 16:49:39 +00004913 case TARGET_NR_mlock:
pbrook53a59602006-03-25 19:31:22 +00004914 ret = get_errno(mlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00004915 break;
thse5febef2007-04-01 18:31:35 +00004916#endif
4917#ifdef TARGET_NR_munlock
bellard9de5e442003-03-23 16:49:39 +00004918 case TARGET_NR_munlock:
pbrook53a59602006-03-25 19:31:22 +00004919 ret = get_errno(munlock(g2h(arg1), arg2));
bellard9de5e442003-03-23 16:49:39 +00004920 break;
thse5febef2007-04-01 18:31:35 +00004921#endif
4922#ifdef TARGET_NR_mlockall
bellard9de5e442003-03-23 16:49:39 +00004923 case TARGET_NR_mlockall:
4924 ret = get_errno(mlockall(arg1));
4925 break;
thse5febef2007-04-01 18:31:35 +00004926#endif
4927#ifdef TARGET_NR_munlockall
bellard9de5e442003-03-23 16:49:39 +00004928 case TARGET_NR_munlockall:
4929 ret = get_errno(munlockall());
4930 break;
thse5febef2007-04-01 18:31:35 +00004931#endif
bellard31e31b82003-02-18 22:55:36 +00004932 case TARGET_NR_truncate:
bellard579a97f2007-11-11 14:26:47 +00004933 if (!(p = lock_user_string(arg1)))
4934 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004935 ret = get_errno(truncate(p, arg2));
4936 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004937 break;
4938 case TARGET_NR_ftruncate:
4939 ret = get_errno(ftruncate(arg1, arg2));
4940 break;
4941 case TARGET_NR_fchmod:
4942 ret = get_errno(fchmod(arg1, arg2));
4943 break;
ths814d7972007-09-24 09:26:51 +00004944#if defined(TARGET_NR_fchmodat) && defined(__NR_fchmodat)
4945 case TARGET_NR_fchmodat:
bellard579a97f2007-11-11 14:26:47 +00004946 if (!(p = lock_user_string(arg2)))
4947 goto efault;
aurel32465c9f02009-04-19 08:52:17 +00004948 ret = get_errno(sys_fchmodat(arg1, p, arg3));
bellard579a97f2007-11-11 14:26:47 +00004949 unlock_user(p, arg2, 0);
ths814d7972007-09-24 09:26:51 +00004950 break;
4951#endif
bellard31e31b82003-02-18 22:55:36 +00004952 case TARGET_NR_getpriority:
thsc6cda172007-10-09 03:42:34 +00004953 /* libc does special remapping of the return value of
4954 * sys_getpriority() so it's just easiest to call
4955 * sys_getpriority() directly rather than through libc. */
4956 ret = sys_getpriority(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00004957 break;
4958 case TARGET_NR_setpriority:
4959 ret = get_errno(setpriority(arg1, arg2, arg3));
4960 break;
bellardebc05482003-09-30 21:08:41 +00004961#ifdef TARGET_NR_profil
bellard31e31b82003-02-18 22:55:36 +00004962 case TARGET_NR_profil:
4963 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00004964#endif
bellard31e31b82003-02-18 22:55:36 +00004965 case TARGET_NR_statfs:
bellard579a97f2007-11-11 14:26:47 +00004966 if (!(p = lock_user_string(arg1)))
4967 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004968 ret = get_errno(statfs(path(p), &stfs));
4969 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00004970 convert_statfs:
4971 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00004972 struct target_statfs *target_stfs;
ths3b46e622007-09-17 08:09:54 +00004973
bellard579a97f2007-11-11 14:26:47 +00004974 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg2, 0))
4975 goto efault;
4976 __put_user(stfs.f_type, &target_stfs->f_type);
4977 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
4978 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
4979 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
4980 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
4981 __put_user(stfs.f_files, &target_stfs->f_files);
4982 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
4983 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
4984 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
4985 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
pbrook53a59602006-03-25 19:31:22 +00004986 unlock_user_struct(target_stfs, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00004987 }
4988 break;
4989 case TARGET_NR_fstatfs:
bellard56c8f682005-11-28 22:28:41 +00004990 ret = get_errno(fstatfs(arg1, &stfs));
bellard31e31b82003-02-18 22:55:36 +00004991 goto convert_statfs;
bellard56c8f682005-11-28 22:28:41 +00004992#ifdef TARGET_NR_statfs64
4993 case TARGET_NR_statfs64:
bellard579a97f2007-11-11 14:26:47 +00004994 if (!(p = lock_user_string(arg1)))
4995 goto efault;
pbrook53a59602006-03-25 19:31:22 +00004996 ret = get_errno(statfs(path(p), &stfs));
4997 unlock_user(p, arg1, 0);
bellard56c8f682005-11-28 22:28:41 +00004998 convert_statfs64:
4999 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005000 struct target_statfs64 *target_stfs;
ths3b46e622007-09-17 08:09:54 +00005001
bellard579a97f2007-11-11 14:26:47 +00005002 if (!lock_user_struct(VERIFY_WRITE, target_stfs, arg3, 0))
5003 goto efault;
5004 __put_user(stfs.f_type, &target_stfs->f_type);
5005 __put_user(stfs.f_bsize, &target_stfs->f_bsize);
5006 __put_user(stfs.f_blocks, &target_stfs->f_blocks);
5007 __put_user(stfs.f_bfree, &target_stfs->f_bfree);
5008 __put_user(stfs.f_bavail, &target_stfs->f_bavail);
5009 __put_user(stfs.f_files, &target_stfs->f_files);
5010 __put_user(stfs.f_ffree, &target_stfs->f_ffree);
5011 __put_user(stfs.f_fsid.__val[0], &target_stfs->f_fsid.val[0]);
5012 __put_user(stfs.f_fsid.__val[1], &target_stfs->f_fsid.val[1]);
5013 __put_user(stfs.f_namelen, &target_stfs->f_namelen);
5014 unlock_user_struct(target_stfs, arg3, 1);
bellard56c8f682005-11-28 22:28:41 +00005015 }
5016 break;
5017 case TARGET_NR_fstatfs64:
5018 ret = get_errno(fstatfs(arg1, &stfs));
5019 goto convert_statfs64;
5020#endif
bellardebc05482003-09-30 21:08:41 +00005021#ifdef TARGET_NR_ioperm
bellard31e31b82003-02-18 22:55:36 +00005022 case TARGET_NR_ioperm:
5023 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005024#endif
thse5febef2007-04-01 18:31:35 +00005025#ifdef TARGET_NR_socketcall
bellard31e31b82003-02-18 22:55:36 +00005026 case TARGET_NR_socketcall:
pbrook53a59602006-03-25 19:31:22 +00005027 ret = do_socketcall(arg1, arg2);
bellard31e31b82003-02-18 22:55:36 +00005028 break;
thse5febef2007-04-01 18:31:35 +00005029#endif
bellard3532fa72006-06-24 15:06:03 +00005030#ifdef TARGET_NR_accept
5031 case TARGET_NR_accept:
pbrook1be9e1d2006-11-19 15:26:04 +00005032 ret = do_accept(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005033 break;
5034#endif
5035#ifdef TARGET_NR_bind
5036 case TARGET_NR_bind:
5037 ret = do_bind(arg1, arg2, arg3);
5038 break;
5039#endif
5040#ifdef TARGET_NR_connect
5041 case TARGET_NR_connect:
5042 ret = do_connect(arg1, arg2, arg3);
5043 break;
5044#endif
5045#ifdef TARGET_NR_getpeername
5046 case TARGET_NR_getpeername:
pbrook1be9e1d2006-11-19 15:26:04 +00005047 ret = do_getpeername(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005048 break;
5049#endif
5050#ifdef TARGET_NR_getsockname
5051 case TARGET_NR_getsockname:
pbrook1be9e1d2006-11-19 15:26:04 +00005052 ret = do_getsockname(arg1, arg2, arg3);
bellard3532fa72006-06-24 15:06:03 +00005053 break;
5054#endif
5055#ifdef TARGET_NR_getsockopt
5056 case TARGET_NR_getsockopt:
5057 ret = do_getsockopt(arg1, arg2, arg3, arg4, arg5);
5058 break;
5059#endif
5060#ifdef TARGET_NR_listen
5061 case TARGET_NR_listen:
pbrook1be9e1d2006-11-19 15:26:04 +00005062 ret = get_errno(listen(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00005063 break;
5064#endif
5065#ifdef TARGET_NR_recv
5066 case TARGET_NR_recv:
pbrook214201b2007-03-17 01:27:24 +00005067 ret = do_recvfrom(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00005068 break;
5069#endif
5070#ifdef TARGET_NR_recvfrom
5071 case TARGET_NR_recvfrom:
pbrook214201b2007-03-17 01:27:24 +00005072 ret = do_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00005073 break;
5074#endif
5075#ifdef TARGET_NR_recvmsg
5076 case TARGET_NR_recvmsg:
5077 ret = do_sendrecvmsg(arg1, arg2, arg3, 0);
5078 break;
5079#endif
5080#ifdef TARGET_NR_send
5081 case TARGET_NR_send:
pbrook1be9e1d2006-11-19 15:26:04 +00005082 ret = do_sendto(arg1, arg2, arg3, arg4, 0, 0);
bellard3532fa72006-06-24 15:06:03 +00005083 break;
5084#endif
5085#ifdef TARGET_NR_sendmsg
5086 case TARGET_NR_sendmsg:
5087 ret = do_sendrecvmsg(arg1, arg2, arg3, 1);
5088 break;
5089#endif
5090#ifdef TARGET_NR_sendto
5091 case TARGET_NR_sendto:
pbrook1be9e1d2006-11-19 15:26:04 +00005092 ret = do_sendto(arg1, arg2, arg3, arg4, arg5, arg6);
bellard3532fa72006-06-24 15:06:03 +00005093 break;
5094#endif
5095#ifdef TARGET_NR_shutdown
5096 case TARGET_NR_shutdown:
pbrook1be9e1d2006-11-19 15:26:04 +00005097 ret = get_errno(shutdown(arg1, arg2));
bellard3532fa72006-06-24 15:06:03 +00005098 break;
5099#endif
5100#ifdef TARGET_NR_socket
5101 case TARGET_NR_socket:
5102 ret = do_socket(arg1, arg2, arg3);
5103 break;
5104#endif
5105#ifdef TARGET_NR_socketpair
5106 case TARGET_NR_socketpair:
pbrook1be9e1d2006-11-19 15:26:04 +00005107 ret = do_socketpair(arg1, arg2, arg3, arg4);
bellard3532fa72006-06-24 15:06:03 +00005108 break;
5109#endif
5110#ifdef TARGET_NR_setsockopt
5111 case TARGET_NR_setsockopt:
5112 ret = do_setsockopt(arg1, arg2, arg3, arg4, (socklen_t) arg5);
5113 break;
5114#endif
ths7494b0f2007-02-11 18:26:53 +00005115
bellard31e31b82003-02-18 22:55:36 +00005116 case TARGET_NR_syslog:
bellard579a97f2007-11-11 14:26:47 +00005117 if (!(p = lock_user_string(arg2)))
5118 goto efault;
thse5574482007-02-11 20:03:13 +00005119 ret = get_errno(sys_syslog((int)arg1, p, (int)arg3));
5120 unlock_user(p, arg2, 0);
ths7494b0f2007-02-11 18:26:53 +00005121 break;
5122
bellard31e31b82003-02-18 22:55:36 +00005123 case TARGET_NR_setitimer:
bellard66fb9762003-03-23 01:06:05 +00005124 {
bellard66fb9762003-03-23 01:06:05 +00005125 struct itimerval value, ovalue, *pvalue;
5126
pbrook53a59602006-03-25 19:31:22 +00005127 if (arg2) {
bellard66fb9762003-03-23 01:06:05 +00005128 pvalue = &value;
ths788f5ec2007-12-09 02:37:05 +00005129 if (copy_from_user_timeval(&pvalue->it_interval, arg2)
5130 || copy_from_user_timeval(&pvalue->it_value,
5131 arg2 + sizeof(struct target_timeval)))
5132 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005133 } else {
5134 pvalue = NULL;
5135 }
5136 ret = get_errno(setitimer(arg1, pvalue, &ovalue));
pbrook53a59602006-03-25 19:31:22 +00005137 if (!is_error(ret) && arg3) {
ths788f5ec2007-12-09 02:37:05 +00005138 if (copy_to_user_timeval(arg3,
5139 &ovalue.it_interval)
5140 || copy_to_user_timeval(arg3 + sizeof(struct target_timeval),
5141 &ovalue.it_value))
5142 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005143 }
5144 }
5145 break;
bellard31e31b82003-02-18 22:55:36 +00005146 case TARGET_NR_getitimer:
bellard66fb9762003-03-23 01:06:05 +00005147 {
bellard66fb9762003-03-23 01:06:05 +00005148 struct itimerval value;
ths3b46e622007-09-17 08:09:54 +00005149
bellard66fb9762003-03-23 01:06:05 +00005150 ret = get_errno(getitimer(arg1, &value));
pbrook53a59602006-03-25 19:31:22 +00005151 if (!is_error(ret) && arg2) {
ths788f5ec2007-12-09 02:37:05 +00005152 if (copy_to_user_timeval(arg2,
5153 &value.it_interval)
5154 || copy_to_user_timeval(arg2 + sizeof(struct target_timeval),
5155 &value.it_value))
5156 goto efault;
bellard66fb9762003-03-23 01:06:05 +00005157 }
5158 }
5159 break;
bellard31e31b82003-02-18 22:55:36 +00005160 case TARGET_NR_stat:
bellard579a97f2007-11-11 14:26:47 +00005161 if (!(p = lock_user_string(arg1)))
5162 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005163 ret = get_errno(stat(path(p), &st));
5164 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005165 goto do_stat;
5166 case TARGET_NR_lstat:
bellard579a97f2007-11-11 14:26:47 +00005167 if (!(p = lock_user_string(arg1)))
5168 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005169 ret = get_errno(lstat(path(p), &st));
5170 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005171 goto do_stat;
5172 case TARGET_NR_fstat:
5173 {
5174 ret = get_errno(fstat(arg1, &st));
5175 do_stat:
5176 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005177 struct target_stat *target_st;
thse3584652007-06-01 11:49:38 +00005178
bellard579a97f2007-11-11 14:26:47 +00005179 if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
5180 goto efault;
bellardd2fd1af2007-11-14 18:08:56 +00005181 __put_user(st.st_dev, &target_st->st_dev);
5182 __put_user(st.st_ino, &target_st->st_ino);
5183 __put_user(st.st_mode, &target_st->st_mode);
5184 __put_user(st.st_uid, &target_st->st_uid);
5185 __put_user(st.st_gid, &target_st->st_gid);
5186 __put_user(st.st_nlink, &target_st->st_nlink);
5187 __put_user(st.st_rdev, &target_st->st_rdev);
5188 __put_user(st.st_size, &target_st->st_size);
5189 __put_user(st.st_blksize, &target_st->st_blksize);
5190 __put_user(st.st_blocks, &target_st->st_blocks);
5191 __put_user(st.st_atime, &target_st->target_st_atime);
5192 __put_user(st.st_mtime, &target_st->target_st_mtime);
5193 __put_user(st.st_ctime, &target_st->target_st_ctime);
pbrook53a59602006-03-25 19:31:22 +00005194 unlock_user_struct(target_st, arg2, 1);
bellard31e31b82003-02-18 22:55:36 +00005195 }
5196 }
5197 break;
bellardebc05482003-09-30 21:08:41 +00005198#ifdef TARGET_NR_olduname
bellard31e31b82003-02-18 22:55:36 +00005199 case TARGET_NR_olduname:
5200 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005201#endif
5202#ifdef TARGET_NR_iopl
bellard31e31b82003-02-18 22:55:36 +00005203 case TARGET_NR_iopl:
5204 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005205#endif
bellard31e31b82003-02-18 22:55:36 +00005206 case TARGET_NR_vhangup:
5207 ret = get_errno(vhangup());
5208 break;
bellardebc05482003-09-30 21:08:41 +00005209#ifdef TARGET_NR_idle
bellard31e31b82003-02-18 22:55:36 +00005210 case TARGET_NR_idle:
5211 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005212#endif
bellard42ad6ae2005-01-03 22:48:11 +00005213#ifdef TARGET_NR_syscall
5214 case TARGET_NR_syscall:
5215 ret = do_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
5216 break;
5217#endif
bellard31e31b82003-02-18 22:55:36 +00005218 case TARGET_NR_wait4:
5219 {
5220 int status;
blueswir1992f48a2007-10-14 16:27:31 +00005221 abi_long status_ptr = arg2;
bellard31e31b82003-02-18 22:55:36 +00005222 struct rusage rusage, *rusage_ptr;
blueswir1992f48a2007-10-14 16:27:31 +00005223 abi_ulong target_rusage = arg4;
bellard31e31b82003-02-18 22:55:36 +00005224 if (target_rusage)
5225 rusage_ptr = &rusage;
5226 else
5227 rusage_ptr = NULL;
5228 ret = get_errno(wait4(arg1, &status, arg3, rusage_ptr));
5229 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005230 if (status_ptr) {
pbrook1d9d8b52009-04-16 15:17:02 +00005231 status = host_to_target_waitstatus(status);
bellard2f619692007-11-16 10:46:05 +00005232 if (put_user_s32(status, status_ptr))
5233 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005234 }
bellard2f619692007-11-16 10:46:05 +00005235 if (target_rusage)
5236 host_to_target_rusage(target_rusage, &rusage);
bellard31e31b82003-02-18 22:55:36 +00005237 }
5238 }
5239 break;
thse5febef2007-04-01 18:31:35 +00005240#ifdef TARGET_NR_swapoff
bellard31e31b82003-02-18 22:55:36 +00005241 case TARGET_NR_swapoff:
bellard579a97f2007-11-11 14:26:47 +00005242 if (!(p = lock_user_string(arg1)))
5243 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005244 ret = get_errno(swapoff(p));
5245 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005246 break;
thse5febef2007-04-01 18:31:35 +00005247#endif
bellard31e31b82003-02-18 22:55:36 +00005248 case TARGET_NR_sysinfo:
bellarda5448a72004-06-19 16:59:03 +00005249 {
pbrook53a59602006-03-25 19:31:22 +00005250 struct target_sysinfo *target_value;
bellarda5448a72004-06-19 16:59:03 +00005251 struct sysinfo value;
5252 ret = get_errno(sysinfo(&value));
pbrook53a59602006-03-25 19:31:22 +00005253 if (!is_error(ret) && arg1)
bellarda5448a72004-06-19 16:59:03 +00005254 {
bellard579a97f2007-11-11 14:26:47 +00005255 if (!lock_user_struct(VERIFY_WRITE, target_value, arg1, 0))
5256 goto efault;
bellarda5448a72004-06-19 16:59:03 +00005257 __put_user(value.uptime, &target_value->uptime);
5258 __put_user(value.loads[0], &target_value->loads[0]);
5259 __put_user(value.loads[1], &target_value->loads[1]);
5260 __put_user(value.loads[2], &target_value->loads[2]);
5261 __put_user(value.totalram, &target_value->totalram);
5262 __put_user(value.freeram, &target_value->freeram);
5263 __put_user(value.sharedram, &target_value->sharedram);
5264 __put_user(value.bufferram, &target_value->bufferram);
5265 __put_user(value.totalswap, &target_value->totalswap);
5266 __put_user(value.freeswap, &target_value->freeswap);
5267 __put_user(value.procs, &target_value->procs);
5268 __put_user(value.totalhigh, &target_value->totalhigh);
5269 __put_user(value.freehigh, &target_value->freehigh);
5270 __put_user(value.mem_unit, &target_value->mem_unit);
pbrook53a59602006-03-25 19:31:22 +00005271 unlock_user_struct(target_value, arg1, 1);
bellarda5448a72004-06-19 16:59:03 +00005272 }
5273 }
5274 break;
thse5febef2007-04-01 18:31:35 +00005275#ifdef TARGET_NR_ipc
bellard31e31b82003-02-18 22:55:36 +00005276 case TARGET_NR_ipc:
bellard8853f862004-02-22 14:57:26 +00005277 ret = do_ipc(arg1, arg2, arg3, arg4, arg5, arg6);
5278 break;
thse5febef2007-04-01 18:31:35 +00005279#endif
aurel32e5289082009-04-18 16:16:12 +00005280#ifdef TARGET_NR_semget
5281 case TARGET_NR_semget:
5282 ret = get_errno(semget(arg1, arg2, arg3));
5283 break;
5284#endif
5285#ifdef TARGET_NR_semop
5286 case TARGET_NR_semop:
5287 ret = get_errno(do_semop(arg1, arg2, arg3));
5288 break;
5289#endif
5290#ifdef TARGET_NR_semctl
5291 case TARGET_NR_semctl:
5292 ret = do_semctl(arg1, arg2, arg3, (union target_semun)(abi_ulong)arg4);
5293 break;
5294#endif
aurel32eeb438c2008-10-13 21:08:55 +00005295#ifdef TARGET_NR_msgctl
5296 case TARGET_NR_msgctl:
5297 ret = do_msgctl(arg1, arg2, arg3);
5298 break;
5299#endif
5300#ifdef TARGET_NR_msgget
5301 case TARGET_NR_msgget:
5302 ret = get_errno(msgget(arg1, arg2));
5303 break;
5304#endif
5305#ifdef TARGET_NR_msgrcv
5306 case TARGET_NR_msgrcv:
5307 ret = do_msgrcv(arg1, arg2, arg3, arg4, arg5);
5308 break;
5309#endif
5310#ifdef TARGET_NR_msgsnd
5311 case TARGET_NR_msgsnd:
5312 ret = do_msgsnd(arg1, arg2, arg3, arg4);
5313 break;
5314#endif
bellard31e31b82003-02-18 22:55:36 +00005315 case TARGET_NR_fsync:
5316 ret = get_errno(fsync(arg1));
5317 break;
bellard31e31b82003-02-18 22:55:36 +00005318 case TARGET_NR_clone:
aurel320b6d3ae2008-09-15 07:43:43 +00005319#if defined(TARGET_SH4)
5320 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg5, arg4));
edgar_iglb15ad612009-01-07 19:43:47 +00005321#elif defined(TARGET_CRIS)
5322 ret = get_errno(do_fork(cpu_env, arg2, arg1, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00005323#else
pbrookd865bab2008-06-07 22:12:17 +00005324 ret = get_errno(do_fork(cpu_env, arg1, arg2, arg3, arg4, arg5));
aurel320b6d3ae2008-09-15 07:43:43 +00005325#endif
bellard1b6b0292003-03-22 17:31:38 +00005326 break;
bellardec86b0f2003-04-11 00:15:04 +00005327#ifdef __NR_exit_group
5328 /* new thread calls */
5329 case TARGET_NR_exit_group:
aurel326d946cd2008-11-06 16:15:18 +00005330#ifdef HAVE_GPROF
5331 _mcleanup();
5332#endif
bellarde9009672005-04-26 20:42:36 +00005333 gdb_exit(cpu_env, arg1);
bellardec86b0f2003-04-11 00:15:04 +00005334 ret = get_errno(exit_group(arg1));
5335 break;
5336#endif
bellard31e31b82003-02-18 22:55:36 +00005337 case TARGET_NR_setdomainname:
bellard579a97f2007-11-11 14:26:47 +00005338 if (!(p = lock_user_string(arg1)))
5339 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005340 ret = get_errno(setdomainname(p, arg2));
5341 unlock_user(p, arg1, 0);
bellard31e31b82003-02-18 22:55:36 +00005342 break;
5343 case TARGET_NR_uname:
5344 /* no need to transcode because we use the linux syscall */
bellard29e619b2004-09-13 21:41:04 +00005345 {
5346 struct new_utsname * buf;
ths3b46e622007-09-17 08:09:54 +00005347
bellard579a97f2007-11-11 14:26:47 +00005348 if (!lock_user_struct(VERIFY_WRITE, buf, arg1, 0))
5349 goto efault;
bellard29e619b2004-09-13 21:41:04 +00005350 ret = get_errno(sys_uname(buf));
5351 if (!is_error(ret)) {
5352 /* Overrite the native machine name with whatever is being
5353 emulated. */
5354 strcpy (buf->machine, UNAME_MACHINE);
pbrookc5937222006-05-14 11:30:38 +00005355 /* Allow the user to override the reported release. */
5356 if (qemu_uname_release && *qemu_uname_release)
5357 strcpy (buf->release, qemu_uname_release);
bellard29e619b2004-09-13 21:41:04 +00005358 }
pbrook53a59602006-03-25 19:31:22 +00005359 unlock_user_struct(buf, arg1, 1);
bellard29e619b2004-09-13 21:41:04 +00005360 }
bellard31e31b82003-02-18 22:55:36 +00005361 break;
bellard6dbad632003-03-16 18:05:05 +00005362#ifdef TARGET_I386
bellard31e31b82003-02-18 22:55:36 +00005363 case TARGET_NR_modify_ldt:
bellard03acab62007-11-11 14:57:14 +00005364 ret = do_modify_ldt(cpu_env, arg1, arg2, arg3);
bellard5cd43932003-03-29 16:54:36 +00005365 break;
j_mayer84409dd2007-04-06 08:56:50 +00005366#if !defined(TARGET_X86_64)
bellard5cd43932003-03-29 16:54:36 +00005367 case TARGET_NR_vm86old:
5368 goto unimplemented;
5369 case TARGET_NR_vm86:
pbrook53a59602006-03-25 19:31:22 +00005370 ret = do_vm86(cpu_env, arg1, arg2);
bellard6dbad632003-03-16 18:05:05 +00005371 break;
5372#endif
j_mayer84409dd2007-04-06 08:56:50 +00005373#endif
bellard31e31b82003-02-18 22:55:36 +00005374 case TARGET_NR_adjtimex:
5375 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005376#ifdef TARGET_NR_create_module
bellard31e31b82003-02-18 22:55:36 +00005377 case TARGET_NR_create_module:
thse5febef2007-04-01 18:31:35 +00005378#endif
bellard31e31b82003-02-18 22:55:36 +00005379 case TARGET_NR_init_module:
5380 case TARGET_NR_delete_module:
thse5febef2007-04-01 18:31:35 +00005381#ifdef TARGET_NR_get_kernel_syms
bellard31e31b82003-02-18 22:55:36 +00005382 case TARGET_NR_get_kernel_syms:
thse5febef2007-04-01 18:31:35 +00005383#endif
bellard31e31b82003-02-18 22:55:36 +00005384 goto unimplemented;
5385 case TARGET_NR_quotactl:
5386 goto unimplemented;
5387 case TARGET_NR_getpgid:
5388 ret = get_errno(getpgid(arg1));
5389 break;
5390 case TARGET_NR_fchdir:
5391 ret = get_errno(fchdir(arg1));
5392 break;
j_mayer84409dd2007-04-06 08:56:50 +00005393#ifdef TARGET_NR_bdflush /* not on x86_64 */
bellard31e31b82003-02-18 22:55:36 +00005394 case TARGET_NR_bdflush:
5395 goto unimplemented;
j_mayer84409dd2007-04-06 08:56:50 +00005396#endif
thse5febef2007-04-01 18:31:35 +00005397#ifdef TARGET_NR_sysfs
bellard31e31b82003-02-18 22:55:36 +00005398 case TARGET_NR_sysfs:
5399 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005400#endif
bellard31e31b82003-02-18 22:55:36 +00005401 case TARGET_NR_personality:
bellard1b6b0292003-03-22 17:31:38 +00005402 ret = get_errno(personality(arg1));
bellard31e31b82003-02-18 22:55:36 +00005403 break;
thse5febef2007-04-01 18:31:35 +00005404#ifdef TARGET_NR_afs_syscall
bellard31e31b82003-02-18 22:55:36 +00005405 case TARGET_NR_afs_syscall:
5406 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005407#endif
j_mayer7a3148a2007-04-05 07:13:51 +00005408#ifdef TARGET_NR__llseek /* Not on alpha */
bellard31e31b82003-02-18 22:55:36 +00005409 case TARGET_NR__llseek:
5410 {
bellard4f2ac232004-04-26 19:44:02 +00005411#if defined (__x86_64__)
5412 ret = get_errno(lseek(arg1, ((uint64_t )arg2 << 32) | arg3, arg5));
bellard2f619692007-11-16 10:46:05 +00005413 if (put_user_s64(ret, arg4))
5414 goto efault;
bellard4f2ac232004-04-26 19:44:02 +00005415#else
bellard31e31b82003-02-18 22:55:36 +00005416 int64_t res;
5417 ret = get_errno(_llseek(arg1, arg2, arg3, &res, arg5));
bellard2f619692007-11-16 10:46:05 +00005418 if (put_user_s64(res, arg4))
5419 goto efault;
bellard4f2ac232004-04-26 19:44:02 +00005420#endif
bellard31e31b82003-02-18 22:55:36 +00005421 }
5422 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005423#endif
bellard31e31b82003-02-18 22:55:36 +00005424 case TARGET_NR_getdents:
blueswir1992f48a2007-10-14 16:27:31 +00005425#if TARGET_ABI_BITS != 32
pbrook53a59602006-03-25 19:31:22 +00005426 goto unimplemented;
blueswir1992f48a2007-10-14 16:27:31 +00005427#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
bellard4add45b2003-06-05 01:52:59 +00005428 {
pbrook53a59602006-03-25 19:31:22 +00005429 struct target_dirent *target_dirp;
aurel326556a832008-10-13 21:08:17 +00005430 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00005431 abi_long count = arg3;
bellard4add45b2003-06-05 01:52:59 +00005432
5433 dirp = malloc(count);
ths0da46a62007-10-20 20:23:07 +00005434 if (!dirp) {
bellard579a97f2007-11-11 14:26:47 +00005435 ret = -TARGET_ENOMEM;
ths0da46a62007-10-20 20:23:07 +00005436 goto fail;
5437 }
ths3b46e622007-09-17 08:09:54 +00005438
bellard4add45b2003-06-05 01:52:59 +00005439 ret = get_errno(sys_getdents(arg1, dirp, count));
5440 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00005441 struct linux_dirent *de;
bellard4add45b2003-06-05 01:52:59 +00005442 struct target_dirent *tde;
5443 int len = ret;
5444 int reclen, treclen;
5445 int count1, tnamelen;
5446
5447 count1 = 0;
5448 de = dirp;
bellard579a97f2007-11-11 14:26:47 +00005449 if (!(target_dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5450 goto efault;
bellard4add45b2003-06-05 01:52:59 +00005451 tde = target_dirp;
5452 while (len > 0) {
5453 reclen = de->d_reclen;
blueswir1992f48a2007-10-14 16:27:31 +00005454 treclen = reclen - (2 * (sizeof(long) - sizeof(abi_long)));
bellard4add45b2003-06-05 01:52:59 +00005455 tde->d_reclen = tswap16(treclen);
5456 tde->d_ino = tswapl(de->d_ino);
5457 tde->d_off = tswapl(de->d_off);
blueswir1992f48a2007-10-14 16:27:31 +00005458 tnamelen = treclen - (2 * sizeof(abi_long) + 2);
bellard4add45b2003-06-05 01:52:59 +00005459 if (tnamelen > 256)
5460 tnamelen = 256;
bellard80a9d032005-01-03 23:31:27 +00005461 /* XXX: may not be correct */
blueswir1be15b142008-10-25 11:21:28 +00005462 pstrcpy(tde->d_name, tnamelen, de->d_name);
aurel326556a832008-10-13 21:08:17 +00005463 de = (struct linux_dirent *)((char *)de + reclen);
bellard4add45b2003-06-05 01:52:59 +00005464 len -= reclen;
j_mayer1c5bf3b2007-04-14 12:17:59 +00005465 tde = (struct target_dirent *)((char *)tde + treclen);
bellard4add45b2003-06-05 01:52:59 +00005466 count1 += treclen;
5467 }
5468 ret = count1;
bellard579a97f2007-11-11 14:26:47 +00005469 unlock_user(target_dirp, arg2, ret);
bellard4add45b2003-06-05 01:52:59 +00005470 }
5471 free(dirp);
5472 }
5473#else
bellard31e31b82003-02-18 22:55:36 +00005474 {
aurel326556a832008-10-13 21:08:17 +00005475 struct linux_dirent *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00005476 abi_long count = arg3;
bellarddab2ed92003-03-22 15:23:14 +00005477
bellard579a97f2007-11-11 14:26:47 +00005478 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5479 goto efault;
bellard72f03902003-02-18 23:33:18 +00005480 ret = get_errno(sys_getdents(arg1, dirp, count));
bellard31e31b82003-02-18 22:55:36 +00005481 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00005482 struct linux_dirent *de;
bellard31e31b82003-02-18 22:55:36 +00005483 int len = ret;
5484 int reclen;
5485 de = dirp;
5486 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00005487 reclen = de->d_reclen;
bellard31e31b82003-02-18 22:55:36 +00005488 if (reclen > len)
5489 break;
bellard8083a3e2003-03-24 23:12:16 +00005490 de->d_reclen = tswap16(reclen);
bellard31e31b82003-02-18 22:55:36 +00005491 tswapls(&de->d_ino);
5492 tswapls(&de->d_off);
aurel326556a832008-10-13 21:08:17 +00005493 de = (struct linux_dirent *)((char *)de + reclen);
bellard31e31b82003-02-18 22:55:36 +00005494 len -= reclen;
5495 }
5496 }
pbrook53a59602006-03-25 19:31:22 +00005497 unlock_user(dirp, arg2, ret);
bellard31e31b82003-02-18 22:55:36 +00005498 }
bellard4add45b2003-06-05 01:52:59 +00005499#endif
bellard31e31b82003-02-18 22:55:36 +00005500 break;
ths3ae43202007-09-16 21:39:48 +00005501#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
bellarddab2ed92003-03-22 15:23:14 +00005502 case TARGET_NR_getdents64:
5503 {
aurel326556a832008-10-13 21:08:17 +00005504 struct linux_dirent64 *dirp;
blueswir1992f48a2007-10-14 16:27:31 +00005505 abi_long count = arg3;
bellard579a97f2007-11-11 14:26:47 +00005506 if (!(dirp = lock_user(VERIFY_WRITE, arg2, count, 0)))
5507 goto efault;
bellarddab2ed92003-03-22 15:23:14 +00005508 ret = get_errno(sys_getdents64(arg1, dirp, count));
5509 if (!is_error(ret)) {
aurel326556a832008-10-13 21:08:17 +00005510 struct linux_dirent64 *de;
bellarddab2ed92003-03-22 15:23:14 +00005511 int len = ret;
5512 int reclen;
5513 de = dirp;
5514 while (len > 0) {
bellard8083a3e2003-03-24 23:12:16 +00005515 reclen = de->d_reclen;
bellarddab2ed92003-03-22 15:23:14 +00005516 if (reclen > len)
5517 break;
bellard8083a3e2003-03-24 23:12:16 +00005518 de->d_reclen = tswap16(reclen);
bellard8582a532007-11-11 23:11:36 +00005519 tswap64s((uint64_t *)&de->d_ino);
5520 tswap64s((uint64_t *)&de->d_off);
aurel326556a832008-10-13 21:08:17 +00005521 de = (struct linux_dirent64 *)((char *)de + reclen);
bellarddab2ed92003-03-22 15:23:14 +00005522 len -= reclen;
5523 }
5524 }
pbrook53a59602006-03-25 19:31:22 +00005525 unlock_user(dirp, arg2, ret);
bellarddab2ed92003-03-22 15:23:14 +00005526 }
5527 break;
bellarda541f292004-04-12 20:39:29 +00005528#endif /* TARGET_NR_getdents64 */
thse5febef2007-04-01 18:31:35 +00005529#ifdef TARGET_NR__newselect
bellard31e31b82003-02-18 22:55:36 +00005530 case TARGET_NR__newselect:
pbrook53a59602006-03-25 19:31:22 +00005531 ret = do_select(arg1, arg2, arg3, arg4, arg5);
bellard31e31b82003-02-18 22:55:36 +00005532 break;
thse5febef2007-04-01 18:31:35 +00005533#endif
5534#ifdef TARGET_NR_poll
bellard9de5e442003-03-23 16:49:39 +00005535 case TARGET_NR_poll:
5536 {
pbrook53a59602006-03-25 19:31:22 +00005537 struct target_pollfd *target_pfd;
bellard9de5e442003-03-23 16:49:39 +00005538 unsigned int nfds = arg2;
5539 int timeout = arg3;
5540 struct pollfd *pfd;
bellard7854b052003-03-29 17:22:23 +00005541 unsigned int i;
bellard9de5e442003-03-23 16:49:39 +00005542
bellard579a97f2007-11-11 14:26:47 +00005543 target_pfd = lock_user(VERIFY_WRITE, arg1, sizeof(struct target_pollfd) * nfds, 1);
5544 if (!target_pfd)
5545 goto efault;
bellard9de5e442003-03-23 16:49:39 +00005546 pfd = alloca(sizeof(struct pollfd) * nfds);
5547 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00005548 pfd[i].fd = tswap32(target_pfd[i].fd);
5549 pfd[i].events = tswap16(target_pfd[i].events);
bellard9de5e442003-03-23 16:49:39 +00005550 }
5551 ret = get_errno(poll(pfd, nfds, timeout));
5552 if (!is_error(ret)) {
5553 for(i = 0; i < nfds; i++) {
bellard5cd43932003-03-29 16:54:36 +00005554 target_pfd[i].revents = tswap16(pfd[i].revents);
bellard9de5e442003-03-23 16:49:39 +00005555 }
pbrook53a59602006-03-25 19:31:22 +00005556 ret += nfds * (sizeof(struct target_pollfd)
5557 - sizeof(struct pollfd));
bellard9de5e442003-03-23 16:49:39 +00005558 }
pbrook53a59602006-03-25 19:31:22 +00005559 unlock_user(target_pfd, arg1, ret);
bellard9de5e442003-03-23 16:49:39 +00005560 }
5561 break;
thse5febef2007-04-01 18:31:35 +00005562#endif
bellard31e31b82003-02-18 22:55:36 +00005563 case TARGET_NR_flock:
bellard9de5e442003-03-23 16:49:39 +00005564 /* NOTE: the flock constant seems to be the same for every
5565 Linux platform */
5566 ret = get_errno(flock(arg1, arg2));
bellard31e31b82003-02-18 22:55:36 +00005567 break;
5568 case TARGET_NR_readv:
5569 {
5570 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00005571 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00005572
5573 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00005574 if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
5575 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005576 ret = get_errno(readv(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00005577 unlock_iovec(vec, arg2, count, 1);
bellard31e31b82003-02-18 22:55:36 +00005578 }
5579 break;
5580 case TARGET_NR_writev:
5581 {
5582 int count = arg3;
bellard31e31b82003-02-18 22:55:36 +00005583 struct iovec *vec;
bellard31e31b82003-02-18 22:55:36 +00005584
5585 vec = alloca(count * sizeof(struct iovec));
bellard41df8412008-02-04 22:26:57 +00005586 if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
5587 goto efault;
bellard31e31b82003-02-18 22:55:36 +00005588 ret = get_errno(writev(arg1, vec, count));
pbrook53a59602006-03-25 19:31:22 +00005589 unlock_iovec(vec, arg2, count, 0);
bellard31e31b82003-02-18 22:55:36 +00005590 }
5591 break;
5592 case TARGET_NR_getsid:
5593 ret = get_errno(getsid(arg1));
5594 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005595#if defined(TARGET_NR_fdatasync) /* Not on alpha (osf_datasync ?) */
bellard31e31b82003-02-18 22:55:36 +00005596 case TARGET_NR_fdatasync:
bellard5cd43932003-03-29 16:54:36 +00005597 ret = get_errno(fdatasync(arg1));
5598 break;
j_mayer7a3148a2007-04-05 07:13:51 +00005599#endif
bellard31e31b82003-02-18 22:55:36 +00005600 case TARGET_NR__sysctl:
ths0da46a62007-10-20 20:23:07 +00005601 /* We don't implement this, but ENOTDIR is always a safe
bellard29e619b2004-09-13 21:41:04 +00005602 return value. */
ths0da46a62007-10-20 20:23:07 +00005603 ret = -TARGET_ENOTDIR;
5604 break;
bellard31e31b82003-02-18 22:55:36 +00005605 case TARGET_NR_sched_setparam:
bellard5cd43932003-03-29 16:54:36 +00005606 {
pbrook53a59602006-03-25 19:31:22 +00005607 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00005608 struct sched_param schp;
pbrook53a59602006-03-25 19:31:22 +00005609
bellard579a97f2007-11-11 14:26:47 +00005610 if (!lock_user_struct(VERIFY_READ, target_schp, arg2, 1))
5611 goto efault;
bellard5cd43932003-03-29 16:54:36 +00005612 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00005613 unlock_user_struct(target_schp, arg2, 0);
bellard5cd43932003-03-29 16:54:36 +00005614 ret = get_errno(sched_setparam(arg1, &schp));
5615 }
5616 break;
bellard31e31b82003-02-18 22:55:36 +00005617 case TARGET_NR_sched_getparam:
bellard5cd43932003-03-29 16:54:36 +00005618 {
pbrook53a59602006-03-25 19:31:22 +00005619 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00005620 struct sched_param schp;
5621 ret = get_errno(sched_getparam(arg1, &schp));
5622 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005623 if (!lock_user_struct(VERIFY_WRITE, target_schp, arg2, 0))
5624 goto efault;
bellard5cd43932003-03-29 16:54:36 +00005625 target_schp->sched_priority = tswap32(schp.sched_priority);
pbrook53a59602006-03-25 19:31:22 +00005626 unlock_user_struct(target_schp, arg2, 1);
bellard5cd43932003-03-29 16:54:36 +00005627 }
5628 }
5629 break;
bellard31e31b82003-02-18 22:55:36 +00005630 case TARGET_NR_sched_setscheduler:
bellard5cd43932003-03-29 16:54:36 +00005631 {
pbrook53a59602006-03-25 19:31:22 +00005632 struct sched_param *target_schp;
bellard5cd43932003-03-29 16:54:36 +00005633 struct sched_param schp;
bellard579a97f2007-11-11 14:26:47 +00005634 if (!lock_user_struct(VERIFY_READ, target_schp, arg3, 1))
5635 goto efault;
bellard5cd43932003-03-29 16:54:36 +00005636 schp.sched_priority = tswap32(target_schp->sched_priority);
pbrook53a59602006-03-25 19:31:22 +00005637 unlock_user_struct(target_schp, arg3, 0);
bellard5cd43932003-03-29 16:54:36 +00005638 ret = get_errno(sched_setscheduler(arg1, arg2, &schp));
5639 }
5640 break;
bellard31e31b82003-02-18 22:55:36 +00005641 case TARGET_NR_sched_getscheduler:
bellard5cd43932003-03-29 16:54:36 +00005642 ret = get_errno(sched_getscheduler(arg1));
5643 break;
bellard31e31b82003-02-18 22:55:36 +00005644 case TARGET_NR_sched_yield:
5645 ret = get_errno(sched_yield());
5646 break;
5647 case TARGET_NR_sched_get_priority_max:
bellard5cd43932003-03-29 16:54:36 +00005648 ret = get_errno(sched_get_priority_max(arg1));
5649 break;
bellard31e31b82003-02-18 22:55:36 +00005650 case TARGET_NR_sched_get_priority_min:
bellard5cd43932003-03-29 16:54:36 +00005651 ret = get_errno(sched_get_priority_min(arg1));
5652 break;
bellard31e31b82003-02-18 22:55:36 +00005653 case TARGET_NR_sched_rr_get_interval:
bellard5cd43932003-03-29 16:54:36 +00005654 {
bellard5cd43932003-03-29 16:54:36 +00005655 struct timespec ts;
5656 ret = get_errno(sched_rr_get_interval(arg1, &ts));
5657 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005658 host_to_target_timespec(arg2, &ts);
bellard5cd43932003-03-29 16:54:36 +00005659 }
5660 }
5661 break;
bellard31e31b82003-02-18 22:55:36 +00005662 case TARGET_NR_nanosleep:
bellard1b6b0292003-03-22 17:31:38 +00005663 {
bellard1b6b0292003-03-22 17:31:38 +00005664 struct timespec req, rem;
pbrook53a59602006-03-25 19:31:22 +00005665 target_to_host_timespec(&req, arg1);
bellard1b6b0292003-03-22 17:31:38 +00005666 ret = get_errno(nanosleep(&req, &rem));
pbrook53a59602006-03-25 19:31:22 +00005667 if (is_error(ret) && arg2) {
5668 host_to_target_timespec(arg2, &rem);
bellard1b6b0292003-03-22 17:31:38 +00005669 }
5670 }
5671 break;
thse5febef2007-04-01 18:31:35 +00005672#ifdef TARGET_NR_query_module
bellard31e31b82003-02-18 22:55:36 +00005673 case TARGET_NR_query_module:
bellard5cd43932003-03-29 16:54:36 +00005674 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005675#endif
5676#ifdef TARGET_NR_nfsservctl
bellard31e31b82003-02-18 22:55:36 +00005677 case TARGET_NR_nfsservctl:
bellard5cd43932003-03-29 16:54:36 +00005678 goto unimplemented;
thse5febef2007-04-01 18:31:35 +00005679#endif
bellard31e31b82003-02-18 22:55:36 +00005680 case TARGET_NR_prctl:
thse5574482007-02-11 20:03:13 +00005681 switch (arg1)
5682 {
5683 case PR_GET_PDEATHSIG:
5684 {
5685 int deathsig;
5686 ret = get_errno(prctl(arg1, &deathsig, arg3, arg4, arg5));
bellard2f619692007-11-16 10:46:05 +00005687 if (!is_error(ret) && arg2
5688 && put_user_ual(deathsig, arg2))
5689 goto efault;
thse5574482007-02-11 20:03:13 +00005690 }
5691 break;
5692 default:
5693 ret = get_errno(prctl(arg1, arg2, arg3, arg4, arg5));
5694 break;
5695 }
ths39b9aae2007-02-11 18:36:44 +00005696 break;
bellardd2fd1af2007-11-14 18:08:56 +00005697#ifdef TARGET_NR_arch_prctl
5698 case TARGET_NR_arch_prctl:
5699#if defined(TARGET_I386) && !defined(TARGET_ABI32)
5700 ret = do_arch_prctl(cpu_env, arg1, arg2);
5701 break;
5702#else
5703 goto unimplemented;
5704#endif
5705#endif
bellard67867302003-11-23 17:05:30 +00005706#ifdef TARGET_NR_pread
bellard31e31b82003-02-18 22:55:36 +00005707 case TARGET_NR_pread:
balroga4ae00b2008-09-20 03:14:14 +00005708#ifdef TARGET_ARM
5709 if (((CPUARMState *)cpu_env)->eabi)
5710 arg4 = arg5;
5711#endif
bellard579a97f2007-11-11 14:26:47 +00005712 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5713 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005714 ret = get_errno(pread(arg1, p, arg3, arg4));
5715 unlock_user(p, arg2, ret);
bellard206f0fa2003-05-14 19:01:56 +00005716 break;
bellard31e31b82003-02-18 22:55:36 +00005717 case TARGET_NR_pwrite:
balroga4ae00b2008-09-20 03:14:14 +00005718#ifdef TARGET_ARM
5719 if (((CPUARMState *)cpu_env)->eabi)
5720 arg4 = arg5;
5721#endif
bellard579a97f2007-11-11 14:26:47 +00005722 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5723 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005724 ret = get_errno(pwrite(arg1, p, arg3, arg4));
5725 unlock_user(p, arg2, 0);
bellard206f0fa2003-05-14 19:01:56 +00005726 break;
bellard67867302003-11-23 17:05:30 +00005727#endif
aurel32f2c7ba12008-03-28 22:32:06 +00005728#ifdef TARGET_NR_pread64
5729 case TARGET_NR_pread64:
5730 if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
5731 goto efault;
5732 ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
5733 unlock_user(p, arg2, ret);
5734 break;
5735 case TARGET_NR_pwrite64:
5736 if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
5737 goto efault;
5738 ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
5739 unlock_user(p, arg2, 0);
5740 break;
5741#endif
bellard31e31b82003-02-18 22:55:36 +00005742 case TARGET_NR_getcwd:
bellard579a97f2007-11-11 14:26:47 +00005743 if (!(p = lock_user(VERIFY_WRITE, arg1, arg2, 0)))
5744 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005745 ret = get_errno(sys_getcwd1(p, arg2));
5746 unlock_user(p, arg1, ret);
bellard31e31b82003-02-18 22:55:36 +00005747 break;
5748 case TARGET_NR_capget:
bellard5cd43932003-03-29 16:54:36 +00005749 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00005750 case TARGET_NR_capset:
bellard5cd43932003-03-29 16:54:36 +00005751 goto unimplemented;
bellard31e31b82003-02-18 22:55:36 +00005752 case TARGET_NR_sigaltstack:
ths198a74d2007-09-27 16:44:32 +00005753#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
5754 defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
bellard579a97f2007-11-11 14:26:47 +00005755 ret = do_sigaltstack(arg1, arg2, get_sp_from_cpustate((CPUState *)cpu_env));
thsa04e1342007-09-27 13:57:58 +00005756 break;
5757#else
bellard5cd43932003-03-29 16:54:36 +00005758 goto unimplemented;
thsa04e1342007-09-27 13:57:58 +00005759#endif
bellard31e31b82003-02-18 22:55:36 +00005760 case TARGET_NR_sendfile:
bellard5cd43932003-03-29 16:54:36 +00005761 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005762#ifdef TARGET_NR_getpmsg
bellard31e31b82003-02-18 22:55:36 +00005763 case TARGET_NR_getpmsg:
bellard5cd43932003-03-29 16:54:36 +00005764 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005765#endif
5766#ifdef TARGET_NR_putpmsg
bellard31e31b82003-02-18 22:55:36 +00005767 case TARGET_NR_putpmsg:
bellard5cd43932003-03-29 16:54:36 +00005768 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00005769#endif
bellard048f6b42005-11-26 18:47:20 +00005770#ifdef TARGET_NR_vfork
bellard31e31b82003-02-18 22:55:36 +00005771 case TARGET_NR_vfork:
pbrookd865bab2008-06-07 22:12:17 +00005772 ret = get_errno(do_fork(cpu_env, CLONE_VFORK | CLONE_VM | SIGCHLD,
5773 0, 0, 0, 0));
bellard31e31b82003-02-18 22:55:36 +00005774 break;
bellard048f6b42005-11-26 18:47:20 +00005775#endif
bellardebc05482003-09-30 21:08:41 +00005776#ifdef TARGET_NR_ugetrlimit
bellard31e31b82003-02-18 22:55:36 +00005777 case TARGET_NR_ugetrlimit:
bellard728584b2003-04-29 20:43:36 +00005778 {
5779 struct rlimit rlim;
5780 ret = get_errno(getrlimit(arg1, &rlim));
5781 if (!is_error(ret)) {
pbrook53a59602006-03-25 19:31:22 +00005782 struct target_rlimit *target_rlim;
bellard579a97f2007-11-11 14:26:47 +00005783 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0))
5784 goto efault;
bellard728584b2003-04-29 20:43:36 +00005785 target_rlim->rlim_cur = tswapl(rlim.rlim_cur);
5786 target_rlim->rlim_max = tswapl(rlim.rlim_max);
pbrook53a59602006-03-25 19:31:22 +00005787 unlock_user_struct(target_rlim, arg2, 1);
bellard728584b2003-04-29 20:43:36 +00005788 }
5789 break;
5790 }
bellardebc05482003-09-30 21:08:41 +00005791#endif
bellarda315a142005-01-30 22:59:18 +00005792#ifdef TARGET_NR_truncate64
bellard31e31b82003-02-18 22:55:36 +00005793 case TARGET_NR_truncate64:
bellard579a97f2007-11-11 14:26:47 +00005794 if (!(p = lock_user_string(arg1)))
5795 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005796 ret = target_truncate64(cpu_env, p, arg2, arg3, arg4);
5797 unlock_user(p, arg1, 0);
bellard667f38b2005-07-23 14:46:27 +00005798 break;
bellarda315a142005-01-30 22:59:18 +00005799#endif
5800#ifdef TARGET_NR_ftruncate64
bellard31e31b82003-02-18 22:55:36 +00005801 case TARGET_NR_ftruncate64:
pbrookce4defa2006-02-09 16:49:55 +00005802 ret = target_ftruncate64(cpu_env, arg1, arg2, arg3, arg4);
bellard667f38b2005-07-23 14:46:27 +00005803 break;
bellarda315a142005-01-30 22:59:18 +00005804#endif
5805#ifdef TARGET_NR_stat64
bellard31e31b82003-02-18 22:55:36 +00005806 case TARGET_NR_stat64:
bellard579a97f2007-11-11 14:26:47 +00005807 if (!(p = lock_user_string(arg1)))
5808 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005809 ret = get_errno(stat(path(p), &st));
5810 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00005811 if (!is_error(ret))
5812 ret = host_to_target_stat64(cpu_env, arg2, &st);
5813 break;
bellarda315a142005-01-30 22:59:18 +00005814#endif
5815#ifdef TARGET_NR_lstat64
bellard31e31b82003-02-18 22:55:36 +00005816 case TARGET_NR_lstat64:
bellard579a97f2007-11-11 14:26:47 +00005817 if (!(p = lock_user_string(arg1)))
5818 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005819 ret = get_errno(lstat(path(p), &st));
5820 unlock_user(p, arg1, 0);
balrog6a24a772008-09-20 02:23:36 +00005821 if (!is_error(ret))
5822 ret = host_to_target_stat64(cpu_env, arg2, &st);
5823 break;
bellarda315a142005-01-30 22:59:18 +00005824#endif
5825#ifdef TARGET_NR_fstat64
bellard31e31b82003-02-18 22:55:36 +00005826 case TARGET_NR_fstat64:
balrog6a24a772008-09-20 02:23:36 +00005827 ret = get_errno(fstat(arg1, &st));
5828 if (!is_error(ret))
5829 ret = host_to_target_stat64(cpu_env, arg2, &st);
5830 break;
bellardec86b0f2003-04-11 00:15:04 +00005831#endif
aurel329d33b762009-04-08 23:07:05 +00005832#if (defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)) && \
5833 (defined(__NR_fstatat64) || defined(__NR_newfstatat))
5834#ifdef TARGET_NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00005835 case TARGET_NR_fstatat64:
aurel329d33b762009-04-08 23:07:05 +00005836#endif
5837#ifdef TARGET_NR_newfstatat
5838 case TARGET_NR_newfstatat:
5839#endif
balrog6a24a772008-09-20 02:23:36 +00005840 if (!(p = lock_user_string(arg2)))
5841 goto efault;
aurel329d33b762009-04-08 23:07:05 +00005842#ifdef __NR_fstatat64
balrog6a24a772008-09-20 02:23:36 +00005843 ret = get_errno(sys_fstatat64(arg1, path(p), &st, arg4));
aurel329d33b762009-04-08 23:07:05 +00005844#else
5845 ret = get_errno(sys_newfstatat(arg1, path(p), &st, arg4));
5846#endif
balrog6a24a772008-09-20 02:23:36 +00005847 if (!is_error(ret))
5848 ret = host_to_target_stat64(cpu_env, arg3, &st);
bellard60cd49d2003-03-16 22:53:56 +00005849 break;
bellarda315a142005-01-30 22:59:18 +00005850#endif
bellard67867302003-11-23 17:05:30 +00005851#ifdef USE_UID16
5852 case TARGET_NR_lchown:
bellard579a97f2007-11-11 14:26:47 +00005853 if (!(p = lock_user_string(arg1)))
5854 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005855 ret = get_errno(lchown(p, low2highuid(arg2), low2highgid(arg3)));
5856 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00005857 break;
5858 case TARGET_NR_getuid:
5859 ret = get_errno(high2lowuid(getuid()));
5860 break;
5861 case TARGET_NR_getgid:
5862 ret = get_errno(high2lowgid(getgid()));
5863 break;
5864 case TARGET_NR_geteuid:
5865 ret = get_errno(high2lowuid(geteuid()));
5866 break;
5867 case TARGET_NR_getegid:
5868 ret = get_errno(high2lowgid(getegid()));
5869 break;
5870 case TARGET_NR_setreuid:
5871 ret = get_errno(setreuid(low2highuid(arg1), low2highuid(arg2)));
5872 break;
5873 case TARGET_NR_setregid:
5874 ret = get_errno(setregid(low2highgid(arg1), low2highgid(arg2)));
5875 break;
5876 case TARGET_NR_getgroups:
5877 {
5878 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00005879 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00005880 gid_t *grouplist;
5881 int i;
5882
5883 grouplist = alloca(gidsetsize * sizeof(gid_t));
5884 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00005885 if (gidsetsize == 0)
5886 break;
bellard67867302003-11-23 17:05:30 +00005887 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00005888 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
5889 if (!target_grouplist)
5890 goto efault;
balroga2155fc2008-09-20 02:12:08 +00005891 for(i = 0;i < ret; i++)
bellard67867302003-11-23 17:05:30 +00005892 target_grouplist[i] = tswap16(grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00005893 unlock_user(target_grouplist, arg2, gidsetsize * 2);
bellard67867302003-11-23 17:05:30 +00005894 }
5895 }
5896 break;
5897 case TARGET_NR_setgroups:
5898 {
5899 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00005900 uint16_t *target_grouplist;
bellard67867302003-11-23 17:05:30 +00005901 gid_t *grouplist;
5902 int i;
5903
5904 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00005905 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
5906 if (!target_grouplist) {
5907 ret = -TARGET_EFAULT;
5908 goto fail;
5909 }
bellard67867302003-11-23 17:05:30 +00005910 for(i = 0;i < gidsetsize; i++)
5911 grouplist[i] = tswap16(target_grouplist[i]);
pbrook53a59602006-03-25 19:31:22 +00005912 unlock_user(target_grouplist, arg2, 0);
bellard67867302003-11-23 17:05:30 +00005913 ret = get_errno(setgroups(gidsetsize, grouplist));
5914 }
5915 break;
5916 case TARGET_NR_fchown:
5917 ret = get_errno(fchown(arg1, low2highuid(arg2), low2highgid(arg3)));
5918 break;
thsccfa72b2007-09-24 09:23:34 +00005919#if defined(TARGET_NR_fchownat) && defined(__NR_fchownat)
5920 case TARGET_NR_fchownat:
bellard579a97f2007-11-11 14:26:47 +00005921 if (!(p = lock_user_string(arg2)))
5922 goto efault;
5923 ret = get_errno(sys_fchownat(arg1, p, low2highuid(arg3), low2highgid(arg4), arg5));
5924 unlock_user(p, arg2, 0);
thsccfa72b2007-09-24 09:23:34 +00005925 break;
5926#endif
bellard67867302003-11-23 17:05:30 +00005927#ifdef TARGET_NR_setresuid
5928 case TARGET_NR_setresuid:
ths5fafdf22007-09-16 21:08:06 +00005929 ret = get_errno(setresuid(low2highuid(arg1),
5930 low2highuid(arg2),
bellard67867302003-11-23 17:05:30 +00005931 low2highuid(arg3)));
5932 break;
5933#endif
5934#ifdef TARGET_NR_getresuid
5935 case TARGET_NR_getresuid:
5936 {
pbrook53a59602006-03-25 19:31:22 +00005937 uid_t ruid, euid, suid;
bellard67867302003-11-23 17:05:30 +00005938 ret = get_errno(getresuid(&ruid, &euid, &suid));
5939 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005940 if (put_user_u16(high2lowuid(ruid), arg1)
5941 || put_user_u16(high2lowuid(euid), arg2)
5942 || put_user_u16(high2lowuid(suid), arg3))
5943 goto efault;
bellard67867302003-11-23 17:05:30 +00005944 }
5945 }
5946 break;
5947#endif
5948#ifdef TARGET_NR_getresgid
5949 case TARGET_NR_setresgid:
ths5fafdf22007-09-16 21:08:06 +00005950 ret = get_errno(setresgid(low2highgid(arg1),
5951 low2highgid(arg2),
bellard67867302003-11-23 17:05:30 +00005952 low2highgid(arg3)));
5953 break;
5954#endif
5955#ifdef TARGET_NR_getresgid
5956 case TARGET_NR_getresgid:
5957 {
pbrook53a59602006-03-25 19:31:22 +00005958 gid_t rgid, egid, sgid;
bellard67867302003-11-23 17:05:30 +00005959 ret = get_errno(getresgid(&rgid, &egid, &sgid));
5960 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00005961 if (put_user_u16(high2lowgid(rgid), arg1)
5962 || put_user_u16(high2lowgid(egid), arg2)
5963 || put_user_u16(high2lowgid(sgid), arg3))
5964 goto efault;
bellard67867302003-11-23 17:05:30 +00005965 }
5966 }
5967 break;
5968#endif
5969 case TARGET_NR_chown:
bellard579a97f2007-11-11 14:26:47 +00005970 if (!(p = lock_user_string(arg1)))
5971 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005972 ret = get_errno(chown(p, low2highuid(arg2), low2highgid(arg3)));
5973 unlock_user(p, arg1, 0);
bellard67867302003-11-23 17:05:30 +00005974 break;
5975 case TARGET_NR_setuid:
5976 ret = get_errno(setuid(low2highuid(arg1)));
5977 break;
5978 case TARGET_NR_setgid:
5979 ret = get_errno(setgid(low2highgid(arg1)));
5980 break;
5981 case TARGET_NR_setfsuid:
5982 ret = get_errno(setfsuid(arg1));
5983 break;
5984 case TARGET_NR_setfsgid:
5985 ret = get_errno(setfsgid(arg1));
5986 break;
5987#endif /* USE_UID16 */
5988
bellarda315a142005-01-30 22:59:18 +00005989#ifdef TARGET_NR_lchown32
bellard31e31b82003-02-18 22:55:36 +00005990 case TARGET_NR_lchown32:
bellard579a97f2007-11-11 14:26:47 +00005991 if (!(p = lock_user_string(arg1)))
5992 goto efault;
pbrook53a59602006-03-25 19:31:22 +00005993 ret = get_errno(lchown(p, arg2, arg3));
5994 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00005995 break;
bellarda315a142005-01-30 22:59:18 +00005996#endif
5997#ifdef TARGET_NR_getuid32
bellard31e31b82003-02-18 22:55:36 +00005998 case TARGET_NR_getuid32:
bellardb03c60f2003-03-23 17:19:56 +00005999 ret = get_errno(getuid());
6000 break;
bellarda315a142005-01-30 22:59:18 +00006001#endif
aurel3264b4d282008-11-14 17:20:15 +00006002
6003#if defined(TARGET_NR_getxuid) && defined(TARGET_ALPHA)
6004 /* Alpha specific */
6005 case TARGET_NR_getxuid:
6006 {
6007 uid_t euid;
6008 euid=geteuid();
6009 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=euid;
6010 }
6011 ret = get_errno(getuid());
6012 break;
6013#endif
6014#if defined(TARGET_NR_getxgid) && defined(TARGET_ALPHA)
6015 /* Alpha specific */
6016 case TARGET_NR_getxgid:
6017 {
6018 uid_t egid;
6019 egid=getegid();
6020 ((CPUAlphaState *)cpu_env)->ir[IR_A4]=egid;
6021 }
6022 ret = get_errno(getgid());
6023 break;
6024#endif
6025
bellarda315a142005-01-30 22:59:18 +00006026#ifdef TARGET_NR_getgid32
bellard31e31b82003-02-18 22:55:36 +00006027 case TARGET_NR_getgid32:
bellardb03c60f2003-03-23 17:19:56 +00006028 ret = get_errno(getgid());
6029 break;
bellarda315a142005-01-30 22:59:18 +00006030#endif
6031#ifdef TARGET_NR_geteuid32
bellard31e31b82003-02-18 22:55:36 +00006032 case TARGET_NR_geteuid32:
bellardb03c60f2003-03-23 17:19:56 +00006033 ret = get_errno(geteuid());
6034 break;
bellarda315a142005-01-30 22:59:18 +00006035#endif
6036#ifdef TARGET_NR_getegid32
bellard31e31b82003-02-18 22:55:36 +00006037 case TARGET_NR_getegid32:
bellardb03c60f2003-03-23 17:19:56 +00006038 ret = get_errno(getegid());
6039 break;
bellarda315a142005-01-30 22:59:18 +00006040#endif
6041#ifdef TARGET_NR_setreuid32
bellard31e31b82003-02-18 22:55:36 +00006042 case TARGET_NR_setreuid32:
bellardb03c60f2003-03-23 17:19:56 +00006043 ret = get_errno(setreuid(arg1, arg2));
6044 break;
bellarda315a142005-01-30 22:59:18 +00006045#endif
6046#ifdef TARGET_NR_setregid32
bellard31e31b82003-02-18 22:55:36 +00006047 case TARGET_NR_setregid32:
bellardb03c60f2003-03-23 17:19:56 +00006048 ret = get_errno(setregid(arg1, arg2));
6049 break;
bellarda315a142005-01-30 22:59:18 +00006050#endif
6051#ifdef TARGET_NR_getgroups32
bellard31e31b82003-02-18 22:55:36 +00006052 case TARGET_NR_getgroups32:
bellard99c475a2005-01-31 20:45:13 +00006053 {
6054 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006055 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00006056 gid_t *grouplist;
6057 int i;
6058
6059 grouplist = alloca(gidsetsize * sizeof(gid_t));
6060 ret = get_errno(getgroups(gidsetsize, grouplist));
balrogcb3bc232008-09-20 02:08:13 +00006061 if (gidsetsize == 0)
6062 break;
bellard99c475a2005-01-31 20:45:13 +00006063 if (!is_error(ret)) {
bellard579a97f2007-11-11 14:26:47 +00006064 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 4, 0);
6065 if (!target_grouplist) {
6066 ret = -TARGET_EFAULT;
6067 goto fail;
6068 }
balroga2155fc2008-09-20 02:12:08 +00006069 for(i = 0;i < ret; i++)
pbrook53a59602006-03-25 19:31:22 +00006070 target_grouplist[i] = tswap32(grouplist[i]);
6071 unlock_user(target_grouplist, arg2, gidsetsize * 4);
bellard99c475a2005-01-31 20:45:13 +00006072 }
6073 }
6074 break;
bellarda315a142005-01-30 22:59:18 +00006075#endif
6076#ifdef TARGET_NR_setgroups32
bellard31e31b82003-02-18 22:55:36 +00006077 case TARGET_NR_setgroups32:
bellard99c475a2005-01-31 20:45:13 +00006078 {
6079 int gidsetsize = arg1;
pbrook53a59602006-03-25 19:31:22 +00006080 uint32_t *target_grouplist;
bellard99c475a2005-01-31 20:45:13 +00006081 gid_t *grouplist;
6082 int i;
ths3b46e622007-09-17 08:09:54 +00006083
bellard99c475a2005-01-31 20:45:13 +00006084 grouplist = alloca(gidsetsize * sizeof(gid_t));
bellard579a97f2007-11-11 14:26:47 +00006085 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 4, 1);
6086 if (!target_grouplist) {
6087 ret = -TARGET_EFAULT;
6088 goto fail;
6089 }
bellard99c475a2005-01-31 20:45:13 +00006090 for(i = 0;i < gidsetsize; i++)
pbrook53a59602006-03-25 19:31:22 +00006091 grouplist[i] = tswap32(target_grouplist[i]);
6092 unlock_user(target_grouplist, arg2, 0);
bellard99c475a2005-01-31 20:45:13 +00006093 ret = get_errno(setgroups(gidsetsize, grouplist));
6094 }
6095 break;
bellarda315a142005-01-30 22:59:18 +00006096#endif
6097#ifdef TARGET_NR_fchown32
bellard31e31b82003-02-18 22:55:36 +00006098 case TARGET_NR_fchown32:
bellardb03c60f2003-03-23 17:19:56 +00006099 ret = get_errno(fchown(arg1, arg2, arg3));
6100 break;
bellarda315a142005-01-30 22:59:18 +00006101#endif
6102#ifdef TARGET_NR_setresuid32
bellard31e31b82003-02-18 22:55:36 +00006103 case TARGET_NR_setresuid32:
bellardb03c60f2003-03-23 17:19:56 +00006104 ret = get_errno(setresuid(arg1, arg2, arg3));
6105 break;
bellarda315a142005-01-30 22:59:18 +00006106#endif
6107#ifdef TARGET_NR_getresuid32
bellard31e31b82003-02-18 22:55:36 +00006108 case TARGET_NR_getresuid32:
bellardb03c60f2003-03-23 17:19:56 +00006109 {
pbrook53a59602006-03-25 19:31:22 +00006110 uid_t ruid, euid, suid;
bellardb03c60f2003-03-23 17:19:56 +00006111 ret = get_errno(getresuid(&ruid, &euid, &suid));
6112 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006113 if (put_user_u32(ruid, arg1)
6114 || put_user_u32(euid, arg2)
6115 || put_user_u32(suid, arg3))
6116 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00006117 }
6118 }
6119 break;
bellarda315a142005-01-30 22:59:18 +00006120#endif
6121#ifdef TARGET_NR_setresgid32
bellard31e31b82003-02-18 22:55:36 +00006122 case TARGET_NR_setresgid32:
bellardb03c60f2003-03-23 17:19:56 +00006123 ret = get_errno(setresgid(arg1, arg2, arg3));
6124 break;
bellarda315a142005-01-30 22:59:18 +00006125#endif
6126#ifdef TARGET_NR_getresgid32
bellard31e31b82003-02-18 22:55:36 +00006127 case TARGET_NR_getresgid32:
bellardb03c60f2003-03-23 17:19:56 +00006128 {
pbrook53a59602006-03-25 19:31:22 +00006129 gid_t rgid, egid, sgid;
bellardb03c60f2003-03-23 17:19:56 +00006130 ret = get_errno(getresgid(&rgid, &egid, &sgid));
6131 if (!is_error(ret)) {
bellard2f619692007-11-16 10:46:05 +00006132 if (put_user_u32(rgid, arg1)
6133 || put_user_u32(egid, arg2)
6134 || put_user_u32(sgid, arg3))
6135 goto efault;
bellardb03c60f2003-03-23 17:19:56 +00006136 }
6137 }
6138 break;
bellarda315a142005-01-30 22:59:18 +00006139#endif
6140#ifdef TARGET_NR_chown32
bellard31e31b82003-02-18 22:55:36 +00006141 case TARGET_NR_chown32:
bellard579a97f2007-11-11 14:26:47 +00006142 if (!(p = lock_user_string(arg1)))
6143 goto efault;
pbrook53a59602006-03-25 19:31:22 +00006144 ret = get_errno(chown(p, arg2, arg3));
6145 unlock_user(p, arg1, 0);
bellardb03c60f2003-03-23 17:19:56 +00006146 break;
bellarda315a142005-01-30 22:59:18 +00006147#endif
6148#ifdef TARGET_NR_setuid32
bellard31e31b82003-02-18 22:55:36 +00006149 case TARGET_NR_setuid32:
bellardb03c60f2003-03-23 17:19:56 +00006150 ret = get_errno(setuid(arg1));
6151 break;
bellarda315a142005-01-30 22:59:18 +00006152#endif
6153#ifdef TARGET_NR_setgid32
bellard31e31b82003-02-18 22:55:36 +00006154 case TARGET_NR_setgid32:
bellardb03c60f2003-03-23 17:19:56 +00006155 ret = get_errno(setgid(arg1));
6156 break;
bellarda315a142005-01-30 22:59:18 +00006157#endif
6158#ifdef TARGET_NR_setfsuid32
bellard31e31b82003-02-18 22:55:36 +00006159 case TARGET_NR_setfsuid32:
bellardb03c60f2003-03-23 17:19:56 +00006160 ret = get_errno(setfsuid(arg1));
6161 break;
bellarda315a142005-01-30 22:59:18 +00006162#endif
6163#ifdef TARGET_NR_setfsgid32
bellard31e31b82003-02-18 22:55:36 +00006164 case TARGET_NR_setfsgid32:
bellardb03c60f2003-03-23 17:19:56 +00006165 ret = get_errno(setfsgid(arg1));
6166 break;
bellarda315a142005-01-30 22:59:18 +00006167#endif
bellard67867302003-11-23 17:05:30 +00006168
bellard31e31b82003-02-18 22:55:36 +00006169 case TARGET_NR_pivot_root:
bellardb03c60f2003-03-23 17:19:56 +00006170 goto unimplemented;
bellardffa65c32004-01-04 23:57:22 +00006171#ifdef TARGET_NR_mincore
bellard31e31b82003-02-18 22:55:36 +00006172 case TARGET_NR_mincore:
aurel3204bb9ac2008-10-01 21:46:41 +00006173 {
6174 void *a;
6175 ret = -TARGET_EFAULT;
6176 if (!(a = lock_user(VERIFY_READ, arg1,arg2, 0)))
6177 goto efault;
6178 if (!(p = lock_user_string(arg3)))
6179 goto mincore_fail;
6180 ret = get_errno(mincore(a, arg2, p));
6181 unlock_user(p, arg3, ret);
6182 mincore_fail:
6183 unlock_user(a, arg1, 0);
6184 }
6185 break;
bellardffa65c32004-01-04 23:57:22 +00006186#endif
aurel32408321b2008-10-01 21:46:32 +00006187#ifdef TARGET_NR_arm_fadvise64_64
6188 case TARGET_NR_arm_fadvise64_64:
6189 {
6190 /*
6191 * arm_fadvise64_64 looks like fadvise64_64 but
6192 * with different argument order
6193 */
6194 abi_long temp;
6195 temp = arg3;
6196 arg3 = arg4;
6197 arg4 = temp;
6198 }
6199#endif
6200#if defined(TARGET_NR_fadvise64_64) || defined(TARGET_NR_arm_fadvise64_64)
6201#ifdef TARGET_NR_fadvise64_64
6202 case TARGET_NR_fadvise64_64:
6203#endif
6204 /* This is a hint, so ignoring and returning success is ok. */
6205 ret = get_errno(0);
6206 break;
6207#endif
bellardffa65c32004-01-04 23:57:22 +00006208#ifdef TARGET_NR_madvise
bellard31e31b82003-02-18 22:55:36 +00006209 case TARGET_NR_madvise:
pbrook24836682006-04-16 14:14:53 +00006210 /* A straight passthrough may not be safe because qemu sometimes
6211 turns private flie-backed mappings into anonymous mappings.
6212 This will break MADV_DONTNEED.
6213 This is a hint, so ignoring and returning success is ok. */
6214 ret = get_errno(0);
6215 break;
bellardffa65c32004-01-04 23:57:22 +00006216#endif
blueswir1992f48a2007-10-14 16:27:31 +00006217#if TARGET_ABI_BITS == 32
bellard31e31b82003-02-18 22:55:36 +00006218 case TARGET_NR_fcntl64:
bellard77e46722003-04-29 20:39:06 +00006219 {
thsb1e341e2007-03-20 21:50:52 +00006220 int cmd;
bellard77e46722003-04-29 20:39:06 +00006221 struct flock64 fl;
pbrook53a59602006-03-25 19:31:22 +00006222 struct target_flock64 *target_fl;
pbrookce4defa2006-02-09 16:49:55 +00006223#ifdef TARGET_ARM
pbrook53a59602006-03-25 19:31:22 +00006224 struct target_eabi_flock64 *target_efl;
pbrookce4defa2006-02-09 16:49:55 +00006225#endif
bellard77e46722003-04-29 20:39:06 +00006226
thsb1e341e2007-03-20 21:50:52 +00006227 switch(arg2){
6228 case TARGET_F_GETLK64:
6229 cmd = F_GETLK64;
thsa7222582007-03-22 15:08:55 +00006230 break;
thsb1e341e2007-03-20 21:50:52 +00006231 case TARGET_F_SETLK64:
6232 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00006233 break;
thsb1e341e2007-03-20 21:50:52 +00006234 case TARGET_F_SETLKW64:
6235 cmd = F_SETLK64;
thsa7222582007-03-22 15:08:55 +00006236 break;
thsb1e341e2007-03-20 21:50:52 +00006237 default:
6238 cmd = arg2;
thsa7222582007-03-22 15:08:55 +00006239 break;
thsb1e341e2007-03-20 21:50:52 +00006240 }
6241
bellard60cd49d2003-03-16 22:53:56 +00006242 switch(arg2) {
thsb1e341e2007-03-20 21:50:52 +00006243 case TARGET_F_GETLK64:
ths58134272007-03-31 18:59:32 +00006244#ifdef TARGET_ARM
6245 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00006246 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
6247 goto efault;
ths58134272007-03-31 18:59:32 +00006248 fl.l_type = tswap16(target_efl->l_type);
6249 fl.l_whence = tswap16(target_efl->l_whence);
6250 fl.l_start = tswap64(target_efl->l_start);
6251 fl.l_len = tswap64(target_efl->l_len);
6252 fl.l_pid = tswapl(target_efl->l_pid);
6253 unlock_user_struct(target_efl, arg3, 0);
6254 } else
6255#endif
6256 {
bellard9ee1fa22007-11-11 15:11:19 +00006257 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
6258 goto efault;
ths58134272007-03-31 18:59:32 +00006259 fl.l_type = tswap16(target_fl->l_type);
6260 fl.l_whence = tswap16(target_fl->l_whence);
6261 fl.l_start = tswap64(target_fl->l_start);
6262 fl.l_len = tswap64(target_fl->l_len);
6263 fl.l_pid = tswapl(target_fl->l_pid);
6264 unlock_user_struct(target_fl, arg3, 0);
6265 }
thsb1e341e2007-03-20 21:50:52 +00006266 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00006267 if (ret == 0) {
pbrookce4defa2006-02-09 16:49:55 +00006268#ifdef TARGET_ARM
6269 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00006270 if (!lock_user_struct(VERIFY_WRITE, target_efl, arg3, 0))
6271 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00006272 target_efl->l_type = tswap16(fl.l_type);
6273 target_efl->l_whence = tswap16(fl.l_whence);
6274 target_efl->l_start = tswap64(fl.l_start);
6275 target_efl->l_len = tswap64(fl.l_len);
6276 target_efl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00006277 unlock_user_struct(target_efl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00006278 } else
6279#endif
6280 {
bellard9ee1fa22007-11-11 15:11:19 +00006281 if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0))
6282 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00006283 target_fl->l_type = tswap16(fl.l_type);
6284 target_fl->l_whence = tswap16(fl.l_whence);
6285 target_fl->l_start = tswap64(fl.l_start);
6286 target_fl->l_len = tswap64(fl.l_len);
6287 target_fl->l_pid = tswapl(fl.l_pid);
pbrook53a59602006-03-25 19:31:22 +00006288 unlock_user_struct(target_fl, arg3, 1);
pbrookce4defa2006-02-09 16:49:55 +00006289 }
bellard77e46722003-04-29 20:39:06 +00006290 }
6291 break;
6292
thsb1e341e2007-03-20 21:50:52 +00006293 case TARGET_F_SETLK64:
6294 case TARGET_F_SETLKW64:
pbrookce4defa2006-02-09 16:49:55 +00006295#ifdef TARGET_ARM
6296 if (((CPUARMState *)cpu_env)->eabi) {
bellard9ee1fa22007-11-11 15:11:19 +00006297 if (!lock_user_struct(VERIFY_READ, target_efl, arg3, 1))
6298 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00006299 fl.l_type = tswap16(target_efl->l_type);
6300 fl.l_whence = tswap16(target_efl->l_whence);
6301 fl.l_start = tswap64(target_efl->l_start);
6302 fl.l_len = tswap64(target_efl->l_len);
6303 fl.l_pid = tswapl(target_efl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00006304 unlock_user_struct(target_efl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00006305 } else
6306#endif
6307 {
bellard9ee1fa22007-11-11 15:11:19 +00006308 if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1))
6309 goto efault;
pbrookce4defa2006-02-09 16:49:55 +00006310 fl.l_type = tswap16(target_fl->l_type);
6311 fl.l_whence = tswap16(target_fl->l_whence);
6312 fl.l_start = tswap64(target_fl->l_start);
6313 fl.l_len = tswap64(target_fl->l_len);
6314 fl.l_pid = tswapl(target_fl->l_pid);
pbrook53a59602006-03-25 19:31:22 +00006315 unlock_user_struct(target_fl, arg3, 0);
pbrookce4defa2006-02-09 16:49:55 +00006316 }
thsb1e341e2007-03-20 21:50:52 +00006317 ret = get_errno(fcntl(arg1, cmd, &fl));
bellard77e46722003-04-29 20:39:06 +00006318 break;
bellard60cd49d2003-03-16 22:53:56 +00006319 default:
bellard9ee1fa22007-11-11 15:11:19 +00006320 ret = do_fcntl(arg1, cmd, arg3);
bellard60cd49d2003-03-16 22:53:56 +00006321 break;
6322 }
bellard77e46722003-04-29 20:39:06 +00006323 break;
6324 }
bellard60cd49d2003-03-16 22:53:56 +00006325#endif
ths7d600c82006-12-08 01:32:58 +00006326#ifdef TARGET_NR_cacheflush
6327 case TARGET_NR_cacheflush:
6328 /* self-modifying code is handled automatically, so nothing needed */
6329 ret = 0;
6330 break;
6331#endif
bellardebc05482003-09-30 21:08:41 +00006332#ifdef TARGET_NR_security
bellard31e31b82003-02-18 22:55:36 +00006333 case TARGET_NR_security:
6334 goto unimplemented;
bellardebc05482003-09-30 21:08:41 +00006335#endif
bellardc573ff62004-01-04 15:51:36 +00006336#ifdef TARGET_NR_getpagesize
6337 case TARGET_NR_getpagesize:
6338 ret = TARGET_PAGE_SIZE;
6339 break;
6340#endif
bellard31e31b82003-02-18 22:55:36 +00006341 case TARGET_NR_gettid:
6342 ret = get_errno(gettid());
6343 break;
thse5febef2007-04-01 18:31:35 +00006344#ifdef TARGET_NR_readahead
bellard31e31b82003-02-18 22:55:36 +00006345 case TARGET_NR_readahead:
aurel322054ac92008-10-13 21:08:07 +00006346#if TARGET_ABI_BITS == 32
6347#ifdef TARGET_ARM
6348 if (((CPUARMState *)cpu_env)->eabi)
6349 {
6350 arg2 = arg3;
6351 arg3 = arg4;
6352 arg4 = arg5;
6353 }
6354#endif
6355 ret = get_errno(readahead(arg1, ((off64_t)arg3 << 32) | arg2, arg4));
6356#else
6357 ret = get_errno(readahead(arg1, arg2, arg3));
6358#endif
6359 break;
thse5febef2007-04-01 18:31:35 +00006360#endif
bellardebc05482003-09-30 21:08:41 +00006361#ifdef TARGET_NR_setxattr
bellard31e31b82003-02-18 22:55:36 +00006362 case TARGET_NR_setxattr:
6363 case TARGET_NR_lsetxattr:
6364 case TARGET_NR_fsetxattr:
6365 case TARGET_NR_getxattr:
6366 case TARGET_NR_lgetxattr:
6367 case TARGET_NR_fgetxattr:
6368 case TARGET_NR_listxattr:
6369 case TARGET_NR_llistxattr:
6370 case TARGET_NR_flistxattr:
6371 case TARGET_NR_removexattr:
6372 case TARGET_NR_lremovexattr:
6373 case TARGET_NR_fremovexattr:
bellard5cd43932003-03-29 16:54:36 +00006374 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00006375#endif
6376#ifdef TARGET_NR_set_thread_area
bellard5cd43932003-03-29 16:54:36 +00006377 case TARGET_NR_set_thread_area:
bellard8d18e892007-11-14 15:18:40 +00006378#if defined(TARGET_MIPS)
ths6f5b89a2007-03-02 20:48:00 +00006379 ((CPUMIPSState *) cpu_env)->tls_value = arg1;
6380 ret = 0;
6381 break;
edgar_iglef967792009-01-07 14:19:38 +00006382#elif defined(TARGET_CRIS)
6383 if (arg1 & 0xff)
6384 ret = -TARGET_EINVAL;
6385 else {
6386 ((CPUCRISState *) cpu_env)->pregs[PR_PID] = arg1;
6387 ret = 0;
6388 }
6389 break;
bellard8d18e892007-11-14 15:18:40 +00006390#elif defined(TARGET_I386) && defined(TARGET_ABI32)
6391 ret = do_set_thread_area(cpu_env, arg1);
6392 break;
ths6f5b89a2007-03-02 20:48:00 +00006393#else
6394 goto unimplemented_nowarn;
6395#endif
6396#endif
6397#ifdef TARGET_NR_get_thread_area
bellard5cd43932003-03-29 16:54:36 +00006398 case TARGET_NR_get_thread_area:
bellard8d18e892007-11-14 15:18:40 +00006399#if defined(TARGET_I386) && defined(TARGET_ABI32)
6400 ret = do_get_thread_area(cpu_env, arg1);
6401#else
bellard5cd43932003-03-29 16:54:36 +00006402 goto unimplemented_nowarn;
bellardebc05482003-09-30 21:08:41 +00006403#endif
bellard8d18e892007-11-14 15:18:40 +00006404#endif
bellard48dc41e2006-06-21 18:15:50 +00006405#ifdef TARGET_NR_getdomainname
6406 case TARGET_NR_getdomainname:
6407 goto unimplemented_nowarn;
6408#endif
ths6f5b89a2007-03-02 20:48:00 +00006409
thsb5906f92007-03-19 13:32:45 +00006410#ifdef TARGET_NR_clock_gettime
6411 case TARGET_NR_clock_gettime:
6412 {
6413 struct timespec ts;
6414 ret = get_errno(clock_gettime(arg1, &ts));
6415 if (!is_error(ret)) {
6416 host_to_target_timespec(arg2, &ts);
6417 }
6418 break;
6419 }
6420#endif
6421#ifdef TARGET_NR_clock_getres
6422 case TARGET_NR_clock_getres:
6423 {
6424 struct timespec ts;
6425 ret = get_errno(clock_getres(arg1, &ts));
6426 if (!is_error(ret)) {
6427 host_to_target_timespec(arg2, &ts);
6428 }
6429 break;
6430 }
6431#endif
pbrook63d76512008-05-29 13:43:29 +00006432#ifdef TARGET_NR_clock_nanosleep
6433 case TARGET_NR_clock_nanosleep:
6434 {
6435 struct timespec ts;
6436 target_to_host_timespec(&ts, arg3);
6437 ret = get_errno(clock_nanosleep(arg1, arg2, &ts, arg4 ? &ts : NULL));
6438 if (arg4)
6439 host_to_target_timespec(arg4, &ts);
6440 break;
6441 }
6442#endif
thsb5906f92007-03-19 13:32:45 +00006443
ths6f5b89a2007-03-02 20:48:00 +00006444#if defined(TARGET_NR_set_tid_address) && defined(__NR_set_tid_address)
6445 case TARGET_NR_set_tid_address:
bellard579a97f2007-11-11 14:26:47 +00006446 ret = get_errno(set_tid_address((int *)g2h(arg1)));
6447 break;
ths6f5b89a2007-03-02 20:48:00 +00006448#endif
6449
ths3ae43202007-09-16 21:39:48 +00006450#if defined(TARGET_NR_tkill) && defined(__NR_tkill)
ths4cae1d12007-07-12 11:06:53 +00006451 case TARGET_NR_tkill:
pbrook4cb05962008-05-30 18:05:19 +00006452 ret = get_errno(sys_tkill((int)arg1, target_to_host_signal(arg2)));
ths4cae1d12007-07-12 11:06:53 +00006453 break;
6454#endif
6455
ths3ae43202007-09-16 21:39:48 +00006456#if defined(TARGET_NR_tgkill) && defined(__NR_tgkill)
ths71455572007-06-21 21:45:30 +00006457 case TARGET_NR_tgkill:
pbrook4cb05962008-05-30 18:05:19 +00006458 ret = get_errno(sys_tgkill((int)arg1, (int)arg2,
6459 target_to_host_signal(arg3)));
ths71455572007-06-21 21:45:30 +00006460 break;
6461#endif
6462
ths4f2b1fe2007-06-21 21:57:12 +00006463#ifdef TARGET_NR_set_robust_list
6464 case TARGET_NR_set_robust_list:
6465 goto unimplemented_nowarn;
6466#endif
6467
ths9007f0e2007-09-25 17:50:37 +00006468#if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
6469 case TARGET_NR_utimensat:
6470 {
6471 struct timespec ts[2];
6472 target_to_host_timespec(ts, arg3);
6473 target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
6474 if (!arg2)
6475 ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
6476 else {
bellard579a97f2007-11-11 14:26:47 +00006477 if (!(p = lock_user_string(arg2))) {
ths0da46a62007-10-20 20:23:07 +00006478 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +00006479 goto fail;
6480 }
6481 ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
6482 unlock_user(p, arg2, 0);
ths9007f0e2007-09-25 17:50:37 +00006483 }
6484 }
6485 break;
6486#endif
pbrookbd0c5662008-05-29 14:34:11 +00006487#if defined(USE_NPTL)
6488 case TARGET_NR_futex:
6489 ret = do_futex(arg1, arg2, arg3, arg4, arg5, arg6);
6490 break;
6491#endif
aurel32dbfe4c32009-04-08 21:29:30 +00006492#if defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)
aurel3239b59762008-10-01 21:46:50 +00006493 case TARGET_NR_inotify_init:
6494 ret = get_errno(sys_inotify_init());
6495 break;
6496#endif
aurel32dbfe4c32009-04-08 21:29:30 +00006497#if defined(TARGET_NR_inotify_add_watch) && defined(__NR_inotify_add_watch)
aurel3239b59762008-10-01 21:46:50 +00006498 case TARGET_NR_inotify_add_watch:
6499 p = lock_user_string(arg2);
6500 ret = get_errno(sys_inotify_add_watch(arg1, path(p), arg3));
6501 unlock_user(p, arg2, 0);
6502 break;
6503#endif
aurel32dbfe4c32009-04-08 21:29:30 +00006504#if defined(TARGET_NR_inotify_rm_watch) && defined(__NR_inotify_rm_watch)
aurel3239b59762008-10-01 21:46:50 +00006505 case TARGET_NR_inotify_rm_watch:
6506 ret = get_errno(sys_inotify_rm_watch(arg1, arg2));
6507 break;
6508#endif
ths9007f0e2007-09-25 17:50:37 +00006509
aurel3224e10032009-04-15 16:11:43 +00006510#ifdef TARGET_NR_mq_open
6511 case TARGET_NR_mq_open:
6512 {
6513 struct mq_attr posix_mq_attr;
6514
6515 p = lock_user_string(arg1 - 1);
6516 if (arg4 != 0)
6517 copy_from_user_mq_attr (&posix_mq_attr, arg4);
6518 ret = get_errno(mq_open(p, arg2, arg3, &posix_mq_attr));
6519 unlock_user (p, arg1, 0);
6520 }
6521 break;
6522
6523 case TARGET_NR_mq_unlink:
6524 p = lock_user_string(arg1 - 1);
6525 ret = get_errno(mq_unlink(p));
6526 unlock_user (p, arg1, 0);
6527 break;
6528
6529 case TARGET_NR_mq_timedsend:
6530 {
6531 struct timespec ts;
6532
6533 p = lock_user (VERIFY_READ, arg2, arg3, 1);
6534 if (arg5 != 0) {
6535 target_to_host_timespec(&ts, arg5);
6536 ret = get_errno(mq_timedsend(arg1, p, arg3, arg4, &ts));
6537 host_to_target_timespec(arg5, &ts);
6538 }
6539 else
6540 ret = get_errno(mq_send(arg1, p, arg3, arg4));
6541 unlock_user (p, arg2, arg3);
6542 }
6543 break;
6544
6545 case TARGET_NR_mq_timedreceive:
6546 {
6547 struct timespec ts;
6548 unsigned int prio;
6549
6550 p = lock_user (VERIFY_READ, arg2, arg3, 1);
6551 if (arg5 != 0) {
6552 target_to_host_timespec(&ts, arg5);
6553 ret = get_errno(mq_timedreceive(arg1, p, arg3, &prio, &ts));
6554 host_to_target_timespec(arg5, &ts);
6555 }
6556 else
6557 ret = get_errno(mq_receive(arg1, p, arg3, &prio));
6558 unlock_user (p, arg2, arg3);
6559 if (arg4 != 0)
6560 put_user_u32(prio, arg4);
6561 }
6562 break;
6563
6564 /* Not implemented for now... */
6565/* case TARGET_NR_mq_notify: */
6566/* break; */
6567
6568 case TARGET_NR_mq_getsetattr:
6569 {
6570 struct mq_attr posix_mq_attr_in, posix_mq_attr_out;
6571 ret = 0;
6572 if (arg3 != 0) {
6573 ret = mq_getattr(arg1, &posix_mq_attr_out);
6574 copy_to_user_mq_attr(arg3, &posix_mq_attr_out);
6575 }
6576 if (arg2 != 0) {
6577 copy_from_user_mq_attr(&posix_mq_attr_in, arg2);
6578 ret |= mq_setattr(arg1, &posix_mq_attr_in, &posix_mq_attr_out);
6579 }
6580
6581 }
6582 break;
6583#endif
6584
bellard31e31b82003-02-18 22:55:36 +00006585 default:
6586 unimplemented:
bellard5cd43932003-03-29 16:54:36 +00006587 gemu_log("qemu: Unsupported syscall: %d\n", num);
ths4f2b1fe2007-06-21 21:57:12 +00006588#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 +00006589 unimplemented_nowarn:
bellard80a9d032005-01-03 23:31:27 +00006590#endif
ths0da46a62007-10-20 20:23:07 +00006591 ret = -TARGET_ENOSYS;
bellard31e31b82003-02-18 22:55:36 +00006592 break;
6593 }
bellard579a97f2007-11-11 14:26:47 +00006594fail:
bellardc573ff62004-01-04 15:51:36 +00006595#ifdef DEBUG
6596 gemu_log(" = %ld\n", ret);
6597#endif
thsb92c47c2007-11-01 00:07:38 +00006598 if(do_strace)
6599 print_syscall_ret(num, ret);
bellard31e31b82003-02-18 22:55:36 +00006600 return ret;
bellard579a97f2007-11-11 14:26:47 +00006601efault:
6602 ret = -TARGET_EFAULT;
6603 goto fail;
bellard31e31b82003-02-18 22:55:36 +00006604}